@clawroom/openclaw 0.5.1 → 0.5.20
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/node_modules/@clawroom/protocol/package.json +1 -1
- package/node_modules/@clawroom/protocol/src/index.ts +70 -2
- package/node_modules/@clawroom/sdk/package.json +1 -1
- package/node_modules/@clawroom/sdk/src/client.ts +278 -28
- package/node_modules/@clawroom/sdk/src/index.ts +2 -0
- package/node_modules/@clawroom/sdk/src/machine-client.ts +200 -53
- package/node_modules/@clawroom/sdk/src/protocol.ts +2 -0
- package/node_modules/@clawroom/sdk/src/ws-transport.ts +123 -28
- package/package.json +1 -1
- package/src/channel.ts +23 -59
- package/src/chat-executor.ts +185 -26
- package/src/reflections.ts +60 -0
- package/src/runtime.ts +1 -0
- package/src/task-executor.ts +114 -20
- package/src/client.ts +0 -56
|
@@ -2,6 +2,11 @@ export interface AgentHeartbeat {
|
|
|
2
2
|
type: "agent.heartbeat";
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
+
export interface AgentWorkRef {
|
|
6
|
+
workId: string;
|
|
7
|
+
leaseToken: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
5
10
|
export interface AgentResultFile {
|
|
6
11
|
filename: string;
|
|
7
12
|
mimeType: string;
|
|
@@ -13,19 +18,21 @@ export interface AgentComplete {
|
|
|
13
18
|
taskId: string;
|
|
14
19
|
output: string;
|
|
15
20
|
attachments?: AgentResultFile[];
|
|
21
|
+
workRef?: AgentWorkRef;
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
export interface AgentProgress {
|
|
19
25
|
type: "agent.progress";
|
|
20
26
|
taskId: string;
|
|
21
27
|
message: string;
|
|
22
|
-
|
|
28
|
+
workRef?: AgentWorkRef;
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
export interface AgentFail {
|
|
26
32
|
type: "agent.fail";
|
|
27
33
|
taskId: string;
|
|
28
34
|
reason: string;
|
|
35
|
+
workRef?: AgentWorkRef;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
export interface AgentChatReply {
|
|
@@ -33,6 +40,7 @@ export interface AgentChatReply {
|
|
|
33
40
|
channelId: string;
|
|
34
41
|
content: string;
|
|
35
42
|
replyTo?: string;
|
|
43
|
+
workRefs?: AgentWorkRef[];
|
|
36
44
|
}
|
|
37
45
|
|
|
38
46
|
export interface AgentTyping {
|
|
@@ -54,13 +62,62 @@ export interface ServerTask {
|
|
|
54
62
|
title: string;
|
|
55
63
|
description: string;
|
|
56
64
|
input: string;
|
|
57
|
-
|
|
65
|
+
channelId?: string | null;
|
|
66
|
+
workId: string;
|
|
67
|
+
leaseToken: string;
|
|
68
|
+
taskRole?: "root" | "child";
|
|
69
|
+
assignedAgentId?: string | null;
|
|
70
|
+
assignedAgentName?: string | null;
|
|
71
|
+
executionBrief?: ServerTaskExecutionBrief | null;
|
|
72
|
+
taskDiscussionContext?: ServerTaskDiscussionEntry[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface AgentChatProfile {
|
|
76
|
+
role: string;
|
|
77
|
+
systemPrompt: string;
|
|
78
|
+
memory: string;
|
|
79
|
+
continuityPacket: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface ServerTaskExecutionBrief {
|
|
83
|
+
goal: string;
|
|
84
|
+
firstActions: string[];
|
|
85
|
+
blockingUnknowns: string[];
|
|
86
|
+
nonBlockingUnknowns: string[];
|
|
87
|
+
doneDefinition: string;
|
|
88
|
+
nextCheckpoint: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface ServerTaskDiscussionEntry {
|
|
92
|
+
id: string;
|
|
93
|
+
senderType: string;
|
|
94
|
+
senderName: string;
|
|
95
|
+
content: string;
|
|
96
|
+
createdAt: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface ChatAttachmentRef {
|
|
100
|
+
id?: string;
|
|
101
|
+
filename: string;
|
|
102
|
+
mimeType: string;
|
|
103
|
+
byteSize?: number;
|
|
104
|
+
downloadUrl: string;
|
|
58
105
|
}
|
|
59
106
|
|
|
60
107
|
export interface ServerChatMessage {
|
|
108
|
+
type: "server.chat";
|
|
109
|
+
kind: "chat_reply" | "wake";
|
|
110
|
+
workId: string;
|
|
111
|
+
leaseToken: string;
|
|
61
112
|
messageId: string;
|
|
62
113
|
channelId: string;
|
|
114
|
+
taskId?: string | null;
|
|
63
115
|
content: string;
|
|
116
|
+
attachments?: ChatAttachmentRef[];
|
|
117
|
+
isMention?: boolean;
|
|
118
|
+
wakeReason?: "mention" | "dm" | "follow_through" | "goal_drift" | "trigger" | "broadcast" | "routed" | "coordination" | "review";
|
|
119
|
+
triggerReason?: string;
|
|
120
|
+
agentProfile: AgentChatProfile;
|
|
64
121
|
context: Array<{
|
|
65
122
|
id: string;
|
|
66
123
|
senderType: string;
|
|
@@ -68,6 +125,17 @@ export interface ServerChatMessage {
|
|
|
68
125
|
content: string;
|
|
69
126
|
createdAt: number;
|
|
70
127
|
}>;
|
|
128
|
+
replyToMessageId?: string | null;
|
|
129
|
+
senderName?: string;
|
|
130
|
+
senderType?: string;
|
|
131
|
+
createdAt?: number;
|
|
132
|
+
channelMembers?: Array<{ id: string; name: string; type: string }>;
|
|
133
|
+
taskTitle?: string | null;
|
|
134
|
+
taskRole?: "root" | "child";
|
|
135
|
+
assignedAgentId?: string | null;
|
|
136
|
+
assignedAgentName?: string | null;
|
|
137
|
+
executionBrief?: ServerTaskExecutionBrief | null;
|
|
138
|
+
taskDiscussionContext?: ServerTaskDiscussionEntry[];
|
|
71
139
|
}
|
|
72
140
|
|
|
73
141
|
export type ServerMessage = ServerTask | ServerChatMessage;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
AgentChatProfile,
|
|
3
|
+
AgentResultFile,
|
|
2
4
|
AgentMessage,
|
|
5
|
+
AgentWorkRef,
|
|
3
6
|
ServerTask,
|
|
4
7
|
ServerChatMessage,
|
|
5
8
|
} from "./protocol.js";
|
|
@@ -8,9 +11,18 @@ import { WsTransport } from "./ws-transport.js";
|
|
|
8
11
|
const DEFAULT_ENDPOINT = "https://clawroom.site9.ai/api/agents";
|
|
9
12
|
const HEARTBEAT_INTERVAL_MS = 30_000;
|
|
10
13
|
const POLL_INTERVAL_MS = 10_000;
|
|
14
|
+
const DEFAULT_AGENT_CHAT_PROFILE: AgentChatProfile = {
|
|
15
|
+
role: "No role defined",
|
|
16
|
+
systemPrompt: "No system prompt configured",
|
|
17
|
+
memory: "No memory recorded yet",
|
|
18
|
+
continuityPacket: "No continuity packet available yet",
|
|
19
|
+
};
|
|
11
20
|
|
|
12
21
|
export type TaskCallback = (task: ServerTask) => void;
|
|
13
22
|
export type ChatCallback = (messages: ServerChatMessage[]) => void;
|
|
23
|
+
export type ConnectedCallback = (agentId: string) => void;
|
|
24
|
+
export type DisconnectedCallback = () => void;
|
|
25
|
+
export type Unsubscribe = () => void;
|
|
14
26
|
|
|
15
27
|
export type ClawroomClientOptions = {
|
|
16
28
|
endpoint?: string;
|
|
@@ -26,6 +38,17 @@ export type ClawroomClientOptions = {
|
|
|
26
38
|
};
|
|
27
39
|
};
|
|
28
40
|
|
|
41
|
+
type AgentHeartbeatResponse = {
|
|
42
|
+
ok?: boolean;
|
|
43
|
+
agentId?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type AgentPollResponse = {
|
|
47
|
+
agentId?: string;
|
|
48
|
+
task: ServerTask | null;
|
|
49
|
+
chat: ServerChatMessage[] | null;
|
|
50
|
+
};
|
|
51
|
+
|
|
29
52
|
/**
|
|
30
53
|
* ClawRoom SDK client.
|
|
31
54
|
* WebSocket primary for real-time push, HTTP polling as fallback.
|
|
@@ -33,12 +56,19 @@ export type ClawroomClientOptions = {
|
|
|
33
56
|
export class ClawroomClient {
|
|
34
57
|
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
35
58
|
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
59
|
+
private heartbeatInFlight = false;
|
|
60
|
+
private pollInFlight = false;
|
|
36
61
|
protected stopped = false;
|
|
37
62
|
protected readonly httpBase: string;
|
|
38
63
|
protected readonly options: ClawroomClientOptions;
|
|
39
64
|
protected taskCallbacks: TaskCallback[] = [];
|
|
40
65
|
protected chatCallbacks: ChatCallback[] = [];
|
|
66
|
+
private connectedCallbacks: ConnectedCallback[] = [];
|
|
67
|
+
private disconnectedCallbacks: DisconnectedCallback[] = [];
|
|
41
68
|
private wsTransport: WsTransport | null = null;
|
|
69
|
+
private recentChatIds = new Set<string>();
|
|
70
|
+
private connectedAgentId: string | null = null;
|
|
71
|
+
private isConnectedValue = false;
|
|
42
72
|
|
|
43
73
|
constructor(options: ClawroomClientOptions) {
|
|
44
74
|
this.options = options;
|
|
@@ -50,12 +80,26 @@ export class ClawroomClient {
|
|
|
50
80
|
return this.httpBase.replace(/\/api\/agents$/, "").replace(/^http/, "ws") + "/api/ws";
|
|
51
81
|
}
|
|
52
82
|
|
|
83
|
+
get connected(): boolean {
|
|
84
|
+
return this.isConnectedValue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get agentId(): string | null {
|
|
88
|
+
return this.connectedAgentId;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get isAlive(): boolean {
|
|
92
|
+
return !this.stopped;
|
|
93
|
+
}
|
|
94
|
+
|
|
53
95
|
connect(): void {
|
|
54
96
|
this.stopped = false;
|
|
55
97
|
this.startHeartbeat();
|
|
56
98
|
void this.register();
|
|
57
99
|
|
|
58
100
|
// WebSocket transport
|
|
101
|
+
this.wsTransport?.disconnect();
|
|
102
|
+
this.wsTransport = null;
|
|
59
103
|
this.wsTransport = new WsTransport({
|
|
60
104
|
url: this.wsUrl,
|
|
61
105
|
token: this.options.token,
|
|
@@ -64,21 +108,48 @@ export class ClawroomClient {
|
|
|
64
108
|
onDisconnected: () => { this.options.log?.info?.("[clawroom] WebSocket disconnected"); },
|
|
65
109
|
onMessage: (msg) => {
|
|
66
110
|
if (msg.type === "task" && msg.task) {
|
|
67
|
-
|
|
111
|
+
const task = msg.task as ServerTask;
|
|
112
|
+
void this.dispatchTask(task);
|
|
68
113
|
}
|
|
69
114
|
if (msg.type === "chat" && Array.isArray(msg.messages)) {
|
|
70
|
-
|
|
115
|
+
const fresh = msg.messages.filter((m: ServerChatMessage) => this.rememberChat(m.workId ?? m.messageId));
|
|
116
|
+
if (fresh.length > 0) {
|
|
117
|
+
for (const cb of this.chatCallbacks) cb(fresh);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const message = getRecord(msg.message);
|
|
121
|
+
if (msg.type === "message" && message) {
|
|
122
|
+
const agentProfile = resolveAgentChatProfile(msg.agentProfile, message.agentProfile);
|
|
123
|
+
const delivered = [{
|
|
124
|
+
workId: getString(message.workId),
|
|
125
|
+
leaseToken: getString(message.leaseToken),
|
|
126
|
+
messageId: getString(message.id, getString(msg.messageId)),
|
|
127
|
+
channelId: getString(message.channelId),
|
|
128
|
+
content: getString(message.content),
|
|
129
|
+
attachments: getAttachments(msg.attachments) ?? getAttachments(message.attachments),
|
|
130
|
+
context: getContext(msg.context),
|
|
131
|
+
isMention: typeof msg.isMention === "boolean" ? msg.isMention : false,
|
|
132
|
+
wakeReason: getWakeReason(msg.wakeReason) ?? getWakeReason(message.wakeReason),
|
|
133
|
+
triggerReason: getOptionalString(msg.triggerReason) ?? getOptionalString(message.triggerReason),
|
|
134
|
+
agentProfile,
|
|
135
|
+
}] as ServerChatMessage[];
|
|
136
|
+
const fresh = delivered.filter((m) => this.rememberChat(m.workId ?? m.messageId));
|
|
137
|
+
if (fresh.length > 0) {
|
|
138
|
+
for (const cb of this.chatCallbacks) cb(fresh);
|
|
139
|
+
}
|
|
71
140
|
}
|
|
72
141
|
},
|
|
73
142
|
});
|
|
74
143
|
this.wsTransport.connect();
|
|
75
144
|
|
|
76
|
-
// HTTP
|
|
145
|
+
// Keep HTTP polling active even when WS is up.
|
|
146
|
+
// WS is the fast path; polling is the durable delivery path.
|
|
77
147
|
this.stopPolling();
|
|
78
148
|
this.pollTimer = setInterval(() => {
|
|
79
|
-
if (this.wsTransport?.connected) return;
|
|
80
149
|
void this.pollTick();
|
|
81
150
|
}, POLL_INTERVAL_MS);
|
|
151
|
+
|
|
152
|
+
void this.pollTick();
|
|
82
153
|
}
|
|
83
154
|
|
|
84
155
|
disconnect(): void {
|
|
@@ -87,14 +158,84 @@ export class ClawroomClient {
|
|
|
87
158
|
this.stopPolling();
|
|
88
159
|
this.wsTransport?.disconnect();
|
|
89
160
|
this.wsTransport = null;
|
|
161
|
+
this.recentChatIds.clear();
|
|
162
|
+
this.markDisconnected();
|
|
163
|
+
this.connectedAgentId = null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async send(message: AgentMessage): Promise<void> {
|
|
167
|
+
await this.sendViaHttp(message);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
onTask(cb: TaskCallback): Unsubscribe {
|
|
171
|
+
this.taskCallbacks.push(cb);
|
|
172
|
+
return () => this.offTask(cb);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
offTask(cb: TaskCallback): void {
|
|
176
|
+
removeCallback(this.taskCallbacks, cb);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
onChatMessage(cb: ChatCallback): Unsubscribe {
|
|
180
|
+
this.chatCallbacks.push(cb);
|
|
181
|
+
return () => this.offChatMessage(cb);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
offChatMessage(cb: ChatCallback): void {
|
|
185
|
+
removeCallback(this.chatCallbacks, cb);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
onConnected(cb: ConnectedCallback): Unsubscribe {
|
|
189
|
+
this.connectedCallbacks.push(cb);
|
|
190
|
+
return () => this.offConnected(cb);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
offConnected(cb: ConnectedCallback): void {
|
|
194
|
+
removeCallback(this.connectedCallbacks, cb);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
onDisconnected(cb: DisconnectedCallback): Unsubscribe {
|
|
198
|
+
this.disconnectedCallbacks.push(cb);
|
|
199
|
+
return () => this.offDisconnected(cb);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
offDisconnected(cb: DisconnectedCallback): void {
|
|
203
|
+
removeCallback(this.disconnectedCallbacks, cb);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async sendComplete(taskId: string, output: string, attachments?: AgentResultFile[], workRef?: AgentWorkRef): Promise<void> {
|
|
207
|
+
await this.send({ type: "agent.complete", taskId, output, attachments, workRef });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async sendFail(taskId: string, reason: string, workRef?: AgentWorkRef): Promise<void> {
|
|
211
|
+
await this.send({ type: "agent.fail", taskId, reason, workRef });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async sendProgress(taskId: string, message: string, workRef?: AgentWorkRef): Promise<void> {
|
|
215
|
+
await this.send({ type: "agent.progress", taskId, message, workRef });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async sendChatReply(channelId: string, content: string, replyTo?: string, workRefs?: AgentWorkRef[]): Promise<void> {
|
|
219
|
+
await this.send({ type: "agent.chat.reply", channelId, content, replyTo, workRefs });
|
|
90
220
|
}
|
|
91
221
|
|
|
92
|
-
|
|
93
|
-
this.
|
|
222
|
+
async sendTyping(channelId: string): Promise<void> {
|
|
223
|
+
await this.send({ type: "agent.typing", channelId });
|
|
94
224
|
}
|
|
95
225
|
|
|
96
|
-
|
|
97
|
-
|
|
226
|
+
async sendReflection(reflection: {
|
|
227
|
+
scope: "chat" | "task";
|
|
228
|
+
status: string;
|
|
229
|
+
summary: string;
|
|
230
|
+
channelId?: string | null;
|
|
231
|
+
taskId?: string | null;
|
|
232
|
+
messageId?: string | null;
|
|
233
|
+
toolsUsed?: string[];
|
|
234
|
+
responseExcerpt?: string | null;
|
|
235
|
+
detail?: Record<string, unknown>;
|
|
236
|
+
}): Promise<void> {
|
|
237
|
+
await this.httpRequest("POST", "/reflections", reflection);
|
|
238
|
+
}
|
|
98
239
|
|
|
99
240
|
// ── Heartbeat ─────────────────────────────────────────
|
|
100
241
|
|
|
@@ -114,67 +255,176 @@ export class ClawroomClient {
|
|
|
114
255
|
}
|
|
115
256
|
|
|
116
257
|
protected async register(): Promise<void> {
|
|
258
|
+
if (this.heartbeatInFlight) return;
|
|
259
|
+
this.heartbeatInFlight = true;
|
|
117
260
|
try {
|
|
118
|
-
await this.httpRequest("POST", "/heartbeat", {
|
|
261
|
+
const response = await this.httpRequest("POST", "/heartbeat", {
|
|
119
262
|
deviceId: this.options.deviceId,
|
|
120
263
|
skills: this.options.skills,
|
|
121
264
|
kind: this.options.kind ?? "openclaw",
|
|
122
|
-
});
|
|
123
|
-
this.onPollSuccess(
|
|
265
|
+
}) as AgentHeartbeatResponse;
|
|
266
|
+
this.onPollSuccess(response.agentId);
|
|
124
267
|
} catch (err) {
|
|
125
268
|
this.options.log?.warn?.(`[clawroom] heartbeat error: ${err}`);
|
|
126
|
-
this.
|
|
269
|
+
if (!this.wsTransport?.connected) {
|
|
270
|
+
this.onPollError(err);
|
|
271
|
+
}
|
|
272
|
+
} finally {
|
|
273
|
+
this.heartbeatInFlight = false;
|
|
127
274
|
}
|
|
128
275
|
}
|
|
129
276
|
|
|
130
277
|
protected async pollTick(): Promise<void> {
|
|
131
|
-
if (this.stopped) return;
|
|
278
|
+
if (this.stopped || this.pollInFlight) return;
|
|
279
|
+
this.pollInFlight = true;
|
|
132
280
|
try {
|
|
133
|
-
const res = await this.httpRequest("POST", "/poll", {});
|
|
281
|
+
const res = await this.httpRequest("POST", "/poll", {}) as AgentPollResponse;
|
|
134
282
|
this.onPollSuccess(res?.agentId);
|
|
135
283
|
if (res.task) {
|
|
136
|
-
this.
|
|
137
|
-
for (const cb of this.taskCallbacks) cb(res.task);
|
|
284
|
+
await this.dispatchTask(res.task);
|
|
138
285
|
}
|
|
139
286
|
if (res.chat && Array.isArray(res.chat) && res.chat.length > 0) {
|
|
140
|
-
|
|
141
|
-
|
|
287
|
+
const fresh = res.chat.filter((m: ServerChatMessage) => this.rememberChat(m.workId ?? m.messageId));
|
|
288
|
+
if (fresh.length > 0) {
|
|
289
|
+
this.options.log?.info?.(`[clawroom] received ${fresh.length} chat mention(s)`);
|
|
290
|
+
for (const cb of this.chatCallbacks) cb(fresh);
|
|
291
|
+
}
|
|
142
292
|
}
|
|
143
293
|
} catch (err) {
|
|
144
294
|
this.options.log?.warn?.(`[clawroom] poll error: ${err}`);
|
|
145
295
|
this.onPollError(err);
|
|
296
|
+
} finally {
|
|
297
|
+
this.pollInFlight = false;
|
|
146
298
|
}
|
|
147
299
|
}
|
|
148
300
|
|
|
149
|
-
protected onPollSuccess(
|
|
150
|
-
|
|
301
|
+
protected onPollSuccess(agentId: string | undefined): void {
|
|
302
|
+
this.markConnected(agentId);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
protected onPollError(err: unknown): void {
|
|
306
|
+
void err;
|
|
307
|
+
if (this.wsTransport?.connected) return;
|
|
308
|
+
this.markDisconnected();
|
|
309
|
+
}
|
|
151
310
|
|
|
152
311
|
// ── HTTP ──────────────────────────────────────────────
|
|
153
312
|
|
|
154
313
|
private async sendViaHttp(message: AgentMessage): Promise<void> {
|
|
155
314
|
switch (message.type) {
|
|
156
|
-
case "agent.complete": await this.httpRequest("POST", "/complete", { taskId: message.taskId, output: message.output, attachments: message.attachments }); break;
|
|
157
|
-
case "agent.fail": await this.httpRequest("POST", "/fail", { taskId: message.taskId, reason: message.reason }); break;
|
|
158
|
-
case "agent.progress": await this.httpRequest("POST", "/progress", { taskId: message.taskId, message: message.message,
|
|
315
|
+
case "agent.complete": await this.httpRequest("POST", "/complete", { taskId: message.taskId, output: message.output, attachments: message.attachments, workRef: message.workRef }); break;
|
|
316
|
+
case "agent.fail": await this.httpRequest("POST", "/fail", { taskId: message.taskId, reason: message.reason, workRef: message.workRef }); break;
|
|
317
|
+
case "agent.progress": await this.httpRequest("POST", "/progress", { taskId: message.taskId, message: message.message, workRef: message.workRef }); break;
|
|
159
318
|
case "agent.heartbeat": await this.httpRequest("POST", "/heartbeat", {}); break;
|
|
160
|
-
case "agent.chat.reply": await this.httpRequest("POST", "/chat/reply", {
|
|
319
|
+
case "agent.chat.reply": await this.httpRequest("POST", "/chat/reply", {
|
|
320
|
+
channelId: message.channelId,
|
|
321
|
+
content: message.content,
|
|
322
|
+
replyTo: message.replyTo,
|
|
323
|
+
workRefs: message.workRefs,
|
|
324
|
+
}); break;
|
|
161
325
|
case "agent.typing": await this.httpRequest("POST", "/typing", { channelId: message.channelId }); break;
|
|
162
326
|
}
|
|
163
327
|
}
|
|
164
328
|
|
|
165
|
-
protected async httpRequest(method: string, path: string, body: unknown): Promise<
|
|
329
|
+
protected async httpRequest(method: string, path: string, body: unknown): Promise<unknown> {
|
|
166
330
|
const res = await fetch(`${this.httpBase}${path}`, {
|
|
167
331
|
method,
|
|
168
332
|
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${this.options.token}` },
|
|
169
333
|
body: JSON.stringify(body),
|
|
170
334
|
});
|
|
335
|
+
const text = await res.text().catch(() => "");
|
|
171
336
|
if (!res.ok) {
|
|
172
|
-
const text = await res.text().catch(() => "");
|
|
173
337
|
this.onHttpError(res.status, text);
|
|
174
338
|
throw new Error(`HTTP ${res.status}: ${text}`);
|
|
175
339
|
}
|
|
176
|
-
|
|
340
|
+
if (!text) return null;
|
|
341
|
+
try {
|
|
342
|
+
return JSON.parse(text) as unknown;
|
|
343
|
+
} catch {
|
|
344
|
+
throw new Error(`Invalid JSON response from ${path}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
protected onHttpError(status: number, text: string): void {
|
|
349
|
+
void status;
|
|
350
|
+
void text;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private rememberChat(messageId?: string): boolean {
|
|
354
|
+
if (!messageId) return false;
|
|
355
|
+
if (this.recentChatIds.has(messageId)) return false;
|
|
356
|
+
this.recentChatIds.add(messageId);
|
|
357
|
+
if (this.recentChatIds.size > 1000) {
|
|
358
|
+
const oldest = this.recentChatIds.values().next().value;
|
|
359
|
+
if (oldest) this.recentChatIds.delete(oldest);
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private async dispatchTask(task: ServerTask): Promise<void> {
|
|
365
|
+
this.options.log?.info?.(`[clawroom] received task ${task.taskId}: ${task.title}`);
|
|
366
|
+
for (const cb of this.taskCallbacks) cb(task);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
private markConnected(agentId?: string): void {
|
|
370
|
+
if (agentId) this.connectedAgentId = agentId;
|
|
371
|
+
if (!this.connectedAgentId || this.isConnectedValue) return;
|
|
372
|
+
this.isConnectedValue = true;
|
|
373
|
+
for (const callback of this.connectedCallbacks) callback(this.connectedAgentId);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private markDisconnected(): void {
|
|
377
|
+
if (!this.isConnectedValue) return;
|
|
378
|
+
this.isConnectedValue = false;
|
|
379
|
+
for (const callback of this.disconnectedCallbacks) callback();
|
|
177
380
|
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function removeCallback<T>(callbacks: T[], callback: T): void {
|
|
384
|
+
const index = callbacks.indexOf(callback);
|
|
385
|
+
if (index !== -1) callbacks.splice(index, 1);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function getRecord(value: unknown): Record<string, unknown> | null {
|
|
389
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
390
|
+
return value as Record<string, unknown>;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function getString(value: unknown, fallback = ""): string {
|
|
394
|
+
return typeof value === "string" ? value : fallback;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function getOptionalString(value: unknown): string | undefined {
|
|
398
|
+
return typeof value === "string" ? value : undefined;
|
|
399
|
+
}
|
|
178
400
|
|
|
179
|
-
|
|
401
|
+
function getAttachments(value: unknown): ServerChatMessage["attachments"] | undefined {
|
|
402
|
+
return Array.isArray(value) ? value as ServerChatMessage["attachments"] : undefined;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function getContext(value: unknown): ServerChatMessage["context"] {
|
|
406
|
+
return Array.isArray(value) ? value as ServerChatMessage["context"] : [];
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function getWakeReason(value: unknown): ServerChatMessage["wakeReason"] | undefined {
|
|
410
|
+
return typeof value === "string" ? value as ServerChatMessage["wakeReason"] : undefined;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function getAgentChatProfile(value: unknown): AgentChatProfile | null {
|
|
414
|
+
const record = getRecord(value);
|
|
415
|
+
if (!record) return null;
|
|
416
|
+
return {
|
|
417
|
+
role: getString(record.role, DEFAULT_AGENT_CHAT_PROFILE.role),
|
|
418
|
+
systemPrompt: getString(record.systemPrompt, DEFAULT_AGENT_CHAT_PROFILE.systemPrompt),
|
|
419
|
+
memory: getString(record.memory, DEFAULT_AGENT_CHAT_PROFILE.memory),
|
|
420
|
+
continuityPacket: getString(record.continuityPacket, DEFAULT_AGENT_CHAT_PROFILE.continuityPacket),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function resolveAgentChatProfile(...values: unknown[]): AgentChatProfile {
|
|
425
|
+
for (const value of values) {
|
|
426
|
+
const profile = getAgentChatProfile(value);
|
|
427
|
+
if (profile) return profile;
|
|
428
|
+
}
|
|
429
|
+
return DEFAULT_AGENT_CHAT_PROFILE;
|
|
180
430
|
}
|