@dotcms/ai 26.10.20-1 → 26.10.21-1

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 (72) hide show
  1. package/README.md +203 -29
  2. package/adapter.cjs.default.js +1 -0
  3. package/adapter.cjs.js +14 -0
  4. package/adapter.cjs.mjs +2 -0
  5. package/adapter.cjs2.js +0 -0
  6. package/adapter.d.ts +1 -0
  7. package/adapter.esm.js +2 -0
  8. package/adapter.esm2.js +0 -0
  9. package/errors.cjs.js +192 -0
  10. package/errors.esm.js +180 -0
  11. package/format-result.cjs.js +833 -0
  12. package/format-result.esm.js +825 -0
  13. package/package.json +25 -20
  14. package/runtime.cjs.js +0 -0
  15. package/runtime.esm.js +0 -0
  16. package/sandbox.cjs.default.js +1 -0
  17. package/sandbox.cjs.js +69 -0
  18. package/sandbox.cjs.mjs +2 -0
  19. package/sandbox.d.ts +1 -0
  20. package/sandbox.esm.js +50 -0
  21. package/spec.cjs.default.js +1 -0
  22. package/spec.cjs.js +3 -23912
  23. package/spec.cjs.mjs +2 -0
  24. package/spec.cjs2.js +23916 -0
  25. package/spec.d.ts +1 -0
  26. package/spec.esm.js +1 -23914
  27. package/spec.esm2.js +23914 -0
  28. package/src/adapter/context.d.ts +11 -0
  29. package/src/adapter/request-core.d.ts +30 -0
  30. package/src/adapter.d.ts +1 -0
  31. package/src/runtime.d.ts +5 -4
  32. package/src/sandbox/errors.d.ts +23 -1
  33. package/src/sandbox/index.d.ts +1 -1
  34. package/src/sandbox.d.ts +1 -0
  35. package/src/spec.d.ts +1 -0
  36. package/src/tools/definitions/download-assets.d.ts +27 -0
  37. package/src/tools/definitions/execute.d.ts +15 -0
  38. package/src/tools/definitions/page-create.d.ts +23 -0
  39. package/src/tools/definitions/page-place-content.d.ts +32 -0
  40. package/src/tools/definitions/page-verify.d.ts +20 -0
  41. package/src/tools/definitions/search.d.ts +13 -0
  42. package/src/tools/definitions/upload-assets.d.ts +23 -0
  43. package/src/tools/index.d.ts +40 -0
  44. package/src/tools/operations/download-assets.d.ts +55 -0
  45. package/src/tools/operations/page-create.d.ts +81 -0
  46. package/src/tools/operations/page-place-content.d.ts +102 -0
  47. package/src/tools/operations/page-verify.d.ts +164 -0
  48. package/src/tools/operations/shared/asset-common.d.ts +28 -0
  49. package/src/tools/operations/shared/glob.d.ts +22 -0
  50. package/src/tools/operations/shared/local-root.d.ts +11 -0
  51. package/src/tools/operations/shared/page-common.d.ts +45 -0
  52. package/src/tools/operations/shared/page-path.d.ts +49 -0
  53. package/src/tools/operations/shared/resolve.d.ts +43 -0
  54. package/src/tools/operations/upload-assets.d.ts +64 -0
  55. package/src/tools/toolkit/connection.d.ts +56 -0
  56. package/src/tools/toolkit/create-tool.d.ts +66 -0
  57. package/src/tools/toolkit/endpoints.d.ts +52 -0
  58. package/src/tools/toolkit/lenient-boolean.d.ts +14 -0
  59. package/src/tools/toolkit/results.d.ts +32 -0
  60. package/src/tools/toolkit/tool-runtime.d.ts +108 -0
  61. package/src/tools/toolkit/types.d.ts +120 -0
  62. package/src/tools.d.ts +1 -0
  63. package/tools.cjs.default.js +1 -0
  64. package/tools.cjs.js +3850 -0
  65. package/tools.cjs.mjs +2 -0
  66. package/tools.d.ts +1 -0
  67. package/tools.esm.js +3817 -0
  68. package/index.cjs.default.js +0 -1
  69. package/index.cjs.js +0 -7
  70. package/index.cjs.mjs +0 -2
  71. package/index.d.ts +0 -1
  72. package/index.esm.js +0 -1
package/errors.esm.js ADDED
@@ -0,0 +1,180 @@
1
+ /**
2
+ * The single typed error hierarchy for `@dotcms/ai`.
3
+ *
4
+ * Both verbs of the runtime — `request()` (direct) and `run()` (sandboxed) — surface
5
+ * the *same* error types, because both route through one shared request core. The
6
+ * model-facing string an MCP tool builds is *formatting on top of* these errors, not a
7
+ * separate error model.
8
+ *
9
+ * Every error carries a stable, machine-readable `code` and a `toJSON()` so it survives
10
+ * the worker→host serialization boundary (a thrown error becomes `{ name, message, stack }`
11
+ * by default; these types preserve `code` and the structured detail too).
12
+ */
13
+ class DotCMSError extends Error {
14
+ constructor(message, options) {
15
+ super(message);
16
+ // A fallback only: every subclass declares its own `name`. The class name cannot be
17
+ // trusted — a bundler minifies it, and the built MCP server showed the model
18
+ // "b: …" for a PolicyError. A new subclass must declare `override readonly name`.
19
+ this.name = new.target.name;
20
+ if (options?.cause !== undefined) {
21
+ // Standard ES2022 `cause`, but assigned defensively for older lib targets.
22
+ this.cause = options.cause;
23
+ }
24
+ // Restore the prototype chain so `instanceof` works after transpilation to ES5/ES2017.
25
+ Object.setPrototypeOf(this, new.target.prototype);
26
+ }
27
+ /** Type-specific structured detail; overridden by subclasses that carry data. */
28
+ detail() {
29
+ return undefined;
30
+ }
31
+ toJSON() {
32
+ return {
33
+ name: this.name,
34
+ code: this.code,
35
+ message: this.message,
36
+ stack: this.stack,
37
+ detail: this.detail()
38
+ };
39
+ }
40
+ }
41
+ /** Adapter input failed validation, or request options were malformed. */
42
+ class ValidationError extends DotCMSError {
43
+ constructor(message, issues) {
44
+ super(message);
45
+ this.name = 'ValidationError';
46
+ this.code = 'VALIDATION';
47
+ this.issues = issues;
48
+ }
49
+ detail() {
50
+ return this.issues ? { issues: this.issues } : undefined;
51
+ }
52
+ }
53
+ /** The allow-list / policy rejected the call before it reached the network. */
54
+ class PolicyError extends DotCMSError {
55
+ constructor(message, method, path) {
56
+ super(message);
57
+ this.name = 'PolicyError';
58
+ this.code = 'POLICY';
59
+ this.method = method;
60
+ this.path = path;
61
+ }
62
+ detail() {
63
+ return { method: this.method, path: this.path };
64
+ }
65
+ }
66
+ /** dotCMS responded with a non-2xx status. Carries the status and the (text) body. */
67
+ class HttpError extends DotCMSError {
68
+ constructor(status, statusText, body) {
69
+ super(`HTTP ${status} ${statusText}: ${body}`);
70
+ this.name = 'HttpError';
71
+ this.code = 'HTTP';
72
+ this.status = status;
73
+ this.statusText = statusText;
74
+ this.body = body;
75
+ }
76
+ detail() {
77
+ return { status: this.status, statusText: this.statusText, body: this.body };
78
+ }
79
+ }
80
+ /**
81
+ * The request never got a response — the connection was refused or reset, the host did not
82
+ * resolve, the socket timed out. Distinct from `HttpError` (the instance answered, with an
83
+ * error) because the two call for opposite reactions: this one is usually worth retrying.
84
+ */
85
+ class NetworkError extends DotCMSError {
86
+ constructor(method, path, cause) {
87
+ const reason = networkReason(cause);
88
+ super(`Request ${method} ${path} could not reach the dotCMS instance` +
89
+ (reason ? ` (${reason})` : '') +
90
+ '. The instance may be down, unreachable or restarting; this is worth retrying.', { cause });
91
+ this.name = 'NetworkError';
92
+ this.code = 'NETWORK';
93
+ this.method = method;
94
+ this.path = path;
95
+ this.reason = reason;
96
+ }
97
+ detail() {
98
+ return { method: this.method, path: this.path, reason: this.reason };
99
+ }
100
+ }
101
+ /** The most specific reason a failed `fetch` carries — undici nests it under `cause`. */
102
+ function networkReason(error) {
103
+ const nested = error?.cause;
104
+ const code = nested?.code ?? error?.code;
105
+ if (typeof code === 'string' && code) {
106
+ return code;
107
+ }
108
+ const message = nested?.message ?? error?.message;
109
+ return typeof message === 'string' && message ? message : undefined;
110
+ }
111
+ /** A timeout elapsed (sandbox wall-clock, per-adapter-call, or context load). */
112
+ class TimeoutError extends DotCMSError {
113
+ constructor(message, timeoutMs) {
114
+ super(message);
115
+ this.name = 'TimeoutError';
116
+ this.code = 'TIMEOUT';
117
+ this.timeoutMs = timeoutMs;
118
+ }
119
+ detail() {
120
+ return this.timeoutMs === undefined ? undefined : { timeoutMs: this.timeoutMs };
121
+ }
122
+ }
123
+ /** The call was aborted in-flight (typically because the sandbox timed out). */
124
+ class AbortError extends DotCMSError {
125
+ constructor() {
126
+ super(...arguments);
127
+ this.name = 'AbortError';
128
+ this.code = 'ABORT';
129
+ }
130
+ }
131
+ /** Model-authored code threw while executing inside the sandbox. */
132
+ class SandboxError extends DotCMSError {
133
+ constructor(message, originalName, stack) {
134
+ super(message);
135
+ this.name = 'SandboxError';
136
+ this.code = 'SANDBOX';
137
+ this.originalName = originalName;
138
+ if (stack)
139
+ this.stack = stack;
140
+ }
141
+ detail() {
142
+ return this.originalName ? { originalName: this.originalName } : undefined;
143
+ }
144
+ }
145
+ /** Catch-all for runtime-internal failures that don't fit a more specific type. */
146
+ class RuntimeError extends DotCMSError {
147
+ constructor() {
148
+ super(...arguments);
149
+ this.name = 'RuntimeError';
150
+ this.code = 'RUNTIME';
151
+ }
152
+ }
153
+ /** Type guard for any error in the hierarchy. */
154
+ function isDotCMSError(value) {
155
+ return value instanceof DotCMSError;
156
+ }
157
+ /**
158
+ * Normalize any thrown value into a serializable error shape. Used at the worker→host
159
+ * boundary and when formatting a result, so callers always get a consistent structure
160
+ * regardless of what was thrown.
161
+ */
162
+ function serializeError(value) {
163
+ if (isDotCMSError(value)) {
164
+ return value.toJSON();
165
+ }
166
+ if (value instanceof Error) {
167
+ // Preserve a code already set on a plain Error (e.g. Node system errors, or an
168
+ // adapter that tags its own); fall back to RUNTIME.
169
+ const existingCode = value.code;
170
+ return {
171
+ name: value.name,
172
+ code: (typeof existingCode === 'string' ? existingCode : 'RUNTIME'),
173
+ message: value.message,
174
+ stack: value.stack
175
+ };
176
+ }
177
+ return { name: 'Error', code: 'RUNTIME', message: String(value) };
178
+ }
179
+
180
+ export { AbortError as A, DotCMSError as D, HttpError as H, NetworkError as N, PolicyError as P, RuntimeError as R, SandboxError as S, TimeoutError as T, ValidationError as V, isDotCMSError as i, serializeError as s };