@@ -3,6 +3,8 @@ import { existsSync, mkdirSync } from 'fs';
33import { KvStore } from './kvstore' ;
44import { Filesystem } from './filesystem' ;
55import { ToolCalls } from './toolcalls' ;
6+ import type { SyncProvider , TursoSyncFactory } from './sync' ;
7+ import { TursoSyncProvider } from './providers/turso' ;
68
79/**
810 * Configuration options for opening an AgentFS instance
@@ -14,8 +16,13 @@ export interface AgentFSOptions {
1416 * - If omitted: Uses ephemeral in-memory database
1517 */
1618 id ?: string ;
17- // Future: sync configuration will be added here
18- // sync?: SyncConfig;
19+
20+ /**
21+ * Optional sync configuration
22+ * - Pass a TursoSyncFactory from tursoSync() for Turso Cloud sync
23+ * - Pass a custom SyncProvider for other backends
24+ */
25+ sync ?: TursoSyncFactory | SyncProvider ;
1926}
2027
2128export class AgentFS {
@@ -24,15 +31,23 @@ export class AgentFS {
2431 public readonly kv : KvStore ;
2532 public readonly fs : Filesystem ;
2633 public readonly tools : ToolCalls ;
34+ public readonly sync ?: SyncProvider ;
2735
2836 /**
2937 * Private constructor - use AgentFS.open() instead
3038 */
31- private constructor ( db : Database , kv : KvStore , fs : Filesystem , tools : ToolCalls ) {
39+ private constructor (
40+ db : Database ,
41+ kv : KvStore ,
42+ fs : Filesystem ,
43+ tools : ToolCalls ,
44+ sync ?: SyncProvider
45+ ) {
3246 this . db = db ;
3347 this . kv = kv ;
3448 this . fs = fs ;
3549 this . tools = tools ;
50+ this . sync = sync ;
3651 }
3752
3853 /**
@@ -47,6 +62,13 @@ export class AgentFS {
4762 *
4863 * // Ephemeral in-memory database
4964 * const agent = await AgentFS.open();
65+ *
66+ * // With Turso Cloud sync
67+ * import { tursoSync } from 'agentfs-sdk';
68+ * const agent = await AgentFS.open({
69+ * id: 'my-agent',
70+ * sync: tursoSync()
71+ * });
5072 * ```
5173 */
5274 static async open ( options ?: AgentFSOptions ) : Promise < AgentFS > {
@@ -60,7 +82,7 @@ export class AgentFS {
6082 ) ;
6183 }
6284
63- const { id } = options || { } ;
85+ const { id, sync } = options || { } ;
6486
6587 // Determine database path based on id
6688 let dbPath : string ;
@@ -91,8 +113,22 @@ export class AgentFS {
91113 await fs . ready ( ) ;
92114 await tools . ready ( ) ;
93115
116+ // Initialize sync if configured
117+ let syncProvider : SyncProvider | undefined ;
118+ if ( sync ) {
119+ if ( '__type' in sync && sync . __type === 'turso-sync-factory' ) {
120+ // Turso sync factory
121+ syncProvider = new TursoSyncProvider ( db , id || 'default' , sync . config ) ;
122+ } else {
123+ // Custom sync provider
124+ syncProvider = sync as SyncProvider ;
125+ }
126+
127+ await syncProvider . initialize ( ) ;
128+ }
129+
94130 // Return fully initialized instance
95- return new AgentFS ( db , kv , fs , tools ) ;
131+ return new AgentFS ( db , kv , fs , tools , syncProvider ) ;
96132 }
97133
98134 /**
@@ -103,9 +139,12 @@ export class AgentFS {
103139 }
104140
105141 /**
106- * Close the database connection
142+ * Close the database connection and cleanup sync
107143 */
108144 async close ( ) : Promise < void > {
145+ if ( this . sync ) {
146+ await this . sync . cleanup ( ) ;
147+ }
109148 await this . db . close ( ) ;
110149 }
111150}
@@ -115,3 +154,5 @@ export { Filesystem } from './filesystem';
115154export type { Stats } from './filesystem' ;
116155export { ToolCalls } from './toolcalls' ;
117156export type { ToolCall , ToolCallStats } from './toolcalls' ;
157+ export { tursoSync } from './sync' ;
158+ export type { SyncProvider , TursoSyncConfig , SyncStatus } from './sync' ;
0 commit comments