mobiledev/taskflow
TaskFlow, a team task board you run from the web or from Claude and ChatGPT. FastAPI + DataK3 + appid + Models + MCP.
A remix template · 0 remixes · Remix mobiledev/taskflow
app/taskflow/infrastructure/dodil.py
"""The Dodil side: a service-account token, DataK3 on the Postgres wire, and Dodil Models.
The remix creates the service account and puts its id and secret into the repo's CI secrets;
.dodil/deploy.yaml hands them to the app as environment variables. Nothing secret is in git.
"""
import os
import threading
import time
import httpx
import psycopg
TOKEN_URL = "https://id.dodil.io/realms/dodil/protocol/openid-connect/token"
PG_HOST = os.environ.get("PG_HOST", "pg.uk-lon-1.dodil.io")
MODELS_BASE = os.environ.get("MODELS_BASE", "https://api.dodil.io/v1").rstrip("/")
CHAT_MODEL = os.environ.get("CHAT_MODEL", "kimi-k2.6")
EMBED_MODEL = os.environ.get("EMBED_MODEL", "jina-embeddings-v4")
_lock, _tok = threading.Lock(), {"v": None, "exp": 0.0}
def token() -> str:
"""A short-lived service-account token (client_credentials), cached until near expiry.
It is both the DataK3 password and the Models bearer."""
with _lock:
if _tok["v"] and time.time() < _tok["exp"] - 60:
return _tok["v"]
r = httpx.post(TOKEN_URL, timeout=15, data={
"grant_type": "client_credentials",
"client_id": os.environ["DODIL_SERVICE_ACCOUNT_ID"],
"client_secret": os.environ["DODIL_SERVICE_ACCOUNT_SECRET"]})
r.raise_for_status()
d = r.json()
_tok.update(v=d["access_token"], exp=time.time() + float(d.get("expires_in", 300)))
return _tok["v"]
def connect() -> psycopg.Connection:
"""DataK3 speaks Postgres: the database is the bucket, TLS is required."""
return psycopg.connect(host=PG_HOST, port=5432, dbname=os.environ["BUCKET"], user="token",
password=token(), sslmode="require", autocommit=True)
def _models(path: str, payload: dict) -> dict:
"""POST to Dodil Models (OpenAI-compatible). The gateway wraps the OpenAI body as
{"status": ..., "data": {...}}, so unwrap it first."""
r = httpx.post(f"{MODELS_BASE}{path}", timeout=120, json=payload,
headers={"Authorization": f"Bearer {token()}"})
r.raise_for_status()
body = r.json()
inner = body.get("data") if isinstance(body, dict) else None
return inner if isinstance(inner, dict) else body
def chat(prompt: str) -> str:
body = _models("/chat/completions", {"model": CHAT_MODEL,
"messages": [{"role": "user", "content": prompt}]})
return body["choices"][0]["message"]["content"] or ""
class ModelsAssistant:
def checklist(self, title: str, description: str) -> list:
text = chat("Break this task into a short checklist of 3 to 6 concrete steps. "
"One step per line, no numbering, nothing else.\n\n"
f"Task: {title}\nDetails: {description or '-'}")
return [line.strip("-*•0123456789. ").strip()
for line in text.splitlines() if line.strip()][:8]
def summarize(self, facts: str) -> str:
return chat("Write a short daily summary of this task board for the team: 3 to 5 plain "
"sentences, overdue work first, no invented facts.\n\n" + facts).strip()
def embed(self, texts: list) -> list:
body = _models("/embeddings", {"model": EMBED_MODEL, "input": texts})
rows = sorted(body["data"], key=lambda row: row.get("index", 0))
return [row["embedding"] for row in rows]