agent-bridge-doubao 1.0.7 → 1.0.8
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.
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DoubaoCdpClient = void 0;
|
|
3
|
+
exports.DoubaoCdpClient = exports.responseTextScript = void 0;
|
|
4
4
|
exports.requestBody = requestBody;
|
|
5
|
-
exports.responseText = responseText;
|
|
6
5
|
const node_crypto_1 = require("node:crypto");
|
|
7
6
|
const defaults = { port: Number(process.env.DOUBAO_CDP_PORT ?? "9227"), conversationId: process.env.DOUBAO_CONVERSATION_ID ?? "759450172544514", botId: process.env.DOUBAO_BOT_ID ?? "7338286299411103781", timeoutMs: Number(process.env.DOUBAO_TIMEOUT_MS ?? "180000") };
|
|
8
7
|
function sleep(ms, signal) {
|
|
@@ -15,28 +14,7 @@ function requestBody(text, conversationId, botId, pluginList = []) {
|
|
|
15
14
|
const block = { block_type: 10000, content: { text_block: { text, icon_url: "", icon_url_dark: "", summary: "" }, pc_event_block: "" }, block_id: (0, node_crypto_1.randomUUID)(), parent_id: "", meta_info: [], append_fields: [] };
|
|
16
15
|
return JSON.stringify({ client_meta: { conversation_id: conversationId, bot_id: botId, last_section_id: "", last_message_index: null, local_permissions: [{ permission_name: "ACCESS_COARSE_LOCATION", status: 1 }, { permission_name: "ACCESS_FINE_LOCATION", status: 1 }, { permission_name: "ACCESS_BACKGROUND_LOCATION", status: 1 }] }, messages: [{ local_message_id: (0, node_crypto_1.randomUUID)(), content_block: [block], message_status: 0 }], option: { send_message_scene: "", create_time_ms: Date.now(), collect_id: "", is_audio: false, answer_with_suggest: false, agent_mode: 2, tts_switch: false, need_deep_think: 0, click_clear_context: false, is_regen: false, is_replace: false, is_from_click_option: false, is_from_click_softlink: false, disable_sse_cache: false, scene_type: 0, unique_key: (0, node_crypto_1.randomUUID)(), start_seq: 0, need_create_conversation: false, regen_query_id: [], edit_query_id: [], message_from: 0, sse_recv_event_options: { support_chunk_delta: true }, support_lazy_fetch_stream: true, is_ai_playground: false, is_old_user: true, general_task_param: general, recovery_option: { is_recovery: false, req_create_time_sec: Math.floor(Date.now() / 1000), append_sse_event_scene: 0 }, message_storage_type: 0, related_deleted_message_ids: {}, connector_info_list: [], model_config: { model_item_key: "0", model_extra_params: {}, reasoning_effort: 5 }, aggregate_params: { mention_skill_list: "[]", mention_plugin_list: JSON.stringify(pluginList), mention_ext: "[{}]", conversation_mode: "", mode_id: "1", model_item_key: "0", agent_mode: "2", reasoning_effort: "5", provider_id: "" } }, user_context: [], ext: { agent_mode: "2", use_deep_think: "0", general_task_param: JSON.stringify(general), collection_id: "", is_finish: "1", commerce_credit_config_enable: "0" } });
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
if (event !== "STREAM_MSG_NOTIFY" && event !== "FULL_MSG_NOTIFY")
|
|
20
|
-
return [];
|
|
21
|
-
const texts = [];
|
|
22
|
-
const visit = (item) => {
|
|
23
|
-
if (Array.isArray(item)) {
|
|
24
|
-
for (const child of item)
|
|
25
|
-
visit(child);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
if (!item || typeof item !== "object")
|
|
29
|
-
return;
|
|
30
|
-
const record = item;
|
|
31
|
-
const block = record.text_block;
|
|
32
|
-
if (block && typeof block === "object" && typeof block.text === "string")
|
|
33
|
-
texts.push(block.text);
|
|
34
|
-
for (const child of Object.values(record))
|
|
35
|
-
visit(child);
|
|
36
|
-
};
|
|
37
|
-
visit(value);
|
|
38
|
-
return texts;
|
|
39
|
-
}
|
|
17
|
+
exports.responseTextScript = `(value) => { const texts = []; const visit = (item) => { if (Array.isArray(item)) { for (const child of item) visit(child); return; } if (!item || typeof item !== 'object') return; if (item.text_block && typeof item.text_block.text === 'string') texts.push(item.text_block.text); for (const child of Object.values(item)) visit(child); }; visit(value); return texts; }`;
|
|
40
18
|
class DoubaoCdpClient {
|
|
41
19
|
socket;
|
|
42
20
|
sequence = 0;
|
|
@@ -107,7 +85,7 @@ function buildInjection(body) {
|
|
|
107
85
|
if (!response.ok) throw new Error('HTTP ' + response.status);
|
|
108
86
|
const reader = response.body?.getReader(); if (!reader) throw new Error('响应无 body');
|
|
109
87
|
const decoder = new TextDecoder(); let buffer = '', reply = '', events = [];
|
|
110
|
-
const consume = (block) => { const event = (block.match(/^event:\s*(.+)$/m) || [])[1] || ''; const data = (block.match(/^data:\s*(.+)$/m) || [])[1] || ''; if (event) events.push(event); if (event === 'STREAM_ERROR') throw new Error('STREAM_ERROR ' + data.slice(0, 500)); if (!data) return; try { const json = JSON.parse(data); if (event === 'STREAM_CHUNK') for (const patch of (json.patch_op || [])) reply = merge(reply, patch.patch_value?.tts_content || ''); if (event === 'CHUNK_DELTA' && typeof json.text === 'string') reply = merge(reply, json.text); for (const text of ${
|
|
88
|
+
const consume = (block) => { const event = (block.match(/^event:\s*(.+)$/m) || [])[1] || ''; const data = (block.match(/^data:\s*(.+)$/m) || [])[1] || ''; if (event) events.push(event); if (event === 'STREAM_ERROR') throw new Error('STREAM_ERROR ' + data.slice(0, 500)); if (!data) return; try { const json = JSON.parse(data); if (event === 'STREAM_CHUNK') for (const patch of (json.patch_op || [])) reply = merge(reply, patch.patch_value?.tts_content || ''); if (event === 'CHUNK_DELTA' && typeof json.text === 'string') reply = merge(reply, json.text); if (event === 'STREAM_MSG_NOTIFY' || event === 'FULL_MSG_NOTIFY') for (const text of (${exports.responseTextScript})(json)) reply = merge(reply, text); if (event === 'SSE_REPLY_END') reply = merge(reply, json.msg_finish_attr?.brief || json.data?.msg_finish_attr?.brief || ''); } catch {} };
|
|
111
89
|
for (;;) { const item = await reader.read(); if (item.done) break; buffer += decoder.decode(item.value, { stream: true }).replace(/\r\n/g, '\n'); let index; while ((index = buffer.indexOf('\n\n')) >= 0) { consume(buffer.slice(0, index)); buffer = buffer.slice(index + 2); } }
|
|
112
90
|
if (buffer.trim()) consume(buffer);
|
|
113
91
|
window.__mab_doubao_result = JSON.stringify({ ok: Boolean(reply.trim()), reply, reason: reply.trim() ? '' : 'SSE 未提取到文本; events=' + events.slice(0, 20).join(',') });
|
package/package.json
CHANGED