dev: implement generic extract_text for ai messages

This commit is contained in:
KernelDeimos
2025-01-29 16:08:30 -05:00
parent e9279ffb36
commit 0009ededfa
2 changed files with 71 additions and 0 deletions
@@ -46,4 +46,30 @@ module.exports = class Messages {
messages[i] = this.normalize_single_message(messages[i], params);
}
}
static extract_text (messages) {
return messages.map(m => {
if ( whatis(m) === 'string' ) {
return m;
}
if ( whatis(m) !== 'object' ) {
return '';
}
if ( whatis(m.content) === 'array' ) {
return m.content.map(c => c.text).join(' ');
}
if ( whatis(m.content) === 'string' ) {
return m.content;
} else {
const is_text_type = m.content.type === 'text' ||
! m.content.hasOwnProperty('type');
if ( is_text_type ) {
if ( whatis(m.content.text) !== 'string' ) {
throw new Error('text content must be a string');
}
return m.content.text;
}
return '';
}
}).join(' ');
}
}
@@ -25,4 +25,49 @@ describe('Messages', () => {
});
}
});
describe('extract_text', () => {
const cases = [
{
name: 'string message',
input: ['Hello, world!'],
output: 'Hello, world!',
},
{
name: 'object message',
input: [{
content: [
{
type: 'text',
text: 'Hello, world!',
}
]
}],
output: 'Hello, world!',
},
{
name: 'irregular messages',
input: [
'First Part',
{
content: [
{
type: 'text',
text: 'Second Part',
}
]
},
{
content: 'Third Part',
}
],
output: 'First Part Second Part Third Part',
}
];
for ( const tc of cases ) {
it(`should extract text from ${tc.name}`, () => {
const output = Messages.extract_text(tc.input);
expect(output).to.equal(tc.output);
});
}
});
});