pi-hub-server 0.4.2 → 0.9.0

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/web/compose.js ADDED
@@ -0,0 +1,39 @@
1
+ // 网页端的纯逻辑(无 DOM、无 localStorage、无网络):node 直接单测,见 test/web-compose.mjs。
2
+ // app.js 只负责把 DOM 量出来的数喂进来、把键写进 localStorage;判定与拼键都留在这里。
3
+ "use strict";
4
+
5
+ /** 「贴底」阈值(px):与安卓端 `offset < 32px` 同一量级。差半屏不算贴底,否则上滑也会被拽回来。 */
6
+ export const PIN_BOTTOM_THRESHOLD = 32;
7
+
8
+ /**
9
+ * 是否「贴底」——只有它为真时,流式/新消息才允许自动滚到底。
10
+ * `metrics` 可以是 `{scrollHeight, scrollTop, clientHeight}`,也可以直接传滚动容器元素(同名字段)。
11
+ * 量不到(null / 字段缺失)按 0 算 ⇒ 贴底,等价于「没有视口就谈不上偏离」。
12
+ * @param {{scrollHeight?: number, scrollTop?: number, clientHeight?: number}|null} metrics
13
+ * @returns {boolean}
14
+ */
15
+ export function isPinned(metrics) {
16
+ const { scrollHeight = 0, scrollTop = 0, clientHeight = 0 } = metrics ?? {};
17
+ return scrollHeight - scrollTop - clientHeight < PIN_BOTTOM_THRESHOLD;
18
+ }
19
+
20
+ /** 草稿在 localStorage 里的键前缀:一条会话/一个窗口一条记录。 */
21
+ export const DRAFT_STORAGE_PREFIX = "pihub.draft.";
22
+
23
+ /**
24
+ * 草稿键:会话里=会话 id;还没建会话的新开窗口=`n:<cwd>`。
25
+ * 所以同一 cwd 的新窗口共用一份草稿、不同 cwd 各自独立;cwd 为空(空项目/空会话)也有
26
+ * `n:` 这一份兜底草稿,不会因为「还没工作区」就不留字。
27
+ * @param {string|null|undefined} sessionId
28
+ * @param {string|null|undefined} cwd
29
+ * @returns {string}
30
+ */
31
+ export function draftKeyOf(sessionId, cwd) {
32
+ if (sessionId) return String(sessionId);
33
+ return `n:${cwd ?? ""}`;
34
+ }
35
+
36
+ /** 草稿键 → localStorage 键(集中在这里,测试可以直接断言不会串键)。 */
37
+ export function draftStorageKey(key) {
38
+ return `${DRAFT_STORAGE_PREFIX}${key}`;
39
+ }
package/web/index.html CHANGED
@@ -24,7 +24,8 @@
24
24
  <section id="login" class="screen login" hidden>
25
25
  <form id="login-form" class="login-card" autocomplete="off">
26
26
  <h1>pi hub</h1>
27
- <p class="hint">粘贴主机令牌(<code>pi-hub token</code> 可查看),或输入主机上 <code>pi-hub web-login</code> 打印的一次性码。</p>
27
+ <p class="hint">粘贴主机令牌(<code>pi-hub token</code> 可查看),或输入主机上 <code>pi-hub web</code> 打印的一次性码。</p>
28
+ <p class="hint small">连不上?看<a href="/tutorial.html" target="_blank" rel="noopener">监听地址 / 端口 / 配对教程</a>(本机也能 <code>pi-hub tutorial</code> 打开)。</p>
28
29
  <label class="field">
29
30
  <span>令牌</span>
30
31
  <input id="login-token" type="password" inputmode="text" autocomplete="off" spellcheck="false" placeholder="bearer token" required />
@@ -51,6 +52,7 @@
51
52
  <span id="session-sub" class="sub"></span>
52
53
  </div>
53
54
  <span id="conn-dot" class="dot off" title="未连接"></span>
55
+ <button id="btn-new" class="icon-btn" type="button" aria-label="新建会话" title="新建会话">+</button>
54
56
  <button id="btn-menu" class="icon-btn" type="button" aria-label="更多">⋯</button>
55
57
  </header>
56
58
 
@@ -63,14 +65,20 @@
63
65
  <button id="btn-logout" type="button" class="danger">退出登录</button>
64
66
  </div>
65
67
 
68
+ <!-- 常驻错误横幅(P1-9):不自动消失、可手动关闭;贴在顶栏下方,不遮输入框 -->
69
+ <div id="error-banner" class="error-banner" role="alert" hidden>
70
+ <span id="error-banner-text"></span>
71
+ <button id="error-banner-close" type="button" aria-label="关闭错误提示" title="关闭">✕</button>
72
+ </div>
73
+
66
74
  <main id="messages" class="messages" tabindex="0"></main>
67
75
 
68
76
  <button id="btn-jump" class="jump" type="button" hidden>↓ 最新</button>
69
77
 
70
78
  <footer class="composer">
71
79
  <div id="usage-bar" class="usage-bar" hidden>
72
- <span id="usage-context"></span>
73
- <span id="usage-cost"></span>
80
+ <span id="usage-full"></span>
81
+ <span id="usage-narrow"></span>
74
82
  </div>
75
83
  <div id="attachments" class="attachments" hidden></div>
76
84
  <div id="ui-requests" class="ui-requests" hidden></div>
@@ -93,6 +101,7 @@
93
101
  <input id="doc-input" type="file" hidden />
94
102
  <span>📎</span>
95
103
  </label>
104
+ <!-- 发送键永远在(F9):执行中也照样发,服务端自己排队 follow-up;停止键只在执行中出现 -->
96
105
  <button id="btn-send" class="btn primary send" type="button">发送</button>
97
106
  <button id="btn-stop" class="btn danger send" type="button" hidden>停止</button>
98
107
  </div>
@@ -101,30 +110,52 @@
101
110
 
102
111
  <!-- 会话抽屉:搜索 / 分组 / 新建 -->
103
112
  <aside id="drawer" class="drawer" hidden>
113
+ <!-- 桌面折叠态(W1-c):56px 图标栏,展开态的抽屉内容整体收起。
114
+ 窄屏永远看不到它(抽屉语义不变)。 -->
115
+ <div class="rail">
116
+ <span class="rail-logo" aria-label="pi hub" title="pi hub">π</span>
117
+ <button id="btn-rail-new" class="rail-btn" type="button" aria-label="新建会话" title="新建会话">+</button>
118
+ <button id="btn-rail-search" class="rail-btn" type="button" aria-label="搜索会话" title="搜索会话">🔍</button>
119
+ <button id="btn-rail-more" class="rail-btn" type="button" aria-label="更多" title="更多">⋯</button>
120
+ <button id="btn-rail-expand" class="rail-btn" type="button" aria-label="展开侧栏" aria-expanded="true" title="展开侧栏">›</button>
121
+ </div>
104
122
  <div class="drawer-head">
105
123
  <input id="session-search" type="search" placeholder="搜索会话…" spellcheck="false" />
106
- <button id="btn-drawer-close" class="icon-btn" type="button" aria-label="关闭">✕</button>
124
+ <!-- 收起侧栏(R8-F2):图标与折叠态图标栏的「展开侧栏」› 对称成 ‹,别再用「✕」(像关闭窗口) -->
125
+ <button id="btn-drawer-close" class="icon-btn" type="button" aria-label="收起侧栏" title="收起侧栏">‹</button>
107
126
  </div>
108
127
  <div id="session-list" class="session-list"></div>
109
- <form id="new-session" class="new-session" autocomplete="off">
110
- <div class="drawer-head">
111
- <strong>新建会话</strong>
112
- </div>
113
- <label class="field">
114
- <span>工作区 cwd</span>
115
- <input id="new-cwd" list="cwd-options" placeholder="/home/me/project" spellcheck="false" required />
116
- <datalist id="cwd-options"></datalist>
117
- </label>
118
- <label class="field">
119
- <span>模型(可选)</span>
120
- <select id="new-model"><option value="">默认</option></select>
121
- </label>
122
- <button class="btn primary" type="submit">开始</button>
123
- <p id="new-error" class="error" hidden></p>
124
- </form>
128
+ <!-- 新建会话默认收起:常驻展开的表单会把历史列表挤掉一大截(窄屏尤其明显)。
129
+ 顶栏 + 与每个仓库分组右侧的 + 都会展开它并预填 cwd。 -->
130
+ <div class="drawer-foot">
131
+ <!-- 展开/收起线索(R8-F2):▸ 收起 ↔ ▾ 展开,两态由 aria-expanded 驱动(CSS 只认这一个开关) -->
132
+ <button id="btn-new-session-toggle" class="new-session-toggle" type="button" aria-expanded="false" aria-controls="new-session">
133
+ <span class="caret" aria-hidden="true"></span><span>新建会话</span>
134
+ </button>
135
+ <form id="new-session" class="new-session" autocomplete="off" hidden>
136
+ <label class="field">
137
+ <span>工作区 cwd</span>
138
+ <input id="new-cwd" list="cwd-options" placeholder="/home/me/project" spellcheck="false" required />
139
+ <datalist id="cwd-options"></datalist>
140
+ </label>
141
+ <label class="field">
142
+ <span>模型(可选)</span>
143
+ <select id="new-model"><option value="">默认</option></select>
144
+ </label>
145
+ <button class="btn primary" type="submit">开始</button>
146
+ <p id="new-error" class="error" hidden></p>
147
+ </form>
148
+ </div>
125
149
  </aside>
126
150
  <div id="scrim" class="scrim" hidden></div>
127
151
 
152
+ <!-- 工作区组头 ⋮ 的菜单(P1-11):同一套 .menu 样式 + 同一个 openMenuAt(anchor) 定位器。
153
+ 必须在 `.app` 外:`.app` 是 position:fixed 栈上下文,放里面会被抽屉(z-index:40)盖住。 -->
154
+ <div id="group-menu" class="menu" hidden>
155
+ <button id="btn-group-all" type="button">查看全部会话</button>
156
+ <button id="btn-group-toggle" type="button">折叠该组</button>
157
+ </div>
158
+
128
159
  <div id="toast" class="toast" hidden></div>
129
160
  <script type="module" src="/app.js"></script>
130
161
  </body>
@@ -0,0 +1,182 @@
1
+ // 网页端的轻量 markdown 渲染器:先转义再拼标签,代码块整体转义(无第三方依赖、无 DOM 依赖)。
2
+ // 单独成文件是为了能在 node 里直接 import 单测(`test/markdown.mjs`),app.js 只负责挂载。
3
+ "use strict";
4
+
5
+ const HTML_ESCAPES = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
6
+
7
+ export function escapeHtml(value) {
8
+ return String(value ?? "").replace(/[&<>"']/g, (c) => HTML_ESCAPES[c]);
9
+ }
10
+
11
+ function safeHref(href) {
12
+ const raw = href.replace(/&amp;/g, "&");
13
+ if (/^https?:\/\//i.test(raw) || /^mailto:/i.test(raw) || raw.startsWith("/")) return true;
14
+ return false;
15
+ }
16
+
17
+ /** 输入必须是已转义文本,输出只含我们自己生成的标签。 */
18
+ export function inlineMarkdown(escaped) {
19
+ let out = escaped;
20
+ out = out.replace(/`([^`\n]+)`/g, "<code>$1</code>");
21
+ out = out.replace(/\*\*([^*\n]+)\*\*/g, "<strong>$1</strong>");
22
+ out = out.replace(/(^|[\s(])\*([^*\s][^*\n]*)\*/g, "$1<em>$2</em>");
23
+ out = out.replace(/~~([^~\n]+)~~/g, "<del>$1</del>");
24
+ out = out.replace(/\[([^\]\n]+)\]\(([^)\s]+)\)/g, (match, label, href) =>
25
+ safeHref(href) ? `<a href="${href}" target="_blank" rel="noopener noreferrer">${label}</a>` : label,
26
+ );
27
+ return out;
28
+ }
29
+
30
+ export function isTableSeparator(line) {
31
+ return /^\s*\|?[\s:-]*-[\s|:-]*$/.test(line) && line.includes("-");
32
+ }
33
+
34
+ /** 拆一行表格:去掉首尾竖线后按 `|` 切(`\|` 是转义竖线,留在同一格内)。 */
35
+ function splitRow(row) {
36
+ return row
37
+ .replace(/^\s*\|/, "")
38
+ .replace(/\|\s*$/, "")
39
+ .split(/(?<!\\)\|/)
40
+ .map((c) => c.trim().replace(/\\\|/g, "|"));
41
+ }
42
+
43
+ // 左对齐是 CSS 默认,不写 class(HTML 更好读、少一半噪音)
44
+ const ALIGN_CLASS = { center: "al-c", right: "al-r" };
45
+
46
+ /** 分隔行 → 每列对齐(`---` 左、`:---:` 中、`---:` 右;缺省不写 class)。 */
47
+ function tableAligns(separator) {
48
+ return splitRow(separator).map((cell) => {
49
+ const t = cell.replace(/\s/g, "");
50
+ const left = t.startsWith(":");
51
+ const right = t.endsWith(":");
52
+ if (left && right) return "center";
53
+ if (right) return "right";
54
+ return "left";
55
+ });
56
+ }
57
+
58
+ const alignClass = (align) => (ALIGN_CLASS[align] ? ` class="${ALIGN_CLASS[align]}"` : "");
59
+
60
+ /**
61
+ * 渲染 markdown 为 HTML 字符串。
62
+ * @param {string} source
63
+ * @returns {string}
64
+ */
65
+ export function renderMarkdown(source) {
66
+ const lines = String(source ?? "").replace(/\r\n?/g, "\n").split("\n");
67
+ const out = [];
68
+ let para = [];
69
+ const flush = () => {
70
+ if (para.length) {
71
+ out.push(`<p>${para.map((l) => inlineMarkdown(escapeHtml(l))).join("<br>")}</p>`);
72
+ para = [];
73
+ }
74
+ };
75
+
76
+ let i = 0;
77
+ while (i < lines.length) {
78
+ const line = lines[i];
79
+
80
+ // 代码块:内容整体转义,语言标记只用于角标
81
+ const fence = /^\s{0,3}(`{3,}|~{3,})\s*([\w+-]*)\s*$/.exec(line);
82
+ if (fence) {
83
+ flush();
84
+ const marker = fence[1][0];
85
+ const lang = fence[2] || "";
86
+ const body = [];
87
+ i += 1;
88
+ while (i < lines.length) {
89
+ const closing = new RegExp(`^\\s{0,3}${marker}{${fence[1].length},}\\s*$`);
90
+ if (closing.test(lines[i])) {
91
+ i += 1;
92
+ break;
93
+ }
94
+ body.push(lines[i]);
95
+ i += 1;
96
+ }
97
+ out.push(
98
+ `<div class="code-card"><div class="code-head"><span>${escapeHtml(lang || "code")}</span>` +
99
+ `<button type="button" class="copy" data-copy>复制</button></div>` +
100
+ `<pre><code>${escapeHtml(body.join("\n"))}</code></pre></div>`,
101
+ );
102
+ continue;
103
+ }
104
+
105
+ if (/^\s*$/.test(line)) {
106
+ flush();
107
+ i += 1;
108
+ continue;
109
+ }
110
+
111
+ const heading = /^\s{0,3}(#{1,6})\s+(.*)$/.exec(line);
112
+ if (heading) {
113
+ flush();
114
+ const level = Math.min(heading[1].length, 4);
115
+ out.push(`<h${level}>${inlineMarkdown(escapeHtml(heading[2]))}</h${level}>`);
116
+ i += 1;
117
+ continue;
118
+ }
119
+
120
+ if (/^\s{0,3}([-*_])(\s*\1){2,}\s*$/.test(line)) {
121
+ flush();
122
+ out.push("<hr />");
123
+ i += 1;
124
+ continue;
125
+ }
126
+
127
+ const quote = /^\s{0,3}>\s?(.*)$/.exec(line);
128
+ if (quote) {
129
+ flush();
130
+ const body = [];
131
+ while (i < lines.length) {
132
+ const q = /^\s{0,3}>\s?(.*)$/.exec(lines[i]);
133
+ if (!q) break;
134
+ body.push(q[1]);
135
+ i += 1;
136
+ }
137
+ out.push(`<blockquote>${inlineMarkdown(escapeHtml(body.join("\n")))}</blockquote>`);
138
+ continue;
139
+ }
140
+
141
+ const bullet = /^\s{0,3}([-*+])\s+(.*)$/.exec(line);
142
+ const numbered = /^\s{0,3}(\d{1,9})[.)]\s+(.*)$/.exec(line);
143
+ if (bullet || numbered) {
144
+ flush();
145
+ const ordered = Boolean(numbered);
146
+ const items = [];
147
+ while (i < lines.length) {
148
+ const b = ordered ? /^\s{0,3}(\d{1,9})[.)]\s+(.*)$/.exec(lines[i]) : /^\s{0,3}([-*+])\s+(.*)$/.exec(lines[i]);
149
+ if (!b) break;
150
+ items.push(`<li>${inlineMarkdown(escapeHtml(b[2]))}</li>`);
151
+ i += 1;
152
+ }
153
+ out.push(ordered ? `<ol>${items.join("")}</ol>` : `<ul>${items.join("")}</ul>`);
154
+ continue;
155
+ }
156
+
157
+ // 表格:表头 + 分隔行才认,否则按普通段落
158
+ if (line.includes("|") && i + 1 < lines.length && isTableSeparator(lines[i + 1])) {
159
+ flush();
160
+ const aligns = tableAligns(lines[i + 1]);
161
+ const cell = (tag, text, index) => `<${tag}${alignClass(aligns[index])}>${inlineMarkdown(escapeHtml(text))}</${tag}>`;
162
+ const head = splitRow(line);
163
+ i += 2;
164
+ const rows = [];
165
+ while (i < lines.length && lines[i].includes("|") && !/^\s*$/.test(lines[i])) {
166
+ rows.push(`<tr>${splitRow(lines[i]).map((c, n) => cell("td", c, n)).join("")}</tr>`);
167
+ i += 1;
168
+ }
169
+ // 宽表格(多列/长内容)靠外层 .table-wrap 横向滚动,不撑破消息气泡
170
+ const headHtml = head.map((c, n) => cell("th", c, n)).join("");
171
+ out.push(
172
+ `<div class="table-wrap"><table><thead><tr>${headHtml}</tr></thead><tbody>${rows.join("")}</tbody></table></div>`,
173
+ );
174
+ continue;
175
+ }
176
+
177
+ para.push(line);
178
+ i += 1;
179
+ }
180
+ flush();
181
+ return out.join("");
182
+ }