Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/runner/src/builtins/llm-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ async function invokeToolCall(
await ensureSourceCharmRunning(runtime, charmMeta);
}

const { resolve, promise } = Promise.withResolvers<any>();

// runSynced charm
runtime.editWithRetry((tx) => {
if (pattern) {
Expand All @@ -372,12 +374,22 @@ async function invokeToolCall(
}
});

await runtime.idle();

// If this was a pattern, stop it now that we have the result
if (pattern) runtime.runner.stop(result);
if (pattern) {
// wait until we know we have the result of the tool call
// not just that the transaction has been comitted
const cancel = result.sink((r) => {
r !== undefined && resolve(r);
});
const resultValue = await promise;
cancel();

return { type: "json", value: result.get() };
// stop it now that we have the result
runtime.runner.stop(result);
return { type: "json", value: resultValue };
} else {
await runtime.idle();
return { type: "json", value: result.get() };
}
}

/**
Expand Down