浏览代码

dev: implement generic extract_text for ai messages

KernelDeimos 4 月之前
父节点
当前提交
0009ededfa
共有 2 个文件被更改,包括 71 次插入0 次删除
  1. 26 0
      src/backend/src/modules/puterai/lib/Messages.js
  2. 45 0
      src/backend/src/modules/puterai/lib/messages.test.js

+ 26 - 0
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(' ');
+    }
 }

+ 45 - 0
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);
+            });
+        }
+    });
 });