@context-chef/ai-sdk-middleware 0.1.0 → 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.
Files changed (2) hide show
  1. package/README.md +30 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,13 +1,17 @@
1
1
  # @context-chef/ai-sdk-middleware
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/@context-chef/ai-sdk-middleware.svg)](https://www.npmjs.com/package/@context-chef/ai-sdk-middleware)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@context-chef/ai-sdk-middleware.svg)](https://www.npmjs.com/package/@context-chef/ai-sdk-middleware)
5
+ [![License](https://img.shields.io/npm/l/@context-chef/ai-sdk-middleware.svg)](https://github.com/MyPrototypeWhat/context-chef/blob/main/LICENSE)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue.svg)](https://www.typescriptlang.org/)
7
+ [![AI SDK](https://img.shields.io/badge/AI%20SDK-v6-black.svg)](https://ai-sdk.dev)
4
8
 
5
- [Vercel AI SDK](https://sdk.vercel.ai) 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.
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 @context-chef/core ai
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
- // Use exactly like normaleverything below is unchanged
30
+ // Everything below stays exactly the sameworks 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
@@ -157,7 +181,7 @@ The middleware is **stateful** — it tracks token usage across calls to know wh
157
181
 
158
182
  ## Need More Control?
159
183
 
160
- The middleware covers the most common use case: transparent compression and truncation. For advanced features like dynamic state injection, tool namespaces, memory, or snapshot/restore, use [`@context-chef/core`](../core) directly.
184
+ The middleware covers the most common use case: transparent compression and truncation. For advanced features like dynamic state injection, tool namespaces, memory, or snapshot/restore, use [`@context-chef/core`](https://www.npmjs.com/package/@context-chef/core) directly.
161
185
 
162
186
  ## License
163
187
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@context-chef/ai-sdk-middleware",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",