Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vercel/ai
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ai@3.2.13
Choose a base ref
...
head repository: vercel/ai
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ai@3.2.14
Choose a head ref
  • 5 commits
  • 23 files changed
  • 4 contributors

Commits on Jun 27, 2024

  1. fix (provider/anthropic): automatically trim trailing whitespace on p…

    …re-filled assistant responses (#2124)
    lgrammel authored Jun 27, 2024
    Copy the full SHA
    7e86b45 View commit details
  2. Version Packages (#2125)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Jun 27, 2024
    Copy the full SHA
    83204b7 View commit details
  3. fix (ai/react): useObject stop & isLoading reset at end of stream (#2126

    )
    lgrammel authored Jun 27, 2024
    Copy the full SHA
    9b50003 View commit details
  4. feat (ai/ui): allow JSONValue as data in useChat handleSubmit (#2128)

    Co-authored-by: Brian Hung <brianlhung@gmail.com>
    lgrammel and BrianHung authored Jun 27, 2024
    Copy the full SHA
    1894f81 View commit details
  5. Version Packages (#2127)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Jun 27, 2024
    Copy the full SHA
    3cee4c3 View commit details
4 changes: 2 additions & 2 deletions content/docs/07-reference/ai-sdk-ui/01-use-chat.mdx
Original file line number Diff line number Diff line change
@@ -136,7 +136,7 @@ Allows you to easily create a conversational user interface for your chatbot app
},
{
name: 'experimental_prepareRequestBody',
type: '(options: { messages: Message[]; requestData?: Record<string, string>; requestBody?: object }) => JSONValue',
type: '(options: { messages: Message[]; requestData?: JSONValue; requestBody?: object }) => JSONValue',
optional: true,
description:
'Experimental (React only). When a function is provided, it will be used to prepare the request body for the chat API. This can be useful for customizing the request body based on the messages and data in the chat.',
@@ -232,7 +232,7 @@ Allows you to easily create a conversational user interface for your chatbot app
},
{
name: 'data',
type: 'Record<string,string>',
type: 'JSONValue',
description: 'Additional data to be sent to the server.',
},
],
6 changes: 6 additions & 0 deletions packages/anthropic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ai-sdk/anthropic

## 0.0.27

### Patch Changes

- 7e86b45e: fix (provider/anthropic): automatically trim trailing whitespace on pre-filled assistant responses

## 0.0.26

### Patch Changes
2 changes: 1 addition & 1 deletion packages/anthropic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-sdk/anthropic",
"version": "0.0.26",
"version": "0.0.27",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
Original file line number Diff line number Diff line change
@@ -254,3 +254,71 @@ describe('tool messages', () => {
});
});
});

describe('assistant messages', () => {
it('should remove trailing whitespace from last assistant message when there is no further user message', async () => {
const result = await convertToAnthropicMessagesPrompt({
prompt: [
{
role: 'user',
content: [{ type: 'text', text: 'user content' }],
},
{
role: 'assistant',
content: [{ type: 'text', text: 'assistant content ' }],
},
],
});

expect(result).toEqual({
messages: [
{
role: 'user',
content: [{ type: 'text', text: 'user content' }],
},
{
role: 'assistant',
content: [{ type: 'text', text: 'assistant content' }],
},
],
system: undefined,
});
});

it('should keep trailing whitespace from assistant message when there is a further user message', async () => {
const result = await convertToAnthropicMessagesPrompt({
prompt: [
{
role: 'user',
content: [{ type: 'text', text: 'user content' }],
},
{
role: 'assistant',
content: [{ type: 'text', text: 'assistant content ' }],
},
{
role: 'user',
content: [{ type: 'text', text: 'user content 2' }],
},
],
});

expect(result).toEqual({
messages: [
{
role: 'user',
content: [{ type: 'text', text: 'user content' }],
},
{
role: 'assistant',
content: [{ type: 'text', text: 'assistant content ' }],
},
{
role: 'user',
content: [{ type: 'text', text: 'user content 2' }],
},
],
system: undefined,
});
});
});
15 changes: 13 additions & 2 deletions packages/anthropic/src/convert-to-anthropic-messages-prompt.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@ export async function convertToAnthropicMessagesPrompt({
let system: string | undefined = undefined;
const messages: AnthropicMessage[] = [];

for (const block of blocks) {
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const type = block.type;

switch (type) {
@@ -118,9 +119,19 @@ export async function convertToAnthropicMessagesPrompt({

messages.push({
role: 'assistant',
content: content.map(part => {
content: content.map((part, j) => {
switch (part.type) {
case 'text': {
// trim the last text part if it's the last message in the block
// because Anthropic does not allow trailing whitespace
// in pre-filled assistant responses
if (
i === blocks.length - 1 &&
j === block.messages.length - 1
) {
return { type: 'text', text: part.text.trim() };
}

return { type: 'text', text: part.text };
}
case 'tool-call': {
12 changes: 12 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# ai

## 3.2.14

### Patch Changes

- Updated dependencies [9b50003d]
- Updated dependencies [1894f811]
- @ai-sdk/react@0.0.14
- @ai-sdk/ui-utils@0.0.9
- @ai-sdk/solid@0.0.11
- @ai-sdk/svelte@0.0.12
- @ai-sdk/vue@0.0.11

## 3.2.13

### Patch Changes
12 changes: 6 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ai",
"version": "3.2.13",
"version": "3.2.14",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
@@ -77,11 +77,11 @@
"dependencies": {
"@ai-sdk/provider": "0.0.11",
"@ai-sdk/provider-utils": "1.0.0",
"@ai-sdk/react": "0.0.13",
"@ai-sdk/solid": "0.0.10",
"@ai-sdk/svelte": "0.0.11",
"@ai-sdk/ui-utils": "0.0.8",
"@ai-sdk/vue": "0.0.10",
"@ai-sdk/react": "0.0.14",
"@ai-sdk/solid": "0.0.11",
"@ai-sdk/svelte": "0.0.12",
"@ai-sdk/ui-utils": "0.0.9",
"@ai-sdk/vue": "0.0.11",
"eventsource-parser": "1.1.2",
"jsondiffpatch": "0.6.0",
"json-schema": "0.4.0",
6 changes: 6 additions & 0 deletions packages/core/tests/e2e/next-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,12 @@

### Patch Changes

- ai@3.2.14

## null

### Patch Changes

- Updated dependencies [d3100b9c]
- ai@3.2.13

9 changes: 9 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @ai-sdk/react

## 0.0.14

### Patch Changes

- 9b50003d: fix (ai/react): useObject stop & isLoading reset at end of stream
- 1894f811: feat (ai/ui): allow JSONValue as data in useChat handleSubmit
- Updated dependencies [1894f811]
- @ai-sdk/ui-utils@0.0.9

## 0.0.13

### Patch Changes
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-sdk/react",
"version": "0.0.13",
"version": "0.0.14",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
@@ -26,7 +26,7 @@
},
"dependencies": {
"@ai-sdk/provider-utils": "1.0.0",
"@ai-sdk/ui-utils": "0.0.8",
"@ai-sdk/ui-utils": "0.0.9",
"swr": "2.2.0"
},
"devDependencies": {
4 changes: 2 additions & 2 deletions packages/react/src/use-chat.ts
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ const getStreamedResponse = async (
experimental_prepareRequestBody:
| ((options: {
messages: Message[];
requestData?: Record<string, string>;
requestData?: JSONValue;
requestBody?: object;
}) => JSONValue)
| undefined,
@@ -220,7 +220,7 @@ export function useChat({
*/
experimental_prepareRequestBody?: (options: {
messages: Message[];
requestData?: Record<string, string>;
requestData?: JSONValue;
requestBody?: object;
}) => JSONValue;

8 changes: 5 additions & 3 deletions packages/react/src/use-object.ts
Original file line number Diff line number Diff line change
@@ -148,15 +148,17 @@ function useObject<RESULT, INPUT = any>({
mutate(currentObject);
}
},

close() {
setIsLoading(false);
abortControllerRef.current = null;
},
}),
);

setError(undefined);
} catch (error) {
setError(error);
} finally {
setIsLoading(false);
abortControllerRef.current = null;
}
};

6 changes: 4 additions & 2 deletions packages/react/src/use-object.ui.test.tsx
Original file line number Diff line number Diff line change
@@ -111,8 +111,10 @@ describe('text stream', () => {
streamController.enqueue('"Hello, world!"}');
streamController.close();

await screen.findByTestId('loading');
expect(screen.getByTestId('loading')).toHaveTextContent('false');
// wait for element "loading" to have text content "false":
await waitFor(() => {
expect(screen.getByTestId('loading')).toHaveTextContent('true');
});
});
});

7 changes: 7 additions & 0 deletions packages/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ai-sdk/solid

## 0.0.11

### Patch Changes

- Updated dependencies [1894f811]
- @ai-sdk/ui-utils@0.0.9

## 0.0.10

### Patch Changes
4 changes: 2 additions & 2 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-sdk/solid",
"version": "0.0.10",
"version": "0.0.11",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
@@ -25,7 +25,7 @@
}
},
"dependencies": {
"@ai-sdk/ui-utils": "0.0.8"
"@ai-sdk/ui-utils": "0.0.9"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.4.5",
7 changes: 7 additions & 0 deletions packages/svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ai-sdk/svelte

## 0.0.12

### Patch Changes

- Updated dependencies [1894f811]
- @ai-sdk/ui-utils@0.0.9

## 0.0.11

### Patch Changes
4 changes: 2 additions & 2 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-sdk/svelte",
"version": "0.0.11",
"version": "0.0.12",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
@@ -24,7 +24,7 @@
},
"dependencies": {
"@ai-sdk/provider-utils": "1.0.0",
"@ai-sdk/ui-utils": "0.0.8",
"@ai-sdk/ui-utils": "0.0.9",
"sswr": "2.1.0"
},
"devDependencies": {
6 changes: 6 additions & 0 deletions packages/ui-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ai-sdk/ui-utils

## 0.0.9

### Patch Changes

- 1894f811: feat (ai/ui): allow JSONValue as data in useChat handleSubmit

## 0.0.8

### Patch Changes
2 changes: 1 addition & 1 deletion packages/ui-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-sdk/ui-utils",
"version": "0.0.8",
"version": "0.0.9",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
8 changes: 6 additions & 2 deletions packages/ui-utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -192,7 +192,7 @@ export type CreateMessage = Omit<Message, 'id'> & {
export type ChatRequest = {
messages: Message[];
options?: RequestOptions;
data?: Record<string, string>;
data?: JSONValue;

/**
* @deprecated
@@ -272,7 +272,7 @@ The options to be passed to the fetch call.
/**
Additional data to be sent to the server.
*/
data?: Record<string, string>;
data?: JSONValue;
};

export type UseChatOptions = {
@@ -468,6 +468,10 @@ or to provide a custom fetch implementation for e.g. testing.
fetch?: FetchFunction;
};

/**
A JSON value can be a string, number, boolean, object, array, or null.
JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
*/
export type JSONValue =
| null
| string
Loading