Skip to content

Commit e471664

Browse files
docs: one page for a coding assistant, and a copy-page control on every page (#42)
1 parent d6257d6 commit e471664

13 files changed

Lines changed: 1135 additions & 1 deletion

File tree

‎.github/PULL_REQUEST_TEMPLATE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
- [ ] Tests added or updated
1616
- [ ] `make check` passes locally (`ruff` + `mypy`)
1717
- [ ] `CHANGELOG.md` updated under `[Unreleased]`
18-
- [ ] Documentation updated (if the public API changed)
18+
- [ ] Documentation updated (if the public API changed), `docs/agents.md` included
1919

2020
## Related issues
2121

‎.github/workflows/docs.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
- run: uv sync --no-dev --group docs
1919
- run: cp CHANGELOG.md docs/changelog.md
2020
- run: uv run zensical build --clean
21+
- run: uv run python scripts/emit_markdown.py
2122
- uses: actions/upload-pages-artifact@v5
2223
with:
2324
path: site

‎CONTRIBUTING.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ Breaking changes: add `!` after the type (`feat!:`) or include a `BREAKING CHANG
9797
4. Run `make check && make test-unit` locally
9898
5. Open a PR against `master`
9999

100+
## The agents page
101+
102+
`docs/agents.md` is the whole library on one page, written for a coding assistant: the
103+
public API, the startup and shutdown ordering that breaks code when it is broken, which
104+
batteries are opt-in, the mistakes models make, and a map of which page to fetch for the
105+
rest. People hand it to an assistant instead of the site, which is what makes a stale one
106+
worse than none — it teaches a model an API that no longer exists.
107+
108+
It is part of the public API, so it changes in the same pull request the API does: a name
109+
added, renamed or removed, a changed default or signature, a new rule a caller has to obey,
110+
a new extra. A new docs page means a new row in the documentation map. The review check is
111+
mechanical — if the diff changes the public surface and `docs/agents.md` is untouched, the
112+
pull request is not finished.
113+
100114
## Releasing (maintainers only)
101115

102116
Releases are fully automated via [Release Please](https://github.com/googleapis/release-please).

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ core: a cron job and an HTTP request are the same thing — one unit of work in
1717
This is the model behind .NET Generic Host, Spring `SmartLifecycle` and go-kratos
1818
`transport.Server`, adapted to async Python.
1919

20+
> [!TIP]
21+
> **Building this with an AI assistant?** Hand it
22+
> **[one page](https://bedrock-python.github.io/servicewright/agents/)** instead of the
23+
> whole site: the public API surface, the startup and shutdown ordering that breaks code
24+
> when it is broken, which batteries are opt-in and silent when they are not switched on,
25+
> the mistakes models actually make with this API, and a map of which page to fetch for the
26+
> rest. Every docs page is also served as raw Markdown at its own URL, and a **Copy page**
27+
> button at the top of each one hands it straight to a chat window.
28+
2029
## Why
2130

2231
- **One lifecycle for every archetype**: Bootstrap → Warmup → Ready → Serve → Drain → Cleanup.
@@ -157,6 +166,7 @@ Full documentation: **[bedrock-python.github.io/servicewright](https://bedrock-p
157166

158167
| | |
159168
| --- | --- |
169+
| [For AI agents](https://bedrock-python.github.io/servicewright/agents/) | the whole library on one page, written for a coding assistant |
160170
| [Your first service](https://bedrock-python.github.io/servicewright/getting-started/first-service/) | a complete service with nothing installed but the kernel |
161171
| [Tutorial](https://bedrock-python.github.io/servicewright/getting-started/tutorial/) | an HTTP API and a cron job in one process |
162172
| [Architecture](https://bedrock-python.github.io/servicewright/concepts/architecture/) | the six nouns, the two layers, the dependency rule |

‎docs/agents.md‎

Lines changed: 579 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* Behaviour for the "Copy page" control.
2+
*
3+
* Every handler is delegated from `document`, because the theme swaps the
4+
* content in place when instant navigation is on: a listener bound to an
5+
* element of one page would not survive the move to the next.
6+
*/
7+
(function () {
8+
"use strict";
9+
10+
var RESET_AFTER_MS = 2000;
11+
12+
/* Where the Markdown of a page is written, as an absolute URL. Two data
13+
attributes say how far the site root is from here and where this page sits
14+
below it; scripts/emit_markdown.py writes the file to match. The site's own
15+
name is a third, so this file is the same in every project that carries it. */
16+
function markdownUrl(widget) {
17+
var base = (widget.dataset.copyBase || ".").replace(/\/$/, "");
18+
var page = widget.dataset.copyPage || "";
19+
var relative = page === "" ? "index.md" : page.replace(/\/$/, "") + ".md";
20+
return new URL(base + "/" + relative, window.location.href).href;
21+
}
22+
23+
function prompt(widget) {
24+
var title = widget.dataset.copyTitle || document.title;
25+
var site = widget.dataset.copySite || "project";
26+
return (
27+
"Read " +
28+
markdownUrl(widget) +
29+
' -- the "' +
30+
title +
31+
'" page of the ' +
32+
site +
33+
" documentation -- so I can ask questions about it."
34+
);
35+
}
36+
37+
function destination(widget, name) {
38+
var question = encodeURIComponent(prompt(widget));
39+
switch (name) {
40+
case "markdown":
41+
return markdownUrl(widget);
42+
case "chatgpt":
43+
return "https://chatgpt.com/?hints=search&q=" + question;
44+
case "claude":
45+
return "https://claude.ai/new?q=" + question;
46+
case "perplexity":
47+
return "https://www.perplexity.ai/search?q=" + question;
48+
default:
49+
return markdownUrl(widget);
50+
}
51+
}
52+
53+
function write(text) {
54+
if (navigator.clipboard && window.isSecureContext) {
55+
return navigator.clipboard.writeText(text);
56+
}
57+
/* Insecure origins have no clipboard API; the old selection dance still
58+
works there, which keeps a local preview usable. */
59+
return new Promise(function (resolve, reject) {
60+
var area = document.createElement("textarea");
61+
area.value = text;
62+
area.style.position = "fixed";
63+
area.style.opacity = "0";
64+
document.body.appendChild(area);
65+
area.select();
66+
var ok = document.execCommand("copy");
67+
document.body.removeChild(area);
68+
ok ? resolve() : reject(new Error("copy refused"));
69+
});
70+
}
71+
72+
function announce(widget, label, state) {
73+
var slot = widget.querySelector("[data-copy-label]");
74+
if (slot) slot.textContent = label;
75+
if (state) {
76+
widget.dataset.copyState = state;
77+
} else {
78+
delete widget.dataset.copyState;
79+
}
80+
}
81+
82+
function copy(widget) {
83+
var reset = function () {
84+
window.setTimeout(function () {
85+
announce(widget, "Copy page", null);
86+
}, RESET_AFTER_MS);
87+
};
88+
fetch(markdownUrl(widget))
89+
.then(function (response) {
90+
if (!response.ok) throw new Error(String(response.status));
91+
return response.text();
92+
})
93+
.then(write)
94+
.then(function () {
95+
announce(widget, "Copied", "copied");
96+
reset();
97+
})
98+
.catch(function () {
99+
announce(widget, "Copy failed", null);
100+
reset();
101+
});
102+
}
103+
104+
function close(widget) {
105+
var menu = widget.querySelector("[data-copy-menu]");
106+
var toggle = widget.querySelector("[data-copy-toggle]");
107+
if (menu) menu.hidden = true;
108+
if (toggle) toggle.setAttribute("aria-expanded", "false");
109+
}
110+
111+
function closeAll(except) {
112+
var widgets = document.querySelectorAll(".md-copy-page");
113+
for (var i = 0; i < widgets.length; i++) {
114+
if (widgets[i] !== except) close(widgets[i]);
115+
}
116+
}
117+
118+
document.addEventListener("click", function (event) {
119+
var target = event.target;
120+
if (!(target instanceof Element)) return;
121+
122+
var widget = target.closest(".md-copy-page");
123+
if (!widget) {
124+
closeAll(null);
125+
return;
126+
}
127+
closeAll(widget);
128+
129+
if (target.closest("[data-copy-action]")) {
130+
event.preventDefault();
131+
close(widget);
132+
copy(widget);
133+
return;
134+
}
135+
136+
var toggle = target.closest("[data-copy-toggle]");
137+
if (toggle) {
138+
event.preventDefault();
139+
var menu = widget.querySelector("[data-copy-menu]");
140+
if (!menu) return;
141+
/* The destinations are filled in on the way out rather than at load:
142+
the page under the widget may have changed since. */
143+
var links = menu.querySelectorAll("[data-copy-open]");
144+
for (var i = 0; i < links.length; i++) {
145+
links[i].href = destination(widget, links[i].dataset.copyOpen);
146+
}
147+
menu.hidden = !menu.hidden;
148+
toggle.setAttribute("aria-expanded", menu.hidden ? "false" : "true");
149+
return;
150+
}
151+
152+
if (target.closest("[data-copy-open]")) close(widget);
153+
});
154+
155+
document.addEventListener("keydown", function (event) {
156+
if (event.key === "Escape") closeAll(null);
157+
});
158+
})();

0 commit comments

Comments
 (0)