@baishuyun/coze-provider 0.0.16 → 0.1.0
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +19 -0
- package/dist/index.js +162 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +162 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/coze-language-model.ts +77 -19
- package/src/stream-trasnformer/fields-json-transform-stream.ts +21 -23
- package/src/types.ts +25 -2
- package/src/utils/accumulate-sse-response.ts +83 -0
- package/src/utils/build-generate-result-by-chat-res.ts +71 -0
- package/src/utils/create-coze-non-streaming-chat.ts +54 -0
- package/src/utils/fields-json-rebuild/index.ts +3 -0
- package/src/utils/get-coze-chat-msg-detail.ts +19 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @baishuyun/coze-provider@0.0.
|
|
3
|
+
> @baishuyun/coze-provider@0.0.17 build /Users/bbbottle/Code/bs-chat-sdk/packages/coze-provider
|
|
4
4
|
> tsup --tsconfig tsconfig.json --clean
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
[34mCLI[39m Cleaning output folder
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
13
|
[34mESM[39m Build start
|
|
14
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
15
|
-
[32mCJS[39m [1mdist/index.js.map [22m[
|
|
16
|
-
[32mCJS[39m ⚡️ Build success in
|
|
17
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
18
|
-
[32mESM[39m [1mdist/index.mjs.map [22m[
|
|
19
|
-
[32mESM[39m ⚡️ Build success in
|
|
14
|
+
[32mCJS[39m [1mdist/index.js [22m[32m50.91 KB[39m
|
|
15
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m94.87 KB[39m
|
|
16
|
+
[32mCJS[39m ⚡️ Build success in 30ms
|
|
17
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m48.87 KB[39m
|
|
18
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m94.55 KB[39m
|
|
19
|
+
[32mESM[39m ⚡️ Build success in 31ms
|
|
20
20
|
DTS Build start
|
|
21
|
-
DTS ⚡️ Build success in
|
|
21
|
+
DTS ⚡️ Build success in 860ms
|
|
22
22
|
DTS dist/index.d.ts 1.57 KB
|
|
23
23
|
DTS dist/index.d.mts 1.57 KB
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @baishuyun/coze-provider
|
|
2
2
|
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- widget agent
|
|
8
|
+
- 4acb2cc: widget agent
|
|
9
|
+
- 35058a9: widget agent
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- e5dd21a: widget agent
|
|
14
|
+
- a934bc0: add widget agent
|
|
15
|
+
|
|
16
|
+
## 0.0.17
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- update style and bug fix
|
|
21
|
+
|
|
3
22
|
## 0.0.16
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -45,8 +45,8 @@ var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
|
45
45
|
var VERSION = "0.0.1";
|
|
46
46
|
|
|
47
47
|
// src/coze-language-model.ts
|
|
48
|
-
var
|
|
49
|
-
var
|
|
48
|
+
var import_parse_sse2 = require("parse-sse");
|
|
49
|
+
var import_web2 = require("stream/web");
|
|
50
50
|
|
|
51
51
|
// src/utils/prompt-to-model-msg.ts
|
|
52
52
|
var import_api = require("@coze/api");
|
|
@@ -140,6 +140,62 @@ var convertPromptToMessages = (messages) => {
|
|
|
140
140
|
});
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
+
// src/utils/accumulate-sse-response.ts
|
|
144
|
+
var import_parse_sse = require("parse-sse");
|
|
145
|
+
var import_web = require("stream/web");
|
|
146
|
+
async function accumulateSSEResponse(body) {
|
|
147
|
+
const sseStream = body.pipeThrough(
|
|
148
|
+
new import_web.TextDecoderStream()
|
|
149
|
+
).pipeThrough(new import_parse_sse.ServerSentEventTransformStream());
|
|
150
|
+
let textContent = "";
|
|
151
|
+
let reasoningContent = "";
|
|
152
|
+
let finishReason = "stop";
|
|
153
|
+
let usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
154
|
+
const reader = sseStream.getReader();
|
|
155
|
+
try {
|
|
156
|
+
while (true) {
|
|
157
|
+
const { done, value: evt } = await reader.read();
|
|
158
|
+
if (done) break;
|
|
159
|
+
if (!evt) continue;
|
|
160
|
+
let evtData;
|
|
161
|
+
try {
|
|
162
|
+
evtData = JSON.parse(evt.data);
|
|
163
|
+
} catch {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const event = evt.type;
|
|
167
|
+
if (event === "conversation.message.delta") {
|
|
168
|
+
if (evtData.reasoning_content) {
|
|
169
|
+
reasoningContent += evtData.reasoning_content;
|
|
170
|
+
} else if (evtData.content) {
|
|
171
|
+
textContent += evtData.content;
|
|
172
|
+
}
|
|
173
|
+
} else if (event === "conversation.chat.completed" || event === "done") {
|
|
174
|
+
finishReason = "stop";
|
|
175
|
+
if (evtData.usage) {
|
|
176
|
+
usage = {
|
|
177
|
+
inputTokens: evtData.usage.input_count || 0,
|
|
178
|
+
outputTokens: evtData.usage.output_count || 0,
|
|
179
|
+
totalTokens: evtData.usage.total_count || 0
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
} else if (event === "error") {
|
|
183
|
+
finishReason = "error";
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
} finally {
|
|
187
|
+
reader.releaseLock();
|
|
188
|
+
}
|
|
189
|
+
const content = [];
|
|
190
|
+
if (reasoningContent) {
|
|
191
|
+
content.push({ type: "reasoning", text: reasoningContent });
|
|
192
|
+
}
|
|
193
|
+
if (textContent) {
|
|
194
|
+
content.push({ type: "text", text: textContent });
|
|
195
|
+
}
|
|
196
|
+
return { content, finishReason, usage };
|
|
197
|
+
}
|
|
198
|
+
|
|
143
199
|
// src/utils/create-error-stream.ts
|
|
144
200
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
145
201
|
var createErrorStream = (error) => {
|
|
@@ -366,6 +422,31 @@ var LLMPartTrasnformStream = class extends TransformStream {
|
|
|
366
422
|
}
|
|
367
423
|
};
|
|
368
424
|
|
|
425
|
+
// src/utils/build-generate-result-by-chat-res.ts
|
|
426
|
+
var BuildWarningGenerateResult = (message) => {
|
|
427
|
+
return {
|
|
428
|
+
content: [],
|
|
429
|
+
finishReason: "error",
|
|
430
|
+
request: {
|
|
431
|
+
body: null
|
|
432
|
+
},
|
|
433
|
+
usage: {
|
|
434
|
+
inputTokens: 0,
|
|
435
|
+
outputTokens: 0,
|
|
436
|
+
totalTokens: 0
|
|
437
|
+
},
|
|
438
|
+
response: {
|
|
439
|
+
body: null
|
|
440
|
+
},
|
|
441
|
+
warnings: [
|
|
442
|
+
{
|
|
443
|
+
type: "other",
|
|
444
|
+
message
|
|
445
|
+
}
|
|
446
|
+
]
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
|
|
369
450
|
// src/coze-language-model.ts
|
|
370
451
|
var CozeLanguageModel = class {
|
|
371
452
|
specificationVersion = "v2";
|
|
@@ -379,8 +460,59 @@ var CozeLanguageModel = class {
|
|
|
379
460
|
this.supportedUrls = config.supportedUrls?.() || {};
|
|
380
461
|
this.config = config;
|
|
381
462
|
}
|
|
382
|
-
doGenerate(options) {
|
|
383
|
-
|
|
463
|
+
async doGenerate(options) {
|
|
464
|
+
const messages = convertPromptToMessages(options.prompt);
|
|
465
|
+
const warnings = [];
|
|
466
|
+
const url = this.config.url({ path: "/chat" });
|
|
467
|
+
const user_id = options.headers ? options.headers["x-user-id"] : "";
|
|
468
|
+
const user_var = options.headers ? options.headers["x-user-var"] : "";
|
|
469
|
+
let customVar;
|
|
470
|
+
if (user_var) {
|
|
471
|
+
try {
|
|
472
|
+
customVar = JSON.parse(user_var);
|
|
473
|
+
} catch (e) {
|
|
474
|
+
console.warn("x-user-var parse error", e);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const body = {
|
|
478
|
+
bot_id: this.config.botId,
|
|
479
|
+
auto_save_history: false,
|
|
480
|
+
user_id: user_id || "7543191937730215936",
|
|
481
|
+
stream: true,
|
|
482
|
+
additional_messages: messages
|
|
483
|
+
};
|
|
484
|
+
if (customVar) {
|
|
485
|
+
body.custom_variables = customVar;
|
|
486
|
+
}
|
|
487
|
+
const headers = this.config.headers();
|
|
488
|
+
const abortSignal = options.abortSignal;
|
|
489
|
+
const response = await fetch(url, {
|
|
490
|
+
method: "POST",
|
|
491
|
+
headers,
|
|
492
|
+
credentials: "include",
|
|
493
|
+
body: JSON.stringify(body),
|
|
494
|
+
signal: abortSignal
|
|
495
|
+
});
|
|
496
|
+
if (!response.ok || !response.body) {
|
|
497
|
+
const rawError = await response.text();
|
|
498
|
+
let errorDetail;
|
|
499
|
+
try {
|
|
500
|
+
errorDetail = JSON.parse(rawError);
|
|
501
|
+
} catch {
|
|
502
|
+
errorDetail = rawError;
|
|
503
|
+
}
|
|
504
|
+
const errorMessage = typeof errorDetail === "object" && errorDetail?.msg ? errorDetail.msg : `Coze API error: ${response.status} ${response.statusText}`;
|
|
505
|
+
return BuildWarningGenerateResult(errorMessage);
|
|
506
|
+
}
|
|
507
|
+
const { content, finishReason, usage } = await accumulateSSEResponse(response.body);
|
|
508
|
+
return {
|
|
509
|
+
content,
|
|
510
|
+
finishReason,
|
|
511
|
+
usage,
|
|
512
|
+
request: { body },
|
|
513
|
+
response: { body: null },
|
|
514
|
+
warnings
|
|
515
|
+
};
|
|
384
516
|
}
|
|
385
517
|
isVisibleMsgPart(evtData) {
|
|
386
518
|
if (evtData.type === "knowledge" && evtData.role === "assistant") {
|
|
@@ -394,7 +526,7 @@ var CozeLanguageModel = class {
|
|
|
394
526
|
const url = this.config.url({
|
|
395
527
|
path: "/chat"
|
|
396
528
|
});
|
|
397
|
-
const user_id =
|
|
529
|
+
const user_id = options.headers ? options.headers["x-user-id"] : "";
|
|
398
530
|
const user_var = options.headers ? options.headers["x-user-var"] : "";
|
|
399
531
|
let customVar;
|
|
400
532
|
if (user_var) {
|
|
@@ -407,14 +539,12 @@ var CozeLanguageModel = class {
|
|
|
407
539
|
const body = {
|
|
408
540
|
bot_id: this.config.botId,
|
|
409
541
|
auto_save_history: false,
|
|
410
|
-
user_id: "7543191937730215936",
|
|
411
|
-
// user_id,
|
|
542
|
+
user_id: user_id || "7543191937730215936",
|
|
412
543
|
stream: true,
|
|
413
544
|
additional_messages: messages
|
|
414
545
|
};
|
|
415
|
-
console.log("Request body for Coze API:", JSON.stringify(body));
|
|
416
546
|
if (customVar) {
|
|
417
|
-
body.
|
|
547
|
+
body.custom_variables = customVar;
|
|
418
548
|
}
|
|
419
549
|
const headers = this.config.headers();
|
|
420
550
|
const abortSignal = options.abortSignal;
|
|
@@ -446,8 +576,8 @@ var CozeLanguageModel = class {
|
|
|
446
576
|
const isBuildStage = stage === "build";
|
|
447
577
|
const isFillStage = stage === "fill";
|
|
448
578
|
const result = response.body.pipeThrough(
|
|
449
|
-
new
|
|
450
|
-
).pipeThrough(new
|
|
579
|
+
new import_web2.TextDecoderStream()
|
|
580
|
+
).pipeThrough(new import_parse_sse2.ServerSentEventTransformStream()).pipeThrough(new MCPChunkFilterTransformStream(isBuildStage || isFillStage)).pipeThrough(new LLMChunkInjectorTransformStream()).pipeThrough(new LLMPartTrasnformStream());
|
|
451
581
|
let finalResult = result;
|
|
452
582
|
if (externalTransformers.length > 0) {
|
|
453
583
|
finalResult = externalTransformers.reduce(
|
|
@@ -1692,6 +1822,9 @@ var RebuilderMap = {
|
|
|
1692
1822
|
dept: rebuildDept,
|
|
1693
1823
|
sn: rebuildSn,
|
|
1694
1824
|
deptgroup: rebuildDeptGroup,
|
|
1825
|
+
flowState: defaultBuilder,
|
|
1826
|
+
flowstate: defaultBuilder,
|
|
1827
|
+
formula: defaultBuilder,
|
|
1695
1828
|
unknown: defaultBuilder
|
|
1696
1829
|
};
|
|
1697
1830
|
var rebuildFieldsJsonStructure = (truncatedJson, subItemsOnly = false) => {
|
|
@@ -1709,9 +1842,12 @@ var rebuildFieldsJsonStructure = (truncatedJson, subItemsOnly = false) => {
|
|
|
1709
1842
|
// src/stream-trasnformer/fields-json-transform-stream.ts
|
|
1710
1843
|
var createFieldsJsonTransformStream = (enableJsonParser) => {
|
|
1711
1844
|
let parser;
|
|
1845
|
+
let enqueueTextDelta;
|
|
1712
1846
|
let id;
|
|
1847
|
+
let isFirstChunk = true;
|
|
1848
|
+
let passthroughMode = false;
|
|
1713
1849
|
const chunkGuard = (chunk) => {
|
|
1714
|
-
if (!enableJsonParser) {
|
|
1850
|
+
if (!enableJsonParser || passthroughMode) {
|
|
1715
1851
|
return false;
|
|
1716
1852
|
}
|
|
1717
1853
|
if (chunk.type !== "text-delta" || !("delta" in chunk)) {
|
|
@@ -1728,19 +1864,28 @@ var createFieldsJsonTransformStream = (enableJsonParser) => {
|
|
|
1728
1864
|
}
|
|
1729
1865
|
},
|
|
1730
1866
|
transform: (chunk, controller) => {
|
|
1731
|
-
if (!parser) {
|
|
1867
|
+
if (!parser && !passthroughMode) {
|
|
1732
1868
|
return;
|
|
1733
1869
|
}
|
|
1734
1870
|
try {
|
|
1735
1871
|
if (chunkGuard(chunk)) {
|
|
1736
1872
|
if ("id" in chunk && "delta" in chunk) {
|
|
1737
1873
|
id = chunk.id;
|
|
1738
|
-
|
|
1874
|
+
let delta = chunk.delta;
|
|
1875
|
+
if (isFirstChunk) {
|
|
1876
|
+
delta = delta.replace(/^\uFEFF/, "");
|
|
1877
|
+
isFirstChunk = false;
|
|
1878
|
+
}
|
|
1879
|
+
parser.write(delta);
|
|
1880
|
+
} else {
|
|
1881
|
+
console.warn("Unexpected chunk format for JSON parsing:", chunk);
|
|
1882
|
+
controller.enqueue(chunk);
|
|
1739
1883
|
}
|
|
1740
1884
|
} else {
|
|
1741
1885
|
controller.enqueue(chunk);
|
|
1742
1886
|
}
|
|
1743
1887
|
} catch (e) {
|
|
1888
|
+
console.log("write or enqueue error", e);
|
|
1744
1889
|
controller.error(e);
|
|
1745
1890
|
}
|
|
1746
1891
|
},
|
|
@@ -1748,7 +1893,7 @@ var createFieldsJsonTransformStream = (enableJsonParser) => {
|
|
|
1748
1893
|
parser = new import_json.JSONParser({
|
|
1749
1894
|
paths: ["$.*"]
|
|
1750
1895
|
});
|
|
1751
|
-
|
|
1896
|
+
enqueueTextDelta = enableJsonParser ? createTextInfoEnqueuer(controller) : (content) => {
|
|
1752
1897
|
};
|
|
1753
1898
|
parser.onValue = (parsedInfo) => {
|
|
1754
1899
|
const value = parsedInfo.value;
|
|
@@ -1764,12 +1909,9 @@ var createFieldsJsonTransformStream = (enableJsonParser) => {
|
|
|
1764
1909
|
id
|
|
1765
1910
|
);
|
|
1766
1911
|
};
|
|
1767
|
-
let errorLogged = false;
|
|
1768
1912
|
parser.onError = (err) => {
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
errorLogged = true;
|
|
1772
|
-
}
|
|
1913
|
+
console.error("JsonWidgetStream: JSON Parsing Error:", err);
|
|
1914
|
+
passthroughMode = true;
|
|
1773
1915
|
};
|
|
1774
1916
|
parser.onEnd = () => {
|
|
1775
1917
|
enqueueTextDelta(
|