@revojs/ai 0.2.23 → 0.2.25

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.
@@ -9,8 +9,8 @@ function toUsage(inputTokens, cacheRead, cacheWrite, outputTokens) {
9
9
  if (outputTokens !== void 0) raw.output_tokens = outputTokens;
10
10
  return {
11
11
  inputTokens: {
12
- total: inputTokens,
13
- noCache: inputTokens !== void 0 && cacheRead !== void 0 && cacheWrite !== void 0 ? Math.max(0, inputTokens - cacheRead - cacheWrite) : void 0,
12
+ total: inputTokens === void 0 ? void 0 : inputTokens + (cacheRead ?? 0) + (cacheWrite ?? 0),
13
+ noCache: inputTokens,
14
14
  cacheRead,
15
15
  cacheWrite
16
16
  },
@@ -145,6 +145,24 @@ function toBody(options, stream) {
145
145
  });
146
146
  }
147
147
  flushUser();
148
+ const cache = options.providerOptions?.messages?.cache !== false;
149
+ const tools = [];
150
+ const lastContent = wire.at(-1)?.content;
151
+ for (const tool of options.tools ?? []) if (tool.type === "provider") tools.push({
152
+ type: "custom",
153
+ name: `${tool.id}.${tool.name}`,
154
+ ...tool.args
155
+ });
156
+ else tools.push({
157
+ name: tool.name,
158
+ description: tool.description,
159
+ input_schema: toJsonSchema(tool.inputSchema)
160
+ });
161
+ if (cache && tools.length > 0) tools[tools.length - 1].cache_control = { type: "ephemeral" };
162
+ if (cache && Array.isArray(lastContent)) {
163
+ const lastBlock = lastContent.at(-1);
164
+ if (lastBlock !== void 0 && lastBlock.type !== "thinking") lastBlock.cache_control = { type: "ephemeral" };
165
+ }
148
166
  return [{
149
167
  model: options.model,
150
168
  max_tokens: options.maxOutputTokens ?? 8192,
@@ -153,16 +171,13 @@ function toBody(options, stream) {
153
171
  ...options.topP !== void 0 ? { top_p: options.topP } : {},
154
172
  ...options.topK !== void 0 ? { top_k: options.topK } : {},
155
173
  ...options.stopSequences !== void 0 ? { stop_sequences: options.stopSequences } : {},
156
- ...system.length > 0 ? { system } : {},
157
- ...options.tools !== void 0 && options.tools.length > 0 ? { tools: options.tools.map((tool) => tool.type === "provider" ? {
158
- type: "custom",
159
- name: `${tool.id}.${tool.name}`,
160
- ...tool.args
161
- } : {
162
- name: tool.name,
163
- description: tool.description,
164
- input_schema: toJsonSchema(tool.inputSchema)
165
- }) } : {},
174
+ ...system.length > 0 && cache ? { system: [{
175
+ type: "text",
176
+ text: system,
177
+ cache_control: { type: "ephemeral" }
178
+ }] } : {},
179
+ ...system.length > 0 && !cache ? { system } : {},
180
+ ...tools.length > 0 ? { tools } : {},
166
181
  ...options.toolChoice !== void 0 ? { tool_choice: options.toolChoice.type === "tool" ? {
167
182
  type: "tool",
168
183
  name: options.toolChoice.toolName
@@ -210,7 +225,8 @@ async function* readParts(response, warnings) {
210
225
  if (chunk.type === "content_block_start") {
211
226
  const index = chunk.index ?? 0;
212
227
  const kind = chunk.content_block?.type ?? "";
213
- const streamId = `anthropic-${kind}-${streamCount++}`;
228
+ const toolCallId = kind === "tool_use" ? chunk.content_block?.id ?? "" : "";
229
+ const streamId = toolCallId.length > 0 ? toolCallId : `anthropic-${kind}-${streamCount++}`;
214
230
  const record = {
215
231
  index,
216
232
  kind,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revojs/ai",
3
- "version": "0.2.23",
3
+ "version": "0.2.25",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",