dewee-webchat 0.3.0 → 0.4.1-beta.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.
@@ -0,0 +1,6 @@
1
+ import { D as DeweeIdentitySdk } from './handler-DunrbnSD.js';
2
+ import './protocol-CLbGE9Ns.js';
3
+
4
+ declare const deweeIdentitySdk: DeweeIdentitySdk;
5
+
6
+ export { DeweeIdentitySdk, deweeIdentitySdk };
@@ -0,0 +1,6 @@
1
+ // src/server/identity.ts
2
+ import { createAssertion, exchangeSession, startLink } from "@dewee/identity";
3
+ var deweeIdentitySdk = { exchangeSession, startLink, createAssertion };
4
+ export {
5
+ deweeIdentitySdk
6
+ };
package/dist/server.d.ts CHANGED
@@ -1,74 +1,7 @@
1
- import { A as AgentInfo, H as HistoryMessage, C as ChatStreamEvent, a as ActionManifestEntry } from './protocol-B72Ry-fY.js';
2
- export { b as ActionSensitivity, c as ChatRequestBody, P as PublicConfig, S as SessionResponse } from './protocol-B72Ry-fY.js';
1
+ export { A as AuthenticatedUser, C as CcpError, a as CcpOptions, b as DeweeHandler, c as DeweeHandlerOptions, d as DeweeIdentityOptions, D as DeweeIdentitySdk, e as DeweeTransport, I as IdentityPrivateJwk, f as IdentitySigner, M as MemoryRateLimits, R as RateLimitBucket, g as RateLimiter, V as VisitorClaims, h as createDeweeHandler, m as memoryRateLimiter, o as optionsFromEnv, s as signVisitorToken, v as verifyVisitorToken } from './handler-DunrbnSD.js';
3
2
  import { IncomingMessage, ServerResponse } from 'node:http';
4
-
5
- interface VisitorClaims {
6
- /** Visitor id (random, 22 chars). */
7
- v: string;
8
- /** Expiry, epoch ms. */
9
- exp: number;
10
- }
11
- declare function signVisitorToken(secret: string, claims: VisitorClaims): Promise<string>;
12
- declare function verifyVisitorToken(secret: string, token: string): Promise<VisitorClaims | null>;
13
- /** Returns true when the key may proceed. Implementations may be async (KV, DO, Redis…). */
14
- type RateLimiter = (key: string, bucket: "session" | "chat") => boolean | Promise<boolean>;
15
- /**
16
- * Fixed-window in-memory limiter. Good enough for a single Node process; on serverless
17
- * runtimes each isolate keeps its own counters, so pass a shared limiter for hard limits.
18
- */
19
- declare function memoryRateLimiter(limits: {
20
- chat: [number, number];
21
- session: [number, number];
22
- }): RateLimiter;
23
-
24
- interface DeweeHandlerOptions {
25
- /** Gateway runtime origin. Required unless every request brings its own credentials. */
26
- gatewayUrl?: string;
27
- /** Gateway token or operator API key (server-side only). */
28
- token?: string;
29
- tenantId?: string;
30
- /** HMAC secret for visitor tokens (>= 32 random chars). */
31
- secret?: string;
32
- /** Agents visitors may talk to. The first is the default. `"*"` = every active agent. */
33
- agents?: Array<string | AgentInfo> | "*";
34
- defaultAgent?: string;
35
- /** Let the widget switch between allowed agents. Default: true when more than one agent. */
36
- allowAgentSwitch?: boolean;
37
- /** Cross-origin sites allowed to call this backend. Same-origin is always allowed. */
38
- allowedOrigins?: string[] | "*";
39
- /**
40
- * Let visitors bring their own gateway URL + token (sent per request, never stored).
41
- * Meant for playgrounds. `allowedHosts` restricts which gateways may be targeted.
42
- */
43
- allowClientCredentials?: boolean | {
44
- allowedHosts?: string[];
45
- };
46
- /** Cloudflare Turnstile. When set, issuing a visitor session requires a valid challenge. */
47
- turnstile?: {
48
- siteKey: string;
49
- secretKey: string;
50
- };
51
- /** Custom limiter, or false to disable. Default: in-memory 20 msgs/min, 10 sessions/10 min per IP. */
52
- rateLimit?: RateLimiter | false;
53
- maxMessageLength?: number;
54
- /** Prefix for the per-visitor gateway user id. */
55
- userIdPrefix?: string;
56
- sessionTtlMs?: number;
57
- /** Max duration of one agent run. */
58
- timeoutMs?: number;
59
- /** Observe traffic (logging/analytics). Errors thrown here are ignored. */
60
- onChat?: (info: {
61
- visitorId: string;
62
- agent: string;
63
- message: string;
64
- reply?: string;
65
- error?: string;
66
- }) => void;
67
- }
68
- type Handler = (request: Request) => Promise<Response>;
69
- declare function createDeweeHandler(options: DeweeHandlerOptions): Handler;
70
- /** Reads options from environment variables (process.env, Workers env, import.meta.env…). */
71
- declare function optionsFromEnv(env: Record<string, string | undefined>, overrides?: Partial<DeweeHandlerOptions>): DeweeHandlerOptions;
3
+ import { R as Remediation, H as HistoryMessage, A as AgentInfo, C as ChatStreamEvent, a as ActionManifestEntry, b as AdmissionErrorCode } from './protocol-CLbGE9Ns.js';
4
+ export { c as ADMISSION_ERROR_CODES, d as ActionSensitivity, e as ChatRequestBody, E as ErrorBody, I as IdentityErrorCode, L as LinkResponse, f as LinkTarget, P as PublicConfig, S as SessionResponse, i as isAdmissionErrorCode } from './protocol-CLbGE9Ns.js';
72
5
 
73
6
  type FetchHandler = (request: Request) => Promise<Response>;
74
7
  /** Wraps a fetch-style handler for `http.createServer`, Express (`app.all("/api/dewee/*", ...)`), etc. */
@@ -85,9 +18,19 @@ interface GatewayCredentials {
85
18
  /** Optional tenant id/slug for multi-tenant gateways. */
86
19
  tenantId?: string;
87
20
  }
21
+ /**
22
+ * Who a connection acts as: a gateway user id chosen by this server (anonymous visitors), or a Dewee
23
+ * identity session, in which case the gateway derives the user and no `user_id` is sent.
24
+ */
25
+ type GatewayActor = string | {
26
+ identitySession: string;
27
+ };
88
28
  declare class GatewayError extends Error {
89
29
  code?: string | undefined;
90
- constructor(message: string, code?: string | undefined);
30
+ remediation?: Remediation | undefined;
31
+ retryAfterMs?: unknown | undefined;
32
+ /** `retryAfterMs` is the gateway's raw value for retryable refusals (`RATE_LIMITED`), unvalidated. */
33
+ constructor(message: string, code?: string | undefined, remediation?: Remediation | undefined, retryAfterMs?: unknown | undefined);
91
34
  }
92
35
  /** One authenticated gateway connection. Short-lived: one per chat turn or lookup. */
93
36
  declare class GatewayConnection {
@@ -97,7 +40,7 @@ declare class GatewayConnection {
97
40
  private listeners;
98
41
  private closed;
99
42
  private constructor();
100
- static connect(creds: GatewayCredentials, userId: string, timeoutMs?: number): Promise<GatewayConnection>;
43
+ static connect(creds: GatewayCredentials, actor: GatewayActor, timeoutMs?: number): Promise<GatewayConnection>;
101
44
  call<T = any>(method: string, params: unknown, timeoutMs?: number): Promise<T>;
102
45
  onEvent(cb: (event: string, payload: any) => void): () => void;
103
46
  close(): void;
@@ -107,7 +50,12 @@ declare class GatewayConnection {
107
50
  declare function buildSessionKey(agent: string, conversationId: string): string;
108
51
  interface StreamChatOptions {
109
52
  creds: GatewayCredentials;
110
- userId: string;
53
+ /** Gateway user id (anonymous visitors). Ignored when `identitySession` or `connection` is set. */
54
+ userId?: string;
55
+ /** Dewee identity session: the gateway derives the user; no `user_id` is sent. */
56
+ identitySession?: string;
57
+ /** An already connected connection to use (and close) instead of opening one. */
58
+ connection?: GatewayConnection;
111
59
  agent: string;
112
60
  sessionKey: string;
113
61
  message: string;
@@ -117,7 +65,7 @@ interface StreamChatOptions {
117
65
  /** Runs one chat turn and yields normalized stream events until the run ends. */
118
66
  declare function streamChat(opts: StreamChatOptions): AsyncGenerator<ChatStreamEvent>;
119
67
  /** Fetches a session transcript and folds tool calls/results into assistant turns. */
120
- declare function fetchHistory(creds: GatewayCredentials, userId: string, agent: string, sessionKey: string): Promise<HistoryMessage[]>;
68
+ declare function fetchHistory(creds: GatewayCredentials, actor: GatewayActor, agent: string, sessionKey: string): Promise<HistoryMessage[]>;
121
69
  declare function foldHistory(raw: any[]): HistoryMessage[];
122
70
  /** Lists agents via the gateway REST API. */
123
71
  declare function listAgents(creds: GatewayCredentials): Promise<AgentInfo[]>;
@@ -146,4 +94,14 @@ declare function buildPageBlock(p: PageBlockInput): string;
146
94
  /** Removes a leading page block from a stored user message (history display). */
147
95
  declare function stripPageBlock(content: string): string;
148
96
 
149
- export { ACTION_LIMITS, ActionManifestEntry, AgentInfo, ChatStreamEvent, type DeweeHandlerOptions, GatewayConnection, type GatewayCredentials, GatewayError, HistoryMessage, type RateLimiter, type StreamChatOptions, buildPageBlock, buildSessionKey, createDeweeHandler, fetchHistory, foldHistory, hashManifest, listAgents, memoryRateLimiter, normalizeManifest, optionsFromEnv, signVisitorToken, streamChat, stripPageBlock, toNodeHandler, verifyVisitorToken };
97
+ /** Cooldown when a `RATE_LIMITED` refusal carries no usable `retryAfterMs`. */
98
+ declare const DEFAULT_RETRY_AFTER_MS = 5000;
99
+ /** Upper bound of the cooldown passed to the browser, so a bogus value cannot lock the widget for long. */
100
+ declare const MAX_RETRY_AFTER_MS = 600000;
101
+ type AdmissionErrorEvent = Extract<ChatStreamEvent, {
102
+ type: "error";
103
+ }>;
104
+ /** The SSE `error` event of an admission refusal. Only `RATE_LIMITED` carries `retryAfterMs`. */
105
+ declare function admissionErrorEvent(code: AdmissionErrorCode, retryAfterMs?: unknown, gatewayReason?: string): AdmissionErrorEvent;
106
+
107
+ export { ACTION_LIMITS, ActionManifestEntry, AdmissionErrorCode, AgentInfo, ChatStreamEvent, DEFAULT_RETRY_AFTER_MS, type GatewayActor, GatewayConnection, type GatewayCredentials, GatewayError, HistoryMessage, MAX_RETRY_AFTER_MS, Remediation, type StreamChatOptions, admissionErrorEvent, buildPageBlock, buildSessionKey, fetchHistory, foldHistory, hashManifest, listAgents, normalizeManifest, streamChat, stripPageBlock, toNodeHandler };