@aigne/afs 1.0.0
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.
- package/CHANGELOG.md +15 -0
- package/LICENSE.md +93 -0
- package/lib/cjs/afs.d.ts +26 -0
- package/lib/cjs/afs.js +126 -0
- package/lib/cjs/history/index.d.ts +14 -0
- package/lib/cjs/history/index.js +45 -0
- package/lib/cjs/index.d.ts +4 -0
- package/lib/cjs/index.js +20 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/storage/index.d.ts +24 -0
- package/lib/cjs/storage/index.js +72 -0
- package/lib/cjs/storage/migrate.d.ts +3 -0
- package/lib/cjs/storage/migrate.js +31 -0
- package/lib/cjs/storage/migrations/001-init.d.ts +2 -0
- package/lib/cjs/storage/migrations/001-init.js +25 -0
- package/lib/cjs/storage/models/entries.d.ts +199 -0
- package/lib/cjs/storage/models/entries.js +28 -0
- package/lib/cjs/storage/type.d.ts +23 -0
- package/lib/cjs/storage/type.js +2 -0
- package/lib/cjs/type.d.ts +54 -0
- package/lib/cjs/type.js +2 -0
- package/lib/dts/afs.d.ts +26 -0
- package/lib/dts/history/index.d.ts +14 -0
- package/lib/dts/index.d.ts +4 -0
- package/lib/dts/storage/index.d.ts +24 -0
- package/lib/dts/storage/migrate.d.ts +3 -0
- package/lib/dts/storage/migrations/001-init.d.ts +2 -0
- package/lib/dts/storage/models/entries.d.ts +199 -0
- package/lib/dts/storage/type.d.ts +23 -0
- package/lib/dts/type.d.ts +54 -0
- package/lib/esm/afs.d.ts +26 -0
- package/lib/esm/afs.js +122 -0
- package/lib/esm/history/index.d.ts +14 -0
- package/lib/esm/history/index.js +41 -0
- package/lib/esm/index.d.ts +4 -0
- package/lib/esm/index.js +4 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/storage/index.d.ts +24 -0
- package/lib/esm/storage/index.js +67 -0
- package/lib/esm/storage/migrate.d.ts +3 -0
- package/lib/esm/storage/migrate.js +28 -0
- package/lib/esm/storage/migrations/001-init.d.ts +2 -0
- package/lib/esm/storage/migrations/001-init.js +22 -0
- package/lib/esm/storage/models/entries.d.ts +199 -0
- package/lib/esm/storage/models/entries.js +23 -0
- package/lib/esm/storage/type.d.ts +23 -0
- package/lib/esm/storage/type.js +1 -0
- package/lib/esm/type.d.ts +54 -0
- package/lib/esm/type.js +1 -0
- package/package.json +69 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 (2025-10-07)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **afs:** add basic AFS(AIGNE File System) support ([#505](https://github.com/AIGNE-io/aigne-framework/issues/505)) ([ac2a18a](https://github.com/AIGNE-io/aigne-framework/commit/ac2a18a82470a2f31c466f329386525eb1cdab6d))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @aigne/sqlite bumped to 0.4.3-beta
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor’s trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
70
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
71
|
+
legal claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that
|
|
84
|
+
organization. **control** means ownership of substantially all the assets of an
|
|
85
|
+
entity, or the power to direct its management and policies by vote, contract, or
|
|
86
|
+
otherwise. Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/lib/cjs/afs.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Emitter } from "strict-event-emitter";
|
|
2
|
+
import { SharedAFSStorage, type SharedAFSStorageOptions } from "./storage/index.js";
|
|
3
|
+
import type { AFSStorage } from "./storage/type.js";
|
|
4
|
+
import type { AFSEntry, AFSListOptions, AFSModule, AFSRoot, AFSRootEvents, AFSSearchOptions, AFSWriteEntryPayload } from "./type.js";
|
|
5
|
+
export interface AFSOptions {
|
|
6
|
+
storage?: SharedAFSStorage | SharedAFSStorageOptions;
|
|
7
|
+
}
|
|
8
|
+
export declare class AFS extends Emitter<AFSRootEvents> implements AFSRoot {
|
|
9
|
+
moduleId: string;
|
|
10
|
+
path: string;
|
|
11
|
+
constructor(options?: AFSOptions);
|
|
12
|
+
private _storage;
|
|
13
|
+
storage(module: AFSModule): AFSStorage;
|
|
14
|
+
private modules;
|
|
15
|
+
use(module: AFSModule): this;
|
|
16
|
+
list(path: string, options?: AFSListOptions): Promise<{
|
|
17
|
+
list: AFSEntry[];
|
|
18
|
+
}>;
|
|
19
|
+
private findModules;
|
|
20
|
+
private isSubpath;
|
|
21
|
+
read(path: string): Promise<AFSEntry | undefined>;
|
|
22
|
+
write(path: string, content: AFSWriteEntryPayload): Promise<AFSEntry>;
|
|
23
|
+
search(path: string, query: string, options?: AFSSearchOptions): Promise<{
|
|
24
|
+
list: AFSEntry[];
|
|
25
|
+
}>;
|
|
26
|
+
}
|
package/lib/cjs/afs.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AFS = void 0;
|
|
4
|
+
const strict_event_emitter_1 = require("strict-event-emitter");
|
|
5
|
+
const ufo_1 = require("ufo");
|
|
6
|
+
const index_js_1 = require("./history/index.js");
|
|
7
|
+
const index_js_2 = require("./storage/index.js");
|
|
8
|
+
const DEFAULT_MAX_DEPTH = 5;
|
|
9
|
+
class AFS extends strict_event_emitter_1.Emitter {
|
|
10
|
+
moduleId = "AFSRoot";
|
|
11
|
+
path = "/";
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super();
|
|
14
|
+
this._storage =
|
|
15
|
+
options?.storage instanceof index_js_2.SharedAFSStorage
|
|
16
|
+
? options.storage
|
|
17
|
+
: new index_js_2.SharedAFSStorage(options?.storage);
|
|
18
|
+
this.use(new index_js_1.AFSHistory());
|
|
19
|
+
}
|
|
20
|
+
_storage;
|
|
21
|
+
storage(module) {
|
|
22
|
+
return this._storage.withModule(module);
|
|
23
|
+
}
|
|
24
|
+
modules = new Map();
|
|
25
|
+
use(module) {
|
|
26
|
+
this.modules.set(module.path, module);
|
|
27
|
+
module.onMount?.(this);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
async list(path, options) {
|
|
31
|
+
const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
32
|
+
if (!(maxDepth >= 0))
|
|
33
|
+
throw new Error(`Invalid maxDepth: ${maxDepth}`);
|
|
34
|
+
const results = [];
|
|
35
|
+
const modules = this.findModules(path);
|
|
36
|
+
for (const { module, subpath, mountPath } of modules) {
|
|
37
|
+
if (!module.list)
|
|
38
|
+
continue;
|
|
39
|
+
try {
|
|
40
|
+
const newMaxDepth = maxDepth - mountPath.split("/").filter(Boolean).length;
|
|
41
|
+
if (newMaxDepth < 0)
|
|
42
|
+
continue;
|
|
43
|
+
const { list } = await module.list(subpath, { ...options, maxDepth: newMaxDepth });
|
|
44
|
+
results.push(...list.map((entry) => ({
|
|
45
|
+
...entry,
|
|
46
|
+
path: (0, ufo_1.joinURL)(mountPath, entry.path),
|
|
47
|
+
})));
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
console.error(`Error listing from module at ${mountPath}`, error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { list: results };
|
|
54
|
+
}
|
|
55
|
+
findModules(fullPath) {
|
|
56
|
+
const modules = [];
|
|
57
|
+
for (const [mountPath, module] of this.modules) {
|
|
58
|
+
const match = this.isSubpath(fullPath, mountPath);
|
|
59
|
+
if (!match)
|
|
60
|
+
continue;
|
|
61
|
+
modules.push({ ...match, module, mountPath });
|
|
62
|
+
}
|
|
63
|
+
return modules.sort((a, b) => b.matchedDepth - a.matchedDepth);
|
|
64
|
+
}
|
|
65
|
+
isSubpath(fullPath, mountPath) {
|
|
66
|
+
const fullPathSegments = fullPath.split("/").filter(Boolean);
|
|
67
|
+
const mountPathSegments = mountPath.split("/").filter(Boolean);
|
|
68
|
+
const fp = fullPathSegments.join("/");
|
|
69
|
+
const mp = mountPathSegments.join("/");
|
|
70
|
+
if (fp.startsWith(mp)) {
|
|
71
|
+
return {
|
|
72
|
+
matchedDepth: mountPathSegments.length,
|
|
73
|
+
subpath: (0, ufo_1.joinURL)("/", ...fullPathSegments.slice(mountPathSegments.length)),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
else if (mp.startsWith(fp)) {
|
|
77
|
+
return {
|
|
78
|
+
matchedDepth: fullPathSegments.length,
|
|
79
|
+
subpath: (0, ufo_1.joinURL)("/", ...mountPathSegments.slice(fullPathSegments.length)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async read(path) {
|
|
84
|
+
const modules = this.findModules(path);
|
|
85
|
+
for (const { module, mountPath, subpath } of modules) {
|
|
86
|
+
const entry = await module.read?.(subpath);
|
|
87
|
+
if (entry) {
|
|
88
|
+
return {
|
|
89
|
+
...entry,
|
|
90
|
+
path: (0, ufo_1.joinURL)(mountPath, entry.path),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async write(path, content) {
|
|
96
|
+
const module = this.findModules(path)[0];
|
|
97
|
+
if (!module?.module.write)
|
|
98
|
+
throw new Error(`No module found for path: ${path}`);
|
|
99
|
+
const entry = await module.module.write(module.subpath, content);
|
|
100
|
+
return {
|
|
101
|
+
...entry,
|
|
102
|
+
path: (0, ufo_1.joinURL)(module.mountPath, entry.path),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async search(path, query, options) {
|
|
106
|
+
const results = [];
|
|
107
|
+
for (const { module, mountPath, subpath } of this.findModules(path)) {
|
|
108
|
+
if (mountPath.startsWith(path)) {
|
|
109
|
+
if (!module.search)
|
|
110
|
+
continue;
|
|
111
|
+
try {
|
|
112
|
+
const { list } = await module.search(subpath, query, options);
|
|
113
|
+
results.push(...list.map((entry) => ({
|
|
114
|
+
...entry,
|
|
115
|
+
path: (0, ufo_1.joinURL)(mountPath, entry.path),
|
|
116
|
+
})));
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.error(`Error searching in module at ${mountPath}`, error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { list: results };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.AFS = AFS;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AFSEntry, AFSListOptions, AFSModule, AFSRoot, AFSWriteEntryPayload } from "../type.js";
|
|
2
|
+
export declare class AFSHistory implements AFSModule {
|
|
3
|
+
static Path: string;
|
|
4
|
+
path: string;
|
|
5
|
+
moduleId: string;
|
|
6
|
+
private _afs?;
|
|
7
|
+
get afs(): AFSRoot;
|
|
8
|
+
onMount(afs: AFSRoot): void;
|
|
9
|
+
list(path: string, options?: AFSListOptions): Promise<{
|
|
10
|
+
list: AFSEntry[];
|
|
11
|
+
}>;
|
|
12
|
+
read(path: string): Promise<AFSEntry | undefined>;
|
|
13
|
+
write(path: string, content: AFSWriteEntryPayload): Promise<AFSEntry>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AFSHistory = void 0;
|
|
4
|
+
const uuid_1 = require("@aigne/uuid");
|
|
5
|
+
const ufo_1 = require("ufo");
|
|
6
|
+
class AFSHistory {
|
|
7
|
+
static Path = "/history";
|
|
8
|
+
path = AFSHistory.Path;
|
|
9
|
+
moduleId = "AFSHistory";
|
|
10
|
+
_afs;
|
|
11
|
+
get afs() {
|
|
12
|
+
if (!this._afs)
|
|
13
|
+
throw new Error("AFSHistory module is not mounted");
|
|
14
|
+
return this._afs;
|
|
15
|
+
}
|
|
16
|
+
onMount(afs) {
|
|
17
|
+
this._afs = afs;
|
|
18
|
+
afs.on("agentSucceed", ({ input, output }) => {
|
|
19
|
+
this.afs
|
|
20
|
+
.storage(this)
|
|
21
|
+
.create({
|
|
22
|
+
path: (0, ufo_1.joinURL)("/", (0, uuid_1.v7)()),
|
|
23
|
+
content: { input, output },
|
|
24
|
+
})
|
|
25
|
+
.then((entry) => {
|
|
26
|
+
afs.emit("historyCreated", { entry });
|
|
27
|
+
})
|
|
28
|
+
.catch((error) => {
|
|
29
|
+
console.error("Failed to store history entry", error);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async list(path, options) {
|
|
34
|
+
if (path !== "/")
|
|
35
|
+
return { list: [] };
|
|
36
|
+
return this.afs.storage(this).list(options);
|
|
37
|
+
}
|
|
38
|
+
async read(path) {
|
|
39
|
+
return this.afs.storage(this).read(path);
|
|
40
|
+
}
|
|
41
|
+
async write(path, content) {
|
|
42
|
+
return this.afs.storage(this).create({ ...content, path });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.AFSHistory = AFSHistory;
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./afs.js"), exports);
|
|
18
|
+
__exportStar(require("./history/index.js"), exports);
|
|
19
|
+
__exportStar(require("./storage/index.js"), exports);
|
|
20
|
+
__exportStar(require("./type.js"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { initDatabase } from "@aigne/sqlite";
|
|
2
|
+
import type { AFSEntry, AFSModule } from "../type.js";
|
|
3
|
+
import { entriesTable } from "./models/entries.js";
|
|
4
|
+
import type { AFSStorage, AFSStorageCreatePayload, AFSStorageListOptions } from "./type.js";
|
|
5
|
+
export interface SharedAFSStorageOptions {
|
|
6
|
+
url?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class SharedAFSStorage {
|
|
9
|
+
options?: SharedAFSStorageOptions | undefined;
|
|
10
|
+
constructor(options?: SharedAFSStorageOptions | undefined);
|
|
11
|
+
private _db;
|
|
12
|
+
get db(): Promise<import("drizzle-orm/libsql/driver-core.js").LibSQLDatabase<Record<string, never>> | import("drizzle-orm/sqlite-proxy/driver.js").SqliteRemoteDatabase<Record<string, never>>>;
|
|
13
|
+
withModule(module: AFSModule): AFSStorage;
|
|
14
|
+
}
|
|
15
|
+
export declare class AFSStorageWithModule implements AFSStorage {
|
|
16
|
+
private db;
|
|
17
|
+
private table;
|
|
18
|
+
constructor(db: ReturnType<typeof initDatabase>, table: Promise<ReturnType<typeof entriesTable>>);
|
|
19
|
+
list(options?: AFSStorageListOptions): Promise<{
|
|
20
|
+
list: AFSEntry[];
|
|
21
|
+
}>;
|
|
22
|
+
read(path: string): Promise<AFSEntry | undefined>;
|
|
23
|
+
create(entry: AFSStorageCreatePayload): Promise<AFSEntry>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AFSStorageWithModule = exports.SharedAFSStorage = void 0;
|
|
4
|
+
const sqlite_1 = require("@aigne/sqlite");
|
|
5
|
+
const migrate_js_1 = require("./migrate.js");
|
|
6
|
+
const entries_js_1 = require("./models/entries.js");
|
|
7
|
+
const DEFAULT_AFS_STORAGE_LIST_LIMIT = 10;
|
|
8
|
+
class SharedAFSStorage {
|
|
9
|
+
options;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
_db;
|
|
14
|
+
get db() {
|
|
15
|
+
this._db ??= (0, sqlite_1.initDatabase)({ url: this.options?.url });
|
|
16
|
+
return this._db;
|
|
17
|
+
}
|
|
18
|
+
withModule(module) {
|
|
19
|
+
return new AFSStorageWithModule(this.db, (0, migrate_js_1.migrate)(this.db, module).then(() => (0, entries_js_1.entriesTable)(module)));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SharedAFSStorage = SharedAFSStorage;
|
|
23
|
+
class AFSStorageWithModule {
|
|
24
|
+
db;
|
|
25
|
+
table;
|
|
26
|
+
constructor(db, table) {
|
|
27
|
+
this.db = db;
|
|
28
|
+
this.table = table;
|
|
29
|
+
}
|
|
30
|
+
async list(options = {}) {
|
|
31
|
+
const { filter, limit = DEFAULT_AFS_STORAGE_LIST_LIMIT } = options;
|
|
32
|
+
const db = await this.db;
|
|
33
|
+
const table = await this.table;
|
|
34
|
+
const list = await db
|
|
35
|
+
.select()
|
|
36
|
+
.from(table)
|
|
37
|
+
.where((0, sqlite_1.and)(filter?.userId ? (0, sqlite_1.eq)(table.userId, filter.userId) : undefined, filter?.sessionId ? (0, sqlite_1.eq)(table.sessionId, filter.sessionId) : undefined))
|
|
38
|
+
.orderBy(...(options.orderBy ?? []).map((item) => (item[1] === "asc" ? sqlite_1.asc : sqlite_1.desc)(sqlite_1.sql.identifier(item[0]))))
|
|
39
|
+
.limit(limit)
|
|
40
|
+
.execute();
|
|
41
|
+
return { list };
|
|
42
|
+
}
|
|
43
|
+
async read(path) {
|
|
44
|
+
const db = await this.db;
|
|
45
|
+
const table = await this.table;
|
|
46
|
+
return db
|
|
47
|
+
.select()
|
|
48
|
+
.from(table)
|
|
49
|
+
.where((0, sqlite_1.eq)(table.path, path))
|
|
50
|
+
.limit(1)
|
|
51
|
+
.execute()
|
|
52
|
+
.then((memory) => memory.at(0));
|
|
53
|
+
}
|
|
54
|
+
async create(entry) {
|
|
55
|
+
const db = await this.db;
|
|
56
|
+
const table = await this.table;
|
|
57
|
+
let result = await db
|
|
58
|
+
.update(table)
|
|
59
|
+
.set(entry)
|
|
60
|
+
.where((0, sqlite_1.eq)(table.path, entry.path))
|
|
61
|
+
.returning()
|
|
62
|
+
.execute();
|
|
63
|
+
if (!result.length) {
|
|
64
|
+
result = await db.insert(table).values(entry).returning().execute();
|
|
65
|
+
}
|
|
66
|
+
const [res] = result;
|
|
67
|
+
if (!res)
|
|
68
|
+
throw new Error("Failed to create AFS entry, no result");
|
|
69
|
+
return res;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.AFSStorageWithModule = AFSStorageWithModule;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.migrate = migrate;
|
|
4
|
+
const sqlite_1 = require("@aigne/sqlite");
|
|
5
|
+
const uuid_1 = require("@aigne/uuid");
|
|
6
|
+
const _001_init_js_1 = require("./migrations/001-init.js");
|
|
7
|
+
async function migrate(db, module) {
|
|
8
|
+
const migrations = [_001_init_js_1.init];
|
|
9
|
+
const migrationsTable = "__drizzle_migrations";
|
|
10
|
+
const migrationTableCreate = (0, sqlite_1.sql) `
|
|
11
|
+
CREATE TABLE IF NOT EXISTS ${sqlite_1.sql.identifier(migrationsTable)} (
|
|
12
|
+
"id" SERIAL PRIMARY KEY,
|
|
13
|
+
"moduleId" TEXT NOT NULL,
|
|
14
|
+
"hash" TEXT NOT NULL
|
|
15
|
+
)
|
|
16
|
+
`;
|
|
17
|
+
await (await db).run(migrationTableCreate).execute();
|
|
18
|
+
const dbMigrations = await (await db)
|
|
19
|
+
.values((0, sqlite_1.sql) `SELECT "id", "moduleId", "hash" FROM ${sqlite_1.sql.identifier(migrationsTable)} WHERE "moduleId" = ${sqlite_1.sql.param(module.moduleId)} ORDER BY id DESC LIMIT 1`)
|
|
20
|
+
.execute();
|
|
21
|
+
const lastDbMigration = dbMigrations[0];
|
|
22
|
+
const queriesToRun = [];
|
|
23
|
+
for (const migration of migrations) {
|
|
24
|
+
if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
|
|
25
|
+
queriesToRun.push(...migration.sql(module), (0, sqlite_1.sql) `INSERT INTO ${sqlite_1.sql.identifier(migrationsTable)} ("id", "moduleId", "hash") VALUES(${sqlite_1.sql.param((0, uuid_1.v7)())}, ${sqlite_1.sql.param(module.moduleId)}, ${sqlite_1.sql.param(migration.hash)})`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const query of queriesToRun) {
|
|
29
|
+
await (await db).run(query).execute();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.init = void 0;
|
|
4
|
+
const sqlite_1 = require("@aigne/sqlite");
|
|
5
|
+
const entries_js_1 = require("../models/entries.js");
|
|
6
|
+
exports.init = {
|
|
7
|
+
hash: "001-init",
|
|
8
|
+
sql: (module) => [
|
|
9
|
+
(0, sqlite_1.sql) `\
|
|
10
|
+
CREATE TABLE ${sqlite_1.sql.identifier((0, entries_js_1.entriesTableName)(module))} (
|
|
11
|
+
"id" TEXT NOT NULL PRIMARY KEY,
|
|
12
|
+
"createdAt" DATETIME NOT NULL,
|
|
13
|
+
"updatedAt" DATETIME NOT NULL,
|
|
14
|
+
"path" TEXT NOT NULL,
|
|
15
|
+
"userId" TEXT,
|
|
16
|
+
"sessionId" TEXT,
|
|
17
|
+
"summary" TEXT,
|
|
18
|
+
"metadata" JSON,
|
|
19
|
+
"linkTo" TEXT,
|
|
20
|
+
"content" JSON,
|
|
21
|
+
UNIQUE (path)
|
|
22
|
+
)
|
|
23
|
+
`,
|
|
24
|
+
],
|
|
25
|
+
};
|