Skip to content

Commit 42209be

Browse files
authoredMar 15, 2024··
AssistantResponse: specify forwardStream return type. (#1164)
1 parent efff0d4 commit 42209be

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed
 

‎.changeset/giant-dodos-glow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
AssistantResponse: specify forwardStream return type.

‎docs/pages/docs/api-reference/providers/assistant-response.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The process parameter is a callback in which you can run the assistant on thread
3535

3636
It gets invoked with the following functions that you can use to send messages and data messages to the client:
3737

38-
- `forwardStream: (stream: AssistantStream) => void`: Forwards the assistant response stream to the client.
38+
- `forwardStream: (stream: AssistantStream) => Run | undefined`: Forwards the assistant response stream to the client. Returns the `Run` object after it completes, or when it requires an action.
3939
- `sendDataMessage: (message: DataMessage) => void`: Send a data message to the client. You can use this to provide information for rendering custom UIs while the assistant is processing the thread.
4040

4141
## Example
@@ -100,7 +100,7 @@ export async function POST(req: Request) {
100100

101101
// status can be: queued, in_progress, requires_action, cancelling, cancelled, failed, completed, or expired
102102
while (
103-
runResult.status === 'requires_action' &&
103+
runResult?.status === 'requires_action' &&
104104
runResult.required_action?.type === 'submit_tool_outputs'
105105
) {
106106
const tool_outputs =

‎examples/next-openai/app/api/assistant/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function POST(req: Request) {
5050

5151
// status can be: queued, in_progress, requires_action, cancelling, cancelled, failed, completed, or expired
5252
while (
53-
runResult.status === 'requires_action' &&
53+
runResult?.status === 'requires_action' &&
5454
runResult.required_action?.type === 'submit_tool_outputs'
5555
) {
5656
const tool_outputs =

‎packages/core/streams/assistant-response.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AssistantStream } from 'openai/lib/AssistantStream';
22
import { formatStreamPart } from '../shared/stream-parts';
33
import { AssistantMessage, DataMessage } from '../shared/types';
4+
import { Run } from 'openai/resources/beta/threads/runs/runs';
45

56
type AssistantResponseSettings = {
67
threadId: string;
@@ -12,7 +13,7 @@ type AssistantResponseCallback = (options: {
1213
messageId: string;
1314
sendMessage: (message: AssistantMessage) => void;
1415
sendDataMessage: (message: DataMessage) => void;
15-
forwardStream: (stream: AssistantStream) => Promise<any>;
16+
forwardStream: (stream: AssistantStream) => Promise<Run | undefined>;
1617
}) => Promise<void>;
1718

1819
export function experimental_AssistantResponse(
@@ -42,7 +43,7 @@ export function experimental_AssistantResponse(
4243
};
4344

4445
const forwardStream = async (stream: AssistantStream) => {
45-
let result: any = undefined;
46+
let result: Run | undefined = undefined;
4647

4748
for await (const value of stream) {
4849
switch (value.event) {

0 commit comments

Comments
 (0)
Please sign in to comment.