pi-hub-server 0.4.0 → 0.4.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/README.md +60 -5
- package/bin/pi-hub.mjs +311 -33
- package/docs/AUTH.md +3 -1
- package/package.json +6 -2
- package/src/open-browser.mjs +21 -0
- package/src/pi-session.mjs +61 -12
- package/src/pi-spawn.mjs +115 -0
- package/src/pi-version.mjs +116 -0
- package/src/registry.mjs +10 -1
- package/src/rpc-once.mjs +28 -8
- package/src/server.mjs +54 -7
- package/src/session-lock-fallback.mjs +223 -0
- package/src/session-lock.mjs +80 -25
- package/src/sessions-index.mjs +7 -0
- package/web/app.js +10 -3
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内置的最小会话锁 —— 没装 `pi-session-lock` 插件时的降级实现。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有它:pi-hub 的唯一硬要求是「同一个 session .jsonl 只有一个写者」,
|
|
5
|
+
* 而插件只是这个判定的*首选*实现(它还带让锁/fork 策略)。全新机器上没装插件时
|
|
6
|
+
* 不能因此让 `pi-hub doctor` / `pi-hub serve` 崩掉,所以这里保留最小的一份。
|
|
7
|
+
*
|
|
8
|
+
* 与插件**共用同一个命名空间**:`<agentDir>/session-locks/<sha256(realpath(session))>.lock/owner.json`,
|
|
9
|
+
* 装了插件之后两边看到的是同一把锁,可以互相判活。判定口径也只保留插件文档里的那几条:
|
|
10
|
+
* 1. mkdir 的 O_EXCL 语义给出原子认领(并发抢锁只有一个成功)
|
|
11
|
+
* 2. owner.json 记录**实际写盘进程**的 pid / starttime / boot_id(hub 代管时是 pi 子进程)
|
|
12
|
+
* 3. 只有能证明 owner 已死(pid 没了 / pid 被复用 / 关机重启)才回收,其余一律 fail-open
|
|
13
|
+
* 降级不放宽互斥:抢到手的锁靠 mkdir 原子性保证,不依赖任何进程内状态。
|
|
14
|
+
* 删掉的是插件的额外策略(yield 请求、paseo 轮次、follow 转发),那些不影响互斥。
|
|
15
|
+
*/
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import os from "node:os";
|
|
18
|
+
import path from "node:path";
|
|
19
|
+
import crypto from "node:crypto";
|
|
20
|
+
|
|
21
|
+
const HOST = os.hostname();
|
|
22
|
+
|
|
23
|
+
/** 与插件同一个 globalThis 键:真插件与 fallback 同进程加载时不会互相判成「别人」。 */
|
|
24
|
+
const IDENTITY = Symbol.for("pi.session-lock.identity");
|
|
25
|
+
|
|
26
|
+
export function resolveAgentDir(explicit) {
|
|
27
|
+
return explicit || process.env.PI_CODING_AGENT_DIR || path.join(os.homedir(), ".pi", "agent");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 逃生阀:`PI_SESSION_LOCK=off`(插件口径)/ `PI_HUB_NO_SESSION_LOCK=1` 都关锁。 */
|
|
31
|
+
export function enabled() {
|
|
32
|
+
if ((process.env.PI_SESSION_LOCK ?? "").toLowerCase() === "off") return false;
|
|
33
|
+
if (process.env.PI_HUB_NO_SESSION_LOCK === "1") return false;
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 本平台的判活能力(doctor 打印;不影响互斥强度)。
|
|
39
|
+
*
|
|
40
|
+
* Linux 有 /proc:能拿到 boot_id 与 starttime,可以识别「pid 被复用」和「重启过」。
|
|
41
|
+
* 其他平台(Windows/macOS)没有等价节点:boot_id 读不到就留空(跳过重启判定),
|
|
42
|
+
* starttime 留空(跳过 pid 复用判定),只剩 pid 存活这一条 —— 仍能证明「已死」,
|
|
43
|
+
* 但判错方向永远是 fail-open(宁可判 unknown 也不误判 dead),互斥不受影响。
|
|
44
|
+
*/
|
|
45
|
+
export function capabilities() {
|
|
46
|
+
const linux = process.platform === "linux";
|
|
47
|
+
return {
|
|
48
|
+
platform: process.platform,
|
|
49
|
+
bootId: linux,
|
|
50
|
+
startTicks: linux,
|
|
51
|
+
note: linux
|
|
52
|
+
? null
|
|
53
|
+
: `${process.platform} 没有 /proc:只按 pid 存活判死,pid 复用与重启检测不可用(互斥口径不变)`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readBootId() {
|
|
58
|
+
try {
|
|
59
|
+
return fs.readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim();
|
|
60
|
+
} catch {
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function readStartTicks(pid) {
|
|
66
|
+
try {
|
|
67
|
+
const stat = fs.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
68
|
+
// 字段从 "pid (comm) " 之后从 #3(state) 开始;starttime 是 #22 → 下标 19。
|
|
69
|
+
const rest = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
70
|
+
return rest[19] ?? "";
|
|
71
|
+
} catch {
|
|
72
|
+
return "";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function isAlive(pid) {
|
|
77
|
+
try {
|
|
78
|
+
process.kill(pid, 0);
|
|
79
|
+
return true;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
return error.code === "EPERM";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 本进程的稳定身份(Symbol 复用:同进程多次 import / 扩展 reload 不改 instanceId)。 */
|
|
86
|
+
export function identity() {
|
|
87
|
+
const g = globalThis;
|
|
88
|
+
if (!g[IDENTITY]) {
|
|
89
|
+
g[IDENTITY] = {
|
|
90
|
+
v: 1,
|
|
91
|
+
pid: process.pid,
|
|
92
|
+
host: HOST,
|
|
93
|
+
bootId: readBootId(),
|
|
94
|
+
startTicks: readStartTicks(process.pid),
|
|
95
|
+
instanceId: crypto.randomUUID(),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return g[IDENTITY];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 与插件逐字一致的锁目录:sha256(realpath(session)),文件还没落盘时退化为 path.resolve。 */
|
|
102
|
+
export function lockDirFor(sessionFile, opts = {}) {
|
|
103
|
+
const canonical = (() => {
|
|
104
|
+
try {
|
|
105
|
+
return fs.realpathSync(sessionFile);
|
|
106
|
+
} catch {
|
|
107
|
+
return path.resolve(sessionFile);
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
110
|
+
const hash = crypto.createHash("sha256").update(canonical).digest("hex");
|
|
111
|
+
return path.join(resolveAgentDir(opts.agentDir), "session-locks", `${hash}.lock`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function readOwner(dir) {
|
|
115
|
+
try {
|
|
116
|
+
return JSON.parse(fs.readFileSync(path.join(dir, "owner.json"), "utf8"));
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** live = 证明有另一个活进程;dead = 证明所有者已消失;unknown = 说不清(→ fail-open)。 */
|
|
123
|
+
export function classify(owner) {
|
|
124
|
+
const me = identity();
|
|
125
|
+
if (owner?.instanceId === me.instanceId) return { kind: "self", reason: "owned by this process" };
|
|
126
|
+
if (owner?.host !== HOST) return { kind: "unknown", reason: `owner on another host (${owner?.host})` };
|
|
127
|
+
if (owner?.bootId !== me.bootId) return { kind: "dead", reason: "host rebooted since lock was taken" };
|
|
128
|
+
if (typeof owner?.pid !== "number") return { kind: "unknown", reason: "owner has no pid" };
|
|
129
|
+
if (!isAlive(owner.pid)) return { kind: "dead", reason: `pid ${owner.pid} is gone` };
|
|
130
|
+
if (owner.startTicks && owner.startTicks !== readStartTicks(owner.pid))
|
|
131
|
+
return { kind: "dead", reason: `pid ${owner.pid} was reused (starttime mismatch)` };
|
|
132
|
+
return { kind: "live", reason: `pid ${owner.pid} is alive` };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 认领锁:mkdir 的 O_EXCL 语义是唯一原子点;死锁回收后重试(最多 3 次)。
|
|
137
|
+
*
|
|
138
|
+
* @param {string} sessionFile
|
|
139
|
+
* @param {{agentDir?:string, writerPid?:number, source?:string}} [opts]
|
|
140
|
+
* writerPid = 实际写盘进程(hub 传 pi 子进程 pid;不传则记自身)
|
|
141
|
+
* @returns {{ok:boolean, state:"owned"|"conflict"|"unknown"|"off", dir:string|null, reason:string, owner?:any}}
|
|
142
|
+
*/
|
|
143
|
+
export function claim(sessionFile, opts = {}) {
|
|
144
|
+
if (!enabled()) return { ok: true, state: "off", dir: null, reason: "disabled" };
|
|
145
|
+
const dir = lockDirFor(sessionFile, opts);
|
|
146
|
+
const me = identity();
|
|
147
|
+
const writer = opts.writerPid ?? me.pid;
|
|
148
|
+
|
|
149
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
150
|
+
let taken = false;
|
|
151
|
+
try {
|
|
152
|
+
fs.mkdirSync(path.dirname(dir), { recursive: true, mode: 0o700 });
|
|
153
|
+
fs.mkdirSync(dir, { mode: 0o700 });
|
|
154
|
+
taken = true;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (error.code !== "EEXIST") return { ok: false, state: "unknown", dir, reason: String(error) };
|
|
157
|
+
}
|
|
158
|
+
if (taken) {
|
|
159
|
+
const owner = {
|
|
160
|
+
...me,
|
|
161
|
+
pid: writer,
|
|
162
|
+
startTicks: readStartTicks(writer),
|
|
163
|
+
source: opts.source ?? "pi-hub-fallback",
|
|
164
|
+
sessionFile,
|
|
165
|
+
acquiredAt: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
// hub 代管时 pid 是 pi 子进程:hub 被 SIGKILL 后子进程也走了,锁随之判死;
|
|
168
|
+
// ownerPid 只为排查「谁认领的」。
|
|
169
|
+
if (me.pid !== writer) owner.ownerPid = me.pid;
|
|
170
|
+
try {
|
|
171
|
+
fs.writeFileSync(path.join(dir, "owner.json"), `${JSON.stringify(owner, null, 2)}\n`, { mode: 0o600 });
|
|
172
|
+
} catch (error) {
|
|
173
|
+
// 目录建了但 owner.json 写不进去:清掉,别留一把谁也认不出的锁。
|
|
174
|
+
try {
|
|
175
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
176
|
+
} catch {
|
|
177
|
+
/* best effort */
|
|
178
|
+
}
|
|
179
|
+
return { ok: false, state: "unknown", dir, reason: `owner.json write failed: ${error.message}` };
|
|
180
|
+
}
|
|
181
|
+
return { ok: true, state: "owned", dir, reason: "claimed", owner };
|
|
182
|
+
}
|
|
183
|
+
const owner = readOwner(dir);
|
|
184
|
+
if (!owner || typeof owner.pid !== "number") {
|
|
185
|
+
return { ok: false, state: "unknown", dir, reason: "owner.json missing or malformed" };
|
|
186
|
+
}
|
|
187
|
+
const verdict = classify(owner);
|
|
188
|
+
if (verdict.kind === "self") return { ok: true, state: "owned", dir, reason: "already ours", owner };
|
|
189
|
+
if (verdict.kind === "unknown") return { ok: false, state: "unknown", dir, reason: verdict.reason, owner };
|
|
190
|
+
if (verdict.kind === "live") return { ok: false, state: "conflict", dir, reason: verdict.reason, owner };
|
|
191
|
+
// dead → 回收后重试
|
|
192
|
+
try {
|
|
193
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
194
|
+
} catch {
|
|
195
|
+
return { ok: false, state: "unknown", dir, reason: "stale lock could not be removed" };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return { ok: false, state: "unknown", dir, reason: "lock reclaim retried 3x" };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 只读探测:不创建、不回收(DELETE ?delete=1 的 409 前置检查用)。 */
|
|
202
|
+
export function peek(sessionFile, opts = {}) {
|
|
203
|
+
if (!enabled()) return { state: "off", dir: null, owner: null, reason: "disabled" };
|
|
204
|
+
const dir = lockDirFor(sessionFile, opts);
|
|
205
|
+
if (!fs.existsSync(dir)) return { state: "none", dir, owner: null, reason: "no lock" };
|
|
206
|
+
const owner = readOwner(dir);
|
|
207
|
+
if (!owner || typeof owner.pid !== "number") {
|
|
208
|
+
return { state: "unknown", dir, owner: null, reason: "owner.json missing or malformed" };
|
|
209
|
+
}
|
|
210
|
+
const verdict = classify(owner);
|
|
211
|
+
return { state: verdict.kind, dir, owner, reason: verdict.reason };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 只释放自己持有的锁(按 instanceId 认),绝不误删别人的。 */
|
|
215
|
+
export function release(dir) {
|
|
216
|
+
if (!dir) return;
|
|
217
|
+
try {
|
|
218
|
+
const owner = readOwner(dir);
|
|
219
|
+
if (owner && owner.instanceId === identity().instanceId) fs.rmSync(dir, { recursive: true, force: true });
|
|
220
|
+
} catch {
|
|
221
|
+
/* best effort */
|
|
222
|
+
}
|
|
223
|
+
}
|
package/src/session-lock.mjs
CHANGED
|
@@ -1,42 +1,97 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 会话锁 ——
|
|
2
|
+
* 会话锁 —— 优先用 `pi-session-lock` 插件的唯一实现,找不到就降级到内置最小实现。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* 加载期**不再 throw**:全新机器上 `pi-hub doctor` / `pi-hub serve` 必须能起来
|
|
5
|
+
* (开箱即用是硬指标;旧实现 `throw new Error("pi-session-lock core not found")` 让
|
|
6
|
+
* 所有子命令连 import 都过不去)。降级不等于静默:`lockSource` 由 serve banner、
|
|
7
|
+
* `pi-hub doctor` 与 `/v1/health` 打出来,`corePath` 指到实际加载的那份。
|
|
8
|
+
*
|
|
9
|
+
* 解析顺序(显式优先,其次各处常见安装位置):
|
|
10
|
+
* 1. `PI_HUB_SESSION_LOCK_CORE`(显式指定,gate S6 用它验证 plugin 分支)
|
|
11
|
+
* 2. 自身 node_modules —— `require.resolve`,覆盖 `npm i -g pi-session-lock`(全局时是本包的
|
|
12
|
+
* 嵌套依赖)与仓内 `npm i pi-session-lock` 两种装法
|
|
13
|
+
* 3. 兄弟全局目录 `<prefix>/lib/node_modules/pi-session-lock/core.mjs`(两个包各自全局装)
|
|
14
|
+
* 4. `~/pi-plugins/pi-session-lock/core.mjs`(插件仓直接 clone 的装法)
|
|
15
|
+
* 5. `~/.pi/agent/npm/node_modules/pi-session-lock/core.mjs`(pi 自己的 npm 布局)
|
|
6
16
|
*/
|
|
7
17
|
import fs from "node:fs";
|
|
8
18
|
import path from "node:path";
|
|
9
|
-
import {
|
|
19
|
+
import { createRequire } from "node:module";
|
|
20
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
21
|
import { HOME } from "./config.mjs";
|
|
22
|
+
import * as fallbackLock from "./session-lock-fallback.mjs";
|
|
23
|
+
|
|
24
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
11
25
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
/** 插件必须给出的函数(= 本模块导出的全部读写面);缺一个就当没装(降级要显式,不能半残着跑)。 */
|
|
27
|
+
const REQUIRED = ["identity", "lockDirFor", "peek", "claim", "release", "classify", "readOwner"];
|
|
28
|
+
|
|
29
|
+
function resolveInstalled() {
|
|
30
|
+
const require = createRequire(import.meta.url);
|
|
31
|
+
// 插件的 exports 表里 "." 与 "./core" 都指向 core.mjs;两种写法都试,避免依赖表细节。
|
|
32
|
+
for (const specifier of ["pi-session-lock/core", "pi-session-lock"]) {
|
|
33
|
+
try {
|
|
34
|
+
return require.resolve(specifier);
|
|
35
|
+
} catch {
|
|
36
|
+
// 换下一个写法
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function candidates() {
|
|
43
|
+
return [
|
|
44
|
+
process.env.PI_HUB_SESSION_LOCK_CORE,
|
|
45
|
+
resolveInstalled(),
|
|
46
|
+
path.join(HERE, "..", "node_modules", "pi-session-lock", "core.mjs"),
|
|
47
|
+
path.join(HERE, "..", "..", "pi-session-lock", "core.mjs"),
|
|
48
|
+
path.join(HOME, "pi-plugins", "pi-session-lock", "core.mjs"),
|
|
49
|
+
path.join(HOME, ".pi", "agent", "npm", "node_modules", "pi-session-lock", "core.mjs"),
|
|
50
|
+
].filter(Boolean);
|
|
51
|
+
}
|
|
17
52
|
|
|
18
|
-
|
|
53
|
+
function exists(p) {
|
|
19
54
|
try {
|
|
20
55
|
return fs.existsSync(p);
|
|
21
56
|
} catch {
|
|
22
57
|
return false;
|
|
23
58
|
}
|
|
24
|
-
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const foundPath = candidates().find(exists) ?? null;
|
|
25
62
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
63
|
+
async function loadPlugin() {
|
|
64
|
+
if (!foundPath) return null;
|
|
65
|
+
let mod;
|
|
66
|
+
try {
|
|
67
|
+
mod = await import(pathToFileURL(foundPath).href);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.error(`pi-session-lock(${foundPath})加载失败,降级到内置实现:${err.message}`);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const missing = REQUIRED.filter((name) => typeof mod[name] !== "function");
|
|
73
|
+
if (missing.length) {
|
|
74
|
+
console.error(`pi-session-lock(${foundPath})缺少 ${missing.join(", ")},降级到内置实现`);
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return mod;
|
|
31
78
|
}
|
|
32
79
|
|
|
33
|
-
const
|
|
80
|
+
const plugin = await loadPlugin();
|
|
81
|
+
const impl = plugin ?? fallbackLock;
|
|
82
|
+
|
|
83
|
+
/** 实际使用的锁实现来源:"plugin" | "fallback"(serve banner / doctor / /v1/health 都打它)。 */
|
|
84
|
+
export const lockSource = plugin ? "plugin" : "fallback";
|
|
85
|
+
/** 插件 core.mjs 的绝对路径;降级时为 null(内置实现的路径就是本仓 src/session-lock-fallback.mjs)。 */
|
|
86
|
+
export const corePath = plugin ? foundPath : null;
|
|
34
87
|
|
|
35
|
-
export const
|
|
36
|
-
export const
|
|
37
|
-
export const
|
|
38
|
-
export const
|
|
39
|
-
export const
|
|
40
|
-
export const
|
|
41
|
-
export const
|
|
42
|
-
|
|
88
|
+
export const identity = impl.identity;
|
|
89
|
+
export const lockDirFor = impl.lockDirFor;
|
|
90
|
+
export const peek = impl.peek;
|
|
91
|
+
export const claim = impl.claim;
|
|
92
|
+
export const release = impl.release;
|
|
93
|
+
export const classify = impl.classify;
|
|
94
|
+
export const readOwner = impl.readOwner;
|
|
95
|
+
/** 判活能力(plugin 若没导出就报 plugin,doctor 只用来提示平台差异)。 */
|
|
96
|
+
export const capabilities =
|
|
97
|
+
typeof impl.capabilities === "function" ? impl.capabilities : () => ({ platform: process.platform, note: null });
|
package/src/sessions-index.mjs
CHANGED
|
@@ -157,15 +157,22 @@ export function readHistory(file, opts = {}) {
|
|
|
157
157
|
if (!m) continue;
|
|
158
158
|
const blocks = Array.isArray(m.content) ? m.content : [{ type: "text", text: m.content ?? "" }];
|
|
159
159
|
const textParts = [];
|
|
160
|
+
const thinkingParts = [];
|
|
160
161
|
const toolCalls = [];
|
|
161
162
|
for (const b of blocks) {
|
|
162
163
|
if (b?.type === "text" && b.text) textParts.push(b.text);
|
|
164
|
+
else if (b?.type === "thinking" && b.thinking) thinkingParts.push(b.thinking);
|
|
163
165
|
else if (b?.type === "toolCall") toolCalls.push({ id: b.id, name: b.name, args: b.args });
|
|
164
166
|
}
|
|
165
167
|
out.push({
|
|
166
168
|
entryId: rec.id,
|
|
167
169
|
role: m.role,
|
|
168
170
|
text: textParts.join("\n"),
|
|
171
|
+
// 思考正文:以前不回放,客户端「过程」折叠块重开会话后读不到思考内容(R4 实测)。
|
|
172
|
+
// 无 thinking 块时是空串,旧客户端忽略这个字段,契约向后兼容。
|
|
173
|
+
thinking: thinkingParts.join("\n"),
|
|
174
|
+
// toolResult 靠它并回对应工具卡片(多工具轮次里只看「上一条 tool」会堆错位置)。
|
|
175
|
+
toolCallId: m.toolCallId ?? null,
|
|
169
176
|
toolCalls,
|
|
170
177
|
ts: m.timestamp ? new Date(m.timestamp).toISOString() : rec.timestamp,
|
|
171
178
|
usage: m.usage ?? null,
|
package/web/app.js
CHANGED
|
@@ -814,6 +814,9 @@ function setStreamingUi(busy) {
|
|
|
814
814
|
|
|
815
815
|
// ---------- UI 对话框(扩展 UI 桥) ----------
|
|
816
816
|
|
|
817
|
+
// 扩展对话框的兜底占位符:服务端(扩展)给了 ev.placeholder 就用它的;没给才用统一默认文案。
|
|
818
|
+
const UI_PLACEHOLDER_DEFAULT = { input: "输入…", editor: "输入多行文本…" };
|
|
819
|
+
|
|
817
820
|
function renderUiRequest(ev) {
|
|
818
821
|
const box = $("ui-requests");
|
|
819
822
|
const wrap = document.createElement("div");
|
|
@@ -850,9 +853,13 @@ function renderUiRequest(ev) {
|
|
|
850
853
|
button("确定", { confirmed: true }, "btn primary");
|
|
851
854
|
button("取消", { confirmed: false });
|
|
852
855
|
} else {
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
+
// `editor` 是多行语义(pi 的 editor 请求),必须用 textarea;`input` 仍是单行。
|
|
857
|
+
// 两者都优先用服务端给的 placeholder / prefill,只有 placeholder 缺省时才回退到统一文案。
|
|
858
|
+
const multiline = ev.method === "editor";
|
|
859
|
+
const input = document.createElement(multiline ? "textarea" : "input");
|
|
860
|
+
if (multiline) input.rows = 8;
|
|
861
|
+
else input.type = "text";
|
|
862
|
+
input.placeholder = ev.placeholder || (multiline ? UI_PLACEHOLDER_DEFAULT.editor : UI_PLACEHOLDER_DEFAULT.input);
|
|
856
863
|
input.value = ev.prefill ?? "";
|
|
857
864
|
row.append(input);
|
|
858
865
|
button("提交", null, "btn primary");
|