@@ -45,8 +45,13 @@ import { normalizeBasePath } from "./navigation.ts";
4545import {
4646 areUiSessionKeysEquivalent ,
4747 DEFAULT_AGENT_ID ,
48+ isUiGlobalSessionKey ,
4849 normalizeAgentId ,
4950 parseAgentSessionKey ,
51+ resolveUiDefaultAgentId ,
52+ resolveUiGlobalAliasAgentId ,
53+ resolveUiKnownSelectedGlobalAgentId ,
54+ resolveUiSelectedGlobalAgentId ,
5055} from "./session-key.ts" ;
5156import { isSessionRunActive } from "./session-run-state.ts" ;
5257import { normalizeLowercaseStringOrEmpty , normalizeOptionalString } from "./string-coerce.ts" ;
@@ -219,61 +224,17 @@ function isBtwCommand(text: string) {
219224 return / ^ \/ (?: b t w | s i d e ) (?: : | \s | $ ) / i. test ( text . trim ( ) ) ;
220225}
221226
222- function isGlobalSessionKey ( sessionKey : string | undefined | null ) : boolean {
223- return normalizeLowercaseStringOrEmpty ( sessionKey ) === "global" ;
224- }
225-
226227function readHelloDefaultAgentId ( host : Pick < ChatHost , "hello" > ) : string | undefined {
227228 const snapshot = host . hello ?. snapshot as
228229 | { sessionDefaults ?: SessionDefaultsSnapshot }
229230 | undefined ;
230231 return snapshot ?. sessionDefaults ?. defaultAgentId ?. trim ( ) || undefined ;
231232}
232233
233- function readHelloMainKey ( host : Pick < ChatHost , "hello" > ) : string | undefined {
234- const snapshot = host . hello ?. snapshot as
235- | { sessionDefaults ?: SessionDefaultsSnapshot }
236- | undefined ;
237- return snapshot ?. sessionDefaults ?. mainKey ?. trim ( ) || undefined ;
238- }
239-
240- function resolveGlobalAliasAgentId (
241- host : Pick < ChatHost , "agentsList" | "hello" > ,
242- sessionKey : string | undefined | null ,
243- ) : string | undefined {
244- const parsed = parseAgentSessionKey ( sessionKey ) ;
245- if ( ! parsed ) {
246- return undefined ;
247- }
248- const rest = normalizeLowercaseStringOrEmpty ( parsed . rest ) ;
249- const configuredMainKey = normalizeLowercaseStringOrEmpty (
250- host . agentsList ?. mainKey ?? readHelloMainKey ( host ) ?? "main" ,
251- ) ;
252- return rest === "global" || rest === "main" || rest === configuredMainKey
253- ? normalizeAgentId ( parsed . agentId )
254- : undefined ;
255- }
256-
257- function resolveSelectedGlobalAgentId (
258- host : Pick < ChatHost , "assistantAgentId" | "agentsList" | "hello" > ,
259- ) : string | undefined {
260- const agentId =
261- host . assistantAgentId ?. trim ( ) ||
262- host . agentsList ?. defaultId ?. trim ( ) ||
263- readHelloDefaultAgentId ( host ) ;
264- return agentId ? normalizeAgentId ( agentId ) : undefined ;
265- }
266-
267- function resolveDefaultAgentIdForList ( host : Pick < ChatHost , "agentsList" | "hello" > ) : string {
268- return normalizeAgentId (
269- host . agentsList ?. defaultId ?? readHelloDefaultAgentId ( host ) ?? DEFAULT_AGENT_ID ,
270- ) ;
271- }
272-
273234function scopedAgentIdForSession ( host : ChatHost , sessionKey : string | undefined | null ) {
274- return isGlobalSessionKey ( sessionKey )
275- ? resolveSelectedGlobalAgentId ( host )
276- : resolveGlobalAliasAgentId ( host , sessionKey ) ;
235+ return isUiGlobalSessionKey ( sessionKey )
236+ ? resolveUiKnownSelectedGlobalAgentId ( host )
237+ : ( resolveUiGlobalAliasAgentId ( host , sessionKey ) ?? undefined ) ;
277238}
278239
279240function visibleSessionMatches (
@@ -282,19 +243,19 @@ function visibleSessionMatches(
282243 agentId : string | undefined ,
283244) : boolean {
284245 if ( host . sessionKey !== sessionKey ) {
285- const hostAliasAgentId = resolveGlobalAliasAgentId ( host , host . sessionKey ) ;
286- if ( ! hostAliasAgentId || ! isGlobalSessionKey ( sessionKey ) ) {
246+ const hostAliasAgentId = resolveUiGlobalAliasAgentId ( host , host . sessionKey ) ;
247+ if ( ! hostAliasAgentId || ! isUiGlobalSessionKey ( sessionKey ) ) {
287248 return false ;
288249 }
289250 const expectedAgentId = agentId ?? host . agentsList ?. defaultId ?? readHelloDefaultAgentId ( host ) ;
290251 return expectedAgentId
291252 ? hostAliasAgentId === normalizeAgentId ( expectedAgentId )
292253 : hostAliasAgentId === normalizeAgentId ( "main" ) ;
293254 }
294- if ( ! isGlobalSessionKey ( sessionKey ) ) {
255+ if ( ! isUiGlobalSessionKey ( sessionKey ) ) {
295256 return true ;
296257 }
297- const selectedAgentId = resolveSelectedGlobalAgentId ( host ) ;
258+ const selectedAgentId = resolveUiKnownSelectedGlobalAgentId ( host ) ;
298259 const expectedAgentId = agentId ?? host . agentsList ?. defaultId ?? readHelloDefaultAgentId ( host ) ;
299260 return expectedAgentId
300261 ? selectedAgentId === normalizeAgentId ( expectedAgentId )
@@ -305,9 +266,9 @@ export function scopedAgentParamsForSession(
305266 host : Pick < ChatHost , "assistantAgentId" | "agentsList" | "hello" > ,
306267 sessionKey : string ,
307268) {
308- const agentId = isGlobalSessionKey ( sessionKey )
309- ? resolveSelectedGlobalAgentId ( host )
310- : resolveGlobalAliasAgentId ( host , sessionKey ) ;
269+ const agentId = isUiGlobalSessionKey ( sessionKey )
270+ ? resolveUiKnownSelectedGlobalAgentId ( host )
271+ : resolveUiGlobalAliasAgentId ( host , sessionKey ) ;
311272 return agentId ? { agentId } : { } ;
312273}
313274
@@ -320,10 +281,10 @@ export function scopedAgentListParamsForSession(
320281 const agentId =
321282 parsed ?. agentId ??
322283 ( normalizedSessionKey === "global"
323- ? resolveSelectedGlobalAgentId ( host )
284+ ? resolveUiKnownSelectedGlobalAgentId ( host )
324285 : normalizedSessionKey === "unknown"
325286 ? undefined
326- : resolveDefaultAgentIdForList ( host ) ) ;
287+ : resolveUiDefaultAgentId ( host ) ) ;
327288 return agentId ? { agentId : normalizeAgentId ( agentId ) } : { } ;
328289}
329290
@@ -986,8 +947,8 @@ function isHistorySessionInfoForRequestedSession(
986947 }
987948 return Boolean (
988949 historySessionKey &&
989- isGlobalSessionKey ( historySessionKey ) &&
990- resolveGlobalAliasAgentId ( host , requestedSessionKey ) ,
950+ isUiGlobalSessionKey ( historySessionKey ) &&
951+ resolveUiGlobalAliasAgentId ( host , requestedSessionKey ) ,
991952 ) ;
992953}
993954
@@ -998,16 +959,16 @@ function findSelectedSessionRow(
998959 historySessionKey : string | undefined ,
999960) : GatewaySessionRow | undefined {
1000961 const requestedGlobalAgentId =
1001- historySessionKey && isGlobalSessionKey ( historySessionKey )
1002- ? resolveGlobalAliasAgentId ( host , sessionKey )
962+ historySessionKey && isUiGlobalSessionKey ( historySessionKey )
963+ ? resolveUiGlobalAliasAgentId ( host , sessionKey )
1003964 : undefined ;
1004965 return sessionsResult ?. sessions . find ( ( session ) => {
1005966 if ( areUiSessionKeysEquivalent ( session . key , sessionKey ) ) {
1006967 return true ;
1007968 }
1008969 return (
1009970 requestedGlobalAgentId != null &&
1010- resolveGlobalAliasAgentId ( host , session . key ) === requestedGlobalAgentId
971+ resolveUiGlobalAliasAgentId ( host , session . key ) === requestedGlobalAgentId
1011972 ) ;
1012973 } ) ;
1013974}
@@ -1558,8 +1519,8 @@ function resolveAgentIdForSession(host: ChatHost): string | null {
15581519 if ( parsed ?. agentId ) {
15591520 return parsed . agentId ;
15601521 }
1561- if ( isGlobalSessionKey ( host . sessionKey ) ) {
1562- return resolveSelectedGlobalAgentId ( host ) || DEFAULT_AGENT_ID ;
1522+ if ( isUiGlobalSessionKey ( host . sessionKey ) ) {
1523+ return resolveUiSelectedGlobalAgentId ( host ) || DEFAULT_AGENT_ID ;
15631524 }
15641525 return readHelloDefaultAgentId ( host ) || DEFAULT_AGENT_ID ;
15651526}
0 commit comments