feat: add single-call action protocol and Android stage interfaces
This commit is contained in:
+23
-4
@@ -58,10 +58,21 @@ CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY AUTOINCREMENT, conver
|
||||
|
||||
async def context(self, cid, current):
|
||||
def query(db):
|
||||
rows = list(db.execute("SELECT u.text user,a.text assistant FROM messages u JOIN messages a ON u.run_id=a.run_id AND u.conversation_id=a.conversation_id WHERE u.conversation_id=? AND u.run_id<>? AND u.role='user' AND a.role='assistant' AND u.status='completed' AND a.status='completed' AND a.playback='confirmed' ORDER BY u.id DESC LIMIT 10", (cid, current)))
|
||||
out = []
|
||||
for r in reversed(rows):
|
||||
out += [{'role': 'user', 'content': r['user'][:2000]}, {'role': 'assistant', 'content': r['assistant'][:2000]}]
|
||||
rows = list(db.execute("""SELECT m.role,m.text FROM messages m
|
||||
WHERE m.conversation_id=? AND m.run_id<>? AND m.status='completed'
|
||||
AND EXISTS (SELECT 1 FROM messages a WHERE a.conversation_id=m.conversation_id
|
||||
AND a.run_id=m.run_id AND a.role='assistant' AND a.status='completed' AND a.playback='confirmed')
|
||||
ORDER BY m.id DESC LIMIT 20""", (cid, current)))
|
||||
# Server followups have their own persisted speech turn, but providers
|
||||
# may require alternating chat roles. Join adjacent spoken assistant
|
||||
# rows in the request context only; preserve original history records.
|
||||
out=[]
|
||||
for row in reversed(rows):
|
||||
if not out and row['role'] != 'user': continue
|
||||
if out and out[-1]['role'] == row['role']:
|
||||
out[-1]['content']=(out[-1]['content']+'\n'+row['text'])[:2000]
|
||||
else:
|
||||
out.append({'role':row['role'],'content':row['text'][:2000]})
|
||||
return out
|
||||
return await self.call(query)
|
||||
|
||||
@@ -83,3 +94,11 @@ CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY AUTOINCREMENT, conver
|
||||
|
||||
async def clear(self, cid):
|
||||
await self.call(lambda db: db.execute('DELETE FROM messages WHERE conversation_id=?', (cid,)))
|
||||
|
||||
async def spoken(self, cid, rid, text):
|
||||
"""Server-initiated speech enters history only after actual playback.drained."""
|
||||
def insert(db):
|
||||
now = time.time()
|
||||
db.execute("INSERT OR IGNORE INTO messages(conversation_id,run_id,role,text,status,playback,created,updated) VALUES (?,?,?,?,?,?,?,?)",
|
||||
(cid, rid, 'assistant', text[:2000], 'completed', 'confirmed', now, now))
|
||||
await self.call(insert)
|
||||
|
||||
Reference in New Issue
Block a user