@adia-ai/agent 0.8.28 → 0.8.30
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/CHANGELOG.md +45 -0
- package/README.md +384 -11
- package/agent.d.ts +44 -3
- package/agent.d.ts.map +1 -1
- package/agent.js +172 -8
- package/agent.js.map +1 -1
- package/events.d.ts +46 -2
- package/events.d.ts.map +1 -1
- package/events.js +30 -1
- package/events.js.map +1 -1
- package/guardrail.d.ts +68 -0
- package/guardrail.d.ts.map +1 -0
- package/guardrail.js +79 -0
- package/guardrail.js.map +1 -0
- package/index.d.ts +15 -7
- package/index.d.ts.map +1 -1
- package/index.js +7 -3
- package/index.js.map +1 -1
- package/integrations.d.ts +19 -2
- package/integrations.d.ts.map +1 -1
- package/integrations.js +6 -1
- package/integrations.js.map +1 -1
- package/loop.d.ts +19 -8
- package/loop.d.ts.map +1 -1
- package/loop.js +176 -9
- package/loop.js.map +1 -1
- package/mcp-transport.d.ts +128 -0
- package/mcp-transport.d.ts.map +1 -0
- package/mcp-transport.js +361 -0
- package/mcp-transport.js.map +1 -0
- package/mcp.d.ts +120 -0
- package/mcp.d.ts.map +1 -0
- package/mcp.js +264 -0
- package/mcp.js.map +1 -0
- package/package.json +1 -1
- package/resource.d.ts +28 -3
- package/resource.d.ts.map +1 -1
- package/resource.js +43 -5
- package/resource.js.map +1 -1
- package/session.d.ts +18 -0
- package/session.d.ts.map +1 -1
- package/session.js +40 -0
- package/session.js.map +1 -1
- package/stub.d.ts +8 -0
- package/stub.d.ts.map +1 -1
- package/stub.js +13 -0
- package/stub.js.map +1 -1
- package/tools.d.ts +6 -0
- package/tools.d.ts.map +1 -1
- package/tools.js.map +1 -1
- package/trace.d.ts +50 -0
- package/trace.d.ts.map +1 -0
- package/trace.js +60 -0
- package/trace.js.map +1 -0
- package/workflow.d.ts +113 -11
- package/workflow.d.ts.map +1 -1
- package/workflow.js +93 -15
- package/workflow.js.map +1 -1
package/index.js
CHANGED
|
@@ -8,11 +8,15 @@
|
|
|
8
8
|
export { createAgent } from './agent.js';
|
|
9
9
|
export { promptLayer, composeSystem } from './prompt.js';
|
|
10
10
|
export { defineTool, executeTool, checkInput, toSpecs } from './tools.js';
|
|
11
|
-
export { defineWorkflow, runWorkflow } from './workflow.js';
|
|
11
|
+
export { defineWorkflow, runWorkflow, toVerdict } from './workflow.js';
|
|
12
|
+
export { defineGuardrail } from './guardrail.js';
|
|
13
|
+
export { createTracer, consoleSink, TRACE_PHASES, isTracePhase } from './trace.js';
|
|
12
14
|
export { defineResource } from './resource.js';
|
|
15
|
+
export { mcpServer, connectMcpSource } from './mcp.js';
|
|
16
|
+
export { connectStreamableHttp, McpError, McpConnectionError, McpCallError, DEFAULT_MCP_TIMEOUT_MS, } from './mcp-transport.js';
|
|
13
17
|
export { createIntegrationRegistry, resolveIntegrations, buildToolDispatch, MAX_INTEGRATIONS } from './integrations.js';
|
|
14
|
-
export { createSession, toJSON, fromJSON } from './session.js';
|
|
15
|
-
export { reduce, reduceAll, PROGRESS_STAGES, isProgressStage } from './events.js';
|
|
18
|
+
export { createSession, toJSON, fromJSON, assertJsonSafe } from './session.js';
|
|
19
|
+
export { reduce, reduceAll, remember, PROGRESS_STAGES, isProgressStage } from './events.js';
|
|
16
20
|
export { frameClientMessage, shouldRunTurn } from './frame.js';
|
|
17
21
|
export { scriptClient } from './stub.js';
|
|
18
22
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAE,c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
49
49
|
/** Display name for a human (picker, dropped-integration notice). */
|
|
50
50
|
label: string;
|
|
51
|
+
/** Where the AUTHORITATIVE input check lives.
|
|
52
|
+
*
|
|
53
|
+
* - `'local'` (default) — this process is the only validator, so
|
|
54
|
+
* registration enforces the allowlist below: a keyword `checkInput`
|
|
55
|
+
* cannot enforce would make the manifest's declared contract a lie.
|
|
56
|
+
* - `'remote'` — the executor validates on its own side and reports a
|
|
57
|
+
* rejection back as a normal error result (an MCP server does exactly
|
|
58
|
+
* this). The schema is then a FAITHFUL COPY of a contract someone else
|
|
59
|
+
* owns, and trimming it to this package's allowlist would ship the
|
|
60
|
+
* model a weaker description of the tool than the tool actually has.
|
|
61
|
+
* `checkInput` still runs as a cheap pre-filter on the subset it knows.
|
|
62
|
+
*
|
|
63
|
+
* Only a manifest whose executor genuinely re-validates may claim
|
|
64
|
+
* `'remote'` — it is a statement about the executor, not an escape hatch
|
|
65
|
+
* for a schema that failed registration. */
|
|
66
|
+
validation?: 'local' | 'remote';
|
|
51
67
|
auth?: {
|
|
52
68
|
kind: 'serverKey';
|
|
53
69
|
envKey: string;
|
|
@@ -64,8 +80,9 @@ export interface IntegrationContext extends ToolContext {
|
|
|
64
80
|
}
|
|
65
81
|
export interface IntegrationRegistry {
|
|
66
82
|
/** Boot-fail-fast: throws on a duplicate id, a duplicate tool.name, an
|
|
67
|
-
* inputSchema outside checkInput's supported subset
|
|
68
|
-
*
|
|
83
|
+
* inputSchema outside checkInput's supported subset (locally-validated
|
|
84
|
+
* manifests only — see `validation`), or a serverKey auth with a
|
|
85
|
+
* missing/empty envKey. Returns the manifest on success. */
|
|
69
86
|
register(manifest: IntegrationManifest): IntegrationManifest;
|
|
70
87
|
get(id: string): IntegrationManifest | undefined;
|
|
71
88
|
list(): IntegrationManifest[];
|
package/integrations.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integrations.d.ts","sourceRoot":"","sources":["src/integrations.ts"],"names":[],"mappings":"AAA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C,oBAAoB,CAyBtB;AAED;;;;;;;;;6EAS6E;AAC7E,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,mBAAmB,EAAE,EAC/B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GACtC;IAAE,KAAK,EAAE,OAAO,EAAE,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAY9C"}
|
package/integrations.js
CHANGED
|
@@ -87,7 +87,12 @@ export function createIntegrationRegistry() {
|
|
|
87
87
|
throw new Error(`createIntegrationRegistry: duplicate tool name "${manifest.tool.name}" — ` +
|
|
88
88
|
`already registered by integration "${existingOwner}" (integration "${manifest.id}")`);
|
|
89
89
|
}
|
|
90
|
-
const schemaProblem =
|
|
90
|
+
const schemaProblem = manifest.validation === 'remote'
|
|
91
|
+
? (manifest.tool.inputSchema && typeof manifest.tool.inputSchema === 'object'
|
|
92
|
+
&& !Array.isArray(manifest.tool.inputSchema)
|
|
93
|
+
? null
|
|
94
|
+
: 'must be an object')
|
|
95
|
+
: unsupportedSchemaFeature(manifest.tool.inputSchema);
|
|
91
96
|
if (schemaProblem) {
|
|
92
97
|
throw new Error(`createIntegrationRegistry "${manifest.id}": inputSchema ${schemaProblem}`);
|
|
93
98
|
}
|
package/integrations.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integrations.js","sourceRoot":"","sources":["src/integrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EAAE,UAAU,EAAkC,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"integrations.js","sourceRoot":"","sources":["src/integrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EAAE,UAAU,EAAkC,MAAM,YAAY,CAAC;AAkDxE,MAAM,iBAAiB,GAAG,IAAI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O,EAAE,CAAC,KAA8B,EAAE,GAAgB,EAAE,EAAE;YAC5D,MAAM,GAAG,GAAG,QAAQ,CAAC,IAAI,EAAE,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YACxF,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAC5E,CAAC;KACF,CAAC,CAAC,CAAC;IACJ,OAAO,EAAE,KAAK,EAAE,CAAC;AACnB,CAAC"}
|
package/loop.d.ts
CHANGED
|
@@ -2,18 +2,28 @@
|
|
|
2
2
|
* The turn loop — stream → tool_use → execute → tool_result → continue.
|
|
3
3
|
*
|
|
4
4
|
* One agent.send() = one runLoop(). Each round streams a model response;
|
|
5
|
-
* tool calls are executed (
|
|
6
|
-
* as tool_result parts, then the loop re-enters. A round with no tool
|
|
7
|
-
* is the final round: the loop emits `done` and returns.
|
|
5
|
+
* tool calls are executed (after the guardrail chain has seen them) and fed
|
|
6
|
+
* back as tool_result parts, then the loop re-enters. A round with no tool
|
|
7
|
+
* calls is the final round: the loop emits `done` and returns.
|
|
8
8
|
*
|
|
9
9
|
* Bounded by maxToolRounds: on exhaustion the loop emits `done` with the
|
|
10
10
|
* synthetic stopReason 'max_tool_rounds' (loop-level signal, not a
|
|
11
11
|
* provider value — the adapter-level never-normalize rule is untouched).
|
|
12
|
+
* An output guardrail's veto adds the second and last synthetic value,
|
|
13
|
+
* 'guardrail_denied'.
|
|
12
14
|
*
|
|
13
15
|
* The loop maintains its OWN working message list; the caller's Session is
|
|
14
16
|
* never mutated here (reduce() is the single session writer). The events
|
|
15
17
|
* this generator yields, folded through reduce(), reconstruct exactly the
|
|
16
18
|
* messages the loop appended internally — one contract, two consumers.
|
|
19
|
+
* Memory is the same story: a tool calling `ctx.remember(patch)` does not
|
|
20
|
+
* write anything, it queues a `memory` EVENT the loop yields.
|
|
21
|
+
*
|
|
22
|
+
* Output guardrails see the text of a round that ENDS the turn. Text from a
|
|
23
|
+
* round that also called tools is intermediate reasoning the model itself
|
|
24
|
+
* will supersede, and denying it mid-turn has no coherent answer for the
|
|
25
|
+
* tool calls already emitted alongside it — so that text passes through
|
|
26
|
+
* unguarded (README records this caveat; revisit if a consumer needs it).
|
|
17
27
|
*
|
|
18
28
|
* Progress markers (law 3): the loop emits ONLY the stages it genuinely
|
|
19
29
|
* reaches — `sent` before every provider call, `retry` before a call that
|
|
@@ -24,20 +34,21 @@
|
|
|
24
34
|
* observe today — inventing an emission point for them would violate the
|
|
25
35
|
* honesty guard, so they stay declared-but-unemitted here.
|
|
26
36
|
*/
|
|
27
|
-
import type { ChatOpts, LLMClient
|
|
37
|
+
import type { ChatOpts, LLMClient } from '@adia-ai/llm';
|
|
28
38
|
import type { AgentEvent } from './events.js';
|
|
39
|
+
import { type Guardrail } from './guardrail.js';
|
|
29
40
|
import type { Msg } from './session.js';
|
|
30
41
|
import { type ToolContext, type ToolDef } from './tools.js';
|
|
42
|
+
import type { Tracer } from './trace.js';
|
|
31
43
|
export interface LoopOpts {
|
|
32
44
|
client: LLMClient;
|
|
33
45
|
/** Per-call ChatOpts overrides (model, system, cache, temperature…). */
|
|
34
46
|
chat?: Partial<ChatOpts>;
|
|
35
47
|
tools: ToolDef[];
|
|
36
48
|
maxToolRounds: number;
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
onToolCall?: (call: ToolUse, ctx: ToolContext) => boolean | Promise<boolean>;
|
|
49
|
+
/** The declared guardrail chain (agent.ts folds `onToolCall` into it). */
|
|
50
|
+
guardrails?: Guardrail[];
|
|
51
|
+
tracer?: Tracer;
|
|
41
52
|
ctx: ToolContext;
|
|
42
53
|
}
|
|
43
54
|
export declare function runLoop(messages: Msg[], opts: LoopOpts): AsyncGenerator<AgentEvent>;
|
package/loop.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["src/loop.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["src/loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAA+B,MAAM,cAAc,CAAC;AACrF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAkD,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChG,OAAO,KAAK,EAAa,GAAG,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAwB,KAAK,WAAW,EAAE,KAAK,OAAO,EAAE,MAAM,YAAY,CAAC;AAClF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,SAAS,CAAC;IAClB,wEAAwE;IACxE,IAAI,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IACzB,KAAK,EAAE,OAAO,EAAE,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,WAAW,CAAC;CAClB;AAED,wBAAuB,OAAO,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,QAAQ,GAAG,cAAc,CAAC,UAAU,CAAC,CAwO1F"}
|
package/loop.js
CHANGED
|
@@ -2,18 +2,28 @@
|
|
|
2
2
|
* The turn loop — stream → tool_use → execute → tool_result → continue.
|
|
3
3
|
*
|
|
4
4
|
* One agent.send() = one runLoop(). Each round streams a model response;
|
|
5
|
-
* tool calls are executed (
|
|
6
|
-
* as tool_result parts, then the loop re-enters. A round with no tool
|
|
7
|
-
* is the final round: the loop emits `done` and returns.
|
|
5
|
+
* tool calls are executed (after the guardrail chain has seen them) and fed
|
|
6
|
+
* back as tool_result parts, then the loop re-enters. A round with no tool
|
|
7
|
+
* calls is the final round: the loop emits `done` and returns.
|
|
8
8
|
*
|
|
9
9
|
* Bounded by maxToolRounds: on exhaustion the loop emits `done` with the
|
|
10
10
|
* synthetic stopReason 'max_tool_rounds' (loop-level signal, not a
|
|
11
11
|
* provider value — the adapter-level never-normalize rule is untouched).
|
|
12
|
+
* An output guardrail's veto adds the second and last synthetic value,
|
|
13
|
+
* 'guardrail_denied'.
|
|
12
14
|
*
|
|
13
15
|
* The loop maintains its OWN working message list; the caller's Session is
|
|
14
16
|
* never mutated here (reduce() is the single session writer). The events
|
|
15
17
|
* this generator yields, folded through reduce(), reconstruct exactly the
|
|
16
18
|
* messages the loop appended internally — one contract, two consumers.
|
|
19
|
+
* Memory is the same story: a tool calling `ctx.remember(patch)` does not
|
|
20
|
+
* write anything, it queues a `memory` EVENT the loop yields.
|
|
21
|
+
*
|
|
22
|
+
* Output guardrails see the text of a round that ENDS the turn. Text from a
|
|
23
|
+
* round that also called tools is intermediate reasoning the model itself
|
|
24
|
+
* will supersede, and denying it mid-turn has no coherent answer for the
|
|
25
|
+
* tool calls already emitted alongside it — so that text passes through
|
|
26
|
+
* unguarded (README records this caveat; revisit if a consumer needs it).
|
|
17
27
|
*
|
|
18
28
|
* Progress markers (law 3): the loop emits ONLY the stages it genuinely
|
|
19
29
|
* reaches — `sent` before every provider call, `retry` before a call that
|
|
@@ -24,12 +34,36 @@
|
|
|
24
34
|
* observe today — inventing an emission point for them would violate the
|
|
25
35
|
* honesty guard, so they stay declared-but-unemitted here.
|
|
26
36
|
*/
|
|
37
|
+
import { guardOutput, guardToolCall, hasOutputGuardrail } from './guardrail.js';
|
|
27
38
|
import { executeTool, toSpecs } from './tools.js';
|
|
28
39
|
export async function* runLoop(messages, opts) {
|
|
29
40
|
const working = [...messages];
|
|
30
41
|
const total = { input: 0, output: 0, cacheCreation: 0, cacheRead: 0 };
|
|
31
42
|
const specs = toSpecs(opts.tools);
|
|
32
43
|
const byName = new Map(opts.tools.map(t => [t.name, t]));
|
|
44
|
+
const guardrails = opts.guardrails ?? [];
|
|
45
|
+
const tracer = opts.tracer;
|
|
46
|
+
// Memory writes a tool (or a guardrail — same ctx) asks for, drained as
|
|
47
|
+
// `memory` events after each tool result and before every `done`. The
|
|
48
|
+
// asker never touches state.
|
|
49
|
+
const pendingMemory = [];
|
|
50
|
+
const ctx = {
|
|
51
|
+
...opts.ctx,
|
|
52
|
+
remember: (patch) => { pendingMemory.push(patch); },
|
|
53
|
+
};
|
|
54
|
+
function trace(phase, name, data) {
|
|
55
|
+
const record = tracer?.record(phase, name, data);
|
|
56
|
+
return record ? { type: 'trace', record } : null;
|
|
57
|
+
}
|
|
58
|
+
/** Drain whatever asked to be remembered. Called after each tool result
|
|
59
|
+
* AND before every `done` — a guardrail (which receives the same ctx)
|
|
60
|
+
* or a tool in the final round would otherwise have its write silently
|
|
61
|
+
* dropped when the loop exits without another tool round. */
|
|
62
|
+
function* drainMemory() {
|
|
63
|
+
while (pendingMemory.length) {
|
|
64
|
+
yield { type: 'memory', patch: pendingMemory.shift() };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
33
67
|
for (let round = 0; round < opts.maxToolRounds; round++) {
|
|
34
68
|
let text = '';
|
|
35
69
|
let stopReason = 'end';
|
|
@@ -37,11 +71,20 @@ export async function* runLoop(messages, opts) {
|
|
|
37
71
|
if (round > 0)
|
|
38
72
|
yield { type: 'progress', stage: 'retry' };
|
|
39
73
|
yield { type: 'progress', stage: 'sent' };
|
|
74
|
+
const requestTrace = trace('request', 'provider:call', {
|
|
75
|
+
round,
|
|
76
|
+
...(opts.chat?.model ? { model: opts.chat.model } : {}),
|
|
77
|
+
tools: specs.length,
|
|
78
|
+
messages: working.length,
|
|
79
|
+
});
|
|
80
|
+
if (requestTrace)
|
|
81
|
+
yield requestTrace;
|
|
82
|
+
const startedAt = Date.now();
|
|
40
83
|
const stream = opts.client.stream({
|
|
41
84
|
...opts.chat,
|
|
42
85
|
messages: working,
|
|
43
86
|
...(specs.length ? { tools: specs } : {}),
|
|
44
|
-
...(
|
|
87
|
+
...(ctx.signal ? { signal: ctx.signal } : {}),
|
|
45
88
|
});
|
|
46
89
|
for await (const chunk of stream) {
|
|
47
90
|
if (chunk.type === 'text') {
|
|
@@ -51,6 +94,17 @@ export async function* runLoop(messages, opts) {
|
|
|
51
94
|
else if (chunk.type === 'thinking') {
|
|
52
95
|
yield { type: 'thinking', text: chunk.text };
|
|
53
96
|
}
|
|
97
|
+
else if (chunk.type === 'tool_use_delta') {
|
|
98
|
+
// Arguments still arriving — progress only. Nothing executes off a
|
|
99
|
+
// partial: the matching `tool_use` chunk is the call.
|
|
100
|
+
yield {
|
|
101
|
+
type: 'tool_input_delta',
|
|
102
|
+
id: chunk.id,
|
|
103
|
+
name: chunk.name,
|
|
104
|
+
partial: chunk.partial,
|
|
105
|
+
snapshot: chunk.snapshot,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
54
108
|
else if (chunk.type === 'tool_use') {
|
|
55
109
|
calls.push({ id: chunk.id, name: chunk.name, input: chunk.input });
|
|
56
110
|
yield { type: 'tool_use', id: chunk.id, name: chunk.name, input: chunk.input };
|
|
@@ -71,9 +125,66 @@ export async function* runLoop(messages, opts) {
|
|
|
71
125
|
return;
|
|
72
126
|
}
|
|
73
127
|
}
|
|
128
|
+
const responseTrace = trace('response', 'provider:done', {
|
|
129
|
+
round,
|
|
130
|
+
chars: text.length,
|
|
131
|
+
toolCalls: calls.length,
|
|
132
|
+
stopReason,
|
|
133
|
+
ms: Date.now() - startedAt,
|
|
134
|
+
});
|
|
135
|
+
if (responseTrace)
|
|
136
|
+
yield responseTrace;
|
|
74
137
|
if (!calls.length) {
|
|
138
|
+
let finalText = text;
|
|
139
|
+
if (hasOutputGuardrail(guardrails)) {
|
|
140
|
+
const verdict = await guardOutput(guardrails, text, ctx);
|
|
141
|
+
for (const name of verdict.rewrittenBy) {
|
|
142
|
+
yield { type: 'guardrail', name, target: 'output', action: 'rewrite' };
|
|
143
|
+
const rewriteTrace = trace('guardrail', 'output:rewrite', { guardrail: name });
|
|
144
|
+
if (rewriteTrace)
|
|
145
|
+
yield rewriteTrace;
|
|
146
|
+
}
|
|
147
|
+
if (verdict.denied) {
|
|
148
|
+
yield {
|
|
149
|
+
type: 'guardrail',
|
|
150
|
+
name: verdict.denied.name,
|
|
151
|
+
target: 'output',
|
|
152
|
+
action: 'deny',
|
|
153
|
+
reason: verdict.denied.reason,
|
|
154
|
+
};
|
|
155
|
+
const denyTrace = trace('guardrail', 'output:deny', {
|
|
156
|
+
guardrail: verdict.denied.name,
|
|
157
|
+
reason: verdict.denied.reason,
|
|
158
|
+
});
|
|
159
|
+
if (denyTrace)
|
|
160
|
+
yield denyTrace;
|
|
161
|
+
const usageTrace = trace('usage', 'turn:total', { ...total, rounds: round + 1, denied: true });
|
|
162
|
+
if (usageTrace)
|
|
163
|
+
yield usageTrace;
|
|
164
|
+
yield* drainMemory();
|
|
165
|
+
yield { type: 'progress', stage: 'done' };
|
|
166
|
+
// The model's text must not reach the transcript, and reduce()'s
|
|
167
|
+
// draft already holds what streamed — so clear the draft the same
|
|
168
|
+
// way a rewrite re-snapshots it, then close the turn with no
|
|
169
|
+
// assistant content and a synthetic stopReason the caller can
|
|
170
|
+
// branch on. The reason rides the guardrail event above.
|
|
171
|
+
yield { type: 'text', text: '', snapshot: '' };
|
|
172
|
+
yield { type: 'done', text: '', usage: total, stopReason: 'guardrail_denied' };
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (verdict.value !== text) {
|
|
176
|
+
finalText = verdict.value;
|
|
177
|
+
// Re-snapshot so a UI folding `text` events lands on the rewritten
|
|
178
|
+
// string rather than the original it already rendered.
|
|
179
|
+
yield { type: 'text', text: '', snapshot: finalText };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const usageTrace = trace('usage', 'turn:total', { ...total, rounds: round + 1 });
|
|
183
|
+
if (usageTrace)
|
|
184
|
+
yield usageTrace;
|
|
185
|
+
yield* drainMemory();
|
|
75
186
|
yield { type: 'progress', stage: 'done' };
|
|
76
|
-
yield { type: 'done', text, usage: total, stopReason };
|
|
187
|
+
yield { type: 'done', text: finalText, usage: total, stopReason };
|
|
77
188
|
return;
|
|
78
189
|
}
|
|
79
190
|
// Assistant turn: text (when present) + the tool_use parts, then one
|
|
@@ -88,14 +199,57 @@ export async function* runLoop(messages, opts) {
|
|
|
88
199
|
yield { type: 'progress', stage: 'tool' };
|
|
89
200
|
const tool = byName.get(call.name);
|
|
90
201
|
let outcome;
|
|
202
|
+
let effective = call;
|
|
91
203
|
if (!tool) {
|
|
92
204
|
outcome = { output: `Unknown tool "${call.name}"`, isError: true };
|
|
93
205
|
}
|
|
94
|
-
else if (opts.onToolCall && (await opts.onToolCall(call, opts.ctx)) === false) {
|
|
95
|
-
outcome = { output: `Tool call "${call.name}" denied`, isError: true };
|
|
96
|
-
}
|
|
97
206
|
else {
|
|
98
|
-
|
|
207
|
+
const verdict = await guardToolCall(guardrails, call, ctx);
|
|
208
|
+
for (const name of verdict.rewrittenBy) {
|
|
209
|
+
yield {
|
|
210
|
+
type: 'guardrail',
|
|
211
|
+
name,
|
|
212
|
+
target: 'tool_call',
|
|
213
|
+
action: 'rewrite',
|
|
214
|
+
toolName: call.name,
|
|
215
|
+
toolUseId: call.id,
|
|
216
|
+
};
|
|
217
|
+
const rewriteTrace = trace('guardrail', 'tool_call:rewrite', { guardrail: name, tool: call.name });
|
|
218
|
+
if (rewriteTrace)
|
|
219
|
+
yield rewriteTrace;
|
|
220
|
+
}
|
|
221
|
+
effective = verdict.value;
|
|
222
|
+
if (verdict.denied) {
|
|
223
|
+
yield {
|
|
224
|
+
type: 'guardrail',
|
|
225
|
+
name: verdict.denied.name,
|
|
226
|
+
target: 'tool_call',
|
|
227
|
+
action: 'deny',
|
|
228
|
+
reason: verdict.denied.reason,
|
|
229
|
+
toolName: call.name,
|
|
230
|
+
toolUseId: call.id,
|
|
231
|
+
};
|
|
232
|
+
const denyTrace = trace('guardrail', 'tool_call:deny', {
|
|
233
|
+
guardrail: verdict.denied.name,
|
|
234
|
+
tool: call.name,
|
|
235
|
+
reason: verdict.denied.reason,
|
|
236
|
+
});
|
|
237
|
+
if (denyTrace)
|
|
238
|
+
yield denyTrace;
|
|
239
|
+
outcome = { output: denialMessage(call.name, verdict.denied), isError: true };
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
const startedTool = Date.now();
|
|
243
|
+
outcome = await executeTool(tool, effective.input, ctx);
|
|
244
|
+
const toolTrace = trace('tool', 'execute', {
|
|
245
|
+
tool: call.name,
|
|
246
|
+
id: call.id,
|
|
247
|
+
ms: Date.now() - startedTool,
|
|
248
|
+
...(outcome.isError ? { isError: true } : {}),
|
|
249
|
+
});
|
|
250
|
+
if (toolTrace)
|
|
251
|
+
yield toolTrace;
|
|
252
|
+
}
|
|
99
253
|
}
|
|
100
254
|
resultParts.push({
|
|
101
255
|
type: 'tool_result',
|
|
@@ -111,10 +265,23 @@ export async function* runLoop(messages, opts) {
|
|
|
111
265
|
output: outcome.output,
|
|
112
266
|
...(outcome.isError ? { isError: true } : {}),
|
|
113
267
|
};
|
|
268
|
+
yield* drainMemory();
|
|
114
269
|
}
|
|
115
270
|
working.push({ role: 'tool', content: resultParts });
|
|
116
271
|
}
|
|
272
|
+
const usageTrace = trace('usage', 'turn:total', { ...total, rounds: opts.maxToolRounds, exhausted: true });
|
|
273
|
+
if (usageTrace)
|
|
274
|
+
yield usageTrace;
|
|
275
|
+
yield* drainMemory();
|
|
117
276
|
yield { type: 'progress', stage: 'done' };
|
|
118
277
|
yield { type: 'done', text: '', usage: total, stopReason: 'max_tool_rounds' };
|
|
119
278
|
}
|
|
279
|
+
/** The `onToolCall` gate's original wording survives verbatim — it is now
|
|
280
|
+
* one guardrail among others, and a consumer asserting on that string
|
|
281
|
+
* predates guardrails. */
|
|
282
|
+
function denialMessage(toolName, denied) {
|
|
283
|
+
return denied.name === 'onToolCall'
|
|
284
|
+
? `Tool call "${toolName}" denied`
|
|
285
|
+
: `Tool call "${toolName}" denied by guardrail "${denied.name}": ${denied.reason}`;
|
|
286
|
+
}
|
|
120
287
|
//# sourceMappingURL=loop.js.map
|
package/loop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.js","sourceRoot":"","sources":["src/loop.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"loop.js","sourceRoot":"","sources":["src/loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAIH,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,kBAAkB,EAAkB,MAAM,gBAAgB,CAAC;AAEhG,OAAO,EAAE,WAAW,EAAE,OAAO,EAAkC,MAAM,YAAY,CAAC;AAelF,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,OAAO,CAAC,QAAe,EAAE,IAAc;IAC5D,MAAM,OAAO,GAAU,CAAC,GAAG,QAAQ,CAAC,CAAC;IACrC,MAAM,KAAK,GAAiB,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;IACpF,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACzD,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC;IACzC,MAAM,MAAM,GAAG,IAA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AC,CAAC,CAAC,EAAE,CAAC;aAC9C,CAAC;YACF,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,GAAG,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3G,IAAI,UAAU;QAAE,MAAM,UAAU,CAAC;IACjC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IACrB,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC1C,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAC;AAChF,CAAC;AAED;;2BAE2B;AAC3B,SAAS,aAAa,CAAC,QAAgB,EAAE,MAAwC;IAC/E,OAAO,MAAM,CAAC,IAAI,KAAK,YAAY;QACjC,CAAC,CAAC,cAAc,QAAQ,UAAU;QAClC,CAAC,CAAC,cAAc,QAAQ,0BAA0B,MAAM,CAAC,IAAI,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC;AACvF,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP streamable-HTTP client transport — the smallest thing that speaks the
|
|
3
|
+
* protocol.
|
|
4
|
+
*
|
|
5
|
+
* Deliberately NOT `@modelcontextprotocol/sdk`. The subset a client needs to
|
|
6
|
+
* consume a server as a tool/resource source is five methods over one POST
|
|
7
|
+
* endpoint (initialize · notifications/initialized · tools/list · tools/call ·
|
|
8
|
+
* resources/list · resources/read); the SDK brings a server framework, stdio
|
|
9
|
+
* and websocket transports, an auth stack, and zod along with it. This
|
|
10
|
+
* package's whole posture is zero runtime dependencies beyond `@adia-ai/llm`
|
|
11
|
+
* and a browser-first build, so the subset is hand-rolled here and the SDK
|
|
12
|
+
* stays where it belongs — `packages/a2ui/mcp`, which SERVES.
|
|
13
|
+
*
|
|
14
|
+
* Wire rules the spec fixes and this module obeys:
|
|
15
|
+
* - Every request is a POST carrying one JSON-RPC message, with
|
|
16
|
+
* `accept: application/json, text/event-stream` — the server picks.
|
|
17
|
+
* - A `mcp-session-id` response header on the initialize response is echoed
|
|
18
|
+
* on every subsequent request (and on the DELETE that closes the session).
|
|
19
|
+
* - The negotiated `MCP-Protocol-Version` rides every post-initialize
|
|
20
|
+
* request.
|
|
21
|
+
* - An SSE response body is read until the event whose `data:` payload is
|
|
22
|
+
* the JSON-RPC response for THIS request id; server-initiated
|
|
23
|
+
* notifications interleaved on that stream are ignored (this client
|
|
24
|
+
* subscribes to nothing, so there is nothing it can act on).
|
|
25
|
+
* - The protocol headers are the LAST spread in every request, so a caller
|
|
26
|
+
* header cannot quietly replace `accept` or the session id and turn a
|
|
27
|
+
* working connection into an inexplicable one.
|
|
28
|
+
*
|
|
29
|
+
* Nothing here waits forever. Every round trip carries a deadline covering
|
|
30
|
+
* BOTH the fetch and the wait for the answer to arrive on an SSE body — a
|
|
31
|
+
* server that accepts a POST and then says nothing is a real failure mode,
|
|
32
|
+
* and it is indistinguishable from a hang unless someone is holding a clock.
|
|
33
|
+
* The deadline and the caller's `signal` share one abort path, so a timeout
|
|
34
|
+
* and a user cancellation both actually cancel the request rather than
|
|
35
|
+
* abandoning it.
|
|
36
|
+
*
|
|
37
|
+
* Everything is `fetch`-only, so it runs unchanged in a browser — with the
|
|
38
|
+
* CORS caveat the README records. `fetchImpl` exists so a test can hand the
|
|
39
|
+
* transport an in-process server instead of a network.
|
|
40
|
+
*/
|
|
41
|
+
/**
|
|
42
|
+
* Default per-request deadline: 60 seconds.
|
|
43
|
+
*
|
|
44
|
+
* It has to clear the slowest legitimate answer, and for MCP that is a tool
|
|
45
|
+
* that thinks — the in-repo a2ui server's `generate_ui` runs an LLM inside
|
|
46
|
+
* `tools/call`. A handshake-sized number (5–10s) would make MCP-client mode
|
|
47
|
+
* useless for exactly the servers this repo has. One knob rather than
|
|
48
|
+
* separate connect/call budgets: two numbers to tune is worse than one
|
|
49
|
+
* generous one, and `timeoutMs` is per-request, so a caller with a slower
|
|
50
|
+
* tool raises it in the one place that matters.
|
|
51
|
+
*/
|
|
52
|
+
export declare const DEFAULT_MCP_TIMEOUT_MS = 60000;
|
|
53
|
+
/** Every MCP failure this package raises carries this name, so a consumer
|
|
54
|
+
* can tell an MCP fault from a tool's own bad day without string-matching. */
|
|
55
|
+
export declare class McpError extends Error {
|
|
56
|
+
readonly name: string;
|
|
57
|
+
/** The server the fault belongs to. Empty for a fault raised while
|
|
58
|
+
* assembling several sources, where no single server owns it. */
|
|
59
|
+
readonly url: string;
|
|
60
|
+
constructor(message: string, url?: string, options?: {
|
|
61
|
+
cause?: unknown;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Connect-time faults: unreachable server, non-2xx, protocol mismatch, a
|
|
65
|
+
* timeout, a JSON-RPC error on initialize/list, or a listed tool this
|
|
66
|
+
* harness cannot register. Thrown, never swallowed — an agent that
|
|
67
|
+
* silently ran with an empty tool list would look like a model that forgot
|
|
68
|
+
* how to use its tools. */
|
|
69
|
+
export declare class McpConnectionError extends McpError {
|
|
70
|
+
readonly name = "McpConnectionError";
|
|
71
|
+
}
|
|
72
|
+
/** A `tools/call` or `resources/read` that failed after the connection was
|
|
73
|
+
* established. Thrown inside a ToolDef's `execute`, which `executeTool`
|
|
74
|
+
* turns into an `isError` tool_result — the model sees it and can react. */
|
|
75
|
+
export declare class McpCallError extends McpError {
|
|
76
|
+
readonly name = "McpCallError";
|
|
77
|
+
}
|
|
78
|
+
export interface McpTransportOptions {
|
|
79
|
+
url: string;
|
|
80
|
+
/** Static request headers — the v1 auth story (`authorization: Bearer …`).
|
|
81
|
+
* No OAuth flow: a token this client cannot refresh is the caller's to
|
|
82
|
+
* provision, exactly like `@adia-ai/llm`'s proxy headers. Protocol
|
|
83
|
+
* headers win over these; see the module note. */
|
|
84
|
+
headers?: Record<string, string>;
|
|
85
|
+
/** Per-request deadline in ms. Default `DEFAULT_MCP_TIMEOUT_MS` (60s).
|
|
86
|
+
* `0` disables it — for a caller who owns the clock through `signal` and
|
|
87
|
+
* means it. */
|
|
88
|
+
timeoutMs?: number;
|
|
89
|
+
/** Injected `fetch` — the seam the in-process test server rides on. */
|
|
90
|
+
fetchImpl?: typeof fetch;
|
|
91
|
+
clientInfo?: {
|
|
92
|
+
name: string;
|
|
93
|
+
version: string;
|
|
94
|
+
};
|
|
95
|
+
/** Connection-level cancellation: aborts whatever request is in flight. */
|
|
96
|
+
signal?: AbortSignal;
|
|
97
|
+
}
|
|
98
|
+
/** Per-request overrides. `signal` is how a turn's `ctx.signal` reaches an
|
|
99
|
+
* in-flight `tools/call`, so a user abort cancels the request instead of
|
|
100
|
+
* leaving it running and ignored. */
|
|
101
|
+
export interface McpRequestOptions {
|
|
102
|
+
signal?: AbortSignal;
|
|
103
|
+
timeoutMs?: number;
|
|
104
|
+
}
|
|
105
|
+
export interface McpConnection {
|
|
106
|
+
url: string;
|
|
107
|
+
serverInfo: {
|
|
108
|
+
name: string;
|
|
109
|
+
version: string;
|
|
110
|
+
};
|
|
111
|
+
protocolVersion: string;
|
|
112
|
+
capabilities: Record<string, unknown>;
|
|
113
|
+
/** One JSON-RPC round trip. Rejects with `McpCallError` on transport,
|
|
114
|
+
* timeout, abort, or protocol failure; a JSON-RPC error result rejects
|
|
115
|
+
* too (the caller decides whether that is fatal or model-visible). */
|
|
116
|
+
request<T = unknown>(method: string, params?: Record<string, unknown>, options?: McpRequestOptions): Promise<T>;
|
|
117
|
+
/** Best-effort session teardown (DELETE). Never throws — a close that
|
|
118
|
+
* fails leaves a session the server will time out on its own. */
|
|
119
|
+
close(): Promise<void>;
|
|
120
|
+
}
|
|
121
|
+
/** `connectOptions.signal` covers the HANDSHAKE only. It is deliberately not
|
|
122
|
+
* `options.signal`: that one is connection-level and would keep aborting
|
|
123
|
+
* every later call, so a turn that cancelled the connect would poison every
|
|
124
|
+
* turn after it. */
|
|
125
|
+
export declare function connectStreamableHttp(options: McpTransportOptions, connectOptions?: {
|
|
126
|
+
signal?: AbortSignal;
|
|
127
|
+
}): Promise<McpConnection>;
|
|
128
|
+
//# sourceMappingURL=mcp-transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-transport.d.ts","sourceRoot":"","sources":["src/mcp-transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAcH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;+EAC+E;AAC/E,qBAAa,QAAS,SAAQ,KAAK;IACjC,SAAkB,IAAI,EAAE,MAAM,CAAc;IAC5C;sEACkE;IAClE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;gBACT,OAAO,EAAE,MAAM,EAAE,GAAG,SAAK,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAIrE;AAED;;;;4BAI4B;AAC5B,qBAAa,kBAAmB,SAAQ,QAAQ;IAC9C,SAAkB,IAAI,wBAAwB;CAC/C;AAED;;6EAE6E;AAC7E,qBAAa,YAAa,SAAQ,QAAQ;IACxC,SAAkB,IAAI,kBAAkB;CACzC;AAED,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ;;;uDAGmD;IACnD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;oBAEgB;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;IACzB,UAAU,CAAC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