feat: NanoClaude Phase 1 — 비동기 job 기반 AI Gateway 코어 구현
POST /chat → job_id ACK, GET /chat/{job_id}/stream → SSE 스트리밍,
EXAONE Ollama adapter, JobManager, StateStream, Worker 구조
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
57
nanoclaude/services/job_manager.py
Normal file
57
nanoclaude/services/job_manager.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""JobManager — job lifecycle: create, track status, cancel."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from time import time
|
||||
|
||||
from models.schemas import JobStatus
|
||||
|
||||
|
||||
@dataclass
|
||||
class Job:
|
||||
id: str
|
||||
message: str
|
||||
status: JobStatus = JobStatus.queued
|
||||
created_at: float = field(default_factory=time)
|
||||
task: asyncio.Task | None = field(default=None, repr=False)
|
||||
|
||||
|
||||
class JobManager:
|
||||
def __init__(self) -> None:
|
||||
self._jobs: dict[str, Job] = {}
|
||||
|
||||
def create(self, message: str) -> Job:
|
||||
job_id = uuid.uuid4().hex[:12]
|
||||
job = Job(id=job_id, message=message)
|
||||
self._jobs[job_id] = job
|
||||
return job
|
||||
|
||||
def get(self, job_id: str) -> Job | None:
|
||||
return self._jobs.get(job_id)
|
||||
|
||||
def set_status(self, job_id: str, status: JobStatus) -> None:
|
||||
job = self._jobs.get(job_id)
|
||||
if job:
|
||||
job.status = status
|
||||
|
||||
def cancel(self, job_id: str) -> bool:
|
||||
job = self._jobs.get(job_id)
|
||||
if not job:
|
||||
return False
|
||||
if job.status in (JobStatus.completed, JobStatus.failed, JobStatus.cancelled):
|
||||
return False
|
||||
job.status = JobStatus.cancelled
|
||||
if job.task and not job.task.done():
|
||||
job.task.cancel()
|
||||
return True
|
||||
|
||||
def attach_task(self, job_id: str, task: asyncio.Task) -> None:
|
||||
job = self._jobs.get(job_id)
|
||||
if job:
|
||||
job.task = task
|
||||
|
||||
|
||||
job_manager = JobManager()
|
||||
Reference in New Issue
Block a user