Skip to content

Commit be95bb7

Browse files
authored
refactor: consolidate core stream cleanup, watchdog locality, and approval text duplication (#99901)
* refactor(infra): share guarded body-stream cleanup across fetch consumers * refactor(agents): extract shared runtime model locality for llm watchdogs * refactor(auto-reply): share exec approval route resolution between command handlers * refactor(plugin-sdk): share approval reaction hint text helpers * refactor(plugin-sdk): share planned migration target resolution * refactor(infra): use canonical sleep helper in backup retry * docs(plugins): mention shared migration targets and reaction hint helpers * chore(plugin-sdk): pin surface budgets for reaction hint and migration target helpers
1 parent eafe2a8 commit be95bb7

16 files changed

Lines changed: 267 additions & 276 deletions

File tree

‎docs/plugins/sdk-subpaths.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ For the plugin authoring guide, see [Plugin SDK overview](/plugins/sdk-overview)
2828
| `plugin-sdk/config-schema` | `OpenClawSchema` |
2929
| `plugin-sdk/provider-entry` | `defineSingleProviderPluginEntry` |
3030
| `plugin-sdk/migration` | Migration provider item helpers such as `createMigrationItem`, reason constants, item status markers, redaction helpers, and `summarizeMigrationItems` |
31-
| `plugin-sdk/migration-runtime` | Runtime migration helpers such as `copyMigrationFileItem`, `withCachedMigrationConfigRuntime`, and `writeMigrationReport` |
31+
| `plugin-sdk/migration-runtime` | Runtime migration helpers such as `copyMigrationFileItem`, `resolvePlannedMigrationTargets`, `withCachedMigrationConfigRuntime`, and `writeMigrationReport` |
3232
| `plugin-sdk/health` | Doctor health-check registration, detection, repair, selection, severity, and finding types for bundled health consumers |
3333

3434
### Deprecated compatibility and test helpers
@@ -190,7 +190,7 @@ usage endpoint failed or returned no usable usage data.
190190
| `plugin-sdk/approval-handler-adapter-runtime` | Lightweight native approval adapter loading helpers for hot channel entrypoints |
191191
| `plugin-sdk/approval-handler-runtime` | Broader approval handler runtime helpers; prefer the narrower adapter/gateway seams when they are enough |
192192
| `plugin-sdk/approval-native-runtime` | Native approval target, account-binding, route-gate, forwarding fallback, and local native exec prompt suppression helpers |
193-
| `plugin-sdk/approval-reaction-runtime` | Hardcoded approval reaction bindings, reaction prompt payloads, reaction target stores, and compatibility export for local native exec prompt suppression |
193+
| `plugin-sdk/approval-reaction-runtime` | Hardcoded approval reaction bindings, reaction prompt payloads, reaction target stores, reaction hint text helpers, and compatibility export for local native exec prompt suppression |
194194
| `plugin-sdk/approval-reply-runtime` | Exec/plugin approval reply payload helpers |
195195
| `plugin-sdk/approval-runtime` | Exec/plugin approval payload helpers, native approval routing/runtime helpers, and structured approval display helpers such as `formatApprovalDisplayPath` |
196196
| `plugin-sdk/reply-dedupe` | Narrow inbound reply dedupe reset helpers |
@@ -241,7 +241,7 @@ usage endpoint failed or returned no usable usage data.
241241
| `plugin-sdk/runtime-config-snapshot` | Current process config snapshot helpers such as `getRuntimeConfig`, `getRuntimeConfigSnapshot`, and test snapshot setters |
242242
| `plugin-sdk/telegram-command-config` | Telegram command-name/description normalization and duplicate/conflict checks, even when the bundled Telegram contract surface is unavailable |
243243
| `plugin-sdk/text-autolink-runtime` | File-reference autolink detection without the broad text barrel |
244-
| `plugin-sdk/approval-reaction-runtime` | Hardcoded approval reaction bindings, reaction prompt payloads, reaction target stores, and compatibility export for local native exec prompt suppression |
244+
| `plugin-sdk/approval-reaction-runtime` | Hardcoded approval reaction bindings, reaction prompt payloads, reaction target stores, reaction hint text helpers, and compatibility export for local native exec prompt suppression |
245245
| `plugin-sdk/approval-runtime` | Exec/plugin approval helpers, approval-capability builders, auth/profile helpers, native routing/runtime helpers, and structured approval display path formatting |
246246
| `plugin-sdk/reply-runtime` | Shared inbound/reply runtime helpers, chunking, dispatch, heartbeat, reply planner |
247247
| `plugin-sdk/reply-dispatch-runtime` | Narrow reply dispatch/finalize and conversation-label helpers |

‎extensions/imessage/src/approval-reactions.ts‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Imessage plugin module implements approval reactions behavior.
22
import {
3+
addApprovalReactionHintToText,
34
buildApprovalReactionHint,
45
createApprovalReactionTargetStore,
6+
hasApprovalReactionHintText,
57
listApprovalReactionBindings,
68
resolveApprovalReactionTarget,
79
type ApprovalReactionDecisionBinding,
@@ -243,38 +245,15 @@ export function buildIMessageApprovalReactionHint(
243245
return buildApprovalReactionHint({ allowedDecisions });
244246
}
245247

246-
function insertIMessageApprovalReactionHintNearHeader(params: {
247-
text: string;
248-
hint: string;
249-
}): string {
250-
const lines = params.text.split(/\r?\n/);
251-
const idLineIndex = lines.findIndex((line) => /^ID:\s*\S+/.test(line.trim()));
252-
if (idLineIndex >= 0) {
253-
const before = lines.slice(0, idLineIndex + 1).join("\n");
254-
const after = lines
255-
.slice(idLineIndex + 1)
256-
.join("\n")
257-
.replace(/^\n+/, "");
258-
return after ? `${before}\n\n${params.hint}\n\n${after}` : `${before}\n\n${params.hint}`;
259-
}
260-
return `${params.hint}\n\n${params.text}`;
261-
}
262-
263248
export function addIMessageApprovalReactionHintToText(params: {
264249
text: string;
265250
allowedDecisions: readonly ExecApprovalReplyDecision[];
266251
}): string {
267-
if (/(^|\n)React with:\s*(\n|$)/i.test(params.text)) {
268-
return params.text;
269-
}
270-
const hint = buildIMessageApprovalReactionHint(params.allowedDecisions);
271-
return hint
272-
? insertIMessageApprovalReactionHintNearHeader({ text: params.text, hint })
273-
: params.text;
252+
return addApprovalReactionHintToText(params);
274253
}
275254

276255
export function appendIMessageApprovalReactionHintForOutboundMessage(text: string): string {
277-
if (/(^|\n)React with:\s*(\n|$)/i.test(text)) {
256+
if (hasApprovalReactionHintText(text)) {
278257
return text;
279258
}
280259
const binding = extractIMessageApprovalPromptBinding(text);
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
1-
// Migrate Claude plugin module implements targets behavior.
2-
import path from "node:path";
3-
import {
4-
resolveAgentConfig,
5-
resolveAgentWorkspaceDir,
6-
resolveDefaultAgentId,
7-
} from "openclaw/plugin-sdk/agent-runtime";
8-
import type { MigrationProviderContext } from "openclaw/plugin-sdk/plugin-entry";
9-
import { resolveHomePath } from "./helpers.js";
10-
11-
export type PlannedTargets = {
12-
workspaceDir: string;
13-
stateDir: string;
14-
agentDir: string;
15-
};
16-
17-
export function resolveTargets(ctx: MigrationProviderContext): PlannedTargets {
18-
const cfg = ctx.config;
19-
const agentId = resolveDefaultAgentId(cfg);
20-
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
21-
const configuredAgentDir = resolveAgentConfig(cfg, agentId)?.agentDir?.trim();
22-
const agentDir =
23-
ctx.runtime?.agent?.resolveAgentDir(cfg, agentId) ??
24-
(configuredAgentDir ? resolveHomePath(configuredAgentDir) : undefined) ??
25-
path.join(ctx.stateDir, "agents", agentId, "agent");
26-
return {
27-
workspaceDir,
28-
stateDir: ctx.stateDir,
29-
agentDir,
30-
};
31-
}
1+
// Migrate Claude plugin re-exports the shared migration target resolution.
2+
export {
3+
resolvePlannedMigrationTargets as resolveTargets,
4+
type PlannedMigrationTargets as PlannedTargets,
5+
} from "openclaw/plugin-sdk/migration-runtime";
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,5 @@
1-
// Migrate Hermes plugin module implements targets behavior.
2-
import path from "node:path";
3-
import {
4-
resolveAgentConfig,
5-
resolveAgentWorkspaceDir,
6-
resolveDefaultAgentId,
7-
} from "openclaw/plugin-sdk/agent-runtime";
8-
import type { MigrationProviderContext } from "openclaw/plugin-sdk/plugin-entry";
9-
import { resolveHomePath } from "./helpers.js";
10-
11-
export type PlannedTargets = {
12-
workspaceDir: string;
13-
stateDir: string;
14-
agentDir: string;
15-
};
16-
17-
export function resolveTargets(ctx: MigrationProviderContext): PlannedTargets {
18-
const cfg = ctx.config;
19-
const agentId = resolveDefaultAgentId(cfg);
20-
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
21-
const configuredAgentDir = resolveAgentConfig(cfg, agentId)?.agentDir?.trim();
22-
const agentDir =
23-
ctx.runtime?.agent?.resolveAgentDir(cfg, agentId) ??
24-
(configuredAgentDir ? resolveHomePath(configuredAgentDir) : undefined) ??
25-
path.join(ctx.stateDir, "agents", agentId, "agent");
26-
return {
27-
workspaceDir,
28-
stateDir: ctx.stateDir,
29-
agentDir,
30-
};
31-
}
1+
// Migrate Hermes plugin re-exports the shared migration target resolution.
2+
export {
3+
resolvePlannedMigrationTargets as resolveTargets,
4+
type PlannedMigrationTargets as PlannedTargets,
5+
} from "openclaw/plugin-sdk/migration-runtime";

‎extensions/signal/src/approval-reactions.ts‎

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Signal plugin module implements approval reactions behavior.
22
import { matchesApprovalRequestFilters } from "openclaw/plugin-sdk/approval-client-runtime";
33
import {
4+
addApprovalReactionHintToText,
45
buildApprovalReactionHint,
56
createApprovalReactionTargetStore,
7+
hasApprovalReactionHintText,
68
listApprovalReactionBindings,
79
resolveApprovalReactionTarget,
810
type ApprovalReactionDecisionBinding,
@@ -362,38 +364,11 @@ export function buildSignalApprovalReactionHint(
362364
return buildApprovalReactionHint({ allowedDecisions });
363365
}
364366

365-
function insertSignalApprovalReactionHintNearHeader(params: {
366-
text: string;
367-
hint: string;
368-
}): string {
369-
const lines = params.text.split(/\r?\n/);
370-
const idLineIndex = lines.findIndex((line) => /^ID:\s*\S+/.test(line.trim()));
371-
if (idLineIndex >= 0) {
372-
const before = lines.slice(0, idLineIndex + 1).join("\n");
373-
const after = lines
374-
.slice(idLineIndex + 1)
375-
.join("\n")
376-
.replace(/^\n+/, "");
377-
return after ? `${before}\n\n${params.hint}\n\n${after}` : `${before}\n\n${params.hint}`;
378-
}
379-
return `${params.hint}\n\n${params.text}`;
380-
}
381-
382367
export function addSignalApprovalReactionHintToText(params: {
383368
text: string;
384369
allowedDecisions: readonly ExecApprovalReplyDecision[];
385370
}): string {
386-
if (hasSignalApprovalReactionHintText(params.text)) {
387-
return params.text;
388-
}
389-
const hint = buildSignalApprovalReactionHint(params.allowedDecisions);
390-
return hint
391-
? insertSignalApprovalReactionHintNearHeader({ text: params.text, hint })
392-
: params.text;
393-
}
394-
395-
function hasSignalApprovalReactionHintText(text?: string | null): boolean {
396-
return /(^|\n)React with:\s*(\n|$)/i.test(text ?? "");
371+
return addApprovalReactionHintToText(params);
397372
}
398373

399374
function buildTargetRoute(params: {
@@ -571,8 +546,8 @@ function listDeliveredSignalMessageIdsWithVisibleHint(params: {
571546
const ids = candidates
572547
.filter((result) =>
573548
resultsWithVisibleText.length > 0
574-
? hasSignalApprovalReactionHintText(readSignalDeliveryVisibleText(result))
575-
: hasSignalApprovalReactionHintText(params.payload.text),
549+
? hasApprovalReactionHintText(readSignalDeliveryVisibleText(result))
550+
: hasApprovalReactionHintText(params.payload.text),
576551
)
577552
.map((result) => normalizeOptionalString(result.messageId))
578553
.filter((messageId): messageId is string => Boolean(messageId && messageId !== "unknown"));
@@ -595,7 +570,7 @@ export function registerSignalApprovalReactionTargetForDeliveredPayload(params:
595570
if (!metadata?.allowedDecisions || metadata.allowedDecisions.length === 0) {
596571
return false;
597572
}
598-
if (!hasSignalApprovalReactionHintText(params.payload.text)) {
573+
if (!hasApprovalReactionHintText(params.payload.text)) {
599574
return false;
600575
}
601576
if (

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ let publicDeprecatedExportsByEntrypointBudget;
202202
try {
203203
budgets = {
204204
publicEntrypoints: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_ENTRYPOINTS", 323),
205-
publicExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS", 10416),
206-
publicFunctionExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS", 5230),
205+
publicExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS", 10421),
206+
publicFunctionExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS", 5234),
207207
publicDeprecatedExports: readBudgetEnv(
208208
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_DEPRECATED_EXPORTS",
209209
3261,

‎src/agents/embedded-agent-runner/run/llm-idle-timeout.ts‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,43 @@ function isOllamaCloudModel(model: { id?: string; provider?: string } | undefine
198198
return bareModelId.endsWith(":cloud");
199199
}
200200

201+
type RuntimeModelLocality = {
202+
isLocalRuntimeModel: boolean;
203+
isExplicitLocalHostnameRuntimeModel: boolean;
204+
isSelfHostedHostnameRuntimeModel: boolean;
205+
};
206+
207+
/**
208+
* Classifies the model endpoint locality shared by the idle and first-event
209+
* watchdogs. Ollama `*:cloud` models stay "cloud" even behind a local proxy.
210+
*/
211+
function resolveRuntimeModelLocality(params?: {
212+
cfg?: OpenClawConfig;
213+
model?: { baseUrl?: string; id?: string; provider?: string };
214+
}): RuntimeModelLocality {
215+
const baseUrl = params?.model?.baseUrl;
216+
if (typeof baseUrl !== "string" || baseUrl.length === 0) {
217+
return {
218+
isLocalRuntimeModel: false,
219+
isExplicitLocalHostnameRuntimeModel: false,
220+
isSelfHostedHostnameRuntimeModel: false,
221+
};
222+
}
223+
const notCloudModel = !isOllamaCloudModel(params?.model);
224+
return {
225+
isLocalRuntimeModel: isLocalProviderBaseUrl(baseUrl) && notCloudModel,
226+
isExplicitLocalHostnameRuntimeModel: isExplicitLocalHostnameBaseUrl(baseUrl) && notCloudModel,
227+
isSelfHostedHostnameRuntimeModel:
228+
isBareProviderHostnameBaseUrl(baseUrl) &&
229+
(isSelfHostedProviderId(params?.model?.provider) ||
230+
hasConfiguredLocalProviderSignal({
231+
cfg: params?.cfg,
232+
provider: params?.model?.provider,
233+
})) &&
234+
notCloudModel,
235+
};
236+
}
237+
201238
/**
202239
* Resolves the stream-idle watchdog timeout for one embedded run. Explicit
203240
* provider request timeouts and bounded run/agent timeouts cap the watchdog;
@@ -220,22 +257,11 @@ export function resolveLlmIdleTimeoutMs(params?: {
220257
const hasExplicitRunTimeout =
221258
typeof runTimeoutMs === "number" && Number.isFinite(runTimeoutMs) && runTimeoutMs > 0;
222259
const runTimeoutIsNoTimeout = hasExplicitRunTimeout && runTimeoutMs >= MAX_TIMER_TIMEOUT_MS;
223-
const baseUrl = params?.model?.baseUrl;
224-
const isLocalProvider =
225-
typeof baseUrl === "string" && baseUrl.length > 0 && isLocalProviderBaseUrl(baseUrl);
226-
const isLocalRuntimeModel = isLocalProvider && !isOllamaCloudModel(params?.model);
227-
const isExplicitLocalHostnameRuntimeModel =
228-
typeof baseUrl === "string" &&
229-
baseUrl.length > 0 &&
230-
isExplicitLocalHostnameBaseUrl(baseUrl) &&
231-
!isOllamaCloudModel(params?.model);
232-
const isSelfHostedHostnameRuntimeModel =
233-
typeof baseUrl === "string" &&
234-
baseUrl.length > 0 &&
235-
isBareProviderHostnameBaseUrl(baseUrl) &&
236-
(isSelfHostedProviderId(params?.model?.provider) ||
237-
hasConfiguredLocalProviderSignal({ cfg: params?.cfg, provider: params?.model?.provider })) &&
238-
!isOllamaCloudModel(params?.model);
260+
const {
261+
isLocalRuntimeModel,
262+
isExplicitLocalHostnameRuntimeModel,
263+
isSelfHostedHostnameRuntimeModel,
264+
} = resolveRuntimeModelLocality(params);
239265
const timeoutBounds = [
240266
runTimeoutIsNoTimeout ? undefined : runTimeoutMs,
241267
hasExplicitRunTimeout ? undefined : agentTimeoutMs,
@@ -320,22 +346,11 @@ export function resolveLlmFirstEventTimeoutMs(params?: {
320346
const hasExplicitRunTimeout =
321347
typeof runTimeoutMs === "number" && Number.isFinite(runTimeoutMs) && runTimeoutMs > 0;
322348
const runTimeoutIsBounded = hasExplicitRunTimeout && runTimeoutMs < MAX_TIMER_TIMEOUT_MS;
323-
const baseUrl = params?.model?.baseUrl;
324-
const isLocalProvider =
325-
typeof baseUrl === "string" && baseUrl.length > 0 && isLocalProviderBaseUrl(baseUrl);
326-
const isLocalRuntimeModel = isLocalProvider && !isOllamaCloudModel(params?.model);
327-
const isExplicitLocalHostnameRuntimeModel =
328-
typeof baseUrl === "string" &&
329-
baseUrl.length > 0 &&
330-
isExplicitLocalHostnameBaseUrl(baseUrl) &&
331-
!isOllamaCloudModel(params?.model);
332-
const isSelfHostedHostnameRuntimeModel =
333-
typeof baseUrl === "string" &&
334-
baseUrl.length > 0 &&
335-
isBareProviderHostnameBaseUrl(baseUrl) &&
336-
(isSelfHostedProviderId(params?.model?.provider) ||
337-
hasConfiguredLocalProviderSignal({ cfg: params?.cfg, provider: params?.model?.provider })) &&
338-
!isOllamaCloudModel(params?.model);
349+
const {
350+
isLocalRuntimeModel,
351+
isExplicitLocalHostnameRuntimeModel,
352+
isSelfHostedHostnameRuntimeModel,
353+
} = resolveRuntimeModelLocality(params);
339354
const timeoutBounds = [
340355
runTimeoutIsBounded ? runTimeoutMs : undefined,
341356
hasExplicitRunTimeout ? undefined : agentTimeoutMs,

0 commit comments

Comments
 (0)