@leitwerk-dev/worker 0.1.6 → 0.1.7

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 (48) hide show
  1. package/dist/{chunk-6OAKGOYC.js → chunk-ZTJ7UAOC.js} +177 -44
  2. package/dist/chunk-ZTJ7UAOC.js.map +1 -0
  3. package/dist/integration-tool-bridge.d.ts +14 -0
  4. package/dist/integration-tool-bridge.d.ts.map +1 -0
  5. package/dist/integration-tool-bridge.js +53 -0
  6. package/dist/integration-tool-bridge.js.map +1 -0
  7. package/dist/question-bridge.d.ts +1 -3
  8. package/dist/question-bridge.d.ts.map +1 -1
  9. package/dist/question-bridge.js +8 -39
  10. package/dist/question-bridge.js.map +1 -1
  11. package/dist/replaying-request-bridge.d.ts +17 -0
  12. package/dist/replaying-request-bridge.d.ts.map +1 -0
  13. package/dist/replaying-request-bridge.js +71 -0
  14. package/dist/replaying-request-bridge.js.map +1 -0
  15. package/dist/runtime/bootstrap-session.d.ts +1 -0
  16. package/dist/runtime/bootstrap-session.d.ts.map +1 -1
  17. package/dist/runtime/bootstrap-session.js +1 -0
  18. package/dist/runtime/bootstrap-session.js.map +1 -1
  19. package/dist/runtime/index.d.ts.map +1 -1
  20. package/dist/runtime/index.js +9 -0
  21. package/dist/runtime/index.js.map +1 -1
  22. package/dist/runtime/llm-turn-execution.d.ts +7 -0
  23. package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
  24. package/dist/runtime/llm-turn-execution.js +24 -2
  25. package/dist/runtime/llm-turn-execution.js.map +1 -1
  26. package/dist/runtime/turn-execution.d.ts +1 -0
  27. package/dist/runtime/turn-execution.d.ts.map +1 -1
  28. package/dist/runtime/turn-execution.js +1 -0
  29. package/dist/runtime/turn-execution.js.map +1 -1
  30. package/dist/turn-outcome-tool-session.d.ts +1 -0
  31. package/dist/turn-outcome-tool-session.d.ts.map +1 -1
  32. package/dist/turn-outcome-tool-session.js +1 -0
  33. package/dist/turn-outcome-tool-session.js.map +1 -1
  34. package/package.json +7 -7
  35. package/src/integration-tool-bridge.ts +84 -0
  36. package/src/question-bridge.ts +30 -47
  37. package/src/replaying-request-bridge.ts +83 -0
  38. package/src/runtime/bootstrap-session.ts +2 -0
  39. package/src/runtime/index.ts +12 -0
  40. package/src/runtime/llm-turn-execution.ts +38 -5
  41. package/src/runtime/turn-execution.ts +2 -0
  42. package/src/turn-outcome-tool-session.ts +2 -0
  43. package/dist/chunk-6OAKGOYC.js.map +0 -1
  44. package/dist/test-helpers/worker-message-builders.d.ts +0 -21
  45. package/dist/test-helpers/worker-message-builders.d.ts.map +0 -1
  46. package/dist/test-helpers/worker-message-builders.js +0 -62
  47. package/dist/test-helpers/worker-message-builders.js.map +0 -1
  48. package/src/test-helpers/worker-message-builders.ts +0 -92
@@ -1997,6 +1997,131 @@ async function deliverBatch(session, inputs, hasActiveTurn) {
1997
1997
  return out;
1998
1998
  }
1999
1999
 
2000
+ // src/replaying-request-bridge.ts
2001
+ var ReplayingRequestBridge = class {
2002
+ constructor(emit, resolveResponse, cancel) {
2003
+ this.emit = emit;
2004
+ this.resolveResponse = resolveResponse;
2005
+ this.cancel = cancel;
2006
+ }
2007
+ emit;
2008
+ resolveResponse;
2009
+ cancel;
2010
+ pending = /* @__PURE__ */ new Map();
2011
+ request(payload, signal, cancellationError) {
2012
+ if (signal.aborted) {
2013
+ return Promise.reject(new Error(cancellationError));
2014
+ }
2015
+ if (this.pending.has(payload.toolCallId)) {
2016
+ return Promise.reject(new Error(`Duplicate in-flight request '${payload.toolCallId}'`));
2017
+ }
2018
+ return new Promise((resolve, reject) => {
2019
+ const onAbort = () => {
2020
+ this.pending.delete(payload.toolCallId);
2021
+ try {
2022
+ this.cancel?.(payload);
2023
+ } catch {
2024
+ }
2025
+ reject(new Error(cancellationError));
2026
+ };
2027
+ signal.addEventListener("abort", onAbort, { once: true });
2028
+ this.pending.set(payload.toolCallId, {
2029
+ payload,
2030
+ resolve,
2031
+ reject,
2032
+ removeAbort: () => signal.removeEventListener("abort", onAbort)
2033
+ });
2034
+ this.emit(payload);
2035
+ });
2036
+ }
2037
+ handle(payload) {
2038
+ const pending = this.pending.get(payload.toolCallId);
2039
+ if (!pending || pending.payload.turnRecordId !== payload.turnRecordId) return false;
2040
+ this.pending.delete(payload.toolCallId);
2041
+ pending.removeAbort();
2042
+ try {
2043
+ pending.resolve(this.resolveResponse(payload));
2044
+ } catch (error) {
2045
+ pending.reject(error instanceof Error ? error : new Error(String(error)));
2046
+ }
2047
+ return true;
2048
+ }
2049
+ replay() {
2050
+ for (const pending of this.pending.values()) this.emit(pending.payload);
2051
+ }
2052
+ cancelAll(reason) {
2053
+ for (const pending of this.pending.values()) {
2054
+ pending.removeAbort();
2055
+ try {
2056
+ this.cancel?.(pending.payload);
2057
+ } catch {
2058
+ }
2059
+ pending.reject(new Error(reason));
2060
+ }
2061
+ this.pending.clear();
2062
+ }
2063
+ };
2064
+
2065
+ // src/integration-tool-bridge.ts
2066
+ var WorkerIntegrationToolBridge = class {
2067
+ bridge;
2068
+ constructor(reporter) {
2069
+ this.bridge = new ReplayingRequestBridge(
2070
+ (payload) => reporter.project({ kind: "protocol", type: "worker.integration_tool_request", payload }),
2071
+ (payload) => {
2072
+ if (!payload.ok) {
2073
+ throw new Error(payload.error ?? "Integration tool execution failed");
2074
+ }
2075
+ return payload.result;
2076
+ },
2077
+ (payload) => reporter.project({
2078
+ kind: "protocol",
2079
+ type: "worker.integration_tool_cancel",
2080
+ payload: {
2081
+ turnRecordId: payload.turnRecordId,
2082
+ toolCallId: payload.toolCallId,
2083
+ toolName: payload.toolName
2084
+ }
2085
+ })
2086
+ );
2087
+ }
2088
+ createTools(declarations, turnRecordId) {
2089
+ return declarations.map((declaration) => ({
2090
+ ...declaration,
2091
+ executionMode: "sequential",
2092
+ execute: async (args, context) => {
2093
+ if (!context?.toolCallId) {
2094
+ throw new Error("Integration tool call is missing its Pi identity");
2095
+ }
2096
+ const resumeGuards = context.suspendPromptGuards?.();
2097
+ try {
2098
+ return await this.bridge.request(
2099
+ {
2100
+ turnRecordId,
2101
+ toolCallId: context.toolCallId,
2102
+ toolName: declaration.name,
2103
+ args
2104
+ },
2105
+ context.signal,
2106
+ "Integration tool call cancelled because the turn stopped"
2107
+ );
2108
+ } finally {
2109
+ resumeGuards?.();
2110
+ }
2111
+ }
2112
+ }));
2113
+ }
2114
+ handle(message) {
2115
+ return this.bridge.handle(message.payload);
2116
+ }
2117
+ replay() {
2118
+ this.bridge.replay();
2119
+ }
2120
+ cancelAll(reason) {
2121
+ this.bridge.cancelAll(reason);
2122
+ }
2123
+ };
2124
+
2000
2125
  // src/event-translator.ts
2001
2126
  function translatePiEvent(piEvent, currentSelectedTurnId) {
2002
2127
  return {
@@ -2140,55 +2265,32 @@ function createPiEventReporter(options) {
2140
2265
 
2141
2266
  // src/question-bridge.ts
2142
2267
  var WorkerQuestionBridge = class {
2268
+ bridge;
2143
2269
  constructor(reporter) {
2144
- this.reporter = reporter;
2270
+ this.bridge = new ReplayingRequestBridge(
2271
+ (payload) => reporter.project({ kind: "protocol", type: "worker.question_requested", payload }),
2272
+ (payload) => [...payload.answers]
2273
+ );
2145
2274
  }
2146
- reporter;
2147
- pending = /* @__PURE__ */ new Map();
2148
2275
  request(request) {
2149
- if (this.pending.has(request.toolCallId)) {
2150
- return Promise.reject(new Error("Duplicate in-flight question request"));
2151
- }
2152
- const payload = {
2153
- turnRecordId: request.turnRecordId,
2154
- toolCallId: request.toolCallId,
2155
- questions: request.questions
2156
- };
2157
- return new Promise((resolve, reject) => {
2158
- const onAbort = () => {
2159
- this.pending.delete(request.toolCallId);
2160
- reject(new Error("Question request cancelled because the turn stopped"));
2161
- };
2162
- request.signal.addEventListener("abort", onAbort, { once: true });
2163
- this.pending.set(request.toolCallId, {
2164
- payload,
2165
- resolve,
2166
- reject,
2167
- removeAbort: () => request.signal.removeEventListener("abort", onAbort)
2168
- });
2169
- this.emit(payload);
2170
- });
2276
+ return this.bridge.request(
2277
+ {
2278
+ turnRecordId: request.turnRecordId,
2279
+ toolCallId: request.toolCallId,
2280
+ questions: request.questions
2281
+ },
2282
+ request.signal,
2283
+ "Question request cancelled because the turn stopped"
2284
+ );
2171
2285
  }
2172
2286
  handle(message) {
2173
- const pending = this.pending.get(message.payload.toolCallId);
2174
- if (!pending || pending.payload.turnRecordId !== message.payload.turnRecordId) return false;
2175
- this.pending.delete(message.payload.toolCallId);
2176
- pending.removeAbort();
2177
- pending.resolve([...message.payload.answers]);
2178
- return true;
2287
+ return this.bridge.handle(message.payload);
2179
2288
  }
2180
2289
  replay() {
2181
- for (const pending of this.pending.values()) this.emit(pending.payload);
2290
+ this.bridge.replay();
2182
2291
  }
2183
2292
  cancelAll(reason) {
2184
- for (const pending of this.pending.values()) {
2185
- pending.removeAbort();
2186
- pending.reject(new Error(reason));
2187
- }
2188
- this.pending.clear();
2189
- }
2190
- emit(payload) {
2191
- this.reporter.project({ kind: "protocol", type: "worker.question_requested", payload });
2293
+ this.bridge.cancelAll(reason);
2192
2294
  }
2193
2295
  };
2194
2296
 
@@ -3612,7 +3714,8 @@ function validatePreparedSession(input) {
3612
3714
  workspaceRoot: payload.treePaths.workspaceRoot,
3613
3715
  turnResultMarkdownBySemanticRef: payload.turnResultMarkdownBySemanticRef,
3614
3716
  turnResultMarkdownByProduct: payload.turnResultMarkdownByProduct,
3615
- settings
3717
+ settings,
3718
+ integrationTools: payload.integrationTools ?? []
3616
3719
  };
3617
3720
  if (binding.definition.kind === "automatic") {
3618
3721
  return { ...base, kind: "automatic", piAvailable: false, activeModelProfileId: null };
@@ -5982,7 +6085,8 @@ function createTurnOutcomeToolSession(input) {
5982
6085
  ...hasResultImageTool && input.resultImageTool ? [input.resultImageTool] : [],
5983
6086
  ...input.turnDef.turnResultMarkdown?.mode === "tool_call" && input.turnDef.turnResultMarkdown.toolName === "markdown_result" ? [createMarkdownResultTool(turnResultMarkdownStateRef)] : [],
5984
6087
  ...outcomeActions,
5985
- ...questionTool ? [questionTool] : []
6088
+ ...questionTool ? [questionTool] : [],
6089
+ ...input.integrationTools ?? []
5986
6090
  ];
5987
6091
  const activeTools = resolveTurnActiveToolNames({
5988
6092
  turnId: input.turnId,
@@ -6407,6 +6511,22 @@ ${guidance}`
6407
6511
  } : targetedInput
6408
6512
  );
6409
6513
  }
6514
+ var PI_SESSION_BOOTSTRAP_ENTRY_TYPES = /* @__PURE__ */ new Set(["model_change", "thinking_level_change"]);
6515
+ function visitPiTreeEntries(nodes, visit) {
6516
+ for (const node of nodes) {
6517
+ visit(node.entry.type);
6518
+ visitPiTreeEntries(node.children, visit);
6519
+ }
6520
+ }
6521
+ function isPiTreeEmptyForPreparedEmptyPlan(piHandle) {
6522
+ let hasNonBootstrap = false;
6523
+ visitPiTreeEntries(piHandle.getTree(), (entryType) => {
6524
+ if (!PI_SESSION_BOOTSTRAP_ENTRY_TYPES.has(entryType)) {
6525
+ hasNonBootstrap = true;
6526
+ }
6527
+ });
6528
+ return !hasNonBootstrap;
6529
+ }
6410
6530
  function treePlanFromPreparedStart(input) {
6411
6531
  const { preparedStart, turnDef, piHandle } = input;
6412
6532
  if (preparedStart.pathType !== turnDef.branchType) {
@@ -6418,7 +6538,7 @@ function treePlanFromPreparedStart(input) {
6418
6538
  switch (preparedStart.startTarget.kind) {
6419
6539
  case "current_leaf":
6420
6540
  if (preparedStart.forkPiEntryId === null) {
6421
- if (!input.allowRetainedExecution && (piHandle.getLeafId() !== null || piHandle.getTree().length !== 0)) {
6541
+ if (!input.allowRetainedExecution && !isPiTreeEmptyForPreparedEmptyPlan(piHandle)) {
6422
6542
  throw new Error("Accepted empty-tree plan no longer has an empty Pi tree");
6423
6543
  }
6424
6544
  } else if (!piHandle.getBranch(piHandle.getLeafId() ?? void 0).some((entry) => entry.id === preparedStart.forkPiEntryId)) {
@@ -6545,6 +6665,7 @@ async function executeLlmTurn(input) {
6545
6665
  turnRecordId,
6546
6666
  turnDef: input.turnDef,
6547
6667
  requestQuestions: input.callbacks.requestQuestions,
6668
+ integrationTools: input.callbacks.integrationTools,
6548
6669
  resultImageTool: input.resultImageTools.create({
6549
6670
  workspaceRoot: input.ctx.workspaceRoot,
6550
6671
  instanceId: input.ctx.process.id,
@@ -6922,6 +7043,7 @@ async function executeSelectedTurn(input) {
6922
7043
  operatorAbortSignal: input.signal,
6923
7044
  callbacks: {
6924
7045
  requestQuestions: input.requestQuestions,
7046
+ integrationTools: input.integrationTools,
6925
7047
  onPreTurnTargetedInputApplied(inputId, sequence, meta) {
6926
7048
  appliedTargetedInputs.push({ inputId, sequence, ...meta ? { meta } : {} });
6927
7049
  },
@@ -6999,6 +7121,7 @@ function createWorkerRuntime(options) {
6999
7121
  emitExtensionEvent: (event, payload) => adapters.extensionEvents?.emit(event, payload)
7000
7122
  });
7001
7123
  const questionBridge = new WorkerQuestionBridge(reporter);
7124
+ const integrationToolBridge = new WorkerIntegrationToolBridge(reporter);
7002
7125
  let dispatch;
7003
7126
  const piEvents = createPiEventReporter({
7004
7127
  reporter,
@@ -7121,6 +7244,10 @@ function createWorkerRuntime(options) {
7121
7244
  (signal) => executeSelectedTurn({
7122
7245
  session: command.session,
7123
7246
  requestQuestions: (request) => questionBridge.request(request),
7247
+ integrationTools: integrationToolBridge.createTools(
7248
+ command.session.integrationTools,
7249
+ command.turnRecordId
7250
+ ),
7124
7251
  piHandle: resources.piHandle,
7125
7252
  turnRecordId: command.turnRecordId,
7126
7253
  targetedInputs: command.targetedInputs,
@@ -7241,6 +7368,10 @@ function createWorkerRuntime(options) {
7241
7368
  drain();
7242
7369
  };
7243
7370
  const receive = (message) => {
7371
+ if (message.type === "worker.integration_tool_result") {
7372
+ integrationToolBridge.handle(message);
7373
+ return;
7374
+ }
7244
7375
  if (message.type === "worker.question_response") {
7245
7376
  questionBridge.handle(message);
7246
7377
  return;
@@ -7261,6 +7392,7 @@ function createWorkerRuntime(options) {
7261
7392
  ipc.onConnect?.(() => {
7262
7393
  dispatch({ kind: "transport_connected" });
7263
7394
  questionBridge.replay();
7395
+ integrationToolBridge.replay();
7264
7396
  });
7265
7397
  ipc.start();
7266
7398
  dispatch({ kind: "runtime_started" });
@@ -7268,6 +7400,7 @@ function createWorkerRuntime(options) {
7268
7400
  async stop(reason) {
7269
7401
  if (state.phase.kind === "exited") return;
7270
7402
  questionBridge.cancelAll(`Question request cancelled: ${reason}`);
7403
+ integrationToolBridge.cancelAll(`Integration tool request cancelled: ${reason}`);
7271
7404
  dispatch({ kind: "stop_requested", reason, exitAfterCleanup: false });
7272
7405
  await closed;
7273
7406
  }
@@ -8122,4 +8255,4 @@ export {
8122
8255
  createWorkerRuntime,
8123
8256
  createWorkerEntryRuntime
8124
8257
  };
8125
- //# sourceMappingURL=chunk-6OAKGOYC.js.map
8258
+ //# sourceMappingURL=chunk-ZTJ7UAOC.js.map