@diyor28/context 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.
- package/README.md +270 -0
- package/dist/__tests__/attachment-selector.test.d.ts +11 -0
- package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
- package/dist/__tests__/attachment-selector.test.js +449 -0
- package/dist/__tests__/attachment-selector.test.js.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.js +398 -0
- package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
- package/dist/__tests__/codecs.test.d.ts +7 -0
- package/dist/__tests__/codecs.test.d.ts.map +1 -0
- package/dist/__tests__/codecs.test.js +331 -0
- package/dist/__tests__/codecs.test.js.map +1 -0
- package/dist/__tests__/compactor.test.d.ts +11 -0
- package/dist/__tests__/compactor.test.d.ts.map +1 -0
- package/dist/__tests__/compactor.test.js +519 -0
- package/dist/__tests__/compactor.test.js.map +1 -0
- package/dist/__tests__/context-graph.test.d.ts +7 -0
- package/dist/__tests__/context-graph.test.d.ts.map +1 -0
- package/dist/__tests__/context-graph.test.js +262 -0
- package/dist/__tests__/context-graph.test.js.map +1 -0
- package/dist/__tests__/hash.test.d.ts +7 -0
- package/dist/__tests__/hash.test.d.ts.map +1 -0
- package/dist/__tests__/hash.test.js +228 -0
- package/dist/__tests__/hash.test.js.map +1 -0
- package/dist/__tests__/integration.test.d.ts +15 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +728 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/__tests__/kind-order.test.d.ts +7 -0
- package/dist/__tests__/kind-order.test.d.ts.map +1 -0
- package/dist/__tests__/kind-order.test.js +243 -0
- package/dist/__tests__/kind-order.test.js.map +1 -0
- package/dist/__tests__/phase2-integration.test.d.ts +5 -0
- package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
- package/dist/__tests__/phase2-integration.test.js +222 -0
- package/dist/__tests__/phase2-integration.test.js.map +1 -0
- package/dist/__tests__/queries.test.d.ts +7 -0
- package/dist/__tests__/queries.test.d.ts.map +1 -0
- package/dist/__tests__/queries.test.js +254 -0
- package/dist/__tests__/queries.test.js.map +1 -0
- package/dist/__tests__/token-estimator.test.d.ts +7 -0
- package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
- package/dist/__tests__/token-estimator.test.js +267 -0
- package/dist/__tests__/token-estimator.test.js.map +1 -0
- package/dist/adapters/anthropic-estimator.d.ts +38 -0
- package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
- package/dist/adapters/anthropic-estimator.js +108 -0
- package/dist/adapters/anthropic-estimator.js.map +1 -0
- package/dist/adapters/attachment-resolver.d.ts +96 -0
- package/dist/adapters/attachment-resolver.d.ts.map +1 -0
- package/dist/adapters/attachment-resolver.js +176 -0
- package/dist/adapters/attachment-resolver.js.map +1 -0
- package/dist/adapters/attachment-selector.d.ts +59 -0
- package/dist/adapters/attachment-selector.d.ts.map +1 -0
- package/dist/adapters/attachment-selector.js +163 -0
- package/dist/adapters/attachment-selector.js.map +1 -0
- package/dist/adapters/gemini-estimator.d.ts +27 -0
- package/dist/adapters/gemini-estimator.d.ts.map +1 -0
- package/dist/adapters/gemini-estimator.js +80 -0
- package/dist/adapters/gemini-estimator.js.map +1 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +28 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/memory-store.d.ts +139 -0
- package/dist/adapters/memory-store.d.ts.map +1 -0
- package/dist/adapters/memory-store.js +187 -0
- package/dist/adapters/memory-store.js.map +1 -0
- package/dist/adapters/openai-estimator.d.ts +35 -0
- package/dist/adapters/openai-estimator.d.ts.map +1 -0
- package/dist/adapters/openai-estimator.js +89 -0
- package/dist/adapters/openai-estimator.js.map +1 -0
- package/dist/adapters/summarizer.d.ts +121 -0
- package/dist/adapters/summarizer.d.ts.map +1 -0
- package/dist/adapters/summarizer.js +121 -0
- package/dist/adapters/summarizer.js.map +1 -0
- package/dist/adapters/token-estimator.d.ts +63 -0
- package/dist/adapters/token-estimator.d.ts.map +1 -0
- package/dist/adapters/token-estimator.js +37 -0
- package/dist/adapters/token-estimator.js.map +1 -0
- package/dist/builder/context-builder.d.ts +186 -0
- package/dist/builder/context-builder.d.ts.map +1 -0
- package/dist/builder/context-builder.js +305 -0
- package/dist/builder/context-builder.js.map +1 -0
- package/dist/builder/context-fork.d.ts +166 -0
- package/dist/builder/context-fork.d.ts.map +1 -0
- package/dist/builder/context-fork.js +282 -0
- package/dist/builder/context-fork.js.map +1 -0
- package/dist/builder/index.d.ts +6 -0
- package/dist/builder/index.d.ts.map +1 -0
- package/dist/builder/index.js +22 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/codecs/base.d.ts +18 -0
- package/dist/codecs/base.d.ts.map +1 -0
- package/dist/codecs/base.js +39 -0
- package/dist/codecs/base.js.map +1 -0
- package/dist/codecs/conversation-history.codec.d.ts +81 -0
- package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
- package/dist/codecs/conversation-history.codec.js +89 -0
- package/dist/codecs/conversation-history.codec.js.map +1 -0
- package/dist/codecs/index.d.ts +31 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +71 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/redacted-stub.codec.d.ts +32 -0
- package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
- package/dist/codecs/redacted-stub.codec.js +64 -0
- package/dist/codecs/redacted-stub.codec.js.map +1 -0
- package/dist/codecs/structured-reference.codec.d.ts +40 -0
- package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
- package/dist/codecs/structured-reference.codec.js +81 -0
- package/dist/codecs/structured-reference.codec.js.map +1 -0
- package/dist/codecs/system-rules.codec.d.ts +32 -0
- package/dist/codecs/system-rules.codec.d.ts.map +1 -0
- package/dist/codecs/system-rules.codec.js +62 -0
- package/dist/codecs/system-rules.codec.js.map +1 -0
- package/dist/codecs/tool-output.codec.d.ts +66 -0
- package/dist/codecs/tool-output.codec.d.ts.map +1 -0
- package/dist/codecs/tool-output.codec.js +95 -0
- package/dist/codecs/tool-output.codec.js.map +1 -0
- package/dist/codecs/tool-schema.codec.d.ts +36 -0
- package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
- package/dist/codecs/tool-schema.codec.js +74 -0
- package/dist/codecs/tool-schema.codec.js.map +1 -0
- package/dist/codecs/unsafe-text.codec.d.ts +28 -0
- package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
- package/dist/codecs/unsafe-text.codec.js +63 -0
- package/dist/codecs/unsafe-text.codec.js.map +1 -0
- package/dist/graph/context-graph.d.ts +121 -0
- package/dist/graph/context-graph.d.ts.map +1 -0
- package/dist/graph/context-graph.js +166 -0
- package/dist/graph/context-graph.js.map +1 -0
- package/dist/graph/index.d.ts +8 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +24 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/kind-order.d.ts +60 -0
- package/dist/graph/kind-order.d.ts.map +1 -0
- package/dist/graph/kind-order.js +113 -0
- package/dist/graph/kind-order.js.map +1 -0
- package/dist/graph/queries.d.ts +68 -0
- package/dist/graph/queries.d.ts.map +1 -0
- package/dist/graph/queries.js +240 -0
- package/dist/graph/queries.js.map +1 -0
- package/dist/graph/views.d.ts +90 -0
- package/dist/graph/views.d.ts.map +1 -0
- package/dist/graph/views.js +173 -0
- package/dist/graph/views.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/compactor.d.ts +128 -0
- package/dist/pipeline/compactor.d.ts.map +1 -0
- package/dist/pipeline/compactor.js +346 -0
- package/dist/pipeline/compactor.js.map +1 -0
- package/dist/pipeline/index.d.ts +6 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +22 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/summarizer.d.ts +18 -0
- package/dist/pipeline/summarizer.d.ts.map +1 -0
- package/dist/pipeline/summarizer.js +68 -0
- package/dist/pipeline/summarizer.js.map +1 -0
- package/dist/policies/default-policy.d.ts +29 -0
- package/dist/policies/default-policy.d.ts.map +1 -0
- package/dist/policies/default-policy.js +58 -0
- package/dist/policies/default-policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +21 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/providers/anthropic-compiler.d.ts +58 -0
- package/dist/providers/anthropic-compiler.d.ts.map +1 -0
- package/dist/providers/anthropic-compiler.js +182 -0
- package/dist/providers/anthropic-compiler.js.map +1 -0
- package/dist/providers/capabilities.d.ts +54 -0
- package/dist/providers/capabilities.d.ts.map +1 -0
- package/dist/providers/capabilities.js +87 -0
- package/dist/providers/capabilities.js.map +1 -0
- package/dist/providers/gemini-compiler.d.ts +51 -0
- package/dist/providers/gemini-compiler.d.ts.map +1 -0
- package/dist/providers/gemini-compiler.js +206 -0
- package/dist/providers/gemini-compiler.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +24 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compiler.d.ts +46 -0
- package/dist/providers/openai-compiler.d.ts.map +1 -0
- package/dist/providers/openai-compiler.js +149 -0
- package/dist/providers/openai-compiler.js.map +1 -0
- package/dist/types/attachment.d.ts +62 -0
- package/dist/types/attachment.d.ts.map +1 -0
- package/dist/types/attachment.js +6 -0
- package/dist/types/attachment.js.map +1 -0
- package/dist/types/block.d.ts +61 -0
- package/dist/types/block.d.ts.map +1 -0
- package/dist/types/block.js +8 -0
- package/dist/types/block.js.map +1 -0
- package/dist/types/codec.d.ts +58 -0
- package/dist/types/codec.d.ts.map +1 -0
- package/dist/types/codec.js +6 -0
- package/dist/types/codec.js.map +1 -0
- package/dist/types/compiled.d.ts +91 -0
- package/dist/types/compiled.d.ts.map +1 -0
- package/dist/types/compiled.js +6 -0
- package/dist/types/compiled.js.map +1 -0
- package/dist/types/hash.d.ts +24 -0
- package/dist/types/hash.d.ts.map +1 -0
- package/dist/types/hash.js +49 -0
- package/dist/types/hash.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/policy.d.ts +128 -0
- package/dist/types/policy.d.ts.map +1 -0
- package/dist/types/policy.js +55 -0
- package/dist/types/policy.js.map +1 -0
- package/package.json +55 -0
- package/postcss.config.js +4 -0
- package/src/__tests__/attachment-selector.test.ts +559 -0
- package/src/__tests__/cache-breakpoints.test.ts +566 -0
- package/src/__tests__/codecs.test.ts +417 -0
- package/src/__tests__/compactor.test.ts +608 -0
- package/src/__tests__/context-graph.test.ts +383 -0
- package/src/__tests__/hash.test.ts +274 -0
- package/src/__tests__/integration.test.ts +866 -0
- package/src/__tests__/kind-order.test.ts +312 -0
- package/src/__tests__/phase2-integration.test.ts +253 -0
- package/src/__tests__/queries.test.ts +387 -0
- package/src/__tests__/token-estimator.test.ts +326 -0
- package/src/adapters/anthropic-estimator.ts +125 -0
- package/src/adapters/attachment-resolver.ts +295 -0
- package/src/adapters/attachment-selector.ts +218 -0
- package/src/adapters/gemini-estimator.ts +93 -0
- package/src/adapters/index.ts +12 -0
- package/src/adapters/memory-store.ts +299 -0
- package/src/adapters/openai-estimator.ts +105 -0
- package/src/adapters/summarizer.ts +250 -0
- package/src/adapters/token-estimator.ts +74 -0
- package/src/builder/context-builder.ts +467 -0
- package/src/builder/context-fork.ts +471 -0
- package/src/builder/index.ts +6 -0
- package/src/codecs/base.ts +36 -0
- package/src/codecs/conversation-history.codec.ts +108 -0
- package/src/codecs/index.ts +57 -0
- package/src/codecs/redacted-stub.codec.ts +76 -0
- package/src/codecs/structured-reference.codec.ts +96 -0
- package/src/codecs/system-rules.codec.ts +74 -0
- package/src/codecs/tool-output.codec.ts +109 -0
- package/src/codecs/tool-schema.codec.ts +87 -0
- package/src/codecs/unsafe-text.codec.ts +74 -0
- package/src/graph/context-graph.ts +205 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/kind-order.ts +125 -0
- package/src/graph/queries.ts +306 -0
- package/src/graph/views.ts +255 -0
- package/src/index.ts +31 -0
- package/src/pipeline/compactor.ts +563 -0
- package/src/pipeline/index.ts +6 -0
- package/src/pipeline/summarizer.ts +76 -0
- package/src/policies/default-policy.ts +69 -0
- package/src/policies/index.ts +5 -0
- package/src/providers/anthropic-compiler.ts +294 -0
- package/src/providers/capabilities.ts +144 -0
- package/src/providers/gemini-compiler.ts +272 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/openai-compiler.ts +191 -0
- package/src/types/attachment.ts +86 -0
- package/src/types/block.ts +84 -0
- package/src/types/codec.ts +68 -0
- package/src/types/compiled.ts +109 -0
- package/src/types/hash.ts +58 -0
- package/src/types/index.ts +10 -0
- package/src/types/policy.ts +194 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# @foundry/context
|
|
2
|
+
|
|
3
|
+
Context management library for LLM conversations with deterministic block ordering, token budgeting, and multi-provider support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Deterministic block ordering**: KIND_ORDER ensures consistent context compilation
|
|
8
|
+
- **Content-addressed blocks**: Stable hashing for deduplication and caching
|
|
9
|
+
- **Token budgeting**: First-class token estimation and overflow handling
|
|
10
|
+
- **Multi-provider support**: Anthropic Claude, OpenAI GPT, Google Gemini
|
|
11
|
+
- **Codec system**: Extensible block rendering with validation
|
|
12
|
+
- **Automatic compaction**: Tool output pruning and history summarization
|
|
13
|
+
- **Sensitivity filtering**: Fine-grained content control for sensitive data
|
|
14
|
+
- **Attachment management**: Budget-aware file selection
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm add @foundry/context
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import {
|
|
26
|
+
ContextBuilder,
|
|
27
|
+
DEFAULT_POLICY,
|
|
28
|
+
BUILT_IN_CODECS,
|
|
29
|
+
AnthropicTokenEstimator,
|
|
30
|
+
AnthropicCompiler,
|
|
31
|
+
} from '@foundry/context';
|
|
32
|
+
|
|
33
|
+
// 1. Create a context builder with default policy
|
|
34
|
+
const builder = new ContextBuilder({
|
|
35
|
+
policy: DEFAULT_POLICY,
|
|
36
|
+
codecs: BUILT_IN_CODECS,
|
|
37
|
+
tokenEstimator: new AnthropicTokenEstimator('claude-sonnet-4-5'),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// 2. Add context blocks
|
|
41
|
+
builder.addBlock({
|
|
42
|
+
kind: 'pinned',
|
|
43
|
+
codecId: 'system-rules',
|
|
44
|
+
payload: {
|
|
45
|
+
rules: ['Be helpful and concise', 'Always validate inputs'],
|
|
46
|
+
},
|
|
47
|
+
sensitivity: 'public',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
builder.addBlock({
|
|
51
|
+
kind: 'history',
|
|
52
|
+
codecId: 'conversation-history',
|
|
53
|
+
payload: {
|
|
54
|
+
messages: [
|
|
55
|
+
{ role: 'user', content: 'Hello!' },
|
|
56
|
+
{ role: 'assistant', content: 'Hi! How can I help you?' },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
sensitivity: 'public',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// 3. Compile to provider-specific format
|
|
63
|
+
const compiled = AnthropicCompiler.compile(builder.getGraph());
|
|
64
|
+
|
|
65
|
+
// 4. Send to LLM
|
|
66
|
+
const response = await anthropic.messages.create({
|
|
67
|
+
model: 'claude-sonnet-4-5',
|
|
68
|
+
max_tokens: 8000,
|
|
69
|
+
system: compiled.system,
|
|
70
|
+
messages: compiled.messages,
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Core Concepts
|
|
75
|
+
|
|
76
|
+
### Context Blocks
|
|
77
|
+
|
|
78
|
+
Context blocks are the fundamental unit of context management. Each block has:
|
|
79
|
+
|
|
80
|
+
- **kind**: Determines ordering and priority (`pinned`, `reference`, `memory`, `state`, `tool_output`, `history`, `turn`)
|
|
81
|
+
- **codecId**: Defines how the block is rendered (e.g., `system-rules`, `tool-schema`, `conversation-history`)
|
|
82
|
+
- **payload**: The actual content (codec-specific structure)
|
|
83
|
+
- **sensitivity**: Access level (`public`, `internal`, `confidential`, `secret`)
|
|
84
|
+
|
|
85
|
+
### Block Ordering (KIND_ORDER)
|
|
86
|
+
|
|
87
|
+
Blocks are automatically ordered by kind for deterministic compilation:
|
|
88
|
+
|
|
89
|
+
1. `pinned` - System rules, core instructions (always at top)
|
|
90
|
+
2. `reference` - Documentation, schemas, guidelines
|
|
91
|
+
3. `memory` - Long-term facts, user preferences
|
|
92
|
+
4. `state` - Workflow state, execution context
|
|
93
|
+
5. `tool_output` - Tool/function call results
|
|
94
|
+
6. `history` - Conversation history
|
|
95
|
+
7. `turn` - Current user message (always at bottom)
|
|
96
|
+
|
|
97
|
+
### Token Budgeting
|
|
98
|
+
|
|
99
|
+
The library automatically manages token budgets:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const policy = {
|
|
103
|
+
...DEFAULT_POLICY,
|
|
104
|
+
contextWindow: 200_000,
|
|
105
|
+
completionReserve: 8_000,
|
|
106
|
+
overflowStrategy: 'compact', // Auto-prune old tool outputs
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Built-in Codecs
|
|
111
|
+
|
|
112
|
+
- `system-rules` - System instructions and rules
|
|
113
|
+
- `tool-schema` - Function/tool definitions
|
|
114
|
+
- `structured-reference` - Markdown documentation
|
|
115
|
+
- `conversation-history` - Chat messages
|
|
116
|
+
- `tool-output` - Function call results
|
|
117
|
+
- `redacted-stub` - Placeholder for sensitive content
|
|
118
|
+
- `unsafe-text` - Raw text (bypasses validation)
|
|
119
|
+
|
|
120
|
+
### Custom Codecs
|
|
121
|
+
|
|
122
|
+
Implement the `BlockCodec` interface to create custom renderers:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { BlockCodec, registerCodec } from '@foundry/context';
|
|
126
|
+
|
|
127
|
+
const myCodec: BlockCodec = {
|
|
128
|
+
codecId: 'my-custom-codec',
|
|
129
|
+
|
|
130
|
+
encode(payload: unknown) {
|
|
131
|
+
// Validate and return typed payload
|
|
132
|
+
return payload as MyPayloadType;
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
render(payload: MyPayloadType) {
|
|
136
|
+
// Return rendered text
|
|
137
|
+
return `# Custom Content\n${payload.text}`;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
estimateTokens(payload: MyPayloadType) {
|
|
141
|
+
// Return estimated token count
|
|
142
|
+
return payload.text.length / 4;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
registerCodec(myCodec);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Multi-Provider Support
|
|
150
|
+
|
|
151
|
+
### Anthropic Claude
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { AnthropicTokenEstimator, AnthropicCompiler } from '@foundry/context';
|
|
155
|
+
|
|
156
|
+
const estimator = new AnthropicTokenEstimator('claude-sonnet-4-5');
|
|
157
|
+
const compiled = AnthropicCompiler.compile(graph);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### OpenAI GPT
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { OpenAITokenEstimator, OpenAICompiler } from '@foundry/context';
|
|
164
|
+
|
|
165
|
+
const estimator = new OpenAITokenEstimator('gpt-5.2');
|
|
166
|
+
const compiled = OpenAICompiler.compile(graph);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Google Gemini
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { GeminiTokenEstimator, GeminiCompiler } from '@foundry/context';
|
|
173
|
+
|
|
174
|
+
const estimator = new GeminiTokenEstimator('gemini-3-pro-preview');
|
|
175
|
+
const compiled = GeminiCompiler.compile(graph);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Advanced Features
|
|
179
|
+
|
|
180
|
+
### Automatic Compaction
|
|
181
|
+
|
|
182
|
+
When token budget is exceeded, the library automatically:
|
|
183
|
+
|
|
184
|
+
1. Prunes old tool outputs (configurable age threshold)
|
|
185
|
+
2. Summarizes conversation history
|
|
186
|
+
3. Removes least-recently-used attachments
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const policy = {
|
|
190
|
+
...DEFAULT_POLICY,
|
|
191
|
+
compaction: {
|
|
192
|
+
pruneToolOutputs: true,
|
|
193
|
+
maxToolOutputAge: 3600, // 1 hour
|
|
194
|
+
summarizeHistory: true,
|
|
195
|
+
maxHistoryMessages: 20,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Context Forking
|
|
201
|
+
|
|
202
|
+
Create isolated branches for parallel operations:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
const fork = builder.fork({
|
|
206
|
+
branchId: 'speculative-execution',
|
|
207
|
+
sensitivity: {
|
|
208
|
+
maxSensitivity: 'public', // Restrict to public data only
|
|
209
|
+
redactRestricted: true,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
fork.addBlock({
|
|
214
|
+
kind: 'turn',
|
|
215
|
+
codecId: 'conversation-history',
|
|
216
|
+
payload: { messages: [{ role: 'user', content: 'What if...?' }] },
|
|
217
|
+
sensitivity: 'public',
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Original builder is unchanged
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Attachment Management
|
|
224
|
+
|
|
225
|
+
Smart selection of attachments based on budget and priority:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { AttachmentSelector } from '@foundry/context';
|
|
229
|
+
|
|
230
|
+
const selector = new AttachmentSelector({
|
|
231
|
+
maxTokensTotal: 10_000,
|
|
232
|
+
selectionStrategy: {
|
|
233
|
+
rankBy: ['purpose', 'user_mention', 'recency'],
|
|
234
|
+
purposePriority: {
|
|
235
|
+
evidence: 1, // Highest priority
|
|
236
|
+
input: 2,
|
|
237
|
+
context: 3,
|
|
238
|
+
artifact: 4, // Lowest priority
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const selected = selector.select(attachments, tokenEstimator);
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Testing
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# Run all tests
|
|
250
|
+
pnpm test
|
|
251
|
+
|
|
252
|
+
# Run with coverage
|
|
253
|
+
pnpm test:coverage
|
|
254
|
+
|
|
255
|
+
# Watch mode
|
|
256
|
+
pnpm test:watch
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Documentation
|
|
260
|
+
|
|
261
|
+
See `/docs` for detailed documentation:
|
|
262
|
+
|
|
263
|
+
- Architecture overview
|
|
264
|
+
- Token estimation guide
|
|
265
|
+
- Custom codec development
|
|
266
|
+
- Provider integration guide
|
|
267
|
+
|
|
268
|
+
## License
|
|
269
|
+
|
|
270
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for AttachmentSelector: budget enforcement and ranking.
|
|
3
|
+
*
|
|
4
|
+
* Tests:
|
|
5
|
+
* - Budget enforcement (token limit)
|
|
6
|
+
* - Ranking by purpose, user mention, recency
|
|
7
|
+
* - Deterministic ordering (same input → same selection)
|
|
8
|
+
* - Token estimation for different attachment types
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=attachment-selector.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment-selector.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/attachment-selector.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
|