hob-bot 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +111 -2
- package/README.zh-CN.md +112 -0
- package/bin/hob.mjs +7 -0
- package/dist/web/assets/index-D88l35PH.css +1 -0
- package/dist/web/assets/index-DUs8nN3E.js +218 -0
- package/dist/web/favicon.ico +0 -0
- package/dist/web/fonts/inter-latin.woff2 +0 -0
- package/dist/web/fonts/jetbrains-mono-latin.woff2 +0 -0
- package/dist/web/icons/apple-touch-icon-180.png +0 -0
- package/dist/web/icons/favicon-32.png +0 -0
- package/dist/web/icons/icon-192.png +0 -0
- package/dist/web/icons/icon-512.png +0 -0
- package/dist/web/icons/icon-maskable-512.png +0 -0
- package/dist/web/index.html +32 -0
- package/dist/web/manifest.webmanifest +16 -0
- package/dist/web/sw.js +131 -0
- package/package.json +62 -4
- package/server/src/actions.ts +230 -0
- package/server/src/boot.ts +85 -0
- package/server/src/bus.ts +21 -0
- package/server/src/config.ts +123 -0
- package/server/src/cron-text.ts +49 -0
- package/server/src/delegations.ts +113 -0
- package/server/src/files.ts +142 -0
- package/server/src/http.ts +802 -0
- package/server/src/ids.ts +19 -0
- package/server/src/image.ts +218 -0
- package/server/src/imports.ts +302 -0
- package/server/src/jobs.ts +730 -0
- package/server/src/main.ts +69 -0
- package/server/src/memory.ts +272 -0
- package/server/src/models.ts +358 -0
- package/server/src/overview.ts +116 -0
- package/server/src/pages.ts +54 -0
- package/server/src/password.ts +23 -0
- package/server/src/pi.ts +273 -0
- package/server/src/prompt.ts +227 -0
- package/server/src/providers.ts +532 -0
- package/server/src/push.ts +285 -0
- package/server/src/router-tools.ts +549 -0
- package/server/src/router.ts +841 -0
- package/server/src/routines.ts +129 -0
- package/server/src/runtime.ts +104 -0
- package/server/src/search.ts +91 -0
- package/server/src/secrets.ts +46 -0
- package/server/src/settings.ts +452 -0
- package/server/src/skills.ts +150 -0
- package/server/src/store.ts +324 -0
- package/server/src/text.ts +14 -0
- package/server/src/time.ts +42 -0
- package/server/src/transcript.ts +272 -0
- package/server/src/worker-tools.ts +129 -0
- package/shared/command.ts +42 -0
- package/shared/files.ts +141 -0
- package/shared/media.ts +17 -0
- package/shared/model-note.ts +32 -0
- package/shared/page-shell.ts +108 -0
- package/shared/search.ts +19 -0
- package/shared/skills.ts +37 -0
- package/shared/sprites.ts +422 -0
- package/shared/thinking.ts +118 -0
- package/shared/types.ts +631 -0
- package/vendor/lore-pi-extension/README.md +28 -0
- package/vendor/lore-pi-extension/api.ts +140 -0
- package/vendor/lore-pi-extension/formatters.ts +204 -0
- package/vendor/lore-pi-extension/hooks.ts +195 -0
- package/vendor/lore-pi-extension/index.ts +25 -0
- package/vendor/lore-pi-extension/package.json +23 -0
- package/vendor/lore-pi-extension/skills.ts +447 -0
- package/vendor/lore-pi-extension/tools.ts +344 -0
- package/vendor/lore-pi-extension/uri.ts +67 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/index.d.mts +266 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/index.mjs +1328 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/skills-sync.d.mts +33 -0
- package/vendor/lore-pi-extension/vendor/skill-workcopy/skills-sync.mjs +202 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// HOB_HOME/push.json:VAPID 密钥 + 订阅设备(0600、原子写)。推送走 web-push。
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import type { Approval, Job, PushDevice, PushSubscriptionJson, PushView } from "../../shared/types.ts";
|
|
6
|
+
import type { Store } from "./store.ts";
|
|
7
|
+
|
|
8
|
+
const requirePush = createRequire(import.meta.url);
|
|
9
|
+
const webpush = requirePush("web-push") as typeof import("web-push");
|
|
10
|
+
|
|
11
|
+
const FILE = "push.json";
|
|
12
|
+
|
|
13
|
+
/** 一条订阅在磁盘上的样子(密钥只在这里)。 */
|
|
14
|
+
interface DeviceRecord extends PushDevice {
|
|
15
|
+
p256dh: string;
|
|
16
|
+
auth: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface VapidKeys {
|
|
20
|
+
publicKey: string;
|
|
21
|
+
privateKey: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface PushFile {
|
|
25
|
+
vapid: VapidKeys;
|
|
26
|
+
devices: DeviceRecord[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 发给浏览器的那条通知(sw.js 的 push 事件照这个解)。 */
|
|
30
|
+
export interface PushPayload {
|
|
31
|
+
title: string;
|
|
32
|
+
body: string;
|
|
33
|
+
url: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PushTarget {
|
|
37
|
+
endpoint: string;
|
|
38
|
+
p256dh: string;
|
|
39
|
+
auth: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 真正发出去的一步;测试注入假的,不碰网络。 */
|
|
43
|
+
export type PushSender = (
|
|
44
|
+
target: PushTarget,
|
|
45
|
+
payload: string,
|
|
46
|
+
vapid: VapidKeys & { subject: string },
|
|
47
|
+
) => Promise<void>;
|
|
48
|
+
|
|
49
|
+
const webPushSender: PushSender = async (target, payload, vapid) => {
|
|
50
|
+
await webpush.sendNotification(
|
|
51
|
+
{ endpoint: target.endpoint, keys: { p256dh: target.p256dh, auth: target.auth } },
|
|
52
|
+
payload,
|
|
53
|
+
{ vapidDetails: { subject: vapid.subject, publicKey: vapid.publicKey, privateKey: vapid.privateKey } },
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export interface PushResult {
|
|
58
|
+
sent: number;
|
|
59
|
+
removed: number; // 订阅已失效(404/410)被删掉的条数
|
|
60
|
+
skipped?: "visible" | "no-devices"; // 没发的原因
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface PushServiceOptions {
|
|
64
|
+
home: string;
|
|
65
|
+
/** 设置 → 通知里填的邮箱(VAPID subject 用) */
|
|
66
|
+
email: () => string;
|
|
67
|
+
sender?: PushSender;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isVapid(value: unknown): value is VapidKeys {
|
|
71
|
+
const keys = value as Partial<VapidKeys> | undefined;
|
|
72
|
+
return typeof keys?.publicKey === "string" && !!keys.publicKey && typeof keys.privateKey === "string" && !!keys.privateKey;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isDevice(value: unknown): value is DeviceRecord {
|
|
76
|
+
const device = value as Partial<DeviceRecord> | undefined;
|
|
77
|
+
return (
|
|
78
|
+
typeof device?.endpoint === "string" &&
|
|
79
|
+
!!device.endpoint &&
|
|
80
|
+
typeof device.p256dh === "string" &&
|
|
81
|
+
typeof device.auth === "string" &&
|
|
82
|
+
typeof device.label === "string" &&
|
|
83
|
+
typeof device.createdAt === "string"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function statusOf(error: unknown): number | undefined {
|
|
88
|
+
const status = (error as { statusCode?: unknown })?.statusCode;
|
|
89
|
+
return typeof status === "number" ? status : undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 推送服务(可见性只在内存里:进程重启后没有前台客户端)。 */
|
|
93
|
+
export class PushService {
|
|
94
|
+
private data?: PushFile;
|
|
95
|
+
private readonly sender: PushSender;
|
|
96
|
+
/** 每个页面客户端的可见性;SSE 连接断了就删掉这一项 */
|
|
97
|
+
private readonly visible = new Map<string, boolean>();
|
|
98
|
+
/** clientId → 还连着几条 SSE(重连时旧的断开不会把新的状态清掉) */
|
|
99
|
+
private readonly clients = new Map<string, number>();
|
|
100
|
+
|
|
101
|
+
constructor(private readonly options: PushServiceOptions) {
|
|
102
|
+
this.sender = options.sender ?? webPushSender;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private file(): string {
|
|
106
|
+
return join(this.options.home, FILE);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 读磁盘;文件没有就生成 VAPID 密钥,坏的订阅丢掉。 */
|
|
110
|
+
private load(): PushFile {
|
|
111
|
+
if (this.data) return this.data;
|
|
112
|
+
let stored: Partial<PushFile> = {};
|
|
113
|
+
if (existsSync(this.file())) {
|
|
114
|
+
try {
|
|
115
|
+
stored = JSON.parse(readFileSync(this.file(), "utf8")) as Partial<PushFile>;
|
|
116
|
+
} catch {
|
|
117
|
+
stored = {};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const vapid = isVapid(stored.vapid) ? stored.vapid : webpush.generateVAPIDKeys();
|
|
121
|
+
const devices = Array.isArray(stored.devices) ? stored.devices.filter(isDevice) : [];
|
|
122
|
+
this.data = { vapid, devices };
|
|
123
|
+
this.write();
|
|
124
|
+
return this.data;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private write(): void {
|
|
128
|
+
const path = this.file();
|
|
129
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
130
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
131
|
+
writeFileSync(tmp, `${JSON.stringify(this.data, null, 2)}\n`, { mode: 0o600 });
|
|
132
|
+
chmodSync(tmp, 0o600);
|
|
133
|
+
renameSync(tmp, path);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private subject(): string {
|
|
137
|
+
const email = this.options.email().trim();
|
|
138
|
+
return `mailto:${email || "hob@localhost"}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
view(): PushView {
|
|
142
|
+
const file = this.load();
|
|
143
|
+
return {
|
|
144
|
+
publicKey: file.vapid.publicKey,
|
|
145
|
+
email: this.options.email().trim(),
|
|
146
|
+
devices: file.devices.map(({ endpoint, label, createdAt }) => ({ endpoint, label, createdAt })).reverse(),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
subscribe(subscription: PushSubscriptionJson, label: string): PushDevice {
|
|
151
|
+
const endpoint = typeof subscription?.endpoint === "string" ? subscription.endpoint.trim() : "";
|
|
152
|
+
const p256dh = typeof subscription?.keys?.p256dh === "string" ? subscription.keys.p256dh : "";
|
|
153
|
+
const auth = typeof subscription?.keys?.auth === "string" ? subscription.keys.auth : "";
|
|
154
|
+
if (!endpoint || !p256dh || !auth) throw new Error("订阅信息不完整:缺 endpoint 或 keys");
|
|
155
|
+
const file = this.load();
|
|
156
|
+
const name = label.trim().slice(0, 40) || "本机";
|
|
157
|
+
const existing = file.devices.find((device) => device.endpoint === endpoint);
|
|
158
|
+
const device: DeviceRecord = {
|
|
159
|
+
endpoint,
|
|
160
|
+
p256dh,
|
|
161
|
+
auth,
|
|
162
|
+
label: name,
|
|
163
|
+
// 同一台重新订阅:保留第一次的时间,只更新名称和密钥
|
|
164
|
+
createdAt: existing?.createdAt ?? new Date().toISOString(),
|
|
165
|
+
};
|
|
166
|
+
file.devices = [...file.devices.filter((item) => item.endpoint !== endpoint), device];
|
|
167
|
+
this.write();
|
|
168
|
+
return { endpoint: device.endpoint, label: device.label, createdAt: device.createdAt };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
unsubscribe(endpoint: string): boolean {
|
|
172
|
+
const file = this.load();
|
|
173
|
+
const before = file.devices.length;
|
|
174
|
+
file.devices = file.devices.filter((device) => device.endpoint !== endpoint);
|
|
175
|
+
if (file.devices.length === before) return false;
|
|
176
|
+
this.write();
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** 有推送权限的页面客户端的可见性(页面自己报,只存内存)。 */
|
|
181
|
+
setVisibility(clientId: string, visible: boolean): void {
|
|
182
|
+
if (!clientId) return;
|
|
183
|
+
this.visible.set(clientId, visible);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** SSE 连上:记一条连接,断开时把可见性一起清掉(没法确认还在看就是不在看)。 */
|
|
187
|
+
attachClient(clientId: string): () => void {
|
|
188
|
+
if (!clientId) return () => undefined;
|
|
189
|
+
this.clients.set(clientId, (this.clients.get(clientId) ?? 0) + 1);
|
|
190
|
+
return () => {
|
|
191
|
+
const left = (this.clients.get(clientId) ?? 1) - 1;
|
|
192
|
+
if (left > 0) {
|
|
193
|
+
this.clients.set(clientId, left);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
this.clients.delete(clientId);
|
|
197
|
+
this.visible.delete(clientId);
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 只要有一个页面在看着就不推:通知是给不在电脑前的人看的。 */
|
|
202
|
+
hasVisible(): boolean {
|
|
203
|
+
for (const visible of this.visible.values()) if (visible) return true;
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async deliver(devices: DeviceRecord[], payload: PushPayload): Promise<PushResult> {
|
|
208
|
+
const file = this.load();
|
|
209
|
+
const body = JSON.stringify(payload);
|
|
210
|
+
const vapid = { ...file.vapid, subject: this.subject() };
|
|
211
|
+
const gone: string[] = [];
|
|
212
|
+
let sent = 0;
|
|
213
|
+
await Promise.all(
|
|
214
|
+
devices.map(async (device) => {
|
|
215
|
+
try {
|
|
216
|
+
await this.sender(
|
|
217
|
+
{ endpoint: device.endpoint, p256dh: device.p256dh, auth: device.auth },
|
|
218
|
+
body,
|
|
219
|
+
vapid,
|
|
220
|
+
);
|
|
221
|
+
sent += 1;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
// 404/410 = 订阅没了(卸载了 PWA、清过数据):删掉,别一直发空
|
|
224
|
+
const status = statusOf(error);
|
|
225
|
+
if (status === 404 || status === 410) gone.push(device.endpoint);
|
|
226
|
+
}
|
|
227
|
+
}),
|
|
228
|
+
);
|
|
229
|
+
if (gone.length) {
|
|
230
|
+
file.devices = file.devices.filter((device) => !gone.includes(device.endpoint));
|
|
231
|
+
this.write();
|
|
232
|
+
}
|
|
233
|
+
return { sent, removed: gone.length };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** 该推的时候推:有可见的客户端、或一台设备都没订阅就不发。 */
|
|
237
|
+
async send(payload: PushPayload): Promise<PushResult> {
|
|
238
|
+
if (this.hasVisible()) return { sent: 0, removed: 0, skipped: "visible" };
|
|
239
|
+
const devices = this.load().devices;
|
|
240
|
+
if (!devices.length) return { sent: 0, removed: 0, skipped: "no-devices" };
|
|
241
|
+
return this.deliver(devices, payload);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** 设置页的「发一条测试」:不看可见性,只发指定设备(不给就发全部)。 */
|
|
245
|
+
async test(endpoint?: string): Promise<PushResult> {
|
|
246
|
+
const devices = this.load().devices;
|
|
247
|
+
const targets = endpoint ? devices.filter((device) => device.endpoint === endpoint) : devices;
|
|
248
|
+
if (!targets.length) return { sent: 0, removed: 0, skipped: "no-devices" };
|
|
249
|
+
return this.deliver(targets, { title: "Hob", body: "测试通知:收到此消息说明推送功能正常。", url: "/" });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 该推送的子集:只用到这两个方法(测试可以直接给假对象)。 */
|
|
254
|
+
export interface PushSink {
|
|
255
|
+
send(payload: PushPayload): Promise<PushResult>;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface NotifyDeps {
|
|
259
|
+
push: PushSink;
|
|
260
|
+
store: Store;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function openUrl(assistantId: string): string {
|
|
264
|
+
return `/?a=${encodeURIComponent(assistantId)}`;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** 出审批:这个助手开了「需要确认时通知我」才推送。 */
|
|
268
|
+
export function pushApproval(deps: NotifyDeps, approval: Approval): void {
|
|
269
|
+
const assistant = deps.store.assistant(approval.assistantId);
|
|
270
|
+
if (!assistant?.notify) return;
|
|
271
|
+
// 推送失败不能影响审批本身(也不能变成未处理的 rejection 把进程带走)
|
|
272
|
+
void deps.push.send({ title: assistant.name, body: approval.title, url: openUrl(approval.assistantId) }).catch(() => undefined);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** 任务受阻或失败:所属助手开了通知才推送。 */
|
|
276
|
+
export function pushJobEnd(deps: NotifyDeps, job: Job): void {
|
|
277
|
+
if (job.status !== "stuck" && job.status !== "failed") return;
|
|
278
|
+
const assistant = deps.store.assistant(job.assistantId);
|
|
279
|
+
if (!assistant?.notify) return;
|
|
280
|
+
const what = job.status === "stuck" ? "受阻" : "失败";
|
|
281
|
+
const reason = job.statusReason ? `:${job.statusReason}` : "";
|
|
282
|
+
void deps.push
|
|
283
|
+
.send({ title: assistant.name, body: `任务「${job.title}」${what}${reason}`, url: openUrl(job.assistantId) })
|
|
284
|
+
.catch(() => undefined);
|
|
285
|
+
}
|