forked from CMU-313/cmu-313-s26-nodebb-spring-26-NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: restore and complete LLM translation feature #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8af48ed
feat: add Python/Flask LLM translation microservice
JoshuaNam c620110
feat: restore async translation on post creation
JoshuaNam 266a683
feat: add translation loading state and socket update handler
JoshuaNam db601bb
test: add mock tests for translate module
JoshuaNam b7ee030
chore: carry over OpenAPI schema updates for translation fields
JoshuaNam a893c3b
fix: show loading indicator immediately upon reply
cirex-web ea016ec
refactor: move websockets require to top level in posts/create.js
JoshuaNam 62be558
chore: remove LLM microservice source from repo
JoshuaNam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,6 +207,8 @@ get: | |
| type: number | ||
| isEnglish: | ||
| type: boolean | ||
| isTranslating: | ||
| type: boolean | ||
| tags: | ||
| type: array | ||
| items: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ const groups = require('../groups'); | |
| const activitypub = require('../activitypub'); | ||
| const utils = require('../utils'); | ||
| const translate = require('../translate'); | ||
| const websockets = require('../socket.io'); | ||
|
|
||
| module.exports = function (Posts) { | ||
| Posts.create = async function (data) { | ||
|
|
@@ -19,9 +20,6 @@ module.exports = function (Posts) { | |
| const timestamp = data.timestamp || Date.now(); | ||
| const isMain = data.isMain || false; | ||
| let hasAttachment = false; | ||
| const [isEnglish, translatedContent] = await translate.translate(data); | ||
|
|
||
|
|
||
| if (!uid && parseInt(uid, 10) !== 0) { | ||
| throw new Error('[[error:invalid-uid]]'); | ||
| } | ||
|
|
@@ -31,7 +29,7 @@ module.exports = function (Posts) { | |
| } | ||
|
|
||
| const pid = data.pid || await db.incrObjectField('global', 'nextPid'); | ||
| let postData = { pid, uid, tid, content, sourceContent, timestamp, isEnglish, translatedContent}; | ||
| let postData = { pid, uid, tid, content, sourceContent, timestamp, isEnglish: 'loading', translatedContent: '', isTranslating: true }; | ||
|
|
||
| if (data.toPid) { | ||
| postData.toPid = data.toPid; | ||
|
|
@@ -75,6 +73,22 @@ module.exports = function (Posts) { | |
| ({ post: postData } = await plugins.hooks.fire('filter:post.create', { post: postData, data: data })); | ||
| await db.setObject(`post:${postData.pid}`, postData); | ||
|
|
||
| // Fire-and-forget translation | ||
| const savedPid = postData.pid; | ||
| const savedTid = tid; | ||
| translate.translate(postData).then(([isEng, transContent]) => { | ||
| Posts.setPostFields(savedPid, { isEnglish: isEng, translatedContent: transContent, isTranslating: false }); | ||
| if (websockets.server) { | ||
| websockets.in(`topic_${savedTid}`).emit('event:post_translated', { | ||
| pid: savedPid, | ||
| isEnglish: isEng, | ||
| translatedContent: transContent, | ||
| }); | ||
| } | ||
| }).catch(() => { | ||
| Posts.setPostFields(savedPid, { isEnglish: 'true', translatedContent: '', isTranslating: false }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error handling good |
||
| }); | ||
|
|
||
| const topicData = await topics.getTopicFields(tid, ['cid', 'pinned']); | ||
| postData.cid = topicData.cid; | ||
| await Promise.all([ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,20 @@ | ||
|
|
||
| /* eslint-disable strict */ | ||
| //var request = require('request'); | ||
| 'use strict'; | ||
| const nconf = require('nconf'); | ||
|
|
||
| const translatorApi = module.exports; | ||
|
|
||
| translatorApi.translate = function (postData) { | ||
| return ['is_english',postData]; | ||
| const TRANSLATOR_URL = nconf.get('llm_endpoint') || 'http://localhost:5000'; | ||
|
|
||
| translatorApi.translate = async function (postData) { | ||
| try { | ||
| const response = await fetch( | ||
| `${TRANSLATOR_URL}/?content=${encodeURIComponent(postData.content)}` | ||
| ); | ||
| const data = await response.json(); | ||
| const isEnglish = data.is_english ? 'true' : 'false'; | ||
| const translatedContent = data.translated_content || ''; | ||
| return [isEnglish, translatedContent]; | ||
| } catch (err) { | ||
| return ['true', '']; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
|
|
||
| const db = require('./mocks/databasemock'); | ||
|
|
||
| describe('Translate', () => { | ||
| let originalFetch; | ||
|
|
||
| beforeEach(() => { | ||
| originalFetch = global.fetch; | ||
| // Clear the module cache so each test gets a fresh require | ||
| delete require.cache[require.resolve('../src/translate/index')]; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| global.fetch = originalFetch; | ||
| }); | ||
|
|
||
| it('should return [true, ""] when fetch throws a network error', async () => { | ||
| global.fetch = async () => { | ||
| throw new Error('network error'); | ||
| }; | ||
| const translate = require('../src/translate/index'); | ||
| const result = await translate.translate({ content: 'hello world' }); | ||
| assert.deepStrictEqual(result, ['true', '']); | ||
| }); | ||
|
|
||
| it('should return [false, translated text] for non-English response', async () => { | ||
| global.fetch = async () => ({ | ||
| json: async () => ({ is_english: false, translated_content: 'hello world' }), | ||
| }); | ||
| const translate = require('../src/translate/index'); | ||
| const result = await translate.translate({ content: 'hola mundo' }); | ||
| assert.deepStrictEqual(result, ['false', 'hello world']); | ||
| }); | ||
|
|
||
| it('should return [true, ""] for English response', async () => { | ||
| global.fetch = async () => ({ | ||
| json: async () => ({ is_english: true, translated_content: '' }), | ||
| }); | ||
| const translate = require('../src/translate/index'); | ||
| const result = await translate.translate({ content: 'hello world' }); | ||
| assert.deepStrictEqual(result, ['true', '']); | ||
| }); | ||
|
|
||
| it('should return [true, ""] for emoji content with English response', async () => { | ||
| global.fetch = async () => ({ | ||
| json: async () => ({ is_english: true, translated_content: '' }), | ||
| }); | ||
| const translate = require('../src/translate/index'); | ||
| const result = await translate.translate({ content: '😀🎉' }); | ||
| assert.deepStrictEqual(result, ['true', '']); | ||
| }); | ||
|
|
||
| it('should return [true, ""] when response contains malformed JSON', async () => { | ||
| global.fetch = async () => ({ | ||
| json: async () => { throw new SyntaxError('Unexpected token'); }, | ||
| }); | ||
| const translate = require('../src/translate/index'); | ||
| const result = await translate.translate({ content: 'hello world' }); | ||
| assert.deepStrictEqual(result, ['true', '']); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.