@arbol-ai/maximus 0.0.0-stage → 7.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/config/oxfmt.d.mts +8 -0
  4. package/config/oxfmt.mjs +30 -0
  5. package/config/oxlint-plugin.mjs +41 -0
  6. package/config/oxlint.d.mts +8 -0
  7. package/config/oxlint.mjs +118 -0
  8. package/dist/analytics/analytics.d.ts +34 -0
  9. package/dist/analytics/analytics.js +62 -0
  10. package/dist/body.d.ts +2 -0
  11. package/dist/body.js +4 -0
  12. package/dist/chunks/body-BcMbreNd.js +106 -0
  13. package/dist/chunks/correlation-gaiEoMBr.js +45 -0
  14. package/dist/chunks/execution-qfZuMSIu.js +700 -0
  15. package/dist/chunks/json-Dwni3u4c.js +63 -0
  16. package/dist/chunks/record-D2H3cg8S.js +36 -0
  17. package/dist/chunks/schemas-q-51wBMQ.js +216 -0
  18. package/dist/chunks/src-Ph5KHCRP.js +1160 -0
  19. package/dist/chunks/telemetry-DvZgrzZn.js +220 -0
  20. package/dist/chunks/types-CGwAhnTE.d.ts +1630 -0
  21. package/dist/chunks/validation-DWxKk1Js.js +382 -0
  22. package/dist/client/client.d.ts +131 -0
  23. package/dist/client/client.js +325 -0
  24. package/dist/diagnostics.d.ts +41 -0
  25. package/dist/diagnostics.js +4 -0
  26. package/dist/errors.d.ts +2 -0
  27. package/dist/errors.js +128 -0
  28. package/dist/http-paths.d.ts +2 -0
  29. package/dist/http-paths.js +200 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.js +11 -0
  32. package/dist/internals.d.ts +2 -0
  33. package/dist/internals.js +5 -0
  34. package/dist/mcp/index.d.ts +12 -0
  35. package/dist/mcp/index.js +406 -0
  36. package/dist/postgres/index.d.ts +81 -0
  37. package/dist/postgres/index.js +140 -0
  38. package/dist/schemas.d.ts +2 -0
  39. package/dist/schemas.js +4 -0
  40. package/dist/standard-failures.d.ts +91 -0
  41. package/dist/standard-failures.js +75 -0
  42. package/dist/tooling/cli.js +110 -0
  43. package/dist/tooling/testing.d.ts +7 -0
  44. package/dist/tooling/testing.js +21 -0
  45. package/dist/webhooks.d.ts +69 -0
  46. package/dist/webhooks.js +162 -0
  47. package/dist/workers/runtime.d.ts +112 -0
  48. package/dist/workers/runtime.js +228 -0
  49. package/package.json +153 -4
  50. package/skills/maximus/SKILL.md +89 -0
  51. package/skills/maximus/references/adding-an-operation.md +179 -0
  52. package/skills/maximus/references/building-a-service.md +158 -0
  53. package/skills/maximus/references/errors-and-results.md +77 -0
  54. package/skills/maximus/references/migration-from-v2.md +62 -0
  55. package/skills/maximus/references/migration-to-v5.md +37 -0
  56. package/skills/maximus/references/publishing-and-verification.md +39 -0
  57. package/skills/maximus/references/testing.md +73 -0
  58. package/skills/maximus/references/transports.md +119 -0
  59. package/templates/service/.agents/skills/project/SKILL.md.template +10 -0
  60. package/templates/service/.github/workflows/ci.yml.template +21 -0
  61. package/templates/service/.gitignore.template +8 -0
  62. package/templates/service/CLAUDE.md.template +3 -0
  63. package/templates/service/README.md.template +45 -0
  64. package/templates/service/cloudflare.config.ts.template +30 -0
  65. package/templates/service/oxfmt.config.ts.template +1 -0
  66. package/templates/service/oxlint.config.ts.template +1 -0
  67. package/templates/service/package.json.template +31 -0
  68. package/templates/service/src/app.ts.template +30 -0
  69. package/templates/service/src/index.ts.template +13 -0
  70. package/templates/service/tests/contract.test.ts.template +12 -0
  71. package/templates/service/tsconfig.json.template +16 -0
  72. package/templates/service/vite.config.ts.template +7 -0
  73. package/templates/service/vitest.config.ts.template +10 -0
@@ -0,0 +1,220 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+
3
+ //#region src/internal/diagnostics.ts
4
+ /**
5
+ * Safe error descriptions for diagnostics: an error's known names, its causes' names, its issue count and
6
+ * its stack locations, never a message.
7
+ */
8
+ /** Built-in identities only; each consumer supplies its domain error names. */
9
+ const BUILTIN_ERROR_NAMES = [
10
+ "AbortError",
11
+ "AggregateError",
12
+ "DOMException",
13
+ "Error",
14
+ "EvalError",
15
+ "RangeError",
16
+ "ReferenceError",
17
+ "SyntaxError",
18
+ "TimeoutError",
19
+ "TypeError",
20
+ "URIError"
21
+ ];
22
+ /** Read a diagnostic property without letting a getter or Proxy break recovery. */
23
+ function errorProperty(value, key) {
24
+ try {
25
+ return typeof value === "object" && value !== null && key in value ? Reflect.get(value, key) : void 0;
26
+ } catch {
27
+ return;
28
+ }
29
+ }
30
+ /**
31
+ * Create a pure projection for SDK events and owner-written log lines. Names are
32
+ * developer constants, never values taken from an upstream response. Messages,
33
+ * record keys, URLs and query parameters are never returned. No logging occurs.
34
+ */
35
+ function createDiagnostics(errorNames = []) {
36
+ const names = /* @__PURE__ */ new Set([...BUILTIN_ERROR_NAMES, ...errorNames]);
37
+ /** The error's name when it is a known one, else `UnknownError`. */
38
+ function safeErrorName(error) {
39
+ const name = errorProperty(error, "name");
40
+ return typeof name === "string" && names.has(name) ? name : "UnknownError";
41
+ }
42
+ /**
43
+ * The error's safe description: names, up to three cause names, its issue count and up to eight stack
44
+ * locations, never a message.
45
+ */
46
+ function describeError(error) {
47
+ if (!(error instanceof Error)) return { name: "NonError" };
48
+ const result = { name: safeErrorName(error) };
49
+ const seen = /* @__PURE__ */ new Set([error]);
50
+ let current = errorProperty(error, "cause");
51
+ const causes = [];
52
+ while (current && !seen.has(current) && causes.length < 3) {
53
+ seen.add(current);
54
+ causes.push(safeErrorName(current));
55
+ current = errorProperty(current, "cause");
56
+ }
57
+ if (causes.length) result.causes = causes;
58
+ const issues = errorProperty(error, "issues") ?? errorProperty(errorProperty(error, "cause"), "issues");
59
+ if (Array.isArray(issues)) result.issueCount = issues.length;
60
+ const stack = errorProperty(error, "stack");
61
+ const name = errorProperty(error, "name");
62
+ const message = errorProperty(error, "message");
63
+ let heading;
64
+ if (typeof name === "string") heading = typeof message === "string" && message ? `${name}: ${message}` : name;
65
+ if (typeof heading === "string" && typeof stack === "string" && stack.startsWith(`${heading}\n`)) {
66
+ const locations = [];
67
+ for (const frame of stack.slice(heading.length + 1, heading.length + 4001).split("\n")) {
68
+ const match = frame.startsWith(" at ") ? /:(\d+):(\d+)\)?$/.exec(frame) : null;
69
+ if (!match) continue;
70
+ const line = Number(match[1]);
71
+ const column = Number(match[2]);
72
+ if (line > 0 && line <= 1e7 && column > 0 && column <= 1e7) locations.push({
73
+ line,
74
+ column
75
+ });
76
+ if (locations.length === 8) break;
77
+ }
78
+ if (locations.length) result.locations = locations;
79
+ }
80
+ return result;
81
+ }
82
+ return {
83
+ safeErrorName,
84
+ describeError
85
+ };
86
+ }
87
+
88
+ //#endregion
89
+ //#region src/telemetry.ts
90
+ /**
91
+ * Telemetry: invocation-local diagnostics kept in AsyncLocalStorage, safe native log events, and observers
92
+ * that receive execution and admission observations as background work.
93
+ */
94
+ /** Microseconds since `started`, a `performance.now()` reading. */
95
+ function elapsedMicros(started) {
96
+ return Math.max(0, Math.round((performance.now() - started) * 1e3));
97
+ }
98
+ /**
99
+ * Hands one event to one observer as background work: a failed delivery is logged with its request id
100
+ * and never reaches the response or another observer.
101
+ */
102
+ function deliver(background, requestId, send) {
103
+ background.waitUntil(Promise.resolve().then(send).catch(() => {
104
+ console.error(JSON.stringify({
105
+ event: "maximus.observer.delivery_failed",
106
+ requestId
107
+ }));
108
+ }));
109
+ }
110
+ /**
111
+ * The active trace id the configuration reads, when it is a valid non-zero W3C trace id; never a generated
112
+ * one.
113
+ */
114
+ function activeTraceId(config) {
115
+ try {
116
+ const id = config?.traceId?.();
117
+ return id && /^[0-9a-f]{32}$/.test(id) && !/^0+$/.test(id) ? id : null;
118
+ } catch {
119
+ return null;
120
+ }
121
+ }
122
+ /** Logs a refused admission (`maximus.admission.rejected`) and hands it to the observers. */
123
+ function recordAdmission(manifest, env, background, admission) {
124
+ if (!background || !telemetryEnabled(manifest.telemetry, env)) return;
125
+ const event = {
126
+ service: manifest.name,
127
+ operation: admission.operation,
128
+ principal: admission.principal,
129
+ requestId: admission.requestId,
130
+ traceId: activeTraceId(manifest.telemetry),
131
+ surface: admission.surface,
132
+ occurredAt: Date.now(),
133
+ durationMicros: elapsedMicros(admission.started),
134
+ errorCode: admission.errorCode
135
+ };
136
+ try {
137
+ console.warn(JSON.stringify({
138
+ event: "maximus.admission.rejected",
139
+ service: event.service,
140
+ operation: event.operation,
141
+ requestId: event.requestId,
142
+ traceId: event.traceId,
143
+ surface: event.surface,
144
+ occurredAt: event.occurredAt,
145
+ durationMicros: event.durationMicros,
146
+ errorCode: event.errorCode
147
+ }));
148
+ } catch {}
149
+ for (const observer of manifest.telemetry?.observers ?? []) if (observer.admission) deliver(background, event.requestId, () => observer.admission?.(event, env));
150
+ }
151
+ /**
152
+ * Whether telemetry is on for this env: it is unless `enabled` is false, or a function that returns false or
153
+ * has no env to read.
154
+ */
155
+ function telemetryEnabled(config, env) {
156
+ return typeof config?.enabled === "function" ? env !== void 0 && config.enabled(env) : config?.enabled !== false;
157
+ }
158
+ /** Describes errors by their built-in names only. */
159
+ const diagnostics = createDiagnostics();
160
+ let storage;
161
+ /** The invocation-local store, created on first use. */
162
+ function current() {
163
+ storage ??= new AsyncLocalStorage();
164
+ return storage;
165
+ }
166
+ /**
167
+ * Runs `callback` and everything it calls, synchronously or not, with diagnostics on or off and these
168
+ * observers.
169
+ */
170
+ function withDiagnostics(enabled, callback, observers = []) {
171
+ return current().run({
172
+ enabled,
173
+ observers
174
+ }, callback);
175
+ }
176
+ /** Runs `work` with the diagnostics an application's telemetry turns on for this `env`. */
177
+ function withTelemetry(config, env, work) {
178
+ return withDiagnostics(telemetryEnabled(config, env), work, config?.observers);
179
+ }
180
+ /** Whether the current call runs with diagnostics on; they are on outside any `withDiagnostics`. */
181
+ function diagnosticsEnabled() {
182
+ return storage?.getStore()?.enabled !== false;
183
+ }
184
+ /**
185
+ * Logs an unexpected error by its safe description only (`maximus.unexpected`: error and cause names, issue
186
+ * count, stack locations; never a message) and tells the observers.
187
+ */
188
+ function reportUnexpected(error, surface = "application", requestId) {
189
+ if (!diagnosticsEnabled()) return;
190
+ try {
191
+ console.error(JSON.stringify({
192
+ event: "maximus.unexpected",
193
+ surface,
194
+ requestId,
195
+ ...diagnostics.describeError(error)
196
+ }));
197
+ } catch {}
198
+ for (const observer of storage?.getStore()?.observers ?? []) try {
199
+ observer.error?.(error, surface, requestId);
200
+ } catch {}
201
+ }
202
+ /** Hands the observation of one operation call to the observers, as background work. */
203
+ function recordExecution(manifest, env, background, invocation) {
204
+ if (!background || !telemetryEnabled(manifest.telemetry, env)) return;
205
+ const event = {
206
+ service: manifest.name,
207
+ operation: invocation.operation,
208
+ principal: invocation.principal,
209
+ surface: invocation.surface,
210
+ occurredAt: Date.now(),
211
+ durationMicros: elapsedMicros(invocation.started),
212
+ outcome: invocation.result.ok ? "success" : "failure",
213
+ errorCode: invocation.result.ok ? null : invocation.result.error.code,
214
+ traceId: activeTraceId(manifest.telemetry)
215
+ };
216
+ for (const observer of manifest.telemetry?.observers ?? []) if (observer.execution) deliver(background, event.principal.requestId, () => observer.execution?.(event, env));
217
+ }
218
+
219
+ //#endregion
220
+ export { telemetryEnabled as a, createDiagnostics as c, reportUnexpected as i, recordAdmission as n, withDiagnostics as o, recordExecution as r, withTelemetry as s, diagnosticsEnabled as t };