nexus-ai-pro 2.2.0 → 2.3.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.
Files changed (135) hide show
  1. package/API_STABILITY.md +39 -0
  2. package/CHANGELOG.md +142 -1
  3. package/README.md +10 -5
  4. package/dist/agent/capabilities.d.ts +39 -0
  5. package/dist/agent/capabilities.js +36 -0
  6. package/dist/agent/create-agent.d.ts +61 -11
  7. package/dist/agent/create-agent.js +136 -22
  8. package/dist/agent/index.d.ts +3 -2
  9. package/dist/agent/index.js +1 -1
  10. package/dist/agent/middleware/approval.d.ts +26 -0
  11. package/dist/agent/middleware/approval.js +23 -0
  12. package/dist/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
  13. package/dist/agent/{middleware.js → middleware/basic.js} +3 -5
  14. package/dist/agent/middleware/context-editor.d.ts +52 -0
  15. package/dist/agent/middleware/context-editor.js +66 -0
  16. package/dist/agent/middleware/filesystem.d.ts +44 -0
  17. package/dist/agent/middleware/filesystem.js +83 -0
  18. package/dist/agent/middleware/index.d.ts +19 -0
  19. package/dist/agent/middleware/index.js +9 -0
  20. package/dist/agent/middleware/limits.d.ts +42 -0
  21. package/dist/agent/middleware/limits.js +52 -0
  22. package/dist/agent/middleware/models.d.ts +48 -0
  23. package/dist/agent/middleware/models.js +44 -0
  24. package/dist/agent/middleware/pii.d.ts +67 -0
  25. package/dist/agent/middleware/pii.js +173 -0
  26. package/dist/agent/middleware/retry.d.ts +66 -0
  27. package/dist/agent/middleware/retry.js +59 -0
  28. package/dist/agent/middleware/shared.d.ts +41 -0
  29. package/dist/agent/middleware/shared.js +73 -0
  30. package/dist/agent/middleware/tool-selector.d.ts +64 -0
  31. package/dist/agent/middleware/tool-selector.js +106 -0
  32. package/dist/agent/permissions.d.ts +113 -0
  33. package/dist/agent/permissions.js +180 -0
  34. package/dist/agent/sandbox.d.ts +151 -0
  35. package/dist/agent/sandbox.js +257 -0
  36. package/dist/agent/tool.d.ts +8 -3
  37. package/dist/agent/tool.js +14 -3
  38. package/dist/deep-agent/index.d.ts +109 -0
  39. package/dist/deep-agent/index.js +216 -0
  40. package/dist/graph/functional.d.ts +9 -0
  41. package/dist/graph/functional.js +20 -3
  42. package/dist/graph/graph.js +25 -4
  43. package/dist/graph/lint.d.ts +4 -1
  44. package/dist/graph/lint.js +35 -0
  45. package/dist/postgres/fulltext.d.ts +67 -0
  46. package/dist/postgres/fulltext.js +91 -0
  47. package/dist/rag/elasticsearch.d.ts +71 -0
  48. package/dist/rag/elasticsearch.js +130 -0
  49. package/dist/rag/pipeline.d.ts +140 -0
  50. package/dist/rag/pipeline.js +187 -0
  51. package/dist/rag/rerankers.d.ts +92 -0
  52. package/dist/rag/rerankers.js +97 -0
  53. package/dist/rag/retrievers.d.ts +12 -1
  54. package/dist/server/assistant.d.ts +5 -1
  55. package/dist/server/assistant.js +4 -2
  56. package/dist/server/auth.d.ts +250 -0
  57. package/dist/server/auth.js +392 -0
  58. package/dist/store/memory.d.ts +10 -0
  59. package/dist/store/memory.js +10 -0
  60. package/dist/types/graph.d.ts +38 -0
  61. package/dist/types/messages.d.ts +36 -2
  62. package/dist/types/principal.d.ts +21 -0
  63. package/dist/types/principal.js +1 -0
  64. package/dist/types/server.d.ts +2 -9
  65. package/dist/utils/tenant.d.ts +11 -0
  66. package/dist/utils/tenant.js +14 -0
  67. package/dist/version.d.ts +1 -1
  68. package/dist/version.js +1 -1
  69. package/dist-cjs/agent/capabilities.d.ts +39 -0
  70. package/dist-cjs/agent/capabilities.js +41 -0
  71. package/dist-cjs/agent/create-agent.d.ts +61 -11
  72. package/dist-cjs/agent/create-agent.js +136 -22
  73. package/dist-cjs/agent/index.d.ts +3 -2
  74. package/dist-cjs/agent/index.js +4 -4
  75. package/dist-cjs/agent/middleware/approval.d.ts +26 -0
  76. package/dist-cjs/agent/middleware/approval.js +26 -0
  77. package/dist-cjs/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
  78. package/dist-cjs/agent/{middleware.js → middleware/basic.js} +3 -5
  79. package/dist-cjs/agent/middleware/context-editor.d.ts +52 -0
  80. package/dist-cjs/agent/middleware/context-editor.js +70 -0
  81. package/dist-cjs/agent/middleware/filesystem.d.ts +44 -0
  82. package/dist-cjs/agent/middleware/filesystem.js +86 -0
  83. package/dist-cjs/agent/middleware/index.d.ts +19 -0
  84. package/dist-cjs/agent/middleware/index.js +28 -0
  85. package/dist-cjs/agent/middleware/limits.d.ts +42 -0
  86. package/dist-cjs/agent/middleware/limits.js +57 -0
  87. package/dist-cjs/agent/middleware/models.d.ts +48 -0
  88. package/dist-cjs/agent/middleware/models.js +48 -0
  89. package/dist-cjs/agent/middleware/pii.d.ts +67 -0
  90. package/dist-cjs/agent/middleware/pii.js +178 -0
  91. package/dist-cjs/agent/middleware/retry.d.ts +66 -0
  92. package/dist-cjs/agent/middleware/retry.js +63 -0
  93. package/dist-cjs/agent/middleware/shared.d.ts +41 -0
  94. package/dist-cjs/agent/middleware/shared.js +83 -0
  95. package/dist-cjs/agent/middleware/tool-selector.d.ts +64 -0
  96. package/dist-cjs/agent/middleware/tool-selector.js +109 -0
  97. package/dist-cjs/agent/permissions.d.ts +113 -0
  98. package/dist-cjs/agent/permissions.js +189 -0
  99. package/dist-cjs/agent/sandbox.d.ts +151 -0
  100. package/dist-cjs/agent/sandbox.js +297 -0
  101. package/dist-cjs/agent/tool.d.ts +8 -3
  102. package/dist-cjs/agent/tool.js +14 -3
  103. package/dist-cjs/deep-agent/index.d.ts +109 -0
  104. package/dist-cjs/deep-agent/index.js +219 -0
  105. package/dist-cjs/graph/functional.d.ts +9 -0
  106. package/dist-cjs/graph/functional.js +20 -3
  107. package/dist-cjs/graph/graph.js +25 -4
  108. package/dist-cjs/graph/lint.d.ts +4 -1
  109. package/dist-cjs/graph/lint.js +35 -0
  110. package/dist-cjs/postgres/fulltext.d.ts +67 -0
  111. package/dist-cjs/postgres/fulltext.js +129 -0
  112. package/dist-cjs/rag/elasticsearch.d.ts +71 -0
  113. package/dist-cjs/rag/elasticsearch.js +135 -0
  114. package/dist-cjs/rag/pipeline.d.ts +140 -0
  115. package/dist-cjs/rag/pipeline.js +191 -0
  116. package/dist-cjs/rag/rerankers.d.ts +92 -0
  117. package/dist-cjs/rag/rerankers.js +107 -0
  118. package/dist-cjs/rag/retrievers.d.ts +12 -1
  119. package/dist-cjs/server/assistant.d.ts +5 -1
  120. package/dist-cjs/server/assistant.js +4 -2
  121. package/dist-cjs/server/auth.d.ts +250 -0
  122. package/dist-cjs/server/auth.js +402 -0
  123. package/dist-cjs/store/memory.d.ts +10 -0
  124. package/dist-cjs/store/memory.js +10 -0
  125. package/dist-cjs/types/graph.d.ts +38 -0
  126. package/dist-cjs/types/messages.d.ts +36 -2
  127. package/dist-cjs/types/principal.d.ts +21 -0
  128. package/dist-cjs/types/principal.js +2 -0
  129. package/dist-cjs/types/server.d.ts +2 -9
  130. package/dist-cjs/utils/tenant.d.ts +11 -0
  131. package/dist-cjs/utils/tenant.js +15 -0
  132. package/dist-cjs/version.d.ts +1 -1
  133. package/dist-cjs/version.js +1 -1
  134. package/llms.txt +4 -3
  135. package/package.json +118 -1
@@ -6,6 +6,7 @@ exports.agentAsTool = agentAsTool;
6
6
  const channels_js_1 = require("../graph/channels.js");
7
7
  const graph_js_1 = require("../graph/graph.js");
8
8
  const graph_js_2 = require("../types/graph.js");
9
+ const capabilities_js_1 = require("./capabilities.js");
9
10
  const tool_js_1 = require("./tool.js");
10
11
  const agentChannels = () => ({
11
12
  messages: (0, channels_js_1.appendList)(),
@@ -39,24 +40,47 @@ function createAgent(options) {
39
40
  ...(options.responseFormat ? { responseFormat: options.responseFormat } : {}),
40
41
  ...(options.metadata ? { metadata: options.metadata } : {}),
41
42
  };
43
+ const hooks = hookContext(context, state, options.store);
42
44
  for (const item of middleware) {
43
- request = (await item.beforeModel?.({ request, state, store: options.store })) ?? request;
45
+ request = (await item.beforeModel?.({ ...hooks, request })) ?? request;
44
46
  }
45
- let response = options.streamTokens && options.client.stream
46
- ? await streamed(options.client.stream(request), (chunk) => context.message(chunk))
47
- : await options.client.complete(request);
48
- if (!(options.streamTokens && options.client.stream) && response.content) {
47
+ const streaming = Boolean(options.streamTokens && options.client.stream);
48
+ const send = (next) => streaming && options.client.stream
49
+ ? streamed(options.client.stream(next), (chunk) => context.message(chunk))
50
+ : options.client.complete(next);
51
+ const stops = new WeakSet();
52
+ const modelContext = {
53
+ ...hooks,
54
+ stop: (answer) => {
55
+ const stopped = stopResponse(answer, request.model);
56
+ stops.add(stopped);
57
+ return stopped;
58
+ },
59
+ };
60
+ const wrapped = middleware.reduceRight((next, item) => {
61
+ const wrap = item.wrapModelCall?.bind(item);
62
+ return wrap ? (current) => Promise.resolve(wrap(current, next, modelContext)) : next;
63
+ }, send);
64
+ let response = await wrapped(request);
65
+ const stopped = stops.has(response);
66
+ if ((!streaming || stopped) && response.content) {
49
67
  context.message({ content: response.content });
50
68
  }
51
69
  for (const item of middleware) {
52
- response = (await item.afterModel?.({ response, state, store: options.store })) ?? response;
70
+ response = (await item.afterModel?.({ ...hooks, response })) ?? response;
53
71
  }
54
72
  const assistant = {
55
73
  role: 'assistant',
56
74
  content: response.content,
57
- ...(response.toolCalls?.length ? { toolCalls: response.toolCalls } : {}),
75
+ ...(response.toolCalls?.length && !stopped ? { toolCalls: response.toolCalls } : {}),
58
76
  };
59
- const calls = response.toolCalls ?? [];
77
+ const calls = stopped ? [] : (response.toolCalls ?? []);
78
+ if (stopped) {
79
+ return new graph_js_2.Command({
80
+ update: { messages: [assistant], iterations: 1, answer: response.content, stopReason: 'stopped' },
81
+ goto: graph_js_2.END,
82
+ });
83
+ }
60
84
  if (calls.length === 0) {
61
85
  return new graph_js_2.Command({
62
86
  update: { messages: [assistant], iterations: 1, answer: response.content, stopReason: 'completed' },
@@ -81,27 +105,82 @@ function createAgent(options) {
81
105
  return { messages: [toolMessage(call, { ok: false, error: parsed.error })] };
82
106
  }
83
107
  let args = parsed.args;
84
- const description = { id: call.id, name: call.function.name, args };
108
+ const definition = executor.get(call.function.name);
109
+ const capabilities = (0, capabilities_js_1.capabilitiesOf)(definition, args);
110
+ const description = {
111
+ id: call.id,
112
+ name: call.function.name,
113
+ args,
114
+ ...(capabilities ? { capabilities } : {}),
115
+ };
116
+ const refuse = (error) => {
117
+ context.tool({ phase: 'error', id: call.id, name: description.name, error });
118
+ return { messages: [toolMessage(call, { ok: false, error })] };
119
+ };
120
+ let permission;
121
+ if (options.permissions) {
122
+ const verdict = await options.permissions.decide({
123
+ tool: description.name,
124
+ args,
125
+ capabilities,
126
+ ...(context.principal ? { principal: context.principal } : {}),
127
+ });
128
+ if (verdict.decision === 'deny')
129
+ return refuse(`Permission denied: ${verdict.reason}`);
130
+ if (verdict.decision === 'ask')
131
+ permission = verdict.reason;
132
+ }
85
133
  const policy = approvals[description.name];
86
- if (policy) {
134
+ if (policy || permission) {
135
+ const asked = permission ? { ...description, permission } : description;
87
136
  const decision = context.interrupt({
88
- reason: (policy === true ? undefined : policy.reason?.(description)) ?? defaultReason(description),
89
- payload: description,
137
+ reason: (policy && policy !== true ? policy.reason?.(asked) : undefined) ??
138
+ (permission ? `${defaultReason(asked)} (${permission})` : defaultReason(asked)),
139
+ payload: asked,
90
140
  });
91
141
  const verdict = normalizeApproval(decision);
92
- if (!verdict.approved) {
93
- return {
94
- messages: [toolMessage(call, { ok: false, error: verdict.reason ?? 'A human refused this tool call' })],
95
- };
96
- }
97
- if (verdict.args)
142
+ if (!verdict.approved)
143
+ return refuse(verdict.reason ?? 'A human refused this tool call');
144
+ if (verdict.args) {
98
145
  args = verdict.args;
146
+ if (options.permissions) {
147
+ const recheck = await options.permissions.decide({
148
+ tool: description.name,
149
+ args,
150
+ capabilities: (0, capabilities_js_1.capabilitiesOf)(definition, args),
151
+ });
152
+ if (recheck.decision === 'deny')
153
+ return refuse(`Permission denied: ${recheck.reason}`);
154
+ }
155
+ }
99
156
  }
100
- const run = async () => executor.execute(description.name, args);
157
+ const run = async (current) => {
158
+ if (options.permissions && current.args !== args) {
159
+ const recheck = await options.permissions.decide({
160
+ tool: description.name,
161
+ args: current.args,
162
+ capabilities: (0, capabilities_js_1.capabilitiesOf)(definition, current.args),
163
+ });
164
+ if (recheck.decision === 'deny')
165
+ return { ok: false, error: `Permission denied: ${recheck.reason}` };
166
+ }
167
+ return executor.execute(description.name, current.args, {
168
+ toolCallId: call.id,
169
+ threadId: context.threadId,
170
+ ...(context.tenantId !== undefined ? { tenantId: context.tenantId } : {}),
171
+ ...(context.principal ? { principal: context.principal } : {}),
172
+ signal: context.signal,
173
+ ...(context.store ? { store: context.store } : {}),
174
+ });
175
+ };
101
176
  context.tool({ phase: 'start', id: call.id, name: description.name, args });
102
- const result = await middleware.reduceRight((next, item) => item.wrapToolCall
103
- ? () => Promise.resolve(item.wrapToolCall?.({ ...description, args }, next))
104
- : next, run)();
177
+ const hooks = hookContext(context, context.state, options.store);
178
+ const result = await middleware.reduceRight((next, item) => {
179
+ const wrap = item.wrapToolCall?.bind(item);
180
+ return wrap
181
+ ? (current) => Promise.resolve(wrap(current, (changed) => next(changed ?? current), hooks))
182
+ : next;
183
+ }, run)({ ...description, args });
105
184
  context.tool(result.ok
106
185
  ? { phase: 'result', id: call.id, name: description.name, result: result.result }
107
186
  : { phase: 'error', id: call.id, name: description.name, error: result.error ?? 'The tool failed' });
@@ -116,6 +195,12 @@ function createAgent(options) {
116
195
  ...(options.store ? { store: options.store } : {}),
117
196
  ...(options.name ? { name: options.name } : {}),
118
197
  ...(options.lifecycle ? { lifecycle: options.lifecycle, lifecycleFamily: 'agent' } : {}),
198
+ tools: executor.list().map((definition) => ({
199
+ name: definition.name,
200
+ ...(Array.isArray(definition.capabilities) ? { capabilities: [...definition.capabilities] } : {}),
201
+ ...(typeof definition.capabilities === 'function' ? { dynamic: true } : {}),
202
+ approval: approvals[definition.name] ? 'interrupt' : options.permissions ? 'policy' : 'none',
203
+ })),
119
204
  });
120
205
  }
121
206
  function agentAsTool(options) {
@@ -170,6 +255,35 @@ function parseArguments(raw) {
170
255
  return { ok: false, error: `Tool arguments are not valid JSON (${reason}); the tool was not run` };
171
256
  }
172
257
  }
258
+ function hookContext(context, state, store) {
259
+ return {
260
+ state,
261
+ ...((context.store ?? store) ? { store: context.store ?? store } : {}),
262
+ threadId: context.threadId,
263
+ ...(context.tenantId ? { tenantId: context.tenantId } : {}),
264
+ ...(context.principal ? { principal: context.principal } : {}),
265
+ signal: context.signal,
266
+ interrupt: (request) => context.interrupt(request),
267
+ };
268
+ }
269
+ function stopResponse(answer, model) {
270
+ return {
271
+ content: answer,
272
+ role: 'assistant',
273
+ finishReason: 'stop',
274
+ meta: {
275
+ requestId: '',
276
+ providerUsed: 'none',
277
+ modelUsed: model,
278
+ latencyMs: 0,
279
+ tokensInput: 0,
280
+ tokensOutput: 0,
281
+ tokensSaved: 0,
282
+ cacheHit: false,
283
+ guardrailsApplied: ['stopped-by-middleware'],
284
+ },
285
+ };
286
+ }
173
287
  async function streamed(chunks, onChunk) {
174
288
  let content = '';
175
289
  const toolCalls = [];
@@ -1,5 +1,6 @@
1
1
  export { AgentLoop, type AgentModelClient as AgentLoopModelClient } from './loop.js';
2
2
  export { isToolOutput, tool, ToolExecutor, toolMessageContent, toolOutput } from './tool.js';
3
- export { limitToolCalls, redactMessages, type RedactOptions, summarizeHistory, type SummarizeOptions, } from './middleware.js';
4
- export { agentAsTool, agentInput, type AgentApproval, type AgentApprovalPolicy, type AgentChannels, type AgentGraph, type AgentMiddleware, type AgentModelClient, type AgentState, type AgentStopReason, type AgentToolCall, type AgentToolResult, createAgent, type CreateAgentOptions, } from './create-agent.js';
3
+ export { limitToolCalls, redactMessages, type RedactOptions, summarizeHistory, type SummarizeOptions, } from './middleware/basic.js';
4
+ export { agentAsTool, agentInput, type AgentApproval, type AgentApprovalPolicy, type AgentChannels, type AgentGraph, type AgentMiddleware, type AgentMiddlewareContext, type AgentModelCallContext, type AgentModelClient, type AgentState, type AgentStopReason, type AgentToolCall, type AgentToolResult, createAgent, type CreateAgentOptions, } from './create-agent.js';
5
5
  export type { AgentConfig, AgentResult, AgentStep, ToolExecutionResult } from '../types/agent.js';
6
+ export type { ToolContext } from '../types/messages.js';
@@ -9,10 +9,10 @@ Object.defineProperty(exports, "tool", { enumerable: true, get: function () { re
9
9
  Object.defineProperty(exports, "ToolExecutor", { enumerable: true, get: function () { return tool_js_1.ToolExecutor; } });
10
10
  Object.defineProperty(exports, "toolMessageContent", { enumerable: true, get: function () { return tool_js_1.toolMessageContent; } });
11
11
  Object.defineProperty(exports, "toolOutput", { enumerable: true, get: function () { return tool_js_1.toolOutput; } });
12
- var middleware_js_1 = require("./middleware.js");
13
- Object.defineProperty(exports, "limitToolCalls", { enumerable: true, get: function () { return middleware_js_1.limitToolCalls; } });
14
- Object.defineProperty(exports, "redactMessages", { enumerable: true, get: function () { return middleware_js_1.redactMessages; } });
15
- Object.defineProperty(exports, "summarizeHistory", { enumerable: true, get: function () { return middleware_js_1.summarizeHistory; } });
12
+ var basic_js_1 = require("./middleware/basic.js");
13
+ Object.defineProperty(exports, "limitToolCalls", { enumerable: true, get: function () { return basic_js_1.limitToolCalls; } });
14
+ Object.defineProperty(exports, "redactMessages", { enumerable: true, get: function () { return basic_js_1.redactMessages; } });
15
+ Object.defineProperty(exports, "summarizeHistory", { enumerable: true, get: function () { return basic_js_1.summarizeHistory; } });
16
16
  var create_agent_js_1 = require("./create-agent.js");
17
17
  Object.defineProperty(exports, "agentAsTool", { enumerable: true, get: function () { return create_agent_js_1.agentAsTool; } });
18
18
  Object.defineProperty(exports, "agentInput", { enumerable: true, get: function () { return create_agent_js_1.agentInput; } });
@@ -0,0 +1,26 @@
1
+ import type { AgentApproval, AgentMiddleware, AgentMiddlewareContext, AgentToolCall } from '../create-agent.js';
2
+ /** Options for `humanApproval()`. */
3
+ export interface HumanApprovalOptions {
4
+ /**
5
+ * Which calls need a person's approval. Defaults to every call whose tool declares nothing, or
6
+ * declares a capability that does anything but read.
7
+ */
8
+ when?: (call: AgentToolCall, context: AgentMiddlewareContext) => boolean;
9
+ /** The question a person sees. Defaults to naming the tool and its arguments. */
10
+ reason?: (call: AgentToolCall) => string;
11
+ /**
12
+ * Asks out of band instead of suspending the run: posts to a chat and waits for the answer, or
13
+ * applies a rule of your own. Without it, the run suspends through `interrupt()` until `resume()`
14
+ * answers, as `interruptOn` does.
15
+ */
16
+ approve?: (call: AgentToolCall, context: AgentMiddlewareContext) => AgentApproval | Promise<AgentApproval>;
17
+ }
18
+ /**
19
+ * Asks a person before a tool call runs, decided per call rather than per tool name.
20
+ *
21
+ * `interruptOn` approves every call to a named tool; this approves the calls a rule picks: a refund
22
+ * over a limit, a write outside a draft folder, anything a tool does that is not a read. An
23
+ * approver may refuse, or correct the arguments, which the permission policy then decides again.
24
+ * Put it before `toolRetry()`, so a retried call is not asked about twice.
25
+ */
26
+ export declare function humanApproval(options?: HumanApprovalOptions): AgentMiddleware;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.humanApproval = humanApproval;
4
+ const capabilities_js_1 = require("../capabilities.js");
5
+ function humanApproval(options = {}) {
6
+ const when = options.when ??
7
+ ((call) => call.capabilities === undefined || call.capabilities.some(capabilities_js_1.isSensitiveCapability));
8
+ const reason = options.reason ?? ((call) => `Run tool "${call.name}" with ${JSON.stringify(call.args)}?`);
9
+ return {
10
+ name: 'human-approval',
11
+ async wrapToolCall(call, next, context) {
12
+ if (!when(call, context))
13
+ return next();
14
+ const answer = options.approve
15
+ ? await options.approve(call, context)
16
+ : context.interrupt({ reason: reason(call), payload: call });
17
+ if (answer === false || (typeof answer === 'object' && !answer.approved)) {
18
+ const why = typeof answer === 'object' && !answer.approved ? answer.reason : undefined;
19
+ return { ok: false, error: why ?? 'A person refused this tool call' };
20
+ }
21
+ return typeof answer === 'object' && answer.approved && answer.args
22
+ ? next({ ...call, args: answer.args })
23
+ : next();
24
+ },
25
+ };
26
+ }
@@ -1,5 +1,5 @@
1
- import type { Message } from '../types/messages.js';
2
- import type { AgentMiddleware } from './create-agent.js';
1
+ import type { Message } from '../../types/messages.js';
2
+ import type { AgentMiddleware } from '../create-agent.js';
3
3
  /**
4
4
  * Middleware that ships with the agent.
5
5
  *
@@ -40,9 +40,10 @@ export interface RedactOptions {
40
40
  */
41
41
  export declare function redactMessages(options: RedactOptions): AgentMiddleware;
42
42
  /**
43
- * Caps how often a tool may be called in one run.
43
+ * Caps how often a tool may be called in one run: one turn, from a user message to the answer.
44
44
  *
45
45
  * A model that loops on the same call otherwise burns the iteration budget, and with an expensive
46
- * tool it burns money too.
46
+ * tool it burns money too. Calls are counted from the transcript, so the count survives a restart
47
+ * and a resumed approval, and two threads never share one.
47
48
  */
48
49
  export declare function limitToolCalls(limits: Record<string, number>): AgentMiddleware;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.summarizeHistory = summarizeHistory;
4
4
  exports.redactMessages = redactMessages;
5
5
  exports.limitToolCalls = limitToolCalls;
6
+ const shared_js_1 = require("./shared.js");
6
7
  function summarizeHistory(options) {
7
8
  const keepLast = options.keepLast ?? 6;
8
9
  const triggerAfter = options.triggerAfter ?? 20;
@@ -45,18 +46,15 @@ function redactMessages(options) {
45
46
  };
46
47
  }
47
48
  function limitToolCalls(limits) {
48
- const counts = new Map();
49
49
  return {
50
50
  name: 'limit-tool-calls',
51
- async wrapToolCall(call, next) {
51
+ async wrapToolCall(call, next, context) {
52
52
  const limit = limits[call.name];
53
53
  if (limit === undefined)
54
54
  return next();
55
- const used = counts.get(call.name) ?? 0;
56
- if (used >= limit) {
55
+ if ((0, shared_js_1.callPosition)(context.state.messages, call) > limit) {
57
56
  return { ok: false, error: `Tool "${call.name}" has already run ${limit} times in this run` };
58
57
  }
59
- counts.set(call.name, used + 1);
60
58
  return next();
61
59
  },
62
60
  };
@@ -0,0 +1,52 @@
1
+ import type { CompletionRequest } from '../../types/messages.js';
2
+ import type { AgentMiddleware } from '../create-agent.js';
3
+ /** One cleared result, for a custom placeholder. */
4
+ export interface ClearedToolResult {
5
+ /** The tool whose result was cleared. */
6
+ tool: string;
7
+ /** How long the result was, in characters. */
8
+ length: number;
9
+ }
10
+ /** What one edit did, for logs and for measuring what it saves. */
11
+ export interface ContextEdit {
12
+ /** Tool results cleared. */
13
+ cleared: number;
14
+ /** The request's estimated tokens before. */
15
+ tokensBefore: number;
16
+ /** And after. */
17
+ tokensAfter: number;
18
+ }
19
+ /** Options for `contextEditor()`. */
20
+ export interface ContextEditorOptions {
21
+ /** Editing starts once the request's estimated tokens pass this. Defaults to 30,000. */
22
+ triggerTokens?: number;
23
+ /** The most recent tool results, kept whole. Defaults to 3. */
24
+ keepToolResults?: number;
25
+ /**
26
+ * Results are cleared this many at a time, so the edited prefix stays the same for several calls
27
+ * and a provider's prompt cache keeps working between edits. Defaults to 5.
28
+ */
29
+ batch?: number;
30
+ /** Tools whose results are never cleared. */
31
+ exclude?: readonly string[];
32
+ /** Also empties the arguments of each call whose result is cleared, for tools that take large inputs. */
33
+ clearArguments?: boolean;
34
+ /** What a cleared result says instead. Defaults to a line naming the tool and how to get it back. */
35
+ placeholder?: string | ((cleared: ClearedToolResult) => string);
36
+ /** Estimates a request's tokens. Defaults to about four characters a token. */
37
+ estimateTokens?: (request: CompletionRequest) => number;
38
+ /** Called after each edit. */
39
+ onEdit?: (edit: ContextEdit) => void;
40
+ }
41
+ /**
42
+ * Clears old tool results from what the model is sent, once the request grows past a threshold.
43
+ *
44
+ * Tool output is most of a long agent's context, and the model rarely needs a result from twenty
45
+ * steps ago verbatim. Every result but the most recent few is replaced by a short placeholder, in
46
+ * batches, so the edited prefix stays the same between edits and a provider's prompt cache keeps
47
+ * working. Only the request changes: the transcript in state keeps every result, and checkpoints,
48
+ * events, and a later summary still see them.
49
+ */
50
+ export declare function contextEditor(options?: ContextEditorOptions): AgentMiddleware;
51
+ /** About four characters a token, over the messages, the tool calls, and the tool schemas. */
52
+ export declare function estimateRequestTokens(request: CompletionRequest): number;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contextEditor = contextEditor;
4
+ exports.estimateRequestTokens = estimateRequestTokens;
5
+ const shared_js_1 = require("./shared.js");
6
+ function contextEditor(options = {}) {
7
+ const triggerTokens = options.triggerTokens ?? 30_000;
8
+ const keep = Math.max(0, options.keepToolResults ?? 3);
9
+ const batch = Math.max(1, options.batch ?? 5);
10
+ const exclude = new Set(options.exclude ?? []);
11
+ const estimate = options.estimateTokens ?? estimateRequestTokens;
12
+ const placeholder = (cleared) => typeof options.placeholder === 'function'
13
+ ? options.placeholder(cleared)
14
+ : (options.placeholder ??
15
+ `[Cleared to save context: the result of ${cleared.tool}, ${cleared.length} characters. Call the tool again if you need it.]`);
16
+ return {
17
+ name: 'context-editor',
18
+ beforeModel({ request }) {
19
+ const tokensBefore = estimate(request);
20
+ if (tokensBefore <= triggerTokens)
21
+ return;
22
+ const names = new Map();
23
+ for (const message of request.messages) {
24
+ for (const call of message.toolCalls ?? [])
25
+ names.set(call.id, call.function.name);
26
+ }
27
+ const results = request.messages.flatMap((message, index) => (message.role === 'tool' ? [index] : []));
28
+ const eligible = Math.max(0, results.length - keep);
29
+ const count = eligible < batch ? eligible : eligible - (eligible % batch);
30
+ const clearable = new Set(results.slice(0, count).filter((index) => {
31
+ const id = request.messages[index]?.toolCallId;
32
+ return !exclude.has(names.get(id ?? '') ?? '');
33
+ }));
34
+ if (clearable.size === 0)
35
+ return;
36
+ const clearedIds = new Set();
37
+ const messages = request.messages.map((message, index) => {
38
+ if (!clearable.has(index))
39
+ return message;
40
+ const text = (0, shared_js_1.textOf)(message.content);
41
+ const tool = names.get(message.toolCallId ?? '') ?? 'a tool';
42
+ clearedIds.add(message.toolCallId ?? '');
43
+ return { ...message, content: placeholder({ tool, length: text.length }) };
44
+ });
45
+ const edited = options.clearArguments
46
+ ? messages.map((message) => message.toolCalls?.some((call) => clearedIds.has(call.id))
47
+ ? {
48
+ ...message,
49
+ toolCalls: message.toolCalls.map((call) => clearedIds.has(call.id) ? { ...call, function: { ...call.function, arguments: '{}' } } : call),
50
+ }
51
+ : message)
52
+ : messages;
53
+ const next = { ...request, messages: edited };
54
+ options.onEdit?.({ cleared: clearable.size, tokensBefore, tokensAfter: estimate(next) });
55
+ return next;
56
+ },
57
+ };
58
+ }
59
+ function estimateRequestTokens(request) {
60
+ let characters = 0;
61
+ for (const message of request.messages) {
62
+ characters += (0, shared_js_1.textOf)(message.content).length + 4;
63
+ for (const call of message.toolCalls ?? [])
64
+ characters += call.function.name.length + call.function.arguments.length;
65
+ }
66
+ for (const tool of request.tools ?? []) {
67
+ characters += tool.name.length + tool.description.length + JSON.stringify(tool.parameters ?? {}).length;
68
+ }
69
+ return Math.ceil(characters / 4);
70
+ }
@@ -0,0 +1,44 @@
1
+ import type { AgentMiddleware } from '../create-agent.js';
2
+ import type { Sandbox } from '../sandbox.js';
3
+ /** Where `filesystemContext()` reads and writes: a `Sandbox`, or anything with the same file methods. */
4
+ export type FilesystemContextSource = Pick<Sandbox, 'readFile'> & Partial<Pick<Sandbox, 'writeFile' | 'listFiles'>>;
5
+ /** Where large tool results go instead of the transcript. */
6
+ export interface ContextOffloadOptions {
7
+ /** Results longer than this, in characters, are offloaded. Defaults to 20,000. */
8
+ overChars?: number;
9
+ /** The directory they are written to, inside the source. Defaults to `.context`. */
10
+ directory?: string;
11
+ /** How much of a result stays in the transcript as a preview, in characters. Defaults to 1,000. */
12
+ previewChars?: number;
13
+ /** Tools whose results are never offloaded. Defaults to `read_file`, so reading one back cannot loop. */
14
+ exclude?: readonly string[];
15
+ }
16
+ /** Options for `filesystemContext()`. */
17
+ export interface FilesystemContextOptions {
18
+ /** Where files are read from and offloaded to. */
19
+ source: FilesystemContextSource;
20
+ /**
21
+ * Files whose contents the model sees on every call, after the system prompt: project
22
+ * instructions such as `AGENTS.md`, conventions, a glossary. A missing file is skipped.
23
+ */
24
+ include?: readonly string[];
25
+ /** Lists a directory after the files, so the model knows what exists without a tool call. Needs `listFiles`. */
26
+ listing?: {
27
+ directory?: string;
28
+ maxEntries?: number;
29
+ };
30
+ /** The most characters of included files, together. Defaults to 32,000; the rest is cut. */
31
+ maxChars?: number;
32
+ /** Writes large tool results to a file and leaves a preview and the path. Needs `writeFile`. */
33
+ offload?: ContextOffloadOptions | boolean;
34
+ }
35
+ /**
36
+ * The filesystem as the agent's context: files in, large results out.
37
+ *
38
+ * Instruction files reach the model on every call without a tool call, read once per run and placed
39
+ * right after the system prompt, so the prefix a provider caches stays the same. A tool result too
40
+ * large for the context is written to a file instead, and the model reads a preview and a path it can
41
+ * open with `read_file` when it needs the rest: the transcript stays small however much a tool
42
+ * returns, and nothing is lost.
43
+ */
44
+ export declare function filesystemContext(options: FilesystemContextOptions): AgentMiddleware;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filesystemContext = filesystemContext;
4
+ function filesystemContext(options) {
5
+ const maxChars = options.maxChars ?? 32_000;
6
+ const offload = options.offload === true
7
+ ? {}
8
+ : options.offload === false || options.offload === undefined
9
+ ? undefined
10
+ : options.offload;
11
+ if (offload && !options.source.writeFile) {
12
+ throw new TypeError('filesystemContext() offloading needs a source with writeFile()');
13
+ }
14
+ const loaded = new Map();
15
+ const load = async () => {
16
+ const sections = [];
17
+ let budget = maxChars;
18
+ for (const file of options.include ?? []) {
19
+ if (budget <= 0)
20
+ break;
21
+ let text;
22
+ try {
23
+ text = await options.source.readFile(file);
24
+ }
25
+ catch {
26
+ continue;
27
+ }
28
+ const cut = text.length > budget ? `${text.slice(0, budget)}\n[cut: ${text.length - budget} more characters]` : text;
29
+ budget -= text.length;
30
+ sections.push(`<file path="${file}">\n${cut}\n</file>`);
31
+ }
32
+ if (options.listing && options.source.listFiles) {
33
+ try {
34
+ const entries = await options.source.listFiles(options.listing.directory ?? '.');
35
+ const max = options.listing.maxEntries ?? 200;
36
+ const shown = entries.slice(0, max).join('\n');
37
+ sections.push(`<listing directory="${options.listing.directory ?? '.'}">\n${shown}${entries.length > max ? `\n[${entries.length - max} more]` : ''}\n</listing>`);
38
+ }
39
+ catch {
40
+ }
41
+ }
42
+ return sections.length ? `Files from the workspace:\n\n${sections.join('\n\n')}` : undefined;
43
+ };
44
+ return {
45
+ name: 'filesystem-context',
46
+ async beforeModel({ request, state, threadId }) {
47
+ if (!options.include?.length && !options.listing)
48
+ return;
49
+ const turn = state.messages.filter((message) => message.role === 'user').length;
50
+ const key = `${threadId}:${turn}`;
51
+ let pending = loaded.get(key);
52
+ if (!pending) {
53
+ pending = load();
54
+ loaded.set(key, pending);
55
+ if (loaded.size > 256)
56
+ loaded.delete(loaded.keys().next().value);
57
+ }
58
+ const text = await pending;
59
+ if (!text)
60
+ return;
61
+ const systemCount = request.messages.findIndex((message) => message.role !== 'system');
62
+ const at = systemCount === -1 ? request.messages.length : systemCount;
63
+ const context = { role: 'system', content: text };
64
+ return { ...request, messages: [...request.messages.slice(0, at), context, ...request.messages.slice(at)] };
65
+ },
66
+ async wrapToolCall(call, next) {
67
+ const result = await next();
68
+ if (!offload || !result.ok || (offload.exclude ?? ['read_file']).includes(call.name))
69
+ return result;
70
+ const value = result.result;
71
+ if (value?.type === 'tool_output')
72
+ return result;
73
+ const text = typeof value === 'string' ? value : JSON.stringify(value);
74
+ if (text === undefined || text.length <= (offload.overChars ?? 20_000))
75
+ return result;
76
+ const directory = (offload.directory ?? '.context').replace(/\/+$/, '');
77
+ const file = `${directory}/${`${call.name}-${call.id}`.replace(/[^A-Za-z0-9._-]/g, '_')}.txt`;
78
+ await options.source.writeFile?.(file, text);
79
+ const preview = text.slice(0, offload.previewChars ?? 1_000);
80
+ return {
81
+ ok: true,
82
+ result: `${preview}\n\n[This result is ${text.length} characters; the first ${preview.length} are above. The whole result is in ${file}: read it with read_file when you need the rest.]`,
83
+ };
84
+ },
85
+ };
86
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Middleware for `createAgent()`, each its own import: retries, fallbacks, model choice, tool
3
+ * selection, context editing, personal data, approvals, limits, and the filesystem as context.
4
+ *
5
+ * Each is ordinary `AgentMiddleware` built on the same hooks an application's own middleware uses,
6
+ * so any of them can be read, copied, and changed. None adds a dependency, and a bundler keeps only
7
+ * the ones imported.
8
+ */
9
+ export { limitToolCalls, redactMessages, type RedactOptions, summarizeHistory, type SummarizeOptions, } from './basic.js';
10
+ export { type ClearedToolResult, type ContextEdit, contextEditor, type ContextEditorOptions, estimateRequestTokens, } from './context-editor.js';
11
+ export { type ContextOffloadOptions, filesystemContext, type FilesystemContextOptions, type FilesystemContextSource, } from './filesystem.js';
12
+ export { humanApproval, type HumanApprovalOptions } from './approval.js';
13
+ export { modelCallLimit, ModelCallLimitError, type ModelCallLimitOptions } from './limits.js';
14
+ export { type DynamicModelContext, dynamicModel, modelFallback, type ModelFallbackEvent, type ModelFallbackOptions, type ModelFallbackTarget, } from './models.js';
15
+ export { PiiBlockedError, type PiiFinding, type PiiKind, piiMiddleware, type PiiMiddlewareOptions, type PiiStrategy, type PiiWhere, } from './pii.js';
16
+ export { modelRetry, type ModelRetryOptions, type RetryEvent, toolRetry, type ToolRetryOptions } from './retry.js';
17
+ export type { RetryBackoff } from './shared.js';
18
+ export { type ToolSelection, type ToolSelectionContext, toolSelector, type ToolSelectorOptions, } from './tool-selector.js';
19
+ export type { AgentMiddleware, AgentMiddlewareContext, AgentModelCallContext, AgentToolCall, AgentToolResult, } from '../create-agent.js';
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toolSelector = exports.toolRetry = exports.modelRetry = exports.piiMiddleware = exports.PiiBlockedError = exports.modelFallback = exports.dynamicModel = exports.ModelCallLimitError = exports.modelCallLimit = exports.humanApproval = exports.filesystemContext = exports.estimateRequestTokens = exports.contextEditor = exports.summarizeHistory = exports.redactMessages = exports.limitToolCalls = void 0;
4
+ var basic_js_1 = require("./basic.js");
5
+ Object.defineProperty(exports, "limitToolCalls", { enumerable: true, get: function () { return basic_js_1.limitToolCalls; } });
6
+ Object.defineProperty(exports, "redactMessages", { enumerable: true, get: function () { return basic_js_1.redactMessages; } });
7
+ Object.defineProperty(exports, "summarizeHistory", { enumerable: true, get: function () { return basic_js_1.summarizeHistory; } });
8
+ var context_editor_js_1 = require("./context-editor.js");
9
+ Object.defineProperty(exports, "contextEditor", { enumerable: true, get: function () { return context_editor_js_1.contextEditor; } });
10
+ Object.defineProperty(exports, "estimateRequestTokens", { enumerable: true, get: function () { return context_editor_js_1.estimateRequestTokens; } });
11
+ var filesystem_js_1 = require("./filesystem.js");
12
+ Object.defineProperty(exports, "filesystemContext", { enumerable: true, get: function () { return filesystem_js_1.filesystemContext; } });
13
+ var approval_js_1 = require("./approval.js");
14
+ Object.defineProperty(exports, "humanApproval", { enumerable: true, get: function () { return approval_js_1.humanApproval; } });
15
+ var limits_js_1 = require("./limits.js");
16
+ Object.defineProperty(exports, "modelCallLimit", { enumerable: true, get: function () { return limits_js_1.modelCallLimit; } });
17
+ Object.defineProperty(exports, "ModelCallLimitError", { enumerable: true, get: function () { return limits_js_1.ModelCallLimitError; } });
18
+ var models_js_1 = require("./models.js");
19
+ Object.defineProperty(exports, "dynamicModel", { enumerable: true, get: function () { return models_js_1.dynamicModel; } });
20
+ Object.defineProperty(exports, "modelFallback", { enumerable: true, get: function () { return models_js_1.modelFallback; } });
21
+ var pii_js_1 = require("./pii.js");
22
+ Object.defineProperty(exports, "PiiBlockedError", { enumerable: true, get: function () { return pii_js_1.PiiBlockedError; } });
23
+ Object.defineProperty(exports, "piiMiddleware", { enumerable: true, get: function () { return pii_js_1.piiMiddleware; } });
24
+ var retry_js_1 = require("./retry.js");
25
+ Object.defineProperty(exports, "modelRetry", { enumerable: true, get: function () { return retry_js_1.modelRetry; } });
26
+ Object.defineProperty(exports, "toolRetry", { enumerable: true, get: function () { return retry_js_1.toolRetry; } });
27
+ var tool_selector_js_1 = require("./tool-selector.js");
28
+ Object.defineProperty(exports, "toolSelector", { enumerable: true, get: function () { return tool_selector_js_1.toolSelector; } });