Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tauri-plugin-deep-link = "2"
# 编出来的东西不一样;而更要紧的是,打进 `.app` 的那个 twcore 二进制
# 必须和这里编译进去的协议镜像来自同一个 core 版本 —— 打包脚本正是从
# 这个 tag 去取二进制的(见 scripts/fetch-core.sh)。
tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.19.0" }
tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.21.0" }

anyhow = "1"
libc = "0.2"
Expand Down
19 changes: 11 additions & 8 deletions src/App.i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export const appText = messages(
loadingConfig: "读取配置中…",

// 过滤条
search: "搜索路径、客户端、上游、错误… ⌘F",
search: "搜索路径、密钥、上游、错误… ⌘F",
failedOnly: "仅显示失败",
groupBySession: "按会话归组",
unpricedOnly: "仅显示无法计价",
expandSession: "展开这次会话",
collapseSession: "收起这次会话",
turnCount: (n: number) => `${n} 轮`,
allClients: "全部客户端",
allClients: "全部密钥",
allUpstreams: "全部上游",
shownOf: (shown: number, total: number) => `${shown} / ${total} 条`,
total: (n: number) => `${n} 条`,
Expand All @@ -94,7 +94,9 @@ export const appText = messages(
// 表头
status: "状态",
time: "时间",
client: "客户端",
/** 请求带的是哪把网关密钥。**不是哪个应用** —— 应用写在后面,是按请求头推测的 */
client: "密钥",
fromPeer: (ip: string) => `来自 ${ip}`,
model: "模型",
upstream: "上游",
latency: "延迟",
Expand All @@ -104,7 +106,7 @@ export const appText = messages(
// 行的右键菜单
openDetails: "打开详情",
onlyUpstream: (name: string) => `仅显示上游 ${name}`,
onlyClient: (name: string) => `仅显示客户端 ${name}`,
onlyClient: (name: string) => `仅显示密钥 ${name}`,
copyId: "复制请求 ID",
copyRow: "复制此行",

Expand Down Expand Up @@ -191,14 +193,14 @@ export const appText = messages(
loadingConfig: "Loading config…",

// 输入框 256px 宽,放得下的文字约 234px;带上「Search」就放不下 ⌘F 了
search: "Path, client, upstream, error… ⌘F",
search: "Path, key, upstream, error… ⌘F",
failedOnly: "Failed only",
groupBySession: "Group by session",
unpricedOnly: "Unpriced only",
expandSession: "Expand this session",
collapseSession: "Collapse this session",
turnCount: (n: number) => (n === 1 ? "1 turn" : `${n} turns`),
allClients: "All clients",
allClients: "All keys",
allUpstreams: "All upstreams",
shownOf: (shown: number, total: number) => `${shown} / ${count(total, "request", "requests")}`,
total: (n: number) => count(n, "request", "requests"),
Expand Down Expand Up @@ -232,7 +234,8 @@ export const appText = messages(

status: "Status",
time: "Time",
client: "Client",
client: "Key",
fromPeer: (ip: string) => `from ${ip}`,
model: "Model",
upstream: "Upstream",
latency: "Latency",
Expand All @@ -241,7 +244,7 @@ export const appText = messages(

openDetails: "Open details",
onlyUpstream: (name: string) => `Show only upstream ${name}`,
onlyClient: (name: string) => `Show only client ${name}`,
onlyClient: (name: string) => `Show only key ${name}`,
copyId: "Copy request ID",
copyRow: "Copy row",

Expand Down
11 changes: 9 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,20 @@ export default function App() {
: gs;
}, [grouped, rows, sessions, sortKey, sortDir]);
/**
* 客户端这一列只在真的分得开的时候才出现。
* 「密钥」这一列只在真的分得开的时候才出现。
*
* 目标用户「一个 key 就够」,那时整列二十五行是同一个值 —— 占着宽度
* 却零信息,而那点宽度给模型名用正好。**按实际出现过的算,不按配置里
* 有几个算**:配了两个而只有一个在发请求时,这一列同样是常量。
*
* 格子里除了密钥还写推测出的应用和非本机的来源,所以**这两样分得开也
* 算**:一把密钥几个应用共用、或者局域网里另一台机器也在用,这一列就
* 有话可说。
*/
const showClient = facet.clients.length > 1;
const showClient =
facet.clients.length > 1 ||
new Set(allRows.map((r) => r.hint ?? "")).size > 1 ||
allRows.some((r) => r.peer);
const searchRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (openSession === null) {
Expand Down
14 changes: 14 additions & 0 deletions src/KeyLabel.i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { messages } from "@/i18n";

export const keyLabelText = messages(
{
/** 打码的密钥后面括号里的名字 */
named: (name: string) => `(${name})`,
/** 老记录没有打码的值,只有名字 */
bare: (name: string) => `密钥 ${name}`,
},
{
named: (name: string) => ` (${name})`,
bare: (name: string) => `Key ${name}`,
},
);
27 changes: 27 additions & 0 deletions src/KeyLabel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { textOf, useText } from "@/i18n";
import { keyLabelText } from "./KeyLabel.i18n";

/**
* 一把网关密钥:打码后的值,括号里是名字 —— `tw-re…wb4e(claude-code)`,
* 和密钥页的写法一样。
*
* **打码的值是 core 在请求那一刻记下的**,不是按名字去查现在的密钥:名字
* 随便起、密钥会更换,按名字查的话,更换之后老记录上会是新密钥的尾巴。
* 老记录没有这个值,退回「密钥 名字」—— 只写名字的话,读起来像是哪个应用。
*/
export function KeyLabel({ name, masked }: { name: string; masked?: string | null }) {
const t = useText(keyLabelText);
if (!masked) return <>{t.bare(name)}</>;
return (
<>
<span className="font-mono">{masked}</span>
{t.named(name)}
</>
);
}

/** 同上,写成一句话。悬停说明里用 */
export function keyText(name: string, masked?: string | null): string {
const t = textOf(keyLabelText);
return masked ? masked + t.named(name) : t.bare(name);
}
13 changes: 11 additions & 2 deletions src/RequestDrawer.i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export const requestDrawerText = messages(
generationTime: "生成用时",
upstream: "上游",
answeredLocally: "本地应答",
client: "客户端",
/** 请求带的是哪把网关密钥 —— 身份 */
client: "密钥",
/** 按请求头推测的应用 —— 旁证 */
app: "应用",
guessed: "(按请求头推测)",
/** 非本机来的请求从哪台机器来 */
peer: "来源",
path: "路径",
conversion: "格式转换",
dropped: "丢弃字段",
Expand Down Expand Up @@ -123,7 +129,10 @@ export const requestDrawerText = messages(
generationTime: "Generation",
upstream: "Upstream",
answeredLocally: "Answered locally",
client: "Client",
client: "Key",
app: "App",
guessed: " (guessed from the request headers)",
peer: "Source",
path: "Path",
conversion: "Conversion",
dropped: "Dropped",
Expand Down
19 changes: 17 additions & 2 deletions src/RequestDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { NativeSelect, NativeSelectOption } from "@/ui/native-select";
import { Collapsible, CollapsibleTrigger } from "@/ui/collapsible";
import { XIcon } from "lucide-react";
import { priceSourceDetail } from "./upstreams/labels";
import { attemptText, formatLabel, quoteText, targetLabel } from "./labels";
import { appLabel, attemptText, formatLabel, quoteText, targetLabel } from "./labels";
import { KeyLabel } from "./KeyLabel";
import { useText } from "@/i18n";
import { commonText } from "@/i18n/common.i18n";
import { requestDrawerText } from "./RequestDrawer.i18n";
Expand Down Expand Up @@ -284,7 +285,21 @@ export default function RequestDrawer({
}
/>
<Row label={t.upstream} value={r.local ? t.answeredLocally : r.provider} />
<Row label={t.client} value={r.client} />
{/* 密钥是身份;应用是按请求头推测的,能伪造;来源是这条连接对面的
地址,只有非本机来的才有 */}
<Row label={t.client} value={<KeyLabel name={r.client} masked={r.key_masked} />} />
{r.client_hint && (
<Row
label={t.app}
value={
<>
{appLabel(r.client_hint)}
<span className="text-muted-foreground">{t.guessed}</span>
</>
}
/>
)}
{r.peer && <Row label={t.peer} value={<span className="font-mono">{r.peer}</span>} />}
<Row label={t.path} value={<span className="font-mono">{r.path}</span>} />
{/* **转了就要看得见,丢了字段更要看得见** —— 「扩展思考开了却没
生效」在客户端那头无从查起 */}
Expand Down
22 changes: 22 additions & 0 deletions src/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,25 @@ export function takesEffectText(t: TakesEffect): string {
export function fieldsOnlyText(): string {
return textOf(labelsText).fieldsOnly;
}

/**
* 按请求头认出来的应用叫什么。**这是旁证,不是身份** —— core 按
* User-Agent 之类猜的,能被伪造,只用来显示;身份是请求带的那把密钥。
*
* 名字是产品名,中英文一样。认不出的原样显示。
*/
const APPS: Record<string, string> = {
"claude-code": "Claude Code",
"claude-desktop": "Claude Desktop",
codex: "Codex CLI",
cursor: "Cursor",
opencode: "opencode",
aider: "Aider",
zed: "Zed",
continue: "Continue",
"gemini-cli": "Gemini CLI",
};

export function appLabel(hint: string): string {
return APPS[hint] ?? hint;
}
6 changes: 4 additions & 2 deletions src/requestTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ export function filterRows(rows: RequestRow[], f: Filter): RequestRow[] {
if (f.client && r.client !== f.client) return false;
if (f.provider && r.provider !== f.provider) return false;
if (!q) return true;
// 路径、客户端、上游、模型、错误信息都算 —— 排查时你记得住的往往是
// 错误里的那半句话,而不是哪个字段装着它。
// 路径、密钥、应用、来源、上游、模型、错误信息都算 —— 排查时记得住的
// 往往是错误里的那半句话,而不是哪个字段装着它。
return (
r.path.toLowerCase().includes(q) ||
r.client.toLowerCase().includes(q) ||
(r.hint ?? "").toLowerCase().includes(q) ||
(r.peer ?? "").includes(q) ||
r.provider.toLowerCase().includes(q) ||
(r.model ?? "").toLowerCase().includes(q) ||
coreText(r.error).toLowerCase().includes(q)
Expand Down
2 changes: 2 additions & 0 deletions src/security/LogTab.i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const logTabText = messages(
hit: "命中",
action: "处置",
request: "请求",
from: (ip: string) => `来自 ${ip}`,
/** 还有更多没读进来时,说的是「前 N 条」 */
count: (n: number, more: boolean) => (more ? `前 ${n} 条` : `${n} 条`),
/** 同一个值在一个请求里出现了几次 */
Expand All @@ -28,6 +29,7 @@ export const logTabText = messages(
hit: "Match",
action: "Action",
request: "Request",
from: (ip: string) => `from ${ip}`,
count: (n: number, more: boolean) =>
more ? `First ${n}` : n === 1 ? "1 entry" : `${n} entries`,
times: (n: number) => `${n} times`,
Expand Down
16 changes: 12 additions & 4 deletions src/security/LogTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Tip } from "@/ui/tip";
import { Segmented } from "@/ui/segmented";
import { windowStart, type Range } from "@/ui/range";
import { when } from "@/format";
import { appLabel } from "@/labels";
import { KeyLabel } from "@/KeyLabel";
import { useText } from "@/i18n";
import { errorText } from "@/i18n/core.i18n";
import RequestDrawer from "@/RequestDrawer";
Expand Down Expand Up @@ -184,13 +186,13 @@ export function LogTab({
) : (
<>
{/* 列宽是定死的:窗口再窄,「命中」一列也要留出能读的宽度,放不下就横向滚 */}
<Table className="table-fixed min-w-[680px]">
<Table className="table-fixed min-w-[710px]">
<colgroup>
<col className="w-[92px]" />
<col className="w-[84px]" />
<col />
<col className="w-[84px]" />
<col className="w-[200px]" />
<col className="w-[230px]" />
<col className="w-9" />
</colgroup>
<TableHeader>
Expand Down Expand Up @@ -232,12 +234,18 @@ export function LogTab({
<ActionBadge action={e.action} />
</TableCell>
<TableCell className="py-2">
{/* 密钥是身份:打码的值加名字。名字是随便起的,不代表是哪个应用 */}
<div className="truncate">
{e.client || "—"}
<KeyLabel name={e.client || "—"} masked={e.key_masked} />
<span className="text-muted-foreground"> → </span>
{e.provider || "—"}
</div>
{e.model && <div className="truncate tw-label text-muted-foreground">{e.model}</div>}
{/* 模型、推测出的应用、非本机的来源 */}
<div className="truncate tw-label text-muted-foreground">
{[e.model, e.client_hint && appLabel(e.client_hint), e.peer && t.from(e.peer)]
.filter(Boolean)
.join(" · ")}
</div>
</TableCell>
<TableCell className="text-right" onClick={(ev) => ev.stopPropagation()}>
<RowMenuButton items={items} label={t.actionsFor(name)} />
Expand Down
16 changes: 12 additions & 4 deletions src/traffic/RequestTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { coreText } from "@/i18n/core.i18n";
import { cn } from "@/lib/utils";
import { latency, money, statusTone, tokens, when } from "@/format";
import { Tip } from "@/ui/tip";
import { translatedText } from "@/labels";
import { appLabel, translatedText } from "@/labels";
import { KeyLabel, keyText } from "@/KeyLabel";
import { ruleName } from "@/security/labels";
import { Button } from "@/ui/button";
import { RowMenu } from "@/ui/row-menu";
Expand Down Expand Up @@ -503,15 +504,22 @@ function Row({
</Tip>
</TableCell>
{/* 和上一行相同就淡化 —— 眼睛要找的是变化的那一行 */}
{/* 密钥是身份;后面是推测出的应用和非本机的来源,淡一档 */}
{showClient && (
<TableCell
className={
same((x) => x.client)
"max-w-56 truncate " +
(same((x) => `${x.client}|${x.keyMasked ?? ""}|${x.hint ?? ""}|${x.peer ?? ""}`)
? "text-neutral-400/50"
: ""
: "")
}
title={[keyText(r.client, r.keyMasked), r.hint && appLabel(r.hint), r.peer && t.fromPeer(r.peer)]
.filter(Boolean)
.join(" · ")}
>
{r.client}
<KeyLabel name={r.client} masked={r.keyMasked} />
{r.hint && <span className="text-muted-foreground"> · {appLabel(r.hint)}</span>}
{r.peer && <span className="text-muted-foreground"> · {t.fromPeer(r.peer)}</span>}
</TableCell>
)}
{/*
Expand Down
Loading
Loading