@yaks/harness 0.0.0-stage → 0.2.36
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/FRONTEND.md +170 -0
- package/LICENSE +202 -0
- package/MCPAuthPanel.js +166 -0
- package/MCPAuthPanel.js.map +1 -0
- package/MCPAuthPanel.ts +184 -0
- package/ModelPanel.js +124 -0
- package/ModelPanel.js.map +1 -0
- package/ModelPanel.ts +143 -0
- package/PersonaPanel.js +106 -0
- package/PersonaPanel.js.map +1 -0
- package/PersonaPanel.ts +110 -0
- package/README.md +1570 -2
- package/RuntimePanel.js +180 -0
- package/RuntimePanel.js.map +1 -0
- package/RuntimePanel.ts +205 -0
- package/STREAMING.md +101 -0
- package/WINDOWS.md +78 -0
- package/WORKER.md +221 -0
- package/_dist/agent.d.ts +197 -0
- package/_dist/agent.d.ts.map +1 -0
- package/_dist/cli.d.ts +10 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/conversations.d.ts +45 -0
- package/_dist/conversations.d.ts.map +1 -0
- package/_dist/default.d.ts +32 -0
- package/_dist/default.d.ts.map +1 -0
- package/_dist/detail.d.ts +16 -0
- package/_dist/detail.d.ts.map +1 -0
- package/_dist/diagnostics.d.ts +5 -0
- package/_dist/diagnostics.d.ts.map +1 -0
- package/_dist/effects.d.ts +35 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/http.d.ts +12 -0
- package/_dist/http.d.ts.map +1 -0
- package/_dist/isolated.d.ts +6 -0
- package/_dist/isolated.d.ts.map +1 -0
- package/_dist/local.d.ts +74 -0
- package/_dist/local.d.ts.map +1 -0
- package/_dist/machine.d.ts +3 -0
- package/_dist/machine.d.ts.map +1 -0
- package/_dist/mcp_auth.d.ts +8 -0
- package/_dist/mcp_auth.d.ts.map +1 -0
- package/_dist/mod.d.ts +43 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/model_selection.d.ts +8 -0
- package/_dist/model_selection.d.ts.map +1 -0
- package/_dist/routes.d.ts +11 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/runs.d.ts +7 -0
- package/_dist/runs.d.ts.map +1 -0
- package/_dist/runtime.d.ts +2 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/session_machines.d.ts +18 -0
- package/_dist/session_machines.d.ts.map +1 -0
- package/_dist/skills.d.ts +11 -0
- package/_dist/skills.d.ts.map +1 -0
- package/_dist/store.d.ts +38 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/tools.d.ts +28 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/view.d.ts +5 -0
- package/_dist/view.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/worn.d.ts +52 -0
- package/_dist/worn.d.ts.map +1 -0
- package/agent.js +773 -0
- package/agent.js.map +1 -0
- package/agent.ts +959 -0
- package/app.js +811 -0
- package/app.js.map +1 -0
- package/app.ts +868 -0
- package/artifact_tools.js +186 -0
- package/artifact_tools.js.map +1 -0
- package/artifact_tools.ts +190 -0
- package/backend_worker.ts +286 -0
- package/cli.js +43 -0
- package/cli.js.map +1 -0
- package/cli.ts +48 -0
- package/conversations.js +526 -0
- package/conversations.js.map +1 -0
- package/conversations.ts +576 -0
- package/conversations_testing.ts +162 -0
- package/default.js +162 -0
- package/default.js.map +1 -0
- package/default.ts +168 -0
- package/deferred_model.ts +1 -0
- package/deno.json +1 -0
- package/detail.js +50 -0
- package/detail.js.map +1 -0
- package/detail.ts +75 -0
- package/diagnostics.js +132 -0
- package/diagnostics.js.map +1 -0
- package/diagnostics.ts +150 -0
- package/draft_vault.js +149 -0
- package/draft_vault.js.map +1 -0
- package/draft_vault.ts +176 -0
- package/effects.js +233 -0
- package/effects.js.map +1 -0
- package/effects.ts +250 -0
- package/frontend/vocab.json +376 -0
- package/frontend.js +511 -0
- package/frontend.js.map +1 -0
- package/frontend.ts +389 -0
- package/http.js +107 -0
- package/http.js.map +1 -0
- package/http.ts +105 -0
- package/input_text.js +91 -0
- package/input_text.js.map +1 -0
- package/input_text.ts +99 -0
- package/instructions.js +111 -0
- package/instructions.js.map +1 -0
- package/instructions.ts +145 -0
- package/isolated.js +512 -0
- package/isolated.js.map +1 -0
- package/isolated.ts +502 -0
- package/keyboard.js +544 -0
- package/keyboard.js.map +1 -0
- package/keyboard.ts +403 -0
- package/legacy_instructions.js +3 -0
- package/legacy_instructions.js.map +1 -0
- package/legacy_instructions.ts +13 -0
- package/local.js +181 -0
- package/local.js.map +1 -0
- package/local.ts +282 -0
- package/machine.js +3 -0
- package/machine.js.map +1 -0
- package/machine.ts +8 -0
- package/mcp.js +123 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +141 -0
- package/mcp_auth.js +63 -0
- package/mcp_auth.js.map +1 -0
- package/mcp_auth.ts +91 -0
- package/mcp_authorize.js +17 -0
- package/mcp_authorize.js.map +1 -0
- package/mcp_authorize.ts +18 -0
- package/mcp_registry.js +178 -0
- package/mcp_registry.js.map +1 -0
- package/mcp_registry.ts +193 -0
- package/mod.js +43 -0
- package/mod.js.map +1 -0
- package/mod.ts +45 -0
- package/model_selection.js +72 -0
- package/model_selection.js.map +1 -0
- package/model_selection.ts +86 -0
- package/numbers.js +6 -0
- package/numbers.js.map +1 -0
- package/numbers.ts +8 -0
- package/package.json +106 -4
- package/panels.js +119 -0
- package/panels.js.map +1 -0
- package/panels.ts +259 -0
- package/patch.js +329 -0
- package/patch.js.map +1 -0
- package/patch.ts +325 -0
- package/perf.ts +65 -0
- package/providers.js +36 -0
- package/providers.js.map +1 -0
- package/providers.ts +53 -0
- package/remote.js +441 -0
- package/remote.js.map +1 -0
- package/remote.ts +446 -0
- package/results.js +49 -0
- package/results.js.map +1 -0
- package/results.ts +39 -0
- package/routes.js +64 -0
- package/routes.js.map +1 -0
- package/routes.ts +87 -0
- package/runs.js +478 -0
- package/runs.js.map +1 -0
- package/runs.ts +479 -0
- package/runtime/vocab.json +36 -0
- package/runtime.js +105 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +112 -0
- package/session_machines.js +203 -0
- package/session_machines.js.map +1 -0
- package/session_machines.ts +216 -0
- package/sheet.js +55 -0
- package/sheet.js.map +1 -0
- package/sheet.ts +31 -0
- package/shutdown.js +7 -0
- package/shutdown.js.map +1 -0
- package/shutdown.ts +7 -0
- package/skills.js +116 -0
- package/skills.js.map +1 -0
- package/skills.ts +114 -0
- package/startup_bench.ts +140 -0
- package/status.js +82 -0
- package/status.js.map +1 -0
- package/status.ts +97 -0
- package/steps.baseline.json +28 -0
- package/steps_bench.ts +155 -0
- package/store.js +31 -0
- package/store.js.map +1 -0
- package/store.ts +82 -0
- package/streaming.js +3 -0
- package/streaming.js.map +1 -0
- package/streaming.ts +8 -0
- package/streaming_bench.ts +76 -0
- package/switch_bench.ts +97 -0
- package/testing.ts +177 -0
- package/tools.js +287 -0
- package/tools.js.map +1 -0
- package/tools.ts +313 -0
- package/transcript.js +149 -0
- package/transcript.js.map +1 -0
- package/transcript.ts +187 -0
- package/tree.js +97 -0
- package/tree.js.map +1 -0
- package/tree.ts +114 -0
- package/unfinished.js +186 -0
- package/unfinished.js.map +1 -0
- package/unfinished.ts +198 -0
- package/view.js +92 -0
- package/view.js.map +1 -0
- package/view.ts +102 -0
- package/vocab.js +23 -0
- package/vocab.js.map +1 -0
- package/vocab.json +494 -0
- package/vocab.ts +22 -0
- package/window_bench.ts +114 -0
- package/worker_bench.ts +72 -0
- package/workspace.js +21 -0
- package/workspace.js.map +1 -0
- package/workspace.ts +17 -0
- package/worn.js +194 -0
- package/worn.js.map +1 -0
- package/worn.ts +246 -0
package/tools.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { derivedEid, mint } from "@yaks/graph/identity";
|
|
2
|
+
import { toolName } from "@yaks/graph/call";
|
|
3
|
+
import { line } from "@yaks/query";
|
|
4
|
+
import { answerOf, faultOf, toolEid, worded } from "@yaks/tools/result";
|
|
5
|
+
import { Interrupted } from "@yaks/tools/errors";
|
|
6
|
+
import { runner } from "@yaks/tools";
|
|
7
|
+
import { artifactTools } from "./artifact_tools.js";
|
|
8
|
+
import { skillRead } from "./skills.js";
|
|
9
|
+
import { valueTools } from "@yaks/blob";
|
|
10
|
+
// What the agent can do here: run a program, and read and write its own graph.
|
|
11
|
+
//
|
|
12
|
+
// Both halves already exist — ./machine.ts declares the machine tools over
|
|
13
|
+
// whatever machine the host lends, @yaks/mcp declares the generic graph tools —
|
|
14
|
+
// and the only
|
|
15
|
+
// thing between them is a difference of format. A graph tool may declare its
|
|
16
|
+
// arguments as a Zod schema, which the MCP SDK takes; a model wants JSON
|
|
17
|
+
// Schema. So this file is one adapter: `graphTools` declares each
|
|
18
|
+
// tool with its arguments as JSON Schema, writes a call entity for each call
|
|
19
|
+
// and records what it returned.
|
|
20
|
+
//
|
|
21
|
+
// The model never calls a tool function directly here either. A call is an
|
|
22
|
+
// entity attributed to the session that asked for it, so what the tool writes
|
|
23
|
+
// is written in the agent's name and not the runner's, and the transcript's own
|
|
24
|
+
// record of the call is the same entity @yaks/tools' runner answered.
|
|
25
|
+
//
|
|
26
|
+
// The conversion is not written here. `inputSchemaOf` (@yaks/tools/schema) is
|
|
27
|
+
// what an MCP `tools/list` sends, with references inlined: a provider reads a
|
|
28
|
+
// tool's parameters on its own, without a document to resolve `$ref` against.
|
|
29
|
+
// That subpath loads no MCP SDK, so a session that serves nothing never pays
|
|
30
|
+
// for a server.
|
|
31
|
+
import { machines } from "./session_machines.js";
|
|
32
|
+
import { core } from "@yaks/mcp/tools";
|
|
33
|
+
import { inputSchemaOf } from "@yaks/tools/schema";
|
|
34
|
+
import { effectResult, requestEffect } from "@yaks/effects/invocation";
|
|
35
|
+
import { argsOf, who } from "@yaks/graph";
|
|
36
|
+
import { workspaceTools } from "@yaks/machine/tools";
|
|
37
|
+
import { sessionHome, targetFor } from "@yaks/machine/targets";
|
|
38
|
+
import { sessionTools, speaking, ToolError } from "@yaks/session";
|
|
39
|
+
// A graph call that failed, thrown again as what it was, so the session's own
|
|
40
|
+
// runner records it as that rather than as words it answered: a refusal keeps
|
|
41
|
+
// its code, interrupted work stays interrupted, and a defect is a defect.
|
|
42
|
+
let failure = (fault)=>{
|
|
43
|
+
let words = String(fault.content?.body ?? '');
|
|
44
|
+
let refusal = fault.refusal;
|
|
45
|
+
let exception = fault.exception;
|
|
46
|
+
if (refusal) return new ToolError(String(refusal.code), words);
|
|
47
|
+
if (!exception) return new Interrupted(words);
|
|
48
|
+
return Object.assign(new Error(String(exception.value ?? words)), {
|
|
49
|
+
name: String(exception.type ?? 'Error')
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Graph tools as tools a model may call: the generic tier (`graph_apply`,
|
|
54
|
+
* `graph_query`, `graph_show`, `graph_schema`) where `tools` names none, or
|
|
55
|
+
* the ones it names (a host's plugins' own, `memory_around` among them). The
|
|
56
|
+
* agent reads and writes the same graph its transcript lives in.
|
|
57
|
+
*/ export let graphTools = (g, opts = {})=>{
|
|
58
|
+
let tier = opts.tools ?? core({
|
|
59
|
+
vocab: g.vocab
|
|
60
|
+
});
|
|
61
|
+
// A runner: these calls are this agent's own, and `call()` runs them here.
|
|
62
|
+
// Nothing sweeps a queue from inside an agent — a call somebody else wrote
|
|
63
|
+
// is for whoever works the effects to notice, by handling the same rules.
|
|
64
|
+
// What the host's own runner adds to a direct call's answer, this one adds
|
|
65
|
+
// too, so the agent reads what any caller reads.
|
|
66
|
+
let r = runner(g, {
|
|
67
|
+
tools: tier,
|
|
68
|
+
host: g,
|
|
69
|
+
...opts.reply ? {
|
|
70
|
+
reply: opts.reply
|
|
71
|
+
} : {}
|
|
72
|
+
});
|
|
73
|
+
return r.registry.list().map((t)=>({
|
|
74
|
+
name: toolName(t),
|
|
75
|
+
...t.title ? {
|
|
76
|
+
title: t.title
|
|
77
|
+
} : {},
|
|
78
|
+
...t.step ? {
|
|
79
|
+
step: t.step
|
|
80
|
+
} : {},
|
|
81
|
+
description: t.description,
|
|
82
|
+
parameters: inputSchemaOf(t),
|
|
83
|
+
...t.readOnly ? {
|
|
84
|
+
readOnly: true
|
|
85
|
+
} : {},
|
|
86
|
+
// A call, signed as the session that asked: `by` the actor it works for
|
|
87
|
+
// and `via` the session itself (@yaks/session `speaking`), so the runner
|
|
88
|
+
// lands what the function answered in that actor's name.
|
|
89
|
+
run: async (args, call)=>{
|
|
90
|
+
let actor = call?.session ? await speaking(g, {
|
|
91
|
+
entity: {
|
|
92
|
+
eid: call.session
|
|
93
|
+
}
|
|
94
|
+
}) : opts.actor ? {
|
|
95
|
+
by: opts.actor.eid
|
|
96
|
+
} : null;
|
|
97
|
+
await r.ensure();
|
|
98
|
+
let id = call?.call ? derivedEid(`harness graph tool ${call.call.entity.eid}`) : mint();
|
|
99
|
+
let landed = await r.call({
|
|
100
|
+
entity: {
|
|
101
|
+
eid: id
|
|
102
|
+
},
|
|
103
|
+
call: {
|
|
104
|
+
to: toolEid(toolName(t)),
|
|
105
|
+
args: args ?? {}
|
|
106
|
+
},
|
|
107
|
+
...actor ? {
|
|
108
|
+
$actor: actor
|
|
109
|
+
} : {}
|
|
110
|
+
});
|
|
111
|
+
let fault = faultOf(landed, id);
|
|
112
|
+
if (fault) throw failure(fault);
|
|
113
|
+
return worded(answerOf(landed, id), Infinity);
|
|
114
|
+
},
|
|
115
|
+
recover: async (_, ctx)=>{
|
|
116
|
+
let id = derivedEid(`harness graph tool ${ctx.call.entity.eid}`);
|
|
117
|
+
let [call] = await g.get([
|
|
118
|
+
id
|
|
119
|
+
]);
|
|
120
|
+
if (!call) return undefined;
|
|
121
|
+
let answer = await r.interruptCall(id, 'Graph tool execution was interrupted. Its write may have landed; ' + 'inspect the graph before repeating it.');
|
|
122
|
+
return worded(answerOf(answer, id), Infinity);
|
|
123
|
+
}
|
|
124
|
+
}));
|
|
125
|
+
};
|
|
126
|
+
/** The skill loader (./skills.ts `skillRead`) through the graph runner, so
|
|
127
|
+
* each load is attributed to the session that asked. */ export let skillTools = (g, reply)=>graphTools(g, {
|
|
128
|
+
reply,
|
|
129
|
+
tools: [
|
|
130
|
+
skillRead
|
|
131
|
+
]
|
|
132
|
+
});
|
|
133
|
+
let workspaceAnswer = (answer)=>{
|
|
134
|
+
if (answer.refusal || answer.exception) throw failure(answer);
|
|
135
|
+
return String(answer.content?.body ?? '');
|
|
136
|
+
};
|
|
137
|
+
let workspaceCall = (call)=>derivedEid('workspace effect|' + call.entity.eid);
|
|
138
|
+
/** Keep a session tool's transcript interface over a durable operation. */ export let workspaceTool = (g, tool, target)=>{
|
|
139
|
+
let requested = async (call)=>{
|
|
140
|
+
let [request] = await g.get([
|
|
141
|
+
workspaceCall(call)
|
|
142
|
+
], [
|
|
143
|
+
'call'
|
|
144
|
+
]);
|
|
145
|
+
return !!request?.call;
|
|
146
|
+
};
|
|
147
|
+
let asked = (ctx, args)=>requestEffect(g, tool.name, args, {
|
|
148
|
+
id: workspaceCall(ctx.call),
|
|
149
|
+
source: ctx.call.entity.eid,
|
|
150
|
+
actor: who(ctx.call),
|
|
151
|
+
signal: ctx.signal,
|
|
152
|
+
defer: ctx.deferred
|
|
153
|
+
});
|
|
154
|
+
return {
|
|
155
|
+
...tool,
|
|
156
|
+
defers: (call)=>!call.execution || requested(call),
|
|
157
|
+
run: async (args, ctx)=>ctx ? workspaceAnswer(await asked(ctx, ()=>target(args, ctx))) : await tool.run(args, ctx),
|
|
158
|
+
...tool.recover ? {
|
|
159
|
+
recover: async (args, ctx)=>{
|
|
160
|
+
let [answer] = await g.get([
|
|
161
|
+
effectResult(workspaceCall(ctx.call))
|
|
162
|
+
]);
|
|
163
|
+
if (answer?.result) return workspaceAnswer(answer);
|
|
164
|
+
if (!await requested(ctx.call)) return await tool.recover(args, ctx);
|
|
165
|
+
return workspaceAnswer(await asked(ctx, args));
|
|
166
|
+
}
|
|
167
|
+
} : {}
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
/** The machine, delegation, and the graph, with one wait for all three targets.
|
|
171
|
+
* react records every tool's wall-clock and projects it after output bounding;
|
|
172
|
+
* these raw answers remain parseable JSON for non-model callers. */ export let harnessTools = (g, opts = {})=>{
|
|
173
|
+
let binding = opts.machines ?? machines(g);
|
|
174
|
+
let selected = opts.workspaces ?? {
|
|
175
|
+
connect: ()=>{
|
|
176
|
+
throw new Error('This host lends no workspace execution binding');
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
let machine = workspaceTools(g, selected).map((tool)=>{
|
|
180
|
+
let fileOrCommand = [
|
|
181
|
+
'shell',
|
|
182
|
+
'read',
|
|
183
|
+
'write'
|
|
184
|
+
].includes(tool.name);
|
|
185
|
+
let home = async (args, ctx)=>{
|
|
186
|
+
if (!args.workspace && ctx && fileOrCommand) {
|
|
187
|
+
await binding.home(ctx.session);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
return opts.workspaceEffects ? workspaceTool(g, tool, async (args, ctx)=>{
|
|
191
|
+
await home(args, ctx);
|
|
192
|
+
return fileOrCommand ? {
|
|
193
|
+
...args,
|
|
194
|
+
...await targetFor(g, ctx.session, args)
|
|
195
|
+
} : args;
|
|
196
|
+
}) : {
|
|
197
|
+
...tool,
|
|
198
|
+
run: async (args, ctx)=>{
|
|
199
|
+
await home(args, ctx);
|
|
200
|
+
return tool.run(args, ctx);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
let session = sessionTools(g, {
|
|
205
|
+
...binding.limits,
|
|
206
|
+
...opts
|
|
207
|
+
});
|
|
208
|
+
let processWait = machine.find((t)=>t.name == 'wait');
|
|
209
|
+
let childWait = session.find((t)=>t.name == 'wait');
|
|
210
|
+
let wait = {
|
|
211
|
+
name: 'wait',
|
|
212
|
+
waits: true,
|
|
213
|
+
defers: (call)=>argsOf(call).process != null && (processWait.defers?.(call) ?? false),
|
|
214
|
+
description: 'Wait for a process OR named child sessions OR tasks, with a timeout in milliseconds.',
|
|
215
|
+
parameters: {
|
|
216
|
+
type: 'object',
|
|
217
|
+
properties: {
|
|
218
|
+
process: {
|
|
219
|
+
type: 'string'
|
|
220
|
+
},
|
|
221
|
+
children: {
|
|
222
|
+
type: 'array',
|
|
223
|
+
items: {
|
|
224
|
+
type: 'string'
|
|
225
|
+
},
|
|
226
|
+
minItems: 1
|
|
227
|
+
},
|
|
228
|
+
tasks: {
|
|
229
|
+
type: 'array',
|
|
230
|
+
items: {
|
|
231
|
+
type: 'string'
|
|
232
|
+
},
|
|
233
|
+
minItems: 1
|
|
234
|
+
},
|
|
235
|
+
timeout: {
|
|
236
|
+
type: 'number'
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
run: (args, ctx)=>{
|
|
241
|
+
if ([
|
|
242
|
+
args.children,
|
|
243
|
+
args.process,
|
|
244
|
+
args.tasks
|
|
245
|
+
].filter((v)=>v != null).length != 1) {
|
|
246
|
+
throw new ToolError('wait', 'name exactly one of process, children, or tasks');
|
|
247
|
+
}
|
|
248
|
+
return args.process != null ? processWait.run(args, ctx) : childWait.run(args, ctx);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
return [
|
|
252
|
+
...machine.map((t)=>t.name == 'wait' ? wait : t),
|
|
253
|
+
...session.filter((t)=>t.name != 'wait'),
|
|
254
|
+
{
|
|
255
|
+
name: 'session_home',
|
|
256
|
+
description: 'Set a session default workspace without moving files or stopping commands. Omitting cwd clears the previous directory.',
|
|
257
|
+
parameters: {
|
|
258
|
+
type: 'object',
|
|
259
|
+
properties: {
|
|
260
|
+
session: {
|
|
261
|
+
type: 'string'
|
|
262
|
+
},
|
|
263
|
+
workspace: {
|
|
264
|
+
type: 'string'
|
|
265
|
+
},
|
|
266
|
+
cwd: {
|
|
267
|
+
type: 'string'
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
required: [
|
|
271
|
+
'session',
|
|
272
|
+
'workspace'
|
|
273
|
+
]
|
|
274
|
+
},
|
|
275
|
+
run: async (args)=>{
|
|
276
|
+
await sessionHome(g, String(args.session), String(args.workspace), args.cwd == null ? undefined : String(args.cwd));
|
|
277
|
+
return 'Session home updated';
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
...artifactTools(g, opts),
|
|
281
|
+
...graphTools(g, {
|
|
282
|
+
reply: opts.reply
|
|
283
|
+
}).filter((t)=>t.name != 'session_home'),
|
|
284
|
+
...valueTools(async (entity)=>(await g.read(line`.entity.eid=${entity}`))[0])
|
|
285
|
+
];
|
|
286
|
+
};
|
|
287
|
+
//# sourceMappingURL=tools.js.map
|
package/tools.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,SAAS,UAAU,EAAE,IAAI,oCAAyC;AAClE,SAAS,QAAQ,gCAAqC;AACtD,SAAS,IAAI,2BAAgC;AAE7C,SAAS,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,kCAAuC;AAClF,SAAS,WAAW,kCAAuC;AAC3D,SAAS,MAAM,2BAAgC;AAC/C,SAAS,aAAa,8BAA6B;AACnD,SAAS,SAAS,sBAAqB;AAEvC,SAAqB,UAAU,0BAA+B;AAC9D,+EAA+E;AAC/E,EAAE;AACF,2EAA2E;AAC3E,gFAAgF;AAChF,eAAe;AACf,6EAA6E;AAC7E,yEAAyE;AACzE,kEAAkE;AAClE,6EAA6E;AAC7E,gCAAgC;AAChC,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,gFAAgF;AAChF,sEAAsE;AACtE,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,8EAA8E;AAC9E,6EAA6E;AAC7E,gBAAgB;AAEhB,SAAS,QAAQ,gCAAqD;AAQtE,SAAS,IAAI,+BAAoC;AACjD,SAAS,aAAa,kCAAuC;AAC7D,SAAS,YAAY,EAAE,aAAa,wCAA6C;AACjF,SAAS,MAAM,EAAE,GAAG,2BAAgC;AACpD,SAAS,cAAc,mCAAwC;AAC/D,SAAS,WAAW,EAAE,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,IACE;QAAC,KAAK,QAAQ;QAAE,KAAK,OAAO;QAAE,KAAK,KAAK;OAAC,CAAC,MAAM,CAAC,CAAC,IAAM,KAAK,MAC1D,MAAM,IAAI,GACb;QACA,MAAM,IAAI,UACR,QACA;MAEJ;MACA,OAAO,KAAK,OAAO,IAAI,OACnB,YAAY,GAAG,CAAC,MAAM,OACtB,UAAU,GAAG,CAAC,MAAM;IAC1B;EACF;EACA,OAAO;OACF,QAAQ,GAAG,CAAC,CAAC,IAAM,EAAE,IAAI,IAAI,SAAS,OAAO;OAC7C,QAAQ,MAAM,CAAC,CAAC,IAAM,EAAE,IAAI,IAAI;IACnC;MACE,MAAM;MACN,aACE;MACF,YAAY;QACV,MAAM;QACN,YAAY;UACV,SAAS;YAAE,MAAM;UAAS;UAC1B,WAAW;YAAE,MAAM;UAAS;UAC5B,KAAK;YAAE,MAAM;UAAS;QACxB;QACA,UAAU;UAAC;UAAW;SAAY;MACpC;MACA,KAAK,OAAO;QACV,MAAM,YACJ,GACA,OAAO,KAAK,OAAO,GACnB,OAAO,KAAK,SAAS,GACrB,KAAK,GAAG,IAAI,OAAO,YAAY,OAAO,KAAK,GAAG;QAEhD,OAAO;MACT;IACF;OACG,cAAc,GAAG;OACjB,WAAW,GAAG;MAAE,OAAO,KAAK,KAAK;IAAC,GAAG,MAAM,CAAC,CAAC,IAC9C,EAAE,IAAI,IAAI;OAET,WAAW,OAAO,SACnB,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE;GAEjD;AACH,EAAC"}
|
package/tools.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { derivedEid, mint } from '@yaks/graph/identity'
|
|
2
|
+
import { toolName } from '@yaks/graph/call'
|
|
3
|
+
import { line } from '@yaks/query'
|
|
4
|
+
import type { Reply } from '@yaks/tools'
|
|
5
|
+
import { answerOf, faultOf, toolEid, worded } from '@yaks/tools/result'
|
|
6
|
+
import { Interrupted } from '@yaks/tools/errors'
|
|
7
|
+
import { runner } from '@yaks/tools'
|
|
8
|
+
import { artifactTools } from './artifact_tools.js'
|
|
9
|
+
import { skillRead } from './skills.js'
|
|
10
|
+
|
|
11
|
+
import { type Blobs, valueTools } from '@yaks/blob'
|
|
12
|
+
// What the agent can do here: run a program, and read and write its own graph.
|
|
13
|
+
//
|
|
14
|
+
// Both halves already exist — ./machine.ts declares the machine tools over
|
|
15
|
+
// whatever machine the host lends, @yaks/mcp declares the generic graph tools —
|
|
16
|
+
// and the only
|
|
17
|
+
// thing between them is a difference of format. A graph tool may declare its
|
|
18
|
+
// arguments as a Zod schema, which the MCP SDK takes; a model wants JSON
|
|
19
|
+
// Schema. So this file is one adapter: `graphTools` declares each
|
|
20
|
+
// tool with its arguments as JSON Schema, writes a call entity for each call
|
|
21
|
+
// and records what it returned.
|
|
22
|
+
//
|
|
23
|
+
// The model never calls a tool function directly here either. A call is an
|
|
24
|
+
// entity attributed to the session that asked for it, so what the tool writes
|
|
25
|
+
// is written in the agent's name and not the runner's, and the transcript's own
|
|
26
|
+
// record of the call is the same entity @yaks/tools' runner answered.
|
|
27
|
+
//
|
|
28
|
+
// The conversion is not written here. `inputSchemaOf` (@yaks/tools/schema) is
|
|
29
|
+
// what an MCP `tools/list` sends, with references inlined: a provider reads a
|
|
30
|
+
// tool's parameters on its own, without a document to resolve `$ref` against.
|
|
31
|
+
// That subpath loads no MCP SDK, so a session that serves nothing never pays
|
|
32
|
+
// for a server.
|
|
33
|
+
|
|
34
|
+
import { machines, type SessionMachines } from './session_machines.js'
|
|
35
|
+
import type {
|
|
36
|
+
Bundle,
|
|
37
|
+
Comp,
|
|
38
|
+
Entity,
|
|
39
|
+
Graph,
|
|
40
|
+
Tool as GraphTool,
|
|
41
|
+
} from '@yaks/graph'
|
|
42
|
+
import { core } from '@yaks/mcp/tools'
|
|
43
|
+
import { inputSchemaOf } from '@yaks/tools/schema'
|
|
44
|
+
import { effectResult, requestEffect } from '@yaks/effects/invocation'
|
|
45
|
+
import { argsOf, who } from '@yaks/graph'
|
|
46
|
+
import { workspaceTools } from '@yaks/machine/tools'
|
|
47
|
+
import { sessionHome, targetFor, type Workspaces } from '@yaks/machine/targets'
|
|
48
|
+
import {
|
|
49
|
+
type ChildLimits,
|
|
50
|
+
sessionTools,
|
|
51
|
+
speaking,
|
|
52
|
+
type Tool,
|
|
53
|
+
type ToolContext,
|
|
54
|
+
ToolError,
|
|
55
|
+
} from '@yaks/session'
|
|
56
|
+
|
|
57
|
+
// A graph call that failed, thrown again as what it was, so the session's own
|
|
58
|
+
// runner records it as that rather than as words it answered: a refusal keeps
|
|
59
|
+
// its code, interrupted work stays interrupted, and a defect is a defect.
|
|
60
|
+
let failure = (fault: Bundle): Error => {
|
|
61
|
+
let words = String((fault.content as Comp | undefined)?.body ?? '')
|
|
62
|
+
let refusal = fault.refusal as Comp | undefined
|
|
63
|
+
let exception = fault.exception as Comp | undefined
|
|
64
|
+
if (refusal) return new ToolError(String(refusal.code), words)
|
|
65
|
+
if (!exception) return new Interrupted(words)
|
|
66
|
+
return Object.assign(new Error(String(exception.value ?? words)), {
|
|
67
|
+
name: String(exception.type ?? 'Error'),
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Graph tools as tools a model may call: the generic tier (`graph_apply`,
|
|
73
|
+
* `graph_query`, `graph_show`, `graph_schema`) where `tools` names none, or
|
|
74
|
+
* the ones it names (a host's plugins' own, `memory_around` among them). The
|
|
75
|
+
* agent reads and writes the same graph its transcript lives in.
|
|
76
|
+
*/
|
|
77
|
+
export let graphTools = (
|
|
78
|
+
g: Graph,
|
|
79
|
+
opts: {
|
|
80
|
+
actor?: Entity | null
|
|
81
|
+
reply?: Reply
|
|
82
|
+
tools?: GraphTool[]
|
|
83
|
+
} = {},
|
|
84
|
+
): Tool[] => {
|
|
85
|
+
let tier = opts.tools ??
|
|
86
|
+
core({ vocab: g.vocab })
|
|
87
|
+
// A runner: these calls are this agent's own, and `call()` runs them here.
|
|
88
|
+
// Nothing sweeps a queue from inside an agent — a call somebody else wrote
|
|
89
|
+
// is for whoever works the effects to notice, by handling the same rules.
|
|
90
|
+
// What the host's own runner adds to a direct call's answer, this one adds
|
|
91
|
+
// too, so the agent reads what any caller reads.
|
|
92
|
+
let r = runner(g, {
|
|
93
|
+
tools: tier,
|
|
94
|
+
host: g,
|
|
95
|
+
...opts.reply ? { reply: opts.reply } : {},
|
|
96
|
+
})
|
|
97
|
+
return r.registry.list().map((t) => ({
|
|
98
|
+
name: toolName(t),
|
|
99
|
+
...t.title ? { title: t.title } : {},
|
|
100
|
+
...t.step ? { step: t.step } : {},
|
|
101
|
+
description: t.description,
|
|
102
|
+
parameters: inputSchemaOf(t),
|
|
103
|
+
...t.readOnly ? { readOnly: true } : {},
|
|
104
|
+
// A call, signed as the session that asked: `by` the actor it works for
|
|
105
|
+
// and `via` the session itself (@yaks/session `speaking`), so the runner
|
|
106
|
+
// lands what the function answered in that actor's name.
|
|
107
|
+
run: async (args: Record<string, unknown>, call) => {
|
|
108
|
+
let actor = call?.session
|
|
109
|
+
? await speaking(g, { entity: { eid: call.session } })
|
|
110
|
+
: opts.actor
|
|
111
|
+
? { by: opts.actor.eid }
|
|
112
|
+
: null
|
|
113
|
+
await r.ensure()
|
|
114
|
+
let id = call?.call
|
|
115
|
+
? derivedEid(`harness graph tool ${call.call.entity.eid}`)
|
|
116
|
+
: mint()
|
|
117
|
+
let landed = await r.call({
|
|
118
|
+
entity: { eid: id },
|
|
119
|
+
call: { to: toolEid(toolName(t)), args: args ?? {} },
|
|
120
|
+
...(actor ? { $actor: actor } : {}),
|
|
121
|
+
})
|
|
122
|
+
let fault = faultOf(landed, id)
|
|
123
|
+
if (fault) throw failure(fault)
|
|
124
|
+
return worded(answerOf(landed, id), Infinity)
|
|
125
|
+
},
|
|
126
|
+
recover: async (_, ctx) => {
|
|
127
|
+
let id = derivedEid(`harness graph tool ${ctx.call.entity.eid}`)
|
|
128
|
+
let [call] = await g.get([id])
|
|
129
|
+
if (!call) return undefined
|
|
130
|
+
let answer = await r.interruptCall(
|
|
131
|
+
id,
|
|
132
|
+
'Graph tool execution was interrupted. Its write may have landed; ' +
|
|
133
|
+
'inspect the graph before repeating it.',
|
|
134
|
+
)
|
|
135
|
+
return worded(answerOf(answer, id), Infinity)
|
|
136
|
+
},
|
|
137
|
+
}))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The skill loader (./skills.ts `skillRead`) through the graph runner, so
|
|
141
|
+
* each load is attributed to the session that asked. */
|
|
142
|
+
export let skillTools = (g: Graph, reply?: Reply): Tool[] =>
|
|
143
|
+
graphTools(g, { reply, tools: [skillRead] })
|
|
144
|
+
|
|
145
|
+
let workspaceAnswer = (answer: Bundle): string => {
|
|
146
|
+
if (answer.refusal || answer.exception) throw failure(answer)
|
|
147
|
+
return String((answer.content as Comp | undefined)?.body ?? '')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let workspaceCall = (call: Bundle) =>
|
|
151
|
+
derivedEid('workspace effect|' + call.entity.eid)
|
|
152
|
+
|
|
153
|
+
/** Keep a session tool's transcript interface over a durable operation. */
|
|
154
|
+
export let workspaceTool = (
|
|
155
|
+
g: Graph,
|
|
156
|
+
tool: Tool,
|
|
157
|
+
target: (
|
|
158
|
+
args: Comp,
|
|
159
|
+
ctx: ToolContext,
|
|
160
|
+
) => Promise<Comp>,
|
|
161
|
+
): Tool => {
|
|
162
|
+
let requested = async (call: Bundle) => {
|
|
163
|
+
let [request] = await g.get([workspaceCall(call)], ['call'])
|
|
164
|
+
return !!request?.call
|
|
165
|
+
}
|
|
166
|
+
let asked = (
|
|
167
|
+
ctx: ToolContext,
|
|
168
|
+
args: Comp | (() => Promise<Comp>),
|
|
169
|
+
) =>
|
|
170
|
+
requestEffect(g, tool.name, args, {
|
|
171
|
+
id: workspaceCall(ctx.call),
|
|
172
|
+
source: ctx.call.entity.eid,
|
|
173
|
+
actor: who(ctx.call),
|
|
174
|
+
signal: ctx.signal,
|
|
175
|
+
defer: ctx.deferred,
|
|
176
|
+
})
|
|
177
|
+
return {
|
|
178
|
+
...tool,
|
|
179
|
+
defers: (call) => !call.execution || requested(call),
|
|
180
|
+
run: async (args, ctx) =>
|
|
181
|
+
ctx
|
|
182
|
+
? workspaceAnswer(await asked(ctx, () => target(args, ctx)))
|
|
183
|
+
: await tool.run(args, ctx),
|
|
184
|
+
...tool.recover
|
|
185
|
+
? {
|
|
186
|
+
recover: async (args: Comp, ctx: ToolContext) => {
|
|
187
|
+
let [answer] = await g.get([effectResult(workspaceCall(ctx.call))])
|
|
188
|
+
if (answer?.result) return workspaceAnswer(answer)
|
|
189
|
+
if (!await requested(ctx.call)) return await tool.recover!(args, ctx)
|
|
190
|
+
return workspaceAnswer(await asked(ctx, args))
|
|
191
|
+
},
|
|
192
|
+
}
|
|
193
|
+
: {},
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The machine, delegation, and the graph, with one wait for all three targets.
|
|
198
|
+
* react records every tool's wall-clock and projects it after output bounding;
|
|
199
|
+
* these raw answers remain parseable JSON for non-model callers. */
|
|
200
|
+
export let harnessTools = (
|
|
201
|
+
g: Graph,
|
|
202
|
+
opts:
|
|
203
|
+
& {
|
|
204
|
+
cwd?: string
|
|
205
|
+
/** where artifacts' bytes are kept (the harness's `artifacts`) */
|
|
206
|
+
artifacts?: Blobs
|
|
207
|
+
/** the root a task child's checkout is cut under */
|
|
208
|
+
machines?: SessionMachines
|
|
209
|
+
workspaces?: Workspaces
|
|
210
|
+
workspaceEffects?: boolean
|
|
211
|
+
/** what a direct graph tool call is owed beside its answer (the
|
|
212
|
+
* harness's `reply`) */
|
|
213
|
+
reply?: Reply
|
|
214
|
+
}
|
|
215
|
+
& ChildLimits = {},
|
|
216
|
+
): Tool[] => {
|
|
217
|
+
let binding = opts.machines ?? machines(g)
|
|
218
|
+
let selected = opts.workspaces ?? {
|
|
219
|
+
connect: () => {
|
|
220
|
+
throw new Error('This host lends no workspace execution binding')
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
let machine: Tool[] = workspaceTools(g, selected).map((tool) => {
|
|
224
|
+
let fileOrCommand = ['shell', 'read', 'write'].includes(tool.name)
|
|
225
|
+
let home = async (args: Comp, ctx?: ToolContext) => {
|
|
226
|
+
if (!args.workspace && ctx && fileOrCommand) {
|
|
227
|
+
await binding.home(ctx.session)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return opts.workspaceEffects
|
|
231
|
+
? workspaceTool(g, tool, async (args, ctx) => {
|
|
232
|
+
await home(args, ctx)
|
|
233
|
+
return fileOrCommand
|
|
234
|
+
? { ...args, ...await targetFor(g, ctx.session, args) }
|
|
235
|
+
: args
|
|
236
|
+
})
|
|
237
|
+
: {
|
|
238
|
+
...tool,
|
|
239
|
+
run: async (args, ctx) => {
|
|
240
|
+
await home(args, ctx)
|
|
241
|
+
return tool.run(args, ctx)
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
})
|
|
245
|
+
let session = sessionTools(g, { ...binding.limits, ...opts })
|
|
246
|
+
let processWait = machine.find((t) => t.name == 'wait')!
|
|
247
|
+
let childWait = session.find((t) => t.name == 'wait')!
|
|
248
|
+
let wait: Tool = {
|
|
249
|
+
name: 'wait',
|
|
250
|
+
waits: true,
|
|
251
|
+
defers: (call) =>
|
|
252
|
+
argsOf(call).process != null && (processWait.defers?.(call) ?? false),
|
|
253
|
+
description:
|
|
254
|
+
'Wait for a process OR named child sessions OR tasks, with a timeout in milliseconds.',
|
|
255
|
+
parameters: {
|
|
256
|
+
type: 'object',
|
|
257
|
+
properties: {
|
|
258
|
+
process: { type: 'string' },
|
|
259
|
+
children: { type: 'array', items: { type: 'string' }, minItems: 1 },
|
|
260
|
+
tasks: { type: 'array', items: { type: 'string' }, minItems: 1 },
|
|
261
|
+
timeout: { type: 'number' },
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
run: (args, ctx) => {
|
|
265
|
+
if (
|
|
266
|
+
[args.children, args.process, args.tasks].filter((v) => v != null)
|
|
267
|
+
.length != 1
|
|
268
|
+
) {
|
|
269
|
+
throw new ToolError(
|
|
270
|
+
'wait',
|
|
271
|
+
'name exactly one of process, children, or tasks',
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
return args.process != null
|
|
275
|
+
? processWait.run(args, ctx)
|
|
276
|
+
: childWait.run(args, ctx)
|
|
277
|
+
},
|
|
278
|
+
}
|
|
279
|
+
return [
|
|
280
|
+
...machine.map((t) => t.name == 'wait' ? wait : t),
|
|
281
|
+
...session.filter((t) => t.name != 'wait'),
|
|
282
|
+
{
|
|
283
|
+
name: 'session_home',
|
|
284
|
+
description:
|
|
285
|
+
'Set a session default workspace without moving files or stopping commands. Omitting cwd clears the previous directory.',
|
|
286
|
+
parameters: {
|
|
287
|
+
type: 'object',
|
|
288
|
+
properties: {
|
|
289
|
+
session: { type: 'string' },
|
|
290
|
+
workspace: { type: 'string' },
|
|
291
|
+
cwd: { type: 'string' },
|
|
292
|
+
},
|
|
293
|
+
required: ['session', 'workspace'],
|
|
294
|
+
},
|
|
295
|
+
run: async (args) => {
|
|
296
|
+
await sessionHome(
|
|
297
|
+
g,
|
|
298
|
+
String(args.session),
|
|
299
|
+
String(args.workspace),
|
|
300
|
+
args.cwd == null ? undefined : String(args.cwd),
|
|
301
|
+
)
|
|
302
|
+
return 'Session home updated'
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
...artifactTools(g, opts),
|
|
306
|
+
...graphTools(g, { reply: opts.reply }).filter((t) =>
|
|
307
|
+
t.name != 'session_home'
|
|
308
|
+
),
|
|
309
|
+
...valueTools(async (entity) =>
|
|
310
|
+
(await g.read(line`.entity.eid=${entity}`))[0]
|
|
311
|
+
),
|
|
312
|
+
]
|
|
313
|
+
}
|