the-token-company 0.2.1 → 0.2.5
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/dist/ai-sdk.js +3 -3
- package/dist/anthropic.js +4 -4
- package/dist/client.d.ts +1 -0
- package/dist/client.js +3 -1
- package/dist/compress.d.ts +1 -1
- package/dist/compress.js +1 -1
- package/dist/openai.js +3 -3
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/ai-sdk.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { wrapLanguageModel } from "ai";
|
|
2
2
|
import { TheTokenCompany } from "./client.js";
|
|
3
|
-
import {
|
|
3
|
+
import { StatsTTC, compressAISDKPrompt, resolveAggressiveness } from "./compress.js";
|
|
4
4
|
import { CompressionStats } from "./types.js";
|
|
5
5
|
/**
|
|
6
6
|
* Create a Vercel AI SDK middleware that auto-compresses non-assistant messages.
|
|
@@ -17,7 +17,7 @@ import { CompressionStats } from "./types.js";
|
|
|
17
17
|
*/
|
|
18
18
|
export function compressionMiddleware(options) {
|
|
19
19
|
const stats = new CompressionStats();
|
|
20
|
-
const
|
|
20
|
+
const compressor = new StatsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId, fetch: options.fetch }), stats);
|
|
21
21
|
const compressionModel = options.model ?? "bear-2";
|
|
22
22
|
const roleAggr = resolveAggressiveness(options.aggressiveness ?? 0.2);
|
|
23
23
|
const middleware = {
|
|
@@ -26,7 +26,7 @@ export function compressionMiddleware(options) {
|
|
|
26
26
|
transformParams: async ({ params }) => {
|
|
27
27
|
if (params.prompt) {
|
|
28
28
|
stats._startTurn();
|
|
29
|
-
const compressed = await compressAISDKPrompt(
|
|
29
|
+
const compressed = await compressAISDKPrompt(compressor, params.prompt, compressionModel, roleAggr);
|
|
30
30
|
stats._endTurn();
|
|
31
31
|
return { ...params, prompt: compressed };
|
|
32
32
|
}
|
package/dist/anthropic.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TheTokenCompany } from "./client.js";
|
|
2
|
-
import {
|
|
2
|
+
import { StatsTTC, compressAnthropicMessages, resolveAggressiveness } from "./compress.js";
|
|
3
3
|
import { CompressionStats } from "./types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Wrap an Anthropic client to auto-compress non-assistant messages.
|
|
@@ -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
|
|
27
|
+
const compressor = new StatsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId, fetch: options.fetch }), stats);
|
|
28
28
|
const model = options.model ?? "bear-2";
|
|
29
29
|
const roleAggr = resolveAggressiveness(options.aggressiveness ?? 0.2);
|
|
30
30
|
const systemAggr = roleAggr["system"];
|
|
@@ -34,11 +34,11 @@ export function withCompression(client, options) {
|
|
|
34
34
|
if (params?.messages) {
|
|
35
35
|
params = {
|
|
36
36
|
...params,
|
|
37
|
-
messages: await compressAnthropicMessages(
|
|
37
|
+
messages: await compressAnthropicMessages(compressor, params.messages, model, roleAggr),
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
if (systemAggr != null && typeof params?.system === "string" && params.system.trim()) {
|
|
41
|
-
const result = await
|
|
41
|
+
const result = await compressor.compress(params.system, { model, aggressiveness: systemAggr });
|
|
42
42
|
params = { ...params, system: result.output };
|
|
43
43
|
}
|
|
44
44
|
stats._endTurn();
|
package/dist/client.d.ts
CHANGED
package/dist/client.js
CHANGED
|
@@ -14,12 +14,14 @@ export class TheTokenCompany {
|
|
|
14
14
|
timeout;
|
|
15
15
|
gzip;
|
|
16
16
|
appId;
|
|
17
|
+
fetchFn;
|
|
17
18
|
constructor(options) {
|
|
18
19
|
this.apiKey = options.apiKey;
|
|
19
20
|
this.baseUrl = (options.baseUrl ?? BASE_URL).replace(/\/$/, "");
|
|
20
21
|
this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
21
22
|
this.gzip = options.gzip ?? true;
|
|
22
23
|
this.appId = options.appId;
|
|
24
|
+
this.fetchFn = options.fetch ?? globalThis.fetch;
|
|
23
25
|
}
|
|
24
26
|
async compress(text, options = {}) {
|
|
25
27
|
const { model = "bear-2", aggressiveness = 0.2 } = options;
|
|
@@ -49,7 +51,7 @@ export class TheTokenCompany {
|
|
|
49
51
|
else {
|
|
50
52
|
body = jsonBody;
|
|
51
53
|
}
|
|
52
|
-
const response = await
|
|
54
|
+
const response = await this.fetchFn(`${this.baseUrl}/v1/compress`, {
|
|
53
55
|
method: "POST",
|
|
54
56
|
headers,
|
|
55
57
|
body: body,
|
package/dist/compress.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface Compressor {
|
|
|
7
7
|
aggressiveness?: number;
|
|
8
8
|
}): Promise<CompressResult>;
|
|
9
9
|
}
|
|
10
|
-
export declare class
|
|
10
|
+
export declare class StatsTTC implements Compressor {
|
|
11
11
|
private readonly inner;
|
|
12
12
|
readonly stats: CompressionStats;
|
|
13
13
|
constructor(inner: TheTokenCompany, stats: CompressionStats);
|
package/dist/compress.js
CHANGED
package/dist/openai.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TheTokenCompany } from "./client.js";
|
|
2
|
-
import {
|
|
2
|
+
import { StatsTTC, compressOpenAIMessages, resolveAggressiveness } from "./compress.js";
|
|
3
3
|
import { CompressionStats } from "./types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Wrap an OpenAI-compatible client to auto-compress non-assistant messages.
|
|
@@ -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
|
|
25
|
+
const compressor = new StatsTTC(new TheTokenCompany({ apiKey: options.compressionApiKey, appId: options.appId, fetch: options.fetch }), 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);
|
|
@@ -31,7 +31,7 @@ export function withCompression(client, options) {
|
|
|
31
31
|
stats._startTurn();
|
|
32
32
|
params = {
|
|
33
33
|
...params,
|
|
34
|
-
messages: await compressOpenAIMessages(
|
|
34
|
+
messages: await compressOpenAIMessages(compressor, params.messages, model, roleAggr),
|
|
35
35
|
};
|
|
36
36
|
stats._endTurn();
|
|
37
37
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface TheTokenCompanyOptions {
|
|
|
24
24
|
timeout?: number;
|
|
25
25
|
gzip?: boolean;
|
|
26
26
|
appId?: string;
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
27
28
|
}
|
|
28
29
|
export type Aggressiveness = number | Record<string, number>;
|
|
29
30
|
export interface WithCompressionOptions {
|
|
@@ -31,6 +32,7 @@ export interface WithCompressionOptions {
|
|
|
31
32
|
model?: string;
|
|
32
33
|
aggressiveness?: Aggressiveness;
|
|
33
34
|
appId?: string;
|
|
35
|
+
fetch?: typeof globalThis.fetch;
|
|
34
36
|
}
|
|
35
37
|
export interface TurnStats {
|
|
36
38
|
inputTokens: number;
|