@leitwerk-dev/worker 0.1.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.
- package/dist/LICENSE +13 -0
- package/dist/automatic-turn-executor.d.ts +28 -0
- package/dist/automatic-turn-executor.d.ts.map +1 -0
- package/dist/automatic-turn-executor.js +58 -0
- package/dist/automatic-turn-executor.js.map +1 -0
- package/dist/chunk-N5HLJDWA.js +8124 -0
- package/dist/chunk-N5HLJDWA.js.map +1 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +51 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/entry-runtime.d.ts +24 -0
- package/dist/entry-runtime.d.ts.map +1 -0
- package/dist/entry-runtime.js +160 -0
- package/dist/entry-runtime.js.map +1 -0
- package/dist/event-translator.d.ts +8 -0
- package/dist/event-translator.d.ts.map +1 -0
- package/dist/event-translator.js +12 -0
- package/dist/event-translator.js.map +1 -0
- package/dist/extension-entry-roots.d.ts +7 -0
- package/dist/extension-entry-roots.d.ts.map +1 -0
- package/dist/extension-entry-roots.js +41 -0
- package/dist/extension-entry-roots.js.map +1 -0
- package/dist/identified-pi-entry.d.ts +56 -0
- package/dist/identified-pi-entry.d.ts.map +1 -0
- package/dist/identified-pi-entry.js +125 -0
- package/dist/identified-pi-entry.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/input-consumer.d.ts +24 -0
- package/dist/input-consumer.d.ts.map +1 -0
- package/dist/input-consumer.js +34 -0
- package/dist/input-consumer.js.map +1 -0
- package/dist/ipc.d.ts +22 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +192 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logical-prompt-runner.d.ts +49 -0
- package/dist/logical-prompt-runner.d.ts.map +1 -0
- package/dist/logical-prompt-runner.js +225 -0
- package/dist/logical-prompt-runner.js.map +1 -0
- package/dist/managed-pi-agent-dir.d.ts +13 -0
- package/dist/managed-pi-agent-dir.d.ts.map +1 -0
- package/dist/managed-pi-agent-dir.js +66 -0
- package/dist/managed-pi-agent-dir.js.map +1 -0
- package/dist/managed-pi-bootstrap.d.ts +24 -0
- package/dist/managed-pi-bootstrap.d.ts.map +1 -0
- package/dist/managed-pi-bootstrap.js +135 -0
- package/dist/managed-pi-bootstrap.js.map +1 -0
- package/dist/missing-tool-recovery-runner.d.ts +23 -0
- package/dist/missing-tool-recovery-runner.d.ts.map +1 -0
- package/dist/missing-tool-recovery-runner.js +73 -0
- package/dist/missing-tool-recovery-runner.js.map +1 -0
- package/dist/pi-adapter.d.ts +134 -0
- package/dist/pi-adapter.d.ts.map +1 -0
- package/dist/pi-adapter.js +1648 -0
- package/dist/pi-adapter.js.map +1 -0
- package/dist/pi-branch-guard.d.ts +39 -0
- package/dist/pi-branch-guard.d.ts.map +1 -0
- package/dist/pi-branch-guard.js +75 -0
- package/dist/pi-branch-guard.js.map +1 -0
- package/dist/pi-event-reporter.d.ts +15 -0
- package/dist/pi-event-reporter.d.ts.map +1 -0
- package/dist/pi-event-reporter.js +136 -0
- package/dist/pi-event-reporter.js.map +1 -0
- package/dist/pi-resource-bundle.d.ts +12 -0
- package/dist/pi-resource-bundle.d.ts.map +1 -0
- package/dist/pi-resource-bundle.js +58 -0
- package/dist/pi-resource-bundle.js.map +1 -0
- package/dist/pre-turn-targeted-inputs.d.ts +44 -0
- package/dist/pre-turn-targeted-inputs.d.ts.map +1 -0
- package/dist/pre-turn-targeted-inputs.js +101 -0
- package/dist/pre-turn-targeted-inputs.js.map +1 -0
- package/dist/prompt-guards.d.ts +52 -0
- package/dist/prompt-guards.d.ts.map +1 -0
- package/dist/prompt-guards.js +318 -0
- package/dist/prompt-guards.js.map +1 -0
- package/dist/question-bridge.d.ts +16 -0
- package/dist/question-bridge.d.ts.map +1 -0
- package/dist/question-bridge.js +55 -0
- package/dist/question-bridge.js.map +1 -0
- package/dist/question-tool.d.ts +14 -0
- package/dist/question-tool.d.ts.map +1 -0
- package/dist/question-tool.js +54 -0
- package/dist/question-tool.js.map +1 -0
- package/dist/resource-loader-factory.d.ts +11 -0
- package/dist/resource-loader-factory.d.ts.map +1 -0
- package/dist/resource-loader-factory.js +87 -0
- package/dist/resource-loader-factory.js.map +1 -0
- package/dist/result-image-upload.d.ts +14 -0
- package/dist/result-image-upload.d.ts.map +1 -0
- package/dist/result-image-upload.js +183 -0
- package/dist/result-image-upload.js.map +1 -0
- package/dist/runtime/adapters.d.ts +48 -0
- package/dist/runtime/adapters.d.ts.map +1 -0
- package/dist/runtime/adapters.js +15 -0
- package/dist/runtime/adapters.js.map +1 -0
- package/dist/runtime/bootstrap-session.d.ts +95 -0
- package/dist/runtime/bootstrap-session.d.ts.map +1 -0
- package/dist/runtime/bootstrap-session.js +198 -0
- package/dist/runtime/bootstrap-session.js.map +1 -0
- package/dist/runtime/bootstrap.d.ts +81 -0
- package/dist/runtime/bootstrap.d.ts.map +1 -0
- package/dist/runtime/bootstrap.js +440 -0
- package/dist/runtime/bootstrap.js.map +1 -0
- package/dist/runtime/failure-policy.d.ts +79 -0
- package/dist/runtime/failure-policy.d.ts.map +1 -0
- package/dist/runtime/failure-policy.js +182 -0
- package/dist/runtime/failure-policy.js.map +1 -0
- package/dist/runtime/index.d.ts +9 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +326 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/lifecycle-reducer.d.ts +328 -0
- package/dist/runtime/lifecycle-reducer.d.ts.map +1 -0
- package/dist/runtime/lifecycle-reducer.js +1100 -0
- package/dist/runtime/lifecycle-reducer.js.map +1 -0
- package/dist/runtime/llm-turn-execution.d.ts +42 -0
- package/dist/runtime/llm-turn-execution.d.ts.map +1 -0
- package/dist/runtime/llm-turn-execution.js +472 -0
- package/dist/runtime/llm-turn-execution.js.map +1 -0
- package/dist/runtime/settings.d.ts +11 -0
- package/dist/runtime/settings.d.ts.map +1 -0
- package/dist/runtime/settings.js +23 -0
- package/dist/runtime/settings.js.map +1 -0
- package/dist/runtime/snapshot-policy.d.ts +11 -0
- package/dist/runtime/snapshot-policy.d.ts.map +1 -0
- package/dist/runtime/snapshot-policy.js +16 -0
- package/dist/runtime/snapshot-policy.js.map +1 -0
- package/dist/runtime/turn-execution.d.ts +49 -0
- package/dist/runtime/turn-execution.d.ts.map +1 -0
- package/dist/runtime/turn-execution.js +120 -0
- package/dist/runtime/turn-execution.js.map +1 -0
- package/dist/session-snapshot-exchange.d.ts +39 -0
- package/dist/session-snapshot-exchange.d.ts.map +1 -0
- package/dist/session-snapshot-exchange.js +102 -0
- package/dist/session-snapshot-exchange.js.map +1 -0
- package/dist/test-helpers/fake-websocket.d.ts +16 -0
- package/dist/test-helpers/fake-websocket.d.ts.map +1 -0
- package/dist/test-helpers/fake-websocket.js +40 -0
- package/dist/test-helpers/fake-websocket.js.map +1 -0
- package/dist/test-helpers/ipc-harness.d.ts +11 -0
- package/dist/test-helpers/ipc-harness.d.ts.map +1 -0
- package/dist/test-helpers/ipc-harness.js +17 -0
- package/dist/test-helpers/ipc-harness.js.map +1 -0
- package/dist/test-helpers/worker-message-builders.d.ts +21 -0
- package/dist/test-helpers/worker-message-builders.d.ts.map +1 -0
- package/dist/test-helpers/worker-message-builders.js +62 -0
- package/dist/test-helpers/worker-message-builders.js.map +1 -0
- package/dist/turn-continuation.d.ts +28 -0
- package/dist/turn-continuation.d.ts.map +1 -0
- package/dist/turn-continuation.js +80 -0
- package/dist/turn-continuation.js.map +1 -0
- package/dist/turn-execution-error.d.ts +39 -0
- package/dist/turn-execution-error.d.ts.map +1 -0
- package/dist/turn-execution-error.js +64 -0
- package/dist/turn-execution-error.js.map +1 -0
- package/dist/turn-execution-result.d.ts +35 -0
- package/dist/turn-execution-result.d.ts.map +1 -0
- package/dist/turn-execution-result.js +21 -0
- package/dist/turn-execution-result.js.map +1 -0
- package/dist/turn-outcome-tool-session.d.ts +50 -0
- package/dist/turn-outcome-tool-session.d.ts.map +1 -0
- package/dist/turn-outcome-tool-session.js +209 -0
- package/dist/turn-outcome-tool-session.js.map +1 -0
- package/dist/turn-record-id.d.ts +3 -0
- package/dist/turn-record-id.d.ts.map +1 -0
- package/dist/turn-record-id.js +10 -0
- package/dist/turn-record-id.js.map +1 -0
- package/dist/turn-result-markdown.d.ts +74 -0
- package/dist/turn-result-markdown.d.ts.map +1 -0
- package/dist/turn-result-markdown.js +263 -0
- package/dist/turn-result-markdown.js.map +1 -0
- package/dist/turn-tree-strategy.d.ts +29 -0
- package/dist/turn-tree-strategy.d.ts.map +1 -0
- package/dist/turn-tree-strategy.js +80 -0
- package/dist/turn-tree-strategy.js.map +1 -0
- package/dist/worker-entry.d.ts +2 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-entry.js +7 -0
- package/dist/worker-entry.js.map +1 -0
- package/dist/worker-http.d.ts +2 -0
- package/dist/worker-http.d.ts.map +1 -0
- package/dist/worker-http.js +9 -0
- package/dist/worker-http.js.map +1 -0
- package/dist/worker-ipc-reporter.d.ts +29 -0
- package/dist/worker-ipc-reporter.d.ts.map +1 -0
- package/dist/worker-ipc-reporter.js +58 -0
- package/dist/worker-ipc-reporter.js.map +1 -0
- package/dist/worker-payloads.d.ts +10 -0
- package/dist/worker-payloads.d.ts.map +1 -0
- package/dist/worker-payloads.js +66 -0
- package/dist/worker-payloads.js.map +1 -0
- package/dist/worker-start-payload.d.ts +7 -0
- package/dist/worker-start-payload.d.ts.map +1 -0
- package/dist/worker-start-payload.js +5 -0
- package/dist/worker-start-payload.js.map +1 -0
- package/dist/workspace/manifest.d.ts +35 -0
- package/dist/workspace/manifest.d.ts.map +1 -0
- package/dist/workspace/manifest.js +79 -0
- package/dist/workspace/manifest.js.map +1 -0
- package/dist/workspace/node-run-root-git-ops.d.ts +17 -0
- package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -0
- package/dist/workspace/node-run-root-git-ops.js +96 -0
- package/dist/workspace/node-run-root-git-ops.js.map +1 -0
- package/dist/workspace/resource-aggregator.d.ts +15 -0
- package/dist/workspace/resource-aggregator.d.ts.map +1 -0
- package/dist/workspace/resource-aggregator.js +39 -0
- package/dist/workspace/resource-aggregator.js.map +1 -0
- package/dist/workspace/run-root.d.ts +52 -0
- package/dist/workspace/run-root.d.ts.map +1 -0
- package/dist/workspace/run-root.js +209 -0
- package/dist/workspace/run-root.js.map +1 -0
- package/package.json +55 -0
- package/src/automatic-turn-executor.ts +106 -0
- package/src/diagnostics.ts +82 -0
- package/src/entry-runtime.ts +235 -0
- package/src/event-translator.ts +22 -0
- package/src/extension-entry-roots.ts +43 -0
- package/src/identified-pi-entry.ts +188 -0
- package/src/index.ts +54 -0
- package/src/input-consumer.ts +71 -0
- package/src/ipc.ts +250 -0
- package/src/logical-prompt-runner.ts +320 -0
- package/src/managed-pi-agent-dir.ts +86 -0
- package/src/managed-pi-bootstrap.ts +208 -0
- package/src/missing-tool-recovery-runner.ts +112 -0
- package/src/pi-adapter.ts +2197 -0
- package/src/pi-branch-guard.ts +104 -0
- package/src/pi-event-reporter.ts +164 -0
- package/src/pi-resource-bundle.ts +90 -0
- package/src/pre-turn-targeted-inputs.ts +167 -0
- package/src/prompt-guards.ts +385 -0
- package/src/question-bridge.ts +69 -0
- package/src/question-tool.ts +65 -0
- package/src/resource-loader-factory.ts +115 -0
- package/src/result-image-upload.ts +256 -0
- package/src/runtime/adapters.ts +67 -0
- package/src/runtime/bootstrap-session.ts +294 -0
- package/src/runtime/bootstrap.ts +621 -0
- package/src/runtime/failure-policy.ts +274 -0
- package/src/runtime/index.ts +362 -0
- package/src/runtime/lifecycle-reducer.ts +1537 -0
- package/src/runtime/llm-turn-execution.ts +696 -0
- package/src/runtime/settings.ts +41 -0
- package/src/runtime/snapshot-policy.ts +28 -0
- package/src/runtime/turn-execution.ts +203 -0
- package/src/session-snapshot-exchange.ts +155 -0
- package/src/test-helpers/fake-websocket.ts +46 -0
- package/src/test-helpers/ipc-harness.ts +19 -0
- package/src/test-helpers/worker-message-builders.ts +92 -0
- package/src/turn-continuation.ts +147 -0
- package/src/turn-execution-error.ts +102 -0
- package/src/turn-execution-result.ts +48 -0
- package/src/turn-outcome-tool-session.ts +311 -0
- package/src/turn-record-id.ts +17 -0
- package/src/turn-result-markdown.ts +355 -0
- package/src/turn-tree-strategy.ts +141 -0
- package/src/worker-entry.ts +8 -0
- package/src/worker-http.ts +6 -0
- package/src/worker-ipc-reporter.ts +100 -0
- package/src/worker-payloads.ts +94 -0
- package/src/worker-start-payload.ts +10 -0
- package/src/workspace/manifest.ts +126 -0
- package/src/workspace/node-run-root-git-ops.ts +137 -0
- package/src/workspace/resource-aggregator.ts +43 -0
- package/src/workspace/run-root.ts +361 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { ProcessInstance } from "@leitwerk-dev/domain";
|
|
2
|
+
import { DEFAULT_CONTINUE_PROMPT, normalizeContinuePrompt } from "@leitwerk-dev/domain";
|
|
3
|
+
import type { PiTreeHandle } from "./pi-adapter.js";
|
|
4
|
+
import type { PreTurnTargetedInput } from "./pre-turn-targeted-inputs.js";
|
|
5
|
+
import { TurnExecutionError } from "./turn-execution-error.js";
|
|
6
|
+
import { readProcessSemanticEntryRefs } from "./worker-payloads.js";
|
|
7
|
+
|
|
8
|
+
export type PromptlessTurnResumeReason =
|
|
9
|
+
| "failed_turn_continue"
|
|
10
|
+
| "targeted_follow_up"
|
|
11
|
+
| "active_turn_resume";
|
|
12
|
+
|
|
13
|
+
function readCurrentLeafMessage(piHandle: Pick<PiTreeHandle, "getLeafId" | "getEntry">): {
|
|
14
|
+
leafId: string | null;
|
|
15
|
+
parentId: string | null;
|
|
16
|
+
role: string | null;
|
|
17
|
+
content: string | null;
|
|
18
|
+
} {
|
|
19
|
+
const leafId = piHandle.getLeafId();
|
|
20
|
+
if (!leafId) {
|
|
21
|
+
return { leafId: null, parentId: null, role: null, content: null };
|
|
22
|
+
}
|
|
23
|
+
const entry = piHandle.getEntry(leafId);
|
|
24
|
+
const role = entry?.message?.role;
|
|
25
|
+
const content = entry?.message?.content;
|
|
26
|
+
return {
|
|
27
|
+
leafId,
|
|
28
|
+
parentId: entry?.parentId ?? null,
|
|
29
|
+
role: typeof role === "string" && role.trim() !== "" ? role : null,
|
|
30
|
+
content: typeof content === "string" && content.trim() !== "" ? content.trim() : null,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function prepareContinuationUserPrompt(input: {
|
|
35
|
+
piHandle: PiTreeHandle;
|
|
36
|
+
turnId: string;
|
|
37
|
+
promptText: string;
|
|
38
|
+
}): Promise<string | null> {
|
|
39
|
+
const normalizedPrompt = input.promptText.trim();
|
|
40
|
+
if (normalizedPrompt === "") {
|
|
41
|
+
throw new TurnExecutionError(
|
|
42
|
+
input.turnId,
|
|
43
|
+
"protocol_error",
|
|
44
|
+
`Turn '${input.turnId}' cannot append an empty continuation prompt`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
let currentLeaf = readCurrentLeafMessage(input.piHandle);
|
|
48
|
+
if (!currentLeaf.leafId) {
|
|
49
|
+
throw new TurnExecutionError(
|
|
50
|
+
input.turnId,
|
|
51
|
+
"infrastructure",
|
|
52
|
+
`Turn '${input.turnId}' cannot append the continuation prompt because there is no active Pi leaf to continue from`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
while (currentLeaf.role === "user") {
|
|
56
|
+
if (currentLeaf.content === normalizedPrompt) return null;
|
|
57
|
+
if (!currentLeaf.parentId) {
|
|
58
|
+
throw new TurnExecutionError(
|
|
59
|
+
input.turnId,
|
|
60
|
+
"infrastructure",
|
|
61
|
+
`Turn '${input.turnId}' cannot replace the saved continuation prompt at the root of the Pi tree`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
await input.piHandle.branch(currentLeaf.parentId);
|
|
65
|
+
currentLeaf = readCurrentLeafMessage(input.piHandle);
|
|
66
|
+
if (!currentLeaf.leafId) {
|
|
67
|
+
throw new TurnExecutionError(
|
|
68
|
+
input.turnId,
|
|
69
|
+
"infrastructure",
|
|
70
|
+
`Turn '${input.turnId}' lost its continuation anchor while preparing a continuation prompt`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return normalizedPrompt;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readNonEmptyMetadataString(
|
|
78
|
+
metadata: ProcessInstance["metadata"],
|
|
79
|
+
key: string,
|
|
80
|
+
): string | null {
|
|
81
|
+
const value = metadata?.[key];
|
|
82
|
+
return typeof value === "string" && value.trim() !== "" ? value : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function resolveTurnContinuationState(input: {
|
|
86
|
+
process: Pick<ProcessInstance, "selectedTurnId" | "metadata">;
|
|
87
|
+
turnId: string;
|
|
88
|
+
state: unknown;
|
|
89
|
+
}): {
|
|
90
|
+
continueFromPiEntryId: string;
|
|
91
|
+
continuePrompt: string;
|
|
92
|
+
savedPrimaryLeafId: string | null;
|
|
93
|
+
} | null {
|
|
94
|
+
if (input.process.selectedTurnId !== input.turnId) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const continueFromPiEntryId = readNonEmptyMetadataString(
|
|
98
|
+
input.process.metadata,
|
|
99
|
+
"continueFromPiEntryId",
|
|
100
|
+
);
|
|
101
|
+
const continueFromTurnRecordId = readNonEmptyMetadataString(
|
|
102
|
+
input.process.metadata,
|
|
103
|
+
"continueFromTurnRecordId",
|
|
104
|
+
);
|
|
105
|
+
if (!continueFromPiEntryId || !continueFromTurnRecordId) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
continueFromPiEntryId,
|
|
110
|
+
continuePrompt:
|
|
111
|
+
normalizeContinuePrompt(input.process.metadata?.continuePrompt) ?? DEFAULT_CONTINUE_PROMPT,
|
|
112
|
+
savedPrimaryLeafId:
|
|
113
|
+
readNonEmptyMetadataString(input.process.metadata, "continueSavedPrimaryLeafEntryId") ??
|
|
114
|
+
readProcessSemanticEntryRefs(input.state)?.currentPrimaryPathLeaf?.entryId ??
|
|
115
|
+
null,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function resolvePromptlessTurnResumeReason(input: {
|
|
120
|
+
process: Pick<ProcessInstance, "selectedTurnId">;
|
|
121
|
+
acceptedTurnRecordId: string | null;
|
|
122
|
+
turnId: string;
|
|
123
|
+
hasFailedTurnContinuation: boolean;
|
|
124
|
+
preTurnTargetedInputs: readonly Pick<PreTurnTargetedInput, "source">[];
|
|
125
|
+
piHandleIsResumed: boolean;
|
|
126
|
+
hasCurrentLeaf: boolean;
|
|
127
|
+
}): PromptlessTurnResumeReason | null {
|
|
128
|
+
if (input.hasFailedTurnContinuation) {
|
|
129
|
+
return "failed_turn_continue";
|
|
130
|
+
}
|
|
131
|
+
if (
|
|
132
|
+
input.preTurnTargetedInputs.length > 0 &&
|
|
133
|
+
input.preTurnTargetedInputs.every((targetedInput) => targetedInput.source === "action_prompt")
|
|
134
|
+
) {
|
|
135
|
+
return "targeted_follow_up";
|
|
136
|
+
}
|
|
137
|
+
if (
|
|
138
|
+
input.piHandleIsResumed &&
|
|
139
|
+
input.hasCurrentLeaf &&
|
|
140
|
+
input.process.selectedTurnId === input.turnId &&
|
|
141
|
+
typeof input.acceptedTurnRecordId === "string" &&
|
|
142
|
+
input.acceptedTurnRecordId.trim() !== ""
|
|
143
|
+
) {
|
|
144
|
+
return "active_turn_resume";
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FailedTurnRecoveryContext,
|
|
3
|
+
TurnFailureCode,
|
|
4
|
+
WorkerErrorClass,
|
|
5
|
+
} from "@leitwerk-dev/domain";
|
|
6
|
+
import { isWorkerErrorClass } from "@leitwerk-dev/domain";
|
|
7
|
+
import { isPiBranchDriftError } from "./pi-branch-guard.js";
|
|
8
|
+
|
|
9
|
+
export type TurnFailureReportOptions = {
|
|
10
|
+
recoveryContext?: FailedTurnRecoveryContext | null;
|
|
11
|
+
restorePrimaryLeaf?: boolean;
|
|
12
|
+
failureCode?: TurnFailureCode | null;
|
|
13
|
+
failureDetails?: Record<string, unknown> | null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export class TurnExecutionError extends Error {
|
|
17
|
+
readonly errorClass: WorkerErrorClass;
|
|
18
|
+
readonly turnId: string;
|
|
19
|
+
readonly failureCode: TurnFailureCode | null;
|
|
20
|
+
readonly failureDetails: Record<string, unknown> | null;
|
|
21
|
+
readonly recoveryContext: FailedTurnRecoveryContext | null;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
turnId: string,
|
|
25
|
+
errorClass: WorkerErrorClass,
|
|
26
|
+
message: string,
|
|
27
|
+
options: Omit<TurnFailureReportOptions, "restorePrimaryLeaf"> = {},
|
|
28
|
+
) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = "TurnExecutionError";
|
|
31
|
+
this.turnId = turnId;
|
|
32
|
+
this.errorClass = errorClass;
|
|
33
|
+
this.failureCode = options.failureCode ?? null;
|
|
34
|
+
this.failureDetails = options.failureDetails ?? null;
|
|
35
|
+
this.recoveryContext = options.recoveryContext ?? null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function classifyRuntimeError(
|
|
40
|
+
error: unknown,
|
|
41
|
+
fallbackErrorClass: WorkerErrorClass = "infrastructure",
|
|
42
|
+
): {
|
|
43
|
+
errorClass: WorkerErrorClass;
|
|
44
|
+
message: string;
|
|
45
|
+
} {
|
|
46
|
+
if (error instanceof TurnExecutionError) {
|
|
47
|
+
return { errorClass: error.errorClass, message: error.message };
|
|
48
|
+
}
|
|
49
|
+
if (
|
|
50
|
+
typeof error === "object" &&
|
|
51
|
+
error !== null &&
|
|
52
|
+
"errorClass" in error &&
|
|
53
|
+
isWorkerErrorClass(String((error as { errorClass?: unknown }).errorClass))
|
|
54
|
+
) {
|
|
55
|
+
const customError = error as { errorClass: WorkerErrorClass; message?: unknown };
|
|
56
|
+
return {
|
|
57
|
+
errorClass: customError.errorClass,
|
|
58
|
+
message:
|
|
59
|
+
typeof customError.message === "string" && customError.message.trim() !== ""
|
|
60
|
+
? customError.message
|
|
61
|
+
: String(error),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (error instanceof Error && error.message.trim() !== "") {
|
|
65
|
+
return { errorClass: fallbackErrorClass, message: error.message };
|
|
66
|
+
}
|
|
67
|
+
return { errorClass: fallbackErrorClass, message: String(error) };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function buildTurnFailureReport(
|
|
71
|
+
error: unknown,
|
|
72
|
+
input: {
|
|
73
|
+
turnId: string;
|
|
74
|
+
currentLeafId: string | null;
|
|
75
|
+
fallbackErrorClass?: WorkerErrorClass;
|
|
76
|
+
messagePrefix?: string;
|
|
77
|
+
},
|
|
78
|
+
) {
|
|
79
|
+
const classified = classifyRuntimeError(error, input.fallbackErrorClass);
|
|
80
|
+
const branchDrift = isPiBranchDriftError(error) ? error : null;
|
|
81
|
+
return {
|
|
82
|
+
errorClass: classified.errorClass,
|
|
83
|
+
message: input.messagePrefix
|
|
84
|
+
? `${input.messagePrefix}: ${classified.message}`
|
|
85
|
+
: classified.message,
|
|
86
|
+
resultPiEntryId: branchDrift ? null : input.currentLeafId,
|
|
87
|
+
...(branchDrift
|
|
88
|
+
? {
|
|
89
|
+
details: branchDrift.failureDetails,
|
|
90
|
+
options: branchDrift.toTurnFailureOptions(),
|
|
91
|
+
taintReason: `branch_drift:${input.turnId}`,
|
|
92
|
+
}
|
|
93
|
+
: {}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function toErrorMessage(error: unknown): string {
|
|
98
|
+
if (error instanceof Error && error.message.trim() !== "") {
|
|
99
|
+
return error.message;
|
|
100
|
+
}
|
|
101
|
+
return String(error);
|
|
102
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ProcessTurnRecordPathType, ProcessTurnType } from "@leitwerk-dev/domain";
|
|
2
|
+
import type { TurnResult } from "@leitwerk-dev/process-sdk";
|
|
3
|
+
import type { TurnExecutionError } from "./turn-execution-error.js";
|
|
4
|
+
|
|
5
|
+
export interface TurnExecutionMeta {
|
|
6
|
+
turnRecordId: string;
|
|
7
|
+
turnType: ProcessTurnType;
|
|
8
|
+
pathType: ProcessTurnRecordPathType;
|
|
9
|
+
forkPiEntryId?: string | null;
|
|
10
|
+
resultPiEntryId?: string | null;
|
|
11
|
+
turnResultMarkdown?: string | null;
|
|
12
|
+
rootEntryId?: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface LlmTurnExecutionSuccess<TOutcome extends string> {
|
|
16
|
+
turnResult: TurnResult<TOutcome>;
|
|
17
|
+
meta: TurnExecutionMeta;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class TurnExecutionFailure extends Error {
|
|
21
|
+
readonly turnRecordId: string;
|
|
22
|
+
readonly turnId: string;
|
|
23
|
+
readonly turnType: ProcessTurnType;
|
|
24
|
+
readonly pathType: ProcessTurnRecordPathType;
|
|
25
|
+
readonly failure: TurnExecutionError;
|
|
26
|
+
readonly forkPiEntryId: string | null;
|
|
27
|
+
readonly resultPiEntryId: string | null;
|
|
28
|
+
|
|
29
|
+
constructor(input: {
|
|
30
|
+
turnRecordId: string;
|
|
31
|
+
turnId: string;
|
|
32
|
+
turnType: ProcessTurnType;
|
|
33
|
+
pathType: ProcessTurnRecordPathType;
|
|
34
|
+
failure: TurnExecutionError;
|
|
35
|
+
forkPiEntryId?: string | null;
|
|
36
|
+
resultPiEntryId?: string | null;
|
|
37
|
+
}) {
|
|
38
|
+
super(input.failure.message, { cause: input.failure });
|
|
39
|
+
this.name = "TurnExecutionFailure";
|
|
40
|
+
this.turnRecordId = input.turnRecordId;
|
|
41
|
+
this.turnId = input.turnId;
|
|
42
|
+
this.turnType = input.turnType;
|
|
43
|
+
this.pathType = input.pathType;
|
|
44
|
+
this.failure = input.failure;
|
|
45
|
+
this.forkPiEntryId = input.forkPiEntryId ?? null;
|
|
46
|
+
this.resultPiEntryId = input.resultPiEntryId ?? null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import type { FailedTurnRecoveryContext } from "@leitwerk-dev/domain";
|
|
2
|
+
import type {
|
|
3
|
+
LlmTurnDefinition,
|
|
4
|
+
OutcomeToolSpec,
|
|
5
|
+
PiCustomTool,
|
|
6
|
+
PiTerminalAcknowledgementControl,
|
|
7
|
+
} from "@leitwerk-dev/process-sdk";
|
|
8
|
+
import { resolveTurnActiveToolNames } from "@leitwerk-dev/process-sdk/pi-config";
|
|
9
|
+
import {
|
|
10
|
+
ASK_QUESTIONS_PROMPT_GUIDANCE,
|
|
11
|
+
createAskQuestionsTool,
|
|
12
|
+
type WorkerQuestionRequest,
|
|
13
|
+
} from "./question-tool.js";
|
|
14
|
+
import {
|
|
15
|
+
buildMissingTurnToolCallRecoveryPrompt,
|
|
16
|
+
buildTurnResultMarkdownPromptSuffix,
|
|
17
|
+
buildTurnToolCallPromptSuffix,
|
|
18
|
+
createMarkdownResultTool,
|
|
19
|
+
createTurnResultMarkdownState,
|
|
20
|
+
describeMissingTurnToolCallRecovery,
|
|
21
|
+
finalizeTurnResultMarkdown,
|
|
22
|
+
type MissingTurnToolCallRecovery,
|
|
23
|
+
maybeCaptureTurnResultMarkdownFromToolCall,
|
|
24
|
+
publishTurnResultMarkdownValue,
|
|
25
|
+
resolveMissingTurnToolCallRecovery,
|
|
26
|
+
type TurnResultMarkdownState,
|
|
27
|
+
} from "./turn-result-markdown.js";
|
|
28
|
+
|
|
29
|
+
export type TurnOutcomeSelection<TOutcome extends string> = {
|
|
30
|
+
outcome: TOutcome;
|
|
31
|
+
params: Record<string, unknown>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const TERMINAL_ACKNOWLEDGEMENT_TIMEOUT_MS = 30_000;
|
|
35
|
+
|
|
36
|
+
export type TerminalAcknowledgementState =
|
|
37
|
+
| { kind: "open" }
|
|
38
|
+
| { kind: "outcome_accepted" }
|
|
39
|
+
| { kind: "acknowledgement_succeeded" }
|
|
40
|
+
| { kind: "acknowledgement_failed_ignored"; reason: string };
|
|
41
|
+
|
|
42
|
+
export type ToolCompletionSnapshot<TOutcome extends string> = {
|
|
43
|
+
selectedOutcome: TurnOutcomeSelection<TOutcome> | null;
|
|
44
|
+
markdownState: TurnResultMarkdownState;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export interface TurnOutcomeToolSession<TOutcome extends string> {
|
|
48
|
+
readonly outcomeToolNames: string[];
|
|
49
|
+
readonly usesOutcomeTools: boolean;
|
|
50
|
+
readonly requiresMarkdownResultToolCall: boolean;
|
|
51
|
+
readonly tools: PiCustomTool[];
|
|
52
|
+
readonly activeTools: readonly string[];
|
|
53
|
+
addPromptSuffix(promptText: string): string;
|
|
54
|
+
reset(baseState?: ToolCompletionSnapshot<TOutcome>): void;
|
|
55
|
+
getCompletionState(): ToolCompletionSnapshot<TOutcome>;
|
|
56
|
+
readonly terminalAcknowledgement: Pick<
|
|
57
|
+
PiTerminalAcknowledgementControl,
|
|
58
|
+
"state" | "markSucceeded" | "markFailed"
|
|
59
|
+
> & { failureReason(): string | null };
|
|
60
|
+
shouldBlockToolCall(toolName: string): string | null;
|
|
61
|
+
resolveOutcome(
|
|
62
|
+
turnDef: LlmTurnDefinition<TOutcome, unknown, unknown>,
|
|
63
|
+
): TurnOutcomeSelection<TOutcome> | null;
|
|
64
|
+
resolveMissingToolRecovery(
|
|
65
|
+
completionState: ToolCompletionSnapshot<TOutcome>,
|
|
66
|
+
): MissingTurnToolCallRecovery | null;
|
|
67
|
+
buildRecoveryContext(recovery: MissingTurnToolCallRecovery): FailedTurnRecoveryContext;
|
|
68
|
+
describeRecovery(recovery: MissingTurnToolCallRecovery): string;
|
|
69
|
+
finalizeMarkdown(
|
|
70
|
+
completionState: ToolCompletionSnapshot<TOutcome>,
|
|
71
|
+
assistantMarkdown?: string | null,
|
|
72
|
+
): ReturnType<typeof finalizeTurnResultMarkdown>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function cloneSelectedOutcome<TOutcome extends string>(
|
|
76
|
+
value: TurnOutcomeSelection<TOutcome> | null,
|
|
77
|
+
): TurnOutcomeSelection<TOutcome> | null {
|
|
78
|
+
return value ? { outcome: value.outcome, params: { ...value.params } } : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function cloneTurnResultMarkdownState(state: TurnResultMarkdownState): TurnResultMarkdownState {
|
|
82
|
+
return {
|
|
83
|
+
markdown: state.markdown,
|
|
84
|
+
publicationCount: state.publicationCount,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function snapshotToolCompletionState<TOutcome extends string>(
|
|
89
|
+
value: ToolCompletionSnapshot<TOutcome>,
|
|
90
|
+
): ToolCompletionSnapshot<TOutcome> {
|
|
91
|
+
return {
|
|
92
|
+
selectedOutcome: cloneSelectedOutcome(value.selectedOutcome),
|
|
93
|
+
markdownState: cloneTurnResultMarkdownState(value.markdownState),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function createTurnOutcomeToolSession<TOutcome extends string>(input: {
|
|
98
|
+
turnId: string;
|
|
99
|
+
turnRecordId?: string;
|
|
100
|
+
turnDef: LlmTurnDefinition<TOutcome, unknown, unknown>;
|
|
101
|
+
resultImageTool?: PiCustomTool | null;
|
|
102
|
+
requestQuestions?: (request: WorkerQuestionRequest) => Promise<string[]>;
|
|
103
|
+
}): TurnOutcomeToolSession<TOutcome> {
|
|
104
|
+
let selectedOutcome: TurnOutcomeSelection<TOutcome> | null = null;
|
|
105
|
+
let terminalAcknowledgement: TerminalAcknowledgementState = { kind: "open" };
|
|
106
|
+
const turnResultMarkdownStateRef = { current: createTurnResultMarkdownState() };
|
|
107
|
+
const outcomeToolEntries = Object.entries(input.turnDef.outcomes ?? {}) as [
|
|
108
|
+
string,
|
|
109
|
+
OutcomeToolSpec,
|
|
110
|
+
][];
|
|
111
|
+
const outcomeToolNames = outcomeToolEntries.map(([outcome]) => outcome);
|
|
112
|
+
const usesOutcomeTools = outcomeToolEntries.length > 0;
|
|
113
|
+
const requiresMarkdownResultToolCall =
|
|
114
|
+
input.turnDef.turnResultMarkdown?.mode === "tool_call" &&
|
|
115
|
+
input.turnDef.turnResultMarkdown.toolName === "markdown_result" &&
|
|
116
|
+
input.turnDef.turnResultMarkdown.required === true;
|
|
117
|
+
const hasResultImageTool = Boolean(
|
|
118
|
+
input.turnDef.turnResultMarkdown &&
|
|
119
|
+
input.turnDef.turnResultMarkdown.mode !== "none" &&
|
|
120
|
+
input.resultImageTool,
|
|
121
|
+
);
|
|
122
|
+
const outcomeToolMarkdownParameterName =
|
|
123
|
+
input.turnDef.turnResultMarkdown?.mode === "outcome_tool_argument"
|
|
124
|
+
? input.turnDef.turnResultMarkdown.parameterName
|
|
125
|
+
: undefined;
|
|
126
|
+
const outcomeActions = outcomeToolEntries.map(([outcome, spec]) => {
|
|
127
|
+
const markdownParameterName =
|
|
128
|
+
spec.turnResultMarkdownParameter ?? outcomeToolMarkdownParameterName;
|
|
129
|
+
const injectsGlobalMarkdownParameter =
|
|
130
|
+
Boolean(outcomeToolMarkdownParameterName) && !spec.turnResultMarkdownParameter;
|
|
131
|
+
return {
|
|
132
|
+
name: outcome,
|
|
133
|
+
description: spec.description,
|
|
134
|
+
parameters: {
|
|
135
|
+
...Object.fromEntries(Object.entries(spec.parameters).map(([key, value]) => [key, value])),
|
|
136
|
+
...(injectsGlobalMarkdownParameter && outcomeToolMarkdownParameterName
|
|
137
|
+
? {
|
|
138
|
+
[outcomeToolMarkdownParameterName]: {
|
|
139
|
+
type: "string",
|
|
140
|
+
description:
|
|
141
|
+
"Operator-facing markdown result for this turn. Pass raw markdown only and do not wrap the entire markdown in triple backticks.",
|
|
142
|
+
required: true,
|
|
143
|
+
requiredErrorCode: `${outcomeToolMarkdownParameterName}_required`,
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
: {}),
|
|
147
|
+
},
|
|
148
|
+
executionMode: "sequential" as const,
|
|
149
|
+
execute: async (args: Record<string, unknown>) => {
|
|
150
|
+
const result = { status: "ok", outcome };
|
|
151
|
+
const acceptsOutcome = selectedOutcome === null;
|
|
152
|
+
if (acceptsOutcome) {
|
|
153
|
+
let outcomeParams = args;
|
|
154
|
+
if (markdownParameterName) {
|
|
155
|
+
const published = publishTurnResultMarkdownValue(
|
|
156
|
+
turnResultMarkdownStateRef.current,
|
|
157
|
+
args[markdownParameterName],
|
|
158
|
+
);
|
|
159
|
+
if (!published.response.ok) {
|
|
160
|
+
return published.response;
|
|
161
|
+
}
|
|
162
|
+
turnResultMarkdownStateRef.current = published.state;
|
|
163
|
+
if (injectsGlobalMarkdownParameter) {
|
|
164
|
+
outcomeParams = { ...args };
|
|
165
|
+
delete outcomeParams[markdownParameterName];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
selectedOutcome = { outcome: outcome as TOutcome, params: outcomeParams };
|
|
169
|
+
}
|
|
170
|
+
maybeCaptureTurnResultMarkdownFromToolCall({
|
|
171
|
+
behavior: input.turnDef.turnResultMarkdown,
|
|
172
|
+
stateRef: turnResultMarkdownStateRef,
|
|
173
|
+
toolName: outcome,
|
|
174
|
+
args,
|
|
175
|
+
result,
|
|
176
|
+
});
|
|
177
|
+
if (acceptsOutcome) {
|
|
178
|
+
terminalAcknowledgement = { kind: "outcome_accepted" };
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
});
|
|
184
|
+
const questionTool =
|
|
185
|
+
input.turnDef.askQuestions && input.turnRecordId && input.requestQuestions
|
|
186
|
+
? createAskQuestionsTool({
|
|
187
|
+
turnRecordId: input.turnRecordId,
|
|
188
|
+
request: input.requestQuestions,
|
|
189
|
+
})
|
|
190
|
+
: null;
|
|
191
|
+
const tools = [
|
|
192
|
+
...(hasResultImageTool && input.resultImageTool ? [input.resultImageTool] : []),
|
|
193
|
+
...(input.turnDef.turnResultMarkdown?.mode === "tool_call" &&
|
|
194
|
+
input.turnDef.turnResultMarkdown.toolName === "markdown_result"
|
|
195
|
+
? [createMarkdownResultTool(turnResultMarkdownStateRef)]
|
|
196
|
+
: []),
|
|
197
|
+
...outcomeActions,
|
|
198
|
+
...(questionTool ? [questionTool] : []),
|
|
199
|
+
];
|
|
200
|
+
const activeTools = resolveTurnActiveToolNames({
|
|
201
|
+
turnId: input.turnId,
|
|
202
|
+
turnDef: input.turnDef as LlmTurnDefinition<string, unknown, unknown>,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const terminalAcknowledgementControl = {
|
|
206
|
+
state: () => terminalAcknowledgement.kind,
|
|
207
|
+
markSucceeded() {
|
|
208
|
+
if (terminalAcknowledgement.kind === "outcome_accepted") {
|
|
209
|
+
terminalAcknowledgement = { kind: "acknowledgement_succeeded" };
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
markFailed(reason: string) {
|
|
213
|
+
if (terminalAcknowledgement.kind === "outcome_accepted") {
|
|
214
|
+
terminalAcknowledgement = { kind: "acknowledgement_failed_ignored", reason };
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
failureReason: () =>
|
|
218
|
+
terminalAcknowledgement.kind === "acknowledgement_failed_ignored"
|
|
219
|
+
? terminalAcknowledgement.reason
|
|
220
|
+
: null,
|
|
221
|
+
} satisfies TurnOutcomeToolSession<TOutcome>["terminalAcknowledgement"];
|
|
222
|
+
|
|
223
|
+
return {
|
|
224
|
+
outcomeToolNames,
|
|
225
|
+
usesOutcomeTools,
|
|
226
|
+
requiresMarkdownResultToolCall,
|
|
227
|
+
tools,
|
|
228
|
+
activeTools,
|
|
229
|
+
terminalAcknowledgement: terminalAcknowledgementControl,
|
|
230
|
+
addPromptSuffix(promptText) {
|
|
231
|
+
let next = promptText;
|
|
232
|
+
if (questionTool) next += ASK_QUESTIONS_PROMPT_GUIDANCE;
|
|
233
|
+
next += buildTurnToolCallPromptSuffix({
|
|
234
|
+
outcomeToolNames,
|
|
235
|
+
requiresMarkdownResultToolCall,
|
|
236
|
+
outcomeToolMarkdownParameterName,
|
|
237
|
+
});
|
|
238
|
+
next += buildTurnResultMarkdownPromptSuffix(input.turnDef.turnResultMarkdown, {
|
|
239
|
+
usesOutcomeTools,
|
|
240
|
+
hasResultImageTool,
|
|
241
|
+
});
|
|
242
|
+
return next;
|
|
243
|
+
},
|
|
244
|
+
reset(baseState) {
|
|
245
|
+
selectedOutcome = cloneSelectedOutcome(baseState?.selectedOutcome ?? null);
|
|
246
|
+
terminalAcknowledgement =
|
|
247
|
+
selectedOutcome === null ? { kind: "open" } : { kind: "acknowledgement_succeeded" };
|
|
248
|
+
turnResultMarkdownStateRef.current = cloneTurnResultMarkdownState(
|
|
249
|
+
baseState?.markdownState ?? createTurnResultMarkdownState(),
|
|
250
|
+
);
|
|
251
|
+
},
|
|
252
|
+
getCompletionState() {
|
|
253
|
+
return snapshotToolCompletionState({
|
|
254
|
+
selectedOutcome,
|
|
255
|
+
markdownState: turnResultMarkdownStateRef.current,
|
|
256
|
+
});
|
|
257
|
+
},
|
|
258
|
+
shouldBlockToolCall(toolName) {
|
|
259
|
+
if (selectedOutcome === null) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
if (
|
|
263
|
+
requiresMarkdownResultToolCall &&
|
|
264
|
+
turnResultMarkdownStateRef.current.publicationCount === 0 &&
|
|
265
|
+
toolName === "markdown_result"
|
|
266
|
+
) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
return `Tool '${toolName}' was skipped because an outcome has already been accepted for this turn.`;
|
|
270
|
+
},
|
|
271
|
+
resolveOutcome(turnDef) {
|
|
272
|
+
if (usesOutcomeTools) {
|
|
273
|
+
return selectedOutcome;
|
|
274
|
+
}
|
|
275
|
+
return turnDef.turnEnd
|
|
276
|
+
? {
|
|
277
|
+
outcome: turnDef.turnEnd.outcome,
|
|
278
|
+
params: turnDef.turnEnd.params ?? {},
|
|
279
|
+
}
|
|
280
|
+
: null;
|
|
281
|
+
},
|
|
282
|
+
resolveMissingToolRecovery(completionState) {
|
|
283
|
+
return resolveMissingTurnToolCallRecovery({
|
|
284
|
+
selectedOutcome: completionState.selectedOutcome,
|
|
285
|
+
outcomeToolNames,
|
|
286
|
+
turnResultMarkdownBehavior: input.turnDef.turnResultMarkdown,
|
|
287
|
+
turnResultMarkdownState: completionState.markdownState,
|
|
288
|
+
});
|
|
289
|
+
},
|
|
290
|
+
buildRecoveryContext(recovery) {
|
|
291
|
+
return {
|
|
292
|
+
strategy: "continue",
|
|
293
|
+
suggestedContinuePrompt: buildMissingTurnToolCallRecoveryPrompt(recovery),
|
|
294
|
+
failureCode: recovery.failureCode,
|
|
295
|
+
...(recovery.missingToolNames.length > 0
|
|
296
|
+
? { missingToolNames: recovery.missingToolNames }
|
|
297
|
+
: {}),
|
|
298
|
+
};
|
|
299
|
+
},
|
|
300
|
+
describeRecovery(recovery) {
|
|
301
|
+
return describeMissingTurnToolCallRecovery(recovery);
|
|
302
|
+
},
|
|
303
|
+
finalizeMarkdown(completionState, assistantMarkdown) {
|
|
304
|
+
return finalizeTurnResultMarkdown({
|
|
305
|
+
behavior: input.turnDef.turnResultMarkdown,
|
|
306
|
+
state: completionState.markdownState,
|
|
307
|
+
assistantMarkdown,
|
|
308
|
+
});
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { ProcessInstance } from "@leitwerk-dev/domain";
|
|
3
|
+
|
|
4
|
+
export function resolveTurnRecordIdForExecution(
|
|
5
|
+
process: Pick<ProcessInstance, "selectedTurnId">,
|
|
6
|
+
turnId: string,
|
|
7
|
+
acceptedTurnRecordId?: string | null,
|
|
8
|
+
): string {
|
|
9
|
+
if (
|
|
10
|
+
process.selectedTurnId === turnId &&
|
|
11
|
+
typeof acceptedTurnRecordId === "string" &&
|
|
12
|
+
acceptedTurnRecordId.trim() !== ""
|
|
13
|
+
) {
|
|
14
|
+
return acceptedTurnRecordId;
|
|
15
|
+
}
|
|
16
|
+
return `trn_${randomUUID().replace(/-/g, "")}`;
|
|
17
|
+
}
|