@openclaw/tlon 2026.2.21
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/README.md +5 -0
- package/index.ts +17 -0
- package/openclaw.plugin.json +9 -0
- package/package.json +32 -0
- package/src/account-fields.ts +25 -0
- package/src/channel.ts +384 -0
- package/src/config-schema.test.ts +31 -0
- package/src/config-schema.ts +47 -0
- package/src/monitor/discovery.ts +78 -0
- package/src/monitor/history.ts +92 -0
- package/src/monitor/index.ts +603 -0
- package/src/monitor/processed-messages.test.ts +23 -0
- package/src/monitor/processed-messages.ts +46 -0
- package/src/monitor/utils.ts +106 -0
- package/src/onboarding.ts +229 -0
- package/src/runtime.ts +14 -0
- package/src/targets.ts +89 -0
- package/src/types.ts +99 -0
- package/src/urbit/auth.ssrf.test.ts +44 -0
- package/src/urbit/auth.ts +48 -0
- package/src/urbit/base-url.test.ts +41 -0
- package/src/urbit/base-url.ts +57 -0
- package/src/urbit/channel-client.ts +157 -0
- package/src/urbit/channel-ops.ts +164 -0
- package/src/urbit/context.ts +47 -0
- package/src/urbit/errors.ts +51 -0
- package/src/urbit/fetch.ts +39 -0
- package/src/urbit/send.test.ts +38 -0
- package/src/urbit/send.ts +131 -0
- package/src/urbit/sse-client.test.ts +44 -0
- package/src/urbit/sse-client.ts +430 -0
package/README.md
ADDED
package/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
2
|
+
import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
|
|
3
|
+
import { tlonPlugin } from "./src/channel.js";
|
|
4
|
+
import { setTlonRuntime } from "./src/runtime.js";
|
|
5
|
+
|
|
6
|
+
const plugin = {
|
|
7
|
+
id: "tlon",
|
|
8
|
+
name: "Tlon",
|
|
9
|
+
description: "Tlon/Urbit channel plugin",
|
|
10
|
+
configSchema: emptyPluginConfigSchema(),
|
|
11
|
+
register(api: OpenClawPluginApi) {
|
|
12
|
+
setTlonRuntime(api.runtime);
|
|
13
|
+
api.registerChannel({ plugin: tlonPlugin });
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default plugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openclaw/tlon",
|
|
3
|
+
"version": "2026.2.21",
|
|
4
|
+
"description": "OpenClaw Tlon/Urbit channel plugin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@urbit/aura": "^3.0.0"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"openclaw": "workspace:*"
|
|
11
|
+
},
|
|
12
|
+
"openclaw": {
|
|
13
|
+
"extensions": [
|
|
14
|
+
"./index.ts"
|
|
15
|
+
],
|
|
16
|
+
"channel": {
|
|
17
|
+
"id": "tlon",
|
|
18
|
+
"label": "Tlon",
|
|
19
|
+
"selectionLabel": "Tlon (Urbit)",
|
|
20
|
+
"docsPath": "/channels/tlon",
|
|
21
|
+
"docsLabel": "tlon",
|
|
22
|
+
"blurb": "decentralized messaging on Urbit; install the plugin to enable.",
|
|
23
|
+
"order": 90,
|
|
24
|
+
"quickstartAllowFrom": true
|
|
25
|
+
},
|
|
26
|
+
"install": {
|
|
27
|
+
"npmSpec": "@openclaw/tlon",
|
|
28
|
+
"localPath": "extensions/tlon",
|
|
29
|
+
"defaultChoice": "npm"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type TlonAccountFieldsInput = {
|
|
2
|
+
ship?: string;
|
|
3
|
+
url?: string;
|
|
4
|
+
code?: string;
|
|
5
|
+
allowPrivateNetwork?: boolean;
|
|
6
|
+
groupChannels?: string[];
|
|
7
|
+
dmAllowlist?: string[];
|
|
8
|
+
autoDiscoverChannels?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function buildTlonAccountFields(input: TlonAccountFieldsInput) {
|
|
12
|
+
return {
|
|
13
|
+
...(input.ship ? { ship: input.ship } : {}),
|
|
14
|
+
...(input.url ? { url: input.url } : {}),
|
|
15
|
+
...(input.code ? { code: input.code } : {}),
|
|
16
|
+
...(typeof input.allowPrivateNetwork === "boolean"
|
|
17
|
+
? { allowPrivateNetwork: input.allowPrivateNetwork }
|
|
18
|
+
: {}),
|
|
19
|
+
...(input.groupChannels ? { groupChannels: input.groupChannels } : {}),
|
|
20
|
+
...(input.dmAllowlist ? { dmAllowlist: input.dmAllowlist } : {}),
|
|
21
|
+
...(typeof input.autoDiscoverChannels === "boolean"
|
|
22
|
+
? { autoDiscoverChannels: input.autoDiscoverChannels }
|
|
23
|
+
: {}),
|
|
24
|
+
};
|
|
25
|
+
}
|
package/src/channel.ts
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChannelAccountSnapshot,
|
|
3
|
+
ChannelOutboundAdapter,
|
|
4
|
+
ChannelPlugin,
|
|
5
|
+
ChannelSetupInput,
|
|
6
|
+
OpenClawConfig,
|
|
7
|
+
} from "openclaw/plugin-sdk";
|
|
8
|
+
import {
|
|
9
|
+
applyAccountNameToChannelSection,
|
|
10
|
+
DEFAULT_ACCOUNT_ID,
|
|
11
|
+
normalizeAccountId,
|
|
12
|
+
} from "openclaw/plugin-sdk";
|
|
13
|
+
import { buildTlonAccountFields } from "./account-fields.js";
|
|
14
|
+
import { tlonChannelConfigSchema } from "./config-schema.js";
|
|
15
|
+
import { monitorTlonProvider } from "./monitor/index.js";
|
|
16
|
+
import { tlonOnboardingAdapter } from "./onboarding.js";
|
|
17
|
+
import { formatTargetHint, normalizeShip, parseTlonTarget } from "./targets.js";
|
|
18
|
+
import { resolveTlonAccount, listTlonAccountIds } from "./types.js";
|
|
19
|
+
import { authenticate } from "./urbit/auth.js";
|
|
20
|
+
import { UrbitChannelClient } from "./urbit/channel-client.js";
|
|
21
|
+
import { ssrfPolicyFromAllowPrivateNetwork } from "./urbit/context.js";
|
|
22
|
+
import { buildMediaText, sendDm, sendGroupMessage } from "./urbit/send.js";
|
|
23
|
+
|
|
24
|
+
const TLON_CHANNEL_ID = "tlon" as const;
|
|
25
|
+
|
|
26
|
+
type TlonSetupInput = ChannelSetupInput & {
|
|
27
|
+
ship?: string;
|
|
28
|
+
url?: string;
|
|
29
|
+
code?: string;
|
|
30
|
+
allowPrivateNetwork?: boolean;
|
|
31
|
+
groupChannels?: string[];
|
|
32
|
+
dmAllowlist?: string[];
|
|
33
|
+
autoDiscoverChannels?: boolean;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function applyTlonSetupConfig(params: {
|
|
37
|
+
cfg: OpenClawConfig;
|
|
38
|
+
accountId: string;
|
|
39
|
+
input: TlonSetupInput;
|
|
40
|
+
}): OpenClawConfig {
|
|
41
|
+
const { cfg, accountId, input } = params;
|
|
42
|
+
const useDefault = accountId === DEFAULT_ACCOUNT_ID;
|
|
43
|
+
const namedConfig = applyAccountNameToChannelSection({
|
|
44
|
+
cfg,
|
|
45
|
+
channelKey: "tlon",
|
|
46
|
+
accountId,
|
|
47
|
+
name: input.name,
|
|
48
|
+
});
|
|
49
|
+
const base = namedConfig.channels?.tlon ?? {};
|
|
50
|
+
|
|
51
|
+
const payload = buildTlonAccountFields(input);
|
|
52
|
+
|
|
53
|
+
if (useDefault) {
|
|
54
|
+
return {
|
|
55
|
+
...namedConfig,
|
|
56
|
+
channels: {
|
|
57
|
+
...namedConfig.channels,
|
|
58
|
+
tlon: {
|
|
59
|
+
...base,
|
|
60
|
+
enabled: true,
|
|
61
|
+
...payload,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
...namedConfig,
|
|
69
|
+
channels: {
|
|
70
|
+
...namedConfig.channels,
|
|
71
|
+
tlon: {
|
|
72
|
+
...base,
|
|
73
|
+
enabled: base.enabled ?? true,
|
|
74
|
+
accounts: {
|
|
75
|
+
...(base as { accounts?: Record<string, unknown> }).accounts,
|
|
76
|
+
[accountId]: {
|
|
77
|
+
...(base as { accounts?: Record<string, Record<string, unknown>> }).accounts?.[
|
|
78
|
+
accountId
|
|
79
|
+
],
|
|
80
|
+
enabled: true,
|
|
81
|
+
...payload,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const tlonOutbound: ChannelOutboundAdapter = {
|
|
90
|
+
deliveryMode: "direct",
|
|
91
|
+
textChunkLimit: 10000,
|
|
92
|
+
resolveTarget: ({ to }) => {
|
|
93
|
+
const parsed = parseTlonTarget(to ?? "");
|
|
94
|
+
if (!parsed) {
|
|
95
|
+
return {
|
|
96
|
+
ok: false,
|
|
97
|
+
error: new Error(`Invalid Tlon target. Use ${formatTargetHint()}`),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (parsed.kind === "direct") {
|
|
101
|
+
return { ok: true, to: parsed.ship };
|
|
102
|
+
}
|
|
103
|
+
return { ok: true, to: parsed.nest };
|
|
104
|
+
},
|
|
105
|
+
sendText: async ({ cfg, to, text, accountId, replyToId, threadId }) => {
|
|
106
|
+
const account = resolveTlonAccount(cfg, accountId ?? undefined);
|
|
107
|
+
if (!account.configured || !account.ship || !account.url || !account.code) {
|
|
108
|
+
throw new Error("Tlon account not configured");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const parsed = parseTlonTarget(to);
|
|
112
|
+
if (!parsed) {
|
|
113
|
+
throw new Error(`Invalid Tlon target. Use ${formatTargetHint()}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const ssrfPolicy = ssrfPolicyFromAllowPrivateNetwork(account.allowPrivateNetwork);
|
|
117
|
+
const cookie = await authenticate(account.url, account.code, { ssrfPolicy });
|
|
118
|
+
const api = new UrbitChannelClient(account.url, cookie, {
|
|
119
|
+
ship: account.ship.replace(/^~/, ""),
|
|
120
|
+
ssrfPolicy,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
const fromShip = normalizeShip(account.ship);
|
|
125
|
+
if (parsed.kind === "direct") {
|
|
126
|
+
return await sendDm({
|
|
127
|
+
api,
|
|
128
|
+
fromShip,
|
|
129
|
+
toShip: parsed.ship,
|
|
130
|
+
text,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const replyId = (replyToId ?? threadId) ? String(replyToId ?? threadId) : undefined;
|
|
134
|
+
return await sendGroupMessage({
|
|
135
|
+
api,
|
|
136
|
+
fromShip,
|
|
137
|
+
hostShip: parsed.hostShip,
|
|
138
|
+
channelName: parsed.channelName,
|
|
139
|
+
text,
|
|
140
|
+
replyToId: replyId,
|
|
141
|
+
});
|
|
142
|
+
} finally {
|
|
143
|
+
await api.close();
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
sendMedia: async ({ cfg, to, text, mediaUrl, accountId, replyToId, threadId }) => {
|
|
147
|
+
const mergedText = buildMediaText(text, mediaUrl);
|
|
148
|
+
return await tlonOutbound.sendText!({
|
|
149
|
+
cfg,
|
|
150
|
+
to,
|
|
151
|
+
text: mergedText,
|
|
152
|
+
accountId,
|
|
153
|
+
replyToId,
|
|
154
|
+
threadId,
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const tlonPlugin: ChannelPlugin = {
|
|
160
|
+
id: TLON_CHANNEL_ID,
|
|
161
|
+
meta: {
|
|
162
|
+
id: TLON_CHANNEL_ID,
|
|
163
|
+
label: "Tlon",
|
|
164
|
+
selectionLabel: "Tlon (Urbit)",
|
|
165
|
+
docsPath: "/channels/tlon",
|
|
166
|
+
docsLabel: "tlon",
|
|
167
|
+
blurb: "Decentralized messaging on Urbit",
|
|
168
|
+
aliases: ["urbit"],
|
|
169
|
+
order: 90,
|
|
170
|
+
},
|
|
171
|
+
capabilities: {
|
|
172
|
+
chatTypes: ["direct", "group", "thread"],
|
|
173
|
+
media: false,
|
|
174
|
+
reply: true,
|
|
175
|
+
threads: true,
|
|
176
|
+
},
|
|
177
|
+
onboarding: tlonOnboardingAdapter,
|
|
178
|
+
reload: { configPrefixes: ["channels.tlon"] },
|
|
179
|
+
configSchema: tlonChannelConfigSchema,
|
|
180
|
+
config: {
|
|
181
|
+
listAccountIds: (cfg) => listTlonAccountIds(cfg),
|
|
182
|
+
resolveAccount: (cfg, accountId) => resolveTlonAccount(cfg, accountId ?? undefined),
|
|
183
|
+
defaultAccountId: () => "default",
|
|
184
|
+
setAccountEnabled: ({ cfg, accountId, enabled }) => {
|
|
185
|
+
const useDefault = !accountId || accountId === "default";
|
|
186
|
+
if (useDefault) {
|
|
187
|
+
return {
|
|
188
|
+
...cfg,
|
|
189
|
+
channels: {
|
|
190
|
+
...cfg.channels,
|
|
191
|
+
tlon: {
|
|
192
|
+
...(cfg.channels?.tlon as Record<string, unknown>),
|
|
193
|
+
enabled,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
} as OpenClawConfig;
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
...cfg,
|
|
200
|
+
channels: {
|
|
201
|
+
...cfg.channels,
|
|
202
|
+
tlon: {
|
|
203
|
+
...(cfg.channels?.tlon as Record<string, unknown>),
|
|
204
|
+
accounts: {
|
|
205
|
+
...cfg.channels?.tlon?.accounts,
|
|
206
|
+
[accountId]: {
|
|
207
|
+
...cfg.channels?.tlon?.accounts?.[accountId],
|
|
208
|
+
enabled,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
} as OpenClawConfig;
|
|
214
|
+
},
|
|
215
|
+
deleteAccount: ({ cfg, accountId }) => {
|
|
216
|
+
const useDefault = !accountId || accountId === "default";
|
|
217
|
+
if (useDefault) {
|
|
218
|
+
// oxlint-disable-next-line no-unused-vars
|
|
219
|
+
const { ship, code, url, name, ...rest } = (cfg.channels?.tlon ?? {}) as Record<
|
|
220
|
+
string,
|
|
221
|
+
unknown
|
|
222
|
+
>;
|
|
223
|
+
return {
|
|
224
|
+
...cfg,
|
|
225
|
+
channels: {
|
|
226
|
+
...cfg.channels,
|
|
227
|
+
tlon: rest,
|
|
228
|
+
},
|
|
229
|
+
} as OpenClawConfig;
|
|
230
|
+
}
|
|
231
|
+
// oxlint-disable-next-line no-unused-vars
|
|
232
|
+
const { [accountId]: removed, ...remainingAccounts } = (cfg.channels?.tlon?.accounts ??
|
|
233
|
+
{}) as Record<string, unknown>;
|
|
234
|
+
return {
|
|
235
|
+
...cfg,
|
|
236
|
+
channels: {
|
|
237
|
+
...cfg.channels,
|
|
238
|
+
tlon: {
|
|
239
|
+
...(cfg.channels?.tlon as Record<string, unknown>),
|
|
240
|
+
accounts: remainingAccounts,
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
} as OpenClawConfig;
|
|
244
|
+
},
|
|
245
|
+
isConfigured: (account) => account.configured,
|
|
246
|
+
describeAccount: (account) => ({
|
|
247
|
+
accountId: account.accountId,
|
|
248
|
+
name: account.name,
|
|
249
|
+
enabled: account.enabled,
|
|
250
|
+
configured: account.configured,
|
|
251
|
+
ship: account.ship,
|
|
252
|
+
url: account.url,
|
|
253
|
+
}),
|
|
254
|
+
},
|
|
255
|
+
setup: {
|
|
256
|
+
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
|
257
|
+
applyAccountName: ({ cfg, accountId, name }) =>
|
|
258
|
+
applyAccountNameToChannelSection({
|
|
259
|
+
cfg: cfg,
|
|
260
|
+
channelKey: "tlon",
|
|
261
|
+
accountId,
|
|
262
|
+
name,
|
|
263
|
+
}),
|
|
264
|
+
validateInput: ({ cfg, accountId, input }) => {
|
|
265
|
+
const setupInput = input as TlonSetupInput;
|
|
266
|
+
const resolved = resolveTlonAccount(cfg, accountId ?? undefined);
|
|
267
|
+
const ship = setupInput.ship?.trim() || resolved.ship;
|
|
268
|
+
const url = setupInput.url?.trim() || resolved.url;
|
|
269
|
+
const code = setupInput.code?.trim() || resolved.code;
|
|
270
|
+
if (!ship) {
|
|
271
|
+
return "Tlon requires --ship.";
|
|
272
|
+
}
|
|
273
|
+
if (!url) {
|
|
274
|
+
return "Tlon requires --url.";
|
|
275
|
+
}
|
|
276
|
+
if (!code) {
|
|
277
|
+
return "Tlon requires --code.";
|
|
278
|
+
}
|
|
279
|
+
return null;
|
|
280
|
+
},
|
|
281
|
+
applyAccountConfig: ({ cfg, accountId, input }) =>
|
|
282
|
+
applyTlonSetupConfig({
|
|
283
|
+
cfg: cfg,
|
|
284
|
+
accountId,
|
|
285
|
+
input: input as TlonSetupInput,
|
|
286
|
+
}),
|
|
287
|
+
},
|
|
288
|
+
messaging: {
|
|
289
|
+
normalizeTarget: (target) => {
|
|
290
|
+
const parsed = parseTlonTarget(target);
|
|
291
|
+
if (!parsed) {
|
|
292
|
+
return target.trim();
|
|
293
|
+
}
|
|
294
|
+
if (parsed.kind === "direct") {
|
|
295
|
+
return parsed.ship;
|
|
296
|
+
}
|
|
297
|
+
return parsed.nest;
|
|
298
|
+
},
|
|
299
|
+
targetResolver: {
|
|
300
|
+
looksLikeId: (target) => Boolean(parseTlonTarget(target)),
|
|
301
|
+
hint: formatTargetHint(),
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
outbound: tlonOutbound,
|
|
305
|
+
status: {
|
|
306
|
+
defaultRuntime: {
|
|
307
|
+
accountId: "default",
|
|
308
|
+
running: false,
|
|
309
|
+
lastStartAt: null,
|
|
310
|
+
lastStopAt: null,
|
|
311
|
+
lastError: null,
|
|
312
|
+
},
|
|
313
|
+
collectStatusIssues: (accounts) => {
|
|
314
|
+
return accounts.flatMap((account) => {
|
|
315
|
+
if (!account.configured) {
|
|
316
|
+
return [
|
|
317
|
+
{
|
|
318
|
+
channel: TLON_CHANNEL_ID,
|
|
319
|
+
accountId: account.accountId,
|
|
320
|
+
kind: "config",
|
|
321
|
+
message: "Account not configured (missing ship, code, or url)",
|
|
322
|
+
},
|
|
323
|
+
];
|
|
324
|
+
}
|
|
325
|
+
return [];
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
buildChannelSummary: ({ snapshot }) => ({
|
|
329
|
+
configured: snapshot.configured ?? false,
|
|
330
|
+
ship: (snapshot as { ship?: string | null }).ship ?? null,
|
|
331
|
+
url: (snapshot as { url?: string | null }).url ?? null,
|
|
332
|
+
}),
|
|
333
|
+
probeAccount: async ({ account }) => {
|
|
334
|
+
if (!account.configured || !account.ship || !account.url || !account.code) {
|
|
335
|
+
return { ok: false, error: "Not configured" };
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
const ssrfPolicy = ssrfPolicyFromAllowPrivateNetwork(account.allowPrivateNetwork);
|
|
339
|
+
const cookie = await authenticate(account.url, account.code, { ssrfPolicy });
|
|
340
|
+
const api = new UrbitChannelClient(account.url, cookie, {
|
|
341
|
+
ship: account.ship.replace(/^~/, ""),
|
|
342
|
+
ssrfPolicy,
|
|
343
|
+
});
|
|
344
|
+
try {
|
|
345
|
+
await api.getOurName();
|
|
346
|
+
return { ok: true };
|
|
347
|
+
} finally {
|
|
348
|
+
await api.close();
|
|
349
|
+
}
|
|
350
|
+
} catch (error) {
|
|
351
|
+
return { ok: false, error: (error as { message?: string })?.message ?? String(error) };
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
buildAccountSnapshot: ({ account, runtime, probe }) => ({
|
|
355
|
+
accountId: account.accountId,
|
|
356
|
+
name: account.name,
|
|
357
|
+
enabled: account.enabled,
|
|
358
|
+
configured: account.configured,
|
|
359
|
+
ship: account.ship,
|
|
360
|
+
url: account.url,
|
|
361
|
+
running: runtime?.running ?? false,
|
|
362
|
+
lastStartAt: runtime?.lastStartAt ?? null,
|
|
363
|
+
lastStopAt: runtime?.lastStopAt ?? null,
|
|
364
|
+
lastError: runtime?.lastError ?? null,
|
|
365
|
+
probe,
|
|
366
|
+
}),
|
|
367
|
+
},
|
|
368
|
+
gateway: {
|
|
369
|
+
startAccount: async (ctx) => {
|
|
370
|
+
const account = ctx.account;
|
|
371
|
+
ctx.setStatus({
|
|
372
|
+
accountId: account.accountId,
|
|
373
|
+
ship: account.ship,
|
|
374
|
+
url: account.url,
|
|
375
|
+
} as ChannelAccountSnapshot);
|
|
376
|
+
ctx.log?.info(`[${account.accountId}] starting Tlon provider for ${account.ship ?? "tlon"}`);
|
|
377
|
+
return monitorTlonProvider({
|
|
378
|
+
runtime: ctx.runtime,
|
|
379
|
+
abortSignal: ctx.abortSignal,
|
|
380
|
+
accountId: account.accountId,
|
|
381
|
+
});
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { TlonAuthorizationSchema, TlonConfigSchema } from "./config-schema.js";
|
|
3
|
+
|
|
4
|
+
describe("Tlon config schema", () => {
|
|
5
|
+
it("accepts channelRules with string keys", () => {
|
|
6
|
+
const parsed = TlonAuthorizationSchema.parse({
|
|
7
|
+
channelRules: {
|
|
8
|
+
"chat/~zod/test": {
|
|
9
|
+
mode: "open",
|
|
10
|
+
allowedShips: ["~zod"],
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
expect(parsed.channelRules?.["chat/~zod/test"]?.mode).toBe("open");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("accepts accounts with string keys", () => {
|
|
19
|
+
const parsed = TlonConfigSchema.parse({
|
|
20
|
+
accounts: {
|
|
21
|
+
primary: {
|
|
22
|
+
ship: "~zod",
|
|
23
|
+
url: "https://example.com",
|
|
24
|
+
code: "code-123",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(parsed.accounts?.primary?.ship).toBe("~zod");
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { buildChannelConfigSchema } from "openclaw/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
const ShipSchema = z.string().min(1);
|
|
5
|
+
const ChannelNestSchema = z.string().min(1);
|
|
6
|
+
|
|
7
|
+
export const TlonChannelRuleSchema = z.object({
|
|
8
|
+
mode: z.enum(["restricted", "open"]).optional(),
|
|
9
|
+
allowedShips: z.array(ShipSchema).optional(),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const TlonAuthorizationSchema = z.object({
|
|
13
|
+
channelRules: z.record(z.string(), TlonChannelRuleSchema).optional(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const TlonAccountSchema = z.object({
|
|
17
|
+
name: z.string().optional(),
|
|
18
|
+
enabled: z.boolean().optional(),
|
|
19
|
+
ship: ShipSchema.optional(),
|
|
20
|
+
url: z.string().optional(),
|
|
21
|
+
code: z.string().optional(),
|
|
22
|
+
allowPrivateNetwork: z.boolean().optional(),
|
|
23
|
+
groupChannels: z.array(ChannelNestSchema).optional(),
|
|
24
|
+
dmAllowlist: z.array(ShipSchema).optional(),
|
|
25
|
+
autoDiscoverChannels: z.boolean().optional(),
|
|
26
|
+
showModelSignature: z.boolean().optional(),
|
|
27
|
+
responsePrefix: z.string().optional(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export const TlonConfigSchema = z.object({
|
|
31
|
+
name: z.string().optional(),
|
|
32
|
+
enabled: z.boolean().optional(),
|
|
33
|
+
ship: ShipSchema.optional(),
|
|
34
|
+
url: z.string().optional(),
|
|
35
|
+
code: z.string().optional(),
|
|
36
|
+
allowPrivateNetwork: z.boolean().optional(),
|
|
37
|
+
groupChannels: z.array(ChannelNestSchema).optional(),
|
|
38
|
+
dmAllowlist: z.array(ShipSchema).optional(),
|
|
39
|
+
autoDiscoverChannels: z.boolean().optional(),
|
|
40
|
+
showModelSignature: z.boolean().optional(),
|
|
41
|
+
responsePrefix: z.string().optional(),
|
|
42
|
+
authorization: TlonAuthorizationSchema.optional(),
|
|
43
|
+
defaultAuthorizedShips: z.array(ShipSchema).optional(),
|
|
44
|
+
accounts: z.record(z.string(), TlonAccountSchema).optional(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const tlonChannelConfigSchema = buildChannelConfigSchema(TlonConfigSchema);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { RuntimeEnv } from "openclaw/plugin-sdk";
|
|
2
|
+
import { formatChangesDate } from "./utils.js";
|
|
3
|
+
|
|
4
|
+
export async function fetchGroupChanges(
|
|
5
|
+
api: { scry: (path: string) => Promise<unknown> },
|
|
6
|
+
runtime: RuntimeEnv,
|
|
7
|
+
daysAgo = 5,
|
|
8
|
+
) {
|
|
9
|
+
try {
|
|
10
|
+
const changeDate = formatChangesDate(daysAgo);
|
|
11
|
+
runtime.log?.(`[tlon] Fetching group changes since ${daysAgo} days ago (${changeDate})...`);
|
|
12
|
+
const changes = await api.scry(`/groups-ui/v5/changes/${changeDate}.json`);
|
|
13
|
+
if (changes) {
|
|
14
|
+
runtime.log?.("[tlon] Successfully fetched changes data");
|
|
15
|
+
return changes;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
runtime.log?.(
|
|
20
|
+
`[tlon] Failed to fetch changes (falling back to full init): ${(error as { message?: string })?.message ?? String(error)}`,
|
|
21
|
+
);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function fetchAllChannels(
|
|
27
|
+
api: { scry: (path: string) => Promise<unknown> },
|
|
28
|
+
runtime: RuntimeEnv,
|
|
29
|
+
): Promise<string[]> {
|
|
30
|
+
try {
|
|
31
|
+
runtime.log?.("[tlon] Attempting auto-discovery of group channels...");
|
|
32
|
+
const changes = await fetchGroupChanges(api, runtime, 5);
|
|
33
|
+
|
|
34
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
35
|
+
let initData: any;
|
|
36
|
+
if (changes) {
|
|
37
|
+
runtime.log?.("[tlon] Changes data received, using full init for channel extraction");
|
|
38
|
+
initData = await api.scry("/groups-ui/v6/init.json");
|
|
39
|
+
} else {
|
|
40
|
+
initData = await api.scry("/groups-ui/v6/init.json");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const channels: string[] = [];
|
|
44
|
+
if (initData && initData.groups) {
|
|
45
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
46
|
+
for (const groupData of Object.values(initData.groups as Record<string, any>)) {
|
|
47
|
+
if (groupData && typeof groupData === "object" && groupData.channels) {
|
|
48
|
+
for (const channelNest of Object.keys(groupData.channels)) {
|
|
49
|
+
if (channelNest.startsWith("chat/")) {
|
|
50
|
+
channels.push(channelNest);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (channels.length > 0) {
|
|
58
|
+
runtime.log?.(`[tlon] Auto-discovered ${channels.length} chat channel(s)`);
|
|
59
|
+
runtime.log?.(
|
|
60
|
+
`[tlon] Channels: ${channels.slice(0, 5).join(", ")}${channels.length > 5 ? "..." : ""}`,
|
|
61
|
+
);
|
|
62
|
+
} else {
|
|
63
|
+
runtime.log?.("[tlon] No chat channels found via auto-discovery");
|
|
64
|
+
runtime.log?.("[tlon] Add channels manually to config: channels.tlon.groupChannels");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return channels;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
runtime.log?.(
|
|
70
|
+
`[tlon] Auto-discovery failed: ${(error as { message?: string })?.message ?? String(error)}`,
|
|
71
|
+
);
|
|
72
|
+
runtime.log?.(
|
|
73
|
+
"[tlon] To monitor group channels, add them to config: channels.tlon.groupChannels",
|
|
74
|
+
);
|
|
75
|
+
runtime.log?.('[tlon] Example: ["chat/~host-ship/channel-name"]');
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|