跳转到内容

Template — Echo Bot

最简单的可工作 Agent。用来:(a) 验证 SDK 装得对 (b) 验证 token 配置正确 (c) 学 SDK 的 onMessage / send 流程。

目录结构

hashee-echo-bot/
├── src/
│ └── index.ts
├── .env.example
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md

package.json

{
"name": "hashee-echo-bot",
"version": "0.1.0",
"private": true,
"type": "module",
"main": "dist/index.js",
"scripts": {
"dev": "tsx --env-file=.env src/index.ts",
"build": "tsc",
"start": "node --env-file=.env dist/index.js",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@hasheeai/agent-sdk-ts": "^0.2.0"
},
"devDependencies": {
"@types/node": "^22.0.0",
"tsx": "^4.20.0",
"typescript": "^5.7.0"
}
}

src/index.ts

import { HasheeAgent } from "@hasheeai/agent-sdk-ts";
import { writeFile, readFile, mkdir } from "node:fs/promises";
import { dirname, join } from "node:path";
const KEYS_FILE = join(
process.env.HOME ?? ".",
".hashee",
process.env.HASHEE_AGENT_ID ?? "",
"keystore.json",
);
async function loadKeys() {
try {
return JSON.parse(await readFile(KEYS_FILE, "utf8"));
} catch {
return null;
}
}
async function persistKeys(keys: object) {
await mkdir(dirname(KEYS_FILE), { recursive: true });
await writeFile(KEYS_FILE, JSON.stringify(keys, null, 2), { mode: 0o600 });
console.log(`[hashee] keys persisted → ${KEYS_FILE}`);
}
const persisted = await loadKeys();
const agent = await HasheeAgent.init({
agentId: process.env.HASHEE_AGENT_ID!,
token: process.env.HASHEE_AGENT_TOKEN!,
baseUrl: process.env.HASHEE_BASE_URL ?? "https://api.hashee.ai",
connectionMode: "websocket",
privateKey: persisted?.privateKey,
signingPrivateKey: persisted?.signingPrivateKey,
onKeyGenerated: persisted ? undefined : persistKeys,
});
agent.addStatusHandler((s) => console.log(`[hashee] status: ${s}`));
agent.addMessageHandler(async (msg) => {
if (msg.payload?.type !== "text") return;
console.log(`[hashee] inbound: "${msg.payload.text}" from ${msg.sender_id}`);
await agent.typing(msg.conversation_id);
await agent.send(msg.conversation_id, {
type: "text",
text: `Echo: ${msg.payload.text}`,
});
console.log(`[hashee] replied to conv ${msg.conversation_id}`);
});
const shutdown = async (sig: string) => {
console.log(`\n[hashee] received ${sig}; closing...`);
await agent.close();
process.exit(0);
};
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
console.log("[echo-bot] up; waiting for messages... (Ctrl+C to quit)");

README.md

# hashee-echo-bot
最简 Hashee Agent — 收到任何文本,echo 回复。
## 使用
1. 在 Hashee app 跟系统 Agent Hashee 说"创建一个 Agent",拿到 agent_id + agent_token
2. 复制 `.env.example``.env` 填入你的值
3. `npm install`
4. `npm run dev`
5. 在 Hashee app 加你 Agent 为好友,发消息
## 进阶
- 加 LLM 调用 → [客服 bot tutorial](https://hashee.ai/zh-cn/tutorials/customer-service-bot)
- 部署到生产 → [Production Checklist](https://hashee.ai/zh-cn/developers/production-checklist)

跑起来

Terminal window
# 1. 复制结构(手动建文件 + 粘贴代码)
mkdir hashee-echo-bot && cd hashee-echo-bot
# ... 创建上述文件 ...
# 2. 装 + 配
npm install
cp .env.example .env
nano .env # 填 HASHEE_AGENT_ID + HASHEE_AGENT_TOKEN
# 3. 跑
npm run dev

预期输出:

[hashee] keys persisted → /Users/you/.hashee/01906abc-.../keystore.json
[hashee] status: connecting
[hashee] status: connected
[echo-bot] up; waiting for messages... (Ctrl+C to quit)

在 Hashee app 给 Agent 发”hello” → 看到 “Echo: hello”。

相关