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

RuntimePolicy.ts

153 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: (input) =>
55 Effect.succeed({
56 runtimeMode: input.thread.runtimeMode,
57 interactionMode: input.thread.interactionMode,
58 cwd: input.thread.worktreePath,
59 }),
60});
61​
62function providerRuntimeMode(
63 runtimeMode: RuntimeMode,
64 supportedRuntimeModes: ReadonlyArray<RuntimeMode> | undefined,
65): RuntimeMode {
66 return supportedRuntimeModes === undefined ||
67 supportedRuntimeModes.length === 0 ||
68 supportedRuntimeModes.includes(runtimeMode)
69 ? runtimeMode
70 : "approval-required";
72​
73export const layerFromProjectStore: Layer.Layer<
74 RuntimePolicyV2,
75 never,
76 ProjectStore.ProjectStoreV2 | ProviderInstanceRegistry.ProviderInstanceRegistry
77> = Layer.effect(
78 RuntimePolicyV2,
79 Effect.gen(function* () {
80 const projects = yield* ProjectStore.ProjectStoreV2;
81 const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
82 return RuntimePolicyV2.of({
83 resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) {
84 const instance = yield* providerInstances.getInstance(input.modelSelection.instanceId);
85 const supportedRuntimeModes =
86 instance === undefined
87 ? undefined
88 : (yield* instance.snapshot.getSnapshot).supportedRuntimeModes;
89 const cwd =
90 input.thread.worktreePath ??
91 (yield* projects.get(input.thread.projectId).pipe(
92 Effect.mapError(
93 (cause) =>
94 new RuntimePolicyResolveError({
95 projectId: input.thread.projectId,
96 providerInstanceId: input.modelSelection.instanceId,
97 cause,
98 }),
99 ),
100 Effect.flatMap(
101 Option.match({
102 onNone: () =>
103 Effect.fail(
104 new RuntimePolicyResolveError({
105 projectId: input.thread.projectId,
106 providerInstanceId: input.modelSelection.instanceId,
107 cause: "Project not found.",
108 }),
109 ),
110 onSome: (project) => Effect.succeed(project.workspaceRoot),
111 }),
112 ),
113 ));
114 return ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
115 runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes),
116 interactionMode: input.thread.interactionMode,
117 cwd,
118 });
119 }),
120 });
121 }),
122);
123​
124export function layerWithOverride(
125 override: RuntimePolicyV2Override,
126): Layer.Layer<RuntimePolicyV2, never, RuntimePolicyV2> {
127 return Layer.effect(
128 RuntimePolicyV2,
129 Effect.gen(function* () {
130 const base = yield* RuntimePolicyV2;
131 return {
132 resolve: (input) =>
133 base.resolve(input).pipe(
134 Effect.map((policy) =>
135 ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
136 ...policy,
137 ...(override.cwd === undefined ? {} : { cwd: override.cwd }),
138 ...(override.approvalPolicy === undefined
139 ? {}
140 : { approvalPolicy: override.approvalPolicy }),
141 ...(override.sandboxPolicy === undefined
142 ? {}
143 : { sandboxPolicy: override.sandboxPolicy }),
144 ...(override.reasoningEffort === undefined
145 ? {}
146 : { reasoningEffort: override.reasoningEffort }),
147 }),
148 ),
149 ),
150 } satisfies RuntimePolicyV2Shape;
151 }),
152 );