@combycode/llm-sdk 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/agent/approval-types.d.ts +49 -0
- package/dist/agent/context-registry/layers.d.ts +39 -0
- package/dist/agent/context-registry/registry-internal.d.ts +14 -0
- package/dist/agent/context-registry/registry.d.ts +104 -0
- package/dist/agent/context-registry/types.d.ts +110 -0
- package/dist/agent/guardrail-types.d.ts +58 -0
- package/dist/agent/history-types.d.ts +40 -0
- package/dist/agent/history.d.ts +111 -0
- package/dist/agent/loop-config.d.ts +67 -0
- package/dist/agent/loop-internals.d.ts +46 -0
- package/dist/agent/loop-step-state.d.ts +20 -0
- package/dist/agent/loop.d.ts +133 -0
- package/dist/agent/tool-key.d.ts +3 -0
- package/dist/agent/types.d.ts +138 -0
- package/dist/bus/agent-bus.d.ts +99 -0
- package/dist/bus/async-context.browser.d.ts +13 -0
- package/dist/bus/async-context.d.ts +11 -0
- package/dist/bus/async-context.types.d.ts +14 -0
- package/dist/bus/hook-bus.d.ts +43 -0
- package/dist/bus/hook-map.d.ts +592 -0
- package/dist/helpers/agent.d.ts +32 -0
- package/dist/helpers/batch.d.ts +74 -0
- package/dist/helpers/calibration-store.d.ts +27 -0
- package/dist/helpers/calibration-types.d.ts +64 -0
- package/dist/helpers/chain.d.ts +30 -0
- package/dist/helpers/client-pool.d.ts +15 -0
- package/dist/helpers/client-resolver.d.ts +62 -0
- package/dist/helpers/collection.d.ts +19 -0
- package/dist/helpers/consolidate.d.ts +66 -0
- package/dist/helpers/content.d.ts +25 -0
- package/dist/helpers/conversation-export.d.ts +14 -0
- package/dist/helpers/conversation-zip.d.ts +28 -0
- package/dist/helpers/count-tokens.d.ts +26 -0
- package/dist/helpers/define-tool.d.ts +66 -0
- package/dist/helpers/delegate.d.ts +6 -0
- package/dist/helpers/embed.d.ts +24 -0
- package/dist/helpers/engine.d.ts +121 -0
- package/dist/helpers/estimate-types.d.ts +67 -0
- package/dist/helpers/estimate.d.ts +45 -0
- package/dist/helpers/estimator.d.ts +48 -0
- package/dist/helpers/handoff-types.d.ts +18 -0
- package/dist/helpers/handoff.d.ts +14 -0
- package/dist/helpers/llm.d.ts +24 -0
- package/dist/helpers/mcp.d.ts +82 -0
- package/dist/helpers/media.d.ts +58 -0
- package/dist/helpers/models.d.ts +36 -0
- package/dist/helpers/moderate-types.d.ts +73 -0
- package/dist/helpers/moderate.d.ts +13 -0
- package/dist/helpers/moderation-guardrail.d.ts +29 -0
- package/dist/helpers/observer.d.ts +36 -0
- package/dist/helpers/one-shot.d.ts +86 -0
- package/dist/helpers/parallel.d.ts +16 -0
- package/dist/helpers/realtime.d.ts +25 -0
- package/dist/helpers/route.d.ts +29 -0
- package/dist/helpers/select-model.d.ts +41 -0
- package/dist/helpers/server.d.ts +32 -0
- package/dist/helpers/transcribe.d.ts +36 -0
- package/dist/index.browser.js +38047 -0
- package/dist/index.d.ts +275 -0
- package/dist/index.js +37974 -0
- package/dist/llm/audio/voices.d.ts +7 -0
- package/dist/llm/client-config.d.ts +37 -0
- package/dist/llm/client-internal.d.ts +28 -0
- package/dist/llm/client.d.ts +68 -0
- package/dist/llm/providers/_shared/constants.d.ts +5 -0
- package/dist/llm/providers/_shared/response-utils.d.ts +8 -0
- package/dist/llm/providers/anthropic/batch.d.ts +19 -0
- package/dist/llm/providers/anthropic/constants.d.ts +13 -0
- package/dist/llm/providers/anthropic/files.d.ts +23 -0
- package/dist/llm/providers/anthropic/messages.d.ts +30 -0
- package/dist/llm/providers/google/batch.d.ts +20 -0
- package/dist/llm/providers/google/constants.d.ts +6 -0
- package/dist/llm/providers/google/embeddings.d.ts +15 -0
- package/dist/llm/providers/google/files.d.ts +22 -0
- package/dist/llm/providers/google/generate.d.ts +27 -0
- package/dist/llm/providers/google/interactions.d.ts +30 -0
- package/dist/llm/providers/google/media.d.ts +27 -0
- package/dist/llm/providers/google/realtime.d.ts +27 -0
- package/dist/llm/providers/openai/batch.d.ts +19 -0
- package/dist/llm/providers/openai/completions.d.ts +25 -0
- package/dist/llm/providers/openai/embeddings.d.ts +16 -0
- package/dist/llm/providers/openai/files.d.ts +23 -0
- package/dist/llm/providers/openai/media.d.ts +28 -0
- package/dist/llm/providers/openai/moderations.d.ts +19 -0
- package/dist/llm/providers/openai/realtime.d.ts +24 -0
- package/dist/llm/providers/openai/responses.d.ts +29 -0
- package/dist/llm/providers/openai/tiers.d.ts +13 -0
- package/dist/llm/providers/openai/transcription.d.ts +20 -0
- package/dist/llm/providers/openrouter/completions.d.ts +15 -0
- package/dist/llm/providers/openrouter/embeddings.d.ts +7 -0
- package/dist/llm/providers/openrouter/media.d.ts +25 -0
- package/dist/llm/providers/openrouter/responses.d.ts +17 -0
- package/dist/llm/providers/xai/batch.d.ts +19 -0
- package/dist/llm/providers/xai/completions.d.ts +24 -0
- package/dist/llm/providers/xai/files.d.ts +23 -0
- package/dist/llm/providers/xai/media.d.ts +27 -0
- package/dist/llm/providers/xai/responses.d.ts +20 -0
- package/dist/llm/realtime/session.d.ts +34 -0
- package/dist/llm/realtime/types.d.ts +66 -0
- package/dist/llm/server-state.d.ts +32 -0
- package/dist/llm/types/audio.d.ts +18 -0
- package/dist/llm/types/messages.d.ts +125 -0
- package/dist/llm/types/options.d.ts +51 -0
- package/dist/llm/types/provider.d.ts +39 -0
- package/dist/llm/types/request.d.ts +51 -0
- package/dist/llm/types/response.d.ts +36 -0
- package/dist/llm/types/schema-utils.d.ts +8 -0
- package/dist/llm/types/stream.d.ts +42 -0
- package/dist/llm/types/tiers.d.ts +11 -0
- package/dist/llm/types/tools.d.ts +20 -0
- package/dist/network/engine.d.ts +87 -0
- package/dist/network/errors.d.ts +14 -0
- package/dist/network/queue-state-config.d.ts +41 -0
- package/dist/network/queue-state.d.ts +45 -0
- package/dist/network/rate-limiter.d.ts +49 -0
- package/dist/network/realtime-connection.d.ts +40 -0
- package/dist/network/request-queue.d.ts +40 -0
- package/dist/network/semaphore.d.ts +12 -0
- package/dist/network/sse.d.ts +3 -0
- package/dist/network/types.d.ts +127 -0
- package/dist/plugins/batch/batcher.d.ts +45 -0
- package/dist/plugins/batch/strategy.d.ts +20 -0
- package/dist/plugins/batch/types.d.ts +54 -0
- package/dist/plugins/cache/cache.d.ts +50 -0
- package/dist/plugins/cache/file-store.d.ts +15 -0
- package/dist/plugins/cache/memory-store.d.ts +10 -0
- package/dist/plugins/cache/types.d.ts +28 -0
- package/dist/plugins/configuration/configuration.d.ts +71 -0
- package/dist/plugins/context-guard/facts.d.ts +14 -0
- package/dist/plugins/context-guard/guard.d.ts +27 -0
- package/dist/plugins/context-guard/strategies/layered.d.ts +24 -0
- package/dist/plugins/context-guard/strategies/truncate.d.ts +19 -0
- package/dist/plugins/context-guard/tools.d.ts +50 -0
- package/dist/plugins/context-guard/types.d.ts +110 -0
- package/dist/plugins/context-measurer/calibration/store.d.ts +19 -0
- package/dist/plugins/context-measurer/counter/count-api.d.ts +41 -0
- package/dist/plugins/context-measurer/counter/heuristic.d.ts +25 -0
- package/dist/plugins/context-measurer/counter/hybrid.d.ts +33 -0
- package/dist/plugins/context-measurer/counter/tiktoken.d.ts +13 -0
- package/dist/plugins/context-measurer/measurer.d.ts +49 -0
- package/dist/plugins/context-measurer/types.d.ts +40 -0
- package/dist/plugins/cost-collector/collector.d.ts +42 -0
- package/dist/plugins/cost-collector/cost-collector-internal.d.ts +58 -0
- package/dist/plugins/cost-collector/cost-collector-types.d.ts +42 -0
- package/dist/plugins/embeddings/types.d.ts +23 -0
- package/dist/plugins/files/attachment.d.ts +79 -0
- package/dist/plugins/files/provider-adapter.d.ts +37 -0
- package/dist/plugins/files/registry.d.ts +48 -0
- package/dist/plugins/files/strategy.d.ts +38 -0
- package/dist/plugins/internal-tools/backends/local.d.ts +12 -0
- package/dist/plugins/internal-tools/builtin/builtin.d.ts +21 -0
- package/dist/plugins/internal-tools/builtin/clarify.d.ts +12 -0
- package/dist/plugins/internal-tools/builtin/classify.d.ts +12 -0
- package/dist/plugins/internal-tools/builtin/score.d.ts +13 -0
- package/dist/plugins/internal-tools/builtin/structure.d.ts +11 -0
- package/dist/plugins/internal-tools/builtin/summarize.d.ts +19 -0
- package/dist/plugins/internal-tools/id.d.ts +12 -0
- package/dist/plugins/internal-tools/registry.d.ts +24 -0
- package/dist/plugins/internal-tools/runner/define.d.ts +7 -0
- package/dist/plugins/internal-tools/runner/json-enforcement.d.ts +3 -0
- package/dist/plugins/internal-tools/runner/runner.d.ts +47 -0
- package/dist/plugins/internal-tools/runner/template.d.ts +10 -0
- package/dist/plugins/internal-tools/runner/types.d.ts +57 -0
- package/dist/plugins/internal-tools/runner/variants.d.ts +34 -0
- package/dist/plugins/internal-tools/types.d.ts +80 -0
- package/dist/plugins/logger/console-sink.d.ts +26 -0
- package/dist/plugins/logger/logger.d.ts +38 -0
- package/dist/plugins/logger/types.d.ts +25 -0
- package/dist/plugins/mcp/base-transport.d.ts +58 -0
- package/dist/plugins/mcp/client.d.ts +92 -0
- package/dist/plugins/mcp/jsonrpc.d.ts +20 -0
- package/dist/plugins/mcp/oauth.d.ts +122 -0
- package/dist/plugins/mcp/sampling.d.ts +17 -0
- package/dist/plugins/mcp/tools.d.ts +20 -0
- package/dist/plugins/mcp/transport-http.d.ts +50 -0
- package/dist/plugins/mcp/transport-stdio.d.ts +23 -0
- package/dist/plugins/mcp/transport-ws.d.ts +32 -0
- package/dist/plugins/mcp/transport.d.ts +29 -0
- package/dist/plugins/mcp/types.d.ts +202 -0
- package/dist/plugins/mcp/url-guard.d.ts +70 -0
- package/dist/plugins/mcp/win-spawn.d.ts +20 -0
- package/dist/plugins/media/file-store.d.ts +24 -0
- package/dist/plugins/media/memory-store.d.ts +17 -0
- package/dist/plugins/media/output.d.ts +44 -0
- package/dist/plugins/media/source-image.d.ts +36 -0
- package/dist/plugins/media/types.d.ts +146 -0
- package/dist/plugins/model-catalog/catalog.d.ts +152 -0
- package/dist/plugins/permissions/glob.d.ts +6 -0
- package/dist/plugins/permissions/matchers.d.ts +7 -0
- package/dist/plugins/permissions/policy.d.ts +10 -0
- package/dist/plugins/permissions/types.d.ts +22 -0
- package/dist/plugins/persistence/file.d.ts +25 -0
- package/dist/plugins/persistence/memory.d.ts +23 -0
- package/dist/plugins/persistence/types.d.ts +21 -0
- package/dist/plugins/retrieval/chunker.d.ts +28 -0
- package/dist/plugins/retrieval/hosted-google.d.ts +60 -0
- package/dist/plugins/retrieval/hosted-openai.d.ts +45 -0
- package/dist/plugins/retrieval/hosted-xai.d.ts +57 -0
- package/dist/plugins/retrieval/index.d.ts +70 -0
- package/dist/plugins/retrieval/local.d.ts +50 -0
- package/dist/plugins/retrieval/types.d.ts +161 -0
- package/dist/plugins/retrieval/vector-store.d.ts +70 -0
- package/dist/plugins/scheduler/scheduler.d.ts +31 -0
- package/dist/plugins/telemetry/telemetry.d.ts +118 -0
- package/dist/plugins/tool-catalog/catalog.d.ts +31 -0
- package/dist/plugins/tool-catalog/errors.d.ts +22 -0
- package/dist/plugins/tool-catalog/types.d.ts +44 -0
- package/dist/runtime/runtime.d.ts +20 -0
- package/dist/server/auth.d.ts +25 -0
- package/dist/server/dispatch.d.ts +35 -0
- package/dist/server/loaders.d.ts +26 -0
- package/dist/server/oai-adapter.d.ts +30 -0
- package/dist/server/oai-types.d.ts +114 -0
- package/dist/server/response-store.d.ts +59 -0
- package/dist/server/router.d.ts +59 -0
- package/dist/server/server.d.ts +61 -0
- package/dist/types/request-context.d.ts +46 -0
- package/dist/util/async.d.ts +3 -0
- package/dist/util/base64.d.ts +8 -0
- package/dist/util/duration.d.ts +12 -0
- package/dist/util/http.d.ts +9 -0
- package/dist/util/image-mime.d.ts +8 -0
- package/dist/util/json-schema.d.ts +7 -0
- package/dist/util/wav.d.ts +20 -0
- package/package.json +95 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@combycode/llm-sdk` are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/) and the project adheres to
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [1.0.0] - 2026-06-13
|
|
8
|
+
|
|
9
|
+
First public release.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Unified API across Anthropic, OpenAI, Google, xAI, and OpenRouter.
|
|
13
|
+
- Model catalog: normalised slug names, `model:tier` selectors, and
|
|
14
|
+
capability-based `select()`.
|
|
15
|
+
- Tiered pricing with cost tracking and budget limits.
|
|
16
|
+
- Service tiers end to end (request → bill → cost).
|
|
17
|
+
- Cross-environment: runs on Node, Bun, and the browser. ESM, zero runtime deps.
|
|
18
|
+
|
|
19
|
+
[1.0.0]: https://github.com/combycode/llm-sdk-ts/releases/tag/v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ORXA / CombyCode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @combycode/llm-sdk
|
|
2
|
+
|
|
3
|
+
A unified, pluggable AI SDK for accessing the LLMs of every major provider —
|
|
4
|
+
**Anthropic, OpenAI, Google, xAI, and OpenRouter** — through one API.
|
|
5
|
+
|
|
6
|
+
- **One API, every provider.** Switch model or provider without rewriting calls.
|
|
7
|
+
- **Pluggable.** Opt-in subsystems: a model catalog, cost tracking + budgets,
|
|
8
|
+
rate-limit-aware queueing, tools/agents, and an OpenAI-compatible server.
|
|
9
|
+
- **Unified model catalog** — normalised slug names (`anthropic/claude-haiku-4.5`),
|
|
10
|
+
`model:tier` selectors, capability-based `select()`, tiered pricing, and cost
|
|
11
|
+
tracking.
|
|
12
|
+
- **Cross-environment.** The same code runs on Node, Bun, and the browser. Zero
|
|
13
|
+
runtime dependencies, ESM.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @combycode/llm-sdk # or: bun add @combycode/llm-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires Node ≥ 18 or Bun ≥ 1.1.
|
|
22
|
+
|
|
23
|
+
## Quickstart
|
|
24
|
+
|
|
25
|
+
### Simplest path — a callable provider id
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { complete } from '@combycode/llm-sdk';
|
|
29
|
+
|
|
30
|
+
const r = await complete({
|
|
31
|
+
model: 'anthropic/claude-haiku-4-5', // provider/model, sent verbatim
|
|
32
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
33
|
+
prompt: 'Say hello in one word.',
|
|
34
|
+
});
|
|
35
|
+
console.log(r.text);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Unified names + capability selection
|
|
39
|
+
|
|
40
|
+
`createEngine()` registers a default engine that the helpers use automatically —
|
|
41
|
+
you only pass `engine` explicitly when you run more than one.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { complete, createEngine, select } from '@combycode/llm-sdk';
|
|
45
|
+
|
|
46
|
+
createEngine({ catalog: 'defaults', apiKeys: { anthropic: process.env.ANTHROPIC_API_KEY! } });
|
|
47
|
+
|
|
48
|
+
// Normalised slug → translated to the provider's callable id via the catalog.
|
|
49
|
+
await complete({ model: 'anthropic/claude-haiku-4.5', prompt: 'Hi' });
|
|
50
|
+
|
|
51
|
+
// Pick the cheapest model matching a capability query.
|
|
52
|
+
const best = select('type:chat; vision; cheap'); // -> "provider/slug"
|
|
53
|
+
await complete({ model: best!, prompt: 'Hi' });
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Streaming
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { createLLM } from '@combycode/llm-sdk';
|
|
60
|
+
|
|
61
|
+
const llm = createLLM({ model: 'openai/gpt-5.4-nano', apiKey: process.env.OPENAI_API_KEY });
|
|
62
|
+
for await (const ev of llm.stream('Count to 5.')) {
|
|
63
|
+
if (ev.type === 'text') process.stdout.write(ev.text);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Documentation
|
|
68
|
+
|
|
69
|
+
Full guide pages covering all export groups:
|
|
70
|
+
|
|
71
|
+
- **[docs/guide/](./docs/guide/README.md)** -- index of all guide pages
|
|
72
|
+
- [Network Engine](./docs/guide/network.md)
|
|
73
|
+
- [LLM Client + complete / stream](./docs/guide/llm-client.md)
|
|
74
|
+
- [Agent Loop + delegate / chain / consolidate](./docs/guide/agent-loop.md)
|
|
75
|
+
- [Tools (defineTool)](./docs/guide/tools.md)
|
|
76
|
+
- [Tokens + Embeddings](./docs/guide/tokens-embeddings.md)
|
|
77
|
+
- [Cost Tracking + estimate()](./docs/guide/cost.md)
|
|
78
|
+
- [Observability / Telemetry](./docs/guide/telemetry.md)
|
|
79
|
+
- [Media / Files / Batch](./docs/guide/media-files-batch.md)
|
|
80
|
+
- [MCP (Model Context Protocol)](./docs/guide/mcp.md)
|
|
81
|
+
- [Context Guard + Permissions + Persistence + Cache](./docs/guide/context-guard.md)
|
|
82
|
+
- [OpenAI-Compatible Server](./docs/guide/server.md)
|
|
83
|
+
- [Agent Patterns](./docs/guide/agent-patterns.md)
|
|
84
|
+
- [Moderation](./docs/guide/moderation.md)
|
|
85
|
+
- [Retrieval (RAG)](./docs/guide/retrieval.md)
|
|
86
|
+
- [Approval and Checkpoints](./docs/guide/approval-and-checkpoints.md)
|
|
87
|
+
- [Realtime (Live)](./docs/guide/realtime.md)
|
|
88
|
+
|
|
89
|
+
MCP subsystem design: [docs/design/mcp.md](./docs/design/mcp.md)
|
|
90
|
+
|
|
91
|
+
## Development
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
bun install
|
|
95
|
+
bun test # unit tests
|
|
96
|
+
bun run typecheck # tsc --noEmit
|
|
97
|
+
bun run lint # biome
|
|
98
|
+
bun run build # dist/ (node + browser builds + types)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** HITL (Human-in-the-Loop) approval types.
|
|
2
|
+
*
|
|
3
|
+
* ApprovalRequest — sent to the approve() callback when a tool call needs human sign-off.
|
|
4
|
+
* ApprovalDecision — returned by the approver to tell the loop what to do next.
|
|
5
|
+
* PendingToolCall — serializable record stored in AgentLoopSnapshot while approval is awaited. */
|
|
6
|
+
import type { TraceContext } from '../network/types';
|
|
7
|
+
/** Sent to the `approve` callback when a tool call needs human approval. */
|
|
8
|
+
export interface ApprovalRequest {
|
|
9
|
+
/** Unique ID of the tool call (from the LLM response). */
|
|
10
|
+
callId: string;
|
|
11
|
+
/** Name of the tool being called. */
|
|
12
|
+
toolName: string;
|
|
13
|
+
/** Arguments the model wants to pass to the tool. */
|
|
14
|
+
arguments: Record<string, unknown>;
|
|
15
|
+
/** Human-readable reason why approval is required (from the matching permission rule). */
|
|
16
|
+
reason?: string;
|
|
17
|
+
/** Step index within the run. */
|
|
18
|
+
step: number;
|
|
19
|
+
/** Run identity: sessionId = agentId (ConversationHistory id),
|
|
20
|
+
* requestId = runId for this .complete()/.stream() invocation,
|
|
21
|
+
* callId = this tool call's id (same as ApprovalRequest.callId). */
|
|
22
|
+
trace: TraceContext;
|
|
23
|
+
}
|
|
24
|
+
/** Returned by the approver to direct the loop. */
|
|
25
|
+
export interface ApprovalDecision {
|
|
26
|
+
/** 'approve' — execute the tool normally.
|
|
27
|
+
* 'deny' — block the tool; the model receives a denial message.
|
|
28
|
+
* 'skip' — skip silently; the model receives a skip message. */
|
|
29
|
+
decision: 'approve' | 'deny' | 'skip';
|
|
30
|
+
/** When set, inject this string as the tool result instead of executing. */
|
|
31
|
+
overrideResult?: string;
|
|
32
|
+
/** Optional note logged in the tool report. */
|
|
33
|
+
note?: string;
|
|
34
|
+
}
|
|
35
|
+
/** Serializable record of a pending approval stored in AgentLoopSnapshot. */
|
|
36
|
+
export interface PendingToolCall {
|
|
37
|
+
/** Unique ID of the tool call. */
|
|
38
|
+
callId: string;
|
|
39
|
+
/** Name of the tool. */
|
|
40
|
+
toolName: string;
|
|
41
|
+
/** Arguments the model wanted to pass. */
|
|
42
|
+
arguments: Record<string, unknown>;
|
|
43
|
+
/** Step index within the run. */
|
|
44
|
+
step: number;
|
|
45
|
+
/** ISO timestamp (ms since epoch) when the approval was requested. */
|
|
46
|
+
requestedAt: number;
|
|
47
|
+
/** Run ID of the suspended run. */
|
|
48
|
+
runId: string;
|
|
49
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Well-known system layer names + priorities used by SDK subsystems.
|
|
2
|
+
*
|
|
3
|
+
* AgentLoop, ContextGuard, memory tools and other writers all contribute
|
|
4
|
+
* layers to a conversation's `history.registry`. Their relative priority
|
|
5
|
+
* determines the rendered order of the system prompt the LLM sees.
|
|
6
|
+
*
|
|
7
|
+
* Lower priority renders earlier (closer to the top of the system prompt).
|
|
8
|
+
* Stable prefix layers go first so prompt-cache hits are maximized; dynamic
|
|
9
|
+
* contributors (memory, facts) come after. */
|
|
10
|
+
import type { ContextRegistry } from './registry';
|
|
11
|
+
/** Role / persona / behavior. Stable per-conversation. Lowest priority so
|
|
12
|
+
* cache prefixes start here. */
|
|
13
|
+
export declare const LAYER_AGENTLOOP_SYSTEM = "agentloop.system";
|
|
14
|
+
/** Run-scenario context the AgentLoop was constructed with (e.g. background
|
|
15
|
+
* for the current task). Stable per-run; rendered after system. */
|
|
16
|
+
export declare const LAYER_AGENTLOOP_CONTEXT = "agentloop.context";
|
|
17
|
+
/** Legacy ConversationHistory.system setter — kept for backward compat with
|
|
18
|
+
* pre-registry callers. */
|
|
19
|
+
export declare const LAYER_LEGACY_SYSTEM = "_legacy_system";
|
|
20
|
+
/** Memory layer for free-form notes / scratch (long-lived, low churn). */
|
|
21
|
+
export declare const LAYER_MEMORY = "memory";
|
|
22
|
+
/** Conversational facts surfaced from the user/assistant pair. Updated turn-
|
|
23
|
+
* to-turn so it lands later in render order to keep earlier prefix stable. */
|
|
24
|
+
export declare const LAYER_CHAT_FACTS = "chat.facts";
|
|
25
|
+
/** Worker-side: examples log distilled from prior tool calls. */
|
|
26
|
+
export declare const LAYER_EXECUTOR_TOOL_EXAMPLES = "executor.tool-examples";
|
|
27
|
+
/** ContextGuard's compaction summary layer (replaces compacted message ranges). */
|
|
28
|
+
export declare const LAYER_CONTEXT_GUARD_SUMMARY = "context-guard.summary";
|
|
29
|
+
export declare const PRIORITY_AGENTLOOP_SYSTEM = 10;
|
|
30
|
+
export declare const PRIORITY_LEGACY_SYSTEM = 50;
|
|
31
|
+
export declare const PRIORITY_AGENTLOOP_CONTEXT = 100;
|
|
32
|
+
export declare const PRIORITY_MEMORY = 200;
|
|
33
|
+
export declare const PRIORITY_CHAT_FACTS = 250;
|
|
34
|
+
export declare const PRIORITY_EXECUTOR_TOOL_EXAMPLES = 280;
|
|
35
|
+
export declare const PRIORITY_CONTEXT_GUARD_SUMMARY = 300;
|
|
36
|
+
/** Set the AgentLoop's persona/system layer on a registry (or remove if blank). */
|
|
37
|
+
export declare function writeAgentLoopSystem(registry: ContextRegistry, text: string | undefined, owner: string): void;
|
|
38
|
+
/** Set the AgentLoop's run-scenario context layer (or remove if blank). */
|
|
39
|
+
export declare function writeAgentLoopContext(registry: ContextRegistry, text: string | undefined, owner: string): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** ContextRegistry rendering helpers — layer filtering, sorting, and text
|
|
2
|
+
* flattening. Pure functions split out of registry.ts to keep the class file
|
|
3
|
+
* focused on the registry behavior. */
|
|
4
|
+
import type { ContentPart } from '../../llm/types/messages';
|
|
5
|
+
import type { ContextLayer, RenderOptions } from './types';
|
|
6
|
+
export interface CollectedEntry {
|
|
7
|
+
layer: ContextLayer;
|
|
8
|
+
source: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function passesFilter(layer: ContextLayer, opts: RenderOptions): boolean;
|
|
11
|
+
export declare function sortLayers(a: CollectedEntry, b: CollectedEntry): number;
|
|
12
|
+
export declare function layerToText(layer: ContextLayer): string;
|
|
13
|
+
export declare function concatContent(a: string | ContentPart[], b: string | ContentPart[]): string | ContentPart[];
|
|
14
|
+
export declare function cryptoRandomShort(): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** ContextRegistry — layered, observable, composable context store.
|
|
2
|
+
*
|
|
3
|
+
* Lives at three conceptual scopes (all use the same class, distinguished
|
|
4
|
+
* only by their parent chain): orchestrator-global, module-level, and
|
|
5
|
+
* conversation-level. Composed render walks the parent chain; child layers
|
|
6
|
+
* override parents by default, or merge additively via layer.mergeParent.
|
|
7
|
+
*
|
|
8
|
+
* Events bubble from parent to children — subscribers on child registries
|
|
9
|
+
* see all effective changes without wiring to parents manually. */
|
|
10
|
+
import type { ContentPart } from '../../llm/types/messages';
|
|
11
|
+
import type { ContextLayer, ContextRegistryConfig, RegistryEventHandler, RegistrySnapshot, RenderOptions, RenderResult, SetLayerOptions, SizeChangeHandler } from './types';
|
|
12
|
+
export declare class ContextRegistry {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
private readonly defaultOwner;
|
|
15
|
+
private readonly separator;
|
|
16
|
+
private readonly counter;
|
|
17
|
+
private readonly layers;
|
|
18
|
+
private readonly exactSubs;
|
|
19
|
+
private readonly prefixSubs;
|
|
20
|
+
private readonly wildcardSubs;
|
|
21
|
+
private readonly sizeSubs;
|
|
22
|
+
private _parent;
|
|
23
|
+
private parentUnsub;
|
|
24
|
+
constructor(config?: ContextRegistryConfig);
|
|
25
|
+
get parent(): ContextRegistry | null;
|
|
26
|
+
/** Attach or detach a parent registry. Throws on cycle. Re-wires bubbling. */
|
|
27
|
+
setParent(parent: ContextRegistry | null): void;
|
|
28
|
+
private wouldCreateCycle;
|
|
29
|
+
/** Set or replace a layer. Bumps version. Emits 'set' (if new) or 'update'. */
|
|
30
|
+
set(name: string, content: string | ContentPart[], opts?: SetLayerOptions): ContextLayer;
|
|
31
|
+
/** Modify a layer in place via a function.
|
|
32
|
+
* - fn receives the current layer (or undefined if absent)
|
|
33
|
+
* - fn may return: new content (string | ContentPart[]) OR a full layer shape
|
|
34
|
+
* - Missing fields inherit from previous layer. */
|
|
35
|
+
patch(name: string, fn: (prev: ContextLayer | undefined) => string | ContentPart[] | ContextLayer): ContextLayer;
|
|
36
|
+
/** Lookup by name. Does NOT walk parent chain. Use render() for composed views. */
|
|
37
|
+
get(name: string): ContextLayer | undefined;
|
|
38
|
+
has(name: string): boolean;
|
|
39
|
+
/** Remove a layer. Returns true if something was removed. */
|
|
40
|
+
remove(name: string): boolean;
|
|
41
|
+
/** List layers, optionally filtered. Does NOT walk parent. */
|
|
42
|
+
list(filter?: {
|
|
43
|
+
tag?: string;
|
|
44
|
+
tags?: string[];
|
|
45
|
+
owner?: string;
|
|
46
|
+
}): ContextLayer[];
|
|
47
|
+
/** All layer names in this registry (not parent). */
|
|
48
|
+
names(): string[];
|
|
49
|
+
/** Compose layers into a rendered result.
|
|
50
|
+
*
|
|
51
|
+
* - If includeParent !== false AND a parent exists, parent's layers are
|
|
52
|
+
* collected first; then THIS registry's layers overlay on top.
|
|
53
|
+
* - Same-named collision: child replaces parent (default) OR merges
|
|
54
|
+
* (if child layer.mergeParent === true).
|
|
55
|
+
* - Sort: priority ascending → updatedAt ascending → name ascending. */
|
|
56
|
+
render(opts?: RenderOptions): RenderResult;
|
|
57
|
+
/** Convenience: render().flat. */
|
|
58
|
+
flat(opts?: RenderOptions): string;
|
|
59
|
+
/** Internal recursive collector — merges parent chain, applies filters. */
|
|
60
|
+
private collectForRender;
|
|
61
|
+
/** Total character count of rendered output (with given options). */
|
|
62
|
+
sizeChars(opts?: RenderOptions): number;
|
|
63
|
+
/** Estimated token count. Uses injected TokenCounter when available,
|
|
64
|
+
* otherwise a 4-chars/token heuristic. */
|
|
65
|
+
sizeTokens(ctx?: {
|
|
66
|
+
provider?: string;
|
|
67
|
+
model?: string;
|
|
68
|
+
}, opts?: RenderOptions): number;
|
|
69
|
+
/** Local (this-registry-only) char count — used for sizeBefore/sizeAfter
|
|
70
|
+
* on emitted events. Does NOT walk parent chain. */
|
|
71
|
+
private sizeCharsLocal;
|
|
72
|
+
/** Subscribe to layer events. Pattern matches layer name:
|
|
73
|
+
* - exact: 'facts'
|
|
74
|
+
* - prefix: 'memory.*'
|
|
75
|
+
* - wildcard: '*' (all events, including bubbled from parent)
|
|
76
|
+
* Returns unsubscribe. */
|
|
77
|
+
subscribe(pattern: string, handler: RegistryEventHandler): () => void;
|
|
78
|
+
/** Subscribe to every event (equivalent to subscribe('*', ...)). */
|
|
79
|
+
onChange(handler: RegistryEventHandler): () => void;
|
|
80
|
+
/** Subscribe to size changes. Fires whenever a layer changes and produces
|
|
81
|
+
* a non-zero delta. Delta reflects the EMITTING registry's own size change
|
|
82
|
+
* — subscribers on composed/child registries should recompute their own
|
|
83
|
+
* effective size if precise composed-size tracking is required. */
|
|
84
|
+
onSizeChange(handler: SizeChangeHandler): () => void;
|
|
85
|
+
/** Internal: subscribe to all events — used by children to bubble parent
|
|
86
|
+
* events. Separate from public subscribe('*') so internal bookkeeping
|
|
87
|
+
* doesn't mix with user state. */
|
|
88
|
+
private subscribeInternalWildcard;
|
|
89
|
+
/** Emit an event originating from THIS registry. */
|
|
90
|
+
private fireEvent;
|
|
91
|
+
/** Emit a bubbled event (originated in an ancestor). We re-publish to our
|
|
92
|
+
* subscribers, preserving the original event.registry for traceability. */
|
|
93
|
+
private fireBubbledEvent;
|
|
94
|
+
private dispatchToSubscribers;
|
|
95
|
+
/** Serialize to a JSON-safe snapshot. Parent is NOT serialized — caller
|
|
96
|
+
* re-attaches via setParent() after fromSnapshot. */
|
|
97
|
+
snapshot(): RegistrySnapshot;
|
|
98
|
+
/** Reconstruct from a snapshot. Events are NOT emitted for initial restore. */
|
|
99
|
+
static fromSnapshot(snap: RegistrySnapshot, config?: ContextRegistryConfig): ContextRegistry;
|
|
100
|
+
/** Total handlers across all subscription types. For test leak detection. */
|
|
101
|
+
get handlerCount(): number;
|
|
102
|
+
/** Detach from parent and clear all subscribers. Idempotent. */
|
|
103
|
+
clear(): void;
|
|
104
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/** ContextRegistry types — layered, observable, composable context storage. */
|
|
2
|
+
import type { ContentPart } from '../../llm/types/messages';
|
|
3
|
+
import type { TokenCounter } from '../types';
|
|
4
|
+
import type { ContextRegistry as ContextRegistryClass } from './registry';
|
|
5
|
+
/** A named, versioned slice of context. Each mutation bumps `version`. */
|
|
6
|
+
export interface ContextLayer {
|
|
7
|
+
/** Unique within a single registry. Also the subscribe() pattern target. */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Free-form content. Strings are most common; ContentPart[] supports multi-modal
|
|
10
|
+
* and structured hints (tool calls / results). Rendering flattens to text. */
|
|
11
|
+
content: string | ContentPart[];
|
|
12
|
+
/** Sort key when rendering. Lower renders earlier. Default 100. */
|
|
13
|
+
priority: number;
|
|
14
|
+
/** Free-form grouping tags. Render filters by tag. */
|
|
15
|
+
tags: string[];
|
|
16
|
+
/** Who wrote this last. Useful for audit and per-owner filtering. */
|
|
17
|
+
owner?: string;
|
|
18
|
+
/** Monotonic — bumps on every set/patch. */
|
|
19
|
+
version: number;
|
|
20
|
+
createdAt: number;
|
|
21
|
+
updatedAt: number;
|
|
22
|
+
/** When true AND a parent registry has a same-named layer, composed render
|
|
23
|
+
* concatenates parent content BEFORE this layer's content instead of
|
|
24
|
+
* replacing. Used for additive layers (facts, memory). Default: false. */
|
|
25
|
+
mergeParent?: boolean;
|
|
26
|
+
/** Owner-supplied arbitrary data. Not serialized further. */
|
|
27
|
+
metadata?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
/** Registry construction config. */
|
|
30
|
+
export interface ContextRegistryConfig {
|
|
31
|
+
/** Stable ID. Auto-generated if absent. */
|
|
32
|
+
id?: string;
|
|
33
|
+
/** Parent registry — registry inherits layers from parent when rendering.
|
|
34
|
+
* Child can override same-named layers OR merge them via `mergeParent`. */
|
|
35
|
+
parent?: ContextRegistryClass;
|
|
36
|
+
/** Token counter for sizeTokens() method. */
|
|
37
|
+
counter?: TokenCounter;
|
|
38
|
+
/** Default owner tag for mutations that don't specify one. */
|
|
39
|
+
defaultOwner?: string;
|
|
40
|
+
/** Separator between rendered parts in .flat output. Default '\n\n'. */
|
|
41
|
+
separator?: string;
|
|
42
|
+
}
|
|
43
|
+
/** Partial fields for set/patch — missing fields inherit from existing layer
|
|
44
|
+
* (if any) or use defaults. */
|
|
45
|
+
export interface SetLayerOptions {
|
|
46
|
+
priority?: number;
|
|
47
|
+
tags?: string[];
|
|
48
|
+
owner?: string;
|
|
49
|
+
mergeParent?: boolean;
|
|
50
|
+
metadata?: Record<string, unknown>;
|
|
51
|
+
}
|
|
52
|
+
/** Render filter + formatting options. */
|
|
53
|
+
export interface RenderOptions {
|
|
54
|
+
/** Only include layers whose name is in this list. */
|
|
55
|
+
include?: string[];
|
|
56
|
+
/** Skip layers whose name is in this list. */
|
|
57
|
+
exclude?: string[];
|
|
58
|
+
/** Only include layers that have this tag. */
|
|
59
|
+
tag?: string;
|
|
60
|
+
/** Only include layers that have AT LEAST ONE of these tags. */
|
|
61
|
+
tags?: string[];
|
|
62
|
+
/** Only include layers with this owner. */
|
|
63
|
+
ownerFilter?: string;
|
|
64
|
+
/** Separator between parts when producing .flat. */
|
|
65
|
+
separator?: string;
|
|
66
|
+
/** When false, skip parent chain — render only this registry's layers.
|
|
67
|
+
* Default: true (compose parent chain). */
|
|
68
|
+
includeParent?: boolean;
|
|
69
|
+
}
|
|
70
|
+
/** One entry in the rendered output. */
|
|
71
|
+
export interface RenderedPart {
|
|
72
|
+
name: string;
|
|
73
|
+
content: string;
|
|
74
|
+
priority: number;
|
|
75
|
+
tags: string[];
|
|
76
|
+
owner?: string;
|
|
77
|
+
/** ID of the registry this layer was sourced from (this registry or a parent).
|
|
78
|
+
* Crucial for debugging cascades. */
|
|
79
|
+
registry: string;
|
|
80
|
+
}
|
|
81
|
+
/** Structured result of render(). .flat is the string for immediate use. */
|
|
82
|
+
export interface RenderResult {
|
|
83
|
+
parts: RenderedPart[];
|
|
84
|
+
flat: string;
|
|
85
|
+
totalChars: number;
|
|
86
|
+
rendered: number;
|
|
87
|
+
}
|
|
88
|
+
/** Change event fired on set/update/remove. Also bubbles from parents. */
|
|
89
|
+
export interface ContextRegistryEvent {
|
|
90
|
+
type: 'set' | 'update' | 'remove';
|
|
91
|
+
name: string;
|
|
92
|
+
previous?: ContextLayer;
|
|
93
|
+
current?: ContextLayer;
|
|
94
|
+
/** ID of the registry that originated this change (not the one forwarding). */
|
|
95
|
+
registry: string;
|
|
96
|
+
/** Total chars of the emitting registry BEFORE the change. */
|
|
97
|
+
sizeBefore: number;
|
|
98
|
+
/** Total chars of the emitting registry AFTER the change. */
|
|
99
|
+
sizeAfter: number;
|
|
100
|
+
timestamp: number;
|
|
101
|
+
}
|
|
102
|
+
export type RegistryEventHandler = (event: ContextRegistryEvent) => void;
|
|
103
|
+
export type SizeChangeHandler = (totalChars: number, delta: number) => void;
|
|
104
|
+
/** Persistence snapshot. Does NOT serialize parent — that's a runtime relationship. */
|
|
105
|
+
export interface RegistrySnapshot {
|
|
106
|
+
v: 1;
|
|
107
|
+
id: string;
|
|
108
|
+
layers: ContextLayer[];
|
|
109
|
+
separator: string;
|
|
110
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** Guardrail types — input/output validators with tripwire halt.
|
|
2
|
+
* Extracted per the library rule: types in *-types.ts, never inline. */
|
|
3
|
+
import type { Message } from '../llm/types/messages';
|
|
4
|
+
import type { CompletionResponse } from '../llm/types/response';
|
|
5
|
+
import type { TraceContext } from '../network/types';
|
|
6
|
+
/** Guardrail passed — proceed normally. */
|
|
7
|
+
export interface GuardrailPass {
|
|
8
|
+
pass: true;
|
|
9
|
+
}
|
|
10
|
+
/** Guardrail tripped — halt the run. */
|
|
11
|
+
export interface GuardrailTrip {
|
|
12
|
+
pass: false;
|
|
13
|
+
/** When true the loop MUST halt immediately (hard stop). */
|
|
14
|
+
tripwire: true;
|
|
15
|
+
/** Human-readable explanation surfaced in the response and hooks. */
|
|
16
|
+
reason: string;
|
|
17
|
+
/** Optional severity label for observability routing. */
|
|
18
|
+
severity?: 'low' | 'medium' | 'high';
|
|
19
|
+
}
|
|
20
|
+
export type GuardrailDecision = GuardrailPass | GuardrailTrip;
|
|
21
|
+
/** Context given to an input guardrail (before the LLM call). */
|
|
22
|
+
export interface InputGuardrailContext {
|
|
23
|
+
kind: 'input';
|
|
24
|
+
/** Run trace: sessionId = agentId (ConversationHistory id),
|
|
25
|
+
* requestId = runId for this .complete()/.stream() invocation. */
|
|
26
|
+
trace: TraceContext;
|
|
27
|
+
step: number;
|
|
28
|
+
messages: Message[];
|
|
29
|
+
system?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Context given to an output guardrail (after a step's response is produced). */
|
|
32
|
+
export interface OutputGuardrailContext {
|
|
33
|
+
kind: 'output';
|
|
34
|
+
/** Run trace: sessionId = agentId (ConversationHistory id),
|
|
35
|
+
* requestId = runId for this .complete()/.stream() invocation. */
|
|
36
|
+
trace: TraceContext;
|
|
37
|
+
step: number;
|
|
38
|
+
response: CompletionResponse;
|
|
39
|
+
}
|
|
40
|
+
export type GuardrailCheckContext = InputGuardrailContext | OutputGuardrailContext;
|
|
41
|
+
export interface Guardrail {
|
|
42
|
+
/** Unique label shown in hooks and error messages. */
|
|
43
|
+
name: string;
|
|
44
|
+
/** Whether this runs before ('input') or after ('output') the LLM call. */
|
|
45
|
+
kind: 'input' | 'output';
|
|
46
|
+
/** Return a decision; throw only for unexpected infrastructure errors. */
|
|
47
|
+
check(ctx: GuardrailCheckContext): Promise<GuardrailDecision>;
|
|
48
|
+
}
|
|
49
|
+
export interface GuardrailTriggeredContext {
|
|
50
|
+
runId: string;
|
|
51
|
+
agentId: string;
|
|
52
|
+
step: number;
|
|
53
|
+
guardrailName: string;
|
|
54
|
+
kind: 'input' | 'output';
|
|
55
|
+
reason: string;
|
|
56
|
+
severity?: 'low' | 'medium' | 'high';
|
|
57
|
+
trace?: TraceContext;
|
|
58
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** ConversationHistory types: entries, config, and serialized snapshots. */
|
|
2
|
+
import type { Message } from '../llm/types/messages';
|
|
3
|
+
import type { Usage } from '../llm/types/response';
|
|
4
|
+
import type { RegistrySnapshot } from './context-registry/types';
|
|
5
|
+
import type { TokenCounter } from './types';
|
|
6
|
+
export interface HistoryEntry {
|
|
7
|
+
index: number;
|
|
8
|
+
message: Message;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
model?: string;
|
|
11
|
+
usage?: Usage;
|
|
12
|
+
latencyMs?: number;
|
|
13
|
+
tokenEstimate?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface ConversationHistoryConfig {
|
|
16
|
+
id?: string;
|
|
17
|
+
/** Optional token counter. If absent, falls back to length/4 heuristic. */
|
|
18
|
+
counter?: TokenCounter;
|
|
19
|
+
/** Provider/model hint for counter strategy. */
|
|
20
|
+
provider?: string;
|
|
21
|
+
model?: string;
|
|
22
|
+
/** Which ContextGuard strategy should apply to this conversation.
|
|
23
|
+
* - string: strategy name, matched against the guard's strategies map.
|
|
24
|
+
* - false: opt out of context-guarding entirely for this conversation.
|
|
25
|
+
* - undefined: use the guard's defaultStrategy.
|
|
26
|
+
* Stored in metadata.contextStrategy; readable/writable at runtime. */
|
|
27
|
+
strategy?: string | false;
|
|
28
|
+
}
|
|
29
|
+
export interface HistorySnapshot {
|
|
30
|
+
id: string;
|
|
31
|
+
entries: HistoryEntry[];
|
|
32
|
+
/** Legacy field — preserved for backward compat with existing snapshots.
|
|
33
|
+
* When both `system` and `registry` are present, `registry` wins on restore. */
|
|
34
|
+
system?: string;
|
|
35
|
+
/** Layered context state (system prompt, facts, memory, ...). */
|
|
36
|
+
registry?: RegistrySnapshot;
|
|
37
|
+
metadata: Record<string, unknown>;
|
|
38
|
+
createdAt: number;
|
|
39
|
+
updatedAt: number;
|
|
40
|
+
}
|