Skip to content

refactor: refactor chat service#1447

Open
bytecii wants to merge 8 commits intomainfrom
refactor/refactor_chat_service
Open

refactor: refactor chat service#1447
bytecii wants to merge 8 commits intomainfrom
refactor/refactor_chat_service

Conversation

@bytecii
Copy link
Collaborator

@bytecii bytecii commented Mar 5, 2026

Description

refactor 1 / n

Source split:

backend/app/service/chat_service.py
  → backend/app/service/chat_service/

├── __init__.py              — Public API re-exports
├── step_solve.py            — Main async generator loop (was step_solve function)
├── question_router.py       — Query classification + simple answer routing (was question_confirm/simple_answer calls + routing logic)
├── handlers.py              — Action handlers: improve, pause, resume, end, new_task, etc.
├── lifecycle.py             — Agent/workforce construction: construct_workforce, new_agent_model, install_mcp
├── decomposition.py         — Task decomposition: make_sub_tasks, start_workforce
└── types.py                 — StepSolveState dataclass (shared mutable state for the loop)

Functions moved to agent layer:

chat_service.py → backend/app/agent/factory/
├── question_confirm.py      — question_confirm() + simple_answer() + _create_question_agent()
│                              (were inline in chat_service, now cached on task_lock.question_agent)
├── task_summary.py          — summary_task() + summary_subtasks_result() + get_task_result_with_optional_summary()
│                              (inference functions moved to agent layer where they belong)
└── workforce_agents.py      — create_coordinator_and_task_agents() + create_new_worker_agent()
                               (were inline in construct_workforce, extracted as factory functions)

Shared constants extracted:

chat_service.py → backend/app/agent/prompt.py

├── AGENT_ENVIRONMENT_PROMPT          — Platform/working-dir/date template (was duplicated 4x inline)
├── DEVELOPER_WORKER_DESCRIPTION      — Workforce agent description (was inline string in construct_workforce)
├── BROWSER_WORKER_DESCRIPTION        — Same
├── DOCUMENT_WORKER_DESCRIPTION       — Same
└── MULTI_MODAL_WORKER_DESCRIPTION    — Same

Context utilities extracted:

chat_service.py → backend/app/utils/context.py

├── build_conversation_context()      — Formats task_lock.conversation_history
├── collect_previous_task_context()   — Gathers previous task content + generated files
└── format_task_context()             — Formats a single task context block

Testing Evidence (REQUIRED)

  • I have included human-verified testing evidence in this PR.
  • This PR includes frontend/UI changes, and I attached screenshot(s) or screen recording(s).
  • No frontend/UI changes in this PR.

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Contribution Guidelines Acknowledgement

@bytecii bytecii marked this pull request as ready for review March 5, 2026 20:02
@bytecii bytecii requested review from 4pmtong and Wendong-Fan March 5, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant