@@ -0,0 +1,500 @@
""" Single-process R1 API. Store is authoritative; no demo state enters this module. """
from __future__ import annotations
import asyncio
import json
import os
import sqlite3
import time
from contextlib import asynccontextmanager , suppress
from dataclasses import asdict
from datetime import date , datetime
from pathlib import Path
from typing import Literal
from uuid import UUID
from zoneinfo import ZoneInfo
import yaml
from anyio import CancelScope
from fastapi import FastAPI , HTTPException , Query , Request
from fastapi . exceptions import RequestValidationError
from fastapi . responses import JSONResponse , StreamingResponse
from fastapi . staticfiles import StaticFiles
from pydantic import BaseModel , ConfigDict , field_validator
from . . config import ModelsConfig
from . . contracts import LLMMessage
from . . core . bazi import (
GAN_ELEM ,
compile_pillars ,
element_scores ,
pillars_from_lunar ,
ten_god_distribution ,
)
from . . memory . store import Store
from . . think . scenes import Scene
from . . think . wiring import build_router_from_config
HOURS = [
" 子时 " ,
" 丑时 " ,
" 寅时 " ,
" 卯时 " ,
" 辰时 " ,
" 巳时 " ,
" 午时 " ,
" 未时 " ,
" 申时 " ,
" 酉时 " ,
" 戌时 " ,
" 亥时 " ,
]
ERROR = " 她这会儿没接上话,稍后再试试。你写的话还在。 "
def safe_text ( value : str , maximum : int , label : str , required : bool = False ) - > str :
value = value . strip ( )
if (
( required and not value )
or len ( value ) > maximum
or any ( ord ( c ) < 32 or ord ( c ) == 127 or c in " <> " for c in value )
) :
raise ValueError ( f " { label } 请填写 { 1 if required else 0 } – { maximum } 字,不含控制字符或尖括号。 " )
return value
class StrictBody ( BaseModel ) :
model_config = ConfigDict ( extra = " forbid " , strict = True )
class BirthInput ( StrictBody ) :
name : str
birth : str
hour : str
@field_validator ( " name " )
@classmethod
def name_valid ( cls , value : str ) - > str :
return safe_text ( value , 20 , " 名字 " , True )
@field_validator ( " birth " )
@classmethod
def birth_valid ( cls , value : str ) - > str :
try :
born = date . fromisoformat ( value )
today = datetime . now ( ZoneInfo ( " Asia/Shanghai " ) ) . date ( )
age = today . year - born . year - ( ( today . month , today . day ) < ( born . month , born . day ) )
if born . isoformat ( ) != value or born . year < 1900 or age < 18 :
raise ValueError
except ValueError as exc :
raise ValueError ( " 请填写有效的出生日期,且已满18岁(不早于1900年)。 " ) from exc
return value
@field_validator ( " hour " )
@classmethod
def hour_valid ( cls , value : str ) - > str :
if value not in HOURS :
raise ValueError ( " 请选择有效的出生时辰。 " )
return value
class RelationInput ( StrictBody ) :
relation : Literal [ " 朋友 " , " 恋人 " ]
nickname : str = " "
boundary : str = " 尊重彼此的节奏 "
@field_validator ( " nickname " )
@classmethod
def nickname_valid ( cls , value : str ) - > str :
return safe_text ( value , 40 , " 称呼 " )
@field_validator ( " boundary " )
@classmethod
def boundary_valid ( cls , value : str ) - > str :
return safe_text ( value , 2000 , " 相处边界 " )
class CreationInput ( BirthInput , RelationInput ) :
pass
class ChatInput ( StrictBody ) :
text : str
turn_id : str
epoch : int
@field_validator ( " text " )
@classmethod
def text_valid ( cls , value : str ) - > str :
if not value . strip ( ) or len ( value ) > 4000 :
raise ValueError ( " 请输入1– 4000字的消息。 " )
return value . strip ( )
@field_validator ( " turn_id " )
@classmethod
def turn_valid ( cls , value : str ) - > str :
try :
UUID ( value )
except ValueError as exc :
raise ValueError ( " 轮次标识无效,请刷新后重试。 " ) from exc
return value
def compile_birth ( data : BirthInput ) - > tuple [ dict , object , object ] :
born = date . fromisoformat ( data . birth )
pillars = pillars_from_lunar ( born . year , born . month , born . day , HOURS . index ( data . hour ) * 2 )
derived = compile_pillars ( pillars )
return { " pillars " : asdict ( pillars ) , " traits " : derived . traits_text ( ) } , pillars , derived
def create_app (
* ,
db_path : Path | str | None = None ,
models : ModelsConfig | None = None ,
router = None ,
web_dir : Path | None = None ,
stream_timeout : float = 60 ,
) - > FastAPI :
""" Dependencies can be injected for isolated fault/cancellation tests, never via browser flags. """
db_path = db_path or os . environ . get (
" CYBERGRIL_DB " , str ( Path . home ( ) / " .local/share/cybergril/companion.db " )
)
if models is None :
config = Path ( os . environ . get ( " CYBERGRIL_MODELS " , " config/models.yaml " ) )
models = ModelsConfig ( * * yaml . safe_load ( config . read_text ( ) ) )
router = router or build_router_from_config ( models )
@asynccontextmanager
async def lifespan ( app ) :
store = Store ( db_path )
app . state . store = store
with store . conn :
store . execute (
" INSERT INTO api_meta(key,value) VALUES( ' epoch ' ,1) ON CONFLICT(key) DO UPDATE SET value=value+1 "
)
store . execute ( " UPDATE chat_message SET status= ' cancelled ' WHERE status= ' pending ' " )
app . state . epoch = store . query_one ( " SELECT value FROM api_meta WHERE key= ' epoch ' " ) [ " value " ]
app . state . active = None
app . state . chat_lock = asyncio . Lock ( )
app . state . health_cache = ( 0 , False )
app . state . health_lock = asyncio . Lock ( )
try :
yield
finally :
if app . state . active :
app . state . active [ " cancel " ] . set ( )
await app . state . active [ " finished " ] . wait ( )
for provider in set ( router . provider_for ( t ) for t in router . available_tiers ( ) ) :
await provider . aclose ( )
store . close ( )
app = FastAPI ( lifespan = lifespan , docs_url = None , redoc_url = None )
@app.exception_handler ( RequestValidationError )
async def invalid ( _request , exc ) :
fields = { }
for e in exc . errors ( ) :
key = str ( e [ " loc " ] [ - 1 ] )
msg = e [ " msg " ] . removeprefix ( " Value error, " )
if e [ " type " ] == " missing " :
msg = " 请填写此项。 " if key != " relation " else " 请亲自选择你们的关系。 "
elif e [ " type " ] == " literal_error " :
msg = " 请亲自选择朋友或恋人。 "
elif e [ " type " ] == " extra_forbidden " :
msg = " 此字段不允许修改。 "
elif not any ( " \u4e00 " < = c < = " \u9fff " for c in msg ) :
msg = " 字段格式无效,请检查后重试。 "
fields [ key ] = msg
return JSONResponse ( { " detail " : " 请检查填写内容。 " , " fields " : fields } , status_code = 422 )
def companion ( ) :
row = app . state . store . query_one ( " SELECT identity_json FROM companion WHERE id=1 " )
if not row :
raise HTTPException ( 404 , " 还没有创建真实伴侣。 " )
return json . loads ( row [ " identity_json " ] )
@app.get ( " /api/health " )
async def health ( ) :
async with app . state . health_lock :
checked , ok = app . state . health_cache
if time . monotonic ( ) - checked > 30 :
provider = router . route ( Scene . DIALOGUE_MAIN ) . provider
try :
async with asyncio . timeout ( 5 ) :
ok = bool ( provider and ( await provider . health ( ) ) . ok )
except Exception :
ok = False
app . state . health_cache = ( time . monotonic ( ) , ok )
return {
" ok " : True ,
" version " : " R1 " ,
" epoch " : app . state . epoch ,
" companion_created " : bool ( app . state . store . query_one ( " SELECT id FROM companion " ) ) ,
" llm " : { " ok " : ok , " model " : models . tiers [ " high " ] . model } ,
" capabilities " : {
" text " : True ,
* * dict . fromkeys (
[ " life " , " memory " , " voice " , " vision " , " weather " , " materials " , " backup " ] , False
) ,
} ,
}
@app.get ( " /api/companion " )
async def get_companion ( ) :
return companion ( )
@app.post ( " /api/companion/preview " )
async def preview ( data : BirthInput ) :
return compile_birth ( data ) [ 0 ]
@app.post ( " /api/companion " , status_code = 201 )
async def create ( data : CreationInput ) :
store = app . state . store
if store . query_one ( " SELECT id FROM companion " ) :
raise HTTPException ( 409 , " 已经有一位她了,请重新打开以恢复档案。 " )
color , pillars , derived = compile_birth ( data )
identity = { " id " : " 1 " , * * data . model_dump ( ) , " color " : color }
try :
with store . conn :
store . execute (
" INSERT INTO companion VALUES(1,?) " , ( json . dumps ( identity , ensure_ascii = False ) , )
)
store . execute (
""" INSERT INTO bazi_core
(id,birth_input,pillars,day_master,day_master_elem,ten_gods,element_scores,strength,derived,created_at)
VALUES(1,?,?,?,?,?,?,?,?,?) """ ,
(
json . dumps ( { " birth " : data . birth , " hour " : data . hour , " tz " : " Asia/Shanghai " } ) ,
json . dumps ( asdict ( pillars ) , ensure_ascii = False ) ,
pillars . day [ 0 ] ,
GAN_ELEM [ pillars . day [ 0 ] ] ,
json . dumps ( ten_god_distribution ( pillars ) ) ,
json . dumps ( element_scores ( pillars ) ) ,
derived . strength ,
json . dumps ( derived . as_dict ( ) ) ,
int ( time . time ( ) * 1000 ) ,
) ,
)
except sqlite3 . IntegrityError as exc :
raise HTTPException ( 409 , " 已经有一位她了,请重新打开以恢复档案。 " ) from exc
return identity
@app.patch ( " /api/relation " )
async def relation ( data : RelationInput ) :
identity = companion ( )
identity . update ( data . model_dump ( ) )
with app . state . store . conn :
app . state . store . execute (
" UPDATE companion SET identity_json=? WHERE id=1 " ,
( json . dumps ( identity , ensure_ascii = False ) , ) ,
)
return identity
@app.get ( " /api/messages " )
async def messages (
limit : int = Query ( 200 , ge = 1 , le = 200 ) , before : int | None = Query ( None , ge = 1 )
) :
companion ( )
rows = app . state . store . query (
" SELECT * FROM chat_message WHERE id<? ORDER BY id DESC LIMIT ? " ,
( before or 9223372036854775807 , limit + 1 ) ,
)
more = len ( rows ) > limit
rows = rows [ : limit ]
return {
" messages " : [ dict ( r ) for r in reversed ( rows ) ] ,
" next_before " : rows [ - 1 ] [ " id " ] if more else None ,
" epoch " : app . state . epoch ,
}
def event ( data , kind , * * payload ) :
return (
" data: "
+ json . dumps (
{ " t " : kind , " epoch " : app . state . epoch , " turn_id " : data . turn_id , * * payload } ,
ensure_ascii = False ,
)
+ " \n \n "
)
@app.post ( " /api/chat " )
async def chat ( data : ChatInput , request : Request ) :
identity = companion ( )
if data . epoch != app . state . epoch :
return JSONResponse (
{ " detail " : " 服务已重新启动,请同步后重试。 " , " epoch " : app . state . epoch } ,
status_code = 409 ,
)
store = app . state . store
# Serialize reservation, including replays. Concurrent retries of a completed
# turn must never call the model twice or race the unique message constraint.
async with app . state . chat_lock :
previous = store . query_one (
" SELECT * FROM chat_message WHERE turn_id=? AND role= ' user ' " , ( data . turn_id , )
)
if previous and previous [ " text " ] != data . text :
raise HTTPException ( 409 , " 相同轮次不能改写消息。 " )
if app . state . active :
active = app . state . active
if active [ " turn_id " ] != data . turn_id :
active [ " cancel " ] . set ( )
await active [ " finished " ] . wait ( )
saved = store . query_one (
" SELECT text FROM chat_message WHERE turn_id=? AND role= ' companion ' AND status= ' sent ' " ,
( data . turn_id , ) ,
)
if saved :
async def replay ( ) :
yield event (
data , " chat.final " , utterance = { " text " : saved [ " text " ] , " speech_tags " : [ ] }
)
return StreamingResponse ( replay ( ) , media_type = " text/event-stream " )
active = {
" cancel " : asyncio . Event ( ) ,
" finished " : asyncio . Event ( ) ,
" turn_id " : data . turn_id ,
}
app . state . active = active
with store . conn :
store . execute (
" INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES(?, ' user ' ,?,?, ' pending ' ) ON CONFLICT(turn_id,role) DO UPDATE SET status= ' pending ' " ,
( int ( time . time ( ) * 1000 ) , data . text , data . turn_id ) ,
)
turns = store . query ( """ SELECT u.text AS user_text, a.text AS assistant_text
FROM chat_message a JOIN chat_message u ON u.turn_id=a.turn_id AND u.role= ' user '
WHERE a.role= ' companion ' AND a.status= ' sent ' AND u.status= ' sent '
ORDER BY a.id DESC LIMIT 20 """ )
system = (
f " 你叫 { identity [ ' name ' ] } ,与对方是 { identity [ ' relation ' ] } 。称呼对方: { identity [ ' nickname ' ] or ' 你 ' } 。 "
f " 相处边界: { identity [ ' boundary ' ] } 。出生底色仅供措辞参考: { identity [ ' color ' ] [ ' traits ' ] } 。 "
" 用自然简短的中文交流。只有信息能穿过两个世界,不能触碰或给予实物。 "
" 当前只接通文字与近轮对话历史;生活循环、长期记忆、主动消息、语音、视觉、天气都未接通。 "
" 不得编造自己经历过的生活事件,不声称后台一直在生活或记忆已运行。 "
" 不窥探用户未分享的信息,不以缺席、孤独或终止要挟对方,不输出内部数值或思考过程。 "
)
context = (
[ LLMMessage ( role = " system " , content = system ) ]
+ [
message
for turn in reversed ( turns )
for message in (
LLMMessage ( role = " user " , content = turn [ " user_text " ] ) ,
LLMMessage ( role = " assistant " , content = turn [ " assistant_text " ] ) ,
)
]
+ [ LLMMessage ( role = " user " , content = data . text ) ]
)
async def generate ( ) :
completed = False
status , text , upstream , pending , cancelling = " cancelled " , " " , None , None , None
usage = { }
try :
decision = router . route (
Scene . DIALOGUE_MAIN ,
estimated_tokens = sum ( len ( m . content ) for m in context ) + 1024 ,
)
if not decision . provider :
raise RuntimeError ( " No dialogue provider " )
history_budget = max ( 0 , decision . context_budget - len ( system ) - len ( data . text ) )
recent = [ ]
# Order by completed turns, not adjacent message ids: a retried user
# may have an older id than messages sent while that turn had failed.
for turn in turns :
size = len ( turn [ " user_text " ] ) + len ( turn [ " assistant_text " ] )
if size > history_budget :
break
recent = [
LLMMessage ( role = " user " , content = turn [ " user_text " ] ) ,
LLMMessage ( role = " assistant " , content = turn [ " assistant_text " ] ) ,
] + recent
history_budget - = size
context [ : ] = [ context [ 0 ] ] + recent + [ context [ - 1 ] ]
cfg = models . tiers [ decision . effective . value ]
upstream = decision . provider . stream (
context , temperature = cfg . temperature , max_tokens = cfg . max_tokens or 1024
)
cancelling = asyncio . create_task ( active [ " cancel " ] . wait ( ) )
async with asyncio . timeout ( stream_timeout ) :
while True :
pending = asyncio . create_task ( anext ( upstream ) )
done , _ = await asyncio . wait (
[ pending , cancelling ] , return_when = asyncio . FIRST_COMPLETED
)
if cancelling in done or await request . is_disconnected ( ) :
return
try :
delta = pending . result ( )
except StopAsyncIteration :
break
if delta . usage :
usage = delta . usage
if delta . text :
text + = delta . text
yield event ( data , " chat.delta " , delta = delta . text )
if delta . done or delta . finish_reason :
completed = True
if delta . done :
break
if active [ " cancel " ] . is_set ( ) or await request . is_disconnected ( ) :
return
if not text . strip ( ) or not completed :
raise RuntimeError ( " Empty upstream response " )
with store . conn :
store . execute (
" INSERT INTO chat_message(ts,role,text,turn_id,status) VALUES(?, ' companion ' ,?,?, ' sent ' ) " ,
( int ( time . time ( ) * 1000 ) , text , data . turn_id ) ,
)
store . execute (
" UPDATE chat_message SET status= ' sent ' WHERE turn_id=? " , ( data . turn_id , )
)
status = " sent "
yield event ( data , " chat.final " , utterance = { " text " : text , " speech_tags " : [ ] } )
except asyncio . CancelledError :
raise
except Exception :
status = " failed "
yield event ( data , " chat.error " , phi = ERROR , retryable = True )
finally :
with CancelScope ( shield = True ) :
# Cancel the in-flight __anext__ before closing the async generator.
for task in ( pending , cancelling ) :
if task is not None :
task . cancel ( )
with suppress ( asyncio . CancelledError , StopAsyncIteration , Exception ) :
await task
if upstream is not None :
with suppress ( Exception ) :
await upstream . aclose ( )
try :
if status != " sent " :
with store . conn :
store . execute (
" UPDATE chat_message SET status=? WHERE turn_id=? AND role= ' user ' " ,
( status , data . turn_id ) ,
)
if usage :
router . guard . record_usage ( usage , Scene . DIALOGUE_MAIN . value )
else :
router . guard . record (
sum ( len ( m . content ) for m in context ) + len ( text ) ,
Scene . DIALOGUE_MAIN . value ,
)
finally :
if app . state . active is active :
app . state . active = None
active [ " finished " ] . set ( )
return StreamingResponse (
generate ( ) ,
media_type = " text/event-stream " ,
headers = { " Cache-Control " : " no-cache " , " X-Accel-Buffering " : " no " } ,
)
static = web_dir or Path ( os . environ . get ( " CYBERGRIL_WEB " , " web/dist " ) )
if static . is_dir ( ) :
app . mount ( " / " , StaticFiles ( directory = static , html = True ) , name = " web " )
return app