import { useEffect, useRef, useState } from "react";
import { displayModelName, type SessionConfigOption, type SessionSummary } from "@linkshell/wire";
import { ChevronDown, Folder, Settings2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Icon } from "../icons";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { ErrorNotice, useActions, useJob } from "./common";
/** Keep settings recognisable in both the new-session form and an existing conversation. */
export function SessionConfigIcon({ category, value = "" }: { category: SessionConfigOption["category"]; value?: string }) {
const name = category === "model" ? "model"
: category === "effort" ? "effort"
: /plan/i.test(value) ? "plan"
: /bypass|full|yolo|never/i.test(value) ? "unlocked" : "shield";
return ;
}
/** Settings outside model, effort and mode that shipped drivers report, by id or by English name. */
const OPTION_NAMES: Record = {
plan: "计划模式",
"plan mode": "计划模式",
fast: "快速模式",
"fast mode": "快速模式",
agent: "Agent 角色",
"approval policy": "审批策略",
approval_policy: "审批策略",
sandbox: "沙盒",
"sandbox mode": "沙盒模式",
};
export function sessionConfigLabel(option: SessionConfigOption): string {
if (option.category === "model") return "模型";
if (option.category === "effort") return "推理强度";
const known = OPTION_NAMES[option.id] ?? OPTION_NAMES[option.name.trim().toLowerCase()];
if (known) return known;
return option.category === "mode" ? "权限模式" : option.name;
}
/** What those settings say about themselves, in Chinese; an agent's own wording stays as given. */
const OPTION_DESCRIPTIONS: Record = {
"faster responses on supported models": "在支持的模型上回复更快,额度消耗更多",
"main-thread agent persona": "主对话使用的 Agent 角色",
"session permission mode": "会话的权限模式",
"available effort levels for this model": "这个模型可选的推理强度",
"ai model to use": "使用的模型",
};
export function optionDescription(text: string): string {
const trimmed = text.trim();
// Claude appends why fast mode is unavailable: "… — not available on the free plan".
const [head, reason] = trimmed.split(/\s+—\s+/, 2);
const base = OPTION_DESCRIPTIONS[(head ?? trimmed).toLowerCase()];
if (!base) return text;
return reason ? `${base}(${FAST_MODE_REASONS[reason.toLowerCase()] ?? reason})` : base;
}
const FAST_MODE_REASONS: Record = {
"not available on the free plan": "免费套餐不可用",
"requires extra usage to be enabled for this account": "需要为账号开启额外用量",
"not available for the selected model": "当前模型不支持",
"not available on this api provider": "当前 API 服务商不支持",
"disabled by environment configuration": "已被环境配置关闭",
"eligibility could not be verified (network error)": "网络错误,无法确认是否可用",
};
/**
* The settings people change mid-conversation, on the composer itself: mode, model and effort,
* each a menu of its values, under the prompt.
* Everything else stays in the settings panel.
*/
/**
* Agents name their modes, models and efforts in English. The common ones read in Chinese here;
* anything else is shown as the agent says it.
*/
const VALUE_NAMES: Record = {
"default": "默认",
"default (recommended)": "默认(推荐)",
"bypass permissions": "免确认",
"bypasspermissions": "免确认",
"accept edits": "自动接受编辑",
"acceptedits": "自动接受编辑",
"plan": "计划模式",
"plan mode": "计划模式",
"read only": "只读",
"read-only": "只读",
"auto": "自动",
"full access": "完全访问",
"full-access": "完全访问",
"ask": "每次询问",
"manual": "每次询问",
"minimal": "最少",
"low": "低",
"medium": "中",
"high": "高",
"xhigh": "很高",
"max": "最高",
"ultra": "极高",
"none": "关闭",
"on": "开",
"off": "关",
"custom": "自定义",
// Gemini's approval modes.
"auto edit": "自动编辑",
"yolo": "全部自动批准",
};
export function valueName(name: string): string {
return VALUE_NAMES[name.trim().toLowerCase()] ?? displayModelName(name);
}
/** What Claude's and Codex's permission modes say they do, in Chinese; anything else as given. */
const DESCRIPTIONS: Record = {
"always ask before making changes": "改动前都先问你",
"automatically accept all file edits": "文件改动自动接受,其他操作仍会问你",
"create a plan before making changes": "先出计划,确认后再动手",
"claude handles permission decisions": "由 Claude 判断哪些操作需要问你",
"accepts all permissions": "所有操作都不再询问",
"can read files; asks before editing or running commands": "可以读文件,编辑或运行命令前会问你",
"edits and runs commands in the workspace; asks before anything else": "在工作区内编辑和运行命令,其他操作会问你",
"edits and runs anything without asking": "编辑和运行任何东西都不问",
"discuss and plan first; no changes until the plan is agreed": "先讨论和计划,计划确认前不做改动",
"set in codex's own config": "按 Codex 自己的配置",
"standard claude code agent": "标准的 Claude Code Agent",
// Codex's reasoning efforts.
"fast responses with lighter reasoning": "回复快,思考较浅",
"balances speed and reasoning depth for everyday tasks": "兼顾速度和思考深度,适合日常任务",
"greater reasoning depth for complex problems": "思考更深,适合复杂问题",
"extra high reasoning depth for complex problems": "思考很深,适合复杂问题",
"maximum reasoning depth for the hardest problems": "最深的思考,适合最难的问题",
"maximum reasoning with automatic task delegation": "最深的思考,并自动分派子任务",
// Gemini's approval modes.
"prompts for approval": "每次操作前询问",
"auto-approves edit tools": "自动批准编辑,其他操作仍会询问",
"auto-approves all tools": "自动批准所有操作",
"read-only mode": "只读,先出计划",
};
export function valueDescription(text: string): string {
return DESCRIPTIONS[text.trim().toLowerCase()] ?? text;
}
export interface ComposerPickerRequest {
category: "model" | "mode" | "effort";
nonce: number;
}
export function ComposerConfig({
session,
config,
disabled,
openSettings,
pickerRequest,
}: {
session: SessionSummary;
config: SessionConfigOption[];
disabled: boolean;
openSettings: () => void;
pickerRequest?: ComposerPickerRequest;
}) {
const actions = useActions();
const job = useJob();
const [openCategory, setOpenCategory] = useState<"model" | "effort" | "mode" | null>(null);
const handledNonce = useRef(0);
const shown = (["model", "effort", "mode"] as const)
.map((category) => config.find((option) => option.category === category && option.type !== "boolean" && option.values.length > 1))
.filter((option): option is SessionConfigOption => !!option);
useEffect(() => {
if (!pickerRequest || pickerRequest.nonce === handledNonce.current) return;
handledNonce.current = pickerRequest.nonce;
if (disabled || job.busy) return;
const match = shown.find((option) => option.category === pickerRequest.category);
if (match) {
setOpenCategory(match.category as "model" | "effort" | "mode");
if (match.category === "model") {
void job.run(() => actions.agentModels(session.agent, session.cwd));
}
return;
}
if (pickerRequest.category === "mode") {
const boolMode = config.find(
(option) =>
option.type === "boolean" &&
(option.category === "mode" || /plan/i.test(option.id) || /plan/i.test(option.name)),
);
if (boolMode) {
void job.run(() =>
actions.setConfig(
session.id,
boolMode.id,
boolMode.current === "true" ? "false" : "true",
),
);
return;
}
}
openSettings();
}, [actions, config, disabled, job, openSettings, pickerRequest, session.agent, session.cwd, session.id, shown]);
return (
<>
{shown.map((option) => {
const current = option.values.find((value) => value.value === option.current);
const label = sessionConfigLabel(option);
const category = option.category as "model" | "effort" | "mode";
return (
{
setOpenCategory((prev) => (open ? category : prev === category ? null : prev));
if (open && option.category === "model") void job.run(() => actions.agentModels(session.agent, session.cwd));
}}
>
{option.category === "model" && job.busy ? "正在更新模型…" : label}
void job.run(() => actions.setConfig(session.id, option.id, value))}
>
{option.values.map((value) => (
{value.group ? `${value.group} · ${valueName(value.name)}` : valueName(value.name)}
{value.description && {valueDescription(value.description)}}
))}
全部会话设置
);
})}
{!shown.length && (
)}
>
);
}
/** Where the work happens: the project, and the worktree's branch when the session has its own. */
export function ComposerPlace({ session }: { session: SessionSummary }) {
const project = session.worktree?.source ?? session.cwd;
const name = project.split(/[\\/]/).filter(Boolean).pop() ?? project;
return (
{name}
{session.worktree && {session.worktree.branch}}
);
}
/** Context used, as a ring that fills; the numbers on hover. */
export function ContextRing({ used, window }: { used: number; window: number }) {
const ratio = Math.min(1, Math.max(0, used / window));
const radius = 7;
const length = 2 * Math.PI * radius;
const format = (value: number) => (value >= 1000 ? `${Math.round(value / 100) / 10}K` : String(value));
return (
0.9 ? "high" : ratio > 0.7 ? "warm" : undefined}
title={`上下文 ${Math.round(ratio * 100)}% · ${format(used)} / ${format(window)} Token`}
aria-label={`上下文已用 ${Math.round(ratio * 100)}%`}
>
{Math.round(ratio * 100)}%
);
}