@openclaw/tlon 2026.2.21 → 2026.5.2-beta.1

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.
Files changed (63) hide show
  1. package/api.ts +16 -0
  2. package/channel-plugin-api.ts +1 -0
  3. package/doctor-contract-api.ts +1 -0
  4. package/index.ts +177 -11
  5. package/openclaw.plugin.json +194 -1
  6. package/package.json +57 -4
  7. package/runtime-api.ts +17 -0
  8. package/setup-api.ts +2 -0
  9. package/setup-entry.ts +9 -0
  10. package/src/account-fields.ts +9 -3
  11. package/src/channel.runtime.ts +248 -0
  12. package/src/channel.ts +153 -364
  13. package/src/config-schema.ts +22 -16
  14. package/src/core.test.ts +278 -0
  15. package/src/doctor-contract.ts +9 -0
  16. package/src/doctor.test.ts +40 -0
  17. package/src/doctor.ts +10 -0
  18. package/src/logger-runtime.ts +1 -0
  19. package/src/monitor/approval-runtime.ts +363 -0
  20. package/src/monitor/approval.test.ts +33 -0
  21. package/src/monitor/approval.ts +286 -0
  22. package/src/monitor/authorization.ts +30 -0
  23. package/src/monitor/cites.ts +54 -0
  24. package/src/monitor/discovery.ts +44 -54
  25. package/src/monitor/history.ts +155 -21
  26. package/src/monitor/index.ts +1290 -369
  27. package/src/monitor/media.test.ts +91 -0
  28. package/src/monitor/media.ts +172 -0
  29. package/src/monitor/processed-messages.test.ts +36 -1
  30. package/src/monitor/processed-messages.ts +62 -19
  31. package/src/monitor/settings-helpers.test.ts +113 -0
  32. package/src/monitor/settings-helpers.ts +158 -0
  33. package/src/monitor/utils.ts +311 -45
  34. package/src/runtime.ts +8 -13
  35. package/src/security.test.ts +622 -0
  36. package/src/session-route.ts +40 -0
  37. package/src/settings.ts +391 -0
  38. package/src/setup-core.ts +228 -0
  39. package/src/setup-surface.ts +103 -0
  40. package/src/targets.ts +29 -16
  41. package/src/tlon-api.test.ts +589 -0
  42. package/src/tlon-api.ts +402 -0
  43. package/src/types.ts +106 -45
  44. package/src/urbit/auth.ssrf.test.ts +3 -2
  45. package/src/urbit/auth.ts +2 -2
  46. package/src/urbit/base-url.test.ts +18 -11
  47. package/src/urbit/base-url.ts +9 -5
  48. package/src/urbit/channel-ops.ts +40 -59
  49. package/src/urbit/context.ts +13 -10
  50. package/src/urbit/errors.ts +2 -2
  51. package/src/urbit/fetch.ts +6 -3
  52. package/src/urbit/foreigns.ts +49 -0
  53. package/src/urbit/send.ts +53 -8
  54. package/src/urbit/sse-client.test.ts +184 -25
  55. package/src/urbit/sse-client.ts +123 -69
  56. package/src/urbit/story.ts +332 -0
  57. package/src/urbit/upload.test.ts +154 -0
  58. package/src/urbit/upload.ts +60 -0
  59. package/test-api.ts +1 -0
  60. package/tsconfig.json +16 -0
  61. package/src/config-schema.test.ts +0 -31
  62. package/src/onboarding.ts +0 -229
  63. package/src/urbit/channel-client.ts +0 -157
@@ -1,12 +1,62 @@
1
+ import { formatErrorMessage as sharedFormatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
1
2
  import { normalizeShip } from "../targets.js";
2
3
 
4
+ export interface ParsedCite {
5
+ type: "chan" | "group" | "desk" | "bait";
6
+ nest?: string;
7
+ author?: string;
8
+ postId?: string;
9
+ group?: string;
10
+ flag?: string;
11
+ where?: string;
12
+ }
13
+
14
+ // Extract all cites from message content
15
+ export function extractCites(content: unknown): ParsedCite[] {
16
+ if (!content || !Array.isArray(content)) {
17
+ return [];
18
+ }
19
+
20
+ const cites: ParsedCite[] = [];
21
+
22
+ for (const verse of content) {
23
+ if (verse?.block?.cite && typeof verse.block.cite === "object") {
24
+ const cite = verse.block.cite;
25
+
26
+ if (cite.chan && typeof cite.chan === "object") {
27
+ const { nest, where } = cite.chan;
28
+ const whereMatch = where?.match(/\/msg\/(~[a-z-]+)\/(.+)/);
29
+ cites.push({
30
+ type: "chan",
31
+ nest,
32
+ where,
33
+ author: whereMatch?.[1],
34
+ postId: whereMatch?.[2],
35
+ });
36
+ } else if (cite.group && typeof cite.group === "string") {
37
+ cites.push({ type: "group", group: cite.group });
38
+ } else if (cite.desk && typeof cite.desk === "object") {
39
+ cites.push({ type: "desk", flag: cite.desk.flag, where: cite.desk.where });
40
+ } else if (cite.bait && typeof cite.bait === "object") {
41
+ cites.push({
42
+ type: "bait",
43
+ group: cite.bait.group,
44
+ nest: cite.bait.graph,
45
+ where: cite.bait.where,
46
+ });
47
+ }
48
+ }
49
+ }
50
+
51
+ return cites;
52
+ }
53
+
3
54
  export function formatModelName(modelString?: string | null): string {
4
55
  if (!modelString) {
5
56
  return "AI";
6
57
  }
7
58
  const modelName = modelString.includes("/") ? modelString.split("/")[1] : modelString;
8
59
  const modelMappings: Record<string, string> = {
9
- "claude-opus-4-6": "Claude Opus 4.6",
10
60
  "claude-opus-4-5": "Claude Opus 4.5",
11
61
  "claude-sonnet-4-5": "Claude Sonnet 4.5",
12
62
  "claude-sonnet-3-5": "Claude Sonnet 3.5",
@@ -27,62 +77,287 @@ export function formatModelName(modelString?: string | null): string {
27
77
  .join(" ");
28
78
  }
29
79
 
30
- export function isBotMentioned(messageText: string, botShipName: string): boolean {
80
+ export function isBotMentioned(
81
+ messageText: string,
82
+ botShipName: string,
83
+ nickname?: string,
84
+ ): boolean {
31
85
  if (!messageText || !botShipName) {
32
86
  return false;
33
87
  }
88
+
89
+ // Check for @all mention
90
+ if (/@all\b/i.test(messageText)) {
91
+ return true;
92
+ }
93
+
94
+ // Check for ship mention
34
95
  const normalizedBotShip = normalizeShip(botShipName);
35
96
  const escapedShip = normalizedBotShip.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
36
97
  const mentionPattern = new RegExp(`(^|\\s)${escapedShip}(?=\\s|$)`, "i");
37
- return mentionPattern.test(messageText);
98
+ if (mentionPattern.test(messageText)) {
99
+ return true;
100
+ }
101
+
102
+ // Check for nickname mention (case-insensitive, word boundary)
103
+ if (nickname) {
104
+ const escapedNickname = nickname.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
105
+ const nicknamePattern = new RegExp(`(^|\\s)${escapedNickname}(?=\\s|$|[,!?.])`, "i");
106
+ if (nicknamePattern.test(messageText)) {
107
+ return true;
108
+ }
109
+ }
110
+
111
+ return false;
112
+ }
113
+
114
+ /**
115
+ * Strip bot ship mention from message text for command detection.
116
+ * "~bot-ship /status" → "/status"
117
+ */
118
+ export function stripBotMention(messageText: string, botShipName: string): string {
119
+ if (!messageText || !botShipName) {
120
+ return messageText;
121
+ }
122
+ return messageText.replace(normalizeShip(botShipName), "").trim();
38
123
  }
39
124
 
40
125
  export function isDmAllowed(senderShip: string, allowlist: string[] | undefined): boolean {
41
126
  if (!allowlist || allowlist.length === 0) {
42
- return true;
127
+ return false;
43
128
  }
44
129
  const normalizedSender = normalizeShip(senderShip);
45
130
  return allowlist.map((ship) => normalizeShip(ship)).some((ship) => ship === normalizedSender);
46
131
  }
47
132
 
133
+ /**
134
+ * Check if a group invite from a ship should be auto-accepted.
135
+ *
136
+ * SECURITY: Fail-safe to deny. If allowlist is empty or undefined,
137
+ * ALL invites are rejected - even if autoAcceptGroupInvites is enabled.
138
+ * This prevents misconfigured bots from accepting malicious invites.
139
+ */
140
+ export function isGroupInviteAllowed(
141
+ inviterShip: string,
142
+ allowlist: string[] | undefined,
143
+ ): boolean {
144
+ // SECURITY: Fail-safe to deny when no allowlist configured
145
+ if (!allowlist || allowlist.length === 0) {
146
+ return false;
147
+ }
148
+ const normalizedInviter = normalizeShip(inviterShip);
149
+ return allowlist.map((ship) => normalizeShip(ship)).some((ship) => ship === normalizedInviter);
150
+ }
151
+
152
+ /**
153
+ * Resolve quoted/cited content only after the caller has passed authorization.
154
+ * Unauthorized paths must keep raw text and must not trigger cross-channel cite fetches.
155
+ */
156
+ export async function resolveAuthorizedMessageText(params: {
157
+ rawText: string;
158
+ content: unknown;
159
+ authorizedForCites: boolean;
160
+ resolveAllCites: (content: unknown) => Promise<string>;
161
+ }): Promise<string> {
162
+ const { rawText, content, authorizedForCites, resolveAllCites } = params;
163
+ if (!authorizedForCites) {
164
+ return rawText;
165
+ }
166
+ const citedContent = await resolveAllCites(content);
167
+ return citedContent + rawText;
168
+ }
169
+
170
+ export const asRecord = asNullableObjectRecord;
171
+ export const formatErrorMessage = sharedFormatErrorMessage;
172
+ export const readString = readStringField;
173
+
174
+ function asNullableObjectRecord(value: unknown): Record<string, unknown> | null {
175
+ return value && typeof value === "object" && !Array.isArray(value)
176
+ ? (value as Record<string, unknown>)
177
+ : null;
178
+ }
179
+
180
+ function readStringField(
181
+ record: Record<string, unknown> | null | undefined,
182
+ field: string,
183
+ ): string | undefined {
184
+ const value = record?.[field];
185
+ return typeof value === "string" ? value : undefined;
186
+ }
187
+
188
+ // Helper to recursively extract text from inline content
189
+ function renderInlineItem(
190
+ item: unknown,
191
+ options?: {
192
+ linkMode?: "content-or-href" | "href";
193
+ allowBreak?: boolean;
194
+ allowBlockquote?: boolean;
195
+ },
196
+ ): string {
197
+ if (typeof item === "string") {
198
+ return item;
199
+ }
200
+ const record = asRecord(item);
201
+ if (!record) {
202
+ return "";
203
+ }
204
+ const ship = readString(record, "ship");
205
+ if (ship) {
206
+ return ship;
207
+ }
208
+ if ("sect" in record) {
209
+ const sect = record.sect;
210
+ if (typeof sect === "string") {
211
+ return `@${sect || "all"}`;
212
+ }
213
+ if (sect === null) {
214
+ return "@all";
215
+ }
216
+ }
217
+ if (options?.allowBreak && "break" in record) {
218
+ return "\n";
219
+ }
220
+ const inlineCode = readString(record, "inline-code");
221
+ if (inlineCode) {
222
+ return `\`${inlineCode}\``;
223
+ }
224
+ const code = readString(record, "code");
225
+ if (code) {
226
+ return `\`${code}\``;
227
+ }
228
+ const link = asRecord(record.link);
229
+ const linkHref = link ? readString(link, "href") : undefined;
230
+ if (link && linkHref) {
231
+ const linkContent = readString(link, "content");
232
+ return options?.linkMode === "href" ? linkHref : linkContent || linkHref;
233
+ }
234
+ if (Array.isArray(record.bold)) {
235
+ return `**${extractInlineText(record.bold)}**`;
236
+ }
237
+ if (Array.isArray(record.italics)) {
238
+ return `*${extractInlineText(record.italics)}*`;
239
+ }
240
+ if (Array.isArray(record.strike)) {
241
+ return `~~${extractInlineText(record.strike)}~~`;
242
+ }
243
+ if (options?.allowBlockquote && Array.isArray(record.blockquote)) {
244
+ return `> ${extractInlineText(record.blockquote)}`;
245
+ }
246
+ return "";
247
+ }
248
+
249
+ function extractInlineText(items: readonly unknown[]): string {
250
+ return items.map((item) => renderInlineItem(item)).join("");
251
+ }
252
+
48
253
  export function extractMessageText(content: unknown): string {
49
254
  if (!content || !Array.isArray(content)) {
50
255
  return "";
51
256
  }
52
257
 
53
- return (
54
- content
55
- // oxlint-disable-next-line typescript/no-explicit-any
56
- .map((block: any) => {
57
- if (block.inline && Array.isArray(block.inline)) {
58
- return (
59
- block.inline
60
- // oxlint-disable-next-line typescript/no-explicit-any
61
- .map((item: any) => {
62
- if (typeof item === "string") {
63
- return item;
64
- }
65
- if (item && typeof item === "object") {
66
- if (item.ship) {
67
- return item.ship;
68
- }
69
- if (item.break !== undefined) {
70
- return "\n";
71
- }
72
- if (item.link && item.link.href) {
73
- return item.link.href;
74
- }
75
- }
76
- return "";
77
- })
78
- .join("")
79
- );
80
- }
258
+ return content
259
+ .map((verse) => {
260
+ const verseRecord = asRecord(verse);
261
+ if (!verseRecord) {
81
262
  return "";
82
- })
83
- .join("\n")
84
- .trim()
85
- );
263
+ }
264
+
265
+ // Handle inline content (text, ships, links, etc.)
266
+ if (Array.isArray(verseRecord.inline)) {
267
+ return verseRecord.inline
268
+ .map((item) =>
269
+ renderInlineItem(item, {
270
+ linkMode: "href",
271
+ allowBreak: true,
272
+ allowBlockquote: true,
273
+ }),
274
+ )
275
+ .join("");
276
+ }
277
+
278
+ // Handle block content (images, code blocks, etc.)
279
+ const block = asRecord(verseRecord.block);
280
+ if (block) {
281
+ const image = asRecord(block.image);
282
+
283
+ // Image blocks
284
+ if (image) {
285
+ const imageSrc = readString(image, "src");
286
+ if (imageSrc) {
287
+ const altText = readString(image, "alt");
288
+ const alt = altText ? ` (${altText})` : "";
289
+ return `\n${imageSrc}${alt}\n`;
290
+ }
291
+ }
292
+
293
+ // Code blocks
294
+ const codeBlock = asRecord(block.code);
295
+ if (codeBlock) {
296
+ const lang = readString(codeBlock, "lang") ?? "";
297
+ const code = readString(codeBlock, "code") ?? "";
298
+ return `\n\`\`\`${lang}\n${code}\n\`\`\`\n`;
299
+ }
300
+
301
+ // Header blocks
302
+ const header = asRecord(block.header);
303
+ if (header) {
304
+ const headerContent = Array.isArray(header.content) ? header.content : [];
305
+ const text =
306
+ headerContent.map((item) => (typeof item === "string" ? item : "")).join("") || "";
307
+ return `\n## ${text}\n`;
308
+ }
309
+
310
+ // Cite/quote blocks - parse the reference structure
311
+ const cite = asRecord(block.cite);
312
+ if (cite) {
313
+ const chanCite = asRecord(cite.chan);
314
+
315
+ // ChanCite - reference to a channel message
316
+ if (chanCite) {
317
+ const nest = readString(chanCite, "nest");
318
+ const where = readString(chanCite, "where");
319
+ // where is typically /msg/~author/timestamp
320
+ const whereMatch = where?.match(/\/msg\/(~[a-z-]+)\/(.+)/);
321
+ if (whereMatch) {
322
+ const [, author, _postId] = whereMatch;
323
+ return `\n> [quoted: ${author} in ${nest}]\n`;
324
+ }
325
+ return `\n> [quoted from ${nest}]\n`;
326
+ }
327
+
328
+ // GroupCite - reference to a group
329
+ const group = readString(cite, "group");
330
+ if (group) {
331
+ return `\n> [ref: group ${group}]\n`;
332
+ }
333
+
334
+ // DeskCite - reference to an app/desk
335
+ const desk = asRecord(cite.desk);
336
+ if (desk) {
337
+ const flag = readString(desk, "flag");
338
+ if (flag) {
339
+ return `\n> [ref: ${flag}]\n`;
340
+ }
341
+ }
342
+
343
+ // BaitCite - reference with group+graph context
344
+ const bait = asRecord(cite.bait);
345
+ if (bait) {
346
+ const graph = readString(bait, "graph");
347
+ const groupName = readString(bait, "group");
348
+ if (graph && groupName) {
349
+ return `\n> [ref: ${graph} in ${groupName}]\n`;
350
+ }
351
+ }
352
+
353
+ return `\n> [quoted message]\n`;
354
+ }
355
+ }
356
+
357
+ return "";
358
+ })
359
+ .join("\n")
360
+ .trim();
86
361
  }
87
362
 
88
363
  export function isSummarizationRequest(messageText: string): boolean {
@@ -95,12 +370,3 @@ export function isSummarizationRequest(messageText: string): boolean {
95
370
  ];
96
371
  return patterns.some((pattern) => pattern.test(messageText));
97
372
  }
98
-
99
- export function formatChangesDate(daysAgo = 5): string {
100
- const now = new Date();
101
- const targetDate = new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000);
102
- const year = targetDate.getFullYear();
103
- const month = targetDate.getMonth() + 1;
104
- const day = targetDate.getDate();
105
- return `~${year}.${month}.${day}..20.19.51..9b9d`;
106
- }
package/src/runtime.ts CHANGED
@@ -1,14 +1,9 @@
1
- import type { PluginRuntime } from "openclaw/plugin-sdk";
1
+ import type { PluginRuntime } from "openclaw/plugin-sdk/plugin-runtime";
2
+ import { createPluginRuntimeStore } from "openclaw/plugin-sdk/runtime-store";
2
3
 
3
- let runtime: PluginRuntime | null = null;
4
-
5
- export function setTlonRuntime(next: PluginRuntime) {
6
- runtime = next;
7
- }
8
-
9
- export function getTlonRuntime(): PluginRuntime {
10
- if (!runtime) {
11
- throw new Error("Tlon runtime not initialized");
12
- }
13
- return runtime;
14
- }
4
+ const { setRuntime: setTlonRuntime, getRuntime: getTlonRuntime } =
5
+ createPluginRuntimeStore<PluginRuntime>({
6
+ pluginId: "tlon",
7
+ errorMessage: "Tlon runtime not initialized",
8
+ });
9
+ export { getTlonRuntime, setTlonRuntime };