@infinityi/engine-lib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +488 -0
  3. package/dist/agent/agent-registry.d.ts +46 -0
  4. package/dist/agent/as-tool.d.ts +64 -0
  5. package/dist/agent/define.d.ts +35 -0
  6. package/dist/agent/handoff.d.ts +39 -0
  7. package/dist/agent/index.d.ts +20 -0
  8. package/dist/agent/index.js +38 -0
  9. package/dist/agent/registry.d.ts +27 -0
  10. package/dist/agent/types.d.ts +109 -0
  11. package/dist/context/index.d.ts +11 -0
  12. package/dist/context/index.js +21 -0
  13. package/dist/context/providers.d.ts +25 -0
  14. package/dist/context/types.d.ts +63 -0
  15. package/dist/context/window.d.ts +41 -0
  16. package/dist/errors.d.ts +93 -0
  17. package/dist/errors.js +24 -0
  18. package/dist/events/hub.d.ts +15 -0
  19. package/dist/events/index.d.ts +26 -0
  20. package/dist/events/index.js +24 -0
  21. package/dist/events/subscribers.d.ts +57 -0
  22. package/dist/events/telemetry.d.ts +61 -0
  23. package/dist/events/types.d.ts +39 -0
  24. package/dist/execution/index.d.ts +11 -0
  25. package/dist/execution/index.js +22 -0
  26. package/dist/execution/run.d.ts +35 -0
  27. package/dist/execution/types.d.ts +203 -0
  28. package/dist/execution/usage.d.ts +14 -0
  29. package/dist/index-02s1fjxr.js +226 -0
  30. package/dist/index-19pwq79t.js +0 -0
  31. package/dist/index-1p6mb2vz.js +32 -0
  32. package/dist/index-64tt9696.js +1796 -0
  33. package/dist/index-7690reng.js +96 -0
  34. package/dist/index-bqg01r42.js +354 -0
  35. package/dist/index-d4xz3abn.js +0 -0
  36. package/dist/index-dexgmwg6.js +148 -0
  37. package/dist/index-fkr3rcq9.js +97 -0
  38. package/dist/index-jg19te9v.js +0 -0
  39. package/dist/index-jp2b31xs.js +101 -0
  40. package/dist/index-jxgj4z08.js +68 -0
  41. package/dist/index-kte2h4k2.js +0 -0
  42. package/dist/index-pwr8179t.js +492 -0
  43. package/dist/index-rentvdpp.js +27 -0
  44. package/dist/index-vnby35rm.js +84 -0
  45. package/dist/index-w34cbktd.js +14 -0
  46. package/dist/index-xsv43c5j.js +39 -0
  47. package/dist/index-yrqrxwjt.js +148 -0
  48. package/dist/index-zfgr4xx3.js +90 -0
  49. package/dist/index.d.ts +45 -0
  50. package/dist/index.js +117 -0
  51. package/dist/lifecycle/component.d.ts +74 -0
  52. package/dist/lifecycle/index.d.ts +12 -0
  53. package/dist/lifecycle/index.js +72 -0
  54. package/dist/messages/factory.d.ts +24 -0
  55. package/dist/messages/index.d.ts +8 -0
  56. package/dist/messages/index.js +17 -0
  57. package/dist/messages/types.d.ts +52 -0
  58. package/dist/providers/adapter.d.ts +42 -0
  59. package/dist/providers/anthropic/index.d.ts +31 -0
  60. package/dist/providers/anthropic/map.d.ts +12 -0
  61. package/dist/providers/anthropic/stream.d.ts +9 -0
  62. package/dist/providers/google/index.d.ts +29 -0
  63. package/dist/providers/google/map.d.ts +13 -0
  64. package/dist/providers/google/stream.d.ts +11 -0
  65. package/dist/providers/http.d.ts +61 -0
  66. package/dist/providers/index.d.ts +32 -0
  67. package/dist/providers/index.js +35 -0
  68. package/dist/providers/openai/index.d.ts +34 -0
  69. package/dist/providers/openai/map.d.ts +10 -0
  70. package/dist/providers/openai/stream.d.ts +9 -0
  71. package/dist/providers/openai-compatible/index.d.ts +37 -0
  72. package/dist/providers/openai-compatible/map.d.ts +13 -0
  73. package/dist/providers/openai-compatible/stream.d.ts +11 -0
  74. package/dist/providers/shared.d.ts +34 -0
  75. package/dist/providers/sse.d.ts +19 -0
  76. package/dist/providers/stream.d.ts +69 -0
  77. package/dist/providers/types.d.ts +137 -0
  78. package/dist/runtime/index.d.ts +11 -0
  79. package/dist/runtime/index.js +11 -0
  80. package/dist/runtime/secret.d.ts +12 -0
  81. package/dist/runtime/types.d.ts +27 -0
  82. package/dist/schema/builder.d.ts +70 -0
  83. package/dist/schema/index.d.ts +13 -0
  84. package/dist/schema/index.js +15 -0
  85. package/dist/schema/json-schema.d.ts +19 -0
  86. package/dist/schema/types.d.ts +70 -0
  87. package/dist/schema/validate.d.ts +19 -0
  88. package/dist/session/index.d.ts +11 -0
  89. package/dist/session/index.js +8 -0
  90. package/dist/session/session.d.ts +31 -0
  91. package/dist/session/store.d.ts +20 -0
  92. package/dist/session/types.d.ts +55 -0
  93. package/dist/testing/conformance.d.ts +106 -0
  94. package/dist/testing/conformance.js +132 -0
  95. package/dist/testing/index.d.ts +84 -0
  96. package/dist/testing/index.js +31 -0
  97. package/dist/tools/define.d.ts +42 -0
  98. package/dist/tools/index.d.ts +11 -0
  99. package/dist/tools/index.js +15 -0
  100. package/dist/tools/result.d.ts +36 -0
  101. package/dist/tools/types.d.ts +85 -0
  102. package/docs/README.md +36 -0
  103. package/examples/README.md +24 -0
  104. package/examples/incident-analysis.ts +100 -0
  105. package/examples/lifecycle.ts +53 -0
  106. package/examples/multi-agent.ts +93 -0
  107. package/examples/terminal-coder.ts +80 -0
  108. package/package.json +114 -0
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Handoff / delegation helpers (Phase 7).
3
+ *
4
+ * A handoff lets one agent transfer the running conversation to another
5
+ * (triage → specialist) while keeping the message history intact. Rather than
6
+ * inventing a new protocol, each declared {@link AgentDefinition.handoffs}
7
+ * target is surfaced to the model as a synthetic `transfer_to_<name>` tool
8
+ * (Principle 1, provider-native): when the model calls it, the run loop
9
+ * acknowledges the tool call, emits `agent.handoff`, and switches the *active
10
+ * agent* instead of dispatching a normal tool.
11
+ *
12
+ * This module only computes the synthetic toolset and resolves targets; the
13
+ * active-agent switch lives in the run loop (`executeAgent`).
14
+ *
15
+ * @module
16
+ */
17
+ import type { ProviderTool } from "../providers/types";
18
+ import type { AgentRegistry } from "./agent-registry";
19
+ import type { AgentDefinition } from "./types";
20
+ /** Prefix for the synthetic tools the loop advertises for an agent's handoffs. */
21
+ export declare const HANDOFF_TOOL_PREFIX = "transfer_to_";
22
+ /** The synthetic transfer-tool name for a target agent. */
23
+ export declare function handoffToolName(agentName: string): string;
24
+ /**
25
+ * Resolve an agent's declared {@link AgentDefinition.handoffs} into a map keyed
26
+ * by synthetic transfer-tool name (`transfer_to_<name>`). String targets are
27
+ * resolved through `registry`.
28
+ *
29
+ * @throws {ExecutionError} if a string target is declared without a registry,
30
+ * the target name is unknown, or two targets resolve to the same name. The
31
+ * run loop also rejects synthetic handoff tool names that collide with a real
32
+ * tool on the same agent.
33
+ */
34
+ export declare function resolveHandoffTargets(agent: AgentDefinition, registry?: AgentRegistry): Map<string, AgentDefinition>;
35
+ /**
36
+ * Synthesize the provider-advertised transfer tools for a resolved target map
37
+ * (as produced by {@link resolveHandoffTargets}).
38
+ */
39
+ export declare function handoffProviderTools(targets: ReadonlyMap<string, AgentDefinition>): ProviderTool[];
@@ -0,0 +1,20 @@
1
+ /**
2
+ * `@infinityi/engine-lib/agent` — the agent contract: declarative {@link AgentDefinition}s,
3
+ * the {@link defineAgent} constructor, and the per-agent {@link ToolRegistry}.
4
+ *
5
+ * Re-exports the tool contract too, so the common path (define a tool, define an
6
+ * agent) is reachable from a single import.
7
+ *
8
+ * @module
9
+ */
10
+ export { defineAgent } from "./define";
11
+ export { createToolRegistry } from "./registry";
12
+ export type { ToolRegistry } from "./registry";
13
+ export { createAgentRegistry } from "./agent-registry";
14
+ export type { AgentRegistry } from "./agent-registry";
15
+ export { asTool } from "./as-tool";
16
+ export type { AsToolOptions } from "./as-tool";
17
+ export { handoffProviderTools, handoffToolName, resolveHandoffTargets } from "./handoff";
18
+ export type { AgentDefinition, AgentHooks, GenerationSettings, InstructionContext, Instructions, } from "./types";
19
+ export { defineTool, renderToolContent, toProviderTool, toToolResultMessage } from "../tools/index";
20
+ export type { ToolContext, ToolDefinition, ToolFailure, ToolResult, ToolSpec, ToolSuccess, } from "../tools/index";
@@ -0,0 +1,38 @@
1
+ import {
2
+ asTool,
3
+ createAgentRegistry,
4
+ defineAgent
5
+ } from "../index-jp2b31xs.js";
6
+ import {
7
+ createToolRegistry,
8
+ handoffProviderTools,
9
+ handoffToolName,
10
+ resolveHandoffTargets
11
+ } from "../index-pwr8179t.js";
12
+ import"../index-yrqrxwjt.js";
13
+ import"../index-fkr3rcq9.js";
14
+ import"../index-02s1fjxr.js";
15
+ import"../index-zfgr4xx3.js";
16
+ import"../index-7690reng.js";
17
+ import {
18
+ defineTool
19
+ } from "../index-w34cbktd.js";
20
+ import {
21
+ renderToolContent,
22
+ toProviderTool,
23
+ toToolResultMessage
24
+ } from "../index-rentvdpp.js";
25
+ import"../index-1p6mb2vz.js";
26
+ export {
27
+ toToolResultMessage,
28
+ toProviderTool,
29
+ resolveHandoffTargets,
30
+ renderToolContent,
31
+ handoffToolName,
32
+ handoffProviderTools,
33
+ defineTool,
34
+ defineAgent,
35
+ createToolRegistry,
36
+ createAgentRegistry,
37
+ asTool
38
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The per-agent tool registry.
3
+ *
4
+ * A {@link ToolRegistry} provides name-based lookup over an agent's toolset and
5
+ * generates the provider-facing JSON-Schema toolset. It is built eagerly so
6
+ * duplicate tool names fail fast (a configuration bug), rather than surfacing as
7
+ * a confusing mid-run dispatch error.
8
+ *
9
+ * @module
10
+ */
11
+ import type { ProviderTool } from "../providers/types";
12
+ import type { ToolDefinition } from "../tools/types";
13
+ /** Name-based lookup over a toolset, plus provider-toolset generation. */
14
+ export interface ToolRegistry {
15
+ readonly size: number;
16
+ has(name: string): boolean;
17
+ get(name: string): ToolDefinition | undefined;
18
+ list(): readonly ToolDefinition[];
19
+ /** Project every tool into a Phase-2 {@link ProviderTool}, preserving order. */
20
+ toProviderTools(): ProviderTool[];
21
+ }
22
+ /**
23
+ * Build a {@link ToolRegistry} from a toolset.
24
+ *
25
+ * @throws {ExecutionError} if two tools share a name.
26
+ */
27
+ export declare function createToolRegistry(tools: readonly ToolDefinition[]): ToolRegistry;
@@ -0,0 +1,109 @@
1
+ /**
2
+ * The agent contract — a declarative description of *what* an agent is.
3
+ *
4
+ * An {@link AgentDefinition} is plain data, not a class hierarchy: which
5
+ * {@link Provider} to call, what {@link Instructions} to send, which
6
+ * {@link ToolDefinition tools} the model may invoke, the default
7
+ * {@link GenerationSettings generation settings}, and {@link AgentHooks}
8
+ * lifecycle slots. The stable application path is `defineAgent`; hand-written
9
+ * {@link AgentDefinition} objects are mainly useful for tests and adapters.
10
+ * Definitions are data only: the run loop resolves instructions, invokes
11
+ * hooks, advertises tools, and performs handoffs.
12
+ *
13
+ * @module
14
+ */
15
+ import type { Message } from "../messages/types";
16
+ import type { CompletionResult, Provider, ToolCall, ToolChoice, Usage } from "../providers/types";
17
+ import type { EngineContext } from "../runtime/types";
18
+ import type { AgentError } from "../errors";
19
+ import type { ToolDefinition, ToolResult } from "../tools/types";
20
+ /**
21
+ * Default per-turn generation knobs an agent applies to each provider call.
22
+ * A subset of the Phase-2 `CompletionRequest`; the run loop merges these in,
23
+ * letting per-run overrides win.
24
+ */
25
+ export interface GenerationSettings {
26
+ /** Model id; falls back to the provider's `defaultModel` when omitted. */
27
+ readonly model?: string;
28
+ readonly temperature?: number;
29
+ readonly topP?: number;
30
+ readonly maxOutputTokens?: number;
31
+ readonly stopSequences?: readonly string[];
32
+ /** How the model may use tools this turn. */
33
+ readonly toolChoice?: ToolChoice;
34
+ }
35
+ /** Context available to dynamic {@link Instructions}. */
36
+ export interface InstructionContext extends EngineContext {
37
+ readonly agent: AgentDefinition;
38
+ }
39
+ /**
40
+ * System instructions: a static string, or a function of run context resolved
41
+ * at run time so the host can fold in per-run facts. Resolved instructions are
42
+ * injected into the system layer for the provider request, but are not persisted
43
+ * to sessions.
44
+ */
45
+ export type Instructions = string | ((ctx: InstructionContext) => string | Promise<string>);
46
+ /**
47
+ * Lifecycle hook slots. Declared here on the agent; the run loop invokes them
48
+ * at well-defined points. All hooks are optional, awaited, and receive the
49
+ * shared engine context.
50
+ *
51
+ * Public `RunEvent`s are emitted before the corresponding hook is invoked.
52
+ * Hook failures fail the run and are routed through `onError`; an `onError`
53
+ * failure never replaces the original run failure.
54
+ */
55
+ export interface AgentHooks {
56
+ /** Before the first provider call. */
57
+ onStart?(event: {
58
+ agent: AgentDefinition;
59
+ messages: Message[];
60
+ }, ctx: EngineContext): void | Promise<void>;
61
+ /** After each completed provider turn. */
62
+ onStep?(event: {
63
+ step: number;
64
+ result: CompletionResult;
65
+ }, ctx: EngineContext): void | Promise<void>;
66
+ /** Before a validated tool call is dispatched. */
67
+ onToolCall?(event: {
68
+ call: ToolCall;
69
+ tool: ToolDefinition;
70
+ }, ctx: EngineContext): void | Promise<void>;
71
+ /** After a tool produces a result. */
72
+ onToolResult?(event: {
73
+ call: ToolCall;
74
+ result: ToolResult;
75
+ }, ctx: EngineContext): void | Promise<void>;
76
+ /** When the run produces its final answer. */
77
+ onFinish?(event: {
78
+ output: string;
79
+ usage?: Usage;
80
+ }, ctx: EngineContext): void | Promise<void>;
81
+ /** When the run fails. */
82
+ onError?(event: {
83
+ error: AgentError;
84
+ }, ctx: EngineContext): void | Promise<void>;
85
+ /** When this agent hands the run off to another agent (Phase 7). */
86
+ onHandoff?(event: {
87
+ from: AgentDefinition;
88
+ to: AgentDefinition;
89
+ }, ctx: EngineContext): void | Promise<void>;
90
+ }
91
+ /** A declarative agent definition — data describing behavior, executed by the runtime. */
92
+ export interface AgentDefinition {
93
+ readonly name: string;
94
+ readonly provider: Provider;
95
+ readonly instructions?: Instructions;
96
+ readonly tools?: readonly ToolDefinition[];
97
+ /**
98
+ * Agents this one may hand the run off to (Phase 7). Each target becomes a
99
+ * synthetic `transfer_to_<name>` tool the model can call to switch the active
100
+ * agent. A target may be given directly as an {@link AgentDefinition}, or by
101
+ * name as a `string` resolved through the {@link AgentRegistry} passed in
102
+ * `RunOptions.registry`. String targets without a registry fail the run.
103
+ */
104
+ readonly handoffs?: readonly (AgentDefinition | string)[];
105
+ /** Default generation settings applied to each turn. */
106
+ readonly generation?: GenerationSettings;
107
+ /** Lifecycle hook slots. */
108
+ readonly hooks?: AgentHooks;
109
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `@infinityi/engine-lib/context` — run-time context injection ({@link staticContext},
3
+ * {@link dynamicContext}) and context-window management ({@link truncateOldest},
4
+ * {@link summarizeOldest}, {@link estimateTokens}). engine-lib injects context and
5
+ * keeps history within budget; the host owns the content and retrieval.
6
+ *
7
+ * @module
8
+ */
9
+ export { dynamicContext, resolveContext, staticContext } from "./providers";
10
+ export { applyContextWindow, estimateTokens, summarizeOldest, truncateOldest, } from "./window";
11
+ export type { ContextItem, ContextProvider, ContextStrategy, ContextStrategyContext, ContextWindowOptions, TokenCounter, } from "./types";
@@ -0,0 +1,21 @@
1
+ import"../index-d4xz3abn.js";
2
+ import {
3
+ applyContextWindow,
4
+ dynamicContext,
5
+ estimateTokens,
6
+ resolveContext,
7
+ staticContext,
8
+ summarizeOldest,
9
+ truncateOldest
10
+ } from "../index-yrqrxwjt.js";
11
+ import"../index-7690reng.js";
12
+ import"../index-1p6mb2vz.js";
13
+ export {
14
+ truncateOldest,
15
+ summarizeOldest,
16
+ staticContext,
17
+ resolveContext,
18
+ estimateTokens,
19
+ dynamicContext,
20
+ applyContextWindow
21
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Built-in {@link ContextProvider}s and the resolver that turns them into the
3
+ * system messages injected at the head of a run. Context providers are resolved
4
+ * once per run, before the first provider call; their output is never persisted
5
+ * to a session.
6
+ *
7
+ * @module
8
+ */
9
+ import type { Message } from "../messages/types";
10
+ import type { EngineContext } from "../runtime/types";
11
+ import type { ContextProvider } from "./types";
12
+ /**
13
+ * Inject static facts verbatim. `content` is rendered as-is (strings) or
14
+ * JSON-encoded; an optional `title` becomes a markdown heading.
15
+ */
16
+ export declare function staticContext(content: unknown, title?: string): ContextProvider;
17
+ /** Inject context computed lazily at run time from the {@link EngineContext}. */
18
+ export declare function dynamicContext(name: string, fn: (ctx: EngineContext) => unknown | Promise<unknown>, title?: string): ContextProvider;
19
+ /**
20
+ * Resolve all providers and fold their items into a single `system` message
21
+ * (the injected-context block), or `[]` when there is nothing to inject.
22
+ * Providers resolve concurrently; their declaration order is preserved in the
23
+ * output.
24
+ */
25
+ export declare function resolveContext(providers: readonly ContextProvider[] | undefined, ctx: EngineContext): Promise<Message[]>;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Context injection + context-window management contracts.
3
+ *
4
+ * A {@link ContextProvider} supplies run-time facts (system state, retrieved
5
+ * documents, environment) that engine-lib merges into the system layer of a run —
6
+ * the library *injects*; the host decides the content (no RAG engine). A
7
+ * {@link ContextStrategy} keeps the growing history within the model's window via
8
+ * pluggable token-budgeting (truncate / summarize), raising
9
+ * `ContextWindowError` only when the history is irreducible. Window strategies
10
+ * reduce the request view sent to the provider; they must not mutate persisted
11
+ * or returned session history.
12
+ *
13
+ * @module
14
+ */
15
+ import type { Message } from "../messages/types";
16
+ import type { Provider } from "../providers/types";
17
+ import type { EngineContext } from "../runtime/types";
18
+ /** A unit of injected context, rendered into a system message at run time. */
19
+ export interface ContextItem {
20
+ /** Optional heading shown before the rendered content. */
21
+ readonly title?: string;
22
+ /** Strings pass through; anything else is JSON-encoded when rendered. */
23
+ readonly content: unknown;
24
+ }
25
+ /**
26
+ * A host-supplied context source, resolved once per run before the first
27
+ * provider call. Results are injected as system context and are not persisted.
28
+ */
29
+ export interface ContextProvider {
30
+ readonly name: string;
31
+ resolve(ctx: EngineContext): ContextItem[] | Promise<ContextItem[]>;
32
+ }
33
+ /** Counts tokens for a message list (pluggable; a char-based heuristic ships built-in). */
34
+ export type TokenCounter = (messages: readonly Message[]) => number;
35
+ /** Inputs a {@link ContextStrategy} needs to reduce history (besides the messages). */
36
+ export interface ContextStrategyContext {
37
+ readonly maxTokens: number;
38
+ readonly countTokens: TokenCounter;
39
+ /** Provider available to strategies that need a model call (e.g. summarize). */
40
+ readonly provider: Provider;
41
+ readonly model: string;
42
+ readonly engine: EngineContext;
43
+ }
44
+ /** A pluggable history-reduction policy for the provider request view. */
45
+ export interface ContextStrategy {
46
+ readonly name: string;
47
+ /**
48
+ * Reduce `messages` so `countTokens(result) <= maxTokens`. Must preserve
49
+ * conversational validity (for example, keep required system messages).
50
+ * Return a new request view and throw `ContextWindowError` when the history
51
+ * cannot be reduced to fit.
52
+ */
53
+ reduce(messages: Message[], ctx: ContextStrategyContext): Message[] | Promise<Message[]>;
54
+ }
55
+ /** Per-run context-window budgeting configuration. */
56
+ export interface ContextWindowOptions {
57
+ /** Maximum tokens the assembled request may consume. */
58
+ readonly maxTokens: number;
59
+ /** Reduction policy; defaults to `truncateOldest()`. */
60
+ readonly strategy?: ContextStrategy;
61
+ /** Token counter; defaults to {@link estimateTokens}. */
62
+ readonly countTokens?: TokenCounter;
63
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Context-window management: a default token estimator, two built-in reduction
3
+ * strategies ({@link truncateOldest}, {@link summarizeOldest}), and
4
+ * {@link applyContextWindow} which produces the (possibly trimmed) message view
5
+ * sent to the provider without mutating the canonical history.
6
+ *
7
+ * {@link truncateOldest} is the stable default. {@link summarizeOldest} is also
8
+ * public, but it performs an additional provider call and should be chosen
9
+ * deliberately when summarization is acceptable.
10
+ *
11
+ * @module
12
+ */
13
+ import type { Message } from "../messages/types";
14
+ import type { ContextStrategy, ContextStrategyContext, ContextWindowOptions } from "./types";
15
+ /**
16
+ * Default {@link TokenCounter}: a provider-agnostic heuristic of ~4 chars/token
17
+ * over all message text. Swap in a real tokenizer via `ContextWindowOptions.countTokens`.
18
+ */
19
+ export declare function estimateTokens(messages: readonly Message[]): number;
20
+ /**
21
+ * Drop the oldest non-`system` messages until the history fits `maxTokens`.
22
+ * System messages are always retained; if they alone exceed the budget the
23
+ * history is irreducible and {@link ContextWindowError} is thrown. This is the
24
+ * default stable context-window strategy.
25
+ */
26
+ export declare function truncateOldest(): ContextStrategy;
27
+ /**
28
+ * Compress the oldest overflow into a single `system` summary via a provider
29
+ * call, keeping the most recent `keepRecent` (default 4) non-system messages
30
+ * verbatim. Throws {@link ContextWindowError} if the result still overflows.
31
+ */
32
+ export declare function summarizeOldest(opts?: {
33
+ keepRecent?: number;
34
+ }): ContextStrategy;
35
+ /**
36
+ * Produce the message view to send to the provider. Returns `messages` unchanged
37
+ * when no `window` is configured or the history already fits; otherwise applies
38
+ * the configured (or default `truncateOldest`) strategy. Never mutates the input
39
+ * or persisted session history.
40
+ */
41
+ export declare function applyContextWindow(messages: Message[], window: ContextWindowOptions | undefined, ctx: Pick<ContextStrategyContext, "provider" | "model" | "engine">): Promise<Message[]>;
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Typed error taxonomy for `@infinityi/engine-lib`.
3
+ *
4
+ * Every error the library throws is a subclass of {@link AgentError},
5
+ * so consumers can branch with a single `instanceof AgentError` check
6
+ * or narrow to a specific category.
7
+ *
8
+ * The full hierarchy is defined up-front so later phases import stable
9
+ * symbols; only {@link SchemaValidationError} and
10
+ * {@link ToolValidationError} are thrown by Phase-1 code.
11
+ *
12
+ * @module
13
+ */
14
+ import type { Usage } from "./providers/types";
15
+ /** Structured validation issue attached to {@link SchemaValidationError}. */
16
+ export interface SchemaIssue {
17
+ readonly path: ReadonlyArray<string | number>;
18
+ readonly message: string;
19
+ }
20
+ /**
21
+ * Base class for every error thrown by `@infinityi/engine-lib`.
22
+ * `instanceof AgentError` catches the entire family.
23
+ */
24
+ export declare class AgentError extends Error {
25
+ /**
26
+ * Token usage accumulated before the run failed, when known. The run loop
27
+ * stamps this onto the error it throws so a failed `runAgent` (or a failed
28
+ * sub-agent invoked via `asTool`) still surfaces the tokens it consumed
29
+ * rather than silently dropping them.
30
+ */
31
+ usage?: Usage;
32
+ constructor(message: string, options?: ErrorOptions);
33
+ }
34
+ /** A provider / LLM call failed (HTTP, protocol, refusal). */
35
+ export declare class ProviderError extends AgentError {
36
+ readonly provider?: string;
37
+ constructor(message: string, options?: ErrorOptions & {
38
+ provider?: string;
39
+ });
40
+ }
41
+ /** A tool's `execute` function threw (as opposed to returning an error result). */
42
+ export declare class ToolError extends AgentError {
43
+ readonly toolName?: string;
44
+ constructor(message: string, options?: ErrorOptions & {
45
+ toolName?: string;
46
+ });
47
+ }
48
+ /** Input failed schema validation. Carries machine-readable {@link SchemaIssue}s. */
49
+ export declare class SchemaValidationError extends AgentError {
50
+ readonly issues: ReadonlyArray<SchemaIssue>;
51
+ constructor(message: string, options: ErrorOptions & {
52
+ issues: SchemaIssue[];
53
+ });
54
+ }
55
+ /** Tool-call arguments from the model failed the tool's parameter schema. */
56
+ export declare class ToolValidationError extends SchemaValidationError {
57
+ readonly toolName?: string;
58
+ constructor(message: string, options: ErrorOptions & {
59
+ toolName?: string;
60
+ issues: SchemaIssue[];
61
+ });
62
+ }
63
+ /** The run loop / execution failed for a non-provider, non-tool reason. */
64
+ export declare class ExecutionError extends AgentError {
65
+ constructor(message: string, options?: ErrorOptions);
66
+ }
67
+ /** The model exceeded the configured max step / turn budget. */
68
+ export declare class MaxStepsExceededError extends ExecutionError {
69
+ readonly steps?: number;
70
+ constructor(message: string, options?: ErrorOptions & {
71
+ steps?: number;
72
+ });
73
+ }
74
+ /** The run exceeded the configured maximum number of agent handoffs. */
75
+ export declare class MaxHandoffsExceededError extends ExecutionError {
76
+ readonly handoffs?: number;
77
+ constructor(message: string, options?: ErrorOptions & {
78
+ handoffs?: number;
79
+ });
80
+ }
81
+ /** The run was aborted via `AbortSignal`. */
82
+ export declare class CancelledError extends AgentError {
83
+ constructor(message: string, options?: ErrorOptions);
84
+ }
85
+ /** History cannot be reduced to fit the model context window. */
86
+ export declare class ContextWindowError extends AgentError {
87
+ readonly tokens?: number;
88
+ readonly limit?: number;
89
+ constructor(message: string, options?: ErrorOptions & {
90
+ tokens?: number;
91
+ limit?: number;
92
+ });
93
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,24 @@
1
+ import {
2
+ AgentError,
3
+ CancelledError,
4
+ ContextWindowError,
5
+ ExecutionError,
6
+ MaxHandoffsExceededError,
7
+ MaxStepsExceededError,
8
+ ProviderError,
9
+ SchemaValidationError,
10
+ ToolError,
11
+ ToolValidationError
12
+ } from "./index-7690reng.js";
13
+ export {
14
+ ToolValidationError,
15
+ ToolError,
16
+ SchemaValidationError,
17
+ ProviderError,
18
+ MaxStepsExceededError,
19
+ MaxHandoffsExceededError,
20
+ ExecutionError,
21
+ ContextWindowError,
22
+ CancelledError,
23
+ AgentError
24
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * {@link createEventHub} — the multi-subscriber fan-out used by the run loop.
3
+ *
4
+ * Dispatch is ordered and awaited: each subscriber sees every event in the
5
+ * order they were registered, and the next event is not delivered until the
6
+ * current one has been handed to every subscriber. Each subscriber is
7
+ * isolated: a throw/rejection is routed to `onSubscriberError` and
8
+ * swallowed, so one misbehaving subscriber can neither abort the run nor
9
+ * prevent the others from observing the event.
10
+ *
11
+ * @module
12
+ */
13
+ import type { EventHub, EventHubOptions } from "./types";
14
+ /** Build an {@link EventHub} from a fixed list of subscribers. */
15
+ export declare function createEventHub(opts?: EventHubOptions): EventHub;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * `@infinityi/engine-lib/events` — the event system & telemetry bridge (Phase 6).
3
+ *
4
+ * The run loop already emits a typed {@link RunEvent} stream (Phase 4). This
5
+ * module adds:
6
+ *
7
+ * - {@link createEventHub} — fan a single run's events out to **multiple
8
+ * independent subscribers** with ordered, isolated delivery.
9
+ * - {@link loggingSubscriber} / {@link messageBusSubscriber} — ready-made sinks
10
+ * for forge `Logger` and `MessageBus`.
11
+ * - {@link createRunTelemetry} — automatic `forge/telemetry` spans + metrics for
12
+ * runs, provider calls, and tool calls.
13
+ *
14
+ * Wire subscribers via {@link RunOptions.subscribers}; telemetry is enabled
15
+ * automatically whenever a {@link RunOptions.telemetry} handle is supplied.
16
+ * The root package exports subscriber factories, while this subpath also
17
+ * exposes event projection and telemetry helpers for integrations.
18
+ *
19
+ * @module
20
+ */
21
+ export { createEventHub } from "./hub";
22
+ export type { EventHub, EventHubOptions, RunSubscriber } from "./types";
23
+ export { eventFields, eventPayload, loggingSubscriber, messageBusSubscriber, } from "./subscribers";
24
+ export type { LoggingSubscriberOptions, LogLevel, MessageBusSubscriberOptions, } from "./subscribers";
25
+ export { createRunTelemetry, SPAN_PROVIDER, SPAN_RUN, SPAN_TOOL } from "./telemetry";
26
+ export type { Attrs, RunTelemetry, SpanHandle } from "./telemetry";
@@ -0,0 +1,24 @@
1
+ import {
2
+ eventFields,
3
+ eventPayload,
4
+ loggingSubscriber,
5
+ messageBusSubscriber
6
+ } from "../index-jxgj4z08.js";
7
+ import {
8
+ SPAN_PROVIDER,
9
+ SPAN_RUN,
10
+ SPAN_TOOL,
11
+ createEventHub,
12
+ createRunTelemetry
13
+ } from "../index-fkr3rcq9.js";
14
+ export {
15
+ messageBusSubscriber,
16
+ loggingSubscriber,
17
+ eventPayload,
18
+ eventFields,
19
+ createRunTelemetry,
20
+ createEventHub,
21
+ SPAN_TOOL,
22
+ SPAN_RUN,
23
+ SPAN_PROVIDER
24
+ };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Built-in {@link RunSubscriber} factories and event projection helpers.
3
+ *
4
+ * These are optional conveniences for the common external sinks. They depend
5
+ * only on forge contracts (`Logger`, `MessageBus`) the host already owns, so
6
+ * engine-lib never forces a transport or a logging backend on you.
7
+ * `eventFields` and `eventPayload` are stable on the `@infinityi/engine-lib/events`
8
+ * subpath for custom subscribers, but intentionally not exported from the root
9
+ * barrel.
10
+ *
11
+ * @module
12
+ */
13
+ import type { MessageBus } from "@infinityi/forge/messaging";
14
+ import type { RunEvent } from "../execution/types";
15
+ import type { Logger } from "../runtime/types";
16
+ import type { RunSubscriber } from "./types";
17
+ /** Log severities a {@link loggingSubscriber} may emit at. */
18
+ export type LogLevel = "trace" | "debug" | "info";
19
+ /** Options for {@link loggingSubscriber}. */
20
+ export interface LoggingSubscriberOptions {
21
+ /** Severity for each event line. Defaults to `"debug"`. */
22
+ readonly level?: LogLevel;
23
+ }
24
+ /**
25
+ * A subscriber that writes one structured log line per {@link RunEvent} via a
26
+ * forge {@link Logger}. It logs compact fields from {@link eventFields}, not
27
+ * the full event payload.
28
+ */
29
+ export declare function loggingSubscriber(logger: Logger, opts?: LoggingSubscriberOptions): RunSubscriber;
30
+ /** Options for {@link messageBusSubscriber}. */
31
+ export interface MessageBusSubscriberOptions {
32
+ /** Prefix prepended to each message `type`. Defaults to `"agent."`. */
33
+ readonly typePrefix?: string;
34
+ }
35
+ /**
36
+ * A subscriber that republishes every {@link RunEvent} onto a forge
37
+ * {@link MessageBus} (e.g. `agent.run.start`, `agent.tool.call`). The payload
38
+ * is a serialization-safe projection of the event (see {@link eventPayload}).
39
+ * Publish is awaited; a failing publish surfaces through the hub's subscriber
40
+ * isolation rather than aborting the run.
41
+ */
42
+ export declare function messageBusSubscriber(bus: MessageBus, opts?: MessageBusSubscriberOptions): RunSubscriber;
43
+ /**
44
+ * Compact, log-friendly fields describing an event (no large payloads).
45
+ *
46
+ * This is a stable projection helper for custom subscribers; it is not the full
47
+ * event object.
48
+ */
49
+ export declare function eventFields(event: RunEvent): Record<string, string | number | boolean>;
50
+ /**
51
+ * A JSON-serializable projection of an event, for publishing onto a bus.
52
+ *
53
+ * This projection is intentionally smaller and more serialization-friendly than
54
+ * the full {@link RunEvent}; custom subscribers that need exact event objects
55
+ * should consume `RunEvent` directly.
56
+ */
57
+ export declare function eventPayload(event: RunEvent): Record<string, unknown>;