@valbuild/server 0.157.0 → 0.158.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 (36) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/browser/dist/valbuild-server-browser.cjs.dev.js +3 -3
  3. package/browser/dist/valbuild-server-browser.cjs.prod.js +3 -3
  4. package/browser/dist/valbuild-server-browser.esm.js +3 -3
  5. package/dist/{ValOpsMemory-56cd8c26.cjs.dev.js → ValOpsMemory-a7b04349.cjs.dev.js} +2 -1
  6. package/dist/{ValOpsMemory-9470f349.cjs.prod.js → ValOpsMemory-bb148f59.cjs.prod.js} +2 -1
  7. package/dist/{ValOpsMemory-e534f2ff.esm.js → ValOpsMemory-c54956f0.esm.js} +2 -2
  8. package/dist/{ValRouterCore-65bb6323.cjs.prod.js → ValRouterCore-062cfff6.cjs.prod.js} +77 -0
  9. package/dist/{ValRouterCore-adb69698.cjs.dev.js → ValRouterCore-1bbc8dd2.cjs.dev.js} +77 -0
  10. package/dist/{ValRouterCore-f7d6394e.esm.js → ValRouterCore-bfaee688.esm.js} +78 -1
  11. package/dist/{createValRuntime-aff8e536.cjs.prod.js → createValRuntime-1db76229.cjs.prod.js} +2 -2
  12. package/dist/{createValRuntime-ff609f66.esm.js → createValRuntime-27c0301e.esm.js} +2 -2
  13. package/dist/{createValRuntime-0de65330.cjs.dev.js → createValRuntime-46935850.cjs.dev.js} +2 -2
  14. package/dist/declarations/src/index.d.ts +4 -0
  15. package/dist/declarations/src/siteErrors.d.ts +90 -0
  16. package/dist/declarations/src/siteLogs.d.ts +136 -0
  17. package/dist/declarations/src/valBuildRead.d.ts +44 -0
  18. package/dist/{inTabFetch-7cbaa9de.cjs.dev.js → inTabFetch-0b1870b3.cjs.dev.js} +2 -2
  19. package/dist/{inTabFetch-b1001caf.cjs.prod.js → inTabFetch-4cf9f7a6.cjs.prod.js} +2 -2
  20. package/dist/{inTabFetch-8278c58e.esm.js → inTabFetch-cbbb1977.esm.js} +2 -2
  21. package/dist/{publishKit-9b32f7f6.cjs.prod.js → publishKit-0dd1be10.cjs.prod.js} +1 -1
  22. package/dist/{publishKit-a90d22ad.cjs.dev.js → publishKit-0df51bbb.cjs.dev.js} +1 -1
  23. package/dist/{publishKit-bbca36c0.esm.js → publishKit-a37ad1ca.esm.js} +1 -1
  24. package/dist/{valRuntime-26ed874d.esm.js → valRuntime-4cbd82ee.esm.js} +3 -3
  25. package/dist/{valRuntime-9ac56da5.cjs.prod.js → valRuntime-5819c89d.cjs.prod.js} +3 -3
  26. package/dist/{valRuntime-3e709d67.cjs.dev.js → valRuntime-ffd8d8bd.cjs.dev.js} +3 -3
  27. package/dist/valbuild-server.cjs.dev.js +234 -5
  28. package/dist/valbuild-server.cjs.prod.js +234 -5
  29. package/dist/valbuild-server.esm.js +244 -18
  30. package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +3 -3
  31. package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +3 -3
  32. package/internal/browser/dist/valbuild-server-internal-browser.esm.js +3 -3
  33. package/internal/dist/valbuild-server-internal.cjs.dev.js +1 -1
  34. package/internal/dist/valbuild-server-internal.cjs.prod.js +1 -1
  35. package/internal/dist/valbuild-server-internal.esm.js +1 -1
  36. package/package.json +4 -4
@@ -11,23 +11,23 @@ var ts = require('typescript');
11
11
  var path = require('path');
12
12
  var fs = require('fs');
13
13
  var jsonValuesPatch = require('./jsonValuesPatch-88bc661d.cjs.dev.js');
14
- var ValOpsMemory = require('./ValOpsMemory-56cd8c26.cjs.dev.js');
14
+ var ValOpsMemory = require('./ValOpsMemory-a7b04349.cjs.dev.js');
15
15
  var internal = require('@valbuild/shared/internal');
16
16
  var server = require('@valbuild/ui/server');
17
- var valRuntime = require('./valRuntime-3e709d67.cjs.dev.js');
17
+ var valRuntime = require('./valRuntime-ffd8d8bd.cjs.dev.js');
18
18
  var http = require('node:http');
19
19
  var https = require('node:https');
20
20
  var net = require('node:net');
21
21
  var sizeOf = require('image-size');
22
22
  var http$1 = require('http');
23
23
  var https$1 = require('https');
24
- var ValRouterCore = require('./ValRouterCore-adb69698.cjs.dev.js');
24
+ var ValRouterCore = require('./ValRouterCore-1bbc8dd2.cjs.dev.js');
25
25
  var createPrettierFormatter = require('./createPrettierFormatter-a9109288.cjs.dev.js');
26
26
  var proposalJsonEntries = require('./proposalJsonEntries-878ad5b6.cjs.dev.js');
27
+ var z = require('zod');
27
28
  var sucrase = require('sucrase');
28
29
  var fs$1 = require('fs/promises');
29
30
  var vm = require('node:vm');
30
- var z = require('zod');
31
31
  var os = require('os');
32
32
  var ValPatchStore = require('./ValPatchStore-b53db740.cjs.dev.js');
33
33
  require('crypto');
@@ -64,9 +64,9 @@ var net__default = /*#__PURE__*/_interopDefault(net);
64
64
  var sizeOf__default = /*#__PURE__*/_interopDefault(sizeOf);
65
65
  var http__default$1 = /*#__PURE__*/_interopDefault(http$1);
66
66
  var https__default$1 = /*#__PURE__*/_interopDefault(https$1);
67
+ var z__default = /*#__PURE__*/_interopDefault(z);
67
68
  var fs__default$1 = /*#__PURE__*/_interopDefault(fs$1);
68
69
  var vm__default = /*#__PURE__*/_interopDefault(vm);
69
- var z__default = /*#__PURE__*/_interopDefault(z);
70
70
  var os__default = /*#__PURE__*/_interopDefault(os);
71
71
 
72
72
  function getSyntheticContainingPath(rootDir) {
@@ -5605,6 +5605,232 @@ function createValApiRouter(route, valServerPromise, convert) {
5605
5605
  return ValRouterCore.createValRequestHandler(route, valServerPromise, convert, uiRequestHandler);
5606
5606
  }
5607
5607
 
5608
+ /**
5609
+ * One GET to Val Build's `/v1/{org}/{project}/…` with a developer's `val login`
5610
+ * token or the project's API key, read against a schema: what `fetchSiteErrors`
5611
+ * and `fetchSiteLogs` share, so both say the same things about the same
5612
+ * failures.
5613
+ */
5614
+
5615
+ async function readFromValBuild({
5616
+ contentUrl,
5617
+ project,
5618
+ resource,
5619
+ query,
5620
+ credential,
5621
+ schema,
5622
+ invalid,
5623
+ fetchImpl = fetch,
5624
+ timeoutMs = 15_000
5625
+ }) {
5626
+ const [org, name] = project.split("/");
5627
+ const url = new URL(`v1/${encodeURIComponent(org ?? "")}/${encodeURIComponent(name ?? "")}/${resource}`, contentUrl.endsWith("/") ? contentUrl : `${contentUrl}/`);
5628
+ for (const [key, value] of Object.entries(query)) {
5629
+ url.searchParams.set(key, value);
5630
+ }
5631
+ let res;
5632
+ try {
5633
+ res = await fetchImpl(url, {
5634
+ headers: {
5635
+ accept: "application/json",
5636
+ ...("pat" in credential ? {
5637
+ "x-val-pat": credential.pat
5638
+ } : {
5639
+ authorization: `Bearer ${credential.apiKey}`
5640
+ })
5641
+ },
5642
+ // A credential goes to the host it was meant for, and nowhere a
5643
+ // redirect would send it.
5644
+ redirect: "manual",
5645
+ signal: AbortSignal.timeout(timeoutMs)
5646
+ });
5647
+ } catch (err) {
5648
+ return {
5649
+ status: "error",
5650
+ reason: "unreachable",
5651
+ message: err instanceof Error ? err.message : String(err)
5652
+ };
5653
+ }
5654
+ const body = await res.json().catch(() => null);
5655
+ if (!res.ok) {
5656
+ return {
5657
+ status: "error",
5658
+ reason: reasonOf(res.status),
5659
+ message: messageOf(body) ?? `Val Build answered ${res.status}.`
5660
+ };
5661
+ }
5662
+ const parsed = schema.safeParse(body);
5663
+ if (!parsed.success) {
5664
+ return {
5665
+ status: "error",
5666
+ reason: "invalid-response",
5667
+ message: invalid
5668
+ };
5669
+ }
5670
+ return {
5671
+ status: "ok",
5672
+ answer: parsed.data
5673
+ };
5674
+ }
5675
+ function reasonOf(status) {
5676
+ if (status === 401) return "unauthenticated";
5677
+ if (status === 403) return "forbidden";
5678
+ if (status === 404) return "not-found";
5679
+ if (status >= 500) return "unavailable";
5680
+ return "invalid-response";
5681
+ }
5682
+ function messageOf(body) {
5683
+ if (typeof body === "object" && body !== null && "message" in body && typeof body.message === "string") {
5684
+ return body.message;
5685
+ }
5686
+ return null;
5687
+ }
5688
+
5689
+ /**
5690
+ * A hosted site's recent errors, from Val Build:
5691
+ * `GET {valContentUrl}/v1/{org}/{project}/site-errors`. What `val errors` and
5692
+ * the MCP tool `get_site_errors` read. The Studio reads the same answer through
5693
+ * its admin proxy, as a web component served by Val Build.
5694
+ *
5695
+ * Errors are kept only for sites Val Build hosts: the platform's tail is what
5696
+ * sees them. A site served anywhere else answers `not-hosted`, which is said
5697
+ * rather than shown as an empty list, so "no errors" never means "nobody is
5698
+ * watching". See `docs/site-errors.md` in valbuild/home.
5699
+ *
5700
+ * Either credential `resolveRemoteFileAuth` gives works: the project's API key
5701
+ * or a developer's `val login` token.
5702
+ */
5703
+
5704
+ const SiteErrorSchema = z.z.object({
5705
+ fingerprint: z.z.string(),
5706
+ kind: z.z.enum(["exception", "console", "limit", "status"]),
5707
+ level: z.z.enum(["error", "warn"]),
5708
+ message: z.z.string(),
5709
+ stack: z.z.string().nullable(),
5710
+ path: z.z.string().nullable(),
5711
+ method: z.z.string().nullable(),
5712
+ status: z.z.number().nullable(),
5713
+ count: z.z.number(),
5714
+ firstSeen: z.z.string(),
5715
+ lastSeen: z.z.string(),
5716
+ firstBuild: z.z.string().nullable(),
5717
+ lastBuild: z.z.string().nullable(),
5718
+ proposal: z.z.string().nullable()
5719
+ });
5720
+ const SiteErrorsAnswerSchema = z.z.discriminatedUnion("status", [z.z.object({
5721
+ status: z.z.literal("ok"),
5722
+ errors: z.z.array(SiteErrorSchema),
5723
+ retentionDays: z.z.number(),
5724
+ liveBuild: z.z.string().nullable(),
5725
+ dropped: z.z.boolean()
5726
+ }), z.z.object({
5727
+ status: z.z.literal("not-hosted")
5728
+ })]);
5729
+ function fetchSiteErrors({
5730
+ limit,
5731
+ ...options
5732
+ }) {
5733
+ return readFromValBuild({
5734
+ ...options,
5735
+ resource: "site-errors",
5736
+ query: limit !== undefined ? {
5737
+ limit: String(limit)
5738
+ } : {},
5739
+ schema: SiteErrorsAnswerSchema,
5740
+ invalid: "Val Build's answer was not a list of site errors."
5741
+ });
5742
+ }
5743
+
5744
+ /**
5745
+ * A hosted site's last hour of requests, from Val Build:
5746
+ * `GET {valContentUrl}/v1/{org}/{project}/logs`. What `val logs` and the MCP
5747
+ * tool `get_logs` read. The Studio reads the same answer through its admin
5748
+ * proxy, as a web component served by Val Build.
5749
+ *
5750
+ * Each request is what the site's server did with one visit: where, how it
5751
+ * ended, how long it took, and every line it printed. Kept for an hour, for
5752
+ * sites Val Build hosts; a site served anywhere else answers `not-hosted`.
5753
+ * `after` asks for what arrived since an earlier answer's `cursor`, and `live`
5754
+ * tells Val Build someone is watching, so it gathers new requests every few
5755
+ * seconds instead of once a minute. See `docs/site-errors.md` in valbuild/home.
5756
+ */
5757
+
5758
+ const LineSchema = z.z.object({
5759
+ time: z.z.string(),
5760
+ level: z.z.enum(["error", "warn", "info", "debug"]),
5761
+ message: z.z.string(),
5762
+ stack: z.z.string().nullable()
5763
+ });
5764
+ const RequestSchema = z.z.object({
5765
+ id: z.z.string(),
5766
+ time: z.z.string(),
5767
+ method: z.z.string().nullable(),
5768
+ path: z.z.string().nullable(),
5769
+ status: z.z.number().nullable(),
5770
+ durationMs: z.z.number().nullable(),
5771
+ cpuMs: z.z.number().nullable(),
5772
+ outcome: z.z.enum(["ok", "exception", "cpu", "memory", "canceled"]),
5773
+ build: z.z.string().nullable(),
5774
+ proposal: z.z.string().nullable(),
5775
+ lines: z.z.array(LineSchema),
5776
+ errorFingerprint: z.z.string().nullable()
5777
+ });
5778
+ const SiteLogsAnswerSchema = z.z.discriminatedUnion("status", [z.z.object({
5779
+ status: z.z.literal("ok"),
5780
+ requests: z.z.array(RequestSchema),
5781
+ cursor: z.z.string(),
5782
+ histogram: z.z.object({
5783
+ start: z.z.string(),
5784
+ bucketMs: z.z.number(),
5785
+ buckets: z.z.array(z.z.object({
5786
+ error: z.z.number(),
5787
+ warn: z.z.number(),
5788
+ info: z.z.number()
5789
+ }))
5790
+ }),
5791
+ liveBuild: z.z.string().nullable(),
5792
+ retentionHours: z.z.number(),
5793
+ truncated: z.z.boolean()
5794
+ }), z.z.object({
5795
+ status: z.z.literal("not-hosted")
5796
+ })]);
5797
+
5798
+ /** A request's level: the worst thing about it, as Val Build filters by. */
5799
+
5800
+ function fetchSiteLogs({
5801
+ since,
5802
+ after,
5803
+ q,
5804
+ levels,
5805
+ limit,
5806
+ live,
5807
+ ...options
5808
+ }) {
5809
+ const query = {};
5810
+ if (since !== undefined) query["since"] = since.toISOString();
5811
+ if (after !== undefined) query["after"] = after;
5812
+ if (q !== undefined && q.trim() !== "") query["q"] = q.trim();
5813
+ if (levels !== undefined) query["levels"] = levels.join(",");
5814
+ if (limit !== undefined) query["limit"] = String(limit);
5815
+ if (live === true) query["live"] = "1";
5816
+ return readFromValBuild({
5817
+ ...options,
5818
+ resource: "logs",
5819
+ query,
5820
+ schema: SiteLogsAnswerSchema,
5821
+ invalid: "Val Build's answer was not the site's logs."
5822
+ });
5823
+ }
5824
+
5825
+ /** The worst thing about a request: how it ended, its status, its worst line. */
5826
+ function siteLogRequestLevel(request) {
5827
+ if (request.outcome !== "ok" && request.outcome !== "canceled" || request.status !== null && request.status >= 500 || request.lines.some(line => line.level === "error")) {
5828
+ return "error";
5829
+ }
5830
+ if (request.lines.some(line => line.level === "warn")) return "warn";
5831
+ return "info";
5832
+ }
5833
+
5608
5834
  const JsFileLookupMapping = [
5609
5835
  // NOTE: first one matching will be used
5610
5836
  [".cjs.d.ts", [".esm.js", ".mjs.js"]], [".cjs.js", [".esm.js", ".mjs.js"]], [".cjs", [".mjs"]], [".d.ts", [".js", ".esm.js", ".mjs.js"]]];
@@ -7210,6 +7436,8 @@ exports.extractImageMetadata = extractImageMetadata;
7210
7436
  exports.extractJsonValuesEntry = extractJsonValuesEntry;
7211
7437
  exports.extractVideoMetadata = extractVideoMetadata;
7212
7438
  exports.extractVideoMetadataFromFile = extractVideoMetadataFromFile;
7439
+ exports.fetchSiteErrors = fetchSiteErrors;
7440
+ exports.fetchSiteLogs = fetchSiteLogs;
7213
7441
  exports.filesOfVideo = filesOfVideo;
7214
7442
  exports.filesOfVideosetEntry = filesOfVideosetEntry;
7215
7443
  exports.findAndEvalValConfigFile = findAndEvalValConfigFile;
@@ -7240,6 +7468,7 @@ exports.planJsonValuesEntryExtraction = planJsonValuesEntryExtraction;
7240
7468
  exports.readCapturedReport = readCapturedReport;
7241
7469
  exports.replaySnapshot = replaySnapshot;
7242
7470
  exports.safeReadGit = safeReadGit;
7471
+ exports.siteLogRequestLevel = siteLogRequestLevel;
7243
7472
  exports.startValLogin = startValLogin;
7244
7473
  exports.validateMetadata = validateMetadata;
7245
7474
  exports.videosetEntryVideoSchema = videosetEntryVideoSchema;
@@ -11,23 +11,23 @@ var ts = require('typescript');
11
11
  var path = require('path');
12
12
  var fs = require('fs');
13
13
  var jsonValuesPatch = require('./jsonValuesPatch-92798812.cjs.prod.js');
14
- var ValOpsMemory = require('./ValOpsMemory-9470f349.cjs.prod.js');
14
+ var ValOpsMemory = require('./ValOpsMemory-bb148f59.cjs.prod.js');
15
15
  var internal = require('@valbuild/shared/internal');
16
16
  var server = require('@valbuild/ui/server');
17
- var valRuntime = require('./valRuntime-9ac56da5.cjs.prod.js');
17
+ var valRuntime = require('./valRuntime-5819c89d.cjs.prod.js');
18
18
  var http = require('node:http');
19
19
  var https = require('node:https');
20
20
  var net = require('node:net');
21
21
  var sizeOf = require('image-size');
22
22
  var http$1 = require('http');
23
23
  var https$1 = require('https');
24
- var ValRouterCore = require('./ValRouterCore-65bb6323.cjs.prod.js');
24
+ var ValRouterCore = require('./ValRouterCore-062cfff6.cjs.prod.js');
25
25
  var createPrettierFormatter = require('./createPrettierFormatter-8e60f596.cjs.prod.js');
26
26
  var proposalJsonEntries = require('./proposalJsonEntries-4fcfab08.cjs.prod.js');
27
+ var z = require('zod');
27
28
  var sucrase = require('sucrase');
28
29
  var fs$1 = require('fs/promises');
29
30
  var vm = require('node:vm');
30
- var z = require('zod');
31
31
  var os = require('os');
32
32
  var ValPatchStore = require('./ValPatchStore-457b166f.cjs.prod.js');
33
33
  require('crypto');
@@ -64,9 +64,9 @@ var net__default = /*#__PURE__*/_interopDefault(net);
64
64
  var sizeOf__default = /*#__PURE__*/_interopDefault(sizeOf);
65
65
  var http__default$1 = /*#__PURE__*/_interopDefault(http$1);
66
66
  var https__default$1 = /*#__PURE__*/_interopDefault(https$1);
67
+ var z__default = /*#__PURE__*/_interopDefault(z);
67
68
  var fs__default$1 = /*#__PURE__*/_interopDefault(fs$1);
68
69
  var vm__default = /*#__PURE__*/_interopDefault(vm);
69
- var z__default = /*#__PURE__*/_interopDefault(z);
70
70
  var os__default = /*#__PURE__*/_interopDefault(os);
71
71
 
72
72
  function getSyntheticContainingPath(rootDir) {
@@ -5605,6 +5605,232 @@ function createValApiRouter(route, valServerPromise, convert) {
5605
5605
  return ValRouterCore.createValRequestHandler(route, valServerPromise, convert, uiRequestHandler);
5606
5606
  }
5607
5607
 
5608
+ /**
5609
+ * One GET to Val Build's `/v1/{org}/{project}/…` with a developer's `val login`
5610
+ * token or the project's API key, read against a schema: what `fetchSiteErrors`
5611
+ * and `fetchSiteLogs` share, so both say the same things about the same
5612
+ * failures.
5613
+ */
5614
+
5615
+ async function readFromValBuild({
5616
+ contentUrl,
5617
+ project,
5618
+ resource,
5619
+ query,
5620
+ credential,
5621
+ schema,
5622
+ invalid,
5623
+ fetchImpl = fetch,
5624
+ timeoutMs = 15_000
5625
+ }) {
5626
+ const [org, name] = project.split("/");
5627
+ const url = new URL(`v1/${encodeURIComponent(org ?? "")}/${encodeURIComponent(name ?? "")}/${resource}`, contentUrl.endsWith("/") ? contentUrl : `${contentUrl}/`);
5628
+ for (const [key, value] of Object.entries(query)) {
5629
+ url.searchParams.set(key, value);
5630
+ }
5631
+ let res;
5632
+ try {
5633
+ res = await fetchImpl(url, {
5634
+ headers: {
5635
+ accept: "application/json",
5636
+ ...("pat" in credential ? {
5637
+ "x-val-pat": credential.pat
5638
+ } : {
5639
+ authorization: `Bearer ${credential.apiKey}`
5640
+ })
5641
+ },
5642
+ // A credential goes to the host it was meant for, and nowhere a
5643
+ // redirect would send it.
5644
+ redirect: "manual",
5645
+ signal: AbortSignal.timeout(timeoutMs)
5646
+ });
5647
+ } catch (err) {
5648
+ return {
5649
+ status: "error",
5650
+ reason: "unreachable",
5651
+ message: err instanceof Error ? err.message : String(err)
5652
+ };
5653
+ }
5654
+ const body = await res.json().catch(() => null);
5655
+ if (!res.ok) {
5656
+ return {
5657
+ status: "error",
5658
+ reason: reasonOf(res.status),
5659
+ message: messageOf(body) ?? `Val Build answered ${res.status}.`
5660
+ };
5661
+ }
5662
+ const parsed = schema.safeParse(body);
5663
+ if (!parsed.success) {
5664
+ return {
5665
+ status: "error",
5666
+ reason: "invalid-response",
5667
+ message: invalid
5668
+ };
5669
+ }
5670
+ return {
5671
+ status: "ok",
5672
+ answer: parsed.data
5673
+ };
5674
+ }
5675
+ function reasonOf(status) {
5676
+ if (status === 401) return "unauthenticated";
5677
+ if (status === 403) return "forbidden";
5678
+ if (status === 404) return "not-found";
5679
+ if (status >= 500) return "unavailable";
5680
+ return "invalid-response";
5681
+ }
5682
+ function messageOf(body) {
5683
+ if (typeof body === "object" && body !== null && "message" in body && typeof body.message === "string") {
5684
+ return body.message;
5685
+ }
5686
+ return null;
5687
+ }
5688
+
5689
+ /**
5690
+ * A hosted site's recent errors, from Val Build:
5691
+ * `GET {valContentUrl}/v1/{org}/{project}/site-errors`. What `val errors` and
5692
+ * the MCP tool `get_site_errors` read. The Studio reads the same answer through
5693
+ * its admin proxy, as a web component served by Val Build.
5694
+ *
5695
+ * Errors are kept only for sites Val Build hosts: the platform's tail is what
5696
+ * sees them. A site served anywhere else answers `not-hosted`, which is said
5697
+ * rather than shown as an empty list, so "no errors" never means "nobody is
5698
+ * watching". See `docs/site-errors.md` in valbuild/home.
5699
+ *
5700
+ * Either credential `resolveRemoteFileAuth` gives works: the project's API key
5701
+ * or a developer's `val login` token.
5702
+ */
5703
+
5704
+ const SiteErrorSchema = z.z.object({
5705
+ fingerprint: z.z.string(),
5706
+ kind: z.z.enum(["exception", "console", "limit", "status"]),
5707
+ level: z.z.enum(["error", "warn"]),
5708
+ message: z.z.string(),
5709
+ stack: z.z.string().nullable(),
5710
+ path: z.z.string().nullable(),
5711
+ method: z.z.string().nullable(),
5712
+ status: z.z.number().nullable(),
5713
+ count: z.z.number(),
5714
+ firstSeen: z.z.string(),
5715
+ lastSeen: z.z.string(),
5716
+ firstBuild: z.z.string().nullable(),
5717
+ lastBuild: z.z.string().nullable(),
5718
+ proposal: z.z.string().nullable()
5719
+ });
5720
+ const SiteErrorsAnswerSchema = z.z.discriminatedUnion("status", [z.z.object({
5721
+ status: z.z.literal("ok"),
5722
+ errors: z.z.array(SiteErrorSchema),
5723
+ retentionDays: z.z.number(),
5724
+ liveBuild: z.z.string().nullable(),
5725
+ dropped: z.z.boolean()
5726
+ }), z.z.object({
5727
+ status: z.z.literal("not-hosted")
5728
+ })]);
5729
+ function fetchSiteErrors({
5730
+ limit,
5731
+ ...options
5732
+ }) {
5733
+ return readFromValBuild({
5734
+ ...options,
5735
+ resource: "site-errors",
5736
+ query: limit !== undefined ? {
5737
+ limit: String(limit)
5738
+ } : {},
5739
+ schema: SiteErrorsAnswerSchema,
5740
+ invalid: "Val Build's answer was not a list of site errors."
5741
+ });
5742
+ }
5743
+
5744
+ /**
5745
+ * A hosted site's last hour of requests, from Val Build:
5746
+ * `GET {valContentUrl}/v1/{org}/{project}/logs`. What `val logs` and the MCP
5747
+ * tool `get_logs` read. The Studio reads the same answer through its admin
5748
+ * proxy, as a web component served by Val Build.
5749
+ *
5750
+ * Each request is what the site's server did with one visit: where, how it
5751
+ * ended, how long it took, and every line it printed. Kept for an hour, for
5752
+ * sites Val Build hosts; a site served anywhere else answers `not-hosted`.
5753
+ * `after` asks for what arrived since an earlier answer's `cursor`, and `live`
5754
+ * tells Val Build someone is watching, so it gathers new requests every few
5755
+ * seconds instead of once a minute. See `docs/site-errors.md` in valbuild/home.
5756
+ */
5757
+
5758
+ const LineSchema = z.z.object({
5759
+ time: z.z.string(),
5760
+ level: z.z.enum(["error", "warn", "info", "debug"]),
5761
+ message: z.z.string(),
5762
+ stack: z.z.string().nullable()
5763
+ });
5764
+ const RequestSchema = z.z.object({
5765
+ id: z.z.string(),
5766
+ time: z.z.string(),
5767
+ method: z.z.string().nullable(),
5768
+ path: z.z.string().nullable(),
5769
+ status: z.z.number().nullable(),
5770
+ durationMs: z.z.number().nullable(),
5771
+ cpuMs: z.z.number().nullable(),
5772
+ outcome: z.z.enum(["ok", "exception", "cpu", "memory", "canceled"]),
5773
+ build: z.z.string().nullable(),
5774
+ proposal: z.z.string().nullable(),
5775
+ lines: z.z.array(LineSchema),
5776
+ errorFingerprint: z.z.string().nullable()
5777
+ });
5778
+ const SiteLogsAnswerSchema = z.z.discriminatedUnion("status", [z.z.object({
5779
+ status: z.z.literal("ok"),
5780
+ requests: z.z.array(RequestSchema),
5781
+ cursor: z.z.string(),
5782
+ histogram: z.z.object({
5783
+ start: z.z.string(),
5784
+ bucketMs: z.z.number(),
5785
+ buckets: z.z.array(z.z.object({
5786
+ error: z.z.number(),
5787
+ warn: z.z.number(),
5788
+ info: z.z.number()
5789
+ }))
5790
+ }),
5791
+ liveBuild: z.z.string().nullable(),
5792
+ retentionHours: z.z.number(),
5793
+ truncated: z.z.boolean()
5794
+ }), z.z.object({
5795
+ status: z.z.literal("not-hosted")
5796
+ })]);
5797
+
5798
+ /** A request's level: the worst thing about it, as Val Build filters by. */
5799
+
5800
+ function fetchSiteLogs({
5801
+ since,
5802
+ after,
5803
+ q,
5804
+ levels,
5805
+ limit,
5806
+ live,
5807
+ ...options
5808
+ }) {
5809
+ const query = {};
5810
+ if (since !== undefined) query["since"] = since.toISOString();
5811
+ if (after !== undefined) query["after"] = after;
5812
+ if (q !== undefined && q.trim() !== "") query["q"] = q.trim();
5813
+ if (levels !== undefined) query["levels"] = levels.join(",");
5814
+ if (limit !== undefined) query["limit"] = String(limit);
5815
+ if (live === true) query["live"] = "1";
5816
+ return readFromValBuild({
5817
+ ...options,
5818
+ resource: "logs",
5819
+ query,
5820
+ schema: SiteLogsAnswerSchema,
5821
+ invalid: "Val Build's answer was not the site's logs."
5822
+ });
5823
+ }
5824
+
5825
+ /** The worst thing about a request: how it ended, its status, its worst line. */
5826
+ function siteLogRequestLevel(request) {
5827
+ if (request.outcome !== "ok" && request.outcome !== "canceled" || request.status !== null && request.status >= 500 || request.lines.some(line => line.level === "error")) {
5828
+ return "error";
5829
+ }
5830
+ if (request.lines.some(line => line.level === "warn")) return "warn";
5831
+ return "info";
5832
+ }
5833
+
5608
5834
  const JsFileLookupMapping = [
5609
5835
  // NOTE: first one matching will be used
5610
5836
  [".cjs.d.ts", [".esm.js", ".mjs.js"]], [".cjs.js", [".esm.js", ".mjs.js"]], [".cjs", [".mjs"]], [".d.ts", [".js", ".esm.js", ".mjs.js"]]];
@@ -7210,6 +7436,8 @@ exports.extractImageMetadata = extractImageMetadata;
7210
7436
  exports.extractJsonValuesEntry = extractJsonValuesEntry;
7211
7437
  exports.extractVideoMetadata = extractVideoMetadata;
7212
7438
  exports.extractVideoMetadataFromFile = extractVideoMetadataFromFile;
7439
+ exports.fetchSiteErrors = fetchSiteErrors;
7440
+ exports.fetchSiteLogs = fetchSiteLogs;
7213
7441
  exports.filesOfVideo = filesOfVideo;
7214
7442
  exports.filesOfVideosetEntry = filesOfVideosetEntry;
7215
7443
  exports.findAndEvalValConfigFile = findAndEvalValConfigFile;
@@ -7240,6 +7468,7 @@ exports.planJsonValuesEntryExtraction = planJsonValuesEntryExtraction;
7240
7468
  exports.readCapturedReport = readCapturedReport;
7241
7469
  exports.replaySnapshot = replaySnapshot;
7242
7470
  exports.safeReadGit = safeReadGit;
7471
+ exports.siteLogRequestLevel = siteLogRequestLevel;
7243
7472
  exports.startValLogin = startValLogin;
7244
7473
  exports.validateMetadata = validateMetadata;
7245
7474
  exports.videosetEntryVideoSchema = videosetEntryVideoSchema;