the-token-company 0.1.0 → 0.2.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 +28 -0
- package/dist/ai-sdk.d.ts +6 -10
- package/dist/ai-sdk.js +11 -13
- package/dist/anthropic.js +1 -1
- package/dist/client.d.ts +2 -0
- package/dist/client.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/openai.js +1 -1
- package/dist/types.d.ts +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# The Token Company Node.js SDK
|
|
2
4
|
|
|
3
5
|
Compress LLM prompts to reduce costs and latency. 100K tokens compressed in ~85ms.
|
|
4
6
|
|
|
7
|
+
[](https://github.com/TheTokenCompany/the-token-company-node/actions/workflows/ci.yml)
|
|
8
|
+
[](https://www.npmjs.com/package/the-token-company)
|
|
9
|
+
[](https://github.com/TheTokenCompany/the-token-company-node/blob/main/LICENSE)
|
|
10
|
+
|
|
11
|
+
[Docs](https://thetokencompany.com/docs) · [Website](https://thetokencompany.com) · [Dashboard](https://app.thetokencompany.com) · [Python SDK](https://github.com/TheTokenCompany/the-token-company-python)
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
5
15
|
## Install
|
|
6
16
|
|
|
7
17
|
```bash
|
|
@@ -131,6 +141,24 @@ withCompression(client, {
|
|
|
131
141
|
| `system` | `role: "system"` messages | `system` parameter | System messages |
|
|
132
142
|
| `tool` | `tool` + `function` messages | `tool_result` content blocks | Tool result parts |
|
|
133
143
|
|
|
144
|
+
## App ID
|
|
145
|
+
|
|
146
|
+
Tag compression requests with an application identifier for usage tracking:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
// Set on the client — applies to all requests
|
|
150
|
+
const client = new TheTokenCompany({ apiKey: "ttc-...", appId: "my-chatbot" });
|
|
151
|
+
|
|
152
|
+
// Or per-request (overrides the client-level value)
|
|
153
|
+
const result = await client.compress(text, { model: "bear-2", appId: "my-chatbot" });
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Also supported in wrappers:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const client = withCompression(new OpenAI(), { compressionApiKey: "ttc-...", appId: "my-chatbot" });
|
|
160
|
+
```
|
|
161
|
+
|
|
134
162
|
## Gzip
|
|
135
163
|
|
|
136
164
|
Gzip compression of request payloads is on by default. Disable with:
|
package/dist/ai-sdk.d.ts
CHANGED
|
@@ -5,20 +5,17 @@ import { CompressionStats } from "./types.js";
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a Vercel AI SDK middleware that auto-compresses non-assistant messages.
|
|
7
7
|
*
|
|
8
|
-
* Use when composing with other middleware:
|
|
9
|
-
*
|
|
10
8
|
* ```ts
|
|
11
9
|
* import { wrapLanguageModel } from "ai";
|
|
12
10
|
* import { openai } from "@ai-sdk/openai";
|
|
13
11
|
* import { compressionMiddleware } from "the-token-company/ai-sdk";
|
|
14
12
|
*
|
|
15
|
-
* const
|
|
13
|
+
* const middleware = compressionMiddleware({ compressionApiKey: "ttc-..." });
|
|
16
14
|
* const model = wrapLanguageModel({ model: openai("gpt-4o"), middleware });
|
|
17
|
-
* // After calls: compression.totalTokensSaved
|
|
15
|
+
* // After calls: middleware.compression.totalTokensSaved
|
|
18
16
|
* ```
|
|
19
17
|
*/
|
|
20
|
-
export declare function compressionMiddleware(options: WithCompressionOptions): {
|
|
21
|
-
middleware: LanguageModelMiddleware;
|
|
18
|
+
export declare function compressionMiddleware(options: WithCompressionOptions): LanguageModelMiddleware & {
|
|
22
19
|
compression: CompressionStats;
|
|
23
20
|
};
|
|
24
21
|
/**
|
|
@@ -29,18 +26,17 @@ export declare function compressionMiddleware(options: WithCompressionOptions):
|
|
|
29
26
|
* import { generateText } from "ai";
|
|
30
27
|
* import { withCompression } from "the-token-company/ai-sdk";
|
|
31
28
|
*
|
|
32
|
-
* const
|
|
29
|
+
* const model = withCompression(openai("gpt-4o"), { compressionApiKey: "ttc-..." });
|
|
33
30
|
*
|
|
34
31
|
* const { text } = await generateText({
|
|
35
32
|
* model,
|
|
36
33
|
* messages: [{ role: "user", content: "Summarize these results..." }],
|
|
37
34
|
* });
|
|
38
|
-
* console.log(compression.totalTokensSaved);
|
|
35
|
+
* console.log(model.compression.totalTokensSaved);
|
|
39
36
|
* ```
|
|
40
37
|
*
|
|
41
38
|
* Works with any provider (`@ai-sdk/openai`, `@ai-sdk/anthropic`, `@ai-sdk/google`, etc.).
|
|
42
39
|
*/
|
|
43
|
-
export declare function withCompression(model: LanguageModelV3, options: WithCompressionOptions): {
|
|
44
|
-
model: LanguageModelV3;
|
|
40
|
+
export declare function withCompression(model: LanguageModelV3, options: WithCompressionOptions): LanguageModelV3 & {
|
|
45
41
|
compression: CompressionStats;
|
|
46
42
|
};
|
package/dist/ai-sdk.js
CHANGED
|
@@ -5,25 +5,24 @@ import { CompressionStats } from "./types.js";
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a Vercel AI SDK middleware that auto-compresses non-assistant messages.
|
|
7
7
|
*
|
|
8
|
-
* Use when composing with other middleware:
|
|
9
|
-
*
|
|
10
8
|
* ```ts
|
|
11
9
|
* import { wrapLanguageModel } from "ai";
|
|
12
10
|
* import { openai } from "@ai-sdk/openai";
|
|
13
11
|
* import { compressionMiddleware } from "the-token-company/ai-sdk";
|
|
14
12
|
*
|
|
15
|
-
* const
|
|
13
|
+
* const middleware = compressionMiddleware({ compressionApiKey: "ttc-..." });
|
|
16
14
|
* const model = wrapLanguageModel({ model: openai("gpt-4o"), middleware });
|
|
17
|
-
* // After calls: compression.totalTokensSaved
|
|
15
|
+
* // After calls: middleware.compression.totalTokensSaved
|
|
18
16
|
* ```
|
|
19
17
|
*/
|
|
20
18
|
export function compressionMiddleware(options) {
|
|
21
19
|
const stats = new CompressionStats();
|
|
22
|
-
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey }), stats);
|
|
20
|
+
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId }), stats);
|
|
23
21
|
const compressionModel = options.model ?? "bear-2";
|
|
24
22
|
const roleAggr = resolveAggressiveness(options.aggressiveness ?? 0.2);
|
|
25
23
|
const middleware = {
|
|
26
24
|
specificationVersion: "v3",
|
|
25
|
+
compression: stats,
|
|
27
26
|
transformParams: async ({ params }) => {
|
|
28
27
|
if (params.prompt) {
|
|
29
28
|
stats._startTurn();
|
|
@@ -34,7 +33,7 @@ export function compressionMiddleware(options) {
|
|
|
34
33
|
return params;
|
|
35
34
|
},
|
|
36
35
|
};
|
|
37
|
-
return
|
|
36
|
+
return middleware;
|
|
38
37
|
}
|
|
39
38
|
/**
|
|
40
39
|
* Wrap any Vercel AI SDK language model with automatic compression.
|
|
@@ -44,21 +43,20 @@ export function compressionMiddleware(options) {
|
|
|
44
43
|
* import { generateText } from "ai";
|
|
45
44
|
* import { withCompression } from "the-token-company/ai-sdk";
|
|
46
45
|
*
|
|
47
|
-
* const
|
|
46
|
+
* const model = withCompression(openai("gpt-4o"), { compressionApiKey: "ttc-..." });
|
|
48
47
|
*
|
|
49
48
|
* const { text } = await generateText({
|
|
50
49
|
* model,
|
|
51
50
|
* messages: [{ role: "user", content: "Summarize these results..." }],
|
|
52
51
|
* });
|
|
53
|
-
* console.log(compression.totalTokensSaved);
|
|
52
|
+
* console.log(model.compression.totalTokensSaved);
|
|
54
53
|
* ```
|
|
55
54
|
*
|
|
56
55
|
* Works with any provider (`@ai-sdk/openai`, `@ai-sdk/anthropic`, `@ai-sdk/google`, etc.).
|
|
57
56
|
*/
|
|
58
57
|
export function withCompression(model, options) {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
};
|
|
58
|
+
const mw = compressionMiddleware(options);
|
|
59
|
+
const wrapped = wrapLanguageModel({ model, middleware: mw });
|
|
60
|
+
wrapped.compression = mw.compression;
|
|
61
|
+
return wrapped;
|
|
64
62
|
}
|
package/dist/anthropic.js
CHANGED
|
@@ -24,7 +24,7 @@ import { CompressionStats } from "./types.js";
|
|
|
24
24
|
*/
|
|
25
25
|
export function withCompression(client, options) {
|
|
26
26
|
const stats = new CompressionStats();
|
|
27
|
-
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey }), stats);
|
|
27
|
+
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId }), stats);
|
|
28
28
|
const model = options.model ?? "bear-2";
|
|
29
29
|
const roleAggr = resolveAggressiveness(options.aggressiveness ?? 0.2);
|
|
30
30
|
const systemAggr = roleAggr["system"];
|
package/dist/client.d.ts
CHANGED
|
@@ -8,10 +8,12 @@ export declare class TheTokenCompany {
|
|
|
8
8
|
private readonly apiKey;
|
|
9
9
|
private readonly timeout;
|
|
10
10
|
private readonly gzip;
|
|
11
|
+
private readonly appId?;
|
|
11
12
|
constructor(options: TheTokenCompanyOptions);
|
|
12
13
|
compress(text: string, options?: {
|
|
13
14
|
model?: string;
|
|
14
15
|
aggressiveness?: number;
|
|
16
|
+
appId?: string;
|
|
15
17
|
}): Promise<CompressResult>;
|
|
16
18
|
private parseError;
|
|
17
19
|
}
|
package/dist/client.js
CHANGED
|
@@ -13,11 +13,13 @@ export class TheTokenCompany {
|
|
|
13
13
|
apiKey;
|
|
14
14
|
timeout;
|
|
15
15
|
gzip;
|
|
16
|
+
appId;
|
|
16
17
|
constructor(options) {
|
|
17
18
|
this.apiKey = options.apiKey;
|
|
18
19
|
this.baseUrl = (options.baseUrl ?? BASE_URL).replace(/\/$/, "");
|
|
19
20
|
this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
20
21
|
this.gzip = options.gzip ?? true;
|
|
22
|
+
this.appId = options.appId;
|
|
21
23
|
}
|
|
22
24
|
async compress(text, options = {}) {
|
|
23
25
|
const { model = "bear-2", aggressiveness = 0.2 } = options;
|
|
@@ -27,10 +29,12 @@ export class TheTokenCompany {
|
|
|
27
29
|
if (aggressiveness < 0 || aggressiveness > 1) {
|
|
28
30
|
throw new InvalidRequestError("aggressiveness must be between 0.0 and 1.0");
|
|
29
31
|
}
|
|
32
|
+
const resolvedAppId = options.appId ?? this.appId;
|
|
30
33
|
const payload = {
|
|
31
34
|
model,
|
|
32
35
|
input: text,
|
|
33
36
|
compression_settings: { aggressiveness },
|
|
37
|
+
...(resolvedAppId !== undefined && { app_id: resolvedAppId }),
|
|
34
38
|
};
|
|
35
39
|
const jsonBody = JSON.stringify(payload);
|
|
36
40
|
const headers = {
|
package/dist/index.d.ts
CHANGED
|
@@ -2,3 +2,7 @@ export { TheTokenCompany, BEAR_1, BEAR_1_1, BEAR_1_2, BEAR_2 } from "./client.js
|
|
|
2
2
|
export { TheTokenCompanyError, AuthenticationError, InvalidRequestError, PaymentRequiredError, RequestTooLargeError, RateLimitError, APIError, } from "./errors.js";
|
|
3
3
|
export { CompressionStats } from "./types.js";
|
|
4
4
|
export type { CompressResult, TheTokenCompanyOptions, Aggressiveness, WithCompressionOptions, TurnStats, } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Wrap text in `<ttc_safe>` tags to protect it from compression.
|
|
7
|
+
*/
|
|
8
|
+
export declare function protect(text: string): string;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
export { TheTokenCompany, BEAR_1, BEAR_1_1, BEAR_1_2, BEAR_2 } from "./client.js";
|
|
2
2
|
export { TheTokenCompanyError, AuthenticationError, InvalidRequestError, PaymentRequiredError, RequestTooLargeError, RateLimitError, APIError, } from "./errors.js";
|
|
3
3
|
export { CompressionStats } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Wrap text in `<ttc_safe>` tags to protect it from compression.
|
|
6
|
+
*/
|
|
7
|
+
export function protect(text) {
|
|
8
|
+
return `<ttc_safe>${text}</ttc_safe>`;
|
|
9
|
+
}
|
package/dist/openai.js
CHANGED
|
@@ -22,7 +22,7 @@ import { CompressionStats } from "./types.js";
|
|
|
22
22
|
*/
|
|
23
23
|
export function withCompression(client, options) {
|
|
24
24
|
const stats = new CompressionStats();
|
|
25
|
-
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey }), stats);
|
|
25
|
+
const analytics = new AnalyticsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId }), stats);
|
|
26
26
|
const model = options.model ?? "bear-2";
|
|
27
27
|
const roleAggr = resolveAggressiveness(options.aggressiveness ?? 0.2);
|
|
28
28
|
const originalCreate = client.chat.completions.create.bind(client.chat.completions);
|
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface CompressRequest {
|
|
|
4
4
|
compression_settings: {
|
|
5
5
|
aggressiveness: number;
|
|
6
6
|
};
|
|
7
|
+
app_id?: string;
|
|
7
8
|
}
|
|
8
9
|
export interface CompressResponse {
|
|
9
10
|
output: string;
|
|
@@ -22,12 +23,14 @@ export interface TheTokenCompanyOptions {
|
|
|
22
23
|
baseUrl?: string;
|
|
23
24
|
timeout?: number;
|
|
24
25
|
gzip?: boolean;
|
|
26
|
+
appId?: string;
|
|
25
27
|
}
|
|
26
28
|
export type Aggressiveness = number | Record<string, number>;
|
|
27
29
|
export interface WithCompressionOptions {
|
|
28
30
|
compressionApiKey: string;
|
|
29
31
|
model?: string;
|
|
30
32
|
aggressiveness?: Aggressiveness;
|
|
33
|
+
appId?: string;
|
|
31
34
|
}
|
|
32
35
|
export interface TurnStats {
|
|
33
36
|
inputTokens: number;
|