@abelxiaoxing/cadence 1.8.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/README.md +324 -111
  2. package/THIRD_PARTY_NOTICES.md +15 -0
  3. package/agents/design-explorer.md +14 -26
  4. package/agents/diagnosis-worker.md +14 -25
  5. package/agents/implementation-worker.md +16 -35
  6. package/config/.env.example +2 -5
  7. package/config/workflow-task.example.json +7 -0
  8. package/docs/remote-access.md +60 -0
  9. package/docs/reuse-and-distribution.md +44 -0
  10. package/docs/service-management.md +108 -0
  11. package/licenses/bundled-web.txt +6245 -0
  12. package/licenses/pi-web-ui-MIT.txt +21 -0
  13. package/licenses/pi-web-ui-reuse.md +22 -0
  14. package/package.json +94 -21
  15. package/src/index.ts +22 -1807
  16. package/src/native-build.ts +6 -0
  17. package/src/native-client.ts +103 -0
  18. package/src/native-export.ts +58 -0
  19. package/src/native-history.ts +359 -0
  20. package/src/operator-cli.mjs +453 -1937
  21. package/src/plan-proposal.ts +87 -0
  22. package/src/sdk-worker.mjs +549 -0
  23. package/src/service/contracts/active.ts +82 -0
  24. package/src/service/contracts/budget.ts +148 -0
  25. package/src/service/contracts/build.ts +67 -0
  26. package/src/service/contracts/capture.ts +48 -0
  27. package/src/service/contracts/core.ts +33 -0
  28. package/src/service/contracts/current.ts +261 -0
  29. package/src/service/contracts/domain.ts +68 -0
  30. package/src/service/contracts/history.ts +2245 -0
  31. package/src/service/contracts/models.ts +139 -0
  32. package/src/service/coordination.ts +862 -0
  33. package/src/service/private-files.ts +204 -0
  34. package/src/service-cli.mjs +5592 -0
  35. package/src/service-client.ts +747 -0
  36. package/src/service-contract.ts +245 -0
  37. package/src/service-host.mjs +22382 -0
  38. package/src/service-http-client.ts +425 -0
  39. package/src/service-wire.ts +372 -0
  40. package/src/task-shell.ts +1045 -0
  41. package/src/task-store.ts +479 -0
  42. package/src/task-types.ts +184 -0
  43. package/src/verification-diagnostics.ts +29 -72
  44. package/src/worker-capture.mjs +1197 -0
  45. package/src/worker-public.mjs +1753 -0
  46. package/src/worker-redaction.mjs +608 -0
  47. package/src/worker-tools.mjs +202 -0
  48. package/web/dist/assets/KaTeX_AMS-Regular.ttf +0 -0
  49. package/web/dist/assets/KaTeX_AMS-Regular.woff +0 -0
  50. package/web/dist/assets/KaTeX_AMS-Regular.woff2 +0 -0
  51. package/web/dist/assets/KaTeX_Caligraphic-Bold.ttf +0 -0
  52. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff +0 -0
  53. package/web/dist/assets/KaTeX_Caligraphic-Bold.woff2 +0 -0
  54. package/web/dist/assets/KaTeX_Caligraphic-Regular.ttf +0 -0
  55. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff +0 -0
  56. package/web/dist/assets/KaTeX_Caligraphic-Regular.woff2 +0 -0
  57. package/web/dist/assets/KaTeX_Fraktur-Bold.ttf +0 -0
  58. package/web/dist/assets/KaTeX_Fraktur-Bold.woff +0 -0
  59. package/web/dist/assets/KaTeX_Fraktur-Bold.woff2 +0 -0
  60. package/web/dist/assets/KaTeX_Fraktur-Regular.ttf +0 -0
  61. package/web/dist/assets/KaTeX_Fraktur-Regular.woff +0 -0
  62. package/web/dist/assets/KaTeX_Fraktur-Regular.woff2 +0 -0
  63. package/web/dist/assets/KaTeX_Main-Bold.ttf +0 -0
  64. package/web/dist/assets/KaTeX_Main-Bold.woff +0 -0
  65. package/web/dist/assets/KaTeX_Main-Bold.woff2 +0 -0
  66. package/web/dist/assets/KaTeX_Main-BoldItalic.ttf +0 -0
  67. package/web/dist/assets/KaTeX_Main-BoldItalic.woff +0 -0
  68. package/web/dist/assets/KaTeX_Main-BoldItalic.woff2 +0 -0
  69. package/web/dist/assets/KaTeX_Main-Italic.ttf +0 -0
  70. package/web/dist/assets/KaTeX_Main-Italic.woff +0 -0
  71. package/web/dist/assets/KaTeX_Main-Italic.woff2 +0 -0
  72. package/web/dist/assets/KaTeX_Main-Regular.ttf +0 -0
  73. package/web/dist/assets/KaTeX_Main-Regular.woff +0 -0
  74. package/web/dist/assets/KaTeX_Main-Regular.woff2 +0 -0
  75. package/web/dist/assets/KaTeX_Math-BoldItalic.ttf +0 -0
  76. package/web/dist/assets/KaTeX_Math-BoldItalic.woff +0 -0
  77. package/web/dist/assets/KaTeX_Math-BoldItalic.woff2 +0 -0
  78. package/web/dist/assets/KaTeX_Math-Italic.ttf +0 -0
  79. package/web/dist/assets/KaTeX_Math-Italic.woff +0 -0
  80. package/web/dist/assets/KaTeX_Math-Italic.woff2 +0 -0
  81. package/web/dist/assets/KaTeX_SansSerif-Bold.ttf +0 -0
  82. package/web/dist/assets/KaTeX_SansSerif-Bold.woff +0 -0
  83. package/web/dist/assets/KaTeX_SansSerif-Bold.woff2 +0 -0
  84. package/web/dist/assets/KaTeX_SansSerif-Italic.ttf +0 -0
  85. package/web/dist/assets/KaTeX_SansSerif-Italic.woff +0 -0
  86. package/web/dist/assets/KaTeX_SansSerif-Italic.woff2 +0 -0
  87. package/web/dist/assets/KaTeX_SansSerif-Regular.ttf +0 -0
  88. package/web/dist/assets/KaTeX_SansSerif-Regular.woff +0 -0
  89. package/web/dist/assets/KaTeX_SansSerif-Regular.woff2 +0 -0
  90. package/web/dist/assets/KaTeX_Script-Regular.ttf +0 -0
  91. package/web/dist/assets/KaTeX_Script-Regular.woff +0 -0
  92. package/web/dist/assets/KaTeX_Script-Regular.woff2 +0 -0
  93. package/web/dist/assets/KaTeX_Size1-Regular.ttf +0 -0
  94. package/web/dist/assets/KaTeX_Size1-Regular.woff +0 -0
  95. package/web/dist/assets/KaTeX_Size1-Regular.woff2 +0 -0
  96. package/web/dist/assets/KaTeX_Size2-Regular.ttf +0 -0
  97. package/web/dist/assets/KaTeX_Size2-Regular.woff +0 -0
  98. package/web/dist/assets/KaTeX_Size2-Regular.woff2 +0 -0
  99. package/web/dist/assets/KaTeX_Size3-Regular.ttf +0 -0
  100. package/web/dist/assets/KaTeX_Size3-Regular.woff +0 -0
  101. package/web/dist/assets/KaTeX_Size3-Regular.woff2 +0 -0
  102. package/web/dist/assets/KaTeX_Size4-Regular.ttf +0 -0
  103. package/web/dist/assets/KaTeX_Size4-Regular.woff +0 -0
  104. package/web/dist/assets/KaTeX_Size4-Regular.woff2 +0 -0
  105. package/web/dist/assets/KaTeX_Typewriter-Regular.ttf +0 -0
  106. package/web/dist/assets/KaTeX_Typewriter-Regular.woff +0 -0
  107. package/web/dist/assets/KaTeX_Typewriter-Regular.woff2 +0 -0
  108. package/web/dist/assets/cadence.css +2 -0
  109. package/web/dist/assets/cadence.js +7 -0
  110. package/web/dist/assets/markdown.js +296 -0
  111. package/web/dist/assets/react.js +9 -0
  112. package/web/dist/assets/rolldown-runtime.js +1 -0
  113. package/web/dist/assets/timeline-client.js +41 -0
  114. package/web/dist/assets/vis-timeline.css +1274 -0
  115. package/web/dist/assets/vis-timeline.js +32 -0
  116. package/web/dist/index.html +18 -0
  117. package/web/dist/manifest.json +505 -0
  118. package/config/plan-draft.example.json +0 -125
  119. package/config/plan-draft.multiple-tasks.example.json +0 -375
  120. package/config/plan-draft.quick.example.json +0 -78
  121. package/config/routes.example.json +0 -39
  122. package/prompts/abel-design.md +0 -382
  123. package/prompts/abel-diagnose.md +0 -71
  124. package/prompts/abel-implement.md +0 -284
  125. package/prompts/abel-init.md +0 -52
  126. package/src/activation.ts +0 -44
  127. package/src/activity-contracts.ts +0 -82
  128. package/src/agent-registry.ts +0 -53
  129. package/src/apply-transaction.ts +0 -1437
  130. package/src/artifact-store.ts +0 -213
  131. package/src/build-windows-job.mjs +0 -95
  132. package/src/candidate-artifact.ts +0 -143
  133. package/src/candidate-patch.ts +0 -271
  134. package/src/candidate-workspace.ts +0 -80
  135. package/src/canonical.ts +0 -16
  136. package/src/change-contract-schema.ts +0 -191
  137. package/src/change-contract.ts +0 -324
  138. package/src/change-verification.ts +0 -1617
  139. package/src/child-budget.ts +0 -58
  140. package/src/child-model.ts +0 -142
  141. package/src/child-session.ts +0 -640
  142. package/src/contracts.ts +0 -2476
  143. package/src/control-contracts.ts +0 -382
  144. package/src/delivery-compiler.ts +0 -1805
  145. package/src/delivery-revision.ts +0 -42
  146. package/src/design-control.ts +0 -1299
  147. package/src/design-diagnostics.ts +0 -181
  148. package/src/design-journal.ts +0 -1773
  149. package/src/directory-sync.ts +0 -36
  150. package/src/durable-contracts.ts +0 -231
  151. package/src/durable-workflow.ts +0 -978
  152. package/src/evidence-draft.ts +0 -122
  153. package/src/execution-profile.ts +0 -64
  154. package/src/execution-retention.ts +0 -77
  155. package/src/file-snapshot.ts +0 -163
  156. package/src/implement-continuation.ts +0 -559
  157. package/src/implement-graph.ts +0 -690
  158. package/src/implement-plan.ts +0 -72
  159. package/src/install-config.d.mts +0 -6
  160. package/src/isolation-backend.ts +0 -353
  161. package/src/model-source.ts +0 -22
  162. package/src/openspec-cli.ts +0 -494
  163. package/src/operator-cli-entry.mjs +0 -24
  164. package/src/operator-tools.ts +0 -309
  165. package/src/package-candidate.ts +0 -227
  166. package/src/package-delivery.ts +0 -486
  167. package/src/package-state.ts +0 -320
  168. package/src/package-verification.ts +0 -635
  169. package/src/package-workflow.ts +0 -342
  170. package/src/packet-runtime.ts +0 -570
  171. package/src/parent-provider.ts +0 -375
  172. package/src/phase-execution.ts +0 -2259
  173. package/src/pi-adapter.ts +0 -19
  174. package/src/plan-draft-summary.ts +0 -527
  175. package/src/plan-draft.ts +0 -502
  176. package/src/remove-path.ts +0 -32
  177. package/src/route-policy.ts +0 -545
  178. package/src/run-state.ts +0 -420
  179. package/src/run-store.ts +0 -1179
  180. package/src/safe-path.ts +0 -77
  181. package/src/scoped-grep-worker.mjs +0 -23
  182. package/src/scoped-tools.ts +0 -775
  183. package/src/single-task-draft.ts +0 -142
  184. package/src/sqlite-schema.ts +0 -171
  185. package/src/state-root.ts +0 -215
  186. package/src/storage-schema.ts +0 -360
  187. package/src/subagent-activity.ts +0 -1417
  188. package/src/submit-tool.ts +0 -950
  189. package/src/task-ledger.ts +0 -1338
  190. package/src/transport-budget.ts +0 -52
  191. package/src/verification-capability.ts +0 -1191
  192. package/src/verification-environment-io.ts +0 -389
  193. package/src/verification-environment.ts +0 -162
  194. package/src/verification-identity.ts +0 -77
  195. package/src/verification-scan.ts +0 -64
  196. package/src/windows-job-backend.ts +0 -419
  197. package/src/windows-job.c +0 -149
  198. package/src/worker-broker.ts +0 -806
  199. package/src/workflow-engine.ts +0 -28
  200. package/src/workflow-policy.ts +0 -1679
  201. package/src/workflow-recovery-policy.ts +0 -97
  202. package/src/workflow-scheduling.ts +0 -105
  203. package/src/workflow-state-machine.ts +0 -4355
  204. package/src/workflow-status.ts +0 -576
  205. package/src/workspace-io-entry.mjs +0 -76
  206. package/src/workspace-io-worker.mjs +0 -1312
  207. package/src/workspace-io.ts +0 -89
  208. package/src/workspace-store.ts +0 -634
@@ -1,375 +0,0 @@
1
- import {
2
- createModels,
3
- createProvider,
4
- InMemoryCredentialStore,
5
- lazyApi,
6
- type Model,
7
- type Models,
8
- type Provider,
9
- type StreamOptions,
10
- } from "@earendil-works/pi-ai";
11
- import type { ChildFailure } from "./contracts.ts";
12
- import type { ParentModelSource } from "./model-source.ts";
13
- import {
14
- type CustomRoutePolicy,
15
- type InheritedRoutePolicy,
16
- type ResolvedCustomRoute,
17
- resolveCustomRoute,
18
- } from "./route-policy.ts";
19
-
20
- const SUBAGENT_PROVIDER_ID = "abel-subagent";
21
-
22
- export interface PhaseTransportObserver {
23
- onResponse?(): void;
24
- }
25
-
26
- function observedStreamOptions<T extends StreamOptions>(
27
- options?: T,
28
- observer?: PhaseTransportObserver,
29
- ): T | undefined {
30
- if (!observer?.onResponse) return options;
31
- const childOnResponse = options?.onResponse;
32
- return {
33
- ...options,
34
- onResponse: async (response, requestModel) => {
35
- observer.onResponse?.();
36
- await childOnResponse?.(response, requestModel);
37
- },
38
- } as T;
39
- }
40
-
41
- // Public API modules, selected explicitly by the closed route dialect. No
42
- // global compat registry or provider-specific payload rewriting is involved.
43
- const routeApis = {
44
- "openai-completions": lazyApi(
45
- () => import("@earendil-works/pi-ai/api/openai-completions"),
46
- ),
47
- "openai-responses": lazyApi(
48
- () => import("@earendil-works/pi-ai/api/openai-responses"),
49
- ),
50
- "anthropic-messages": lazyApi(
51
- () => import("@earendil-works/pi-ai/api/anthropic-messages"),
52
- ),
53
- };
54
-
55
- function subagentStreams(
56
- dialect: ResolvedCustomRoute["dialect"],
57
- observer?: PhaseTransportObserver,
58
- ) {
59
- const api = routeApis[dialect];
60
- return {
61
- stream: ((model, context, options) =>
62
- api.stream(
63
- model,
64
- context,
65
- observedStreamOptions({ ...options, maxRetries: 0 }, observer),
66
- )) as Provider["stream"],
67
- streamSimple: ((model, context, options) =>
68
- api.streamSimple(
69
- model,
70
- context,
71
- observedStreamOptions({ ...options, maxRetries: 0 }, observer),
72
- )) as Provider["streamSimple"],
73
- };
74
- }
75
-
76
- function modelsForProvider(provider: Provider, signal?: AbortSignal): Models {
77
- signal?.throwIfAborted();
78
- const models = createModels({ credentials: new InMemoryCredentialStore() });
79
- models.setProvider(provider);
80
- return models;
81
- }
82
-
83
- function customSubagentModel(endpoint: ResolvedCustomRoute): Model<string> {
84
- return {
85
- id: endpoint.model,
86
- name: endpoint.model,
87
- api: endpoint.dialect,
88
- provider: SUBAGENT_PROVIDER_ID,
89
- baseUrl: endpoint.url,
90
- reasoning: true,
91
- input: ["text"],
92
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
93
- contextWindow: endpoint.contextWindow,
94
- maxTokens: endpoint.maxTokens,
95
- ...(endpoint.dialect === "openai-completions"
96
- ? { compat: { supportsDeveloperRole: false } }
97
- : {}),
98
- } as Model<string>;
99
- }
100
-
101
- function customSubagentProvider(
102
- endpoint: ResolvedCustomRoute,
103
- model: Model<string>,
104
- observer?: PhaseTransportObserver,
105
- ): Provider {
106
- return createProvider({
107
- id: SUBAGENT_PROVIDER_ID,
108
- name: "Abel Subagent Endpoint",
109
- baseUrl: endpoint.url,
110
- auth: {
111
- apiKey: {
112
- name: "Abel subagent endpoint auth",
113
- resolve: async () =>
114
- endpoint.apiKey
115
- ? { auth: { apiKey: endpoint.apiKey }, source: "subagent-endpoint" }
116
- : {
117
- auth: {
118
- apiKey: "unused",
119
- headers: { authorization: null, "x-api-key": null },
120
- },
121
- source: "subagent-endpoint-keyless",
122
- },
123
- },
124
- },
125
- models: [model],
126
- api: subagentStreams(endpoint.dialect, observer),
127
- }) as Provider;
128
- }
129
-
130
- export async function customPhaseRuntime(
131
- route: CustomRoutePolicy,
132
- signal?: AbortSignal,
133
- environment: Readonly<Record<string, string | undefined>> = process.env,
134
- observer?: PhaseTransportObserver,
135
- ): Promise<PhaseRuntimeResult> {
136
- if (signal?.aborted) return cancelledPhaseRuntime();
137
- const endpoint = resolveCustomRoute(route, environment);
138
- const model = customSubagentModel(endpoint);
139
- const provider = customSubagentProvider(endpoint, model, observer);
140
- let modelRuntime: Models;
141
- try {
142
- modelRuntime = modelsForProvider(provider, signal);
143
- } catch (error) {
144
- if (isCancellation(error, signal)) return cancelledPhaseRuntime();
145
- throw error;
146
- }
147
- if (signal?.aborted) return cancelledPhaseRuntime();
148
- return { ok: true, modelRuntime, model };
149
- }
150
-
151
- type PhaseRuntimeFailure = Extract<
152
- ChildFailure,
153
- { kind: "cancelled" | "environment" | "transport" }
154
- >;
155
-
156
- export type PhaseRuntimeResult =
157
- | {
158
- ok: true;
159
- modelRuntime: Models;
160
- model: Model<string>;
161
- }
162
- | { ok: false; error: string; failure: PhaseRuntimeFailure };
163
-
164
- function phaseRuntimeFailure(
165
- failure: PhaseRuntimeFailure,
166
- error: string,
167
- ): Extract<PhaseRuntimeResult, { ok: false }> {
168
- return { ok: false, error, failure };
169
- }
170
-
171
- function cancelledPhaseRuntime(): Extract<PhaseRuntimeResult, { ok: false }> {
172
- return phaseRuntimeFailure(
173
- { kind: "cancelled", code: "cancelled" },
174
- "child phase cancelled",
175
- );
176
- }
177
-
178
- function isCancellation(error: unknown, signal?: AbortSignal): boolean {
179
- return signal?.aborted === true && error === signal.reason;
180
- }
181
-
182
- export function phaseProvider(
183
- parent: Provider,
184
- auth: {
185
- apiKey?: string;
186
- headers?: Record<string, string | null>;
187
- baseUrl?: string;
188
- env?: Record<string, string>;
189
- },
190
- observer?: PhaseTransportObserver,
191
- ): Provider {
192
- auth = structuredClone(auth);
193
- return {
194
- id: parent.id,
195
- name: parent.name,
196
- getModels: () => parent.getModels(),
197
- baseUrl: auth.baseUrl ?? parent.baseUrl,
198
- headers: { ...parent.headers, ...auth.headers },
199
- auth: {
200
- apiKey: {
201
- name: "Abel phase-local parent auth",
202
- resolve: async () => ({
203
- auth: {
204
- apiKey: auth.apiKey,
205
- headers: auth.headers,
206
- baseUrl: auth.baseUrl,
207
- },
208
- env: auth.env,
209
- }),
210
- },
211
- },
212
- stream(model, context, options) {
213
- return parent.stream(model, context, {
214
- ...observedStreamOptions(options, observer),
215
- apiKey: auth.apiKey,
216
- headers: { ...options?.headers, ...auth.headers },
217
- env: { ...options?.env, ...auth.env },
218
- maxRetries: 0,
219
- } as never);
220
- },
221
- streamSimple(model, context, options) {
222
- return parent.streamSimple(model, context, {
223
- ...observedStreamOptions(options, observer),
224
- apiKey: auth.apiKey,
225
- headers: { ...options?.headers, ...auth.headers },
226
- env: { ...options?.env, ...auth.env },
227
- maxRetries: 0,
228
- });
229
- },
230
- };
231
- }
232
-
233
- function modelKeyFor(model: Model<string>) {
234
- return {
235
- provider: model.provider,
236
- id: model.id,
237
- api: model.api,
238
- baseUrl: model.baseUrl,
239
- };
240
- }
241
-
242
- function sameSelectedModel(
243
- left: Model<string>,
244
- right: ReturnType<typeof modelKeyFor>,
245
- ): boolean {
246
- return (
247
- left.provider === right.provider &&
248
- left.id === right.id &&
249
- left.api === right.api &&
250
- left.baseUrl === right.baseUrl
251
- );
252
- }
253
-
254
- export async function runtimeFromContext(
255
- ctx: ParentModelSource,
256
- signal?: AbortSignal,
257
- observer?: PhaseTransportObserver,
258
- ): Promise<PhaseRuntimeResult> {
259
- if (signal?.aborted) return cancelledPhaseRuntime();
260
- if (!ctx.model) {
261
- return phaseRuntimeFailure(
262
- { kind: "environment", code: "sandbox-runtime-unavailable" },
263
- "parent model is unavailable",
264
- );
265
- }
266
- const selectedModel = structuredClone(ctx.model);
267
- const selectedModelKey = modelKeyFor(selectedModel);
268
- const admittedProvider = ctx.modelRegistry.getProvider(
269
- selectedModel.provider,
270
- );
271
- let resolved: Awaited<
272
- ReturnType<typeof ctx.modelRegistry.getApiKeyAndHeaders>
273
- >;
274
- try {
275
- resolved = await abortable(
276
- ctx.modelRegistry.getApiKeyAndHeaders(selectedModel),
277
- signal,
278
- );
279
- } catch (error) {
280
- if (isCancellation(error, signal)) return cancelledPhaseRuntime();
281
- throw error;
282
- }
283
- if (!resolved.ok) {
284
- return phaseRuntimeFailure(
285
- { kind: "environment", code: "sandbox-runtime-unavailable" },
286
- "phase authentication is unavailable",
287
- );
288
- }
289
- if (signal?.aborted) return cancelledPhaseRuntime();
290
- const model = {
291
- ...selectedModel,
292
- baseUrl: resolved.baseUrl ?? selectedModel.baseUrl,
293
- } as Model<string>;
294
- const delegate = ctx.modelRegistry.getProvider(model.provider);
295
- if (delegate !== admittedProvider)
296
- return phaseRuntimeFailure(
297
- {
298
- kind: "environment",
299
- code: "sandbox-runtime-unavailable",
300
- stage: "phase-runtime",
301
- },
302
- "parent Provider changed during authentication",
303
- );
304
- if (!delegate)
305
- return phaseRuntimeFailure(
306
- {
307
- kind: "environment",
308
- code: "sandbox-runtime-unavailable",
309
- stage: "phase-runtime",
310
- },
311
- "parent Provider is unavailable",
312
- );
313
- if (!ctx.model || !sameSelectedModel(ctx.model, selectedModelKey))
314
- return phaseRuntimeFailure(
315
- {
316
- kind: "environment",
317
- code: "sandbox-runtime-unavailable",
318
- stage: "phase-runtime",
319
- },
320
- "parent model changed during authentication",
321
- );
322
- // Snapshot effective Provider behavior and auth, never mutate its registry or
323
- // capture host-session callbacks. Explicit provider implementations still run.
324
- const provider = phaseProvider(delegate, resolved, observer);
325
- let modelRuntime: Models;
326
- try {
327
- modelRuntime = modelsForProvider(provider, signal);
328
- } catch (error) {
329
- if (isCancellation(error, signal)) return cancelledPhaseRuntime();
330
- throw error;
331
- }
332
- if (signal?.aborted) return cancelledPhaseRuntime();
333
- if (
334
- !ctx.model ||
335
- !sameSelectedModel(ctx.model, selectedModelKey) ||
336
- ctx.modelRegistry.getProvider(model.provider) !== delegate
337
- ) {
338
- return phaseRuntimeFailure(
339
- {
340
- kind: "environment",
341
- code: "sandbox-runtime-unavailable",
342
- stage: "phase-runtime",
343
- },
344
- "parent model or Provider changed during admission",
345
- );
346
- }
347
- return { ok: true, modelRuntime, model };
348
- }
349
-
350
- export async function runtimeForWorkerRoute(
351
- route: CustomRoutePolicy | InheritedRoutePolicy,
352
- ctx: ParentModelSource,
353
- signal?: AbortSignal,
354
- environment: Readonly<Record<string, string | undefined>> = process.env,
355
- observer?: PhaseTransportObserver,
356
- ): Promise<PhaseRuntimeResult> {
357
- return route.kind === "custom"
358
- ? customPhaseRuntime(route, signal, environment, observer)
359
- : runtimeFromContext(ctx, signal, observer);
360
- }
361
-
362
- function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
363
- if (!signal) return promise;
364
- signal.throwIfAborted();
365
- return new Promise<T>((resolve, reject) => {
366
- const onAbort = () => {
367
- signal.removeEventListener("abort", onAbort);
368
- reject(signal.reason);
369
- };
370
- signal.addEventListener("abort", onAbort, { once: true });
371
- promise
372
- .then(resolve, reject)
373
- .finally(() => signal.removeEventListener("abort", onAbort));
374
- });
375
- }