openclaw-client 1.0.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,69 @@
1
+ import { OpenClawClient, type OpenClawClientConfig } from './client';
2
+
3
+ /**
4
+ * Server-side OpenClaw client manager
5
+ * Handles connection lifecycle for server actions
6
+ */
7
+ export class ServerOpenClawClient {
8
+ private config: OpenClawClientConfig;
9
+ private client: OpenClawClient | null = null;
10
+
11
+ constructor(config: OpenClawClientConfig) {
12
+ this.config = config;
13
+ }
14
+
15
+ /**
16
+ * Execute a function with a connected client
17
+ * Automatically handles connection and disconnection
18
+ */
19
+ async withClient<T>(fn: (client: OpenClawClient) => Promise<T>): Promise<T> {
20
+ const client = new OpenClawClient(this.config);
21
+
22
+ try {
23
+ await client.connect();
24
+ return await fn(client);
25
+ } finally {
26
+ client.disconnect();
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Get or create a persistent client connection
32
+ * Note: Use with caution in serverless environments
33
+ */
34
+ async getClient(): Promise<OpenClawClient> {
35
+ if (!this.client || !this.client.isConnected()) {
36
+ this.client = new OpenClawClient(this.config);
37
+ await this.client.connect();
38
+ }
39
+ return this.client;
40
+ }
41
+
42
+ /**
43
+ * Disconnect the persistent client
44
+ */
45
+ disconnect(): void {
46
+ if (this.client) {
47
+ this.client.disconnect();
48
+ this.client = null;
49
+ }
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Create a server OpenClaw client from environment variables
55
+ */
56
+ export function createServerClient(): ServerOpenClawClient {
57
+ // Convert HTTP URL to WebSocket URL
58
+ const gatewayUrl = process.env.OPENCLAW_GATEWAY_URL || 'http://localhost:18789';
59
+ const wsUrl = gatewayUrl.replace(/^http/, 'ws');
60
+
61
+ return new ServerOpenClawClient({
62
+ gatewayUrl: wsUrl,
63
+ token: process.env.OPENCLAW_TOKEN || '',
64
+ clientId: 'gateway-client',
65
+ clientVersion: '1.0.0',
66
+ platform: 'web',
67
+ mode: 'ui',
68
+ });
69
+ }