diff --git a/apps/api/src/opendirector_api/agent.py b/apps/api/src/opendirector_api/agent.py index 7ef36a60b..7f7ee4c3d 100644 --- a/apps/api/src/opendirector_api/agent.py +++ b/apps/api/src/opendirector_api/agent.py @@ -1,15 +1,16 @@ """Project-scoped assistant persistence and native runtime streaming.""" import asyncio from contextlib import aclosing, suppress +from dataclasses import dataclass import json import logging from pathlib import Path from typing import Annotated, Literal -from uuid import UUID, uuid5 +from uuid import UUID, uuid4, uuid5 from anyio import CancelScope from fastapi import APIRouter, Header -from fastapi.responses import FileResponse, StreamingResponse +from fastapi.responses import FileResponse, JSONResponse, StreamingResponse from pydantic import BaseModel, ConfigDict, Field, JsonValue from opendirector.agent.catalog import model_choices @@ -148,6 +149,122 @@ def update(self, project_id: UUID, session: str | None = None, *, draft: Draft | result = public_conversation(value) return with_trace_sources(result, directory) + def queue(self, project_id: UUID, session: str, message: dict, conversation_id: UUID | None = None) -> dict: + """Store a supplementary message that runs after the active turn. + + The record keeps the shape of an accepted turn, so the run that picks it + up later needs nothing but what the user sent and the settings frozen + with it. + """ + with self.projects.local.transaction() as db: + self.projects._recover() + directory, _, _ = self.projects._load(db, project_id) + self.projects._lease(db, project_id, directory, session) + folder = child(directory, 'agent-runs') + folder.mkdir(exist_ok=True) + file = child(folder, 'conversation.json') + value = read_json(file) if file.exists() else {'messages': [], **Draft().model_dump()} + value['conversationId'] = value.get('conversationId') or str(uuid5(project_id, 'assistant')) + if conversation_id is not None and str(conversation_id) != value['conversationId']: + raise ProjectError('当前对话已切换,请重新加载后重试;原对话未覆盖。', 409) + if any(item['id'] == message['id'] for item in value['messages']): + raise ProjectError('这条消息已接受,请读取对话,不要重复发送。', 409) + value['queueSeq'] = value.get('queueSeq', 0) + 1 + queued = {**message, 'status': 'queued', 'queueSeq': value['queueSeq']} + value['messages'].append(queued) + durable_json(file, value) + return queued + + def dispatch(self, project_id: UUID, session: str | None = None) -> dict | None: + """Start the oldest queued message as this turn's user message. + + Dispatching takes the same lock as starting a run, so the project slot + never opens between one turn ending and the next beginning; a second run + cannot slip in ahead of the message that is already waiting. + """ + with self.projects.local.transaction() as db: + self.projects._recover() + directory, _, _ = self.projects._load(db, project_id) + if session: + self.projects._lease(db, project_id, directory, session) + folder = child(directory, 'agent-runs') + folder.mkdir(exist_ok=True) + file = child(folder, 'conversation.json') + value = read_json(file) if file.exists() else {'messages': [], **Draft().model_dump()} + value['conversationId'] = value.get('conversationId') or str(uuid5(project_id, 'assistant')) + pending = next((item for item in value['messages'] if item.get('status') == 'queued'), None) + if pending is None: + return None + index = value['messages'].index(pending) + pending['status'] = 'complete' + value['messages'].insert(index + 1, { + 'id': str(uuid5(UUID(pending['id']), 'assistant')), + 'role': 'assistant', 'text': '', 'status': 'streaming', + **{key: pending[key] for key in ('model', 'runtime') if key in pending}}) + value.update(draft='', draftContent=None, assets=[], skills=[]) + durable_json(file, value) + result = public_conversation(value) + return {'message': pending, 'conversation': with_trace_sources(result, directory)} + + def cancel_queued(self, project_id: UUID, session: str | None = None) -> list[dict]: + """Mark messages that will not run as cancelled, keeping them readable.""" + with self.projects.local.transaction() as db: + self.projects._recover() + directory, _, _ = self.projects._load(db, project_id) + if session: + self.projects._lease(db, project_id, directory, session) + file = child(child(directory, 'agent-runs'), 'conversation.json') + if not file.exists(): + return [] + value = read_json(file) + cancelled = [item for item in value['messages'] if item.get('status') == 'queued'] + if not cancelled: + return [] + for item in cancelled: + item['status'] = 'cancelled' + durable_json(file, value) + return cancelled + + def conversation_of(self, project_id: UUID, message_id: UUID) -> UUID | None: + """Find the conversation a message belongs to. + + A viewer that lost the connection keeps the turn it saw last. Once the + chain has moved on, that turn is over, but the conversation it belongs to + is still the one being written into, and that is how the stream can be + found again. + """ + directory = self.path(project_id) + file = child(child(directory, 'agent-runs'), 'conversation.json') + with self.projects.local.transaction(): + current = read_json(file) if file.exists() else {'messages': []} + for item in [current, *(current.get('archivedConversations') or [])]: + if any(message.get('id') == str(message_id) for message in item.get('messages') or []): + return UUID(item['conversationId']) + return None + + def retract(self, project_id: UUID, session: str, message_id: UUID) -> dict: + """Take a message back while it is still waiting in the queue. + + A message that already started belongs to the conversation now: it can + be stopped, but removing it would leave the reply it produced without + the words that asked for it. + """ + with self.projects.local.transaction() as db: + self.projects._recover() + directory, _, _ = self.projects._load(db, project_id) + self.projects._lease(db, project_id, directory, session) + file = child(child(directory, 'agent-runs'), 'conversation.json') + value = read_json(file) if file.exists() else {'messages': [], **Draft().model_dump()} + found = next((item for item in value['messages'] if item['id'] == str(message_id)), None) + if found is None: + raise ProjectError('这条消息不在当前对话里,请重新加载。', 404) + if found.get('status') != 'queued': + raise ProjectError('这条消息已经开始执行,停止回复后才能撤回。', 409) + value['messages'] = [item for item in value['messages'] if item['id'] != str(message_id)] + durable_json(file, value) + result = public_conversation(value) + return {'retracted': str(message_id), 'conversation': with_trace_sources(result, directory)} + def list(self, project_id: UUID) -> dict: directory = self.path(project_id) file = child(child(directory, 'agent-runs'), 'conversation.json') @@ -204,34 +321,66 @@ class ConversationCreate(BaseModel): class RunEvents: - """Events of one accepted turn, kept until the turn ends. + """Events of one conversation chain, replayable from any cursor. The run belongs to the project, not to the HTTP request that started it: leaving the project closes the stream, and returning attaches a new one that - replays from the beginning. Only an explicit stop cancels the run. + replays from the beginning, or from the last frame a viewer already has. A + chain keeps its events across turns, so one stream can carry the turn that is + running and the supplementary messages queued behind it. """ def __init__(self): self.items: list[dict] = [] self.changed = asyncio.Event() + self.closed = False def put(self, event: dict) -> None: - self.items.append(event) + self.items.append({**event, 'seq': len(self.items)}) + if event['type'] == 'end' and not event.get('nextTurnId'): + self.closed = True self.changed.set() self.changed = asyncio.Event() - async def follow(self): - index = 0 + async def follow(self, after: int = 0): + index = max(0, after) while True: while index < len(self.items): event = self.items[index] index += 1 yield 'data: ' + json.dumps(event, ensure_ascii=False) + '\n\n' - if event['type'] == 'end': + if event['type'] == 'end' and not event.get('nextTurnId'): return + if self.closed: + # The chain finished while this viewer was still catching up: its + # cursor is past the last frame, so serve what is left and stop + # rather than waiting for events that will never come. + return await self.changed.wait() +@dataclass +class Chain: + """One running turn and the messages queued behind it. + + The chain owns the project slot rather than the turn: the moment between one + turn ending and the next starting must stay closed, otherwise a second run + could be admitted alongside the follow-up that is about to start. A chain + lives until its queue is empty, so a supplementary message never races the + turn it was sent during. + """ + + id: UUID + project_id: UUID + events: RunEvents + session: str + turn: UUID + conversation_id: UUID | None = None + task: asyncio.Task | None = None + stopped: bool = False + last: dict | None = None + + def agent_routes(projects, generation, configs, provider=None) -> APIRouter: router = APIRouter(prefix='/api/creation/projects/{project_id}/assistant') conversations = Conversations(projects) @@ -257,17 +406,17 @@ def trace(project_id: UUID, turn_id: UUID, raise ProjectError('轨迹文件不存在。', 404) return FileResponse(file, media_type='text/plain', headers={'Cache-Control': 'no-store'}) - active: dict[UUID, tuple[UUID, asyncio.Task, RunEvents]] = {} + active: dict[UUID, Chain] = {} admission: dict[UUID, asyncio.Lock] = {} @router.get('') async def conversation(project_id: UUID): value = await asyncio.to_thread(conversations.update, project_id) running = active.get(project_id) - # A streaming message without an active turn was interrupted (for - # example by a restart); with one, the client can attach to it. - if running and not running[1].done(): - value = {**value, 'activeTurnId': str(running[0])} + # A streaming message without a chain was interrupted (for example by a + # restart); with one, the client can attach to the turn it is running. + if running is not None: + value = {**value, 'activeTurnId': str(running.turn)} return value @router.get('/conversations') @@ -320,163 +469,347 @@ async def catalog(project_id: UUID): found = await skills(projects.local.repository, directory) return [{key: item[key] for key in ('name', 'description', 'source')} for item in found] + def preferences_for(model_selections: dict) -> dict: + names = {row['id']: row['modelId'] for row in configs.list()} + return {kind: {'modelConfigId': model_id, 'modelName': names.get(model_id)} + for kind, model_id in model_selections.items() if model_id} + + async def prepare(project_id: UUID, value: Turn, session: str, accept_language: str | None) -> dict: + """Validate a submitted turn and resolve everything it needs to run. + + Validation happens while the user is still at the keyboard: a message + that could never run must fail now, not after the active turn, when the + user has already moved on. + """ + directory = await asyncio.to_thread(conversations.path, project_id, session) + if not runtime_available(value.runtime): + raise ProjectError(f'{value.runtime} 未安装,请安装 CLI 并登录。', 503) + spec = runtime_spec(value.runtime) + if not spec.supportsSkills and value.skills: + raise ProjectError(f'{spec.label} 不支持技能,请移除技能后发送。', 409) + if value.runtime == 'jiuwenswarm' and value.thinking != 'auto': + raise ProjectError('JiuwenSwarm 当前使用原生默认推理强度。', 409) + config = await asyncio.to_thread(configs.resolve, str(value.modelId), 'agent') if spec.modelSource == 'settings' else None + available = await skills(projects.local.repository, directory) if spec.supportsSkills else [] + if set(value.skills) - {item['name'] for item in available}: + raise ProjectError('技能已失效,请重新选择。', 409) + snapshot = await asyncio.to_thread(projects.snapshot, project_id) + assets = {str(asset.id): asset for asset in snapshot.assets} + for asset_id in value.assets: + if str(asset_id) not in assets: + raise ProjectError('引用素材不属于当前工程。', 409) + model = config['modelId'] if config else (value.runtimeModel or 'CLI 默认模型') + model_selections = dict(value.modelSelections) + return {'session': session, 'directory': directory, 'config': config, 'available': available, + 'model': model, 'model_selections': model_selections, + 'preferences': preferences_for(model_selections), 'thinking': value.thinking, + 'accept_language': accept_language, + 'message': {'id': str(value.id), 'role': 'user', 'text': value.text, 'content': value.content, + 'assets': [{'id': str(asset.id), 'name': asset.name, 'type': asset.metadata.mimeType, + 'size': asset.metadata.size, 'fingerprint': asset.fingerprint} + for asset in (assets[str(key)] for key in value.assets)], + 'skills': value.skills, 'runtime': value.runtime, 'runtimeModel': value.runtimeModel, + 'modelSelections': dict(value.modelSelections), 'model': model, + 'thinking': value.thinking, 'status': 'complete', + **({'modelConfigId': str(value.modelId)} if value.modelId else {})}} + + async def plan_for(project_id: UUID, message: dict, session: str, accept_language: str | None) -> dict: + """Settings for a message that was accepted earlier and is due now. + + The message was validated when the user sent it, so this rebuilds the + run settings without rejecting input that is already typed: a model that + has disappeared in the meantime falls back to the runtime default. + """ + directory = await asyncio.to_thread(conversations.path, project_id, session) + spec = runtime_spec(message['runtime']) + config = None + if spec.modelSource == 'settings' and message.get('modelConfigId'): + try: + config = await asyncio.to_thread(configs.resolve, message['modelConfigId'], 'agent') + except Exception: + logger.warning('agent.queue.model_missing project_id=%s message_id=%s', + project_id, message.get('id')) + available = await skills(projects.local.repository, directory) if spec.supportsSkills else [] + model_selections = dict(message.get('modelSelections') or {}) + return {'session': session, 'directory': directory, 'config': config, 'available': available, + 'model': message.get('model') or 'CLI 默认模型', 'model_selections': model_selections, + 'preferences': preferences_for(model_selections), 'accept_language': accept_language, + 'thinking': message.get('thinking') or 'auto', 'message': message} + + async def produce_one(chain: Chain, plan: dict) -> tuple[dict | None, str | None, str]: + """Run one turn and persist its reply; the caller decides what runs next.""" + project_id = chain.project_id + message = plan['message'] + turn_id = UUID(message['id']) + text, status, error = '', 'complete', None + activities: list[dict] = [] + sources: dict = {} + previous_sources: dict = {} + steps = None + try: + saved = plan['start'] + chain.conversation_id = UUID(saved['conversationId']) + chain.events.put({'type': 'start', 'conversation': saved}) + previous_sources = {source['id']: source for reply in saved['messages'][:-2] + for source in reply.get('sources', [])} + snapshot = await asyncio.to_thread(projects.snapshot, project_id) + assets = {str(asset.id): asset for asset in snapshot.assets} + context = [] + for reply in saved['messages'][:-1]: + content = reply['text'] or '(empty reply)' + references = [] + for attachment in reply.get('assets', []): + asset = assets.get(attachment['id']) + if asset is not None: + references.append({'id': str(asset.id), 'name': attachment['name'], + 'kind': asset.kind, 'path': str((plan['directory'] / asset.path).resolve()), + 'metadata': asset.metadata.model_dump(mode='json', exclude_none=True)}) + if references: + content += '\nReferenced project assets: ' + json.dumps(references, ensure_ascii=False) + content += '\nThese are asset references, not visual observations. Inspect media with available tools before describing its contents.' + context.append({'role': reply['role'], 'content': content}) + if plan['preferences']: + context[-1]['content'] += '\nUser model preferences for this turn: ' + json.dumps(plan['preferences'], ensure_ascii=False) + context[-1]['content'] += '\nUse these models for new generation unless the user explicitly requests another. If unavailable or unsuitable, explain and ask before substituting. Keep existing nodes on their configured models unless the user requests a change.' + run = RunContext(runtime=message['runtime'], project_id=project_id, turn_id=turn_id, + session=chain.session, directory=plan['directory'], config=plan['config'], messages=context, + selected_skills=message.get('skills') or [], thinking=plan['thinking'], + model_selections=plan['model_selections'], runtime_model=message.get('runtimeModel'), + skills=plan['available'], conversation_id=chain.conversation_id) + async with aclosing(runtimes.stream(run)) as stream: + async for event in stream: + if event['type'] == 'delta': + text += event['text'] + inherited = ([source for source in referenced_sources(text, previous_sources) + if source['id'] not in sources] + if previous_sources and '\ue201' in event['text'] else []) + if inherited: + sources.update({source['id']: source for source in inherited}) + chain.events.put({'type': 'sources', 'sources': inherited}) + if event['type'] == 'sources': + for source in event['sources']: + sources.setdefault(source['id'], source) + if event['type'] == 'plan': + steps = event['entries'] + if event['type'] == 'tool': + previous = next((item for item in activities if item['id'] == event['id']), None) + activity = {**{key: item for key, item in event.items() if key != 'type'}, + 'textOffset': previous['textOffset'] if previous else len(text)} + if previous is None: + activities.append(activity) + else: + activities[activities.index(previous)] = activity + event = {'type': 'tool', **activity} + chain.events.put(event) + except asyncio.CancelledError: + status = 'stopped' + raise + except Exception as failure: + # Persist partial output even when an integration fails unexpectedly. + status = 'interrupted' + if isinstance(failure, ProjectError): + error = localized_error_for_language( + plan.get('accept_language'), chinese=str(failure), + status=failure.status, code=failure.code) + else: + error = localized_error_for_language( + plan.get('accept_language'), + chinese='Agent 执行失败,请检查后端日志。', + status=500, code='operation_rejected') + logger.exception(f'agent.turn.failed project_id={project_id} turn_id={turn_id}') + finally: + for activity in activities: + if activity['status'] == 'running': + activity['status'] = 'interrupted' + try: + saved = await asyncio.to_thread(conversations.update, project_id, finish={ + 'id': str(uuid5(turn_id, 'assistant')), 'role': 'assistant', 'text': text, 'status': status, + 'model': plan['model'], + 'runtime': message['runtime'], 'thinking': plan['thinking'], 'tools': activities, + 'sources': list(sources.values()), + **({'plan': steps} if steps is not None else {})}, conversation_id=chain.conversation_id) + except Exception: + logger.exception(f'agent.turn.unfinished project_id={project_id} turn_id={turn_id}') + saved = None + chain.last = saved + return saved, error, status + + async def run_chain(chain: Chain, plan: dict) -> None: + """Run a turn, then take the next queued message until the queue is empty. + + Dispatching the next message and releasing the project slot happen under + the admission lock, so a message can never be appended after the queue + was found empty but before the slot is released: it either runs in this + chain or it starts a new one. + """ + project_id = chain.project_id + turn_id, saved, error, status = chain.turn, None, None, 'complete' + try: + while True: + saved, error, status = await produce_one(chain, plan) + turn_id = UUID(plan['message']['id']) + if status != 'complete' or chain.stopped: + break + async with admission.setdefault(project_id, asyncio.Lock()): + dispatched = None if chain.stopped else await asyncio.to_thread( + conversations.dispatch, project_id, chain.session) + if dispatched is None: + break + chain.turn = UUID(dispatched['message']['id']) + chain.events.put({'type': 'end', 'turnId': str(turn_id), 'nextTurnId': str(chain.turn), + 'conversation': dispatched['conversation'], 'error': None}) + plan = await plan_for(project_id, dispatched['message'], chain.session, None) + plan['start'] = dispatched['conversation'] + saved = None + except asyncio.CancelledError: + status, error = 'stopped', None + raise + finally: + chain.events.put({'type': 'end', 'turnId': str(turn_id), 'nextTurnId': None, + 'conversation': saved if saved is not None else chain.last, 'error': error}) + if active.get(project_id) is chain: + active.pop(project_id, None) + + def launch(project_id: UUID, plan: dict, conversation_id: UUID | None) -> StreamingResponse: + chain = Chain(id=uuid4(), project_id=project_id, events=RunEvents(), session=plan['session'], + turn=UUID(plan['message']['id']), conversation_id=conversation_id) + active[project_id] = chain + chain.task = asyncio.create_task(run_chain(chain, plan)) + def release(completed: asyncio.Task) -> None: + # The chain normally releases the slot itself; this covers a task + # cancelled before it ever started. + if active.get(project_id) is chain and completed is chain.task: + active.pop(project_id, None) + chain.task.add_done_callback(release) + # Closing this stream (switching projects, reloading) only detaches the + # viewer; the run continues and can be attached again. + return StreamingResponse(chain.events.follow(), media_type='text/event-stream') + + def rejected(turn_id: UUID, failure: ProjectError, accept_language: str | None) -> StreamingResponse: + """Report a message that cannot be opened as one already closed stream. + + A rejected message never entered the conversation, so the viewer has + nothing to catch up on: the stream carries the end frame a client + already knows how to show. + """ + events = RunEvents() + events.put({'type': 'end', 'turnId': str(turn_id), 'nextTurnId': None, 'conversation': None, + 'error': localized_error_for_language(accept_language, chinese=str(failure), + status=failure.status, code=failure.code)}) + return StreamingResponse(events.follow(), media_type='text/event-stream') + @router.post('/turns/{turn_id}/stop') async def stop(project_id: UUID, turn_id: UUID, x_opendirector_session: UUID = Header()): - await asyncio.to_thread(conversations.path, project_id, str(x_opendirector_session)) - running = active.get(project_id) - if running and running[0] == turn_id: + session = str(x_opendirector_session) + await asyncio.to_thread(conversations.path, project_id, session) + chain = active.get(project_id) + if chain is not None and turn_id in (chain.turn, chain.id, chain.conversation_id): + chain.stopped = True with CancelScope(shield=True): - if not running[1].cancelling(): - running[1].cancel() - with suppress(asyncio.CancelledError): - await running[1] + if chain.task is not None and not chain.task.cancelling(): + chain.task.cancel() + if chain.task is not None: + with suppress(asyncio.CancelledError): + await chain.task + async with admission.setdefault(project_id, asyncio.Lock()): + await asyncio.to_thread(conversations.cancel_queued, project_id, session) return {'ok': True} @router.get('/turns/{turn_id}/events') - async def turn_events(project_id: UUID, turn_id: UUID): + async def turn_events(project_id: UUID, turn_id: UUID, after: int = 0): await asyncio.to_thread(conversations.path, project_id) - running = active.get(project_id) - if not running or running[0] != turn_id: + chain = active.get(project_id) + if chain is not None and turn_id not in (chain.turn, chain.id): + # A viewer that lost the stream between two turns remembers the turn + # it saw last, which is already over while its chain keeps running. + settled = await asyncio.to_thread(conversations.conversation_of, project_id, turn_id) + if settled is None or settled != chain.conversation_id: + chain = None + if chain is None: raise ProjectError('这轮回复已结束,请重新读取对话。', 404) - return StreamingResponse(running[2].follow(), media_type='text/event-stream') + return StreamingResponse(chain.events.follow(after), media_type='text/event-stream') @router.post('/turns') async def turn(project_id: UUID, value: Turn, x_opendirector_session: UUID = Header(), accept_language: str | None = Header(default=None, alias='Accept-Language')): + session = str(x_opendirector_session) async with admission.setdefault(project_id, asyncio.Lock()): if project_id in active: raise ProjectError('当前工程已有回复正在执行。', 409) - session = str(x_opendirector_session) - directory = await asyncio.to_thread(conversations.path, project_id, session) - if not runtime_available(value.runtime): - raise ProjectError(f'{value.runtime} 未安装,请安装 CLI 并登录。', 503) - spec = runtime_spec(value.runtime) - if not spec.supportsSkills and value.skills: - raise ProjectError(f'{spec.label} 不支持技能,请移除技能后发送。', 409) - if value.runtime == 'jiuwenswarm' and value.thinking != 'auto': - raise ProjectError('JiuwenSwarm 当前使用原生默认推理强度。', 409) - config = await asyncio.to_thread(configs.resolve, str(value.modelId), 'agent') if spec.modelSource == 'settings' else None - available = await skills(projects.local.repository, directory) if spec.supportsSkills else [] - if set(value.skills) - {item['name'] for item in available}: - raise ProjectError('技能已失效,请重新选择。', 409) - snapshot = await asyncio.to_thread(projects.snapshot, project_id) - assets = {str(asset.id): asset for asset in snapshot.assets} - for asset_id in value.assets: - if str(asset_id) not in assets: - raise ProjectError('引用素材不属于当前工程。', 409) - model = config['modelId'] if config else (value.runtimeModel or 'CLI 默认模型') - model_selections = dict(value.modelSelections) - model_names = {row['id']: row['modelId'] for row in configs.list()} - preferences = {kind: {'modelConfigId': model_id, 'modelName': model_names.get(model_id)} - for kind, model_id in model_selections.items() if model_id} - events = RunEvents() - async def produce() -> None: - text, status, error, accepted = '', 'complete', None, False - activities = [] - sources = {} - previous_sources = {} - plan = None - try: - user = {'id': str(value.id), 'role': 'user', 'text': value.text, 'content': value.content, - 'assets': [{'id': str(asset.id), 'name': asset.name, 'type': asset.metadata.mimeType, - 'size': asset.metadata.size, 'fingerprint': asset.fingerprint} - for asset in (assets[str(key)] for key in value.assets)], - 'skills': value.skills, 'runtime': value.runtime, 'runtimeModel': value.runtimeModel, - 'modelSelections': dict(value.modelSelections), - 'model': model, 'status': 'complete'} - saved = await asyncio.to_thread(conversations.update, project_id, session, begin=user, conversation_id=value.conversationId) - accepted = True - events.put({'type': 'start', 'conversation': saved}) - previous_sources = {source['id']: source for message in saved['messages'][:-2] - for source in message.get('sources', [])} - context = [] - for message in saved['messages'][:-1]: - content = message['text'] or '(empty reply)' - references = [] - for attachment in message.get('assets', []): - asset = assets.get(attachment['id']) - if asset is not None: - references.append({'id': str(asset.id), 'name': attachment['name'], - 'kind': asset.kind, 'path': str((directory / asset.path).resolve()), - 'metadata': asset.metadata.model_dump(mode='json', exclude_none=True)}) - if references: - content += '\nReferenced project assets: ' + json.dumps(references, ensure_ascii=False) - content += '\nThese are asset references, not visual observations. Inspect media with available tools before describing its contents.' - context.append({'role': message['role'], 'content': content}) - if preferences: - context[-1]['content'] += '\nUser model preferences for this turn: ' + json.dumps(preferences, ensure_ascii=False) - context[-1]['content'] += '\nUse these models for new generation unless the user explicitly requests another. If unavailable or unsuitable, explain and ask before substituting. Keep existing nodes on their configured models unless the user requests a change.' - run = RunContext(runtime=value.runtime, project_id=project_id, turn_id=value.id, - session=session, directory=directory, config=config, messages=context, - selected_skills=value.skills, thinking=value.thinking, model_selections=model_selections, - runtime_model=value.runtimeModel, skills=available, conversation_id=UUID(saved['conversationId'])) - async with aclosing(runtimes.stream(run)) as stream: - async for event in stream: - if event['type'] == 'delta': - text += event['text'] - inherited = ([source for source in referenced_sources(text, previous_sources) - if source['id'] not in sources] - if previous_sources and '\ue201' in event['text'] else []) - if inherited: - sources.update({source['id']: source for source in inherited}) - events.put({'type': 'sources', 'sources': inherited}) - if event['type'] == 'sources': - for source in event['sources']: - sources.setdefault(source['id'], source) - if event['type'] == 'plan': - plan = event['entries'] - if event['type'] == 'tool': - previous = next((item for item in activities if item['id'] == event['id']), None) - activity = {**{key: item for key, item in event.items() if key != 'type'}, - 'textOffset': previous['textOffset'] if previous else len(text)} - if previous is None: - activities.append(activity) - else: - activities[activities.index(previous)] = activity - event = {'type': 'tool', **activity} - events.put(event) - except asyncio.CancelledError: - status = 'stopped' - raise - except Exception as failure: - # Persist partial output even when an integration fails unexpectedly. - status = 'interrupted' - if isinstance(failure, ProjectError): - error = localized_error_for_language( - accept_language, chinese=str(failure), - status=failure.status, code=failure.code) - else: - error = localized_error_for_language( - accept_language, - chinese='Agent 执行失败,请检查后端日志。', - status=500, code='operation_rejected') - logger.exception(f'agent.turn.failed project_id={project_id} turn_id={value.id}') - finally: - if accepted: - for activity in activities: - if activity['status'] == 'running': - activity['status'] = 'interrupted' - saved = await asyncio.to_thread(conversations.update, project_id, finish={ - 'id': str(uuid5(value.id, 'assistant')), 'role': 'assistant', 'text': text, 'status': status, - 'model': model, - 'runtime': value.runtime, 'thinking': value.thinking, 'tools': activities, - 'sources': list(sources.values()), - **({'plan': plan} if plan is not None else {})}, conversation_id=value.conversationId) - else: - saved = None - events.put({'type': 'end', 'conversation': saved, 'error': error}) - + plan = await prepare(project_id, value, session, accept_language) + try: + plan['start'] = await asyncio.to_thread(conversations.update, project_id, session, + begin=plan['message'], conversation_id=value.conversationId) + except ProjectError as failure: + # The message points at a thread that is no longer the active one. + # Clients of this endpoint read that from the stream, so report it + # where they already look instead of as a bare status code. + return rejected(UUID(plan['message']['id']), failure, accept_language) + return launch(project_id, plan, UUID(plan['start']['conversationId'])) + + @router.post('/messages') + async def message(project_id: UUID, value: Turn, x_opendirector_session: UUID = Header(), + accept_language: str | None = Header(default=None, alias='Accept-Language')): + """Accept a message while a turn runs and run it when that turn is done. + + A message that arrives while the project is idle starts a turn right + away, so a client can use one endpoint for both cases and never has to + guess whether the active turn has finished. + """ + session = str(x_opendirector_session) + async with admission.setdefault(project_id, asyncio.Lock()): + plan = await prepare(project_id, value, session, accept_language) + chain = active.get(project_id) + if chain is not None: + if value.conversationId is not None and chain.conversation_id is not None \ + and str(value.conversationId) != str(chain.conversation_id): + raise ProjectError('当前对话已切换,请重新加载后重试;原对话未覆盖。', 409) + queued = await asyncio.to_thread(conversations.queue, project_id, session, + plan['message'], value.conversationId) + chain.events.put({'type': 'queued', 'message': queued}) + return JSONResponse({'queued': queued, 'chainId': str(chain.id), + 'turnId': str(chain.turn)}, status_code=202) + await asyncio.to_thread(conversations.queue, project_id, session, plan['message'], value.conversationId) + dispatched = await asyncio.to_thread(conversations.dispatch, project_id, session) + if dispatched is None: + raise ProjectError('这条消息未能开始执行,请重新发送。', 409) + started = await plan_for(project_id, dispatched['message'], session, accept_language) + started['start'] = dispatched['conversation'] + return launch(project_id, started, UUID(dispatched['conversation']['conversationId'])) + + @router.delete('/messages/{message_id}') + async def retract(project_id: UUID, message_id: UUID, x_opendirector_session: UUID = Header()): + """Take back a message that has not started yet. + + Retracting under the admission lock is what keeps it from being picked + up in the same instant: the message is either still waiting and goes + away, or it has started and the caller is told to stop it instead. + """ + session = str(x_opendirector_session) + async with admission.setdefault(project_id, asyncio.Lock()): + result = await asyncio.to_thread(conversations.retract, project_id, session, message_id) + chain = active.get(project_id) + if chain is not None: + chain.events.put({'type': 'retracted', 'messageId': str(message_id)}) + return result + + @router.post('/queue/dispatch') + async def dispatch(project_id: UUID, x_opendirector_session: UUID = Header(), + accept_language: str | None = Header(default=None, alias='Accept-Language')): + """Continue the queue when nothing is running. + + Messages outlive a restart, and a turn that failed leaves them waiting; + this starts the oldest one so the user does not have to retype it. + """ + session = str(x_opendirector_session) + async with admission.setdefault(project_id, asyncio.Lock()): if project_id in active: raise ProjectError('当前工程已有回复正在执行。', 409) - task = asyncio.create_task(produce()) - active[project_id] = (value.id, task, events) - def release(completed: asyncio.Task) -> None: - running = active.get(project_id) - if running and running[0] == value.id and running[1] is completed: - active.pop(project_id) - task.add_done_callback(release) - # Closing this stream (switching projects, reloading) only detaches - # the viewer; the run continues and can be attached again. - return StreamingResponse(events.follow(), media_type='text/event-stream') + dispatched = await asyncio.to_thread(conversations.dispatch, project_id, session) + if dispatched is None: + raise ProjectError('没有等待执行的排队消息。', 409) + plan = await plan_for(project_id, dispatched['message'], session, accept_language) + plan['start'] = dispatched['conversation'] + return launch(project_id, plan, UUID(dispatched['conversation']['conversationId'])) return router diff --git a/docs/architecture/agent-runtime.md b/docs/architecture/agent-runtime.md index 3f86072af..c2751a701 100644 --- a/docs/architecture/agent-runtime.md +++ b/docs/architecture/agent-runtime.md @@ -51,7 +51,7 @@ Default 将模型配置中的输入模态传入 bundled OpenCode。外部 harnes Search citations keep their native markers in the original reply and trace. Completed Codex MCP web-search results supply validated source IDs, HTTP(S) URLs and titles through `sources` events; the conversation retains this metadata with the reply. The UI splits tool activities using raw text offsets before formatting prose citations as source links, and uses the same formatting for copying. Code examples remain literal. Missing sources and incomplete final markers have a localized fallback. For saved Codex replies without source metadata, queries derive it from the matching turn's project-local trace without rewriting the conversation; a bounded metadata cache is invalidated by trace modification time and size. Reading a trace never requests a model call. -同一工程同时只接受一个回复运行。运行事件转换为回复正文和工具活动,保存到对应会话;原生事件与业务工具事件追加到 `agent-runs/..jsonl`。Default 使用 `opencode` 作为轨迹文件的 runtime 名称。 +同一工程同时只执行一个回复。运行期间仍可提交补充消息:`POST /assistant/messages` 在空闲时直接开始一轮,在运行中返回 202 并按 `queueSeq` 把消息排入投递队列,本轮结束后依次运行。`end` 事件带 `nextTurnId` 时事件流不关闭,客户端因此能在同一条连接上接着收到下一轮;断线重连用 `GET /turns/{turn_id}/events?after=` 从已收到的最后一帧续读;链已经推进到下一轮时,客户端手里上一轮的 turn id 同样能找到这条链,不必等页面刷新。排队消息立即出现在会话里并可读;停止时整条链一起停止,排队消息标记为 `cancelled` 并保留下来供用户重新发送。运行事件转换为回复正文和工具活动,保存到对应会话;原生事件与业务工具事件追加到 `agent-runs/..jsonl`。Default 使用 `opencode` 作为轨迹文件的 runtime 名称。 Default 的模型网关在 `agent-runs/.model.jsonl` 记录实际转发的请求体,包括生效的系统消息、会话上下文、工具 Schema 与模型参数。请求 ID 关联上游 HTTP 状态、响应头等待耗时和传输错误。该文件仅供本地调试,创建权限为 `0600`,不记录 HTTP 鉴权头。回复片段、工具结果和 token 用量仍见原生轨迹。 diff --git a/tests/test_assistant_conversations.py b/tests/test_assistant_conversations.py index 8480e1619..56f7d5dc7 100644 --- a/tests/test_assistant_conversations.py +++ b/tests/test_assistant_conversations.py @@ -461,11 +461,11 @@ async def stream(self, run): yield {'type': 'delta', 'text': ',返回后完成'} monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) follow, calls = agent.RunEvents.follow, [] - async def counted(self): + async def counted(self, after=0): calls.append(self) if len(calls) > 1: attached.set() - async for item in follow(self): + async for item in follow(self, after): yield item monkeypatch.setattr(agent.RunEvents, 'follow', counted) turn_id = str(uuid4()) diff --git a/tests/test_assistant_queue.py b/tests/test_assistant_queue.py new file mode 100644 index 000000000..091606897 --- /dev/null +++ b/tests/test_assistant_queue.py @@ -0,0 +1,288 @@ +"""A message sent while a turn runs is queued and runs when that turn ends. + +#379: the project accepts one reply at a time, so a supplementary message used +to be refused outright. These cases pin the delivery guarantee: the message is +stored at once, it runs in order after the active turn, and stopping or +rejecting cannot leave a message that runs later by surprise. +""" +import asyncio +from concurrent.futures import ThreadPoolExecutor +import json +import threading +import time +from uuid import uuid4 + +from fastapi.testclient import TestClient +import pytest + +from opendirector.infrastructure.local_projects import LocalProjects +from opendirector_api.main import create_app +from opendirector_api import agent + + +def frames(response): + return [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')] + + +def wait_until(condition, timeout=5.0): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + if condition(): + return True + time.sleep(0.01) + return False + + +def replay(events, after=0): + """Collect one chain's replay exactly as a viewer receives it.""" + async def collect(): + return [json.loads(chunk.removeprefix('data: ')) + for chunk in [item async for item in events.follow(after)]] + return asyncio.run(collect()) + + +async def empty_skills(*args): + return [] + + +@pytest.fixture +def assistant(tmp_path, monkeypatch): + """A project whose first turn blocks until the test lets it finish.""" + contexts = [] + gate = {'started': threading.Event(), 'release': threading.Event()} + + async def stream(self, run): + contexts.append(run.messages) + gate['started'].set() + yield {'type': 'delta', 'text': '收到:' + run.messages[-1]['content']} + await asyncio.to_thread(gate['release'].wait, 10) + yield {'type': 'delta', 'text': '|完'} + + monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + monkeypatch.setattr(agent, 'skills', empty_skills) + from opendirector.infrastructure.model_configs import ModelConfigs + monkeypatch.setattr(ModelConfigs, 'resolve', lambda *args: {'id': str(uuid4()), 'modelId': 'test-model'}) + with TestClient(create_app(LocalProjects(tmp_path)), headers={ + 'X-OpenDirector-Request': '1', 'X-OpenDirector-Session': str(uuid4())}) as client: + pid = str(uuid4()) + root = f'/api/creation/projects/{pid}' + assert client.post(root + '/commands', json={'type': 'project.create', 'projectId': pid, + 'commandId': str(uuid4()), 'payload': {'name': '排队'}}).status_code == 200 + assert client.post(root + '/open').status_code == 200 + try: + yield client, root + '/assistant', contexts, gate + finally: + # A cancelled run is still waiting on this gate in a worker thread; + # releasing it lets the client shut down without waiting out the wait. + gate['release'].set() + + + +@pytest.fixture +def gated(tmp_path, monkeypatch): + """A project whose every turn waits for a release of its own.""" + contexts = [] + gates = {} + + async def stream(self, run): + text = run.messages[-1]['content'] + contexts.append(text) + gates.setdefault(text, threading.Event()) + yield {'type': 'delta', 'text': '收到:' + text} + await asyncio.to_thread(gates[text].wait, 10) + yield {'type': 'delta', 'text': '|完'} + + monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + monkeypatch.setattr(agent, 'skills', empty_skills) + from opendirector.infrastructure.model_configs import ModelConfigs + monkeypatch.setattr(ModelConfigs, 'resolve', lambda *args: {'id': str(uuid4()), 'modelId': 'test-model'}) + with TestClient(create_app(LocalProjects(tmp_path)), headers={ + 'X-OpenDirector-Request': '1', 'X-OpenDirector-Session': str(uuid4())}) as client: + pid = str(uuid4()) + root = f'/api/creation/projects/{pid}' + assert client.post(root + '/commands', json={'type': 'project.create', 'projectId': pid, + 'commandId': str(uuid4()), 'payload': {'name': '续读'}}).status_code == 200 + assert client.post(root + '/open').status_code == 200 + try: + yield client, root + '/assistant', contexts, gates + finally: + for waiting in gates.values(): + waiting.set() + +def test_supplementary_message_waits_for_the_running_turn_and_then_runs(assistant): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first, second = str(uuid4()), str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '先做这个'}) + assert gate['started'].wait(5) + sent = client.post(path + '/messages', json={ + 'id': second, 'runtime': 'default', 'conversationId': cid, 'text': '再补一句'}) + assert sent.status_code == 202, sent.text + assert sent.json()['queued']['status'] == 'queued' + assert sent.json()['queued']['id'] == second and sent.json()['turnId'] == first + # The viewer sees the supplementary message at once, without waiting. + waiting = client.get(path).json() + assert [item['status'] for item in waiting['messages']] == ['complete', 'streaming', 'queued'] + assert waiting['activeTurnId'] == first + gate['release'].set() + events = frames(running.result(timeout=10)) + + ending = [event for event in events if event['type'] == 'end'] + # The message is queued, not merged: it becomes its own turn on the same stream. + assert [event['turnId'] for event in ending] == [first, second] + assert ending[0]['nextTurnId'] == second + assert ending[-1]['nextTurnId'] is None and ending[-1]['error'] is None + messages = ending[-1]['conversation']['messages'] + assert [(item['role'], item['status']) for item in messages] == [ + ('user', 'complete'), ('assistant', 'complete'), ('user', 'complete'), ('assistant', 'complete')] + assert [item['text'] for item in messages if item['role'] == 'assistant'] == [ + '收到:先做这个|完', '收到:再补一句|完'] + assert [context[-1]['content'] for context in contexts] == ['先做这个', '再补一句'] + assert 'activeTurnId' not in client.get(path).json() + + +def test_one_stream_carries_every_turn_and_replays_from_a_cursor(): + events = agent.RunEvents() + events.put({'type': 'start'}) + events.put({'type': 'end', 'turnId': 't1', 'nextTurnId': 't2'}) + events.put({'type': 'start'}) + events.put({'type': 'end', 'turnId': 't2', 'nextTurnId': None}) + + # An end that names the next turn does not close the stream: the queued + # message runs on the connection the viewer already has open. + assert [frame['seq'] for frame in replay(events)] == [0, 1, 2, 3] + assert [frame['seq'] for frame in replay(events, 2)] == [2, 3] + # A cursor past the end of a finished chain ends at once instead of waiting. + assert replay(events, 9) == [] + + +def test_events_endpoint_resumes_from_the_cursor_the_viewer_sent(assistant, monkeypatch): + client, path, _, gate = assistant + seen = [] + original = agent.RunEvents.follow + + async def recorded(self, after=0): + seen.append(after) + async for item in original(self, after): + yield item + + monkeypatch.setattr(agent.RunEvents, 'follow', recorded) + cid = client.get(path).json()['conversationId'] + turn = str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': turn, 'runtime': 'default', 'conversationId': cid, 'text': '长任务'}) + assert gate['started'].wait(5) + tail = pool.submit(client.get, path + f'/turns/{turn}/events?after=1') + assert wait_until(lambda: len(seen) >= 2), seen + gate['release'].set() + resumed = frames(tail.result(timeout=10)) + whole = frames(running.result(timeout=10)) + + assert seen == [0, 1] + # The viewer receives exactly the frames that follow the one it already has. + assert resumed == whole[1:] + + +def test_stop_cancels_the_chain_and_leaves_queued_messages_cancelled(assistant): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first, second = str(uuid4()), str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '做到一半'}) + assert gate['started'].wait(5) + assert client.post(path + '/messages', json={ + 'id': second, 'runtime': 'default', 'conversationId': cid, 'text': '还没轮到'}).status_code == 202 + assert client.post(path + f'/turns/{first}/stop').status_code == 200 + events = frames(running.result(timeout=10)) + + assert events[-1]['type'] == 'end' and events[-1]['nextTurnId'] is None + # Stopping ends the whole chain: the queued message never runs, and it stays + # readable so the user can send it again deliberately. + assert [item['status'] for item in client.get(path).json()['messages']] == [ + 'complete', 'stopped', 'cancelled'] + assert len(contexts) == 1 + + +def test_supplementary_message_that_cannot_run_is_rejected_before_it_is_queued(assistant, monkeypatch): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first = str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '先做'}) + assert gate['started'].wait(5) + before = len(client.get(path).json()['messages']) + monkeypatch.setattr(agent, 'runtime_available', lambda _: False) + missing = client.post(path + '/messages', json={ + 'id': str(uuid4()), 'runtime': 'codex', 'conversationId': cid, 'text': '发不出去'}) + assert missing.status_code == 503 and '未安装' in missing.json()['detail'] + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + stale = client.post(path + '/messages', json={ + 'id': str(uuid4()), 'runtime': 'default', 'conversationId': str(uuid4()), 'text': '发错会话'}) + assert stale.status_code == 409 + # A rejected message is visible to the sender now, not after the active turn: + # the queue never holds work that was known to be unsendable. + assert len(client.get(path).json()['messages']) == before + gate['release'].set() + frames(running.result(timeout=10)) + assert len(contexts) == 1 + + +def test_idle_message_endpoint_starts_a_turn_and_keeps_message_ids_unique(assistant): + client, path, _, gate = assistant + cid = client.get(path).json()['conversationId'] + turn = str(uuid4()) + with ThreadPoolExecutor() as pool: + started = pool.submit(client.post, path + '/messages', json={ + 'id': turn, 'runtime': 'default', 'conversationId': cid, 'text': '直接说'}) + assert gate['started'].wait(5) + gate['release'].set() + response = started.result(timeout=10) + + # With nothing running the same endpoint starts the turn, so a client never + # has to guess whether the active turn has already finished. + assert response.status_code == 200 + events = frames(response) + assert events[0]['type'] == 'start' + assert events[-1]['conversation']['messages'][-1]['text'] == '收到:直接说|完' + duplicate = client.post(path + '/messages', json={ + 'id': turn, 'runtime': 'default', 'conversationId': cid, 'text': '重复发送'}) + assert duplicate.status_code == 409 + + +def test_a_viewer_that_lost_the_stream_resumes_through_the_turn_it_remembers(gated): + """Reconnecting between two turns still finds the chain. + + The viewer knows the turn it was watching, not the one that started while it + was away. Refusing that turn would leave it with a frozen conversation until + it thought to reload. + """ + client, path, contexts, gates = gated + cid = client.get(path).json()['conversationId'] + first, second = str(uuid4()), str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '先做这个'}) + assert wait_until(lambda: '先做这个' in gates) + assert client.post(path + '/messages', json={ + 'id': second, 'runtime': 'default', 'conversationId': cid, 'text': '再补一句'}).status_code == 202 + gates['先做这个'].set() + assert wait_until(lambda: '再补一句' in gates), contexts + # A turn from somewhere else is still refused: only this chain's own + # finished turn is enough to find this chain. + assert client.get(path + f'/turns/{uuid4()}/events').status_code == 404 + with client.stream('GET', path + f'/turns/{first}/events') as response: + assert response.status_code == 200 + gates['再补一句'].set() + lines = [line for line in response.iter_lines() if line.startswith('data: ')] + resumed = [json.loads(line[6:]) for line in lines] + whole = frames(running.result(timeout=10)) + + assert resumed == whole + assert [context for context in contexts] == ['先做这个', '再补一句'] diff --git a/tests/test_assistant_retract.py b/tests/test_assistant_retract.py new file mode 100644 index 000000000..3ea7f535d --- /dev/null +++ b/tests/test_assistant_retract.py @@ -0,0 +1,133 @@ +"""A waiting message can be taken back, and a stalled queue can be continued. + +#379: a supplementary message waits for the active turn. These cases pin what +happens around that wait: a message that has not started can be withdrawn, a +message that has started cannot, and a queue left behind by a failed turn waits +for the user instead of running on its own and then can be continued by hand. +""" +import asyncio +from concurrent.futures import ThreadPoolExecutor +import json +import threading +from uuid import UUID, uuid4, uuid5 + +from fastapi.testclient import TestClient +import pytest + +from opendirector.infrastructure.local_projects import LocalProjects +from opendirector_api.main import create_app +from opendirector_api import agent + + +def frames(response): + return [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')] + + +async def empty_skills(*args): + return [] + + +@pytest.fixture +def assistant(tmp_path, monkeypatch): + """A project whose turns block until the test lets them finish.""" + contexts = [] + gate = {'started': threading.Event(), 'release': threading.Event(), 'fail': threading.Event()} + + async def stream(self, run): + contexts.append(run.messages) + gate['started'].set() + yield {'type': 'delta', 'text': '收到:' + run.messages[-1]['content']} + await asyncio.to_thread(gate['release'].wait, 10) + if gate['fail'].is_set(): + raise RuntimeError('provider down') + yield {'type': 'delta', 'text': '|完'} + + monkeypatch.setattr(agent.AgentRuntimes, 'stream', stream) + monkeypatch.setattr(agent, 'runtime_available', lambda _: True) + monkeypatch.setattr(agent, 'skills', empty_skills) + from opendirector.infrastructure.model_configs import ModelConfigs + monkeypatch.setattr(ModelConfigs, 'resolve', lambda *args: {'id': str(uuid4()), 'modelId': 'test-model'}) + with TestClient(create_app(LocalProjects(tmp_path)), headers={ + 'X-OpenDirector-Request': '1', 'X-OpenDirector-Session': str(uuid4())}) as client: + pid = str(uuid4()) + root = f'/api/creation/projects/{pid}' + assert client.post(root + '/commands', json={'type': 'project.create', 'projectId': pid, + 'commandId': str(uuid4()), 'payload': {'name': '撤回'}}).status_code == 200 + assert client.post(root + '/open').status_code == 200 + yield client, root + '/assistant', contexts, gate + + +def test_a_queued_message_can_be_taken_back_before_it_runs(assistant): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first, second = str(uuid4()), str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '先做这个'}) + assert gate['started'].wait(5) + assert client.post(path + '/messages', json={ + 'id': second, 'runtime': 'default', 'conversationId': cid, 'text': '再补一句'}).status_code == 202 + taken = client.delete(path + f'/messages/{second}') + assert taken.status_code == 200, taken.text + assert taken.json()['retracted'] == second + # The message is gone from the thread the user is reading, while the + # turn that is still running keeps its own reply in place. + assert [item['id'] for item in taken.json()['conversation']['messages']] == [ + first, str(uuid5(UUID(first), 'assistant'))] + gate['release'].set() + events = frames(running.result(timeout=10)) + + # The turn that was already running is not disturbed, and nothing runs after it. + assert [event['turnId'] for event in events if event['type'] == 'end'] == [first] + assert [context[-1]['content'] for context in contexts] == ['先做这个'] + assert [item['status'] for item in client.get(path).json()['messages']] == ['complete', 'complete'] + + +def test_a_message_that_already_started_is_stopped_not_removed(assistant): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first = str(uuid4()) + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '做到一半'}) + assert gate['started'].wait(5) + started = client.delete(path + f'/messages/{first}') + assert started.status_code == 409 and '已经开始执行' in started.json()['detail'] + unknown = client.delete(path + f'/messages/{uuid4()}') + assert unknown.status_code == 404 and '不在当前对话里' in unknown.json()['detail'] + gate['release'].set() + frames(running.result(timeout=10)) + + # A message that has begun is part of the record: it stays, with its reply. + assert [item['status'] for item in client.get(path).json()['messages']] == ['complete', 'complete'] + assert len(contexts) == 1 + + +def test_a_failed_turn_leaves_the_queue_to_be_continued_by_hand(assistant): + client, path, contexts, gate = assistant + cid = client.get(path).json()['conversationId'] + first, second = str(uuid4()), str(uuid4()) + gate['fail'].set() + with ThreadPoolExecutor() as pool: + running = pool.submit(client.post, path + '/turns', json={ + 'id': first, 'runtime': 'default', 'conversationId': cid, 'text': '先做这个'}) + assert gate['started'].wait(5) + assert client.post(path + '/messages', json={ + 'id': second, 'runtime': 'default', 'conversationId': cid, 'text': '补一句'}).status_code == 202 + gate['release'].set() + events = frames(running.result(timeout=10)) + + assert events[-1]['error'] is not None + # A failure does not spend the queue by itself, and it is not silent about it. + assert [item['status'] for item in client.get(path).json()['messages']] == [ + 'complete', 'interrupted', 'queued'] + assert [context[-1]['content'] for context in contexts] == ['先做这个'] + + gate['fail'].clear() + continued = client.post(path + '/queue/dispatch') + assert continued.status_code == 200, continued.text + tail = frames(continued) + assert tail[-1]['conversation']['messages'][-1]['text'] == '收到:补一句|完' + # Nothing is left waiting, so continuing again has nothing to start. + assert client.post(path + '/queue/dispatch').status_code == 409 + assert 'activeTurnId' not in client.get(path).json()