跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

transcript.jsx

304 行 · 8.2 KB历史下载
1import React, { createContext, memo, useContext, useMemo } from "react";
2import { Marked } from "marked";
3import { Bell, Check, ChevronRight, CircleAlert, Terminal } from "lucide-react";
4import { ProjectMedia, projectMediaKind, webURL } from "./transcript-media";
5import "./transcript.css";
6
7export const TranscriptContext = createContext(null);
8const statuses = {
9 completed: "已完成",
10 failed: "失败",
11 killed: "已终止",
12 stopped: "已停止",
13 running: "运行中",
14 pending: "等待中",
15 called: "已调用",
16};
17const wrapperLabels = {
18 "task-notification": "后台任务",
19 "system-reminder": "系统提示",
20 "local-command-stdout": "命令输出",
21 "local-command-stderr": "命令错误",
22 "command-name": "命令",
23 "command-message": "命令消息",
24 "command-args": "命令参数",
25};
26const wrapper =
27 /^ {0,3}<(task-notification|system-reminder|local-command-std(?:out|err)|command-(?:name|message|args))>([\s\S]*?)<\/\1>/;
28const markdown = new Marked({
29 gfm: true,
30 breaks: true,
31 extensions: [
32 {
33 name: "claudeRecord",
34 level: "block",
35 start(src) {
36 return src.search(
37 /^ {0,3}<(?:task-notification|system-reminder|local-command-std(?:out|err)|command-(?:name|message|args))>/m,
38 );
39 },
40 tokenizer(src) {
41 const m = wrapper.exec(src);
42 if (m)
43 return {
44 type: "claudeRecord",
45 raw: m[0],
46 tag: m[1],
47 text: m[2].trim(),
48 };
49 },
50 },
51 ],
52});
53
54function Record({ token }) {
55 if (token.tag !== "task-notification")
56 return (
57 <details className="claude-record">
58 <summary>
59 <Terminal size={16} />
60 {wrapperLabels[token.tag]}
61 <ChevronRight size={15} />
62 </summary>
63 <pre>{token.text}</pre>
64 </details>
65 );
66 const field = (name) =>
67 token.text
68 .match(new RegExp("<" + name + ">([\\s\\S]*?)</" + name + ">"))?.[1]
69 ?.trim();
70 const status = field("status");
71 const Icon =
72 status === "completed" ? Check : status === "failed" ? CircleAlert : Bell;
73 const known = new Set(["summary", "status"]);
74 const fields = [...token.text.matchAll(/<([\w-]+)>([\s\S]*?)<\/\1>/g)].filter(
75 (m) => !known.has(m[1]),
76 );
77 const names = {
78 "task-id": "任务编号",
79 "tool-use-id": "工具调用",
80 "output-file": "输出文件",
81 result: "结果",
82 usage: "用量",
83 };
84 return (
85 <details className="claude-record task-record">
86 <summary>
87 <Icon className={status === "failed" ? "bad" : "task-icon"} size={17} />
88 <span>
89 <small>
90 后台任务
91 {status &&
92 " · " +
93 (Object.hasOwn(statuses, status) ? statuses[status] : status)}
94 </small>
95 <strong>{field("summary") || "任务通知"}</strong>
96 </span>
97 <ChevronRight size={15} />
98 </summary>
99 {fields.length ? (
100 <dl>
101 {fields.map(([, name, value], i) => (
102 <React.Fragment key={i}>
103 <dt>{Object.hasOwn(names, name) ? names[name] : name}</dt>
104 <dd>{value.trim()}</dd>
105 </React.Fragment>
106 ))}
107 </dl>
108 ) : (
109 <pre>{token.text}</pre>
110 )}
111 </details>
112 );
113}
114
115function Reference({ token }) {
116 const { sessionID, openWeb } = useContext(TranscriptContext);
117 const href = token.href;
118 const url = webURL(href);
119 if (url)
120 return (
121 <a
122 href={url}
123 title={"用远程浏览器打开 " + new URL(url).hostname}
124 onClick={(e) => {
125 e.preventDefault();
126 openWeb(url);
127 }}
128 >
129 {token.type === "image" ? (
130 token.text || "查看图片"
131 ) : (
132 <Tokens list={token.tokens} />
133 )}
134 </a>
135 );
136 if (projectMediaKind(href))
137 return (
138 <ProjectMedia
139 key={href}
140 reference={href}
141 label={token.text}
142 sessionID={sessionID}
143 />
144 );
145 return <span title={href}>{token.text || href}</span>;
146}
147
148function CodeSpan({ token }) {
149 const { sessionID } = useContext(TranscriptContext);
150 if (
151 projectMediaKind(token.text) &&
152 (/^(?:[./]|[A-Za-z]:[\\/])/.test(token.text) || !/\s/.test(token.text))
153 )
154 return (
155 <ProjectMedia
156 key={token.text}
157 reference={token.text}
158 sessionID={sessionID}
159 />
160 );
161 return <code>{token.text}</code>;
162}
163
164// Create React elements only. Transcript HTML (including partial tags during
165// streaming) remains selectable text, with no access to the phone bridge.
166function Token({ token: t }) {
167 const children = t.tokens && <Tokens list={t.tokens} />;
168 switch (t.type) {
169 case "space":
170 case "def":
171 return null;
172 case "claudeRecord":
173 return <Record token={t} />;
174 case "heading": {
175 const H = "h" + Math.max(2, t.depth);
176 return <H>{children}</H>;
177 }
178 case "paragraph":
179 return <p>{children}</p>;
180 case "br":
181 return <br />;
182 case "hr":
183 return <hr />;
184 case "strong":
185 return <strong>{children}</strong>;
186 case "em":
187 return <em>{children}</em>;
188 case "del":
189 return <del>{children}</del>;
190 case "blockquote":
191 return <blockquote>{children}</blockquote>;
192 case "list": {
193 const List = t.ordered ? "ol" : "ul";
194 return (
195 <List start={t.ordered ? t.start : undefined}>
196 {t.items.map((item, i) => (
197 <li key={i} className={item.task ? "check-item" : undefined}>
198 <Tokens list={item.tokens} />
199 </li>
200 ))}
201 </List>
202 );
203 }
204 case "checkbox":
205 return (
206 <input
207 type="checkbox"
208 checked={t.checked}
209 disabled
210 aria-label={t.checked ? "已完成" : "未完成"}
211 />
212 );
213 case "codespan":
214 return <CodeSpan token={t} />;
215 case "code":
216 return (
217 <div className="code-block">
218 {t.lang && <small>{t.lang.split(/\s+/)[0]}</small>}
219 <pre tabIndex={0}>
220 <code>{t.text}</code>
221 </pre>
222 </div>
223 );
224 case "image":
225 case "link":
226 return <Reference token={t} />;
227 case "table":
228 return (
229 <div className="md-table" tabIndex={0}>
230 <table>
231 <thead>
232 <tr>
233 {t.header.map((c, i) => (
234 <th key={i} style={{ textAlign: c.align }}>
235 <Tokens list={c.tokens} />
236 </th>
237 ))}
238 </tr>
239 </thead>
240 <tbody>
241 {t.rows.map((row, r) => (
242 <tr key={r}>
243 {row.map((c, i) => (
244 <td key={i} style={{ textAlign: c.align }}>
245 <Tokens list={c.tokens} />
246 </td>
247 ))}
248 </tr>
249 ))}
250 </tbody>
251 </table>
252 </div>
253 );
254 case "html":
255 return <span className="literal-markup">{t.raw}</span>;
256 default:
257 return children || t.text || t.raw;
258 }
259}
260function Tokens({ list }) {
261 return list?.map((t, i) => <Token key={i} token={t} />);
262}
263export const MessageBody = memo(function MessageBody({ text }) {
264 const tokens = useMemo(() => {
265 try {
266 return markdown.lexer(text);
267 } catch {
268 return [{ type: "html", raw: text }];
269 }
270 }, [text]);
271 return (
272 <div className="message-body markdown">
273 <Tokens list={tokens} />
274 </div>
275 );
276});
277
278export function ToolActivity({ tools }) {
279 if (!tools?.length) return null;
280 const failures = tools.filter((t) => t.status === "failed").length;
281 return (
282 <details className="claude-record tool-history">
283 <summary>
284 <Terminal size={16} />
285 <span>
286 {tools.length} 次工具调用
287 {failures > 0 && <small className="bad"> · {failures} 次失败</small>}
288 </span>
289 <ChevronRight size={15} />
290 </summary>
291 <ul>
292 {tools.map((tool) => (
293 <li key={tool.id}>
294 <strong>{tool.name}</strong>
295 <small className={tool.status === "failed" ? "bad" : ""}>
296 {statuses[tool.status] || "已调用"}
297 </small>
298 {tool.detail && <p>{tool.detail}</p>}
299 </li>
300 ))}
301 </ul>
302 </details>
303 );
304}