@newbase-clawchat/openclaw-clawchat 2026.4.29 → 2026.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +33 -10
  2. package/dist/index.js +27 -0
  3. package/dist/src/api-client.js +156 -0
  4. package/dist/src/api-types.js +17 -0
  5. package/dist/src/buffered-stream.js +177 -0
  6. package/dist/src/channel.js +191 -0
  7. package/dist/src/client.js +176 -0
  8. package/dist/src/commands.js +35 -0
  9. package/dist/src/config.js +214 -0
  10. package/dist/src/inbound.js +133 -0
  11. package/dist/src/login.runtime.js +130 -0
  12. package/dist/src/media-runtime.js +85 -0
  13. package/dist/src/message-mapper.js +82 -0
  14. package/dist/src/outbound.js +181 -0
  15. package/dist/src/protocol.js +38 -0
  16. package/dist/src/reply-dispatcher.js +440 -0
  17. package/dist/src/runtime.js +288 -0
  18. package/dist/src/streaming.js +65 -0
  19. package/dist/src/tools-schema.js +38 -0
  20. package/dist/src/tools.js +287 -0
  21. package/openclaw.plugin.json +12 -0
  22. package/package.json +25 -5
  23. package/skills/clawchat-activate/SKILL.md +17 -8
  24. package/src/buffered-stream.test.ts +10 -0
  25. package/src/buffered-stream.ts +6 -6
  26. package/src/channel.outbound.test.ts +3 -3
  27. package/src/channel.ts +11 -3
  28. package/src/client.test.ts +8 -1
  29. package/src/client.ts +11 -10
  30. package/src/commands.test.ts +6 -0
  31. package/src/commands.ts +5 -1
  32. package/src/config.test.ts +3 -0
  33. package/src/config.ts +7 -0
  34. package/src/inbound.test.ts +4 -1
  35. package/src/inbound.ts +11 -10
  36. package/src/login.runtime.test.ts +36 -0
  37. package/src/login.runtime.ts +54 -26
  38. package/src/manifest.test.ts +98 -22
  39. package/src/outbound.test.ts +6 -5
  40. package/src/outbound.ts +8 -7
  41. package/src/reply-dispatcher.test.ts +418 -3
  42. package/src/reply-dispatcher.ts +137 -12
  43. package/src/runtime.ts +1 -0
  44. package/src/streaming.test.ts +12 -9
  45. package/src/streaming.ts +6 -6
  46. package/src/tools.test.ts +81 -18
  47. package/src/tools.ts +63 -72
package/README.md CHANGED
@@ -10,7 +10,7 @@ OpenClaw channel plugin that connects an agent to ClawChat over the ClawChat Pro
10
10
  - Outbound text replies in `static` or `stream` mode, with a consolidated final `message.reply`
11
11
  - Typing indicators and filtered forwarding for thinking / tool-call content
12
12
  - Media fragments (image / file / audio / video) in either direction
13
- - Channel-login onboarding plus `clawchat_*` agent tools for the configured ClawChat account profile, friends, and media upload
13
+ - Invite-code onboarding via `clawchat_activate` or channel login, plus always-registered `clawchat_*` account/media tools
14
14
 
15
15
  ## Install
16
16
 
@@ -19,7 +19,7 @@ OpenClaw channel plugin that connects an agent to ClawChat over the ClawChat Pro
19
19
  npm i @newbase-clawchat/openclaw-clawchat
20
20
  ```
21
21
 
22
- Requires `openclaw >= 2026.3.23` as a peer host.
22
+ Requires `openclaw >= 2026.4.29` as a peer host.
23
23
 
24
24
  For the OpenClaw plugin install/update flow, see [`INSTALL.md`](./INSTALL.md).
25
25
 
@@ -37,23 +37,35 @@ Send one of these in chat:
37
37
  activate ClawChat with invite code A1B2C3
38
38
  ```
39
39
 
40
- The activation skill executes OpenClaw channel login:
40
+ If the plugin is already loaded by the Gateway, the activation skill calls the
41
+ `clawchat_activate` tool and OpenClaw hot-reloads the updated `channels.*`
42
+ credentials. If the tool is not available yet, use channel login as the CLI
43
+ fallback:
41
44
 
42
45
  ```bash
43
46
  openclaw channels login --channel openclaw-clawchat
47
+ openclaw channels status --probe
48
+ ```
49
+
50
+ Restart the Gateway after installing/updating the plugin, when config reload is
51
+ disabled, or when the channel probe does not become healthy:
52
+
53
+ ```bash
44
54
  openclaw gateway restart
45
55
  ```
46
56
 
47
- If you run the gateway manually instead of as a service, start it after login:
57
+ If you run the gateway manually instead of as a service and it is not already
58
+ running, start it after login:
48
59
 
49
60
  ```bash
50
61
  openclaw gateway run
51
62
  ```
52
63
 
53
- The invite code is not a token; token fields are written only after login. Before
54
- login, the plugin registers no ClawChat account tools; after activation/login,
55
- the channel is enabled and the `clawchat_*` account tools register on config
56
- reload.
64
+ The invite code is not a token; token fields are written only after login. The
65
+ plugin registers `clawchat_activate` and the six account/media tools at plugin
66
+ load time so they stay visible before activation. Before login, account/media
67
+ tools return a config error instead of disappearing; after activation/login, the
68
+ channel is enabled and the same tools read the hot-reloaded token/userId.
57
69
 
58
70
  After activation/login, the channel section is enabled and has credentials:
59
71
 
@@ -64,7 +76,7 @@ After activation/login, the channel section is enabled and has credentials:
64
76
  enabled: true,
65
77
  replyMode: "stream",
66
78
  forwardThinking: true,
67
- forwardToolCalls: false
79
+ forwardToolCalls: false,
68
80
  token: "...",
69
81
  userId: "...",
70
82
  refreshToken: "..."
@@ -132,7 +144,18 @@ npx vitest run
132
144
  npm run typecheck
133
145
  ```
134
146
 
135
- Tests live next to the source they cover (`*.test.ts`). The plugin is pure TypeScript and is consumed via the OpenClaw host's extension loader no bundling step is required.
147
+ Tests live next to the source they cover (`*.test.ts`). The development entrypoint stays in TypeScript for the OpenClaw extension loader, while npm installs use the compiled runtime entrypoint generated by `npm run build` / `prepack` under `dist/`.
148
+
149
+ For OpenClaw host SDK/source lookup while developing this plugin, optionally
150
+ clone OpenClaw into `tmp/openclaw`:
151
+
152
+ ```bash
153
+ npm run dev:openclaw-source
154
+ # equivalent: git clone --depth=1 https://github.com/openclaw/openclaw.git tmp/openclaw
155
+ ```
156
+
157
+ This checkout is local-only. It is ignored by git and is not required to run the
158
+ plugin tests or publish the package.
136
159
 
137
160
  ## License
138
161
 
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ import { openclawClawlingPlugin } from "./src/channel.js";
2
+ import { registerOpenclawClawlingCommands } from "./src/commands.js";
3
+ import { setOpenclawClawlingRuntime } from "./src/runtime.js";
4
+ import { registerOpenclawClawlingTools } from "./src/tools.js";
5
+ import { openclawClawlingConfigSchema } from "./src/config.js";
6
+ export default {
7
+ id: "openclaw-clawchat",
8
+ name: "Clawling Chat",
9
+ description: "Clawling Chat Protocol v2 channel plugin (chat-sdk)",
10
+ configSchema: openclawClawlingConfigSchema,
11
+ register(api) {
12
+ setOpenclawClawlingRuntime(api.runtime);
13
+ api.registerChannel({ plugin: openclawClawlingPlugin });
14
+ registerOpenclawClawlingCommands(api);
15
+ registerOpenclawClawlingTools(api);
16
+ }
17
+ };
18
+ // export default defineChannelPluginEntry({
19
+ // id: "openclaw-clawchat",
20
+ // name: "Clawling Chat",
21
+ // description: "Clawling Chat Protocol v2 channel plugin (chat-sdk)",
22
+ // plugin: openclawClawlingPlugin,
23
+ // setRuntime: setOpenclawClawlingRuntime,
24
+ // registerFull(api) {
25
+ // registerOpenclawClawlingTools(api);
26
+ // },
27
+ // });
@@ -0,0 +1,156 @@
1
+ import { ClawlingApiError, } from "./api-types.js";
2
+ import { CHANNEL_ID } from "./config.js";
3
+ export function createOpenclawClawlingApiClient(opts) {
4
+ if (!/^https?:\/\//i.test(opts.baseUrl)) {
5
+ throw new ClawlingApiError("validation", `openclaw-clawchat baseUrl must start with http:// or https:// (got "${opts.baseUrl}")`);
6
+ }
7
+ const baseUrl = opts.baseUrl.replace(/\/+$/, "");
8
+ const fetchImpl = opts.fetchImpl ?? globalThis.fetch.bind(globalThis);
9
+ function url(path) {
10
+ return `${baseUrl}${path}`;
11
+ }
12
+ function authHeaders(extra = {}) {
13
+ // `X-Device-Id` is sent on every request so the server can correlate
14
+ // activity back to this plugin instance. Callers may override via
15
+ // `extra` (e.g. tests) but the default is the channel id.
16
+ return {
17
+ authorization: `Bearer ${opts.token}`,
18
+ "x-device-id": CHANNEL_ID,
19
+ ...extra,
20
+ };
21
+ }
22
+ async function readEnvelope(res, path) {
23
+ if (res.status === 401 || res.status === 403) {
24
+ throw new ClawlingApiError("auth", `unauthorized (status ${res.status})`, {
25
+ status: res.status,
26
+ path,
27
+ });
28
+ }
29
+ if (!res.ok) {
30
+ const snippet = await res.text().catch(() => "");
31
+ throw new ClawlingApiError("transport", `http ${res.status} ${snippet.slice(0, 200)}`, {
32
+ status: res.status,
33
+ path,
34
+ });
35
+ }
36
+ let parsed;
37
+ try {
38
+ parsed = await res.json();
39
+ }
40
+ catch (err) {
41
+ throw new ClawlingApiError("transport", `non-JSON response: ${err instanceof Error ? err.message : String(err)}`, { status: res.status, path });
42
+ }
43
+ // Unified envelope: `{ code: number, msg: string, data: T }`.
44
+ // `code === 0` means success; any other value is a business error whose
45
+ // `msg` is surfaced to callers and `code` is preserved on the error meta.
46
+ const env = parsed;
47
+ const code = typeof env.code === "number" ? env.code : Number.NaN;
48
+ const msg = typeof env.msg === "string"
49
+ ? env.msg
50
+ : typeof env.message === "string"
51
+ ? env.message
52
+ : "";
53
+ if (!Number.isFinite(code)) {
54
+ throw new ClawlingApiError("transport", "invalid envelope: missing numeric `code`", {
55
+ status: res.status,
56
+ path,
57
+ });
58
+ }
59
+ if (code !== 0) {
60
+ throw new ClawlingApiError("api", msg || `code=${code}`, {
61
+ code,
62
+ status: res.status,
63
+ path,
64
+ });
65
+ }
66
+ return env.data;
67
+ }
68
+ async function call(method, path, init) {
69
+ let res;
70
+ try {
71
+ const requestInit = {
72
+ method,
73
+ headers: authHeaders(init?.headers),
74
+ };
75
+ if (init?.body !== undefined) {
76
+ requestInit.body = init.body;
77
+ }
78
+ res = await fetchImpl(url(path), requestInit);
79
+ }
80
+ catch (err) {
81
+ throw new ClawlingApiError("transport", `fetch failed: ${err instanceof Error ? err.message : String(err)}`, { path });
82
+ }
83
+ return await readEnvelope(res, path);
84
+ }
85
+ // All JSON API endpoints live under `/v1/...`. Media upload is the one
86
+ // intentional exception — the upstream server mounts it at `/media/upload`
87
+ // without the version prefix.
88
+ return {
89
+ async getMyProfile() {
90
+ return await call("GET", "/v1/users/me");
91
+ },
92
+ async getUserInfo(userId) {
93
+ return await call("GET", `/v1/users/${encodeURIComponent(userId)}`);
94
+ },
95
+ async listFriends(params) {
96
+ const sp = new URLSearchParams();
97
+ if (typeof params.page === "number")
98
+ sp.set("page", String(params.page));
99
+ if (typeof params.pageSize === "number")
100
+ sp.set("pageSize", String(params.pageSize));
101
+ const q = sp.toString();
102
+ return await call("GET", q ? `/v1/friends?${q}` : "/v1/friends");
103
+ },
104
+ async updateMyProfile(patch) {
105
+ if (!opts.userId?.trim()) {
106
+ throw new ClawlingApiError("validation", "updateMyProfile: userId is required to target /v1/agents/{userId}");
107
+ }
108
+ return await call("PATCH", `/v1/users/me`, {
109
+ body: JSON.stringify(patch),
110
+ headers: { "content-type": "application/json" },
111
+ });
112
+ },
113
+ async agentsConnect({ code: inviteCode, platform, type }) {
114
+ if (!inviteCode?.trim()) {
115
+ throw new ClawlingApiError("validation", "agentsConnect: inviteCode is required");
116
+ }
117
+ if (!platform?.trim()) {
118
+ throw new ClawlingApiError("validation", "agentsConnect: platform is required");
119
+ }
120
+ if (!type?.trim()) {
121
+ throw new ClawlingApiError("validation", "agentsConnect: type is required");
122
+ }
123
+ return await call("POST", "/v1/agents/connect", {
124
+ // `X-Device-Id` is added globally via `authHeaders` on every request.
125
+ headers: { "content-type": "application/json" },
126
+ body: JSON.stringify({
127
+ code: inviteCode.trim(),
128
+ platform: platform.trim(),
129
+ type: type.trim(),
130
+ }),
131
+ });
132
+ },
133
+ async uploadMedia(params) {
134
+ const blob = new Blob([new Uint8Array(params.buffer)], {
135
+ type: params.mime ?? "application/octet-stream",
136
+ });
137
+ const file = new File([blob], params.filename, {
138
+ type: params.mime ?? "application/octet-stream",
139
+ });
140
+ const fd = new FormData();
141
+ fd.set("file", file);
142
+ return await call("POST", "/media/upload", { body: fd });
143
+ },
144
+ async uploadAvatar(params) {
145
+ const blob = new Blob([new Uint8Array(params.buffer)], {
146
+ type: params.mime ?? "application/octet-stream",
147
+ });
148
+ const file = new File([blob], params.filename, {
149
+ type: params.mime ?? "application/octet-stream",
150
+ });
151
+ const fd = new FormData();
152
+ fd.set("file", file);
153
+ return await call("POST", "/v1/files/upload-url", { body: fd });
154
+ },
155
+ };
156
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Public-facing types for the Clawling Chat HTTP API.
3
+ *
4
+ * Field names mirror the upstream API (snake_case) so we don't lose
5
+ * fidelity on responses. Tool schemas accept camelCase externally and
6
+ * translate at the api-client boundary.
7
+ */
8
+ export class ClawlingApiError extends Error {
9
+ kind;
10
+ meta;
11
+ constructor(kind, message, meta) {
12
+ super(message);
13
+ this.kind = kind;
14
+ this.meta = meta;
15
+ this.name = "ClawlingApiError";
16
+ }
17
+ }
@@ -0,0 +1,177 @@
1
+ import { emitStreamAdd, emitStreamCreated, emitStreamDone, emitStreamFailed, } from "./client.js";
2
+ /**
3
+ * Merge two views of the same progressively-revealed text.
4
+ *
5
+ * The agent runner may give us either:
6
+ * - full snapshots ("Hel", "Hello", "Hello, world") where each item is
7
+ * a superset of the previous; or
8
+ * - overlapping slices ("hello ", "world hello ") that don't share a
9
+ * prefix but share an overlap at the join.
10
+ *
11
+ * This helper returns a longest-sensible combined string. Ported from
12
+ * `clawling-channel/src/reply-dispatcher.ts`.
13
+ */
14
+ export function mergeStreamingText(previousText, nextText) {
15
+ const currentSnapshot = typeof previousText === "string" ? previousText : "";
16
+ const incomingText = typeof nextText === "string" ? nextText : "";
17
+ if (!incomingText)
18
+ return currentSnapshot;
19
+ if (!currentSnapshot || incomingText === currentSnapshot)
20
+ return incomingText;
21
+ if (incomingText.startsWith(currentSnapshot))
22
+ return incomingText;
23
+ if (currentSnapshot.startsWith(incomingText))
24
+ return currentSnapshot;
25
+ if (incomingText.includes(currentSnapshot))
26
+ return incomingText;
27
+ if (currentSnapshot.includes(incomingText))
28
+ return currentSnapshot;
29
+ const maxOverlap = Math.min(currentSnapshot.length, incomingText.length);
30
+ for (let overlap = maxOverlap; overlap > 0; overlap -= 1) {
31
+ if (currentSnapshot.slice(-overlap) === incomingText.slice(0, overlap)) {
32
+ return `${currentSnapshot}${incomingText.slice(overlap)}`;
33
+ }
34
+ }
35
+ return `${currentSnapshot}${incomingText}`;
36
+ }
37
+ function resolveRouting(options) {
38
+ if (options.routing)
39
+ return options.routing;
40
+ if (options.to)
41
+ return { chatId: options.to.id, chatType: options.to.type };
42
+ throw new Error("openclaw-clawchat buffered stream requires routing");
43
+ }
44
+ /**
45
+ * Build a streaming session wrapper around message.created/add/done events.
46
+ *
47
+ * Usage pattern (matching clawling-channel):
48
+ * const session = openBufferedStreamingSession({...});
49
+ * await session.queueSnapshot("Hel");
50
+ * await session.queueSnapshot("Hello");
51
+ * await session.queueDelta(", world");
52
+ * await session.done();
53
+ */
54
+ export function openBufferedStreamingSession(options) {
55
+ const routing = resolveRouting(options);
56
+ const emitTyping = options.emitTyping !== false;
57
+ if (emitTyping)
58
+ options.client.typing(routing.chatId, true);
59
+ emitStreamCreated(options.client, {
60
+ messageId: options.messageId,
61
+ routing,
62
+ });
63
+ let bufferedSnapshot = "";
64
+ let flushedSnapshot = "";
65
+ let sequence = -1;
66
+ let flushTimer = null;
67
+ let pendingFlush = Promise.resolve();
68
+ let closed = false;
69
+ const clearTimer = () => {
70
+ if (flushTimer) {
71
+ clearTimeout(flushTimer);
72
+ flushTimer = null;
73
+ }
74
+ };
75
+ const performFlush = async () => {
76
+ clearTimer();
77
+ if (closed)
78
+ return;
79
+ if (bufferedSnapshot === flushedSnapshot)
80
+ return;
81
+ const snapshot = bufferedSnapshot;
82
+ const delta = snapshot.slice(flushedSnapshot.length);
83
+ if (!delta)
84
+ return;
85
+ sequence += 1;
86
+ emitStreamAdd(options.client, {
87
+ messageId: options.messageId,
88
+ routing,
89
+ sequence,
90
+ fullText: snapshot,
91
+ textDelta: delta,
92
+ });
93
+ flushedSnapshot = snapshot;
94
+ };
95
+ const flush = async () => {
96
+ pendingFlush = pendingFlush.then(performFlush);
97
+ await pendingFlush;
98
+ };
99
+ const scheduleFlush = () => {
100
+ if (flushTimer || closed)
101
+ return;
102
+ flushTimer = setTimeout(() => {
103
+ flushTimer = null;
104
+ void flush();
105
+ }, options.flushIntervalMs);
106
+ };
107
+ const queueSnapshot = async (snapshot) => {
108
+ if (closed || !snapshot)
109
+ return;
110
+ const base = bufferedSnapshot.length >= flushedSnapshot.length ? bufferedSnapshot : flushedSnapshot;
111
+ const merged = mergeStreamingText(base, snapshot);
112
+ if (merged === bufferedSnapshot)
113
+ return;
114
+ bufferedSnapshot = merged;
115
+ const deltaChars = Math.max(0, bufferedSnapshot.length - flushedSnapshot.length);
116
+ if (deltaChars >= options.minChunkChars || bufferedSnapshot.length >= options.maxBufferChars) {
117
+ await flush();
118
+ }
119
+ else {
120
+ scheduleFlush();
121
+ }
122
+ };
123
+ const queueDelta = async (delta) => {
124
+ if (closed || !delta)
125
+ return;
126
+ bufferedSnapshot = `${bufferedSnapshot}${delta}`;
127
+ const deltaChars = Math.max(0, bufferedSnapshot.length - flushedSnapshot.length);
128
+ if (deltaChars >= options.minChunkChars || bufferedSnapshot.length >= options.maxBufferChars) {
129
+ await flush();
130
+ }
131
+ else {
132
+ scheduleFlush();
133
+ }
134
+ };
135
+ const done = async () => {
136
+ if (closed)
137
+ return;
138
+ await flush();
139
+ closed = true;
140
+ clearTimer();
141
+ emitStreamDone(options.client, {
142
+ messageId: options.messageId,
143
+ routing,
144
+ finalSequence: Math.max(sequence, 0),
145
+ finalText: bufferedSnapshot,
146
+ });
147
+ if (emitTyping)
148
+ options.client.typing(routing.chatId, false);
149
+ };
150
+ const fail = async (reason) => {
151
+ if (closed)
152
+ return;
153
+ closed = true;
154
+ clearTimer();
155
+ emitStreamFailed(options.client, {
156
+ messageId: options.messageId,
157
+ routing,
158
+ sequence: Math.max(sequence, 0),
159
+ ...(reason !== undefined ? { reason } : {}),
160
+ });
161
+ if (emitTyping)
162
+ options.client.typing(routing.chatId, false);
163
+ };
164
+ return {
165
+ get currentText() {
166
+ return bufferedSnapshot;
167
+ },
168
+ get flushedText() {
169
+ return flushedSnapshot;
170
+ },
171
+ queueSnapshot,
172
+ queueDelta,
173
+ flush,
174
+ done,
175
+ fail,
176
+ };
177
+ }
@@ -0,0 +1,191 @@
1
+ import { createTopLevelChannelConfigAdapter } from "openclaw/plugin-sdk/channel-config-helpers";
2
+ import { createChatChannelPlugin, } from "openclaw/plugin-sdk/core";
3
+ import { createEmptyChannelDirectoryAdapter } from "openclaw/plugin-sdk/directory-runtime";
4
+ import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/setup";
5
+ import { createComputedAccountStatusAdapter, createDefaultChannelRuntimeState, } from "openclaw/plugin-sdk/status-helpers";
6
+ import { CHANNEL_ID, listOpenclawClawlingAccountIds, mergeOpenclawClawchatToolAllow, openclawClawlingConfigSchema, resolveOpenclawClawlingAccount, } from "./config.js";
7
+ import { openclawClawlingOutbound } from "./outbound.js";
8
+ import { getOpenclawClawlingRuntime, startOpenclawClawlingGateway } from "./runtime.js";
9
+ const configAdapter = createTopLevelChannelConfigAdapter({
10
+ sectionKey: CHANNEL_ID,
11
+ resolveAccount: (cfg) => resolveOpenclawClawlingAccount(cfg),
12
+ listAccountIds: () => listOpenclawClawlingAccountIds(),
13
+ defaultAccountId: () => DEFAULT_ACCOUNT_ID,
14
+ deleteMode: "clear-fields",
15
+ clearBaseFields: [
16
+ "websocketUrl",
17
+ "baseUrl",
18
+ "token",
19
+ "userId",
20
+ "replyMode",
21
+ "forwardThinking",
22
+ "forwardToolCalls",
23
+ "richInteractions",
24
+ "enabled",
25
+ ],
26
+ resolveAllowFrom: (account) => account.allowFrom,
27
+ formatAllowFrom: () => [],
28
+ });
29
+ /**
30
+ * Invite-code setup adapter used by OpenClaw setup surfaces that already have
31
+ * a concrete plugin instance. This plugin does not advertise catalog-driven
32
+ * one-shot setup metadata because current hosts do not discover channels from
33
+ * `plugins.load.paths`.
34
+ *
35
+ * Setup takes exactly ONE input: `code` (an invite code). URL + token +
36
+ * userId come from the login flow which is triggered automatically in
37
+ * `afterAccountConfigWritten`.
38
+ *
39
+ * `applyAccountConfig` itself only marks the section `enabled: true`;
40
+ * credentials are written by `runOpenclawClawlingLogin` via the runtime config
41
+ * mutator after the `/v1/agents/connect` response lands.
42
+ */
43
+ const setupAdapter = {
44
+ resolveAccountId: () => DEFAULT_ACCOUNT_ID,
45
+ validateInput: ({ input }) => {
46
+ if (!input.code?.trim()) {
47
+ return "ClawChat invite code is required.";
48
+ }
49
+ return null;
50
+ },
51
+ applyAccountConfig: ({ cfg, }) => {
52
+ // Base config: just enable the channel. Credentials arrive via
53
+ // `afterAccountConfigWritten` → `runOpenclawClawlingLogin`.
54
+ const channels = (cfg.channels ?? {});
55
+ const current = (channels[CHANNEL_ID] ?? {});
56
+ return mergeOpenclawClawchatToolAllow({
57
+ ...cfg,
58
+ channels: {
59
+ ...channels,
60
+ [CHANNEL_ID]: { ...current, enabled: true },
61
+ },
62
+ });
63
+ },
64
+ afterAccountConfigWritten: async ({ cfg, input, runtime, }) => {
65
+ const code = input.code?.trim();
66
+ if (!code)
67
+ return;
68
+ // Lazy-import the login runtime to keep @clack/prompts / readline /
69
+ // config-runtime off the plugin's cold-start path. `readInviteCode`
70
+ // feeds the fixed code so the stdin prompt is skipped entirely.
71
+ const { runOpenclawClawlingLogin } = await import("./login.runtime.js");
72
+ await runOpenclawClawlingLogin({
73
+ cfg,
74
+ accountId: null,
75
+ runtime: { log: (message) => runtime.log(message) },
76
+ readInviteCode: async () => code,
77
+ mutateConfigFile: getOpenclawClawlingRuntime().config.mutateConfigFile,
78
+ });
79
+ },
80
+ };
81
+ export const openclawClawlingPlugin = createChatChannelPlugin({
82
+ base: {
83
+ id: CHANNEL_ID,
84
+ meta: {
85
+ id: CHANNEL_ID,
86
+ label: "Clawling Chat",
87
+ selectionLabel: "Clawling Chat",
88
+ docsPath: "/channels/openclaw-clawchat",
89
+ docsLabel: "openclaw-clawchat",
90
+ blurb: "Clawling Protocol v2 over WebSocket (chat-sdk).",
91
+ order: 110,
92
+ },
93
+ capabilities: {
94
+ chatTypes: ["direct", "group"],
95
+ media: true,
96
+ reactions: false,
97
+ threads: false,
98
+ polls: false,
99
+ blockStreaming: true,
100
+ },
101
+ reload: {
102
+ configPrefixes: [`channels.${CHANNEL_ID}`],
103
+ },
104
+ configSchema: {
105
+ schema: openclawClawlingConfigSchema,
106
+ },
107
+ config: {
108
+ ...configAdapter,
109
+ isConfigured: (account) => account.configured,
110
+ describeAccount: (account) => ({
111
+ accountId: account.accountId,
112
+ name: account.name,
113
+ enabled: account.enabled,
114
+ configured: account.configured,
115
+ }),
116
+ },
117
+ directory: createEmptyChannelDirectoryAdapter(),
118
+ setup: setupAdapter,
119
+ status: createComputedAccountStatusAdapter({
120
+ defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID, {
121
+ connected: false,
122
+ lastInboundAt: null,
123
+ lastOutboundAt: null,
124
+ }),
125
+ resolveAccountSnapshot: ({ account }) => ({
126
+ accountId: account.accountId,
127
+ name: account.name,
128
+ enabled: account.enabled,
129
+ configured: account.configured,
130
+ extra: {
131
+ websocketUrl: account.websocketUrl || null,
132
+ baseUrl: account.baseUrl || null,
133
+ userId: account.userId || null,
134
+ },
135
+ }),
136
+ }),
137
+ auth: {
138
+ login: async ({ cfg, accountId, runtime }) => {
139
+ // Lazy-load login.runtime: it pulls in @clack/prompts and other
140
+ // heavy modules that have no business loading on every plugin
141
+ // boot. Only the rare `openclaw channels login --channel
142
+ // openclaw-clawchat` invocation pays the import cost.
143
+ const { runOpenclawClawlingLogin } = await import("./login.runtime.js");
144
+ await runOpenclawClawlingLogin({
145
+ cfg,
146
+ accountId: accountId ?? null,
147
+ runtime: { log: (message) => runtime.log(message) },
148
+ mutateConfigFile: getOpenclawClawlingRuntime().config.mutateConfigFile,
149
+ });
150
+ },
151
+ },
152
+ gateway: {
153
+ startAccount: async (ctx) => {
154
+ const account = ctx.account ?? resolveOpenclawClawlingAccount(ctx.cfg);
155
+ if (!account.configured) {
156
+ throw new Error("Clawling Chat websocketUrl/token/userId are required");
157
+ }
158
+ return await startOpenclawClawlingGateway({
159
+ cfg: ctx.cfg,
160
+ account,
161
+ abortSignal: ctx.abortSignal,
162
+ setStatus: ctx.setStatus,
163
+ getStatus: ctx.getStatus,
164
+ log: ctx.log,
165
+ });
166
+ },
167
+ },
168
+ agentPrompt: {
169
+ messageToolHints: () => [
170
+ "To send an image or file to the current chat, use the message tool with action='send' and set 'media' to a local file path or a remote URL.",
171
+ "When the user asks you to find an image from the web, find a suitable HTTPS image URL and send it using the message tool with 'media' set to that URL — do NOT download the image first.",
172
+ "For configured ClawChat account profile, user profile, friends, avatar, or standalone media upload/share-link workflows, use `clawchat-account-tools` for tool-selection details.",
173
+ "For ClawChat account avatar changes using a local image, call `clawchat_upload_avatar_image` first, then `clawchat_update_account_profile` with `avatar_url`.",
174
+ "- Targeting: omit `target` to reply here; for a different chat use `target=\"cc:{chat_id}\"` for direct or `target=\"cc:group:{chat_id}\"` for group.",
175
+ "- ClawChat supports image / file / audio / video media alongside text.",
176
+ ],
177
+ },
178
+ messaging: {
179
+ normalizeTarget: (target) => target
180
+ .trim()
181
+ .replace(/^openclaw-clawchat:/i, "")
182
+ .replace(/^clawchat:/i, "")
183
+ .replace(/^cc:/i, ""),
184
+ targetResolver: {
185
+ looksLikeId: (raw, normalized) => Boolean((normalized ?? raw).trim()),
186
+ hint: "active-session",
187
+ },
188
+ },
189
+ },
190
+ outbound: openclawClawlingOutbound,
191
+ });