Files
openclaw/src/agents/generated-attachments.ts
Peter Steinberger 77f1359612 refactor: extract media and ACP core packages (#88534)
* refactor: extract media and acp core packages

* refactor: remove relocated media and acp sources

* build: wire new core packages into dependency checks

* test: alias new core packages in vitest

* build: keep media sniffer runtime dependency

* docs: refresh plugin sdk api baseline

* fix: keep normalized proposal queries non-empty

* test: keep channel timer tests isolated

* fix: keep rebased plugin checks green

* fix: preserve sms numeric allowlist entries

* test: harden exec foreground timeout failure

* test: remove duplicate skill workshop assertion

* fix: remove channel config lint suppression

* test: refresh lint suppression allowlist
2026-05-31 11:30:33 +01:00

72 lines
2.2 KiB
TypeScript

import { basenameFromAnyPath } from "@openclaw/media-core/file-name";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { uniqueStrings } from "@openclaw/normalization-core/string-normalization";
export type AgentGeneratedAttachment = {
type?: "image" | "audio" | "video" | "file";
path?: string;
url?: string;
mediaUrl?: string;
filePath?: string;
mimeType?: string;
name?: string;
};
export function generatedAttachmentReference(
attachment: AgentGeneratedAttachment,
): string | undefined {
return normalizeOptionalString(
attachment.path ?? attachment.url ?? attachment.mediaUrl ?? attachment.filePath,
);
}
export function mediaUrlsFromGeneratedAttachments(
attachments: readonly AgentGeneratedAttachment[] | undefined,
): string[] {
return uniqueStrings(
attachments?.flatMap((attachment) => generatedAttachmentReference(attachment) ?? []) ?? [],
);
}
export function nameFromGeneratedAttachment(
attachment: AgentGeneratedAttachment,
): string | undefined {
return (
normalizeOptionalString(attachment.name) ??
basenameFromAnyPath(generatedAttachmentReference(attachment) ?? "")
);
}
export function formatGeneratedAttachmentLines(
attachments: readonly AgentGeneratedAttachment[] | undefined,
): string[] {
if (!attachments?.length) {
return [];
}
const lines = ["Attachments:"];
for (const [index, attachment] of attachments.entries()) {
const parts = [`${index + 1}.`];
const type = normalizeOptionalString(attachment.type);
const name = nameFromGeneratedAttachment(attachment);
const mimeType = normalizeOptionalString(attachment.mimeType);
const path = normalizeOptionalString(attachment.path ?? attachment.filePath);
const url = normalizeOptionalString(attachment.url ?? attachment.mediaUrl);
if (type) {
parts.push(`type=${type}`);
}
if (name) {
parts.push(`name=${JSON.stringify(name)}`);
}
if (mimeType) {
parts.push(`mimeType=${mimeType}`);
}
if (path) {
parts.push(`path=${JSON.stringify(path)}`);
} else if (url) {
parts.push(`mediaUrl=${JSON.stringify(url)}`);
}
lines.push(parts.join(" "));
}
return lines;
}