@contractspec/lib.ai-agent 1.57.0 → 1.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/agent-factory.d.ts +67 -71
- package/dist/agent/agent-factory.d.ts.map +1 -1
- package/dist/agent/agent-factory.js +658 -100
- package/dist/agent/agent.test.d.ts +2 -0
- package/dist/agent/agent.test.d.ts.map +1 -0
- package/dist/agent/contract-spec-agent.d.ts +48 -52
- package/dist/agent/contract-spec-agent.d.ts.map +1 -1
- package/dist/agent/contract-spec-agent.js +605 -146
- package/dist/agent/index.d.ts +4 -4
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +2102 -4
- package/dist/agent/json-runner.d.ts +15 -19
- package/dist/agent/json-runner.d.ts.map +1 -1
- package/dist/agent/json-runner.js +672 -57
- package/dist/agent/json-runner.test.d.ts +2 -0
- package/dist/agent/json-runner.test.d.ts.map +1 -0
- package/dist/agent/unified-agent.d.ts +132 -109
- package/dist/agent/unified-agent.d.ts.map +1 -1
- package/dist/agent/unified-agent.js +2011 -293
- package/dist/approval/index.d.ts +3 -2
- package/dist/approval/index.d.ts.map +1 -0
- package/dist/approval/index.js +128 -2
- package/dist/approval/workflow.d.ts +106 -110
- package/dist/approval/workflow.d.ts.map +1 -1
- package/dist/approval/workflow.js +126 -157
- package/dist/exporters/claude-agent-exporter.d.ts +50 -48
- package/dist/exporters/claude-agent-exporter.d.ts.map +1 -1
- package/dist/exporters/claude-agent-exporter.js +258 -203
- package/dist/exporters/index.d.ts +28 -4
- package/dist/exporters/index.d.ts.map +1 -0
- package/dist/exporters/index.js +737 -3
- package/dist/exporters/opencode-exporter.d.ts +47 -45
- package/dist/exporters/opencode-exporter.d.ts.map +1 -1
- package/dist/exporters/opencode-exporter.js +507 -191
- package/dist/exporters/types.d.ts +171 -169
- package/dist/exporters/types.d.ts.map +1 -1
- package/dist/exporters/types.js +1 -0
- package/dist/index.d.ts +15 -39
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3337 -32
- package/dist/interop/index.d.ts +48 -4
- package/dist/interop/index.d.ts.map +1 -0
- package/dist/interop/index.js +709 -3
- package/dist/interop/spec-consumer.d.ts +57 -53
- package/dist/interop/spec-consumer.d.ts.map +1 -1
- package/dist/interop/spec-consumer.js +302 -282
- package/dist/interop/tool-consumer.d.ts +57 -53
- package/dist/interop/tool-consumer.d.ts.map +1 -1
- package/dist/interop/tool-consumer.js +412 -213
- package/dist/interop/types.d.ts +173 -171
- package/dist/interop/types.d.ts.map +1 -1
- package/dist/interop/types.js +1 -0
- package/dist/knowledge/index.d.ts +2 -2
- package/dist/knowledge/index.d.ts.map +1 -0
- package/dist/knowledge/index.js +66 -2
- package/dist/knowledge/injector.d.ts +16 -20
- package/dist/knowledge/injector.d.ts.map +1 -1
- package/dist/knowledge/injector.js +56 -47
- package/dist/memory/in-memory.d.ts +15 -19
- package/dist/memory/in-memory.d.ts.map +1 -1
- package/dist/memory/in-memory.js +152 -46
- package/dist/memory/index.d.ts +3 -3
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +155 -3
- package/dist/memory/manager.d.ts +32 -36
- package/dist/memory/manager.d.ts.map +1 -1
- package/dist/memory/manager.js +96 -70
- package/dist/memory/memory.test.d.ts +2 -0
- package/dist/memory/memory.test.d.ts.map +1 -0
- package/dist/node/agent/agent-factory.js +661 -0
- package/dist/node/agent/contract-spec-agent.js +607 -0
- package/dist/node/agent/index.js +2103 -0
- package/dist/node/agent/json-runner.js +684 -0
- package/dist/node/agent/unified-agent.js +2019 -0
- package/dist/node/approval/index.js +129 -0
- package/dist/node/approval/workflow.js +129 -0
- package/dist/node/exporters/claude-agent-exporter.js +265 -0
- package/dist/node/exporters/index.js +738 -0
- package/dist/node/exporters/opencode-exporter.js +516 -0
- package/dist/node/exporters/types.js +0 -0
- package/dist/node/index.js +3337 -0
- package/dist/node/interop/index.js +710 -0
- package/dist/node/interop/spec-consumer.js +307 -0
- package/dist/node/interop/tool-consumer.js +419 -0
- package/dist/node/interop/types.js +0 -0
- package/dist/node/knowledge/index.js +67 -0
- package/dist/node/knowledge/injector.js +67 -0
- package/dist/node/memory/in-memory.js +154 -0
- package/dist/node/memory/index.js +156 -0
- package/dist/node/memory/manager.js +105 -0
- package/dist/node/providers/claude-agent-sdk/adapter.js +624 -0
- package/dist/node/providers/claude-agent-sdk/index.js +673 -0
- package/dist/node/providers/claude-agent-sdk/session-bridge.js +149 -0
- package/dist/node/providers/claude-agent-sdk/tool-bridge.js +118 -0
- package/dist/node/providers/index.js +1261 -0
- package/dist/node/providers/opencode-sdk/adapter.js +669 -0
- package/dist/node/providers/opencode-sdk/agent-bridge.js +299 -0
- package/dist/node/providers/opencode-sdk/index.js +703 -0
- package/dist/node/providers/opencode-sdk/tool-bridge.js +141 -0
- package/dist/node/providers/registry.js +89 -0
- package/dist/node/providers/types.js +56 -0
- package/dist/node/schema/index.js +195 -0
- package/dist/node/schema/json-schema-to-zod.js +152 -0
- package/dist/node/schema/schema-output.js +190 -0
- package/dist/node/session/index.js +90 -0
- package/dist/node/session/store.js +90 -0
- package/dist/node/spec/index.js +85 -0
- package/dist/node/spec/registry.js +56 -0
- package/dist/node/spec/spec.js +44 -0
- package/dist/node/telemetry/adapter.js +85 -0
- package/dist/node/telemetry/index.js +86 -0
- package/dist/node/tools/index.js +345 -0
- package/dist/node/tools/knowledge-tool.js +74 -0
- package/dist/node/tools/mcp-client.js +47 -0
- package/dist/node/tools/mcp-server.js +205 -0
- package/dist/node/tools/tool-adapter.js +197 -0
- package/dist/node/types.js +0 -0
- package/dist/providers/claude-agent-sdk/adapter.d.ts +60 -52
- package/dist/providers/claude-agent-sdk/adapter.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/adapter.js +622 -304
- package/dist/providers/claude-agent-sdk/index.d.ts +22 -4
- package/dist/providers/claude-agent-sdk/index.d.ts.map +1 -0
- package/dist/providers/claude-agent-sdk/index.js +672 -4
- package/dist/providers/claude-agent-sdk/session-bridge.d.ts +43 -40
- package/dist/providers/claude-agent-sdk/session-bridge.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/session-bridge.js +121 -130
- package/dist/providers/claude-agent-sdk/tool-bridge.d.ts +63 -60
- package/dist/providers/claude-agent-sdk/tool-bridge.d.ts.map +1 -1
- package/dist/providers/claude-agent-sdk/tool-bridge.js +104 -108
- package/dist/providers/index.d.ts +28 -7
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +1261 -8
- package/dist/providers/opencode-sdk/adapter.d.ts +56 -48
- package/dist/providers/opencode-sdk/adapter.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/adapter.js +667 -274
- package/dist/providers/opencode-sdk/agent-bridge.d.ts +62 -57
- package/dist/providers/opencode-sdk/agent-bridge.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/agent-bridge.js +289 -155
- package/dist/providers/opencode-sdk/index.d.ts +22 -4
- package/dist/providers/opencode-sdk/index.d.ts.map +1 -0
- package/dist/providers/opencode-sdk/index.js +702 -4
- package/dist/providers/opencode-sdk/tool-bridge.d.ts +41 -42
- package/dist/providers/opencode-sdk/tool-bridge.d.ts.map +1 -1
- package/dist/providers/opencode-sdk/tool-bridge.js +121 -107
- package/dist/providers/registry.d.ts +10 -11
- package/dist/providers/registry.d.ts.map +1 -1
- package/dist/providers/registry.js +86 -49
- package/dist/providers/types.d.ts +169 -166
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +54 -42
- package/dist/schema/index.d.ts +3 -3
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +194 -3
- package/dist/schema/json-schema-to-zod.d.ts +23 -26
- package/dist/schema/json-schema-to-zod.d.ts.map +1 -1
- package/dist/schema/json-schema-to-zod.js +138 -110
- package/dist/schema/schema-output.d.ts +29 -32
- package/dist/schema/schema-output.d.ts.map +1 -1
- package/dist/schema/schema-output.js +178 -53
- package/dist/session/index.d.ts +2 -2
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +89 -2
- package/dist/session/store.d.ts +51 -55
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +85 -74
- package/dist/spec/index.d.ts +3 -3
- package/dist/spec/index.d.ts.map +1 -0
- package/dist/spec/index.js +84 -3
- package/dist/spec/registry.d.ts +32 -36
- package/dist/spec/registry.d.ts.map +1 -1
- package/dist/spec/registry.js +51 -60
- package/dist/spec/spec.d.ts +80 -84
- package/dist/spec/spec.d.ts.map +1 -1
- package/dist/spec/spec.js +40 -26
- package/dist/telemetry/adapter.d.ts +33 -37
- package/dist/telemetry/adapter.d.ts.map +1 -1
- package/dist/telemetry/adapter.js +78 -96
- package/dist/telemetry/index.d.ts +2 -2
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +85 -2
- package/dist/tools/index.d.ts +5 -5
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +344 -5
- package/dist/tools/knowledge-tool.d.ts +4 -8
- package/dist/tools/knowledge-tool.d.ts.map +1 -1
- package/dist/tools/knowledge-tool.js +68 -48
- package/dist/tools/mcp-client.d.ts +17 -21
- package/dist/tools/mcp-client.d.ts.map +1 -1
- package/dist/tools/mcp-client.js +42 -53
- package/dist/tools/mcp-server.d.ts +14 -18
- package/dist/tools/mcp-server.d.ts.map +1 -1
- package/dist/tools/mcp-server.js +200 -64
- package/dist/tools/tool-adapter.d.ts +7 -11
- package/dist/tools/tool-adapter.d.ts.map +1 -1
- package/dist/tools/tool-adapter.js +187 -70
- package/dist/tools/tools.test.d.ts +2 -0
- package/dist/tools/tools.test.d.ts.map +1 -0
- package/dist/types.d.ts +108 -111
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +448 -90
- package/dist/_virtual/_rolldown/runtime.js +0 -8
- package/dist/agent/agent-factory.js.map +0 -1
- package/dist/agent/contract-spec-agent.js.map +0 -1
- package/dist/agent/json-runner.js.map +0 -1
- package/dist/agent/unified-agent.js.map +0 -1
- package/dist/approval/workflow.js.map +0 -1
- package/dist/exporters/claude-agent-exporter.js.map +0 -1
- package/dist/exporters/opencode-exporter.js.map +0 -1
- package/dist/interop/spec-consumer.js.map +0 -1
- package/dist/interop/tool-consumer.js.map +0 -1
- package/dist/knowledge/injector.js.map +0 -1
- package/dist/memory/in-memory.js.map +0 -1
- package/dist/memory/manager.js.map +0 -1
- package/dist/providers/claude-agent-sdk/adapter.js.map +0 -1
- package/dist/providers/claude-agent-sdk/session-bridge.js.map +0 -1
- package/dist/providers/claude-agent-sdk/tool-bridge.js.map +0 -1
- package/dist/providers/opencode-sdk/adapter.js.map +0 -1
- package/dist/providers/opencode-sdk/agent-bridge.js.map +0 -1
- package/dist/providers/opencode-sdk/tool-bridge.js.map +0 -1
- package/dist/providers/registry.js.map +0 -1
- package/dist/providers/types.js.map +0 -1
- package/dist/schema/json-schema-to-zod.js.map +0 -1
- package/dist/schema/schema-output.js.map +0 -1
- package/dist/session/store.js.map +0 -1
- package/dist/spec/registry.js.map +0 -1
- package/dist/spec/spec.js.map +0 -1
- package/dist/telemetry/adapter.js.map +0 -1
- package/dist/tools/knowledge-tool.js.map +0 -1
- package/dist/tools/mcp-client.js.map +0 -1
- package/dist/tools/mcp-server.js.map +0 -1
- package/dist/tools/tool-adapter.js.map +0 -1
|
@@ -1,210 +1,265 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
export(spec, options = {}) {
|
|
13
|
-
const tools = this.exportTools(spec);
|
|
14
|
-
return {
|
|
15
|
-
config: this.buildConfig(spec, tools, options),
|
|
16
|
-
claudeMd: options.generateClaudeMd ? this.generateClaudeMd(spec, options) : void 0,
|
|
17
|
-
tools,
|
|
18
|
-
exportedAt: /* @__PURE__ */ new Date(),
|
|
19
|
-
sourceSpec: agentKey(spec.meta)
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Export multiple specs.
|
|
24
|
-
*/
|
|
25
|
-
exportMany(specs, options = {}) {
|
|
26
|
-
return specs.map((spec) => this.export(spec, options));
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Validate that a spec can be exported.
|
|
30
|
-
*/
|
|
31
|
-
validate(spec) {
|
|
32
|
-
const errors = [];
|
|
33
|
-
if (!spec.meta?.key) errors.push("Spec must have a meta.key");
|
|
34
|
-
if (!spec.instructions) errors.push("Spec must have instructions");
|
|
35
|
-
if (!spec.tools || spec.tools.length === 0) errors.push("Spec must have at least one tool");
|
|
36
|
-
for (const tool of spec.tools ?? []) {
|
|
37
|
-
if (!tool.name) errors.push("All tools must have a name");
|
|
38
|
-
if (!tool.description && !tool.name) errors.push(`Tool must have a description or name`);
|
|
39
|
-
}
|
|
40
|
-
return {
|
|
41
|
-
valid: errors.length === 0,
|
|
42
|
-
errors
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Build Claude Agent SDK configuration.
|
|
47
|
-
*/
|
|
48
|
-
buildConfig(spec, tools, options) {
|
|
49
|
-
const config = {
|
|
50
|
-
model: options.model ?? "claude-sonnet-4-20250514",
|
|
51
|
-
system: this.buildSystemPrompt(spec, options),
|
|
52
|
-
tools,
|
|
53
|
-
max_turns: spec.maxSteps ?? 10
|
|
54
|
-
};
|
|
55
|
-
if (options.computerUse) config.computer_use = true;
|
|
56
|
-
if (options.extendedThinking) config.extended_thinking = true;
|
|
57
|
-
if (options.mcpServers && options.mcpServers.length > 0) config.mcp_servers = options.mcpServers;
|
|
58
|
-
return config;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Build system prompt from spec.
|
|
62
|
-
*/
|
|
63
|
-
buildSystemPrompt(spec, options) {
|
|
64
|
-
const parts = [];
|
|
65
|
-
parts.push(spec.instructions);
|
|
66
|
-
if (spec.knowledge && spec.knowledge.length > 0) {
|
|
67
|
-
parts.push("");
|
|
68
|
-
parts.push("## Knowledge Sources");
|
|
69
|
-
for (const k of spec.knowledge) if (k.instructions) parts.push(`- ${k.key}: ${k.instructions}`);
|
|
70
|
-
}
|
|
71
|
-
if (spec.policy) {
|
|
72
|
-
parts.push("");
|
|
73
|
-
parts.push("## Policy");
|
|
74
|
-
if (spec.policy.confidence?.min) parts.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
|
|
75
|
-
if (spec.policy.escalation) parts.push("- Escalation policy is configured");
|
|
76
|
-
}
|
|
77
|
-
if (options.metadata) {
|
|
78
|
-
parts.push("");
|
|
79
|
-
parts.push("## Additional Context");
|
|
80
|
-
for (const [key, value] of Object.entries(options.metadata)) parts.push(`- ${key}: ${String(value)}`);
|
|
81
|
-
}
|
|
82
|
-
return parts.join("\n");
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Export tools to Claude Agent SDK format.
|
|
86
|
-
*/
|
|
87
|
-
exportTools(spec) {
|
|
88
|
-
return spec.tools.map((tool) => ({
|
|
89
|
-
name: tool.name,
|
|
90
|
-
description: tool.description ?? `Execute ${tool.name}`,
|
|
91
|
-
input_schema: this.normalizeSchema(tool.schema),
|
|
92
|
-
requires_confirmation: tool.requiresApproval ?? !tool.automationSafe
|
|
93
|
-
}));
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Normalize schema to Claude Agent SDK format.
|
|
97
|
-
*/
|
|
98
|
-
normalizeSchema(schema) {
|
|
99
|
-
if (!schema) return { type: "object" };
|
|
100
|
-
if (schema.type === "object") return {
|
|
101
|
-
type: "object",
|
|
102
|
-
properties: schema.properties,
|
|
103
|
-
required: schema.required
|
|
104
|
-
};
|
|
105
|
-
return {
|
|
106
|
-
type: "object",
|
|
107
|
-
properties: { value: schema },
|
|
108
|
-
required: ["value"]
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Generate CLAUDE.md content for Claude Code CLI integration.
|
|
113
|
-
*/
|
|
114
|
-
generateClaudeMd(spec, options) {
|
|
115
|
-
const lines = [];
|
|
116
|
-
lines.push("# Agent Configuration");
|
|
117
|
-
lines.push("");
|
|
118
|
-
if (spec.description) {
|
|
119
|
-
lines.push(`> ${spec.description}`);
|
|
120
|
-
lines.push("");
|
|
121
|
-
}
|
|
122
|
-
lines.push("## Metadata");
|
|
123
|
-
lines.push("");
|
|
124
|
-
lines.push(`- **Name**: ${spec.meta.key}`);
|
|
125
|
-
lines.push(`- **Version**: ${spec.meta.version}`);
|
|
126
|
-
if (spec.meta.owners && spec.meta.owners.length > 0) lines.push(`- **Owners**: ${spec.meta.owners.join(", ")}`);
|
|
127
|
-
if (options.model) lines.push(`- **Model**: ${options.model}`);
|
|
128
|
-
lines.push("");
|
|
129
|
-
lines.push("## Instructions");
|
|
130
|
-
lines.push("");
|
|
131
|
-
lines.push(spec.instructions);
|
|
132
|
-
lines.push("");
|
|
133
|
-
if (spec.tools.length > 0) {
|
|
134
|
-
lines.push("## Available Tools");
|
|
135
|
-
lines.push("");
|
|
136
|
-
for (const tool of spec.tools) {
|
|
137
|
-
const flags = [];
|
|
138
|
-
if (tool.requiresApproval) flags.push("requires approval");
|
|
139
|
-
if (tool.automationSafe === false) flags.push("not automation safe");
|
|
140
|
-
const flagStr = flags.length > 0 ? ` (${flags.join(", ")})` : "";
|
|
141
|
-
lines.push(`### ${tool.name}${flagStr}`);
|
|
142
|
-
lines.push("");
|
|
143
|
-
if (tool.description) {
|
|
144
|
-
lines.push(tool.description);
|
|
145
|
-
lines.push("");
|
|
146
|
-
}
|
|
147
|
-
if (tool.schema) {
|
|
148
|
-
lines.push("**Parameters:**");
|
|
149
|
-
lines.push("```json");
|
|
150
|
-
lines.push(JSON.stringify(tool.schema, null, 2));
|
|
151
|
-
lines.push("```");
|
|
152
|
-
lines.push("");
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (spec.knowledge && spec.knowledge.length > 0) {
|
|
157
|
-
lines.push("## Knowledge Sources");
|
|
158
|
-
lines.push("");
|
|
159
|
-
for (const k of spec.knowledge) {
|
|
160
|
-
const required = k.required ? "(required)" : "(optional)";
|
|
161
|
-
lines.push(`- **${k.key}** ${required}`);
|
|
162
|
-
if (k.instructions) lines.push(` - ${k.instructions}`);
|
|
163
|
-
}
|
|
164
|
-
lines.push("");
|
|
165
|
-
}
|
|
166
|
-
if (spec.policy) {
|
|
167
|
-
lines.push("## Policy");
|
|
168
|
-
lines.push("");
|
|
169
|
-
if (spec.policy.confidence?.min) lines.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
|
|
170
|
-
if (spec.policy.escalation) lines.push("- Escalation policy configured");
|
|
171
|
-
if (spec.policy.flags && spec.policy.flags.length > 0) lines.push(`- Feature flags: ${spec.policy.flags.join(", ")}`);
|
|
172
|
-
lines.push("");
|
|
173
|
-
}
|
|
174
|
-
if (options.mcpServers && options.mcpServers.length > 0) {
|
|
175
|
-
lines.push("## MCP Servers");
|
|
176
|
-
lines.push("");
|
|
177
|
-
for (const server of options.mcpServers) lines.push(`- **${server.name}**: \`${server.command}${server.args ? " " + server.args.join(" ") : ""}\``);
|
|
178
|
-
lines.push("");
|
|
179
|
-
}
|
|
180
|
-
lines.push("---");
|
|
181
|
-
lines.push("");
|
|
182
|
-
lines.push(`*Generated from ContractSpec: ${agentKey(spec.meta)}*`);
|
|
183
|
-
return lines.join("\n");
|
|
184
|
-
}
|
|
1
|
+
// @bun
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
set: (newValue) => all[name] = () => newValue
|
|
10
|
+
});
|
|
185
11
|
};
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
12
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
13
|
+
var __require = import.meta.require;
|
|
14
|
+
|
|
15
|
+
// src/spec/spec.ts
|
|
16
|
+
function defineAgent(spec) {
|
|
17
|
+
if (!spec.meta?.key) {
|
|
18
|
+
throw new Error("Agent key is required");
|
|
19
|
+
}
|
|
20
|
+
if (typeof spec.meta.version !== "string") {
|
|
21
|
+
throw new Error(`Agent ${spec.meta.key} is missing a string version`);
|
|
22
|
+
}
|
|
23
|
+
if (!spec.instructions?.trim()) {
|
|
24
|
+
throw new Error(`Agent ${spec.meta.key} requires instructions`);
|
|
25
|
+
}
|
|
26
|
+
if (!spec.tools?.length) {
|
|
27
|
+
throw new Error(`Agent ${spec.meta.key} must expose at least one tool`);
|
|
28
|
+
}
|
|
29
|
+
const toolNames = new Set;
|
|
30
|
+
for (const tool of spec.tools) {
|
|
31
|
+
if (toolNames.has(tool.name)) {
|
|
32
|
+
throw new Error(`Agent ${spec.meta.key} has duplicate tool name: ${tool.name}`);
|
|
33
|
+
}
|
|
34
|
+
toolNames.add(tool.name);
|
|
35
|
+
}
|
|
36
|
+
return Object.freeze(spec);
|
|
37
|
+
}
|
|
38
|
+
function agentKey(meta) {
|
|
39
|
+
return `${meta.key}.v${meta.version}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/exporters/claude-agent-exporter.ts
|
|
43
|
+
class ClaudeAgentExporter {
|
|
44
|
+
format = "claude-agent";
|
|
45
|
+
export(spec, options = {}) {
|
|
46
|
+
const tools = this.exportTools(spec);
|
|
47
|
+
const config = this.buildConfig(spec, tools, options);
|
|
48
|
+
const claudeMd = options.generateClaudeMd ? this.generateClaudeMd(spec, options) : undefined;
|
|
49
|
+
return {
|
|
50
|
+
config,
|
|
51
|
+
claudeMd,
|
|
52
|
+
tools,
|
|
53
|
+
exportedAt: new Date,
|
|
54
|
+
sourceSpec: agentKey(spec.meta)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
exportMany(specs, options = {}) {
|
|
58
|
+
return specs.map((spec) => this.export(spec, options));
|
|
59
|
+
}
|
|
60
|
+
validate(spec) {
|
|
61
|
+
const errors = [];
|
|
62
|
+
if (!spec.meta?.key) {
|
|
63
|
+
errors.push("Spec must have a meta.key");
|
|
64
|
+
}
|
|
65
|
+
if (!spec.instructions) {
|
|
66
|
+
errors.push("Spec must have instructions");
|
|
67
|
+
}
|
|
68
|
+
if (!spec.tools || spec.tools.length === 0) {
|
|
69
|
+
errors.push("Spec must have at least one tool");
|
|
70
|
+
}
|
|
71
|
+
for (const tool of spec.tools ?? []) {
|
|
72
|
+
if (!tool.name) {
|
|
73
|
+
errors.push("All tools must have a name");
|
|
74
|
+
}
|
|
75
|
+
if (!tool.description && !tool.name) {
|
|
76
|
+
errors.push(`Tool must have a description or name`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { valid: errors.length === 0, errors };
|
|
80
|
+
}
|
|
81
|
+
buildConfig(spec, tools, options) {
|
|
82
|
+
const config = {
|
|
83
|
+
model: options.model ?? "claude-sonnet-4-20250514",
|
|
84
|
+
system: this.buildSystemPrompt(spec, options),
|
|
85
|
+
tools,
|
|
86
|
+
max_turns: spec.maxSteps ?? 10
|
|
87
|
+
};
|
|
88
|
+
if (options.computerUse) {
|
|
89
|
+
config.computer_use = true;
|
|
90
|
+
}
|
|
91
|
+
if (options.extendedThinking) {
|
|
92
|
+
config.extended_thinking = true;
|
|
93
|
+
}
|
|
94
|
+
if (options.mcpServers && options.mcpServers.length > 0) {
|
|
95
|
+
config.mcp_servers = options.mcpServers;
|
|
96
|
+
}
|
|
97
|
+
return config;
|
|
98
|
+
}
|
|
99
|
+
buildSystemPrompt(spec, options) {
|
|
100
|
+
const parts = [];
|
|
101
|
+
parts.push(spec.instructions);
|
|
102
|
+
if (spec.knowledge && spec.knowledge.length > 0) {
|
|
103
|
+
parts.push("");
|
|
104
|
+
parts.push("## Knowledge Sources");
|
|
105
|
+
for (const k of spec.knowledge) {
|
|
106
|
+
if (k.instructions) {
|
|
107
|
+
parts.push(`- ${k.key}: ${k.instructions}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (spec.policy) {
|
|
112
|
+
parts.push("");
|
|
113
|
+
parts.push("## Policy");
|
|
114
|
+
if (spec.policy.confidence?.min) {
|
|
115
|
+
parts.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
|
|
116
|
+
}
|
|
117
|
+
if (spec.policy.escalation) {
|
|
118
|
+
parts.push("- Escalation policy is configured");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (options.metadata) {
|
|
122
|
+
parts.push("");
|
|
123
|
+
parts.push("## Additional Context");
|
|
124
|
+
for (const [key, value] of Object.entries(options.metadata)) {
|
|
125
|
+
parts.push(`- ${key}: ${String(value)}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return parts.join(`
|
|
129
|
+
`);
|
|
130
|
+
}
|
|
131
|
+
exportTools(spec) {
|
|
132
|
+
return spec.tools.map((tool) => ({
|
|
133
|
+
name: tool.name,
|
|
134
|
+
description: tool.description ?? `Execute ${tool.name}`,
|
|
135
|
+
input_schema: this.normalizeSchema(tool.schema),
|
|
136
|
+
requires_confirmation: tool.requiresApproval ?? !tool.automationSafe
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
normalizeSchema(schema) {
|
|
140
|
+
if (!schema) {
|
|
141
|
+
return { type: "object" };
|
|
142
|
+
}
|
|
143
|
+
if (schema.type === "object") {
|
|
144
|
+
return {
|
|
145
|
+
type: "object",
|
|
146
|
+
properties: schema.properties,
|
|
147
|
+
required: schema.required
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
type: "object",
|
|
152
|
+
properties: { value: schema },
|
|
153
|
+
required: ["value"]
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
generateClaudeMd(spec, options) {
|
|
157
|
+
const lines = [];
|
|
158
|
+
lines.push("# Agent Configuration");
|
|
159
|
+
lines.push("");
|
|
160
|
+
if (spec.description) {
|
|
161
|
+
lines.push(`> ${spec.description}`);
|
|
162
|
+
lines.push("");
|
|
163
|
+
}
|
|
164
|
+
lines.push("## Metadata");
|
|
165
|
+
lines.push("");
|
|
166
|
+
lines.push(`- **Name**: ${spec.meta.key}`);
|
|
167
|
+
lines.push(`- **Version**: ${spec.meta.version}`);
|
|
168
|
+
if (spec.meta.owners && spec.meta.owners.length > 0) {
|
|
169
|
+
lines.push(`- **Owners**: ${spec.meta.owners.join(", ")}`);
|
|
170
|
+
}
|
|
171
|
+
if (options.model) {
|
|
172
|
+
lines.push(`- **Model**: ${options.model}`);
|
|
173
|
+
}
|
|
174
|
+
lines.push("");
|
|
175
|
+
lines.push("## Instructions");
|
|
176
|
+
lines.push("");
|
|
177
|
+
lines.push(spec.instructions);
|
|
178
|
+
lines.push("");
|
|
179
|
+
if (spec.tools.length > 0) {
|
|
180
|
+
lines.push("## Available Tools");
|
|
181
|
+
lines.push("");
|
|
182
|
+
for (const tool of spec.tools) {
|
|
183
|
+
const flags = [];
|
|
184
|
+
if (tool.requiresApproval) {
|
|
185
|
+
flags.push("requires approval");
|
|
186
|
+
}
|
|
187
|
+
if (tool.automationSafe === false) {
|
|
188
|
+
flags.push("not automation safe");
|
|
189
|
+
}
|
|
190
|
+
const flagStr = flags.length > 0 ? ` (${flags.join(", ")})` : "";
|
|
191
|
+
lines.push(`### ${tool.name}${flagStr}`);
|
|
192
|
+
lines.push("");
|
|
193
|
+
if (tool.description) {
|
|
194
|
+
lines.push(tool.description);
|
|
195
|
+
lines.push("");
|
|
196
|
+
}
|
|
197
|
+
if (tool.schema) {
|
|
198
|
+
lines.push("**Parameters:**");
|
|
199
|
+
lines.push("```json");
|
|
200
|
+
lines.push(JSON.stringify(tool.schema, null, 2));
|
|
201
|
+
lines.push("```");
|
|
202
|
+
lines.push("");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (spec.knowledge && spec.knowledge.length > 0) {
|
|
207
|
+
lines.push("## Knowledge Sources");
|
|
208
|
+
lines.push("");
|
|
209
|
+
for (const k of spec.knowledge) {
|
|
210
|
+
const required = k.required ? "(required)" : "(optional)";
|
|
211
|
+
lines.push(`- **${k.key}** ${required}`);
|
|
212
|
+
if (k.instructions) {
|
|
213
|
+
lines.push(` - ${k.instructions}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
lines.push("");
|
|
217
|
+
}
|
|
218
|
+
if (spec.policy) {
|
|
219
|
+
lines.push("## Policy");
|
|
220
|
+
lines.push("");
|
|
221
|
+
if (spec.policy.confidence?.min) {
|
|
222
|
+
lines.push(`- Minimum confidence: ${spec.policy.confidence.min}`);
|
|
223
|
+
}
|
|
224
|
+
if (spec.policy.escalation) {
|
|
225
|
+
lines.push("- Escalation policy configured");
|
|
226
|
+
}
|
|
227
|
+
if (spec.policy.flags && spec.policy.flags.length > 0) {
|
|
228
|
+
lines.push(`- Feature flags: ${spec.policy.flags.join(", ")}`);
|
|
229
|
+
}
|
|
230
|
+
lines.push("");
|
|
231
|
+
}
|
|
232
|
+
if (options.mcpServers && options.mcpServers.length > 0) {
|
|
233
|
+
lines.push("## MCP Servers");
|
|
234
|
+
lines.push("");
|
|
235
|
+
for (const server of options.mcpServers) {
|
|
236
|
+
lines.push(`- **${server.name}**: \`${server.command}${server.args ? " " + server.args.join(" ") : ""}\``);
|
|
237
|
+
}
|
|
238
|
+
lines.push("");
|
|
239
|
+
}
|
|
240
|
+
lines.push("---");
|
|
241
|
+
lines.push("");
|
|
242
|
+
lines.push(`*Generated from ContractSpec: ${agentKey(spec.meta)}*`);
|
|
243
|
+
return lines.join(`
|
|
244
|
+
`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
189
247
|
function exportToClaudeAgent(spec, options) {
|
|
190
|
-
|
|
248
|
+
const exporter = new ClaudeAgentExporter;
|
|
249
|
+
return exporter.export(spec, options);
|
|
191
250
|
}
|
|
192
|
-
/**
|
|
193
|
-
* Generate CLAUDE.md content from an AgentSpec.
|
|
194
|
-
*/
|
|
195
251
|
function generateClaudeMd(spec, options) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}).claudeMd ?? "";
|
|
252
|
+
const exporter = new ClaudeAgentExporter;
|
|
253
|
+
const result = exporter.export(spec, { ...options, generateClaudeMd: true });
|
|
254
|
+
return result.claudeMd ?? "";
|
|
200
255
|
}
|
|
201
|
-
/**
|
|
202
|
-
* Validate an AgentSpec for Claude Agent SDK export.
|
|
203
|
-
*/
|
|
204
256
|
function validateForClaudeAgent(spec) {
|
|
205
|
-
|
|
257
|
+
const exporter = new ClaudeAgentExporter;
|
|
258
|
+
return exporter.validate(spec);
|
|
206
259
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
260
|
+
export {
|
|
261
|
+
validateForClaudeAgent,
|
|
262
|
+
generateClaudeMd,
|
|
263
|
+
exportToClaudeAgent,
|
|
264
|
+
ClaudeAgentExporter
|
|
265
|
+
};
|
|
@@ -1,4 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Spec exporters for ContractSpec agents.
|
|
3
|
+
*
|
|
4
|
+
* Exporters convert ContractSpec AgentSpec definitions into formats
|
|
5
|
+
* compatible with external agent SDKs.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import {
|
|
10
|
+
* exportToClaudeAgent,
|
|
11
|
+
* exportToOpenCode,
|
|
12
|
+
* } from '@contractspec/lib.ai-agent/exporters';
|
|
13
|
+
*
|
|
14
|
+
* // Export to Claude Agent SDK format
|
|
15
|
+
* const claudeResult = exportToClaudeAgent(agentSpec, {
|
|
16
|
+
* generateClaudeMd: true,
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Export to OpenCode SDK format
|
|
20
|
+
* const openCodeResult = exportToOpenCode(agentSpec, {
|
|
21
|
+
* agentType: 'build',
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export * from './types';
|
|
26
|
+
export { ClaudeAgentExporter, exportToClaudeAgent, generateClaudeMd, validateForClaudeAgent, } from './claude-agent-exporter';
|
|
27
|
+
export { OpenCodeExporter, exportToOpenCode, generateOpenCodeMarkdown, generateOpenCodeJSON, validateForOpenCode, } from './opencode-exporter';
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/exporters/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,sBAAsB,GACvB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,wBAAwB,EACxB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC"}
|