@invergent/website-widget 2.7.5 → 2.7.7

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.ts CHANGED
@@ -1,176 +1,7 @@
1
- import { AbstractAgent, AgentConfig, RunAgentInput, BaseEvent } from '@ag-ui/client';
1
+ export { W as WebsiteAgent, a as WebsiteAgentConfig } from './agent-ia0JiKMG.js';
2
+ import { BaseEvent } from '@ag-ui/client';
2
3
  export { AbstractAgent, EventType } from '@ag-ui/client';
3
- import { Observable } from 'rxjs';
4
-
5
- /**
6
- * HTTP + SSE wire layer.
7
- *
8
- * Pure functions that talk to the website channel's three endpoints,
9
- * isolated here so :class:`WebsiteAgentClient` can stay focused on
10
- * state-machine orchestration. All fetches use ``credentials:
11
- * 'include'`` so the browser sends the HttpOnly session cookie that
12
- * ``bootstrap`` set -- without it the message/SSE/end routes return
13
- * 401 and the whole channel falls apart.
14
- *
15
- * SSE is handled by the native ``EventSource`` rather than a custom
16
- * fetch-based parser because the native implementation is zero bytes,
17
- * auto-reconnects on network drops, and respects browser-internal
18
- * backoff heuristics. The one thing it can't do is resume with a
19
- * server-side cursor, so we manage the ``?after=N`` reopen logic
20
- * explicitly in :class:`WebsiteAgentClient`.
21
- */
22
- interface BootstrapResult {
23
- sessionId: string;
24
- csrfToken: string;
25
- expiresAt: number;
26
- agentName: string;
27
- }
28
-
29
- /**
30
- * AG-UI ``AbstractAgent`` implementation for the Surogates public-website channel.
31
- *
32
- * A ``WebsiteAgent`` bridges the channel-specific HTTP protocol
33
- * (publishable-key bootstrap, HttpOnly cookie, CSRF double-submit,
34
- * ``/v1/website/sessions/{id}/events`` SSE) onto AG-UI's standard
35
- * ``run()`` contract so any AG-UI-aware host app -- CopilotKit,
36
- * custom chat UIs, LangGraph-style clients -- can talk to a Surogates
37
- * backend with no custom glue beyond instantiation.
38
- *
39
- * Responsibilities
40
- * ----------------
41
- * * **Lazy bootstrap.** The first ``runAgent`` call POSTs to
42
- * ``/v1/website/sessions`` to exchange the publishable key for a
43
- * session cookie + CSRF token; subsequent calls reuse them. A cookie
44
- * expiry (HTTP 401) triggers a transparent re-bootstrap so the host
45
- * app never has to manage auth state.
46
- *
47
- * * **Message → POST.** On each ``runAgent`` call the agent finds the
48
- * latest user message in ``input.messages`` and posts its content to
49
- * ``/v1/website/sessions/{id}/messages`` with the CSRF header.
50
- *
51
- * * **SSE → AG-UI.** The channel's native event stream is converted
52
- * to AG-UI events by :class:`Translator` frame-by-frame. The agent
53
- * opens a fresh EventSource per run (with ``?after=<cursor>`` to
54
- * skip already-processed frames) and closes it after a short grace
55
- * drain on turn completion so trailing server frames (late
56
- * ``memory.update``, ``session.done``) advance the cursor rather
57
- * than replaying on the next run.
58
- *
59
- * * **RUN lifecycle.** AG-UI requires every run to be bracketed by
60
- * ``RUN_STARTED`` and exactly one of ``RUN_FINISHED`` / ``RUN_ERROR``.
61
- * Surogates has no such sentinel on the wire, so the agent emits
62
- * them itself; a per-run ``terminated`` flag guarantees at most one
63
- * terminal event even under concurrent SSE ``onerror`` + translator
64
- * end-of-turn detection.
65
- */
66
-
67
- /**
68
- * Constructor config for :class:`WebsiteAgent`. Extends AG-UI's
69
- * base ``AgentConfig`` with the two Surogates-specific fields the
70
- * bootstrap needs; everything else (``agentId``, ``threadId``,
71
- * ``initialMessages``, ``initialState``, ``debug``) is inherited
72
- * unchanged so consumers reuse the AG-UI patterns they already know.
73
- */
74
- interface WebsiteAgentConfig extends AgentConfig {
75
- /** Base URL of the Surogates API, e.g. ``https://agent.acme.com``. */
76
- apiUrl: string;
77
- /** Publishable key issued by ops (``surg_wk_...``). */
78
- publishableKey: string;
79
- }
80
- declare class WebsiteAgent extends AbstractAgent {
81
- readonly apiUrl: string;
82
- readonly publishableKey: string;
83
- /** Cached bootstrap result. ``undefined`` until the first ``run()``. */
84
- private bootstrapResult;
85
- /** Monotonic cursor across runs. Passed to the SSE ``?after=`` param. */
86
- private cursor;
87
- constructor(config: WebsiteAgentConfig);
88
- /**
89
- * Ensure we have a live session cookie + CSRF token.
90
- *
91
- * Idempotent; returns the cached result when one already exists.
92
- * ``bootstrap()`` is the only call that requires the publishable
93
- * key to travel in an Authorization header, so callers that want
94
- * to eagerly validate their configuration (e.g. to surface a
95
- * setup error at widget-load time) can invoke this directly.
96
- */
97
- ensureBootstrapped(): Promise<BootstrapResult>;
98
- /**
99
- * Close the current session on the server side and drop local state.
100
- *
101
- * Callers should invoke this when the visitor closes the chat UI
102
- * so the server can mark the session complete instead of waiting
103
- * for the idle-reset timer. Safe to call when no session has been
104
- * bootstrapped yet.
105
- */
106
- end(): Promise<void>;
107
- /**
108
- * Implementation of the AG-UI ``run()`` contract.
109
- *
110
- * Returns a cold observable: one subscription per run. AG-UI's
111
- * ``runAgent`` pipeline pipes this through chunk transformation,
112
- * verification, and subscriber notifications before updating
113
- * ``this.messages``.
114
- */
115
- run(input: RunAgentInput): Observable<BaseEvent>;
116
- /**
117
- * Async body of the observable factory.
118
- *
119
- * Emits ``RUN_STARTED`` up front so every downstream failure is
120
- * well-bracketed by the AG-UI lifecycle. ``RUN_FINISHED`` is
121
- * emitted by the SSE listeners when the translator detects end-of-
122
- * turn; ``RUN_ERROR`` is emitted from the catch below when bootstrap
123
- * or message-send fails before the SSE has any chance to close out.
124
- *
125
- * The ``ctx.terminated`` guard is the single enforcement point for
126
- * AG-UI's "exactly one terminal event" invariant. Every emission
127
- * site checks it before calling ``subscriber.next`` with RUN_FINISHED
128
- * or RUN_ERROR, and every terminal call sets it to true first.
129
- */
130
- private driveRun;
131
- /**
132
- * Tear down whatever EventSource is currently live on ``ctx`` and
133
- * open a fresh one against *sessionId* starting at *after*. Called
134
- * both for the initial stream and after cookie recovery. Always
135
- * clears the stale ``onerror`` first so the old source can't race
136
- * a ``RUN_ERROR`` into the subscriber after it's been replaced.
137
- */
138
- private replaceStreamSource;
139
- /**
140
- * Try ``sendMessage``; on auth failure re-bootstrap once and retry.
141
- *
142
- * The re-bootstrap mints a new session id, so the caller passes an
143
- * ``onRebootstrap`` callback that reopens the SSE stream against the
144
- * fresh session before the retry. Visitor continuity across
145
- * bootstraps is a deliberate non-feature for v1; the new session
146
- * starts empty. Only frames emitted on the OLD session between
147
- * SSE-open and send-failure are lost -- and those are always
148
- * previous-turn events, since the current turn's ``sendMessage`` is
149
- * what 401-ed.
150
- */
151
- private sendWithCookieRecovery;
152
- /**
153
- * Wire up the EventSource to translate and forward every frame,
154
- * and terminate the observable when the translator flags
155
- * end-of-turn or a transport failure occurs.
156
- *
157
- * After the translator flips end-of-turn we emit ``RUN_FINISHED``
158
- * immediately, then keep the stream open for ``DRAIN_WINDOW_MS``
159
- * so trailing frames (late ``memory.update``, the sentinel
160
- * ``session.done``) advance ``this.cursor`` rather than arriving
161
- * on the next run. The ``ctx.terminated`` gate stops additional
162
- * subscriber emissions during the drain.
163
- */
164
- private attachStreamListeners;
165
- /**
166
- * Find the last user-authored message body to post. AG-UI supports
167
- * multimodal content arrays; this v1 only handles string content
168
- * (the website channel's ``POST /messages`` endpoint accepts plain
169
- * text). Multimodal support lands once the server grows a media
170
- * upload path.
171
- */
172
- private extractUserText;
173
- }
4
+ import 'rxjs';
174
5
 
175
6
  /**
176
7
  * Pure event translation: Surogates wire events → AG-UI ``BaseEvent``\ s.
@@ -403,4 +234,4 @@ declare const SURG_EVENT: {
403
234
  readonly HARNESS_CRASH: "harness.crash";
404
235
  };
405
236
 
406
- export { PROTOCOL_VERSION, SDK_VERSION, SURG_EVENT, SurogatesAuthError, SurogatesError, type SurogatesFrame, SurogatesNetworkError, SurogatesProtocolError, SurogatesRateLimitError, Translator, WebsiteAgent, type WebsiteAgentConfig };
237
+ export { PROTOCOL_VERSION, SDK_VERSION, SURG_EVENT, SurogatesAuthError, SurogatesError, type SurogatesFrame, SurogatesNetworkError, SurogatesProtocolError, SurogatesRateLimitError, Translator };