clawgram 2.26.0 → 2.27.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.
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readAccountReadChats = readAccountReadChats;
4
+ exports.resolveAccountReadChats = resolveAccountReadChats;
5
+ exports.resolveAccountSendChats = resolveAccountSendChats;
6
+ exports.refuseOutboundOutsideScope = refuseOutboundOutsideScope;
7
+ exports.resolveAccountOperatorIds = resolveAccountOperatorIds;
8
+ exports.resolveAccountDiscoverChats = resolveAccountDiscoverChats;
9
+ exports.resolveAccountManageChats = resolveAccountManageChats;
10
+ exports.readAccountManageChats = readAccountManageChats;
11
+ const core_1 = require("openclaw/plugin-sdk/core");
12
+ const history_1 = require("./history");
13
+ const send_scope_1 = require("./send-scope");
14
+ /**
15
+ * Per-account scope as configured — the readers every action module shares.
16
+ *
17
+ * Lived at the top of `channel.ts` next to the dispatcher that used them;
18
+ * moved out with the dispatcher's branches (audit B5-13, part 3). Pure
19
+ * config readers: no runtime, no state.
20
+ */
21
+ const actionLog = (0, core_1.createSubsystemLogger)("channels/clawgram");
22
+ /**
23
+ * Read scope as configured for the account. Left `undefined` when the key is
24
+ * absent so `isChatReadable` can tell "not configured" from "configured empty" —
25
+ * the first means no restriction, the second denies everything.
26
+ */
27
+ function readAccountReadChats(account) {
28
+ return (0, history_1.normalizeScopeList)(account?.readChats);
29
+ }
30
+ function resolveAccountReadChats(cfg, accountId) {
31
+ return readAccountReadChats(cfg?.channels?.["clawgram"]?.accounts?.[accountId]);
32
+ }
33
+ /**
34
+ * Outbound scope as configured. Handed to `isChatSendable` raw: an absent
35
+ * value means "unrestricted" and an empty list means "deny", and only the
36
+ * raw value tells those apart — same shape as `readChats`.
37
+ */
38
+ function resolveAccountSendChats(cfg, accountId) {
39
+ return cfg?.channels?.["clawgram"]?.accounts?.[accountId]?.sendChats;
40
+ }
41
+ /** One refusal for every outbound action, so the three read the same. */
42
+ function refuseOutboundOutsideScope(action, accountId, target) {
43
+ const refusal = (0, send_scope_1.describeSendRefusal)(target);
44
+ actionLog.warn(`clawgram ${action} refused: ${refusal.reason}`, { accountId, ...refusal.logFields });
45
+ throw refusal.error;
46
+ }
47
+ /**
48
+ * Who receives core's operational telemetry in a DM.
49
+ */
50
+ function resolveAccountOperatorIds(cfg, accountId) {
51
+ const account = cfg?.channels?.["clawgram"]?.accounts?.[accountId];
52
+ // Только явный список. Умолчание «operatorIds = allowFrom» делало
53
+ // оператором каждого допущенного собеседника — и телеметрию с путями
54
+ // secret-store получал любой из них (D2-03, A5-11). Не назван — не
55
+ // назван: уведомления подавляются везде.
56
+ const raw = account?.operatorIds;
57
+ if (raw === undefined || raw === null)
58
+ return [];
59
+ const entries = Array.isArray(raw) ? raw : [raw];
60
+ return entries.map((entry) => String(entry).trim()).filter(Boolean);
61
+ }
62
+ function resolveAccountDiscoverChats(cfg, accountId) {
63
+ return cfg?.channels?.["clawgram"]?.accounts?.[accountId]?.discoverChats;
64
+ }
65
+ /**
66
+ * Management scope as configured. Handed to `isChatManageable` raw: unlike
67
+ * `readChats`, an absent value already means "deny", so there is nothing to
68
+ * tell apart — but the raw value keeps the two gates symmetrical.
69
+ */
70
+ function resolveAccountManageChats(cfg, accountId) {
71
+ return cfg?.channels?.["clawgram"]?.accounts?.[accountId]?.manageChats;
72
+ }
73
+ /** Same normalization `readChats` gets, for the resolved-account copy. */
74
+ function readAccountManageChats(account) {
75
+ return (0, history_1.normalizeScopeList)(account?.manageChats);
76
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleManageAction = handleManageAction;
4
+ const core_1 = require("openclaw/plugin-sdk/core");
5
+ const account_scopes_1 = require("./account-scopes");
6
+ const actions_1 = require("./actions");
7
+ const manage_1 = require("./manage");
8
+ /**
9
+ * The chat-management actions, gated by `manageChats`.
10
+ *
11
+ * Cut out of `handleAction` in `channel.ts` unchanged (audit B5-13, part 3);
12
+ * the probe is the proof. Answers `undefined` for any other action.
13
+ */
14
+ const actionLog = (0, core_1.createSubsystemLogger)("channels/clawgram");
15
+ async function handleManageAction(ctx) {
16
+ const { canonical, params, cfg, accountId, dryRun, toolContext, resolveRuntimeAccountId, requireRuntimeFor } = ctx;
17
+ // ---- Chat management (2.12.0) ----
18
+ //
19
+ // Assembling a chat rather than speaking in it: create a supergroup,
20
+ // add and remove people, appoint admins, hand the chat over, issue an
21
+ // invite link. All of it is possible only because this is a personal
22
+ // MTProto account — a bot could do almost none of this.
23
+ //
24
+ // Every branch is gated by the account's `manageChats` scope, which is
25
+ // opt-in (absent = deny, see manage.ts) — these are the first actions
26
+ // that change a chat rather than write into it. Parsing runs before
27
+ // the gate so a malformed call fails on its own shape, and `dryRun`
28
+ // returns after the gate so a dry run exercises the same refusals a
29
+ // real call would hit. People's ids stay out of the logs throughout;
30
+ // the JSON result carries them to the caller, the journal does not.
31
+ const manageAction = actions_1.MANAGE_ACTIONS.has(canonical) ? canonical : undefined;
32
+ if (manageAction) {
33
+ const manageAccountId = resolveRuntimeAccountId(cfg, accountId);
34
+ if (!manageAccountId) {
35
+ throw new Error("clawgram: no configured account found");
36
+ }
37
+ const manageScope = (0, account_scopes_1.resolveAccountManageChats)(cfg, manageAccountId);
38
+ const requireRuntime = () => requireRuntimeFor(manageAccountId);
39
+ /**
40
+ * The scaffold every management action shares.
41
+ *
42
+ * Six actions used to spell it out one after another: resolve the
43
+ * account, check the scope, log, answer a dry run, call the
44
+ * runtime, log again, build the result. A change to any of those —
45
+ * the dry-run contract, say — was a six-place edit in the plugin's
46
+ * largest file, and the one deliberate exception (createGroup does
47
+ * not check a chat scope, because the chat does not exist yet) was
48
+ * invisible among the copies (finding A12-06).
49
+ *
50
+ * The differences stay written at each call site: what to parse,
51
+ * what to log, what to run, what to answer. Only the scaffold moved.
52
+ */
53
+ const runManage = async (spec) => {
54
+ const parsed = spec.parse();
55
+ const target = spec.target(parsed);
56
+ if (target === undefined) {
57
+ // Nothing to check a scope against yet, so the gate is coarser:
58
+ // management must be enabled at all for this account.
59
+ if (!(0, manage_1.isManagementEnabled)(manageScope)) {
60
+ actionLog.warn(`clawgram ${spec.name} refused: management is not enabled`, {
61
+ accountId: manageAccountId,
62
+ });
63
+ throw new Error("clawgram: chat management is not enabled for this account — "
64
+ + `set channels.clawgram.accounts.${manageAccountId}.manageChats`);
65
+ }
66
+ }
67
+ else if (!(0, manage_1.isChatManageable)(target, manageScope)) {
68
+ actionLog.warn("clawgram management refused: chat outside manage scope", {
69
+ accountId: manageAccountId,
70
+ action: manageAction,
71
+ target,
72
+ });
73
+ throw new Error(`clawgram: not-managed-chat ${target}`);
74
+ }
75
+ actionLog.info(`clawgram handleAction ${spec.name}`, {
76
+ accountId: manageAccountId,
77
+ dryRun: dryRun === true,
78
+ ...spec.before(parsed),
79
+ });
80
+ if (dryRun === true) {
81
+ return (0, core_1.jsonResult)({
82
+ ok: true,
83
+ dryRun: true,
84
+ accountId: manageAccountId,
85
+ ...(target === undefined ? {} : { chatId: target }),
86
+ });
87
+ }
88
+ const gram = requireRuntime();
89
+ spec.precondition?.(gram);
90
+ const result = await spec.run(gram, parsed);
91
+ actionLog.info(`clawgram handleAction ${spec.name} completed`, {
92
+ accountId: manageAccountId,
93
+ ...spec.after(parsed, result),
94
+ });
95
+ return (0, core_1.jsonResult)({ ok: true, accountId: manageAccountId, ...spec.result(parsed, result) });
96
+ };
97
+ if (manageAction === "createGroup") {
98
+ return await runManage({
99
+ name: "createGroup",
100
+ parse: () => (0, manage_1.parseCreateGroupParams)(params),
101
+ // A group being created is not in any scope yet.
102
+ target: () => undefined,
103
+ before: (p) => ({ users: p.users.length, hasAbout: Boolean(p.about) }),
104
+ run: (gram, p) => gram.createGroup(p),
105
+ after: (_p, created) => ({ chatId: created.chatId ?? null, missing: created.missing.length }),
106
+ result: (_p, created) => ({ chatId: created.chatId, missing: created.missing }),
107
+ });
108
+ }
109
+ if (manageAction === "addMembers") {
110
+ return await runManage({
111
+ name: "addMembers",
112
+ parse: () => (0, manage_1.parseAddMembersParams)(params, toolContext),
113
+ target: (p) => p.target,
114
+ before: (p) => ({ target: p.target, users: p.users.length }),
115
+ run: (gram, p) => gram.addChatMembers(p),
116
+ after: (p, added) => ({
117
+ target: p.target,
118
+ requested: p.users.length,
119
+ missing: added.missing.length,
120
+ }),
121
+ result: (p, added) => ({
122
+ chatId: added.chatId ?? p.target,
123
+ requested: p.users.length,
124
+ // Telegram refuses silently-restricted invites per user; the
125
+ // caller gets the ids so it can hand them an invite link.
126
+ missing: added.missing,
127
+ }),
128
+ });
129
+ }
130
+ if (manageAction === "removeMember") {
131
+ return await runManage({
132
+ name: "removeMember",
133
+ parse: () => (0, manage_1.parseRemoveMemberParams)(params, toolContext),
134
+ target: (p) => p.target,
135
+ before: (p) => ({ target: p.target, ban: p.ban }),
136
+ run: (gram, p) => gram.removeChatMember(p),
137
+ after: (p) => ({ target: p.target, ban: p.ban }),
138
+ result: (p) => ({ chatId: p.target, user: p.user, banned: p.ban }),
139
+ });
140
+ }
141
+ if (manageAction === "promoteAdmin" || manageAction === "demoteAdmin") {
142
+ const promote = manageAction === "promoteAdmin";
143
+ return await runManage({
144
+ // Both spellings log as `setAdmin`, as they always have.
145
+ name: "setAdmin",
146
+ parse: () => (promote
147
+ ? (0, manage_1.parsePromoteAdminParams)(params, toolContext)
148
+ : (0, manage_1.parseDemoteAdminParams)(params, toolContext)),
149
+ target: (p) => p.target,
150
+ before: (p) => ({ target: p.target, isAdmin: p.isAdmin, hasRank: Boolean(p.rank) }),
151
+ run: (gram, p) => gram.setChatAdmin(p),
152
+ after: (p) => ({ target: p.target, isAdmin: p.isAdmin }),
153
+ result: (p) => ({
154
+ chatId: p.target,
155
+ user: p.user,
156
+ isAdmin: p.isAdmin,
157
+ ...(p.rank ? { rank: p.rank } : {}),
158
+ }),
159
+ });
160
+ }
161
+ if (manageAction === "transferOwnership") {
162
+ return await runManage({
163
+ name: "transferOwnership",
164
+ parse: () => (0, manage_1.parseTransferOwnershipParams)(params, toolContext),
165
+ target: (p) => p.target,
166
+ before: (p) => ({ target: p.target }),
167
+ // The password stays inside the runtime: it is read from the
168
+ // account config at start-up and never travels through dispatch
169
+ // arguments, which are one log call away from the journal.
170
+ precondition: (gram) => {
171
+ if (!gram.twoFaPassword) {
172
+ throw new Error("clawgram: ownership transfer requires twoFaPassword in the account config "
173
+ + "(the account's Telegram 2FA password, as a literal or a SecretRef)");
174
+ }
175
+ },
176
+ run: (gram, p) => gram.transferChatOwnership(p),
177
+ after: (p) => ({ target: p.target }),
178
+ result: (p) => ({ chatId: p.target, newOwner: p.user }),
179
+ });
180
+ }
181
+ // inviteLink — the only management action left.
182
+ return await runManage({
183
+ name: "inviteLink",
184
+ parse: () => (0, manage_1.parseInviteLinkParams)(params, toolContext),
185
+ target: (p) => p.target,
186
+ before: (p) => ({
187
+ target: p.target,
188
+ hasExpiry: p.expireDate !== undefined,
189
+ usageLimit: p.usageLimit ?? null,
190
+ requestNeeded: p.requestNeeded,
191
+ }),
192
+ run: (gram, p) => gram.exportChatInviteLink(p),
193
+ after: (p, exported) => ({ target: p.target, hasLink: Boolean(exported.link) }),
194
+ result: (p, exported) => ({ chatId: p.target, link: exported.link }),
195
+ });
196
+ }
197
+ return undefined;
198
+ }
@@ -0,0 +1,394 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.handleReadAction = handleReadAction;
7
+ const core_1 = require("openclaw/plugin-sdk/core");
8
+ const node_os_1 = __importDefault(require("node:os"));
9
+ const node_path_1 = __importDefault(require("node:path"));
10
+ const account_scopes_1 = require("./account-scopes");
11
+ const attachments_1 = require("./attachments");
12
+ const chat_info_1 = require("./chat-info");
13
+ const dialogs_1 = require("./dialogs");
14
+ const fetch_media_1 = require("./fetch-media");
15
+ const history_1 = require("./history");
16
+ const joins_1 = require("./joins");
17
+ const media_1 = require("./media");
18
+ const state_dir_1 = require("./state-dir");
19
+ const topics_1 = require("./topics");
20
+ /**
21
+ * The read-shaped actions: `read`, `fetch-media`, `participants`, `topics`,
22
+ * `dialogs`, `joins`, `chatInfo`.
23
+ *
24
+ * Cut out of `handleAction` in `channel.ts` unchanged — same gates, same log
25
+ * lines, same answers; the probe in `scripts/verify/action-probe.cjs` is the
26
+ * proof (audit B5-13, part 3). Answers `undefined` for any other action.
27
+ */
28
+ const actionLog = (0, core_1.createSubsystemLogger)("channels/clawgram");
29
+ // Час, а не сутки: `fetch-media` существует ради «прочитать и переслать», и
30
+ // файл нужен ровно на время хода. Сутки означали сутки чужой личной переписки
31
+ // на диске (A5-13).
32
+ const FETCHED_MEDIA_TTL_MS = 60 * 60 * 1000;
33
+ async function handleReadAction(ctx) {
34
+ const { canonical, params, cfg, accountId, toolContext, pluginRuntime, resolveRuntimeAccountId, requireRuntimeFor } = ctx;
35
+ // `read` is what OpenClaw core dispatches (`openclaw message read`,
36
+ // MCP `messages_read`); `list` resolves to it too.
37
+ if (canonical === "read") {
38
+ const listParams = (0, history_1.parseListMessagesParams)(params);
39
+ const listAccountId = resolveRuntimeAccountId(cfg, accountId);
40
+ if (!listAccountId) {
41
+ throw new Error("clawgram: no configured account found");
42
+ }
43
+ // Reading is not a side effect, so a dry run still answers — reporting
44
+ // an empty window would look like a quiet chat rather than a no-op.
45
+ if (!(0, history_1.isChatReadable)(listParams.target, (0, account_scopes_1.resolveAccountReadChats)(cfg, listAccountId))) {
46
+ actionLog.warn("clawgram list refused: chat outside read scope", {
47
+ accountId: listAccountId,
48
+ target: listParams.target,
49
+ });
50
+ throw new Error(`clawgram: not-allowed-chat ${listParams.target}`);
51
+ }
52
+ const listGram = requireRuntimeFor(listAccountId);
53
+ const history = await listGram.listMessages(listParams);
54
+ // Metadata only. Message text is the user's correspondence and has no
55
+ // business in a log that is read while debugging something else.
56
+ actionLog.info("clawgram handleAction list completed", {
57
+ accountId: listAccountId,
58
+ target: listParams.target,
59
+ limit: listParams.limit,
60
+ since: listParams.since ?? null,
61
+ until: listParams.until ?? null,
62
+ returned: history.messages.length,
63
+ truncated: history.truncated,
64
+ });
65
+ return (0, core_1.jsonResult)({
66
+ ok: true,
67
+ accountId: listAccountId,
68
+ chatId: history.chatId ?? listParams.target,
69
+ count: history.messages.length,
70
+ truncated: history.truncated,
71
+ messages: history.messages,
72
+ });
73
+ }
74
+ // The attachment on a message that is already in a chat.
75
+ //
76
+ // `read` says a photo exists; it does not fetch it, and the inbound
77
+ // path only ever reads what arrives while the agent is being addressed.
78
+ // Everything else — a screenshot posted an hour ago, a diagram in a
79
+ // chat the agent reads but was not tagged in — was visible to the
80
+ // channel and unreachable to the agent. Same `readChats` scope as
81
+ // history: this must not become a way to pull bytes out of a chat the
82
+ // account was never allowed to read.
83
+ if (canonical === "fetch-media") {
84
+ const fetchParams = (0, fetch_media_1.parseFetchMediaParams)(params);
85
+ const fetchAccountId = resolveRuntimeAccountId(cfg, accountId);
86
+ if (!fetchAccountId) {
87
+ throw new Error("clawgram: no configured account found");
88
+ }
89
+ if (!(0, history_1.isChatReadable)(fetchParams.target, (0, account_scopes_1.resolveAccountReadChats)(cfg, fetchAccountId))) {
90
+ actionLog.warn("clawgram fetch-media refused: chat outside read scope", {
91
+ accountId: fetchAccountId,
92
+ target: fetchParams.target,
93
+ });
94
+ throw new Error(`clawgram: not-allowed-chat ${fetchParams.target}`);
95
+ }
96
+ const fetchGram = requireRuntimeFor(fetchAccountId);
97
+ // Fetching is a read: a dry run answers for real, the same way `read`
98
+ // does. Nothing leaves the machine — the file lands in a temp
99
+ // directory this channel prunes — so a rehearsal that reported
100
+ // "would fetch" would only teach the agent to ask twice.
101
+ const found = await fetchGram.getMessageById(fetchParams.target, fetchParams.messageId);
102
+ const fetchChatId = found.chatId ?? fetchParams.target;
103
+ if (!found.message) {
104
+ actionLog.info("clawgram fetch-media found no message", {
105
+ accountId: fetchAccountId,
106
+ chatId: fetchChatId,
107
+ messageId: fetchParams.messageId,
108
+ });
109
+ return (0, core_1.jsonResult)({
110
+ ok: false,
111
+ accountId: fetchAccountId,
112
+ chatId: fetchChatId,
113
+ messageId: String(fetchParams.messageId),
114
+ error: "message-not-found",
115
+ });
116
+ }
117
+ // `read` throws the file away, so it gets a directory of its own —
118
+ // the shared directory is keyed by chat and message, and deleting
119
+ // that path would pull the file out from under an earlier `both`
120
+ // fetch of the same message that handed the caller a path.
121
+ // Не общий /tmp: там файлы видит каждый локальный пользователь, а на
122
+ // этом хосте живёт ещё и раннер деплоя. Каталог состояния OpenClaw
123
+ // принадлежит агенту; если он не задан, остаётся /tmp — но права
124
+ // 0700/0600 ставятся в любом случае (A5-13).
125
+ // Каталог состояния принадлежит агенту; при явно заданном
126
+ // OPENCLAW_STATE_DIR вложения не покидают его.
127
+ const mediaRoot = process.env.OPENCLAW_STATE_DIR?.trim()
128
+ ? node_path_1.default.join((0, state_dir_1.resolveStateDir)(), "tmp")
129
+ : node_os_1.default.tmpdir();
130
+ const sharedFetchDir = node_path_1.default.join(mediaRoot, "clawgram-fetched");
131
+ let fetchDir = sharedFetchDir;
132
+ if (fetchParams.mode === "read") {
133
+ const { mkdtemp, mkdir } = await import("node:fs/promises");
134
+ await mkdir(mediaRoot, { recursive: true, mode: 0o700 });
135
+ fetchDir = await mkdtemp(node_path_1.default.join(mediaRoot, "clawgram-media-"));
136
+ }
137
+ else {
138
+ await (0, media_1.pruneFetchedMedia)(sharedFetchDir, FETCHED_MEDIA_TTL_MS, Date.now());
139
+ }
140
+ const downloaded = await (0, media_1.downloadMessageMediaToFile)({
141
+ client: fetchGram.getClient(),
142
+ message: found.message,
143
+ maxBytes: attachments_1.INBOUND_MEDIA_MAX_BYTES,
144
+ dir: fetchDir,
145
+ fileNameFor: ({ media, extension }) => (0, fetch_media_1.fetchedMediaFileName)({
146
+ chatId: fetchChatId,
147
+ messageId: fetchParams.messageId,
148
+ extension,
149
+ fileName: media.fileName,
150
+ }),
151
+ });
152
+ if (!downloaded) {
153
+ // Three different nothings, and the agent has to be able to tell
154
+ // them apart: a message with no attachment, an attachment this
155
+ // channel does not read (a video, a spreadsheet), and one too
156
+ // large to be worth the transfer. Saying "could not fetch" to all
157
+ // three is how "she ignored the picture" starts.
158
+ const described = (0, media_1.describeMedia)(found.message?.media);
159
+ const tooLarge = typeof described?.size === "number" && described.size > attachments_1.INBOUND_MEDIA_MAX_BYTES;
160
+ const error = !described
161
+ ? "no-media"
162
+ : tooLarge
163
+ ? "media-too-large"
164
+ : "unsupported-media";
165
+ actionLog.info("clawgram fetch-media returned nothing", {
166
+ accountId: fetchAccountId,
167
+ chatId: fetchChatId,
168
+ messageId: fetchParams.messageId,
169
+ kind: described?.kind ?? null,
170
+ error,
171
+ });
172
+ return (0, core_1.jsonResult)({
173
+ ok: false,
174
+ accountId: fetchAccountId,
175
+ chatId: fetchChatId,
176
+ messageId: String(fetchParams.messageId),
177
+ media: described ?? null,
178
+ error,
179
+ });
180
+ }
181
+ let read;
182
+ let readError;
183
+ if (fetchParams.mode !== "file") {
184
+ try {
185
+ read = await (0, attachments_1.understandAttachmentFile)({
186
+ runtime: pluginRuntime,
187
+ cfg,
188
+ filePath: downloaded.path,
189
+ mimeType: downloaded.mimeType,
190
+ understanding: downloaded.understanding,
191
+ });
192
+ if (!read) {
193
+ readError = "read-empty";
194
+ }
195
+ }
196
+ catch (err) {
197
+ // The bytes are already here. A failed reading is worth
198
+ // reporting, but it does not undo a successful fetch: the file
199
+ // still exists and can still be forwarded.
200
+ readError = String(err);
201
+ }
202
+ }
203
+ // `read` mode is the inbound contract — the words, not the file — so
204
+ // the bytes go away with the answer. Any other mode keeps them:
205
+ // that is the whole point of asking for a path.
206
+ if (fetchParams.mode === "read") {
207
+ try {
208
+ const { rm } = await import("node:fs/promises");
209
+ await rm(fetchDir, { recursive: true, force: true });
210
+ }
211
+ catch {
212
+ // A file left behind is pruned within a day; failing the call
213
+ // over it would throw away a reading that already succeeded.
214
+ }
215
+ }
216
+ actionLog.info("clawgram fetch-media completed", {
217
+ accountId: fetchAccountId,
218
+ chatId: fetchChatId,
219
+ messageId: fetchParams.messageId,
220
+ mode: fetchParams.mode,
221
+ kind: downloaded.media.kind,
222
+ understanding: downloaded.understanding,
223
+ characters: read?.length ?? 0,
224
+ readError: readError ?? null,
225
+ });
226
+ return (0, core_1.jsonResult)({
227
+ ok: true,
228
+ accountId: fetchAccountId,
229
+ chatId: fetchChatId,
230
+ messageId: String(fetchParams.messageId),
231
+ mode: fetchParams.mode,
232
+ media: downloaded.media,
233
+ understanding: downloaded.understanding,
234
+ filePath: fetchParams.mode === "read" ? undefined : downloaded.path,
235
+ text: read,
236
+ readError,
237
+ });
238
+ }
239
+ /**
240
+ * The scaffold every chat-shaped read shares.
241
+ *
242
+ * `participants`, `topics`, `dialogs`, `joins` and `chatInfo` each
243
+ * spelled out the same sequence: parse, resolve the account, check a
244
+ * scope, fetch the runtime, call it, log counts, answer. Roughly
245
+ * forty lines apiece, differing in four places — which is how a new
246
+ * action came to cost sixty lines of scaffold and how the two gates
247
+ * drifted apart (finding A6-11).
248
+ *
249
+ * The gate follows from the shape rather than being restated: an
250
+ * action that names a chat is gated by `readChats`, `dialogs` has its
251
+ * own discovery gate precisely because its point is to find chats
252
+ * that are not in scope yet, and `joins` has none — the journal only
253
+ * ever holds chats this account was put into.
254
+ *
255
+ * The runtime is a getter, not a value: `joins` reads a file and must
256
+ * not fail merely because no runtime is connected.
257
+ */
258
+ const runRead = async (spec) => {
259
+ const parsed = spec.parse();
260
+ const readAccountId = resolveRuntimeAccountId(cfg, accountId);
261
+ if (!readAccountId) {
262
+ throw new Error("clawgram: no configured account found");
263
+ }
264
+ const target = spec.target?.(parsed);
265
+ if (target !== undefined) {
266
+ if (!(0, history_1.isChatReadable)(target, (0, account_scopes_1.resolveAccountReadChats)(cfg, readAccountId))) {
267
+ actionLog.warn(`clawgram ${spec.name} refused: chat outside read scope`, {
268
+ accountId: readAccountId,
269
+ target,
270
+ });
271
+ throw new Error(`clawgram: not-allowed-chat ${target}`);
272
+ }
273
+ }
274
+ else if (spec.discovery) {
275
+ if (!(0, dialogs_1.isChatDiscoveryEnabled)((0, account_scopes_1.resolveAccountDiscoverChats)(cfg, readAccountId))) {
276
+ actionLog.warn(`clawgram ${spec.name} refused: chat-discovery is not enabled`, {
277
+ accountId: readAccountId,
278
+ });
279
+ throw new Error("clawgram: chat-discovery is not enabled");
280
+ }
281
+ }
282
+ const gram = () => requireRuntimeFor(readAccountId);
283
+ const result = await spec.run({ parsed, accountId: readAccountId, gram });
284
+ actionLog.info(`clawgram handleAction ${spec.name} completed`, {
285
+ accountId: readAccountId,
286
+ ...spec.after(parsed, result),
287
+ });
288
+ return (0, core_1.jsonResult)({ ok: true, accountId: readAccountId, ...spec.result(parsed, result) });
289
+ };
290
+ // Membership is a read, so the same `readChats` scope that gates history
291
+ // gates it too: this cannot become a way to enumerate chats the account
292
+ // was never allowed to read.
293
+ if (canonical === "participants") {
294
+ return await runRead({
295
+ name: "participants",
296
+ parse: () => (0, history_1.parseListParticipantsParams)(params),
297
+ target: (p) => p.target,
298
+ run: ({ parsed, gram }) => gram().listParticipants(parsed),
299
+ // Counts only. Member ids are personal data and have no business in
300
+ // a log that is read while debugging something else.
301
+ after: (p, m) => ({
302
+ target: p.target,
303
+ limit: p.limit,
304
+ returned: m.participants.length,
305
+ truncated: m.truncated,
306
+ }),
307
+ result: (p, m) => ({
308
+ chatId: m.chatId ?? p.target,
309
+ count: m.participants.length,
310
+ truncated: m.truncated,
311
+ participants: m.participants,
312
+ }),
313
+ });
314
+ }
315
+ // Topic names. A forum chat is addressed by topic id, and until now an
316
+ // id could only be lifted off an inbound message — so a topic nobody had
317
+ // written in yet was unreachable, and one named in words was unfindable.
318
+ // Titles say what a chat is working on, so the read scope gates them.
319
+ if (canonical === "topics") {
320
+ return await runRead({
321
+ name: "topics",
322
+ parse: () => (0, topics_1.parseTopicsParams)(params),
323
+ target: (p) => p.target,
324
+ run: ({ parsed, gram }) => gram().listTopics(parsed),
325
+ after: (p, f) => ({
326
+ target: p.target,
327
+ limit: p.limit,
328
+ returned: f.topics.length,
329
+ truncated: f.truncated,
330
+ }),
331
+ result: (p, f) => ({
332
+ chatId: f.chatId ?? p.target,
333
+ count: f.topics.length,
334
+ truncated: f.truncated,
335
+ topics: f.topics,
336
+ }),
337
+ });
338
+ }
339
+ // Which chats this account is in. Not gated by `readChats` — the whole
340
+ // point is to find chats that are not in it yet — so it has a gate of
341
+ // its own, is metadata only, and never reports direct chats.
342
+ if (canonical === "dialogs") {
343
+ return await runRead({
344
+ name: "dialogs",
345
+ parse: () => (0, dialogs_1.parseDialogsParams)(params),
346
+ discovery: true,
347
+ run: ({ parsed, gram }) => gram().listDialogs(parsed),
348
+ // Counts only: which chats a person's account sits in is exactly
349
+ // the kind of thing that should not be sitting in a log.
350
+ after: (p, f) => ({ limit: p.limit, returned: f.dialogs.length, truncated: f.truncated }),
351
+ result: (_p, f) => ({ count: f.dialogs.length, truncated: f.truncated, dialogs: f.dialogs }),
352
+ });
353
+ }
354
+ // Where this account was recently added, and by whom. Reading the journal
355
+ // has no scope check of its own: it only ever contains chats this account
356
+ // was put into, which is exactly what the caller is allowed to learn.
357
+ if (canonical === "joins") {
358
+ return await runRead({
359
+ name: "joins",
360
+ parse: () => (0, joins_1.parseJoinsParams)(params),
361
+ // No runtime: this reads a file, and must answer with none connected.
362
+ run: async ({ parsed, accountId: joinsAccountId }) => (0, joins_1.selectJoinRecords)((0, joins_1.readJoinRecords)((0, joins_1.resolveJoinsJournalPath)(cfg?.channels?.["clawgram"]?.accounts?.[joinsAccountId], joinsAccountId)), parsed),
363
+ after: (p, selected) => ({
364
+ since: p.since ?? null,
365
+ limit: p.limit,
366
+ returned: selected.length,
367
+ }),
368
+ result: (_p, selected) => ({ count: selected.length, joins: selected }),
369
+ });
370
+ }
371
+ // Describing a chat is a read, so the same `readChats` scope that gates
372
+ // history gates it too — this must not become a way to learn the title
373
+ // and size of a chat the account was never allowed to read.
374
+ if (canonical === "chatInfo") {
375
+ return await runRead({
376
+ name: "chatInfo",
377
+ parse: () => (0, chat_info_1.parseChatInfoParams)(params, toolContext),
378
+ target: (p) => p.target,
379
+ run: async ({ parsed, gram }) => {
380
+ const { entity, full } = await gram().getChatInfo(parsed.target);
381
+ return (0, chat_info_1.describeChat)(entity, full);
382
+ },
383
+ // Type and size only. The title of a private chat is as personal as
384
+ // its contents and has no business in a debugging log.
385
+ after: (_p, info) => ({
386
+ type: info.type,
387
+ memberCount: info.memberCount ?? null,
388
+ isForum: info.isForum ?? null,
389
+ }),
390
+ result: (p, info) => ({ chat: { ...info, chatId: info.chatId ?? p.target } }),
391
+ });
392
+ }
393
+ return undefined;
394
+ }