@privateclaw/privateclaw 0.1.23 → 0.1.25
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/README.md +13 -0
- package/dist/cli.js +24 -0
- package/dist/cli.js.map +1 -1
- package/dist/compat/openclaw.d.ts +14 -0
- package/dist/compat/openclaw.js +14 -0
- package/dist/compat/openclaw.js.map +1 -1
- package/dist/openclaw-plugin.d.ts +3 -0
- package/dist/openclaw-plugin.js +35 -0
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/provider.d.ts +15 -0
- package/dist/provider.js +327 -0
- package/dist/provider.js.map +1 -1
- package/dist/relay-provider-client.d.ts +4 -0
- package/dist/relay-provider-client.js +73 -27
- package/dist/relay-provider-client.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/openclaw.plugin.json +15 -1
- package/package.json +1 -1
- package/src/cli.ts +33 -0
- package/src/compat/openclaw.ts +15 -0
- package/src/openclaw-plugin.test.ts +3 -0
- package/src/openclaw-plugin.ts +46 -0
- package/src/provider.test.ts +427 -1
- package/src/provider.ts +419 -0
- package/src/relay-provider-client.test.ts +155 -0
- package/src/relay-provider-client.ts +115 -33
- package/src/types.ts +13 -0
|
@@ -10,6 +10,7 @@ const HEARTBEAT_INTERVAL_MS = 15_000;
|
|
|
10
10
|
const HEARTBEAT_TIMEOUT_MS = 45_000;
|
|
11
11
|
const INITIAL_RECONNECT_DELAY_MS = 1_000;
|
|
12
12
|
const MAX_RECONNECT_DELAY_MS = 30_000;
|
|
13
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
13
14
|
|
|
14
15
|
interface RelayProviderClientOptions {
|
|
15
16
|
providerWsUrl: string;
|
|
@@ -17,17 +18,24 @@ interface RelayProviderClientOptions {
|
|
|
17
18
|
onFrame?: (sessionId: string, envelope: EncryptedEnvelope) => Promise<void> | void;
|
|
18
19
|
onSessionClosed?: (sessionId: string, reason: string) => Promise<void> | void;
|
|
19
20
|
onError?: (message: string) => void;
|
|
21
|
+
requestTimeoutMs?: number;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
interface
|
|
23
|
-
resolve: (value:
|
|
24
|
+
interface PendingRequest<T> {
|
|
25
|
+
resolve: (value: T) => void;
|
|
24
26
|
reject: (error: Error) => void;
|
|
27
|
+
timeout: NodeJS.Timeout;
|
|
25
28
|
}
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
30
|
+
type PendingSessionCreation = PendingRequest<{
|
|
31
|
+
sessionId: string;
|
|
32
|
+
expiresAt: string;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
type PendingSessionRenewal = PendingRequest<{
|
|
36
|
+
sessionId: string;
|
|
37
|
+
expiresAt: string;
|
|
38
|
+
}>;
|
|
31
39
|
|
|
32
40
|
function parseRelayMessage(raw: string): RelayToProviderMessage {
|
|
33
41
|
return JSON.parse(raw) as RelayToProviderMessage;
|
|
@@ -39,6 +47,7 @@ export class RelayProviderClient {
|
|
|
39
47
|
private readonly pendingSessionCreations = new Map<string, PendingSessionCreation>();
|
|
40
48
|
private readonly pendingSessionRenewals = new Map<string, PendingSessionRenewal>();
|
|
41
49
|
private readonly providerId: string;
|
|
50
|
+
private readonly requestTimeoutMs: number;
|
|
42
51
|
private reconnectTimer: NodeJS.Timeout | undefined;
|
|
43
52
|
private heartbeatTimer: NodeJS.Timeout | undefined;
|
|
44
53
|
private reconnectDelayMs = INITIAL_RECONNECT_DELAY_MS;
|
|
@@ -48,6 +57,13 @@ export class RelayProviderClient {
|
|
|
48
57
|
|
|
49
58
|
constructor(private readonly options: RelayProviderClientOptions) {
|
|
50
59
|
this.providerId = options.providerId?.trim() || randomUUID();
|
|
60
|
+
this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
61
|
+
if (
|
|
62
|
+
!Number.isInteger(this.requestTimeoutMs) ||
|
|
63
|
+
this.requestTimeoutMs <= 0
|
|
64
|
+
) {
|
|
65
|
+
throw new Error("Relay provider requestTimeoutMs must be a positive integer.");
|
|
66
|
+
}
|
|
51
67
|
}
|
|
52
68
|
|
|
53
69
|
private buildProviderUrl(): string {
|
|
@@ -180,15 +196,63 @@ export class RelayProviderClient {
|
|
|
180
196
|
|
|
181
197
|
private rejectPending(error: Error): void {
|
|
182
198
|
for (const pending of this.pendingSessionCreations.values()) {
|
|
199
|
+
clearTimeout(pending.timeout);
|
|
183
200
|
pending.reject(error);
|
|
184
201
|
}
|
|
185
202
|
for (const pending of this.pendingSessionRenewals.values()) {
|
|
203
|
+
clearTimeout(pending.timeout);
|
|
186
204
|
pending.reject(error);
|
|
187
205
|
}
|
|
188
206
|
this.pendingSessionCreations.clear();
|
|
189
207
|
this.pendingSessionRenewals.clear();
|
|
190
208
|
}
|
|
191
209
|
|
|
210
|
+
private takePendingRequest<T>(
|
|
211
|
+
pendingRequests: Map<string, PendingRequest<T>>,
|
|
212
|
+
requestId: string,
|
|
213
|
+
): PendingRequest<T> | undefined {
|
|
214
|
+
const pending = pendingRequests.get(requestId);
|
|
215
|
+
if (!pending) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
pendingRequests.delete(requestId);
|
|
219
|
+
clearTimeout(pending.timeout);
|
|
220
|
+
return pending;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private queuePendingRequest<T>(params: {
|
|
224
|
+
pendingRequests: Map<string, PendingRequest<T>>;
|
|
225
|
+
requestId: string;
|
|
226
|
+
timeoutMessage: string;
|
|
227
|
+
send: () => void;
|
|
228
|
+
}): Promise<T> {
|
|
229
|
+
return new Promise<T>((resolve, reject) => {
|
|
230
|
+
const timeout = setTimeout(() => {
|
|
231
|
+
if (params.pendingRequests.delete(params.requestId)) {
|
|
232
|
+
reject(new Error(params.timeoutMessage));
|
|
233
|
+
}
|
|
234
|
+
}, this.requestTimeoutMs);
|
|
235
|
+
|
|
236
|
+
params.pendingRequests.set(params.requestId, {
|
|
237
|
+
resolve,
|
|
238
|
+
reject,
|
|
239
|
+
timeout,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
params.send();
|
|
244
|
+
} catch (error) {
|
|
245
|
+
clearTimeout(timeout);
|
|
246
|
+
params.pendingRequests.delete(params.requestId);
|
|
247
|
+
reject(
|
|
248
|
+
error instanceof Error
|
|
249
|
+
? error
|
|
250
|
+
: new Error(String(error)),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
192
256
|
private async handleMessage(
|
|
193
257
|
raw: string,
|
|
194
258
|
hooks?: { markReady?: () => void },
|
|
@@ -200,26 +264,30 @@ export class RelayProviderClient {
|
|
|
200
264
|
hooks?.markReady?.();
|
|
201
265
|
return;
|
|
202
266
|
case "relay:session_created": {
|
|
203
|
-
const pending = this.
|
|
267
|
+
const pending = this.takePendingRequest(
|
|
268
|
+
this.pendingSessionCreations,
|
|
269
|
+
message.requestId,
|
|
270
|
+
);
|
|
204
271
|
if (!pending) {
|
|
205
272
|
this.options.onError?.(
|
|
206
273
|
`Received relay:session_created for unknown request ${message.requestId}.`,
|
|
207
274
|
);
|
|
208
275
|
return;
|
|
209
276
|
}
|
|
210
|
-
this.pendingSessionCreations.delete(message.requestId);
|
|
211
277
|
pending.resolve({ sessionId: message.sessionId, expiresAt: message.expiresAt });
|
|
212
278
|
return;
|
|
213
279
|
}
|
|
214
280
|
case "relay:session_renewed": {
|
|
215
|
-
const pending = this.
|
|
281
|
+
const pending = this.takePendingRequest(
|
|
282
|
+
this.pendingSessionRenewals,
|
|
283
|
+
message.requestId,
|
|
284
|
+
);
|
|
216
285
|
if (!pending) {
|
|
217
286
|
this.options.onError?.(
|
|
218
287
|
`Received relay:session_renewed for unknown request ${message.requestId}.`,
|
|
219
288
|
);
|
|
220
289
|
return;
|
|
221
290
|
}
|
|
222
|
-
this.pendingSessionRenewals.delete(message.requestId);
|
|
223
291
|
pending.resolve({ sessionId: message.sessionId, expiresAt: message.expiresAt });
|
|
224
292
|
return;
|
|
225
293
|
}
|
|
@@ -231,15 +299,19 @@ export class RelayProviderClient {
|
|
|
231
299
|
return;
|
|
232
300
|
case "relay:error": {
|
|
233
301
|
if (message.requestId) {
|
|
234
|
-
const creation = this.
|
|
302
|
+
const creation = this.takePendingRequest(
|
|
303
|
+
this.pendingSessionCreations,
|
|
304
|
+
message.requestId,
|
|
305
|
+
);
|
|
235
306
|
if (creation) {
|
|
236
|
-
this.pendingSessionCreations.delete(message.requestId);
|
|
237
307
|
creation.reject(new Error(`[${message.code}] ${message.message}`));
|
|
238
308
|
return;
|
|
239
309
|
}
|
|
240
|
-
const renewal = this.
|
|
310
|
+
const renewal = this.takePendingRequest(
|
|
311
|
+
this.pendingSessionRenewals,
|
|
312
|
+
message.requestId,
|
|
313
|
+
);
|
|
241
314
|
if (renewal) {
|
|
242
|
-
this.pendingSessionRenewals.delete(message.requestId);
|
|
243
315
|
renewal.reject(new Error(`[${message.code}] ${message.message}`));
|
|
244
316
|
return;
|
|
245
317
|
}
|
|
@@ -271,16 +343,21 @@ export class RelayProviderClient {
|
|
|
271
343
|
): Promise<{ sessionId: string; expiresAt: string }> {
|
|
272
344
|
await this.connect();
|
|
273
345
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
this.pendingSessionCreations
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
346
|
+
const requestId = randomUUID();
|
|
347
|
+
return this.queuePendingRequest({
|
|
348
|
+
pendingRequests: this.pendingSessionCreations,
|
|
349
|
+
requestId,
|
|
350
|
+
timeoutMessage:
|
|
351
|
+
`Timed out waiting for relay to create a session after ${this.requestTimeoutMs}ms.`,
|
|
352
|
+
send: () => {
|
|
353
|
+
this.send({
|
|
354
|
+
type: "provider:create_session",
|
|
355
|
+
requestId,
|
|
356
|
+
...(ttlMs ? { ttlMs } : {}),
|
|
357
|
+
...(label ? { label } : {}),
|
|
358
|
+
...(typeof groupMode === "boolean" ? { groupMode } : {}),
|
|
359
|
+
});
|
|
360
|
+
},
|
|
284
361
|
});
|
|
285
362
|
}
|
|
286
363
|
|
|
@@ -290,15 +367,20 @@ export class RelayProviderClient {
|
|
|
290
367
|
): Promise<{ sessionId: string; expiresAt: string }> {
|
|
291
368
|
await this.connect();
|
|
292
369
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
this.pendingSessionRenewals
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
370
|
+
const requestId = randomUUID();
|
|
371
|
+
return this.queuePendingRequest({
|
|
372
|
+
pendingRequests: this.pendingSessionRenewals,
|
|
373
|
+
requestId,
|
|
374
|
+
timeoutMessage:
|
|
375
|
+
`Timed out waiting for relay to renew session ${sessionId} after ${this.requestTimeoutMs}ms.`,
|
|
376
|
+
send: () => {
|
|
377
|
+
this.send({
|
|
378
|
+
type: "provider:renew_session",
|
|
379
|
+
requestId,
|
|
380
|
+
sessionId,
|
|
381
|
+
ttlMs,
|
|
382
|
+
});
|
|
383
|
+
},
|
|
302
384
|
});
|
|
303
385
|
}
|
|
304
386
|
|
package/src/types.ts
CHANGED
|
@@ -85,6 +85,9 @@ export interface PrivateClawProviderOptions {
|
|
|
85
85
|
defaultTtlMs?: number;
|
|
86
86
|
providerLabel?: string;
|
|
87
87
|
welcomeMessage?: string;
|
|
88
|
+
botMode?: boolean;
|
|
89
|
+
botModeSilentJoinDelayMs?: number;
|
|
90
|
+
botModeIdleDelayMs?: number;
|
|
88
91
|
commandsProvider?: () => Promise<PrivateClawSlashCommand[]>;
|
|
89
92
|
verboseController?: PrivateClawVerboseController;
|
|
90
93
|
onLog?: (message: string) => void;
|
|
@@ -92,6 +95,8 @@ export interface PrivateClawProviderOptions {
|
|
|
92
95
|
|
|
93
96
|
export interface ProviderParticipantState extends PrivateClawParticipant {
|
|
94
97
|
lastSeenAt: string;
|
|
98
|
+
lastUserMessageAt?: string;
|
|
99
|
+
botModeSilentJoinPromptSentAt?: string;
|
|
95
100
|
supportsThinkingTrace?: boolean;
|
|
96
101
|
}
|
|
97
102
|
|
|
@@ -124,12 +129,17 @@ export interface ProviderSessionState {
|
|
|
124
129
|
participants: Map<string, ProviderParticipantState>;
|
|
125
130
|
removedParticipantAppIds: Set<string>;
|
|
126
131
|
state: "awaiting_hello" | "active";
|
|
132
|
+
lastGroupActivityAt?: string;
|
|
133
|
+
botModeIdleAnchorAt?: string;
|
|
134
|
+
botModeLastIdlePromptAt?: string;
|
|
127
135
|
pendingRenewal?: {
|
|
128
136
|
expiresAt: string;
|
|
129
137
|
sentAt: string;
|
|
130
138
|
};
|
|
131
139
|
renewalReminderSentAt?: string;
|
|
132
140
|
renewalReminderTimer?: ReturnType<typeof setTimeout>;
|
|
141
|
+
botModeIdleTimer?: ReturnType<typeof setTimeout>;
|
|
142
|
+
botModeSilentJoinTimers?: Map<string, ReturnType<typeof setTimeout>>;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
export interface PrivateClawProviderSessionHandoff {
|
|
@@ -146,6 +156,9 @@ export interface PrivateClawProviderSessionHandoff {
|
|
|
146
156
|
sentAt: string;
|
|
147
157
|
};
|
|
148
158
|
renewalReminderSentAt?: string;
|
|
159
|
+
lastGroupActivityAt?: string;
|
|
160
|
+
botModeIdleAnchorAt?: string;
|
|
161
|
+
botModeLastIdlePromptAt?: string;
|
|
149
162
|
}
|
|
150
163
|
|
|
151
164
|
export interface PrivateClawProviderHandoffState {
|