@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,13 +1,51 @@
1
- import type { RuntimeEnv } from "openclaw/plugin-sdk";
2
- import { extractMessageText } from "./utils.js";
1
+ import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime";
2
+ import { asRecord, extractMessageText, formatErrorMessage } from "./utils.js";
3
3
 
4
- export type TlonHistoryEntry = {
4
+ /**
5
+ * Format a number as @ud (with dots every 3 digits from the right)
6
+ * e.g., 170141184507799509469114119040828178432 -> 170.141.184.507.799.509.469.114.119.040.828.178.432
7
+ */
8
+ function formatUd(id: string | number): string {
9
+ const str = String(id).replace(/\./g, ""); // Remove any existing dots
10
+ const reversed = str.split("").toReversed();
11
+ const chunks: string[] = [];
12
+ for (let i = 0; i < reversed.length; i += 3) {
13
+ chunks.push(
14
+ reversed
15
+ .slice(i, i + 3)
16
+ .toReversed()
17
+ .join(""),
18
+ );
19
+ }
20
+ return chunks.toReversed().join(".");
21
+ }
22
+
23
+ type TlonHistoryEntry = {
5
24
  author: string;
6
25
  content: string;
7
26
  timestamp: number;
8
27
  id?: string;
9
28
  };
10
29
 
30
+ function createHistoryEntryFromMemo(params: {
31
+ memo?: Record<string, unknown> | null;
32
+ seal?: Record<string, unknown> | null;
33
+ fallbackId?: unknown;
34
+ }): TlonHistoryEntry {
35
+ const { memo, seal, fallbackId } = params;
36
+ return {
37
+ author: typeof memo?.author === "string" ? memo.author : "unknown",
38
+ content: extractMessageText(memo?.content || []),
39
+ timestamp: typeof memo?.sent === "number" ? memo.sent : Date.now(),
40
+ id:
41
+ typeof seal?.id === "string"
42
+ ? seal.id
43
+ : typeof fallbackId === "string"
44
+ ? fallbackId
45
+ : undefined,
46
+ };
47
+ }
48
+
11
49
  const messageCache = new Map<string, TlonHistoryEntry[]>();
12
50
  const MAX_CACHED_MESSAGES = 100;
13
51
 
@@ -25,7 +63,7 @@ export function cacheMessage(channelNest: string, message: TlonHistoryEntry) {
25
63
  }
26
64
  }
27
65
 
28
- export async function fetchChannelHistory(
66
+ async function fetchChannelHistory(
29
67
  api: { scry: (path: string) => Promise<unknown> },
30
68
  channelNest: string,
31
69
  count = 50,
@@ -35,42 +73,45 @@ export async function fetchChannelHistory(
35
73
  const scryPath = `/channels/v4/${channelNest}/posts/newest/${count}/outline.json`;
36
74
  runtime?.log?.(`[tlon] Fetching history: ${scryPath}`);
37
75
 
38
- // oxlint-disable-next-line typescript/no-explicit-any
39
- const data: any = await api.scry(scryPath);
76
+ const data: unknown = await api.scry(scryPath);
40
77
  if (!data) {
41
78
  return [];
42
79
  }
43
80
 
44
- // oxlint-disable-next-line typescript/no-explicit-any
45
- let posts: any[] = [];
81
+ let posts: unknown[] = [];
46
82
  if (Array.isArray(data)) {
47
83
  posts = data;
48
- } else if (data.posts && typeof data.posts === "object") {
49
- posts = Object.values(data.posts);
50
- } else if (typeof data === "object") {
51
- posts = Object.values(data);
84
+ } else {
85
+ const dataRecord = asRecord(data);
86
+ const postMap = asRecord(dataRecord?.posts);
87
+ if (postMap) {
88
+ posts = Object.values(postMap);
89
+ } else if (dataRecord) {
90
+ posts = Object.values(dataRecord);
91
+ }
52
92
  }
53
93
 
54
94
  const messages = posts
55
95
  .map((item) => {
56
- const essay = item.essay || item["r-post"]?.set?.essay;
57
- const seal = item.seal || item["r-post"]?.set?.seal;
96
+ const itemRecord = asRecord(item);
97
+ const replyPost = asRecord(itemRecord?.["r-post"]);
98
+ const replyPostSet = asRecord(replyPost?.set);
99
+ const essay = asRecord(itemRecord?.essay) ?? asRecord(replyPostSet?.essay);
100
+ const seal = asRecord(itemRecord?.seal) ?? asRecord(replyPostSet?.seal);
58
101
 
59
102
  return {
60
- author: essay?.author || "unknown",
103
+ author: typeof essay?.author === "string" ? essay.author : "unknown",
61
104
  content: extractMessageText(essay?.content || []),
62
- timestamp: essay?.sent || Date.now(),
63
- id: seal?.id,
105
+ timestamp: typeof essay?.sent === "number" ? essay.sent : Date.now(),
106
+ id: typeof seal?.id === "string" ? seal.id : undefined,
64
107
  } as TlonHistoryEntry;
65
108
  })
66
109
  .filter((msg) => msg.content);
67
110
 
68
111
  runtime?.log?.(`[tlon] Extracted ${messages.length} messages from history`);
69
112
  return messages;
70
- } catch (error) {
71
- runtime?.log?.(
72
- `[tlon] Error fetching channel history: ${(error as { message?: string })?.message ?? String(error)}`,
73
- );
113
+ } catch (error: unknown) {
114
+ runtime?.log?.(`[tlon] Error fetching channel history: ${formatErrorMessage(error)}`);
74
115
  return [];
75
116
  }
76
117
  }
@@ -90,3 +131,96 @@ export async function getChannelHistory(
90
131
  runtime?.log?.(`[tlon] Cache has ${cache.length} messages, need ${count}, fetching from scry...`);
91
132
  return await fetchChannelHistory(api, channelNest, count, runtime);
92
133
  }
134
+
135
+ /**
136
+ * Fetch thread/reply history for a specific parent post.
137
+ * Used to get context when entering a thread conversation.
138
+ */
139
+ export async function fetchThreadHistory(
140
+ api: { scry: (path: string) => Promise<unknown> },
141
+ channelNest: string,
142
+ parentId: string,
143
+ count = 50,
144
+ runtime?: RuntimeEnv,
145
+ ): Promise<TlonHistoryEntry[]> {
146
+ try {
147
+ // Tlon API: fetch replies to a specific post
148
+ // Format: /channels/v4/{nest}/posts/post/{parentId}/replies/newest/{count}.json
149
+ // parentId needs @ud formatting (dots every 3 digits)
150
+ const formattedParentId = formatUd(parentId);
151
+ runtime?.log?.(
152
+ `[tlon] Thread history - parentId: ${parentId} -> formatted: ${formattedParentId}`,
153
+ );
154
+
155
+ const scryPath = `/channels/v4/${channelNest}/posts/post/id/${formattedParentId}/replies/newest/${count}.json`;
156
+ runtime?.log?.(`[tlon] Fetching thread history: ${scryPath}`);
157
+
158
+ const data: unknown = await api.scry(scryPath);
159
+ if (!data) {
160
+ runtime?.log?.(`[tlon] No thread history data returned`);
161
+ return [];
162
+ }
163
+
164
+ let replies: unknown[] = [];
165
+ if (Array.isArray(data)) {
166
+ replies = data;
167
+ } else {
168
+ const dataRecord = asRecord(data);
169
+ const replyValue = dataRecord?.replies;
170
+ if (Array.isArray(replyValue)) {
171
+ replies = replyValue;
172
+ } else if (typeof replyValue === "object" && replyValue) {
173
+ replies = Object.values(replyValue as Record<string, unknown>);
174
+ } else if (dataRecord) {
175
+ replies = Object.values(dataRecord);
176
+ }
177
+ }
178
+
179
+ const messages = replies
180
+ .map((item) => {
181
+ // Thread replies use 'memo' structure
182
+ const itemRecord = asRecord(item);
183
+ const replyRecord = asRecord(itemRecord?.["r-reply"]);
184
+ const replySet = asRecord(replyRecord?.set);
185
+ const memo = asRecord(itemRecord?.memo) ?? asRecord(replySet?.memo) ?? itemRecord;
186
+ const seal = asRecord(itemRecord?.seal) ?? asRecord(replySet?.seal);
187
+
188
+ return createHistoryEntryFromMemo({ memo, seal, fallbackId: itemRecord?.id });
189
+ })
190
+ .filter((msg) => msg.content);
191
+
192
+ runtime?.log?.(`[tlon] Extracted ${messages.length} thread replies from history`);
193
+ return messages;
194
+ } catch (error: unknown) {
195
+ runtime?.log?.(`[tlon] Error fetching thread history: ${formatErrorMessage(error)}`);
196
+ // Fall back to trying alternate path structure
197
+ try {
198
+ const altPath = `/channels/v4/${channelNest}/posts/post/id/${formatUd(parentId)}.json`;
199
+ runtime?.log?.(`[tlon] Trying alternate path: ${altPath}`);
200
+ const data = asRecord(await api.scry(altPath));
201
+ const dataSeal = asRecord(data?.seal);
202
+ const dataMeta = asRecord(dataSeal?.meta);
203
+ const repliesValue = data?.replies;
204
+
205
+ if (typeof dataMeta?.replyCount === "number" && dataMeta.replyCount > 0 && repliesValue) {
206
+ const replies = Array.isArray(repliesValue)
207
+ ? repliesValue
208
+ : Object.values(repliesValue as Record<string, unknown>);
209
+ const messages = replies
210
+ .map((reply: unknown) => {
211
+ const replyRecord = asRecord(reply);
212
+ const memo = asRecord(replyRecord?.memo);
213
+ const seal = asRecord(replyRecord?.seal);
214
+ return createHistoryEntryFromMemo({ memo, seal });
215
+ })
216
+ .filter((msg: TlonHistoryEntry) => msg.content);
217
+
218
+ runtime?.log?.(`[tlon] Extracted ${messages.length} replies from post data`);
219
+ return messages;
220
+ }
221
+ } catch (altError: unknown) {
222
+ runtime?.log?.(`[tlon] Alternate path also failed: ${formatErrorMessage(altError)}`);
223
+ }
224
+ return [];
225
+ }
226
+ }