Files
cowork-local/core/co4e_builtins.py
T
2026-08-09 20:12:05 +07:00

197 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Built-in Co4E agent personas (ported from nova-platform).
Built-ins are code constants — always available in the palette / sidebar /
parallel sub-agent picker, never saved to disk. Kept Qt-free for testing."""
from __future__ import annotations
from dataclasses import dataclass
from typing import List, Optional
from pathlib import Path
from .co4e import (
Edge, Node, Step, Workflow, get_workflow, save_workflow,
)
@dataclass
class BuiltinAgent:
slug: str
name: str
role: str
icon: str
instructions: str
permission_preset: str = "inherit"
# 14 built-in agent personas (name / role / read-only where the job is analysis).
BUILTIN_AGENTS: List[BuiltinAgent] = [
BuiltinAgent("business-analyst", "Business Analyst", "ANALYST", "file",
"Analyze the requirement/RFP. Extract goals, scope, stakeholders, constraints and "
"acceptance criteria. Output a clear, structured requirements breakdown.", "read-only"),
BuiltinAgent("slide-craft", "Slide Craft — RFP to Proposal", "PRESENTER", "eye",
"Turn the input into a persuasive proposal/slide outline: executive summary, win "
"themes, solution, plan, and pricing structure. Output ready-to-slide sections."),
BuiltinAgent("project-manager", "Project Manager", "PM", "schedule",
"Plan delivery: break work into milestones and tasks with owners, dependencies and a "
"realistic timeline. Flag risks and mitigations."),
BuiltinAgent("solution-architect", "Solution Architect", "ARCHITECT", "server",
"Design the solution architecture: components, data flow, technology choices and the "
"list of files/modules to create or change. Justify key decisions."),
BuiltinAgent("dba-expert", "DBA Expert", "DBA", "database",
"Design/optimize the database: schema, indexes, migrations and queries. Ensure "
"integrity, performance and safe rollout."),
BuiltinAgent("security-auditor", "Security Auditor", "SECURITY", "shield",
"Review for security issues: authn/authz, injection, secrets, data exposure and unsafe "
"dependencies. List findings by severity with concrete fixes.", "read-only"),
BuiltinAgent("pdf-to-markdown", "PDF to Markdown", "CONVERTER", "file",
"Convert the provided document faithfully to clean, well-structured Markdown, "
"preserving headings, tables and lists.", "read-only"),
BuiltinAgent("migration-expert", "Migration Expert", "MIGRATION", "link",
"Plan and perform the migration: assess the source, map to the target, produce "
"step-by-step migration scripts and a verification/rollback plan."),
BuiltinAgent("reviewer", "Reviewer", "REVIEWER", "check",
"Critically review the work for correctness, completeness and quality. Give specific, "
"actionable feedback; approve only when it genuinely meets the goal.", "read-only"),
BuiltinAgent("backend-dev", "Backend Dev", "BACKEND", "server",
"Implement the backend: APIs, business logic, data access and tests. Write clean, "
"working code in the working folder."),
BuiltinAgent("reverse-engineering", "Reverse Engineering", "REVERSE", "search",
"Analyze the existing code/binary to explain how it works: structure, key flows and "
"behavior. Produce a clear technical write-up.", "read-only"),
BuiltinAgent("cloud-expert", "Cloud Expert", "CLOUD", "cloud",
"Design/operate the cloud setup: infrastructure, deployment, scaling, cost and "
"reliability. Provide IaC or concrete configuration."),
BuiltinAgent("frontend-dev", "Frontend Dev", "FRONTEND", "workspaces",
"Implement the frontend/UI: components, state and styling. Write clean, working code "
"and match the design intent."),
BuiltinAgent("tester", "Tester", "QA", "beaker",
"Test what was built: write and run meaningful tests, report pass/fail and file clear "
"defects with reproduction steps."),
# ---- Delivery-lifecycle personas, one per bundled skill (see skill_library
# and DELIVERY_LIFECYCLE_FLOW). Each pairs with an attached skill of the same
# topic; the built-in flow chains all four end-to-end. ----
BuiltinAgent("requirement-analyst", "Requirement Analyst", "ANALYST", "file",
"Act as an expert Business Analyst. Do NOT jump to solution or code. Extract business "
"goal, current issue, expected outcome, scope, constraints, data and stakeholders; "
"classify requirements (functional/non-functional/data/integration/security/operation/"
"UI-UX/AI-agent/business/constraint); write acceptance criteria; list open questions. "
"Mark missing info as `Need Confirm` instead of assuming. Save the requirement "
"artifacts to the workspace. Keep requirement IDs traceable.", "full"),
BuiltinAgent("solution-designer", "Solution Designer", "ARCHITECT", "server",
"Act as an expert Solution Architect. Map each requirement to solution components; "
"propose at least two options when uncertain and recommend one; define architecture, "
"workflow, data model, integration, security/governance, deployment and risks; prepare "
"a WBS-level scope and ADR decisions. Save the design artifacts to the workspace. Keep "
"every decision traceable to requirement IDs or documented assumptions.", "full"),
BuiltinAgent("build-implementer", "Build Implementer", "BUILDER", "beaker",
"Act as an expert Tech Lead. Break the design into small executable tasks with "
"dependencies and done criteria; enforce coding, security, logging and error-handling "
"rules; prepare a test plan and review checklist; implement in the working folder and "
"verify against acceptance criteria. Never hardcode secrets. Keep tasks/changes/tests "
"traceable to requirement IDs.", "full"),
BuiltinAgent("demo-preparer", "Demo Preparer", "PRESENTER", "eye",
"Act as an expert Demo Director / Presales consultant. Start from the business story, "
"not features. Prepare demo goal, audience, before/after storyline, happy-path + "
"edge-case + failure/fallback scenarios, speaker script, data & environment checklists, "
"customer Q&A and next actions. Then ACTUALLY SET UP THE DEMO ENVIRONMENT AND RUN THE "
"APP in the workspace: create/prepare the runtime (venv + install dependencies or the "
"documented setup), start the app/build, and verify it launches and the happy-path "
"works — capturing the exact run commands and any fixes into a runbook. Save the demo "
"artifacts to the workspace. Never expose credentials or sensitive data; keep claims "
"aligned with what you actually ran and verified.", "full"),
BuiltinAgent("security-agent", "Security Agent", "SECURITY", "shield",
"Act as an expert Security Reviewer / governance auditor. Assess the prompt, attached "
"content, tool/action, design or code against security rules: prompt injection, "
"secret/credential exposure, destructive or out-of-scope actions, RBAC/least "
"privilege, injection, data exposure, unsafe dependencies, audit and data "
"classification. Report findings by severity with concrete fixes and give a clear "
"ALLOW / ALLOW-WITH-CONDITIONS / BLOCK verdict — default to BLOCK when uncertain. "
"Never expose secrets or protected source code.", "read-only"),
]
BUILTIN_AGENTS_BY_NAME = {a.name: a for a in BUILTIN_AGENTS}
BUILTIN_AGENTS_BY_SLUG = {a.slug: a for a in BUILTIN_AGENTS}
def step_from_builtin(name: str, instructions: str = "") -> Step:
"""A Step persona seeded from a built-in agent name (fallback to a generic
step if the name is unknown, e.g. a since-removed built-in)."""
a = BUILTIN_AGENTS_BY_NAME.get(name)
if a is None:
return Step(label=name or "New Step", instructions=instructions)
return Step(label=a.name, agent_slug=a.slug, role=a.role, icon=a.icon,
instructions=instructions or a.instructions,
permission_preset=a.permission_preset)
# ---- Built-in end-to-end flow: Analyze → Design → Build → Demo -----------
# Stable id so the built-in flow is refreshed (not duplicated) across upgrades.
DELIVERY_LIFECYCLE_FLOW_ID = "wf-builtin-delivery-lifecycle"
# (builtin agent name, attached skill name, permission preset, x position, per-step
# handoff context). All steps run with full workspace access + auto self-verify;
# each consumes the previous step's output (the runner threads it automatically),
# so a single raw requirement flows through to a demo end-to-end.
_LIFECYCLE_STEPS = [
("Requirement Analyst", "Analyze Requirement", "full", 0.0,
"FIRST step of the delivery pipeline. The raw requirement / RFP / meeting "
"notes are provided as THIS step's attachment or in the input. Analyze it per "
"the attached skill and SAVE the requirement artifacts (REQ_SPEC, scope "
"matrix, acceptance criteria, open questions) into the workspace. If nothing "
"was provided, clearly state what input you need and stop."),
("Solution Designer", "Solution Design", "full", 260.0,
"Use the requirement breakdown from the previous step (provided above) as your "
"input. Produce and SAVE the solution-design artifacts (architecture, option "
"comparison, data model, security design, WBS, ADRs). Keep every decision "
"traceable to the requirement IDs."),
("Build Implementer", "Build Implementation", "full", 520.0,
"Use the requirements + solution design from the previous steps. IMPLEMENT the "
"solution in the workspace with real, working code and tests; then run/verify "
"it. Save the code, test plan and a short verification report. Keep changes "
"traceable to the requirement IDs."),
("Demo Preparer", "Demo Preparation", "full", 780.0,
"Use everything produced by the previous steps. Prepare and SAVE a compelling, "
"safe demo of what was built: goal, before/after story, happy-path + edge-case "
"+ fallback scenarios, speaker script, data & environment checklists, Q&A and "
"next actions. Keep claims aligned with what was actually verified."),
]
def build_delivery_lifecycle_flow() -> Workflow:
"""A ready-to-run Co4E flow chaining the four delivery-lifecycle agents (skills
01–04), each carrying its matching bundled skill + a handoff context, so a raw
requirement runs FULLY end-to-end: Analyze Requirement → Solution Design →
Build Implementation → Demo Preparation. Every step has full workspace access
and auto self-verifies before advancing."""
nodes: List[Node] = []
for i, (agent_name, skill_name, preset, x, ctx) in enumerate(_LIFECYCLE_STEPS):
step = step_from_builtin(agent_name)
step.permission_preset = preset
step.skills = [skill_name] # resolved to full skill text by the runner's skill_map
step.context = ctx # per-step handoff guidance
step.self_verify = True # quality gate before the next step
nodes.append(Node(id=f"n-lc-{i}", x=x, y=0.0, data=step))
edges = [Edge(id=f"e-lc-{i}", source=nodes[i].id, target=nodes[i + 1].id)
for i in range(len(nodes) - 1)]
return Workflow(id=DELIVERY_LIFECYCLE_FLOW_ID,
name="Req2 Demo",
is_template=False, nodes=nodes, edges=edges)
def seed_builtin_flows(seeded_ids=None, directory: Optional[Path] = None) -> List[str]:
"""Ensure the built-in Co4E flow is present + current in the user's workflow
store on every launch, so it ALWAYS shows up in the Flow sidebar.
It is (re)written to its latest definition unconditionally — the canonical
built-in flow is treated like the built-in agents/skills (always available).
To customise it, Duplicate it in the Flow sidebar (the copy is yours and is
never overwritten). Returns the ids newly seeded THIS call (for the caller to
persist — informational only, since presence no longer depends on it)."""
already = set(seeded_ids or [])
newly: List[str] = []
fid = DELIVERY_LIFECYCLE_FLOW_ID
save_workflow(build_delivery_lifecycle_flow(), directory) # always ensure present + current
if fid not in already:
newly.append(fid)
return newly