Files
LLM-Robot-Control/llm/credentials.py
T

44 lines
1.1 KiB
Python

import json
from dataclasses import asdict
from pathlib import Path
from pydantic_ai.providers.openai_codex import (
OpenAICodexCredentials,
OpenAICodexCredentialSource,
)
class FileCodexCredentionalSource(OpenAICodexCredentialSource):
def __init__(self,
path: Path,
encoding: str="utf-8",
ensure_ascii: bool=False,
indent: int=2):
super().__init__()
self._path = path
self._encoding=encoding
self._ensure_ascii=ensure_ascii
self._indent=indent
@property
def path(self):
return self._path
async def load(self) -> OpenAICodexCredentials:
return OpenAICodexCredentials(
**json.loads(
self._path.read_text(encoding=self._encoding)
)
)
async def save(self, credentials: OpenAICodexCredentials) -> None:
self._path.write_text(
json.dumps(
asdict(credentials),
ensure_ascii=self._ensure_ascii,
indent=self._indent
),
encoding=self._encoding
)