@context-chef/ai-sdk-middleware 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
# @context-chef/ai-sdk-middleware
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@context-chef/ai-sdk-middleware)
|
|
4
|
+
[](https://www.npmjs.com/package/@context-chef/ai-sdk-middleware)
|
|
5
|
+
[](https://github.com/MyPrototypeWhat/context-chef/blob/main/LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://ai-sdk.dev)
|
|
4
8
|
|
|
5
|
-
[Vercel AI SDK](https://sdk.
|
|
9
|
+
[Vercel AI SDK](https://ai-sdk.dev) middleware powered by [context-chef](https://github.com/MyPrototypeWhat/context-chef). Transparent history compression, tool result truncation, and token budget management — zero code changes required.
|
|
6
10
|
|
|
7
11
|
## Installation
|
|
8
12
|
|
|
9
13
|
```bash
|
|
10
|
-
npm install @context-chef/ai-sdk-middleware
|
|
14
|
+
npm install @context-chef/ai-sdk-middleware ai
|
|
11
15
|
```
|
|
12
16
|
|
|
13
17
|
## Quick Start
|
|
@@ -19,9 +23,11 @@ import { generateText } from 'ai';
|
|
|
19
23
|
|
|
20
24
|
const model = withContextChef(openai('gpt-4o'), {
|
|
21
25
|
contextWindow: 128_000,
|
|
26
|
+
compress: { model: openai('gpt-4o-mini') },
|
|
27
|
+
truncate: { threshold: 5000, headChars: 500, tailChars: 1000 },
|
|
22
28
|
});
|
|
23
29
|
|
|
24
|
-
//
|
|
30
|
+
// Everything below stays exactly the same — works with generateText and streamText
|
|
25
31
|
const result = await generateText({
|
|
26
32
|
model,
|
|
27
33
|
messages: conversationHistory,
|
|
@@ -29,7 +35,7 @@ const result = await generateText({
|
|
|
29
35
|
});
|
|
30
36
|
```
|
|
31
37
|
|
|
32
|
-
That's it. History compression and token budget tracking happen automatically behind the scenes.
|
|
38
|
+
That's it. History compression, tool result truncation, and token budget tracking happen automatically behind the scenes.
|
|
33
39
|
|
|
34
40
|
## Features
|
|
35
41
|
|
|
@@ -72,6 +78,22 @@ const model = withContextChef(openai('gpt-4o'), {
|
|
|
72
78
|
});
|
|
73
79
|
```
|
|
74
80
|
|
|
81
|
+
Optionally persist the original content via a storage adapter so the LLM can retrieve it later via a `context://vfs/` URI:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { FileSystemAdapter } from '@context-chef/core';
|
|
85
|
+
|
|
86
|
+
const model = withContextChef(openai('gpt-4o'), {
|
|
87
|
+
contextWindow: 128_000,
|
|
88
|
+
truncate: {
|
|
89
|
+
threshold: 5000,
|
|
90
|
+
headChars: 500,
|
|
91
|
+
tailChars: 1000,
|
|
92
|
+
storage: new FileSystemAdapter('.context_vfs'), // or your own DB adapter
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
75
97
|
### Token Budget Tracking
|
|
76
98
|
|
|
77
99
|
The middleware automatically extracts token usage from `generateText` and `streamText` responses and feeds it back to the compression engine. No manual `reportTokenUsage()` calls needed.
|
|
@@ -100,6 +122,7 @@ const wrappedModel = withContextChef(model, options);
|
|
|
100
122
|
| `truncate.threshold` | `number` | Yes (if truncate) | Character count to trigger truncation |
|
|
101
123
|
| `truncate.headChars` | `number` | No | Characters to preserve from start (default: `0`) |
|
|
102
124
|
| `truncate.tailChars` | `number` | No | Characters to preserve from end (default: `1000`) |
|
|
125
|
+
| `truncate.storage` | `VFSStorageAdapter` | No | Storage adapter to persist original content before truncation |
|
|
103
126
|
| `tokenizer` | `(msgs) => number` | No | Custom tokenizer for precise counting |
|
|
104
127
|
| `onCompress` | `(summary, count) => void` | No | Hook called after compression |
|
|
105
128
|
|
|
@@ -132,11 +155,12 @@ const aiSdkPrompt = toAISDK(irMessages);
|
|
|
132
155
|
## How It Works
|
|
133
156
|
|
|
134
157
|
```
|
|
135
|
-
generateText({ model: wrappedModel, messages })
|
|
158
|
+
generateText / streamText ({ model: wrappedModel, messages })
|
|
136
159
|
|
|
|
137
160
|
v
|
|
138
161
|
transformParams (before LLM call)
|
|
139
162
|
1. Truncate large tool results (if configured)
|
|
163
|
+
- Optionally persist originals to storage adapter
|
|
140
164
|
2. Convert AI SDK messages -> context-chef IR
|
|
141
165
|
3. Run Janitor compression (if over token budget)
|
|
142
166
|
4. Convert back to AI SDK messages
|