|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { RunId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | + |
| 5 | +import { type ThreadTools, waitForRun } from "./thread.ts"; |
| 6 | + |
| 7 | +const threadId = ThreadId.make("thread-1"); |
| 8 | +const runId = RunId.make("run-1"); |
| 9 | + |
| 10 | +/** Answers each tool call with the next scripted response for that tool. */ |
| 11 | +const scriptedTools = (script: Record<string, Array<unknown>>) => { |
| 12 | + const calls: Array<{ readonly tool: string; readonly args: unknown }> = []; |
| 13 | + const call = ((tool: string, args: unknown) => { |
| 14 | + calls.push({ tool, args }); |
| 15 | + const next = script[tool]?.shift(); |
| 16 | + return next === undefined ? Effect.die(`unscripted ${tool} call`) : Effect.succeed(next); |
| 17 | + }) as ThreadTools["call"]; |
| 18 | + return { tools: { call } satisfies ThreadTools, calls }; |
| 19 | +}; |
| 20 | + |
| 21 | +it.effect("--wait follows the sent run to its continuation's terminal status", () => |
| 22 | + Effect.gen(function* () { |
| 23 | + const continuation = RunId.make("run-2"); |
| 24 | + const { tools, calls } = scriptedTools({ |
| 25 | + t3_thread_wait: [ |
| 26 | + { threadId, runId, status: "running", timedOut: true }, |
| 27 | + { threadId, runId: continuation, status: "failed", timedOut: false }, |
| 28 | + ], |
| 29 | + t3_thread_read: [{ thread: { pendingRequestCount: 0 } }], |
| 30 | + }); |
| 31 | + |
| 32 | + const outcome = yield* waitForRun(tools, threadId, runId); |
| 33 | + |
| 34 | + assert.deepStrictEqual(outcome, { type: "ended", runId: continuation, status: "failed" }); |
| 35 | + assert.deepStrictEqual( |
| 36 | + calls.filter((entry) => entry.tool === "t3_thread_wait").map((entry) => entry.args), |
| 37 | + [ |
| 38 | + { threadId, runId, timeoutMs: 15_000 }, |
| 39 | + { threadId, runId, timeoutMs: 15_000 }, |
| 40 | + ], |
| 41 | + ); |
| 42 | + }), |
| 43 | +); |
| 44 | + |
| 45 | +it.effect("--wait stops when the thread waits for an answer instead of polling forever", () => |
| 46 | + Effect.gen(function* () { |
| 47 | + const { tools } = scriptedTools({ |
| 48 | + t3_thread_wait: [ |
| 49 | + { threadId, runId, status: "queued", timedOut: true }, |
| 50 | + { threadId, runId, status: "queued", timedOut: true }, |
| 51 | + ], |
| 52 | + t3_thread_read: [{ thread: { pendingRequestCount: 1 } }], |
| 53 | + }); |
| 54 | + |
| 55 | + assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), { type: "needs_input" }); |
| 56 | + }), |
| 57 | +); |
| 58 | + |
| 59 | +it.effect("--wait reports its run's result when a later run is the one waiting", () => |
| 60 | + Effect.gen(function* () { |
| 61 | + const { tools } = scriptedTools({ |
| 62 | + t3_thread_wait: [ |
| 63 | + { threadId, runId, status: "running", timedOut: true }, |
| 64 | + { threadId, runId, status: "completed", timedOut: false }, |
| 65 | + ], |
| 66 | + t3_thread_read: [{ thread: { pendingRequestCount: 1 } }], |
| 67 | + }); |
| 68 | + |
| 69 | + assert.deepStrictEqual(yield* waitForRun(tools, threadId, runId), { |
| 70 | + type: "ended", |
| 71 | + runId, |
| 72 | + status: "completed", |
| 73 | + }); |
| 74 | + }), |
| 75 | +); |
0 commit comments