Skip to content

Commit 1588f29

Browse files
zhonghao luzhonghao lu
authored andcommitted
lint
1 parent 3a81886 commit 1588f29

6 files changed

Lines changed: 165 additions & 93 deletions

File tree

python/scripts/init_database.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
"""Standalone database initialization script for ValueCell."""
33

44
import sys
5-
import os
65
from pathlib import Path
76

8-
# Add the project root to Python path
9-
project_root = Path(__file__).parent.parent
10-
sys.path.insert(0, str(project_root))
117

12-
# Now import from valuecell
13-
from valuecell.server.db.init_db import main
8+
def setup_path_and_run():
9+
"""Setup Python path and run the database initialization."""
10+
# Add the project root to Python path
11+
project_root = Path(__file__).parent.parent
12+
sys.path.insert(0, str(project_root))
13+
14+
# Import after path setup to avoid import errors
15+
import valuecell.server.db.init_db as init_db_module
16+
17+
# Run the main function
18+
init_db_module.main()
19+
1420

1521
if __name__ == "__main__":
16-
main()
22+
setup_path_and_run()

python/valuecell/server/db/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,33 @@ The Agent model stores information about all available AI agents in the ValueCel
7878
2. **Create database file**: Create new database file if it doesn't exist
7979
3. **Create table structure**: Create agents table based on model definitions
8080
4. **Initialize Agent data**:
81-
- Load all JSON configuration files from `configs/agent_cards/` directory
82-
- Create corresponding Agent records for each configuration file
81+
- Insert default Agent records directly from code
82+
- Create three default agents: AIHedgeFundAgent, Sec13FundAgent, and TradingAgentsAdapter
8383
- Support updating existing Agent configuration information
8484
5. **Verify initialization**: Confirm database connection and table structure are correct
8585

86-
### Agent Configuration Files
86+
### Default Agent Records
8787

88-
The initialization script automatically loads all JSON configuration files from the `configs/agent_cards/` directory:
88+
The initialization script automatically creates default Agent records directly in the code:
8989

90-
**Configuration file example**:
91-
```json
90+
**Default agents created**:
91+
92+
1. **AIHedgeFundAgent**: AI-powered hedge fund analysis and trading agent
93+
2. **Sec13FundAgent**: SEC 13F fund analysis and tracking agent
94+
3. **TradingAgentsAdapter**: Multi-agent trading analysis system with market, sentiment, news and fundamentals analysis
95+
96+
**Agent data structure example**:
97+
```python
9298
{
9399
"name": "TradingAgentsAdapter",
100+
"display_name": "Trading Agents Adapter",
94101
"description": "TradingAgents - Multi-agent trading analysis system",
102+
"version": "1.0.0",
103+
"enabled": True,
104+
"is_active": True,
95105
"capabilities": {
96-
"streaming": true,
97-
"push_notifications": false
106+
"streaming": True,
107+
"push_notifications": False
98108
},
99109
"metadata": {
100110
"version": "1.0.0",
@@ -106,11 +116,6 @@ The initialization script automatically loads all JSON configuration files from
106116
}
107117
```
108118

109-
**Currently supported Agents**:
110-
- `AIHedgeFundAgent`: AI hedge fund agent
111-
- `Sec13FundAgent`: SEC 13F fund analysis agent
112-
- `TradingAgentsAdapter`: Multi-agent trading analysis system
113-
114119
## Usage in Code
115120

116121
### Getting Database Session

python/valuecell/server/db/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
get_db,
77
)
88
from .init_db import DatabaseInitializer, init_database
9-
from .models import *
9+
from .models import Base, Agent
1010

1111
__all__ = [
1212
# Connection management
@@ -16,4 +16,7 @@
1616
# Database initialization
1717
"DatabaseInitializer",
1818
"init_database",
19+
# Models
20+
"Base",
21+
"Agent",
1922
]

python/valuecell/server/db/connection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Database connection and session management for ValueCell Server."""
22

3-
import os
43
from typing import Generator
54
from sqlalchemy import create_engine, Engine
65
from sqlalchemy.orm import sessionmaker, Session

python/valuecell/server/db/init_db.py

Lines changed: 128 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -119,91 +119,151 @@ def create_tables(self) -> bool:
119119
return False
120120

121121
def initialize_basic_data(self) -> bool:
122-
"""Initialize agent data from configuration files."""
122+
"""Initialize default agent data."""
123123
try:
124-
logger.info("Initializing agent data...")
124+
logger.info("Initializing default agent data...")
125125

126126
# Get a database session
127127
session = self.db_manager.get_session()
128128

129129
try:
130130
# Import models here to avoid circular imports
131131
from .models.agent import Agent
132-
import json
133-
import os
134-
from pathlib import Path
135132

136-
# Get the project root directory
137-
project_root = Path(__file__).parent.parent.parent.parent
138-
agent_configs_dir = project_root / "configs" / "agent_cards"
139-
140-
if not agent_configs_dir.exists():
141-
logger.warning(
142-
f"Agent configs directory not found: {agent_configs_dir}"
133+
# Define default agents
134+
default_agents = [
135+
{
136+
"name": "AIHedgeFundAgent",
137+
"display_name": "AI Hedge Fund Agent",
138+
"description": "AI-powered hedge fund analysis and trading agent",
139+
"version": "1.0.0",
140+
"enabled": True,
141+
"is_active": True,
142+
"capabilities": {
143+
"streaming": False,
144+
"push_notifications": False,
145+
},
146+
"metadata": {
147+
"version": "1.0.0",
148+
"author": "ValueCell Team",
149+
"tags": ["hedge-fund", "ai", "trading"],
150+
},
151+
},
152+
{
153+
"name": "Sec13FundAgent",
154+
"display_name": "SEC 13F Fund Agent",
155+
"description": "SEC 13F fund analysis and tracking agent",
156+
"version": "1.0.0",
157+
"enabled": True,
158+
"is_active": True,
159+
"capabilities": {
160+
"streaming": False,
161+
"push_notifications": False,
162+
},
163+
"metadata": {
164+
"version": "1.0.0",
165+
"author": "ValueCell Team",
166+
"tags": ["sec", "13f", "fund-analysis"],
167+
},
168+
},
169+
{
170+
"name": "TradingAgentsAdapter",
171+
"display_name": "Trading Agents Adapter",
172+
"description": "TradingAgents - Multi-agent trading analysis system with market, sentiment, news and fundamentals analysis",
173+
"version": "1.0.0",
174+
"enabled": True,
175+
"is_active": True,
176+
"capabilities": {
177+
"streaming": True,
178+
"push_notifications": False,
179+
},
180+
"metadata": {
181+
"version": "1.0.0",
182+
"author": "ValueCell Team",
183+
"tags": [
184+
"trading",
185+
"analysis",
186+
"multi-agent",
187+
"stocks",
188+
"finance",
189+
],
190+
"supported_tickers": [
191+
"AAPL",
192+
"GOOGL",
193+
"MSFT",
194+
"NVDA",
195+
"TSLA",
196+
"AMZN",
197+
"META",
198+
"NFLX",
199+
"SPY",
200+
],
201+
"supported_analysts": [
202+
"market",
203+
"social",
204+
"news",
205+
"fundamentals",
206+
],
207+
"supported_llm_providers": [
208+
"openai",
209+
"anthropic",
210+
"google",
211+
"ollama",
212+
"openrouter",
213+
],
214+
},
215+
},
216+
]
217+
218+
# Insert default agents
219+
for agent_data in default_agents:
220+
agent_name = agent_data["name"]
221+
222+
# Check if agent already exists
223+
existing_agent = (
224+
session.query(Agent).filter_by(name=agent_name).first()
143225
)
144-
return True
145-
146-
# Load agent configurations from JSON files
147-
for config_file in agent_configs_dir.glob("*.json"):
148-
try:
149-
with open(config_file, "r", encoding="utf-8") as f:
150-
config_data = json.load(f)
151-
152-
agent_name = config_data.get("name")
153-
if not agent_name:
154-
logger.warning(
155-
f"Agent config missing 'name' field: {config_file}"
156-
)
157-
continue
158-
159-
# Check if agent already exists
160-
existing_agent = (
161-
session.query(Agent).filter_by(name=agent_name).first()
226+
227+
if not existing_agent:
228+
# Create new agent
229+
agent = Agent.from_config(agent_data)
230+
session.add(agent)
231+
logger.info(f"Added default agent: {agent_name}")
232+
else:
233+
# Update existing agent with default data
234+
existing_agent.display_name = agent_data.get(
235+
"display_name", existing_agent.display_name
236+
)
237+
existing_agent.description = agent_data.get(
238+
"description", existing_agent.description
239+
)
240+
existing_agent.version = agent_data.get(
241+
"version", existing_agent.version
242+
)
243+
existing_agent.enabled = agent_data.get(
244+
"enabled", existing_agent.enabled
245+
)
246+
existing_agent.is_active = agent_data.get(
247+
"is_active", existing_agent.is_active
248+
)
249+
existing_agent.capabilities = agent_data.get(
250+
"capabilities", existing_agent.capabilities
251+
)
252+
existing_agent.agent_metadata = agent_data.get(
253+
"metadata", existing_agent.agent_metadata
254+
)
255+
existing_agent.config = agent_data.get(
256+
"config", existing_agent.config
162257
)
163-
if not existing_agent:
164-
# Create new agent from config
165-
agent = Agent.from_config(config_data)
166-
session.add(agent)
167-
logger.info(f"Added agent: {agent_name}")
168-
else:
169-
# Update existing agent with new config data
170-
existing_agent.display_name = config_data.get(
171-
"display_name", existing_agent.display_name
172-
)
173-
existing_agent.description = config_data.get(
174-
"description", existing_agent.description
175-
)
176-
existing_agent.url = config_data.get(
177-
"url", existing_agent.url
178-
)
179-
existing_agent.version = config_data.get(
180-
"version", existing_agent.version
181-
)
182-
existing_agent.enabled = config_data.get(
183-
"enabled", existing_agent.enabled
184-
)
185-
existing_agent.capabilities = config_data.get(
186-
"capabilities", existing_agent.capabilities
187-
)
188-
existing_agent.agent_metadata = config_data.get(
189-
"metadata", existing_agent.agent_metadata
190-
)
191-
existing_agent.config = config_data.get(
192-
"config", existing_agent.config
193-
)
194-
logger.info(f"Updated agent: {agent_name}")
195-
196-
except Exception as e:
197-
logger.error(f"Error loading agent config {config_file}: {e}")
198-
continue
258+
logger.info(f"Updated default agent: {agent_name}")
199259

200260
session.commit()
201-
logger.info("Agent data initialization completed")
261+
logger.info("Default agent data initialization completed")
202262
return True
203263

204264
except Exception as e:
205265
session.rollback()
206-
logger.error(f"Error initializing agent data: {e}")
266+
logger.error(f"Error initializing default agent data: {e}")
207267
return False
208268
finally:
209269
session.close()

python/valuecell/server/db/models/agent.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
This module defines the database models for agents in the ValueCell system.
55
"""
66

7-
from datetime import datetime
8-
from typing import Dict, List, Optional, Any
7+
from typing import Dict, Any
98
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, JSON
109
from sqlalchemy.sql import func
1110

@@ -122,4 +121,4 @@ def from_config(cls, config_data: Dict[str, Any]) -> "Agent":
122121
capabilities=config_data.get("capabilities"),
123122
agent_metadata=config_data.get("metadata"),
124123
config=config_data.get("config"),
125-
)
124+
)

0 commit comments

Comments
 (0)