Repository: microsoft/TypeChat
File: python/examples/multiSchema/agents.py (Python)

Code before:
                return args[0]
            case _:
                raise ValueError(f'Unexpected function name {func}')

    async def handle_request(self, line: str):
        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            math_result = await evaluate_json_program(result, self._handle_json_program_call)
            print(f"Math Result: {math_result}")


class MusicAgent:
    _validator: TypeChatValidator[music_schema.PlayerActions]
    _translator: TypeChatTranslator[music_schema.PlayerActions]
    _client_context: ClientContext
    _authentication_vals: dict[str,str | None]

    def __init__(self, model: TypeChatModel, authentication_vals: dict[str,str | None]):
        super().__init__()
        self._validator = TypeChatValidator(music_schema.PlayerActions)
        self._translator = TypeChatTranslator(model, self._validator, music_schema.PlayerActions)
        self._authentication_vals = authentication_vals
    
    async def authenticate(self):
        self._client_context = await get_client_context(self._authentication_vals)

    async def handle_request(self, line: str):
        if not self._client_context:
            await self.authenticate()

        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            try:
                for action in result["actions"]:
                    await handle_call(action, self._client_context)

Review comment (security):
```suggestion
    def __init__(self, model: TypeChatModel, authentication_vals: dict[str, str | None]):
```

Code after:
                return args[0]
            case _:
                raise ValueError(f'Unexpected function name {func}')

    async def handle_request(self, line: str):
        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            math_result = await evaluate_json_program(result, self._handle_json_program_call)
            print(f"Math Result: {math_result}")


class MusicAgent:
    _validator: TypeChatValidator[music_schema.PlayerActions]
    _translator: TypeChatTranslator[music_schema.PlayerActions]
    _client_context: ClientContext
    _authentication_vals: dict[str, str | None]

    def __init__(self, model: TypeChatModel, authentication_vals: dict[str, str | None]):
        super().__init__()
        self._validator = TypeChatValidator(music_schema.PlayerActions)
        self._translator = TypeChatTranslator(model, self._validator, music_schema.PlayerActions)
        self._authentication_vals = authentication_vals

    async def authenticate(self):
        self._client_context = await get_client_context(self._authentication_vals)

    async def handle_request(self, line: str):
        if not self._client_context:
            await self.authenticate()

        result = await self._translator.translate(line)
        if isinstance(result, Failure):
            print("Translation Failed ❌")
            print(f"Context: {result.message}")
        else:
            result = result.value
            print("Translation Succeeded! ✅\n")
            print("JSON View")
            print(json.dumps(result, indent=2))

            try:
                for action in result["actions"]:
                    await handle_call(action, self._client_context)