@agentchatme/openclaw 0.10.2 → 0.11.0
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/CHANGELOG.md +30 -0
- package/README.md +4 -3
- package/RUNBOOK.md +1 -1
- package/dist/binding/agents-anchor.cjs.map +1 -1
- package/dist/binding/agents-anchor.d.cts +1 -1
- package/dist/binding/agents-anchor.d.ts +1 -1
- package/dist/binding/agents-anchor.js.map +1 -1
- package/dist/index.cjs +355 -807
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -186
- package/dist/index.d.ts +28 -186
- package/dist/index.js +355 -803
- package/dist/index.js.map +1 -1
- package/dist/setup-entry.cjs +338 -778
- package/dist/setup-entry.cjs.map +1 -1
- package/dist/setup-entry.js +339 -779
- package/dist/setup-entry.js.map +1 -1
- package/openclaw.plugin.json +5 -5
- package/package.json +2 -2
- package/skills/agentchat/SKILL.md +64 -27
package/dist/setup-entry.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { buildChannelConfigSchema, defineChannelPluginEntry, defineSetupPluginEntry } from 'openclaw/plugin-sdk/channel-core';
|
|
2
2
|
import { setAccountEnabledInConfigSection, deleteAccountFromConfigSection } from 'openclaw/plugin-sdk/channel-plugin-common';
|
|
3
3
|
import { setSetupChannelEnabled, WizardCancelledError } from 'openclaw/plugin-sdk/setup';
|
|
4
|
-
import {
|
|
4
|
+
import { createHash } from 'crypto';
|
|
5
|
+
import { integration, AgentChatClient, ConnectionError, RealtimeClient } from 'agentchatme';
|
|
6
|
+
import { WAKE_MODE_ENV, LEGACY_WAKE_MODE_ENV, readWakeModeFromEnv, readLegacyReplyGateFailOpenFromEnv, LEGACY_REPLY_GATE_FAIL_OPEN_ENV, readReplyGateTimeoutMsFromEnv, API_KEY_ENV, readReplyGateEnabledFromEnv } from './credentials/read-env.js';
|
|
5
7
|
import { readApiKeyFromEnv } from './credentials/read-env.js';
|
|
6
8
|
import * as fs from 'fs';
|
|
7
9
|
import * as path2 from 'path';
|
|
8
|
-
import { WAKE_MODE_ENV, LEGACY_WAKE_MODE_ENV, readWakeModeFromEnv, readLegacyReplyGateFailOpenFromEnv, LEGACY_REPLY_GATE_FAIL_OPEN_ENV, readReplyGateTimeoutMsFromEnv, API_KEY_ENV, readReplyGateEnabledFromEnv } from './credentials/read-env.js';
|
|
9
10
|
import { resolveBoundAgentId, resolveOpenClawDefaultAgentId, resolveOpenClawStateDir } from './openclaw-layout.js';
|
|
10
11
|
import { removeAgentsAnchor, agentsAnchorPath, hasAgentsAnchorFor, writeAgentsAnchor, readAgentsAnchor } from './binding/agents-anchor.js';
|
|
11
12
|
import { z } from 'zod';
|
|
@@ -106,7 +107,7 @@ function applyAgentchatAccountPatch(cfg, accountId, patch) {
|
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
// src/version.ts
|
|
109
|
-
var PACKAGE_VERSION = "0.
|
|
110
|
+
var PACKAGE_VERSION = "0.11.0";
|
|
110
111
|
|
|
111
112
|
// src/client-identity.ts
|
|
112
113
|
var AGENTCHAT_CLIENT_NAME = "openclaw";
|
|
@@ -115,219 +116,6 @@ var AGENTCHAT_CLIENT_IDENTITY = {
|
|
|
115
116
|
version: PACKAGE_VERSION
|
|
116
117
|
};
|
|
117
118
|
|
|
118
|
-
// src/setup-client.ts
|
|
119
|
-
var DEFAULT_API_BASE = "https://api.agentchat.me";
|
|
120
|
-
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
121
|
-
async function validateApiKey(apiKey, opts = {}) {
|
|
122
|
-
if (!apiKey || typeof apiKey !== "string") {
|
|
123
|
-
return { ok: false, reason: "unauthorized", message: "API key is empty" };
|
|
124
|
-
}
|
|
125
|
-
const client = new AgentChatClient({
|
|
126
|
-
apiKey,
|
|
127
|
-
baseUrl: apiBaseOf(opts.apiBase),
|
|
128
|
-
clientIdentity: AGENTCHAT_CLIENT_IDENTITY,
|
|
129
|
-
timeoutMs: opts.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
130
|
-
retry: ONE_ATTEMPT,
|
|
131
|
-
...opts.fetch ? { fetch: opts.fetch } : {}
|
|
132
|
-
});
|
|
133
|
-
let me;
|
|
134
|
-
try {
|
|
135
|
-
me = await client.getMe();
|
|
136
|
-
} catch (err) {
|
|
137
|
-
const failure = describeFailure(err);
|
|
138
|
-
if (failure.kind === "network") {
|
|
139
|
-
const unreachable = failure.timedOut || /ECONN|ETIMEDOUT|ENOTFOUND|EAI_AGAIN/i.test(failure.message);
|
|
140
|
-
return {
|
|
141
|
-
ok: false,
|
|
142
|
-
reason: unreachable ? "unreachable" : "network-error",
|
|
143
|
-
message: `agentchat: GET /v1/agents/me failed: ${failure.message}`
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
const status = failure.status;
|
|
147
|
-
if (status === 401) return { ok: false, reason: "unauthorized", status, message: "API key is invalid or revoked" };
|
|
148
|
-
if (status === 403) return { ok: false, reason: "forbidden", status, message: "API key lacks permission to read /agents/me" };
|
|
149
|
-
if (status === 410) return { ok: false, reason: "deleted", status, message: "Agent account has been deleted" };
|
|
150
|
-
return { ok: false, reason: "server-error", status, message: `AgentChat API returned ${status}` };
|
|
151
|
-
}
|
|
152
|
-
const body = asRecord(me);
|
|
153
|
-
const email = typeof body?.email === "string" ? body.email : typeof body?.email_masked === "string" ? body.email_masked : null;
|
|
154
|
-
if (!body || typeof body.handle !== "string" || email === null || typeof body.created_at !== "string") {
|
|
155
|
-
return { ok: false, reason: "unexpected-shape", status: 200, message: "AgentChat /agents/me returned an unrecognized shape" };
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
ok: true,
|
|
159
|
-
agent: {
|
|
160
|
-
handle: body.handle,
|
|
161
|
-
displayName: typeof body.display_name === "string" ? body.display_name : null,
|
|
162
|
-
email,
|
|
163
|
-
createdAt: body.created_at
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
async function registerAgentStart(input, opts = {}) {
|
|
168
|
-
let data;
|
|
169
|
-
try {
|
|
170
|
-
data = await AgentChatClient.register({
|
|
171
|
-
email: input.email,
|
|
172
|
-
handle: input.handle,
|
|
173
|
-
...input.displayName !== void 0 ? { display_name: input.displayName } : {},
|
|
174
|
-
...input.description !== void 0 ? { description: input.description } : {},
|
|
175
|
-
baseUrl: apiBaseOf(opts.apiBase),
|
|
176
|
-
clientIdentity: AGENTCHAT_CLIENT_IDENTITY
|
|
177
|
-
});
|
|
178
|
-
} catch (err) {
|
|
179
|
-
const failure = describeFailure(err);
|
|
180
|
-
if (failure.kind === "network") return { ok: false, reason: "network-error", message: networkMessage(failure) };
|
|
181
|
-
const { status, code, message } = failure;
|
|
182
|
-
if (status === 400 && code === "INVALID_HANDLE") return { ok: false, reason: "invalid-handle", message, status };
|
|
183
|
-
if (status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status };
|
|
184
|
-
if (status === 409 && code === "HANDLE_TAKEN") return { ok: false, reason: "handle-taken", message, status };
|
|
185
|
-
const emailPolicy = status === 409 ? classifyEmailPolicyRejection(code) : void 0;
|
|
186
|
-
if (emailPolicy) return { ok: false, reason: emailPolicy, message, status, limit: readPolicyLimit(failure.details) };
|
|
187
|
-
if (status === 429) return { ok: false, reason: "rate-limited", message, status, retryAfterSeconds: failure.retryAfterSeconds };
|
|
188
|
-
if (status >= 500 || code === "OTP_FAILED") return { ok: false, reason: "otp-failed", message, status };
|
|
189
|
-
return { ok: false, reason: "server-error", status, message };
|
|
190
|
-
}
|
|
191
|
-
const pendingId = asRecord(data)?.pending_id;
|
|
192
|
-
if (typeof pendingId !== "string") {
|
|
193
|
-
return { ok: false, reason: "server-error", status: 200, message: "AgentChat /register returned no pending_id" };
|
|
194
|
-
}
|
|
195
|
-
return { ok: true, pendingId };
|
|
196
|
-
}
|
|
197
|
-
async function registerAgentVerify(input, opts = {}) {
|
|
198
|
-
let verified;
|
|
199
|
-
try {
|
|
200
|
-
verified = await AgentChatClient.verify(input.pendingId, input.code, {
|
|
201
|
-
baseUrl: apiBaseOf(opts.apiBase),
|
|
202
|
-
clientIdentity: AGENTCHAT_CLIENT_IDENTITY
|
|
203
|
-
});
|
|
204
|
-
} catch (err) {
|
|
205
|
-
const failure = describeFailure(err);
|
|
206
|
-
if (failure.kind === "network") return { ok: false, reason: "network-error", message: networkMessage(failure) };
|
|
207
|
-
const { status, code, message } = failure;
|
|
208
|
-
if (status === 400 && code === "EXPIRED") return { ok: false, reason: "expired", message, status };
|
|
209
|
-
if (status === 400 && code === "INVALID_CODE") return { ok: false, reason: "invalid-code", message, status };
|
|
210
|
-
if (status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status };
|
|
211
|
-
if (status === 409 && code === "HANDLE_TAKEN") return { ok: false, reason: "handle-taken", message, status };
|
|
212
|
-
const emailPolicy = status === 409 ? classifyEmailPolicyRejection(code) : void 0;
|
|
213
|
-
if (emailPolicy) return { ok: false, reason: emailPolicy, message, status, limit: readPolicyLimit(failure.details) };
|
|
214
|
-
if (status === 429) return { ok: false, reason: "rate-limited", message, status, retryAfterSeconds: failure.retryAfterSeconds };
|
|
215
|
-
return { ok: false, reason: "server-error", status, message };
|
|
216
|
-
}
|
|
217
|
-
const agent = asRecord(verified.agent);
|
|
218
|
-
if (typeof verified.apiKey !== "string" || !agent || typeof agent.handle !== "string" || typeof agent.email !== "string" || typeof agent.created_at !== "string") {
|
|
219
|
-
return { ok: false, reason: "unexpected-shape", status: 201, message: "AgentChat /register/verify returned an unrecognized shape" };
|
|
220
|
-
}
|
|
221
|
-
return {
|
|
222
|
-
ok: true,
|
|
223
|
-
apiKey: verified.apiKey,
|
|
224
|
-
agent: {
|
|
225
|
-
handle: agent.handle,
|
|
226
|
-
displayName: typeof agent.display_name === "string" ? agent.display_name : null,
|
|
227
|
-
email: agent.email,
|
|
228
|
-
createdAt: agent.created_at
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
async function recoverAgentStart(input, opts = {}) {
|
|
233
|
-
let data;
|
|
234
|
-
try {
|
|
235
|
-
data = await AgentChatClient.recover(input.email, {
|
|
236
|
-
handle: input.handle,
|
|
237
|
-
baseUrl: apiBaseOf(opts.apiBase),
|
|
238
|
-
clientIdentity: AGENTCHAT_CLIENT_IDENTITY
|
|
239
|
-
});
|
|
240
|
-
} catch (err) {
|
|
241
|
-
const failure = describeFailure(err);
|
|
242
|
-
if (failure.kind === "network") return { ok: false, reason: "network-error", message: networkMessage(failure) };
|
|
243
|
-
const { status, code, message: message2 } = failure;
|
|
244
|
-
if (status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message: message2, status };
|
|
245
|
-
if (status === 429) return { ok: false, reason: "rate-limited", message: message2, status, retryAfterSeconds: failure.retryAfterSeconds };
|
|
246
|
-
return { ok: false, reason: "server-error", status, message: message2 };
|
|
247
|
-
}
|
|
248
|
-
const body = asRecord(data);
|
|
249
|
-
const message = typeof body?.message === "string" ? body.message : "status 200";
|
|
250
|
-
if (typeof body?.pending_id !== "string") {
|
|
251
|
-
return {
|
|
252
|
-
ok: false,
|
|
253
|
-
reason: "unexpected-shape",
|
|
254
|
-
status: 200,
|
|
255
|
-
message: "AgentChat did not start a recovery (no pending_id in the response). Check that the email is the one this agent registered with."
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
return { ok: true, pendingId: body.pending_id, message };
|
|
259
|
-
}
|
|
260
|
-
async function recoverAgentVerify(input, opts = {}) {
|
|
261
|
-
let recovered;
|
|
262
|
-
try {
|
|
263
|
-
recovered = await AgentChatClient.recoverVerify(input.pendingId, input.code, {
|
|
264
|
-
baseUrl: apiBaseOf(opts.apiBase),
|
|
265
|
-
clientIdentity: AGENTCHAT_CLIENT_IDENTITY
|
|
266
|
-
});
|
|
267
|
-
} catch (err) {
|
|
268
|
-
const failure = describeFailure(err);
|
|
269
|
-
if (failure.kind === "network") return { ok: false, reason: "network-error", message: networkMessage(failure) };
|
|
270
|
-
const { status, code, message } = failure;
|
|
271
|
-
if (status === 400 && code === "EXPIRED") return { ok: false, reason: "expired", message, status };
|
|
272
|
-
if (status === 400 && code === "INVALID_CODE") return { ok: false, reason: "invalid-code", message, status };
|
|
273
|
-
if (status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status };
|
|
274
|
-
if (status === 409 && code === "HANDLE_REQUIRED") {
|
|
275
|
-
return { ok: false, reason: "handle-required", message, status, handles: readHandleList(failure.details) };
|
|
276
|
-
}
|
|
277
|
-
if (status === 429) return { ok: false, reason: "rate-limited", message, status, retryAfterSeconds: failure.retryAfterSeconds };
|
|
278
|
-
return { ok: false, reason: "server-error", status, message };
|
|
279
|
-
}
|
|
280
|
-
if (typeof recovered.apiKey !== "string" || typeof recovered.handle !== "string") {
|
|
281
|
-
return { ok: false, reason: "unexpected-shape", status: 200, message: "AgentChat /agents/recover/verify returned an unrecognized shape" };
|
|
282
|
-
}
|
|
283
|
-
return { ok: true, apiKey: recovered.apiKey, handle: recovered.handle };
|
|
284
|
-
}
|
|
285
|
-
function classifyEmailPolicyRejection(code) {
|
|
286
|
-
if (code === "EMAIL_LIMIT_REACHED" || code === "EMAIL_TAKEN") return "email-limit-reached";
|
|
287
|
-
if (code === "EMAIL_EXHAUSTED") return "email-exhausted";
|
|
288
|
-
return void 0;
|
|
289
|
-
}
|
|
290
|
-
function readPolicyLimit(details) {
|
|
291
|
-
if (!details || typeof details !== "object") return void 0;
|
|
292
|
-
const limit = details.limit;
|
|
293
|
-
return typeof limit === "number" && Number.isInteger(limit) && limit > 0 ? limit : void 0;
|
|
294
|
-
}
|
|
295
|
-
function readHandleList(details) {
|
|
296
|
-
if (!details || typeof details !== "object") return [];
|
|
297
|
-
const handles = details.handles;
|
|
298
|
-
if (!Array.isArray(handles)) return [];
|
|
299
|
-
return handles.filter((h) => typeof h === "string" && h.length > 0);
|
|
300
|
-
}
|
|
301
|
-
var ONE_ATTEMPT = { maxRetries: 0, baseDelayMs: 0, maxDelayMs: 0 };
|
|
302
|
-
function apiBaseOf(apiBase) {
|
|
303
|
-
return (apiBase ?? DEFAULT_API_BASE).replace(/\/+$/, "");
|
|
304
|
-
}
|
|
305
|
-
function describeFailure(err) {
|
|
306
|
-
if (err instanceof AgentChatError) {
|
|
307
|
-
const raw = asRecord(asRecord(err.details)?.body);
|
|
308
|
-
const code = typeof raw?.code === "string" ? raw.code : String(err.code ?? "");
|
|
309
|
-
const message2 = raw ? typeof raw.message === "string" ? raw.message : `status ${err.status}` : err.message || `status ${err.status}`;
|
|
310
|
-
const retryAfterMs = err instanceof RateLimitedError ? err.retryAfterMs : null;
|
|
311
|
-
return {
|
|
312
|
-
kind: "http",
|
|
313
|
-
status: err.status,
|
|
314
|
-
code,
|
|
315
|
-
message: message2,
|
|
316
|
-
details: raw ? raw.details : err.details,
|
|
317
|
-
...typeof retryAfterMs === "number" && Number.isFinite(retryAfterMs) ? { retryAfterSeconds: Math.ceil(retryAfterMs / 1e3) } : {}
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
321
|
-
const timedOut = err instanceof Error && err.name === "AbortError" || /timed out|abort/i.test(message);
|
|
322
|
-
return { kind: "network", message, timedOut };
|
|
323
|
-
}
|
|
324
|
-
function networkMessage(failure) {
|
|
325
|
-
return failure.timedOut ? "request timed out" : failure.message;
|
|
326
|
-
}
|
|
327
|
-
function asRecord(value) {
|
|
328
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
119
|
// src/integration/turn.ts
|
|
332
120
|
var MCP_SERVER_NAME = "agentchat";
|
|
333
121
|
var MCP_TOOL_DELIMITER = "__";
|
|
@@ -364,8 +152,8 @@ function sanitizeKeyPart(value) {
|
|
|
364
152
|
// src/integration/mcp.ts
|
|
365
153
|
var MCP_PATH = "/mcp";
|
|
366
154
|
var AUTH_HEADER_TEMPLATE = `Bearer \${${API_KEY_ENV}}`;
|
|
367
|
-
var
|
|
368
|
-
function hostedMcpUrl(apiBase =
|
|
155
|
+
var DEFAULT_API_BASE = "https://api.agentchat.me";
|
|
156
|
+
function hostedMcpUrl(apiBase = DEFAULT_API_BASE) {
|
|
369
157
|
return `${apiBase.replace(/\/+$/, "")}${MCP_PATH}`;
|
|
370
158
|
}
|
|
371
159
|
var CLIENT_HEADER = "X-AgentChat-Client";
|
|
@@ -375,14 +163,14 @@ function clientIdentityHeaders() {
|
|
|
375
163
|
function clientIdentityText() {
|
|
376
164
|
return `${CLIENT_HEADER}: ${AGENTCHAT_CLIENT_NAME}`;
|
|
377
165
|
}
|
|
378
|
-
function hostedMcpServerEntry(apiBase =
|
|
166
|
+
function hostedMcpServerEntry(apiBase = DEFAULT_API_BASE) {
|
|
379
167
|
return {
|
|
380
168
|
url: hostedMcpUrl(apiBase),
|
|
381
169
|
transport: "streamable-http",
|
|
382
170
|
headers: { Authorization: AUTH_HEADER_TEMPLATE, ...clientIdentityHeaders() }
|
|
383
171
|
};
|
|
384
172
|
}
|
|
385
|
-
function describeMcpEntry(cfg, apiBase =
|
|
173
|
+
function describeMcpEntry(cfg, apiBase = DEFAULT_API_BASE, apiKey) {
|
|
386
174
|
const servers = cfg?.mcp?.servers;
|
|
387
175
|
if (!servers || typeof servers !== "object" || Array.isArray(servers)) return "missing";
|
|
388
176
|
const entry = servers[MCP_SERVER_NAME];
|
|
@@ -415,7 +203,7 @@ function describeMcpIdentity(cfg) {
|
|
|
415
203
|
const client = clients[0];
|
|
416
204
|
return clients.length === 1 && typeof client === "string" && client.trim().toLowerCase() === want[CLIENT_HEADER] ? "current" : "missing";
|
|
417
205
|
}
|
|
418
|
-
function withHostedMcpEntry(cfg, apiBase =
|
|
206
|
+
function withHostedMcpEntry(cfg, apiBase = DEFAULT_API_BASE, apiKey) {
|
|
419
207
|
const state = describeMcpEntry(cfg, apiBase, apiKey);
|
|
420
208
|
const base = cfg ?? {};
|
|
421
209
|
if (state === "user_managed") return { cfg: base, outcome: "user_managed" };
|
|
@@ -428,7 +216,7 @@ function withHostedMcpEntry(cfg, apiBase = DEFAULT_API_BASE2, apiKey) {
|
|
|
428
216
|
mcp.servers = servers;
|
|
429
217
|
return { cfg: { ...base, mcp }, outcome: state === "ours" ? "updated" : "added" };
|
|
430
218
|
}
|
|
431
|
-
function upgradedHostedMcpEntry(entry, apiBase =
|
|
219
|
+
function upgradedHostedMcpEntry(entry, apiBase = DEFAULT_API_BASE) {
|
|
432
220
|
const headers = isRecord(entry.headers) ? entry.headers : {};
|
|
433
221
|
const kept = Object.fromEntries(Object.entries(headers).filter(([name]) => name.toLowerCase() !== CLIENT_HEADER.toLowerCase()));
|
|
434
222
|
return {
|
|
@@ -438,7 +226,7 @@ function upgradedHostedMcpEntry(entry, apiBase = DEFAULT_API_BASE2) {
|
|
|
438
226
|
headers: { ...kept, ...clientIdentityHeaders() }
|
|
439
227
|
};
|
|
440
228
|
}
|
|
441
|
-
function upgradeHostedMcpEntryInPlace(draft, apiBase =
|
|
229
|
+
function upgradeHostedMcpEntryInPlace(draft, apiBase = DEFAULT_API_BASE, apiKey) {
|
|
442
230
|
if (describeMcpEntry(draft, apiBase, apiKey) !== "ours" || describeMcpIdentity(draft) === "current") return false;
|
|
443
231
|
const servers = draft.mcp.servers;
|
|
444
232
|
const existing = servers[MCP_SERVER_NAME];
|
|
@@ -455,7 +243,7 @@ var NoChange = class extends Error {
|
|
|
455
243
|
};
|
|
456
244
|
var NIX_MODE_ERROR_CODE = "OPENCLAW_NIX_MODE_CONFIG_IMMUTABLE";
|
|
457
245
|
async function refreshHostedMcpEntry(params) {
|
|
458
|
-
const apiBase = params.apiBase ??
|
|
246
|
+
const apiBase = params.apiBase ?? DEFAULT_API_BASE;
|
|
459
247
|
if (describeMcpEntry(params.liveCfg, apiBase, params.apiKey) !== "ours") return { outcome: "not_ours" };
|
|
460
248
|
if (describeMcpIdentity(params.liveCfg) === "current") return { outcome: "current" };
|
|
461
249
|
const mutate = params.config?.mutateConfigFile;
|
|
@@ -492,7 +280,7 @@ function headerValues(headers, name) {
|
|
|
492
280
|
const wanted = name.toLowerCase();
|
|
493
281
|
return Object.entries(headers).filter(([key]) => key.toLowerCase() === wanted).map(([, value]) => value);
|
|
494
282
|
}
|
|
495
|
-
function withoutHostedMcpEntry(cfg, apiBase =
|
|
283
|
+
function withoutHostedMcpEntry(cfg, apiBase = DEFAULT_API_BASE, apiKey) {
|
|
496
284
|
if (describeMcpEntry(cfg, apiBase, apiKey) !== "ours") return { cfg, removed: false };
|
|
497
285
|
const mcp = { ...cfg.mcp ?? {} };
|
|
498
286
|
const servers = { ...mcp.servers ?? {} };
|
|
@@ -502,7 +290,7 @@ function withoutHostedMcpEntry(cfg, apiBase = DEFAULT_API_BASE2, apiKey) {
|
|
|
502
290
|
}
|
|
503
291
|
async function probeHostedMcp(params) {
|
|
504
292
|
const result = await integration.probeHostedTools({
|
|
505
|
-
apiBase: params.apiBase ??
|
|
293
|
+
apiBase: params.apiBase ?? DEFAULT_API_BASE,
|
|
506
294
|
apiKey: params.apiKey,
|
|
507
295
|
clientIdentity: AGENTCHAT_CLIENT_IDENTITY,
|
|
508
296
|
...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
|
|
@@ -521,6 +309,40 @@ async function probeHostedMcp(params) {
|
|
|
521
309
|
};
|
|
522
310
|
}
|
|
523
311
|
|
|
312
|
+
// src/setup-client.ts
|
|
313
|
+
var KEY_CHECK_TIMEOUT_MS = 1e4;
|
|
314
|
+
function accountCallOptions(params = {}) {
|
|
315
|
+
return {
|
|
316
|
+
clientIdentity: AGENTCHAT_CLIENT_IDENTITY,
|
|
317
|
+
apiBase: (params.apiBase ?? DEFAULT_API_BASE).replace(/\/+$/, ""),
|
|
318
|
+
...params.fetch ? { fetch: params.fetch } : {},
|
|
319
|
+
...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {}
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
var KeyCheckUnanswered = class extends Error {
|
|
323
|
+
constructor(message) {
|
|
324
|
+
super(message);
|
|
325
|
+
this.name = "KeyCheckUnanswered";
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
async function checkApiKey(apiKey, params = {}) {
|
|
329
|
+
let status;
|
|
330
|
+
const send = params.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
331
|
+
const observed = (async (input, init) => {
|
|
332
|
+
const response = await send(input, init);
|
|
333
|
+
status = response.status;
|
|
334
|
+
return response;
|
|
335
|
+
});
|
|
336
|
+
const check = await integration.account.checkKey(
|
|
337
|
+
apiKey,
|
|
338
|
+
accountCallOptions({ timeoutMs: KEY_CHECK_TIMEOUT_MS, ...params, fetch: observed })
|
|
339
|
+
);
|
|
340
|
+
if (!check.ok && status !== 401 && status !== 403) {
|
|
341
|
+
throw new KeyCheckUnanswered(`AgentChat answered ${status ?? "without a status"} (${check.code}): ${check.message}`);
|
|
342
|
+
}
|
|
343
|
+
return check;
|
|
344
|
+
}
|
|
345
|
+
|
|
524
346
|
// src/integration/runtime-holder.ts
|
|
525
347
|
var current = null;
|
|
526
348
|
function isUsablePluginRuntime(runtime) {
|
|
@@ -672,14 +494,14 @@ var FAILURE_STATUSES = /* @__PURE__ */ new Set([
|
|
|
672
494
|
"invalid"
|
|
673
495
|
]);
|
|
674
496
|
function coversWholeSession(rows) {
|
|
675
|
-
if (
|
|
497
|
+
if (asRecord(rows[0]?.__openclaw)?.seq !== 1) return false;
|
|
676
498
|
return rows.every((row) => {
|
|
677
|
-
const meta =
|
|
499
|
+
const meta = asRecord(row.__openclaw);
|
|
678
500
|
return meta?.kind !== "compaction" && meta?.kind !== "reset" && meta?.truncated !== true;
|
|
679
501
|
});
|
|
680
502
|
}
|
|
681
503
|
function isOtherRun(row, runId) {
|
|
682
|
-
const tagged =
|
|
504
|
+
const tagged = asRecord(row.__openclaw)?.runId ?? nestedCallOf(row)?.runId;
|
|
683
505
|
return typeof tagged === "string" && tagged.trim().length > 0 && tagged.trim() !== runId;
|
|
684
506
|
}
|
|
685
507
|
var CODE_MODE_ONLY_TOOL_NAMES = /* @__PURE__ */ new Set(["tool_search_code", "wait"]);
|
|
@@ -691,7 +513,7 @@ function codeModeScriptsCompleted(rows, calls) {
|
|
|
691
513
|
for (const row of rows) {
|
|
692
514
|
if ((row.role === "toolResult" || row.role === "tool") && typeof row.toolCallId === "string") results.set(row.toolCallId, row);
|
|
693
515
|
}
|
|
694
|
-
const completed = (row) => row !== void 0 && row.isError !== true &&
|
|
516
|
+
const completed = (row) => row !== void 0 && row.isError !== true && asRecord(row.details)?.status === "completed";
|
|
695
517
|
for (const [id, call] of calls) {
|
|
696
518
|
if (isCodeModeCall(call) && !completed(results.get(id))) return false;
|
|
697
519
|
}
|
|
@@ -710,7 +532,7 @@ function isShellExecResult(row, calls) {
|
|
|
710
532
|
const call = typeof row.toolCallId === "string" ? calls.get(row.toolCallId) : void 0;
|
|
711
533
|
const args = callArguments(call);
|
|
712
534
|
if (!args || typeof args.command !== "string" || args.code !== void 0) return false;
|
|
713
|
-
const details =
|
|
535
|
+
const details = asRecord(row.details);
|
|
714
536
|
if (!details || details.telemetry !== void 0) return false;
|
|
715
537
|
return details.exitCode !== void 0 || typeof details.durationMs === "number" || typeof details.aggregated === "string";
|
|
716
538
|
}
|
|
@@ -719,7 +541,7 @@ function assistantToolCalls(rows) {
|
|
|
719
541
|
for (const row of rows) {
|
|
720
542
|
if (row.role !== "assistant" || !Array.isArray(row.content)) continue;
|
|
721
543
|
for (const block of row.content) {
|
|
722
|
-
const call =
|
|
544
|
+
const call = asRecord(block);
|
|
723
545
|
if (call?.type === "toolCall" && typeof call.id === "string") calls.set(call.id, call);
|
|
724
546
|
}
|
|
725
547
|
}
|
|
@@ -732,7 +554,7 @@ function releaseOf(rows, calls, nested, version) {
|
|
|
732
554
|
return { recordsNestedCalls: trusted, marksFailedRows: trusted };
|
|
733
555
|
}
|
|
734
556
|
const parents = new Set(nested.map((call) => call.parentToolCallId));
|
|
735
|
-
const dispatchAction = rows.some((row) => (row.role === "toolResult" || row.role === "tool") && toolNameOf(row) === "dispatch_action" &&
|
|
557
|
+
const dispatchAction = rows.some((row) => (row.role === "toolResult" || row.role === "tool") && toolNameOf(row) === "dispatch_action" && asRecord(asRecord(row.details)?.tool) !== null) || [...calls].some(([id, call]) => call.name === "dispatch_action" && parents.has(id));
|
|
736
558
|
return { recordsNestedCalls: nested.length > 0 || dispatchAction, marksFailedRows: dispatchAction };
|
|
737
559
|
}
|
|
738
560
|
var OPENCLAW_VERSION = /^(\d+)\.(\d+)\.(\d+)(-.+)?$/;
|
|
@@ -758,13 +580,13 @@ function toolNameOf(row) {
|
|
|
758
580
|
var NESTED_TOOL_ACTIVITY = "openclaw.nested-tool.v1";
|
|
759
581
|
function nestedCallOf(row) {
|
|
760
582
|
if (row.role !== "custom" || row.customType !== NESTED_TOOL_ACTIVITY) return null;
|
|
761
|
-
return
|
|
583
|
+
return asRecord(row.details);
|
|
762
584
|
}
|
|
763
585
|
function nestedVerdict(call) {
|
|
764
586
|
const toolName = typeof call.toolName === "string" ? call.toolName : "";
|
|
765
587
|
if (!toolName.startsWith(TOOL_PREFIX)) return "not_agentchat";
|
|
766
|
-
const result =
|
|
767
|
-
if (call.isError === true || result !== null && failedResult(result.isError,
|
|
588
|
+
const result = asRecord(call.result);
|
|
589
|
+
if (call.isError === true || result !== null && failedResult(result.isError, asRecord(result.details))) return "failed";
|
|
768
590
|
return call.isError === false ? "ok" : "unknown";
|
|
769
591
|
}
|
|
770
592
|
var OTHER_SESSION_TOOLS = /* @__PURE__ */ new Set(["sessions_spawn", "sessions_send"]);
|
|
@@ -775,13 +597,13 @@ function delegatedToChild(rows, calls, nested) {
|
|
|
775
597
|
if (typeof call.name === "string" && DISPATCHER_TOOL_NAMES.has(call.name) && names(dispatchTarget(call))) return true;
|
|
776
598
|
}
|
|
777
599
|
if (nested.some((call) => names(call.toolName))) return true;
|
|
778
|
-
return rows.some((row) => names(toolNameOf(row)) || names(
|
|
600
|
+
return rows.some((row) => names(toolNameOf(row)) || names(asRecord(asRecord(row.details)?.tool)?.name));
|
|
779
601
|
}
|
|
780
602
|
function callVerdict(message, calls, release) {
|
|
781
603
|
const toolName = toolNameOf(message);
|
|
782
|
-
const details =
|
|
604
|
+
const details = asRecord(message.details);
|
|
783
605
|
if (toolName.startsWith(TOOL_PREFIX)) return directVerdict(message.isError, details);
|
|
784
|
-
const entry =
|
|
606
|
+
const entry = asRecord(details?.tool);
|
|
785
607
|
if (entry && isAgentChatEntry(entry)) return dispatchedVerdict(message.isError, details);
|
|
786
608
|
if (!DISPATCHER_TOOL_NAMES.has(toolName) || entry) return "not_agentchat";
|
|
787
609
|
const target = dispatchTarget(typeof message.toolCallId === "string" ? calls.get(message.toolCallId) : void 0);
|
|
@@ -805,10 +627,10 @@ function wasHostedResult(details) {
|
|
|
805
627
|
return keys.includes("mcpServer") && (keys.includes("mcpTool") || keys.includes("mcpOperation"));
|
|
806
628
|
}
|
|
807
629
|
function dispatchedVerdict(isError, details) {
|
|
808
|
-
const result =
|
|
630
|
+
const result = asRecord(details?.result);
|
|
809
631
|
if (failedResult(isError, details)) return "failed";
|
|
810
|
-
if (result && failedResult(result.isError,
|
|
811
|
-
const ok = isError === false && details?.status === void 0 && isHostedSuccessDetails(
|
|
632
|
+
if (result && failedResult(result.isError, asRecord(result.details))) return "failed";
|
|
633
|
+
const ok = isError === false && details?.status === void 0 && isHostedSuccessDetails(asRecord(result?.details));
|
|
812
634
|
return ok ? "ok" : "unknown";
|
|
813
635
|
}
|
|
814
636
|
function failedResult(isError, details) {
|
|
@@ -832,7 +654,7 @@ function isHostedSuccessDetails(details) {
|
|
|
832
654
|
function isAgentChatEntry(tool) {
|
|
833
655
|
if (typeof tool.name === "string" && tool.name.startsWith(TOOL_PREFIX)) return true;
|
|
834
656
|
if (tool.sourceName === MCP_SERVER_NAME) return true;
|
|
835
|
-
const mcp =
|
|
657
|
+
const mcp = asRecord(tool.mcp);
|
|
836
658
|
return mcp?.serverName === MCP_SERVER_NAME;
|
|
837
659
|
}
|
|
838
660
|
function dispatchTarget(call) {
|
|
@@ -852,16 +674,16 @@ function callArguments(call) {
|
|
|
852
674
|
return null;
|
|
853
675
|
}
|
|
854
676
|
}
|
|
855
|
-
return
|
|
677
|
+
return asRecord(args);
|
|
856
678
|
}
|
|
857
679
|
function unwrapMessage(row) {
|
|
858
|
-
const record =
|
|
680
|
+
const record = asRecord(row);
|
|
859
681
|
if (!record) return null;
|
|
860
682
|
if (typeof record.role === "string") return record;
|
|
861
|
-
const inner =
|
|
683
|
+
const inner = asRecord(record.message);
|
|
862
684
|
return inner && typeof inner.role === "string" ? inner : null;
|
|
863
685
|
}
|
|
864
|
-
function
|
|
686
|
+
function asRecord(value) {
|
|
865
687
|
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
866
688
|
}
|
|
867
689
|
function nonEmpty(value) {
|
|
@@ -1117,7 +939,7 @@ function applyIntegrationConfig(cfg, params) {
|
|
|
1117
939
|
const changes = [];
|
|
1118
940
|
let next = cfg;
|
|
1119
941
|
const bareHandle = (params.handle ?? "").trim().replace(/^@/, "");
|
|
1120
|
-
if (bareHandle && readAgentchatConfigField(next, params.accountId, "agentHandle") !== bareHandle
|
|
942
|
+
if (bareHandle && readAgentchatConfigField(next, params.accountId, "agentHandle") !== bareHandle) {
|
|
1121
943
|
next = applyAgentchatAccountPatch(next, params.accountId, { agentHandle: bareHandle });
|
|
1122
944
|
changes.push(`channels.agentchat${params.accountId === AGENTCHAT_DEFAULT_ACCOUNT_ID ? "" : `.accounts.${params.accountId}`}.agentHandle = ${bareHandle}`);
|
|
1123
945
|
}
|
|
@@ -1452,57 +1274,15 @@ function getClient({ accountId, config, options }) {
|
|
|
1452
1274
|
|
|
1453
1275
|
// src/channel.wizard.ts
|
|
1454
1276
|
var JUST_REGISTERED_SENTINEL = "_agentchatJustRegistered";
|
|
1455
|
-
var
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
function isValidHandleShape2(value) {
|
|
1460
|
-
return value.length >= HANDLE_MIN_LENGTH && value.length <= HANDLE_MAX_LENGTH && HANDLE_PATTERN2.test(value);
|
|
1461
|
-
}
|
|
1462
|
-
var OTP_PATTERN = /^\d{6}$/;
|
|
1277
|
+
var LABELS = {
|
|
1278
|
+
...integration.account.DEFAULT_LABELS,
|
|
1279
|
+
handle: "Handle (your @name on AgentChat)"
|
|
1280
|
+
};
|
|
1463
1281
|
function hasConfiguredKey(cfg, accountId) {
|
|
1464
1282
|
return isApiKeyPresent(readAgentchatConfigField(cfg, accountId, "apiKey"));
|
|
1465
1283
|
}
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
return (await prompter.text({
|
|
1469
|
-
message: opts.message ?? "Email \u2014 receives a 6-digit verification code",
|
|
1470
|
-
placeholder: "you@example.com",
|
|
1471
|
-
validate: (value) => {
|
|
1472
|
-
const trimmed = value.trim();
|
|
1473
|
-
if (!trimmed) return "Email is required";
|
|
1474
|
-
if (!EMAIL_PATTERN.test(trimmed)) return "Not a valid email format";
|
|
1475
|
-
return void 0;
|
|
1476
|
-
}
|
|
1477
|
-
})).trim();
|
|
1478
|
-
}
|
|
1479
|
-
async function promptHandle(prompter, opts = {}) {
|
|
1480
|
-
return (await prompter.text({
|
|
1481
|
-
message: opts.message ?? "Choose a handle (your @name on AgentChat)",
|
|
1482
|
-
placeholder: "3\u201330 chars, lowercase a-z, 0-9, hyphens, starts with a letter",
|
|
1483
|
-
...opts.initialValue ? { initialValue: opts.initialValue } : {},
|
|
1484
|
-
validate: (value) => {
|
|
1485
|
-
const trimmed = value.trim();
|
|
1486
|
-
if (!trimmed) return "Handle is required";
|
|
1487
|
-
if (trimmed.length < HANDLE_MIN_LENGTH || trimmed.length > HANDLE_MAX_LENGTH) {
|
|
1488
|
-
return `Length must be ${HANDLE_MIN_LENGTH}\u2013${HANDLE_MAX_LENGTH} chars (you entered ${trimmed.length})`;
|
|
1489
|
-
}
|
|
1490
|
-
if (!/^[a-z]/.test(trimmed)) return "Must start with a lowercase letter";
|
|
1491
|
-
if (/[^a-z0-9-]/.test(trimmed)) {
|
|
1492
|
-
return "Only lowercase letters, digits, and hyphens \u2014 no underscores, dots, or symbols";
|
|
1493
|
-
}
|
|
1494
|
-
if (trimmed.includes("--")) return "No consecutive hyphens";
|
|
1495
|
-
if (trimmed.endsWith("-")) return "Cannot end with a hyphen";
|
|
1496
|
-
return void 0;
|
|
1497
|
-
}
|
|
1498
|
-
})).trim();
|
|
1499
|
-
}
|
|
1500
|
-
async function promptDisplayName(prompter) {
|
|
1501
|
-
return (await prompter.text({
|
|
1502
|
-
message: "Display name (shown next to your @handle)",
|
|
1503
|
-
placeholder: "e.g. Anton, Builder Bot, Sasha",
|
|
1504
|
-
validate: () => void 0
|
|
1505
|
-
})).trim();
|
|
1284
|
+
function nonEmpty2(value) {
|
|
1285
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
1506
1286
|
}
|
|
1507
1287
|
async function runChangeApiBaseFlow(params) {
|
|
1508
1288
|
const { cfg, accountId, prompter } = params;
|
|
@@ -1540,388 +1320,182 @@ async function runChangeApiBaseFlow(params) {
|
|
|
1540
1320
|
await prompter.note(`API base set to ${input}`, "Updated");
|
|
1541
1321
|
return { cfg: patched };
|
|
1542
1322
|
}
|
|
1543
|
-
function
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
message: `${describeEmailPolicyRejection(email, result)} What next?`,
|
|
1550
|
-
options: [
|
|
1551
|
-
{
|
|
1552
|
-
value: "retry",
|
|
1553
|
-
label: "Use a different email address",
|
|
1554
|
-
hint: "a +alias like you+agent2@example.com counts as a separate email"
|
|
1555
|
-
},
|
|
1556
|
-
{
|
|
1557
|
-
value: "recover",
|
|
1558
|
-
label: "Recover the API key of an agent this email already backs",
|
|
1559
|
-
hint: "needs that agent\u2019s handle \u2014 a code goes to this email"
|
|
1560
|
-
},
|
|
1561
|
-
{ value: "paste", label: "Paste a key from an existing agent" },
|
|
1562
|
-
{ value: "cancel", label: "Cancel registration" }
|
|
1563
|
-
],
|
|
1564
|
-
initialValue: "retry"
|
|
1565
|
-
});
|
|
1323
|
+
async function askText(prompter, message, opts = {}) {
|
|
1324
|
+
return (await prompter.text({
|
|
1325
|
+
message,
|
|
1326
|
+
...opts,
|
|
1327
|
+
validate: (value) => value.trim() ? void 0 : "Required"
|
|
1328
|
+
})).trim();
|
|
1566
1329
|
}
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
"
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
);
|
|
1602
|
-
return "
|
|
1603
|
-
}
|
|
1604
|
-
if (startResult.ok) {
|
|
1605
|
-
startSpinner.stop(`Verification code sent to ${email}`);
|
|
1606
|
-
startedOk = true;
|
|
1607
|
-
break;
|
|
1608
|
-
}
|
|
1609
|
-
startSpinner.stop("Registration rejected");
|
|
1610
|
-
switch (startResult.reason) {
|
|
1611
|
-
case "invalid-handle":
|
|
1612
|
-
case "handle-taken": {
|
|
1613
|
-
const detail = startResult.reason === "handle-taken" ? `Handle @${handle} is already taken.` : "That handle is not acceptable (3\u201330 chars \u2014 lowercase letters/digits/hyphens; must start with a letter).";
|
|
1614
|
-
await prompter.note(`${detail} Pick a different handle and we'll try again.`, "Pick a different handle");
|
|
1615
|
-
handle = await promptHandle(prompter);
|
|
1616
|
-
continue;
|
|
1617
|
-
}
|
|
1618
|
-
case "email-limit-reached":
|
|
1619
|
-
case "email-exhausted": {
|
|
1620
|
-
const choice = await promptEmailPolicyChoice(prompter, email, {
|
|
1621
|
-
reason: startResult.reason,
|
|
1622
|
-
limit: startResult.limit,
|
|
1623
|
-
message: startResult.message
|
|
1624
|
-
});
|
|
1625
|
-
if (choice === "paste") return "user-chose-paste";
|
|
1626
|
-
if (choice === "recover") return "user-chose-recover";
|
|
1627
|
-
if (choice === "cancel") return "abort";
|
|
1628
|
-
email = await promptEmail(prompter);
|
|
1629
|
-
continue;
|
|
1630
|
-
}
|
|
1631
|
-
case "rate-limited": {
|
|
1632
|
-
const wait = startResult.retryAfterSeconds ? ` Try again in ${startResult.retryAfterSeconds}s.` : "";
|
|
1633
|
-
await prompter.note(`Too many registration attempts.${wait}`, "Rate limited");
|
|
1634
|
-
return "abort";
|
|
1635
|
-
}
|
|
1636
|
-
case "otp-failed": {
|
|
1637
|
-
await prompter.note(
|
|
1638
|
-
"The verification-code email could not be sent. Try again in a minute, or paste an existing key instead.",
|
|
1639
|
-
"OTP delivery failed"
|
|
1640
|
-
);
|
|
1641
|
-
return "abort";
|
|
1642
|
-
}
|
|
1643
|
-
case "network-error":
|
|
1644
|
-
case "server-error":
|
|
1645
|
-
case "validation":
|
|
1646
|
-
default: {
|
|
1647
|
-
await prompter.note(describeRegisterStartError(startResult), "Could not start registration");
|
|
1648
|
-
return "abort";
|
|
1649
|
-
}
|
|
1330
|
+
var EMAIL_PLACEHOLDER = "you@example.com";
|
|
1331
|
+
async function askCode(prompter, label) {
|
|
1332
|
+
for (; ; ) {
|
|
1333
|
+
const code = (await prompter.text({ message: label, placeholder: "leave blank for a new code or to cancel" })).trim();
|
|
1334
|
+
if (code) return { answer: "value", value: code };
|
|
1335
|
+
const choice = await prompter.select({
|
|
1336
|
+
message: "What would you like to do?",
|
|
1337
|
+
options: [
|
|
1338
|
+
{ value: "new_code", label: "Send a new code" },
|
|
1339
|
+
{ value: "enter", label: "Enter the code" },
|
|
1340
|
+
{ value: "cancel", label: "Cancel", hint: "paste an existing key instead, or leave setup" }
|
|
1341
|
+
],
|
|
1342
|
+
initialValue: "new_code"
|
|
1343
|
+
});
|
|
1344
|
+
if (choice === "new_code") return { answer: "new_code" };
|
|
1345
|
+
if (choice === "cancel") return { answer: "cancel" };
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
function askThrough(prompter) {
|
|
1349
|
+
return async (question) => {
|
|
1350
|
+
if (question.field === "new_code") {
|
|
1351
|
+
const yes = await prompter.confirm({ message: "Send a new code?", initialValue: true });
|
|
1352
|
+
return yes ? { answer: "new_code" } : { answer: "cancel" };
|
|
1353
|
+
}
|
|
1354
|
+
if (question.field === "code") return askCode(prompter, question.label ?? LABELS.code);
|
|
1355
|
+
const label = question.label ?? LABELS[question.field];
|
|
1356
|
+
const handles = (question.handles ?? []).filter((handle) => handle.length > 0);
|
|
1357
|
+
if (handles.length > 0) {
|
|
1358
|
+
const picked = await prompter.select({
|
|
1359
|
+
message: label,
|
|
1360
|
+
options: [
|
|
1361
|
+
...handles.map((handle) => ({ value: handle, label: `@${handle}` })),
|
|
1362
|
+
{ value: "", label: "Another handle" }
|
|
1363
|
+
]
|
|
1364
|
+
});
|
|
1365
|
+
if (picked) return { answer: "value", value: picked };
|
|
1650
1366
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
}
|
|
1659
|
-
const maxCodeAttempts = 3;
|
|
1660
|
-
let verifyResult = null;
|
|
1661
|
-
for (let attempt = 1; attempt <= maxCodeAttempts; attempt += 1) {
|
|
1662
|
-
const code = (await prompter.text({
|
|
1663
|
-
message: attempt === 1 ? "Enter the 6-digit verification code from your email" : `Verification code (attempt ${attempt}/${maxCodeAttempts})`,
|
|
1664
|
-
placeholder: "123456",
|
|
1665
|
-
validate: (value) => {
|
|
1666
|
-
const trimmed = value.trim();
|
|
1667
|
-
if (!trimmed) return "Code is required";
|
|
1668
|
-
if (!OTP_PATTERN.test(trimmed)) return "Code is 6 digits";
|
|
1669
|
-
return void 0;
|
|
1670
|
-
}
|
|
1671
|
-
})).trim();
|
|
1672
|
-
const verifySpinner = prompter.progress("Verifying code\u2026");
|
|
1367
|
+
const typed = await askText(prompter, label, question.field === "email" ? { placeholder: EMAIL_PLACEHOLDER } : {});
|
|
1368
|
+
return { answer: "value", value: typed };
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
function fetchWithProgress(prompter) {
|
|
1372
|
+
return (async (input, init) => {
|
|
1373
|
+
const progress = prompter.progress("Contacting AgentChat\u2026");
|
|
1673
1374
|
try {
|
|
1674
|
-
|
|
1675
|
-
}
|
|
1676
|
-
|
|
1677
|
-
await prompter.note(
|
|
1678
|
-
`${err instanceof Error ? err.message : String(err)}. Try again, or paste an existing key instead.`,
|
|
1679
|
-
"Verification failed"
|
|
1680
|
-
);
|
|
1681
|
-
return "abort";
|
|
1682
|
-
}
|
|
1683
|
-
if (verifyResult.ok) {
|
|
1684
|
-
verifySpinner.stop(`Registered as @${verifyResult.agent.handle}`);
|
|
1685
|
-
break;
|
|
1686
|
-
}
|
|
1687
|
-
verifySpinner.stop("Verification failed");
|
|
1688
|
-
if (verifyResult.reason === "invalid-code" && attempt < maxCodeAttempts) {
|
|
1689
|
-
await prompter.note(
|
|
1690
|
-
"That code did not match. Check your email and try again.",
|
|
1691
|
-
"Invalid verification code"
|
|
1692
|
-
);
|
|
1693
|
-
continue;
|
|
1375
|
+
return await globalThis.fetch(input, init);
|
|
1376
|
+
} finally {
|
|
1377
|
+
progress.stop();
|
|
1694
1378
|
}
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
)
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
function sleepWithProgress(prompter) {
|
|
1382
|
+
const label = (seconds) => `Waiting ${seconds} s\u2026`;
|
|
1383
|
+
return (ms) => new Promise((resolve) => {
|
|
1384
|
+
let left = Math.ceil(ms / 1e3);
|
|
1385
|
+
const progress = prompter.progress(label(left));
|
|
1386
|
+
const tick = setInterval(() => {
|
|
1387
|
+
left -= 1;
|
|
1388
|
+
if (left > 0) progress.update(label(left));
|
|
1389
|
+
}, 1e3);
|
|
1390
|
+
setTimeout(() => {
|
|
1391
|
+
clearInterval(tick);
|
|
1392
|
+
progress.stop();
|
|
1393
|
+
resolve();
|
|
1394
|
+
}, ms);
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
async function registerInput(prompter) {
|
|
1710
1398
|
await prompter.note(
|
|
1711
1399
|
[
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1400
|
+
"Registration creates a new AgentChat agent tied to your email address.",
|
|
1401
|
+
"Your handle is the agent\u2019s @name on AgentChat. AgentChat emails you a",
|
|
1402
|
+
"code to confirm the address \u2014 check your inbox (and spam)."
|
|
1715
1403
|
].join("\n"),
|
|
1716
|
-
"AgentChat
|
|
1404
|
+
"AgentChat: register a new agent"
|
|
1717
1405
|
);
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
};
|
|
1725
|
-
}
|
|
1726
|
-
function describeRegisterStartError(result) {
|
|
1727
|
-
switch (result.reason) {
|
|
1728
|
-
case "invalid-handle":
|
|
1729
|
-
return "That handle is not acceptable. Try a different one (3\u201330 chars \u2014 lowercase letters/digits/hyphens; must start with a letter).";
|
|
1730
|
-
case "handle-taken":
|
|
1731
|
-
return "That handle is already taken. Try a different one.";
|
|
1732
|
-
case "email-limit-reached":
|
|
1733
|
-
return `${result.limit === void 0 ? result.message : `This email already backs ${result.limit} active agents \u2014 the per-email limit.`} Use a different email (a +alias works), recover a key for one of its agents, or paste an existing key.`;
|
|
1734
|
-
case "email-exhausted":
|
|
1735
|
-
return `${result.limit === void 0 ? result.message : `This email has used all ${result.limit} of its lifetime account registrations.`} Use a different email (a +alias works), or paste a key from an existing agent.`;
|
|
1736
|
-
case "rate-limited": {
|
|
1737
|
-
const wait = result.retryAfterSeconds ? ` Try again in ${result.retryAfterSeconds}s.` : "";
|
|
1738
|
-
return `Rate limited.${wait}`;
|
|
1739
|
-
}
|
|
1740
|
-
case "otp-failed":
|
|
1741
|
-
return "The verification-code email could not be sent. Try again in a minute.";
|
|
1742
|
-
case "network-error":
|
|
1743
|
-
case "server-error":
|
|
1744
|
-
case "validation":
|
|
1745
|
-
default:
|
|
1746
|
-
return result.message;
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
function describeRegisterVerifyError(result) {
|
|
1750
|
-
switch (result.reason) {
|
|
1751
|
-
case "expired":
|
|
1752
|
-
return "This code expired. Restart the wizard to receive a new one.";
|
|
1753
|
-
case "invalid-code":
|
|
1754
|
-
return "Too many incorrect codes. Restart the wizard to receive a new one.";
|
|
1755
|
-
case "handle-taken":
|
|
1756
|
-
return "Your chosen handle was claimed by another registration in the meantime. Restart with a different handle.";
|
|
1757
|
-
case "email-limit-reached":
|
|
1758
|
-
return `${result.limit === void 0 ? result.message : `This email reached its limit of ${result.limit} active agents while you were verifying.`} Restart with a different email (a +alias works), or paste an existing key.`;
|
|
1759
|
-
case "email-exhausted":
|
|
1760
|
-
return `${result.limit === void 0 ? result.message : `This email used all ${result.limit} of its lifetime account registrations while you were verifying.`} Restart with a different email (a +alias works), or paste an existing key.`;
|
|
1761
|
-
case "rate-limited": {
|
|
1762
|
-
const wait = result.retryAfterSeconds ? ` Try again in ${result.retryAfterSeconds}s.` : "";
|
|
1763
|
-
return `Rate limited.${wait}`;
|
|
1764
|
-
}
|
|
1765
|
-
case "network-error":
|
|
1766
|
-
case "server-error":
|
|
1767
|
-
case "unexpected-shape":
|
|
1768
|
-
case "validation":
|
|
1769
|
-
default:
|
|
1770
|
-
return result.message;
|
|
1771
|
-
}
|
|
1406
|
+
const email = await askText(prompter, LABELS.email, { placeholder: EMAIL_PLACEHOLDER });
|
|
1407
|
+
const handle = await askText(prompter, LABELS.handle);
|
|
1408
|
+
const displayName = (await prompter.text({
|
|
1409
|
+
message: "Display name (shown next to your @handle)",
|
|
1410
|
+
placeholder: "e.g. Anton, Builder Bot, Sasha"
|
|
1411
|
+
})).trim();
|
|
1412
|
+
return { email, handle, ...displayName ? { display_name: displayName } : {} };
|
|
1772
1413
|
}
|
|
1773
|
-
async function
|
|
1774
|
-
const { cfg, accountId, prompter, apiBase } = params;
|
|
1775
|
-
const storedHandle = readAgentchatConfigField(cfg, accountId, "agentHandle");
|
|
1776
|
-
const defaultHandle = storedHandle && isValidHandleShape2(storedHandle) ? storedHandle : void 0;
|
|
1414
|
+
async function recoverInput(prompter, configuredHandle) {
|
|
1777
1415
|
await prompter.note(
|
|
1778
1416
|
[
|
|
1779
|
-
"Recovery
|
|
1780
|
-
"
|
|
1781
|
-
"
|
|
1782
|
-
"is
|
|
1783
|
-
...defaultHandle ? ["", `@${defaultHandle} is configured here \u2014 press Enter at the handle prompt to recover it.`] : []
|
|
1417
|
+
"Recovery issues a new API key for an agent you registered. AgentChat",
|
|
1418
|
+
"emails a code to the address the agent registered with; the old key",
|
|
1419
|
+
"stops working once the new one is issued.",
|
|
1420
|
+
...configuredHandle ? ["", `@${configuredHandle} is configured here \u2014 press Enter at the handle prompt to recover it.`] : []
|
|
1784
1421
|
].join("\n"),
|
|
1785
1422
|
"AgentChat: recover a lost API key"
|
|
1786
1423
|
);
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1424
|
+
if (!configuredHandle) return {};
|
|
1425
|
+
const handle = (await prompter.text({ message: LABELS.handle, initialValue: configuredHandle })).trim();
|
|
1426
|
+
return handle ? { handle } : {};
|
|
1427
|
+
}
|
|
1428
|
+
async function notCompleted(prompter, kind) {
|
|
1429
|
+
await prompter.note(
|
|
1430
|
+
`${kind === "register" ? "Registration" : "Recovery"} was not completed. You can still paste an existing API key at the next prompt, or cancel the wizard.`,
|
|
1431
|
+
"Falling back to credential entry"
|
|
1432
|
+
);
|
|
1433
|
+
return void 0;
|
|
1434
|
+
}
|
|
1435
|
+
async function runAccountFlow(params) {
|
|
1436
|
+
const { kind, cfg, accountId, prompter } = params;
|
|
1437
|
+
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase");
|
|
1438
|
+
const input = kind === "register" ? await registerInput(prompter) : await recoverInput(prompter, nonEmpty2(readAgentchatConfigField(cfg, accountId, "agentHandle")));
|
|
1439
|
+
let result;
|
|
1796
1440
|
try {
|
|
1797
|
-
|
|
1441
|
+
result = await integration.account.run({
|
|
1442
|
+
kind,
|
|
1443
|
+
input,
|
|
1444
|
+
labels: LABELS,
|
|
1445
|
+
ask: askThrough(prompter),
|
|
1446
|
+
show: (message) => prompter.note(message, "AgentChat"),
|
|
1447
|
+
sleep: sleepWithProgress(prompter),
|
|
1448
|
+
...accountCallOptions({ apiBase, fetch: fetchWithProgress(prompter) })
|
|
1449
|
+
});
|
|
1798
1450
|
} catch (err) {
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
`${err instanceof Error ? err.message : String(err)}. Try again when the network is available, or paste an existing key instead.`,
|
|
1802
|
-
"Recovery failed"
|
|
1803
|
-
);
|
|
1804
|
-
return "abort";
|
|
1805
|
-
}
|
|
1806
|
-
if (!startResult.ok) {
|
|
1807
|
-
startSpinner.stop("Recovery rejected");
|
|
1808
|
-
await prompter.note(describeRecoverStartError(startResult), "Could not start recovery");
|
|
1809
|
-
return "abort";
|
|
1810
|
-
}
|
|
1811
|
-
startSpinner.stop(startResult.message);
|
|
1812
|
-
const maxCodeAttempts = 3;
|
|
1813
|
-
let verifyResult = null;
|
|
1814
|
-
for (let attempt = 1; attempt <= maxCodeAttempts; attempt += 1) {
|
|
1815
|
-
const code = (await prompter.text({
|
|
1816
|
-
message: attempt === 1 ? `Enter the 6-digit recovery code (check ${email}, including spam)` : `Recovery code (attempt ${attempt}/${maxCodeAttempts})`,
|
|
1817
|
-
placeholder: "123456",
|
|
1818
|
-
validate: (value) => {
|
|
1819
|
-
const trimmed = value.trim();
|
|
1820
|
-
if (!trimmed) return "Code is required";
|
|
1821
|
-
if (!OTP_PATTERN.test(trimmed)) return "Code is 6 digits";
|
|
1822
|
-
return void 0;
|
|
1823
|
-
}
|
|
1824
|
-
})).trim();
|
|
1825
|
-
const verifySpinner = prompter.progress("Verifying code\u2026");
|
|
1826
|
-
try {
|
|
1827
|
-
verifyResult = await recoverAgentVerify({ pendingId: startResult.pendingId, code }, { apiBase });
|
|
1828
|
-
} catch (err) {
|
|
1829
|
-
verifySpinner.stop("Could not reach AgentChat");
|
|
1830
|
-
await prompter.note(
|
|
1831
|
-
`${err instanceof Error ? err.message : String(err)}. Try again, or paste an existing key instead.`,
|
|
1832
|
-
"Recovery failed"
|
|
1833
|
-
);
|
|
1834
|
-
return "abort";
|
|
1835
|
-
}
|
|
1836
|
-
if (verifyResult.ok) {
|
|
1837
|
-
verifySpinner.stop(`Recovered @${verifyResult.handle}`);
|
|
1838
|
-
break;
|
|
1839
|
-
}
|
|
1840
|
-
verifySpinner.stop("Verification failed");
|
|
1841
|
-
if (verifyResult.reason === "invalid-code" && attempt < maxCodeAttempts) {
|
|
1842
|
-
await prompter.note(
|
|
1843
|
-
"That code did not match. Check your email and try again \u2014 if no code arrived, the handle and email may not belong to the same agent.",
|
|
1844
|
-
"Invalid recovery code"
|
|
1845
|
-
);
|
|
1846
|
-
continue;
|
|
1847
|
-
}
|
|
1848
|
-
await prompter.note(describeRecoverVerifyError(verifyResult), "Recovery failed");
|
|
1849
|
-
return "abort";
|
|
1850
|
-
}
|
|
1851
|
-
if (!verifyResult || !verifyResult.ok) {
|
|
1451
|
+
if (err instanceof WizardCancelledError) throw err;
|
|
1452
|
+
const unreached = err instanceof ConnectionError || err instanceof Error && err.name === "AbortError";
|
|
1852
1453
|
await prompter.note(
|
|
1853
|
-
|
|
1854
|
-
"Recovery failed
|
|
1454
|
+
err instanceof Error ? err.message : String(err),
|
|
1455
|
+
unreached ? "Could not reach AgentChat" : `${kind === "register" ? "Registration" : "Recovery"} failed`
|
|
1855
1456
|
);
|
|
1856
|
-
return
|
|
1457
|
+
return notCompleted(prompter, kind);
|
|
1857
1458
|
}
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
patch.agentHandle = verifyResult.handle;
|
|
1861
|
-
}
|
|
1862
|
-
const nextCfg = applyAgentchatAccountPatch(cfg, accountId, patch);
|
|
1459
|
+
if (result.outcome !== "done") return notCompleted(prompter, kind);
|
|
1460
|
+
const nextCfg = applyAgentchatAccountPatch(cfg, accountId, { apiKey: result.apiKey, agentHandle: result.handle });
|
|
1863
1461
|
await prompter.note(
|
|
1864
1462
|
[
|
|
1865
|
-
`Handle:
|
|
1866
|
-
`API key:
|
|
1867
|
-
"",
|
|
1868
|
-
"The previous key for this agent has been revoked."
|
|
1463
|
+
`Handle: @${result.handle}`,
|
|
1464
|
+
`API key: ${redactKey(result.apiKey)} (saved to your OpenClaw config)`
|
|
1869
1465
|
].join("\n"),
|
|
1870
|
-
"AgentChat API key recovered"
|
|
1466
|
+
kind === "register" ? "AgentChat account created" : "AgentChat API key recovered"
|
|
1871
1467
|
);
|
|
1872
1468
|
return {
|
|
1873
1469
|
cfg: nextCfg,
|
|
1874
1470
|
credentialValues: {
|
|
1875
|
-
token:
|
|
1471
|
+
token: result.apiKey,
|
|
1876
1472
|
[JUST_REGISTERED_SENTINEL]: "1"
|
|
1877
1473
|
}
|
|
1878
1474
|
};
|
|
1879
1475
|
}
|
|
1880
|
-
function describeRecoverStartError(result) {
|
|
1881
|
-
switch (result.reason) {
|
|
1882
|
-
case "rate-limited": {
|
|
1883
|
-
const wait = result.retryAfterSeconds ? ` Try again in ${result.retryAfterSeconds}s.` : "";
|
|
1884
|
-
return `Too many recovery attempts from this network.${wait}`;
|
|
1885
|
-
}
|
|
1886
|
-
case "validation":
|
|
1887
|
-
return `AgentChat rejected the request: ${result.message}`;
|
|
1888
|
-
case "network-error":
|
|
1889
|
-
case "server-error":
|
|
1890
|
-
case "unexpected-shape":
|
|
1891
|
-
default:
|
|
1892
|
-
return result.message;
|
|
1893
|
-
}
|
|
1894
|
-
}
|
|
1895
|
-
function describeRecoverVerifyError(result) {
|
|
1896
|
-
switch (result.reason) {
|
|
1897
|
-
case "expired":
|
|
1898
|
-
return "This recovery code expired. Restart the wizard to request a new one.";
|
|
1899
|
-
case "invalid-code":
|
|
1900
|
-
return "Too many incorrect codes. Restart the wizard to request a new one.";
|
|
1901
|
-
case "handle-required": {
|
|
1902
|
-
const handles = result.handles ?? [];
|
|
1903
|
-
const list = handles.length > 0 ? `
|
|
1904
|
-
|
|
1905
|
-
Agents on this email:
|
|
1906
|
-
${handles.map((h) => ` @${h}`).join("\n")}` : "";
|
|
1907
|
-
return `This email backs more than one agent. Run recovery again and enter the handle you want to recover.${list}`;
|
|
1908
|
-
}
|
|
1909
|
-
case "rate-limited": {
|
|
1910
|
-
const wait = result.retryAfterSeconds ? ` Try again in ${result.retryAfterSeconds}s.` : "";
|
|
1911
|
-
return `Rate limited.${wait}`;
|
|
1912
|
-
}
|
|
1913
|
-
case "network-error":
|
|
1914
|
-
case "server-error":
|
|
1915
|
-
case "unexpected-shape":
|
|
1916
|
-
case "validation":
|
|
1917
|
-
default:
|
|
1918
|
-
return result.message;
|
|
1919
|
-
}
|
|
1920
|
-
}
|
|
1921
1476
|
function redactKey(apiKey) {
|
|
1922
1477
|
if (apiKey.length < 12) return "\u2022\u2022\u2022\u2022";
|
|
1923
1478
|
return `${apiKey.slice(0, 8)}\u2026${apiKey.slice(-4)}`;
|
|
1924
1479
|
}
|
|
1480
|
+
async function saveEnteredKey(cfg, accountId, value, key) {
|
|
1481
|
+
if (typeof value !== "string" || !nonEmpty2(key)) return cfg;
|
|
1482
|
+
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase");
|
|
1483
|
+
let check;
|
|
1484
|
+
try {
|
|
1485
|
+
check = await checkApiKey(key, { apiBase });
|
|
1486
|
+
} catch {
|
|
1487
|
+
check = null;
|
|
1488
|
+
}
|
|
1489
|
+
if (check !== null && !check.ok) return cfg;
|
|
1490
|
+
const handle = check?.handle;
|
|
1491
|
+
const sameKey = readAgentchatConfigField(cfg, accountId, "apiKey") === value;
|
|
1492
|
+
const withKey = applyAgentchatAccountPatch(cfg, accountId, {
|
|
1493
|
+
apiKey: value,
|
|
1494
|
+
// A new key whose handle is unknown must not keep the old key's handle.
|
|
1495
|
+
...handle ? { agentHandle: handle } : sameKey ? {} : { agentHandle: void 0 }
|
|
1496
|
+
});
|
|
1497
|
+
return applyIntegrationConfig(withKey, { accountId, handle, apiKey: key, apiBase: apiBase ?? DEFAULT_API_BASE }).cfg;
|
|
1498
|
+
}
|
|
1925
1499
|
var agentchatSetupWizard = {
|
|
1926
1500
|
channel: AGENTCHAT_CHANNEL_ID,
|
|
1927
1501
|
// AgentChat is one-agent-per-account by product design — the agent IS the
|
|
@@ -1966,9 +1540,9 @@ var agentchatSetupWizard = {
|
|
|
1966
1540
|
"AgentChat is a messaging platform for AI agents \u2014 direct messages,",
|
|
1967
1541
|
"groups, presence, attachments. Registration is free.",
|
|
1968
1542
|
"",
|
|
1969
|
-
"This wizard
|
|
1970
|
-
"API key, or
|
|
1971
|
-
"
|
|
1543
|
+
"This wizard registers a new agent by email code, accepts an existing",
|
|
1544
|
+
"API key, or recovers a lost key by email code \u2014 your choice in the",
|
|
1545
|
+
"next prompt."
|
|
1972
1546
|
]
|
|
1973
1547
|
},
|
|
1974
1548
|
prepare: async ({ cfg, accountId, credentialValues, prompter }) => {
|
|
@@ -2004,7 +1578,7 @@ var agentchatSetupWizard = {
|
|
|
2004
1578
|
options: [
|
|
2005
1579
|
{
|
|
2006
1580
|
value: "register",
|
|
2007
|
-
label: "Register a new agent (email
|
|
1581
|
+
label: "Register a new agent (email code)",
|
|
2008
1582
|
hint: "no account yet \u2014 the wizard creates one"
|
|
2009
1583
|
},
|
|
2010
1584
|
{
|
|
@@ -2014,64 +1588,14 @@ var agentchatSetupWizard = {
|
|
|
2014
1588
|
},
|
|
2015
1589
|
{
|
|
2016
1590
|
value: "recover",
|
|
2017
|
-
label: "Recover a lost API key (
|
|
1591
|
+
label: "Recover a lost API key (email code)",
|
|
2018
1592
|
hint: "an agent exists but its key is gone \u2014 re-issue it"
|
|
2019
1593
|
}
|
|
2020
1594
|
],
|
|
2021
1595
|
initialValue: "register"
|
|
2022
1596
|
});
|
|
2023
|
-
if (choice === "paste")
|
|
2024
|
-
|
|
2025
|
-
}
|
|
2026
|
-
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase");
|
|
2027
|
-
const recover = async () => {
|
|
2028
|
-
try {
|
|
2029
|
-
const result = await runRecoverFlow({ cfg, accountId, prompter, apiBase });
|
|
2030
|
-
if (result === "abort") {
|
|
2031
|
-
await prompter.note(
|
|
2032
|
-
"Recovery was not completed. You can still paste an existing API key at the next prompt, or cancel the wizard.",
|
|
2033
|
-
"Falling back to credential entry"
|
|
2034
|
-
);
|
|
2035
|
-
return;
|
|
2036
|
-
}
|
|
2037
|
-
return result;
|
|
2038
|
-
} catch (err) {
|
|
2039
|
-
if (err instanceof WizardCancelledError) throw err;
|
|
2040
|
-
await prompter.note(
|
|
2041
|
-
`${err instanceof Error ? err.message : String(err)}`,
|
|
2042
|
-
"Recovery flow failed"
|
|
2043
|
-
);
|
|
2044
|
-
return;
|
|
2045
|
-
}
|
|
2046
|
-
};
|
|
2047
|
-
if (choice === "recover") {
|
|
2048
|
-
return await recover();
|
|
2049
|
-
}
|
|
2050
|
-
let registerOutcome;
|
|
2051
|
-
try {
|
|
2052
|
-
registerOutcome = await runRegisterFlow({ cfg, accountId, prompter, apiBase });
|
|
2053
|
-
} catch (err) {
|
|
2054
|
-
if (err instanceof WizardCancelledError) throw err;
|
|
2055
|
-
await prompter.note(
|
|
2056
|
-
`${err instanceof Error ? err.message : String(err)}`,
|
|
2057
|
-
"Registration flow failed"
|
|
2058
|
-
);
|
|
2059
|
-
return;
|
|
2060
|
-
}
|
|
2061
|
-
if (registerOutcome === "abort") {
|
|
2062
|
-
await prompter.note(
|
|
2063
|
-
"Registration was not completed. You can still paste an existing API key at the next prompt, or cancel the wizard.",
|
|
2064
|
-
"Falling back to credential entry"
|
|
2065
|
-
);
|
|
2066
|
-
return;
|
|
2067
|
-
}
|
|
2068
|
-
if (registerOutcome === "user-chose-paste") {
|
|
2069
|
-
return;
|
|
2070
|
-
}
|
|
2071
|
-
if (registerOutcome === "user-chose-recover") {
|
|
2072
|
-
return await recover();
|
|
2073
|
-
}
|
|
2074
|
-
return registerOutcome;
|
|
1597
|
+
if (choice === "paste") return;
|
|
1598
|
+
return await runAccountFlow({ kind: choice, cfg, accountId, prompter });
|
|
2075
1599
|
},
|
|
2076
1600
|
credentials: [
|
|
2077
1601
|
{
|
|
@@ -2084,9 +1608,9 @@ var agentchatSetupWizard = {
|
|
|
2084
1608
|
inputPrompt: "Paste your AgentChat API key (ac_\u2026)",
|
|
2085
1609
|
helpTitle: "AgentChat API key",
|
|
2086
1610
|
helpLines: [
|
|
2087
|
-
"
|
|
2088
|
-
"
|
|
2089
|
-
"
|
|
1611
|
+
"AgentChat checks the key before it is saved: a key it does not",
|
|
1612
|
+
"accept is not saved, so a bad key fails here instead of flapping",
|
|
1613
|
+
"reconnects at runtime."
|
|
2090
1614
|
],
|
|
2091
1615
|
allowEnv: () => true,
|
|
2092
1616
|
shouldPrompt: ({ credentialValues, currentValue }) => {
|
|
@@ -2104,61 +1628,67 @@ var agentchatSetupWizard = {
|
|
|
2104
1628
|
resolvedValue: configured ? apiKey : void 0,
|
|
2105
1629
|
envValue
|
|
2106
1630
|
};
|
|
1631
|
+
},
|
|
1632
|
+
// The key the person entered, checked before it is saved.
|
|
1633
|
+
applySet: ({ cfg, accountId, value, resolvedValue }) => saveEnteredKey(cfg, accountId, value, resolvedValue),
|
|
1634
|
+
applyUseEnv: ({ cfg, accountId }) => {
|
|
1635
|
+
const key = readApiKeyFromEnv(MIN_API_KEY_LENGTH);
|
|
1636
|
+
return key ? saveEnteredKey(cfg, accountId, key, key) : cfg;
|
|
2107
1637
|
}
|
|
2108
1638
|
}
|
|
2109
1639
|
],
|
|
2110
1640
|
finalize: async ({ cfg, accountId, credentialValues, prompter }) => {
|
|
2111
|
-
const
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
}
|
|
1641
|
+
const stored = nonEmpty2(readAgentchatConfigField(cfg, accountId, "apiKey"));
|
|
1642
|
+
const apiKey = nonEmpty2(credentialValues.token) ?? stored;
|
|
1643
|
+
if (!apiKey) return;
|
|
2115
1644
|
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase");
|
|
2116
|
-
const spinner = prompter.progress("
|
|
1645
|
+
const spinner = prompter.progress("Checking the API key with AgentChat\u2026");
|
|
1646
|
+
let check;
|
|
2117
1647
|
try {
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
const handle = result.agent.handle;
|
|
2122
|
-
const resolvedBase = apiBase ?? DEFAULT_API_BASE2;
|
|
2123
|
-
const identityLine = await loadIdentityLine({ client: createSdkClient({ apiKey, apiBase: resolvedBase }), accountId });
|
|
2124
|
-
const placed = placeIntegration({ cfg, handle, apiKey, apiBase: resolvedBase, identityLine, accountId });
|
|
2125
|
-
const failed = placed.steps.filter((s) => s.status === "failed");
|
|
2126
|
-
const skipped = placed.steps.filter((s) => s.status === "skipped" && s.name === "anchor");
|
|
2127
|
-
await prompter.note(
|
|
2128
|
-
[
|
|
2129
|
-
...formatPlacement(placed.steps),
|
|
2130
|
-
"",
|
|
2131
|
-
placed.mcp === "added" || placed.steps.some((s) => s.name === "key" && s.status === "changed") ? "Restart the gateway once so its environment carries the key and it connects the hosted tools. Then `openclaw channels status` shows the integration and the tools." : "The agent is reachable: a peer's message wakes it; the agent decides with its own model; it answers through the hosted AgentChat tools.",
|
|
2132
|
-
...failed.length > 0 ? ["", "Some placement failed (above). Your account is configured; re-run `openclaw channels add agentchat` after fixing the path, or `openclaw doctor --fix` for the config entry."] : [],
|
|
2133
|
-
...skipped.length > 0 ? ["", "The identity line waits for AgentChat to be reachable: run `openclaw doctor --fix` then."] : []
|
|
2134
|
-
].join("\n"),
|
|
2135
|
-
"AgentChat integration placed"
|
|
2136
|
-
);
|
|
2137
|
-
const applied = applyIntegrationConfig(placed.cfg, { accountId, handle, apiKey, apiBase: resolvedBase });
|
|
2138
|
-
return { cfg: applied.cfg };
|
|
2139
|
-
}
|
|
2140
|
-
spinner.stop(`API key did not pass the live check (${result.reason})`);
|
|
1648
|
+
check = await checkApiKey(apiKey, { apiBase });
|
|
1649
|
+
} catch (err) {
|
|
1650
|
+
spinner.stop("Could not reach AgentChat to check the key");
|
|
2141
1651
|
await prompter.note(
|
|
2142
1652
|
[
|
|
2143
|
-
|
|
1653
|
+
err instanceof Error ? err.message : String(err),
|
|
2144
1654
|
"",
|
|
2145
|
-
"The config was saved \u2014
|
|
2146
|
-
"to replace the key, or edit ~/.openclaw/config.yaml directly."
|
|
1655
|
+
stored === apiKey ? "The config was saved \u2014 the runtime will retry on startup." : "The key was not saved."
|
|
2147
1656
|
].join("\n"),
|
|
2148
|
-
"AgentChat
|
|
1657
|
+
"AgentChat API unreachable"
|
|
2149
1658
|
);
|
|
2150
|
-
|
|
2151
|
-
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
if (!check.ok) {
|
|
1662
|
+
spinner.stop("AgentChat did not accept the API key");
|
|
2152
1663
|
await prompter.note(
|
|
2153
1664
|
[
|
|
2154
|
-
|
|
1665
|
+
check.message,
|
|
2155
1666
|
"",
|
|
2156
|
-
"
|
|
1667
|
+
stored === apiKey ? "This key is the one in your OpenClaw config. Re-run `openclaw channels add agentchat` \u2192 Replace the API key to recover a lost key or paste another one." : "The key was not saved. Re-run `openclaw channels add agentchat` to paste another key, register a new agent, or recover a lost key."
|
|
2157
1668
|
].join("\n"),
|
|
2158
|
-
"AgentChat API
|
|
1669
|
+
"AgentChat API key not accepted"
|
|
2159
1670
|
);
|
|
1671
|
+
return;
|
|
2160
1672
|
}
|
|
2161
|
-
|
|
1673
|
+
spinner.stop(`Authenticated as @${check.handle}`);
|
|
1674
|
+
const handle = check.handle;
|
|
1675
|
+
const resolvedBase = apiBase ?? DEFAULT_API_BASE;
|
|
1676
|
+
const identityLine = await loadIdentityLine({ client: createSdkClient({ apiKey, apiBase: resolvedBase }), accountId });
|
|
1677
|
+
const placed = placeIntegration({ cfg, handle, apiKey, apiBase: resolvedBase, identityLine, accountId });
|
|
1678
|
+
const failed = placed.steps.filter((s) => s.status === "failed");
|
|
1679
|
+
const skipped = placed.steps.filter((s) => s.status === "skipped" && s.name === "anchor");
|
|
1680
|
+
await prompter.note(
|
|
1681
|
+
[
|
|
1682
|
+
...formatPlacement(placed.steps),
|
|
1683
|
+
"",
|
|
1684
|
+
placed.mcp === "added" || placed.steps.some((s) => s.name === "key" && s.status === "changed") ? "Restart the gateway once so its environment carries the key and it connects the hosted tools. Then `openclaw channels status` shows the integration and the tools." : "The agent is reachable: a peer's message wakes it; the agent decides with its own model; it answers through the hosted AgentChat tools.",
|
|
1685
|
+
...failed.length > 0 ? ["", "Some placement failed (above). Your account is configured; re-run `openclaw channels add agentchat` after fixing the path, or `openclaw doctor --fix` for the config entry."] : [],
|
|
1686
|
+
...skipped.length > 0 ? ["", "The identity line waits for AgentChat to be reachable: run `openclaw doctor --fix` then."] : []
|
|
1687
|
+
].join("\n"),
|
|
1688
|
+
"AgentChat integration placed"
|
|
1689
|
+
);
|
|
1690
|
+
const applied = applyIntegrationConfig(placed.cfg, { accountId, handle, apiKey, apiBase: resolvedBase });
|
|
1691
|
+
return { cfg: applied.cfg };
|
|
2162
1692
|
},
|
|
2163
1693
|
completionNote: {
|
|
2164
1694
|
title: "AgentChat is ready",
|
|
@@ -3737,7 +3267,7 @@ var agentchatStatusAdapter = {
|
|
|
3737
3267
|
const servedFrom = live?.texts?.source;
|
|
3738
3268
|
const textsSource = live ? servedFrom === "server" || servedFrom === "cache" ? servedFrom : "missing" : void 0;
|
|
3739
3269
|
const socketCounters = live?.metrics?.snapshot?.();
|
|
3740
|
-
const
|
|
3270
|
+
const integration8 = {
|
|
3741
3271
|
mode: readWakeModeFromEnv().mode,
|
|
3742
3272
|
...health ? { state: health.state } : {},
|
|
3743
3273
|
...counters ? { wakes: counters.wakes, cycles: counters.cycles, turns: counters.turns } : {},
|
|
@@ -3754,7 +3284,7 @@ var agentchatStatusAdapter = {
|
|
|
3754
3284
|
pausedByOwner: me.paused_by_owner ?? "none",
|
|
3755
3285
|
mcp,
|
|
3756
3286
|
placement,
|
|
3757
|
-
integration:
|
|
3287
|
+
integration: integration8,
|
|
3758
3288
|
...refusal ? { refusal } : {},
|
|
3759
3289
|
...enabled ? {} : { disabled: agentchatReenableHint(cfg, account.accountId) }
|
|
3760
3290
|
};
|
|
@@ -3821,7 +3351,7 @@ var agentchatDoctorAdapter = {
|
|
|
3821
3351
|
continue;
|
|
3822
3352
|
}
|
|
3823
3353
|
const apiKey = readAgentchatConfigField(cfg, accountId, "apiKey");
|
|
3824
|
-
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ??
|
|
3354
|
+
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ?? DEFAULT_API_BASE;
|
|
3825
3355
|
const handle = readAgentchatConfigField(cfg, accountId, "agentHandle");
|
|
3826
3356
|
const agentId = resolvePlacementAgentId(cfg, accountId);
|
|
3827
3357
|
for (const check of checkPlacement({ cfg, handle, apiKey, apiBase, identityLine: cachedIdentityLine(accountId), agentId, accountId, env })) {
|
|
@@ -3860,16 +3390,16 @@ var agentchatDoctorAdapter = {
|
|
|
3860
3390
|
for (const accountId of listConfiguredAccountIds(cfg)) {
|
|
3861
3391
|
if (!isAgentchatAccountEnabled(cfg, accountId)) continue;
|
|
3862
3392
|
const apiKey = readAgentchatConfigField(cfg, accountId, "apiKey");
|
|
3863
|
-
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ??
|
|
3393
|
+
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ?? DEFAULT_API_BASE;
|
|
3864
3394
|
bases.set(apiBase, apiKey);
|
|
3865
3395
|
if (apiKey && !readAgentchatConfigField(cfg, accountId, "agentHandle")) {
|
|
3866
3396
|
try {
|
|
3867
|
-
const result = await
|
|
3397
|
+
const result = await checkApiKey(apiKey, { apiBase });
|
|
3868
3398
|
if (result.ok) {
|
|
3869
|
-
next = applyAgentchatAccountPatch(next, accountId, { agentHandle: result.
|
|
3870
|
-
changes.push(`set channels.agentchat${accountId === "default" ? "" : `.accounts.${accountId}`}.agentHandle = ${result.
|
|
3399
|
+
next = applyAgentchatAccountPatch(next, accountId, { agentHandle: result.handle });
|
|
3400
|
+
changes.push(`set channels.agentchat${accountId === "default" ? "" : `.accounts.${accountId}`}.agentHandle = ${result.handle}`);
|
|
3871
3401
|
} else {
|
|
3872
|
-
warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] agentHandle not set and the key did not pass the live check (${result.
|
|
3402
|
+
warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] agentHandle not set and the key did not pass the live check (${result.code}): ${result.message}`);
|
|
3873
3403
|
}
|
|
3874
3404
|
} catch (err) {
|
|
3875
3405
|
warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] agentHandle not set and AgentChat was not reachable to learn it: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -3924,7 +3454,7 @@ var uiHints = {
|
|
|
3924
3454
|
label: "AgentChat API key",
|
|
3925
3455
|
placeholder: "ac_\u2026",
|
|
3926
3456
|
sensitive: true,
|
|
3927
|
-
help: "The setup wizard registers
|
|
3457
|
+
help: "The setup wizard registers a new agent or recovers a lost key by email code, or accepts an existing ac_\u2026 key; setup checks a key with AgentChat before saving it."
|
|
3928
3458
|
},
|
|
3929
3459
|
apiBase: {
|
|
3930
3460
|
label: "API base URL",
|
|
@@ -3935,19 +3465,26 @@ var uiHints = {
|
|
|
3935
3465
|
agentHandle: {
|
|
3936
3466
|
label: "Agent handle",
|
|
3937
3467
|
placeholder: "my-agent",
|
|
3938
|
-
help: "
|
|
3468
|
+
help: "The handle the API key belongs to, as AgentChat returned it; setup and `openclaw doctor --fix` write it."
|
|
3939
3469
|
},
|
|
3940
3470
|
reconnect: { label: "Reconnect backoff", advanced: true },
|
|
3941
3471
|
ping: { label: "WebSocket ping cadence", advanced: true },
|
|
3942
3472
|
outbound: { label: "Outbound send tuning", advanced: true },
|
|
3943
3473
|
observability: { label: "Logs & metrics", advanced: true }
|
|
3944
3474
|
};
|
|
3945
|
-
|
|
3946
|
-
function validHandle(value) {
|
|
3475
|
+
function handleOf(value) {
|
|
3947
3476
|
if (typeof value !== "string") return void 0;
|
|
3948
3477
|
const bare = value.trim().replace(/^@/, "");
|
|
3949
|
-
return bare.length
|
|
3478
|
+
return bare.length > 0 ? bare : void 0;
|
|
3950
3479
|
}
|
|
3480
|
+
function keyRefusal(check) {
|
|
3481
|
+
return [
|
|
3482
|
+
`AgentChat did not accept the API key (${check.code}): ${check.message}`,
|
|
3483
|
+
"The key was not saved. Run `openclaw channels add agentchat` to register a new agent or recover a lost key (email code)."
|
|
3484
|
+
].join("\n");
|
|
3485
|
+
}
|
|
3486
|
+
var keyAskedBeforeWrite = null;
|
|
3487
|
+
var keyDigest = (apiKey) => createHash("sha256").update(apiKey).digest("hex");
|
|
3951
3488
|
function say(runtime, level, line) {
|
|
3952
3489
|
const env = runtime;
|
|
3953
3490
|
const fn = env?.[level];
|
|
@@ -3955,13 +3492,18 @@ function say(runtime, level, line) {
|
|
|
3955
3492
|
}
|
|
3956
3493
|
var agentchatSetupAdapter = {
|
|
3957
3494
|
/**
|
|
3958
|
-
* Pre-write gate:
|
|
3959
|
-
*
|
|
3960
|
-
*
|
|
3495
|
+
* Pre-write gate: a key AgentChat turned away in
|
|
3496
|
+
* `prepareAccountConfigInput` ends here (OpenClaw prints the line and
|
|
3497
|
+
* writes nothing); otherwise a cheap, sync check that the caller supplied
|
|
3498
|
+
* an API key that's at least plausibly shaped. On an OpenClaw without
|
|
3499
|
+
* `prepareAccountConfigInput`, the lifecycle hook refuses a rejected key
|
|
3500
|
+
* before the write.
|
|
3961
3501
|
*/
|
|
3962
3502
|
validateInput({ input }) {
|
|
3503
|
+
const refusal = input.keyRefusal;
|
|
3504
|
+
if (typeof refusal === "string" && refusal.length > 0) return refusal;
|
|
3963
3505
|
if (typeof input.token !== "string" || input.token.trim().length === 0) {
|
|
3964
|
-
return "apiKey is required \u2014 pass via --token, or run `openclaw channels add agentchat` to register a new agent or recover a lost key (
|
|
3506
|
+
return "apiKey is required \u2014 pass via --token, or run `openclaw channels add agentchat` to register a new agent or recover a lost key (email code)";
|
|
3965
3507
|
}
|
|
3966
3508
|
if (input.token.length < MIN_API_KEY_LENGTH) {
|
|
3967
3509
|
return `apiKey looks too short (got ${input.token.length} chars, expect \u2265${MIN_API_KEY_LENGTH})`;
|
|
@@ -3979,29 +3521,31 @@ var agentchatSetupAdapter = {
|
|
|
3979
3521
|
return null;
|
|
3980
3522
|
},
|
|
3981
3523
|
/**
|
|
3982
|
-
*
|
|
3983
|
-
*
|
|
3984
|
-
*
|
|
3985
|
-
*
|
|
3524
|
+
* Ask AgentChat about the key BEFORE the write. Accepted: the handle it
|
|
3525
|
+
* belongs to goes into the same config write OpenClaw already makes.
|
|
3526
|
+
* Turned away (401 / 403): the input carries the refusal, `validateInput`
|
|
3527
|
+
* returns it, and OpenClaw writes nothing. No verdict (unreachable, a
|
|
3528
|
+
* 5xx): the key is written for the runtime to try, as the wizard does.
|
|
3986
3529
|
*/
|
|
3987
|
-
async prepareAccountConfigInput({ accountId, input, runtime }) {
|
|
3530
|
+
async prepareAccountConfigInput({ cfg, accountId, input, runtime }) {
|
|
3988
3531
|
const apiKey = typeof input.token === "string" ? input.token : void 0;
|
|
3989
3532
|
if (!apiKey || apiKey.length < MIN_API_KEY_LENGTH) return input;
|
|
3990
|
-
const apiBase = typeof input.url === "string" && input.url.length > 0 ? input.url :
|
|
3533
|
+
const apiBase = typeof input.url === "string" && input.url.length > 0 ? input.url : readAgentchatConfigField(cfg, accountId, "apiBase");
|
|
3534
|
+
keyAskedBeforeWrite = keyDigest(apiKey);
|
|
3991
3535
|
try {
|
|
3992
|
-
const result = await
|
|
3536
|
+
const result = await checkApiKey(apiKey, { apiBase });
|
|
3993
3537
|
if (result.ok) {
|
|
3994
|
-
say(runtime, "log", `[agentchat:${accountId}] authenticated as @${result.
|
|
3538
|
+
say(runtime, "log", `[agentchat:${accountId}] authenticated as @${result.handle}`);
|
|
3995
3539
|
try {
|
|
3996
3540
|
placeApiKeyInDotEnv(apiKey);
|
|
3997
3541
|
} catch (err) {
|
|
3998
3542
|
say(runtime, "error", `[agentchat:${accountId}] could not write AGENTCHAT_API_KEY to ${dotEnvPath()}: ${err instanceof Error ? err.message : String(err)}`);
|
|
3999
3543
|
}
|
|
4000
|
-
return { ...input, agentHandle: result.
|
|
3544
|
+
return { ...input, agentHandle: result.handle };
|
|
4001
3545
|
}
|
|
4002
|
-
|
|
3546
|
+
return { ...input, keyRefusal: keyRefusal(result) };
|
|
4003
3547
|
} catch (err) {
|
|
4004
|
-
say(runtime, "error", `[agentchat:${accountId}]
|
|
3548
|
+
say(runtime, "error", `[agentchat:${accountId}] no verdict on the API key, saving it for the runtime to try: ${err instanceof Error ? err.message : String(err)}`);
|
|
4005
3549
|
}
|
|
4006
3550
|
return input;
|
|
4007
3551
|
},
|
|
@@ -4015,12 +3559,12 @@ var agentchatSetupAdapter = {
|
|
|
4015
3559
|
const patch = {};
|
|
4016
3560
|
if (typeof input.token === "string" && input.token.length > 0) patch.apiKey = input.token;
|
|
4017
3561
|
if (typeof input.url === "string" && input.url.length > 0) patch.apiBase = input.url;
|
|
4018
|
-
const handle =
|
|
3562
|
+
const handle = handleOf(input.agentHandle);
|
|
4019
3563
|
if (handle) patch.agentHandle = handle;
|
|
4020
3564
|
const withAccount = applyAgentchatAccountPatch(cfg, accountId, patch);
|
|
4021
3565
|
const apiKey = readAgentchatConfigField(withAccount, accountId, "apiKey");
|
|
4022
3566
|
if (!apiKey) return withAccount;
|
|
4023
|
-
const apiBase = readAgentchatConfigField(withAccount, accountId, "apiBase") ??
|
|
3567
|
+
const apiBase = readAgentchatConfigField(withAccount, accountId, "apiBase") ?? DEFAULT_API_BASE;
|
|
4024
3568
|
return applyIntegrationConfig(withAccount, { accountId, handle, apiKey, apiBase }).cfg;
|
|
4025
3569
|
},
|
|
4026
3570
|
/**
|
|
@@ -4031,16 +3575,16 @@ var agentchatSetupAdapter = {
|
|
|
4031
3575
|
async afterAccountConfigWritten({ cfg, accountId, input, runtime }) {
|
|
4032
3576
|
const apiKey = typeof input.token === "string" ? input.token : readAgentchatConfigField(cfg, accountId, "apiKey");
|
|
4033
3577
|
if (!apiKey) return;
|
|
4034
|
-
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ??
|
|
4035
|
-
let handle =
|
|
3578
|
+
const apiBase = readAgentchatConfigField(cfg, accountId, "apiBase") ?? DEFAULT_API_BASE;
|
|
3579
|
+
let handle = handleOf(input.agentHandle) ?? handleOf(readAgentchatConfigField(cfg, accountId, "agentHandle"));
|
|
4036
3580
|
if (!handle) {
|
|
4037
3581
|
try {
|
|
4038
|
-
const result = await
|
|
3582
|
+
const result = await checkApiKey(apiKey, { apiBase });
|
|
4039
3583
|
if (result.ok) {
|
|
4040
|
-
handle = result.
|
|
3584
|
+
handle = result.handle;
|
|
4041
3585
|
say(runtime, "log", `[agentchat:${accountId}] authenticated as @${handle}; run \`openclaw doctor --fix\` to persist channels.agentchat.agentHandle`);
|
|
4042
3586
|
} else {
|
|
4043
|
-
say(runtime, "error", `[agentchat:${accountId}] api key did not pass live check (${result.
|
|
3587
|
+
say(runtime, "error", `[agentchat:${accountId}] api key did not pass live check (${result.code}): ${result.message}`);
|
|
4044
3588
|
return;
|
|
4045
3589
|
}
|
|
4046
3590
|
} catch (err) {
|
|
@@ -4074,8 +3618,24 @@ function accountSectionPersistable(cfg, accountId) {
|
|
|
4074
3618
|
if (!raw) return true;
|
|
4075
3619
|
return agentchatChannelConfigSchema.safeParse(raw).success;
|
|
4076
3620
|
}
|
|
3621
|
+
async function refuseRejectedKey(prevCfg, nextCfg, accountId, runtime) {
|
|
3622
|
+
const apiKey = readAgentchatConfigField(nextCfg, accountId, "apiKey");
|
|
3623
|
+
if (!apiKey || apiKey === readAgentchatConfigField(prevCfg, accountId, "apiKey")) return;
|
|
3624
|
+
if (keyAskedBeforeWrite === keyDigest(apiKey)) return;
|
|
3625
|
+
let check;
|
|
3626
|
+
try {
|
|
3627
|
+
check = await checkApiKey(apiKey, { apiBase: readAgentchatConfigField(nextCfg, accountId, "apiBase") });
|
|
3628
|
+
} catch (err) {
|
|
3629
|
+
say(runtime, "error", `[agentchat:${accountId}] no verdict on the API key, saving it for the runtime to try: ${err instanceof Error ? err.message : String(err)}`);
|
|
3630
|
+
return;
|
|
3631
|
+
}
|
|
3632
|
+
if (check.ok) return;
|
|
3633
|
+
say(runtime, "error", keyRefusal(check));
|
|
3634
|
+
throw new WizardCancelledError("AgentChat did not accept the API key");
|
|
3635
|
+
}
|
|
4077
3636
|
var agentchatLifecycleAdapter = {
|
|
4078
3637
|
async onAccountConfigChanged({ prevCfg, nextCfg, accountId, runtime }) {
|
|
3638
|
+
await refuseRejectedKey(prevCfg, nextCfg, accountId, runtime);
|
|
4079
3639
|
const wasEnabled = isAccountEnabled(prevCfg, accountId);
|
|
4080
3640
|
const isEnabled = isAccountEnabled(nextCfg, accountId);
|
|
4081
3641
|
if (wasEnabled === isEnabled) return;
|
|
@@ -4091,13 +3651,13 @@ var agentchatLifecycleAdapter = {
|
|
|
4091
3651
|
for (const line of removeIntegrationFiles(nextCfg, accountId, { clearKey: false })) say(runtime, "log", `[agentchat:${accountId}] ${line}`);
|
|
4092
3652
|
return;
|
|
4093
3653
|
}
|
|
4094
|
-
const handle =
|
|
3654
|
+
const handle = handleOf(readAgentchatConfigField(nextCfg, accountId, "agentHandle"));
|
|
4095
3655
|
if (!handle) {
|
|
4096
3656
|
say(runtime, "log", `[agentchat:${accountId}] agentHandle not in config \u2014 run \`openclaw doctor --fix\` to learn it from the key and place the identity line`);
|
|
4097
3657
|
return;
|
|
4098
3658
|
}
|
|
4099
3659
|
const apiKey = readAgentchatConfigField(nextCfg, accountId, "apiKey");
|
|
4100
|
-
const apiBase = readAgentchatConfigField(nextCfg, accountId, "apiBase") ??
|
|
3660
|
+
const apiBase = readAgentchatConfigField(nextCfg, accountId, "apiBase") ?? DEFAULT_API_BASE;
|
|
4101
3661
|
const identityLine = apiKey ? await loadIdentityLine({ client: createSdkClient({ apiKey, apiBase }), accountId }) : null;
|
|
4102
3662
|
const restored = restoreIdentityLine(nextCfg, accountId, handle, identityLine);
|
|
4103
3663
|
if (restored.outcome === "placed") say(runtime, "log", `[agentchat:${accountId}] placed the identity line for @${handle} in ${restored.path}`);
|