|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +/** |
| 3 | + * A workflow member never becomes a provider task and the CLI forwards none of |
| 4 | + * its messages, so the transcript the harness writes is the only record of its |
| 5 | + * conversation. The path is derived here from the run's transcript directory |
| 6 | + * and the member's provider id rather than accepted from the caller, and the |
| 7 | + * read itself is contained by readContainedWorkflowFile. |
| 8 | + */ |
| 9 | +import * as NodePath from "node:path"; |
| 10 | + |
| 11 | +import { OrchestrationWorkflowFileError } from "@t3tools/contracts"; |
| 12 | +import * as Context from "effect/Context"; |
| 13 | +import * as Effect from "effect/Effect"; |
| 14 | +import * as Option from "effect/Option"; |
| 15 | +import * as Schema from "effect/Schema"; |
| 16 | + |
| 17 | +import { field, text } from "./unknownField.ts"; |
| 18 | +import { readContainedWorkflowFile, type WorkflowFileIdentity } from "./workflowFileRead.ts"; |
| 19 | + |
| 20 | +const TRANSCRIPT_READ_BYTE_CAP = 512 * 1024; |
| 21 | + |
| 22 | +const AGENT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/; |
| 23 | + |
| 24 | +const decodeJsonLine = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown)); |
| 25 | + |
| 26 | +export interface WorkflowAgentTranscriptCursor { |
| 27 | + readonly offset: number; |
| 28 | + readonly identity: WorkflowFileIdentity | null; |
| 29 | +} |
| 30 | + |
| 31 | +export const initialWorkflowAgentTranscriptCursor: WorkflowAgentTranscriptCursor = { |
| 32 | + offset: 0, |
| 33 | + identity: null, |
| 34 | +}; |
| 35 | + |
| 36 | +/** Sees every transcript read: where it started and ended, and how many lines it kept. */ |
| 37 | +export const WorkflowAgentTranscriptReadObserver = Context.Reference< |
| 38 | + (read: { |
| 39 | + readonly agentId: string; |
| 40 | + readonly startOffset: number; |
| 41 | + readonly endOffset: number; |
| 42 | + readonly lineCount: number; |
| 43 | + }) => Effect.Effect<void> |
| 44 | +>("t3/orchestration/WorkflowAgentTranscriptReadObserver", { |
| 45 | + defaultValue: () => () => Effect.void, |
| 46 | +}); |
| 47 | + |
| 48 | +/** |
| 49 | + * Conversation lines only: `attachment` lines and `isMeta` user lines are |
| 50 | + * harness bookkeeping (hook output, skill listings, injected context), and a |
| 51 | + * line without a uuid cannot be projected idempotently. |
| 52 | + */ |
| 53 | +function parseWorkflowAgentTranscriptLines(contents: string): ReadonlyArray<unknown> { |
| 54 | + return contents.split("\n").flatMap((line) => { |
| 55 | + if (line.trim().length === 0) return []; |
| 56 | + const entry = Option.getOrUndefined(decodeJsonLine(line)); |
| 57 | + const type = text(field(entry, "type")); |
| 58 | + if (type !== "assistant" && type !== "user") return []; |
| 59 | + if (field(entry, "isMeta") === true || text(field(entry, "uuid")) === undefined) return []; |
| 60 | + return [entry]; |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +/** Reads the complete lines written since `cursor`, and the cursor after them. */ |
| 65 | +export const readWorkflowAgentTranscript = Effect.fn("orchestration.readWorkflowAgentTranscript")( |
| 66 | + function* (input: { |
| 67 | + readonly transcriptDir: string; |
| 68 | + readonly agentId: string; |
| 69 | + readonly cursor: WorkflowAgentTranscriptCursor; |
| 70 | + readonly configDir?: string; |
| 71 | + }) { |
| 72 | + if (!AGENT_ID_PATTERN.test(input.agentId)) { |
| 73 | + return yield* new OrchestrationWorkflowFileError({ |
| 74 | + reason: "invalid-path", |
| 75 | + path: input.agentId, |
| 76 | + }); |
| 77 | + } |
| 78 | + const file = yield* readContainedWorkflowFile({ |
| 79 | + path: NodePath.join(input.transcriptDir, `agent-${input.agentId}.jsonl`), |
| 80 | + extension: ".jsonl", |
| 81 | + byteCap: TRANSCRIPT_READ_BYTE_CAP, |
| 82 | + fromOffset: input.cursor, |
| 83 | + ...(input.configDir === undefined ? {} : { configDir: input.configDir }), |
| 84 | + }); |
| 85 | + const lines = parseWorkflowAgentTranscriptLines(file.contents); |
| 86 | + yield* (yield* WorkflowAgentTranscriptReadObserver)({ |
| 87 | + agentId: input.agentId, |
| 88 | + startOffset: file.startOffset, |
| 89 | + endOffset: file.endOffset, |
| 90 | + lineCount: lines.length, |
| 91 | + }); |
| 92 | + return { |
| 93 | + lines, |
| 94 | + cursor: { offset: file.endOffset, identity: file.identity }, |
| 95 | + // More complete lines may follow past the per-read cap. |
| 96 | + truncated: file.truncated, |
| 97 | + }; |
| 98 | + }, |
| 99 | +); |
0 commit comments