/**
 * 思考工具（1 个）
 *
 * think — 结构化思考空间（无副作用）
 *
 * 参考 Claude Code 的 think 工具设计。
 * 帮助 LLM 在工具调用上下文中组织思路。
 */

import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';

export class ThinkTool implements IMetonaTool {
  readonly definition: MetonaToolDef = {
    name: 'think',
    description:
      'Use this tool for structured thinking. It helps you organize your thoughts before complex actions. This tool has no side effects — it simply returns your input for your own reference. Use it when: 1) Breaking down complex tasks into steps, 2) Evaluating multiple approaches, 3) Planning before writing code, 4) Reviewing your work before completing.',
    parameters: {
      type: 'object',
      properties: {
        thought: { type: 'string', description: 'The thought content' },
        nextAction: { type: 'string', description: 'Next action plan' },
        reasoning: { type: 'string', description: 'Reasoning process' },
      },
      required: ['thought'],
    },
    category: MetonaToolCategory.CUSTOM,
    riskLevel: MetonaRiskLevel.SAFE,
    requiresPermission: false,
    timeoutMs: 1_000,
  };

  async execute(args: Record<string, unknown>, _context: ToolExecutionContext): Promise<unknown> {
    try {
      const thought = args.thought as string;
      const nextAction = (args.nextAction as string) ?? null;
      const reasoning = (args.reasoning as string) ?? null;

      return {
        thought,
        nextAction,
        reasoning,
        timestamp: Date.now(),
      };
    } catch (error) {
      const errMsg = error instanceof Error ? error.message : String(error);
      return { error: errMsg, success: false };
    }
  }
}
