"""Routes behind the dashboard's run page and trace panel, mounted under /api/app by app_api.py: full-text search over a run's traces, a trace's sampled tokens with their logprobs, the system prompt its attempts ran with, and the run's reports. Search results, tokens and prompts are transcript text, so they follow the transcript rules (app_api.transcript_reader): a BenchFlow Fireworks run's are readable by BenchFlow editors only; a public run's by anyone, since its source published its rollouts. A run's list of reports is public; a report's text follows the same rule as its transcripts. """ from fastapi import APIRouter, HTTPException, Query, Request from fastapi.responses import PlainTextResponse import fwruns router = APIRouter() Q = Query('', max_length=200, description='The text to find: one phrase, case-insensitive, at least 2 characters') def names(*values): if not fwruns.safe(*values): raise HTTPException(404, 'No such record.') def reader(request, name): import app_api # imported here: app_api imports this module app_api.transcript_reader(request, name) @router.get('/fwruns/{name}/search') def fw_search(name: str, request: Request, q: str = Q, limit: int = Query(200, ge=1, le=500)): """Every trace of a run whose transcript or verifier output contains q, in step order, with a snippet per match.""" names(name); reader(request, name) out = fwruns.search(name, q, limit) if out is None: raise HTTPException(404, 'No such run.') return out @router.get('/fwruns/{name}/traces/{attempt_id}/tokens') def fw_tokens(name: str, attempt_id: str, request: Request): """The attempt's sampled tokens with their sampling and inference logprobs (fw_sync.token_record).""" names(name, attempt_id); reader(request, name) t = fwruns.tokens(name, attempt_id) if not t: raise HTTPException(404, "This attempt has no token record: its model calls were not recorded with logprobs, or its records hold no token files.") return t @router.get('/fwruns/{name}/prompts/{key}') def fw_prompt(name: str, key: str, request: Request): """The system prompt and tool definitions the run's attempts ran with.""" names(name, key); reader(request, name) p = fwruns.prompt(name, key) if not p: raise HTTPException(404, 'No such prompt in this run.') return p @router.get('/fwruns/{name}/reports') def fw_reports(name: str): """The run's reports: file name, title and size of each markdown file under the run's reports/.""" names(name) return fwruns.reports(name) @router.get('/fwruns/{name}/reports/{file}', response_class=PlainTextResponse) def fw_report(name: str, file: str, request: Request): names(name, file); reader(request, name) text = fwruns.report(name, file) if text is None: raise HTTPException(404, 'No such report.') return PlainTextResponse(text, media_type='text/markdown; charset=utf-8')