@nanhara/hara 0.121.1 → 0.122.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/CHANGELOG.md +66 -0
- package/README.md +40 -6
- package/dist/agent/loop.js +136 -21
- package/dist/agent/reminders.js +22 -7
- package/dist/agent/repeat-guard.js +26 -7
- package/dist/agent/structured.js +231 -0
- package/dist/agent/touched.js +20 -6
- package/dist/config.js +33 -23
- package/dist/cron/deliver.js +37 -3
- package/dist/feedback.js +3 -2
- package/dist/fs-read.js +106 -12
- package/dist/fs-write.js +242 -16
- package/dist/gateway/dingtalk.js +4 -1
- package/dist/gateway/discord.js +53 -18
- package/dist/gateway/feishu.js +158 -57
- package/dist/gateway/flows-pending.js +720 -0
- package/dist/gateway/flows.js +391 -16
- package/dist/gateway/matrix.js +80 -15
- package/dist/gateway/mattermost.js +44 -32
- package/dist/gateway/media.js +659 -0
- package/dist/gateway/serve.js +657 -162
- package/dist/gateway/sessions.js +475 -78
- package/dist/gateway/signal.js +27 -22
- package/dist/gateway/slack.js +26 -17
- package/dist/gateway/telegram.js +32 -18
- package/dist/gateway/wecom.js +32 -24
- package/dist/gateway/weixin.js +127 -49
- package/dist/hooks.js +32 -20
- package/dist/index.js +631 -222
- package/dist/org/projects.js +347 -0
- package/dist/org/roles.js +38 -11
- package/dist/security/secrets.js +84 -9
- package/dist/serve/server.js +772 -317
- package/dist/serve/sessions.js +113 -33
- package/dist/session/store.js +298 -47
- package/dist/tools/all.js +1 -0
- package/dist/tools/builtin.js +30 -28
- package/dist/tools/codebase.js +3 -1
- package/dist/tools/computer.js +98 -92
- package/dist/tools/edit.js +11 -8
- package/dist/tools/patch.js +230 -31
- package/dist/tools/search.js +482 -72
- package/dist/tools/task.js +453 -0
- package/dist/tools/todo.js +67 -16
- package/dist/tools/web.js +168 -54
- package/dist/undo.js +83 -7
- package/package.json +1 -1
package/dist/fs-write.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
// Crash-safe UTF-8 writes for coding tools. Content is staged beside the destination, fsynced, then
|
|
2
2
|
// renamed into place so a killed process never leaves a half-written source file.
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { constants, linkSync, lstatSync, readlinkSync, renameSync, symlinkSync, unlinkSync } from "node:fs";
|
|
6
|
+
import { link, lstat, mkdir, open, realpath, rename, rmdir, stat, unlink } from "node:fs/promises";
|
|
7
|
+
import { NonRegularFileError, readRegularFileSnapshotNoFollow } from "./fs-read.js";
|
|
5
8
|
export class FileChangedError extends Error {
|
|
6
9
|
code = "HARA_FILE_CHANGED";
|
|
7
10
|
constructor(path) {
|
|
@@ -24,12 +27,139 @@ async function writeTarget(path) {
|
|
|
24
27
|
}
|
|
25
28
|
return path;
|
|
26
29
|
}
|
|
30
|
+
/** mkdir -p with ownership accounting: only a mkdir call that actually succeeded is recorded. */
|
|
31
|
+
async function ensureDirectory(path, created) {
|
|
32
|
+
try {
|
|
33
|
+
const info = await stat(path);
|
|
34
|
+
if (!info.isDirectory())
|
|
35
|
+
throw new Error(`${path} is not a directory`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error?.code !== "ENOENT")
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
const parent = dirname(path);
|
|
43
|
+
if (parent === path)
|
|
44
|
+
throw new Error(`cannot create directory ${path}`);
|
|
45
|
+
await ensureDirectory(parent, created);
|
|
46
|
+
try {
|
|
47
|
+
await mkdir(path);
|
|
48
|
+
const info = await lstat(path);
|
|
49
|
+
if (!info.isDirectory())
|
|
50
|
+
throw new Error(`${path} changed while it was being created`);
|
|
51
|
+
// Store the canonical name now. Keeping a lexical path beneath a symlink would make undo look in a
|
|
52
|
+
// different tree after that parent link is retargeted, silently leaving directories we created behind.
|
|
53
|
+
const canonical = await realpath(path);
|
|
54
|
+
const canonicalInfo = await lstat(canonical);
|
|
55
|
+
if (!canonicalInfo.isDirectory() || canonicalInfo.dev !== info.dev || canonicalInfo.ino !== info.ino) {
|
|
56
|
+
throw new FileChangedError(path);
|
|
57
|
+
}
|
|
58
|
+
created.push({ path: canonical, dev: info.dev, ino: info.ino, mode: info.mode & 0o777 });
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error?.code !== "EEXIST")
|
|
62
|
+
throw error;
|
|
63
|
+
const info = await stat(path);
|
|
64
|
+
if (!info.isDirectory())
|
|
65
|
+
throw new Error(`${path} is not a directory`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Remove only directories this process actually created and that still name the same empty inode. */
|
|
69
|
+
export async function removeCreatedDirectories(created) {
|
|
70
|
+
for (let i = created.length - 1; i >= 0; i--) {
|
|
71
|
+
const expected = created[i];
|
|
72
|
+
let info;
|
|
73
|
+
try {
|
|
74
|
+
info = await lstat(expected.path);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (error?.code === "ENOENT")
|
|
78
|
+
continue;
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
if (!info.isDirectory() || info.dev !== expected.dev || info.ino !== expected.ino || (info.mode & 0o777) !== expected.mode) {
|
|
82
|
+
throw new FileChangedError(expected.path);
|
|
83
|
+
}
|
|
84
|
+
await rmdir(expected.path); // ENOTEMPTY is a safe refusal: concurrent content is never removed.
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Delete a previously verified quarantine entry without reopening the long verify→unlink race.
|
|
89
|
+
*
|
|
90
|
+
* The caller has already verified content through a no-follow descriptor. We atomically claim the current
|
|
91
|
+
* directory entry under one more unpredictable name, then perform the final no-follow identity check and
|
|
92
|
+
* unlink synchronously in the same JS turn. A same-process watcher therefore cannot replace the entry in
|
|
93
|
+
* the long asynchronous content-read window and have an unrelated inode unlinked. On any mismatch/failure,
|
|
94
|
+
* the claimed entry is deliberately retained and its exact recovery path is included in the error.
|
|
95
|
+
*/
|
|
96
|
+
export function discardClaimedPath(path, expected) {
|
|
97
|
+
const disposal = join(dirname(path), `.hara-discard-${process.pid}-${randomUUID()}.tmp`);
|
|
98
|
+
renameSync(path, disposal);
|
|
99
|
+
try {
|
|
100
|
+
const info = lstatSync(disposal);
|
|
101
|
+
const isLink = expected.linkTarget !== undefined;
|
|
102
|
+
const same = info.dev === expected.dev &&
|
|
103
|
+
info.ino === expected.ino &&
|
|
104
|
+
(info.mode & 0o777) === expected.mode &&
|
|
105
|
+
info.nlink === expected.nlink &&
|
|
106
|
+
info.isSymbolicLink() === isLink &&
|
|
107
|
+
(!isLink || readlinkSync(disposal) === expected.linkTarget);
|
|
108
|
+
if (!same)
|
|
109
|
+
throw new FileChangedError(path);
|
|
110
|
+
unlinkSync(disposal);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw new Error(`${error instanceof Error ? error.message : String(error)}; claimed entry is preserved at ${disposal}`, { cause: error });
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function sameClaimedPath(path, expected) {
|
|
117
|
+
const info = lstatSync(path);
|
|
118
|
+
const isLink = expected.linkTarget !== undefined;
|
|
119
|
+
return (info.dev === expected.dev &&
|
|
120
|
+
info.ino === expected.ino &&
|
|
121
|
+
(info.mode & 0o777) === expected.mode &&
|
|
122
|
+
info.nlink === expected.nlink &&
|
|
123
|
+
info.isSymbolicLink() === isLink &&
|
|
124
|
+
(!isLink || readlinkSync(path) === expected.linkTarget));
|
|
125
|
+
}
|
|
126
|
+
/** Restore a move-claimed entry without overwriting a path that appeared concurrently. */
|
|
127
|
+
function restoreClaimedPath(claimed, target, expected) {
|
|
128
|
+
if (!sameClaimedPath(claimed, expected)) {
|
|
129
|
+
throw new Error(`${new FileChangedError(target).message}; claimed entry is preserved at ${claimed}`);
|
|
130
|
+
}
|
|
131
|
+
const info = lstatSync(claimed);
|
|
132
|
+
if (info.isDirectory()) {
|
|
133
|
+
// POSIX has no portable rename-no-replace for directories. Retaining is safer than overwriting a new target.
|
|
134
|
+
throw new Error(`cannot safely restore a concurrently claimed directory; it is preserved at ${claimed}`);
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
if (expected.linkTarget !== undefined)
|
|
138
|
+
symlinkSync(expected.linkTarget, target);
|
|
139
|
+
else
|
|
140
|
+
linkSync(claimed, target);
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error?.code === "EEXIST") {
|
|
144
|
+
throw new Error(`another entry appeared at ${target}; the claimed entry is preserved at ${claimed}`);
|
|
145
|
+
}
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
discardClaimedPath(claimed, {
|
|
149
|
+
...expected,
|
|
150
|
+
nlink: expected.nlink + (expected.linkTarget === undefined ? 1 : 0),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
27
153
|
async function syncDirectory(path) {
|
|
28
154
|
// Directory fsync makes the rename durable across a power loss on POSIX. Some filesystems/platforms
|
|
29
155
|
// reject opening directories, so durability degrades gracefully after the file itself was synced.
|
|
30
156
|
try {
|
|
31
|
-
|
|
157
|
+
// The directory name can be exchanged after rename. O_NONBLOCK plus fstat on this exact descriptor
|
|
158
|
+
// keeps best-effort durability from hanging forever on a replacement FIFO/device.
|
|
159
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
32
160
|
try {
|
|
161
|
+
if (!(await handle.stat()).isDirectory())
|
|
162
|
+
return;
|
|
33
163
|
await handle.sync();
|
|
34
164
|
}
|
|
35
165
|
finally {
|
|
@@ -42,12 +172,27 @@ async function syncDirectory(path) {
|
|
|
42
172
|
}
|
|
43
173
|
/** Atomically replace/create a UTF-8 file, optionally refusing to overwrite a newer disk version. */
|
|
44
174
|
export async function atomicWriteText(path, content, options = {}) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
175
|
+
let target = await writeTarget(path);
|
|
176
|
+
let dir = dirname(target);
|
|
177
|
+
const createdDirs = [];
|
|
178
|
+
await ensureDirectory(dir, createdDirs);
|
|
179
|
+
// Canonicalize the parent too, not just a final-component symlink. A workspace may sit beneath a linked
|
|
180
|
+
// directory; returning/rolling back the canonical target prevents a concurrent parent retarget from sending
|
|
181
|
+
// later transaction steps to a different tree.
|
|
182
|
+
dir = await realpath(dir);
|
|
183
|
+
target = join(dir, basename(target));
|
|
184
|
+
// A caller-provided preflight identity is authoritative. Reading mode from a path before the move-claim
|
|
185
|
+
// would let a temporary same-path replacement influence the mode even when the expected inode is restored
|
|
186
|
+
// before claim verification.
|
|
187
|
+
let mode = options.mode === undefined ? (options.expectedIdentity?.mode ?? 0o666) : options.mode & 0o777;
|
|
188
|
+
let preserveExactMode = options.mode !== undefined || options.expectedIdentity !== undefined;
|
|
49
189
|
try {
|
|
50
|
-
|
|
190
|
+
const info = await stat(target);
|
|
191
|
+
if (!info.isFile())
|
|
192
|
+
throw new NonRegularFileError(path);
|
|
193
|
+
if (options.mode === undefined && !options.expectedIdentity)
|
|
194
|
+
mode = info.mode & 0o777;
|
|
195
|
+
preserveExactMode = true;
|
|
51
196
|
}
|
|
52
197
|
catch (error) {
|
|
53
198
|
if (error?.code !== "ENOENT")
|
|
@@ -57,12 +202,21 @@ export async function atomicWriteText(path, content, options = {}) {
|
|
|
57
202
|
// perfectly valid near-NAME_MAX file impossible to edit because the temporary name becomes longer.
|
|
58
203
|
const temp = join(dir, `.hara-${process.pid}-${Date.now().toString(36)}-${tempSequence++}.tmp`);
|
|
59
204
|
let staged = false;
|
|
205
|
+
let writtenIdentity;
|
|
206
|
+
const warnings = [];
|
|
207
|
+
let succeeded = false;
|
|
60
208
|
try {
|
|
61
209
|
const handle = await open(temp, "wx", mode);
|
|
62
210
|
staged = true;
|
|
63
211
|
try {
|
|
64
212
|
await handle.writeFile(content, "utf8");
|
|
213
|
+
// open(2) applies the process umask even when replacing an existing file. Restore that existing (or
|
|
214
|
+
// explicit rollback) mode on the staged fd; brand-new ordinary files still honor the user's umask.
|
|
215
|
+
if (preserveExactMode)
|
|
216
|
+
await handle.chmod(mode);
|
|
65
217
|
await handle.sync();
|
|
218
|
+
const info = await handle.stat();
|
|
219
|
+
writtenIdentity = { dev: info.dev, ino: info.ino, mode: info.mode & 0o777, nlink: info.nlink };
|
|
66
220
|
}
|
|
67
221
|
finally {
|
|
68
222
|
await handle.close();
|
|
@@ -81,25 +235,97 @@ export async function atomicWriteText(path, content, options = {}) {
|
|
|
81
235
|
await unlink(temp);
|
|
82
236
|
staged = false;
|
|
83
237
|
}
|
|
84
|
-
else {
|
|
85
|
-
|
|
86
|
-
|
|
238
|
+
else if (typeof options.expected === "string") {
|
|
239
|
+
// Claim the directory entry BEFORE verification. Reading the old fd and then rename-overwriting the
|
|
240
|
+
// path leaves a verify→commit race; a concurrent replacement can otherwise be silently destroyed.
|
|
241
|
+
const claimed = join(dir, `.hara-claim-${process.pid}-${randomUUID()}.tmp`);
|
|
242
|
+
try {
|
|
243
|
+
await rename(target, claimed);
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
if (error?.code === "ENOENT")
|
|
247
|
+
throw new FileChangedError(path);
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
let claimedIdentity;
|
|
251
|
+
try {
|
|
252
|
+
const pathInfo = await lstat(claimed);
|
|
253
|
+
claimedIdentity = {
|
|
254
|
+
dev: pathInfo.dev,
|
|
255
|
+
ino: pathInfo.ino,
|
|
256
|
+
mode: pathInfo.mode & 0o777,
|
|
257
|
+
nlink: pathInfo.nlink,
|
|
258
|
+
linkTarget: pathInfo.isSymbolicLink() ? readlinkSync(claimed) : undefined,
|
|
259
|
+
};
|
|
260
|
+
const current = await readRegularFileSnapshotNoFollow(claimed);
|
|
261
|
+
const expectedIdentity = options.expectedIdentity;
|
|
262
|
+
const identityMatches = !expectedIdentity ||
|
|
263
|
+
(current.dev === expectedIdentity.dev &&
|
|
264
|
+
current.ino === expectedIdentity.ino &&
|
|
265
|
+
current.mode === expectedIdentity.mode &&
|
|
266
|
+
current.nlink === expectedIdentity.nlink);
|
|
267
|
+
if (!identityMatches || current.text !== options.expected)
|
|
268
|
+
throw new FileChangedError(path);
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
87
271
|
try {
|
|
88
|
-
|
|
272
|
+
if (!claimedIdentity) {
|
|
273
|
+
const info = lstatSync(claimed);
|
|
274
|
+
claimedIdentity = {
|
|
275
|
+
dev: info.dev,
|
|
276
|
+
ino: info.ino,
|
|
277
|
+
mode: info.mode & 0o777,
|
|
278
|
+
nlink: info.nlink,
|
|
279
|
+
linkTarget: info.isSymbolicLink() ? readlinkSync(claimed) : undefined,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
restoreClaimedPath(claimed, target, claimedIdentity);
|
|
89
283
|
}
|
|
90
|
-
catch {
|
|
91
|
-
throw new
|
|
284
|
+
catch (restoreError) {
|
|
285
|
+
throw new Error(`${error instanceof Error ? error.message : String(error)}; safe restore was incomplete: ${restoreError?.message ?? String(restoreError)}`, { cause: error });
|
|
92
286
|
}
|
|
93
|
-
|
|
94
|
-
throw new FileChangedError(path);
|
|
287
|
+
throw error;
|
|
95
288
|
}
|
|
289
|
+
if (!claimedIdentity)
|
|
290
|
+
throw new Error(`Failed to identify claimed file for ${path}`);
|
|
291
|
+
try {
|
|
292
|
+
await link(temp, target); // atomic create-if-absent: never overwrites an entry created after claim.
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
let recovery = "";
|
|
296
|
+
try {
|
|
297
|
+
restoreClaimedPath(claimed, target, claimedIdentity);
|
|
298
|
+
}
|
|
299
|
+
catch (restoreError) {
|
|
300
|
+
recovery = `; claimed old entry is retained: ${restoreError?.message ?? String(restoreError)}`;
|
|
301
|
+
}
|
|
302
|
+
if (error?.code === "EEXIST")
|
|
303
|
+
throw new Error(`${new FileChangedError(path).message}${recovery}`);
|
|
304
|
+
throw new Error(`${error?.message ?? String(error)}${recovery}`, { cause: error });
|
|
305
|
+
}
|
|
306
|
+
await unlink(temp);
|
|
307
|
+
staged = false;
|
|
308
|
+
try {
|
|
309
|
+
discardClaimedPath(claimed, claimedIdentity);
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
warnings.push(`old entry cleanup was refused: ${error?.message ?? String(error)}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
96
316
|
await rename(temp, target);
|
|
97
317
|
staged = false;
|
|
98
318
|
}
|
|
99
319
|
await syncDirectory(dir);
|
|
320
|
+
succeeded = true;
|
|
100
321
|
}
|
|
101
322
|
finally {
|
|
102
323
|
if (staged)
|
|
103
324
|
await unlink(temp).catch(() => { });
|
|
325
|
+
if (!succeeded)
|
|
326
|
+
await removeCreatedDirectories(createdDirs).catch(() => { });
|
|
104
327
|
}
|
|
328
|
+
if (!writtenIdentity)
|
|
329
|
+
throw new Error(`Failed to identify staged file for ${path}`);
|
|
330
|
+
return { ...writtenIdentity, target, createdDirs, ...(warnings.length ? { warnings } : {}) };
|
|
105
331
|
}
|
package/dist/gateway/dingtalk.js
CHANGED
|
@@ -38,7 +38,10 @@ export function parseDingtalkMessage(msg) {
|
|
|
38
38
|
text = flattenRichText(msg.content?.richText).trim();
|
|
39
39
|
if (!text)
|
|
40
40
|
return null; // unsupported type (audio/file/etc.) or empty
|
|
41
|
-
|
|
41
|
+
// DingTalk documents conversationType=1 for a one-to-one bot chat and 2 for groups. Missing/novel values
|
|
42
|
+
// stay group-classified so protocol drift cannot expose the full-auto DM driver in a channel.
|
|
43
|
+
const chatType = String(msg.conversationType ?? "").trim() === "1" ? "p2p" : "group";
|
|
44
|
+
return { msg: { chatId, userId, userName, text, chatType }, sessionWebhook };
|
|
42
45
|
}
|
|
43
46
|
/** Flatten a DingTalk richText message (an array of {text}/{type} runs) into plain text (pure). */
|
|
44
47
|
function flattenRichText(runs) {
|
package/dist/gateway/discord.js
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
// the cross-platform gateway plumbing (send_file, in-chat system context, stuck-guard, image attach/describe)
|
|
5
5
|
// works unchanged. NOTE: receiving message text needs the privileged "Message Content Intent" enabled for the
|
|
6
6
|
// bot in the Discord developer portal.
|
|
7
|
-
import { readFileSync
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { basename } from "node:path";
|
|
9
|
+
import { InboundMediaBudget, savePrivateResponse } from "./media.js";
|
|
10
10
|
import { chunkText } from "./telegram.js";
|
|
11
11
|
const REST = "https://discord.com/api/v10";
|
|
12
12
|
const GATEWAY = "wss://gateway.discord.gg/?v=10&encoding=json";
|
|
@@ -20,16 +20,12 @@ const sleep = (ms, signal) => new Promise((r) => {
|
|
|
20
20
|
signal?.addEventListener?.("abort", () => { clearTimeout(t); r(); }, { once: true });
|
|
21
21
|
});
|
|
22
22
|
const isImage = (name, mime) => (mime?.startsWith("image/") ?? false) || /\.(png|jpe?g|gif|webp)$/i.test(name);
|
|
23
|
-
async function downloadDiscordAttachment(url, filename) {
|
|
23
|
+
async function downloadDiscordAttachment(url, filename, options) {
|
|
24
24
|
try {
|
|
25
|
-
const r = await fetch(url);
|
|
25
|
+
const r = await fetch(url, { signal: options.signal });
|
|
26
26
|
if (!r.ok)
|
|
27
27
|
return null;
|
|
28
|
-
|
|
29
|
-
mkdirSync(dir, { recursive: true });
|
|
30
|
-
const path = join(dir, `dc_${Date.now()}_${basename(filename) || "file.bin"}`);
|
|
31
|
-
writeFileSync(path, Buffer.from(await r.arrayBuffer()));
|
|
32
|
-
return path;
|
|
28
|
+
return await savePrivateResponse(r, { platform: "discord", filenameHint: filename, ...options });
|
|
33
29
|
}
|
|
34
30
|
catch {
|
|
35
31
|
return null;
|
|
@@ -37,7 +33,13 @@ async function downloadDiscordAttachment(url, filename) {
|
|
|
37
33
|
}
|
|
38
34
|
/** Parse a Discord MESSAGE_CREATE payload → InboundMsg + its image attachment URLs (pure; download happens in
|
|
39
35
|
* start()). null = ignore (own message / another bot / empty). */
|
|
40
|
-
export function
|
|
36
|
+
export function discordChatType(message, resolvedChannelType) {
|
|
37
|
+
if (message?.guild_id)
|
|
38
|
+
return "group";
|
|
39
|
+
// Discord channel type 1 is a one-to-one DM; type 3 is a group DM. Missing/failed REST metadata stays group.
|
|
40
|
+
return Number(resolvedChannelType) === 1 ? "p2p" : "group";
|
|
41
|
+
}
|
|
42
|
+
export function parseDiscordMessage(d, selfId, resolvedChannelType) {
|
|
41
43
|
if (!d?.channel_id || !d?.author?.id)
|
|
42
44
|
return null;
|
|
43
45
|
if (d.author.id === selfId || d.author.bot)
|
|
@@ -55,6 +57,7 @@ export function parseDiscordMessage(d, selfId) {
|
|
|
55
57
|
userId: String(d.author.id),
|
|
56
58
|
userName: d.author.global_name || d.author.username || String(d.author.id),
|
|
57
59
|
text: text || "[图片]",
|
|
60
|
+
chatType: discordChatType(d, resolvedChannelType),
|
|
58
61
|
},
|
|
59
62
|
imageUrls,
|
|
60
63
|
};
|
|
@@ -78,13 +81,13 @@ export function discordAdapter(token) {
|
|
|
78
81
|
form.append("files[0]", new Blob([readFileSync(filePath)]), basename(filePath));
|
|
79
82
|
await fetch(`${REST}/channels/${chatId}/messages`, { method: "POST", headers: auth, body: form }).catch(() => { });
|
|
80
83
|
},
|
|
81
|
-
async start(onMessage, signal) {
|
|
84
|
+
async start(onMessage, signal, shouldDownload) {
|
|
82
85
|
if (!WSImpl) {
|
|
83
86
|
console.error("hara gateway: Discord needs Node ≥ 22 (global WebSocket). Upgrade Node.");
|
|
84
87
|
return;
|
|
85
88
|
}
|
|
86
89
|
while (!signal.aborted) {
|
|
87
|
-
await connectOnce(token, onMessage, signal);
|
|
90
|
+
await connectOnce(token, onMessage, signal, shouldDownload);
|
|
88
91
|
if (!signal.aborted)
|
|
89
92
|
await sleep(3000, signal); // reconnect backoff
|
|
90
93
|
}
|
|
@@ -93,9 +96,10 @@ export function discordAdapter(token) {
|
|
|
93
96
|
}
|
|
94
97
|
/** One gateway connection: HELLO→heartbeat, IDENTIFY, then dispatch MESSAGE_CREATE. Resolves on close/abort;
|
|
95
98
|
* the caller reconnects. v1 keeps it simple — fresh IDENTIFY each time, no RESUME. */
|
|
96
|
-
function connectOnce(token, onMessage, signal) {
|
|
99
|
+
function connectOnce(token, onMessage, signal, shouldDownload) {
|
|
97
100
|
return new Promise((resolve) => {
|
|
98
101
|
const ws = new WSImpl(GATEWAY);
|
|
102
|
+
const channelTypes = new Map();
|
|
99
103
|
let hb = null;
|
|
100
104
|
let seq = null;
|
|
101
105
|
let selfId = "";
|
|
@@ -145,12 +149,43 @@ function connectOnce(token, onMessage, signal) {
|
|
|
145
149
|
if (p.t === "READY")
|
|
146
150
|
selfId = p.d?.user?.id ?? "";
|
|
147
151
|
else if (p.t === "MESSAGE_CREATE") {
|
|
152
|
+
// Parse/filter first with the safe group default, then resolve non-guild channel metadata only for a
|
|
153
|
+
// real user message. Channel type is immutable, so known results are safe to cache for this connection.
|
|
148
154
|
const parsed = parseDiscordMessage(p.d, selfId);
|
|
149
155
|
if (parsed) {
|
|
150
|
-
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
156
|
+
if (!p.d?.guild_id) {
|
|
157
|
+
const channelId = String(p.d?.channel_id ?? "");
|
|
158
|
+
let channelType = channelTypes.get(channelId);
|
|
159
|
+
if (channelType === undefined && channelId) {
|
|
160
|
+
try {
|
|
161
|
+
const response = await fetch(`${REST}/channels/${encodeURIComponent(channelId)}`, {
|
|
162
|
+
headers: { Authorization: `Bot ${token}` },
|
|
163
|
+
signal,
|
|
164
|
+
});
|
|
165
|
+
const body = response.ok ? (await response.json()) : null;
|
|
166
|
+
const candidate = Number(body?.type);
|
|
167
|
+
if (Number.isSafeInteger(candidate)) {
|
|
168
|
+
channelType = candidate;
|
|
169
|
+
channelTypes.set(channelId, candidate);
|
|
170
|
+
if (channelTypes.size > 1000)
|
|
171
|
+
channelTypes.delete(channelTypes.keys().next().value);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
/* cannot prove a DM → retain the safe group classification and retry on the next message */
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
parsed.msg.chatType = discordChatType(p.d, channelType);
|
|
179
|
+
}
|
|
180
|
+
if (shouldDownload?.(parsed.msg) === true) {
|
|
181
|
+
const budget = new InboundMediaBudget("discord", signal);
|
|
182
|
+
for (const im of parsed.imageUrls) {
|
|
183
|
+
const path = await budget.download((options) => downloadDiscordAttachment(im.url, im.name, options));
|
|
184
|
+
if (path) {
|
|
185
|
+
(parsed.msg.images ??= []).push(path);
|
|
186
|
+
(parsed.msg.transientFiles ??= []).push(path);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
154
189
|
}
|
|
155
190
|
await onMessage(parsed.msg).catch(() => { });
|
|
156
191
|
}
|