-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsession.ts
More file actions
987 lines (921 loc) · 35.7 KB
/
session.ts
File metadata and controls
987 lines (921 loc) · 35.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/**
* Copilot Session - represents a single conversation session with the Copilot CLI.
* @module session
*/
import type { MessageConnection } from "vscode-jsonrpc/node.js";
import { ConnectionError, ResponseError } from "vscode-jsonrpc/node.js";
import { createSessionRpc } from "./generated/rpc.js";
import { getTraceContext } from "./telemetry.js";
import type {
CommandHandler,
ElicitationParams,
ElicitationResult,
InputOptions,
MessageOptions,
PermissionHandler,
PermissionRequest,
PermissionRequestResult,
ReasoningEffort,
SectionTransformFn,
SessionCapabilities,
SessionEvent,
SessionEventHandler,
SessionEventPayload,
SessionEventType,
SessionHooks,
SessionUiApi,
Tool,
ToolHandler,
TraceContextProvider,
TypedSessionEventHandler,
UserInputHandler,
UserInputRequest,
UserInputResponse,
} from "./types.js";
export const NO_RESULT_PERMISSION_V2_ERROR =
"Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
/** Assistant message event - the final response from the assistant. */
export type AssistantMessageEvent = Extract<SessionEvent, { type: "assistant.message" }>;
/**
* Represents a single conversation session with the Copilot CLI.
*
* A session maintains conversation state, handles events, and manages tool execution.
* Sessions are created via {@link CopilotClient.createSession} or resumed via
* {@link CopilotClient.resumeSession}.
*
* @example
* ```typescript
* const session = await client.createSession({ model: "gpt-4" });
*
* // Subscribe to events
* session.on((event) => {
* if (event.type === "assistant.message") {
* console.log(event.data.content);
* }
* });
*
* // Send a message and wait for completion
* await session.sendAndWait({ prompt: "Hello, world!" });
*
* // Clean up
* await session.disconnect();
* ```
*/
export class CopilotSession {
private eventHandlers: Set<SessionEventHandler> = new Set();
private typedEventHandlers: Map<SessionEventType, Set<(event: SessionEvent) => void>> =
new Map();
private toolHandlers: Map<string, ToolHandler> = new Map();
private commandHandlers: Map<string, CommandHandler> = new Map();
private permissionHandler?: PermissionHandler;
private userInputHandler?: UserInputHandler;
private hooks?: SessionHooks;
private transformCallbacks?: Map<string, SectionTransformFn>;
private _rpc: ReturnType<typeof createSessionRpc> | null = null;
private traceContextProvider?: TraceContextProvider;
private _capabilities: SessionCapabilities = {};
/**
* Creates a new CopilotSession instance.
*
* @param sessionId - The unique identifier for this session
* @param connection - The JSON-RPC message connection to the Copilot CLI
* @param workspacePath - Path to the session workspace directory (when infinite sessions enabled)
* @param traceContextProvider - Optional callback to get W3C Trace Context for outbound RPCs
* @internal This constructor is internal. Use {@link CopilotClient.createSession} to create sessions.
*/
constructor(
public readonly sessionId: string,
private connection: MessageConnection,
private _workspacePath?: string,
traceContextProvider?: TraceContextProvider
) {
this.traceContextProvider = traceContextProvider;
}
/**
* Typed session-scoped RPC methods.
*/
get rpc(): ReturnType<typeof createSessionRpc> {
if (!this._rpc) {
this._rpc = createSessionRpc(this.connection, this.sessionId);
}
return this._rpc;
}
/**
* Path to the session workspace directory when infinite sessions are enabled.
* Contains checkpoints/, plan.md, and files/ subdirectories.
* Undefined if infinite sessions are disabled.
*/
get workspacePath(): string | undefined {
return this._workspacePath;
}
/**
* Host capabilities reported when the session was created or resumed.
* Use this to check feature support before calling capability-gated APIs.
*/
get capabilities(): SessionCapabilities {
return this._capabilities;
}
/**
* Interactive UI methods for showing dialogs to the user.
* Only available when the CLI host supports elicitation
* (`session.capabilities.ui?.elicitation === true`).
*
* @example
* ```typescript
* if (session.capabilities.ui?.elicitation) {
* const ok = await session.ui.confirm("Deploy to production?");
* }
* ```
*/
get ui(): SessionUiApi {
return {
elicitation: (params: ElicitationParams) => this._elicitation(params),
confirm: (message: string) => this._confirm(message),
select: (message: string, options: string[]) => this._select(message, options),
input: (message: string, options?: InputOptions) => this._input(message, options),
};
}
/**
* Sends a message to this session and waits for the response.
*
* The message is processed asynchronously. Subscribe to events via {@link on}
* to receive streaming responses and other session events.
*
* @param options - The message options including the prompt and optional attachments
* @returns A promise that resolves with the message ID of the response
* @throws Error if the session has been disconnected or the connection fails
*
* @example
* ```typescript
* const messageId = await session.send({
* prompt: "Explain this code",
* attachments: [{ type: "file", path: "./src/index.ts" }]
* });
* ```
*/
async send(options: MessageOptions): Promise<string> {
const response = await this.connection.sendRequest("session.send", {
...(await getTraceContext(this.traceContextProvider)),
sessionId: this.sessionId,
prompt: options.prompt,
attachments: options.attachments,
mode: options.mode,
});
return (response as { messageId: string }).messageId;
}
/**
* Sends a message to this session and waits until the session becomes idle.
*
* This is a convenience method that combines {@link send} with waiting for
* the `session.idle` event. Use this when you want to block until the
* assistant has finished processing the message.
*
* Events are still delivered to handlers registered via {@link on} while waiting.
*
* @param options - The message options including the prompt and optional attachments
* @param timeout - Timeout in milliseconds (default: 60000). Controls how long to wait; does not abort in-flight agent work.
* @returns A promise that resolves with the final assistant message when the session becomes idle,
* or undefined if no assistant message was received
* @throws Error if the timeout is reached before the session becomes idle
* @throws Error if the session has been disconnected or the connection fails
*
* @example
* ```typescript
* // Send and wait for completion with default 60s timeout
* const response = await session.sendAndWait({ prompt: "What is 2+2?" });
* console.log(response?.data.content); // "4"
* ```
*/
async sendAndWait(
options: MessageOptions,
timeout?: number
): Promise<AssistantMessageEvent | undefined> {
const effectiveTimeout = timeout ?? 60_000;
let resolveIdle: () => void;
let rejectWithError: (error: Error) => void;
const idlePromise = new Promise<void>((resolve, reject) => {
resolveIdle = resolve;
rejectWithError = reject;
});
let lastAssistantMessage: AssistantMessageEvent | undefined;
// Register event handler BEFORE calling send to avoid race condition
// where session.idle fires before we start listening
const unsubscribe = this.on((event) => {
if (event.type === "assistant.message") {
lastAssistantMessage = event;
} else if (event.type === "session.idle") {
resolveIdle();
} else if (event.type === "session.error") {
const error = new Error(event.data.message);
error.stack = event.data.stack;
rejectWithError(error);
}
});
let timeoutId: ReturnType<typeof setTimeout> | undefined;
try {
await this.send(options);
const timeoutPromise = new Promise<never>((_, reject) => {
timeoutId = setTimeout(
() =>
reject(
new Error(
`Timeout after ${effectiveTimeout}ms waiting for session.idle`
)
),
effectiveTimeout
);
});
await Promise.race([idlePromise, timeoutPromise]);
return lastAssistantMessage;
} finally {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
unsubscribe();
}
}
/**
* Subscribes to events from this session.
*
* Events include assistant messages, tool executions, errors, and session state changes.
* Multiple handlers can be registered and will all receive events.
*
* @param eventType - The specific event type to listen for (e.g., "assistant.message", "session.idle")
* @param handler - A callback function that receives events of the specified type
* @returns A function that, when called, unsubscribes the handler
*
* @example
* ```typescript
* // Listen for a specific event type
* const unsubscribe = session.on("assistant.message", (event) => {
* console.log("Assistant:", event.data.content);
* });
*
* // Later, to stop receiving events:
* unsubscribe();
* ```
*/
on<K extends SessionEventType>(eventType: K, handler: TypedSessionEventHandler<K>): () => void;
/**
* Subscribes to all events from this session.
*
* @param handler - A callback function that receives all session events
* @returns A function that, when called, unsubscribes the handler
*
* @example
* ```typescript
* const unsubscribe = session.on((event) => {
* switch (event.type) {
* case "assistant.message":
* console.log("Assistant:", event.data.content);
* break;
* case "session.error":
* console.error("Error:", event.data.message);
* break;
* }
* });
*
* // Later, to stop receiving events:
* unsubscribe();
* ```
*/
on(handler: SessionEventHandler): () => void;
on<K extends SessionEventType>(
eventTypeOrHandler: K | SessionEventHandler,
handler?: TypedSessionEventHandler<K>
): () => void {
// Overload 1: on(eventType, handler) - typed event subscription
if (typeof eventTypeOrHandler === "string" && handler) {
const eventType = eventTypeOrHandler;
if (!this.typedEventHandlers.has(eventType)) {
this.typedEventHandlers.set(eventType, new Set());
}
// Cast is safe: handler receives the correctly typed event at dispatch time
const storedHandler = handler as (event: SessionEvent) => void;
this.typedEventHandlers.get(eventType)!.add(storedHandler);
return () => {
const handlers = this.typedEventHandlers.get(eventType);
if (handlers) {
handlers.delete(storedHandler);
}
};
}
// Overload 2: on(handler) - wildcard subscription
const wildcardHandler = eventTypeOrHandler as SessionEventHandler;
this.eventHandlers.add(wildcardHandler);
return () => {
this.eventHandlers.delete(wildcardHandler);
};
}
/**
* Dispatches an event to all registered handlers.
* Also handles broadcast request events internally (external tool calls, permissions).
*
* @param event - The session event to dispatch
* @internal This method is for internal use by the SDK.
*/
_dispatchEvent(event: SessionEvent): void {
// Handle broadcast request events internally (fire-and-forget)
this._handleBroadcastEvent(event);
// Dispatch to typed handlers for this specific event type
const typedHandlers = this.typedEventHandlers.get(event.type);
if (typedHandlers) {
for (const handler of typedHandlers) {
try {
handler(event as SessionEventPayload<typeof event.type>);
} catch (_error) {
// Handler error
}
}
}
// Dispatch to wildcard handlers
for (const handler of this.eventHandlers) {
try {
handler(event);
} catch (_error) {
// Handler error
}
}
}
/**
* Handles broadcast request events by executing local handlers and responding via RPC.
* Handlers are dispatched as fire-and-forget — rejections propagate as unhandled promise
* rejections, consistent with standard EventEmitter / event handler semantics.
* @internal
*/
private _handleBroadcastEvent(event: SessionEvent): void {
if (event.type === "external_tool.requested") {
const { requestId, toolName } = event.data as {
requestId: string;
toolName: string;
arguments: unknown;
toolCallId: string;
sessionId: string;
};
const args = (event.data as { arguments: unknown }).arguments;
const toolCallId = (event.data as { toolCallId: string }).toolCallId;
const traceparent = (event.data as { traceparent?: string }).traceparent;
const tracestate = (event.data as { tracestate?: string }).tracestate;
const handler = this.toolHandlers.get(toolName);
if (handler) {
void this._executeToolAndRespond(
requestId,
toolName,
toolCallId,
args,
handler,
traceparent,
tracestate
);
}
} else if (event.type === "permission.requested") {
const { requestId, permissionRequest } = event.data as {
requestId: string;
permissionRequest: PermissionRequest;
};
if (this.permissionHandler) {
void this._executePermissionAndRespond(requestId, permissionRequest);
}
} else if (event.type === "command.execute") {
const { requestId, commandName, command, args } = event.data as {
requestId: string;
command: string;
commandName: string;
args: string;
};
void this._executeCommandAndRespond(requestId, commandName, command, args);
}
}
/**
* Executes a tool handler and sends the result back via RPC.
* @internal
*/
private async _executeToolAndRespond(
requestId: string,
toolName: string,
toolCallId: string,
args: unknown,
handler: ToolHandler,
traceparent?: string,
tracestate?: string
): Promise<void> {
try {
const rawResult = await handler(args, {
sessionId: this.sessionId,
toolCallId,
toolName,
arguments: args,
traceparent,
tracestate,
});
let result: string;
if (rawResult == null) {
result = "";
} else if (typeof rawResult === "string") {
result = rawResult;
} else {
result = JSON.stringify(rawResult);
}
await this.rpc.tools.handlePendingToolCall({ requestId, result });
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
try {
await this.rpc.tools.handlePendingToolCall({ requestId, error: message });
} catch (rpcError) {
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
throw rpcError;
}
// Connection lost or RPC error — nothing we can do
}
}
}
/**
* Executes a permission handler and sends the result back via RPC.
* @internal
*/
private async _executePermissionAndRespond(
requestId: string,
permissionRequest: PermissionRequest
): Promise<void> {
try {
const result = await this.permissionHandler!(permissionRequest, {
sessionId: this.sessionId,
});
if (result.kind === "no-result") {
return;
}
await this.rpc.permissions.handlePendingPermissionRequest({ requestId, result });
} catch (_error) {
try {
await this.rpc.permissions.handlePendingPermissionRequest({
requestId,
result: {
kind: "denied-no-approval-rule-and-could-not-request-from-user",
},
});
} catch (rpcError) {
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
throw rpcError;
}
// Connection lost or RPC error — nothing we can do
}
}
}
/**
* Executes a command handler and sends the result back via RPC.
* @internal
*/
private async _executeCommandAndRespond(
requestId: string,
commandName: string,
command: string,
args: string
): Promise<void> {
const handler = this.commandHandlers.get(commandName);
if (!handler) {
try {
await this.rpc.commands.handlePendingCommand({
requestId,
error: `Unknown command: ${commandName}`,
});
} catch (rpcError) {
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
throw rpcError;
}
}
return;
}
try {
await handler({ sessionId: this.sessionId, command, commandName, args });
await this.rpc.commands.handlePendingCommand({ requestId });
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
try {
await this.rpc.commands.handlePendingCommand({ requestId, error: message });
} catch (rpcError) {
if (!(rpcError instanceof ConnectionError || rpcError instanceof ResponseError)) {
throw rpcError;
}
}
}
}
/**
* Registers custom tool handlers for this session.
*
* Tools allow the assistant to execute custom functions. When the assistant
* invokes a tool, the corresponding handler is called with the tool arguments.
*
* @param tools - An array of tool definitions with their handlers, or undefined to clear all tools
* @internal This method is typically called internally when creating a session with tools.
*/
registerTools(tools?: Tool[]): void {
this.toolHandlers.clear();
if (!tools) {
return;
}
for (const tool of tools) {
this.toolHandlers.set(tool.name, tool.handler);
}
}
/**
* Retrieves a registered tool handler by name.
*
* @param name - The name of the tool to retrieve
* @returns The tool handler if found, or undefined
* @internal This method is for internal use by the SDK.
*/
getToolHandler(name: string): ToolHandler | undefined {
return this.toolHandlers.get(name);
}
/**
* Registers command handlers for this session.
*
* @param commands - An array of command definitions with handlers, or undefined to clear
* @internal This method is typically called internally when creating/resuming a session.
*/
registerCommands(commands?: { name: string; handler: CommandHandler }[]): void {
this.commandHandlers.clear();
if (!commands) {
return;
}
for (const cmd of commands) {
this.commandHandlers.set(cmd.name, cmd.handler);
}
}
/**
* Sets the host capabilities for this session.
*
* @param capabilities - The capabilities object from the create/resume response
* @internal This method is typically called internally when creating/resuming a session.
*/
setCapabilities(capabilities?: SessionCapabilities): void {
this._capabilities = capabilities ?? {};
}
private assertElicitation(): void {
if (!this._capabilities.ui?.elicitation) {
throw new Error(
"Elicitation is not supported by the host. " +
"Check session.capabilities.ui?.elicitation before calling UI methods."
);
}
}
private async _elicitation(params: ElicitationParams): Promise<ElicitationResult> {
this.assertElicitation();
return this.rpc.ui.elicitation({
message: params.message,
requestedSchema: params.requestedSchema,
});
}
private async _confirm(message: string): Promise<boolean> {
this.assertElicitation();
const result = await this.rpc.ui.elicitation({
message,
requestedSchema: {
type: "object",
properties: {
confirmed: { type: "boolean", default: true },
},
required: ["confirmed"],
},
});
return result.action === "accept" && (result.content?.confirmed as boolean) === true;
}
private async _select(message: string, options: string[]): Promise<string | null> {
this.assertElicitation();
const result = await this.rpc.ui.elicitation({
message,
requestedSchema: {
type: "object",
properties: {
selection: { type: "string", enum: options },
},
required: ["selection"],
},
});
if (result.action === "accept" && result.content?.selection != null) {
return result.content.selection as string;
}
return null;
}
private async _input(message: string, options?: InputOptions): Promise<string | null> {
this.assertElicitation();
const field: Record<string, unknown> = { type: "string" as const };
if (options?.title) field.title = options.title;
if (options?.description) field.description = options.description;
if (options?.minLength != null) field.minLength = options.minLength;
if (options?.maxLength != null) field.maxLength = options.maxLength;
if (options?.format) field.format = options.format;
if (options?.default != null) field.default = options.default;
const result = await this.rpc.ui.elicitation({
message,
requestedSchema: {
type: "object",
properties: {
value: field as ElicitationParams["requestedSchema"]["properties"][string],
},
required: ["value"],
},
});
if (result.action === "accept" && result.content?.value != null) {
return result.content.value as string;
}
return null;
}
/**
* Registers a handler for permission requests.
*
* When the assistant needs permission to perform certain actions (e.g., file operations),
* this handler is called to approve or deny the request.
*
* @param handler - The permission handler function, or undefined to remove the handler
* @internal This method is typically called internally when creating a session.
*/
registerPermissionHandler(handler?: PermissionHandler): void {
this.permissionHandler = handler;
}
/**
* Registers a user input handler for ask_user requests.
*
* When the agent needs input from the user (via ask_user tool),
* this handler is called to provide the response.
*
* @param handler - The user input handler function, or undefined to remove the handler
* @internal This method is typically called internally when creating a session.
*/
registerUserInputHandler(handler?: UserInputHandler): void {
this.userInputHandler = handler;
}
/**
* Registers hook handlers for session lifecycle events.
*
* Hooks allow custom logic to be executed at various points during
* the session lifecycle (before/after tool use, session start/end, etc.).
*
* @param hooks - The hook handlers object, or undefined to remove all hooks
* @internal This method is typically called internally when creating a session.
*/
registerHooks(hooks?: SessionHooks): void {
this.hooks = hooks;
}
/**
* Registers transform callbacks for system message sections.
*
* @param callbacks - Map of section ID to transform callback, or undefined to clear
* @internal This method is typically called internally when creating a session.
*/
registerTransformCallbacks(callbacks?: Map<string, SectionTransformFn>): void {
this.transformCallbacks = callbacks;
}
/**
* Handles a systemMessage.transform request from the runtime.
* Dispatches each section to its registered transform callback.
*
* @param sections - Map of section IDs to their current rendered content
* @returns A promise that resolves with the transformed sections
* @internal This method is for internal use by the SDK.
*/
async _handleSystemMessageTransform(
sections: Record<string, { content: string }>
): Promise<{ sections: Record<string, { content: string }> }> {
const result: Record<string, { content: string }> = {};
for (const [sectionId, { content }] of Object.entries(sections)) {
const callback = this.transformCallbacks?.get(sectionId);
if (callback) {
try {
const transformed = await callback(content);
result[sectionId] = { content: transformed };
} catch (_error) {
// Callback failed — return original content
result[sectionId] = { content };
}
} else {
// No callback for this section — pass through unchanged
result[sectionId] = { content };
}
}
return { sections: result };
}
/**
* Handles a permission request in the v2 protocol format (synchronous RPC).
* Used as a back-compat adapter when connected to a v2 server.
*
* @param request - The permission request data from the CLI
* @returns A promise that resolves with the permission decision
* @internal This method is for internal use by the SDK.
*/
async _handlePermissionRequestV2(request: unknown): Promise<PermissionRequestResult> {
if (!this.permissionHandler) {
return { kind: "denied-no-approval-rule-and-could-not-request-from-user" };
}
try {
const result = await this.permissionHandler(request as PermissionRequest, {
sessionId: this.sessionId,
});
if (result.kind === "no-result") {
throw new Error(NO_RESULT_PERMISSION_V2_ERROR);
}
return result;
} catch (error) {
if (error instanceof Error && error.message === NO_RESULT_PERMISSION_V2_ERROR) {
throw error;
}
return { kind: "denied-no-approval-rule-and-could-not-request-from-user" };
}
}
/**
* Handles a user input request from the Copilot CLI.
*
* @param request - The user input request data from the CLI
* @returns A promise that resolves with the user's response
* @internal This method is for internal use by the SDK.
*/
async _handleUserInputRequest(request: unknown): Promise<UserInputResponse> {
if (!this.userInputHandler) {
// No handler registered, throw error
throw new Error("User input requested but no handler registered");
}
try {
const result = await this.userInputHandler(request as UserInputRequest, {
sessionId: this.sessionId,
});
return result;
} catch (error) {
// Handler failed, rethrow
throw error;
}
}
/**
* Handles a hooks invocation from the Copilot CLI.
*
* @param hookType - The type of hook being invoked
* @param input - The input data for the hook
* @returns A promise that resolves with the hook output, or undefined
* @internal This method is for internal use by the SDK.
*/
async _handleHooksInvoke(hookType: string, input: unknown): Promise<unknown> {
if (!this.hooks) {
return undefined;
}
// Type-safe handler lookup with explicit casting
type GenericHandler = (
input: unknown,
invocation: { sessionId: string }
) => Promise<unknown> | unknown;
const handlerMap: Record<string, GenericHandler | undefined> = {
preToolUse: this.hooks.onPreToolUse as GenericHandler | undefined,
postToolUse: this.hooks.onPostToolUse as GenericHandler | undefined,
userPromptSubmitted: this.hooks.onUserPromptSubmitted as GenericHandler | undefined,
sessionStart: this.hooks.onSessionStart as GenericHandler | undefined,
sessionEnd: this.hooks.onSessionEnd as GenericHandler | undefined,
errorOccurred: this.hooks.onErrorOccurred as GenericHandler | undefined,
};
const handler = handlerMap[hookType];
if (!handler) {
return undefined;
}
try {
const result = await handler(input, { sessionId: this.sessionId });
return result;
} catch (_error) {
// Hook failed, return undefined
return undefined;
}
}
/**
* Retrieves all events and messages from this session's history.
*
* This returns the complete conversation history including user messages,
* assistant responses, tool executions, and other session events.
*
* @returns A promise that resolves with an array of all session events
* @throws Error if the session has been disconnected or the connection fails
*
* @example
* ```typescript
* const events = await session.getMessages();
* for (const event of events) {
* if (event.type === "assistant.message") {
* console.log("Assistant:", event.data.content);
* }
* }
* ```
*/
async getMessages(): Promise<SessionEvent[]> {
const response = await this.connection.sendRequest("session.getMessages", {
sessionId: this.sessionId,
});
return (response as { events: SessionEvent[] }).events;
}
/**
* Disconnects this session and releases all in-memory resources (event handlers,
* tool handlers, permission handlers).
*
* Session state on disk (conversation history, planning state, artifacts) is
* preserved, so the conversation can be resumed later by calling
* {@link CopilotClient.resumeSession} with the session ID. To permanently
* remove all session data including files on disk, use
* {@link CopilotClient.deleteSession} instead.
*
* After calling this method, the session object can no longer be used.
*
* @returns A promise that resolves when the session is disconnected
* @throws Error if the connection fails
*
* @example
* ```typescript
* // Clean up when done — session can still be resumed later
* await session.disconnect();
* ```
*/
async disconnect(): Promise<void> {
await this.connection.sendRequest("session.destroy", {
sessionId: this.sessionId,
});
this.eventHandlers.clear();
this.typedEventHandlers.clear();
this.toolHandlers.clear();
this.permissionHandler = undefined;
}
/**
* @deprecated Use {@link disconnect} instead. This method will be removed in a future release.
*
* Disconnects this session and releases all in-memory resources.
* Session data on disk is preserved for later resumption.
*
* @returns A promise that resolves when the session is disconnected
* @throws Error if the connection fails
*/
async destroy(): Promise<void> {
return this.disconnect();
}
/** Enables `await using session = ...` syntax for automatic cleanup. */
async [Symbol.asyncDispose](): Promise<void> {
return this.disconnect();
}
/**
* Aborts the currently processing message in this session.
*
* Use this to cancel a long-running request. The session remains valid
* and can continue to be used for new messages.
*
* @returns A promise that resolves when the abort request is acknowledged
* @throws Error if the session has been disconnected or the connection fails
*
* @example
* ```typescript
* // Start a long-running request
* const messagePromise = session.send({ prompt: "Write a very long story..." });
*
* // Abort after 5 seconds
* setTimeout(async () => {
* await session.abort();
* }, 5000);
* ```
*/
async abort(): Promise<void> {
await this.connection.sendRequest("session.abort", {
sessionId: this.sessionId,
});
}
/**
* Change the model for this session.
* The new model takes effect for the next message. Conversation history is preserved.
*
* @param model - Model ID to switch to
* @param options - Optional settings for the new model
*
* @example
* ```typescript
* await session.setModel("gpt-4.1");
* await session.setModel("claude-sonnet-4.6", { reasoningEffort: "high" });
* ```
*/
async setModel(model: string, options?: { reasoningEffort?: ReasoningEffort }): Promise<void> {
await this.rpc.model.switchTo({ modelId: model, ...options });
}
/**
* Log a message to the session timeline.
* The message appears in the session event stream and is visible to SDK consumers
* and (for non-ephemeral messages) persisted to the session event log on disk.
*
* @param message - Human-readable message text
* @param options - Optional log level and ephemeral flag
*
* @example
* ```typescript
* await session.log("Processing started");
* await session.log("Disk usage high", { level: "warning" });
* await session.log("Connection failed", { level: "error" });
* await session.log("Debug info", { ephemeral: true });
* ```
*/
async log(
message: string,
options?: { level?: "info" | "warning" | "error"; ephemeral?: boolean }
): Promise<void> {
await this.rpc.log({ message, ...options });
}
}