@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
@@ -0,0 +1,248 @@
1
+ import crypto from "node:crypto";
2
+ import type { ChannelAccountSnapshot } from "openclaw/plugin-sdk/channel-contract";
3
+ import type { ChannelOutboundAdapter } from "openclaw/plugin-sdk/channel-send-result";
4
+ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
5
+ import type { ChannelPlugin } from "openclaw/plugin-sdk/core";
6
+ import { monitorTlonProvider } from "./monitor/index.js";
7
+ import { tlonSetupWizard } from "./setup-surface.js";
8
+ import {
9
+ formatTargetHint,
10
+ normalizeShip,
11
+ parseTlonTarget,
12
+ resolveTlonOutboundTarget,
13
+ } from "./targets.js";
14
+ import { configureClient } from "./tlon-api.js";
15
+ import { resolveTlonAccount } from "./types.js";
16
+ import { authenticate } from "./urbit/auth.js";
17
+ import { ssrfPolicyFromDangerouslyAllowPrivateNetwork } from "./urbit/context.js";
18
+ import { urbitFetch } from "./urbit/fetch.js";
19
+ import {
20
+ buildMediaStory,
21
+ sendDm,
22
+ sendDmWithStory,
23
+ sendGroupMessage,
24
+ sendGroupMessageWithStory,
25
+ } from "./urbit/send.js";
26
+ import { uploadImageFromUrl } from "./urbit/upload.js";
27
+
28
+ type ResolvedTlonAccount = ReturnType<typeof resolveTlonAccount>;
29
+ type ConfiguredTlonAccount = ResolvedTlonAccount & {
30
+ ship: string;
31
+ url: string;
32
+ code: string;
33
+ };
34
+
35
+ async function createHttpPokeApi(params: {
36
+ url: string;
37
+ code: string;
38
+ ship: string;
39
+ dangerouslyAllowPrivateNetwork?: boolean;
40
+ }) {
41
+ const ssrfPolicy = ssrfPolicyFromDangerouslyAllowPrivateNetwork(
42
+ params.dangerouslyAllowPrivateNetwork,
43
+ );
44
+ const cookie = await authenticate(params.url, params.code, { ssrfPolicy });
45
+ const channelId = `${Math.floor(Date.now() / 1000)}-${crypto.randomUUID()}`;
46
+ const channelPath = `/~/channel/${channelId}`;
47
+ const shipName = params.ship.replace(/^~/, "");
48
+
49
+ return {
50
+ poke: async (pokeParams: { app: string; mark: string; json: unknown }) => {
51
+ const pokeId = Date.now();
52
+ const pokeData = {
53
+ id: pokeId,
54
+ action: "poke",
55
+ ship: shipName,
56
+ app: pokeParams.app,
57
+ mark: pokeParams.mark,
58
+ json: pokeParams.json,
59
+ };
60
+
61
+ const { response, release } = await urbitFetch({
62
+ baseUrl: params.url,
63
+ path: channelPath,
64
+ init: {
65
+ method: "PUT",
66
+ headers: {
67
+ "Content-Type": "application/json",
68
+ Cookie: cookie.split(";")[0],
69
+ },
70
+ body: JSON.stringify([pokeData]),
71
+ },
72
+ ssrfPolicy,
73
+ auditContext: "tlon-poke",
74
+ });
75
+
76
+ try {
77
+ if (!response.ok && response.status !== 204) {
78
+ const errorText = await response.text();
79
+ throw new Error(`Poke failed: ${response.status} - ${errorText}`);
80
+ }
81
+
82
+ return pokeId;
83
+ } finally {
84
+ await release();
85
+ }
86
+ },
87
+ delete: async () => {
88
+ // No-op for HTTP-only client
89
+ },
90
+ };
91
+ }
92
+
93
+ function resolveOutboundContext(params: {
94
+ cfg: OpenClawConfig;
95
+ accountId?: string | null;
96
+ to: string;
97
+ }) {
98
+ const account = resolveTlonAccount(params.cfg, params.accountId ?? undefined);
99
+ if (!account.configured || !account.ship || !account.url || !account.code) {
100
+ throw new Error("Tlon account not configured");
101
+ }
102
+
103
+ const parsed = parseTlonTarget(params.to);
104
+ if (!parsed) {
105
+ throw new Error(`Invalid Tlon target. Use ${formatTargetHint()}`);
106
+ }
107
+
108
+ return { account: account as ConfiguredTlonAccount, parsed };
109
+ }
110
+
111
+ function resolveReplyId(replyToId?: string | null, threadId?: string | number | null) {
112
+ return (replyToId ?? threadId) ? String(replyToId ?? threadId) : undefined;
113
+ }
114
+
115
+ async function withHttpPokeAccountApi<T>(
116
+ account: ConfiguredTlonAccount,
117
+ run: (api: Awaited<ReturnType<typeof createHttpPokeApi>>) => Promise<T>,
118
+ ) {
119
+ const api = await createHttpPokeApi({
120
+ url: account.url,
121
+ ship: account.ship,
122
+ code: account.code,
123
+ dangerouslyAllowPrivateNetwork: account.dangerouslyAllowPrivateNetwork ?? undefined,
124
+ });
125
+
126
+ try {
127
+ return await run(api);
128
+ } finally {
129
+ try {
130
+ await api.delete();
131
+ } catch {
132
+ // ignore cleanup errors
133
+ }
134
+ }
135
+ }
136
+
137
+ export const tlonRuntimeOutbound: ChannelOutboundAdapter = {
138
+ deliveryMode: "direct",
139
+ textChunkLimit: 10000,
140
+ resolveTarget: ({ to }) => resolveTlonOutboundTarget(to),
141
+ sendText: async ({ cfg, to, text, accountId, replyToId, threadId }) => {
142
+ const { account, parsed } = resolveOutboundContext({ cfg, accountId, to });
143
+ return withHttpPokeAccountApi(account, async (api) => {
144
+ const fromShip = normalizeShip(account.ship);
145
+ if (parsed.kind === "dm") {
146
+ return await sendDm({
147
+ api,
148
+ fromShip,
149
+ toShip: parsed.ship,
150
+ text,
151
+ });
152
+ }
153
+ return await sendGroupMessage({
154
+ api,
155
+ fromShip,
156
+ hostShip: parsed.hostShip,
157
+ channelName: parsed.channelName,
158
+ text,
159
+ replyToId: resolveReplyId(replyToId, threadId),
160
+ });
161
+ });
162
+ },
163
+ sendMedia: async ({ cfg, to, text, mediaUrl, accountId, replyToId, threadId }) => {
164
+ const { account, parsed } = resolveOutboundContext({ cfg, accountId, to });
165
+
166
+ configureClient({
167
+ shipUrl: account.url,
168
+ shipName: account.ship.replace(/^~/, ""),
169
+ verbose: false,
170
+ getCode: async () => account.code,
171
+ dangerouslyAllowPrivateNetwork: account.dangerouslyAllowPrivateNetwork ?? undefined,
172
+ });
173
+
174
+ const uploadedUrl = mediaUrl ? await uploadImageFromUrl(mediaUrl) : undefined;
175
+ return withHttpPokeAccountApi(account, async (api) => {
176
+ const fromShip = normalizeShip(account.ship);
177
+ const story = buildMediaStory(text, uploadedUrl);
178
+
179
+ if (parsed.kind === "dm") {
180
+ return await sendDmWithStory({
181
+ api,
182
+ fromShip,
183
+ toShip: parsed.ship,
184
+ story,
185
+ });
186
+ }
187
+ return await sendGroupMessageWithStory({
188
+ api,
189
+ fromShip,
190
+ hostShip: parsed.hostShip,
191
+ channelName: parsed.channelName,
192
+ story,
193
+ replyToId: resolveReplyId(replyToId, threadId),
194
+ });
195
+ });
196
+ },
197
+ };
198
+
199
+ export async function probeTlonAccount(account: ConfiguredTlonAccount) {
200
+ try {
201
+ const ssrfPolicy = ssrfPolicyFromDangerouslyAllowPrivateNetwork(
202
+ account.dangerouslyAllowPrivateNetwork,
203
+ );
204
+ const cookie = await authenticate(account.url, account.code, { ssrfPolicy });
205
+ const { response, release } = await urbitFetch({
206
+ baseUrl: account.url,
207
+ path: "/~/name",
208
+ init: {
209
+ method: "GET",
210
+ headers: { Cookie: cookie },
211
+ },
212
+ ssrfPolicy,
213
+ timeoutMs: 30_000,
214
+ auditContext: "tlon-probe-account",
215
+ });
216
+ try {
217
+ if (!response.ok) {
218
+ return { ok: false, error: `Name request failed: ${response.status}` };
219
+ }
220
+ return { ok: true };
221
+ } finally {
222
+ await release();
223
+ }
224
+ } catch (error) {
225
+ return { ok: false, error: (error as { message?: string })?.message ?? String(error) };
226
+ }
227
+ }
228
+
229
+ export async function startTlonGatewayAccount(
230
+ ctx: Parameters<
231
+ NonNullable<NonNullable<ChannelPlugin<ResolvedTlonAccount>["gateway"]>["startAccount"]>
232
+ >[0],
233
+ ) {
234
+ const account = ctx.account;
235
+ ctx.setStatus({
236
+ accountId: account.accountId,
237
+ ship: account.ship,
238
+ url: account.url,
239
+ } as ChannelAccountSnapshot);
240
+ ctx.log?.info(`[${account.accountId}] starting Tlon provider for ${account.ship ?? "tlon"}`);
241
+ return monitorTlonProvider({
242
+ runtime: ctx.runtime,
243
+ abortSignal: ctx.abortSignal,
244
+ accountId: account.accountId,
245
+ });
246
+ }
247
+
248
+ export { tlonSetupWizard };