web-dc-api 0.0.87 → 0.0.89
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/cjs/index.js +1 -0
- package/dist/cjs/protobuf-BVBdi7Hh.js +1 -0
- package/dist/dc.min.js +1 -19
- package/dist/esm/chunks/protobuf-CbxDm-Gy.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +947 -886
- package/package.json +14 -17
- package/dist/index.cjs.js +0 -19
- package/dist/index.esm.js +0 -19
- package/lib/common/blowfish/block.ts +0 -259
- package/lib/common/blowfish/cipher.ts +0 -144
- package/lib/common/blowfish/const.ts +0 -195
- package/lib/common/chain.ts +0 -469
- package/lib/common/commonclient.ts +0 -202
- package/lib/common/constants.ts +0 -55
- package/lib/common/dc-key/ed25519.ts +0 -343
- package/lib/common/dc-key/keyManager.ts +0 -424
- package/lib/common/dcapi.ts +0 -98
- package/lib/common/dcutil.ts +0 -595
- package/lib/common/define.ts +0 -66
- package/lib/common/error.ts +0 -67
- package/lib/common/grpc-dc.ts +0 -104
- package/lib/common/module-system.ts +0 -184
- package/lib/common/service-worker.ts +0 -234
- package/lib/common/types/types.ts +0 -344
- package/lib/dc.ts +0 -694
- package/lib/implements/account/client.ts +0 -185
- package/lib/implements/account/manager.ts +0 -683
- package/lib/implements/aiproxy/client.ts +0 -357
- package/lib/implements/aiproxy/manager.ts +0 -670
- package/lib/implements/cache/client.ts +0 -105
- package/lib/implements/cache/manager.ts +0 -127
- package/lib/implements/comment/client.ts +0 -982
- package/lib/implements/comment/manager.ts +0 -1151
- package/lib/implements/dc/client.ts +0 -51
- package/lib/implements/dc/manager.ts +0 -33
- package/lib/implements/file/client.ts +0 -253
- package/lib/implements/file/file-cache-manager.ts +0 -142
- package/lib/implements/file/manager.ts +0 -1240
- package/lib/implements/file/seekableFileStream.ts +0 -344
- package/lib/implements/file/streamwriter.ts +0 -322
- package/lib/implements/keyvalue/client.ts +0 -376
- package/lib/implements/keyvalue/manager.ts +0 -759
- package/lib/implements/message/client.ts +0 -250
- package/lib/implements/message/manager.ts +0 -215
- package/lib/implements/threaddb/cbor/coding.ts +0 -62
- package/lib/implements/threaddb/cbor/event.ts +0 -336
- package/lib/implements/threaddb/cbor/node.ts +0 -542
- package/lib/implements/threaddb/cbor/record.ts +0 -398
- package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
- package/lib/implements/threaddb/common/addrinfo.ts +0 -135
- package/lib/implements/threaddb/common/dispatcher.ts +0 -81
- package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
- package/lib/implements/threaddb/common/json-patcher.ts +0 -204
- package/lib/implements/threaddb/common/key.ts +0 -290
- package/lib/implements/threaddb/common/level-adapter.ts +0 -235
- package/lib/implements/threaddb/common/lineReader.ts +0 -79
- package/lib/implements/threaddb/common/logstore.ts +0 -215
- package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
- package/lib/implements/threaddb/core/app.ts +0 -202
- package/lib/implements/threaddb/core/core.ts +0 -230
- package/lib/implements/threaddb/core/db.ts +0 -249
- package/lib/implements/threaddb/core/event.ts +0 -54
- package/lib/implements/threaddb/core/head.ts +0 -89
- package/lib/implements/threaddb/core/identity.ts +0 -171
- package/lib/implements/threaddb/core/logstore.ts +0 -137
- package/lib/implements/threaddb/core/options.ts +0 -14
- package/lib/implements/threaddb/core/record.ts +0 -54
- package/lib/implements/threaddb/db/collection.ts +0 -1910
- package/lib/implements/threaddb/db/db.ts +0 -698
- package/lib/implements/threaddb/db/json2Query.ts +0 -192
- package/lib/implements/threaddb/db/query.ts +0 -524
- package/lib/implements/threaddb/dbclient.ts +0 -443
- package/lib/implements/threaddb/dbmanager.ts +0 -1901
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -452
- package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
- package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
- package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
- package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -280
- package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
- package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
- package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
- package/lib/implements/threaddb/net/define.ts +0 -138
- package/lib/implements/threaddb/net/grpcClient.ts +0 -582
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2006
- package/lib/implements/threaddb/pb/lstore.proto +0 -38
- package/lib/implements/threaddb/pb/lstore.ts +0 -393
- package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
- package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
- package/lib/implements/threaddb/pb/net.proto +0 -194
- package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
- package/lib/implements/threaddb/pb/net_pb.js +0 -5525
- package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
- package/lib/implements/util/client.ts +0 -72
- package/lib/implements/util/manager.ts +0 -146
- package/lib/implements/wallet/manager.ts +0 -664
- package/lib/index.ts +0 -57
- package/lib/interfaces/DCContext.ts +0 -51
- package/lib/interfaces/aiproxy-interface.ts +0 -145
- package/lib/interfaces/auth-interface.ts +0 -118
- package/lib/interfaces/cache-interface.ts +0 -22
- package/lib/interfaces/client-interface.ts +0 -11
- package/lib/interfaces/comment-interface.ts +0 -167
- package/lib/interfaces/database-interface.ts +0 -169
- package/lib/interfaces/file-interface.ts +0 -120
- package/lib/interfaces/index.ts +0 -10
- package/lib/interfaces/keyvalue-interface.ts +0 -156
- package/lib/interfaces/message-interface.ts +0 -22
- package/lib/interfaces/util-interface.ts +0 -31
- package/lib/modules/aiproxy-module.ts +0 -246
- package/lib/modules/auth-module.ts +0 -753
- package/lib/modules/cache-module.ts +0 -99
- package/lib/modules/client-module.ts +0 -71
- package/lib/modules/comment-module.ts +0 -429
- package/lib/modules/database-module.ts +0 -598
- package/lib/modules/file-module.ts +0 -291
- package/lib/modules/index.ts +0 -13
- package/lib/modules/keyvalue-module.ts +0 -379
- package/lib/modules/message-module.ts +0 -107
- package/lib/modules/util-module.ts +0 -148
- package/lib/polyfills/process-env-browser.ts +0 -1
- package/lib/proto/datasource.ts +0 -93
- package/lib/proto/dcnet.proto +0 -1601
- package/lib/proto/dcnet_proto.d.ts +0 -22857
- package/lib/proto/dcnet_proto.js +0 -55204
- package/lib/proto/dcnet_proto_sparse.js +0 -55166
- package/lib/proto/oidfetch.proto +0 -25
- package/lib/proto/oidfetch_proto.d.ts +0 -585
- package/lib/proto/oidfetch_proto.js +0 -1247
- package/lib/serverless/babel-browser.ts +0 -39
- package/lib/serverless/base_entity.ts +0 -78
- package/lib/serverless/base_repository.ts +0 -414
- package/lib/serverless/browser_schema_extractor.ts +0 -283
- package/lib/serverless/decorator_factory.ts +0 -322
- package/lib/util/BrowserLineReader.ts +0 -73
- package/lib/util/base64.ts +0 -105
- package/lib/util/bcrypt.ts +0 -206
- package/lib/util/curve25519Encryption.ts +0 -418
- package/lib/util/dccrypt.ts +0 -73
- package/lib/util/logger.ts +0 -104
- package/lib/util/utils.ts +0 -289
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
2
|
-
import type { Client } from "../../common/dcapi";
|
|
3
|
-
import { Libp2pGrpcClient } from "grpc-libp2p-client";
|
|
4
|
-
import { dcnet } from "../../proto/dcnet_proto";
|
|
5
|
-
import { base58btc } from "multiformats/bases/base58";
|
|
6
|
-
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
|
|
7
|
-
import { Errors } from "../../common/error";
|
|
8
|
-
import { DCContext } from "../../../lib/interfaces/DCContext";
|
|
9
|
-
import { AIStreamResponseFlag, OnStreamResponseType } from "../../common/types/types";
|
|
10
|
-
import { error } from "ajv/dist/vocabularies/applicator/dependencies";
|
|
11
|
-
|
|
12
|
-
export class AIProxyClient {
|
|
13
|
-
client: Client;
|
|
14
|
-
context: DCContext;
|
|
15
|
-
|
|
16
|
-
constructor(dcClient: Client, context: DCContext) {
|
|
17
|
-
this.client = dcClient;
|
|
18
|
-
this.context = context;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async GetAIProxyConfig(
|
|
22
|
-
appId: string,
|
|
23
|
-
themeAuthor: string,
|
|
24
|
-
configTheme: string,
|
|
25
|
-
): Promise<[proxyConfigCid: string,aesKey: string, error: Error | null]> {
|
|
26
|
-
const message = new dcnet.pb.GetAIProxyConfigRequest({});
|
|
27
|
-
message.appId = new TextEncoder().encode(appId);
|
|
28
|
-
message.themeAuthor = new TextEncoder().encode(themeAuthor);
|
|
29
|
-
message.theme = new TextEncoder().encode(configTheme);
|
|
30
|
-
|
|
31
|
-
const messageBytes = dcnet.pb.GetAIProxyConfigRequest.encode(message).finish();
|
|
32
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
33
|
-
this.client.p2pNode,
|
|
34
|
-
this.client.peerAddr,
|
|
35
|
-
this.client.token,
|
|
36
|
-
this.client.protocol
|
|
37
|
-
);
|
|
38
|
-
try {
|
|
39
|
-
const reply = await grpcClient.unaryCall(
|
|
40
|
-
"/dcnet.pb.Service/GetAIProxyConfig",
|
|
41
|
-
messageBytes,
|
|
42
|
-
30000
|
|
43
|
-
);
|
|
44
|
-
console.log("GetAIProxyConfig reply", reply);
|
|
45
|
-
const decoded = dcnet.pb.GetAIProxyConfigReply.decode(reply);
|
|
46
|
-
if (decoded.flag != 0) {
|
|
47
|
-
throw new Error(Errors.INVALID_TOKEN.message+" flag:"+decoded.flag);
|
|
48
|
-
}
|
|
49
|
-
const proxyConfigCid = new TextDecoder().decode(decoded.proxyConfigCid);
|
|
50
|
-
const aesKey = new TextDecoder().decode(decoded.aeskey);
|
|
51
|
-
return [proxyConfigCid,aesKey, null];
|
|
52
|
-
} catch (error) {
|
|
53
|
-
if (
|
|
54
|
-
typeof error === "object" &&
|
|
55
|
-
error !== null &&
|
|
56
|
-
"message" in error &&
|
|
57
|
-
typeof (error as any).message === "string" &&
|
|
58
|
-
(error as any).message.indexOf(Errors.INVALID_TOKEN.message) != -1
|
|
59
|
-
) {
|
|
60
|
-
// try to get token
|
|
61
|
-
const token = await this.client.GetToken(
|
|
62
|
-
this.context.appInfo.appId || "",
|
|
63
|
-
this.context.getPublicKey().string(),
|
|
64
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
65
|
-
return this.context.sign(payload);
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
if (!token) {
|
|
69
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
70
|
-
}
|
|
71
|
-
const reply = await grpcClient.unaryCall(
|
|
72
|
-
"/dcnet.pb.Service/GetAIProxyConfig",
|
|
73
|
-
messageBytes,
|
|
74
|
-
30000
|
|
75
|
-
);
|
|
76
|
-
console.log("GetAIProxyConfig reply", reply);
|
|
77
|
-
const decoded = dcnet.pb.GetAIProxyConfigReply.decode(reply);
|
|
78
|
-
console.log("GetAIProxyConfig decoded", decoded);
|
|
79
|
-
if (decoded.flag != 0) {
|
|
80
|
-
throw new Error(Errors.INVALID_TOKEN.message+" flag:"+decoded.flag);
|
|
81
|
-
}
|
|
82
|
-
const proxyConfigCid = new TextDecoder().decode(decoded.proxyConfigCid);
|
|
83
|
-
const aesKey = new TextDecoder().decode(decoded.aeskey);
|
|
84
|
-
return [proxyConfigCid,aesKey, null];
|
|
85
|
-
}
|
|
86
|
-
console.error("GetAIProxyConfig error:", error);
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
async GetUserOwnAIProxyAuth(
|
|
94
|
-
appId: string,
|
|
95
|
-
themeAuthor: string,
|
|
96
|
-
configTheme: string,
|
|
97
|
-
): Promise<[authInfo: string, error: Error | null]> {
|
|
98
|
-
const message = new dcnet.pb.GetUserOwnAIProxyAuthRequest({});
|
|
99
|
-
message.appId = new TextEncoder().encode(appId);
|
|
100
|
-
message.themeAuthor = new TextEncoder().encode(themeAuthor);
|
|
101
|
-
message.theme = new TextEncoder().encode(configTheme);
|
|
102
|
-
const messageBytes = dcnet.pb.GetUserOwnAIProxyAuthRequest.encode(message).finish();
|
|
103
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
104
|
-
this.client.p2pNode,
|
|
105
|
-
this.client.peerAddr,
|
|
106
|
-
this.client.token,
|
|
107
|
-
this.client.protocol
|
|
108
|
-
);
|
|
109
|
-
try {
|
|
110
|
-
const reply = await grpcClient.unaryCall(
|
|
111
|
-
"/dcnet.pb.Service/GetUserOwnAIProxyAuth",
|
|
112
|
-
messageBytes,
|
|
113
|
-
30000
|
|
114
|
-
);
|
|
115
|
-
console.log("GetUserOwnAIProxyAuth reply", reply);
|
|
116
|
-
const decoded = dcnet.pb.GetUserOwnAIProxyAuthReply.decode(reply);
|
|
117
|
-
if (decoded.flag != 0) {
|
|
118
|
-
throw new Error(Errors.INVALID_TOKEN.message+" flag:"+decoded.flag);
|
|
119
|
-
}
|
|
120
|
-
const authInfo = new TextDecoder().decode(decoded.authInfo);
|
|
121
|
-
return [authInfo, null];
|
|
122
|
-
} catch (error) {
|
|
123
|
-
if (
|
|
124
|
-
typeof error === "object" &&
|
|
125
|
-
error !== null &&
|
|
126
|
-
"message" in error &&
|
|
127
|
-
typeof (error as any).message === "string" &&
|
|
128
|
-
(error as any).message.indexOf(Errors.INVALID_TOKEN.message) != -1
|
|
129
|
-
) {
|
|
130
|
-
// try to get token
|
|
131
|
-
const token = await this.client.GetToken(
|
|
132
|
-
this.context.appInfo.appId || "",
|
|
133
|
-
this.context.getPublicKey().string(),
|
|
134
|
-
(payload: Uint8Array): Promise<Uint8Array> => {
|
|
135
|
-
return this.context.sign(payload);
|
|
136
|
-
}
|
|
137
|
-
);
|
|
138
|
-
if (!token) {
|
|
139
|
-
throw new Error(Errors.INVALID_TOKEN.message);
|
|
140
|
-
}
|
|
141
|
-
const reply = await grpcClient.unaryCall(
|
|
142
|
-
"/dcnet.pb.Service/GetUserOwnAIProxyAuth",
|
|
143
|
-
messageBytes,
|
|
144
|
-
30000
|
|
145
|
-
);
|
|
146
|
-
console.log("GetUserOwnAIProxyAuth reply", reply);
|
|
147
|
-
const decoded = dcnet.pb.GetUserOwnAIProxyAuthReply.decode(reply);
|
|
148
|
-
console.log("GetUserOwnAIProxyAuth decoded", decoded);
|
|
149
|
-
if (decoded.flag != 0) {
|
|
150
|
-
return ["", new Error(Errors.INVALID_TOKEN.message+" flag:"+decoded.flag)];
|
|
151
|
-
}
|
|
152
|
-
const authInfo = new TextDecoder().decode(decoded.authInfo);
|
|
153
|
-
return [authInfo, null];
|
|
154
|
-
}
|
|
155
|
-
return ["", error instanceof Error ? error : new Error(String(error))];
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
async DoAIProxyCall(
|
|
161
|
-
context: { signal?: AbortSignal },
|
|
162
|
-
appId: string,
|
|
163
|
-
themeAuthor: string,
|
|
164
|
-
configTheme: string,
|
|
165
|
-
serviceName: string,
|
|
166
|
-
path: string,
|
|
167
|
-
headers: string,
|
|
168
|
-
reqBody: string,
|
|
169
|
-
model: string,
|
|
170
|
-
forceRefresh: number,
|
|
171
|
-
blockHeight: number,
|
|
172
|
-
signature: Uint8Array,
|
|
173
|
-
onStreamResponse: OnStreamResponseType | null = null): Promise<number> {
|
|
174
|
-
|
|
175
|
-
const message = new dcnet.pb.DoAIProxyCallRequest({});
|
|
176
|
-
message.appId = new TextEncoder().encode(appId);
|
|
177
|
-
message.themeAuthor = new TextEncoder().encode(themeAuthor);
|
|
178
|
-
message.theme = new TextEncoder().encode(configTheme);
|
|
179
|
-
message.configKey = new TextEncoder().encode(serviceName);
|
|
180
|
-
message.path = new TextEncoder().encode(path);
|
|
181
|
-
message.headers = new TextEncoder().encode(headers);
|
|
182
|
-
message.reqBody = new TextEncoder().encode(reqBody);
|
|
183
|
-
message.modelConfig = new TextEncoder().encode(model);
|
|
184
|
-
message.forceRefresh = forceRefresh;
|
|
185
|
-
message.blockheight = blockHeight;
|
|
186
|
-
message.signature = signature;
|
|
187
|
-
|
|
188
|
-
const messageBytes = dcnet.pb.DoAIProxyCallRequest.encode(message).finish();
|
|
189
|
-
const grpcClient = new Libp2pGrpcClient(
|
|
190
|
-
this.client.p2pNode,
|
|
191
|
-
this.client.peerAddr,
|
|
192
|
-
this.client.token,
|
|
193
|
-
this.client.protocol
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
// 添加超时检测相关变量
|
|
197
|
-
let hasStartedReceiving = false;
|
|
198
|
-
let timeoutTimer: NodeJS.Timeout | null = null;
|
|
199
|
-
let isCompleted = false;
|
|
200
|
-
let isAborted = false;
|
|
201
|
-
|
|
202
|
-
// 清理定时器的函数
|
|
203
|
-
const clearTimeoutTimer = () => {
|
|
204
|
-
if (timeoutTimer) {
|
|
205
|
-
clearTimeout(timeoutTimer);
|
|
206
|
-
timeoutTimer = null;
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
// 设置超时检测定时器
|
|
211
|
-
const setTimeoutTimer = () => {
|
|
212
|
-
clearTimeoutTimer();
|
|
213
|
-
timeoutTimer = setTimeout(() => {
|
|
214
|
-
if (!isCompleted && !isAborted && hasStartedReceiving) {
|
|
215
|
-
console.warn('DoAIProxyCall 数据流超时,超过30秒未收到数据');
|
|
216
|
-
isAborted = true;
|
|
217
|
-
|
|
218
|
-
// 通知调用者超时
|
|
219
|
-
if (onStreamResponse) {
|
|
220
|
-
onStreamResponse(AIStreamResponseFlag.STREAM_HANG, "", "数据流超时:超过30秒未收到响应数据");
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}, 60000); // 60秒超时
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
// 检查是否已经被中止
|
|
227
|
-
const checkAborted = () => {
|
|
228
|
-
if (context.signal?.aborted) {
|
|
229
|
-
isAborted = true;
|
|
230
|
-
clearTimeoutTimer();
|
|
231
|
-
return true;
|
|
232
|
-
}
|
|
233
|
-
return false;
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
const onDataCallback = async (payload: Uint8Array) => {
|
|
237
|
-
if (isAborted || isCompleted || checkAborted()) return;
|
|
238
|
-
|
|
239
|
-
// 更新最后接收数据的时间
|
|
240
|
-
hasStartedReceiving = true;
|
|
241
|
-
|
|
242
|
-
// 重置超时定时器
|
|
243
|
-
setTimeoutTimer();
|
|
244
|
-
|
|
245
|
-
try {
|
|
246
|
-
const decodedPayload = dcnet.pb.DoAIProxyCallReply.decode(payload);
|
|
247
|
-
if (decodedPayload.flag == AIStreamResponseFlag.CONNECTION_CLOSED) {
|
|
248
|
-
isCompleted = true; // 如果不是流式响应,标记为完成
|
|
249
|
-
clearTimeoutTimer();
|
|
250
|
-
console.log('*********** CONNECTION_CLOSED ***********222');
|
|
251
|
-
}
|
|
252
|
-
if (onStreamResponse) {
|
|
253
|
-
onStreamResponse(
|
|
254
|
-
decodedPayload.flag,
|
|
255
|
-
new TextDecoder().decode(decodedPayload.content),
|
|
256
|
-
new TextDecoder().decode(decodedPayload.err)
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
} catch (error) {
|
|
260
|
-
console.error('DoAIProxyCall 解析数据失败:', error);
|
|
261
|
-
if (onStreamResponse) {
|
|
262
|
-
onStreamResponse(AIStreamResponseFlag.OTHER_ERROR, "", error instanceof Error ? error.message : String(error));
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const onEndCallback = async () => {
|
|
268
|
-
clearTimeoutTimer();
|
|
269
|
-
if (isAborted || checkAborted() || isCompleted) return;
|
|
270
|
-
isCompleted = true;
|
|
271
|
-
if (onStreamResponse) {
|
|
272
|
-
onStreamResponse(AIStreamResponseFlag.CONNECTION_CLOSED, "", "");
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const onErrorCallback = async (error: unknown) => {
|
|
277
|
-
if (isAborted || checkAborted() || isCompleted) return;
|
|
278
|
-
//不要标记完成,和清理定时器,统一由onEndCallback处理
|
|
279
|
-
if (onStreamResponse) {
|
|
280
|
-
onStreamResponse(AIStreamResponseFlag.OTHER_ERROR, "", error instanceof Error ? error.message : String(error));
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
// 监听外部中止信号 - 这不会拦截信号,只是添加监听器
|
|
285
|
-
let abortListener: (() => void) | null = null;
|
|
286
|
-
if (context.signal) {
|
|
287
|
-
// 先检查是否已经被中止
|
|
288
|
-
if (context.signal.aborted) {
|
|
289
|
-
isAborted = true;
|
|
290
|
-
clearTimeoutTimer();
|
|
291
|
-
console.log('DoAIProxyCall 调用前已被中止');
|
|
292
|
-
return AIStreamResponseFlag.EXTERNAL_EXIT;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
abortListener = () => {
|
|
296
|
-
isAborted = true;
|
|
297
|
-
clearTimeoutTimer();
|
|
298
|
-
console.log('DoAIProxyCall 被外部中止');
|
|
299
|
-
if (abortListener && context.signal) {//确保只触发一次
|
|
300
|
-
context.signal.removeEventListener('abort', abortListener);
|
|
301
|
-
}
|
|
302
|
-
// 通知调用者已被中止
|
|
303
|
-
if (onStreamResponse) {
|
|
304
|
-
onStreamResponse(AIStreamResponseFlag.EXTERNAL_EXIT, "", "调用被用户中止");
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
context.signal.addEventListener('abort', abortListener);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
try {
|
|
312
|
-
// 开始调用前设置初始超时定时器(用于检测是否开始接收数据)
|
|
313
|
-
setTimeoutTimer();
|
|
314
|
-
|
|
315
|
-
// 将完整的 context 传递给 grpcClient.Call,让它自己处理中止信号
|
|
316
|
-
await grpcClient.Call(
|
|
317
|
-
"/dcnet.pb.Service/DoAIProxyCall",
|
|
318
|
-
messageBytes,
|
|
319
|
-
3600 * 1000,
|
|
320
|
-
"server-streaming",
|
|
321
|
-
onDataCallback,
|
|
322
|
-
undefined, // dataSourceCallback not needed for server-streaming
|
|
323
|
-
onEndCallback,
|
|
324
|
-
onErrorCallback,
|
|
325
|
-
context // 完整传递 context,包括 signal
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
return 0;
|
|
329
|
-
} catch (error) {
|
|
330
|
-
isCompleted = true;
|
|
331
|
-
clearTimeoutTimer();
|
|
332
|
-
|
|
333
|
-
console.error("DoAIProxyCall error:", error);
|
|
334
|
-
|
|
335
|
-
// 如果是超时导致的错误,返回特定的错误码
|
|
336
|
-
if (isAborted) {
|
|
337
|
-
return AIStreamResponseFlag.STREAM_HANG;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// 检查是否是中止导致的错误
|
|
341
|
-
if (context.signal?.aborted) {
|
|
342
|
-
return AIStreamResponseFlag.EXTERNAL_EXIT;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
throw error;
|
|
346
|
-
} finally {
|
|
347
|
-
// 确保定时器被清理
|
|
348
|
-
clearTimeoutTimer();
|
|
349
|
-
|
|
350
|
-
// 清理事件监听器
|
|
351
|
-
if (abortListener && context.signal) {
|
|
352
|
-
context.signal.removeEventListener('abort', abortListener);
|
|
353
|
-
}
|
|
354
|
-
// 无需关闭 gRPC 客户端连接,因为 Libp2pGrpcClient 没有 close 方法
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|