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.0.6
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.0.7
Choose a head ref
  • 3 commits
  • 27 files changed
  • 3 contributors

Commits on Mar 6, 2024

  1. feat(ai/rsc): readStreamableValue (#1114)

    shuding authored Mar 6, 2024
    Copy the full SHA
    2367b59 View commit details
  2. chore: Add changeset for #1114 (#1120)

    shuding authored Mar 6, 2024
    Copy the full SHA
    9a9ae73 View commit details
  3. Version Packages (#1121)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Mar 6, 2024
    Copy the full SHA
    afee5a2 View commit details
2 changes: 1 addition & 1 deletion examples/next-anthropic/package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
},
"dependencies": {
"@anthropic-ai/sdk": "0.15.0",
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
2 changes: 1 addition & 1 deletion examples/next-aws-bedrock/package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
},
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "3.451.0",
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
2 changes: 1 addition & 1 deletion examples/next-fireworks/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"openai": "4.16.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-google-generative-ai/package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
},
"dependencies": {
"@google/generative-ai": "0.1.1",
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
2 changes: 1 addition & 1 deletion examples/next-huggingface/package.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
"dependencies": {
"@huggingface/inference": "^2.5.1",
"next": "14.1.1",
"ai": "3.0.6",
"ai": "3.0.7",
"react": "18.2.0",
"react-dom": "^18.2.0"
},
2 changes: 1 addition & 1 deletion examples/next-inkeep/package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
},
"dependencies": {
"@inkeep/ai-api": "^0.1.8",
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"react": "18.2.0",
"react-dom": "^18.2.0"
2 changes: 1 addition & 1 deletion examples/next-langchain/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"langchain": "^0.0.196",
"next": "14.1.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-mistral/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"@mistralai/mistralai": "0.1.3",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-openai-pages/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"openai": "4.16.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-openai-rate-limits/package.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
"dependencies": {
"@upstash/ratelimit": "^0.4.3",
"@vercel/kv": "^0.2.2",
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"openai": "4.16.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-openai/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"openai": "4.16.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/next-perplexity/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"ai": "3.0.6",
"ai": "3.0.7",
"next": "14.1.1",
"openai": "4.16.1",
"react": "18.2.0",
2 changes: 1 addition & 1 deletion examples/nuxt-openai/package.json
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
"@vue/runtime-core": "^3.3.4",
"@vue/runtime-dom": "^3.3.4",
"@vue/shared": "^3.3.4",
"ai": "3.0.6",
"ai": "3.0.7",
"nuxt": "^3.6.5",
"openai": "4.16.1",
"tailwindcss": "^3.3.3",
2 changes: 1 addition & 1 deletion examples/solidstart-openai/package.json
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
"dependencies": {
"@solidjs/meta": "0.29.3",
"@solidjs/router": "0.8.2",
"ai": "3.0.6",
"ai": "3.0.7",
"openai": "4.16.1",
"solid-js": "1.8.7",
"solid-start": "0.3.10",
2 changes: 1 addition & 1 deletion examples/sveltekit-openai/package.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
},
"dependencies": {
"openai": "4.16.1",
"ai": "3.0.6"
"ai": "3.0.7"
},
"devDependencies": {
"@fontsource/fira-mono": "^4.5.10",
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ai

## 3.0.7

### Patch Changes

- 9a9ae73: feat(ai/rsc): readStreamableValue

## 3.0.6

### Patch Changes
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ai",
"version": "3.0.6",
"version": "3.0.7",
"license": "Apache-2.0",
"sideEffects": false,
"main": "./dist/index.js",
2 changes: 1 addition & 1 deletion packages/core/rsc/index.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ export type {
createAI,
} from './rsc-server';
export type {
useStreamableValue,
readStreamableValue,
useUIState,
useAIState,
useActions,
2 changes: 1 addition & 1 deletion packages/core/rsc/rsc-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
useStreamableValue,
readStreamableValue,
useUIState,
useAIState,
useActions,
2 changes: 1 addition & 1 deletion packages/core/rsc/rsc-shared.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

export {
useStreamableValue,
readStreamableValue,
useUIState,
useAIState,
useActions,
59 changes: 0 additions & 59 deletions packages/core/rsc/shared-client/hooks.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/rsc/shared-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

export { useStreamableValue } from './hooks';
export { readStreamableValue } from './streamable';
export {
useUIState,
useAIState,
100 changes: 100 additions & 0 deletions packages/core/rsc/shared-client/streamable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { STREAMABLE_VALUE_TYPE } from '../constants';
import type { StreamableValue } from '../types';

function assertStreamableValue(
value: unknown,
): asserts value is StreamableValue {
if (
!value ||
typeof value !== 'object' ||
!('type' in value) ||
value.type !== STREAMABLE_VALUE_TYPE
) {
throw new Error(
'Invalid value: this hook only accepts values created via `createValueStream` from the server.',
);
}
}

/**
* `readStreamableValue` takes a streamable value created via the `createStreamableValue().value` API,
* and returns an async iterator.
*
* ```js
* // Inside your AI action:
*
* async function action() {
* 'use server'
* const streamable = createStreamableValue();
*
* streamable.update(1);
* streamable.update(2);
* streamable.done(3);
* // ...
* return streamable.value;
* }
* ```
*
* And to read the value:
*
* ```js
* const streamableValue = await action()
* for await (const v of readStreamableValue(streamableValue)) {
* console.log(v)
* }
* ```
*
* This logs out 1, 2, 3 on console.
*/
export function readStreamableValue<T = unknown>(
streamableValue: StreamableValue<T>,
): AsyncIterable<T | undefined> {
assertStreamableValue(streamableValue);

return {
[Symbol.asyncIterator]() {
let row: StreamableValue<T> | Promise<StreamableValue<T>> =
streamableValue;
let curr = row.curr;
let done = false;

return {
async next() {
if (done) return { value: curr, done: true };

row = await row;

if (typeof row.error !== 'undefined') {
throw row.error;
}
if ('curr' in row) {
curr = row.curr;

// The last emitted { done: true } won't be used as the value
// by the for await...of syntax.
if (!row.next) {
done = true;
return {
value: curr,
done: false,
};
}
}

if (!row.next) {
return {
value: curr,
done: true,
};
}

row = row.next;
return {
value: curr,
done: false,
};
},
};
},
};
}
Loading