stateDiagram-v2
[*] --> RouteResolved
RouteResolved --> ScopeResolved: parse route/query/share
ScopeResolved --> SessionStart: new chat request
SessionStart --> ThreadReady: session start returns thread and canonical path
ScopeResolved --> ThreadReady: has existing thread
ScopeResolved --> ThreadBootstrap: fallback when thread missing during send
ThreadBootstrap --> ThreadReady: createThread or ensureShareSessionThread
ThreadReady --> Sending: user sends message
Sending --> Streaming: /api/ai/stream connected
Streaming --> AwaitingDecision: actions require HITL
Streaming --> ThreadReady: done without HITL
AwaitingDecision --> Resuming: approve/reject batch
Resuming --> Streaming: /api/ai/resume stream
Resuming --> ThreadReady: resume non-stream fallback
ThreadReady --> RouteResolved: thread switch / new chat reset
- append pending user message ใน local state
- materialize thread
- primary path: ใช้
startSession สำหรับ new chat/discovery start
- fallback path:
ensureShareSessionThread หรือ createThread เมื่อ send แล้ว thread ยังไม่พร้อม
- persist user message
- create assistant placeholder (
isStreaming=true)
- call
/api/ai/stream
- stream chunks เข้าข้อความ placeholder
- normalize/filter actions ตาม policy
- persist final assistant message และ sync pending IDs
flowchart LR
A[Route enters with partial scope] --> B[load thread metadata]
B --> C{missing scope?}
C -- yes --> D[derive board/map/dashboard]
D --> E[replace URL to canonical path]
C -- no --> F[keep current URL]
- endpoint:
POST /api/sessions/start
- request keys:
projectId, agentId, optional scopeId, history, intent, source
- response keys หลัก:
threadId, scopeKind, scopeId, canonicalPath, sessionVersion
- URL metadata ที่ใช้ sync รอบใหม่:
scopeKind, scopeId, sv
- decisions ถูก index ด้วย
toolCallId
- staged states:
approved / rejected / submitted
- resume รองรับ stream และ non-stream fallback
- หลัง resume ต้อง persist summary/actions เพื่อกันค้าง
isStreaming
addMessage เจอ missing thread -> recover thread แล้ว retry
- scope mismatch -> clear poisoned session และ redirect path ที่ปลอดภัย
- resume stream ไม่จบสมบูรณ์ -> synthetic finalization + persist follow-up
- analytics not ready -> block input จน dataset/template พร้อม
- map thread insert เจอ legacy FK -> return
map_scope_schema_outdated และต้อง apply migration ที่เกี่ยวข้อง