@botbotgo/agent-harness 0.0.475 → 0.0.476
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/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1115
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,40 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
const CAPTURED_TOOL_CALLS_KEY = "__harnessPromptedJsonToolCalls";
|
|
3
|
-
const storage = new AsyncLocalStorage();
|
|
4
|
-
export function recordPromptedJsonToolCall(toolCall) {
|
|
5
|
-
const active = storage.getStore();
|
|
6
|
-
if (!active) {
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
active.push(toolCall);
|
|
10
|
-
}
|
|
11
|
-
export async function capturePromptedJsonToolCalls(producer) {
|
|
12
|
-
const captured = [];
|
|
13
|
-
const result = await storage.run(captured, producer);
|
|
14
|
-
if (typeof result === "object"
|
|
15
|
-
&& result !== null
|
|
16
|
-
&& captured.length > 0
|
|
17
|
-
&& !Object.prototype.hasOwnProperty.call(result, CAPTURED_TOOL_CALLS_KEY)) {
|
|
18
|
-
Object.defineProperty(result, CAPTURED_TOOL_CALLS_KEY, {
|
|
19
|
-
configurable: true,
|
|
20
|
-
enumerable: false,
|
|
21
|
-
value: [...captured],
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
26
|
-
export function readCapturedPromptedJsonToolCalls(value) {
|
|
27
|
-
if (typeof value !== "object" || value === null) {
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
const captured = value[CAPTURED_TOOL_CALLS_KEY];
|
|
31
|
-
if (!Array.isArray(captured)) {
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
return captured.filter((item) => typeof item === "object"
|
|
35
|
-
&& item !== null
|
|
36
|
-
&& typeof item.name === "string"
|
|
37
|
-
&& typeof item.args === "object"
|
|
38
|
-
&& item.args !== null
|
|
39
|
-
&& !Array.isArray(item.args));
|
|
40
|
-
}
|
|
1
|
+
import{AsyncLocalStorage as a}from"node:async_hooks";const e="__harnessPromptedJsonToolCalls",n=new a;function s(t){const o=n.getStore();o&&o.push(t)}async function c(t){const o=[],r=await n.run(o,t);return typeof r=="object"&&r!==null&&o.length>0&&!Object.prototype.hasOwnProperty.call(r,e)&&Object.defineProperty(r,e,{configurable:!0,enumerable:!1,value:[...o]}),r}function p(t){if(typeof t!="object"||t===null)return[];const o=t[e];return Array.isArray(o)?o.filter(r=>typeof r=="object"&&r!==null&&typeof r.name=="string"&&typeof r.args=="object"&&r.args!==null&&!Array.isArray(r.args)):[]}export{c as capturePromptedJsonToolCalls,p as readCapturedPromptedJsonToolCalls,s as recordPromptedJsonToolCall};
|
|
@@ -1,22 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export function readPromptedJsonToolPolicy(input) {
|
|
3
|
-
if (typeof input !== "object" || input === null) {
|
|
4
|
-
return undefined;
|
|
5
|
-
}
|
|
6
|
-
const value = input[PROMPTED_JSON_TOOL_POLICY_KEY];
|
|
7
|
-
return value === "planning" || value === "nonPlanningEvidence" ? value : undefined;
|
|
8
|
-
}
|
|
9
|
-
export function withPromptedJsonToolPolicy(input, policy) {
|
|
10
|
-
if (typeof input !== "object" || input === null) {
|
|
11
|
-
return input;
|
|
12
|
-
}
|
|
13
|
-
if (Array.isArray(input)) {
|
|
14
|
-
return Object.assign([...input], {
|
|
15
|
-
[PROMPTED_JSON_TOOL_POLICY_KEY]: policy,
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
return {
|
|
19
|
-
...input,
|
|
20
|
-
[PROMPTED_JSON_TOOL_POLICY_KEY]: policy,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
const n="__agentHarnessPromptedJsonToolPolicy";function r(e){if(typeof e!="object"||e===null)return;const o=e[n];return o==="planning"||o==="nonPlanningEvidence"?o:void 0}function t(e,o){return typeof e!="object"||e===null?e:Array.isArray(e)?Object.assign([...e],{[n]:o}):{...e,[n]:o}}export{n as PROMPTED_JSON_TOOL_POLICY_KEY,r as readPromptedJsonToolPolicy,t as withPromptedJsonToolPolicy};
|
|
@@ -1,104 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export function resolveTimeoutMs(value) {
|
|
3
|
-
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
4
|
-
}
|
|
5
|
-
export function computeRemainingTimeoutMs(deadlineAt, fallbackTimeoutMs) {
|
|
6
|
-
if (!deadlineAt) {
|
|
7
|
-
return fallbackTimeoutMs;
|
|
8
|
-
}
|
|
9
|
-
const remaining = deadlineAt - Date.now();
|
|
10
|
-
if (remaining <= 0) {
|
|
11
|
-
return 0;
|
|
12
|
-
}
|
|
13
|
-
return fallbackTimeoutMs ? Math.min(fallbackTimeoutMs, remaining) : remaining;
|
|
14
|
-
}
|
|
15
|
-
export function resolveBindingTimeout(binding) {
|
|
16
|
-
return resolveTimeoutMs(getBindingModelInit(binding)?.timeout);
|
|
17
|
-
}
|
|
18
|
-
export function resolveStreamIdleTimeout(binding) {
|
|
19
|
-
const configuredIdleTimeout = resolveTimeoutMs(getBindingModelInit(binding)?.streamIdleTimeout);
|
|
20
|
-
if (configuredIdleTimeout) {
|
|
21
|
-
return configuredIdleTimeout;
|
|
22
|
-
}
|
|
23
|
-
const primaryModel = getBindingPrimaryModel(binding);
|
|
24
|
-
if (primaryModel?.provider === "ollama") {
|
|
25
|
-
return 180_000;
|
|
26
|
-
}
|
|
27
|
-
const invokeTimeout = resolveBindingTimeout(binding);
|
|
28
|
-
if (invokeTimeout) {
|
|
29
|
-
return Math.min(invokeTimeout, 60_000);
|
|
30
|
-
}
|
|
31
|
-
return 60_000;
|
|
32
|
-
}
|
|
33
|
-
const BUILTIN_RETRYABLE_PROVIDER_MESSAGES = [
|
|
34
|
-
"eof",
|
|
35
|
-
"unexpected eof",
|
|
36
|
-
"other side closed",
|
|
37
|
-
"socket hang up",
|
|
38
|
-
"connection reset",
|
|
39
|
-
"econnreset",
|
|
40
|
-
];
|
|
41
|
-
export function isEmptyFinalAiMessageError(error) {
|
|
42
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
43
|
-
return message.toLowerCase().startsWith("empty_final_ai_message:");
|
|
44
|
-
}
|
|
45
|
-
function isRuntimeOperationTimeoutError(error) {
|
|
46
|
-
return typeof error === "object" && error !== null && error.name === "RuntimeOperationTimeoutError";
|
|
47
|
-
}
|
|
48
|
-
function isRetryableHttpStatus(status) {
|
|
49
|
-
return typeof status === "number" && Number.isInteger(status) && status >= 500 && status <= 599;
|
|
50
|
-
}
|
|
51
|
-
function hasRetryableHttpStatus(error, seen = new Set()) {
|
|
52
|
-
if (typeof error !== "object" || error === null || seen.has(error)) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
seen.add(error);
|
|
56
|
-
const typed = error;
|
|
57
|
-
return isRetryableHttpStatus(typed.status)
|
|
58
|
-
|| isRetryableHttpStatus(typed.statusCode)
|
|
59
|
-
|| isRetryableHttpStatus(typed.status_code)
|
|
60
|
-
|| isRetryableHttpStatus(typed.code)
|
|
61
|
-
|| hasRetryableHttpStatus(typed.cause, seen);
|
|
62
|
-
}
|
|
63
|
-
export function resolveProviderRetryPolicy(binding) {
|
|
64
|
-
const resilience = typeof binding.harnessRuntime.resilience === "object" && binding.harnessRuntime.resilience
|
|
65
|
-
? binding.harnessRuntime.resilience
|
|
66
|
-
: {};
|
|
67
|
-
const providerRetries = typeof resilience.providerRetries === "object" && resilience.providerRetries
|
|
68
|
-
? resilience.providerRetries
|
|
69
|
-
: {};
|
|
70
|
-
const maxAttempts = typeof providerRetries.maxAttempts === "number" &&
|
|
71
|
-
Number.isFinite(providerRetries.maxAttempts) &&
|
|
72
|
-
providerRetries.maxAttempts > 0
|
|
73
|
-
? Math.floor(providerRetries.maxAttempts)
|
|
74
|
-
: 2;
|
|
75
|
-
const backoffMs = typeof providerRetries.backoffMs === "number" &&
|
|
76
|
-
Number.isFinite(providerRetries.backoffMs) &&
|
|
77
|
-
providerRetries.backoffMs >= 0
|
|
78
|
-
? Math.floor(providerRetries.backoffMs)
|
|
79
|
-
: 1_000;
|
|
80
|
-
const retryableMessages = Array.isArray(providerRetries.retryableMessages)
|
|
81
|
-
? providerRetries.retryableMessages.filter((value) => typeof value === "string" && value.trim().length > 0)
|
|
82
|
-
: [];
|
|
83
|
-
return {
|
|
84
|
-
maxAttempts,
|
|
85
|
-
backoffMs,
|
|
86
|
-
retryableMessages,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
export function isRetryableProviderError(binding, error) {
|
|
90
|
-
if (isRuntimeOperationTimeoutError(error)) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
if (isEmptyFinalAiMessageError(error)) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
if (hasRetryableHttpStatus(error)) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
100
|
-
const normalized = message.toLowerCase();
|
|
101
|
-
const { retryableMessages } = resolveProviderRetryPolicy(binding);
|
|
102
|
-
return [...BUILTIN_RETRYABLE_PROVIDER_MESSAGES, ...retryableMessages]
|
|
103
|
-
.some((candidate) => normalized.includes(candidate.toLowerCase()));
|
|
104
|
-
}
|
|
1
|
+
import{getBindingModelInit as u,getBindingPrimaryModel as f}from"../support/compiled-binding.js";function m(e){return typeof e=="number"&&Number.isFinite(e)&&e>0?e:void 0}function b(e,t){if(!e)return t;const r=e-Date.now();return r<=0?0:t?Math.min(t,r):r}function p(e){return m(u(e)?.timeout)}function R(e){const t=m(u(e)?.streamIdleTimeout);if(t)return t;if(f(e)?.provider==="ollama")return 18e4;const n=p(e);return n?Math.min(n,6e4):6e4}const l=["eof","unexpected eof","other side closed","socket hang up","connection reset","econnreset"];function y(e){return(e instanceof Error?e.message:String(e)).toLowerCase().startsWith("empty_final_ai_message:")}function d(e){return typeof e=="object"&&e!==null&&e.name==="RuntimeOperationTimeoutError"}function o(e){return typeof e=="number"&&Number.isInteger(e)&&e>=500&&e<=599}function c(e,t=new Set){if(typeof e!="object"||e===null||t.has(e))return!1;t.add(e);const r=e;return o(r.status)||o(r.statusCode)||o(r.status_code)||o(r.code)||c(r.cause,t)}function g(e){const t=typeof e.harnessRuntime.resilience=="object"&&e.harnessRuntime.resilience?e.harnessRuntime.resilience:{},r=typeof t.providerRetries=="object"&&t.providerRetries?t.providerRetries:{},n=typeof r.maxAttempts=="number"&&Number.isFinite(r.maxAttempts)&&r.maxAttempts>0?Math.floor(r.maxAttempts):2,s=typeof r.backoffMs=="number"&&Number.isFinite(r.backoffMs)&&r.backoffMs>=0?Math.floor(r.backoffMs):1e3,i=Array.isArray(r.retryableMessages)?r.retryableMessages.filter(a=>typeof a=="string"&&a.trim().length>0):[];return{maxAttempts:n,backoffMs:s,retryableMessages:i}}function h(e,t){if(d(t))return!1;if(y(t)||c(t))return!0;const n=(t instanceof Error?t.message:String(t)).toLowerCase(),{retryableMessages:s}=g(e);return[...l,...s].some(i=>n.includes(i.toLowerCase()))}export{b as computeRemainingTimeoutMs,y as isEmptyFinalAiMessageError,h as isRetryableProviderError,p as resolveBindingTimeout,g as resolveProviderRetryPolicy,R as resolveStreamIdleTimeout,m as resolveTimeoutMs};
|
|
@@ -1,141 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (joined.length <= maxChars) {
|
|
5
|
-
return joined;
|
|
6
|
-
}
|
|
7
|
-
return `${joined.slice(0, maxChars - 18)}\n...[truncated]`;
|
|
8
|
-
}
|
|
9
|
-
function findTodosArray(value, depth = 0) {
|
|
10
|
-
if (depth > 3 || !isRecord(value)) {
|
|
11
|
-
return [];
|
|
12
|
-
}
|
|
13
|
-
if (Array.isArray(value.todos)) {
|
|
14
|
-
return value.todos;
|
|
15
|
-
}
|
|
16
|
-
const nestedFromUpdate = findTodosArray(value.update, depth + 1);
|
|
17
|
-
if (nestedFromUpdate.length > 0) {
|
|
18
|
-
return nestedFromUpdate;
|
|
19
|
-
}
|
|
20
|
-
const nestedFromOutput = findTodosArray(value.output, depth + 1);
|
|
21
|
-
if (nestedFromOutput.length > 0) {
|
|
22
|
-
return nestedFromOutput;
|
|
23
|
-
}
|
|
24
|
-
const nestedFromData = findTodosArray(value.data, depth + 1);
|
|
25
|
-
if (nestedFromData.length > 0) {
|
|
26
|
-
return nestedFromData;
|
|
27
|
-
}
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
function stripWrappingQuotes(value) {
|
|
31
|
-
const trimmed = value.trim().replace(/,$/, "").trim();
|
|
32
|
-
if (trimmed.length >= 2) {
|
|
33
|
-
const first = trimmed[0];
|
|
34
|
-
const last = trimmed[trimmed.length - 1];
|
|
35
|
-
if ((first === "\"" && last === "\"") || (first === "'" && last === "'")) {
|
|
36
|
-
return trimmed.slice(1, -1).trim();
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return trimmed;
|
|
40
|
-
}
|
|
41
|
-
function normalizeTodoContentValue(value) {
|
|
42
|
-
const trimmed = value.trim();
|
|
43
|
-
if (!trimmed) {
|
|
44
|
-
return "";
|
|
45
|
-
}
|
|
46
|
-
if (/^\*{1,2}\s*(?:call\s+)?(?:write|read)[ _-]?todos\s*\*{1,2}$/iu.test(trimmed)) {
|
|
47
|
-
return "";
|
|
48
|
-
}
|
|
49
|
-
const contentField = /^["']?(content|description|title|name|text)["']?\s*:\s*(.+)$/iu.exec(trimmed);
|
|
50
|
-
if (contentField) {
|
|
51
|
-
const normalized = stripWrappingQuotes(contentField[2]);
|
|
52
|
-
return normalized && normalized !== "null" && normalized !== "undefined" ? normalized : "";
|
|
53
|
-
}
|
|
54
|
-
if (/^["']?(status|id|ownerAgentId|startedAt|endedAt|result|metadata)["']?\s*:/u.test(trimmed)) {
|
|
55
|
-
return "";
|
|
56
|
-
}
|
|
57
|
-
return stripWrappingQuotes(trimmed);
|
|
58
|
-
}
|
|
59
|
-
function readTodoContent(todo) {
|
|
60
|
-
const candidates = [todo.content, todo.description, todo.title, todo.name, todo.text];
|
|
61
|
-
for (const candidate of candidates) {
|
|
62
|
-
if (typeof candidate !== "string") {
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const normalized = normalizeTodoContentValue(candidate);
|
|
66
|
-
if (normalized) {
|
|
67
|
-
return normalized;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return "";
|
|
71
|
-
}
|
|
72
|
-
function normalizeTodoStatus(value) {
|
|
73
|
-
if (typeof value !== "string") {
|
|
74
|
-
return "pending";
|
|
75
|
-
}
|
|
76
|
-
const normalized = value.trim().toLowerCase();
|
|
77
|
-
if (normalized === "pending"
|
|
78
|
-
|| normalized === "in_progress"
|
|
79
|
-
|| normalized === "completed"
|
|
80
|
-
|| normalized === "failed"
|
|
81
|
-
|| normalized === "cancelled") {
|
|
82
|
-
return normalized;
|
|
83
|
-
}
|
|
84
|
-
return "pending";
|
|
85
|
-
}
|
|
86
|
-
export function summarizeBuiltinWriteTodosArgs(args) {
|
|
87
|
-
const todos = findTodosArray(args);
|
|
88
|
-
const items = todos.flatMap((todo) => {
|
|
89
|
-
if (!isRecord(todo)) {
|
|
90
|
-
return [];
|
|
91
|
-
}
|
|
92
|
-
const content = readTodoContent(todo);
|
|
93
|
-
const status = normalizeTodoStatus(todo.status);
|
|
94
|
-
const metadata = isRecord(todo.metadata) ? todo.metadata : undefined;
|
|
95
|
-
return content ? [{
|
|
96
|
-
...((typeof todo.id === "string" && todo.id.trim().length > 0)
|
|
97
|
-
|| typeof todo.id === "number"
|
|
98
|
-
? { id: String(todo.id).trim() }
|
|
99
|
-
: {}),
|
|
100
|
-
content,
|
|
101
|
-
status,
|
|
102
|
-
...(typeof todo.ownerAgentId === "string" && todo.ownerAgentId.trim().length > 0 ? { ownerAgentId: todo.ownerAgentId.trim() } : {}),
|
|
103
|
-
...(typeof todo.startedAt === "string" && todo.startedAt.trim().length > 0 ? { startedAt: todo.startedAt.trim() } : {}),
|
|
104
|
-
...(typeof todo.endedAt === "string" && todo.endedAt.trim().length > 0 ? { endedAt: todo.endedAt.trim() } : {}),
|
|
105
|
-
...(todo.result !== undefined ? { result: todo.result } : {}),
|
|
106
|
-
...(metadata ? { metadata } : {}),
|
|
107
|
-
}] : [];
|
|
108
|
-
});
|
|
109
|
-
const summary = {
|
|
110
|
-
total: items.length,
|
|
111
|
-
pending: items.filter((item) => item.status === "pending").length,
|
|
112
|
-
inProgress: items.filter((item) => item.status === "in_progress").length,
|
|
113
|
-
completed: items.filter((item) => item.status === "completed").length,
|
|
114
|
-
failed: items.filter((item) => item.status === "failed").length,
|
|
115
|
-
cancelled: items.filter((item) => item.status === "cancelled").length,
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
items,
|
|
119
|
-
summary,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
export function formatBuiltinTodoSnapshot(snapshot) {
|
|
123
|
-
if (snapshot.summary.total === 0) {
|
|
124
|
-
return "No todos tracked.";
|
|
125
|
-
}
|
|
126
|
-
const lines = [
|
|
127
|
-
`Tracked ${snapshot.summary.total} todo item(s): ${snapshot.summary.pending + snapshot.summary.inProgress} pending, ${snapshot.summary.completed} completed.`,
|
|
128
|
-
...snapshot.items.map((item, index) => `${index + 1}. [${item.status}] ${item.content}`),
|
|
129
|
-
];
|
|
130
|
-
return truncateLines(lines);
|
|
131
|
-
}
|
|
132
|
-
export function buildRequestPlanState(input) {
|
|
133
|
-
return {
|
|
134
|
-
sessionId: input.sessionId,
|
|
135
|
-
requestId: input.requestId,
|
|
136
|
-
version: input.version ?? 1,
|
|
137
|
-
updatedAt: input.updatedAt,
|
|
138
|
-
items: input.snapshot.items,
|
|
139
|
-
summary: input.snapshot.summary,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
1
|
+
import{isRecord as d}from"../../utils/object.js";function c(t,n=12e3){const r=t.join(`
|
|
2
|
+
`);return r.length<=n?r:`${r.slice(0,n-18)}
|
|
3
|
+
...[truncated]`}function s(t,n=0){if(n>3||!d(t))return[];if(Array.isArray(t.todos))return t.todos;const r=s(t.update,n+1);if(r.length>0)return r;const i=s(t.output,n+1);if(i.length>0)return i;const e=s(t.data,n+1);return e.length>0?e:[]}function o(t){const n=t.trim().replace(/,$/,"").trim();if(n.length>=2){const r=n[0],i=n[n.length-1];if(r==='"'&&i==='"'||r==="'"&&i==="'")return n.slice(1,-1).trim()}return n}function l(t){const n=t.trim();if(!n||/^\*{1,2}\s*(?:call\s+)?(?:write|read)[ _-]?todos\s*\*{1,2}$/iu.test(n))return"";const r=/^["']?(content|description|title|name|text)["']?\s*:\s*(.+)$/iu.exec(n);if(r){const i=o(r[2]);return i&&i!=="null"&&i!=="undefined"?i:""}return/^["']?(status|id|ownerAgentId|startedAt|endedAt|result|metadata)["']?\s*:/u.test(n)?"":o(n)}function f(t){const n=[t.content,t.description,t.title,t.name,t.text];for(const r of n){if(typeof r!="string")continue;const i=l(r);if(i)return i}return""}function g(t){if(typeof t!="string")return"pending";const n=t.trim().toLowerCase();return n==="pending"||n==="in_progress"||n==="completed"||n==="failed"||n==="cancelled"?n:"pending"}function A(t){const r=s(t).flatMap(e=>{if(!d(e))return[];const a=f(e),m=g(e.status),u=d(e.metadata)?e.metadata:void 0;return a?[{...typeof e.id=="string"&&e.id.trim().length>0||typeof e.id=="number"?{id:String(e.id).trim()}:{},content:a,status:m,...typeof e.ownerAgentId=="string"&&e.ownerAgentId.trim().length>0?{ownerAgentId:e.ownerAgentId.trim()}:{},...typeof e.startedAt=="string"&&e.startedAt.trim().length>0?{startedAt:e.startedAt.trim()}:{},...typeof e.endedAt=="string"&&e.endedAt.trim().length>0?{endedAt:e.endedAt.trim()}:{},...e.result!==void 0?{result:e.result}:{},...u?{metadata:u}:{}}]:[]}),i={total:r.length,pending:r.filter(e=>e.status==="pending").length,inProgress:r.filter(e=>e.status==="in_progress").length,completed:r.filter(e=>e.status==="completed").length,failed:r.filter(e=>e.status==="failed").length,cancelled:r.filter(e=>e.status==="cancelled").length};return{items:r,summary:i}}function y(t){if(t.summary.total===0)return"No todos tracked.";const n=[`Tracked ${t.summary.total} todo item(s): ${t.summary.pending+t.summary.inProgress} pending, ${t.summary.completed} completed.`,...t.items.map((r,i)=>`${i+1}. [${r.status}] ${r.content}`)];return c(n)}function h(t){return{sessionId:t.sessionId,requestId:t.requestId,version:t.version??1,updatedAt:t.updatedAt,items:t.snapshot.items,summary:t.snapshot.summary}}export{h as buildRequestPlanState,y as formatBuiltinTodoSnapshot,A as summarizeBuiltinWriteTodosArgs,c as truncateLines};
|
|
@@ -1,166 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { capturePromptedJsonToolCalls } from "./model/prompted-json-tool-call-capture.js";
|
|
7
|
-
export class RuntimeOperationTimeoutError extends Error {
|
|
8
|
-
operation;
|
|
9
|
-
timeoutMs;
|
|
10
|
-
stage;
|
|
11
|
-
constructor(operation, timeoutMs, stage = operation.includes("stream") ? "stream" : "invoke") {
|
|
12
|
-
super(`${operation} timed out after ${timeoutMs}ms`);
|
|
13
|
-
this.operation = operation;
|
|
14
|
-
this.timeoutMs = timeoutMs;
|
|
15
|
-
this.stage = stage;
|
|
16
|
-
this.name = "RuntimeOperationTimeoutError";
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export async function invokeWithProviderRetry(binding, operation, options = {}) {
|
|
20
|
-
const retryPolicy = resolveProviderRetryPolicy(binding);
|
|
21
|
-
let lastError;
|
|
22
|
-
for (let attempt = 1; attempt <= retryPolicy.maxAttempts; attempt += 1) {
|
|
23
|
-
try {
|
|
24
|
-
return await operation();
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
lastError = error;
|
|
28
|
-
if (attempt >= retryPolicy.maxAttempts || !isRetryableProviderError(binding, error)) {
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
await options.onRetry?.(attempt, error);
|
|
32
|
-
if (retryPolicy.backoffMs > 0) {
|
|
33
|
-
await sleep(retryPolicy.backoffMs);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
throw lastError instanceof Error ? lastError : new Error(String(lastError));
|
|
38
|
-
}
|
|
39
|
-
export async function withRuntimeTimeout(producer, timeoutMs, operation, stage = operation.includes("stream") ? "stream" : "invoke") {
|
|
40
|
-
if (!timeoutMs) {
|
|
41
|
-
return Promise.resolve(producer());
|
|
42
|
-
}
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
const timer = setTimeout(() => reject(new RuntimeOperationTimeoutError(operation, timeoutMs, stage)), timeoutMs);
|
|
45
|
-
Promise.resolve(producer()).then((value) => {
|
|
46
|
-
clearTimeout(timer);
|
|
47
|
-
resolve(value);
|
|
48
|
-
}, (error) => {
|
|
49
|
-
clearTimeout(timer);
|
|
50
|
-
reject(error);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
export async function* iterateWithTimeout(iterable, timeoutMs, operation, deadlineAt, deadlineTimeoutMs) {
|
|
55
|
-
const iterator = iterable[Symbol.asyncIterator]();
|
|
56
|
-
try {
|
|
57
|
-
for (;;) {
|
|
58
|
-
const effectiveTimeoutMs = computeRemainingTimeoutMs(deadlineAt, timeoutMs);
|
|
59
|
-
if (effectiveTimeoutMs !== undefined && effectiveTimeoutMs <= 0) {
|
|
60
|
-
throw new RuntimeOperationTimeoutError(operation, deadlineTimeoutMs ?? timeoutMs ?? 0, "invoke");
|
|
61
|
-
}
|
|
62
|
-
let next;
|
|
63
|
-
try {
|
|
64
|
-
next = await withRuntimeTimeout(() => iterator.next(), effectiveTimeoutMs, operation, "stream");
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
if (error instanceof RuntimeOperationTimeoutError &&
|
|
68
|
-
deadlineAt &&
|
|
69
|
-
deadlineTimeoutMs &&
|
|
70
|
-
effectiveTimeoutMs !== timeoutMs) {
|
|
71
|
-
throw new RuntimeOperationTimeoutError(operation, deadlineTimeoutMs, "invoke");
|
|
72
|
-
}
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
if (next.done) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
yield next.value;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
finally {
|
|
82
|
-
if (typeof iterator.return === "function") {
|
|
83
|
-
const returnResult = iterator.return();
|
|
84
|
-
if (returnResult && typeof returnResult.then === "function") {
|
|
85
|
-
void returnResult.catch(() => undefined);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
export async function materializeModelStream(streamFactory, input, config) {
|
|
91
|
-
const stream = await streamFactory(input, config);
|
|
92
|
-
let content = "";
|
|
93
|
-
for await (const chunk of stream) {
|
|
94
|
-
const delta = readStreamDelta(chunk) || extractVisibleOutput(chunk);
|
|
95
|
-
if (delta) {
|
|
96
|
-
content += delta;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return { content };
|
|
100
|
-
}
|
|
101
|
-
export function createModelFallbackRunnable(model) {
|
|
102
|
-
return {
|
|
103
|
-
invoke: async (input, config) => {
|
|
104
|
-
const request = typeof input === "object" && input !== null && "messages" in input
|
|
105
|
-
? input.messages
|
|
106
|
-
: input;
|
|
107
|
-
if (typeof model.invoke === "function") {
|
|
108
|
-
return model.invoke(request, config);
|
|
109
|
-
}
|
|
110
|
-
if (typeof model.stream === "function") {
|
|
111
|
-
return materializeModelStream(model.stream.bind(model), request, config);
|
|
112
|
-
}
|
|
113
|
-
throw new Error("Resolved model must define invoke or stream.");
|
|
114
|
-
},
|
|
115
|
-
stream: async (input, config) => {
|
|
116
|
-
if (typeof model.stream === "function") {
|
|
117
|
-
const request = typeof input === "object" && input !== null && "messages" in input
|
|
118
|
-
? input.messages
|
|
119
|
-
: input;
|
|
120
|
-
return model.stream(request, config);
|
|
121
|
-
}
|
|
122
|
-
if (typeof model.invoke === "function") {
|
|
123
|
-
const request = typeof input === "object" && input !== null && "messages" in input
|
|
124
|
-
? input.messages
|
|
125
|
-
: input;
|
|
126
|
-
const result = await model.invoke(request, config);
|
|
127
|
-
const text = extractVisibleOutput(result);
|
|
128
|
-
async function* singleChunk() {
|
|
129
|
-
yield { content: text };
|
|
130
|
-
}
|
|
131
|
-
return singleChunk();
|
|
132
|
-
}
|
|
133
|
-
throw new Error("Resolved model must define invoke or stream.");
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
export function applyStrictToolJsonInstruction(binding) {
|
|
138
|
-
return applyToolRecoveryInstruction(binding, STRICT_TOOL_JSON_INSTRUCTION);
|
|
139
|
-
}
|
|
140
|
-
export function applyToolRecoveryInstruction(binding, instruction) {
|
|
141
|
-
if (isLangChainBinding(binding)) {
|
|
142
|
-
return withUpdatedBindingExecutionParams(binding, (params) => ({
|
|
143
|
-
...params,
|
|
144
|
-
systemPrompt: [params.systemPrompt, instruction].filter(Boolean).join("\n\n"),
|
|
145
|
-
}));
|
|
146
|
-
}
|
|
147
|
-
if (isDeepAgentBinding(binding)) {
|
|
148
|
-
return withUpdatedBindingExecutionParams(binding, (params) => ({
|
|
149
|
-
...params,
|
|
150
|
-
systemPrompt: [params.systemPrompt, instruction].filter(Boolean).join("\n\n"),
|
|
151
|
-
}));
|
|
152
|
-
}
|
|
153
|
-
return binding;
|
|
154
|
-
}
|
|
155
|
-
export async function callRuntimeWithToolParseRecovery(input) {
|
|
156
|
-
try {
|
|
157
|
-
return await capturePromptedJsonToolCalls(() => input.callRuntime(input.binding, input.request));
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
const recoveryInstruction = resolveToolCallRecoveryInstruction(error);
|
|
161
|
-
if (input.resumePayload !== undefined || !recoveryInstruction || !isToolCallRecoveryFailure(error)) {
|
|
162
|
-
throw error;
|
|
163
|
-
}
|
|
164
|
-
return capturePromptedJsonToolCalls(() => input.callRuntime(applyToolRecoveryInstruction(input.binding, recoveryInstruction), appendToolRecoveryInstruction(input.request, recoveryInstruction)));
|
|
165
|
-
}
|
|
166
|
-
}
|
|
1
|
+
import{setTimeout as v}from"node:timers/promises";import{appendToolRecoveryInstruction as h,extractVisibleOutput as m,isToolCallRecoveryFailure as w,resolveToolCallRecoveryInstruction as p,STRICT_TOOL_JSON_INSTRUCTION as R}from"../parsing/output-parsing.js";import{readStreamDelta as T}from"../parsing/stream-event-parsing.js";import{computeRemainingTimeoutMs as d,isRetryableProviderError as k,resolveProviderRetryPolicy as x}from"./resilience.js";import{isDeepAgentBinding as P,isLangChainBinding as g,withUpdatedBindingExecutionParams as l}from"../support/compiled-binding.js";import{capturePromptedJsonToolCalls as u}from"./model/prompted-json-tool-call-capture.js";class c extends Error{operation;timeoutMs;stage;constructor(e,r,n=e.includes("stream")?"stream":"invoke"){super(`${e} timed out after ${r}ms`),this.operation=e,this.timeoutMs=r,this.stage=n,this.name="RuntimeOperationTimeoutError"}}async function j(t,e,r={}){const n=x(t);let i;for(let s=1;s<=n.maxAttempts;s+=1)try{return await e()}catch(o){if(i=o,s>=n.maxAttempts||!k(t,o))throw o;await r.onRetry?.(s,o),n.backoffMs>0&&await v(n.backoffMs)}throw i instanceof Error?i:new Error(String(i))}async function E(t,e,r,n=r.includes("stream")?"stream":"invoke"){return e?new Promise((i,s)=>{const o=setTimeout(()=>s(new c(r,e,n)),e);Promise.resolve(t()).then(a=>{clearTimeout(o),i(a)},a=>{clearTimeout(o),s(a)})}):Promise.resolve(t())}async function*J(t,e,r,n,i){const s=t[Symbol.asyncIterator]();try{for(;;){const o=d(n,e);if(o!==void 0&&o<=0)throw new c(r,i??e??0,"invoke");let a;try{a=await E(()=>s.next(),o,r,"stream")}catch(f){throw f instanceof c&&n&&i&&o!==e?new c(r,i,"invoke"):f}if(a.done)return;yield a.value}}finally{if(typeof s.return=="function"){const o=s.return();o&&typeof o.then=="function"&&o.catch(()=>{})}}}async function I(t,e,r){const n=await t(e,r);let i="";for await(const s of n){const o=T(s)||m(s);o&&(i+=o)}return{content:i}}function N(t){return{invoke:async(e,r)=>{const n=typeof e=="object"&&e!==null&&"messages"in e?e.messages:e;if(typeof t.invoke=="function")return t.invoke(n,r);if(typeof t.stream=="function")return I(t.stream.bind(t),n,r);throw new Error("Resolved model must define invoke or stream.")},stream:async(e,r)=>{if(typeof t.stream=="function"){const n=typeof e=="object"&&e!==null&&"messages"in e?e.messages:e;return t.stream(n,r)}if(typeof t.invoke=="function"){const n=typeof e=="object"&&e!==null&&"messages"in e?e.messages:e,i=await t.invoke(n,r),s=m(i);async function*o(){yield{content:s}}return o()}throw new Error("Resolved model must define invoke or stream.")}}}function W(t){return y(t,R)}function y(t,e){return g(t)?l(t,r=>({...r,systemPrompt:[r.systemPrompt,e].filter(Boolean).join(`
|
|
2
|
+
|
|
3
|
+
`)})):P(t)?l(t,r=>({...r,systemPrompt:[r.systemPrompt,e].filter(Boolean).join(`
|
|
4
|
+
|
|
5
|
+
`)})):t}async function _(t){try{return await u(()=>t.callRuntime(t.binding,t.request))}catch(e){const r=p(e);if(t.resumePayload!==void 0||!r||!w(e))throw e;return u(()=>t.callRuntime(y(t.binding,r),h(t.request,r)))}}export{c as RuntimeOperationTimeoutError,W as applyStrictToolJsonInstruction,y as applyToolRecoveryInstruction,_ as callRuntimeWithToolParseRecovery,N as createModelFallbackRunnable,j as invokeWithProviderRetry,J as iterateWithTimeout,I as materializeModelStream,E as withRuntimeTimeout};
|