Skip to content

Commit 3a1ec67

Browse files
React to runtime changes
1 parent 4438bb9 commit 3a1ec67

File tree

2 files changed

+38
-49
lines changed

2 files changed

+38
-49
lines changed

nodejs/src/client.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ import { getTraceContext } from "./telemetry.js";
3131
import type {
3232
ConnectionState,
3333
CopilotClientOptions,
34-
ElicitationRequest,
35-
ElicitationResult,
3634
ForegroundSessionInfo,
3735
GetAuthStatusResponse,
3836
GetStatusResponse,
@@ -1551,18 +1549,6 @@ export class CopilotClient {
15511549
await this.handleUserInputRequest(params)
15521550
);
15531551

1554-
this.connection.onRequest(
1555-
"elicitation.request",
1556-
async (params: {
1557-
sessionId: string;
1558-
requestId: string;
1559-
message: string;
1560-
requestedSchema?: unknown;
1561-
mode?: "form" | "url";
1562-
elicitationSource?: string;
1563-
}): Promise<ElicitationResult> => await this.handleElicitationRequest(params)
1564-
);
1565-
15661552
this.connection.onRequest(
15671553
"hooks.invoke",
15681554
async (params: {
@@ -1670,34 +1656,6 @@ export class CopilotClient {
16701656
return result;
16711657
}
16721658

1673-
private async handleElicitationRequest(params: {
1674-
sessionId: string;
1675-
requestId: string;
1676-
message: string;
1677-
requestedSchema?: unknown;
1678-
mode?: "form" | "url";
1679-
elicitationSource?: string;
1680-
}): Promise<ElicitationResult> {
1681-
if (!params || typeof params.sessionId !== "string" || typeof params.message !== "string") {
1682-
throw new Error("Invalid elicitation request payload");
1683-
}
1684-
1685-
const session = this.sessions.get(params.sessionId);
1686-
if (!session) {
1687-
throw new Error(`Session not found: ${params.sessionId}`);
1688-
}
1689-
1690-
return await session._handleElicitationRequest(
1691-
{
1692-
message: params.message,
1693-
requestedSchema: params.requestedSchema as ElicitationRequest["requestedSchema"],
1694-
mode: params.mode,
1695-
elicitationSource: params.elicitationSource,
1696-
},
1697-
params.sessionId
1698-
);
1699-
}
1700-
17011659
private async handleHooksInvoke(params: {
17021660
sessionId: string;
17031661
hookType: string;

nodejs/src/session.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ export class CopilotSession {
417417
args: string;
418418
};
419419
void this._executeCommandAndRespond(requestId, commandName, command, args);
420+
} else if ((event as { type: string }).type === "elicitation.requested") {
421+
// TODO: Remove type casts above once session-events codegen includes these event types
422+
if (this.elicitationHandler) {
423+
const data = (event as { data: Record<string, unknown> }).data;
424+
void this._handleElicitationRequest(
425+
{
426+
message: data.message as string,
427+
requestedSchema:
428+
data.requestedSchema as ElicitationRequest["requestedSchema"],
429+
mode: data.mode as ElicitationRequest["mode"],
430+
elicitationSource: data.elicitationSource as string | undefined,
431+
},
432+
data.requestId as string
433+
);
434+
}
420435
} else if ((event as { type: string }).type === "capabilities.changed") {
421436
const data = (event as { data: Partial<SessionCapabilities> }).data;
422437
this._capabilities = { ...this._capabilities, ...data };
@@ -598,17 +613,33 @@ export class CopilotSession {
598613
}
599614

600615
/**
601-
* Handles an elicitation.request RPC callback from the server.
616+
* Handles an elicitation.requested broadcast event.
617+
* Invokes the registered handler and responds via handlePendingElicitation RPC.
602618
* @internal
603619
*/
604-
async _handleElicitationRequest(
605-
request: ElicitationRequest,
606-
sessionId: string
607-
): Promise<ElicitationResult> {
620+
async _handleElicitationRequest(request: ElicitationRequest, requestId: string): Promise<void> {
608621
if (!this.elicitationHandler) {
609-
throw new Error("Elicitation requested but no handler registered");
622+
return;
623+
}
624+
try {
625+
const result = await this.elicitationHandler(request, { sessionId: this.sessionId });
626+
await this.connection.sendRequest("session.ui.handlePendingElicitation", {
627+
sessionId: this.sessionId,
628+
requestId,
629+
result,
630+
});
631+
} catch {
632+
// Handler failed — attempt to cancel so the request doesn't hang
633+
try {
634+
await this.connection.sendRequest("session.ui.handlePendingElicitation", {
635+
sessionId: this.sessionId,
636+
requestId,
637+
result: { action: "cancel" },
638+
});
639+
} catch {
640+
// Best effort — another client may have already responded
641+
}
610642
}
611-
return await this.elicitationHandler(request, { sessionId });
612643
}
613644

614645
/**

0 commit comments

Comments
 (0)