|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +/** |
| 6 | + * `query()` — a convenience wrapper that provides a simple async-iterator API |
| 7 | + * over the Copilot SDK. It creates a client + session, sends a prompt, and |
| 8 | + * yields every {@link SessionEvent} as it arrives. |
| 9 | + * |
| 10 | + * @example |
| 11 | + * ```typescript |
| 12 | + * import { query, defineTool } from "@github/copilot-sdk"; |
| 13 | + * |
| 14 | + * for await (const event of query({ prompt: "Hello!", tools: [myTool] })) { |
| 15 | + * if (event.type === "assistant.message_delta") { |
| 16 | + * process.stdout.write(event.data.deltaContent); |
| 17 | + * } |
| 18 | + * } |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * @module query |
| 22 | + */ |
| 23 | + |
| 24 | +import { CopilotClient } from "./client.js"; |
| 25 | +import { approveAll, type QueryOptions, type SessionEvent } from "./types.js"; |
| 26 | + |
| 27 | +/** |
| 28 | + * Send a prompt and yield every session event as an async iterator. |
| 29 | + * |
| 30 | + * Internally creates a {@link CopilotClient} and session, sends the prompt, |
| 31 | + * and tears everything down when the iterator finishes or is broken out of. |
| 32 | + * |
| 33 | + * The generator ends when: |
| 34 | + * - The session becomes idle (model finished), or |
| 35 | + * - `maxTurns` tool-calling turns have been reached, or |
| 36 | + * - The consumer breaks out of the `for await` loop. |
| 37 | + */ |
| 38 | +export async function* query(options: QueryOptions): AsyncGenerator<SessionEvent> { |
| 39 | + const cliUrl = options.cliUrl ?? process.env.COPILOT_CLI_URL; |
| 40 | + const client = new CopilotClient({ |
| 41 | + ...(cliUrl ? { cliUrl } : {}), |
| 42 | + ...(options.cliPath ? { cliPath: options.cliPath } : {}), |
| 43 | + ...(options.githubToken ? { githubToken: options.githubToken } : {}), |
| 44 | + }); |
| 45 | + |
| 46 | + try { |
| 47 | + const session = await client.createSession({ |
| 48 | + model: options.model, |
| 49 | + tools: options.tools ?? [], |
| 50 | + streaming: options.streaming ?? true, |
| 51 | + systemMessage: options.systemMessage, |
| 52 | + onPermissionRequest: options.onPermissionRequest ?? approveAll, |
| 53 | + }); |
| 54 | + |
| 55 | + // Bridge the event-driven API to an async iterator via a simple queue. |
| 56 | + let resolve: ((value: IteratorResult<SessionEvent>) => void) | null = null; |
| 57 | + const buffer: SessionEvent[] = []; |
| 58 | + let done = false; |
| 59 | + let turns = 0; |
| 60 | + |
| 61 | + const finish = () => { |
| 62 | + done = true; |
| 63 | + if (resolve) { |
| 64 | + resolve({ value: undefined as unknown as SessionEvent, done: true }); |
| 65 | + resolve = null; |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + session.on((event: SessionEvent) => { |
| 70 | + if (done) return; |
| 71 | + |
| 72 | + // Count tool-calling turns for maxTurns support. |
| 73 | + if ( |
| 74 | + options.maxTurns && |
| 75 | + event.type === "assistant.message" && |
| 76 | + event.data.toolRequests?.length |
| 77 | + ) { |
| 78 | + turns++; |
| 79 | + if (turns >= options.maxTurns) { |
| 80 | + if (resolve) { |
| 81 | + resolve({ value: event, done: false }); |
| 82 | + resolve = null; |
| 83 | + } else { |
| 84 | + buffer.push(event); |
| 85 | + } |
| 86 | + finish(); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (event.type === "session.idle") { |
| 92 | + if (resolve) { |
| 93 | + resolve({ value: event, done: false }); |
| 94 | + resolve = null; |
| 95 | + } else { |
| 96 | + buffer.push(event); |
| 97 | + } |
| 98 | + finish(); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (resolve) { |
| 103 | + resolve({ value: event, done: false }); |
| 104 | + resolve = null; |
| 105 | + } else { |
| 106 | + buffer.push(event); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + await session.send({ prompt: options.prompt }); |
| 111 | + |
| 112 | + while (!done || buffer.length > 0) { |
| 113 | + if (buffer.length > 0) { |
| 114 | + yield buffer.shift()!; |
| 115 | + } else if (done) { |
| 116 | + break; |
| 117 | + } else { |
| 118 | + yield await new Promise<SessionEvent>((r) => { |
| 119 | + resolve = (result) => { |
| 120 | + if (result.done) { |
| 121 | + r(undefined as unknown as SessionEvent); |
| 122 | + } else { |
| 123 | + r(result.value); |
| 124 | + } |
| 125 | + }; |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + } finally { |
| 130 | + await client.stop(); |
| 131 | + } |
| 132 | +} |
0 commit comments