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

RuntimePolicy.ts

146 lines4.7 KB

1import {
2 ModelSelection,
3 OrchestrationV2AppThread,
4 ProjectId,
5 ProviderInstanceId,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12​
13import * as ProjectionProjects from "../persistence/Services/ProjectionProjects.ts";
14import {
15 ProviderAdapterV2RuntimePolicy,
16 type ProviderAdapterV2RuntimePolicy as ProviderAdapterV2RuntimePolicyType,
17} from "./ProviderAdapter.ts";
18​
19/**
20 * ERRORS
21 */
22export class RuntimePolicyResolveError extends Schema.TaggedError<RuntimePolicyResolveError>()(
23 "RuntimePolicyResolveError",
24 {
25 projectId: ProjectId,
26 providerInstanceId: ProviderInstanceId,
27 cause: Schema.optional(Schema.Defect()),
28 },
29) {
30 override get message(): string {
31 return `Failed to resolve runtime policy for provider instance ${this.providerInstanceId} in project ${this.projectId}.`;
32 }
34​
35export const RuntimePolicyV2Error = Schema.Union([RuntimePolicyResolveError]);
36export type RuntimePolicyV2Error = typeof RuntimePolicyV2Error.Type;
37​
38export const RuntimePolicyV2Override = Schema.Struct({
39 cwd: Schema.optional(Schema.String),
40 approvalPolicy: Schema.optional(Schema.Unknown),
41 sandboxPolicy: Schema.optional(Schema.Unknown),
42 reasoningEffort: Schema.optional(Schema.String),
43});
44export type RuntimePolicyV2Override = typeof RuntimePolicyV2Override.Type;
45​
46/**
47 * SERVICE DEFINITION
48 */
49export interface RuntimePolicyV2Shape {
50 readonly resolve: (input: {
51 readonly thread: OrchestrationV2AppThread;
52 readonly modelSelection: ModelSelection;
53 }) => Effect.Effect<ProviderAdapterV2RuntimePolicyType, RuntimePolicyV2Error>;
55​
56export class RuntimePolicyV2 extends Context.Service<RuntimePolicyV2, RuntimePolicyV2Shape>()(
57 "t3/orchestration-v2/RuntimePolicy/RuntimePolicyV2",
58) {}
59​
60/**
61 * IMPLEMENTATIONS
62 */
63export const layer: Layer.Layer<RuntimePolicyV2> = Layer.succeed(RuntimePolicyV2, {
64 resolve: (input) =>
65 Effect.succeed({
66 runtimeMode: input.thread.runtimeMode,
67 interactionMode: input.thread.interactionMode,
68 cwd: input.thread.worktreePath,
69 }),
70});
71​
72export const layerFromProjectRepository: Layer.Layer<
73 RuntimePolicyV2,
74 never,
75 ProjectionProjects.ProjectionProjectRepository
76> = Layer.effect(
77 RuntimePolicyV2,
78 Effect.gen(function* () {
79 const projects = yield* ProjectionProjects.ProjectionProjectRepository;
80 return RuntimePolicyV2.of({
81 resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) {
82 const cwd =
83 input.thread.worktreePath ??
84 (yield* projects.getById({ projectId: input.thread.projectId }).pipe(
85 Effect.mapError(
86 (cause) =>
87 new RuntimePolicyResolveError({
88 projectId: input.thread.projectId,
89 providerInstanceId: input.modelSelection.instanceId,
90 cause,
91 }),
92 ),
93 Effect.flatMap(
94 Option.match({
95 onNone: () =>
96 Effect.fail(
97 new RuntimePolicyResolveError({
98 projectId: input.thread.projectId,
99 providerInstanceId: input.modelSelection.instanceId,
100 cause: "Project not found.",
101 }),
102 ),
103 onSome: (project) => Effect.succeed(project.workspaceRoot),
104 }),
105 ),
106 ));
107 return ProviderAdapterV2RuntimePolicy.make({
108 runtimeMode: input.thread.runtimeMode,
109 interactionMode: input.thread.interactionMode,
110 cwd,
111 });
112 }),
113 });
114 }),
115);
116​
117export function layerWithOverride(
118 override: RuntimePolicyV2Override,
119): Layer.Layer<RuntimePolicyV2, never, RuntimePolicyV2> {
120 return Layer.effect(
121 RuntimePolicyV2,
122 Effect.gen(function* () {
123 const base = yield* RuntimePolicyV2;
124 return {
125 resolve: (input) =>
126 base.resolve(input).pipe(
127 Effect.map((policy) =>
128 ProviderAdapterV2RuntimePolicy.make({
129 ...policy,
130 ...(override.cwd === undefined ? {} : { cwd: override.cwd }),
131 ...(override.approvalPolicy === undefined
132 ? {}
133 : { approvalPolicy: override.approvalPolicy }),
134 ...(override.sandboxPolicy === undefined
135 ? {}
136 : { sandboxPolicy: override.sandboxPolicy }),
137 ...(override.reasoningEffort === undefined
138 ? {}
139 : { reasoningEffort: override.reasoningEffort }),
140 }),
141 ),
142 ),
143 } satisfies RuntimePolicyV2Shape;
144 }),
145 );