src/vs/platform/agentHost/node/codex/codexElicitationMapper.ts
177 LOC · 164 covered · 13 uncovered · 25 ranges · 32 concepts · 7 introducers · 17 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
codexElicitationMapper.ts ×7
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { hasKey } from '../../../../base/common/types.js';
import { ChatInputAnswerState, ChatInputAnswerValueKind, ChatInputQuestionKind, ChatInputResponseKind, type ChatInputAnswer, type ChatInputOption, type ChatInputQuestion, type ChatInputRequest } from '../../common/state/sessionState.js';
import type { JsonValue } from './protocol/generated/serde_json/JsonValue.js';
import type { McpElicitationPrimitiveSchema } from './protocol/generated/v2/McpElicitationPrimitiveSchema.js';
import type { McpServerElicitationRequestParams } from './protocol/generated/v2/McpServerElicitationRequestParams.js';
import type { McpServerElicitationRequestResponse } from './protocol/generated/v2/McpServerElicitationRequestResponse.js';
/**
* Translate a codex `mcpServer/elicitation/request` into an agent-host
* {@link ChatInputRequest}. Three modes are supported, mirroring the MCP
* elicitation spec:
*
* - `form` — projects each field of the requested JSON schema into a
* {@link ChatInputQuestion} (text / number / boolean / single- or
* multi-select), reusing the same chat-input surface as the model's
* `ask_user` tool.
* - `openai/form` — carries an opaque, OpenAI-specific form schema we
* cannot project into typed questions; surfaces the message only so the
* user can still accept or decline.
* - `url` — surfaces the URL the server wants the user to open via
* {@link ChatInputRequest.url} with no questions.
*
* MCP field names are used directly as the stable question id (the key
* the answer map is later read back by).
*/
export function buildElicitationRequest(requestId: string, params: McpServerElicitationRequestParams): ChatInputRequest {
const request: ChatInputRequest = { id: requestId, message: params.message };
codexElicitationMapper.ts ×1
if (params.url) {
request.url = params.url;
}
return request;
}
// `openai/form` carries an opaque, OpenAI-specific schema we cannot
// project into typed questions; surface the message only so the user
// can still accept or decline.
return { id: requestId, message: params.message };
}
const questions: ChatInputQuestion[] = [];
for (const [name, field] of Object.entries(params.requestedSchema.properties)) {
questions.push(elicitationFieldToQuestion(name, field, required.has(name)));
}
}
return questions.length > 0
? { id: requestId, message: params.message, questions }
: { id: requestId, message: params.message };
/**
* Build the codex elicitation response from the client's answers. A
* declined request maps to `decline`, a cancelled/closed request to
* `cancel`, and an accepted request to `accept` with a `content` object
* keyed by field name (omitting skipped/missing answers). `url`-mode
* acceptances carry no content.
*/
export function elicitationResponseFromAnswers(
response: ChatInputResponseKind,
answers: Record<string, ChatInputAnswer> | undefined,
): McpServerElicitationRequestResponse {
if (response === ChatInputResponseKind.Decline) {
}
}
// `url` and `openai/form` acceptances carry no projected content.
codexElicitationMapper.ts ×1
return { action: 'accept', content: null, _meta: null };
}
for (const [name, field] of Object.entries(params.requestedSchema.properties)) {
if (!field) {
continue;
}
if (value !== undefined) {
content[name] = value;
}
}
return { action: 'accept', content, _meta: null };
}
/** Decline response used when there is no session to route the elicitation to. */
export function declinedElicitationResponse(): McpServerElicitationRequestResponse {
return { action: 'decline', content: null, _meta: null };
}
/** Cancel response used when the session is torn down mid-elicitation. */
export function cancelledElicitationResponse(): McpServerElicitationRequestResponse {
return { action: 'cancel', content: null, _meta: null };
}
function elicitationFieldToQuestion(id: string, field: McpElicitationPrimitiveSchema, required: boolean): ChatInputQuestion {
codexElicitationMapper.ts ×5
const base = { id, title: field.title, message: field.description ?? field.title ?? id, required };
switch (field.type) {
case 'boolean':
return { ...base, kind: ChatInputQuestionKind.Boolean, defaultValue: field.default };
case 'number':
case 'integer':
return {
...base,
kind: field.type === 'integer' ? ChatInputQuestionKind.Integer : ChatInputQuestionKind.Number,
min: field.minimum,
max: field.maximum,
defaultValue: field.default,
};
case 'array':
return {
...base,
kind: ChatInputQuestionKind.MultiSelect,
options: hasKey(field.items, { anyOf: true })
? field.items.anyOf.map((o): ChatInputOption => ({ id: o.const, label: o.title || o.const }))
: field.items.enum.map((v): ChatInputOption => ({ id: v, label: v })),
codexElicitationMapper.ts ×5
min: bigintToNumber(field.minItems),
max: bigintToNumber(field.maxItems),
};
case 'string':
// Titled single-select (`oneOf`), enum/legacy single-select (`enum`,
// optionally `enumNames`), or a plain text field.
if (hasKey(field, { oneOf: true })) {
return {
...base,
kind: ChatInputQuestionKind.SingleSelect,
options: field.oneOf.map((o): ChatInputOption => ({ id: o.const, label: o.title || o.const })),
};
}
if (hasKey(field, { enum: true })) {
const names: readonly string[] | undefined = (field as { enumNames?: readonly string[] }).enumNames;
return {
...base,
kind: ChatInputQuestionKind.SingleSelect,
options: field.enum.map((v, i): ChatInputOption => ({ id: v, label: names?.[i] || v })),
};
}
return {
...base,
kind: ChatInputQuestionKind.Text,
format: field.format,
min: field.minLength,
max: field.maxLength,
defaultValue: field.default,
};
}
}
function elicitationAnswerToValue(answer: ChatInputAnswer | undefined): JsonValue | undefined {
codexElicitationMapper.ts ×5
if (!answer || answer.state === ChatInputAnswerState.Skipped) {
return undefined;
}
const { value } = answer;
switch (value.kind) {
case ChatInputAnswerValueKind.Text:
return value.value;
case ChatInputAnswerValueKind.Number:
return value.value;
case ChatInputAnswerValueKind.Boolean:
return value.value;
case ChatInputAnswerValueKind.Selected:
return value.value;
case ChatInputAnswerValueKind.SelectedMany:
return value.value;
}
}
function bigintToNumber(value: bigint | null | undefined): number | undefined {
codexElicitationMapper.ts ×5
return value === null || value === undefined ? undefined : Number(value);
}