ttp-agent-sdk 2.45.4 → 2.45.7
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/agent-widget.dev.js +536 -370
- package/dist/agent-widget.dev.js.map +1 -1
- package/dist/agent-widget.esm.js +1 -1
- package/dist/agent-widget.esm.js.map +1 -1
- package/dist/agent-widget.js +1 -1
- package/dist/agent-widget.js.map +1 -1
- package/dist/index.html +150 -0
- package/package.json +1 -1
package/dist/index.html
CHANGED
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
<li><a href="#vanilla-js" class="nav-link">Vanilla JavaScript</a></li>
|
|
57
57
|
<li><a href="#react" class="nav-link">React Integration</a></li>
|
|
58
58
|
<li><a href="#voice-button" class="nav-link">VoiceButton Component</a></li>
|
|
59
|
+
<li><a href="#client-script-tools" class="nav-link">Client-Script Tools</a></li>
|
|
60
|
+
<li><a href="#chain-tools" class="nav-link">Chain Tools</a></li>
|
|
59
61
|
</ul>
|
|
60
62
|
</div>
|
|
61
63
|
|
|
@@ -2642,6 +2644,154 @@ function App() {
|
|
|
2642
2644
|
</table>
|
|
2643
2645
|
</section>
|
|
2644
2646
|
|
|
2647
|
+
<!-- Client-Script Tools -->
|
|
2648
|
+
<section id="client-script-tools" class="doc-section">
|
|
2649
|
+
<h1>Client-Script Tools</h1>
|
|
2650
|
+
<p>Client-script tools let you attach <strong>backend-authored JavaScript</strong> to a client tool (<code>tool_type: 'client'</code>) that runs in the visitor's browser when the LLM calls the tool — <strong>no host-page <code>registerToolHandler()</code> code required</strong>. The script is configured in the dashboard (tool form → "Agent scripts"), stored with the tool, and delivered to the widget automatically at session start. Available since SDK <strong>v2.45.3</strong>.</p>
|
|
2651
|
+
|
|
2652
|
+
<div class="info-box">
|
|
2653
|
+
<strong>When to use which:</strong> use <code>registerToolHandler()</code> when the host page owns the logic and ships its own JS. Use a client-script tool when the agent owner wants browser-side behavior (DOM reads, page API calls, UI nudges) configurable from the dashboard without touching the embedding site.
|
|
2654
|
+
</div>
|
|
2655
|
+
|
|
2656
|
+
<h2>How it works</h2>
|
|
2657
|
+
<ol>
|
|
2658
|
+
<li><strong>Session init</strong> — the backend pushes a <code>partner_bundle</code> message with the reserved partner id <code>__client_tools__</code>, carrying every scripted tool attached to the agent (plus auto-run library scripts). The bundle is pushed on both the voice and text-chat channels, works with or without a widget flavor, and is re-pushed on reconnect (a new bundle replaces the previous one).</li>
|
|
2659
|
+
<li><strong>Compile on load</strong> — each entry compiles into a strict-mode async function immediately when the bundle lands. A syntax error poisons only that entry (logged at load time); the rest of the bundle still works.</li>
|
|
2660
|
+
<li><strong>Invocation</strong> — when the LLM calls the tool, the SDK runs the compiled script and returns its result to the backend.</li>
|
|
2661
|
+
<li><strong>Auto-run</strong> — library scripts flagged <code>auto_run</code> execute exactly once when the bundle arrives (e.g. to set up page listeners).</li>
|
|
2662
|
+
</ol>
|
|
2663
|
+
|
|
2664
|
+
<h2>Authoring styles</h2>
|
|
2665
|
+
<p>Two styles compile — the SDK detects the shape and picks the right wrapping:</p>
|
|
2666
|
+
<pre><code>// 1. Raw statement body (typical in the dashboard UI; `ctx` is in scope)
|
|
2667
|
+
alert("hi");
|
|
2668
|
+
return { ok: true, clicked: true };
|
|
2669
|
+
|
|
2670
|
+
// 2. Function expression
|
|
2671
|
+
async (ctx) => {
|
|
2672
|
+
const res = await ctx.fetch('/api/something');
|
|
2673
|
+
return { ok: true, data: await res.json() };
|
|
2674
|
+
}</code></pre>
|
|
2675
|
+
<p>Both run inside an async function, so <code>await</code> is always allowed. A script that returns nothing resolves to <code>{ ok: true }</code>.</p>
|
|
2676
|
+
|
|
2677
|
+
<h2>The <code>ctx</code> object</h2>
|
|
2678
|
+
<table class="properties-table">
|
|
2679
|
+
<thead>
|
|
2680
|
+
<tr><th>Property</th><th>Description</th></tr>
|
|
2681
|
+
</thead>
|
|
2682
|
+
<tbody>
|
|
2683
|
+
<tr><td><code>ctx.args</code></td><td>Parameters from the LLM tool call.</td></tr>
|
|
2684
|
+
<tr><td><code>ctx.host</code></td><td><code>window.location.hostname</code>.</td></tr>
|
|
2685
|
+
<tr><td><code>ctx.fetch</code></td><td>Bound <code>window.fetch</code>.</td></tr>
|
|
2686
|
+
<tr><td><code>ctx.log(...)</code></td><td><code>console.log</code> prefixed with <code>[adapter:<tool>]</code>.</td></tr>
|
|
2687
|
+
<tr><td><code>ctx.runAdapter(action, args)</code></td><td>Invoke another script in the same bundle (max nesting depth 16).</td></tr>
|
|
2688
|
+
<tr><td><code>ctx.emit(msg)</code></td><td>Send an unsolicited JSON message back to the backend over the active channel.</td></tr>
|
|
2689
|
+
</tbody>
|
|
2690
|
+
</table>
|
|
2691
|
+
<p>Client-tool scripts run with a <strong>generic context</strong> — the ecommerce-only helpers (<code>ctx.platform</code>, <code>ctx.normalizeCart</code>, <code>ctx.refreshUI</code>) are not available; those exist only in ecommerce partner-adapter bundles.</p>
|
|
2692
|
+
|
|
2693
|
+
<h2>Pre / main / post steps</h2>
|
|
2694
|
+
<p>A scripted tool may carry up to three steps, executed in order: <strong>pre → main → post</strong>.</p>
|
|
2695
|
+
<ul>
|
|
2696
|
+
<li><strong>Pre</strong> is best-effort — if it throws, the error is logged and main still runs.</li>
|
|
2697
|
+
<li><strong>Main</strong> produces the tool result. A thrown error becomes an <code>ok: false</code> result.</li>
|
|
2698
|
+
<li><strong>Post</strong> runs only when main succeeded; its <code>ctx.args._mainResult</code> holds main's return value.</li>
|
|
2699
|
+
</ul>
|
|
2700
|
+
|
|
2701
|
+
<h2>Sync vs async results</h2>
|
|
2702
|
+
<p>Configured per tool in the dashboard ("Wait for result"):</p>
|
|
2703
|
+
<table class="properties-table">
|
|
2704
|
+
<thead>
|
|
2705
|
+
<tr><th>Mode</th><th>Behavior</th></tr>
|
|
2706
|
+
</thead>
|
|
2707
|
+
<tbody>
|
|
2708
|
+
<tr><td><strong>Wait ON</strong> (sync)</td><td>The backend awaits the script result up to <code>timeoutMs</code> (1000–15000 ms, default 8000) and feeds it to the LLM as the tool result.</td></tr>
|
|
2709
|
+
<tr><td><strong>Wait OFF</strong> (async)</td><td>The LLM immediately receives the configured pending message; the real result is injected later: <code>SPOKEN_EVENT</code> (spoken immediately), <code>SPOKEN_DEFERRED</code> (next natural turn, default), or <code>SILENT_CONTEXT</code> (silent context update).</td></tr>
|
|
2710
|
+
</tbody>
|
|
2711
|
+
</table>
|
|
2712
|
+
|
|
2713
|
+
<h2>Limits</h2>
|
|
2714
|
+
<table class="properties-table">
|
|
2715
|
+
<thead>
|
|
2716
|
+
<tr><th>Limit</th><th>Value</th></tr>
|
|
2717
|
+
</thead>
|
|
2718
|
+
<tbody>
|
|
2719
|
+
<tr><td>Per-step code size</td><td>32 KB</td></tr>
|
|
2720
|
+
<tr><td>Result size</td><td>50 KB serialized (oversize → <code>ok: false, error: "RESULT_TOO_LARGE"</code>)</td></tr>
|
|
2721
|
+
<tr><td>Sync timeout</td><td>1000–15000 ms (default 8000)</td></tr>
|
|
2722
|
+
<tr><td>Pending message</td><td>500 chars</td></tr>
|
|
2723
|
+
</tbody>
|
|
2724
|
+
</table>
|
|
2725
|
+
|
|
2726
|
+
<h2>Wire protocol (reference)</h2>
|
|
2727
|
+
<p>Script invocations arrive on two wire forms, both handled by the SDK on both channels:</p>
|
|
2728
|
+
<pre><code>// Bundle (session init / reconnect)
|
|
2729
|
+
{ "t": "partner_bundle", "partner_id": "__client_tools__",
|
|
2730
|
+
"adapters": { "my_tool": { "code_js": "...", "pre_code_js": "...", "post_code_js": "..." } },
|
|
2731
|
+
"autoRun": { "my_script": { "code_js": "..." } } }
|
|
2732
|
+
|
|
2733
|
+
// Form A — dedicated envelope (async path / chain steps)
|
|
2734
|
+
→ { "t": "run_partner_script", "requestId": "...", "partnerId": "__client_tools__",
|
|
2735
|
+
"action": "my_tool", "args": { ... } }
|
|
2736
|
+
← { "t": "run_partner_script_result", "requestId": "...", "ok": true, "result": { ... } }
|
|
2737
|
+
|
|
2738
|
+
// Form B — client_tool_call piggyback (backend sync-await path)
|
|
2739
|
+
→ { "t": "client_tool_call", "toolCallId": "...", "toolName": "run_partner_script",
|
|
2740
|
+
"parameters": { "action": "my_tool", "args": { ... }, "partner_id": "__client_tools__" } }
|
|
2741
|
+
← { "t": "client_tool_result", "toolCallId": "...", "result": { ... } }</code></pre>
|
|
2742
|
+
<p>Routing rule: <code>partner_id === '__client_tools__'</code> → the flavor-independent <code>ClientScriptManager</code>; any other partner id → the ecommerce flavor's partner-adapter path. The two bundles coexist.</p>
|
|
2743
|
+
|
|
2744
|
+
<h2>Troubleshooting</h2>
|
|
2745
|
+
<table class="properties-table">
|
|
2746
|
+
<thead>
|
|
2747
|
+
<tr><th>Symptom</th><th>Cause / fix</th></tr>
|
|
2748
|
+
</thead>
|
|
2749
|
+
<tbody>
|
|
2750
|
+
<tr><td><code>compile failed ... SyntaxError</code> at bundle load</td><td>Script doesn't parse. On SDK < 2.45.1, raw statement bodies (e.g. <code>alert("stop");</code>) failed even when valid — check the SDK version banner in the console and hard-refresh if stale.</td></tr>
|
|
2751
|
+
<tr><td><code>NO_HANDLER</code> for <code>run_partner_script</code></td><td>SDK < 2.45.3 didn't route the <code>client_tool_call</code> piggyback form without an ecommerce flavor. Fixed in 2.45.3.</td></tr>
|
|
2752
|
+
<tr><td><code>adapter_not_in_bundle</code></td><td>Tool not attached to the agent, or the bundle hasn't arrived yet. Check the session-start <code>partner_bundle</code> log.</td></tr>
|
|
2753
|
+
<tr><td>Sync result is a timeout</td><td>Script exceeded <code>timeoutMs</code>. Raise it (max 15 s) or switch to async delivery.</td></tr>
|
|
2754
|
+
</tbody>
|
|
2755
|
+
</table>
|
|
2756
|
+
<p>For the full internals reference see <code>CLIENT_SCRIPT_TOOLS_GUIDE.md</code> in the repository root.</p>
|
|
2757
|
+
</section>
|
|
2758
|
+
|
|
2759
|
+
<!-- Chain Tools -->
|
|
2760
|
+
<section id="chain-tools" class="doc-section">
|
|
2761
|
+
<h1>Chain Tools</h1>
|
|
2762
|
+
<p>A chain tool (<code>tool_type: 'chain'</code>) runs a <strong>graph of steps</strong> — server tools, client tools, agent switches, and library scripts — as a single LLM tool call. Chains are edited visually in the dashboard (Chain Editor: nodes are steps, edges are data-flow dependencies) and executed by the backend; the widget participates only when a step targets the browser.</p>
|
|
2763
|
+
|
|
2764
|
+
<h2>Execution model</h2>
|
|
2765
|
+
<ul>
|
|
2766
|
+
<li>Steps with no dependencies run first; steps at the same level run <strong>in parallel</strong>; levels run sequentially.</li>
|
|
2767
|
+
<li>A step's <code>input_from</code> lists the upstream steps whose outputs feed it. Dependent steps receive the original LLM arguments shallow-merged with each upstream result (a non-object result lands under the upstream step's id).</li>
|
|
2768
|
+
<li><strong>Fail-fast</strong>: the first failing step aborts the chain with <code>{ "success": false, "error": ..., "step": ... }</code>.</li>
|
|
2769
|
+
<li>The LLM receives the terminal step's result (multiple terminals merge keyed by step id).</li>
|
|
2770
|
+
<li>Inside a chain, every step is awaited — a referenced client tool's own "wait for result = off" setting is ignored.</li>
|
|
2771
|
+
</ul>
|
|
2772
|
+
|
|
2773
|
+
<h2>Step types and the widget</h2>
|
|
2774
|
+
<table class="properties-table">
|
|
2775
|
+
<thead>
|
|
2776
|
+
<tr><th>Step type</th><th>Runs where</th><th>Widget involvement</th></tr>
|
|
2777
|
+
</thead>
|
|
2778
|
+
<tbody>
|
|
2779
|
+
<tr><td><code>server_tool</code></td><td>Backend (webhook)</td><td>None.</td></tr>
|
|
2780
|
+
<tr><td><code>switch_agent</code></td><td>Backend</td><td>None.</td></tr>
|
|
2781
|
+
<tr><td><code>client_tool</code> (plain)</td><td>Browser</td><td>Arrives as a normal <code>client_tool_call</code> → your <code>registerToolHandler()</code> handler.</td></tr>
|
|
2782
|
+
<tr><td><code>client_tool</code> (scripted)</td><td>Browser</td><td>Dispatched as a <code>__client_tools__</code> bundle action keyed by <strong>tool name</strong>.</td></tr>
|
|
2783
|
+
<tr><td><code>client_script</code> (library)</td><td>Browser</td><td>Dispatched as a <code>__client_tools__</code> bundle action keyed by <code>script:{scriptId}</code>.</td></tr>
|
|
2784
|
+
</tbody>
|
|
2785
|
+
</table>
|
|
2786
|
+
<p>Scripts and scripted client tools referenced by a chain are resolved into the session bundle automatically — they do <strong>not</strong> need to be attached to the agent.</p>
|
|
2787
|
+
|
|
2788
|
+
<h2>Constraints</h2>
|
|
2789
|
+
<ul>
|
|
2790
|
+
<li>1–30 steps per chain; no cycles; chains cannot reference other chain tools (no recursion).</li>
|
|
2791
|
+
<li>Deleting a tool or library script that a chain references is blocked in the dashboard (the delete dialog lists the referencing chains).</li>
|
|
2792
|
+
</ul>
|
|
2793
|
+
</section>
|
|
2794
|
+
|
|
2645
2795
|
<!-- VoiceSDK API -->
|
|
2646
2796
|
<section id="voicesdk" class="doc-section">
|
|
2647
2797
|
<h1>VoiceSDK Class</h1>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ttp-agent-sdk",
|
|
3
|
-
"version": "2.45.
|
|
3
|
+
"version": "2.45.7",
|
|
4
4
|
"description": "Comprehensive Voice Agent SDK with Customizable Widget - Real-time audio, WebSocket communication, React components, and extensive customization options",
|
|
5
5
|
"main": "dist/agent-widget.js",
|
|
6
6
|
"module": "dist/agent-widget.esm.js",
|