From 0009ededfa1885251338880b72fbe87960cdd168 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Wed, 29 Jan 2025 16:08:30 -0500 Subject: [PATCH] dev: implement generic extract_text for ai messages --- .../src/modules/puterai/lib/Messages.js | 26 +++++++++++ .../src/modules/puterai/lib/messages.test.js | 45 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/backend/src/modules/puterai/lib/Messages.js b/src/backend/src/modules/puterai/lib/Messages.js index 5739c1ca3..e3ab62340 100644 --- a/src/backend/src/modules/puterai/lib/Messages.js +++ b/src/backend/src/modules/puterai/lib/Messages.js @@ -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(' '); + } } \ No newline at end of file diff --git a/src/backend/src/modules/puterai/lib/messages.test.js b/src/backend/src/modules/puterai/lib/messages.test.js index 5f5603fea..f7ca36956 100644 --- a/src/backend/src/modules/puterai/lib/messages.test.js +++ b/src/backend/src/modules/puterai/lib/messages.test.js @@ -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); + }); + } + }); });