Skip to content

Commit

Permalink
Fix RunThreadNode not actually waiting for run to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
abrenneke committed Jan 23, 2024
1 parent f9d3023 commit 4e30864
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/core/src/plugins/openai/nodes/RunThreadNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,14 @@ export const RunThreadNodeImpl: PluginNodeImpl<RunThreadNode> = {
})
: Promise.resolve();

try {
let runStatus = body.status;
let latestBody = body;

while (runStatus === 'in_progress' || runStatus === 'queued') {
try {
while (
latestBody.status === 'in_progress' ||
latestBody.status === 'queued' ||
latestBody.status === 'requires_action'
) {
const runResponse = await fetch(`https://api.openai.com/v1/threads/${body.thread_id}/runs/${body.id}`, {
method: 'GET',
headers: {
Expand All @@ -600,12 +604,11 @@ export const RunThreadNodeImpl: PluginNodeImpl<RunThreadNode> = {

await handleOpenAIError(runResponse);

const runBody = (await runResponse.json()) as OpenAIRun;
runStatus = runBody.status;
latestBody = (await runResponse.json()) as OpenAIRun;

// Requires action, start calling subgraphs
if (runStatus === 'requires_action') {
const toolCalls = runBody.required_action!.submit_tool_outputs.tool_calls;
if (latestBody.status === 'requires_action') {
const toolCalls = latestBody.required_action!.submit_tool_outputs.tool_calls;

/** Run one subgraph per tool call requested */
const toolCallOutputs = await Promise.all(
Expand All @@ -620,11 +623,11 @@ export const RunThreadNodeImpl: PluginNodeImpl<RunThreadNode> = {
const inputs: Record<string, DataValue> = {
run_id: {
type: 'string',
value: runBody.id,
value: latestBody.id,
},
run: {
type: 'object',
value: runBody,
value: latestBody,
},
tool_call_id: {
type: 'string',
Expand Down Expand Up @@ -697,12 +700,12 @@ export const RunThreadNodeImpl: PluginNodeImpl<RunThreadNode> = {
}

if (
runStatus === 'cancelled' ||
runStatus === 'cancelling' ||
runStatus === 'expired' ||
runStatus === 'failed'
latestBody.status === 'cancelled' ||
latestBody.status === 'cancelling' ||
latestBody.status === 'expired' ||
latestBody.status === 'failed'
) {
throw new Error(`Run failed with status: ${runStatus}`);
throw new Error(`Run failed with status: ${latestBody.status}`);
}

const listStepsQuery = new URLSearchParams({
Expand Down Expand Up @@ -758,11 +761,11 @@ export const RunThreadNodeImpl: PluginNodeImpl<RunThreadNode> = {
return {
['runId' as PortId]: {
type: 'string',
value: body.id,
value: latestBody.id,
},
['run' as PortId]: {
type: 'object',
value: body,
value: latestBody,
},
['steps' as PortId]: {
type: 'object[]',
Expand Down

1 comment on commit 4e30864

@hotelbuddy-online
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice job

Please sign in to comment.