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