Skip to main content
Home
This release is 4 versions behind 0.6.1 — the latest version of @corespeed/zypher. Jump to latest

Built and signed on GitHub Actions

An open-source agent framework for building production-ready agentic AI agents

This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
88%
Published
a month ago (0.4.4)
Package root>src>storage>FileAttachmentManager.ts
import * as path from "jsr:@std/path@^1.1.2"; import { type FileAttachment, isFileAttachment, type Message, } from "../message.ts"; import type { StorageService } from "./StorageService.ts"; import { fileExists } from "../utils/mod.ts"; /** * A map of file attachment IDs to their cached file paths and signed URLs */ export interface FileAttachmentCacheMap { [fileId: string]: FileAttachmentCache; } export interface FileAttachmentCache { /** * The local cache file path for the file attachment, this can be used to read the file from local file system */ cachePath: string; /** * The signed URL for the file attachment, this can be used to download the file attachment from public internet */ signedUrl: string; } export class FileAttachmentManager { constructor( private readonly storageService: StorageService, readonly cacheDir: string, ) {} /** * Retrieves a file attachment from storage service * @param fileId ID of the file to retrieve * @returns Promise resolving to a FileAttachment object or null if file doesn't exist or isn't supported */ async getFileAttachment(fileId: string): Promise<FileAttachment | null> { // Get metadata and check if the file exists const metadata = await this.storageService.getFileMetadata(fileId); if (!metadata) { return null; } // Return formatted file attachment return { type: "file_attachment", fileId, mimeType: metadata.contentType, }; } /** * Get the local cache file path for a file attachment * @param fileId ID of the file attachment * @returns Promise resolving to the cache file path */ getFileAttachmentCachePath(fileId: string): string { return path.join(this.cacheDir, fileId); } /** * Caches all file attachments in a messages * @param messages The messages to cache file attachments from * @returns Promise resolving to a dictionary of file attachment caches */ async cacheMessageFileAttachments( messages: Message[], ): Promise<FileAttachmentCacheMap> { const cacheDict: FileAttachmentCacheMap = {}; for (const message of messages) { for (const block of message.content) { if (isFileAttachment(block)) { const cache = await this.cacheFileAttachment(block.fileId); if (cache) { cacheDict[block.fileId] = cache; } } } } return cacheDict; } /** * Caches a file attachment if it's not already cached if possible * @param fileId ID of the file attachment * @returns Promise resolving to a FileAttachmentCache object, * or null if: * - the file ID does not exist on storage service */ async cacheFileAttachment( fileId: string, ): Promise<FileAttachmentCache | null> { const cachePath = this.getFileAttachmentCachePath(fileId); if (!await fileExists(cachePath)) { // Download the file attachment from storage service to cache path await this.storageService.downloadFile(fileId, cachePath); } return { cachePath, signedUrl: await this.storageService.getSignedUrl(fileId), }; } }