Skip to content

Commit 8043d3d

Browse files
React to runtime changes
1 parent 296c48e commit 8043d3d

2 files changed

Lines changed: 38 additions & 49 deletions

File tree

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,
@@ -1607,18 +1605,6 @@ export class CopilotClient {
16071605
await this.handleUserInputRequest(params)
16081606
);
16091607

1610-
this.connection.onRequest(
1611-
"elicitation.request",
1612-
async (params: {
1613-
sessionId: string;
1614-
requestId: string;
1615-
message: string;
1616-
requestedSchema?: unknown;
1617-
mode?: "form" | "url";
1618-
elicitationSource?: string;
1619-
}): Promise<ElicitationResult> => await this.handleElicitationRequest(params)
1620-
);
1621-
16221608
this.connection.onRequest(
16231609
"hooks.invoke",
16241610
async (params: {
@@ -1726,34 +1712,6 @@ export class CopilotClient {
17261712
return result;
17271713
}
17281714

1729-
private async handleElicitationRequest(params: {
1730-
sessionId: string;
1731-
requestId: string;
1732-
message: string;
1733-
requestedSchema?: unknown;
1734-
mode?: "form" | "url";
1735-
elicitationSource?: string;
1736-
}): Promise<ElicitationResult> {
1737-
if (!params || typeof params.sessionId !== "string" || typeof params.message !== "string") {
1738-
throw new Error("Invalid elicitation request payload");
1739-
}
1740-
1741-
const session = this.sessions.get(params.sessionId);
1742-
if (!session) {
1743-
throw new Error(`Session not found: ${params.sessionId}`);
1744-
}
1745-
1746-
return await session._handleElicitationRequest(
1747-
{
1748-
message: params.message,
1749-
requestedSchema: params.requestedSchema as ElicitationRequest["requestedSchema"],
1750-
mode: params.mode,
1751-
elicitationSource: params.elicitationSource,
1752-
},
1753-
params.sessionId
1754-
);
1755-
}
1756-
17571715
private async handleHooksInvoke(params: {
17581716
sessionId: string;
17591717
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)