@waniwani/sdk 0.2.2-beta.2 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mcp/index.d.ts +54 -59
- package/dist/mcp/index.js +3 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react.d.ts +13 -18
- package/dist/mcp/react.js +6 -6
- package/dist/mcp/react.js.map +1 -1
- package/package.json +1 -1
package/dist/mcp/index.d.ts
CHANGED
|
@@ -301,6 +301,52 @@ type RegisteredResource = {
|
|
|
301
301
|
register: (server: McpServer) => Promise<void>;
|
|
302
302
|
};
|
|
303
303
|
|
|
304
|
+
type ToolHandlerContext = {
|
|
305
|
+
/** Raw MCP request extra data (includes _meta for session extraction) */
|
|
306
|
+
extra?: {
|
|
307
|
+
_meta?: Record<string, unknown>;
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
type ToolConfig<TInput extends ZodRawShapeCompat> = {
|
|
311
|
+
/** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
|
|
312
|
+
resource?: RegisteredResource;
|
|
313
|
+
/** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
|
|
314
|
+
id?: string;
|
|
315
|
+
/** Display title. Defaults to resource.title when resource is present, required otherwise. */
|
|
316
|
+
title?: string;
|
|
317
|
+
/** Action-oriented description for the tool (tells the model WHEN to use it) */
|
|
318
|
+
description: string;
|
|
319
|
+
/** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
|
|
320
|
+
widgetDescription?: string;
|
|
321
|
+
/** Input schema using zod */
|
|
322
|
+
inputSchema: TInput;
|
|
323
|
+
/** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
|
|
324
|
+
invoking?: string;
|
|
325
|
+
/** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
|
|
326
|
+
invoked?: string;
|
|
327
|
+
/** Annotations describe the tool's potential impact. */
|
|
328
|
+
annotations?: {
|
|
329
|
+
readOnlyHint?: boolean;
|
|
330
|
+
idempotentHint?: boolean;
|
|
331
|
+
openWorldHint?: boolean;
|
|
332
|
+
destructiveHint?: boolean;
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
|
|
336
|
+
/** Text content to return */
|
|
337
|
+
text: string;
|
|
338
|
+
/** Structured data to pass to the widget. Only meaningful when resource is present. */
|
|
339
|
+
data?: Record<string, unknown>;
|
|
340
|
+
}>;
|
|
341
|
+
type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
|
|
342
|
+
type RegisteredTool = {
|
|
343
|
+
id: string;
|
|
344
|
+
title: string;
|
|
345
|
+
description: string;
|
|
346
|
+
/** Register the tool on the server */
|
|
347
|
+
register: (server: McpServer) => Promise<void>;
|
|
348
|
+
};
|
|
349
|
+
|
|
304
350
|
declare const START: "__start__";
|
|
305
351
|
declare const END: "__end__";
|
|
306
352
|
declare const INTERRUPT: unique symbol;
|
|
@@ -329,9 +375,9 @@ type InterruptSignal = {
|
|
|
329
375
|
};
|
|
330
376
|
type WidgetSignal = {
|
|
331
377
|
readonly __type: typeof WIDGET;
|
|
332
|
-
/** The
|
|
333
|
-
|
|
334
|
-
/** Data to pass to the
|
|
378
|
+
/** The display tool to delegate rendering to */
|
|
379
|
+
tool: RegisteredTool;
|
|
380
|
+
/** Data to pass to the display tool */
|
|
335
381
|
data: Record<string, unknown>;
|
|
336
382
|
/** Description of what the widget does (for the AI's context) */
|
|
337
383
|
description?: string;
|
|
@@ -370,12 +416,15 @@ declare function interrupt(config: {
|
|
|
370
416
|
context?: string;
|
|
371
417
|
}): InterruptSignal;
|
|
372
418
|
/**
|
|
373
|
-
* Create a widget signal — pauses the flow and
|
|
419
|
+
* Create a widget signal — pauses the flow and delegates rendering to a display tool.
|
|
420
|
+
*
|
|
421
|
+
* The display tool is a regular `createTool()` with a resource attached.
|
|
422
|
+
* The flow engine will instruct the model to call the display tool separately.
|
|
374
423
|
*
|
|
375
424
|
* Pass `field` to enable auto-skip: if the field is already in state, the widget
|
|
376
425
|
* step will be skipped automatically.
|
|
377
426
|
*/
|
|
378
|
-
declare function showWidget(
|
|
427
|
+
declare function showWidget(tool: RegisteredTool, config: {
|
|
379
428
|
data: Record<string, unknown>;
|
|
380
429
|
description?: string;
|
|
381
430
|
field?: string;
|
|
@@ -394,14 +443,6 @@ type NodeConfig<TState extends Record<string, unknown> = Record<string, unknown>
|
|
|
394
443
|
* the node is auto-skipped. (Alternatively, pass `field` to `showWidget()`.)
|
|
395
444
|
*/
|
|
396
445
|
field?: Extract<keyof TState, string>;
|
|
397
|
-
/**
|
|
398
|
-
* Mark this node as conversational — the AI will engage in back-and-forth
|
|
399
|
-
* conversation before advancing to the next node.
|
|
400
|
-
*
|
|
401
|
-
* - `true` — generic conversational behavior
|
|
402
|
-
* - `string` — specific guidance for the AI (e.g., "Help the user compare plans")
|
|
403
|
-
*/
|
|
404
|
-
conversational?: boolean | string;
|
|
405
446
|
};
|
|
406
447
|
/**
|
|
407
448
|
* Node handler — a single function type for all node kinds.
|
|
@@ -584,52 +625,6 @@ declare function createFlow<const TSchema extends Record<string, z.ZodType>>(con
|
|
|
584
625
|
*/
|
|
585
626
|
declare function createResource(config: ResourceConfig): RegisteredResource;
|
|
586
627
|
|
|
587
|
-
type ToolHandlerContext = {
|
|
588
|
-
/** Raw MCP request extra data (includes _meta for session extraction) */
|
|
589
|
-
extra?: {
|
|
590
|
-
_meta?: Record<string, unknown>;
|
|
591
|
-
};
|
|
592
|
-
};
|
|
593
|
-
type ToolConfig<TInput extends ZodRawShapeCompat> = {
|
|
594
|
-
/** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
|
|
595
|
-
resource?: RegisteredResource;
|
|
596
|
-
/** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
|
|
597
|
-
id?: string;
|
|
598
|
-
/** Display title. Defaults to resource.title when resource is present, required otherwise. */
|
|
599
|
-
title?: string;
|
|
600
|
-
/** Action-oriented description for the tool (tells the model WHEN to use it) */
|
|
601
|
-
description: string;
|
|
602
|
-
/** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
|
|
603
|
-
widgetDescription?: string;
|
|
604
|
-
/** Input schema using zod */
|
|
605
|
-
inputSchema: TInput;
|
|
606
|
-
/** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
|
|
607
|
-
invoking?: string;
|
|
608
|
-
/** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
|
|
609
|
-
invoked?: string;
|
|
610
|
-
/** Annotations describe the tool's potential impact. */
|
|
611
|
-
annotations?: {
|
|
612
|
-
readOnlyHint?: boolean;
|
|
613
|
-
idempotentHint?: boolean;
|
|
614
|
-
openWorldHint?: boolean;
|
|
615
|
-
destructiveHint?: boolean;
|
|
616
|
-
};
|
|
617
|
-
};
|
|
618
|
-
type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
|
|
619
|
-
/** Text content to return */
|
|
620
|
-
text: string;
|
|
621
|
-
/** Structured data to pass to the widget. Only meaningful when resource is present. */
|
|
622
|
-
data?: Record<string, unknown>;
|
|
623
|
-
}>;
|
|
624
|
-
type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
|
|
625
|
-
type RegisteredTool = {
|
|
626
|
-
id: string;
|
|
627
|
-
title: string;
|
|
628
|
-
description: string;
|
|
629
|
-
/** Register the tool on the server */
|
|
630
|
-
register: (server: McpServer) => Promise<void>;
|
|
631
|
-
};
|
|
632
|
-
|
|
633
628
|
/**
|
|
634
629
|
* Creates an MCP tool with minimal boilerplate.
|
|
635
630
|
*
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
2
|
-
`)}function ke(e){if(e.flowToken){let t=
|
|
3
|
-
${a}`;async function s(d,f){let g=ke(d),u=g.state;if(d.action==="start"){let l=o.get(S);if(!l)return{payload:{status:"error",error:"No start edge"}};let c={...u,...d.stateUpdates??{}},p=await M(l,c);return B(p,c,n,r,o,f)}if(d.action==="continue"){let l=g.step;if(!l)return{payload:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let c={...u,...d.stateUpdates??{}};if(g.questions){let p=te(g.questions,g.interruptContext,l,c,r.get(l)?.conversational);if(p)return p}if(g.questions||g.widgetId){let p=o.get(l);if(!p)return{payload:{status:"error",error:`No edge from step "${l}"`}};let T=await M(p,c);return B(T,c,n,r,o,f)}return B(l,c,n,r,o,f)}return{payload:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:i,async register(d){d.registerTool(t.id,{title:t.title,description:i,inputSchema:ve,annotations:t.annotations,_meta:{"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0}},(async(f,g)=>{let l=g._meta??{},c=await s(f,l),p=c.flowMeta?Q({step:c.flowMeta.step??"",state:c.flowMeta.state,field:c.flowMeta.field,widgetId:c.flowMeta.widgetId,questions:c.flowMeta.questions,interruptContext:c.flowMeta.interruptContext}):void 0,T={...c.payload,...p?{flowToken:p,flowId:t.id,...c.flowMeta?.widgetId?{widgetId:c.flowMeta.widgetId}:{}}:{}},w=[{type:"text",text:JSON.stringify(T,null,2)}],v={...c.widgetMeta??{},...l};return c.widgetMeta?{content:w,structuredContent:c.data,_meta:v}:{content:w,...Object.keys(v).length>0?{_meta:v}:{}}}))}}}var x=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===S||t===E)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`);let o,a={};if(typeof n=="function")o=n;else if(r)o=r,a=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,a),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}compile(){return this.validate(),ne({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(S))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(S);if(t?.type==="direct"&&t.to!==E&&!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!==S&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==E&&!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 re(e){return new x(e)}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:d=!0}=e,f=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(f={...f,connect_domains:[...f.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...f.resource_domains||[],`http://${w}:*`]})}catch{}let g=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,l=null,c=()=>(l||(l=J(o,a)),l),p=r;async function T(w){let v=await c();w.registerResource(`${t}-openai-widget`,g,{title:n,description:p,mimeType:C,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":s}},async k=>({contents:[{uri:k.href,mimeType:C,text:v,_meta:Z({description:p,prefersBorder:s,widgetDomain:i,widgetCSP:f})}]})),w.registerResource(`${t}-mcp-widget`,u,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:s}}},async k=>({contents:[{uri:k.href,mimeType:_,text:v,_meta:G({description:p,prefersBorder:s,widgetCSP:f})}]}))}return{id:t,title:n,description:r,openaiUri:g,mcpUri:u,autoHeight:d,register:T}}function Se(e,t){let{resource:n,description:r,inputSchema:o,annotations:a}=e,i=e.id??n?.id,s=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!s)throw new Error("createTool: `title` is required when no resource is provided");let d=n?R({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:s,description:r,async register(f){f.registerTool(i,{title:s,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(g,u)=>{let c=u._meta??{},p=await t(g,{extra:{_meta:c}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...d,...c}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var P=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let d=r(),f=`${t.replace(/\/$/,"")}${i}`,g={Authorization:`Bearer ${d}`,"X-WaniWani-SDK":xe},u={method:a,headers:g};s!==void 0&&(g["Content-Type"]="application/json",u.body=JSON.stringify(s));let l=await fetch(f,u);if(!l.ok){let p=await l.text().catch(()=>"");throw new P(p||`KB API error: HTTP ${l.status}`,l.status)}return(await l.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),a=W(e.meta),i=W(e.metadata),s=Ce(e,a),d=y(e.eventId)??r(),f=_e(e.timestamp,n),g=y(e.source)??t.source??Re,u=$(e)?{...e}:void 0,l={...i};return Object.keys(a).length>0&&(l.meta=a),u&&(l.rawLegacy=u),{id:d,type:"mcp.event",name:o,source:g,timestamp:f,correlation:s,properties:Ie(e,o),metadata:l,rawLegacy:u}}function se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ie(e,t){if(!$(e))return W(e.properties);let n=Me(e,t),r=W(e.properties);return{...n,...r}}function Me(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function Ce(e,t){let n=y(e.requestId)??b(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??b(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??b(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=y(e.externalUserId)??b(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??b(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function _e(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 b(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function W(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Ae=new Set([408,425,429,500,502,503,504]);function de(e){return new L(e)}var L=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=De(t.baseUrl,t.endpointPath??Pe),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":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ue(a)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Ae.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(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:ae,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Fe(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}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 Fe(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 Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function ce(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?de({baseUrl: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,i={async track(s){o();let d=j(s);return a?.enqueue(d),{eventId:d.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&Oe(i,r.shutdownTimeoutMs),i}function Oe(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 A(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=ce(o),i=ie(o);return{...a,kb:i,_config:o}}function Be(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 qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=A(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),d=[];for(let f of i.events){let g=Be(f),u=await s.track(g);d.push(u.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:d.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let d=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:d}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=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=je(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,d=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(d)?null:(this.cached={token:s.token,expiresAt:d},s.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??A(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=r._config.apiKey;return d?(a=new F({baseUrl:r._config.baseUrl??ue,apiKey:d}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[f,g,u]=d,l=typeof f=="string"&&f.trim().length>0?f:"unknown";if(typeof u!="function")return s(...d);let c=u;return s(f,g,async(T,w)=>{let v=performance.now();try{let k=await c(T,w),D=Math.round(performance.now()-v);return await pe(r,le(l,w,t,{durationMs:D,status:"ok"}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),o&&await Le(k,i(),r._config.baseUrl??ue,t.onError),k}catch(k){let D=Math.round(performance.now()-v);throw await pe(r,le(l,w,t,{durationMs:D,status:"error",errorMessage:k instanceof Error?k.message:String(k)}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),k}})}),n}async function Le(e,t,n,r){if(!N(e))return;N(e._meta)||(e._meta={});let o=e._meta,a={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(a.token=i)}catch(i){r?.(V(i))}o.waniwani=a}function le(e,t,n,r){let o=Ve(e,n.toolType),a=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!N(e))return;let t=e._meta;if(N(t))return t}function N(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function pe(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function ge(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{E as END,S as START,x as StateGraph,re as createFlow,oe as createResource,Se as createTool,qe as createTrackingRoute,U as detectPlatform,z as interrupt,he as isMCPApps,fe as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
|
|
1
|
+
function D(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ge(){return D()==="openai"}function he(){return D()==="mcp-apps"}var k="__start__",S="__end__",U=Symbol.for("waniwani.flow.interrupt"),H=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:U,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:U,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:H,tool:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===U}function J(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===H}import{z as R}from"zod";import{deflateSync as me,inflateSync as we}from"zlib";function X(e){let t=JSON.stringify(e);return me(t).toString("base64")}function Z(e){try{let t=Buffer.from(e,"base64"),n=we(t).toString("utf-8");return JSON.parse(n)}catch{return null}}function ye(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 Te(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. 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=Object.entries(e.state).map(([r,o])=>{let s=ye(o);return s?`\`${r}\` (${s})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("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"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `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."," Present the widget result to the user. When the user makes a choice or interacts"," with the widget, call THIS flow tool again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. 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(`
|
|
2
|
+
`)}function ke(e){if(e.flowToken){let t=Z(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function I(e,t){return e.type==="direct"?e.to:e.condition(t)}function O(e){return e!=null&&e!==""}function G(e,t,n,r){if(e.every(c=>O(r[c.field])))return null;let o=e.filter(c=>!O(r[c.field])),s=o.length===1,i=o[0];return{content:s&&i?{status:"interrupt",question:i.question,field:i.field,...i.suggestions?{suggestions:i.suggestions}:{},...i.context||t?{context:i.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&i?{field:i.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function B(e,t,n,r,o,s){let i=e,a={...t},c=50,l=0;for(;l++<c;){if(i===S)return{content:{status:"complete"},flowTokenContent:{state:a}};let f=n.get(i);if(!f)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await f(a,s);if(Y(d)){let g=G(d.questions,d.context,i,a);if(g)return g;let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}if(J(d)){let g=d.field??r.get(i)?.field;if(g&&O(a[g])){let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}return{content:{status:"widget",tool:d.tool.id,data:d.data,description:d.description},flowTokenContent:{step:i,state:a,field:g,widgetId:d.tool.id}}}a={...a,...d};let u=o.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(u,a)}catch(d){return{content:{status:"error",error:d instanceof Error?d.message:String(d)},flowTokenContent:{step:i,state:a}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Se={action:R.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:R.record(R.string(),R.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."),flowToken:R.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function Q(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,s=Te(t),i=`${t.description}
|
|
3
|
+
${s}`;async function a(c,l){let f=ke(c),d=f.state;if(c.action==="start"){let u=o.get(k);if(!u)return{content:{status:"error",error:"No start edge"}};let g={...d,...c.stateUpdates??{}},p=await I(u,g);return B(p,g,n,r,o,l)}if(c.action==="continue"){let u=f.step;if(!u)return{content:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let g={...d,...c.stateUpdates??{}};if(f.questions){let p=G(f.questions,f.interruptContext,u,g);if(p)return p}if(f.questions||f.widgetId){let p=o.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let v=await I(p,g);return B(v,g,n,r,o,l)}return B(u,g,n,r,o,l)}return{content:{status:"error",error:`Unknown action: "${c.action}"`}}}return{id:t.id,title:t.title,description:i,async register(c){c.registerTool(t.id,{title:t.title,description:i,inputSchema:Se,annotations:t.annotations},(async(l,f)=>{let u=f._meta??{},g=await a(l,u),p=g.flowTokenContent?X(g.flowTokenContent):void 0,v={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(v,null,2)}],...u}}))}}}var E=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===k||t===S)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`);let o,s={};if(typeof n=="function")o=n;else if(r)o=r,s=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,s),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}compile(){return this.validate(),Q({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(k))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(k);if(t?.type==="direct"&&t.to!==S&&!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!==k&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==S&&!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 ee(e){return new E(e)}var b="text/html+skybridge",_="text/html;profile=mcp-app",te=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function ne(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function re(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 q(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}}}}}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:i,prefersBorder:a=!0,autoHeight:c=!0}=e,l=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...l.resource_domains||[],`http://${w}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,u=null,g=()=>(u||(u=te(o,s)),u),p=r;async function v(w){let x=await g();w.registerResource(`${t}-openai-widget`,f,{title:n,description:p,mimeType:b,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":a}},async T=>({contents:[{uri:T.href,mimeType:b,text:x,_meta:ne({description:p,prefersBorder:a,widgetDomain:i,widgetCSP:l})}]})),w.registerResource(`${t}-mcp-widget`,d,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:a}}},async T=>({contents:[{uri:T.href,mimeType:_,text:x,_meta:re({description:p,prefersBorder:a,widgetCSP:l})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:d,autoHeight:c,register:v}}function ve(e,t){let{resource:n,description:r,inputSchema:o,annotations:s}=e,i=e.id??n?.id,a=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!a)throw new Error("createTool: `title` is required when no resource is provided");let c=n?q({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:a,description:r,async register(l){l.registerTool(i,{title:a,description:r,inputSchema:o,annotations:s,...c&&{_meta:c}},(async(f,d)=>{let g=d._meta??{},p=await t(f,{extra:{_meta:g}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...c,...g}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var M=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,i,a){let c=r(),l=`${t.replace(/\/$/,"")}${i}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":xe},d={method:s,headers:f};a!==void 0&&(f["Content-Type"]="application/json",d.body=JSON.stringify(a));let u=await fetch(l,d);if(!u.ok){let p=await u.text().catch(()=>"");throw new M(p||`KB API error: HTTP ${u.status}`,u.status)}return(await u.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,i){return o("POST","/api/mcp/kb/search",{query:s,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),s=P(e.meta),i=P(e.metadata),a=_e(e,s),c=y(e.eventId)??r(),l=Me(e.timestamp,n),f=y(e.source)??t.source??Re,d=$(e)?{...e}:void 0,u={...i};return Object.keys(s).length>0&&(u.meta=s),d&&(u.rawLegacy=d),{id:c,type:"mcp.event",name:o,source:f,timestamp:l,correlation:a,properties:Ie(e,o),metadata:u,rawLegacy:d}}function se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ie(e,t){if(!$(e))return P(e.properties);let n=Ce(e,t),r=P(e.properties);return{...n,...r}}function Ce(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function _e(e,t){let n=y(e.requestId)??C(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??C(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??C(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),s=y(e.externalUserId)??C(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??C(t,["correlationId","openai/requestId"])??n,a={};return r&&(a.sessionId=r),o&&(a.traceId=o),n&&(a.requestId=n),i&&(a.correlationId=i),s&&(a.externalUserId=s),a}function Me(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 C(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function P(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Fe=new Set([408,425,429,500,502,503,504]);function ce(e){return new L(e)}var L=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=De(t.baseUrl,t.endpointPath??Pe),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":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Ue(s)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Fe.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(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:ae,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],s=[];for(let i of n){let a=r.get(i.eventId);if(a){if(Ae(i)){o.push(a);continue}s.push(a)}}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 Ae(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 Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function de(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?ce({baseUrl: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,i={async track(a){o();let c=j(a);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(a){return o(),await s?.shutdown({timeoutMs:a?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&Be(i,r.shutdownTimeoutMs),i}function Be(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 W(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},s=de(o),i=ie(o);return{...s,kb:i,_config:o}}function Oe(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 qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=W(t)),n}return async function(s){let i;try{i=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=r(),c=[];for(let l of i.events){let f=Oe(l),d=await a.track(f);c.push(d.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let c=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=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=je(this.config.baseUrl,"/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 i=await s.json(),a=i.data&&typeof i.data=="object"?i.data:i,c=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(c)?null:(this.cached={token:a.token,expiresAt:c},a.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??W(t.config),o=t.injectWidgetToken!==!1,s=null;function i(){if(s)return s;let c=r._config.apiKey;return c?(s=new F({baseUrl:r._config.baseUrl??ue,apiKey:c}),s):null}let a=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[l,f,d]=c,u=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof d!="function")return a(...c);let g=d;return a(l,f,async(v,w)=>{let x=performance.now();try{let T=await g(v,w),N=Math.round(performance.now()-x);return await le(r,pe(u,w,t,{durationMs:N,status:"ok"}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),o&&await Le(T,i(),r._config.baseUrl??ue,t.onError),T}catch(T){let N=Math.round(performance.now()-x);throw await le(r,pe(u,w,t,{durationMs:N,status:"error",errorMessage:T instanceof Error?T.message:String(T)}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),T}})}),n}async function Le(e,t,n,r){if(!A(e))return;A(e._meta)||(e._meta={});let o=e._meta,s={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(s.token=i)}catch(i){r?.(V(i))}o.waniwani=s}function pe(e,t,n,r){let o=Ve(e,n.toolType),s=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:s,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!A(e))return;let t=e._meta;if(A(t))return t}function A(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function le(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function fe(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{S as END,k as START,E as StateGraph,ee as createFlow,oe as createResource,ve as createTool,qe as createTrackingRoute,D as detectPlatform,z as interrupt,he as isMCPApps,ge as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
|
|
4
4
|
//# sourceMappingURL=index.js.map
|