@artinet/cruiser 0.1.5 → 0.1.6

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/dist/index.d.ts CHANGED
@@ -17,6 +17,7 @@
17
17
  * | Claude | `@artinet/cruiser/claude` | {@link ClaudeAgent} |
18
18
  * | LangChain | `@artinet/cruiser/langchain` | {@link ReactAgent} |
19
19
  * | Mastra | `@artinet/cruiser/mastra` | {@link MastraAgent} |
20
+ * | OpenClaw | `@artinet/cruiser/openclaw` | {@link OpenClawAgent}|
20
21
  * | OpenAI | `@artinet/cruiser/openai` | {@link OpenAIAgent} |
21
22
  * | Strands | `@artinet/cruiser/strands` | {@link StrandsAgent} |
22
23
  *
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@
17
17
  * | Claude | `@artinet/cruiser/claude` | {@link ClaudeAgent} |
18
18
  * | LangChain | `@artinet/cruiser/langchain` | {@link ReactAgent} |
19
19
  * | Mastra | `@artinet/cruiser/mastra` | {@link MastraAgent} |
20
+ * | OpenClaw | `@artinet/cruiser/openclaw` | {@link OpenClawAgent}|
20
21
  * | OpenAI | `@artinet/cruiser/openai` | {@link OpenAIAgent} |
21
22
  * | Strands | `@artinet/cruiser/strands` | {@link StrandsAgent} |
22
23
  *
@@ -40,9 +40,9 @@
40
40
  *
41
41
  * @see {@link https://mastra.ai/docs} Mastra Documentation
42
42
  */
43
- import { OutputSchema } from "@mastra/core/stream";
44
- import { Agent as MastraAgent, AgentExecutionOptions } from "@mastra/core/agent";
45
- import { Dock, Park } from "../corsair.js";
43
+ import { OutputSchema } from '@mastra/core/stream';
44
+ import { Agent as MastraAgent, AgentExecutionOptions } from '@mastra/core/agent';
45
+ import { Dock, Park } from '../corsair.js';
46
46
  /**
47
47
  * Docks a Mastra agent onto artinet.
48
48
  *
@@ -104,8 +104,8 @@ import { Dock, Park } from "../corsair.js";
104
104
  * });
105
105
  * ```
106
106
  */
107
- export declare const dock: Dock<MastraAgent, AgentExecutionOptions<OutputSchema | undefined, "aisdk" | "mastra">>;
107
+ export declare const dock: Dock<MastraAgent, AgentExecutionOptions<OutputSchema | undefined>>;
108
108
  /**
109
109
  * @deprecated Use {@link dock} instead.
110
110
  */
111
- export declare const park: Park<MastraAgent, AgentExecutionOptions<OutputSchema | undefined, "aisdk" | "mastra">>;
111
+ export declare const park: Park<MastraAgent, AgentExecutionOptions<OutputSchema | undefined>>;
@@ -40,9 +40,9 @@
40
40
  *
41
41
  * @see {@link https://mastra.ai/docs} Mastra Documentation
42
42
  */
43
- import * as sdk from "@artinet/sdk";
44
- import { getAgentCard, convertToCoreMessage } from "./utils.js";
45
- import { v4 as uuidv4 } from "uuid";
43
+ import * as sdk from '@artinet/sdk';
44
+ import { getAgentCard, convertToCoreMessage } from './utils.js';
45
+ import { v4 as uuidv4 } from 'uuid';
46
46
  /**
47
47
  * Docks a Mastra agent onto artinet.
48
48
  *
@@ -135,7 +135,7 @@ export const dock = async (agent, card, options) => {
135
135
  contextId: task.contextId,
136
136
  message: sdk.describe.message({
137
137
  messageId: uuidv4(),
138
- role: "agent",
138
+ role: 'agent',
139
139
  parts: [sdk.describe.part.text(result.text)],
140
140
  metadata,
141
141
  }),
@@ -9,10 +9,10 @@
9
9
  *
10
10
  * @see {@link https://github.com/mastra-ai/mastra} Mastra Source
11
11
  */
12
- import * as sdk from "@artinet/sdk";
13
- import { AgentExecutionOptions, Agent as MastraAgent } from "@mastra/core/agent";
14
- import { CoreMessage } from "@mastra/core/llm";
15
- import { OutputSchema } from "@mastra/core/stream";
12
+ import * as sdk from '@artinet/sdk';
13
+ import { AgentExecutionOptions, Agent as MastraAgent } from '@mastra/core/agent';
14
+ import { CoreMessage } from '@mastra/core/llm';
15
+ import { OutputSchema } from '@mastra/core/stream';
16
16
  /**
17
17
  * Builds an {@link sdk.A2A.AgentCard} from Mastra agent configuration.
18
18
  *
@@ -36,10 +36,10 @@ import { OutputSchema } from "@mastra/core/stream";
36
36
  * });
37
37
  * ```
38
38
  */
39
- export declare function getAgentCard<OUTPUT extends OutputSchema = undefined, FORMAT extends "aisdk" | "mastra" = "mastra">({ agent, card, options: _options, }: {
39
+ export declare function getAgentCard<OUTPUT extends OutputSchema = undefined>({ agent, card, options: _options, }: {
40
40
  agent: MastraAgent;
41
41
  card?: sdk.A2A.AgentCardParams;
42
- options?: AgentExecutionOptions<OUTPUT, FORMAT>;
42
+ options?: AgentExecutionOptions<OUTPUT>;
43
43
  }): Promise<sdk.A2A.AgentCard>;
44
44
  /**
45
45
  * Converts an {@link sdk.A2A.Message} to Mastra's {@link CoreMessage} format.
@@ -9,7 +9,7 @@
9
9
  *
10
10
  * @see {@link https://github.com/mastra-ai/mastra} Mastra Source
11
11
  */
12
- import * as sdk from "@artinet/sdk";
12
+ import * as sdk from '@artinet/sdk';
13
13
  /**
14
14
  * Converts Mastra {@link SystemMessage} instructions to a plain string.
15
15
  *
@@ -27,25 +27,25 @@ import * as sdk from "@artinet/sdk";
27
27
  */
28
28
  function convertInstructionsToString(message) {
29
29
  if (!message) {
30
- return "";
30
+ return '';
31
31
  }
32
- if (typeof message === "string") {
32
+ if (typeof message === 'string') {
33
33
  return message;
34
34
  }
35
35
  if (Array.isArray(message)) {
36
36
  return message
37
37
  .map((m) => {
38
- if (typeof m === "string") {
38
+ if (typeof m === 'string') {
39
39
  return m;
40
40
  }
41
41
  // Safely extract content from message objects
42
- return typeof m.content === "string" ? m.content : "";
42
+ return typeof m.content === 'string' ? m.content : '';
43
43
  })
44
44
  .filter((content) => content) // Remove empty strings
45
- .join("\n");
45
+ .join('\n');
46
46
  }
47
47
  // Handle single message object - safely extract content
48
- return typeof message.content === "string" ? message.content : "";
48
+ return typeof message.content === 'string' ? message.content : '';
49
49
  }
50
50
  /**
51
51
  * Builds an {@link sdk.A2A.AgentCard} from Mastra agent configuration.
@@ -71,23 +71,23 @@ function convertInstructionsToString(message) {
71
71
  * ```
72
72
  */
73
73
  export async function getAgentCard({ agent, card, options: _options, }) {
74
- const [instructions, tools] = await Promise.all([agent.getInstructions(), agent.getTools()]);
74
+ const [instructions, tools = {}] = await Promise.all([agent.getInstructions(), agent.listTools?.() ?? Promise.resolve({})]);
75
75
  const agentCard = sdk.describe.card({
76
76
  name: agent.id,
77
- ...(typeof card === "string" ? { name: card } : card),
77
+ ...(typeof card === 'string' ? { name: card } : card),
78
78
  description: convertInstructionsToString(instructions),
79
79
  capabilities: {
80
80
  streaming: true,
81
81
  pushNotifications: true,
82
82
  stateTransitionHistory: false,
83
83
  },
84
- defaultInputModes: ["text"],
85
- defaultOutputModes: ["text"],
84
+ defaultInputModes: ['text'],
85
+ defaultOutputModes: ['text'],
86
86
  skills: Object.entries(tools).map(([toolId, tool]) => ({
87
87
  id: toolId,
88
88
  name: toolId,
89
89
  description: tool.description || `Tool: ${toolId}`,
90
- tags: ["tool"],
90
+ tags: ['tool'],
91
91
  })),
92
92
  });
93
93
  return agentCard;
@@ -112,7 +112,7 @@ export async function getAgentCard({ agent, card, options: _options, }) {
112
112
  */
113
113
  export function convertToCoreMessage(message) {
114
114
  return {
115
- role: message.role === "user" ? "user" : "assistant",
115
+ role: message.role === 'user' ? 'user' : 'assistant',
116
116
  content: message.parts.map((msg) => convertToCoreMessagePart(msg)),
117
117
  };
118
118
  }
@@ -131,21 +131,21 @@ export function convertToCoreMessage(message) {
131
131
  */
132
132
  function convertToCoreMessagePart(part) {
133
133
  switch (part.kind) {
134
- case "text":
134
+ case 'text':
135
135
  return {
136
- type: "text",
136
+ type: 'text',
137
137
  text: part.text,
138
138
  };
139
- case "file":
139
+ case 'file':
140
140
  return {
141
- type: "file",
142
- data: "uri" in part.file && part.file.uri
141
+ type: 'file',
142
+ data: 'uri' in part.file && part.file.uri
143
143
  ? new URL(part.file.uri)
144
144
  : /**Appeasing the type system */
145
145
  part.file.bytes,
146
- mimeType: part.file.mimeType ?? "unknown",
146
+ mimeType: part.file.mimeType ?? 'unknown',
147
147
  };
148
- case "data":
149
- throw new Error("Data parts are not supported in core messages");
148
+ case 'data':
149
+ throw new Error('Data parts are not supported in core messages');
150
150
  }
151
151
  }
@@ -0,0 +1,48 @@
1
+ import type { OpenClawAgent } from './utils.js';
2
+ type StoredAuthFile = {
3
+ version: 1;
4
+ device?: {
5
+ id: string;
6
+ publicKey: string;
7
+ privateKeyPem: string;
8
+ };
9
+ tokens?: {
10
+ operator?: {
11
+ token: string;
12
+ role?: string;
13
+ scopes?: string[];
14
+ updatedAtMs: number;
15
+ };
16
+ };
17
+ };
18
+ export type ResolvedDeviceIdentity = {
19
+ id: string;
20
+ publicKey: string;
21
+ privateKeyPem: string;
22
+ };
23
+ export declare function resolveAuthFilePath(agent: OpenClawAgent): string | undefined;
24
+ export declare function readStoredAuth(path: string | undefined): StoredAuthFile | undefined;
25
+ export declare function writeStoredAuth(path: string | undefined, auth: StoredAuthFile): void;
26
+ export declare function resolveOrCreateDeviceIdentity({ agent, authFilePath, }: {
27
+ agent: OpenClawAgent;
28
+ authFilePath: string | undefined;
29
+ }): ResolvedDeviceIdentity | undefined;
30
+ export declare function createSignedDevicePayload({ identity, scopes, token, nonce, }: {
31
+ identity: ResolvedDeviceIdentity;
32
+ scopes: string[];
33
+ token?: string;
34
+ nonce?: string;
35
+ }): {
36
+ id: string;
37
+ publicKey: string;
38
+ signature: string;
39
+ signedAt: number;
40
+ nonce?: string;
41
+ };
42
+ export declare function persistConnectAuth({ authFilePath, payload, scopes, deviceIdentity, }: {
43
+ authFilePath: string | undefined;
44
+ payload: unknown;
45
+ scopes: string[];
46
+ deviceIdentity?: ResolvedDeviceIdentity;
47
+ }): void;
48
+ export {};
@@ -0,0 +1,152 @@
1
+ import { createHash, createPrivateKey, createPublicKey, generateKeyPairSync, sign } from 'node:crypto';
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { homedir } from 'node:os';
4
+ import { dirname, join } from 'node:path';
5
+ const ED25519_SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
6
+ function base64UrlEncode(buffer) {
7
+ return buffer.toString('base64').replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/g, '');
8
+ }
9
+ function derivePublicKeyRawFromPem(publicKeyPem) {
10
+ const key = createPublicKey(publicKeyPem);
11
+ const spki = key.export({ type: 'spki', format: 'der' });
12
+ if (spki.length === ED25519_SPKI_PREFIX.length + 32 &&
13
+ spki.subarray(0, ED25519_SPKI_PREFIX.length).equals(ED25519_SPKI_PREFIX)) {
14
+ return spki.subarray(ED25519_SPKI_PREFIX.length);
15
+ }
16
+ return spki;
17
+ }
18
+ function fingerprintPublicKey(publicKeyBase64Url) {
19
+ return createHash('sha256')
20
+ .update(Buffer.from(publicKeyBase64Url.replaceAll('-', '+').replaceAll('_', '/'), 'base64'))
21
+ .digest('hex');
22
+ }
23
+ function buildDeviceAuthPayload({ version, deviceId, clientId, clientMode, role, scopes, signedAt, token, nonce, }) {
24
+ const base = [version, deviceId, clientId, clientMode, role, scopes.join(','), String(signedAt), token ?? ''];
25
+ if (version === 'v2') {
26
+ base.push(nonce ?? '');
27
+ }
28
+ return base.join('|');
29
+ }
30
+ export function resolveAuthFilePath(agent) {
31
+ if (agent.autoDeviceAuth === false) {
32
+ return undefined;
33
+ }
34
+ if (agent.authFilePath && agent.authFilePath.trim().length > 0) {
35
+ return agent.authFilePath.trim();
36
+ }
37
+ return join(homedir(), 'artinet-openclaw.auth');
38
+ }
39
+ export function readStoredAuth(path) {
40
+ if (!path || !existsSync(path)) {
41
+ return undefined;
42
+ }
43
+ try {
44
+ const parsed = JSON.parse(readFileSync(path, 'utf8'));
45
+ if (parsed.version !== 1) {
46
+ return undefined;
47
+ }
48
+ return parsed;
49
+ }
50
+ catch {
51
+ return undefined;
52
+ }
53
+ }
54
+ export function writeStoredAuth(path, auth) {
55
+ if (!path) {
56
+ return;
57
+ }
58
+ mkdirSync(dirname(path), { recursive: true });
59
+ writeFileSync(path, `${JSON.stringify(auth, null, 2)}\n`, { encoding: 'utf8' });
60
+ }
61
+ export function resolveOrCreateDeviceIdentity({ agent, authFilePath, }) {
62
+ const manual = agent.device;
63
+ if (manual?.id && manual.publicKey && manual.privateKeyPem) {
64
+ return {
65
+ id: manual.id,
66
+ publicKey: manual.publicKey,
67
+ privateKeyPem: manual.privateKeyPem,
68
+ };
69
+ }
70
+ const stored = readStoredAuth(authFilePath);
71
+ if (stored?.device?.id && stored.device.publicKey && stored.device.privateKeyPem) {
72
+ return stored.device;
73
+ }
74
+ if (agent.autoDeviceAuth === false) {
75
+ return undefined;
76
+ }
77
+ const keyPair = generateKeyPairSync('ed25519');
78
+ const publicKeyPem = keyPair.publicKey.export({ type: 'spki', format: 'pem' }).toString();
79
+ const privateKeyPem = keyPair.privateKey.export({ type: 'pkcs8', format: 'pem' }).toString();
80
+ const publicKey = base64UrlEncode(derivePublicKeyRawFromPem(publicKeyPem));
81
+ const identity = {
82
+ id: fingerprintPublicKey(publicKey),
83
+ publicKey,
84
+ privateKeyPem,
85
+ };
86
+ const nextAuth = stored ?? { version: 1 };
87
+ nextAuth.device = identity;
88
+ writeStoredAuth(authFilePath, nextAuth);
89
+ return identity;
90
+ }
91
+ export function createSignedDevicePayload({ identity, scopes, token, nonce, }) {
92
+ const signedAt = Date.now();
93
+ const version = nonce ? 'v2' : 'v1';
94
+ const payload = buildDeviceAuthPayload({
95
+ version,
96
+ deviceId: identity.id,
97
+ clientId: 'cli',
98
+ clientMode: 'cli',
99
+ role: 'operator',
100
+ scopes,
101
+ signedAt,
102
+ token,
103
+ nonce,
104
+ });
105
+ const signature = base64UrlEncode(sign(null, Buffer.from(payload, 'utf8'), createPrivateKey(identity.privateKeyPem)));
106
+ return {
107
+ id: identity.id,
108
+ publicKey: identity.publicKey,
109
+ signature,
110
+ signedAt,
111
+ ...(nonce ? { nonce } : {}),
112
+ };
113
+ }
114
+ export function persistConnectAuth({ authFilePath, payload, scopes, deviceIdentity, }) {
115
+ if (!authFilePath) {
116
+ return;
117
+ }
118
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
119
+ return;
120
+ }
121
+ const record = payload;
122
+ const auth = record.auth;
123
+ if (!auth || typeof auth !== 'object' || Array.isArray(auth)) {
124
+ return;
125
+ }
126
+ const authRecord = auth;
127
+ const deviceToken = authRecord.deviceToken;
128
+ if (typeof deviceToken !== 'string' || deviceToken.trim().length === 0) {
129
+ return;
130
+ }
131
+ const role = typeof authRecord.role === 'string' ? authRecord.role : 'operator';
132
+ const resolvedScopes = Array.isArray(authRecord.scopes)
133
+ ? authRecord.scopes.filter((scope) => typeof scope === 'string')
134
+ : scopes;
135
+ const existing = readStoredAuth(authFilePath) ?? { version: 1 };
136
+ const next = {
137
+ ...existing,
138
+ tokens: {
139
+ ...(existing.tokens ?? {}),
140
+ operator: {
141
+ token: deviceToken,
142
+ role,
143
+ scopes: resolvedScopes,
144
+ updatedAtMs: Date.now(),
145
+ },
146
+ },
147
+ };
148
+ if (deviceIdentity) {
149
+ next.device = deviceIdentity;
150
+ }
151
+ writeStoredAuth(authFilePath, next);
152
+ }
@@ -0,0 +1,37 @@
1
+ import type { OpenClawAgent, OpenClawResult } from './utils.js';
2
+ export declare class OpenClawGatewayClient {
3
+ private readonly url;
4
+ private readonly authToken?;
5
+ private readonly authPassword?;
6
+ private readonly device?;
7
+ private readonly deviceIdentity?;
8
+ private readonly authFilePath;
9
+ private readonly scopes;
10
+ private readonly connectTimeoutMs;
11
+ private readonly clientId;
12
+ private socket;
13
+ private connectPromise;
14
+ private isConnected;
15
+ private connectRequestId;
16
+ private pendingRequests;
17
+ constructor({ url, authToken, authPassword, agent, device, scopes, connectTimeoutMs, }: {
18
+ url: string;
19
+ authToken?: string;
20
+ authPassword?: string;
21
+ agent: OpenClawAgent;
22
+ device?: OpenClawAgent['device'];
23
+ scopes?: string[];
24
+ connectTimeoutMs: number;
25
+ });
26
+ ensureConnected(): Promise<void>;
27
+ requestAgentRun({ message, agentId, sessionKey, timeoutMs, }: {
28
+ message: string;
29
+ agentId: string;
30
+ sessionKey?: string;
31
+ timeoutMs: number;
32
+ }): Promise<OpenClawResult>;
33
+ private handleMessage;
34
+ private sendConnectRequest;
35
+ private sendFrame;
36
+ private rejectAllPending;
37
+ }
@@ -0,0 +1,236 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import { createSignedDevicePayload, persistConnectAuth, readStoredAuth, resolveAuthFilePath, resolveOrCreateDeviceIdentity, } from './auth.js';
3
+ export class OpenClawGatewayClient {
4
+ url;
5
+ authToken;
6
+ authPassword;
7
+ device;
8
+ deviceIdentity;
9
+ authFilePath;
10
+ scopes;
11
+ connectTimeoutMs;
12
+ clientId;
13
+ socket;
14
+ connectPromise;
15
+ isConnected = false;
16
+ connectRequestId;
17
+ pendingRequests = new Map();
18
+ constructor({ url, authToken, authPassword, agent, device, scopes, connectTimeoutMs, }) {
19
+ this.url = url;
20
+ this.authFilePath = resolveAuthFilePath(agent);
21
+ const storedAuth = readStoredAuth(this.authFilePath);
22
+ this.authToken = authToken ?? storedAuth?.tokens?.operator?.token;
23
+ this.authPassword = authPassword;
24
+ this.device = device;
25
+ this.deviceIdentity = resolveOrCreateDeviceIdentity({ agent, authFilePath: this.authFilePath });
26
+ const requestedScopes = scopes ?? [];
27
+ const requiredScopes = ['operator.read', 'operator.write'];
28
+ this.scopes = Array.from(new Set([...requiredScopes, ...requestedScopes]));
29
+ this.connectTimeoutMs = connectTimeoutMs;
30
+ this.clientId = uuidv4();
31
+ }
32
+ async ensureConnected() {
33
+ if (this.isConnected && this.socket?.readyState === WebSocket.OPEN) {
34
+ return;
35
+ }
36
+ if (this.connectPromise) {
37
+ return this.connectPromise;
38
+ }
39
+ this.connectPromise = new Promise((resolve, reject) => {
40
+ const socket = new WebSocket(this.url);
41
+ this.socket = socket;
42
+ const connectTimeout = setTimeout(() => {
43
+ reject(new Error('OpenClaw gateway connect timeout'));
44
+ }, this.connectTimeoutMs);
45
+ let challengeFallback;
46
+ function cleanupConnect() {
47
+ clearTimeout(connectTimeout);
48
+ if (challengeFallback) {
49
+ clearTimeout(challengeFallback);
50
+ challengeFallback = undefined;
51
+ }
52
+ }
53
+ socket.onopen = () => {
54
+ challengeFallback = setTimeout(() => {
55
+ this.sendConnectRequest();
56
+ }, 1_000);
57
+ };
58
+ socket.onmessage = (event) => {
59
+ this.handleMessage(event.data, resolve, reject, cleanupConnect);
60
+ };
61
+ socket.onerror = () => {
62
+ cleanupConnect();
63
+ reject(new Error('OpenClaw gateway socket error'));
64
+ };
65
+ socket.onclose = () => {
66
+ this.isConnected = false;
67
+ this.connectPromise = undefined;
68
+ this.connectRequestId = undefined;
69
+ this.socket = undefined;
70
+ this.rejectAllPending(new Error('OpenClaw gateway socket closed'));
71
+ };
72
+ })
73
+ .catch((error) => {
74
+ this.connectPromise = undefined;
75
+ throw error;
76
+ })
77
+ .then(() => undefined);
78
+ return this.connectPromise;
79
+ }
80
+ async requestAgentRun({ message, agentId, sessionKey, timeoutMs, }) {
81
+ await this.ensureConnected();
82
+ const requestId = uuidv4();
83
+ const idempotencyKey = uuidv4();
84
+ const frame = {
85
+ type: 'req',
86
+ id: requestId,
87
+ method: 'agent',
88
+ params: {
89
+ message,
90
+ agentId,
91
+ sessionKey,
92
+ deliver: false,
93
+ idempotencyKey,
94
+ },
95
+ };
96
+ const responsePromise = new Promise((resolve, reject) => {
97
+ this.pendingRequests.set(requestId, {
98
+ expectFinal: true,
99
+ resolve,
100
+ reject,
101
+ });
102
+ });
103
+ this.sendFrame(frame);
104
+ const timeout = new Promise((_, reject) => {
105
+ setTimeout(() => {
106
+ this.pendingRequests.delete(requestId);
107
+ reject(new Error('OpenClaw gateway request timeout'));
108
+ }, timeoutMs);
109
+ });
110
+ const payload = (await Promise.race([responsePromise, timeout]));
111
+ if (payload.status && payload.status !== 'ok') {
112
+ const details = payload.error?.message ?? 'unknown error';
113
+ throw new Error(`OpenClaw agent failed: ${payload.status}: ${details}`);
114
+ }
115
+ return (payload.result ?? payload);
116
+ }
117
+ handleMessage(raw, connectResolve, connectReject, cleanupConnect) {
118
+ let parsed;
119
+ try {
120
+ parsed = JSON.parse(raw);
121
+ }
122
+ catch {
123
+ return;
124
+ }
125
+ const frame = parsed;
126
+ if (frame.type === 'event' && frame.event === 'connect.challenge') {
127
+ this.sendConnectRequest(frame.payload?.nonce);
128
+ return;
129
+ }
130
+ if (frame.type !== 'res' || !frame.id) {
131
+ return;
132
+ }
133
+ const response = parsed;
134
+ if (frame.id === this.connectRequestId) {
135
+ cleanupConnect();
136
+ if (!response.ok) {
137
+ connectReject(new Error(response.error?.message ?? 'OpenClaw gateway connect rejected'));
138
+ return;
139
+ }
140
+ persistConnectAuth({
141
+ authFilePath: this.authFilePath,
142
+ payload: response.payload,
143
+ scopes: this.scopes,
144
+ deviceIdentity: this.deviceIdentity,
145
+ });
146
+ this.isConnected = true;
147
+ connectResolve();
148
+ return;
149
+ }
150
+ const pending = this.pendingRequests.get(frame.id);
151
+ if (!pending) {
152
+ return;
153
+ }
154
+ const payload = response.payload;
155
+ if (pending.expectFinal && payload?.status === 'accepted') {
156
+ return;
157
+ }
158
+ this.pendingRequests.delete(frame.id);
159
+ if (!response.ok) {
160
+ pending.reject(new Error(response.error?.message ?? 'OpenClaw gateway request failed'));
161
+ return;
162
+ }
163
+ pending.resolve(response.payload);
164
+ }
165
+ sendConnectRequest(nonce) {
166
+ if (this.connectRequestId) {
167
+ return;
168
+ }
169
+ const params = {
170
+ minProtocol: 3,
171
+ maxProtocol: 3,
172
+ client: {
173
+ id: 'cli',
174
+ displayName: 'cruiser-openclaw',
175
+ version: '0.1.5',
176
+ platform: 'node',
177
+ mode: 'cli',
178
+ instanceId: this.clientId,
179
+ },
180
+ role: 'operator',
181
+ scopes: this.scopes,
182
+ caps: [],
183
+ commands: [],
184
+ permissions: {},
185
+ locale: 'en-US',
186
+ userAgent: 'artinet-cruiser/openclaw',
187
+ };
188
+ if (this.authToken || this.authPassword) {
189
+ params.auth = {
190
+ ...(this.authToken ? { token: this.authToken } : {}),
191
+ ...(this.authPassword ? { password: this.authPassword } : {}),
192
+ };
193
+ }
194
+ const signedDevice = this.deviceIdentity
195
+ ? createSignedDevicePayload({
196
+ identity: this.deviceIdentity,
197
+ scopes: this.scopes,
198
+ token: this.authToken,
199
+ nonce,
200
+ })
201
+ : undefined;
202
+ if (signedDevice) {
203
+ params.device = signedDevice;
204
+ }
205
+ else if (this.device) {
206
+ const deviceNonce = nonce ?? this.device.nonce;
207
+ params.device = {
208
+ id: this.device.id,
209
+ publicKey: this.device.publicKey,
210
+ signature: this.device.signature,
211
+ signedAt: this.device.signedAt,
212
+ ...(deviceNonce ? { nonce: deviceNonce } : {}),
213
+ };
214
+ }
215
+ const requestId = uuidv4();
216
+ this.connectRequestId = requestId;
217
+ this.sendFrame({
218
+ type: 'req',
219
+ id: requestId,
220
+ method: 'connect',
221
+ params,
222
+ });
223
+ }
224
+ sendFrame(frame) {
225
+ if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
226
+ throw new Error('OpenClaw gateway is not connected');
227
+ }
228
+ this.socket.send(JSON.stringify(frame));
229
+ }
230
+ rejectAllPending(error) {
231
+ for (const pending of this.pendingRequests.values()) {
232
+ pending.reject(error);
233
+ }
234
+ this.pendingRequests.clear();
235
+ }
236
+ }