dooers-agents-client 0.4.0 → 0.6.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.
package/dist/main.cjs CHANGED
@@ -6,7 +6,23 @@ var shallow = require('zustand/shallow');
6
6
  var vanilla = require('zustand/vanilla');
7
7
  var jsxRuntime = require('react/jsx-runtime');
8
8
 
9
- // src/hooks/use-analytics.ts
9
+ // src/helpers/api-messages-url-to-ws.ts
10
+ function apiMessagesUrlToWebSocketUrl(url) {
11
+ const t = url.trim();
12
+ if (!t) {
13
+ throw new Error("API Messages URL is empty");
14
+ }
15
+ if (/^wss:\/\//i.test(t) || /^ws:\/\//i.test(t)) {
16
+ return t;
17
+ }
18
+ if (/^https:\/\//i.test(t)) {
19
+ return `wss://${t.slice("https://".length)}`;
20
+ }
21
+ if (/^http:\/\//i.test(t)) {
22
+ return `ws://${t.slice("http://".length)}`;
23
+ }
24
+ throw new Error("API Messages URL must start with http://, https://, ws://, or wss://");
25
+ }
10
26
 
11
27
  // src/types.ts
12
28
  function isSettingsFieldGroup(item) {
@@ -179,6 +195,8 @@ function toSettingsField(w) {
179
195
  label: w.label,
180
196
  required: w.required,
181
197
  readonly: w.readonly,
198
+ user_editable: w.user_editable ?? true,
199
+ visibility: w.visibility ?? "user",
182
200
  value: w.value,
183
201
  placeholder: w.placeholder,
184
202
  options: w.options,
@@ -199,7 +217,8 @@ function toSettingsItem(w) {
199
217
  id: g.id,
200
218
  label: g.label,
201
219
  fields: g.fields.map(toSettingsField),
202
- collapsible: g.collapsible
220
+ collapsible: g.collapsible,
221
+ visibility: g.visibility ?? "user"
203
222
  };
204
223
  }
205
224
  return toSettingsField(w);
@@ -233,6 +252,92 @@ function toWireContentPart(p) {
233
252
  var MAX_RECONNECT_ATTEMPTS = 5;
234
253
  var RECONNECT_DELAYS = [1e3, 2e3, 4e3, 8e3, 16e3];
235
254
  var SEND_MESSAGE_TIMEOUT = 3e4;
255
+ var AgentServerClient = class {
256
+ wsUrl;
257
+ constructor(apiMessagesUrl) {
258
+ this.wsUrl = apiMessagesUrlToWebSocketUrl(apiMessagesUrl);
259
+ }
260
+ /**
261
+ * Opens a short-lived WebSocket, requests `settings.public_schema`, and returns the public field
262
+ * list. Closes the socket when done.
263
+ */
264
+ fetchPublicSettingsSchema(options) {
265
+ const timeoutMs = options?.timeoutMs ?? 15e3;
266
+ return new Promise((resolve, reject) => {
267
+ const ws = new WebSocket(this.wsUrl);
268
+ const frameId = crypto.randomUUID();
269
+ let settled = false;
270
+ const finish = (fn) => {
271
+ if (settled) return;
272
+ settled = true;
273
+ clearTimeout(timer);
274
+ try {
275
+ ws.close();
276
+ } catch {
277
+ }
278
+ fn();
279
+ };
280
+ const timer = setTimeout(() => {
281
+ finish(() => reject(new Error("AgentServerClient.fetchPublicSettingsSchema: timeout")));
282
+ }, timeoutMs);
283
+ ws.onopen = () => {
284
+ ws.send(
285
+ JSON.stringify({
286
+ id: frameId,
287
+ type: "settings.public_schema",
288
+ payload: {}
289
+ })
290
+ );
291
+ };
292
+ ws.onmessage = (ev) => {
293
+ let msg;
294
+ try {
295
+ msg = JSON.parse(ev.data);
296
+ } catch {
297
+ return;
298
+ }
299
+ if (msg.type === "ack" && msg.payload?.ack_id === frameId) {
300
+ if (msg.payload?.ok === false) {
301
+ finish(
302
+ () => reject(
303
+ new Error(
304
+ msg.payload?.error?.message ?? "AgentServerClient.fetchPublicSettingsSchema: request rejected"
305
+ )
306
+ )
307
+ );
308
+ }
309
+ return;
310
+ }
311
+ if (msg.type === "settings.public_schema.result" && msg.id === frameId) {
312
+ const raw = msg.payload?.schema;
313
+ const fieldsWire = raw?.fields ?? [];
314
+ const fields = fieldsWire.map((w) => toSettingsItem(w));
315
+ finish(
316
+ () => resolve({
317
+ version: typeof raw?.version === "string" ? raw.version : "1.0",
318
+ fields
319
+ })
320
+ );
321
+ }
322
+ };
323
+ ws.onerror = () => {
324
+ finish(
325
+ () => reject(new Error("AgentServerClient.fetchPublicSettingsSchema: WebSocket error"))
326
+ );
327
+ };
328
+ ws.onclose = (ev) => {
329
+ if (settled) return;
330
+ finish(
331
+ () => reject(
332
+ new Error(
333
+ `AgentServerClient.fetchPublicSettingsSchema: connection closed (${ev.code})${ev.reason ? ` ${ev.reason}` : ""}`
334
+ )
335
+ )
336
+ );
337
+ };
338
+ });
339
+ }
340
+ };
236
341
  var AgentClient = class {
237
342
  ws = null;
238
343
  callbacks;
@@ -299,12 +404,12 @@ var AgentClient = class {
299
404
  };
300
405
  }
301
406
  connect(url, agentId, config) {
302
- this.url = url;
407
+ this.url = apiMessagesUrlToWebSocketUrl(url);
303
408
  this.agentId = agentId;
304
409
  this.config = config ?? { organizationId: "", workspaceId: "", userId: "" };
305
410
  this.isIntentionallyClosed = false;
306
411
  try {
307
- const parsed = new URL(url);
412
+ const parsed = new URL(this.url);
308
413
  parsed.protocol = parsed.protocol === "wss:" ? "https:" : "http:";
309
414
  parsed.pathname = "";
310
415
  parsed.search = "";
@@ -385,8 +490,12 @@ var AgentClient = class {
385
490
  });
386
491
  }
387
492
  // --- Settings ---
388
- subscribeSettings() {
389
- this.send("settings.subscribe", { agent_id: this.agentId });
493
+ subscribeSettings(options) {
494
+ this.send("settings.subscribe", {
495
+ agent_id: this.agentId,
496
+ audience: options?.audience ?? "user",
497
+ agent_owner_user_id: options?.agentOwnerUserId ?? null
498
+ });
390
499
  }
391
500
  unsubscribeSettings() {
392
501
  this.send("settings.unsubscribe", { agent_id: this.agentId });
@@ -662,6 +771,8 @@ var AgentClient = class {
662
771
  frame.payload.updated_at
663
772
  );
664
773
  break;
774
+ case "settings.public_schema.result":
775
+ break;
665
776
  case "feedback.ack":
666
777
  if (frame.payload.ok) {
667
778
  this.callbacks.onFeedbackAck(
@@ -1398,6 +1509,7 @@ function useFormFileUpload() {
1398
1509
  const formData = new FormData();
1399
1510
  formData.append("file", params.file);
1400
1511
  formData.append("field_id", params.fieldId);
1512
+ formData.append("source", "chat");
1401
1513
  formData.append("agent_id", params.agentId);
1402
1514
  formData.append("run_id", params.runId);
1403
1515
  formData.append("thread_id", params.threadId);
@@ -1438,7 +1550,10 @@ function useSettings() {
1438
1550
  const fields = useShallowStore((s) => s.settings.fields);
1439
1551
  const updatedAt = useStore((s) => s.settings.updatedAt);
1440
1552
  const isLoading = useStore((s) => s.settings.isLoading);
1441
- const subscribe = react.useCallback(() => client.subscribeSettings(), [client]);
1553
+ const subscribe = react.useCallback(
1554
+ (options) => client.subscribeSettings(options),
1555
+ [client]
1556
+ );
1442
1557
  const unsubscribe = react.useCallback(() => client.unsubscribeSettings(), [client]);
1443
1558
  const patchField = react.useCallback(
1444
1559
  (fieldId, value) => client.patchSetting(fieldId, value),
@@ -1565,6 +1680,8 @@ function useUpload() {
1565
1680
  }
1566
1681
 
1567
1682
  exports.AgentProvider = AgentProvider;
1683
+ exports.AgentServerClient = AgentServerClient;
1684
+ exports.apiMessagesUrlToWebSocketUrl = apiMessagesUrlToWebSocketUrl;
1568
1685
  exports.isSettingsFieldGroup = isSettingsFieldGroup;
1569
1686
  exports.toFormElement = toFormElement;
1570
1687
  exports.toFormEventData = toFormEventData;