@theoremjs/agents 0.3.4-next.805.g5285614 → 0.3.4-next.809.g208c24f

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.
@@ -160,6 +160,11 @@ export const REQUEST_FIELDS = {
160
160
  type: 'ResolveHost',
161
161
  doc: 'Resolves the host name of a remote tool before each request; a name that resolves to a private address is refused.',
162
162
  },
163
+ 'runTurn.fetch': {
164
+ type: 'typeof fetch',
165
+ doc: "Sends each request a remote tool makes, once that request's network checks pass. A page whose tools cannot reach a host directly sends them through its own server here.",
166
+ unset: 'The global fetch',
167
+ },
163
168
  'runTurn.onStage': {
164
169
  type: 'StageHandler',
165
170
  doc: 'Your function that sees each stage of a text turn and can answer with what the stage allows.',
@@ -482,7 +482,7 @@ function stepReply(outcome) {
482
482
  * After the step, any gate stops the turn. A call a stage cancelled stops the turn too,
483
483
  * once every result that did come back is recorded.
484
484
  */
485
- async function* handleModelCalls(outcome, generation, profile, state, system, { onStage, signal, credentials, resolveHost } = {}) {
485
+ async function* handleModelCalls(outcome, generation, profile, state, system, { onStage, signal, credentials, resolveHost, fetch } = {}) {
486
486
  const { calls: upstreamCalls, reasoning } = outcome;
487
487
  if (upstreamCalls.length > 0)
488
488
  recordReply(state, stepReply(outcome), outcome.replyAt);
@@ -557,6 +557,7 @@ async function* handleModelCalls(outcome, generation, profile, state, system, {
557
557
  turn,
558
558
  credentials,
559
559
  resolveHost,
560
+ fetch,
560
561
  host: generation.host,
561
562
  resume: undefined,
562
563
  signal,
@@ -347,7 +347,7 @@ function* yieldLiveNonDoneEvents(gated, includeMatch) {
347
347
  return doneBatch;
348
348
  }
349
349
  function buildLiveSession(args) {
350
- const { profile, canary, connection, gate, givenUrls, destinations, signal, onStage, host: sessionHost, resolveHost, path, snapshot, trace, gateTtlMs, signInGate, } = args;
350
+ const { profile, canary, connection, gate, givenUrls, destinations, signal, onStage, host: sessionHost, resolveHost, fetch, path, snapshot, trace, gateTtlMs, signInGate, } = args;
351
351
  /** Grows as the user approves `session_consent` tools. */
352
352
  let sessionPermissions = args.sessionPermissions;
353
353
  /** Where keys typed at sign-in gates go when a call brings no source of its own. */
@@ -713,6 +713,7 @@ function buildLiveSession(args) {
713
713
  sessionPermissions,
714
714
  credentials: per.credentials ?? sessionCredentials,
715
715
  resolveHost,
716
+ fetch,
716
717
  path,
717
718
  signal,
718
719
  resume,
@@ -1241,6 +1242,7 @@ async function openTracedSession(registry, req, options, trace) {
1241
1242
  host: req.host,
1242
1243
  credentials: req.credentials,
1243
1244
  resolveHost: req.resolveHost,
1245
+ fetch: req.fetch,
1244
1246
  sessionPermissions: req.sessionPermissions,
1245
1247
  path: req.path,
1246
1248
  snapshot: gen0.tools,
@@ -46,6 +46,7 @@ async function fetchOne(url, ctx, limits) {
46
46
  policy: toolNetworkPolicy(ctx),
47
47
  followRedirects: true,
48
48
  resolveHost: ctx.resolveHost,
49
+ fetchFn: ctx.fetch,
49
50
  });
50
51
  if (!response.ok)
51
52
  throw new Error(`HTTP ${response.status}`); // labels-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
@@ -289,6 +289,7 @@ async function* invokePrepared(registry, request, profile, snapshot, callId, ope
289
289
  sessionPermissions: request.sessionPermissions,
290
290
  credentials: request.credentials,
291
291
  resolveHost: request.resolveHost,
292
+ fetch: request.fetch,
292
293
  path: request.path,
293
294
  signal: request.signal,
294
295
  resume: request.resume,
@@ -597,7 +597,7 @@ async function* sendHttpRequest(tool, targetUrl, body, { authHeaders, secret },
597
597
  followRedirects: true,
598
598
  // why: a read changes nothing, so a busy or briefly down service is asked again, as a
599
599
  // model call is; a write is never sent twice.
600
- ...(tool.access === 'read-only' ? { fetchFn: retryTransient(fetch) } : {}),
600
+ fetchFn: tool.access === 'read-only' ? retryTransient(ctx.fetch ?? fetch) : ctx.fetch,
601
601
  originBoundHeaders: { ...tool.headers, ...authHeaders },
602
602
  resolveHost: ctx.resolveHost,
603
603
  onCheck: checks.onCheck,
@@ -755,6 +755,7 @@ async function fetchMcpProtocolAttempt(transport, rpcId, mcpToolName, input, pro
755
755
  followRedirects: true,
756
756
  originBoundHeaders: transport.originBoundHeaders,
757
757
  resolveHost: transport.resolveHost,
758
+ fetchFn: transport.fetch,
758
759
  onCheck: transport.onCheck,
759
760
  ...(transport.beforeSend ? { beforeSend: transport.beforeSend } : {}),
760
761
  });
@@ -785,7 +786,7 @@ async function fetchMcpProtocolAttempt(transport, rpcId, mcpToolName, input, pro
785
786
  failure: {
786
787
  code: `mcp_http_${response.status}`,
787
788
  kind: kindOfToolHttpStatus(response.status),
788
- message: `MCP server error HTTP ${response.status}: ${quotedBody(text)}`, // labels-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
789
+ message: `MCP server error HTTP ${response.status} from ${new URL(transport.url).hostname}: ${quotedBody(text)}`, // labels-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
789
790
  },
790
791
  };
791
792
  }
@@ -854,6 +855,7 @@ async function openMcpSession(transport) {
854
855
  followRedirects: false,
855
856
  originBoundHeaders: { ...transport.originBoundHeaders, ...originBound },
856
857
  resolveHost: transport.resolveHost,
858
+ fetchFn: transport.fetch,
857
859
  onCheck: transport.onCheck,
858
860
  });
859
861
  const response = await post({
@@ -872,7 +874,7 @@ async function openMcpSession(transport) {
872
874
  throw new McpSessionFailure(tooLargeFailure(host, transport.maxResponseBytes));
873
875
  }
874
876
  if (!response.ok) {
875
- throw sessionFailure(`mcp_session_http_${response.status}`, `MCP server refused to open a session: HTTP ${response.status}: ${quotedBody(text)}`);
877
+ throw sessionFailure(`mcp_session_http_${response.status}`, `MCP server ${new URL(transport.url).hostname} refused to open a session: HTTP ${response.status}: ${quotedBody(text)}`);
876
878
  }
877
879
  const id = response.headers.get(SESSION_HEADER);
878
880
  if (!isMcpSessionId(id)) {
@@ -1101,6 +1103,7 @@ async function* sendMcpRequest(tool, url, input, { authHeaders, secret }, ctx, b
1101
1103
  originBoundHeaders: { ...tool.headers, ...authHeaders },
1102
1104
  policy: toolNetworkPolicy(ctx),
1103
1105
  resolveHost: ctx.resolveHost,
1106
+ fetch: ctx.fetch,
1104
1107
  signal: ctx.signal,
1105
1108
  onCheck: checks.onCheck,
1106
1109
  ...(tool.maxResponseBytes === undefined ? {} : { maxResponseBytes: tool.maxResponseBytes }),
@@ -33,6 +33,7 @@ export function signedInFetch(prepared, ctx, checks) {
33
33
  followRedirects: true,
34
34
  originBoundHeaders: prepared.authHeaders,
35
35
  resolveHost: ctx.resolveHost,
36
+ fetchFn: ctx.fetch,
36
37
  onCheck: checks.onCheck,
37
38
  });
38
39
  const refusal = credentialRefusal(response);
@@ -95,6 +95,8 @@ export interface ToolContext {
95
95
  resume?: InvokeToolResume;
96
96
  credentials?: ToolCredentialSource;
97
97
  resolveHost?: ResolveHost;
98
+ /** Sends this call's requests once their checks pass; the run's `fetch`. Default: the global `fetch`. */
99
+ fetch?: typeof fetch;
98
100
  /** Opaque application context from `TurnRequest.host` / `InvokeToolRequest.host`; the kernel never reads it. */
99
101
  host?: unknown;
100
102
  /** The page's answer to this call, for a tool with `answeredBy: 'page'`. */
@@ -428,6 +430,8 @@ export interface InvokeToolRequest extends RunControls {
428
430
  sessionPermissions?: string[];
429
431
  credentials?: ToolCredentialSource;
430
432
  resolveHost?: ResolveHost;
433
+ /** Sends the call's requests once their checks pass. Default: the global `fetch`. */
434
+ fetch?: typeof fetch;
431
435
  path?: string;
432
436
  /** Handed to the tool as `ctx.host`; the kernel never reads it. */
433
437
  host?: unknown;
@@ -795,6 +795,12 @@ export interface AgentRunRequest extends RunControls, ModelChoice {
795
795
  * resolves to a private address is refused (see `fetchGuarded`).
796
796
  */
797
797
  resolveHost?: ResolveHost;
798
+ /**
799
+ * Sends each request a remote tool makes (HTTP, MCP, a signed-in fetch, an image read), once
800
+ * that request's network checks pass. A page whose tools cannot reach a host directly sends them
801
+ * through its own server here. Default: the global `fetch`.
802
+ */
803
+ fetch?: typeof fetch;
798
804
  /** Text `runTurn` and `runSession` emit stages and apply the returned affordances. A session's is set once. */
799
805
  onStage?: StageHandler;
800
806
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.805.g5285614",
3
+ "version": "0.3.4-next.809.g208c24f",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",