@agentchatme/openclaw 0.9.1 → 0.10.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.
@@ -1,15 +1,16 @@
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 { integration, AgentChatClient, AgentChatError, RateLimitedError, RealtimeClient } from 'agentchatme';
4
5
  import { readApiKeyFromEnv } from './credentials/read-env.js';
5
6
  import * as fs from 'fs';
6
7
  import * as path2 from 'path';
7
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';
8
9
  import { resolveBoundAgentId, resolveOpenClawDefaultAgentId, resolveOpenClawStateDir } from './openclaw-layout.js';
9
10
  import { removeAgentsAnchor, agentsAnchorPath, hasAgentsAnchorFor, writeAgentsAnchor, readAgentsAnchor } from './binding/agents-anchor.js';
10
- import { AgentChatClient, RealtimeClient, gate } from 'agentchatme';
11
11
  import { z } from 'zod';
12
12
  import pino from 'pino';
13
+ import { AsyncLocalStorage } from 'async_hooks';
13
14
 
14
15
  // src/channel.setup.ts
15
16
 
@@ -24,9 +25,9 @@ function readChannelSection(cfg) {
24
25
  }
25
26
  function readAccountRaw(section, accountId) {
26
27
  if (!section) return void 0;
27
- const { accounts } = section;
28
- if (accounts && typeof accounts === "object" && !Array.isArray(accounts)) {
29
- const entry = accounts[accountId];
28
+ const { accounts: accounts2 } = section;
29
+ if (accounts2 && typeof accounts2 === "object" && !Array.isArray(accounts2)) {
30
+ const entry = accounts2[accountId];
30
31
  if (entry && typeof entry === "object") return entry;
31
32
  }
32
33
  if (accountId === AGENTCHAT_DEFAULT_ACCOUNT_ID) {
@@ -51,8 +52,8 @@ function agentchatEnabledConfigPath(cfg, accountId) {
51
52
  const section = readChannelSection(cfg);
52
53
  const sectionPath = `channels.${AGENTCHAT_CHANNEL_ID}.enabled`;
53
54
  if (!section || section.enabled === false) return sectionPath;
54
- const { accounts } = section;
55
- const entry = accounts && typeof accounts === "object" && !Array.isArray(accounts) ? accounts[accountId] : void 0;
55
+ const { accounts: accounts2 } = section;
56
+ const entry = accounts2 && typeof accounts2 === "object" && !Array.isArray(accounts2) ? accounts2[accountId] : void 0;
56
57
  if (entry && typeof entry === "object") return `channels.${AGENTCHAT_CHANNEL_ID}.accounts.${accountId}.enabled`;
57
58
  return sectionPath;
58
59
  }
@@ -62,9 +63,9 @@ function agentchatReenableHint(cfg, accountId) {
62
63
  function listConfiguredAccountIds(cfg) {
63
64
  const section = readChannelSection(cfg);
64
65
  if (!section) return [];
65
- const { accounts } = section;
66
- if (accounts && typeof accounts === "object" && !Array.isArray(accounts)) {
67
- const ids = Object.keys(accounts);
66
+ const { accounts: accounts2 } = section;
67
+ if (accounts2 && typeof accounts2 === "object" && !Array.isArray(accounts2)) {
68
+ const ids = Object.keys(accounts2);
68
69
  if (ids.length > 0) return ids;
69
70
  }
70
71
  return typeof section.apiKey === "string" ? [AGENTCHAT_DEFAULT_ACCOUNT_ID] : [];
@@ -93,19 +94,19 @@ function applyAgentchatAccountPatch(cfg, accountId, patch) {
93
94
  if (accountId === AGENTCHAT_DEFAULT_ACCOUNT_ID && !section.accounts) {
94
95
  Object.assign(section, patch);
95
96
  } else {
96
- const accounts = {
97
+ const accounts2 = {
97
98
  ...section.accounts ?? {}
98
99
  };
99
- const prevAccount = typeof accounts[accountId] === "object" && accounts[accountId] !== null ? accounts[accountId] : {};
100
- accounts[accountId] = { ...prevAccount, ...patch };
101
- section.accounts = accounts;
100
+ const prevAccount = typeof accounts2[accountId] === "object" && accounts2[accountId] !== null ? accounts2[accountId] : {};
101
+ accounts2[accountId] = { ...prevAccount, ...patch };
102
+ section.accounts = accounts2;
102
103
  }
103
104
  channels[AGENTCHAT_CHANNEL_ID] = section;
104
105
  return { ...cfg ?? {}, channels };
105
106
  }
106
107
 
107
108
  // src/version.ts
108
- var PACKAGE_VERSION = "0.9.1";
109
+ var PACKAGE_VERSION = "0.10.0";
109
110
 
110
111
  // src/client-identity.ts
111
112
  var AGENTCHAT_CLIENT_NAME = "openclaw";
@@ -113,10 +114,6 @@ var AGENTCHAT_CLIENT_IDENTITY = {
113
114
  name: AGENTCHAT_CLIENT_NAME,
114
115
  version: PACKAGE_VERSION
115
116
  };
116
- var AGENTCHAT_CLIENT_HEADERS = {
117
- "X-AgentChat-Client": AGENTCHAT_CLIENT_NAME,
118
- "X-AgentChat-Client-Version": PACKAGE_VERSION
119
- };
120
117
 
121
118
  // src/setup-client.ts
122
119
  var DEFAULT_API_BASE = "https://api.agentchat.me";
@@ -125,63 +122,37 @@ async function validateApiKey(apiKey, opts = {}) {
125
122
  if (!apiKey || typeof apiKey !== "string") {
126
123
  return { ok: false, reason: "unauthorized", message: "API key is empty" };
127
124
  }
128
- const base = (opts.apiBase ?? DEFAULT_API_BASE).replace(/\/+$/, "");
129
- const url = `${base}/v1/agents/me`;
130
- const controller = new AbortController();
131
- const fetchImpl = opts.fetch ?? fetch;
132
- const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
133
- const timer = setTimeout(() => controller.abort(), timeoutMs);
134
- let res;
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;
135
134
  try {
136
- res = await fetchImpl(url, {
137
- method: "GET",
138
- headers: {
139
- Authorization: `Bearer ${apiKey}`,
140
- Accept: "application/json",
141
- ...AGENTCHAT_CLIENT_HEADERS
142
- },
143
- signal: controller.signal
144
- });
135
+ me = await client.getMe();
145
136
  } catch (err) {
146
- const message = err instanceof Error ? err.message : String(err);
147
- const unreachable = err instanceof Error && (err.name === "AbortError" || /ECONN|ETIMEDOUT|ENOTFOUND|EAI_AGAIN/i.test(message));
148
- return {
149
- ok: false,
150
- reason: unreachable ? "unreachable" : "network-error",
151
- message: `agentchat: GET /v1/agents/me failed: ${message}`
152
- };
153
- } finally {
154
- clearTimeout(timer);
155
- }
156
- if (res.status === 401) {
157
- return { ok: false, reason: "unauthorized", status: 401, message: "API key is invalid or revoked" };
158
- }
159
- if (res.status === 403) {
160
- return { ok: false, reason: "forbidden", status: 403, message: "API key lacks permission to read /agents/me" };
161
- }
162
- if (res.status === 410) {
163
- return { ok: false, reason: "deleted", status: 410, message: "Agent account has been deleted" };
164
- }
165
- if (res.status >= 500) {
166
- return { ok: false, reason: "server-error", status: res.status, message: `AgentChat API returned ${res.status}` };
167
- }
168
- if (!res.ok) {
169
- return {
170
- ok: false,
171
- reason: "server-error",
172
- status: res.status,
173
- message: `AgentChat API returned ${res.status}`
174
- };
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}` };
175
151
  }
176
- const body = await res.json().catch(() => null);
152
+ const body = asRecord(me);
177
153
  const email = typeof body?.email === "string" ? body.email : typeof body?.email_masked === "string" ? body.email_masked : null;
178
154
  if (!body || typeof body.handle !== "string" || email === null || typeof body.created_at !== "string") {
179
- return {
180
- ok: false,
181
- reason: "unexpected-shape",
182
- status: res.status,
183
- message: "AgentChat /agents/me returned an unrecognized shape"
184
- };
155
+ return { ok: false, reason: "unexpected-shape", status: 200, message: "AgentChat /agents/me returned an unrecognized shape" };
185
156
  }
186
157
  return {
187
158
  ok: true,
@@ -194,139 +165,122 @@ async function validateApiKey(apiKey, opts = {}) {
194
165
  };
195
166
  }
196
167
  async function registerAgentStart(input, opts = {}) {
197
- const res = await post("/v1/register", input, opts);
198
- if (res.kind === "network") {
199
- return { ok: false, reason: "network-error", message: res.message };
200
- }
201
- if (res.kind === "timeout") {
202
- return { ok: false, reason: "network-error", message: "request timed out" };
203
- }
204
- const body = res.body ?? {};
205
- if (res.status === 200) {
206
- if (typeof body.pending_id !== "string") {
207
- return {
208
- ok: false,
209
- reason: "server-error",
210
- status: 200,
211
- message: "AgentChat /register returned no pending_id"
212
- };
213
- }
214
- return { ok: true, pendingId: body.pending_id };
215
- }
216
- const code = typeof body.code === "string" ? body.code : "";
217
- const message = typeof body.message === "string" ? body.message : `status ${res.status}`;
218
- if (res.status === 400 && code === "INVALID_HANDLE") return { ok: false, reason: "invalid-handle", message, status: 400 };
219
- if (res.status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status: 400 };
220
- if (res.status === 409 && code === "HANDLE_TAKEN") return { ok: false, reason: "handle-taken", message, status: 409 };
221
- const emailPolicy = res.status === 409 ? classifyEmailPolicyRejection(code) : void 0;
222
- if (emailPolicy) {
223
- return { ok: false, reason: emailPolicy, message, status: 409, limit: readPolicyLimit(body.details) };
224
- }
225
- if (res.status === 429) {
226
- return {
227
- ok: false,
228
- reason: "rate-limited",
229
- message,
230
- status: 429,
231
- retryAfterSeconds: res.retryAfterSeconds
232
- };
233
- }
234
- if (res.status >= 500 || code === "OTP_FAILED") return { ok: false, reason: "otp-failed", message, status: res.status };
235
- return { ok: false, reason: "server-error", status: res.status, message };
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 };
236
196
  }
237
197
  async function registerAgentVerify(input, opts = {}) {
238
- const res = await post("/v1/register/verify", { pending_id: input.pendingId, code: input.code }, opts);
239
- if (res.kind === "network") return { ok: false, reason: "network-error", message: res.message };
240
- if (res.kind === "timeout") return { ok: false, reason: "network-error", message: "request timed out" };
241
- const body = res.body ?? {};
242
- if (res.status === 201) {
243
- const agent = body.agent;
244
- if (typeof body.api_key !== "string" || !agent || typeof agent.handle !== "string" || typeof agent.email !== "string" || typeof agent.created_at !== "string") {
245
- return {
246
- ok: false,
247
- reason: "unexpected-shape",
248
- status: 201,
249
- message: "AgentChat /register/verify returned an unrecognized shape"
250
- };
251
- }
252
- return {
253
- ok: true,
254
- apiKey: body.api_key,
255
- agent: {
256
- handle: agent.handle,
257
- displayName: typeof agent.display_name === "string" ? agent.display_name : null,
258
- email: agent.email,
259
- createdAt: agent.created_at
260
- }
261
- };
262
- }
263
- const code = typeof body.code === "string" ? body.code : "";
264
- const message = typeof body.message === "string" ? body.message : `status ${res.status}`;
265
- if (res.status === 400 && code === "EXPIRED") return { ok: false, reason: "expired", message, status: 400 };
266
- if (res.status === 400 && code === "INVALID_CODE") return { ok: false, reason: "invalid-code", message, status: 400 };
267
- if (res.status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status: 400 };
268
- if (res.status === 409 && code === "HANDLE_TAKEN") return { ok: false, reason: "handle-taken", message, status: 409 };
269
- const emailPolicy = res.status === 409 ? classifyEmailPolicyRejection(code) : void 0;
270
- if (emailPolicy) {
271
- return { ok: false, reason: emailPolicy, message, status: 409, limit: readPolicyLimit(body.details) };
272
- }
273
- if (res.status === 429) {
274
- return { ok: false, reason: "rate-limited", message, status: 429, retryAfterSeconds: res.retryAfterSeconds };
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" };
275
220
  }
276
- return { ok: false, reason: "server-error", status: res.status, message };
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
+ };
277
231
  }
278
232
  async function recoverAgentStart(input, opts = {}) {
279
- const res = await post("/v1/agents/recover", { email: input.email, handle: input.handle }, opts);
280
- if (res.kind === "network") return { ok: false, reason: "network-error", message: res.message };
281
- if (res.kind === "timeout") return { ok: false, reason: "network-error", message: "request timed out" };
282
- const body = res.body ?? {};
283
- const message = typeof body.message === "string" ? body.message : `status ${res.status}`;
284
- if (res.status === 200) {
285
- if (typeof body.pending_id !== "string") {
286
- return {
287
- ok: false,
288
- reason: "unexpected-shape",
289
- status: 200,
290
- message: "AgentChat did not start a recovery (no pending_id in the response). Check that the email is the one this agent registered with."
291
- };
292
- }
293
- return { ok: true, pendingId: body.pending_id, message };
294
- }
295
- const code = typeof body.code === "string" ? body.code : "";
296
- if (res.status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status: 400 };
297
- if (res.status === 429) {
298
- return { ok: false, reason: "rate-limited", message, status: 429, retryAfterSeconds: res.retryAfterSeconds };
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
+ };
299
257
  }
300
- return { ok: false, reason: "server-error", status: res.status, message };
258
+ return { ok: true, pendingId: body.pending_id, message };
301
259
  }
302
260
  async function recoverAgentVerify(input, opts = {}) {
303
- const res = await post("/v1/agents/recover/verify", { pending_id: input.pendingId, code: input.code }, opts);
304
- if (res.kind === "network") return { ok: false, reason: "network-error", message: res.message };
305
- if (res.kind === "timeout") return { ok: false, reason: "network-error", message: "request timed out" };
306
- const body = res.body ?? {};
307
- if (res.status === 200) {
308
- if (typeof body.api_key !== "string" || typeof body.handle !== "string") {
309
- return {
310
- ok: false,
311
- reason: "unexpected-shape",
312
- status: 200,
313
- message: "AgentChat /agents/recover/verify returned an unrecognized shape"
314
- };
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) };
315
276
  }
316
- return { ok: true, apiKey: body.api_key, handle: body.handle };
277
+ if (status === 429) return { ok: false, reason: "rate-limited", message, status, retryAfterSeconds: failure.retryAfterSeconds };
278
+ return { ok: false, reason: "server-error", status, message };
317
279
  }
318
- const code = typeof body.code === "string" ? body.code : "";
319
- const message = typeof body.message === "string" ? body.message : `status ${res.status}`;
320
- if (res.status === 400 && code === "EXPIRED") return { ok: false, reason: "expired", message, status: 400 };
321
- if (res.status === 400 && code === "INVALID_CODE") return { ok: false, reason: "invalid-code", message, status: 400 };
322
- if (res.status === 400 && code === "VALIDATION_ERROR") return { ok: false, reason: "validation", message, status: 400 };
323
- if (res.status === 409 && code === "HANDLE_REQUIRED") {
324
- return { ok: false, reason: "handle-required", message, status: 409, handles: readHandleList(body.details) };
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" };
325
282
  }
326
- if (res.status === 429) {
327
- return { ok: false, reason: "rate-limited", message, status: 429, retryAfterSeconds: res.retryAfterSeconds };
328
- }
329
- return { ok: false, reason: "server-error", status: res.status, message };
283
+ return { ok: true, apiKey: recovered.apiKey, handle: recovered.handle };
330
284
  }
331
285
  function classifyEmailPolicyRejection(code) {
332
286
  if (code === "EMAIL_LIMIT_REACHED" || code === "EMAIL_TAKEN") return "email-limit-reached";
@@ -344,39 +298,34 @@ function readHandleList(details) {
344
298
  if (!Array.isArray(handles)) return [];
345
299
  return handles.filter((h) => typeof h === "string" && h.length > 0);
346
300
  }
347
- async function post(path5, body, opts) {
348
- const base = (opts.apiBase ?? DEFAULT_API_BASE).replace(/\/+$/, "");
349
- const url = `${base}${path5}`;
350
- const controller = new AbortController();
351
- const fetchImpl = opts.fetch ?? fetch;
352
- const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
353
- const timer = setTimeout(() => controller.abort(), timeoutMs);
354
- try {
355
- const res = await fetchImpl(url, {
356
- method: "POST",
357
- headers: {
358
- "content-type": "application/json",
359
- accept: "application/json",
360
- ...AGENTCHAT_CLIENT_HEADERS
361
- },
362
- body: JSON.stringify(body),
363
- signal: controller.signal
364
- });
365
- const parsed = await res.json().catch(() => null);
366
- const retryAfterHeader = res.headers.get("retry-after");
367
- const retryAfterSeconds = retryAfterHeader ? Number(retryAfterHeader) : void 0;
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;
368
311
  return {
369
312
  kind: "http",
370
- status: res.status,
371
- body: parsed,
372
- retryAfterSeconds: Number.isFinite(retryAfterSeconds) ? retryAfterSeconds : void 0
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) } : {}
373
318
  };
374
- } catch (err) {
375
- if (err instanceof Error && err.name === "AbortError") return { kind: "timeout" };
376
- return { kind: "network", message: err instanceof Error ? err.message : String(err) };
377
- } finally {
378
- clearTimeout(timer);
379
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;
380
329
  }
381
330
 
382
331
  // src/integration/turn.ts
@@ -386,34 +335,20 @@ var MCP_TOOL_PREFIX = `${MCP_SERVER_NAME}${MCP_TOOL_DELIMITER}`;
386
335
  function mcpToolName(tool) {
387
336
  return `${MCP_TOOL_PREFIX}${tool}`;
388
337
  }
389
- var DECIDE_TOOL = mcpToolName("agentchat_decide");
390
- var GET_CONVERSATION_TOOL = mcpToolName("agentchat_get_conversation");
391
338
  var OPENCLAW_TOOL_NAMER = (tool) => tool.startsWith(MCP_TOOL_PREFIX) ? tool : mcpToolName(tool);
392
339
  var HOSTED_TOOL_TOKEN = /\bagentchat_(?!_)[a-z0-9_]+\b/g;
393
340
  function spellToolsForOpenClaw(text) {
394
341
  return text.replace(HOSTED_TOOL_TOKEN, (token) => OPENCLAW_TOOL_NAMER(token));
395
342
  }
396
- function wakeTurnPrompt(texts, conversationId) {
397
- const instruction = texts.wake_instruction.trim();
398
- if (!conversationId) return instruction;
399
- return `${instruction}
400
-
401
- ${conversationLine(conversationId)}`;
402
- }
403
- function conversationLine(conversationId) {
404
- return `Conversation to answer in this turn: ${conversationId}. The gate decided reply; its card is still on the tray while you work, and the reply verdict for it is recorded for you when this turn completes (recording it yourself with ${DECIDE_TOOL} is idempotent) \u2014 open it with ${GET_CONVERSATION_TOOL} and reply.`;
405
- }
406
- function inviteTurnPrompt(texts) {
407
- return texts.invite_prompt.trim();
408
- }
409
343
  function wakeSessionKey(agentId, now = Date.now(), nonce = randomNonce()) {
410
344
  return `agent:${agentId}:subagent:agentchat-wake-${utcStamp(now)}-${nonce}`;
411
345
  }
412
- function inviteSessionKey(agentId, inviteId) {
413
- return `agent:${agentId}:subagent:agentchat-invite-${sanitizeKeyPart(inviteId)}`;
346
+ function inviteSessionKey(agentId, inviteId, now = Date.now(), nonce = randomNonce()) {
347
+ return `agent:${agentId}:subagent:agentchat-invite-${sanitizeKeyPart(inviteId)}-${utcStamp(now)}-${nonce}`;
414
348
  }
415
- function integrationLane(accountId) {
416
- return `agentchat-integration:${sanitizeKeyPart(accountId)}`;
349
+ function integrationLane(accountId, generation2 = 0) {
350
+ const base = `agentchat-integration:${sanitizeKeyPart(accountId)}`;
351
+ return generation2 > 0 ? `${base}:${generation2}` : base;
417
352
  }
418
353
  function utcStamp(ms) {
419
354
  return new Date(ms).toISOString().replace(/[-:]/g, "").replace(/\.\d{3}Z$/, "Z");
@@ -430,7 +365,6 @@ function sanitizeKeyPart(value) {
430
365
  var MCP_PATH = "/mcp";
431
366
  var AUTH_HEADER_TEMPLATE = `Bearer \${${API_KEY_ENV}}`;
432
367
  var DEFAULT_API_BASE2 = "https://api.agentchat.me";
433
- var MCP_PROTOCOL_VERSION = "2025-06-18";
434
368
  function hostedMcpUrl(apiBase = DEFAULT_API_BASE2) {
435
369
  return `${apiBase.replace(/\/+$/, "")}${MCP_PATH}`;
436
370
  }
@@ -484,67 +418,24 @@ function withoutHostedMcpEntry(cfg, apiBase = DEFAULT_API_BASE2, apiKey) {
484
418
  return { cfg: { ...cfg, mcp }, removed: true };
485
419
  }
486
420
  async function probeHostedMcp(params) {
487
- const fetchImpl = params.fetch ?? fetch;
488
- const controller = new AbortController();
489
- const timer = setTimeout(() => controller.abort(), params.timeoutMs ?? 1e4);
490
- try {
491
- const res = await fetchImpl(hostedMcpUrl(params.apiBase ?? DEFAULT_API_BASE2), {
492
- method: "POST",
493
- headers: {
494
- authorization: `Bearer ${params.apiKey}`,
495
- "content-type": "application/json",
496
- accept: "application/json, text/event-stream",
497
- ...AGENTCHAT_CLIENT_HEADERS
498
- },
499
- body: JSON.stringify({
500
- jsonrpc: "2.0",
501
- id: 1,
502
- method: "initialize",
503
- params: {
504
- protocolVersion: MCP_PROTOCOL_VERSION,
505
- capabilities: {},
506
- clientInfo: { name: "@agentchatme/openclaw doctor", version: "0" }
507
- }
508
- }),
509
- signal: controller.signal
510
- });
511
- if (!res.ok) {
512
- return { ok: false, status: res.status, error: res.status === 401 ? "the key does not authenticate against the hosted MCP server" : `HTTP ${res.status}` };
513
- }
514
- const text = await res.text();
515
- const result = readInitializeResult(text);
516
- if (!result) return { ok: false, status: res.status, error: "no initialize result in the answer" };
517
- const serverInfo = result.serverInfo ?? {};
518
- return {
519
- ok: true,
520
- status: res.status,
521
- serverName: typeof serverInfo.name === "string" ? serverInfo.name : void 0,
522
- serverVersion: typeof serverInfo.version === "string" ? serverInfo.version : void 0,
523
- protocolVersion: typeof result.protocolVersion === "string" ? result.protocolVersion : void 0,
524
- hasInstructions: typeof result.instructions === "string" && result.instructions.trim().length > 0
525
- };
526
- } catch (err) {
527
- return { ok: false, error: err instanceof Error ? err.message : String(err) };
528
- } finally {
529
- clearTimeout(timer);
530
- }
531
- }
532
- function readInitializeResult(text) {
533
- const candidates = [];
534
- const trimmed = text.trim();
535
- if (trimmed.startsWith("{")) candidates.push(trimmed);
536
- for (const line of text.split(/\r?\n/)) {
537
- if (line.startsWith("data:")) candidates.push(line.slice(5).trim());
538
- }
539
- for (const candidate of candidates) {
540
- try {
541
- const parsed = JSON.parse(candidate);
542
- const result = parsed.result;
543
- if (result && typeof result === "object" && !Array.isArray(result)) return result;
544
- } catch {
545
- }
546
- }
547
- return null;
421
+ const result = await integration.probeHostedTools({
422
+ apiBase: params.apiBase ?? DEFAULT_API_BASE2,
423
+ apiKey: params.apiKey,
424
+ clientIdentity: AGENTCHAT_CLIENT_IDENTITY,
425
+ ...params.timeoutMs !== void 0 ? { timeoutMs: params.timeoutMs } : {},
426
+ ...params.fetch ? { fetch: params.fetch } : {}
427
+ });
428
+ const missing = result.missing ?? [];
429
+ return {
430
+ ok: result.ok,
431
+ ...result.status !== void 0 ? { status: result.status } : {},
432
+ ...result.serverName ? { serverName: result.serverName } : {},
433
+ ...result.serverVersion ? { serverVersion: result.serverVersion } : {},
434
+ ...result.protocolVersion ? { protocolVersion: result.protocolVersion } : {},
435
+ ...result.tools ? { tools: result.tools } : {},
436
+ ...missing.length > 0 ? { missing } : {},
437
+ ...result.error ? { error: result.error } : !result.ok && missing.length > 0 ? { error: `the server lists no ${missing.join(", ")} for this key` } : {}
438
+ };
548
439
  }
549
440
 
550
441
  // src/integration/runtime-holder.ts
@@ -564,11 +455,137 @@ function setPluginRuntime(runtime) {
564
455
  function getPluginRuntime() {
565
456
  return current;
566
457
  }
458
+
459
+ // src/integration/run.ts
460
+ var DEFAULT_RUN_WAIT_CHUNK_MS = 5 * 6e4;
461
+ var RUN_WAIT_RETRY_MS = [1e3, 5e3, 15e3, 3e4, 6e4];
462
+ var MIN_WAIT_SPACING_MS = 1e3;
463
+ var TRANSCRIPT_READ_LIMIT = 200;
464
+ var METADATA_KEYS = ["endedAt", "stopReason", "livenessState", "pendingError", "timeoutPhase", "startedAt", "providerStarted"];
465
+ function runEndOf(wait, timing) {
466
+ const status = wait?.status;
467
+ const error = nonEmpty(wait?.error);
468
+ if (status === "ok") return { ended: true, status: "ok" };
469
+ if (status === "error") {
470
+ if (error && error.trim().toLowerCase() === "completed") return { ended: true, status: "ok" };
471
+ return { ended: true, status: "error", ...error ? { error } : {} };
472
+ }
473
+ if (status !== "timeout") {
474
+ return { ended: false };
475
+ }
476
+ const ended = { ended: true, status: "timeout", ...error ? { error } : {} };
477
+ const hasMetadata = wait !== null && wait !== void 0 && METADATA_KEYS.some((key) => key in wait);
478
+ if (!hasMetadata) {
479
+ return timing.elapsedMs < timing.requestedMs / 2 ? ended : { ended: false };
480
+ }
481
+ if (wait.pendingError === true) return { ended: false };
482
+ if (typeof wait.endedAt === "number" || nonEmpty(wait.stopReason) || nonEmpty(wait.livenessState)) return ended;
483
+ if (isHardTimeout(wait)) return ended;
484
+ return error ? ended : { ended: false };
485
+ }
486
+ var HARD_TIMEOUT_PHASES = /* @__PURE__ */ new Set(["preflight", "provider", "tool_execution", "post_turn"]);
487
+ function isHardTimeout(wait) {
488
+ const phase = typeof wait.timeoutPhase === "string" ? wait.timeoutPhase.trim() : "";
489
+ if (HARD_TIMEOUT_PHASES.has(phase)) return true;
490
+ return wait.status === "timeout" && wait.providerStarted === true && nonEmpty(wait.stopReason) !== "restart";
491
+ }
492
+ async function watchRunUntilEnded(options) {
493
+ const chunkMs = options.chunkMs ?? DEFAULT_RUN_WAIT_CHUNK_MS;
494
+ const retryMs = options.retryMs ?? RUN_WAIT_RETRY_MS;
495
+ const now = options.now ?? Date.now;
496
+ const sleep = options.sleep ?? defaultSleep;
497
+ const started = now();
498
+ let failures = 0;
499
+ for (; ; ) {
500
+ const runtime = options.runtime();
501
+ if (!runtime) return { ended: false, reason: "gone", elapsedMs: now() - started };
502
+ const waitStarted = now();
503
+ let observation;
504
+ try {
505
+ observation = await runtime.subagent.waitForRun({ runId: options.runId, timeoutMs: chunkMs });
506
+ } catch (err) {
507
+ const message = describe(err);
508
+ const delay = retryMs[Math.min(failures, retryMs.length - 1)] ?? 6e4;
509
+ failures += 1;
510
+ options.onWaitError?.(message, failures, delay);
511
+ await sleep(delay);
512
+ continue;
513
+ }
514
+ failures = 0;
515
+ const elapsedMs = now() - waitStarted;
516
+ const end = runEndOf(observation, { requestedMs: chunkMs, elapsedMs });
517
+ if (end.ended) return { ...end, elapsedMs: now() - started };
518
+ options.onStillRunning?.(now() - started);
519
+ if (elapsedMs < MIN_WAIT_SPACING_MS) await sleep(MIN_WAIT_SPACING_MS - elapsedMs);
520
+ }
521
+ }
522
+ function countAgentChatCalls(messages, runId) {
523
+ if (messages.length === 0) return null;
524
+ let recognised = false;
525
+ let made = 0;
526
+ let failed = 0;
527
+ for (const row of messages) {
528
+ const message = unwrapMessage(row);
529
+ if (!message) continue;
530
+ const role = message.role;
531
+ if (role === "assistant" || role === "user") recognised = true;
532
+ if (role !== "toolResult" && role !== "tool") continue;
533
+ recognised = true;
534
+ const meta = asRecord2(message.__openclaw);
535
+ if (runId && typeof meta?.runId === "string" && meta.runId !== runId) continue;
536
+ if (!isAgentChatToolResult(message)) continue;
537
+ made += 1;
538
+ if (message.isError === true) failed += 1;
539
+ }
540
+ return recognised ? { made, failed } : null;
541
+ }
542
+ async function readAgentChatCalls(runtime, sessionKey, runId) {
543
+ if (!runtime || typeof runtime.subagent?.getSessionMessages !== "function") return null;
544
+ try {
545
+ const result = await runtime.subagent.getSessionMessages({ sessionKey, limit: TRANSCRIPT_READ_LIMIT });
546
+ const messages = result?.messages;
547
+ return Array.isArray(messages) ? countAgentChatCalls(messages, runId) : null;
548
+ } catch {
549
+ return null;
550
+ }
551
+ }
552
+ var TOOL_PREFIX = `${MCP_SERVER_NAME}${MCP_TOOL_DELIMITER}`;
553
+ function isAgentChatToolResult(message) {
554
+ const toolName = typeof message.toolName === "string" ? message.toolName : typeof message.name === "string" ? message.name : "";
555
+ if (toolName.startsWith(TOOL_PREFIX)) return true;
556
+ const tool = asRecord2(asRecord2(message.details)?.tool);
557
+ if (!tool) return false;
558
+ if (typeof tool.name === "string" && tool.name.startsWith(TOOL_PREFIX)) return true;
559
+ if (tool.sourceName === MCP_SERVER_NAME) return true;
560
+ const mcp = asRecord2(tool.mcp);
561
+ return mcp?.serverName === MCP_SERVER_NAME;
562
+ }
563
+ function unwrapMessage(row) {
564
+ const record = asRecord2(row);
565
+ if (!record) return null;
566
+ if (typeof record.role === "string") return record;
567
+ const inner = asRecord2(record.message);
568
+ return inner && typeof inner.role === "string" ? inner : null;
569
+ }
570
+ function asRecord2(value) {
571
+ return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
572
+ }
573
+ function nonEmpty(value) {
574
+ return typeof value === "string" && value.trim().length > 0 ? value : void 0;
575
+ }
576
+ function describe(err) {
577
+ return err instanceof Error ? err.message : String(err);
578
+ }
579
+ function defaultSleep(ms) {
580
+ return new Promise((resolve) => {
581
+ const timer = setTimeout(resolve, ms);
582
+ timer.unref?.();
583
+ });
584
+ }
567
585
  var DEFAULT_AGENT_ID = "main";
568
586
  var GATE_TEMPERATURE = 0;
569
587
  var DEFAULT_GATE_MAX_TOKENS = 2048;
570
588
  var DEFAULT_GATE_TIMEOUT_MS = 2e4;
571
- var DEFAULT_TURN_WAIT_MS = 15 * 6e4;
572
589
  var RuntimeUnavailableError = class extends Error {
573
590
  constructor(what) {
574
591
  super(`OpenClaw plugin runtime unavailable \u2014 ${what} needs setRuntime(...) from defineChannelPluginEntry`);
@@ -593,16 +610,21 @@ function createRuntimeModelCall(params) {
593
610
  const getRuntime = params.runtime ?? getPluginRuntime;
594
611
  const maxTokens = params.maxTokens ?? DEFAULT_GATE_MAX_TOKENS;
595
612
  const timeoutMs = params.timeoutMs ?? DEFAULT_GATE_TIMEOUT_MS;
596
- return async (prompt) => {
613
+ return async (prompt, options) => {
597
614
  const runtime = getRuntime();
598
615
  if (!runtime) throw new RuntimeUnavailableError("the gate call");
616
+ const signals = [
617
+ ...Number.isFinite(timeoutMs) && timeoutMs > 0 ? [AbortSignal.timeout(timeoutMs)] : [],
618
+ ...options?.signal ? [options.signal] : []
619
+ ];
620
+ const signal = signals.length === 0 ? void 0 : signals.length === 1 ? signals[0] : AbortSignal.any(signals);
599
621
  const result = await runtime.llm.complete({
600
622
  messages: [{ role: "user", content: prompt }],
601
623
  maxTokens,
602
624
  temperature: GATE_TEMPERATURE,
603
625
  ...params.agentIdOverride ? { agentId: params.agentIdOverride } : {},
604
626
  purpose: "AgentChat reply gate: the server's question about the server's cards (tool-free triage)",
605
- ...Number.isFinite(timeoutMs) && timeoutMs > 0 ? { signal: AbortSignal.timeout(timeoutMs) } : {}
627
+ ...signal ? { signal } : {}
606
628
  });
607
629
  return typeof result?.text === "string" ? result.text : "";
608
630
  };
@@ -632,10 +654,14 @@ function resolveGateAgent(params) {
632
654
  return { agentId: routed, agentIdOverride: routed === (ambient ?? routed) ? void 0 : routed };
633
655
  }
634
656
  function readLiveConfig(fallback, runtime = getPluginRuntime) {
635
- try {
636
- const loaded = runtime()?.config?.loadConfig?.();
637
- if (loaded && typeof loaded === "object") return { cfg: loaded, live: true };
638
- } catch {
657
+ const config = runtime()?.config;
658
+ for (const read of [config?.current, config?.loadConfig]) {
659
+ if (typeof read !== "function") continue;
660
+ try {
661
+ const loaded = read.call(config);
662
+ if (loaded && typeof loaded === "object") return { cfg: loaded, live: true };
663
+ } catch {
664
+ }
639
665
  }
640
666
  return { cfg: fallback, live: false };
641
667
  }
@@ -663,11 +689,11 @@ function turnReadiness(params) {
663
689
  }
664
690
  function createRuntimeTurnStarter(params = {}) {
665
691
  const getRuntime = params.runtime ?? getPluginRuntime;
666
- const waitMs = params.waitMs ?? DEFAULT_TURN_WAIT_MS;
667
692
  return async (request) => {
668
693
  const runtime = getRuntime();
669
694
  if (!runtime) return { ran: false, error: new RuntimeUnavailableError("the agent turn").message };
670
695
  let runId;
696
+ let sessionKey = request.sessionKey;
671
697
  try {
672
698
  const admitted = await runtime.subagent.run({
673
699
  sessionKey: request.sessionKey,
@@ -676,15 +702,46 @@ function createRuntimeTurnStarter(params = {}) {
676
702
  deliver: false
677
703
  });
678
704
  runId = admitted.runId;
705
+ const accepted = admitted.sessionKey;
706
+ if (typeof accepted === "string" && accepted.trim().length > 0) sessionKey = accepted.trim();
679
707
  } catch (err) {
680
708
  return { ran: false, error: err instanceof Error ? err.message : String(err) };
681
709
  }
682
- const done = runtime.subagent.waitForRun({ runId, timeoutMs: waitMs }).then(
683
- (waited) => ({ ran: true, runId, status: waited.status, ...waited.error ? { error: waited.error } : {} }),
684
- // The run was admitted; only the wait failed. The turn is OpenClaw's now.
685
- (err) => ({ ran: true, runId, status: "unknown", error: err instanceof Error ? err.message : String(err) })
686
- );
687
- return { ran: true, runId, done };
710
+ const done = watchRunUntilEnded({
711
+ runtime: getRuntime,
712
+ runId,
713
+ ...params.waitChunkMs !== void 0 ? { chunkMs: params.waitChunkMs } : {},
714
+ ...params.onStillRunning ? { onStillRunning: (elapsedMs) => params.onStillRunning?.({ runId, sessionKey, elapsedMs }) } : {},
715
+ ...params.onWaitError ? { onWaitError: (error, attempt, retryInMs) => params.onWaitError?.({ runId, sessionKey, error, attempt, retryInMs }) } : {},
716
+ ...params.now ? { now: params.now } : {},
717
+ ...params.sleep ? { sleep: params.sleep } : {}
718
+ }).then(async (watched) => {
719
+ if (!watched.ended) return { ran: true, runId, status: "gone", elapsedMs: watched.elapsedMs };
720
+ const agentchatCalls = await readAgentChatCalls(getRuntime(), sessionKey, runId);
721
+ return {
722
+ ran: true,
723
+ runId,
724
+ status: watched.status,
725
+ ...watched.error ? { error: watched.error } : {},
726
+ agentchatCalls,
727
+ elapsedMs: watched.elapsedMs
728
+ };
729
+ });
730
+ return { ran: true, runId, sessionKey, done };
731
+ };
732
+ }
733
+ function createRunWatcher(params = {}) {
734
+ const getRuntime = params.runtime ?? getPluginRuntime;
735
+ return async ({ runId, sessionKey }) => {
736
+ const watched = await watchRunUntilEnded({
737
+ runtime: getRuntime,
738
+ runId,
739
+ ...params.waitChunkMs !== void 0 ? { chunkMs: params.waitChunkMs } : {},
740
+ ...params.onStillRunning ? { onStillRunning: (elapsedMs) => params.onStillRunning?.({ runId, sessionKey, elapsedMs }) } : {}
741
+ });
742
+ if (!watched.ended) return null;
743
+ const agentchatCalls = await readAgentChatCalls(getRuntime(), sessionKey, runId);
744
+ return { ran: true, runId, status: watched.status, ...watched.error ? { error: watched.error } : {}, agentchatCalls, elapsedMs: watched.elapsedMs };
688
745
  };
689
746
  }
690
747
  var HANDLE_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
@@ -823,7 +880,7 @@ function restoreIdentityLine(cfg, accountId, handle, identityLine) {
823
880
  const written = writeAgentsAnchor({ cfg, handle: bare, text: identityLine, agentId });
824
881
  return { outcome: "placed", path: written.path };
825
882
  } catch (err) {
826
- return { outcome: "failed", path: filePath, detail: describe(err) };
883
+ return { outcome: "failed", path: filePath, detail: describe2(err) };
827
884
  }
828
885
  }
829
886
  function refreshIdentityLine(cfg, accountId, handle, identityLine) {
@@ -835,7 +892,7 @@ function refreshIdentityLine(cfg, accountId, handle, identityLine) {
835
892
  const written = writeAgentsAnchor({ cfg, handle, text: identityLine, agentId });
836
893
  return { outcome: "updated", path: written.path };
837
894
  } catch (err) {
838
- return { outcome: "failed", path: agentsAnchorPath(cfg, agentId), detail: describe(err) };
895
+ return { outcome: "failed", path: agentsAnchorPath(cfg, agentId), detail: describe2(err) };
839
896
  }
840
897
  }
841
898
  function placeIntegration(params) {
@@ -849,7 +906,7 @@ function placeIntegration(params) {
849
906
  detail: `${API_KEY_ENV} in ${key.path}${key.changed ? ` (a gateway started before this needs a restart, or a config reload, to resolve \${${API_KEY_ENV}})` : ""}`
850
907
  });
851
908
  } catch (err) {
852
- steps.push({ name: "key", status: "failed", detail: `could not write ${API_KEY_ENV} to the OpenClaw .env: ${describe(err)}` });
909
+ steps.push({ name: "key", status: "failed", detail: `could not write ${API_KEY_ENV} to the OpenClaw .env: ${describe2(err)}` });
853
910
  }
854
911
  }
855
912
  const agentId = params.agentId ?? resolvePlacementAgentId(params.cfg, params.accountId ?? AGENTCHAT_DEFAULT_ACCOUNT_ID);
@@ -865,7 +922,7 @@ function placeIntegration(params) {
865
922
  const anchor = writeAgentsAnchor({ cfg: params.cfg, handle: params.handle, text: params.identityLine, agentId });
866
923
  steps.push({ name: "anchor", status: "ok", detail: `identity line for @${handle} in ${anchor.path}` });
867
924
  } catch (err) {
868
- steps.push({ name: "anchor", status: "failed", detail: `could not write the identity line to AGENTS.md: ${describe(err)}` });
925
+ steps.push({ name: "anchor", status: "failed", detail: `could not write the identity line to AGENTS.md: ${describe2(err)}` });
869
926
  }
870
927
  }
871
928
  steps.push({ name: "manual", status: "ok", detail: "manual bundled as the agentchat skill (skills/agentchat/SKILL.md, https://agentchat.me/skill.md verbatim)" });
@@ -926,7 +983,7 @@ function checkPlacement(params) {
926
983
  });
927
984
  return checks;
928
985
  }
929
- function describe(err) {
986
+ function describe2(err) {
930
987
  return err instanceof Error ? err.message : String(err);
931
988
  }
932
989
  var PLUGIN_STATE_DIRNAME = "agentchat";
@@ -937,236 +994,115 @@ function safeSegment(value) {
937
994
  function integrationStateDir(accountId, stateDir = resolveOpenClawStateDir()) {
938
995
  return path2.join(stateDir, PLUGIN_STATE_DIRNAME, safeSegment(accountId));
939
996
  }
940
- function readJsonFile(file) {
941
- let raw;
942
- try {
943
- raw = fs.readFileSync(file, "utf8");
944
- } catch {
945
- return null;
946
- }
997
+ var OLD_INVITE_LEDGER_FILE = "invites.json";
998
+ function removeOldStateFiles(accountId, stateDir) {
999
+ const dir = integrationStateDir(accountId, stateDir);
1000
+ const removed = [];
1001
+ const ledger = path2.join(dir, OLD_INVITE_LEDGER_FILE);
1002
+ if (tryUnlink(ledger)) removed.push(ledger);
1003
+ const texts = path2.join(dir, "texts.json");
947
1004
  try {
948
- return JSON.parse(raw);
949
- } catch {
950
- return null;
1005
+ const entry = JSON.parse(fs.readFileSync(texts, "utf8"));
1006
+ if (entry.version !== 1 || typeof entry.runtime !== "string") {
1007
+ if (tryUnlink(texts)) removed.push(texts);
1008
+ }
1009
+ } catch (err) {
1010
+ if (err?.code !== "ENOENT" && tryUnlink(texts)) removed.push(texts);
951
1011
  }
1012
+ return removed;
952
1013
  }
953
- function writeJsonFile(file, value) {
954
- fs.mkdirSync(path2.dirname(file), { recursive: true });
955
- const tmp = `${file}.${process.pid}.${Date.now().toString(36)}.tmp`;
956
- fs.writeFileSync(tmp, `${JSON.stringify(value, null, 2)}
957
- `, "utf8");
1014
+ function tryUnlink(file) {
958
1015
  try {
959
- fs.renameSync(tmp, file);
960
- } catch (err) {
961
- try {
962
- fs.unlinkSync(tmp);
963
- } catch {
964
- }
965
- throw err;
1016
+ fs.unlinkSync(file);
1017
+ return true;
1018
+ } catch {
1019
+ return false;
966
1020
  }
967
1021
  }
968
1022
 
969
1023
  // src/integration/texts.ts
970
1024
  var TEXTS_RUNTIME = "openclaw";
971
- var TEXTS_REFRESH_MS = 24 * 60 * 6e4;
972
- var TEXTS_RETRY_MS = 5 * 6e4;
973
- var TEXTS_FETCH_TIMEOUT_MS = 1e4;
974
1025
  var TEXTS_CACHE_FILE = "texts.json";
975
- function isTexts(value) {
976
- if (!value || typeof value !== "object" || Array.isArray(value)) return false;
977
- const t = value;
978
- const manual = t.manual;
979
- return typeof t.version === "string" && typeof t.wake_instruction === "string" && t.wake_instruction.length > 0 && typeof t.invite_prompt === "string" && t.invite_prompt.length > 0 && typeof t.identity_line === "string" && t.identity_line.length > 0 && typeof t.identity_line_template === "string" && !!manual && typeof manual === "object" && typeof manual.url === "string" && typeof manual.version === "string";
980
- }
981
1026
  function textsCachePath(accountId, stateDir) {
982
1027
  return path2.join(integrationStateDir(accountId, stateDir), TEXTS_CACHE_FILE);
983
1028
  }
984
- function readCachedTexts(accountId, stateDir) {
985
- const raw = readJsonFile(textsCachePath(accountId, stateDir));
986
- if (!raw || typeof raw !== "object") return null;
987
- const entry = raw;
988
- if (typeof entry.fetched_at !== "string" || !isTexts(entry.texts)) return null;
989
- return { fetched_at: entry.fetched_at, texts: entry.texts };
990
- }
991
- function writeCachedTexts(accountId, texts, fetchedAt, stateDir) {
992
- const file = textsCachePath(accountId, stateDir);
993
- const entry = { fetched_at: fetchedAt.toISOString(), texts };
994
- writeJsonFile(file, entry);
995
- return file;
996
- }
997
1029
  function spellTextsForOpenClaw(texts) {
998
1030
  return {
999
1031
  ...texts,
1000
1032
  wake_instruction: spellToolsForOpenClaw(texts.wake_instruction),
1001
1033
  invite_prompt: spellToolsForOpenClaw(texts.invite_prompt),
1002
1034
  identity_line: spellToolsForOpenClaw(texts.identity_line),
1003
- identity_line_template: spellToolsForOpenClaw(texts.identity_line_template)
1035
+ identity_line_template: spellToolsForOpenClaw(texts.identity_line_template),
1036
+ ...typeof texts.gate_call_preamble === "string" ? { gate_call_preamble: spellToolsForOpenClaw(texts.gate_call_preamble) } : {},
1037
+ ...typeof texts.gate_call_answer_format === "string" ? { gate_call_answer_format: spellToolsForOpenClaw(texts.gate_call_answer_format) } : {}
1004
1038
  };
1005
1039
  }
1006
- async function fetchTexts(client, timeoutMs = TEXTS_FETCH_TIMEOUT_MS) {
1007
- const texts = await client.texts.get({ runtime: TEXTS_RUNTIME, timeoutMs });
1008
- if (!isTexts(texts)) throw new Error("GET /v1/texts: answer is not a texts set (missing wake_instruction, invite_prompt or identity_line)");
1009
- return texts;
1040
+ function createTextsCache(params) {
1041
+ return new integration.TextsCache({
1042
+ client: params.client,
1043
+ runtime: TEXTS_RUNTIME,
1044
+ filePath: textsCachePath(params.accountId, params.stateDir),
1045
+ ...params.logger ? { onError: (err) => params.logger?.warn({ err: err.message }, "served texts") } : {}
1046
+ });
1010
1047
  }
1011
- async function loadTexts(params) {
1012
- const now = params.now ?? Date.now;
1013
- try {
1014
- const texts = await fetchTexts(params.client, params.timeoutMs);
1015
- const fetchedAt = new Date(now());
1016
- try {
1017
- writeCachedTexts(params.accountId, texts, fetchedAt, params.stateDir);
1018
- } catch (err) {
1019
- params.log?.warn({ err: describe2(err) }, "texts fetched but the cache could not be written");
1020
- }
1021
- return { texts: spellTextsForOpenClaw(texts), source: "server", fetchedAt: fetchedAt.toISOString() };
1022
- } catch (err) {
1023
- const cached = readCachedTexts(params.accountId, params.stateDir);
1024
- if (cached) {
1025
- params.log?.info({ err: describe2(err), fetchedAt: cached.fetched_at }, "texts: server unreachable \u2014 using the cached copy");
1026
- return { texts: spellTextsForOpenClaw(cached.texts), source: "cache", fetchedAt: cached.fetched_at };
1027
- }
1028
- params.log?.debug({ err: describe2(err) }, "texts: server unreachable and nothing cached");
1029
- return null;
1048
+ var OpenClawTexts = class {
1049
+ constructor(cache2) {
1050
+ this.cache = cache2;
1030
1051
  }
1031
- }
1032
- async function loadIdentityLine(params) {
1033
- const loaded = await loadTexts(params);
1034
- return loaded ? loaded.texts.identity_line : null;
1035
- }
1036
- function cachedIdentityLine(accountId, stateDir) {
1037
- const cached = readCachedTexts(accountId, stateDir);
1038
- return cached ? spellToolsForOpenClaw(cached.texts.identity_line) : null;
1039
- }
1040
- var TextsStore = class {
1041
- client;
1042
- accountId;
1043
- logger;
1044
- stateDir;
1045
- refreshMs;
1046
- retryMs;
1047
- fetchTimeoutMs;
1048
- now;
1049
- _setTimeout;
1050
- _clearTimeout;
1051
- loaded = null;
1052
- timer = null;
1053
- inFlight = null;
1054
- stopped = false;
1055
- missingAnnounced = false;
1056
- listeners = /* @__PURE__ */ new Set();
1057
- loads = 0;
1058
- serverLoads = 0;
1059
- constructor(options) {
1060
- this.client = options.client;
1061
- this.accountId = options.accountId;
1062
- this.logger = options.logger.child({ component: "texts", accountId: options.accountId });
1063
- this.stateDir = options.stateDir;
1064
- this.refreshMs = options.refreshMs ?? TEXTS_REFRESH_MS;
1065
- this.retryMs = options.retryMs ?? TEXTS_RETRY_MS;
1066
- this.fetchTimeoutMs = options.fetchTimeoutMs ?? TEXTS_FETCH_TIMEOUT_MS;
1067
- this.now = options.now ?? Date.now;
1068
- this._setTimeout = options.setTimeout ?? ((fn, ms) => setTimeout(fn, ms));
1069
- this._clearTimeout = options.clearTimeout ?? ((h) => clearTimeout(h));
1052
+ cache;
1053
+ spelledFrom = null;
1054
+ spelled = null;
1055
+ current() {
1056
+ return this.spell(this.cache.current());
1070
1057
  }
1071
- /** The texts spelled for OpenClaw, or null until the first copy exists. */
1072
- get current() {
1073
- return this.loaded?.texts ?? null;
1058
+ async ensure() {
1059
+ return this.spell(await this.cache.ensure());
1074
1060
  }
1075
1061
  get source() {
1076
- return this.loaded?.source ?? null;
1062
+ return this.cache.source;
1077
1063
  }
1078
1064
  get fetchedAt() {
1079
- return this.loaded?.fetchedAt ?? null;
1065
+ return this.cache.fetchedAt;
1080
1066
  }
1081
- /** Called with every copy that arrives from the server (not with a cached one). */
1082
- onServerCopy(listener) {
1083
- this.listeners.add(listener);
1084
- return () => {
1085
- this.listeners.delete(listener);
1086
- };
1067
+ get lastError() {
1068
+ return this.cache.lastError;
1087
1069
  }
1088
- /** First load (server, then cache), then the timer. Resolves after the first attempt; never throws. */
1089
- async start() {
1090
- const texts = await this.load();
1091
- if (!texts && !this.missingAnnounced) {
1092
- this.missingAnnounced = true;
1093
- this.logger.warn(
1094
- { retryMs: this.retryMs },
1095
- "AgentChat texts unreachable and nothing cached \u2014 starting anyway; turns wait for the first copy, retried until it arrives"
1096
- );
1070
+ spell(texts) {
1071
+ if (texts === null) return null;
1072
+ if (texts !== this.spelledFrom) {
1073
+ this.spelledFrom = texts;
1074
+ this.spelled = spellTextsForOpenClaw(texts);
1097
1075
  }
1098
- this.arm();
1099
- return texts;
1100
- }
1101
- /** The current copy, or one load attempt now (coalesced with any in flight). */
1102
- async ensure() {
1103
- if (this.loaded) return this.loaded.texts;
1104
- if (this.stopped) return null;
1105
- return this.load();
1076
+ return this.spelled;
1106
1077
  }
1107
- /** Force a refresh (tests, operators). Coalesced. */
1108
- refresh() {
1109
- return this.load();
1078
+ };
1079
+ async function loadIdentityLine(params) {
1080
+ const cache2 = createTextsCache(params);
1081
+ try {
1082
+ const texts = await cache2.start();
1083
+ return texts ? spellToolsForOpenClaw(texts.identity_line) : null;
1084
+ } catch {
1085
+ return null;
1086
+ } finally {
1087
+ cache2.stop();
1110
1088
  }
1111
- stop() {
1112
- this.stopped = true;
1113
- if (this.timer !== null) {
1114
- this._clearTimeout(this.timer);
1115
- this.timer = null;
1116
- }
1117
- this.listeners.clear();
1089
+ }
1090
+ function cachedIdentityLine(accountId, stateDir) {
1091
+ let raw;
1092
+ try {
1093
+ raw = fs.readFileSync(textsCachePath(accountId, stateDir), "utf8");
1094
+ } catch {
1095
+ return null;
1118
1096
  }
1119
- load() {
1120
- if (this.inFlight) return this.inFlight;
1121
- this.inFlight = (async () => {
1122
- this.loads += 1;
1123
- const next = await loadTexts({
1124
- client: this.client,
1125
- accountId: this.accountId,
1126
- stateDir: this.stateDir,
1127
- now: this.now,
1128
- log: this.logger,
1129
- timeoutMs: this.fetchTimeoutMs
1130
- });
1131
- if (this.stopped) return next?.texts ?? null;
1132
- if (next) {
1133
- const changed = this.loaded?.texts.version !== next.texts.version || this.loaded?.texts.wake_instruction !== next.texts.wake_instruction;
1134
- this.loaded = next;
1135
- if (next.source === "server") {
1136
- this.serverLoads += 1;
1137
- this.logger[changed ? "info" : "debug"](
1138
- { version: next.texts.version, manual: next.texts.manual.version },
1139
- "texts fetched from the server"
1140
- );
1141
- for (const listener of this.listeners) {
1142
- try {
1143
- listener(next.texts, next.source);
1144
- } catch (err) {
1145
- this.logger.warn({ err: describe2(err) }, "texts listener threw \u2014 continuing");
1146
- }
1147
- }
1148
- }
1149
- }
1150
- return next?.texts ?? null;
1151
- })().finally(() => {
1152
- this.inFlight = null;
1153
- });
1154
- return this.inFlight;
1155
- }
1156
- arm() {
1157
- if (this.stopped) return;
1158
- if (this.timer !== null) this._clearTimeout(this.timer);
1159
- const delay = this.loaded === null ? this.retryMs : this.loaded.source === "cache" ? this.retryMs : this.refreshMs;
1160
- this.timer = this._setTimeout(() => {
1161
- this.timer = null;
1162
- void this.load().finally(() => this.arm());
1163
- }, delay);
1164
- this.timer.unref?.();
1097
+ try {
1098
+ const entry = JSON.parse(raw);
1099
+ if (entry.runtime !== TEXTS_RUNTIME || !isTexts(entry.texts)) return null;
1100
+ return spellToolsForOpenClaw(entry.texts.identity_line);
1101
+ } catch {
1102
+ return null;
1165
1103
  }
1166
- };
1167
- function describe2(err) {
1168
- return err instanceof Error ? err.message : String(err);
1169
1104
  }
1105
+ var isTexts = integration.isTexts;
1170
1106
  var cache = /* @__PURE__ */ new Map();
1171
1107
  function createSdkClient(params) {
1172
1108
  return new AgentChatClient({
@@ -1972,40 +1908,674 @@ var agentchatChannelConfigSchema = z.object({
1972
1908
  function parseChannelConfig(input) {
1973
1909
  return agentchatChannelConfigSchema.parse(input);
1974
1910
  }
1975
- function createLogger(options) {
1976
- if (options.delegate) {
1977
- return options.delegate;
1978
- }
1979
- const pinoLogger = pino({
1980
- level: options.level,
1981
- base: { plugin: "@agentchatme/openclaw" },
1982
- redact: {
1983
- paths: buildRedactPaths(options.redactKeys),
1984
- censor: "[REDACTED]"
1985
- },
1986
- timestamp: pino.stdTimeFunctions.isoTime
1987
- });
1988
- return wrapPino(pinoLogger);
1989
- }
1990
- function buildRedactPaths(keys) {
1991
- const prefixes = ["", "config.", "headers.", "context.", "req.headers.", "res.headers."];
1992
- const paths = [];
1993
- for (const key of keys) {
1994
- for (const prefix of prefixes) {
1995
- paths.push(`${prefix}${key}`);
1911
+ var TRAY_LIMIT = 50;
1912
+ function createAfterTurnRecorder(options) {
1913
+ const { client, logger } = options;
1914
+ return async (record, end) => {
1915
+ if (end.status === "still_running" || record.kind === "run") return;
1916
+ let fresh;
1917
+ try {
1918
+ fresh = await client.inbox.tray({ waitMs: 0, limit: TRAY_LIMIT, includeInvites: true });
1919
+ } catch (err) {
1920
+ logger.warn({ runId: record.runId, err: describe3(err) }, "after-turn tray read failed \u2014 nothing recorded; the server re-wakes the card");
1921
+ fresh = null;
1922
+ }
1923
+ try {
1924
+ if (record.kind === "reply") {
1925
+ if (!record.card) return;
1926
+ const outcome2 = integration.turnOutcome({
1927
+ turn: end,
1928
+ turnStartedAt: record.startedAt,
1929
+ gateCard: record.card,
1930
+ category: record.category ?? null,
1931
+ freshTray: fresh,
1932
+ silentBefore: 0
1933
+ });
1934
+ const recorded2 = await integration.applyOutcome(client, outcome2);
1935
+ logger.info({ runId: record.runId, conversationId: record.id, action: outcome2.action, reason: outcome2.reason, recorded: recorded2 }, "after-turn rule applied (turn ended outside the loop\u2019s watch)");
1936
+ return;
1937
+ }
1938
+ if (!record.invite) return;
1939
+ const outcome = integration.inviteOutcome({ turn: end, invite: record.invite, freshTray: fresh, silentBefore: 0 });
1940
+ const recorded = await integration.applyInviteOutcome(client, outcome);
1941
+ logger.info({ runId: record.runId, inviteId: record.id, action: outcome.action, reason: outcome.reason, recorded }, "after-turn rule applied to the invite (turn ended outside the loop\u2019s watch)");
1942
+ } catch (err) {
1943
+ logger.warn({ runId: record.runId, err: describe3(err) }, "after-turn recording failed \u2014 the card stays on the tray; the server re-wakes it");
1996
1944
  }
1997
- }
1998
- paths.push(...keys.map((k) => `*.${k}`));
1999
- return paths;
2000
- }
2001
- function wrapPino(p) {
2002
- return {
2003
- trace: (obj, msg) => p.trace(obj, msg),
2004
- debug: (obj, msg) => p.debug(obj, msg),
2005
- info: (obj, msg) => p.info(obj, msg),
2006
- warn: (obj, msg) => p.warn(obj, msg),
2007
- error: (obj, msg) => p.error(obj, msg),
2008
- child: (bindings) => wrapPino(p.child(bindings))
1945
+ };
1946
+ }
1947
+ function cardFromStored(stored) {
1948
+ return { preview: null, ...stored };
1949
+ }
1950
+ function describe3(err) {
1951
+ return err instanceof Error ? err.message : String(err);
1952
+ }
1953
+ var TURN_STORE_FILE = "turns.json";
1954
+ var KEEP_ENDED_MS = 6e4;
1955
+ function storedCard(card, anchor) {
1956
+ return {
1957
+ conversation_id: card.conversation_id,
1958
+ ...anchor ? { last_message_id: anchor } : {},
1959
+ ...card.your_last_message_at !== void 0 ? { your_last_message_at: card.your_last_message_at } : {},
1960
+ ...card.you_owe_reply !== void 0 ? { you_owe_reply: card.you_owe_reply } : {},
1961
+ ...card.your_last_decision !== void 0 ? { your_last_decision: card.your_last_decision } : {}
1962
+ };
1963
+ }
1964
+ var TurnStore = class {
1965
+ file;
1966
+ constructor(accountId, stateDir) {
1967
+ this.file = path2.join(integrationStateDir(accountId, stateDir), TURN_STORE_FILE);
1968
+ }
1969
+ /** What the last process left. Empty when there is no file or it cannot be read. */
1970
+ load() {
1971
+ let parsed;
1972
+ try {
1973
+ parsed = JSON.parse(fs.readFileSync(this.file, "utf8"));
1974
+ } catch {
1975
+ return [];
1976
+ }
1977
+ const turns = parsed?.turns;
1978
+ if (parsed?.version !== 1 || !Array.isArray(turns)) return [];
1979
+ return turns.filter(isStoredTurn);
1980
+ }
1981
+ /** Replace the file with `turns` (removed when empty). Returns false when the write failed. */
1982
+ write(turns) {
1983
+ try {
1984
+ if (turns.length === 0) {
1985
+ fs.rmSync(this.file, { force: true });
1986
+ return true;
1987
+ }
1988
+ fs.mkdirSync(path2.dirname(this.file), { recursive: true });
1989
+ const body = { version: 1, turns };
1990
+ const tmp = `${this.file}.${process.pid}.tmp`;
1991
+ fs.writeFileSync(tmp, `${JSON.stringify(body)}
1992
+ `, { mode: 384 });
1993
+ fs.renameSync(tmp, this.file);
1994
+ return true;
1995
+ } catch {
1996
+ return false;
1997
+ }
1998
+ }
1999
+ };
2000
+ function isStoredTurn(value) {
2001
+ const turn = value;
2002
+ return typeof turn === "object" && turn !== null && typeof turn.runId === "string" && (turn.kind === "reply" || turn.kind === "invite") && typeof turn.id === "string" && turn.id.length > 0 && typeof turn.sessionKey === "string" && turn.sessionKey.length > 0 && typeof turn.startedAt === "number" && (turn.anchor === null || typeof turn.anchor === "string");
2003
+ }
2004
+
2005
+ // src/integration/running.ts
2006
+ var TURN_OVERDUE_MS = 30 * 6e4;
2007
+ var OVERDUE_SLACK_MS = 5e3;
2008
+ function isOverdue(elapsedMs) {
2009
+ return elapsedMs + OVERDUE_SLACK_MS >= TURN_OVERDUE_MS;
2010
+ }
2011
+ var MAX_HUNG_TURNS = 2;
2012
+ var OPENCLAW_DEFAULT_RUN_TIMEOUT_SECONDS = 2880 * 60;
2013
+ var RESTART_HOLD_MS = 3 * 6e4;
2014
+ var STORED_TURN_MAX_AGE_MS = 24 * 60 * 6e4;
2015
+ var RESTART_END_DETAIL = "the gateway restarted during the turn";
2016
+ function trackerKey(kind, id) {
2017
+ return kind === "reply" ? `conversation:${id}` : kind === "invite" ? `invite:${id}` : `run:${id}`;
2018
+ }
2019
+ function trackedTurn(record) {
2020
+ if (record.kind === "reply") return { kind: "reply", conversationId: record.id, anchor: record.anchor };
2021
+ if (record.kind === "invite") return { kind: "invite", inviteId: record.id };
2022
+ return null;
2023
+ }
2024
+ function storedOf(record) {
2025
+ if (record.kind === "run") return null;
2026
+ return {
2027
+ runId: record.runId,
2028
+ kind: record.kind,
2029
+ id: record.id,
2030
+ anchor: record.anchor,
2031
+ sessionKey: record.sessionKey,
2032
+ startedAt: record.startedAt,
2033
+ ...record.firstStartedAt !== void 0 ? { firstStartedAt: record.firstStartedAt } : {},
2034
+ ...record.category !== void 0 ? { category: record.category } : {},
2035
+ ...record.kind === "reply" && record.card ? { card: storedCard(record.card, record.anchor) } : {}
2036
+ };
2037
+ }
2038
+ function recordOf(stored) {
2039
+ return {
2040
+ runId: stored.runId,
2041
+ kind: stored.kind,
2042
+ id: stored.id,
2043
+ anchor: stored.anchor,
2044
+ sessionKey: stored.sessionKey,
2045
+ startedAt: stored.startedAt,
2046
+ firstStartedAt: stored.firstStartedAt ?? stored.startedAt,
2047
+ owner: "previous-process",
2048
+ ...stored.category !== void 0 ? { category: stored.category } : {},
2049
+ ...stored.kind === "reply" && stored.card ? { card: cardFromStored(stored.card) } : {},
2050
+ ...stored.kind === "invite" ? { invite: { invite_id: stored.id } } : {}
2051
+ };
2052
+ }
2053
+ var AccountTurns = class {
2054
+ /** The kit's tracker for the account, shared by every loop for it. */
2055
+ tracker;
2056
+ entries = /* @__PURE__ */ new Map();
2057
+ /** Turns the last process left in flight, by session key. */
2058
+ adopted = /* @__PURE__ */ new Map();
2059
+ /** Turns OpenClaw is admitting right now. */
2060
+ admitting = /* @__PURE__ */ new Set();
2061
+ /** Ended runs still written to the store (`KEEP_ENDED_MS`). */
2062
+ kept = /* @__PURE__ */ new Map();
2063
+ keptTimer = null;
2064
+ live = null;
2065
+ wiring = {};
2066
+ /** Which OpenClaw lane new turns go to; moves on at every hand-off (a hung run blocks its lane). */
2067
+ lane = 0;
2068
+ now;
2069
+ constructor(options = {}) {
2070
+ this.now = options.now ?? Date.now;
2071
+ this.tracker = new integration.TurnTracker({ now: this.now });
2072
+ this.tracker.onRelease((handle) => {
2073
+ for (const admission of this.admitting) if (admission.key === handle.key) admission.released = true;
2074
+ });
2075
+ }
2076
+ /** The account's after-turn recorder, store and run watcher (each account start; the latest wins). */
2077
+ configure(wiring) {
2078
+ this.wiring = { ...this.wiring, ...wiring };
2079
+ }
2080
+ /** The loop now receiving for the account; a held run that ends wakes it. `run` = the loop's own async context (default: as called). */
2081
+ setLive(owner, wake, run = (fn) => fn()) {
2082
+ this.live = { owner, wake, run };
2083
+ }
2084
+ clearLive(owner) {
2085
+ if (this.live?.owner === owner) this.live = null;
2086
+ }
2087
+ /** A turn is about to be admitted by OpenClaw: watch whether its kit hold is released meanwhile. */
2088
+ admission(kind, id) {
2089
+ const admission = { key: trackerKey(kind, id), released: false };
2090
+ this.admitting.add(admission);
2091
+ return admission;
2092
+ }
2093
+ /** OpenClaw refused the turn: nothing to watch. */
2094
+ admitted(admission) {
2095
+ this.admitting.delete(admission);
2096
+ }
2097
+ /**
2098
+ * A run OpenClaw admitted. `letGo` (gate-off runs): ends the cycle waiting
2099
+ * on it when it is handed off or released. `admission`: a turn whose kit
2100
+ * hold was released during admission starts out let go of.
2101
+ */
2102
+ started(record, options = {}) {
2103
+ const releasedMeanwhile = options.admission?.released === true;
2104
+ if (options.admission) this.admitting.delete(options.admission);
2105
+ this.entries.set(record.runId, {
2106
+ record,
2107
+ state: releasedMeanwhile ? "released" : "kit",
2108
+ handle: null,
2109
+ since: 0,
2110
+ ...options.letGo ? { letGo: options.letGo } : {}
2111
+ });
2112
+ this.persist();
2113
+ }
2114
+ run(runId) {
2115
+ return this.entries.get(runId)?.record;
2116
+ }
2117
+ /** Where the run stands (`null`: not a run of this account, or ended). */
2118
+ stateOf(runId) {
2119
+ return this.entries.get(runId)?.state ?? null;
2120
+ }
2121
+ /** Every run OpenClaw has not reported ended that still holds something, oldest first. */
2122
+ runningRuns() {
2123
+ return [...this.entries.values()].filter((entry) => entry.state !== "released").map((entry) => entry.record).sort((a, b) => a.startedAt - b.startedAt);
2124
+ }
2125
+ hungRuns() {
2126
+ return [...this.entries.values()].filter((entry) => entry.state === "hung").map((entry) => entry.record);
2127
+ }
2128
+ get hungCount() {
2129
+ let count = 0;
2130
+ for (const entry of this.entries.values()) if (entry.state === "hung") count += 1;
2131
+ return count;
2132
+ }
2133
+ /** The handed-off gate-off run still going, if any: no new run starts while it lives. */
2134
+ hungSingleRun() {
2135
+ for (const entry of this.entries.values()) if (entry.state === "hung" && entry.record.kind === "run") return entry.record;
2136
+ return null;
2137
+ }
2138
+ /** Conversations (and invites) held after a restart: adopted (until when), or continued by a run OpenClaw started. */
2139
+ restartHolds() {
2140
+ const holds = [...this.adopted.values()].map((a) => ({
2141
+ record: a.record,
2142
+ until: a.until,
2143
+ watching: null
2144
+ }));
2145
+ for (const entry of this.entries.values()) if (entry.state === "watch") holds.push({ record: entry.record, until: null, watching: entry.record.runId });
2146
+ return holds;
2147
+ }
2148
+ /** The lane generation new turns start on (`integrationLane(accountId, laneGeneration)`). */
2149
+ get laneGeneration() {
2150
+ return this.lane;
2151
+ }
2152
+ /**
2153
+ * OpenClaw reported the run ended (`end`: what it did, when known). A turn
2154
+ * held here (handed off, or continued after a restart) gets the after-turn
2155
+ * rule, then its hold drops and the live loop is woken. A turn the kit
2156
+ * still holds is the kit's to record; the live loop is woken once the card
2157
+ * is free, only when an earlier loop started it. A run let go of records
2158
+ * nothing.
2159
+ */
2160
+ async ended(runId, end) {
2161
+ const entry = this.entries.get(runId);
2162
+ if (!entry) return;
2163
+ this.entries.delete(runId);
2164
+ if (entry.state !== "released") this.keep(entry.record);
2165
+ else this.persist();
2166
+ if (entry.state === "hung" || entry.state === "watch") {
2167
+ try {
2168
+ if (end && this.wiring.recorder && entry.record.kind !== "run") await this.wiring.recorder(entry.record, end);
2169
+ } finally {
2170
+ if (entry.handle) this.tracker.end(entry.handle);
2171
+ this.live?.wake();
2172
+ }
2173
+ return;
2174
+ }
2175
+ if (entry.state === "released" || entry.record.kind === "run") return;
2176
+ if (!this.live || this.live.owner === entry.record.owner) return;
2177
+ this.wakeWhenFree(trackerKey(entry.record.kind, entry.record.id));
2178
+ }
2179
+ /**
2180
+ * Hand a long run off, once, by run id: release the kit's slot for it (its
2181
+ * loop records nothing for it and may start other turns) and hold the
2182
+ * conversation here until the run ends. A gate-off run: the cycle waiting
2183
+ * on it is let go of. A continued run is already held here: it becomes
2184
+ * hung (counted toward `MAX_HUNG_TURNS`). Returns the record when it was
2185
+ * handed off now; null when it was already handed off or let go of.
2186
+ */
2187
+ handOff(runId) {
2188
+ const entry = this.entries.get(runId);
2189
+ if (!entry || entry.state !== "kit" && entry.state !== "watch") return null;
2190
+ const record = entry.record;
2191
+ entry.since = this.now();
2192
+ if (entry.state === "watch") {
2193
+ entry.state = "hung";
2194
+ this.live?.wake();
2195
+ return record;
2196
+ }
2197
+ this.lane += 1;
2198
+ entry.state = "hung";
2199
+ const turn = trackedTurn(record);
2200
+ if (turn === null) {
2201
+ entry.letGo?.();
2202
+ } else {
2203
+ this.tracker.release(trackerKey(record.kind, record.id));
2204
+ entry.handle = this.tracker.begin(turn);
2205
+ if (!entry.handle) entry.state = "released";
2206
+ }
2207
+ this.live?.wake();
2208
+ return entry.state === "hung" ? record : null;
2209
+ }
2210
+ /**
2211
+ * An operator lets go of a conversation's (or invite's) turn: a hung hold,
2212
+ * a restart hold, or — past `TURN_OVERDUE_MS`, or with `force` — a turn
2213
+ * still running. Its run may still be live and answer: a second reply is
2214
+ * then possible, which is why a turn under 30 minutes (most likely still
2215
+ * working) is refused without `force`. A gate-off run always needs
2216
+ * `force`: it reads the whole tray, so a new run would answer the same
2217
+ * cards while it may still be going.
2218
+ */
2219
+ release(id, options = {}) {
2220
+ const live = this.live;
2221
+ return live ? live.run(() => this.releaseNow(id, options.force === true)) : this.releaseNow(id, options.force === true);
2222
+ }
2223
+ releaseNow(id, force) {
2224
+ const matches = (record) => record.id === id || record.runId === id;
2225
+ for (const entry of this.entries.values()) {
2226
+ if (entry.state !== "hung" || !matches(entry.record)) continue;
2227
+ if (entry.record.kind === "run" && !force) return this.refuseSingleRun(entry.record);
2228
+ entry.state = "released";
2229
+ if (entry.handle) this.tracker.end(entry.handle);
2230
+ entry.handle = null;
2231
+ this.persist();
2232
+ this.live?.wake();
2233
+ return { released: true, what: "hung", key: trackerKey(entry.record.kind, entry.record.id), runId: entry.record.runId };
2234
+ }
2235
+ for (const [sessionKey, adopted] of this.adopted) {
2236
+ if (adopted.record.id !== id) continue;
2237
+ this.finishAdopted(sessionKey);
2238
+ return { released: true, what: "restart", key: trackerKey(adopted.record.kind, adopted.record.id), runId: null };
2239
+ }
2240
+ for (const entry of this.entries.values()) {
2241
+ if (entry.state !== "kit" && entry.state !== "watch" || !matches(entry.record)) continue;
2242
+ const refused = this.refuseYoung(entry.record.kind, entry.record.id, entry.record.startedAt, force);
2243
+ if (refused) return refused;
2244
+ const key = trackerKey(entry.record.kind, entry.record.id);
2245
+ if (entry.state === "watch") {
2246
+ if (entry.handle) this.tracker.end(entry.handle);
2247
+ entry.handle = null;
2248
+ } else {
2249
+ this.lane += 1;
2250
+ if (entry.record.kind === "run") entry.letGo?.();
2251
+ else this.tracker.release(key);
2252
+ }
2253
+ entry.state = "released";
2254
+ this.persist();
2255
+ this.live?.wake();
2256
+ return { released: true, what: "running", key, runId: entry.record.runId };
2257
+ }
2258
+ for (const held of this.tracker.running()) {
2259
+ const heldId = held.kind === "reply" ? held.conversationId : held.inviteId;
2260
+ if (heldId !== id) continue;
2261
+ const refused = this.refuseYoung(held.kind, heldId, held.startedAt, force);
2262
+ if (refused) return refused;
2263
+ this.lane += 1;
2264
+ const key = trackerKey(held.kind, heldId);
2265
+ if (this.tracker.release(key)) {
2266
+ this.live?.wake();
2267
+ return { released: true, what: "running", key, runId: null };
2268
+ }
2269
+ }
2270
+ return { released: false, reason: `no turn holds ${id}` };
2271
+ }
2272
+ refuseYoung(kind, id, startedAt, force) {
2273
+ const age = this.now() - startedAt;
2274
+ if (force || age >= TURN_OVERDUE_MS) return null;
2275
+ return {
2276
+ released: false,
2277
+ reason: `the turn for ${whatOf(kind, id)} has run ${Math.floor(age / 6e4)} min \u2014 under ${TURN_OVERDUE_MS / 6e4} min it is most likely still working, and releasing it lets the conversation be gated again while that run can still send its answer (the peer would get two replies); wait, or repeat with --force`
2278
+ };
2279
+ }
2280
+ refuseSingleRun(record) {
2281
+ return {
2282
+ released: false,
2283
+ reason: `${whatOf("run", record.runId)} has run ${Math.floor((this.now() - record.startedAt) / 6e4)} min and reads the whole tray \u2014 releasing it lets a new run answer the same cards while this one may still be going (the peers would get two replies); repeat with --force`
2284
+ };
2285
+ }
2286
+ /**
2287
+ * Adopt the turns the last process left in flight (the store). Each one's
2288
+ * conversation (or invite) is held at once — before the loop's first gate —
2289
+ * for `RESTART_HOLD_MS`; the after-turn rule is applied to what the ended
2290
+ * run did now and again when the hold expires. A run OpenClaw starts in the
2291
+ * turn's session meanwhile (`observeRun`) takes the hold over. A turn first
2292
+ * started more than `STORED_TURN_MAX_AGE_MS` ago is dropped. Returns what
2293
+ * was adopted.
2294
+ */
2295
+ adopt(stored = this.wiring.store?.load() ?? []) {
2296
+ const adopted = [];
2297
+ const now = this.now();
2298
+ for (const turn of stored) {
2299
+ if (this.entries.has(turn.runId) || this.adopted.has(turn.sessionKey) || this.kept.has(turn.runId)) continue;
2300
+ const first = turn.firstStartedAt ?? turn.startedAt;
2301
+ if (now - first > STORED_TURN_MAX_AGE_MS) {
2302
+ this.wiring.logger?.info({ runId: turn.runId, [turn.kind === "reply" ? "conversationId" : "inviteId"]: turn.id }, "a stored turn older than a day was dropped, not adopted again");
2303
+ continue;
2304
+ }
2305
+ const record = recordOf(turn);
2306
+ const tracked = trackedTurn(record);
2307
+ const handle = tracked ? this.tracker.begin(tracked) : null;
2308
+ if (!handle) continue;
2309
+ const entry = { stored: { ...turn, firstStartedAt: first }, record, handle, until: now + RESTART_HOLD_MS, end: null, timer: null };
2310
+ this.adopted.set(turn.sessionKey, entry);
2311
+ entry.timer = setTimeout(() => this.expireAdopted(turn.sessionKey, entry), RESTART_HOLD_MS);
2312
+ entry.timer.unref?.();
2313
+ adopted.push(record);
2314
+ void this.recordRestartEnded(entry);
2315
+ }
2316
+ this.persist();
2317
+ return adopted;
2318
+ }
2319
+ /**
2320
+ * A run started in some session (the `model_call_started` hook). One in
2321
+ * the session of a turn adopted after a restart is OpenClaw continuing
2322
+ * that turn: it takes the hold over as a turn of its own (`watch`), handed
2323
+ * off past `TURN_OVERDUE_MS` like any other.
2324
+ */
2325
+ observeRun(runId, sessionKey) {
2326
+ if (!sessionKey || this.entries.has(runId)) return;
2327
+ const adopted = this.adopted.get(sessionKey);
2328
+ if (!adopted) return;
2329
+ this.adopted.delete(sessionKey);
2330
+ if (adopted.timer) clearTimeout(adopted.timer);
2331
+ const record = { ...adopted.record, runId, startedAt: this.now(), owner: "restart" };
2332
+ this.entries.set(runId, { record, state: "watch", handle: adopted.handle, since: 0 });
2333
+ this.persist();
2334
+ this.wiring.logger?.info(
2335
+ { runId, sessionKey, [record.kind === "reply" ? "conversationId" : "inviteId"]: record.id },
2336
+ "OpenClaw continued a turn session after the restart \u2014 its conversation stays held until that run ends"
2337
+ );
2338
+ void this.watchContinued(record);
2339
+ }
2340
+ async watchContinued(record) {
2341
+ let end = null;
2342
+ try {
2343
+ end = this.wiring.watch ? await this.wiring.watch(record) : null;
2344
+ } catch {
2345
+ end = null;
2346
+ }
2347
+ if (end === null) return;
2348
+ await this.ended(record.runId, end);
2349
+ }
2350
+ async recordRestartEnded(adopted) {
2351
+ const { record } = adopted;
2352
+ let calls = null;
2353
+ try {
2354
+ calls = this.wiring.readCalls ? await this.wiring.readCalls(record.sessionKey, record.runId) : null;
2355
+ } catch {
2356
+ calls = null;
2357
+ }
2358
+ adopted.end = { status: "failed", agentchatCalls: calls, detail: RESTART_END_DETAIL };
2359
+ if (this.adopted.get(record.sessionKey) !== adopted || !this.wiring.recorder) return;
2360
+ await this.wiring.recorder(record, adopted.end);
2361
+ }
2362
+ /**
2363
+ * The hold expired with nothing continuing the turn: the after-turn rule
2364
+ * once more — the resumed main session may have answered the card
2365
+ * meanwhile — then the conversation is free.
2366
+ */
2367
+ expireAdopted(sessionKey, adopted) {
2368
+ if (this.adopted.get(sessionKey) !== adopted) return;
2369
+ void (async () => {
2370
+ try {
2371
+ if (this.wiring.recorder) await this.wiring.recorder(adopted.record, adopted.end ?? { status: "failed", agentchatCalls: null, detail: RESTART_END_DETAIL });
2372
+ } catch {
2373
+ }
2374
+ if (this.adopted.get(sessionKey) === adopted) this.finishAdopted(sessionKey);
2375
+ })();
2376
+ }
2377
+ finishAdopted(sessionKey) {
2378
+ const adopted = this.adopted.get(sessionKey);
2379
+ if (!adopted) return;
2380
+ this.adopted.delete(sessionKey);
2381
+ if (adopted.timer) clearTimeout(adopted.timer);
2382
+ this.tracker.end(adopted.handle);
2383
+ this.persist();
2384
+ this.live?.wake();
2385
+ }
2386
+ /** An ended run stays in the store a little longer: a restart ends the run before it ends the process. */
2387
+ keep(record) {
2388
+ const turn = storedOf(record);
2389
+ if (turn) this.kept.set(record.runId, { turn, endedAt: this.now() });
2390
+ this.persist();
2391
+ if (!this.keptTimer) this.schedulePrune();
2392
+ }
2393
+ /** The next prune at the oldest kept entry's expiry. */
2394
+ schedulePrune() {
2395
+ let oldest = Infinity;
2396
+ for (const kept of this.kept.values()) oldest = Math.min(oldest, kept.endedAt);
2397
+ if (oldest === Infinity) return;
2398
+ this.keptTimer = setTimeout(() => this.pruneKept(), Math.max(0, oldest + KEEP_ENDED_MS - this.now()));
2399
+ this.keptTimer.unref?.();
2400
+ }
2401
+ pruneKept() {
2402
+ this.keptTimer = null;
2403
+ const cutoff = this.now() - KEEP_ENDED_MS;
2404
+ for (const [runId, kept] of this.kept) if (kept.endedAt <= cutoff) this.kept.delete(runId);
2405
+ this.persist();
2406
+ this.schedulePrune();
2407
+ }
2408
+ persist() {
2409
+ const store = this.wiring.store;
2410
+ if (!store) return;
2411
+ const turns = /* @__PURE__ */ new Map();
2412
+ for (const entry of this.entries.values()) {
2413
+ if (entry.state === "released") continue;
2414
+ const turn = storedOf(entry.record);
2415
+ if (turn) turns.set(turn.runId, turn);
2416
+ }
2417
+ for (const [runId, kept] of this.kept) turns.set(runId, kept.turn);
2418
+ for (const adopted of this.adopted.values()) turns.set(adopted.stored.runId, adopted.stored);
2419
+ if (!store.write([...turns.values()])) this.wiring.logger?.debug({ file: store.file }, "turns in flight could not be written (the restart defense is off until the next write)");
2420
+ }
2421
+ wakeWhenFree(key, attempts = 300) {
2422
+ const held = key.startsWith("conversation:") ? this.tracker.conversationRunning(key.slice("conversation:".length)) : this.tracker.inviteRunning(key.slice("invite:".length));
2423
+ if (!held || attempts <= 0) {
2424
+ this.live?.wake();
2425
+ return;
2426
+ }
2427
+ const timer = setTimeout(() => this.wakeWhenFree(key, attempts - 1), 100);
2428
+ timer.unref?.();
2429
+ }
2430
+ };
2431
+ var accounts = /* @__PURE__ */ new Map();
2432
+ function accountTurns(accountId) {
2433
+ let turns = accounts.get(accountId);
2434
+ if (!turns) {
2435
+ turns = new AccountTurns();
2436
+ accounts.set(accountId, turns);
2437
+ }
2438
+ return turns;
2439
+ }
2440
+ function findAccountTurns(accountId) {
2441
+ return accounts.get(accountId);
2442
+ }
2443
+ function observeRunStarted(runId, sessionKey) {
2444
+ if (typeof runId !== "string" || runId.length === 0 || typeof sessionKey !== "string" || sessionKey.length === 0) return;
2445
+ for (const turns of accounts.values()) turns.observeRun(runId, sessionKey);
2446
+ }
2447
+ function runDeadline(cfg) {
2448
+ const value = cfg?.agents?.defaults?.timeoutSeconds;
2449
+ if (typeof value === "number" && Number.isFinite(value) && value >= 0) return { seconds: Math.floor(value), configured: true };
2450
+ return { seconds: OPENCLAW_DEFAULT_RUN_TIMEOUT_SECONDS, configured: false };
2451
+ }
2452
+ function whatOf(kind, id) {
2453
+ return kind === "reply" ? `conversation ${id}` : kind === "invite" ? `invite ${id}` : `the gate-off run ${id}`;
2454
+ }
2455
+ function deadlineText(cfg) {
2456
+ const deadline = runDeadline(cfg);
2457
+ return deadline.seconds === 0 ? "agents.defaults.timeoutSeconds is 0 (no deadline), so it may never end" : `OpenClaw ends it at its run deadline, agents.defaults.timeoutSeconds = ${deadline.seconds}${deadline.configured ? "" : " (OpenClaw's default, 48 h)"}`;
2458
+ }
2459
+ function overdueTurnLine(turn, cfg) {
2460
+ const minutes = Math.round(turn.elapsedMs / 6e4);
2461
+ if (turn.kind === "run") {
2462
+ return `${whatOf(turn.kind, turn.id)} has run ${minutes} min \u2014 no new run starts until it ends (one run reads the whole tray); ${deadlineText(cfg)}`;
2463
+ }
2464
+ return `a turn for ${whatOf(turn.kind, turn.id)} has run ${minutes} min \u2014 that conversation stays held (no second reply) until the run ends; ${deadlineText(cfg)}; other conversations are not held up by it`;
2465
+ }
2466
+ function overdueTurnLines(runs, cfg, now = Date.now()) {
2467
+ return runs.filter((run) => now - run.startedAt > TURN_OVERDUE_MS).map((run) => overdueTurnLine({ kind: run.kind, id: run.id, elapsedMs: now - run.startedAt }, cfg));
2468
+ }
2469
+ function restartHoldLines(turns, now = Date.now()) {
2470
+ return turns.restartHolds().map(
2471
+ ({ record, until, watching }) => watching || until === null ? `${whatOf(record.kind, record.id)} is held: OpenClaw continued its turn's session after the gateway restart (run ${watching ?? "unknown"}) \u2014 gated again once that run ends (handed off like any turn past 30 min)` : `${whatOf(record.kind, record.id)} is held for ${Math.max(0, Math.ceil((until - now) / 6e4))} more min: its turn was running when the gateway restarted, and OpenClaw may continue that turn's session`
2472
+ );
2473
+ }
2474
+ function hungTurnsReason(turns, cfg, now = Date.now()) {
2475
+ const single = turns.hungSingleRun();
2476
+ if (single) {
2477
+ return `${whatOf("run", single.runId)} has run ${Math.floor((now - single.startedAt) / 6e4)} min and is still going in OpenClaw \u2014 no new run starts while it lives (one run reads the whole tray; a second would answer the same cards), so the cards wait on the tray; ${deadlineText(cfg)}. \`openclaw agentchat release ${single.runId} --force\` lets a new run start (the old one may still answer)`;
2478
+ }
2479
+ if (turns.hungCount < MAX_HUNG_TURNS) return null;
2480
+ const held = turns.hungRuns().map((run) => `${whatOf(run.kind, run.id)} for ${Math.floor((now - run.startedAt) / 6e4)} min`).join(", ");
2481
+ return `${turns.hungCount} turns are hung in OpenClaw (${held}) \u2014 no gate calls until one ends, so other cards are waiting on the tray; ${deadlineText(cfg)}. \`openclaw agentchat release <conversation_id>\` lets go of one (its run may still answer, so a second reply is possible), or restart the gateway`;
2482
+ }
2483
+
2484
+ // src/binding/release.ts
2485
+ var RELEASE_METHOD = "agentchat.release";
2486
+ function releaseTurnFor(params) {
2487
+ const id = typeof params.id === "string" ? params.id.trim() : "";
2488
+ if (!id) return { ok: false, error: "a conversation (or invite) id is required" };
2489
+ const accountId = typeof params.accountId === "string" && params.accountId.trim() ? params.accountId.trim() : "default";
2490
+ const turns = findAccountTurns(accountId);
2491
+ if (!turns) return { ok: true, result: { released: false, reason: `the AgentChat account "${accountId}" has no turns in this gateway` } };
2492
+ return { ok: true, result: turns.release(id, { force: params.force === true }) };
2493
+ }
2494
+ function registerReleaseGatewayMethod(api) {
2495
+ api.registerGatewayMethod(
2496
+ RELEASE_METHOD,
2497
+ ({ params, respond }) => {
2498
+ const outcome = releaseTurnFor(params);
2499
+ if (!outcome.ok) {
2500
+ respond(false, void 0, { code: "INVALID_REQUEST", message: outcome.error });
2501
+ return;
2502
+ }
2503
+ respond(true, outcome.result);
2504
+ },
2505
+ { scope: "operator.admin" }
2506
+ );
2507
+ }
2508
+ function registerReleaseCli(api) {
2509
+ api.registerCli(
2510
+ ({ program }) => {
2511
+ const root = program.command(AGENTCHAT_CHANNEL_ID).description("The AgentChat integration");
2512
+ root.command("release <id>").description(
2513
+ `Let go of the turn holding a conversation (or invite) whose OpenClaw run never ended. The run is not stopped: if it answers later, the peer can get a second reply. A turn under ${TURN_OVERDUE_MS / 6e4} minutes is refused without --force.`
2514
+ ).option("--account <accountId>", "the AgentChat account", "default").option("--force", `release a turn that has run under ${TURN_OVERDUE_MS / 6e4} minutes (it is most likely still working: two replies are possible)`).option("--url <url>", "gateway URL (default: the configured gateway)").option("--token <token>", "gateway token (default: the configured one)").option("--json", "print the raw result").action(async (id, opts) => {
2515
+ const { callGatewayFromCli } = await import('openclaw/plugin-sdk/gateway-runtime');
2516
+ const result = await callGatewayFromCli(
2517
+ RELEASE_METHOD,
2518
+ { ...opts.url ? { url: opts.url } : {}, ...opts.token ? { token: opts.token } : {}, ...opts.json ? { json: true } : {} },
2519
+ { id, accountId: opts.account ?? "default", ...opts.force ? { force: true } : {} }
2520
+ );
2521
+ process.stdout.write(opts.json ? `${JSON.stringify(result)}
2522
+ ` : `${releaseLine(id, result)}
2523
+ `);
2524
+ });
2525
+ },
2526
+ {
2527
+ commands: [AGENTCHAT_CHANNEL_ID],
2528
+ descriptors: [{ name: AGENTCHAT_CHANNEL_ID, description: "The AgentChat integration (release a hung turn)", hasSubcommands: true }]
2529
+ }
2530
+ );
2531
+ }
2532
+ function releaseLine(id, result) {
2533
+ if (!result?.released) return `nothing released: ${result?.reason ?? "unknown"}`;
2534
+ if (result.what === "restart") return `released ${id} (held after a gateway restart) \u2014 it may be gated again now`;
2535
+ const what = result.what === "hung" ? "a hung turn" : "a running turn";
2536
+ return `released ${id} (${what}) \u2014 it may be gated again now; its OpenClaw run was not stopped, and if it still sends an answer the peer gets two replies`;
2537
+ }
2538
+
2539
+ // src/binding/run-observer.ts
2540
+ function registerRunObserver(api) {
2541
+ api.on("model_call_started", (event, ctx) => {
2542
+ observeRunStarted(event.runId, event.sessionKey ?? ctx?.sessionKey);
2543
+ });
2544
+ }
2545
+ function createLogger(options) {
2546
+ if (options.delegate) {
2547
+ return options.delegate;
2548
+ }
2549
+ const pinoLogger = pino({
2550
+ level: options.level,
2551
+ base: { plugin: "@agentchatme/openclaw" },
2552
+ redact: {
2553
+ paths: buildRedactPaths(options.redactKeys),
2554
+ censor: "[REDACTED]"
2555
+ },
2556
+ timestamp: pino.stdTimeFunctions.isoTime
2557
+ });
2558
+ return wrapPino(pinoLogger);
2559
+ }
2560
+ function buildRedactPaths(keys) {
2561
+ const prefixes = ["", "config.", "headers.", "context.", "req.headers.", "res.headers."];
2562
+ const paths = [];
2563
+ for (const key of keys) {
2564
+ for (const prefix of prefixes) {
2565
+ paths.push(`${prefix}${key}`);
2566
+ }
2567
+ }
2568
+ paths.push(...keys.map((k) => `*.${k}`));
2569
+ return paths;
2570
+ }
2571
+ function wrapPino(p) {
2572
+ return {
2573
+ trace: (obj, msg) => p.trace(obj, msg),
2574
+ debug: (obj, msg) => p.debug(obj, msg),
2575
+ info: (obj, msg) => p.info(obj, msg),
2576
+ warn: (obj, msg) => p.warn(obj, msg),
2577
+ error: (obj, msg) => p.error(obj, msg),
2578
+ child: (bindings) => wrapPino(p.child(bindings))
2009
2579
  };
2010
2580
  }
2011
2581
 
@@ -2037,85 +2607,12 @@ function createNoopMetrics() {
2037
2607
  incWake: () => void 0
2038
2608
  };
2039
2609
  }
2040
- function groupInviteFromRecord(record) {
2041
- if (!record || typeof record !== "object" || Array.isArray(record)) return null;
2042
- const row = record;
2043
- const inviteId = str(row.id);
2044
- const groupId = str(row.group_id);
2045
- if (!inviteId || !groupId) return null;
2046
- return {
2047
- inviteId,
2048
- groupId,
2049
- groupName: str(row.group_name) ?? "",
2050
- inviterHandle: (str(row.inviter_handle) ?? "").replace(/^@/, "")
2051
- };
2052
- }
2053
- function str(value) {
2054
- return typeof value === "string" && value.length > 0 ? value : void 0;
2055
- }
2056
- var INVITE_LEDGER_FILE = "invites.json";
2057
- var INVITE_LEDGER_MAX_AGE_MS = 30 * 24 * 60 * 6e4;
2058
- function inviteLedgerPath(accountId, stateDir) {
2059
- return path2.join(integrationStateDir(accountId, stateDir), INVITE_LEDGER_FILE);
2060
- }
2061
- var InviteLedger = class {
2062
- file;
2063
- now;
2064
- maxAgeMs;
2065
- onWriteError;
2066
- offered;
2067
- constructor(options) {
2068
- this.file = options.file ?? inviteLedgerPath(options.accountId, options.stateDir);
2069
- this.now = options.now ?? Date.now;
2070
- this.maxAgeMs = options.maxAgeMs ?? INVITE_LEDGER_MAX_AGE_MS;
2071
- this.onWriteError = options.onWriteError;
2072
- this.offered = this.read();
2073
- }
2074
- get path() {
2075
- return this.file;
2076
- }
2077
- get size() {
2078
- return this.offered.size;
2079
- }
2080
- /** True when a turn for this invite was started before (in this process or an earlier one). */
2081
- isOffered(inviteId) {
2082
- return this.offered.has(inviteId);
2083
- }
2084
- /** Record that a turn for this invite was admitted. Persists at once. */
2085
- markOffered(inviteId) {
2086
- this.offered.set(inviteId, new Date(this.now()).toISOString());
2087
- this.prune();
2088
- try {
2089
- writeJsonFile(this.file, { offered: Object.fromEntries(this.offered) });
2090
- } catch (err) {
2091
- this.onWriteError?.(err);
2092
- }
2093
- }
2094
- read() {
2095
- const raw = readJsonFile(this.file);
2096
- const out = /* @__PURE__ */ new Map();
2097
- if (!raw || typeof raw !== "object") return out;
2098
- const offered = raw.offered;
2099
- if (!offered || typeof offered !== "object" || Array.isArray(offered)) return out;
2100
- for (const [id, at] of Object.entries(offered)) {
2101
- if (typeof at === "string" && id.length > 0) out.set(id, at);
2102
- }
2103
- this.offered = out;
2104
- this.prune();
2105
- return this.offered;
2106
- }
2107
- prune() {
2108
- const cutoff = this.now() - this.maxAgeMs;
2109
- for (const [id, at] of this.offered) {
2110
- const t = Date.parse(at);
2111
- if (!Number.isFinite(t) || t < cutoff) this.offered.delete(id);
2112
- }
2113
- }
2114
- };
2115
2610
 
2116
2611
  // src/integration/socket.ts
2117
2612
  function wakeSourceOf(reason) {
2118
- return reason === "reminder" ? "reminder" : "other";
2613
+ if (reason === "reminder") return "reminder";
2614
+ if (reason === "unanswered") return "unanswered";
2615
+ return "other";
2119
2616
  }
2120
2617
  function realtimeOptionsFor(config, extra = {}) {
2121
2618
  return {
@@ -2123,6 +2620,10 @@ function realtimeOptionsFor(config, extra = {}) {
2123
2620
  baseUrl: config.apiBase.replace(/^http/, "ws").replace(/\/+$/, ""),
2124
2621
  clientIdentity: AGENTCHAT_CLIENT_IDENTITY,
2125
2622
  ackOnReceipt: true,
2623
+ // `rewake`: the server re-wakes this socket while cards stay unanswered
2624
+ // (contract v1, D3) — the integration keeps no retry ladder of its own.
2625
+ // `heartbeat` is offered by default (the idle watchdog's proof of life).
2626
+ capabilities: { rewake: true },
2126
2627
  reconnect: true,
2127
2628
  reconnectInterval: config.reconnect.initialBackoffMs,
2128
2629
  maxReconnectInterval: config.reconnect.maxBackoffMs,
@@ -2131,169 +2632,118 @@ function realtimeOptionsFor(config, extra = {}) {
2131
2632
  };
2132
2633
  }
2133
2634
  var IntegrationSocket = class {
2134
- config;
2635
+ /** The SDK client: handed to the receive loop, which connects and disconnects it. */
2636
+ realtime;
2135
2637
  logger;
2136
2638
  handlers;
2137
2639
  metrics;
2138
- client;
2139
- createRealtime;
2140
- webSocket;
2141
2640
  now;
2142
- selfHandle;
2143
- realtime = null;
2144
- /** The SDK's state as last reported through its `state` event; `stopped` is set here only for a stop before any start. */
2641
+ /** The SDK's state as last reported through its `state` event. */
2145
2642
  state = "idle";
2146
2643
  connectedAt = null;
2644
+ bound = false;
2147
2645
  stopping = false;
2148
- frames = 0;
2149
- selfFiltered = 0;
2150
- wakes = 0;
2151
- invites = 0;
2646
+ unbind = [];
2647
+ wakeFrames = 0;
2152
2648
  disconnects = 0;
2153
2649
  constructor(options) {
2154
- this.config = options.config;
2155
2650
  this.logger = options.logger.child({ component: "socket" });
2156
2651
  this.handlers = options.handlers ?? {};
2157
2652
  this.metrics = options.metrics ?? createNoopMetrics();
2158
- this.client = options.client;
2159
- this.createRealtime = options.createRealtime ?? ((opts) => new RealtimeClient(opts));
2160
- this.webSocket = options.webSocket;
2161
2653
  this.now = options.now ?? Date.now;
2162
- this.selfHandle = this.config.agentHandle?.replace(/^@/, "").toLowerCase();
2654
+ const create = options.createRealtime ?? ((opts) => new RealtimeClient(opts));
2655
+ this.realtime = create(realtimeOptionsFor(options.config, { client: options.client, webSocket: options.webSocket }));
2163
2656
  this.metrics.setConnectionState(this.state);
2164
2657
  }
2165
- /**
2166
- * Open the socket. Idempotent. Resolves once the SDK has started its
2167
- * connect loop; a failure to resolve a WebSocket implementation is logged
2168
- * and left to the SDK's own retry. Never throws.
2169
- */
2170
- async start() {
2171
- if (this.realtime || this.stopping) return;
2172
- const realtime = this.createRealtime(realtimeOptionsFor(this.config, { client: this.client, webSocket: this.webSocket }));
2173
- this.realtime = realtime;
2174
- this.bind(realtime);
2175
- try {
2176
- await realtime.connect();
2177
- } catch (err) {
2178
- this.logger.warn({ err: describe3(err) }, "socket connect failed \u2014 the SDK retries on its own schedule");
2179
- }
2658
+ /** Attach the lifecycle handlers. Idempotent. The receive loop connects the client. */
2659
+ bind() {
2660
+ if (this.bound || this.stopping) return;
2661
+ this.bound = true;
2662
+ const realtime = this.realtime;
2663
+ this.unbind.push(
2664
+ realtime.on("state", (next, prev) => this.onState(next, prev)),
2665
+ realtime.onConnect(() => {
2666
+ const at = this.now();
2667
+ this.connectedAt = at;
2668
+ this.logger.info({ at: new Date(at).toISOString() }, "connected (HELLO accepted)");
2669
+ this.metrics.incWake({ source: "connect" });
2670
+ this.guard("onConnected", () => this.handlers.onConnected?.(at));
2671
+ }),
2672
+ realtime.onDisconnect((info) => {
2673
+ if (this.stopping) return;
2674
+ this.disconnects += 1;
2675
+ this.connectedAt = null;
2676
+ this.metrics.incDisconnect({ code: String(info.code), initiator: info.initiator });
2677
+ this.logger.info({ code: info.code, reason: info.reason, initiator: info.initiator }, "socket closed \u2014 the SDK decides what follows (backoff or blocked)");
2678
+ this.guard("onDisconnected", () => this.handlers.onDisconnected?.(info));
2679
+ }),
2680
+ realtime.on("blocked", (info) => {
2681
+ if (this.stopping) return;
2682
+ this.metrics.incError({ errorClass: "auth" });
2683
+ this.logger.warn(
2684
+ { code: info.code, reason: info.reason, retryInMs: info.retryInMs },
2685
+ "the server rejected the key during the handshake \u2014 the SDK retries a fresh HELLO on its own"
2686
+ );
2687
+ this.guard("onBlocked", () => this.handlers.onBlocked?.(info));
2688
+ }),
2689
+ realtime.onError((error) => {
2690
+ if (this.stopping) return;
2691
+ if (realtime.state === "blocked") {
2692
+ this.logger.debug({ err: error.message }, "SDK notice while blocked");
2693
+ return;
2694
+ }
2695
+ this.metrics.incError({ errorClass: classifyRealtimeError(error) });
2696
+ this.logger.debug({ err: error.message }, "socket error");
2697
+ this.guard("onError", () => this.handlers.onError?.(error));
2698
+ }),
2699
+ // The loop turns a wake frame into a cycle; here it is only labelled
2700
+ // by its reason for the log line and the metric.
2701
+ realtime.on("wake", (frame) => {
2702
+ this.wakeFrames += 1;
2703
+ const payload = frame.payload ?? {};
2704
+ const conversations = Array.isArray(payload.conversation_ids) ? payload.conversation_ids.length : void 0;
2705
+ this.metrics.incWake({ source: wakeSourceOf(payload.reason) });
2706
+ this.logger.info(
2707
+ {
2708
+ reason: payload.reason,
2709
+ step: payload.step,
2710
+ ...typeof payload.conversation_id === "string" ? { conversationId: payload.conversation_id } : {},
2711
+ ...conversations !== void 0 ? { conversations } : {}
2712
+ },
2713
+ "wake \u2014 server wake frame"
2714
+ );
2715
+ })
2716
+ );
2180
2717
  }
2181
- /** Close the socket for good; a stopped socket does not reconnect. Idempotent. */
2182
- async stop() {
2718
+ /** The loop has disconnected the client (or never started it): report `stopped`, detach. Idempotent. */
2719
+ stopped() {
2183
2720
  this.stopping = true;
2184
- const realtime = this.realtime;
2185
- this.realtime = null;
2186
2721
  this.connectedAt = null;
2187
- if (realtime) {
2722
+ for (const off of this.unbind.splice(0)) {
2188
2723
  try {
2189
- realtime.disconnect();
2190
- } catch (err) {
2191
- this.logger.warn({ err: describe3(err) }, "socket disconnect threw \u2014 continuing");
2724
+ off();
2725
+ } catch {
2192
2726
  }
2193
2727
  }
2194
2728
  if (this.state !== "stopped") this.onState("stopped", this.state);
2195
2729
  }
2196
2730
  getHealth() {
2197
- return {
2198
- state: this.state,
2199
- connectedAt: this.connectedAt,
2200
- counters: { frames: this.frames, selfFiltered: this.selfFiltered, wakes: this.wakes, invites: this.invites, disconnects: this.disconnects }
2201
- };
2202
- }
2203
- // ─── Internals ─────────────────────────────────────────────────────
2204
- bind(realtime) {
2205
- realtime.on("state", (next, prev) => this.onState(next, prev));
2206
- realtime.onConnect(() => {
2207
- const at = this.now();
2208
- this.connectedAt = at;
2209
- this.logger.info({ at: new Date(at).toISOString() }, "connected (HELLO accepted)");
2210
- this.guard("onConnected", () => this.handlers.onConnected?.(at));
2211
- this.wake("connect", {});
2212
- });
2213
- realtime.onDisconnect((info) => {
2214
- if (this.stopping) return;
2215
- this.disconnects += 1;
2216
- this.connectedAt = null;
2217
- this.metrics.incDisconnect({ code: String(info.code), initiator: info.initiator });
2218
- this.logger.info({ code: info.code, reason: info.reason, initiator: info.initiator }, "socket closed \u2014 the SDK decides what follows (backoff or blocked)");
2219
- this.guard("onDisconnected", () => this.handlers.onDisconnected?.(info));
2220
- });
2221
- realtime.on("blocked", (info) => {
2222
- if (this.stopping) return;
2223
- this.metrics.incError({ errorClass: "auth" });
2224
- this.logger.warn(
2225
- { code: info.code, reason: info.reason, retryInMs: info.retryInMs },
2226
- "the server rejected the key during the handshake \u2014 the SDK retries a fresh HELLO on its own"
2227
- );
2228
- this.guard("onBlocked", () => this.handlers.onBlocked?.(info));
2229
- });
2230
- realtime.onError((error) => {
2231
- if (this.stopping) return;
2232
- if (realtime.state === "blocked") {
2233
- this.logger.debug({ err: error.message }, "SDK notice while blocked");
2234
- return;
2235
- }
2236
- this.metrics.incError({ errorClass: classifyRealtimeError(error) });
2237
- this.logger.debug({ err: error.message }, "socket error");
2238
- this.guard("onError", () => this.handlers.onError?.(error));
2239
- });
2240
- realtime.on("message.new", (message) => this.onMessageNew(message));
2241
- realtime.on("wake", (frame) => this.onWakeFrame(frame));
2242
- realtime.on("group.invite.received", (message) => this.onInvite(message));
2243
- }
2244
- onMessageNew(message) {
2245
- this.frames += 1;
2246
- const payload = message.payload ?? {};
2247
- const senderRaw = payload.sender ?? payload.from ?? "";
2248
- const sender = typeof senderRaw === "string" ? senderRaw.replace(/^@/, "").toLowerCase() : "";
2249
- const detail = {
2250
- ...typeof payload.conversation_id === "string" ? { conversationId: payload.conversation_id } : {},
2251
- ...typeof payload.id === "string" ? { messageId: payload.id } : {},
2252
- ...sender ? { from: sender } : {}
2253
- };
2254
- if (this.selfHandle && sender === this.selfHandle) {
2255
- this.selfFiltered += 1;
2256
- this.logger.debug(detail, "own echo \u2014 not a wake");
2257
- return;
2258
- }
2259
- this.logger.debug(detail, "wake \u2014 message.new");
2260
- this.wake("message", detail);
2261
- }
2262
- onWakeFrame(frame) {
2263
- this.frames += 1;
2264
- const reason = frame.payload?.reason;
2265
- const detail = { conversationId: frame.payload.conversation_id };
2266
- this.logger.info({ ...detail, reason, step: frame.payload.step }, "wake \u2014 server wake frame");
2267
- this.wake(wakeSourceOf(reason), detail);
2268
- }
2269
- onInvite(message) {
2270
- this.frames += 1;
2271
- const invite = groupInviteFromRecord(message.payload);
2272
- if (!invite) {
2273
- this.logger.warn({ keys: Object.keys(message.payload ?? {}).sort() }, "group.invite.received without ids \u2014 dropped");
2274
- return;
2275
- }
2276
- this.invites += 1;
2277
- this.guard("onGroupInvite", () => this.handlers.onGroupInvite?.(invite));
2278
- }
2279
- wake(source, detail) {
2280
- this.wakes += 1;
2281
- this.metrics.incWake({ source });
2282
- this.guard("onWake", () => this.handlers.onWake?.(source, detail));
2731
+ return { state: this.state, connectedAt: this.connectedAt, counters: { wakeFrames: this.wakeFrames, disconnects: this.disconnects } };
2283
2732
  }
2284
2733
  /** The SDK's state transition, mirrored for the health probe and the gauge, then handed on. */
2285
2734
  onState(next, prev) {
2735
+ if (this.state === next) return;
2286
2736
  this.state = next;
2287
2737
  this.metrics.setConnectionState(next);
2288
2738
  this.logger.debug({ from: prev, to: next }, "socket state");
2289
2739
  this.guard("onStateChanged", () => this.handlers.onStateChanged?.(next, prev));
2290
2740
  }
2291
- /** A failing handler never takes the socket down; the next frame wakes again and the tray keeps the cards. */
2741
+ /** A failing handler never takes the socket down. */
2292
2742
  guard(name, fn) {
2293
2743
  try {
2294
2744
  fn();
2295
2745
  } catch (err) {
2296
- this.logger.error({ err: describe3(err), handler: name }, "handler threw \u2014 continuing");
2746
+ this.logger.error({ err: describe4(err), handler: name }, "handler threw \u2014 continuing");
2297
2747
  }
2298
2748
  }
2299
2749
  };
@@ -2304,7 +2754,7 @@ function classifyRealtimeError(error) {
2304
2754
  if (error.name === "ConnectionError" || /websocket|hello|handshake|liveness|reconnecting|sync drain|sync ack/i.test(message)) return "transport";
2305
2755
  return "other";
2306
2756
  }
2307
- function describe3(err) {
2757
+ function describe4(err) {
2308
2758
  return err instanceof Error ? err.message : String(err);
2309
2759
  }
2310
2760
 
@@ -2334,8 +2784,6 @@ function registerIntegration(params) {
2334
2784
  }
2335
2785
  let built = null;
2336
2786
  const handlers = {
2337
- onWake: (source, detail) => built?.handlers.onWake?.(source, detail),
2338
- onGroupInvite: (invite) => built?.handlers.onGroupInvite?.(invite),
2339
2787
  onConnected: (at) => built?.handlers.onConnected?.(at),
2340
2788
  onDisconnected: (info) => built?.handlers.onDisconnected?.(info),
2341
2789
  onBlocked: (info) => built?.handlers.onBlocked?.(info),
@@ -2347,19 +2795,20 @@ function registerIntegration(params) {
2347
2795
  built = params.build(socket);
2348
2796
  const entry = {
2349
2797
  socket,
2350
- invoker: built.invoker,
2798
+ loop: built.loop,
2351
2799
  texts: built.texts ?? null,
2800
+ textsCache: built.textsCache ?? null,
2352
2801
  metrics: params.socketOptions?.metrics ?? null,
2353
2802
  config: params.config,
2354
2803
  logger: params.logger
2355
2804
  };
2356
2805
  registry.set(params.accountId, entry);
2806
+ socket.bind();
2357
2807
  try {
2358
- await socket.start();
2808
+ await built.loop?.loop.start();
2359
2809
  } catch (err) {
2360
2810
  registry.delete(params.accountId);
2361
- built.invoker?.stop();
2362
- built.texts?.stop();
2811
+ await stopEntry(entry);
2363
2812
  throw err;
2364
2813
  }
2365
2814
  return entry;
@@ -2374,557 +2823,272 @@ function unregisterIntegration(accountId) {
2374
2823
  });
2375
2824
  }
2376
2825
  async function stopEntry(entry) {
2377
- entry.invoker?.stop();
2378
- entry.texts?.stop();
2379
2826
  try {
2380
- await entry.socket.stop();
2827
+ await entry.loop?.stop();
2381
2828
  } catch (err) {
2382
- entry.logger.error({ err: err instanceof Error ? err.message : String(err) }, "socket.stop threw during unregister");
2829
+ entry.logger.error({ err: err instanceof Error ? err.message : String(err) }, "receive loop stop threw during unregister");
2383
2830
  }
2384
- }
2385
- function getIntegration(accountId) {
2386
- return registry.get(accountId);
2387
- }
2388
-
2389
- // src/integration/wake.ts
2390
- var DEFAULT_DEBOUNCE_MS = 2e3;
2391
- var DEFAULT_MAX_WINDOW_MS = 6e3;
2392
- var WakeCoalescer = class {
2393
- onFire;
2394
- onDelayedWake;
2395
- debounceMs;
2396
- maxWindowMs;
2397
- now;
2398
- _setTimeout;
2399
- _clearTimeout;
2400
- timer = null;
2401
- burstStartedAt = null;
2402
- delayed = null;
2403
- delayedDeadline = null;
2404
- stopped = false;
2405
- wakes = 0;
2406
- absorbed = 0;
2407
- fires = 0;
2408
- constructor(options) {
2409
- const debounce = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
2410
- if (!(debounce >= 0)) throw new RangeError("debounceMs must be >= 0");
2411
- const maxWindow = options.maxWindowMs ?? DEFAULT_MAX_WINDOW_MS;
2412
- if (!(maxWindow >= debounce)) throw new RangeError("maxWindowMs must be >= debounceMs");
2413
- this.onFire = options.onFire;
2414
- this.onDelayedWake = options.onDelayedWake;
2415
- this.debounceMs = debounce;
2416
- this.maxWindowMs = maxWindow;
2417
- this.now = options.now ?? Date.now;
2418
- this._setTimeout = options.setTimeout ?? ((fn, ms) => setTimeout(fn, ms));
2419
- this._clearTimeout = options.clearTimeout ?? ((h) => clearTimeout(h));
2420
- }
2421
- /** True while a debounce timer is armed. */
2422
- get armed() {
2423
- return this.timer !== null;
2424
- }
2425
- /** True while a retry wake is scheduled and has not fired. */
2426
- get delayedPending() {
2427
- return this.delayed !== null;
2428
- }
2429
- /**
2430
- * A wake. The first one of a burst arms the window; every later one is
2431
- * absorbed and restarts it (trailing edge), never past the burst's cap.
2432
- */
2433
- wake() {
2434
- if (this.stopped) return;
2435
- this.wakes += 1;
2436
- const now = this.now();
2437
- if (this.timer === null) {
2438
- this.burstStartedAt = now;
2439
- this.timer = this._setTimeout(() => this.fire(), this.debounceMs);
2440
- return;
2441
- }
2442
- this.absorbed += 1;
2443
- const capAt = (this.burstStartedAt ?? now) + this.maxWindowMs;
2444
- const delay = Math.max(0, Math.min(this.debounceMs, capAt - now));
2445
- this._clearTimeout(this.timer);
2446
- this.timer = this._setTimeout(() => this.fire(), delay);
2447
- }
2448
- /** A delayed wake (retry ladder). The sooner pending deadline is kept. */
2449
- wakeAfter(delayMs) {
2450
- if (this.stopped) return;
2451
- const delay = Math.max(0, delayMs);
2452
- const deadline = this.now() + delay;
2453
- if (this.delayed !== null) {
2454
- if (this.delayedDeadline !== null && this.delayedDeadline <= deadline) {
2455
- return;
2456
- }
2457
- this._clearTimeout(this.delayed);
2458
- }
2459
- this.delayedDeadline = deadline;
2460
- this.delayed = this._setTimeout(() => {
2461
- this.delayed = null;
2462
- this.delayedDeadline = null;
2463
- if (this.stopped) return;
2464
- try {
2465
- this.onDelayedWake?.();
2466
- } catch {
2467
- }
2468
- this.wake();
2469
- }, delay);
2470
- }
2471
- /** Cancel everything; later wakes are ignored. */
2472
- stop() {
2473
- this.stopped = true;
2474
- if (this.timer !== null) {
2475
- this._clearTimeout(this.timer);
2476
- this.timer = null;
2477
- this.burstStartedAt = null;
2478
- }
2479
- if (this.delayed !== null) {
2480
- this._clearTimeout(this.delayed);
2481
- this.delayed = null;
2482
- this.delayedDeadline = null;
2483
- }
2484
- }
2485
- fire() {
2486
- this.timer = null;
2487
- this.burstStartedAt = null;
2488
- if (this.stopped) return;
2489
- this.fires += 1;
2831
+ if (!entry.loop) {
2490
2832
  try {
2491
- this.onFire();
2833
+ entry.socket.realtime.disconnect();
2492
2834
  } catch {
2493
2835
  }
2494
2836
  }
2495
- };
2837
+ entry.socket.stopped();
2838
+ entry.textsCache?.stop();
2839
+ }
2840
+ function getIntegration(accountId) {
2841
+ return registry.get(accountId);
2842
+ }
2843
+ function listActiveAccounts() {
2844
+ return [...registry.keys()];
2845
+ }
2496
2846
 
2497
- // src/integration/invoker.ts
2498
- var RETRY_LADDER_MS = [6e4, 3e5, 18e5];
2499
- var AgentInvoker = class {
2500
- accountId;
2501
- agentId;
2502
- client;
2503
- callModel;
2504
- startTurn;
2505
- texts;
2506
- invites;
2507
- logger;
2508
- metrics;
2509
- readiness;
2510
- gateEnabled;
2511
- ladder;
2512
- now;
2513
- coalescer;
2514
- cycleDue = false;
2515
- running = null;
2516
- stopped = false;
2517
- pendingInvites = [];
2518
- invitesDirty = false;
2519
- retryRung = 0;
2520
- turnsInFlight = 0;
2521
- /**
2522
- * Conversations whose reply turn is running. Their verdict is recorded
2523
- * only when the turn completes, so the card stays on the tray meanwhile
2524
- * and every wake in between would re-gate it and queue a second turn.
2525
- * Such cards are left out of the cycle; once the turn ends the tray is
2526
- * read again if any were.
2527
- */
2528
- replyTurnsInFlight = /* @__PURE__ */ new Set();
2529
- skippedWhileInFlight = false;
2530
- /** The reason the tools were last found unavailable; logged when it changes, not per wake. */
2531
- unreadyReason = null;
2532
- cycles = 0;
2533
- turns = 0;
2534
- inviteTurns = 0;
2535
- constructor(options) {
2536
- this.accountId = options.accountId;
2537
- this.agentId = options.agentId;
2538
- this.client = options.client;
2539
- this.callModel = options.callModel;
2540
- this.startTurn = options.startTurn;
2541
- this.texts = options.texts;
2542
- this.invites = options.invites;
2543
- this.logger = options.logger.child({ component: "integration", accountId: options.accountId });
2544
- this.metrics = options.metrics ?? createNoopMetrics();
2545
- this.readiness = options.readiness ?? (() => ({ ready: true }));
2546
- this.gateEnabled = options.gateEnabled ?? true;
2547
- this.ladder = options.retryLadderMs ?? RETRY_LADDER_MS;
2548
- this.now = options.now ?? Date.now;
2549
- this.coalescer = new WakeCoalescer({
2550
- onFire: () => this.onWakeFired(),
2551
- onDelayedWake: () => {
2552
- this.logger.debug({ source: "retry" }, "wake");
2553
- this.metrics.incWake({ source: "retry" });
2554
- },
2555
- debounceMs: options.debounceMs ?? DEFAULT_DEBOUNCE_MS,
2556
- now: this.now,
2557
- ...options.setTimeout ? { setTimeout: options.setTimeout } : {},
2558
- ...options.clearTimeout ? { clearTimeout: options.clearTimeout } : {}
2559
- });
2560
- }
2561
- // ─── wakes ──────────────────────────────────────────────────────────
2562
- /** A wake from the socket (a frame, a connect, a reminder) or any other source. Coalesced, then cycled. */
2563
- wake(source) {
2564
- if (this.stopped) return;
2565
- this.logger.debug({ source }, "wake");
2566
- this.coalescer.wake();
2567
- }
2568
- /**
2569
- * The second wake kind. An invite already offered (this process or an
2570
- * earlier one, per the ledger) or already pending is ignored, so an
2571
- * invite seen live and again on a connect-time catch-up is decided once.
2572
- * Returns true when newly queued.
2573
- */
2574
- groupInvite(invite, source) {
2575
- if (this.stopped) return false;
2576
- if (this.invites.isOffered(invite.inviteId)) return false;
2577
- if (this.pendingInvites.some((pending) => pending.inviteId === invite.inviteId)) return false;
2578
- this.logger.info(
2579
- { groupId: invite.groupId, groupName: invite.groupName, inviter: invite.inviterHandle, inviteId: invite.inviteId, source },
2580
- "group invite \u2014 queuing one invite turn"
2581
- );
2582
- this.pendingInvites.push(invite);
2583
- this.invitesDirty = true;
2584
- this.schedule();
2585
- return true;
2586
- }
2587
- /** Cancel timers; a cycle in flight finishes on its own. */
2588
- stop() {
2589
- this.stopped = true;
2590
- this.coalescer.stop();
2591
- }
2592
- /** The promise of the work currently running, for tests and for graceful stops. */
2593
- settled() {
2594
- return this.running ?? Promise.resolve();
2595
- }
2596
- counters() {
2597
- return {
2598
- cycles: this.cycles,
2599
- turns: this.turns,
2600
- inviteTurns: this.inviteTurns,
2601
- turnsInFlight: this.turnsInFlight,
2602
- wakes: this.coalescer.wakes,
2603
- absorbed: this.coalescer.absorbed,
2604
- retryRung: this.retryRung,
2605
- pendingInvites: this.pendingInvites.length
2606
- };
2607
- }
2608
- // ─── worker ─────────────────────────────────────────────────────────
2609
- onWakeFired() {
2610
- this.cycleDue = true;
2611
- if (this.pendingInvites.length > 0) this.invitesDirty = true;
2612
- this.schedule();
2613
- }
2614
- /** Run the worker if idle. One cycle at a time; work that lands mid-cycle runs afterwards. */
2615
- schedule() {
2616
- if (this.running) return;
2617
- this.running = this.drain().finally(() => {
2618
- this.running = null;
2619
- if (!this.stopped && (this.cycleDue || this.invitesDirty)) this.schedule();
2620
- });
2621
- }
2622
- async drain() {
2623
- while (!this.stopped) {
2624
- if (this.invitesDirty) {
2625
- this.invitesDirty = false;
2626
- await this.runInviteTurns();
2627
- continue;
2628
- }
2629
- if (this.cycleDue) {
2630
- this.cycleDue = false;
2631
- try {
2632
- await this.runCycle();
2633
- } catch (err) {
2634
- this.logger.error({ err: describe4(err), ...this.retry() }, "cycle failed with an unhandled error \u2014 re-waking later");
2635
- }
2636
- continue;
2637
- }
2638
- return;
2639
- }
2640
- }
2641
- // ─── one cycle ──────────────────────────────────────────────────────
2642
- /** Read the tray, gate it, start a turn per reply. Public so tests can drive one cycle. */
2643
- async runCycle() {
2644
- this.cycles += 1;
2645
- const started = this.now();
2646
- let tray;
2647
- try {
2648
- tray = await this.client.inbox.tray({ waitMs: 0 });
2649
- } catch (err) {
2650
- this.logger.warn({ err: describe4(err), ...this.retry() }, "tray read failed \u2014 re-waking later");
2651
- return this.result("tray_failed", 0, false, started, describe4(err));
2652
- }
2653
- const allCards = gate.cardsOf(tray);
2654
- const truncated = tray.truncated === true;
2655
- if (allCards.length === 0) {
2656
- this.logger.info({ deferred: tray.deferred ?? 0 }, "tray empty \u2014 nothing to decide");
2657
- this.retryRung = 0;
2658
- return this.result("empty", 0, truncated, started);
2659
- }
2660
- const cards = allCards.filter((card) => !this.replyTurnsInFlight.has(card.conversation_id));
2661
- const skippedInFlight = allCards.length - cards.length;
2662
- if (skippedInFlight > 0) this.skippedWhileInFlight = true;
2663
- if (cards.length === 0) {
2664
- this.logger.info({ skippedInFlight }, "every card on the tray has a reply turn running \u2014 nothing new to decide");
2665
- return { ...this.result("empty", 0, truncated, started), skippedInFlight };
2666
- }
2667
- const texts = await this.texts.ensure();
2668
- if (!texts) {
2669
- this.logger.warn({ cards: cards.length, ...this.retry() }, "served texts not available \u2014 not gating; the cards wait on the tray");
2670
- return { ...this.result("texts_missing", cards.length, truncated, started, "texts missing"), skippedInFlight };
2671
- }
2672
- const readiness = this.readiness();
2673
- if (!readiness.ready) {
2674
- const changed = this.unreadyReason !== readiness.reason;
2675
- this.unreadyReason = readiness.reason;
2676
- this.logger[changed ? "warn" : "debug"]({ cards: cards.length, reason: readiness.reason, ...this.retry() }, "AgentChat tools not available to a turn \u2014 not gating; the cards wait on the tray");
2677
- return { ...this.result("tools_unavailable", cards.length, truncated, started, readiness.reason), skippedInFlight };
2678
- }
2679
- if (this.unreadyReason !== null) {
2680
- this.unreadyReason = null;
2681
- this.logger.info({}, "AgentChat tools available to turns again");
2682
- }
2683
- let result;
2684
- if (!this.gateEnabled) {
2685
- result = await this.runUngated(cards.length, texts, truncated, started);
2686
- } else {
2687
- const trayToGate = skippedInFlight > 0 ? { ...tray, conversations: cards } : tray;
2688
- result = await this.runGated(trayToGate, cards.length, texts, truncated, started, skippedInFlight);
2689
- }
2690
- if (skippedInFlight > 0) result = { ...result, skippedInFlight };
2691
- if (truncated && result.outcome !== "fail_closed") {
2692
- this.logger.info({}, "tray truncated \u2014 waking again for the rest");
2693
- this.coalescer.wake();
2694
- }
2695
- return result;
2696
- }
2697
- /** Gate off: the agent judges in-turn with the hosted tools. One turn, nothing recorded here. */
2698
- async runUngated(cards, texts, truncated, started) {
2699
- this.logger.info({ cards }, "gate off \u2014 starting one turn with the served wake instruction");
2700
- const sessionKey = wakeSessionKey(this.agentId, this.now());
2701
- this.logger.info({ sessionKey }, "turn start");
2702
- const admission = await this.startTurn({ sessionKey, prompt: wakeTurnPrompt(texts), lane: integrationLane(this.accountId) });
2847
+ // src/integration/turns.ts
2848
+ function createLoopTurnStarter(options) {
2849
+ const now = options.now ?? Date.now;
2850
+ return async (request) => {
2851
+ const id = request.kind === "reply" ? request.conversationId : request.invite.invite_id;
2852
+ const sessionKey = request.kind === "reply" ? wakeSessionKey(options.agentId, now()) : inviteSessionKey(options.agentId, id, now());
2853
+ const watch = options.turns.admission(request.kind, id);
2854
+ const admission = await options.startRun({ sessionKey, prompt: request.prompt, lane: integrationLane(options.accountId, options.turns.laneGeneration) });
2703
2855
  if (!admission.ran) {
2704
- this.logger.warn({ sessionKey, error: admission.error, ...this.retry() }, "turn not started \u2014 re-waking later");
2705
- } else {
2706
- this.admitted(sessionKey, admission, "turn complete (final text discarded; the send is a hosted-tool call)");
2707
- this.retryRung = 0;
2708
- }
2709
- return {
2710
- ...this.result("gate_off", cards, truncated, started),
2711
- turnsStarted: admission.ran ? 1 : 0,
2712
- turnsRefused: admission.ran ? 0 : 1
2713
- };
2714
- }
2715
- async runGated(tray, cards, texts, truncated, started, skippedInFlight = 0) {
2716
- const decided = await gate.run({ tray, callModel: this.callModel, client: this.client, toolNamer: OPENCLAW_TOOL_NAMER });
2717
- const latencyMs = this.now() - started;
2718
- const counts = { reply: 0, dismiss: 0, defer: 0 };
2719
- for (const verdict of decided.verdicts) counts[verdict.decision] += 1;
2720
- if (decided.outcome === "fail_closed") {
2721
- this.logger.warn({ cards, failure: decided.failure, latencyMs, ...this.retry() }, "gate failed closed \u2014 nothing recorded, no turn started");
2722
- return { ...this.result("fail_closed", cards, truncated, started, decided.failure), undecided: cards, latencyMs };
2723
- }
2724
- if (decided.outcome === "empty") {
2725
- this.retryRung = 0;
2726
- return this.result("empty", cards, truncated, started);
2727
- }
2728
- let turnsStarted = 0;
2729
- let turnsRefused = 0;
2730
- const recordFailures = decided.recordFailures.length;
2731
- const recorded = decided.recorded;
2732
- for (const entry of decided.reply) {
2733
- if (this.replyTurnsInFlight.has(entry.conversationId)) {
2734
- this.skippedWhileInFlight = true;
2735
- continue;
2736
- }
2737
- const sessionKey = wakeSessionKey(this.agentId, this.now());
2738
- this.logger.info({ sessionKey, conversationId: entry.conversationId }, "turn start");
2739
- const admission = await this.startTurn({ sessionKey, prompt: wakeTurnPrompt(texts, entry.conversationId), lane: integrationLane(this.accountId) });
2740
- if (!admission.ran) {
2741
- turnsRefused += 1;
2742
- this.logger.warn({ sessionKey, conversationId: entry.conversationId, error: admission.error }, "turn not started \u2014 reply not recorded; the conversation stays unread");
2743
- continue;
2856
+ options.turns.admitted(watch);
2857
+ return { started: false, error: admission.error };
2858
+ }
2859
+ options.turns.started({
2860
+ runId: admission.runId,
2861
+ kind: request.kind,
2862
+ id,
2863
+ anchor: request.kind === "reply" ? request.anchor : null,
2864
+ sessionKey: admission.sessionKey,
2865
+ startedAt: now(),
2866
+ owner: options.owner,
2867
+ // What the after-turn rule compares when the turn ends outside the
2868
+ // kit's watch (handed off, or after a restart).
2869
+ ...request.kind === "reply" ? { card: request.card, category: request.category } : { invite: request.invite }
2870
+ }, { admission: watch });
2871
+ options.logger.info(
2872
+ { kind: request.kind, sessionKey, runId: admission.runId, ...request.kind === "reply" ? { conversationId: id } : { inviteId: id } },
2873
+ request.kind === "reply" ? "turn start" : "group-invite turn start"
2874
+ );
2875
+ const done = admission.done.then((outcome) => {
2876
+ if (outcome.status === "gone") {
2877
+ options.logger.warn({ sessionKey, runId: outcome.runId }, "turn end not confirmed: the plugin runtime is gone \u2014 its card stays held");
2878
+ return { status: "still_running", detail: "the plugin runtime is gone" };
2744
2879
  }
2745
- turnsStarted += 1;
2746
- this.replyTurnsInFlight.add(entry.conversationId);
2747
- this.admitted(sessionKey, admission, "turn complete (final text discarded; the send is a hosted-tool call)", entry);
2748
- }
2749
- const summary = {
2750
- cards,
2751
- reply: counts.reply,
2752
- dismiss: counts.dismiss,
2753
- defer: counts.defer,
2754
- undecided: decided.undecided.length,
2755
- recorded,
2756
- recordFailures,
2757
- turnsStarted,
2758
- turnsRefused,
2759
- skippedInFlight,
2760
- latencyMs
2761
- };
2762
- if (turnsRefused > 0) {
2763
- this.logger.warn({ ...summary, ...this.retry() }, "gate \u2014 some turns were refused; re-waking later");
2764
- } else if (recordFailures > 0 || decided.undecided.length > 0) {
2765
- this.logger.warn({ ...summary, ...this.retry() }, "gate \u2014 cards left on the tray; re-waking later");
2766
- } else {
2767
- this.retryRung = 0;
2768
- this.logger.info(summary, "gate");
2880
+ const end = turnEndOf(outcome);
2881
+ void options.turns.ended(admission.runId, end);
2882
+ options.logger[outcome.status === "ok" ? "info" : "warn"](
2883
+ {
2884
+ sessionKey,
2885
+ runId: outcome.runId,
2886
+ status: outcome.status,
2887
+ ...outcome.error ? { error: outcome.error } : {},
2888
+ ...outcome.elapsedMs !== void 0 ? { elapsedMs: outcome.elapsedMs } : {},
2889
+ agentchatCalls: outcome.agentchatCalls ?? null
2890
+ },
2891
+ request.kind === "reply" ? "turn ended (final text discarded; the send is a hosted-tool call)" : "group-invite turn ended"
2892
+ );
2893
+ return end;
2894
+ });
2895
+ return { started: true, done };
2896
+ };
2897
+ }
2898
+ function turnEndOf(outcome) {
2899
+ const agentchatCalls = outcome.agentchatCalls ?? null;
2900
+ if (outcome.status === "ok" && !outcome.error) return { status: "completed", agentchatCalls };
2901
+ return { status: "failed", agentchatCalls, detail: outcome.error ?? `run ended: ${outcome.status ?? "unknown"}` };
2902
+ }
2903
+
2904
+ // src/integration/loop.ts
2905
+ var generation = 0;
2906
+ function createIntegrationLoop(options) {
2907
+ const logger = options.logger.child({ component: "integration", accountId: options.accountId });
2908
+ const metrics = options.metrics ?? createNoopMetrics();
2909
+ const now = options.now ?? Date.now;
2910
+ generation += 1;
2911
+ const owner = `${options.accountId}#${generation}`;
2912
+ const gateEnabled = options.gateEnabled ?? true;
2913
+ let unreadyReason = null;
2914
+ const readiness = options.readiness ? () => {
2915
+ const ready = options.readiness();
2916
+ if (ready.ready) {
2917
+ if (unreadyReason !== null) logger.info({}, "turns can start again \u2014 gating resumes");
2918
+ unreadyReason = null;
2919
+ return ready;
2920
+ }
2921
+ if (unreadyReason !== ready.reason) logger.warn({ reason: ready.reason }, "not gating \u2014 no turn can start now; the cards wait on the tray");
2922
+ unreadyReason = ready.reason;
2923
+ return ready;
2924
+ } : void 0;
2925
+ const startTurn = createLoopTurnStarter({ accountId: options.accountId, agentId: options.agentId, startRun: options.startRun, turns: options.turns, owner, logger, now });
2926
+ const loop = new integration.ReceiveLoop({
2927
+ client: options.client,
2928
+ texts: options.texts,
2929
+ mode: gateEnabled ? "gateCall" : "singleRun",
2930
+ ...gateEnabled ? { callModel: options.callModel, startTurn } : { startRun: singleRunStarter(options, owner, logger, now) },
2931
+ socket: options.socket,
2932
+ ...options.handle ? { handle: options.handle } : {},
2933
+ toolNamer: OPENCLAW_TOOL_NAMER,
2934
+ // One account's turns run on one OpenClaw lane anyway; one slot keeps
2935
+ // the rest held out of the gate (no second model call decides them).
2936
+ maxConcurrentTurns: 1,
2937
+ tracker: options.turns.tracker,
2938
+ ...readiness ? { readiness } : {},
2939
+ ...options.paceMs !== void 0 ? { paceMs: options.paceMs } : {},
2940
+ ...options.quietMs !== void 0 ? { quietMs: options.quietMs } : {},
2941
+ ...options.maxMs !== void 0 ? { maxMs: options.maxMs } : {},
2942
+ now,
2943
+ ...options.setTimeout ? { setTimeout: options.setTimeout } : {},
2944
+ ...options.clearTimeout ? { clearTimeout: options.clearTimeout } : {},
2945
+ onEvent: (event) => logEvent(event, logger, metrics)
2946
+ });
2947
+ const inLoopContext = AsyncLocalStorage.snapshot();
2948
+ options.turns.setLive(
2949
+ owner,
2950
+ () => inLoopContext(() => loop.wake()),
2951
+ (fn) => inLoopContext(fn)
2952
+ );
2953
+ return {
2954
+ loop,
2955
+ async stop() {
2956
+ options.turns.clearLive(owner);
2957
+ await loop.stop();
2769
2958
  }
2770
- return { ...this.result("decided", cards, truncated, started), ...summary };
2771
- }
2772
- result(outcome, cards, truncated, started, failure = null) {
2773
- return {
2774
- outcome,
2775
- cards,
2776
- reply: 0,
2777
- dismiss: 0,
2778
- defer: 0,
2779
- undecided: 0,
2780
- recorded: 0,
2781
- recordFailures: 0,
2782
- turnsStarted: 0,
2783
- turnsRefused: 0,
2784
- skippedInFlight: 0,
2785
- truncated,
2786
- latencyMs: this.now() - started,
2787
- failure
2959
+ };
2960
+ }
2961
+ function singleRunStarter(options, owner, logger, now) {
2962
+ return async (request) => {
2963
+ const hung = options.turns.hungSingleRun();
2964
+ if (hung) {
2965
+ logger.warn({ runId: hung.runId }, "gate off \u2014 a handed-off run is still going; no second run while it lives (the cards wait on the tray)");
2966
+ return { status: "not_started", detail: `the gate-off run ${hung.runId} is still going` };
2967
+ }
2968
+ const sessionKey = wakeSessionKey(options.agentId, now());
2969
+ logger.info({ sessionKey, cards: request.cards, invites: request.invites }, "gate off \u2014 one run with the served wake instruction");
2970
+ const admission = await options.startRun({ sessionKey, prompt: request.prompt, lane: integrationLane(options.accountId, options.turns.laneGeneration) });
2971
+ if (!admission.ran) return { status: "not_started", detail: admission.error };
2972
+ let letGo = () => {
2788
2973
  };
2789
- }
2790
- /** Arm the next rung (or report why not). Returned as log fields so every caller writes ONE line. */
2791
- retry() {
2792
- if (this.coalescer.delayedPending) {
2793
- return { retry: { kind: "pending" } };
2794
- }
2795
- if (this.retryRung >= this.ladder.length) {
2796
- return { retry: { kind: "exhausted" } };
2974
+ const handedOff = new Promise((resolve) => {
2975
+ letGo = () => resolve("let_go");
2976
+ });
2977
+ const { runId } = admission;
2978
+ options.turns.started({ runId, kind: "run", id: runId, anchor: null, sessionKey: admission.sessionKey, startedAt: now(), owner }, { letGo });
2979
+ const ended = admission.done.then((outcome) => {
2980
+ if (outcome.status !== "gone") void options.turns.ended(runId);
2981
+ logger[outcome.status === "ok" ? "info" : "warn"]({ sessionKey, runId, status: outcome.status, agentchatCalls: outcome.agentchatCalls ?? null }, "gate off \u2014 run ended");
2982
+ return outcome;
2983
+ });
2984
+ const first = await Promise.race([ended, handedOff]);
2985
+ if (first === "let_go") {
2986
+ logger.warn({ sessionKey, runId }, "gate off \u2014 the run is still going after 30 min; no new run starts until it ends (or `openclaw agentchat release <run id> --force`)");
2987
+ return { status: "failed", detail: "the run was handed off while still going" };
2797
2988
  }
2798
- const delayMs = this.ladder[this.retryRung] ?? this.ladder[this.ladder.length - 1] ?? 6e4;
2799
- this.retryRung += 1;
2800
- this.coalescer.wakeAfter(delayMs);
2801
- return { retry: { kind: "armed", delayMs, rung: this.retryRung } };
2802
- }
2803
- // ─── turns ──────────────────────────────────────────────────────────
2804
- /**
2805
- * Book an admitted turn and log its end whenever it comes. The cycle moves
2806
- * on at once; OpenClaw's lane keeps the runs of one account in order.
2807
- *
2808
- * For a reply turn the end is also when the `reply` verdict is recorded —
2809
- * only if the run completed (`status: ok`, no error): a turn that looked
2810
- * and chose not to send still counts as read. A run that ended in error,
2811
- * on the wait timeout, or whose wait failed records nothing: the card stays
2812
- * on the tray and the ladder re-wakes so the conversation is gated again.
2813
- */
2814
- admitted(sessionKey, admission, completeLine, reply) {
2815
- this.turns += 1;
2816
- this.turnsInFlight += 1;
2817
- void admission.done.then(
2818
- async (outcome) => {
2819
- this.turnsInFlight -= 1;
2820
- const completed = outcome.ran && outcome.status === "ok" && !outcome.error;
2821
- this.logger[completed ? "info" : "warn"](
2822
- { sessionKey, runId: outcome.runId, status: outcome.status, ...outcome.error ? { error: outcome.error } : {} },
2823
- completeLine
2824
- );
2825
- if (!reply) return;
2826
- try {
2827
- if (!completed) {
2828
- this.logger.warn(
2829
- { sessionKey, conversationId: reply.conversationId, status: outcome.status, ...outcome.error ? { error: outcome.error } : {}, ...this.retry() },
2830
- "turn did not complete \u2014 reply not recorded; the conversation stays unread and is gated again"
2831
- );
2832
- return;
2833
- }
2834
- await this.recordReply(sessionKey, reply);
2835
- } finally {
2836
- this.replyTurnEnded(reply.conversationId);
2837
- }
2838
- },
2839
- async (err) => {
2840
- this.turnsInFlight -= 1;
2841
- this.logger.warn({ sessionKey, runId: admission.runId, err: describe4(err) }, `${completeLine} \u2014 the wait failed`);
2842
- if (reply) {
2843
- this.logger.warn(
2844
- { sessionKey, conversationId: reply.conversationId, err: describe4(err), ...this.retry() },
2845
- "turn did not complete \u2014 reply not recorded; the conversation stays unread and is gated again"
2846
- );
2847
- this.replyTurnEnded(reply.conversationId);
2989
+ return first.status === "ok" && !first.error ? { status: "completed" } : { status: "failed", detail: first.error ?? `run ended: ${first.status ?? "unknown"}` };
2990
+ };
2991
+ }
2992
+ function logEvent(event, logger, metrics) {
2993
+ switch (event.type) {
2994
+ case "wake":
2995
+ if (event.source === "message") metrics.incWake({ source: "message" });
2996
+ else if (event.source === "invite") metrics.incWake({ source: "invite" });
2997
+ logger.debug({ source: event.source, ...event.conversationId ? { conversationId: event.conversationId } : {} }, "wake");
2998
+ return;
2999
+ case "echo":
3000
+ logger.debug(event.conversationId ? { conversationId: event.conversationId } : {}, "own echo \u2014 not a wake");
3001
+ return;
3002
+ case "cycle": {
3003
+ const s = event.summary;
3004
+ const fields = {
3005
+ outcome: s.outcome,
3006
+ cards: s.cards,
3007
+ invites: s.invites,
3008
+ reply: s.reply,
3009
+ dismiss: s.dismiss,
3010
+ defer: s.defer,
3011
+ undecided: s.undecided,
3012
+ recordFailures: s.recordFailures,
3013
+ turnsStarted: s.turnsStarted,
3014
+ turnsRefused: s.turnsRefused,
3015
+ heldByTurns: s.heldByTurns,
3016
+ ...s.truncated ? { truncated: true } : {},
3017
+ ...s.failure ? { failure: s.failure } : {}
3018
+ };
3019
+ switch (s.outcome) {
3020
+ case "empty":
3021
+ logger.info({}, "tray empty \u2014 nothing to decide");
3022
+ return;
3023
+ case "nothing_owed":
3024
+ logger.info(fields, "nothing new to decide (muted cards, or cards a running turn holds)");
3025
+ return;
3026
+ case "fail_closed":
3027
+ logger.warn(fields, "gate failed closed \u2014 nothing recorded, no turn started; the server re-wakes");
3028
+ return;
3029
+ case "tray_failed":
3030
+ logger.warn(fields, "tray read failed \u2014 the cards stay on the tray; the server re-wakes");
3031
+ return;
3032
+ case "texts_missing":
3033
+ logger.warn(fields, "served texts not available \u2014 not gating; the cards wait on the tray");
3034
+ return;
3035
+ case "tools_unavailable":
3036
+ logger.debug(fields, "not gating \u2014 no turn can start now");
3037
+ return;
3038
+ case "abandoned":
3039
+ logger.warn(fields, "cycle abandoned \u2014 it ran past its deadline; nothing more is done for it, the next wake starts a new one");
3040
+ return;
3041
+ default: {
3042
+ const incomplete = s.turnsRefused > 0 || s.recordFailures > 0 || s.undecided > 0;
3043
+ logger[incomplete ? "warn" : "info"](fields, incomplete ? "gate \u2014 some cards were left on the tray; the server re-wakes" : "gate");
3044
+ return;
2848
3045
  }
2849
3046
  }
2850
- );
2851
- }
2852
- /** The reply turn for `conversationId` is over: its card may be gated again, and a tray that held skipped cards is read once more. */
2853
- replyTurnEnded(conversationId) {
2854
- this.replyTurnsInFlight.delete(conversationId);
2855
- if (this.skippedWhileInFlight && !this.stopped) {
2856
- this.skippedWhileInFlight = false;
2857
- this.logger.debug({ conversationId }, "reply turn ended \u2014 re-reading the tray for the cards left out meanwhile");
2858
- this.coalescer.wake();
2859
3047
  }
2860
- }
2861
- /** The `reply` verdict for a completed turn. A record the server refuses leaves the conversation unread; the ladder re-wakes. */
2862
- async recordReply(sessionKey, reply) {
2863
- try {
2864
- await reply.record();
2865
- this.logger.info({ sessionKey, conversationId: reply.conversationId }, "reply verdict recorded \u2014 turn completed");
2866
- } catch (err) {
2867
- this.logger.warn(
2868
- { sessionKey, conversationId: reply.conversationId, err: describe4(err), ...this.retry() },
2869
- "reply verdict not recorded after the turn completed \u2014 the conversation stays unread; the next wake gates it again"
3048
+ case "turn_started":
3049
+ return;
3050
+ // the turn starter logs it with the OpenClaw session and run
3051
+ case "turn_refused":
3052
+ logger.warn({ kind: event.kind, id: event.id, error: event.error }, "turn not started \u2014 nothing recorded; the card stays on the tray");
3053
+ return;
3054
+ case "turn_outcome": {
3055
+ const o = event.outcome;
3056
+ logger.info(
3057
+ { conversationId: o.conversationId, action: o.action, reason: o.reason, recorded: event.recorded, ...o.action !== "none" ? { through: o.throughMessageId } : {} },
3058
+ event.recorded ? `${o.action === "record_reply" ? "reply" : "dismiss"} recorded after the turn (${o.reason})` : `nothing recorded after the turn (${o.reason})`
2870
3059
  );
3060
+ return;
2871
3061
  }
3062
+ case "invite_outcome":
3063
+ logger.info(
3064
+ { inviteId: event.outcome.inviteId, action: event.outcome.action, reason: event.outcome.reason, recorded: event.recorded },
3065
+ event.recorded ? "invite dismissed after a second silent turn (left pending)" : `nothing recorded for the invite (${event.outcome.reason})`
3066
+ );
3067
+ return;
3068
+ case "run":
3069
+ return;
3070
+ // single-run mode logs the run itself
3071
+ case "error":
3072
+ logger.debug({ err: event.error.message }, "receive loop notice");
3073
+ return;
2872
3074
  }
2873
- /**
2874
- * Every pending invite gets one attempt. An invite leaves the pending list
2875
- * only once its turn was admitted (and is then remembered in the ledger);
2876
- * a refused one stays there for the next wake. Invites that arrive while
2877
- * this runs are appended and get their pass on the next drain.
2878
- */
2879
- async runInviteTurns() {
2880
- const batch = [...this.pendingInvites];
2881
- if (batch.length > 0) {
2882
- const readiness = this.readiness();
2883
- if (!readiness.ready) {
2884
- const changed = this.unreadyReason !== readiness.reason;
2885
- this.unreadyReason = readiness.reason;
2886
- this.logger[changed ? "warn" : "debug"]({ pending: batch.length, reason: readiness.reason, ...this.retry() }, "AgentChat tools not available to a turn \u2014 invite turns wait; re-waking later");
2887
- return;
2888
- }
2889
- }
2890
- let texts = null;
2891
- let refused = 0;
2892
- let waitingForTexts = 0;
2893
- for (const invite of batch) {
2894
- if (this.stopped) return;
2895
- texts ??= await this.texts.ensure();
2896
- if (!texts) {
2897
- waitingForTexts = batch.length;
2898
- break;
2899
- }
2900
- const sessionKey = inviteSessionKey(this.agentId, invite.inviteId);
2901
- this.logger.info({ sessionKey, groupId: invite.groupId, inviteId: invite.inviteId }, "group-invite turn start");
2902
- let admission;
2903
- try {
2904
- admission = await this.startTurn({ sessionKey, prompt: inviteTurnPrompt(texts), lane: integrationLane(this.accountId) });
2905
- } catch (err) {
2906
- admission = { ran: false, error: describe4(err) };
2907
- }
2908
- if (!admission.ran) {
2909
- refused += 1;
2910
- this.logger.warn({ sessionKey, inviteId: invite.inviteId, error: admission.error }, "group-invite turn not started \u2014 the invite stays pending; retried on the next wake");
2911
- continue;
2912
- }
2913
- this.inviteTurns += 1;
2914
- this.invites.markOffered(invite.inviteId);
2915
- this.pendingInvites = this.pendingInvites.filter((pending) => pending.inviteId !== invite.inviteId);
2916
- this.admitted(sessionKey, admission, "group-invite turn complete");
2917
- }
2918
- if (this.stopped) return;
2919
- if (waitingForTexts > 0) {
2920
- this.logger.warn({ pending: waitingForTexts, ...this.retry() }, "served texts not available \u2014 invite turns wait; re-waking later");
2921
- } else if (refused > 0) {
2922
- this.logger.warn({ pending: refused, ...this.retry() }, "invite turns refused \u2014 re-waking later");
2923
- }
2924
- }
3075
+ }
3076
+ var TROUBLE_OUTCOMES = /* @__PURE__ */ new Set(["fail_closed", "tools_unavailable", "texts_missing", "abandoned"]);
3077
+ var CYCLE_TROUBLE_RUN = 3;
3078
+ var TROUBLE_MEANING = {
3079
+ fail_closed: "the gate call failed (model unreachable, too slow, or nothing usable)",
3080
+ tools_unavailable: "no turn could have the hosted tools",
3081
+ texts_missing: "the served texts are not available",
3082
+ abandoned: "the cycle ran past its deadline and was abandoned"
2925
3083
  };
2926
- function describe4(err) {
2927
- return err instanceof Error ? err.message : String(err);
3084
+ function cycleTroubleLine(recentCycles) {
3085
+ let run = 0;
3086
+ for (let i = recentCycles.length - 1; i >= 0 && TROUBLE_OUTCOMES.has(recentCycles[i].outcome); i -= 1) run += 1;
3087
+ if (run < CYCLE_TROUBLE_RUN) return null;
3088
+ const last = recentCycles[recentCycles.length - 1];
3089
+ const kinds = [...new Set(recentCycles.slice(-run).map((c) => c.outcome))];
3090
+ const meaning = kinds.map((k) => TROUBLE_MEANING[k] ?? k).join("; ");
3091
+ return `the last ${run} cycles decided nothing (${kinds.join(", ")}): ${meaning}${last.failure ? ` \u2014 latest: ${last.failure}` : ""}; the cards wait on the tray and AgentChat re-wakes them`;
2928
3092
  }
2929
3093
  function adaptLog(log) {
2930
3094
  if (!log) return void 0;
@@ -2982,7 +3146,7 @@ var agentchatGatewayAdapter = {
2982
3146
  }
2983
3147
  if (readLegacyReplyGateFailOpenFromEnv()) {
2984
3148
  ctx.log?.warn?.(
2985
- `[agentchat:${ctx.accountId}] ${LEGACY_REPLY_GATE_FAIL_OPEN_ENV} is set but no longer honoured \u2014 a gate that cannot run fails closed (nothing recorded, no turn) and re-wakes on the ladder`
3149
+ `[agentchat:${ctx.accountId}] ${LEGACY_REPLY_GATE_FAIL_OPEN_ENV} is set but no longer honoured \u2014 a gate that cannot run fails closed (nothing recorded, no turn); the server re-wakes the cards`
2986
3150
  );
2987
3151
  }
2988
3152
  const refusal = multiAccountRefusal(ctx.cfg);
@@ -3004,20 +3168,57 @@ var agentchatGatewayAdapter = {
3004
3168
  }
3005
3169
  const client = getClient({ accountId: ctx.accountId, config });
3006
3170
  const gateTimeoutMs = readReplyGateTimeoutMsFromEnv();
3007
- const texts = new TextsStore({ client, accountId: ctx.accountId, logger });
3171
+ const overdueWarned = /* @__PURE__ */ new Map();
3172
+ const turns = accountTurns(ctx.accountId);
3173
+ const reportOverdue = (runId, kind, id, elapsedMs, handed) => {
3174
+ const mark = Math.floor((elapsedMs + OVERDUE_SLACK_MS) / TURN_OVERDUE_MS);
3175
+ if (!handed && overdueWarned.get(runId) === mark) return;
3176
+ overdueWarned.set(runId, mark);
3177
+ ctx.log?.warn?.(`[agentchat:${ctx.accountId}] ${overdueTurnLine({ kind, id, elapsedMs }, readLiveConfig(ctx.cfg).cfg)}`);
3178
+ };
3179
+ const onStillRunning = ({ runId, sessionKey, elapsedMs }) => {
3180
+ const run = turns.run(runId);
3181
+ if (turns.stateOf(runId) === "released") {
3182
+ logger.debug({ runId, sessionKey, elapsedMin: Math.round(elapsedMs / 6e4) }, "released turn still running in OpenClaw \u2014 it holds nothing");
3183
+ return;
3184
+ }
3185
+ logger.info(
3186
+ { runId, sessionKey, elapsedMin: Math.round(elapsedMs / 6e4), ...run ? { [run.kind === "reply" ? "conversationId" : run.kind === "invite" ? "inviteId" : "run"]: run.id } : {} },
3187
+ "turn still running \u2014 its conversation stays in flight until OpenClaw reports the run ended"
3188
+ );
3189
+ if (!run || !isOverdue(elapsedMs)) return;
3190
+ const handed = turns.handOff(runId) !== null;
3191
+ reportOverdue(runId, run.kind, run.id, elapsedMs, handed);
3192
+ };
3193
+ const watchRun = createRunWatcher({ onStillRunning });
3194
+ turns.configure({
3195
+ recorder: createAfterTurnRecorder({ client, logger }),
3196
+ store: new TurnStore(ctx.accountId),
3197
+ watch: async (record) => {
3198
+ const outcome = await watchRun(record);
3199
+ return outcome ? turnEndOf(outcome) : null;
3200
+ },
3201
+ readCalls: (sessionKey, runId) => readAgentChatCalls(getPluginRuntime(), sessionKey, runId),
3202
+ logger
3203
+ });
3204
+ turns.adopt();
3205
+ for (const line of restartHoldLines(turns)) ctx.log?.warn?.(`[agentchat:${ctx.accountId}] ${line}`);
3206
+ for (const file of removeOldStateFiles(ctx.accountId)) logger.info({ file }, "removed a file an earlier release kept (no longer used)");
3207
+ const textsCache = createTextsCache({ client, accountId: ctx.accountId, logger });
3208
+ const texts = new OpenClawTexts(textsCache);
3209
+ const first = await textsCache.start();
3210
+ if (first) logger.info({ version: first.version, manual: first.manual?.version, source: textsCache.source }, "served texts in hand");
3211
+ else logger.warn({ err: textsCache.lastError }, "served texts not available yet (AgentChat unreachable, nothing cached) \u2014 nothing is gated until the first copy arrives");
3008
3212
  const handle = config.agentHandle;
3009
3213
  if (handle) {
3010
- texts.onServerCopy((copy) => {
3011
- const refreshed = refreshIdentityLine(ctx.cfg, ctx.accountId, handle, copy.identity_line);
3214
+ const refresh = (copy) => {
3215
+ const refreshed = refreshIdentityLine(ctx.cfg, ctx.accountId, handle, spellTextsForOpenClaw(copy).identity_line);
3012
3216
  if (refreshed.outcome === "updated") ctx.log?.info?.(`[agentchat:${ctx.accountId}] identity line refreshed from the served texts in ${refreshed.path}`);
3013
3217
  else if (refreshed.outcome === "failed") ctx.log?.warn?.(`[agentchat:${ctx.accountId}] identity line could not be refreshed in ${refreshed.path}: ${refreshed.detail ?? "unknown error"}`);
3014
- });
3218
+ };
3219
+ if (first) refresh(first);
3220
+ textsCache.onChange((copy) => refresh(copy));
3015
3221
  }
3016
- await texts.start();
3017
- const invites = new InviteLedger({
3018
- accountId: ctx.accountId,
3019
- onWriteError: (err) => ctx.log?.warn?.(`[agentchat:${ctx.accountId}] invite ledger could not be written: ${err instanceof Error ? err.message : String(err)}`)
3020
- });
3021
3222
  const metrics = createInMemoryMetrics();
3022
3223
  await registerIntegration({
3023
3224
  accountId: ctx.accountId,
@@ -3025,33 +3226,44 @@ var agentchatGatewayAdapter = {
3025
3226
  logger,
3026
3227
  socketOptions: { client, metrics },
3027
3228
  build: (socket) => {
3028
- const invoker = new AgentInvoker({
3229
+ const loop = createIntegrationLoop({
3029
3230
  accountId: ctx.accountId,
3030
3231
  agentId,
3031
3232
  client,
3233
+ texts,
3234
+ socket: socket.realtime,
3235
+ handle: config.agentHandle,
3032
3236
  callModel: createRuntimeModelCall({ agentIdOverride, ...gateTimeoutMs !== void 0 ? { timeoutMs: gateTimeoutMs } : {} }),
3033
- startTurn: createRuntimeTurnStarter(),
3237
+ // A turn holds its conversation until OpenClaw confirms its run
3238
+ // ended; a wait that elapses is asked again (`integration/run.ts`).
3239
+ startRun: createRuntimeTurnStarter({
3240
+ onStillRunning,
3241
+ onWaitError: ({ runId, sessionKey, error, attempt, retryInMs }) => logger[attempt === 1 ? "warn" : "debug"]({ runId, sessionKey, error, attempt, retryInMs }, "waiting for the turn to end failed \u2014 asking OpenClaw again")
3242
+ }),
3034
3243
  // Gating waits until a turn can have the hosted tools (the MCP
3035
3244
  // entry in the LIVE config — OpenClaw hot-applies `mcp.*` — and
3036
3245
  // its key in this process): a connect wake on a cold gateway
3037
3246
  // otherwise spends a gate call and runs a turn that cannot look.
3038
3247
  readiness: () => {
3039
3248
  const live = readLiveConfig(ctx.cfg);
3249
+ const hung = hungTurnsReason(turns, live.cfg);
3250
+ if (hung) return { ready: false, reason: hung };
3040
3251
  return turnReadiness({ cfg: live.cfg, apiBase: config.apiBase, apiKey: config.apiKey, live: live.live });
3041
3252
  },
3042
- texts,
3043
- invites,
3253
+ // The account's runs outlive this loop: a config change restarts
3254
+ // the account while its runs go on in OpenClaw, so every loop for
3255
+ // the account shares the same turns (and the kit tracker in them).
3256
+ turns,
3044
3257
  logger,
3045
3258
  metrics,
3046
3259
  gateEnabled: readReplyGateEnabledFromEnv()
3047
3260
  });
3048
3261
  let blocked = false;
3049
3262
  return {
3050
- invoker,
3263
+ loop,
3051
3264
  texts,
3265
+ textsCache,
3052
3266
  handlers: {
3053
- onWake: (source) => invoker.wake(source),
3054
- onGroupInvite: (invite) => invoker.groupInvite(invite, "live"),
3055
3267
  onConnected: (at) => {
3056
3268
  if (blocked) {
3057
3269
  blocked = false;
@@ -3059,7 +3271,6 @@ var agentchatGatewayAdapter = {
3059
3271
  }
3060
3272
  ctx.log?.info?.(`[agentchat:${ctx.accountId}] connected at ${new Date(at).toISOString()} (agent ${agentId})`);
3061
3273
  ctx.setStatus(withLifecycle({ ...ctx.getStatus(), running: true, connected: true, linked: true, statusState: "linked", lastConnectedAt: at }, "ready"));
3062
- void catchUpInvites(ctx.accountId, client, invoker, logger);
3063
3274
  },
3064
3275
  onBlocked: (info) => {
3065
3276
  const close = `close ${info.code}${info.reason ? ` ${info.reason}` : ""}`;
@@ -3115,23 +3326,6 @@ var agentchatGatewayAdapter = {
3115
3326
  ctx.setStatus(withLifecycle({ ...ctx.getStatus(), running: false, connected: false }, "stopped"));
3116
3327
  }
3117
3328
  };
3118
- async function catchUpInvites(accountId, client, invoker, logger) {
3119
- try {
3120
- const invites = await client.listGroupInvites();
3121
- if (!Array.isArray(invites)) return;
3122
- let surfaced = 0;
3123
- for (const row of invites) {
3124
- const invite = groupInviteFromRecord(row);
3125
- if (invite && invoker.groupInvite(invite, "catch-up")) surfaced += 1;
3126
- }
3127
- if (surfaced > 0) logger.info({ accountId, surfaced }, "pending group invites surfaced on connect");
3128
- } catch (err) {
3129
- logger.warn(
3130
- { accountId, err: err instanceof Error ? err.message : String(err) },
3131
- "pending-invite catch-up failed \u2014 live invite frames are unaffected and the agent can still list invites with the hosted tools"
3132
- );
3133
- }
3134
- }
3135
3329
  var PROBE_MIN_MS = 1e3;
3136
3330
  var PROBE_MAX_MS = 1e4;
3137
3331
  function formatCounts(counts) {
@@ -3173,15 +3367,27 @@ var agentchatStatusAdapter = {
3173
3367
  const enabled = account.enabled && isAgentchatAccountEnabled(cfg, account.accountId);
3174
3368
  const live = getIntegration(account.accountId);
3175
3369
  const health = live?.socket.getHealth();
3176
- const counters = live?.invoker?.counters();
3177
- const textsSource = live ? live.texts?.source ?? "missing" : void 0;
3370
+ const loopStatus = live?.loop?.loop.status();
3371
+ const counters = loopStatus ? {
3372
+ wakes: loopStatus.coalescer.wakes,
3373
+ cycles: loopStatus.coalescer.cycles,
3374
+ turns: loopStatus.counters.turnsStarted + loopStatus.counters.inviteTurnsStarted + loopStatus.counters.runsStarted
3375
+ } : void 0;
3376
+ const turnsHere = live ? findAccountTurns(account.accountId) ?? null : null;
3377
+ const hungReason = turnsHere ? hungTurnsReason(turnsHere, cfg) : null;
3378
+ const overdue = turnsHere ? [...hungReason ? [hungReason] : [], ...overdueTurnLines(turnsHere.runningRuns(), cfg), ...restartHoldLines(turnsHere)] : [];
3379
+ const trouble = loopStatus ? cycleTroubleLine(loopStatus.recentCycles ?? []) : null;
3380
+ const servedFrom = live?.texts?.source;
3381
+ const textsSource = live ? servedFrom === "server" || servedFrom === "cache" ? servedFrom : "missing" : void 0;
3178
3382
  const socketCounters = live?.metrics?.snapshot?.();
3179
- const integration = {
3383
+ const integration6 = {
3180
3384
  mode: readWakeModeFromEnv().mode,
3181
3385
  ...health ? { state: health.state } : {},
3182
3386
  ...counters ? { wakes: counters.wakes, cycles: counters.cycles, turns: counters.turns } : {},
3183
3387
  ...textsSource ? { texts: textsSource } : {},
3184
- ...socketCounters ? { socket: socketCounters } : {}
3388
+ ...socketCounters ? { socket: socketCounters } : {},
3389
+ ...overdue.length > 0 ? { overdue } : {},
3390
+ ...trouble ? { trouble } : {}
3185
3391
  };
3186
3392
  const refusal = multiAccountRefusal(cfg);
3187
3393
  return {
@@ -3191,7 +3397,7 @@ var agentchatStatusAdapter = {
3191
3397
  pausedByOwner: me.paused_by_owner ?? "none",
3192
3398
  mcp,
3193
3399
  placement,
3194
- integration,
3400
+ integration: integration6,
3195
3401
  ...refusal ? { refusal } : {},
3196
3402
  ...enabled ? {} : { disabled: agentchatReenableHint(cfg, account.accountId) }
3197
3403
  };
@@ -3229,6 +3435,8 @@ var agentchatStatusAdapter = {
3229
3435
  tone: "muted"
3230
3436
  });
3231
3437
  }
3438
+ if (d.trouble) lines.push({ text: d.trouble, tone: "warn" });
3439
+ for (const line of d.overdue ?? []) lines.push({ text: line, tone: "warn" });
3232
3440
  if (d.texts === "missing") {
3233
3441
  lines.push({ text: "served texts not available yet (AgentChat unreachable, nothing cached) \u2014 turns wait for the first copy", tone: "error" });
3234
3442
  } else if (d.texts === "cache") {
@@ -3272,6 +3480,18 @@ var agentchatDoctorAdapter = {
3272
3480
  );
3273
3481
  }
3274
3482
  }
3483
+ for (const accountId of listActiveAccounts()) {
3484
+ const status = getIntegration(accountId)?.loop?.loop.status();
3485
+ if (!status) continue;
3486
+ const turns = findAccountTurns(accountId);
3487
+ if (turns) {
3488
+ const hung = hungTurnsReason(turns, cfg);
3489
+ if (hung) warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] ${hung}`);
3490
+ for (const line of [...overdueTurnLines(turns.runningRuns(), cfg), ...restartHoldLines(turns)]) warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] ${line}`);
3491
+ }
3492
+ const trouble = cycleTroubleLine(status.recentCycles ?? []);
3493
+ if (trouble) warnings.push(`[${AGENTCHAT_CHANNEL_ID}:${accountId}] ${trouble}`);
3494
+ }
3275
3495
  return warnings;
3276
3496
  },
3277
3497
  async repairConfig({ cfg }) {
@@ -3566,9 +3786,9 @@ var agentchatPlugin = {
3566
3786
  listAccountIds(cfg) {
3567
3787
  const section = readChannelSection(cfg);
3568
3788
  if (!section) return [];
3569
- const { accounts } = section;
3570
- if (accounts && typeof accounts === "object" && !Array.isArray(accounts)) {
3571
- const ids = Object.keys(accounts);
3789
+ const { accounts: accounts2 } = section;
3790
+ if (accounts2 && typeof accounts2 === "object" && !Array.isArray(accounts2)) {
3791
+ const ids = Object.keys(accounts2);
3572
3792
  if (ids.length > 0) return ids;
3573
3793
  }
3574
3794
  const hasFlatFields = typeof section.apiKey === "string" || typeof section.apiBase === "string" || typeof section.agentHandle === "string";
@@ -3608,9 +3828,9 @@ var agentchatPlugin = {
3608
3828
  hasConfiguredState({ cfg }) {
3609
3829
  const section = readChannelSection(cfg);
3610
3830
  if (!section) return false;
3611
- const { accounts } = section;
3612
- if (accounts && typeof accounts === "object" && !Array.isArray(accounts)) {
3613
- for (const entry of Object.values(accounts)) {
3831
+ const { accounts: accounts2 } = section;
3832
+ if (accounts2 && typeof accounts2 === "object" && !Array.isArray(accounts2)) {
3833
+ for (const entry of Object.values(accounts2)) {
3614
3834
  if (entry && typeof entry === "object") {
3615
3835
  const candidate = entry.apiKey;
3616
3836
  if (isApiKeyPresent(candidate)) return true;
@@ -3654,7 +3874,15 @@ defineChannelPluginEntry({
3654
3874
  // (docs.openclaw.ai/plugins/sdk-entrypoints/define-channel-plugin-entry).
3655
3875
  // The integration reads `runtime.llm.complete` (the gate on the agent's own
3656
3876
  // model) and `runtime.subagent.run` (one turn, `deliver: false`) from it.
3657
- setRuntime: (runtime) => setPluginRuntime(runtime)
3877
+ setRuntime: (runtime) => setPluginRuntime(runtime),
3878
+ // `openclaw agentchat release <conversation_id>`: the CLI half is
3879
+ // registered for every CLI (parse-time metadata), the gateway half only
3880
+ // where the integration runs.
3881
+ registerCliMetadata: (api) => registerReleaseCli(api),
3882
+ registerFull: (api) => {
3883
+ registerReleaseGatewayMethod(api);
3884
+ registerRunObserver(api);
3885
+ }
3658
3886
  });
3659
3887
 
3660
3888
  // src/channel.setup.ts