Repository: microsoft/TypeChat
File: examples/healthData/src/translator.ts (TypeScript)

Code before:
import {Result, TypeChatLanguageModel, createJsonTranslator, TypeChatJsonTranslator} from "typechat";

type ChatMessage = {
    source: "system" | "user" | "assistant";
    body: string;
};

export interface TranslatorWithHistory<T extends object> {
    _chat_history: ChatMessage[];
    _max_prompt_length: number;
    _additional_agent_instructions: string;
    translator: TypeChatJsonTranslator<T>;
    translate(request: string): Promise<Result<T>>;
}

export function createHealthDataTranslator<T extends object>(model: TypeChatLanguageModel, schema: string, type_name: string, additional_agent_instructions: string): TranslatorWithHistory<T> {
    const _chat_history: ChatMessage[] = [];
    const _max_prompt_length = 2048;
    const _additional_agent_instructions = additional_agent_instructions;
    
    const translator = createJsonTranslator<T>(model, schema, type_name);
    translator.createRequestPrompt = _create_request_prompt;
    
    const customtranslator: TranslatorWithHistory<T> = {
        _chat_history,
        _max_prompt_length,
        _additional_agent_instructions,
        translator,
        translate,
    };

    return customtranslator;

    async function translate(request: string): Promise<Result<T>> {
        const response = await translator.translate(request);
        if (response.success) {
            _chat_history.push({ source: 'assistant', body: JSON.stringify(response.data) });
        }
        return response;
    }


Review comment (suggestion):
```suggestion
export function createHealthDataTranslator<T extends object>(model: TypeChatLanguageModel, schema: string, typeName: string, additionalAgentInstructions: string): TranslatorWithHistory<T> {
```

Code after:
import {Result, TypeChatLanguageModel, createJsonTranslator, TypeChatJsonTranslator} from "typechat";

type ChatMessage = {
    source: "system" | "user" | "assistant";
    body: object;
};

export interface TranslatorWithHistory<T extends object> {
    _chatHistory: ChatMessage[];
    _maxPromptLength: number;
    _additionalAgentInstructions: string;
    _translator: TypeChatJsonTranslator<T>;
    translate(request: string): Promise<Result<T>>;
}

export function createHealthDataTranslator<T extends object>(model: TypeChatLanguageModel, schema: string, typename: string, additionalAgentInstructions: string): TranslatorWithHistory<T> {
    const _chatHistory: ChatMessage[] = [];
    const _maxPromptLength = 2048;
    const _additionalAgentInstructions = additionalAgentInstructions;
    
    const _translator = createJsonTranslator<T>(model, schema, typename);
    _translator.createRequestPrompt = createRequestPrompt;
    
    const customtranslator: TranslatorWithHistory<T> = {
        _chatHistory,
        _maxPromptLength,
        _additionalAgentInstructions,
        _translator,
        translate,
    };

    return customtranslator;

    async function translate(request: string): Promise<Result<T>> {
        const response = await _translator.translate(request);
        if (response.success) {
            _chatHistory.push({ source: "assistant", body: response.data });
        }
        return response;

    }