-
Notifications
You must be signed in to change notification settings - Fork 274
✨ feat(lara): upgrade sdk and add streaming translator wrapper #4550
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
Open
riccio82
wants to merge
3
commits into
develop
Choose a base branch
from
feat/lara-patch
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−48
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import {laraTranslate} from './laraTranslate' | ||
|
|
||
| const mockPostAndGetStream = jest.fn() | ||
|
|
||
| jest.mock('@translated/lara', () => { | ||
| class MockTranslator { | ||
| constructor() { | ||
| this.client = {postAndGetStream: mockPostAndGetStream} | ||
| } | ||
| } | ||
| return { | ||
| AuthToken: jest.fn(), | ||
| Translator: MockTranslator, | ||
| } | ||
| }) | ||
|
|
||
| global.config = { | ||
| source_rfc: 'en-US', | ||
| target_rfc: 'it-IT', | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| mockPostAndGetStream.mockReset() | ||
| }) | ||
|
|
||
| async function* asyncIterableOf(...items) { | ||
| for (const item of items) { | ||
| yield item | ||
| } | ||
| } | ||
|
|
||
| test('Returns last streamed translation result', async () => { | ||
| mockPostAndGetStream.mockReturnValue( | ||
| asyncIterableOf({translation: 'partial'}, {translation: 'Ciao mondo'}), | ||
| ) | ||
|
|
||
| const result = await laraTranslate({ | ||
| token: 'fake-token', | ||
| source: 'Hello world', | ||
| contextListBefore: [], | ||
| contextListAfter: [], | ||
| sid: '1', | ||
| jobId: '100', | ||
| glossaries: [], | ||
| style: undefined, | ||
| reasoning: false, | ||
| }) | ||
|
|
||
| expect(result).toEqual({translation: 'Ciao mondo'}) | ||
| }) | ||
|
|
||
| test('Builds text blocks with context before and after', async () => { | ||
| mockPostAndGetStream.mockReturnValue( | ||
| asyncIterableOf({translation: 'result'}), | ||
| ) | ||
|
|
||
| await laraTranslate({ | ||
| token: 'fake-token', | ||
| source: 'Main segment', | ||
| contextListBefore: ['ctx before 1', 'ctx before 2'], | ||
| contextListAfter: ['ctx after 1'], | ||
| sid: '5', | ||
| jobId: '200', | ||
| glossaries: [], | ||
| style: undefined, | ||
| reasoning: false, | ||
| }) | ||
|
|
||
| const callArgs = mockPostAndGetStream.mock.calls[0] | ||
| const endpoint = callArgs[0] | ||
| const payload = callArgs[1] | ||
|
|
||
| expect(endpoint).toBe('/v2/translate') | ||
| expect(payload.q).toEqual([ | ||
| {text: 'ctx before 1', translatable: false}, | ||
| {text: 'ctx before 2', translatable: false}, | ||
| {text: 'Main segment', translatable: true}, | ||
| {text: 'ctx after 1', translatable: false}, | ||
| ]) | ||
| }) | ||
|
|
||
| test('Passes correct translation options', async () => { | ||
| mockPostAndGetStream.mockReturnValue( | ||
| asyncIterableOf({translation: 'result'}), | ||
| ) | ||
|
|
||
| await laraTranslate({ | ||
| token: 'fake-token', | ||
| source: 'Hello', | ||
| contextListBefore: [], | ||
| contextListAfter: [], | ||
| sid: '3', | ||
| jobId: '50', | ||
| glossaries: [{id: 'g1'}], | ||
| style: 'formal', | ||
| reasoning: true, | ||
| }) | ||
|
|
||
| const callArgs = mockPostAndGetStream.mock.calls[0] | ||
| const payload = callArgs[1] | ||
| const headers = callArgs[3] | ||
|
|
||
| expect(payload.source).toBe('en-US') | ||
| expect(payload.target).toBe('it-IT') | ||
| expect(payload.multiline).toBe(false) | ||
| expect(payload.content_type).toBe('application/xliff+xml') | ||
| expect(payload.glossaries).toEqual([{id: 'g1'}]) | ||
| expect(payload.reasoning).toBe(true) | ||
| expect(payload.style).toBe('formal') | ||
| expect(headers).toEqual({'X-Lara-Engine-Tuid': '50:3'}) | ||
| }) | ||
|
|
||
| test('Sets X-Lara-Engine-Tuid header from jobId and sid', async () => { | ||
| mockPostAndGetStream.mockReturnValue( | ||
| asyncIterableOf({translation: 'result'}), | ||
| ) | ||
|
|
||
| await laraTranslate({ | ||
| token: 'token', | ||
| source: 'text', | ||
| contextListBefore: [], | ||
| contextListAfter: [], | ||
| sid: '42', | ||
| jobId: '999', | ||
| glossaries: [], | ||
| style: undefined, | ||
| reasoning: false, | ||
| }) | ||
|
|
||
| const headers = mockPostAndGetStream.mock.calls[0][3] | ||
| expect(headers).toEqual({'X-Lara-Engine-Tuid': '999:42'}) | ||
| }) | ||
|
|
||
| test('Throws error when stream yields no results', async () => { | ||
| mockPostAndGetStream.mockReturnValue(asyncIterableOf()) | ||
|
|
||
| await expect( | ||
| laraTranslate({ | ||
| token: 'token', | ||
| source: 'text', | ||
| contextListBefore: [], | ||
| contextListAfter: [], | ||
| sid: '1', | ||
| jobId: '1', | ||
| glossaries: [], | ||
| style: undefined, | ||
| reasoning: false, | ||
| }), | ||
| ).rejects.toThrow('No translation result received.') | ||
| }) | ||
|
|
||
| test('Returns final result when multiple partials are streamed with reasoning', async () => { | ||
| const partial1 = {translation: 'thinking...', reasoning: 'step 1'} | ||
| const partial2 = {translation: 'final answer', reasoning: 'step 2'} | ||
| mockPostAndGetStream.mockReturnValue(asyncIterableOf(partial1, partial2)) | ||
|
|
||
| const result = await laraTranslate({ | ||
| token: 'token', | ||
| source: 'text', | ||
| contextListBefore: [], | ||
| contextListAfter: [], | ||
| sid: '1', | ||
| jobId: '1', | ||
| glossaries: [], | ||
| style: undefined, | ||
| reasoning: true, | ||
| }) | ||
|
|
||
| expect(result).toEqual(partial2) | ||
| }) |
Oops, something went wrong.
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.