smriti/cli/smriti_cli/client.py
Himanshu Dongre 6028dacff1 Extract checkpoint fields from freeform markdown via LLM
Round 3 of the dogfood confirmed that every multi-branch CLI command
works end-to-end, but the single biggest remaining friction is still
checkpoint payload construction. Each agent hands off a ~15-18 KB
markdown document; turning that into the Smriti schema (decisions,
assumptions, tasks, open_questions, entities, artifacts) is three
minutes of hand-written JSON per checkpoint and adds no product value.

This build adds an LLM-powered extractor that collapses that work into
one pipe command:

  cat /tmp/r3_agent_a_output.md | smriti checkpoint create my-project \
      --extract --author-agent codex-A

The CLI reads stdin as freeform markdown, calls the new
POST /api/v5/checkpoint/extract endpoint, maps the returned fields
into a commit payload, and writes the checkpoint. --dry-run prints the
extracted payload without committing so users can review first.
--extract and --from-json are mutually exclusive.

Backend architecture mirrors the existing review endpoint: stateless
LLM call (no session or commit ID required), uses the same background
intelligence provider (cfg.background.provider / cfg.background.model)
as draft and review, same JSON-mode prompt shape, same 502-on-parse-
error pattern. The extractor endpoint differs in one small way: it
passes allow_mock=True to get_adapter so unconfigured test envs fall
back to MockAdapter without raising 500. Production envs always have
a real provider configured and never hit this fallback.

The extractor is the first LLM-backed endpoint that gets tested
against a real mock response. To make that work, MockAdapter.send now
detects response_format={"type": "json_object"} in kwargs and returns
a canned JSON blob covering every field any current Smriti endpoint
looks for (title, objective, summary, decisions, assumptions, tasks,
open_questions, entities, artifacts, issues, suggestions). Existing
chat.send text-mode tests are unaffected because they don't pass
response_format. This also unblocks future tests for draft and review.

Manual verification against a real OpenAI provider: piped a realistic
23-line handoff markdown with 4 decisions, 3 assumptions, 3 tasks,
2 open questions, and a python code block. The extractor returned
exactly those items in the right fields (4/3/3/2/1) and produced a
valid checkpoint with all fields populated. Round 4's load-bearing
claim — zero hand-written JSON per checkpoint — is now achievable.

153/153 backend tests pass (149 pre-existing + 4 new extract tests).
2026-04-11 19:15:03 +05:30

224 lines
8.6 KiB
Python

"""Thin HTTP wrapper over the Smriti REST API.
No fancy features. Each method maps to a single endpoint. The CLI layer on
top composes these into user-facing commands and formats output.
All methods raise SmritiError on any non-2xx response. Callers should let
these bubble up to main() which turns them into clean exit codes.
"""
from __future__ import annotations
import os
from typing import Any
import requests
DEFAULT_API_URL = "http://localhost:8000"
class SmritiError(Exception):
"""Raised for any API error or unreachable backend."""
def __init__(self, message: str, status: int | None = None, detail: Any = None):
super().__init__(message)
self.status = status
self.detail = detail
class SmritiClient:
def __init__(self, base_url: str | None = None, timeout: float = 60.0):
self.base_url = (base_url or os.environ.get("SMRITI_API_URL") or DEFAULT_API_URL).rstrip("/")
self.timeout = timeout
self._session = requests.Session()
# ── internals ──────────────────────────────────────────────────────────
def _request(self, method: str, path: str, *, json: Any = None, params: dict | None = None) -> Any:
url = f"{self.base_url}{path}"
try:
resp = self._session.request(
method=method,
url=url,
json=json,
params=params,
timeout=self.timeout,
)
except requests.ConnectionError as e:
raise SmritiError(
f"Could not reach Smriti at {self.base_url}. "
f"Is the backend running? ({e})"
)
except requests.Timeout:
raise SmritiError(f"Request to {url} timed out after {self.timeout}s")
if not resp.ok:
detail_obj: Any = None
message: str
try:
detail_obj = resp.json().get("detail")
except Exception:
detail_obj = None
message = resp.text[:200]
else:
if isinstance(detail_obj, str):
message = detail_obj
elif isinstance(detail_obj, dict) and "message" in detail_obj:
message = str(detail_obj.get("message"))
else:
message = str(detail_obj) if detail_obj is not None else ""
raise SmritiError(
f"{method} {path} failed: HTTP {resp.status_code}{message}",
status=resp.status_code,
detail=detail_obj,
)
if resp.status_code == 204 or not resp.content:
return None
return resp.json()
# ── spaces (V2) ────────────────────────────────────────────────────────
def list_spaces(self) -> list[dict]:
return self._request("GET", "/api/v2/repos")
def get_space(self, space_id: str) -> dict:
return self._request("GET", f"/api/v2/repos/{space_id}")
def create_space(self, name: str, description: str = "") -> dict:
return self._request(
"POST",
"/api/v2/repos",
json={"name": name, "description": description},
)
def delete_space(self, space_id: str) -> None:
self._request("DELETE", f"/api/v2/repos/{space_id}")
def resolve_space(self, name_or_id: str) -> dict:
"""Look up a space by UUID or by name. Returns the full space dict.
Tries UUID lookup first; falls back to scanning the space list and
matching on name (case-sensitive exact match, then case-insensitive).
"""
# UUID-ish shape
if len(name_or_id) == 36 and name_or_id.count("-") == 4:
try:
return self.get_space(name_or_id)
except SmritiError as e:
if e.status != 404:
raise
# fall through to name lookup
spaces = self.list_spaces()
exact = [s for s in spaces if s["name"] == name_or_id]
if len(exact) == 1:
return exact[0]
if len(exact) > 1:
raise SmritiError(
f"Multiple spaces named '{name_or_id}'. Use the UUID to disambiguate."
)
lower = name_or_id.lower()
case_insensitive = [s for s in spaces if s["name"].lower() == lower]
if len(case_insensitive) == 1:
return case_insensitive[0]
if len(case_insensitive) > 1:
raise SmritiError(
f"Multiple spaces matching '{name_or_id}' (case-insensitive). "
f"Use the UUID to disambiguate."
)
raise SmritiError(f"No space found matching '{name_or_id}'")
# ── checkpoints ────────────────────────────────────────────────────────
def get_commit(self, commit_id: str) -> dict:
return self._request("GET", f"/api/v2/commits/{commit_id}")
def list_commits(self, space_id: str, branch: str | None = None) -> list[dict]:
params = {"branch": branch} if branch else None
return self._request("GET", f"/api/v2/repos/{space_id}/commits", params=params)
def get_head(self, space_id: str) -> dict:
return self._request("GET", f"/api/v4/chat/spaces/{space_id}/head")
def create_chat_commit(self, payload: dict) -> dict:
"""Create a checkpoint via the V4 commit endpoint (full schema).
Requires session_id. The CLI handles session creation if the caller
does not supply one.
"""
return self._request("POST", "/api/v4/chat/commit", json=payload)
def review_checkpoint(self, commit_id: str) -> dict:
return self._request("POST", f"/api/v5/checkpoint/{commit_id}/review")
def extract_checkpoint_content(self, content: str, use_mock: bool = False) -> dict:
"""POST /api/v5/checkpoint/extract
Sends a freeform markdown document to the extractor endpoint and
returns the structured checkpoint fields (title, objective, summary,
decisions, assumptions, tasks, open_questions, entities, artifacts).
Used by `smriti checkpoint create --extract` to build a commit
payload from agent output without hand-authoring JSON.
"""
return self._request(
"POST",
"/api/v5/checkpoint/extract",
json={"content": content, "use_mock": use_mock},
)
def compare_checkpoints(self, checkpoint_a_id: str, checkpoint_b_id: str) -> dict:
"""GET /api/v5/lineage/checkpoints/{a}/compare/{b}
Returns the full CompareResponse dict with `checkpoint_a`,
`checkpoint_b`, and `diff` (including the new
`common_ancestor_commit_id` field and normalized shared sets).
"""
return self._request(
"GET",
f"/api/v5/lineage/checkpoints/{checkpoint_a_id}/compare/{checkpoint_b_id}",
)
def fork_session(
self,
space_id: str,
checkpoint_id: str,
branch_name: str = "",
) -> dict:
"""POST /api/v5/lineage/sessions/fork
Returns the ForkSessionResponse: session_id, branch_name,
forked_from_checkpoint_id, history_base_seq.
"""
payload: dict = {
"space_id": space_id,
"checkpoint_id": checkpoint_id,
}
if branch_name:
payload["branch_name"] = branch_name
return self._request("POST", "/api/v5/lineage/sessions/fork", json=payload)
def delete_commit(self, commit_id: str, cascade: bool = False) -> None:
params = {"cascade": "true"} if cascade else None
self._request("DELETE", f"/api/v2/commits/{commit_id}", params=params)
def delete_session(self, session_id: str) -> None:
self._request("DELETE", f"/api/v4/chat/sessions/{session_id}")
# ── sessions (used internally by CLI) ──────────────────────────────────
def create_session(self, repo_id: str, title: str = "", provider: str = "anthropic", model: str = "claude-sonnet-4-6") -> dict:
return self._request(
"POST",
"/api/v4/chat/sessions",
json={
"repo_id": repo_id,
"title": title or "agent-session",
"provider": provider,
"model": model,
"seed_from": "none",
},
)