@kaleidorg/mind 0.0.1 → 0.2.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.
- package/dist/capabilities.d.ts +34 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +34 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/context/budget.d.ts +29 -0
- package/dist/context/budget.d.ts.map +1 -0
- package/dist/context/budget.js +36 -0
- package/dist/context/budget.js.map +1 -0
- package/dist/context/builder.d.ts +39 -0
- package/dist/context/builder.d.ts.map +1 -0
- package/dist/context/builder.js +77 -0
- package/dist/context/builder.js.map +1 -0
- package/dist/engine.d.ts +9 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +18 -2
- package/dist/engine.js.map +1 -1
- package/dist/fastpath/fastpath.d.ts +38 -0
- package/dist/fastpath/fastpath.d.ts.map +1 -0
- package/dist/fastpath/fastpath.js +52 -0
- package/dist/fastpath/fastpath.js.map +1 -0
- package/dist/funnel.d.ts +111 -0
- package/dist/funnel.d.ts.map +1 -0
- package/dist/funnel.js +175 -0
- package/dist/funnel.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -1
- package/dist/knowledge/bitcoin-copilot.d.ts +11 -0
- package/dist/knowledge/bitcoin-copilot.d.ts.map +1 -0
- package/dist/knowledge/bitcoin-copilot.js +155 -0
- package/dist/knowledge/bitcoin-copilot.js.map +1 -0
- package/dist/knowledge/merchants.d.ts +24 -0
- package/dist/knowledge/merchants.d.ts.map +1 -0
- package/dist/knowledge/merchants.js +34 -0
- package/dist/knowledge/merchants.js.map +1 -0
- package/dist/knowledge/wallet.d.ts +34 -0
- package/dist/knowledge/wallet.d.ts.map +1 -0
- package/dist/knowledge/wallet.js +63 -0
- package/dist/knowledge/wallet.js.map +1 -0
- package/dist/memory/store.d.ts +34 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +103 -0
- package/dist/memory/store.js.map +1 -0
- package/dist/memory/tool.d.ts +9 -0
- package/dist/memory/tool.d.ts.map +1 -0
- package/dist/memory/tool.js +70 -0
- package/dist/memory/tool.js.map +1 -0
- package/dist/memory/types.d.ts +56 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +14 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/rag/retriever.d.ts +30 -0
- package/dist/rag/retriever.d.ts.map +1 -0
- package/dist/rag/retriever.js +72 -0
- package/dist/rag/retriever.js.map +1 -0
- package/dist/rag/tool.d.ts +15 -0
- package/dist/rag/tool.d.ts.map +1 -0
- package/dist/rag/tool.js +42 -0
- package/dist/rag/tool.js.map +1 -0
- package/dist/rag/types.d.ts +44 -0
- package/dist/rag/types.d.ts.map +1 -0
- package/dist/rag/types.js +11 -0
- package/dist/rag/types.js.map +1 -0
- package/dist/rag/vector-store.d.ts +23 -0
- package/dist/rag/vector-store.d.ts.map +1 -0
- package/dist/rag/vector-store.js +72 -0
- package/dist/rag/vector-store.js.map +1 -0
- package/dist/recipe/asset-send.d.ts +15 -0
- package/dist/recipe/asset-send.d.ts.map +1 -0
- package/dist/recipe/asset-send.js +83 -0
- package/dist/recipe/asset-send.js.map +1 -0
- package/dist/recipe/payments.d.ts +15 -0
- package/dist/recipe/payments.d.ts.map +1 -0
- package/dist/recipe/payments.js +119 -0
- package/dist/recipe/payments.js.map +1 -0
- package/dist/recipe/receive.d.ts +14 -0
- package/dist/recipe/receive.d.ts.map +1 -0
- package/dist/recipe/receive.js +109 -0
- package/dist/recipe/receive.js.map +1 -0
- package/dist/recipe/runner.d.ts +42 -0
- package/dist/recipe/runner.d.ts.map +1 -0
- package/dist/recipe/runner.js +94 -0
- package/dist/recipe/runner.js.map +1 -0
- package/dist/recipe/swap.d.ts +16 -0
- package/dist/recipe/swap.d.ts.map +1 -0
- package/dist/recipe/swap.js +73 -0
- package/dist/recipe/swap.js.map +1 -0
- package/dist/recipe/types.d.ts +71 -0
- package/dist/recipe/types.d.ts.map +1 -0
- package/dist/recipe/types.js +13 -0
- package/dist/recipe/types.js.map +1 -0
- package/dist/skills/bundle.d.ts +30 -0
- package/dist/skills/bundle.d.ts.map +1 -0
- package/dist/skills/bundle.js +24 -0
- package/dist/skills/bundle.js.map +1 -0
- package/dist/skills/loader.d.ts +33 -0
- package/dist/skills/loader.d.ts.map +1 -0
- package/dist/skills/loader.js +59 -0
- package/dist/skills/loader.js.map +1 -0
- package/dist/skills/reference-source.d.ts +18 -0
- package/dist/skills/reference-source.d.ts.map +1 -0
- package/dist/skills/reference-source.js +53 -0
- package/dist/skills/reference-source.js.map +1 -0
- package/dist/skills/registry.d.ts +41 -0
- package/dist/skills/registry.d.ts.map +1 -0
- package/dist/skills/registry.js +167 -0
- package/dist/skills/registry.js.map +1 -0
- package/dist/skills/types.d.ts +53 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +18 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/tools/cli.d.ts +43 -0
- package/dist/tools/cli.d.ts.map +1 -0
- package/dist/tools/cli.js +61 -0
- package/dist/tools/cli.js.map +1 -0
- package/dist/tools/l402.d.ts +47 -0
- package/dist/tools/l402.d.ts.map +1 -0
- package/dist/tools/l402.js +84 -0
- package/dist/tools/l402.js.map +1 -0
- package/dist/tools/mcp.d.ts +3 -2
- package/dist/tools/mcp.d.ts.map +1 -1
- package/dist/tools/mcp.js +3 -2
- package/dist/tools/mcp.js.map +1 -1
- package/dist/wallet/contract.d.ts +57 -0
- package/dist/wallet/contract.d.ts.map +1 -0
- package/dist/wallet/contract.js +113 -0
- package/dist/wallet/contract.js.map +1 -0
- package/package.json +16 -5
- package/scripts/bundle-skills.mjs +84 -0
- package/skills/README.md +74 -0
- package/skills/bitrefill/SKILL.md +66 -0
- package/skills/bitrefill/references/api.md +99 -0
- package/skills/bitrefill/references/browse.md +71 -0
- package/skills/bitrefill/references/capability-matrix.md +115 -0
- package/skills/bitrefill/references/cli-headless-auth.md +133 -0
- package/skills/bitrefill/references/cli.md +237 -0
- package/skills/bitrefill/references/host-openclaw.md +167 -0
- package/skills/bitrefill/references/mcp.md +150 -0
- package/skills/bitrefill/references/safeguards.md +138 -0
- package/skills/bitrefill/references/troubleshooting.md +182 -0
- package/skills/kaleido-trading/SKILL.md +31 -0
- package/skills/kaleido-wallet/SKILL.md +28 -0
- package/src/capabilities.ts +67 -0
- package/src/context/budget.ts +46 -0
- package/src/context/builder.ts +100 -0
- package/src/context/context.test.ts +83 -0
- package/src/engine.test.ts +204 -0
- package/src/engine.ts +27 -2
- package/src/fastpath/fastpath.test.ts +34 -0
- package/src/fastpath/fastpath.ts +70 -0
- package/src/funnel.test.ts +207 -0
- package/src/funnel.ts +260 -0
- package/src/index.ts +102 -0
- package/src/knowledge/bitcoin-copilot.ts +177 -0
- package/src/knowledge/knowledge.test.ts +63 -0
- package/src/knowledge/merchants.ts +49 -0
- package/src/knowledge/wallet.ts +84 -0
- package/src/memory/memory.test.ts +85 -0
- package/src/memory/store.ts +129 -0
- package/src/memory/tool.ts +76 -0
- package/src/memory/types.ts +63 -0
- package/src/rag/rag.test.ts +85 -0
- package/src/rag/retriever.ts +94 -0
- package/src/rag/tool.ts +55 -0
- package/src/rag/types.ts +49 -0
- package/src/rag/vector-store.ts +78 -0
- package/src/recipe/asset-send.ts +79 -0
- package/src/recipe/payments.ts +116 -0
- package/src/recipe/receive.ts +98 -0
- package/src/recipe/recipe.test.ts +193 -0
- package/src/recipe/runner.ts +122 -0
- package/src/recipe/swap.ts +74 -0
- package/src/recipe/types.ts +76 -0
- package/src/skills/bundle.ts +42 -0
- package/src/skills/loader.ts +63 -0
- package/src/skills/reference-source.ts +60 -0
- package/src/skills/registry.ts +183 -0
- package/src/skills/skills.test.ts +191 -0
- package/src/skills/types.ts +55 -0
- package/src/tools/cli.test.ts +53 -0
- package/src/tools/cli.ts +98 -0
- package/src/tools/l402.test.ts +113 -0
- package/src/tools/l402.ts +122 -0
- package/src/tools/mcp.ts +3 -2
- package/src/wallet/contract.test.ts +89 -0
- package/src/wallet/contract.ts +157 -0
- package/dist/providers/qvac.d.ts +0 -89
- package/dist/providers/qvac.d.ts.map +0 -1
- package/dist/providers/qvac.js +0 -150
- package/dist/providers/qvac.js.map +0 -1
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Path: MCP
|
|
2
|
+
|
|
3
|
+
**Preferred purchase channel.** Typed tool calls, OAuth or API key, no shell, works in 10+ hosts.
|
|
4
|
+
|
|
5
|
+
## Two MCP servers
|
|
6
|
+
|
|
7
|
+
### eCommerce MCP — for purchases
|
|
8
|
+
|
|
9
|
+
URL: `https://api.bitrefill.com/mcp` (OAuth) **or** `https://api.bitrefill.com/mcp/YOUR_API_KEY` (header-less, key-in-path).
|
|
10
|
+
|
|
11
|
+
7 tools:
|
|
12
|
+
|
|
13
|
+
- `search-products` — keyword + country + category
|
|
14
|
+
- `product-details` — packages (denominations) + pricing
|
|
15
|
+
- `buy-products` — create invoice
|
|
16
|
+
- `get-invoice-by-id` — poll payment status
|
|
17
|
+
- `get-order-by-id` — get redemption info (codes, eSIM QR)
|
|
18
|
+
- `list-invoices` — invoice history
|
|
19
|
+
- `list-orders` — order history
|
|
20
|
+
|
|
21
|
+
Auth: OAuth (recommended for interactive use) or API key from <https://www.bitrefill.com/account/developers>.
|
|
22
|
+
|
|
23
|
+
### Development MCP — for docs only
|
|
24
|
+
|
|
25
|
+
URL: `https://docs.bitrefill.com/mcp`. Indexes the docs site for code-help. **Not for purchases.** Use only when authoring an integration against the Bitrefill API/CLI.
|
|
26
|
+
|
|
27
|
+
## Per-client setup
|
|
28
|
+
|
|
29
|
+
### Cursor — `.cursor/mcp.json` (project) or `~/.cursor/mcp.json` (global)
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"mcpServers": {
|
|
34
|
+
"bitrefill": {
|
|
35
|
+
"url": "https://api.bitrefill.com/mcp",
|
|
36
|
+
"autoApprove": [
|
|
37
|
+
"search-products", "product-details",
|
|
38
|
+
"list-invoices", "get-invoice-by-id",
|
|
39
|
+
"list-orders", "get-order-by-id"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Keep `buy-products` **out** of `autoApprove`. Cursor caps at 40 active tools across all servers.
|
|
47
|
+
|
|
48
|
+
### Claude Code
|
|
49
|
+
|
|
50
|
+
With the **bitrefill** plugin installed from this repo’s marketplace, the eCommerce MCP is auto-registered; `claude mcp add` below is for manual-only setups.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
claude mcp add bitrefill --url https://api.bitrefill.com/mcp
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or edit `~/.claude.json`. Override output cap with `MAX_MCP_OUTPUT_TOKENS` (default 25 000).
|
|
57
|
+
|
|
58
|
+
### Claude Desktop — `claude_desktop_config.json`
|
|
59
|
+
|
|
60
|
+
macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`. Windows: `%APPDATA%\Claude\claude_desktop_config.json`.
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"mcpServers": {
|
|
65
|
+
"bitrefill": { "url": "https://api.bitrefill.com/mcp" }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Claude.ai (web) — Pro / Max / Team / Enterprise
|
|
71
|
+
|
|
72
|
+
Settings → Connectors → Add custom connector → URL `https://api.bitrefill.com/mcp`. Free tier cannot add custom URLs.
|
|
73
|
+
|
|
74
|
+
### ChatGPT (Plus / Pro / Business / Enterprise / Edu)
|
|
75
|
+
|
|
76
|
+
Settings → Apps & Connectors → Add → URL `https://api.bitrefill.com/mcp`. Toggle **Developer Mode** to allow `buy-products` (write tool). Free tier blocked.
|
|
77
|
+
|
|
78
|
+
### Codex CLI — `~/.codex/config.toml`
|
|
79
|
+
|
|
80
|
+
```toml
|
|
81
|
+
[mcp_servers.bitrefill]
|
|
82
|
+
url = "https://api.bitrefill.com/mcp"
|
|
83
|
+
bearer_token_env_var = "BITREFILL_API_KEY"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
OAuth: `codex mcp login bitrefill`.
|
|
87
|
+
|
|
88
|
+
### Gemini CLI — `~/.gemini/settings.json` (or project `.gemini/settings.json`)
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"mcpServers": {
|
|
93
|
+
"bitrefill": {
|
|
94
|
+
"url": "https://api.bitrefill.com/mcp",
|
|
95
|
+
"headers": { "Authorization": "Bearer ${BITREFILL_API_KEY}" }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
OAuth: `gemini mcp auth bitrefill`.
|
|
102
|
+
|
|
103
|
+
### OpenCode — `opencode.jsonc`
|
|
104
|
+
|
|
105
|
+
```jsonc
|
|
106
|
+
{
|
|
107
|
+
"mcp": {
|
|
108
|
+
"bitrefill": {
|
|
109
|
+
"url": "https://api.bitrefill.com/mcp",
|
|
110
|
+
"headers": { "Authorization": "Bearer ${BITREFILL_API_KEY}" }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### OpenClaw — see [host-openclaw.md](host-openclaw.md)
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
openclaw mcp set bitrefill --url "https://api.bitrefill.com/mcp/$BITREFILL_API_KEY"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Workflow
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
search-products → product-details → buy-products → get-invoice-by-id → get-order-by-id
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
1. **Search**: `search-products(query="Steam", country="US", product_type="giftcard")`. `country` is uppercase Alpha-2.
|
|
129
|
+
2. **Details**: `product-details(product_id="steam-usa", currency="USDC")`. Returns `packages` array with `package_id` in form `{product_id}<&>{value}`.
|
|
130
|
+
3. **Buy**: `buy-products(cart_items=[{product_id, package_id}], payment_method, return_payment_link=true)`. Max 15 items per call.
|
|
131
|
+
- For instant fulfillment: `payment_method: "balance"` + `auto_pay: true`.
|
|
132
|
+
- For agent-driven crypto: `payment_method: "usdc_base"` + `return_payment_link: true` → use `x402_payment_url`.
|
|
133
|
+
4. **Poll**: `get-invoice-by-id(invoice_id)`. Statuses: `unpaid` → `payment_detected` → `payment_confirmed` → `complete`.
|
|
134
|
+
5. **Redeem**: `get-order-by-id(order_id, include_redemption_info=true)` → returns code / link / eSIM install URL.
|
|
135
|
+
|
|
136
|
+
Confirm with user before step 3. Logging per [safeguards.md](safeguards.md).
|
|
137
|
+
|
|
138
|
+
## Caveats
|
|
139
|
+
|
|
140
|
+
- **ChatGPT** custom MCP requires Plus+; write tools require Developer Mode (admin-enabled on workspaces).
|
|
141
|
+
- **Cursor** 40-tool cap across all servers.
|
|
142
|
+
- **Claude.ai** consumer needs Pro+ for custom URLs.
|
|
143
|
+
- **Code-execution sandboxes** (Claude.ai analysis tool, ChatGPT Code Interpreter) have **no network egress** — they can't call MCP servers; install MCP at the chat level instead.
|
|
144
|
+
|
|
145
|
+
## Source of truth
|
|
146
|
+
|
|
147
|
+
- <https://docs.bitrefill.com/docs/ecommerce-mcp>
|
|
148
|
+
- <https://docs.bitrefill.com/docs/development-mcp>
|
|
149
|
+
- <https://docs.bitrefill.com/docs/setup-guides>
|
|
150
|
+
- Per-client setup: <https://docs.bitrefill.com/docs/use-with-cursor>, `/use-with-claude-chat`, `/use-with-claude-code`, `/use-with-chatgpt`
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Spending Safeguards
|
|
2
|
+
|
|
3
|
+
This skill enables **real-money transactions**. Purchases are fulfilled instantly after payment confirms. Digital codes are non-refundable per EU consumer rights once delivered.
|
|
4
|
+
|
|
5
|
+
This page is the **agent-policy layer** — not in upstream Bitrefill or host docs. Read fully before any purchase tool call.
|
|
6
|
+
|
|
7
|
+
## Universal rules
|
|
8
|
+
|
|
9
|
+
- **Default: always confirm before purchasing.** Present product, denomination, price, payment method. Wait for explicit user approval. Autonomous purchasing only when user explicitly opts in for the current session.
|
|
10
|
+
- **Codes are cash-like.** A gift card code or eSIM QR is bearer money. Store securely. Never share publicly.
|
|
11
|
+
- **Prefer in-memory storage.** Don't write codes to plain-text logs, transcripts, or unencrypted files. Programmatically read code → use it → discard.
|
|
12
|
+
- **If user asks for the code**: return it but advise to (a) store securely, (b) not share, (c) redeem ASAP.
|
|
13
|
+
- **Dedicated, low-balance account.** Never give the agent access to high-balance accounts. Pre-fund only what the agent may spend in the current session.
|
|
14
|
+
- **Headless CLI: agent-owned inbox.** Register Bitrefill with an agent inbox ([AgentMail](https://www.agentmail.to/) or equivalent — [cli-headless-auth.md](cli-headless-auth.md)), not the human's primary email. Inbox compromise = account takeover.
|
|
15
|
+
- **Not a wallet.** This skill does not store private keys or manage crypto wallets. Never give the agent seed phrases, hardware-wallet PINs, or signing keys.
|
|
16
|
+
- **Log every purchase.** `invoice_id`, product slug, amount, payment method, timestamp.
|
|
17
|
+
- **Refunds**: digital goods refundable only if they don't work as expected (defective code). EU 14-day change-of-mind does **not** apply.
|
|
18
|
+
- **Browser redemption fallback.** If trying to redeem on a brand site triggers anti-bot, ask the user to complete redemption manually and return the code.
|
|
19
|
+
|
|
20
|
+
Terms: <https://www.bitrefill.com/terms/>.
|
|
21
|
+
|
|
22
|
+
## Per-host hardening
|
|
23
|
+
|
|
24
|
+
### OpenClaw
|
|
25
|
+
|
|
26
|
+
Defaults are permissive (sandboxing off, `security: full`, `ask: off`). Tighten:
|
|
27
|
+
|
|
28
|
+
- **Prefer guest CLI via `exec`** for first purchases (no auth); sign in only when human wants `balance` cap or cashback. See [host-openclaw.md](host-openclaw.md).
|
|
29
|
+
|
|
30
|
+
- `channels.<ch>.allowFrom: ["<your_id>"]` + `dmPolicy: "pairing"` on every channel.
|
|
31
|
+
- `~/.openclaw/exec-approvals.json`: `security: allowlist` + `ask: on-miss`. Allowlist read tools (`bitrefill search-products`, `bitrefill list-*`, `bitrefill get-*`). Force `/approve` for `bitrefill buy-products` and the MCP `buy-products` call.
|
|
32
|
+
- `agents.list[]` Bitrefill persona with `tools.deny: ["gateway"]` so the agent cannot rewrite Gateway config.
|
|
33
|
+
- Disable voice readback (`audio_as_voice` / TTS) for the Bitrefill agent. Codes spoken aloud over voice notes leak.
|
|
34
|
+
- Force text-only delivery — no `MEDIA:<url>` for redemption code output.
|
|
35
|
+
|
|
36
|
+
Full detail in [host-openclaw.md](host-openclaw.md) §8.
|
|
37
|
+
|
|
38
|
+
### Cursor
|
|
39
|
+
|
|
40
|
+
`.cursor/mcp.json` `autoApprove` may include read tools. **Never** include `buy-products`:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"mcpServers": {
|
|
45
|
+
"bitrefill": {
|
|
46
|
+
"url": "https://api.bitrefill.com/mcp",
|
|
47
|
+
"autoApprove": [
|
|
48
|
+
"search-products", "product-details",
|
|
49
|
+
"list-invoices", "get-invoice-by-id",
|
|
50
|
+
"list-orders", "get-order-by-id"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Codex CLI
|
|
58
|
+
|
|
59
|
+
Run with sandbox + approval:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
codex --sandbox workspace-write --ask-for-approval on-request
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Put `BITREFILL_API_KEY` in a profile (`~/.codex/config.toml` `[profiles.bitrefill]`), not in committed config.
|
|
66
|
+
|
|
67
|
+
### Claude Code
|
|
68
|
+
|
|
69
|
+
In `~/.claude/settings.json` (or project `.claude/settings.json`):
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"sandbox": {
|
|
74
|
+
"filesystem": {
|
|
75
|
+
"denyRead": ["~/.ssh", ".env", "*.pem", "**/.bitrefill_token"],
|
|
76
|
+
"denyWrite": ["~/.ssh", ".env"]
|
|
77
|
+
},
|
|
78
|
+
"network": {
|
|
79
|
+
"allow": ["api.bitrefill.com", "registry.npmjs.org"]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Claude Desktop / Claude.ai web
|
|
86
|
+
|
|
87
|
+
Per-tool approval prompts on by default. Keep them on. Don't whitelist `buy-products`.
|
|
88
|
+
|
|
89
|
+
### ChatGPT (web / Desktop / Atlas / Agent)
|
|
90
|
+
|
|
91
|
+
Developer Mode required for write tools. Keep it **off** unless actively purchasing. Confirm in-chat before every `buy-products`.
|
|
92
|
+
|
|
93
|
+
### Gemini CLI
|
|
94
|
+
|
|
95
|
+
Run with `--sandbox` (Seatbelt / Docker / gVisor). Per-shell command confirmation prompts on by default.
|
|
96
|
+
|
|
97
|
+
### OpenCode
|
|
98
|
+
|
|
99
|
+
Set permissions per agent:
|
|
100
|
+
|
|
101
|
+
```jsonc
|
|
102
|
+
{
|
|
103
|
+
"agents": {
|
|
104
|
+
"bitrefill": {
|
|
105
|
+
"permissions": {
|
|
106
|
+
"edit": "ask",
|
|
107
|
+
"bash": { "*": "ask", "bitrefill list-*": "allow", "bitrefill get-*": "allow" },
|
|
108
|
+
"webfetch": "ask"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Payment method risk
|
|
116
|
+
|
|
117
|
+
- `balance` — instant, capped by pre-funded amount. **Lowest blast radius.**
|
|
118
|
+
- `usdc_base` via x402 — autonomous payment from agent-controlled wallet. Bound the wallet balance.
|
|
119
|
+
- `lightning` — fast, low fee. Manual pay or Lightning-capable agent.
|
|
120
|
+
- Other on-chain crypto — slow, requires polling. Higher chance of expired invoices (180 min).
|
|
121
|
+
|
|
122
|
+
Default recommendation: pre-fund `balance` with low cap → use `payment_method: "balance"` + `auto_pay: true`.
|
|
123
|
+
|
|
124
|
+
## What to NEVER do
|
|
125
|
+
|
|
126
|
+
- Pass redemption codes through group chats, public channels, screen-shared sessions, or shared documents.
|
|
127
|
+
- Speak codes aloud via TTS / voice notes.
|
|
128
|
+
- Store codes in version control, even private repos.
|
|
129
|
+
- Give the agent seed phrases or hardware-wallet PINs.
|
|
130
|
+
- Auto-approve `buy-products` in any host's MCP config.
|
|
131
|
+
- Run the Bitrefill skill from an account with stored payment cards or high balances.
|
|
132
|
+
|
|
133
|
+
## Source of truth
|
|
134
|
+
|
|
135
|
+
- Bitrefill ToS: <https://www.bitrefill.com/terms/>
|
|
136
|
+
- Refund policy: <https://docs.bitrefill.com/docs/refunds>
|
|
137
|
+
- Path setup: [mcp.md](mcp.md), [cli.md](cli.md), [api.md](api.md), [browse.md](browse.md)
|
|
138
|
+
- OpenClaw hardening: [host-openclaw.md](host-openclaw.md)
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Common errors across all paths. Full enum: <https://docs.bitrefill.com/docs/error-codes> and <https://docs.bitrefill.com/docs/References>.
|
|
4
|
+
|
|
5
|
+
## Browse path
|
|
6
|
+
|
|
7
|
+
### `403 Forbidden` when fetching bitrefill.com
|
|
8
|
+
|
|
9
|
+
Cloudflare blocks datacenter IPs. Fix: switch to residential browser (ChatGPT Atlas, Cursor browser, Claude+Chrome ext, OpenClaw on user host) or pivot to MCP/CLI/API.
|
|
10
|
+
|
|
11
|
+
### Product appears in listing but not purchasable
|
|
12
|
+
|
|
13
|
+
Geolock at IP level. URL country only filters listed inventory; checkout enforces user's IP. Tell user to access from the matching country (or VPN) — but warn this may violate ToS.
|
|
14
|
+
|
|
15
|
+
## MCP path
|
|
16
|
+
|
|
17
|
+
### Tool not visible to agent
|
|
18
|
+
|
|
19
|
+
- Cursor: 40-tool cap exceeded across all servers. Disable an unused MCP server.
|
|
20
|
+
- ChatGPT: Developer Mode off → write tools (`buy-products`) hidden. Toggle in Settings.
|
|
21
|
+
- Claude.ai consumer: Free tier cannot add custom MCP URLs. Upgrade to Pro+.
|
|
22
|
+
- OpenClaw: `tools.deny: ["bundle-mcp"]` accidentally hiding the server, or per-agent `tools.allow` whitelist excluding it.
|
|
23
|
+
|
|
24
|
+
### `StreamableHTTPError` with HTML body
|
|
25
|
+
|
|
26
|
+
Wrong `MCP_URL` — pointing at non-Bitrefill endpoint. Unset `MCP_URL` env var or set to `https://api.bitrefill.com/mcp`.
|
|
27
|
+
|
|
28
|
+
### OAuth loop in Cursor / Claude.ai
|
|
29
|
+
|
|
30
|
+
Clear browser cookies for `bitrefill.com`, try a different browser, ensure pop-ups not blocked.
|
|
31
|
+
|
|
32
|
+
### MCP server filtered out (OpenClaw)
|
|
33
|
+
|
|
34
|
+
OpenClaw startup safety filter rejects env keys: `NODE_OPTIONS`, `PYTHONSTARTUP`, `PYTHONPATH`, `PERL5OPT`, `RUBYOPT`, `SHELLOPTS`, `PS4`. Use only standard `*_API_KEY` / `GITHUB_TOKEN` / proxy vars in MCP server `env` blocks.
|
|
35
|
+
|
|
36
|
+
### MCP output truncated
|
|
37
|
+
|
|
38
|
+
Default cap varies by host. Claude Code: `MAX_MCP_OUTPUT_TOKENS=50000` to raise. OpenClaw: `tools.toolResultMaxChars` (default 16000). Use pagination: `--per_page 25`, multiple `list-orders` calls.
|
|
39
|
+
|
|
40
|
+
## CLI path
|
|
41
|
+
|
|
42
|
+
### `cart_items` JSON shape error
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
# WRONG (object)
|
|
46
|
+
--cart_items '{"product_id": "steam-usa", "package_id": 5}'
|
|
47
|
+
|
|
48
|
+
# RIGHT (array)
|
|
49
|
+
--cart_items '[{"product_id": "steam-usa", "package_id": 5}]'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### `Invalid denomination 'undefined'`
|
|
53
|
+
|
|
54
|
+
Both `product_id` AND `package_id` required per item.
|
|
55
|
+
|
|
56
|
+
### `Too big: expected array to have <=15 items`
|
|
57
|
+
|
|
58
|
+
Split into multiple `buy-products` calls.
|
|
59
|
+
|
|
60
|
+
### `per_page must be less than 500`
|
|
61
|
+
|
|
62
|
+
Server limit. Use 500 max.
|
|
63
|
+
|
|
64
|
+
### `error: required option '--<name>' not specified`
|
|
65
|
+
|
|
66
|
+
Client-side validation. Add the missing option.
|
|
67
|
+
|
|
68
|
+
### "Must be one of" enum errors
|
|
69
|
+
|
|
70
|
+
| Option | Valid values | Common mistakes |
|
|
71
|
+
|--------|--------------|-----------------|
|
|
72
|
+
| `--payment_method` | `bitcoin`, `lightning`, `ethereum`, `usdc_polygon`, `usdt_polygon`, `usdc_erc20`, `usdt_erc20`, `usdc_arbitrum`, `usdc_solana`, `usdc_base`, `eth_base`, `balance` | `paypal`, `visa`, `USDC_BASE` (case-sensitive) |
|
|
73
|
+
| `--product_type` | `giftcard`, `esim` | `giftcards`, `gift_card`, `sim` |
|
|
74
|
+
| `--country` | `US`, `IT`, `BR` (uppercase Alpha-2) | `us`, `USA`, `"United States"` |
|
|
75
|
+
|
|
76
|
+
### Wrong `package_id` for named denominations
|
|
77
|
+
|
|
78
|
+
Exact, case-sensitive. WRONG `"1GB"`, `"300 nc"`. RIGHT `"1GB, 7 Days"`, `"PUBG New State 300 NC"`. Get exact strings from `get-product-details` `packages` array.
|
|
79
|
+
|
|
80
|
+
### Compound key in `package_id`
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
# WRONG
|
|
84
|
+
--cart_items '[{"product_id": "steam-usa", "package_id": "steam-usa<&>5"}]'
|
|
85
|
+
|
|
86
|
+
# RIGHT (value after <&>)
|
|
87
|
+
--cart_items '[{"product_id": "steam-usa", "package_id": 5}]'
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### CLI auth failure (≥ 0.3.0)
|
|
91
|
+
|
|
92
|
+
CLI uses OAuth client_credentials + email magic link — not API keys, not browser OAuth.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
bitrefill reset # clear corrupt state
|
|
96
|
+
bitrefill login --email you@example.com
|
|
97
|
+
bitrefill verify --code 123456 # add --otp for TOTP
|
|
98
|
+
bitrefill whoami --json
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| Error | Fix |
|
|
102
|
+
|-------|-----|
|
|
103
|
+
| `Access token is required for login/verify` | Any command first (MCP connect), or `reset` then retry |
|
|
104
|
+
| `No pending login` | Run `login --email` before `verify` |
|
|
105
|
+
| `No OTP code provided` on verify | Account has 2FA — add `--otp` (authenticator), keep `--code` (email magic link) |
|
|
106
|
+
| `Invalid code` on verify | Wrong email code or wrong TOTP; don't swap them — email code → `--code`, authenticator → `--otp` |
|
|
107
|
+
| `unknown option '--code'` on login | Code goes on `verify --code`, not `login` |
|
|
108
|
+
| `unknown command 'login'` | Already signed in — `logout` first |
|
|
109
|
+
| `Failed to establish a session` | `reset`, then retry |
|
|
110
|
+
| Invalid / expired code | Re-run `login --email`; headless → poll agent inbox (AgentMail or equivalent — [cli-headless-auth.md](cli-headless-auth.md)) |
|
|
111
|
+
| Missing TOTP | `verify --code … --otp "$(op read 'op://Vault/Item/one-time password?attribute=otp')"` |
|
|
112
|
+
|
|
113
|
+
State: `~/.config/bitrefill-cli/<host>.v1.json`. `logout` revokes session; `reset` clears everything.
|
|
114
|
+
|
|
115
|
+
Pre-0.3.0 (`credentials.json`, `--api-key`): upgrade CLI. Developer API keys still work for [mcp.md](mcp.md) / [api.md](api.md) paths only.
|
|
116
|
+
|
|
117
|
+
### Empty search results, no error
|
|
118
|
+
|
|
119
|
+
`found: 0` with no error message. Causes:
|
|
120
|
+
|
|
121
|
+
- `--category` slug doesn't exist (silent miss).
|
|
122
|
+
- Product not available in `--country`.
|
|
123
|
+
- `--in_stock true` (default) filters out-of-stock.
|
|
124
|
+
|
|
125
|
+
Fix: drop `--category`, change `--country`, or `--in_stock false`.
|
|
126
|
+
|
|
127
|
+
### Unpaid invoices missing from list
|
|
128
|
+
|
|
129
|
+
`list-invoices` defaults `--only_paid true`. Use `--only_paid false`.
|
|
130
|
+
|
|
131
|
+
## API path
|
|
132
|
+
|
|
133
|
+
### `401 Unauthorized`
|
|
134
|
+
|
|
135
|
+
- Personal: `Authorization: Bearer $BITREFILL_API_KEY` missing or wrong key.
|
|
136
|
+
- Business / Affiliate: `Authorization: Basic $(echo -n "$ID:$SECRET" | base64)` malformed.
|
|
137
|
+
|
|
138
|
+
### `429 Too Many Requests`
|
|
139
|
+
|
|
140
|
+
Rate limited. Defaults: 60 req / 10 min on most endpoints, 60 req/min on `/products` + `/products/search` plus 1000 product req/hr quota, 1 req / 3 s on `/ping`. Back off + retry. Cache product catalog locally.
|
|
141
|
+
|
|
142
|
+
### `RESOURCE_NOT_FOUND` on `GET /invoices/{id}`
|
|
143
|
+
|
|
144
|
+
Bad invoice ID. Verify via `list-invoices`.
|
|
145
|
+
|
|
146
|
+
### `Product '{slug}' is not available`
|
|
147
|
+
|
|
148
|
+
Bad product slug. Verify via `search-products`.
|
|
149
|
+
|
|
150
|
+
### Invoice expired
|
|
151
|
+
|
|
152
|
+
Invoices expire after **180 minutes**. Cannot re-pay. Create new one.
|
|
153
|
+
|
|
154
|
+
## OpenClaw-specific
|
|
155
|
+
|
|
156
|
+
### Cron purchase failed silently
|
|
157
|
+
|
|
158
|
+
`exec-approvals.json` set to `ask: on-miss` but no operator online to `/approve`. Either pre-approve `bitrefill buy-products` for trusted SKU/amount, or schedule when operator available.
|
|
159
|
+
|
|
160
|
+
### Pi agent can't see the Bitrefill MCP
|
|
161
|
+
|
|
162
|
+
Check:
|
|
163
|
+
|
|
164
|
+
1. `openclaw mcp list` shows entry.
|
|
165
|
+
2. `~/.openclaw/openclaw.json` parses (no trailing commas).
|
|
166
|
+
3. Agent profile not denying `bundle-mcp` or whitelisting tools narrowly.
|
|
167
|
+
4. CLI signed in (`bitrefill whoami --json`) or MCP OAuth completed — not just shell env vars.
|
|
168
|
+
|
|
169
|
+
### Mobile node camera tool unavailable
|
|
170
|
+
|
|
171
|
+
Node not paired or paired but offline. Check `openclaw nodes list`. Re-pair via Control UI (`openclaw dashboard`).
|
|
172
|
+
|
|
173
|
+
### Telegram message not reaching agent
|
|
174
|
+
|
|
175
|
+
`channels.telegram.dmPolicy: "pairing"` and sender not paired. Run `openclaw pairing approve telegram <CODE>` (codes expire 1 hr).
|
|
176
|
+
|
|
177
|
+
## Source of truth
|
|
178
|
+
|
|
179
|
+
- Bitrefill error codes: <https://docs.bitrefill.com/docs/error-codes>
|
|
180
|
+
- Bitrefill error handling: <https://docs.bitrefill.com/docs/References>
|
|
181
|
+
- Rate limits: <https://docs.bitrefill.com/docs/rate-limits>
|
|
182
|
+
- OpenClaw troubleshooting: <https://docs.openclaw.ai/help> + per-tool pages
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: kaleido-trading
|
|
3
|
+
description: "Trade and manage a portfolio on KaleidoSwap: get live prices and market data, quote and place atomic swaps between BTC and RGB assets (USDT, XAUT), acquire inbound liquidity from the LSP, and check positions. Triggers when the user wants a price, a quote, to swap or trade assets, to rebalance a portfolio, or to acquire a Lightning channel from the LSP."
|
|
4
|
+
tools: get_price, get_market_data, kaleidoswap_get_pairs, kaleidoswap_get_quote, kaleidoswap_place_order, kaleidoswap_get_order_status, kaleidoswap_get_position, kaleidoswap_lsp_get_info, kaleidoswap_lsp_create_order
|
|
5
|
+
triggers: price, quote, swap, trade, rebalance, portfolio, market, slippage, liquidity, channel order
|
|
6
|
+
metadata:
|
|
7
|
+
author: kaleidoswap
|
|
8
|
+
version: "0.1.0"
|
|
9
|
+
surface: "kaleido-mcp (KaleidoSwap maker + market data)"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# KaleidoSwap trading
|
|
13
|
+
|
|
14
|
+
Quote, swap, and manage a portfolio through the KaleidoSwap maker and market
|
|
15
|
+
data MCP tools.
|
|
16
|
+
|
|
17
|
+
## Flow
|
|
18
|
+
|
|
19
|
+
1. **Price first.** Use `get_price` / `get_market_data` for context and
|
|
20
|
+
`kaleidoswap_get_quote` for an executable price before proposing a trade.
|
|
21
|
+
2. **Confirm the trade.** Show pair, direction, amount in, expected amount out,
|
|
22
|
+
and fees. Wait for explicit approval before `kaleidoswap_place_order`.
|
|
23
|
+
3. **Track it.** After placing, poll `kaleidoswap_get_order_status` until the
|
|
24
|
+
swap settles, then report the result and the new `kaleidoswap_get_position`.
|
|
25
|
+
4. **Need inbound liquidity?** If a swap can't route for lack of a channel, use
|
|
26
|
+
`kaleidoswap_lsp_get_info` then `kaleidoswap_lsp_create_order` to buy one.
|
|
27
|
+
|
|
28
|
+
## Rules
|
|
29
|
+
|
|
30
|
+
- Never trade on a stale quote — re-quote if the user hesitates.
|
|
31
|
+
- Surface slippage and fees explicitly; small local models must not hide cost.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: kaleido-wallet
|
|
3
|
+
description: "Manage a KaleidoSwap Lightning + RGB wallet: check BTC and asset balances, get a receive address, create or pay Lightning invoices, send on-chain BTC, open or list channels. Triggers when the user asks about their balance, wants to receive or send funds, pay an invoice, or manage Lightning channels."
|
|
4
|
+
tools: wdk_get_balances, wdk_get_asset_balance, wdk_get_address, wdk_create_ln_invoice, wdk_pay_invoice, wdk_send_btc, wdk_list_channels, wdk_open_channel, wdk_get_node_info
|
|
5
|
+
triggers: balance, receive, address, send, pay, invoice, channel, deposit, withdraw, funds
|
|
6
|
+
metadata:
|
|
7
|
+
author: kaleidoswap
|
|
8
|
+
version: "0.1.0"
|
|
9
|
+
surface: "kaleido-mcp (WDK node)"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# KaleidoSwap wallet
|
|
13
|
+
|
|
14
|
+
Operate the user's KaleidoSwap node (Lightning + RGB assets) through the
|
|
15
|
+
`wdk_*` MCP tools.
|
|
16
|
+
|
|
17
|
+
## Rules
|
|
18
|
+
|
|
19
|
+
- **Read before you write.** Check the balance (`wdk_get_balances`) before any
|
|
20
|
+
send or channel open, and confirm the node is healthy (`wdk_get_node_info`).
|
|
21
|
+
- **Confirm every spend.** State the amount, destination, and resulting balance,
|
|
22
|
+
then wait for explicit approval before calling `wdk_send_btc`,
|
|
23
|
+
`wdk_pay_invoice`, or `wdk_open_channel`.
|
|
24
|
+
- **Match the rail to the asset.** Lightning for fast BTC/asset payments,
|
|
25
|
+
on-chain for settlement or channel funding. Use `wdk_get_asset_balance` for
|
|
26
|
+
RGB assets (USDT, XAUT, …).
|
|
27
|
+
- Never reveal seeds or private keys — this skill operates a node, it is not a
|
|
28
|
+
key vault.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability profiling — decide which features to turn on for a given device +
|
|
3
|
+
* model, so a 2 GB phone running a 0.6B model doesn't try to do everything.
|
|
4
|
+
*
|
|
5
|
+
* Pure heuristic. Hosts call this once (with device RAM + the model's context
|
|
6
|
+
* size + whether an embedder is available) and get back feature flags + sane
|
|
7
|
+
* retrieval defaults to feed the ContextBuilder.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { contextBudgetTokens } from './context/budget.js';
|
|
11
|
+
|
|
12
|
+
export interface CapabilityInput {
|
|
13
|
+
/** Total device RAM in bytes (e.g. react-native-device-info getTotalMemory). */
|
|
14
|
+
ramBytes?: number;
|
|
15
|
+
/** The loaded model's context window in tokens (modelConfig.ctx_size). */
|
|
16
|
+
modelCtxTokens: number;
|
|
17
|
+
/** Whether an EmbeddingProvider is wired (QVAC embed, etc.). */
|
|
18
|
+
hasEmbeddings?: boolean;
|
|
19
|
+
/** Running inference on a remote provider (desktop/server) — relaxes limits. */
|
|
20
|
+
delegated?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MindCapabilities {
|
|
24
|
+
/** Long-term memory (cheap — on unless the window is tiny). */
|
|
25
|
+
memory: boolean;
|
|
26
|
+
/** Semantic recall for memory (needs embeddings). */
|
|
27
|
+
semanticMemory: boolean;
|
|
28
|
+
/** Retrieval-augmented generation (needs embeddings + enough RAM/context). */
|
|
29
|
+
rag: boolean;
|
|
30
|
+
/** Token budget for injected system context. */
|
|
31
|
+
contextBudgetTokens: number;
|
|
32
|
+
/** Memories to auto-recall into context. */
|
|
33
|
+
topKMemory: number;
|
|
34
|
+
/** Knowledge chunks the search_knowledge tool returns. */
|
|
35
|
+
topKRag: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const GiB = 1024 * 1024 * 1024;
|
|
39
|
+
|
|
40
|
+
export function capabilityProfile(input: CapabilityInput): MindCapabilities {
|
|
41
|
+
const ramGb = input.delegated ? Infinity : (input.ramBytes ?? 0) / GiB;
|
|
42
|
+
const ctx = input.modelCtxTokens;
|
|
43
|
+
const hasEmb = !!input.hasEmbeddings;
|
|
44
|
+
|
|
45
|
+
const budget = contextBudgetTokens(ctx);
|
|
46
|
+
|
|
47
|
+
// Memory: on whenever there's room for a few lines. Semantic needs embeddings.
|
|
48
|
+
const memory = budget >= 256;
|
|
49
|
+
const semanticMemory = memory && hasEmb;
|
|
50
|
+
|
|
51
|
+
// RAG is the expensive one: needs embeddings, a non-tiny context window, and
|
|
52
|
+
// (on-device) enough RAM to hold an embedding model + index.
|
|
53
|
+
const rag = hasEmb && ctx >= 4096 && (input.delegated || ramGb >= 3);
|
|
54
|
+
|
|
55
|
+
// Scale how much we pull in with the available window.
|
|
56
|
+
const topKMemory = budget >= 1500 ? 4 : budget >= 700 ? 3 : 2;
|
|
57
|
+
const topKRag = rag ? (budget >= 2500 ? 5 : 3) : 0;
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
memory,
|
|
61
|
+
semanticMemory,
|
|
62
|
+
rag,
|
|
63
|
+
contextBudgetTokens: budget,
|
|
64
|
+
topKMemory,
|
|
65
|
+
topKRag,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context budgeting — the hardware-aware part.
|
|
3
|
+
*
|
|
4
|
+
* Small on-device models have small context windows (a 0.6B may run at 2k, a
|
|
5
|
+
* desktop 8B at 8k+). Memory, skills, RAG and tool schemas all compete for that
|
|
6
|
+
* window. These helpers turn a model's `ctx_size` into a token budget for the
|
|
7
|
+
* injected context, so we never overflow a tiny model.
|
|
8
|
+
*
|
|
9
|
+
* Token estimation is deliberately rough (≈ 4 chars/token) — no tokenizer dep.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Rough token count for a string (no tokenizer — ~4 chars/token). */
|
|
13
|
+
export function estimateTokens(text: string): number {
|
|
14
|
+
return Math.ceil(text.length / 4);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Trim text to at most `maxTokens` (on a word boundary where possible). */
|
|
18
|
+
export function clampToTokens(text: string, maxTokens: number): string {
|
|
19
|
+
const maxChars = maxTokens * 4;
|
|
20
|
+
if (text.length <= maxChars) return text;
|
|
21
|
+
const cut = text.slice(0, maxChars);
|
|
22
|
+
const sp = cut.lastIndexOf(' ');
|
|
23
|
+
return (sp > maxChars * 0.6 ? cut.slice(0, sp) : cut).trimEnd() + '…';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface BudgetReserves {
|
|
27
|
+
/** Tokens to leave for the model's reply. Default 512. */
|
|
28
|
+
output?: number;
|
|
29
|
+
/** Tokens to budget for tool schemas the engine sends. Default 600. */
|
|
30
|
+
tools?: number;
|
|
31
|
+
/** Tokens to budget for the running conversation (user + history). Default 768. */
|
|
32
|
+
conversation?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Tokens available for the *injected system context* (soul + instructions +
|
|
37
|
+
* skill + memory + RAG), given the model's context window and what else must
|
|
38
|
+
* fit. Never negative; clamped to a sane floor so something always gets in.
|
|
39
|
+
*/
|
|
40
|
+
export function contextBudgetTokens(ctxSize: number, reserves: BudgetReserves = {}): number {
|
|
41
|
+
const output = reserves.output ?? 512;
|
|
42
|
+
const tools = reserves.tools ?? 600;
|
|
43
|
+
const conversation = reserves.conversation ?? 768;
|
|
44
|
+
const available = ctxSize - output - tools - conversation;
|
|
45
|
+
return Math.max(256, available);
|
|
46
|
+
}
|