@trucore/openclaw-atf 0.1.1 → 0.2.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.
@@ -0,0 +1,181 @@
1
+ /**
2
+ * tool_response.mjs — OpenClaw adapter envelope builder
3
+ *
4
+ * ADAPTER LAYER: Wraps universal ATF results into the OpenClaw-specific
5
+ * response envelope format (content array with text + json items).
6
+ *
7
+ * Architecture:
8
+ * Universal contract layer (contracts/) → platform-agnostic results
9
+ * This file (tool_response.mjs) → OpenClaw envelope wrapping
10
+ *
11
+ * All native OpenClaw tools use these helpers to produce consistent,
12
+ * machine-readable response shapes. The envelope is OpenClaw-specific;
13
+ * the payload inside is universal.
14
+ *
15
+ * Response contract (OpenClaw envelope):
16
+ * Success payload (inside content[].json):
17
+ * { status, result, _meta: { plugin_id, plugin_version, tool, ts, backend?, warnings? } }
18
+ * Error payload (inside content[].json):
19
+ * { status: "error", error, remediation?, _meta: { ... } }
20
+ *
21
+ * Exports:
22
+ * - nativeToolSuccess(toolName, summary, result, opts?)
23
+ * - nativeToolError(toolName, message, opts?)
24
+ * - attachBackendMeta(backendResolution) → backend meta object
25
+ * - RESPONSE_STATUS → status enum (re-exported from contracts)
26
+ *
27
+ * Uses ONLY built-in Node modules + sibling contracts/. No external deps.
28
+ */
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Imports from universal contract layer
32
+ // ---------------------------------------------------------------------------
33
+
34
+ import {
35
+ RESPONSE_STATUS as _RESPONSE_STATUS,
36
+ buildMeta,
37
+ } from "./contracts/index.mjs";
38
+
39
+ // Re-export for backward compatibility — existing consumers import from here.
40
+ export const RESPONSE_STATUS = _RESPONSE_STATUS;
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // Constants — OpenClaw-specific adapter identity
44
+ // ---------------------------------------------------------------------------
45
+
46
+ /**
47
+ * OpenClaw plugin identity — adapter-specific, maps to universal product_id.
48
+ * Kept in sync by version consistency tests.
49
+ */
50
+ const PLUGIN_ID = "trucore-atf";
51
+ const PLUGIN_VERSION = "0.2.0";
52
+
53
+ // ---------------------------------------------------------------------------
54
+ // Backend meta builder
55
+ // ---------------------------------------------------------------------------
56
+
57
+ /**
58
+ * Extract a compact backend metadata object from a BackendResolution.
59
+ *
60
+ * @param {object} backendResolution Output from resolveBackend().
61
+ * @returns {{ preferred: string, effective: string, fallback_occurred: boolean, fallback_reason: string|null }}
62
+ */
63
+ export function attachBackendMeta(backendResolution) {
64
+ if (!backendResolution || typeof backendResolution !== "object") {
65
+ return {
66
+ preferred: "unknown",
67
+ effective: "unknown",
68
+ fallback_occurred: false,
69
+ fallback_reason: null,
70
+ };
71
+ }
72
+ return {
73
+ preferred: backendResolution.preferred_backend ?? "unknown",
74
+ effective: backendResolution.effective_backend ?? "unknown",
75
+ fallback_occurred: backendResolution.fallback_occurred ?? false,
76
+ fallback_reason: backendResolution.fallback_reason ?? null,
77
+ };
78
+ }
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // Success response — OpenClaw adapter envelope
82
+ // ---------------------------------------------------------------------------
83
+
84
+ /**
85
+ * Build an OpenClaw-envelope success response for a native tool.
86
+ *
87
+ * ADAPTER FUNCTION: Wraps a universal ATF result into the OpenClaw
88
+ * content-array format. The payload inside content[].json is the
89
+ * universal result shape.
90
+ *
91
+ * Universal payload (inside json):
92
+ * { status, result, _meta: { plugin_id, plugin_version, tool, ts, backend?, warnings? } }
93
+ *
94
+ * OpenClaw envelope (this function's output):
95
+ * { content: [{ type: "text", text }, { type: "json", json: universalPayload }] }
96
+ *
97
+ * @param {string} toolName Tool name (e.g. "atf_health").
98
+ * @param {string} summary Human-readable summary line.
99
+ * @param {object} result Tool-specific result data.
100
+ * @param {object} [opts] Optional overrides.
101
+ * @param {string} [opts.status] One of RESPONSE_STATUS values (default: "ok").
102
+ * @param {object} [opts.backend] BackendResolution from resolveBackend().
103
+ * @param {string[]} [opts.warnings] Operator-facing warnings.
104
+ * @returns {{ content: Array<{type:string, text?:string, json?:unknown}> }}
105
+ */
106
+ export function nativeToolSuccess(toolName, summary, result, opts = {}) {
107
+ // Build universal metadata via the contract layer's builder
108
+ const universalMeta = buildMeta(toolName, opts);
109
+
110
+ // Augment with OpenClaw-specific adapter identity fields
111
+ const meta = {
112
+ ...universalMeta,
113
+ plugin_id: PLUGIN_ID,
114
+ plugin_version: PLUGIN_VERSION,
115
+ };
116
+
117
+ // Universal payload — same shape regardless of adapter
118
+ const payload = {
119
+ status: opts.status ?? RESPONSE_STATUS.OK,
120
+ result: result ?? {},
121
+ _meta: meta,
122
+ };
123
+
124
+ // OpenClaw-specific envelope wrapping
125
+ return {
126
+ content: [
127
+ { type: "text", text: summary },
128
+ { type: "json", json: payload },
129
+ ],
130
+ };
131
+ }
132
+
133
+ // ---------------------------------------------------------------------------
134
+ // Error response — OpenClaw adapter envelope
135
+ // ---------------------------------------------------------------------------
136
+
137
+ /**
138
+ * Build an OpenClaw-envelope error response for a native tool.
139
+ *
140
+ * ADAPTER FUNCTION: Wraps a universal ATF error result into the OpenClaw
141
+ * content-array format.
142
+ *
143
+ * @param {string} toolName Tool name.
144
+ * @param {string} message Human-readable error message.
145
+ * @param {object} [opts] Optional overrides.
146
+ * @param {object} [opts.backend] BackendResolution from resolveBackend().
147
+ * @param {string[]} [opts.warnings] Operator-facing warnings.
148
+ * @param {string[]} [opts.remediation] Fix instructions.
149
+ * @returns {{ content: Array<{type:string, text?:string, json?:unknown}>, isError: boolean }}
150
+ */
151
+ export function nativeToolError(toolName, message, opts = {}) {
152
+ // Build universal metadata via the contract layer's builder
153
+ const universalMeta = buildMeta(toolName, opts);
154
+
155
+ // Augment with OpenClaw-specific adapter identity fields
156
+ const meta = {
157
+ ...universalMeta,
158
+ plugin_id: PLUGIN_ID,
159
+ plugin_version: PLUGIN_VERSION,
160
+ };
161
+
162
+ // Universal error payload
163
+ const payload = {
164
+ status: RESPONSE_STATUS.ERROR,
165
+ error: message,
166
+ _meta: meta,
167
+ };
168
+
169
+ if (Array.isArray(opts.remediation) && opts.remediation.length > 0) {
170
+ payload.remediation = [...opts.remediation];
171
+ }
172
+
173
+ // OpenClaw-specific envelope wrapping
174
+ return {
175
+ content: [
176
+ { type: "text", text: message },
177
+ { type: "json", json: payload },
178
+ ],
179
+ isError: true,
180
+ };
181
+ }