@cloudbase/agent-adapter-adp 1.0.1-alpha.11 → 1.0.1-alpha.12
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 +273 -70
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +273 -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,24 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
160
170
|
}
|
|
161
171
|
async _run(subscriber, input) {
|
|
162
172
|
try {
|
|
163
|
-
const {
|
|
173
|
+
const { runId, threadId } = input;
|
|
164
174
|
subscriber.next({
|
|
165
175
|
type: import_client.EventType.RUN_STARTED,
|
|
166
176
|
runId,
|
|
167
177
|
threadId
|
|
168
178
|
});
|
|
169
|
-
if (!this.
|
|
179
|
+
if (!this.finalAppKey) {
|
|
170
180
|
throw new AdpAgentError(
|
|
171
|
-
"
|
|
181
|
+
"ADP_APP_KEY is required, check your env variables or config passed with the adapter",
|
|
172
182
|
"MISSING_APP_KEY"
|
|
173
183
|
);
|
|
174
184
|
}
|
|
175
|
-
const { messages: docExtractedMessages, fileInfos } = await extractDocuments(
|
|
176
|
-
const { message, trimmed } = convertAGUIMessagesToAdpMessages(docExtractedMessages);
|
|
185
|
+
const { messages: docExtractedMessages, fileInfos } = await this.extractDocuments(input, subscriber);
|
|
186
|
+
const { message, trimmed } = await this.convertAGUIMessagesToAdpMessages(docExtractedMessages);
|
|
177
187
|
if (!message) {
|
|
178
188
|
throw new AdpAgentError(
|
|
179
189
|
"Message content format error, or empty content",
|
|
180
|
-
"
|
|
190
|
+
"INVALID_MESSAGE_FORMAT"
|
|
181
191
|
);
|
|
182
192
|
}
|
|
183
193
|
if (trimmed > 0) {
|
|
@@ -192,9 +202,7 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
192
202
|
const requestBody = this.generateRequestBody({
|
|
193
203
|
message,
|
|
194
204
|
fileInfos,
|
|
195
|
-
|
|
196
|
-
threadId,
|
|
197
|
-
forwardedProps
|
|
205
|
+
input
|
|
198
206
|
});
|
|
199
207
|
const response = await this.reqAppClient.post(
|
|
200
208
|
this.adpConfig.request?.endpoint || "/v1/qbot/chat/sse",
|
|
@@ -363,7 +371,7 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
363
371
|
console.error(JSON.stringify(data));
|
|
364
372
|
throw new AdpAgentError(
|
|
365
373
|
data.error.message,
|
|
366
|
-
`ADP_ERROR_${data.error.code}`
|
|
374
|
+
data.error.code ? `ADP_ERROR_${data.error.code}` : "ADP_ERROR_-1"
|
|
367
375
|
);
|
|
368
376
|
}
|
|
369
377
|
case "token_stat": {
|
|
@@ -415,66 +423,261 @@ var AdpAgent = class extends import_client.AbstractAgent {
|
|
|
415
423
|
subscriber.complete();
|
|
416
424
|
}
|
|
417
425
|
}
|
|
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();
|
|
426
|
+
async convertAGUIMessagesToAdpMessages(messages) {
|
|
427
|
+
let result = "";
|
|
428
|
+
let trimmed = messages.length;
|
|
429
|
+
for (const message of messages.reverse()) {
|
|
430
|
+
if (message.role === "assistant") {
|
|
431
|
+
break;
|
|
441
432
|
}
|
|
442
|
-
|
|
433
|
+
if (message.role === "user") {
|
|
434
|
+
trimmed--;
|
|
435
|
+
let content = "";
|
|
436
|
+
if (typeof message.content === "string") {
|
|
437
|
+
content = message.content;
|
|
438
|
+
} else {
|
|
439
|
+
const imageMap = /* @__PURE__ */ new Map();
|
|
440
|
+
const imagesToUpload = [];
|
|
441
|
+
message.content.forEach((item) => {
|
|
442
|
+
if (item.type === "binary") {
|
|
443
|
+
if (Object.keys(IMAGE_MIME_TYPES).includes(item.mimeType)) {
|
|
444
|
+
imagesToUpload.push(item);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
if (imagesToUpload.length) {
|
|
449
|
+
await this.uploadToCos(
|
|
450
|
+
imagesToUpload,
|
|
451
|
+
(data, _, file) => {
|
|
452
|
+
imageMap.set(
|
|
453
|
+
file.id || file.filename,
|
|
454
|
+
`https://${data.Location}`
|
|
455
|
+
);
|
|
456
|
+
},
|
|
457
|
+
(fileName, error) => {
|
|
458
|
+
throw new AdpAgentError(
|
|
459
|
+
`Upload image ${fileName} failed: ${error}`,
|
|
460
|
+
"UPLOAD_IMAGE_FAILED"
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
content = message.content.reduce((acc, cur) => {
|
|
466
|
+
if (cur.type === "text") {
|
|
467
|
+
return acc + `${cur.text} `;
|
|
468
|
+
} else if (cur.type === "binary") {
|
|
469
|
+
if (Object.keys(IMAGE_MIME_TYPES).includes(cur.mimeType)) {
|
|
470
|
+
if (imageMap.has(cur.id || cur.filename)) {
|
|
471
|
+
return acc + `}) `;
|
|
472
|
+
} else {
|
|
473
|
+
return acc;
|
|
474
|
+
}
|
|
475
|
+
} else return acc;
|
|
476
|
+
} else {
|
|
477
|
+
return acc;
|
|
478
|
+
}
|
|
479
|
+
}, "").trim();
|
|
480
|
+
}
|
|
481
|
+
result = `${message.role}: ${content}
|
|
443
482
|
${result}`;
|
|
444
|
-
|
|
445
|
-
|
|
483
|
+
} else {
|
|
484
|
+
result = `${message.role}: ${message.content}
|
|
446
485
|
${result}`;
|
|
486
|
+
}
|
|
447
487
|
}
|
|
488
|
+
return { message: result.trim(), trimmed };
|
|
448
489
|
}
|
|
449
|
-
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
490
|
+
async extractDocuments(input, subscriber) {
|
|
491
|
+
const { messages, runId, threadId } = input;
|
|
492
|
+
const documentFiles = [];
|
|
493
|
+
const newMessages = messages.map((msg) => {
|
|
494
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
495
|
+
let newContent = [];
|
|
496
|
+
msg.content.forEach((item) => {
|
|
497
|
+
if (item.type === "text") {
|
|
498
|
+
newContent.push(item);
|
|
499
|
+
} else if (item.type === "binary") {
|
|
500
|
+
if (Object.keys(DOCUMENT_MIME_TYPES).includes(item.mimeType)) {
|
|
501
|
+
documentFiles.push(item);
|
|
502
|
+
} else {
|
|
503
|
+
newContent.push(item);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
return {
|
|
508
|
+
...msg,
|
|
509
|
+
content: newContent
|
|
510
|
+
};
|
|
511
|
+
} else return msg;
|
|
512
|
+
});
|
|
513
|
+
const fileInfos = [];
|
|
514
|
+
const successedFiles = [];
|
|
515
|
+
const failedFiles = [];
|
|
516
|
+
if (documentFiles.length) {
|
|
517
|
+
try {
|
|
518
|
+
await this.uploadToCos(
|
|
519
|
+
documentFiles,
|
|
520
|
+
(data, cosParams, file) => {
|
|
521
|
+
successedFiles.push({ data, cosParams, file });
|
|
522
|
+
},
|
|
523
|
+
(fileName, error) => {
|
|
524
|
+
failedFiles.push({ fileName, error });
|
|
525
|
+
}
|
|
526
|
+
);
|
|
527
|
+
} catch (e) {
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (successedFiles.length) {
|
|
531
|
+
successedFiles.forEach(async ({ data: cosData, cosParams, file }) => {
|
|
532
|
+
const extName = MIME_TYPES[file.mimeType];
|
|
533
|
+
const requestBody = {
|
|
534
|
+
sessionId: threadId,
|
|
535
|
+
botAppKey: this.finalAppKey,
|
|
536
|
+
requestId: (0, import_crypto.randomUUID)(),
|
|
537
|
+
cosBucket: cosParams.Bucket,
|
|
538
|
+
fileType: extName,
|
|
539
|
+
fileName: file.filename,
|
|
540
|
+
cosUrl: cosParams.UploadPath,
|
|
541
|
+
cosHash: cosData.headers?.["x-cos-hash-crc64ecma"] || "",
|
|
542
|
+
eTag: cosData.ETag,
|
|
543
|
+
size: file.data?.length.toString() || "0"
|
|
544
|
+
};
|
|
545
|
+
const response = await this.reqAppClient.post(
|
|
546
|
+
this.adpConfig.request?.endpoint || "/v1/qbot/chat/docParse",
|
|
547
|
+
camelToSnakeKeys(requestBody),
|
|
548
|
+
{ responseType: "stream" }
|
|
549
|
+
);
|
|
550
|
+
const sseStream = response.data;
|
|
551
|
+
let buffer = "";
|
|
552
|
+
for await (const chunk of sseStream) {
|
|
553
|
+
buffer += chunk.toString();
|
|
554
|
+
const parts = buffer.split("\n\n");
|
|
555
|
+
buffer = parts.pop() || "";
|
|
556
|
+
for (const part of parts) {
|
|
557
|
+
if (!part.trim()) continue;
|
|
558
|
+
const event = { data: "", event: "" };
|
|
559
|
+
for (const line of part.split("\n")) {
|
|
560
|
+
if (line.startsWith("data:")) {
|
|
561
|
+
event.data += line.slice(5);
|
|
562
|
+
} else if (line.startsWith("event:")) {
|
|
563
|
+
event.event = line.slice(6);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (event.data) {
|
|
567
|
+
let data;
|
|
568
|
+
try {
|
|
569
|
+
data = JSON.parse(event.data);
|
|
570
|
+
} catch (e) {
|
|
571
|
+
throw new AdpAgentError(
|
|
572
|
+
`ADP returned invalid data: ${event.data}`,
|
|
573
|
+
"INVALID_DATA"
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
switch (data.type) {
|
|
577
|
+
case "parsing": {
|
|
578
|
+
subscriber.next({
|
|
579
|
+
type: import_client.EventType.RAW,
|
|
580
|
+
rawEvent: {
|
|
581
|
+
message: `Parsing document ${file.filename}: ${data.payload.process}%`,
|
|
582
|
+
type: "info"
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
if (data.payload.is_final) {
|
|
586
|
+
if (data.payload.error_message) {
|
|
587
|
+
subscriber.next({
|
|
588
|
+
type: import_client.EventType.RAW,
|
|
589
|
+
rawEvent: {
|
|
590
|
+
message: `Parsing document ${file.filename} failed: ${data.payload.error_message}`,
|
|
591
|
+
type: "error"
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
} else {
|
|
595
|
+
const fileNameNoExt = file.filename.split(".").splice(0, -1).join(".");
|
|
596
|
+
fileInfos.push({
|
|
597
|
+
docId: data.payload.doc_id,
|
|
598
|
+
fileName: fileNameNoExt,
|
|
599
|
+
fileType: extName,
|
|
600
|
+
fileSize: file.data?.length.toString() || "0",
|
|
601
|
+
fileUrl: `https://${cosData.Location}`
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
default: {
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
462
612
|
}
|
|
463
613
|
}
|
|
464
614
|
});
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
messages: newMessages,
|
|
618
|
+
fileInfos,
|
|
619
|
+
failedFiles
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
async getCosParams(mimeType) {
|
|
623
|
+
const extName = MIME_TYPES?.[mimeType];
|
|
624
|
+
if (!extName) {
|
|
625
|
+
throw new Error(`Unsupported mime type: ${mimeType}`);
|
|
626
|
+
}
|
|
627
|
+
const isPicture = Object.keys(IMAGE_MIME_TYPES).includes(mimeType);
|
|
628
|
+
const bizIdRes = await this.reqLkeClient.DescribeRobotBizIDByAppKey({
|
|
629
|
+
AppKey: this.finalAppKey
|
|
630
|
+
});
|
|
631
|
+
const storageRes = await this.reqLkeClient.DescribeStorageCredential({
|
|
632
|
+
BotBizId: bizIdRes.BotBizId,
|
|
633
|
+
FileType: extName,
|
|
634
|
+
IsPublic: isPicture,
|
|
635
|
+
TypeKey: "realtime"
|
|
636
|
+
});
|
|
637
|
+
return storageRes;
|
|
638
|
+
}
|
|
639
|
+
async uploadToCos(files, onSuccess, onError) {
|
|
640
|
+
const promises = [];
|
|
641
|
+
for (const file of files) {
|
|
642
|
+
const extName = MIME_TYPES?.[file.mimeType];
|
|
643
|
+
if (!extName) {
|
|
644
|
+
onError(file.filename || "unknown-file", "Unsupported file type");
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
const cosParams = await this.getCosParams(file.mimeType);
|
|
648
|
+
const cosClient = new import_cos_nodejs_sdk_v5.default({
|
|
649
|
+
SecretId: cosParams.Credentials?.TmpSecretId,
|
|
650
|
+
SecretKey: cosParams.Credentials?.TmpSecretKey,
|
|
651
|
+
SecurityToken: cosParams.Credentials?.Token
|
|
652
|
+
});
|
|
653
|
+
if (!cosParams.Bucket || !cosParams.Region || !cosParams.UploadPath) {
|
|
654
|
+
onError(file.filename || "unknown-file", "Failed to get COS params");
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
const promise = new Promise((resolve, reject) => {
|
|
658
|
+
cosClient.putObject(
|
|
659
|
+
{
|
|
660
|
+
Bucket: cosParams.Bucket,
|
|
661
|
+
Region: cosParams.Region,
|
|
662
|
+
Key: cosParams.UploadPath,
|
|
663
|
+
Body: file.data
|
|
664
|
+
},
|
|
665
|
+
(err, data) => {
|
|
666
|
+
if (err) {
|
|
667
|
+
onError(file.filename || "unknown-file", err.message);
|
|
668
|
+
reject();
|
|
669
|
+
} else {
|
|
670
|
+
onSuccess(data, cosParams, file);
|
|
671
|
+
resolve();
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
);
|
|
675
|
+
});
|
|
676
|
+
promises.push(promise);
|
|
677
|
+
}
|
|
678
|
+
await Promise.allSettled(promises);
|
|
679
|
+
}
|
|
680
|
+
};
|
|
478
681
|
// Annotate the CommonJS export names for ESM import in node:
|
|
479
682
|
0 && (module.exports = {
|
|
480
683
|
AdpAgent,
|