File size: 6,132 Bytes
666aab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Pydantic Models β€” Task, Chat, Memory, GitHub
"""

import time
import uuid
from enum import Enum
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field


def gen_id(prefix: str = "") -> str:
    return f"{prefix}{uuid.uuid4().hex[:12]}"


# ─── Enums ─────────────────────────────────────────────────────────────────────

class TaskStatus(str, Enum):
    queued = "queued"
    initializing = "initializing"
    planning = "planning"
    executing = "executing"
    streaming = "streaming"
    waiting_input = "waiting_input"
    retrying = "retrying"
    finalizing = "finalizing"
    completed = "completed"
    failed = "failed"
    cancelled = "cancelled"


class EventType(str, Enum):
    task_created = "task_created"
    task_queued = "task_queued"
    task_started = "task_started"
    plan_generated = "plan_generated"
    step_started = "step_started"
    step_progress = "step_progress"
    tool_called = "tool_called"
    tool_result = "tool_result"
    llm_chunk = "llm_chunk"
    memory_updated = "memory_updated"
    retry_attempt = "retry_attempt"
    step_completed = "step_completed"
    warning = "warning"
    error = "error"
    task_completed = "task_completed"
    task_failed = "task_failed"
    heartbeat = "heartbeat"


class MemoryType(str, Enum):
    conversation = "conversation"
    task = "task"
    project = "project"
    execution = "execution"
    tool = "tool"
    error = "error"
    repo = "repo"
    planning = "planning"


# ─── Task Models ───────────────────────────────────────────────────────────────

class TaskCreateRequest(BaseModel):
    goal: str = Field(..., min_length=1, max_length=10000, description="What should the agent do?")
    session_id: str = Field(default_factory=lambda: gen_id("sess_"))
    project_id: str = Field(default="")
    stream: bool = True
    metadata: Dict[str, Any] = Field(default_factory=dict)
    github_repo: Optional[str] = None
    auto_commit: bool = False


class TaskStep(BaseModel):
    id: str = Field(default_factory=lambda: gen_id("step_"))
    name: str
    description: str = ""
    tool: Optional[str] = None
    status: str = "pending"
    output: Optional[str] = None
    error: Optional[str] = None
    started_at: Optional[float] = None
    completed_at: Optional[float] = None
    duration_ms: Optional[float] = None


class TaskPlan(BaseModel):
    goal: str
    steps: List[TaskStep]
    estimated_duration: int = 0
    tools_needed: List[str] = []
    created_at: float = Field(default_factory=time.time)


class TaskResponse(BaseModel):
    id: str
    goal: str
    status: TaskStatus
    session_id: str
    project_id: str
    plan: Optional[TaskPlan] = None
    result: Optional[str] = None
    error: Optional[str] = None
    created_at: float
    started_at: Optional[float] = None
    completed_at: Optional[float] = None
    retry_count: int = 0
    stream_url: Optional[str] = None
    ws_url: Optional[str] = None


class TaskCancelRequest(BaseModel):
    reason: str = "User cancelled"


class TaskRetryRequest(BaseModel):
    reset_state: bool = True


# ─── Chat Models ───────────────────────────────────────────────────────────────

class ChatMessage(BaseModel):
    role: str = Field(..., pattern="^(user|assistant|system)$")
    content: str
    timestamp: float = Field(default_factory=time.time)


class ChatRequest(BaseModel):
    messages: List[ChatMessage]
    session_id: str = Field(default_factory=lambda: gen_id("sess_"))
    project_id: str = ""
    stream: bool = True
    model: str = "gpt-4o"
    temperature: float = 0.7
    max_tokens: int = 4096
    system_prompt: Optional[str] = None


class GoalRequest(BaseModel):
    goal: str = Field(..., min_length=1, max_length=10000)
    session_id: str = Field(default_factory=lambda: gen_id("sess_"))
    project_id: str = ""
    stream: bool = True
    auto_execute: bool = True
    github_repo: Optional[str] = None


# ─── Memory Models ─────────────────────────────────────────────────────────────

class MemorySaveRequest(BaseModel):
    content: str
    memory_type: MemoryType
    session_id: str = ""
    project_id: str = ""
    key: str = ""
    metadata: Dict[str, Any] = {}


class MemorySearchRequest(BaseModel):
    query: str
    session_id: str = ""
    project_id: str = ""
    limit: int = 20


# ─── GitHub Models ─────────────────────────────────────────────────────────────

class GitHubCloneRequest(BaseModel):
    repo_url: str
    branch: str = "main"
    local_path: Optional[str] = None


class GitHubCreateRepoRequest(BaseModel):
    name: str
    description: str = ""
    private: bool = False
    auto_init: bool = True


class GitHubCommitRequest(BaseModel):
    repo: str
    branch: str = "main"
    files: Dict[str, str]  # path β†’ content
    message: str
    create_branch: bool = False


class GitHubPRRequest(BaseModel):
    repo: str
    title: str
    body: str = ""
    head: str
    base: str = "main"
    draft: bool = False


class GitHubIssueRequest(BaseModel):
    repo: str
    title: str
    body: str = ""
    labels: List[str] = []


# ─── Event Schema (unified) ────────────────────────────────────────────────────

class StreamEvent(BaseModel):
    type: str
    task_id: str = ""
    session_id: str = ""
    timestamp: float = Field(default_factory=time.time)
    data: Dict[str, Any] = {}