/** * Cast-mapping and content helpers shared across the plugin: `neynarCastToCast` * translates a Neynar cast into the domain `Cast`, `castId`/`castUuid` derive a * stable memory id (`stringToUuid`) from a cast hash, `extractCastEmbedUrls` * pulls attachment URLs for outbound embeds, and `splitPostContent` chunks * over-length prose into a cast thread. */ import type { Content, IAgentRuntime, Memory, UUID } from "@elizaos/core"; import { stringToUuid } from "@elizaos/core"; import type { Cast as NeynarCast } from "@neynar/nodejs-sdk/build/api"; import { type Cast, FARCASTER_SOURCE } from "../types"; export const MAX_CAST_LENGTH = 1024; /** * Extract agent-generated attachment URLs from a message {@link Content} to ride * along as Farcaster cast embeds (#8876 / #8990). Neynar embeds are URL-based, * so this returns each attachment's non-empty `url`. Shared by the POST connector * (`handleSendPost`) and the mention-reply callback so both outbound paths attach * media identically instead of one of them silently dropping it. */ export function extractCastEmbedUrls(content: Content): string[] { return Array.isArray(content.attachments) ? content.attachments .map((m) => m?.url) .filter((u): u is string => typeof u === "string" && u.trim().length > 0) : []; } export function castId({ hash, agentId }: { hash: string; agentId: string }): string { return `${hash}-${agentId}`; } export function castUuid(props: { hash: string; agentId: string }): UUID { return stringToUuid(castId(props)); } export function splitPostContent(content: string, maxLength: number = MAX_CAST_LENGTH): string[] { const paragraphs = content.split("\n\n").map((p) => p.trim()); const posts: string[] = []; let currentCast = ""; for (const paragraph of paragraphs) { if (!paragraph) continue; if (`${currentCast}\n\n${paragraph}`.trim().length <= maxLength) { if (currentCast) { currentCast += `\n\n${paragraph}`; } else { currentCast = paragraph; } } else { if (currentCast) { posts.push(currentCast.trim()); } if (paragraph.length <= maxLength) { currentCast = paragraph; } else { const chunks = splitParagraph(paragraph, maxLength); posts.push(...chunks.slice(0, -1)); currentCast = chunks[chunks.length - 1]; } } } if (currentCast) { posts.push(currentCast.trim()); } return posts; } export function splitParagraph(paragraph: string, maxLength: number): string[] { const sentences = paragraph.match(/[^.!?]+[.!?]+|[^.!?]+$/g) || [paragraph]; const chunks: string[] = []; let currentChunk = ""; for (const sentence of sentences) { if (`${currentChunk} ${sentence}`.trim().length <= maxLength) { if (currentChunk) { currentChunk += ` ${sentence}`; } else { currentChunk = sentence; } } else { if (currentChunk) { chunks.push(currentChunk.trim()); } if (sentence.length <= maxLength) { currentChunk = sentence; } else { const words = sentence.split(" "); currentChunk = ""; for (const word of words) { if (`${currentChunk} ${word}`.trim().length <= maxLength) { if (currentChunk) { currentChunk += ` ${word}`; } else { currentChunk = word; } } else { if (currentChunk) { chunks.push(currentChunk.trim()); } // A single unbroken word (long URL, hash, etc.) can exceed the // platform limit on its own — hard-slice it so no emitted chunk // is ever longer than maxLength. let rest = word; while (rest.length > maxLength) { chunks.push(rest.slice(0, maxLength)); rest = rest.slice(maxLength); } currentChunk = rest; } } } } } if (currentChunk) { chunks.push(currentChunk.trim()); } return chunks; } export function lastCastCacheKey(fid: number): string { return `farcaster/${fid}/lastCast`; } export function neynarCastToCast(neynarCast: NeynarCast): Cast { return { hash: neynarCast.hash, authorFid: neynarCast.author.fid, text: neynarCast.text, threadId: neynarCast.thread_hash ?? undefined, profile: { fid: neynarCast.author.fid, name: neynarCast.author.display_name || "anon", username: neynarCast.author.username, }, ...(neynarCast.parent_hash && neynarCast.parent_author?.fid ? { inReplyTo: { hash: neynarCast.parent_hash, fid: neynarCast.parent_author.fid, }, } : {}), timestamp: new Date(neynarCast.timestamp), embeds: neynarCast.embeds && neynarCast.embeds.length > 0 ? neynarCast.embeds : undefined, }; } export function createCastMemory({ roomId, senderId, runtime, cast, accountId, }: { roomId: UUID; senderId: UUID; runtime: IAgentRuntime; cast: Cast; accountId?: string; }): Memory { const inReplyTo = cast.inReplyTo ? castUuid({ hash: cast.inReplyTo.hash, agentId: runtime.agentId, }) : undefined; return { id: castUuid({ hash: cast.hash, agentId: runtime.agentId, }), agentId: runtime.agentId, entityId: senderId, content: { text: cast.text, source: FARCASTER_SOURCE, ...(accountId ? { accountId } : {}), url: "", inReplyTo, hash: cast.hash, threadId: cast.threadId, attachments: cast.media && cast.media.length > 0 ? cast.media : undefined, }, metadata: { ...(accountId ? { accountId } : {}), }, roomId, }; } export function formatCastTimestamp(timestamp: Date): string { return timestamp.toLocaleString("en-US", { hour: "2-digit", minute: "2-digit", month: "short", day: "numeric", }); }