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/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, reject;
7
- const promise = new Promise((res, rej) => {
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
  });
@@ -26,7 +26,7 @@ export interface IFileOperations {
26
26
  * 获取缓存统计信息
27
27
  * @returns 包含缓存总数和键列表的统计对象
28
28
  */
29
- getCacheStats(): { total: number; keys: string[] };
29
+ getCacheStats(): [{ total: number, keys: string[] } | null, Error | null];
30
30
 
31
31
  /**
32
32
  * 获取文件内容
@@ -1,4 +1,4 @@
1
- import { IAppInfo } from 'lib/common/types/types';
1
+ import { IAppInfo } from '../../lib/common/types/types';
2
2
  import { SymmetricKey, Key as ThreadKey } from '../implements/threaddb/common/key';
3
3
 
4
4
 
@@ -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
- this.assertInitialized();
65
- return this.aiProxyManager.createProxyConfig(appId, configTheme);
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
- this.assertInitialized();
73
- return this.aiProxyManager.deleteProxyConfig(appId, configTheme);
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
- this.assertInitialized();
88
- return this.aiProxyManager.configAIProxy(appId, configAuthor, configTheme, serviceName, serviceConfig, vaccount);
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
- this.assertInitialized();
103
- return this.aiProxyManager.configAuth(appId, configAuthor, configTheme, authPubkey, permission, authConfig, vaccount);
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
- this.assertInitialized();
115
- return this.aiProxyManager.GetAIProxyConfig(appId, themeAuthor, configTheme, vaccount);
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
- this.assertInitialized();
125
- return this.aiProxyManager.GetUserOwnAIProxyAuth(appId, themeAuthor, configTheme);
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
- this.aiCallConfig = callConfig
206
- return null
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, ): Promise<Error | null> {
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
- this.assertInitialized();
130
- return this.fileCacheManager.getCacheStats();
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-dc-api",
3
- "version": "0.0.40",
3
+ "version": "0.0.41",
4
4
  "description": "web相关的dcapi",
5
5
  "type": "module",
6
6
  "browser": "dist/dc.min.js",