@timeatlas/openclaw-timeatlas 0.0.0-stage → 0.1.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.
- package/README.md +31 -2
- package/dist/index.js +8 -0
- package/dist/setup-entry.js +3 -0
- package/dist/src/channel.js +90 -0
- package/dist/src/link.js +224 -0
- package/dist/src/token-store.js +29 -0
- package/index.ts +9 -0
- package/openclaw.plugin.json +32 -0
- package/package.json +43 -4
- package/setup-entry.ts +4 -0
- package/src/channel.ts +117 -0
- package/src/link.ts +274 -0
- package/src/token-store.ts +46 -0
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Time Atlas channel for OpenClaw
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Makes Pat, the Time Atlas chat, an agent: the user's chat messages go to
|
|
4
|
+
their OpenClaw, and its answers come back to the app.
|
|
5
|
+
|
|
6
|
+
1. The app asks the proxy for a pairing code (Make Pat an Agent).
|
|
7
|
+
2. The user sets it here, and restarts the gateway:
|
|
8
|
+
|
|
9
|
+
openclaw plugins install npm:@timeatlas/openclaw-timeatlas
|
|
10
|
+
openclaw config set channels.timeatlas.pairingCode ABCD-EFGH
|
|
11
|
+
openclaw gateway restart
|
|
12
|
+
|
|
13
|
+
3. The plugin trades the code at `POST /agent/pair` for a token, kept in
|
|
14
|
+
OpenClaw's state directory (`timeatlas/<account>.json`).
|
|
15
|
+
4. It long polls `GET /agent/poll` for the user's messages, hands each to
|
|
16
|
+
the agent, and posts typing, text and done to `POST /agent/events`.
|
|
17
|
+
|
|
18
|
+
`channels.timeatlas.proxyUrl` points it at another proxy, for testing.
|
|
19
|
+
|
|
20
|
+
For development, `openclaw plugins install --link ./openclaw/timeatlas-channel`.
|
|
21
|
+
|
|
22
|
+
Type check with `npm install && npm run typecheck`.
|
|
23
|
+
|
|
24
|
+
OpenClaw loads the TypeScript entries from a checkout, and the JavaScript
|
|
25
|
+
`npm run build` writes to `dist/` from an installed package
|
|
26
|
+
(`runtimeExtensions`). Publishing builds it first:
|
|
27
|
+
|
|
28
|
+
npm version patch
|
|
29
|
+
npm publish
|
|
30
|
+
|
|
31
|
+
OpenClaw installs package names from ClawHub, so the npm package is
|
|
32
|
+
installed with the `npm:` prefix.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineChannelPluginEntry } from "openclaw/plugin-sdk/channel-core";
|
|
2
|
+
import { timeAtlasPlugin } from "./src/channel.js";
|
|
3
|
+
export default defineChannelPluginEntry({
|
|
4
|
+
id: "timeatlas",
|
|
5
|
+
name: "Time Atlas",
|
|
6
|
+
description: "Time Atlas channel: your Time Atlas chat answered by your agent",
|
|
7
|
+
plugin: timeAtlasPlugin,
|
|
8
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createChannelPluginBase, createChatChannelPlugin, } from "openclaw/plugin-sdk/channel-core";
|
|
2
|
+
import { CHANNEL_ID, DEFAULT_PROXY_URL, linkFor, ProxyLink, } from "./link.js";
|
|
3
|
+
const DEFAULT_ACCOUNT_ID = "default";
|
|
4
|
+
function section(cfg) {
|
|
5
|
+
return (cfg.channels?.[CHANNEL_ID] ??
|
|
6
|
+
{});
|
|
7
|
+
}
|
|
8
|
+
function resolveAccount(cfg, accountId) {
|
|
9
|
+
const config = section(cfg);
|
|
10
|
+
return {
|
|
11
|
+
accountId: accountId ?? DEFAULT_ACCOUNT_ID,
|
|
12
|
+
pairingCode: (config.pairingCode ?? "").trim().toUpperCase(),
|
|
13
|
+
proxyUrl: (config.proxyUrl ?? DEFAULT_PROXY_URL).replace(/\/+$/, ""),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const config = {
|
|
17
|
+
// One Time Atlas user per OpenClaw.
|
|
18
|
+
listAccountIds: () => [DEFAULT_ACCOUNT_ID],
|
|
19
|
+
resolveAccount,
|
|
20
|
+
inspectAccount(cfg) {
|
|
21
|
+
const config = section(cfg);
|
|
22
|
+
return {
|
|
23
|
+
enabled: true,
|
|
24
|
+
// Paired already, or about to with a code.
|
|
25
|
+
configured: Boolean(config.pairingCode),
|
|
26
|
+
tokenStatus: config.pairingCode ? "available" : "missing",
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
const chatPlugin = createChatChannelPlugin({
|
|
31
|
+
base: {
|
|
32
|
+
...createChannelPluginBase({
|
|
33
|
+
id: CHANNEL_ID,
|
|
34
|
+
setup: {
|
|
35
|
+
applyAccountConfig: ({ cfg, input }) => ({
|
|
36
|
+
...cfg,
|
|
37
|
+
channels: {
|
|
38
|
+
...cfg.channels,
|
|
39
|
+
[CHANNEL_ID]: { ...section(cfg), ...input },
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
// Given here rather than to the base helper, whose result leaves it
|
|
45
|
+
// optional where the plugin needs it.
|
|
46
|
+
config,
|
|
47
|
+
},
|
|
48
|
+
// The proxy only hands over messages of the user who paired, so whoever
|
|
49
|
+
// comes in on this channel is them.
|
|
50
|
+
security: {
|
|
51
|
+
dm: {
|
|
52
|
+
channelKey: CHANNEL_ID,
|
|
53
|
+
resolvePolicy: () => "open",
|
|
54
|
+
resolveAllowFrom: () => ["*"],
|
|
55
|
+
defaultPolicy: "open",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
threading: { topLevelReplyToMode: "reply" },
|
|
59
|
+
outbound: {
|
|
60
|
+
base: { deliveryMode: "direct" },
|
|
61
|
+
attachedResults: {
|
|
62
|
+
channel: CHANNEL_ID,
|
|
63
|
+
// What the agent sends unasked. The proxy adds it to the answer the
|
|
64
|
+
// app is waiting for, if any; there is no way yet to reach the user
|
|
65
|
+
// otherwise.
|
|
66
|
+
sendText: async ({ accountId, text }) => {
|
|
67
|
+
const link = linkFor(accountId);
|
|
68
|
+
if (!link) {
|
|
69
|
+
throw new Error("timeatlas: not connected to Time Atlas");
|
|
70
|
+
}
|
|
71
|
+
await link.post([{ type: "text", text }]);
|
|
72
|
+
return { messageId: `timeatlas-${Date.now()}` };
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
export const timeAtlasPlugin = {
|
|
78
|
+
...chatPlugin,
|
|
79
|
+
gateway: {
|
|
80
|
+
...chatPlugin.gateway,
|
|
81
|
+
startAccount: async (ctx) => {
|
|
82
|
+
const log = {
|
|
83
|
+
info: (message) => ctx.log?.info?.(message),
|
|
84
|
+
warn: (message) => ctx.log?.warn?.(message),
|
|
85
|
+
error: (message) => ctx.log?.error?.(message),
|
|
86
|
+
};
|
|
87
|
+
await new ProxyLink(ctx.cfg, ctx.account, ctx.abortSignal, log).run();
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
};
|
package/dist/src/link.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { dispatchInboundDirectDm } from "openclaw/plugin-sdk/channel-inbound";
|
|
2
|
+
import { forgetToken, loadToken, saveToken } from "./token-store.js";
|
|
3
|
+
export const CHANNEL_ID = "timeatlas";
|
|
4
|
+
export const DEFAULT_PROXY_URL = "https://ai.timeatlas.app";
|
|
5
|
+
/** The one person on the other end: the Time Atlas user who paired. */
|
|
6
|
+
const USER_PEER_ID = "timeatlas-user";
|
|
7
|
+
const MIN_BACKOFF_MS = 1_000;
|
|
8
|
+
const MAX_BACKOFF_MS = 30_000;
|
|
9
|
+
/** Longer than the proxy holds a poll open. */
|
|
10
|
+
const POLL_TIMEOUT_MS = 40_000;
|
|
11
|
+
class Unauthorized extends Error {
|
|
12
|
+
}
|
|
13
|
+
/** The links running, by account, for what the agent sends unasked. */
|
|
14
|
+
const links = new Map();
|
|
15
|
+
export function linkFor(accountId) {
|
|
16
|
+
return links.get(accountId ?? "default");
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* ProxyLink is one account's connection to the Time Atlas proxy. It pairs
|
|
20
|
+
* with the code from the app, then polls for the user's chat messages,
|
|
21
|
+
* hands each to the agent, and posts back what the agent answers.
|
|
22
|
+
*/
|
|
23
|
+
export class ProxyLink {
|
|
24
|
+
cfg;
|
|
25
|
+
account;
|
|
26
|
+
abortSignal;
|
|
27
|
+
log;
|
|
28
|
+
token;
|
|
29
|
+
/** Messages are answered one after another, in the order they came. */
|
|
30
|
+
queue = Promise.resolve();
|
|
31
|
+
constructor(cfg, account, abortSignal, log) {
|
|
32
|
+
this.cfg = cfg;
|
|
33
|
+
this.account = account;
|
|
34
|
+
this.abortSignal = abortSignal;
|
|
35
|
+
this.log = log;
|
|
36
|
+
}
|
|
37
|
+
/** Runs until the account is stopped. */
|
|
38
|
+
async run() {
|
|
39
|
+
links.set(this.account.accountId, this);
|
|
40
|
+
try {
|
|
41
|
+
await this.runUntilStopped();
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
if (links.get(this.account.accountId) === this) {
|
|
45
|
+
links.delete(this.account.accountId);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async runUntilStopped() {
|
|
50
|
+
let backoff = MIN_BACKOFF_MS;
|
|
51
|
+
while (!this.abortSignal.aborted) {
|
|
52
|
+
try {
|
|
53
|
+
if (!this.token) {
|
|
54
|
+
this.token = await this.pairedToken();
|
|
55
|
+
if (!this.token) {
|
|
56
|
+
this.log.warn("timeatlas: no pairing code. Get one from Make Pat an Agent in " +
|
|
57
|
+
"the Time Atlas app and set channels.timeatlas.pairingCode.");
|
|
58
|
+
await this.stopped();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const messages = await this.poll();
|
|
63
|
+
backoff = MIN_BACKOFF_MS;
|
|
64
|
+
for (const message of messages) {
|
|
65
|
+
this.queue = this.queue.then(() => this.answer(message));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
if (this.abortSignal.aborted) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (err instanceof Unauthorized) {
|
|
73
|
+
this.log.error("timeatlas: disconnected from Time Atlas. Pair again with a new " +
|
|
74
|
+
"code from the app.");
|
|
75
|
+
this.token = undefined;
|
|
76
|
+
await forgetToken(this.account.accountId);
|
|
77
|
+
await this.stopped();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.log.warn(`timeatlas: ${String(err)}, trying again`);
|
|
81
|
+
await this.sleep(backoff);
|
|
82
|
+
backoff = Math.min(backoff * 2, MAX_BACKOFF_MS);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The token to poll with: the one kept, unless the config has a code it
|
|
88
|
+
* was not paired with, which is traded for a new one.
|
|
89
|
+
*/
|
|
90
|
+
async pairedToken() {
|
|
91
|
+
const accountId = this.account.accountId;
|
|
92
|
+
const code = this.account.pairingCode;
|
|
93
|
+
const stored = await loadToken(accountId);
|
|
94
|
+
if (stored && (!code || stored.pairedWithCode === code)) {
|
|
95
|
+
return stored.token;
|
|
96
|
+
}
|
|
97
|
+
if (!code) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/pair`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
headers: { "Content-Type": "application/json" },
|
|
103
|
+
body: JSON.stringify({ code }),
|
|
104
|
+
signal: this.abortSignal,
|
|
105
|
+
});
|
|
106
|
+
const body = (await response.json().catch(() => ({})));
|
|
107
|
+
if (!response.ok || !body.token) {
|
|
108
|
+
throw new Error(`pairing failed: ${body.error ?? response.statusText}`);
|
|
109
|
+
}
|
|
110
|
+
await saveToken(accountId, { token: body.token, pairedWithCode: code });
|
|
111
|
+
this.log.info("timeatlas: paired with Time Atlas");
|
|
112
|
+
return body.token;
|
|
113
|
+
}
|
|
114
|
+
async poll() {
|
|
115
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/poll`, {
|
|
116
|
+
headers: { Authorization: `Bearer ${this.token}` },
|
|
117
|
+
signal: AbortSignal.any([
|
|
118
|
+
this.abortSignal,
|
|
119
|
+
AbortSignal.timeout(POLL_TIMEOUT_MS),
|
|
120
|
+
]),
|
|
121
|
+
});
|
|
122
|
+
if (response.status === 401) {
|
|
123
|
+
throw new Unauthorized();
|
|
124
|
+
}
|
|
125
|
+
if (!response.ok) {
|
|
126
|
+
throw new Error(`poll: ${response.status} ${response.statusText}`);
|
|
127
|
+
}
|
|
128
|
+
const body = (await response.json());
|
|
129
|
+
return body.messages ?? [];
|
|
130
|
+
}
|
|
131
|
+
/** Tells the proxy what the agent says. */
|
|
132
|
+
async post(events) {
|
|
133
|
+
if (!this.token) {
|
|
134
|
+
throw new Error("not paired");
|
|
135
|
+
}
|
|
136
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/events`, {
|
|
137
|
+
method: "POST",
|
|
138
|
+
headers: {
|
|
139
|
+
Authorization: `Bearer ${this.token}`,
|
|
140
|
+
"Content-Type": "application/json",
|
|
141
|
+
},
|
|
142
|
+
body: JSON.stringify({ events }),
|
|
143
|
+
signal: this.abortSignal,
|
|
144
|
+
});
|
|
145
|
+
if (response.status === 401) {
|
|
146
|
+
throw new Unauthorized();
|
|
147
|
+
}
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
throw new Error(`events: ${response.status} ${response.statusText}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Has the agent answer the message. It types from when the message comes
|
|
154
|
+
* until the whole turn is over, which is when the proxy hears it is done.
|
|
155
|
+
*/
|
|
156
|
+
async answer(message) {
|
|
157
|
+
const replyTo = message.id;
|
|
158
|
+
let said = false;
|
|
159
|
+
try {
|
|
160
|
+
await this.post([{ type: "typing", replyTo, on: true }]);
|
|
161
|
+
await dispatchInboundDirectDm({
|
|
162
|
+
cfg: this.cfg,
|
|
163
|
+
channel: CHANNEL_ID,
|
|
164
|
+
channelLabel: "Time Atlas",
|
|
165
|
+
accountId: this.account.accountId,
|
|
166
|
+
peer: { kind: "direct", id: USER_PEER_ID },
|
|
167
|
+
senderId: USER_PEER_ID,
|
|
168
|
+
senderAddress: `${CHANNEL_ID}:${USER_PEER_ID}`,
|
|
169
|
+
recipientAddress: `${CHANNEL_ID}:pat`,
|
|
170
|
+
conversationLabel: "Time Atlas",
|
|
171
|
+
rawBody: message.text,
|
|
172
|
+
messageId: message.id,
|
|
173
|
+
timestamp: message.timestamp,
|
|
174
|
+
// The proxy only hands over the paired user's messages.
|
|
175
|
+
commandAuthorized: true,
|
|
176
|
+
inboundAccessAuthorized: true,
|
|
177
|
+
channelIngress: "unsupported",
|
|
178
|
+
deliver: async (payload) => {
|
|
179
|
+
const text = payload.text?.trim();
|
|
180
|
+
if (!text) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// Each reply of the turn is a paragraph of one answer.
|
|
184
|
+
await this.post([
|
|
185
|
+
{ type: "text", replyTo, text: said ? `\n\n${text}` : text },
|
|
186
|
+
]);
|
|
187
|
+
said = true;
|
|
188
|
+
},
|
|
189
|
+
onRecordError: (err) => this.log.warn(`timeatlas: could not record the turn: ${String(err)}`),
|
|
190
|
+
onDispatchError: (err, info) => this.log.error(`timeatlas: ${info.kind} failed: ${String(err)}`),
|
|
191
|
+
});
|
|
192
|
+
await this.post([
|
|
193
|
+
{ type: "typing", replyTo, on: false },
|
|
194
|
+
{ type: "done", replyTo },
|
|
195
|
+
]);
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
this.log.error(`timeatlas: could not answer: ${String(err)}`);
|
|
199
|
+
await this.post([
|
|
200
|
+
{ type: "error", replyTo, text: "The agent ran into a problem." },
|
|
201
|
+
]).catch(() => { });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
sleep(ms) {
|
|
205
|
+
return new Promise((resolve) => {
|
|
206
|
+
const timer = setTimeout(resolve, ms);
|
|
207
|
+
this.abortSignal.addEventListener("abort", () => {
|
|
208
|
+
clearTimeout(timer);
|
|
209
|
+
resolve();
|
|
210
|
+
}, { once: true });
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
stopped() {
|
|
214
|
+
return new Promise((resolve) => {
|
|
215
|
+
if (this.abortSignal.aborted) {
|
|
216
|
+
resolve();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
this.abortSignal.addEventListener("abort", () => resolve(), {
|
|
220
|
+
once: true,
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";
|
|
4
|
+
function tokenPath(accountId) {
|
|
5
|
+
return path.join(resolveStateDir(), "timeatlas", `${accountId}.json`);
|
|
6
|
+
}
|
|
7
|
+
export async function loadToken(accountId) {
|
|
8
|
+
try {
|
|
9
|
+
const stored = JSON.parse(await readFile(tokenPath(accountId), "utf8"));
|
|
10
|
+
if (typeof stored?.token === "string" && stored.token) {
|
|
11
|
+
return {
|
|
12
|
+
token: stored.token,
|
|
13
|
+
pairedWithCode: String(stored.pairedWithCode ?? ""),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// None yet.
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
export async function saveToken(accountId, stored) {
|
|
23
|
+
const file = tokenPath(accountId);
|
|
24
|
+
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
25
|
+
await writeFile(file, JSON.stringify(stored), { mode: 0o600 });
|
|
26
|
+
}
|
|
27
|
+
export async function forgetToken(accountId) {
|
|
28
|
+
await rm(tokenPath(accountId), { force: true });
|
|
29
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineChannelPluginEntry } from "openclaw/plugin-sdk/channel-core";
|
|
2
|
+
import { timeAtlasPlugin } from "./src/channel.js";
|
|
3
|
+
|
|
4
|
+
export default defineChannelPluginEntry({
|
|
5
|
+
id: "timeatlas",
|
|
6
|
+
name: "Time Atlas",
|
|
7
|
+
description: "Time Atlas channel: your Time Atlas chat answered by your agent",
|
|
8
|
+
plugin: timeAtlasPlugin,
|
|
9
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "timeatlas",
|
|
3
|
+
"channels": ["timeatlas"],
|
|
4
|
+
"name": "Time Atlas",
|
|
5
|
+
"description": "Time Atlas channel: your Time Atlas chat answered by your agent",
|
|
6
|
+
"configSchema": {
|
|
7
|
+
"type": "object",
|
|
8
|
+
"additionalProperties": false,
|
|
9
|
+
"properties": {}
|
|
10
|
+
},
|
|
11
|
+
"channelConfigs": {
|
|
12
|
+
"timeatlas": {
|
|
13
|
+
"schema": {
|
|
14
|
+
"type": "object",
|
|
15
|
+
"additionalProperties": false,
|
|
16
|
+
"properties": {
|
|
17
|
+
"pairingCode": { "type": "string" },
|
|
18
|
+
"proxyUrl": { "type": "string" }
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"uiHints": {
|
|
22
|
+
"pairingCode": {
|
|
23
|
+
"label": "Pairing code from the Time Atlas app",
|
|
24
|
+
"sensitive": true
|
|
25
|
+
},
|
|
26
|
+
"proxyUrl": {
|
|
27
|
+
"label": "Time Atlas server"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@timeatlas/openclaw-timeatlas",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Time Atlas channel for OpenClaw: Pat, the Time Atlas chat, as an agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"openclaw": {
|
|
7
|
+
"extensions": [
|
|
8
|
+
"./index.ts"
|
|
9
|
+
],
|
|
10
|
+
"runtimeExtensions": [
|
|
11
|
+
"./dist/index.js"
|
|
12
|
+
],
|
|
13
|
+
"setupEntry": "./setup-entry.ts",
|
|
14
|
+
"runtimeSetupEntry": "./dist/setup-entry.js",
|
|
15
|
+
"channel": {
|
|
16
|
+
"id": "timeatlas",
|
|
17
|
+
"label": "Time Atlas",
|
|
18
|
+
"blurb": "Answer your Time Atlas chat with your OpenClaw agent."
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"openclaw": ">=2026.11.10"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "22.20.7",
|
|
26
|
+
"openclaw": "2026.11.10",
|
|
27
|
+
"typescript": "5.9.3"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "rm -rf dist && tsc",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"prepublishOnly": "npm run build"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"index.ts",
|
|
37
|
+
"setup-entry.ts",
|
|
38
|
+
"src",
|
|
39
|
+
"openclaw.plugin.json",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/setup-entry.ts
ADDED
package/src/channel.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createChannelPluginBase,
|
|
3
|
+
createChatChannelPlugin,
|
|
4
|
+
} from "openclaw/plugin-sdk/channel-core";
|
|
5
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/channel-core";
|
|
6
|
+
import {
|
|
7
|
+
CHANNEL_ID,
|
|
8
|
+
DEFAULT_PROXY_URL,
|
|
9
|
+
linkFor,
|
|
10
|
+
ProxyLink,
|
|
11
|
+
type ResolvedAccount,
|
|
12
|
+
} from "./link.js";
|
|
13
|
+
|
|
14
|
+
const DEFAULT_ACCOUNT_ID = "default";
|
|
15
|
+
|
|
16
|
+
type TimeAtlasSection = {
|
|
17
|
+
pairingCode?: string;
|
|
18
|
+
proxyUrl?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function section(cfg: OpenClawConfig): TimeAtlasSection {
|
|
22
|
+
return ((cfg.channels as Record<string, unknown> | undefined)?.[CHANNEL_ID] ??
|
|
23
|
+
{}) as TimeAtlasSection;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function resolveAccount(
|
|
27
|
+
cfg: OpenClawConfig,
|
|
28
|
+
accountId?: string | null,
|
|
29
|
+
): ResolvedAccount {
|
|
30
|
+
const config = section(cfg);
|
|
31
|
+
return {
|
|
32
|
+
accountId: accountId ?? DEFAULT_ACCOUNT_ID,
|
|
33
|
+
pairingCode: (config.pairingCode ?? "").trim().toUpperCase(),
|
|
34
|
+
proxyUrl: (config.proxyUrl ?? DEFAULT_PROXY_URL).replace(/\/+$/, ""),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const config = {
|
|
39
|
+
// One Time Atlas user per OpenClaw.
|
|
40
|
+
listAccountIds: () => [DEFAULT_ACCOUNT_ID],
|
|
41
|
+
resolveAccount,
|
|
42
|
+
inspectAccount(cfg: OpenClawConfig) {
|
|
43
|
+
const config = section(cfg);
|
|
44
|
+
return {
|
|
45
|
+
enabled: true,
|
|
46
|
+
// Paired already, or about to with a code.
|
|
47
|
+
configured: Boolean(config.pairingCode),
|
|
48
|
+
tokenStatus: config.pairingCode ? "available" : "missing",
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const chatPlugin = createChatChannelPlugin<ResolvedAccount>({
|
|
54
|
+
base: {
|
|
55
|
+
...createChannelPluginBase<ResolvedAccount>({
|
|
56
|
+
id: CHANNEL_ID,
|
|
57
|
+
setup: {
|
|
58
|
+
applyAccountConfig: ({ cfg, input }) => ({
|
|
59
|
+
...cfg,
|
|
60
|
+
channels: {
|
|
61
|
+
...cfg.channels,
|
|
62
|
+
[CHANNEL_ID]: { ...section(cfg), ...input },
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
// Given here rather than to the base helper, whose result leaves it
|
|
68
|
+
// optional where the plugin needs it.
|
|
69
|
+
config,
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
// The proxy only hands over messages of the user who paired, so whoever
|
|
73
|
+
// comes in on this channel is them.
|
|
74
|
+
security: {
|
|
75
|
+
dm: {
|
|
76
|
+
channelKey: CHANNEL_ID,
|
|
77
|
+
resolvePolicy: () => "open",
|
|
78
|
+
resolveAllowFrom: () => ["*"],
|
|
79
|
+
defaultPolicy: "open",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
threading: { topLevelReplyToMode: "reply" },
|
|
84
|
+
|
|
85
|
+
outbound: {
|
|
86
|
+
base: { deliveryMode: "direct" },
|
|
87
|
+
attachedResults: {
|
|
88
|
+
channel: CHANNEL_ID,
|
|
89
|
+
// What the agent sends unasked. The proxy adds it to the answer the
|
|
90
|
+
// app is waiting for, if any; there is no way yet to reach the user
|
|
91
|
+
// otherwise.
|
|
92
|
+
sendText: async ({ accountId, text }) => {
|
|
93
|
+
const link = linkFor(accountId);
|
|
94
|
+
if (!link) {
|
|
95
|
+
throw new Error("timeatlas: not connected to Time Atlas");
|
|
96
|
+
}
|
|
97
|
+
await link.post([{ type: "text", text }]);
|
|
98
|
+
return { messageId: `timeatlas-${Date.now()}` };
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
export const timeAtlasPlugin: typeof chatPlugin = {
|
|
105
|
+
...chatPlugin,
|
|
106
|
+
gateway: {
|
|
107
|
+
...chatPlugin.gateway,
|
|
108
|
+
startAccount: async (ctx) => {
|
|
109
|
+
const log = {
|
|
110
|
+
info: (message: string) => ctx.log?.info?.(message),
|
|
111
|
+
warn: (message: string) => ctx.log?.warn?.(message),
|
|
112
|
+
error: (message: string) => ctx.log?.error?.(message),
|
|
113
|
+
};
|
|
114
|
+
await new ProxyLink(ctx.cfg, ctx.account, ctx.abortSignal, log).run();
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
package/src/link.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { dispatchInboundDirectDm } from "openclaw/plugin-sdk/channel-inbound";
|
|
2
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/channel-core";
|
|
3
|
+
import { forgetToken, loadToken, saveToken } from "./token-store.js";
|
|
4
|
+
|
|
5
|
+
export const CHANNEL_ID = "timeatlas";
|
|
6
|
+
export const DEFAULT_PROXY_URL = "https://ai.timeatlas.app";
|
|
7
|
+
|
|
8
|
+
/** The one person on the other end: the Time Atlas user who paired. */
|
|
9
|
+
const USER_PEER_ID = "timeatlas-user";
|
|
10
|
+
|
|
11
|
+
const MIN_BACKOFF_MS = 1_000;
|
|
12
|
+
const MAX_BACKOFF_MS = 30_000;
|
|
13
|
+
/** Longer than the proxy holds a poll open. */
|
|
14
|
+
const POLL_TIMEOUT_MS = 40_000;
|
|
15
|
+
|
|
16
|
+
export type ResolvedAccount = {
|
|
17
|
+
accountId: string;
|
|
18
|
+
pairingCode: string;
|
|
19
|
+
proxyUrl: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type Log = {
|
|
23
|
+
info: (message: string) => void;
|
|
24
|
+
warn: (message: string) => void;
|
|
25
|
+
error: (message: string) => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** A chat message from the Time Atlas app, as the proxy hands it over. */
|
|
29
|
+
type ProxyMessage = {
|
|
30
|
+
id: string;
|
|
31
|
+
text: string;
|
|
32
|
+
timestamp?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** What the agent says about an answer, for the proxy to pass on. */
|
|
36
|
+
type ProxyEvent =
|
|
37
|
+
| { type: "typing"; replyTo?: string; on: boolean }
|
|
38
|
+
| { type: "text"; replyTo?: string; text: string }
|
|
39
|
+
| { type: "done"; replyTo: string }
|
|
40
|
+
| { type: "error"; replyTo: string; text: string };
|
|
41
|
+
|
|
42
|
+
class Unauthorized extends Error {}
|
|
43
|
+
|
|
44
|
+
/** The links running, by account, for what the agent sends unasked. */
|
|
45
|
+
const links = new Map<string, ProxyLink>();
|
|
46
|
+
|
|
47
|
+
export function linkFor(accountId: string | null | undefined) {
|
|
48
|
+
return links.get(accountId ?? "default");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* ProxyLink is one account's connection to the Time Atlas proxy. It pairs
|
|
53
|
+
* with the code from the app, then polls for the user's chat messages,
|
|
54
|
+
* hands each to the agent, and posts back what the agent answers.
|
|
55
|
+
*/
|
|
56
|
+
export class ProxyLink {
|
|
57
|
+
private token?: string;
|
|
58
|
+
/** Messages are answered one after another, in the order they came. */
|
|
59
|
+
private queue: Promise<void> = Promise.resolve();
|
|
60
|
+
|
|
61
|
+
constructor(
|
|
62
|
+
private cfg: OpenClawConfig,
|
|
63
|
+
private account: ResolvedAccount,
|
|
64
|
+
private abortSignal: AbortSignal,
|
|
65
|
+
private log: Log,
|
|
66
|
+
) {}
|
|
67
|
+
|
|
68
|
+
/** Runs until the account is stopped. */
|
|
69
|
+
async run(): Promise<void> {
|
|
70
|
+
links.set(this.account.accountId, this);
|
|
71
|
+
try {
|
|
72
|
+
await this.runUntilStopped();
|
|
73
|
+
} finally {
|
|
74
|
+
if (links.get(this.account.accountId) === this) {
|
|
75
|
+
links.delete(this.account.accountId);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private async runUntilStopped(): Promise<void> {
|
|
81
|
+
let backoff = MIN_BACKOFF_MS;
|
|
82
|
+
while (!this.abortSignal.aborted) {
|
|
83
|
+
try {
|
|
84
|
+
if (!this.token) {
|
|
85
|
+
this.token = await this.pairedToken();
|
|
86
|
+
if (!this.token) {
|
|
87
|
+
this.log.warn(
|
|
88
|
+
"timeatlas: no pairing code. Get one from Make Pat an Agent in " +
|
|
89
|
+
"the Time Atlas app and set channels.timeatlas.pairingCode.",
|
|
90
|
+
);
|
|
91
|
+
await this.stopped();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const messages = await this.poll();
|
|
96
|
+
backoff = MIN_BACKOFF_MS;
|
|
97
|
+
for (const message of messages) {
|
|
98
|
+
this.queue = this.queue.then(() => this.answer(message));
|
|
99
|
+
}
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (this.abortSignal.aborted) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (err instanceof Unauthorized) {
|
|
105
|
+
this.log.error(
|
|
106
|
+
"timeatlas: disconnected from Time Atlas. Pair again with a new " +
|
|
107
|
+
"code from the app.",
|
|
108
|
+
);
|
|
109
|
+
this.token = undefined;
|
|
110
|
+
await forgetToken(this.account.accountId);
|
|
111
|
+
await this.stopped();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.log.warn(`timeatlas: ${String(err)}, trying again`);
|
|
115
|
+
await this.sleep(backoff);
|
|
116
|
+
backoff = Math.min(backoff * 2, MAX_BACKOFF_MS);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The token to poll with: the one kept, unless the config has a code it
|
|
123
|
+
* was not paired with, which is traded for a new one.
|
|
124
|
+
*/
|
|
125
|
+
private async pairedToken(): Promise<string | undefined> {
|
|
126
|
+
const accountId = this.account.accountId;
|
|
127
|
+
const code = this.account.pairingCode;
|
|
128
|
+
const stored = await loadToken(accountId);
|
|
129
|
+
if (stored && (!code || stored.pairedWithCode === code)) {
|
|
130
|
+
return stored.token;
|
|
131
|
+
}
|
|
132
|
+
if (!code) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/pair`, {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers: { "Content-Type": "application/json" },
|
|
138
|
+
body: JSON.stringify({ code }),
|
|
139
|
+
signal: this.abortSignal,
|
|
140
|
+
});
|
|
141
|
+
const body = (await response.json().catch(() => ({}))) as {
|
|
142
|
+
token?: string;
|
|
143
|
+
error?: string;
|
|
144
|
+
};
|
|
145
|
+
if (!response.ok || !body.token) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`pairing failed: ${body.error ?? response.statusText}`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
await saveToken(accountId, { token: body.token, pairedWithCode: code });
|
|
151
|
+
this.log.info("timeatlas: paired with Time Atlas");
|
|
152
|
+
return body.token;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private async poll(): Promise<ProxyMessage[]> {
|
|
156
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/poll`, {
|
|
157
|
+
headers: { Authorization: `Bearer ${this.token}` },
|
|
158
|
+
signal: AbortSignal.any([
|
|
159
|
+
this.abortSignal,
|
|
160
|
+
AbortSignal.timeout(POLL_TIMEOUT_MS),
|
|
161
|
+
]),
|
|
162
|
+
});
|
|
163
|
+
if (response.status === 401) {
|
|
164
|
+
throw new Unauthorized();
|
|
165
|
+
}
|
|
166
|
+
if (!response.ok) {
|
|
167
|
+
throw new Error(`poll: ${response.status} ${response.statusText}`);
|
|
168
|
+
}
|
|
169
|
+
const body = (await response.json()) as { messages?: ProxyMessage[] };
|
|
170
|
+
return body.messages ?? [];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Tells the proxy what the agent says. */
|
|
174
|
+
async post(events: ProxyEvent[]): Promise<void> {
|
|
175
|
+
if (!this.token) {
|
|
176
|
+
throw new Error("not paired");
|
|
177
|
+
}
|
|
178
|
+
const response = await fetch(`${this.account.proxyUrl}/agent/events`, {
|
|
179
|
+
method: "POST",
|
|
180
|
+
headers: {
|
|
181
|
+
Authorization: `Bearer ${this.token}`,
|
|
182
|
+
"Content-Type": "application/json",
|
|
183
|
+
},
|
|
184
|
+
body: JSON.stringify({ events }),
|
|
185
|
+
signal: this.abortSignal,
|
|
186
|
+
});
|
|
187
|
+
if (response.status === 401) {
|
|
188
|
+
throw new Unauthorized();
|
|
189
|
+
}
|
|
190
|
+
if (!response.ok) {
|
|
191
|
+
throw new Error(`events: ${response.status} ${response.statusText}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Has the agent answer the message. It types from when the message comes
|
|
197
|
+
* until the whole turn is over, which is when the proxy hears it is done.
|
|
198
|
+
*/
|
|
199
|
+
private async answer(message: ProxyMessage): Promise<void> {
|
|
200
|
+
const replyTo = message.id;
|
|
201
|
+
let said = false;
|
|
202
|
+
try {
|
|
203
|
+
await this.post([{ type: "typing", replyTo, on: true }]);
|
|
204
|
+
await dispatchInboundDirectDm({
|
|
205
|
+
cfg: this.cfg,
|
|
206
|
+
channel: CHANNEL_ID,
|
|
207
|
+
channelLabel: "Time Atlas",
|
|
208
|
+
accountId: this.account.accountId,
|
|
209
|
+
peer: { kind: "direct", id: USER_PEER_ID },
|
|
210
|
+
senderId: USER_PEER_ID,
|
|
211
|
+
senderAddress: `${CHANNEL_ID}:${USER_PEER_ID}`,
|
|
212
|
+
recipientAddress: `${CHANNEL_ID}:pat`,
|
|
213
|
+
conversationLabel: "Time Atlas",
|
|
214
|
+
rawBody: message.text,
|
|
215
|
+
messageId: message.id,
|
|
216
|
+
timestamp: message.timestamp,
|
|
217
|
+
// The proxy only hands over the paired user's messages.
|
|
218
|
+
commandAuthorized: true,
|
|
219
|
+
inboundAccessAuthorized: true,
|
|
220
|
+
channelIngress: "unsupported",
|
|
221
|
+
deliver: async (payload) => {
|
|
222
|
+
const text = payload.text?.trim();
|
|
223
|
+
if (!text) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
// Each reply of the turn is a paragraph of one answer.
|
|
227
|
+
await this.post([
|
|
228
|
+
{ type: "text", replyTo, text: said ? `\n\n${text}` : text },
|
|
229
|
+
]);
|
|
230
|
+
said = true;
|
|
231
|
+
},
|
|
232
|
+
onRecordError: (err) =>
|
|
233
|
+
this.log.warn(`timeatlas: could not record the turn: ${String(err)}`),
|
|
234
|
+
onDispatchError: (err, info) =>
|
|
235
|
+
this.log.error(`timeatlas: ${info.kind} failed: ${String(err)}`),
|
|
236
|
+
});
|
|
237
|
+
await this.post([
|
|
238
|
+
{ type: "typing", replyTo, on: false },
|
|
239
|
+
{ type: "done", replyTo },
|
|
240
|
+
]);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
this.log.error(`timeatlas: could not answer: ${String(err)}`);
|
|
243
|
+
await this.post([
|
|
244
|
+
{ type: "error", replyTo, text: "The agent ran into a problem." },
|
|
245
|
+
]).catch(() => {});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private sleep(ms: number): Promise<void> {
|
|
250
|
+
return new Promise((resolve) => {
|
|
251
|
+
const timer = setTimeout(resolve, ms);
|
|
252
|
+
this.abortSignal.addEventListener(
|
|
253
|
+
"abort",
|
|
254
|
+
() => {
|
|
255
|
+
clearTimeout(timer);
|
|
256
|
+
resolve();
|
|
257
|
+
},
|
|
258
|
+
{ once: true },
|
|
259
|
+
);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private stopped(): Promise<void> {
|
|
264
|
+
return new Promise((resolve) => {
|
|
265
|
+
if (this.abortSignal.aborted) {
|
|
266
|
+
resolve();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
this.abortSignal.addEventListener("abort", () => resolve(), {
|
|
270
|
+
once: true,
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The token the Time Atlas proxy gave for a pairing code, and the code,
|
|
7
|
+
* so that a new code in the config pairs again.
|
|
8
|
+
*/
|
|
9
|
+
export type StoredToken = {
|
|
10
|
+
token: string;
|
|
11
|
+
pairedWithCode: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function tokenPath(accountId: string): string {
|
|
15
|
+
return path.join(resolveStateDir(), "timeatlas", `${accountId}.json`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function loadToken(
|
|
19
|
+
accountId: string,
|
|
20
|
+
): Promise<StoredToken | undefined> {
|
|
21
|
+
try {
|
|
22
|
+
const stored = JSON.parse(await readFile(tokenPath(accountId), "utf8"));
|
|
23
|
+
if (typeof stored?.token === "string" && stored.token) {
|
|
24
|
+
return {
|
|
25
|
+
token: stored.token,
|
|
26
|
+
pairedWithCode: String(stored.pairedWithCode ?? ""),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// None yet.
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function saveToken(
|
|
36
|
+
accountId: string,
|
|
37
|
+
stored: StoredToken,
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
const file = tokenPath(accountId);
|
|
40
|
+
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
41
|
+
await writeFile(file, JSON.stringify(stored), { mode: 0o600 });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function forgetToken(accountId: string): Promise<void> {
|
|
45
|
+
await rm(tokenPath(accountId), { force: true });
|
|
46
|
+
}
|