mirror of
https://github.com/HeyPuter/puter.git
synced 2026-04-27 18:51:45 -05:00
dev: implement generic extract_text for ai messages
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user