@github/copilot-sdk 0.1.10-preview.0 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.js +257 -19
- package/dist/nodejs/src/client.d.ts +306 -0
- package/dist/{generated → nodejs/src/generated}/session-events.d.ts +8 -1
- package/dist/nodejs/src/index.d.ts +9 -0
- package/dist/nodejs/src/sdkProtocolVersion.d.ts +5 -0
- package/dist/nodejs/src/session.d.ts +194 -0
- package/dist/{types.d.ts → nodejs/src/types.d.ts} +113 -6
- package/dist/sdkProtocolVersion.js +7 -0
- package/dist/session.js +150 -7
- package/package.json +4 -3
- package/dist/client.d.ts +0 -95
- package/dist/index.d.ts +0 -9
- package/dist/session.d.ts +0 -39
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Generated from: @github/copilot/session-events.schema.json
|
|
5
5
|
* Generated by: scripts/generate-session-types.ts
|
|
6
|
-
* Generated at: 2026-01-
|
|
6
|
+
* Generated at: 2026-01-13T00:08:20.716Z
|
|
7
7
|
*
|
|
8
8
|
* To update these types:
|
|
9
9
|
* 1. Update the schema in copilot-agent-runtime
|
|
@@ -121,6 +121,13 @@ export type SessionEvent = {
|
|
|
121
121
|
}[];
|
|
122
122
|
source?: string;
|
|
123
123
|
};
|
|
124
|
+
} | {
|
|
125
|
+
id: string;
|
|
126
|
+
timestamp: string;
|
|
127
|
+
parentId: string | null;
|
|
128
|
+
ephemeral: true;
|
|
129
|
+
type: "pending_messages.modified";
|
|
130
|
+
data: {};
|
|
124
131
|
} | {
|
|
125
132
|
id: string;
|
|
126
133
|
timestamp: string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copilot SDK - TypeScript/Node.js Client
|
|
3
|
+
*
|
|
4
|
+
* JSON-RPC based SDK for programmatic control of GitHub Copilot CLI
|
|
5
|
+
*/
|
|
6
|
+
export { CopilotClient } from "./client.js";
|
|
7
|
+
export { CopilotSession } from "./session.js";
|
|
8
|
+
export { defineTool } from "./types.js";
|
|
9
|
+
export type { ConnectionState, CopilotClientOptions, CustomAgentConfig, MCPLocalServerConfig, MCPRemoteServerConfig, MCPServerConfig, MessageOptions, PermissionHandler, PermissionRequest, PermissionRequestResult, ResumeSessionConfig, SessionConfig, SessionEvent, SessionEventHandler, SessionMetadata, SystemMessageAppendConfig, SystemMessageConfig, SystemMessageReplaceConfig, Tool, ToolHandler, ToolInvocation, ToolResultObject, ZodSchema, } from "./types.js";
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copilot Session - represents a single conversation session with the Copilot CLI.
|
|
3
|
+
* @module session
|
|
4
|
+
*/
|
|
5
|
+
import type { MessageConnection } from "vscode-jsonrpc/node";
|
|
6
|
+
import type { MessageOptions, PermissionHandler, PermissionRequestResult, SessionEvent, SessionEventHandler, Tool, ToolHandler } from "./types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Represents a single conversation session with the Copilot CLI.
|
|
9
|
+
*
|
|
10
|
+
* A session maintains conversation state, handles events, and manages tool execution.
|
|
11
|
+
* Sessions are created via {@link CopilotClient.createSession} or resumed via
|
|
12
|
+
* {@link CopilotClient.resumeSession}.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const session = await client.createSession({ model: "gpt-4" });
|
|
17
|
+
*
|
|
18
|
+
* // Subscribe to events
|
|
19
|
+
* const unsubscribe = session.on((event) => {
|
|
20
|
+
* if (event.type === "assistant.message") {
|
|
21
|
+
* console.log(event.data.content);
|
|
22
|
+
* }
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Send a message
|
|
26
|
+
* await session.send({ prompt: "Hello, world!" });
|
|
27
|
+
*
|
|
28
|
+
* // Clean up
|
|
29
|
+
* unsubscribe();
|
|
30
|
+
* await session.destroy();
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare class CopilotSession {
|
|
34
|
+
readonly sessionId: string;
|
|
35
|
+
private connection;
|
|
36
|
+
private eventHandlers;
|
|
37
|
+
private toolHandlers;
|
|
38
|
+
private permissionHandler?;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new CopilotSession instance.
|
|
41
|
+
*
|
|
42
|
+
* @param sessionId - The unique identifier for this session
|
|
43
|
+
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
44
|
+
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
45
|
+
*/
|
|
46
|
+
constructor(sessionId: string, connection: MessageConnection);
|
|
47
|
+
/**
|
|
48
|
+
* Sends a message to this session and waits for the response.
|
|
49
|
+
*
|
|
50
|
+
* The message is processed asynchronously. Subscribe to events via {@link on}
|
|
51
|
+
* to receive streaming responses and other session events.
|
|
52
|
+
*
|
|
53
|
+
* @param options - The message options including the prompt and optional attachments
|
|
54
|
+
* @returns A promise that resolves with the message ID of the response
|
|
55
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const messageId = await session.send({
|
|
60
|
+
* prompt: "Explain this code",
|
|
61
|
+
* attachments: [{ type: "file", path: "./src/index.ts" }]
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
send(options: MessageOptions): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Subscribes to events from this session.
|
|
68
|
+
*
|
|
69
|
+
* Events include assistant messages, tool executions, errors, and session state changes.
|
|
70
|
+
* Multiple handlers can be registered and will all receive events.
|
|
71
|
+
*
|
|
72
|
+
* @param handler - A callback function that receives session events
|
|
73
|
+
* @returns A function that, when called, unsubscribes the handler
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const unsubscribe = session.on((event) => {
|
|
78
|
+
* switch (event.type) {
|
|
79
|
+
* case "assistant.message":
|
|
80
|
+
* console.log("Assistant:", event.data.content);
|
|
81
|
+
* break;
|
|
82
|
+
* case "session.error":
|
|
83
|
+
* console.error("Error:", event.data.message);
|
|
84
|
+
* break;
|
|
85
|
+
* }
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* // Later, to stop receiving events:
|
|
89
|
+
* unsubscribe();
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
on(handler: SessionEventHandler): () => void;
|
|
93
|
+
/**
|
|
94
|
+
* Dispatches an event to all registered handlers.
|
|
95
|
+
*
|
|
96
|
+
* @param event - The session event to dispatch
|
|
97
|
+
* @internal This method is for internal use by the SDK.
|
|
98
|
+
*/
|
|
99
|
+
_dispatchEvent(event: SessionEvent): void;
|
|
100
|
+
/**
|
|
101
|
+
* Registers custom tool handlers for this session.
|
|
102
|
+
*
|
|
103
|
+
* Tools allow the assistant to execute custom functions. When the assistant
|
|
104
|
+
* invokes a tool, the corresponding handler is called with the tool arguments.
|
|
105
|
+
*
|
|
106
|
+
* @param tools - An array of tool definitions with their handlers, or undefined to clear all tools
|
|
107
|
+
* @internal This method is typically called internally when creating a session with tools.
|
|
108
|
+
*/
|
|
109
|
+
registerTools(tools?: Tool[]): void;
|
|
110
|
+
/**
|
|
111
|
+
* Retrieves a registered tool handler by name.
|
|
112
|
+
*
|
|
113
|
+
* @param name - The name of the tool to retrieve
|
|
114
|
+
* @returns The tool handler if found, or undefined
|
|
115
|
+
* @internal This method is for internal use by the SDK.
|
|
116
|
+
*/
|
|
117
|
+
getToolHandler(name: string): ToolHandler | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Registers a handler for permission requests.
|
|
120
|
+
*
|
|
121
|
+
* When the assistant needs permission to perform certain actions (e.g., file operations),
|
|
122
|
+
* this handler is called to approve or deny the request.
|
|
123
|
+
*
|
|
124
|
+
* @param handler - The permission handler function, or undefined to remove the handler
|
|
125
|
+
* @internal This method is typically called internally when creating a session.
|
|
126
|
+
*/
|
|
127
|
+
registerPermissionHandler(handler?: PermissionHandler): void;
|
|
128
|
+
/**
|
|
129
|
+
* Handles a permission request from the Copilot CLI.
|
|
130
|
+
*
|
|
131
|
+
* @param request - The permission request data from the CLI
|
|
132
|
+
* @returns A promise that resolves with the permission decision
|
|
133
|
+
* @internal This method is for internal use by the SDK.
|
|
134
|
+
*/
|
|
135
|
+
_handlePermissionRequest(request: unknown): Promise<PermissionRequestResult>;
|
|
136
|
+
/**
|
|
137
|
+
* Retrieves all events and messages from this session's history.
|
|
138
|
+
*
|
|
139
|
+
* This returns the complete conversation history including user messages,
|
|
140
|
+
* assistant responses, tool executions, and other session events.
|
|
141
|
+
*
|
|
142
|
+
* @returns A promise that resolves with an array of all session events
|
|
143
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const events = await session.getMessages();
|
|
148
|
+
* for (const event of events) {
|
|
149
|
+
* if (event.type === "assistant.message") {
|
|
150
|
+
* console.log("Assistant:", event.data.content);
|
|
151
|
+
* }
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
getMessages(): Promise<SessionEvent[]>;
|
|
156
|
+
/**
|
|
157
|
+
* Destroys this session and releases all associated resources.
|
|
158
|
+
*
|
|
159
|
+
* After calling this method, the session can no longer be used. All event
|
|
160
|
+
* handlers and tool handlers are cleared. To continue the conversation,
|
|
161
|
+
* use {@link CopilotClient.resumeSession} with the session ID.
|
|
162
|
+
*
|
|
163
|
+
* @returns A promise that resolves when the session is destroyed
|
|
164
|
+
* @throws Error if the connection fails
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* // Clean up when done
|
|
169
|
+
* await session.destroy();
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
destroy(): Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Aborts the currently processing message in this session.
|
|
175
|
+
*
|
|
176
|
+
* Use this to cancel a long-running request. The session remains valid
|
|
177
|
+
* and can continue to be used for new messages.
|
|
178
|
+
*
|
|
179
|
+
* @returns A promise that resolves when the abort request is acknowledged
|
|
180
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* // Start a long-running request
|
|
185
|
+
* const messagePromise = session.send({ prompt: "Write a very long story..." });
|
|
186
|
+
*
|
|
187
|
+
* // Abort after 5 seconds
|
|
188
|
+
* setTimeout(async () => {
|
|
189
|
+
* await session.abort();
|
|
190
|
+
* }, 5000);
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
abort(): Promise<void>;
|
|
194
|
+
}
|
|
@@ -151,6 +151,105 @@ export interface SystemMessageReplaceConfig {
|
|
|
151
151
|
* - Replace mode: Full control, caller provides entire system message
|
|
152
152
|
*/
|
|
153
153
|
export type SystemMessageConfig = SystemMessageAppendConfig | SystemMessageReplaceConfig;
|
|
154
|
+
/**
|
|
155
|
+
* Permission request types from the server
|
|
156
|
+
*/
|
|
157
|
+
export interface PermissionRequest {
|
|
158
|
+
kind: "shell" | "write" | "mcp" | "read" | "url";
|
|
159
|
+
toolCallId?: string;
|
|
160
|
+
[key: string]: unknown;
|
|
161
|
+
}
|
|
162
|
+
export interface PermissionRequestResult {
|
|
163
|
+
kind: "approved" | "denied-by-rules" | "denied-no-approval-rule-and-could-not-request-from-user" | "denied-interactively-by-user";
|
|
164
|
+
rules?: unknown[];
|
|
165
|
+
}
|
|
166
|
+
export type PermissionHandler = (request: PermissionRequest, invocation: {
|
|
167
|
+
sessionId: string;
|
|
168
|
+
}) => Promise<PermissionRequestResult> | PermissionRequestResult;
|
|
169
|
+
/**
|
|
170
|
+
* Base interface for MCP server configuration.
|
|
171
|
+
*/
|
|
172
|
+
interface MCPServerConfigBase {
|
|
173
|
+
/**
|
|
174
|
+
* List of tools to include from this server. [] means none. "*" means all.
|
|
175
|
+
*/
|
|
176
|
+
tools: string[];
|
|
177
|
+
/**
|
|
178
|
+
* Indicates "remote" or "local" server type.
|
|
179
|
+
* If not specified, defaults to "local".
|
|
180
|
+
*/
|
|
181
|
+
type?: string;
|
|
182
|
+
/**
|
|
183
|
+
* Optional timeout in milliseconds for tool calls to this server.
|
|
184
|
+
*/
|
|
185
|
+
timeout?: number;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Configuration for a local/stdio MCP server.
|
|
189
|
+
*/
|
|
190
|
+
export interface MCPLocalServerConfig extends MCPServerConfigBase {
|
|
191
|
+
type?: "local" | "stdio";
|
|
192
|
+
command: string;
|
|
193
|
+
args: string[];
|
|
194
|
+
/**
|
|
195
|
+
* Environment variables to pass to the server.
|
|
196
|
+
*/
|
|
197
|
+
env?: Record<string, string>;
|
|
198
|
+
cwd?: string;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Configuration for a remote MCP server (HTTP or SSE).
|
|
202
|
+
*/
|
|
203
|
+
export interface MCPRemoteServerConfig extends MCPServerConfigBase {
|
|
204
|
+
type: "http" | "sse";
|
|
205
|
+
/**
|
|
206
|
+
* URL of the remote server.
|
|
207
|
+
*/
|
|
208
|
+
url: string;
|
|
209
|
+
/**
|
|
210
|
+
* Optional HTTP headers to include in requests.
|
|
211
|
+
*/
|
|
212
|
+
headers?: Record<string, string>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Union type for MCP server configurations.
|
|
216
|
+
*/
|
|
217
|
+
export type MCPServerConfig = MCPLocalServerConfig | MCPRemoteServerConfig;
|
|
218
|
+
/**
|
|
219
|
+
* Configuration for a custom agent.
|
|
220
|
+
*/
|
|
221
|
+
export interface CustomAgentConfig {
|
|
222
|
+
/**
|
|
223
|
+
* Unique name of the custom agent.
|
|
224
|
+
*/
|
|
225
|
+
name: string;
|
|
226
|
+
/**
|
|
227
|
+
* Display name for UI purposes.
|
|
228
|
+
*/
|
|
229
|
+
displayName?: string;
|
|
230
|
+
/**
|
|
231
|
+
* Description of what the agent does.
|
|
232
|
+
*/
|
|
233
|
+
description?: string;
|
|
234
|
+
/**
|
|
235
|
+
* List of tool names the agent can use.
|
|
236
|
+
* Use null or undefined for all tools.
|
|
237
|
+
*/
|
|
238
|
+
tools?: string[] | null;
|
|
239
|
+
/**
|
|
240
|
+
* The prompt content for the agent.
|
|
241
|
+
*/
|
|
242
|
+
prompt: string;
|
|
243
|
+
/**
|
|
244
|
+
* MCP servers specific to this agent.
|
|
245
|
+
*/
|
|
246
|
+
mcpServers?: Record<string, MCPServerConfig>;
|
|
247
|
+
/**
|
|
248
|
+
* Whether the agent should be available for model inference.
|
|
249
|
+
* @default true
|
|
250
|
+
*/
|
|
251
|
+
infer?: boolean;
|
|
252
|
+
}
|
|
154
253
|
export interface SessionConfig {
|
|
155
254
|
/**
|
|
156
255
|
* Optional custom session ID
|
|
@@ -186,18 +285,25 @@ export interface SessionConfig {
|
|
|
186
285
|
*/
|
|
187
286
|
provider?: ProviderConfig;
|
|
188
287
|
/**
|
|
189
|
-
*
|
|
190
|
-
* When
|
|
191
|
-
* events are sent as the response is generated. Clients should accumulate
|
|
192
|
-
* deltaContent values to build the full response.
|
|
193
|
-
* @default false
|
|
288
|
+
* Handler for permission requests from the server.
|
|
289
|
+
* When provided, the server will call this handler to request permission for operations.
|
|
194
290
|
*/
|
|
291
|
+
onPermissionRequest?: PermissionHandler;
|
|
195
292
|
streaming?: boolean;
|
|
293
|
+
/**
|
|
294
|
+
* MCP server configurations for the session.
|
|
295
|
+
* Keys are server names, values are server configurations.
|
|
296
|
+
*/
|
|
297
|
+
mcpServers?: Record<string, MCPServerConfig>;
|
|
298
|
+
/**
|
|
299
|
+
* Custom agent configurations for the session.
|
|
300
|
+
*/
|
|
301
|
+
customAgents?: CustomAgentConfig[];
|
|
196
302
|
}
|
|
197
303
|
/**
|
|
198
304
|
* Configuration for resuming a session
|
|
199
305
|
*/
|
|
200
|
-
export type ResumeSessionConfig = Pick<SessionConfig, "tools" | "provider" | "streaming">;
|
|
306
|
+
export type ResumeSessionConfig = Pick<SessionConfig, "tools" | "provider" | "streaming" | "onPermissionRequest" | "mcpServers" | "customAgents">;
|
|
201
307
|
/**
|
|
202
308
|
* Configuration for a custom API provider.
|
|
203
309
|
*/
|
|
@@ -275,3 +381,4 @@ export interface SessionMetadata {
|
|
|
275
381
|
summary?: string;
|
|
276
382
|
isRemote: boolean;
|
|
277
383
|
}
|
|
384
|
+
export {};
|
package/dist/session.js
CHANGED
|
@@ -1,12 +1,35 @@
|
|
|
1
1
|
class CopilotSession {
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new CopilotSession instance.
|
|
4
|
+
*
|
|
5
|
+
* @param sessionId - The unique identifier for this session
|
|
6
|
+
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
7
|
+
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
8
|
+
*/
|
|
2
9
|
constructor(sessionId, connection) {
|
|
3
10
|
this.sessionId = sessionId;
|
|
4
11
|
this.connection = connection;
|
|
5
12
|
}
|
|
6
13
|
eventHandlers = /* @__PURE__ */ new Set();
|
|
7
14
|
toolHandlers = /* @__PURE__ */ new Map();
|
|
15
|
+
permissionHandler;
|
|
8
16
|
/**
|
|
9
|
-
*
|
|
17
|
+
* Sends a message to this session and waits for the response.
|
|
18
|
+
*
|
|
19
|
+
* The message is processed asynchronously. Subscribe to events via {@link on}
|
|
20
|
+
* to receive streaming responses and other session events.
|
|
21
|
+
*
|
|
22
|
+
* @param options - The message options including the prompt and optional attachments
|
|
23
|
+
* @returns A promise that resolves with the message ID of the response
|
|
24
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const messageId = await session.send({
|
|
29
|
+
* prompt: "Explain this code",
|
|
30
|
+
* attachments: [{ type: "file", path: "./src/index.ts" }]
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
10
33
|
*/
|
|
11
34
|
async send(options) {
|
|
12
35
|
const response = await this.connection.sendRequest("session.send", {
|
|
@@ -18,8 +41,30 @@ class CopilotSession {
|
|
|
18
41
|
return response.messageId;
|
|
19
42
|
}
|
|
20
43
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
44
|
+
* Subscribes to events from this session.
|
|
45
|
+
*
|
|
46
|
+
* Events include assistant messages, tool executions, errors, and session state changes.
|
|
47
|
+
* Multiple handlers can be registered and will all receive events.
|
|
48
|
+
*
|
|
49
|
+
* @param handler - A callback function that receives session events
|
|
50
|
+
* @returns A function that, when called, unsubscribes the handler
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const unsubscribe = session.on((event) => {
|
|
55
|
+
* switch (event.type) {
|
|
56
|
+
* case "assistant.message":
|
|
57
|
+
* console.log("Assistant:", event.data.content);
|
|
58
|
+
* break;
|
|
59
|
+
* case "session.error":
|
|
60
|
+
* console.error("Error:", event.data.message);
|
|
61
|
+
* break;
|
|
62
|
+
* }
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // Later, to stop receiving events:
|
|
66
|
+
* unsubscribe();
|
|
67
|
+
* ```
|
|
23
68
|
*/
|
|
24
69
|
on(handler) {
|
|
25
70
|
this.eventHandlers.add(handler);
|
|
@@ -28,7 +73,10 @@ class CopilotSession {
|
|
|
28
73
|
};
|
|
29
74
|
}
|
|
30
75
|
/**
|
|
31
|
-
*
|
|
76
|
+
* Dispatches an event to all registered handlers.
|
|
77
|
+
*
|
|
78
|
+
* @param event - The session event to dispatch
|
|
79
|
+
* @internal This method is for internal use by the SDK.
|
|
32
80
|
*/
|
|
33
81
|
_dispatchEvent(event) {
|
|
34
82
|
for (const handler of this.eventHandlers) {
|
|
@@ -38,6 +86,15 @@ class CopilotSession {
|
|
|
38
86
|
}
|
|
39
87
|
}
|
|
40
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Registers custom tool handlers for this session.
|
|
91
|
+
*
|
|
92
|
+
* Tools allow the assistant to execute custom functions. When the assistant
|
|
93
|
+
* invokes a tool, the corresponding handler is called with the tool arguments.
|
|
94
|
+
*
|
|
95
|
+
* @param tools - An array of tool definitions with their handlers, or undefined to clear all tools
|
|
96
|
+
* @internal This method is typically called internally when creating a session with tools.
|
|
97
|
+
*/
|
|
41
98
|
registerTools(tools) {
|
|
42
99
|
this.toolHandlers.clear();
|
|
43
100
|
if (!tools) {
|
|
@@ -47,11 +104,66 @@ class CopilotSession {
|
|
|
47
104
|
this.toolHandlers.set(tool.name, tool.handler);
|
|
48
105
|
}
|
|
49
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves a registered tool handler by name.
|
|
109
|
+
*
|
|
110
|
+
* @param name - The name of the tool to retrieve
|
|
111
|
+
* @returns The tool handler if found, or undefined
|
|
112
|
+
* @internal This method is for internal use by the SDK.
|
|
113
|
+
*/
|
|
50
114
|
getToolHandler(name) {
|
|
51
115
|
return this.toolHandlers.get(name);
|
|
52
116
|
}
|
|
53
117
|
/**
|
|
54
|
-
*
|
|
118
|
+
* Registers a handler for permission requests.
|
|
119
|
+
*
|
|
120
|
+
* When the assistant needs permission to perform certain actions (e.g., file operations),
|
|
121
|
+
* this handler is called to approve or deny the request.
|
|
122
|
+
*
|
|
123
|
+
* @param handler - The permission handler function, or undefined to remove the handler
|
|
124
|
+
* @internal This method is typically called internally when creating a session.
|
|
125
|
+
*/
|
|
126
|
+
registerPermissionHandler(handler) {
|
|
127
|
+
this.permissionHandler = handler;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Handles a permission request from the Copilot CLI.
|
|
131
|
+
*
|
|
132
|
+
* @param request - The permission request data from the CLI
|
|
133
|
+
* @returns A promise that resolves with the permission decision
|
|
134
|
+
* @internal This method is for internal use by the SDK.
|
|
135
|
+
*/
|
|
136
|
+
async _handlePermissionRequest(request) {
|
|
137
|
+
if (!this.permissionHandler) {
|
|
138
|
+
return { kind: "denied-no-approval-rule-and-could-not-request-from-user" };
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const result = await this.permissionHandler(request, {
|
|
142
|
+
sessionId: this.sessionId
|
|
143
|
+
});
|
|
144
|
+
return result;
|
|
145
|
+
} catch (_error) {
|
|
146
|
+
return { kind: "denied-no-approval-rule-and-could-not-request-from-user" };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Retrieves all events and messages from this session's history.
|
|
151
|
+
*
|
|
152
|
+
* This returns the complete conversation history including user messages,
|
|
153
|
+
* assistant responses, tool executions, and other session events.
|
|
154
|
+
*
|
|
155
|
+
* @returns A promise that resolves with an array of all session events
|
|
156
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const events = await session.getMessages();
|
|
161
|
+
* for (const event of events) {
|
|
162
|
+
* if (event.type === "assistant.message") {
|
|
163
|
+
* console.log("Assistant:", event.data.content);
|
|
164
|
+
* }
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
55
167
|
*/
|
|
56
168
|
async getMessages() {
|
|
57
169
|
const response = await this.connection.sendRequest("session.getMessages", {
|
|
@@ -60,7 +172,20 @@ class CopilotSession {
|
|
|
60
172
|
return response.events;
|
|
61
173
|
}
|
|
62
174
|
/**
|
|
63
|
-
*
|
|
175
|
+
* Destroys this session and releases all associated resources.
|
|
176
|
+
*
|
|
177
|
+
* After calling this method, the session can no longer be used. All event
|
|
178
|
+
* handlers and tool handlers are cleared. To continue the conversation,
|
|
179
|
+
* use {@link CopilotClient.resumeSession} with the session ID.
|
|
180
|
+
*
|
|
181
|
+
* @returns A promise that resolves when the session is destroyed
|
|
182
|
+
* @throws Error if the connection fails
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* // Clean up when done
|
|
187
|
+
* await session.destroy();
|
|
188
|
+
* ```
|
|
64
189
|
*/
|
|
65
190
|
async destroy() {
|
|
66
191
|
await this.connection.sendRequest("session.destroy", {
|
|
@@ -68,9 +193,27 @@ class CopilotSession {
|
|
|
68
193
|
});
|
|
69
194
|
this.eventHandlers.clear();
|
|
70
195
|
this.toolHandlers.clear();
|
|
196
|
+
this.permissionHandler = void 0;
|
|
71
197
|
}
|
|
72
198
|
/**
|
|
73
|
-
*
|
|
199
|
+
* Aborts the currently processing message in this session.
|
|
200
|
+
*
|
|
201
|
+
* Use this to cancel a long-running request. The session remains valid
|
|
202
|
+
* and can continue to be used for new messages.
|
|
203
|
+
*
|
|
204
|
+
* @returns A promise that resolves when the abort request is acknowledged
|
|
205
|
+
* @throws Error if the session has been destroyed or the connection fails
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* // Start a long-running request
|
|
210
|
+
* const messagePromise = session.send({ prompt: "Write a very long story..." });
|
|
211
|
+
*
|
|
212
|
+
* // Abort after 5 seconds
|
|
213
|
+
* setTimeout(async () => {
|
|
214
|
+
* await session.abort();
|
|
215
|
+
* }, 5000);
|
|
216
|
+
* ```
|
|
74
217
|
*/
|
|
75
218
|
async abort() {
|
|
76
219
|
await this.connection.sendRequest("session.abort", {
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"type": "git",
|
|
5
5
|
"url": "https://github.com/github/copilot-sdk.git"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.1.10
|
|
7
|
+
"version": "0.1.10",
|
|
8
8
|
"description": "TypeScript SDK for programmatic control of GitHub Copilot CLI via JSON-RPC",
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"lint:fix": "eslint --fix \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
27
27
|
"typecheck": "tsc --noEmit",
|
|
28
28
|
"generate:session-types": "tsx scripts/generate-session-types.ts",
|
|
29
|
+
"update:protocol-version": "tsx scripts/generate-protocol-version.ts",
|
|
29
30
|
"prepublishOnly": "npm run build",
|
|
30
31
|
"package": "npm run clean && npm run build && node scripts/set-version.js && npm pack && npm version 0.1.0 --no-git-tag-version --allow-same-version"
|
|
31
32
|
},
|
|
@@ -39,12 +40,12 @@
|
|
|
39
40
|
"author": "GitHub",
|
|
40
41
|
"license": "MIT",
|
|
41
42
|
"dependencies": {
|
|
42
|
-
"@github/copilot": "^0.0.
|
|
43
|
+
"@github/copilot": "^0.0.382-0",
|
|
43
44
|
"vscode-jsonrpc": "^8.2.1",
|
|
44
45
|
"zod": "^4.3.5"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
|
-
"@types/node": "^22.
|
|
48
|
+
"@types/node": "^22.19.6",
|
|
48
49
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
49
50
|
"@typescript-eslint/parser": "^8.0.0",
|
|
50
51
|
"esbuild": "^0.27.0",
|