Skip to content

Commit 5b5e6af

Browse files
authored
Merge pull request #28 from Avivbens/feat/support-source-language-detection-in-translate
2 parents a76f165 + 6086696 commit 5b5e6af

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

projects/packages/text-transformer/info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2434,7 +2434,7 @@ https://github.com/Avivbens/alfredo</string>
24342434
</dict>
24352435
</array>
24362436
<key>version</key>
2437-
<string>5.4.1</string>
2437+
<string>5.4.2</string>
24382438
<key>webaddress</key>
24392439
<string>https://github.com/Avivbens/alfredo</string>
24402440
</dict>

projects/packages/text-transformer/src/main/translate/translate.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import type { AlfredListItem } from 'fast-alfred';
22
import { FastAlfred } from 'fast-alfred';
33
import { setTimeout } from 'node:timers/promises';
44
import { getActiveApp } from '@alfredo/active-app';
5-
import { AvailableModels, callModel } from '@alfredo/llm';
5+
import { AvailableModels, callModelWithStructuredResponse } from '@alfredo/llm';
66
import { registerUpdater } from '@alfredo/updater';
77
import { DEFAULT_DEBOUNCE_TIME, LANGUAGE_DELIMITER } from '../../common/defaults.constants';
88
import { TRANSLATE_SYSTEM_PROMPT } from '../../common/prompts/translate.prompt';
99
import { Variables } from '../../common/variables.enum';
10+
import { translateResultSchema } from '../../models/translate-results.schema';
1011

1112
interface ParsedTranslationInput {
1213
targetLanguage: string;
@@ -88,13 +89,22 @@ function parseLanguageFromInput(input: string): ParsedTranslationInput {
8889
targetLanguage,
8990
});
9091

91-
const res = await callModel(token, model, { system, user: textToTranslate });
92-
92+
/**
93+
* Call the model with structured response parsing
94+
*/
95+
const { translation, sourceLanguage } = await callModelWithStructuredResponse(
96+
token,
97+
model,
98+
{ system, user: textToTranslate },
99+
translateResultSchema,
100+
);
101+
102+
const subtitle = `${sourceLanguage} => ${targetLanguage}`;
93103
const items: AlfredListItem[] = [
94104
{
95-
title: res,
96-
subtitle: 'Translate',
97-
arg: res,
105+
title: translation,
106+
subtitle,
107+
arg: translation,
98108
},
99109
];
100110

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import z from 'zod';
2+
3+
export const translateResultSchema = z.object({
4+
translation: z.string(),
5+
sourceLanguage: z
6+
.string()
7+
.describe(
8+
'The original language of the provided text. Should be the full name of the language, e.g., "English", "Spanish", "French".',
9+
),
10+
});

0 commit comments

Comments
 (0)