@useago/sdk 1.5.5 → 1.7.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +230 -0
  2. package/README.md +59 -0
  3. package/dist/AgoClient-BHegN73y.cjs +9 -0
  4. package/dist/{AgoClient-BI5esqbd.js → AgoClient-PWPaekcg.js} +657 -387
  5. package/dist/angular.cjs +1 -1
  6. package/dist/angular.d.cts +230 -5
  7. package/dist/angular.d.ts +230 -5
  8. package/dist/angular.js +10 -1
  9. package/dist/{createConversationSession-0zVkMdYj.cjs → createConversationSession-BbUUukV0.cjs} +1 -1
  10. package/dist/{createConversationSession-BlYTpxwz.js → createConversationSession-BkruoOVm.js} +1 -1
  11. package/dist/{createFormCollector-DiCe_8ik.cjs → createFormCollector-C1ixsjV0.cjs} +1 -1
  12. package/dist/{createFormCollector-Bgs94NyR.js → createFormCollector-CvVFNkp6.js} +1 -1
  13. package/dist/{createMockClient-DcuNlufr.js → createMockClient-BDLW6tVc.js} +5 -2
  14. package/dist/createMockClient-BSNuN3ys.cjs +1 -0
  15. package/dist/devtools.d.cts +220 -5
  16. package/dist/devtools.d.ts +220 -5
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.d.cts +238 -6
  19. package/dist/index.d.ts +238 -6
  20. package/dist/index.js +111 -109
  21. package/dist/react.cjs +40 -3
  22. package/dist/react.d.cts +419 -7
  23. package/dist/react.d.ts +419 -7
  24. package/dist/react.js +1195 -743
  25. package/dist/sheetController-CBUVOroo.js +205 -0
  26. package/dist/sheetController-DY-sC13T.cjs +1 -0
  27. package/dist/testing.cjs +1 -1
  28. package/dist/testing.d.cts +220 -5
  29. package/dist/testing.d.ts +220 -5
  30. package/dist/testing.js +1 -1
  31. package/dist/vue.cjs +1 -1
  32. package/dist/vue.d.cts +235 -5
  33. package/dist/vue.d.ts +235 -5
  34. package/dist/vue.js +121 -100
  35. package/dist/widget.cjs +4 -4
  36. package/dist/widget.d.cts +328 -5
  37. package/dist/widget.d.ts +328 -5
  38. package/dist/widget.js +721 -629
  39. package/llms-full.txt +202 -14
  40. package/package.json +1 -1
  41. package/dist/AgoClient-CQh8YovA.cjs +0 -8
  42. package/dist/createMockClient-Dw-cyISN.cjs +0 -1
package/dist/widget.d.ts CHANGED
@@ -57,6 +57,10 @@ declare class AgoClient {
57
57
  private config;
58
58
  /** Conversations already warned about an empty reply (one warning each). */
59
59
  private warnedEmptyConversations;
60
+ /** Whether the placeholder-mode page-data warning has already been logged. */
61
+ private warnedPageDataPlaceholder;
62
+ /** `readPageData` companions this SDK registered, so cleanup only removes its own. */
63
+ private pageDataCompanions;
60
64
  /** Paused turns awaiting auto-resume, keyed by assistant message id. */
61
65
  private pendingResumes;
62
66
  /**
@@ -67,6 +71,20 @@ declare class AgoClient {
67
71
  private pendingApprovals;
68
72
  /** App-provided gate deciding WHEN a paused turn resumes (see {@link registerResumeGate}). */
69
73
  private resumeGate;
74
+ /** The turn currently streaming, or `null` when the client is idle. */
75
+ private activeTurn;
76
+ /**
77
+ * A turn that closed as `WAITING_CLIENT` and has not resumed yet. There is no
78
+ * open stream in that window, but the turn is still live for the user, so it
79
+ * stays stoppable (see {@link stop}).
80
+ */
81
+ private pausedTurn;
82
+ /**
83
+ * Turns stopped while they were paused on client functions. A resume may
84
+ * already be queued (or waiting on the resume gate) at that point; this drops
85
+ * it, so a stopped turn is never restarted.
86
+ */
87
+ private stoppedTurnIds;
70
88
  /**
71
89
  * Proactive-mode controller, set when the client was created with
72
90
  * `proactive` options or when `createAgoProactive(client, opts)` was called.
@@ -78,7 +96,53 @@ declare class AgoClient {
78
96
  * Send a message and receive a streaming response
79
97
  */
80
98
  sendMessage(content: string, options?: SendMessageOptions): Promise<AgoMessage>;
99
+ /**
100
+ * Fire the streaming request, returning `null` when the user stopped before
101
+ * the response came back. Any other failure ends the turn and propagates, so
102
+ * a real error still reaches the caller.
103
+ */
104
+ private sendRequest;
81
105
  private processSSEResponse;
106
+ /**
107
+ * Stop the turn that is generating right now: closes the stream so the UI
108
+ * stops immediately, and tells the backend to stop generating.
109
+ *
110
+ * Both halves matter. Closing the stream alone does NOT stop the agent — it
111
+ * keeps running and the full answer reappears the next time the conversation
112
+ * is loaded. The backend unwinds at its next safe point (any tool already
113
+ * running finishes, no further tool or reasoning round starts), keeps the text
114
+ * produced so far and finalizes the message as `CANCELED`.
115
+ *
116
+ * The in-flight `sendMessage` / `continueMessage` promise resolves normally
117
+ * with that partial message (it does not reject), `message:complete` fires
118
+ * with status `CANCELED`, then `message:stopped`.
119
+ *
120
+ * ```ts
121
+ * // "Stop" button
122
+ * stopButton.onclick = () => void client.stop();
123
+ * ```
124
+ *
125
+ * Returns the backend's verdict, or `null` when nothing was generating or the
126
+ * stop landed before the message had an id (the stream is still closed).
127
+ */
128
+ stop(): Promise<StopMessageResult | null>;
129
+ /**
130
+ * Ask the backend to stop a specific in-flight turn, without touching any
131
+ * local stream. Idempotent — stopping a finished turn answers
132
+ * `{ status: "not_running" }` rather than failing. Most apps want
133
+ * {@link stop} instead; use this to stop a turn started elsewhere (e.g. one
134
+ * still running after a page reload, found via `getConversation`).
135
+ */
136
+ stopMessage(messageId: string): Promise<StopMessageResult>;
137
+ /** Whether a turn is generating right now (i.e. {@link stop} has something to stop). */
138
+ isGenerating(): boolean;
139
+ /** Open a new turn, replacing any bookkeeping left by the previous one. */
140
+ private beginTurn;
141
+ private endTurn;
142
+ /** POST the stop for a turn, once, never letting the failure escape. */
143
+ private postStopForTurn;
144
+ /** The message a caller gets back when the stop beat the response. */
145
+ private buildStoppedMessage;
82
146
  private resolveClientFunctionsMode;
83
147
  /**
84
148
  * Register a gate deciding WHEN a paused turn resumes. The SDK still decides
@@ -306,13 +370,38 @@ declare class AgoClient {
306
370
  * },
307
371
  * ]);
308
372
  * ```
373
+ *
374
+ * Pass `opts.data` and the agent also gets back **what the page now shows**,
375
+ * as the result of its own call, instead of having to wait for the next
376
+ * message's context. That also registers a read-only `readPageData`
377
+ * companion. See {@link AgoPageDataSource}.
309
378
  */
310
379
  registerPageStateFunction(controls: AgoStateControl[], opts?: AgoPageStateOptions): void;
311
380
  /**
312
- * Unregister a page state function and its dynamic context provider.
313
- * Mirror of unregistering `navigateToPage`.
381
+ * Unregister a page state function, its `readPageData` companion, and its
382
+ * dynamic context provider. Mirror of unregistering `navigateToPage`.
314
383
  */
315
384
  unregisterPageStateFunction(functionName?: string): void;
385
+ /**
386
+ * Size ceiling for a page snapshot, before the envelope is accounted for.
387
+ * Read at call time, not at registration, so `updateConfig` moves it.
388
+ */
389
+ private pageDataBudget;
390
+ /** Bytes the non-`data` part of the result costs, `data` included as `null`. */
391
+ private envelopeBytes;
392
+ /**
393
+ * Wait for the page to stop reloading, then read the snapshot. `get` and
394
+ * `isLoading` are re-read from `source` on every tick so a re-render that
395
+ * swaps the closures (without re-registering) is picked up mid-wait.
396
+ */
397
+ private readPageData;
398
+ /**
399
+ * The snapshot only reaches the agent inside the same turn in pause mode,
400
+ * where the SDK submits the tool result and resumes. In placeholder mode the
401
+ * turn is already over, so a `data` source looks broken from the outside:
402
+ * say so once rather than let the integrator hunt for an SDK bug.
403
+ */
404
+ private warnIfPlaceholderMode;
316
405
  /**
317
406
  * Register or update a piece of client-side context.
318
407
  * This context is sent with every message so the AI understands the user's situation.
@@ -480,6 +569,18 @@ declare interface AgoClientEvents {
480
569
  conversationId: string;
481
570
  messageId: string;
482
571
  };
572
+ /**
573
+ * The user stopped the turn (`client.stop()`): the stream was closed and the
574
+ * backend was told to stop generating. Always fires, so this is the event to
575
+ * finalize a UI on. When there was an answer to keep, a `message:complete`
576
+ * carrying it with status `CANCELED` fires first; there is none when the stop
577
+ * landed before the backend named the message (`messageId` is then an empty
578
+ * string) or when the stopped turn was paused on client functions.
579
+ */
580
+ "message:stopped": {
581
+ conversationId: string;
582
+ messageId: string;
583
+ };
483
584
  "message:error": {
484
585
  error: string;
485
586
  /** Stable error code (see configuration.md#error-codes) when the failure was an AgoError. */
@@ -673,6 +774,80 @@ declare interface AgoMessage {
673
774
  createdAt: Date;
674
775
  }
675
776
 
777
+ /**
778
+ * What the page currently displays, returned to the agent as the *result* of
779
+ * `setPageState` instead of only as context on the next message.
780
+ *
781
+ * Declare it when the agent should be able to act on what its own change
782
+ * produced ("search for dupont" → it can say who was found).
783
+ *
784
+ * A control change usually kicks off work (a refetch, a recomputation). The SDK
785
+ * has to wait for that work before reading, and there are two ways to tell it
786
+ * when the work is done. **Prefer the first.**
787
+ *
788
+ * **1. Return a promise from `get()`** — you own the await, so the SDK is
789
+ * exact: it resolves when your work resolves, with no polling and no timing
790
+ * assumption. With TanStack Query, `ensureQueryData` gives you that promise and
791
+ * shares the single in-flight request with the UI:
792
+ *
793
+ * ```ts
794
+ * useAgoPageState(controls, {
795
+ * data: {
796
+ * description: "The users matching the current filters.",
797
+ * get: () => queryClient.ensureQueryData({
798
+ * queryKey: ["users", filters],
799
+ * queryFn: fetchUsers,
800
+ * }),
801
+ * },
802
+ * });
803
+ * ```
804
+ *
805
+ * **2. Declare `isLoading`** — for data you cannot get a promise for. The SDK
806
+ * polls it instead, which is a heuristic: the flag has to be up within ~100 ms
807
+ * of the change or the SDK concludes the page settled and reads the previous
808
+ * rows. Fine for a query library that flips `isFetching` on render, wrong for a
809
+ * debounced fetch.
810
+ *
811
+ * ```ts
812
+ * const { data: users, isFetching } = useQuery({ queryKey: ["users", filters], queryFn: fetchUsers });
813
+ *
814
+ * useAgoPageState(controls, {
815
+ * data: {
816
+ * description: "The users matching the current filters.",
817
+ * get: () => users ?? [],
818
+ * // isFetching, NOT isLoading: a background refetch must count as loading,
819
+ * // otherwise the agent reads the rows from the previous search.
820
+ * isLoading: () => isFetching,
821
+ * },
822
+ * });
823
+ * ```
824
+ */
825
+ declare interface AgoPageDataSource<T = unknown> {
826
+ /** What this data is, for the LLM: "The users matching the current filters." */
827
+ description: string;
828
+ /**
829
+ * The snapshot handed back to the agent.
830
+ *
831
+ * Return a promise and the SDK awaits it: that promise resolving *is* the
832
+ * signal that the work the change triggered has finished, so nothing is
833
+ * guessed. It is bounded by `settleTimeoutMs`.
834
+ */
835
+ get: () => T | Promise<T>;
836
+ /**
837
+ * True while the page is (re)loading this data. Only needed when `get()`
838
+ * cannot return a promise. The SDK polls it every 50 ms and treats two
839
+ * consecutive `false` readings as settled, so a flag raised more than ~100 ms
840
+ * after the change is missed and the agent reads stale rows.
841
+ */
842
+ isLoading?: () => boolean;
843
+ /**
844
+ * Size ceiling for the returned snapshot, in bytes. Defaults to the client's
845
+ * `maxFunctionResultBytes`. Over the ceiling the snapshot keeps the first
846
+ * whole items that fit; `success` and `applied` are never dropped.
847
+ */
848
+ maxResultBytes?: number;
849
+ }
850
+
676
851
  /**
677
852
  * Options for {@link AgoClient.registerPageStateFunction}.
678
853
  */
@@ -684,6 +859,23 @@ declare interface AgoPageStateOptions {
684
859
  * {@link AgoConfig.approvalPolicy} — pause mode only. Defaults to `false`.
685
860
  */
686
861
  requiresApproval?: boolean;
862
+ /**
863
+ * Declare what the page displays so `setPageState` returns it. Omit it and
864
+ * the result stays exactly `{ success, applied }`.
865
+ *
866
+ * Also registers a read-only companion function (`readPageData`, derived from
867
+ * `functionName`) that returns the same snapshot without touching the page.
868
+ *
869
+ * The snapshot travels only when the agent calls one of those functions: it
870
+ * is deliberately kept out of the `page-state:` dynamic context, which would
871
+ * put it in every message.
872
+ */
873
+ data?: AgoPageDataSource;
874
+ /**
875
+ * How long to wait for the page to finish reloading before reading the
876
+ * snapshot. Defaults to 10 000 ms; on timeout whatever is there is returned.
877
+ */
878
+ settleTimeoutMs?: number;
687
879
  }
688
880
 
689
881
  /**
@@ -758,6 +950,8 @@ export declare interface ChatWidgetHandle {
758
950
  element: HTMLElement;
759
951
  /** Programmatically send a message (same path as the input). */
760
952
  sendMessage: (content: string, files?: File[]) => Promise<void>;
953
+ /** Stop the turn being generated (same path as the Stop button). No-op when idle. */
954
+ stop: () => Promise<void>;
761
955
  /**
762
956
  * Open / close / toggle the panel. Present for `placement: "left" | "right"`,
763
957
  * and for `placement: "inline"` in a browser (where they expand / collapse the
@@ -874,6 +1068,15 @@ declare interface ClientFunctionSchema {
874
1068
  /** See {@link AgoConfig.clientFunctionsMode}. */
875
1069
  declare type ClientFunctionsMode = "placeholder" | "pause";
876
1070
 
1071
+ /**
1072
+ * Build the media query for the compact layout.
1073
+ *
1074
+ * Width alone is not enough: a phone in landscape is 844x390, so a
1075
+ * `max-width: 768px` query stops matching on the viewport that needs the sheet
1076
+ * most. The height clause covers it.
1077
+ */
1078
+ export declare function compactMediaQuery(breakpoint: number): string;
1079
+
877
1080
  /**
878
1081
  * A single piece of context the client wants the LLM to know about.
879
1082
  */
@@ -994,6 +1197,8 @@ export declare interface CreateFormCollectorOptions<V = Record<string, unknown>>
994
1197
  autoSubmit?: boolean;
995
1198
  }
996
1199
 
1200
+ export declare function createSheetController(options?: SheetOptions): SheetController;
1201
+
997
1202
  /**
998
1203
  * A function that returns a fresh context entry on demand.
999
1204
  * Evaluated every time a message is sent — use it to pull data from stores,
@@ -1001,6 +1206,13 @@ export declare interface CreateFormCollectorOptions<V = Record<string, unknown>>
1001
1206
  */
1002
1207
  declare type DynamicContextProvider = () => ContextEntry | null | undefined;
1003
1208
 
1209
+ /** Style/attribute bag for one element. Style keys are camelCase: React spreads
1210
+ * them directly, and the vanilla `css()` helper takes the same shape. */
1211
+ export declare interface ElementProps {
1212
+ style: Record<string, string>;
1213
+ attrs: Record<string, string | null>;
1214
+ }
1215
+
1004
1216
  /**
1005
1217
  * The backend-stored shape returned by `GET /api/sdk/v1/forms/{name}` (see
1006
1218
  * {@link AgoClient.getFormCollector}). Mirrors the persistable subset of
@@ -1123,13 +1335,21 @@ declare class HttpClient {
1123
1335
  keepalive?: boolean;
1124
1336
  }): Promise<T>;
1125
1337
  /**
1126
- * Make a POST request and return the raw Response (for streaming)
1338
+ * Make a POST request and return the raw Response (for streaming).
1339
+ *
1340
+ * `signal` lets the caller close the stream mid-flight (see
1341
+ * `AgoClient.stop`); an abort is rethrown as-is rather than wrapped in an
1342
+ * `AgoNetworkError`, so a deliberate stop is not mistaken for a network drop.
1127
1343
  */
1128
- postStream(path: string, body?: unknown): Promise<Response>;
1344
+ postStream(path: string, body?: unknown, options?: {
1345
+ signal?: AbortSignal;
1346
+ }): Promise<Response>;
1129
1347
  /**
1130
1348
  * Make a POST request with FormData (for file uploads)
1131
1349
  */
1132
- postFormData(path: string, formData: FormData): Promise<Response>;
1350
+ postFormData(path: string, formData: FormData, options?: {
1351
+ signal?: AbortSignal;
1352
+ }): Promise<Response>;
1133
1353
  private request;
1134
1354
  private handleErrorResponse;
1135
1355
  }
@@ -1217,6 +1437,12 @@ export declare interface MountChatWidgetOptions {
1217
1437
  placeholder?: string;
1218
1438
  /** Enable file attachments. */
1219
1439
  allowFiles?: boolean;
1440
+ /**
1441
+ * While the agent is answering, turn the send button into a Stop button that
1442
+ * interrupts the turn. Default `true`; set `false` to keep showing a disabled
1443
+ * spinner instead.
1444
+ */
1445
+ allowStop?: boolean;
1220
1446
  /** Widget height (number → px). Ignored when `placement` is `"left"`/`"right"`
1221
1447
  * (a side panel is always full-height). */
1222
1448
  height?: string | number;
@@ -1616,6 +1842,88 @@ declare interface SendMessageOptions {
1616
1842
  clientFunctionsMode?: ClientFunctionsMode;
1617
1843
  }
1618
1844
 
1845
+ /** Why a transition happened, so hosts can tell a tap from a breakpoint change. */
1846
+ export declare type SheetChangeCause = "api" | "user" | "breakpoint";
1847
+
1848
+ export declare interface SheetController {
1849
+ subscribe(onChange: () => void): () => void;
1850
+ getSnapshot(): SheetSnapshot;
1851
+ /**
1852
+ * Snapshot for server rendering: never compact, so the server HTML matches the
1853
+ * client's first paint. Promotion to a sheet happens after hydration, which
1854
+ * keeps `matchMedia` (absent in Node) out of the render path entirely.
1855
+ */
1856
+ getServerSnapshot(): SheetSnapshot;
1857
+ expand(cause?: SheetChangeCause): void;
1858
+ collapse(cause?: SheetChangeCause): void;
1859
+ toggle(cause?: SheetChangeCause): void;
1860
+ destroy(): void;
1861
+ }
1862
+
1863
+ export declare interface SheetOptions {
1864
+ /**
1865
+ * Max viewport width (px) treated as compact. A short landscape viewport
1866
+ * (`max-height: 500px`) counts as compact too, so a phone turned sideways
1867
+ * keeps the sheet instead of dropping to the desktop layout mid-conversation.
1868
+ * Defaults to `768`.
1869
+ */
1870
+ breakpoint?: number;
1871
+ /** State to rest in when the compact layout first applies. Defaults to `"peek"`. */
1872
+ initialState?: SheetState;
1873
+ /**
1874
+ * Space to leave below the sheet, for hosts whose page already owns the bottom
1875
+ * edge (a bottom nav, a sticky checkout bar). A number is treated as px; a
1876
+ * string is used verbatim, so `env(safe-area-inset-bottom)` or a `calc()` both
1877
+ * work. Defaults to `0`.
1878
+ */
1879
+ bottomOffset?: number | string;
1880
+ /**
1881
+ * Minimum height of the collapsed sheet, excluding the bottom safe area.
1882
+ * The bar grows past it if the host's own composer styling is taller.
1883
+ * Defaults to `152`.
1884
+ */
1885
+ peekHeight?: number;
1886
+ /** Called after every state change, with what caused it. */
1887
+ onStateChange?: (state: SheetState, cause: SheetChangeCause) => void;
1888
+ }
1889
+
1890
+ export declare interface SheetSnapshot {
1891
+ /** Whether the compact (phone-shaped) layout applies at all. */
1892
+ compact: boolean;
1893
+ /** Current resting state. Meaningless while `compact` is false. */
1894
+ state: SheetState;
1895
+ /**
1896
+ * Whether the sheet currently covers the viewport. Drives scroll locking,
1897
+ * dialog semantics, focus trapping and Escape. One predicate, so those can
1898
+ * never drift apart. False on a desktop viewport even when open: the host page
1899
+ * is still usable beside the widget and must stay keyboard-reachable.
1900
+ */
1901
+ isModal: boolean;
1902
+ /** Props for the sheet surface itself. */
1903
+ surface: ElementProps;
1904
+ }
1905
+
1906
+ /**
1907
+ * Framework-agnostic bottom-sheet controller.
1908
+ *
1909
+ * Owns the `peek <-> full` state machine and turns it into a **props bag**: a
1910
+ * plain object of styles and ARIA attributes that a renderer applies however it
1911
+ * likes. It never touches the DOM itself.
1912
+ *
1913
+ * That shape is deliberate. The React widget re-renders on every streamed token
1914
+ * and re-applies its inline `style` object, so a controller that wrote to the
1915
+ * DOM directly would be clobbered mid-stream. Returning props instead means
1916
+ * React can spread them, the vanilla adapter can hand them to `css()`, and a Vue
1917
+ * or Angular binding can bind them, all from one implementation.
1918
+ *
1919
+ * Modality is expressed, not delegated to `<dialog>`: promoting a dialog from
1920
+ * `show()` to `showModal()` requires closing it and re-parenting into the top
1921
+ * layer, which cancels any running transition. The renderer applies
1922
+ * `role="dialog"` / `aria-modal` / focus trapping from `isModal` instead.
1923
+ */
1924
+ /** The two resting states. There is no `half`: it needs a drag engine to be worth having. */
1925
+ export declare type SheetState = "peek" | "full";
1926
+
1619
1927
  /** Tuning knobs for the collector's detectors. All optional. */
1620
1928
  declare interface SignalCollectorTuning {
1621
1929
  /** Min gap between heavy activity work (route checks, notifications), ms. Default 250. */
@@ -1709,6 +2017,21 @@ declare interface SSEChunkData {
1709
2017
  waiting_tool_call_ids?: string[];
1710
2018
  }
1711
2019
 
2020
+ /**
2021
+ * Response of `POST /messages/{id}/stop` (see `AgoClient.stopMessage`).
2022
+ *
2023
+ * - `"stopping"` — the request was accepted; the turn unwinds at its next safe
2024
+ * point and finalizes as `CANCELED`, keeping the text produced so far.
2025
+ * - `"not_running"` — the turn had already finished. Harmless.
2026
+ * - `"not_supported"` — a background agent run, which cannot be stopped once
2027
+ * started.
2028
+ */
2029
+ declare interface StopMessageResult {
2030
+ status: "stopping" | "not_running" | "not_supported";
2031
+ /** The message's status when the stop was requested. */
2032
+ messageStatus?: MessageStatus;
2033
+ }
2034
+
1712
2035
  /** The slice of the Web Storage API that {@link createStore} relies on. */
1713
2036
  declare type StorageLike = Pick<Storage, "getItem" | "setItem">;
1714
2037