|
|
import { BaseOutputParser } from './base-parser.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ListOutputParser extends BaseOutputParser {
|
|
|
constructor(options = {}) {
|
|
|
super();
|
|
|
this.separator = options.separator;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async parse(text) {
|
|
|
const cleaned = text.trim();
|
|
|
|
|
|
|
|
|
if (this.separator) {
|
|
|
return cleaned
|
|
|
.split(this.separator)
|
|
|
.map(item => item.trim())
|
|
|
.filter(item => item.length > 0);
|
|
|
}
|
|
|
|
|
|
|
|
|
if (this._isNumberedList(cleaned)) {
|
|
|
return this._parseNumberedList(cleaned);
|
|
|
}
|
|
|
|
|
|
if (this._isBulletList(cleaned)) {
|
|
|
return this._parseBulletList(cleaned);
|
|
|
}
|
|
|
|
|
|
|
|
|
if (cleaned.includes(',')) {
|
|
|
return cleaned
|
|
|
.split(',')
|
|
|
.map(item => item.trim())
|
|
|
.filter(item => item.length > 0);
|
|
|
}
|
|
|
|
|
|
|
|
|
return cleaned
|
|
|
.split('\n')
|
|
|
.map(item => item.trim())
|
|
|
.filter(item => item.length > 0);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_isNumberedList(text) {
|
|
|
return /^\d+\./.test(text);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_isBulletList(text) {
|
|
|
return /^[-*•]/.test(text);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_parseNumberedList(text) {
|
|
|
return text
|
|
|
.split('\n')
|
|
|
.map(line => line.replace(/^\d+\.\s*/, '').trim())
|
|
|
.filter(item => item.length > 0);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_parseBulletList(text) {
|
|
|
return text
|
|
|
.split('\n')
|
|
|
.map(line => line.replace(/^[-*•]\s*/, '').trim())
|
|
|
.filter(item => item.length > 0);
|
|
|
}
|
|
|
|
|
|
getFormatInstructions() {
|
|
|
if (this.separator) {
|
|
|
return `Respond with items separated by "${this.separator}".`;
|
|
|
}
|
|
|
return 'Respond with a numbered list (1. Item) or bullet list (- Item).';
|
|
|
}
|
|
|
} |