@github/copilot-sdk 0.1.33-unstable.1 → 0.2.1-preview.0
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/README.md +276 -6
- package/dist/cjs/client.js +1329 -0
- package/dist/cjs/extension.js +45 -0
- package/dist/cjs/generated/rpc.js +123 -0
- package/dist/cjs/generated/session-events.js +16 -0
- package/dist/cjs/index.js +38 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sdkProtocolVersion.js +33 -0
- package/dist/cjs/session.js +774 -0
- package/dist/cjs/telemetry.js +35 -0
- package/dist/cjs/types.js +49 -0
- package/dist/client.d.ts +2 -4
- package/dist/client.js +216 -88
- package/dist/extension.d.ts +6 -7
- package/dist/extension.js +5 -1
- package/dist/generated/rpc.d.ts +540 -1
- package/dist/generated/rpc.js +41 -2
- package/dist/generated/session-events.d.ts +829 -25
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- package/dist/session.d.ts +81 -4
- package/dist/session.js +231 -8
- package/dist/telemetry.d.ts +14 -0
- package/dist/telemetry.js +11 -0
- package/dist/types.d.ts +340 -6
- package/dist/types.js +15 -0
- package/docs/agent-author.md +0 -2
- package/docs/examples.md +2 -15
- package/docs/extensions.md +0 -2
- package/package.json +19 -7
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var telemetry_exports = {};
|
|
20
|
+
__export(telemetry_exports, {
|
|
21
|
+
getTraceContext: () => getTraceContext
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(telemetry_exports);
|
|
24
|
+
async function getTraceContext(provider) {
|
|
25
|
+
if (!provider) return {};
|
|
26
|
+
try {
|
|
27
|
+
return await provider() ?? {};
|
|
28
|
+
} catch {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
getTraceContext
|
|
35
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var types_exports = {};
|
|
20
|
+
__export(types_exports, {
|
|
21
|
+
SYSTEM_PROMPT_SECTIONS: () => SYSTEM_PROMPT_SECTIONS,
|
|
22
|
+
approveAll: () => approveAll,
|
|
23
|
+
defineTool: () => defineTool
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(types_exports);
|
|
26
|
+
function defineTool(name, config) {
|
|
27
|
+
return { name, ...config };
|
|
28
|
+
}
|
|
29
|
+
const SYSTEM_PROMPT_SECTIONS = {
|
|
30
|
+
identity: { description: "Agent identity preamble and mode statement" },
|
|
31
|
+
tone: { description: "Response style, conciseness rules, output formatting preferences" },
|
|
32
|
+
tool_efficiency: { description: "Tool usage patterns, parallel calling, batching guidelines" },
|
|
33
|
+
environment_context: { description: "CWD, OS, git root, directory listing, available tools" },
|
|
34
|
+
code_change_rules: { description: "Coding rules, linting/testing, ecosystem tools, style" },
|
|
35
|
+
guidelines: { description: "Tips, behavioral best practices, behavioral guidelines" },
|
|
36
|
+
safety: { description: "Environment limitations, prohibited actions, security policies" },
|
|
37
|
+
tool_instructions: { description: "Per-tool usage instructions" },
|
|
38
|
+
custom_instructions: { description: "Repository and organization custom instructions" },
|
|
39
|
+
last_instructions: {
|
|
40
|
+
description: "End-of-prompt instructions: parallel tool calling, persistence, task completion"
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const approveAll = () => ({ kind: "approved" });
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
SYSTEM_PROMPT_SECTIONS,
|
|
47
|
+
approveAll,
|
|
48
|
+
defineTool
|
|
49
|
+
});
|
package/dist/client.d.ts
CHANGED
|
@@ -47,6 +47,7 @@ export declare class CopilotClient {
|
|
|
47
47
|
private isExternalServer;
|
|
48
48
|
private forceStopping;
|
|
49
49
|
private onListModels?;
|
|
50
|
+
private onGetTraceContext?;
|
|
50
51
|
private modelsCache;
|
|
51
52
|
private modelsCacheLock;
|
|
52
53
|
private sessionLifecycleHandlers;
|
|
@@ -422,6 +423,7 @@ export declare class CopilotClient {
|
|
|
422
423
|
private handleSessionLifecycleNotification;
|
|
423
424
|
private handleUserInputRequest;
|
|
424
425
|
private handleHooksInvoke;
|
|
426
|
+
private handleSystemMessageTransform;
|
|
425
427
|
/**
|
|
426
428
|
* Handles a v2-style tool.call RPC request from the server.
|
|
427
429
|
* Looks up the session and tool handler, executes it, and returns the result
|
|
@@ -434,8 +436,4 @@ export declare class CopilotClient {
|
|
|
434
436
|
private handlePermissionRequestV2;
|
|
435
437
|
private normalizeToolResultV2;
|
|
436
438
|
private isToolResultObject;
|
|
437
|
-
/**
|
|
438
|
-
* Attempt to reconnect to the server
|
|
439
|
-
*/
|
|
440
|
-
private reconnect;
|
|
441
439
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import { existsSync } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
3
5
|
import { Socket } from "node:net";
|
|
4
6
|
import { dirname, join } from "node:path";
|
|
5
7
|
import { fileURLToPath } from "node:url";
|
|
@@ -10,7 +12,8 @@ import {
|
|
|
10
12
|
} from "vscode-jsonrpc/node.js";
|
|
11
13
|
import { createServerRpc } from "./generated/rpc.js";
|
|
12
14
|
import { getSdkProtocolVersion } from "./sdkProtocolVersion.js";
|
|
13
|
-
import { CopilotSession } from "./session.js";
|
|
15
|
+
import { CopilotSession, NO_RESULT_PERMISSION_V2_ERROR } from "./session.js";
|
|
16
|
+
import { getTraceContext } from "./telemetry.js";
|
|
14
17
|
const MIN_PROTOCOL_VERSION = 2;
|
|
15
18
|
function isZodSchema(value) {
|
|
16
19
|
return value != null && typeof value === "object" && "toJSONSchema" in value && typeof value.toJSONSchema === "function";
|
|
@@ -22,6 +25,30 @@ function toJsonSchema(parameters) {
|
|
|
22
25
|
}
|
|
23
26
|
return parameters;
|
|
24
27
|
}
|
|
28
|
+
function extractTransformCallbacks(systemMessage) {
|
|
29
|
+
if (!systemMessage || systemMessage.mode !== "customize" || !systemMessage.sections) {
|
|
30
|
+
return { wirePayload: systemMessage, transformCallbacks: void 0 };
|
|
31
|
+
}
|
|
32
|
+
const transformCallbacks = /* @__PURE__ */ new Map();
|
|
33
|
+
const wireSections = {};
|
|
34
|
+
for (const [sectionId, override] of Object.entries(systemMessage.sections)) {
|
|
35
|
+
if (!override) continue;
|
|
36
|
+
if (typeof override.action === "function") {
|
|
37
|
+
transformCallbacks.set(sectionId, override.action);
|
|
38
|
+
wireSections[sectionId] = { action: "transform" };
|
|
39
|
+
} else {
|
|
40
|
+
wireSections[sectionId] = { action: override.action, content: override.content };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (transformCallbacks.size === 0) {
|
|
44
|
+
return { wirePayload: systemMessage, transformCallbacks: void 0 };
|
|
45
|
+
}
|
|
46
|
+
const wirePayload = {
|
|
47
|
+
...systemMessage,
|
|
48
|
+
sections: wireSections
|
|
49
|
+
};
|
|
50
|
+
return { wirePayload, transformCallbacks };
|
|
51
|
+
}
|
|
25
52
|
function getNodeExecPath() {
|
|
26
53
|
if (process.versions.bun) {
|
|
27
54
|
return "node";
|
|
@@ -29,9 +56,22 @@ function getNodeExecPath() {
|
|
|
29
56
|
return process.execPath;
|
|
30
57
|
}
|
|
31
58
|
function getBundledCliPath() {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
59
|
+
if (typeof import.meta.resolve === "function") {
|
|
60
|
+
const sdkUrl = import.meta.resolve("@github/copilot/sdk");
|
|
61
|
+
const sdkPath = fileURLToPath(sdkUrl);
|
|
62
|
+
return join(dirname(dirname(sdkPath)), "index.js");
|
|
63
|
+
}
|
|
64
|
+
const req = createRequire(__filename);
|
|
65
|
+
const searchPaths = req.resolve.paths("@github/copilot") ?? [];
|
|
66
|
+
for (const base of searchPaths) {
|
|
67
|
+
const candidate = join(base, "@github", "copilot", "index.js");
|
|
68
|
+
if (existsSync(candidate)) {
|
|
69
|
+
return candidate;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Could not find @github/copilot package. Searched ${searchPaths.length} paths. Ensure it is installed, or pass cliPath/cliUrl to CopilotClient.`
|
|
74
|
+
);
|
|
35
75
|
}
|
|
36
76
|
class CopilotClient {
|
|
37
77
|
cliProcess = null;
|
|
@@ -47,6 +87,7 @@ class CopilotClient {
|
|
|
47
87
|
isExternalServer = false;
|
|
48
88
|
forceStopping = false;
|
|
49
89
|
onListModels;
|
|
90
|
+
onGetTraceContext;
|
|
50
91
|
modelsCache = null;
|
|
51
92
|
modelsCacheLock = Promise.resolve();
|
|
52
93
|
sessionLifecycleHandlers = /* @__PURE__ */ new Set();
|
|
@@ -113,8 +154,9 @@ class CopilotClient {
|
|
|
113
154
|
this.isExternalServer = true;
|
|
114
155
|
}
|
|
115
156
|
this.onListModels = options.onListModels;
|
|
157
|
+
this.onGetTraceContext = options.onGetTraceContext;
|
|
116
158
|
this.options = {
|
|
117
|
-
cliPath: options.cliPath || getBundledCliPath(),
|
|
159
|
+
cliPath: options.cliUrl ? void 0 : options.cliPath || getBundledCliPath(),
|
|
118
160
|
cliArgs: options.cliArgs ?? [],
|
|
119
161
|
cwd: options.cwd ?? process.cwd(),
|
|
120
162
|
port: options.port || 0,
|
|
@@ -124,11 +166,12 @@ class CopilotClient {
|
|
|
124
166
|
cliUrl: options.cliUrl,
|
|
125
167
|
logLevel: options.logLevel || "debug",
|
|
126
168
|
autoStart: options.autoStart ?? true,
|
|
127
|
-
autoRestart:
|
|
169
|
+
autoRestart: false,
|
|
128
170
|
env: options.env ?? process.env,
|
|
129
171
|
githubToken: options.githubToken,
|
|
130
172
|
// Default useLoggedInUser to false when githubToken is provided, otherwise true
|
|
131
|
-
useLoggedInUser: options.useLoggedInUser ?? (options.githubToken ? false : true)
|
|
173
|
+
useLoggedInUser: options.useLoggedInUser ?? (options.githubToken ? false : true),
|
|
174
|
+
telemetry: options.telemetry
|
|
132
175
|
};
|
|
133
176
|
}
|
|
134
177
|
/**
|
|
@@ -380,38 +423,15 @@ class CopilotClient {
|
|
|
380
423
|
throw new Error("Client not connected. Call start() first.");
|
|
381
424
|
}
|
|
382
425
|
}
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
sessionId
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
description: tool.description,
|
|
391
|
-
parameters: toJsonSchema(tool.parameters),
|
|
392
|
-
overridesBuiltInTool: tool.overridesBuiltInTool
|
|
393
|
-
})),
|
|
394
|
-
systemMessage: config.systemMessage,
|
|
395
|
-
availableTools: config.availableTools,
|
|
396
|
-
excludedTools: config.excludedTools,
|
|
397
|
-
provider: config.provider,
|
|
398
|
-
requestPermission: true,
|
|
399
|
-
requestUserInput: !!config.onUserInputRequest,
|
|
400
|
-
hooks: !!(config.hooks && Object.values(config.hooks).some(Boolean)),
|
|
401
|
-
workingDirectory: config.workingDirectory,
|
|
402
|
-
streaming: config.streaming,
|
|
403
|
-
mcpServers: config.mcpServers,
|
|
404
|
-
envValueMode: "direct",
|
|
405
|
-
customAgents: config.customAgents,
|
|
406
|
-
agent: config.agent,
|
|
407
|
-
configDir: config.configDir,
|
|
408
|
-
skillDirectories: config.skillDirectories,
|
|
409
|
-
disabledSkills: config.disabledSkills,
|
|
410
|
-
infiniteSessions: config.infiniteSessions
|
|
411
|
-
});
|
|
412
|
-
const { sessionId, workspacePath } = response;
|
|
413
|
-
const session = new CopilotSession(sessionId, this.connection, workspacePath);
|
|
426
|
+
const sessionId = config.sessionId ?? randomUUID();
|
|
427
|
+
const session = new CopilotSession(
|
|
428
|
+
sessionId,
|
|
429
|
+
this.connection,
|
|
430
|
+
void 0,
|
|
431
|
+
this.onGetTraceContext
|
|
432
|
+
);
|
|
414
433
|
session.registerTools(config.tools);
|
|
434
|
+
session.registerCommands(config.commands);
|
|
415
435
|
session.registerPermissionHandler(config.onPermissionRequest);
|
|
416
436
|
if (config.onUserInputRequest) {
|
|
417
437
|
session.registerUserInputHandler(config.onUserInputRequest);
|
|
@@ -419,7 +439,59 @@ class CopilotClient {
|
|
|
419
439
|
if (config.hooks) {
|
|
420
440
|
session.registerHooks(config.hooks);
|
|
421
441
|
}
|
|
442
|
+
const { wirePayload: wireSystemMessage, transformCallbacks } = extractTransformCallbacks(
|
|
443
|
+
config.systemMessage
|
|
444
|
+
);
|
|
445
|
+
if (transformCallbacks) {
|
|
446
|
+
session.registerTransformCallbacks(transformCallbacks);
|
|
447
|
+
}
|
|
448
|
+
if (config.onEvent) {
|
|
449
|
+
session.on(config.onEvent);
|
|
450
|
+
}
|
|
422
451
|
this.sessions.set(sessionId, session);
|
|
452
|
+
try {
|
|
453
|
+
const response = await this.connection.sendRequest("session.create", {
|
|
454
|
+
...await getTraceContext(this.onGetTraceContext),
|
|
455
|
+
model: config.model,
|
|
456
|
+
sessionId,
|
|
457
|
+
clientName: config.clientName,
|
|
458
|
+
reasoningEffort: config.reasoningEffort,
|
|
459
|
+
tools: config.tools?.map((tool) => ({
|
|
460
|
+
name: tool.name,
|
|
461
|
+
description: tool.description,
|
|
462
|
+
parameters: toJsonSchema(tool.parameters),
|
|
463
|
+
overridesBuiltInTool: tool.overridesBuiltInTool,
|
|
464
|
+
skipPermission: tool.skipPermission
|
|
465
|
+
})),
|
|
466
|
+
commands: config.commands?.map((cmd) => ({
|
|
467
|
+
name: cmd.name,
|
|
468
|
+
description: cmd.description
|
|
469
|
+
})),
|
|
470
|
+
systemMessage: wireSystemMessage,
|
|
471
|
+
availableTools: config.availableTools,
|
|
472
|
+
excludedTools: config.excludedTools,
|
|
473
|
+
provider: config.provider,
|
|
474
|
+
requestPermission: true,
|
|
475
|
+
requestUserInput: !!config.onUserInputRequest,
|
|
476
|
+
hooks: !!(config.hooks && Object.values(config.hooks).some(Boolean)),
|
|
477
|
+
workingDirectory: config.workingDirectory,
|
|
478
|
+
streaming: config.streaming,
|
|
479
|
+
mcpServers: config.mcpServers,
|
|
480
|
+
envValueMode: "direct",
|
|
481
|
+
customAgents: config.customAgents,
|
|
482
|
+
agent: config.agent,
|
|
483
|
+
configDir: config.configDir,
|
|
484
|
+
skillDirectories: config.skillDirectories,
|
|
485
|
+
disabledSkills: config.disabledSkills,
|
|
486
|
+
infiniteSessions: config.infiniteSessions
|
|
487
|
+
});
|
|
488
|
+
const { workspacePath, capabilities } = response;
|
|
489
|
+
session["_workspacePath"] = workspacePath;
|
|
490
|
+
session.setCapabilities(capabilities);
|
|
491
|
+
} catch (e) {
|
|
492
|
+
this.sessions.delete(sessionId);
|
|
493
|
+
throw e;
|
|
494
|
+
}
|
|
423
495
|
return session;
|
|
424
496
|
}
|
|
425
497
|
/**
|
|
@@ -459,39 +531,14 @@ class CopilotClient {
|
|
|
459
531
|
throw new Error("Client not connected. Call start() first.");
|
|
460
532
|
}
|
|
461
533
|
}
|
|
462
|
-
const
|
|
534
|
+
const session = new CopilotSession(
|
|
463
535
|
sessionId,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
availableTools: config.availableTools,
|
|
469
|
-
excludedTools: config.excludedTools,
|
|
470
|
-
tools: config.tools?.map((tool) => ({
|
|
471
|
-
name: tool.name,
|
|
472
|
-
description: tool.description,
|
|
473
|
-
parameters: toJsonSchema(tool.parameters),
|
|
474
|
-
overridesBuiltInTool: tool.overridesBuiltInTool
|
|
475
|
-
})),
|
|
476
|
-
provider: config.provider,
|
|
477
|
-
requestPermission: true,
|
|
478
|
-
requestUserInput: !!config.onUserInputRequest,
|
|
479
|
-
hooks: !!(config.hooks && Object.values(config.hooks).some(Boolean)),
|
|
480
|
-
workingDirectory: config.workingDirectory,
|
|
481
|
-
configDir: config.configDir,
|
|
482
|
-
streaming: config.streaming,
|
|
483
|
-
mcpServers: config.mcpServers,
|
|
484
|
-
envValueMode: "direct",
|
|
485
|
-
customAgents: config.customAgents,
|
|
486
|
-
agent: config.agent,
|
|
487
|
-
skillDirectories: config.skillDirectories,
|
|
488
|
-
disabledSkills: config.disabledSkills,
|
|
489
|
-
infiniteSessions: config.infiniteSessions,
|
|
490
|
-
disableResume: config.disableResume
|
|
491
|
-
});
|
|
492
|
-
const { sessionId: resumedSessionId, workspacePath } = response;
|
|
493
|
-
const session = new CopilotSession(resumedSessionId, this.connection, workspacePath);
|
|
536
|
+
this.connection,
|
|
537
|
+
void 0,
|
|
538
|
+
this.onGetTraceContext
|
|
539
|
+
);
|
|
494
540
|
session.registerTools(config.tools);
|
|
541
|
+
session.registerCommands(config.commands);
|
|
495
542
|
session.registerPermissionHandler(config.onPermissionRequest);
|
|
496
543
|
if (config.onUserInputRequest) {
|
|
497
544
|
session.registerUserInputHandler(config.onUserInputRequest);
|
|
@@ -499,7 +546,60 @@ class CopilotClient {
|
|
|
499
546
|
if (config.hooks) {
|
|
500
547
|
session.registerHooks(config.hooks);
|
|
501
548
|
}
|
|
502
|
-
|
|
549
|
+
const { wirePayload: wireSystemMessage, transformCallbacks } = extractTransformCallbacks(
|
|
550
|
+
config.systemMessage
|
|
551
|
+
);
|
|
552
|
+
if (transformCallbacks) {
|
|
553
|
+
session.registerTransformCallbacks(transformCallbacks);
|
|
554
|
+
}
|
|
555
|
+
if (config.onEvent) {
|
|
556
|
+
session.on(config.onEvent);
|
|
557
|
+
}
|
|
558
|
+
this.sessions.set(sessionId, session);
|
|
559
|
+
try {
|
|
560
|
+
const response = await this.connection.sendRequest("session.resume", {
|
|
561
|
+
...await getTraceContext(this.onGetTraceContext),
|
|
562
|
+
sessionId,
|
|
563
|
+
clientName: config.clientName,
|
|
564
|
+
model: config.model,
|
|
565
|
+
reasoningEffort: config.reasoningEffort,
|
|
566
|
+
systemMessage: wireSystemMessage,
|
|
567
|
+
availableTools: config.availableTools,
|
|
568
|
+
excludedTools: config.excludedTools,
|
|
569
|
+
tools: config.tools?.map((tool) => ({
|
|
570
|
+
name: tool.name,
|
|
571
|
+
description: tool.description,
|
|
572
|
+
parameters: toJsonSchema(tool.parameters),
|
|
573
|
+
overridesBuiltInTool: tool.overridesBuiltInTool,
|
|
574
|
+
skipPermission: tool.skipPermission
|
|
575
|
+
})),
|
|
576
|
+
commands: config.commands?.map((cmd) => ({
|
|
577
|
+
name: cmd.name,
|
|
578
|
+
description: cmd.description
|
|
579
|
+
})),
|
|
580
|
+
provider: config.provider,
|
|
581
|
+
requestPermission: true,
|
|
582
|
+
requestUserInput: !!config.onUserInputRequest,
|
|
583
|
+
hooks: !!(config.hooks && Object.values(config.hooks).some(Boolean)),
|
|
584
|
+
workingDirectory: config.workingDirectory,
|
|
585
|
+
configDir: config.configDir,
|
|
586
|
+
streaming: config.streaming,
|
|
587
|
+
mcpServers: config.mcpServers,
|
|
588
|
+
envValueMode: "direct",
|
|
589
|
+
customAgents: config.customAgents,
|
|
590
|
+
agent: config.agent,
|
|
591
|
+
skillDirectories: config.skillDirectories,
|
|
592
|
+
disabledSkills: config.disabledSkills,
|
|
593
|
+
infiniteSessions: config.infiniteSessions,
|
|
594
|
+
disableResume: config.disableResume
|
|
595
|
+
});
|
|
596
|
+
const { workspacePath, capabilities } = response;
|
|
597
|
+
session["_workspacePath"] = workspacePath;
|
|
598
|
+
session.setCapabilities(capabilities);
|
|
599
|
+
} catch (e) {
|
|
600
|
+
this.sessions.delete(sessionId);
|
|
601
|
+
throw e;
|
|
602
|
+
}
|
|
503
603
|
return session;
|
|
504
604
|
}
|
|
505
605
|
/**
|
|
@@ -802,6 +902,27 @@ class CopilotClient {
|
|
|
802
902
|
if (this.options.githubToken) {
|
|
803
903
|
envWithoutNodeDebug.COPILOT_SDK_AUTH_TOKEN = this.options.githubToken;
|
|
804
904
|
}
|
|
905
|
+
if (!this.options.cliPath) {
|
|
906
|
+
throw new Error(
|
|
907
|
+
"Path to Copilot CLI is required. Please provide it via the cliPath option, or use cliUrl to rely on a remote CLI."
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
if (this.options.telemetry) {
|
|
911
|
+
const t = this.options.telemetry;
|
|
912
|
+
envWithoutNodeDebug.COPILOT_OTEL_ENABLED = "true";
|
|
913
|
+
if (t.otlpEndpoint !== void 0)
|
|
914
|
+
envWithoutNodeDebug.OTEL_EXPORTER_OTLP_ENDPOINT = t.otlpEndpoint;
|
|
915
|
+
if (t.filePath !== void 0)
|
|
916
|
+
envWithoutNodeDebug.COPILOT_OTEL_FILE_EXPORTER_PATH = t.filePath;
|
|
917
|
+
if (t.exporterType !== void 0)
|
|
918
|
+
envWithoutNodeDebug.COPILOT_OTEL_EXPORTER_TYPE = t.exporterType;
|
|
919
|
+
if (t.sourceName !== void 0)
|
|
920
|
+
envWithoutNodeDebug.COPILOT_OTEL_SOURCE_NAME = t.sourceName;
|
|
921
|
+
if (t.captureContent !== void 0)
|
|
922
|
+
envWithoutNodeDebug.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = String(
|
|
923
|
+
t.captureContent
|
|
924
|
+
);
|
|
925
|
+
}
|
|
805
926
|
if (!existsSync(this.options.cliPath)) {
|
|
806
927
|
throw new Error(
|
|
807
928
|
`Copilot CLI not found at ${this.options.cliPath}. Ensure @github/copilot is installed.`
|
|
@@ -901,8 +1022,6 @@ stderr: ${stderrOutput}`
|
|
|
901
1022
|
} else {
|
|
902
1023
|
reject(new Error(`CLI server exited with code ${code}`));
|
|
903
1024
|
}
|
|
904
|
-
} else if (this.options.autoRestart && this.state === "connected") {
|
|
905
|
-
void this.reconnect();
|
|
906
1025
|
}
|
|
907
1026
|
});
|
|
908
1027
|
setTimeout(() => {
|
|
@@ -1007,12 +1126,15 @@ stderr: ${stderrOutput}`
|
|
|
1007
1126
|
"hooks.invoke",
|
|
1008
1127
|
async (params) => await this.handleHooksInvoke(params)
|
|
1009
1128
|
);
|
|
1129
|
+
this.connection.onRequest(
|
|
1130
|
+
"systemMessage.transform",
|
|
1131
|
+
async (params) => await this.handleSystemMessageTransform(params)
|
|
1132
|
+
);
|
|
1010
1133
|
this.connection.onClose(() => {
|
|
1011
|
-
|
|
1012
|
-
void this.reconnect();
|
|
1013
|
-
}
|
|
1134
|
+
this.state = "disconnected";
|
|
1014
1135
|
});
|
|
1015
1136
|
this.connection.onError((_error) => {
|
|
1137
|
+
this.state = "disconnected";
|
|
1016
1138
|
});
|
|
1017
1139
|
}
|
|
1018
1140
|
handleSessionEventNotification(notification) {
|
|
@@ -1071,6 +1193,16 @@ stderr: ${stderrOutput}`
|
|
|
1071
1193
|
const output = await session._handleHooksInvoke(params.hookType, params.input);
|
|
1072
1194
|
return { output };
|
|
1073
1195
|
}
|
|
1196
|
+
async handleSystemMessageTransform(params) {
|
|
1197
|
+
if (!params || typeof params.sessionId !== "string" || !params.sections || typeof params.sections !== "object") {
|
|
1198
|
+
throw new Error("Invalid systemMessage.transform payload");
|
|
1199
|
+
}
|
|
1200
|
+
const session = this.sessions.get(params.sessionId);
|
|
1201
|
+
if (!session) {
|
|
1202
|
+
throw new Error(`Session not found: ${params.sessionId}`);
|
|
1203
|
+
}
|
|
1204
|
+
return await session._handleSystemMessageTransform(params.sections);
|
|
1205
|
+
}
|
|
1074
1206
|
// ========================================================================
|
|
1075
1207
|
// Protocol v2 backward-compatibility adapters
|
|
1076
1208
|
// ========================================================================
|
|
@@ -1099,11 +1231,15 @@ stderr: ${stderrOutput}`
|
|
|
1099
1231
|
};
|
|
1100
1232
|
}
|
|
1101
1233
|
try {
|
|
1234
|
+
const traceparent = params.traceparent;
|
|
1235
|
+
const tracestate = params.tracestate;
|
|
1102
1236
|
const invocation = {
|
|
1103
1237
|
sessionId: params.sessionId,
|
|
1104
1238
|
toolCallId: params.toolCallId,
|
|
1105
1239
|
toolName: params.toolName,
|
|
1106
|
-
arguments: params.arguments
|
|
1240
|
+
arguments: params.arguments,
|
|
1241
|
+
traceparent,
|
|
1242
|
+
tracestate
|
|
1107
1243
|
};
|
|
1108
1244
|
const result = await handler(params.arguments, invocation);
|
|
1109
1245
|
return { result: this.normalizeToolResultV2(result) };
|
|
@@ -1133,7 +1269,10 @@ stderr: ${stderrOutput}`
|
|
|
1133
1269
|
try {
|
|
1134
1270
|
const result = await session._handlePermissionRequestV2(params.permissionRequest);
|
|
1135
1271
|
return { result };
|
|
1136
|
-
} catch (
|
|
1272
|
+
} catch (error) {
|
|
1273
|
+
if (error instanceof Error && error.message === NO_RESULT_PERMISSION_V2_ERROR) {
|
|
1274
|
+
throw error;
|
|
1275
|
+
}
|
|
1137
1276
|
return {
|
|
1138
1277
|
result: {
|
|
1139
1278
|
kind: "denied-no-approval-rule-and-could-not-request-from-user"
|
|
@@ -1163,17 +1302,6 @@ stderr: ${stderrOutput}`
|
|
|
1163
1302
|
isToolResultObject(value) {
|
|
1164
1303
|
return typeof value === "object" && value !== null && "textResultForLlm" in value && typeof value.textResultForLlm === "string" && "resultType" in value;
|
|
1165
1304
|
}
|
|
1166
|
-
/**
|
|
1167
|
-
* Attempt to reconnect to the server
|
|
1168
|
-
*/
|
|
1169
|
-
async reconnect() {
|
|
1170
|
-
this.state = "disconnected";
|
|
1171
|
-
try {
|
|
1172
|
-
await this.stop();
|
|
1173
|
-
await this.start();
|
|
1174
|
-
} catch (_error) {
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
1305
|
}
|
|
1178
1306
|
export {
|
|
1179
1307
|
CopilotClient
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { CopilotSession } from "./session.js";
|
|
2
|
-
import type { ResumeSessionConfig } from "./types.js";
|
|
2
|
+
import type { PermissionHandler, ResumeSessionConfig } from "./types.js";
|
|
3
|
+
export type JoinSessionConfig = Omit<ResumeSessionConfig, "onPermissionRequest"> & {
|
|
4
|
+
onPermissionRequest?: PermissionHandler;
|
|
5
|
+
};
|
|
3
6
|
/**
|
|
4
7
|
* Joins the current foreground session.
|
|
5
8
|
*
|
|
@@ -8,13 +11,9 @@ import type { ResumeSessionConfig } from "./types.js";
|
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```typescript
|
|
11
|
-
* import { approveAll } from "@github/copilot-sdk";
|
|
12
14
|
* import { joinSession } from "@github/copilot-sdk/extension";
|
|
13
15
|
*
|
|
14
|
-
* const session = await joinSession({
|
|
15
|
-
* onPermissionRequest: approveAll,
|
|
16
|
-
* tools: [myTool],
|
|
17
|
-
* });
|
|
16
|
+
* const session = await joinSession({ tools: [myTool] });
|
|
18
17
|
* ```
|
|
19
18
|
*/
|
|
20
|
-
export declare function joinSession(config
|
|
19
|
+
export declare function joinSession(config?: JoinSessionConfig): Promise<CopilotSession>;
|
package/dist/extension.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { CopilotClient } from "./client.js";
|
|
2
|
-
|
|
2
|
+
const defaultJoinSessionPermissionHandler = () => ({
|
|
3
|
+
kind: "no-result"
|
|
4
|
+
});
|
|
5
|
+
async function joinSession(config = {}) {
|
|
3
6
|
const sessionId = process.env.SESSION_ID;
|
|
4
7
|
if (!sessionId) {
|
|
5
8
|
throw new Error(
|
|
@@ -9,6 +12,7 @@ async function joinSession(config) {
|
|
|
9
12
|
const client = new CopilotClient({ isChildProcess: true });
|
|
10
13
|
return client.resumeSession(sessionId, {
|
|
11
14
|
...config,
|
|
15
|
+
onPermissionRequest: config.onPermissionRequest ?? defaultJoinSessionPermissionHandler,
|
|
12
16
|
disableResume: config.disableResume ?? true
|
|
13
17
|
});
|
|
14
18
|
}
|