@basedchef/contextkit 0.1.2 → 0.1.4

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 CHANGED
@@ -2,13 +2,7 @@
2
2
 
3
3
  TypeScript SDK for ContextKit, a Bankr-hosted x402 context API for AI agents.
4
4
 
5
- Use it when you are building an advanced TypeScript integration that needs typed calls to ContextKit direct APIs, webhook verification, and optional x402 payment handling. For the simplest paid path, agents can call the Bankr-hosted x402 endpoints directly without this SDK.
6
-
7
- ## Install
8
-
9
- ```bash
10
- npm install @basedchef/contextkit
11
- ```
5
+ Use it when you are building a TypeScript integration that needs typed calls to ContextKit direct APIs, webhook verification, API-key credits, and optional x402 payment handling. For the simplest paid path, agents can still call the Bankr-hosted x402 endpoints directly without this SDK.
12
6
 
13
7
  ## What It Does
14
8
 
@@ -18,64 +12,298 @@ npm install @basedchef/contextkit
18
12
  - Extract durable user profile memory.
19
13
  - Estimate tokens for API-key workflows.
20
14
  - Verify signed ContextKit webhook deliveries.
21
- - Attach API keys and optional x402 payment handlers for direct API integrations.
15
+ - Attach API keys for direct API integrations.
16
+ - Use account credits so SDK users can call paid endpoints without Bankr.
17
+ - Optionally attach an x402 payment handler when credits are not available.
18
+
19
+ ContextKit has two usage paths:
20
+
21
+ - **Bankr-hosted x402:** simplest paid path for users and agents. No SDK and no ContextKit API key required.
22
+ - **SDK + API key credits:** direct app integration. The user buys ContextKit credits, then your app calls ContextKit with an API key without requiring Bankr on every request.
23
+
24
+ ## Install
25
+
26
+ ```bash
27
+ npm install @basedchef/contextkit
28
+ ```
29
+
30
+ ## Quick Check
31
+
32
+ ```bash
33
+ node --input-type=module -e 'import { ContextKit } from "@basedchef/contextkit"; console.log(typeof ContextKit)'
34
+ ```
35
+
36
+ Expected:
37
+
38
+ ```txt
39
+ function
40
+ ```
22
41
 
23
- ## Quick Start
42
+ ## Create A Client
24
43
 
25
44
  ```ts
26
45
  import { ContextKit } from "@basedchef/contextkit";
27
46
 
28
47
  const client = new ContextKit({
29
- apiKey: process.env.CONTEXTKIT_API_KEY!,
30
- baseUrl: "https://91.107.248.223.sslip.io",
31
- x402: async (challenge) => wallet.pay(challenge)
48
+ apiKey: "ck_live_replace_me",
49
+ baseUrl: "https://91.107.248.223.sslip.io"
50
+ });
51
+ ```
52
+
53
+ ## API Key Credits
54
+
55
+ Paid direct endpoints use the API key owner's ContextKit credit balance first. If credits are available, no Bankr payment is needed per request.
56
+
57
+ ```ts
58
+ const credits = await client.credits();
59
+ console.log(credits.balanceUsd);
60
+ ```
61
+
62
+ Equivalent terminal test:
63
+
64
+ ```bash
65
+ cat > credits.mjs <<'EOF'
66
+ import { ContextKit } from "@basedchef/contextkit";
67
+
68
+ const client = new ContextKit({
69
+ apiKey: "ck_live_replace_me",
70
+ baseUrl: "https://91.107.248.223.sslip.io"
71
+ });
72
+
73
+ const result = await client.credits();
74
+ console.log(JSON.stringify(result, null, 2));
75
+ EOF
76
+
77
+ node credits.mjs
78
+ ```
79
+
80
+ If the balance is too low, direct paid routes return a normal HTTP 402 x402 challenge.
81
+
82
+ ## Buy Credits
83
+
84
+ Users can top up credits from the ContextKit dashboard:
85
+
86
+ 1. Sign in to the dashboard.
87
+ 2. Open `/dashboard/credits`.
88
+ 3. Create a USDC invoice.
89
+ 4. Send the exact USDC amount on Base to the shown wallet.
90
+ 5. Paste the transaction hash.
91
+ 6. ContextKit verifies the transaction and credits the account automatically.
92
+
93
+ The backend verifies:
94
+
95
+ - the transaction exists on Base,
96
+ - the transaction succeeded,
97
+ - the Base USDC contract emitted a `Transfer`,
98
+ - the recipient is the ContextKit wallet,
99
+ - the amount is at least the invoice amount,
100
+ - the tx hash was not already used.
101
+
102
+ ## Summarize
103
+
104
+ ```bash
105
+ cat > summarize.mjs <<'EOF'
106
+ import { ContextKit } from "@basedchef/contextkit";
107
+
108
+ const client = new ContextKit({
109
+ apiKey: "ck_live_replace_me",
110
+ baseUrl: "https://91.107.248.223.sslip.io"
32
111
  });
33
112
 
34
- const response = await client.summarize({
113
+ const result = await client.summarize({
114
+ mode: "compact",
35
115
  messages: [
36
116
  {
37
117
  role: "user",
38
- content: "Summarize this long project context for the next AI agent."
118
+ content: "We are launching a night-bus pilot. Preserve current goal, blockers, constraints, and next actions for the next AI agent."
39
119
  }
40
- ],
41
- mode: "compact"
120
+ ]
42
121
  });
122
+
123
+ console.log(JSON.stringify(result, null, 2));
124
+ EOF
125
+
126
+ node summarize.mjs
43
127
  ```
44
128
 
45
- ## API Methods
129
+ Available summarize modes:
46
130
 
47
- ```ts
48
- await client.summarize({ messages, mode: "micro" });
49
- await client.compressContext({ messages });
50
- await client.handoff({ messages });
51
- await client.extractProfile({ messages });
52
- await client.estimateTokens({ modelFamily: "openai", input: messages });
131
+ - `micro`: highest compression for agent memory.
132
+ - `compact`: default AI-to-AI transfer format.
133
+ - `extended`: human-readable continuation summary.
134
+ - `debug`: full diagnostic shape for development.
135
+
136
+ ## Compress Context
137
+
138
+ ```bash
139
+ cat > compress.mjs <<'EOF'
140
+ import { ContextKit } from "@basedchef/contextkit";
141
+
142
+ const client = new ContextKit({
143
+ apiKey: "ck_live_replace_me",
144
+ baseUrl: "https://91.107.248.223.sslip.io"
145
+ });
146
+
147
+ const result = await client.compressContext({
148
+ messages: [
149
+ {
150
+ role: "user",
151
+ content: "Project Atlas is a transit analytics platform. Stack is Next.js, Postgres, Redis. Current issues are slow reports and missing operator onboarding. Deadline is beta in six weeks."
152
+ }
153
+ ]
154
+ });
155
+
156
+ console.log(JSON.stringify(result, null, 2));
157
+ EOF
158
+
159
+ node compress.mjs
160
+ ```
161
+
162
+ ## Handoff
163
+
164
+ ```bash
165
+ cat > handoff.mjs <<'EOF'
166
+ import { ContextKit } from "@basedchef/contextkit";
167
+
168
+ const client = new ContextKit({
169
+ apiKey: "ck_live_replace_me",
170
+ baseUrl: "https://91.107.248.223.sslip.io"
171
+ });
172
+
173
+ const result = await client.handoff({
174
+ messages: [
175
+ {
176
+ role: "user",
177
+ content: "ContextKit is live on Hetzner with Postgres, dashboard auth, API credits, webhooks, and Bankr-hosted x402. Next work is docs, SDK examples, credit top-up polish, and long-context demos."
178
+ }
179
+ ]
180
+ });
181
+
182
+ console.log(JSON.stringify(result, null, 2));
183
+ EOF
184
+
185
+ node handoff.mjs
53
186
  ```
54
187
 
55
- ## Bankr-Hosted x402 Path
188
+ ## Extract Profile
56
189
 
57
- Most users and agents do not need the SDK. They can call the hosted paid endpoint from a Bankr-authenticated terminal:
190
+ ```bash
191
+ cat > profile.mjs <<'EOF'
192
+ import { ContextKit } from "@basedchef/contextkit";
193
+
194
+ const client = new ContextKit({
195
+ apiKey: "ck_live_replace_me",
196
+ baseUrl: "https://91.107.248.223.sslip.io"
197
+ });
198
+
199
+ const result = await client.extractProfile({
200
+ messages: [
201
+ {
202
+ role: "user",
203
+ content: "I prefer short technical updates, direct debugging help, clear risks, and command-by-command deployment instructions."
204
+ }
205
+ ]
206
+ });
207
+
208
+ console.log(JSON.stringify(result, null, 2));
209
+ EOF
210
+
211
+ node profile.mjs
212
+ ```
213
+
214
+ ## Memory Enrichment
215
+
216
+ `memoryEnrichment` is a direct API-key endpoint for evolving long-term memory. It is useful when your app already manages user accounts and wants durable memory without a per-request Bankr terminal flow.
58
217
 
59
218
  ```bash
60
- bankr x402 call https://x402.bankr.bot/0xdace98cd605dd56b2edc66f0f4df3687f64fd824/contextkit-summarize \
61
- -X POST \
62
- -d '{"messages":[{"role":"user","content":"Summarize this context."}]}'
219
+ cat > memory.mjs <<'EOF'
220
+ import { ContextKit } from "@basedchef/contextkit";
221
+
222
+ const client = new ContextKit({
223
+ apiKey: "ck_live_replace_me",
224
+ baseUrl: "https://91.107.248.223.sslip.io"
225
+ });
226
+
227
+ const result = await client.memoryEnrichment({
228
+ messages: [
229
+ {
230
+ role: "user",
231
+ content: "I used to prefer long weekly reports, but now I want short risk-focused updates with clear next actions."
232
+ }
233
+ ]
234
+ });
235
+
236
+ console.log(JSON.stringify(result, null, 2));
237
+ EOF
238
+
239
+ node memory.mjs
63
240
  ```
64
241
 
65
- That flow requires no ContextKit API key, no npm package, and no SDK.
242
+ ## Token Estimate
243
+
244
+ ```bash
245
+ cat > estimate.mjs <<'EOF'
246
+ import { ContextKit } from "@basedchef/contextkit";
247
+
248
+ const client = new ContextKit({
249
+ apiKey: "ck_live_replace_me",
250
+ baseUrl: "https://91.107.248.223.sslip.io"
251
+ });
252
+
253
+ const result = await client.estimateTokens({
254
+ modelFamily: "openai",
255
+ input: [
256
+ {
257
+ role: "user",
258
+ content: "We need to preserve only goals, blockers, constraints, and next steps."
259
+ }
260
+ ],
261
+ compressed: "Preserve goals, blockers, constraints, next steps."
262
+ });
66
263
 
67
- ## Direct API Path
264
+ console.log(JSON.stringify(result, null, 2));
265
+ EOF
68
266
 
69
- The SDK is for advanced integrations. Direct paid generation routes still require x402 payment. API keys are used for dashboard operations, analytics, webhook management, token estimates, and selected direct API routes.
267
+ node estimate.mjs
268
+ ```
269
+
270
+ ## Optional x402 Fallback
271
+
272
+ If an API key has no credits, ContextKit direct paid routes return HTTP 402. Advanced apps can provide an x402 payment handler.
70
273
 
71
274
  ```ts
72
275
  const client = new ContextKit({
73
- apiKey: process.env.CONTEXTKIT_API_KEY!,
276
+ apiKey: "ck_live_replace_me",
74
277
  baseUrl: "https://91.107.248.223.sslip.io",
75
- x402: async (challenge) => wallet.pay(challenge)
278
+ x402: async (challenge, request) => {
279
+ return wallet.pay(challenge, request);
280
+ }
76
281
  });
77
282
  ```
78
283
 
284
+ Most SDK integrations should instead top up credits from the dashboard and avoid per-request Bankr interaction.
285
+
286
+ ## Bankr-Hosted x402 Without SDK
287
+
288
+ For simple users and autonomous agents, this is the main path:
289
+
290
+ ```bash
291
+ bankr x402 call https://x402.bankr.bot/0xdace98cd605dd56b2edc66f0f4df3687f64fd824/contextkit-summarize \
292
+ -X POST \
293
+ -d '{"messages":[{"role":"user","content":"Summarize this context."}]}'
294
+ ```
295
+
296
+ This path requires:
297
+
298
+ - Bankr login
299
+ - USDC payment approval
300
+
301
+ This path does **not** require:
302
+
303
+ - ContextKit API key
304
+ - npm package
305
+ - SDK integration
306
+
79
307
  ## Webhook Verification
80
308
 
81
309
  ```ts
@@ -88,6 +316,45 @@ const valid = await verifyContextKitWebhook({
88
316
  });
89
317
  ```
90
318
 
319
+ ## Local SDK Development
320
+
321
+ From the repo root:
322
+
323
+ ```bash
324
+ npm install
325
+ npm --workspace @basedchef/contextkit run typecheck
326
+ npm --workspace @basedchef/contextkit run build
327
+ ```
328
+
329
+ Publish a new SDK version:
330
+
331
+ ```bash
332
+ cd packages/sdk
333
+ npm version patch --no-git-tag-version
334
+ npm run build
335
+ npm publish --access public
336
+ ```
337
+
338
+ If npm asks for browser auth or OTP, complete the npm prompt and retry with a fresh authenticator code.
339
+
340
+ ## Troubleshooting
341
+
342
+ `ContextKit API request failed with 402`
343
+
344
+ Your API key has insufficient credits. Top up credits in `/dashboard/credits` or provide an x402 payment handler.
345
+
346
+ `ContextKit API request failed with 401`
347
+
348
+ The API key is invalid, revoked, or missing required scopes.
349
+
350
+ `npm publish` says version already exists
351
+
352
+ Increase `packages/sdk/package.json` version before publishing.
353
+
354
+ `npm publish` says OTP failed
355
+
356
+ Use the current six-digit authenticator code and make sure server time is synchronized.
357
+
91
358
  ## License
92
359
 
93
360
  MIT
package/dist/index.cjs CHANGED
@@ -60,6 +60,20 @@ var ContextKit = class {
60
60
  async estimateTokens(input) {
61
61
  return this.post("/api/tokens/estimate", input);
62
62
  }
63
+ credits() {
64
+ return this.get("/api/auth/credits");
65
+ }
66
+ async get(path) {
67
+ const response = await this.fetcher(`${this.baseUrl.replace(/\/$/, "")}${path}`, {
68
+ headers: {
69
+ Authorization: `Bearer ${this.options.apiKey}`
70
+ }
71
+ });
72
+ if (response.ok) {
73
+ return await response.json();
74
+ }
75
+ throw new ContextKitError(response.status, await response.text());
76
+ }
63
77
  async post(path, body) {
64
78
  const url = `${this.baseUrl.replace(/\/$/, "")}${path}`;
65
79
  const init = {
package/dist/index.d.cts CHANGED
@@ -191,6 +191,23 @@ type MemoryEnrichmentResponse = {
191
191
  deprecatedMemories: string[];
192
192
  confidence: number;
193
193
  };
194
+ type CreditEvent = {
195
+ id: string;
196
+ ownerId: string;
197
+ type: "grant" | "debit" | "refund";
198
+ amountUsd: number;
199
+ balanceAfterUsd: number;
200
+ route?: string;
201
+ requestId?: string;
202
+ apiKeyId?: string;
203
+ note?: string;
204
+ createdAt: string;
205
+ };
206
+ type CreditsResponse = {
207
+ ownerId: string;
208
+ balanceUsd: number;
209
+ events: CreditEvent[];
210
+ };
194
211
  type X402PaymentHandler = (challenge: unknown, request: RequestInit & {
195
212
  url: string;
196
213
  }) => Promise<string>;
@@ -219,6 +236,8 @@ declare class ContextKit {
219
236
  compressedTokens: number;
220
237
  reductionPercent: number;
221
238
  }>;
239
+ credits(): Promise<CreditsResponse>;
240
+ private get;
222
241
  private post;
223
242
  }
224
243
  declare class ContextKitError extends Error {
@@ -253,4 +272,4 @@ declare function getFirstX402Requirement(challenge: X402Challenge): {
253
272
  };
254
273
  declare function encodeX402Payment(payment: unknown): string;
255
274
 
256
- export { type CompressContextResponse, ContextKit, ContextKitError, type ContextRequest, type ConversationMessage, type HandoffResponse, type MemoryEnrichmentResponse, type ProfileResponse, type SummarizeResponse, type X402PaymentHandler, encodeX402Payment, getFirstX402Requirement, verifyContextKitWebhook };
275
+ export { type CompressContextResponse, ContextKit, ContextKitError, type ContextRequest, type ConversationMessage, type CreditEvent, type CreditsResponse, type HandoffResponse, type MemoryEnrichmentResponse, type ProfileResponse, type SummarizeResponse, type X402PaymentHandler, encodeX402Payment, getFirstX402Requirement, verifyContextKitWebhook };
package/dist/index.d.ts CHANGED
@@ -191,6 +191,23 @@ type MemoryEnrichmentResponse = {
191
191
  deprecatedMemories: string[];
192
192
  confidence: number;
193
193
  };
194
+ type CreditEvent = {
195
+ id: string;
196
+ ownerId: string;
197
+ type: "grant" | "debit" | "refund";
198
+ amountUsd: number;
199
+ balanceAfterUsd: number;
200
+ route?: string;
201
+ requestId?: string;
202
+ apiKeyId?: string;
203
+ note?: string;
204
+ createdAt: string;
205
+ };
206
+ type CreditsResponse = {
207
+ ownerId: string;
208
+ balanceUsd: number;
209
+ events: CreditEvent[];
210
+ };
194
211
  type X402PaymentHandler = (challenge: unknown, request: RequestInit & {
195
212
  url: string;
196
213
  }) => Promise<string>;
@@ -219,6 +236,8 @@ declare class ContextKit {
219
236
  compressedTokens: number;
220
237
  reductionPercent: number;
221
238
  }>;
239
+ credits(): Promise<CreditsResponse>;
240
+ private get;
222
241
  private post;
223
242
  }
224
243
  declare class ContextKitError extends Error {
@@ -253,4 +272,4 @@ declare function getFirstX402Requirement(challenge: X402Challenge): {
253
272
  };
254
273
  declare function encodeX402Payment(payment: unknown): string;
255
274
 
256
- export { type CompressContextResponse, ContextKit, ContextKitError, type ContextRequest, type ConversationMessage, type HandoffResponse, type MemoryEnrichmentResponse, type ProfileResponse, type SummarizeResponse, type X402PaymentHandler, encodeX402Payment, getFirstX402Requirement, verifyContextKitWebhook };
275
+ export { type CompressContextResponse, ContextKit, ContextKitError, type ContextRequest, type ConversationMessage, type CreditEvent, type CreditsResponse, type HandoffResponse, type MemoryEnrichmentResponse, type ProfileResponse, type SummarizeResponse, type X402PaymentHandler, encodeX402Payment, getFirstX402Requirement, verifyContextKitWebhook };
package/dist/index.js CHANGED
@@ -30,6 +30,20 @@ var ContextKit = class {
30
30
  async estimateTokens(input) {
31
31
  return this.post("/api/tokens/estimate", input);
32
32
  }
33
+ credits() {
34
+ return this.get("/api/auth/credits");
35
+ }
36
+ async get(path) {
37
+ const response = await this.fetcher(`${this.baseUrl.replace(/\/$/, "")}${path}`, {
38
+ headers: {
39
+ Authorization: `Bearer ${this.options.apiKey}`
40
+ }
41
+ });
42
+ if (response.ok) {
43
+ return await response.json();
44
+ }
45
+ throw new ContextKitError(response.status, await response.text());
46
+ }
33
47
  async post(path, body) {
34
48
  const url = `${this.baseUrl.replace(/\/$/, "")}${path}`;
35
49
  const init = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basedchef/contextkit",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "TypeScript SDK for ContextKit, a Bankr-hosted x402 context API for AI agents, memory compression, handoffs, profiles, and webhooks.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",