Add conversation support with resume and DB storage
This commit is contained in:
@@ -287,6 +287,37 @@ delete_instance() {
|
|||||||
send_json '{"deleted":true}'
|
send_json '{"deleted":true}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Store conversation in DB
|
||||||
|
store_conversation() {
|
||||||
|
local conv_id="$1"
|
||||||
|
local instance_id="$2"
|
||||||
|
local db="$AGENTS_DATA_DIR/agents.db"
|
||||||
|
|
||||||
|
sqlite3 "$db" "INSERT OR IGNORE INTO conversations (id, instance_id, status) VALUES ('$conv_id', '$instance_id', 'active');"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Store message in DB
|
||||||
|
store_message() {
|
||||||
|
local conv_id="$1"
|
||||||
|
local role="$2"
|
||||||
|
local content="$3"
|
||||||
|
local db="$AGENTS_DATA_DIR/agents.db"
|
||||||
|
local msg_id=$(gen_id)
|
||||||
|
|
||||||
|
# Escape single quotes
|
||||||
|
content=$(echo "$content" | sed "s/'/''/g")
|
||||||
|
|
||||||
|
sqlite3 "$db" "INSERT INTO messages (id, conversation_id, role, content) VALUES ('$msg_id', '$conv_id', '$role', '$content');"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get conversation messages
|
||||||
|
get_conversation() {
|
||||||
|
local conv_id="$1"
|
||||||
|
local db="$AGENTS_DATA_DIR/agents.db"
|
||||||
|
|
||||||
|
sqlite3 -json "$db" "SELECT * FROM messages WHERE conversation_id='$conv_id' ORDER BY timestamp;"
|
||||||
|
}
|
||||||
|
|
||||||
# Invoke instance (run a prompt)
|
# Invoke instance (run a prompt)
|
||||||
invoke_instance() {
|
invoke_instance() {
|
||||||
local id="$1"
|
local id="$1"
|
||||||
@@ -303,14 +334,49 @@ invoke_instance() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
local conversation_id=$(echo "$body" | jq -r '.conversation_id // empty')
|
||||||
local inst_id=$(echo "$instance" | jq -r '.id')
|
local inst_id=$(echo "$instance" | jq -r '.id')
|
||||||
local workspace=$(echo "$instance" | jq -r '.workspace')
|
local workspace=$(echo "$instance" | jq -r '.workspace')
|
||||||
|
|
||||||
|
# Generate new conversation_id if not provided
|
||||||
|
local is_new_conversation=false
|
||||||
|
if [ -z "$conversation_id" ]; then
|
||||||
|
conversation_id=$(gen_id)$(gen_id)
|
||||||
|
is_new_conversation=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Store conversation if new
|
||||||
|
if [ "$is_new_conversation" = true ]; then
|
||||||
|
store_conversation "$conversation_id" "$inst_id"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Store user message
|
||||||
|
store_message "$conversation_id" "user" "$prompt"
|
||||||
|
|
||||||
cd "$workspace"
|
cd "$workspace"
|
||||||
local result=$("/tmp/agent_${inst_id}.sh" "$prompt" 2>&1) || true
|
|
||||||
|
# Build command - add --resume if continuing conversation
|
||||||
|
local result
|
||||||
|
if [ "$is_new_conversation" = true ]; then
|
||||||
|
result=$("/tmp/agent_${inst_id}.sh" "$prompt" 2>&1) || true
|
||||||
|
else
|
||||||
|
# Resume existing conversation
|
||||||
|
result=$(claude --print \
|
||||||
|
--resume "$conversation_id" \
|
||||||
|
"$prompt" 2>&1) || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Store assistant response
|
||||||
|
store_message "$conversation_id" "assistant" "$result"
|
||||||
|
|
||||||
local result_json=$(echo "$result" | jq -Rs '.')
|
local result_json=$(echo "$result" | jq -Rs '.')
|
||||||
send_json "{\"result\":$result_json}"
|
send_json "{\"conversation_id\":\"$conversation_id\",\"result\":$result_json}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# List conversations
|
||||||
|
list_conversations() {
|
||||||
|
local db="$AGENTS_DATA_DIR/agents.db"
|
||||||
|
sqlite3 -json "$db" "SELECT c.*, i.name as instance_name, i.personality FROM conversations c LEFT JOIN instances i ON c.instance_id = i.id ORDER BY c.started_at DESC LIMIT 50;" 2>/dev/null || echo "[]"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Route requests
|
# Route requests
|
||||||
@@ -356,8 +422,17 @@ case "$method:$path" in
|
|||||||
body="{\"personality\":\"$personality\"}"
|
body="{\"personality\":\"$personality\"}"
|
||||||
spawn_instance
|
spawn_instance
|
||||||
;;
|
;;
|
||||||
|
GET:/conversations)
|
||||||
|
result=$(list_conversations)
|
||||||
|
send_json "$result"
|
||||||
|
;;
|
||||||
|
GET:/conversations/*)
|
||||||
|
conv_id="${path#/conversations/}"
|
||||||
|
result=$(get_conversation "$conv_id")
|
||||||
|
send_json "$result"
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
send_error "404 Not Found" "Unknown endpoint. Try: GET /personalities, GET /instances, POST /instances"
|
send_error "404 Not Found" "Endpoints: GET /personalities, GET /instances, POST /instances, GET /conversations"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
HANDLER
|
HANDLER
|
||||||
|
|||||||
Reference in New Issue
Block a user