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