Commit d837f6f7 authored by Your Name's avatar Your Name

Fix model naming conventions: remove user- prefix from rotation/autoselect...

Fix model naming conventions: remove user- prefix from rotation/autoselect model IDs, fetch live provider models
parent a139a7d0
...@@ -2335,12 +2335,11 @@ class RotationHandler: ...@@ -2335,12 +2335,11 @@ class RotationHandler:
# Check for user-specific rotation config first # Check for user-specific rotation config first
if self.user_id: if self.user_id:
# Database user: ONLY use user-specific configs - NO global fallback # Database user: ONLY use user-specific configs - NO global fallback
rotation_config = next((rot['config'] for rot in self.user_rotations if rot['rotation_id'] == rotation_id), None) if rotation_id not in self.rotations:
if rotation_config:
logger.info(f"Using user-specific rotation config for {rotation_id}")
else:
logger.error(f"User rotation {rotation_id} not found - NO global fallback") logger.error(f"User rotation {rotation_id} not found - NO global fallback")
raise HTTPException(status_code=400, detail=f"Rotation {rotation_id} not found for this user") raise HTTPException(status_code=400, detail=f"Rotation {rotation_id} not found for this user")
rotation_config = self.rotations[rotation_id]
logger.info(f"Using user-specific rotation config for {rotation_id}")
else: else:
# Admin user: use global config # Admin user: use global config
rotation_config = self.config.get_rotation(rotation_id) rotation_config = self.config.get_rotation(rotation_id)
...@@ -3866,8 +3865,8 @@ class AutoselectHandler: ...@@ -3866,8 +3865,8 @@ class AutoselectHandler:
self._load_user_configs() self._load_user_configs()
# Override config to only use user-specific configs with NO global fallback # Override config to only use user-specific configs with NO global fallback
self.autoselects = {} self.autoselects = {}
for autoselect in self.user_autoselects: for autoselect_id, autoselect_config in self.user_autoselects.items():
self.autoselects[autoselect['autoselect_id']] = autoselect['config'] self.autoselects[autoselect_id] = autoselect_config
else: else:
self.user_providers = {} self.user_providers = {}
self.user_rotations = {} self.user_rotations = {}
...@@ -3888,8 +3887,8 @@ class AutoselectHandler: ...@@ -3888,8 +3887,8 @@ class AutoselectHandler:
self._load_user_configs() self._load_user_configs()
# Refresh autoselects dict after reload # Refresh autoselects dict after reload
self.autoselects = {} self.autoselects = {}
for autoselect in self.user_autoselects: for autoselect_id, autoselect_config in self.user_autoselects.items():
self.autoselects[autoselect['autoselect_id']] = autoselect['config'] self.autoselects[autoselect_id] = autoselect_config
def _get_skill_file_content(self) -> str: def _get_skill_file_content(self) -> str:
"""Load the autoselect.md skill file content""" """Load the autoselect.md skill file content"""
......
...@@ -10487,11 +10487,11 @@ async def user_list_models_by_username(request: Request, username: str): ...@@ -10487,11 +10487,11 @@ async def user_list_models_by_username(request: Request, username: str):
for rotation_id, rotation_config in rotation_handler.rotations.items(): for rotation_id, rotation_config in rotation_handler.rotations.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-rotation/{rotation_id}", 'id': f"rotation/{rotation_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-rotation', 'owned_by': 'aisbf-rotation',
'type': 'user_rotation', 'type': 'rotation',
'rotation_id': rotation_id, 'rotation_id': rotation_id,
'source': 'user_config' 'source': 'user_config'
}) })
...@@ -10502,11 +10502,11 @@ async def user_list_models_by_username(request: Request, username: str): ...@@ -10502,11 +10502,11 @@ async def user_list_models_by_username(request: Request, username: str):
for autoselect_id, autoselect_config in autoselect_handler.autoselects.items(): for autoselect_id, autoselect_config in autoselect_handler.autoselects.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-autoselect/{autoselect_id}", 'id': f"autoselect/{autoselect_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-autoselect', 'owned_by': 'aisbf-autoselect',
'type': 'user_autoselect', 'type': 'autoselect',
'autoselect_id': autoselect_id, 'autoselect_id': autoselect_id,
'source': 'user_config' 'source': 'user_config'
}) })
...@@ -10521,25 +10521,31 @@ async def user_list_models_by_username(request: Request, username: str): ...@@ -10521,25 +10521,31 @@ async def user_list_models_by_username(request: Request, username: str):
# Get user-specific handler for providers # Get user-specific handler for providers
handler = get_user_handler('request', target_user_id) handler = get_user_handler('request', target_user_id)
# Add user providers # Add user providers - fetch live models from each provider API
for provider_id, provider_config in handler.user_providers.items(): for provider_id, provider_config in handler.user_providers.items():
try: try:
if hasattr(provider_config, 'models') and provider_config.models: # Get provider handler for this user provider
for model in provider_config.models: provider_handler = get_provider_handler(provider_id, user_id=target_user_id)
model_id = f"{provider_id}/{model.name}"
all_models.append({ # Fetch live models from provider
'id': model_id, provider_models = await provider_handler.get_models()
'object': 'model',
'created': int(time.time()), # Add all models from this provider
'owned_by': provider_id, for model in provider_models:
'provider': provider_id, model_id = f"{provider_id}/{model.id}"
'type': 'user_provider', all_models.append({
'model_name': model.name, 'id': model_id,
'context_size': getattr(model, 'context_size', None), 'object': 'model',
'capabilities': getattr(model, 'capabilities', []), 'created': int(time.time()),
'description': getattr(model, 'description', None), 'owned_by': provider_id,
'source': 'user_config' 'provider': provider_id,
}) 'type': 'user_provider',
'model_name': model.name,
'context_size': getattr(model, 'context_size', None),
'capabilities': getattr(model, 'capabilities', []),
'description': getattr(model, 'description', None),
'source': 'user_config'
})
except Exception as e: except Exception as e:
logger.warning(f"Error listing models for user provider {provider_id}: {e}") logger.warning(f"Error listing models for user provider {provider_id}: {e}")
...@@ -10548,11 +10554,11 @@ async def user_list_models_by_username(request: Request, username: str): ...@@ -10548,11 +10554,11 @@ async def user_list_models_by_username(request: Request, username: str):
for rotation_id, rotation_config in rotation_handler.rotations.items(): for rotation_id, rotation_config in rotation_handler.rotations.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-rotation/{rotation_id}", 'id': f"rotation/{rotation_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-rotation', 'owned_by': 'aisbf-rotation',
'type': 'user_rotation', 'type': 'rotation',
'rotation_id': rotation_id, 'rotation_id': rotation_id,
'model_name': rotation_config.get('model_name', rotation_id), 'model_name': rotation_config.get('model_name', rotation_id),
'capabilities': rotation_config.get('capabilities', []), 'capabilities': rotation_config.get('capabilities', []),
...@@ -10566,11 +10572,11 @@ async def user_list_models_by_username(request: Request, username: str): ...@@ -10566,11 +10572,11 @@ async def user_list_models_by_username(request: Request, username: str):
for autoselect_id, autoselect_config in autoselect_handler.autoselects.items(): for autoselect_id, autoselect_config in autoselect_handler.autoselects.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-autoselect/{autoselect_id}", 'id': f"autoselect/{autoselect_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-autoselect', 'owned_by': 'aisbf-autoselect',
'type': 'user_autoselect', 'type': 'autoselect',
'autoselect_id': autoselect_id, 'autoselect_id': autoselect_id,
'model_name': autoselect_config.get('model_name', autoselect_id), 'model_name': autoselect_config.get('model_name', autoselect_id),
'description': autoselect_config.get('description'), 'description': autoselect_config.get('description'),
...@@ -10686,11 +10692,11 @@ async def user_list_models(request: Request, username: str): ...@@ -10686,11 +10692,11 @@ async def user_list_models(request: Request, username: str):
for rotation_id, rotation_config in rotation_handler.rotations.items(): for rotation_id, rotation_config in rotation_handler.rotations.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-rotation/{rotation_id}", 'id': f"rotation/{rotation_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-rotation', 'owned_by': 'aisbf-rotation',
'type': 'user_rotation', 'type': 'rotation',
'rotation_id': rotation_id, 'rotation_id': rotation_id,
'source': 'user_config' 'source': 'user_config'
}) })
...@@ -10701,11 +10707,11 @@ async def user_list_models(request: Request, username: str): ...@@ -10701,11 +10707,11 @@ async def user_list_models(request: Request, username: str):
for autoselect_id, autoselect_config in autoselect_handler.autoselects.items(): for autoselect_id, autoselect_config in autoselect_handler.autoselects.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-autoselect/{autoselect_id}", 'id': f"autoselect/{autoselect_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-autoselect', 'owned_by': 'aisbf-autoselect',
'type': 'user_autoselect', 'type': 'autoselect',
'autoselect_id': autoselect_id, 'autoselect_id': autoselect_id,
'source': 'user_config' 'source': 'user_config'
}) })
...@@ -10753,11 +10759,11 @@ async def user_list_models(request: Request, username: str): ...@@ -10753,11 +10759,11 @@ async def user_list_models(request: Request, username: str):
for rotation_id, rotation_config in rotation_handler.rotations.items(): for rotation_id, rotation_config in rotation_handler.rotations.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-rotation/{rotation_id}", 'id': f"rotation/{rotation_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-rotation', 'owned_by': 'aisbf-rotation',
'type': 'user_rotation', 'type': 'rotation',
'rotation_id': rotation_id, 'rotation_id': rotation_id,
'model_name': rotation_config.get('model_name', rotation_id), 'model_name': rotation_config.get('model_name', rotation_id),
'capabilities': rotation_config.get('capabilities', []), 'capabilities': rotation_config.get('capabilities', []),
...@@ -10771,11 +10777,11 @@ async def user_list_models(request: Request, username: str): ...@@ -10771,11 +10777,11 @@ async def user_list_models(request: Request, username: str):
for autoselect_id, autoselect_config in autoselect_handler.autoselects.items(): for autoselect_id, autoselect_config in autoselect_handler.autoselects.items():
try: try:
all_models.append({ all_models.append({
'id': f"user-autoselect/{autoselect_id}", 'id': f"autoselect/{autoselect_id}",
'object': 'model', 'object': 'model',
'created': int(time.time()), 'created': int(time.time()),
'owned_by': 'aisbf-user-autoselect', 'owned_by': 'aisbf-autoselect',
'type': 'user_autoselect', 'type': 'autoselect',
'autoselect_id': autoselect_id, 'autoselect_id': autoselect_id,
'model_name': autoselect_config.get('model_name', autoselect_id), 'model_name': autoselect_config.get('model_name', autoselect_id),
'description': autoselect_config.get('description'), 'description': autoselect_config.get('description'),
...@@ -11376,13 +11382,13 @@ async def user_chat_completions_by_username(request: Request, username: str, bod ...@@ -11376,13 +11382,13 @@ async def user_chat_completions_by_username(request: Request, username: str, bod
- 'rotation/name' - global rotation (admin only) - 'rotation/name' - global rotation (admin only)
- 'autoselect/name' - global autoselect (admin only) - 'autoselect/name' - global autoselect (admin only)
- 'user-provider/model' - user's provider - 'user-provider/model' - user's provider
- 'user-rotation/name' - user's rotation - 'rotation/name' - user's rotation
- 'user-autoselect/name' - user's autoselect - 'user-autoselect/name' - user's autoselect
Example: Example:
curl -X POST -H "Authorization: Bearer YOUR_TOKEN" \ curl -X POST -H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{"model": "user-rotation/myrotation", "messages": [{"role": "user", "content": "Hello"}]}' \ -d '{"model": "rotation/myrotation", "messages": [{"role": "user", "content": "Hello"}]}' \
http://localhost:17765/api/u/{username}/chat/completions http://localhost:17765/api/u/{username}/chat/completions
""" """
from aisbf.database import get_database from aisbf.database import get_database
...@@ -11411,7 +11417,7 @@ async def user_chat_completions_by_username(request: Request, username: str, bod ...@@ -11411,7 +11417,7 @@ async def user_chat_completions_by_username(request: Request, username: str, bod
if not provider_id: if not provider_id:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Model must be in format 'provider/model', 'rotation/name', 'autoselect/name', 'rotations/name', 'autoselections/name', 'user-provider/model', 'user-rotation/name', or 'user-autoselect/name'" detail="Model must be in format 'provider/model', 'rotation/name', 'autoselect/name', 'rotations/name', 'autoselections/name', or 'user-provider/model'"
) )
body_dict = body.model_dump() body_dict = body.model_dump()
...@@ -11545,7 +11551,7 @@ async def user_chat_completions_by_username(request: Request, username: str, bod ...@@ -11545,7 +11551,7 @@ async def user_chat_completions_by_username(request: Request, username: str, bod
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Model must be in format 'user-provider/model', 'user-rotation/name', or 'user-autoselect/name'. Global configurations are only available to admin users." detail="Model must be in format 'provider/model', 'rotation/name', or 'autoselect/name'. Global configurations are only available to admin users."
) )
...@@ -11706,7 +11712,7 @@ async def user_list_config_models_by_username(request: Request, username: str, c ...@@ -11706,7 +11712,7 @@ async def user_list_config_models_by_username(request: Request, username: str, c
for provider in providers: for provider in providers:
for model in provider.get('models', []): for model in provider.get('models', []):
all_models.append({ all_models.append({
"id": f"user-rotation/{rotation_id}/{model.get('name', '')}", "id": f"rotation/{rotation_id}/{model.get('name', '')}",
"name": rotation_id, "name": rotation_id,
"object": "model", "object": "model",
"created": int(time.time()), "created": int(time.time()),
...@@ -11765,13 +11771,13 @@ async def user_chat_completions(request: Request, username: str, body: ChatCompl ...@@ -11765,13 +11771,13 @@ async def user_chat_completions(request: Request, username: str, body: ChatCompl
- 'rotation/name' - global rotation (admin only) - 'rotation/name' - global rotation (admin only)
- 'autoselect/name' - global autoselect (admin only) - 'autoselect/name' - global autoselect (admin only)
- 'user-provider/model' - user's provider - 'user-provider/model' - user's provider
- 'user-rotation/name' - user's rotation - 'rotation/name' - user's rotation
- 'user-autoselect/name' - user's autoselect - 'user-autoselect/name' - user's autoselect
Example: Example:
curl -X POST -H "Authorization: Bearer YOUR_TOKEN" \ curl -X POST -H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{"model": "user-rotation/myrotation", "messages": [{"role": "user", "content": "Hello"}]}' \ -d '{"model": "rotation/myrotation", "messages": [{"role": "user", "content": "Hello"}]}' \
http://localhost:17765/api/user/chat/completions http://localhost:17765/api/user/chat/completions
""" """
user_id = getattr(request.state, 'user_id', None) user_id = getattr(request.state, 'user_id', None)
...@@ -11904,7 +11910,7 @@ async def user_chat_completions(request: Request, username: str, body: ChatCompl ...@@ -11904,7 +11910,7 @@ async def user_chat_completions(request: Request, username: str, body: ChatCompl
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,
detail="Model must be in format 'user-provider/model', 'user-rotation/name', or 'user-autoselect/name'. Global configurations are only available to admin users." detail="Model must be in format 'provider/model', 'rotation/name', or 'autoselect/name'. Global configurations are only available to admin users."
) )
...@@ -11972,7 +11978,7 @@ async def user_list_config_models(request: Request, username: str, config_type: ...@@ -11972,7 +11978,7 @@ async def user_list_config_models(request: Request, username: str, config_type:
for provider in providers: for provider in providers:
for model in provider.get('models', []): for model in provider.get('models', []):
all_models.append({ all_models.append({
"id": f"user-rotation/{rotation_id}/{model.get('name', '')}", "id": f"rotation/{rotation_id}/{model.get('name', '')}",
"name": rotation_id, "name": rotation_id,
"object": "model", "object": "model",
"created": int(time.time()), "created": int(time.time()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment