173 lines
6.0 KiB
TypeScript
173 lines
6.0 KiB
TypeScript
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
import { Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
import { UI_DIMENSIONS } from "./constants/ui.ts";
|
|
import { renderResultText } from "./result.ts";
|
|
import { createInitialState } from "./state/create.ts";
|
|
import { collectValidationIssues } from "./state/normalize.ts";
|
|
import { summarizeResult, toAskResult } from "./state/result.ts";
|
|
import type {
|
|
AskParams,
|
|
AskQuestionInput,
|
|
AskResult,
|
|
AskValidationIssue,
|
|
} from "./types.ts";
|
|
|
|
export const ASK_TOOL_DESCRIPTION =
|
|
"Interactive clarification tool for cases where the next step depends on user preferences, missing requirements, or choosing between multiple valid directions. Ask a short structured interview, collect normalized answers, and continue using those answers explicitly instead of guessing. Supports single-select, multi-select, and preview-pane questions. Always include a stable `id` and non-empty `prompt` for every question, plus a machine-readable `value` and visible `label` for every option. Use `preview` only when every option includes `preview` text; descriptions alone are not enough.";
|
|
|
|
export const ASK_TOOL_PROMPT_GUIDELINES = [
|
|
"Use `ask_user` before making preference-sensitive decisions about scope, tone, UX, naming, architecture, docs, or implementation direction.",
|
|
"When multiple valid directions exist, call `ask_user` with 1-3 concise questions instead of committing to one path on your own.",
|
|
"When calling `ask_user`, prefer one focused decision per question. Use short labels. Provide clear, distinct options. Do not add filler options.",
|
|
"When calling `ask_user`, always include a stable `id` and non-empty `prompt` for every question.",
|
|
"When calling `ask_user`, always include a non-empty machine-readable `value` and visible `label` for every option.",
|
|
"When calling `ask_user`, choose question `type` from the question semantics: `single` means one answer is expected, `multi` means multiple answers could reasonably be selected, and `preview` means options need preview-pane detail.",
|
|
'When calling `ask_user`, use `type: "preview"` only when every option includes non-empty `preview` text. Option descriptions do not satisfy this requirement.',
|
|
"After an `ask_user` elaboration or follow-up note, prefer another structured `ask_user` follow-up if a choice is still needed instead of switching to plain-text multiple choice in chat.",
|
|
"When prior `ask_user` answers narrow the branch, bundle the next 2-3 related unresolved decisions into one follow-up `ask_user` call when possible.",
|
|
"Use one-at-a-time `ask_user` follow-up calls only when the next question materially depends on the previous answer.",
|
|
] as const;
|
|
|
|
interface ValidateParamsOptions {
|
|
allowFreeform?: boolean;
|
|
presentSingleAsMulti?: boolean;
|
|
}
|
|
|
|
export function validateParams(
|
|
params: AskParams,
|
|
options: ValidateParamsOptions = {}
|
|
):
|
|
| { ok: true; state: ReturnType<typeof createInitialState> }
|
|
| { ok: false; issues: AskValidationIssue[] } {
|
|
const issues = collectValidationIssues(params, options);
|
|
if (issues.length > 0) {
|
|
return { ok: false, issues };
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
state: createInitialState(params, options),
|
|
};
|
|
}
|
|
|
|
export function invalidPayloadResponse(
|
|
params: AskParams,
|
|
issues: AskValidationIssue[]
|
|
) {
|
|
return {
|
|
content: [{ type: "text" as const, text: formatValidationError(issues) }],
|
|
details: errorResultDetails(params, issues),
|
|
};
|
|
}
|
|
|
|
export function nonInteractiveResponse(
|
|
state: ReturnType<typeof createInitialState>
|
|
) {
|
|
return {
|
|
content: [
|
|
{ type: "text" as const, text: formatNonInteractiveMessage(state) },
|
|
],
|
|
details: {
|
|
...toAskResult(state),
|
|
cancelled: true,
|
|
},
|
|
};
|
|
}
|
|
|
|
export function successfulResponse(result: AskResult) {
|
|
return {
|
|
content: [{ type: "text" as const, text: summarizeResult(result) }],
|
|
details: result,
|
|
};
|
|
}
|
|
|
|
type ToolTheme = ExtensionContext["ui"]["theme"];
|
|
|
|
export function renderAskToolCall(args: unknown, theme: ToolTheme) {
|
|
const params = args as AskParams;
|
|
const labels = Array.isArray(params.questions)
|
|
? params.questions
|
|
.map(
|
|
(question: AskQuestionInput, index) =>
|
|
question.label?.trim() || `Q${index + 1}`
|
|
)
|
|
.join(", ")
|
|
: "";
|
|
let text = theme.fg("toolTitle", theme.bold("ask_user "));
|
|
text += theme.fg("muted", `${params.questions?.length ?? 0} question(s)`);
|
|
if (labels) {
|
|
text += theme.fg(
|
|
"dim",
|
|
` (${truncateToWidth(labels, UI_DIMENSIONS.callLabelTruncateWidth)})`
|
|
);
|
|
}
|
|
return new Text(text, 0, 0);
|
|
}
|
|
|
|
export function renderAskToolResult(
|
|
result: {
|
|
content: Array<{ type?: string; text?: string }>;
|
|
details?: AskResult;
|
|
},
|
|
_options: unknown,
|
|
theme: ToolTheme
|
|
) {
|
|
const details = result.details;
|
|
if (!(details && Array.isArray(details.questions))) {
|
|
const text = result.content[0];
|
|
return new Text(text?.type === "text" ? (text.text ?? "") : "", 0, 0);
|
|
}
|
|
const text = renderResultText(details);
|
|
return new Text(
|
|
details.error || details.cancelled ? theme.fg("warning", text) : text,
|
|
0,
|
|
0
|
|
);
|
|
}
|
|
|
|
function errorResultDetails(
|
|
params: AskParams,
|
|
issues: AskValidationIssue[]
|
|
): AskResult {
|
|
return {
|
|
title: params.title,
|
|
cancelled: true,
|
|
mode: "submit",
|
|
questions: [],
|
|
answers: {},
|
|
error: {
|
|
kind: "invalid_input",
|
|
issues,
|
|
},
|
|
};
|
|
}
|
|
|
|
function formatValidationError(issues: AskValidationIssue[]): string {
|
|
return [
|
|
"Invalid ask_user payload:",
|
|
...issues.map((issue) => `- ${issue.path}: ${issue.message}`),
|
|
].join("\n");
|
|
}
|
|
|
|
function formatNonInteractiveMessage(
|
|
state: ReturnType<typeof createInitialState>
|
|
): string {
|
|
const lines = [
|
|
"Needs user input: ask_user requires interactive TUI mode.",
|
|
"Run same tool call in interactive TUI mode, or ask user these questions manually:",
|
|
];
|
|
|
|
for (const [index, question] of state.questions.entries()) {
|
|
lines.push(`${index + 1}. ${question.label}: ${question.prompt}`);
|
|
for (const option of question.options) {
|
|
lines.push(` - ${option.label} [${option.value}]`);
|
|
}
|
|
lines.push(" - Type your own [custom]");
|
|
}
|
|
|
|
lines.push(
|
|
"details.questions contains normalized pending questions. details.answers stays empty until user responds."
|
|
);
|
|
return lines.join("\n");
|
|
}
|