Repository navigation
Expand file tree
/
Copy pathgenerate_docs.py
More file actions
1628 lines (1388 loc) · 61.6 KB
/
Copy pathgenerate_docs.py
File metadata and controls
1628 lines (1388 loc) · 61.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Generate the docs that restate facts the code owns (#795).
Each class of fact has one owner. Where a page restates a fact the code
already holds, this script writes that part of the page from the code, and a
unit test (``tests/unit/test_generated_docs.py``) fails when the committed
page and the code disagree, the same way the ``docs-llms-drift`` CI job fails
on stale ``website/public`` files.
Targets:
* ``python/README.md`` — the PyPI description, written whole from the
repository ``README.md`` with every relative link made absolute (PyPI does
not resolve them). Edit the root ``README.md``, never this copy.
* ``website/content/reference/configuration.mdx`` — the ``settings-index``
region: every ``DonkeyConfig`` field with its env var, config-file key and
default, from the field table in ``core/config.py``.
* ``website/content/cli.mdx`` — the ``cli-reference`` region: every visible
``donkey`` command and option, from the typer app. Needs the ``[cli]`` extra.
* ``website/content/frameworks/index.mdx`` — the ``adapter-capabilities``
region: each adapter's extra and each factory's ``capabilities()``, from the
``ADAPTERS`` roster and the adapter classes.
* ``website/content/reference/api.mdx`` — the Python API reference, written
whole from the public signatures and the first paragraph of each docstring.
* Per-framework verification status (#1081), owned by ``docs/verified-apis.md``:
the §8 Status column rates each factory, the §8.4 roster maps each
``ADAPTERS`` factory to its §8 row and page, and ``conformance_tested`` on
the roster gives its depth. From those: the ``framework-status`` table and
the ``framework-status-summary`` sentences of ``README.md``, the summary in
``ARCHITECTURE.md``, the status table and card badges of
``website/content/frameworks/index.mdx``, the frontmatter ``status:`` of
every ``frameworks/*.mdx`` page, the framework row of ``quickstart.mdx`` and
the framework card badge of ``feature-overview.mdx``. A §8 status with no
docs label, a factory with no roster row, or a page with no factory fails.
* Framework version compatibility (#1068): ``docs/framework-support.md`` and
``website/content/frameworks/compatibility.mdx``, written whole. Per adapter
package: the floor from its extra in ``pyproject.toml``, the version the PR
``adapter-contract`` leg tests from its lock in ``python/constraints/``, and
the open ``docs/verified-apis.md`` §8.1 rows that name it. An unknown §8.1
status, a requirement with no ``>=`` floor, or a missing lock or pin fails.
A region sits between two marker comments, ``BEGIN GENERATED: <name>`` and
``END GENERATED: <name>``; everything between them is replaced.
Frontmatter, card badges and single table rows are rewritten in place.
python scripts/generate_docs.py # rewrite every target
python scripts/generate_docs.py --check # exit 1 and list stale targets
A target whose optional dependency is not installed is skipped and named.
"""
from __future__ import annotations
import argparse
import dataclasses
import functools
import importlib
import importlib.util
import inspect
import re
import sys
import typing
from collections.abc import Callable, Iterable
from dataclasses import dataclass
from pathlib import Path
from typing import Any
if sys.version_info >= (3, 11):
import tomllib
else: # pragma: no cover - 3.10 backfill
import tomli as tomllib
REPO = Path(__file__).resolve().parents[2]
REPO_URL = "https://github.com/Donkey-Development-Kit/donkey-development-kit"
# Reader-facing links point at the released branch (website/lib/repo.mjs).
DOCS_REF = "main"
SITE = "https://docs.donkey-kit.dev"
_SCRIPT = "python/scripts/generate_docs.py"
# --- regions -----------------------------------------------------------------
class GenerationError(Exception):
"""A target file is missing its markers or cannot be rendered."""
def replace_region(text: str, name: str, body: str) -> str:
"""``text`` with the lines between the ``name`` markers replaced by ``body``.
The marker lines themselves are kept as written, so a page chooses its own
comment syntax (``<!-- -->`` in Markdown, ``{/* */}`` in MDX).
"""
lines = text.splitlines(keepends=True)
begin = [i for i, line in enumerate(lines) if f"BEGIN GENERATED: {name} " in line]
end = [i for i, line in enumerate(lines) if f"END GENERATED: {name} " in line]
if len(begin) != 1 or len(end) != 1 or end[0] < begin[0]:
raise GenerationError(
f"expected one 'BEGIN GENERATED: {name}' marker followed by one "
f"'END GENERATED: {name}' marker"
)
return "".join([*lines[: begin[0] + 1], body.rstrip("\n") + "\n", *lines[end[0] :]])
# --- Markdown helpers ----------------------------------------------------------
_CODE_SPAN = re.compile(r"(`+)(.+?)\1")
_ROLE = re.compile(r":[a-z]+:`(~?)([^`<]+?)(?:\s*<([^`>]+)>)?`")
_REF = (
r"(?:`?BG §[\d.]+`?|§[\d.]+|#\d+(?:/#\d+)*|ADR \d+|ADR-\d+|"
r"docs/[\w./-]+(?: §[\d.]+)?|upstream gap #\d+|AC\d+|hazard #\d+|"
r"config resolution|the conformance kit|Phase \d+(?:\.\d+)?)"
)
# A parenthetical that holds nothing but references: "(BG §1.1, #195)".
_REFS = re.compile(rf"\s*\(\s*{_REF}(?:\s*[,;]\s*{_REF})*\s*\)")
# A reference closing a parenthetical that says more: "(..., #352)".
_TRAILING_REF = re.compile(rf"(?:\s*[,;]\s*{_REF})+(?=\))")
# An inline pointer into the ledger: "a governed response's docs/verified-apis.md §3 headers".
_LEDGER_MENTION = re.compile(r"\s*docs/[\w./-]+\.md §[\d.]+")
_DOCS_LINK = re.compile(rf"^Docs:\s*({re.escape(SITE)}\S*)\s*$", re.MULTILINE)
def _outside_code(text: str, fn: Callable[[str], str]) -> str:
"""Apply ``fn`` to the parts of ``text`` that are not inline code spans."""
out: list[str] = []
last = 0
for match in _CODE_SPAN.finditer(text):
out.append(fn(text[last : match.start()]))
out.append(match.group(0))
last = match.end()
out.append(fn(text[last:]))
return "".join(out)
def _escape_mdx(text: str) -> str:
return re.sub(r"([{}<])", r"\\\1", text)
def rst_to_markdown(text: str) -> str:
"""One docstring paragraph as MDX-safe Markdown on a single line.
Sphinx roles become code spans (``:class:`~a.B``` → ```B```), double
backticks become single ones, reference-only parentheticals such as
``(BG §1.1, #195)`` are dropped, and ``{``, ``}`` and ``<`` outside code
are escaped so MDX does not read them as JSX.
"""
text = " ".join(text.split())
def role(match: re.Match[str]) -> str:
tilde, title, target = match.groups()
name = (target or title).strip()
if target is None and tilde:
name = name.rsplit(".", 1)[-1]
if target is not None:
name = title.strip()
return f"`{name}`"
text = _REFS.sub("", text)
text = _TRAILING_REF.sub("", text)
text = _LEDGER_MENTION.sub("", text)
text = _ROLE.sub(role, text)
text = re.sub(r"``(.+?)``", r"`\1`", text)
# A paragraph that introduces an example ends "...::" or "...:".
text = re.sub(r":+$", ".", text)
return _outside_code(text, _escape_mdx)
def _cell(text: str) -> str:
"""``text`` made safe for a Markdown table cell."""
return text.replace("|", "\\|")
def _table(header: Iterable[str], rows: Iterable[Iterable[str]]) -> str:
head = list(header)
lines = ["| " + " | ".join(head) + " |", "|" + "---|" * len(head)]
lines += ["| " + " | ".join(_cell(c) for c in row) + " |" for row in rows]
return "\n".join(lines)
def _summary(obj: object) -> str:
"""The first paragraph of ``obj``'s docstring, as Markdown."""
doc = inspect.getdoc(obj) or ""
first = doc.split("\n\n", 1)[0].strip()
return rst_to_markdown(first) if first else ""
def _docs_route(obj: object) -> str | None:
"""The site route a ``Docs: https://docs.donkey-kit.dev/...`` line names."""
match = _DOCS_LINK.search(inspect.getdoc(obj) or "")
if match is None:
return None
route = match.group(1)[len(SITE) :]
return route or "/"
# --- README.md -> python/README.md ----------------------------------------------
_MD_LINK = re.compile(r"(!?)\[([^\]]*)\]\(([^)\s]+)\)")
_HTML_ATTR = re.compile(r"""(\s(?:src|href)=")([^"]+)(")""")
_ABSOLUTE = re.compile(r"^(?:[a-z][a-z0-9+.-]*:|#)", re.I)
_RAW_URL = "https://raw.githubusercontent.com/Donkey-Development-Kit/donkey-development-kit"
def _absolute(target: str, *, image: bool) -> str:
if _ABSOLUTE.match(target):
return target
path = target.removeprefix("./")
if image:
return f"{_RAW_URL}/{DOCS_REF}/{path}"
kind = "tree" if path.split("#", 1)[0].endswith("/") else "blob"
return f"{REPO_URL}/{kind}/{DOCS_REF}/{path}"
def pypi_readme(root_readme: str) -> str:
"""The PyPI description: the repository README with absolute links.
Inline Markdown links and HTML ``src``/``href`` attributes are rewritten;
images go to ``raw.githubusercontent.com`` so PyPI can load them. Links in
fenced code blocks are left alone.
"""
out: list[str] = []
fenced = False
for line in root_readme.splitlines(keepends=True):
if line.lstrip().startswith(("```", "~~~")):
fenced = not fenced
if not fenced:
line = _MD_LINK.sub(
lambda m: (
f"{m.group(1)}[{m.group(2)}]({_absolute(m.group(3), image=bool(m.group(1)))})"
),
line,
)
line = _HTML_ATTR.sub(
lambda m: (
m.group(1) + _absolute(m.group(2), image="src=" in m.group(1)) + m.group(3)
),
line,
)
out.append(line)
text = "".join(out)
return text.rstrip("\n") + (
"\n\n<!-- Generated from the repository README.md by "
f"{_SCRIPT}; edit that file, then run the script. -->\n"
)
# --- configuration: settings index ----------------------------------------------
def _default_text(value: object) -> str:
if value is None:
return "unset"
if isinstance(value, bool):
return f"`{str(value).lower()}`"
return f"`{value}`"
def settings_index() -> str:
"""Every ``DonkeyConfig`` field from the field table, in its order."""
from donkey_kit.core import config
defaults = {f.name: _default_text(f.default) for f in dataclasses.fields(config.DonkeyConfig)}
# ``None`` on a header field means "send the built-in name", not "unset".
defaults.update((key, f"`{name}`") for key, name in config._HEADER_KEYS) # noqa: SLF001
defaults["base_url"] = "from `region`"
rows = [
(
f"`{spec.env}`",
f"`{spec.name}`",
f"`{spec.toml_key}`",
defaults[spec.name],
)
for spec in config._FIELDS # noqa: SLF001 (the one config table, as its test reads it)
]
intro = (
"Every `DonkeyConfig` field except `cost` (set per dimension, see\n"
"[Cost-attribution tags](#cost-attribution-tags)), in the order the SDK reads\n"
f"them. The config-file key goes in the `[donkey]` table of `{config.TOML_NAME}`.\n"
"This table is generated from the field table in `core/config.py`.\n"
)
header = ("Env var", "`DonkeyConfig` field", "Config-file key", "Default")
return intro + "\n" + _table(header, rows)
# --- CLI reference -------------------------------------------------------------
_TRAILING_REFS = re.compile(r"\s*\([^()]*\)\.?$")
def _command_summary(help_text: str | None) -> str:
first = (help_text or "").strip().split("\n\n", 1)[0]
first = " ".join(first.split())
first = _TRAILING_REFS.sub("", first).rstrip(".")
return rst_to_markdown(first + ".") if first else ""
def _option_default(param: Any) -> str: # noqa: ANN401 (a click.Parameter from typer)
if getattr(param, "is_flag", False):
return "off" if not param.default else "on"
default = param.default
if default is None or default == () or default == []:
return "none"
return f"`{default}`"
def _option_rows(params: Iterable[Any]) -> list[tuple[str, str, str]]:
rows: list[tuple[str, str, str]] = []
for param in params:
if getattr(param, "hidden", False) or not param.opts:
continue
flags = ", ".join(f"`{opt}`" for opt in param.opts)
meaning = rst_to_markdown(" ".join((param.help or "").split()))
meaning = meaning[:1].upper() + meaning[1:]
if meaning and not meaning.endswith("."):
meaning += "."
if param.multiple and "repeatable" not in meaning.lower():
meaning += " Repeatable."
rows.append((flags, _option_default(param), meaning.strip()))
return rows
def cli_reference() -> str:
"""Every visible command of ``donkey`` and its options, from the typer app."""
import typer.main
import donkey_kit.cli # noqa: F401 (registers every command on the app)
from donkey_kit.cli._app import app
group = typer.main.get_command(app)
parts = [
"Generated from the CLI itself; `donkey <command> --help` prints the same.",
"",
"Global options go before the command (`donkey --json init`):",
"",
_table(("Option", "Default", "Meaning"), _option_rows(group.params)),
]
commands = getattr(group, "commands", {})
for name in sorted(commands):
command = commands[name]
if command.hidden:
continue
parts += ["", f"`donkey {name}`: {_command_summary(command.help)}"]
rows = _option_rows(command.params)
if rows:
parts += ["", _table(("Option", "Default", "Meaning"), rows)]
else:
parts += ["", "No options of its own."]
return "\n".join(parts)
# --- framework adapters ----------------------------------------------------------
_CAPABILITY_FIELDS = ("transport", "sync", "streaming", "typed_refusals", "observes_last_call")
def _capability(value: object) -> str:
if isinstance(value, bool):
return "✅" if value else "❌"
return f"`{value}`"
def adapter_capabilities() -> str:
"""Each adapter factory's ``capabilities()``, from the adapter roster."""
from donkey_kit import integrations
rows: list[tuple[str, ...]] = []
for spec in integrations.ADAPTERS.values():
module = importlib.import_module(spec.module, integrations.__name__)
cls = getattr(module, spec.cls)
for factory, caps in cls.factories.items():
rows.append(
(
f"`donkey.{spec.attr}.{factory}()`",
f"`[{spec.extra}]`",
*(_capability(getattr(caps, field)) for field in _CAPABILITY_FIELDS),
)
)
header = ("Factory", "Extra", *(f"`{field}`" for field in _CAPABILITY_FIELDS))
return (
"What each factory reports, generated from the adapter roster and each\n"
"adapter's declared `factories`:\n\n" + _table(header, rows)
)
# --- framework verification status (#1081) ----------------------------------------
LEDGER = REPO / "docs" / "verified-apis.md"
_LEDGER_REF = "docs/verified-apis.md"
FRAMEWORK_PAGES = REPO / "website" / "content" / "frameworks"
#: The ledger's §8 Status values a docs label is derived from, by prefix. Any
#: other value is refused rather than mapped: a new status needs a human to
#: decide what the docs may claim for it (§0.3).
_LEDGER_STATUSES: tuple[tuple[str, str], ...] = (
("VERIFIED (LIVE)", "live-verified"),
("VERIFIED (CLI)", "live-verified"),
("UNVERIFIED (signature confirmed", "signature-confirmed"),
)
#: How each docs status reads: the inline badge text, its tone and the prose phrase.
_LABELS = {
"conformance-tested": ("Conformance-tested", "accent", "conformance-tested"),
"live-verified": ("Live-verified", "live", "live-verified"),
"signature-confirmed": ("Signature-confirmed", "neutral", "signature-confirmed offline"),
}
_PAGE_STATUS = {"live-verified": "live", "signature-confirmed": "offline-verified"}
#: The page badge of each frontmatter status (``STATUSES`` in ``website/lib/status.mjs``).
_PAGE_BADGE = {"live": ("Live", "live"), "offline-verified": ("Offline-verified", "neutral")}
#: Strongest first. A page that covers several factories is rated by its weakest.
_VERIFICATION_ORDER = ("live-verified", "signature-confirmed")
_PIPE = re.compile(r"(?<!\\)\|")
_FACTORY_CELL = re.compile(r"^`donkey\.(\w+)\.(\w+)\(\)`$")
_ROUTE_CELL = re.compile(r"^`(/frameworks/[\w-]+)`$")
def _table_rows(lines: Iterable[str]) -> list[list[str]]:
"""The body rows of the first Markdown table in ``lines``, as stripped cells."""
rows: list[list[str]] = []
started = False
for line in lines:
if not line.startswith("|"):
if started:
break
continue
started = True
cells = [c.strip() for c in _PIPE.split(line.strip())[1:-1]]
if all(set(c) <= set("-: ") for c in cells):
continue
rows.append(cells)
return rows[1:] # drop the header row
def ledger_framework_statuses(ledger: str) -> dict[str, str]:
"""The §8 table of the ledger as ``{Framework column: Status column}``."""
lines = ledger.splitlines()
start = [i for i, line in enumerate(lines) if line.startswith("## 8. ")]
if len(start) != 1:
raise GenerationError(f"{_LEDGER_REF}: expected one '## 8. ' heading")
out: dict[str, str] = {}
for cells in _table_rows(lines[start[0] + 1 :]):
if len(cells) < 3:
raise GenerationError(f"{_LEDGER_REF} §8: a row has fewer than 3 cells: {cells}")
name, status = cells[0], cells[2]
if name in out:
raise GenerationError(f"{_LEDGER_REF} §8: row {name!r} appears twice")
out[name] = status
return out
def verification_of(status: str) -> str:
"""The docs label for one §8 Status value; refuses one it does not know."""
for prefix, label in _LEDGER_STATUSES:
if status.startswith(prefix):
return label
raise GenerationError(
f"{_LEDGER_REF} §8: no docs label for status {status!r}; extend _LEDGER_STATUSES "
f"in {_SCRIPT} and the §8.4 mapping table together"
)
@dataclass(frozen=True)
class FactoryStatus:
"""One SDK factory's status, from the ledger and the adapter roster."""
framework: str
attr: str
factory: str
route: str
ledger_row: str
#: ``live-verified`` or ``signature-confirmed``, from the §8 Status column.
verification: str
#: ``AdapterSpec.conformance_tested`` of the factory's adapter.
conformance_tested: bool
#: Whether this is the adapter's default factory (the first in ``factories``).
default: bool
#: How many factories the adapter declares.
siblings: int
@property
def call(self) -> str:
return f"donkey.{self.attr}.{self.factory}()"
@property
def headline(self) -> str:
"""The badge a card or list shows: depth first, then verification."""
return "conformance-tested" if self.conformance_tested else self.verification
@property
def short_name(self) -> str:
"""The framework, plus the factory when the adapter has more than one."""
if self.siblings > 1:
return f"{self.framework} `{self.factory}()`"
return self.framework
def framework_statuses(ledger: str | None = None) -> list[FactoryStatus]:
"""Every SDK factory's status, in roster order (``docs/verified-apis.md`` §8.4).
The roster maps each factory to its §8 row and docs page; the status is
that row's Status column, and the depth is the adapter's
``conformance_tested``. Raises :class:`GenerationError` when the roster and
``ADAPTERS`` list different factories, or a row names a §8 row or page that
does not exist.
"""
from donkey_kit import integrations
text = LEDGER.read_text(encoding="utf-8") if ledger is None else ledger
statuses = ledger_framework_statuses(text)
lines = text.splitlines()
begin = [i for i, line in enumerate(lines) if "BEGIN ROSTER: framework-status" in line]
end = [i for i, line in enumerate(lines) if "END ROSTER: framework-status" in line]
if len(begin) != 1 or len(end) != 1 or end[0] < begin[0]:
raise GenerationError(f"{_LEDGER_REF}: expected one framework-status roster (§8.4)")
factories: dict[tuple[str, str], tuple[Any, bool, int]] = {}
for spec in integrations.ADAPTERS.values():
module = importlib.import_module(spec.module, integrations.__name__)
names = list(getattr(module, spec.cls).factories)
for index, name in enumerate(names):
factories[(spec.attr, name)] = (spec, index == 0, len(names))
out: list[FactoryStatus] = []
for cells in _table_rows(lines[begin[0] + 1 : end[0]]):
if len(cells) != 4:
raise GenerationError(f"{_LEDGER_REF} §8.4: a roster row needs 4 cells: {cells}")
framework, call, row, route_cell = cells
call_match = _FACTORY_CELL.match(call)
route_match = _ROUTE_CELL.match(route_cell)
if call_match is None or route_match is None:
raise GenerationError(f"{_LEDGER_REF} §8.4: unreadable roster row {cells}")
key = (call_match.group(1), call_match.group(2))
if key not in factories:
raise GenerationError(f"{_LEDGER_REF} §8.4: {call} is not a factory in ADAPTERS")
if row not in statuses:
raise GenerationError(f"{_LEDGER_REF} §8.4: {call} names no §8 row {row!r}")
route = route_match.group(1)
if not (REPO / "website" / "content" / f"{route.lstrip('/')}.mdx").is_file():
raise GenerationError(f"{_LEDGER_REF} §8.4: {call} names a missing page {route}")
spec, default, siblings = factories[key]
out.append(
FactoryStatus(
framework=framework,
attr=key[0],
factory=key[1],
route=route,
ledger_row=row,
verification=verification_of(statuses[row]),
conformance_tested=spec.conformance_tested,
default=default,
siblings=siblings,
)
)
seen = [(s.attr, s.factory) for s in out]
if len(seen) != len(set(seen)):
raise GenerationError(f"{_LEDGER_REF} §8.4: a factory is listed twice")
missing = sorted(f"donkey.{a}.{f}()" for a, f in set(factories) - set(seen))
if missing:
raise GenerationError(f"{_LEDGER_REF} §8.4: no roster row for {', '.join(missing)}")
for attr in {s.attr for s in out}:
rows = [s for s in out if s.attr == attr]
if len({(s.framework, s.route) for s in rows}) != 1:
raise GenerationError(
f"{_LEDGER_REF} §8.4: donkey.{attr} factories name different frameworks or pages"
)
return out
def _weakest(verifications: Iterable[str]) -> str:
return max(verifications, key=_VERIFICATION_ORDER.index)
def page_statuses(rows: list[FactoryStatus]) -> dict[str, str]:
"""Each framework page's frontmatter ``status:``, by route.
A framework page is rated by its adapter's default factory, the surface
its main code uses; the ``/frameworks`` overview by the weakest factory.
"""
out = {s.route: _PAGE_STATUS[s.verification] for s in rows if s.default}
out["/frameworks"] = _PAGE_STATUS[_weakest(s.verification for s in rows)]
return out
def _badge(label: str) -> str:
text, tone, _ = _LABELS[label]
return f'<Badge tone="{tone}">{text}</Badge>'
def _join(names: list[str]) -> str:
if len(names) <= 1:
return "".join(names)
return ", ".join(names[:-1]) + " and " + names[-1]
def framework_status_table(*, mdx: bool) -> str:
"""One row per SDK factory: its depth and its ledger status."""
rows = framework_statuses()
ledger_link = (
f"[ledger §8]({REPO_URL}/blob/{DOCS_REF}/{_LEDGER_REF})"
if mdx
else f"[`{_LEDGER_REF}` §8]({_LEDGER_REF})"
)
def label(name: str) -> str:
return _badge(name) if mdx else f"**{_LABELS[name][0]}**"
body = [
(
s.framework if not mdx else f"[{s.framework}]({s.route})",
f"`{s.call}`",
label("conformance-tested") if s.conformance_tested else "`connection_kwargs()`",
label(s.verification),
)
for s in rows
]
intro = (
f"Each factory's status is its constructor row in the {ledger_link}; its\n"
"depth is the adapter roster's `conformance_tested`.\n\n"
)
return intro + _table(("Framework", "Factory", "Depth", "Ledger status"), body)
def framework_status_summary() -> str:
"""The per-factory statuses as two sentences of prose."""
rows = framework_statuses()
deep = list(dict.fromkeys(s.framework for s in rows if s.conformance_tested))
if deep:
verb = "is" if len(deep) == 1 else "are"
first = f"{_join(deep)} {verb} conformance-tested against the simulator in CI."
else:
first = "No adapter is conformance-tested against the simulator."
groups = {v: [s for s in rows if s.verification == v] for v in _VERIFICATION_ORDER}
groups = {v: g for v, g in groups.items() if g}
common = max(groups, key=lambda v: len(groups[v]))
others = [v for v in groups if v != common]
second = (
f"Each adapter factory's constructor row in §8 of the ledger reads **{_LABELS[common][2]}**"
)
if others:
parts = []
for v in others:
names = [
f"{s.framework}'s `{s.factory}()`" if s.siblings > 1 else s.framework
for s in groups[v]
]
verb = "which is" if len(names) == 1 else "which are"
parts.append(f"{_join(names)}, {verb} **{_LABELS[v][2]}**")
second += ", except " + "; and ".join(parts)
return f"{first}\n{second}."
def _frontmatter_status(text: str, status: str, where: str) -> str:
lines = text.splitlines(keepends=True)
if not lines or lines[0].strip() != "---":
raise GenerationError(f"{where}: no frontmatter")
for i, line in enumerate(lines[1:], start=1):
if line.strip() == "---":
break
if line.startswith("status:"):
lines[i] = f"status: {status}\n"
return "".join(lines)
raise GenerationError(f"{where}: no 'status:' line in the frontmatter")
_CARD = re.compile(r'(<Card\b[^>]*?\bhref="(?P<href>[^"]+)"[^>]*>)')
_CARD_BADGE = re.compile(r'\bbadge="[^"]*"(\s+)badgeTone="[^"]*"')
def _card_badges(text: str, badges: dict[str, tuple[str, str]], where: str) -> str:
"""``text`` with the ``badge``/``badgeTone`` of each ``<Card href=...>`` in ``badges``."""
seen: list[str] = []
def card(match: re.Match[str]) -> str:
href = match.group("href")
if href not in badges:
return match.group(0)
seen.append(href)
badge, tone = badges[href]
tag, count = _CARD_BADGE.subn(
lambda m: f'badge="{badge}"{m.group(1)}badgeTone="{tone}"', match.group(0)
)
if count != 1:
raise GenerationError(f'{where}: the {href} card needs badge="…" badgeTone="…"')
return tag
out = _CARD.sub(card, text)
for href in badges:
if seen.count(href) != 1:
raise GenerationError(f'{where}: expected one <Card href="{href}">')
return out
def _replace_row(text: str, prefix: str, row: str, where: str) -> str:
lines = text.splitlines(keepends=True)
hits = [i for i, line in enumerate(lines) if line.startswith(prefix)]
if len(hits) != 1:
raise GenerationError(f"{where}: expected one table row starting {prefix!r}")
lines[hits[0]] = row + "\n"
return "".join(lines)
def _rel(path: Path) -> str:
return str(path.relative_to(REPO))
def framework_page(path: Path) -> str:
"""A framework page with its frontmatter ``status:`` from its factories."""
route = f"/frameworks/{path.stem}" if path.stem != "index" else "/frameworks"
statuses = page_statuses(framework_statuses())
if route not in statuses:
raise GenerationError(
f"{_rel(path)}: no factory in {_LEDGER_REF} §8.4 names {route}; add its roster row"
)
return _frontmatter_status(path.read_text(encoding="utf-8"), statuses[route], _rel(path))
def frameworks_index() -> str:
"""``frameworks/index.mdx``: its status, the cards' badges and the status table."""
path = FRAMEWORK_PAGES / "index.mdx"
rows = framework_statuses()
badges = {}
for s in rows:
if s.default:
text, tone, _ = _LABELS[s.headline]
badges[s.route] = (text, tone)
text = replace_region(
framework_page(path), "framework-status", framework_status_table(mdx=True)
)
return _card_badges(text, badges, _rel(path))
_QUICKSTART_ROW = "| Framework adapters for "
def quickstart() -> str:
"""``quickstart.mdx`` with its framework-adapters row from the factories' statuses."""
path = REPO / "website" / "content" / "quickstart.mdx"
rows = framework_statuses()
adapters = len({s.attr for s in rows})
groups: list[tuple[str, list[FactoryStatus]]] = []
for label in ("conformance-tested", *_VERIFICATION_ORDER):
members = [s for s in rows if s.headline == label]
if members:
groups.append((label, members))
largest = max(range(len(groups)), key=lambda i: len(groups[i][1]))
parts = []
for index, (label, members) in enumerate(groups):
if len(groups) == 1:
names = "all of them"
elif index == largest:
names = "the rest"
else:
names = _join(list(dict.fromkeys(s.short_name for s in members)))
parts.append(f"{_badge(label)} {names}")
if largest != len(groups) - 1:
parts.append(parts.pop(largest))
row = (
f"{_QUICKSTART_ROW}{adapters} frameworks | {' · '.join(parts)} | "
"[Frameworks](/frameworks#supported-frameworks) |"
)
return _replace_row(path.read_text(encoding="utf-8"), _QUICKSTART_ROW, row, _rel(path))
def feature_overview() -> str:
"""``feature-overview.mdx`` with its framework card badge from the overview page's status."""
path = REPO / "website" / "content" / "feature-overview.mdx"
status = page_statuses(framework_statuses())["/frameworks"]
text = path.read_text(encoding="utf-8")
return _card_badges(text, {"/frameworks": _PAGE_BADGE[status]}, _rel(path))
def _with_region(path: Path, region: str, render: Callable[[], str]) -> Callable[[], str]:
def rendered() -> str:
return replace_region(path.read_text(encoding="utf-8"), region, render())
return rendered
# --- API reference -------------------------------------------------------------
def _annotation(value: object) -> str:
if value is inspect.Parameter.empty:
return ""
if isinstance(value, str):
return value
return str(inspect.formatannotation(value))
def _default(value: object) -> str:
text = repr(value)
return "..." if " at 0x" in text else text
def _param(param: inspect.Parameter) -> str:
text = param.name
if param.kind is inspect.Parameter.VAR_POSITIONAL:
text = "*" + text
elif param.kind is inspect.Parameter.VAR_KEYWORD:
text = "**" + text
annotation = _annotation(param.annotation)
if annotation:
text += f": {annotation}"
if param.default is not param.empty:
text += f" = {_default(param.default)}" if annotation else f"={_default(param.default)}"
return text
def format_signature(call: str, func: Callable[..., Any], *, bound: bool) -> str:
"""``call(params) -> return``, one parameter per line when it runs long.
A coroutine function is shown as ``async call(...)``, and a function
wrapped by ``@contextmanager``/``@asynccontextmanager`` with the context
manager it returns rather than its generator's annotation.
``bound`` drops the first parameter (``self`` or ``cls``). Annotations are
printed as written in the source, so the output does not depend on which
optional packages are installed.
"""
signature = inspect.signature(func)
params = list(signature.parameters.values())[1 if bound else 0 :]
parts: list[str] = []
star = False
for index, param in enumerate(params):
if param.kind is param.VAR_POSITIONAL:
star = True
if param.kind is param.KEYWORD_ONLY and not star:
parts.append("*")
star = True
parts.append(_param(param))
if param.kind is param.POSITIONAL_ONLY and (
index + 1 == len(params) or params[index + 1].kind is not param.POSITIONAL_ONLY
):
parts.append("/")
returns = _annotation(signature.return_annotation)
namespace = getattr(inspect.unwrap(func), "__globals__", {})
parts = [_public_type_vars(part, namespace) for part in parts]
returns = _public_type_vars(returns, namespace)
wrapped = getattr(func, "__wrapped__", None)
if inspect.isasyncgenfunction(wrapped):
returns = _context_manager("AbstractAsyncContextManager", returns)
elif inspect.isgeneratorfunction(wrapped):
returns = _context_manager("AbstractContextManager", returns)
if inspect.iscoroutinefunction(func):
call = f"async {call}"
tail = f" -> {returns}" if returns else ""
one_line = f"{call}({', '.join(parts)}){tail}"
if len(one_line) <= 88:
return one_line
inner = "".join(f" {part},\n" for part in parts)
return f"{call}(\n{inner}){tail}"
_PRIVATE_NAME = re.compile(r"\b_[A-Za-z]\w*")
def _public_type_vars(text: str, namespace: dict[str, Any]) -> str:
"""``text`` with private type-variable names shown without the underscore.
``Callable[_P, _R]`` reads as ``Callable[P, R]``; a private name that is not
a ``TypeVar`` or ``ParamSpec`` in the function's module is left alone.
"""
def public(match: re.Match[str]) -> str:
name = match.group(0)
is_var = isinstance(namespace.get(name), (typing.TypeVar, typing.ParamSpec))
return name[1:] if is_var else name
return _PRIVATE_NAME.sub(public, text)
_YIELDS = re.compile(r"^(?:[\w.]+\.)?(?:Async)?(?:Iterator|Generator)\[([^,\]]+)")
def _context_manager(kind: str, returns: str) -> str:
"""The type a ``@contextmanager`` function returns, from its generator annotation.
``inspect.signature`` follows ``__wrapped__`` to the generator, whose
annotation (``AsyncIterator[None]``) is not what a caller gets back.
"""
match = _YIELDS.match(returns)
return f"{kind}[{match.group(1).strip() if match else 'Any'}]"
@dataclass(frozen=True)
class _ClassDoc:
cls: type
#: How code reaches an instance: ``donkey``, ``donkey.llm``, ...
instance: str
title: str
intro: str = ""
#: Members left out (documented elsewhere, or not for callers).
skip: frozenset[str] = frozenset()
#: Whether to show the constructor.
constructor: bool = False
def _members(cls: type, skip: frozenset[str]) -> list[tuple[str, object]]:
return [
(name, obj)
for name, obj in vars(cls).items()
if not name.startswith("_") and name not in skip and not _is_data(obj)
]
def _is_data(obj: object) -> bool:
return not (isinstance(obj, (property, classmethod, staticmethod)) or inspect.isfunction(obj))
#: Signatures are not runnable Python, so the snippet check skips them (#797).
_SIGNATURE_FENCE = "```python nocheck"
def _member_section(doc: _ClassDoc, name: str, obj: object) -> list[str]:
owner = doc.cls.__name__
if isinstance(obj, property):
assert obj.fget is not None
returns = _annotation(inspect.signature(obj.fget).return_annotation)
heading = f"`{doc.instance}.{name}`"
code = f"{doc.instance}.{name}: {returns}" if returns else f"{doc.instance}.{name}"
target: object = obj
elif isinstance(obj, classmethod):
heading = f"`{owner}.{name}()`"
code = format_signature(f"{owner}.{name}", obj.__func__, bound=True)
target = obj.__func__
elif isinstance(obj, staticmethod):
heading = f"`{doc.instance}.{name}()`"
code = format_signature(f"{doc.instance}.{name}", obj.__func__, bound=False)
target = obj.__func__
else:
assert callable(obj)
heading = f"`{doc.instance}.{name}()`"
code = format_signature(f"{doc.instance}.{name}", obj, bound=True)
target = obj
lines = ["", f"### {heading}", "", _SIGNATURE_FENCE, code, "```", ""]
summary = _summary(target)
if summary:
lines.append(summary)
route = _docs_route(target)
if route:
lines += ["", f"Guide: [`{route}`]({route})"]
return lines
def _class_section(doc: _ClassDoc) -> list[str]:
lines = ["", f"## {doc.title}", ""]
summary = _summary(doc.cls)
if summary:
lines.append(summary)
if doc.intro:
lines += ["", doc.intro]
if doc.constructor:
init = vars(doc.cls)["__init__"]
lines += [
"",
_SIGNATURE_FENCE,
format_signature(doc.cls.__name__, init, bound=True),
"```",
]
for name, obj in _members(doc.cls, doc.skip):
lines += _member_section(doc, name, obj)
return lines
def _export_summary(obj: object) -> str:
if typing.get_origin(obj) is typing.Literal:
return "One of " + ", ".join(f"`{value}`" for value in typing.get_args(obj)) + "."
return _summary(obj)
def _kind(obj: object) -> str:
if inspect.isclass(obj):
if issubclass(obj, BaseException):
return "exception"
return "class"
if inspect.isfunction(obj):
return "function"
# An SDK instance with methods, e.g. ``classify`` (#210); typing aliases
# are callable too, so the test is the instance's own type.
if type(obj).__module__.startswith("donkey_kit.") and callable(obj):
return "callable object"
return "type alias"
def api_reference() -> str:
"""The whole ``reference/api.mdx`` page."""
import donkey_kit
from donkey_kit import Budget, Donkey, DonkeyConfig, LastCall, LLMClient, integrations
docs = [
_ClassDoc(
Donkey,
"donkey",
"`Donkey`",
intro=(
"Each framework adapter is an attribute too, listed under\n"
"[Framework adapters](#framework-adapters)."
),
constructor=True,
),
_ClassDoc(