@github/copilot-sdk 0.1.33-preview.1 → 0.1.33-preview.3
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 +142 -4
- package/dist/cjs/client.js +1267 -0
- package/dist/cjs/extension.js +45 -0
- package/dist/cjs/generated/rpc.js +91 -0
- package/dist/cjs/generated/session-events.js +16 -0
- package/dist/cjs/index.js +36 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sdkProtocolVersion.js +33 -0
- package/dist/cjs/session.js +581 -0
- package/dist/cjs/telemetry.js +35 -0
- package/dist/cjs/types.js +33 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +57 -7
- package/dist/generated/rpc.d.ts +15 -0
- package/dist/generated/rpc.js +3 -0
- package/dist/index.d.ts +1 -1
- package/dist/session.d.ts +9 -3
- package/dist/session.js +25 -6
- package/dist/telemetry.d.ts +14 -0
- package/dist/telemetry.js +11 -0
- package/dist/types.d.ts +73 -1
- package/package.json +18 -6
package/dist/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
4
5
|
import { Socket } from "node:net";
|
|
5
6
|
import { dirname, join } from "node:path";
|
|
6
7
|
import { fileURLToPath } from "node:url";
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
import { createServerRpc } from "./generated/rpc.js";
|
|
13
14
|
import { getSdkProtocolVersion } from "./sdkProtocolVersion.js";
|
|
14
15
|
import { CopilotSession, NO_RESULT_PERMISSION_V2_ERROR } from "./session.js";
|
|
16
|
+
import { getTraceContext } from "./telemetry.js";
|
|
15
17
|
const MIN_PROTOCOL_VERSION = 2;
|
|
16
18
|
function isZodSchema(value) {
|
|
17
19
|
return value != null && typeof value === "object" && "toJSONSchema" in value && typeof value.toJSONSchema === "function";
|
|
@@ -30,9 +32,22 @@ function getNodeExecPath() {
|
|
|
30
32
|
return process.execPath;
|
|
31
33
|
}
|
|
32
34
|
function getBundledCliPath() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
if (typeof import.meta.resolve === "function") {
|
|
36
|
+
const sdkUrl = import.meta.resolve("@github/copilot/sdk");
|
|
37
|
+
const sdkPath = fileURLToPath(sdkUrl);
|
|
38
|
+
return join(dirname(dirname(sdkPath)), "index.js");
|
|
39
|
+
}
|
|
40
|
+
const req = createRequire(__filename);
|
|
41
|
+
const searchPaths = req.resolve.paths("@github/copilot") ?? [];
|
|
42
|
+
for (const base of searchPaths) {
|
|
43
|
+
const candidate = join(base, "@github", "copilot", "index.js");
|
|
44
|
+
if (existsSync(candidate)) {
|
|
45
|
+
return candidate;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
throw new Error(
|
|
49
|
+
`Could not find @github/copilot package. Searched ${searchPaths.length} paths. Ensure it is installed, or pass cliPath/cliUrl to CopilotClient.`
|
|
50
|
+
);
|
|
36
51
|
}
|
|
37
52
|
class CopilotClient {
|
|
38
53
|
cliProcess = null;
|
|
@@ -48,6 +63,7 @@ class CopilotClient {
|
|
|
48
63
|
isExternalServer = false;
|
|
49
64
|
forceStopping = false;
|
|
50
65
|
onListModels;
|
|
66
|
+
onGetTraceContext;
|
|
51
67
|
modelsCache = null;
|
|
52
68
|
modelsCacheLock = Promise.resolve();
|
|
53
69
|
sessionLifecycleHandlers = /* @__PURE__ */ new Set();
|
|
@@ -114,6 +130,7 @@ class CopilotClient {
|
|
|
114
130
|
this.isExternalServer = true;
|
|
115
131
|
}
|
|
116
132
|
this.onListModels = options.onListModels;
|
|
133
|
+
this.onGetTraceContext = options.onGetTraceContext;
|
|
117
134
|
this.options = {
|
|
118
135
|
cliPath: options.cliUrl ? void 0 : options.cliPath || getBundledCliPath(),
|
|
119
136
|
cliArgs: options.cliArgs ?? [],
|
|
@@ -129,7 +146,8 @@ class CopilotClient {
|
|
|
129
146
|
env: options.env ?? process.env,
|
|
130
147
|
githubToken: options.githubToken,
|
|
131
148
|
// Default useLoggedInUser to false when githubToken is provided, otherwise true
|
|
132
|
-
useLoggedInUser: options.useLoggedInUser ?? (options.githubToken ? false : true)
|
|
149
|
+
useLoggedInUser: options.useLoggedInUser ?? (options.githubToken ? false : true),
|
|
150
|
+
telemetry: options.telemetry
|
|
133
151
|
};
|
|
134
152
|
}
|
|
135
153
|
/**
|
|
@@ -382,7 +400,12 @@ class CopilotClient {
|
|
|
382
400
|
}
|
|
383
401
|
}
|
|
384
402
|
const sessionId = config.sessionId ?? randomUUID();
|
|
385
|
-
const session = new CopilotSession(
|
|
403
|
+
const session = new CopilotSession(
|
|
404
|
+
sessionId,
|
|
405
|
+
this.connection,
|
|
406
|
+
void 0,
|
|
407
|
+
this.onGetTraceContext
|
|
408
|
+
);
|
|
386
409
|
session.registerTools(config.tools);
|
|
387
410
|
session.registerPermissionHandler(config.onPermissionRequest);
|
|
388
411
|
if (config.onUserInputRequest) {
|
|
@@ -397,6 +420,7 @@ class CopilotClient {
|
|
|
397
420
|
this.sessions.set(sessionId, session);
|
|
398
421
|
try {
|
|
399
422
|
const response = await this.connection.sendRequest("session.create", {
|
|
423
|
+
...await getTraceContext(this.onGetTraceContext),
|
|
400
424
|
model: config.model,
|
|
401
425
|
sessionId,
|
|
402
426
|
clientName: config.clientName,
|
|
@@ -471,7 +495,12 @@ class CopilotClient {
|
|
|
471
495
|
throw new Error("Client not connected. Call start() first.");
|
|
472
496
|
}
|
|
473
497
|
}
|
|
474
|
-
const session = new CopilotSession(
|
|
498
|
+
const session = new CopilotSession(
|
|
499
|
+
sessionId,
|
|
500
|
+
this.connection,
|
|
501
|
+
void 0,
|
|
502
|
+
this.onGetTraceContext
|
|
503
|
+
);
|
|
475
504
|
session.registerTools(config.tools);
|
|
476
505
|
session.registerPermissionHandler(config.onPermissionRequest);
|
|
477
506
|
if (config.onUserInputRequest) {
|
|
@@ -486,6 +515,7 @@ class CopilotClient {
|
|
|
486
515
|
this.sessions.set(sessionId, session);
|
|
487
516
|
try {
|
|
488
517
|
const response = await this.connection.sendRequest("session.resume", {
|
|
518
|
+
...await getTraceContext(this.onGetTraceContext),
|
|
489
519
|
sessionId,
|
|
490
520
|
clientName: config.clientName,
|
|
491
521
|
model: config.model,
|
|
@@ -829,6 +859,22 @@ class CopilotClient {
|
|
|
829
859
|
"Path to Copilot CLI is required. Please provide it via the cliPath option, or use cliUrl to rely on a remote CLI."
|
|
830
860
|
);
|
|
831
861
|
}
|
|
862
|
+
if (this.options.telemetry) {
|
|
863
|
+
const t = this.options.telemetry;
|
|
864
|
+
envWithoutNodeDebug.COPILOT_OTEL_ENABLED = "true";
|
|
865
|
+
if (t.otlpEndpoint !== void 0)
|
|
866
|
+
envWithoutNodeDebug.OTEL_EXPORTER_OTLP_ENDPOINT = t.otlpEndpoint;
|
|
867
|
+
if (t.filePath !== void 0)
|
|
868
|
+
envWithoutNodeDebug.COPILOT_OTEL_FILE_EXPORTER_PATH = t.filePath;
|
|
869
|
+
if (t.exporterType !== void 0)
|
|
870
|
+
envWithoutNodeDebug.COPILOT_OTEL_EXPORTER_TYPE = t.exporterType;
|
|
871
|
+
if (t.sourceName !== void 0)
|
|
872
|
+
envWithoutNodeDebug.COPILOT_OTEL_SOURCE_NAME = t.sourceName;
|
|
873
|
+
if (t.captureContent !== void 0)
|
|
874
|
+
envWithoutNodeDebug.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = String(
|
|
875
|
+
t.captureContent
|
|
876
|
+
);
|
|
877
|
+
}
|
|
832
878
|
if (!existsSync(this.options.cliPath)) {
|
|
833
879
|
throw new Error(
|
|
834
880
|
`Copilot CLI not found at ${this.options.cliPath}. Ensure @github/copilot is installed.`
|
|
@@ -1123,11 +1169,15 @@ stderr: ${stderrOutput}`
|
|
|
1123
1169
|
};
|
|
1124
1170
|
}
|
|
1125
1171
|
try {
|
|
1172
|
+
const traceparent = params.traceparent;
|
|
1173
|
+
const tracestate = params.tracestate;
|
|
1126
1174
|
const invocation = {
|
|
1127
1175
|
sessionId: params.sessionId,
|
|
1128
1176
|
toolCallId: params.toolCallId,
|
|
1129
1177
|
toolName: params.toolName,
|
|
1130
|
-
arguments: params.arguments
|
|
1178
|
+
arguments: params.arguments,
|
|
1179
|
+
traceparent,
|
|
1180
|
+
tracestate
|
|
1131
1181
|
};
|
|
1132
1182
|
const result = await handler(params.arguments, invocation);
|
|
1133
1183
|
return { result: this.normalizeToolResultV2(result) };
|
package/dist/generated/rpc.d.ts
CHANGED
|
@@ -315,12 +315,14 @@ export interface SessionWorkspaceCreateFileParams {
|
|
|
315
315
|
*/
|
|
316
316
|
content: string;
|
|
317
317
|
}
|
|
318
|
+
/** @experimental */
|
|
318
319
|
export interface SessionFleetStartResult {
|
|
319
320
|
/**
|
|
320
321
|
* Whether fleet mode was successfully activated
|
|
321
322
|
*/
|
|
322
323
|
started: boolean;
|
|
323
324
|
}
|
|
325
|
+
/** @experimental */
|
|
324
326
|
export interface SessionFleetStartParams {
|
|
325
327
|
/**
|
|
326
328
|
* Target session identifier
|
|
@@ -331,6 +333,7 @@ export interface SessionFleetStartParams {
|
|
|
331
333
|
*/
|
|
332
334
|
prompt?: string;
|
|
333
335
|
}
|
|
336
|
+
/** @experimental */
|
|
334
337
|
export interface SessionAgentListResult {
|
|
335
338
|
/**
|
|
336
339
|
* Available custom agents
|
|
@@ -350,12 +353,14 @@ export interface SessionAgentListResult {
|
|
|
350
353
|
description: string;
|
|
351
354
|
}[];
|
|
352
355
|
}
|
|
356
|
+
/** @experimental */
|
|
353
357
|
export interface SessionAgentListParams {
|
|
354
358
|
/**
|
|
355
359
|
* Target session identifier
|
|
356
360
|
*/
|
|
357
361
|
sessionId: string;
|
|
358
362
|
}
|
|
363
|
+
/** @experimental */
|
|
359
364
|
export interface SessionAgentGetCurrentResult {
|
|
360
365
|
/**
|
|
361
366
|
* Currently selected custom agent, or null if using the default agent
|
|
@@ -375,12 +380,14 @@ export interface SessionAgentGetCurrentResult {
|
|
|
375
380
|
description: string;
|
|
376
381
|
} | null;
|
|
377
382
|
}
|
|
383
|
+
/** @experimental */
|
|
378
384
|
export interface SessionAgentGetCurrentParams {
|
|
379
385
|
/**
|
|
380
386
|
* Target session identifier
|
|
381
387
|
*/
|
|
382
388
|
sessionId: string;
|
|
383
389
|
}
|
|
390
|
+
/** @experimental */
|
|
384
391
|
export interface SessionAgentSelectResult {
|
|
385
392
|
/**
|
|
386
393
|
* The newly selected custom agent
|
|
@@ -400,6 +407,7 @@ export interface SessionAgentSelectResult {
|
|
|
400
407
|
description: string;
|
|
401
408
|
};
|
|
402
409
|
}
|
|
410
|
+
/** @experimental */
|
|
403
411
|
export interface SessionAgentSelectParams {
|
|
404
412
|
/**
|
|
405
413
|
* Target session identifier
|
|
@@ -410,14 +418,17 @@ export interface SessionAgentSelectParams {
|
|
|
410
418
|
*/
|
|
411
419
|
name: string;
|
|
412
420
|
}
|
|
421
|
+
/** @experimental */
|
|
413
422
|
export interface SessionAgentDeselectResult {
|
|
414
423
|
}
|
|
424
|
+
/** @experimental */
|
|
415
425
|
export interface SessionAgentDeselectParams {
|
|
416
426
|
/**
|
|
417
427
|
* Target session identifier
|
|
418
428
|
*/
|
|
419
429
|
sessionId: string;
|
|
420
430
|
}
|
|
431
|
+
/** @experimental */
|
|
421
432
|
export interface SessionCompactionCompactResult {
|
|
422
433
|
/**
|
|
423
434
|
* Whether compaction completed successfully
|
|
@@ -432,6 +443,7 @@ export interface SessionCompactionCompactResult {
|
|
|
432
443
|
*/
|
|
433
444
|
messagesRemoved: number;
|
|
434
445
|
}
|
|
446
|
+
/** @experimental */
|
|
435
447
|
export interface SessionCompactionCompactParams {
|
|
436
448
|
/**
|
|
437
449
|
* Target session identifier
|
|
@@ -589,15 +601,18 @@ export declare function createSessionRpc(connection: MessageConnection, sessionI
|
|
|
589
601
|
readFile: (params: Omit<SessionWorkspaceReadFileParams, "sessionId">) => Promise<SessionWorkspaceReadFileResult>;
|
|
590
602
|
createFile: (params: Omit<SessionWorkspaceCreateFileParams, "sessionId">) => Promise<SessionWorkspaceCreateFileResult>;
|
|
591
603
|
};
|
|
604
|
+
/** @experimental */
|
|
592
605
|
fleet: {
|
|
593
606
|
start: (params: Omit<SessionFleetStartParams, "sessionId">) => Promise<SessionFleetStartResult>;
|
|
594
607
|
};
|
|
608
|
+
/** @experimental */
|
|
595
609
|
agent: {
|
|
596
610
|
list: () => Promise<SessionAgentListResult>;
|
|
597
611
|
getCurrent: () => Promise<SessionAgentGetCurrentResult>;
|
|
598
612
|
select: (params: Omit<SessionAgentSelectParams, "sessionId">) => Promise<SessionAgentSelectResult>;
|
|
599
613
|
deselect: () => Promise<SessionAgentDeselectResult>;
|
|
600
614
|
};
|
|
615
|
+
/** @experimental */
|
|
601
616
|
compaction: {
|
|
602
617
|
compact: () => Promise<SessionCompactionCompactResult>;
|
|
603
618
|
};
|
package/dist/generated/rpc.js
CHANGED
|
@@ -32,15 +32,18 @@ function createSessionRpc(connection, sessionId) {
|
|
|
32
32
|
readFile: async (params) => connection.sendRequest("session.workspace.readFile", { sessionId, ...params }),
|
|
33
33
|
createFile: async (params) => connection.sendRequest("session.workspace.createFile", { sessionId, ...params })
|
|
34
34
|
},
|
|
35
|
+
/** @experimental */
|
|
35
36
|
fleet: {
|
|
36
37
|
start: async (params) => connection.sendRequest("session.fleet.start", { sessionId, ...params })
|
|
37
38
|
},
|
|
39
|
+
/** @experimental */
|
|
38
40
|
agent: {
|
|
39
41
|
list: async () => connection.sendRequest("session.agent.list", { sessionId }),
|
|
40
42
|
getCurrent: async () => connection.sendRequest("session.agent.getCurrent", { sessionId }),
|
|
41
43
|
select: async (params) => connection.sendRequest("session.agent.select", { sessionId, ...params }),
|
|
42
44
|
deselect: async () => connection.sendRequest("session.agent.deselect", { sessionId })
|
|
43
45
|
},
|
|
46
|
+
/** @experimental */
|
|
44
47
|
compaction: {
|
|
45
48
|
compact: async () => connection.sendRequest("session.compaction.compact", { sessionId })
|
|
46
49
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
export { CopilotClient } from "./client.js";
|
|
7
7
|
export { CopilotSession, type AssistantMessageEvent } from "./session.js";
|
|
8
8
|
export { defineTool, approveAll } from "./types.js";
|
|
9
|
-
export type { ConnectionState, CopilotClientOptions, CustomAgentConfig, ForegroundSessionInfo, GetAuthStatusResponse, GetStatusResponse, InfiniteSessionConfig, MCPLocalServerConfig, MCPRemoteServerConfig, MCPServerConfig, MessageOptions, ModelBilling, ModelCapabilities, ModelInfo, ModelPolicy, PermissionHandler, PermissionRequest, PermissionRequestResult, ResumeSessionConfig, SessionConfig, SessionEvent, SessionEventHandler, SessionEventPayload, SessionEventType, SessionLifecycleEvent, SessionLifecycleEventType, SessionLifecycleHandler, SessionContext, SessionListFilter, SessionMetadata, SystemMessageAppendConfig, SystemMessageConfig, SystemMessageReplaceConfig, Tool, ToolHandler, ToolInvocation, ToolResultObject, TypedSessionEventHandler, TypedSessionLifecycleHandler, ZodSchema, } from "./types.js";
|
|
9
|
+
export type { ConnectionState, CopilotClientOptions, CustomAgentConfig, ForegroundSessionInfo, GetAuthStatusResponse, GetStatusResponse, InfiniteSessionConfig, MCPLocalServerConfig, MCPRemoteServerConfig, MCPServerConfig, MessageOptions, ModelBilling, ModelCapabilities, ModelInfo, ModelPolicy, PermissionHandler, PermissionRequest, PermissionRequestResult, ResumeSessionConfig, SessionConfig, SessionEvent, SessionEventHandler, SessionEventPayload, SessionEventType, SessionLifecycleEvent, SessionLifecycleEventType, SessionLifecycleHandler, SessionContext, SessionListFilter, SessionMetadata, SystemMessageAppendConfig, SystemMessageConfig, SystemMessageReplaceConfig, TelemetryConfig, TraceContext, TraceContextProvider, Tool, ToolHandler, ToolInvocation, ToolResultObject, TypedSessionEventHandler, TypedSessionLifecycleHandler, ZodSchema, } from "./types.js";
|
package/dist/session.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { MessageConnection } from "vscode-jsonrpc/node.js";
|
|
6
6
|
import { createSessionRpc } from "./generated/rpc.js";
|
|
7
|
-
import type { MessageOptions, PermissionHandler, PermissionRequestResult, SessionEvent, SessionEventHandler, SessionEventType, SessionHooks, Tool, ToolHandler, TypedSessionEventHandler, UserInputHandler, UserInputResponse } from "./types.js";
|
|
7
|
+
import type { MessageOptions, PermissionHandler, PermissionRequestResult, ReasoningEffort, SessionEvent, SessionEventHandler, SessionEventType, SessionHooks, Tool, ToolHandler, TraceContextProvider, TypedSessionEventHandler, UserInputHandler, UserInputResponse } from "./types.js";
|
|
8
8
|
export declare const NO_RESULT_PERMISSION_V2_ERROR = "Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
|
|
9
9
|
/** Assistant message event - the final response from the assistant. */
|
|
10
10
|
export type AssistantMessageEvent = Extract<SessionEvent, {
|
|
@@ -46,15 +46,17 @@ export declare class CopilotSession {
|
|
|
46
46
|
private userInputHandler?;
|
|
47
47
|
private hooks?;
|
|
48
48
|
private _rpc;
|
|
49
|
+
private traceContextProvider?;
|
|
49
50
|
/**
|
|
50
51
|
* Creates a new CopilotSession instance.
|
|
51
52
|
*
|
|
52
53
|
* @param sessionId - The unique identifier for this session
|
|
53
54
|
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
54
55
|
* @param workspacePath - Path to the session workspace directory (when infinite sessions enabled)
|
|
56
|
+
* @param traceContextProvider - Optional callback to get W3C Trace Context for outbound RPCs
|
|
55
57
|
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
56
58
|
*/
|
|
57
|
-
constructor(sessionId: string, connection: MessageConnection, _workspacePath?: string | undefined);
|
|
59
|
+
constructor(sessionId: string, connection: MessageConnection, _workspacePath?: string | undefined, traceContextProvider?: TraceContextProvider);
|
|
58
60
|
/**
|
|
59
61
|
* Typed session-scoped RPC methods.
|
|
60
62
|
*/
|
|
@@ -333,13 +335,17 @@ export declare class CopilotSession {
|
|
|
333
335
|
* The new model takes effect for the next message. Conversation history is preserved.
|
|
334
336
|
*
|
|
335
337
|
* @param model - Model ID to switch to
|
|
338
|
+
* @param options - Optional settings for the new model
|
|
336
339
|
*
|
|
337
340
|
* @example
|
|
338
341
|
* ```typescript
|
|
339
342
|
* await session.setModel("gpt-4.1");
|
|
343
|
+
* await session.setModel("claude-sonnet-4.6", { reasoningEffort: "high" });
|
|
340
344
|
* ```
|
|
341
345
|
*/
|
|
342
|
-
setModel(model: string
|
|
346
|
+
setModel(model: string, options?: {
|
|
347
|
+
reasoningEffort?: ReasoningEffort;
|
|
348
|
+
}): Promise<void>;
|
|
343
349
|
/**
|
|
344
350
|
* Log a message to the session timeline.
|
|
345
351
|
* The message appears in the session event stream and is visible to SDK consumers
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ConnectionError, ResponseError } from "vscode-jsonrpc/node.js";
|
|
2
2
|
import { createSessionRpc } from "./generated/rpc.js";
|
|
3
|
+
import { getTraceContext } from "./telemetry.js";
|
|
3
4
|
const NO_RESULT_PERMISSION_V2_ERROR = "Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
|
|
4
5
|
class CopilotSession {
|
|
5
6
|
/**
|
|
@@ -8,12 +9,14 @@ class CopilotSession {
|
|
|
8
9
|
* @param sessionId - The unique identifier for this session
|
|
9
10
|
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
10
11
|
* @param workspacePath - Path to the session workspace directory (when infinite sessions enabled)
|
|
12
|
+
* @param traceContextProvider - Optional callback to get W3C Trace Context for outbound RPCs
|
|
11
13
|
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
12
14
|
*/
|
|
13
|
-
constructor(sessionId, connection, _workspacePath) {
|
|
15
|
+
constructor(sessionId, connection, _workspacePath, traceContextProvider) {
|
|
14
16
|
this.sessionId = sessionId;
|
|
15
17
|
this.connection = connection;
|
|
16
18
|
this._workspacePath = _workspacePath;
|
|
19
|
+
this.traceContextProvider = traceContextProvider;
|
|
17
20
|
}
|
|
18
21
|
eventHandlers = /* @__PURE__ */ new Set();
|
|
19
22
|
typedEventHandlers = /* @__PURE__ */ new Map();
|
|
@@ -22,6 +25,7 @@ class CopilotSession {
|
|
|
22
25
|
userInputHandler;
|
|
23
26
|
hooks;
|
|
24
27
|
_rpc = null;
|
|
28
|
+
traceContextProvider;
|
|
25
29
|
/**
|
|
26
30
|
* Typed session-scoped RPC methods.
|
|
27
31
|
*/
|
|
@@ -59,6 +63,7 @@ class CopilotSession {
|
|
|
59
63
|
*/
|
|
60
64
|
async send(options) {
|
|
61
65
|
const response = await this.connection.sendRequest("session.send", {
|
|
66
|
+
...await getTraceContext(this.traceContextProvider),
|
|
62
67
|
sessionId: this.sessionId,
|
|
63
68
|
prompt: options.prompt,
|
|
64
69
|
attachments: options.attachments,
|
|
@@ -188,9 +193,19 @@ class CopilotSession {
|
|
|
188
193
|
const { requestId, toolName } = event.data;
|
|
189
194
|
const args = event.data.arguments;
|
|
190
195
|
const toolCallId = event.data.toolCallId;
|
|
196
|
+
const traceparent = event.data.traceparent;
|
|
197
|
+
const tracestate = event.data.tracestate;
|
|
191
198
|
const handler = this.toolHandlers.get(toolName);
|
|
192
199
|
if (handler) {
|
|
193
|
-
void this._executeToolAndRespond(
|
|
200
|
+
void this._executeToolAndRespond(
|
|
201
|
+
requestId,
|
|
202
|
+
toolName,
|
|
203
|
+
toolCallId,
|
|
204
|
+
args,
|
|
205
|
+
handler,
|
|
206
|
+
traceparent,
|
|
207
|
+
tracestate
|
|
208
|
+
);
|
|
194
209
|
}
|
|
195
210
|
} else if (event.type === "permission.requested") {
|
|
196
211
|
const { requestId, permissionRequest } = event.data;
|
|
@@ -203,13 +218,15 @@ class CopilotSession {
|
|
|
203
218
|
* Executes a tool handler and sends the result back via RPC.
|
|
204
219
|
* @internal
|
|
205
220
|
*/
|
|
206
|
-
async _executeToolAndRespond(requestId, toolName, toolCallId, args, handler) {
|
|
221
|
+
async _executeToolAndRespond(requestId, toolName, toolCallId, args, handler, traceparent, tracestate) {
|
|
207
222
|
try {
|
|
208
223
|
const rawResult = await handler(args, {
|
|
209
224
|
sessionId: this.sessionId,
|
|
210
225
|
toolCallId,
|
|
211
226
|
toolName,
|
|
212
|
-
arguments: args
|
|
227
|
+
arguments: args,
|
|
228
|
+
traceparent,
|
|
229
|
+
tracestate
|
|
213
230
|
});
|
|
214
231
|
let result;
|
|
215
232
|
if (rawResult == null) {
|
|
@@ -502,14 +519,16 @@ class CopilotSession {
|
|
|
502
519
|
* The new model takes effect for the next message. Conversation history is preserved.
|
|
503
520
|
*
|
|
504
521
|
* @param model - Model ID to switch to
|
|
522
|
+
* @param options - Optional settings for the new model
|
|
505
523
|
*
|
|
506
524
|
* @example
|
|
507
525
|
* ```typescript
|
|
508
526
|
* await session.setModel("gpt-4.1");
|
|
527
|
+
* await session.setModel("claude-sonnet-4.6", { reasoningEffort: "high" });
|
|
509
528
|
* ```
|
|
510
529
|
*/
|
|
511
|
-
async setModel(model) {
|
|
512
|
-
await this.rpc.model.switchTo({ modelId: model });
|
|
530
|
+
async setModel(model, options) {
|
|
531
|
+
await this.rpc.model.switchTo({ modelId: model, ...options });
|
|
513
532
|
}
|
|
514
533
|
/**
|
|
515
534
|
* Log a message to the session timeline.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trace-context helpers.
|
|
3
|
+
*
|
|
4
|
+
* The SDK does not depend on any OpenTelemetry packages. Instead, users
|
|
5
|
+
* provide an {@link TraceContextProvider} callback via client options.
|
|
6
|
+
*
|
|
7
|
+
* @module telemetry
|
|
8
|
+
*/
|
|
9
|
+
import type { TraceContext, TraceContextProvider } from "./types.js";
|
|
10
|
+
/**
|
|
11
|
+
* Calls the user-provided {@link TraceContextProvider} to obtain the current
|
|
12
|
+
* W3C Trace Context. Returns `{}` when no provider is configured.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getTraceContext(provider?: TraceContextProvider): Promise<TraceContext>;
|
package/dist/types.d.ts
CHANGED
|
@@ -6,6 +6,38 @@ export type SessionEvent = GeneratedSessionEvent;
|
|
|
6
6
|
/**
|
|
7
7
|
* Options for creating a CopilotClient
|
|
8
8
|
*/
|
|
9
|
+
/**
|
|
10
|
+
* W3C Trace Context headers used for distributed trace propagation.
|
|
11
|
+
*/
|
|
12
|
+
export interface TraceContext {
|
|
13
|
+
traceparent?: string;
|
|
14
|
+
tracestate?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Callback that returns the current W3C Trace Context.
|
|
18
|
+
* Wire this up to your OpenTelemetry (or other tracing) SDK to enable
|
|
19
|
+
* distributed trace propagation between your app and the Copilot CLI.
|
|
20
|
+
*/
|
|
21
|
+
export type TraceContextProvider = () => TraceContext | Promise<TraceContext>;
|
|
22
|
+
/**
|
|
23
|
+
* Configuration for OpenTelemetry instrumentation.
|
|
24
|
+
*
|
|
25
|
+
* When provided via {@link CopilotClientOptions.telemetry}, the SDK sets
|
|
26
|
+
* the corresponding environment variables on the spawned CLI process so
|
|
27
|
+
* that the CLI's built-in OTel exporter is configured automatically.
|
|
28
|
+
*/
|
|
29
|
+
export interface TelemetryConfig {
|
|
30
|
+
/** OTLP HTTP endpoint URL for trace/metric export. Sets OTEL_EXPORTER_OTLP_ENDPOINT. */
|
|
31
|
+
otlpEndpoint?: string;
|
|
32
|
+
/** File path for JSON-lines trace output. Sets COPILOT_OTEL_FILE_EXPORTER_PATH. */
|
|
33
|
+
filePath?: string;
|
|
34
|
+
/** Exporter backend type: "otlp-http" or "file". Sets COPILOT_OTEL_EXPORTER_TYPE. */
|
|
35
|
+
exporterType?: string;
|
|
36
|
+
/** Instrumentation scope name. Sets COPILOT_OTEL_SOURCE_NAME. */
|
|
37
|
+
sourceName?: string;
|
|
38
|
+
/** Whether to capture message content (prompts, responses). Sets OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT. */
|
|
39
|
+
captureContent?: boolean;
|
|
40
|
+
}
|
|
9
41
|
export interface CopilotClientOptions {
|
|
10
42
|
/**
|
|
11
43
|
* Path to the CLI executable or JavaScript entry point.
|
|
@@ -83,6 +115,37 @@ export interface CopilotClientOptions {
|
|
|
83
115
|
* available from your custom provider.
|
|
84
116
|
*/
|
|
85
117
|
onListModels?: () => Promise<ModelInfo[]> | ModelInfo[];
|
|
118
|
+
/**
|
|
119
|
+
* OpenTelemetry configuration for the CLI process.
|
|
120
|
+
* When provided, the corresponding OTel environment variables are set
|
|
121
|
+
* on the spawned CLI server.
|
|
122
|
+
*/
|
|
123
|
+
telemetry?: TelemetryConfig;
|
|
124
|
+
/**
|
|
125
|
+
* Advanced: callback that returns the current W3C Trace Context for distributed
|
|
126
|
+
* trace propagation. Most users do not need this — the {@link telemetry} config
|
|
127
|
+
* alone is sufficient to collect traces from the CLI.
|
|
128
|
+
*
|
|
129
|
+
* This callback is only useful when your application creates its own
|
|
130
|
+
* OpenTelemetry spans and you want them to appear in the **same** distributed
|
|
131
|
+
* trace as the CLI's spans. The SDK calls this before `session.create`,
|
|
132
|
+
* `session.resume`, and `session.send` RPCs to inject `traceparent`/`tracestate`
|
|
133
|
+
* into the request.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* import { propagation, context } from "@opentelemetry/api";
|
|
138
|
+
*
|
|
139
|
+
* const client = new CopilotClient({
|
|
140
|
+
* onGetTraceContext: () => {
|
|
141
|
+
* const carrier: Record<string, string> = {};
|
|
142
|
+
* propagation.inject(context.active(), carrier);
|
|
143
|
+
* return carrier;
|
|
144
|
+
* },
|
|
145
|
+
* });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
onGetTraceContext?: TraceContextProvider;
|
|
86
149
|
}
|
|
87
150
|
/**
|
|
88
151
|
* Configuration for creating a session
|
|
@@ -108,6 +171,10 @@ export interface ToolInvocation {
|
|
|
108
171
|
toolCallId: string;
|
|
109
172
|
toolName: string;
|
|
110
173
|
arguments: unknown;
|
|
174
|
+
/** W3C Trace Context traceparent from the CLI's execute_tool span. */
|
|
175
|
+
traceparent?: string;
|
|
176
|
+
/** W3C Trace Context tracestate from the CLI's execute_tool span. */
|
|
177
|
+
tracestate?: string;
|
|
111
178
|
}
|
|
112
179
|
export type ToolHandler<TArgs = unknown> = (args: TArgs, invocation: ToolInvocation) => Promise<unknown> | unknown;
|
|
113
180
|
/**
|
|
@@ -686,7 +753,7 @@ export interface MessageOptions {
|
|
|
686
753
|
*/
|
|
687
754
|
prompt: string;
|
|
688
755
|
/**
|
|
689
|
-
* File, directory, or
|
|
756
|
+
* File, directory, selection, or blob attachments
|
|
690
757
|
*/
|
|
691
758
|
attachments?: Array<{
|
|
692
759
|
type: "file";
|
|
@@ -711,6 +778,11 @@ export interface MessageOptions {
|
|
|
711
778
|
};
|
|
712
779
|
};
|
|
713
780
|
text?: string;
|
|
781
|
+
} | {
|
|
782
|
+
type: "blob";
|
|
783
|
+
data: string;
|
|
784
|
+
mimeType: string;
|
|
785
|
+
displayName?: string;
|
|
714
786
|
}>;
|
|
715
787
|
/**
|
|
716
788
|
* Message delivery mode
|
package/package.json
CHANGED
|
@@ -4,18 +4,30 @@
|
|
|
4
4
|
"type": "git",
|
|
5
5
|
"url": "https://github.com/github/copilot-sdk.git"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.1.33-preview.
|
|
7
|
+
"version": "0.1.33-preview.3",
|
|
8
8
|
"description": "TypeScript SDK for programmatic control of GitHub Copilot CLI via JSON-RPC",
|
|
9
|
-
"main": "./dist/index.js",
|
|
9
|
+
"main": "./dist/cjs/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
-
"import":
|
|
14
|
-
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/cjs/index.js"
|
|
20
|
+
}
|
|
15
21
|
},
|
|
16
22
|
"./extension": {
|
|
17
|
-
"import":
|
|
18
|
-
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/extension.d.ts",
|
|
25
|
+
"default": "./dist/extension.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/extension.d.ts",
|
|
29
|
+
"default": "./dist/cjs/extension.js"
|
|
30
|
+
}
|
|
19
31
|
}
|
|
20
32
|
},
|
|
21
33
|
"type": "module",
|