-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask_manager.py
More file actions
396 lines (341 loc) · 16 KB
/
task_manager.py
File metadata and controls
396 lines (341 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
"""
Task management system for Dark.RL with SQLite storage.
Handles task creation, message history, and persistence.
"""
import sqlite3
import json
import uuid
from datetime import datetime
from typing import List, Dict, Optional, Any
from pathlib import Path
class TaskManager:
"""Manages tasks and conversation history in SQLite"""
def __init__(self, db_path: str = "dark_rl_tasks.db"):
self.db_path = db_path
self.init_database()
def init_database(self):
"""Initialize the SQLite database with required tables"""
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
initial_prompt TEXT NOT NULL,
model TEXT DEFAULT 'qwen2.5-vl',
mcp_servers TEXT DEFAULT '[]',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status TEXT DEFAULT 'active',
current_state TEXT DEFAULT 'idle'
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (task_id) REFERENCES tasks (id)
)
""")
# Create indexes for better performance
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_messages_task_id
ON messages (task_id)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_messages_timestamp
ON messages (timestamp)
""")
# New table for storing response preferences
conn.execute("""
CREATE TABLE IF NOT EXISTS response_preferences (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_id TEXT NOT NULL,
user_prompt TEXT NOT NULL,
local_response TEXT NOT NULL,
gpt_response TEXT NOT NULL,
preferred_model TEXT NOT NULL, -- 'local' or 'gpt'
local_model_name TEXT,
gpt_model_name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (task_id) REFERENCES tasks (id)
)
""")
# New table for storing pending dual responses
conn.execute("""
CREATE TABLE IF NOT EXISTS pending_dual_responses (
task_id TEXT PRIMARY KEY,
local_response TEXT NOT NULL,
gpt_response TEXT NOT NULL,
local_model TEXT,
gpt_model TEXT,
local_finished BOOLEAN DEFAULT 0,
gpt_finished BOOLEAN DEFAULT 0,
session_id INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (task_id) REFERENCES tasks (id)
)
""")
conn.commit()
def create_task(self, initial_prompt: str, model: str = "qwen2.5-vl") -> str:
"""Create a new task and return its ID"""
task_id = str(uuid.uuid4())
# Generate a title from the first few words of the prompt
words = initial_prompt.split()[:8]
title = " ".join(words) + ("..." if len(initial_prompt.split()) > 8 else "")
# Extract MCP servers referenced with @ symbol
mcp_servers = self._extract_mcp_servers(initial_prompt)
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
INSERT INTO tasks (id, title, initial_prompt, model, mcp_servers)
VALUES (?, ?, ?, ?, ?)
""", (task_id, title, initial_prompt, model, json.dumps(mcp_servers)))
# Add the initial user message
conn.execute("""
INSERT INTO messages (task_id, role, content)
VALUES (?, ?, ?)
""", (task_id, 'user', initial_prompt))
conn.commit()
return task_id
def get_task(self, task_id: str) -> Optional[Dict[str, Any]]:
"""Get task details by ID"""
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute("""
SELECT * FROM tasks WHERE id = ?
""", (task_id,))
row = cursor.fetchone()
if row:
task_data = dict(row)
# Parse MCP servers JSON string back to list
try:
task_data['mcp_servers'] = json.loads(task_data['mcp_servers'])
except (json.JSONDecodeError, TypeError):
task_data['mcp_servers'] = []
return task_data
return None
def get_task_messages(self, task_id: str) -> List[Dict[str, Any]]:
"""Get all messages for a task"""
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute("""
SELECT role, content, timestamp
FROM messages
WHERE task_id = ?
ORDER BY timestamp ASC
""", (task_id,))
return [dict(row) for row in cursor.fetchall()]
def get_task_messages_with_indexes(self, task_id: str) -> List[Dict[str, Any]]:
"""Get all messages for a task with their indexes for correction purposes"""
messages = self.get_task_messages(task_id)
# Add index to each message
for i, message in enumerate(messages):
message['index'] = i
return messages
def add_message(self, task_id: str, role: str, content: str) -> bool:
"""Add a message to a task"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
INSERT INTO messages (task_id, role, content)
VALUES (?, ?, ?)
""", (task_id, role, content))
# Update task's updated_at timestamp
conn.execute("""
UPDATE tasks SET updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (task_id,))
conn.commit()
return True
except Exception as e:
print(f"Error adding message: {e}")
return False
def replace_message(self, task_id: str, message_index: int, new_content: str) -> bool:
"""Replace a message at the specified index with new content"""
try:
with sqlite3.connect(self.db_path) as conn:
# First get all messages ordered by timestamp to find the correct message ID
cursor = conn.execute("""
SELECT id FROM messages
WHERE task_id = ?
ORDER BY timestamp ASC
""", (task_id,))
message_ids = [row[0] for row in cursor.fetchall()]
# Check if the index is valid
if message_index < 0 or message_index >= len(message_ids):
print(f"Error: Invalid message index {message_index}. Task has {len(message_ids)} messages.")
return False
# Get the message ID at the specified index
target_message_id = message_ids[message_index]
# Update the message content
conn.execute("""
UPDATE messages
SET content = ?, timestamp = CURRENT_TIMESTAMP
WHERE id = ?
""", (new_content, target_message_id))
# Update task's updated_at timestamp
conn.execute("""
UPDATE tasks SET updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (task_id,))
conn.commit()
print(f"✅ Successfully replaced message {message_index} in task {task_id}")
return True
except Exception as e:
print(f"Error replacing message: {e}")
return False
def get_recent_tasks(self, limit: int = 10) -> List[Dict[str, Any]]:
"""Get recent tasks ordered by updated_at"""
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute("""
SELECT id, title, initial_prompt, model, created_at, updated_at, status
FROM tasks
ORDER BY updated_at DESC
LIMIT ?
""", (limit,))
return [dict(row) for row in cursor.fetchall()]
def update_task_status(self, task_id: str, status: str) -> bool:
"""Update task status"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
UPDATE tasks SET status = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (status, task_id))
conn.commit()
return True
except Exception as e:
print(f"Error updating task status: {e}")
return False
def task_exists(self, task_id: str) -> bool:
"""Check if a task exists"""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute("""
SELECT 1 FROM tasks WHERE id = ? LIMIT 1
""", (task_id,))
return cursor.fetchone() is not None
def _extract_mcp_servers(self, text: str) -> List[str]:
"""Extract MCP server references from text (e.g., @playwright, @github)"""
import re
# Find all @ mentions followed by word characters
# Pattern: @ followed by word characters (letters, numbers, underscore, hyphen)
pattern = r'@([a-zA-Z0-9_-]+)'
matches = re.findall(pattern, text)
# Remove duplicates and return
return list(set(matches))
def save_response_preference(self, task_id: str, user_prompt: str, local_response: str,
gpt_response: str, preferred_model: str, local_model_name: str = None,
gpt_model_name: str = None) -> int:
"""Save user's response preference for analytics and learning"""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute("""
INSERT INTO response_preferences
(task_id, user_prompt, local_response, gpt_response, preferred_model, local_model_name, gpt_model_name)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (task_id, user_prompt, local_response, gpt_response, preferred_model, local_model_name, gpt_model_name))
conn.commit()
return cursor.lastrowid
def get_response_preferences(self, task_id: str = None) -> list:
"""Get response preferences, optionally filtered by task_id"""
with sqlite3.connect(self.db_path) as conn:
if task_id:
cursor = conn.execute("""
SELECT * FROM response_preferences WHERE task_id = ?
ORDER BY created_at DESC
""", (task_id,))
else:
cursor = conn.execute("""
SELECT * FROM response_preferences
ORDER BY created_at DESC
""")
return [dict(row) for row in cursor.fetchall()]
# New methods for simplified state management
def get_task_state(self, task_id: str) -> str:
"""Get current task state (idle, streaming_single, streaming_dual, awaiting_dual_selection)"""
with sqlite3.connect(self.db_path) as conn:
cursor = conn.execute("""
SELECT current_state FROM tasks WHERE id = ?
""", (task_id,))
row = cursor.fetchone()
return row[0] if row else 'idle'
def set_task_state(self, task_id: str, state: str) -> bool:
"""Set current task state"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
UPDATE tasks SET current_state = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""", (state, task_id))
conn.commit()
return True
except Exception as e:
print(f"Error setting task state: {e}")
return False
def get_pending_dual_responses(self, task_id: str) -> Optional[Dict[str, Any]]:
"""Get pending dual responses for a task"""
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.execute("""
SELECT * FROM pending_dual_responses WHERE task_id = ?
""", (task_id,))
row = cursor.fetchone()
if row:
return dict(row)
return None
def set_pending_dual_responses(self, task_id: str, local_response: str, gpt_response: str,
local_model: str = None, gpt_model: str = None,
local_finished: bool = False, gpt_finished: bool = False,
session_id: int = None) -> bool:
"""Save or update pending dual responses"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
INSERT OR REPLACE INTO pending_dual_responses
(task_id, local_response, gpt_response, local_model, gpt_model,
local_finished, gpt_finished, session_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", (task_id, local_response, gpt_response, local_model, gpt_model,
local_finished, gpt_finished, session_id))
conn.commit()
return True
except Exception as e:
print(f"Error setting pending dual responses: {e}")
return False
def clear_pending_dual_responses(self, task_id: str) -> bool:
"""Clear pending dual responses for a task"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.execute("""
DELETE FROM pending_dual_responses WHERE task_id = ?
""", (task_id,))
conn.commit()
return True
except Exception as e:
print(f"Error clearing pending dual responses: {e}")
return False
def update_dual_response_progress(self, task_id: str, source: str, content: str,
finished: bool = False) -> bool:
"""Update dual response progress (for streaming)"""
try:
with sqlite3.connect(self.db_path) as conn:
if source == 'local':
conn.execute("""
UPDATE pending_dual_responses
SET local_response = ?, local_finished = ?
WHERE task_id = ?
""", (content, finished, task_id))
elif source == 'gpt':
conn.execute("""
UPDATE pending_dual_responses
SET gpt_response = ?, gpt_finished = ?
WHERE task_id = ?
""", (content, finished, task_id))
conn.commit()
return True
except Exception as e:
print(f"Error updating dual response progress: {e}")
return False