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

RuntimePolicy.ts

170 lines5.7 KB

1import {
2 ModelSelection,
3 OrchestrationV2AppThread,
4 ProjectId,
5 ProviderInstanceId,
6 type RuntimeMode,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Schema from "effect/Schema";
13​
14import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
15import {
16 ProviderAdapterV2RuntimePolicy,
17 type ProviderAdapterV2RuntimePolicy as ProviderAdapterV2RuntimePolicyType,
18} from "./ProviderAdapter.ts";
19import * as ProjectStore from "./ProjectStore.ts";
20​
21/**
22 * ERRORS
23 */
24export class RuntimePolicyResolveError extends Schema.TaggedError<RuntimePolicyResolveError>()(
25 "RuntimePolicyResolveError",
26 {
27 projectId: ProjectId,
28 providerInstanceId: ProviderInstanceId,
29 cause: Schema.optional(Schema.Defect()),
30 },
31) {
32 override get message(): string {
33 return `Failed to resolve runtime policy for provider instance ${this.providerInstanceId} in project ${this.projectId}.`;
34 }
36​
37export const RuntimePolicyV2Error = Schema.Union([RuntimePolicyResolveError]);
38export type RuntimePolicyV2Error = typeof RuntimePolicyV2Error.Type;
39​
40export const RuntimePolicyV2Override = Schema.Struct({
41 cwd: Schema.optional(Schema.String),
42 approvalPolicy: Schema.optional(Schema.Unknown),
43 sandboxPolicy: Schema.optional(Schema.Unknown),
44 reasoningEffort: Schema.optional(Schema.String),
45});
46export type RuntimePolicyV2Override = typeof RuntimePolicyV2Override.Type;
47​
48/**
49 * SERVICE DEFINITION
50 */
51export interface RuntimePolicyV2Shape {
52 readonly resolve: (input: {
53 readonly thread: OrchestrationV2AppThread;
54 readonly modelSelection: ModelSelection;
55 }) => Effect.Effect<ProviderAdapterV2RuntimePolicyType, RuntimePolicyV2Error>;
57​
58export class RuntimePolicyV2 extends Context.Service<RuntimePolicyV2, RuntimePolicyV2Shape>()(
59 "t3/orchestration-v2/RuntimePolicy/RuntimePolicyV2",
60) {}
61​
62/**
63 * IMPLEMENTATIONS
64 */
65export const layer: Layer.Layer<RuntimePolicyV2> = Layer.succeed(RuntimePolicyV2, {
66 resolve: (input) =>
67 Effect.succeed({
68 runtimeMode: input.thread.runtimeMode,
69 interactionMode: input.thread.interactionMode,
70 cwd: input.thread.worktreePath,
71 }),
72});
73​
74/**
75 * The mode a provider runs a thread in. A mode the provider does not offer
76 * (a thread set before it stopped offering it, or a stale client) runs in
77 * Supervised rather than having T3 imitate it.
78 */
79function providerRuntimeMode(
80 runtimeMode: RuntimeMode,
81 supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
82): RuntimeMode {
83 return supportedRuntimeModes === undefined ||
84 supportedRuntimeModes.length === 0 ||
85 supportedRuntimeModes.includes(runtimeMode)
86 ? runtimeMode
87 : "approval-required";
89​
90export const layerFromProjectStore: Layer.Layer<
91 RuntimePolicyV2,
92 never,
93 ProjectStore.ProjectStoreV2 | ProviderInstanceRegistry.ProviderInstanceRegistry
94> = Layer.effect(
95 RuntimePolicyV2,
96 Effect.gen(function* () {
97 const projects = yield* ProjectStore.ProjectStoreV2;
98 const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
99 return RuntimePolicyV2.of({
100 resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) {
101 const instance = yield* providerInstances.getInstance(input.modelSelection.instanceId);
102 const supportedRuntimeModes =
103 instance === undefined
104 ? undefined
105 : (yield* instance.snapshot.getSnapshot).supportedRuntimeModes;
106 const cwd =
107 input.thread.worktreePath ??
108 (yield* projects.get(input.thread.projectId).pipe(
109 Effect.mapError(
110 (cause) =>
111 new RuntimePolicyResolveError({
112 projectId: input.thread.projectId,
113 providerInstanceId: input.modelSelection.instanceId,
114 cause,
115 }),
116 ),
117 Effect.flatMap(
118 Option.match({
119 onNone: () =>
120 Effect.fail(
121 new RuntimePolicyResolveError({
122 projectId: input.thread.projectId,
123 providerInstanceId: input.modelSelection.instanceId,
124 cause: "Project not found.",
125 }),
126 ),
127 onSome: (project) => Effect.succeed(project.workspaceRoot),
128 }),
129 ),
130 ));
131 return ProviderAdapterV2RuntimePolicy.make({
132 runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes),
133 interactionMode: input.thread.interactionMode,
134 cwd,
135 });
136 }),
137 });
138 }),
139);
140​
141export function layerWithOverride(
142 override: RuntimePolicyV2Override,
143): Layer.Layer<RuntimePolicyV2, never, RuntimePolicyV2> {
144 return Layer.effect(
145 RuntimePolicyV2,
146 Effect.gen(function* () {
147 const base = yield* RuntimePolicyV2;
148 return {
149 resolve: (input) =>
150 base.resolve(input).pipe(
151 Effect.map((policy) =>
152 ProviderAdapterV2RuntimePolicy.make({
153 ...policy,
154 ...(override.cwd === undefined ? {} : { cwd: override.cwd }),
155 ...(override.approvalPolicy === undefined
156 ? {}
157 : { approvalPolicy: override.approvalPolicy }),
158 ...(override.sandboxPolicy === undefined
159 ? {}
160 : { sandboxPolicy: override.sandboxPolicy }),
161 ...(override.reasoningEffort === undefined
162 ? {}
163 : { reasoningEffort: override.reasoningEffort }),
164 }),
165 ),
166 ),
167 } satisfies RuntimePolicyV2Shape;
168 }),
169 );