44 CommandId ,
55 MessageId ,
66 type OrchestrationEvent ,
7- type OrchestrationMessage ,
87 type OrchestrationProposedPlanId ,
98 CheckpointRef ,
109 classifyTaskAgentKind ,
@@ -15,7 +14,6 @@ import {
1514 TurnId ,
1615 type OrchestrationCheckpointSummary ,
1716 type OrchestrationProposedPlan ,
18- type OrchestrationThread ,
1917 type OrchestrationThreadActivity ,
2018 type ProviderRuntimeEvent ,
2119} from "@t3tools/contracts" ;
@@ -32,6 +30,12 @@ import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
3230import { ProviderService } from "../../provider/Services/ProviderService.ts" ;
3331import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts" ;
3432import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts" ;
33+ import { ProjectionThreadActivityRepository } from "../../persistence/Services/ProjectionThreadActivities.ts" ;
34+ import { ProjectionThreadActivityRepositoryLive } from "../../persistence/Layers/ProjectionThreadActivities.ts" ;
35+ import { ProjectionThreadMessageRepository } from "../../persistence/Services/ProjectionThreadMessages.ts" ;
36+ import { ProjectionThreadMessageRepositoryLive } from "../../persistence/Layers/ProjectionThreadMessages.ts" ;
37+ import { ProjectionThreadProposedPlanRepository } from "../../persistence/Services/ProjectionThreadProposedPlans.ts" ;
38+ import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/Layers/ProjectionThreadProposedPlans.ts" ;
3539import { isGitRepository } from "../../git/Utils.ts" ;
3640import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts" ;
3741import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts" ;
@@ -53,7 +57,7 @@ const providerTaskKey = (threadId: ThreadId, taskId: string) => `${threadId}:${t
5357// (server restart, session-exit sweep, TTL/capacity eviction): earlier
5458// task.started/task.progress activities for the task are persisted with it.
5559function findTaskTitleInActivities (
56- activities : ReadonlyArray < OrchestrationThreadActivity > | undefined ,
60+ activities : ReadonlyArray < { readonly kind : string ; readonly payload : unknown } > | undefined ,
5761 taskId : string ,
5862) : string | undefined {
5963 if ( ! activities ) {
@@ -131,40 +135,6 @@ function sameId(left: string | null | undefined, right: string | null | undefine
131135 return left === right ;
132136}
133137
134- function hasAssistantMessageForTurn (
135- messages : ReadonlyArray < OrchestrationMessage > ,
136- turnId : TurnId ,
137- options ?: { readonly streamingOnly ?: boolean } ,
138- ) : boolean {
139- for ( let index = 0 ; index < messages . length ; index += 1 ) {
140- const message = messages [ index ] ;
141- if ( ! message ) {
142- continue ;
143- }
144- if ( message . role !== "assistant" || message . turnId !== turnId ) {
145- continue ;
146- }
147- if ( options ?. streamingOnly === true && ! message . streaming ) {
148- continue ;
149- }
150- return true ;
151- }
152- return false ;
153- }
154-
155- function findMessageById (
156- messages : ReadonlyArray < OrchestrationMessage > ,
157- messageId : MessageId ,
158- ) : OrchestrationMessage | undefined {
159- for ( let index = 0 ; index < messages . length ; index += 1 ) {
160- const message = messages [ index ] ;
161- if ( message ?. id === messageId ) {
162- return message ;
163- }
164- }
165- return undefined ;
166- }
167-
168138function findProposedPlanById (
169139 proposedPlans : ReadonlyArray <
170140 Pick < OrchestrationProposedPlan , "id" | "createdAt" | "implementedAt" | "implementationThreadId" >
@@ -894,6 +864,9 @@ const make = Effect.gen(function* () {
894864 const orchestrationEngine = yield * OrchestrationEngineService ;
895865 const projectionSnapshotQuery = yield * ProjectionSnapshotQuery ;
896866 const providerService = yield * ProviderService ;
867+ const projectionThreadActivities = yield * ProjectionThreadActivityRepository ;
868+ const projectionThreadMessages = yield * ProjectionThreadMessageRepository ;
869+ const projectionThreadProposedPlans = yield * ProjectionThreadProposedPlanRepository ;
897870 const projectionTurnRepository = yield * ProjectionTurnRepository ;
898871 const serverSettingsService = yield * ServerSettingsService ;
899872 const providerCommandId = ( event : ProviderRuntimeEvent , tag : string ) =>
@@ -949,18 +922,39 @@ const make = Effect.gen(function* () {
949922 ) ,
950923 ) ;
951924
952- const resolveThreadDetail = Effect . fn ( "resolveThreadDetail" ) ( function * ( threadId : ThreadId ) {
953- return yield * projectionSnapshotQuery
954- . getThreadDetailById ( threadId )
955- . pipe ( Effect . map ( Option . getOrUndefined ) ) ;
956- } ) ;
957-
958925 const resolveThreadShell = Effect . fn ( "resolveThreadShell" ) ( function * ( threadId : ThreadId ) {
959926 return yield * projectionSnapshotQuery
960927 . getThreadShellById ( threadId )
961928 . pipe ( Effect . map ( Option . getOrUndefined ) ) ;
962929 } ) ;
963930
931+ const getThreadMessageById = Effect . fn ( "getThreadMessageById" ) ( function * (
932+ threadId : ThreadId ,
933+ messageId : MessageId ,
934+ ) {
935+ const message = yield * projectionThreadMessages . getByMessageId ( { messageId } ) ;
936+ return Option . filter ( message , ( entry ) => entry . threadId === threadId ) . pipe (
937+ Option . getOrUndefined ,
938+ ) ;
939+ } ) ;
940+
941+ const listThreadProposedPlans = Effect . fn ( "listThreadProposedPlans" ) ( function * (
942+ threadId : ThreadId ,
943+ ) {
944+ const proposedPlans = yield * projectionThreadProposedPlans . listByThreadId ( { threadId } ) ;
945+ return proposedPlans . map (
946+ ( plan ) : OrchestrationProposedPlan => ( {
947+ id : plan . planId ,
948+ turnId : plan . turnId ,
949+ planMarkdown : plan . planMarkdown ,
950+ implementedAt : plan . implementedAt ,
951+ implementationThreadId : plan . implementationThreadId ,
952+ createdAt : plan . createdAt ,
953+ updatedAt : plan . updatedAt ,
954+ } ) ,
955+ ) ;
956+ } ) ;
957+
964958 const rememberAssistantMessageId = ( threadId : ThreadId , turnId : TurnId , messageId : MessageId ) =>
965959 Cache . getOption ( turnMessageIdsByTurnKey , providerTurnKey ( threadId , turnId ) ) . pipe (
966960 Effect . flatMap ( ( existingIds ) =>
@@ -1469,8 +1463,11 @@ const make = Effect.gen(function* () {
14691463 implementationThreadId : ThreadId ,
14701464 implementedAt : string ,
14711465 ) {
1472- const sourceThread = yield * resolveThreadDetail ( sourceThreadId ) ;
1473- const sourcePlan = sourceThread ?. proposedPlans . find ( ( entry ) => entry . id === sourcePlanId ) ;
1466+ const [ sourceThread , sourcePlans ] = yield * Effect . all ( [
1467+ resolveThreadShell ( sourceThreadId ) ,
1468+ listThreadProposedPlans ( sourceThreadId ) ,
1469+ ] ) ;
1470+ const sourcePlan = sourcePlans . find ( ( entry ) => entry . id === sourcePlanId ) ;
14741471 if ( ! sourceThread || ! sourcePlan || sourcePlan . implementedAt !== null ) {
14751472 return ;
14761473 }
@@ -1481,7 +1478,7 @@ const make = Effect.gen(function* () {
14811478 commandId : CommandId . make (
14821479 `provider:source-proposed-plan-implemented:${ implementationThreadId } :${ commandUuid } ` ,
14831480 ) ,
1484- threadId : sourceThread . id ,
1481+ threadId : sourceThreadId ,
14851482 proposedPlan : {
14861483 ...sourcePlan ,
14871484 implementedAt,
@@ -1498,14 +1495,14 @@ const make = Effect.gen(function* () {
14981495 const thread = yield * resolveThreadShell ( event . threadId ) ;
14991496 if ( ! thread ) return ;
15001497
1501- let loadedThreadDetail : OrchestrationThread | null | undefined ;
1502- const getLoadedThreadDetail = ( ) =>
1498+ let loadedThreadProposedPlans : ReadonlyArray < OrchestrationProposedPlan > | undefined ;
1499+ const getLoadedThreadProposedPlans = ( ) =>
15031500 Effect . gen ( function * ( ) {
1504- if ( loadedThreadDetail !== undefined ) {
1505- return loadedThreadDetail ;
1501+ if ( loadedThreadProposedPlans !== undefined ) {
1502+ return loadedThreadProposedPlans ;
15061503 }
1507- loadedThreadDetail = ( yield * resolveThreadDetail ( thread . id ) ) ?? null ;
1508- return loadedThreadDetail ;
1504+ loadedThreadProposedPlans = yield * listThreadProposedPlans ( thread . id ) ;
1505+ return loadedThreadProposedPlans ;
15091506 } ) ;
15101507
15111508 const now = event . createdAt ;
@@ -1715,7 +1712,11 @@ const make = Effect.gen(function* () {
17151712 ? toTurnId ( event . turnId )
17161713 : undefined ;
17171714 if ( pauseForUserTurnId ) {
1718- const detailedThread = yield * getLoadedThreadDetail ( ) ;
1715+ const hasProjectedMessage = yield * projectionThreadMessages . hasAssistantMessageForTurn ( {
1716+ threadId : thread . id ,
1717+ turnId : pauseForUserTurnId ,
1718+ streamingOnly : true ,
1719+ } ) ;
17191720 const assistantDeliveryMode : AssistantDeliveryMode = yield * Effect . map (
17201721 serverSettingsService . getSettings ,
17211722 ( settings ) => ( settings . enableLegacyTokenStreaming ? "streaming" : "buffered" ) ,
@@ -1746,11 +1747,7 @@ const make = Effect.gen(function* () {
17461747 event . type === "request.opened"
17471748 ? "assistant-delta-finalize-on-request-opened"
17481749 : "assistant-delta-finalize-on-user-input-requested" ,
1749- hasProjectedMessage :
1750- detailedThread !== null &&
1751- hasAssistantMessageForTurn ( detailedThread . messages , pauseForUserTurnId , {
1752- streamingOnly : true ,
1753- } ) ,
1750+ hasProjectedMessage,
17541751 flushedMessageIds,
17551752 } ) ;
17561753 }
@@ -1779,19 +1776,24 @@ const make = Effect.gen(function* () {
17791776 : undefined ;
17801777
17811778 if ( assistantCompletion ) {
1782- const detailedThread = yield * getLoadedThreadDetail ( ) ;
1783- const messages = detailedThread ?. messages ?? [ ] ;
17841779 const turnId = toTurnId ( event . turnId ) ;
17851780 const activeAssistantMessageId = turnId
17861781 ? yield * getActiveAssistantMessageIdForTurn ( thread . id , turnId )
17871782 : Option . none < MessageId > ( ) ;
1788- const hasAssistantMessagesForTurn =
1789- turnId !== undefined ? hasAssistantMessageForTurn ( messages , turnId ) : false ;
17901783 const assistantMessageId = Option . getOrElse (
17911784 activeAssistantMessageId ,
17921785 ( ) => assistantCompletion . messageId ,
17931786 ) ;
1794- const existingAssistantMessage = findMessageById ( messages , assistantMessageId ) ;
1787+ const [ existingAssistantMessage , hasAssistantMessagesForTurn ] = yield * Effect . all ( [
1788+ getThreadMessageById ( thread . id , assistantMessageId ) ,
1789+ turnId === undefined
1790+ ? Effect . succeed ( false )
1791+ : projectionThreadMessages . hasAssistantMessageForTurn ( {
1792+ threadId : thread . id ,
1793+ turnId,
1794+ streamingOnly : false ,
1795+ } ) ,
1796+ ] ) ;
17951797 const shouldApplyFallbackCompletionText =
17961798 ! existingAssistantMessage || existingAssistantMessage . text . length === 0 ;
17971799
@@ -1831,11 +1833,11 @@ const make = Effect.gen(function* () {
18311833 }
18321834
18331835 if ( proposedPlanCompletion ) {
1834- const detailedThread = yield * getLoadedThreadDetail ( ) ;
1836+ const proposedPlans = yield * getLoadedThreadProposedPlans ( ) ;
18351837 yield * finalizeBufferedProposedPlan ( {
18361838 event,
18371839 threadId : thread . id ,
1838- threadProposedPlans : detailedThread ?. proposedPlans ?? [ ] ,
1840+ threadProposedPlans : proposedPlans ,
18391841 planId : proposedPlanCompletion . planId ,
18401842 ...( proposedPlanCompletion . turnId ? { turnId : proposedPlanCompletion . turnId } : { } ) ,
18411843 fallbackMarkdown : proposedPlanCompletion . planMarkdown ,
@@ -1844,25 +1846,27 @@ const make = Effect.gen(function* () {
18441846 }
18451847
18461848 if ( event . type === "turn.completed" ) {
1847- const detailedThread = yield * getLoadedThreadDetail ( ) ;
1848- const messages = detailedThread ?. messages ?? [ ] ;
1849- const proposedPlans = detailedThread ?. proposedPlans ?? [ ] ;
1849+ const proposedPlans = yield * getLoadedThreadProposedPlans ( ) ;
18501850 const turnId = toTurnId ( event . turnId ) ;
18511851 if ( turnId ) {
18521852 const assistantMessageIds = yield * getAssistantMessageIdsForTurn ( thread . id , turnId ) ;
18531853 yield * Effect . forEach (
18541854 assistantMessageIds ,
18551855 ( assistantMessageId ) =>
1856- finalizeAssistantMessage ( {
1857- event,
1858- threadId : thread . id ,
1859- messageId : assistantMessageId ,
1860- turnId,
1861- createdAt : now ,
1862- commandTag : "assistant-complete-finalize" ,
1863- finalDeltaCommandTag : "assistant-delta-finalize-fallback" ,
1864- hasProjectedMessage : findMessageById ( messages , assistantMessageId ) !== undefined ,
1865- } ) ,
1856+ getThreadMessageById ( thread . id , assistantMessageId ) . pipe (
1857+ Effect . flatMap ( ( existingMessage ) =>
1858+ finalizeAssistantMessage ( {
1859+ event,
1860+ threadId : thread . id ,
1861+ messageId : assistantMessageId ,
1862+ turnId,
1863+ createdAt : now ,
1864+ commandTag : "assistant-complete-finalize" ,
1865+ finalDeltaCommandTag : "assistant-delta-finalize-fallback" ,
1866+ hasProjectedMessage : existingMessage !== undefined ,
1867+ } ) ,
1868+ ) ,
1869+ ) ,
18661870 { concurrency : 1 } ,
18671871 ) . pipe ( Effect . asVoid ) ;
18681872 yield * clearAssistantMessageIdsForTurn ( thread . id , turnId ) ;
@@ -2022,8 +2026,17 @@ const make = Effect.gen(function* () {
20222026 if ( event . type === "task.completed" ) {
20232027 taskTitle = yield * lookupTaskDescription ( thread . id , event . payload . taskId ) ;
20242028 if ( ! taskTitle ) {
2025- const threadDetail = yield * getLoadedThreadDetail ( ) ;
2026- taskTitle = findTaskTitleInActivities ( threadDetail ?. activities , event . payload . taskId ) ;
2029+ const taskActivity = yield * projectionThreadActivities . getLatestTaskActivity ( {
2030+ threadId : thread . id ,
2031+ taskId : event . payload . taskId ,
2032+ } ) ;
2033+ taskTitle = findTaskTitleInActivities (
2034+ Option . match ( taskActivity , {
2035+ onNone : ( ) => undefined ,
2036+ onSome : ( activity ) => [ activity ] ,
2037+ } ) ,
2038+ event . payload . taskId ,
2039+ ) ;
20272040 }
20282041 }
20292042
@@ -2091,4 +2104,9 @@ const make = Effect.gen(function* () {
20912104export const ProviderRuntimeIngestionLive = Layer . effect (
20922105 ProviderRuntimeIngestionService ,
20932106 make ,
2094- ) . pipe ( Layer . provide ( ProjectionTurnRepositoryLive ) ) ;
2107+ ) . pipe (
2108+ Layer . provide ( ProjectionThreadActivityRepositoryLive ) ,
2109+ Layer . provide ( ProjectionThreadMessageRepositoryLive ) ,
2110+ Layer . provide ( ProjectionThreadProposedPlanRepositoryLive ) ,
2111+ Layer . provide ( ProjectionTurnRepositoryLive ) ,
2112+ ) ;
0 commit comments