Skip to content

Commit cd3409d

Browse files
authored
πŸ“¦ NEW: Export helper functions (#83)
* πŸ“¦ NEW: Export `setInput` * πŸ“¦ NEW: Handle response stream helper function
1 parent d106886 commit cd3409d

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

β€Žpackages/core/src/common/request.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Stream} from 'openai/streaming';
1+
import {handleResponseStream} from 'src/helpers';
22
import {APIConnectionError, APIError} from './errors';
33

44
interface RequestOptions {
@@ -64,7 +64,7 @@ export class Request {
6464
const threadId = response.headers.get('lb-thread-id');
6565

6666
if (options.body?.stream) {
67-
return this.handleRunResponseStream({
67+
return handleResponseStream({
6868
response,
6969
rawResponse: options.body.rawResponse,
7070
}) as T;
@@ -128,42 +128,6 @@ export class Request {
128128
);
129129
}
130130

131-
private handleRunResponseStream({
132-
response,
133-
rawResponse,
134-
}: {
135-
response: Response;
136-
rawResponse?: boolean;
137-
}): {
138-
stream: any;
139-
threadId: string | null;
140-
rawResponse?: {
141-
headers: Record<string, string>;
142-
};
143-
} {
144-
const controller = new AbortController();
145-
// const stream = Stream.fromSSEResponse(response, controller);
146-
const streamSSE = Stream.fromSSEResponse(response, controller);
147-
const stream = streamSSE.toReadableStream();
148-
149-
const result: {
150-
stream: ReadableStream<any>;
151-
threadId: string | null;
152-
rawResponse?: {
153-
headers: Record<string, string>;
154-
};
155-
} = {
156-
stream,
157-
threadId: response.headers.get('lb-thread-id'),
158-
};
159-
if (rawResponse) {
160-
result.rawResponse = {
161-
headers: Object.fromEntries(response.headers.entries()),
162-
};
163-
}
164-
return result;
165-
}
166-
167131
private async handleRunResponse({
168132
response,
169133
isChat,

β€Žpackages/core/src/helpers/stream.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ChatCompletionStream} from 'openai/lib/ChatCompletionStream';
22
import {ChunkStream} from 'src/pipes';
3+
import {Stream} from 'openai/streaming';
34

45
export interface Runner extends ChatCompletionStream<null> {}
56

@@ -32,3 +33,51 @@ export const getRunner = (readableStream: ReadableStream) => {
3233
export const getTextPart = (chunk: ChunkStream) => {
3334
return chunk.choices[0]?.delta?.content || '';
3435
};
36+
37+
/**
38+
* Handles the response stream from a given `Response` object.
39+
*
40+
* @param {Object} params - The parameters for handling the response stream.
41+
* @param {Response} params.response - The API response to handle.
42+
* @param {boolean} params.rawResponse - Optional flag to include raw response headers.
43+
*
44+
* @returns {Object} An object containing the processed stream, thread ID, and optionally raw response headers.
45+
* @returns {ReadableStream<any>} return.stream - The readable stream created from the response.
46+
* @returns {string | null} return.threadId - The thread ID extracted from the response headers.
47+
* @returns {Object} [return.rawResponse] - Optional raw response headers.
48+
* @returns {Record<string, string>} return.rawResponse.headers - The headers from the raw response.
49+
*/
50+
export function handleResponseStream({
51+
response,
52+
rawResponse,
53+
}: {
54+
response: Response;
55+
rawResponse?: boolean;
56+
}): {
57+
stream: any;
58+
threadId: string | null;
59+
rawResponse?: {
60+
headers: Record<string, string>;
61+
};
62+
} {
63+
const controller = new AbortController();
64+
const streamSSE = Stream.fromSSEResponse(response, controller);
65+
const stream = streamSSE.toReadableStream();
66+
67+
const result: {
68+
stream: ReadableStream<any>;
69+
threadId: string | null;
70+
rawResponse?: {
71+
headers: Record<string, string>;
72+
};
73+
} = {
74+
stream,
75+
threadId: response.headers.get('lb-thread-id'),
76+
};
77+
if (rawResponse) {
78+
result.rawResponse = {
79+
headers: Object.fromEntries(response.headers.entries()),
80+
};
81+
}
82+
return result;
83+
}

β€Žpackages/core/src/react/use-pipe.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export function usePipe({
263263
setMessages: updateMessages,
264264
threadId: threadIdRef.current,
265265
sendMessage,
266+
setInput,
266267
}),
267268
[
268269
messages,

0 commit comments

Comments
Β (0)