@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,472 +1 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { readdir } from "node:fs/promises";
|
|
4
|
-
import { pathToFileURL } from "node:url";
|
|
5
|
-
import { ensureResourceSources } from "../resource/resource.js";
|
|
6
|
-
import { ensureExternalResourceSource, isExternalSourceLocator, resolveResourcePackageRoot } from "../resource/sources.js";
|
|
7
|
-
import { loadWorkspaceObjects, readToolModuleItems, readYamlItems } from "./object-loader.js";
|
|
8
|
-
import { validateSkillMetadata } from "../runtime/skills/skill-metadata.js";
|
|
9
|
-
import { parseEmbeddingModelObject, parseMcpServerObject, parseModelObject, parseToolObject, parseVectorStoreObject, validateEmbeddingModelObject, validateMcpServerObject, validateModelObject, validateToolObject, validateVectorStoreObject, } from "./resource-compilers.js";
|
|
10
|
-
import { validateAgent, validateTopology } from "./validate.js";
|
|
11
|
-
import { compileBinding } from "./agent-binding-compiler.js";
|
|
12
|
-
import { resolveFrameworkContractValidationMode, validateFrameworkContracts, } from "./framework-contract-validation.js";
|
|
13
|
-
import { discoverSubagents, ensureDiscoverySources } from "./support/discovery.js";
|
|
14
|
-
import { collectAgentDiscoverySourceRefs, collectToolSourceRefs } from "./support/source-collectors.js";
|
|
15
|
-
import { getRoutingDefaultAgentId, getRuntimeSources, getRuntimeResources, getRuntimeStorageRoots, getToolModuleDiscoveryConfig, getRoutingRules, resolveRefId, } from "./support/workspace-ref-utils.js";
|
|
16
|
-
import { hydrateAgentMcpTools, hydrateResourceAndExternalTools } from "./tool-hydration.js";
|
|
17
|
-
import { traceStartupStage } from "../runtime/startup-tracing.js";
|
|
18
|
-
import { shouldSkipScanDirectory } from "../utils/fs.js";
|
|
19
|
-
import { ensurePackageSource, ensureRemoteSkillSource, isFileSourceUri, isHttpSourceUri, isNpmSourceUri, resolveFileSourcePath, } from "./support/source-protocols.js";
|
|
20
|
-
import { discoverToolModuleDefinitions } from "../tooling/module-loader.js";
|
|
21
|
-
function mergeObjectValues(base, override) {
|
|
22
|
-
if (override === undefined) {
|
|
23
|
-
return base;
|
|
24
|
-
}
|
|
25
|
-
if (typeof base === "object" &&
|
|
26
|
-
base &&
|
|
27
|
-
typeof override === "object" &&
|
|
28
|
-
override &&
|
|
29
|
-
!Array.isArray(base) &&
|
|
30
|
-
!Array.isArray(override)) {
|
|
31
|
-
const merged = { ...base };
|
|
32
|
-
for (const [key, value] of Object.entries(override)) {
|
|
33
|
-
merged[key] = key in merged ? mergeObjectValues(merged[key], value) : value;
|
|
34
|
-
}
|
|
35
|
-
return merged;
|
|
36
|
-
}
|
|
37
|
-
return override;
|
|
38
|
-
}
|
|
39
|
-
function collectParsedResources(refs) {
|
|
40
|
-
const embeddings = new Map();
|
|
41
|
-
const mcpServers = new Map();
|
|
42
|
-
const models = new Map();
|
|
43
|
-
const vectorStores = new Map();
|
|
44
|
-
const tools = new Map();
|
|
45
|
-
for (const [ref, object] of refs) {
|
|
46
|
-
if (typeof object !== "object" || !object || "executionMode" in object) {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
const typed = object;
|
|
50
|
-
if (ref.startsWith("embedding-model/")) {
|
|
51
|
-
embeddings.set(typed.id, parseEmbeddingModelObject(object));
|
|
52
|
-
}
|
|
53
|
-
else if (ref.startsWith("mcp/")) {
|
|
54
|
-
mcpServers.set(typed.id, parseMcpServerObject(object));
|
|
55
|
-
}
|
|
56
|
-
else if (ref.startsWith("vector-store/")) {
|
|
57
|
-
vectorStores.set(typed.id, parseVectorStoreObject(object));
|
|
58
|
-
}
|
|
59
|
-
else if (ref.startsWith("model/")) {
|
|
60
|
-
models.set(typed.id, parseModelObject(object));
|
|
61
|
-
}
|
|
62
|
-
else if (ref.startsWith("tool/")) {
|
|
63
|
-
tools.set(typed.id, parseToolObject(object));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return { embeddings, mcpServers, models, vectorStores, tools };
|
|
67
|
-
}
|
|
68
|
-
function validateWorkspaceResources(embeddings, mcpServers, models, vectorStores, tools, agents) {
|
|
69
|
-
embeddings.forEach((embedding) => validateEmbeddingModelObject(embedding));
|
|
70
|
-
mcpServers.forEach((server) => validateMcpServerObject(server));
|
|
71
|
-
models.forEach((model) => validateModelObject(model, models));
|
|
72
|
-
vectorStores.forEach((vectorStore) => validateVectorStoreObject(vectorStore));
|
|
73
|
-
tools.forEach((tool) => validateToolObject(tool, tools));
|
|
74
|
-
tools.forEach((tool) => {
|
|
75
|
-
if (!tool.embeddingModelRef) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const embeddingModelId = tool.embeddingModelRef.startsWith("embedding-model/")
|
|
79
|
-
? tool.embeddingModelRef.slice("embedding-model/".length)
|
|
80
|
-
: tool.embeddingModelRef;
|
|
81
|
-
if (!embeddings.has(embeddingModelId)) {
|
|
82
|
-
throw new Error(`Tool ${tool.id} references missing embedding model ${tool.embeddingModelRef}`);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
agents.forEach(validateAgent);
|
|
86
|
-
validateTopology(agents);
|
|
87
|
-
tools.forEach((tool) => {
|
|
88
|
-
if (tool.type !== "mcp") {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const mcpConfig = typeof tool.config?.mcp === "object" && tool.config.mcp
|
|
92
|
-
? tool.config.mcp
|
|
93
|
-
: undefined;
|
|
94
|
-
const serverRef = typeof mcpConfig?.serverRef === "string" ? mcpConfig.serverRef : undefined;
|
|
95
|
-
if (!serverRef) {
|
|
96
|
-
throw new Error(`MCP tool ${tool.id} must define mcp.server or mcp.serverRef`);
|
|
97
|
-
}
|
|
98
|
-
const serverId = serverRef.startsWith("mcp/") ? serverRef.slice(4) : serverRef;
|
|
99
|
-
if (!mcpServers.has(serverId)) {
|
|
100
|
-
throw new Error(`MCP tool ${tool.id} references missing MCP server ${serverRef}`);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
function compileBindings(workspaceRoot, refs, agentsList, models, tools) {
|
|
105
|
-
const agents = new Map(agentsList.map((agent) => [agent.id, agent]));
|
|
106
|
-
const referencedSubagentIds = new Set(agentsList.flatMap((agent) => agent.subagentRefs.map((ref) => resolveRefId(ref))));
|
|
107
|
-
const bindings = new Map();
|
|
108
|
-
for (const agent of agentsList) {
|
|
109
|
-
bindings.set(agent.id, compileBinding(workspaceRoot, agent, agents, referencedSubagentIds, refs, models, tools));
|
|
110
|
-
}
|
|
111
|
-
return bindings;
|
|
112
|
-
}
|
|
113
|
-
function validateRoutingTargets(refs, agentsList) {
|
|
114
|
-
const runtimeEntryAgentIds = new Set(agentsList.map((agent) => agent.id));
|
|
115
|
-
const defaultAgentId = getRoutingDefaultAgentId(refs);
|
|
116
|
-
if (defaultAgentId && !runtimeEntryAgentIds.has(defaultAgentId)) {
|
|
117
|
-
throw new Error(`Runtime routing.defaultAgentId references unknown runtime entry agent ${defaultAgentId}`);
|
|
118
|
-
}
|
|
119
|
-
for (const rule of getRoutingRules(refs)) {
|
|
120
|
-
if (!runtimeEntryAgentIds.has(rule.agentId)) {
|
|
121
|
-
throw new Error(`Runtime routing.rules references unknown runtime entry agent ${rule.agentId}`);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function resolveLocalResourceRoot(candidate, workspaceRoot) {
|
|
126
|
-
const resolved = path.resolve(workspaceRoot, candidate);
|
|
127
|
-
const directPackageJsonPath = path.join(resolved, "package.json");
|
|
128
|
-
const resourceRoot = existsSync(directPackageJsonPath) ? resolved : resolveResourcePackageRoot(resolved);
|
|
129
|
-
if (!resourceRoot) {
|
|
130
|
-
throw new Error(`Workspace resource ${candidate} is missing package.json or resources/package.json.`);
|
|
131
|
-
}
|
|
132
|
-
return resourceRoot;
|
|
133
|
-
}
|
|
134
|
-
async function resolveConfiguredResources(entries, workspaceRoot) {
|
|
135
|
-
const resolved = [];
|
|
136
|
-
for (const entry of entries) {
|
|
137
|
-
const key = entry.trim();
|
|
138
|
-
if (!key) {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
const root = isExternalSourceLocator(key)
|
|
142
|
-
? await ensureExternalResourceSource(key, workspaceRoot)
|
|
143
|
-
: resolveLocalResourceRoot(key, workspaceRoot);
|
|
144
|
-
resolved.push({ key, root });
|
|
145
|
-
}
|
|
146
|
-
return resolved;
|
|
147
|
-
}
|
|
148
|
-
async function registerAttachedResourceTools(tools, resourceRoot, toolModuleDiscoveryScope) {
|
|
149
|
-
const toolsRoot = path.join(resourceRoot, "tools");
|
|
150
|
-
for (const { item, sourcePath } of await readYamlItems(toolsRoot, undefined, { recursive: true })) {
|
|
151
|
-
const parsed = parseToolObject({
|
|
152
|
-
id: typeof item.id === "string" ? item.id : path.basename(sourcePath).replace(/\.(yaml|yml|json)$/i, ""),
|
|
153
|
-
kind: "tool",
|
|
154
|
-
sourcePath,
|
|
155
|
-
value: item,
|
|
156
|
-
});
|
|
157
|
-
tools.set(parsed.id, mergeParsedToolObject(tools.get(parsed.id), parsed));
|
|
158
|
-
}
|
|
159
|
-
for (const { item, sourcePath } of await readToolModuleItems(toolsRoot, { scope: toolModuleDiscoveryScope })) {
|
|
160
|
-
const parsed = parseToolObject({
|
|
161
|
-
id: String(item.id),
|
|
162
|
-
kind: "tool",
|
|
163
|
-
sourcePath,
|
|
164
|
-
value: item,
|
|
165
|
-
});
|
|
166
|
-
tools.set(parsed.id, mergeParsedToolObject(tools.get(parsed.id), parsed));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
async function collectSkillRoots(root) {
|
|
170
|
-
try {
|
|
171
|
-
const entries = await readdir(root, { withFileTypes: true });
|
|
172
|
-
const skillDocument = entries.find((entry) => entry.isFile() && entry.name === "SKILL.md");
|
|
173
|
-
if (skillDocument) {
|
|
174
|
-
return [root];
|
|
175
|
-
}
|
|
176
|
-
const nested = await Promise.all(entries
|
|
177
|
-
.filter((entry) => entry.isDirectory() && !shouldSkipScanDirectory(entry.name))
|
|
178
|
-
.map((entry) => collectSkillRoots(path.join(root, entry.name))));
|
|
179
|
-
return nested.flat();
|
|
180
|
-
}
|
|
181
|
-
catch {
|
|
182
|
-
return [];
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
async function resolveConfiguredSkillSourceRoots(sources, workspaceRoot) {
|
|
186
|
-
const resolved = [];
|
|
187
|
-
for (const source of sources) {
|
|
188
|
-
if (isFileSourceUri(source)) {
|
|
189
|
-
const resolvedPath = resolveFileSourcePath(source, workspaceRoot);
|
|
190
|
-
resolved.push(resolvedPath.endsWith(`${path.sep}SKILL.md`) || resolvedPath.endsWith("/SKILL.md")
|
|
191
|
-
? path.dirname(resolvedPath)
|
|
192
|
-
: resolvedPath);
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
if (isHttpSourceUri(source)) {
|
|
196
|
-
resolved.push(await ensureRemoteSkillSource(source));
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
if (isNpmSourceUri(source)) {
|
|
200
|
-
const installed = await ensurePackageSource(source, workspaceRoot);
|
|
201
|
-
resolved.push(path.join(installed.packageRoot, "skills"));
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
throw new Error(`Unsupported skill source ${source}. Use file://, npm://, or https://.`);
|
|
205
|
-
}
|
|
206
|
-
return resolved;
|
|
207
|
-
}
|
|
208
|
-
async function registerToolFolderSource(tools, folderRoot, toolModuleDiscoveryScope) {
|
|
209
|
-
for (const { item, sourcePath } of await readYamlItems(folderRoot, undefined, { recursive: true })) {
|
|
210
|
-
const parsed = parseToolObject({
|
|
211
|
-
id: typeof item.id === "string" ? item.id : path.basename(sourcePath).replace(/\.(yaml|yml|json)$/i, ""),
|
|
212
|
-
kind: "tool",
|
|
213
|
-
sourcePath,
|
|
214
|
-
value: item,
|
|
215
|
-
});
|
|
216
|
-
tools.set(parsed.id, mergeParsedToolObject(tools.get(parsed.id), parsed));
|
|
217
|
-
}
|
|
218
|
-
for (const { item, sourcePath } of await readToolModuleItems(folderRoot, { scope: toolModuleDiscoveryScope })) {
|
|
219
|
-
const parsed = parseToolObject({
|
|
220
|
-
id: String(item.id),
|
|
221
|
-
kind: "tool",
|
|
222
|
-
sourcePath,
|
|
223
|
-
value: item,
|
|
224
|
-
});
|
|
225
|
-
tools.set(parsed.id, mergeParsedToolObject(tools.get(parsed.id), parsed));
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
async function registerToolPackageSource(tools, source, workspaceRoot, toolModuleDiscoveryScope) {
|
|
229
|
-
const installed = await ensurePackageSource(source, workspaceRoot);
|
|
230
|
-
const toolsRoot = path.join(installed.packageRoot, "tools");
|
|
231
|
-
if (existsSync(toolsRoot)) {
|
|
232
|
-
await registerToolFolderSource(tools, toolsRoot, toolModuleDiscoveryScope);
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
if (!installed.entryPath) {
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
const imported = await import(pathToFileURL(installed.entryPath).href);
|
|
239
|
-
const definitions = discoverToolModuleDefinitions("", imported);
|
|
240
|
-
for (const definition of definitions) {
|
|
241
|
-
const parsed = parseToolObject({
|
|
242
|
-
id: definition.implementationName,
|
|
243
|
-
kind: "tool",
|
|
244
|
-
sourcePath: installed.entryPath,
|
|
245
|
-
value: {
|
|
246
|
-
kind: "tool",
|
|
247
|
-
id: definition.implementationName,
|
|
248
|
-
type: "function",
|
|
249
|
-
name: definition.implementationName,
|
|
250
|
-
description: definition.description,
|
|
251
|
-
implementationName: definition.implementationName,
|
|
252
|
-
hasModuleSchema: definition.hasModuleSchema,
|
|
253
|
-
...(definition.modelSchema ? { modelSchema: definition.modelSchema } : {}),
|
|
254
|
-
...(definition.retryable !== undefined ? { retryable: definition.retryable } : {}),
|
|
255
|
-
...(definition.memory ? { config: { memory: definition.memory } } : {}),
|
|
256
|
-
},
|
|
257
|
-
});
|
|
258
|
-
tools.set(parsed.id, mergeParsedToolObject(tools.get(parsed.id), parsed));
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
function mergeParsedToolObject(current, incoming) {
|
|
262
|
-
if (!current) {
|
|
263
|
-
return incoming;
|
|
264
|
-
}
|
|
265
|
-
return {
|
|
266
|
-
...current,
|
|
267
|
-
...incoming,
|
|
268
|
-
config: mergeObjectValues(current.config, incoming.config),
|
|
269
|
-
subprocess: incoming.subprocess ?? current.subprocess,
|
|
270
|
-
inputSchemaRef: incoming.inputSchemaRef ?? current.inputSchemaRef,
|
|
271
|
-
hasModuleSchema: incoming.hasModuleSchema ?? current.hasModuleSchema,
|
|
272
|
-
modelSchema: incoming.modelSchema ?? current.modelSchema,
|
|
273
|
-
embeddingModelRef: incoming.embeddingModelRef ?? current.embeddingModelRef,
|
|
274
|
-
backendOperation: incoming.backendOperation ?? current.backendOperation,
|
|
275
|
-
mcpRef: incoming.mcpRef ?? current.mcpRef,
|
|
276
|
-
bundleRefs: incoming.bundleRefs.length > 0 ? incoming.bundleRefs : current.bundleRefs,
|
|
277
|
-
hitl: incoming.hitl ?? current.hitl,
|
|
278
|
-
retryable: incoming.retryable ?? current.retryable,
|
|
279
|
-
sourcePath: incoming.sourcePath,
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
async function registerWorkspaceSkillRegistry(skillCollectionRoots) {
|
|
283
|
-
const registry = new Map();
|
|
284
|
-
for (const skillsRoot of skillCollectionRoots) {
|
|
285
|
-
for (const skillRoot of await collectSkillRoots(skillsRoot)) {
|
|
286
|
-
const metadata = validateSkillMetadata(skillRoot);
|
|
287
|
-
const existing = registry.get(metadata.name);
|
|
288
|
-
if (existing && existing !== skillRoot) {
|
|
289
|
-
throw new Error(`Duplicate skill name ${metadata.name} found in ${existing} and ${skillRoot}`);
|
|
290
|
-
}
|
|
291
|
-
registry.set(metadata.name, skillRoot);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
return registry;
|
|
295
|
-
}
|
|
296
|
-
function validateToolNameConflicts(tools) {
|
|
297
|
-
const seen = new Map();
|
|
298
|
-
for (const tool of tools.values()) {
|
|
299
|
-
const existing = seen.get(tool.name);
|
|
300
|
-
if (existing && existing !== tool.sourcePath) {
|
|
301
|
-
throw new Error(`Duplicate tool name ${tool.name} found in ${existing} and ${tool.sourcePath}`);
|
|
302
|
-
}
|
|
303
|
-
seen.set(tool.name, tool.sourcePath);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
function resolveAgentSkillNames(agents, skillRegistry, skillCollectionRoots) {
|
|
307
|
-
for (const agent of agents) {
|
|
308
|
-
agent.skillPathRefs = agent.skillPathRefs.map((entry) => {
|
|
309
|
-
const normalizedEntry = entry.startsWith("skill/") ? entry.slice("skill/".length) : entry;
|
|
310
|
-
const registered = skillRegistry.get(normalizedEntry);
|
|
311
|
-
if (registered) {
|
|
312
|
-
return registered;
|
|
313
|
-
}
|
|
314
|
-
if (path.isAbsolute(entry) || entry.startsWith("builtin://") || entry.startsWith("resource://")) {
|
|
315
|
-
return entry;
|
|
316
|
-
}
|
|
317
|
-
for (const skillsRoot of skillCollectionRoots) {
|
|
318
|
-
const candidate = path.join(skillsRoot, normalizedEntry);
|
|
319
|
-
if (existsSync(path.join(candidate, "SKILL.md"))) {
|
|
320
|
-
return candidate;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return normalizedEntry;
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
export async function loadWorkspace(workspaceRoot, options = {}) {
|
|
328
|
-
const loaded = await traceStartupStage("workspace.load.objects", () => loadWorkspaceObjects(workspaceRoot, options), {
|
|
329
|
-
workspaceRoot,
|
|
330
|
-
});
|
|
331
|
-
const runtimeStorage = getRuntimeStorageRoots(loaded.refs, workspaceRoot);
|
|
332
|
-
if (path.resolve(runtimeStorage.applicationRoot) !== path.resolve(workspaceRoot)) {
|
|
333
|
-
throw new Error(`Runtime applicationRoot ${runtimeStorage.applicationRoot} must resolve to the loaded workspace root ${workspaceRoot}. ` +
|
|
334
|
-
"Load the shared application folder as the workspace root and use Runtime.dataRoot for writable profile data.");
|
|
335
|
-
}
|
|
336
|
-
loaded.agents = await traceStartupStage("workspace.discover.subagents", () => discoverSubagents(loaded.agents, workspaceRoot), {
|
|
337
|
-
workspaceRoot,
|
|
338
|
-
});
|
|
339
|
-
const discoverySourceRefs = collectAgentDiscoverySourceRefs(loaded.agents);
|
|
340
|
-
await traceStartupStage("workspace.ensure.discoverySources", () => ensureDiscoverySources(discoverySourceRefs, workspaceRoot), {
|
|
341
|
-
workspaceRoot,
|
|
342
|
-
sourceCount: discoverySourceRefs.length,
|
|
343
|
-
});
|
|
344
|
-
for (const agent of loaded.agents) {
|
|
345
|
-
loaded.refs.set(`agent/${agent.id}`, agent);
|
|
346
|
-
}
|
|
347
|
-
const { embeddings, mcpServers, models, vectorStores, tools } = collectParsedResources(loaded.refs);
|
|
348
|
-
const toolModuleDiscoveryConfig = getToolModuleDiscoveryConfig(loaded.refs);
|
|
349
|
-
const runtimeSources = getRuntimeSources(loaded.refs);
|
|
350
|
-
await traceStartupStage("workspace.hydrate.agentMcpTools", () => hydrateAgentMcpTools(loaded.agents, mcpServers, tools), {
|
|
351
|
-
workspaceRoot,
|
|
352
|
-
agentCount: loaded.agents.length,
|
|
353
|
-
});
|
|
354
|
-
for (const source of runtimeSources.tools) {
|
|
355
|
-
if (isFileSourceUri(source)) {
|
|
356
|
-
const folderRoot = resolveFileSourcePath(source, workspaceRoot);
|
|
357
|
-
await traceStartupStage("workspace.register.toolFolderSource", () => registerToolFolderSource(tools, folderRoot, toolModuleDiscoveryConfig.scope), {
|
|
358
|
-
workspaceRoot,
|
|
359
|
-
source,
|
|
360
|
-
});
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
if (isNpmSourceUri(source)) {
|
|
364
|
-
await traceStartupStage("workspace.register.toolPackageSource", () => registerToolPackageSource(tools, source, workspaceRoot, toolModuleDiscoveryConfig.scope), {
|
|
365
|
-
workspaceRoot,
|
|
366
|
-
source,
|
|
367
|
-
});
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
370
|
-
throw new Error(`Unsupported tool source ${source}. Use file:// or npm://.`);
|
|
371
|
-
}
|
|
372
|
-
const configuredResources = Array.from(new Set([
|
|
373
|
-
...getRuntimeResources(loaded.refs),
|
|
374
|
-
...(options.resources ?? []),
|
|
375
|
-
]));
|
|
376
|
-
const resolvedConfiguredResources = await traceStartupStage("workspace.resolve.configuredResources", () => resolveConfiguredResources(configuredResources, workspaceRoot), {
|
|
377
|
-
workspaceRoot,
|
|
378
|
-
configuredResourceCount: configuredResources.length,
|
|
379
|
-
});
|
|
380
|
-
for (const resource of resolvedConfiguredResources) {
|
|
381
|
-
await traceStartupStage("workspace.register.attachedResourceTools", () => registerAttachedResourceTools(tools, resource.root, toolModuleDiscoveryConfig.scope), {
|
|
382
|
-
workspaceRoot,
|
|
383
|
-
resourceRoot: resource.root,
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
const localResourceRoot = resolveResourcePackageRoot(workspaceRoot);
|
|
387
|
-
const configuredSkillSourceRoots = await traceStartupStage("workspace.resolve.skillSources", () => resolveConfiguredSkillSourceRoots(runtimeSources.skills, workspaceRoot), {
|
|
388
|
-
workspaceRoot,
|
|
389
|
-
skillSourceCount: runtimeSources.skills.length,
|
|
390
|
-
});
|
|
391
|
-
const skillCollectionRoots = Array.from(new Set([
|
|
392
|
-
path.join(workspaceRoot, "modules", "skills"),
|
|
393
|
-
...(localResourceRoot ? [path.join(localResourceRoot, "skills")] : []),
|
|
394
|
-
...configuredSkillSourceRoots,
|
|
395
|
-
...resolvedConfiguredResources.map((resource) => path.join(resource.root, "skills")),
|
|
396
|
-
]));
|
|
397
|
-
const skillRegistry = await traceStartupStage("workspace.register.skillRegistry", () => registerWorkspaceSkillRegistry(skillCollectionRoots), {
|
|
398
|
-
workspaceRoot,
|
|
399
|
-
skillCollectionRootCount: skillCollectionRoots.length,
|
|
400
|
-
});
|
|
401
|
-
resolveAgentSkillNames(loaded.agents, skillRegistry, skillCollectionRoots);
|
|
402
|
-
const collectedResources = collectToolSourceRefs(tools, loaded.agents, {
|
|
403
|
-
...options,
|
|
404
|
-
resources: configuredResources,
|
|
405
|
-
});
|
|
406
|
-
const externalResources = collectedResources.filter((resource) => isExternalSourceLocator(resource));
|
|
407
|
-
await traceStartupStage("workspace.ensure.externalResourceSources", () => ensureResourceSources(externalResources, workspaceRoot), {
|
|
408
|
-
workspaceRoot,
|
|
409
|
-
externalResourceCount: externalResources.length,
|
|
410
|
-
});
|
|
411
|
-
await traceStartupStage("workspace.hydrate.externalTools", () => hydrateResourceAndExternalTools(tools, externalResources, workspaceRoot, toolModuleDiscoveryConfig.scope), {
|
|
412
|
-
workspaceRoot,
|
|
413
|
-
externalResourceCount: externalResources.length,
|
|
414
|
-
});
|
|
415
|
-
validateToolNameConflicts(tools);
|
|
416
|
-
const frameworkContractValidation = resolveFrameworkContractValidationMode(options.frameworkContractValidation);
|
|
417
|
-
const contractOwnedRoots = Array.from(new Set([
|
|
418
|
-
workspaceRoot,
|
|
419
|
-
...(localResourceRoot ? [localResourceRoot] : []),
|
|
420
|
-
...resolvedConfiguredResources.map((resource) => resource.root),
|
|
421
|
-
]));
|
|
422
|
-
await traceStartupStage("workspace.validate.frameworkContracts", async () => {
|
|
423
|
-
validateFrameworkContracts({
|
|
424
|
-
agents: loaded.agents,
|
|
425
|
-
tools,
|
|
426
|
-
skillRegistry,
|
|
427
|
-
ownedRoots: contractOwnedRoots,
|
|
428
|
-
refs: loaded.refs,
|
|
429
|
-
mode: frameworkContractValidation,
|
|
430
|
-
});
|
|
431
|
-
}, {
|
|
432
|
-
workspaceRoot,
|
|
433
|
-
mode: frameworkContractValidation,
|
|
434
|
-
});
|
|
435
|
-
const resources = Array.from(new Set([
|
|
436
|
-
...(localResourceRoot ? [localResourceRoot] : []),
|
|
437
|
-
...runtimeSources.tools.filter((source) => isNpmSourceUri(source)),
|
|
438
|
-
...runtimeSources.skills.filter((source) => isNpmSourceUri(source)),
|
|
439
|
-
...collectedResources,
|
|
440
|
-
]));
|
|
441
|
-
await traceStartupStage("workspace.validate.resources", async () => {
|
|
442
|
-
validateWorkspaceResources(embeddings, mcpServers, models, vectorStores, tools, loaded.agents);
|
|
443
|
-
}, {
|
|
444
|
-
workspaceRoot,
|
|
445
|
-
agentCount: loaded.agents.length,
|
|
446
|
-
toolCount: tools.size,
|
|
447
|
-
modelCount: models.size,
|
|
448
|
-
mcpServerCount: mcpServers.size,
|
|
449
|
-
});
|
|
450
|
-
await traceStartupStage("workspace.validate.routingTargets", async () => {
|
|
451
|
-
validateRoutingTargets(loaded.refs, loaded.agents);
|
|
452
|
-
}, {
|
|
453
|
-
workspaceRoot,
|
|
454
|
-
agentCount: loaded.agents.length,
|
|
455
|
-
});
|
|
456
|
-
const bindings = await traceStartupStage("workspace.compile.bindings", async () => compileBindings(workspaceRoot, loaded.refs, loaded.agents, models, tools), {
|
|
457
|
-
workspaceRoot,
|
|
458
|
-
agentCount: loaded.agents.length,
|
|
459
|
-
});
|
|
460
|
-
return {
|
|
461
|
-
workspaceRoot,
|
|
462
|
-
resources,
|
|
463
|
-
refs: loaded.refs,
|
|
464
|
-
embeddings,
|
|
465
|
-
mcpServers,
|
|
466
|
-
models,
|
|
467
|
-
vectorStores,
|
|
468
|
-
tools,
|
|
469
|
-
agents: new Map(loaded.agents.map((agent) => [agent.id, agent])),
|
|
470
|
-
bindings,
|
|
471
|
-
};
|
|
472
|
-
}
|
|
1
|
+
import u from"node:path";import{existsSync as k}from"node:fs";import{readdir as z}from"node:fs/promises";import{pathToFileURL as K}from"node:url";import{ensureResourceSources as B}from"../resource/resource.js";import{ensureExternalResourceSource as H,isExternalSourceLocator as P,resolveResourcePackageRoot as E}from"../resource/sources.js";import{loadWorkspaceObjects as J,readToolModuleItems as T,readYamlItems as $}from"./object-loader.js";import{validateSkillMetadata as Y}from"../runtime/skills/skill-metadata.js";import{parseEmbeddingModelObject as q,parseMcpServerObject as G,parseModelObject as Q,parseToolObject as g,parseVectorStoreObject as X,validateEmbeddingModelObject as Z,validateMcpServerObject as _,validateModelObject as ee,validateToolObject as te,validateVectorStoreObject as oe}from"./resource-compilers.js";import{validateAgent as re,validateTopology as se}from"./validate.js";import{compileBinding as ne}from"./agent-binding-compiler.js";import{resolveFrameworkContractValidationMode as ie,validateFrameworkContracts as ae}from"./framework-contract-validation.js";import{discoverSubagents as ce,ensureDiscoverySources as le}from"./support/discovery.js";import{collectAgentDiscoverySourceRefs as de,collectToolSourceRefs as ue}from"./support/source-collectors.js";import{getRoutingDefaultAgentId as fe,getRuntimeSources as me,getRuntimeResources as ge,getRuntimeStorageRoots as pe,getToolModuleDiscoveryConfig as he,getRoutingRules as Se,resolveRefId as ye}from"./support/workspace-ref-utils.js";import{hydrateAgentMcpTools as we,hydrateResourceAndExternalTools as ve}from"./tool-hydration.js";import{traceStartupStage as f}from"../runtime/startup-tracing.js";import{shouldSkipScanDirectory as be}from"../utils/fs.js";import{ensurePackageSource as A,ensureRemoteSkillSource as Re,isFileSourceUri as O,isHttpSourceUri as ke,isNpmSourceUri as w,resolveFileSourcePath as W}from"./support/source-protocols.js";import{discoverToolModuleDefinitions as Me}from"../tooling/module-loader.js";function I(e,r){if(r===void 0)return e;if(typeof e=="object"&&e&&typeof r=="object"&&r&&!Array.isArray(e)&&!Array.isArray(r)){const t={...e};for(const[s,o]of Object.entries(r))t[s]=s in t?I(t[s],o):o;return t}return r}function je(e){const r=new Map,t=new Map,s=new Map,o=new Map,a=new Map;for(const[n,c]of e){if(typeof c!="object"||!c||"executionMode"in c)continue;const i=c;n.startsWith("embedding-model/")?r.set(i.id,q(c)):n.startsWith("mcp/")?t.set(i.id,G(c)):n.startsWith("vector-store/")?o.set(i.id,X(c)):n.startsWith("model/")?s.set(i.id,Q(c)):n.startsWith("tool/")&&a.set(i.id,g(c))}return{embeddings:r,mcpServers:t,models:s,vectorStores:o,tools:a}}function Ce(e,r,t,s,o,a){e.forEach(n=>Z(n)),r.forEach(n=>_(n)),t.forEach(n=>ee(n,t)),s.forEach(n=>oe(n)),o.forEach(n=>te(n,o)),o.forEach(n=>{if(!n.embeddingModelRef)return;const c=n.embeddingModelRef.startsWith("embedding-model/")?n.embeddingModelRef.slice(16):n.embeddingModelRef;if(!e.has(c))throw new Error(`Tool ${n.id} references missing embedding model ${n.embeddingModelRef}`)}),a.forEach(re),se(a),o.forEach(n=>{if(n.type!=="mcp")return;const c=typeof n.config?.mcp=="object"&&n.config.mcp?n.config.mcp:void 0,i=typeof c?.serverRef=="string"?c.serverRef:void 0;if(!i)throw new Error(`MCP tool ${n.id} must define mcp.server or mcp.serverRef`);const d=i.startsWith("mcp/")?i.slice(4):i;if(!r.has(d))throw new Error(`MCP tool ${n.id} references missing MCP server ${i}`)})}function Pe(e,r,t,s,o){const a=new Map(t.map(i=>[i.id,i])),n=new Set(t.flatMap(i=>i.subagentRefs.map(d=>ye(d)))),c=new Map;for(const i of t)c.set(i.id,ne(e,i,a,n,r,s,o));return c}function Ee(e,r){const t=new Set(r.map(o=>o.id)),s=fe(e);if(s&&!t.has(s))throw new Error(`Runtime routing.defaultAgentId references unknown runtime entry agent ${s}`);for(const o of Se(e))if(!t.has(o.agentId))throw new Error(`Runtime routing.rules references unknown runtime entry agent ${o.agentId}`)}function Te(e,r){const t=u.resolve(r,e),s=u.join(t,"package.json"),o=k(s)?t:E(t);if(!o)throw new Error(`Workspace resource ${e} is missing package.json or resources/package.json.`);return o}async function $e(e,r){const t=[];for(const s of e){const o=s.trim();if(!o)continue;const a=P(o)?await H(o,r):Te(o,r);t.push({key:o,root:a})}return t}async function Ae(e,r,t){const s=u.join(r,"tools");for(const{item:o,sourcePath:a}of await $(s,void 0,{recursive:!0})){const n=g({id:typeof o.id=="string"?o.id:u.basename(a).replace(/\.(yaml|yml|json)$/i,""),kind:"tool",sourcePath:a,value:o});e.set(n.id,h(e.get(n.id),n))}for(const{item:o,sourcePath:a}of await T(s,{scope:t})){const n=g({id:String(o.id),kind:"tool",sourcePath:a,value:o});e.set(n.id,h(e.get(n.id),n))}}async function x(e){try{const r=await z(e,{withFileTypes:!0});return r.find(o=>o.isFile()&&o.name==="SKILL.md")?[e]:(await Promise.all(r.filter(o=>o.isDirectory()&&!be(o.name)).map(o=>x(u.join(e,o.name))))).flat()}catch{return[]}}async function Oe(e,r){const t=[];for(const s of e){if(O(s)){const o=W(s,r);t.push(o.endsWith(`${u.sep}SKILL.md`)||o.endsWith("/SKILL.md")?u.dirname(o):o);continue}if(ke(s)){t.push(await Re(s));continue}if(w(s)){const o=await A(s,r);t.push(u.join(o.packageRoot,"skills"));continue}throw new Error(`Unsupported skill source ${s}. Use file://, npm://, or https://.`)}return t}async function D(e,r,t){for(const{item:s,sourcePath:o}of await $(r,void 0,{recursive:!0})){const a=g({id:typeof s.id=="string"?s.id:u.basename(o).replace(/\.(yaml|yml|json)$/i,""),kind:"tool",sourcePath:o,value:s});e.set(a.id,h(e.get(a.id),a))}for(const{item:s,sourcePath:o}of await T(r,{scope:t})){const a=g({id:String(s.id),kind:"tool",sourcePath:o,value:s});e.set(a.id,h(e.get(a.id),a))}}async function We(e,r,t,s){const o=await A(r,t),a=u.join(o.packageRoot,"tools");if(k(a)){await D(e,a,s);return}if(!o.entryPath)return;const n=await import(K(o.entryPath).href),c=Me("",n);for(const i of c){const d=g({id:i.implementationName,kind:"tool",sourcePath:o.entryPath,value:{kind:"tool",id:i.implementationName,type:"function",name:i.implementationName,description:i.description,implementationName:i.implementationName,hasModuleSchema:i.hasModuleSchema,...i.modelSchema?{modelSchema:i.modelSchema}:{},...i.retryable!==void 0?{retryable:i.retryable}:{},...i.memory?{config:{memory:i.memory}}:{}}});e.set(d.id,h(e.get(d.id),d))}}function h(e,r){return e?{...e,...r,config:I(e.config,r.config),subprocess:r.subprocess??e.subprocess,inputSchemaRef:r.inputSchemaRef??e.inputSchemaRef,hasModuleSchema:r.hasModuleSchema??e.hasModuleSchema,modelSchema:r.modelSchema??e.modelSchema,embeddingModelRef:r.embeddingModelRef??e.embeddingModelRef,backendOperation:r.backendOperation??e.backendOperation,mcpRef:r.mcpRef??e.mcpRef,bundleRefs:r.bundleRefs.length>0?r.bundleRefs:e.bundleRefs,hitl:r.hitl??e.hitl,retryable:r.retryable??e.retryable,sourcePath:r.sourcePath}:r}async function Ie(e){const r=new Map;for(const t of e)for(const s of await x(t)){const o=Y(s),a=r.get(o.name);if(a&&a!==s)throw new Error(`Duplicate skill name ${o.name} found in ${a} and ${s}`);r.set(o.name,s)}return r}function xe(e){const r=new Map;for(const t of e.values()){const s=r.get(t.name);if(s&&s!==t.sourcePath)throw new Error(`Duplicate tool name ${t.name} found in ${s} and ${t.sourcePath}`);r.set(t.name,t.sourcePath)}}function De(e,r,t){for(const s of e)s.skillPathRefs=s.skillPathRefs.map(o=>{const a=o.startsWith("skill/")?o.slice(6):o,n=r.get(a);if(n)return n;if(u.isAbsolute(o)||o.startsWith("builtin://")||o.startsWith("resource://"))return o;for(const c of t){const i=u.join(c,a);if(k(u.join(i,"SKILL.md")))return i}return a})}async function rt(e,r={}){const t=await f("workspace.load.objects",()=>J(e,r),{workspaceRoot:e}),s=pe(t.refs,e);if(u.resolve(s.applicationRoot)!==u.resolve(e))throw new Error(`Runtime applicationRoot ${s.applicationRoot} must resolve to the loaded workspace root ${e}. Load the shared application folder as the workspace root and use Runtime.dataRoot for writable profile data.`);t.agents=await f("workspace.discover.subagents",()=>ce(t.agents,e),{workspaceRoot:e});const o=de(t.agents);await f("workspace.ensure.discoverySources",()=>le(o,e),{workspaceRoot:e,sourceCount:o.length});for(const l of t.agents)t.refs.set(`agent/${l.id}`,l);const{embeddings:a,mcpServers:n,models:c,vectorStores:i,tools:d}=je(t.refs),S=he(t.refs),p=me(t.refs);await f("workspace.hydrate.agentMcpTools",()=>we(t.agents,n,d),{workspaceRoot:e,agentCount:t.agents.length});for(const l of p.tools){if(O(l)){const V=W(l,e);await f("workspace.register.toolFolderSource",()=>D(d,V,S.scope),{workspaceRoot:e,source:l});continue}if(w(l)){await f("workspace.register.toolPackageSource",()=>We(d,l,e,S.scope),{workspaceRoot:e,source:l});continue}throw new Error(`Unsupported tool source ${l}. Use file:// or npm://.`)}const v=Array.from(new Set([...ge(t.refs),...r.resources??[]])),b=await f("workspace.resolve.configuredResources",()=>$e(v,e),{workspaceRoot:e,configuredResourceCount:v.length});for(const l of b)await f("workspace.register.attachedResourceTools",()=>Ae(d,l.root,S.scope),{workspaceRoot:e,resourceRoot:l.root});const m=E(e),L=await f("workspace.resolve.skillSources",()=>Oe(p.skills,e),{workspaceRoot:e,skillSourceCount:p.skills.length}),R=Array.from(new Set([u.join(e,"modules","skills"),...m?[u.join(m,"skills")]:[],...L,...b.map(l=>u.join(l.root,"skills"))])),M=await f("workspace.register.skillRegistry",()=>Ie(R),{workspaceRoot:e,skillCollectionRootCount:R.length});De(t.agents,M,R);const j=ue(d,t.agents,{...r,resources:v}),y=j.filter(l=>P(l));await f("workspace.ensure.externalResourceSources",()=>B(y,e),{workspaceRoot:e,externalResourceCount:y.length}),await f("workspace.hydrate.externalTools",()=>ve(d,y,e,S.scope),{workspaceRoot:e,externalResourceCount:y.length}),xe(d);const C=ie(r.frameworkContractValidation),F=Array.from(new Set([e,...m?[m]:[],...b.map(l=>l.root)]));await f("workspace.validate.frameworkContracts",async()=>{ae({agents:t.agents,tools:d,skillRegistry:M,ownedRoots:F,refs:t.refs,mode:C})},{workspaceRoot:e,mode:C});const N=Array.from(new Set([...m?[m]:[],...p.tools.filter(l=>w(l)),...p.skills.filter(l=>w(l)),...j]));await f("workspace.validate.resources",async()=>{Ce(a,n,c,i,d,t.agents)},{workspaceRoot:e,agentCount:t.agents.length,toolCount:d.size,modelCount:c.size,mcpServerCount:n.size}),await f("workspace.validate.routingTargets",async()=>{Ee(t.refs,t.agents)},{workspaceRoot:e,agentCount:t.agents.length});const U=await f("workspace.compile.bindings",async()=>Pe(e,t.refs,t.agents,c,d),{workspaceRoot:e,agentCount:t.agents.length});return{workspaceRoot:e,resources:N,refs:t.refs,embeddings:a,mcpServers:n,models:c,vectorStores:i,tools:d,agents:new Map(t.agents.map(l=>[l.id,l])),bindings:U}}export{rt as loadWorkspace};
|