// Channel submissions target any conversation. EOF and Stop close the harness cleanly. export async function serve(harness: any, context: any, afterSubmit?: () => Promise, api?: any) { const pending = new Set>(); const track = (work: Promise) => { 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) { track(submission.wait(context).then(async (result: any) => { await afterSubmit(); if (result.status !== 'done') throw new Error(`Run ended: ${JSON.stringify(result)}`); })); } } }