From b77da51d58c4a0c845ef2b836567e8bc0f12d8dd Mon Sep 17 00:00:00 2001 From: Azat Date: Mon, 2 Feb 2026 23:25:12 +0100 Subject: [PATCH] Add conversation support with resume and DB storage --- scripts/run.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/scripts/run.sh b/scripts/run.sh index c0dc8cb..5d8bb1e 100644 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -287,6 +287,37 @@ delete_instance() { 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() { local id="$1" @@ -303,14 +334,49 @@ invoke_instance() { return fi + local conversation_id=$(echo "$body" | jq -r '.conversation_id // empty') local inst_id=$(echo "$instance" | jq -r '.id') 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" - 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 '.') - 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 @@ -356,8 +422,17 @@ case "$method:$path" in body="{\"personality\":\"$personality\"}" 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 HANDLER