@wakeshift/openclaw 0.0.0-stage → 1.4.24

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.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,56 @@
1
- # Temporary Holding Version
1
+ # Wakeshift for OpenClaw
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Wakeshift's plugin for OpenClaw (2026.10.4 and later). It wakes an OpenClaw session when an app the session watches through [Wakeshift](https://wakeshift.ai) sends something, even while the session is idle, as long as the gateway runs. Messages arrive as outside information, never as the user.
4
+
5
+ ## Install
6
+
7
+ Install it where the OpenClaw gateway runs (in its container, if it's in one). The agent offers this when an app's watch result asks, and runs it after your yes:
8
+
9
+ ```sh
10
+ openclaw plugins install npm:@wakeshift/openclaw --force --accept-capabilities && openclaw plugins enable wakeshift --accept-capabilities
11
+ ```
12
+
13
+ (The agent's command names the version, `npm:@wakeshift/openclaw@<version>`.)
14
+
15
+ `--force` is what OpenClaw asks for to install a plugin from npm rather than ClawHub, and `--accept-capabilities` accepts the plugin's tool-result filter (it takes Wakeshift's beacon out of what the model sees) where nothing can prompt. There's nothing to approve afterwards and no restart: the plugin starts with the next turn.
16
+
17
+ The plugin trusts Wakeshift's own service, `https://app.wakeshift.ai`. For another Wakeshift, set it first, before the install:
18
+
19
+ ```sh
20
+ openclaw config set plugins.entries.wakeshift.config.issuer '"https://your-wakeshift.example"' --strict-json
21
+ ```
22
+
23
+ The package is published from [github.com/wakeshift/connectors](https://github.com/wakeshift/connectors) by GitHub Actions with npm provenance, so npm can show which commit and workflow built it.
24
+
25
+ To update, run the install command again (it also replaces a plugin installed by Wakeshift's earlier `openclaw.sh` installer).
26
+
27
+ ## Remove
28
+
29
+ ```sh
30
+ openclaw wakeshift reset && openclaw plugins uninstall wakeshift --force
31
+ ```
32
+
33
+ `openclaw wakeshift reset` undoes what the plugin turned on (below): it puts OpenClaw's hooks settings back as the plugin found them, apart from what you changed since, removes `allowNetworkHooks`, and deletes `~/.openclaw/wakeshift`. OpenClaw's uninstall then removes the plugin and its settings. OpenClaw has no uninstall hook, so `openclaw plugins uninstall` on its own leaves the hooks settings as the plugin left them.
34
+
35
+ ## How it wakes a session
36
+
37
+ The plugin starts a turn with OpenClaw's own hooks endpoint, `POST <hooks.path>/agent` on the gateway's listener, in the session's own context (`sessionMode: "persistent"`). OpenClaw wraps the message as outside content. The turn's reply goes to the channel the session last spoke on, or stays in its transcript when it has none. A message is acknowledged to Wakeshift once the turn it started has finished.
38
+
39
+ When it first connects a session, the plugin turns on OpenClaw's hooks:
40
+
41
+ - `hooks.enabled`, with a random token of its own in `hooks.token`;
42
+ - `hooks.allowRequestSessionKey`, with `hooks.defaultSessionKey: "hook:wakeshift"`;
43
+ - `hooks.allowedSessionKeyPrefixes` and `hooks.allowedAgentIds`, allowing only the sessions and agents that connect through Wakeshift, each removed again when its session's subscription ends.
44
+
45
+ If OpenClaw's hooks are on already, the plugin only adds the session and its agent, and only if your settings already let a caller name the session from a restricted prefix list; otherwise it changes nothing and tells the agent what it would need. Everything it changes is recorded in `~/.openclaw/wakeshift/hooks.json`, which `openclaw wakeshift reset` uses to put your settings back.
46
+
47
+ The hooks endpoint shares the gateway's listener. On a gateway that listens beyond this computer (its bind isn't `loopback`), the endpoint is reachable there too, guarded by the token, so the plugin turns nothing on there until you agree: the agent asks first, and after your yes its command starts with `openclaw config set plugins.entries.wakeshift.config.allowNetworkHooks true --strict-json`. Block `/hooks/` from other machines with a firewall or proxy rule.
48
+
49
+ ## Settings
50
+
51
+ - `plugins.entries.wakeshift.config.issuer`: the Wakeshift whose beacons the plugin trusts (unset: `https://app.wakeshift.ai`).
52
+ - `plugins.entries.wakeshift.config.allowNetworkHooks`: you agreed to the hooks endpoint on a gateway that listens beyond this computer.
53
+
54
+ ## Development
55
+
56
+ `npm test` runs the tests; `npm run pack` builds `wakeshift-openclaw.tgz`, which Wakeshift's earlier `/install/openclaw.sh` installs. Each release of github.com/wakeshift/connectors carries the npm package at the release's version, and its `npm.yml` publishes it with provenance. The design and what was verified are in `docs/harnesses.md` (OpenClaw) in Wakeshift's main repository.
package/dist/beacon.js ADDED
@@ -0,0 +1,287 @@
1
+ // The connector beacon (internal/beacon): a compact JWS (EdDSA) a Wakeshift
2
+ // tool result carries in _meta["wakeshift/connector"], holding a one-time
3
+ // ticket that's redeemed for the conversation's own stream link. The plugin
4
+ // trusts a beacon only if it's signed by the Wakeshift pinned at install
5
+ // (keys from <issuer>/oauth/jwks) and is for this conversation
6
+ // (VerifyBound), and it never trusts anything it can't verify.
7
+ import { createPublicKey, verify as edVerify } from "node:crypto";
8
+ export const META_KEY = "wakeshift/connector";
9
+ export const TYPE = "wakeshift-connector+jwt";
10
+ export const AUDIENCE = "wakeshift-connector";
11
+ export class BeaconError extends Error {
12
+ }
13
+ export const UNTRUSTED = "beacon: not signed by a trusted Wakeshift";
14
+ export const INVALID = "beacon: invalid";
15
+ const b64 = (s) => Buffer.from(s, "base64url");
16
+ const TOKEN = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
17
+ /** A beacon's issuer, unchecked (to find its keys). */
18
+ export function issuerOf(token) {
19
+ if (!TOKEN.test(token))
20
+ return "";
21
+ try {
22
+ const c = JSON.parse(b64(token.split(".")[1]).toString("utf8"));
23
+ return typeof c?.iss === "string" ? c.iss : "";
24
+ }
25
+ catch {
26
+ return "";
27
+ }
28
+ }
29
+ /** An Ed25519 public key from a JWK ({kty: OKP, crv: Ed25519, x}). */
30
+ export function jwkKey(k) {
31
+ if (k.kty !== "OKP" || k.crv !== "Ed25519" || typeof k.x !== "string" || b64(k.x).length !== 32)
32
+ return undefined;
33
+ try {
34
+ return createPublicKey({ key: { kty: "OKP", crv: "Ed25519", x: k.x }, format: "jwk" });
35
+ }
36
+ catch {
37
+ return undefined;
38
+ }
39
+ }
40
+ /** beacon.VerifyBound: signed by a trusted issuer, for this binding, unexpired, redeemable. */
41
+ export function verifyBound(token, b, issuers, keyFor, now = Date.now()) {
42
+ if (!b.conversation)
43
+ throw new BeaconError(INVALID);
44
+ if (!issuers.includes(issuerOf(token)))
45
+ throw new BeaconError(UNTRUSTED);
46
+ const [h, c, s] = token.split(".");
47
+ let header;
48
+ try {
49
+ header = JSON.parse(b64(h).toString("utf8"));
50
+ }
51
+ catch {
52
+ throw new BeaconError(UNTRUSTED);
53
+ }
54
+ if (header.alg !== "EdDSA" || header.typ !== TYPE)
55
+ throw new BeaconError(UNTRUSTED);
56
+ const key = keyFor(header.kid ?? "");
57
+ if (!key || !edVerify(null, Buffer.from(h + "." + c), key, b64(s)))
58
+ throw new BeaconError(UNTRUSTED);
59
+ let claims;
60
+ try {
61
+ claims = JSON.parse(b64(c).toString("utf8"));
62
+ }
63
+ catch {
64
+ throw new BeaconError(UNTRUSTED);
65
+ }
66
+ if (!issuers.includes(claims.iss) ||
67
+ claims.aud !== AUDIENCE ||
68
+ Math.floor(now / 1000) > claims.exp ||
69
+ !claims.ticket ||
70
+ typeof claims.app !== "string" ||
71
+ !claims.app ||
72
+ !redeemable(claims.redeem, claims.iss))
73
+ throw new BeaconError(INVALID);
74
+ if (claims.thread) {
75
+ if (claims.thread !== b.conversation)
76
+ throw new BeaconError(INVALID);
77
+ }
78
+ else if (claims.call) {
79
+ if (claims.call !== b.call)
80
+ throw new BeaconError(INVALID);
81
+ }
82
+ else if (!b.unbound) {
83
+ throw new BeaconError(INVALID);
84
+ }
85
+ return claims;
86
+ }
87
+ /** On the issuer's own origin (the ticket and the earlier link go there), over https, or plain http on loopback (development). */
88
+ export function redeemable(raw, issuer) {
89
+ if (typeof raw !== "string" || typeof issuer !== "string")
90
+ return false;
91
+ let u, i;
92
+ try {
93
+ u = new URL(raw);
94
+ i = new URL(issuer);
95
+ }
96
+ catch {
97
+ return false;
98
+ }
99
+ if (!u.host || u.username || u.password)
100
+ return false;
101
+ if (u.protocol !== i.protocol || u.host.toLowerCase() !== i.host.toLowerCase())
102
+ return false;
103
+ if (u.protocol === "https:")
104
+ return true;
105
+ return u.protocol === "http:" && isLoopback(u.hostname);
106
+ }
107
+ export function isLoopback(host) {
108
+ host = host.replace(/^\[|\]$/g, "");
109
+ return host === "localhost" || /^127\.\d+\.\d+\.\d+$/.test(host) || host === "::1";
110
+ }
111
+ /**
112
+ * A beacon in an MCP tool result: its _meta (at the top or a few levels
113
+ * down: harnesses wrap results differently), or else a beacon.Text line.
114
+ */
115
+ export function beaconIn(result) {
116
+ let v = result;
117
+ if (typeof v === "string") {
118
+ try {
119
+ v = JSON.parse(v);
120
+ }
121
+ catch {
122
+ return inText(v);
123
+ }
124
+ }
125
+ let raw;
126
+ try {
127
+ raw = typeof result === "string" ? result : (JSON.stringify(result) ?? "");
128
+ }
129
+ catch {
130
+ raw = "";
131
+ }
132
+ return metaIn(v, 0) || inText(raw);
133
+ }
134
+ function metaIn(result, depth) {
135
+ if (depth > 4 || result === null || typeof result !== "object")
136
+ return "";
137
+ const meta = result._meta;
138
+ if (meta && typeof meta === "object") {
139
+ const t = meta[META_KEY];
140
+ if (typeof t === "string" && TOKEN.test(t))
141
+ return t;
142
+ }
143
+ for (const v of Array.isArray(result) ? result : Object.values(result)) {
144
+ if (v && typeof v === "object") {
145
+ const t = metaIn(v, depth + 1);
146
+ if (t)
147
+ return t;
148
+ }
149
+ }
150
+ return "";
151
+ }
152
+ /**
153
+ * The MCP server an OpenClaw tool name names: <server>__<tool> (its own
154
+ * runtime) or mcp__<server>__<tool> (Codex's). "" for a tool that isn't an
155
+ * MCP server's (exec, web fetch…), whose results never connect anything.
156
+ */
157
+ export function mcpServer(tool) {
158
+ if (typeof tool !== "string")
159
+ return "";
160
+ const rest = tool.startsWith("mcp__") ? tool.slice(5) : tool;
161
+ const i = rest.indexOf("__");
162
+ return i > 0 && i + 2 < rest.length ? rest.slice(0, i) : "";
163
+ }
164
+ /**
165
+ * The MCP tool an OpenClaw tool name names (what follows its server's
166
+ * "__", as mcpServer splits it), "" for a tool that isn't an MCP server's.
167
+ */
168
+ export function mcpTool(tool) {
169
+ const server = mcpServer(tool);
170
+ if (!server)
171
+ return "";
172
+ const name = tool;
173
+ const rest = name.startsWith("mcp__") ? name.slice(5) : name;
174
+ return rest.slice(server.length + 2);
175
+ }
176
+ /** A tool name with every character but ASCII letters, digits, _ and - turned into _ (beacon.NormalTool). */
177
+ export function normalTool(s) {
178
+ return s.replace(/[^A-Za-z0-9_-]/g, "_");
179
+ }
180
+ /** Whether a beacon's tool claim names tool, the bare MCP tool that produced the result (beacon.ToolMatches). */
181
+ export function toolMatches(claim, tool) {
182
+ return typeof claim === "string" && claim !== "" && tool !== "" && normalTool(claim) === normalTool(tool);
183
+ }
184
+ const TRAILER_RE = /(?:^|\n)\(For Wakeshift's hook on this computer, not for you: wakeshift-beacon: ([A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})\)$/;
185
+ /**
186
+ * The beacon on the last line of a result's text (beacon.Trailer): the
187
+ * SDKs put Wakeshift's text after the app's own, so a beacon the result
188
+ * was given ends it; one inside what the app wrote (a message it relays,
189
+ * JSON it returns) doesn't.
190
+ */
191
+ export function trailer(s) {
192
+ return TRAILER_RE.exec(s.replace(/[ \t\r\n]+$/, ""))?.[1] ?? "";
193
+ }
194
+ /** A result's text: itself if a string, else its last text content item's (a result as JSON text too). */
195
+ export function resultText(result, depth = 0) {
196
+ if (depth > 2)
197
+ return "";
198
+ if (typeof result === "string") {
199
+ const t = result.trim();
200
+ if (t.startsWith("{")) {
201
+ try {
202
+ const inner = resultText(JSON.parse(t), depth + 1);
203
+ if (inner)
204
+ return inner;
205
+ }
206
+ catch {
207
+ /* text */
208
+ }
209
+ }
210
+ return result;
211
+ }
212
+ const content = result && typeof result === "object" ? result.content : undefined;
213
+ if (!Array.isArray(content))
214
+ return "";
215
+ for (let i = content.length - 1; i >= 0; i--) {
216
+ const c = content[i];
217
+ if (c && c.type === "text" && typeof c.text === "string")
218
+ return c.text;
219
+ }
220
+ return "";
221
+ }
222
+ function ownMeta(v) {
223
+ if (!v || typeof v !== "object")
224
+ return "";
225
+ const meta = v._meta;
226
+ if (meta && typeof meta === "object") {
227
+ const t = meta[META_KEY];
228
+ if (typeof t === "string" && TOKEN.test(t))
229
+ return t;
230
+ }
231
+ return "";
232
+ }
233
+ /**
234
+ * The beacon a result carries, and where (connector.FindBeacon): its own
235
+ * _meta (or, in Codex's runtime, the MCP result's _meta that OpenClaw keeps
236
+ * in details), a _meta further down, or the trailer of its text.
237
+ */
238
+ export function findBeacon(result) {
239
+ let v = result;
240
+ if (typeof v === "string") {
241
+ try {
242
+ v = JSON.parse(v);
243
+ }
244
+ catch {
245
+ /* text */
246
+ }
247
+ }
248
+ // A result that came as text is the tool's words: a _meta in it (a chat
249
+ // message a read tool relays as its whole text), at the top or further
250
+ // in, isn't the result's, so only the line ending the text counts
251
+ // (connector.TextOnlyFormat; security review, October 28, 2026).
252
+ if (typeof result === "string") {
253
+ const t = trailer(resultText(result));
254
+ return t ? { token: t, carried: "text" } : { token: "", carried: "" };
255
+ }
256
+ const own = ownMeta(v) || (v && typeof v === "object" ? ownMeta(v.details) : "");
257
+ if (own)
258
+ return { token: own, carried: "meta" };
259
+ // The text's last line before a _meta further in: a document a tool
260
+ // returned can hold one, and mustn't hide the result's own beacon.
261
+ const t = trailer(resultText(result));
262
+ if (t)
263
+ return { token: t, carried: "text" };
264
+ const nested = metaIn(v, 0);
265
+ return nested ? { token: nested, carried: "nested" } : { token: "", carried: "" };
266
+ }
267
+ /**
268
+ * Whether a verified beacon may connect from where it was found, in the
269
+ * result of tool (the bare MCP tool; connector.acceptable): only from the
270
+ * tool it names, and one naming no tool (an app on an older SDK) only from
271
+ * the result's own _meta, which no text a tool relays can set.
272
+ */
273
+ export function acceptable(c, carried, tool) {
274
+ if (!carried)
275
+ return false;
276
+ // A _meta inside the result (a document or message a tool returned)
277
+ // carries only a beacon bound to a call (connector.acceptable).
278
+ if (carried === "nested" && !c.call)
279
+ return false;
280
+ if (!c.tool)
281
+ return carried === "meta";
282
+ return toolMatches(c.tool, tool);
283
+ }
284
+ const TEXT_RE = /wakeshift-beacon: ?([A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/;
285
+ export function inText(s) {
286
+ return TEXT_RE.exec(s)?.[1] ?? "";
287
+ }