@@ -113,6 +113,11 @@ const ProjectionThreadActivityIdRowSchema = Schema.Struct({
113113 activityId : ProjectionThreadActivity . fields . activityId ,
114114} ) ;
115115const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession ;
116+ const ProjectionThreadRuntimeContextDbRowSchema = Schema . Struct ( {
117+ id : ThreadId ,
118+ title : Schema . String ,
119+ session : Schema . NullOr ( ProjectionThreadSessionDbRowSchema ) ,
120+ } ) ;
116121const ProjectionCheckpointDbRowSchema = ProjectionCheckpoint . mapFields (
117122 Struct . assign ( {
118123 files : Schema . fromJsonString ( Schema . Array ( OrchestrationCheckpointFile ) ) ,
@@ -1038,6 +1043,40 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10381043 ` ,
10391044 } ) ;
10401045
1046+ const getThreadRuntimeContextRow = SqlSchema . findOneOption ( {
1047+ Request : ThreadIdLookupInput ,
1048+ Result : ProjectionThreadRuntimeContextDbRowSchema ,
1049+ execute : ( { threadId } ) =>
1050+ sql `
1051+ SELECT
1052+ threads.thread_id AS id,
1053+ threads.title,
1054+ sessions.thread_id AS "threadId",
1055+ sessions.status,
1056+ sessions.provider_name AS "providerName",
1057+ sessions.provider_instance_id AS "providerInstanceId",
1058+ sessions.runtime_mode AS "runtimeMode",
1059+ sessions.active_turn_id AS "activeTurnId",
1060+ sessions.last_error AS "lastError",
1061+ sessions.updated_at AS "updatedAt"
1062+ FROM projection_threads AS threads
1063+ LEFT JOIN projection_thread_sessions AS sessions
1064+ ON sessions.thread_id = threads.thread_id
1065+ WHERE threads.thread_id = ${ threadId }
1066+ AND threads.deleted_at IS NULL
1067+ AND threads.archived_at IS NULL
1068+ LIMIT 1
1069+ ` . pipe (
1070+ Effect . map ( ( rows ) =>
1071+ rows . map ( ( row ) => ( {
1072+ id : row . id ,
1073+ title : row . title ,
1074+ session : row . threadId === null ? null : row ,
1075+ } ) ) ,
1076+ ) ,
1077+ ) ,
1078+ } ) ;
1079+
10411080 const listThreadMessageRowsByThread = SqlSchema . findAll ( {
10421081 Request : ThreadIdLookupInput ,
10431082 Result : ProjectionThreadMessageDbRowSchema ,
@@ -2764,6 +2803,23 @@ pending_approval_requests AS (
27642803 } satisfies OrchestrationThreadShell ) ;
27652804 } ) ;
27662805
2806+ const getThreadRuntimeContext : ProjectionSnapshotQueryShape [ "getThreadRuntimeContext" ] =
2807+ Effect . fn ( "ProjectionSnapshotQuery.getThreadRuntimeContext" ) ( function * ( threadId ) {
2808+ const context = yield * getThreadRuntimeContextRow ( { threadId } ) . pipe (
2809+ Effect . mapError (
2810+ toPersistenceSqlOrDecodeError (
2811+ "ProjectionSnapshotQuery.getThreadRuntimeContext:query" ,
2812+ "ProjectionSnapshotQuery.getThreadRuntimeContext:decodeRow" ,
2813+ ) ,
2814+ ) ,
2815+ ) ;
2816+ return Option . map ( context , ( row ) => ( {
2817+ id : row . id ,
2818+ title : row . title ,
2819+ session : row . session === null ? null : mapSessionRow ( row . session ) ,
2820+ } ) ) ;
2821+ } ) ;
2822+
27672823 // Contiguous turn range bounding a windowed detail read; undefined loads the
27682824 // full thread. Resolved from a window request inside the snapshot
27692825 // transaction (see getThreadDetailSnapshot).
@@ -3200,6 +3256,7 @@ pending_approval_requests AS (
32003256 getThreadCheckpointContext,
32013257 getFullThreadDiffContext,
32023258 getThreadShellById,
3259+ getThreadRuntimeContext,
32033260 getThreadDetailById,
32043261 getThreadDetailSnapshot,
32053262 } satisfies ProjectionSnapshotQueryShape ;
0 commit comments