@cloudbase/agent-adapter-adp 0.0.14 → 0.0.16
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/index.d.mts +36 -5
- package/dist/index.d.ts +36 -5
- package/dist/index.js +274 -70
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +274 -70
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/CHANGELOG.md +0 -48
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent, AgentConfig, RunAgentInput, EventType } from '@ag-ui/client';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
+
import { lke } from 'tencentcloud-sdk-nodejs-lke';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* 文件信息
|
|
@@ -244,24 +245,50 @@ interface ThoughtProcedure {
|
|
|
244
245
|
interface Debugging {
|
|
245
246
|
content: string;
|
|
246
247
|
}
|
|
248
|
+
interface DocumentParseRequest {
|
|
249
|
+
sessionId: string;
|
|
250
|
+
botAppKey: string;
|
|
251
|
+
requestId: string;
|
|
252
|
+
cosBucket: string;
|
|
253
|
+
fileType: string;
|
|
254
|
+
fileName: string;
|
|
255
|
+
cosUrl: string;
|
|
256
|
+
cosHash: string;
|
|
257
|
+
eTag: string;
|
|
258
|
+
size: string;
|
|
259
|
+
}
|
|
260
|
+
interface DocumentParseChunk {
|
|
261
|
+
type: "parsing";
|
|
262
|
+
payload: ParsingEvent;
|
|
263
|
+
}
|
|
264
|
+
interface ParsingEvent {
|
|
265
|
+
session_id: string;
|
|
266
|
+
trace_id: string;
|
|
267
|
+
is_final: boolean;
|
|
268
|
+
doc_id: string;
|
|
269
|
+
process: number;
|
|
270
|
+
status: string;
|
|
271
|
+
timestamp: number;
|
|
272
|
+
error_message: string;
|
|
273
|
+
}
|
|
247
274
|
|
|
248
275
|
declare class AdpAgentError extends Error {
|
|
249
276
|
code?: string;
|
|
250
277
|
constructor(message: string, code?: string);
|
|
251
278
|
}
|
|
279
|
+
type CosParams = Awaited<ReturnType<typeof lke.v20231130.Client.prototype.DescribeStorageCredential>>;
|
|
252
280
|
declare class AdpAgent extends AbstractAgent {
|
|
253
281
|
private reqAppClient;
|
|
254
282
|
private reqLkeClient;
|
|
255
283
|
protected adpConfig: AdpConfig;
|
|
284
|
+
private finalAppKey;
|
|
256
285
|
constructor(config: AgentConfig & {
|
|
257
286
|
adpConfig: AdpConfig;
|
|
258
287
|
});
|
|
259
|
-
generateRequestBody({ message, fileInfos,
|
|
288
|
+
generateRequestBody({ message, fileInfos, input, }: {
|
|
260
289
|
message: string;
|
|
261
290
|
fileInfos: FileInfo[];
|
|
262
|
-
|
|
263
|
-
threadId: string;
|
|
264
|
-
forwardedProps: Record<string, string>;
|
|
291
|
+
input: RunAgentInput;
|
|
265
292
|
}): AdpChatRequest;
|
|
266
293
|
run(input: RunAgentInput): Observable<{
|
|
267
294
|
type: EventType;
|
|
@@ -269,6 +296,10 @@ declare class AdpAgent extends AbstractAgent {
|
|
|
269
296
|
rawEvent?: any;
|
|
270
297
|
}>;
|
|
271
298
|
private _run;
|
|
299
|
+
private convertAGUIMessagesToAdpMessages;
|
|
300
|
+
private extractDocuments;
|
|
301
|
+
private getCosParams;
|
|
302
|
+
private uploadToCos;
|
|
272
303
|
}
|
|
273
304
|
|
|
274
|
-
export { AdpAgent, AdpAgentError, type AdpChatRequest, type AdpChunk, type AdpConfig, type CurrentNode, CurrentNodeStatus, type Debugging, type ErrorInfo, type FileInfo, type Knowledge, type QuoteInfo, type Reference, type ReferenceEvent, type ReplyEvent, ReplyMethod, type StatisticInfo, type TaskFlow, type ThoughtEvent, type ThoughtProcedure, type TokenStatEvent, type TokenStatProcedure, type WorkFlow };
|
|
305
|
+
export { AdpAgent, AdpAgentError, type AdpChatRequest, type AdpChunk, type AdpConfig, type CosParams, type CurrentNode, CurrentNodeStatus, type Debugging, type DocumentParseChunk, type DocumentParseRequest, type ErrorInfo, type FileInfo, type Knowledge, type ParsingEvent, type QuoteInfo, type Reference, type ReferenceEvent, type ReplyEvent, ReplyMethod, type StatisticInfo, type TaskFlow, type ThoughtEvent, type ThoughtProcedure, type TokenStatEvent, type TokenStatProcedure, type WorkFlow };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent, AgentConfig, RunAgentInput, EventType } from '@ag-ui/client';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
+
import { lke } from 'tencentcloud-sdk-nodejs-lke';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* 文件信息
|
|
@@ -244,24 +245,50 @@ interface ThoughtProcedure {
|
|
|
244
245
|
interface Debugging {
|
|
245
246
|
content: string;
|
|
246
247
|
}
|
|
248
|
+
interface DocumentParseRequest {
|
|
249
|
+
sessionId: string;
|
|
250
|
+
botAppKey: string;
|
|
251
|
+
requestId: string;
|
|
252
|
+
cosBucket: string;
|
|
253
|
+
fileType: string;
|
|
254
|
+
fileName: string;
|
|
255
|
+
cosUrl: string;
|
|
256
|
+
cosHash: string;
|
|
257
|
+
eTag: string;
|
|
258
|
+
size: string;
|
|
259
|
+
}
|
|
260
|
+
interface DocumentParseChunk {
|
|
261
|
+
type: "parsing";
|
|
262
|
+
payload: ParsingEvent;
|
|
263
|
+
}
|
|
264
|
+
interface ParsingEvent {
|
|
265
|
+
session_id: string;
|
|
266
|
+
trace_id: string;
|
|
267
|
+
is_final: boolean;
|
|
268
|
+
doc_id: string;
|
|
269
|
+
process: number;
|
|
270
|
+
status: string;
|
|
271
|
+
timestamp: number;
|
|
272
|
+
error_message: string;
|
|
273
|
+
}
|
|
247
274
|
|
|
248
275
|
declare class AdpAgentError extends Error {
|
|
249
276
|
code?: string;
|
|
250
277
|
constructor(message: string, code?: string);
|
|
251
278
|
}
|
|
279
|
+
type CosParams = Awaited<ReturnType<typeof lke.v20231130.Client.prototype.DescribeStorageCredential>>;
|
|
252
280
|
declare class AdpAgent extends AbstractAgent {
|
|
253
281
|
private reqAppClient;
|
|
254
282
|
private reqLkeClient;
|
|
255
283
|
protected adpConfig: AdpConfig;
|
|
284
|
+
private finalAppKey;
|
|
256
285
|
constructor(config: AgentConfig & {
|
|
257
286
|
adpConfig: AdpConfig;
|
|
258
287
|
});
|
|
259
|
-
generateRequestBody({ message, fileInfos,
|
|
288
|
+
generateRequestBody({ message, fileInfos, input, }: {
|
|
260
289
|
message: string;
|
|
261
290
|
fileInfos: FileInfo[];
|
|
262
|
-
|
|
263
|
-
threadId: string;
|
|
264
|
-
forwardedProps: Record<string, string>;
|
|
291
|
+
input: RunAgentInput;
|
|
265
292
|
}): AdpChatRequest;
|
|
266
293
|
run(input: RunAgentInput): Observable<{
|
|
267
294
|
type: EventType;
|
|
@@ -269,6 +296,10 @@ declare class AdpAgent extends AbstractAgent {
|
|
|
269
296
|
rawEvent?: any;
|
|
270
297
|
}>;
|
|
271
298
|
private _run;
|
|
299
|
+
private convertAGUIMessagesToAdpMessages;
|
|
300
|
+
private extractDocuments;
|
|
301
|
+
private getCosParams;
|
|
302
|
+
private uploadToCos;
|
|
272
303
|
}
|
|
273
304
|
|
|
274
|
-
export { AdpAgent, AdpAgentError, type AdpChatRequest, type AdpChunk, type AdpConfig, type CurrentNode, CurrentNodeStatus, type Debugging, type ErrorInfo, type FileInfo, type Knowledge, type QuoteInfo, type Reference, type ReferenceEvent, type ReplyEvent, ReplyMethod, type StatisticInfo, type TaskFlow, type ThoughtEvent, type ThoughtProcedure, type TokenStatEvent, type TokenStatProcedure, type WorkFlow };
|
|
305
|
+
export { AdpAgent, AdpAgentError, type AdpChatRequest, type AdpChunk, type AdpConfig, type CosParams, type CurrentNode, CurrentNodeStatus, type Debugging, type DocumentParseChunk, type DocumentParseRequest, type ErrorInfo, type FileInfo, type Knowledge, type ParsingEvent, type QuoteInfo, type Reference, type ReferenceEvent, type ReplyEvent, ReplyMethod, type StatisticInfo, type TaskFlow, type ThoughtEvent, type ThoughtProcedure, type TokenStatEvent, type TokenStatProcedure, type WorkFlow };
|
package/dist/index.js
CHANGED
|
@@ -65,7 +65,7 @@ function camelToSnakeKeys(obj) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// src/constant.ts
|
|
68
|
-
var
|
|
68
|
+
var DOCUMENT_MIME_TYPES = {
|
|
69
69
|
"text/plain": "txt",
|
|
70
70
|
"application/msword": "doc",
|
|
71
71
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
@@ -73,6 +73,15 @@ var MIME_TYPES = {
|
|
|
73
73
|
"application/vnd.ms-powerpoint": "ppt",
|
|
74
74
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx"
|
|
75
75
|
};
|
|
76
|
+
var IMAGE_MIME_TYPES = {
|
|
77
|
+
"image/png": "png",
|
|
78
|
+
"image/jpeg": "jpg",
|
|
79
|
+
"image/bmp": "bmp"
|
|
80
|
+
};
|
|
81
|
+
var MIME_TYPES = {
|
|
82
|
+
...DOCUMENT_MIME_TYPES,
|
|
83
|
+
...IMAGE_MIME_TYPES
|
|
84
|
+
};
|
|
76
85
|
|
|
77
86
|
// src/types.ts
|
|
78
87
|
var ReplyMethod = /* @__PURE__ */ ((ReplyMethod2) => {
|
|
@@ -108,6 +117,7 @@ var CurrentNodeStatus = /* @__PURE__ */ ((CurrentNodeStatus2) => {
|
|
|
108
117
|
// src/agent.ts
|
|
109
118
|
var import_crypto = require("crypto");
|
|
110
119
|
var import_tencentcloud_sdk_nodejs_lke = require("tencentcloud-sdk-nodejs-lke");
|
|
120
|
+
var import_cos_nodejs_sdk_v5 = __toESM(require("cos-nodejs-sdk-v5"));
|
|
111
121
|
var AdpAgentError = class extends Error {
|
|
112
122
|
constructor(message, code) {
|
|
113
123
|
super(message);
|
|
@@ -118,7 +128,9 @@ var AdpAgentError = class extends Error {
|
|
|
118
128
|
var AdpAgent = class extends import_client.AbstractAgent {
|
|
119
129
|
constructor(config) {
|
|
120
130
|
super(config);
|
|
131
|
+
this.finalAppKey = "";
|
|
121
132
|
this.adpConfig = config.adpConfig;
|
|
133
|
+
this.finalAppKey = this.adpConfig.appKey || this.adpConfig.request?.body?.botAppKey || process.env.ADP_APP_KEY || "";
|
|
122
134
|
this.reqAppClient = import_axios.default.create({
|
|
123
135
|
baseURL: this.adpConfig.request?.baseUrl || "https://wss.lke.cloud.tencent.com"
|
|
124
136
|
});
|
|
@@ -134,25 +146,23 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
134
146
|
generateRequestBody({
|
|
135
147
|
message,
|
|
136
148
|
fileInfos,
|
|
137
|
-
|
|
138
|
-
threadId,
|
|
139
|
-
forwardedProps
|
|
149
|
+
input
|
|
140
150
|
}) {
|
|
151
|
+
const { state, runId, threadId, forwardedProps } = input;
|
|
141
152
|
const requestBody = {
|
|
142
153
|
incremental: true,
|
|
143
154
|
stream: "enable",
|
|
144
155
|
...this.adpConfig.request?.body || {},
|
|
145
156
|
...forwardedProps || {},
|
|
146
|
-
botAppKey: this.
|
|
147
|
-
visitorBizId: forwardedProps?.visitorBizId || (0, import_crypto.randomUUID)(),
|
|
157
|
+
botAppKey: this.finalAppKey,
|
|
158
|
+
visitorBizId: state?.__request_context__?.id || forwardedProps?.visitorBizId || (0, import_crypto.randomUUID)(),
|
|
148
159
|
requestId: runId,
|
|
149
|
-
sessionId: threadId
|
|
160
|
+
sessionId: threadId,
|
|
150
161
|
content: message,
|
|
151
162
|
fileInfos
|
|
152
163
|
};
|
|
153
164
|
return requestBody;
|
|
154
165
|
}
|
|
155
|
-
// @ts-ignore
|
|
156
166
|
run(input) {
|
|
157
167
|
return new import_rxjs.Observable((subscriber) => {
|
|
158
168
|
this._run(subscriber, input);
|
|
@@ -160,24 +170,25 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
160
170
|
}
|
|
161
171
|
async _run(subscriber, input) {
|
|
162
172
|
try {
|
|
163
|
-
const {
|
|
173
|
+
const { runId, threadId: _threadId } = input;
|
|
174
|
+
const threadId = _threadId || (0, import_crypto.randomUUID)();
|
|
164
175
|
subscriber.next({
|
|
165
176
|
type: import_client.EventType.RUN_STARTED,
|
|
166
177
|
runId,
|
|
167
178
|
threadId
|
|
168
179
|
});
|
|
169
|
-
if (!this.
|
|
180
|
+
if (!this.finalAppKey) {
|
|
170
181
|
throw new AdpAgentError(
|
|
171
|
-
"
|
|
182
|
+
"ADP_APP_KEY is required, check your env variables or config passed with the adapter",
|
|
172
183
|
"MISSING_APP_KEY"
|
|
173
184
|
);
|
|
174
185
|
}
|
|
175
|
-
const { messages: docExtractedMessages, fileInfos } = await extractDocuments(
|
|
176
|
-
const { message, trimmed } = convertAGUIMessagesToAdpMessages(docExtractedMessages);
|
|
186
|
+
const { messages: docExtractedMessages, fileInfos } = await this.extractDocuments(input, subscriber);
|
|
187
|
+
const { message, trimmed } = await this.convertAGUIMessagesToAdpMessages(docExtractedMessages);
|
|
177
188
|
if (!message) {
|
|
178
189
|
throw new AdpAgentError(
|
|
179
190
|
"Message content format error, or empty content",
|
|
180
|
-
"
|
|
191
|
+
"INVALID_MESSAGE_FORMAT"
|
|
181
192
|
);
|
|
182
193
|
}
|
|
183
194
|
if (trimmed > 0) {
|
|
@@ -192,9 +203,7 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
192
203
|
const requestBody = this.generateRequestBody({
|
|
193
204
|
message,
|
|
194
205
|
fileInfos,
|
|
195
|
-
|
|
196
|
-
threadId,
|
|
197
|
-
forwardedProps
|
|
206
|
+
input
|
|
198
207
|
});
|
|
199
208
|
const response = await this.reqAppClient.post(
|
|
200
209
|
this.adpConfig.request?.endpoint || "/v1/qbot/chat/sse",
|
|
@@ -363,7 +372,7 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
363
372
|
console.error(JSON.stringify(data));
|
|
364
373
|
throw new AdpAgentError(
|
|
365
374
|
data.error.message,
|
|
366
|
-
`ADP_ERROR_${data.error.code}`
|
|
375
|
+
data.error.code ? `ADP_ERROR_${data.error.code}` : "ADP_ERROR_-1"
|
|
367
376
|
);
|
|
368
377
|
}
|
|
369
378
|
case "token_stat": {
|
|
@@ -415,66 +424,261 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
415
424
|
subscriber.complete();
|
|
416
425
|
}
|
|
417
426
|
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
break;
|
|
425
|
-
}
|
|
426
|
-
if (message.role === "user") {
|
|
427
|
-
trimmed--;
|
|
428
|
-
let content = "";
|
|
429
|
-
if (typeof message.content === "string") {
|
|
430
|
-
content = message.content;
|
|
431
|
-
} else {
|
|
432
|
-
content = message.content.reduce((acc, cur) => {
|
|
433
|
-
if (cur.type === "text") {
|
|
434
|
-
return acc + `${cur.text} `;
|
|
435
|
-
} else if (cur.type === "binary") {
|
|
436
|
-
return acc + ` `;
|
|
437
|
-
} else {
|
|
438
|
-
return acc;
|
|
439
|
-
}
|
|
440
|
-
}, "").trim();
|
|
427
|
+
async convertAGUIMessagesToAdpMessages(messages) {
|
|
428
|
+
let result = "";
|
|
429
|
+
let trimmed = messages.length;
|
|
430
|
+
for (const message of messages.reverse()) {
|
|
431
|
+
if (message.role === "assistant") {
|
|
432
|
+
break;
|
|
441
433
|
}
|
|
442
|
-
|
|
434
|
+
if (message.role === "user") {
|
|
435
|
+
trimmed--;
|
|
436
|
+
let content = "";
|
|
437
|
+
if (typeof message.content === "string") {
|
|
438
|
+
content = message.content;
|
|
439
|
+
} else {
|
|
440
|
+
const imageMap = /* @__PURE__ */ new Map();
|
|
441
|
+
const imagesToUpload = [];
|
|
442
|
+
message.content.forEach((item) => {
|
|
443
|
+
if (item.type === "binary") {
|
|
444
|
+
if (Object.keys(IMAGE_MIME_TYPES).includes(item.mimeType)) {
|
|
445
|
+
imagesToUpload.push(item);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
if (imagesToUpload.length) {
|
|
450
|
+
await this.uploadToCos(
|
|
451
|
+
imagesToUpload,
|
|
452
|
+
(data, _, file) => {
|
|
453
|
+
imageMap.set(
|
|
454
|
+
file.id || file.filename,
|
|
455
|
+
`https://${data.Location}`
|
|
456
|
+
);
|
|
457
|
+
},
|
|
458
|
+
(fileName, error) => {
|
|
459
|
+
throw new AdpAgentError(
|
|
460
|
+
`Upload image ${fileName} failed: ${error}`,
|
|
461
|
+
"UPLOAD_IMAGE_FAILED"
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
content = message.content.reduce((acc, cur) => {
|
|
467
|
+
if (cur.type === "text") {
|
|
468
|
+
return acc + `${cur.text} `;
|
|
469
|
+
} else if (cur.type === "binary") {
|
|
470
|
+
if (Object.keys(IMAGE_MIME_TYPES).includes(cur.mimeType)) {
|
|
471
|
+
if (imageMap.has(cur.id || cur.filename)) {
|
|
472
|
+
return acc + `}) `;
|
|
473
|
+
} else {
|
|
474
|
+
return acc;
|
|
475
|
+
}
|
|
476
|
+
} else return acc;
|
|
477
|
+
} else {
|
|
478
|
+
return acc;
|
|
479
|
+
}
|
|
480
|
+
}, "").trim();
|
|
481
|
+
}
|
|
482
|
+
result = `${message.role}: ${content}
|
|
443
483
|
${result}`;
|
|
444
|
-
|
|
445
|
-
|
|
484
|
+
} else {
|
|
485
|
+
result = `${message.role}: ${message.content}
|
|
446
486
|
${result}`;
|
|
487
|
+
}
|
|
447
488
|
}
|
|
489
|
+
return { message: result.trim(), trimmed };
|
|
448
490
|
}
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
491
|
+
async extractDocuments(input, subscriber) {
|
|
492
|
+
const { messages, runId, threadId } = input;
|
|
493
|
+
const documentFiles = [];
|
|
494
|
+
const newMessages = messages.map((msg) => {
|
|
495
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
496
|
+
let newContent = [];
|
|
497
|
+
msg.content.forEach((item) => {
|
|
498
|
+
if (item.type === "text") {
|
|
499
|
+
newContent.push(item);
|
|
500
|
+
} else if (item.type === "binary") {
|
|
501
|
+
if (Object.keys(DOCUMENT_MIME_TYPES).includes(item.mimeType)) {
|
|
502
|
+
documentFiles.push(item);
|
|
503
|
+
} else {
|
|
504
|
+
newContent.push(item);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
return {
|
|
509
|
+
...msg,
|
|
510
|
+
content: newContent
|
|
511
|
+
};
|
|
512
|
+
} else return msg;
|
|
513
|
+
});
|
|
514
|
+
const fileInfos = [];
|
|
515
|
+
const successedFiles = [];
|
|
516
|
+
const failedFiles = [];
|
|
517
|
+
if (documentFiles.length) {
|
|
518
|
+
try {
|
|
519
|
+
await this.uploadToCos(
|
|
520
|
+
documentFiles,
|
|
521
|
+
(data, cosParams, file) => {
|
|
522
|
+
successedFiles.push({ data, cosParams, file });
|
|
523
|
+
},
|
|
524
|
+
(fileName, error) => {
|
|
525
|
+
failedFiles.push({ fileName, error });
|
|
526
|
+
}
|
|
527
|
+
);
|
|
528
|
+
} catch (e) {
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
if (successedFiles.length) {
|
|
532
|
+
successedFiles.forEach(async ({ data: cosData, cosParams, file }) => {
|
|
533
|
+
const extName = MIME_TYPES[file.mimeType];
|
|
534
|
+
const requestBody = {
|
|
535
|
+
sessionId: threadId,
|
|
536
|
+
botAppKey: this.finalAppKey,
|
|
537
|
+
requestId: (0, import_crypto.randomUUID)(),
|
|
538
|
+
cosBucket: cosParams.Bucket,
|
|
539
|
+
fileType: extName,
|
|
540
|
+
fileName: file.filename,
|
|
541
|
+
cosUrl: cosParams.UploadPath,
|
|
542
|
+
cosHash: cosData.headers?.["x-cos-hash-crc64ecma"] || "",
|
|
543
|
+
eTag: cosData.ETag,
|
|
544
|
+
size: file.data?.length.toString() || "0"
|
|
545
|
+
};
|
|
546
|
+
const response = await this.reqAppClient.post(
|
|
547
|
+
this.adpConfig.request?.endpoint || "/v1/qbot/chat/docParse",
|
|
548
|
+
camelToSnakeKeys(requestBody),
|
|
549
|
+
{ responseType: "stream" }
|
|
550
|
+
);
|
|
551
|
+
const sseStream = response.data;
|
|
552
|
+
let buffer = "";
|
|
553
|
+
for await (const chunk of sseStream) {
|
|
554
|
+
buffer += chunk.toString();
|
|
555
|
+
const parts = buffer.split("\n\n");
|
|
556
|
+
buffer = parts.pop() || "";
|
|
557
|
+
for (const part of parts) {
|
|
558
|
+
if (!part.trim()) continue;
|
|
559
|
+
const event = { data: "", event: "" };
|
|
560
|
+
for (const line of part.split("\n")) {
|
|
561
|
+
if (line.startsWith("data:")) {
|
|
562
|
+
event.data += line.slice(5);
|
|
563
|
+
} else if (line.startsWith("event:")) {
|
|
564
|
+
event.event = line.slice(6);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (event.data) {
|
|
568
|
+
let data;
|
|
569
|
+
try {
|
|
570
|
+
data = JSON.parse(event.data);
|
|
571
|
+
} catch (e) {
|
|
572
|
+
throw new AdpAgentError(
|
|
573
|
+
`ADP returned invalid data: ${event.data}`,
|
|
574
|
+
"INVALID_DATA"
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
switch (data.type) {
|
|
578
|
+
case "parsing": {
|
|
579
|
+
subscriber.next({
|
|
580
|
+
type: import_client.EventType.RAW,
|
|
581
|
+
rawEvent: {
|
|
582
|
+
message: `Parsing document ${file.filename}: ${data.payload.process}%`,
|
|
583
|
+
type: "info"
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
if (data.payload.is_final) {
|
|
587
|
+
if (data.payload.error_message) {
|
|
588
|
+
subscriber.next({
|
|
589
|
+
type: import_client.EventType.RAW,
|
|
590
|
+
rawEvent: {
|
|
591
|
+
message: `Parsing document ${file.filename} failed: ${data.payload.error_message}`,
|
|
592
|
+
type: "error"
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
} else {
|
|
596
|
+
const fileNameNoExt = file.filename.split(".").splice(0, -1).join(".");
|
|
597
|
+
fileInfos.push({
|
|
598
|
+
docId: data.payload.doc_id,
|
|
599
|
+
fileName: fileNameNoExt,
|
|
600
|
+
fileType: extName,
|
|
601
|
+
fileSize: file.data?.length.toString() || "0",
|
|
602
|
+
fileUrl: `https://${cosData.Location}`
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
default: {
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
462
613
|
}
|
|
463
614
|
}
|
|
464
615
|
});
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
616
|
+
}
|
|
617
|
+
return {
|
|
618
|
+
messages: newMessages,
|
|
619
|
+
fileInfos,
|
|
620
|
+
failedFiles
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
async getCosParams(mimeType) {
|
|
624
|
+
const extName = MIME_TYPES?.[mimeType];
|
|
625
|
+
if (!extName) {
|
|
626
|
+
throw new Error(`Unsupported mime type: ${mimeType}`);
|
|
627
|
+
}
|
|
628
|
+
const isPicture = Object.keys(IMAGE_MIME_TYPES).includes(mimeType);
|
|
629
|
+
const bizIdRes = await this.reqLkeClient.DescribeRobotBizIDByAppKey({
|
|
630
|
+
AppKey: this.finalAppKey
|
|
631
|
+
});
|
|
632
|
+
const storageRes = await this.reqLkeClient.DescribeStorageCredential({
|
|
633
|
+
BotBizId: bizIdRes.BotBizId,
|
|
634
|
+
FileType: extName,
|
|
635
|
+
IsPublic: isPicture,
|
|
636
|
+
TypeKey: "realtime"
|
|
637
|
+
});
|
|
638
|
+
return storageRes;
|
|
639
|
+
}
|
|
640
|
+
async uploadToCos(files, onSuccess, onError) {
|
|
641
|
+
const promises = [];
|
|
642
|
+
for (const file of files) {
|
|
643
|
+
const extName = MIME_TYPES?.[file.mimeType];
|
|
644
|
+
if (!extName) {
|
|
645
|
+
onError(file.filename || "unknown-file", "Unsupported file type");
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const cosParams = await this.getCosParams(file.mimeType);
|
|
649
|
+
const cosClient = new import_cos_nodejs_sdk_v5.default({
|
|
650
|
+
SecretId: cosParams.Credentials?.TmpSecretId,
|
|
651
|
+
SecretKey: cosParams.Credentials?.TmpSecretKey,
|
|
652
|
+
SecurityToken: cosParams.Credentials?.Token
|
|
653
|
+
});
|
|
654
|
+
if (!cosParams.Bucket || !cosParams.Region || !cosParams.UploadPath) {
|
|
655
|
+
onError(file.filename || "unknown-file", "Failed to get COS params");
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
const promise = new Promise((resolve, reject) => {
|
|
659
|
+
cosClient.putObject(
|
|
660
|
+
{
|
|
661
|
+
Bucket: cosParams.Bucket,
|
|
662
|
+
Region: cosParams.Region,
|
|
663
|
+
Key: cosParams.UploadPath,
|
|
664
|
+
Body: file.data
|
|
665
|
+
},
|
|
666
|
+
(err, data) => {
|
|
667
|
+
if (err) {
|
|
668
|
+
onError(file.filename || "unknown-file", err.message);
|
|
669
|
+
reject();
|
|
670
|
+
} else {
|
|
671
|
+
onSuccess(data, cosParams, file);
|
|
672
|
+
resolve();
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
);
|
|
676
|
+
});
|
|
677
|
+
promises.push(promise);
|
|
678
|
+
}
|
|
679
|
+
await Promise.allSettled(promises);
|
|
680
|
+
}
|
|
681
|
+
};
|
|
478
682
|
// Annotate the CommonJS export names for ESM import in node:
|
|
479
683
|
0 && (module.exports = {
|
|
480
684
|
AdpAgent,
|