chore: update to ai sdk v5 beta (#1074)
This commit is contained in:
parent
7d8e71383f
commit
4c281fe09d
54 changed files with 1372 additions and 1060 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import type { CoreMessage } from 'ai';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
export const TEST_PROMPTS: Record<string, CoreMessage> = {
|
||||
export const TEST_PROMPTS: Record<string, ModelMessage> = {
|
||||
USER_SKY: {
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'Why is the sky blue?' }],
|
||||
|
|
@ -23,12 +23,13 @@ export const TEST_PROMPTS: Record<string, CoreMessage> = {
|
|||
role: 'user',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: 'Who painted this?',
|
||||
type: 'file',
|
||||
mediaType: '...',
|
||||
data: '...',
|
||||
},
|
||||
{
|
||||
type: 'image',
|
||||
image: '...',
|
||||
type: 'text',
|
||||
text: 'Who painted this?',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
|
@ -57,11 +58,14 @@ export const TEST_PROMPTS: Record<string, CoreMessage> = {
|
|||
type: 'tool-result',
|
||||
toolCallId: 'call_123',
|
||||
toolName: 'createDocument',
|
||||
result: {
|
||||
id: '3ca386a4-40c6-4630-8ed1-84cbd46cc7eb',
|
||||
title: 'Essay about Silicon Valley',
|
||||
kind: 'text',
|
||||
content: 'A document was created and is now visible to the user.',
|
||||
output: {
|
||||
type: 'json',
|
||||
value: {
|
||||
id: '3ca386a4-40c6-4630-8ed1-84cbd46cc7eb',
|
||||
title: 'Essay about Silicon Valley',
|
||||
kind: 'text',
|
||||
content: 'A document was created and is now visible to the user.',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
|
|
@ -82,57 +86,60 @@ export const TEST_PROMPTS: Record<string, CoreMessage> = {
|
|||
type: 'tool-result',
|
||||
toolCallId: 'call_456',
|
||||
toolName: 'getWeather',
|
||||
result: {
|
||||
latitude: 37.763283,
|
||||
longitude: -122.41286,
|
||||
generationtime_ms: 0.06449222564697266,
|
||||
utc_offset_seconds: -25200,
|
||||
timezone: 'America/Los_Angeles',
|
||||
timezone_abbreviation: 'GMT-7',
|
||||
elevation: 18,
|
||||
current_units: {
|
||||
time: 'iso8601',
|
||||
interval: 'seconds',
|
||||
temperature_2m: '°C',
|
||||
},
|
||||
current: {
|
||||
time: '2025-03-10T14:00',
|
||||
interval: 900,
|
||||
temperature_2m: 17,
|
||||
},
|
||||
daily_units: {
|
||||
time: 'iso8601',
|
||||
sunrise: 'iso8601',
|
||||
sunset: 'iso8601',
|
||||
},
|
||||
daily: {
|
||||
time: [
|
||||
'2025-03-10',
|
||||
'2025-03-11',
|
||||
'2025-03-12',
|
||||
'2025-03-13',
|
||||
'2025-03-14',
|
||||
'2025-03-15',
|
||||
'2025-03-16',
|
||||
],
|
||||
sunrise: [
|
||||
'2025-03-10T07:27',
|
||||
'2025-03-11T07:25',
|
||||
'2025-03-12T07:24',
|
||||
'2025-03-13T07:22',
|
||||
'2025-03-14T07:21',
|
||||
'2025-03-15T07:19',
|
||||
'2025-03-16T07:18',
|
||||
],
|
||||
sunset: [
|
||||
'2025-03-10T19:12',
|
||||
'2025-03-11T19:13',
|
||||
'2025-03-12T19:14',
|
||||
'2025-03-13T19:15',
|
||||
'2025-03-14T19:16',
|
||||
'2025-03-15T19:17',
|
||||
'2025-03-16T19:17',
|
||||
],
|
||||
output: {
|
||||
type: 'json',
|
||||
value: {
|
||||
latitude: 37.763283,
|
||||
longitude: -122.41286,
|
||||
generationtime_ms: 0.06449222564697266,
|
||||
utc_offset_seconds: -25200,
|
||||
timezone: 'America/Los_Angeles',
|
||||
timezone_abbreviation: 'GMT-7',
|
||||
elevation: 18,
|
||||
current_units: {
|
||||
time: 'iso8601',
|
||||
interval: 'seconds',
|
||||
temperature_2m: '°C',
|
||||
},
|
||||
current: {
|
||||
time: '2025-03-10T14:00',
|
||||
interval: 900,
|
||||
temperature_2m: 17,
|
||||
},
|
||||
daily_units: {
|
||||
time: 'iso8601',
|
||||
sunrise: 'iso8601',
|
||||
sunset: 'iso8601',
|
||||
},
|
||||
daily: {
|
||||
time: [
|
||||
'2025-03-10',
|
||||
'2025-03-11',
|
||||
'2025-03-12',
|
||||
'2025-03-13',
|
||||
'2025-03-14',
|
||||
'2025-03-15',
|
||||
'2025-03-16',
|
||||
],
|
||||
sunrise: [
|
||||
'2025-03-10T07:27',
|
||||
'2025-03-11T07:25',
|
||||
'2025-03-12T07:24',
|
||||
'2025-03-13T07:22',
|
||||
'2025-03-14T07:21',
|
||||
'2025-03-15T07:19',
|
||||
'2025-03-16T07:18',
|
||||
],
|
||||
sunset: [
|
||||
'2025-03-10T19:12',
|
||||
'2025-03-11T19:13',
|
||||
'2025-03-12T19:14',
|
||||
'2025-03-13T19:15',
|
||||
'2025-03-14T19:16',
|
||||
'2025-03-15T19:17',
|
||||
'2025-03-16T19:17',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -10,12 +10,16 @@ export const TEST_PROMPTS = {
|
|||
parts: [{ type: 'text', text: 'Why is the sky blue?' }],
|
||||
},
|
||||
OUTPUT_STREAM: [
|
||||
'0:"It\'s "',
|
||||
'0:"just "',
|
||||
'0:"blue "',
|
||||
'0:"duh! "',
|
||||
'e:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10},"isContinued":false}',
|
||||
'd:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10}}',
|
||||
'data: {"type":"start-step"}',
|
||||
'data: {"type":"text-start","id":"STATIC_ID"}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"It\'s "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"just "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"blue "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"duh! "}',
|
||||
'data: {"type":"text-end","id":"STATIC_ID"}',
|
||||
'data: {"type":"finish-step"}',
|
||||
'data: {"type":"finish"}',
|
||||
'data: [DONE]',
|
||||
],
|
||||
},
|
||||
GRASS: {
|
||||
|
|
@ -26,14 +30,17 @@ export const TEST_PROMPTS = {
|
|||
content: 'Why is grass green?',
|
||||
parts: [{ type: 'text', text: 'Why is grass green?' }],
|
||||
},
|
||||
|
||||
OUTPUT_STREAM: [
|
||||
'0:"It\'s "',
|
||||
'0:"just "',
|
||||
'0:"green "',
|
||||
'0:"duh! "',
|
||||
'e:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10},"isContinued":false}',
|
||||
'd:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10}}',
|
||||
'data: {"type":"start-step"}',
|
||||
'data: {"type":"text-start","id":"STATIC_ID"}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"It\'s "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"just "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"green "}',
|
||||
'data: {"type":"text-delta","id":"STATIC_ID","delta":"duh! "}',
|
||||
'data: {"type":"text-end","id":"STATIC_ID"}',
|
||||
'data: {"type":"finish-step"}',
|
||||
'data: {"type":"finish"}',
|
||||
'data: [DONE]',
|
||||
],
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { CoreMessage, LanguageModelV1StreamPart } from 'ai';
|
||||
import { generateId, type ModelMessage } from 'ai';
|
||||
import { TEST_PROMPTS } from './basic';
|
||||
import type { LanguageModelV2StreamPart } from '@ai-sdk/provider';
|
||||
|
||||
export function compareMessages(
|
||||
firstMessage: CoreMessage,
|
||||
secondMessage: CoreMessage,
|
||||
firstMessage: ModelMessage,
|
||||
secondMessage: ModelMessage,
|
||||
): boolean {
|
||||
if (firstMessage.role !== secondMessage.role) return false;
|
||||
|
||||
|
|
@ -24,7 +25,7 @@ export function compareMessages(
|
|||
|
||||
if (item1.type !== item2.type) return false;
|
||||
|
||||
if (item1.type === 'image' && item2.type === 'image') {
|
||||
if (item1.type === 'file' && item2.type === 'file') {
|
||||
// if (item1.image.toString() !== item2.image.toString()) return false;
|
||||
// if (item1.mimeType !== item2.mimeType) return false;
|
||||
} else if (item1.type === 'text' && item2.type === 'text') {
|
||||
|
|
@ -39,26 +40,38 @@ export function compareMessages(
|
|||
return true;
|
||||
}
|
||||
|
||||
const textToDeltas = (text: string): LanguageModelV1StreamPart[] => {
|
||||
const deltas = text
|
||||
.split(' ')
|
||||
.map((char) => ({ type: 'text-delta' as const, textDelta: `${char} ` }));
|
||||
const textToDeltas = (text: string): LanguageModelV2StreamPart[] => {
|
||||
const id = generateId();
|
||||
|
||||
return deltas;
|
||||
const deltas = text.split(' ').map((char) => ({
|
||||
id,
|
||||
type: 'text-delta' as const,
|
||||
delta: `${char} `,
|
||||
}));
|
||||
|
||||
return [{ id, type: 'text-start' }, ...deltas, { id, type: 'text-end' }];
|
||||
};
|
||||
|
||||
const reasoningToDeltas = (text: string): LanguageModelV1StreamPart[] => {
|
||||
const deltas = text
|
||||
.split(' ')
|
||||
.map((char) => ({ type: 'reasoning' as const, textDelta: `${char} ` }));
|
||||
const reasoningToDeltas = (text: string): LanguageModelV2StreamPart[] => {
|
||||
const id = generateId();
|
||||
|
||||
return deltas;
|
||||
const deltas = text.split(' ').map((char) => ({
|
||||
id,
|
||||
type: 'reasoning-delta' as const,
|
||||
delta: `${char} `,
|
||||
}));
|
||||
|
||||
return [
|
||||
{ id, type: 'reasoning-start' },
|
||||
...deltas,
|
||||
{ id, type: 'reasoning-end' },
|
||||
];
|
||||
};
|
||||
|
||||
export const getResponseChunksByPrompt = (
|
||||
prompt: CoreMessage[],
|
||||
isReasoningEnabled: boolean = false,
|
||||
): Array<LanguageModelV1StreamPart> => {
|
||||
prompt: ModelMessage[],
|
||||
isReasoningEnabled = false,
|
||||
): LanguageModelV2StreamPart[] => {
|
||||
const recentMessage = prompt.at(-1);
|
||||
|
||||
if (!recentMessage) {
|
||||
|
|
@ -73,8 +86,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_GRASS)) {
|
||||
|
|
@ -86,8 +98,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
}
|
||||
|
|
@ -99,8 +110,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_GRASS)) {
|
||||
|
|
@ -109,8 +119,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_SKY)) {
|
||||
|
|
@ -119,8 +128,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_NEXTJS)) {
|
||||
|
|
@ -130,8 +138,7 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (
|
||||
|
|
@ -142,27 +149,44 @@ export const getResponseChunksByPrompt = (
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_TEXT_ARTIFACT)) {
|
||||
const toolCallId = generateId();
|
||||
|
||||
return [
|
||||
{
|
||||
type: 'tool-call',
|
||||
toolCallId: 'call_123',
|
||||
id: toolCallId,
|
||||
type: 'tool-input-start',
|
||||
toolName: 'createDocument',
|
||||
toolCallType: 'function',
|
||||
args: JSON.stringify({
|
||||
},
|
||||
{
|
||||
id: toolCallId,
|
||||
type: 'tool-input-delta',
|
||||
delta: JSON.stringify({
|
||||
title: 'Essay about Silicon Valley',
|
||||
kind: 'text',
|
||||
}),
|
||||
},
|
||||
{
|
||||
id: toolCallId,
|
||||
type: 'tool-input-end',
|
||||
},
|
||||
{
|
||||
toolCallId: toolCallId,
|
||||
type: 'tool-result',
|
||||
toolName: 'createDocument',
|
||||
result: {
|
||||
id: 'doc_123',
|
||||
title: 'Essay about Silicon Valley',
|
||||
kind: 'text',
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (
|
||||
|
|
@ -191,23 +215,18 @@ As we move forward, Silicon Valley continues to reinvent itself. While some pred
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (
|
||||
compareMessages(recentMessage, TEST_PROMPTS.CREATE_DOCUMENT_TEXT_RESULT)
|
||||
) {
|
||||
return [
|
||||
{
|
||||
type: 'text-delta',
|
||||
textDelta: 'A document was created and is now visible to the user.',
|
||||
},
|
||||
...textToDeltas('A document was created and is now visible to the user.'),
|
||||
{
|
||||
type: 'finish',
|
||||
finishReason: 'tool-calls',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
finishReason: 'stop',
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.GET_WEATHER_CALL)) {
|
||||
|
|
@ -216,14 +235,12 @@ As we move forward, Silicon Valley continues to reinvent itself. While some pred
|
|||
type: 'tool-call',
|
||||
toolCallId: 'call_456',
|
||||
toolName: 'getWeather',
|
||||
toolCallType: 'function',
|
||||
args: JSON.stringify({ latitude: 37.7749, longitude: -122.4194 }),
|
||||
input: JSON.stringify({ latitude: 37.7749, longitude: -122.4194 }),
|
||||
},
|
||||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
} else if (compareMessages(recentMessage, TEST_PROMPTS.GET_WEATHER_RESULT)) {
|
||||
|
|
@ -232,11 +249,10 @@ As we move forward, Silicon Valley continues to reinvent itself. While some pred
|
|||
{
|
||||
type: 'finish',
|
||||
finishReason: 'stop',
|
||||
logprobs: undefined,
|
||||
usage: { completionTokens: 10, promptTokens: 3 },
|
||||
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return [{ type: 'text-delta', textDelta: 'Unknown test prompt!' }];
|
||||
return [{ id: '6', type: 'text-delta', delta: 'Unknown test prompt!' }];
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue