@noetic-tools/core 5.22.0-next.3 → 5.22.0-next.5

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.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * A readable message for anything thrown. JSON-RPC peers (an ACP agent) reject
3
+ * with `{ code, message, data }` — as a plain object or as an `Error` carrying
4
+ * `code`/`data` — and the useful part ("Authentication required") is often in
5
+ * `data`, so it is appended rather than lost as `[object Object]` or a bare
6
+ * "Internal error".
7
+ */
8
+ export function errorMessage(cause) {
9
+ if (typeof cause !== 'object' || cause === null) {
10
+ return String(cause);
11
+ }
12
+ if (!isRpcError(cause)) {
13
+ const message = Reflect.get(cause, 'message');
14
+ if (typeof message === 'string') {
15
+ return message;
16
+ }
17
+ return safeJson(cause);
18
+ }
19
+ const rawMessage = Reflect.get(cause, 'message');
20
+ const message = typeof rawMessage === 'string' && rawMessage.length > 0 ? rawMessage : undefined;
21
+ const code = Reflect.get(cause, 'code');
22
+ const detail = detailOf(Reflect.get(cause, 'data'));
23
+ const parts = [
24
+ message ?? (detail === undefined ? 'JSON-RPC error' : undefined),
25
+ detail !== undefined && detail !== message ? detail : undefined,
26
+ ].filter((part) => part !== undefined);
27
+ const text = parts.join(': ');
28
+ return typeof code === 'number' ? `${text} (${code})` : text;
29
+ }
30
+ /**
31
+ * The error a step fails with for a JSON-RPC rejection: the readable message,
32
+ * keeping the rejection's `code` and `data` (and the original as `cause`) for
33
+ * callers that branch on them, such as an auth-required code.
34
+ */
35
+ export class RpcFailure extends Error {
36
+ code;
37
+ data;
38
+ constructor(cause) {
39
+ super(errorMessage(cause), {
40
+ cause,
41
+ });
42
+ this.name = 'RpcFailure';
43
+ const code = typeof cause === 'object' && cause !== null ? Reflect.get(cause, 'code') : undefined;
44
+ this.code = typeof code === 'number' ? code : undefined;
45
+ this.data =
46
+ typeof cause === 'object' && cause !== null ? Reflect.get(cause, 'data') : undefined;
47
+ }
48
+ }
49
+ /** The detail a JSON-RPC error's `data` carries: its message, its details, or the value itself. */
50
+ function detailOf(data) {
51
+ if (data === undefined || data === null) {
52
+ return undefined;
53
+ }
54
+ if (typeof data === 'string') {
55
+ return data;
56
+ }
57
+ if (typeof data !== 'object') {
58
+ return String(data);
59
+ }
60
+ for (const key of [
61
+ 'message',
62
+ 'details',
63
+ ]) {
64
+ const value = Reflect.get(data, key);
65
+ if (typeof value === 'string' && value.length > 0) {
66
+ return value;
67
+ }
68
+ }
69
+ return safeJson(data);
70
+ }
71
+ const MAX_DETAIL_CHARS = 500;
72
+ function safeJson(value) {
73
+ try {
74
+ const text = JSON.stringify(value) ?? String(value);
75
+ return text.length > MAX_DETAIL_CHARS ? `${text.slice(0, MAX_DETAIL_CHARS)}…` : text;
76
+ }
77
+ catch {
78
+ return String(value);
79
+ }
80
+ }
81
+ /**
82
+ * Whether a thrown value is a JSON-RPC error: it carries a numeric `code` or a
83
+ * `data` payload. A `DOMException` (an AbortError, a TimeoutError) has a
84
+ * numeric legacy `code` too, and is not one.
85
+ */
86
+ export function isRpcError(cause) {
87
+ if (typeof cause !== 'object' || cause === null) {
88
+ return false;
89
+ }
90
+ if (typeof DOMException !== 'undefined' && cause instanceof DOMException) {
91
+ return false;
92
+ }
93
+ return typeof Reflect.get(cause, 'code') === 'number' || Reflect.get(cause, 'data') !== undefined;
94
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.22.0-next.3",
3
+ "version": "5.22.0-next.5",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "dependencies": {
71
71
  "@noetic-tools/context": "^2.0.0-next.1",
72
- "@noetic-tools/types": "^3.19.0-next.2",
72
+ "@noetic-tools/types": "^3.19.0-next.4",
73
73
  "@openrouter/agent": "^0.11.0",
74
74
  "@openrouter/sdk": "1.4.25",
75
75
  "jsonata": "2.2.2",