refactor: share agent binding formatter

This commit is contained in:
Peter Steinberger
2026-04-19 00:01:49 +01:00
parent 570fb5594c
commit 2070142c49
3 changed files with 22 additions and 36 deletions

View File

@@ -0,0 +1,19 @@
import type { AgentRouteBinding } from "../config/types.js";
export function describeBinding(binding: AgentRouteBinding): string {
const match = binding.match;
const parts = [match.channel];
if (match.accountId) {
parts.push(`accountId=${match.accountId}`);
}
if (match.peer) {
parts.push(`peer=${match.peer.kind}:${match.peer.id}`);
}
if (match.guildId) {
parts.push(`guild=${match.guildId}`);
}
if (match.teamId) {
parts.push(`team=${match.teamId}`);
}
return parts.join(" ");
}

View File

@@ -9,6 +9,8 @@ import { normalizeOptionalString } from "../shared/string-coerce.js";
import { normalizeStringEntries } from "../shared/string-normalization.js";
import type { ChannelChoice } from "./onboard-types.js";
export { describeBinding } from "./agents.binding-format.js";
function bindingMatchKey(match: AgentRouteBinding["match"]) {
const accountId = normalizeOptionalString(match.accountId) || DEFAULT_ACCOUNT_ID;
const identityKey = bindingMatchIdentityKey(match);
@@ -49,24 +51,6 @@ function canUpgradeBindingAccountScope(params: {
);
}
export function describeBinding(binding: AgentRouteBinding) {
const match = binding.match;
const parts = [match.channel];
if (match.accountId) {
parts.push(`accountId=${match.accountId}`);
}
if (match.peer) {
parts.push(`peer=${match.peer.kind}:${match.peer.id}`);
}
if (match.guildId) {
parts.push(`guild=${match.guildId}`);
}
if (match.teamId) {
parts.push(`team=${match.teamId}`);
}
return parts.join(" ");
}
export function applyAgentBindings(
cfg: OpenClawConfig,
bindings: AgentRouteBinding[],

View File

@@ -6,6 +6,7 @@ import type { AgentRouteBinding } from "../config/types.js";
import { normalizeAgentId } from "../routing/session-key.js";
import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
import { defaultRuntime } from "../runtime.js";
import { describeBinding } from "./agents.binding-format.js";
import { requireValidConfig, requireValidConfigFileSnapshot } from "./agents.command-shared.js";
type AgentBindingsModule = typeof import("./agents.bindings.js");
@@ -35,24 +36,6 @@ function loadAgentBindingsModule(): Promise<AgentBindingsModule> {
return agentBindingsModulePromise;
}
function describeBinding(binding: AgentRouteBinding): string {
const match = binding.match;
const parts = [match.channel];
if (match.accountId) {
parts.push(`accountId=${match.accountId}`);
}
if (match.peer) {
parts.push(`peer=${match.peer.kind}:${match.peer.id}`);
}
if (match.guildId) {
parts.push(`guild=${match.guildId}`);
}
if (match.teamId) {
parts.push(`team=${match.teamId}`);
}
return parts.join(" ");
}
function resolveAgentId(
cfg: Awaited<ReturnType<typeof requireValidConfig>>,
agentInput: string | undefined,