@useago/sdk 1.0.0 → 1.0.2

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/index.d.cts CHANGED
@@ -27,12 +27,26 @@ export declare class AgoClient {
27
27
  private contextRegistry;
28
28
  private eventEmitter;
29
29
  private config;
30
+ /** Conversations already warned about an empty reply (one warning each). */
31
+ private warnedEmptyConversations;
30
32
  constructor(config: AgoConfig);
31
33
  /**
32
34
  * Send a message and receive a streaming response
33
35
  */
34
36
  sendMessage(content: string, options?: SendMessageOptions): Promise<AgoMessage>;
35
37
  private processSSEResponse;
38
+ /**
39
+ * Detect a reply that completed as DONE with no content and no activity —
40
+ * today's backend answers an unknown `agent` slug with an empty 200, which
41
+ * otherwise fails silently. Emits `message:empty` (always) and warns on the
42
+ * console once per conversation (unless `warnOnEmptyReply: false`).
43
+ *
44
+ * Deferred with setTimeout(0) so both land AFTER `sendMessage` resolves:
45
+ * `await client.sendMessage(...)` followed by `client.waitFor("message:empty")`
46
+ * must not race. TEMPORARY heuristic: remove once the backend returns an
47
+ * explicit error for unknown agents (see TODOS.md, backend issue).
48
+ */
49
+ private maybeFlagEmptyReply;
36
50
  private handleClientFunctionInvocation;
37
51
  /**
38
52
  * Get list of conversations
@@ -64,6 +78,13 @@ export declare class AgoClient {
64
78
  * Reject a tool call
65
79
  */
66
80
  rejectToolCall(toolCallId: string): Promise<void>;
81
+ /**
82
+ * Fetch a backend-stored form collector definition by name (the single source
83
+ * of truth for the form's description, schema, and submit target). Used by
84
+ * `loadFormCollector` / the name-only form of `useFormCollector` so the schema
85
+ * lives in the backend instead of being hardcoded in client code.
86
+ */
87
+ getFormCollector(name: string): Promise<FormCollectorDefinition>;
67
88
  /**
68
89
  * Relay a completed form collector's values to a server-configured destination.
69
90
  *
@@ -198,6 +219,11 @@ export declare class AgoClient {
198
219
  }): Promise<AgoClientEvents[K]>;
199
220
  /**
200
221
  * Update client configuration
222
+ *
223
+ * Validates the MERGED config before committing: omitting a key (or passing
224
+ * it as `undefined`, common with spread-built partials) keeps the current
225
+ * value; an explicit empty/non-string `baseUrl` throws `config_missing_base_url`
226
+ * and leaves the client unchanged (no half-updated state).
201
227
  */
202
228
  updateConfig(config: Partial<AgoConfig>): void;
203
229
  /**
@@ -233,8 +259,23 @@ export declare interface AgoClientEvents {
233
259
  */
234
260
  "message:answer-complete": AgoMessage;
235
261
  "message:complete": AgoMessage;
262
+ /**
263
+ * A reply finished as `DONE` with empty content and no tool calls, client
264
+ * functions, or follow-up replies — usually an unknown `agent` slug (the
265
+ * backend currently answers those with an empty 200). Fires after
266
+ * `message:complete` AND after `sendMessage` resolves, so subscribing right
267
+ * after `await sendMessage(...)` still catches it. `messageId` and
268
+ * `conversationId` are empty strings when the stream completed without any
269
+ * message data at all (e.g. a proxy stripped the SSE stream).
270
+ */
271
+ "message:empty": {
272
+ conversationId: string;
273
+ messageId: string;
274
+ };
236
275
  "message:error": {
237
276
  error: string;
277
+ /** Stable error code (see configuration.md#error-codes) when the failure was an AgoError. */
278
+ code?: string;
238
279
  conversationId?: string;
239
280
  messageId?: string;
240
281
  };
@@ -263,7 +304,7 @@ export declare interface AgoClientEvents {
263
304
  export declare interface AgoConfig {
264
305
  /** API base URL (e.g., https://YOUR-DOMAIN.useago.com) */
265
306
  baseUrl: string;
266
- /** Widget ID (X-Widget-Id header). Auto-generated if not provided. */
307
+ /** Per-visitor anonymous ID (X-User-Anon-Id header). Auto-generated if not provided. */
267
308
  widgetId?: string;
268
309
  /** Default agent (id or slug) for new conversations. Shorthand for `defaultAgentId`. */
269
310
  agent?: string;
@@ -277,10 +318,23 @@ export declare interface AgoConfig {
277
318
  userJwt?: string;
278
319
  /** Enable debug logging */
279
320
  debug?: boolean;
321
+ /**
322
+ * Warn on the console when a reply completes with empty content and no tool
323
+ * calls (the usual cause is an unknown `agent` slug, which the backend
324
+ * currently answers with an empty 200). Default `true`. The `message:empty`
325
+ * event fires regardless of this setting.
326
+ */
327
+ warnOnEmptyReply?: boolean;
280
328
  }
281
329
 
282
330
  /**
283
331
  * Base error class for AGO SDK
332
+ *
333
+ * Error convention: every throw carries a stable `code` (snake_case), a
334
+ * one-line fix hint in the message, and a doc URL/anchor when one exists.
335
+ * `code` is the compatibility surface — match on it, never on message text,
336
+ * which may be reworded in any release. The code registry lives in
337
+ * docs/general/configuration.md#error-codes.
284
338
  */
285
339
  export declare class AgoError extends Error {
286
340
  code: string;
@@ -336,9 +390,14 @@ export declare interface AgoSource {
336
390
 
337
391
  /**
338
392
  * SSE stream error
393
+ *
394
+ * `code` distinguishes failure classes without message parsing:
395
+ * - `stream_no_body`: the response had no streamable body (usually a
396
+ * misconfigured endpoint or a proxy stripping the SSE stream)
397
+ * - `stream_error` (default): the stream failed mid-flight
339
398
  */
340
399
  export declare class AgoStreamError extends AgoError {
341
- constructor(message: string);
400
+ constructor(message: string, code?: string);
342
401
  }
343
402
 
344
403
  /**
@@ -616,8 +675,8 @@ export declare const copyToClipboard: ClientFunctionDefinition;
616
675
  export declare function createAgo(overrides?: Partial<AgoConfig>): AgoClient;
617
676
 
618
677
  /**
619
- * Identify a returning visitor with a single, stable widget id — the same model as
620
- * the frontend's `getOrGenerateWidgetId` — and resume their last active thread.
678
+ * Identify a returning visitor with a single, stable anon id (one id, generated
679
+ * once and reused forever) and resume their last active thread.
621
680
  *
622
681
  * There is no per-agent conversation id: the widget id is the identity, and the last
623
682
  * active thread (its id + the time of its last message) is cached locally so resuming
@@ -814,6 +873,22 @@ export declare interface FormCollector<V = Record<string, unknown>> {
814
873
  getValues: () => Partial<V>;
815
874
  setValues: (patch: Partial<V>) => void;
816
875
  reset: () => void;
876
+ /** Completeness status (`missing`/`complete`) derived from the current values + schema. */
877
+ getStatus: () => FormCollectorStatus;
878
+ }
879
+
880
+ /**
881
+ * The backend-stored shape returned by `GET /api/sdk/v1/forms/{name}` (see
882
+ * {@link AgoClient.getFormCollector}). Mirrors the persistable subset of
883
+ * {@link CreateFormCollectorOptions}: the `submit` here is never the client-only
884
+ * `handler` variant (a function can't be stored server-side).
885
+ */
886
+ export declare interface FormCollectorDefinition<V = Record<string, unknown>> {
887
+ name: string;
888
+ description: string;
889
+ schema: FormCollectorSchema;
890
+ /** Server-stored submit target, if any. Absent means collect-only. */
891
+ submit?: SubmitConfig<V>;
817
892
  }
818
893
 
819
894
  /**
@@ -988,6 +1063,30 @@ export declare const highlightElement: ClientFunctionDefinition;
988
1063
  */
989
1064
  export declare function isStreamNetworkError(error: Error): boolean;
990
1065
 
1066
+ /**
1067
+ * Async sibling of {@link createFormCollector} that pulls the form's definition
1068
+ * (description + schema + submit target) from the backend by name — the single
1069
+ * source of truth — instead of hardcoding it in client code. Any field passed in
1070
+ * `options` overrides the fetched one (e.g. a client-only `submit` handler or
1071
+ * `initialValues`).
1072
+ *
1073
+ * ```ts
1074
+ * const order = await loadFormCollector(client, { name: "order" });
1075
+ * order.install(client);
1076
+ * ```
1077
+ */
1078
+ export declare function loadFormCollector<V = Record<string, unknown>>(client: Pick<AgoClient, "getFormCollector">, options: LoadFormCollectorOptions<V>): Promise<FormCollector<V>>;
1079
+
1080
+ /**
1081
+ * Options for {@link loadFormCollector} / the name-only form of `useFormCollector`:
1082
+ * the `name` is required and the definition is fetched from the backend, while any
1083
+ * field here overrides the fetched one — notably a client-only `submit` handler,
1084
+ * which can't be stored server-side.
1085
+ */
1086
+ export declare interface LoadFormCollectorOptions<V = Record<string, unknown>> extends Partial<Omit<CreateFormCollectorOptions<V>, "name">> {
1087
+ name: string;
1088
+ }
1089
+
991
1090
  /**
992
1091
  * Debug logger for SDK
993
1092
  */
@@ -1314,6 +1413,29 @@ export declare type ToolCallType = "form" | "confirmation_input" | "status_messa
1314
1413
  */
1315
1414
  export declare const trackEvent: ClientFunctionDefinition;
1316
1415
 
1416
+ /**
1417
+ * Validate an AgoConfig at the public API boundary.
1418
+ *
1419
+ * Rules:
1420
+ * - nullish / non-object config, or a missing/empty/non-string `baseUrl`,
1421
+ * throws `AgoError` with the stable code `config_missing_base_url`.
1422
+ * - Values starting with `/` are allowed (same-origin proxy paths like
1423
+ * `/ago-proxy` are a supported embed pattern).
1424
+ * - Values with any `scheme://` are allowed.
1425
+ * - Anything else (no scheme, no leading slash, e.g. "ago.api.useago.com")
1426
+ * warns once per process — it usually means a forgotten protocol. It does
1427
+ * not throw: the request fails at send time with a runtime-specific error
1428
+ * (Node: URL parse error; browser: relative fetch), and a hard throw here
1429
+ * could break embeds we cannot foresee.
1430
+ *
1431
+ * The warning never echoes the configured value: misconfigured baseUrls
1432
+ * sometimes carry credentials (e.g. `https://user:token@host`).
1433
+ *
1434
+ * @param context Prefix for the error message, naming the API the developer
1435
+ * actually called (e.g. "AgoClient", "createAgo()").
1436
+ */
1437
+ export declare function validateConfig(config: unknown, context?: string): asserts config is AgoConfig;
1438
+
1317
1439
  /**
1318
1440
  * Attach a handler to a pre-built function definition.
1319
1441
  *
package/dist/index.d.ts CHANGED
@@ -27,12 +27,26 @@ export declare class AgoClient {
27
27
  private contextRegistry;
28
28
  private eventEmitter;
29
29
  private config;
30
+ /** Conversations already warned about an empty reply (one warning each). */
31
+ private warnedEmptyConversations;
30
32
  constructor(config: AgoConfig);
31
33
  /**
32
34
  * Send a message and receive a streaming response
33
35
  */
34
36
  sendMessage(content: string, options?: SendMessageOptions): Promise<AgoMessage>;
35
37
  private processSSEResponse;
38
+ /**
39
+ * Detect a reply that completed as DONE with no content and no activity —
40
+ * today's backend answers an unknown `agent` slug with an empty 200, which
41
+ * otherwise fails silently. Emits `message:empty` (always) and warns on the
42
+ * console once per conversation (unless `warnOnEmptyReply: false`).
43
+ *
44
+ * Deferred with setTimeout(0) so both land AFTER `sendMessage` resolves:
45
+ * `await client.sendMessage(...)` followed by `client.waitFor("message:empty")`
46
+ * must not race. TEMPORARY heuristic: remove once the backend returns an
47
+ * explicit error for unknown agents (see TODOS.md, backend issue).
48
+ */
49
+ private maybeFlagEmptyReply;
36
50
  private handleClientFunctionInvocation;
37
51
  /**
38
52
  * Get list of conversations
@@ -64,6 +78,13 @@ export declare class AgoClient {
64
78
  * Reject a tool call
65
79
  */
66
80
  rejectToolCall(toolCallId: string): Promise<void>;
81
+ /**
82
+ * Fetch a backend-stored form collector definition by name (the single source
83
+ * of truth for the form's description, schema, and submit target). Used by
84
+ * `loadFormCollector` / the name-only form of `useFormCollector` so the schema
85
+ * lives in the backend instead of being hardcoded in client code.
86
+ */
87
+ getFormCollector(name: string): Promise<FormCollectorDefinition>;
67
88
  /**
68
89
  * Relay a completed form collector's values to a server-configured destination.
69
90
  *
@@ -198,6 +219,11 @@ export declare class AgoClient {
198
219
  }): Promise<AgoClientEvents[K]>;
199
220
  /**
200
221
  * Update client configuration
222
+ *
223
+ * Validates the MERGED config before committing: omitting a key (or passing
224
+ * it as `undefined`, common with spread-built partials) keeps the current
225
+ * value; an explicit empty/non-string `baseUrl` throws `config_missing_base_url`
226
+ * and leaves the client unchanged (no half-updated state).
201
227
  */
202
228
  updateConfig(config: Partial<AgoConfig>): void;
203
229
  /**
@@ -233,8 +259,23 @@ export declare interface AgoClientEvents {
233
259
  */
234
260
  "message:answer-complete": AgoMessage;
235
261
  "message:complete": AgoMessage;
262
+ /**
263
+ * A reply finished as `DONE` with empty content and no tool calls, client
264
+ * functions, or follow-up replies — usually an unknown `agent` slug (the
265
+ * backend currently answers those with an empty 200). Fires after
266
+ * `message:complete` AND after `sendMessage` resolves, so subscribing right
267
+ * after `await sendMessage(...)` still catches it. `messageId` and
268
+ * `conversationId` are empty strings when the stream completed without any
269
+ * message data at all (e.g. a proxy stripped the SSE stream).
270
+ */
271
+ "message:empty": {
272
+ conversationId: string;
273
+ messageId: string;
274
+ };
236
275
  "message:error": {
237
276
  error: string;
277
+ /** Stable error code (see configuration.md#error-codes) when the failure was an AgoError. */
278
+ code?: string;
238
279
  conversationId?: string;
239
280
  messageId?: string;
240
281
  };
@@ -263,7 +304,7 @@ export declare interface AgoClientEvents {
263
304
  export declare interface AgoConfig {
264
305
  /** API base URL (e.g., https://YOUR-DOMAIN.useago.com) */
265
306
  baseUrl: string;
266
- /** Widget ID (X-Widget-Id header). Auto-generated if not provided. */
307
+ /** Per-visitor anonymous ID (X-User-Anon-Id header). Auto-generated if not provided. */
267
308
  widgetId?: string;
268
309
  /** Default agent (id or slug) for new conversations. Shorthand for `defaultAgentId`. */
269
310
  agent?: string;
@@ -277,10 +318,23 @@ export declare interface AgoConfig {
277
318
  userJwt?: string;
278
319
  /** Enable debug logging */
279
320
  debug?: boolean;
321
+ /**
322
+ * Warn on the console when a reply completes with empty content and no tool
323
+ * calls (the usual cause is an unknown `agent` slug, which the backend
324
+ * currently answers with an empty 200). Default `true`. The `message:empty`
325
+ * event fires regardless of this setting.
326
+ */
327
+ warnOnEmptyReply?: boolean;
280
328
  }
281
329
 
282
330
  /**
283
331
  * Base error class for AGO SDK
332
+ *
333
+ * Error convention: every throw carries a stable `code` (snake_case), a
334
+ * one-line fix hint in the message, and a doc URL/anchor when one exists.
335
+ * `code` is the compatibility surface — match on it, never on message text,
336
+ * which may be reworded in any release. The code registry lives in
337
+ * docs/general/configuration.md#error-codes.
284
338
  */
285
339
  export declare class AgoError extends Error {
286
340
  code: string;
@@ -336,9 +390,14 @@ export declare interface AgoSource {
336
390
 
337
391
  /**
338
392
  * SSE stream error
393
+ *
394
+ * `code` distinguishes failure classes without message parsing:
395
+ * - `stream_no_body`: the response had no streamable body (usually a
396
+ * misconfigured endpoint or a proxy stripping the SSE stream)
397
+ * - `stream_error` (default): the stream failed mid-flight
339
398
  */
340
399
  export declare class AgoStreamError extends AgoError {
341
- constructor(message: string);
400
+ constructor(message: string, code?: string);
342
401
  }
343
402
 
344
403
  /**
@@ -616,8 +675,8 @@ export declare const copyToClipboard: ClientFunctionDefinition;
616
675
  export declare function createAgo(overrides?: Partial<AgoConfig>): AgoClient;
617
676
 
618
677
  /**
619
- * Identify a returning visitor with a single, stable widget id — the same model as
620
- * the frontend's `getOrGenerateWidgetId` — and resume their last active thread.
678
+ * Identify a returning visitor with a single, stable anon id (one id, generated
679
+ * once and reused forever) and resume their last active thread.
621
680
  *
622
681
  * There is no per-agent conversation id: the widget id is the identity, and the last
623
682
  * active thread (its id + the time of its last message) is cached locally so resuming
@@ -814,6 +873,22 @@ export declare interface FormCollector<V = Record<string, unknown>> {
814
873
  getValues: () => Partial<V>;
815
874
  setValues: (patch: Partial<V>) => void;
816
875
  reset: () => void;
876
+ /** Completeness status (`missing`/`complete`) derived from the current values + schema. */
877
+ getStatus: () => FormCollectorStatus;
878
+ }
879
+
880
+ /**
881
+ * The backend-stored shape returned by `GET /api/sdk/v1/forms/{name}` (see
882
+ * {@link AgoClient.getFormCollector}). Mirrors the persistable subset of
883
+ * {@link CreateFormCollectorOptions}: the `submit` here is never the client-only
884
+ * `handler` variant (a function can't be stored server-side).
885
+ */
886
+ export declare interface FormCollectorDefinition<V = Record<string, unknown>> {
887
+ name: string;
888
+ description: string;
889
+ schema: FormCollectorSchema;
890
+ /** Server-stored submit target, if any. Absent means collect-only. */
891
+ submit?: SubmitConfig<V>;
817
892
  }
818
893
 
819
894
  /**
@@ -988,6 +1063,30 @@ export declare const highlightElement: ClientFunctionDefinition;
988
1063
  */
989
1064
  export declare function isStreamNetworkError(error: Error): boolean;
990
1065
 
1066
+ /**
1067
+ * Async sibling of {@link createFormCollector} that pulls the form's definition
1068
+ * (description + schema + submit target) from the backend by name — the single
1069
+ * source of truth — instead of hardcoding it in client code. Any field passed in
1070
+ * `options` overrides the fetched one (e.g. a client-only `submit` handler or
1071
+ * `initialValues`).
1072
+ *
1073
+ * ```ts
1074
+ * const order = await loadFormCollector(client, { name: "order" });
1075
+ * order.install(client);
1076
+ * ```
1077
+ */
1078
+ export declare function loadFormCollector<V = Record<string, unknown>>(client: Pick<AgoClient, "getFormCollector">, options: LoadFormCollectorOptions<V>): Promise<FormCollector<V>>;
1079
+
1080
+ /**
1081
+ * Options for {@link loadFormCollector} / the name-only form of `useFormCollector`:
1082
+ * the `name` is required and the definition is fetched from the backend, while any
1083
+ * field here overrides the fetched one — notably a client-only `submit` handler,
1084
+ * which can't be stored server-side.
1085
+ */
1086
+ export declare interface LoadFormCollectorOptions<V = Record<string, unknown>> extends Partial<Omit<CreateFormCollectorOptions<V>, "name">> {
1087
+ name: string;
1088
+ }
1089
+
991
1090
  /**
992
1091
  * Debug logger for SDK
993
1092
  */
@@ -1314,6 +1413,29 @@ export declare type ToolCallType = "form" | "confirmation_input" | "status_messa
1314
1413
  */
1315
1414
  export declare const trackEvent: ClientFunctionDefinition;
1316
1415
 
1416
+ /**
1417
+ * Validate an AgoConfig at the public API boundary.
1418
+ *
1419
+ * Rules:
1420
+ * - nullish / non-object config, or a missing/empty/non-string `baseUrl`,
1421
+ * throws `AgoError` with the stable code `config_missing_base_url`.
1422
+ * - Values starting with `/` are allowed (same-origin proxy paths like
1423
+ * `/ago-proxy` are a supported embed pattern).
1424
+ * - Values with any `scheme://` are allowed.
1425
+ * - Anything else (no scheme, no leading slash, e.g. "ago.api.useago.com")
1426
+ * warns once per process — it usually means a forgotten protocol. It does
1427
+ * not throw: the request fails at send time with a runtime-specific error
1428
+ * (Node: URL parse error; browser: relative fetch), and a hard throw here
1429
+ * could break embeds we cannot foresee.
1430
+ *
1431
+ * The warning never echoes the configured value: misconfigured baseUrls
1432
+ * sometimes carry credentials (e.g. `https://user:token@host`).
1433
+ *
1434
+ * @param context Prefix for the error message, naming the API the developer
1435
+ * actually called (e.g. "AgoClient", "createAgo()").
1436
+ */
1437
+ export declare function validateConfig(config: unknown, context?: string): asserts config is AgoConfig;
1438
+
1317
1439
  /**
1318
1440
  * Attach a handler to a pre-built function definition.
1319
1441
  *