I am using the new stream feature of the AssistantApi.
I wonder how does one get the run id using the event-based approach described in the docs?
export const createStreamedRun = (threadId: string, assistantId: string, callback: any) => {
let run;
return new Promise((resolve) => {
const tsStart = Date.now();
try {
let allContent = '',
finishReason = '',
toolCalls: any = [];
run = openai.beta.threads.runs
.createAndStream(threadId, {
assistant_id: assistantId,
})
.on('textCreated', () => {
allContent = '';
})
.on('textDelta', (textDelta, _snapshot) => {
allContent += textDelta.value;
callback(allContent);
})
.on('toolCallCreated', (toolCall) => {
toolCalls.push(toolCall);
finishReason = 'tool_calls';
})
.on('toolCallDelta', (toolCallDelta, _snapshot) => {
const { type } = toolCallDelta;
if (type !== 'function') return;
mergeToolCalls(toolCalls, toolCallDelta);
})
.on('error', (err) => {
resolve({
success: false,
error: err.message,
});
})
.on('end', async () => {
const tsEnd = Date.now();
const duration = (tsEnd - tsStart) / 1000;
resolve({
success: true,
content: allContent,
finishReason,
duration,
tsStart,
tsEnd,
toolCalls,
});
});
} catch (err: any) {
resolve({
success: false,
error: err.message,
});
}
});
};