agentfootprint 2.11.0 → 2.11.2
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 +89 -1341
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/agent/AgentBuilder.js +489 -0
- package/dist/core/agent/AgentBuilder.js.map +1 -0
- package/dist/core/agent/buildAgentChart.js +227 -0
- package/dist/core/agent/buildAgentChart.js.map +1 -0
- package/dist/core/agent/buildToolRegistry.js +115 -0
- package/dist/core/agent/buildToolRegistry.js.map +1 -0
- package/dist/core/agent/stages/breakFinal.js +28 -0
- package/dist/core/agent/stages/breakFinal.js.map +1 -0
- package/dist/core/agent/stages/callLLM.js +129 -0
- package/dist/core/agent/stages/callLLM.js.map +1 -0
- package/dist/core/agent/stages/iterationStart.js +24 -0
- package/dist/core/agent/stages/iterationStart.js.map +1 -0
- package/dist/core/agent/stages/prepareFinal.js +45 -0
- package/dist/core/agent/stages/prepareFinal.js.map +1 -0
- package/dist/core/agent/stages/route.js +36 -0
- package/dist/core/agent/stages/route.js.map +1 -0
- package/dist/core/agent/stages/seed.js +95 -0
- package/dist/core/agent/stages/seed.js.map +1 -0
- package/dist/core/agent/stages/toolCalls.js +250 -0
- package/dist/core/agent/stages/toolCalls.js.map +1 -0
- 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 +87 -1338
- package/dist/esm/core/Agent.js.map +1 -1
- package/dist/esm/core/agent/AgentBuilder.js +485 -0
- package/dist/esm/core/agent/AgentBuilder.js.map +1 -0
- package/dist/esm/core/agent/buildAgentChart.js +223 -0
- package/dist/esm/core/agent/buildAgentChart.js.map +1 -0
- package/dist/esm/core/agent/buildToolRegistry.js +111 -0
- package/dist/esm/core/agent/buildToolRegistry.js.map +1 -0
- package/dist/esm/core/agent/stages/breakFinal.js +24 -0
- package/dist/esm/core/agent/stages/breakFinal.js.map +1 -0
- package/dist/esm/core/agent/stages/callLLM.js +125 -0
- package/dist/esm/core/agent/stages/callLLM.js.map +1 -0
- package/dist/esm/core/agent/stages/iterationStart.js +20 -0
- package/dist/esm/core/agent/stages/iterationStart.js.map +1 -0
- package/dist/esm/core/agent/stages/prepareFinal.js +41 -0
- package/dist/esm/core/agent/stages/prepareFinal.js.map +1 -0
- package/dist/esm/core/agent/stages/route.js +32 -0
- package/dist/esm/core/agent/stages/route.js.map +1 -0
- package/dist/esm/core/agent/stages/seed.js +91 -0
- package/dist/esm/core/agent/stages/seed.js.map +1 -0
- package/dist/esm/core/agent/stages/toolCalls.js +246 -0
- package/dist/esm/core/agent/stages/toolCalls.js.map +1 -0
- 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 +7 -400
- package/dist/types/core/Agent.d.ts.map +1 -1
- package/dist/types/core/agent/AgentBuilder.d.ts +348 -0
- package/dist/types/core/agent/AgentBuilder.d.ts.map +1 -0
- package/dist/types/core/agent/buildAgentChart.d.ts +74 -0
- package/dist/types/core/agent/buildAgentChart.d.ts.map +1 -0
- package/dist/types/core/agent/buildToolRegistry.d.ts +62 -0
- package/dist/types/core/agent/buildToolRegistry.d.ts.map +1 -0
- package/dist/types/core/agent/stages/breakFinal.d.ts +23 -0
- package/dist/types/core/agent/stages/breakFinal.d.ts.map +1 -0
- package/dist/types/core/agent/stages/callLLM.d.ts +54 -0
- package/dist/types/core/agent/stages/callLLM.d.ts.map +1 -0
- package/dist/types/core/agent/stages/iterationStart.d.ts +16 -0
- package/dist/types/core/agent/stages/iterationStart.d.ts.map +1 -0
- package/dist/types/core/agent/stages/prepareFinal.d.ts +20 -0
- package/dist/types/core/agent/stages/prepareFinal.d.ts.map +1 -0
- package/dist/types/core/agent/stages/route.d.ts +19 -0
- package/dist/types/core/agent/stages/route.d.ts.map +1 -0
- package/dist/types/core/agent/stages/seed.d.ts +54 -0
- package/dist/types/core/agent/stages/seed.d.ts.map +1 -0
- package/dist/types/core/agent/stages/toolCalls.d.ts +50 -0
- package/dist/types/core/agent/stages/toolCalls.d.ts.map +1 -0
- 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,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* seed — initial stage of the agent's chart. Initializes every mutable
|
|
3
|
+
* field of `AgentState` from the consumer's input.
|
|
4
|
+
*
|
|
5
|
+
* Runs once per `agent.run({ input })`. The chart is built once at
|
|
6
|
+
* Agent construction, so seed has access to BOTH:
|
|
7
|
+
*
|
|
8
|
+
* • CHART-BUILD-TIME constants (maxIterations, cachingDisabled,
|
|
9
|
+
* toolSchemas) — passed as direct values to the factory.
|
|
10
|
+
* • PER-RUN MUTABLE state (pendingResumeHistory from
|
|
11
|
+
* resumeOnError(), currentRunContext.runId set per run) —
|
|
12
|
+
* passed as accessor closures over the Agent instance, since
|
|
13
|
+
* these change between consecutive `agent.run()` invocations.
|
|
14
|
+
*
|
|
15
|
+
* The accessor pattern keeps `seed` decoupled from the Agent class
|
|
16
|
+
* while preserving the per-run mutability the resume + identity
|
|
17
|
+
* features need.
|
|
18
|
+
*/
|
|
19
|
+
import type { TypedScope } from 'footprintjs';
|
|
20
|
+
import type { LLMMessage, LLMToolSchema } from '../../../adapters/types.js';
|
|
21
|
+
import type { AgentState } from '../types.js';
|
|
22
|
+
export interface SeedStageDeps {
|
|
23
|
+
/** Resolved `clampIterations(opts.maxIterations ?? 10)`. Frozen at
|
|
24
|
+
* chart-build time. */
|
|
25
|
+
readonly maxIterations: number;
|
|
26
|
+
/** Resolved cache kill switch from `Agent.create({ caching: 'off' })`. */
|
|
27
|
+
readonly cachingDisabled: boolean;
|
|
28
|
+
/** Static tool schemas resolved at chart-build time. The tools slot
|
|
29
|
+
* subflow can OVERRIDE this per-iteration via `dynamicToolSchemas`,
|
|
30
|
+
* but seed populates the initial value so iter 1 has it. */
|
|
31
|
+
readonly toolSchemas: readonly LLMToolSchema[];
|
|
32
|
+
/**
|
|
33
|
+
* Read-AND-CLEAR accessor for the resume side-channel. Called exactly
|
|
34
|
+
* once per `agent.run()` from inside seed. If `resumeOnError(checkpoint)`
|
|
35
|
+
* was invoked before `run()`, this returns the checkpointed history
|
|
36
|
+
* and clears the field so the NEXT `run()` starts fresh. Returns
|
|
37
|
+
* `undefined` for the normal (non-resume) path.
|
|
38
|
+
*/
|
|
39
|
+
readonly consumePendingResumeHistory: () => readonly LLMMessage[] | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Accessor for the current run's id, used to default the memory
|
|
42
|
+
* identity when consumer didn't pass `agent.run({ identity })`. Set
|
|
43
|
+
* by RunnerBase on every `agent.run()` call before the chart starts.
|
|
44
|
+
* Returns `undefined` only in degenerate (test) cases.
|
|
45
|
+
*/
|
|
46
|
+
readonly getCurrentRunId: () => string | undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Build the seed stage function for an Agent instance. Captures both
|
|
50
|
+
* the chart-build-time constants and the per-run mutable accessors
|
|
51
|
+
* via the deps object.
|
|
52
|
+
*/
|
|
53
|
+
export declare function buildSeedStage(deps: SeedStageDeps): (scope: TypedScope<AgentState>) => void;
|
|
54
|
+
//# sourceMappingURL=seed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../../../../../src/core/agent/stages/seed.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE5E,OAAO,KAAK,EAAc,UAAU,EAAE,MAAM,aAAa,CAAC;AAE1D,MAAM,WAAW,aAAa;IAC5B;4BACwB;IACxB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,0EAA0E;IAC1E,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;IAClC;;iEAE6D;IAC7D,QAAQ,CAAC,WAAW,EAAE,SAAS,aAAa,EAAE,CAAC;IAC/C;;;;;;OAMG;IACH,QAAQ,CAAC,2BAA2B,EAAE,MAAM,SAAS,UAAU,EAAE,GAAG,SAAS,CAAC;IAC9E;;;;;OAKG;IACH,QAAQ,CAAC,eAAe,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;CACpD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,aAAa,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,UAAU,CAAC,KAAK,IAAI,CAmE3F"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* toolCalls — pausable handler for executing the LLM-requested tool
|
|
3
|
+
* calls in the agent's ReAct loop.
|
|
4
|
+
*
|
|
5
|
+
* • `execute` iterates `scope.llmLatestToolCalls`, dispatches each
|
|
6
|
+
* tool, appends results to scope.history, and increments
|
|
7
|
+
* `scope.iteration`. If a tool throws `PauseRequest` (via
|
|
8
|
+
* `pauseHere()`), commits partial state and returns the pause
|
|
9
|
+
* payload so footprintjs captures a checkpoint.
|
|
10
|
+
* • `resume` runs after the consumer supplies the human's answer.
|
|
11
|
+
* Treats that answer as the paused tool's result, appends to
|
|
12
|
+
* history, then continues the ReAct iteration loop.
|
|
13
|
+
*
|
|
14
|
+
* Dispatch resolution order:
|
|
15
|
+
* 1. Static registry built at chart-build time (registryByName).
|
|
16
|
+
* 2. External `ToolProvider.list(ctx).find(...)` if a `.toolProvider()`
|
|
17
|
+
* was wired and the tool isn't in the static registry.
|
|
18
|
+
*
|
|
19
|
+
* Permission gate (when `permissionChecker` is configured) runs BEFORE
|
|
20
|
+
* `tool.execute`. Deny → tool not executed; result is a synthetic
|
|
21
|
+
* denial string. Allow / gate_open → execution proceeds.
|
|
22
|
+
*
|
|
23
|
+
* `read_skill` is the auto-attached activation tool — when the LLM
|
|
24
|
+
* calls it with a valid Skill id, the next InjectionEngine pass
|
|
25
|
+
* activates that Skill (lifetime: turn).
|
|
26
|
+
*/
|
|
27
|
+
import type { PausableHandler, TypedScope } from 'footprintjs';
|
|
28
|
+
import type { PermissionChecker } from '../../../adapters/types.js';
|
|
29
|
+
import type { ToolProvider } from '../../../tool-providers/types.js';
|
|
30
|
+
import type { Tool } from '../../tools.js';
|
|
31
|
+
import type { AgentState } from '../types.js';
|
|
32
|
+
export interface ToolCallsHandlerDeps {
|
|
33
|
+
/** Map from tool name → Tool instance, built from the augmented
|
|
34
|
+
* registry (static .tool() entries + read_skill if any skills +
|
|
35
|
+
* shared skill tools). The dispatch primary lookup. */
|
|
36
|
+
readonly registryByName: ReadonlyMap<string, Tool>;
|
|
37
|
+
/** Optional external `.toolProvider()` for per-iteration dynamic
|
|
38
|
+
* tools (skill-scoped, multi-tenant, etc.). Consulted only when
|
|
39
|
+
* the static registry doesn't have the tool. */
|
|
40
|
+
readonly externalToolProvider?: ToolProvider;
|
|
41
|
+
/** Optional permission gate. When present, every tool dispatch
|
|
42
|
+
* awaits `check({capability: 'tool_call', ...})` BEFORE executing.
|
|
43
|
+
* Throwing checkers are treated as deny-by-default. */
|
|
44
|
+
readonly permissionChecker?: PermissionChecker;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Build the pausable tool-call handler for the agent's chart.
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildToolCallsHandler(deps: ToolCallsHandlerDeps): PausableHandler<TypedScope<AgentState>>;
|
|
50
|
+
//# sourceMappingURL=toolCalls.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolCalls.d.ts","sourceRoot":"","sources":["../../../../../src/core/agent/stages/toolCalls.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAc,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAGhF,OAAO,KAAK,EAAuB,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAE1F,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,WAAW,oBAAoB;IACnC;;4DAEwD;IACxD,QAAQ,CAAC,cAAc,EAAE,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACnD;;qDAEiD;IACjD,QAAQ,CAAC,oBAAoB,CAAC,EAAE,YAAY,CAAC;IAC7C;;4DAEwD;IACxD,QAAQ,CAAC,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;CAChD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,oBAAoB,GACzB,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CA2NzC"}
|
|
@@ -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,C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**
|
|
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":["../../../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AK,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"}
|