Open History to see the last commit
1import type {
2 ModelSelection,
3 OrchestrationV2ThreadLaunchWorkspaceStrategy,
4 ProviderInteractionMode,
5 RuntimeMode,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import type * as Duration from "effect/Duration";
9import type * as Effect from "effect/Effect";
10import * as Schema from "effect/Schema";
11
12import type { IssueAddress } from "./WorkflowForge.ts";
13
14export class WorkflowAgentError extends Schema.TaggedError<WorkflowAgentError>()(
15 "WorkflowAgentError",
16 { reason: Schema.String },
17) {
18 override get message(): string {
19 return this.reason;
20 }
21}
22
23export interface AgentModel {
24 readonly instanceId: string;
25 readonly model: string;
26 /** The model's own options, such as reasoning effort, as `{ id, value }` pairs. */
27 readonly options?: ModelSelection["options"];
28}
29
30/** How a new thread starts, as `t3_thread_launch` takes it. Unset fields follow the project. */
31export interface AgentLaunch {
32 /** Where its files are: the project's checkout by default, or a worktree of its own. */
33 readonly workspace?: OrchestrationV2ThreadLaunchWorkspaceStrategy | undefined;
34 readonly runtimeMode?: RuntimeMode | undefined;
35 readonly interactionMode?: ProviderInteractionMode | undefined;
36}
37
38export const AgentTurn = Schema.Struct({
39 threadId: Schema.String,
40 turnId: Schema.String,
41 /** The linked environment the thread is in. Absent for this one. */
42 environment: Schema.optional(Schema.String),
43 /** When the step's first turn started, which its time limit counts from. */
44 startedAt: Schema.optional(Schema.Number),
45 /** Why no turn was started, for a step that takes that as an answer. The ids are then empty. */
46 failed: Schema.optional(Schema.String),
47});
48export type AgentTurn = typeof AgentTurn.Type;
49
50export const AgentResult = Schema.Struct({
51 /**
52 * How the turn ended. `stopped` is someone ending it on purpose, which is not a reason to try
53 * again; `failed` is the turn dying underneath the agent.
54 */
55 ending: Schema.Literals(["completed", "failed", "stopped"]),
56 detail: Schema.String,
57 reply: Schema.String,
58 /** What the agent submitted against the output schema, when it did. */
59 output: Schema.optional(Schema.Json),
60});
61export type AgentResult = typeof AgentResult.Type;
62
63/** What a continued thread says to options that only shape a new one. */
64export const launchOnContinue = new WorkflowAgentError({
65 reason:
66 "`workspace`, `runtimeMode` and `interactionMode` shape a new thread. A continued `thread` keeps how it started.",
67});
68
69/** A project named by its folder instead of its id. */
70export const isProjectPath = (project: string) => project.startsWith("/");
71
72/** Where agent steps run. The server implements it with threads; tests stand in for it. */
73export class WorkflowAgents extends Context.Service<
74 WorkflowAgents,
75 {
76 /** Start a turn. The same `key` is the same turn, however often it is asked for. */
77 readonly start: (input: {
78 readonly key: string;
79 /** The run and step this turn is for, which a new thread is marked with. */
80 readonly origin: { readonly runId: string; readonly workflow: string; readonly step: string };
81 readonly title: string;
82 readonly prompt: string;
83 readonly outputSchema: Readonly<Record<string, Schema.Json>> | undefined;
84 readonly model: AgentModel | undefined;
85 /** The project a new thread is made in: its id, or its folder. Not needed to continue `thread`. */
86 readonly project: string | undefined;
87 /** The linked environment to run in. Absent for this one. */
88 readonly environment: string | undefined;
89 /** Continue this thread instead of starting one. */
90 readonly thread: string | undefined;
91 /** An issue the thread is about, which it is linked to. */
92 readonly issue?: IssueAddress | undefined;
93 /** How a new thread starts. Refused with `thread`, which keeps how it started. */
94 readonly launch?: AgentLaunch | undefined;
95 }) => Effect.Effect<AgentTurn, WorkflowAgentError>;
96 /**
97 * Wait for the turn to end and return what it produced. With `quiet`, a turn that ended
98 * without the output it owes is waited past: its agent may be waiting to be woken, and
99 * submits from a later turn. Only once the thread has done nothing for `quiet` is the turn
100 * returned without output.
101 */
102 readonly result: (
103 turn: AgentTurn,
104 options?: { readonly quiet?: Duration.Duration | undefined },
105 ) => Effect.Effect<AgentResult, WorkflowAgentError>;
106 /**
107 * Stop whatever the thread is doing, when its run is cancelled. `ended` when there was nothing
108 * to stop: the turn had already ended.
109 */
110 readonly interrupt: (thread: {
111 readonly threadId: string;
112 readonly environment?: string | undefined;
113 /** The step's own turn. With it, a turn of someone else's on the same thread is left alone. */
114 readonly turnId?: string | undefined;
115 }) => Effect.Effect<"stopped" | "ended", WorkflowAgentError>;
116 }
117>()("t3/workflows/WorkflowAgents") {}