graphlit-client 1.0.20250610005 → 1.0.20250610006
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/streaming/chunk-buffer.d.ts +27 -12
- package/dist/streaming/chunk-buffer.js +130 -117
- package/dist/streaming/providers.js +12 -5
- package/package.json +1 -1
@@ -1,25 +1,40 @@
|
|
1
|
+
/**
|
2
|
+
* Breaks an LLM’s streaming token deltas into character, word, or sentence
|
3
|
+
* chunks – or lets you plug in your own chunker.
|
4
|
+
*
|
5
|
+
* Usage
|
6
|
+
* -----
|
7
|
+
* const buf = new ChunkBuffer('sentence');
|
8
|
+
* stream.on('delta', d => buf.addToken(d).forEach(pushToUI));
|
9
|
+
* stream.on('end', () => buf.flush().forEach(pushToUI));
|
10
|
+
*/
|
1
11
|
export type ChunkingStrategy = "character" | "word" | "sentence" | ((text: string) => {
|
2
12
|
chunks: string[];
|
3
13
|
remainder: string;
|
4
14
|
});
|
15
|
+
export interface ChunkerOpts {
|
16
|
+
/** Flush “words” longer than this (default = 50 chars). */
|
17
|
+
maxWordLen?: number;
|
18
|
+
/** Force a break after this many chars with no whitespace (default = 400). */
|
19
|
+
maxBufferNoBreak?: number;
|
20
|
+
}
|
5
21
|
export declare class ChunkBuffer {
|
6
|
-
|
7
|
-
|
8
|
-
private static readonly MAX_BUFFER_NO_BREAK;
|
9
|
-
private readonly graphemeSeg;
|
10
|
-
private readonly wordSeg;
|
11
|
-
private readonly sentenceSeg;
|
12
|
-
private readonly customChunker?;
|
13
|
-
private readonly strategy;
|
14
|
-
constructor(strategy: ChunkingStrategy);
|
15
|
-
/** Feed one LLM token, receive zero-or-more flushed chunks. */
|
22
|
+
constructor(strategy: ChunkingStrategy, opts?: ChunkerOpts);
|
23
|
+
/** Feed one LLM delta; receive zero‑or‑more flushed chunks. */
|
16
24
|
addToken(token: string): string[];
|
17
|
-
/**
|
25
|
+
/** Call when the stream closes to emit the final remainder. */
|
18
26
|
flush(): string[];
|
27
|
+
private buffer;
|
28
|
+
private readonly strategy;
|
29
|
+
private readonly customChunker?;
|
30
|
+
private readonly MAX_WORD_LEN;
|
31
|
+
private readonly MAX_BUFFER_NO_BREAK;
|
32
|
+
private readonly graphemeSeg?;
|
33
|
+
private readonly wordSeg?;
|
34
|
+
private readonly sentenceSeg?;
|
19
35
|
private flushGraphemes;
|
20
36
|
private flushWords;
|
21
37
|
private flushSentences;
|
22
|
-
/** Fallback guard to break up very long runs of text with no natural breaks. */
|
23
38
|
private flushLongRuns;
|
24
39
|
private flushCustom;
|
25
40
|
}
|
@@ -1,15 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* Breaks an LLM’s streaming token deltas into character, word, or sentence
|
3
|
+
* chunks – or lets you plug in your own chunker.
|
4
|
+
*
|
5
|
+
* Usage
|
6
|
+
* -----
|
7
|
+
* const buf = new ChunkBuffer('sentence');
|
8
|
+
* stream.on('delta', d => buf.addToken(d).forEach(pushToUI));
|
9
|
+
* stream.on('end', () => buf.flush().forEach(pushToUI));
|
10
|
+
*/
|
11
|
+
const hasSegmenter = typeof Intl !== "undefined" && "Segmenter" in Intl;
|
1
12
|
export class ChunkBuffer {
|
2
|
-
|
3
|
-
//
|
4
|
-
|
5
|
-
|
6
|
-
// --------------------------------
|
7
|
-
graphemeSeg;
|
8
|
-
wordSeg;
|
9
|
-
sentenceSeg;
|
10
|
-
customChunker;
|
11
|
-
strategy;
|
12
|
-
constructor(strategy) {
|
13
|
+
// ────────────────────────────────────────────────────────────────────
|
14
|
+
// public API
|
15
|
+
// ────────────────────────────────────────────────────────────────────
|
16
|
+
constructor(strategy, opts = {}) {
|
13
17
|
if (typeof strategy === "function") {
|
14
18
|
this.customChunker = strategy;
|
15
19
|
this.strategy = "custom";
|
@@ -17,143 +21,152 @@ export class ChunkBuffer {
|
|
17
21
|
else {
|
18
22
|
this.strategy = strategy;
|
19
23
|
}
|
20
|
-
this.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
this.MAX_WORD_LEN = opts.maxWordLen ?? 50;
|
25
|
+
this.MAX_BUFFER_NO_BREAK = opts.maxBufferNoBreak ?? 400;
|
26
|
+
if (hasSegmenter) {
|
27
|
+
this.graphemeSeg = new Intl.Segmenter(undefined, {
|
28
|
+
granularity: "grapheme",
|
29
|
+
});
|
30
|
+
this.wordSeg = new Intl.Segmenter(undefined, { granularity: "word" });
|
31
|
+
this.sentenceSeg = new Intl.Segmenter(undefined, {
|
32
|
+
granularity: "sentence",
|
33
|
+
});
|
34
|
+
}
|
27
35
|
}
|
28
|
-
/** Feed one LLM
|
36
|
+
/** Feed one LLM delta; receive zero‑or‑more flushed chunks. */
|
29
37
|
addToken(token) {
|
30
38
|
this.buffer += token;
|
31
|
-
if (this.customChunker)
|
39
|
+
if (this.customChunker)
|
32
40
|
return this.flushCustom();
|
33
|
-
|
34
|
-
|
35
|
-
const
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
case "word":
|
42
|
-
newChunks = this.flushWords();
|
43
|
-
break;
|
44
|
-
case "sentence":
|
45
|
-
newChunks = this.flushSentences();
|
46
|
-
break;
|
47
|
-
}
|
48
|
-
return [...longRunChunks, ...newChunks];
|
41
|
+
// emergency bailout for giant uninterrupted text
|
42
|
+
const forced = this.flushLongRuns();
|
43
|
+
const fresh = this.strategy === "character"
|
44
|
+
? this.flushGraphemes()
|
45
|
+
: this.strategy === "word"
|
46
|
+
? this.flushWords()
|
47
|
+
: this.flushSentences();
|
48
|
+
return forced.concat(fresh);
|
49
49
|
}
|
50
|
-
/**
|
50
|
+
/** Call when the stream closes to emit the final remainder. */
|
51
51
|
flush() {
|
52
|
-
if (!this.buffer)
|
52
|
+
if (!this.buffer.length)
|
53
53
|
return [];
|
54
|
-
let finalChunks = [];
|
55
54
|
if (this.customChunker) {
|
56
|
-
// For custom chunkers, flush everything by treating the whole buffer as input.
|
57
55
|
const { chunks, remainder } = this.customChunker(this.buffer);
|
58
|
-
|
59
|
-
|
60
|
-
finalChunks.push(remainder);
|
61
|
-
}
|
56
|
+
this.buffer = "";
|
57
|
+
return [...chunks, remainder].filter(Boolean);
|
62
58
|
}
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
// Re‑use the normal strategy until nothing more flushes.
|
60
|
+
const out = [];
|
61
|
+
while (true) {
|
62
|
+
const next = this.strategy === "character"
|
63
|
+
? this.flushGraphemes()
|
64
|
+
: this.strategy === "word"
|
65
|
+
? this.flushWords()
|
66
|
+
: this.flushSentences();
|
67
|
+
if (!next.length)
|
68
|
+
break;
|
69
|
+
out.push(...next);
|
66
70
|
}
|
71
|
+
if (this.buffer)
|
72
|
+
out.push(this.buffer);
|
67
73
|
this.buffer = "";
|
68
|
-
|
69
|
-
return finalChunks.filter((c) => c.length > 0);
|
74
|
+
return out;
|
70
75
|
}
|
71
|
-
//
|
72
|
-
//
|
73
|
-
//
|
76
|
+
// ────────────────────────────────────────────────────────────────────
|
77
|
+
// internals
|
78
|
+
// ────────────────────────────────────────────────────────────────────
|
79
|
+
buffer = "";
|
80
|
+
strategy;
|
81
|
+
customChunker;
|
82
|
+
MAX_WORD_LEN;
|
83
|
+
MAX_BUFFER_NO_BREAK;
|
84
|
+
// These are only defined when Intl.Segmenter exists.
|
85
|
+
graphemeSeg;
|
86
|
+
wordSeg;
|
87
|
+
sentenceSeg;
|
88
|
+
// -- character ------------------------------------------------------
|
74
89
|
flushGraphemes() {
|
75
|
-
|
76
|
-
|
77
|
-
|
90
|
+
if (!hasSegmenter)
|
91
|
+
return []; // unreachable on modern runtimes
|
92
|
+
const segs = Array.from(this.graphemeSeg.segment(this.buffer)).map((s) => s.segment);
|
93
|
+
/* Strategy: always keep exactly one segment in the buffer.
|
94
|
+
If we only have one segment so far, we don’t know whether it’s
|
95
|
+
complete (could be half a surrogate pair). Wait for more. */
|
96
|
+
if (segs.length <= 1)
|
78
97
|
return [];
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
this.buffer = segments[segments.length - 1];
|
83
|
-
return chunksToFlush;
|
98
|
+
const emit = segs.slice(0, -1);
|
99
|
+
this.buffer = segs[segs.length - 1];
|
100
|
+
return emit;
|
84
101
|
}
|
102
|
+
// -- word -----------------------------------------------------------
|
85
103
|
flushWords() {
|
104
|
+
if (!hasSegmenter)
|
105
|
+
return []; // unreachable on modern runtimes
|
86
106
|
const chunks = [];
|
87
|
-
let
|
88
|
-
let
|
89
|
-
|
90
|
-
const
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
107
|
+
let leadNonWord = "";
|
108
|
+
let word = "";
|
109
|
+
let tailNonWord = "";
|
110
|
+
for (const s of this.wordSeg.segment(this.buffer)) {
|
111
|
+
if (s.isWordLike) {
|
112
|
+
if (word && tailNonWord) {
|
113
|
+
// previous word finished
|
114
|
+
chunks.push(word + tailNonWord);
|
115
|
+
word = tailNonWord = "";
|
116
|
+
}
|
117
|
+
word += s.segment;
|
118
|
+
if (word.length > this.MAX_WORD_LEN) {
|
119
|
+
// force‑break huge “word”
|
120
|
+
chunks.push(word + tailNonWord);
|
121
|
+
word = tailNonWord = "";
|
101
122
|
}
|
102
|
-
currentWord += part.segment;
|
103
123
|
}
|
104
124
|
else {
|
105
|
-
//
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
currentWord = "";
|
113
|
-
currentNonWord = "";
|
125
|
+
// non‑word segment (space / punctuation)
|
126
|
+
if (!word) {
|
127
|
+
leadNonWord += s.segment; // leading whitespace
|
128
|
+
}
|
129
|
+
else {
|
130
|
+
tailNonWord += s.segment; // trailing whitespace
|
131
|
+
}
|
114
132
|
}
|
115
133
|
}
|
116
|
-
//
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
134
|
+
// flush leading non‑word if present and some word followed
|
135
|
+
if (leadNonWord && word) {
|
136
|
+
chunks.push(leadNonWord);
|
137
|
+
leadNonWord = "";
|
138
|
+
}
|
139
|
+
this.buffer = leadNonWord + word + tailNonWord;
|
140
|
+
return chunks.filter(Boolean);
|
121
141
|
}
|
142
|
+
// -- sentence -------------------------------------------------------
|
122
143
|
flushSentences() {
|
123
|
-
|
124
|
-
|
125
|
-
//
|
126
|
-
const
|
127
|
-
let
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
}
|
132
|
-
if (lastMatchIndex === -1) {
|
133
|
-
// No definitive sentence boundary found yet.
|
144
|
+
if (!hasSegmenter)
|
145
|
+
return []; // unreachable on modern runtimes
|
146
|
+
// find last confirmed boundary with regex (includes CJK punctuation)
|
147
|
+
const boundary = /.*?[.?!。!?](\s+|$)/g; // negative‑look‑behind ellipsis left out for perf
|
148
|
+
let last = -1, m;
|
149
|
+
while ((m = boundary.exec(this.buffer)))
|
150
|
+
last = boundary.lastIndex;
|
151
|
+
if (last === -1)
|
134
152
|
return [];
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
this.buffer = this.buffer.substring(lastMatchIndex);
|
139
|
-
// 3. Now, use Intl.Segmenter on the confirmed text to correctly split it.
|
140
|
-
// This handles cases where `textToFlush` contains multiple sentences.
|
141
|
-
return Array.from(this.sentenceSeg.segment(textToFlush))
|
153
|
+
const slice = this.buffer.slice(0, last);
|
154
|
+
this.buffer = this.buffer.slice(last);
|
155
|
+
return Array.from(this.sentenceSeg.segment(slice))
|
142
156
|
.map((s) => s.segment)
|
143
|
-
.filter(
|
157
|
+
.filter(Boolean);
|
144
158
|
}
|
145
|
-
|
159
|
+
// -- long‑run bailout ----------------------------------------------
|
146
160
|
flushLongRuns() {
|
147
|
-
|
148
|
-
// If the buffer is very long and contains no spaces (e.g., a single long word/URL),
|
149
|
-
// force a break to prevent excessive buffering.
|
150
|
-
if (this.buffer.length > ChunkBuffer.MAX_BUFFER_NO_BREAK &&
|
161
|
+
if (this.buffer.length > this.MAX_BUFFER_NO_BREAK &&
|
151
162
|
!/\s/.test(this.buffer)) {
|
152
|
-
|
153
|
-
this.buffer = this.buffer.slice(
|
163
|
+
const head = this.buffer.slice(0, this.MAX_BUFFER_NO_BREAK);
|
164
|
+
this.buffer = this.buffer.slice(this.MAX_BUFFER_NO_BREAK);
|
165
|
+
return [head];
|
154
166
|
}
|
155
|
-
return
|
167
|
+
return [];
|
156
168
|
}
|
169
|
+
// -- custom ---------------------------------------------------------
|
157
170
|
flushCustom() {
|
158
171
|
try {
|
159
172
|
const { chunks, remainder } = this.customChunker(this.buffer);
|
@@ -161,7 +174,7 @@ export class ChunkBuffer {
|
|
161
174
|
return chunks;
|
162
175
|
}
|
163
176
|
catch (err) {
|
164
|
-
console.error("Custom chunker failed
|
177
|
+
console.error("Custom chunker failed – flushing whole buffer to avoid data loss", err);
|
165
178
|
const all = this.buffer;
|
166
179
|
this.buffer = "";
|
167
180
|
return [all];
|
@@ -17,8 +17,12 @@ onEvent, onComplete) {
|
|
17
17
|
stream: true,
|
18
18
|
temperature: specification.openAI?.temperature,
|
19
19
|
//top_p: specification.openAI?.probability,
|
20
|
-
max_completion_tokens: specification.openAI?.completionTokenLimit,
|
21
20
|
};
|
21
|
+
// Only add max_completion_tokens if it's defined
|
22
|
+
if (specification.openAI?.completionTokenLimit) {
|
23
|
+
streamConfig.max_completion_tokens =
|
24
|
+
specification.openAI.completionTokenLimit;
|
25
|
+
}
|
22
26
|
// Add tools if provided
|
23
27
|
if (tools && tools.length > 0) {
|
24
28
|
streamConfig.tools = tools.map((tool) => ({
|
@@ -111,7 +115,7 @@ onEvent, onComplete) {
|
|
111
115
|
stream: true,
|
112
116
|
temperature: specification.anthropic?.temperature,
|
113
117
|
//top_p: specification.anthropic?.probability,
|
114
|
-
max_tokens: specification.anthropic?.completionTokenLimit,
|
118
|
+
max_tokens: specification.anthropic?.completionTokenLimit || 1024, // required
|
115
119
|
};
|
116
120
|
if (systemPrompt) {
|
117
121
|
streamConfig.system = systemPrompt;
|
@@ -207,8 +211,11 @@ onEvent, onComplete) {
|
|
207
211
|
stream: true,
|
208
212
|
temperature: specification.google?.temperature,
|
209
213
|
//top_p: specification.google?.probability,
|
210
|
-
max_tokens: specification.google?.completionTokenLimit,
|
211
214
|
};
|
215
|
+
// Only add max_tokens if it's defined
|
216
|
+
if (specification.google?.completionTokenLimit) {
|
217
|
+
streamConfig.max_tokens = specification.google.completionTokenLimit;
|
218
|
+
}
|
212
219
|
if (systemPrompt) {
|
213
220
|
streamConfig.system = systemPrompt;
|
214
221
|
}
|
@@ -235,8 +242,8 @@ onEvent, onComplete) {
|
|
235
242
|
const model = googleClient.getGenerativeModel({
|
236
243
|
model: modelName,
|
237
244
|
generationConfig: {
|
238
|
-
temperature: streamConfig.temperature
|
239
|
-
maxOutputTokens: streamConfig.max_tokens
|
245
|
+
temperature: streamConfig.temperature,
|
246
|
+
maxOutputTokens: streamConfig.max_tokens,
|
240
247
|
},
|
241
248
|
tools: googleTools,
|
242
249
|
});
|