@botbotgo/agent-harness 0.0.474 → 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 +2 -2
- 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 -411
- 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 -1089
- 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,137 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return [];
|
|
7
|
-
}
|
|
8
|
-
if (typeof error === "object" || typeof error === "function") {
|
|
9
|
-
seen.add(error);
|
|
10
|
-
}
|
|
11
|
-
if (typeof error === "string") {
|
|
12
|
-
const trimmed = error.trim();
|
|
13
|
-
return trimmed ? [trimmed] : [];
|
|
14
|
-
}
|
|
15
|
-
if (!(error instanceof Error)) {
|
|
16
|
-
const message = String(error).trim();
|
|
17
|
-
return message ? [message] : [];
|
|
18
|
-
}
|
|
19
|
-
const messages = [];
|
|
20
|
-
const primary = error.message.trim();
|
|
21
|
-
if (primary) {
|
|
22
|
-
messages.push(primary);
|
|
23
|
-
}
|
|
24
|
-
const composite = error;
|
|
25
|
-
if (Array.isArray(composite.errors)) {
|
|
26
|
-
for (const nested of composite.errors) {
|
|
27
|
-
messages.push(...collectRuntimeErrorMessages(nested, seen));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (composite.cause !== undefined) {
|
|
31
|
-
messages.push(...collectRuntimeErrorMessages(composite.cause, seen));
|
|
32
|
-
}
|
|
33
|
-
return messages;
|
|
34
|
-
}
|
|
35
|
-
export function describeRuntimeError(error) {
|
|
36
|
-
const uniqueMessages = Array.from(new Set(collectRuntimeErrorMessages(error)));
|
|
37
|
-
if (uniqueMessages.length === 0) {
|
|
38
|
-
return String(error).trim();
|
|
39
|
-
}
|
|
40
|
-
if (uniqueMessages.length === 1) {
|
|
41
|
-
return uniqueMessages[0];
|
|
42
|
-
}
|
|
43
|
-
const nested = uniqueMessages.slice(1, 6);
|
|
44
|
-
const suffix = uniqueMessages.length > 6 ? `\n...and ${uniqueMessages.length - 6} more` : "";
|
|
45
|
-
return `${uniqueMessages[0]}\ncauses:\n${nested.map((message, index) => `${index + 1}. ${message}`).join("\n")}${suffix}`;
|
|
46
|
-
}
|
|
47
|
-
export function renderRuntimeFailure(error) {
|
|
48
|
-
return `runtime_error=${describeRuntimeError(error)}`.trim();
|
|
49
|
-
}
|
|
50
|
-
function readToolErrorMessage(output) {
|
|
51
|
-
if (typeof output === "string" && output.trim()) {
|
|
52
|
-
return output.trim();
|
|
53
|
-
}
|
|
54
|
-
if (output instanceof Error) {
|
|
55
|
-
return output.message;
|
|
56
|
-
}
|
|
57
|
-
if (typeof output !== "object" || !output) {
|
|
58
|
-
return "";
|
|
59
|
-
}
|
|
60
|
-
const record = output;
|
|
61
|
-
for (const candidate of [record.error, record.message, record.detail, record.details]) {
|
|
62
|
-
if (typeof candidate === "string" && candidate.trim()) {
|
|
63
|
-
return candidate.trim();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
return JSON.stringify(output);
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
return String(output);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
export function renderToolFailure(toolName, output) {
|
|
74
|
-
const detail = readToolErrorMessage(output);
|
|
75
|
-
return detail ? `Tool ${toolName} failed: ${detail}` : `Tool ${toolName} failed.`;
|
|
76
|
-
}
|
|
77
|
-
export function parseInterruptContent(content) {
|
|
78
|
-
try {
|
|
79
|
-
const raw = JSON.parse(content);
|
|
80
|
-
const parsed = Array.isArray(raw) && raw.length > 0 && typeof raw[0] === "object" && raw[0]
|
|
81
|
-
? raw[0]
|
|
82
|
-
: typeof raw === "object" && raw
|
|
83
|
-
? raw
|
|
84
|
-
: {};
|
|
85
|
-
const allowedDecisions = Array.isArray(parsed.allowedDecisions)
|
|
86
|
-
? parsed.allowedDecisions.filter((item) => item === "approve" || item === "edit" || item === "reject")
|
|
87
|
-
: undefined;
|
|
88
|
-
return {
|
|
89
|
-
toolName: typeof parsed.toolName === "string" ? parsed.toolName : undefined,
|
|
90
|
-
toolId: typeof parsed.toolId === "string" ? parsed.toolId : undefined,
|
|
91
|
-
allowedDecisions,
|
|
92
|
-
inputPreview: typeof parsed.inputPreview === "object" && parsed.inputPreview ? parsed.inputPreview : undefined,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
catch {
|
|
96
|
-
return {};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
export function createHarnessEvent(sessionId, requestId, sequence, eventType, payload, source = "runtime") {
|
|
100
|
-
return {
|
|
101
|
-
eventId: `evt-${String(sequence).padStart(6, "0")}`,
|
|
102
|
-
eventType,
|
|
103
|
-
timestamp: new Date().toISOString(),
|
|
104
|
-
sessionId,
|
|
105
|
-
requestId,
|
|
106
|
-
sequence,
|
|
107
|
-
source,
|
|
108
|
-
payload,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
export function createPendingApproval(sessionId, requestId, checkpointRef, input, interruptContent) {
|
|
112
|
-
const requestedAt = new Date().toISOString();
|
|
113
|
-
const interrupt = interruptContent ? parseInterruptContent(interruptContent) : {};
|
|
114
|
-
const approvalId = `approval-${createPersistentId()}`;
|
|
115
|
-
return {
|
|
116
|
-
approvalId,
|
|
117
|
-
pendingActionId: approvalId,
|
|
118
|
-
sessionId,
|
|
119
|
-
requestId,
|
|
120
|
-
toolCallId: interrupt.toolId ?? `tool-${requestId}`,
|
|
121
|
-
toolName: interrupt.toolName ?? "write_file",
|
|
122
|
-
status: "pending",
|
|
123
|
-
requestedAt,
|
|
124
|
-
resolvedAt: null,
|
|
125
|
-
allowedDecisions: interrupt.allowedDecisions ?? ["approve", "edit", "reject"],
|
|
126
|
-
inputPreview: interrupt.inputPreview ?? { input },
|
|
127
|
-
checkpointRef,
|
|
128
|
-
eventRefs: ["000003.json", "000004.json"],
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
export function inferRoutingBindings(workspace) {
|
|
132
|
-
const runtimeEntryBindings = Array.from(workspace.bindings.values());
|
|
133
|
-
const orchestrationRuntimeEntries = runtimeEntryBindings.filter((binding) => isDeepAgentBinding(binding));
|
|
134
|
-
const routingEntries = orchestrationRuntimeEntries.length > 0 ? orchestrationRuntimeEntries : runtimeEntryBindings;
|
|
135
|
-
const primaryBinding = routingEntries.find((binding) => isDelegationCapableBinding(binding)) ?? routingEntries[0] ?? runtimeEntryBindings[0];
|
|
136
|
-
return { primaryBinding, runtimeEntryBindings };
|
|
137
|
-
}
|
|
1
|
+
import{createPersistentId as f}from"../../utils/id.js";import{isDelegationCapableBinding as d}from"../../workspace/support/agent-capabilities.js";import{isDeepAgentBinding as l}from"./compiled-binding.js";function a(e,n=new Set){if(e==null||n.has(e))return[];if((typeof e=="object"||typeof e=="function")&&n.add(e),typeof e=="string"){const r=e.trim();return r?[r]:[]}if(!(e instanceof Error)){const r=String(e).trim();return r?[r]:[]}const t=[],o=e.message.trim();o&&t.push(o);const i=e;if(Array.isArray(i.errors))for(const r of i.errors)t.push(...a(r,n));return i.cause!==void 0&&t.push(...a(i.cause,n)),t}function u(e){const n=Array.from(new Set(a(e)));if(n.length===0)return String(e).trim();if(n.length===1)return n[0];const t=n.slice(1,6),o=n.length>6?`
|
|
2
|
+
...and ${n.length-6} more`:"";return`${n[0]}
|
|
3
|
+
causes:
|
|
4
|
+
${t.map((i,r)=>`${r+1}. ${i}`).join(`
|
|
5
|
+
`)}${o}`}function w(e){return`runtime_error=${u(e)}`.trim()}function p(e){if(typeof e=="string"&&e.trim())return e.trim();if(e instanceof Error)return e.message;if(typeof e!="object"||!e)return"";const n=e;for(const t of[n.error,n.message,n.detail,n.details])if(typeof t=="string"&&t.trim())return t.trim();try{return JSON.stringify(e)}catch{return String(e)}}function h(e,n){const t=p(n);return t?`Tool ${e} failed: ${t}`:`Tool ${e} failed.`}function m(e){try{const n=JSON.parse(e),t=Array.isArray(n)&&n.length>0&&typeof n[0]=="object"&&n[0]?n[0]:typeof n=="object"&&n?n:{},o=Array.isArray(t.allowedDecisions)?t.allowedDecisions.filter(i=>i==="approve"||i==="edit"||i==="reject"):void 0;return{toolName:typeof t.toolName=="string"?t.toolName:void 0,toolId:typeof t.toolId=="string"?t.toolId:void 0,allowedDecisions:o,inputPreview:typeof t.inputPreview=="object"&&t.inputPreview?t.inputPreview:void 0}}catch{return{}}}function A(e,n,t,o,i,r="runtime"){return{eventId:`evt-${String(t).padStart(6,"0")}`,eventType:o,timestamp:new Date().toISOString(),sessionId:e,requestId:n,sequence:t,source:r,payload:i}}function S(e,n,t,o,i){const r=new Date().toISOString(),s=i?m(i):{},c=`approval-${f()}`;return{approvalId:c,pendingActionId:c,sessionId:e,requestId:n,toolCallId:s.toolId??`tool-${n}`,toolName:s.toolName??"write_file",status:"pending",requestedAt:r,resolvedAt:null,allowedDecisions:s.allowedDecisions??["approve","edit","reject"],inputPreview:s.inputPreview??{input:o},checkpointRef:t,eventRefs:["000003.json","000004.json"]}}function $(e){const n=Array.from(e.bindings.values()),t=n.filter(r=>l(r)),o=t.length>0?t:n;return{primaryBinding:o.find(r=>d(r))??o[0]??n[0],runtimeEntryBindings:n}}export{A as createHarnessEvent,S as createPendingApproval,u as describeRuntimeError,$ as inferRoutingBindings,m as parseInterruptContent,w as renderRuntimeFailure,h as renderToolFailure};
|
|
@@ -1,108 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { mkdir } from "node:fs/promises";
|
|
3
|
-
import { Document, NodeRelationship, SimpleVectorStore, VectorStoreQueryMode } from "llamaindex";
|
|
4
|
-
import { OllamaEmbedding } from "@llamaindex/ollama";
|
|
5
|
-
function resolveLlamaIndexOllamaInit(init) {
|
|
6
|
-
const config = typeof init.config === "object" && init.config
|
|
7
|
-
? { ...init.config }
|
|
8
|
-
: {};
|
|
9
|
-
if (typeof init.baseUrl === "string" && !("host" in config)) {
|
|
10
|
-
config.host = init.baseUrl;
|
|
11
|
-
}
|
|
12
|
-
const options = typeof init.options === "object" && init.options
|
|
13
|
-
? { ...init.options }
|
|
14
|
-
: undefined;
|
|
15
|
-
return {
|
|
16
|
-
model: String(init.model ?? ""),
|
|
17
|
-
config,
|
|
18
|
-
...(options ? { options } : {}),
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function resolveFilePath(rawUrl, workspaceRoot) {
|
|
22
|
-
const normalized = rawUrl.startsWith("file:") ? rawUrl.slice("file:".length) : rawUrl;
|
|
23
|
-
return path.isAbsolute(normalized) ? normalized : path.resolve(workspaceRoot, normalized);
|
|
24
|
-
}
|
|
25
|
-
export function createLlamaIndexEmbeddingModel(embeddingModel) {
|
|
26
|
-
if (embeddingModel.provider !== "llamaindex-ollama") {
|
|
27
|
-
throw new Error(`Embedding model provider ${embeddingModel.provider} is not supported by the built-in LlamaIndex runtime.`);
|
|
28
|
-
}
|
|
29
|
-
const runtime = new OllamaEmbedding({
|
|
30
|
-
...resolveLlamaIndexOllamaInit({
|
|
31
|
-
...embeddingModel.init,
|
|
32
|
-
model: embeddingModel.model,
|
|
33
|
-
}),
|
|
34
|
-
});
|
|
35
|
-
return {
|
|
36
|
-
async embedDocuments(texts) {
|
|
37
|
-
return Promise.all(texts.map((text) => runtime.getTextEmbedding(text)));
|
|
38
|
-
},
|
|
39
|
-
async embedQuery(text) {
|
|
40
|
-
return runtime.getTextEmbedding(text);
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
export async function createLlamaIndexVectorStore(dataRoot, vectorStore, embeddings) {
|
|
45
|
-
const persistPath = resolveFilePath(vectorStore.url ?? "knowledge/vectors.json", dataRoot);
|
|
46
|
-
await mkdir(path.dirname(persistPath), { recursive: true });
|
|
47
|
-
const emptyStore = () => SimpleVectorStore.fromDict({
|
|
48
|
-
embeddingDict: {},
|
|
49
|
-
textIdToRefDocId: {},
|
|
50
|
-
metadataDict: {},
|
|
51
|
-
}, embeddings);
|
|
52
|
-
let store = await SimpleVectorStore.fromPersistPath(persistPath, embeddings);
|
|
53
|
-
return {
|
|
54
|
-
kind: vectorStore.kind,
|
|
55
|
-
embeddingModelRef: vectorStore.embeddingModelRef,
|
|
56
|
-
async addDocuments(documents) {
|
|
57
|
-
const ids = documents.map((_, index) => `${Date.now()}-${index}-${Math.random().toString(36).slice(2, 10)}`);
|
|
58
|
-
const vectors = documents.length > 0 ? await embeddings.embedDocuments(documents.map((document) => document.pageContent)) : [];
|
|
59
|
-
const nodes = documents.map((document, index) => new Document({
|
|
60
|
-
id_: ids[index],
|
|
61
|
-
text: document.pageContent,
|
|
62
|
-
embedding: vectors[index] ?? [],
|
|
63
|
-
metadata: {
|
|
64
|
-
...(document.metadata ?? {}),
|
|
65
|
-
__pageContent: document.pageContent,
|
|
66
|
-
},
|
|
67
|
-
relationships: {
|
|
68
|
-
[NodeRelationship.SOURCE]: {
|
|
69
|
-
nodeId: ids[index] ?? "",
|
|
70
|
-
metadata: document.metadata ?? {},
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
}));
|
|
74
|
-
const created = await store.add(nodes);
|
|
75
|
-
await store.persist(persistPath);
|
|
76
|
-
return created;
|
|
77
|
-
},
|
|
78
|
-
async similaritySearch(query, limit) {
|
|
79
|
-
const queryEmbedding = await embeddings.embedQuery(query);
|
|
80
|
-
const result = await store.query({
|
|
81
|
-
queryEmbedding,
|
|
82
|
-
similarityTopK: limit,
|
|
83
|
-
mode: VectorStoreQueryMode.DEFAULT,
|
|
84
|
-
});
|
|
85
|
-
return result.ids.map((id, index) => {
|
|
86
|
-
const metadata = (store.toDict().metadataDict[id] ?? {});
|
|
87
|
-
const pageContent = typeof metadata.__pageContent === "string" ? metadata.__pageContent : "";
|
|
88
|
-
const { __pageContent: _, ...rest } = metadata;
|
|
89
|
-
return {
|
|
90
|
-
pageContent,
|
|
91
|
-
metadata: rest,
|
|
92
|
-
score: result.similarities[index],
|
|
93
|
-
};
|
|
94
|
-
});
|
|
95
|
-
},
|
|
96
|
-
async delete(params) {
|
|
97
|
-
if (params.deleteAll) {
|
|
98
|
-
store = emptyStore();
|
|
99
|
-
await store.persist(persistPath);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
for (const id of params.ids ?? []) {
|
|
103
|
-
await store.delete(id);
|
|
104
|
-
}
|
|
105
|
-
await store.persist(persistPath);
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
1
|
+
import p from"node:path";import{mkdir as b}from"node:fs/promises";import{Document as h,NodeRelationship as w,SimpleVectorStore as f,VectorStoreQueryMode as D}from"llamaindex";import{OllamaEmbedding as _}from"@llamaindex/ollama";function x(e){const o=typeof e.config=="object"&&e.config?{...e.config}:{};typeof e.baseUrl=="string"&&!("host"in o)&&(o.host=e.baseUrl);const t=typeof e.options=="object"&&e.options?{...e.options}:void 0;return{model:String(e.model??""),config:o,...t?{options:t}:{}}}function C(e,o){const t=e.startsWith("file:")?e.slice(5):e;return p.isAbsolute(t)?t:p.resolve(o,t)}function P(e){if(e.provider!=="llamaindex-ollama")throw new Error(`Embedding model provider ${e.provider} is not supported by the built-in LlamaIndex runtime.`);const o=new _({...x({...e.init,model:e.model})});return{async embedDocuments(t){return Promise.all(t.map(s=>o.getTextEmbedding(s)))},async embedQuery(t){return o.getTextEmbedding(t)}}}async function R(e,o,t){const s=C(o.url??"knowledge/vectors.json",e);await b(p.dirname(s),{recursive:!0});const u=()=>f.fromDict({embeddingDict:{},textIdToRefDocId:{},metadataDict:{}},t);let i=await f.fromPersistPath(s,t);return{kind:o.kind,embeddingModelRef:o.embeddingModelRef,async addDocuments(a){const d=a.map((n,r)=>`${Date.now()}-${r}-${Math.random().toString(36).slice(2,10)}`),c=a.length>0?await t.embedDocuments(a.map(n=>n.pageContent)):[],m=a.map((n,r)=>new h({id_:d[r],text:n.pageContent,embedding:c[r]??[],metadata:{...n.metadata??{},__pageContent:n.pageContent},relationships:{[w.SOURCE]:{nodeId:d[r]??"",metadata:n.metadata??{}}}})),l=await i.add(m);return await i.persist(s),l},async similaritySearch(a,d){const c=await t.embedQuery(a),m=await i.query({queryEmbedding:c,similarityTopK:d,mode:D.DEFAULT});return m.ids.map((l,n)=>{const r=i.toDict().metadataDict[l]??{},g=typeof r.__pageContent=="string"?r.__pageContent:"",{__pageContent:E,...y}=r;return{pageContent:g,metadata:y,score:m.similarities[n]}})},async delete(a){if(a.deleteAll){i=u(),await i.persist(s);return}for(const d of a.ids??[])await i.delete(d);await i.persist(s)}}}export{P as createLlamaIndexEmbeddingModel,R as createLlamaIndexVectorStore};
|
|
@@ -1,29 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { resolveCheckpointer, resolveEmbeddingModel, resolveStore, resolveVectorStore, } from "../harness/run/resources.js";
|
|
3
|
-
import { getBindingStoreConfig } from "./compiled-binding.js";
|
|
4
|
-
export function createBindingStoreResolver(input) {
|
|
5
|
-
return (binding) => resolveStore(input.stores, input.defaultStore, input.getDefaultRuntimeRoot(), (currentBinding) => currentBinding ? getBindingStoreConfig(currentBinding) : undefined, binding);
|
|
6
|
-
}
|
|
7
|
-
export function resolveRuntimeAdapterOptions(input) {
|
|
8
|
-
const { workspace, runtimeAdapterOptions, checkpointers, stores, defaultStore, embeddingModels, vectorStores, getDefaultRuntimeRoot, } = input;
|
|
9
|
-
const storeResolver = runtimeAdapterOptions.storeResolver ??
|
|
10
|
-
createBindingStoreResolver({
|
|
11
|
-
stores,
|
|
12
|
-
defaultStore,
|
|
13
|
-
getDefaultRuntimeRoot,
|
|
14
|
-
});
|
|
15
|
-
return {
|
|
16
|
-
...runtimeAdapterOptions,
|
|
17
|
-
toolResolver: runtimeAdapterOptions.toolResolver ??
|
|
18
|
-
createResourceToolResolver(workspace, {
|
|
19
|
-
getStore: (binding) => binding ? storeResolver(binding) : defaultStore,
|
|
20
|
-
getEmbeddingModel: (embeddingModelRef) => resolveEmbeddingModel(workspace, embeddingModels, embeddingModelRef, runtimeAdapterOptions),
|
|
21
|
-
getVectorStore: (vectorStoreRef) => resolveVectorStore(workspace, vectorStores, vectorStoreRef, runtimeAdapterOptions),
|
|
22
|
-
}),
|
|
23
|
-
checkpointerResolver: runtimeAdapterOptions.checkpointerResolver ??
|
|
24
|
-
((binding) => resolveCheckpointer(checkpointers, binding)),
|
|
25
|
-
storeResolver,
|
|
26
|
-
backendResolver: runtimeAdapterOptions.backendResolver ??
|
|
27
|
-
((binding) => createResourceBackendResolver(workspace)(binding)),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
1
|
+
import{createResourceBackendResolver as v,createResourceToolResolver as a}from"../../resource/resource.js";import{resolveCheckpointer as m,resolveEmbeddingModel as p,resolveStore as u,resolveVectorStore as f}from"../harness/run/resources.js";import{getBindingStoreConfig as g}from"./compiled-binding.js";function S(t){return r=>u(t.stores,t.defaultStore,t.getDefaultRuntimeRoot(),e=>e?g(e):void 0,r)}function M(t){const{workspace:r,runtimeAdapterOptions:e,checkpointers:n,stores:c,defaultStore:s,embeddingModels:d,vectorStores:i,getDefaultRuntimeRoot:R}=t,l=e.storeResolver??S({stores:c,defaultStore:s,getDefaultRuntimeRoot:R});return{...e,toolResolver:e.toolResolver??a(r,{getStore:o=>o?l(o):s,getEmbeddingModel:o=>p(r,d,o,e),getVectorStore:o=>f(r,i,o,e)}),checkpointerResolver:e.checkpointerResolver??(o=>m(n,o)),storeResolver:l,backendResolver:e.backendResolver??(o=>v(r)(o))}}export{S as createBindingStoreResolver,M as resolveRuntimeAdapterOptions};
|
|
@@ -1,51 +1 @@
|
|
|
1
|
-
import path from "
|
|
2
|
-
import { MemorySaver } from "@langchain/langgraph";
|
|
3
|
-
import { FileCheckpointSaver } from "../maintenance/file-checkpoint-saver.js";
|
|
4
|
-
import { SqliteCheckpointSaver } from "../maintenance/sqlite-checkpoint-saver.js";
|
|
5
|
-
import { createInMemoryStore, FileBackedStore, SqliteBackedStore } from "../harness/system/store.js";
|
|
6
|
-
import { resolveKnowledgeFileStorePath, resolveKnowledgeStorePath, resolveRuntimeCheckpointerPath, } from "../layout/runtime-layout.js";
|
|
7
|
-
export function createStoreForConfig(storeConfig, runtimeRoot) {
|
|
8
|
-
const kind = typeof storeConfig.kind === "string" ? storeConfig.kind : "FileStore";
|
|
9
|
-
switch (kind) {
|
|
10
|
-
case "FileStore": {
|
|
11
|
-
const configuredPath = typeof storeConfig.path === "string" ? storeConfig.path : resolveKnowledgeFileStorePath(runtimeRoot, "records.json");
|
|
12
|
-
return new FileBackedStore(path.isAbsolute(configuredPath) ? configuredPath : path.join(runtimeRoot, configuredPath));
|
|
13
|
-
}
|
|
14
|
-
case "SqliteStore": {
|
|
15
|
-
const configuredPath = typeof storeConfig.path === "string" ? storeConfig.path : resolveKnowledgeStorePath(runtimeRoot, "knowledge.sqlite");
|
|
16
|
-
return new SqliteBackedStore(path.isAbsolute(configuredPath) ? configuredPath : path.join(runtimeRoot, configuredPath));
|
|
17
|
-
}
|
|
18
|
-
case "InMemoryStore":
|
|
19
|
-
return createInMemoryStore();
|
|
20
|
-
case "RedisStore":
|
|
21
|
-
throw new Error("Store kind RedisStore is not implemented in this runtime yet");
|
|
22
|
-
case "PostgresStore":
|
|
23
|
-
throw new Error("Store kind PostgresStore is not implemented in this runtime yet");
|
|
24
|
-
default:
|
|
25
|
-
throw new Error(`Unsupported store kind ${String(kind)}`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
export function createCheckpointerForConfig(checkpointerConfig, runtimeRoot) {
|
|
29
|
-
if (typeof checkpointerConfig === "boolean") {
|
|
30
|
-
return checkpointerConfig;
|
|
31
|
-
}
|
|
32
|
-
const kind = typeof checkpointerConfig.kind === "string" ? checkpointerConfig.kind : "SqliteSaver";
|
|
33
|
-
switch (kind) {
|
|
34
|
-
case "MemorySaver":
|
|
35
|
-
return new MemorySaver();
|
|
36
|
-
case "SqliteSaver": {
|
|
37
|
-
const configuredPath = typeof checkpointerConfig.path === "string"
|
|
38
|
-
? String(checkpointerConfig.path)
|
|
39
|
-
: resolveRuntimeCheckpointerPath(runtimeRoot, "checkpoints.sqlite");
|
|
40
|
-
return new SqliteCheckpointSaver(path.isAbsolute(configuredPath) ? configuredPath : path.join(runtimeRoot, configuredPath));
|
|
41
|
-
}
|
|
42
|
-
case "FileCheckpointer": {
|
|
43
|
-
const configuredPath = typeof checkpointerConfig.path === "string"
|
|
44
|
-
? String(checkpointerConfig.path)
|
|
45
|
-
: resolveRuntimeCheckpointerPath(runtimeRoot, "checkpoints.json");
|
|
46
|
-
return new FileCheckpointSaver(path.isAbsolute(configuredPath) ? configuredPath : path.join(runtimeRoot, configuredPath));
|
|
47
|
-
}
|
|
48
|
-
default:
|
|
49
|
-
throw new Error(`Unsupported checkpointer kind ${String(kind)}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
1
|
+
import o from"node:path";import{MemorySaver as s}from"@langchain/langgraph";import{FileCheckpointSaver as a}from"../maintenance/file-checkpoint-saver.js";import{SqliteCheckpointSaver as p}from"../maintenance/sqlite-checkpoint-saver.js";import{createInMemoryStore as S,FileBackedStore as d,SqliteBackedStore as l}from"../harness/system/store.js";import{resolveKnowledgeFileStorePath as c,resolveKnowledgeStorePath as h,resolveRuntimeCheckpointerPath as i}from"../layout/runtime-layout.js";function g(e,r){const n=typeof e.kind=="string"?e.kind:"FileStore";switch(n){case"FileStore":{const t=typeof e.path=="string"?e.path:c(r,"records.json");return new d(o.isAbsolute(t)?t:o.join(r,t))}case"SqliteStore":{const t=typeof e.path=="string"?e.path:h(r,"knowledge.sqlite");return new l(o.isAbsolute(t)?t:o.join(r,t))}case"InMemoryStore":return S();case"RedisStore":throw new Error("Store kind RedisStore is not implemented in this runtime yet");case"PostgresStore":throw new Error("Store kind PostgresStore is not implemented in this runtime yet");default:throw new Error(`Unsupported store kind ${String(n)}`)}}function v(e,r){if(typeof e=="boolean")return e;const n=typeof e.kind=="string"?e.kind:"SqliteSaver";switch(n){case"MemorySaver":return new s;case"SqliteSaver":{const t=typeof e.path=="string"?String(e.path):i(r,"checkpoints.sqlite");return new p(o.isAbsolute(t)?t:o.join(r,t))}case"FileCheckpointer":{const t=typeof e.path=="string"?String(e.path):i(r,"checkpoints.json");return new a(o.isAbsolute(t)?t:o.join(r,t))}default:throw new Error(`Unsupported checkpointer kind ${String(n)}`)}}export{v as createCheckpointerForConfig,g as createStoreForConfig};
|
|
@@ -1,274 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { createClient } from "@libsql/client";
|
|
4
|
-
import { QdrantClient } from "@qdrant/js-client-rest";
|
|
5
|
-
import { compileVectorStore } from "../../workspace/resource-compilers.js";
|
|
6
|
-
import { getRuntimeStorageRoots, resolveRefId } from "../../workspace/support/workspace-ref-utils.js";
|
|
7
|
-
import { resolveCompiledEmbeddingModel, resolveCompiledEmbeddingModelRef } from "./embedding-models.js";
|
|
8
|
-
function resolveFileUrl(rawUrl, workspaceRoot) {
|
|
9
|
-
if (!rawUrl.startsWith("file:")) {
|
|
10
|
-
return rawUrl;
|
|
11
|
-
}
|
|
12
|
-
const filePath = rawUrl.slice("file:".length);
|
|
13
|
-
if (!filePath) {
|
|
14
|
-
return rawUrl;
|
|
15
|
-
}
|
|
16
|
-
if (path.isAbsolute(filePath)) {
|
|
17
|
-
return `file:${filePath}`;
|
|
18
|
-
}
|
|
19
|
-
return `file:${path.resolve(workspaceRoot, filePath)}`;
|
|
20
|
-
}
|
|
21
|
-
async function ensureFileUrlDirectory(rawUrl, workspaceRoot) {
|
|
22
|
-
const resolvedUrl = resolveFileUrl(rawUrl, workspaceRoot);
|
|
23
|
-
if (!resolvedUrl.startsWith("file:")) {
|
|
24
|
-
return resolvedUrl;
|
|
25
|
-
}
|
|
26
|
-
const filePath = resolvedUrl.slice("file:".length);
|
|
27
|
-
await mkdir(path.dirname(filePath), { recursive: true });
|
|
28
|
-
return resolvedUrl;
|
|
29
|
-
}
|
|
30
|
-
function assertIdentifier(value, label) {
|
|
31
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(value)) {
|
|
32
|
-
throw new Error(`Invalid ${label} ${value}. Use only letters, numbers, and underscores.`);
|
|
33
|
-
}
|
|
34
|
-
return value;
|
|
35
|
-
}
|
|
36
|
-
async function ensureLibSqlSchema(db, table, column, dimensions) {
|
|
37
|
-
const safeTable = assertIdentifier(table, "table name");
|
|
38
|
-
const safeColumn = assertIdentifier(column, "column name");
|
|
39
|
-
const safeIndex = assertIdentifier(`idx_${safeTable}_${safeColumn}`, "index name");
|
|
40
|
-
await db.execute(`
|
|
41
|
-
CREATE TABLE IF NOT EXISTS ${safeTable} (
|
|
1
|
+
import C from"node:path";import{mkdir as A}from"node:fs/promises";import{createClient as L}from"@libsql/client";import{QdrantClient as x}from"@qdrant/js-client-rest";import{compileVectorStore as I}from"../../workspace/resource-compilers.js";import{getRuntimeStorageRoots as V,resolveRefId as _}from"../../workspace/support/workspace-ref-utils.js";import{resolveCompiledEmbeddingModel as $,resolveCompiledEmbeddingModelRef as R}from"./embedding-models.js";function O(e,t){if(!e.startsWith("file:"))return e;const n=e.slice(5);return n?C.isAbsolute(n)?`file:${n}`:`file:${C.resolve(t,n)}`:e}async function S(e,t){const n=O(e,t);if(!n.startsWith("file:"))return n;const o=n.slice(5);return await A(C.dirname(o),{recursive:!0}),n}function b(e,t){if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(e))throw new Error(`Invalid ${t} ${e}. Use only letters, numbers, and underscores.`);return e}async function k(e,t,n,o){const r=b(t,"table name"),i=b(n,"column name"),u=b(`idx_${r}_${i}`,"index name");await e.execute(`
|
|
2
|
+
CREATE TABLE IF NOT EXISTS ${r} (
|
|
42
3
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
43
4
|
content TEXT NOT NULL,
|
|
44
5
|
metadata TEXT NOT NULL,
|
|
45
|
-
${
|
|
6
|
+
${i} F32_BLOB(${o}) NOT NULL
|
|
46
7
|
)
|
|
47
|
-
`)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
function serializeVector(vector) {
|
|
54
|
-
if (!vector.every((value) => typeof value === "number" && Number.isFinite(value))) {
|
|
55
|
-
throw new Error("Invalid vector: all elements must be finite numbers.");
|
|
56
|
-
}
|
|
57
|
-
return `[${vector.join(",")}]`;
|
|
58
|
-
}
|
|
59
|
-
async function addLibSqlDocuments(db, table, column, documents, vectors) {
|
|
60
|
-
const safeTable = assertIdentifier(table, "table name");
|
|
61
|
-
const safeColumn = assertIdentifier(column, "column name");
|
|
62
|
-
const statements = documents.map((document, index) => ({
|
|
63
|
-
sql: `INSERT INTO ${safeTable} (content, metadata, ${safeColumn}) VALUES (:content, :metadata, vector(:embedding)) RETURNING ${safeTable}.rowid AS id`,
|
|
64
|
-
args: {
|
|
65
|
-
content: document.pageContent,
|
|
66
|
-
metadata: JSON.stringify(document.metadata ?? {}),
|
|
67
|
-
embedding: serializeVector(vectors[index] ?? []),
|
|
68
|
-
},
|
|
69
|
-
}));
|
|
70
|
-
if (statements.length === 0) {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
const results = await db.batch(statements);
|
|
74
|
-
return results.flatMap((result) => result.rows.map((row) => String(row.id)));
|
|
75
|
-
}
|
|
76
|
-
async function similaritySearchLibSql(db, table, column, queryVector, limit) {
|
|
77
|
-
const safeTable = assertIdentifier(table, "table name");
|
|
78
|
-
const safeColumn = assertIdentifier(column, "column name");
|
|
79
|
-
const safeIndex = assertIdentifier(`idx_${safeTable}_${safeColumn}`, "index name");
|
|
80
|
-
const rows = await db.execute({
|
|
81
|
-
sql: `SELECT ${safeTable}.rowid AS id, ${safeTable}.content, ${safeTable}.metadata, vector_distance_cos(${safeTable}.${safeColumn}, vector(:queryVector)) AS distance
|
|
82
|
-
FROM vector_top_k('${safeIndex}', vector(:queryVector), CAST(:limit AS INTEGER)) AS top_k
|
|
83
|
-
JOIN ${safeTable} ON top_k.rowid = ${safeTable}.rowid`,
|
|
84
|
-
args: {
|
|
85
|
-
queryVector: serializeVector(queryVector),
|
|
86
|
-
limit,
|
|
87
|
-
},
|
|
88
|
-
});
|
|
89
|
-
return rows.rows.map((row) => {
|
|
90
|
-
const parsedMetadata = typeof row.metadata === "string" ? JSON.parse(row.metadata) : {};
|
|
91
|
-
const metadata = typeof parsedMetadata === "object" && parsedMetadata && !Array.isArray(parsedMetadata)
|
|
92
|
-
? parsedMetadata
|
|
93
|
-
: {};
|
|
94
|
-
return {
|
|
95
|
-
pageContent: typeof row.content === "string" ? row.content : "",
|
|
96
|
-
metadata,
|
|
97
|
-
score: typeof row.distance === "number" ? 1 - row.distance : undefined,
|
|
98
|
-
};
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
async function deleteLibSqlVectors(db, table, params) {
|
|
102
|
-
const safeTable = assertIdentifier(table, "table name");
|
|
103
|
-
if (params.deleteAll) {
|
|
104
|
-
await db.execute(`DELETE FROM ${safeTable}`);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
const ids = params.ids ?? [];
|
|
108
|
-
if (ids.length === 0) {
|
|
109
|
-
throw new Error('You must provide an "ids" parameter or a "deleteAll" parameter.');
|
|
110
|
-
}
|
|
111
|
-
await db.batch(ids.map((id) => ({
|
|
112
|
-
sql: `DELETE FROM ${safeTable} WHERE rowid = :id`,
|
|
113
|
-
args: { id },
|
|
114
|
-
})));
|
|
115
|
-
}
|
|
116
|
-
function createQdrantClientForStore(vectorStore) {
|
|
117
|
-
return new QdrantClient({
|
|
118
|
-
...(vectorStore.url ? { url: vectorStore.url } : {}),
|
|
119
|
-
...(vectorStore.host ? { host: vectorStore.host } : {}),
|
|
120
|
-
...(typeof vectorStore.port === "number" ? { port: vectorStore.port } : {}),
|
|
121
|
-
...(vectorStore.authToken ? { apiKey: vectorStore.authToken } : {}),
|
|
122
|
-
checkCompatibility: false,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
async function ensureQdrantCollection(client, collection, dimensions) {
|
|
126
|
-
const collections = await client.getCollections();
|
|
127
|
-
if (collections.collections.some((entry) => entry.name === collection)) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
await client.createCollection(collection, {
|
|
131
|
-
vectors: {
|
|
132
|
-
size: dimensions,
|
|
133
|
-
distance: "Cosine",
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
async function createLazyLlamaIndexVectorStore(dataRoot, vectorStore, embeddings) {
|
|
138
|
-
const { createLlamaIndexVectorStore } = await import("./llamaindex.js");
|
|
139
|
-
return createLlamaIndexVectorStore(dataRoot, vectorStore, embeddings);
|
|
140
|
-
}
|
|
141
|
-
export function resolveCompiledVectorStoreRef(workspace, vectorStoreRef) {
|
|
142
|
-
const resolvedId = vectorStoreRef ? resolveRefId(vectorStoreRef) : "default";
|
|
143
|
-
const vectorStore = workspace.vectorStores.get(resolvedId);
|
|
144
|
-
if (vectorStore) {
|
|
145
|
-
return compileVectorStore(vectorStore);
|
|
146
|
-
}
|
|
147
|
-
if (!vectorStoreRef && workspace.vectorStores.size === 1) {
|
|
148
|
-
const soleStore = workspace.vectorStores.values().next().value;
|
|
149
|
-
if (soleStore) {
|
|
150
|
-
return compileVectorStore(soleStore);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
throw new Error(`Missing vector store ${vectorStoreRef ?? "vector-store/default"}`);
|
|
154
|
-
}
|
|
155
|
-
export async function resolveCompiledVectorStore(workspace, vectorStore, options = {}) {
|
|
156
|
-
if (options.vectorStoreResolver) {
|
|
157
|
-
return options.vectorStoreResolver(vectorStore.id);
|
|
158
|
-
}
|
|
159
|
-
const { dataRoot } = getRuntimeStorageRoots(workspace.refs, workspace.workspaceRoot);
|
|
160
|
-
if (vectorStore.kind === "LlamaIndexSimpleVectorStore") {
|
|
161
|
-
const embeddingModel = resolveCompiledEmbeddingModelRef(workspace, vectorStore.embeddingModelRef);
|
|
162
|
-
const embeddings = await resolveCompiledEmbeddingModel(embeddingModel, options.embeddingModelResolver);
|
|
163
|
-
return createLazyLlamaIndexVectorStore(dataRoot, vectorStore, embeddings);
|
|
164
|
-
}
|
|
165
|
-
if (vectorStore.kind === "QdrantVectorStore") {
|
|
166
|
-
const embeddingModel = resolveCompiledEmbeddingModelRef(workspace, vectorStore.embeddingModelRef);
|
|
167
|
-
const embeddings = await resolveCompiledEmbeddingModel(embeddingModel, options.embeddingModelResolver);
|
|
168
|
-
const client = createQdrantClientForStore(vectorStore);
|
|
169
|
-
const collection = vectorStore.collection ?? "vectors";
|
|
170
|
-
const ensureCollectionForTexts = async (texts) => {
|
|
171
|
-
const seedText = texts.find((text) => text.trim().length > 0) ?? "seed";
|
|
172
|
-
const sample = await embeddings.embedQuery(seedText);
|
|
173
|
-
await ensureQdrantCollection(client, collection, sample.length);
|
|
174
|
-
};
|
|
175
|
-
return {
|
|
176
|
-
kind: vectorStore.kind,
|
|
177
|
-
embeddingModelRef: vectorStore.embeddingModelRef,
|
|
178
|
-
addDocuments: async (documents) => {
|
|
179
|
-
if (documents.length === 0) {
|
|
180
|
-
return [];
|
|
181
|
-
}
|
|
182
|
-
await ensureCollectionForTexts(documents.map((document) => document.pageContent));
|
|
183
|
-
const vectors = await embeddings.embedDocuments(documents.map((document) => document.pageContent));
|
|
184
|
-
const ids = documents.map((_, index) => `${Date.now()}-${index}-${Math.random().toString(36).slice(2, 10)}`);
|
|
185
|
-
await client.upsert(collection, {
|
|
186
|
-
wait: true,
|
|
187
|
-
points: documents.map((document, index) => ({
|
|
188
|
-
id: ids[index] ?? index,
|
|
189
|
-
vector: vectors[index] ?? [],
|
|
190
|
-
payload: {
|
|
191
|
-
pageContent: document.pageContent,
|
|
192
|
-
...(document.metadata ? { metadata: document.metadata } : {}),
|
|
193
|
-
},
|
|
194
|
-
})),
|
|
195
|
-
});
|
|
196
|
-
return ids;
|
|
197
|
-
},
|
|
198
|
-
similaritySearch: async (query, limit) => {
|
|
199
|
-
const collections = await client.getCollections();
|
|
200
|
-
if (!collections.collections.some((entry) => entry.name === collection)) {
|
|
201
|
-
return [];
|
|
202
|
-
}
|
|
203
|
-
const queryVector = await embeddings.embedQuery(query);
|
|
204
|
-
const rows = await client.search(collection, {
|
|
205
|
-
vector: queryVector,
|
|
206
|
-
limit,
|
|
207
|
-
with_payload: true,
|
|
208
|
-
});
|
|
209
|
-
return rows.map((row) => {
|
|
210
|
-
const payload = (row.payload ?? {});
|
|
211
|
-
return {
|
|
212
|
-
pageContent: typeof payload.pageContent === "string" ? payload.pageContent : "",
|
|
213
|
-
metadata: typeof payload.metadata === "object" && payload.metadata && !Array.isArray(payload.metadata)
|
|
214
|
-
? payload.metadata
|
|
215
|
-
: {},
|
|
216
|
-
score: typeof row.score === "number" ? row.score : undefined,
|
|
217
|
-
};
|
|
218
|
-
});
|
|
219
|
-
},
|
|
220
|
-
delete: async (params) => {
|
|
221
|
-
const collections = await client.getCollections();
|
|
222
|
-
if (!collections.collections.some((entry) => entry.name === collection)) {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
if (params.deleteAll) {
|
|
226
|
-
await client.deleteCollection(collection);
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
const pointIds = (params.ids ?? []).filter((id) => typeof id === "string" || typeof id === "number");
|
|
230
|
-
if (pointIds.length === 0) {
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
await client.delete(collection, {
|
|
234
|
-
wait: true,
|
|
235
|
-
points: pointIds,
|
|
236
|
-
});
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
if (vectorStore.kind !== "LibSQLVectorStore") {
|
|
241
|
-
throw new Error(`Vector store kind ${vectorStore.kind} is not supported by the built-in runtime.`);
|
|
242
|
-
}
|
|
243
|
-
const embeddingModel = resolveCompiledEmbeddingModelRef(workspace, vectorStore.embeddingModelRef);
|
|
244
|
-
const embeddings = await resolveCompiledEmbeddingModel(embeddingModel, options.embeddingModelResolver);
|
|
245
|
-
const db = createClient({
|
|
246
|
-
url: await ensureFileUrlDirectory(vectorStore.url ?? "", dataRoot),
|
|
247
|
-
...(vectorStore.authToken ? { authToken: vectorStore.authToken } : {}),
|
|
248
|
-
});
|
|
249
|
-
const table = vectorStore.table ?? "vectors";
|
|
250
|
-
const column = vectorStore.column ?? "embedding";
|
|
251
|
-
const ensureSchemaForTexts = async (texts) => {
|
|
252
|
-
const seedText = texts.find((text) => text.trim().length > 0) ?? "seed";
|
|
253
|
-
const sample = await embeddings.embedQuery(seedText);
|
|
254
|
-
await ensureLibSqlSchema(db, table, column, sample.length);
|
|
255
|
-
};
|
|
256
|
-
return {
|
|
257
|
-
kind: vectorStore.kind,
|
|
258
|
-
embeddingModelRef: vectorStore.embeddingModelRef,
|
|
259
|
-
addDocuments: async (documents) => {
|
|
260
|
-
await ensureSchemaForTexts(documents.map((document) => document.pageContent));
|
|
261
|
-
const vectors = await embeddings.embedDocuments(documents.map((document) => document.pageContent));
|
|
262
|
-
return addLibSqlDocuments(db, table, column, documents, vectors);
|
|
263
|
-
},
|
|
264
|
-
similaritySearch: async (query, limit) => {
|
|
265
|
-
await ensureSchemaForTexts([query]);
|
|
266
|
-
const queryVector = await embeddings.embedQuery(query);
|
|
267
|
-
return similaritySearchLibSql(db, table, column, queryVector, limit);
|
|
268
|
-
},
|
|
269
|
-
delete: async (params) => {
|
|
270
|
-
await ensureSchemaForTexts(["seed"]);
|
|
271
|
-
await deleteLibSqlVectors(db, table, params);
|
|
272
|
-
},
|
|
273
|
-
};
|
|
274
|
-
}
|
|
8
|
+
`),await e.execute(`
|
|
9
|
+
CREATE INDEX IF NOT EXISTS ${u}
|
|
10
|
+
ON ${r}(libsql_vector_idx(${i}))
|
|
11
|
+
`)}function M(e){if(!e.every(t=>typeof t=="number"&&Number.isFinite(t)))throw new Error("Invalid vector: all elements must be finite numbers.");return`[${e.join(",")}]`}async function q(e,t,n,o,r){const i=b(t,"table name"),u=b(n,"column name"),f=o.map((l,a)=>({sql:`INSERT INTO ${i} (content, metadata, ${u}) VALUES (:content, :metadata, vector(:embedding)) RETURNING ${i}.rowid AS id`,args:{content:l.pageContent,metadata:JSON.stringify(l.metadata??{}),embedding:M(r[a]??[])}}));return f.length===0?[]:(await e.batch(f)).flatMap(l=>l.rows.map(a=>String(a.id)))}async function F(e,t,n,o,r){const i=b(t,"table name"),u=b(n,"column name"),f=b(`idx_${i}_${u}`,"index name");return(await e.execute({sql:`SELECT ${i}.rowid AS id, ${i}.content, ${i}.metadata, vector_distance_cos(${i}.${u}, vector(:queryVector)) AS distance
|
|
12
|
+
FROM vector_top_k('${f}', vector(:queryVector), CAST(:limit AS INTEGER)) AS top_k
|
|
13
|
+
JOIN ${i} ON top_k.rowid = ${i}.rowid`,args:{queryVector:M(o),limit:r}})).rows.map(l=>{const a=typeof l.metadata=="string"?JSON.parse(l.metadata):{},c=typeof a=="object"&&a&&!Array.isArray(a)?a:{};return{pageContent:typeof l.content=="string"?l.content:"",metadata:c,score:typeof l.distance=="number"?1-l.distance:void 0}})}async function v(e,t,n){const o=b(t,"table name");if(n.deleteAll){await e.execute(`DELETE FROM ${o}`);return}const r=n.ids??[];if(r.length===0)throw new Error('You must provide an "ids" parameter or a "deleteAll" parameter.');await e.batch(r.map(i=>({sql:`DELETE FROM ${o} WHERE rowid = :id`,args:{id:i}})))}function D(e){return new x({...e.url?{url:e.url}:{},...e.host?{host:e.host}:{},...typeof e.port=="number"?{port:e.port}:{},...e.authToken?{apiKey:e.authToken}:{},checkCompatibility:!1})}async function Q(e,t,n){(await e.getCollections()).collections.some(r=>r.name===t)||await e.createCollection(t,{vectors:{size:n,distance:"Cosine"}})}async function U(e,t,n){const{createLlamaIndexVectorStore:o}=await import("./llamaindex.js");return o(e,t,n)}function W(e,t){const n=t?_(t):"default",o=e.vectorStores.get(n);if(o)return I(o);if(!t&&e.vectorStores.size===1){const r=e.vectorStores.values().next().value;if(r)return I(r)}throw new Error(`Missing vector store ${t??"vector-store/default"}`)}async function Y(e,t,n={}){if(n.vectorStoreResolver)return n.vectorStoreResolver(t.id);const{dataRoot:o}=V(e.refs,e.workspaceRoot);if(t.kind==="LlamaIndexSimpleVectorStore"){const a=R(e,t.embeddingModelRef),c=await $(a,n.embeddingModelResolver);return U(o,t,c)}if(t.kind==="QdrantVectorStore"){const a=R(e,t.embeddingModelRef),c=await $(a,n.embeddingModelResolver),d=D(t),p=t.collection??"vectors",N=async m=>{const y=m.find(s=>s.trim().length>0)??"seed",g=await c.embedQuery(y);await Q(d,p,g.length)};return{kind:t.kind,embeddingModelRef:t.embeddingModelRef,addDocuments:async m=>{if(m.length===0)return[];await N(m.map(s=>s.pageContent));const y=await c.embedDocuments(m.map(s=>s.pageContent)),g=m.map((s,w)=>`${Date.now()}-${w}-${Math.random().toString(36).slice(2,10)}`);return await d.upsert(p,{wait:!0,points:m.map((s,w)=>({id:g[w]??w,vector:y[w]??[],payload:{pageContent:s.pageContent,...s.metadata?{metadata:s.metadata}:{}}}))}),g},similaritySearch:async(m,y)=>{if(!(await d.getCollections()).collections.some(E=>E.name===p))return[];const s=await c.embedQuery(m);return(await d.search(p,{vector:s,limit:y,with_payload:!0})).map(E=>{const h=E.payload??{};return{pageContent:typeof h.pageContent=="string"?h.pageContent:"",metadata:typeof h.metadata=="object"&&h.metadata&&!Array.isArray(h.metadata)?h.metadata:{},score:typeof E.score=="number"?E.score:void 0}})},delete:async m=>{if(!(await d.getCollections()).collections.some(s=>s.name===p))return;if(m.deleteAll){await d.deleteCollection(p);return}const g=(m.ids??[]).filter(s=>typeof s=="string"||typeof s=="number");g.length!==0&&await d.delete(p,{wait:!0,points:g})}}}if(t.kind!=="LibSQLVectorStore")throw new Error(`Vector store kind ${t.kind} is not supported by the built-in runtime.`);const r=R(e,t.embeddingModelRef),i=await $(r,n.embeddingModelResolver),u=L({url:await S(t.url??"",o),...t.authToken?{authToken:t.authToken}:{}}),f=t.table??"vectors",T=t.column??"embedding",l=async a=>{const c=a.find(p=>p.trim().length>0)??"seed",d=await i.embedQuery(c);await k(u,f,T,d.length)};return{kind:t.kind,embeddingModelRef:t.embeddingModelRef,addDocuments:async a=>{await l(a.map(d=>d.pageContent));const c=await i.embedDocuments(a.map(d=>d.pageContent));return q(u,f,T,a,c)},similaritySearch:async(a,c)=>{await l([a]);const d=await i.embedQuery(a);return F(u,f,T,d,c)},delete:async a=>{await l(["seed"]),await v(u,f,a)}}}export{Y as resolveCompiledVectorStore,W as resolveCompiledVectorStoreRef};
|