Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkflowAgents.ts

119 lines5.0 KB

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