@github/copilot-sdk 1.0.0-beta.5 → 1.0.0-beta.6
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 +22 -23
- package/dist/cjs/client.js +193 -134
- package/dist/cjs/extension.js +2 -2
- package/dist/cjs/generated/rpc.js +13 -1
- package/dist/cjs/index.js +9 -6
- package/dist/cjs/session.js +17 -58
- package/dist/cjs/types.js +30 -0
- package/dist/client.d.ts +43 -23
- package/dist/client.js +193 -134
- package/dist/extension.js +2 -2
- package/dist/generated/rpc.d.ts +67 -57
- package/dist/generated/rpc.js +13 -1
- package/dist/generated/session-events.d.ts +25 -9
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -0
- package/dist/session.d.ts +5 -212
- package/dist/session.js +17 -58
- package/dist/types.d.ts +239 -112
- package/dist/types.js +29 -0
- package/package.json +2 -2
package/dist/session.js
CHANGED
|
@@ -2,6 +2,14 @@ import { ConnectionError, ResponseError } from "vscode-jsonrpc/node.js";
|
|
|
2
2
|
import { createSessionRpc } from "./generated/rpc.js";
|
|
3
3
|
import { getTraceContext } from "./telemetry.js";
|
|
4
4
|
const NO_RESULT_PERMISSION_V2_ERROR = "Permission handlers cannot return 'no-result' when connected to a protocol v2 server.";
|
|
5
|
+
function deserializeHookInput(raw) {
|
|
6
|
+
if (!raw || typeof raw !== "object" || typeof raw.timestamp !== "number") {
|
|
7
|
+
return raw;
|
|
8
|
+
}
|
|
9
|
+
const obj = raw;
|
|
10
|
+
const { cwd, ...rest } = obj;
|
|
11
|
+
return { ...rest, timestamp: new Date(obj.timestamp), workingDirectory: cwd };
|
|
12
|
+
}
|
|
5
13
|
class CopilotSession {
|
|
6
14
|
/**
|
|
7
15
|
* Creates a new CopilotSession instance.
|
|
@@ -78,25 +86,8 @@ class CopilotSession {
|
|
|
78
86
|
input: (message, options) => this._input(message, options)
|
|
79
87
|
};
|
|
80
88
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
*
|
|
84
|
-
* The message is processed asynchronously. Subscribe to events via {@link on}
|
|
85
|
-
* to receive streaming responses and other session events.
|
|
86
|
-
*
|
|
87
|
-
* @param options - The message options including the prompt and optional attachments
|
|
88
|
-
* @returns A promise that resolves with the message ID of the response
|
|
89
|
-
* @throws Error if the session has been disconnected or the connection fails
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```typescript
|
|
93
|
-
* const messageId = await session.send({
|
|
94
|
-
* prompt: "Explain this code",
|
|
95
|
-
* attachments: [{ type: "file", path: "./src/index.ts" }]
|
|
96
|
-
* });
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
async send(options) {
|
|
89
|
+
async send(optionsOrPrompt) {
|
|
90
|
+
const options = typeof optionsOrPrompt === "string" ? { prompt: optionsOrPrompt } : optionsOrPrompt;
|
|
100
91
|
const response = await this.connection.sendRequest("session.send", {
|
|
101
92
|
...await getTraceContext(this.traceContextProvider),
|
|
102
93
|
sessionId: this.sessionId,
|
|
@@ -107,30 +98,8 @@ class CopilotSession {
|
|
|
107
98
|
});
|
|
108
99
|
return response.messageId;
|
|
109
100
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
*
|
|
113
|
-
* This is a convenience method that combines {@link send} with waiting for
|
|
114
|
-
* the `session.idle` event. Use this when you want to block until the
|
|
115
|
-
* assistant has finished processing the message.
|
|
116
|
-
*
|
|
117
|
-
* Events are still delivered to handlers registered via {@link on} while waiting.
|
|
118
|
-
*
|
|
119
|
-
* @param options - The message options including the prompt and optional attachments
|
|
120
|
-
* @param timeout - Timeout in milliseconds (default: 60000). Controls how long to wait; does not abort in-flight agent work.
|
|
121
|
-
* @returns A promise that resolves with the final assistant message when the session becomes idle,
|
|
122
|
-
* or undefined if no assistant message was received
|
|
123
|
-
* @throws Error if the timeout is reached before the session becomes idle
|
|
124
|
-
* @throws Error if the session has been disconnected or the connection fails
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```typescript
|
|
128
|
-
* // Send and wait for completion with default 60s timeout
|
|
129
|
-
* const response = await session.sendAndWait({ prompt: "What is 2+2?" });
|
|
130
|
-
* console.log(response?.data.content); // "4"
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
async sendAndWait(options, timeout) {
|
|
101
|
+
async sendAndWait(optionsOrPrompt, timeout) {
|
|
102
|
+
const options = typeof optionsOrPrompt === "string" ? { prompt: optionsOrPrompt } : optionsOrPrompt;
|
|
134
103
|
const effectiveTimeout = timeout ?? 6e4;
|
|
135
104
|
let resolveIdle;
|
|
136
105
|
let rejectWithError;
|
|
@@ -694,8 +663,10 @@ class CopilotSession {
|
|
|
694
663
|
if (!this.hooks) {
|
|
695
664
|
return void 0;
|
|
696
665
|
}
|
|
666
|
+
const normalized = deserializeHookInput(input);
|
|
697
667
|
const handlerMap = {
|
|
698
668
|
preToolUse: this.hooks.onPreToolUse,
|
|
669
|
+
preMcpToolCall: this.hooks.onPreMcpToolCall,
|
|
699
670
|
postToolUse: this.hooks.onPostToolUse,
|
|
700
671
|
userPromptSubmitted: this.hooks.onUserPromptSubmitted,
|
|
701
672
|
sessionStart: this.hooks.onSessionStart,
|
|
@@ -707,7 +678,7 @@ class CopilotSession {
|
|
|
707
678
|
return void 0;
|
|
708
679
|
}
|
|
709
680
|
try {
|
|
710
|
-
const result = await handler(
|
|
681
|
+
const result = await handler(normalized, { sessionId: this.sessionId });
|
|
711
682
|
return result;
|
|
712
683
|
} catch (_error) {
|
|
713
684
|
return void 0;
|
|
@@ -724,7 +695,7 @@ class CopilotSession {
|
|
|
724
695
|
*
|
|
725
696
|
* @example
|
|
726
697
|
* ```typescript
|
|
727
|
-
* const events = await session.
|
|
698
|
+
* const events = await session.getEvents();
|
|
728
699
|
* for (const event of events) {
|
|
729
700
|
* if (event.type === "assistant.message") {
|
|
730
701
|
* console.log("Assistant:", event.data.content);
|
|
@@ -732,7 +703,7 @@ class CopilotSession {
|
|
|
732
703
|
* }
|
|
733
704
|
* ```
|
|
734
705
|
*/
|
|
735
|
-
async
|
|
706
|
+
async getEvents() {
|
|
736
707
|
const response = await this.connection.sendRequest("session.getMessages", {
|
|
737
708
|
sessionId: this.sessionId
|
|
738
709
|
});
|
|
@@ -772,18 +743,6 @@ class CopilotSession {
|
|
|
772
743
|
this.exitPlanModeHandler = void 0;
|
|
773
744
|
this.autoModeSwitchHandler = void 0;
|
|
774
745
|
}
|
|
775
|
-
/**
|
|
776
|
-
* @deprecated Use {@link disconnect} instead. This method will be removed in a future release.
|
|
777
|
-
*
|
|
778
|
-
* Disconnects this session and releases all in-memory resources.
|
|
779
|
-
* Session data on disk is preserved for later resumption.
|
|
780
|
-
*
|
|
781
|
-
* @returns A promise that resolves when the session is disconnected
|
|
782
|
-
* @throws Error if the connection fails
|
|
783
|
-
*/
|
|
784
|
-
async destroy() {
|
|
785
|
-
return this.disconnect();
|
|
786
|
-
}
|
|
787
746
|
/** Enables `await using session = ...` syntax for automatic cleanup. */
|
|
788
747
|
async [Symbol.asyncDispose]() {
|
|
789
748
|
return this.disconnect();
|