@bymos/agentkit-sdk 0.7.4 → 0.8.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/index.d.ts CHANGED
@@ -119,10 +119,25 @@ export declare type ApiChatResponse = {
119
119
  export declare class ApiClient {
120
120
  private baseUrl;
121
121
  private options;
122
+ private sessionToken;
123
+ private sessionTokenExpiresAt;
122
124
  constructor(baseUrl: string, options?: WidgetRequestOptions);
123
125
  private get credentials();
124
126
  /** Decorate a channel-selecting route's URL with the serving channel. */
125
127
  private channel;
128
+ private authHeaders;
129
+ /** Current cached session token, if any — for stream.ts, which builds its own headers. */
130
+ getSessionToken(): string | undefined;
131
+ /** Whether a cached session token exists and isn't expired/near-expiry (60s buffer). */
132
+ hasValidSessionToken(): boolean;
133
+ /**
134
+ * Establishes a signed widget session token via POST /v1/widget/session,
135
+ * gated by the Turnstile challenge token. Caches the result in memory only
136
+ * (never persisted) for reuse by chat()/uploadImage()/uploadFile(). Never
137
+ * throws — on any failure the client proceeds unauthenticated, same posture
138
+ * as validateWidget().
139
+ */
140
+ establishSession(agentId: string, tenantId: string | undefined, turnstileToken: string | undefined): Promise<boolean>;
126
141
  /**
127
142
  * Called during widget init phase. Validates that the tenantId resolves to a real
128
143
  * tenant and that the agentId is accessible under that tenant.
@@ -302,6 +317,8 @@ export declare type NormalizedWidgetConfig = {
302
317
  attachmentMaxCount: number;
303
318
  /** Maximum per-attachment size (MB) — shared between photos and files. Server-clamped. */
304
319
  attachmentMaxFileSizeMb: number;
320
+ /** Cloudflare Turnstile site key for widget session establishment. Server-only; public by design, not a secret. */
321
+ turnstileSiteKey: string | undefined;
305
322
  };
306
323
 
307
324
  export declare function prepareInlineContainer(element: HTMLElement): HTMLElement;
@@ -475,6 +492,8 @@ export declare type WidgetCallbacks = {
475
492
  export declare class WidgetClient {
476
493
  private readonly hostConfig;
477
494
  private onStateChange;
495
+ /** In-flight Turnstile+session solve, so setOpen and send share one round trip. */
496
+ private sessionTokenPromise;
478
497
  private config;
479
498
  private apiClient;
480
499
  private state;
@@ -517,6 +536,29 @@ export declare class WidgetClient {
517
536
  file: File;
518
537
  kind: 'image' | 'file';
519
538
  }>): Promise<void>;
539
+ /**
540
+ * Re-solves Turnstile and re-establishes the session token when the cached one
541
+ * is missing or expired. Best-effort — a failure here never blocks the send;
542
+ * the resulting chat call is simply unauthenticated, same as a pre-upgrade
543
+ * SDK bundle. No explicit reset needed before re-solving — Turnstile's
544
+ * 'execute' mode (see api/turnstile.ts) returns a fresh token on every call.
545
+ *
546
+ * Deduped: a solve already in flight (from setOpen) is awaited rather than
547
+ * started again, so opening and immediately sending costs one round trip.
548
+ */
549
+ private ensureSessionToken;
550
+ /**
551
+ * Waits for a session token, but only briefly.
552
+ *
553
+ * The token TTL is short (server default 600s) and `hasValidSessionToken` refreshes
554
+ * 60s early, so a visitor who reads a reply for ~9 minutes and then types needs a
555
+ * fresh Turnstile solve. Awaiting that outright put up to CHALLENGE_TIMEOUT_MS (15s)
556
+ * of dead air between pressing send and the request leaving. A missing token is
557
+ * soft-allowed server-side (see widgetSessionAuthMiddleware), so past this deadline
558
+ * it is strictly better to send unauthenticated than to make the user wait — the
559
+ * solve keeps running and lands in time for the next turn.
560
+ */
561
+ private awaitSessionToken;
520
562
  private sendNonStreaming;
521
563
  private sendStreaming;
522
564
  }
@@ -667,6 +709,8 @@ declare interface WidgetRequestOptions {
667
709
  preview?: boolean;
668
710
  /** Which agent config the server should serve. See AgentKitWidgetConfig.configChannel. */
669
711
  configChannel?: 'draft' | 'published';
712
+ /** Signed widget session token from POST /v1/widget/session, sent as x-widget-session-token. */
713
+ sessionToken?: string;
670
714
  }
671
715
 
672
716
  export declare function WidgetShell({ config, messages, isLoading, error, isInline, initStatus, initError, callbacks, }: Props_2): JSX.Element;