Files
jina-ai--reader/src/lib/filtered-stream.ts
T
wehub-resource-sync f1ba9c6c36
/ test (push) Failing after 1s
/ build-and-push-to-ghcr (push) Has been skipped
chore: import upstream snapshot with attribution
2026-07-13 12:23:39 +08:00

60 lines
1.6 KiB
TypeScript

import { Transform, TransformCallback } from "stream";
import _ from 'lodash';
class FilteredTextResponseStream extends Transform {
constructor(public path: string, public predicate?: (data: any) => boolean) {
super({ writableObjectMode: true, encoding: 'utf-8' });
}
override _transform(chunk: any, _encoding: string, callback: TransformCallback) {
if (this.predicate) {
if (!this.predicate(chunk)) {
callback();
return;
}
}
const interest = _.get(chunk, this.path);
if (typeof interest === 'string') {
if (interest) {
this.push(interest);
}
}
callback();
}
}
export function getFilteredTextStream(path: string = 'data.choices[0].delta.content', predicate?: (data: any) => boolean) {
return new FilteredTextResponseStream(path, predicate);
}
class FilteredResponseStream extends Transform {
constructor(public path: string, public predicate?: (data: any) => boolean) {
super({ objectMode: true });
}
override _transform(chunk: any, _encoding: string, callback: TransformCallback) {
if (this.predicate) {
if (!this.predicate(chunk)) {
callback();
return;
}
}
const interest = _.get(chunk, this.path);
if (interest) {
this.push(interest);
}
callback();
}
}
export function getFilteredStream(path: string = 'data.choices[0].delta.content', predicate?: (data: any) => boolean) {
return new FilteredResponseStream(path, predicate);
}