// tools/builtin/clarify.ts
import { Type } from '@sinclair/typebox';
import { AgentToolWithMeta, textResult } from '../common.js';
import { registerSchema } from '../catalog.js';

const ClarifyParams = Type.Object({
  question: Type.String({ description: '要问用户的问题' }),
  choices: Type.Optional(Type.Array(Type.String(), { maxItems: 4, description: '预设选项（最多4个）' })),
});

/** 降级 execute：当无 WebSocket 回调时，返回问题文本让 Agent 作为回复输出 */
export const clarifyTool: AgentToolWithMeta<typeof ClarifyParams, unknown> = {
  name: 'clarify',
  label: '向用户提问',
  description: '当任务需求不明确时，向用户提出澄清问题。支持选择题和开放式问题。',
  parameters: ClarifyParams,
  async execute(_id, params) {
    const choicesText = params.choices?.length
      ? `\n选项: ${params.choices.map((c, i) => `${i + 1}. ${c}`).join(', ')}`
      : '';
    return textResult(`[需要用户确认] ${params.question}${choicesText}`);
  },
};

registerSchema({
  name: 'clarify',
  toolset: 'interaction',
  description: clarifyTool.description,
  capability: 'text',
  visibility: 'tool',
  isCore: true,
  canDisable: false,
  supportsPromptHint: true,
});
