@warlock.js/devtools 5.55.0 → 5.56.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.
Files changed (234) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +26 -13
  3. package/cjs/ai-source-DTpxumMA.cjs +381 -0
  4. package/cjs/ai-source-DTpxumMA.cjs.map +1 -0
  5. package/cjs/auth-DzU0gHDG.cjs +1655 -0
  6. package/cjs/auth-DzU0gHDG.cjs.map +1 -0
  7. package/cjs/boot-BQi0Etor.cjs +138 -0
  8. package/cjs/boot-BQi0Etor.cjs.map +1 -0
  9. package/cjs/capture-CqIiaznN.cjs +1342 -0
  10. package/cjs/capture-CqIiaznN.cjs.map +1 -0
  11. package/cjs/cascade-store-pHjKeSFm.cjs +863 -0
  12. package/cjs/cascade-store-pHjKeSFm.cjs.map +1 -0
  13. package/cjs/collector-CJbQbzzK.cjs +271 -0
  14. package/cjs/collector-CJbQbzzK.cjs.map +1 -0
  15. package/cjs/contracts-CVmplkNb.cjs +112 -0
  16. package/cjs/contracts-CVmplkNb.cjs.map +1 -0
  17. package/cjs/current-request-id-BoJJb3y7.cjs +37 -0
  18. package/cjs/current-request-id-BoJJb3y7.cjs.map +1 -0
  19. package/cjs/errors-CyZp5yS-.cjs +177 -0
  20. package/cjs/errors-CyZp5yS-.cjs.map +1 -0
  21. package/cjs/histogram-BaI-PnCv.cjs +102 -0
  22. package/cjs/histogram-BaI-PnCv.cjs.map +1 -0
  23. package/cjs/http-CabcUwBx.cjs +870 -0
  24. package/cjs/http-CabcUwBx.cjs.map +1 -0
  25. package/cjs/index.cjs +1 -1
  26. package/cjs/insights-BKtqxdnP.cjs +811 -0
  27. package/cjs/insights-BKtqxdnP.cjs.map +1 -0
  28. package/cjs/ip-DbNhPOzi.cjs +153 -0
  29. package/cjs/ip-DbNhPOzi.cjs.map +1 -0
  30. package/cjs/migrations-w7GMsrQw.cjs +297 -0
  31. package/cjs/migrations-w7GMsrQw.cjs.map +1 -0
  32. package/cjs/openapi-CC7OAUc6.cjs +52 -0
  33. package/cjs/openapi-CC7OAUc6.cjs.map +1 -0
  34. package/cjs/options-CWqFC5__.cjs +181 -0
  35. package/cjs/options-CWqFC5__.cjs.map +1 -0
  36. package/cjs/panels-B0dAfGbJ.cjs +298 -0
  37. package/cjs/panels-B0dAfGbJ.cjs.map +1 -0
  38. package/cjs/process-panel-DEOnLxoT.cjs +48 -0
  39. package/cjs/process-panel-DEOnLxoT.cjs.map +1 -0
  40. package/cjs/production/index.cjs +238 -0
  41. package/cjs/production/index.cjs.map +1 -0
  42. package/cjs/rolldown-runtime-Zuuoy_Rh.cjs +52 -0
  43. package/cjs/server-B6xdeKS8.cjs +184 -0
  44. package/cjs/server-B6xdeKS8.cjs.map +1 -0
  45. package/cjs/{src-Xwm3Eel9.cjs → src-Ce-3Lniv.cjs} +163 -295
  46. package/cjs/src-Ce-3Lniv.cjs.map +1 -0
  47. package/cjs/store-aq207T8v.cjs +9 -0
  48. package/cjs/stream-route-BGJa-zQu.cjs +875 -0
  49. package/cjs/stream-route-BGJa-zQu.cjs.map +1 -0
  50. package/esm/devtools-connector.d.mts +16 -13
  51. package/esm/devtools-connector.d.mts.map +1 -1
  52. package/esm/devtools-connector.mjs +127 -18
  53. package/esm/devtools-connector.mjs.map +1 -1
  54. package/esm/index.d.mts +3 -2
  55. package/esm/options.mjs +2 -1
  56. package/esm/options.mjs.map +1 -1
  57. package/esm/panels.mjs +50 -5
  58. package/esm/panels.mjs.map +1 -1
  59. package/esm/production/auth/common-passwords.mjs +303 -0
  60. package/esm/production/auth/common-passwords.mjs.map +1 -0
  61. package/esm/production/auth/default-crypto.mjs +19 -0
  62. package/esm/production/auth/default-crypto.mjs.map +1 -0
  63. package/esm/production/auth/helpers.mjs +77 -0
  64. package/esm/production/auth/helpers.mjs.map +1 -0
  65. package/esm/production/auth/index.mjs +6 -0
  66. package/esm/production/auth/passkeys.mjs +96 -0
  67. package/esm/production/auth/passkeys.mjs.map +1 -0
  68. package/esm/production/auth/service.mjs +1166 -0
  69. package/esm/production/auth/service.mjs.map +1 -0
  70. package/esm/production/boot.mjs +138 -0
  71. package/esm/production/boot.mjs.map +1 -0
  72. package/esm/production/capture/dev-stats.mjs +83 -0
  73. package/esm/production/capture/dev-stats.mjs.map +1 -0
  74. package/esm/production/capture/engine.mjs +442 -0
  75. package/esm/production/capture/engine.mjs.map +1 -0
  76. package/esm/production/capture/explain.mjs +271 -0
  77. package/esm/production/capture/explain.mjs.map +1 -0
  78. package/esm/production/capture/filter.mjs +24 -0
  79. package/esm/production/capture/filter.mjs.map +1 -0
  80. package/esm/production/capture/index.mjs +7 -0
  81. package/esm/production/capture/production-capture.mjs +134 -0
  82. package/esm/production/capture/production-capture.mjs.map +1 -0
  83. package/esm/production/capture/redact.mjs +109 -0
  84. package/esm/production/capture/redact.mjs.map +1 -0
  85. package/esm/production/capture/stats-stores.mjs +300 -0
  86. package/esm/production/capture/stats-stores.mjs.map +1 -0
  87. package/esm/production/cli/actions.mjs +151 -0
  88. package/esm/production/cli/actions.mjs.map +1 -0
  89. package/esm/production/cli/index.d.mts +17 -0
  90. package/esm/production/cli/index.d.mts.map +1 -0
  91. package/esm/production/cli/index.mjs +86 -0
  92. package/esm/production/cli/index.mjs.map +1 -0
  93. package/esm/production/contracts.d.mts +592 -0
  94. package/esm/production/contracts.d.mts.map +1 -0
  95. package/esm/production/contracts.mjs +95 -0
  96. package/esm/production/contracts.mjs.map +1 -0
  97. package/esm/production/errors.d.mts +31 -0
  98. package/esm/production/errors.d.mts.map +1 -0
  99. package/esm/production/errors.mjs +142 -0
  100. package/esm/production/errors.mjs.map +1 -0
  101. package/esm/production/http/access.mjs +124 -0
  102. package/esm/production/http/access.mjs.map +1 -0
  103. package/esm/production/http/auth-routes.mjs +280 -0
  104. package/esm/production/http/auth-routes.mjs.map +1 -0
  105. package/esm/production/http/client.mjs +57 -0
  106. package/esm/production/http/client.mjs.map +1 -0
  107. package/esm/production/http/constants.mjs +11 -0
  108. package/esm/production/http/constants.mjs.map +1 -0
  109. package/esm/production/http/context.mjs +14 -0
  110. package/esm/production/http/context.mjs.map +1 -0
  111. package/esm/production/http/cookie.mjs +39 -0
  112. package/esm/production/http/cookie.mjs.map +1 -0
  113. package/esm/production/http/headers.mjs +25 -0
  114. package/esm/production/http/headers.mjs.map +1 -0
  115. package/esm/production/http/index.mjs +10 -0
  116. package/esm/production/http/ip.mjs +130 -0
  117. package/esm/production/http/ip.mjs.map +1 -0
  118. package/esm/production/http/mount.mjs +300 -0
  119. package/esm/production/http/mount.mjs.map +1 -0
  120. package/esm/production/http/state.mjs +54 -0
  121. package/esm/production/http/state.mjs.map +1 -0
  122. package/esm/production/index.d.mts +6 -0
  123. package/esm/production/index.mjs +5 -0
  124. package/esm/production/insights/cap-report.mjs +67 -0
  125. package/esm/production/insights/cap-report.mjs.map +1 -0
  126. package/esm/production/insights/health.mjs +88 -0
  127. package/esm/production/insights/health.mjs.map +1 -0
  128. package/esm/production/insights/index.mjs +10 -0
  129. package/esm/production/insights/insights.mjs +167 -0
  130. package/esm/production/insights/insights.mjs.map +1 -0
  131. package/esm/production/insights/instance.mjs +24 -0
  132. package/esm/production/insights/instance.mjs.map +1 -0
  133. package/esm/production/insights/jobs.mjs +100 -0
  134. package/esm/production/insights/jobs.mjs.map +1 -0
  135. package/esm/production/insights/reporter.mjs +59 -0
  136. package/esm/production/insights/reporter.mjs.map +1 -0
  137. package/esm/production/insights/resources.mjs +139 -0
  138. package/esm/production/insights/resources.mjs.map +1 -0
  139. package/esm/production/insights/writer.mjs +72 -0
  140. package/esm/production/insights/writer.mjs.map +1 -0
  141. package/esm/production/openapi.mjs +52 -0
  142. package/esm/production/openapi.mjs.map +1 -0
  143. package/esm/production/options.d.mts +60 -0
  144. package/esm/production/options.d.mts.map +1 -0
  145. package/esm/production/options.mjs +170 -0
  146. package/esm/production/options.mjs.map +1 -0
  147. package/esm/production/stats/histogram.mjs +67 -0
  148. package/esm/production/stats/histogram.mjs.map +1 -0
  149. package/esm/production/stats/merge.mjs +106 -0
  150. package/esm/production/stats/merge.mjs.map +1 -0
  151. package/esm/production/store/cascade-store.mjs +322 -0
  152. package/esm/production/store/cascade-store.mjs.map +1 -0
  153. package/esm/production/store/index.mjs +5 -0
  154. package/esm/production/store/mapping.mjs +258 -0
  155. package/esm/production/store/mapping.mjs.map +1 -0
  156. package/esm/production/store/memory-store.mjs +291 -0
  157. package/esm/production/store/memory-store.mjs.map +1 -0
  158. package/esm/production/store/migrations.d.mts +11 -0
  159. package/esm/production/store/migrations.d.mts.map +1 -0
  160. package/esm/production/store/migrations.mjs +29 -0
  161. package/esm/production/store/migrations.mjs.map +1 -0
  162. package/esm/production/store/models/account.migration.mjs +34 -0
  163. package/esm/production/store/models/account.migration.mjs.map +1 -0
  164. package/esm/production/store/models/account.model.mjs +13 -0
  165. package/esm/production/store/models/account.model.mjs.map +1 -0
  166. package/esm/production/store/models/audit.migration.mjs +30 -0
  167. package/esm/production/store/models/audit.migration.mjs.map +1 -0
  168. package/esm/production/store/models/audit.model.mjs +13 -0
  169. package/esm/production/store/models/audit.model.mjs.map +1 -0
  170. package/esm/production/store/models/base.mjs +27 -0
  171. package/esm/production/store/models/base.mjs.map +1 -0
  172. package/esm/production/store/models/index.mjs +17 -0
  173. package/esm/production/store/models/invite.migration.mjs +26 -0
  174. package/esm/production/store/models/invite.migration.mjs.map +1 -0
  175. package/esm/production/store/models/invite.model.mjs +13 -0
  176. package/esm/production/store/models/invite.model.mjs.map +1 -0
  177. package/esm/production/store/models/report.migration.mjs +20 -0
  178. package/esm/production/store/models/report.migration.mjs.map +1 -0
  179. package/esm/production/store/models/report.model.mjs +13 -0
  180. package/esm/production/store/models/report.model.mjs.map +1 -0
  181. package/esm/production/store/models/sample.migration.mjs +25 -0
  182. package/esm/production/store/models/sample.migration.mjs.map +1 -0
  183. package/esm/production/store/models/sample.model.mjs +13 -0
  184. package/esm/production/store/models/sample.model.mjs.map +1 -0
  185. package/esm/production/store/models/session.migration.mjs +31 -0
  186. package/esm/production/store/models/session.migration.mjs.map +1 -0
  187. package/esm/production/store/models/session.model.mjs +13 -0
  188. package/esm/production/store/models/session.model.mjs.map +1 -0
  189. package/esm/production/store/models/state.migration.mjs +26 -0
  190. package/esm/production/store/models/state.migration.mjs.map +1 -0
  191. package/esm/production/store/models/state.model.mjs +13 -0
  192. package/esm/production/store/models/state.model.mjs.map +1 -0
  193. package/esm/query-shape.mjs +14 -1
  194. package/esm/query-shape.mjs.map +1 -1
  195. package/esm/server/explain-route.mjs +26 -1
  196. package/esm/server/explain-route.mjs.map +1 -1
  197. package/esm/server/index.mjs +9 -4
  198. package/esm/server/index.mjs.map +1 -1
  199. package/esm/server/insights-routes.mjs +62 -0
  200. package/esm/server/insights-routes.mjs.map +1 -0
  201. package/esm/server/log-routes.mjs +32 -5
  202. package/esm/server/log-routes.mjs.map +1 -1
  203. package/esm/server/openapi-routes.mjs +3 -2
  204. package/esm/server/openapi-routes.mjs.map +1 -1
  205. package/esm/server/requests-routes.mjs +125 -0
  206. package/esm/server/requests-routes.mjs.map +1 -1
  207. package/esm/server/session-routes.mjs +42 -0
  208. package/esm/server/session-routes.mjs.map +1 -0
  209. package/esm/server/stream-route.mjs +81 -11
  210. package/esm/server/stream-route.mjs.map +1 -1
  211. package/esm/server/ui-assets.mjs +34 -25
  212. package/esm/server/ui-assets.mjs.map +1 -1
  213. package/esm/sources/log-source.mjs +14 -1
  214. package/esm/sources/log-source.mjs.map +1 -1
  215. package/esm/types.d.mts +202 -1
  216. package/esm/types.d.mts.map +1 -1
  217. package/esm/ui-root.mjs +9 -4
  218. package/esm/ui-root.mjs.map +1 -1
  219. package/llms-full.txt +159 -21
  220. package/llms.txt +2 -2
  221. package/package.json +32 -8
  222. package/skills/SKILL.md +159 -21
  223. package/ui/dashboard/assets/app.css +3 -0
  224. package/ui/dashboard/assets/app.js +50 -0
  225. package/ui/dashboard/build-info.json +21 -0
  226. package/ui/dashboard/index.html +15 -0
  227. package/cjs/ai-source-Dz_3tlZ5.cjs +0 -703
  228. package/cjs/ai-source-Dz_3tlZ5.cjs.map +0 -1
  229. package/cjs/server-B11ShQYE.cjs +0 -628
  230. package/cjs/server-B11ShQYE.cjs.map +0 -1
  231. package/cjs/src-Xwm3Eel9.cjs.map +0 -1
  232. package/ui/app.css +0 -376
  233. package/ui/app.js +0 -1158
  234. package/ui/index.html +0 -28
@@ -1,628 +0,0 @@
1
- const require_ai_source = require("./ai-source-Dz_3tlZ5.cjs");
2
- let node_fs = require("node:fs");
3
- let node_path = require("node:path");
4
- node_path = require_ai_source.__toESM(node_path, 1);
5
-
6
- //#region ../../packages/devtools/src/server/ai-routes.ts
7
- /** Read-only AI metadata, inside the same development/loopback guard as every panel. */
8
- function registerAiRoutes(server, deps) {
9
- server.get("/api/ai", async (request, reply) => {
10
- reply.header("Cache-Control", "no-store");
11
- const source = deps.ai;
12
- if (!source?.available) return {
13
- available: false,
14
- message: source?.message ?? "Install @warlock.js/ai and @warlock.js/ai-panoptic, then restart warlock dev. Import @warlock.js/ai-panoptic and enable ai.config({ panoptic: { observeAll: true } }). If already installed, check that both packages are compatible and can be loaded.",
15
- runs: []
16
- };
17
- const filter = request.query.requestId === void 0 ? void 0 : { requestId: request.query.requestId };
18
- return {
19
- available: true,
20
- runs: source.store.query(filter).map(source.summarize),
21
- aggregate: source.store.aggregate(filter)
22
- };
23
- });
24
- server.get("/api/ai/agent-runs", async (request, reply) => {
25
- reply.header("Cache-Control", "no-store");
26
- const source = deps.agentTraces;
27
- if (!source?.available()) return {
28
- available: false,
29
- message: require_ai_source.AGENT_TRACE_INSTALL_GUIDANCE,
30
- runs: []
31
- };
32
- return {
33
- available: true,
34
- runs: source.list(request.query.requestId === void 0 ? void 0 : { requestId: request.query.requestId })
35
- };
36
- });
37
- server.get("/api/ai/agent-runs/:id", async (request, reply) => {
38
- reply.header("Cache-Control", "no-store");
39
- const trace = deps.agentTraces?.get(request.params.id);
40
- if (!trace) return reply.code(404).send();
41
- return trace;
42
- });
43
- server.get("/api/ai/runs/:id", async (request, reply) => {
44
- reply.header("Cache-Control", "no-store");
45
- const source = deps.ai;
46
- const trace = source?.available ? source.store.get(request.params.id) : void 0;
47
- if (!trace) return reply.code(404).send();
48
- return trace;
49
- });
50
- }
51
-
52
- //#endregion
53
- //#region ../../packages/devtools/src/server/background-routes.ts
54
- /** Registers the route that returns work recorded outside any request. */
55
- function registerBackgroundRoutes(server, deps) {
56
- server.get("/api/background", async () => deps.collector.getBackground());
57
- }
58
-
59
- //#endregion
60
- //#region ../../packages/devtools/src/server/explain-route.ts
61
- /**
62
- * The value the logger's redaction substitutes for a censored leaf
63
- * (`logger/src/redact/redact.ts`, `DEFAULT_CENSOR`). Not exported from
64
- * `@warlock.js/logger`, so matched by literal here.
65
- */
66
- const REDACTED_PLACEHOLDER = "[REDACTED]";
67
- function isExplainableSql(sql) {
68
- return /^\s*(select|with)\b/i.test(sql ?? "");
69
- }
70
- /** Registers the EXPLAIN route: Postgres `SELECT`/`WITH` queries only. */
71
- function registerExplainRoute(server, deps) {
72
- server.post("/api/requests/:requestId/queries/:queryId/explain", async (request, reply) => {
73
- const query = deps.collector.getRequest(request.params.requestId)?.queries.find((item) => item.id === request.params.queryId);
74
- if (!query) {
75
- reply.code(404).send();
76
- return;
77
- }
78
- if (query.driver !== "postgres" || !isExplainableSql(query.sql) || !deps.explain) {
79
- reply.code(400).send({ error: "Only Postgres SELECT or WITH queries can be explained" });
80
- return;
81
- }
82
- if (query.bindings?.some((binding) => binding === REDACTED_PLACEHOLDER)) {
83
- reply.code(400).send({ error: "bindings redacted" });
84
- return;
85
- }
86
- try {
87
- return { plan: await deps.explain(query) };
88
- } catch (error) {
89
- reply.code(500).send({ error: error instanceof Error ? error.message : String(error) });
90
- }
91
- });
92
- }
93
-
94
- //#endregion
95
- //#region ../../packages/devtools/src/server/guard.ts
96
- const LOOPBACK_ADDRESSES = /* @__PURE__ */ new Set([
97
- "127.0.0.1",
98
- "::1",
99
- "::ffff:127.0.0.1"
100
- ]);
101
- /** Host names that always reach this machine, so DNS cannot be pointed at them. */
102
- const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
103
- "localhost",
104
- "127.0.0.1",
105
- "[::1]"
106
- ]);
107
- /** Headers a proxy or tunnel adds; their presence means the loopback peer is not the visitor. */
108
- const PROXY_HEADERS = [
109
- "x-forwarded-for",
110
- "x-forwarded-host",
111
- "x-real-ip",
112
- "forwarded"
113
- ];
114
- /** At most this many distinct refusals are logged per boot. */
115
- const MAX_WARNINGS = 20;
116
- /**
117
- * Whether `address` is a loopback address. Deliberately reads
118
- * `request.socket.remoteAddress` rather than `request.ip`, which honours
119
- * `X-Forwarded-For` when the app trusts a proxy — a header any client can
120
- * set.
121
- */
122
- function isLoopbackAddress(address) {
123
- return address !== void 0 && LOOPBACK_ADDRESSES.has(address);
124
- }
125
- /** The lower-cased host name of a `Host` header value, without its port (`[::1]` keeps its brackets). */
126
- function hostnameOf(host) {
127
- const value = host.trim().toLowerCase();
128
- if (value.startsWith("[")) {
129
- const end = value.indexOf("]");
130
- return end === -1 ? "" : value.slice(0, end + 1);
131
- }
132
- const colon = value.indexOf(":");
133
- const name = colon === -1 ? value : value.slice(0, colon);
134
- return name.endsWith(".") ? name.slice(0, -1) : name;
135
- }
136
- /**
137
- * Whether `hostname` is a loopback name or one of `allowedHosts`. An entry
138
- * starting with `.` matches that domain and every subdomain of it.
139
- */
140
- function isAllowedDevtoolsHost(hostname, allowedHosts = []) {
141
- if (!hostname) return false;
142
- if (LOOPBACK_HOSTS.has(hostname) || hostname.endsWith(".localhost")) return true;
143
- return allowedHosts.some((entry) => {
144
- const allowed = entry.trim().toLowerCase();
145
- if (allowed.startsWith(".")) return hostname === allowed.slice(1) || hostname.endsWith(allowed);
146
- return hostname === allowed;
147
- });
148
- }
149
- /** The `Origin` header's host name, `""` for `null` or an unparsable value. */
150
- function originHostname(origin) {
151
- try {
152
- return new URL(origin).hostname.toLowerCase();
153
- } catch {
154
- return "";
155
- }
156
- }
157
- /**
158
- * Why `request` must be refused, or `undefined` when it may be served.
159
- * `"not-local"` is the silent case (production, or a remote socket). Beyond a
160
- * loopback socket, the request must be ADDRESSED to a loopback (or allowed)
161
- * host, come from such an origin, and not have passed through a proxy: a page
162
- * whose domain was re-pointed at 127.0.0.1, or a tunnel, also arrives on a
163
- * loopback socket.
164
- */
165
- function refusalReason(request, deps) {
166
- if (!deps.isDevelopment() || !isLoopbackAddress(request.socket.remoteAddress)) return "not-local";
167
- const allowedHosts = deps.options.allowedHosts ?? [];
168
- const host = hostnameOf(request.headers.host ?? "");
169
- if (!isAllowedDevtoolsHost(host, allowedHosts)) return `Devtools refused a request for host "${host}". Open the dashboard on localhost, or add the host to the devtools "allowedHosts" option.`;
170
- const origin = request.headers.origin;
171
- if (origin !== void 0 && !isAllowedDevtoolsHost(originHostname(origin), allowedHosts)) return `Devtools refused a request from origin "${origin}". Only pages served from localhost or a host in the devtools "allowedHosts" option may call the dashboard.`;
172
- if (!deps.options.allowProxied && PROXY_HEADERS.some((name) => request.headers[name] !== void 0)) return "Devtools refused a proxied request (it carries a forwarding header). Open the dashboard directly on localhost, or set the devtools \"allowProxied\" option for a local proxy nobody else can reach.";
173
- }
174
- /**
175
- * Registers the dev-only, local-only guard as an `onRequest` hook on
176
- * `server`. Callers must register this inside an encapsulated Fastify
177
- * context (a plugin/prefix) so the hook never reaches routes outside the
178
- * dashboard.
179
- */
180
- function registerDevtoolsGuard(server, deps) {
181
- const warned = /* @__PURE__ */ new Set();
182
- server.addHook("onRequest", async (request, reply) => {
183
- const reason = refusalReason(request, deps);
184
- if (reason === void 0) return;
185
- if (reason !== "not-local" && !warned.has(reason) && warned.size < MAX_WARNINGS) {
186
- warned.add(reason);
187
- deps.warn?.(reason);
188
- }
189
- return reply.code(404).send();
190
- });
191
- }
192
-
193
- //#endregion
194
- //#region ../../packages/devtools/src/server/log-routes.ts
195
- /** Registers the log listing route, filterable by `level` and `module`. */
196
- function registerLogRoutes(server, deps) {
197
- server.get("/api/logs", async (request) => deps.collector.listLogs({
198
- level: request.query.level,
199
- module: request.query.module
200
- }));
201
- }
202
-
203
- //#endregion
204
- //#region ../../packages/devtools/src/server/mail-routes.ts
205
- const MAIL_HTML_CSP = "sandbox; default-src 'none'; img-src data: https: http:; style-src 'unsafe-inline'";
206
- /** Registers the mailbox list, single-mail detail, and mail HTML routes. */
207
- function registerMailRoutes(server, deps) {
208
- server.get("/api/mails", async () => deps.collector.listMails());
209
- server.get("/api/mails/:id", async (request, reply) => {
210
- const mail = deps.collector.getMail(request.params.id);
211
- if (!mail) {
212
- reply.code(404).send();
213
- return;
214
- }
215
- const { html, ...rest } = mail;
216
- return rest;
217
- });
218
- server.get("/api/mails/:id/html", async (request, reply) => {
219
- const mail = deps.collector.getMail(request.params.id);
220
- if (!mail || !mail.html) {
221
- reply.code(404).send();
222
- return;
223
- }
224
- reply.header("Content-Security-Policy", MAIL_HTML_CSP).header("X-Content-Type-Options", "nosniff").header("Content-Type", "text/html; charset=utf-8").send(mail.html);
225
- });
226
- }
227
-
228
- //#endregion
229
- //#region ../../packages/devtools/src/server/ui-assets.ts
230
- /** Where `index.html` takes the registered panels' tab buttons. */
231
- const PANEL_TABS_MARKER = "<!-- devtools:panel-tabs -->";
232
- const uiFile = (name) => () => node_path.default.join(require_ai_source.resolveUiRoot(), name);
233
- /**
234
- * The only assets that can be requested. The request's asset name is looked
235
- * up here and never joined into a filesystem path.
236
- */
237
- const UI_ASSETS = {
238
- "app.js": {
239
- contentType: "text/javascript; charset=utf-8",
240
- file: uiFile("app.js")
241
- },
242
- "app.css": {
243
- contentType: "text/css; charset=utf-8",
244
- file: uiFile("app.css")
245
- },
246
- "scalar.js": {
247
- contentType: "text/javascript; charset=utf-8",
248
- file: uiFile("vendor/scalar/standalone.js")
249
- },
250
- "swagger-ui-bundle.js": {
251
- contentType: "text/javascript; charset=utf-8",
252
- file: uiFile("vendor/swagger-ui/swagger-ui-bundle.js")
253
- },
254
- "swagger-ui.css": {
255
- contentType: "text/css; charset=utf-8",
256
- file: uiFile("vendor/swagger-ui/swagger-ui.css")
257
- },
258
- "docs-swagger.js": {
259
- contentType: "text/javascript; charset=utf-8",
260
- file: uiFile("docs-swagger.js")
261
- }
262
- };
263
- /** Sends `file` with `contentType` and no caching. */
264
- function sendFile(reply, file, contentType) {
265
- reply.header("Cache-Control", "no-store").header("Content-Type", contentType).send((0, node_fs.readFileSync)(file));
266
- }
267
- function escapeHtml(value) {
268
- return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;");
269
- }
270
- /** `index.html` with one tab button per registered panel, in registration order. */
271
- function renderIndexHtml(html, panels) {
272
- const buttons = panels.map((panel) => `<button class="tab" data-tab="${panel.id}" data-panel="${panel.id}">${escapeHtml(panel.title)}</button>`).join("\n ");
273
- return html.replace(PANEL_TABS_MARKER, buttons);
274
- }
275
- /**
276
- * Serves the static dashboard: `index.html` at the dashboard root (with a
277
- * tab per registered panel) and the whitelisted assets (`app.js`, `app.css`,
278
- * the vendored Scalar bundle as `scalar.js`, and the vendored Swagger UI
279
- * files plus their init script). The asset name is matched against a fixed
280
- * whitelist — never joined into a filesystem path — so no request can
281
- * traverse outside it.
282
- */
283
- function registerUiAssets(server, panels = []) {
284
- const serveIndex = (_request, reply) => {
285
- const html = (0, node_fs.readFileSync)(node_path.default.join(require_ai_source.resolveUiRoot(), "index.html"), "utf8");
286
- reply.header("Cache-Control", "no-store").header("Content-Type", "text/html; charset=utf-8").send(renderIndexHtml(html, panels));
287
- };
288
- server.get("/", serveIndex);
289
- server.get("/assets/:name", (request, reply) => {
290
- const asset = Object.hasOwn(UI_ASSETS, request.params.name) ? UI_ASSETS[request.params.name] : void 0;
291
- if (!asset) {
292
- reply.code(404).header("Cache-Control", "no-store").send();
293
- return;
294
- }
295
- sendFile(reply, asset.file(), asset.contentType);
296
- });
297
- }
298
-
299
- //#endregion
300
- //#region ../../packages/devtools/src/server/openapi-routes.ts
301
- /**
302
- * Registers the API docs: `GET /api/openapi.json` returns the document core
303
- * builds from the registered routes (`deps.getOpenApiDocument`), and
304
- * `GET /docs` serves the page that renders it with Scalar and
305
- * `GET /docs/swagger` the same document in Swagger UI;
306
- * `GET /api/postman.json` returns the Postman collection core builds
307
- * (`deps.getPostmanCollection`). The pages use relative URLs, so it works under whatever base path the dashboard is
308
- * mounted on.
309
- */
310
- function registerOpenApiRoutes(server, deps) {
311
- server.get("/api/openapi.json", async (_request, reply) => {
312
- if (!deps.getOpenApiDocument) return reply.code(503).header("Cache-Control", "no-store").send({ error: "The OpenAPI document is not available: the host did not provide a generator." });
313
- reply.header("Cache-Control", "no-store");
314
- return deps.getOpenApiDocument();
315
- });
316
- server.get("/api/postman.json", async (_request, reply) => {
317
- if (!deps.getPostmanCollection) return reply.code(503).header("Cache-Control", "no-store").send({ error: "The Postman collection is not available: the host did not provide a generator." });
318
- reply.header("Cache-Control", "no-store");
319
- return deps.getPostmanCollection();
320
- });
321
- server.get("/docs", (_request, reply) => {
322
- sendFile(reply, node_path.default.join(require_ai_source.resolveUiRoot(), "docs.html"), "text/html; charset=utf-8");
323
- });
324
- server.get("/docs/swagger", (_request, reply) => {
325
- sendFile(reply, node_path.default.join(require_ai_source.resolveUiRoot(), "docs-swagger.html"), "text/html; charset=utf-8");
326
- });
327
- }
328
-
329
- //#endregion
330
- //#region ../../packages/devtools/src/server/panel-routes.ts
331
- /**
332
- * Mounts each panel's data routes at `/api/<id><route path>` and its view
333
- * module and assets at `/panels/<id>/<name>`. Callers register this inside
334
- * the devtools scope, so the guard covers every one of them. A file is
335
- * served only when its name is listed by the panel; the request never
336
- * becomes a filesystem path.
337
- */
338
- function registerPanelRoutes(server, panels) {
339
- const byId = new Map(panels.map((panel) => [panel.id, panel]));
340
- for (const panel of panels) for (const route of panel.routes) server.route({
341
- method: route.method,
342
- url: `/api/${panel.id}${route.path}`,
343
- handler: async (request, reply) => {
344
- const input = {
345
- params: request.params ?? {},
346
- query: request.query ?? {},
347
- body: request.body
348
- };
349
- const result = await route.handler(input);
350
- reply.header("cache-control", "no-store");
351
- if (result === void 0) return reply.code(204).send();
352
- return result;
353
- }
354
- });
355
- server.get("/panels/:id/:name", (request, reply) => {
356
- const panel = byId.get(request.params.id);
357
- const name = request.params.name;
358
- const file = panel === void 0 ? void 0 : name === "view.js" ? panel.view : Object.hasOwn(panel.assets, name) ? panel.assets[name] : void 0;
359
- if (file === void 0) {
360
- reply.code(404).header("Cache-Control", "no-store").send();
361
- return;
362
- }
363
- reply.header("X-Content-Type-Options", "nosniff");
364
- sendFile(reply, file, require_ai_source.panelContentType(name));
365
- });
366
- }
367
-
368
- //#endregion
369
- //#region ../../packages/devtools/src/request-history.ts
370
- /** The list row filters run over, from an in-memory summary. */
371
- function summaryToRow(summary) {
372
- return {
373
- id: summary.id,
374
- time: new Date(summary.startedAt).toISOString(),
375
- method: summary.method,
376
- url: summary.path,
377
- ...summary.route ? { route: summary.route } : {},
378
- status: summary.status ?? 0,
379
- durationMs: summary.durationMs ?? 0
380
- };
381
- }
382
- /** An in-memory-shaped list row from a persisted one. */
383
- function rowToSummary(row) {
384
- return {
385
- id: row.id,
386
- method: row.method,
387
- path: row.url,
388
- route: row.route,
389
- status: row.status,
390
- startedAt: Date.parse(row.time),
391
- durationMs: row.durationMs,
392
- queryCount: row.queryCount ?? 0,
393
- warningCount: row.nPlusOneCount ?? 0,
394
- persisted: true
395
- };
396
- }
397
- /**
398
- * Merge the live ring buffer with the persisted log, newest first, without
399
- * duplicates: a request in both is shown from memory (it has the full
400
- * timeline). Persisted rows fill in what a restart or the ring buffer's size
401
- * evicted.
402
- */
403
- function mergeRequestHistory(live, persisted, limit) {
404
- const seen = new Set(live.map((summary) => summary.id));
405
- const merged = [...live];
406
- for (const row of persisted) {
407
- if (seen.has(row.id)) continue;
408
- seen.add(row.id);
409
- merged.push(rowToSummary(row));
410
- }
411
- return merged.sort((first, second) => second.startedAt - first.startedAt).slice(0, limit);
412
- }
413
- const FILTER_KEYS = [
414
- "status",
415
- "route",
416
- "method",
417
- "since",
418
- "until",
419
- "q"
420
- ];
421
- /** The request log filter in a `GET /api/requests` query string. */
422
- function filterFromQuery(query) {
423
- const filter = {};
424
- for (const key of FILTER_KEYS) {
425
- const value = query[key];
426
- if (typeof value === "string" && value.trim() !== "") filter[key] = value.trim();
427
- }
428
- const limit = Number(query.limit);
429
- if (Number.isInteger(limit) && limit > 0) filter.limit = limit;
430
- return filter;
431
- }
432
- /** Whether a filter narrows anything. */
433
- function hasFilter(filter) {
434
- return FILTER_KEYS.some((key) => filter[key] !== void 0);
435
- }
436
- function toLogLevel(level) {
437
- return level === "debug" || level === "info" || level === "warn" || level === "success" ? level : "error";
438
- }
439
- /**
440
- * A request detail rebuilt from a persisted record, in the shape of an
441
- * in-memory one: phases, logs and N+1 warnings carry over; query-by-query
442
- * and cache detail live only in memory, so those lists are empty. `record`
443
- * has everything the log kept.
444
- */
445
- function recordToDetail(record) {
446
- const startedAt = Date.parse(record.time);
447
- const warnings = (record.queries?.nPlusOne ?? []).map((finding) => ({
448
- type: "n+1",
449
- message: `${finding.query} ran ${finding.count} times`,
450
- shape: finding.query,
451
- count: finding.count
452
- }));
453
- return {
454
- id: record.id,
455
- method: record.method,
456
- path: record.url,
457
- route: record.route.pattern,
458
- status: record.status,
459
- startedAt,
460
- durationMs: record.durationMs,
461
- error: record.error?.message,
462
- phases: (record.phases ?? []).map((phase) => ({
463
- name: phase.name,
464
- startedAt,
465
- durationMs: phase.durationMs
466
- })),
467
- queries: [],
468
- cache: [],
469
- logs: (record.logs ?? []).map((entry) => ({
470
- level: toLogLevel(entry.level),
471
- module: entry.module,
472
- action: entry.action,
473
- message: entry.message,
474
- at: entry.at,
475
- ...entry.context ? { context: entry.context } : {}
476
- })),
477
- mails: [],
478
- warnings,
479
- persisted: true,
480
- record
481
- };
482
- }
483
-
484
- //#endregion
485
- //#region ../../packages/devtools/src/server/requests-routes.ts
486
- /**
487
- * Registers the request list, single-request detail, "copy as curl", and
488
- * clear routes.
489
- *
490
- * With core's persistent request log available, the list and the detail also
491
- * read the JSON-lines history, so requests survive a restart. The list
492
- * accepts `status` (404 or 5xx), `route`, `method`, `since`, `until`, `q`
493
- * and `limit` query parameters.
494
- */
495
- function registerRequestsRoutes(server, deps) {
496
- server.get("/api/requests", async (request, reply) => {
497
- const { requestLog, collector, options } = deps;
498
- if (!requestLog) return collector.listRequests();
499
- try {
500
- const filter = filterFromQuery(request.query);
501
- const matches = requestLog.compile(filter);
502
- const live = hasFilter(filter) ? collector.listRequests().filter((summary) => matches(summaryToRow(summary))) : collector.listRequests();
503
- const limit = filter.limit ?? options.maxRequests;
504
- const persisted = await requestLog.list({
505
- ...filter,
506
- limit
507
- });
508
- return mergeRequestHistory(live, persisted, limit);
509
- } catch (error) {
510
- if (error instanceof Error && /^Invalid /.test(error.message)) {
511
- reply.code(400).send({ error: error.message });
512
- return;
513
- }
514
- return collector.listRequests();
515
- }
516
- });
517
- server.get("/api/requests/:id", async (request, reply) => {
518
- const { id } = request.params;
519
- const persisted = await deps.requestLog?.get(id).catch(() => void 0);
520
- const found = deps.collector.getRequest(id);
521
- if (found) return persisted ? {
522
- ...found,
523
- record: persisted
524
- } : found;
525
- if (!persisted) {
526
- reply.code(404).send();
527
- return;
528
- }
529
- return recordToDetail(persisted);
530
- });
531
- server.get("/api/requests/:id/curl", async (request, reply) => {
532
- const { id } = request.params;
533
- const baseUrl = `${request.protocol}://${request.headers.host ?? "localhost"}`;
534
- const persisted = await deps.requestLog?.get(id).catch(() => void 0);
535
- if (persisted && deps.requestLog) return { curl: deps.requestLog.curl(persisted, baseUrl) };
536
- const live = deps.collector.getRequest(id);
537
- if (!live) {
538
- reply.code(404).send();
539
- return;
540
- }
541
- return { curl: `curl -X ${live.method} '${baseUrl}${live.path.replace(/'/g, `'\\''`)}'` };
542
- });
543
- server.delete("/api/requests", async (_request, reply) => {
544
- deps.collector.clear();
545
- if (deps.ai?.available) deps.ai.store.clear();
546
- reply.code(204).send();
547
- });
548
- }
549
-
550
- //#endregion
551
- //#region ../../packages/devtools/src/server/route-routes.ts
552
- /** Registers the route map, sourced from core's router via `deps.listRoutes`. */
553
- function registerRouteRoutes(server, deps) {
554
- server.get("/api/routes", async () => deps.listRoutes ? deps.listRoutes() : []);
555
- }
556
-
557
- //#endregion
558
- //#region ../../packages/devtools/src/server/stream-route.ts
559
- const PING_INTERVAL_MS = 15e3;
560
- /**
561
- * Registers the SSE stream: one `event: <type>\ndata: <json>\n\n` frame per
562
- * collector event, plus a `: ping` comment every 15s to keep intermediaries
563
- * from closing an idle connection. The reply is hijacked so Fastify never
564
- * tries to serialize or close the response itself.
565
- */
566
- function registerStreamRoute(server, deps) {
567
- server.get("/api/stream", async (request, reply) => {
568
- reply.hijack();
569
- const res = reply.raw;
570
- res.writeHead(200, {
571
- "Content-Type": "text/event-stream",
572
- "Cache-Control": "no-store",
573
- Connection: "keep-alive"
574
- });
575
- const dispose = deps.collector.subscribe((event) => {
576
- res.write(`event: ${event.type}\ndata: ${JSON.stringify(event.data)}\n\n`);
577
- });
578
- const disposeAi = deps.ai?.available ? deps.ai.subscribe((event) => {
579
- res.write(`event: ai\ndata: ${JSON.stringify(event)}\n\n`);
580
- }) : void 0;
581
- const disposeAgentTraces = deps.agentTraces?.subscribe((event) => {
582
- res.write(`event: agent-trace\ndata: ${JSON.stringify(event)}\n\n`);
583
- });
584
- const pingTimer = setInterval(() => res.write(": ping\n\n"), PING_INTERVAL_MS);
585
- pingTimer.unref();
586
- request.raw.on("close", () => {
587
- clearInterval(pingTimer);
588
- dispose();
589
- disposeAi?.();
590
- disposeAgentTraces?.();
591
- });
592
- });
593
- }
594
-
595
- //#endregion
596
- //#region ../../packages/devtools/src/server/index.ts
597
- /**
598
- * Mounts the devtools dashboard and its API under `deps.options.path` on
599
- * `server`. Every route is registered inside a single encapsulated Fastify
600
- * context so the dev-only, loopback-only guard never reaches the app's
601
- * other routes — the registered panels' data routes and files included.
602
- *
603
- * Throws before mounting anything when a panel is invalid or reuses an id.
604
- */
605
- function mountDevtoolsRoutes(server, deps) {
606
- const panels = require_ai_source.checkDevtoolsPanels([require_ai_source.processesDevtoolsPanel(deps.processPanel), ...deps.panels ?? []]);
607
- server.register(async (instance) => {
608
- registerDevtoolsGuard(instance, deps);
609
- instance.setErrorHandler((error, _request, reply) => {
610
- reply.code(error.statusCode ?? 500).send({ error: error.message });
611
- });
612
- registerUiAssets(instance, panels);
613
- registerRequestsRoutes(instance, deps);
614
- registerAiRoutes(instance, deps);
615
- registerBackgroundRoutes(instance, deps);
616
- registerMailRoutes(instance, deps);
617
- registerLogRoutes(instance, deps);
618
- registerRouteRoutes(instance, deps);
619
- registerPanelRoutes(instance, panels);
620
- registerOpenApiRoutes(instance, deps);
621
- registerExplainRoute(instance, deps);
622
- registerStreamRoute(instance, deps);
623
- }, { prefix: deps.options.path });
624
- }
625
-
626
- //#endregion
627
- exports.mountDevtoolsRoutes = mountDevtoolsRoutes;
628
- //# sourceMappingURL=server-B11ShQYE.cjs.map