zidane 1.0.2 → 1.2.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/README.md +723 -0
- package/dist/agent-DvZm8U14.d.ts +313 -0
- package/dist/chunk-26LIQARN.js +109 -0
- package/dist/chunk-27EP7HB3.js +1005 -0
- package/dist/chunk-34KXKPNN.js +45 -0
- package/dist/chunk-LMSOIIAT.js +274 -0
- package/dist/chunk-LS57GDAV.js +365 -0
- package/dist/chunk-PNKVD2UK.js +26 -0
- package/dist/harnesses.d.ts +7 -0
- package/dist/harnesses.js +13 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +57 -0
- package/dist/mcp.d.ts +7 -0
- package/dist/mcp.js +11 -0
- package/dist/providers.d.ts +65 -0
- package/dist/providers.js +257 -0
- package/dist/session.d.ts +167 -0
- package/dist/session.js +27 -0
- package/dist/spawn-pP2grsVp.d.ts +63 -0
- package/dist/tools.d.ts +28 -0
- package/dist/tools.js +20 -0
- package/dist/types-4CFQ-6Qu.d.ts +94 -0
- package/package.json +69 -6
- package/index.js +0 -1
- package/zidane.jpeg +0 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { Hookable } from 'hookable';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
import { M as McpServerConfig, d as TurnUsage, b as SessionMessage, C as ChildRunStats, a as AgentStats, A as AgentRunOptions, c as ToolExecutionMode } from './types-4CFQ-6Qu.js';
|
|
4
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
5
|
+
import { Provider, StreamOptions } from './providers.js';
|
|
6
|
+
import { Session } from './session.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Execution context types.
|
|
10
|
+
*
|
|
11
|
+
* An execution context defines *where* and *how* an agent's tools run.
|
|
12
|
+
* The agent loop and tools interact through this interface without knowing
|
|
13
|
+
* whether they're running in-process, in a Docker container, or in a
|
|
14
|
+
* remote sandbox.
|
|
15
|
+
*/
|
|
16
|
+
interface ContextCapabilities {
|
|
17
|
+
/** Can execute shell commands */
|
|
18
|
+
shell: boolean;
|
|
19
|
+
/** Can read/write files in a workspace */
|
|
20
|
+
filesystem: boolean;
|
|
21
|
+
/** Can make outbound network requests */
|
|
22
|
+
network: boolean;
|
|
23
|
+
/** Has GPU access */
|
|
24
|
+
gpu: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Opaque handle to a running execution context instance */
|
|
27
|
+
interface ExecutionHandle {
|
|
28
|
+
id: string;
|
|
29
|
+
type: ContextType;
|
|
30
|
+
/** Working directory within the context */
|
|
31
|
+
cwd: string;
|
|
32
|
+
}
|
|
33
|
+
interface ExecResult {
|
|
34
|
+
stdout: string;
|
|
35
|
+
stderr: string;
|
|
36
|
+
exitCode: number;
|
|
37
|
+
}
|
|
38
|
+
interface SpawnConfig {
|
|
39
|
+
/** Working directory (created if it doesn't exist) */
|
|
40
|
+
cwd?: string;
|
|
41
|
+
/** Environment variables */
|
|
42
|
+
env?: Record<string, string>;
|
|
43
|
+
/** Docker image (only for 'docker' context) */
|
|
44
|
+
image?: string;
|
|
45
|
+
/** Resource limits */
|
|
46
|
+
limits?: {
|
|
47
|
+
/** Memory limit in MB */
|
|
48
|
+
memory?: number;
|
|
49
|
+
/** CPU limit (e.g. '1.0' = 1 core) */
|
|
50
|
+
cpu?: string;
|
|
51
|
+
/** Timeout in seconds for the entire context lifetime */
|
|
52
|
+
timeout?: number;
|
|
53
|
+
};
|
|
54
|
+
/** Sandbox provider config (only for 'sandbox' context) */
|
|
55
|
+
sandbox?: {
|
|
56
|
+
provider: string;
|
|
57
|
+
apiKey?: string;
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
type ContextType = 'process' | 'docker' | 'sandbox';
|
|
62
|
+
interface ExecutionContext {
|
|
63
|
+
/** Context type identifier */
|
|
64
|
+
readonly type: ContextType;
|
|
65
|
+
/** What this context supports */
|
|
66
|
+
readonly capabilities: ContextCapabilities;
|
|
67
|
+
/** Spawn a new execution environment */
|
|
68
|
+
spawn: (config?: SpawnConfig) => Promise<ExecutionHandle>;
|
|
69
|
+
/** Execute a shell command in the context */
|
|
70
|
+
exec: (handle: ExecutionHandle, command: string, options?: {
|
|
71
|
+
cwd?: string;
|
|
72
|
+
env?: Record<string, string>;
|
|
73
|
+
timeout?: number;
|
|
74
|
+
}) => Promise<ExecResult>;
|
|
75
|
+
/** Read a file from the context's filesystem */
|
|
76
|
+
readFile: (handle: ExecutionHandle, path: string) => Promise<string>;
|
|
77
|
+
/** Write a file to the context's filesystem */
|
|
78
|
+
writeFile: (handle: ExecutionHandle, path: string, content: string) => Promise<void>;
|
|
79
|
+
/** List files in a directory */
|
|
80
|
+
listFiles: (handle: ExecutionHandle, path: string) => Promise<string[]>;
|
|
81
|
+
/** Destroy the execution environment and clean up resources */
|
|
82
|
+
destroy: (handle: ExecutionHandle) => Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Core tools available in every basic harness (without spawn) */
|
|
86
|
+
declare const basicTools: {
|
|
87
|
+
shell: ToolDef;
|
|
88
|
+
readFile: ToolDef;
|
|
89
|
+
writeFile: ToolDef;
|
|
90
|
+
listFiles: ToolDef;
|
|
91
|
+
};
|
|
92
|
+
declare const _default: HarnessConfig;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Runtime context passed to every tool execution.
|
|
96
|
+
* Provides access to the agent's provider, abort signal, execution environment, and hooks.
|
|
97
|
+
*/
|
|
98
|
+
interface ToolContext {
|
|
99
|
+
/** The LLM provider for this agent run */
|
|
100
|
+
provider: Provider;
|
|
101
|
+
/** Abort signal — tools should check this for early termination */
|
|
102
|
+
signal: AbortSignal;
|
|
103
|
+
/** The execution context (shell, filesystem, etc.) */
|
|
104
|
+
execution: ExecutionContext;
|
|
105
|
+
/** The active execution handle for the current agent run */
|
|
106
|
+
handle: ExecutionHandle;
|
|
107
|
+
/** Agent hooks for emitting events (e.g. spawn:complete) */
|
|
108
|
+
hooks: Hookable<AgentHooks>;
|
|
109
|
+
/** The harness config for this agent (tools available to the agent) */
|
|
110
|
+
harness: HarnessConfig;
|
|
111
|
+
}
|
|
112
|
+
interface ToolDef {
|
|
113
|
+
spec: Anthropic.Tool;
|
|
114
|
+
execute: (input: Record<string, unknown>, ctx: ToolContext) => Promise<string>;
|
|
115
|
+
}
|
|
116
|
+
type ToolMap = Map<string, ToolDef>;
|
|
117
|
+
interface HarnessConfig {
|
|
118
|
+
/** Display name for this harness */
|
|
119
|
+
name: string;
|
|
120
|
+
/** Default system prompt injected when no system is provided at run time */
|
|
121
|
+
system?: string;
|
|
122
|
+
/** Tool definitions available to the agent */
|
|
123
|
+
tools: Record<string, ToolDef>;
|
|
124
|
+
/** MCP servers to connect and expose as tools */
|
|
125
|
+
mcpServers?: McpServerConfig[];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Define a harness with a name, optional system prompt, and tools.
|
|
129
|
+
*/
|
|
130
|
+
declare function defineHarness(config: HarnessConfig): HarnessConfig;
|
|
131
|
+
type Harness = HarnessConfig;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* MCP (Model Context Protocol) server support.
|
|
135
|
+
*
|
|
136
|
+
* Connects to one or more MCP servers, discovers their tools,
|
|
137
|
+
* and wraps them as zidane ToolDefs for use in agent loops.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
interface McpConnection {
|
|
141
|
+
tools: Record<string, ToolDef>;
|
|
142
|
+
close: () => Promise<void>;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Convert MCP tool result content blocks to a single string.
|
|
146
|
+
* Text blocks are extracted; non-text blocks are JSON-stringified.
|
|
147
|
+
*/
|
|
148
|
+
declare function resultToString(content: unknown[]): string;
|
|
149
|
+
/**
|
|
150
|
+
* Connect to MCP servers and discover their tools.
|
|
151
|
+
*
|
|
152
|
+
* Each tool is namespaced as `mcp_{serverName}_{toolName}` to avoid
|
|
153
|
+
* collisions with harness tools or tools from other servers.
|
|
154
|
+
*
|
|
155
|
+
* @param configs - Array of MCP server configurations
|
|
156
|
+
* @param _clientFactory - Internal: override client construction for testing
|
|
157
|
+
* @param hooks - Optional agent hooks for firing mcp:connect, mcp:error, mcp:close events
|
|
158
|
+
*/
|
|
159
|
+
declare function connectMcpServers(configs: McpServerConfig[], _clientFactory?: () => Client, hooks?: Hookable<AgentHooks>): Promise<McpConnection>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Agent creation and state management.
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
interface AgentHooks {
|
|
166
|
+
'system:before': (ctx: {
|
|
167
|
+
system: string;
|
|
168
|
+
}) => void;
|
|
169
|
+
'turn:before': (ctx: {
|
|
170
|
+
turn: number;
|
|
171
|
+
options: StreamOptions;
|
|
172
|
+
}) => void;
|
|
173
|
+
'turn:after': (ctx: {
|
|
174
|
+
turn: number;
|
|
175
|
+
usage: TurnUsage;
|
|
176
|
+
}) => void;
|
|
177
|
+
'stream:text': (ctx: {
|
|
178
|
+
delta: string;
|
|
179
|
+
text: string;
|
|
180
|
+
}) => void;
|
|
181
|
+
'stream:end': (ctx: {
|
|
182
|
+
text: string;
|
|
183
|
+
}) => void;
|
|
184
|
+
'tool:before': (ctx: {
|
|
185
|
+
name: string;
|
|
186
|
+
input: Record<string, unknown>;
|
|
187
|
+
}) => void;
|
|
188
|
+
'tool:after': (ctx: {
|
|
189
|
+
name: string;
|
|
190
|
+
input: Record<string, unknown>;
|
|
191
|
+
result: string;
|
|
192
|
+
}) => void;
|
|
193
|
+
'tool:error': (ctx: {
|
|
194
|
+
name: string;
|
|
195
|
+
input: Record<string, unknown>;
|
|
196
|
+
error: Error;
|
|
197
|
+
}) => void;
|
|
198
|
+
'tool:gate': (ctx: {
|
|
199
|
+
name: string;
|
|
200
|
+
input: Record<string, unknown>;
|
|
201
|
+
block: boolean;
|
|
202
|
+
reason: string;
|
|
203
|
+
}) => void;
|
|
204
|
+
'tool:transform': (ctx: {
|
|
205
|
+
name: string;
|
|
206
|
+
input: Record<string, unknown>;
|
|
207
|
+
result: string;
|
|
208
|
+
isError: boolean;
|
|
209
|
+
}) => void;
|
|
210
|
+
'context:transform': (ctx: {
|
|
211
|
+
messages: SessionMessage[];
|
|
212
|
+
}) => void;
|
|
213
|
+
'steer:inject': (ctx: {
|
|
214
|
+
message: string;
|
|
215
|
+
}) => void;
|
|
216
|
+
'spawn:complete': (ctx: ChildRunStats) => void;
|
|
217
|
+
'spawn:before': (ctx: {
|
|
218
|
+
id: string;
|
|
219
|
+
task: string;
|
|
220
|
+
}) => void;
|
|
221
|
+
'spawn:error': (ctx: {
|
|
222
|
+
id: string;
|
|
223
|
+
task: string;
|
|
224
|
+
error: Error;
|
|
225
|
+
}) => void;
|
|
226
|
+
'mcp:connect': (ctx: {
|
|
227
|
+
name: string;
|
|
228
|
+
transport: string;
|
|
229
|
+
tools: string[];
|
|
230
|
+
}) => void;
|
|
231
|
+
'mcp:error': (ctx: {
|
|
232
|
+
name: string;
|
|
233
|
+
error: Error;
|
|
234
|
+
}) => void;
|
|
235
|
+
'mcp:close': (ctx: {
|
|
236
|
+
name: string;
|
|
237
|
+
}) => void;
|
|
238
|
+
'mcp:tool:before': (ctx: {
|
|
239
|
+
server: string;
|
|
240
|
+
tool: string;
|
|
241
|
+
input: Record<string, unknown>;
|
|
242
|
+
}) => void;
|
|
243
|
+
'mcp:tool:after': (ctx: {
|
|
244
|
+
server: string;
|
|
245
|
+
tool: string;
|
|
246
|
+
input: Record<string, unknown>;
|
|
247
|
+
result: string;
|
|
248
|
+
}) => void;
|
|
249
|
+
'mcp:tool:error': (ctx: {
|
|
250
|
+
server: string;
|
|
251
|
+
tool: string;
|
|
252
|
+
input: Record<string, unknown>;
|
|
253
|
+
error: Error;
|
|
254
|
+
}) => void;
|
|
255
|
+
'agent:abort': (ctx: object) => void;
|
|
256
|
+
'agent:done': (ctx: AgentStats) => void;
|
|
257
|
+
'session:start': (ctx: {
|
|
258
|
+
sessionId: string;
|
|
259
|
+
runId: string;
|
|
260
|
+
prompt: string;
|
|
261
|
+
}) => void;
|
|
262
|
+
'session:end': (ctx: {
|
|
263
|
+
sessionId: string;
|
|
264
|
+
runId: string;
|
|
265
|
+
status: 'completed' | 'aborted' | 'error';
|
|
266
|
+
}) => void;
|
|
267
|
+
'session:messages': (ctx: {
|
|
268
|
+
sessionId: string;
|
|
269
|
+
count: number;
|
|
270
|
+
}) => void;
|
|
271
|
+
'session:meta': (ctx: {
|
|
272
|
+
sessionId: string;
|
|
273
|
+
key: string;
|
|
274
|
+
value: unknown;
|
|
275
|
+
}) => void;
|
|
276
|
+
'session:save': (ctx: {
|
|
277
|
+
sessionId: string;
|
|
278
|
+
}) => void;
|
|
279
|
+
}
|
|
280
|
+
interface AgentOptions {
|
|
281
|
+
harness: HarnessConfig;
|
|
282
|
+
provider: Provider;
|
|
283
|
+
/** Tool execution mode: 'sequential' (default) or 'parallel' */
|
|
284
|
+
toolExecution?: ToolExecutionMode;
|
|
285
|
+
/** Execution context: where tools run. Defaults to in-process. */
|
|
286
|
+
execution?: ExecutionContext;
|
|
287
|
+
/** MCP servers to connect and expose as tools */
|
|
288
|
+
mcpServers?: McpServerConfig[];
|
|
289
|
+
/** Session for identity, message persistence, and run tracking */
|
|
290
|
+
session?: Session;
|
|
291
|
+
/** @internal */
|
|
292
|
+
_mcpConnector?: (configs: McpServerConfig[]) => Promise<McpConnection>;
|
|
293
|
+
}
|
|
294
|
+
interface Agent {
|
|
295
|
+
hooks: Hookable<AgentHooks>;
|
|
296
|
+
run: (options: AgentRunOptions) => Promise<AgentStats>;
|
|
297
|
+
abort: () => void;
|
|
298
|
+
steer: (message: string) => void;
|
|
299
|
+
followUp: (message: string) => void;
|
|
300
|
+
waitForIdle: () => Promise<void>;
|
|
301
|
+
reset: () => void;
|
|
302
|
+
/** Destroy the execution context and clean up resources */
|
|
303
|
+
destroy: () => Promise<void>;
|
|
304
|
+
readonly isRunning: boolean;
|
|
305
|
+
readonly messages: SessionMessage[];
|
|
306
|
+
readonly execution: ExecutionContext;
|
|
307
|
+
readonly handle: ExecutionHandle | null;
|
|
308
|
+
readonly session: Session | null;
|
|
309
|
+
meta: Record<string, unknown>;
|
|
310
|
+
}
|
|
311
|
+
declare function createAgent({ harness, provider, toolExecution, execution, mcpServers, session, _mcpConnector }: AgentOptions): Agent;
|
|
312
|
+
|
|
313
|
+
export { type Agent as A, type ContextCapabilities as C, type ExecutionContext as E, type Harness as H, type McpConnection as M, type SpawnConfig as S, type ToolContext as T, _default as _, type ExecResult as a, type AgentHooks as b, type AgentOptions as c, type ContextType as d, type ExecutionHandle as e, type HarnessConfig as f, type ToolDef as g, type ToolMap as h, connectMcpServers as i, createAgent as j, defineHarness as k, basicTools as l, resultToString as r };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__esm
|
|
3
|
+
} from "./chunk-PNKVD2UK.js";
|
|
4
|
+
|
|
5
|
+
// src/mcp/index.ts
|
|
6
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
|
+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
8
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
9
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
10
|
+
function resultToString(content) {
|
|
11
|
+
if (!content || !Array.isArray(content))
|
|
12
|
+
return "";
|
|
13
|
+
return content.map((block) => {
|
|
14
|
+
if (block?.type === "text")
|
|
15
|
+
return block.text;
|
|
16
|
+
return JSON.stringify(block);
|
|
17
|
+
}).join("\n");
|
|
18
|
+
}
|
|
19
|
+
function createTransport(config) {
|
|
20
|
+
switch (config.transport) {
|
|
21
|
+
case "stdio":
|
|
22
|
+
return new StdioClientTransport({
|
|
23
|
+
command: config.command,
|
|
24
|
+
args: config.args,
|
|
25
|
+
env: config.env
|
|
26
|
+
});
|
|
27
|
+
case "sse":
|
|
28
|
+
return new SSEClientTransport(new URL(config.url), {
|
|
29
|
+
requestInit: config.headers ? { headers: config.headers } : void 0
|
|
30
|
+
});
|
|
31
|
+
case "streamable-http":
|
|
32
|
+
return new StreamableHTTPClientTransport(new URL(config.url), {
|
|
33
|
+
requestInit: config.headers ? { headers: config.headers } : void 0
|
|
34
|
+
});
|
|
35
|
+
default:
|
|
36
|
+
throw new Error(`Unknown MCP transport: ${config.transport}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function connectMcpServers(configs, _clientFactory, hooks) {
|
|
40
|
+
const connections = [];
|
|
41
|
+
const tools = {};
|
|
42
|
+
const errors = [];
|
|
43
|
+
for (const config of configs) {
|
|
44
|
+
try {
|
|
45
|
+
const client = _clientFactory ? _clientFactory() : new Client({ name: "zidane", version: "1.0.0" });
|
|
46
|
+
const transport = createTransport(config);
|
|
47
|
+
await client.connect(transport);
|
|
48
|
+
connections.push({ name: config.name, client });
|
|
49
|
+
const { tools: mcpTools } = await client.listTools();
|
|
50
|
+
const toolNames = [];
|
|
51
|
+
for (const tool of mcpTools) {
|
|
52
|
+
const namespacedName = `mcp_${config.name}_${tool.name}`;
|
|
53
|
+
toolNames.push(namespacedName);
|
|
54
|
+
tools[namespacedName] = {
|
|
55
|
+
spec: {
|
|
56
|
+
name: namespacedName,
|
|
57
|
+
description: tool.description || "",
|
|
58
|
+
input_schema: tool.inputSchema ?? { type: "object", properties: {} }
|
|
59
|
+
},
|
|
60
|
+
execute: async (input, _ctx) => {
|
|
61
|
+
await hooks?.callHook("mcp:tool:before", { server: config.name, tool: tool.name, input });
|
|
62
|
+
try {
|
|
63
|
+
const result = await client.callTool({ name: tool.name, arguments: input });
|
|
64
|
+
const output = resultToString(result.content);
|
|
65
|
+
await hooks?.callHook("mcp:tool:after", { server: config.name, tool: tool.name, input, result: output });
|
|
66
|
+
return output;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
await hooks?.callHook("mcp:tool:error", { server: config.name, tool: tool.name, input, error: err });
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
await hooks?.callHook("mcp:connect", {
|
|
75
|
+
name: config.name,
|
|
76
|
+
transport: config.transport,
|
|
77
|
+
tools: toolNames
|
|
78
|
+
});
|
|
79
|
+
} catch (err) {
|
|
80
|
+
errors.push({ name: config.name, error: err });
|
|
81
|
+
await hooks?.callHook("mcp:error", { name: config.name, error: err });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (errors.length > 0 && connections.length === 0) {
|
|
85
|
+
const messages = errors.map((e) => `${e.name}: ${e.error.message}`).join(", ");
|
|
86
|
+
throw new Error(`All MCP servers failed to connect: ${messages}`);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
tools,
|
|
90
|
+
close: async () => {
|
|
91
|
+
await Promise.allSettled(
|
|
92
|
+
connections.map(async ({ name, client }) => {
|
|
93
|
+
await hooks?.callHook("mcp:close", { name });
|
|
94
|
+
await client.close();
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
var init_mcp = __esm({
|
|
101
|
+
"src/mcp/index.ts"() {
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
resultToString,
|
|
107
|
+
connectMcpServers,
|
|
108
|
+
init_mcp
|
|
109
|
+
};
|