Repository navigation
Ground cleaned-up quotes; add a TypeScript tool-calling diagram agent #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """Client for the TypeScript diagram agent (``reader/app/api/diagram``). | ||
|
|
||
| The agent draws a ``concept_diagram`` by calling tools that validate each step as it | ||
| is taken, so a mistake costs one more turn of the same conversation rather than a | ||
| whole regeneration. This module only asks it for parameters; everything after that | ||
| (the template's validation, beats, Gate 1) is the same path one-shot codegen takes. | ||
|
|
||
| It never fails a scene. Not configured, out of time, unreachable, or no accepted | ||
| diagram: it returns None and codegen falls back to the one-shot call. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from arcvisual.config import settings | ||
| from arcvisual.storyboard import Storyboard, VisualOpportunity | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| #: Below this, the agent cannot finish a turn on the free endpoint (~15s each). | ||
| MIN_BUDGET_S = 30.0 | ||
| #: Never ask for more: the route caps itself at 250s inside a 300s function. | ||
| MAX_BUDGET_S = 240.0 | ||
|
|
||
|
|
||
| @dataclass | ||
| class AgentDiagram: | ||
| params: dict[str, Any] | ||
| captions: list[str] | ||
| turns: int | ||
| model: str | ||
| notes: list[str] = field(default_factory=list) | ||
|
|
||
|
|
||
| def configured() -> bool: | ||
| cfg = settings() | ||
| return bool(cfg.diagram_agent_url and cfg.internal_secret) | ||
|
|
||
|
|
||
| def draw( | ||
| sb: Storyboard, | ||
| opportunity: VisualOpportunity, | ||
| *, | ||
| budget_s: float, | ||
| client: Any = None, | ||
| ) -> AgentDiagram | None: | ||
| """Ask the agent for one diagram's parameters, or None to fall back.""" | ||
| cfg = settings() | ||
| if not configured(): | ||
| return None | ||
| budget_s = min(budget_s, MAX_BUDGET_S) | ||
| if budget_s < MIN_BUDGET_S: | ||
| log.info("diagram agent skipped: only %.0fs left", budget_s) | ||
| return None | ||
|
|
||
| section = sb.section(opportunity.span.section_id) | ||
| concept = sb.concept(opportunity.concept_id) | ||
| payload = { | ||
| "title": sb.paper.title, | ||
| "claim": opportunity.claim, | ||
| "concept": f"{concept.name} — {concept.statement}" if concept else "", | ||
| "quote": opportunity.span.resolve(section), | ||
| "section": section.raw[:12000], | ||
| "equations": [e.latex for e in sb.equations if e.span.section_id == section.id][ | ||
| :6 | ||
| ], | ||
| "budget_s": round(budget_s), | ||
| } | ||
| headers = {"x-arcvisual-key": cfg.internal_secret} | ||
| try: | ||
| if client is None: | ||
| import httpx | ||
|
|
||
| # The agent bounds itself by budget_s; this only catches a hung network. | ||
| with httpx.Client(timeout=budget_s + 15) as http: | ||
| response = http.post(cfg.diagram_agent_url, json=payload, headers=headers) | ||
| else: | ||
| response = client.post(cfg.diagram_agent_url, json=payload, headers=headers) | ||
| except Exception as exc: # network trouble is a fallback, never a failed scene | ||
| log.warning("diagram agent unreachable: %s", exc) | ||
| return None | ||
| if response.status_code != 200: | ||
| log.warning( | ||
| "diagram agent answered %s: %s", response.status_code, response.text[:200] | ||
| ) | ||
| return None | ||
| try: | ||
| body = response.json() | ||
| except ValueError: | ||
| return None | ||
| if not body.get("ok") or not isinstance(body.get("params"), dict): | ||
| log.info("diagram agent gave no diagram: %s", "; ".join(body.get("notes") or [])) | ||
| return None | ||
| return AgentDiagram( | ||
| params=body["params"], | ||
| captions=[c for c in body.get("captions") or [] if isinstance(c, str)], | ||
| turns=int(body.get("turns") or 0), | ||
| model=str(body.get("model") or ""), | ||
| notes=[n for n in body.get("notes") or [] if isinstance(n, str)], | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the default free endpoint is exhausted or times out, the agent can fall through to paid models, but this reports zero cost and discards the token usage returned by the route.
build_sceneaddsGenerateResult.cost_usdto its spend before checking the per-scene ceiling, so these paid turns are invisible to both the ceiling and the job total. Price the agent usage or prevent unpriced paid fallbacks.Useful? React with 👍 / 👎.