/**
 * Agent Loop — 内部类型定义
 *
 * 用于 Agent Loop 引擎内部的状态管理和迭代记录。
 */

// ===== Agent Loop 状态机 =====

export enum AgentLoopState {
  INIT = 'INIT',
  THINKING = 'THINKING',
  PARSING = 'PARSING',
  EXECUTING = 'EXECUTING',
  OBSERVING = 'OBSERVING',
  REFLECTING = 'REFLECTING',
  COMPRESSING = 'COMPRESSING',
  TERMINATED = 'TERMINATED',
}

export enum TerminationReason {
  COMPLETED = 'completed',
  MAX_ITERATIONS = 'max_iterations',
  TIMEOUT = 'timeout',
  USER_INTERRUPT = 'user_interrupt',
  ERROR = 'error',
}

// ===== 迭代步骤 =====

export interface Thought {
  id: string;
  content: string;
  reasoningContent?: string;
  timestamp: number;
  iteration: number;
}

export interface IterationStep {
  iteration: number;
  state: AgentLoopState;
  startedAt: number;
  completedAt?: number;
  thought?: Thought;
  toolCalls?: import('@shared/index').MetonaToolCall[];
  toolResults?: import('@shared/index').MetonaToolResult[];
  tokenUsage?: TokenUsage;
}

export interface TokenUsage {
  promptTokens: number;
  completionTokens: number;
  totalTokens: number;
}

// ===== Agent Loop 配置 =====

export interface AgentLoopConfig {
  maxIterations: number;
  timeoutMs: number;
  totalTimeoutMs: number;
  enableReflection: boolean;
  compressionThreshold: number;
  contextWindow: number;
  retryCount: number;
  temperature: number;
  /** Ollama 上下文窗口大小（num_ctx），其他 Provider 忽略 */
  contextLength?: number;
}

// ===== Agent Loop 输出 =====

export interface AgentLoopOutput {
  finalAnswer: string;
  terminationReason: TerminationReason;
  iterations: IterationStep[];
  totalTokenUsage: TokenUsage;
  durationMs: number;
  metadata: Record<string, unknown>;
}
