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

RuntimePolicy.ts

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