mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:01:24 +00:00
refactor(gateway): share rpc attachment normalization
This commit is contained in:
32
src/gateway/server-methods/attachment-normalize.ts
Normal file
32
src/gateway/server-methods/attachment-normalize.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { ChatAttachment } from "../chat-attachments.js";
|
||||
|
||||
export type RpcAttachmentInput = {
|
||||
type?: unknown;
|
||||
mimeType?: unknown;
|
||||
fileName?: unknown;
|
||||
content?: unknown;
|
||||
};
|
||||
|
||||
export function normalizeRpcAttachmentsToChatAttachments(
|
||||
attachments: RpcAttachmentInput[] | undefined,
|
||||
): ChatAttachment[] {
|
||||
return (
|
||||
attachments
|
||||
?.map((a) => ({
|
||||
type: typeof a?.type === "string" ? a.type : undefined,
|
||||
mimeType: typeof a?.mimeType === "string" ? a.mimeType : undefined,
|
||||
fileName: typeof a?.fileName === "string" ? a.fileName : undefined,
|
||||
content:
|
||||
typeof a?.content === "string"
|
||||
? a.content
|
||||
: ArrayBuffer.isView(a?.content)
|
||||
? Buffer.from(a.content.buffer, a.content.byteOffset, a.content.byteLength).toString(
|
||||
"base64",
|
||||
)
|
||||
: a?.content instanceof ArrayBuffer
|
||||
? Buffer.from(a.content).toString("base64")
|
||||
: undefined,
|
||||
}))
|
||||
.filter((a) => a.content) ?? []
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user