@seasonkoh/webaz 0.1.1 → 0.1.3
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/demo-agent.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* WAZ Agent 交互演示
|
|
3
3
|
* 模拟两个 Agent(卖家Agent + 买家Agent)通过 MCP 工具完成一笔真实交易
|
|
4
4
|
* 这就是真实 Agent 调用时会发生的事情
|
|
5
5
|
*/
|
|
@@ -25,11 +25,11 @@ async function toolCall(name, result) {
|
|
|
25
25
|
}
|
|
26
26
|
async function main() {
|
|
27
27
|
console.clear();
|
|
28
|
-
console.log('
|
|
28
|
+
console.log('🌐 WebAZ Protocol — Agent 交互演示');
|
|
29
29
|
console.log(' 两个 Agent 自动完成一笔真实去中心化交易');
|
|
30
30
|
line();
|
|
31
31
|
// ── STEP 1: 卖家 Agent 注册 ───────────────────────────────────
|
|
32
|
-
await agentSay('卖家', '🏪', '我要在
|
|
32
|
+
await agentSay('卖家', '🏪', '我要在 WAZ 协议上开店,先注册一个卖家账号');
|
|
33
33
|
const sellerId = generateId('usr');
|
|
34
34
|
const sellerKey = generateId('key');
|
|
35
35
|
db.prepare('INSERT INTO users (id, name, role, api_key) VALUES (?, ?, ?, ?)').run(sellerId, '竹韵手工坊', 'seller', sellerKey);
|
|
@@ -37,7 +37,7 @@ async function main() {
|
|
|
37
37
|
await toolCall('dcp_register', { name: '竹韵手工坊', role: 'seller' });
|
|
38
38
|
console.log(` ✅ 注册成功!api_key 已保存`);
|
|
39
39
|
// ── STEP 2: 卖家 Agent 上架商品 ────────────────────────────────
|
|
40
|
-
await agentSay('卖家', '🏪', '把我的拳头产品上架,定价 168
|
|
40
|
+
await agentSay('卖家', '🏪', '把我的拳头产品上架,定价 168 WAZ,自动质押 15% 保证金');
|
|
41
41
|
const productId = generateId('prd');
|
|
42
42
|
const price = 168;
|
|
43
43
|
const stake = Math.round(price * 0.15 * 100) / 100;
|
|
@@ -45,7 +45,7 @@ async function main() {
|
|
|
45
45
|
.run(productId, sellerId, '竹编茶叶罐(250g装)', '云南手工竹编,密封保鲜,适合储存普洱、岩茶', price, 10, '茶具', stake);
|
|
46
46
|
db.prepare('UPDATE wallets SET balance = balance - ?, staked = staked + ? WHERE user_id = ?').run(stake, stake, sellerId);
|
|
47
47
|
await toolCall('dcp_list_product', { title: '竹编茶叶罐', price: 168, stock: 10 });
|
|
48
|
-
console.log(` ✅ 商品已上架!质押 ${stake}
|
|
48
|
+
console.log(` ✅ 商品已上架!质押 ${stake} WAZ,买家现在可以搜索到`);
|
|
49
49
|
line();
|
|
50
50
|
// ── STEP 3: 买家 Agent 注册 ────────────────────────────────────
|
|
51
51
|
await agentSay('买家', '🛒', '帮我注册买家身份,我想买点好茶具');
|
|
@@ -54,7 +54,7 @@ async function main() {
|
|
|
54
54
|
db.prepare('INSERT INTO users (id, name, role, api_key) VALUES (?, ?, ?, ?)').run(buyerId, '陈先生', 'buyer', buyerKey);
|
|
55
55
|
db.prepare('INSERT INTO wallets (user_id, balance) VALUES (?, 1000)').run(buyerId);
|
|
56
56
|
await toolCall('dcp_register', { name: '陈先生', role: 'buyer' });
|
|
57
|
-
console.log(` ✅ 注册成功!初始余额 1000
|
|
57
|
+
console.log(` ✅ 注册成功!初始余额 1000 WAZ`);
|
|
58
58
|
// ── STEP 4: 买家 Agent 搜索 ────────────────────────────────────
|
|
59
59
|
await agentSay('买家', '🛒', '帮我搜索一下茶具,预算 200 以内');
|
|
60
60
|
const results = db.prepare(`
|
|
@@ -64,7 +64,7 @@ async function main() {
|
|
|
64
64
|
`).all();
|
|
65
65
|
await toolCall('dcp_search', { query: '茶具', max_price: 200 });
|
|
66
66
|
console.log(` ← 找到 ${results.length} 件商品:`);
|
|
67
|
-
results.forEach(p => console.log(` · ${p.title} ¥${p.price}
|
|
67
|
+
results.forEach(p => console.log(` · ${p.title} ¥${p.price} WAZ 卖家:${p.seller_name}`));
|
|
68
68
|
// ── STEP 5: 买家 Agent 下单 ────────────────────────────────────
|
|
69
69
|
await agentSay('买家', '🛒', `好!买这个竹编茶叶罐,地址是上海市静安区南京西路×号`);
|
|
70
70
|
const orderId = generateId('ord');
|
|
@@ -80,7 +80,7 @@ async function main() {
|
|
|
80
80
|
db.prepare('UPDATE products SET stock = stock - 1 WHERE id = ?').run(productId);
|
|
81
81
|
transition(db, orderId, 'paid', buyerId, [], '买家付款,资金托管');
|
|
82
82
|
await toolCall('dcp_place_order', { product_id: productId, shipping_address: '上海市...' });
|
|
83
|
-
console.log(` ✅ 订单创建!${price}
|
|
83
|
+
console.log(` ✅ 订单创建!${price} WAZ 已托管`);
|
|
84
84
|
console.log(` ℹ️ 卖家需在 24h 内接单,否则自动退款`);
|
|
85
85
|
line();
|
|
86
86
|
// ── STEP 6: 卖家 Agent 收到通知,接单 ─────────────────────────
|
|
@@ -139,14 +139,14 @@ async function main() {
|
|
|
139
139
|
const statusInfo = getOrderStatus(db, orderId);
|
|
140
140
|
console.log(`\n 📋 订单 ${orderId}`);
|
|
141
141
|
console.log(` 状态:${statusInfo.order.status}(已完成)\n`);
|
|
142
|
-
console.log(` 💰 资金分配(总额 ${price}
|
|
142
|
+
console.log(` 💰 资金分配(总额 ${price} WAZ):`);
|
|
143
143
|
const sellerW = db.prepare('SELECT * FROM wallets WHERE user_id = ?').get(sellerId);
|
|
144
144
|
const logW = db.prepare('SELECT * FROM wallets WHERE user_id = ?').get(logisticsId);
|
|
145
145
|
const buyerW = db.prepare('SELECT * FROM wallets WHERE user_id = ?').get(buyerId);
|
|
146
|
-
console.log(` 卖家(竹韵手工坊):+${sellerAmount}
|
|
147
|
-
console.log(` 物流(顺丰小哥): +${logisticsFee}
|
|
148
|
-
console.log(` 协议费: -${protocolFee}
|
|
149
|
-
console.log(` 买家(陈先生): -${price}
|
|
146
|
+
console.log(` 卖家(竹韵手工坊):+${sellerAmount} WAZ (${((sellerAmount / price) * 100).toFixed(0)}%) 总余额:${sellerW.balance.toFixed(2)}`);
|
|
147
|
+
console.log(` 物流(顺丰小哥): +${logisticsFee} WAZ (5%) 总余额:${logW.balance.toFixed(2)}`);
|
|
148
|
+
console.log(` 协议费: -${protocolFee} WAZ (2%)`);
|
|
149
|
+
console.log(` 买家(陈先生): -${price} WAZ 总余额:${buyerW.balance.toFixed(2)}`);
|
|
150
150
|
console.log(`\n 📜 完整状态历史(每步都有链上记录):`);
|
|
151
151
|
for (const h of statusInfo.history) {
|
|
152
152
|
const evtCount = JSON.parse(h.evidence_ids || '[]').length;
|
|
@@ -154,7 +154,7 @@ async function main() {
|
|
|
154
154
|
console.log(` ${String(h.from_status).padEnd(12)} → ${String(h.to_status).padEnd(12)} ${String(h.actor_name)}${evtTag}`);
|
|
155
155
|
}
|
|
156
156
|
line();
|
|
157
|
-
console.log('\n🎉 这就是
|
|
157
|
+
console.log('\n🎉 这就是 WAZ 协议的第一笔真实交易!');
|
|
158
158
|
console.log();
|
|
159
159
|
console.log(' 用户做了什么:告诉 Agent「买这个」「确认收货」');
|
|
160
160
|
console.log(' Agent 做了什么:处理所有协议交互、证据上传、状态流转');
|
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import Database from 'better-sqlite3';
|
|
6
6
|
import path from 'path';
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
const
|
|
7
|
+
import os from 'os';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
const DATA_DIR = path.join(os.homedir(), '.webaz');
|
|
10
|
+
if (!fs.existsSync(DATA_DIR))
|
|
11
|
+
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
12
|
+
const DB_PATH = path.join(DATA_DIR, 'webaz.db');
|
|
10
13
|
export function initDatabase() {
|
|
11
14
|
const db = new Database(DB_PATH);
|
|
12
15
|
db.pragma('journal_mode = WAL'); // 更好的并发性能
|
package/package.json
CHANGED