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.
- package/API_STABILITY.md +39 -0
- package/CHANGELOG.md +142 -1
- package/README.md +10 -5
- package/dist/agent/capabilities.d.ts +39 -0
- package/dist/agent/capabilities.js +36 -0
- package/dist/agent/create-agent.d.ts +61 -11
- package/dist/agent/create-agent.js +136 -22
- package/dist/agent/index.d.ts +3 -2
- package/dist/agent/index.js +1 -1
- package/dist/agent/middleware/approval.d.ts +26 -0
- package/dist/agent/middleware/approval.js +23 -0
- package/dist/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
- package/dist/agent/{middleware.js → middleware/basic.js} +3 -5
- package/dist/agent/middleware/context-editor.d.ts +52 -0
- package/dist/agent/middleware/context-editor.js +66 -0
- package/dist/agent/middleware/filesystem.d.ts +44 -0
- package/dist/agent/middleware/filesystem.js +83 -0
- package/dist/agent/middleware/index.d.ts +19 -0
- package/dist/agent/middleware/index.js +9 -0
- package/dist/agent/middleware/limits.d.ts +42 -0
- package/dist/agent/middleware/limits.js +52 -0
- package/dist/agent/middleware/models.d.ts +48 -0
- package/dist/agent/middleware/models.js +44 -0
- package/dist/agent/middleware/pii.d.ts +67 -0
- package/dist/agent/middleware/pii.js +173 -0
- package/dist/agent/middleware/retry.d.ts +66 -0
- package/dist/agent/middleware/retry.js +59 -0
- package/dist/agent/middleware/shared.d.ts +41 -0
- package/dist/agent/middleware/shared.js +73 -0
- package/dist/agent/middleware/tool-selector.d.ts +64 -0
- package/dist/agent/middleware/tool-selector.js +106 -0
- package/dist/agent/permissions.d.ts +113 -0
- package/dist/agent/permissions.js +180 -0
- package/dist/agent/sandbox.d.ts +151 -0
- package/dist/agent/sandbox.js +257 -0
- package/dist/agent/tool.d.ts +8 -3
- package/dist/agent/tool.js +14 -3
- package/dist/deep-agent/index.d.ts +109 -0
- package/dist/deep-agent/index.js +216 -0
- package/dist/graph/functional.d.ts +9 -0
- package/dist/graph/functional.js +20 -3
- package/dist/graph/graph.js +25 -4
- package/dist/graph/lint.d.ts +4 -1
- package/dist/graph/lint.js +35 -0
- package/dist/postgres/fulltext.d.ts +67 -0
- package/dist/postgres/fulltext.js +91 -0
- package/dist/rag/elasticsearch.d.ts +71 -0
- package/dist/rag/elasticsearch.js +130 -0
- package/dist/rag/pipeline.d.ts +140 -0
- package/dist/rag/pipeline.js +187 -0
- package/dist/rag/rerankers.d.ts +92 -0
- package/dist/rag/rerankers.js +97 -0
- package/dist/rag/retrievers.d.ts +12 -1
- package/dist/server/assistant.d.ts +5 -1
- package/dist/server/assistant.js +4 -2
- package/dist/server/auth.d.ts +250 -0
- package/dist/server/auth.js +392 -0
- package/dist/store/memory.d.ts +10 -0
- package/dist/store/memory.js +10 -0
- package/dist/types/graph.d.ts +38 -0
- package/dist/types/messages.d.ts +36 -2
- package/dist/types/principal.d.ts +21 -0
- package/dist/types/principal.js +1 -0
- package/dist/types/server.d.ts +2 -9
- package/dist/utils/tenant.d.ts +11 -0
- package/dist/utils/tenant.js +14 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist-cjs/agent/capabilities.d.ts +39 -0
- package/dist-cjs/agent/capabilities.js +41 -0
- package/dist-cjs/agent/create-agent.d.ts +61 -11
- package/dist-cjs/agent/create-agent.js +136 -22
- package/dist-cjs/agent/index.d.ts +3 -2
- package/dist-cjs/agent/index.js +4 -4
- package/dist-cjs/agent/middleware/approval.d.ts +26 -0
- package/dist-cjs/agent/middleware/approval.js +26 -0
- package/dist-cjs/agent/{middleware.d.ts → middleware/basic.d.ts} +5 -4
- package/dist-cjs/agent/{middleware.js → middleware/basic.js} +3 -5
- package/dist-cjs/agent/middleware/context-editor.d.ts +52 -0
- package/dist-cjs/agent/middleware/context-editor.js +70 -0
- package/dist-cjs/agent/middleware/filesystem.d.ts +44 -0
- package/dist-cjs/agent/middleware/filesystem.js +86 -0
- package/dist-cjs/agent/middleware/index.d.ts +19 -0
- package/dist-cjs/agent/middleware/index.js +28 -0
- package/dist-cjs/agent/middleware/limits.d.ts +42 -0
- package/dist-cjs/agent/middleware/limits.js +57 -0
- package/dist-cjs/agent/middleware/models.d.ts +48 -0
- package/dist-cjs/agent/middleware/models.js +48 -0
- package/dist-cjs/agent/middleware/pii.d.ts +67 -0
- package/dist-cjs/agent/middleware/pii.js +178 -0
- package/dist-cjs/agent/middleware/retry.d.ts +66 -0
- package/dist-cjs/agent/middleware/retry.js +63 -0
- package/dist-cjs/agent/middleware/shared.d.ts +41 -0
- package/dist-cjs/agent/middleware/shared.js +83 -0
- package/dist-cjs/agent/middleware/tool-selector.d.ts +64 -0
- package/dist-cjs/agent/middleware/tool-selector.js +109 -0
- package/dist-cjs/agent/permissions.d.ts +113 -0
- package/dist-cjs/agent/permissions.js +189 -0
- package/dist-cjs/agent/sandbox.d.ts +151 -0
- package/dist-cjs/agent/sandbox.js +297 -0
- package/dist-cjs/agent/tool.d.ts +8 -3
- package/dist-cjs/agent/tool.js +14 -3
- package/dist-cjs/deep-agent/index.d.ts +109 -0
- package/dist-cjs/deep-agent/index.js +219 -0
- package/dist-cjs/graph/functional.d.ts +9 -0
- package/dist-cjs/graph/functional.js +20 -3
- package/dist-cjs/graph/graph.js +25 -4
- package/dist-cjs/graph/lint.d.ts +4 -1
- package/dist-cjs/graph/lint.js +35 -0
- package/dist-cjs/postgres/fulltext.d.ts +67 -0
- package/dist-cjs/postgres/fulltext.js +129 -0
- package/dist-cjs/rag/elasticsearch.d.ts +71 -0
- package/dist-cjs/rag/elasticsearch.js +135 -0
- package/dist-cjs/rag/pipeline.d.ts +140 -0
- package/dist-cjs/rag/pipeline.js +191 -0
- package/dist-cjs/rag/rerankers.d.ts +92 -0
- package/dist-cjs/rag/rerankers.js +107 -0
- package/dist-cjs/rag/retrievers.d.ts +12 -1
- package/dist-cjs/server/assistant.d.ts +5 -1
- package/dist-cjs/server/assistant.js +4 -2
- package/dist-cjs/server/auth.d.ts +250 -0
- package/dist-cjs/server/auth.js +402 -0
- package/dist-cjs/store/memory.d.ts +10 -0
- package/dist-cjs/store/memory.js +10 -0
- package/dist-cjs/types/graph.d.ts +38 -0
- package/dist-cjs/types/messages.d.ts +36 -2
- package/dist-cjs/types/principal.d.ts +21 -0
- package/dist-cjs/types/principal.js +2 -0
- package/dist-cjs/types/server.d.ts +2 -9
- package/dist-cjs/utils/tenant.d.ts +11 -0
- package/dist-cjs/utils/tenant.js +15 -0
- package/dist-cjs/version.d.ts +1 -1
- package/dist-cjs/version.js +1 -1
- package/llms.txt +4 -3
- package/package.json +118 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AgentMiddleware } from '../create-agent.js';
|
|
2
|
+
/** Options for `modelCallLimit()`. Set `run`, `thread`, or both. */
|
|
3
|
+
export interface ModelCallLimitOptions {
|
|
4
|
+
/** Model calls in one run: from a user message to the answer, a resumed approval included. */
|
|
5
|
+
run?: number;
|
|
6
|
+
/**
|
|
7
|
+
* Model calls over a thread's whole life, every run on it included. Counted in the agent's store
|
|
8
|
+
* when it has one, so every process shares the count and it survives a restart; otherwise in this
|
|
9
|
+
* process. A budget, not a lock: two runs on one thread at the same moment may each pass it once.
|
|
10
|
+
*/
|
|
11
|
+
thread?: number;
|
|
12
|
+
/**
|
|
13
|
+
* What happens at a limit: `end` answers with `message` and stops with `stopReason: 'stopped'`,
|
|
14
|
+
* without calling the model; `error` fails the run with a `ModelCallLimitError`. Defaults to `end`.
|
|
15
|
+
*/
|
|
16
|
+
onLimit?: 'end' | 'error';
|
|
17
|
+
/** The answer a run ends with under `end`. Defaults to a sentence naming the limit. */
|
|
18
|
+
message?: string;
|
|
19
|
+
}
|
|
20
|
+
/** Thrown by `modelCallLimit()` under `onLimit: 'error'`. */
|
|
21
|
+
export declare class ModelCallLimitError extends Error {
|
|
22
|
+
/** Which limit was reached. */
|
|
23
|
+
readonly scope: 'run' | 'thread';
|
|
24
|
+
/** The limit. */
|
|
25
|
+
readonly limit: number;
|
|
26
|
+
/** Always `MODEL_CALL_LIMIT`. */
|
|
27
|
+
readonly code = "MODEL_CALL_LIMIT";
|
|
28
|
+
constructor(
|
|
29
|
+
/** Which limit was reached. */
|
|
30
|
+
scope: 'run' | 'thread',
|
|
31
|
+
/** The limit. */
|
|
32
|
+
limit: number);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Caps the model calls of a run, of a thread, or both.
|
|
36
|
+
*
|
|
37
|
+
* `maxIterations` stops one run that keeps calling tools; this is a budget an application sets per
|
|
38
|
+
* run and per conversation, so a thread a user keeps coming back to cannot spend without bound. The
|
|
39
|
+
* run count is read from the transcript, so it survives a restart. A run that reaches a limit ends
|
|
40
|
+
* with an answer rather than an error, unless you ask for one.
|
|
41
|
+
*/
|
|
42
|
+
export declare function modelCallLimit(options: ModelCallLimitOptions): AgentMiddleware;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModelCallLimitError = void 0;
|
|
4
|
+
exports.modelCallLimit = modelCallLimit;
|
|
5
|
+
const shared_js_1 = require("./shared.js");
|
|
6
|
+
class ModelCallLimitError extends Error {
|
|
7
|
+
scope;
|
|
8
|
+
limit;
|
|
9
|
+
code = 'MODEL_CALL_LIMIT';
|
|
10
|
+
constructor(scope, limit) {
|
|
11
|
+
super(`The agent reached its limit of ${limit} model calls per ${scope}`);
|
|
12
|
+
this.scope = scope;
|
|
13
|
+
this.limit = limit;
|
|
14
|
+
this.name = 'ModelCallLimitError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ModelCallLimitError = ModelCallLimitError;
|
|
18
|
+
const NAMESPACE = ['nexus', 'agent', 'model-calls'];
|
|
19
|
+
function modelCallLimit(options) {
|
|
20
|
+
if (options.run === undefined && options.thread === undefined) {
|
|
21
|
+
throw new RangeError('modelCallLimit() needs a run limit, a thread limit, or both');
|
|
22
|
+
}
|
|
23
|
+
const local = new Map();
|
|
24
|
+
return {
|
|
25
|
+
name: 'model-call-limit',
|
|
26
|
+
async wrapModelCall(request, next, context) {
|
|
27
|
+
let reached;
|
|
28
|
+
if (options.run !== undefined && (0, shared_js_1.modelCallsInTurn)(context.state.messages) >= options.run) {
|
|
29
|
+
reached = ['run', options.run];
|
|
30
|
+
}
|
|
31
|
+
let used = 0;
|
|
32
|
+
if (!reached && options.thread !== undefined) {
|
|
33
|
+
used = context.store
|
|
34
|
+
? ((await context.store.get(NAMESPACE, context.threadId))?.value ?? 0)
|
|
35
|
+
: (local.get(context.threadId) ?? 0);
|
|
36
|
+
if (used >= options.thread)
|
|
37
|
+
reached = ['thread', options.thread];
|
|
38
|
+
}
|
|
39
|
+
if (reached) {
|
|
40
|
+
const [scope, limit] = reached;
|
|
41
|
+
if (options.onLimit === 'error')
|
|
42
|
+
throw new ModelCallLimitError(scope, limit);
|
|
43
|
+
return context.stop(options.message ?? `I stopped here: this ${scope} reached its limit of ${limit} model calls.`);
|
|
44
|
+
}
|
|
45
|
+
if (options.thread !== undefined) {
|
|
46
|
+
if (context.store)
|
|
47
|
+
await context.store.put(NAMESPACE, context.threadId, used + 1);
|
|
48
|
+
else {
|
|
49
|
+
local.set(context.threadId, used + 1);
|
|
50
|
+
if (local.size > 10_000)
|
|
51
|
+
local.delete(local.keys().next().value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return next(request);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CompletionRequest } from '../../types/messages.js';
|
|
2
|
+
import type { AgentMiddleware, AgentMiddlewareContext, AgentModelClient } from '../create-agent.js';
|
|
3
|
+
/** A model to fall back to: a model name for the agent's own client, or another client. */
|
|
4
|
+
export type ModelFallbackTarget = string | {
|
|
5
|
+
client: AgentModelClient;
|
|
6
|
+
model?: string;
|
|
7
|
+
};
|
|
8
|
+
/** One fallback about to happen. */
|
|
9
|
+
export interface ModelFallbackEvent {
|
|
10
|
+
/** The model that failed. */
|
|
11
|
+
from: string;
|
|
12
|
+
/** The model tried next. */
|
|
13
|
+
to: string;
|
|
14
|
+
/** What the failed call threw. */
|
|
15
|
+
error: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** Options for `modelFallback()`. */
|
|
18
|
+
export interface ModelFallbackOptions {
|
|
19
|
+
/** Whether an error moves on to the next model. Defaults to every error but a cancellation. */
|
|
20
|
+
fallbackOn?: (error: unknown, model: string) => boolean;
|
|
21
|
+
/** Called before each fallback, for logs and metrics. */
|
|
22
|
+
onFallback?: (event: ModelFallbackEvent) => void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Tries other models, in order, when the model call fails.
|
|
26
|
+
*
|
|
27
|
+
* The client's router already falls back between providers for one request; this is the fallback
|
|
28
|
+
* it cannot make: to a client of a different kind, or once the client's own routes are exhausted. A
|
|
29
|
+
* target with its own client is called through `complete()`, never streamed, and middleware after
|
|
30
|
+
* this one does not wrap it.
|
|
31
|
+
*/
|
|
32
|
+
export declare function modelFallback(models: readonly ModelFallbackTarget[], options?: ModelFallbackOptions): AgentMiddleware;
|
|
33
|
+
/** What `dynamicModel()` decides from: the step's context and the request about to be sent. */
|
|
34
|
+
export type DynamicModelContext = AgentMiddlewareContext & {
|
|
35
|
+
request: CompletionRequest;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Chooses the model for each call from the run so far.
|
|
39
|
+
*
|
|
40
|
+
* A cheap model for the first steps and a stronger one once the task proves hard, a model with a
|
|
41
|
+
* longer context once the transcript grows, or a tenant's own model. Returning nothing keeps the
|
|
42
|
+
* model the request already names.
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* dynamicModel(({ state }) => (state.iterations >= 4 ? 'gpt-5' : 'gpt-5-mini'))
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function dynamicModel(select: (context: DynamicModelContext) => string | undefined | Promise<string | undefined>): AgentMiddleware;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelFallback = modelFallback;
|
|
4
|
+
exports.dynamicModel = dynamicModel;
|
|
5
|
+
const shared_js_1 = require("./shared.js");
|
|
6
|
+
function modelFallback(models, options = {}) {
|
|
7
|
+
if (models.length === 0)
|
|
8
|
+
throw new RangeError('modelFallback() needs at least one model to fall back to');
|
|
9
|
+
return {
|
|
10
|
+
name: 'model-fallback',
|
|
11
|
+
async wrapModelCall(request, next, context) {
|
|
12
|
+
try {
|
|
13
|
+
return await next(request);
|
|
14
|
+
}
|
|
15
|
+
catch (first) {
|
|
16
|
+
let error = first;
|
|
17
|
+
let from = request.model;
|
|
18
|
+
for (const target of models) {
|
|
19
|
+
if ((0, shared_js_1.isCancellation)(error, context.signal) || !(options.fallbackOn?.(error, from) ?? true))
|
|
20
|
+
throw error;
|
|
21
|
+
const model = typeof target === 'string' ? target : (target.model ?? request.model);
|
|
22
|
+
options.onFallback?.({ from, to: model, error });
|
|
23
|
+
try {
|
|
24
|
+
return typeof target === 'string'
|
|
25
|
+
? await next({ ...request, model })
|
|
26
|
+
: await target.client.complete({ ...request, model });
|
|
27
|
+
}
|
|
28
|
+
catch (failure) {
|
|
29
|
+
error = failure;
|
|
30
|
+
from = model;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function dynamicModel(select) {
|
|
39
|
+
return {
|
|
40
|
+
name: 'dynamic-model',
|
|
41
|
+
async beforeModel(context) {
|
|
42
|
+
const model = await select(context);
|
|
43
|
+
if (!model || model === context.request.model)
|
|
44
|
+
return;
|
|
45
|
+
return { ...context.request, model };
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { AgentMiddleware } from '../create-agent.js';
|
|
2
|
+
/** The personal data and secrets `piiMiddleware()` finds on its own. */
|
|
3
|
+
export type PiiKind = 'email' | 'credit-card' | 'ssn' | 'phone' | 'iban' | 'ip-address' | 'secret';
|
|
4
|
+
/**
|
|
5
|
+
* What happens to a match: `redact` replaces it with its kind, `mask` keeps its last four
|
|
6
|
+
* characters, `hash` replaces it with a stable token so two values stay distinguishable, and `block`
|
|
7
|
+
* refuses the whole message.
|
|
8
|
+
*/
|
|
9
|
+
export type PiiStrategy = 'redact' | 'mask' | 'hash' | 'block';
|
|
10
|
+
/** Where a match was found. */
|
|
11
|
+
export type PiiWhere = 'input' | 'output' | 'tool';
|
|
12
|
+
/** One match, as `onDetect` sees it: never the value itself. */
|
|
13
|
+
export interface PiiFinding {
|
|
14
|
+
/** Its kind: a built-in kind or a name from `patterns`. */
|
|
15
|
+
kind: string;
|
|
16
|
+
/** Where it was found. */
|
|
17
|
+
where: PiiWhere;
|
|
18
|
+
/** What was done with it. */
|
|
19
|
+
strategy: PiiStrategy;
|
|
20
|
+
}
|
|
21
|
+
/** Options for `piiMiddleware()`. */
|
|
22
|
+
export interface PiiMiddlewareOptions {
|
|
23
|
+
/** The built-in kinds to find. Defaults to all of them. */
|
|
24
|
+
kinds?: readonly PiiKind[];
|
|
25
|
+
/** Kinds of your own, by name: `{ 'employee-id': /\bEMP-\d{6}\b/ }`. */
|
|
26
|
+
patterns?: Readonly<Record<string, RegExp>>;
|
|
27
|
+
/**
|
|
28
|
+
* What happens to a match, for every kind or per kind: `'mask'`, or
|
|
29
|
+
* `{ email: 'hash', 'credit-card': 'block' }`. Kinds not named are redacted.
|
|
30
|
+
*/
|
|
31
|
+
strategy?: PiiStrategy | Readonly<Record<string, PiiStrategy>>;
|
|
32
|
+
/**
|
|
33
|
+
* Where to look. `input` is every message sent to the model but the system prompt; `output` is
|
|
34
|
+
* the model's answer; `toolResults` cleans a result before it enters the transcript, so it is never
|
|
35
|
+
* checkpointed either. Defaults to input and tool results.
|
|
36
|
+
*/
|
|
37
|
+
apply?: {
|
|
38
|
+
input?: boolean;
|
|
39
|
+
output?: boolean;
|
|
40
|
+
toolResults?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/** Called for each match, for an audit trail. */
|
|
43
|
+
onDetect?: (finding: PiiFinding) => void;
|
|
44
|
+
}
|
|
45
|
+
/** Thrown when a match under the `block` strategy is found in the model's input or output. */
|
|
46
|
+
export declare class PiiBlockedError extends Error {
|
|
47
|
+
/** The kind that was found. */
|
|
48
|
+
readonly kind: string;
|
|
49
|
+
/** Where it was found. */
|
|
50
|
+
readonly where: PiiWhere;
|
|
51
|
+
/** Always `PII_BLOCKED`. */
|
|
52
|
+
readonly code = "PII_BLOCKED";
|
|
53
|
+
constructor(
|
|
54
|
+
/** The kind that was found. */
|
|
55
|
+
kind: string,
|
|
56
|
+
/** Where it was found. */
|
|
57
|
+
where: PiiWhere);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Keeps personal data and secrets away from the model, the transcript, or both.
|
|
61
|
+
*
|
|
62
|
+
* Finds emails, card numbers (checked with Luhn), US social security numbers, phone numbers, IBANs
|
|
63
|
+
* (checked with their checksum), IP addresses, and common API keys and private keys, plus any kinds
|
|
64
|
+
* of your own; and redacts, masks, hashes, or blocks each. The validated kinds keep false positives
|
|
65
|
+
* low: an order number is not a card number unless its checksum says so.
|
|
66
|
+
*/
|
|
67
|
+
export declare function piiMiddleware(options?: PiiMiddlewareOptions): AgentMiddleware;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PiiBlockedError = void 0;
|
|
4
|
+
exports.piiMiddleware = piiMiddleware;
|
|
5
|
+
const shared_js_1 = require("./shared.js");
|
|
6
|
+
class PiiBlockedError extends Error {
|
|
7
|
+
kind;
|
|
8
|
+
where;
|
|
9
|
+
code = 'PII_BLOCKED';
|
|
10
|
+
constructor(kind, where) {
|
|
11
|
+
super(`The ${where === 'input' ? 'request' : 'response'} contains ${kind}, which is blocked`);
|
|
12
|
+
this.kind = kind;
|
|
13
|
+
this.where = where;
|
|
14
|
+
this.name = 'PiiBlockedError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.PiiBlockedError = PiiBlockedError;
|
|
18
|
+
const DETECTORS = {
|
|
19
|
+
secret: {
|
|
20
|
+
pattern: /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----|\b(?:sk-[A-Za-z0-9_-]{20,}|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{36,}|xox[abprs]-[A-Za-z0-9-]{10,}|AIza[0-9A-Za-z_-]{35})\b/g,
|
|
21
|
+
},
|
|
22
|
+
email: { pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g },
|
|
23
|
+
'credit-card': { pattern: /\b\d(?:[ -]?\d){12,18}\b/g, valid: luhn },
|
|
24
|
+
iban: { pattern: /\b[A-Z]{2}\d{2}(?: ?[A-Z0-9]{4}){2,7}(?: ?[A-Z0-9]{1,4})?\b/g, valid: ibanChecksum },
|
|
25
|
+
ssn: { pattern: /\b(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}\b/g },
|
|
26
|
+
'ip-address': { pattern: /\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b/g },
|
|
27
|
+
phone: {
|
|
28
|
+
pattern: /(?<![\w+])\+?(?:\(\d{1,4}\)|\d{1,4})(?:[ .-]?(?:\(\d{1,4}\)|\d{2,4})){2,4}(?!\w)/g,
|
|
29
|
+
valid: (match) => {
|
|
30
|
+
const digits = match.replace(/\D/g, '').length;
|
|
31
|
+
return digits >= 10 && digits <= 15 && (/^\+/.test(match) || /[ .()-]/.test(match));
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
const ORDER = ['secret', 'email', 'credit-card', 'iban', 'ssn', 'ip-address', 'phone'];
|
|
36
|
+
function piiMiddleware(options = {}) {
|
|
37
|
+
const kinds = new Set(options.kinds ?? ORDER);
|
|
38
|
+
const detectors = [
|
|
39
|
+
...ORDER.filter((kind) => kinds.has(kind)).map((kind) => [kind, DETECTORS[kind]]),
|
|
40
|
+
...Object.entries(options.patterns ?? {}).map(([kind, pattern]) => [
|
|
41
|
+
kind,
|
|
42
|
+
{ pattern: pattern.flags.includes('g') ? pattern : new RegExp(pattern.source, `${pattern.flags}g`) },
|
|
43
|
+
]),
|
|
44
|
+
];
|
|
45
|
+
const apply = { input: true, output: false, toolResults: true, ...options.apply };
|
|
46
|
+
const strategyOf = (kind) => typeof options.strategy === 'string' ? options.strategy : (options.strategy?.[kind] ?? 'redact');
|
|
47
|
+
const clean = (text, where) => {
|
|
48
|
+
let current = text;
|
|
49
|
+
for (const [kind, detector] of detectors) {
|
|
50
|
+
const strategy = strategyOf(kind);
|
|
51
|
+
let blocked = false;
|
|
52
|
+
current = current.replace(detector.pattern, (match) => {
|
|
53
|
+
if (detector.valid && !detector.valid(match))
|
|
54
|
+
return match;
|
|
55
|
+
options.onDetect?.({ kind, where, strategy });
|
|
56
|
+
if (strategy === 'block')
|
|
57
|
+
blocked = true;
|
|
58
|
+
return replace(match, kind, strategy);
|
|
59
|
+
});
|
|
60
|
+
if (blocked)
|
|
61
|
+
return { blocked: kind };
|
|
62
|
+
}
|
|
63
|
+
return { text: current };
|
|
64
|
+
};
|
|
65
|
+
const cleanMessage = (message, where) => {
|
|
66
|
+
if (typeof message.content === 'string') {
|
|
67
|
+
const result = clean(message.content, where);
|
|
68
|
+
if ('blocked' in result)
|
|
69
|
+
throw new PiiBlockedError(result.blocked, where);
|
|
70
|
+
return result.text === message.content ? message : { ...message, content: result.text };
|
|
71
|
+
}
|
|
72
|
+
let changed = false;
|
|
73
|
+
const content = message.content.map((part) => {
|
|
74
|
+
if (part.type !== 'text')
|
|
75
|
+
return part;
|
|
76
|
+
const result = clean(part.text, where);
|
|
77
|
+
if ('blocked' in result)
|
|
78
|
+
throw new PiiBlockedError(result.blocked, where);
|
|
79
|
+
if (result.text === part.text)
|
|
80
|
+
return part;
|
|
81
|
+
changed = true;
|
|
82
|
+
return { ...part, text: result.text };
|
|
83
|
+
});
|
|
84
|
+
return changed ? { ...message, content } : message;
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
name: 'pii',
|
|
88
|
+
beforeModel({ request }) {
|
|
89
|
+
if (!apply.input)
|
|
90
|
+
return;
|
|
91
|
+
return {
|
|
92
|
+
...request,
|
|
93
|
+
messages: request.messages.map((message) => message.role === 'system' ? message : cleanMessage(message, 'input')),
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
afterModel({ response }) {
|
|
97
|
+
if (!apply.output || !response.content)
|
|
98
|
+
return;
|
|
99
|
+
const result = clean(response.content, 'output');
|
|
100
|
+
if ('blocked' in result)
|
|
101
|
+
throw new PiiBlockedError(result.blocked, 'output');
|
|
102
|
+
return result.text === response.content ? undefined : { ...response, content: result.text };
|
|
103
|
+
},
|
|
104
|
+
async wrapToolCall(_call, next) {
|
|
105
|
+
const result = await next();
|
|
106
|
+
if (!apply.toolResults || !result.ok)
|
|
107
|
+
return result;
|
|
108
|
+
let blocked;
|
|
109
|
+
const walk = (value) => {
|
|
110
|
+
if (blocked)
|
|
111
|
+
return value;
|
|
112
|
+
if (typeof value === 'string') {
|
|
113
|
+
const cleaned = clean(value, 'tool');
|
|
114
|
+
if ('blocked' in cleaned) {
|
|
115
|
+
blocked = cleaned.blocked;
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
return cleaned.text;
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(value))
|
|
121
|
+
return value.map(walk);
|
|
122
|
+
if (isToolOutput(value)) {
|
|
123
|
+
const content = value.content.map((part) => part.type === 'text' ? { ...part, text: walk(part.text) } : part);
|
|
124
|
+
return { ...value, content };
|
|
125
|
+
}
|
|
126
|
+
if (value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
|
|
127
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, walk(entry)]));
|
|
128
|
+
}
|
|
129
|
+
return value;
|
|
130
|
+
};
|
|
131
|
+
const cleaned = walk(result.result);
|
|
132
|
+
if (blocked)
|
|
133
|
+
return { ok: false, error: `The tool's result was withheld: it contains ${blocked}` };
|
|
134
|
+
return { ...result, result: cleaned };
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function replace(match, kind, strategy) {
|
|
139
|
+
if (strategy === 'mask') {
|
|
140
|
+
const visible = match.replace(/[^A-Za-z0-9]/g, '').slice(-4);
|
|
141
|
+
return `${'*'.repeat(Math.max(4, match.length - visible.length))}${visible}`;
|
|
142
|
+
}
|
|
143
|
+
if (strategy === 'hash')
|
|
144
|
+
return `[${kind}:${(0, shared_js_1.shortHash)(match)}]`;
|
|
145
|
+
return `[${kind}]`;
|
|
146
|
+
}
|
|
147
|
+
function isToolOutput(value) {
|
|
148
|
+
return value?.type === 'tool_output' && Array.isArray(value.content);
|
|
149
|
+
}
|
|
150
|
+
function luhn(match) {
|
|
151
|
+
const digits = match.replace(/\D/g, '');
|
|
152
|
+
if (digits.length < 13 || digits.length > 19)
|
|
153
|
+
return false;
|
|
154
|
+
let sum = 0;
|
|
155
|
+
for (let index = 0; index < digits.length; index += 1) {
|
|
156
|
+
let digit = Number(digits[digits.length - 1 - index]);
|
|
157
|
+
if (index % 2 === 1) {
|
|
158
|
+
digit *= 2;
|
|
159
|
+
if (digit > 9)
|
|
160
|
+
digit -= 9;
|
|
161
|
+
}
|
|
162
|
+
sum += digit;
|
|
163
|
+
}
|
|
164
|
+
return sum % 10 === 0;
|
|
165
|
+
}
|
|
166
|
+
function ibanChecksum(match) {
|
|
167
|
+
const iban = match.replace(/ /g, '');
|
|
168
|
+
if (iban.length < 15 || iban.length > 34)
|
|
169
|
+
return false;
|
|
170
|
+
const rearranged = `${iban.slice(4)}${iban.slice(0, 4)}`;
|
|
171
|
+
let remainder = 0;
|
|
172
|
+
for (const char of rearranged) {
|
|
173
|
+
const value = /\d/.test(char) ? char : String(char.charCodeAt(0) - 55);
|
|
174
|
+
for (const digit of value)
|
|
175
|
+
remainder = (remainder * 10 + Number(digit)) % 97;
|
|
176
|
+
}
|
|
177
|
+
return remainder === 1;
|
|
178
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { NexusResponse } from '../../types/response.js';
|
|
2
|
+
import type { AgentMiddleware, AgentToolCall, AgentToolResult } from '../create-agent.js';
|
|
3
|
+
import { type RetryBackoff } from './shared.js';
|
|
4
|
+
/** One retry about to happen. */
|
|
5
|
+
export interface RetryEvent {
|
|
6
|
+
/** The attempt that failed, from 1. */
|
|
7
|
+
attempt: number;
|
|
8
|
+
/** How long until the next attempt, in milliseconds. */
|
|
9
|
+
delayMs: number;
|
|
10
|
+
/** The error a model call threw, when it threw. */
|
|
11
|
+
error?: unknown;
|
|
12
|
+
/** The failed tool call's result, for a tool retry. */
|
|
13
|
+
result?: AgentToolResult;
|
|
14
|
+
/** The tool's name, for a tool retry. */
|
|
15
|
+
tool?: string;
|
|
16
|
+
}
|
|
17
|
+
/** Options for `modelRetry()`. */
|
|
18
|
+
export interface ModelRetryOptions {
|
|
19
|
+
/** Attempts in all, the first included. Defaults to 3. */
|
|
20
|
+
attempts?: number;
|
|
21
|
+
/** How long to wait between attempts. */
|
|
22
|
+
backoff?: RetryBackoff;
|
|
23
|
+
/**
|
|
24
|
+
* Whether an error is worth another attempt. Defaults to every error not marked
|
|
25
|
+
* `retryable: false`, such as a provider's authentication error, and never a cancellation.
|
|
26
|
+
*/
|
|
27
|
+
retryOn?: (error: unknown, attempt: number) => boolean;
|
|
28
|
+
/** Whether a response that did arrive is tried again, such as an empty one. Defaults to never. */
|
|
29
|
+
retryResponse?: (response: NexusResponse, attempt: number) => boolean;
|
|
30
|
+
/** Called before each new attempt, for logs and metrics. */
|
|
31
|
+
onRetry?: (event: RetryEvent) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Tries a failed model call again, with exponential backoff.
|
|
35
|
+
*
|
|
36
|
+
* The client already retries one provider request; this retries the agent's step as a whole, after
|
|
37
|
+
* the client gave up, and can retry a response that arrived but is unusable. A streamed call that
|
|
38
|
+
* fails halfway streams again from the start. Waits stop when the run is cancelled.
|
|
39
|
+
*/
|
|
40
|
+
export declare function modelRetry(options?: ModelRetryOptions): AgentMiddleware;
|
|
41
|
+
/** Options for `toolRetry()`. */
|
|
42
|
+
export interface ToolRetryOptions {
|
|
43
|
+
/** Attempts in all, the first included. Defaults to 3. */
|
|
44
|
+
attempts?: number;
|
|
45
|
+
/** How long to wait between attempts. */
|
|
46
|
+
backoff?: RetryBackoff;
|
|
47
|
+
/**
|
|
48
|
+
* Which calls may run again: tool names, `'all'`, or a function of the call. Defaults to calls
|
|
49
|
+
* that are safe to repeat, whose tool declares capabilities and none of them writes, runs a
|
|
50
|
+
* command or code, reaches the network, or is the application's own. A call that may already have
|
|
51
|
+
* changed something is retried only when you say so.
|
|
52
|
+
*/
|
|
53
|
+
tools?: readonly string[] | 'all' | ((call: AgentToolCall) => boolean);
|
|
54
|
+
/** Whether a result is a failure worth another attempt. Defaults to any result with `ok: false`. */
|
|
55
|
+
retryOn?: (result: AgentToolResult, call: AgentToolCall, attempt: number) => boolean;
|
|
56
|
+
/** Called before each new attempt, for logs and metrics. */
|
|
57
|
+
onRetry?: (event: RetryEvent) => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Runs a failed tool call again, with exponential backoff.
|
|
61
|
+
*
|
|
62
|
+
* By default only a call that cannot have changed anything is repeated: retrying a payment or a
|
|
63
|
+
* write that timed out after it landed would do it twice. Name the tools that are idempotent to
|
|
64
|
+
* retry those too.
|
|
65
|
+
*/
|
|
66
|
+
export declare function toolRetry(options?: ToolRetryOptions): AgentMiddleware;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelRetry = modelRetry;
|
|
4
|
+
exports.toolRetry = toolRetry;
|
|
5
|
+
const capabilities_js_1 = require("../capabilities.js");
|
|
6
|
+
const shared_js_1 = require("./shared.js");
|
|
7
|
+
function modelRetry(options = {}) {
|
|
8
|
+
const attempts = Math.max(1, options.attempts ?? 3);
|
|
9
|
+
const retryOn = options.retryOn ?? ((error) => error?.retryable !== false);
|
|
10
|
+
return {
|
|
11
|
+
name: 'model-retry',
|
|
12
|
+
async wrapModelCall(request, next, context) {
|
|
13
|
+
for (let attempt = 1;; attempt += 1) {
|
|
14
|
+
try {
|
|
15
|
+
const response = await next(request);
|
|
16
|
+
if (attempt < attempts && options.retryResponse?.(response, attempt)) {
|
|
17
|
+
const delayMs = (0, shared_js_1.backoffDelay)(attempt, options.backoff);
|
|
18
|
+
options.onRetry?.({ attempt, delayMs });
|
|
19
|
+
await (0, shared_js_1.sleep)(delayMs, context.signal);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (attempt >= attempts || (0, shared_js_1.isCancellation)(error, context.signal) || !retryOn(error, attempt))
|
|
26
|
+
throw error;
|
|
27
|
+
const delayMs = (0, shared_js_1.backoffDelay)(attempt, options.backoff);
|
|
28
|
+
options.onRetry?.({ attempt, delayMs, error });
|
|
29
|
+
await (0, shared_js_1.sleep)(delayMs, context.signal);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function toolRetry(options = {}) {
|
|
36
|
+
const attempts = Math.max(1, options.attempts ?? 3);
|
|
37
|
+
const retryOn = options.retryOn ?? ((result) => !result.ok);
|
|
38
|
+
const eligible = (call) => {
|
|
39
|
+
const tools = options.tools;
|
|
40
|
+
if (tools === 'all')
|
|
41
|
+
return true;
|
|
42
|
+
if (typeof tools === 'function')
|
|
43
|
+
return tools(call);
|
|
44
|
+
if (tools)
|
|
45
|
+
return tools.includes(call.name);
|
|
46
|
+
return call.capabilities !== undefined && !call.capabilities.some(capabilities_js_1.isSensitiveCapability);
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
name: 'tool-retry',
|
|
50
|
+
async wrapToolCall(call, next, context) {
|
|
51
|
+
if (!eligible(call))
|
|
52
|
+
return next();
|
|
53
|
+
for (let attempt = 1;; attempt += 1) {
|
|
54
|
+
const result = await next();
|
|
55
|
+
if (attempt >= attempts || context.signal.aborted || !retryOn(result, call, attempt))
|
|
56
|
+
return result;
|
|
57
|
+
const delayMs = (0, shared_js_1.backoffDelay)(attempt, options.backoff);
|
|
58
|
+
options.onRetry?.({ attempt, delayMs, result, tool: call.name });
|
|
59
|
+
await (0, shared_js_1.sleep)(delayMs, context.signal);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Message } from '../../types/messages.js';
|
|
2
|
+
import type { AgentToolCall } from '../create-agent.js';
|
|
3
|
+
/**
|
|
4
|
+
* What the middleware share. Every count here is read from the transcript rather than kept in
|
|
5
|
+
* memory, so it survives a restart, a resumed approval, and a second process, and two threads never
|
|
6
|
+
* share one.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The current turn: every message after the latest user message. One `invoke()` is one turn; a run
|
|
10
|
+
* that resumes after an approval is the same turn.
|
|
11
|
+
*/
|
|
12
|
+
export declare function turnOf(messages: readonly Message[]): readonly Message[];
|
|
13
|
+
/** Model calls made in the current turn: its assistant messages. */
|
|
14
|
+
export declare function modelCallsInTurn(messages: readonly Message[]): number;
|
|
15
|
+
/**
|
|
16
|
+
* Where a call sits among the current turn's calls to the same tool, from 1. Calls the model made in
|
|
17
|
+
* parallel are ordered as the model listed them, so the position never depends on which finished
|
|
18
|
+
* first.
|
|
19
|
+
*/
|
|
20
|
+
export declare function callPosition(messages: readonly Message[], call: Pick<AgentToolCall, 'id' | 'name'>): number;
|
|
21
|
+
/** A message's text: the string, or its text parts joined. */
|
|
22
|
+
export declare function textOf(content: Message['content']): string;
|
|
23
|
+
/** How long to wait between attempts. */
|
|
24
|
+
export interface RetryBackoff {
|
|
25
|
+
/** The wait before the second attempt, in milliseconds. Defaults to 500. */
|
|
26
|
+
initialMs?: number;
|
|
27
|
+
/** The longest wait, in milliseconds. Defaults to 10,000. */
|
|
28
|
+
maxMs?: number;
|
|
29
|
+
/** What each wait is multiplied by. Defaults to 2. */
|
|
30
|
+
factor?: number;
|
|
31
|
+
/** Spreads each wait between half and all of it, so runs that failed together retry apart. Defaults to true. */
|
|
32
|
+
jitter?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** The wait before attempt `attempt + 1`, after `attempt` failed. */
|
|
35
|
+
export declare function backoffDelay(attempt: number, backoff?: RetryBackoff): number;
|
|
36
|
+
/** Waits, or rejects with the signal's reason as soon as it aborts. */
|
|
37
|
+
export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
38
|
+
/** True for a cancellation: never worth retrying or falling back from. */
|
|
39
|
+
export declare function isCancellation(error: unknown, signal?: AbortSignal): boolean;
|
|
40
|
+
/** A short, stable hash of a text: cyrb53 as 8 hex digits. Not a security boundary. */
|
|
41
|
+
export declare function shortHash(text: string): string;
|