@@ -3,13 +3,15 @@ import {
33 type ChatAttachment ,
44 type OrchestrationEvent ,
55 type OrchestrationSessionStatus ,
6+ TaskScheduleSpec ,
67 ThreadId ,
78} from "@t3tools/contracts" ;
89import * as Effect from "effect/Effect" ;
910import * as FileSystem from "effect/FileSystem" ;
1011import * as Layer from "effect/Layer" ;
1112import * as Option from "effect/Option" ;
1213import * as Path from "effect/Path" ;
14+ import * as Schema from "effect/Schema" ;
1315import * as Stream from "effect/Stream" ;
1416import * as SqlClient from "effect/unstable/sql/SqlClient" ;
1517
@@ -34,6 +36,7 @@ import {
3436 ProjectionTurnRepository ,
3537} from "../../persistence/Services/ProjectionTurns.ts" ;
3638import { ProjectionThreadRepository } from "../../persistence/Services/ProjectionThreads.ts" ;
39+ import { ProjectionTaskRepository } from "../../persistence/Services/ProjectionTasks.ts" ;
3740import { ProjectionPendingApprovalRepositoryLive } from "../../persistence/Layers/ProjectionPendingApprovals.ts" ;
3841import { ProjectionProjectRepositoryLive } from "../../persistence/Layers/ProjectionProjects.ts" ;
3942import { ProjectionStateRepositoryLive } from "../../persistence/Layers/ProjectionState.ts" ;
@@ -43,6 +46,7 @@ import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/La
4346import { ProjectionThreadSessionRepositoryLive } from "../../persistence/Layers/ProjectionThreadSessions.ts" ;
4447import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts" ;
4548import { ProjectionThreadRepositoryLive } from "../../persistence/Layers/ProjectionThreads.ts" ;
49+ import { ProjectionTaskRepositoryLive } from "../../persistence/Layers/ProjectionTasks.ts" ;
4650import { ServerConfig } from "../../config.ts" ;
4751import {
4852 OrchestrationProjectionPipeline ,
@@ -55,6 +59,8 @@ import {
5559 toSafeThreadAttachmentSegment ,
5660} from "../../attachmentStore.ts" ;
5761
62+ const encodeTaskScheduleJson = Schema . encodeEffect ( Schema . fromJsonString ( TaskScheduleSpec ) ) ;
63+
5864export const ORCHESTRATION_PROJECTOR_NAMES = {
5965 projects : "projection.projects" ,
6066 threads : "projection.threads" ,
@@ -65,6 +71,7 @@ export const ORCHESTRATION_PROJECTOR_NAMES = {
6571 threadTurns : "projection.thread-turns" ,
6672 checkpoints : "projection.checkpoints" ,
6773 pendingApprovals : "projection.pending-approvals" ,
74+ tasks : "projection.tasks" ,
6875} as const ;
6976
7077type ProjectorName =
@@ -480,6 +487,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
480487 const projectionThreadSessionRepository = yield * ProjectionThreadSessionRepository ;
481488 const projectionTurnRepository = yield * ProjectionTurnRepository ;
482489 const projectionPendingApprovalRepository = yield * ProjectionPendingApprovalRepository ;
490+ const projectionTaskRepository = yield * ProjectionTaskRepository ;
483491
484492 const fileSystem = yield * FileSystem . FileSystem ;
485493 const path = yield * Path . Path ;
@@ -1606,6 +1614,69 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16061614 }
16071615 } ) ;
16081616
1617+ const applyTasksProjection : ProjectorDefinition [ "apply" ] = Effect . fn ( "applyTasksProjection" ) (
1618+ function * ( event , _attachmentSideEffects ) {
1619+ switch ( event . type ) {
1620+ case "task.scheduled" : {
1621+ // The payload was schema-decoded off the wire, so encoding cannot
1622+ // fail here; a failure is a defect, not a projection error.
1623+ const scheduleJson = yield * encodeTaskScheduleJson ( event . payload . schedule ) . pipe (
1624+ Effect . orDie ,
1625+ ) ;
1626+ yield * projectionTaskRepository . upsert ( {
1627+ taskId : event . payload . taskId ,
1628+ projectId : event . payload . projectId ,
1629+ threadId : event . payload . threadId ,
1630+ name : event . payload . name ?? null ,
1631+ prompt : event . payload . prompt ,
1632+ scheduleJson,
1633+ createdAt : event . payload . createdAt ,
1634+ updatedAt : event . payload . updatedAt ,
1635+ lastFiredAt : null ,
1636+ nextFireAt : event . payload . nextFireAt ,
1637+ cancelledAt : null ,
1638+ } ) ;
1639+ return ;
1640+ }
1641+
1642+ case "task.fired" : {
1643+ const existingRow = yield * projectionTaskRepository . getById ( {
1644+ taskId : event . payload . taskId ,
1645+ } ) ;
1646+ if ( Option . isNone ( existingRow ) ) {
1647+ return ;
1648+ }
1649+ yield * projectionTaskRepository . upsert ( {
1650+ ...existingRow . value ,
1651+ lastFiredAt : event . payload . firedAt ,
1652+ nextFireAt : event . payload . nextFireAt ,
1653+ updatedAt : event . payload . updatedAt ,
1654+ } ) ;
1655+ return ;
1656+ }
1657+
1658+ case "task.cancelled" : {
1659+ const existingRow = yield * projectionTaskRepository . getById ( {
1660+ taskId : event . payload . taskId ,
1661+ } ) ;
1662+ if ( Option . isNone ( existingRow ) ) {
1663+ return ;
1664+ }
1665+ yield * projectionTaskRepository . upsert ( {
1666+ ...existingRow . value ,
1667+ nextFireAt : null ,
1668+ cancelledAt : event . payload . cancelledAt ,
1669+ updatedAt : event . payload . updatedAt ,
1670+ } ) ;
1671+ return ;
1672+ }
1673+
1674+ default :
1675+ return ;
1676+ }
1677+ } ,
1678+ ) ;
1679+
16091680 const projectors : ReadonlyArray < ProjectorDefinition > = [
16101681 {
16111682 name : ORCHESTRATION_PROJECTOR_NAMES . projects ,
@@ -1643,6 +1714,10 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16431714 name : ORCHESTRATION_PROJECTOR_NAMES . threads ,
16441715 apply : applyThreadsProjection ,
16451716 } ,
1717+ {
1718+ name : ORCHESTRATION_PROJECTOR_NAMES . tasks ,
1719+ apply : applyTasksProjection ,
1720+ } ,
16461721 ] ;
16471722
16481723 const runProjectorForEvent = Effect . fn ( "runProjectorForEvent" ) ( function * (
@@ -1746,4 +1821,5 @@ export const OrchestrationProjectionPipelineLive = Layer.effect(
17461821 Layer . provideMerge ( ProjectionTurnRepositoryLive ) ,
17471822 Layer . provideMerge ( ProjectionPendingApprovalRepositoryLive ) ,
17481823 Layer . provideMerge ( ProjectionStateRepositoryLive ) ,
1824+ Layer . provideMerge ( ProjectionTaskRepositoryLive ) ,
17491825) ;
0 commit comments