@waniwani/sdk 0.7.3 → 0.7.4
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.
|
@@ -309,12 +309,19 @@ interface NextJsHandlerOptions {
|
|
|
309
309
|
* Logs request details, response codes, resolved URLs, and caught errors.
|
|
310
310
|
*/
|
|
311
311
|
debug?: boolean;
|
|
312
|
+
/**
|
|
313
|
+
* Additional origins allowed for cross-origin requests.
|
|
314
|
+
* The WaniWani platform URL is always included by default.
|
|
315
|
+
*/
|
|
316
|
+
allowedOrigins?: string[];
|
|
312
317
|
}
|
|
313
318
|
interface NextJsHandlerResult {
|
|
314
319
|
/** GET handler: routes sub-paths (e.g. /resource for MCP widget content) */
|
|
315
320
|
GET: (request: Request) => Promise<Response>;
|
|
316
321
|
/** POST handler: proxies chat messages to the WaniWani API */
|
|
317
322
|
POST: (request: Request) => Promise<Response>;
|
|
323
|
+
/** OPTIONS handler: CORS preflight */
|
|
324
|
+
OPTIONS: (request: Request) => Response;
|
|
318
325
|
}
|
|
319
326
|
|
|
320
327
|
/**
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function A(t,e){return e?(...n)=>console.log(`[waniwani:${t}]`,...n):()=>{}}function E(t){let e=new Set(t.map(n=>n.replace(/\/$/,"").toLowerCase()));return function(o,i){let s=i?.headers.get("origin"),h=s?.toLowerCase(),a=h&&e.has(h)?s??"":"";return o.headers.set("Access-Control-Allow-Origin",a),o.headers.set("Access-Control-Allow-Methods","GET, POST, OPTIONS"),o.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),o.headers.set("Access-Control-Expose-Headers","X-Session-Id"),o.headers.set("Vary","Origin"),o}}function I(t){return function(n,o,i){return t(new Response(JSON.stringify(n),{headers:{"Content-Type":"application/json"},status:o}),i)}}var b=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};function N(t){let e=t.headers,n=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,o=n?F(n):void 0,i=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,s=e.get("x-vercel-ip-country-region")??void 0,h=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,f=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,p=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:o,country:i,countryRegion:s,latitude:h,longitude:a,timezone:f,ip:p}}function F(t){try{return decodeURIComponent(t)}catch{return t}}function k(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 W(t){if(!k(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(o=>o.type==="text"&&typeof o.text=="string"?o.text.trim():JSON.stringify(o,null,2)).filter(Boolean).join(`
|
|
2
2
|
|
|
3
|
-
`);
|
|
4
|
-
${
|
|
3
|
+
`);n&&e.push(`Content blocks:
|
|
4
|
+
${n}`)}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
|
|
7
|
+
`)}function L(t,e){if(!k(e))return t;let n=W(e);return n?[t,n].filter(Boolean).join(`
|
|
8
8
|
|
|
9
|
-
`):t}function
|
|
9
|
+
`):t}function J(t){let{apiKey:e,apiUrl:n,source:o,systemPrompt:i,maxSteps:s,beforeRequest:h,mcpServerUrl:a,resolveConfig:f,debug:p}=t,r=A("chat",p);return async function(l){r("\u2192 POST",l.url);try{let c=await l.json(),g=c.messages??[],x=c.sessionId,T=c.modelContext,y=i,v=c.visitorContext??null,O=N(l),H={geo:O,client:v};if(r("body parsed \u2014 messages:",g.length,"sessionId:",x??"(none)","geo:",JSON.stringify(O)),h){r("running beforeRequest hook");try{let d=await h({messages:g,sessionId:x,modelContext:T,request:l,visitor:H});d&&(d.messages&&(g=d.messages),d.systemPrompt!==void 0&&(y=d.systemPrompt),d.sessionId!==void 0&&(x=d.sessionId),d.modelContext!==void 0&&(T=d.modelContext)),r("beforeRequest hook done \u2014 messages:",g.length,"sessionId:",x??"(none)")}catch(d){console.error("[waniwani:chat] beforeRequest hook error:",d);let j=d instanceof b?d.status:400,D=d instanceof Error?d.message:"Request rejected";return r("\u2190 returning",j,"from hook error"),Response.json({error:D},{status:j})}}let u=a??(await f()).mcpServerUrl;r("mcpServerUrl:",u),y=L(y,T);let C=`${n}/api/mcp/chat`;r("forwarding to",C);let U=l.headers.get("user-agent"),m=await fetch(C,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{},...U?{"X-Client-User-Agent":U}:{}},body:JSON.stringify({messages:g,mcpServerUrl:u,sessionId:x,source:o,systemPrompt:y,maxSteps:s,visitor:H}),signal:l.signal});if(r("upstream response status:",m.status),!m.ok){let d=await m.text().catch(()=>"");return r("\u2190 returning",m.status,"upstream error:",d),new Response(d,{status:m.status,headers:{"Content-Type":m.headers.get("Content-Type")??"application/json"}})}let M=new Headers({"Content-Type":m.headers.get("Content-Type")??"text/event-stream"}),R=m.headers.get("x-session-id");return R&&M.set("x-session-id",R),r("\u2190 streaming response",m.status,"body null?",m.body===null),new Response(m.body,{status:m.status,headers:M})}catch(c){console.error("[waniwani:chat] handler error:",c);let g=c instanceof Error?c.message:"Unknown error occurred",x=c instanceof b?c.status:500;return r("\u2190 returning",x,"from caught error"),Response.json({error:g},{status:x})}}}function $(t){let{mcpServerUrl:e,resolveConfig:n,debug:o}=t,i=A("resource",o);return async function(h){i("\u2192 GET",h.toString());try{let a=h.searchParams.get("uri");if(i("uri:",a??"(missing)"),!a)return i("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let f=e??(await n()).mcpServerUrl;i("mcpServerUrl:",f);let p,r;try{[{createMCPClient:p},{StreamableHTTPClientTransport:r}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),i("MCP deps loaded")}catch(l){return console.error("[waniwani:resource] MCP deps import failed:",l),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}i("creating MCP client for",f);let w=await p({transport:new r(new URL(f))});try{i("reading resource:",a);let l=await w.readResource({uri:a});i("resource contents count:",l.contents.length);let c=l.contents[0];if(!c)return i("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let g;return"text"in c&&typeof c.text=="string"?g=c.text:"blob"in c&&typeof c.blob=="string"&&(g=atob(c.blob)),g?(i("\u2190 200 HTML length:",g.length),new Response(g,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(i("\u2190 404 resource has no content, keys:",Object.keys(c)),Response.json({error:"Resource has no content"},{status:404}))}finally{await w.close(),i("MCP client closed")}}catch(a){console.error("[waniwani:resource] handler error:",a);let f=a instanceof Error?a.message:"Unknown error occurred",p=a instanceof b?a.status:500;return i("\u2190 returning",p,"from caught error"),Response.json({error:f},{status:p})}}}function B(t){let{mcpServerUrl:e,resolveConfig:n,debug:o,source:i}=t,s=A("tool",o);return async function(a){s("\u2192 POST",a.url);try{let f=await a.json(),{name:p,arguments:r}=f,w=a.headers.get("x-session-id")?.trim();if(!p||typeof p!="string")return s("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});s("tool:",p,"args:",JSON.stringify(r),"sessionId:",w||"(none)");let l=e??(await n()).mcpServerUrl;s("mcpServerUrl:",l);let c,g;try{[{Client:c},{StreamableHTTPClientTransport:g}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(y){return console.error("[waniwani:tool] MCP deps import failed:",y),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}s("creating MCP client for",l);let x=new g(new URL(l)),T=new c({name:"waniwani-tool-caller",version:"1.0.0"});await T.connect(x);try{s("calling tool:",p);let y={};w&&(y["waniwani/sessionId"]=w),i&&(y["waniwani/source"]=i);let v=await T.callTool({name:p,arguments:r??{},...Object.keys(y).length>0?{_meta:y}:{}});return s("tool result received"),Response.json({content:v.content,structuredContent:v.structuredContent,_meta:v._meta,isError:v.isError})}finally{await T.close(),s("MCP client closed")}}catch(f){console.error("[waniwani:tool] handler error:",f);let p=f instanceof Error?f.message:"Unknown error occurred",r=f instanceof b?f.status:500;return s("\u2190 returning",r,"from caught error"),Response.json({error:p},{status:r})}}}var z=300*1e3;function _(t,e){let n=null,o=null;return async function(){if(n&&Date.now()<n.expiresAt)return n.config;if(o)return o;o=(async()=>{if(!e)throw new b("WANIWANI_API_KEY is required for createChatHandler",401);let s=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!s.ok){let a=await s.text().catch(()=>"");throw new b(`Failed to resolve MCP environment config: ${s.status} ${a}`,s.status)}let h=await s.json();return n={config:h,expiresAt:Date.now()+z},h})();try{return await o}finally{o=null}}}var V="https://app.waniwani.ai";function G(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:n=V,source:o,systemPrompt:i,maxSteps:s=5,beforeRequest:h,mcpServerUrl:a,allowedOrigins:f,debug:p=!1}=t,r=A("router",p),w=E([n,...f??[]]),l=I(w),c=_(n,e),g=J({apiKey:e,apiUrl:n,source:o,systemPrompt:i,maxSteps:s,beforeRequest:h,mcpServerUrl:a,resolveConfig:c,debug:p}),x=$({mcpServerUrl:a,resolveConfig:c,debug:p}),T=B({mcpServerUrl:a,resolveConfig:c,debug:p,source:o}),y=process.env.WANIWANI_EVAL==="1";async function v(u){r("\u2192 GET",u.url);try{let C=new URL(u.url),m=C.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",C.pathname,"subRoute:",m),y&&m==="scenarios"){r("dispatching to scenarios handler (proxy to app API)");try{let R=await(await fetch(`${n}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return l(R.data??R,200,u)}catch{return l([],200,u)}}if(m==="resource"){r("dispatching to resource handler");let M=await x(C);return r("\u2190 resource handler returned",M.status),w(M,u)}return m==="config"?(r("dispatching to config handler"),l({debug:p,eval:y},200,u)):(r("\u2190 404 no matching sub-route for",m),l({error:"Not found"},404,u))}catch(C){console.error("[waniwani:router] GET handler error:",C);let U=C instanceof Error?C.message:"Unknown error occurred";return r("\u2190 500 from caught error"),l({error:U},500,u)}}async function O(u){r("\u2192 POST",u.url);try{let C=new URL(u.url),m=C.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(r("pathname:",C.pathname,"subRoute:",m),y&&m==="scenarios"){r("dispatching to save-scenario handler (proxy to app API)");try{let R=await u.json(),d=await fetch(`${n}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(R)}),j=await d.json();return d.ok?l({ok:!0,scenario:j.data},200,u):l({error:j.message??"Failed to save scenario"},d.status,u)}catch(R){let d=R instanceof Error?R.message:"Failed to save scenario";return l({error:d},400,u)}}if(m==="tool"){r("dispatching to tool handler");let R=await T(u);return r("\u2190 tool handler returned",R.status),w(R,u)}r("dispatching to chat handler");let M=await g(u);return w(M,u)}catch(C){console.error("[waniwani:router] POST handler error:",C);let U=C instanceof Error?C.message:"Unknown error occurred";return r("\u2190 500 from caught error"),l({error:U},500,u)}}function H(u){return w(new Response(null,{status:204}),u)}return{handleChat:g,handleResource:x,handleTool:T,routeGet:v,routePost:O,handleOptions:H}}function ke(t,e){let{apiKey:n,apiUrl:o}=t._config,i=e?.debug??process.env.WANIWANI_DEBUG==="1",s=G({...e?.chat,apiKey:n,apiUrl:o,source:e?.source,allowedOrigins:e?.allowedOrigins,debug:i});return{POST:s.routePost,GET:s.routeGet,OPTIONS:h=>s.handleOptions(h)}}export{ke as toNextJsHandler};
|
|
10
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/logger.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/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 - 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} = 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...(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}),\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","// 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 type { ApiHandler, ApiHandlerOptions } from \"./@types\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\n/**\n * Create a JSON response with the given data and status code.\n * @param data - The data to be serialized to JSON.\n * @param status - The HTTP status code to be returned.\n * @returns A Response object with the JSON data and the given status code.\n */\nfunction jsonResponse(data: object, status: number): Response {\n\treturn new Response(JSON.stringify(data), {\n\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\tstatus,\n\t});\n}\n\n/**\n * Create a framework-agnostic API handler for chat and MCP resources.\n *\n * Returns an object with handler methods that can be wired into\n * any framework (Next.js, Hono, Express, etc.):\n *\n * - `handleChat(request)` → proxies chat messages to WaniWani API\n * - `handleResource(url)` → serves MCP resource content (HTML widgets)\n * - `routeGet(request)` → routes GET sub-paths (e.g. /resource)\n *\n * @example\n * ```typescript\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, dynamic } = toNextJsHandler(wani, {\n * chat: { systemPrompt: \"You are a helpful assistant.\" },\n * });\n * ```\n */\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = \"https://app.waniwani.ai\",\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tdebug = false,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\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});\n\n\tconst handleResource = createResourceHandler({\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 handleConfig(): Promise<Response> {\n\t\treturn jsonResponse({ debug, eval: evalEnabled }, 200);\n\t}\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\t// Proxy scenarios list to the WaniWani app API\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 json = await res.json();\n\t\t\t\t\t// The app API wraps responses in { success, data } — unwrap for the SDK\n\t\t\t\t\tconst scenarios = json.data ?? json;\n\t\t\t\t\treturn jsonResponse(scenarios, 200);\n\t\t\t\t} catch {\n\t\t\t\t\treturn jsonResponse([], 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 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\tconst response = await handleConfig();\n\t\t\t\tlog(\"← config handler returned\", response.status);\n\t\t\t\treturn response;\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn jsonResponse({ 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 jsonResponse({ 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\t// Proxy scenario creation to the WaniWani app API\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 json = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn jsonResponse(\n\t\t\t\t\t\t\t{ error: json.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 jsonResponse({ ok: true, scenario: json.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 jsonResponse({ 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 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\treturn handleChat(request);\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 jsonResponse({ error: message }, 500);\n\t\t}\n\t}\n\n\treturn { handleChat, handleResource, handleTool, routeGet, routePost };\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};\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,CCbO,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,IAAI,kBAAkB,GAAKA,EAAE,IAAI,WAAW,GAAK,OAC7DE,EAAOD,EAAUE,EAAcF,CAAO,EAAI,OAE1CG,EACLJ,EAAE,IAAI,qBAAqB,GAAKA,EAAE,IAAI,cAAc,GAAK,OACpDK,EAAgBL,EAAE,IAAI,4BAA4B,GAAK,OACvDM,EACLN,EAAE,IAAI,sBAAsB,GAAKA,EAAE,IAAI,eAAe,GAAK,OACtDO,EACLP,EAAE,IAAI,uBAAuB,GAAKA,EAAE,IAAI,gBAAgB,GAAK,OACxDQ,EACLR,EAAE,IAAI,sBAAsB,GAAKA,EAAE,IAAI,eAAe,GAAK,OACtDS,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EACA,OAAAC,EACA,OAAQF,GAAS,OACjB,MAAOG,CACR,CAAC,EAED,MAAO,CACN,KAAMC,EAAQ,UACd,IAAKA,EAAQ,QACd,CACD","names":["createLogger","namespace","enabled","args","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","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","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","jsonResponse","data","status","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","debug","log","createLogger","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","handleResource","createResourceHandler","handleTool","createToolHandler","evalEnabled","handleConfig","routeGet","request","url","subRoute","json","scenarios","response","error","message","routePost","body","res","e","msg","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/logger.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/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","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response, request?: Request) => Response;\n\nexport function createCors(allowedOrigins: string[]): CorsFunction {\n\tconst originSet = new Set(\n\t\tallowedOrigins.map((o) => o.replace(/\\/$/, \"\").toLowerCase()),\n\t);\n\n\treturn function applyCors(response: Response, request?: Request): Response {\n\t\tconst requestOrigin = request?.headers.get(\"origin\");\n\t\tconst origin = requestOrigin?.toLowerCase();\n\t\tconst allowed =\n\t\t\torigin && originSet.has(origin) ? (requestOrigin ?? \"\") : \"\";\n\t\tresponse.headers.set(\"Access-Control-Allow-Origin\", allowed);\n\t\tresponse.headers.set(\"Access-Control-Allow-Methods\", \"GET, POST, OPTIONS\");\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\tresponse.headers.set(\"Vary\", \"Origin\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(\n\t\tdata: object,\n\t\tstatus: number,\n\t\trequest?: Request,\n\t): 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\trequest,\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} = 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...(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}),\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","// 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 type { ApiHandler, ApiHandlerOptions } from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\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\tallowedOrigins: extraOrigins,\n\t\tdebug = false,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors([apiUrl, ...(extraOrigins ?? [])]);\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});\n\n\tconst handleResource = createResourceHandler({\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, request);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200, request);\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, request);\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, request);\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, request);\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, request);\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\trequest,\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, request);\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, request);\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, request);\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, request);\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, request);\n\t\t}\n\t}\n\n\tfunction handleOptions(request?: Request): Response {\n\t\treturn cors(new Response(null, { status: 204 }), request);\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\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\tallowedOrigins: options?.allowedOrigins,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tPOST: handler.routePost,\n\t\tGET: handler.routeGet,\n\t\tOPTIONS: (request: Request) => handler.handleOptions(request),\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,CCXO,SAASC,EAAWC,EAAwC,CAClE,IAAMC,EAAY,IAAI,IACrBD,EAAe,IAAKE,GAAMA,EAAE,QAAQ,MAAO,EAAE,EAAE,YAAY,CAAC,CAC7D,EAEA,OAAO,SAAmBC,EAAoBC,EAA6B,CAC1E,IAAMC,EAAgBD,GAAS,QAAQ,IAAI,QAAQ,EAC7CE,EAASD,GAAe,YAAY,EACpCE,EACLD,GAAUL,EAAU,IAAIK,CAAM,EAAKD,GAAiB,GAAM,GAC3D,OAAAF,EAAS,QAAQ,IAAI,8BAA+BI,CAAO,EAC3DJ,EAAS,QAAQ,IAAI,+BAAgC,oBAAoB,EACzEA,EAAS,QAAQ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args","createCors","allowedOrigins","originSet","o","response","request","requestOrigin","origin","allowed","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","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","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","extraOrigins","debug","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","handleResource","createResourceHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","handleOptions","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler","request"]}
|
|
@@ -113,6 +113,11 @@ interface ApiHandlerOptions {
|
|
|
113
113
|
* Logs request details, response codes, resolved URLs, and caught errors.
|
|
114
114
|
*/
|
|
115
115
|
debug?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Additional origins allowed for cross-origin requests.
|
|
118
|
+
* The WaniWani platform URL (apiUrl) is always included.
|
|
119
|
+
*/
|
|
120
|
+
allowedOrigins?: string[];
|
|
116
121
|
}
|
|
117
122
|
interface ApiHandler {
|
|
118
123
|
/** Proxies chat messages to the WaniWani API */
|
|
@@ -125,6 +130,8 @@ interface ApiHandler {
|
|
|
125
130
|
routeGet: (request: Request) => Promise<Response>;
|
|
126
131
|
/** Routes POST sub-paths (e.g. /tool), defaults to chat */
|
|
127
132
|
routePost: (request: Request) => Promise<Response>;
|
|
133
|
+
/** Handles CORS preflight requests */
|
|
134
|
+
handleOptions: (request?: Request) => Response;
|
|
128
135
|
}
|
|
129
136
|
|
|
130
137
|
export { type ApiHandler, type ApiHandlerOptions, type BeforeRequestContext, type BeforeRequestResult, type ClientVisitorContext, type GeoLocation, type VisitorMeta, WaniWaniError, extractGeoFromHeaders };
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function X(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ze(){return X()==="openai"}function He(){return X()==="mcp-apps"}var I="__start__",b="__end__",pe=Symbol.for("waniwani.flow.interrupt"),le=Symbol.for("waniwani.flow.widget");function fe(e,t){let n=t?.context,r=[];for(let[o,s]of Object.entries(e))if(typeof s=="object"&&s!==null&&"question"in s){let a=s;r.push({question:a.question,field:o,suggestions:a.suggestions,context:a.context,validate:a.validate})}return{__type:pe,questions:r,context:n}}function ge(e,t){return{__type:le,tool:e,...t}}function me(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===pe}function we(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===le}import{z as K}from"zod";var G="waniwani/client";function $(e){if(typeof e=="object"&&e!==null)return e[G]}function he(e,t,n){return{track(r){return e.track({...r,meta:{...t,...r.meta}})},identify(r,o){return e.identify(r,o,t)},kb:e.kb,_config:n}}function D(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var Ye=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],Je=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],Xe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],Ge=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Ze=["correlationId","openai/requestId"];function _(e){return e?D(e,Ye):void 0}function ye(e){return e?D(e,Je):void 0}function Te(e){return e?D(e,Xe):void 0}function ke(e){return e?D(e,Ge):void 0}function Se(e){return e?D(e,Ze):void 0}var Qe=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function M(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:r}of Qe){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function ve(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function U(e,t){let n=t.split("."),r=e;for(let o of n){if(r==null||typeof r!="object")return;r=r[o]}return r}function et(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let s=e;for(let a of r)(s[a]==null||typeof s[a]!="object"||Array.isArray(s[a]))&&(s[a]={}),s=s[a];s[o]=n}function xe(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let s of n){if(o==null||typeof o!="object")return;o=o[s]}o!=null&&typeof o=="object"&&delete o[r]}function Z(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?et(t,n,r):t[n]=r;return t}function W(e,t){let n={...e};for(let[r,o]of Object.entries(t))o!==null&&typeof o=="object"&&!Array.isArray(o)&&n[r]!==null&&typeof n[r]=="object"&&!Array.isArray(n[r])?n[r]=W(n[r],o):n[r]=o;return n}function Q(e){return e!=null&&e!==""}async function F(e,t){return e.type==="direct"?e.to:e.condition(t)}function Re(e,t,n,r){if(e.every(c=>Q(U(r,c.field))))return null;let o=e.filter(c=>!Q(U(r,c.field))),s=o.length===1,a=o[0];return{content:s&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&a?{field:a.field}:{}}}}async function B(e,t,n,r,o,s,a){let i=e,c={...t},d=50,
|
|
1
|
+
function X(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ze(){return X()==="openai"}function He(){return X()==="mcp-apps"}var I="__start__",b="__end__",pe=Symbol.for("waniwani.flow.interrupt"),le=Symbol.for("waniwani.flow.widget");function fe(e,t){let n=t?.context,r=[];for(let[o,s]of Object.entries(e))if(typeof s=="object"&&s!==null&&"question"in s){let a=s;r.push({question:a.question,field:o,suggestions:a.suggestions,context:a.context,validate:a.validate})}return{__type:pe,questions:r,context:n}}function ge(e,t){return{__type:le,tool:e,...t}}function me(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===pe}function we(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===le}import{z as K}from"zod";var G="waniwani/client";function $(e){if(typeof e=="object"&&e!==null)return e[G]}function he(e,t,n){return{track(r){return e.track({...r,meta:{...t,...r.meta}})},identify(r,o){return e.identify(r,o,t)},kb:e.kb,_config:n}}function D(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var Ye=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],Je=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],Xe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],Ge=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Ze=["correlationId","openai/requestId"];function _(e){return e?D(e,Ye):void 0}function ye(e){return e?D(e,Je):void 0}function Te(e){return e?D(e,Xe):void 0}function ke(e){return e?D(e,Ge):void 0}function Se(e){return e?D(e,Ze):void 0}var Qe=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function M(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:r}of Qe){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function ve(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function U(e,t){let n=t.split("."),r=e;for(let o of n){if(r==null||typeof r!="object")return;r=r[o]}return r}function et(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let s=e;for(let a of r)(s[a]==null||typeof s[a]!="object"||Array.isArray(s[a]))&&(s[a]={}),s=s[a];s[o]=n}function xe(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let s of n){if(o==null||typeof o!="object")return;o=o[s]}o!=null&&typeof o=="object"&&delete o[r]}function Z(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?et(t,n,r):t[n]=r;return t}function W(e,t){let n={...e};for(let[r,o]of Object.entries(t))o!==null&&typeof o=="object"&&!Array.isArray(o)&&n[r]!==null&&typeof n[r]=="object"&&!Array.isArray(n[r])?n[r]=W(n[r],o):n[r]=o;return n}function Q(e){return e!=null&&e!==""}async function F(e,t){return e.type==="direct"?e.to:e.condition(t)}function Re(e,t,n,r){if(e.every(c=>Q(U(r,c.field))))return null;let o=e.filter(c=>!Q(U(r,c.field))),s=o.length===1,a=o[0];return{content:s&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&a?{field:a.field}:{}}}}async function B(e,t,n,r,o,s,a){let i=e,c={...t},d=50,l=0;for(;l++<d;){if(i===b)return{content:{status:"complete"},flowTokenContent:{state:c}};let f=n.get(i);if(!f)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let p=await f({state:c,meta:s,interrupt:fe,showWidget:ge,waniwani:a});if(me(p)){for(let T of p.questions)T.validate&&o.set(`${i}:${T.field}`,T.validate);let y=Re(p.questions,p.context,i,c);if(y)return y;for(let T of p.questions){let k=o.get(`${i}:${T.field}`);if(k)try{let R=U(c,T.field),E=await k(R);E&&typeof E=="object"&&(c=W(c,E))}catch(R){let E=R instanceof Error?R.message:String(R);xe(c,T.field);let S=p.questions.map(C=>C.field===T.field?{...C,context:C.context?`ERROR: ${E}
|
|
2
2
|
|
|
3
|
-
${C.context}`:`ERROR: ${E}`}:C),P=Re(S,
|
|
3
|
+
${C.context}`:`ERROR: ${E}`}:C),P=Re(S,p.context,i,c);if(P)return P;break}}let u=r.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(u,c);continue}if(we(p)){let y=p.field;if(y&&Q(U(c,y))){let u=r.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(u,c);continue}return{content:{status:"widget",tool:p.tool.id,data:p.data,description:p.description,interactive:p.interactive!==!1},flowTokenContent:{step:i,state:c,field:y,widgetId:p.tool.id}}}c=W(c,p);let h=r.get(i);if(!h)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(h,c)}catch(g){return{content:{status:"error",error:g instanceof Error?g.message:String(g)},flowTokenContent:{step:i,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var tt="@waniwani/sdk",nt="https://app.waniwani.ai",A=class{get baseUrl(){return(process.env.WANIWANI_API_URL??nt).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");return await this.request("/api/mcp/redis/get",{key:t})??null}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");await this.request("/api/mcp/redis/set",{key:t,value:n})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let r=`${this.baseUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":tt},body:JSON.stringify(n)});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(a||`KV store API error: HTTP ${o.status}`)}return(await o.json()).data}};var O=class{store=new A;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Ee(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Ie(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow,"," including relevant prior context that led to triggering it, if available."," Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs."," The engine will auto-skip steps whose fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=[];for(let[r,o]of Object.entries(e.state)){let s=ve(o);if(s){let a=o.description??"",i=Object.entries(s).map(([c,d])=>{let l=Ee(d);return l?`\`${r}.${c}\` (${l})`:`\`${r}.${c}\``}).join(", ");n.push(a?`\`${r}\` (${a}): ${i}`:`\`${r}\`: ${i}`)}else{let a=Ee(o);n.push(a?`\`${r}\` (${a})`:`\`${r}\``)}}t.push(` Known fields: ${n.join(", ")}.`)}return t.push(" For grouped fields (shown as `group.subfield`), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Check the `interactive` field in the response:"," \u2022 `interactive: true` \u2014 The widget requires user interaction. After calling the display tool,"," STOP and WAIT for the user to interact with the widget. Do NOT call this flow tool again"," until the user has responded. When they do, call with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned."," \u2022 `interactive: false` \u2014 The widget is display-only. Call the display tool, then immediately",' call THIS flow tool again with `action: "continue"`. Do NOT wait for user interaction.',' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","4. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped."),t.join(`
|
|
4
4
|
`)}var rt={action:K.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),intent:K.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow, including relevant prior context that led to triggering it, if available. Do not invent missing context.`),stateUpdates:K.record(K.string(),K.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned.")};function Ce(e){let{config:t,nodes:n,edges:r}=e,o=Ie(t),s=`${t.description}
|
|
5
|
-
${o}`,a=e.store??new O,i=new Map;async function c(d,
|
|
6
|
-
`)}var N=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===b)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return be(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Ce({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>be(n,r)})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function _e(e){return new N(e)}function Pe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Me(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,s={registerTool:(...d)=>{r.push(d)}};await e.register(s);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.id}" did not register a handler`);let i={_meta:{sessionId:o}};async function c(d){return{...d,decodedState:n?await n.get(o):null}}return{async start(d,p){let g=await a({action:"start",intent:d,...p?{stateUpdates:p}:{}},i);return c(Pe(g))},async continueWith(d){let p=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(Pe(p))},async lastState(){return n?n.get(o):null}}}var q="text/html+skybridge",L="text/html;profile=mcp-app",We=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Fe(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Ae(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function ee(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Ne(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:a,prefersBorder:i=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:u}=new URL(o);(u==="localhost"||u==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${u}:*`,`wss://${u}:*`],resource_domains:[...d.resource_domains||[],`http://${u}:*`]})}catch{}let p=`ui://widgets/apps-sdk/${t}.html`,g=`ui://widgets/ext-apps/${t}.html`,f=null,l=()=>(f||(f=We(o,s)),f),m=r;async function y(u){let T=await l();u.registerResource(`${t}-openai-widget`,p,{title:n,description:m,mimeType:q,_meta:{"openai/widgetDescription":m,"openai/widgetPrefersBorder":i}},async k=>({contents:[{uri:k.href,mimeType:q,text:T,_meta:Fe({description:m,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),u.registerResource(`${t}-mcp-widget`,g,{title:n,description:m,mimeType:L,_meta:{ui:{prefersBorder:i}}},async k=>({contents:[{uri:k.href,mimeType:L,text:T,_meta:Ae({description:m,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:p,mcpUri:g,autoHeight:c,register:y}}function ot(e,t){let{resource:n,description:r,inputSchema:o,annotations:s,autoInjectResultText:a=!0}=e,i=e.id??n?.id,c=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let d=n?ee({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:c,description:r,async register(p){p.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(g,f)=>{let l=f,m=l._meta??{},y=$(l),u=await t(g,{extra:{_meta:m},waniwani:y});return n&&u.data?{content:[{type:"text",text:u.text}],structuredContent:u.data,_meta:{...d,...m,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:u.text}],...u.data?{structuredContent:u.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function it(e,t){await Promise.all(t.map(n=>n.register(e)))}var V=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var st="@waniwani/sdk";function De(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,a,i){let c=r(),d=`${t.replace(/\/$/,"")}${a}`,p={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":st},g={method:s,headers:p};i!==void 0&&(p["Content-Type"]="application/json",g.body=JSON.stringify(i));let f=await fetch(d,g);if(!f.ok){let m=await f.text().catch(()=>"");throw new V(m||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){return o("POST","/api/mcp/kb/search",{query:s,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var at="@waniwani/sdk";function H(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Ue,o=ut(e),s=z(e.meta),a=z(e.metadata),i=pt(e,s),c=R(e.eventId)??r(),d=lt(e.timestamp,n),p=R(e.source)??M(s)??t.source??at,g=te(e)?{...e}:void 0,f={...a};return Object.keys(s).length>0&&(f.meta=s),g&&(f.rawLegacy=g),{id:c,type:"mcp.event",name:o,source:p,timestamp:d,correlation:i,properties:ct(e,o),metadata:f,rawLegacy:g}}function Ue(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function ct(e,t){if(!te(e))return z(e.properties);let n=dt(e,t),r=z(e.properties);return{...n,...r}}function dt(e,t){switch(t){case"tool.called":{let n={};return R(e.toolName)&&(n.name=e.toolName),R(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),R(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return R(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),R(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function ut(e){return te(e)?e.eventType:e.event}function pt(e,t){let n=R(e.requestId)??ye(t),r=R(e.sessionId)??_(t),o=R(e.traceId)??Te(t),s=R(e.externalUserId)??ke(t),a=R(e.correlationId)??Se(t)??n,i={};return r&&(i.sessionId=r),o&&(i.traceId=o),n&&(i.requestId=n),a&&(i.correlationId=a),s&&(i.externalUserId=s),i}function lt(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function z(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function R(e){if(typeof e=="string"&&e.trim().length!==0)return e}function te(e){return"eventType"in e}var ft="/api/mcp/events/v2/batch";var Oe="@waniwani/sdk",gt=new Set([401,403]),mt=new Set([408,425,429,500,502,503,504]);function Ke(e){return new ne(e)}var ne=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=yt(t.apiUrl,t.endpointPath??ft),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":Oe},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Tt(s)}}if(gt.has(n.status))return{kind:"auth",status:n.status};if(mt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await ht(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Oe,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(wt(a)){o.push(i);continue}s.push(i)}}return{retryable:o,permanent:s}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function wt(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function ht(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function yt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Tt(e){return e instanceof Error?e.message:String(e)}function je(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?Ke({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,a={async identify(i,c,d){o();let p=H({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(p),{eventId:p.id}},async track(i){o();let c=H(i);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(i){return o(),await s?.shutdown({timeoutMs:i?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&kt(a,r.shutdownTimeoutMs),a}function kt(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function Y(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",r=t?.apiKey??process.env.WANIWANI_API_KEY,o={endpointPath:t?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:t?.tracking?.flushIntervalMs??1e3,maxBatchSize:t?.tracking?.maxBatchSize??20,maxBufferSize:t?.tracking?.maxBufferSize??1e3,maxRetries:t?.tracking?.maxRetries??3,retryBaseDelayMs:t?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:t?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:t?.tracking?.shutdownTimeoutMs??2e3},s={apiUrl:n,apiKey:r,tracking:o},a=je(s),i=De(s);return{...a,kb:i,_config:s}}function St(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function vt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Y(t)),n}return async function(s){let a;try{a=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let i=r(),c=[];for(let d of a.events){let p=St(d),g=await i.track(p);c.push(g.eventId)}return await i.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(i){let c=i instanceof Error?i.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var J=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=xt(this.config.apiUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let s=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!s.ok)return null;let a=await s.json(),i=a.data&&typeof a.data=="object"?a.data:a,c=new Date(i.expiresAt).getTime();return!i.token||Number.isNaN(c)?null:(this.cached={token:i.token,expiresAt:c},i.token)}catch{return null}}};function xt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var $e="waniwani/sessionId",re="waniwani/geoLocation",oe="waniwani/userLocation";function x(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function j(e){if(!x(e))return;let t=e._meta;return x(t)?t:void 0}function ie(e){if(!x(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>x(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function Rt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function se(e,t,n,r,o,s){let a=Rt(e,n.toolType),i=j(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",M(i)),{event:"tool.called",properties:{name:e,type:a,...r??{},...s?.input!==void 0&&{input:s.input},...s?.output!==void 0&&{output:s.output}},meta:i,source:M(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ae(e,t,n){try{await e.track(t)}catch(r){n?.(de(r))}}async function ce(e,t){try{await e.flush()}catch(n){t?.(de(n))}}async function Be(e,t,n,r,o){if(!x(e))return;x(e._meta)||(e._meta={});let s=e._meta,a=x(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let g=await t.getToken();g&&(i.token=g)}catch(g){o?.(de(g))}let c=_(s);c&&(i.sessionId||(i.sessionId=c));let d=Le(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let p=M(j(r));p&&!i.source&&(i.source=p),s.waniwani=i}function qe(e,t){let n=j(t);if(!n||!x(e))return;x(e._meta)||(e._meta={});let r=e._meta,o=_(n);o&&!r[$e]&&(r[$e]=o);let s=Le(n);s&&(r[re]||(r[re]=s),r[oe]||(r[oe]=s))}function Le(e){if(!e)return;let t=e[re]??e[oe];if(x(t)||typeof t=="string")return t}function de(e){return e instanceof Error?e:new Error(String(e))}var Ve="https://app.waniwani.ai";function Et(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Y(),s=r.injectWidgetToken!==!1,a=o._config.apiKey?new J({apiUrl:o._config.apiUrl??Ve,apiKey:o._config.apiKey}):null,i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,p,g]=c,f=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof g!="function")return i(...c);let l=g;return i(d,p,async(y,u)=>{let T=j(u)??{},k=he(o,T,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});x(u)&&(u[G]=k);let v=performance.now(),E=e.server?.getClientVersion?.();try{let S=await l(y,u),P=Math.round(performance.now()-v),C=x(S)&&S.isError===!0;if(C){let ue=ie(S);console.error(`[waniwani] Tool "${f}" returned error${ue?`: ${ue}`:""}`)}return await ae(o,se(f,u,r,{durationMs:P,status:C?"error":"ok",...C&&{errorMessage:ie(S)??"Unknown tool error"}},E,{input:y,output:S}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),qe(S,u),s&&await Be(S,a,o._config.apiUrl??Ve,u,r.onError),S}catch(S){let P=Math.round(performance.now()-v);throw await ae(o,se(f,u,r,{durationMs:P,status:"error",errorMessage:S instanceof Error?S.message:String(S)},E,{input:y}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),S}})}),n}export{b as END,I as START,N as StateGraph,A as WaniwaniKvStore,_e as createFlow,Me as createFlowTestHarness,Ne as createResource,ot as createTool,vt as createTrackingRoute,X as detectPlatform,He as isMCPApps,ze as isOpenAI,it as registerTools,Et as withWaniwani};
|
|
5
|
+
${o}`,a=e.store??new O,i=new Map;async function c(d,l,f,g){if(d.action==="start"){let p=typeof d.intent=="string"?d.intent.trim():void 0;if(!p)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};d.intent=p;let h=r.get(I);if(!h)return{content:{status:"error",error:"No start edge"}};let y=Z(d.stateUpdates??{}),u=await F(h,y);return B(u,y,n,r,i,f,g)}if(d.action==="continue"){if(!l)return{content:{status:"error",error:"No session ID available for continue action."}};let p;try{p=await a.get(l)}catch(T){let k=T instanceof Error?T.message:String(T);return{content:{status:"error",error:`Failed to load flow state (session "${l}"): ${k}`}}}if(!p)return{content:{status:"error",error:`Flow state not found for session "${l}". The flow may have expired.`}};let h=p.state,y=p.step;if(!y)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let u=W(h,Z(d.stateUpdates??{}));if(p.widgetId){let T=r.get(y);if(!T)return{content:{status:"error",error:`No edge from step "${y}"`}};let k=await F(T,u);return B(k,u,n,r,i,f,g)}return B(y,u,n,r,i,f,g)}return{content:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:s,graph:e.graph,async register(d){d.registerTool(t.id,{title:t.title,description:s,inputSchema:rt,annotations:t.annotations},(async(l,f)=>{let g=f,p=g._meta??{},h=_(p),y=$(g),u=await c(l,h,p,y);if(h)if(u.flowTokenContent&&u.content.status!=="complete")try{await a.set(h,u.flowTokenContent)}catch(k){let R=k instanceof Error?k.message:String(k);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${h}"): ${R}`},null,2)}],_meta:p,isError:!0}}else u.content.status==="complete"&&await a.delete(h);return{content:[{type:"text",text:JSON.stringify(u.content,null,2)}],_meta:p,...u.content.status==="error"?{isError:!0}:{}}}))}}}function be(e,t){let n=["flowchart TD"];n.push(` ${I}((Start))`);for(let[r]of e)n.push(` ${r}[${r}]`);n.push(` ${b}((End))`);for(let[r,o]of t)o.type==="direct"?n.push(` ${r} --> ${o.to}`):n.push(` ${r} -.-> ${r}_branch([?])`);return n.join(`
|
|
6
|
+
`)}var N=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===b)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return be(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Ce({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>be(n,r)})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function _e(e){return new N(e)}function Pe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Me(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,s={registerTool:(...d)=>{r.push(d)}};await e.register(s);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.id}" did not register a handler`);let i={_meta:{sessionId:o}};async function c(d){return{...d,decodedState:n?await n.get(o):null}}return{async start(d,l){let f=await a({action:"start",intent:d,...l?{stateUpdates:l}:{}},i);return c(Pe(f))},async continueWith(d){let l=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(Pe(l))},async lastState(){return n?n.get(o):null}}}var q="text/html+skybridge",L="text/html;profile=mcp-app",We=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Fe(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Ae(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function ee(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Ne(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:a,prefersBorder:i=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:u}=new URL(o);(u==="localhost"||u==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${u}:*`,`wss://${u}:*`],resource_domains:[...d.resource_domains||[],`http://${u}:*`]})}catch{}let l=`ui://widgets/apps-sdk/${t}.html`,f=`ui://widgets/ext-apps/${t}.html`,g=null,p=()=>(g||(g=We(o,s)),g),h=r;async function y(u){let T=await p();u.registerResource(`${t}-openai-widget`,l,{title:n,description:h,mimeType:q,_meta:{"openai/widgetDescription":h,"openai/widgetPrefersBorder":i}},async k=>({contents:[{uri:k.href,mimeType:q,text:T,_meta:Fe({description:h,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),u.registerResource(`${t}-mcp-widget`,f,{title:n,description:h,mimeType:L,_meta:{ui:{prefersBorder:i}}},async k=>({contents:[{uri:k.href,mimeType:L,text:T,_meta:Ae({description:h,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:l,mcpUri:f,autoHeight:c,register:y}}function ot(e,t){let{resource:n,description:r,inputSchema:o,annotations:s,autoInjectResultText:a=!0}=e,i=e.id??n?.id,c=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let d=n?ee({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:c,description:r,async register(l){l.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(f,g)=>{let p=g,h=p._meta??{},y=$(p),u=await t(f,{extra:{_meta:h},waniwani:y});return n&&u.data?{content:[{type:"text",text:u.text}],structuredContent:u.data,_meta:{...d,...h,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:u.text}],...u.data?{structuredContent:u.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function it(e,t){await Promise.all(t.map(n=>n.register(e)))}var V=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var st="@waniwani/sdk";function De(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,a,i){let c=r(),d=`${t.replace(/\/$/,"")}${a}`,l={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":st},f={method:s,headers:l};i!==void 0&&(l["Content-Type"]="application/json",f.body=JSON.stringify(i));let g=await fetch(d,f);if(!g.ok){let h=await g.text().catch(()=>"");throw new V(h||`KB API error: HTTP ${g.status}`,g.status)}return(await g.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){return o("POST","/api/mcp/kb/search",{query:s,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var at="@waniwani/sdk";function H(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Ue,o=ut(e),s=z(e.meta),a=z(e.metadata),i=pt(e,s),c=x(e.eventId)??r(),d=lt(e.timestamp,n),l=x(e.source)??M(s)??t.source??at,f=te(e)?{...e}:void 0,g={...a};return Object.keys(s).length>0&&(g.meta=s),f&&(g.rawLegacy=f),{id:c,type:"mcp.event",name:o,source:l,timestamp:d,correlation:i,properties:ct(e,o),metadata:g,rawLegacy:f}}function Ue(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function ct(e,t){if(!te(e))return z(e.properties);let n=dt(e,t),r=z(e.properties);return{...n,...r}}function dt(e,t){switch(t){case"tool.called":{let n={};return x(e.toolName)&&(n.name=e.toolName),x(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),x(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return x(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),x(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function ut(e){return te(e)?e.eventType:e.event}function pt(e,t){let n=x(e.requestId)??ye(t),r=x(e.sessionId)??_(t),o=x(e.traceId)??Te(t),s=x(e.externalUserId)??ke(t),a=x(e.correlationId)??Se(t)??n,i={};return r&&(i.sessionId=r),o&&(i.traceId=o),n&&(i.requestId=n),a&&(i.correlationId=a),s&&(i.externalUserId=s),i}function lt(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function z(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function x(e){if(typeof e=="string"&&e.trim().length!==0)return e}function te(e){return"eventType"in e}var ft="/api/mcp/events/v2/batch";var Oe="@waniwani/sdk",gt=new Set([401,403]),mt=new Set([408,425,429,500,502,503,504]);function Ke(e){return new ne(e)}var ne=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=yt(t.apiUrl,t.endpointPath??ft),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":Oe},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Tt(s)}}if(gt.has(n.status))return{kind:"auth",status:n.status};if(mt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await ht(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Oe,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(wt(a)){o.push(i);continue}s.push(i)}}return{retryable:o,permanent:s}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function wt(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function ht(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function yt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Tt(e){return e instanceof Error?e.message:String(e)}function je(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?Ke({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,a={async identify(i,c,d){o();let l=H({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(l),{eventId:l.id}},async track(i){o();let c=H(i);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(i){return o(),await s?.shutdown({timeoutMs:i?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&kt(a,r.shutdownTimeoutMs),a}function kt(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function Y(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",r=t?.apiKey??process.env.WANIWANI_API_KEY,o={endpointPath:t?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:t?.tracking?.flushIntervalMs??1e3,maxBatchSize:t?.tracking?.maxBatchSize??20,maxBufferSize:t?.tracking?.maxBufferSize??1e3,maxRetries:t?.tracking?.maxRetries??3,retryBaseDelayMs:t?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:t?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:t?.tracking?.shutdownTimeoutMs??2e3},s={apiUrl:n,apiKey:r,tracking:o},a=je(s),i=De(s);return{...a,kb:i,_config:s}}function St(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function vt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Y(t)),n}return async function(s){let a;try{a=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let i=r(),c=[];for(let d of a.events){let l=St(d),f=await i.track(l);c.push(f.eventId)}return await i.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(i){let c=i instanceof Error?i.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var J=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=xt(this.config.apiUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let s=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!s.ok)return null;let a=await s.json(),i=a.data&&typeof a.data=="object"?a.data:a,c=new Date(i.expiresAt).getTime();return!i.token||Number.isNaN(c)?null:(this.cached={token:i.token,expiresAt:c},i.token)}catch{return null}}};function xt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var $e="waniwani/sessionId",re="waniwani/geoLocation",oe="waniwani/userLocation";function v(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function j(e){if(!v(e))return;let t=e._meta;return v(t)?t:void 0}function ie(e){if(!v(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>v(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function Rt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function se(e,t,n,r,o,s){let a=Rt(e,n.toolType),i=j(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",M(i)),{event:"tool.called",properties:{name:e,type:a,...r??{},...s?.input!==void 0&&{input:s.input},...s?.output!==void 0&&{output:s.output}},meta:i,source:M(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ae(e,t,n){try{await e.track(t)}catch(r){n?.(de(r))}}async function ce(e,t){try{await e.flush()}catch(n){t?.(de(n))}}async function Be(e,t,n,r,o){if(!v(e))return;v(e._meta)||(e._meta={});let s=e._meta,a=v(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let f=await t.getToken();f&&(i.token=f)}catch(f){o?.(de(f))}let c=_(s);c&&(i.sessionId||(i.sessionId=c));let d=Le(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let l=M(j(r));l&&!i.source&&(i.source=l),s.waniwani=i}function qe(e,t){let n=j(t);if(!n||!v(e))return;v(e._meta)||(e._meta={});let r=e._meta,o=_(n);o&&!r[$e]&&(r[$e]=o);let s=Le(n);s&&(r[re]||(r[re]=s),r[oe]||(r[oe]=s))}function Le(e){if(!e)return;let t=e[re]??e[oe];if(v(t)||typeof t=="string")return t}function de(e){return e instanceof Error?e:new Error(String(e))}var Ve="https://app.waniwani.ai";function Et(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Y(),s=r.injectWidgetToken!==!1,a=o._config.apiKey?new J({apiUrl:o._config.apiUrl??Ve,apiKey:o._config.apiKey}):null,i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,l,f]=c,g=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof f!="function")return i(...c);let p=f;return i(d,l,async(y,u)=>{let T=j(u)??{},k=he(o,T,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});v(u)&&(u[G]=k);let R=performance.now(),E=e.server?.getClientVersion?.();try{let S=await p(y,u),P=Math.round(performance.now()-R),C=v(S)&&S.isError===!0;if(C){let ue=ie(S);console.error(`[waniwani] Tool "${g}" returned error${ue?`: ${ue}`:""}`)}return await ae(o,se(g,u,r,{durationMs:P,status:C?"error":"ok",...C&&{errorMessage:ie(S)??"Unknown tool error"}},E,{input:y,output:S}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),qe(S,u),s&&await Be(S,a,o._config.apiUrl??Ve,u,r.onError),S}catch(S){let P=Math.round(performance.now()-R);throw await ae(o,se(g,u,r,{durationMs:P,status:"error",errorMessage:S instanceof Error?S.message:String(S)},E,{input:y}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),S}})}),n}export{b as END,I as START,N as StateGraph,A as WaniwaniKvStore,_e as createFlow,Me as createFlowTestHarness,Ne as createResource,ot as createTool,vt as createTrackingRoute,X as detectPlatform,He as isMCPApps,ze as isOpenAI,it as registerTools,Et as withWaniwani};
|
|
7
7
|
//# sourceMappingURL=index.js.map
|