@timeatlas/openclaw-timeatlas 0.1.0 → 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 CHANGED
@@ -6,7 +6,7 @@ their OpenClaw, and its answers come back to the app.
6
6
  1. The app asks the proxy for a pairing code (Make Pat an Agent).
7
7
  2. The user sets it here, and restarts the gateway:
8
8
 
9
- openclaw plugins install @timeatlas/openclaw-timeatlas
9
+ openclaw plugins install npm:@timeatlas/openclaw-timeatlas
10
10
  openclaw config set channels.timeatlas.pairingCode ABCD-EFGH
11
11
  openclaw gateway restart
12
12
 
@@ -20,3 +20,13 @@ their OpenClaw, and its answers come back to the app.
20
20
  For development, `openclaw plugins install --link ./openclaw/timeatlas-channel`.
21
21
 
22
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,3 @@
1
+ import { defineSetupPluginEntry } from "openclaw/plugin-sdk/channel-core";
2
+ import { timeAtlasPlugin } from "./src/channel.js";
3
+ export default defineSetupPluginEntry(timeAtlasPlugin);
@@ -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
+ };
@@ -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/package.json CHANGED
@@ -1,11 +1,17 @@
1
1
  {
2
2
  "name": "@timeatlas/openclaw-timeatlas",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Time Atlas channel for OpenClaw: Pat, the Time Atlas chat, as an agent",
5
5
  "type": "module",
6
6
  "openclaw": {
7
- "extensions": ["./index.ts"],
7
+ "extensions": [
8
+ "./index.ts"
9
+ ],
10
+ "runtimeExtensions": [
11
+ "./dist/index.js"
12
+ ],
8
13
  "setupEntry": "./setup-entry.ts",
14
+ "runtimeSetupEntry": "./dist/setup-entry.js",
9
15
  "channel": {
10
16
  "id": "timeatlas",
11
17
  "label": "Time Atlas",
@@ -16,10 +22,24 @@
16
22
  "openclaw": ">=2026.11.10"
17
23
  },
18
24
  "devDependencies": {
25
+ "@types/node": "22.20.7",
19
26
  "openclaw": "2026.11.10",
20
27
  "typescript": "5.9.3"
21
28
  },
22
29
  "scripts": {
23
- "typecheck": "tsc --noEmit"
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"
24
44
  }
25
45
  }
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext",
6
- "strict": true,
7
- "skipLibCheck": true,
8
- "noEmit": true,
9
- "allowImportingTsExtensions": false,
10
- "types": ["node"]
11
- },
12
- "include": ["index.ts", "setup-entry.ts", "src/**/*.ts"]
13
- }