|
| 1 | +# @stackflow/agent (scaffold) |
| 2 | + |
| 3 | +Simple Stackflow agent runtime for AI agents that do **not** run `stacks-node` |
| 4 | +or `stackflow-node`. |
| 5 | + |
| 6 | +Current model: |
| 7 | + |
| 8 | +1. SQLite local state for tracked pipes and latest signatures |
| 9 | +2. signer + contract call adapter backed by AIBTC MCP wallet tools |
| 10 | +3. hourly chain watcher to detect force-close/force-cancel and dispute |
| 11 | + |
| 12 | +## Runtime Components |
| 13 | + |
| 14 | +1. `AgentStateStore`: SQLite persistence |
| 15 | +2. `StackflowAgentService`: pipe tracking + signature state + dispute logic |
| 16 | +3. `AibtcWalletAdapter`: wrapper around AIBTC MCP tools |
| 17 | +4. `HourlyClosureWatcher`: periodic closure scan (default every hour) |
| 18 | + |
| 19 | +## Example Wiring |
| 20 | + |
| 21 | +```js |
| 22 | +import { |
| 23 | + AgentStateStore, |
| 24 | + AibtcPipeStateSource, |
| 25 | + AibtcWalletAdapter, |
| 26 | + HourlyClosureWatcher, |
| 27 | + StackflowAgentService, |
| 28 | +} from "./src/index.js"; |
| 29 | + |
| 30 | +const stateStore = new AgentStateStore({ |
| 31 | + dbFile: "./tmp/stackflow-agent.db", |
| 32 | +}); |
| 33 | + |
| 34 | +// You provide invokeTool using your MCP client runtime. |
| 35 | +const wallet = new AibtcWalletAdapter({ |
| 36 | + invokeTool: async (toolName, args) => { |
| 37 | + // Example: |
| 38 | + // return mcpClient.callTool({ name: toolName, arguments: args }); |
| 39 | + throw new Error("implement invokeTool"); |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +const agent = new StackflowAgentService({ |
| 44 | + stateStore, |
| 45 | + signer: wallet, |
| 46 | + network: "devnet", |
| 47 | + disputeOnlyBeneficial: true, |
| 48 | +}); |
| 49 | + |
| 50 | +const pipeSource = new AibtcPipeStateSource({ |
| 51 | + walletAdapter: wallet, |
| 52 | + contractId: "ST...stackflow", |
| 53 | + network: "devnet", |
| 54 | +}); |
| 55 | + |
| 56 | +const watcher = new HourlyClosureWatcher({ |
| 57 | + agentService: agent, |
| 58 | + // Simpler mode: poll each tracked pipe via read-only `get-pipe`. |
| 59 | + getPipeState: (args) => pipeSource.getPipeState(args), |
| 60 | + intervalMs: 60 * 60 * 1000, // 1 hour |
| 61 | +}); |
| 62 | + |
| 63 | +watcher.start(); |
| 64 | +``` |
| 65 | + |
| 66 | +## Core Operations |
| 67 | + |
| 68 | +1. `trackPipe(...)` |
| 69 | +2. `recordSignedState(...)` |
| 70 | +3. `openPipe(...)` (via wallet `call_contract`) |
| 71 | +4. `buildOutgoingTransfer(...)` |
| 72 | +5. `validateIncomingTransfer(...)` |
| 73 | +6. `acceptIncomingTransfer(...)` (validate + sign + persist) |
| 74 | +7. `evaluateClosureForDispute(...)` |
| 75 | +8. `disputeClosure(...)` |
| 76 | +9. `watcher.runOnce()` or `watcher.start()` for hourly checks |
| 77 | + |
| 78 | +## Notes |
| 79 | + |
| 80 | +1. This scaffold intentionally avoids observer endpoints and local chain node. |
| 81 | +2. The watcher interval defaults to one hour; dispute window is still 144 BTC blocks. |
| 82 | +3. `HourlyClosureWatcher` supports two sources: |
| 83 | + - `getPipeState` (recommended): per-pipe read-only polling (`get-pipe`) |
| 84 | + - `listClosureEvents`: event scan mode |
| 85 | +4. For production hardening, add alerting, signer balance checks, and idempotency audit logs. |
0 commit comments