A client SDK for the Ticos Agent system with HTTP and WebSocket support
Project description
Ticos Client Python SDK
A Python SDK for the Ticos Agent system. This SDK allows you to create applications that can receive messages, handle motion and emotion commands, and store messages and memories locally.
Features
- Local message and memory storage using SQLite
- Asynchronous message handling with thread-safe operations
- Type hints for better development experience
- Extensible architecture with support for custom handlers
- Built-in support for conversation handling and function calls
- Memory generation and management
- Support for different storage modes (INTERNAL, EXTERNAL)
Installation
pip install ticos-client==0.5.0
Depends on ticos-agent 0.10.0 and above.
Configuration
The SDK uses two configuration files:
-
config.toml- Main configuration file (TOML format)- Location:
~/.config/ticos/config.toml - Contains device related settings and defaults
- Location:
-
session_config- Session-specific agent configuration (JSON format)- Location (INTERNAL mode):
~/.config/ticos/session_config - Location (EXTERNAL mode):
/path/to/tf_card/.config/ticos/session_config - Contains session-specific settings and state
- Location (INTERNAL mode):
Quick Start
Basic Usage
import logging
import time
from datetime import datetime
from ticos_client import TicosClient, SaveMode
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def message_handler(message):
"""Handle incoming messages"""
logger.info(f"Received message: {message}")
def motion_handler(parameters):
"""Handle motion commands"""
logger.info(f"Motion command: {parameters}")
def emotion_handler(parameters):
"""Handle emotion commands"""
logger.info(f"Emotion command: {parameters}")
def function_call_handler(name, parameters):
"""Handle function calls"""
logger.info(f"Function call '{name}': {parameters}")
def conversation_handler(message_id, role, content):
"""Handle conversation events"""
logger.info(f"Conversation - ID: {message_id}, Role: {role}, Content: {content}")
def main():
# Create and configure client
# Available save modes: SaveMode.INTERNAL, SaveMode.EXTERNAL
client = TicosClient(port=9999, save_mode=SaveMode.INTERNAL)
# Enable local storage (SQLite by default)
client.enable_local_storage()
# Set up handlers
client.set_message_handler(message_handler)
client.set_motion_handler(motion_handler)
client.set_emotion_handler(emotion_handler)
client.set_function_call_handler(function_call_handler)
client.set_conversation_handler(conversation_handler)
# Start the client
if not client.start():
logger.error("Failed to start client")
return
try:
logger.info("Client started. Press Ctrl+C to stop.")
# Keep the client running
while True:
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logger.info(f"Client running at {current_time}")
time.sleep(30)
except KeyboardInterrupt:
logger.info("Stopping client...")
finally:
client.stop()
if __name__ == "__main__":
main()
API Reference
TicosClient
Constructor
client = TicosClient(
port: int = 9999,
save_mode: SaveMode = SaveMode.INTERNAL,
tf_root_dir: Optional[str] = None
)
Creates a new Ticos client instance.
Parameters:
port: Port number to run the server on (default: 9999)save_mode: Storage mode (SaveMode.INTERNAL or SaveMode.EXTERNAL). SaveMode.INTERNAL will save the messages under the user's home directory. SaveMode.EXTERNAL will save the messages under the TF card root directory.tf_root_dir: Root directory for external storage (ignored if save_mode is EXTERNAL). If not provided, will use the root path of the TF card on the device in linux environment.
Methods
-
enable_local_storage(storage: Optional[StorageService] = None) -> None- Enable local storage with an optional custom storage service
- If no storage service is provided, a default SQLiteStorageService will be used
- If don't need to save messages locally, can ignore this method.
-
start() -> bool- Start the HTTP and WebSocket server
- Returns True if startup was successful
-
stop() -> None- Stop the server and clean up resources
-
is_running() -> bool- Check if the server is running
- Returns True if the server is running
-
set_message_handler(handler: Callable[[dict], None]) -> None- Set handler for general messages
handler: Function that takes a message dictionary as parameter
-
set_motion_handler(handler: Callable[[dict], None]) -> None- Set handler for motion commands
handler: Function that takes a parameters dictionary as parameter
-
set_emotion_handler(handler: Callable[[dict], None]) -> None- Set handler for emotion commands
handler: Function that takes a parameters dictionary as parameter
-
set_function_call_handler(handler: Callable[[str, dict], None]) -> None- Set handler for function calls
handler: Function that takes function name and parameters as parameters
-
set_conversation_handler(handler: Callable[[str, str, str], None]) -> None- Set handler for conversation events
handler: Function that takes message_id, role, and content as parameters
Message Formats
Standard Message Format
Message Types
1. Motion Message
Triggers a physical motion or animation.
{
"name": "motion",
"arguments": {
"motion_tag": "string", // The motion ID (required)
"speed": 1.0,
"repeat": 1,
"wait": true
}
}
2. Emotion Message
Triggers an emotional expression or state change.
{
"name": "emotion",
"arguments": {
"emotion_tag": "string", // The emotion ID (required)
"intensity": 1.0,
"duration": 2.5,
"fade_in": 0.5
}
}
3. Conversation Message
Represents a chat message in a conversation. message_id, role, content
{
"message_id": "msg_abc", // The message ID
"role": "user", // "user" or "assistant"
"content": "Hello!" // The message content
}
4. Function Call Message
Represents a function call to be executed.
{
"name": "function_name", // Name of the function to call
"parameters": { // Parameters for the function
"para_name1": "para_value1",
"para_name2": "para_value2",
...
}
}
5. Raw Message
The raw message received from the Ticos Agent, it will be passed to the set_message_handler callback, which is generally defined in realtime protocol.
{
"content_index": 0,
"event_id": "evt_NwZGB9s8xjxdoKDqSKmquw",
"item_id": "resp_item_QhyuAfFaLaSkc3aJrCUsDa",
"output_index": 0,
"part": {
"audio": null,
"transcript": "",
"type": "audio"
},
"response_id": "evt_khr4a6SUJ6ugVPBp9iiBna",
"type": "response.content_part.done"
}
For details about the messages (events) in the realtime protocol, please refer to Realtime Server Events.
Advanced Features
Storage Modes
The SDK supports different storage modes for flexibility:
-
INTERNAL (Default):
- Uses SQLite for local storage
- Simple to set up and use
- Good for single-instance applications
-
EXTERNAL:
- Uses an external storage service
- Supports custom paths when
tf_root_diris specified
Data Structure
The storage system maintains two main types of data:
-
Messages: Chat history.
-
Memories: The generated long-term memory.
In INTERNAL mode, data is stored in an SQLite database located at ~/.config/ticos/ticos.db. In EXTERNAL mode, the storage location depends on the tf_root_dir configuration.
Error Handling
- All network operations include error handling and retry logic
- Callbacks include error parameters for custom error handling
- Logging is configurable via Python's standard logging module
Thread Safety
- All public methods are thread-safe
- Internal locking mechanisms prevent race conditions
- Asynchronous operations use proper synchronization
Performance Considerations
- Batch operations where possible
- Efficient memory usage with lazy loading
Best Practices
- Error Handling: Always implement error handlers for network operations
- Logging: Use the built-in logging for debugging and monitoring
- Resource Management: Use context managers or
try/finallyblocks to ensure proper cleanup - Message Validation: Validate all incoming and outgoing messages
- Concurrency: Be mindful of thread safety when implementing custom handlers
Development
Prerequisites
- Python 3.8+
- pip 20.0.0+
- Git
Setup
-
Clone the repository:
git clone https://github.com/tiwater/ticos-client.git cd ticos-client/sdk/python
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install development dependencies:
pip install -r requirements-dev.txt
Or using Tsinghua mirror (for users in China):
pip install -r requirements-dev.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Testing
Run the test suite:
# Run all tests
pytest tests/ -v
# Run a specific test file
pytest tests/test_ticos_client.py -v
# Run tests with coverage report
pytest --cov=ticos_client tests/
Building
Build the package:
python -m build
Code Style
The project uses black for code formatting and flake8 for linting:
# Format code
black .
# Check for style issues
flake8
Examples
Check out the examples/python directory for complete working examples, including:
- Basic client setup and usage
- Custom message handlers
Contributing
Contributions are welcome! Please read our Contributing Guidelines for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support, please open an issue on our GitHub repository or contact our support team.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ticos_client-0.6.2.tar.gz.
File metadata
- Download URL: ticos_client-0.6.2.tar.gz
- Upload date:
- Size: 31.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
967ad3fa23f3face0fa00ec24999e92235a70124c8ac01a04ca8c04ff2462aa1
|
|
| MD5 |
be4dec443958aa797d0aa615b7487f18
|
|
| BLAKE2b-256 |
abab7cb60ef7cbf85902ce00efb80e9051780361953e9b0502977f9bfbc40748
|
File details
Details for the file ticos_client-0.6.2-py3-none-any.whl.
File metadata
- Download URL: ticos_client-0.6.2-py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8df76558d5815438cf0918ec528392d6fde5ad440625d73b5bf27fbf8d3222a
|
|
| MD5 |
3cbc91a5107d4d0b13d518c12e3b97bd
|
|
| BLAKE2b-256 |
b89c23853fef0926e229166c9bc603adfe03737a3f7f4d4e115e2c987b84ebc9
|