the-token-company 0.3.0 → 0.3.1
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/anthropic.js +4 -1
- package/dist/compress.d.ts +2 -0
- package/dist/compress.js +18 -2
- package/package.json +1 -1
package/dist/anthropic.js
CHANGED
|
@@ -101,7 +101,10 @@ export function withCompression(client, options) {
|
|
|
101
101
|
if (params?.messages) {
|
|
102
102
|
params = {
|
|
103
103
|
...params,
|
|
104
|
-
messages: await compressAnthropicMessages(compressor, params.messages, model, roleAggr, {
|
|
104
|
+
messages: await compressAnthropicMessages(compressor, params.messages, model, roleAggr, {
|
|
105
|
+
stripServerToolResults,
|
|
106
|
+
skipToolName: webSearch ? "ttc_web_search" : undefined,
|
|
107
|
+
}),
|
|
105
108
|
};
|
|
106
109
|
}
|
|
107
110
|
if (systemAggr != null && typeof params?.system === "string" && params.system.trim()) {
|
package/dist/compress.d.ts
CHANGED
|
@@ -30,6 +30,7 @@ export declare function compressOpenAIMessages(ttc: Compressor, messages: OpenAI
|
|
|
30
30
|
interface AnthropicBlock {
|
|
31
31
|
type: string;
|
|
32
32
|
text?: string;
|
|
33
|
+
tool_use_id?: string;
|
|
33
34
|
content?: string | Array<{
|
|
34
35
|
type: string;
|
|
35
36
|
text?: string;
|
|
@@ -44,6 +45,7 @@ interface AnthropicMessage {
|
|
|
44
45
|
}
|
|
45
46
|
export declare function compressAnthropicMessages(ttc: Compressor, messages: AnthropicMessage[], model: string, roleAggr: Record<string, number>, options?: {
|
|
46
47
|
stripServerToolResults?: boolean;
|
|
48
|
+
skipToolName?: string;
|
|
47
49
|
}): Promise<AnthropicMessage[]>;
|
|
48
50
|
interface AISDKTextPart {
|
|
49
51
|
type: "text";
|
package/dist/compress.js
CHANGED
|
@@ -47,8 +47,22 @@ export async function compressOpenAIMessages(ttc, messages, model, roleAggr) {
|
|
|
47
47
|
return msg;
|
|
48
48
|
}));
|
|
49
49
|
}
|
|
50
|
+
function collectToolUseIds(messages, toolName) {
|
|
51
|
+
const ids = new Set();
|
|
52
|
+
for (const msg of messages) {
|
|
53
|
+
if (msg.role !== "assistant" || !Array.isArray(msg.content))
|
|
54
|
+
continue;
|
|
55
|
+
for (const block of msg.content) {
|
|
56
|
+
if (block.type === "tool_use" && block.name === toolName && block.id) {
|
|
57
|
+
ids.add(block.id);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return ids;
|
|
62
|
+
}
|
|
50
63
|
export async function compressAnthropicMessages(ttc, messages, model, roleAggr, options) {
|
|
51
64
|
const stripServerToolResults = options?.stripServerToolResults ?? false;
|
|
65
|
+
const skipIds = options?.skipToolName ? collectToolUseIds(messages, options.skipToolName) : undefined;
|
|
52
66
|
return Promise.all(messages.map(async (msg) => {
|
|
53
67
|
if (msg.role === "assistant") {
|
|
54
68
|
const assistantAggr = roleAggr["assistant"];
|
|
@@ -75,7 +89,7 @@ export async function compressAnthropicMessages(ttc, messages, model, roleAggr,
|
|
|
75
89
|
return { ...msg, content: result.output };
|
|
76
90
|
}
|
|
77
91
|
if (Array.isArray(msg.content)) {
|
|
78
|
-
const blocks = await compressAnthropicBlocks(ttc, msg.content, model, userAggr, toolAggr);
|
|
92
|
+
const blocks = await compressAnthropicBlocks(ttc, msg.content, model, userAggr, toolAggr, skipIds);
|
|
79
93
|
return { ...msg, content: blocks };
|
|
80
94
|
}
|
|
81
95
|
return msg;
|
|
@@ -95,13 +109,15 @@ async function compressAssistantBlocks(ttc, blocks, model, assistantAggr, stripS
|
|
|
95
109
|
const filtered = results.filter((b) => b != null);
|
|
96
110
|
return filtered.length > 0 ? filtered : blocks;
|
|
97
111
|
}
|
|
98
|
-
async function compressAnthropicBlocks(ttc, blocks, model, userAggr, toolAggr) {
|
|
112
|
+
async function compressAnthropicBlocks(ttc, blocks, model, userAggr, toolAggr, skipIds) {
|
|
99
113
|
return Promise.all(blocks.map(async (block) => {
|
|
100
114
|
if (block.type === "text" && userAggr != null && typeof block.text === "string" && block.text.trim()) {
|
|
101
115
|
const result = await ttc.compress(block.text, { model, aggressiveness: userAggr });
|
|
102
116
|
return { ...block, text: result.output };
|
|
103
117
|
}
|
|
104
118
|
if (block.type === "tool_result" && toolAggr != null) {
|
|
119
|
+
if (block.tool_use_id && skipIds?.has(block.tool_use_id))
|
|
120
|
+
return block;
|
|
105
121
|
return compressToolResult(ttc, block, model, toolAggr);
|
|
106
122
|
}
|
|
107
123
|
return block;
|