import type {
  FailureRecoverability,
  FailureScope,
  ItemId,
  ThreadId,
  ToolFailureKind,
  ToolResultOutcome,
  TurnId,
} from "@best-agent/protocol";
import type {
  ToolDispatchCheckpointPayload,
  ToolResumeCheckpointPayload,
  ToolResumeContext,
} from "./checkpoint-codec.js";
import type { ToolInputMode } from "../tool/registry.js";

export type ThreadErrorTraceRecord = {
  callId?: string;
  childThreadId?: ThreadId;
  errorCode?: string;
  failureKind?: ToolFailureKind;
  itemId: ItemId;
  itemKind: "error" | "tool-result";
  message: string;
  outcome?: ToolResultOutcome;
  recoverability?: FailureRecoverability;
  scope?: FailureScope;
  threadId: ThreadId;
  timestamp: string;
  toolName?: string;
  traceAssetId?: string;
  turnId: TurnId;
};

export type ExecuteToolInvocationOptions = {
  customLoopReplay?: CustomLoopReplayState;
  resumeCheckpoint?: ToolResumeCheckpointPayload | ToolDispatchCheckpointPayload;
  resumeContext?: ToolResumeContext;
  skipPrelude?: boolean;
  toolInputMode?: ToolInputMode;
};

export type CustomLoopToolReplayResult = {
  output: string;
  toolInputDigest?: string;
  toolInputKey: string;
  toolInputStableDigest?: string;
  toolName: string;
};

export type CustomLoopReplayState = {
  evidenceKeys: Set<string>;
  toolResults: CustomLoopToolReplayResult[];
};
