Skip to content

Commit bd05b75

Browse files
committed
Modified how types are exposed and modified tests
1 parent c823d9b commit bd05b75

File tree

5 files changed

+60
-634
lines changed

5 files changed

+60
-634
lines changed

packages/orchestration/src/orchestration-types.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import type {
2626
EmbeddingsModelDetails as OriginalEmbeddingsModelDetails,
2727
EmbeddingsModelParams as OriginalEmbeddingsModelParams,
2828
SAPDocumentTranslationInput,
29-
SAPDocumentTranslationOutput,
30-
SAPDocumentTranslationApplyToSelector
29+
SAPDocumentTranslationOutput
3130
} from './client/api/schema/index.js';
3231

3332
/**
@@ -426,11 +425,33 @@ export type LlamaGuard38BCategory = keyof LlamaGuard38B;
426425
*/
427426
export type ConfigType = 'input' | 'output';
428427

428+
/**
429+
* Category for translation application scope.
430+
*/
431+
export type TranslationApplyToCategory = 'placeholders' | 'template_roles';
432+
429433
/**
430434
* Configuration selector for applying translation to specific placeholders or message roles.
431435
*/
432-
export type DocumentTranslationApplyToSelector =
433-
SAPDocumentTranslationApplyToSelector;
436+
export interface DocumentTranslationApplyToSelector {
437+
/**
438+
* Category to apply translation to.
439+
*/
440+
category: TranslationApplyToCategory;
441+
/**
442+
* List of placeholders or roles to apply translation to.
443+
* @example [
444+
* "groundingInput",
445+
* "inputContext"
446+
* ]
447+
*/
448+
items: string[];
449+
/**
450+
* Language of the text to be translated.
451+
* @example "de-DE"
452+
*/
453+
sourceLanguage?: string;
454+
}
434455

435456
/**
436457
* Target language for translation, either a language code or a selector configuration.
@@ -460,7 +481,7 @@ interface TranslationConfigParametersInput {
460481
translateMessagesHistory?: boolean;
461482
/**
462483
* Configuration for applying translation to specific placeholders or message roles.
463-
* @example applyTo: [{ category: 'placeholders', items: ['user_input'], source_language: 'de-DE' }]
484+
* @example applyTo: [{ category: 'placeholders', items: ['user_input'], sourceLanguage: 'de-DE' }]
464485
*/
465486
applyTo?: DocumentTranslationApplyToSelector[];
466487
}

packages/orchestration/src/util/translation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Translation module config', () => {
4949
{
5050
category: 'placeholders',
5151
items: ['user_input'],
52-
source_language: 'de-DE'
52+
sourceLanguage: 'de-DE'
5353
}
5454
]
5555
});
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
import type { SAPDocumentTranslationApplyToSelector } from '../client/api/schema/index.js';
12
import type {
23
TranslationConfigParams,
34
TranslationReturnType,
45
ConfigType,
5-
TranslationInputParameters
6+
TranslationInputParameters,
7+
DocumentTranslationApplyToSelector
68
} from '../orchestration-types.js';
79

10+
function mapSelectorToBaseType(
11+
selector: DocumentTranslationApplyToSelector
12+
): SAPDocumentTranslationApplyToSelector {
13+
return {
14+
category: selector.category,
15+
items: selector.items,
16+
...(selector.sourceLanguage && {
17+
source_language: selector.sourceLanguage
18+
})
19+
};
20+
}
21+
822
/**
923
* Convenience function to build a document translation configuration for orchestration service.
1024
* @param type - Type of the translation configuration, either `input` or `output`.
@@ -20,13 +34,6 @@ export function buildTranslationConfig<T extends ConfigType>(
2034
type: T,
2135
config: TranslationConfigParams<T>
2236
): TranslationReturnType<T> {
23-
const baseConfig = {
24-
...(config.sourceLanguage && {
25-
source_language: config.sourceLanguage
26-
}),
27-
target_language: config.targetLanguage
28-
};
29-
3037
if (type === 'input') {
3138
const inputConfig = config as TranslationInputParameters;
3239
return {
@@ -35,16 +42,27 @@ export function buildTranslationConfig<T extends ConfigType>(
3542
translate_messages_history: inputConfig.translateMessagesHistory
3643
}),
3744
config: {
38-
...baseConfig,
45+
...(inputConfig.sourceLanguage && {
46+
source_language: inputConfig.sourceLanguage
47+
}),
48+
target_language: inputConfig.targetLanguage,
3949
...(inputConfig.applyTo && {
40-
apply_to: inputConfig.applyTo
50+
apply_to: inputConfig.applyTo.map(mapSelectorToBaseType)
4151
})
4252
}
4353
} as TranslationReturnType<T>;
4454
}
4555

4656
return {
4757
type: 'sap_document_translation',
48-
config: baseConfig
58+
config: {
59+
...(config.sourceLanguage && {
60+
source_language: config.sourceLanguage
61+
}),
62+
target_language:
63+
typeof config.targetLanguage === 'string'
64+
? config.targetLanguage
65+
: mapSelectorToBaseType(config.targetLanguage)
66+
}
4967
} as TranslationReturnType<T>;
5068
}

sample-code/src/orchestration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ export async function orchestrationTranslation(): Promise<OrchestrationResponse>
750750
{
751751
category: 'placeholders',
752752
items: ['user_input'],
753-
source_language: 'en-US'
753+
sourceLanguage: 'en-US'
754754
}
755755
]
756756
}),

0 commit comments

Comments
 (0)