|
| 1 | +import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness"; |
1 | 2 | import { pendingGuestMessageCount } from "@t3tools/contracts"; |
2 | 3 | import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts"; |
3 | 4 | import { projectTurnItemForWire } from "./WireProjection.ts"; |
@@ -491,6 +492,12 @@ export interface ProjectionStoreV2Shape { |
491 | 492 | }, |
492 | 493 | ProjectionStoreV2Error |
493 | 494 | >; |
| 495 | + readonly getAgentAwarenessSnapshot: ( |
| 496 | + threadId?: ThreadId, |
| 497 | + ) => Effect.Effect< |
| 498 | + { readonly snapshotSequence: number; readonly threads: ReadonlyArray<AgentAwarenessThread> }, |
| 499 | + ProjectionStoreV2Error |
| 500 | + >; |
494 | 501 | readonly getShellSnapshot: ( |
495 | 502 | options?: ShellSnapshotOptions, |
496 | 503 | ) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>; |
@@ -980,6 +987,17 @@ type ShellPendingTurnItemRow = PayloadRow & { |
980 | 987 | readonly owning_provider_instance_id: string | null; |
981 | 988 | }; |
982 | 989 |
|
| 990 | +export type AgentAwarenessThread = ProjectThreadAwarenessV2Input["thread"] & |
| 991 | + Pick< |
| 992 | + OrchestrationV2ThreadShell, |
| 993 | + | "projectId" |
| 994 | + | "latestRunCompletedAt" |
| 995 | + | "archivedAt" |
| 996 | + | "activeRunId" |
| 997 | + | "latestRunId" |
| 998 | + | "hasActionableProposedPlan" |
| 999 | + >; |
| 1000 | + |
983 | 1001 | type ShellThreadRow = { |
984 | 1002 | readonly thread_id: string; |
985 | 1003 | readonly payload_json: string; |
@@ -6077,7 +6095,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = |
6077 | 6095 | familyOf, |
6078 | 6096 | worktreePath, |
6079 | 6097 | threadIds, |
6080 | | - }: ShellSnapshotOptions = {}, |
| 6098 | + listedOnly = false, |
| 6099 | + }: ShellSnapshotOptions & { readonly listedOnly?: boolean } = {}, |
6081 | 6100 | ) => |
6082 | 6101 | sql<ShellThreadRow>` |
6083 | 6102 | SELECT |
@@ -6259,7 +6278,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = |
6259 | 6278 | candidate.ordinal DESC, candidate.run_id DESC |
6260 | 6279 | LIMIT 1 |
6261 | 6280 | ) AND blocked.status = 'failed' |
6262 | | - WHERE t.deleted_at IS NULL${threadId === undefined ? sql`` : sql` AND t.thread_id = ${threadId}`}${ |
| 6281 | + WHERE t.deleted_at IS NULL${ |
| 6282 | + listedOnly |
| 6283 | + ? sql` AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' |
| 6284 | + AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1` |
| 6285 | + : sql`` |
| 6286 | + }${threadId === undefined ? sql`` : sql` AND t.thread_id = ${threadId}`}${ |
6263 | 6287 | projectId === undefined ? sql`` : sql` AND t.project_id = ${projectId}` |
6264 | 6288 | }${ |
6265 | 6289 | location === "active" |
@@ -6990,6 +7014,74 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = |
6990 | 7014 | return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads }; |
6991 | 7015 | }, Effect.mapError(shellSnapshotReadError)); |
6992 | 7016 |
|
| 7017 | + const getAgentAwarenessSnapshot = Effect.fn("ProjectionStore.getAgentAwarenessSnapshot")( |
| 7018 | + function* (threadId?: ThreadId) { |
| 7019 | + const { threadRows, providerThreadRows, pendingTurnItemRows, sequenceRows } = |
| 7020 | + yield* sql.withTransaction( |
| 7021 | + Effect.gen(function* () { |
| 7022 | + const threadRows = yield* selectShellThreadRows( |
| 7023 | + threadId, |
| 7024 | + threadId === undefined ? { location: "active", listedOnly: true } : {}, |
| 7025 | + ); |
| 7026 | + const ids = threadRows.map((row) => ThreadId.make(row.thread_id)); |
| 7027 | + const providerThreadRows = |
| 7028 | + ids.length === 0 ? [] : yield* selectShellProviderThreadRows(ids); |
| 7029 | + const pendingTurnItemRows = |
| 7030 | + ids.length === 0 |
| 7031 | + ? [] |
| 7032 | + : yield* selectShellPendingTurnItemRows( |
| 7033 | + ids, |
| 7034 | + threadRows.flatMap((row) => |
| 7035 | + row.active_run_id !== null && row.active_run_status === "running" |
| 7036 | + ? [row.active_run_id] |
| 7037 | + : [], |
| 7038 | + ), |
| 7039 | + ); |
| 7040 | + const sequenceRows = yield* sql<{ readonly snapshot_sequence: number | null }>` |
| 7041 | + SELECT MAX(sequence) AS snapshot_sequence FROM orchestration_events |
| 7042 | + WHERE application_event_version = 2 AND aggregate_kind = 'thread' |
| 7043 | + `; |
| 7044 | + return { threadRows, providerThreadRows, pendingTurnItemRows, sequenceRows }; |
| 7045 | + }), |
| 7046 | + ); |
| 7047 | + const { providerThreadsByThreadId, pendingTurnItemsByThreadId } = |
| 7048 | + yield* pendingBackgroundDataByThreadId({ providerThreadRows, pendingTurnItemRows }); |
| 7049 | + const threads = yield* Effect.forEach(threadRows, (row) => |
| 7050 | + shellThreadStateFromRow({ |
| 7051 | + row, |
| 7052 | + // Awareness never consumes timeline counts or inherited fork history. |
| 7053 | + runOrdinalsByThreadId: new Map(), |
| 7054 | + itemCountsByThreadId: new Map(), |
| 7055 | + shellItemCountsByThreadId: new Map(), |
| 7056 | + providerThreadsByThreadId, |
| 7057 | + pendingTurnItemsByThreadId, |
| 7058 | + }).pipe( |
| 7059 | + Effect.map((state): AgentAwarenessThread => ({ |
| 7060 | + id: state.thread.id, |
| 7061 | + projectId: state.thread.projectId, |
| 7062 | + archivedAt: state.thread.archivedAt, |
| 7063 | + activeRunId: state.activeRunId, |
| 7064 | + latestRunId: state.latestRunId, |
| 7065 | + hasActionableProposedPlan: state.hasActionableProposedPlan, |
| 7066 | + title: state.thread.title, |
| 7067 | + lineage: state.thread.lineage, |
| 7068 | + modelSelection: state.thread.modelSelection, |
| 7069 | + settledOverride: state.thread.settledOverride, |
| 7070 | + updatedAt: state.updatedAt, |
| 7071 | + status: state.latestRunStatus, |
| 7072 | + activityRunStatus: state.activityRunStatus, |
| 7073 | + latestRunCompletedAt: state.latestRunCompletedAt, |
| 7074 | + latestRunStopRequested: state.latestRunStopRequested, |
| 7075 | + pendingRuntimeRequest: state.pendingRuntimeRequest, |
| 7076 | + pendingBackgroundTasks: state.pendingBackgroundTasks, |
| 7077 | + })), |
| 7078 | + ), |
| 7079 | + ); |
| 7080 | + return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads }; |
| 7081 | + }, |
| 7082 | + Effect.mapError(shellSnapshotReadError), |
| 7083 | + ); |
| 7084 | + |
6993 | 7085 | const getShellSnapshot: ProjectionStoreV2Shape["getShellSnapshot"] = (options) => |
6994 | 7086 | sql |
6995 | 7087 | .withTransaction(readShellSnapshotRows(options)) |
@@ -7257,6 +7349,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = |
7257 | 7349 |
|
7258 | 7350 | return { |
7259 | 7351 | apply, |
| 7352 | + getAgentAwarenessSnapshot: readOnReader(getAgentAwarenessSnapshot), |
7260 | 7353 | getShellSnapshot: readOnReader(getShellSnapshot), |
7261 | 7354 | getPullRequestSnapshot: readOnReader(getPullRequestSnapshot), |
7262 | 7355 | listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds), |
|
0 commit comments