mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-04-27 17:36:25 +02:00
copilot: added edit instructions api
This commit is contained in:
parent
5da21d3c39
commit
ee20f7c6e3
2 changed files with 116 additions and 28 deletions
|
|
@ -5,6 +5,7 @@ from copilot import UserMessage, AssistantMessage, get_response
|
|||
from lib import AgentContext, PromptContext, ToolContext, ChatContext
|
||||
import os
|
||||
from functools import wraps
|
||||
from copilot import copilot_instructions, copilot_instructions_edit_agent
|
||||
|
||||
class ApiRequest(BaseModel):
|
||||
messages: List[UserMessage | AssistantMessage]
|
||||
|
|
@ -22,7 +23,7 @@ def validate_request(request_data: ApiRequest) -> None:
|
|||
"""Validate the chat request data."""
|
||||
if not request_data.messages:
|
||||
raise ValueError('Messages list cannot be empty')
|
||||
|
||||
|
||||
if not isinstance(request_data.messages[-1], UserMessage):
|
||||
raise ValueError('Last message must be a user message')
|
||||
|
||||
|
|
@ -49,45 +50,70 @@ def health():
|
|||
@require_api_key
|
||||
def chat():
|
||||
try:
|
||||
# Log incoming request
|
||||
print(f"Incoming request: {request.json}")
|
||||
|
||||
# Parse and validate request
|
||||
request_data = ApiRequest(**request.json)
|
||||
validate_request(request_data)
|
||||
|
||||
# Process chat request
|
||||
|
||||
response = get_response(
|
||||
messages=request_data.messages,
|
||||
workflow_schema=request_data.workflow_schema,
|
||||
current_workflow_config=request_data.current_workflow_config,
|
||||
context=request_data.context
|
||||
context=request_data.context,
|
||||
copilot_instructions=copilot_instructions
|
||||
)
|
||||
|
||||
# Create API response
|
||||
api_response = ApiResponse(response=response).model_dump()
|
||||
|
||||
# Log response before sending
|
||||
print(f"Outgoing response: {api_response}")
|
||||
|
||||
|
||||
return jsonify(api_response)
|
||||
|
||||
|
||||
except ValidationError as ve:
|
||||
print(ve)
|
||||
return jsonify({
|
||||
'error': 'Invalid request format',
|
||||
'details': str(ve)
|
||||
}), 400
|
||||
except ValueError as ve:
|
||||
print(ve)
|
||||
return jsonify({
|
||||
'error': 'Invalid request data',
|
||||
'details': str(ve)
|
||||
}), 400
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({
|
||||
'error': 'Internal server error',
|
||||
'details': str(e)
|
||||
}), 500
|
||||
|
||||
@app.route('/edit_agent_instructions', methods=['POST'])
|
||||
@require_api_key
|
||||
def edit_agent_instructions():
|
||||
try:
|
||||
request_data = ApiRequest(**request.json)
|
||||
validate_request(request_data)
|
||||
|
||||
response = get_response(
|
||||
messages=request_data.messages,
|
||||
workflow_schema=request_data.workflow_schema,
|
||||
current_workflow_config=request_data.current_workflow_config,
|
||||
context=request_data.context,
|
||||
copilot_instructions=copilot_instructions_edit_agent
|
||||
)
|
||||
|
||||
api_response = ApiResponse(response=response).model_dump()
|
||||
return jsonify(api_response)
|
||||
|
||||
except ValidationError as ve:
|
||||
# Log the unexpected error here
|
||||
print(ve)
|
||||
return jsonify({
|
||||
'error': 'Invalid request format',
|
||||
'details': str(ve)
|
||||
}), 400
|
||||
except ValueError as ve:
|
||||
# Log the unexpected error here
|
||||
print(ve)
|
||||
return jsonify({
|
||||
'error': 'Invalid request data',
|
||||
'details': str(ve)
|
||||
}), 400
|
||||
except Exception as e:
|
||||
# Log the unexpected error here
|
||||
print(e)
|
||||
return jsonify({
|
||||
'error': 'Internal server error',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue