Skip to content

Commit e05d845

Browse files
committed
👌 IMPROVE: Code
1 parent 0ae5d60 commit e05d845

File tree

1 file changed

+23
-0
lines changed
  • packages/langbase/src/lib/helpers

1 file changed

+23
-0
lines changed

packages/langbase/src/lib/helpers/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ChatCompletionStream} from 'openai/lib/ChatCompletionStream';
22
import {Stream} from 'openai/streaming';
33
import {ChatCompletionMessageToolCall} from 'openai/resources/chat/completions';
4+
import {RunResponse, RunResponseStream} from '@/langbase/langbase';
45

56
export interface Runner extends ChatCompletionStream {}
67

@@ -121,3 +122,25 @@ export async function getToolsFromStream(
121122
const tools = choices[0].message.tool_calls;
122123
return tools ?? [];
123124
}
125+
126+
/**
127+
* Retrieves tools from a readable stream asynchronously.
128+
*
129+
* @param stream - The readable stream to extract tools from
130+
* @returns A promise that resolves with the tools extracted from the stream
131+
*/
132+
export async function getToolsFromRunStream(response: RunResponseStream) {
133+
return getToolsFromStream(response.stream);
134+
}
135+
136+
/**
137+
* Extracts tool calls from non-stream response.
138+
* @param response - The run response object
139+
* @returns A promise that resolves to an array of tool calls. Returns empty array if no tools are present.
140+
*/
141+
export async function getToolsFromRun(
142+
response: RunResponse,
143+
): Promise<ChatCompletionMessageToolCall[]> {
144+
const tools = response.choices[0].message.tool_calls;
145+
return tools ?? [];
146+
}

0 commit comments

Comments
 (0)