agent-loft 0.2.43 → 0.2.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/package.json +1 -1
- package/src/channels/web/api.ts +146 -3
- package/src/channels/web/http.ts +12 -0
- package/src/channels/web/push.ts +130 -6
- package/src/channels/web/server.ts +4 -3
- package/src/core/config.ts +36 -0
- package/src/core/hub.ts +14 -0
- package/src/core/reply-files.ts +142 -0
- package/src/core/servers.ts +134 -0
- package/src/core/uploads.ts +1 -1
- package/src/core/visualize.ts +123 -0
- package/web/css/messages.css +271 -0
- package/web/css/servers.css +265 -0
- package/web/index.html +1 -0
- package/web/lib/api.js +1 -0
- package/web/lib/markdown.js +5 -3
- package/web/lib/overlay.js +1 -1
- package/web/lib/push.js +8 -1
- package/web/lib/shell.js +691 -0
- package/web/lib/ws.js +7 -1
- package/web/settings/common.js +3 -0
- package/web/settings/index.js +2 -0
- package/web/settings/notify.js +7 -2
- package/web/settings/security.js +8 -3
- package/web/settings/servers.js +207 -0
- package/web/state.js +2 -0
- package/web/views/auth.js +3 -1
- package/web/views/composer.js +1 -1
- package/web/views/file-preview.js +342 -0
- package/web/views/layout.js +43 -5
- package/web/views/messages.js +5 -4
- package/web/views/remote.js +2 -2
- package/web/views/sidebar.js +6 -4
- package/web/views/tasks.js +1 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// agent 回复里提到的文件:网页上点开预览 / 下载。
|
|
2
|
+
//
|
|
3
|
+
// 只放行这条会话的 agent 回复里真的写过的路径(防止拼个地址就读服务器上任意文件),
|
|
4
|
+
// 再挡掉密钥、凭据一类的敏感文件 —— agent 写过也不给看。
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { home } from "../home.js";
|
|
9
|
+
import { uploadsRoot } from "./uploads.js";
|
|
10
|
+
|
|
11
|
+
/** 路径里出现这些目录名就不给看(私钥、云凭据、密码库…) */
|
|
12
|
+
const SENSITIVE_DIRS = new Set([".ssh", ".gnupg", ".aws", ".azure", ".kube", ".docker", ".password-store", ".gcloud"]);
|
|
13
|
+
|
|
14
|
+
/** 家目录下这些位置不给看(相对家目录) */
|
|
15
|
+
const SENSITIVE_HOME = [
|
|
16
|
+
".config/gcloud",
|
|
17
|
+
".config/gh",
|
|
18
|
+
".claude/.credentials.json",
|
|
19
|
+
".codex/auth.json",
|
|
20
|
+
".netrc",
|
|
21
|
+
".git-credentials",
|
|
22
|
+
".npmrc",
|
|
23
|
+
".pypirc",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** 系统里不给看的 */
|
|
27
|
+
const SENSITIVE_SYSTEM = ["/proc", "/sys", "/dev", "/etc/shadow", "/etc/gshadow", "/etc/sudoers", "/etc/sudoers.d"];
|
|
28
|
+
|
|
29
|
+
/** 文件名:.env(示例文件除外)、私钥、证书私钥、密钥库 */
|
|
30
|
+
function sensitiveName(name: string): boolean {
|
|
31
|
+
const n = name.toLowerCase();
|
|
32
|
+
if (/^\.env(\.|$)/.test(n) && !/\.(example|sample|template)$/.test(n)) return true;
|
|
33
|
+
if (/^id_(rsa|dsa|ecdsa|ed25519)/.test(n)) return true;
|
|
34
|
+
return /\.(pem|key|p12|pfx|jks|keystore|kdbx)$/.test(n);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function within(file: string, dir: string): boolean {
|
|
38
|
+
return file === dir || file.startsWith(dir.endsWith(path.sep) ? dir : dir + path.sep);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isSensitivePath(file: string): boolean {
|
|
42
|
+
const parts = file.split(path.sep);
|
|
43
|
+
if (parts.some((p) => SENSITIVE_DIRS.has(p))) return true;
|
|
44
|
+
if (sensitiveName(path.basename(file))) return true;
|
|
45
|
+
const userHome = os.homedir();
|
|
46
|
+
if (SENSITIVE_HOME.some((p) => within(file, path.join(userHome, p)))) return true;
|
|
47
|
+
if (SENSITIVE_SYSTEM.some((p) => within(file, p))) return true;
|
|
48
|
+
// agent-loft 自己的配置和数据(飞书密钥、账号、登录态…);上传的附件除外,那些本来就能看
|
|
49
|
+
return within(file, home) && !within(file, uploadsRoot);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 「file:///a/b」「~/a」「相对路径」→ 绝对路径(相对路径按会话的工作目录算) */
|
|
53
|
+
export function expandPath(p: string, cwd: string): string {
|
|
54
|
+
let s = p.trim();
|
|
55
|
+
if (s.startsWith("file://")) {
|
|
56
|
+
s = s.slice("file://".length);
|
|
57
|
+
try {
|
|
58
|
+
s = decodeURIComponent(s);
|
|
59
|
+
} catch {
|
|
60
|
+
// 编码坏了就按原样
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (s === "~" || s.startsWith("~/")) s = path.join(os.homedir(), s.slice(1));
|
|
64
|
+
return path.resolve(cwd, s);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 解析成可以给前端看的真实文件路径:存在、是普通文件、解析符号链接前后都不敏感。不行返回 undefined
|
|
69
|
+
*/
|
|
70
|
+
export function resolveReplyFile(p: string, cwd: string): string | undefined {
|
|
71
|
+
if (!p.trim() || p.includes("\0")) return undefined;
|
|
72
|
+
try {
|
|
73
|
+
const abs = expandPath(p, cwd);
|
|
74
|
+
if (isSensitivePath(abs)) return undefined;
|
|
75
|
+
const real = fs.realpathSync(abs);
|
|
76
|
+
if (isSensitivePath(real)) return undefined;
|
|
77
|
+
return fs.statSync(real).isFile() ? real : undefined;
|
|
78
|
+
} catch {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 回复里百分号编码的路径(链接地址常见)也解开一份,两种写法都能对上 */
|
|
84
|
+
function withDecoded(text: string): string {
|
|
85
|
+
if (!text.includes("%")) return text;
|
|
86
|
+
const decoded = text.replace(/(?:%[0-9a-f]{2})+/gi, (m) => {
|
|
87
|
+
try {
|
|
88
|
+
return decodeURIComponent(m);
|
|
89
|
+
} catch {
|
|
90
|
+
return m;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return decoded === text ? text : `${text}\n${decoded}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 路径里能出现的字符:前后挨着这些就说明只对上了别的路径的一截(比如 a.ts 对上 src/a.ts) */
|
|
97
|
+
const PATH_CHAR = /[\p{L}\p{N}_\-./~@+]/u;
|
|
98
|
+
|
|
99
|
+
/** text 里有没有完整地写着 p(不是别的路径的一部分) */
|
|
100
|
+
export function mentions(text: string, p: string): boolean {
|
|
101
|
+
const needle = p.trim();
|
|
102
|
+
if (!needle) return false;
|
|
103
|
+
const hay = withDecoded(text);
|
|
104
|
+
for (let i = hay.indexOf(needle); i >= 0; i = hay.indexOf(needle, i + 1)) {
|
|
105
|
+
const before = hay[i - 1];
|
|
106
|
+
const after = hay[i + needle.length];
|
|
107
|
+
// 末尾的「.」是句号,不算路径的一部分
|
|
108
|
+
const afterOk = !after || !PATH_CHAR.test(after) || (after === "." && !PATH_CHAR.test(hay[i + needle.length + 1] ?? " "));
|
|
109
|
+
if ((!before || !PATH_CHAR.test(before)) && afterOk) return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const TYPES: Record<string, string> = {
|
|
115
|
+
png: "image/png",
|
|
116
|
+
jpg: "image/jpeg",
|
|
117
|
+
jpeg: "image/jpeg",
|
|
118
|
+
gif: "image/gif",
|
|
119
|
+
webp: "image/webp",
|
|
120
|
+
avif: "image/avif",
|
|
121
|
+
bmp: "image/bmp",
|
|
122
|
+
ico: "image/x-icon",
|
|
123
|
+
svg: "image/svg+xml",
|
|
124
|
+
pdf: "application/pdf",
|
|
125
|
+
mp4: "video/mp4",
|
|
126
|
+
m4v: "video/mp4",
|
|
127
|
+
webm: "video/webm",
|
|
128
|
+
mov: "video/quicktime",
|
|
129
|
+
mp3: "audio/mpeg",
|
|
130
|
+
m4a: "audio/mp4",
|
|
131
|
+
aac: "audio/aac",
|
|
132
|
+
wav: "audio/wav",
|
|
133
|
+
ogg: "audio/ogg",
|
|
134
|
+
oga: "audio/ogg",
|
|
135
|
+
opus: "audio/ogg",
|
|
136
|
+
flac: "audio/flac",
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** 浏览器里直接打开的类型;其余(包括 HTML)一律当下载 / 纯文本,不在本站域名下当网页执行 */
|
|
140
|
+
export function replyFileType(file: string): string | undefined {
|
|
141
|
+
return TYPES[path.extname(file).slice(1).toLowerCase()];
|
|
142
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// 多服务器:网页左上角切换到别的 agent-loft。
|
|
2
|
+
//
|
|
3
|
+
// 别的服务器的页面原样嵌在这台页面的 iframe 里,各自登录、各自直连自己的服务端,这台不中转。
|
|
4
|
+
// 几台要在同一个主域名下(a.example.com、b.example.com):同站的 iframe 才带得上登录 cookie,
|
|
5
|
+
// iPhone 的 Safari 跨站一律不带。
|
|
6
|
+
//
|
|
7
|
+
// 两头各有一份配置(config.json 的 servers):
|
|
8
|
+
// - list:这台左上角能切到哪几台(这台是「外壳」);
|
|
9
|
+
// - embedders:允许哪些地址把这台的页面嵌进去(这台被别人嵌)。页面的 frame-ancestors 就按它出,
|
|
10
|
+
// 不在里面的一律嵌不进来。
|
|
11
|
+
import crypto from "node:crypto";
|
|
12
|
+
import os from "node:os";
|
|
13
|
+
import type { AppConfig } from "./config.js";
|
|
14
|
+
|
|
15
|
+
export type ServerEntry = AppConfig["servers"]["list"][number];
|
|
16
|
+
|
|
17
|
+
/** 切换列表里最多几台:每台都是一整个常驻的页面 */
|
|
18
|
+
export const MAX_SERVERS = 12;
|
|
19
|
+
const MAX_EMBEDDERS = 20;
|
|
20
|
+
const MAX_NAME = 40;
|
|
21
|
+
/** 图标是 emoji:一个字形可能由好几个码位拼成(肤色、ZWJ 组合),按码位数放宽一点 */
|
|
22
|
+
const MAX_ICON = 16;
|
|
23
|
+
|
|
24
|
+
/** 外壳自己在列表里的 id(别的服务器的 id 是随机生成的,不会撞上) */
|
|
25
|
+
export const SELF_ID = "home";
|
|
26
|
+
|
|
27
|
+
/** 页面切换和通知链接共用的 id;home 只代表外壳自己。 */
|
|
28
|
+
export function validServerId(id: unknown): id is string {
|
|
29
|
+
return typeof id === "string" && id !== SELF_ID && /^[\w-]{1,40}$/.test(id);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 地址规范成 origin(https://a.example.com、http://localhost:8800)。
|
|
34
|
+
* 只认 http / https,不带路径、查询、账号密码:agent-loft 只能挂在根路径上。
|
|
35
|
+
*/
|
|
36
|
+
export function normalizeOrigin(raw: unknown): string {
|
|
37
|
+
const text = String(raw ?? "").trim();
|
|
38
|
+
if (!text) throw new Error("地址不能为空");
|
|
39
|
+
let url: URL;
|
|
40
|
+
try {
|
|
41
|
+
url = new URL(/^[a-z][a-z\d+.-]*:\/\//i.test(text) ? text : `https://${text}`);
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error(`地址不对:${text}`);
|
|
44
|
+
}
|
|
45
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") throw new Error(`只支持 http / https 地址:${text}`);
|
|
46
|
+
if (url.username || url.password) throw new Error(`地址里不能带账号密码:${text}`);
|
|
47
|
+
if ((url.pathname !== "/" && url.pathname !== "") || url.search || url.hash) {
|
|
48
|
+
throw new Error(`只填到域名(和端口)就行,agent-loft 不支持挂在子路径下:${text}`);
|
|
49
|
+
}
|
|
50
|
+
return url.origin;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const clip = (v: unknown, max: number) => [...String(v ?? "").trim()].slice(0, max).join("");
|
|
54
|
+
|
|
55
|
+
function newId(taken: Set<string>): string {
|
|
56
|
+
for (;;) {
|
|
57
|
+
const id = crypto.randomBytes(4).toString("hex");
|
|
58
|
+
if (!taken.has(id) && id !== SELF_ID) return id;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 设置页传来的 servers(只改了其中几项也行),校验后和现有的合并。
|
|
64
|
+
* selfOrigin:这台页面自己的地址,不能把自己加进列表。出错抛 Error(说明给用户看)
|
|
65
|
+
*/
|
|
66
|
+
export function cleanServers(input: unknown, current: AppConfig["servers"], selfOrigin?: string): AppConfig["servers"] {
|
|
67
|
+
const patch = (input ?? {}) as Partial<Record<keyof AppConfig["servers"], unknown>>;
|
|
68
|
+
const next = structuredClone(current);
|
|
69
|
+
if (patch.selfName !== undefined) next.selfName = clip(patch.selfName, MAX_NAME);
|
|
70
|
+
if (patch.selfIcon !== undefined) next.selfIcon = clip(patch.selfIcon, MAX_ICON);
|
|
71
|
+
|
|
72
|
+
if (patch.list !== undefined) {
|
|
73
|
+
if (!Array.isArray(patch.list)) throw new Error("服务器列表格式不对");
|
|
74
|
+
if (patch.list.length > MAX_SERVERS) throw new Error(`最多加 ${MAX_SERVERS} 台服务器`);
|
|
75
|
+
const known = new Set(current.list.map((s) => s.id));
|
|
76
|
+
const taken = new Set<string>();
|
|
77
|
+
const origins = new Set<string>();
|
|
78
|
+
const list: ServerEntry[] = [];
|
|
79
|
+
for (const raw of patch.list as Partial<ServerEntry>[]) {
|
|
80
|
+
const url = normalizeOrigin(raw?.url);
|
|
81
|
+
if (selfOrigin && url === selfOrigin) throw new Error(`${url} 就是这台自己,不用加`);
|
|
82
|
+
if (origins.has(url)) throw new Error(`${url} 加了两次`);
|
|
83
|
+
origins.add(url);
|
|
84
|
+
// 老的 id 留着(页面记着上次切到哪台、推送里带的也是它);新加的现生成
|
|
85
|
+
const id = typeof raw?.id === "string" && known.has(raw.id) && !taken.has(raw.id) ? raw.id : newId(new Set([...known, ...taken]));
|
|
86
|
+
taken.add(id);
|
|
87
|
+
const name = clip(raw?.name, MAX_NAME) || new URL(url).hostname.split(".")[0] || url;
|
|
88
|
+
list.push({ id, name, icon: clip(raw?.icon, MAX_ICON), url });
|
|
89
|
+
}
|
|
90
|
+
next.list = list;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (patch.embedders !== undefined) {
|
|
94
|
+
if (!Array.isArray(patch.embedders)) throw new Error("允许嵌入的地址格式不对");
|
|
95
|
+
const set = new Set<string>();
|
|
96
|
+
for (const raw of patch.embedders) set.add(normalizeOrigin(raw));
|
|
97
|
+
if (set.size > MAX_EMBEDDERS) throw new Error(`允许嵌入的地址最多 ${MAX_EMBEDDERS} 个`);
|
|
98
|
+
next.embedders = [...set];
|
|
99
|
+
}
|
|
100
|
+
return next;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 切换列表里这台的名字:没设就用主机名 */
|
|
104
|
+
export function selfName(cfg: AppConfig["servers"]): string {
|
|
105
|
+
return cfg.selfName || os.hostname().split(".")[0] || "这台";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 页面要的:左上角的切换列表。没加别的服务器就是 null(不显示切换) */
|
|
109
|
+
export function shellInfo(cfg: AppConfig["servers"]) {
|
|
110
|
+
if (cfg.list.length === 0) return null;
|
|
111
|
+
return {
|
|
112
|
+
self: { id: SELF_ID, name: selfName(cfg), icon: cfg.selfIcon },
|
|
113
|
+
list: cfg.list.map(({ id, name, icon, url }) => ({ id, name, icon, url })),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 页面(HTML 等静态文件)的 CSP:
|
|
119
|
+
* - frame-src:外壳要能嵌列表里的服务器;
|
|
120
|
+
* - frame-ancestors:只让 embedders 里的地址嵌这台,没有就谁都不行。
|
|
121
|
+
*/
|
|
122
|
+
export function pageCsp(cfg: AppConfig["servers"]): string {
|
|
123
|
+
const frames = ["'self'", ...cfg.list.map((s) => s.url)].join(" ");
|
|
124
|
+
const ancestors = cfg.embedders.length ? cfg.embedders.join(" ") : "'none'";
|
|
125
|
+
return `default-src 'self'; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'; script-src 'self'; connect-src 'self' ws: wss:; frame-src ${frames}; frame-ancestors ${ancestors}; base-uri 'self'; form-action 'self'`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 这台自己嵌的东西(PDF 预览、回复里的图表)的 frame-ancestors。frame-ancestors 要求每一层祖先
|
|
130
|
+
* 都对得上:这台被外壳嵌着时,只写 'self' 的话外壳那层通不过,预览就是一片空白
|
|
131
|
+
*/
|
|
132
|
+
export function ownFrameAncestors(cfg: AppConfig["servers"]): string {
|
|
133
|
+
return ["'self'", ...cfg.embedders].join(" ");
|
|
134
|
+
}
|
package/src/core/uploads.ts
CHANGED
|
@@ -7,7 +7,7 @@ import path from "node:path";
|
|
|
7
7
|
import { dataDir } from "../home.js";
|
|
8
8
|
import type { AttachmentInfo, AttachmentKind, SavedAttachment } from "./types.js";
|
|
9
9
|
|
|
10
|
-
const uploadsRoot = path.join(dataDir, "uploads");
|
|
10
|
+
export const uploadsRoot = path.join(dataDir, "uploads");
|
|
11
11
|
|
|
12
12
|
export function uploadsDirFor(key: string): string {
|
|
13
13
|
return path.join(uploadsRoot, encodeURIComponent(key));
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// 回复里的 HTML:Codex 的 visualize 插件画的图表(回复里单独一行 visualize{"path":"…"}),
|
|
2
|
+
// 以及 agent 写出来的普通 HTML 文件。网页上放进沙箱 iframe 里显示。
|
|
3
|
+
//
|
|
4
|
+
// visualize 写的是 HTML 片段,要套上插件自带的样式和脚本(和插件的 scripts/render.py 一样)才是完整的页面。
|
|
5
|
+
// 插件装在本机 Codex 的目录里,找不到就用一份最小的样式兜底。
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
|
|
10
|
+
/** 和 visualize 插件约定的一样:图表只能从这几个 CDN 加载资源,不能联网请求别的 */
|
|
11
|
+
const RESOURCES = [
|
|
12
|
+
"blob:",
|
|
13
|
+
"data:",
|
|
14
|
+
"https://cdnjs.cloudflare.com",
|
|
15
|
+
"https://cdn.jsdelivr.net",
|
|
16
|
+
"https://esm.sh",
|
|
17
|
+
"https://fonts.bunny.net",
|
|
18
|
+
"https://fonts.googleapis.com",
|
|
19
|
+
"https://fonts.gstatic.com",
|
|
20
|
+
"https://unpkg.com",
|
|
21
|
+
].join(" ");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 沙箱(不带 allow-same-origin):页面是个匿名的源,读不到本站的登录态、调不了本站接口;
|
|
25
|
+
* connect-src 只给 blob / data,也就没法把东西发出去
|
|
26
|
+
*/
|
|
27
|
+
export const HTML_CSP = [
|
|
28
|
+
"sandbox allow-scripts",
|
|
29
|
+
"default-src 'none'",
|
|
30
|
+
`script-src 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval' ${RESOURCES}`,
|
|
31
|
+
`style-src 'unsafe-inline' ${RESOURCES}`,
|
|
32
|
+
`img-src ${RESOURCES}`,
|
|
33
|
+
`font-src ${RESOURCES}`,
|
|
34
|
+
`media-src ${RESOURCES}`,
|
|
35
|
+
"worker-src blob:",
|
|
36
|
+
"connect-src blob: data:",
|
|
37
|
+
"frame-src 'none'",
|
|
38
|
+
"object-src 'none'",
|
|
39
|
+
"base-uri 'none'",
|
|
40
|
+
"form-action 'none'",
|
|
41
|
+
"frame-ancestors 'self'",
|
|
42
|
+
].join("; ");
|
|
43
|
+
|
|
44
|
+
/** 太大的不显示(插件自己限制片段 1 MB) */
|
|
45
|
+
export const HTML_MAX = 2 * 1024 * 1024;
|
|
46
|
+
|
|
47
|
+
const FRAGMENT_PLACEHOLDER = "<!--__INLINE_VISUALIZATION_FRAGMENT__-->";
|
|
48
|
+
const DOCUMENT_MARKUP = /<!doctype\s|<\s*(?:html|head|body)(?:\s|>)/i;
|
|
49
|
+
|
|
50
|
+
/** 插件目录:$CODEX_HOME/plugins/cache/<来源>/visualize/<版本>/skills/visualize/assets,有多个版本取最新的 */
|
|
51
|
+
function findAssets(): string | undefined {
|
|
52
|
+
const cache = path.join(process.env.CODEX_HOME ?? path.join(os.homedir(), ".codex"), "plugins", "cache");
|
|
53
|
+
const found: { dir: string; version: string }[] = [];
|
|
54
|
+
try {
|
|
55
|
+
for (const source of fs.readdirSync(cache)) {
|
|
56
|
+
const base = path.join(cache, source, "visualize");
|
|
57
|
+
let versions: string[] = [];
|
|
58
|
+
try {
|
|
59
|
+
versions = fs.readdirSync(base);
|
|
60
|
+
} catch {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
for (const version of versions) {
|
|
64
|
+
const dir = path.join(base, version, "skills", "visualize", "assets");
|
|
65
|
+
if (fs.existsSync(path.join(dir, "visualize.css"))) found.push({ dir, version });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
found.sort((a, b) => a.version.localeCompare(b.version, undefined, { numeric: true }));
|
|
72
|
+
return found.at(-1)?.dir;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 没装插件时的兜底:插件约定的几个常用变量 */
|
|
76
|
+
const FALLBACK_CSS = `:root{color-scheme:light dark;--background:transparent;--foreground:light-dark(rgb(26 28 31),rgb(255 255 255));--muted-foreground:light-dark(rgb(26 28 31/50%),rgb(255 255 255/50%));--border:light-dark(rgb(0 0 0/10%),rgb(255 255 255/12%));--card:color-mix(in oklab,var(--foreground) 5%,transparent);--primary:light-dark(rgb(51 156 255),rgb(131 195 255));--viz-series-1:#339cff;--viz-series-2:#f59e0b;--viz-series-3:#10b981;--viz-series-4:#ef4444;--viz-series-5:#8b5cf6}
|
|
77
|
+
body{margin:0;font:14px/1.5 system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;color:var(--foreground)}
|
|
78
|
+
.text-small{font-size:12px}.text-muted{color:var(--muted-foreground)}.tabular-nums{font-variant-numeric:tabular-nums}
|
|
79
|
+
.viz-row{display:flex;justify-content:space-between;gap:8px;align-items:baseline}`;
|
|
80
|
+
|
|
81
|
+
/** 把高度告诉外面的页面(iframe 跟着内容变高),见 web/views/file-preview.js */
|
|
82
|
+
const SIZE_REPORTER = `<script>(()=>{const post=()=>parent!==window&&parent.postMessage({type:"aloft-html-size",height:Math.ceil(document.documentElement.scrollHeight)},"*");new ResizeObserver(post).observe(document.documentElement);addEventListener("load",post);post()})()</script>`;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 给网页显示用的完整 HTML。片段套上插件的外壳;本来就是完整页面的原样,只在末尾加上报高度的脚本。
|
|
86
|
+
* 背景透明,和对话的底色融在一起
|
|
87
|
+
*/
|
|
88
|
+
export function htmlDocument(source: string, title: string): string {
|
|
89
|
+
if (DOCUMENT_MARKUP.test(source)) {
|
|
90
|
+
const at = source.search(/<\/body\s*>/i);
|
|
91
|
+
return at >= 0 ? source.slice(0, at) + SIZE_REPORTER + source.slice(at) : source + SIZE_REPORTER;
|
|
92
|
+
}
|
|
93
|
+
const assets = findAssets();
|
|
94
|
+
let css = FALLBACK_CSS;
|
|
95
|
+
let body = source;
|
|
96
|
+
if (assets) {
|
|
97
|
+
try {
|
|
98
|
+
css = fs.readFileSync(path.join(assets, "visualize.css"), "utf8");
|
|
99
|
+
const kit = fs.readFileSync(path.join(assets, "visualize.html"), "utf8");
|
|
100
|
+
body = kit.includes(FRAGMENT_PLACEHOLDER) ? kit.replace(FRAGMENT_PLACEHOLDER, () => source) : source + kit;
|
|
101
|
+
} catch {
|
|
102
|
+
css = FALLBACK_CSS;
|
|
103
|
+
body = source;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const esc = (s: string) => s.replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]!);
|
|
107
|
+
return `<!doctype html>
|
|
108
|
+
<html lang="zh-CN">
|
|
109
|
+
<head>
|
|
110
|
+
<meta charset="utf-8">
|
|
111
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
112
|
+
<meta name="referrer" content="no-referrer">
|
|
113
|
+
<title>${esc(title)}</title>
|
|
114
|
+
<style>${css}
|
|
115
|
+
:root{--color-background-primary:transparent;background:transparent!important}html>body{padding:0;margin:0}</style>
|
|
116
|
+
</head>
|
|
117
|
+
<body>
|
|
118
|
+
${body}
|
|
119
|
+
${SIZE_REPORTER}
|
|
120
|
+
</body>
|
|
121
|
+
</html>
|
|
122
|
+
`;
|
|
123
|
+
}
|
package/web/css/messages.css
CHANGED
|
@@ -901,3 +901,274 @@ a.att-file:hover {
|
|
|
901
901
|
color: var(--fg);
|
|
902
902
|
background: var(--bg-hover);
|
|
903
903
|
}
|
|
904
|
+
|
|
905
|
+
/* ---------- 回复里提到的文件:能点开预览(views/file-preview.js) ---------- */
|
|
906
|
+
.md a.file-link {
|
|
907
|
+
color: var(--link);
|
|
908
|
+
text-decoration: none;
|
|
909
|
+
border-bottom: 1px dashed currentColor;
|
|
910
|
+
cursor: pointer;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
.md a.file-link:hover {
|
|
914
|
+
border-bottom-style: solid;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
.md a.file-link code {
|
|
918
|
+
color: inherit;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
.md a.file-link.file-img {
|
|
922
|
+
border: 0;
|
|
923
|
+
display: inline-block;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
.md a.file-link.file-img img {
|
|
927
|
+
max-width: min(100%, 420px);
|
|
928
|
+
max-height: 320px;
|
|
929
|
+
border-radius: 8px;
|
|
930
|
+
border: 1px solid var(--border);
|
|
931
|
+
display: block;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
.file-viewer {
|
|
935
|
+
position: fixed;
|
|
936
|
+
inset: 0;
|
|
937
|
+
z-index: 60;
|
|
938
|
+
background: rgba(0, 0, 0, 0.55);
|
|
939
|
+
display: flex;
|
|
940
|
+
align-items: center;
|
|
941
|
+
justify-content: center;
|
|
942
|
+
padding: 24px;
|
|
943
|
+
animation: fade-in 0.12s ease-out;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
.fv-panel {
|
|
947
|
+
background: var(--bg);
|
|
948
|
+
border-radius: 14px;
|
|
949
|
+
box-shadow: var(--shadow);
|
|
950
|
+
width: min(1000px, 100%);
|
|
951
|
+
height: min(88vh, 100%);
|
|
952
|
+
display: flex;
|
|
953
|
+
flex-direction: column;
|
|
954
|
+
overflow: hidden;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
.fv-head {
|
|
958
|
+
display: flex;
|
|
959
|
+
align-items: center;
|
|
960
|
+
gap: 10px;
|
|
961
|
+
padding: 10px 12px 10px 16px;
|
|
962
|
+
border-bottom: 1px solid var(--border);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
.fv-title {
|
|
966
|
+
flex: 1;
|
|
967
|
+
min-width: 0;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
.fv-name {
|
|
971
|
+
font-weight: 600;
|
|
972
|
+
font-size: 15px;
|
|
973
|
+
overflow: hidden;
|
|
974
|
+
text-overflow: ellipsis;
|
|
975
|
+
white-space: nowrap;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
.fv-sub {
|
|
979
|
+
color: var(--fg-soft);
|
|
980
|
+
font-size: 12px;
|
|
981
|
+
overflow: hidden;
|
|
982
|
+
text-overflow: ellipsis;
|
|
983
|
+
white-space: nowrap;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
.fv-actions {
|
|
987
|
+
display: flex;
|
|
988
|
+
gap: 8px;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
.fv-actions .btn {
|
|
992
|
+
padding: 5px 12px;
|
|
993
|
+
font-size: 13px;
|
|
994
|
+
text-decoration: none;
|
|
995
|
+
color: inherit;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
.fv-body {
|
|
999
|
+
flex: 1;
|
|
1000
|
+
min-height: 0;
|
|
1001
|
+
overflow: auto;
|
|
1002
|
+
display: flex;
|
|
1003
|
+
flex-direction: column;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
.fv-loading,
|
|
1007
|
+
.fv-empty {
|
|
1008
|
+
margin: auto;
|
|
1009
|
+
display: flex;
|
|
1010
|
+
flex-direction: column;
|
|
1011
|
+
align-items: center;
|
|
1012
|
+
gap: 14px;
|
|
1013
|
+
color: var(--fg-soft);
|
|
1014
|
+
padding: 24px;
|
|
1015
|
+
text-align: center;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
.fv-empty .btn {
|
|
1019
|
+
text-decoration: none;
|
|
1020
|
+
color: inherit;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
.fv-img,
|
|
1024
|
+
.fv-media {
|
|
1025
|
+
margin: auto;
|
|
1026
|
+
max-width: 100%;
|
|
1027
|
+
max-height: 100%;
|
|
1028
|
+
object-fit: contain;
|
|
1029
|
+
display: block;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
.fv-img {
|
|
1033
|
+
padding: 12px;
|
|
1034
|
+
box-sizing: border-box;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
.fv-media {
|
|
1038
|
+
background: #000;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
.fv-audio {
|
|
1042
|
+
margin: auto;
|
|
1043
|
+
width: min(480px, 90%);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
.fv-frame {
|
|
1047
|
+
flex: 1;
|
|
1048
|
+
border: 0;
|
|
1049
|
+
width: 100%;
|
|
1050
|
+
background: #fff;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
.fv-code {
|
|
1054
|
+
margin: 0;
|
|
1055
|
+
padding: 14px 16px;
|
|
1056
|
+
font-family: var(--mono);
|
|
1057
|
+
font-size: 13px;
|
|
1058
|
+
line-height: 1.55;
|
|
1059
|
+
background: var(--code-bg);
|
|
1060
|
+
flex: 1;
|
|
1061
|
+
white-space: pre;
|
|
1062
|
+
tab-size: 4;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
.fv-code code.hljs {
|
|
1066
|
+
background: transparent;
|
|
1067
|
+
padding: 0;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
.fv-doc {
|
|
1071
|
+
padding: 16px 24px;
|
|
1072
|
+
max-width: 820px;
|
|
1073
|
+
width: 100%;
|
|
1074
|
+
box-sizing: border-box;
|
|
1075
|
+
margin: 0 auto;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
.fv-cut {
|
|
1079
|
+
padding: 6px 16px;
|
|
1080
|
+
font-size: 12px;
|
|
1081
|
+
color: var(--fg-soft);
|
|
1082
|
+
background: var(--bg-soft);
|
|
1083
|
+
border-bottom: 1px solid var(--border);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
@media (max-width: 768px) {
|
|
1087
|
+
.file-viewer {
|
|
1088
|
+
padding: 0;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
.fv-panel {
|
|
1092
|
+
width: 100%;
|
|
1093
|
+
height: 100%;
|
|
1094
|
+
border-radius: 0;
|
|
1095
|
+
padding-top: env(safe-area-inset-top);
|
|
1096
|
+
padding-bottom: env(safe-area-inset-bottom);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
.fv-head {
|
|
1100
|
+
flex-wrap: wrap;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
.fv-actions {
|
|
1104
|
+
order: 3;
|
|
1105
|
+
width: 100%;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/* ---------- Codex visualize 画的图表:嵌在回复里(views/file-preview.js) ---------- */
|
|
1110
|
+
.md .viz {
|
|
1111
|
+
margin: 0.8em 0;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
.viz-frame {
|
|
1115
|
+
display: block;
|
|
1116
|
+
width: 100%;
|
|
1117
|
+
height: 320px;
|
|
1118
|
+
border: 0;
|
|
1119
|
+
background: transparent;
|
|
1120
|
+
/* 和页面一样跟着系统深浅色走:配色方案不一致时浏览器会给 iframe 垫一层不透明的底 */
|
|
1121
|
+
color-scheme: light dark;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
.viz-loading {
|
|
1125
|
+
height: 120px;
|
|
1126
|
+
display: flex;
|
|
1127
|
+
align-items: center;
|
|
1128
|
+
justify-content: center;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
.viz-actions {
|
|
1132
|
+
display: flex;
|
|
1133
|
+
justify-content: flex-end;
|
|
1134
|
+
gap: 4px;
|
|
1135
|
+
margin-top: 2px;
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
.md .viz-btn {
|
|
1139
|
+
font-size: 12px;
|
|
1140
|
+
color: var(--fg-soft);
|
|
1141
|
+
background: none;
|
|
1142
|
+
border: 0;
|
|
1143
|
+
padding: 2px 6px;
|
|
1144
|
+
border-radius: 6px;
|
|
1145
|
+
text-decoration: none;
|
|
1146
|
+
cursor: pointer;
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
.md .viz-btn:hover {
|
|
1150
|
+
background: var(--bg-hover);
|
|
1151
|
+
color: var(--fg);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
.viz-error {
|
|
1155
|
+
font-size: 13px;
|
|
1156
|
+
color: var(--fg-soft);
|
|
1157
|
+
padding: 10px 12px;
|
|
1158
|
+
border: 1px dashed var(--border);
|
|
1159
|
+
border-radius: 10px;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
.viz-path {
|
|
1163
|
+
font-family: var(--mono);
|
|
1164
|
+
font-size: 12px;
|
|
1165
|
+
margin-top: 2px;
|
|
1166
|
+
word-break: break-all;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
.fv-frame.fv-html {
|
|
1170
|
+
background: transparent;
|
|
1171
|
+
color-scheme: light dark;
|
|
1172
|
+
padding: 12px 16px;
|
|
1173
|
+
box-sizing: border-box;
|
|
1174
|
+
}
|