keyring-chatbot-agent-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ {
2
+ "recommendations": [
3
+ "dbaeumer.vscode-eslint",
4
+ "esbenp.prettier-vscode"
5
+ ]
6
+ }
package/README.md ADDED
@@ -0,0 +1,490 @@
1
+ # Keyring Chatbot Agent SDK
2
+
3
+ An AI-powered Web3 chatbot SDK for React applications. Provides a floating chat widget with on-chain capabilities: token swaps, NFT management, token transfers, and AI-driven assistance.
4
+
5
+ ## Features
6
+
7
+ - 🤖 **AI-Powered Chat** — Natural language understanding via Moralis Cortex (GPT-4.1-mini)
8
+ - 💱 **Token Swaps** — Best-price routing powered by deBridge
9
+ - 🖼️ **NFT Management** — View and transfer ERC721 / ERC1155 NFTs
10
+ - 💰 **Token Operations** — Send tokens, approve spending, wrap/unwrap native tokens
11
+ - 📊 **Trending Tokens** — Real-time trending data per chain
12
+ - 🔗 **Multi-Chain** — Ethereum, Optimism, BNB Chain, Polygon, Base, Arbitrum, Avalanche, Linea
13
+ - 🌐 **Internationalization** — English, Japanese, Chinese UI
14
+ - ⚡ **Built with Vite** — Optimized ES module and UMD builds
15
+ - 🔧 **Full TypeScript** — Complete type declarations included
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install keyring-chatbot-agent-sdk-test
23
+ # or
24
+ yarn add keyring-chatbot-agent-sdk-test
25
+ ```
26
+
27
+ **Peer dependencies** (React 17, 18, or 19):
28
+
29
+ ```bash
30
+ npm install react react-dom
31
+ ```
32
+
33
+ ---
34
+
35
+ ## Quick Start (React)
36
+
37
+ ```tsx
38
+ import { ChatWidget } from 'keyring-chatbot-agent-sdk-test';
39
+ import type {
40
+ Transaction,
41
+ TransactionResult,
42
+ } from 'keyring-chatbot-agent-sdk-test';
43
+
44
+ function App() {
45
+ const handleTransaction = async (
46
+ tx: Transaction
47
+ ): Promise<TransactionResult> => {
48
+ try {
49
+ // Sign and send via your wallet provider (wagmi, ethers.js, viem, etc.)
50
+ const hash = await walletClient.sendTransaction(tx);
51
+ return { status: 'success', transactionHash: hash };
52
+ } catch (err: unknown) {
53
+ return { status: 'fail', error: (err as Error).message };
54
+ }
55
+ };
56
+
57
+ return (
58
+ <ChatWidget
59
+ account={{ address: '0x...your-wallet-address', chainId: 10 }}
60
+ onTransaction={handleTransaction}
61
+ />
62
+ );
63
+ }
64
+ ```
65
+
66
+ ---
67
+
68
+ ## React — Full API
69
+
70
+ ### `<ChatWidget />` Props
71
+
72
+ | Prop | Type | Default | Required | Description |
73
+ | --------------- | ------------------------------------------------- | ---------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
74
+ | `account` | `Account` | `undefined` | No | Connected wallet. Pass `undefined` when no wallet is connected. |
75
+ | `onTransaction` | `(tx: Transaction) => Promise<TransactionResult>` | `undefined` | No\* | Called when the chatbot needs to sign/send a transaction. |
76
+ | `position` | `'bottom-right'` \| `'bottom-left'` | `'bottom-right'` | No | Corner where the floating button appears. |
77
+ | `theme` | `ChatWidgetTheme` | `{}` | No | Visual customization. |
78
+ | `defaultOpen` | `boolean` | `false` | No | Open the chat modal on first render. |
79
+ | `onOpen` | `() => void` | — | No | Callback fired when the modal opens. |
80
+ | `onClose` | `() => void` | — | No | Callback fired when the modal closes. |
81
+ | `language` | `'en'` \| `'ja'` \| `'cn'` | `'en'` | No | UI language (English, Japanese, Chinese). |
82
+ | `rpcUrls` | `Record<number, string>` | — | No | Per-chain RPC URL overrides (`chainId → URL`). Takes priority over built-in defaults for balance queries and gas estimation. |
83
+
84
+ > \* `onTransaction` is required to execute on-chain actions (swap, send, approve, etc.). Without it, the AI can still answer questions and display information.
85
+
86
+ ### Basic example
87
+
88
+ ```tsx
89
+ import { ChatWidget } from 'keyring-chatbot-agent-sdk-test';
90
+
91
+ function App() {
92
+ return (
93
+ <ChatWidget
94
+ account={{ address: userAddress, chainId: 10 }}
95
+ onTransaction={handleTransaction}
96
+ language="en"
97
+ position="bottom-right"
98
+ />
99
+ );
100
+ }
101
+ ```
102
+
103
+ ### Full example with all props
104
+
105
+ ```tsx
106
+ import { ChatWidget } from 'keyring-chatbot-agent-sdk-test';
107
+ import type {
108
+ Transaction,
109
+ TransactionResult,
110
+ } from 'keyring-chatbot-agent-sdk-test';
111
+
112
+ function App() {
113
+ const handleTransaction = async (
114
+ tx: Transaction
115
+ ): Promise<TransactionResult> => {
116
+ try {
117
+ const hash = await walletClient.sendTransaction(tx);
118
+ return { status: 'success', transactionHash: hash };
119
+ } catch (err: unknown) {
120
+ return { status: 'fail', error: (err as Error).message };
121
+ }
122
+ };
123
+
124
+ return (
125
+ <ChatWidget
126
+ account={{ address: userAddress, chainId: 10 }}
127
+ onTransaction={handleTransaction}
128
+ position="bottom-right"
129
+ language="en"
130
+ defaultOpen={false}
131
+ theme={{
132
+ primaryColor: '#5B7FFF',
133
+ buttonSize: 60,
134
+ zIndex: 9999,
135
+ }}
136
+ rpcUrls={{
137
+ 1: 'https://mainnet.infura.io/v3/YOUR_KEY',
138
+ 10: 'https://optimism-mainnet.infura.io/v3/YOUR_KEY',
139
+ 56: 'https://bsc-dataseed.binance.org',
140
+ 137: 'https://polygon-rpc.com',
141
+ 8453: 'https://mainnet.base.org',
142
+ 42161: 'https://arb1.arbitrum.io/rpc',
143
+ 43114: 'https://api.avax.network/ext/bc/C/rpc',
144
+ 59144: 'https://rpc.linea.build',
145
+ }}
146
+ onOpen={() => console.log('Chat opened')}
147
+ onClose={() => console.log('Chat closed')}
148
+ />
149
+ );
150
+ }
151
+ ```
152
+
153
+ ### Usage with wagmi v2
154
+
155
+ ```tsx
156
+ import { useSendTransaction, useAccount } from 'wagmi';
157
+ import { ChatWidget } from 'keyring-chatbot-agent-sdk-test';
158
+ import type {
159
+ Transaction,
160
+ TransactionResult,
161
+ } from 'keyring-chatbot-agent-sdk-test';
162
+
163
+ function App() {
164
+ const { address, chainId } = useAccount();
165
+ const { sendTransactionAsync } = useSendTransaction();
166
+
167
+ const handleTransaction = async (
168
+ tx: Transaction
169
+ ): Promise<TransactionResult> => {
170
+ try {
171
+ const hash = await sendTransactionAsync({
172
+ to: tx.to as `0x${string}`,
173
+ data: tx.data as `0x${string}`,
174
+ value: BigInt(tx.value || '0'),
175
+ });
176
+ return { status: 'success', transactionHash: hash };
177
+ } catch (err: unknown) {
178
+ return { status: 'fail', error: (err as Error).message };
179
+ }
180
+ };
181
+
182
+ return (
183
+ <ChatWidget
184
+ account={address ? { address, chainId: chainId ?? 1 } : undefined}
185
+ onTransaction={handleTransaction}
186
+ />
187
+ );
188
+ }
189
+ ```
190
+
191
+ ---
192
+
193
+ ## HTML — Web Component
194
+
195
+ The SDK ships a **Web Component** build (`chat-widget-wc`) that can be dropped into any HTML page without a React build pipeline.
196
+
197
+ The web component supports visual configuration via HTML attributes (`position`, `primary-color`, `button-size`, `z-index`, `default-open`).
198
+
199
+ ### Via CDN — UMD script tag
200
+
201
+ ```html
202
+ <!DOCTYPE html>
203
+ <html lang="en">
204
+ <head>
205
+ <meta charset="UTF-8" />
206
+ <title>My dApp</title>
207
+ </head>
208
+ <body>
209
+ <h1>My Web3 App</h1>
210
+
211
+ <!-- 1. Load the bundle -->
212
+ <script src="https://unpkg.com/keyring-chatbot-agent-sdk-test/dist/chat-widget-wc.umd.js"></script>
213
+
214
+ <!-- 2. Place the custom element -->
215
+ <chat-widget
216
+ position="bottom-right"
217
+ primary-color="#5B7FFF"
218
+ button-size="60"
219
+ z-index="9999"
220
+ ></chat-widget>
221
+ </body>
222
+ </html>
223
+ ```
224
+
225
+ ### Via CDN — ES module
226
+
227
+ ```html
228
+ <!DOCTYPE html>
229
+ <html lang="en">
230
+ <head>
231
+ <meta charset="UTF-8" />
232
+ <title>My dApp</title>
233
+ </head>
234
+ <body>
235
+ <h1>My Web3 App</h1>
236
+
237
+ <chat-widget position="bottom-right" primary-color="#5B7FFF"></chat-widget>
238
+
239
+ <script type="module">
240
+ import 'https://unpkg.com/keyring-chatbot-agent-sdk-test/dist/chat-widget-wc.es.js';
241
+ </script>
242
+ </body>
243
+ </html>
244
+ ```
245
+
246
+ ### Via local `node_modules`
247
+
248
+ ```html
249
+ <script src="./node_modules/keyring-chatbot-agent-sdk-test/dist/chat-widget-wc.umd.js"></script>
250
+
251
+ <chat-widget position="bottom-right"></chat-widget>
252
+ ```
253
+
254
+ ### Via local `dist/` (after building from source)
255
+
256
+ ```html
257
+ <script src="./dist/chat-widget-wc.umd.js"></script>
258
+
259
+ <chat-widget
260
+ position="bottom-right"
261
+ primary-color="#5B7FFF"
262
+ button-size="60"
263
+ ></chat-widget>
264
+ ```
265
+
266
+ ### Web Component — HTML Attributes
267
+
268
+ | Attribute | Type | Default | Description |
269
+ | --------------- | --------- | ---------------- | --------------------------------------------- |
270
+ | `position` | `string` | `'bottom-right'` | `'bottom-right'` or `'bottom-left'` |
271
+ | `primary-color` | `string` | `'#007bff'` | Widget accent color (hex or CSS color) |
272
+ | `button-size` | `number` | `60` | Floating button diameter in pixels |
273
+ | `z-index` | `number` | `9999` | CSS `z-index` of the widget |
274
+ | `default-open` | `boolean` | `false` | Set to `"true"` to open the chat on page load |
275
+
276
+ ---
277
+
278
+ ## Type Definitions
279
+
280
+ ```typescript
281
+ interface Account {
282
+ address: string; // Wallet address (0x…)
283
+ chainId: number | string; // EIP-155 chain ID
284
+ }
285
+
286
+ interface Transaction {
287
+ from: string;
288
+ to: string;
289
+ data: string; // ABI-encoded calldata (hex)
290
+ value: string; // Native amount in wei (decimal or hex string)
291
+ gasLimit?: string;
292
+ gasPrice?: string;
293
+ maxFeePerGas?: string;
294
+ maxPriorityFeePerGas?: string;
295
+ nonce?: number;
296
+ chainId?: number | string;
297
+ }
298
+
299
+ interface TransactionResult {
300
+ status: 'success' | 'fail';
301
+ transactionHash?: string; // Present on success
302
+ error?: string; // Present on failure
303
+ }
304
+
305
+ interface ChatWidgetTheme {
306
+ primaryColor?: string; // Hex / CSS color string
307
+ buttonSize?: number; // Floating button size in px
308
+ zIndex?: number; // CSS z-index
309
+ }
310
+
311
+ type Language = 'en' | 'ja' | 'cn';
312
+ ```
313
+
314
+ ---
315
+
316
+ ## Supported Chains
317
+
318
+ | Chain | Chain ID |
319
+ | --------- | -------- |
320
+ | Ethereum | 1 |
321
+ | Optimism | 10 |
322
+ | BNB Chain | 56 |
323
+ | Polygon | 137 |
324
+ | Base | 8453 |
325
+ | Arbitrum | 42161 |
326
+ | Avalanche | 43114 |
327
+ | Linea | 59144 |
328
+
329
+ ---
330
+
331
+ ## AI Capabilities
332
+
333
+ The embedded AI (GPT-4.1-mini via Moralis Cortex) understands natural language and routes to on-chain actions automatically:
334
+
335
+ | User intent | Action |
336
+ | -------------------------------- | ------------------------------------- |
337
+ | "Swap 1 ETH to USDC" | Token swap via deBridge |
338
+ | "Swap max USDT to ETH" | Swap with full-balance resolution |
339
+ | "Buy WBTC" | Buy flow with trending token lookup |
340
+ | "Send 10 USDC to 0x…" | ERC-20 transfer |
341
+ | "Send 0.05 ETH to 0x…" | Native token transfer |
342
+ | "Wrap 1 ETH" / "Unwrap 0.5 WETH" | Wrap / unwrap native token |
343
+ | "Show my NFTs" | Display NFT gallery |
344
+ | "Send my Bored Ape #1234 to 0x…" | ERC-721 / ERC-1155 transfer |
345
+ | "What is the gas fee?" | General blockchain Q&A |
346
+ | "What tokens are trending?" | Trending tokens list with buy buttons |
347
+
348
+ ### Smart Swap Flow
349
+
350
+ The AI handles missing parameters gracefully:
351
+
352
+ | Parameters available | Behaviour |
353
+ | ----------------------------- | ---------------------------------------------- |
354
+ | token_in + token_out + amount | Full auto-swap: estimation → confirm → execute |
355
+ | token_in + token_out only | Shows 25 / 50 / 75 / 100 % amount selector |
356
+ | token_out only | Shows wallet balances to choose source token |
357
+ | token_in only | Shows trending tokens to choose destination |
358
+ | Neither | Asks user to specify both tokens |
359
+ | ERC-20 needs approval | Prompts Approve step before swap |
360
+ | Insufficient balance / fee | Warning message, no confirmation prompt |
361
+
362
+ ---
363
+
364
+ ## Build Outputs
365
+
366
+ | File | Format | Use case |
367
+ | ---------------------------- | ------ | ------------------------------------------ |
368
+ | `dist/chat-widget.es.js` | ESM | React / Vite / bundler |
369
+ | `dist/chat-widget.umd.js` | UMD | CommonJS / `require` |
370
+ | `dist/chat-widget-wc.es.js` | ESM | Web Component via `<script type="module">` |
371
+ | `dist/chat-widget-wc.umd.js` | UMD | Web Component via `<script src>` / CDN |
372
+ | `dist/lib.d.ts` | Types | TypeScript declarations |
373
+
374
+ ### Package exports map
375
+
376
+ ```json
377
+ {
378
+ ".": {
379
+ "import": "dist/chat-widget.es.js",
380
+ "require": "dist/chat-widget.umd.js",
381
+ "types": "dist/lib.d.ts"
382
+ },
383
+ "./web-component": {
384
+ "import": "dist/chat-widget-wc.es.js",
385
+ "require": "dist/chat-widget-wc.umd.js"
386
+ }
387
+ }
388
+ ```
389
+
390
+ ---
391
+
392
+ ## Development
393
+
394
+ ```bash
395
+ # Clone
396
+ git clone git@bitbucket.org:bacoorteam/keyring-chatbot-agent-sdk.git
397
+ cd keyring-chatbot-agent-sdk
398
+
399
+ # Install dependencies
400
+ yarn install
401
+
402
+ # Start demo dev server
403
+ yarn dev
404
+
405
+ # Build both React and Web Component bundles
406
+ yarn build
407
+
408
+ # Build React bundle only
409
+ yarn build:react
410
+
411
+ # Build Web Component bundle only
412
+ yarn build:wc
413
+
414
+ # Watch mode (auto-rebuild React bundle on change)
415
+ yarn watch
416
+
417
+ # Lint
418
+ yarn lint
419
+ yarn lint:fix
420
+
421
+ # Format
422
+ yarn format
423
+ ```
424
+
425
+ ---
426
+
427
+ ## Project Structure
428
+
429
+ ```
430
+ src/
431
+ ├── components/
432
+ │ ├── ActionForm.tsx # Inline transaction forms (send, approve, …)
433
+ │ ├── ChatButton.tsx # Floating action button
434
+ │ ├── ChatModal.tsx # Chat UI + all AI action logic
435
+ │ ├── ChatWidget.tsx # Root component — public API entry point
436
+ │ ├── MessageContent.tsx # Markdown message renderer
437
+ │ ├── ScrollToBottomButton.tsx
438
+ │ └── WalletUserInfo.tsx
439
+ ├── constants/
440
+ │ ├── agentActions.ts # All AgentActionType definitions + form schemas
441
+ │ ├── chains.ts # Supported chain configs (DATA_CHAIN)
442
+ │ ├── storage.ts # LocalStorage key constants
443
+ │ └── systemPrompt.ts # AI system prompt builder
444
+ ├── contexts/
445
+ │ ├── ConfigContext.tsx # RPC URLs + theme config context
446
+ │ ├── ConnectContext.tsx # Wallet account context
447
+ │ └── LanguageContext.tsx # i18n language context
448
+ ├── hooks/
449
+ │ └── useChatMessages.ts # Chat message state management
450
+ ├── services/
451
+ │ ├── BaseApi.ts # Base HTTP client
452
+ │ ├── deBridge.ts # deBridge swap quote + transaction API
453
+ │ ├── moralis.ts # Moralis AI chat, wallet balances, NFTs, metadata
454
+ │ ├── token.ts # Token info + trending data
455
+ │ └── web3.ts # Transaction builder, fee estimation, allowance check
456
+ ├── types/
457
+ │ └── index.ts # All public TypeScript interfaces
458
+ ├── lib.tsx # React library entry point
459
+ └── web-component.tsx # Web Component entry point
460
+ ```
461
+
462
+ ---
463
+
464
+ ## Publishing
465
+
466
+ ```bash
467
+ # 1. Bump version in package.json
468
+ # 2. Build
469
+ yarn build
470
+
471
+ # 3. Login to npm
472
+ npm login
473
+
474
+ # 4. Publish
475
+ npm publish
476
+ ```
477
+
478
+ ---
479
+
480
+ ## License
481
+
482
+ ISC
483
+
484
+ ## Repository
485
+
486
+ [Bitbucket — keyring-chatbot-agent-sdk](https://bitbucket.org/bacoorteam/keyring-chatbot-agent-sdk)
487
+
488
+ ## Issues
489
+
490
+ [Issue tracker](https://bitbucket.org/bacoorteam/keyring-chatbot-agent-sdk/issues)