quantology-modelmaker 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. modelmaker/__init__.py +2 -0
  2. modelmaker/api.py +1073 -0
  3. modelmaker/blocks/__init__.py +0 -0
  4. modelmaker/blocks/base.py +78 -0
  5. modelmaker/blocks/library.py +327 -0
  6. modelmaker/blocks/modelling.py +312 -0
  7. modelmaker/blocks/stat_tests.py +133 -0
  8. modelmaker/cache.py +44 -0
  9. modelmaker/compiler.py +478 -0
  10. modelmaker/graph.py +171 -0
  11. modelmaker/llm/__init__.py +28 -0
  12. modelmaker/llm/anthropic_provider.py +79 -0
  13. modelmaker/llm/base.py +85 -0
  14. modelmaker/llm/claude_cli_provider.py +90 -0
  15. modelmaker/llm/gemini_provider.py +138 -0
  16. modelmaker/llm/lmstudio_provider.py +140 -0
  17. modelmaker/llm/openai_provider.py +138 -0
  18. modelmaker/llm/prompts.py +189 -0
  19. modelmaker/llm/settings.py +47 -0
  20. modelmaker/llm/stub_provider.py +29 -0
  21. modelmaker/metadata_transforms.py +109 -0
  22. modelmaker/packet.py +111 -0
  23. modelmaker/project.py +129 -0
  24. modelmaker/run_worker.py +143 -0
  25. modelmaker/runner.py +1313 -0
  26. modelmaker/session.py +400 -0
  27. modelmaker/static/assets/index-B9cHjTMW.css +1 -0
  28. modelmaker/static/assets/index-EvjTc-1k.js +34 -0
  29. modelmaker/static/brand-quantology/favicon.svg +4 -0
  30. modelmaker/static/brand-quantology/icon-mark-light.svg +13 -0
  31. modelmaker/static/icons.svg +24 -0
  32. modelmaker/static/index.html +20 -0
  33. modelmaker/tui/__init__.py +0 -0
  34. modelmaker/tui/app.py +785 -0
  35. modelmaker/tui/canvas.py +388 -0
  36. modelmaker/tui/charts.py +84 -0
  37. modelmaker/tui/client.py +270 -0
  38. modelmaker/tui/inspector.py +285 -0
  39. modelmaker/tui/paramspecs.py +128 -0
  40. modelmaker/tui/preview.py +71 -0
  41. modelmaker/tui/screens.py +182 -0
  42. modelmaker/util.py +44 -0
  43. quantology_modelmaker-0.1.0.dist-info/METADATA +247 -0
  44. quantology_modelmaker-0.1.0.dist-info/RECORD +49 -0
  45. quantology_modelmaker-0.1.0.dist-info/WHEEL +5 -0
  46. quantology_modelmaker-0.1.0.dist-info/entry_points.txt +3 -0
  47. quantology_modelmaker-0.1.0.dist-info/licenses/LICENSE +204 -0
  48. quantology_modelmaker-0.1.0.dist-info/licenses/NOTICE +12 -0
  49. quantology_modelmaker-0.1.0.dist-info/top_level.txt +1 -0
modelmaker/api.py ADDED
@@ -0,0 +1,1073 @@
1
+ from __future__ import annotations
2
+
3
+ import functools
4
+ import inspect
5
+ import os
6
+ import threading
7
+ from dataclasses import asdict
8
+ from pathlib import Path
9
+ from typing import Any, Callable
10
+
11
+ from fastapi import FastAPI, HTTPException
12
+ from fastapi.middleware.cors import CORSMiddleware
13
+ from fastapi.responses import Response
14
+ from fastapi.staticfiles import StaticFiles
15
+ from pydantic import BaseModel
16
+
17
+ from . import blocks as _blocks_pkg # noqa: F401 -- populates BLOCK_REGISTRY
18
+ from .blocks import library as _library # noqa: F401
19
+ from .blocks import modelling as _modelling # noqa: F401
20
+ from .blocks import stat_tests as _stat_tests # noqa: F401
21
+ from .blocks.base import BLOCK_REGISTRY
22
+ from .compiler import CompileError, compile_graph
23
+ from .llm import ColumnInfo, DraftContext, LLM_PROVIDER_REGISTRY, LLMProvider, get_provider
24
+ from .llm import claude_cli_provider as _claude_cli_provider
25
+ from .llm import gemini_provider as _gemini_provider
26
+ from .llm import lmstudio_provider as _lmstudio_provider
27
+ from .llm import openai_provider as _openai_provider
28
+ from .llm.settings import LLMSettingsStore
29
+ from .packet import DataFramePacket
30
+ from .session import ProjectSession, wire_is_valid
31
+
32
+ try:
33
+ from .llm import anthropic_provider as _anthropic_provider
34
+ except ImportError:
35
+ _anthropic_provider = None
36
+
37
+ # Load a local .env file (if present) into the process environment before
38
+ # anything reads provider API keys/config from os.environ -- lets
39
+ # ANTHROPIC_API_KEY etc. live in a gitignored .env instead of being
40
+ # exported by hand or typed into Settings every run. A missing .env is not
41
+ # an error; existing environment variables always take precedence.
42
+ from dotenv import load_dotenv # noqa: E402
43
+
44
+ load_dotenv()
45
+
46
+ app = FastAPI(title="Model-Maker API")
47
+ app.add_middleware(
48
+ CORSMiddleware,
49
+ allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
50
+ allow_methods=["*"],
51
+ allow_headers=["*"],
52
+ )
53
+
54
+ SESSION = ProjectSession()
55
+ LLM_SETTINGS = LLMSettingsStore()
56
+
57
+ # Run orchestration: at most one run (a single block, a cascade, or a full
58
+ # sweep) is active at a time, executed on a background thread so a run that
59
+ # genuinely takes a while -- above all a heavily-grouped block, the case
60
+ # this whole mechanism exists to keep safe -- doesn't block the HTTP
61
+ # request indefinitely: the frontend polls GET /api/graph (whose per-block
62
+ # `status` already reflects "running", see Runner.status) to watch it
63
+ # progress, and POST /api/run/cancel to stop it. _start_background_run
64
+ # joins the thread for up to `wait_seconds` before returning, though, so
65
+ # the common case (a normal-sized run finishes in well under that) still
66
+ # gets a response that reflects the final state, exactly as if the call
67
+ # had been synchronous -- only a run that's still going after the wait
68
+ # comes back as "running" for the client to poll. The isolation that
69
+ # actually matters -- one subprocess per block call, so a crash or a
70
+ # runaway group_by can't take the server down -- is Runner's job (see
71
+ # runner.py) and applies either way.
72
+ _RUN_LOCK = threading.Lock()
73
+ _RUN_THREAD: threading.Thread | None = None
74
+ # A precondition failure (e.g. "Run" clicked on a block whose upstream
75
+ # isn't green yet) raises before any block state changes, so there's
76
+ # nothing in the graph's own status to show it happened -- stashed here,
77
+ # raised directly if the run finishes within the wait window (matching the
78
+ # old synchronous endpoints' behavior), and otherwise surfaced once via
79
+ # _graph_out(), which clears it on read.
80
+ _LAST_RUN_ERROR: str | None = None
81
+
82
+
83
+ def _start_background_run(fn: Callable[[], Any], wait_seconds: float = 20.0) -> Any:
84
+ """Returns fn()'s return value if it finishes within wait_seconds (the
85
+ common case -- callers that don't care, like the single-block run
86
+ endpoints, can just ignore it and re-read block state), else None to
87
+ mean "still running" (poll GET /api/graph)."""
88
+ global _RUN_THREAD, _LAST_RUN_ERROR
89
+ with _RUN_LOCK:
90
+ if _RUN_THREAD is not None and _RUN_THREAD.is_alive():
91
+ raise HTTPException(409, "a run is already in progress")
92
+ _LAST_RUN_ERROR = None
93
+ holder: dict[str, Any] = {}
94
+
95
+ def _target() -> None:
96
+ global _LAST_RUN_ERROR
97
+ try:
98
+ holder["result"] = fn()
99
+ except Exception as e: # noqa: BLE001 -- has no HTTP response to attach to once the wait below gives up
100
+ _LAST_RUN_ERROR = f"{type(e).__name__}: {e}"
101
+
102
+ thread = threading.Thread(target=_target, daemon=True)
103
+ _RUN_THREAD = thread
104
+ thread.start()
105
+ thread.join(timeout=wait_seconds)
106
+ if thread.is_alive():
107
+ return None
108
+ if _LAST_RUN_ERROR:
109
+ err, _LAST_RUN_ERROR = _LAST_RUN_ERROR, None
110
+ raise HTTPException(409, err)
111
+ return holder.get("result")
112
+
113
+ # Static curated list -- the `claude` CLI has no scriptable "list models"
114
+ # command, so this is what the model dropdown offers for the claude_cli
115
+ # provider instead of a live query.
116
+ CLAUDE_CLI_KNOWN_MODELS = [
117
+ "claude-opus-5",
118
+ "claude-sonnet-5",
119
+ "claude-fable-5-1",
120
+ "claude-haiku-4-5-20251001",
121
+ ]
122
+
123
+ # Where Save/Load default to when no project path is already known (see
124
+ # Toolbar.tsx) -- a subdirectory next to wherever the server runs. Not
125
+ # created at import time (nothing should touch the filesystem just from
126
+ # importing this module, e.g. under test) -- see project_default_dir below,
127
+ # which creates it on first request instead.
128
+ DEFAULT_PROJECTS_DIR = Path(os.environ.get("MODELMAKER_PROJECTS_DIR", "./projects")).expanduser().resolve()
129
+
130
+
131
+ # ---- schemas -----------------------------------------------------------
132
+
133
+
134
+ class PortSpecOut(BaseModel):
135
+ name: str
136
+ type: str
137
+ required: bool = True
138
+
139
+
140
+ class BlockCreate(BaseModel):
141
+ category: str
142
+ block_type: str | None = None
143
+ name: str | None = None
144
+ lane: str | None = None
145
+ x: float = 0
146
+ y: float = 0
147
+ params: dict[str, Any] = {}
148
+ code: str | None = None
149
+ inputs: list[dict] | None = None
150
+ outputs: list[dict] | None = None
151
+ metadata_transform: dict[str, Any] | None = None
152
+
153
+
154
+ class BlockUpdate(BaseModel):
155
+ name: str | None = None
156
+ lane: str | None = None
157
+ position: dict[str, float] | None = None
158
+ params: dict[str, Any] | None = None
159
+ code: str | None = None
160
+ metadata_transform: dict[str, Any] | None = None
161
+ # Column to run this block once per distinct value of, instead of once
162
+ # overall (see BlockInstance.group_by) -- like `lane`, an explicit null
163
+ # in the request body clears it (session.update_block applies it
164
+ # whenever the key is present at all, not only when non-None).
165
+ group_by: str | None = None
166
+ max_workers: int | None = None
167
+
168
+
169
+ class ColumnRoleUpdate(BaseModel):
170
+ column: str
171
+ role: str
172
+
173
+
174
+ class WireCreate(BaseModel):
175
+ from_block: str
176
+ from_port: str
177
+ to_block: str
178
+ to_port: str
179
+
180
+
181
+ class PortNameUpdate(BaseModel):
182
+ port: str
183
+ name: str | None = None
184
+
185
+
186
+ class LaneUpsert(BaseModel):
187
+ id: str
188
+ name: str
189
+ order: int
190
+ height: float | None = None
191
+
192
+
193
+ class SampleModeUpdate(BaseModel):
194
+ # None turns sample mode off; an int is the row cap applied to every
195
+ # input block's output (see Runner.sample_rows).
196
+ rows: int | None = None
197
+
198
+
199
+ class LoadRequest(BaseModel):
200
+ path: str
201
+
202
+
203
+ class SaveRequest(BaseModel):
204
+ path: str | None = None
205
+
206
+
207
+ class CompileRequest(BaseModel):
208
+ output_blocks: list[str] | None = None
209
+ strict: bool = True
210
+ # Mirrors Runner.run_all_streaming for the compiled script itself (see
211
+ # compile_graph's `stream` param): fuses whatever connected stretch of
212
+ # compatible blocks it safely can into one polars lazy plan per group.
213
+ # Default False keeps every existing "Compile" click's output unchanged.
214
+ stream: bool = False
215
+
216
+
217
+ class DraftRequest(BaseModel):
218
+ instruction: str = ""
219
+ provider: str | None = None
220
+
221
+
222
+ class LLMSettingsUpdate(BaseModel):
223
+ active_provider: str | None = None
224
+ # Global toggle for including the Polars API reference in the system
225
+ # prompt -- applies across every provider (see DraftContext.include_reference).
226
+ include_reference: bool | None = None
227
+ # Per-provider fields, e.g. {"lmstudio": {"model": "...", "base_url": "..."}}
228
+ # or {"openai": {"model": "...", "base_url": "...", "api_key": "sk-..."}}.
229
+ # Unrecognized keys for a given provider are ignored by get_provider. An
230
+ # explicit null for a field (e.g. {"openai": {"api_key": null}}) clears
231
+ # a previously-set override -- see LLMSettingsStore.update. API keys are
232
+ # write-only: GET/PUT never echo a raw key back, only whether one is set
233
+ # (see _redacted_provider_settings).
234
+ settings: dict[str, dict[str, Any]] | None = None
235
+
236
+
237
+ # ---- helpers -------------------------------------------------------------
238
+
239
+
240
+ @functools.lru_cache(maxsize=None)
241
+ def _safe_getsource(fn) -> str | None:
242
+ """inspect.getsource(), but None instead of raising for a function whose
243
+ source isn't available (e.g. defined in a REPL or compiled extension).
244
+ Cached since registry block functions are fixed at import time, and this
245
+ is called on every /api/graph and /api/blocks/{id} poll."""
246
+ try:
247
+ return inspect.getsource(fn)
248
+ except (OSError, TypeError):
249
+ return None
250
+
251
+
252
+ def _source_for_block(block) -> str | None:
253
+ """Read-only source for the block's underlying Python function. For
254
+ custom (is_custom) blocks the editable source is already exposed via
255
+ `code`; for registry blocks it lives in BLOCK_REGISTRY as a plain
256
+ function, so pull it with inspect for display purposes only."""
257
+ if block.is_custom:
258
+ return None
259
+ spec = BLOCK_REGISTRY.get(block.category)
260
+ if spec is None:
261
+ return None
262
+ return _safe_getsource(spec.fn)
263
+
264
+
265
+ def _block_out(block_id: str) -> dict[str, Any]:
266
+ block = SESSION.graph.blocks[block_id]
267
+ st = SESSION.runner.state.get(block_id)
268
+ status = SESSION.runner.status(block_id)
269
+ return {
270
+ # Why a stale block is stale (see Runner.stale_reason) -- only ever
271
+ # asked for an orange block, since that's the state whose cause isn't
272
+ # self-evident: grey has never run and red carries its error.
273
+ "stale_reason": SESSION.runner.stale_reason(block_id) if status == "orange" else None,
274
+ "id": block.id,
275
+ "block_type": block.block_type,
276
+ "is_custom": block.is_custom,
277
+ "category": block.category,
278
+ "name": block.name,
279
+ "lane": block.lane,
280
+ "position": {"x": block.position.x, "y": block.position.y},
281
+ "code_version": block.code_version,
282
+ "params": block.params,
283
+ "code": block.code,
284
+ "source": _source_for_block(block),
285
+ "metadata_transform": block.metadata_transform,
286
+ "inputs": [asdict(p) for p in block.inputs],
287
+ "outputs": [asdict(p) for p in block.outputs],
288
+ "port_names": block.port_names,
289
+ "group_by": block.group_by,
290
+ "max_workers": block.max_workers,
291
+ "status": status,
292
+ "last_error": st.last_error if st else None,
293
+ "last_successful_read_at": st.last_successful_read_at if st else None,
294
+ "last_attempt_at": st.last_attempt_at if st else None,
295
+ # A crude heartbeat while a block (or a whole fused streaming
296
+ # group, for its every exit -- see Runner.running_elapsed) is
297
+ # mid-dispatch -- polars gives no finer-grained progress for a
298
+ # single collect() call, so this is "how long has this been
299
+ # running", not "how far along is it".
300
+ "running_seconds": SESSION.runner.running_elapsed(block_id) if status == "running" else None,
301
+ }
302
+
303
+
304
+ def _graph_out() -> dict[str, Any]:
305
+ global _LAST_RUN_ERROR
306
+ run_error, _LAST_RUN_ERROR = _LAST_RUN_ERROR, None
307
+ return {
308
+ "project_name": SESSION.project_name,
309
+ "project_path": str(SESSION.project_path) if SESSION.project_path else None,
310
+ "dirty": SESSION.dirty,
311
+ "can_undo": SESSION.can_undo,
312
+ "can_redo": SESSION.can_redo,
313
+ "sample_rows": SESSION.runner.sample_rows,
314
+ "lanes": {lid: {"name": l.name, "order": l.order, "height": l.height} for lid, l in SESSION.graph.lanes.items()},
315
+ "blocks": {bid: _block_out(bid) for bid in SESSION.graph.blocks},
316
+ "run_error": run_error,
317
+ "wires": {
318
+ wid: {
319
+ "from_block": w.from_block,
320
+ "from_port": w.from_port,
321
+ "to_block": w.to_block,
322
+ "to_port": w.to_port,
323
+ "valid": wire_is_valid(SESSION.graph, w),
324
+ }
325
+ for wid, w in SESSION.graph.wires.items()
326
+ },
327
+ }
328
+
329
+
330
+ def _packet_preview(packet: DataFramePacket, rows: int, with_summary: bool) -> dict[str, Any]:
331
+ if with_summary:
332
+ packet = packet.compute_summary()
333
+ columns = [
334
+ {
335
+ "name": name,
336
+ "dtype": meta.dtype,
337
+ "role": meta.role.value if hasattr(meta.role, "value") else meta.role,
338
+ "description": meta.description,
339
+ }
340
+ for name, meta in packet.schema_meta.items()
341
+ ]
342
+ summary = None
343
+ if with_summary and packet.summary:
344
+ summary = {
345
+ name: {
346
+ "count": s.count,
347
+ "null_count": s.null_count,
348
+ "n_unique": s.n_unique,
349
+ "mean": s.mean,
350
+ "std": s.std,
351
+ "min": s.min,
352
+ "max": s.max,
353
+ }
354
+ for name, s in packet.summary.items()
355
+ }
356
+ return {
357
+ "columns": columns,
358
+ "rows": packet.data.head(rows).to_dicts(),
359
+ "row_count": packet.data.height,
360
+ "lineage": packet.lineage,
361
+ "summary": summary,
362
+ }
363
+
364
+
365
+ def _input_schema_for_block(block_id: str) -> dict[str, list[ColumnInfo]]:
366
+ result: dict[str, list[ColumnInfo]] = {p.name: [] for p in SESSION.graph.blocks[block_id].inputs}
367
+ for port, wire in SESSION.graph.input_wires(block_id).items():
368
+ result.setdefault(port, [])
369
+ if SESSION.runner.status(wire.from_block) not in ("green", "orange"):
370
+ continue
371
+ st = SESSION.runner.state.get(wire.from_block)
372
+ if st is None or st.last_successful_key is None:
373
+ continue
374
+ entry = SESSION.runner.cache.get(st.last_successful_key)
375
+ if entry is None:
376
+ continue
377
+ packet = entry.outputs.get(wire.from_port)
378
+ if not isinstance(packet, DataFramePacket):
379
+ continue
380
+ result[port] = [
381
+ ColumnInfo(name=name, dtype=meta.dtype, role=meta.role.value if hasattr(meta.role, "value") else meta.role)
382
+ for name, meta in packet.schema_meta.items()
383
+ ]
384
+ return result
385
+
386
+
387
+ # ---- routes ----------------------------------------------------------
388
+
389
+
390
+ @app.get("/api/health")
391
+ def health() -> dict[str, str]:
392
+ return {"status": "ok"}
393
+
394
+
395
+ @app.get("/api/registry")
396
+ def registry() -> list[dict[str, Any]]:
397
+ return [
398
+ {
399
+ "category": spec.category,
400
+ "block_type": spec.block_type,
401
+ "group": spec.group or spec.block_type,
402
+ "display_name": spec.display_name,
403
+ "inputs": [asdict(p) for p in spec.inputs],
404
+ "outputs": [asdict(p) for p in spec.outputs],
405
+ }
406
+ for spec in BLOCK_REGISTRY.values()
407
+ ]
408
+
409
+
410
+ @app.get("/api/graph")
411
+ def get_graph() -> dict[str, Any]:
412
+ return _graph_out()
413
+
414
+
415
+ @app.get("/api/browse")
416
+ def browse(path: str | None = None, ext: str | None = None) -> dict[str, Any]:
417
+ """List a server-side directory so the UI can offer a file picker for
418
+ params like read_csv's `path` -- the frontend runs in a regular browser,
419
+ which can't hand back an absolute filesystem path from <input type=file>,
420
+ so this stands in for a native file dialog. Local dev tool, run by the
421
+ user against their own machine, so no extra path sandboxing beyond
422
+ resolving `..`/symlinks."""
423
+ base = Path(path).expanduser().resolve() if path else Path.cwd()
424
+ if not base.exists():
425
+ base = Path.cwd()
426
+ if base.is_file():
427
+ base = base.parent
428
+
429
+ entries = []
430
+ try:
431
+ for e in sorted(base.iterdir(), key=lambda p: (not p.is_dir(), p.name.lower())):
432
+ if e.name.startswith("."):
433
+ continue
434
+ if not e.is_dir() and ext and not e.name.lower().endswith(ext.lower()):
435
+ continue
436
+ entries.append({"name": e.name, "path": str(e), "is_dir": e.is_dir()})
437
+ except PermissionError:
438
+ pass
439
+
440
+ parent = base.parent
441
+ return {
442
+ "path": str(base),
443
+ "parent": str(parent) if parent != base else None,
444
+ "entries": entries,
445
+ }
446
+
447
+
448
+ @app.get("/api/project/default_dir")
449
+ def project_default_dir() -> dict[str, str]:
450
+ DEFAULT_PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
451
+ return {"path": str(DEFAULT_PROJECTS_DIR)}
452
+
453
+
454
+ @app.post("/api/project/load")
455
+ def load_project_ep(req: LoadRequest) -> dict[str, Any]:
456
+ try:
457
+ SESSION.load(Path(req.path))
458
+ except FileNotFoundError:
459
+ raise HTTPException(404, f"project file not found: {req.path}")
460
+ return _graph_out()
461
+
462
+
463
+ @app.post("/api/project/save")
464
+ def save_project_ep(req: SaveRequest) -> dict[str, Any]:
465
+ try:
466
+ path = SESSION.save(Path(req.path) if req.path else None)
467
+ except ValueError as e:
468
+ raise HTTPException(400, str(e))
469
+ return {"path": str(path)}
470
+
471
+
472
+ @app.get("/api/project/recovery")
473
+ def recovery_info_ep() -> dict[str, Any]:
474
+ """Metadata about the crash-recovery snapshot, if there is one. Offered
475
+ to the user on a fresh start; never applied on its own."""
476
+ return {"recovery": SESSION.recovery_info()}
477
+
478
+
479
+ @app.post("/api/project/recover")
480
+ def recover_ep() -> dict[str, Any]:
481
+ try:
482
+ SESSION.recover()
483
+ except ValueError as e:
484
+ raise HTTPException(404, str(e))
485
+ return _graph_out()
486
+
487
+
488
+ @app.post("/api/project/recovery/dismiss")
489
+ def dismiss_recovery_ep() -> dict[str, str]:
490
+ """Discard the crash-recovery snapshot without applying it -- called
491
+ when the user declines the restore prompt, so the same stale snapshot
492
+ doesn't keep coming back on every future startup."""
493
+ SESSION.clear_recovery()
494
+ return {"status": "ok"}
495
+
496
+
497
+ @app.post("/api/undo")
498
+ def undo_ep() -> dict[str, Any]:
499
+ if not SESSION.undo():
500
+ raise HTTPException(409, "nothing to undo")
501
+ return _graph_out()
502
+
503
+
504
+ @app.post("/api/redo")
505
+ def redo_ep() -> dict[str, Any]:
506
+ if not SESSION.redo():
507
+ raise HTTPException(409, "nothing to redo")
508
+ return _graph_out()
509
+
510
+
511
+ @app.put("/api/sample_mode")
512
+ def set_sample_mode(req: SampleModeUpdate) -> dict[str, Any]:
513
+ """Turn sample mode on (rows=N) or off (rows=null). Changes every
514
+ block's cache key, so statuses shift immediately -- nothing re-runs
515
+ until asked. Safe mid-run: a run in flight is pinned to the sample
516
+ setting it started with (see Runner.pin)."""
517
+ try:
518
+ SESSION.set_sample_rows(req.rows)
519
+ except ValueError as e:
520
+ raise HTTPException(400, str(e))
521
+ return _graph_out()
522
+
523
+
524
+ @app.post("/api/blocks")
525
+ def create_block(req: BlockCreate) -> dict[str, Any]:
526
+ try:
527
+ with SESSION.edit():
528
+ block = SESSION.add_block(
529
+ category=req.category,
530
+ block_type=req.block_type,
531
+ name=req.name,
532
+ lane=req.lane,
533
+ x=req.x,
534
+ y=req.y,
535
+ params=req.params,
536
+ code=req.code,
537
+ inputs=req.inputs,
538
+ outputs=req.outputs,
539
+ metadata_transform=req.metadata_transform,
540
+ )
541
+ except ValueError as e:
542
+ raise HTTPException(400, str(e))
543
+ return _block_out(block.id)
544
+
545
+
546
+ @app.patch("/api/blocks/{block_id}")
547
+ def update_block(block_id: str, req: BlockUpdate) -> dict[str, Any]:
548
+ if block_id not in SESSION.graph.blocks:
549
+ raise HTTPException(404, f"no such block: {block_id}")
550
+ with SESSION.edit():
551
+ SESSION.update_block(block_id, **req.model_dump(exclude_unset=True))
552
+ return _block_out(block_id)
553
+
554
+
555
+ @app.post("/api/blocks/{block_id}/column_role")
556
+ def set_column_role(block_id: str, req: ColumnRoleUpdate) -> dict[str, Any]:
557
+ """Hand-tag a column's role (id/target/weight/feature/date/segment/
558
+ excluded, or "unassigned" to clear it) on this block -- see
559
+ ProjectSession.set_column_role. Changing it is a normal block edit: it
560
+ changes the block's cache key, so this block and everything downstream
561
+ go stale like any other param change, rather than needing separate
562
+ invalidation."""
563
+ _require_block(block_id)
564
+ try:
565
+ with SESSION.edit():
566
+ SESSION.set_column_role(block_id, req.column, req.role)
567
+ except ValueError as e:
568
+ raise HTTPException(400, str(e))
569
+ return _block_out(block_id)
570
+
571
+
572
+ @app.get("/api/blocks/{block_id}/input_schema")
573
+ def input_schema_ep(block_id: str) -> dict[str, list[dict[str, str]]]:
574
+ """Column names/dtypes/roles available on each of a block's input ports
575
+ (empty for a port whose upstream hasn't produced output yet) -- lets the
576
+ UI offer column dropdowns for params instead of free-text entry."""
577
+ _require_block(block_id)
578
+ return {port: [asdict(c) for c in cols] for port, cols in _input_schema_for_block(block_id).items()}
579
+
580
+
581
+ @app.delete("/api/blocks/{block_id}")
582
+ def delete_block(block_id: str) -> dict[str, str]:
583
+ with SESSION.edit():
584
+ SESSION.delete_block(block_id)
585
+ return {"deleted": block_id}
586
+
587
+
588
+ def _get_cached_output(block_id: str, port: str | None, want_type: str | None = None) -> Any:
589
+ """Look up a block's cached output value on the given port (defaulting
590
+ to its declared port of `want_type`, or its first output port). Raises
591
+ the appropriate HTTPException if the block isn't runnable, has no
592
+ matching port, or nothing is cached yet."""
593
+ if block_id not in SESSION.graph.blocks:
594
+ raise HTTPException(404, f"no such block: {block_id}")
595
+ status = SESSION.runner.status(block_id)
596
+ if status not in ("green", "orange"):
597
+ raise HTTPException(409, f"block '{block_id}' has no output to view (status={status})")
598
+ st = SESSION.runner.state[block_id]
599
+ entry = SESSION.runner.cache.get(st.last_successful_key) # type: ignore[arg-type]
600
+ if entry is None:
601
+ raise HTTPException(409, "cached output missing")
602
+ block = SESSION.graph.blocks[block_id]
603
+ if port is None and want_type is not None:
604
+ port = next((p.name for p in block.outputs if p.type == want_type), None)
605
+ port = port or (block.outputs[0].name if block.outputs else None)
606
+ if port is None or port not in entry.outputs:
607
+ raise HTTPException(404, f"no such output port: {port}")
608
+ return entry.outputs[port]
609
+
610
+
611
+ @app.get("/api/blocks/{block_id}/preview")
612
+ def preview_block(block_id: str, port: str | None = None, rows: int = 20, summary: bool = False) -> dict[str, Any]:
613
+ value = _get_cached_output(block_id, port)
614
+ if not isinstance(value, DataFramePacket):
615
+ raise HTTPException(400, "output port is not a dataframe")
616
+ return _packet_preview(value, rows=rows, with_summary=summary)
617
+
618
+
619
+ @app.get("/api/blocks/{block_id}/image")
620
+ def block_image(block_id: str, port: str | None = None) -> Response:
621
+ value = _get_cached_output(block_id, port, want_type="image")
622
+ if not isinstance(value, (bytes, bytearray)):
623
+ raise HTTPException(400, "output port is not an image")
624
+ return Response(content=bytes(value), media_type="image/png")
625
+
626
+
627
+ @app.get("/api/blocks/{block_id}/value")
628
+ def block_value(block_id: str, port: str | None = None) -> Any:
629
+ """Raw JSON-shaped output (a scalar_metric or model port's dict, or any
630
+ other plain value) for ports that aren't a dataframe or an image --
631
+ those have their own endpoints above."""
632
+ value = _get_cached_output(block_id, port)
633
+ if isinstance(value, DataFramePacket):
634
+ raise HTTPException(400, "output port is a dataframe -- use /preview")
635
+ if isinstance(value, (bytes, bytearray)):
636
+ raise HTTPException(400, "output port is binary (e.g. an image) -- use /image")
637
+ return value
638
+
639
+
640
+ @app.post("/api/wires")
641
+ def create_wire(req: WireCreate) -> dict[str, Any]:
642
+ for bid in (req.from_block, req.to_block):
643
+ if bid not in SESSION.graph.blocks:
644
+ raise HTTPException(404, f"no such block: {bid}")
645
+ try:
646
+ with SESSION.edit():
647
+ wire = SESSION.add_wire(req.from_block, req.from_port, req.to_block, req.to_port)
648
+ except ValueError as e:
649
+ raise HTTPException(400, str(e))
650
+ return {
651
+ "id": wire.id,
652
+ "from_block": wire.from_block,
653
+ "from_port": wire.from_port,
654
+ "to_block": wire.to_block,
655
+ "to_port": wire.to_port,
656
+ "valid": wire_is_valid(SESSION.graph, wire),
657
+ }
658
+
659
+
660
+ @app.delete("/api/wires/{wire_id}")
661
+ def delete_wire(wire_id: str) -> dict[str, str]:
662
+ with SESSION.edit():
663
+ SESSION.delete_wire(wire_id)
664
+ return {"deleted": wire_id}
665
+
666
+
667
+ @app.patch("/api/blocks/{block_id}/port_name")
668
+ def rename_port(block_id: str, req: PortNameUpdate) -> dict[str, Any]:
669
+ """Name (or clear the name of) the data on one of this block's output
670
+ ports -- see BlockInstance.port_names. Set by clicking that port's data
671
+ in the UI (PortInspector); used, when set, as the compiled script's
672
+ variable name for it (see compiler.compile_graph)."""
673
+ _require_block(block_id)
674
+ try:
675
+ with SESSION.edit():
676
+ SESSION.rename_port(block_id, req.port, req.name)
677
+ except ValueError as e:
678
+ raise HTTPException(400, str(e))
679
+ return _block_out(block_id)
680
+
681
+
682
+ @app.put("/api/lanes")
683
+ def upsert_lane(req: LaneUpsert) -> dict[str, Any]:
684
+ with SESSION.edit():
685
+ SESSION.set_lane(req.id, req.name, req.order, height=req.height)
686
+ return _graph_out()["lanes"]
687
+
688
+
689
+ @app.delete("/api/lanes/{lane_id}")
690
+ def delete_lane(lane_id: str) -> dict[str, Any]:
691
+ with SESSION.edit():
692
+ SESSION.delete_lane(lane_id)
693
+ return _graph_out()["lanes"]
694
+
695
+
696
+ def _require_block(block_id: str) -> None:
697
+ if block_id not in SESSION.graph.blocks:
698
+ raise HTTPException(404, f"no such block: {block_id}")
699
+
700
+
701
+ @app.post("/api/blocks/{block_id}/run")
702
+ def run_block_ep(block_id: str) -> dict[str, Any]:
703
+ _require_block(block_id)
704
+ _start_background_run(lambda: SESSION.runner.run_block(block_id))
705
+ return _block_out(block_id)
706
+
707
+
708
+ @app.post("/api/blocks/{block_id}/run_to_here")
709
+ def run_to_here_ep(block_id: str) -> dict[str, Any]:
710
+ _require_block(block_id)
711
+ _start_background_run(lambda: SESSION.runner.run_to_here(block_id))
712
+ return _block_out(block_id)
713
+
714
+
715
+ @app.post("/api/blocks/{block_id}/refresh")
716
+ def refresh_ep(block_id: str) -> dict[str, Any]:
717
+ _require_block(block_id)
718
+ if SESSION.graph.blocks[block_id].block_type != "input":
719
+ raise HTTPException(400, "refresh is only valid for input blocks")
720
+ _start_background_run(lambda: SESSION.runner.refresh(block_id))
721
+ return _block_out(block_id)
722
+
723
+
724
+ @app.post("/api/blocks/{block_id}/check_changes")
725
+ def check_changes(block_id: str) -> dict[str, Any]:
726
+ _require_block(block_id)
727
+ return {"block_id": block_id, "changed": SESSION.runner.check_for_changes(block_id)}
728
+
729
+
730
+ @app.post("/api/run_all")
731
+ def run_all() -> dict[str, Any]:
732
+ report = _start_background_run(lambda: SESSION.runner.run_all())
733
+ return report if report is not None else {"started": True}
734
+
735
+
736
+ @app.post("/api/force_run_all")
737
+ def force_run_all() -> dict[str, Any]:
738
+ report = _start_background_run(lambda: SESSION.runner.force_run_all())
739
+ return report if report is not None else {"started": True}
740
+
741
+
742
+ @app.post("/api/run_all_streaming")
743
+ def run_all_streaming() -> dict[str, Any]:
744
+ """Opt-in streaming run (see Runner.run_all_streaming): fuses whatever
745
+ contiguous stretch of compatible blocks it safely can into a single
746
+ polars query per group, so a source larger than memory doesn't fully
747
+ materialize at every block boundary. A precondition failure (sample
748
+ mode is on) surfaces the same way run_to_here's does -- see
749
+ _start_background_run."""
750
+ report = _start_background_run(lambda: SESSION.runner.run_all_streaming())
751
+ return report if report is not None else {"started": True}
752
+
753
+
754
+ @app.post("/api/refresh_all")
755
+ def refresh_all() -> dict[str, Any]:
756
+ report = _start_background_run(lambda: SESSION.runner.refresh_all())
757
+ return report if report is not None else {"started": True}
758
+
759
+
760
+ @app.post("/api/run/cancel")
761
+ def cancel_run() -> dict[str, bool]:
762
+ """Stop whatever's currently running -- a single block, a run_to_here
763
+ cascade, a run_all/force_run_all/refresh_all/run_all_streaming sweep,
764
+ or one streaming run's fused group. The in-flight block's own
765
+ subprocess(es) are terminated immediately (see Runner._dispatch/
766
+ _dispatch_fused_group); a cascade also stops issuing further blocks rather
767
+ than continuing on to the next one."""
768
+ return {"cancelled": SESSION.runner.cancel()}
769
+
770
+
771
+ @app.post("/api/check_all_sources")
772
+ def check_all_sources() -> dict[str, bool]:
773
+ return SESSION.runner.check_all_sources()
774
+
775
+
776
+ @app.post("/api/compile")
777
+ def compile_ep(req: CompileRequest) -> dict[str, str]:
778
+ try:
779
+ source = compile_graph(
780
+ SESSION.graph,
781
+ runner=SESSION.runner,
782
+ output_blocks=req.output_blocks,
783
+ strict=req.strict,
784
+ stream=req.stream,
785
+ )
786
+ except (CompileError, ValueError) as e:
787
+ raise HTTPException(409, str(e))
788
+ return {"source": source}
789
+
790
+
791
+ # Env vars (beyond the per-provider override stored in LLM_SETTINGS) each
792
+ # key-requiring provider's API key can fall back to, in priority order --
793
+ # mirrors what each provider's own __init__ checks, used only to report
794
+ # *whether* a key is available, never its value.
795
+ _API_KEY_ENV_VARS: dict[str, list[str]] = {
796
+ "anthropic": ["ANTHROPIC_API_KEY"],
797
+ "openai": ["MODELMAKER_OPENAI_API_KEY", "OPENAI_API_KEY"],
798
+ "gemini": ["MODELMAKER_GEMINI_API_KEY", "GEMINI_API_KEY", "GOOGLE_API_KEY"],
799
+ }
800
+
801
+
802
+ def _resolve_api_key(provider_name: str) -> str | None:
803
+ """The API key that would actually be used for this provider right now
804
+ (a Settings override, else the first set env var) -- never exposed to
805
+ the client, only used server-side to call the provider or check
806
+ liveness (e.g. for /api/llm/models)."""
807
+ override = LLM_SETTINGS.for_provider(provider_name).get("api_key")
808
+ if override:
809
+ return override
810
+ for var in _API_KEY_ENV_VARS.get(provider_name, []):
811
+ value = os.environ.get(var)
812
+ if value:
813
+ return value
814
+ return None
815
+
816
+
817
+ def _redacted_provider_settings(provider_name: str, **fields: Any) -> dict[str, Any]:
818
+ """Build one provider's entry for GET/PUT /api/llm/settings: the given
819
+ resolved (non-secret) fields, plus an api_key_set/api_key_source pair
820
+ that reports *whether* a key is configured and where it came from --
821
+ never the key itself. This is the only place a stored key's presence is
822
+ surfaced to the client."""
823
+ result: dict[str, Any] = dict(fields)
824
+ if provider_name in _API_KEY_ENV_VARS:
825
+ override = LLM_SETTINGS.for_provider(provider_name).get("api_key")
826
+ if override:
827
+ result["api_key_set"] = True
828
+ result["api_key_source"] = "override"
829
+ else:
830
+ env_value = next(
831
+ (os.environ.get(var) for var in _API_KEY_ENV_VARS[provider_name] if os.environ.get(var)), None
832
+ )
833
+ result["api_key_set"] = bool(env_value)
834
+ result["api_key_source"] = "env" if env_value else None
835
+ return result
836
+
837
+
838
+ def _effective_llm_settings() -> dict[str, Any]:
839
+ """The Settings panel's view of LLM config: every field resolved to what
840
+ would actually be used right now (a UI override, else the env var each
841
+ provider itself falls back to, else its hardcoded default) -- so text
842
+ fields and checkboxes always show a real value instead of blank. API
843
+ keys are the exception: only their presence/source is reported, never
844
+ the value (see _redacted_provider_settings)."""
845
+ lmstudio_override = LLM_SETTINGS.for_provider("lmstudio")
846
+ claude_cli_override = LLM_SETTINGS.for_provider("claude_cli")
847
+ openai_override = LLM_SETTINGS.for_provider("openai")
848
+ gemini_override = LLM_SETTINGS.for_provider("gemini")
849
+ settings: dict[str, Any] = {
850
+ "lmstudio": _redacted_provider_settings(
851
+ "lmstudio",
852
+ base_url=lmstudio_override.get("base_url")
853
+ or os.environ.get("MODELMAKER_LLM_BASE_URL", _lmstudio_provider.DEFAULT_BASE_URL),
854
+ model=lmstudio_override.get("model") or os.environ.get("MODELMAKER_LLM_MODEL"),
855
+ ),
856
+ "claude_cli": _redacted_provider_settings(
857
+ "claude_cli",
858
+ model=claude_cli_override.get("model")
859
+ or os.environ.get("MODELMAKER_LLM_MODEL")
860
+ or _claude_cli_provider.DEFAULT_MODEL,
861
+ ),
862
+ "openai": _redacted_provider_settings(
863
+ "openai",
864
+ base_url=openai_override.get("base_url")
865
+ or os.environ.get("MODELMAKER_OPENAI_BASE_URL", _openai_provider.DEFAULT_BASE_URL),
866
+ model=openai_override.get("model") or os.environ.get("MODELMAKER_LLM_MODEL"),
867
+ ),
868
+ "gemini": _redacted_provider_settings(
869
+ "gemini",
870
+ model=gemini_override.get("model") or os.environ.get("MODELMAKER_LLM_MODEL"),
871
+ ),
872
+ }
873
+ if _anthropic_provider is not None:
874
+ anthropic_override = LLM_SETTINGS.for_provider("anthropic")
875
+ settings["anthropic"] = _redacted_provider_settings(
876
+ "anthropic",
877
+ model=anthropic_override.get("model")
878
+ or os.environ.get("MODELMAKER_LLM_MODEL")
879
+ or _anthropic_provider.DEFAULT_MODEL,
880
+ )
881
+ return {
882
+ "providers": sorted(LLM_PROVIDER_REGISTRY),
883
+ "active_provider": LLM_SETTINGS.active_provider or os.environ.get("MODELMAKER_LLM_PROVIDER", "claude_cli"),
884
+ # Tri-state: null means "each provider's own default" (on for LM
885
+ # Studio, off elsewhere) rather than an explicit choice.
886
+ "include_reference": LLM_SETTINGS.include_reference,
887
+ "settings": settings,
888
+ }
889
+
890
+
891
+ @app.get("/api/llm/settings")
892
+ def get_llm_settings() -> dict[str, Any]:
893
+ return _effective_llm_settings()
894
+
895
+
896
+ @app.put("/api/llm/settings")
897
+ def update_llm_settings(req: LLMSettingsUpdate) -> dict[str, Any]:
898
+ if req.active_provider is not None and req.active_provider not in LLM_PROVIDER_REGISTRY:
899
+ raise HTTPException(400, f"unknown LLM provider: {req.active_provider}")
900
+ LLM_SETTINGS.update(req.active_provider, req.include_reference, req.settings)
901
+ return _effective_llm_settings()
902
+
903
+
904
+ @app.get("/api/llm/models")
905
+ def list_llm_models(provider: str, base_url: str | None = None) -> dict[str, list[str]]:
906
+ """Query a provider for the models it currently has available -- used by
907
+ the Settings panel's "Fetch models" so the user can pick one instead of
908
+ typing an id from memory. `base_url` (LM Studio and openai only) lets
909
+ the panel query a not-yet-saved address before committing it via
910
+ PUT /llm/settings."""
911
+ if provider == "lmstudio":
912
+ effective_base_url = base_url or _effective_llm_settings()["settings"]["lmstudio"]["base_url"]
913
+ try:
914
+ return {"models": _lmstudio_provider.list_models(effective_base_url)}
915
+ except RuntimeError as e:
916
+ raise HTTPException(502, str(e))
917
+ if provider == "claude_cli":
918
+ return {"models": CLAUDE_CLI_KNOWN_MODELS}
919
+ if provider == "anthropic":
920
+ if _anthropic_provider is None:
921
+ raise HTTPException(400, "the anthropic provider is unavailable -- install the `anthropic` package")
922
+ api_key = _resolve_api_key("anthropic")
923
+ try:
924
+ client = _anthropic_provider.anthropic.Anthropic(api_key=api_key) if api_key else _anthropic_provider.anthropic.Anthropic()
925
+ return {"models": [m.id for m in client.models.list()]}
926
+ except Exception as e:
927
+ raise HTTPException(502, f"could not list Anthropic models: {e}")
928
+ if provider == "openai":
929
+ api_key = _resolve_api_key("openai")
930
+ if not api_key:
931
+ raise HTTPException(400, "no OpenAI API key configured -- set one in Settings first")
932
+ effective_base_url = base_url or _effective_llm_settings()["settings"]["openai"]["base_url"]
933
+ try:
934
+ return {"models": _openai_provider.list_models(effective_base_url, api_key)}
935
+ except RuntimeError as e:
936
+ raise HTTPException(502, str(e))
937
+ if provider == "gemini":
938
+ api_key = _resolve_api_key("gemini")
939
+ if not api_key:
940
+ raise HTTPException(400, "no Gemini API key configured -- set one in Settings first")
941
+ try:
942
+ return {"models": _gemini_provider.list_models(_gemini_provider.DEFAULT_BASE_URL, api_key)}
943
+ except RuntimeError as e:
944
+ raise HTTPException(502, str(e))
945
+ if provider == "stub":
946
+ return {"models": []}
947
+ raise HTTPException(400, f"unknown LLM provider: {provider}")
948
+
949
+
950
+ def _provider_for(name: str | None) -> LLMProvider:
951
+ """Resolve a provider for a draft/suggest_fix call: an explicit request
952
+ override if given, else the Settings panel's active provider, else the
953
+ env var default -- configured with whatever overrides (model, base_url)
954
+ are on file for it."""
955
+ provider_name = name or LLM_SETTINGS.active_provider or os.environ.get("MODELMAKER_LLM_PROVIDER", "claude_cli")
956
+ overrides = dict(LLM_SETTINGS.for_provider(provider_name))
957
+ if LLM_SETTINGS.include_reference is not None:
958
+ overrides.setdefault("include_reference", LLM_SETTINGS.include_reference)
959
+ return get_provider(provider_name, **overrides)
960
+
961
+
962
+ def _draft_context_for_block(block, block_id: str, instruction: str, error: str | None = None) -> DraftContext:
963
+ """Build the DraftContext for either flavor of block: custom (is_custom)
964
+ blocks get the original "author full code" mode; registry blocks have
965
+ fixed code, so the model can only choose values for their existing
966
+ parameters ("params_only" mode) -- this is what makes "Draft with AI"
967
+ available on every block, not just custom ones, without letting the
968
+ model touch fixed code."""
969
+ input_ports = _input_schema_for_block(block_id)
970
+ param_names = list(block.params)
971
+ include_reference = LLM_SETTINGS.include_reference if LLM_SETTINGS.include_reference is not None else False
972
+
973
+ if block.is_custom:
974
+ return DraftContext(
975
+ instruction=instruction,
976
+ function_name=block.category,
977
+ input_ports=input_ports,
978
+ param_names=param_names,
979
+ existing_code=block.code,
980
+ error=error,
981
+ include_reference=include_reference,
982
+ )
983
+
984
+ spec = BLOCK_REGISTRY.get(block.category)
985
+ if spec is None:
986
+ raise HTTPException(400, f"unknown block category: {block.category}")
987
+ fixed_source = _safe_getsource(spec.fn)
988
+ return DraftContext(
989
+ instruction=instruction,
990
+ function_name=block.category,
991
+ input_ports=input_ports,
992
+ param_names=param_names,
993
+ fixed_source=fixed_source,
994
+ error=error,
995
+ mode="params_only",
996
+ include_reference=include_reference,
997
+ )
998
+
999
+
1000
+ @app.post("/api/blocks/{block_id}/draft")
1001
+ def draft_block(block_id: str, req: DraftRequest) -> dict[str, Any]:
1002
+ """Draft (or redraft) a block from a natural-language instruction.
1003
+ llm_authored blocks get a full function body proposal; every other
1004
+ block type gets suggested values for its existing parameters only,
1005
+ since their code is fixed. Never applied automatically -- returns the
1006
+ proposal for the caller to review and save via PATCH /api/blocks/{id}."""
1007
+ _require_block(block_id)
1008
+ block = SESSION.graph.blocks[block_id]
1009
+ if not req.instruction.strip():
1010
+ raise HTTPException(400, "instruction is required")
1011
+
1012
+ ctx = _draft_context_for_block(block, block_id, req.instruction)
1013
+ try:
1014
+ provider = _provider_for(req.provider)
1015
+ result = provider.draft(ctx)
1016
+ except Exception as e:
1017
+ raise HTTPException(502, f"LLM draft failed: {e}")
1018
+ return {
1019
+ "code": result.code,
1020
+ "metadata_transform": result.metadata_transform,
1021
+ "params": result.params,
1022
+ "explanation": result.explanation,
1023
+ }
1024
+
1025
+
1026
+ @app.post("/api/blocks/{block_id}/suggest_fix")
1027
+ def suggest_fix(block_id: str, req: DraftRequest = DraftRequest()) -> dict[str, Any]:
1028
+ """AI-assisted fix for a red block: sends the code (or fixed source),
1029
+ params, actual error, and input schema; returns a proposed diff, never
1030
+ auto-applied."""
1031
+ _require_block(block_id)
1032
+ block = SESSION.graph.blocks[block_id]
1033
+ st = SESSION.runner.state.get(block_id)
1034
+ if st is None or not st.last_error:
1035
+ raise HTTPException(400, "block has no recorded error to fix")
1036
+
1037
+ ctx = _draft_context_for_block(block, block_id, req.instruction.strip() or "Fix the error.", error=st.last_error)
1038
+ try:
1039
+ provider = _provider_for(req.provider)
1040
+ result = provider.draft(ctx)
1041
+ except Exception as e:
1042
+ raise HTTPException(502, f"LLM suggest_fix failed: {e}")
1043
+ return {
1044
+ "code": result.code,
1045
+ "metadata_transform": result.metadata_transform,
1046
+ "params": result.params,
1047
+ "explanation": result.explanation,
1048
+ }
1049
+
1050
+
1051
+ # Serve the built frontend (from `npm run build` in frontend/, which emits
1052
+ # into this directory) if present, so `modelmaker-api` alone can serve the
1053
+ # whole app on one port. Mounted last so it never shadows the /api routes
1054
+ # above. Absent in a dev checkout that hasn't built the frontend -- run the
1055
+ # Vite dev server separately in that case.
1056
+ _STATIC_DIR = Path(__file__).parent / "static"
1057
+ if _STATIC_DIR.is_dir():
1058
+ app.mount("/", StaticFiles(directory=_STATIC_DIR, html=True), name="frontend")
1059
+
1060
+
1061
+ def main() -> None:
1062
+ """Entry point for the `modelmaker-api` console script."""
1063
+ import uvicorn
1064
+
1065
+ uvicorn.run(
1066
+ "modelmaker.api:app",
1067
+ host=os.environ.get("MODELMAKER_HOST", "127.0.0.1"),
1068
+ port=int(os.environ.get("MODELMAKER_PORT", "8001")),
1069
+ )
1070
+
1071
+
1072
+ if __name__ == "__main__":
1073
+ main()