@relaymessenger/openclaw-plugin 0.3.4 → 0.4.0-staging.1
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 +1 -1
- package/README.md +162 -123
- package/contracts/relay-sdk-0.3.0-staging.5.registry.json +69 -0
- package/contracts/relay-v1.lock.json +77 -0
- package/dist/index.js +2 -2
- package/dist/setup-entry.js +1 -2
- package/dist/src/accounts.js +63 -34
- package/dist/src/channel.js +144 -533
- package/dist/src/dispatch.js +257 -0
- package/dist/src/full-sync.js +24 -0
- package/dist/src/gateway.js +171 -0
- package/dist/src/inbound.js +54 -85
- package/dist/src/ingress.js +64 -0
- package/dist/src/outbound.js +48 -111
- package/dist/src/runtime.js +2 -3
- package/dist/src/state.js +492 -0
- package/dist/src/types.js +1 -3
- package/index.ts +1 -2
- package/openclaw.plugin.json +15 -18
- package/package.json +114 -40
- package/setup-entry.ts +0 -2
- package/src/accounts.ts +95 -51
- package/src/channel.ts +271 -646
- package/src/dispatch.ts +324 -0
- package/src/full-sync.ts +47 -0
- package/src/gateway.ts +216 -0
- package/src/inbound.ts +71 -122
- package/src/ingress.ts +123 -0
- package/src/outbound.ts +70 -149
- package/src/runtime.ts +4 -4
- package/src/state.ts +609 -0
- package/src/types.ts +51 -162
- package/dist/src/account-lock.js +0 -91
- package/dist/src/client.js +0 -13
- package/dist/src/cursor-store.js +0 -136
- package/dist/src/inbound-dedupe.js +0 -175
- package/dist/src/invocations.js +0 -47
- package/dist/src/lifecycle.js +0 -35
- package/dist/src/poll-loop.js +0 -137
- package/dist/src/responding.js +0 -36
- package/dist/src/security.js +0 -26
- package/dist/src/state-files.js +0 -243
- package/dist/src/vendor/relay-sdk/client.js +0 -163
- package/dist/src/vendor/relay-sdk/errors.js +0 -45
- package/dist/src/vendor/relay-sdk/types.js +0 -2
- package/dist/src/vendor/relay-sdk/url.js +0 -39
- package/src/account-lock.ts +0 -108
- package/src/client.ts +0 -51
- package/src/cursor-store.ts +0 -186
- package/src/inbound-dedupe.ts +0 -241
- package/src/invocations.ts +0 -58
- package/src/lifecycle.ts +0 -42
- package/src/poll-loop.ts +0 -173
- package/src/responding.ts +0 -52
- package/src/security.ts +0 -36
- package/src/state-files.ts +0 -298
- package/src/vendor/relay-sdk/README.md +0 -28
- package/src/vendor/relay-sdk/client.ts +0 -293
- package/src/vendor/relay-sdk/errors.ts +0 -61
- package/src/vendor/relay-sdk/types.ts +0 -82
- package/src/vendor/relay-sdk/url.ts +0 -43
package/src/channel.ts
CHANGED
|
@@ -1,28 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Read core's part index without requiring it to exist. Cores before
|
|
11
|
-
* 2026.7.2-beta.5 have no `deliveryPartIndex` in their outbound context at all,
|
|
12
|
-
* so naming the field directly would not typecheck against them. Reading it
|
|
13
|
-
* through a widened shape keeps one source compiling on every supported core;
|
|
14
|
-
* `deriveRelayIdempotencyKey` handles the undefined case.
|
|
15
|
-
*/
|
|
16
|
-
function deliveryPartIndexOf(ctx: unknown): number | undefined {
|
|
17
|
-
const index = (ctx as { deliveryPartIndex?: unknown }).deliveryPartIndex;
|
|
18
|
-
return typeof index === "number" ? index : undefined;
|
|
19
|
-
}
|
|
20
|
-
import { resolveStableChannelMessageIngress } from "openclaw/plugin-sdk/channel-ingress-runtime";
|
|
1
|
+
import type {
|
|
2
|
+
MessageSendResponse,
|
|
3
|
+
} from "@relaymessenger/sdk";
|
|
4
|
+
import {
|
|
5
|
+
createChatChannelPlugin,
|
|
6
|
+
type ChannelPlugin,
|
|
7
|
+
type OpenClawConfig,
|
|
8
|
+
} from "openclaw/plugin-sdk/channel-core";
|
|
21
9
|
import {
|
|
22
10
|
createMessageReceiptFromOutboundResults,
|
|
23
11
|
defineChannelMessageAdapter,
|
|
12
|
+
type ChannelMessageUnknownSendContext,
|
|
13
|
+
type ChannelMessageUnknownSendReconciliationResult,
|
|
24
14
|
} from "openclaw/plugin-sdk/channel-outbound";
|
|
25
|
-
import { resolveInboundRouteEnvelopeBuilderWithRuntime } from "openclaw/plugin-sdk/inbound-envelope";
|
|
26
15
|
import { chunkText } from "openclaw/plugin-sdk/reply-chunking";
|
|
27
16
|
import {
|
|
28
17
|
DEFAULT_ACCOUNT_ID,
|
|
@@ -30,31 +19,21 @@ import {
|
|
|
30
19
|
resolveDefaultRelayAccountId,
|
|
31
20
|
resolveRelayAccount,
|
|
32
21
|
} from "./accounts.js";
|
|
33
|
-
import { RelayAccountLock } from "./account-lock.js";
|
|
34
22
|
import {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
RelayApiError,
|
|
39
|
-
} from "./client.js";
|
|
40
|
-
import type { RelayClient } from "./client.js";
|
|
41
|
-
import { createRelayCursorStore, openRelayCursorStateStore } from "./cursor-store.js";
|
|
42
|
-
import { createRelayInboundDedupeGuard, createRelayInboundDeduper } from "./inbound-dedupe.js";
|
|
43
|
-
import { buildRelayInboundFacts } from "./inbound.js";
|
|
44
|
-
import type { RelayInboundFacts } from "./inbound.js";
|
|
45
|
-
import { relayInvocationFor, rememberRelayInvocation } from "./invocations.js";
|
|
46
|
-
import { createRelayAccountLifecycleRegistry } from "./lifecycle.js";
|
|
23
|
+
startRelayAccount,
|
|
24
|
+
stopRelayAccount,
|
|
25
|
+
} from "./gateway.js";
|
|
47
26
|
import {
|
|
27
|
+
classifyUnknownRelaySend,
|
|
28
|
+
createRelaySdkClient,
|
|
48
29
|
deriveRelayIdempotencyKey,
|
|
49
30
|
RELAY_TEXT_CHUNK_LIMIT,
|
|
50
|
-
reconcileRelayUnknownSend,
|
|
51
31
|
sendRelayText,
|
|
52
32
|
} from "./outbound.js";
|
|
53
|
-
import {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
import type { RelayCoreConfig, ResolvedRelayAccount } from "./types.js";
|
|
33
|
+
import type {
|
|
34
|
+
RelayCoreConfig,
|
|
35
|
+
ResolvedRelayAccount,
|
|
36
|
+
} from "./types.js";
|
|
58
37
|
|
|
59
38
|
export const RELAY_CHANNEL_ID = "relay" as const;
|
|
60
39
|
|
|
@@ -62,659 +41,305 @@ const relayMeta = {
|
|
|
62
41
|
id: RELAY_CHANNEL_ID,
|
|
63
42
|
label: "Relay",
|
|
64
43
|
selectionLabel: "Relay",
|
|
65
|
-
detailLabel: "Relay",
|
|
44
|
+
detailLabel: "Relay Messenger",
|
|
66
45
|
docsPath: "https://docs.relayapp.im/integrations/openclaw",
|
|
67
|
-
|
|
46
|
+
docsLabel: "Relay OpenClaw",
|
|
47
|
+
blurb: "Message your OpenClaw through Relay.",
|
|
68
48
|
systemImage: "message",
|
|
69
|
-
// Relay renders plain text plus typed parts; no markdown dialect, so core
|
|
70
|
-
// strips formatting instead of leaking `**`.
|
|
71
49
|
markdownCapable: false,
|
|
50
|
+
order: 70,
|
|
72
51
|
};
|
|
73
52
|
|
|
74
|
-
function
|
|
75
|
-
|
|
53
|
+
function requireAccount(
|
|
54
|
+
cfg: RelayCoreConfig,
|
|
55
|
+
accountId?: string | null | undefined,
|
|
56
|
+
): ResolvedRelayAccount {
|
|
57
|
+
const account = resolveRelayAccount({ cfg, accountId });
|
|
58
|
+
if (!account.configured) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`relay: account "${account.accountId}" has no Relay Agent Token`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return account;
|
|
76
64
|
}
|
|
77
65
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
function receipt(
|
|
67
|
+
messages: readonly MessageSendResponse[],
|
|
68
|
+
replyToId?: string | null | undefined,
|
|
69
|
+
) {
|
|
70
|
+
return createMessageReceiptFromOutboundResults({
|
|
71
|
+
results: messages.map((result) => ({
|
|
72
|
+
channel: RELAY_CHANNEL_ID,
|
|
73
|
+
messageId: result.message.id,
|
|
74
|
+
chatId: result.chat_id,
|
|
75
|
+
conversationId: result.chat_id,
|
|
76
|
+
})),
|
|
77
|
+
...(replyToId ? { replyToId } : {}),
|
|
78
|
+
kind: "text",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
renderedBatchPlan?: { textCount: number; mediaCount: number; payloadCount: number };
|
|
92
|
-
}): string | null {
|
|
93
|
-
if (ctx.payloads.length !== 1) {
|
|
82
|
+
function reconciliationText(
|
|
83
|
+
ctx: ChannelMessageUnknownSendContext,
|
|
84
|
+
): string | null {
|
|
85
|
+
if (ctx.payloads.length !== 1) return null;
|
|
86
|
+
if (
|
|
87
|
+
ctx.renderedBatchPlan &&
|
|
88
|
+
(ctx.renderedBatchPlan.payloadCount !== 1 ||
|
|
89
|
+
ctx.renderedBatchPlan.mediaCount > 0)
|
|
90
|
+
) {
|
|
94
91
|
return null;
|
|
95
92
|
}
|
|
93
|
+
const planned = ctx.renderedBatchPlan?.items[0]?.text;
|
|
96
94
|
const payload = ctx.payloads[0];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
95
|
+
const text = planned ?? payload?.text;
|
|
96
|
+
return typeof text === "string" && text.trim() ? text : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function reconcileRelayUnknownSend(
|
|
100
|
+
ctx: ChannelMessageUnknownSendContext,
|
|
101
|
+
): Promise<ChannelMessageUnknownSendReconciliationResult | null> {
|
|
102
|
+
const text = reconciliationText(ctx);
|
|
103
|
+
if (text === null) return null;
|
|
104
|
+
|
|
105
|
+
const account = requireAccount(
|
|
106
|
+
ctx.cfg as RelayCoreConfig,
|
|
107
|
+
ctx.accountId,
|
|
108
|
+
);
|
|
109
|
+
const relay = createRelaySdkClient(account);
|
|
110
|
+
const responses: MessageSendResponse[] = [];
|
|
111
|
+
const effectiveReplyToId =
|
|
112
|
+
ctx.effectiveReplyToId !== undefined
|
|
113
|
+
? ctx.effectiveReplyToId
|
|
114
|
+
: ctx.replyToId;
|
|
115
|
+
try {
|
|
116
|
+
const chunks = chunkText(text, RELAY_TEXT_CHUNK_LIMIT);
|
|
117
|
+
for (const [index, chunk] of chunks.entries()) {
|
|
118
|
+
responses.push(
|
|
119
|
+
await sendRelayText({
|
|
120
|
+
relay,
|
|
121
|
+
chatId: ctx.to,
|
|
122
|
+
text: chunk,
|
|
123
|
+
replyToId: effectiveReplyToId,
|
|
124
|
+
idempotencyKey: deriveRelayIdempotencyKey({
|
|
125
|
+
deliveryQueueId: ctx.queueId,
|
|
126
|
+
deliveryPartIndex: index,
|
|
127
|
+
}),
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const first = responses[0];
|
|
132
|
+
if (!first) {
|
|
133
|
+
return {
|
|
134
|
+
status: "unresolved",
|
|
135
|
+
error: "relay: reconciliation produced no Message",
|
|
136
|
+
retryable: false,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
status: "sent",
|
|
141
|
+
messageId: first.message.id,
|
|
142
|
+
receipt: receipt(responses, effectiveReplyToId),
|
|
143
|
+
};
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return classifyUnknownRelaySend(error);
|
|
110
146
|
}
|
|
111
|
-
return text;
|
|
112
147
|
}
|
|
113
148
|
|
|
114
|
-
const relayMessageAdapter = defineChannelMessageAdapter({
|
|
149
|
+
export const relayMessageAdapter = defineChannelMessageAdapter({
|
|
115
150
|
id: RELAY_CHANNEL_ID,
|
|
116
151
|
durableFinal: {
|
|
152
|
+
automaticUnknownSendReconciliation: true,
|
|
117
153
|
capabilities: {
|
|
118
154
|
text: true,
|
|
119
155
|
replyTo: true,
|
|
120
|
-
// Plain per-send adapter functions: core's message-sending hooks run
|
|
121
|
-
// around every send, which the default durable requirement derivation
|
|
122
|
-
// demands (capabilities.ts requires it unless explicitly waived).
|
|
123
156
|
messageSendingHooks: true,
|
|
124
157
|
reconcileUnknownSend: true,
|
|
125
158
|
},
|
|
126
|
-
// Only single-part text sends: that is what replaying one idempotency key
|
|
127
|
-
// actually proves (multi-chunk sends have per-part keys and stay with the
|
|
128
|
-
// normal retry path).
|
|
129
159
|
reconcileUnknownSendKinds: { text: true },
|
|
130
|
-
reconcileUnknownSend:
|
|
131
|
-
const account = resolveRelayAccount({
|
|
132
|
-
cfg: ctx.cfg as RelayCoreConfig,
|
|
133
|
-
accountId: ctx.accountId,
|
|
134
|
-
});
|
|
135
|
-
if (!account.configured) {
|
|
136
|
-
return { status: "unresolved", error: "relay account not configured", retryable: false };
|
|
137
|
-
}
|
|
138
|
-
const text = resolveSingleReconcilableText(ctx);
|
|
139
|
-
if (text === null) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
const invocationId = relayInvocationFor({
|
|
143
|
-
accountId: account.accountId,
|
|
144
|
-
conversationId: ctx.to,
|
|
145
|
-
});
|
|
146
|
-
const verdict = await reconcileRelayUnknownSend({
|
|
147
|
-
client: relayClientForAccount(account),
|
|
148
|
-
conversationId: ctx.to,
|
|
149
|
-
text,
|
|
150
|
-
replyToId: ctx.effectiveReplyToId ?? ctx.replyToId ?? null,
|
|
151
|
-
...(invocationId ? { invocationId } : {}),
|
|
152
|
-
idempotencyKey: deriveRelayIdempotencyKey({ deliveryQueueId: ctx.queueId }),
|
|
153
|
-
});
|
|
154
|
-
if (verdict.status === "sent") {
|
|
155
|
-
return {
|
|
156
|
-
status: "sent",
|
|
157
|
-
messageId: verdict.messageId,
|
|
158
|
-
// The 202 is an array: name every message the send committed.
|
|
159
|
-
receipt: createMessageReceiptFromOutboundResults({
|
|
160
|
-
results: verdict.messages.map((message) => ({
|
|
161
|
-
channel: RELAY_CHANNEL_ID,
|
|
162
|
-
messageId: message.id,
|
|
163
|
-
})),
|
|
164
|
-
kind: "text",
|
|
165
|
-
}),
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
return verdict;
|
|
169
|
-
},
|
|
160
|
+
reconcileUnknownSend: reconcileRelayUnknownSend,
|
|
170
161
|
},
|
|
171
162
|
send: {
|
|
172
163
|
text: async (ctx) => {
|
|
173
|
-
const account =
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// A group reply must name the invocation it answers. Core's send context
|
|
181
|
-
// has no field for it, so the turn parks it in the invocation registry
|
|
182
|
-
// under (accountId, conversationId) and it is read back here.
|
|
183
|
-
const invocationId = relayInvocationFor({
|
|
184
|
-
accountId: account.accountId,
|
|
185
|
-
conversationId: ctx.to,
|
|
186
|
-
});
|
|
187
|
-
const result = await sendRelayText({
|
|
188
|
-
client: relayClientForAccount(account),
|
|
189
|
-
conversationId: ctx.to,
|
|
164
|
+
const account = requireAccount(
|
|
165
|
+
ctx.cfg as RelayCoreConfig,
|
|
166
|
+
ctx.accountId,
|
|
167
|
+
);
|
|
168
|
+
const response = await sendRelayText({
|
|
169
|
+
relay: createRelaySdkClient(account),
|
|
170
|
+
chatId: ctx.to,
|
|
190
171
|
text: ctx.text,
|
|
191
|
-
replyToId: ctx.replyToId
|
|
192
|
-
...(invocationId ? { invocationId } : {}),
|
|
193
|
-
// Stable per (queueId, part): internal retries replay the same key,
|
|
194
|
-
// so the server-side idempotent commit makes duplicates impossible by
|
|
195
|
-
// contract. On a core with no part index the text names the part.
|
|
172
|
+
replyToId: ctx.replyToId,
|
|
196
173
|
idempotencyKey: deriveRelayIdempotencyKey({
|
|
197
174
|
deliveryQueueId: ctx.deliveryQueueId,
|
|
198
|
-
deliveryPartIndex:
|
|
199
|
-
partText: ctx.text,
|
|
175
|
+
deliveryPartIndex: ctx.deliveryPartIndex,
|
|
200
176
|
}),
|
|
201
177
|
...(ctx.signal ? { signal: ctx.signal } : {}),
|
|
178
|
+
...(ctx.onPlatformSendDispatch
|
|
179
|
+
? { onPlatformSendDispatch: ctx.onPlatformSendDispatch }
|
|
180
|
+
: {}),
|
|
202
181
|
});
|
|
203
182
|
return {
|
|
204
|
-
messageId:
|
|
205
|
-
|
|
206
|
-
receipt: createMessageReceiptFromOutboundResults({
|
|
207
|
-
results: result.messages.map((message) => ({
|
|
208
|
-
channel: RELAY_CHANNEL_ID,
|
|
209
|
-
messageId: message.id,
|
|
210
|
-
})),
|
|
211
|
-
replyToId: ctx.replyToId ?? undefined,
|
|
212
|
-
kind: "text",
|
|
213
|
-
}),
|
|
183
|
+
messageId: response.message.id,
|
|
184
|
+
receipt: receipt([response], ctx.replyToId),
|
|
214
185
|
};
|
|
215
186
|
},
|
|
216
187
|
},
|
|
217
|
-
receive: {
|
|
218
|
-
// Cursor acks after a durable at-most-once attempt marker is written.
|
|
219
|
-
defaultAckPolicy: "after_agent_dispatch",
|
|
220
|
-
supportedAckPolicies: ["after_receive_record", "after_agent_dispatch"],
|
|
221
|
-
},
|
|
222
188
|
});
|
|
223
189
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
channel: relayMeta.label,
|
|
267
|
-
from: facts.senderId,
|
|
268
|
-
...(facts.timestamp ? { timestamp: facts.timestamp } : {}),
|
|
269
|
-
body: facts.text,
|
|
270
|
-
});
|
|
271
|
-
const ctxPayload = runtime.channel.reply.finalizeInboundContext({
|
|
272
|
-
Body: body,
|
|
273
|
-
BodyForAgent: facts.text,
|
|
274
|
-
RawBody: facts.text,
|
|
275
|
-
CommandBody: facts.text,
|
|
276
|
-
From: facts.conversationId,
|
|
277
|
-
To: facts.conversationId,
|
|
278
|
-
SessionKey: route.sessionKey,
|
|
279
|
-
AccountId: route.accountId ?? account.accountId,
|
|
280
|
-
ChatType: "direct",
|
|
281
|
-
ConversationLabel: facts.conversationId,
|
|
282
|
-
SenderId: facts.senderId,
|
|
283
|
-
SenderName: facts.senderId,
|
|
284
|
-
Provider: RELAY_CHANNEL_ID,
|
|
285
|
-
Surface: RELAY_CHANNEL_ID,
|
|
286
|
-
MessageSid: facts.messageId,
|
|
287
|
-
MessageSidFull: facts.messageId,
|
|
288
|
-
...(facts.replyToId ? { ReplyToId: facts.replyToId } : {}),
|
|
289
|
-
...(facts.timestamp ? { Timestamp: facts.timestamp } : {}),
|
|
290
|
-
OriginatingChannel: RELAY_CHANNEL_ID,
|
|
291
|
-
OriginatingTo: facts.conversationId,
|
|
292
|
-
CommandAuthorized: commandAuthorized,
|
|
293
|
-
});
|
|
294
|
-
// A consumed inbound message with a silently lost reply is the worst
|
|
295
|
-
// outcome. Delivery failures are surfaced, but the inbound attempt marker
|
|
296
|
-
// prevents replaying an agent turn whose tools may already have run.
|
|
297
|
-
let deliveryError: unknown;
|
|
298
|
-
let fallbackDeliveryIndex = 0;
|
|
299
|
-
const recordDeliveryError = (error: unknown) => {
|
|
300
|
-
deliveryError ??= error;
|
|
301
|
-
};
|
|
302
|
-
// Park the group invocation for the life of the turn. Core's durable send
|
|
303
|
-
// adapter is a separate entry point with no inbound context, so this is how
|
|
304
|
-
// the reply learns which invocation it answers.
|
|
305
|
-
const releaseInvocation = facts.invocationId
|
|
306
|
-
? rememberRelayInvocation({
|
|
307
|
-
accountId: account.accountId,
|
|
308
|
-
conversationId: facts.conversationId,
|
|
309
|
-
invocationId: facts.invocationId,
|
|
310
|
-
})
|
|
311
|
-
: () => {};
|
|
312
|
-
try {
|
|
313
|
-
// Admission, runtime resolution, route/session lookup, envelope building,
|
|
314
|
-
// and context finalization above are replay-safe. The durable attempt starts
|
|
315
|
-
// immediately before OpenClaw can invoke the agent or its tools.
|
|
316
|
-
await markRespondingBeforeAttempt({
|
|
317
|
-
client: params.client,
|
|
318
|
-
facts,
|
|
319
|
-
label: "OpenClaw",
|
|
320
|
-
markAttempt: params.markAttempt,
|
|
321
|
-
...(params.warn ? { onReceiptFailure: params.warn } : {}),
|
|
322
|
-
});
|
|
323
|
-
await runtime.channel.inbound.dispatchReply({
|
|
324
|
-
cfg: params.cfg,
|
|
325
|
-
channel: RELAY_CHANNEL_ID,
|
|
326
|
-
accountId: account.accountId,
|
|
327
|
-
agentId: route.agentId,
|
|
328
|
-
routeSessionKey: route.sessionKey,
|
|
329
|
-
storePath,
|
|
330
|
-
ctxPayload,
|
|
331
|
-
recordInboundSession: runtime.channel.session.recordInboundSession,
|
|
332
|
-
dispatchReplyWithBufferedBlockDispatcher:
|
|
333
|
-
runtime.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
|
|
334
|
-
delivery: {
|
|
335
|
-
// Final replies go through the durable message adapter: core renders
|
|
336
|
-
// and chunks them (chunker + textChunkLimit) and tracks the send as a
|
|
337
|
-
// durable queue intent. Requiring reconcileUnknownSend forces
|
|
338
|
-
// `durability: "required"`, so single-payload finals carry a stable
|
|
339
|
-
// deliveryQueueId into send.text (stable idempotency key + exact
|
|
340
|
-
// replay), and multi-chunk finals get core's queue-level crash
|
|
341
|
-
// recovery. Replies land as plain messages, not quotes
|
|
342
|
-
// (`replyToId: null`).
|
|
343
|
-
durable: {
|
|
344
|
-
to: facts.conversationId,
|
|
345
|
-
replyToId: null,
|
|
346
|
-
requiredCapabilities: { reconcileUnknownSend: true },
|
|
347
|
-
},
|
|
348
|
-
// Fallback for payloads the durable path does not carry (non-final
|
|
349
|
-
// visible blocks). The event id + block/chunk ordinals identify each
|
|
350
|
-
// logical send: retries reuse it while identical intentional blocks and
|
|
351
|
-
// chunks remain distinct.
|
|
352
|
-
deliver: async (payload) => {
|
|
353
|
-
const text =
|
|
354
|
-
payload && typeof payload === "object" && "text" in payload
|
|
355
|
-
? ((payload as { text?: string }).text ?? "")
|
|
356
|
-
: "";
|
|
357
|
-
if (!text.trim()) {
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
const logicalBlockId = `${facts.eventId}:block:${fallbackDeliveryIndex}`;
|
|
361
|
-
fallbackDeliveryIndex += 1;
|
|
362
|
-
try {
|
|
363
|
-
let chunkIndex = 0;
|
|
364
|
-
for (const chunk of chunkText(text, RELAY_TEXT_CHUNK_LIMIT)) {
|
|
365
|
-
await sendRelayText({
|
|
366
|
-
client: params.client,
|
|
367
|
-
conversationId: facts.conversationId,
|
|
368
|
-
text: chunk,
|
|
369
|
-
...(facts.invocationId ? { invocationId: facts.invocationId } : {}),
|
|
370
|
-
idempotencyKey: deriveRelayIdempotencyKey({
|
|
371
|
-
deliveryQueueId: logicalBlockId,
|
|
372
|
-
deliveryPartIndex: chunkIndex,
|
|
373
|
-
}),
|
|
374
|
-
});
|
|
375
|
-
chunkIndex += 1;
|
|
376
|
-
}
|
|
377
|
-
} catch (error) {
|
|
378
|
-
recordDeliveryError(error);
|
|
379
|
-
throw error;
|
|
380
|
-
}
|
|
190
|
+
export const relayChannelPlugin: ChannelPlugin<ResolvedRelayAccount> =
|
|
191
|
+
createChatChannelPlugin({
|
|
192
|
+
base: {
|
|
193
|
+
id: RELAY_CHANNEL_ID,
|
|
194
|
+
meta: relayMeta,
|
|
195
|
+
capabilities: {
|
|
196
|
+
chatTypes: ["direct", "group"],
|
|
197
|
+
reply: true,
|
|
198
|
+
threads: false,
|
|
199
|
+
media: false,
|
|
200
|
+
reactions: false,
|
|
201
|
+
edit: false,
|
|
202
|
+
unsend: false,
|
|
203
|
+
effects: false,
|
|
204
|
+
blockStreaming: false,
|
|
205
|
+
},
|
|
206
|
+
reload: { configPrefixes: ["channels.relay"] },
|
|
207
|
+
setup: {
|
|
208
|
+
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
209
|
+
const core = cfg as RelayCoreConfig;
|
|
210
|
+
const section = { ...core.channels?.relay };
|
|
211
|
+
const patch = input as Record<string, unknown>;
|
|
212
|
+
const relay =
|
|
213
|
+
!accountId || accountId === DEFAULT_ACCOUNT_ID
|
|
214
|
+
? { ...section, ...patch }
|
|
215
|
+
: {
|
|
216
|
+
...section,
|
|
217
|
+
accounts: {
|
|
218
|
+
...section.accounts,
|
|
219
|
+
[accountId]: {
|
|
220
|
+
...section.accounts?.[accountId],
|
|
221
|
+
...patch,
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
return {
|
|
226
|
+
...cfg,
|
|
227
|
+
channels: {
|
|
228
|
+
...core.channels,
|
|
229
|
+
relay,
|
|
230
|
+
},
|
|
231
|
+
} as OpenClawConfig;
|
|
381
232
|
},
|
|
382
|
-
onError: recordDeliveryError,
|
|
383
233
|
},
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
const log = (line: string) => ctx.log?.info?.(line);
|
|
420
|
-
const warn = (line: string) => ctx.log?.warn?.(line);
|
|
421
|
-
const client = relayClientForAccount(account);
|
|
422
|
-
const lifecycle = relayAccountLifecycles.acquire(account.accountId, ctx.abortSignal);
|
|
423
|
-
const abortSignal = lifecycle.signal;
|
|
424
|
-
let agentKey: string | undefined;
|
|
425
|
-
let accountLock: RelayAccountLock | undefined;
|
|
426
|
-
|
|
427
|
-
const markTerminalDisconnect = (error: Error) => {
|
|
428
|
-
// Operator action required: flag terminalDisconnect so the supervisor
|
|
429
|
-
// does not auto-restart (server-channels.ts:718).
|
|
430
|
-
ctx.setStatus({
|
|
431
|
-
accountId: account.accountId,
|
|
432
|
-
running: false,
|
|
433
|
-
connected: false,
|
|
434
|
-
terminalDisconnect: true,
|
|
435
|
-
lastError: error.message,
|
|
436
|
-
});
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
try {
|
|
440
|
-
const me = await client.getMe({ signal: abortSignal });
|
|
441
|
-
const allowedSenderIds = resolveRelayAllowedSenderIds({
|
|
442
|
-
profile: me,
|
|
443
|
-
allowFrom: account.config.allowFrom,
|
|
444
|
-
});
|
|
445
|
-
if (allowedSenderIds.length === 0) {
|
|
446
|
-
const error = new Error(
|
|
447
|
-
`relay: account "${account.accountId}" has no owner pin. ` +
|
|
448
|
-
"The Relay API did not return owner_user_id and channels.relay.allowFrom is empty.",
|
|
449
|
-
);
|
|
450
|
-
markTerminalDisconnect(error);
|
|
451
|
-
throw error;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
// Two accounts configured with the same token would fight over the
|
|
455
|
-
// server's single consumer slot forever; keep the second one down until
|
|
456
|
-
// the operator fixes the config. account.baseUrl is already canonical.
|
|
457
|
-
agentKey = relayAgentAccountKey(account.baseUrl, me.id);
|
|
458
|
-
const owner = runningRelayAgentAccounts.get(agentKey);
|
|
459
|
-
if (owner !== undefined) {
|
|
460
|
-
const error = new Error(
|
|
461
|
-
`relay: agent ${me.id} is already polled by account "${owner}"; account "${account.accountId}" appears to reuse the same Agent Token. Give each account its own token.`,
|
|
462
|
-
);
|
|
463
|
-
markTerminalDisconnect(error);
|
|
464
|
-
throw error;
|
|
465
|
-
}
|
|
466
|
-
accountLock = new RelayAccountLock(account.baseUrl, me.id, account.accountId);
|
|
467
|
-
try {
|
|
468
|
-
accountLock.acquire();
|
|
469
|
-
} catch (error) {
|
|
470
|
-
const lockError = error instanceof Error ? error : new Error(String(error));
|
|
471
|
-
markTerminalDisconnect(lockError);
|
|
472
|
-
throw lockError;
|
|
473
|
-
}
|
|
474
|
-
runningRelayAgentAccounts.set(agentKey, account.accountId);
|
|
475
|
-
|
|
476
|
-
ctx.setStatus({
|
|
477
|
-
accountId: account.accountId,
|
|
478
|
-
running: true,
|
|
479
|
-
connected: true,
|
|
480
|
-
configured: true,
|
|
481
|
-
enabled: account.enabled,
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
const cursorStore = createRelayCursorStore({
|
|
485
|
-
store: openRelayCursorStateStore(warn),
|
|
486
|
-
baseUrl: account.baseUrl,
|
|
487
|
-
agentId: me.id,
|
|
488
|
-
onPersistError: (error) => warn(`[relay] cursor persistence failed: ${String(error)}`),
|
|
489
|
-
});
|
|
490
|
-
await cursorStore.load();
|
|
491
|
-
const deduper = createRelayInboundDeduper({
|
|
492
|
-
guard: createRelayInboundDedupeGuard({
|
|
493
|
-
onDiskError: (error) => warn(`[relay] inbound dedupe persistence failed: ${String(error)}`),
|
|
494
|
-
}),
|
|
495
|
-
baseUrl: account.baseUrl,
|
|
496
|
-
agentId: me.id,
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
await runRelayPollLoop({
|
|
500
|
-
client,
|
|
501
|
-
cursorStore,
|
|
502
|
-
deduper,
|
|
503
|
-
abortSignal,
|
|
504
|
-
timeoutSeconds: account.pollTimeoutSeconds,
|
|
505
|
-
limit: 100,
|
|
506
|
-
log,
|
|
507
|
-
// Receipts, reactions, and echoes are acked without a dedupe row or a
|
|
508
|
-
// dispatch: reaction.* is observe-only at v1, delivered/read
|
|
509
|
-
// are bookkeeping.
|
|
510
|
-
shouldProcess: (event) => buildRelayInboundFacts(event, { agentId: me.id }) !== null,
|
|
511
|
-
onBatch: () => {
|
|
512
|
-
ctx.setStatus({
|
|
513
|
-
accountId: account.accountId,
|
|
514
|
-
running: true,
|
|
515
|
-
connected: true,
|
|
516
|
-
lastInboundAt: Date.now(),
|
|
517
|
-
});
|
|
234
|
+
config: {
|
|
235
|
+
listAccountIds: (cfg) =>
|
|
236
|
+
listRelayAccountIds(cfg as RelayCoreConfig),
|
|
237
|
+
resolveAccount: (cfg, accountId) =>
|
|
238
|
+
resolveRelayAccount({
|
|
239
|
+
cfg: cfg as RelayCoreConfig,
|
|
240
|
+
accountId,
|
|
241
|
+
}),
|
|
242
|
+
defaultAccountId: (cfg) =>
|
|
243
|
+
resolveDefaultRelayAccountId(cfg as RelayCoreConfig),
|
|
244
|
+
isConfigured: (account) => account.configured,
|
|
245
|
+
inspectAccount: (cfg, accountId) => {
|
|
246
|
+
const account = resolveRelayAccount({
|
|
247
|
+
cfg: cfg as RelayCoreConfig,
|
|
248
|
+
accountId,
|
|
249
|
+
});
|
|
250
|
+
return {
|
|
251
|
+
enabled: account.enabled,
|
|
252
|
+
configured: account.configured,
|
|
253
|
+
tokenStatus: account.configured ? "available" : "missing",
|
|
254
|
+
baseUrl: account.baseUrl,
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
resolveAllowFrom: ({ cfg, accountId }) =>
|
|
258
|
+
(() => {
|
|
259
|
+
const account = resolveRelayAccount({
|
|
260
|
+
cfg: cfg as RelayCoreConfig,
|
|
261
|
+
accountId,
|
|
262
|
+
});
|
|
263
|
+
return account.allowFrom.length > 0
|
|
264
|
+
? account.allowFrom
|
|
265
|
+
: ["*"];
|
|
266
|
+
})(),
|
|
518
267
|
},
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
});
|
|
268
|
+
messaging: {
|
|
269
|
+
targetPrefixes: ["relay"],
|
|
270
|
+
normalizeTarget: (target) => {
|
|
271
|
+
const normalized = target.trim().replace(/^relay:/iu, "");
|
|
272
|
+
return normalized || undefined;
|
|
273
|
+
},
|
|
274
|
+
targetResolver: {
|
|
275
|
+
looksLikeId: (raw) =>
|
|
276
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu.test(
|
|
277
|
+
raw.trim().replace(/^relay:/iu, ""),
|
|
278
|
+
),
|
|
279
|
+
hint: "<Relay Chat ID>",
|
|
280
|
+
},
|
|
533
281
|
},
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
return;
|
|
538
|
-
}
|
|
539
|
-
// Named as `kind === "auth"`, not `error.terminal`. The SDK client counts
|
|
540
|
-
// every non-retryable kind as terminal, which would swallow the 409 cases
|
|
541
|
-
// below — including `terminated_by_other_consumer`, whose whole point is
|
|
542
|
-
// to fall through to the supervisor's restart arbitration.
|
|
543
|
-
if (error instanceof RelayApiError && error.kind === "auth") {
|
|
544
|
-
markTerminalDisconnect(error);
|
|
545
|
-
} else if (isRelayWebhookConflict(error)) {
|
|
546
|
-
// Webhook XOR: long polling stays 409 until the operator
|
|
547
|
-
// disables the webhook endpoint — restarting cannot fix it.
|
|
548
|
-
// `terminated_by_other_consumer` intentionally falls through to the
|
|
549
|
-
// supervisor's normal restart/backoff arbitration.
|
|
550
|
-
markTerminalDisconnect(error);
|
|
551
|
-
}
|
|
552
|
-
throw error;
|
|
553
|
-
} finally {
|
|
554
|
-
if (agentKey && runningRelayAgentAccounts.get(agentKey) === account.accountId) {
|
|
555
|
-
runningRelayAgentAccounts.delete(agentKey);
|
|
556
|
-
}
|
|
557
|
-
accountLock?.release();
|
|
558
|
-
lifecycle.release();
|
|
559
|
-
ctx.setStatus({
|
|
560
|
-
accountId: account.accountId,
|
|
561
|
-
running: false,
|
|
562
|
-
connected: false,
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
async function stopRelayAccount(
|
|
568
|
-
ctx: ChannelGatewayContext<ResolvedRelayAccount>,
|
|
569
|
-
): Promise<void> {
|
|
570
|
-
relayAccountLifecycles.stop(ctx.accountId);
|
|
571
|
-
ctx.setStatus({
|
|
572
|
-
accountId: ctx.accountId,
|
|
573
|
-
running: false,
|
|
574
|
-
connected: false,
|
|
575
|
-
});
|
|
576
|
-
ctx.log?.info?.(`[relay] stopped account "${ctx.accountId}"`);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// ---------------------------------------------------------------------------
|
|
580
|
-
// Plugin object.
|
|
581
|
-
// ---------------------------------------------------------------------------
|
|
582
|
-
|
|
583
|
-
export const relayChannelPlugin: ChannelPlugin<ResolvedRelayAccount> = createChatChannelPlugin({
|
|
584
|
-
base: {
|
|
585
|
-
id: RELAY_CHANNEL_ID,
|
|
586
|
-
meta: relayMeta,
|
|
587
|
-
capabilities: {
|
|
588
|
-
// v1: direct conversations only; media flips on when the agent
|
|
589
|
-
// attachment path ships. Reactions are observe-only.
|
|
590
|
-
chatTypes: ["direct"],
|
|
591
|
-
reply: true,
|
|
592
|
-
threads: false,
|
|
593
|
-
media: false,
|
|
594
|
-
reactions: false,
|
|
595
|
-
},
|
|
596
|
-
reload: { configPrefixes: ["channels.relay"] },
|
|
597
|
-
setup: {
|
|
598
|
-
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
599
|
-
const coreCfg = cfg as RelayCoreConfig;
|
|
600
|
-
const channelSection = { ...coreCfg.channels?.relay };
|
|
601
|
-
const patch = input as Record<string, unknown>;
|
|
602
|
-
const next =
|
|
603
|
-
!accountId || accountId === DEFAULT_ACCOUNT_ID
|
|
604
|
-
? { ...channelSection, ...patch }
|
|
605
|
-
: {
|
|
606
|
-
...channelSection,
|
|
607
|
-
accounts: {
|
|
608
|
-
...channelSection.accounts,
|
|
609
|
-
[accountId]: {
|
|
610
|
-
...channelSection.accounts?.[accountId],
|
|
611
|
-
...patch,
|
|
612
|
-
},
|
|
613
|
-
},
|
|
614
|
-
};
|
|
615
|
-
return {
|
|
616
|
-
...cfg,
|
|
617
|
-
channels: {
|
|
618
|
-
...coreCfg.channels,
|
|
619
|
-
relay: next,
|
|
620
|
-
},
|
|
621
|
-
} as OpenClawConfig;
|
|
282
|
+
gateway: {
|
|
283
|
+
startAccount: startRelayAccount,
|
|
284
|
+
stopAccount: stopRelayAccount,
|
|
622
285
|
},
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
}
|
|
286
|
+
heartbeat: {
|
|
287
|
+
sendTyping: async ({ cfg, to, accountId }) => {
|
|
288
|
+
const account = requireAccount(
|
|
289
|
+
cfg as RelayCoreConfig,
|
|
290
|
+
accountId,
|
|
291
|
+
);
|
|
292
|
+
await createRelaySdkClient(account).chats.startTyping(to);
|
|
293
|
+
},
|
|
294
|
+
clearTyping: async ({ cfg, to, accountId }) => {
|
|
295
|
+
const account = requireAccount(
|
|
296
|
+
cfg as RelayCoreConfig,
|
|
297
|
+
accountId,
|
|
298
|
+
);
|
|
299
|
+
await createRelaySdkClient(account).chats.stopTyping(to);
|
|
300
|
+
},
|
|
638
301
|
},
|
|
639
|
-
|
|
640
|
-
resolveRelayAllowedSenderIds({
|
|
641
|
-
profile: {},
|
|
642
|
-
allowFrom: resolveRelayAccount({ cfg: cfg as RelayCoreConfig, accountId }).config
|
|
643
|
-
.allowFrom,
|
|
644
|
-
}),
|
|
302
|
+
message: relayMessageAdapter,
|
|
645
303
|
},
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
304
|
+
security: {
|
|
305
|
+
dm: {
|
|
306
|
+
channelKey: RELAY_CHANNEL_ID,
|
|
307
|
+
resolvePolicy: (account) =>
|
|
308
|
+
account.allowFrom.length > 0 ? "allowlist" : "open",
|
|
309
|
+
resolveAllowFrom: (account) =>
|
|
310
|
+
account.allowFrom.length > 0 ? account.allowFrom : ["*"],
|
|
311
|
+
defaultPolicy: "open",
|
|
650
312
|
},
|
|
651
313
|
},
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
sendTyping: async ({ cfg, to, accountId }) => {
|
|
659
|
-
const account = resolveRelayAccount({ cfg: cfg as RelayCoreConfig, accountId });
|
|
660
|
-
if (!account.configured) {
|
|
661
|
-
return;
|
|
662
|
-
}
|
|
663
|
-
await relayClientForAccount(account).setTyping({ conversationId: to, started: true });
|
|
314
|
+
outbound: {
|
|
315
|
+
base: {
|
|
316
|
+
deliveryMode: "direct",
|
|
317
|
+
chunker: (text, limit) => chunkText(text, limit),
|
|
318
|
+
chunkerMode: "text",
|
|
319
|
+
textChunkLimit: RELAY_TEXT_CHUNK_LIMIT,
|
|
664
320
|
},
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
// Core's renderer splits long replies before the adapter sees them
|
|
688
|
-
// without a chunker the plan falls back to one oversized
|
|
689
|
-
// unit, which the server 422s at its 8 KiB per-part cap.
|
|
690
|
-
chunker: (text, limit) => chunkText(text, limit),
|
|
691
|
-
chunkerMode: "text",
|
|
692
|
-
textChunkLimit: RELAY_TEXT_CHUNK_LIMIT,
|
|
693
|
-
},
|
|
694
|
-
attachedResults: {
|
|
695
|
-
channel: RELAY_CHANNEL_ID,
|
|
696
|
-
sendText: async (ctx) => {
|
|
697
|
-
const { cfg, to, text, accountId, replyToId } = ctx;
|
|
698
|
-
const account = resolveRelayAccount({ cfg: cfg as RelayCoreConfig, accountId });
|
|
699
|
-
if (!account.configured) {
|
|
700
|
-
throw new Error(`relay: account "${account.accountId}" has no Agent Token configured`);
|
|
701
|
-
}
|
|
702
|
-
const normalizedReplyToId = replyToId == null ? null : String(replyToId);
|
|
703
|
-
const result = await sendRelayText({
|
|
704
|
-
client: relayClientForAccount(account),
|
|
705
|
-
conversationId: to,
|
|
706
|
-
text,
|
|
707
|
-
replyToId: normalizedReplyToId,
|
|
708
|
-
// Stable when core supplies a logical queue id; otherwise fresh for
|
|
709
|
-
// this invocation so two intentional identical sends remain two.
|
|
710
|
-
idempotencyKey: deriveRelayIdempotencyKey({
|
|
711
|
-
deliveryQueueId: ctx.deliveryQueueId,
|
|
712
|
-
deliveryPartIndex: deliveryPartIndexOf(ctx),
|
|
713
|
-
partText: text,
|
|
714
|
-
}),
|
|
715
|
-
});
|
|
716
|
-
return { messageId: result.messageId };
|
|
321
|
+
attachedResults: {
|
|
322
|
+
channel: RELAY_CHANNEL_ID,
|
|
323
|
+
sendText: async (ctx) => {
|
|
324
|
+
const account = requireAccount(
|
|
325
|
+
ctx.cfg as RelayCoreConfig,
|
|
326
|
+
ctx.accountId,
|
|
327
|
+
);
|
|
328
|
+
const response = await sendRelayText({
|
|
329
|
+
relay: createRelaySdkClient(account),
|
|
330
|
+
chatId: ctx.to,
|
|
331
|
+
text: ctx.text,
|
|
332
|
+
replyToId: ctx.replyToId,
|
|
333
|
+
idempotencyKey: deriveRelayIdempotencyKey({
|
|
334
|
+
deliveryQueueId: ctx.deliveryQueueId,
|
|
335
|
+
deliveryPartIndex: ctx.deliveryPartIndex,
|
|
336
|
+
}),
|
|
337
|
+
...(ctx.onPlatformSendDispatch
|
|
338
|
+
? { onPlatformSendDispatch: ctx.onPlatformSendDispatch }
|
|
339
|
+
: {}),
|
|
340
|
+
});
|
|
341
|
+
return { messageId: response.message.id };
|
|
342
|
+
},
|
|
717
343
|
},
|
|
718
344
|
},
|
|
719
|
-
}
|
|
720
|
-
});
|
|
345
|
+
});
|