-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathengine.ts
More file actions
484 lines (421 loc) · 16.1 KB
/
Copy pathengine.ts
File metadata and controls
484 lines (421 loc) · 16.1 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import { Deferred, Effect, Fiber, Ref } from "effect";
import type * as Cause from "effect/Cause";
import type {
Executor,
InvokeOptions,
ElicitationResponse,
ElicitationHandler,
ElicitationContext,
} from "@executor-js/sdk/core";
import { CodeExecutionError } from "@executor-js/codemode-core";
import type { CodeExecutor, ExecuteResult, SandboxToolInvoker } from "@executor-js/codemode-core";
import {
makeExecutorToolInvoker,
searchTools,
listExecutorSources,
describeTool,
} from "./tool-invoker";
import { ExecutionToolError } from "./errors";
import { buildExecuteDescription } from "./description";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export type ExecutionEngineConfig<
E extends Cause.YieldableError = CodeExecutionError,
> = {
readonly executor: Executor;
readonly codeExecutor: CodeExecutor<E>;
};
export type ExecutionResult =
| { readonly status: "completed"; readonly result: ExecuteResult }
| { readonly status: "paused"; readonly execution: PausedExecution };
export type PausedExecution = {
readonly id: string;
readonly elicitationContext: ElicitationContext;
};
/** Internal representation with Effect runtime state for pause/resume. */
type InternalPausedExecution<E> = PausedExecution & {
readonly response: Deferred.Deferred<typeof ElicitationResponse.Type>;
readonly fiber: Fiber.Fiber<ExecuteResult, E>;
readonly pauseSignalRef: Ref.Ref<Deferred.Deferred<InternalPausedExecution<E>>>;
};
export type ResumeResponse = {
readonly action: "accept" | "decline" | "cancel";
readonly content?: Record<string, unknown>;
};
// ---------------------------------------------------------------------------
// Result formatting
// ---------------------------------------------------------------------------
const MAX_PREVIEW_CHARS = 30_000;
const truncate = (value: string, max: number): string =>
value.length > max
? `${value.slice(0, max)}\n... [truncated ${value.length - max} chars]`
: value;
export const formatExecuteResult = (
result: ExecuteResult,
): {
text: string;
structured: Record<string, unknown>;
isError: boolean;
} => {
const resultText =
result.result != null
? typeof result.result === "string"
? result.result
: JSON.stringify(result.result, null, 2)
: null;
const logText = result.logs && result.logs.length > 0 ? result.logs.join("\n") : null;
if (result.error) {
const parts = [`Error: ${result.error}`, ...(logText ? [`\nLogs:\n${logText}`] : [])];
return {
text: truncate(parts.join("\n"), MAX_PREVIEW_CHARS),
structured: { status: "error", error: result.error, logs: result.logs ?? [] },
isError: true,
};
}
const parts = [
...(resultText ? [truncate(resultText, MAX_PREVIEW_CHARS)] : ["(no result)"]),
...(logText ? [`\nLogs:\n${logText}`] : []),
];
return {
text: parts.join("\n"),
structured: { status: "completed", result: result.result ?? null, logs: result.logs ?? [] },
isError: false,
};
};
export const formatPausedExecution = (
paused: PausedExecution,
): {
text: string;
structured: Record<string, unknown>;
} => {
const req = paused.elicitationContext.request;
const lines: string[] = [`Execution paused: ${req.message}`];
if (req._tag === "UrlElicitation") {
lines.push(`\nOpen this URL in a browser:\n${req.url}`);
lines.push("\nAfter the browser flow, resume with the executionId below:");
} else {
lines.push("\nResume with the executionId below and a response matching the requested schema:");
const schema = req.requestedSchema;
if (schema && Object.keys(schema).length > 0) {
lines.push(`\nRequested schema:\n${JSON.stringify(schema, null, 2)}`);
}
}
lines.push(`\nexecutionId: ${paused.id}`);
return {
text: lines.join("\n"),
structured: {
status: "waiting_for_interaction",
executionId: paused.id,
interaction: {
kind: req._tag === "UrlElicitation" ? "url" : "form",
message: req.message,
...(req._tag === "UrlElicitation" ? { url: req.url } : {}),
...(req._tag === "FormElicitation" ? { requestedSchema: req.requestedSchema } : {}),
},
},
};
};
// ---------------------------------------------------------------------------
// Full invoker (base + discover + describe)
// ---------------------------------------------------------------------------
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const readOptionalLimit = (value: unknown, toolName: string): number | ExecutionToolError => {
if (value === undefined) {
return 12;
}
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
return new ExecutionToolError({
message: `${toolName} limit must be a positive number when provided`,
});
}
return Math.floor(value);
};
const readOptionalOffset = (value: unknown, toolName: string): number | ExecutionToolError => {
if (value === undefined) {
return 0;
}
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
return new ExecutionToolError({
message: `${toolName} offset must be a non-negative number when provided`,
});
}
return Math.floor(value);
};
const makeFullInvoker = (executor: Executor, invokeOptions: InvokeOptions): SandboxToolInvoker => {
const base = makeExecutorToolInvoker(executor, { invokeOptions });
return {
invoke: ({ path, args }) => {
if (path === "search") {
if (!isRecord(args)) {
return Effect.fail(
new ExecutionToolError({
message:
"tools.search expects an object: { query?: string; namespace?: string; limit?: number; offset?: number }",
}),
);
}
if (args.query !== undefined && typeof args.query !== "string") {
return Effect.fail(
new ExecutionToolError({
message: "tools.search query must be a string when provided",
}),
);
}
if (args.namespace !== undefined && typeof args.namespace !== "string") {
return Effect.fail(
new ExecutionToolError({
message: "tools.search namespace must be a string when provided",
}),
);
}
const limit = readOptionalLimit(args.limit, "tools.search");
if (limit instanceof ExecutionToolError) {
return Effect.fail(limit);
}
const offset = readOptionalOffset(args.offset, "tools.search");
if (offset instanceof ExecutionToolError) {
return Effect.fail(offset);
}
return searchTools(executor, args.query ?? "", limit, {
namespace: args.namespace,
offset,
}).pipe(
Effect.withSpan("mcp.tool.dispatch", {
attributes: { "mcp.tool.name": path, "executor.tool.builtin": true },
}),
);
}
if (path === "executor.sources.list") {
if (args !== undefined && !isRecord(args)) {
return Effect.fail(
new ExecutionToolError({
message:
"tools.executor.sources.list expects an object: { query?: string; limit?: number; offset?: number }",
}),
);
}
if (isRecord(args) && args.query !== undefined && typeof args.query !== "string") {
return Effect.fail(
new ExecutionToolError({
message: "tools.executor.sources.list query must be a string when provided",
}),
);
}
const limit = readOptionalLimit(
isRecord(args) ? args.limit : undefined,
"tools.executor.sources.list",
);
if (limit instanceof ExecutionToolError) {
return Effect.fail(limit);
}
const offset = readOptionalOffset(
isRecord(args) ? args.offset : undefined,
"tools.executor.sources.list",
);
if (offset instanceof ExecutionToolError) {
return Effect.fail(offset);
}
return listExecutorSources(executor, {
query: isRecord(args) && typeof args.query === "string" ? args.query : undefined,
limit,
offset,
}).pipe(
Effect.withSpan("mcp.tool.dispatch", {
attributes: { "mcp.tool.name": path, "executor.tool.builtin": true },
}),
);
}
if (path === "describe.tool") {
if (!isRecord(args)) {
return Effect.fail(
new ExecutionToolError({
message: "tools.describe.tool expects an object: { path: string }",
}),
);
}
if (typeof args.path !== "string" || args.path.trim().length === 0) {
return Effect.fail(new ExecutionToolError({ message: "describe.tool requires a path" }));
}
if ("includeSchemas" in args) {
return Effect.fail(
new ExecutionToolError({
message: "tools.describe.tool no longer accepts includeSchemas",
}),
);
}
return describeTool(executor, args.path).pipe(
Effect.withSpan("mcp.tool.dispatch", {
attributes: {
"mcp.tool.name": path,
"executor.tool.builtin": true,
"executor.tool.target_path": args.path,
},
}),
);
}
return base.invoke({ path, args });
},
};
};
// ---------------------------------------------------------------------------
// Execution Engine
// ---------------------------------------------------------------------------
export type ExecutionEngine<E extends Cause.YieldableError = CodeExecutionError> = {
/**
* Execute code with elicitation handled inline by the provided handler.
* Use this when the host supports elicitation (e.g. MCP with elicitation capability).
*
* Fails with the code executor's typed error `E` (defaults to
* `CodeExecutionError`). Runtimes surface their own `Data.TaggedError`
* subclass, which flows through here unchanged.
*/
readonly execute: (
code: string,
options: { readonly onElicitation: ElicitationHandler },
) => Effect.Effect<ExecuteResult, E>;
/**
* Execute code, intercepting the first elicitation as a pause point.
* Use this when the host doesn't support inline elicitation.
* Returns either a completed result or a paused execution that can be resumed.
*/
readonly executeWithPause: (code: string) => Effect.Effect<ExecutionResult, E>;
/**
* Resume a paused execution. Returns a completed result, a new pause, or
* null if the executionId was not found.
*/
readonly resume: (
executionId: string,
response: ResumeResponse,
) => Effect.Effect<ExecutionResult | null, E>;
/**
* Get the dynamic tool description (workflow + namespaces).
*/
readonly getDescription: Effect.Effect<string>;
};
export const createExecutionEngine = <
E extends Cause.YieldableError = CodeExecutionError,
>(
config: ExecutionEngineConfig<E>,
): ExecutionEngine<E> => {
const { executor, codeExecutor } = config;
const pausedExecutions = new Map<string, InternalPausedExecution<E>>();
let nextId = 0;
/**
* Race a running fiber against a pause signal. Returns when either
* the fiber completes or an elicitation handler fires (whichever
* comes first). Re-used by both executeWithPause and resume.
*
* `Effect.raceFirst` (not `Effect.race`) — `race` has prefer-success
* semantics in Effect v4 ("first successful result"), which means a
* fiber failure waits indefinitely for the pause Deferred to succeed.
* For a fast `codeExecutor.execute` failure (e.g. a syntax error
* inside the dynamic worker) the pause signal never fires, so the
* outer Effect hangs until the upstream client gives up. `raceFirst`
* settles on whichever side completes first, success or failure.
*/
const awaitCompletionOrPause = (
fiber: Fiber.Fiber<ExecuteResult, E>,
pauseSignal: Deferred.Deferred<InternalPausedExecution<E>>,
): Effect.Effect<ExecutionResult, E> =>
Effect.raceFirst(
Fiber.join(fiber).pipe(
Effect.map((result): ExecutionResult => ({ status: "completed", result })),
),
Deferred.await(pauseSignal).pipe(
Effect.map((paused): ExecutionResult => ({ status: "paused", execution: paused })),
),
);
/**
* Start an execution in pause/resume mode.
*
* The sandbox is forked as a daemon because paused executions can outlive the
* caller scope that returned the first pause, such as an HTTP request handler.
*/
const startPausableExecution = Effect.fn("mcp.execute")(function* (code: string) {
yield* Effect.annotateCurrentSpan({
"mcp.execute.mode": "pausable",
"mcp.execute.code_length": code.length,
});
// Ref holds the current pause signal. The elicitation handler reads
// it each time it fires, so resume() can swap in a fresh Deferred
// before unblocking the fiber.
const pauseSignalRef = yield* Ref.make(yield* Deferred.make<InternalPausedExecution<E>>());
// Will be set once the fiber is forked.
let fiber: Fiber.Fiber<ExecuteResult, E>;
const elicitationHandler: ElicitationHandler = (ctx) =>
Effect.gen(function* () {
const responseDeferred = yield* Deferred.make<typeof ElicitationResponse.Type>();
const id = `exec_${++nextId}`;
const paused: InternalPausedExecution<E> = {
id,
elicitationContext: ctx,
response: responseDeferred,
fiber: fiber!,
pauseSignalRef,
};
pausedExecutions.set(id, paused);
const currentSignal = yield* Ref.get(pauseSignalRef);
yield* Deferred.succeed(currentSignal, paused);
// Suspend until resume() completes responseDeferred.
return yield* Deferred.await(responseDeferred);
});
const invoker = makeFullInvoker(executor, { onElicitation: elicitationHandler });
fiber = yield* Effect.forkDetach(
codeExecutor.execute(code, invoker).pipe(Effect.withSpan("executor.code.exec")),
);
const initialSignal = yield* Ref.get(pauseSignalRef);
return (yield* awaitCompletionOrPause(fiber, initialSignal)) as ExecutionResult;
});
/**
* Resume a paused execution. Swaps in a fresh pause signal, completes
* the response Deferred to unblock the fiber, then races completion
* against the next pause.
*/
const resumeExecution = Effect.fn("mcp.execute.resume")(function* (
executionId: string,
response: ResumeResponse,
) {
yield* Effect.annotateCurrentSpan({
"mcp.execute.resume.action": response.action,
});
const paused = pausedExecutions.get(executionId);
if (!paused) return null;
pausedExecutions.delete(executionId);
// Swap in a fresh pause signal BEFORE unblocking the fiber, so the
// next elicitation handler call signals this new Deferred.
const nextSignal = yield* Deferred.make<InternalPausedExecution<E>>();
yield* Ref.set(paused.pauseSignalRef, nextSignal);
yield* Deferred.succeed(paused.response, {
action: response.action as typeof ElicitationResponse.Type.action,
content: response.content,
});
return (yield* awaitCompletionOrPause(paused.fiber, nextSignal)) as ExecutionResult;
});
/**
* Inline-elicitation execute path. Wrapped so every call produces an
* `mcp.execute` span with the inner `executor.code.exec` as a child.
*/
const runInlineExecution = Effect.fn("mcp.execute")(function* (
code: string,
options: { readonly onElicitation: ElicitationHandler },
) {
yield* Effect.annotateCurrentSpan({
"mcp.execute.mode": "inline",
"mcp.execute.code_length": code.length,
});
const invoker = makeFullInvoker(executor, {
onElicitation: options.onElicitation,
});
return yield* codeExecutor
.execute(code, invoker)
.pipe(Effect.withSpan("executor.code.exec"));
});
return {
execute: runInlineExecution,
executeWithPause: startPausableExecution,
resume: resumeExecution,
getDescription: buildExecuteDescription(executor),
};
};