wtt-connect 0.2.57 → 0.2.58
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/package.json +1 -1
- package/src/wtt-client.js +47 -2
package/package.json
CHANGED
package/src/wtt-client.js
CHANGED
|
@@ -176,13 +176,23 @@ export class WTTClient {
|
|
|
176
176
|
log('info', 'dry-run publish', { topicId, semanticType, chars: content.length });
|
|
177
177
|
return null;
|
|
178
178
|
}
|
|
179
|
-
|
|
179
|
+
const payload = {
|
|
180
180
|
topic_id: topicId,
|
|
181
181
|
content,
|
|
182
182
|
content_type: 'text',
|
|
183
183
|
semantic_type: semanticType,
|
|
184
184
|
...(metadata && typeof metadata === 'object' ? { metadata } : {}),
|
|
185
|
-
}
|
|
185
|
+
};
|
|
186
|
+
try {
|
|
187
|
+
return await this.action('publish', payload, 60000);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
log('warn', 'websocket publish failed; trying HTTP fallback', {
|
|
190
|
+
topicId,
|
|
191
|
+
semanticType,
|
|
192
|
+
error: err?.message || err,
|
|
193
|
+
});
|
|
194
|
+
return await this.httpPublish(payload, 60000);
|
|
195
|
+
}
|
|
186
196
|
}
|
|
187
197
|
|
|
188
198
|
async typing(topicId, state, options = {}) {
|
|
@@ -251,6 +261,41 @@ export class WTTClient {
|
|
|
251
261
|
clearTimeout(timer);
|
|
252
262
|
}
|
|
253
263
|
}
|
|
264
|
+
|
|
265
|
+
async httpPublish(payload, timeoutMs = 60000) {
|
|
266
|
+
const base = String(this.config.wttBaseUrl || '').replace(/\/$/, '');
|
|
267
|
+
if (!base || !this.config.agentId || !this.config.token) throw new Error('missing HTTP publish configuration');
|
|
268
|
+
const controller = new AbortController();
|
|
269
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
270
|
+
try {
|
|
271
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
272
|
+
if (this.config.httpToken) headers.Authorization = `Bearer ${this.config.httpToken}`;
|
|
273
|
+
else headers['X-Agent-Token'] = this.config.token;
|
|
274
|
+
const response = await fetch(
|
|
275
|
+
`${base}/topics/${encodeURIComponent(payload.topic_id)}/messages?agent_id=${encodeURIComponent(this.config.agentId)}`,
|
|
276
|
+
{
|
|
277
|
+
method: 'POST',
|
|
278
|
+
headers,
|
|
279
|
+
body: JSON.stringify({
|
|
280
|
+
sender_id: this.config.agentId,
|
|
281
|
+
sender_type: 'AGENT',
|
|
282
|
+
content: payload.content,
|
|
283
|
+
content_type: payload.content_type || 'text',
|
|
284
|
+
semantic_type: payload.semantic_type || 'CHAT_REPLY',
|
|
285
|
+
...(payload.metadata ? { metadata: payload.metadata } : {}),
|
|
286
|
+
}),
|
|
287
|
+
signal: controller.signal,
|
|
288
|
+
},
|
|
289
|
+
);
|
|
290
|
+
if (!response.ok) {
|
|
291
|
+
const detail = await response.text().catch(() => '');
|
|
292
|
+
throw new Error(`HTTP publish failed: ${response.status}${detail ? ` ${detail.slice(0, 160)}` : ''}`);
|
|
293
|
+
}
|
|
294
|
+
return await response.json().catch(() => ({}));
|
|
295
|
+
} finally {
|
|
296
|
+
clearTimeout(timer);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
254
299
|
}
|
|
255
300
|
|
|
256
301
|
function rid(prefix) {
|