-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathllms.txt
More file actions
239 lines (173 loc) · 5.48 KB
/
llms.txt
File metadata and controls
239 lines (173 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# stream-chain
> Chain functions, generators, and streams into a single Duplex stream with proper backpressure handling. Zero dependencies.
## Install
npm i stream-chain
## Quick start
```js
import chain from 'stream-chain';
const pipeline = chain([
x => x * x,
x => x % 2 ? x : null,
async x => await process(x)
]);
dataSource.pipe(pipeline).pipe(destination);
```
## API
### chain(fns[, options])
Creates a Duplex stream from an array of functions, streams, or arrays (flattened).
- `fns` (array) — functions, streams, or nested arrays. Falsy values are ignored.
- `options` (object, optional) — Duplex options plus:
- `noGrouping` (boolean) — disable function grouping optimization (default: false).
- `skipEvents` (boolean) — disable error event forwarding (default: false).
- Default: `{writableObjectMode: true, readableObjectMode: true}`.
- Returns: `Duplex` stream with `.streams`, `.input`, `.output` properties.
Supported function types: regular, async, generator, async generator.
### chainUnchecked(fns[, options])
Same as `chain()` but bypasses TypeScript type checking on the `fns` parameter.
```js
import {chainUnchecked} from 'stream-chain';
const pipeline = chainUnchecked([x => x * x]);
```
### Special return values
- `none` — skip, produce no value (same as returning `null`/`undefined`).
- `stop` — skip and terminate the generator pipeline.
- `many(values)` — emit multiple values from a single input.
- `finalValue(value)` — skip remaining chain steps, emit value directly (gen/fun only).
- `flushable(fn, final?)` — mark function to be called at stream end.
```js
import chain from 'stream-chain';
import {none, stop, many, finalValue, flushable} from 'stream-chain/defs.js';
chain([
x => x % 2 ? x : none,
x => many([x, x * 10]),
]);
```
### gen(...fns)
Creates an async generator pipeline from functions. Used internally by `chain()` for grouping.
```js
import gen from 'stream-chain/gen.js';
const g = gen(x => x + 1, x => x * x);
for await (const v of g(2)) console.log(v); // 9
```
### fun(...fns)
Like `gen()` but returns an async function. Generator results are collected into `many()`.
```js
import fun from 'stream-chain/fun.js';
const f = fun(x => x + 1, x => x * x);
console.log(await f(2)); // 9
```
### asStream(fn[, options])
Wraps any function as a Duplex stream.
```js
import asStream from 'stream-chain/asStream.js';
const stream = asStream(x => x * x);
```
### dataSource(fn)
Takes a function or iterable and returns the underlying iterator function.
```js
import {dataSource} from 'stream-chain';
const iter = dataSource([1, 2, 3]);
```
## Utilities
All utilities return functions for use in `chain()`.
### Slicing
- `take(n, finalValue?)` — take N items then stop.
- `takeWhile(fn, finalValue?)` — take while predicate is true.
- `takeWithSkip(n, skip?, finalValue?)` — skip then take.
- `skip(n)` — skip N items.
- `skipWhile(fn)` — skip while predicate is true.
### Folding
- `fold(fn, initial)` — reduce stream to single value at end.
- `reduce(fn, initial)` — alias for fold.
- `scan(fn, initial)` — emit running accumulator after each item.
- `reduceStream(fn, initial)` — reduce as Writable stream with `.accumulator`.
### Stream helpers
- `batch(size)` — group items into arrays of `size`.
- `lines()` — split byte stream into lines.
- `fixUtf8Stream()` — repartition chunks for valid UTF-8.
- `readableFrom({iterable})` — convert iterable to Readable stream.
```js
import take from 'stream-chain/utils/take.js';
import fold from 'stream-chain/utils/fold.js';
import batch from 'stream-chain/utils/batch.js';
chain([
take(10, stop),
batch(3),
fold((acc, x) => acc + x.length, 0)
]);
```
## JSONL support
- `parser(reviver?)` — JSONL parser function (returns gen() pipeline).
- `parserStream(options?)` — JSONL parser as a stream.
- `stringerStream(options?)` — JSONL stringer as a stream.
```js
import chain from 'stream-chain';
import parser from 'stream-chain/jsonl/parser.js';
import fs from 'node:fs';
chain([
fs.createReadStream('data.jsonl'),
parser(),
obj => console.log(obj)
]);
```
## Common patterns
### Object processing pipeline
```js
import chain from 'stream-chain';
const pipeline = chain([
x => x * x,
x => chain.many([x - 1, x, x + 1]),
x => x % 2 ? x : null,
]);
dataSource.pipe(pipeline);
pipeline.on('data', x => console.log(x));
```
### Async pipeline with filtering
```js
chain([
async x => await fetchData(x),
x => x.status === 200 ? x.body : null,
x => JSON.parse(x),
]);
```
### Generator producing multiple values
```js
chain([
function* (x) {
for (let i = 0; i < x; ++i) yield i;
},
x => x * x,
]);
```
### Accumulate and emit at end
```js
import {none, flushable} from 'stream-chain/defs.js';
let sum = 0;
chain([
flushable(x => {
if (x === none) return sum;
sum += x;
return none;
})
]);
```
### Web streams
```js
const readable = new ReadableStream({ /* ... */ });
const writable = new WritableStream({ /* ... */ });
chain([readable, x => x * 2, writable]);
```
## TypeScript
```ts
import chain from 'stream-chain';
import {TypedTransform} from 'stream-chain/typed-streams.js';
const transform = new TypedTransform<number, string>({
objectMode: true,
transform(x, _, cb) { cb(null, String(x)); }
});
const pipeline = chain([transform] as const);
```
## Links
- Docs: https://github.com/uhop/stream-chain/wiki
- npm: https://www.npmjs.com/package/stream-chain
- Full LLM reference: https://github.com/uhop/stream-chain/blob/master/llms-full.txt