@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
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { CopilotClient } from "./client.js";
|
|
7
7
|
export { CopilotSession, type AssistantMessageEvent } from "./session.js";
|
|
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";
|
|
8
|
+
export { defineTool, approveAll, SYSTEM_PROMPT_SECTIONS } from "./types.js";
|
|
9
|
+
export type { CommandContext, CommandDefinition, CommandHandler, ConnectionState, CopilotClientOptions, CustomAgentConfig, ElicitationFieldValue, ElicitationParams, ElicitationResult, ElicitationSchema, ElicitationSchemaField, ForegroundSessionInfo, GetAuthStatusResponse, GetStatusResponse, InfiniteSessionConfig, InputOptions, MCPLocalServerConfig, MCPRemoteServerConfig, MCPServerConfig, MessageOptions, ModelBilling, ModelCapabilities, ModelInfo, ModelPolicy, PermissionHandler, PermissionRequest, PermissionRequestResult, ResumeSessionConfig, SectionOverride, SectionOverrideAction, SectionTransformFn, SessionCapabilities, SessionConfig, SessionEvent, SessionEventHandler, SessionEventPayload, SessionEventType, SessionLifecycleEvent, SessionLifecycleEventType, SessionLifecycleHandler, SessionContext, SessionListFilter, SessionMetadata, SessionUiApi, SystemMessageAppendConfig, SystemMessageConfig, SystemMessageCustomizeConfig, SystemMessageReplaceConfig, SystemPromptSection, TelemetryConfig, TraceContext, TraceContextProvider, Tool, ToolHandler, ToolInvocation, ToolResultObject, TypedSessionEventHandler, TypedSessionLifecycleHandler, ZodSchema, } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { CopilotClient } from "./client.js";
|
|
2
2
|
import { CopilotSession } from "./session.js";
|
|
3
|
-
import { defineTool, approveAll } from "./types.js";
|
|
3
|
+
import { defineTool, approveAll, SYSTEM_PROMPT_SECTIONS } from "./types.js";
|
|
4
4
|
export {
|
|
5
5
|
CopilotClient,
|
|
6
6
|
CopilotSession,
|
|
7
|
+
SYSTEM_PROMPT_SECTIONS,
|
|
7
8
|
approveAll,
|
|
8
9
|
defineTool
|
|
9
10
|
};
|
package/dist/session.d.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
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 { CommandHandler, MessageOptions, PermissionHandler, PermissionRequestResult, ReasoningEffort, SectionTransformFn, SessionCapabilities, SessionEvent, SessionEventHandler, SessionEventType, SessionHooks, SessionUiApi, Tool, ToolHandler, TraceContextProvider, TypedSessionEventHandler, UserInputHandler, UserInputResponse } from "./types.js";
|
|
8
|
+
export declare const NO_RESULT_PERMISSION_V2_ERROR = "Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
|
|
8
9
|
/** Assistant message event - the final response from the assistant. */
|
|
9
10
|
export type AssistantMessageEvent = Extract<SessionEvent, {
|
|
10
11
|
type: "assistant.message";
|
|
@@ -37,23 +38,28 @@ export type AssistantMessageEvent = Extract<SessionEvent, {
|
|
|
37
38
|
export declare class CopilotSession {
|
|
38
39
|
readonly sessionId: string;
|
|
39
40
|
private connection;
|
|
40
|
-
private
|
|
41
|
+
private _workspacePath?;
|
|
41
42
|
private eventHandlers;
|
|
42
43
|
private typedEventHandlers;
|
|
43
44
|
private toolHandlers;
|
|
45
|
+
private commandHandlers;
|
|
44
46
|
private permissionHandler?;
|
|
45
47
|
private userInputHandler?;
|
|
46
48
|
private hooks?;
|
|
49
|
+
private transformCallbacks?;
|
|
47
50
|
private _rpc;
|
|
51
|
+
private traceContextProvider?;
|
|
52
|
+
private _capabilities;
|
|
48
53
|
/**
|
|
49
54
|
* Creates a new CopilotSession instance.
|
|
50
55
|
*
|
|
51
56
|
* @param sessionId - The unique identifier for this session
|
|
52
57
|
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
53
58
|
* @param workspacePath - Path to the session workspace directory (when infinite sessions enabled)
|
|
59
|
+
* @param traceContextProvider - Optional callback to get W3C Trace Context for outbound RPCs
|
|
54
60
|
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
55
61
|
*/
|
|
56
|
-
constructor(sessionId: string, connection: MessageConnection, _workspacePath?: string | undefined);
|
|
62
|
+
constructor(sessionId: string, connection: MessageConnection, _workspacePath?: string | undefined, traceContextProvider?: TraceContextProvider);
|
|
57
63
|
/**
|
|
58
64
|
* Typed session-scoped RPC methods.
|
|
59
65
|
*/
|
|
@@ -64,6 +70,24 @@ export declare class CopilotSession {
|
|
|
64
70
|
* Undefined if infinite sessions are disabled.
|
|
65
71
|
*/
|
|
66
72
|
get workspacePath(): string | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Host capabilities reported when the session was created or resumed.
|
|
75
|
+
* Use this to check feature support before calling capability-gated APIs.
|
|
76
|
+
*/
|
|
77
|
+
get capabilities(): SessionCapabilities;
|
|
78
|
+
/**
|
|
79
|
+
* Interactive UI methods for showing dialogs to the user.
|
|
80
|
+
* Only available when the CLI host supports elicitation
|
|
81
|
+
* (`session.capabilities.ui?.elicitation === true`).
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* if (session.capabilities.ui?.elicitation) {
|
|
86
|
+
* const ok = await session.ui.confirm("Deploy to production?");
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
get ui(): SessionUiApi;
|
|
67
91
|
/**
|
|
68
92
|
* Sends a message to this session and waits for the response.
|
|
69
93
|
*
|
|
@@ -178,6 +202,11 @@ export declare class CopilotSession {
|
|
|
178
202
|
* @internal
|
|
179
203
|
*/
|
|
180
204
|
private _executePermissionAndRespond;
|
|
205
|
+
/**
|
|
206
|
+
* Executes a command handler and sends the result back via RPC.
|
|
207
|
+
* @internal
|
|
208
|
+
*/
|
|
209
|
+
private _executeCommandAndRespond;
|
|
181
210
|
/**
|
|
182
211
|
* Registers custom tool handlers for this session.
|
|
183
212
|
*
|
|
@@ -196,6 +225,28 @@ export declare class CopilotSession {
|
|
|
196
225
|
* @internal This method is for internal use by the SDK.
|
|
197
226
|
*/
|
|
198
227
|
getToolHandler(name: string): ToolHandler | undefined;
|
|
228
|
+
/**
|
|
229
|
+
* Registers command handlers for this session.
|
|
230
|
+
*
|
|
231
|
+
* @param commands - An array of command definitions with handlers, or undefined to clear
|
|
232
|
+
* @internal This method is typically called internally when creating/resuming a session.
|
|
233
|
+
*/
|
|
234
|
+
registerCommands(commands?: {
|
|
235
|
+
name: string;
|
|
236
|
+
handler: CommandHandler;
|
|
237
|
+
}[]): void;
|
|
238
|
+
/**
|
|
239
|
+
* Sets the host capabilities for this session.
|
|
240
|
+
*
|
|
241
|
+
* @param capabilities - The capabilities object from the create/resume response
|
|
242
|
+
* @internal This method is typically called internally when creating/resuming a session.
|
|
243
|
+
*/
|
|
244
|
+
setCapabilities(capabilities?: SessionCapabilities): void;
|
|
245
|
+
private assertElicitation;
|
|
246
|
+
private _elicitation;
|
|
247
|
+
private _confirm;
|
|
248
|
+
private _select;
|
|
249
|
+
private _input;
|
|
199
250
|
/**
|
|
200
251
|
* Registers a handler for permission requests.
|
|
201
252
|
*
|
|
@@ -226,6 +277,28 @@ export declare class CopilotSession {
|
|
|
226
277
|
* @internal This method is typically called internally when creating a session.
|
|
227
278
|
*/
|
|
228
279
|
registerHooks(hooks?: SessionHooks): void;
|
|
280
|
+
/**
|
|
281
|
+
* Registers transform callbacks for system message sections.
|
|
282
|
+
*
|
|
283
|
+
* @param callbacks - Map of section ID to transform callback, or undefined to clear
|
|
284
|
+
* @internal This method is typically called internally when creating a session.
|
|
285
|
+
*/
|
|
286
|
+
registerTransformCallbacks(callbacks?: Map<string, SectionTransformFn>): void;
|
|
287
|
+
/**
|
|
288
|
+
* Handles a systemMessage.transform request from the runtime.
|
|
289
|
+
* Dispatches each section to its registered transform callback.
|
|
290
|
+
*
|
|
291
|
+
* @param sections - Map of section IDs to their current rendered content
|
|
292
|
+
* @returns A promise that resolves with the transformed sections
|
|
293
|
+
* @internal This method is for internal use by the SDK.
|
|
294
|
+
*/
|
|
295
|
+
_handleSystemMessageTransform(sections: Record<string, {
|
|
296
|
+
content: string;
|
|
297
|
+
}>): Promise<{
|
|
298
|
+
sections: Record<string, {
|
|
299
|
+
content: string;
|
|
300
|
+
}>;
|
|
301
|
+
}>;
|
|
229
302
|
/**
|
|
230
303
|
* Handles a permission request in the v2 protocol format (synchronous RPC).
|
|
231
304
|
* Used as a back-compat adapter when connected to a v2 server.
|
|
@@ -332,13 +405,17 @@ export declare class CopilotSession {
|
|
|
332
405
|
* The new model takes effect for the next message. Conversation history is preserved.
|
|
333
406
|
*
|
|
334
407
|
* @param model - Model ID to switch to
|
|
408
|
+
* @param options - Optional settings for the new model
|
|
335
409
|
*
|
|
336
410
|
* @example
|
|
337
411
|
* ```typescript
|
|
338
412
|
* await session.setModel("gpt-4.1");
|
|
413
|
+
* await session.setModel("claude-sonnet-4.6", { reasoningEffort: "high" });
|
|
339
414
|
* ```
|
|
340
415
|
*/
|
|
341
|
-
setModel(model: string
|
|
416
|
+
setModel(model: string, options?: {
|
|
417
|
+
reasoningEffort?: ReasoningEffort;
|
|
418
|
+
}): Promise<void>;
|
|
342
419
|
/**
|
|
343
420
|
* Log a message to the session timeline.
|
|
344
421
|
* The message appears in the session event stream and is visible to SDK consumers
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ConnectionError, ResponseError } from "vscode-jsonrpc/node.js";
|
|
2
2
|
import { createSessionRpc } from "./generated/rpc.js";
|
|
3
|
+
import { getTraceContext } from "./telemetry.js";
|
|
4
|
+
const NO_RESULT_PERMISSION_V2_ERROR = "Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
|
|
3
5
|
class CopilotSession {
|
|
4
6
|
/**
|
|
5
7
|
* Creates a new CopilotSession instance.
|
|
@@ -7,20 +9,26 @@ class CopilotSession {
|
|
|
7
9
|
* @param sessionId - The unique identifier for this session
|
|
8
10
|
* @param connection - The JSON-RPC message connection to the Copilot CLI
|
|
9
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
|
|
10
13
|
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
|
|
11
14
|
*/
|
|
12
|
-
constructor(sessionId, connection, _workspacePath) {
|
|
15
|
+
constructor(sessionId, connection, _workspacePath, traceContextProvider) {
|
|
13
16
|
this.sessionId = sessionId;
|
|
14
17
|
this.connection = connection;
|
|
15
18
|
this._workspacePath = _workspacePath;
|
|
19
|
+
this.traceContextProvider = traceContextProvider;
|
|
16
20
|
}
|
|
17
21
|
eventHandlers = /* @__PURE__ */ new Set();
|
|
18
22
|
typedEventHandlers = /* @__PURE__ */ new Map();
|
|
19
23
|
toolHandlers = /* @__PURE__ */ new Map();
|
|
24
|
+
commandHandlers = /* @__PURE__ */ new Map();
|
|
20
25
|
permissionHandler;
|
|
21
26
|
userInputHandler;
|
|
22
27
|
hooks;
|
|
28
|
+
transformCallbacks;
|
|
23
29
|
_rpc = null;
|
|
30
|
+
traceContextProvider;
|
|
31
|
+
_capabilities = {};
|
|
24
32
|
/**
|
|
25
33
|
* Typed session-scoped RPC methods.
|
|
26
34
|
*/
|
|
@@ -38,6 +46,33 @@ class CopilotSession {
|
|
|
38
46
|
get workspacePath() {
|
|
39
47
|
return this._workspacePath;
|
|
40
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Host capabilities reported when the session was created or resumed.
|
|
51
|
+
* Use this to check feature support before calling capability-gated APIs.
|
|
52
|
+
*/
|
|
53
|
+
get capabilities() {
|
|
54
|
+
return this._capabilities;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Interactive UI methods for showing dialogs to the user.
|
|
58
|
+
* Only available when the CLI host supports elicitation
|
|
59
|
+
* (`session.capabilities.ui?.elicitation === true`).
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* if (session.capabilities.ui?.elicitation) {
|
|
64
|
+
* const ok = await session.ui.confirm("Deploy to production?");
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
get ui() {
|
|
69
|
+
return {
|
|
70
|
+
elicitation: (params) => this._elicitation(params),
|
|
71
|
+
confirm: (message) => this._confirm(message),
|
|
72
|
+
select: (message, options) => this._select(message, options),
|
|
73
|
+
input: (message, options) => this._input(message, options)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
41
76
|
/**
|
|
42
77
|
* Sends a message to this session and waits for the response.
|
|
43
78
|
*
|
|
@@ -58,6 +93,7 @@ class CopilotSession {
|
|
|
58
93
|
*/
|
|
59
94
|
async send(options) {
|
|
60
95
|
const response = await this.connection.sendRequest("session.send", {
|
|
96
|
+
...await getTraceContext(this.traceContextProvider),
|
|
61
97
|
sessionId: this.sessionId,
|
|
62
98
|
prompt: options.prompt,
|
|
63
99
|
attachments: options.attachments,
|
|
@@ -187,28 +223,43 @@ class CopilotSession {
|
|
|
187
223
|
const { requestId, toolName } = event.data;
|
|
188
224
|
const args = event.data.arguments;
|
|
189
225
|
const toolCallId = event.data.toolCallId;
|
|
226
|
+
const traceparent = event.data.traceparent;
|
|
227
|
+
const tracestate = event.data.tracestate;
|
|
190
228
|
const handler = this.toolHandlers.get(toolName);
|
|
191
229
|
if (handler) {
|
|
192
|
-
void this._executeToolAndRespond(
|
|
230
|
+
void this._executeToolAndRespond(
|
|
231
|
+
requestId,
|
|
232
|
+
toolName,
|
|
233
|
+
toolCallId,
|
|
234
|
+
args,
|
|
235
|
+
handler,
|
|
236
|
+
traceparent,
|
|
237
|
+
tracestate
|
|
238
|
+
);
|
|
193
239
|
}
|
|
194
240
|
} else if (event.type === "permission.requested") {
|
|
195
241
|
const { requestId, permissionRequest } = event.data;
|
|
196
242
|
if (this.permissionHandler) {
|
|
197
243
|
void this._executePermissionAndRespond(requestId, permissionRequest);
|
|
198
244
|
}
|
|
245
|
+
} else if (event.type === "command.execute") {
|
|
246
|
+
const { requestId, commandName, command, args } = event.data;
|
|
247
|
+
void this._executeCommandAndRespond(requestId, commandName, command, args);
|
|
199
248
|
}
|
|
200
249
|
}
|
|
201
250
|
/**
|
|
202
251
|
* Executes a tool handler and sends the result back via RPC.
|
|
203
252
|
* @internal
|
|
204
253
|
*/
|
|
205
|
-
async _executeToolAndRespond(requestId, toolName, toolCallId, args, handler) {
|
|
254
|
+
async _executeToolAndRespond(requestId, toolName, toolCallId, args, handler, traceparent, tracestate) {
|
|
206
255
|
try {
|
|
207
256
|
const rawResult = await handler(args, {
|
|
208
257
|
sessionId: this.sessionId,
|
|
209
258
|
toolCallId,
|
|
210
259
|
toolName,
|
|
211
|
-
arguments: args
|
|
260
|
+
arguments: args,
|
|
261
|
+
traceparent,
|
|
262
|
+
tracestate
|
|
212
263
|
});
|
|
213
264
|
let result;
|
|
214
265
|
if (rawResult == null) {
|
|
@@ -239,6 +290,9 @@ class CopilotSession {
|
|
|
239
290
|
const result = await this.permissionHandler(permissionRequest, {
|
|
240
291
|
sessionId: this.sessionId
|
|
241
292
|
});
|
|
293
|
+
if (result.kind === "no-result") {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
242
296
|
await this.rpc.permissions.handlePendingPermissionRequest({ requestId, result });
|
|
243
297
|
} catch (_error) {
|
|
244
298
|
try {
|
|
@@ -255,6 +309,39 @@ class CopilotSession {
|
|
|
255
309
|
}
|
|
256
310
|
}
|
|
257
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Executes a command handler and sends the result back via RPC.
|
|
314
|
+
* @internal
|
|
315
|
+
*/
|
|
316
|
+
async _executeCommandAndRespond(requestId, commandName, command, args) {
|
|
317
|
+
const handler = this.commandHandlers.get(commandName);
|
|
318
|
+
if (!handler) {
|
|
319
|
+
try {
|
|
320
|
+
await this.rpc.commands.handlePendingCommand({
|
|
321
|
+
requestId,
|
|
322
|
+
error: `Unknown command: ${commandName}`
|
|
323
|
+
});
|
|
324
|
+
} catch (rpcError) {
|
|
325
|
+
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
|
|
326
|
+
throw rpcError;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
try {
|
|
332
|
+
await handler({ sessionId: this.sessionId, command, commandName, args });
|
|
333
|
+
await this.rpc.commands.handlePendingCommand({ requestId });
|
|
334
|
+
} catch (error) {
|
|
335
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
336
|
+
try {
|
|
337
|
+
await this.rpc.commands.handlePendingCommand({ requestId, error: message });
|
|
338
|
+
} catch (rpcError) {
|
|
339
|
+
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
|
|
340
|
+
throw rpcError;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
258
345
|
/**
|
|
259
346
|
* Registers custom tool handlers for this session.
|
|
260
347
|
*
|
|
@@ -283,6 +370,99 @@ class CopilotSession {
|
|
|
283
370
|
getToolHandler(name) {
|
|
284
371
|
return this.toolHandlers.get(name);
|
|
285
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Registers command handlers for this session.
|
|
375
|
+
*
|
|
376
|
+
* @param commands - An array of command definitions with handlers, or undefined to clear
|
|
377
|
+
* @internal This method is typically called internally when creating/resuming a session.
|
|
378
|
+
*/
|
|
379
|
+
registerCommands(commands) {
|
|
380
|
+
this.commandHandlers.clear();
|
|
381
|
+
if (!commands) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
for (const cmd of commands) {
|
|
385
|
+
this.commandHandlers.set(cmd.name, cmd.handler);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Sets the host capabilities for this session.
|
|
390
|
+
*
|
|
391
|
+
* @param capabilities - The capabilities object from the create/resume response
|
|
392
|
+
* @internal This method is typically called internally when creating/resuming a session.
|
|
393
|
+
*/
|
|
394
|
+
setCapabilities(capabilities) {
|
|
395
|
+
this._capabilities = capabilities ?? {};
|
|
396
|
+
}
|
|
397
|
+
assertElicitation() {
|
|
398
|
+
if (!this._capabilities.ui?.elicitation) {
|
|
399
|
+
throw new Error(
|
|
400
|
+
"Elicitation is not supported by the host. Check session.capabilities.ui?.elicitation before calling UI methods."
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async _elicitation(params) {
|
|
405
|
+
this.assertElicitation();
|
|
406
|
+
return this.rpc.ui.elicitation({
|
|
407
|
+
message: params.message,
|
|
408
|
+
requestedSchema: params.requestedSchema
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
async _confirm(message) {
|
|
412
|
+
this.assertElicitation();
|
|
413
|
+
const result = await this.rpc.ui.elicitation({
|
|
414
|
+
message,
|
|
415
|
+
requestedSchema: {
|
|
416
|
+
type: "object",
|
|
417
|
+
properties: {
|
|
418
|
+
confirmed: { type: "boolean", default: true }
|
|
419
|
+
},
|
|
420
|
+
required: ["confirmed"]
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return result.action === "accept" && result.content?.confirmed === true;
|
|
424
|
+
}
|
|
425
|
+
async _select(message, options) {
|
|
426
|
+
this.assertElicitation();
|
|
427
|
+
const result = await this.rpc.ui.elicitation({
|
|
428
|
+
message,
|
|
429
|
+
requestedSchema: {
|
|
430
|
+
type: "object",
|
|
431
|
+
properties: {
|
|
432
|
+
selection: { type: "string", enum: options }
|
|
433
|
+
},
|
|
434
|
+
required: ["selection"]
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
if (result.action === "accept" && result.content?.selection != null) {
|
|
438
|
+
return result.content.selection;
|
|
439
|
+
}
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
async _input(message, options) {
|
|
443
|
+
this.assertElicitation();
|
|
444
|
+
const field = { type: "string" };
|
|
445
|
+
if (options?.title) field.title = options.title;
|
|
446
|
+
if (options?.description) field.description = options.description;
|
|
447
|
+
if (options?.minLength != null) field.minLength = options.minLength;
|
|
448
|
+
if (options?.maxLength != null) field.maxLength = options.maxLength;
|
|
449
|
+
if (options?.format) field.format = options.format;
|
|
450
|
+
if (options?.default != null) field.default = options.default;
|
|
451
|
+
const result = await this.rpc.ui.elicitation({
|
|
452
|
+
message,
|
|
453
|
+
requestedSchema: {
|
|
454
|
+
type: "object",
|
|
455
|
+
properties: {
|
|
456
|
+
value: field
|
|
457
|
+
},
|
|
458
|
+
required: ["value"]
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
if (result.action === "accept" && result.content?.value != null) {
|
|
462
|
+
return result.content.value;
|
|
463
|
+
}
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
286
466
|
/**
|
|
287
467
|
* Registers a handler for permission requests.
|
|
288
468
|
*
|
|
@@ -319,6 +499,40 @@ class CopilotSession {
|
|
|
319
499
|
registerHooks(hooks) {
|
|
320
500
|
this.hooks = hooks;
|
|
321
501
|
}
|
|
502
|
+
/**
|
|
503
|
+
* Registers transform callbacks for system message sections.
|
|
504
|
+
*
|
|
505
|
+
* @param callbacks - Map of section ID to transform callback, or undefined to clear
|
|
506
|
+
* @internal This method is typically called internally when creating a session.
|
|
507
|
+
*/
|
|
508
|
+
registerTransformCallbacks(callbacks) {
|
|
509
|
+
this.transformCallbacks = callbacks;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Handles a systemMessage.transform request from the runtime.
|
|
513
|
+
* Dispatches each section to its registered transform callback.
|
|
514
|
+
*
|
|
515
|
+
* @param sections - Map of section IDs to their current rendered content
|
|
516
|
+
* @returns A promise that resolves with the transformed sections
|
|
517
|
+
* @internal This method is for internal use by the SDK.
|
|
518
|
+
*/
|
|
519
|
+
async _handleSystemMessageTransform(sections) {
|
|
520
|
+
const result = {};
|
|
521
|
+
for (const [sectionId, { content }] of Object.entries(sections)) {
|
|
522
|
+
const callback = this.transformCallbacks?.get(sectionId);
|
|
523
|
+
if (callback) {
|
|
524
|
+
try {
|
|
525
|
+
const transformed = await callback(content);
|
|
526
|
+
result[sectionId] = { content: transformed };
|
|
527
|
+
} catch (_error) {
|
|
528
|
+
result[sectionId] = { content };
|
|
529
|
+
}
|
|
530
|
+
} else {
|
|
531
|
+
result[sectionId] = { content };
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return { sections: result };
|
|
535
|
+
}
|
|
322
536
|
/**
|
|
323
537
|
* Handles a permission request in the v2 protocol format (synchronous RPC).
|
|
324
538
|
* Used as a back-compat adapter when connected to a v2 server.
|
|
@@ -335,8 +549,14 @@ class CopilotSession {
|
|
|
335
549
|
const result = await this.permissionHandler(request, {
|
|
336
550
|
sessionId: this.sessionId
|
|
337
551
|
});
|
|
552
|
+
if (result.kind === "no-result") {
|
|
553
|
+
throw new Error(NO_RESULT_PERMISSION_V2_ERROR);
|
|
554
|
+
}
|
|
338
555
|
return result;
|
|
339
|
-
} catch (
|
|
556
|
+
} catch (error) {
|
|
557
|
+
if (error instanceof Error && error.message === NO_RESULT_PERMISSION_V2_ERROR) {
|
|
558
|
+
throw error;
|
|
559
|
+
}
|
|
340
560
|
return { kind: "denied-no-approval-rule-and-could-not-request-from-user" };
|
|
341
561
|
}
|
|
342
562
|
}
|
|
@@ -492,14 +712,16 @@ class CopilotSession {
|
|
|
492
712
|
* The new model takes effect for the next message. Conversation history is preserved.
|
|
493
713
|
*
|
|
494
714
|
* @param model - Model ID to switch to
|
|
715
|
+
* @param options - Optional settings for the new model
|
|
495
716
|
*
|
|
496
717
|
* @example
|
|
497
718
|
* ```typescript
|
|
498
719
|
* await session.setModel("gpt-4.1");
|
|
720
|
+
* await session.setModel("claude-sonnet-4.6", { reasoningEffort: "high" });
|
|
499
721
|
* ```
|
|
500
722
|
*/
|
|
501
|
-
async setModel(model) {
|
|
502
|
-
await this.rpc.model.switchTo({ modelId: model });
|
|
723
|
+
async setModel(model, options) {
|
|
724
|
+
await this.rpc.model.switchTo({ modelId: model, ...options });
|
|
503
725
|
}
|
|
504
726
|
/**
|
|
505
727
|
* Log a message to the session timeline.
|
|
@@ -522,5 +744,6 @@ class CopilotSession {
|
|
|
522
744
|
}
|
|
523
745
|
}
|
|
524
746
|
export {
|
|
525
|
-
CopilotSession
|
|
747
|
+
CopilotSession,
|
|
748
|
+
NO_RESULT_PERMISSION_V2_ERROR
|
|
526
749
|
};
|
|
@@ -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>;
|