@dxos/assistant 0.8.2-main.fbd8ed0 → 0.8.2-staging.4d6ad0f
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/lib/browser/index.mjs +1983 -64
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +1976 -63
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1984 -64
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/{src/bin → bin}/chat.d.ts.map +1 -1
- package/dist/types/src/blueprint/blueprint.d.ts +79 -0
- package/dist/types/src/blueprint/blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprint/index.d.ts +4 -0
- package/dist/types/src/blueprint/index.d.ts.map +1 -0
- package/dist/types/src/blueprint/logger.d.ts +3 -0
- package/dist/types/src/blueprint/logger.d.ts.map +1 -0
- package/dist/types/src/{ai-service/parser.d.ts → blueprint/machine.d.ts} +46 -68
- package/dist/types/src/blueprint/machine.d.ts.map +1 -0
- package/dist/types/src/blueprint/test-data.d.ts +3 -0
- package/dist/types/src/blueprint/test-data.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +4 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +210 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +3 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +14 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction.d.ts +242 -0
- package/dist/types/src/extraction/extraction.d.ts.map +1 -0
- package/dist/types/src/extraction/index.d.ts +5 -0
- package/dist/types/src/extraction/index.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.d.ts +15 -0
- package/dist/types/src/extraction/quotes.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.test.d.ts +2 -0
- package/dist/types/src/extraction/quotes.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/research/exa.d.ts +7 -0
- package/dist/types/src/research/exa.d.ts.map +1 -0
- package/dist/types/src/research/graph.d.ts +39 -0
- package/dist/types/src/research/graph.d.ts.map +1 -0
- package/dist/types/src/research/graph.test.d.ts +2 -0
- package/dist/types/src/research/graph.test.d.ts.map +1 -0
- package/dist/types/src/research/index.d.ts +4 -0
- package/dist/types/src/research/index.d.ts.map +1 -0
- package/dist/types/src/research/research.d.ts +10 -0
- package/dist/types/src/research/research.d.ts.map +1 -0
- package/dist/types/src/research/research.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +0 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +47 -22
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +3 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/summarization/summarization.test.d.ts +2 -0
- package/dist/types/src/summarization/summarization.test.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +38 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +37 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +58 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -7
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +3 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/package.json +22 -38
- package/src/blueprint/blueprint.test.ts +142 -0
- package/src/blueprint/blueprint.ts +95 -0
- package/src/blueprint/index.ts +7 -0
- package/src/blueprint/logger.ts +66 -0
- package/src/blueprint/machine.ts +208 -0
- package/src/blueprint/test-data.ts +55 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +95 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +64 -0
- package/src/extraction/extraction-llm-function/index.ts +5 -0
- package/src/extraction/extraction-llm-function/instructions.tpl +28 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +115 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +54 -0
- package/src/extraction/extraction-ner-function/index.ts +6 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +94 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +97 -0
- package/src/extraction/extraction.ts +75 -0
- package/src/extraction/index.ts +8 -0
- package/src/extraction/quotes.test.ts +27 -0
- package/src/extraction/quotes.ts +57 -0
- package/src/index.ts +4 -4
- package/src/research/exa.ts +100 -0
- package/src/research/graph.test.ts +15 -0
- package/src/research/graph.ts +291 -0
- package/src/research/index.ts +7 -0
- package/src/research/instructions-research.tpl +97 -0
- package/src/research/research.test.ts +189 -0
- package/src/research/research.ts +89 -0
- package/src/session/index.ts +0 -1
- package/src/session/session.test.ts +7 -7
- package/src/session/session.ts +46 -19
- package/src/session/version-pin.ts +2 -3
- package/src/summarization/summarization.test.ts +216 -0
- package/src/testing/exa-search-1748337321991.ts +131 -0
- package/src/testing/exa-search-1748337331526.ts +144 -0
- package/src/testing/exa-search-1748337344119.ts +133 -0
- package/src/testing/index.ts +8 -8
- package/src/types.ts +8 -0
- package/src/typings.d.ts +8 -0
- package/dist/lib/browser/chunk-B4HAWUFW.mjs +0 -2786
- package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -658
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node/chunk-NOB7CIIC.cjs +0 -2791
- package/dist/lib/node/chunk-NOB7CIIC.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -672
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs +0 -2788
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -659
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/ai-service/client.test.d.ts +0 -2
- package/dist/types/src/ai-service/client.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/defs.d.ts +0 -9
- package/dist/types/src/ai-service/defs.d.ts.map +0 -1
- package/dist/types/src/ai-service/edge-client.d.ts +0 -18
- package/dist/types/src/ai-service/edge-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/index.d.ts +0 -10
- package/dist/types/src/ai-service/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/message-collector.d.ts +0 -82
- package/dist/types/src/ai-service/message-collector.d.ts.map +0 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +0 -43
- package/dist/types/src/ai-service/ollama-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/parser.d.ts.map +0 -1
- package/dist/types/src/ai-service/service.d.ts +0 -25
- package/dist/types/src/ai-service/service.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.d.ts +0 -40
- package/dist/types/src/ai-service/stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.test.d.ts +0 -2
- package/dist/types/src/ai-service/stream.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/index.d.ts +0 -3
- package/dist/types/src/ai-service/testing/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +0 -11
- package/dist/types/src/ai-service/testing/mock.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/test-stream.d.ts +0 -7
- package/dist/types/src/ai-service/testing/test-stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/transform.d.ts +0 -27
- package/dist/types/src/ai-service/transform.d.ts.map +0 -1
- package/dist/types/src/ai-service/types.d.ts +0 -210
- package/dist/types/src/ai-service/types.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/index.d.ts +0 -2
- package/dist/types/src/ai-service/util/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/sse.d.ts +0 -39
- package/dist/types/src/ai-service/util/sse.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/conversation.d.ts +0 -61
- package/dist/types/src/experimental/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/conversation.test.d.ts +0 -2
- package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/index.d.ts +0 -2
- package/dist/types/src/experimental/conversation/index.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/types.d.ts +0 -78
- package/dist/types/src/experimental/conversation/types.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/ast.d.ts +0 -56
- package/dist/types/src/experimental/cypher/ast.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.d.ts +0 -14
- package/dist/types/src/experimental/cypher/combinators.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts +0 -16
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.d.ts +0 -49
- package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/index.d.ts +0 -5
- package/dist/types/src/experimental/cypher/index.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parjs.d.ts +0 -4
- package/dist/types/src/experimental/cypher/parjs.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parser.d.ts +0 -6
- package/dist/types/src/experimental/cypher/parser.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.d.ts +0 -26
- package/dist/types/src/experimental/cypher/query-executor.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.d.ts +0 -12
- package/dist/types/src/experimental/cypher/schema.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/schema.test.d.ts.map +0 -1
- package/dist/types/src/experimental/index.d.ts +0 -3
- package/dist/types/src/experimental/index.d.ts.map +0 -1
- package/dist/types/src/experimental/ner/ner.test.d.ts +0 -2
- package/dist/types/src/experimental/ner/ner.test.d.ts.map +0 -1
- package/dist/types/src/experimental/research/exa.d.ts +0 -6
- package/dist/types/src/experimental/research/exa.d.ts.map +0 -1
- package/dist/types/src/experimental/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/tools.d.ts +0 -18
- package/dist/types/src/session/tools.d.ts.map +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts.map +0 -1
- package/dist/types/src/testing/data.d.ts +0 -2
- package/dist/types/src/testing/data.d.ts.map +0 -1
- package/dist/types/src/testing/defs.d.ts +0 -5
- package/dist/types/src/testing/defs.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -12
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts.map +0 -1
- package/dist/types/src/testing/ollama.d.ts +0 -6
- package/dist/types/src/testing/ollama.d.ts.map +0 -1
- package/dist/types/src/testing/printer.d.ts +0 -6
- package/dist/types/src/testing/printer.d.ts.map +0 -1
- package/dist/types/src/testing/query-promts.d.ts +0 -5
- package/dist/types/src/testing/query-promts.d.ts.map +0 -1
- package/dist/types/src/testing/query.test.d.ts +0 -2
- package/dist/types/src/testing/query.test.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -29
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -52
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -2
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/trim.d.ts +0 -5
- package/dist/types/src/util/trim.d.ts.map +0 -1
- package/src/ai-service/client.test.ts +0 -247
- package/src/ai-service/defs.ts +0 -39
- package/src/ai-service/edge-client.ts +0 -60
- package/src/ai-service/index.ts +0 -13
- package/src/ai-service/message-collector.ts +0 -84
- package/src/ai-service/ollama-client.ts +0 -506
- package/src/ai-service/parser.ts +0 -369
- package/src/ai-service/service.ts +0 -31
- package/src/ai-service/stream.test.ts +0 -125
- package/src/ai-service/stream.ts +0 -118
- package/src/ai-service/testing/index.ts +0 -6
- package/src/ai-service/testing/mock.ts +0 -30
- package/src/ai-service/testing/test-stream.ts +0 -93
- package/src/ai-service/transform.ts +0 -121
- package/src/ai-service/types.ts +0 -112
- package/src/ai-service/util/index.ts +0 -5
- package/src/ai-service/util/sse.ts +0 -341
- package/src/bin/chat.ts +0 -79
- package/src/experimental/conversation/conversation.test.ts +0 -73
- package/src/experimental/conversation/conversation.ts +0 -142
- package/src/experimental/conversation/index.ts +0 -5
- package/src/experimental/conversation/types.ts +0 -126
- package/src/experimental/cypher/ast.ts +0 -75
- package/src/experimental/cypher/combinators.test.ts +0 -137
- package/src/experimental/cypher/combinators.ts +0 -148
- package/src/experimental/cypher/echo-data-source.ts +0 -105
- package/src/experimental/cypher/execution-plan.test.ts +0 -44
- package/src/experimental/cypher/execution-plan.ts +0 -208
- package/src/experimental/cypher/index.ts +0 -8
- package/src/experimental/cypher/parjs-shims.d.ts +0 -10
- package/src/experimental/cypher/parjs.ts +0 -10
- package/src/experimental/cypher/parser.ts +0 -19
- package/src/experimental/cypher/query-executor.test.ts +0 -47
- package/src/experimental/cypher/query-executor.ts +0 -145
- package/src/experimental/cypher/schema.test.ts +0 -17
- package/src/experimental/cypher/schema.ts +0 -86
- package/src/experimental/index.ts +0 -6
- package/src/experimental/ner/ner.test.ts +0 -163
- package/src/experimental/research/exa.ts +0 -38
- package/src/experimental/research/instructions.tpl +0 -64
- package/src/experimental/research/research.test.ts +0 -263
- package/src/session/tools.ts +0 -117
- package/src/testing/artifacts.test.ts +0 -182
- package/src/testing/data.ts +0 -13
- package/src/testing/defs.ts +0 -9
- package/src/testing/logger.ts +0 -129
- package/src/testing/object-extraction.test.ts +0 -71
- package/src/testing/ollama.ts +0 -15
- package/src/testing/printer.ts +0 -41
- package/src/testing/query-promts.ts +0 -75
- package/src/testing/query.test.ts +0 -88
- package/src/testing/test-data.ts +0 -379
- package/src/testing/test-schema.ts +0 -79
- package/src/util/index.ts +0 -5
- package/src/util/trim.ts +0 -15
- package/vendor/parjs-browser.mjs +0 -4
- package/vendor/parjs-default.mjs +0 -8
- /package/dist/types/{src/bin → bin}/chat.d.ts +0 -0
- /package/dist/types/src/{experimental/research → research}/research.test.d.ts +0 -0
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { ObjectId } from '@dxos/echo-schema';
|
|
6
|
-
|
|
7
|
-
import { type GenerationStreamEvent } from '../types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Mock server-side events (SSE) stream.
|
|
11
|
-
*/
|
|
12
|
-
export const createTestSSEStream = (
|
|
13
|
-
pars: (string | object)[],
|
|
14
|
-
{ splitBy = 'word' }: { splitBy?: 'word' | 'character' } = {},
|
|
15
|
-
): ReadableStream => {
|
|
16
|
-
const encoder = new TextEncoder();
|
|
17
|
-
|
|
18
|
-
return new ReadableStream({
|
|
19
|
-
start: (controller) => {
|
|
20
|
-
const push = (event: GenerationStreamEvent) => {
|
|
21
|
-
controller.enqueue(encoder.encode(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`));
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
push({
|
|
25
|
-
type: 'message_start',
|
|
26
|
-
message: {
|
|
27
|
-
id: ObjectId.random(),
|
|
28
|
-
role: 'assistant',
|
|
29
|
-
content: [],
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
for (const block of pars) {
|
|
34
|
-
let index = 0;
|
|
35
|
-
if (typeof block === 'string') {
|
|
36
|
-
push({
|
|
37
|
-
type: 'content_block_start',
|
|
38
|
-
index,
|
|
39
|
-
content: {
|
|
40
|
-
type: 'text',
|
|
41
|
-
text: '',
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
for (const word of block.split(splitBy === 'word' ? /([ \t\n]+)/ : '')) {
|
|
46
|
-
push({
|
|
47
|
-
type: 'content_block_delta',
|
|
48
|
-
index,
|
|
49
|
-
delta: {
|
|
50
|
-
type: 'text_delta',
|
|
51
|
-
text: word,
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
index += word.length + 1;
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
push({
|
|
58
|
-
type: 'content_block_start',
|
|
59
|
-
index,
|
|
60
|
-
content: {
|
|
61
|
-
type: 'json',
|
|
62
|
-
json: '',
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const text = JSON.stringify(block, null, 2);
|
|
67
|
-
for (const line of text.split('\n')) {
|
|
68
|
-
push({
|
|
69
|
-
type: 'content_block_delta',
|
|
70
|
-
index,
|
|
71
|
-
delta: {
|
|
72
|
-
type: 'input_json_delta',
|
|
73
|
-
partial_json: line,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
index += line.length + 1;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
push({
|
|
81
|
-
type: 'content_block_stop',
|
|
82
|
-
index,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
push({
|
|
87
|
-
type: 'message_stop',
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
controller.close();
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
};
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as P from 'parsimmon';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Parsed block.
|
|
9
|
-
*/
|
|
10
|
-
export type StreamBlock =
|
|
11
|
-
| {
|
|
12
|
-
type: 'tag';
|
|
13
|
-
tag: string;
|
|
14
|
-
attributes?: Record<string, string>;
|
|
15
|
-
content: StreamBlock[];
|
|
16
|
-
closing?: boolean;
|
|
17
|
-
selfClosing?: boolean;
|
|
18
|
-
}
|
|
19
|
-
| { type: 'text'; content: string }
|
|
20
|
-
| { type: 'json'; disposition?: string; content: string };
|
|
21
|
-
|
|
22
|
-
const textChunk: P.Parser<StreamBlock> = P.regexp(/[^<]+/).map((content) => ({
|
|
23
|
-
type: 'text',
|
|
24
|
-
content,
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
const attribute: P.Parser<[string, string]> = P.seq(
|
|
28
|
-
P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/),
|
|
29
|
-
P.string('=').trim(P.optWhitespace),
|
|
30
|
-
P.alt(
|
|
31
|
-
P.regexp(/"[^"]*"/).map((s) => s.slice(1, -1)),
|
|
32
|
-
P.regexp(/'[^']*'/).map((s) => s.slice(1, -1)),
|
|
33
|
-
),
|
|
34
|
-
).map(([name, _, value]) => [name, value]);
|
|
35
|
-
|
|
36
|
-
const tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
|
|
37
|
-
|
|
38
|
-
const selfClosingTag: P.Parser<StreamBlock> = P.seqMap(
|
|
39
|
-
//
|
|
40
|
-
P.string('<'),
|
|
41
|
-
tagName,
|
|
42
|
-
P.optWhitespace.then(attribute).many(),
|
|
43
|
-
P.optWhitespace.then(P.string('/>')),
|
|
44
|
-
P.optWhitespace,
|
|
45
|
-
(_, tag, attributes, __) => ({
|
|
46
|
-
type: 'tag',
|
|
47
|
-
tag,
|
|
48
|
-
attributes: Object.fromEntries(attributes),
|
|
49
|
-
content: [],
|
|
50
|
-
selfClosing: true,
|
|
51
|
-
}),
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
const openTag: P.Parser<StreamBlock> = P.seqMap(
|
|
55
|
-
//
|
|
56
|
-
P.string('<'),
|
|
57
|
-
tagName,
|
|
58
|
-
P.optWhitespace.then(attribute).many(),
|
|
59
|
-
P.string('>'),
|
|
60
|
-
P.regexp(/[^<]*/),
|
|
61
|
-
(_, tag, attributes, __, content) => ({
|
|
62
|
-
type: 'tag',
|
|
63
|
-
tag,
|
|
64
|
-
attributes: Object.fromEntries(attributes),
|
|
65
|
-
content: content.length ? [{ type: 'text', content }] : [],
|
|
66
|
-
}),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const closeTag: P.Parser<StreamBlock> = P.seqMap(
|
|
70
|
-
//
|
|
71
|
-
P.string('</'),
|
|
72
|
-
tagName,
|
|
73
|
-
P.string('>'),
|
|
74
|
-
P.optWhitespace,
|
|
75
|
-
(_, tag) => ({
|
|
76
|
-
type: 'tag',
|
|
77
|
-
tag,
|
|
78
|
-
content: [],
|
|
79
|
-
closing: true,
|
|
80
|
-
}),
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
const mixedChunk: P.Parser<StreamBlock> = P.alt(selfClosingTag, openTag, closeTag, textChunk);
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Permissive streaming transformer that processes mixed content (plain text and XML fragments) from the AI service.
|
|
87
|
-
*/
|
|
88
|
-
export class StreamTransform {
|
|
89
|
-
private _buffer = '';
|
|
90
|
-
|
|
91
|
-
transform(chunk: string): StreamBlock[] {
|
|
92
|
-
this._buffer += chunk;
|
|
93
|
-
|
|
94
|
-
const results: StreamBlock[] = [];
|
|
95
|
-
const parser = mixedChunk.many().skip(P.optWhitespace);
|
|
96
|
-
const result = parser.parse(this._buffer);
|
|
97
|
-
if (result.status) {
|
|
98
|
-
for (const chunk of result.value) {
|
|
99
|
-
results.push(chunk);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this._buffer = '';
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return results;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// TODO(burdon): Process remaining buffer?
|
|
109
|
-
flush(): StreamBlock[] {
|
|
110
|
-
const remaining = this._buffer;
|
|
111
|
-
this._buffer = '';
|
|
112
|
-
return remaining.length > 0
|
|
113
|
-
? [
|
|
114
|
-
{
|
|
115
|
-
type: 'text',
|
|
116
|
-
content: remaining,
|
|
117
|
-
},
|
|
118
|
-
]
|
|
119
|
-
: [];
|
|
120
|
-
}
|
|
121
|
-
}
|
package/src/ai-service/types.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Schema } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { Tool, Message, type MessageContentBlock } from '@dxos/artifact';
|
|
8
|
-
import { type ObjectId } from '@dxos/echo-schema';
|
|
9
|
-
|
|
10
|
-
import { DEFAULT_EDGE_MODELS, DEFAULT_OLLAMA_MODELS } from './defs';
|
|
11
|
-
|
|
12
|
-
export const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;
|
|
13
|
-
|
|
14
|
-
export const LLMModel = Schema.Literal(...DEFAULT_EDGE_MODELS, ...DEFAULT_OLLAMA_MODELS);
|
|
15
|
-
|
|
16
|
-
export type LLMModel = Schema.Schema.Type<typeof LLMModel>;
|
|
17
|
-
|
|
18
|
-
export const ToolTypes = Object.freeze({
|
|
19
|
-
// TODO(dmaretskyi): Not implemented yet.
|
|
20
|
-
// DatabaseQuery: 'database_query',
|
|
21
|
-
TextToImage: 'text_to_image',
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Client GPT request.
|
|
26
|
-
*/
|
|
27
|
-
export const GenerateRequest = Schema.Struct({
|
|
28
|
-
/**
|
|
29
|
-
* Preferred model or system default.
|
|
30
|
-
*/
|
|
31
|
-
model: Schema.optional(LLMModel),
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Tools available for the LLM.
|
|
35
|
-
*/
|
|
36
|
-
tools: Schema.optional(Schema.Array(Tool).pipe(Schema.mutable)),
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* System instructions to the LLM.
|
|
40
|
-
*/
|
|
41
|
-
systemPrompt: Schema.optional(Schema.String),
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* History of messages to include in the context window.
|
|
45
|
-
*/
|
|
46
|
-
// TODO(burdon): Rename messages.
|
|
47
|
-
history: Schema.optional(Schema.Array(Message)),
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Current request.
|
|
51
|
-
*/
|
|
52
|
-
prompt: Schema.optional(Message),
|
|
53
|
-
});
|
|
54
|
-
export type GenerateRequest = Schema.Schema.Type<typeof GenerateRequest>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Non-streaming response.
|
|
58
|
-
*/
|
|
59
|
-
export const GenerateResponse = Schema.Struct({
|
|
60
|
-
messages: Schema.Array(Message),
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Number of tokens used in the response.
|
|
64
|
-
*/
|
|
65
|
-
tokenCount: Schema.optional(Schema.Number),
|
|
66
|
-
});
|
|
67
|
-
export type GenerateResponse = Schema.Schema.Type<typeof GenerateResponse>;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Server-Sent Events (SSE) stream from the AI service.
|
|
71
|
-
* https://docs.anthropic.com/en/api/streaming
|
|
72
|
-
* https://platform.openai.com/docs/api-reference/streaming
|
|
73
|
-
* https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
|
|
74
|
-
*/
|
|
75
|
-
// TODO(dmaretskyi): Effect schema.
|
|
76
|
-
export type GenerationStreamEvent =
|
|
77
|
-
| {
|
|
78
|
-
// TODO(dmaretskyi): Normalize types to our schema.
|
|
79
|
-
type: 'message_start';
|
|
80
|
-
message: Omit<Message, 'spaceId' | 'threadId'>;
|
|
81
|
-
}
|
|
82
|
-
| {
|
|
83
|
-
type: 'message_delta';
|
|
84
|
-
delta: {
|
|
85
|
-
stopReason: 'tool_use' | 'end_turn';
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
| {
|
|
89
|
-
type: 'message_stop';
|
|
90
|
-
}
|
|
91
|
-
| {
|
|
92
|
-
type: 'content_block_start';
|
|
93
|
-
index: number;
|
|
94
|
-
content: MessageContentBlock;
|
|
95
|
-
}
|
|
96
|
-
| {
|
|
97
|
-
type: 'content_block_delta';
|
|
98
|
-
index: number;
|
|
99
|
-
delta:
|
|
100
|
-
| {
|
|
101
|
-
type: 'text_delta';
|
|
102
|
-
text: string;
|
|
103
|
-
}
|
|
104
|
-
| {
|
|
105
|
-
type: 'input_json_delta';
|
|
106
|
-
partial_json: string;
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
| {
|
|
110
|
-
type: 'content_block_stop';
|
|
111
|
-
index: number;
|
|
112
|
-
};
|
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
// Copied from anthropic SDK source
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Server sent events (SSE) iterator.
|
|
8
|
-
* @param response
|
|
9
|
-
* @param controller
|
|
10
|
-
*/
|
|
11
|
-
export async function* iterSSEMessages(
|
|
12
|
-
response: Response,
|
|
13
|
-
controller: AbortController,
|
|
14
|
-
): AsyncGenerator<ServerSentEvent, void, unknown> {
|
|
15
|
-
if (!response.body) {
|
|
16
|
-
controller.abort();
|
|
17
|
-
throw new Error('Attempted to iterate over a response with no body');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const sseDecoder = new SSEDecoder();
|
|
21
|
-
const lineDecoder = new LineDecoder();
|
|
22
|
-
|
|
23
|
-
const iter = readableStreamAsyncIterable<Bytes>(response.body);
|
|
24
|
-
for await (const sseChunk of iterSSEChunks(iter)) {
|
|
25
|
-
for (const line of lineDecoder.decode(sseChunk)) {
|
|
26
|
-
const sse = sseDecoder.decode(line);
|
|
27
|
-
if (sse) {
|
|
28
|
-
yield sse;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
for (const line of lineDecoder.flush()) {
|
|
34
|
-
const sse = sseDecoder.decode(line);
|
|
35
|
-
if (sse) {
|
|
36
|
-
yield sse;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export type ServerSentEvent = {
|
|
42
|
-
event: string | null;
|
|
43
|
-
data: string;
|
|
44
|
-
raw: string[];
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Given an async iterable iterator, iterates over it and yields full
|
|
49
|
-
* SSE chunks, i.e. yields when a double new-line is encountered.
|
|
50
|
-
*/
|
|
51
|
-
async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
|
|
52
|
-
let data = new Uint8Array();
|
|
53
|
-
|
|
54
|
-
for await (const chunk of iterator) {
|
|
55
|
-
if (chunk == null) {
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const binaryChunk =
|
|
60
|
-
chunk instanceof ArrayBuffer
|
|
61
|
-
? new Uint8Array(chunk)
|
|
62
|
-
: typeof chunk === 'string'
|
|
63
|
-
? new TextEncoder().encode(chunk)
|
|
64
|
-
: chunk;
|
|
65
|
-
|
|
66
|
-
const newData = new Uint8Array(data.length + binaryChunk.length);
|
|
67
|
-
newData.set(data);
|
|
68
|
-
newData.set(binaryChunk, data.length);
|
|
69
|
-
data = newData;
|
|
70
|
-
|
|
71
|
-
let patternIndex;
|
|
72
|
-
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
|
|
73
|
-
yield data.slice(0, patternIndex);
|
|
74
|
-
data = data.slice(patternIndex);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (data.length > 0) {
|
|
79
|
-
yield data;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const findDoubleNewlineIndex = (buffer: Uint8Array): number => {
|
|
84
|
-
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
|
|
85
|
-
// and returns the index right after the first occurrence of any pattern,
|
|
86
|
-
// or -1 if none of the patterns are found.
|
|
87
|
-
const newline = 0x0a; // \n
|
|
88
|
-
const carriage = 0x0d; // \r
|
|
89
|
-
|
|
90
|
-
for (let i = 0; i < buffer.length - 2; i++) {
|
|
91
|
-
if (buffer[i] === newline && buffer[i + 1] === newline) {
|
|
92
|
-
// \n\n
|
|
93
|
-
return i + 2;
|
|
94
|
-
}
|
|
95
|
-
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
|
|
96
|
-
// \r\r
|
|
97
|
-
return i + 2;
|
|
98
|
-
}
|
|
99
|
-
if (
|
|
100
|
-
buffer[i] === carriage &&
|
|
101
|
-
buffer[i + 1] === newline &&
|
|
102
|
-
i + 3 < buffer.length &&
|
|
103
|
-
buffer[i + 2] === carriage &&
|
|
104
|
-
buffer[i + 3] === newline
|
|
105
|
-
) {
|
|
106
|
-
// \r\n\r\n
|
|
107
|
-
return i + 4;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return -1;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
class SSEDecoder {
|
|
115
|
-
private data: string[];
|
|
116
|
-
private event: string | null;
|
|
117
|
-
private chunks: string[];
|
|
118
|
-
|
|
119
|
-
constructor() {
|
|
120
|
-
this.event = null;
|
|
121
|
-
this.data = [];
|
|
122
|
-
this.chunks = [];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
decode(line: string) {
|
|
126
|
-
if (line.endsWith('\r')) {
|
|
127
|
-
line = line.substring(0, line.length - 1);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (!line) {
|
|
131
|
-
// empty line and we didn't previously encounter any messages
|
|
132
|
-
if (!this.event && !this.data.length) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const sse: ServerSentEvent = {
|
|
137
|
-
event: this.event,
|
|
138
|
-
data: this.data.join('\n'),
|
|
139
|
-
raw: this.chunks,
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
this.event = null;
|
|
143
|
-
this.data = [];
|
|
144
|
-
this.chunks = [];
|
|
145
|
-
|
|
146
|
-
return sse;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
this.chunks.push(line);
|
|
150
|
-
|
|
151
|
-
if (line.startsWith(':')) {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
let [fieldname, _, value] = partition(line, ':');
|
|
156
|
-
|
|
157
|
-
if (value.startsWith(' ')) {
|
|
158
|
-
value = value.substring(1);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (fieldname === 'event') {
|
|
162
|
-
this.event = value;
|
|
163
|
-
} else if (fieldname === 'data') {
|
|
164
|
-
this.data.push(value);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** This is an internal helper function that's just used for testing */
|
|
172
|
-
export const _decodeChunks = (chunks: string[]): string[] => {
|
|
173
|
-
const decoder = new LineDecoder();
|
|
174
|
-
const lines: string[] = [];
|
|
175
|
-
for (const chunk of chunks) {
|
|
176
|
-
lines.push(...decoder.decode(chunk));
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return lines;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
const partition = (str: string, delimiter: string): [string, string, string] => {
|
|
183
|
-
const index = str.indexOf(delimiter);
|
|
184
|
-
if (index !== -1) {
|
|
185
|
-
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return [str, '', ''];
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Most browsers don't yet have async iterable support for ReadableStream,
|
|
193
|
-
* and Node has a very different way of reading bytes from its "ReadableStream".
|
|
194
|
-
*
|
|
195
|
-
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
|
|
196
|
-
*/
|
|
197
|
-
export function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
|
|
198
|
-
if (stream[Symbol.asyncIterator]) {
|
|
199
|
-
return stream;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const reader = stream.getReader();
|
|
203
|
-
return {
|
|
204
|
-
next: async () => {
|
|
205
|
-
try {
|
|
206
|
-
const result = await reader.read();
|
|
207
|
-
if (result?.done) {
|
|
208
|
-
reader.releaseLock();
|
|
209
|
-
} // release lock when stream becomes closed
|
|
210
|
-
return result;
|
|
211
|
-
} catch (e) {
|
|
212
|
-
reader.releaseLock(); // release lock when stream becomes errored
|
|
213
|
-
throw e;
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
return: async () => {
|
|
217
|
-
const cancelPromise = reader.cancel();
|
|
218
|
-
reader.releaseLock();
|
|
219
|
-
await cancelPromise;
|
|
220
|
-
return { done: true, value: undefined };
|
|
221
|
-
},
|
|
222
|
-
[Symbol.asyncIterator]() {
|
|
223
|
-
return this;
|
|
224
|
-
},
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
|
|
232
|
-
* reading lines from text.
|
|
233
|
-
*
|
|
234
|
-
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
|
|
235
|
-
*/
|
|
236
|
-
export class LineDecoder {
|
|
237
|
-
// prettier-ignore
|
|
238
|
-
static NEWLINE_CHARS = new Set(['\n', '\r']);
|
|
239
|
-
static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
240
|
-
|
|
241
|
-
buffer: string[];
|
|
242
|
-
trailingCR: boolean;
|
|
243
|
-
textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
|
|
244
|
-
|
|
245
|
-
constructor() {
|
|
246
|
-
this.buffer = [];
|
|
247
|
-
this.trailingCR = false;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
decode(chunk: Bytes): string[] {
|
|
251
|
-
let text = this.decodeText(chunk);
|
|
252
|
-
|
|
253
|
-
if (this.trailingCR) {
|
|
254
|
-
text = '\r' + text;
|
|
255
|
-
this.trailingCR = false;
|
|
256
|
-
}
|
|
257
|
-
if (text.endsWith('\r')) {
|
|
258
|
-
this.trailingCR = true;
|
|
259
|
-
text = text.slice(0, -1);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (!text) {
|
|
263
|
-
return [];
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
|
|
267
|
-
let lines = text.split(LineDecoder.NEWLINE_REGEXP);
|
|
268
|
-
|
|
269
|
-
// if there is a trailing new line then the last entry will be an empty
|
|
270
|
-
// string which we don't care about
|
|
271
|
-
if (trailingNewline) {
|
|
272
|
-
lines.pop();
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if (lines.length === 1 && !trailingNewline) {
|
|
276
|
-
this.buffer.push(lines[0]!);
|
|
277
|
-
return [];
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
if (this.buffer.length > 0) {
|
|
281
|
-
lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
|
|
282
|
-
this.buffer = [];
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (!trailingNewline) {
|
|
286
|
-
this.buffer = [lines.pop() || ''];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return lines;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
decodeText(bytes: Bytes): string {
|
|
293
|
-
if (bytes == null) {
|
|
294
|
-
return '';
|
|
295
|
-
}
|
|
296
|
-
if (typeof bytes === 'string') {
|
|
297
|
-
return bytes;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// Node:
|
|
301
|
-
if (typeof Buffer !== 'undefined') {
|
|
302
|
-
if (bytes instanceof Buffer) {
|
|
303
|
-
return bytes.toString();
|
|
304
|
-
}
|
|
305
|
-
if (bytes instanceof Uint8Array) {
|
|
306
|
-
return Buffer.from(bytes).toString();
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
throw new Error(
|
|
310
|
-
`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
// Browser
|
|
315
|
-
if (typeof TextDecoder !== 'undefined') {
|
|
316
|
-
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
|
|
317
|
-
this.textDecoder ??= new TextDecoder('utf8');
|
|
318
|
-
return this.textDecoder.decode(bytes);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
throw new Error(
|
|
322
|
-
`Unexpected: received non-Uint8Array/ArrayBuffer (${
|
|
323
|
-
(bytes as any).constructor.name
|
|
324
|
-
}) in a web platform. Please report this error.`,
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
throw new Error('Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.');
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
flush(): string[] {
|
|
332
|
-
if (!this.buffer.length && !this.trailingCR) {
|
|
333
|
-
return [];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const lines = [this.buffer.join('')];
|
|
337
|
-
this.buffer = [];
|
|
338
|
-
this.trailingCR = false;
|
|
339
|
-
return lines;
|
|
340
|
-
}
|
|
341
|
-
}
|