@@ -15,20 +15,17 @@ import {
1515 resolveDefaultAgentId ,
1616 resolveSessionAgentId ,
1717} from "../../agents/agent-scope.js" ;
18- import type { ModelCatalogEntry , ModelCatalogSnapshot } from "../../agents/model-catalog.types.js" ;
1918import {
2019 isSessionTranscriptProjectionUnavailableError ,
2120 resolveTranscriptSessionKeyBySessionId ,
2221} from "../../config/sessions/session-accessor.js" ;
23- import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
2422import {
2523 measureDiagnosticsTimelineSpan ,
2624 measureDiagnosticsTimelineSpanSync ,
2725} from "../../infra/diagnostics-timeline.js" ;
2826import { formatErrorMessage } from "../../infra/errors.js" ;
2927import { jsonUtf8Bytes } from "../../infra/json-utf8-bytes.js" ;
3028import { normalizeAgentId , scopeLegacySessionKeyToAgent } from "../../routing/session-key.js" ;
31- import { listGatewayAgentsBasic } from "../agent-list.js" ;
3229import {
3330 boundInFlightRunSnapshotForChatHistory ,
3431 resolveInFlightRunSnapshot ,
@@ -59,7 +56,7 @@ import {
5956} from "./chat-history-pages.js" ;
6057import type { ChatMetadataResult } from "./chat-metadata-runtime.js" ;
6158import { resolveRequestedChatAgentId , validateChatSelectedAgent } from "./chat-origin-routing.js" ;
62- import { listMemoizedChatStartupAgents } from "./chat-startup-projection-memo .js" ;
59+ import type { ChatStartupProjectionResult } from "./chat-startup-projection-contract .js" ;
6360import { normalizeOptionalChatText as normalizeOptionalText } from "./chat-text-normalization.js" ;
6461import {
6562 loadOptionalServerMethodModelCatalogSnapshot ,
@@ -101,72 +98,6 @@ async function handleChatMetadataRequest({
10198 ) ;
10299}
103100
104- async function buildChatStartupModelCatalogProjection ( params : {
105- cfg : OpenClawConfig ;
106- snapshot : ModelCatalogSnapshot ;
107- sessionAgentId : string ;
108- sessionEntry : ReturnType < typeof loadSessionEntryReadOnly > [ "entry" ] ;
109- defaultAgentId : string ;
110- includeAgentsList : boolean ;
111- } ) {
112- const { createGatewayAgentModelCatalogProjector } = await import ( "./models-list-result.js" ) ;
113- const projectorByKey = new Map <
114- string ,
115- ReturnType < typeof createGatewayAgentModelCatalogProjector >
116- > ( ) ;
117- const modelCatalogByAgentId = new Map < string , ModelCatalogEntry [ ] > ( ) ;
118- const getProjector = (
119- agentId : string ,
120- profiles : { preferredProfileId ?: string ; lockedProfileId ?: string } = { } ,
121- ) => {
122- const id = normalizeAgentId ( agentId ) ;
123- const key = `${ id } \0${ profiles . preferredProfileId ?? "" } \0${ profiles . lockedProfileId ?? "" } ` ;
124- let projector = projectorByKey . get ( key ) ;
125- if ( ! projector ) {
126- projector = createGatewayAgentModelCatalogProjector ( {
127- cfg : params . cfg ,
128- agentId : id ,
129- snapshot : params . snapshot ,
130- ...( profiles . preferredProfileId ? { preferredProfileId : profiles . preferredProfileId } : { } ) ,
131- ...( profiles . lockedProfileId ? { lockedProfileId : profiles . lockedProfileId } : { } ) ,
132- } ) ;
133- projectorByKey . set ( key , projector ) ;
134- }
135- return projector ;
136- } ;
137- const agentIds = new Set ( [ params . sessionAgentId , params . defaultAgentId ] . map ( normalizeAgentId ) ) ;
138- // Agents-list catalogs are profile-neutral. Session auth shapes only the separate
139- // sessionCatalogProjector below, so switching sessions cannot alter this map.
140- if ( params . includeAgentsList ) {
141- for ( const agent of listGatewayAgentsBasic ( params . cfg ) . agents ) {
142- agentIds . add ( agent . id ) ;
143- }
144- }
145- await Promise . all (
146- [ ...agentIds ] . map ( async ( agentId ) => {
147- modelCatalogByAgentId . set ( agentId , await getProjector ( agentId ) . projectCatalog ( ) ) ;
148- } ) ,
149- ) ;
150- const sessionProfileId = params . sessionEntry ?. authProfileOverride ?. trim ( ) ;
151- const sessionProfileSource = params . sessionEntry ?. authProfileOverrideSource ;
152- // Legacy rows omitted the source; a compaction count is the durable marker
153- // that the profile was adopted automatically and may fall through.
154- const legacyUserProfile =
155- sessionProfileSource === undefined &&
156- params . sessionEntry ?. authProfileOverrideCompactionCount === undefined ;
157- const sessionProfiles = sessionProfileId
158- ? {
159- preferredProfileId : sessionProfileId ,
160- ...( sessionProfileSource === "user" || legacyUserProfile
161- ? { lockedProfileId : sessionProfileId }
162- : { } ) ,
163- }
164- : undefined ;
165- const sessionCatalogProjector = getProjector ( params . sessionAgentId , sessionProfiles ) ;
166- const sessionModelCatalog = await sessionCatalogProjector . projectCatalog ( ) ;
167- return { getProjector, modelCatalogByAgentId, sessionCatalogProjector, sessionModelCatalog } ;
168- }
169-
170101// The UI fills metadata gaps as soon as chat.startup returns, so history never waits
171102// beyond this budget for a catalog snapshot that requires slower discovery.
172103const CHAT_OPTIONAL_MODEL_CATALOG_TIMEOUT_MS = 25 ;
@@ -315,23 +246,32 @@ async function handleChatHistoryRequest({
315246 return ;
316247 }
317248 }
318- const optionalModelCatalogLoad = startOptionalServerMethodModelCatalogSnapshotLoad ( context , {
319- agentId : sessionAgentId ,
320- } ) ;
321- const modelCatalogPromise = measureDiagnosticsTimelineSpan (
322- `gateway.${ method } .model_catalog` ,
323- ( ) =>
324- loadOptionalServerMethodModelCatalogSnapshot ( context , method , {
325- logOnceKey : method ,
326- startedLoad : optionalModelCatalogLoad ,
327- timeoutMs : CHAT_OPTIONAL_MODEL_CATALOG_TIMEOUT_MS ,
328- } ) ,
329- {
330- config : cfg ,
331- phase : method ,
332- } ,
333- ) ;
334- void modelCatalogPromise . catch ( ( ) => undefined ) ;
249+ const modelCatalogPromise =
250+ method === "chat.history"
251+ ? ( ( ) => {
252+ const optionalModelCatalogLoad = startOptionalServerMethodModelCatalogSnapshotLoad (
253+ context ,
254+ {
255+ agentId : sessionAgentId ,
256+ } ,
257+ ) ;
258+ const load = measureDiagnosticsTimelineSpan (
259+ `gateway.${ method } .model_catalog` ,
260+ ( ) =>
261+ loadOptionalServerMethodModelCatalogSnapshot ( context , method , {
262+ logOnceKey : method ,
263+ startedLoad : optionalModelCatalogLoad ,
264+ timeoutMs : CHAT_OPTIONAL_MODEL_CATALOG_TIMEOUT_MS ,
265+ } ) ,
266+ {
267+ config : cfg ,
268+ phase : method ,
269+ } ,
270+ ) ;
271+ void load . catch ( ( ) => undefined ) ;
272+ return load ;
273+ } ) ( )
274+ : Promise . resolve ( undefined ) ;
335275 const sessionId = requestedSessionId ?? entry ?. sessionId ;
336276 const historyEntry =
337277 requestedSessionId && requestedSessionId !== entry ?. sessionId ? undefined : entry ;
@@ -438,53 +378,46 @@ async function handleChatHistoryRequest({
438378 const catalogConfig = catalogOwnedBySessionAgent ? modelCatalogSnapshot . config : cfg ;
439379 const modelCatalog = catalogOwnedBySessionAgent ? modelCatalogSnapshot . entries : undefined ;
440380 const defaultAgentId = resolveDefaultAgentId ( catalogConfig ) ;
441- let startupCatalogProjection :
442- | Awaited < ReturnType < typeof buildChatStartupModelCatalogProjection > >
443- | undefined ;
381+ let startupProjection : ChatStartupProjectionResult | undefined ;
444382 let startupMetadata : ChatMetadataResult | undefined ;
445383 let startupAgentsList : ReturnType < typeof listAgentsForGateway > | undefined ;
446384 if ( method === "chat.startup" ) {
447385 const includeSystem = hasGatewayClientCap ( client ?. connect . caps , GATEWAY_CLIENT_CAPS . AGENT_KIND ) ;
448386 const startupProjections = await measureDiagnosticsTimelineSpan (
449387 `gateway.${ method } .startup_projections` ,
450388 async ( ) => {
451- const catalogProjection = catalogOwnedBySessionAgent
452- ? await buildChatStartupModelCatalogProjection ( {
453- cfg : catalogConfig ,
454- snapshot : modelCatalogSnapshot ,
455- sessionAgentId,
456- sessionEntry : entry ,
457- defaultAgentId,
458- includeAgentsList : includeAgentsList === true ,
459- } )
460- : undefined ;
461- const metadata =
462- includeMetadata && catalogOwnedBySessionAgent
463- ? await context
464- . readChatMetadata ( {
465- agentId : sessionAgentId ,
466- sessionEntry : entry ,
467- } )
468- . catch ( ( error : unknown ) => {
469- context . logGateway . debug (
470- `chat.startup continuing without metadata: ${ formatErrorMessage ( error ) } ` ,
471- ) ;
472- return undefined ;
473- } )
474- : undefined ;
475- const agentsList = includeAgentsList
476- ? catalogProjection && modelCatalog && modelCatalogSnapshot
477- ? listMemoizedChatStartupAgents ( {
478- cfg,
479- context,
389+ const projection = context . readChatStartupProjection
390+ ? await context
391+ . readChatStartupProjection ( {
392+ agentId : sessionAgentId ,
393+ sessionEntry : entry ,
480394 includeSystem,
481- catalogSnapshot : modelCatalogSnapshot ,
482- modelCatalog,
483- modelCatalogByAgentId : catalogProjection . modelCatalogByAgentId ,
484395 } )
485- : listAgentsForGateway ( cfg , modelCatalog , { includeSystem } )
396+ . catch ( ( error : unknown ) => {
397+ context . logGateway . debug (
398+ `chat.startup continuing without prepared startup projection: ${ formatErrorMessage ( error ) } ` ,
399+ ) ;
400+ return undefined ;
401+ } )
402+ : undefined ;
403+ const metadata = includeMetadata
404+ ? ( projection ?. metadata ??
405+ ( await context
406+ . readChatMetadata ( {
407+ agentId : sessionAgentId ,
408+ sessionEntry : entry ,
409+ } )
410+ . catch ( ( error : unknown ) => {
411+ context . logGateway . debug (
412+ `chat.startup continuing without metadata: ${ formatErrorMessage ( error ) } ` ,
413+ ) ;
414+ return undefined ;
415+ } ) ) )
416+ : undefined ;
417+ const agentsList = includeAgentsList
418+ ? ( projection ?. agentsList ?? listAgentsForGateway ( cfg , modelCatalog , { includeSystem } ) )
486419 : undefined ;
487- return { agentsList, catalogProjection , metadata } ;
420+ return { agentsList, projection , metadata } ;
488421 } ,
489422 {
490423 config : cfg ,
@@ -495,14 +428,12 @@ async function handleChatHistoryRequest({
495428 } ,
496429 } ,
497430 ) ;
498- startupCatalogProjection = startupProjections . catalogProjection ;
431+ startupProjection = startupProjections . projection ;
499432 startupMetadata = startupProjections . metadata ;
500433 startupAgentsList = startupProjections . agentsList ;
501434 }
502- const sessionModelCatalog = startupCatalogProjection ?. sessionModelCatalog ?? modelCatalog ;
503- const defaultModelCatalog =
504- startupCatalogProjection ?. modelCatalogByAgentId . get ( normalizeAgentId ( defaultAgentId ) ) ??
505- modelCatalog ;
435+ const sessionModelCatalog = startupProjection ?. sessionModelCatalog ?? modelCatalog ;
436+ const defaultModelCatalog = startupProjection ?. defaultModelCatalog ?? modelCatalog ;
506437 const sessionInfo = measureDiagnosticsTimelineSpanSync (
507438 `gateway.${ method } .session_info` ,
508439 ( ) =>
0 commit comments