Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ def get_chatkit_server() -> FactAssistantServer:
return _chatkit_server


@app.post("/chatkit")
async def chatkit_endpoint(
request: Request, server: FactAssistantServer = Depends(get_chatkit_server)
) -> Response:
payload = await request.body()
result = await server.process(payload, {"request": request})
if isinstance(result, StreamingResult):
return StreamingResponse(result, media_type="text/event-stream")
if hasattr(result, "json"):
return Response(content=result.json, media_type="application/json")
return JSONResponse(result)


@app.get("/facts")
async def list_facts() -> dict[str, Any]:
Expand All @@ -70,3 +58,49 @@ async def discard_fact(fact_id: str) -> dict[str, Any]:
@app.get("/health")
async def health_check() -> dict[str, str]:
return {"status": "healthy"}
# --- ADD: ChatKit session endpoint (no-code friendly) ---
import os, httpx
from fastapi import APIRouter
from fastapi.responses import JSONResponse

router = APIRouter()
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
WORKFLOW_ID = os.environ["WORKFLOW_ID"]

@router.post("/chatkit")
async def create_chatkit_session():
"""
ChatKit frontend bu endpoint'e POST eder.
Biz de OpenAI'den session oluşturup client_secret'i geri döneriz.
"""
url = "https://api.openai.com/v1/chatkit/sessions"
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json",
}
# 'user' serbest bir son-kullanıcı kimliği. Kullanıcı başına sabit bir değer verebilirsin.
payload = {
"user": "web-user-1",
"workflow": { "id": WORKFLOW_ID }, # Agent Builder'dan aldığın ID
# İstersen burada chatkit_configuration / history / file_upload vb. opsiyonları açabilirsin.
}
async with httpx.AsyncClient(timeout=30) as client:
r = await client.post(url, headers=headers, json=payload)
data = r.json()
# ChatKit'in beklediği alan: client_secret
# (frontend bu secret'ı alıp widget'ı başlatır)
return JSONResponse(
{
"client_secret": data.get("client_secret"),
"expires_at": data.get("expires_at"),
},
status_code=r.status_code,
)

# Mevcut FastAPI app'ine router'ı bağla:
# app.include_router(router) satırı zaten varsa gerek yok; yoksa ekleyin:
try:
app.include_router(router)
except Exception:
pass
# --- /ADD ---