import { ClineDefaultTool } from "@shared/tools"

export type AssistantMessageContent = TextStreamContent | ToolUse | ReasoningStreamContent

export { parseAssistantMessageV2 } from "./parse-assistant-message"

export interface TextStreamContent {
	type: "text"
	content: string
	partial: boolean
}

export const toolParamNames = [
	"command",
	"requires_approval",
	"path",
	"absolutePath",
	"content",
	"diff",
	"regex",
	"file_pattern",
	"recursive",
	"action",
	"url",
	"coordinate",
	"text",
	"query",
	"allowed_domains",
	"blocked_domains",
	"prompt",
	"server_name",
	"tool_name",
	"arguments",
	"uri",
	"question",
	"options",
	"response",
	"result",
	"context",
	"title",
	"what_happened",
	"steps_to_reproduce",
	"api_request_output",
	"additional_context",
	"needs_more_exploration",
	"task_progress",
	"timeout",
	"input",
	"from_ref",
	"to_ref",
	"skill_name",
	"operation",
	"port",
	"duration",
	"devices",
	"output",
	"filename",
	// triggerCveScan params — MUST be here or the parser silently drops <sbom>/<build> and the handler
	// reports "Missing value for required parameter 'sbom'" no matter what the model sends (observed on a
	// live cra-sample run). A tool's params are only extracted if their names are in this list.
	"sbom",
	"build",
	// triggerNordicAction param — MUST be here or the parser silently drops <ncs_version>, so multi-NCS
	// disambiguation can NEVER resolve: the explicit override never reaches selectNcsInstall and every command
	// fails "Multiple NCS versions installed… re-run with ncs_version" even when one IS supplied (observed on a
	// live run with v3.2.1 + v3.3.1 both installed — the agent + user picked a version 8× and it was ignored).
	"ncs_version",
] as const

export type ToolParamName = (typeof toolParamNames)[number]

export interface ToolUse {
	type: "tool_use"
	name: ClineDefaultTool // id of the tool being used
	// params is a partial record, allowing only some or none of the possible parameters to be used
	params: Partial<Record<ToolParamName, string>>
	partial: boolean
	/**
	 * Whether this tool use was initiated by a native tool call
	 */
	isNativeToolCall?: boolean
	/**
	 * The call / response ID this tool use is associated with.
	 */
	call_id?: string // optional call ID for tracking tool use calls
	/**
	 * Thought signature associated with this tool use, used by Gemini
	 */
	signature?: string
}

export interface ReasoningStreamContent {
	type: "reasoning"
	/**
	 * The reasoning text generated by the model.
	 * Redacted reasoning block will have this field set to "[REDACTED]" or an empty string.
	 */
	reasoning: string
	/**
	 * openrouter has various properties that we can pass back unmodified in api requests to preserve reasoning traces
	 */
	details?: any
	/**
	 * It's used when sending the thinking block back to the  API.
	 * API expects this in completed form, not as array of deltas.
	 */
	signature?: string
	/**
	 * whether this reasoning block has been redacted
	 */
	redacted?: boolean
	/**
	 * redacted data
	 */
	data?: string
	/**
	 * Indicates whether this is a partial reasoning block
	 */
	partial: boolean
}
