//#region src/filesystem.d.ts /** * Workspace — durable file storage backed by SQLite + optional R2. * * Accepts any `SqlBackend` (two methods: `query` and `run`), or * auto-detects `SqlStorage` (DO built-in) and `D1Database` directly. * * ```ts * // Durable Object (any DO with SQLite storage) * const workspace = new Workspace({ sql: ctx.storage.sql }); * * // D1 * const workspace = new Workspace({ sql: env.MY_DB }); * * // Agent (with R2 and lazy name for observability) * class MyAgent extends Agent { * workspace = new Workspace({ * sql: this.ctx.storage.sql, * r2: this.env.WORKSPACE_FILES, * name: () => this.name, * }); * } * ``` * * @module workspace */ type SqlParam = string | number | boolean | null; /** * Minimal SQL interface: query rows and run statements. * Return values may be sync or async — Workspace awaits either. */ interface SqlBackend { query>( sql: string, ...params: SqlParam[] ): T[] | Promise; run(sql: string, ...params: SqlParam[]): void | Promise; } /** Auto-detect: accepts SqlStorage, D1Database, or a raw SqlBackend. */ type SqlSource = SqlStorage | D1Database | SqlBackend; interface WorkspaceOptions { /** SQL backend — SqlStorage, D1Database, or a custom SqlBackend. */ sql: SqlSource; /** Namespace to isolate this workspace's tables (default: "default"). */ namespace?: string; /** R2 bucket for large-file storage (optional). */ r2?: R2Bucket; /** Prefix for R2 object keys. Defaults to `name`. */ r2Prefix?: string; /** Byte threshold for spilling files to R2 (default: 1_500_000). */ inlineThreshold?: number; /** Called when files/directories change. */ onChange?: (event: WorkspaceChangeEvent) => void; /** * Name used as default R2 prefix and in observability events. * Accepts a string or a function for lazy evaluation (useful when * the name isn't available at class field initialization time, e.g. * in Durable Objects where `this.name` is set after construction). */ name?: string | (() => string | undefined); } type EntryType = "file" | "directory" | "symlink"; type FileInfo = { path: string; name: string; type: EntryType; mimeType: string; size: number; createdAt: number; updatedAt: number; target?: string; }; type FileStat = FileInfo; type WorkspaceChangeType = "create" | "update" | "delete"; type WorkspaceChangeEvent = { type: WorkspaceChangeType; path: string; entryType: EntryType; }; /** * Minimum set of `Workspace` methods required to satisfy the * `FileSystem` adapter (and therefore `createWorkspaceStateBackend`). * * A concrete `Workspace` trivially satisfies this. Callers who wrap a * `Workspace` behind their own layer — most commonly a cross-DO proxy * that forwards each call to a parent agent's real `Workspace` over * RPC — can satisfy `WorkspaceFsLike` without subclassing or casting, * and still pass the object to `WorkspaceFileSystem` or * `createWorkspaceStateBackend`. * * This is a strict superset of the `WorkspaceLike` shape that * `@cloudflare/think` uses for builtin tool wiring. Tooling that only * reaches for the narrow surface can stick with `WorkspaceLike`; * anything touching codemode's `state.*` via * `createWorkspaceStateBackend` needs this. * * @experimental The API surface may change before stabilizing. */ type WorkspaceFsLike = Pick< Workspace, | "readFile" | "readFileBytes" | "writeFile" | "writeFileBytes" | "appendFile" | "exists" | "stat" | "lstat" | "mkdir" | "readDir" | "rm" | "cp" | "mv" | "symlink" | "readlink" | "glob" >; declare class Workspace { private readonly sql; private readonly _nameOrFn; private readonly namespace; private readonly tableName; private readonly indexName; private readonly r2; private readonly r2Prefix; private readonly threshold; private readonly onChange; private initialized; constructor(options: WorkspaceOptions); private get _name(); private emit; private _observe; private ensureInit; private getR2; private resolveR2Prefix; private r2Key; private resolveSymlink; symlink(target: string, linkPath: string): Promise; readlink(path: string): Promise; lstat(path: string): Promise; stat(path: string): Promise; readFile(path: string): Promise; readFileBytes(path: string): Promise; writeFileBytes( path: string, data: Uint8Array | ArrayBuffer, mimeType?: string ): Promise; writeFile(path: string, content: string, mimeType?: string): Promise; readFileStream(path: string): Promise | null>; writeFileStream( path: string, stream: ReadableStream, mimeType?: string ): Promise; appendFile(path: string, content: string, mimeType?: string): Promise; deleteFile(path: string): Promise; fileExists(path: string): Promise; exists(path: string): Promise; readDir( dir?: string, opts?: { limit?: number; offset?: number; } ): Promise; glob(pattern: string): Promise; mkdir( path: string, opts?: { recursive?: boolean; }, _depth?: number ): Promise; rm( path: string, opts?: { recursive?: boolean; force?: boolean; } ): Promise; cp( src: string, dest: string, opts?: { recursive?: boolean; } ): Promise; mv( src: string, dest: string, opts?: { recursive?: boolean; } ): Promise; diff(pathA: string, pathB: string): Promise; diffContent(path: string, newContent: string): Promise; getWorkspaceInfo(): Promise<{ fileCount: number; directoryCount: number; totalBytes: number; r2FileCount: number; }>; /** @internal */ _getAllPaths(): Promise; /** @internal */ _updateModifiedAt(path: string, mtime: Date): Promise; private ensureParentDir; private deleteDescendants; } //#endregion export { SqlParam as a, WorkspaceChangeEvent as c, WorkspaceOptions as d, SqlBackend as i, WorkspaceChangeType as l, FileInfo as n, SqlSource as o, FileStat as r, Workspace as s, EntryType as t, WorkspaceFsLike as u }; //# sourceMappingURL=filesystem-BKxpZmkl.d.ts.map