@dbx-tools/appkit-mastra 0.6.8 → 0.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,208 +1,90 @@
1
1
  /**
2
- * Mastra {@link WorkspaceFilesystem} implementations for Databricks Apps.
2
+ * Mastra workspace filesystem adapters.
3
3
  *
4
- * {@link DatabricksWorkspaceFilesystem} maps a Mastra workspace namespace onto
5
- * an absolute Databricks path (Unity Catalog volume, workspace object tree, or
6
- * DBFS). {@link emptyFilesystem} is a read-only no-op mount used when no
7
- * dynamic mounts resolve for a request.
8
- *
9
- * Path helpers ({@link normalizeDatabricksBasePath}, {@link isDbfsPath}, …)
10
- * are exported for tests and callers that need to reason about Databricks
11
- * paths without constructing a filesystem.
4
+ * {@link filesystems} wraps any portable `@dbx-tools/shared-fs` {@link FileSystem}
5
+ * (local disk, Databricks, memory, …) as a Mastra {@link MastraFilesystem}.
6
+ * {@link scratchFilesystem} always returns a fresh {@link localFS.tmpFS} mount
7
+ * (random id root) when Mastra needs a filesystem and no other mount resolved.
12
8
  *
13
9
  * @module
14
10
  */
15
- import { WorkspaceClient } from "@databricks/sdk-experimental";
11
+ import type { FileSystem } from "@dbx-tools/shared-fs";
16
12
  import { MastraFilesystem } from "@mastra/core/workspace";
17
13
  import type { CopyOptions, FileContent, FileEntry, FileStat, FilesystemInfo, ListOptions, MastraFilesystemOptions, ProviderStatus, ReadOptions, RemoveOptions, WriteOptions } from "@mastra/core/workspace";
18
- /** How {@link DatabricksWorkspaceFilesystem.init} handles a missing {@link basePath}. */
19
- export type DatabricksMkdirsMode = boolean | "try";
20
- /** Options for {@link DatabricksWorkspaceFilesystem}. */
21
- export interface DatabricksWorkspaceFilesystemOptions extends MastraFilesystemOptions {
22
- /** Unique identifier for this filesystem instance. */
14
+ /** Options for {@link filesystems} / {@link MastraFileSystemAdapter}. */
15
+ export interface MastraFileSystemAdapterOptions extends MastraFilesystemOptions {
16
+ /** Override the Mastra filesystem id. Defaults to the source {@link FileSystem.id}. */
23
17
  id?: string;
24
- /** Auth-scoped Databricks workspace client. */
25
- client?: WorkspaceClient;
18
+ /** Override the Mastra display name. Defaults to `MastraFileSystemAdapter`. */
19
+ name?: string;
26
20
  /**
27
- * Absolute Databricks path that roots the workspace namespace, e.g.
28
- * `/Volumes/catalog/schema/volume` or `/dbfs/FileStore/shared`.
21
+ * Override the Mastra provider id. Defaults to the source
22
+ * {@link FileSystem.backend}.
29
23
  */
30
- basePath: string;
24
+ provider?: string;
31
25
  /**
32
- * When the {@link basePath} is missing at {@link init}, create it with the
33
- * matching Databricks `mkdirs` API. `"try"` (default) logs at debug and
34
- * falls back to an empty read-only namespace on failure; `true` fails init;
35
- * `false` skips creation and uses the empty namespace.
36
- *
37
- * A successful mkdir also satisfies the write-access probe when
38
- * {@link readOnly} is omitted.
26
+ * Force read-only mounts even when the source filesystem allows writes.
27
+ * The source {@link FileSystem.readOnly} flag still applies either way.
39
28
  */
40
- mkdirs?: DatabricksMkdirsMode;
41
- /** Block writes while still allowing reads. When omitted, {@link init} probes write access. */
42
29
  readOnly?: boolean;
43
30
  }
44
- /** Normalize a Databricks base path (POSIX, no trailing slash). */
45
- export declare function normalizeDatabricksBasePath(basePath: string): string;
46
- /** True when `absolutePath` is served by DBFS rather than the UC Files API. */
47
- export declare function isDbfsPath(absolutePath: string): boolean;
48
- /** True when `absolutePath` is a Databricks workspace object path. */
49
- export declare function isWorkspaceFilesPath(absolutePath: string): boolean;
50
31
  /**
51
- * Resolve a workspace-relative path to an absolute Databricks path under
52
- * `basePath`.
32
+ * Wrap a portable {@link FileSystem} as a Mastra {@link MastraFilesystem}.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { filesystems } from "@dbx-tools/appkit-mastra";
37
+ * import { DatabricksFileSystem } from "@dbx-tools/databricks";
38
+ * import { localFS } from "@dbx-tools/fs";
39
+ *
40
+ * const volume = filesystems.filesystems(
41
+ * new DatabricksFileSystem({ root: "/Volumes/main/default/assets" }),
42
+ * );
43
+ * const scratch = filesystems.filesystems(localFS.tmpFS("agent-job"));
44
+ * ```
53
45
  */
54
- export declare function resolveDatabricksAbsolutePath(basePath: string, inputPath: string): string;
55
- /** Map an absolute Databricks path back to the workspace namespace. */
56
- export declare function toDatabricksWorkspacePath(basePath: string, absolutePath: string): string;
46
+ export declare function filesystems(fs: FileSystem, options?: MastraFileSystemAdapterOptions): MastraFileSystemAdapter;
57
47
  /**
58
- * Mastra filesystem provider that reads and writes through a Databricks
59
- * {@link WorkspaceClient}.
60
- *
61
- * Workspace paths are absolute within the namespace (`/notes.md` maps to
62
- * `<basePath>/notes.md`). Unity Catalog volumes use the Files API;
63
- * `/dbfs/...` paths use DBFS.
48
+ * Thin Mastra {@link MastraFilesystem} adapter over a `@dbx-tools/shared-fs`
49
+ * {@link FileSystem}. Identity fields are getters that read the source on
50
+ * access; construction does not snapshot them.
64
51
  */
65
- export declare class DatabricksWorkspaceFilesystem extends MastraFilesystem {
66
- readonly id: string;
67
- readonly name = "DatabricksWorkspaceFilesystem";
68
- readonly provider = "databricks";
69
- readonly basePath: string;
52
+ export declare class MastraFileSystemAdapter extends MastraFilesystem {
70
53
  status: ProviderStatus;
71
- private readonly client;
72
- private readonly mkdirs;
73
- private _readOnly;
74
- private _basePathMissing;
75
- get readOnly(): boolean | undefined;
76
- /**
77
- * @param options.client - Defaults to the AppKit execution-context client.
78
- * @param options.mkdirs - Default `"try"`; see {@link DatabricksMkdirsMode}.
79
- * @param options.readOnly - When omitted, {@link init} probes write access.
80
- */
81
- constructor(options: DatabricksWorkspaceFilesystemOptions);
82
- /** Resolve and sandbox a workspace-relative path under {@link basePath}. */
83
- private resolvePath;
84
- /** Map a Databricks absolute path back to the workspace namespace. */
85
- private workspacePath;
86
- /** Throw when the filesystem is read-only. */
87
- private assertWritable;
88
- /** Map SDK / HTTP errors to Mastra workspace filesystem errors. */
89
- private rethrow;
90
- /**
91
- * Probe whether {@link basePath} exists and cache the result on
92
- * {@link _basePathMissing}.
93
- */
94
- private resolveBasePathStatus;
95
- /** Delegate to {@link emptyFilesystem} when the base path is missing. */
96
- private emptyFallback;
54
+ private readonly fs;
55
+ private readonly options;
56
+ constructor(fs: FileSystem, options?: MastraFileSystemAdapterOptions);
57
+ get id(): string;
58
+ get name(): string;
59
+ get provider(): string;
60
+ get readOnly(): boolean;
61
+ get basePath(): string;
97
62
  init(): Promise<void>;
98
- /**
99
- * Write and delete a ephemeral probe file to detect read-only access when
100
- * {@link DatabricksWorkspaceFilesystemOptions.readOnly} was not set.
101
- *
102
- * @returns `true` when the probe write (and cleanup) succeeded.
103
- */
104
- private probeReadOnly;
105
63
  destroy(): Promise<void>;
106
- /** Probe that `absolutePath` exists (file or directory metadata). */
107
- private assertAbsoluteReadable;
108
- /** Read the full contents of `absolutePath` from the matching backend. */
109
- private readAbsolute;
110
- /** Write `buffer` to `absolutePath` on the matching backend. */
111
- private writeAbsolute;
112
- /** Upload a buffer to a Unity Catalog Files API path. */
113
- private uploadUcFile;
114
- /** Delete a single file at `absolutePath` (non-recursive). */
115
- private deleteAbsoluteFile;
116
- /**
117
- * Delete a file or directory at `absolutePath`.
118
- *
119
- * DBFS and workspace APIs accept `recursive`; UC volumes recurse manually.
120
- */
121
- private deleteAbsolutePath;
122
- /** Create `absolutePath` and any missing parents on the matching backend. */
123
- private mkdirAbsolute;
124
- private readDbfsFile;
125
- private readWorkspaceFile;
126
- private writeDbfsFile;
127
- private writeWorkspaceFile;
128
64
  getInfo(): FilesystemInfo<{
129
- basePath: string;
65
+ root: string;
66
+ backend: string;
130
67
  }>;
131
68
  getInstructions(): string;
132
- /** Map a workspace-relative path to the backing Databricks absolute path. */
133
- resolveAbsolutePath(inputPath: string): string | undefined;
134
- /** Read file contents from the workspace namespace. */
135
69
  readFile(inputPath: string, options?: ReadOptions): Promise<string | Buffer>;
136
- /** Write file contents into the workspace namespace. */
137
70
  writeFile(inputPath: string, content: FileContent, options?: WriteOptions): Promise<void>;
138
- /** Append to an existing file, creating it when missing. */
139
71
  appendFile(inputPath: string, content: FileContent): Promise<void>;
140
- /** Delete a file in the workspace namespace. */
141
72
  deleteFile(inputPath: string, options?: RemoveOptions): Promise<void>;
142
- /** Copy a file within the workspace namespace. */
143
73
  copyFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
144
- /** Move or rename a file within the workspace namespace. */
145
74
  moveFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
146
- /** Create a directory in the workspace namespace. */
147
75
  mkdir(inputPath: string, options?: {
148
76
  recursive?: boolean;
149
77
  }): Promise<void>;
150
- /** Remove a directory from the workspace namespace. */
151
78
  rmdir(inputPath: string, options?: RemoveOptions): Promise<void>;
152
- /**
153
- * Recursively delete a Unity Catalog directory tree.
154
- *
155
- * DBFS and workspace trees use native recursive delete via
156
- * {@link deleteAbsolutePath} instead.
157
- */
158
- private deleteUcDirectoryRecursive;
159
- /** List entries in a workspace directory. */
160
79
  readdir(inputPath: string, options?: ListOptions): Promise<FileEntry[]>;
161
- private readDirectoryRecursive;
162
- private listAbsoluteDirectory;
163
- /** Apply Mastra list filters (`extension`, etc.) to directory entries. */
164
- private filterEntries;
165
- /** Return whether `inputPath` exists in the workspace namespace. */
166
- exists(inputPath: string): Promise<boolean>;
167
- /** Return file or directory metadata for `inputPath`. */
168
- stat(inputPath: string): Promise<FileStat>;
169
- /**
170
- * Stat a Unity Catalog path by probing file metadata first, then
171
- * directory metadata.
172
- */
173
- private statUcAbsolute;
174
- }
175
- /**
176
- * Read-only in-memory {@link WorkspaceFilesystem} with a single empty root.
177
- * Use {@link emptyFilesystem} rather than constructing directly.
178
- */
179
- declare class EmptyFilesystem extends MastraFilesystem {
180
- readonly id = "empty-fs";
181
- readonly name = "EmptyFilesystem";
182
- readonly provider = "empty";
183
- readonly readOnly = true;
184
- status: ProviderStatus;
185
- constructor();
186
- init(): Promise<void>;
187
- destroy(): Promise<void>;
188
- getInfo(): FilesystemInfo;
189
- getInstructions(): string;
190
- private rootStat;
191
- private assertWritable;
192
- readFile(inputPath: string, _options?: ReadOptions): Promise<string | Buffer>;
193
- writeFile(_inputPath: string, _content: FileContent, _options?: WriteOptions): Promise<void>;
194
- appendFile(_inputPath: string, _content: FileContent): Promise<void>;
195
- deleteFile(inputPath: string, options?: RemoveOptions): Promise<void>;
196
- copyFile(src: string, _dest: string, _options?: CopyOptions): Promise<void>;
197
- moveFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
198
- mkdir(_inputPath: string, _options?: {
199
- recursive?: boolean;
200
- }): Promise<void>;
201
- rmdir(inputPath: string, options?: RemoveOptions): Promise<void>;
202
- readdir(inputPath: string, _options?: ListOptions): Promise<FileEntry[]>;
203
80
  exists(inputPath: string): Promise<boolean>;
204
81
  stat(inputPath: string): Promise<FileStat>;
82
+ private delegate;
83
+ /** {@link delegate} for a mutation: refuse a read-only mount before doing any work. */
84
+ private delegateWrite;
85
+ /** When Mastra asks for non-recursive writes, require the parent directory. */
86
+ private assertParentExists;
87
+ private rethrow;
205
88
  }
206
- /** Memoized singleton empty read-only filesystem for no-op mounts. */
207
- export declare const emptyFilesystem: () => EmptyFilesystem;
208
- export {};
89
+ /** Fresh writable local temp mount on a root unique to this call. */
90
+ export declare function scratchFilesystem(): MastraFileSystemAdapter;