/** * @module minions-sdk/storage/cloudflare-kv * Cloudflare Workers KV storage adapter for the Minions structured object system. * * Designed for use in Cloudflare Workers. All data is stored in a KV namespace, * with each minion serialised as a JSON string under a namespaced key. * * The adapter does NOT depend on `@cloudflare/workers-types` at compile time — * it defines a minimal `KVNamespaceLike` interface (the same peer-dep pattern * used by the Convex and Supabase adapters). * * @example * ```typescript * import { CloudflareKVStorageAdapter } from 'minions-sdk'; * * export default { * async fetch(request: Request, env: Env) { * const storage = new CloudflareKVStorageAdapter(env.MINIONS_KV); * const minions = new Minions({ storage }); * // … * }, * }; * ``` */ import type { Minion } from '../types/index.js'; import type { StorageAdapter, StorageFilter } from './StorageAdapter.js'; /** Result item returned by `KVNamespace.list()`. */ export interface KVListKey { name: string; expiration?: number; metadata?: unknown; } /** Result returned by `KVNamespace.list()`. */ export interface KVListResult { keys: KVListKey[]; list_complete: boolean; cursor?: string; } /** * Minimal interface for a Cloudflare Workers KV namespace. * * Any real `KVNamespace` binding from a Cloudflare Worker environment satisfies * this contract. By using this interface, the adapter compiles without * requiring `@cloudflare/workers-types` as a direct dependency. */ export interface KVNamespaceLike { get(key: string, options?: { type?: string; }): Promise; put(key: string, value: string, options?: { metadata?: unknown; }): Promise; delete(key: string): Promise; list(options?: { prefix?: string; limit?: number; cursor?: string; }): Promise; } /** Options for the Cloudflare KV storage adapter. */ export interface CloudflareKVStorageOptions { /** * Key prefix used for all minion entries. * Default: `"minion:"`. The resulting key format is ``. */ prefix?: string; /** * Key prefix used for attachment file entries. * Default: `"file:"`. The resulting key format is `:`. */ filePrefix?: string; } /** * Storage adapter backed by **Cloudflare Workers KV**. * * Each minion is stored as a JSON-serialised string under the key * ``. Listing is performed via KV `list()` with cursor-based * pagination, then filtering/sorting is applied client-side using the * shared {@link applyFilter} utility. * * **Setup**: Bind a KV namespace to your Worker in `wrangler.toml`: * * ```toml * [[kv_namespaces]] * binding = "MINIONS_KV" * id = "your-kv-namespace-id" * ``` * * Then pass the binding to the constructor: * * ```typescript * const storage = new CloudflareKVStorageAdapter(env.MINIONS_KV); * ``` * * @example * ```typescript * import { Minions, CloudflareKVStorageAdapter } from 'minions-sdk'; * * export default { * async fetch(request: Request, env: Env) { * const storage = new CloudflareKVStorageAdapter(env.MINIONS_KV); * const minions = new Minions({ storage }); * * const agent = await minions.create('agent', { * title: 'My Agent', * fields: { role: 'assistant', model: 'gpt-4' }, * }); * * return Response.json({ id: agent.data.id }); * }, * }; * ``` */ export declare class CloudflareKVStorageAdapter implements StorageAdapter { private kv; private prefix; private filePrefix; constructor(kv: KVNamespaceLike, options?: CloudflareKVStorageOptions); get(id: string): Promise; set(minion: Minion): Promise; delete(id: string): Promise; list(filter?: StorageFilter): Promise; search(query: string): Promise; putFile(id: string, filename: string, data: Uint8Array): Promise; getFile(id: string, filename: string): Promise; deleteFile(id: string, filename: string): Promise; listFiles(id: string): Promise; /** * Fetch all minion entries from KV using cursor-based pagination. * KV `list()` returns at most 1000 keys per call. */ private fetchAllMinions; /** Build the KV key for an attachment file. */ private fileKey; /** Convert Uint8Array to base64 string (works in Workers runtime). */ private uint8ArrayToBase64; /** Convert base64 string back to Uint8Array. */ private base64ToUint8Array; } //# sourceMappingURL=CloudflareKVStorageAdapter.d.ts.map