@kagura-agent/atlas-openclaw 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 +48 -2
- package/index.mjs +43 -0
- package/openclaw.plugin.json +34 -0
- package/package.json +41 -4
- package/worker.mjs +333 -0
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Atlas channel for OpenClaw
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Atlas owns conversations, selected canvas context, task permissions and native canvas results. OpenClaw owns model execution. The plugin follows Cove's channel registration and outbound connection pattern.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Requires OpenClaw 2026.10.5 or newer and Node.js 22.13 or newer. On the machine running OpenClaw:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
openclaw plugins install @kagura-agent/atlas-openclaw
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In Atlas, choose **Connect AI tools → OpenClaw** from the account menu. The star's first-use prompt and chat's Connections action open the same setup page. Choose **Create connection** and save the displayed configuration. Merge the generated `channels.atlas` configuration into OpenClaw's configuration. Preserve other channels and plugin entries. Enable the installed `atlas` plugin; if `plugins.allow` is configured, add `atlas` to that list. Set `agentId` to an existing OpenClaw agent with a working model configuration.
|
|
14
|
+
|
|
15
|
+
Restart the OpenClaw gateway after installation. Atlas shows **Connected** when the authenticated socket connects. Select the connection and send a message.
|
|
16
|
+
|
|
17
|
+
Use the public HTTPS Atlas origin when OpenClaw runs on another machine. Forward `/agent-worker` with WebSocket upgrades and `/api/agent-worker/` to the same Atlas server. Loopback HTTP is supported for local development and SSH tunnels.
|
|
18
|
+
|
|
19
|
+
For development on a trusted LAN, set `channels.atlas.allowPrivateNetworkHttp` to `true` and use the Atlas machine's private IPv4 address in `baseUrl`, such as `http://10.172.208.53:4317`. Atlas must listen on its LAN interface, and the firewall must allow the OpenClaw machine to reach that port. This opt-in accepts only literal addresses in `10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`; public HTTP and HTTP hostnames remain rejected. Keep the token private and use HTTPS outside the trusted LAN. The browser's Google callback address can stay on localhost.
|
|
20
|
+
|
|
21
|
+
Tokens belong in OpenClaw's private configuration, never in messages, source files or shared installation snippets. Maintainer documentation (repository access required): [Connect agents](https://github.com/kagura-chen/atlas/blob/main/docs/product/AI-TOOLS.md#在画板中与-openclaw-对话) and [Current gaps](https://github.com/kagura-chen/atlas/blob/main/docs/CURRENT-GAPS.md).
|
|
22
|
+
|
|
23
|
+
## Publish (maintainers)
|
|
24
|
+
|
|
25
|
+
The [Publish OpenClaw plugin workflow](https://github.com/kagura-chen/atlas/actions/workflows/publish-openclaw.yml) packs once, tests the archive in clean OpenClaw installations (the minimum supported version and npm's latest version), and publishes that same archive. Pull requests only verify; manual runs publish only when `publish` is selected and the branch is `main`. Leave `publish` off to download the `atlas-openclaw-package` artifact without publishing.
|
|
26
|
+
|
|
27
|
+
For the first release, create a short-lived granular npm token using the `kagura-agent` account. Grant read/write publication access to the `@kagura-agent` scope and enable **Bypass 2FA** for non-interactive CI publication. Save it as the repository Actions secret `NPM_BOOTSTRAP_TOKEN`. Run the workflow on `main`, select `publish`, and choose `bootstrap-token` authentication. This job uses the token without requesting an OIDC identity.
|
|
28
|
+
|
|
29
|
+
After the package exists, configure its npm **Trusted Publisher** for GitHub Actions:
|
|
30
|
+
|
|
31
|
+
- Organization or user: `kagura-chen`
|
|
32
|
+
- Repository: `atlas`
|
|
33
|
+
- Workflow filename: `publish-openclaw.yml`
|
|
34
|
+
- Environment name: leave empty (this workflow does not use an environment)
|
|
35
|
+
- Allow direct publication with `npm publish` if the settings offer allowed actions.
|
|
36
|
+
|
|
37
|
+
Revoke the bootstrap token in npm and delete `NPM_BOOTSTRAP_TOKEN` from GitHub after configuring the trusted publisher. Subsequent releases select `trusted-publisher` (the default) and use OIDC with no npm token in GitHub Secrets. Provenance is disabled because the source repository is private. See [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/).
|
|
38
|
+
|
|
39
|
+
Increase the committed package version before another release. An existing version is not overwritten; registry or authentication errors fail the run. After publishing, verify installation from npm before shipping the corresponding Atlas setup instructions.
|
|
40
|
+
|
|
41
|
+
## Design
|
|
42
|
+
|
|
43
|
+
WebSocket notifications wake the worker; reconnecting and periodic server notifications refresh the queue. Finishing a run immediately checks for the next queued task. Short renewable run leases prevent a lost worker from silently reexecuting work. Tool writes use stable request IDs; canvas changes, run updates and receipts commit in the same SQLite transaction. Lost HTTP responses can be retried without duplicating writes. Final settlement is idempotent too.
|
|
44
|
+
|
|
45
|
+
Each turn gets a fresh OpenClaw session with Atlas's completed conversation history. Cancelled and failed prompts are excluded so runtime transcript replay cannot revive them. The runtime tool allowlist is `atlas_board`; tool instances exist only for the active run's session and cannot choose another board or acquire a new scope. Stop aborts the dispatcher; only after it returns does the plugin report settlement. OpenClaw error payloads are failures, not successful replies.
|
|
46
|
+
|
|
47
|
+
Maintainer source (repository access required): [Channel registration](https://github.com/kagura-chen/atlas/blob/main/plugins/openclaw-atlas/index.mjs) · [Worker and scoped tool](https://github.com/kagura-chen/atlas/blob/main/plugins/openclaw-atlas/worker.mjs) · [Manifest](https://github.com/kagura-chen/atlas/blob/main/plugins/openclaw-atlas/openclaw.plugin.json) · [Atlas service](https://github.com/kagura-chen/atlas/blob/main/server/agent-chat.ts) · [Scope validation](https://github.com/kagura-chen/atlas/blob/main/server/agent-scope.ts) · [HTTP and gateway](https://github.com/kagura-chen/atlas/blob/main/server/agent-chat-api.ts).
|
|
48
|
+
|
|
49
|
+
Reference: [Cove channel entry](https://github.com/kagura-chen/cove/blob/3de8390882e8b0170b493d4f81001b498a78cdc1/packages/plugin/src/index.ts), [channel](https://github.com/kagura-chen/cove/blob/3de8390882e8b0170b493d4f81001b498a78cdc1/packages/plugin/src/channel.ts), [dispatch](https://github.com/kagura-chen/cove/blob/3de8390882e8b0170b493d4f81001b498a78cdc1/packages/plugin/src/dispatch.ts). The installed SDK requires `contracts.tools` in the manifest; its reply dispatcher replaces Cove's older `runInboundReplyTurn` entry point.
|
package/index.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createChatChannelPlugin,
|
|
3
|
+
defineChannelPluginEntry,
|
|
4
|
+
} from "openclaw/plugin-sdk/channel-core";
|
|
5
|
+
import { boardTool, startAccount } from "./worker.mjs";
|
|
6
|
+
|
|
7
|
+
const plugin = createChatChannelPlugin({
|
|
8
|
+
base: {
|
|
9
|
+
id: "atlas",
|
|
10
|
+
meta: {
|
|
11
|
+
id: "atlas",
|
|
12
|
+
label: "Atlas",
|
|
13
|
+
selectionLabel: "Atlas canvas",
|
|
14
|
+
docsPath: "/channels/atlas",
|
|
15
|
+
blurb: "External agent collaboration on an Atlas canvas",
|
|
16
|
+
},
|
|
17
|
+
capabilities: { chatTypes: ["direct"] },
|
|
18
|
+
config: {
|
|
19
|
+
listAccountIds: (cfg) => (cfg.channels?.atlas ? ["default"] : []),
|
|
20
|
+
defaultAccountId: () => "default",
|
|
21
|
+
resolveAccount: (cfg) => ({
|
|
22
|
+
accountId: "default",
|
|
23
|
+
enabled: true,
|
|
24
|
+
...cfg.channels?.atlas,
|
|
25
|
+
}),
|
|
26
|
+
isConfigured: (account) =>
|
|
27
|
+
Boolean(account.baseUrl && account.token && account.agentId),
|
|
28
|
+
isEnabled: (account) => account.enabled !== false,
|
|
29
|
+
},
|
|
30
|
+
gateway: { startAccount },
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export default defineChannelPluginEntry({
|
|
35
|
+
id: "atlas",
|
|
36
|
+
name: "Atlas",
|
|
37
|
+
description: "External agents with scoped canvas tools",
|
|
38
|
+
plugin,
|
|
39
|
+
registerFull: (api) =>
|
|
40
|
+
api.registerTool((context) => boardTool(context), {
|
|
41
|
+
names: ["atlas_board"],
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "atlas",
|
|
3
|
+
"kind": "channel",
|
|
4
|
+
"name": "Atlas",
|
|
5
|
+
"description": "External agent conversations and scoped canvas editing",
|
|
6
|
+
"channels": ["atlas"],
|
|
7
|
+
"contracts": { "tools": ["atlas_board"] },
|
|
8
|
+
"activation": { "onStartup": true },
|
|
9
|
+
"configSchema": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"additionalProperties": false,
|
|
12
|
+
"properties": {}
|
|
13
|
+
},
|
|
14
|
+
"channelConfigs": {
|
|
15
|
+
"atlas": {
|
|
16
|
+
"schema": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"additionalProperties": false,
|
|
19
|
+
"properties": {
|
|
20
|
+
"enabled": { "type": "boolean" },
|
|
21
|
+
"baseUrl": { "type": "string" },
|
|
22
|
+
"token": { "type": "string" },
|
|
23
|
+
"agentId": { "type": "string" },
|
|
24
|
+
"allowPrivateNetworkHttp": {
|
|
25
|
+
"type": "boolean",
|
|
26
|
+
"default": false,
|
|
27
|
+
"description": "Allow HTTP to an explicit private IPv4 address on a trusted local network"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"uiHints": { "token": { "sensitive": true } }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kagura-agent/atlas-openclaw",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Connect an external OpenClaw agent to Atlas conversations and canvas tools",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/kagura-chen/atlas.git",
|
|
8
|
+
"directory": "plugins/openclaw-atlas"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public",
|
|
12
|
+
"registry": "https://registry.npmjs.org/"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "index.mjs",
|
|
16
|
+
"files": [
|
|
17
|
+
"index.mjs",
|
|
18
|
+
"worker.mjs",
|
|
19
|
+
"openclaw.plugin.json",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22.13"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"ws": "^8.18.3"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"openclaw": ">=2026.10.5"
|
|
30
|
+
},
|
|
31
|
+
"openclaw": {
|
|
32
|
+
"extensions": [
|
|
33
|
+
"./index.mjs"
|
|
34
|
+
],
|
|
35
|
+
"channel": {
|
|
36
|
+
"id": "atlas",
|
|
37
|
+
"label": "Atlas",
|
|
38
|
+
"selectionLabel": "Atlas canvas",
|
|
39
|
+
"docsPath": "/channels/atlas",
|
|
40
|
+
"blurb": "Work on an Atlas canvas from OpenClaw"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
package/worker.mjs
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
4
|
+
import WebSocket from "ws";
|
|
5
|
+
|
|
6
|
+
const sessionRegistry = Symbol.for("atlas.openclaw.active-runs");
|
|
7
|
+
export const sessions = (globalThis[sessionRegistry] ||= new Map());
|
|
8
|
+
export function connection(account) {
|
|
9
|
+
const base = new URL(account.baseUrl);
|
|
10
|
+
if (
|
|
11
|
+
base.username ||
|
|
12
|
+
base.password ||
|
|
13
|
+
base.pathname !== "/" ||
|
|
14
|
+
base.search ||
|
|
15
|
+
base.hash ||
|
|
16
|
+
!["http:", "https:"].includes(base.protocol)
|
|
17
|
+
)
|
|
18
|
+
throw new Error("Atlas requires an HTTP(S) origin");
|
|
19
|
+
const [first, second] = base.hostname.split(".").map(Number);
|
|
20
|
+
const privateIpv4 =
|
|
21
|
+
isIP(base.hostname) === 4 &&
|
|
22
|
+
(first === 10 ||
|
|
23
|
+
(first === 172 && second >= 16 && second <= 31) ||
|
|
24
|
+
(first === 192 && second === 168));
|
|
25
|
+
if (
|
|
26
|
+
base.protocol === "http:" &&
|
|
27
|
+
!["localhost", "127.0.0.1", "[::1]"].includes(base.hostname) &&
|
|
28
|
+
!(account.allowPrivateNetworkHttp === true && privateIpv4)
|
|
29
|
+
)
|
|
30
|
+
throw new Error(
|
|
31
|
+
"Use HTTPS, or enable allowPrivateNetworkHttp for a private IPv4 address",
|
|
32
|
+
);
|
|
33
|
+
return base.origin;
|
|
34
|
+
}
|
|
35
|
+
export function client(account) {
|
|
36
|
+
const base = connection(account);
|
|
37
|
+
return async (path, body) => {
|
|
38
|
+
const response = await fetch(`${base}/api/agent-worker/${path}`, {
|
|
39
|
+
method: body === undefined ? "GET" : "POST",
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${account.token}`,
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
},
|
|
44
|
+
...(body !== undefined && { body: JSON.stringify(body) }),
|
|
45
|
+
signal: AbortSignal.timeout(15000),
|
|
46
|
+
redirect: "error",
|
|
47
|
+
});
|
|
48
|
+
const value = await response.json();
|
|
49
|
+
if (!response.ok)
|
|
50
|
+
throw Object.assign(
|
|
51
|
+
new Error(value.error || `Atlas request failed (${response.status})`),
|
|
52
|
+
{ status: response.status },
|
|
53
|
+
);
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function boardTool(context) {
|
|
59
|
+
const run = sessions.get(context.sessionKey);
|
|
60
|
+
if (!run) return null;
|
|
61
|
+
return {
|
|
62
|
+
name: "atlas_board",
|
|
63
|
+
label: "Atlas board",
|
|
64
|
+
description:
|
|
65
|
+
"Read the current Atlas run context, get the canvas composer skill, create or update native canvas objects, or ask the user a structured question. Only this run's selected objects and newly created objects are writable. Read skill before writing. Never use another tool to access Atlas.",
|
|
66
|
+
parameters: {
|
|
67
|
+
type: "object",
|
|
68
|
+
additionalProperties: false,
|
|
69
|
+
required: ["action"],
|
|
70
|
+
properties: {
|
|
71
|
+
action: {
|
|
72
|
+
type: "string",
|
|
73
|
+
enum: ["skill", "read", "create", "update", "ask"],
|
|
74
|
+
},
|
|
75
|
+
svg: {
|
|
76
|
+
type: "string",
|
|
77
|
+
description: "Atlas Canvas Composer SVG for create/update",
|
|
78
|
+
},
|
|
79
|
+
question: { type: "string" },
|
|
80
|
+
options: { type: "array", items: { type: "string" }, maxItems: 6 },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
async execute(callId, args) {
|
|
84
|
+
run.controller.signal.throwIfAborted();
|
|
85
|
+
if (sessions.get(context.sessionKey) !== run)
|
|
86
|
+
throw new Error("Atlas run is no longer active");
|
|
87
|
+
const requestId = `tool:${callId}`;
|
|
88
|
+
let result;
|
|
89
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
90
|
+
try {
|
|
91
|
+
result = await run.post("tool", { ...args, requestId });
|
|
92
|
+
break;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
if (e.status || attempt === 2) throw e;
|
|
95
|
+
await sleep(500, undefined, { signal: run.controller.signal });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (args.action === "ask") {
|
|
99
|
+
for (;;) {
|
|
100
|
+
await sleep(1000, undefined, { signal: run.controller.signal });
|
|
101
|
+
const state = await run.heartbeat();
|
|
102
|
+
if (
|
|
103
|
+
state.question?.id === result.questionId &&
|
|
104
|
+
state.question.answer !== undefined
|
|
105
|
+
)
|
|
106
|
+
return {
|
|
107
|
+
content: [
|
|
108
|
+
{
|
|
109
|
+
type: "text",
|
|
110
|
+
text: JSON.stringify({ answer: state.question.answer }),
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { content: [{ type: "text", text: JSON.stringify(result) }] };
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function executeRun(ctx, request, id) {
|
|
122
|
+
const job = await request(`runs/${encodeURIComponent(id)}/claim`, {});
|
|
123
|
+
const controller = new AbortController();
|
|
124
|
+
let cancelled = false;
|
|
125
|
+
const onAbort = () => {
|
|
126
|
+
cancelled = true;
|
|
127
|
+
controller.abort(new Error("OpenClaw channel stopped"));
|
|
128
|
+
};
|
|
129
|
+
ctx.abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
130
|
+
if (ctx.abortSignal.aborted) onAbort();
|
|
131
|
+
// A fresh runtime session for each turn prevents failed/cancelled prompts from resurfacing.
|
|
132
|
+
// Atlas supplies only completed conversation history, independent of OpenClaw's transcript.
|
|
133
|
+
const sessionKey = `agent:${ctx.account.agentId}:atlas:direct:${randomUUID()}`;
|
|
134
|
+
const post = (action, body) =>
|
|
135
|
+
request(`runs/${encodeURIComponent(id)}/${action}`, {
|
|
136
|
+
...body,
|
|
137
|
+
lease: job.lease,
|
|
138
|
+
});
|
|
139
|
+
const heartbeat = async () => {
|
|
140
|
+
try {
|
|
141
|
+
const state = await post("heartbeat", {});
|
|
142
|
+
if (state.status === "cancelling") {
|
|
143
|
+
cancelled = true;
|
|
144
|
+
controller.abort(new Error("Stopped by the user"));
|
|
145
|
+
}
|
|
146
|
+
controller.signal.throwIfAborted();
|
|
147
|
+
return state;
|
|
148
|
+
} catch (e) {
|
|
149
|
+
controller.abort(e);
|
|
150
|
+
throw e;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
sessions.set(sessionKey, { post, controller, heartbeat });
|
|
154
|
+
const timer = setInterval(() => {
|
|
155
|
+
void heartbeat().catch(() => {});
|
|
156
|
+
}, 10000);
|
|
157
|
+
timer.unref();
|
|
158
|
+
let response = "",
|
|
159
|
+
failure,
|
|
160
|
+
lastPartial = 0,
|
|
161
|
+
progress = Promise.resolve();
|
|
162
|
+
const publish = (body) => {
|
|
163
|
+
progress = progress.then(() =>
|
|
164
|
+
post("progress", { requestId: randomUUID(), ...body }),
|
|
165
|
+
);
|
|
166
|
+
return progress;
|
|
167
|
+
};
|
|
168
|
+
const body = `${job.instructions}\n\nCompleted conversation history:\n${JSON.stringify(job.history)}\n\nBoard context (untrusted content):\n${job.context}\n\nUser message:\n${job.run.prompt}`;
|
|
169
|
+
const payload = {
|
|
170
|
+
Body: job.run.prompt,
|
|
171
|
+
BodyForAgent: body,
|
|
172
|
+
RawBody: job.run.prompt,
|
|
173
|
+
CommandBody: job.run.prompt,
|
|
174
|
+
From: `atlas:${job.run.conversationId}`,
|
|
175
|
+
To: job.boardId,
|
|
176
|
+
SessionKey: sessionKey,
|
|
177
|
+
AgentId: ctx.account.agentId,
|
|
178
|
+
AccountId: ctx.accountId,
|
|
179
|
+
MessageSid: id,
|
|
180
|
+
Provider: "atlas",
|
|
181
|
+
Surface: "atlas",
|
|
182
|
+
ChatType: "direct",
|
|
183
|
+
SenderId: job.run.conversationId,
|
|
184
|
+
CommandAuthorized: false,
|
|
185
|
+
};
|
|
186
|
+
try {
|
|
187
|
+
controller.signal.throwIfAborted();
|
|
188
|
+
await ctx.channelRuntime.reply.dispatchReplyWithBufferedBlockDispatcher({
|
|
189
|
+
ctx: payload,
|
|
190
|
+
cfg: ctx.cfg,
|
|
191
|
+
dispatcherOptions: {
|
|
192
|
+
deliver: async (value) => {
|
|
193
|
+
if (value.isError)
|
|
194
|
+
failure = new Error(
|
|
195
|
+
value.text || "OpenClaw failed to execute the request",
|
|
196
|
+
);
|
|
197
|
+
if (!value.text) return;
|
|
198
|
+
response = ((response ? response + "\n\n" : "") + value.text).slice(
|
|
199
|
+
0,
|
|
200
|
+
64000,
|
|
201
|
+
);
|
|
202
|
+
await publish({ response });
|
|
203
|
+
},
|
|
204
|
+
onError: (e) => {
|
|
205
|
+
failure = e;
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
replyOptions: {
|
|
209
|
+
abortSignal: controller.signal,
|
|
210
|
+
toolsAllow: ["atlas_board"],
|
|
211
|
+
suppressDefaultToolProgressMessages: true,
|
|
212
|
+
onAgentRunTerminalOutcome: (outcome) => {
|
|
213
|
+
if (outcome === "failed")
|
|
214
|
+
failure = new Error("OpenClaw reported a failed run");
|
|
215
|
+
},
|
|
216
|
+
onPartialReply: async (value) => {
|
|
217
|
+
if (
|
|
218
|
+
!value.text ||
|
|
219
|
+
Date.now() - lastPartial < 700 ||
|
|
220
|
+
controller.signal.aborted
|
|
221
|
+
)
|
|
222
|
+
return;
|
|
223
|
+
lastPartial = Date.now();
|
|
224
|
+
await publish({
|
|
225
|
+
response: ((response ? response + "\n\n" : "") + value.text).slice(
|
|
226
|
+
0,
|
|
227
|
+
64000,
|
|
228
|
+
),
|
|
229
|
+
});
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
await progress;
|
|
234
|
+
controller.signal.throwIfAborted();
|
|
235
|
+
if (failure) throw failure;
|
|
236
|
+
if (!response && !controller.signal.aborted)
|
|
237
|
+
throw new Error("OpenClaw finished without a visible response");
|
|
238
|
+
} catch (e) {
|
|
239
|
+
failure = e;
|
|
240
|
+
} finally {
|
|
241
|
+
clearInterval(timer);
|
|
242
|
+
sessions.delete(sessionKey);
|
|
243
|
+
ctx.abortSignal.removeEventListener("abort", onAbort);
|
|
244
|
+
}
|
|
245
|
+
const status = cancelled ? "cancelled" : failure ? "failed" : "completed";
|
|
246
|
+
const final = {
|
|
247
|
+
status,
|
|
248
|
+
response:
|
|
249
|
+
failure && status === "failed"
|
|
250
|
+
? String(failure.message || failure)
|
|
251
|
+
: response,
|
|
252
|
+
};
|
|
253
|
+
// Final settlement is idempotent, including a lost HTTP response.
|
|
254
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
255
|
+
try {
|
|
256
|
+
await post("finish", final);
|
|
257
|
+
return;
|
|
258
|
+
} catch (e) {
|
|
259
|
+
if (e.status || attempt === 2) throw e;
|
|
260
|
+
await sleep(1000);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export async function startAccount(ctx) {
|
|
266
|
+
if (!ctx.channelRuntime)
|
|
267
|
+
throw new Error("Atlas requires the OpenClaw channel runtime");
|
|
268
|
+
const request = client(ctx.account),
|
|
269
|
+
running = new Map();
|
|
270
|
+
let checking = false,
|
|
271
|
+
socket,
|
|
272
|
+
reconnect;
|
|
273
|
+
const check = async () => {
|
|
274
|
+
if (checking || ctx.abortSignal.aborted) return;
|
|
275
|
+
checking = true;
|
|
276
|
+
try {
|
|
277
|
+
for (const run of await request("runs")) {
|
|
278
|
+
if (run.status !== "queued" || running.has(run.id) || running.size >= 4)
|
|
279
|
+
continue;
|
|
280
|
+
const task = executeRun(ctx, request, run.id)
|
|
281
|
+
.catch((e) => ctx.log?.error?.(`Atlas run failed: ${e.message}`))
|
|
282
|
+
.finally(() => {
|
|
283
|
+
running.delete(run.id);
|
|
284
|
+
void check();
|
|
285
|
+
});
|
|
286
|
+
running.set(run.id, task);
|
|
287
|
+
}
|
|
288
|
+
} catch (e) {
|
|
289
|
+
ctx.log?.warn?.(`Atlas connection: ${e.message}`);
|
|
290
|
+
} finally {
|
|
291
|
+
checking = false;
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
const open = () => {
|
|
295
|
+
if (ctx.abortSignal.aborted) return;
|
|
296
|
+
socket = new WebSocket(
|
|
297
|
+
connection(ctx.account).replace(/^http/, "ws") + "/agent-worker",
|
|
298
|
+
{
|
|
299
|
+
headers: { Authorization: `Bearer ${ctx.account.token}` },
|
|
300
|
+
handshakeTimeout: 10000,
|
|
301
|
+
maxPayload: 65536,
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
socket.on("open", () => {
|
|
305
|
+
ctx.setStatus?.({
|
|
306
|
+
accountId: ctx.accountId,
|
|
307
|
+
connected: true,
|
|
308
|
+
running: true,
|
|
309
|
+
});
|
|
310
|
+
void check();
|
|
311
|
+
});
|
|
312
|
+
socket.on("message", () => {
|
|
313
|
+
void check();
|
|
314
|
+
});
|
|
315
|
+
socket.on("error", () => {});
|
|
316
|
+
socket.on("close", () => {
|
|
317
|
+
ctx.setStatus?.({
|
|
318
|
+
accountId: ctx.accountId,
|
|
319
|
+
connected: false,
|
|
320
|
+
running: true,
|
|
321
|
+
});
|
|
322
|
+
if (!ctx.abortSignal.aborted) reconnect = setTimeout(open, 3000);
|
|
323
|
+
});
|
|
324
|
+
};
|
|
325
|
+
open();
|
|
326
|
+
await new Promise((resolve) => {
|
|
327
|
+
if (ctx.abortSignal.aborted) resolve();
|
|
328
|
+
else ctx.abortSignal.addEventListener("abort", resolve, { once: true });
|
|
329
|
+
});
|
|
330
|
+
clearTimeout(reconnect);
|
|
331
|
+
socket?.terminate();
|
|
332
|
+
await Promise.allSettled([...running.values()]);
|
|
333
|
+
}
|