Files
openclaw/src/agents/spawned-context.ts
Peter Steinberger 00d8d7ead0 refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
2026-05-31 01:33:00 +01:00

80 lines
2.7 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { normalizeAgentId, parseAgentSessionKey } from "../routing/session-key.js";
import { resolveAgentWorkspaceDir } from "./agent-scope.js";
export type SpawnedRunMetadata = {
spawnedBy?: string | null;
groupId?: string | null;
groupChannel?: string | null;
groupSpace?: string | null;
workspaceDir?: string | null;
};
export type SpawnedToolContext = {
agentGroupId?: string | null;
agentGroupChannel?: string | null;
agentGroupSpace?: string | null;
agentMemberRoleIds?: string[];
workspaceDir?: string;
inheritedToolAllowlist?: string[];
inheritedToolDenylist?: string[];
};
type NormalizedSpawnedRunMetadata = {
spawnedBy?: string;
groupId?: string;
groupChannel?: string;
groupSpace?: string;
workspaceDir?: string;
};
export function normalizeSpawnedRunMetadata(
value?: SpawnedRunMetadata | null,
): NormalizedSpawnedRunMetadata {
return {
spawnedBy: normalizeOptionalString(value?.spawnedBy),
groupId: normalizeOptionalString(value?.groupId),
groupChannel: normalizeOptionalString(value?.groupChannel),
groupSpace: normalizeOptionalString(value?.groupSpace),
workspaceDir: normalizeOptionalString(value?.workspaceDir),
};
}
export function mapToolContextToSpawnedRunMetadata(
value?: SpawnedToolContext | null,
): Pick<NormalizedSpawnedRunMetadata, "groupId" | "groupChannel" | "groupSpace" | "workspaceDir"> {
return {
groupId: normalizeOptionalString(value?.agentGroupId),
groupChannel: normalizeOptionalString(value?.agentGroupChannel),
groupSpace: normalizeOptionalString(value?.agentGroupSpace),
workspaceDir: normalizeOptionalString(value?.workspaceDir),
};
}
export function resolveSpawnedWorkspaceInheritance(params: {
config: OpenClawConfig;
targetAgentId?: string;
requesterSessionKey?: string;
explicitWorkspaceDir?: string | null;
}): string | undefined {
const explicit = normalizeOptionalString(params.explicitWorkspaceDir);
if (explicit) {
return explicit;
}
// For cross-agent spawns, use the target agent's workspace instead of the requester's.
const agentId =
params.targetAgentId ??
(params.requesterSessionKey
? parseAgentSessionKey(params.requesterSessionKey)?.agentId
: undefined);
return agentId ? resolveAgentWorkspaceDir(params.config, normalizeAgentId(agentId)) : undefined;
}
export function resolveIngressWorkspaceOverrideForSpawnedRun(
metadata?: Pick<SpawnedRunMetadata, "spawnedBy" | "workspaceDir"> | null,
): string | undefined {
const normalized = normalizeSpawnedRunMetadata(metadata);
return normalized.spawnedBy ? normalized.workspaceDir : undefined;
}