web-dc-api 0.0.40 → 0.0.41
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/dist/dc.min.js +2 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +132 -24
- package/dist/index.esm.js +1 -1
- package/lib/common/define.ts +32 -22
- package/lib/common/module-system.ts +1 -0
- package/lib/common/service-worker.ts +2 -1
- package/lib/common/types/types.ts +7 -1
- package/lib/dc.ts +74 -17
- package/lib/implements/aiproxy/client.ts +17 -13
- package/lib/implements/file/client.ts +8 -8
- package/lib/implements/file/manager.ts +6 -2
- package/lib/implements/threaddb/core/logstore.ts +4 -4
- package/lib/implements/threaddb/lsstoreds/metadata.ts +23 -22
- package/lib/implements/threaddb/net/net.ts +0 -1
- package/lib/implements/wallet/manager.ts +287 -160
- package/lib/index.ts +15 -3
- package/lib/interfaces/file-interface.ts +1 -1
- package/lib/interfaces/util-interface.ts +1 -1
- package/lib/modules/aiproxy-module.ts +40 -15
- package/lib/modules/auth-module.ts +6 -6
- package/lib/modules/database-module.ts +51 -3
- package/lib/modules/file-module.ts +12 -7
- package/package.json +1 -1
package/lib/index.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
// 导出主要类和功能
|
|
3
3
|
// 在您库的入口文件开头添加
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface PromiseConstructor {
|
|
7
|
+
withResolvers<T = any>(): {
|
|
8
|
+
promise: Promise<T>;
|
|
9
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
10
|
+
reject: (reason?: any) => void;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
4
15
|
if (typeof Promise !== 'undefined' && !Promise.withResolvers) {
|
|
5
|
-
Promise.withResolvers = function() {
|
|
6
|
-
let resolve
|
|
7
|
-
|
|
16
|
+
Promise.withResolvers = function<T = any>() {
|
|
17
|
+
let resolve!: (value: T | PromiseLike<T>) => void;
|
|
18
|
+
let reject!: (reason?: any) => void;
|
|
19
|
+
const promise = new Promise<T>((res, rej) => {
|
|
8
20
|
resolve = res;
|
|
9
21
|
reject = rej;
|
|
10
22
|
});
|
|
@@ -11,7 +11,7 @@ import { AIProxyUserPermission } from "../common/constants";
|
|
|
11
11
|
const logger = createLogger('KeyValueModule');
|
|
12
12
|
|
|
13
13
|
import { IAICallConfig } from "../common/types/types";
|
|
14
|
-
import { IAIProxyOperations } from "lib/interfaces/aiproxy-interface";
|
|
14
|
+
import { IAIProxyOperations } from "../../lib/interfaces/aiproxy-interface";
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -61,16 +61,24 @@ export class AIProxyModule implements DCModule ,IAIProxyOperations{
|
|
|
61
61
|
appId: string,
|
|
62
62
|
configTheme: string,
|
|
63
63
|
): Promise<[number | null, Error | null]> {
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
try {
|
|
65
|
+
this.assertInitialized();
|
|
66
|
+
return this.aiProxyManager.createProxyConfig(appId, configTheme);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
return Promise.resolve([null, error as Error]);
|
|
69
|
+
}
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
async deleteProxyConfig(
|
|
69
73
|
appId: string,
|
|
70
74
|
configTheme: string,
|
|
71
75
|
): Promise<[number | null, Error | null]> {
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
try {
|
|
77
|
+
this.assertInitialized();
|
|
78
|
+
return this.aiProxyManager.deleteProxyConfig(appId, configTheme);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return Promise.resolve([null, error as Error]);
|
|
81
|
+
}
|
|
74
82
|
}
|
|
75
83
|
|
|
76
84
|
|
|
@@ -84,8 +92,12 @@ configAIProxy(
|
|
|
84
92
|
serviceConfig?: AIProxyConfig,
|
|
85
93
|
vaccount?: string
|
|
86
94
|
): Promise<[boolean | null, Error | null]> {
|
|
87
|
-
|
|
88
|
-
|
|
95
|
+
try {
|
|
96
|
+
this.assertInitialized();
|
|
97
|
+
return this.aiProxyManager.configAIProxy(appId, configAuthor, configTheme, serviceName, serviceConfig, vaccount);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return Promise.resolve([null, error as Error]);
|
|
100
|
+
}
|
|
89
101
|
}
|
|
90
102
|
|
|
91
103
|
|
|
@@ -99,8 +111,13 @@ permission: AIProxyUserPermission,
|
|
|
99
111
|
authConfig: ProxyCallConfig,
|
|
100
112
|
vaccount?: string
|
|
101
113
|
): Promise<[number | null, Error | null]> {
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
try {
|
|
115
|
+
this.assertInitialized();
|
|
116
|
+
return this.aiProxyManager.configAuth(appId, configAuthor, configTheme, authPubkey, permission, authConfig, vaccount);
|
|
117
|
+
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return Promise.resolve([null, error as Error]);
|
|
120
|
+
}
|
|
104
121
|
}
|
|
105
122
|
|
|
106
123
|
|
|
@@ -111,8 +128,12 @@ vaccount?: string
|
|
|
111
128
|
configTheme: string,
|
|
112
129
|
vaccount?: string
|
|
113
130
|
): Promise<[UserProxyCallConfig[] | null,AIProxyConfig[] | null, Error | null]> {
|
|
114
|
-
|
|
115
|
-
|
|
131
|
+
try {
|
|
132
|
+
this.assertInitialized();
|
|
133
|
+
return this.aiProxyManager.GetAIProxyConfig(appId, themeAuthor, configTheme, vaccount);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return Promise.resolve([null, null, error as Error]);
|
|
136
|
+
}
|
|
116
137
|
}
|
|
117
138
|
|
|
118
139
|
|
|
@@ -121,8 +142,12 @@ async GetUserOwnAIProxyAuth(
|
|
|
121
142
|
themeAuthor: string,
|
|
122
143
|
configTheme: string,
|
|
123
144
|
): Promise<[authConfig: ProxyCallConfig | null, error: Error | null]> {
|
|
124
|
-
|
|
125
|
-
|
|
145
|
+
try {
|
|
146
|
+
this.assertInitialized();
|
|
147
|
+
return this.aiProxyManager.GetUserOwnAIProxyAuth(appId, themeAuthor, configTheme);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
return Promise.resolve([null, error as Error]);
|
|
150
|
+
}
|
|
126
151
|
}
|
|
127
152
|
|
|
128
153
|
|
|
@@ -202,8 +227,8 @@ async GetUserOwnAIProxyAuth(
|
|
|
202
227
|
): Promise<Error | null> {
|
|
203
228
|
try{
|
|
204
229
|
this.assertInitialized();
|
|
205
|
-
|
|
206
|
-
|
|
230
|
+
this.aiCallConfig = callConfig
|
|
231
|
+
return null
|
|
207
232
|
} catch (error) {
|
|
208
233
|
logger.error("设置AI调用配置失败:", error);
|
|
209
234
|
return error as Error;
|
|
@@ -75,13 +75,13 @@ export class AuthModule implements DCModule, IAuthOperations {
|
|
|
75
75
|
* @returns 是否登录成功
|
|
76
76
|
*/
|
|
77
77
|
async accountLoginWithWalletCall(): Promise<Account | null> {
|
|
78
|
-
this.assertInitialized();
|
|
79
|
-
|
|
80
|
-
if (!this.context.connectedDc?.client) {
|
|
81
|
-
throw new Error("dcClient is null");
|
|
82
|
-
}
|
|
83
78
|
|
|
84
79
|
try {
|
|
80
|
+
this.assertInitialized();
|
|
81
|
+
|
|
82
|
+
if (!this.context.connectedDc?.client) {
|
|
83
|
+
throw new Error("dcClient is null");
|
|
84
|
+
}
|
|
85
85
|
const data = await this.walletManager.openConnect();
|
|
86
86
|
const publicKey = new Ed25519PubKey(data.appAccount);
|
|
87
87
|
this.context.publicKey = publicKey;
|
|
@@ -471,9 +471,9 @@ export class AuthModule implements DCModule, IAuthOperations {
|
|
|
471
471
|
* @param connectInfo 连接信息
|
|
472
472
|
*/
|
|
473
473
|
private async getTokenWithDCConnectInfo(connectInfo: any): Promise<void> {
|
|
474
|
-
this.assertInitialized();
|
|
475
474
|
|
|
476
475
|
try {
|
|
476
|
+
this.assertInitialized();
|
|
477
477
|
// 判断 client 是否为空
|
|
478
478
|
if (!connectInfo.client) {
|
|
479
479
|
return;
|
|
@@ -148,6 +148,54 @@ export class DatabaseModule implements DCModule, IDatabaseOperations {
|
|
|
148
148
|
return [null, error instanceof Error ? error : new Error(String(error))];
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
+
|
|
152
|
+
async loadVerno(threadId : string): Promise<number> {
|
|
153
|
+
try {
|
|
154
|
+
this.assertInitialized();
|
|
155
|
+
await this.initDBManager();
|
|
156
|
+
|
|
157
|
+
if (!this.context.dbManager) {
|
|
158
|
+
throw new Error("数据库管理器未初始化");
|
|
159
|
+
}
|
|
160
|
+
const tID = ThreadID.fromString(threadId);
|
|
161
|
+
const db = await this.context.dbManager.getDB(tID);
|
|
162
|
+
|
|
163
|
+
if (!db) {
|
|
164
|
+
logger.error(`数据库信息为空`);
|
|
165
|
+
return 0; // 返回默认版本号
|
|
166
|
+
}
|
|
167
|
+
const verno = await db.loadVerno();
|
|
168
|
+
return verno;
|
|
169
|
+
} catch (error) {
|
|
170
|
+
logger.error(`加载数据库版本号失败:`, error);
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async saveVerno(threadId: string, verno: number): Promise<number> {
|
|
176
|
+
try {
|
|
177
|
+
this.assertInitialized();
|
|
178
|
+
await this.initDBManager();
|
|
179
|
+
|
|
180
|
+
if (!this.context.dbManager) {
|
|
181
|
+
throw new Error("数据库管理器未初始化");
|
|
182
|
+
}
|
|
183
|
+
const tID = ThreadID.fromString(threadId);
|
|
184
|
+
const db = await this.context.dbManager.getDB(tID);
|
|
185
|
+
|
|
186
|
+
if (!db) {
|
|
187
|
+
logger.error(`数据库信息为空`);
|
|
188
|
+
return -1; //
|
|
189
|
+
}
|
|
190
|
+
await db.saveVerno(verno);
|
|
191
|
+
return 0;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
logger.error(`加载数据库版本号失败:`, error);
|
|
194
|
+
return -1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
151
199
|
|
|
152
200
|
/**
|
|
153
201
|
* 从DC同步数据库
|
|
@@ -204,9 +252,9 @@ export class DatabaseModule implements DCModule, IDatabaseOperations {
|
|
|
204
252
|
* @param threadid 数据库ID
|
|
205
253
|
* @returns 错误信息或null
|
|
206
254
|
*/
|
|
207
|
-
async refreshDBFromDC(threadid: string,
|
|
208
|
-
|
|
209
|
-
try {
|
|
255
|
+
async refreshDBFromDC(threadid: string, verno?: number): Promise<Error | null> {
|
|
256
|
+
|
|
257
|
+
try {
|
|
210
258
|
this.assertInitialized();
|
|
211
259
|
await this.initDBManager();
|
|
212
260
|
|
|
@@ -85,9 +85,9 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
85
85
|
* 获取可寻址文件流
|
|
86
86
|
*/
|
|
87
87
|
async getSeekableFileStream(ipfsPath: string, decryptKey: string): Promise<SeekableFileStream> {
|
|
88
|
-
this.assertInitialized();
|
|
89
88
|
|
|
90
89
|
try {
|
|
90
|
+
this.assertInitialized();
|
|
91
91
|
// 先查看缓存
|
|
92
92
|
const cachedStream = this.fileCacheManager.getCachedFileStream(ipfsPath, decryptKey);
|
|
93
93
|
if (cachedStream) {
|
|
@@ -114,8 +114,8 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
114
114
|
* 清理文件缓存
|
|
115
115
|
*/
|
|
116
116
|
clearFileCache(pathname?: string): void {
|
|
117
|
-
this.assertInitialized();
|
|
118
117
|
try {
|
|
118
|
+
this.assertInitialized();
|
|
119
119
|
this.fileCacheManager.clearFileCache(pathname);
|
|
120
120
|
} catch (error) {
|
|
121
121
|
logger.error(`清理文件缓存失败`, error);
|
|
@@ -125,17 +125,22 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
125
125
|
/**
|
|
126
126
|
* 获取缓存统计信息
|
|
127
127
|
*/
|
|
128
|
-
getCacheStats(): { total: number, keys: string[] } {
|
|
129
|
-
|
|
130
|
-
|
|
128
|
+
getCacheStats(): [{ total: number, keys: string[] } | null, Error | null] {
|
|
129
|
+
try {
|
|
130
|
+
this.assertInitialized();
|
|
131
|
+
const stats = this.fileCacheManager.getCacheStats();
|
|
132
|
+
return [stats, null];
|
|
133
|
+
} catch (error) {
|
|
134
|
+
return [null, error as Error];
|
|
135
|
+
}
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
/**
|
|
134
139
|
* 获取文件内容
|
|
135
140
|
*/
|
|
136
141
|
async getFile(cid: string, decryptKey: string): Promise<[Uint8Array | null, Error | null]> {
|
|
137
|
-
this.assertInitialized();
|
|
138
142
|
try {
|
|
143
|
+
this.assertInitialized();
|
|
139
144
|
const fileContent = await this.fileManager.getFileFromDc(cid, decryptKey);
|
|
140
145
|
return [fileContent, null];
|
|
141
146
|
} catch (error) {
|
|
@@ -151,8 +156,8 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
151
156
|
cid: string,
|
|
152
157
|
decryptKey: string
|
|
153
158
|
): Promise<ReadableStream<Uint8Array> | null> {
|
|
154
|
-
this.assertInitialized();
|
|
155
159
|
try {
|
|
160
|
+
this.assertInitialized();
|
|
156
161
|
const fileStream = await this.fileManager.createSeekableFileStream(
|
|
157
162
|
cid,
|
|
158
163
|
decryptKey
|