@aidemd-mcp/telemetry-contract 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +193 -2
- package/chunk-WVGQ3VJI.js +404 -0
- package/guide.md +194 -0
- package/lib/openapi/buildPingPath/index.d.ts +10 -0
- package/lib/openapi/index.d.ts +8 -0
- package/lib/openapi/index.js +107 -0
- package/lib/openapi/toComponent/index.d.ts +11 -0
- package/lib/ping/classifyIssues/formatPath/index.d.ts +6 -0
- package/lib/ping/classifyIssues/index.d.ts +14 -0
- package/lib/ping/index.d.ts +15 -0
- package/lib/ping/index.js +94 -0
- package/lib/ping/readVersion/index.d.ts +17 -0
- package/openapi.json +1304 -0
- package/package.json +27 -5
- package/types/ping/answers/alreadyStored/index.d.ts +7 -0
- package/types/ping/answers/failed/index.d.ts +7 -0
- package/types/ping/answers/refused/index.d.ts +16 -0
- package/types/ping/answers/stored/index.d.ts +7 -0
- package/types/ping/buckets/phaseDuration/index.d.ts +24 -0
- package/types/ping/buckets/phasesPerSession/index.d.ts +24 -0
- package/types/ping/buckets/sessionLength/index.d.ts +24 -0
- package/types/ping/buckets/sessionsPerProject/index.d.ts +21 -0
- package/types/ping/buildCounts/index.d.ts +21 -0
- package/types/ping/count/index.d.ts +8 -0
- package/types/ping/examplePing/index.d.ts +8 -0
- package/types/ping/index.d.ts +43 -0
- package/types/ping/index.js +50 -0
- package/types/ping/knownVersions/index.d.ts +4 -0
- package/types/ping/listenerOrigin/index.d.ts +7 -0
- package/types/ping/lists/agents/index.d.ts +4 -0
- package/types/ping/lists/launches/index.d.ts +4 -0
- package/types/ping/lists/osNames/index.d.ts +4 -0
- package/types/ping/lists/phases/index.d.ts +4 -0
- package/types/ping/lists/qaVerdicts/index.d.ts +4 -0
- package/types/ping/lists/refusalReasons/index.d.ts +4 -0
- package/types/ping/lists/toolNames/index.d.ts +4 -0
- package/types/ping/maxPingBytes/index.d.ts +7 -0
- package/types/ping/openApiDocument/index.d.ts +16 -0
- package/types/ping/pingCheck/index.d.ts +23 -0
- package/types/ping/v2/envelope/index.d.ts +25 -0
- package/types/ping/v2/index.d.ts +169 -0
- package/types/ping/v2/projects/index.d.ts +16 -0
- package/types/ping/v2/usage/index.d.ts +144 -0
- package/types/ping/versions/index.d.ts +175 -0
package/guide.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Telemetry ping: guide for the agent that builds pings
|
|
2
|
+
|
|
3
|
+
This guide is for the agent that writes the code which counts, builds and posts AIDEMD usage pings. Follow it exactly. Every counting rule below is the rule the published schema states in its field descriptions; if this guide and a description ever differ, the description in the schema wins and this guide is wrong.
|
|
4
|
+
|
|
5
|
+
## 1. What this package is
|
|
6
|
+
|
|
7
|
+
This package holds the one definition of the usage ping: the schema of every ping version, a plain-language description of every field and exactly how it is counted, the closed lists and bucket edges, the answers the listener gives, an example ping, and this guide. Build pings from it and check every ping with the package's `check` before posting. Install it as a runtime dependency (never a devDependency): your code imports it when it runs. It holds no server code and reads no clock, disk or network.
|
|
8
|
+
|
|
9
|
+
## 2. Checklist
|
|
10
|
+
|
|
11
|
+
Tick each item, in order, for every send attempt.
|
|
12
|
+
|
|
13
|
+
1. Decide consent (section 3). If the result is off, stop: nothing is counted for sending, nothing is posted.
|
|
14
|
+
2. Pick the finished UTC days still due (section 4): at most the last 7 finished days, none on or before the day the notice was seen, none already claimed.
|
|
15
|
+
3. For each due day, count every field as section 5 says.
|
|
16
|
+
4. Build the ping: every field present, every count zero-filled, `schema` set to the version your release was tested against (section 9).
|
|
17
|
+
5. Check the ping with the package's `check`. If it returns an error, do not post it; log only the error's `code` and drop the day.
|
|
18
|
+
6. Claim the day, before posting, so it can never be posted twice.
|
|
19
|
+
7. Post once (section 7).
|
|
20
|
+
8. Handle the answer (section 7): `201` and `200` mean delivered; anything else, retry once after 1 second, then drop the day.
|
|
21
|
+
|
|
22
|
+
## 3. Consent
|
|
23
|
+
|
|
24
|
+
Checked in this order; the first rule that applies wins.
|
|
25
|
+
|
|
26
|
+
1. `DO_NOT_TRACK=1` is set: off.
|
|
27
|
+
2. `AIDEMD_TELEMETRY_DISABLED=1` is set: off.
|
|
28
|
+
3. CI is detected (`CI`, `GITHUB_ACTIONS` and similar): off, and no notice is shown.
|
|
29
|
+
4. Not an interactive session: off.
|
|
30
|
+
5. The notice has not been seen: off.
|
|
31
|
+
6. The notice was seen today: off until that day is over.
|
|
32
|
+
7. The user's setting says off: off.
|
|
33
|
+
8. The user's setting says on: a ping may be sent.
|
|
34
|
+
|
|
35
|
+
Persistence: a process that sees rule 1 or rule 2 saves "off" in the user's setting, so telemetry stays off, even in tools started without that variable, until the user turns it back on themselves. `on` is refused while any of rules 1 to 4 applies.
|
|
36
|
+
|
|
37
|
+
## 4. When a day may be sent
|
|
38
|
+
|
|
39
|
+
- Only finished UTC days: the `day` is earlier than the current UTC date at the moment the ping is built.
|
|
40
|
+
- At most one ping per install per day.
|
|
41
|
+
- The day the notice was seen (or telemetry was turned on) and every earlier day are never sent.
|
|
42
|
+
- Catch-up covers at most the last 7 finished days; older days are dropped.
|
|
43
|
+
- A day is claimed before it is posted, so it is never posted twice, even if the process dies mid-post.
|
|
44
|
+
- A day with zero sessions and zero tool calls is claimed and not sent.
|
|
45
|
+
|
|
46
|
+
## 5. What to count
|
|
47
|
+
|
|
48
|
+
Every field of version 2 is required and every count is a whole number from 0 to 9007199254740991. Every key of every map is always present, zero-filled; a missing key is refused, never defaulted. Bucket bounds: the lower bound is inclusive, the upper bound is the next bucket's lower bound and exclusive, and the last bucket has no upper bound. The fields follow in schema order.
|
|
49
|
+
|
|
50
|
+
### `schema`
|
|
51
|
+
|
|
52
|
+
The format version this ping follows. Always `2` for this version. Reveals only which format the sender built.
|
|
53
|
+
|
|
54
|
+
### `installId`
|
|
55
|
+
|
|
56
|
+
A random version-4 UUID the install made once on its own machine when the user first saw the telemetry notice. Never derived from the machine, the user or anything else; replaced with a new random one when the user resets it. Reveals only that pings with the same id came from the same install.
|
|
57
|
+
|
|
58
|
+
### `day`
|
|
59
|
+
|
|
60
|
+
The UTC calendar date (`YYYY-MM-DD`) the counts cover. Always a finished day: earlier than the sender's current UTC date when the ping was built.
|
|
61
|
+
|
|
62
|
+
### `aidemdVersion`
|
|
63
|
+
|
|
64
|
+
The version of the AIDEMD package that built the ping, as its package.json states it: MAJOR.MINOR.PATCH with optional -prerelease and +build (the semver.org recommended pattern), at most 64 characters.
|
|
65
|
+
|
|
66
|
+
### `launch`
|
|
67
|
+
|
|
68
|
+
How AIDEMD was started. One of `global`, `npx`, `other`.
|
|
69
|
+
|
|
70
|
+
- `npx` when any folder in the running entry script's path, as launched or resolved, is named `_npx`.
|
|
71
|
+
- Otherwise `global` when the entry script's real path is inside a `node_modules` folder whose containing folder has no package.json (an npm global install).
|
|
72
|
+
- Otherwise `other` (a project-local install or a source checkout).
|
|
73
|
+
|
|
74
|
+
### `os`
|
|
75
|
+
|
|
76
|
+
The operating system family: Node's `process.platform` when it is `win32`, `darwin` or `linux`, otherwise `other`.
|
|
77
|
+
|
|
78
|
+
### `node`
|
|
79
|
+
|
|
80
|
+
The major version of the Node.js runtime that ran AIDEMD: the number before the first dot of `process.versions.node`. A whole number from 1 to 999.
|
|
81
|
+
|
|
82
|
+
### `sessions`
|
|
83
|
+
|
|
84
|
+
Sessions counted for this day. A session is one AIDE session: a conversation that ran `/aide`, identified by its session id, whose log holds the `orchestration_began` record written by `aide_orchestration_begin`. A conversation the hooks log without that record is not a session and is never counted. A session's records may span several logs and it is counted once. A session is counted on the UTC day of its first recorded event, and only on that day, even if it runs past midnight. Only counted sessions feed `sessions`, `phases`, `qa`, `sessionLength`, `phasesPerSession`, `phaseDurations`, `projects` and `sessionsPerProject`.
|
|
85
|
+
|
|
86
|
+
### `phases`
|
|
87
|
+
|
|
88
|
+
Pipeline phases started in the counted sessions, by phase name. Each phase start recorded in a counted session adds one to its phase. A start of a phase name not in the list is not counted. Keys, in order: `spec`, `research`, `synthesize`, `structure`, `plan`, `build`, `qa`, `fix`, `align`, `refactor`, `cleanup`.
|
|
89
|
+
|
|
90
|
+
### `qa`
|
|
91
|
+
|
|
92
|
+
QA verdicts recorded in the counted sessions, by result. Keys: `passed` (QA runs whose verdict was passed) and `issues` (QA runs whose verdict was that issues were found).
|
|
93
|
+
|
|
94
|
+
### `toolCalls`
|
|
95
|
+
|
|
96
|
+
Calls to AIDE tools made on this UTC day, counted on the day of each call (not the session's start day), by tool name. A call to any name not in the list is added to `unknown`, so no other name is ever sent. Keys, in order: `aide_discover`, `aide_read`, `aide_scaffold`, `aide_cleanup`, `aide_move`, `aide_discard`, `aide_frontmatter`, `aide_validate`, `aide_upgrade`, `aide_info`, `aide_brain`, `aide_init`, `aide_inspect`, `aide_orchestration_begin`, `aide_session_event`, `aide_session_end`, and `unknown` (calls on this day to any tool name not in the list).
|
|
97
|
+
|
|
98
|
+
### `agents`
|
|
99
|
+
|
|
100
|
+
AIDEMD server processes started on this UTC day, by the agent that connected to them; each server process counts once (it counts server starts, not sessions). The agent is read from the connecting client's own name, lower-cased: containing `claude-code` is `claude-code`, containing `cursor` is `cursor`, anything else or no name is `other`. The client's name itself is never sent. Keys: `claude-code`, `cursor`, `other`.
|
|
101
|
+
|
|
102
|
+
### `sessionLength`
|
|
103
|
+
|
|
104
|
+
Counted sessions by length: the time from the session's first recorded event to its last, in minutes. Each counted session adds one to exactly one bucket, so the buckets add up to `sessions`. A bucket holds lengths at or above its lower bound and below the next bucket's; a negative length from clock skew falls in the first.
|
|
105
|
+
|
|
106
|
+
| Key | From (minutes, inclusive) | Up to (exclusive) |
|
|
107
|
+
| --- | --- | --- |
|
|
108
|
+
| `<5m` | 0 | 5 |
|
|
109
|
+
| `5-15m` | 5 | 15 |
|
|
110
|
+
| `15-60m` | 15 | 60 |
|
|
111
|
+
| `1-3h` | 60 | 180 |
|
|
112
|
+
| `3h+` | 180 | no upper bound |
|
|
113
|
+
|
|
114
|
+
### `phasesPerSession`
|
|
115
|
+
|
|
116
|
+
Counted sessions by how many phase starts each recorded, whatever the phase name. Each counted session adds one to exactly one bucket, so the buckets add up to `sessions`.
|
|
117
|
+
|
|
118
|
+
| Key | From (phase starts, inclusive) | Up to (exclusive) |
|
|
119
|
+
| --- | --- | --- |
|
|
120
|
+
| `0` | 0 | 1 |
|
|
121
|
+
| `1-2` | 1 | 3 |
|
|
122
|
+
| `3-5` | 3 | 6 |
|
|
123
|
+
| `6-10` | 6 | 11 |
|
|
124
|
+
| `11+` | 11 | no upper bound |
|
|
125
|
+
|
|
126
|
+
### `phaseDurations`
|
|
127
|
+
|
|
128
|
+
Finished phase runs in the counted sessions, by phase and duration. One key per phase (the same eleven as `phases`), each holding one count per duration bucket. A run is a phase start paired with the earliest still-open start that has the same pipeline, phase and module when a matching phase finish arrives; its duration is from start to finish, in minutes. A start never finished and a finish with no open start are not counted. The module is used only to pair starts with finishes and is never sent.
|
|
129
|
+
|
|
130
|
+
| Key | From (minutes, inclusive) | Up to (exclusive) |
|
|
131
|
+
| --- | --- | --- |
|
|
132
|
+
| `<1m` | 0 | 1 |
|
|
133
|
+
| `1-5m` | 1 | 5 |
|
|
134
|
+
| `5-15m` | 5 | 15 |
|
|
135
|
+
| `15-60m` | 15 | 60 |
|
|
136
|
+
| `60m+` | 60 | no upper bound |
|
|
137
|
+
|
|
138
|
+
### `projects`
|
|
139
|
+
|
|
140
|
+
Distinct projects with at least one counted session. A session belongs to the one project whose session log holds its first event. Only the number is sent: never a project's name, path, key or anything else about it.
|
|
141
|
+
|
|
142
|
+
### `sessionsPerProject`
|
|
143
|
+
|
|
144
|
+
The projects counted in `projects`, by how many counted sessions each had. Each counted project adds one to exactly one bucket, so the buckets add up to `projects`.
|
|
145
|
+
|
|
146
|
+
| Key | From (sessions, inclusive) | Up to (exclusive) |
|
|
147
|
+
| --- | --- | --- |
|
|
148
|
+
| `1` | 1 | 2 |
|
|
149
|
+
| `2-3` | 2 | 4 |
|
|
150
|
+
| `4-10` | 4 | 11 |
|
|
151
|
+
| `11+` | 11 | no upper bound |
|
|
152
|
+
|
|
153
|
+
The sums stated above (`sessionLength` and `phasesPerSession` add up to `sessions`; `sessionsPerProject` adds up to `projects`) are the counting rules. The check does not enforce them; your code must.
|
|
154
|
+
|
|
155
|
+
## 6. What never goes in a ping
|
|
156
|
+
|
|
157
|
+
Never: paths, project keys, project, task, module or file names, prompts, code, commit hashes, tool arguments, refusal reasons, error text, hostnames, the agent's client name. No field can carry free text, and the schema refuses any key it does not list.
|
|
158
|
+
|
|
159
|
+
## 7. How to post
|
|
160
|
+
|
|
161
|
+
- `POST https://telemetry.aidemd.dev/ping` (`listenerOrigin` plus the path `/ping`).
|
|
162
|
+
- `Content-Type: application/json`; the body is the ping as JSON, at most 64 KiB (`65536` bytes).
|
|
163
|
+
- No credentials, no cookies. A redirect is a failure; do not follow it.
|
|
164
|
+
- A limit of 4 seconds per attempt.
|
|
165
|
+
- On a timeout or any non-2xx answer, retry once 1 second later, then drop the day for good.
|
|
166
|
+
- Check the ping with the package's `check` before posting and never post one it refuses.
|
|
167
|
+
|
|
168
|
+
The listener answers with one of these JSON bodies:
|
|
169
|
+
|
|
170
|
+
| Status | Body | Means | What the client does |
|
|
171
|
+
| --- | --- | --- | --- |
|
|
172
|
+
| 201 | `{ "status": "stored" }` | The ping's install-day was stored by this request. | Delivered. Done. |
|
|
173
|
+
| 200 | `{ "status": "already_stored" }` | The install-day was already stored; nothing stored again. | Delivered. A repeat is not an error. Done. |
|
|
174
|
+
| 400 | `{ "status": "refused", "reason": ..., "message": ... }` | The ping was refused whole; resending the same ping gets the same answer. | Not delivered. Non-2xx: retry once after 1 second, then drop the day. Log only the `reason`, never the `message`. |
|
|
175
|
+
| 500 or 503 | `{ "status": "failed" }` | The listener could not store the ping this time. | Not delivered. Retry once after 1 second, then drop the day. |
|
|
176
|
+
| 404 or 405 | listener-only bodies | The address or method was wrong. | Not delivered. Non-2xx: retry once, then drop the day. |
|
|
177
|
+
|
|
178
|
+
The `reason` in a refusal is one of: `unknown_version` (the ping's schema is not a version the listener knows), `missing_field`, `extra_field`, `bad_value` (a value outside the allowed set, or a body that is not a JSON object), `unreadable` (the body could not be read, is not valid UTF-8 or is not valid JSON), `too_large` (the body is over 64 KiB). The `message` states the same reason in plain words, naming the fields concerned, and never repeats a value that was sent.
|
|
179
|
+
|
|
180
|
+
## 8. What your notice must say
|
|
181
|
+
|
|
182
|
+
Before anything is sent, the user must have seen a notice, and it must:
|
|
183
|
+
|
|
184
|
+
- name every field the ping holds: the counts of sessions, pipeline phases and how long they took, QA results, AIDE tool calls, agents, session lengths, phases per session, and projects (the number of projects and how many sessions each had, never their names), plus the AIDEMD, Node and OS versions, how AIDEMD was launched, the day, and the random install id;
|
|
185
|
+
- say that the install id is random and cannot be traced back to the user;
|
|
186
|
+
- say that the listener derives the country from the sender's address on arrival, and never stores or logs the address;
|
|
187
|
+
- say what is never sent (section 6), how to turn telemetry off (`DO_NOT_TRACK=1`, `AIDEMD_TELEMETRY_DISABLED=1`, and a command or switch the user can reach), and how to see exactly what would be sent;
|
|
188
|
+
- say nothing has been sent yet.
|
|
189
|
+
|
|
190
|
+
When a field is added or its meaning changes, the notice and the README change in the same release.
|
|
191
|
+
|
|
192
|
+
## 9. Versions
|
|
193
|
+
|
|
194
|
+
Known versions: `2` (read them from the package's `knownVersions`). Send exactly the version your release was tested against; never guess a newer one, never send one the package does not list. A new version reaches clients only after the listener accepting it is deployed: publish the new contract package, deploy the listener, and only then release a client that sends the new version (release order in the repository's `docs/contracts.md`). No client may send a version before the deployed listener accepts it. New tool or phase names need a new format version: until one adds a name, fold an unknown tool into `unknown` and do not count an unknown phase.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the OpenAPI path item for `/ping`: one POST whose request body is
|
|
3
|
+
* any known ping version (`oneOf` over the given component names) and whose
|
|
4
|
+
* responses are exactly the five answers the listener gives. The 404 and 405
|
|
5
|
+
* answers belong to other paths and methods, so the description names them
|
|
6
|
+
* instead of listing them.
|
|
7
|
+
*/
|
|
8
|
+
export default function buildPingPath({ versionNames }: {
|
|
9
|
+
versionNames: readonly string[];
|
|
10
|
+
}): Record<string, unknown>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type OpenApiDocument } from "../../types/ping/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Builds the listener's OpenAPI 3.1 document from the ping format's zod
|
|
4
|
+
* schemas: one component per known ping version and per answer, and the
|
|
5
|
+
* `/ping` path item that points at them. Pure and deterministic: the same
|
|
6
|
+
* document on every call, nothing read from the environment.
|
|
7
|
+
*/
|
|
8
|
+
export default function buildDocument(): OpenApiDocument;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
alreadyStored_default,
|
|
3
|
+
examplePing_default,
|
|
4
|
+
failed_default,
|
|
5
|
+
knownVersions_default,
|
|
6
|
+
listenerOrigin_default,
|
|
7
|
+
maxPingBytes_default,
|
|
8
|
+
refused_default,
|
|
9
|
+
stored_default,
|
|
10
|
+
versions_default
|
|
11
|
+
} from "../../chunk-WVGQ3VJI.js";
|
|
12
|
+
|
|
13
|
+
// src/lib/openapi/buildPingPath/index.ts
|
|
14
|
+
var answer = ({ component, description }) => ({
|
|
15
|
+
description,
|
|
16
|
+
content: {
|
|
17
|
+
"application/json": { schema: { $ref: `#/components/schemas/${component}` } }
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
function buildPingPath({ versionNames }) {
|
|
21
|
+
const limit = `${maxPingBytes_default / 1024} KiB`;
|
|
22
|
+
return {
|
|
23
|
+
post: {
|
|
24
|
+
operationId: "postPing",
|
|
25
|
+
summary: "Send one install's usage ping for one finished UTC day.",
|
|
26
|
+
description: [
|
|
27
|
+
"Accepts at most one ping per install per finished UTC day.",
|
|
28
|
+
`The body is the ping as JSON, at most ${maxPingBytes_default} bytes (${limit}).`,
|
|
29
|
+
"A ping must exactly match one known version or it is refused whole.",
|
|
30
|
+
"The sender's address is used on arrival only to look up a country code and is never stored or logged.",
|
|
31
|
+
"Other paths answer 404 and other methods on /ping answer 405."
|
|
32
|
+
].join(" "),
|
|
33
|
+
requestBody: {
|
|
34
|
+
required: true,
|
|
35
|
+
content: {
|
|
36
|
+
"application/json": {
|
|
37
|
+
schema: {
|
|
38
|
+
oneOf: versionNames.map((name) => ({
|
|
39
|
+
$ref: `#/components/schemas/${name}`
|
|
40
|
+
}))
|
|
41
|
+
},
|
|
42
|
+
example: examplePing_default
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
responses: {
|
|
47
|
+
"201": answer({
|
|
48
|
+
component: "Stored",
|
|
49
|
+
description: "Stored: the ping's install-day was stored by this request. The day is delivered."
|
|
50
|
+
}),
|
|
51
|
+
"200": answer({
|
|
52
|
+
component: "AlreadyStored",
|
|
53
|
+
description: "Already stored: this install-day was stored before, so nothing was stored again. The day is delivered; a repeat is not an error."
|
|
54
|
+
}),
|
|
55
|
+
"400": answer({
|
|
56
|
+
component: "Refused",
|
|
57
|
+
description: `Refused: the ping does not exactly match a known version, the body is not readable JSON, or it is over ${limit}. Nothing was stored; the reason says why.`
|
|
58
|
+
}),
|
|
59
|
+
"503": answer({
|
|
60
|
+
component: "Failed",
|
|
61
|
+
description: "Failed: the listener could not store the ping this time (database unavailable or the answer deadline passed). Not delivered; a later retry may succeed."
|
|
62
|
+
}),
|
|
63
|
+
"500": answer({
|
|
64
|
+
component: "Failed",
|
|
65
|
+
description: "Failed: an error inside the listener. Not delivered; a later retry may succeed."
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/lib/openapi/toComponent/index.ts
|
|
73
|
+
import { z } from "zod";
|
|
74
|
+
function toComponent(schema) {
|
|
75
|
+
const { $schema: _dialect, ...component } = z.toJSONSchema(schema);
|
|
76
|
+
return component;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/lib/openapi/index.ts
|
|
80
|
+
function buildDocument() {
|
|
81
|
+
const versionNames = knownVersions_default.map((version) => `PingV${version}`);
|
|
82
|
+
const versionSchemas = Object.fromEntries(
|
|
83
|
+
knownVersions_default.map((version) => [`PingV${version}`, toComponent(versions_default[version])])
|
|
84
|
+
);
|
|
85
|
+
const answerSchemas = {
|
|
86
|
+
Stored: toComponent(stored_default),
|
|
87
|
+
AlreadyStored: toComponent(alreadyStored_default),
|
|
88
|
+
Refused: toComponent(refused_default),
|
|
89
|
+
Failed: toComponent(failed_default)
|
|
90
|
+
};
|
|
91
|
+
const pathItem = buildPingPath({ versionNames });
|
|
92
|
+
const highestVersion = Math.max(...knownVersions_default);
|
|
93
|
+
return {
|
|
94
|
+
openapi: "3.1.0",
|
|
95
|
+
info: {
|
|
96
|
+
title: "AIDEMD telemetry listener",
|
|
97
|
+
version: String(highestVersion),
|
|
98
|
+
description: "Receives AIDEMD's daily usage pings and stores each valid one once, keeping the sender's country and never the address. Generated from @aidemd-mcp/telemetry-contract; never edited by hand."
|
|
99
|
+
},
|
|
100
|
+
servers: [{ url: listenerOrigin_default }],
|
|
101
|
+
paths: { "/ping": pathItem },
|
|
102
|
+
components: { schemas: { ...versionSchemas, ...answerSchemas } }
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
buildDocument as default
|
|
107
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Turns one zod schema into one OpenAPI 3.1 component: zod's own JSON Schema
|
|
4
|
+
* output (draft 2020-12, OpenAPI 3.1's schema dialect) with the top-level
|
|
5
|
+
* `$schema` key removed, because a component is not a standalone document.
|
|
6
|
+
* Reused sub-schemas stay inlined, so each component is self-contained.
|
|
7
|
+
* Input and output shapes are identical (the format has no transforms), so the
|
|
8
|
+
* default `io: "output"` is right. It can only fail through a bug (a schema zod
|
|
9
|
+
* cannot express), so it returns the value directly and does not catch.
|
|
10
|
+
*/
|
|
11
|
+
export default function toComponent(schema: z.ZodType): Record<string, unknown>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Joins an issue path into the dotted form a refusal reports, for example
|
|
3
|
+
* `phaseDurations.build.<1m`. An empty path is `(root)`; a path longer than 100
|
|
4
|
+
* characters is cut so it ends with `…` at exactly 100.
|
|
5
|
+
*/
|
|
6
|
+
export default function formatPath(path: readonly PropertyKey[]): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PingRefusal } from "../../../types/ping/index.js";
|
|
2
|
+
import type { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* Turns the zod issues of a failed version parse into one refusal. The code is
|
|
5
|
+
* the first category present in the order extra, missing, bad; the message
|
|
6
|
+
* names every category present. `value` is the received object, used only to
|
|
7
|
+
* tell a missing key from a wrongly typed one. The refusal is built from paths
|
|
8
|
+
* and fixed words only: never from an issue's `message` or `input`, nor any
|
|
9
|
+
* received value.
|
|
10
|
+
*/
|
|
11
|
+
export default function classifyIssues({ issues, value, }: {
|
|
12
|
+
issues: readonly z.core.$ZodIssue[];
|
|
13
|
+
value: Record<string, unknown>;
|
|
14
|
+
}): PingRefusal;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PingCheck } from "../../types/ping/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Judges one received value against the ping format. Reads the version the
|
|
4
|
+
* value claims, runs only that version's strict schema, and returns the value
|
|
5
|
+
* unchanged as `data` on an exact match, or one refusal as `error`.
|
|
6
|
+
*
|
|
7
|
+
* Error codes:
|
|
8
|
+
* - `unknown_version`: `schema` is not a version the listener knows.
|
|
9
|
+
* - `missing_field`: a required field is absent.
|
|
10
|
+
* - `extra_field`: a field the format does not have is present.
|
|
11
|
+
* - `bad_value`: a field is present but its value is not allowed, or the value is not an object.
|
|
12
|
+
*
|
|
13
|
+
* It never repairs, trims or partly accepts, and never throws for any input.
|
|
14
|
+
*/
|
|
15
|
+
export default function check(value: unknown): PingCheck;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
knownVersions_default,
|
|
3
|
+
versions_default
|
|
4
|
+
} from "../../chunk-WVGQ3VJI.js";
|
|
5
|
+
|
|
6
|
+
// src/lib/ping/classifyIssues/formatPath/index.ts
|
|
7
|
+
var maxLength = 100;
|
|
8
|
+
function formatPath(path) {
|
|
9
|
+
if (path.length === 0) return "(root)";
|
|
10
|
+
const joined = path.map(String).join(".");
|
|
11
|
+
if (joined.length <= maxLength) return joined;
|
|
12
|
+
return `${joined.slice(0, maxLength - 1)}\u2026`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/lib/ping/classifyIssues/index.ts
|
|
16
|
+
var maxFields = 10;
|
|
17
|
+
var categories = [
|
|
18
|
+
{ name: "extra", code: "extra_field", label: "extra field(s)" },
|
|
19
|
+
{ name: "missing", code: "missing_field", label: "missing field(s)" },
|
|
20
|
+
{ name: "bad", code: "bad_value", label: "value(s) outside the allowed set" }
|
|
21
|
+
];
|
|
22
|
+
function classifyIssues({
|
|
23
|
+
issues,
|
|
24
|
+
value
|
|
25
|
+
}) {
|
|
26
|
+
const paths = { extra: [], missing: [], bad: [] };
|
|
27
|
+
const isMissing = (path) => {
|
|
28
|
+
const parent = path.slice(0, -1).reduce(
|
|
29
|
+
(node, key) => typeof node === "object" && node !== null ? node[key] : void 0,
|
|
30
|
+
value
|
|
31
|
+
);
|
|
32
|
+
return typeof parent === "object" && parent !== null && !Object.hasOwn(parent, path[path.length - 1]);
|
|
33
|
+
};
|
|
34
|
+
for (const issue of issues) {
|
|
35
|
+
if (issue.code === "unrecognized_keys") {
|
|
36
|
+
for (const key of issue.keys) paths.extra.push(formatPath([...issue.path, key]));
|
|
37
|
+
} else if (issue.code === "invalid_type" && issue.path.length > 0 && isMissing(issue.path)) {
|
|
38
|
+
paths.missing.push(formatPath(issue.path));
|
|
39
|
+
} else {
|
|
40
|
+
paths.bad.push(formatPath(issue.path));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const present = categories.map((category) => ({ ...category, unique: [...new Set(paths[category.name])] })).filter(({ unique }) => unique.length > 0);
|
|
44
|
+
const message = `${present.map(({ label, unique }) => {
|
|
45
|
+
const listed = unique.slice(0, maxFields).join(", ");
|
|
46
|
+
const more = unique.length > maxFields ? ` and ${unique.length - maxFields} more` : "";
|
|
47
|
+
return `${label}: ${listed}${more}`;
|
|
48
|
+
}).join("; ")}.`;
|
|
49
|
+
const [first] = present;
|
|
50
|
+
return { code: first.code, message, fields: first.unique.slice(0, maxFields) };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/lib/ping/readVersion/index.ts
|
|
54
|
+
function readVersion(value) {
|
|
55
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
56
|
+
return { error: { code: "bad_value", fields: [], message: "The ping is not a JSON object." } };
|
|
57
|
+
}
|
|
58
|
+
if (!Object.hasOwn(value, "schema")) {
|
|
59
|
+
return {
|
|
60
|
+
error: {
|
|
61
|
+
code: "missing_field",
|
|
62
|
+
fields: ["schema"],
|
|
63
|
+
message: "The ping has no schema field, so its format version is unknown."
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const { schema } = value;
|
|
68
|
+
const version = knownVersions_default.find((known) => known === schema);
|
|
69
|
+
if (version === void 0) {
|
|
70
|
+
return {
|
|
71
|
+
error: {
|
|
72
|
+
code: "unknown_version",
|
|
73
|
+
fields: ["schema"],
|
|
74
|
+
// Built from the known list only, never from the received schema.
|
|
75
|
+
message: `The ping's schema is not a version the listener knows. Known versions: ${knownVersions_default.join(", ")}.`
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return { data: version };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/lib/ping/index.ts
|
|
83
|
+
function check(value) {
|
|
84
|
+
const version = readVersion(value);
|
|
85
|
+
if (version.error) return { error: version.error };
|
|
86
|
+
const result = versions_default[version.data].safeParse(value);
|
|
87
|
+
if (!result.success) {
|
|
88
|
+
return { error: classifyIssues({ issues: result.error.issues, value }) };
|
|
89
|
+
}
|
|
90
|
+
return { data: result.data };
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
check as default
|
|
94
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { KnownVersion, PingRefusal } from "../../../types/ping/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decides which known ping version a received value claims, before any field
|
|
4
|
+
* is checked, so the right schema (and only that one) judges the rest.
|
|
5
|
+
*
|
|
6
|
+
* Error codes:
|
|
7
|
+
* - `bad_value`: the value is not a plain JSON object (null, an array, or a primitive).
|
|
8
|
+
* - `missing_field`: the object has no `schema` field, so its version is unknown.
|
|
9
|
+
* - `unknown_version`: `schema` is not strictly equal to a known version number.
|
|
10
|
+
*/
|
|
11
|
+
export default function readVersion(value: unknown): {
|
|
12
|
+
data: KnownVersion;
|
|
13
|
+
error?: never;
|
|
14
|
+
} | {
|
|
15
|
+
data?: never;
|
|
16
|
+
error: PingRefusal;
|
|
17
|
+
};
|