| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | from enum import Enum |
| 8 | from typing import Literal, Optional, Union |
| 9 | |
| 10 | from pydantic import BaseModel, Field |
| 11 | from typing_extensions import Annotated |
| 12 | |
| 13 | from llama_stack.distribution.utils.config_dirs import RUNTIME_BASE_DIR |
| 14 | |
| 15 | |
| 16 | class KVStoreType(Enum): |
| 17 | redis = "redis" |
| 18 | sqlite = "sqlite" |
| 19 | postgres = "postgres" |
| 20 | |
| 21 | |
| 22 | class CommonConfig(BaseModel): |
| 23 | namespace: Optional[str] = Field( |
| 24 | default=None, |
| 25 | description="All keys will be prefixed with this namespace", |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | class RedisKVStoreConfig(CommonConfig): |
| 30 | type: Literal[KVStoreType.redis.value] = KVStoreType.redis.value |
| 31 | host: str = "localhost" |
| 32 | port: int = 6379 |
| 33 | |
| 34 | |
| 35 | class SqliteKVStoreConfig(CommonConfig): |
| 36 | type: Literal[KVStoreType.sqlite.value] = KVStoreType.sqlite.value |
| 37 | db_path: str = Field( |
| 38 | default=(RUNTIME_BASE_DIR / "kvstore.db").as_posix(), |
| 39 | description="File path for the sqlite database", |
| 40 | ) |
| 41 | |
| 42 | |
| 43 | class PostgresKVStoreConfig(CommonConfig): |
| 44 | type: Literal[KVStoreType.postgres.value] = KVStoreType.postgres.value |
| 45 | host: str = "localhost" |
| 46 | port: int = 5432 |
| 47 | db: str = "llamastack" |
| 48 | user: str |
| 49 | password: Optional[str] = None |
| 50 | |
| 51 | |
| 52 | KVStoreConfig = Annotated[ |
| 53 | Union[RedisKVStoreConfig, SqliteKVStoreConfig, PostgresKVStoreConfig], |
| 54 | Field(discriminator="type", default=KVStoreType.sqlite.value), |
| 55 | ] |