host: expose harness state and actions over the world API
This commit is contained in:
+15
-4
@@ -1,20 +1,31 @@
|
||||
// Channel submissions target any conversation. EOF and Stop close the harness cleanly.
|
||||
export async function serve(harness: any, context: any, afterSubmit?: () => Promise<void>) {
|
||||
export async function serve(harness: any, context: any, afterSubmit?: () => Promise<void>, api?: any) {
|
||||
const pending = new Set<Promise<void>>();
|
||||
const track = (work: Promise<void>) => {
|
||||
const task = work.catch(error => Deno.core.ops.op_output(`${String(error)}\n`)).finally(() => pending.delete(task));
|
||||
pending.add(task);
|
||||
};
|
||||
while (true) {
|
||||
const input = await Deno.core.ops.op_input();
|
||||
if (input === null || input.type === 'stop') {
|
||||
await harness.waitForIdle(context);
|
||||
await Promise.all(pending);
|
||||
return;
|
||||
}
|
||||
if (input.type === 'api') {
|
||||
track(api.handle(input));
|
||||
continue;
|
||||
}
|
||||
const conversation = await harness.conversation(input.conversation, context);
|
||||
if (!conversation) throw new Error(`Unknown conversation: ${input.conversation}`);
|
||||
const submission = await conversation.submit({
|
||||
type: 'input', content: input.content, requestId: input.requestId,
|
||||
}, context);
|
||||
if (afterSubmit) {
|
||||
const result = await submission.wait(context);
|
||||
await afterSubmit();
|
||||
if (result.status !== 'done') throw new Error(`Run ended: ${JSON.stringify(result)}`);
|
||||
track(submission.wait(context).then(async (result: any) => {
|
||||
await afterSubmit();
|
||||
if (result.status !== 'done') throw new Error(`Run ended: ${JSON.stringify(result)}`);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user