@miadi/connector 0.1.43 → 0.1.45

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.
package/README.md CHANGED
@@ -22,9 +22,9 @@ Call `get_capabilities` first. It names the signed-in person, their Miadi API ti
22
22
 
23
23
  | kind | tools |
24
24
  |---|---|
25
- | reads | `get_capabilities` `get_context` `list_circles` `get_circle` `list_sessions` `search_sessions` `get_session` `get_evidence` `list_episode_comments` `read_episode_file` `get_evaluations` `get_narrative_beats` `review_lookup` `review_versions` `review_transcript_status` `handoff_status` `list_records` `get_run_result` `mission_status` `provider_capabilities` `mission_model_preflight` `search` `fetch` `list_research_artifacts` `read_research_artifact` |
25
+ | reads | `get_capabilities` `get_context` `list_circles` `get_circle` `list_sessions` `search_sessions` `get_session` `get_evidence` `list_episode_comments` `read_episode_file` `get_evaluations` `get_narrative_beats` `review_lookup` `review_versions` `review_transcript_status` `get_screenwalk` `list_screenwalks` `handoff_status` `list_records` `get_run_result` `mission_status` `provider_capabilities` `mission_model_preflight` `search` `fetch` `list_research_artifacts` `read_research_artifact` |
26
26
  | contributions | `ensure_review_transcript` `append_observation` `comment_on_episode` `open_inquiry` `link_records` `propose_narrative_beat` `record_evaluation` `register_source_intent` `record_handoff` `update_handoff` `prepare_calendar_event` `save_research` |
27
- | requests (approved by a person) | `propose_work` `propose_session_completion` `request_authorized_run` `request_session_steer` `revoke_session_steering` `request_review_operation` `request_deep_diver` `link_review_to_episode` `request_inquiry_episode` |
27
+ | requests (approved by a person) | `propose_work` `propose_session_completion` `request_authorized_run` `request_session_steer` `revoke_session_steering` `request_review_operation` `request_screenwalk_review` `request_deep_diver` `link_review_to_episode` `request_inquiry_episode` |
28
28
  | decisions in the conversation | `show_decisions` (cards), `decide_request` (called by the card only) |
29
29
 
30
30
  - **Reads** need Miadi's read tier and stay within it. A person Miadi gives no API tier reaches only `get_capabilities`. Sessions need the write tier, because a session's name and pane are operator data. For the same reason, below the write tier a record someone else made shows no session, working folder or seat, and no long bodies.
@@ -59,6 +59,57 @@ A review is processed from the words of its video, so the connector acquires the
59
59
  - **A worker's door.** A factory worker's account holds no Miadi API tier, so it cannot call the tools. `node packages/connector/scripts/ensure-transcript.mjs <review id> --mission run_request:<id> [--verify]` runs the same job under an approved run or work proposal that names the review (a person with the write tier approved it, and the job records the mission and who approved it). `--status` reads the state without a mission. A second process's attempt is left alone until it has written no stage for five minutes.
60
60
  - **The host's reading runtime.** `get_capabilities` loads the module `MIADI_READING_ADAPTER` names (`scripts/host-reading-runtime.mjs` in the source tree) and reports `reading_workflow.provider_runtime` from what it finds: `verified_transcript_only` while only caption acquisition has a provider. A reading acquires its transcript with the same reader and the same language rule, keeps durable stage receipts, and stops at the generation stages with a named blocker. Acquiring a transcript does not depend on the review service's import handshake.
61
61
 
62
+ ### Registered screenwalks
63
+
64
+ A screenwalk registered in the Screenwalk Registry is read with `get_screenwalk`, by its registry id (`2612021410`), by `miadi-screenwalk:<id>`, or by its page URL on this factory (`<Miadi's public address>/screenwalks/<id>`). `list_screenwalks` lists the registrations a person may see, by the day they were registered, status, coverage, a name or a video (jgwill/Miadi#1180).
65
+
66
+ Three owners stay apart. The registry owns the registration. The connector gives authorized access to it. A review uses a registered video as its source, under a separate decision.
67
+
68
+ - **Whose authority.** Every call goes to Miadi's registry door (`/api/screenwalks/registrations`) with the signed-in person's own token. The registry's rights decide what is read: a person who may review or steward registrations, or who facilitates a circle of the episode. The connector holds no registry credential. A registration that does not exist and one out of reach answer the same `not_found`.
69
+ - **What a reference may be.** A URL is read only for the id it carries, and only when it is on Miadi's public address or the address the connector calls. Nothing is fetched from an address a caller supplies. A screenwalk is named by its registry id: the episode that keeps its file is never used to find it.
70
+ - **What comes back.** The registration in the registry's own schema, with its registry id and page, the canonical YouTube URL and video id, the safe title, title and description with where they stand (`proposed`, `needs_review`, `approved`…), the revision, who registered it, and counts of what it holds (question sets and their states, segments, purpose lists and items, relations by kind). `parts` keeps the top-level parts named, `purpose` one purpose list. `format: "brief"` gives the registry's Markdown brief and `format: "moments"` its moment index. Every answer says which door was read, as whom, when and at which revision.
71
+ - **Large registrations.** A selection larger than `max_bytes` (default 100,000) comes as pages of its JSON text with the total size and sha256. A page read against another revision is refused. Nothing is cut silently.
72
+ - **What it is not.** The inventory is the registry's record of what was asked about a video. It is not a review and not a transcript, and no tool copies it into one. The registry records no validation verdict, and the answer says so.
73
+
74
+ `request_screenwalk_review` is the one step from a registration to its Miadi Review, safe to repeat:
75
+
76
+ 1. It reads the registration as the person and takes the video's YouTube id.
77
+ 2. It searches the Miadi Review Service for that id (`GET /api/reviews/search`, exact match on `video_id`). A search that cannot run answers `lookup_unavailable` and writes nothing.
78
+ 3. One review: no review is created. The relation is recorded on both sides (below) and the review is returned.
79
+ 4. No review: one `review_operation_request` (`create`) is prepared for the canonical video, with `body.source` naming the registration's reference, the revision read and the matching key. It waits for a person like every review operation. Asking again, as anyone, returns that request. Once it is approved and the review exists, asking again records the relation.
80
+ 5. Several reviews: `ambiguous`, with the candidates. Nothing is written.
81
+
82
+ The relation between a registration and a review uses two records that already existed, and no new kind:
83
+
84
+ | direction | where | record |
85
+ |---|---|---|
86
+ | review to registration | the connector's records | a `link` from `miadi-screenwalk:<id>` to `miadi-review:<uuid>`, relation `review_created_from_source` (the name these records already gave a source and its review). `review_lookup` returns it as `screenwalks`, and Miadi's `/reviews/<id>` lists it |
87
+ | registration to review | the registration | a `correlates` relation to `miadi-review://<uuid>`, written with the registry's own `relate` act as the person. It needs the right to steward registrations. Without it the answer says `not_recorded` and the link above still stands |
88
+
89
+ The Review Service itself stores no source relation, and the registry has no relation kind that means "its review": `correlates` is its kind for an external record that points at the same work.
90
+
91
+ A step that asks for a review when a screenwalk is registered would call `request_screenwalk_review` from Miadi's registry door, after the registration lands, with the registrant's token (`lib/connector-tool.ts` `callConnectorTool`). That step is not built.
92
+
93
+
94
+ ### When a client's tool list is older than the connector
95
+
96
+ A client keeps the tool list it fetched. ChatGPT keeps it until a person refreshes the app, and a terminal client keeps it until it reconnects. After a release, `get_capabilities` names tools such a client cannot call yet. The connector says so itself (jgwill/Miadi#1180).
97
+
98
+ **What is recorded.** At each `tools/list`, the connector writes the version that answered and the tool names it carried to `<state dir>/client-tools.json`. The record is kept per OAuth client id. A client with no client id (a person token used directly) is given an `Mcp-Session-Id` at `initialize` and its record is kept under that id, and the endpoint keeps no session for it. Each record also counts the lists answered and the tool calls made since the last one. The file holds client ids, session ids, versions, tool names, counts and times, and no token or person. Sessions idle for 30 days leave it, and at most 300 are kept.
99
+
100
+ **What `get_capabilities` answers.** When the client's last list is this version's, nothing more. Otherwise a `client_tools` block, before the `tools` map:
101
+
102
+ | the client's last list | `state` | `list_fetched_at_version` | `missing_from_your_list` | the `tools` map |
103
+ |---|---|---|---|---|
104
+ | from another version | `list_older_than_this_version` | that version | the names this version offers that the list lacks, which can be none | each missing tool says "not in your client's tool list" before its status |
105
+ | not recorded | `list_not_recorded` | `unknown` | `unknown`, with `added_by_version`: the tools each recent version added | as it is |
106
+
107
+ The block also carries `refresh`, one sentence for a person (for ChatGPT: Settings, Apps, this app, Refresh), and `until_refresh`: for a tool the list lacks, an older tool that reaches part of the same matter, with what it does and does not do. Today these are `get_context` with a `miadi-screenwalk:<id>` ref in place of `get_screenwalk`, and `review_lookup` in place of `request_screenwalk_review`. The block says what the server offers and what the recorded list holds. It never says that a client can call a tool.
108
+
109
+ A request with neither a client id nor a session id (one bare call from a script) has no record, so it is answered `list_not_recorded` each time.
110
+
111
+ **At a release that adds a tool.** Give the tool an entry in `TOOL_ADDED` (`src/client-tools.ts`) with the version that adds it, and a `meanwhile` when an older tool reaches part of it. `test/client-tools.test.mjs` fails until the entry exists. After the deploy, `client-tools.json` says which clients have fetched the new list and which still call tools from an older one.
112
+
62
113
  ### Revising a mission before it is decided
63
114
 
64
115
  A mission is revised, never re-requested. `revise_pending_request` changes an agent run or work proposal that still awaits a decision, or that its approver sent back, and gives it its next version. Pass the version you read as `expected_version` and only the fields that change. The request keeps its id, its approval page and its one card. A change of `provider` is verified on the host again. Each revision is a record in the ledger carrying the whole new content, its reason, author, changed fields and `sha256` content hash, so every earlier version stays readable verbatim with `get_request_history`. A request already approved, queued or declined is refused: a change after a decision is a new request or a steer.
@@ -222,7 +273,7 @@ A service reads the same file with `EnvironmentFile=-/etc/miadi/miadi.env`, and
222
273
  | `MIADI_CONNECTOR_PORT` / `MIADI_CONNECTOR_HOST` | `3337` / `127.0.0.1` | where it listens |
223
274
  | `MIADI_CONNECTOR_MIADI_URL` | `http://127.0.0.1:3335` | the Miadi app it calls, as the person |
224
275
  | `MIADI_PUBLIC_URL` | `MIADI_URL_BASE`, else `MIADI_CONNECTOR_MIADI_URL` | the Miadi app's address in the links people open from an answer (`/chronicle/…`, `/research`) |
225
- | `MIADI_CONNECTOR_STATE_DIR` | `/srv/miadi/connector` | `oauth.json` (token digests, clients) and `ledger.jsonl` (records), mode 0700 |
276
+ | `MIADI_CONNECTOR_STATE_DIR` | `/srv/miadi/connector` | `oauth.json` (token digests, clients), `ledger.jsonl` (records) and `client-tools.json` (the tool list each client last fetched), mode 0700 |
226
277
  | `MIADI_REVIEW_BASE_URL` | `https://miadi-review-service.vercel.app` | the review service |
227
278
  | `MIADI_REVIEW_TOKEN` | read from `~/.env` | review search and approved review operations |
228
279
  | `MIADI_FACTORY_DIR` | `~/.miadi/factory` | the queue approved runs are written to |
@@ -274,6 +325,8 @@ The issues and the episode behind each part. `jgwill/Miadi` is a private reposit
274
325
  | the mission standard, and the local reviewer that will check S2 (A3) | jgwill/Miadi#939 |
275
326
  | the tests the standard's bars were read from | Episode 564 (miadi-chronicle://564), `mission-draft-review-261105/` |
276
327
  | the factory's review pages | jgwill/Miadi#961 |
328
+ | registered screenwalks read through the registry door, and the step to their review | jgwill/Miadi#1180 (the registry's identity: jgwill/Miadi#1112) |
329
+ | the tool list each client last fetched, and `client_tools` in `get_capabilities` | jgwill/Miadi#1180 |
277
330
  | what a published README holds | jgwill/Miadi#685 |
278
331
 
279
332
  On the factory host (gaia) the connector runs as the user service `miadi-connector.service`, installed from the Miadi repository's `scripts/ops/etc/miadi-connector.user.service`. That host's `/etc/miadi/miadi.env` sets `MIADI_CONNECTOR_PUBLIC_URL`, and the Cloudflare tunnel `gaia` serves that address from the loopback port. Its reading runtime is `packages/connector/scripts/host-reading-runtime.mjs`.
@@ -0,0 +1,119 @@
1
+ /**
2
+ * The tool list each client last fetched (jgwill/Miadi#1180).
3
+ *
4
+ * A client keeps the tool list it fetched: ChatGPT until a person refreshes the app, a terminal client until it
5
+ * reconnects. After a release, get_capabilities names tools such a client cannot call, and the client reads that as
6
+ * the server announcing what it does not offer. So the connector keeps, for each OAuth client and for each session
7
+ * of a client that has no client id, the connector version and the tool names of its last tools/list, and
8
+ * get_capabilities says when that list is older than the running connector.
9
+ *
10
+ * Kept in the state directory as client-tools.json: OAuth client ids, session ids the connector gave, versions, tool
11
+ * names, counts and times. No token and no person.
12
+ */
13
+ import type { JsonFile } from "./store.js";
14
+ export interface ClientList {
15
+ /** The connector version that answered the list. */
16
+ version: string;
17
+ at: string;
18
+ tools: string[];
19
+ }
20
+ export interface ClientEntry {
21
+ /** The last tools/list this client was answered. Null: it called tools and never fetched the list since recording began. */
22
+ list: ClientList | null;
23
+ /** How many tools/list it was answered since recording began. */
24
+ lists: number;
25
+ /** Tool calls since that list, or since recording began when there is none. */
26
+ calls_since_list: number;
27
+ last_call: {
28
+ at: string;
29
+ version: string;
30
+ } | null;
31
+ seen_at: string;
32
+ }
33
+ export interface ClientToolsState {
34
+ /** The first version that recorded lists on this host, and when. */
35
+ recording_since: {
36
+ version: string;
37
+ at: string;
38
+ } | null;
39
+ clients: Record<string, ClientEntry>;
40
+ sessions: Record<string, ClientEntry>;
41
+ }
42
+ export declare const emptyClientTools: () => ClientToolsState;
43
+ export type ClientKey = {
44
+ kind: "client" | "session";
45
+ id: string;
46
+ };
47
+ /** The version that began recording lists: a client with no record fetched its list before it, or never. */
48
+ export declare const RECORDING_SINCE = "0.1.45";
49
+ /** Under which key a request's list is kept: its OAuth client, else the session id it was given at initialize. */
50
+ export declare function clientKeyOf(clientId: string | undefined, sessionId: string | undefined): ClientKey | null;
51
+ /** Does this request come from a client with no client id, which is given a session id at initialize? */
52
+ export declare const needsSession: (clientId: string | undefined) => boolean;
53
+ export declare class ClientLists {
54
+ private readonly file;
55
+ private readonly warn;
56
+ constructor(file: JsonFile<ClientToolsState>, warn?: (message: string) => void);
57
+ /** What is kept for this key. A state file that cannot be read is no record, never a failed request. */
58
+ read(key: ClientKey | null): ClientEntry | null;
59
+ /** A tools/list was answered: the version and the names it carried. */
60
+ listed(key: ClientKey | null, version: string, tools: string[], at?: string): void;
61
+ /** A tool was called. */
62
+ called(key: ClientKey | null, version: string, at?: string): void;
63
+ private change;
64
+ }
65
+ /**
66
+ * The tools added since lists are compared, each with the version that added it. A client whose list is not recorded
67
+ * is told these by version, because nothing else says what its list may lack. `meanwhile` names a tool that existed
68
+ * before and reaches part of the same thing, with what it does and does not do.
69
+ *
70
+ * A tool added in a later release gets its entry here: test/client-tools.test.mjs fails until it has one.
71
+ */
72
+ export declare const TOOL_ADDED: Record<string, {
73
+ since: string;
74
+ meanwhile?: {
75
+ tool: string;
76
+ how: string;
77
+ };
78
+ }>;
79
+ export interface ClientToolsBlock {
80
+ state: "list_older_than_this_version" | "list_not_recorded";
81
+ this_version: string;
82
+ /** The version the client's list was fetched at. "unknown": no list is recorded for it. */
83
+ list_fetched_at_version: string;
84
+ list_fetched_at: string | null;
85
+ /** The tools this version offers that the client's list lacks. "unknown": no list is recorded to compare. */
86
+ missing_from_your_list: string[] | "unknown";
87
+ /** For a list that is not recorded: the tools each recent version added. A list fetched before that version lacks them. */
88
+ added_by_version?: Record<string, string[]>;
89
+ meaning: string;
90
+ /** One sentence for a person: how the client fetches the list again. */
91
+ refresh: string;
92
+ /** What the client can already call for the same matter, each with what it does and does not do. */
93
+ until_refresh: {
94
+ instead_of: string;
95
+ use: string;
96
+ how: string;
97
+ }[];
98
+ }
99
+ type ClientNamed = {
100
+ name?: string | undefined;
101
+ redirect_uris?: readonly unknown[] | undefined;
102
+ };
103
+ /**
104
+ * What get_capabilities says about the caller's tool list, or null when the list it last fetched is this version's.
105
+ * It says what the server offers and what the recorded list holds. It never says a tool can be called.
106
+ */
107
+ export declare function clientToolsBlock(o: {
108
+ key: ClientKey | null;
109
+ entry: ClientEntry | null;
110
+ version: string;
111
+ offered: string[];
112
+ client?: ClientNamed;
113
+ }): ClientToolsBlock | null;
114
+ /**
115
+ * The tools map of get_capabilities. A tool the client's recorded list lacks says so before its status. When no list
116
+ * is recorded nothing is known of any one tool, so the map is left as it is and client_tools says why.
117
+ */
118
+ export declare function annotateTools(status: Record<string, string>, block: ClientToolsBlock | null): Record<string, string>;
119
+ export {};
@@ -0,0 +1,196 @@
1
+ export const emptyClientTools = () => ({ recording_since: null, clients: {}, sessions: {} });
2
+ /** The version that began recording lists: a client with no record fetched its list before it, or never. */
3
+ export const RECORDING_SINCE = "0.1.45";
4
+ /** A person token used directly has this client id (oauth.ts): it names no client. */
5
+ const DIRECT = "direct";
6
+ const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
7
+ const MAX_SESSIONS = 300;
8
+ const SESSION_DAYS = 30;
9
+ /** Under which key a request's list is kept: its OAuth client, else the session id it was given at initialize. */
10
+ export function clientKeyOf(clientId, sessionId) {
11
+ if (clientId && clientId !== DIRECT)
12
+ return { kind: "client", id: clientId };
13
+ if (sessionId && SESSION_ID.test(sessionId))
14
+ return { kind: "session", id: sessionId.toLowerCase() };
15
+ return null;
16
+ }
17
+ /** Does this request come from a client with no client id, which is given a session id at initialize? */
18
+ export const needsSession = (clientId) => clientId === DIRECT;
19
+ export class ClientLists {
20
+ file;
21
+ warn;
22
+ constructor(file, warn = (m) => console.error(m)) {
23
+ this.file = file;
24
+ this.warn = warn;
25
+ }
26
+ /** What is kept for this key. A state file that cannot be read is no record, never a failed request. */
27
+ read(key) {
28
+ if (!key)
29
+ return null;
30
+ try {
31
+ const s = this.file.read();
32
+ return (key.kind === "client" ? s.clients : s.sessions)?.[key.id] ?? null;
33
+ }
34
+ catch (error) {
35
+ this.warn(`[connector] client-tools: ${error instanceof Error ? error.message : String(error)}`);
36
+ return null;
37
+ }
38
+ }
39
+ /** A tools/list was answered: the version and the names it carried. */
40
+ listed(key, version, tools, at = new Date().toISOString()) {
41
+ this.change(key, version, at, (e) => {
42
+ e.list = { version, at, tools: [...tools] };
43
+ e.lists += 1;
44
+ e.calls_since_list = 0;
45
+ });
46
+ }
47
+ /** A tool was called. */
48
+ called(key, version, at = new Date().toISOString()) {
49
+ this.change(key, version, at, (e) => {
50
+ e.calls_since_list += 1;
51
+ e.last_call = { at, version };
52
+ });
53
+ }
54
+ change(key, version, at, fn) {
55
+ if (!key)
56
+ return;
57
+ try {
58
+ let s;
59
+ try {
60
+ s = this.file.read();
61
+ }
62
+ catch (error) {
63
+ // A corrupt file is started again: it holds nothing that cannot be fetched again.
64
+ this.warn(`[connector] client-tools: ${error instanceof Error ? error.message : String(error)}; started again`);
65
+ s = emptyClientTools();
66
+ }
67
+ s.clients ??= {};
68
+ s.sessions ??= {};
69
+ s.recording_since ??= { version, at };
70
+ const bag = key.kind === "client" ? s.clients : s.sessions;
71
+ const entry = (bag[key.id] ??= { list: null, lists: 0, calls_since_list: 0, last_call: null, seen_at: at });
72
+ fn(entry);
73
+ entry.seen_at = at;
74
+ if (key.kind === "session")
75
+ pruneSessions(s, at);
76
+ this.file.write(s);
77
+ }
78
+ catch (error) {
79
+ this.warn(`[connector] client-tools: not written: ${error instanceof Error ? error.message : String(error)}`);
80
+ }
81
+ }
82
+ }
83
+ /** Sessions end without a word: the idle ones go, and the newest stay when there are many. */
84
+ function pruneSessions(s, at) {
85
+ const limit = Date.parse(at) - SESSION_DAYS * 24 * 60 * 60 * 1000;
86
+ for (const [id, e] of Object.entries(s.sessions))
87
+ if (Date.parse(e.seen_at) < limit)
88
+ delete s.sessions[id];
89
+ const ids = Object.keys(s.sessions);
90
+ if (ids.length <= MAX_SESSIONS)
91
+ return;
92
+ ids.sort((a, b) => Date.parse(s.sessions[a].seen_at) - Date.parse(s.sessions[b].seen_at));
93
+ for (const id of ids.slice(0, ids.length - MAX_SESSIONS))
94
+ delete s.sessions[id];
95
+ }
96
+ /**
97
+ * The tools added since lists are compared, each with the version that added it. A client whose list is not recorded
98
+ * is told these by version, because nothing else says what its list may lack. `meanwhile` names a tool that existed
99
+ * before and reaches part of the same thing, with what it does and does not do.
100
+ *
101
+ * A tool added in a later release gets its entry here: test/client-tools.test.mjs fails until it has one.
102
+ */
103
+ export const TOOL_ADDED = {
104
+ get_screenwalk: {
105
+ since: "0.1.44",
106
+ meanwhile: {
107
+ tool: "get_context",
108
+ how: "get_context with ref miadi-screenwalk:<id>, or the registration's page URL, answers the registration's identity, its canonical video, the names and where they stand, its readiness and its inventory counts. It does not return the whole record, its brief or its moments, and it does not say whether a review exists.",
109
+ },
110
+ },
111
+ list_screenwalks: { since: "0.1.44" },
112
+ request_screenwalk_review: {
113
+ since: "0.1.44",
114
+ meanwhile: {
115
+ tool: "review_lookup",
116
+ how: "review_lookup with the video's YouTube URL (get_context gives it) answers the review of that video, or no_review. Each review it answers carries screenwalks: the registrations related to it. It relates nothing and prepares no create request.",
117
+ },
118
+ },
119
+ };
120
+ function isChatGpt(client) {
121
+ if (/chatgpt|openai/i.test(client?.name ?? ""))
122
+ return true;
123
+ return (client?.redirect_uris ?? []).some((u) => {
124
+ try {
125
+ return /(^|\.)chatgpt\.com$|(^|\.)openai\.com$/i.test(new URL(String(u)).hostname);
126
+ }
127
+ catch {
128
+ return false;
129
+ }
130
+ });
131
+ }
132
+ function refreshSentence(key, client) {
133
+ if (isChatGpt(client))
134
+ return "For a person: in ChatGPT, open Settings, then Apps, choose this app and press Refresh. ChatGPT then fetches the tool list again.";
135
+ if (key?.kind === "client")
136
+ return "For a person: reconnect this connector in the client's settings, or start a new session there, so that the client asks for the tool list again.";
137
+ return "For a person: restart or reconnect the client, so that it asks for the tool list again.";
138
+ }
139
+ /**
140
+ * What get_capabilities says about the caller's tool list, or null when the list it last fetched is this version's.
141
+ * It says what the server offers and what the recorded list holds. It never says a tool can be called.
142
+ */
143
+ export function clientToolsBlock(o) {
144
+ const list = o.entry?.list ?? null;
145
+ const refresh = refreshSentence(o.key, o.client);
146
+ if (list) {
147
+ const held = new Set(list.tools);
148
+ const missing = o.offered.filter((n) => !held.has(n));
149
+ if (list.version === o.version && !missing.length)
150
+ return null;
151
+ return {
152
+ state: "list_older_than_this_version",
153
+ this_version: o.version,
154
+ list_fetched_at_version: list.version,
155
+ list_fetched_at: list.at,
156
+ missing_from_your_list: missing,
157
+ meaning: missing.length
158
+ ? `Your client holds the tool list it fetched from version ${list.version}. This connector now runs ${o.version}. The tools in missing_from_your_list are offered by the server and are not in that list: your client cannot call them until it fetches the list again. The tools map below names what the server offers, not what your client can call.`
159
+ : `Your client holds the tool list it fetched from version ${list.version}. This connector now runs ${o.version}. No tool is missing from that list. Descriptions and inputs may have changed since.`,
160
+ refresh,
161
+ until_refresh: missing.flatMap((n) => {
162
+ const m = TOOL_ADDED[n]?.meanwhile;
163
+ return m && held.has(m.tool) ? [{ instead_of: n, use: m.tool, how: m.how }] : [];
164
+ }),
165
+ };
166
+ }
167
+ const offered = new Set(o.offered);
168
+ const added = {};
169
+ for (const [name, a] of Object.entries(TOOL_ADDED))
170
+ if (offered.has(name))
171
+ (added[a.since] ??= []).push(name);
172
+ return {
173
+ state: "list_not_recorded",
174
+ this_version: o.version,
175
+ list_fetched_at_version: "unknown",
176
+ list_fetched_at: null,
177
+ missing_from_your_list: "unknown",
178
+ added_by_version: added,
179
+ meaning: `This connector has no record of a tool list fetched by your client. It records them since version ${RECORDING_SINCE}, so your client fetched its list before that, or not in this session. The connector cannot tell which tools your client holds. A tool named in the tools map below that your client does not offer you cannot be called until the client fetches the list again. added_by_version names the tools each recent version added: a list fetched before that version lacks them.`,
180
+ refresh,
181
+ until_refresh: Object.entries(TOOL_ADDED).flatMap(([n, a]) => (offered.has(n) && a.meanwhile && offered.has(a.meanwhile.tool) && !TOOL_ADDED[a.meanwhile.tool] ? [{ instead_of: n, use: a.meanwhile.tool, how: a.meanwhile.how }] : [])),
182
+ };
183
+ }
184
+ /**
185
+ * The tools map of get_capabilities. A tool the client's recorded list lacks says so before its status. When no list
186
+ * is recorded nothing is known of any one tool, so the map is left as it is and client_tools says why.
187
+ */
188
+ export function annotateTools(status, block) {
189
+ if (!block || !Array.isArray(block.missing_from_your_list) || !block.missing_from_your_list.length)
190
+ return status;
191
+ const out = { ...status };
192
+ for (const n of block.missing_from_your_list)
193
+ if (n in out)
194
+ out[n] = `not in your client's tool list, fetched from version ${block.list_fetched_at_version} (see client_tools). On the server: ${out[n]}`;
195
+ return out;
196
+ }
@@ -0,0 +1,243 @@
1
+ import type { MiadiClient } from "./miadi.js";
2
+ import type { ConnectorRecord } from "./records.js";
3
+ /**
4
+ * The name the connector's records already give a link from a source to the review made of it
5
+ * (link_records, from the source to miadi-review:<uuid>). A registered screenwalk is such a source.
6
+ */
7
+ export declare const REVIEW_SOURCE_RELATION = "review_created_from_source";
8
+ /** The registry's relation kind for an external record that points at the same work. */
9
+ export declare const REGISTRY_REVIEW_KIND = "correlates";
10
+ export declare const DEFAULT_REGISTRATION_BYTES = 100000;
11
+ export declare const MAX_REGISTRATION_BYTES: number;
12
+ export declare const MIN_REGISTRATION_BYTES = 4000;
13
+ export declare class ScreenwalkRefusal extends Error {
14
+ readonly code: string;
15
+ readonly detail?: unknown | undefined;
16
+ constructor(code: string, message: string, detail?: unknown | undefined);
17
+ }
18
+ export interface ScreenwalkRef {
19
+ id: string;
20
+ /** miadi-screenwalk:<id> */
21
+ reference: string;
22
+ form: "id" | "address" | "page_url" | "record_url";
23
+ }
24
+ /** The origins a screenwalk URL may carry: this factory's public address and the address the connector calls. */
25
+ export declare function registryOrigins(config: {
26
+ miadiUrl: string;
27
+ miadiPublicUrl: string;
28
+ }): string[];
29
+ /**
30
+ * The screenwalk a reference names. A URL is only read for the id it carries, and only when it is on
31
+ * one of the given origins: nothing is fetched from an address a caller supplies.
32
+ */
33
+ export declare function screenwalkRefOf(input: string, origins: readonly string[]): ScreenwalkRef;
34
+ /** True when a reference a caller gave should be read as a screenwalk before anything else: its address, or a registry URL. */
35
+ export declare function looksLikeScreenwalk(input: string, origins: readonly string[]): boolean;
36
+ export type RegistryFailure = {
37
+ state: "not_found" | "unauthorized" | "forbidden" | "unavailable" | "registry_error";
38
+ status: number;
39
+ message: string;
40
+ };
41
+ export type RegistryRead = {
42
+ state: "found";
43
+ registration: Record<string, any>;
44
+ summary: Record<string, any> | null;
45
+ } | RegistryFailure;
46
+ /** One registration, read as the person. The registry's own rights decide the answer. */
47
+ export declare function readRegistration(miadi: MiadiClient, token: string, id: string): Promise<RegistryRead>;
48
+ /** The registry's Markdown brief (the door's own projection), whole or for one purpose list. */
49
+ export declare function readBrief(miadi: MiadiClient, token: string, id: string, purpose?: string): Promise<{
50
+ state: "found";
51
+ markdown: string;
52
+ } | RegistryFailure>;
53
+ /** The registry's moment index (the door's own projection): one row per timeframe, with the revision it was read at. */
54
+ export declare function readMoments(miadi: MiadiClient, token: string, id: string): Promise<{
55
+ state: "found";
56
+ revision: number | null;
57
+ moments: unknown[];
58
+ } | RegistryFailure>;
59
+ /** The registrations the person may see, as the registry's list door returns them (its own filters applied there). */
60
+ export declare function listRegistrations(miadi: MiadiClient, token: string, filters?: {
61
+ status?: string;
62
+ name?: string;
63
+ coverage?: string;
64
+ }): Promise<{
65
+ state: "found";
66
+ registrations: Record<string, any>[];
67
+ } | RegistryFailure>;
68
+ /** Write one relation on a registration, as the person, naming the revision read. The registry validates and lands it. */
69
+ export declare function relateRegistration(miadi: MiadiClient, token: string, id: string, expectedRevision: number, relation: {
70
+ id: string;
71
+ kind: string;
72
+ target: string;
73
+ label?: string;
74
+ note?: string;
75
+ }): Promise<{
76
+ state: "recorded";
77
+ revision: number | null;
78
+ landing: unknown;
79
+ } | {
80
+ state: "held";
81
+ } | {
82
+ state: "not_recorded";
83
+ reason: "forbidden" | "conflict" | "unavailable" | "refused";
84
+ status: number;
85
+ message: string;
86
+ }>;
87
+ export interface ScreenwalkVideo {
88
+ /** The URL as the registration holds it. */
89
+ url: string | null;
90
+ /** The YouTube video id: the key a review is matched by. Null when the URL names none. */
91
+ video_id: string | null;
92
+ canonical_url: string | null;
93
+ uploaded_title: string | null;
94
+ duration: string | null;
95
+ published_at: string | null;
96
+ channel: string | null;
97
+ captions: string | null;
98
+ }
99
+ /** The registered video with its canonical YouTube identity. A missing field is null, never guessed. */
100
+ export declare function videoOf(registration: Record<string, any>): ScreenwalkVideo;
101
+ /**
102
+ * The safe title, title and description as they stand, with the registry's own word for where they stand
103
+ * (proposed, needs_review, approved, approved_with_changes, rejected). The registry's summary gives the
104
+ * approved names once a person approved them, else the last proposal.
105
+ */
106
+ export declare function namesOf(registration: Record<string, any>, summary: Record<string, any> | null): {
107
+ state: string | null;
108
+ approved: boolean;
109
+ round: any;
110
+ round_state: string | null;
111
+ safe_title: string | null;
112
+ title: string | null;
113
+ description: string | null;
114
+ uploaded_title: string | null;
115
+ last_decision: {
116
+ decision: string | null;
117
+ by: string | null;
118
+ at: string | null;
119
+ } | null;
120
+ };
121
+ /** What the registration holds, counted from its own parts. Zero means the part is empty; null means the registry records nothing there. */
122
+ export declare function inventoryOf(registration: Record<string, any>): {
123
+ question_sets: number;
124
+ question_set_states: Record<string, number>;
125
+ transcript: any;
126
+ transcript_parts: number;
127
+ segments: number;
128
+ segmentations: number;
129
+ aspects: number;
130
+ sources: number;
131
+ questions: number;
132
+ observations: number;
133
+ purpose_lists: number;
134
+ purpose_items: any;
135
+ purposes: {
136
+ duplicate_of?: any;
137
+ id: any;
138
+ purpose: any;
139
+ label: any;
140
+ items: number;
141
+ }[];
142
+ relations: number;
143
+ relations_by_kind: Record<string, number>;
144
+ names_heard: number;
145
+ topics: number;
146
+ notices: number;
147
+ resupply_open: number;
148
+ testimonies: number;
149
+ amendments: number;
150
+ };
151
+ /**
152
+ * Where a registration stands, each thing by its own name. The registry records where the names stand with a
153
+ * person, how far the questioning reaches and what a person checked. It records no validation verdict: a
154
+ * registration that exists says nothing about another agent's validation pass.
155
+ */
156
+ export declare function readinessOf(registration: Record<string, any>, summary: Record<string, any> | null): {
157
+ names: string | null;
158
+ coverage: any;
159
+ coverage_note: any;
160
+ checked_by_a_person: any;
161
+ unverified_items: any;
162
+ transcript: any;
163
+ validation: string;
164
+ };
165
+ /** Each top-level part of a registration with its size, so a caller can ask for the parts it needs. */
166
+ export declare function partIndex(registration: Record<string, any>): {
167
+ part: string;
168
+ bytes: number;
169
+ items: number | null;
170
+ }[];
171
+ /**
172
+ * The registration, or the parts asked for, in the registry's own schema. With parts, the identity keys come with
173
+ * them. With a purpose, only that purpose list stays among the purposes. Whatever was left out is named.
174
+ */
175
+ export declare function selectRegistration(registration: Record<string, any>, options?: {
176
+ parts?: readonly string[];
177
+ purpose?: string;
178
+ }): {
179
+ purpose?: string | undefined;
180
+ purposes_left_out?: string[] | undefined;
181
+ value: Record<string, unknown>;
182
+ returned: string[];
183
+ left_out: string[];
184
+ };
185
+ export interface TextPage {
186
+ bytes: number;
187
+ sha256: string;
188
+ complete: boolean;
189
+ page: {
190
+ offset: number;
191
+ end: number;
192
+ bytes: number;
193
+ next: {
194
+ offset: number;
195
+ revision: string;
196
+ } | null;
197
+ };
198
+ text: string;
199
+ }
200
+ /**
201
+ * One page of a text. A page never starts or ends inside a character. revision, when given, must still be the
202
+ * text's sha256, so pages of two revisions of a registration are never joined.
203
+ */
204
+ export declare function pageText(content: string, options?: {
205
+ offset?: number;
206
+ maxBytes?: number;
207
+ revision?: string;
208
+ }): TextPage;
209
+ /** The relations a registration itself holds toward a review: the registration's side of the relation. */
210
+ export declare function reviewRelationsOf(registration: Record<string, any>): {
211
+ id: string;
212
+ kind: string;
213
+ target: string;
214
+ review_id: string;
215
+ by: string | null;
216
+ at: string | null;
217
+ }[];
218
+ /** True when a connector record names this screenwalk: in what it cites, in a link's ends, or as the source of a request. */
219
+ export declare function namesScreenwalk(record: ConnectorRecord, id: string): boolean;
220
+ export interface ScreenwalkReviewRecord {
221
+ record: string;
222
+ kind: string;
223
+ created_at: string;
224
+ by: string;
225
+ /** For a link: its relation's name. */
226
+ relation?: string;
227
+ /** The review the record names, when it names one. */
228
+ review_id: string | null;
229
+ approval_state?: string;
230
+ state?: string;
231
+ }
232
+ /**
233
+ * The connector's records that relate a screenwalk to a review: a link from the registration to a review, and a
234
+ * review operation asked with the registration as its source (its receipt names the review once it is created).
235
+ */
236
+ export declare function reviewRecordsOf(records: readonly ConnectorRecord[], id: string): ScreenwalkReviewRecord[];
237
+ /** The screenwalks the connector's records relate to a review: the review's side of the relation. */
238
+ export declare function screenwalksOfReview(records: readonly ConnectorRecord[], reviewId: string): {
239
+ reference: string;
240
+ record: string;
241
+ kind: string;
242
+ relation?: string;
243
+ }[];