@revojs/ai 0.2.22 → 0.2.24
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/index.d.ts
CHANGED
|
@@ -761,6 +761,7 @@ export declare function useModelsDev(id: string, resolve: (model: ModelsDevModel
|
|
|
761
761
|
textStream: Record<string, Protocol<LanguageModelCallOptions<string>, LanguageModelStreamResult>>;
|
|
762
762
|
}>;
|
|
763
763
|
export declare function modelsDevProtocol(model: ModelsDevModel, provider: ModelsDevProvider): ModelsDevProtocol | undefined;
|
|
764
|
+
export declare function toToolInput(value: string | undefined): string;
|
|
764
765
|
export declare function mergeHeaders(...headers: Array<Record<string, string | undefined> | undefined>): Record<string, string>;
|
|
765
766
|
export declare function postJson(url: string, body: unknown, options?: {
|
|
766
767
|
headers?: Record<string, string | undefined>;
|
package/dist/index.js
CHANGED
|
@@ -371,6 +371,9 @@ function modelsDevProtocol(model, provider) {
|
|
|
371
371
|
if (npm === "@ai-sdk/openai") return model.provider?.shape === "completions" ? "chat-completions" : "responses";
|
|
372
372
|
if (npm === "@ai-sdk/openai-compatible") return "chat-completions";
|
|
373
373
|
}
|
|
374
|
+
function toToolInput(value) {
|
|
375
|
+
return value === void 0 || value.trim() === "" ? "{}" : value;
|
|
376
|
+
}
|
|
374
377
|
function mergeHeaders(...headers) {
|
|
375
378
|
const merged = {};
|
|
376
379
|
for (const record of headers) for (const [name, value] of Object.entries(record ?? {})) if (value !== void 0) merged[name] = value;
|
|
@@ -421,4 +424,4 @@ function toStream(read) {
|
|
|
421
424
|
}
|
|
422
425
|
var modelsDevCache = { loadedAt: 0 };
|
|
423
426
|
//#endregion
|
|
424
|
-
export { ModelContextProtocolConnection, defineProvider, defineTool, handleModelContextProtocol, mergeHeaders, modelsDevProtocol, postJson, readDataFrames, toStream, useAi, useModelContextProtocol, useModelsDev, useTools };
|
|
427
|
+
export { ModelContextProtocolConnection, defineProvider, defineTool, handleModelContextProtocol, mergeHeaders, modelsDevProtocol, postJson, readDataFrames, toStream, toToolInput, useAi, useModelContextProtocol, useModelsDev, useTools };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mergeHeaders, postJson, readDataFrames, toStream } from "../index.js";
|
|
1
|
+
import { mergeHeaders, postJson, readDataFrames, toStream, toToolInput } from "../index.js";
|
|
2
2
|
import { toJsonSchema } from "revojs/shared";
|
|
3
3
|
//#region src/protocols/anthropic-messages.ts
|
|
4
4
|
function toUsage(inputTokens, cacheRead, cacheWrite, outputTokens) {
|
|
@@ -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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
} : {
|
|
162
|
-
|
|
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
|
|
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,
|
|
@@ -299,7 +315,7 @@ async function* readParts(response, warnings) {
|
|
|
299
315
|
type: "tool-call",
|
|
300
316
|
toolCallId: record.callId,
|
|
301
317
|
toolName: record.name,
|
|
302
|
-
input: record.json
|
|
318
|
+
input: toToolInput(record.json)
|
|
303
319
|
};
|
|
304
320
|
yield {
|
|
305
321
|
type: "finish",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mergeHeaders, postJson, readDataFrames, toStream } from "../index.js";
|
|
1
|
+
import { mergeHeaders, postJson, readDataFrames, toStream, toToolInput } from "../index.js";
|
|
2
2
|
import { toJsonSchema } from "revojs/shared";
|
|
3
3
|
//#region src/protocols/openai-chat-completions.ts
|
|
4
4
|
function toMessages(prompt) {
|
|
@@ -256,7 +256,7 @@ async function* readParts(response, warnings) {
|
|
|
256
256
|
type: "tool-call",
|
|
257
257
|
toolCallId: accumulator.callId.length > 0 ? accumulator.callId : accumulator.streamId,
|
|
258
258
|
toolName: accumulator.name,
|
|
259
|
-
input: accumulator.arguments
|
|
259
|
+
input: toToolInput(accumulator.arguments)
|
|
260
260
|
};
|
|
261
261
|
}
|
|
262
262
|
yield {
|
|
@@ -289,7 +289,7 @@ function chatCompletionsGenerateProtocol(config) {
|
|
|
289
289
|
type: "tool-call",
|
|
290
290
|
toolCallId: call.id ?? "",
|
|
291
291
|
toolName: call.function?.name ?? "",
|
|
292
|
-
input: call.function?.arguments
|
|
292
|
+
input: toToolInput(call.function?.arguments)
|
|
293
293
|
});
|
|
294
294
|
return {
|
|
295
295
|
content,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mergeHeaders, postJson, readDataFrames, toStream } from "../index.js";
|
|
1
|
+
import { mergeHeaders, postJson, readDataFrames, toStream, toToolInput } from "../index.js";
|
|
2
2
|
import { toJsonSchema } from "revojs/shared";
|
|
3
3
|
//#region src/protocols/openai-responses.ts
|
|
4
4
|
function toUsage(usage) {
|
|
@@ -276,7 +276,7 @@ async function* readParts(response, warnings) {
|
|
|
276
276
|
type: "tool-call",
|
|
277
277
|
toolCallId: event.item.call_id ?? streamId,
|
|
278
278
|
toolName: event.item.name ?? record?.name ?? "",
|
|
279
|
-
input: event.item.arguments ?? record?.arguments
|
|
279
|
+
input: toToolInput(event.item.arguments ?? record?.arguments)
|
|
280
280
|
};
|
|
281
281
|
continue;
|
|
282
282
|
}
|
|
@@ -318,7 +318,7 @@ async function* readParts(response, warnings) {
|
|
|
318
318
|
type: "tool-call",
|
|
319
319
|
toolCallId: record.callId || id,
|
|
320
320
|
toolName: record.name,
|
|
321
|
-
input: record.arguments
|
|
321
|
+
input: toToolInput(record.arguments)
|
|
322
322
|
};
|
|
323
323
|
}
|
|
324
324
|
yield {
|
|
@@ -359,7 +359,7 @@ function responsesGenerateProtocol(config) {
|
|
|
359
359
|
type: "tool-call",
|
|
360
360
|
toolCallId: item.call_id ?? "",
|
|
361
361
|
toolName: item.name ?? "",
|
|
362
|
-
input: item.arguments
|
|
362
|
+
input: toToolInput(item.arguments)
|
|
363
363
|
});
|
|
364
364
|
continue;
|
|
365
365
|
}
|