agentfootprint 2.11.0 → 2.11.1
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/README.md +2 -1
- package/dist/core/Agent.js +10 -105
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/agent/types.js +12 -0
- package/dist/core/agent/types.js.map +1 -0
- package/dist/core/agent/validators.js +131 -0
- package/dist/core/agent/validators.js.map +1 -0
- package/dist/esm/core/Agent.js +5 -100
- package/dist/esm/core/Agent.js.map +1 -1
- package/dist/esm/core/agent/types.js +11 -0
- package/dist/esm/core/agent/types.js.map +1 -0
- package/dist/esm/core/agent/validators.js +124 -0
- package/dist/esm/core/agent/validators.js.map +1 -0
- package/dist/esm/reliability/CircuitBreaker.js +156 -0
- package/dist/esm/reliability/CircuitBreaker.js.map +1 -0
- package/dist/esm/reliability/buildReliabilityGateChart.js +359 -0
- package/dist/esm/reliability/buildReliabilityGateChart.js.map +1 -0
- package/dist/esm/reliability/classifyError.js +56 -0
- package/dist/esm/reliability/classifyError.js.map +1 -0
- package/dist/esm/reliability/index.js +36 -0
- package/dist/esm/reliability/index.js.map +1 -0
- package/dist/esm/reliability/types.js +44 -0
- package/dist/esm/reliability/types.js.map +1 -0
- package/dist/reliability/CircuitBreaker.js +165 -0
- package/dist/reliability/CircuitBreaker.js.map +1 -0
- package/dist/reliability/buildReliabilityGateChart.js +363 -0
- package/dist/reliability/buildReliabilityGateChart.js.map +1 -0
- package/dist/reliability/classifyError.js +60 -0
- package/dist/reliability/classifyError.js.map +1 -0
- package/dist/reliability/index.js +42 -0
- package/dist/reliability/index.js.map +1 -0
- package/dist/reliability/types.js +48 -0
- package/dist/reliability/types.js.map +1 -0
- package/dist/types/core/Agent.d.ts +2 -67
- package/dist/types/core/Agent.d.ts.map +1 -1
- package/dist/types/core/agent/types.d.ts +154 -0
- package/dist/types/core/agent/types.d.ts.map +1 -0
- package/dist/types/core/agent/validators.d.ts +48 -0
- package/dist/types/core/agent/validators.d.ts.map +1 -0
- package/dist/types/reliability/CircuitBreaker.d.ts +76 -0
- package/dist/types/reliability/CircuitBreaker.d.ts.map +1 -0
- package/dist/types/reliability/buildReliabilityGateChart.d.ts +54 -0
- package/dist/types/reliability/buildReliabilityGateChart.d.ts.map +1 -0
- package/dist/types/reliability/classifyError.d.ts +29 -0
- package/dist/types/reliability/classifyError.d.ts.map +1 -0
- package/dist/types/reliability/index.d.ts +34 -0
- package/dist/types/reliability/index.d.ts.map +1 -0
- package/dist/types/reliability/types.d.ts +256 -0
- package/dist/types/reliability/types.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reliability — public surface for the v2.11.1 rules-based reliability
|
|
4
|
+
* subsystem. Internal-only helpers (CircuitBreaker class, classifyError,
|
|
5
|
+
* buildReliabilityGate) live in their own files; this barrel exports
|
|
6
|
+
* the consumer-facing types and the typed error.
|
|
7
|
+
*
|
|
8
|
+
* Consumer use:
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { Agent } from 'agentfootprint';
|
|
11
|
+
* import type { ReliabilityRule, ReliabilityScope } from 'agentfootprint/reliability';
|
|
12
|
+
* import { ReliabilityFailFastError } from 'agentfootprint/reliability';
|
|
13
|
+
*
|
|
14
|
+
* const agent = Agent.create({...}).reliability({
|
|
15
|
+
* postDecide: [
|
|
16
|
+
* { when: (s) => s.errorKind === '5xx-transient' && s.attempt < 3,
|
|
17
|
+
* then: 'retry', kind: 'transient-retry' },
|
|
18
|
+
* { when: (s) => s.error !== undefined,
|
|
19
|
+
* then: 'fail-fast', kind: 'unrecoverable' },
|
|
20
|
+
* ],
|
|
21
|
+
* }).build();
|
|
22
|
+
*
|
|
23
|
+
* try {
|
|
24
|
+
* await agent.run({ message: '...' });
|
|
25
|
+
* } catch (e) {
|
|
26
|
+
* if (e instanceof ReliabilityFailFastError) {
|
|
27
|
+
* console.log(e.kind, e.reason);
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.initialBreakerState = exports.CircuitOpenError = exports.ReliabilityFailFastError = void 0;
|
|
34
|
+
var types_js_1 = require("./types.js");
|
|
35
|
+
Object.defineProperty(exports, "ReliabilityFailFastError", { enumerable: true, get: function () { return types_js_1.ReliabilityFailFastError; } });
|
|
36
|
+
// CircuitBreaker pure-state-machine surface — exposed so consumers can
|
|
37
|
+
// hydrate breaker state from a persistence store (Redis/DynamoDB) or
|
|
38
|
+
// inspect projected state in their own observability adapters.
|
|
39
|
+
var CircuitBreaker_js_1 = require("./CircuitBreaker.js");
|
|
40
|
+
Object.defineProperty(exports, "CircuitOpenError", { enumerable: true, get: function () { return CircuitBreaker_js_1.CircuitOpenError; } });
|
|
41
|
+
Object.defineProperty(exports, "initialBreakerState", { enumerable: true, get: function () { return CircuitBreaker_js_1.initialBreakerState; } });
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/reliability/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;AAYH,uCAAsD;AAA7C,oHAAA,wBAAwB,OAAA;AAEjC,uEAAuE;AACvE,qEAAqE;AACrE,+DAA+D;AAC/D,yDAK6B;AAJ3B,qHAAA,gBAAgB,OAAA;AAChB,wHAAA,mBAAmB,OAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReliabilityFailFastError = void 0;
|
|
4
|
+
// ─── Public typed error ──────────────────────────────────────────────
|
|
5
|
+
/**
|
|
6
|
+
* Thrown by `Agent.run()` when a reliability rule routes to `'fail-fast'`
|
|
7
|
+
* and the gate $breaks with a reason. Carries:
|
|
8
|
+
*
|
|
9
|
+
* • `kind` — machine-readable identifier from the matched rule's
|
|
10
|
+
* `kind` field. Stable across versions; consumers
|
|
11
|
+
* branch on this.
|
|
12
|
+
* • `reason` — human-readable narrative string from `$break(reason)`.
|
|
13
|
+
* Format: `'reliability-{phase}: {label}'` (e.g.,
|
|
14
|
+
* `'reliability-post-decide: cost-cap-exceeded'`).
|
|
15
|
+
* • `cause` — the originating error from the LLM call, when one
|
|
16
|
+
* drove the fail-fast decision (e.g., the underlying
|
|
17
|
+
* HTTP error that tripped a circuit breaker).
|
|
18
|
+
* • `snapshot` — the full `executor.getSnapshot()` at fail-fast time
|
|
19
|
+
* for forensics. Consumers persist this for postmortem
|
|
20
|
+
* analysis (commitLog, narrative, scope state, etc.).
|
|
21
|
+
*
|
|
22
|
+
* Three-channel discipline: `kind`/`payload` came from scope state,
|
|
23
|
+
* `reason` came from $break, `snapshot` is the engine's own audit trail.
|
|
24
|
+
* Emit events flowed independently to any attached observability adapter
|
|
25
|
+
* (this error is the RUNTIME signal; emit is the OBSERVABILITY signal).
|
|
26
|
+
*/
|
|
27
|
+
class ReliabilityFailFastError extends Error {
|
|
28
|
+
code = 'ERR_RELIABILITY_FAIL_FAST';
|
|
29
|
+
kind;
|
|
30
|
+
reason;
|
|
31
|
+
cause;
|
|
32
|
+
snapshot;
|
|
33
|
+
payload;
|
|
34
|
+
constructor(opts) {
|
|
35
|
+
super(`[reliability] ${opts.kind}: ${opts.reason}`);
|
|
36
|
+
this.name = 'ReliabilityFailFastError';
|
|
37
|
+
this.kind = opts.kind;
|
|
38
|
+
this.reason = opts.reason;
|
|
39
|
+
if (opts.cause !== undefined)
|
|
40
|
+
this.cause = opts.cause;
|
|
41
|
+
if (opts.snapshot !== undefined)
|
|
42
|
+
this.snapshot = opts.snapshot;
|
|
43
|
+
if (opts.payload !== undefined)
|
|
44
|
+
this.payload = opts.payload;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ReliabilityFailFastError = ReliabilityFailFastError;
|
|
48
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/reliability/types.ts"],"names":[],"mappings":";;;AA2QA,wEAAwE;AAExE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,wBAAyB,SAAQ,KAAK;IACxC,IAAI,GAAG,2BAAoC,CAAC;IAC5C,IAAI,CAAS;IACb,MAAM,CAAS;IACf,KAAK,CAAS;IACd,QAAQ,CAAW;IACnB,OAAO,CAAmC;IAEnD,YAAY,IAMX;QACC,KAAK,CAAC,iBAAiB,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACpD,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS;YAAE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACtD,IAAI,IAAI,CAAC,QAAQ,KAAK,SAAS;YAAE,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC/D,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS;YAAE,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC9D,CAAC;CACF;AAvBD,4DAuBC"}
|
|
@@ -16,8 +16,6 @@
|
|
|
16
16
|
import { type CombinedNarrativeEntry, type FlowChart, type FlowchartCheckpoint, type RunOptions, type RuntimeSnapshot } from 'footprintjs';
|
|
17
17
|
import type { CachePolicy, CacheStrategy } from '../cache/types.js';
|
|
18
18
|
import { type RunnerPauseOutcome } from './pause.js';
|
|
19
|
-
import type { LLMProvider, PermissionChecker, PricingTable } from '../adapters/types.js';
|
|
20
|
-
import type { MemoryIdentity } from '../memory/identity/index.js';
|
|
21
19
|
import type { MemoryDefinition } from '../memory/define.types.js';
|
|
22
20
|
import type { Injection } from '../lib/injection-engine/types.js';
|
|
23
21
|
import { type OutputFallbackOptions, type ResolvedOutputFallback } from './outputFallback.js';
|
|
@@ -26,71 +24,8 @@ import { type OutputSchemaOptions, type OutputSchemaParser } from './outputSchem
|
|
|
26
24
|
import { RunnerBase } from './RunnerBase.js';
|
|
27
25
|
import type { Tool, ToolRegistryEntry } from './tools.js';
|
|
28
26
|
import type { ToolProvider } from '../tool-providers/types.js';
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/** Human-friendly name shown in events/metrics. Default: 'Agent'. */
|
|
32
|
-
readonly name?: string;
|
|
33
|
-
/** Stable id used for topology + events. Default: 'agent'. */
|
|
34
|
-
readonly id?: string;
|
|
35
|
-
readonly model: string;
|
|
36
|
-
readonly temperature?: number;
|
|
37
|
-
readonly maxTokens?: number;
|
|
38
|
-
/** Hard budget on ReAct iterations. Default: 10. Hard cap: 50. */
|
|
39
|
-
readonly maxIterations?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Pricing adapter. When set, Agent emits `agentfootprint.cost.tick`
|
|
42
|
-
* after every LLM response (once per ReAct iteration) with per-call
|
|
43
|
-
* and cumulative USD. Run-scoped — the cumulative resets each `.run()`.
|
|
44
|
-
*/
|
|
45
|
-
readonly pricingTable?: PricingTable;
|
|
46
|
-
/**
|
|
47
|
-
* Cumulative USD budget per run. With `pricingTable`, Agent emits a
|
|
48
|
-
* one-shot `agentfootprint.cost.limit_hit` (`action: 'warn'`) when
|
|
49
|
-
* cumulative USD crosses this budget. Execution continues — consumers
|
|
50
|
-
* choose whether to abort by listening to the event.
|
|
51
|
-
*/
|
|
52
|
-
readonly costBudget?: number;
|
|
53
|
-
/**
|
|
54
|
-
* Permission adapter. When set, the Agent calls
|
|
55
|
-
* `permissionChecker.check({capability: 'tool_call', ...})` BEFORE every
|
|
56
|
-
* `tool.execute()`. Emits `agentfootprint.permission.check` with the
|
|
57
|
-
* decision. On `deny`, the tool is skipped and its result is a
|
|
58
|
-
* synthetic denial string; on `allow` / `gate_open`, execution proceeds
|
|
59
|
-
* normally.
|
|
60
|
-
*/
|
|
61
|
-
readonly permissionChecker?: PermissionChecker;
|
|
62
|
-
/**
|
|
63
|
-
* Global cache kill switch (v2.6+). `'off'` disables the cache
|
|
64
|
-
* layer entirely — the CacheGate decider routes to `'no-markers'`
|
|
65
|
-
* every iteration regardless of other rules. Default: caching
|
|
66
|
-
* enabled (auto-resolved per provider via the strategy registry).
|
|
67
|
-
*
|
|
68
|
-
* Use `'off'` for low-frequency agents (cron jobs running once per
|
|
69
|
-
* hour) where the cache TTL guarantees zero cache hits and the
|
|
70
|
-
* cache-write penalty isn't worth paying.
|
|
71
|
-
*/
|
|
72
|
-
readonly caching?: 'off';
|
|
73
|
-
/**
|
|
74
|
-
* Optional explicit CacheStrategy override (v2.6+). Defaults to
|
|
75
|
-
* `getDefaultCacheStrategy(provider.name)` — so Anthropic/OpenAI/
|
|
76
|
-
* Bedrock/Mock providers auto-resolve to their respective strategies
|
|
77
|
-
* once those land in Phase 7+.
|
|
78
|
-
*/
|
|
79
|
-
readonly cacheStrategy?: CacheStrategy;
|
|
80
|
-
}
|
|
81
|
-
export interface AgentInput {
|
|
82
|
-
readonly message: string;
|
|
83
|
-
/**
|
|
84
|
-
* Multi-tenant memory scope. Populated to `scope.identity` so memory
|
|
85
|
-
* subflows registered via `.memory()` can isolate reads/writes per
|
|
86
|
-
* tenant + principal + conversation.
|
|
87
|
-
*
|
|
88
|
-
* Defaults to `{ conversationId: '<runId>' }` when omitted, so agents
|
|
89
|
-
* without memory work unchanged.
|
|
90
|
-
*/
|
|
91
|
-
readonly identity?: MemoryIdentity;
|
|
92
|
-
}
|
|
93
|
-
export type AgentOutput = string;
|
|
27
|
+
import type { AgentInput, AgentOptions, AgentOutput } from './agent/types.js';
|
|
28
|
+
export type { AgentInput, AgentOptions, AgentOutput };
|
|
94
29
|
export declare class Agent extends RunnerBase<AgentInput, AgentOutput> {
|
|
95
30
|
readonly name: string;
|
|
96
31
|
readonly id: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../../src/core/Agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,SAAS,EACd,KAAK,mBAAmB,EAExB,KAAK,UAAU,EACf,KAAK,eAAe,EAErB,MAAM,aAAa,CAAC;AAQrB,OAAO,KAAK,EAAe,WAAW,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAOjF,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../../src/core/Agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAGL,KAAK,sBAAsB,EAC3B,KAAK,SAAS,EACd,KAAK,mBAAmB,EAExB,KAAK,UAAU,EACf,KAAK,eAAe,EAErB,MAAM,aAAa,CAAC;AAQrB,OAAO,KAAK,EAAe,WAAW,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAOjF,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAyBrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AASlE,OAAO,KAAK,EAAmB,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAEnF,OAAO,EAIL,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAC5B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAKL,KAAK,kBAAkB,EAExB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAIL,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAa,MAAM,iBAAiB,CAAC;AACxD,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,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,IAAI;IAqBhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,IAAI;IAwB1D,KAAK,IAAI,KAAK;CAmCf"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent type definitions — both PUBLIC types (AgentOptions, AgentInput,
|
|
3
|
+
* AgentOutput) consumed by `Agent.create({...}).run({...})` callers AND
|
|
4
|
+
* the INTERNAL `AgentState` shape used by stage functions.
|
|
5
|
+
*
|
|
6
|
+
* These were originally inline in `core/Agent.ts`; extracted here as
|
|
7
|
+
* part of the v2.11.1 decomposition. `core/Agent.ts` re-exports them
|
|
8
|
+
* for back-compat (the 28+ existing import sites continue to work).
|
|
9
|
+
*/
|
|
10
|
+
import type { LLMMessage, LLMProvider, LLMToolSchema, PermissionChecker, PricingTable } from '../../adapters/types.js';
|
|
11
|
+
import type { CacheMarker, CacheStrategy } from '../../cache/types.js';
|
|
12
|
+
import type { ActiveInjection } from '../../lib/injection-engine/types.js';
|
|
13
|
+
import type { InjectionRecord } from '../../recorders/core/types.js';
|
|
14
|
+
import type { MemoryIdentity } from '../../memory/identity/types.js';
|
|
15
|
+
export interface AgentOptions {
|
|
16
|
+
readonly provider: LLMProvider;
|
|
17
|
+
/** Human-friendly name shown in events/metrics. Default: 'Agent'. */
|
|
18
|
+
readonly name?: string;
|
|
19
|
+
/** Stable id used for topology + events. Default: 'agent'. */
|
|
20
|
+
readonly id?: string;
|
|
21
|
+
readonly model: string;
|
|
22
|
+
readonly temperature?: number;
|
|
23
|
+
readonly maxTokens?: number;
|
|
24
|
+
/** Hard budget on ReAct iterations. Default: 10. Hard cap: 50. */
|
|
25
|
+
readonly maxIterations?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Pricing adapter. When set, Agent emits `agentfootprint.cost.tick`
|
|
28
|
+
* after every LLM response (once per ReAct iteration) with per-call
|
|
29
|
+
* and cumulative USD. Run-scoped — the cumulative resets each `.run()`.
|
|
30
|
+
*/
|
|
31
|
+
readonly pricingTable?: PricingTable;
|
|
32
|
+
/**
|
|
33
|
+
* Cumulative USD budget per run. With `pricingTable`, Agent emits a
|
|
34
|
+
* one-shot `agentfootprint.cost.limit_hit` (`action: 'warn'`) when
|
|
35
|
+
* cumulative USD crosses this budget. Execution continues — consumers
|
|
36
|
+
* choose whether to abort by listening to the event.
|
|
37
|
+
*/
|
|
38
|
+
readonly costBudget?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Permission adapter. When set, the Agent calls
|
|
41
|
+
* `permissionChecker.check({capability: 'tool_call', ...})` BEFORE every
|
|
42
|
+
* `tool.execute()`. Emits `agentfootprint.permission.check` with the
|
|
43
|
+
* decision. On `deny`, the tool is skipped and its result is a
|
|
44
|
+
* synthetic denial string; on `allow` / `gate_open`, execution proceeds
|
|
45
|
+
* normally.
|
|
46
|
+
*/
|
|
47
|
+
readonly permissionChecker?: PermissionChecker;
|
|
48
|
+
/**
|
|
49
|
+
* Global cache kill switch (v2.6+). `'off'` disables the cache
|
|
50
|
+
* layer entirely — the CacheGate decider routes to `'no-markers'`
|
|
51
|
+
* every iteration regardless of other rules. Default: caching
|
|
52
|
+
* enabled (auto-resolved per provider via the strategy registry).
|
|
53
|
+
*
|
|
54
|
+
* Use `'off'` for low-frequency agents (cron jobs running once per
|
|
55
|
+
* hour) where the cache TTL guarantees zero cache hits and the
|
|
56
|
+
* cache-write penalty isn't worth paying.
|
|
57
|
+
*/
|
|
58
|
+
readonly caching?: 'off';
|
|
59
|
+
/**
|
|
60
|
+
* Optional explicit CacheStrategy override (v2.6+). Defaults to
|
|
61
|
+
* `getDefaultCacheStrategy(provider.name)` — so Anthropic/OpenAI/
|
|
62
|
+
* Bedrock/Mock providers auto-resolve to their respective strategies
|
|
63
|
+
* once those land in Phase 7+.
|
|
64
|
+
*/
|
|
65
|
+
readonly cacheStrategy?: CacheStrategy;
|
|
66
|
+
}
|
|
67
|
+
export interface AgentInput {
|
|
68
|
+
readonly message: string;
|
|
69
|
+
/**
|
|
70
|
+
* Multi-tenant memory scope. Populated to `scope.identity` so memory
|
|
71
|
+
* subflows registered via `.memory()` can isolate reads/writes per
|
|
72
|
+
* tenant + principal + conversation.
|
|
73
|
+
*
|
|
74
|
+
* Defaults to `{ conversationId: '<runId>' }` when omitted, so agents
|
|
75
|
+
* without memory work unchanged.
|
|
76
|
+
*/
|
|
77
|
+
readonly identity?: MemoryIdentity;
|
|
78
|
+
}
|
|
79
|
+
export type AgentOutput = string;
|
|
80
|
+
/**
|
|
81
|
+
* Internal scope state for the Agent's flowchart. Recorders never read
|
|
82
|
+
* this directly — they read the InjectionRecord convention keys + emit
|
|
83
|
+
* events. Each stage function under `./stages/` receives a TypedScope
|
|
84
|
+
* over this shape and reads/writes via typed properties.
|
|
85
|
+
*
|
|
86
|
+
* Mutability conventions (followed by every Agent stage):
|
|
87
|
+
* • Per-iteration scalars (iteration, finalContent, llmLatestContent,
|
|
88
|
+
* etc.) are OVERWRITTEN each pass; commitLog preserves history.
|
|
89
|
+
* • Cumulative scalars (cumTokensInput, totalInputTokens, turnNumber)
|
|
90
|
+
* accumulate monotonically across the run.
|
|
91
|
+
* • Arrays from slot subflows (systemPromptInjections,
|
|
92
|
+
* messagesInjections, toolsInjections, dynamicToolSchemas,
|
|
93
|
+
* cacheMarkers) use `arrayMerge: ArrayMergeMode.Replace` semantics —
|
|
94
|
+
* each iteration's value REPLACES the prior iteration's, not
|
|
95
|
+
* appends.
|
|
96
|
+
*/
|
|
97
|
+
export interface AgentState {
|
|
98
|
+
userMessage: string;
|
|
99
|
+
history: readonly LLMMessage[];
|
|
100
|
+
iteration: number;
|
|
101
|
+
maxIterations: number;
|
|
102
|
+
finalContent: string;
|
|
103
|
+
totalInputTokens: number;
|
|
104
|
+
totalOutputTokens: number;
|
|
105
|
+
turnStartMs: number;
|
|
106
|
+
runIdentity: MemoryIdentity;
|
|
107
|
+
newMessages: readonly LLMMessage[];
|
|
108
|
+
turnNumber: number;
|
|
109
|
+
contextTokensRemaining: number;
|
|
110
|
+
systemPromptInjections: readonly InjectionRecord[];
|
|
111
|
+
messagesInjections: readonly InjectionRecord[];
|
|
112
|
+
toolsInjections: readonly InjectionRecord[];
|
|
113
|
+
llmLatestContent: string;
|
|
114
|
+
llmLatestToolCalls: readonly {
|
|
115
|
+
readonly id: string;
|
|
116
|
+
readonly name: string;
|
|
117
|
+
readonly args: Readonly<Record<string, unknown>>;
|
|
118
|
+
}[];
|
|
119
|
+
pausedToolCallId: string;
|
|
120
|
+
pausedToolName: string;
|
|
121
|
+
pausedToolStartMs: number;
|
|
122
|
+
cumTokensInput: number;
|
|
123
|
+
cumTokensOutput: number;
|
|
124
|
+
cumEstimatedUsd: number;
|
|
125
|
+
costBudgetHit: boolean;
|
|
126
|
+
/** Active set output by InjectionEngine subflow each iteration —
|
|
127
|
+
* POJO projections (no functions) suitable for scope round-trip. */
|
|
128
|
+
activeInjections: readonly ActiveInjection[];
|
|
129
|
+
/** IDs of LLM-activated Skills the LLM has activated this turn
|
|
130
|
+
* (via the `read_skill` tool). InjectionEngine matches by id. */
|
|
131
|
+
activatedInjectionIds: readonly string[];
|
|
132
|
+
/** Most recent tool result — drives `on-tool-return` triggers. */
|
|
133
|
+
lastToolResult?: {
|
|
134
|
+
toolName: string;
|
|
135
|
+
result: string;
|
|
136
|
+
};
|
|
137
|
+
/** Tool schemas resolved by the tools slot subflow each iteration
|
|
138
|
+
* (registry + injection-supplied). Used by callLLM. */
|
|
139
|
+
dynamicToolSchemas: readonly LLMToolSchema[];
|
|
140
|
+
/** Provider-agnostic cache markers emitted by CacheDecision subflow.
|
|
141
|
+
* Cleared each iteration by the SkipCaching branch when the
|
|
142
|
+
* CacheGate decides to skip (kill switch / hit-rate / churn). */
|
|
143
|
+
cacheMarkers: readonly CacheMarker[];
|
|
144
|
+
/** Global cache kill switch from `Agent.create({ caching: 'off' })`. */
|
|
145
|
+
cachingDisabled: boolean;
|
|
146
|
+
/** Running cache hit rate from recent iterations (0..1). Computed
|
|
147
|
+
* by cacheRecorder (Phase 9); `undefined` until first metrics. */
|
|
148
|
+
recentHitRate: number | undefined;
|
|
149
|
+
/** Rolling window of active-skill IDs across recent iterations.
|
|
150
|
+
* Maintained by the UpdateSkillHistory function stage; consumed
|
|
151
|
+
* by CacheGate's skill-churn rule. */
|
|
152
|
+
skillHistory: readonly (string | undefined)[];
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/core/agent/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,aAAa,EACb,iBAAiB,EACjB,YAAY,EACb,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACvE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAIrE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC;IAC/B,qEAAqE;IACrE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,kEAAk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,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;KAClD,EAAE,CAAC;IAEJ,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAE1B,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,OAAO,CAAC;IAEvB;yEACqE;IACrE,gBAAgB,EAAE,SAAS,eAAe,EAAE,CAAC;IAC7C;sEACkE;IAClE,qBAAqB,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,kEAAkE;IAClE,cAAc,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD;4DACwD;IACxD,kBAAkB,EAAE,SAAS,aAAa,EAAE,CAAC;IAE7C;;sEAEkE;IAClE,YAAY,EAAE,SAAS,WAAW,EAAE,CAAC;IACrC,wEAAwE;IACxE,eAAe,EAAE,OAAO,CAAC;IACzB;uEACmE;IACnE,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC;;2CAEuC;IACvC,YAAY,EAAE,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,CAAC;CAC/C"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent validators — pure helper functions extracted from Agent.ts.
|
|
3
|
+
*
|
|
4
|
+
* These run at Agent construction time (eagerly, so misconfiguration
|
|
5
|
+
* fails fast at `.build()`) and during stage execution (safeStringify
|
|
6
|
+
* for tool-result formatting).
|
|
7
|
+
*
|
|
8
|
+
* Pure functions, no class state — extracted for readability and
|
|
9
|
+
* isolated testability. The Agent class imports + invokes these in
|
|
10
|
+
* its constructor and stage handlers.
|
|
11
|
+
*/
|
|
12
|
+
import type { MemoryDefinition } from '../../memory/define.types.js';
|
|
13
|
+
import type { Injection } from '../../lib/injection-engine/types.js';
|
|
14
|
+
import type { ToolRegistryEntry } from '../tools.js';
|
|
15
|
+
/**
|
|
16
|
+
* Validate that every memory definition has a unique id. Each memory
|
|
17
|
+
* writes to its own scope key (`memoryInjection_${id}`); duplicates
|
|
18
|
+
* silently overwrite, leading to data loss that's hard to debug.
|
|
19
|
+
*
|
|
20
|
+
* Throws on collision so `Agent.build()` fails fast at construction.
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateMemoryIdUniqueness(memories: readonly MemoryDefinition[]): void;
|
|
23
|
+
/**
|
|
24
|
+
* Clamp `maxIterations` to a safe range. The lower bound (1) prevents
|
|
25
|
+
* a 0-iteration agent (no LLM calls = no work); the upper bound (50)
|
|
26
|
+
* prevents runaway loops in misconfigured agents.
|
|
27
|
+
*/
|
|
28
|
+
export declare function clampIterations(n: number): number;
|
|
29
|
+
/**
|
|
30
|
+
* Validate tool-name uniqueness across `.tool()`-registered tools +
|
|
31
|
+
* every Skill's `inject.tools[]`. The LLM dispatches by `tool.schema.name`
|
|
32
|
+
* (the wire format), so any collision silently shadows execution.
|
|
33
|
+
*
|
|
34
|
+
* Called eagerly in the Agent constructor so `Agent.build()` throws
|
|
35
|
+
* immediately, not on first `run()`.
|
|
36
|
+
*
|
|
37
|
+
* `read_skill` is reserved when ≥1 Skill is registered — collisions
|
|
38
|
+
* with consumer tools throw.
|
|
39
|
+
*/
|
|
40
|
+
export declare function validateToolNameUniqueness(registry: readonly ToolRegistryEntry[], injections: readonly Injection[]): void;
|
|
41
|
+
/**
|
|
42
|
+
* JSON.stringify with circular-ref protection. Tool results are untrusted —
|
|
43
|
+
* a hostile/buggy tool returning a cyclic object must not crash the run.
|
|
44
|
+
* Falls back to '[unstringifiable: <reason>]' so the LLM still sees that
|
|
45
|
+
* the tool ran and produced something unusable.
|
|
46
|
+
*/
|
|
47
|
+
export declare function safeStringify(value: unknown): string;
|
|
48
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../../../src/core/agent/validators.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qCAAqC,CAAC;AACrE,OAAO,KAAK,EAAQ,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAE3D;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAWtF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,QAAQ,EAAE,SAAS,iBAAiB,EAAE,EACtC,UAAU,EAAE,SAAS,SAAS,EAAE,GAC/B,IAAI,CAoEN;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOpD"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CircuitBreaker — pure state-machine functions for the Nygard breaker
|
|
3
|
+
* pattern.
|
|
4
|
+
*
|
|
5
|
+
* Refactored from a class-with-instance-state to PURE FUNCTIONS that
|
|
6
|
+
* take a state record and return a new one. Reasons:
|
|
7
|
+
*
|
|
8
|
+
* 1. **No hidden runtime state.** Breaker state lives in scope where
|
|
9
|
+
* it's visible to commitLog, narrative, and rules — the footprintjs
|
|
10
|
+
* "everything in scope" principle. The closure used to be the
|
|
11
|
+
* source of truth and scope held only a projection; now scope IS
|
|
12
|
+
* the source of truth.
|
|
13
|
+
*
|
|
14
|
+
* 2. **Round-trippable across gate invocations.** Because state is a
|
|
15
|
+
* plain record, gate's outputMapper writes it back to agent scope;
|
|
16
|
+
* agent scope persists across the ReAct loop's many LLM-call gate
|
|
17
|
+
* invocations; gate's inputMapper reads it back in for the next
|
|
18
|
+
* call. Per-process persistence comes from the agent scope, not
|
|
19
|
+
* from a closure that hides between runs.
|
|
20
|
+
*
|
|
21
|
+
* 3. **Distributable later.** A future v2.12 `BreakerStateStore`
|
|
22
|
+
* adapter (Redis/DynamoDB) just needs to serialize/deserialize the
|
|
23
|
+
* state record. No class instances to reconstruct.
|
|
24
|
+
*
|
|
25
|
+
* 4. **Testable in isolation.** Pure functions; no instance setup.
|
|
26
|
+
*
|
|
27
|
+
* Pattern: Nygard *Release It!* — three states (CLOSED → OPEN →
|
|
28
|
+
* HALF-OPEN) with cooldown and probe-success thresholds.
|
|
29
|
+
*/
|
|
30
|
+
import type { CircuitBreakerConfig } from './types.js';
|
|
31
|
+
export type CircuitState = 'closed' | 'open' | 'half-open';
|
|
32
|
+
/** Plain serializable record holding one breaker's full state. */
|
|
33
|
+
export interface BreakerState {
|
|
34
|
+
state: CircuitState;
|
|
35
|
+
consecutiveFailures: number;
|
|
36
|
+
consecutiveSuccesses: number;
|
|
37
|
+
openedAt: number;
|
|
38
|
+
lastErrorMessage?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Thrown by `assertAdmit()` when the breaker is OPEN and the cooldown
|
|
42
|
+
* window has not elapsed. The reliability gate stage catches this,
|
|
43
|
+
* classifies via `classifyError` → `'circuit-open'`, and lets the
|
|
44
|
+
* post-decide rules route on it.
|
|
45
|
+
*/
|
|
46
|
+
export declare class CircuitOpenError extends Error {
|
|
47
|
+
readonly code: "ERR_CIRCUIT_OPEN";
|
|
48
|
+
readonly cause: unknown;
|
|
49
|
+
readonly retryAfter: number;
|
|
50
|
+
constructor(providerName: string, lastErrorMessage: string | undefined, retryAfter: number);
|
|
51
|
+
}
|
|
52
|
+
/** Initial state for a freshly-CLOSED breaker. */
|
|
53
|
+
export declare function initialBreakerState(): BreakerState;
|
|
54
|
+
/**
|
|
55
|
+
* Decide whether to admit a call. Returns the (possibly-updated) state
|
|
56
|
+
* AND whether to admit. If OPEN and cooldown elapsed, transitions to
|
|
57
|
+
* HALF-OPEN and admits. Pure: caller must use the returned state.
|
|
58
|
+
*
|
|
59
|
+
* Usage in the gate stage:
|
|
60
|
+
* ```ts
|
|
61
|
+
* const { admitted, nextState } = admitCall(scope.breakerStates[name], config);
|
|
62
|
+
* scope.breakerStates[name] = nextState;
|
|
63
|
+
* if (!admitted) throw new CircuitOpenError(name, nextState.lastErrorMessage, ...);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function admitCall(state: BreakerState, config: CircuitBreakerConfig | undefined): {
|
|
67
|
+
admitted: boolean;
|
|
68
|
+
nextState: BreakerState;
|
|
69
|
+
};
|
|
70
|
+
/** Record a successful call. Returns the (possibly-updated) state. */
|
|
71
|
+
export declare function recordSuccess(state: BreakerState, config: CircuitBreakerConfig | undefined): BreakerState;
|
|
72
|
+
/** Record a failed call. Returns the (possibly-updated) state. */
|
|
73
|
+
export declare function recordFailure(state: BreakerState, err: unknown, config: CircuitBreakerConfig | undefined): BreakerState;
|
|
74
|
+
/** Compute the next probe time given a state + config. */
|
|
75
|
+
export declare function nextProbeTime(state: BreakerState, config: CircuitBreakerConfig | undefined): number;
|
|
76
|
+
//# sourceMappingURL=CircuitBreaker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CircuitBreaker.d.ts","sourceRoot":"","sources":["../../../src/reliability/CircuitBreaker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE3D,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,YAAY,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED;;;;;GAKG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,IAAI,qBAA+B;IAC5C,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;gBAChB,YAAY,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,GAAG,SAAS,EAAE,UAAU,EAAE,MAAM;CAU3F;AAkBD,kDAAkD;AAClD,wBAAgB,mBAAmB,IAAI,YAAY,CAOlD;AAID;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,oBAAoB,GAAG,SAAS,GACvC;IAAE,QAAQ,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,YAAY,CAAA;CAAE,CAchD;AAED,sEAAsE;AACtE,wBAAgB,aAAa,CAC3B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,oBAAoB,GAAG,SAAS,GACvC,YAAY,CAsBd;AAED,kEAAkE;AAClE,wBAAgB,aAAa,CAC3B,KAAK,EAAE,YAAY,EACnB,GAAG,EAAE,OAAO,EACZ,MAAM,EAAE,oBAAoB,GAAG,SAAS,GACvC,YAAY,CAgCd;AAED,0DAA0D;AAC1D,wBAAgB,aAAa,CAC3B,KAAK,EAAE,YAAY,EACnB,MAAM,EAAE,oBAAoB,GAAG,SAAS,GACvC,MAAM,CAER"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* buildReliabilityGateChart — produces a footprintjs FlowChart that wraps
|
|
3
|
+
* an LLM call with rules-based reliability semantics, using the native
|
|
4
|
+
* `decide()` DSL via `addDeciderFunction` decider stages.
|
|
5
|
+
*
|
|
6
|
+
* The returned chart is mounted as a subflow in the agent's chart at
|
|
7
|
+
* Agent.build() time (only when reliability is configured). Inside the
|
|
8
|
+
* subflow:
|
|
9
|
+
*
|
|
10
|
+
* PreCheck (decider) → CallProvider (function) → PostDecide (decider)
|
|
11
|
+
* │
|
|
12
|
+
* ┌───────────────┘
|
|
13
|
+
* ▼ loopTo('pre-check')
|
|
14
|
+
*
|
|
15
|
+
* Branch outcomes (escape via $break() to stop the gate's loop;
|
|
16
|
+
* fall-through via no-$break to trigger loopTo back to PreCheck):
|
|
17
|
+
*
|
|
18
|
+
* PreCheck:
|
|
19
|
+
* 'continue' → no-op → falls through to CallProvider
|
|
20
|
+
* 'fail-fast' → set failKind, $emit, $break(reason)
|
|
21
|
+
*
|
|
22
|
+
* PostDecide:
|
|
23
|
+
* 'ok' → $break() (subflow exits normally; agent continues)
|
|
24
|
+
* 'retry' → bump attempt; falls through to loopTo
|
|
25
|
+
* 'retry-other' → bump providerIdx; falls through to loopTo
|
|
26
|
+
* 'fallback' → call config.fallback(); $break() on success
|
|
27
|
+
* 'fail-fast' → set failKind, $emit, $break(reason)
|
|
28
|
+
*
|
|
29
|
+
* The subflow is mounted WITHOUT `propagateBreak: true`. Subflow $break is
|
|
30
|
+
* local — agent.ts adds a `TranslateFailFast` agent-level stage AFTER the
|
|
31
|
+
* subflow that reads scope.reliabilityFailKind and converts it into an
|
|
32
|
+
* agent-level `$break(reason)`. This split lets normal subflow exits
|
|
33
|
+
* (`ok`/`fallback`) leave the agent running while fail-fast stops it.
|
|
34
|
+
*
|
|
35
|
+
* Three-channel discipline preserved:
|
|
36
|
+
* • SCOPE STATE — failKind/failPayload/failReason mapped to parent via
|
|
37
|
+
* outputMapper; consumed by agent's TranslateFailFast.
|
|
38
|
+
* • $emit — passive observability for external consumers.
|
|
39
|
+
* • $break(reason)— control flow + human reason for narrative.
|
|
40
|
+
*/
|
|
41
|
+
import type { FlowChart } from 'footprintjs';
|
|
42
|
+
import type { ReliabilityConfig } from './types.js';
|
|
43
|
+
/**
|
|
44
|
+
* Build the reliability gate FlowChart from a config. Mount via
|
|
45
|
+
* `addSubFlowChartNext` in the agent's chart — see `Agent.build()`.
|
|
46
|
+
*
|
|
47
|
+
* Closure state captured by stage functions:
|
|
48
|
+
* • `breakers` — Map<providerName, CircuitBreaker>; per-instance state
|
|
49
|
+
* persists across gate invocations within ONE agent process.
|
|
50
|
+
* • `preRules` / `postRules` — frozen rule arrays.
|
|
51
|
+
* • `fallbackFn` — consumer's fallback function, if configured.
|
|
52
|
+
*/
|
|
53
|
+
export declare function buildReliabilityGateChart(config: ReliabilityConfig): FlowChart;
|
|
54
|
+
//# sourceMappingURL=buildReliabilityGateChart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildReliabilityGateChart.d.ts","sourceRoot":"","sources":["../../../src/reliability/buildReliabilityGateChart.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AAWzD,OAAO,KAAK,EAAE,iBAAiB,EAAqC,MAAM,YAAY,CAAC;AAkFvF;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,iBAAiB,GAAG,SAAS,CA4S9E"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* classifyError — pure function mapping a thrown error to one of the
|
|
3
|
+
* coarse `ReliabilityScope.errorKind` categories used by reliability rules.
|
|
4
|
+
*
|
|
5
|
+
* Centralized so rules read structured `errorKind` instead of doing
|
|
6
|
+
* regex on `error.message` themselves. Add new categories ONLY when a
|
|
7
|
+
* new rule needs to discriminate them — keep the taxonomy small.
|
|
8
|
+
*
|
|
9
|
+
* Categories:
|
|
10
|
+
* • 'ok' — no error (caller should pass `undefined` or omit)
|
|
11
|
+
* • 'circuit-open' — `CircuitOpenError` from the breaker layer
|
|
12
|
+
* • 'rate-limit' — HTTP 429 or vendor rate-limit signal
|
|
13
|
+
* • '5xx-transient' — HTTP 5xx, ETIMEDOUT, ECONNRESET, ECONNREFUSED
|
|
14
|
+
* • 'schema-fail' — `OutputSchemaError` from the schema validator
|
|
15
|
+
* • 'unknown' — anything else (default; still routable but
|
|
16
|
+
* consumers usually `'fail-fast'` on this)
|
|
17
|
+
*/
|
|
18
|
+
import type { ReliabilityScope } from './types.js';
|
|
19
|
+
type ErrorKind = ReliabilityScope['errorKind'];
|
|
20
|
+
/**
|
|
21
|
+
* Classify an error into a `ReliabilityScope['errorKind']` category.
|
|
22
|
+
*
|
|
23
|
+
* @param err - The thrown value. May be an Error, a vendor SDK error
|
|
24
|
+
* shape with `.status`/`.code`, or anything else (`unknown` defaults).
|
|
25
|
+
* @returns the matching coarse category string.
|
|
26
|
+
*/
|
|
27
|
+
export declare function classifyError(err: unknown): ErrorKind;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=classifyError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classifyError.d.ts","sourceRoot":"","sources":["../../../src/reliability/classifyError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,KAAK,SAAS,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;AAU/C;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,CA8BrD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reliability — public surface for the v2.11.1 rules-based reliability
|
|
3
|
+
* subsystem. Internal-only helpers (CircuitBreaker class, classifyError,
|
|
4
|
+
* buildReliabilityGate) live in their own files; this barrel exports
|
|
5
|
+
* the consumer-facing types and the typed error.
|
|
6
|
+
*
|
|
7
|
+
* Consumer use:
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { Agent } from 'agentfootprint';
|
|
10
|
+
* import type { ReliabilityRule, ReliabilityScope } from 'agentfootprint/reliability';
|
|
11
|
+
* import { ReliabilityFailFastError } from 'agentfootprint/reliability';
|
|
12
|
+
*
|
|
13
|
+
* const agent = Agent.create({...}).reliability({
|
|
14
|
+
* postDecide: [
|
|
15
|
+
* { when: (s) => s.errorKind === '5xx-transient' && s.attempt < 3,
|
|
16
|
+
* then: 'retry', kind: 'transient-retry' },
|
|
17
|
+
* { when: (s) => s.error !== undefined,
|
|
18
|
+
* then: 'fail-fast', kind: 'unrecoverable' },
|
|
19
|
+
* ],
|
|
20
|
+
* }).build();
|
|
21
|
+
*
|
|
22
|
+
* try {
|
|
23
|
+
* await agent.run({ message: '...' });
|
|
24
|
+
* } catch (e) {
|
|
25
|
+
* if (e instanceof ReliabilityFailFastError) {
|
|
26
|
+
* console.log(e.kind, e.reason);
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type { CircuitBreakerConfig, ReliabilityConfig, ReliabilityDecision, ReliabilityFallbackFn, ReliabilityProvider, ReliabilityRule, ReliabilityScope, } from './types.js';
|
|
32
|
+
export { ReliabilityFailFastError } from './types.js';
|
|
33
|
+
export { CircuitOpenError, initialBreakerState, type BreakerState, type CircuitState, } from './CircuitBreaker.js';
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/reliability/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,YAAY,EACV,oBAAoB,EACpB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,gBAAgB,GACjB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAKtD,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,YAAY,GAClB,MAAM,qBAAqB,CAAC"}
|