pi-web-ui 0.114.1 → 0.114.2
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/CHANGELOG.md +17 -0
- package/README.md +187 -187
- package/README.zh-CN.md +227 -227
- package/README.zh-TW.md +227 -227
- package/bin/device-hub-cli.mjs +577 -577
- package/bin/frp-manager.mjs +1573 -1573
- package/bin/pi-device-hub.mjs +9 -9
- package/bin/pi-web-ui.mjs +4081 -4081
- package/deploy/nginx-subpath.conf +100 -100
- package/deploy/pi-web-ui.service +36 -36
- package/dist/server/agent-service.js +59 -10
- package/dist/server/attachments.js +6 -6
- package/dist/server/canvas/canvas-orchestrator.js +3 -3
- package/dist/server/communicate-tool.js +37 -11
- package/dist/server/compaction-instructions.js +8 -8
- package/dist/server/device-center/desktop-page.js +228 -228
- package/dist/server/device-center/hub-page.js +64 -64
- package/dist/server/device-center/native-desktop/macos.js +190 -190
- package/dist/server/device-center/native-desktop/windows.js +116 -116
- package/dist/server/dsh/runtime/cordis.yml +1 -1
- package/dist/server/dsh/runtime/custom-prompt.mjs +33 -33
- package/dist/server/dsh/runtime/goal-rpc.mjs +1309 -1309
- package/dist/server/dsh/runtime/launcher.mjs +235 -235
- package/dist/server/dsh/runtime/override.patch.yml +89 -89
- package/dist/server/dsh/runtime/runtime-root.mjs +405 -405
- package/dist/server/durable/durable-event-projector.js +19 -3
- package/dist/server/durable/durable-models.js +134 -65
- package/dist/server/eval-tool.js +135 -135
- package/dist/server/model-resolver.js +4 -1
- package/dist/server/patch-bash-process.js +89 -89
- package/dist/server/patch-tool.js +9 -9
- package/dist/server/patch-turn-end-boundary.js +3 -3
- package/dist/server/persona-chain.js +8 -8
- package/dist/server/remote-ssh-service.js +39 -39
- package/dist/server/schedule-agent-tool.js +11 -6
- package/dist/server/scheduler-tasks.js +84 -2
- package/dist/server/scm-commitmsg.js +10 -10
- package/dist/server/setup-page.js +620 -620
- package/dist/server/universal/universal-session.js +19 -18
- package/dist/server/vision-bridge.js +18 -18
- package/docs/device-center.md +148 -148
- package/docs/device-desktop.md +48 -48
- package/docs/dsh-engine.md +493 -493
- package/extensions/webui.ts +335 -335
- package/package.json +1 -1
- package/plugin-sdk/README.md +174 -174
- package/plugin-sdk/client-utils.mjs +279 -279
- package/plugin-sdk/import-check.mjs +210 -210
- package/plugin-sdk/index.d.ts +698 -698
- package/plugin-sdk/index.mjs +696 -696
- package/plugin-sdk/package.json +13 -13
- package/plugin-sdk/ssh-host-trust.mjs +225 -225
- package/themes/aetheris.css +501 -501
- package/themes/ayu-light.css +116 -116
- package/themes/bases/flow-base.css +1357 -1357
- package/themes/bases/obsidian-base.css +895 -895
- package/themes/bases/statusbar-flat-base.css +112 -112
- package/themes/bases/zhujian-base.css +688 -688
- package/themes/bases/zhupi-base.css +660 -660
- package/themes/catppuccin-latte.css +119 -119
- package/themes/catppuccin.css +116 -116
- package/themes/claude-code-dark.css +147 -147
- package/themes/codex.css +114 -114
- package/themes/cyberpunk.css +50 -50
- package/themes/dark-teal.css +264 -264
- package/themes/dazzle.css +38 -38
- package/themes/everforest-light.css +115 -115
- package/themes/flow-light.css +164 -164
- package/themes/flow.css +161 -161
- package/themes/geist.css +114 -114
- package/themes/gruvbox-light.css +115 -115
- package/themes/kanagawa-lotus.css +115 -115
- package/themes/md-preview.css +24 -24
- package/themes/mist.css +166 -166
- package/themes/nord.css +107 -107
- package/themes/one-dark.css +106 -106
- package/themes/paper.css +174 -174
- package/themes/rose-pine-dawn.css +114 -114
- package/themes/sakura.css +166 -166
- package/themes/solarized-light.css +114 -114
- package/themes/tokyo-night.css +115 -115
- package/themes/translucent.css +85 -85
- package/themes/transparent.css +116 -116
- package/themes/vs-code-dark.css +147 -147
- package/themes/wallpaper-flow.css +382 -382
- package/themes/wallpaper-obsidian.css +74 -74
- package/themes/white.css +164 -164
- package/themes/zhujian-dark.css +115 -115
- package/themes/zhujian.css +114 -114
- package/themes/zhupi-dark.css +115 -115
- package/themes/zhupi.css +114 -114
- package/web/dist/.build-assets.json +1 -1
- package/web/dist/assets/{BgTasksModal-BcXJ20RO.js → BgTasksModal-BIlQ89vr.js} +1 -1
- package/web/dist/assets/{BgTasksModal-DjQmiKnE.js → BgTasksModal-Ck2sjlPM.js} +1 -1
- package/web/dist/assets/{CanvasStudio-PI9pid0Y.js → CanvasStudio-B3W6b_rA.js} +1 -1
- package/web/dist/assets/{CanvasStudio-nf5-HxLM.js → CanvasStudio-Csv1Sj1-.js} +1 -1
- package/web/dist/assets/{DeviceCenter-CYttJw2a.js → DeviceCenter-BQdDcO8A.js} +1 -1
- package/web/dist/assets/{DeviceCenter-DPe4sz74.js → DeviceCenter-CHRKL0Vy.js} +1 -1
- package/web/dist/assets/{FilePreview-C9Ut4Lq6.js → FilePreview-B9xoU6ON.js} +1 -1
- package/web/dist/assets/{FilePreview-CvK3ZzX5.js → FilePreview-DUQpVZkH.js} +1 -1
- package/web/dist/assets/{GlobalSearchModal-C5e-OCw2.js → GlobalSearchModal-BXEGvUZq.js} +1 -1
- package/web/dist/assets/{GlobalSearchModal-H7pFK2UC.js → GlobalSearchModal-CnVPmfOI.js} +1 -1
- package/web/dist/assets/{GroupConversation-C2wNM0dQ.js → GroupConversation-CyIpQzDd.js} +1 -1
- package/web/dist/assets/{GroupConversation-ChN3TWQK.js → GroupConversation-DgYV3uYS.js} +1 -1
- package/web/dist/assets/{SCMPanel-buTGziDB.js → SCMPanel-imYI2Ecw.js} +1 -1
- package/web/dist/assets/{SCMPanel-kx-SCL2s.js → SCMPanel-sVZcV7dR.js} +1 -1
- package/web/dist/assets/{SettingsModal-BsUF8jqr.js → SettingsModal-Xw1Yoi3_.js} +1 -1
- package/web/dist/assets/{SettingsModal-D357Ky7P.js → SettingsModal-mb4e34-0.js} +1 -1
- package/web/dist/assets/{TerminalPanel-BBuM_Sez.js → TerminalPanel-CLkJJvxg.js} +1 -1
- package/web/dist/assets/{TerminalPanel-D6x6xz8Q.js → TerminalPanel-DpcIellm.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-CdbGV3Za.js → TuiOverlayModal-4-fFAZe7.js} +1 -1
- package/web/dist/assets/{TuiOverlayModal-d6x9u25n.js → TuiOverlayModal-DCu01lks.js} +1 -1
- package/web/dist/assets/{index-D6u8xH0o.js → index-Cdh3sibd.js} +4 -4
- package/web/dist/assets/{index-BPvpea8t.js → index-DWw_SxRq.js} +4 -4
- package/web/dist/index.html +29 -29
- package/web/dist/sw.js +179 -179
- package/web/public/sw.js +179 -179
|
@@ -1,279 +1,279 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* client-utils.mjs — pi-web-ui 插件客户端常用工具层。
|
|
3
|
-
*
|
|
4
|
-
* 供插件在 client/entry.mjs 或网页视图中复用,消除 HTML 转义与基础接口漂移面。
|
|
5
|
-
* 纯 ESM,零依赖。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 安全 HTML 实体转义。
|
|
10
|
-
* @param {unknown} s 待转义内容
|
|
11
|
-
* @returns {string} 转义后的 HTML 字符串
|
|
12
|
-
*/
|
|
13
|
-
export function esc(s) {
|
|
14
|
-
return String(s ?? "").replace(
|
|
15
|
-
/[&<>"']/g,
|
|
16
|
-
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c],
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 安全取用 window.__piWebUiHost 宿主桥接 API。
|
|
22
|
-
* @returns {any} 宿主 API 对象或 null
|
|
23
|
-
*/
|
|
24
|
-
export function hostApi() {
|
|
25
|
-
return typeof window !== "undefined" ? (window.__piWebUiHost ?? null) : null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 获取插件服务端路由的统一根路径。
|
|
30
|
-
* @param {string} pluginId 插件 id
|
|
31
|
-
* @returns {string} 路由根路径,如 "/plugins-api/my-plugin"
|
|
32
|
-
*/
|
|
33
|
-
export function apiBase(pluginId) {
|
|
34
|
-
return `/plugins-api/${pluginId}`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 格式化字节大小为人类可读字符串(B / KB / MB / GB)。
|
|
39
|
-
* @param {number|unknown} bytes 字节大小
|
|
40
|
-
* @param {number} [decimals=1] 小数位数
|
|
41
|
-
* @returns {string}
|
|
42
|
-
*/
|
|
43
|
-
export function formatBytes(bytes, decimals = 1) {
|
|
44
|
-
const n = Number(bytes);
|
|
45
|
-
if (!Number.isFinite(n) || n < 0) return "";
|
|
46
|
-
if (n < 1024) return `${n} B`;
|
|
47
|
-
if (n < 1024 * 1024) return `${(n / 1024).toFixed(decimals)} KB`;
|
|
48
|
-
if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(decimals)} MB`;
|
|
49
|
-
return `${(n / (1024 * 1024 * 1024)).toFixed(decimals)} GB`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 为异步操作施加超时限制。
|
|
54
|
-
* @template T
|
|
55
|
-
* @param {Promise<T>} promise 目标 Promise
|
|
56
|
-
* @param {number} timeoutMs 超时时间(毫秒)
|
|
57
|
-
* @param {string} [errorMsg="Operation timed out"] 超时提示文案
|
|
58
|
-
* @returns {Promise<T>}
|
|
59
|
-
*/
|
|
60
|
-
export function withTimeout(promise, timeoutMs, errorMsg = "Operation timed out") {
|
|
61
|
-
return Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(new Error(errorMsg)), timeoutMs))]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 生成随机 UUID(浏览器优先使用 crypto.randomUUID,提供纯 JS 降级)。
|
|
66
|
-
* @returns {string}
|
|
67
|
-
*/
|
|
68
|
-
export function uuid() {
|
|
69
|
-
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
70
|
-
return crypto.randomUUID();
|
|
71
|
-
}
|
|
72
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
73
|
-
const r = (Math.random() * 16) | 0;
|
|
74
|
-
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
|
75
|
-
return v.toString(16);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 归一化相对路径(反斜杠转斜杠、去除首尾多余斜杠与 "./")。
|
|
81
|
-
* @param {string} p
|
|
82
|
-
* @returns {string}
|
|
83
|
-
*/
|
|
84
|
-
export function normalizeRel(p) {
|
|
85
|
-
return String(p ?? "")
|
|
86
|
-
.replace(/\\/g, "/")
|
|
87
|
-
.replace(/^\.?\/+/, "")
|
|
88
|
-
.replace(/\/+$/, "");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 为容器创建 Shadow DOM 并注入基础主题继承样式。
|
|
93
|
-
* @param {HTMLElement} hostEl 宿主容器元素
|
|
94
|
-
* @param {string} [customCss] 额外注入该组件的隔离样式
|
|
95
|
-
* @returns {ShadowRoot} 创建或已存在的 open 模式 ShadowRoot
|
|
96
|
-
*/
|
|
97
|
-
export function attachShadowRoot(hostEl, customCss = "") {
|
|
98
|
-
let shadow = hostEl.shadowRoot;
|
|
99
|
-
if (!shadow) {
|
|
100
|
-
try {
|
|
101
|
-
shadow = hostEl.attachShadow({ mode: "open" });
|
|
102
|
-
} catch {
|
|
103
|
-
shadow = hostEl.shadowRoot;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (!shadow) return hostEl;
|
|
107
|
-
|
|
108
|
-
shadow.replaceChildren();
|
|
109
|
-
const style = document.createElement("style");
|
|
110
|
-
style.textContent = `
|
|
111
|
-
:host {
|
|
112
|
-
display: block;
|
|
113
|
-
width: 100%;
|
|
114
|
-
height: 100%;
|
|
115
|
-
box-sizing: border-box;
|
|
116
|
-
color: var(--text, inherit);
|
|
117
|
-
font-family: inherit;
|
|
118
|
-
font-size: var(--text-base, 13.5px);
|
|
119
|
-
line-height: 1.5;
|
|
120
|
-
}
|
|
121
|
-
*, *::before, *::after {
|
|
122
|
-
box-sizing: inherit;
|
|
123
|
-
}
|
|
124
|
-
${customCss}
|
|
125
|
-
`;
|
|
126
|
-
shadow.appendChild(style);
|
|
127
|
-
return shadow;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 带有 Shadow DOM 样式隔离的视图挂载辅助器。
|
|
132
|
-
* @param {HTMLElement} container 挂载目标容器
|
|
133
|
-
* @param {(root: HTMLElement) => void | (() => void)} mountFn 实际挂载函数
|
|
134
|
-
* @param {string} [customCss] 隔离样式
|
|
135
|
-
* @returns {() => void} 清理函数
|
|
136
|
-
*/
|
|
137
|
-
export function mountIsolated(container, mountFn, customCss = "") {
|
|
138
|
-
const shadow = attachShadowRoot(container, customCss);
|
|
139
|
-
const inner = document.createElement("div");
|
|
140
|
-
inner.className = "plugin-isolated-body";
|
|
141
|
-
inner.style.cssText = "width:100%;height:100%;display:flex;flex-direction:column;";
|
|
142
|
-
shadow.appendChild(inner);
|
|
143
|
-
|
|
144
|
-
const cleanup = mountFn(inner);
|
|
145
|
-
return () => {
|
|
146
|
-
if (typeof cleanup === "function") {
|
|
147
|
-
try {
|
|
148
|
-
cleanup();
|
|
149
|
-
} catch {}
|
|
150
|
-
}
|
|
151
|
-
inner.replaceChildren();
|
|
152
|
-
shadow.replaceChildren();
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const DEFAULT_CHUNK_LIMIT = 3800;
|
|
157
|
-
const CODE_FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 计算安全的截断位置,避免割裂 UTF-16 代理对(Surrogate Pair Emoji/特异字符)。
|
|
161
|
-
* @param {string} value
|
|
162
|
-
* @param {number} limit
|
|
163
|
-
* @returns {number}
|
|
164
|
-
*/
|
|
165
|
-
export function safeSliceIndex(value, limit) {
|
|
166
|
-
let index = Math.min(limit, value.length);
|
|
167
|
-
if (index <= 0) return 0;
|
|
168
|
-
const before = value.charCodeAt(index - 1);
|
|
169
|
-
const after = value.charCodeAt(index);
|
|
170
|
-
if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) {
|
|
171
|
-
index -= 1;
|
|
172
|
-
}
|
|
173
|
-
return Math.max(1, index);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function openingFence(line) {
|
|
177
|
-
const normalized = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
178
|
-
const match = CODE_FENCE_OPEN.exec(normalized);
|
|
179
|
-
if (!match) return null;
|
|
180
|
-
if (match[2][0] === "`" && match[3].includes("`")) return null;
|
|
181
|
-
return { delimiter: match[2], info: match[3], indent: match[1].length };
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function closesFence(line, opening) {
|
|
185
|
-
const match = /^ {0,3}(`{3,}|~{3,})[ \t]*$/.exec(line);
|
|
186
|
-
return Boolean(match && match[1][0] === opening.delimiter[0] && match[1].length >= opening.delimiter.length);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function splitRawText(text, limit) {
|
|
190
|
-
if (text.length <= limit) return [text];
|
|
191
|
-
const parts = [];
|
|
192
|
-
let remaining = text;
|
|
193
|
-
while (remaining.length > limit) {
|
|
194
|
-
let index = safeSliceIndex(remaining, limit);
|
|
195
|
-
const newline = remaining.lastIndexOf("\n", index - 1);
|
|
196
|
-
if (newline >= 0 && newline >= Math.floor(limit * 0.4)) {
|
|
197
|
-
index = newline + 1;
|
|
198
|
-
}
|
|
199
|
-
parts.push(remaining.slice(0, index));
|
|
200
|
-
remaining = remaining.slice(index);
|
|
201
|
-
}
|
|
202
|
-
if (remaining || parts.length === 0) parts.push(remaining);
|
|
203
|
-
return parts;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 结构感知 Markdown 分块工具(专为 IM 机器人、通知卡片与受限文本通道设计)。
|
|
208
|
-
* 跨分块边界保护代码围栏(``` 或 ~~~),自动在上一块末尾闭合并在下一块开头重新打开,
|
|
209
|
-
* 确保代码高亮不破损、超长行不断字。
|
|
210
|
-
*
|
|
211
|
-
* @param {string} text 待分块 Markdown 文本
|
|
212
|
-
* @param {number} [limit=3800] 单块最大字符数(必须 > 20)
|
|
213
|
-
* @returns {string[]} 分块数组
|
|
214
|
-
*/
|
|
215
|
-
export function chunkMarkdownText(text, limit = DEFAULT_CHUNK_LIMIT) {
|
|
216
|
-
const raw = String(text ?? "").trim();
|
|
217
|
-
if (!raw) return [];
|
|
218
|
-
const bound = Math.max(50, Number(limit) || DEFAULT_CHUNK_LIMIT);
|
|
219
|
-
if (raw.length <= bound) return [raw];
|
|
220
|
-
|
|
221
|
-
const lines = raw.split("\n");
|
|
222
|
-
const chunks = [];
|
|
223
|
-
let currentLines = [];
|
|
224
|
-
let currentLen = 0;
|
|
225
|
-
let activeFence = null;
|
|
226
|
-
|
|
227
|
-
function flushChunk() {
|
|
228
|
-
if (currentLines.length === 0) return;
|
|
229
|
-
if (activeFence) {
|
|
230
|
-
currentLines.push(activeFence.delimiter);
|
|
231
|
-
chunks.push(currentLines.join("\n"));
|
|
232
|
-
currentLines = [`${activeFence.delimiter}${activeFence.info}`];
|
|
233
|
-
currentLen = currentLines[0].length + 1;
|
|
234
|
-
} else {
|
|
235
|
-
chunks.push(currentLines.join("\n"));
|
|
236
|
-
currentLines = [];
|
|
237
|
-
currentLen = 0;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
for (let i = 0; i < lines.length; i++) {
|
|
242
|
-
const line = lines[i];
|
|
243
|
-
const lineLen = line.length + 1;
|
|
244
|
-
|
|
245
|
-
if (activeFence) {
|
|
246
|
-
if (closesFence(line, activeFence)) {
|
|
247
|
-
activeFence = null;
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
const open = openingFence(line);
|
|
251
|
-
if (open) {
|
|
252
|
-
activeFence = open;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (currentLen + lineLen > bound) {
|
|
257
|
-
if (currentLines.length === 0) {
|
|
258
|
-
const parts = splitRawText(line, bound - (activeFence ? 20 : 0));
|
|
259
|
-
for (let p = 0; p < parts.length; p++) {
|
|
260
|
-
currentLines.push(parts[p]);
|
|
261
|
-
if (p < parts.length - 1) flushChunk();
|
|
262
|
-
}
|
|
263
|
-
currentLen = currentLines.join("\n").length + 1;
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
flushChunk();
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
currentLines.push(line);
|
|
271
|
-
currentLen += lineLen;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (currentLines.length > 0) {
|
|
275
|
-
chunks.push(currentLines.join("\n"));
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
return chunks;
|
|
279
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* client-utils.mjs — pi-web-ui 插件客户端常用工具层。
|
|
3
|
+
*
|
|
4
|
+
* 供插件在 client/entry.mjs 或网页视图中复用,消除 HTML 转义与基础接口漂移面。
|
|
5
|
+
* 纯 ESM,零依赖。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 安全 HTML 实体转义。
|
|
10
|
+
* @param {unknown} s 待转义内容
|
|
11
|
+
* @returns {string} 转义后的 HTML 字符串
|
|
12
|
+
*/
|
|
13
|
+
export function esc(s) {
|
|
14
|
+
return String(s ?? "").replace(
|
|
15
|
+
/[&<>"']/g,
|
|
16
|
+
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c],
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 安全取用 window.__piWebUiHost 宿主桥接 API。
|
|
22
|
+
* @returns {any} 宿主 API 对象或 null
|
|
23
|
+
*/
|
|
24
|
+
export function hostApi() {
|
|
25
|
+
return typeof window !== "undefined" ? (window.__piWebUiHost ?? null) : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 获取插件服务端路由的统一根路径。
|
|
30
|
+
* @param {string} pluginId 插件 id
|
|
31
|
+
* @returns {string} 路由根路径,如 "/plugins-api/my-plugin"
|
|
32
|
+
*/
|
|
33
|
+
export function apiBase(pluginId) {
|
|
34
|
+
return `/plugins-api/${pluginId}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 格式化字节大小为人类可读字符串(B / KB / MB / GB)。
|
|
39
|
+
* @param {number|unknown} bytes 字节大小
|
|
40
|
+
* @param {number} [decimals=1] 小数位数
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
export function formatBytes(bytes, decimals = 1) {
|
|
44
|
+
const n = Number(bytes);
|
|
45
|
+
if (!Number.isFinite(n) || n < 0) return "";
|
|
46
|
+
if (n < 1024) return `${n} B`;
|
|
47
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(decimals)} KB`;
|
|
48
|
+
if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(decimals)} MB`;
|
|
49
|
+
return `${(n / (1024 * 1024 * 1024)).toFixed(decimals)} GB`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 为异步操作施加超时限制。
|
|
54
|
+
* @template T
|
|
55
|
+
* @param {Promise<T>} promise 目标 Promise
|
|
56
|
+
* @param {number} timeoutMs 超时时间(毫秒)
|
|
57
|
+
* @param {string} [errorMsg="Operation timed out"] 超时提示文案
|
|
58
|
+
* @returns {Promise<T>}
|
|
59
|
+
*/
|
|
60
|
+
export function withTimeout(promise, timeoutMs, errorMsg = "Operation timed out") {
|
|
61
|
+
return Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(new Error(errorMsg)), timeoutMs))]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 生成随机 UUID(浏览器优先使用 crypto.randomUUID,提供纯 JS 降级)。
|
|
66
|
+
* @returns {string}
|
|
67
|
+
*/
|
|
68
|
+
export function uuid() {
|
|
69
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
70
|
+
return crypto.randomUUID();
|
|
71
|
+
}
|
|
72
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
73
|
+
const r = (Math.random() * 16) | 0;
|
|
74
|
+
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
|
75
|
+
return v.toString(16);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 归一化相对路径(反斜杠转斜杠、去除首尾多余斜杠与 "./")。
|
|
81
|
+
* @param {string} p
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
export function normalizeRel(p) {
|
|
85
|
+
return String(p ?? "")
|
|
86
|
+
.replace(/\\/g, "/")
|
|
87
|
+
.replace(/^\.?\/+/, "")
|
|
88
|
+
.replace(/\/+$/, "");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 为容器创建 Shadow DOM 并注入基础主题继承样式。
|
|
93
|
+
* @param {HTMLElement} hostEl 宿主容器元素
|
|
94
|
+
* @param {string} [customCss] 额外注入该组件的隔离样式
|
|
95
|
+
* @returns {ShadowRoot} 创建或已存在的 open 模式 ShadowRoot
|
|
96
|
+
*/
|
|
97
|
+
export function attachShadowRoot(hostEl, customCss = "") {
|
|
98
|
+
let shadow = hostEl.shadowRoot;
|
|
99
|
+
if (!shadow) {
|
|
100
|
+
try {
|
|
101
|
+
shadow = hostEl.attachShadow({ mode: "open" });
|
|
102
|
+
} catch {
|
|
103
|
+
shadow = hostEl.shadowRoot;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (!shadow) return hostEl;
|
|
107
|
+
|
|
108
|
+
shadow.replaceChildren();
|
|
109
|
+
const style = document.createElement("style");
|
|
110
|
+
style.textContent = `
|
|
111
|
+
:host {
|
|
112
|
+
display: block;
|
|
113
|
+
width: 100%;
|
|
114
|
+
height: 100%;
|
|
115
|
+
box-sizing: border-box;
|
|
116
|
+
color: var(--text, inherit);
|
|
117
|
+
font-family: inherit;
|
|
118
|
+
font-size: var(--text-base, 13.5px);
|
|
119
|
+
line-height: 1.5;
|
|
120
|
+
}
|
|
121
|
+
*, *::before, *::after {
|
|
122
|
+
box-sizing: inherit;
|
|
123
|
+
}
|
|
124
|
+
${customCss}
|
|
125
|
+
`;
|
|
126
|
+
shadow.appendChild(style);
|
|
127
|
+
return shadow;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 带有 Shadow DOM 样式隔离的视图挂载辅助器。
|
|
132
|
+
* @param {HTMLElement} container 挂载目标容器
|
|
133
|
+
* @param {(root: HTMLElement) => void | (() => void)} mountFn 实际挂载函数
|
|
134
|
+
* @param {string} [customCss] 隔离样式
|
|
135
|
+
* @returns {() => void} 清理函数
|
|
136
|
+
*/
|
|
137
|
+
export function mountIsolated(container, mountFn, customCss = "") {
|
|
138
|
+
const shadow = attachShadowRoot(container, customCss);
|
|
139
|
+
const inner = document.createElement("div");
|
|
140
|
+
inner.className = "plugin-isolated-body";
|
|
141
|
+
inner.style.cssText = "width:100%;height:100%;display:flex;flex-direction:column;";
|
|
142
|
+
shadow.appendChild(inner);
|
|
143
|
+
|
|
144
|
+
const cleanup = mountFn(inner);
|
|
145
|
+
return () => {
|
|
146
|
+
if (typeof cleanup === "function") {
|
|
147
|
+
try {
|
|
148
|
+
cleanup();
|
|
149
|
+
} catch {}
|
|
150
|
+
}
|
|
151
|
+
inner.replaceChildren();
|
|
152
|
+
shadow.replaceChildren();
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const DEFAULT_CHUNK_LIMIT = 3800;
|
|
157
|
+
const CODE_FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 计算安全的截断位置,避免割裂 UTF-16 代理对(Surrogate Pair Emoji/特异字符)。
|
|
161
|
+
* @param {string} value
|
|
162
|
+
* @param {number} limit
|
|
163
|
+
* @returns {number}
|
|
164
|
+
*/
|
|
165
|
+
export function safeSliceIndex(value, limit) {
|
|
166
|
+
let index = Math.min(limit, value.length);
|
|
167
|
+
if (index <= 0) return 0;
|
|
168
|
+
const before = value.charCodeAt(index - 1);
|
|
169
|
+
const after = value.charCodeAt(index);
|
|
170
|
+
if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) {
|
|
171
|
+
index -= 1;
|
|
172
|
+
}
|
|
173
|
+
return Math.max(1, index);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function openingFence(line) {
|
|
177
|
+
const normalized = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
178
|
+
const match = CODE_FENCE_OPEN.exec(normalized);
|
|
179
|
+
if (!match) return null;
|
|
180
|
+
if (match[2][0] === "`" && match[3].includes("`")) return null;
|
|
181
|
+
return { delimiter: match[2], info: match[3], indent: match[1].length };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function closesFence(line, opening) {
|
|
185
|
+
const match = /^ {0,3}(`{3,}|~{3,})[ \t]*$/.exec(line);
|
|
186
|
+
return Boolean(match && match[1][0] === opening.delimiter[0] && match[1].length >= opening.delimiter.length);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function splitRawText(text, limit) {
|
|
190
|
+
if (text.length <= limit) return [text];
|
|
191
|
+
const parts = [];
|
|
192
|
+
let remaining = text;
|
|
193
|
+
while (remaining.length > limit) {
|
|
194
|
+
let index = safeSliceIndex(remaining, limit);
|
|
195
|
+
const newline = remaining.lastIndexOf("\n", index - 1);
|
|
196
|
+
if (newline >= 0 && newline >= Math.floor(limit * 0.4)) {
|
|
197
|
+
index = newline + 1;
|
|
198
|
+
}
|
|
199
|
+
parts.push(remaining.slice(0, index));
|
|
200
|
+
remaining = remaining.slice(index);
|
|
201
|
+
}
|
|
202
|
+
if (remaining || parts.length === 0) parts.push(remaining);
|
|
203
|
+
return parts;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 结构感知 Markdown 分块工具(专为 IM 机器人、通知卡片与受限文本通道设计)。
|
|
208
|
+
* 跨分块边界保护代码围栏(``` 或 ~~~),自动在上一块末尾闭合并在下一块开头重新打开,
|
|
209
|
+
* 确保代码高亮不破损、超长行不断字。
|
|
210
|
+
*
|
|
211
|
+
* @param {string} text 待分块 Markdown 文本
|
|
212
|
+
* @param {number} [limit=3800] 单块最大字符数(必须 > 20)
|
|
213
|
+
* @returns {string[]} 分块数组
|
|
214
|
+
*/
|
|
215
|
+
export function chunkMarkdownText(text, limit = DEFAULT_CHUNK_LIMIT) {
|
|
216
|
+
const raw = String(text ?? "").trim();
|
|
217
|
+
if (!raw) return [];
|
|
218
|
+
const bound = Math.max(50, Number(limit) || DEFAULT_CHUNK_LIMIT);
|
|
219
|
+
if (raw.length <= bound) return [raw];
|
|
220
|
+
|
|
221
|
+
const lines = raw.split("\n");
|
|
222
|
+
const chunks = [];
|
|
223
|
+
let currentLines = [];
|
|
224
|
+
let currentLen = 0;
|
|
225
|
+
let activeFence = null;
|
|
226
|
+
|
|
227
|
+
function flushChunk() {
|
|
228
|
+
if (currentLines.length === 0) return;
|
|
229
|
+
if (activeFence) {
|
|
230
|
+
currentLines.push(activeFence.delimiter);
|
|
231
|
+
chunks.push(currentLines.join("\n"));
|
|
232
|
+
currentLines = [`${activeFence.delimiter}${activeFence.info}`];
|
|
233
|
+
currentLen = currentLines[0].length + 1;
|
|
234
|
+
} else {
|
|
235
|
+
chunks.push(currentLines.join("\n"));
|
|
236
|
+
currentLines = [];
|
|
237
|
+
currentLen = 0;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
for (let i = 0; i < lines.length; i++) {
|
|
242
|
+
const line = lines[i];
|
|
243
|
+
const lineLen = line.length + 1;
|
|
244
|
+
|
|
245
|
+
if (activeFence) {
|
|
246
|
+
if (closesFence(line, activeFence)) {
|
|
247
|
+
activeFence = null;
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
const open = openingFence(line);
|
|
251
|
+
if (open) {
|
|
252
|
+
activeFence = open;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (currentLen + lineLen > bound) {
|
|
257
|
+
if (currentLines.length === 0) {
|
|
258
|
+
const parts = splitRawText(line, bound - (activeFence ? 20 : 0));
|
|
259
|
+
for (let p = 0; p < parts.length; p++) {
|
|
260
|
+
currentLines.push(parts[p]);
|
|
261
|
+
if (p < parts.length - 1) flushChunk();
|
|
262
|
+
}
|
|
263
|
+
currentLen = currentLines.join("\n").length + 1;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
flushChunk();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
currentLines.push(line);
|
|
271
|
+
currentLen += lineLen;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (currentLines.length > 0) {
|
|
275
|
+
chunks.push(currentLines.join("\n"));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return chunks;
|
|
279
|
+
}
|