@waniwani/sdk 0.10.11 → 0.11.1-beta.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.
@@ -0,0 +1,406 @@
1
+ import { UIMessage } from 'ai';
2
+ import { ContentBlock } from '@modelcontextprotocol/sdk/types.js';
3
+
4
+ interface SearchResult {
5
+ source: string;
6
+ heading: string;
7
+ content: string;
8
+ score: number;
9
+ metadata?: Record<string, string>;
10
+ }
11
+ /** A file to ingest into the knowledge base */
12
+ interface KbIngestFile {
13
+ /** Filename (used as chunk source identifier) */
14
+ filename: string;
15
+ /** Markdown content of the file */
16
+ content: string;
17
+ /** Arbitrary key-value metadata attached to all chunks from this file */
18
+ metadata?: Record<string, string>;
19
+ }
20
+ /** Response from the ingest endpoint */
21
+ interface KbIngestResult {
22
+ /** Number of chunks created from the ingested files */
23
+ chunksIngested: number;
24
+ /** Number of files successfully processed */
25
+ filesProcessed: number;
26
+ }
27
+ /** Options for the search method */
28
+ interface KbSearchOptions {
29
+ /** Number of results to return (1-20, default 5) */
30
+ topK?: number;
31
+ /** Minimum similarity score threshold (0-1, default 0.3) */
32
+ minScore?: number;
33
+ /** Filter results to chunks whose metadata contains all these key-value pairs (exact match) */
34
+ metadata?: Record<string, string>;
35
+ }
36
+ /** A source entry in the knowledge base */
37
+ interface KbSource {
38
+ /** Source filename */
39
+ source: string;
40
+ /** Number of chunks from this source */
41
+ chunkCount: number;
42
+ /** ISO timestamp of when the source was first ingested */
43
+ createdAt: string;
44
+ }
45
+ /** KB client for server-side knowledge base operations */
46
+ interface KbClient {
47
+ /**
48
+ * Ingest files into the knowledge base.
49
+ *
50
+ * **Warning**: This is destructive — it deletes ALL existing chunks
51
+ * for the environment before ingesting the new files.
52
+ */
53
+ ingest(files: KbIngestFile[]): Promise<KbIngestResult>;
54
+ /** Search the knowledge base for relevant chunks */
55
+ search(query: string, options?: KbSearchOptions): Promise<SearchResult[]>;
56
+ /** List all sources in the knowledge base */
57
+ sources(): Promise<KbSource[]>;
58
+ }
59
+
60
+ type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified" | "flow.node_reached";
61
+ interface ToolCalledProperties {
62
+ name?: string;
63
+ type?: "pricing" | "product_info" | "availability" | "support" | "other";
64
+ }
65
+ interface QuoteSucceededProperties {
66
+ amount?: number;
67
+ currency?: string;
68
+ }
69
+ interface LinkClickedProperties {
70
+ url?: string;
71
+ }
72
+ interface PurchaseCompletedProperties {
73
+ amount?: number;
74
+ currency?: string;
75
+ }
76
+ interface TrackingContext {
77
+ /**
78
+ * MCP request metadata passed through to the API.
79
+ *
80
+ * Location varies by MCP library:
81
+ * - `@vercel/mcp-handler`: `extra._meta`
82
+ * - `@modelcontextprotocol/sdk`: `request.params._meta`
83
+ */
84
+ meta?: Record<string, unknown>;
85
+ /** Legacy metadata field supported for backward compatibility. */
86
+ metadata?: Record<string, unknown>;
87
+ /** Optional explicit correlation fields. */
88
+ sessionId?: string;
89
+ traceId?: string;
90
+ requestId?: string;
91
+ correlationId?: string;
92
+ externalUserId?: string;
93
+ /** Optional explicit envelope fields. */
94
+ eventId?: string;
95
+ timestamp?: string | Date;
96
+ source?: string;
97
+ }
98
+ /**
99
+ * Modern tracking shape (preferred).
100
+ */
101
+ interface BaseTrackEvent extends TrackingContext {
102
+ event: EventType;
103
+ properties?: Record<string, unknown>;
104
+ }
105
+ type TrackEvent = ({
106
+ event: "session.started";
107
+ } & BaseTrackEvent) | ({
108
+ event: "tool.called";
109
+ properties?: ToolCalledProperties;
110
+ } & BaseTrackEvent) | ({
111
+ event: "quote.requested";
112
+ } & BaseTrackEvent) | ({
113
+ event: "quote.succeeded";
114
+ properties?: QuoteSucceededProperties;
115
+ } & BaseTrackEvent) | ({
116
+ event: "quote.failed";
117
+ } & BaseTrackEvent) | ({
118
+ event: "link.clicked";
119
+ properties?: LinkClickedProperties;
120
+ } & BaseTrackEvent) | ({
121
+ event: "purchase.completed";
122
+ properties?: PurchaseCompletedProperties;
123
+ } & BaseTrackEvent) | ({
124
+ event: "user.identified";
125
+ } & BaseTrackEvent) | ({
126
+ event: "flow.node_reached";
127
+ properties?: {
128
+ flowId?: string;
129
+ nodeId?: string;
130
+ };
131
+ } & BaseTrackEvent);
132
+ /**
133
+ * Legacy tracking shape supported for existing integrations.
134
+ */
135
+ interface LegacyTrackEvent extends TrackingContext {
136
+ eventType: EventType;
137
+ properties?: Record<string, unknown>;
138
+ toolName?: string;
139
+ toolType?: ToolCalledProperties["type"];
140
+ quoteAmount?: number;
141
+ quoteCurrency?: string;
142
+ linkUrl?: string;
143
+ purchaseAmount?: number;
144
+ purchaseCurrency?: string;
145
+ }
146
+ /**
147
+ * Public track input accepted by `client.track()`.
148
+ */
149
+ type TrackInput = TrackEvent | LegacyTrackEvent;
150
+ interface TrackingConfig {
151
+ /** Events API V2 endpoint path. */
152
+ endpointPath?: string;
153
+ /** Periodic flush interval for buffered events. */
154
+ flushIntervalMs?: number;
155
+ /** Max events per HTTP batch send. */
156
+ maxBatchSize?: number;
157
+ /** Max in-memory buffer size before oldest items are dropped. */
158
+ maxBufferSize?: number;
159
+ /** Number of retries for retryable failures. */
160
+ maxRetries?: number;
161
+ /** Retry backoff base delay. */
162
+ retryBaseDelayMs?: number;
163
+ /** Retry backoff max delay. */
164
+ retryMaxDelayMs?: number;
165
+ /** Default shutdown timeout when none is provided. */
166
+ shutdownTimeoutMs?: number;
167
+ }
168
+ interface TrackingShutdownOptions {
169
+ timeoutMs?: number;
170
+ }
171
+ interface TrackingShutdownResult {
172
+ timedOut: boolean;
173
+ pendingEvents: number;
174
+ }
175
+ /**
176
+ * Tracking module methods for WaniWaniClient.
177
+ */
178
+ interface TrackingClient {
179
+ /**
180
+ * Send a one-shot identify event for a user.
181
+ * userId can be any string: an email, an internal ID, etc.
182
+ */
183
+ identify: (userId: string, properties?: Record<string, unknown>, meta?: Record<string, unknown>) => Promise<{
184
+ eventId: string;
185
+ }>;
186
+ /**
187
+ * Track an event using modern or legacy input shape.
188
+ * Returns a deterministic event id immediately after enqueue.
189
+ */
190
+ track: (event: TrackInput) => Promise<{
191
+ eventId: string;
192
+ }>;
193
+ /**
194
+ * Flush all currently buffered events.
195
+ */
196
+ flush: () => Promise<void>;
197
+ /**
198
+ * Flush and stop the transport.
199
+ */
200
+ shutdown: (options?: TrackingShutdownOptions) => Promise<TrackingShutdownResult>;
201
+ }
202
+
203
+ /**
204
+ * WaniWani SDK Client
205
+ *
206
+ * Extends with each module:
207
+ * - TrackingClient: track(), flush(), shutdown()
208
+ *
209
+ * Pass this client to framework adapters:
210
+ * - `toNextJsHandler(wani, { ... })` for Next.js route handlers
211
+ */
212
+ interface WaniWaniClient extends TrackingClient {
213
+ /** @internal Resolved config — used by framework adapters */
214
+ readonly _config: InternalConfig;
215
+ /** Knowledge base client for ingestion, search, and source listing */
216
+ readonly kb: KbClient;
217
+ }
218
+ interface InternalConfig {
219
+ apiUrl: string;
220
+ apiKey: string | undefined;
221
+ tracking: Required<TrackingConfig>;
222
+ }
223
+
224
+ type ModelContextContentBlock = ContentBlock;
225
+ type ModelContextUpdate = {
226
+ content?: ModelContextContentBlock[];
227
+ structuredContent?: Record<string, unknown>;
228
+ };
229
+
230
+ /**
231
+ * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).
232
+ * All fields are optional — in local dev, no headers are present.
233
+ */
234
+ interface GeoLocation {
235
+ city?: string;
236
+ country?: string;
237
+ countryRegion?: string;
238
+ latitude?: string;
239
+ longitude?: string;
240
+ timezone?: string;
241
+ ip?: string;
242
+ }
243
+
244
+ /** Client-side visitor context sent in the request body */
245
+ interface ClientVisitorContext {
246
+ timezone: string;
247
+ language: string;
248
+ languages: string[];
249
+ deviceType: "mobile" | "tablet" | "desktop";
250
+ referrer: string;
251
+ visitorId: string;
252
+ }
253
+ /** Combined visitor context: server geo + client context */
254
+ interface VisitorMeta {
255
+ geo: GeoLocation;
256
+ client: ClientVisitorContext | null;
257
+ }
258
+ interface BeforeRequestContext {
259
+ /** The conversation messages from the client */
260
+ messages: UIMessage[];
261
+ /** Session identifier for conversation continuity */
262
+ sessionId?: string;
263
+ /** Hidden widget-provided model context for the next assistant turn */
264
+ modelContext?: ModelContextUpdate;
265
+ /** The original HTTP Request object */
266
+ request: Request;
267
+ /** Server-extracted geo location + client-provided visitor context */
268
+ visitor: VisitorMeta;
269
+ }
270
+ type BeforeRequestResult = {
271
+ /** Override messages (e.g., filtered, augmented) */
272
+ messages?: UIMessage[];
273
+ /** Override the system prompt for this request */
274
+ systemPrompt?: string;
275
+ /** Override sessionId */
276
+ sessionId?: string;
277
+ /** Override hidden widget-provided model context */
278
+ modelContext?: ModelContextUpdate;
279
+ };
280
+ interface WebSearchConfig {
281
+ /** Restrict web search results to these domains */
282
+ includeDomains?: string[];
283
+ /** Exclude these domains from web search results */
284
+ excludeDomains?: string[];
285
+ }
286
+ interface ChatOptions {
287
+ /**
288
+ * System prompt for the assistant.
289
+ * Can be overridden per-request via `beforeRequest`.
290
+ */
291
+ systemPrompt?: string;
292
+ /**
293
+ * Maximum number of tool call steps. Defaults to 5.
294
+ */
295
+ maxSteps?: number;
296
+ /**
297
+ * Hook called before each request is forwarded to the WaniWani API.
298
+ * - Return void to use defaults.
299
+ * - Return an object to override messages, systemPrompt, or sessionId.
300
+ * - Throw to reject the request (the error message is returned as JSON).
301
+ */
302
+ beforeRequest?: (context: BeforeRequestContext) => Promise<BeforeRequestResult | undefined> | BeforeRequestResult | undefined;
303
+ /**
304
+ * Override the MCP server URL directly, bypassing config resolution.
305
+ * Useful for development/testing when pointing to a local MCP server.
306
+ */
307
+ mcpServerUrl?: string;
308
+ /**
309
+ * Enable web search as an additional tool alongside MCP tools.
310
+ * Pass `true` to enable with defaults, or a config object to restrict domains.
311
+ */
312
+ webSearch?: boolean | WebSearchConfig;
313
+ }
314
+
315
+ interface ExpressJsHandlerOptions {
316
+ /** Chat handler configuration */
317
+ chat?: ChatOptions;
318
+ /**
319
+ * Identifies this chatbar instance in analytics.
320
+ * Use a descriptive name like "hamilton-support" or "pricing-page".
321
+ * Shows up as `source` on tracked events.
322
+ */
323
+ source: string;
324
+ /**
325
+ * Enable verbose debug logging for all handler steps.
326
+ * Logs request details, response codes, resolved URLs, and caught errors.
327
+ */
328
+ debug?: boolean;
329
+ }
330
+ /**
331
+ * Express middleware signatures. Typed against `unknown` to avoid forcing
332
+ * `@types/express` on consumers that don't use Express. Cast at the call site
333
+ * if your IDE doesn't infer correctly.
334
+ */
335
+ type ExpressMiddleware = (req: ExpressLikeRequest, res: ExpressLikeResponse, next: (err?: unknown) => void) => void;
336
+ /** Subset of Express's Request that the adapter actually reads. */
337
+ interface ExpressLikeRequest {
338
+ method: string;
339
+ url: string;
340
+ originalUrl?: string;
341
+ headers: Record<string, string | string[] | undefined>;
342
+ protocol?: string;
343
+ get?: (name: string) => string | undefined;
344
+ on: (event: string, listener: (...args: unknown[]) => void) => void;
345
+ }
346
+ /** Subset of Express's Response that the adapter actually writes to. */
347
+ interface ExpressLikeResponse {
348
+ statusCode: number;
349
+ setHeader: (name: string, value: string | number | readonly string[]) => void;
350
+ status: (code: number) => ExpressLikeResponse;
351
+ end: (chunk?: unknown, encoding?: BufferEncoding) => void;
352
+ write: (chunk: unknown, encoding?: BufferEncoding) => boolean;
353
+ on: (event: string, listener: (...args: unknown[]) => void) => void;
354
+ }
355
+ interface ExpressJsHandlerResult {
356
+ /** GET handler: routes sub-paths (e.g. /resource for MCP widget content) */
357
+ get: ExpressMiddleware;
358
+ /** POST handler: proxies chat messages to the WaniWani API */
359
+ post: ExpressMiddleware;
360
+ /** PATCH handler: routes updates (e.g. /scenarios/:id) */
361
+ patch: ExpressMiddleware;
362
+ /** OPTIONS handler: CORS preflight (no `next` argument needed) */
363
+ options: (req: ExpressLikeRequest, res: ExpressLikeResponse) => void;
364
+ }
365
+
366
+ /**
367
+ * Create Express middleware from a WaniWani client.
368
+ *
369
+ * Returns `{ get, post, patch, options }` — each is an Express-compatible
370
+ * middleware (`(req, res, next) => void`). Mount on a router/path of your choice:
371
+ *
372
+ * ```ts
373
+ * import express from "express";
374
+ * import { waniwani } from "@waniwani/sdk";
375
+ * import { toExpressJsHandler } from "@waniwani/sdk/express-js";
376
+ *
377
+ * const wani = waniwani();
378
+ * const handler = toExpressJsHandler(wani, {
379
+ * source: "my-app",
380
+ * chat: { mcpServerUrl: process.env.MCP_SERVER_URL },
381
+ * });
382
+ *
383
+ * const app = express();
384
+ * // IMPORTANT: do NOT use express.json() on these routes — the adapter reads
385
+ * // the raw request body stream itself.
386
+ * app.get("/api/waniwani/*", handler.get);
387
+ * app.post("/api/waniwani", handler.post);
388
+ * app.patch("/api/waniwani/*", handler.patch);
389
+ * app.options("/api/waniwani/*", handler.options);
390
+ * ```
391
+ */
392
+ declare function toExpressJsHandler(client: WaniWaniClient, options: ExpressJsHandlerOptions): ExpressJsHandlerResult;
393
+ /**
394
+ * Build a Web `Request` from an Express request, preserving method, headers,
395
+ * URL, and body stream. Body is exposed as a `ReadableStream` so the underlying
396
+ * handlers can call `await request.text()` / `.json()` / pipe through.
397
+ */
398
+ declare function expressToWebRequest(req: ExpressLikeRequest): Request;
399
+ /**
400
+ * Pipe a Web `Response` to an Express response. Status, headers, and body
401
+ * (including streaming bodies) are forwarded. Resolves once the body has been
402
+ * fully written and the response ended.
403
+ */
404
+ declare function sendWebResponse(webRes: Response, res: ExpressLikeResponse): Promise<void>;
405
+
406
+ export { type ChatOptions, type ExpressJsHandlerOptions, type ExpressJsHandlerResult, type ExpressLikeRequest, type ExpressLikeResponse, type ExpressMiddleware, expressToWebRequest, sendWebResponse, toExpressJsHandler };
@@ -0,0 +1,10 @@
1
+ import{Readable as K}from"stream";function k(t,e){return e?(...n)=>console.log(`[waniwani:${t}]`,...n):()=>{}}function W(t){if(t===!0)return{};if(!(t===!1||t===void 0))return t}function q(){return function(e){return e.headers.set("Access-Control-Allow-Origin","*"),e.headers.set("Access-Control-Allow-Methods","GET, POST, PATCH, OPTIONS"),e.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),e.headers.set("Access-Control-Expose-Headers","X-Session-Id"),e}}function B(t){return function(n,l){return t(new Response(JSON.stringify(n),{headers:{"Content-Type":"application/json"},status:l}))}}var P=class extends Error{constructor(n,l){super(n);this.status=l;this.name="WaniWaniError"}};function N(t){let e=t.headers,n=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,l=n?Y(n):void 0,r=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,a=e.get("x-vercel-ip-country-region")??void 0,u=e.get("x-vercel-ip-latitude")??e.get("cf-iplatitude")??void 0,i=e.get("x-vercel-ip-longitude")??e.get("cf-iplongitude")??void 0,o=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,d=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:l,country:r,countryRegion:a,latitude:u,longitude:i,timezone:o,ip:d}}function Y(t){try{return decodeURIComponent(t)}catch{return t}}function O(t){if(!t)return!1;let e=Array.isArray(t.content)&&t.content.length>0,n=typeof t.structuredContent=="object"&&t.structuredContent!==null&&Object.keys(t.structuredContent).length>0;return e||n}function $(t){if(!O(t))return"";let e=["## Widget Model Context","This hidden context was supplied by an MCP App via `ui/update-model-context`.","Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly."];if(t.content?.length){let n=t.content.map(l=>l.type==="text"&&typeof l.text=="string"?l.text.trim():JSON.stringify(l,null,2)).filter(Boolean).join(`
2
+
3
+ `);n&&e.push(`Content blocks:
4
+ ${n}`)}return t.structuredContent&&Object.keys(t.structuredContent).length>0&&e.push(`Structured content JSON:
5
+ ${JSON.stringify(t.structuredContent,null,2)}`),e.join(`
6
+
7
+ `)}function _(t,e){if(!O(e))return t;let n=$(e);return n?[t,n].filter(Boolean).join(`
8
+
9
+ `):t}function D(t){let{apiKey:e,apiUrl:n,source:l,systemPrompt:r,maxSteps:a,beforeRequest:u,mcpServerUrl:i,resolveConfig:o,debug:d,webSearch:s}=t,m=k("chat",d);return async function(g){m("\u2192 POST",g.url);try{let C=await g.json(),M=C.messages??[],w=C.sessionId,v=C.modelContext,b=r,I=C.visitorContext??null,E=N(g),H={geo:E,client:I};if(m("body parsed \u2014 messages:",M.length,"sessionId:",w??"(none)","geo:",JSON.stringify(E)),u){m("running beforeRequest hook");try{let p=await u({messages:M,sessionId:w,modelContext:v,request:g,visitor:H});p&&(p.messages&&(M=p.messages),p.systemPrompt!==void 0&&(b=p.systemPrompt),p.sessionId!==void 0&&(w=p.sessionId),p.modelContext!==void 0&&(v=p.modelContext)),m("beforeRequest hook done \u2014 messages:",M.length,"sessionId:",w??"(none)")}catch(p){console.error("[waniwani:chat] beforeRequest hook error:",p);let S=p instanceof P?p.status:400,A=p instanceof Error?p.message:"Request rejected";return m("\u2190 returning",S,"from hook error"),Response.json({error:A},{status:S})}}let j=i??(await o()).mcpServerUrl;m("mcpServerUrl:",j),b=_(b,v);let R=`${n}/api/mcp/chat`;m("forwarding to",R);let f=g.headers.get("user-agent"),h=await fetch(R,{method:"POST",headers:{"Content-Type":"application/json","X-WaniWani-Stream-Protocol":"2",...e?{Authorization:`Bearer ${e}`}:{},...f?{"X-Client-User-Agent":f}:{}},body:JSON.stringify({messages:M,mcpServerUrl:j,sessionId:w,source:l,systemPrompt:b,maxSteps:a,visitor:H,webSearch:s}),signal:g.signal});if(m("upstream response status:",h.status),!h.ok){let p=await h.text().catch(()=>"");return m("\u2190 returning",h.status,"upstream error:",p),new Response(p,{status:h.status,headers:{"Content-Type":h.headers.get("Content-Type")??"application/json"}})}let y=new Headers({"Content-Type":h.headers.get("Content-Type")??"text/event-stream"}),x=h.headers.get("x-session-id");return x&&y.set("x-session-id",x),m("\u2190 streaming response",h.status,"body null?",h.body===null),new Response(h.body,{status:h.status,headers:y})}catch(C){console.error("[waniwani:chat] handler error:",C);let M=C instanceof Error?C.message:"Unknown error occurred",w=C instanceof P?C.status:500;return m("\u2190 returning",w,"from caught error"),Response.json({error:M},{status:w})}}}function J(t){let{mcpServerUrl:e,resolveConfig:n,debug:l}=t,r=k("resource",l);return async function(u){r("\u2192 GET",u.toString());try{let i=u.searchParams.get("uri");if(r("uri:",i??"(missing)"),!i)return r("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let o=e??(await n()).mcpServerUrl;r("mcpServerUrl:",o);let d,s;try{[{createMCPClient:d},{StreamableHTTPClientTransport:s}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),r("MCP deps loaded")}catch(c){return console.error("[waniwani:resource] MCP deps import failed:",c),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}r("creating MCP client for",o);let m=await d({transport:new s(new URL(o))});try{r("reading resource:",i);let c=await m.readResource({uri:i});r("resource contents count:",c.contents.length);let g=c.contents[0];if(!g)return r("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let C;return"text"in g&&typeof g.text=="string"?C=g.text:"blob"in g&&typeof g.blob=="string"&&(C=atob(g.blob)),C?(r("\u2190 200 HTML length:",C.length),new Response(C,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(r("\u2190 404 resource has no content, keys:",Object.keys(g)),Response.json({error:"Resource has no content"},{status:404}))}finally{await m.close(),r("MCP client closed")}}catch(i){console.error("[waniwani:resource] handler error:",i);let o=i instanceof Error?i.message:"Unknown error occurred",d=i instanceof P?i.status:500;return r("\u2190 returning",d,"from caught error"),Response.json({error:o},{status:d})}}}function G(t){let{mcpServerUrl:e,resolveConfig:n,debug:l,source:r}=t,a=k("tool",l);return async function(i){a("\u2192 POST",i.url);try{let o=await i.json(),{name:d,arguments:s}=o,m=i.headers.get("x-session-id")?.trim();if(!d||typeof d!="string")return a("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});a("tool:",d,"args:",JSON.stringify(s),"sessionId:",m||"(none)");let c=e??(await n()).mcpServerUrl;a("mcpServerUrl:",c);let g,C;try{[{Client:g},{StreamableHTTPClientTransport:C}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),a("MCP deps loaded")}catch(v){return console.error("[waniwani:tool] MCP deps import failed:",v),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}a("creating MCP client for",c);let M=new C(new URL(c)),w=new g({name:"waniwani-tool-caller",version:"1.0.0"});await w.connect(M);try{a("calling tool:",d);let v={};m&&(v["waniwani/sessionId"]=m),r&&(v["waniwani/source"]=r);let b=await w.callTool({name:d,arguments:s??{},...Object.keys(v).length>0?{_meta:v}:{}});return a("tool result received"),Response.json({content:b.content,structuredContent:b.structuredContent,_meta:b._meta,isError:b.isError})}finally{await w.close(),a("MCP client closed")}}catch(o){console.error("[waniwani:tool] handler error:",o);let d=o instanceof Error?o.message:"Unknown error occurred",s=o instanceof P?o.status:500;return a("\u2190 returning",s,"from caught error"),Response.json({error:d},{status:s})}}}function F(t){let{mcpServerUrl:e,resolveConfig:n,debug:l}=t,r=k("tools-list",l);return async function(){r("\u2192 GET tools/list");try{let u=e??(await n()).mcpServerUrl;r("mcpServerUrl:",u);let i,o;try{[{createMCPClient:i},{StreamableHTTPClientTransport:o}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),r("MCP deps loaded")}catch(s){return console.error("[waniwani:tools-list] MCP deps import failed:",s),Response.json({error:"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery."},{status:501})}r("creating MCP client for",u);let d=await i({transport:new o(new URL(u))});try{r("listing tools");let s=await d.listTools();r("tools count:",s.tools.length);let m={tools:s.tools.map(c=>({name:c.name,...c.title!==void 0&&{title:c.title},...c.description!==void 0&&{description:c.description},...c.inputSchema!==void 0&&{inputSchema:c.inputSchema},...c.outputSchema!==void 0&&{outputSchema:c.outputSchema},...c.annotations!==void 0&&{annotations:c.annotations},...c._meta!==void 0&&{_meta:c._meta}})),...s.nextCursor!==void 0&&{nextCursor:s.nextCursor}};return r("\u2190 200"),Response.json(m,{headers:{"Cache-Control":"private, max-age=60"}})}finally{await d.close(),r("MCP client closed")}}catch(u){console.error("[waniwani:tools-list] handler error:",u);let i=u instanceof Error?u.message:"Unknown error occurred",o=u instanceof P?u.status:500;return r("\u2190 returning",o,"from caught error"),Response.json({error:i},{status:o})}}}var Q=300*1e3;function z(t,e){let n=null,l=null;return async function(){if(n&&Date.now()<n.expiresAt)return n.config;if(l)return l;l=(async()=>{if(!e)throw new P("WANIWANI_API_KEY is required for createChatHandler",401);let a=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!a.ok){let i=await a.text().catch(()=>"");throw new P(`Failed to resolve MCP environment config: ${a.status} ${i}`,a.status)}let u=await a.json();return n={config:u,expiresAt:Date.now()+Q},u})();try{return await l}finally{l=null}}}var Z="https://app.waniwani.ai";function V(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:n=Z,source:l,systemPrompt:r,maxSteps:a=5,beforeRequest:u,mcpServerUrl:i,debug:o=!1,webSearch:d}=t,s=k("router",o),m=q(),c=B(m),g=z(n,e),C=D({apiKey:e,apiUrl:n,source:l,systemPrompt:r,maxSteps:a,beforeRequest:u,mcpServerUrl:i,resolveConfig:g,debug:o,webSearch:W(d)}),M=J({mcpServerUrl:i,resolveConfig:g,debug:o}),w=F({mcpServerUrl:i,resolveConfig:g,debug:o}),v=G({mcpServerUrl:i,resolveConfig:g,debug:o,source:l}),b=process.env.WANIWANI_EVAL==="1";async function I(R){s("\u2192 GET",R.url);try{let f=new URL(R.url),y=f.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(s("pathname:",f.pathname,"subRoute:",y),b&&y==="scenarios"){s("dispatching to scenarios handler (proxy to app API)");try{let p=await(await fetch(`${n}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return c(p.data??p,200)}catch{return c([],200)}}if(y==="resource"){s("dispatching to resource handler");let x=await M(f);return s("\u2190 resource handler returned",x.status),m(x)}if(y==="config")return s("dispatching to config handler"),c({debug:o,eval:b},200);if(y==="tools"){s("dispatching to tools-list handler");let x=await w();return s("\u2190 tools-list handler returned",x.status),m(x)}return s("\u2190 404 no matching sub-route for",y),c({error:"Not found"},404)}catch(f){console.error("[waniwani:router] GET handler error:",f);let h=f instanceof Error?f.message:"Unknown error occurred";return s("\u2190 500 from caught error"),c({error:h},500)}}async function E(R){s("\u2192 POST",R.url);try{let f=new URL(R.url),y=f.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(s("pathname:",f.pathname,"subRoute:",y),b&&y==="scenarios"){s("dispatching to save-scenario handler (proxy to app API)");try{let p=await R.json(),S=await fetch(`${n}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(p)}),A=await S.json();return S.ok?c({ok:!0,scenario:A.data},200):c({error:A.message??"Failed to save scenario"},S.status)}catch(p){let S=p instanceof Error?p.message:"Failed to save scenario";return c({error:S},400)}}if(y==="tool"){s("dispatching to tool handler");let p=await v(R);return s("\u2190 tool handler returned",p.status),m(p)}s("dispatching to chat handler");let x=await C(R);return m(x)}catch(f){console.error("[waniwani:router] POST handler error:",f);let h=f instanceof Error?f.message:"Unknown error occurred";return s("\u2190 500 from caught error"),c({error:h},500)}}async function H(R){s("\u2192 PATCH",R.url);try{let f=new URL(R.url),h=f.pathname.replace(/\/$/,"").split("/").filter(Boolean),y=h.at(-1),x=h.at(-2);if(s("pathname:",f.pathname,"subRoute:",x,"id:",y),b&&x==="scenarios"&&y){s("dispatching to update-scenario handler (proxy to app API)");try{let p=await R.json(),S=await fetch(`${n}/api/mcp/scenarios/${y}`,{method:"PATCH",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(p)}),A=await S.json();return S.ok?c({ok:!0,scenario:A.data},200):c({error:A.message??"Failed to update scenario"},S.status)}catch(p){let S=p instanceof Error?p.message:"Failed to update scenario";return c({error:S},400)}}return s("\u2190 404 no matching sub-route for PATCH",x),c({error:"Not found"},404)}catch(f){console.error("[waniwani:router] PATCH handler error:",f);let h=f instanceof Error?f.message:"Unknown error occurred";return s("\u2190 500 from caught error"),c({error:h},500)}}function j(){return m(new Response(null,{status:204}))}return{handleChat:C,handleResource:M,handleTool:v,routeGet:I,routePost:E,routePatch:H,handleOptions:j}}function Ke(t,e){let{apiKey:n,apiUrl:l}=t._config,r=e?.debug??process.env.WANIWANI_DEBUG==="1",a=V({...e?.chat,apiKey:n,apiUrl:l,source:e?.source,debug:r});return{get:(u,i,o)=>L(a.routeGet,u,i,o),post:(u,i,o)=>L(a.routePost,u,i,o),patch:(u,i,o)=>L(a.routePatch,u,i,o),options:(u,i)=>{X(a.handleOptions(),i).catch(o=>{console.error("[waniwani:express] OPTIONS handler error:",o)})}}}async function L(t,e,n,l){try{let r=ee(e),a=await t(r);await X(a,n)}catch(r){l(r)}}function ee(t){let e=t.protocol??(typeof t.get=="function"?t.get("x-forwarded-proto"):void 0)??"http",n=(typeof t.get=="function"?t.get("host"):t.headers.host)??"localhost",l=t.originalUrl??t.url,r=new URL(l,`${e}://${n}`).toString(),a=new Headers;for(let[o,d]of Object.entries(t.headers))if(d!==void 0)if(Array.isArray(d))for(let s of d)a.append(o,s);else a.set(o,d);let u={method:t.method,headers:a},i=t.method.toUpperCase();if(i!=="GET"&&i!=="HEAD"){let o=t;u.body=K.toWeb(o),u.duplex="half"}return new Request(r,u)}async function X(t,e){if(e.statusCode=t.status,t.headers.forEach((l,r)=>{e.setHeader(r,l)}),!t.body){e.end();return}let n=K.fromWeb(t.body);await new Promise((l,r)=>{n.on("data",a=>{e.write(a)}),n.on("end",()=>{e.end(),l()}),n.on("error",a=>{r(a)})})}export{ee as expressToWebRequest,X as sendWebResponse,Ke as toExpressJsHandler};
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/chat/server/express-js/index.ts","../../../src/utils/logger.ts","../../../src/chat/server/@types.ts","../../../src/chat/server/@utils.ts","../../../src/error.ts","../../../src/chat/server/geo.ts","../../../src/shared/model-context.ts","../../../src/chat/server/model-context.ts","../../../src/chat/server/handle-chat.ts","../../../src/chat/server/handle-resource.ts","../../../src/chat/server/handle-tool.ts","../../../src/chat/server/handle-tools-list.ts","../../../src/chat/server/mcp-config-resolver.ts","../../../src/chat/server/api-handler.ts"],"sourcesContent":["// WaniWani SDK - Express Adapter\n\nimport { Readable } from \"node:stream\";\nimport type { WaniWaniClient } from \"../../../types.js\";\nimport { createApiHandler } from \"../api-handler.js\";\nimport type {\n\tExpressJsHandlerOptions,\n\tExpressJsHandlerResult,\n\tExpressLikeRequest,\n\tExpressLikeResponse,\n} from \"./@types.js\";\n\nexport type {\n\tChatOptions,\n\tExpressJsHandlerOptions,\n\tExpressJsHandlerResult,\n\tExpressLikeRequest,\n\tExpressLikeResponse,\n\tExpressMiddleware,\n} from \"./@types.js\";\n\n/**\n * Create Express middleware from a WaniWani client.\n *\n * Returns `{ get, post, patch, options }` — each is an Express-compatible\n * middleware (`(req, res, next) => void`). Mount on a router/path of your choice:\n *\n * ```ts\n * import express from \"express\";\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toExpressJsHandler } from \"@waniwani/sdk/express-js\";\n *\n * const wani = waniwani();\n * const handler = toExpressJsHandler(wani, {\n * source: \"my-app\",\n * chat: { mcpServerUrl: process.env.MCP_SERVER_URL },\n * });\n *\n * const app = express();\n * // IMPORTANT: do NOT use express.json() on these routes — the adapter reads\n * // the raw request body stream itself.\n * app.get(\"/api/waniwani/*\", handler.get);\n * app.post(\"/api/waniwani\", handler.post);\n * app.patch(\"/api/waniwani/*\", handler.patch);\n * app.options(\"/api/waniwani/*\", handler.options);\n * ```\n */\nexport function toExpressJsHandler(\n\tclient: WaniWaniClient,\n\toptions: ExpressJsHandlerOptions,\n): ExpressJsHandlerResult {\n\tconst { apiKey, apiUrl } = client._config;\n\tconst debugEnabled = options?.debug ?? process.env.WANIWANI_DEBUG === \"1\";\n\n\tconst handler = createApiHandler({\n\t\t...options?.chat,\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource: options?.source,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tget: (req, res, next) => adapt(handler.routeGet, req, res, next),\n\t\tpost: (req, res, next) => adapt(handler.routePost, req, res, next),\n\t\tpatch: (req, res, next) => adapt(handler.routePatch, req, res, next),\n\t\toptions: (_req, res) => {\n\t\t\tvoid sendWebResponse(handler.handleOptions(), res).catch((err) => {\n\t\t\t\tconsole.error(\"[waniwani:express] OPTIONS handler error:\", err);\n\t\t\t});\n\t\t},\n\t};\n}\n\nasync function adapt(\n\tfn: (request: Request) => Promise<Response>,\n\treq: ExpressLikeRequest,\n\tres: ExpressLikeResponse,\n\tnext: (err?: unknown) => void,\n): Promise<void> {\n\ttry {\n\t\tconst webRequest = expressToWebRequest(req);\n\t\tconst webResponse = await fn(webRequest);\n\t\tawait sendWebResponse(webResponse, res);\n\t} catch (err) {\n\t\tnext(err);\n\t}\n}\n\n/**\n * Build a Web `Request` from an Express request, preserving method, headers,\n * URL, and body stream. Body is exposed as a `ReadableStream` so the underlying\n * handlers can call `await request.text()` / `.json()` / pipe through.\n */\nexport function expressToWebRequest(req: ExpressLikeRequest): Request {\n\tconst protocol =\n\t\treq.protocol ??\n\t\t(typeof req.get === \"function\"\n\t\t\t? req.get(\"x-forwarded-proto\")\n\t\t\t: undefined) ??\n\t\t\"http\";\n\tconst host =\n\t\t(typeof req.get === \"function\"\n\t\t\t? req.get(\"host\")\n\t\t\t: (req.headers.host as string | undefined)) ?? \"localhost\";\n\tconst path = req.originalUrl ?? req.url;\n\tconst url = new URL(path, `${protocol}://${host}`).toString();\n\n\tconst headers = new Headers();\n\tfor (const [key, value] of Object.entries(req.headers)) {\n\t\tif (value === undefined) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\theaders.append(key, v);\n\t\t\t}\n\t\t} else {\n\t\t\theaders.set(key, value);\n\t\t}\n\t}\n\n\tconst init: RequestInit = { method: req.method, headers };\n\n\tconst method = req.method.toUpperCase();\n\tif (method !== \"GET\" && method !== \"HEAD\") {\n\t\t// Convert Node Readable → Web ReadableStream so fetch's Request can\n\t\t// consume it. Cast to Node Readable: ExpressLikeRequest's `on` matches.\n\t\tconst nodeStream = req as unknown as Readable;\n\t\tinit.body = Readable.toWeb(\n\t\t\tnodeStream,\n\t\t) as unknown as ReadableStream<Uint8Array>;\n\t\t// Fetch spec requires duplex: 'half' when sending a streaming body.\n\t\t(init as RequestInit & { duplex: \"half\" }).duplex = \"half\";\n\t}\n\n\treturn new Request(url, init);\n}\n\n/**\n * Pipe a Web `Response` to an Express response. Status, headers, and body\n * (including streaming bodies) are forwarded. Resolves once the body has been\n * fully written and the response ended.\n */\nexport async function sendWebResponse(\n\twebRes: Response,\n\tres: ExpressLikeResponse,\n): Promise<void> {\n\tres.statusCode = webRes.status;\n\twebRes.headers.forEach((value, key) => {\n\t\tres.setHeader(key, value);\n\t});\n\n\tif (!webRes.body) {\n\t\tres.end();\n\t\treturn;\n\t}\n\n\tconst nodeReadable = Readable.fromWeb(\n\t\twebRes.body as unknown as import(\"node:stream/web\").ReadableStream<Uint8Array>,\n\t);\n\n\tawait new Promise<void>((resolve, reject) => {\n\t\tnodeReadable.on(\"data\", (chunk: Uint8Array | Buffer | string) => {\n\t\t\tres.write(chunk);\n\t\t});\n\t\tnodeReadable.on(\"end\", () => {\n\t\t\tres.end();\n\t\t\tresolve();\n\t\t});\n\t\tnodeReadable.on(\"error\", (err) => {\n\t\t\treject(err);\n\t\t});\n\t});\n}\n","/**\n * Creates a namespaced logger that writes to console.log when enabled,\n * or is a no-op when disabled.\n *\n * @example\n * const log = createLogger(\"chat\", debug);\n * log(\"→ POST\", request.url); // [waniwani:chat] → POST ...\n */\nexport function createLogger(\n\tnamespace: string,\n\tenabled: boolean,\n): (...args: unknown[]) => void {\n\treturn enabled\n\t\t? (...args: unknown[]) => console.log(`[waniwani:${namespace}]`, ...args)\n\t\t: () => {};\n}\n","// WaniWani SDK - Chat Server Types\n\nimport type { UIMessage } from \"ai\";\nimport type { ModelContextUpdate } from \"../../shared/model-context\";\nimport type { GeoLocation } from \"./geo\";\n\n// ============================================================================\n// Visitor Context\n// ============================================================================\n\n/** Client-side visitor context sent in the request body */\nexport interface ClientVisitorContext {\n\ttimezone: string;\n\tlanguage: string;\n\tlanguages: string[];\n\tdeviceType: \"mobile\" | \"tablet\" | \"desktop\";\n\treferrer: string;\n\tvisitorId: string;\n}\n\n/** Combined visitor context: server geo + client context */\nexport interface VisitorMeta {\n\tgeo: GeoLocation;\n\tclient: ClientVisitorContext | null;\n}\n\n// ============================================================================\n// Before Request Hook\n// ============================================================================\n\nexport interface BeforeRequestContext {\n\t/** The conversation messages from the client */\n\tmessages: UIMessage[];\n\t/** Session identifier for conversation continuity */\n\tsessionId?: string;\n\t/** Hidden widget-provided model context for the next assistant turn */\n\tmodelContext?: ModelContextUpdate;\n\t/** The original HTTP Request object */\n\trequest: Request;\n\t/** Server-extracted geo location + client-provided visitor context */\n\tvisitor: VisitorMeta;\n}\n\nexport type BeforeRequestResult = {\n\t/** Override messages (e.g., filtered, augmented) */\n\tmessages?: UIMessage[];\n\t/** Override the system prompt for this request */\n\tsystemPrompt?: string;\n\t/** Override sessionId */\n\tsessionId?: string;\n\t/** Override hidden widget-provided model context */\n\tmodelContext?: ModelContextUpdate;\n};\n\n// ============================================================================\n// Web Search\n// ============================================================================\n\nexport interface WebSearchConfig {\n\t/** Restrict web search results to these domains */\n\tincludeDomains?: string[];\n\t/** Exclude these domains from web search results */\n\texcludeDomains?: string[];\n}\n\n// ============================================================================\n// Chat Options (namespaced under `chat` in framework adapters)\n// ============================================================================\n\nexport interface ChatOptions {\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Options\n// ============================================================================\n\nexport interface ApiHandlerOptions {\n\t/**\n\t * Identifies this chatbar instance in analytics.\n\t * Forwarded as `waniwani/source` in MCP request metadata.\n\t */\n\tsource?: string;\n\n\t/**\n\t * Your WaniWani API key.\n\t * Defaults to process.env.WANIWANI_API_KEY.\n\t */\n\tapiKey?: string;\n\n\t/**\n\t * The base URL of the WaniWani API.\n\t * Defaults to https://app.waniwani.ai.\n\t */\n\tapiUrl?: string;\n\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable verbose debug logging for all handler steps.\n\t * Logs request details, response codes, resolved URLs, and caught errors.\n\t */\n\tdebug?: boolean;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Result\n// ============================================================================\n\nexport interface ApiHandler {\n\t/** Proxies chat messages to the WaniWani API */\n\thandleChat: (request: Request) => Promise<Response>;\n\t/** Serves MCP resource content (HTML widgets) */\n\thandleResource: (url: URL) => Promise<Response>;\n\t/** Calls an MCP server tool and returns JSON */\n\thandleTool: (request: Request) => Promise<Response>;\n\t/** Routes GET sub-paths (e.g. /resource) */\n\trouteGet: (request: Request) => Promise<Response>;\n\t/** Routes POST sub-paths (e.g. /tool), defaults to chat */\n\troutePost: (request: Request) => Promise<Response>;\n\t/** Routes PATCH sub-paths (e.g. /scenarios/:id) */\n\troutePatch: (request: Request) => Promise<Response>;\n\t/** Handles CORS preflight requests */\n\thandleOptions: () => Response;\n}\n\n// ============================================================================\n// Internal Dependencies (shared across sub-handlers)\n// ============================================================================\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\ntype ConfigResolver = () => Promise<McpEnvironmentConfig>;\n\nexport interface ApiHandlerDeps {\n\tapiKey: string | undefined;\n\tapiUrl: string;\n\tsource: string | undefined;\n\tsystemPrompt: string | undefined;\n\tmaxSteps: number;\n\tbeforeRequest: ApiHandlerOptions[\"beforeRequest\"];\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\twebSearch?: WebSearchConfig;\n}\n\n/** Normalize `true` to `{}` so the upstream API always receives an object or undefined */\nexport function resolveWebSearchConfig(\n\tvalue: boolean | WebSearchConfig | undefined,\n): WebSearchConfig | undefined {\n\tif (value === true) {\n\t\treturn {};\n\t}\n\tif (value === false || value === undefined) {\n\t\treturn undefined;\n\t}\n\treturn value;\n}\n\nexport interface ResourceHandlerDeps {\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\tsource?: string;\n}\n","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response) => Response;\n\nexport function createCors(): CorsFunction {\n\treturn function applyCors(response: Response): Response {\n\t\tresponse.headers.set(\"Access-Control-Allow-Origin\", \"*\");\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Methods\",\n\t\t\t\"GET, POST, PATCH, OPTIONS\",\n\t\t);\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Headers\",\n\t\t\t\"Content-Type, Authorization, X-Session-Id, X-Client-User-Agent\",\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Expose-Headers\", \"X-Session-Id\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(data: object, status: number): Response {\n\t\treturn cors(\n\t\t\tnew Response(JSON.stringify(data), {\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tstatus,\n\t\t\t}),\n\t\t);\n\t};\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// Geo — Extract location metadata from platform request headers\n\n/**\n * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).\n * All fields are optional — in local dev, no headers are present.\n */\nexport interface GeoLocation {\n\tcity?: string;\n\tcountry?: string;\n\tcountryRegion?: string;\n\tlatitude?: string;\n\tlongitude?: string;\n\ttimezone?: string;\n\tip?: string;\n}\n\n/**\n * Extracts geolocation from server-side request headers.\n *\n * Supports Vercel (`x-vercel-ip-*`), Cloudflare (`cf-ip*`, `cf-connecting-ip`),\n * and generic IP headers (`x-real-ip`, `x-forwarded-for`).\n *\n * Returns a `GeoLocation` with all fields optional (empty object in local dev).\n */\nexport function extractGeoFromHeaders(request: Request): GeoLocation {\n\tconst h = request.headers;\n\n\t// Vercel URL-encodes city names (e.g. \"S%C3%A3o%20Paulo\")\n\tconst rawCity = h.get(\"x-vercel-ip-city\") ?? h.get(\"cf-ipcity\") ?? undefined;\n\tconst city = rawCity ? safeDecodeURI(rawCity) : undefined;\n\n\tconst country =\n\t\th.get(\"x-vercel-ip-country\") ?? h.get(\"cf-ipcountry\") ?? undefined;\n\tconst countryRegion = h.get(\"x-vercel-ip-country-region\") ?? undefined;\n\tconst latitude =\n\t\th.get(\"x-vercel-ip-latitude\") ?? h.get(\"cf-iplatitude\") ?? undefined;\n\tconst longitude =\n\t\th.get(\"x-vercel-ip-longitude\") ?? h.get(\"cf-iplongitude\") ?? undefined;\n\tconst timezone =\n\t\th.get(\"x-vercel-ip-timezone\") ?? h.get(\"cf-iptimezone\") ?? undefined;\n\tconst ip =\n\t\th.get(\"x-real-ip\") ??\n\t\th.get(\"x-forwarded-for\")?.split(\",\")[0]?.trim() ??\n\t\th.get(\"cf-connecting-ip\") ??\n\t\tundefined;\n\n\treturn { city, country, countryRegion, latitude, longitude, timezone, ip };\n}\n\nfunction safeDecodeURI(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n","import type { ContentBlock } from \"@modelcontextprotocol/sdk/types.js\";\n\nexport type ModelContextContentBlock = ContentBlock;\n\nexport type ModelContextUpdate = {\n\tcontent?: ModelContextContentBlock[];\n\tstructuredContent?: Record<string, unknown>;\n};\n\nexport function hasModelContext(\n\tvalue: ModelContextUpdate | null | undefined,\n): value is ModelContextUpdate {\n\tif (!value) {\n\t\treturn false;\n\t}\n\tconst hasContent = Array.isArray(value.content) && value.content.length > 0;\n\tconst hasStructuredContent =\n\t\ttypeof value.structuredContent === \"object\" &&\n\t\tvalue.structuredContent !== null &&\n\t\tObject.keys(value.structuredContent).length > 0;\n\treturn hasContent || hasStructuredContent;\n}\n\nexport function mergeModelContext(\n\tcurrent: ModelContextUpdate | null | undefined,\n\tnext: ModelContextUpdate | null | undefined,\n): ModelContextUpdate | null {\n\tif (!hasModelContext(current) && !hasModelContext(next)) {\n\t\treturn null;\n\t}\n\tif (!hasModelContext(current)) {\n\t\treturn {\n\t\t\t...(next?.content ? { content: [...next.content] } : {}),\n\t\t\t...(next?.structuredContent\n\t\t\t\t? { structuredContent: { ...next.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (!hasModelContext(next)) {\n\t\treturn {\n\t\t\t...(current.content ? { content: [...current.content] } : {}),\n\t\t\t...(current.structuredContent\n\t\t\t\t? { structuredContent: { ...current.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\n\treturn {\n\t\t...(current.content || next.content\n\t\t\t? { content: [...(current.content ?? []), ...(next.content ?? [])] }\n\t\t\t: {}),\n\t\t...(current.structuredContent || next.structuredContent\n\t\t\t? {\n\t\t\t\t\tstructuredContent: {\n\t\t\t\t\t\t...(current.structuredContent ?? {}),\n\t\t\t\t\t\t...(next.structuredContent ?? {}),\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\nexport function formatModelContextForPrompt(\n\tvalue: ModelContextUpdate | null | undefined,\n): string {\n\tif (!hasModelContext(value)) {\n\t\treturn \"\";\n\t}\n\n\tconst sections: string[] = [\n\t\t\"## Widget Model Context\",\n\t\t\"This hidden context was supplied by an MCP App via `ui/update-model-context`.\",\n\t\t\"Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly.\",\n\t];\n\n\tif (value.content?.length) {\n\t\tconst renderedBlocks = value.content\n\t\t\t.map((block) => {\n\t\t\t\tif (block.type === \"text\" && typeof block.text === \"string\") {\n\t\t\t\t\treturn block.text.trim();\n\t\t\t\t}\n\t\t\t\treturn JSON.stringify(block, null, 2);\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\\n\");\n\t\tif (renderedBlocks) {\n\t\t\tsections.push(`Content blocks:\\n${renderedBlocks}`);\n\t\t}\n\t}\n\n\tif (\n\t\tvalue.structuredContent &&\n\t\tObject.keys(value.structuredContent).length > 0\n\t) {\n\t\tsections.push(\n\t\t\t`Structured content JSON:\\n${JSON.stringify(value.structuredContent, null, 2)}`,\n\t\t);\n\t}\n\n\treturn sections.join(\"\\n\\n\");\n}\n","import {\n\tformatModelContextForPrompt,\n\thasModelContext,\n\ttype ModelContextUpdate,\n} from \"../../shared/model-context\";\n\nexport function applyModelContextToSystemPrompt(\n\tsystemPrompt: string | undefined,\n\tmodelContext: ModelContextUpdate | undefined,\n): string | undefined {\n\tif (!hasModelContext(modelContext)) {\n\t\treturn systemPrompt;\n\t}\n\n\tconst widgetContext = formatModelContextForPrompt(modelContext);\n\tif (!widgetContext) {\n\t\treturn systemPrompt;\n\t}\n\n\treturn [systemPrompt, widgetContext].filter(Boolean).join(\"\\n\\n\");\n}\n","// Handle Chat - Proxies chat requests to the WaniWani API\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type {\n\tApiHandlerDeps,\n\tClientVisitorContext,\n\tVisitorMeta,\n} from \"./@types\";\nimport { extractGeoFromHeaders } from \"./geo\";\nimport { applyModelContextToSystemPrompt } from \"./model-context\";\n\nexport function createChatRequestHandler(deps: ApiHandlerDeps) {\n\tconst {\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch,\n\t} = deps;\n\n\tconst log = createLogger(\"chat\", debug);\n\n\treturn async function handleChat(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\t// 1. Parse request body\n\t\t\tconst body = await request.json();\n\t\t\tlet messages = body.messages ?? [];\n\t\t\tlet sessionId: string | undefined = body.sessionId;\n\t\t\tlet modelContext = body.modelContext;\n\t\t\tlet effectiveSystemPrompt = systemPrompt;\n\n\t\t\t// Extract visitor context (client-side + server-side geo)\n\t\t\tconst clientVisitorContext: ClientVisitorContext | null =\n\t\t\t\tbody.visitorContext ?? null;\n\t\t\tconst geo = extractGeoFromHeaders(request);\n\t\t\tconst visitor: VisitorMeta = { geo, client: clientVisitorContext };\n\n\t\t\tlog(\n\t\t\t\t\"body parsed — messages:\",\n\t\t\t\tmessages.length,\n\t\t\t\t\"sessionId:\",\n\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\"geo:\",\n\t\t\t\tJSON.stringify(geo),\n\t\t\t);\n\n\t\t\t// 2. Run beforeRequest hook\n\t\t\tif (beforeRequest) {\n\t\t\t\tlog(\"running beforeRequest hook\");\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await beforeRequest({\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsessionId,\n\t\t\t\t\t\tmodelContext,\n\t\t\t\t\t\trequest,\n\t\t\t\t\t\tvisitor,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result) {\n\t\t\t\t\t\tif (result.messages) {\n\t\t\t\t\t\t\tmessages = result.messages;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.systemPrompt !== undefined) {\n\t\t\t\t\t\t\teffectiveSystemPrompt = result.systemPrompt;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.sessionId !== undefined) {\n\t\t\t\t\t\t\tsessionId = result.sessionId;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.modelContext !== undefined) {\n\t\t\t\t\t\t\tmodelContext = result.modelContext;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tlog(\n\t\t\t\t\t\t\"beforeRequest hook done — messages:\",\n\t\t\t\t\t\tmessages.length,\n\t\t\t\t\t\t\"sessionId:\",\n\t\t\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\t);\n\t\t\t\t} catch (hookError) {\n\t\t\t\t\tconsole.error(\"[waniwani:chat] beforeRequest hook error:\", hookError);\n\t\t\t\t\tconst status =\n\t\t\t\t\t\thookError instanceof WaniWaniError ? hookError.status : 400;\n\t\t\t\t\tconst message =\n\t\t\t\t\t\thookError instanceof Error ? hookError.message : \"Request rejected\";\n\t\t\t\t\tlog(\"← returning\", status, \"from hook error\");\n\t\t\t\t\treturn Response.json({ error: message }, { status });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 3. Resolve MCP server URL\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\t\t\teffectiveSystemPrompt = applyModelContextToSystemPrompt(\n\t\t\t\teffectiveSystemPrompt,\n\t\t\t\tmodelContext,\n\t\t\t);\n\n\t\t\t// 4. Forward to WaniWani API\n\t\t\tconst upstreamUrl = `${apiUrl}/api/mcp/chat`;\n\t\t\tlog(\"forwarding to\", upstreamUrl);\n\t\t\tconst clientUserAgent = request.headers.get(\"user-agent\");\n\n\t\t\tconst response = await fetch(upstreamUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"X-WaniWani-Stream-Protocol\": \"2\",\n\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t...(clientUserAgent\n\t\t\t\t\t\t? { \"X-Client-User-Agent\": clientUserAgent }\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmessages,\n\t\t\t\t\tmcpServerUrl,\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsource,\n\t\t\t\t\tsystemPrompt: effectiveSystemPrompt,\n\t\t\t\t\tmaxSteps,\n\t\t\t\t\tvisitor,\n\t\t\t\t\twebSearch,\n\t\t\t\t}),\n\t\t\t\tsignal: request.signal,\n\t\t\t});\n\n\t\t\tlog(\"upstream response status:\", response.status);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorBody = await response.text().catch(() => \"\");\n\t\t\t\tlog(\"← returning\", response.status, \"upstream error:\", errorBody);\n\t\t\t\treturn new Response(errorBody, {\n\t\t\t\t\tstatus: response.status,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\":\n\t\t\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 5. Stream the response back\n\t\t\tconst headers = new Headers({\n\t\t\t\t\"Content-Type\":\n\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"text/event-stream\",\n\t\t\t});\n\t\t\tconst upstreamSessionId = response.headers.get(\"x-session-id\");\n\t\t\tif (upstreamSessionId) {\n\t\t\t\theaders.set(\"x-session-id\", upstreamSessionId);\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"← streaming response\",\n\t\t\t\tresponse.status,\n\t\t\t\t\"body null?\",\n\t\t\t\tresponse.body === null,\n\t\t\t);\n\t\t\treturn new Response(response.body, {\n\t\t\t\tstatus: response.status,\n\t\t\t\theaders,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:chat] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Resource - Serves MCP resource content (HTML widgets)\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createResourceHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"resource\", debug);\n\n\treturn async function handleResource(url: URL): Promise<Response> {\n\t\tlog(\"→ GET\", url.toString());\n\t\ttry {\n\t\t\tconst uri = url.searchParams.get(\"uri\");\n\t\t\tlog(\"uri:\", uri ?? \"(missing)\");\n\n\t\t\tif (!uri) {\n\t\t\t\tlog(\"← 400 missing uri\");\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{ error: \"Missing uri query parameter\" },\n\t\t\t\t\t{ status: 400 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:resource] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"reading resource:\", uri);\n\t\t\t\tconst result = await mcp.readResource({ uri });\n\t\t\t\tlog(\"resource contents count:\", result.contents.length);\n\n\t\t\t\tconst content = result.contents[0];\n\t\t\t\tif (!content) {\n\t\t\t\t\tlog(\"← 404 resource not found\");\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource not found\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlet html: string | undefined;\n\t\t\t\tif (\"text\" in content && typeof content.text === \"string\") {\n\t\t\t\t\thtml = content.text;\n\t\t\t\t} else if (\"blob\" in content && typeof content.blob === \"string\") {\n\t\t\t\t\thtml = atob(content.blob);\n\t\t\t\t}\n\n\t\t\t\tif (!html) {\n\t\t\t\t\tlog(\"← 404 resource has no content, keys:\", Object.keys(content));\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource has no content\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlog(\"← 200 HTML length:\", html.length);\n\t\t\t\treturn new Response(html, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=300\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:resource] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tool - Calls MCP server tools directly and returns JSON results\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createToolHandler(deps: ResourceHandlerDeps) {\n\tconst {\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t} = deps;\n\n\tconst log = createLogger(\"tool\", debug);\n\n\treturn async function handleTool(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst body = await request.json();\n\t\t\tconst { name, arguments: args } = body as {\n\t\t\t\tname: string;\n\t\t\t\targuments: Record<string, unknown>;\n\t\t\t};\n\t\t\tconst requestSessionId = request.headers.get(\"x-session-id\")?.trim();\n\n\t\t\tif (!name || typeof name !== \"string\") {\n\t\t\t\tlog(\"← 400 missing tool name\");\n\t\t\t\treturn Response.json({ error: \"Missing tool name\" }, { status: 400 });\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"tool:\",\n\t\t\t\tname,\n\t\t\t\t\"args:\",\n\t\t\t\tJSON.stringify(args),\n\t\t\t\t\"sessionId:\",\n\t\t\t\trequestSessionId || \"(none)\",\n\t\t\t);\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet Client: typeof import(\"@modelcontextprotocol/sdk/client/index.js\")[\"Client\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ Client }, { StreamableHTTPClientTransport }] = await Promise.all([\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/index.js\"),\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\"[waniwani:tool] MCP deps import failed:\", importError);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst transport = new StreamableHTTPClientTransport(\n\t\t\t\tnew URL(mcpServerUrl),\n\t\t\t);\n\t\t\tconst client = new Client({\n\t\t\t\tname: \"waniwani-tool-caller\",\n\t\t\t\tversion: \"1.0.0\",\n\t\t\t});\n\t\t\tawait client.connect(transport);\n\n\t\t\ttry {\n\t\t\t\tlog(\"calling tool:\", name);\n\t\t\t\tconst _meta: Record<string, unknown> = {};\n\t\t\t\tif (requestSessionId) {\n\t\t\t\t\t_meta[\"waniwani/sessionId\"] = requestSessionId;\n\t\t\t\t}\n\t\t\t\tif (source) {\n\t\t\t\t\t_meta[\"waniwani/source\"] = source;\n\t\t\t\t}\n\t\t\t\tconst result = await client.callTool({\n\t\t\t\t\tname,\n\t\t\t\t\targuments: args ?? {},\n\t\t\t\t\t...(Object.keys(_meta).length > 0 ? { _meta } : {}),\n\t\t\t\t} as {\n\t\t\t\t\tname: string;\n\t\t\t\t\targuments: Record<string, unknown>;\n\t\t\t\t\t_meta?: Record<string, unknown>;\n\t\t\t\t});\n\t\t\t\tlog(\"tool result received\");\n\n\t\t\t\treturn Response.json({\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tstructuredContent: result.structuredContent,\n\t\t\t\t\t_meta: result._meta,\n\t\t\t\t\tisError: result.isError,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait client.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tool] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tools List - Returns the MCP server's tool catalog including per-tool\n// `_meta`, so the chat UI can cache it in the browser and resolve widget\n// binding (`_meta.ui.resourceUri`, `_meta[\"openai/outputTemplate\"]`, etc.) by\n// tool name at render time.\n//\n// This mirrors the pattern used by MCPJam's `/api/web/tools/list` route and\n// matches the MCP Apps spec (\"hosts identify UI-enabled tools through\n// `_meta.ui.resourceUri` metadata on the tool definition\"). Kept as an\n// ephemeral operation — one MCP client per HTTP request — so it works on\n// serverless platforms without sticky sessions.\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\n/** Shape returned to the browser. Matches the MCP `tools/list` response. */\nexport interface HandleToolsListResponse {\n\ttools: Array<{\n\t\tname: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t\tinputSchema?: unknown;\n\t\toutputSchema?: unknown;\n\t\tannotations?: unknown;\n\t\t_meta?: Record<string, unknown>;\n\t}>;\n\tnextCursor?: string;\n}\n\nexport function createToolsListHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"tools-list\", debug);\n\n\treturn async function handleToolsList(): Promise<Response> {\n\t\tlog(\"→ GET tools/list\");\n\t\ttry {\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:tools-list] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"listing tools\");\n\t\t\t\tconst result = await mcp.listTools();\n\t\t\t\tlog(\"tools count:\", result.tools.length);\n\n\t\t\t\tconst response: HandleToolsListResponse = {\n\t\t\t\t\ttools: result.tools.map((tool) => ({\n\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t...(tool.title !== undefined && { title: tool.title }),\n\t\t\t\t\t\t...(tool.description !== undefined && {\n\t\t\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.inputSchema !== undefined && {\n\t\t\t\t\t\t\tinputSchema: tool.inputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.outputSchema !== undefined && {\n\t\t\t\t\t\t\toutputSchema: tool.outputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.annotations !== undefined && {\n\t\t\t\t\t\t\tannotations: tool.annotations,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool._meta !== undefined && { _meta: tool._meta }),\n\t\t\t\t\t})),\n\t\t\t\t\t...(result.nextCursor !== undefined && {\n\t\t\t\t\t\tnextCursor: result.nextCursor,\n\t\t\t\t\t}),\n\t\t\t\t};\n\n\t\t\t\tlog(\"← 200\");\n\t\t\t\treturn Response.json(response, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t// Catalog can be safely cached briefly. Browsers get a fresh\n\t\t\t\t\t\t// copy per ChatCard mount but don't hammer the MCP server\n\t\t\t\t\t\t// when the user refreshes quickly.\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=60\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tools-list] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// MCP Config Resolver - Lazy-loads and caches MCP environment config\n\nimport { WaniWaniError } from \"../../error\";\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\nconst TTL_MS = 5 * 60 * 1000; // 5 minutes\n\nexport function createMcpConfigResolver(\n\tapiUrl: string,\n\tapiKey: string | undefined,\n) {\n\tlet cached: { config: McpEnvironmentConfig; expiresAt: number } | null = null;\n\tlet inflight: Promise<McpEnvironmentConfig> | null = null;\n\n\treturn async function resolve(): Promise<McpEnvironmentConfig> {\n\t\tif (cached && Date.now() < cached.expiresAt) {\n\t\t\treturn cached.config;\n\t\t}\n\n\t\t// Deduplicate concurrent requests (cold start scenario)\n\t\tif (inflight) {\n\t\t\treturn inflight;\n\t\t}\n\n\t\tinflight = (async () => {\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t\"WANIWANI_API_KEY is required for createChatHandler\",\n\t\t\t\t\t401,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst response = await fetch(`${apiUrl}/api/mcp/environments/config`, {\n\t\t\t\tmethod: \"GET\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t`Failed to resolve MCP environment config: ${response.status} ${body}`,\n\t\t\t\t\tresponse.status,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data = (await response.json()) as McpEnvironmentConfig;\n\t\t\tcached = { config: data, expiresAt: Date.now() + TTL_MS };\n\t\t\treturn data;\n\t\t})();\n\n\t\ttry {\n\t\t\treturn await inflight;\n\t\t} finally {\n\t\t\tinflight = null;\n\t\t}\n\t};\n}\n","// API Handler - Composes chat and resource handlers into a unified API handler\n\nimport { createLogger } from \"../../utils/logger.js\";\nimport {\n\ttype ApiHandler,\n\ttype ApiHandlerOptions,\n\tresolveWebSearchConfig,\n} from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createToolsListHandler } from \"./handle-tools-list\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\nconst DEFAULT_API_URL = \"https://app.waniwani.ai\";\n\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = DEFAULT_API_URL,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tdebug = false,\n\t\twebSearch,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors();\n\tconst json = createJsonResponse(cors);\n\n\tconst resolveConfig = createMcpConfigResolver(apiUrl, apiKey);\n\n\tconst handleChat = createChatRequestHandler({\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch: resolveWebSearchConfig(webSearch),\n\t});\n\n\tconst handleResource = createResourceHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleToolsList = createToolsListHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleTool = createToolHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t});\n\n\tconst evalEnabled = process.env.WANIWANI_EVAL === \"1\";\n\n\tasync function routeGet(request: Request): Promise<Response> {\n\t\tlog(\"→ GET\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to scenarios handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\treturn json(data.data ?? data, 200);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"resource\") {\n\t\t\t\tlog(\"dispatching to resource handler\");\n\t\t\t\tconst response = await handleResource(url);\n\t\t\t\tlog(\"← resource handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tif (subRoute === \"config\") {\n\t\t\t\tlog(\"dispatching to config handler\");\n\t\t\t\treturn json({ debug, eval: evalEnabled }, 200);\n\t\t\t}\n\n\t\t\tif (subRoute === \"tools\") {\n\t\t\t\tlog(\"dispatching to tools-list handler\");\n\t\t\t\tconst response = await handleToolsList();\n\t\t\t\tlog(\"← tools-list handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] GET handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePost(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to save-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to save scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to save scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"tool\") {\n\t\t\t\tlog(\"dispatching to tool handler\");\n\t\t\t\tconst response = await handleTool(request);\n\t\t\t\tlog(\"← tool handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\t// Default: treat as chat request\n\t\t\tlog(\"dispatching to chat handler\");\n\t\t\tconst chatResponse = await handleChat(request);\n\t\t\treturn cors(chatResponse);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] POST handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePatch(request: Request): Promise<Response> {\n\t\tlog(\"→ PATCH\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst scenarioId = segments.at(-1);\n\t\t\tconst subRoute = segments.at(-2);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute, \"id:\", scenarioId);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\" && scenarioId) {\n\t\t\t\tlog(\"dispatching to update-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios/${scenarioId}`, {\n\t\t\t\t\t\tmethod: \"PATCH\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to update scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to update scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for PATCH\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] PATCH handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tfunction handleOptions(): Response {\n\t\treturn cors(new Response(null, { status: 204 }));\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\n\t\troutePatch,\n\t\thandleOptions,\n\t};\n}\n"],"mappings":"AAEA,OAAS,YAAAA,MAAgB,SCMlB,SAASC,EACfC,EACAC,EAC+B,CAC/B,OAAOA,EACJ,IAAIC,IAAoB,QAAQ,IAAI,aAAaF,CAAS,IAAK,GAAGE,CAAI,EACtE,IAAM,CAAC,CACX,CC2MO,SAASC,EACfC,EAC8B,CAC9B,GAAIA,IAAU,GACb,MAAO,CAAC,EAET,GAAI,EAAAA,IAAU,IAASA,IAAU,QAGjC,OAAOA,CACR,CChOO,SAASC,GAA2B,CAC1C,OAAO,SAAmBC,EAA8B,CACvD,OAAAA,EAAS,QAAQ,IAAI,8BAA+B,GAAG,EACvDA,EAAS,QAAQ,IAChB,+BACA,2BACD,EACAA,EAAS,QAAQ,IAChB,+BACA,gEACD,EACAA,EAAS,QAAQ,IAAI,gCAAiC,cAAc,EAC7DA,CACR,CACD,CAEO,SAASC,EAAmBC,EAAoB,CACtD,OAAO,SAAcC,EAAcC,EAA0B,CAC5D,OAAOF,EACN,IAAI,SAAS,KAAK,UAAUC,CAAI,EAAG,CAClC,QAAS,CAAE,eAAgB,kBAAmB,EAC9C,OAAAC,CACD,CAAC,CACF,CACD,CACD,CC3BO,IAAMC,EAAN,cAA4B,KAAM,CACxC,YACCC,EACOC,EACN,CACD,MAAMD,CAAO,EAFN,YAAAC,EAGP,KAAK,KAAO,eACb,CACD,ECcO,SAASC,EAAsBC,EAA+B,CACpE,IAAMC,EAAID,EAAQ,QAGZE,EAAUD,EAAE,IA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,QAAAe,CAAQ,EAElDK,EAASpB,EAAI,OAAO,YAAY,EACtC,GAAIoB,IAAW,OAASA,IAAW,OAAQ,CAG1C,IAAMC,EAAarB,EACnBmB,EAAK,KAAOG,EAAS,MACpBD,CACD,EAECF,EAA0C,OAAS,MACrD,CAEA,OAAO,IAAI,QAAQL,EAAKK,CAAI,CAC7B,CAOA,eAAsBd,EACrBkB,EACAtB,EACgB,CAMhB,GALAA,EAAI,WAAasB,EAAO,OACxBA,EAAO,QAAQ,QAAQ,CAACN,EAAOD,IAAQ,CACtCf,EAAI,UAAUe,EAAKC,CAAK,CACzB,CAAC,EAEG,CAACM,EAAO,KAAM,CACjBtB,EAAI,IAAI,EACR,MACD,CAEA,IAAMuB,EAAeF,EAAS,QAC7BC,EAAO,IACR,EAEA,MAAM,IAAI,QAAc,CAACE,EAASC,IAAW,CAC5CF,EAAa,GAAG,OAASG,GAAwC,CAChE1B,EAAI,MAAM0B,CAAK,CAChB,CAAC,EACDH,EAAa,GAAG,MAAO,IAAM,CAC5BvB,EAAI,IAAI,EACRwB,EAAQ,CACT,CAAC,EACDD,EAAa,GAAG,QAAUlB,GAAQ,CACjCoB,EAAOpB,CAAG,CACX,CAAC,CACF,CAAC,CACF","names":["Readable","createLogger","namespace","enabled","args","resolveWebSearchConfig","value","createCors","response","createJsonResponse","cors","data","status","WaniWaniError","message","status","extractGeoFromHeaders","request","h","rawCity","city","safeDecodeURI","country","countryRegion","latitude","longitude","timezone","ip","value","hasModelContext","value","hasContent","hasStructuredContent","formatModelContextForPrompt","value","hasModelContext","sections","renderedBlocks","block","applyModelContextToSystemPrompt","systemPrompt","modelContext","hasModelContext","widgetContext","formatModelContextForPrompt","createChatRequestHandler","deps","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrlOverride","resolveConfig","debug","webSearch","log","createLogger","request","body","messages","sessionId","modelContext","effectiveSystemPrompt","clientVisitorContext","geo","extractGeoFromHeaders","visitor","result","hookError","status","WaniWaniError","message","mcpServerUrl","applyModelContextToSystemPrompt","upstreamUrl","clientUserAgent","response","errorBody","headers","upstreamSessionId","error","createResourceHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","url","uri","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","content","html","error","message","status","WaniWaniError","createToolHandler","deps","mcpServerUrlOverride","resolveConfig","debug","source","log","createLogger","request","body","name","args","requestSessionId","mcpServerUrl","Client","StreamableHTTPClientTransport","importError","transport","client","_meta","result","error","message","status","WaniWaniError","createToolsListHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","response","tool","error","message","status","WaniWaniError","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","debug","webSearch","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","resolveWebSearchConfig","handleResource","createResourceHandler","handleToolsList","createToolsListHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","routePatch","segments","scenarioId","handleOptions","toExpressJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler","req","res","next","adapt","_req","sendWebResponse","err","fn","webRequest","expressToWebRequest","webResponse","protocol","host","path","url","headers","key","value","v","init","method","nodeStream","Readable","webRes","nodeReadable","resolve","reject","chunk"]}
@@ -283,7 +283,6 @@ interface WebSearchConfig {
283
283
  /** Exclude these domains from web search results */
284
284
  excludeDomains?: string[];
285
285
  }
286
-
287
286
  interface ChatOptions {
288
287
  /**
289
288
  * System prompt for the assistant.
@@ -312,6 +311,7 @@ interface ChatOptions {
312
311
  */
313
312
  webSearch?: boolean | WebSearchConfig;
314
313
  }
314
+
315
315
  interface NextJsHandlerOptions {
316
316
  /** Chat handler configuration */
317
317
  chat?: ChatOptions;
@@ -347,6 +347,10 @@ interface NextJsHandlerResult {
347
347
  * - `POST /api/waniwani` → chat (proxied to WaniWani API)
348
348
  * - `GET /api/waniwani/resource?uri=…` → MCP resource content
349
349
  *
350
+ * @deprecated Use `toExpressJsHandler` from `@waniwani/sdk/express-js` instead.
351
+ * The Next.js-specific wrapper will be removed in a follow-up release. Both
352
+ * adapters share the same underlying `createApiHandler`, so behavior is identical.
353
+ *
350
354
  * @example
351
355
  * ```typescript
352
356
  * // app/api/waniwani/[[...path]]/route.ts
@@ -1,10 +1,10 @@
1
- function H(t,e){return e?(...o)=>console.log(`[waniwani:${t}]`,...o):()=>{}}function W(t){if(t===!0)return{};if(!(t===!1||t===void 0))return t}function L(){return function(e){return e.headers.set("Access-Control-Allow-Origin","*"),e.headers.set("Access-Control-Allow-Methods","GET, POST, PATCH, OPTIONS"),e.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),e.headers.set("Access-Control-Expose-Headers","X-Session-Id"),e}}function N(t){return function(o,u){return t(new Response(JSON.stringify(o),{headers:{"Content-Type":"application/json"},status:u}))}}var S=class extends Error{constructor(o,u){super(o);this.status=u;this.name="WaniWaniError"}};function q(t){let e=t.headers,o=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,u=o?V(o):void 0,s=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,l=e.get("x-vercel-ip-country-region")??void 0,m=e.get("x-vercel-ip-latitude")??e.get("cf-iplatitude")??void 0,a=e.get("x-vercel-ip-longitude")??e.get("cf-iplongitude")??void 0,c=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,h=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:u,country:s,countryRegion:l,latitude:m,longitude:a,timezone:c,ip:h}}function V(t){try{return decodeURIComponent(t)}catch{return t}}function O(t){if(!t)return!1;let e=Array.isArray(t.content)&&t.content.length>0,o=typeof t.structuredContent=="object"&&t.structuredContent!==null&&Object.keys(t.structuredContent).length>0;return e||o}function B(t){if(!O(t))return"";let e=["## Widget Model Context","This hidden context was supplied by an MCP App via `ui/update-model-context`.","Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly."];if(t.content?.length){let o=t.content.map(u=>u.type==="text"&&typeof u.text=="string"?u.text.trim():JSON.stringify(u,null,2)).filter(Boolean).join(`
1
+ function H(t,e){return e?(...o)=>console.log(`[waniwani:${t}]`,...o):()=>{}}function W(t){if(t===!0)return{};if(!(t===!1||t===void 0))return t}function N(){return function(e){return e.headers.set("Access-Control-Allow-Origin","*"),e.headers.set("Access-Control-Allow-Methods","GET, POST, PATCH, OPTIONS"),e.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),e.headers.set("Access-Control-Expose-Headers","X-Session-Id"),e}}function q(t){return function(o,u){return t(new Response(JSON.stringify(o),{headers:{"Content-Type":"application/json"},status:u}))}}var P=class extends Error{constructor(o,u){super(o);this.status=u;this.name="WaniWaniError"}};function L(t){let e=t.headers,o=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,u=o?K(o):void 0,s=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,l=e.get("x-vercel-ip-country-region")??void 0,m=e.get("x-vercel-ip-latitude")??e.get("cf-iplatitude")??void 0,a=e.get("x-vercel-ip-longitude")??e.get("cf-iplongitude")??void 0,c=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,h=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:u,country:s,countryRegion:l,latitude:m,longitude:a,timezone:c,ip:h}}function K(t){try{return decodeURIComponent(t)}catch{return t}}function O(t){if(!t)return!1;let e=Array.isArray(t.content)&&t.content.length>0,o=typeof t.structuredContent=="object"&&t.structuredContent!==null&&Object.keys(t.structuredContent).length>0;return e||o}function B(t){if(!O(t))return"";let e=["## Widget Model Context","This hidden context was supplied by an MCP App via `ui/update-model-context`.","Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly."];if(t.content?.length){let o=t.content.map(u=>u.type==="text"&&typeof u.text=="string"?u.text.trim():JSON.stringify(u,null,2)).filter(Boolean).join(`
2
2
 
3
3
  `);o&&e.push(`Content blocks:
4
4
  ${o}`)}return t.structuredContent&&Object.keys(t.structuredContent).length>0&&e.push(`Structured content JSON:
5
5
  ${JSON.stringify(t.structuredContent,null,2)}`),e.join(`
6
6
 
7
- `)}function $(t,e){if(!O(e))return t;let o=B(e);return o?[t,o].filter(Boolean).join(`
7
+ `)}function J(t,e){if(!O(e))return t;let o=B(e);return o?[t,o].filter(Boolean).join(`
8
8
 
9
- `):t}function J(t){let{apiKey:e,apiUrl:o,source:u,systemPrompt:s,maxSteps:l,beforeRequest:m,mcpServerUrl:a,resolveConfig:c,debug:h,webSearch:r}=t,p=H("chat",h);return async function(f){p("\u2192 POST",f.url);try{let C=await f.json(),M=C.messages??[],R=C.sessionId,T=C.modelContext,b=s,I=C.visitorContext??null,k=q(f),j={geo:k,client:I};if(p("body parsed \u2014 messages:",M.length,"sessionId:",R??"(none)","geo:",JSON.stringify(k)),m){p("running beforeRequest hook");try{let i=await m({messages:M,sessionId:R,modelContext:T,request:f,visitor:j});i&&(i.messages&&(M=i.messages),i.systemPrompt!==void 0&&(b=i.systemPrompt),i.sessionId!==void 0&&(R=i.sessionId),i.modelContext!==void 0&&(T=i.modelContext)),p("beforeRequest hook done \u2014 messages:",M.length,"sessionId:",R??"(none)")}catch(i){console.error("[waniwani:chat] beforeRequest hook error:",i);let P=i instanceof S?i.status:400,A=i instanceof Error?i.message:"Request rejected";return p("\u2190 returning",P,"from hook error"),Response.json({error:A},{status:P})}}let E=a??(await c()).mcpServerUrl;p("mcpServerUrl:",E),b=$(b,T);let w=`${o}/api/mcp/chat`;p("forwarding to",w);let d=f.headers.get("user-agent"),g=await fetch(w,{method:"POST",headers:{"Content-Type":"application/json","X-WaniWani-Stream-Protocol":"2",...e?{Authorization:`Bearer ${e}`}:{},...d?{"X-Client-User-Agent":d}:{}},body:JSON.stringify({messages:M,mcpServerUrl:E,sessionId:R,source:u,systemPrompt:b,maxSteps:l,visitor:j,webSearch:r}),signal:f.signal});if(p("upstream response status:",g.status),!g.ok){let i=await g.text().catch(()=>"");return p("\u2190 returning",g.status,"upstream error:",i),new Response(i,{status:g.status,headers:{"Content-Type":g.headers.get("Content-Type")??"application/json"}})}let y=new Headers({"Content-Type":g.headers.get("Content-Type")??"text/event-stream"}),x=g.headers.get("x-session-id");return x&&y.set("x-session-id",x),p("\u2190 streaming response",g.status,"body null?",g.body===null),new Response(g.body,{status:g.status,headers:y})}catch(C){console.error("[waniwani:chat] handler error:",C);let M=C instanceof Error?C.message:"Unknown error occurred",R=C instanceof S?C.status:500;return p("\u2190 returning",R,"from caught error"),Response.json({error:M},{status:R})}}}function _(t){let{mcpServerUrl:e,resolveConfig:o,debug:u}=t,s=H("resource",u);return async function(m){s("\u2192 GET",m.toString());try{let a=m.searchParams.get("uri");if(s("uri:",a??"(missing)"),!a)return s("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let c=e??(await o()).mcpServerUrl;s("mcpServerUrl:",c);let h,r;try{[{createMCPClient:h},{StreamableHTTPClientTransport:r}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(n){return console.error("[waniwani:resource] MCP deps import failed:",n),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}s("creating MCP client for",c);let p=await h({transport:new r(new URL(c))});try{s("reading resource:",a);let n=await p.readResource({uri:a});s("resource contents count:",n.contents.length);let f=n.contents[0];if(!f)return s("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let C;return"text"in f&&typeof f.text=="string"?C=f.text:"blob"in f&&typeof f.blob=="string"&&(C=atob(f.blob)),C?(s("\u2190 200 HTML length:",C.length),new Response(C,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(s("\u2190 404 resource has no content, keys:",Object.keys(f)),Response.json({error:"Resource has no content"},{status:404}))}finally{await p.close(),s("MCP client closed")}}catch(a){console.error("[waniwani:resource] handler error:",a);let c=a instanceof Error?a.message:"Unknown error occurred",h=a instanceof S?a.status:500;return s("\u2190 returning",h,"from caught error"),Response.json({error:c},{status:h})}}}function D(t){let{mcpServerUrl:e,resolveConfig:o,debug:u,source:s}=t,l=H("tool",u);return async function(a){l("\u2192 POST",a.url);try{let c=await a.json(),{name:h,arguments:r}=c,p=a.headers.get("x-session-id")?.trim();if(!h||typeof h!="string")return l("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});l("tool:",h,"args:",JSON.stringify(r),"sessionId:",p||"(none)");let n=e??(await o()).mcpServerUrl;l("mcpServerUrl:",n);let f,C;try{[{Client:f},{StreamableHTTPClientTransport:C}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),l("MCP deps loaded")}catch(T){return console.error("[waniwani:tool] MCP deps import failed:",T),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}l("creating MCP client for",n);let M=new C(new URL(n)),R=new f({name:"waniwani-tool-caller",version:"1.0.0"});await R.connect(M);try{l("calling tool:",h);let T={};p&&(T["waniwani/sessionId"]=p),s&&(T["waniwani/source"]=s);let b=await R.callTool({name:h,arguments:r??{},...Object.keys(T).length>0?{_meta:T}:{}});return l("tool result received"),Response.json({content:b.content,structuredContent:b.structuredContent,_meta:b._meta,isError:b.isError})}finally{await R.close(),l("MCP client closed")}}catch(c){console.error("[waniwani:tool] handler error:",c);let h=c instanceof Error?c.message:"Unknown error occurred",r=c instanceof S?c.status:500;return l("\u2190 returning",r,"from caught error"),Response.json({error:h},{status:r})}}}function G(t){let{mcpServerUrl:e,resolveConfig:o,debug:u}=t,s=H("tools-list",u);return async function(){s("\u2192 GET tools/list");try{let m=e??(await o()).mcpServerUrl;s("mcpServerUrl:",m);let a,c;try{[{createMCPClient:a},{StreamableHTTPClientTransport:c}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(r){return console.error("[waniwani:tools-list] MCP deps import failed:",r),Response.json({error:"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery."},{status:501})}s("creating MCP client for",m);let h=await a({transport:new c(new URL(m))});try{s("listing tools");let r=await h.listTools();s("tools count:",r.tools.length);let p={tools:r.tools.map(n=>({name:n.name,...n.title!==void 0&&{title:n.title},...n.description!==void 0&&{description:n.description},...n.inputSchema!==void 0&&{inputSchema:n.inputSchema},...n.outputSchema!==void 0&&{outputSchema:n.outputSchema},...n.annotations!==void 0&&{annotations:n.annotations},...n._meta!==void 0&&{_meta:n._meta}})),...r.nextCursor!==void 0&&{nextCursor:r.nextCursor}};return s("\u2190 200"),Response.json(p,{headers:{"Cache-Control":"private, max-age=60"}})}finally{await h.close(),s("MCP client closed")}}catch(m){console.error("[waniwani:tools-list] handler error:",m);let a=m instanceof Error?m.message:"Unknown error occurred",c=m instanceof S?m.status:500;return s("\u2190 returning",c,"from caught error"),Response.json({error:a},{status:c})}}}var K=300*1e3;function F(t,e){let o=null,u=null;return async function(){if(o&&Date.now()<o.expiresAt)return o.config;if(u)return u;u=(async()=>{if(!e)throw new S("WANIWANI_API_KEY is required for createChatHandler",401);let l=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!l.ok){let a=await l.text().catch(()=>"");throw new S(`Failed to resolve MCP environment config: ${l.status} ${a}`,l.status)}let m=await l.json();return o={config:m,expiresAt:Date.now()+K},m})();try{return await u}finally{u=null}}}var X="https://app.waniwani.ai";function z(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:o=X,source:u,systemPrompt:s,maxSteps:l=5,beforeRequest:m,mcpServerUrl:a,debug:c=!1,webSearch:h}=t,r=H("router",c),p=L(),n=N(p),f=F(o,e),C=J({apiKey:e,apiUrl:o,source:u,systemPrompt:s,maxSteps:l,beforeRequest:m,mcpServerUrl:a,resolveConfig:f,debug:c,webSearch:W(h)}),M=_({mcpServerUrl:a,resolveConfig:f,debug:c}),R=G({mcpServerUrl:a,resolveConfig:f,debug:c}),T=D({mcpServerUrl:a,resolveConfig:f,debug:c,source:u}),b=process.env.WANIWANI_EVAL==="1";async function I(w){r("\u2192 GET",w.url);try{let d=new URL(w.url),y=d.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",d.pathname,"subRoute:",y),b&&y==="scenarios"){r("dispatching to scenarios handler (proxy to app API)");try{let i=await(await fetch(`${o}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return n(i.data??i,200)}catch{return n([],200)}}if(y==="resource"){r("dispatching to resource handler");let x=await M(d);return r("\u2190 resource handler returned",x.status),p(x)}if(y==="config")return r("dispatching to config handler"),n({debug:c,eval:b},200);if(y==="tools"){r("dispatching to tools-list handler");let x=await R();return r("\u2190 tools-list handler returned",x.status),p(x)}return r("\u2190 404 no matching sub-route for",y),n({error:"Not found"},404)}catch(d){console.error("[waniwani:router] GET handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}async function k(w){r("\u2192 POST",w.url);try{let d=new URL(w.url),y=d.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",d.pathname,"subRoute:",y),b&&y==="scenarios"){r("dispatching to save-scenario handler (proxy to app API)");try{let i=await w.json(),P=await fetch(`${o}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(i)}),A=await P.json();return P.ok?n({ok:!0,scenario:A.data},200):n({error:A.message??"Failed to save scenario"},P.status)}catch(i){let P=i instanceof Error?i.message:"Failed to save scenario";return n({error:P},400)}}if(y==="tool"){r("dispatching to tool handler");let i=await T(w);return r("\u2190 tool handler returned",i.status),p(i)}r("dispatching to chat handler");let x=await C(w);return p(x)}catch(d){console.error("[waniwani:router] POST handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}async function j(w){r("\u2192 PATCH",w.url);try{let d=new URL(w.url),g=d.pathname.replace(/\/$/,"").split("/").filter(Boolean),y=g.at(-1),x=g.at(-2);if(r("pathname:",d.pathname,"subRoute:",x,"id:",y),b&&x==="scenarios"&&y){r("dispatching to update-scenario handler (proxy to app API)");try{let i=await w.json(),P=await fetch(`${o}/api/mcp/scenarios/${y}`,{method:"PATCH",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(i)}),A=await P.json();return P.ok?n({ok:!0,scenario:A.data},200):n({error:A.message??"Failed to update scenario"},P.status)}catch(i){let P=i instanceof Error?i.message:"Failed to update scenario";return n({error:P},400)}}return r("\u2190 404 no matching sub-route for PATCH",x),n({error:"Not found"},404)}catch(d){console.error("[waniwani:router] PATCH handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}function E(){return p(new Response(null,{status:204}))}return{handleChat:C,handleResource:M,handleTool:T,routeGet:I,routePost:k,routePatch:j,handleOptions:E}}function De(t,e){let{apiKey:o,apiUrl:u}=t._config,s=e?.debug??process.env.WANIWANI_DEBUG==="1",l=z({...e?.chat,apiKey:o,apiUrl:u,source:e?.source,debug:s});return{POST:l.routePost,GET:l.routeGet,PATCH:l.routePatch,OPTIONS:()=>l.handleOptions()}}export{De as toNextJsHandler};
9
+ `):t}function _(t){let{apiKey:e,apiUrl:o,source:u,systemPrompt:s,maxSteps:l,beforeRequest:m,mcpServerUrl:a,resolveConfig:c,debug:h,webSearch:r}=t,p=H("chat",h);return async function(f){p("\u2192 POST",f.url);try{let C=await f.json(),M=C.messages??[],R=C.sessionId,v=C.modelContext,b=s,I=C.visitorContext??null,k=L(f),j={geo:k,client:I};if(p("body parsed \u2014 messages:",M.length,"sessionId:",R??"(none)","geo:",JSON.stringify(k)),m){p("running beforeRequest hook");try{let i=await m({messages:M,sessionId:R,modelContext:v,request:f,visitor:j});i&&(i.messages&&(M=i.messages),i.systemPrompt!==void 0&&(b=i.systemPrompt),i.sessionId!==void 0&&(R=i.sessionId),i.modelContext!==void 0&&(v=i.modelContext)),p("beforeRequest hook done \u2014 messages:",M.length,"sessionId:",R??"(none)")}catch(i){console.error("[waniwani:chat] beforeRequest hook error:",i);let S=i instanceof P?i.status:400,A=i instanceof Error?i.message:"Request rejected";return p("\u2190 returning",S,"from hook error"),Response.json({error:A},{status:S})}}let E=a??(await c()).mcpServerUrl;p("mcpServerUrl:",E),b=J(b,v);let w=`${o}/api/mcp/chat`;p("forwarding to",w);let d=f.headers.get("user-agent"),g=await fetch(w,{method:"POST",headers:{"Content-Type":"application/json","X-WaniWani-Stream-Protocol":"2",...e?{Authorization:`Bearer ${e}`}:{},...d?{"X-Client-User-Agent":d}:{}},body:JSON.stringify({messages:M,mcpServerUrl:E,sessionId:R,source:u,systemPrompt:b,maxSteps:l,visitor:j,webSearch:r}),signal:f.signal});if(p("upstream response status:",g.status),!g.ok){let i=await g.text().catch(()=>"");return p("\u2190 returning",g.status,"upstream error:",i),new Response(i,{status:g.status,headers:{"Content-Type":g.headers.get("Content-Type")??"application/json"}})}let y=new Headers({"Content-Type":g.headers.get("Content-Type")??"text/event-stream"}),x=g.headers.get("x-session-id");return x&&y.set("x-session-id",x),p("\u2190 streaming response",g.status,"body null?",g.body===null),new Response(g.body,{status:g.status,headers:y})}catch(C){console.error("[waniwani:chat] handler error:",C);let M=C instanceof Error?C.message:"Unknown error occurred",R=C instanceof P?C.status:500;return p("\u2190 returning",R,"from caught error"),Response.json({error:M},{status:R})}}}function $(t){let{mcpServerUrl:e,resolveConfig:o,debug:u}=t,s=H("resource",u);return async function(m){s("\u2192 GET",m.toString());try{let a=m.searchParams.get("uri");if(s("uri:",a??"(missing)"),!a)return s("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let c=e??(await o()).mcpServerUrl;s("mcpServerUrl:",c);let h,r;try{[{createMCPClient:h},{StreamableHTTPClientTransport:r}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(n){return console.error("[waniwani:resource] MCP deps import failed:",n),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}s("creating MCP client for",c);let p=await h({transport:new r(new URL(c))});try{s("reading resource:",a);let n=await p.readResource({uri:a});s("resource contents count:",n.contents.length);let f=n.contents[0];if(!f)return s("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let C;return"text"in f&&typeof f.text=="string"?C=f.text:"blob"in f&&typeof f.blob=="string"&&(C=atob(f.blob)),C?(s("\u2190 200 HTML length:",C.length),new Response(C,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(s("\u2190 404 resource has no content, keys:",Object.keys(f)),Response.json({error:"Resource has no content"},{status:404}))}finally{await p.close(),s("MCP client closed")}}catch(a){console.error("[waniwani:resource] handler error:",a);let c=a instanceof Error?a.message:"Unknown error occurred",h=a instanceof P?a.status:500;return s("\u2190 returning",h,"from caught error"),Response.json({error:c},{status:h})}}}function D(t){let{mcpServerUrl:e,resolveConfig:o,debug:u,source:s}=t,l=H("tool",u);return async function(a){l("\u2192 POST",a.url);try{let c=await a.json(),{name:h,arguments:r}=c,p=a.headers.get("x-session-id")?.trim();if(!h||typeof h!="string")return l("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});l("tool:",h,"args:",JSON.stringify(r),"sessionId:",p||"(none)");let n=e??(await o()).mcpServerUrl;l("mcpServerUrl:",n);let f,C;try{[{Client:f},{StreamableHTTPClientTransport:C}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),l("MCP deps loaded")}catch(v){return console.error("[waniwani:tool] MCP deps import failed:",v),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}l("creating MCP client for",n);let M=new C(new URL(n)),R=new f({name:"waniwani-tool-caller",version:"1.0.0"});await R.connect(M);try{l("calling tool:",h);let v={};p&&(v["waniwani/sessionId"]=p),s&&(v["waniwani/source"]=s);let b=await R.callTool({name:h,arguments:r??{},...Object.keys(v).length>0?{_meta:v}:{}});return l("tool result received"),Response.json({content:b.content,structuredContent:b.structuredContent,_meta:b._meta,isError:b.isError})}finally{await R.close(),l("MCP client closed")}}catch(c){console.error("[waniwani:tool] handler error:",c);let h=c instanceof Error?c.message:"Unknown error occurred",r=c instanceof P?c.status:500;return l("\u2190 returning",r,"from caught error"),Response.json({error:h},{status:r})}}}function G(t){let{mcpServerUrl:e,resolveConfig:o,debug:u}=t,s=H("tools-list",u);return async function(){s("\u2192 GET tools/list");try{let m=e??(await o()).mcpServerUrl;s("mcpServerUrl:",m);let a,c;try{[{createMCPClient:a},{StreamableHTTPClientTransport:c}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(r){return console.error("[waniwani:tools-list] MCP deps import failed:",r),Response.json({error:"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery."},{status:501})}s("creating MCP client for",m);let h=await a({transport:new c(new URL(m))});try{s("listing tools");let r=await h.listTools();s("tools count:",r.tools.length);let p={tools:r.tools.map(n=>({name:n.name,...n.title!==void 0&&{title:n.title},...n.description!==void 0&&{description:n.description},...n.inputSchema!==void 0&&{inputSchema:n.inputSchema},...n.outputSchema!==void 0&&{outputSchema:n.outputSchema},...n.annotations!==void 0&&{annotations:n.annotations},...n._meta!==void 0&&{_meta:n._meta}})),...r.nextCursor!==void 0&&{nextCursor:r.nextCursor}};return s("\u2190 200"),Response.json(p,{headers:{"Cache-Control":"private, max-age=60"}})}finally{await h.close(),s("MCP client closed")}}catch(m){console.error("[waniwani:tools-list] handler error:",m);let a=m instanceof Error?m.message:"Unknown error occurred",c=m instanceof P?m.status:500;return s("\u2190 returning",c,"from caught error"),Response.json({error:a},{status:c})}}}var X=300*1e3;function F(t,e){let o=null,u=null;return async function(){if(o&&Date.now()<o.expiresAt)return o.config;if(u)return u;u=(async()=>{if(!e)throw new P("WANIWANI_API_KEY is required for createChatHandler",401);let l=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!l.ok){let a=await l.text().catch(()=>"");throw new P(`Failed to resolve MCP environment config: ${l.status} ${a}`,l.status)}let m=await l.json();return o={config:m,expiresAt:Date.now()+X},m})();try{return await u}finally{u=null}}}var Y="https://app.waniwani.ai";function z(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:o=Y,source:u,systemPrompt:s,maxSteps:l=5,beforeRequest:m,mcpServerUrl:a,debug:c=!1,webSearch:h}=t,r=H("router",c),p=N(),n=q(p),f=F(o,e),C=_({apiKey:e,apiUrl:o,source:u,systemPrompt:s,maxSteps:l,beforeRequest:m,mcpServerUrl:a,resolveConfig:f,debug:c,webSearch:W(h)}),M=$({mcpServerUrl:a,resolveConfig:f,debug:c}),R=G({mcpServerUrl:a,resolveConfig:f,debug:c}),v=D({mcpServerUrl:a,resolveConfig:f,debug:c,source:u}),b=process.env.WANIWANI_EVAL==="1";async function I(w){r("\u2192 GET",w.url);try{let d=new URL(w.url),y=d.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",d.pathname,"subRoute:",y),b&&y==="scenarios"){r("dispatching to scenarios handler (proxy to app API)");try{let i=await(await fetch(`${o}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return n(i.data??i,200)}catch{return n([],200)}}if(y==="resource"){r("dispatching to resource handler");let x=await M(d);return r("\u2190 resource handler returned",x.status),p(x)}if(y==="config")return r("dispatching to config handler"),n({debug:c,eval:b},200);if(y==="tools"){r("dispatching to tools-list handler");let x=await R();return r("\u2190 tools-list handler returned",x.status),p(x)}return r("\u2190 404 no matching sub-route for",y),n({error:"Not found"},404)}catch(d){console.error("[waniwani:router] GET handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}async function k(w){r("\u2192 POST",w.url);try{let d=new URL(w.url),y=d.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",d.pathname,"subRoute:",y),b&&y==="scenarios"){r("dispatching to save-scenario handler (proxy to app API)");try{let i=await w.json(),S=await fetch(`${o}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(i)}),A=await S.json();return S.ok?n({ok:!0,scenario:A.data},200):n({error:A.message??"Failed to save scenario"},S.status)}catch(i){let S=i instanceof Error?i.message:"Failed to save scenario";return n({error:S},400)}}if(y==="tool"){r("dispatching to tool handler");let i=await v(w);return r("\u2190 tool handler returned",i.status),p(i)}r("dispatching to chat handler");let x=await C(w);return p(x)}catch(d){console.error("[waniwani:router] POST handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}async function j(w){r("\u2192 PATCH",w.url);try{let d=new URL(w.url),g=d.pathname.replace(/\/$/,"").split("/").filter(Boolean),y=g.at(-1),x=g.at(-2);if(r("pathname:",d.pathname,"subRoute:",x,"id:",y),b&&x==="scenarios"&&y){r("dispatching to update-scenario handler (proxy to app API)");try{let i=await w.json(),S=await fetch(`${o}/api/mcp/scenarios/${y}`,{method:"PATCH",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(i)}),A=await S.json();return S.ok?n({ok:!0,scenario:A.data},200):n({error:A.message??"Failed to update scenario"},S.status)}catch(i){let S=i instanceof Error?i.message:"Failed to update scenario";return n({error:S},400)}}return r("\u2190 404 no matching sub-route for PATCH",x),n({error:"Not found"},404)}catch(d){console.error("[waniwani:router] PATCH handler error:",d);let g=d instanceof Error?d.message:"Unknown error occurred";return r("\u2190 500 from caught error"),n({error:g},500)}}function E(){return p(new Response(null,{status:204}))}return{handleChat:C,handleResource:M,handleTool:v,routeGet:I,routePost:k,routePatch:j,handleOptions:E}}var V=!1;function Ge(t,e){!V&&process.env.NODE_ENV!=="test"&&(console.warn("[waniwani-sdk] toNextJsHandler is deprecated; switch to toExpressJsHandler from @waniwani/sdk/express-js. It will be removed in a follow-up release."),V=!0);let{apiKey:o,apiUrl:u}=t._config,s=e?.debug??process.env.WANIWANI_DEBUG==="1",l=z({...e?.chat,apiKey:o,apiUrl:u,source:e?.source,debug:s});return{POST:l.routePost,GET:l.routeGet,PATCH:l.routePatch,OPTIONS:()=>l.handleOptions()}}export{Ge as toNextJsHandler};
10
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/logger.ts","../../../src/chat/server/@types.ts","../../../src/chat/server/@utils.ts","../../../src/error.ts","../../../src/chat/server/geo.ts","../../../src/shared/model-context.ts","../../../src/chat/server/model-context.ts","../../../src/chat/server/handle-chat.ts","../../../src/chat/server/handle-resource.ts","../../../src/chat/server/handle-tool.ts","../../../src/chat/server/handle-tools-list.ts","../../../src/chat/server/mcp-config-resolver.ts","../../../src/chat/server/api-handler.ts","../../../src/chat/server/next-js/index.ts"],"sourcesContent":["/**\n * Creates a namespaced logger that writes to console.log when enabled,\n * or is a no-op when disabled.\n *\n * @example\n * const log = createLogger(\"chat\", debug);\n * log(\"→ POST\", request.url); // [waniwani:chat] → POST ...\n */\nexport function createLogger(\n\tnamespace: string,\n\tenabled: boolean,\n): (...args: unknown[]) => void {\n\treturn enabled\n\t\t? (...args: unknown[]) => console.log(`[waniwani:${namespace}]`, ...args)\n\t\t: () => {};\n}\n","// WaniWani SDK - Chat Server Types\n\nimport type { UIMessage } from \"ai\";\nimport type { ModelContextUpdate } from \"../../shared/model-context\";\nimport type { GeoLocation } from \"./geo\";\n\n// ============================================================================\n// Visitor Context\n// ============================================================================\n\n/** Client-side visitor context sent in the request body */\nexport interface ClientVisitorContext {\n\ttimezone: string;\n\tlanguage: string;\n\tlanguages: string[];\n\tdeviceType: \"mobile\" | \"tablet\" | \"desktop\";\n\treferrer: string;\n\tvisitorId: string;\n}\n\n/** Combined visitor context: server geo + client context */\nexport interface VisitorMeta {\n\tgeo: GeoLocation;\n\tclient: ClientVisitorContext | null;\n}\n\n// ============================================================================\n// Before Request Hook\n// ============================================================================\n\nexport interface BeforeRequestContext {\n\t/** The conversation messages from the client */\n\tmessages: UIMessage[];\n\t/** Session identifier for conversation continuity */\n\tsessionId?: string;\n\t/** Hidden widget-provided model context for the next assistant turn */\n\tmodelContext?: ModelContextUpdate;\n\t/** The original HTTP Request object */\n\trequest: Request;\n\t/** Server-extracted geo location + client-provided visitor context */\n\tvisitor: VisitorMeta;\n}\n\nexport type BeforeRequestResult = {\n\t/** Override messages (e.g., filtered, augmented) */\n\tmessages?: UIMessage[];\n\t/** Override the system prompt for this request */\n\tsystemPrompt?: string;\n\t/** Override sessionId */\n\tsessionId?: string;\n\t/** Override hidden widget-provided model context */\n\tmodelContext?: ModelContextUpdate;\n};\n\n// ============================================================================\n// Web Search\n// ============================================================================\n\nexport interface WebSearchConfig {\n\t/** Restrict web search results to these domains */\n\tincludeDomains?: string[];\n\t/** Exclude these domains from web search results */\n\texcludeDomains?: string[];\n}\n\n// ============================================================================\n// API Handler Options\n// ============================================================================\n\nexport interface ApiHandlerOptions {\n\t/**\n\t * Identifies this chatbar instance in analytics.\n\t * Forwarded as `waniwani/source` in MCP request metadata.\n\t */\n\tsource?: string;\n\n\t/**\n\t * Your WaniWani API key.\n\t * Defaults to process.env.WANIWANI_API_KEY.\n\t */\n\tapiKey?: string;\n\n\t/**\n\t * The base URL of the WaniWani API.\n\t * Defaults to https://app.waniwani.ai.\n\t */\n\tapiUrl?: string;\n\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable verbose debug logging for all handler steps.\n\t * Logs request details, response codes, resolved URLs, and caught errors.\n\t */\n\tdebug?: boolean;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Result\n// ============================================================================\n\nexport interface ApiHandler {\n\t/** Proxies chat messages to the WaniWani API */\n\thandleChat: (request: Request) => Promise<Response>;\n\t/** Serves MCP resource content (HTML widgets) */\n\thandleResource: (url: URL) => Promise<Response>;\n\t/** Calls an MCP server tool and returns JSON */\n\thandleTool: (request: Request) => Promise<Response>;\n\t/** Routes GET sub-paths (e.g. /resource) */\n\trouteGet: (request: Request) => Promise<Response>;\n\t/** Routes POST sub-paths (e.g. /tool), defaults to chat */\n\troutePost: (request: Request) => Promise<Response>;\n\t/** Routes PATCH sub-paths (e.g. /scenarios/:id) */\n\troutePatch: (request: Request) => Promise<Response>;\n\t/** Handles CORS preflight requests */\n\thandleOptions: () => Response;\n}\n\n// ============================================================================\n// Internal Dependencies (shared across sub-handlers)\n// ============================================================================\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\ntype ConfigResolver = () => Promise<McpEnvironmentConfig>;\n\nexport interface ApiHandlerDeps {\n\tapiKey: string | undefined;\n\tapiUrl: string;\n\tsource: string | undefined;\n\tsystemPrompt: string | undefined;\n\tmaxSteps: number;\n\tbeforeRequest: ApiHandlerOptions[\"beforeRequest\"];\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\twebSearch?: WebSearchConfig;\n}\n\n/** Normalize `true` to `{}` so the upstream API always receives an object or undefined */\nexport function resolveWebSearchConfig(\n\tvalue: boolean | WebSearchConfig | undefined,\n): WebSearchConfig | undefined {\n\tif (value === true) {\n\t\treturn {};\n\t}\n\tif (value === false || value === undefined) {\n\t\treturn undefined;\n\t}\n\treturn value;\n}\n\nexport interface ResourceHandlerDeps {\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\tsource?: string;\n}\n","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response) => Response;\n\nexport function createCors(): CorsFunction {\n\treturn function applyCors(response: Response): Response {\n\t\tresponse.headers.set(\"Access-Control-Allow-Origin\", \"*\");\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Methods\",\n\t\t\t\"GET, POST, PATCH, OPTIONS\",\n\t\t);\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Headers\",\n\t\t\t\"Content-Type, Authorization, X-Session-Id, X-Client-User-Agent\",\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Expose-Headers\", \"X-Session-Id\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(data: object, status: number): Response {\n\t\treturn cors(\n\t\t\tnew Response(JSON.stringify(data), {\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tstatus,\n\t\t\t}),\n\t\t);\n\t};\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// Geo — Extract location metadata from platform request headers\n\n/**\n * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).\n * All fields are optional — in local dev, no headers are present.\n */\nexport interface GeoLocation {\n\tcity?: string;\n\tcountry?: string;\n\tcountryRegion?: string;\n\tlatitude?: string;\n\tlongitude?: string;\n\ttimezone?: string;\n\tip?: string;\n}\n\n/**\n * Extracts geolocation from server-side request headers.\n *\n * Supports Vercel (`x-vercel-ip-*`), Cloudflare (`cf-ip*`, `cf-connecting-ip`),\n * and generic IP headers (`x-real-ip`, `x-forwarded-for`).\n *\n * Returns a `GeoLocation` with all fields optional (empty object in local dev).\n */\nexport function extractGeoFromHeaders(request: Request): GeoLocation {\n\tconst h = request.headers;\n\n\t// Vercel URL-encodes city names (e.g. \"S%C3%A3o%20Paulo\")\n\tconst rawCity = h.get(\"x-vercel-ip-city\") ?? h.get(\"cf-ipcity\") ?? undefined;\n\tconst city = rawCity ? safeDecodeURI(rawCity) : undefined;\n\n\tconst country =\n\t\th.get(\"x-vercel-ip-country\") ?? h.get(\"cf-ipcountry\") ?? undefined;\n\tconst countryRegion = h.get(\"x-vercel-ip-country-region\") ?? undefined;\n\tconst latitude =\n\t\th.get(\"x-vercel-ip-latitude\") ?? h.get(\"cf-iplatitude\") ?? undefined;\n\tconst longitude =\n\t\th.get(\"x-vercel-ip-longitude\") ?? h.get(\"cf-iplongitude\") ?? undefined;\n\tconst timezone =\n\t\th.get(\"x-vercel-ip-timezone\") ?? h.get(\"cf-iptimezone\") ?? undefined;\n\tconst ip =\n\t\th.get(\"x-real-ip\") ??\n\t\th.get(\"x-forwarded-for\")?.split(\",\")[0]?.trim() ??\n\t\th.get(\"cf-connecting-ip\") ??\n\t\tundefined;\n\n\treturn { city, country, countryRegion, latitude, longitude, timezone, ip };\n}\n\nfunction safeDecodeURI(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n","import type { ContentBlock } from \"@modelcontextprotocol/sdk/types.js\";\n\nexport type ModelContextContentBlock = ContentBlock;\n\nexport type ModelContextUpdate = {\n\tcontent?: ModelContextContentBlock[];\n\tstructuredContent?: Record<string, unknown>;\n};\n\nexport function hasModelContext(\n\tvalue: ModelContextUpdate | null | undefined,\n): value is ModelContextUpdate {\n\tif (!value) {\n\t\treturn false;\n\t}\n\tconst hasContent = Array.isArray(value.content) && value.content.length > 0;\n\tconst hasStructuredContent =\n\t\ttypeof value.structuredContent === \"object\" &&\n\t\tvalue.structuredContent !== null &&\n\t\tObject.keys(value.structuredContent).length > 0;\n\treturn hasContent || hasStructuredContent;\n}\n\nexport function mergeModelContext(\n\tcurrent: ModelContextUpdate | null | undefined,\n\tnext: ModelContextUpdate | null | undefined,\n): ModelContextUpdate | null {\n\tif (!hasModelContext(current) && !hasModelContext(next)) {\n\t\treturn null;\n\t}\n\tif (!hasModelContext(current)) {\n\t\treturn {\n\t\t\t...(next?.content ? { content: [...next.content] } : {}),\n\t\t\t...(next?.structuredContent\n\t\t\t\t? { structuredContent: { ...next.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (!hasModelContext(next)) {\n\t\treturn {\n\t\t\t...(current.content ? { content: [...current.content] } : {}),\n\t\t\t...(current.structuredContent\n\t\t\t\t? { structuredContent: { ...current.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\n\treturn {\n\t\t...(current.content || next.content\n\t\t\t? { content: [...(current.content ?? []), ...(next.content ?? [])] }\n\t\t\t: {}),\n\t\t...(current.structuredContent || next.structuredContent\n\t\t\t? {\n\t\t\t\t\tstructuredContent: {\n\t\t\t\t\t\t...(current.structuredContent ?? {}),\n\t\t\t\t\t\t...(next.structuredContent ?? {}),\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\nexport function formatModelContextForPrompt(\n\tvalue: ModelContextUpdate | null | undefined,\n): string {\n\tif (!hasModelContext(value)) {\n\t\treturn \"\";\n\t}\n\n\tconst sections: string[] = [\n\t\t\"## Widget Model Context\",\n\t\t\"This hidden context was supplied by an MCP App via `ui/update-model-context`.\",\n\t\t\"Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly.\",\n\t];\n\n\tif (value.content?.length) {\n\t\tconst renderedBlocks = value.content\n\t\t\t.map((block) => {\n\t\t\t\tif (block.type === \"text\" && typeof block.text === \"string\") {\n\t\t\t\t\treturn block.text.trim();\n\t\t\t\t}\n\t\t\t\treturn JSON.stringify(block, null, 2);\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\\n\");\n\t\tif (renderedBlocks) {\n\t\t\tsections.push(`Content blocks:\\n${renderedBlocks}`);\n\t\t}\n\t}\n\n\tif (\n\t\tvalue.structuredContent &&\n\t\tObject.keys(value.structuredContent).length > 0\n\t) {\n\t\tsections.push(\n\t\t\t`Structured content JSON:\\n${JSON.stringify(value.structuredContent, null, 2)}`,\n\t\t);\n\t}\n\n\treturn sections.join(\"\\n\\n\");\n}\n","import {\n\tformatModelContextForPrompt,\n\thasModelContext,\n\ttype ModelContextUpdate,\n} from \"../../shared/model-context\";\n\nexport function applyModelContextToSystemPrompt(\n\tsystemPrompt: string | undefined,\n\tmodelContext: ModelContextUpdate | undefined,\n): string | undefined {\n\tif (!hasModelContext(modelContext)) {\n\t\treturn systemPrompt;\n\t}\n\n\tconst widgetContext = formatModelContextForPrompt(modelContext);\n\tif (!widgetContext) {\n\t\treturn systemPrompt;\n\t}\n\n\treturn [systemPrompt, widgetContext].filter(Boolean).join(\"\\n\\n\");\n}\n","// Handle Chat - Proxies chat requests to the WaniWani API\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type {\n\tApiHandlerDeps,\n\tClientVisitorContext,\n\tVisitorMeta,\n} from \"./@types\";\nimport { extractGeoFromHeaders } from \"./geo\";\nimport { applyModelContextToSystemPrompt } from \"./model-context\";\n\nexport function createChatRequestHandler(deps: ApiHandlerDeps) {\n\tconst {\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch,\n\t} = deps;\n\n\tconst log = createLogger(\"chat\", debug);\n\n\treturn async function handleChat(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\t// 1. Parse request body\n\t\t\tconst body = await request.json();\n\t\t\tlet messages = body.messages ?? [];\n\t\t\tlet sessionId: string | undefined = body.sessionId;\n\t\t\tlet modelContext = body.modelContext;\n\t\t\tlet effectiveSystemPrompt = systemPrompt;\n\n\t\t\t// Extract visitor context (client-side + server-side geo)\n\t\t\tconst clientVisitorContext: ClientVisitorContext | null =\n\t\t\t\tbody.visitorContext ?? null;\n\t\t\tconst geo = extractGeoFromHeaders(request);\n\t\t\tconst visitor: VisitorMeta = { geo, client: clientVisitorContext };\n\n\t\t\tlog(\n\t\t\t\t\"body parsed — messages:\",\n\t\t\t\tmessages.length,\n\t\t\t\t\"sessionId:\",\n\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\"geo:\",\n\t\t\t\tJSON.stringify(geo),\n\t\t\t);\n\n\t\t\t// 2. Run beforeRequest hook\n\t\t\tif (beforeRequest) {\n\t\t\t\tlog(\"running beforeRequest hook\");\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await beforeRequest({\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsessionId,\n\t\t\t\t\t\tmodelContext,\n\t\t\t\t\t\trequest,\n\t\t\t\t\t\tvisitor,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result) {\n\t\t\t\t\t\tif (result.messages) {\n\t\t\t\t\t\t\tmessages = result.messages;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.systemPrompt !== undefined) {\n\t\t\t\t\t\t\teffectiveSystemPrompt = result.systemPrompt;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.sessionId !== undefined) {\n\t\t\t\t\t\t\tsessionId = result.sessionId;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.modelContext !== undefined) {\n\t\t\t\t\t\t\tmodelContext = result.modelContext;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tlog(\n\t\t\t\t\t\t\"beforeRequest hook done — messages:\",\n\t\t\t\t\t\tmessages.length,\n\t\t\t\t\t\t\"sessionId:\",\n\t\t\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\t);\n\t\t\t\t} catch (hookError) {\n\t\t\t\t\tconsole.error(\"[waniwani:chat] beforeRequest hook error:\", hookError);\n\t\t\t\t\tconst status =\n\t\t\t\t\t\thookError instanceof WaniWaniError ? hookError.status : 400;\n\t\t\t\t\tconst message =\n\t\t\t\t\t\thookError instanceof Error ? hookError.message : \"Request rejected\";\n\t\t\t\t\tlog(\"← returning\", status, \"from hook error\");\n\t\t\t\t\treturn Response.json({ error: message }, { status });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 3. Resolve MCP server URL\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\t\t\teffectiveSystemPrompt = applyModelContextToSystemPrompt(\n\t\t\t\teffectiveSystemPrompt,\n\t\t\t\tmodelContext,\n\t\t\t);\n\n\t\t\t// 4. Forward to WaniWani API\n\t\t\tconst upstreamUrl = `${apiUrl}/api/mcp/chat`;\n\t\t\tlog(\"forwarding to\", upstreamUrl);\n\t\t\tconst clientUserAgent = request.headers.get(\"user-agent\");\n\n\t\t\tconst response = await fetch(upstreamUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"X-WaniWani-Stream-Protocol\": \"2\",\n\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t...(clientUserAgent\n\t\t\t\t\t\t? { \"X-Client-User-Agent\": clientUserAgent }\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmessages,\n\t\t\t\t\tmcpServerUrl,\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsource,\n\t\t\t\t\tsystemPrompt: effectiveSystemPrompt,\n\t\t\t\t\tmaxSteps,\n\t\t\t\t\tvisitor,\n\t\t\t\t\twebSearch,\n\t\t\t\t}),\n\t\t\t\tsignal: request.signal,\n\t\t\t});\n\n\t\t\tlog(\"upstream response status:\", response.status);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorBody = await response.text().catch(() => \"\");\n\t\t\t\tlog(\"← returning\", response.status, \"upstream error:\", errorBody);\n\t\t\t\treturn new Response(errorBody, {\n\t\t\t\t\tstatus: response.status,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\":\n\t\t\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 5. Stream the response back\n\t\t\tconst headers = new Headers({\n\t\t\t\t\"Content-Type\":\n\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"text/event-stream\",\n\t\t\t});\n\t\t\tconst upstreamSessionId = response.headers.get(\"x-session-id\");\n\t\t\tif (upstreamSessionId) {\n\t\t\t\theaders.set(\"x-session-id\", upstreamSessionId);\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"← streaming response\",\n\t\t\t\tresponse.status,\n\t\t\t\t\"body null?\",\n\t\t\t\tresponse.body === null,\n\t\t\t);\n\t\t\treturn new Response(response.body, {\n\t\t\t\tstatus: response.status,\n\t\t\t\theaders,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:chat] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Resource - Serves MCP resource content (HTML widgets)\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createResourceHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"resource\", debug);\n\n\treturn async function handleResource(url: URL): Promise<Response> {\n\t\tlog(\"→ GET\", url.toString());\n\t\ttry {\n\t\t\tconst uri = url.searchParams.get(\"uri\");\n\t\t\tlog(\"uri:\", uri ?? \"(missing)\");\n\n\t\t\tif (!uri) {\n\t\t\t\tlog(\"← 400 missing uri\");\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{ error: \"Missing uri query parameter\" },\n\t\t\t\t\t{ status: 400 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:resource] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"reading resource:\", uri);\n\t\t\t\tconst result = await mcp.readResource({ uri });\n\t\t\t\tlog(\"resource contents count:\", result.contents.length);\n\n\t\t\t\tconst content = result.contents[0];\n\t\t\t\tif (!content) {\n\t\t\t\t\tlog(\"← 404 resource not found\");\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource not found\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlet html: string | undefined;\n\t\t\t\tif (\"text\" in content && typeof content.text === \"string\") {\n\t\t\t\t\thtml = content.text;\n\t\t\t\t} else if (\"blob\" in content && typeof content.blob === \"string\") {\n\t\t\t\t\thtml = atob(content.blob);\n\t\t\t\t}\n\n\t\t\t\tif (!html) {\n\t\t\t\t\tlog(\"← 404 resource has no content, keys:\", Object.keys(content));\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource has no content\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlog(\"← 200 HTML length:\", html.length);\n\t\t\t\treturn new Response(html, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=300\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:resource] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tool - Calls MCP server tools directly and returns JSON results\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createToolHandler(deps: ResourceHandlerDeps) {\n\tconst {\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t} = deps;\n\n\tconst log = createLogger(\"tool\", debug);\n\n\treturn async function handleTool(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst body = await request.json();\n\t\t\tconst { name, arguments: args } = body as {\n\t\t\t\tname: string;\n\t\t\t\targuments: Record<string, unknown>;\n\t\t\t};\n\t\t\tconst requestSessionId = request.headers.get(\"x-session-id\")?.trim();\n\n\t\t\tif (!name || typeof name !== \"string\") {\n\t\t\t\tlog(\"← 400 missing tool name\");\n\t\t\t\treturn Response.json({ error: \"Missing tool name\" }, { status: 400 });\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"tool:\",\n\t\t\t\tname,\n\t\t\t\t\"args:\",\n\t\t\t\tJSON.stringify(args),\n\t\t\t\t\"sessionId:\",\n\t\t\t\trequestSessionId || \"(none)\",\n\t\t\t);\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet Client: typeof import(\"@modelcontextprotocol/sdk/client/index.js\")[\"Client\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ Client }, { StreamableHTTPClientTransport }] = await Promise.all([\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/index.js\"),\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\"[waniwani:tool] MCP deps import failed:\", importError);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst transport = new StreamableHTTPClientTransport(\n\t\t\t\tnew URL(mcpServerUrl),\n\t\t\t);\n\t\t\tconst client = new Client({\n\t\t\t\tname: \"waniwani-tool-caller\",\n\t\t\t\tversion: \"1.0.0\",\n\t\t\t});\n\t\t\tawait client.connect(transport);\n\n\t\t\ttry {\n\t\t\t\tlog(\"calling tool:\", name);\n\t\t\t\tconst _meta: Record<string, unknown> = {};\n\t\t\t\tif (requestSessionId) {\n\t\t\t\t\t_meta[\"waniwani/sessionId\"] = requestSessionId;\n\t\t\t\t}\n\t\t\t\tif (source) {\n\t\t\t\t\t_meta[\"waniwani/source\"] = source;\n\t\t\t\t}\n\t\t\t\tconst result = await client.callTool({\n\t\t\t\t\tname,\n\t\t\t\t\targuments: args ?? {},\n\t\t\t\t\t...(Object.keys(_meta).length > 0 ? { _meta } : {}),\n\t\t\t\t} as {\n\t\t\t\t\tname: string;\n\t\t\t\t\targuments: Record<string, unknown>;\n\t\t\t\t\t_meta?: Record<string, unknown>;\n\t\t\t\t});\n\t\t\t\tlog(\"tool result received\");\n\n\t\t\t\treturn Response.json({\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tstructuredContent: result.structuredContent,\n\t\t\t\t\t_meta: result._meta,\n\t\t\t\t\tisError: result.isError,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait client.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tool] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tools List - Returns the MCP server's tool catalog including per-tool\n// `_meta`, so the chat UI can cache it in the browser and resolve widget\n// binding (`_meta.ui.resourceUri`, `_meta[\"openai/outputTemplate\"]`, etc.) by\n// tool name at render time.\n//\n// This mirrors the pattern used by MCPJam's `/api/web/tools/list` route and\n// matches the MCP Apps spec (\"hosts identify UI-enabled tools through\n// `_meta.ui.resourceUri` metadata on the tool definition\"). Kept as an\n// ephemeral operation — one MCP client per HTTP request — so it works on\n// serverless platforms without sticky sessions.\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\n/** Shape returned to the browser. Matches the MCP `tools/list` response. */\nexport interface HandleToolsListResponse {\n\ttools: Array<{\n\t\tname: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t\tinputSchema?: unknown;\n\t\toutputSchema?: unknown;\n\t\tannotations?: unknown;\n\t\t_meta?: Record<string, unknown>;\n\t}>;\n\tnextCursor?: string;\n}\n\nexport function createToolsListHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"tools-list\", debug);\n\n\treturn async function handleToolsList(): Promise<Response> {\n\t\tlog(\"→ GET tools/list\");\n\t\ttry {\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:tools-list] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"listing tools\");\n\t\t\t\tconst result = await mcp.listTools();\n\t\t\t\tlog(\"tools count:\", result.tools.length);\n\n\t\t\t\tconst response: HandleToolsListResponse = {\n\t\t\t\t\ttools: result.tools.map((tool) => ({\n\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t...(tool.title !== undefined && { title: tool.title }),\n\t\t\t\t\t\t...(tool.description !== undefined && {\n\t\t\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.inputSchema !== undefined && {\n\t\t\t\t\t\t\tinputSchema: tool.inputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.outputSchema !== undefined && {\n\t\t\t\t\t\t\toutputSchema: tool.outputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.annotations !== undefined && {\n\t\t\t\t\t\t\tannotations: tool.annotations,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool._meta !== undefined && { _meta: tool._meta }),\n\t\t\t\t\t})),\n\t\t\t\t\t...(result.nextCursor !== undefined && {\n\t\t\t\t\t\tnextCursor: result.nextCursor,\n\t\t\t\t\t}),\n\t\t\t\t};\n\n\t\t\t\tlog(\"← 200\");\n\t\t\t\treturn Response.json(response, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t// Catalog can be safely cached briefly. Browsers get a fresh\n\t\t\t\t\t\t// copy per ChatCard mount but don't hammer the MCP server\n\t\t\t\t\t\t// when the user refreshes quickly.\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=60\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tools-list] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// MCP Config Resolver - Lazy-loads and caches MCP environment config\n\nimport { WaniWaniError } from \"../../error\";\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\nconst TTL_MS = 5 * 60 * 1000; // 5 minutes\n\nexport function createMcpConfigResolver(\n\tapiUrl: string,\n\tapiKey: string | undefined,\n) {\n\tlet cached: { config: McpEnvironmentConfig; expiresAt: number } | null = null;\n\tlet inflight: Promise<McpEnvironmentConfig> | null = null;\n\n\treturn async function resolve(): Promise<McpEnvironmentConfig> {\n\t\tif (cached && Date.now() < cached.expiresAt) {\n\t\t\treturn cached.config;\n\t\t}\n\n\t\t// Deduplicate concurrent requests (cold start scenario)\n\t\tif (inflight) {\n\t\t\treturn inflight;\n\t\t}\n\n\t\tinflight = (async () => {\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t\"WANIWANI_API_KEY is required for createChatHandler\",\n\t\t\t\t\t401,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst response = await fetch(`${apiUrl}/api/mcp/environments/config`, {\n\t\t\t\tmethod: \"GET\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t`Failed to resolve MCP environment config: ${response.status} ${body}`,\n\t\t\t\t\tresponse.status,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data = (await response.json()) as McpEnvironmentConfig;\n\t\t\tcached = { config: data, expiresAt: Date.now() + TTL_MS };\n\t\t\treturn data;\n\t\t})();\n\n\t\ttry {\n\t\t\treturn await inflight;\n\t\t} finally {\n\t\t\tinflight = null;\n\t\t}\n\t};\n}\n","// API Handler - Composes chat and resource handlers into a unified API handler\n\nimport { createLogger } from \"../../utils/logger.js\";\nimport {\n\ttype ApiHandler,\n\ttype ApiHandlerOptions,\n\tresolveWebSearchConfig,\n} from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createToolsListHandler } from \"./handle-tools-list\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\nconst DEFAULT_API_URL = \"https://app.waniwani.ai\";\n\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = DEFAULT_API_URL,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tdebug = false,\n\t\twebSearch,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors();\n\tconst json = createJsonResponse(cors);\n\n\tconst resolveConfig = createMcpConfigResolver(apiUrl, apiKey);\n\n\tconst handleChat = createChatRequestHandler({\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch: resolveWebSearchConfig(webSearch),\n\t});\n\n\tconst handleResource = createResourceHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleToolsList = createToolsListHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleTool = createToolHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t});\n\n\tconst evalEnabled = process.env.WANIWANI_EVAL === \"1\";\n\n\tasync function routeGet(request: Request): Promise<Response> {\n\t\tlog(\"→ GET\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to scenarios handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\treturn json(data.data ?? data, 200);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"resource\") {\n\t\t\t\tlog(\"dispatching to resource handler\");\n\t\t\t\tconst response = await handleResource(url);\n\t\t\t\tlog(\"← resource handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tif (subRoute === \"config\") {\n\t\t\t\tlog(\"dispatching to config handler\");\n\t\t\t\treturn json({ debug, eval: evalEnabled }, 200);\n\t\t\t}\n\n\t\t\tif (subRoute === \"tools\") {\n\t\t\t\tlog(\"dispatching to tools-list handler\");\n\t\t\t\tconst response = await handleToolsList();\n\t\t\t\tlog(\"← tools-list handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] GET handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePost(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to save-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to save scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to save scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"tool\") {\n\t\t\t\tlog(\"dispatching to tool handler\");\n\t\t\t\tconst response = await handleTool(request);\n\t\t\t\tlog(\"← tool handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\t// Default: treat as chat request\n\t\t\tlog(\"dispatching to chat handler\");\n\t\t\tconst chatResponse = await handleChat(request);\n\t\t\treturn cors(chatResponse);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] POST handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePatch(request: Request): Promise<Response> {\n\t\tlog(\"→ PATCH\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst scenarioId = segments.at(-1);\n\t\t\tconst subRoute = segments.at(-2);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute, \"id:\", scenarioId);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\" && scenarioId) {\n\t\t\t\tlog(\"dispatching to update-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios/${scenarioId}`, {\n\t\t\t\t\t\tmethod: \"PATCH\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to update scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to update scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for PATCH\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] PATCH handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tfunction handleOptions(): Response {\n\t\treturn cors(new Response(null, { status: 204 }));\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\n\t\troutePatch,\n\t\thandleOptions,\n\t};\n}\n","// WaniWani SDK - Next.js Adapter\n\nimport type { WaniWaniClient } from \"../../../types.js\";\nimport { createApiHandler } from \"../api-handler.js\";\nimport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\nexport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\n/**\n * Create Next.js route handlers from a WaniWani client.\n *\n * Returns `{ GET, POST }` for use with catch-all routes.\n * Mount at `app/api/waniwani/[[...path]]/route.ts`:\n *\n * - `POST /api/waniwani` → chat (proxied to WaniWani API)\n * - `GET /api/waniwani/resource?uri=…` → MCP resource content\n *\n * @example\n * ```typescript\n * // app/api/waniwani/[[...path]]/route.ts\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani();\n *\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: {\n * systemPrompt: \"You are a helpful assistant.\",\n * mcpServerUrl: process.env.MCP_SERVER_URL!,\n * },\n * });\n * ```\n */\nexport function toNextJsHandler(\n\tclient: WaniWaniClient,\n\toptions: NextJsHandlerOptions,\n): NextJsHandlerResult {\n\tconst { apiKey, apiUrl } = client._config;\n\n\tconst debugEnabled = options?.debug ?? process.env.WANIWANI_DEBUG === \"1\";\n\n\tconst handler = createApiHandler({\n\t\t...options?.chat,\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource: options?.source,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tPOST: handler.routePost,\n\t\tGET: handler.routeGet,\n\t\tPATCH: handler.routePatch,\n\t\tOPTIONS: () => handler.handleOptions(),\n\t};\n}\n"],"mappings":"AAQO,SAASA,EACfC,EACAC,EAC+B,CAC/B,OAAOA,EACJ,IAAIC,IAAoB,QAAQ,IAAI,aAAaF,CAAS,IAAK,GAAGE,CAAI,EACtE,IAAM,CAAC,CACX,CCiKO,SAASC,EACfC,EAC8B,CAC9B,GAAIA,IAAU,GACb,MAAO,CAAC,EAET,GAAI,EAAAA,IAAU,IAASA,IAAU,QAGjC,OAAOA,CACR,CCtLO,SAASC,GAA2B,CAC1C,OAAO,SAAmBC,EAA8B,CACvD,OAAAA,EAAS,QAAQ,IAAI,8BAA+B,GAAG,EACvDA,EAAS,QAAQ,IAChB,+BACA,2BACD,EACAA,EAAS,QAAQ,IAChB,+BACA,gEACD,EACAA,EAAS,QAAQ,IAAI,gCAAiC,cAAc,EAC7DA,CACR,CACD,CAEO,SAASC,EAAmBC,EAAoB,CACtD,OAAO,SAAcC,EAAcC,EAA0B,CAC5D,OAAOF,EACN,IAAI,SAAS,KAAK,UAAUC,CAAI,EAAG,CAClC,QAAS,CAAE,eAAgB,kBAAmB,EAC9C,OAAAC,CACD,CAAC,CACF,CACD,CACD,CC3BO,IAAMC,EAAN,cAA4B,KAAM,CACxC,YACCC,EACOC,EACN,CACD,MAAMD,CAAO,EAFN,YAAAC,EAGP,KAAK,KAAO,e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AmB,IAEhEI,EAAUC,EAAiB,CAChC,GAAGL,GAAS,KACZ,OAAAC,EACA,OAAAC,EACA,OAAQF,GAAS,OACjB,MAAOG,CACR,CAAC,EAED,MAAO,CACN,KAAMC,EAAQ,UACd,IAAKA,EAAQ,SACb,MAAOA,EAAQ,WACf,QAAS,IAAMA,EAAQ,cAAc,CACtC,CACD","names":["createLogger","namespace","enabled","args","resolveWebSearchConfig","value","createCors","response","createJsonResponse","cors","data","status","WaniWaniError","message","status","extractGeoFromHeaders","request","h","rawCity","city","safeDecodeURI","country","countryRegion","latitude","longitude","timezone","ip","value","hasModelContext","value","hasContent","hasStructuredContent","formatModelContextForPrompt","value","hasModelContext","sections","renderedBlocks","block","applyModelContextToSystemPrompt","systemPrompt","modelContext","hasModelContext","widgetContext","formatModelContextForPrompt","createChatRequestHandler","deps","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrlOverride","resolveConfig","debug","webSearch","log","createLogger","request","body","messages","sessionId","modelContext","effectiveSystemPrompt","clientVisitorContext","geo","extractGeoFromHeaders","visitor","result","hookError","status","WaniWaniError","message","mcpServerUrl","applyModelContextToSystemPrompt","upstreamUrl","clientUserAgent","response","errorBody","headers","upstreamSessionId","error","createResourceHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","url","uri","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","content","html","error","message","status","WaniWaniError","createToolHandler","deps","mcpServerUrlOverride","resolveConfig","debug","source","log","createLogger","request","body","name","args","requestSessionId","mcpServerUrl","Client","StreamableHTTPClientTransport","importError","transport","client","_meta","result","error","message","status","WaniWaniError","createToolsListHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","response","tool","error","message","status","WaniWaniError","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","debug","webSearch","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","resolveWebSearchConfig","handleResource","createResourceHandler","handleToolsList","createToolsListHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","routePatch","segments","scenarioId","handleOptions","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler"]}
1
+ {"version":3,"sources":["../../../src/utils/logger.ts","../../../src/chat/server/@types.ts","../../../src/chat/server/@utils.ts","../../../src/error.ts","../../../src/chat/server/geo.ts","../../../src/shared/model-context.ts","../../../src/chat/server/model-context.ts","../../../src/chat/server/handle-chat.ts","../../../src/chat/server/handle-resource.ts","../../../src/chat/server/handle-tool.ts","../../../src/chat/server/handle-tools-list.ts","../../../src/chat/server/mcp-config-resolver.ts","../../../src/chat/server/api-handler.ts","../../../src/chat/server/next-js/index.ts"],"sourcesContent":["/**\n * Creates a namespaced logger that writes to console.log when enabled,\n * or is a no-op when disabled.\n *\n * @example\n * const log = createLogger(\"chat\", debug);\n * log(\"→ POST\", request.url); // [waniwani:chat] → POST ...\n */\nexport function createLogger(\n\tnamespace: string,\n\tenabled: boolean,\n): (...args: unknown[]) => void {\n\treturn enabled\n\t\t? (...args: unknown[]) => console.log(`[waniwani:${namespace}]`, ...args)\n\t\t: () => {};\n}\n","// WaniWani SDK - Chat Server Types\n\nimport type { UIMessage } from \"ai\";\nimport type { ModelContextUpdate } from \"../../shared/model-context\";\nimport type { GeoLocation } from \"./geo\";\n\n// ============================================================================\n// Visitor Context\n// ============================================================================\n\n/** Client-side visitor context sent in the request body */\nexport interface ClientVisitorContext {\n\ttimezone: string;\n\tlanguage: string;\n\tlanguages: string[];\n\tdeviceType: \"mobile\" | \"tablet\" | \"desktop\";\n\treferrer: string;\n\tvisitorId: string;\n}\n\n/** Combined visitor context: server geo + client context */\nexport interface VisitorMeta {\n\tgeo: GeoLocation;\n\tclient: ClientVisitorContext | null;\n}\n\n// ============================================================================\n// Before Request Hook\n// ============================================================================\n\nexport interface BeforeRequestContext {\n\t/** The conversation messages from the client */\n\tmessages: UIMessage[];\n\t/** Session identifier for conversation continuity */\n\tsessionId?: string;\n\t/** Hidden widget-provided model context for the next assistant turn */\n\tmodelContext?: ModelContextUpdate;\n\t/** The original HTTP Request object */\n\trequest: Request;\n\t/** Server-extracted geo location + client-provided visitor context */\n\tvisitor: VisitorMeta;\n}\n\nexport type BeforeRequestResult = {\n\t/** Override messages (e.g., filtered, augmented) */\n\tmessages?: UIMessage[];\n\t/** Override the system prompt for this request */\n\tsystemPrompt?: string;\n\t/** Override sessionId */\n\tsessionId?: string;\n\t/** Override hidden widget-provided model context */\n\tmodelContext?: ModelContextUpdate;\n};\n\n// ============================================================================\n// Web Search\n// ============================================================================\n\nexport interface WebSearchConfig {\n\t/** Restrict web search results to these domains */\n\tincludeDomains?: string[];\n\t/** Exclude these domains from web search results */\n\texcludeDomains?: string[];\n}\n\n// ============================================================================\n// Chat Options (namespaced under `chat` in framework adapters)\n// ============================================================================\n\nexport interface ChatOptions {\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Options\n// ============================================================================\n\nexport interface ApiHandlerOptions {\n\t/**\n\t * Identifies this chatbar instance in analytics.\n\t * Forwarded as `waniwani/source` in MCP request metadata.\n\t */\n\tsource?: string;\n\n\t/**\n\t * Your WaniWani API key.\n\t * Defaults to process.env.WANIWANI_API_KEY.\n\t */\n\tapiKey?: string;\n\n\t/**\n\t * The base URL of the WaniWani API.\n\t * Defaults to https://app.waniwani.ai.\n\t */\n\tapiUrl?: string;\n\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable verbose debug logging for all handler steps.\n\t * Logs request details, response codes, resolved URLs, and caught errors.\n\t */\n\tdebug?: boolean;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Result\n// ============================================================================\n\nexport interface ApiHandler {\n\t/** Proxies chat messages to the WaniWani API */\n\thandleChat: (request: Request) => Promise<Response>;\n\t/** Serves MCP resource content (HTML widgets) */\n\thandleResource: (url: URL) => Promise<Response>;\n\t/** Calls an MCP server tool and returns JSON */\n\thandleTool: (request: Request) => Promise<Response>;\n\t/** Routes GET sub-paths (e.g. /resource) */\n\trouteGet: (request: Request) => Promise<Response>;\n\t/** Routes POST sub-paths (e.g. /tool), defaults to chat */\n\troutePost: (request: Request) => Promise<Response>;\n\t/** Routes PATCH sub-paths (e.g. /scenarios/:id) */\n\troutePatch: (request: Request) => Promise<Response>;\n\t/** Handles CORS preflight requests */\n\thandleOptions: () => Response;\n}\n\n// ============================================================================\n// Internal Dependencies (shared across sub-handlers)\n// ============================================================================\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\ntype ConfigResolver = () => Promise<McpEnvironmentConfig>;\n\nexport interface ApiHandlerDeps {\n\tapiKey: string | undefined;\n\tapiUrl: string;\n\tsource: string | undefined;\n\tsystemPrompt: string | undefined;\n\tmaxSteps: number;\n\tbeforeRequest: ApiHandlerOptions[\"beforeRequest\"];\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\twebSearch?: WebSearchConfig;\n}\n\n/** Normalize `true` to `{}` so the upstream API always receives an object or undefined */\nexport function resolveWebSearchConfig(\n\tvalue: boolean | WebSearchConfig | undefined,\n): WebSearchConfig | undefined {\n\tif (value === true) {\n\t\treturn {};\n\t}\n\tif (value === false || value === undefined) {\n\t\treturn undefined;\n\t}\n\treturn value;\n}\n\nexport interface ResourceHandlerDeps {\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\tsource?: string;\n}\n","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response) => Response;\n\nexport function createCors(): CorsFunction {\n\treturn function applyCors(response: Response): Response {\n\t\tresponse.headers.set(\"Access-Control-Allow-Origin\", \"*\");\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Methods\",\n\t\t\t\"GET, POST, PATCH, OPTIONS\",\n\t\t);\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Headers\",\n\t\t\t\"Content-Type, Authorization, X-Session-Id, X-Client-User-Agent\",\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Expose-Headers\", \"X-Session-Id\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(data: object, status: number): Response {\n\t\treturn cors(\n\t\t\tnew Response(JSON.stringify(data), {\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tstatus,\n\t\t\t}),\n\t\t);\n\t};\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// Geo — Extract location metadata from platform request headers\n\n/**\n * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).\n * All fields are optional — in local dev, no headers are present.\n */\nexport interface GeoLocation {\n\tcity?: string;\n\tcountry?: string;\n\tcountryRegion?: string;\n\tlatitude?: string;\n\tlongitude?: string;\n\ttimezone?: string;\n\tip?: string;\n}\n\n/**\n * Extracts geolocation from server-side request headers.\n *\n * Supports Vercel (`x-vercel-ip-*`), Cloudflare (`cf-ip*`, `cf-connecting-ip`),\n * and generic IP headers (`x-real-ip`, `x-forwarded-for`).\n *\n * Returns a `GeoLocation` with all fields optional (empty object in local dev).\n */\nexport function extractGeoFromHeaders(request: Request): GeoLocation {\n\tconst h = request.headers;\n\n\t// Vercel URL-encodes city names (e.g. \"S%C3%A3o%20Paulo\")\n\tconst rawCity = h.get(\"x-vercel-ip-city\") ?? h.get(\"cf-ipcity\") ?? undefined;\n\tconst city = rawCity ? safeDecodeURI(rawCity) : undefined;\n\n\tconst country =\n\t\th.get(\"x-vercel-ip-country\") ?? h.get(\"cf-ipcountry\") ?? undefined;\n\tconst countryRegion = h.get(\"x-vercel-ip-country-region\") ?? undefined;\n\tconst latitude =\n\t\th.get(\"x-vercel-ip-latitude\") ?? h.get(\"cf-iplatitude\") ?? undefined;\n\tconst longitude =\n\t\th.get(\"x-vercel-ip-longitude\") ?? h.get(\"cf-iplongitude\") ?? undefined;\n\tconst timezone =\n\t\th.get(\"x-vercel-ip-timezone\") ?? h.get(\"cf-iptimezone\") ?? undefined;\n\tconst ip =\n\t\th.get(\"x-real-ip\") ??\n\t\th.get(\"x-forwarded-for\")?.split(\",\")[0]?.trim() ??\n\t\th.get(\"cf-connecting-ip\") ??\n\t\tundefined;\n\n\treturn { city, country, countryRegion, latitude, longitude, timezone, ip };\n}\n\nfunction safeDecodeURI(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n","import type { ContentBlock } from \"@modelcontextprotocol/sdk/types.js\";\n\nexport type ModelContextContentBlock = ContentBlock;\n\nexport type ModelContextUpdate = {\n\tcontent?: ModelContextContentBlock[];\n\tstructuredContent?: Record<string, unknown>;\n};\n\nexport function hasModelContext(\n\tvalue: ModelContextUpdate | null | undefined,\n): value is ModelContextUpdate {\n\tif (!value) {\n\t\treturn false;\n\t}\n\tconst hasContent = Array.isArray(value.content) && value.content.length > 0;\n\tconst hasStructuredContent =\n\t\ttypeof value.structuredContent === \"object\" &&\n\t\tvalue.structuredContent !== null &&\n\t\tObject.keys(value.structuredContent).length > 0;\n\treturn hasContent || hasStructuredContent;\n}\n\nexport function mergeModelContext(\n\tcurrent: ModelContextUpdate | null | undefined,\n\tnext: ModelContextUpdate | null | undefined,\n): ModelContextUpdate | null {\n\tif (!hasModelContext(current) && !hasModelContext(next)) {\n\t\treturn null;\n\t}\n\tif (!hasModelContext(current)) {\n\t\treturn {\n\t\t\t...(next?.content ? { content: [...next.content] } : {}),\n\t\t\t...(next?.structuredContent\n\t\t\t\t? { structuredContent: { ...next.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (!hasModelContext(next)) {\n\t\treturn {\n\t\t\t...(current.content ? { content: [...current.content] } : {}),\n\t\t\t...(current.structuredContent\n\t\t\t\t? { structuredContent: { ...current.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\n\treturn {\n\t\t...(current.content || next.content\n\t\t\t? { content: [...(current.content ?? []), ...(next.content ?? [])] }\n\t\t\t: {}),\n\t\t...(current.structuredContent || next.structuredContent\n\t\t\t? {\n\t\t\t\t\tstructuredContent: {\n\t\t\t\t\t\t...(current.structuredContent ?? {}),\n\t\t\t\t\t\t...(next.structuredContent ?? {}),\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\nexport function formatModelContextForPrompt(\n\tvalue: ModelContextUpdate | null | undefined,\n): string {\n\tif (!hasModelContext(value)) {\n\t\treturn \"\";\n\t}\n\n\tconst sections: string[] = [\n\t\t\"## Widget Model Context\",\n\t\t\"This hidden context was supplied by an MCP App via `ui/update-model-context`.\",\n\t\t\"Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly.\",\n\t];\n\n\tif (value.content?.length) {\n\t\tconst renderedBlocks = value.content\n\t\t\t.map((block) => {\n\t\t\t\tif (block.type === \"text\" && typeof block.text === \"string\") {\n\t\t\t\t\treturn block.text.trim();\n\t\t\t\t}\n\t\t\t\treturn JSON.stringify(block, null, 2);\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\\n\");\n\t\tif (renderedBlocks) {\n\t\t\tsections.push(`Content blocks:\\n${renderedBlocks}`);\n\t\t}\n\t}\n\n\tif (\n\t\tvalue.structuredContent &&\n\t\tObject.keys(value.structuredContent).length > 0\n\t) {\n\t\tsections.push(\n\t\t\t`Structured content JSON:\\n${JSON.stringify(value.structuredContent, null, 2)}`,\n\t\t);\n\t}\n\n\treturn sections.join(\"\\n\\n\");\n}\n","import {\n\tformatModelContextForPrompt,\n\thasModelContext,\n\ttype ModelContextUpdate,\n} from \"../../shared/model-context\";\n\nexport function applyModelContextToSystemPrompt(\n\tsystemPrompt: string | undefined,\n\tmodelContext: ModelContextUpdate | undefined,\n): string | undefined {\n\tif (!hasModelContext(modelContext)) {\n\t\treturn systemPrompt;\n\t}\n\n\tconst widgetContext = formatModelContextForPrompt(modelContext);\n\tif (!widgetContext) {\n\t\treturn systemPrompt;\n\t}\n\n\treturn [systemPrompt, widgetContext].filter(Boolean).join(\"\\n\\n\");\n}\n","// Handle Chat - Proxies chat requests to the WaniWani API\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type {\n\tApiHandlerDeps,\n\tClientVisitorContext,\n\tVisitorMeta,\n} from \"./@types\";\nimport { extractGeoFromHeaders } from \"./geo\";\nimport { applyModelContextToSystemPrompt } from \"./model-context\";\n\nexport function createChatRequestHandler(deps: ApiHandlerDeps) {\n\tconst {\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch,\n\t} = deps;\n\n\tconst log = createLogger(\"chat\", debug);\n\n\treturn async function handleChat(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\t// 1. Parse request body\n\t\t\tconst body = await request.json();\n\t\t\tlet messages = body.messages ?? [];\n\t\t\tlet sessionId: string | undefined = body.sessionId;\n\t\t\tlet modelContext = body.modelContext;\n\t\t\tlet effectiveSystemPrompt = systemPrompt;\n\n\t\t\t// Extract visitor context (client-side + server-side geo)\n\t\t\tconst clientVisitorContext: ClientVisitorContext | null =\n\t\t\t\tbody.visitorContext ?? null;\n\t\t\tconst geo = extractGeoFromHeaders(request);\n\t\t\tconst visitor: VisitorMeta = { geo, client: clientVisitorContext };\n\n\t\t\tlog(\n\t\t\t\t\"body parsed — messages:\",\n\t\t\t\tmessages.length,\n\t\t\t\t\"sessionId:\",\n\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\"geo:\",\n\t\t\t\tJSON.stringify(geo),\n\t\t\t);\n\n\t\t\t// 2. Run beforeRequest hook\n\t\t\tif (beforeRequest) {\n\t\t\t\tlog(\"running beforeRequest hook\");\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await beforeRequest({\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsessionId,\n\t\t\t\t\t\tmodelContext,\n\t\t\t\t\t\trequest,\n\t\t\t\t\t\tvisitor,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result) {\n\t\t\t\t\t\tif (result.messages) {\n\t\t\t\t\t\t\tmessages = result.messages;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.systemPrompt !== undefined) {\n\t\t\t\t\t\t\teffectiveSystemPrompt = result.systemPrompt;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.sessionId !== undefined) {\n\t\t\t\t\t\t\tsessionId = result.sessionId;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.modelContext !== undefined) {\n\t\t\t\t\t\t\tmodelContext = result.modelContext;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tlog(\n\t\t\t\t\t\t\"beforeRequest hook done — messages:\",\n\t\t\t\t\t\tmessages.length,\n\t\t\t\t\t\t\"sessionId:\",\n\t\t\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\t);\n\t\t\t\t} catch (hookError) {\n\t\t\t\t\tconsole.error(\"[waniwani:chat] beforeRequest hook error:\", hookError);\n\t\t\t\t\tconst status =\n\t\t\t\t\t\thookError instanceof WaniWaniError ? hookError.status : 400;\n\t\t\t\t\tconst message =\n\t\t\t\t\t\thookError instanceof Error ? hookError.message : \"Request rejected\";\n\t\t\t\t\tlog(\"← returning\", status, \"from hook error\");\n\t\t\t\t\treturn Response.json({ error: message }, { status });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 3. Resolve MCP server URL\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\t\t\teffectiveSystemPrompt = applyModelContextToSystemPrompt(\n\t\t\t\teffectiveSystemPrompt,\n\t\t\t\tmodelContext,\n\t\t\t);\n\n\t\t\t// 4. Forward to WaniWani API\n\t\t\tconst upstreamUrl = `${apiUrl}/api/mcp/chat`;\n\t\t\tlog(\"forwarding to\", upstreamUrl);\n\t\t\tconst clientUserAgent = request.headers.get(\"user-agent\");\n\n\t\t\tconst response = await fetch(upstreamUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"X-WaniWani-Stream-Protocol\": \"2\",\n\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t...(clientUserAgent\n\t\t\t\t\t\t? { \"X-Client-User-Agent\": clientUserAgent }\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmessages,\n\t\t\t\t\tmcpServerUrl,\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsource,\n\t\t\t\t\tsystemPrompt: effectiveSystemPrompt,\n\t\t\t\t\tmaxSteps,\n\t\t\t\t\tvisitor,\n\t\t\t\t\twebSearch,\n\t\t\t\t}),\n\t\t\t\tsignal: request.signal,\n\t\t\t});\n\n\t\t\tlog(\"upstream response status:\", response.status);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorBody = await response.text().catch(() => \"\");\n\t\t\t\tlog(\"← returning\", response.status, \"upstream error:\", errorBody);\n\t\t\t\treturn new Response(errorBody, {\n\t\t\t\t\tstatus: response.status,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\":\n\t\t\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 5. Stream the response back\n\t\t\tconst headers = new Headers({\n\t\t\t\t\"Content-Type\":\n\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"text/event-stream\",\n\t\t\t});\n\t\t\tconst upstreamSessionId = response.headers.get(\"x-session-id\");\n\t\t\tif (upstreamSessionId) {\n\t\t\t\theaders.set(\"x-session-id\", upstreamSessionId);\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"← streaming response\",\n\t\t\t\tresponse.status,\n\t\t\t\t\"body null?\",\n\t\t\t\tresponse.body === null,\n\t\t\t);\n\t\t\treturn new Response(response.body, {\n\t\t\t\tstatus: response.status,\n\t\t\t\theaders,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:chat] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Resource - Serves MCP resource content (HTML widgets)\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createResourceHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"resource\", debug);\n\n\treturn async function handleResource(url: URL): Promise<Response> {\n\t\tlog(\"→ GET\", url.toString());\n\t\ttry {\n\t\t\tconst uri = url.searchParams.get(\"uri\");\n\t\t\tlog(\"uri:\", uri ?? \"(missing)\");\n\n\t\t\tif (!uri) {\n\t\t\t\tlog(\"← 400 missing uri\");\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{ error: \"Missing uri query parameter\" },\n\t\t\t\t\t{ status: 400 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:resource] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"reading resource:\", uri);\n\t\t\t\tconst result = await mcp.readResource({ uri });\n\t\t\t\tlog(\"resource contents count:\", result.contents.length);\n\n\t\t\t\tconst content = result.contents[0];\n\t\t\t\tif (!content) {\n\t\t\t\t\tlog(\"← 404 resource not found\");\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource not found\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlet html: string | undefined;\n\t\t\t\tif (\"text\" in content && typeof content.text === \"string\") {\n\t\t\t\t\thtml = content.text;\n\t\t\t\t} else if (\"blob\" in content && typeof content.blob === \"string\") {\n\t\t\t\t\thtml = atob(content.blob);\n\t\t\t\t}\n\n\t\t\t\tif (!html) {\n\t\t\t\t\tlog(\"← 404 resource has no content, keys:\", Object.keys(content));\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource has no content\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlog(\"← 200 HTML length:\", html.length);\n\t\t\t\treturn new Response(html, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=300\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:resource] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tool - Calls MCP server tools directly and returns JSON results\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createToolHandler(deps: ResourceHandlerDeps) {\n\tconst {\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t} = deps;\n\n\tconst log = createLogger(\"tool\", debug);\n\n\treturn async function handleTool(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst body = await request.json();\n\t\t\tconst { name, arguments: args } = body as {\n\t\t\t\tname: string;\n\t\t\t\targuments: Record<string, unknown>;\n\t\t\t};\n\t\t\tconst requestSessionId = request.headers.get(\"x-session-id\")?.trim();\n\n\t\t\tif (!name || typeof name !== \"string\") {\n\t\t\t\tlog(\"← 400 missing tool name\");\n\t\t\t\treturn Response.json({ error: \"Missing tool name\" }, { status: 400 });\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"tool:\",\n\t\t\t\tname,\n\t\t\t\t\"args:\",\n\t\t\t\tJSON.stringify(args),\n\t\t\t\t\"sessionId:\",\n\t\t\t\trequestSessionId || \"(none)\",\n\t\t\t);\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet Client: typeof import(\"@modelcontextprotocol/sdk/client/index.js\")[\"Client\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ Client }, { StreamableHTTPClientTransport }] = await Promise.all([\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/index.js\"),\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\"[waniwani:tool] MCP deps import failed:\", importError);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst transport = new StreamableHTTPClientTransport(\n\t\t\t\tnew URL(mcpServerUrl),\n\t\t\t);\n\t\t\tconst client = new Client({\n\t\t\t\tname: \"waniwani-tool-caller\",\n\t\t\t\tversion: \"1.0.0\",\n\t\t\t});\n\t\t\tawait client.connect(transport);\n\n\t\t\ttry {\n\t\t\t\tlog(\"calling tool:\", name);\n\t\t\t\tconst _meta: Record<string, unknown> = {};\n\t\t\t\tif (requestSessionId) {\n\t\t\t\t\t_meta[\"waniwani/sessionId\"] = requestSessionId;\n\t\t\t\t}\n\t\t\t\tif (source) {\n\t\t\t\t\t_meta[\"waniwani/source\"] = source;\n\t\t\t\t}\n\t\t\t\tconst result = await client.callTool({\n\t\t\t\t\tname,\n\t\t\t\t\targuments: args ?? {},\n\t\t\t\t\t...(Object.keys(_meta).length > 0 ? { _meta } : {}),\n\t\t\t\t} as {\n\t\t\t\t\tname: string;\n\t\t\t\t\targuments: Record<string, unknown>;\n\t\t\t\t\t_meta?: Record<string, unknown>;\n\t\t\t\t});\n\t\t\t\tlog(\"tool result received\");\n\n\t\t\t\treturn Response.json({\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tstructuredContent: result.structuredContent,\n\t\t\t\t\t_meta: result._meta,\n\t\t\t\t\tisError: result.isError,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait client.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tool] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tools List - Returns the MCP server's tool catalog including per-tool\n// `_meta`, so the chat UI can cache it in the browser and resolve widget\n// binding (`_meta.ui.resourceUri`, `_meta[\"openai/outputTemplate\"]`, etc.) by\n// tool name at render time.\n//\n// This mirrors the pattern used by MCPJam's `/api/web/tools/list` route and\n// matches the MCP Apps spec (\"hosts identify UI-enabled tools through\n// `_meta.ui.resourceUri` metadata on the tool definition\"). Kept as an\n// ephemeral operation — one MCP client per HTTP request — so it works on\n// serverless platforms without sticky sessions.\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\n/** Shape returned to the browser. Matches the MCP `tools/list` response. */\nexport interface HandleToolsListResponse {\n\ttools: Array<{\n\t\tname: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t\tinputSchema?: unknown;\n\t\toutputSchema?: unknown;\n\t\tannotations?: unknown;\n\t\t_meta?: Record<string, unknown>;\n\t}>;\n\tnextCursor?: string;\n}\n\nexport function createToolsListHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"tools-list\", debug);\n\n\treturn async function handleToolsList(): Promise<Response> {\n\t\tlog(\"→ GET tools/list\");\n\t\ttry {\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:tools-list] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tools list handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable tool discovery.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"listing tools\");\n\t\t\t\tconst result = await mcp.listTools();\n\t\t\t\tlog(\"tools count:\", result.tools.length);\n\n\t\t\t\tconst response: HandleToolsListResponse = {\n\t\t\t\t\ttools: result.tools.map((tool) => ({\n\t\t\t\t\t\tname: tool.name,\n\t\t\t\t\t\t...(tool.title !== undefined && { title: tool.title }),\n\t\t\t\t\t\t...(tool.description !== undefined && {\n\t\t\t\t\t\t\tdescription: tool.description,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.inputSchema !== undefined && {\n\t\t\t\t\t\t\tinputSchema: tool.inputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.outputSchema !== undefined && {\n\t\t\t\t\t\t\toutputSchema: tool.outputSchema,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool.annotations !== undefined && {\n\t\t\t\t\t\t\tannotations: tool.annotations,\n\t\t\t\t\t\t}),\n\t\t\t\t\t\t...(tool._meta !== undefined && { _meta: tool._meta }),\n\t\t\t\t\t})),\n\t\t\t\t\t...(result.nextCursor !== undefined && {\n\t\t\t\t\t\tnextCursor: result.nextCursor,\n\t\t\t\t\t}),\n\t\t\t\t};\n\n\t\t\t\tlog(\"← 200\");\n\t\t\t\treturn Response.json(response, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t// Catalog can be safely cached briefly. Browsers get a fresh\n\t\t\t\t\t\t// copy per ChatCard mount but don't hammer the MCP server\n\t\t\t\t\t\t// when the user refreshes quickly.\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=60\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tools-list] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// MCP Config Resolver - Lazy-loads and caches MCP environment config\n\nimport { WaniWaniError } from \"../../error\";\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\nconst TTL_MS = 5 * 60 * 1000; // 5 minutes\n\nexport function createMcpConfigResolver(\n\tapiUrl: string,\n\tapiKey: string | undefined,\n) {\n\tlet cached: { config: McpEnvironmentConfig; expiresAt: number } | null = null;\n\tlet inflight: Promise<McpEnvironmentConfig> | null = null;\n\n\treturn async function resolve(): Promise<McpEnvironmentConfig> {\n\t\tif (cached && Date.now() < cached.expiresAt) {\n\t\t\treturn cached.config;\n\t\t}\n\n\t\t// Deduplicate concurrent requests (cold start scenario)\n\t\tif (inflight) {\n\t\t\treturn inflight;\n\t\t}\n\n\t\tinflight = (async () => {\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t\"WANIWANI_API_KEY is required for createChatHandler\",\n\t\t\t\t\t401,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst response = await fetch(`${apiUrl}/api/mcp/environments/config`, {\n\t\t\t\tmethod: \"GET\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t`Failed to resolve MCP environment config: ${response.status} ${body}`,\n\t\t\t\t\tresponse.status,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data = (await response.json()) as McpEnvironmentConfig;\n\t\t\tcached = { config: data, expiresAt: Date.now() + TTL_MS };\n\t\t\treturn data;\n\t\t})();\n\n\t\ttry {\n\t\t\treturn await inflight;\n\t\t} finally {\n\t\t\tinflight = null;\n\t\t}\n\t};\n}\n","// API Handler - Composes chat and resource handlers into a unified API handler\n\nimport { createLogger } from \"../../utils/logger.js\";\nimport {\n\ttype ApiHandler,\n\ttype ApiHandlerOptions,\n\tresolveWebSearchConfig,\n} from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createToolsListHandler } from \"./handle-tools-list\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\nconst DEFAULT_API_URL = \"https://app.waniwani.ai\";\n\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = DEFAULT_API_URL,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tdebug = false,\n\t\twebSearch,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors();\n\tconst json = createJsonResponse(cors);\n\n\tconst resolveConfig = createMcpConfigResolver(apiUrl, apiKey);\n\n\tconst handleChat = createChatRequestHandler({\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch: resolveWebSearchConfig(webSearch),\n\t});\n\n\tconst handleResource = createResourceHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleToolsList = createToolsListHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleTool = createToolHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t});\n\n\tconst evalEnabled = process.env.WANIWANI_EVAL === \"1\";\n\n\tasync function routeGet(request: Request): Promise<Response> {\n\t\tlog(\"→ GET\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to scenarios handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\treturn json(data.data ?? data, 200);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"resource\") {\n\t\t\t\tlog(\"dispatching to resource handler\");\n\t\t\t\tconst response = await handleResource(url);\n\t\t\t\tlog(\"← resource handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tif (subRoute === \"config\") {\n\t\t\t\tlog(\"dispatching to config handler\");\n\t\t\t\treturn json({ debug, eval: evalEnabled }, 200);\n\t\t\t}\n\n\t\t\tif (subRoute === \"tools\") {\n\t\t\t\tlog(\"dispatching to tools-list handler\");\n\t\t\t\tconst response = await handleToolsList();\n\t\t\t\tlog(\"← tools-list handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] GET handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePost(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to save-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to save scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to save scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"tool\") {\n\t\t\t\tlog(\"dispatching to tool handler\");\n\t\t\t\tconst response = await handleTool(request);\n\t\t\t\tlog(\"← tool handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\t// Default: treat as chat request\n\t\t\tlog(\"dispatching to chat handler\");\n\t\t\tconst chatResponse = await handleChat(request);\n\t\t\treturn cors(chatResponse);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] POST handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePatch(request: Request): Promise<Response> {\n\t\tlog(\"→ PATCH\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst scenarioId = segments.at(-1);\n\t\t\tconst subRoute = segments.at(-2);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute, \"id:\", scenarioId);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\" && scenarioId) {\n\t\t\t\tlog(\"dispatching to update-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios/${scenarioId}`, {\n\t\t\t\t\t\tmethod: \"PATCH\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to update scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to update scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for PATCH\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] PATCH handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tfunction handleOptions(): Response {\n\t\treturn cors(new Response(null, { status: 204 }));\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\n\t\troutePatch,\n\t\thandleOptions,\n\t};\n}\n","// WaniWani SDK - Next.js Adapter\n\nimport type { WaniWaniClient } from \"../../../types.js\";\nimport { createApiHandler } from \"../api-handler.js\";\nimport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\nexport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\nlet deprecationWarned = false;\n\n/**\n * Create Next.js route handlers from a WaniWani client.\n *\n * Returns `{ GET, POST }` for use with catch-all routes.\n * Mount at `app/api/waniwani/[[...path]]/route.ts`:\n *\n * - `POST /api/waniwani` → chat (proxied to WaniWani API)\n * - `GET /api/waniwani/resource?uri=…` → MCP resource content\n *\n * @deprecated Use `toExpressJsHandler` from `@waniwani/sdk/express-js` instead.\n * The Next.js-specific wrapper will be removed in a follow-up release. Both\n * adapters share the same underlying `createApiHandler`, so behavior is identical.\n *\n * @example\n * ```typescript\n * // app/api/waniwani/[[...path]]/route.ts\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani();\n *\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: {\n * systemPrompt: \"You are a helpful assistant.\",\n * mcpServerUrl: process.env.MCP_SERVER_URL!,\n * },\n * });\n * ```\n */\nexport function toNextJsHandler(\n\tclient: WaniWaniClient,\n\toptions: NextJsHandlerOptions,\n): NextJsHandlerResult {\n\tif (!deprecationWarned && process.env.NODE_ENV !== \"test\") {\n\t\tconsole.warn(\n\t\t\t\"[waniwani-sdk] toNextJsHandler is deprecated; switch to toExpressJsHandler from @waniwani/sdk/express-js. It will be removed in a follow-up release.\",\n\t\t);\n\t\tdeprecationWarned = true;\n\t}\n\n\tconst { apiKey, apiUrl } = client._config;\n\n\tconst debugEnabled = options?.debug ?? process.env.WANIWANI_DEBUG === \"1\";\n\n\tconst handler = createApiHandler({\n\t\t...options?.chat,\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource: options?.source,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tPOST: handler.routePost,\n\t\tGET: handler.routeGet,\n\t\tPATCH: handler.routePatch,\n\t\tOPTIONS: () => handler.handleOptions(),\n\t};\n}\n"],"mappings":"AAQO,SAASA,EACfC,EACAC,EAC+B,CAC/B,OAAOA,EACJ,IAAIC,IAAoB,QAAQ,IAAI,aAAaF,CAAS,IAAK,GAAGE,CAAI,EACtE,IAAM,CAAC,CACX,CC2MO,SAASC,EACfC,EAC8B,CAC9B,GAAIA,IAAU,GACb,MAAO,CAAC,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qB,CAAQ,EAC9CjB,EAAK,CAAE,MAAO,WAAY,EAAG,GAAG,CACxC,OAASoB,EAAO,CACf,QAAQ,MAAM,yCAA0CA,CAAK,EAC7D,IAAMC,EACLD,aAAiB,MAAQA,EAAM,QAAU,yBAC1C,OAAAxB,EAAI,8BAAyB,EACtBI,EAAK,CAAE,MAAOqB,CAAQ,EAAG,GAAG,CACpC,CACD,CAEA,SAASU,GAA0B,CAClC,OAAOjC,EAAK,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,CAAC,CAChD,CAEA,MAAO,CACN,WAAAM,EACA,eAAAG,EACA,WAAAI,EACA,SAAAG,EACA,UAAAQ,EACA,WAAAM,EACA,cAAAG,CACD,CACD,CC/OA,IAAIC,EAAoB,GA+BjB,SAASC,GACfC,EACAC,EACsB,CAClB,CAACH,GAAqB,QAAQ,IAAI,WAAa,SAClD,QAAQ,KACP,sJACD,EACAA,EAAoB,IAGrB,GAAM,CAAE,OAAAI,EAAQ,OAAAC,CAAO,EAAIH,EAAO,QAE5BI,EAAeH,GAAS,OAAS,QAAQ,IAAI,iBAAmB,IAEhEI,EAAUC,EAAiB,CAChC,GAAGL,GAAS,KACZ,OAAAC,EACA,OAAAC,EACA,OAAQF,GAAS,OACjB,MAAOG,CACR,CAAC,EAED,MAAO,CACN,KAAMC,EAAQ,UACd,IAAKA,EAAQ,SACb,MAAOA,EAAQ,WACf,QAAS,IAAMA,EAAQ,cAAc,CACtC,CACD","names":["createLogger","namespace","enabled","args","resolveWebSearchConfig","value","createCors","response","createJsonResponse","cors","data","status","WaniWaniError","message","status","extractGeoFromHeaders","request","h","rawCity","city","safeDecodeURI","country","countryRegion","latitude","longitude","timezone","ip","value","hasModelContext","value","hasContent","hasStructuredContent","formatModelContextForPrompt","value","hasModelContext","sections","renderedBlocks","block","applyModelContextToSystemPrompt","systemPrompt","modelContext","hasModelContext","widgetContext","formatModelContextForPrompt","createChatRequestHandler","deps","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrlOverride","resolveConfig","debug","webSearch","log","createLogger","request","body","messages","sessionId","modelContext","effectiveSystemPrompt","clientVisitorContext","geo","extractGeoFromHeaders","visitor","result","hookError","status","WaniWaniError","message","mcpServerUrl","applyModelContextToSystemPrompt","upstreamUrl","clientUserAgent","response","errorBody","headers","upstreamSessionId","error","createResourceHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","url","uri","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","content","html","error","message","status","WaniWaniError","createToolHandler","deps","mcpServerUrlOverride","resolveConfig","debug","source","log","createLogger","request","body","name","args","requestSessionId","mcpServerUrl","Client","StreamableHTTPClientTransport","importError","transport","client","_meta","result","error","message","status","WaniWaniError","createToolsListHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","response","tool","error","message","status","WaniWaniError","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","debug","webSearch","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","resolveWebSearchConfig","handleResource","createResourceHandler","handleToolsList","createToolsListHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","routePatch","segments","scenarioId","handleOptions","deprecationWarned","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waniwani/sdk",
3
- "version": "0.10.11",
3
+ "version": "0.11.1-beta.0",
4
4
  "description": "WaniWani SDK - MCP event tracking, widget framework, and tools",
5
5
  "type": "module",
6
6
  "exports": {
@@ -36,6 +36,11 @@
36
36
  "import": "./dist/chat/next-js/index.js",
37
37
  "default": "./dist/chat/next-js/index.js"
38
38
  },
39
+ "./express-js": {
40
+ "types": "./dist/chat/express-js/index.d.ts",
41
+ "import": "./dist/chat/express-js/index.js",
42
+ "default": "./dist/chat/express-js/index.js"
43
+ },
39
44
  "./kb": {
40
45
  "types": "./dist/kb/index.d.ts",
41
46
  "import": "./dist/kb/index.js",