@kaleidorg/mind 0.8.1 → 0.10.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.
Files changed (133) hide show
  1. package/README.md +7 -5
  2. package/dist/bitrefill/index.d.ts +4 -0
  3. package/dist/bitrefill/index.d.ts.map +1 -0
  4. package/dist/bitrefill/index.js +3 -0
  5. package/dist/bitrefill/index.js.map +1 -0
  6. package/dist/capabilities.d.ts +3 -3
  7. package/dist/capabilities.d.ts.map +1 -1
  8. package/dist/capabilities.js +4 -4
  9. package/dist/capabilities.js.map +1 -1
  10. package/dist/engine/answer.d.ts +37 -0
  11. package/dist/engine/answer.d.ts.map +1 -0
  12. package/dist/engine/answer.js +35 -0
  13. package/dist/engine/answer.js.map +1 -0
  14. package/dist/engine.d.ts +9 -3
  15. package/dist/engine.d.ts.map +1 -1
  16. package/dist/engine.js +159 -175
  17. package/dist/engine.js.map +1 -1
  18. package/dist/evidence.d.ts +1 -1
  19. package/dist/evidence.d.ts.map +1 -1
  20. package/dist/flashnet/index.d.ts +5 -0
  21. package/dist/flashnet/index.d.ts.map +1 -0
  22. package/dist/flashnet/index.js +4 -0
  23. package/dist/flashnet/index.js.map +1 -0
  24. package/dist/guards.d.ts.map +1 -1
  25. package/dist/guards.js +20 -0
  26. package/dist/guards.js.map +1 -1
  27. package/dist/index.d.ts +7 -24
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +11 -28
  30. package/dist/index.js.map +1 -1
  31. package/dist/kaleidoswap/contract.d.ts +7 -0
  32. package/dist/kaleidoswap/contract.d.ts.map +1 -1
  33. package/dist/kaleidoswap/contract.js +71 -17
  34. package/dist/kaleidoswap/contract.js.map +1 -1
  35. package/dist/kaleidoswap/index.d.ts +8 -0
  36. package/dist/kaleidoswap/index.d.ts.map +1 -0
  37. package/dist/kaleidoswap/index.js +7 -0
  38. package/dist/kaleidoswap/index.js.map +1 -0
  39. package/dist/knowledge/index.d.ts +9 -0
  40. package/dist/knowledge/index.d.ts.map +1 -0
  41. package/dist/knowledge/index.js +6 -0
  42. package/dist/knowledge/index.js.map +1 -0
  43. package/dist/lsps1/index.d.ts +4 -0
  44. package/dist/lsps1/index.d.ts.map +1 -0
  45. package/dist/lsps1/index.js +3 -0
  46. package/dist/lsps1/index.js.map +1 -0
  47. package/dist/providers/types.d.ts +3 -3
  48. package/dist/providers/types.js +3 -3
  49. package/dist/qvac/index.d.ts +0 -1
  50. package/dist/qvac/index.d.ts.map +1 -1
  51. package/dist/qvac/index.js +0 -1
  52. package/dist/qvac/index.js.map +1 -1
  53. package/dist/qvac/provider.d.ts +9 -9
  54. package/dist/qvac/provider.d.ts.map +1 -1
  55. package/dist/qvac/provider.js +115 -109
  56. package/dist/qvac/provider.js.map +1 -1
  57. package/dist/qvac/stream.d.ts +4 -3
  58. package/dist/qvac/stream.d.ts.map +1 -1
  59. package/dist/qvac/stream.js.map +1 -1
  60. package/dist/qvac/voice.d.ts +1 -1
  61. package/dist/recipe/asset-send.js +1 -1
  62. package/dist/recipe/asset-send.js.map +1 -1
  63. package/dist/submarine/index.d.ts +5 -0
  64. package/dist/submarine/index.d.ts.map +1 -0
  65. package/dist/submarine/index.js +4 -0
  66. package/dist/submarine/index.js.map +1 -0
  67. package/dist/testing/mock-wallet.d.ts.map +1 -1
  68. package/dist/testing/mock-wallet.js +6 -0
  69. package/dist/testing/mock-wallet.js.map +1 -1
  70. package/dist/tools/in-process.d.ts +2 -2
  71. package/dist/tools/in-process.js +2 -2
  72. package/dist/wallet/contract.d.ts +5 -0
  73. package/dist/wallet/contract.d.ts.map +1 -1
  74. package/dist/wallet/contract.js +39 -6
  75. package/dist/wallet/contract.js.map +1 -1
  76. package/package.json +32 -2
  77. package/scripts/snapshot-mcp-tools.mjs +38 -0
  78. package/skills/README.md +98 -64
  79. package/skills/bitrefill/SKILL.md +30 -157
  80. package/skills/channel-manager/SKILL.md +31 -52
  81. package/skills/flashnet-swaps/SKILL.md +24 -150
  82. package/skills/kaleido-node/SKILL.md +25 -55
  83. package/skills/kaleido-trading/SKILL.md +28 -172
  84. package/skills/kaleido-trading/references/assets.md +4 -4
  85. package/skills/kaleido-trading/references/atomic.md +5 -7
  86. package/skills/merchant-finder/SKILL.md +25 -108
  87. package/skills/paid-data/SKILL.md +25 -58
  88. package/skills/portfolio-manager/SKILL.md +26 -60
  89. package/skills/rgb-lightning-node/SKILL.md +37 -255
  90. package/skills/rgb-lightning-node/references/channels.md +34 -0
  91. package/skills/spark-wallet/SKILL.md +26 -228
  92. package/skills/submarine-swaps/SKILL.md +19 -37
  93. package/skills/wallet-assistant/SKILL.md +26 -44
  94. package/src/bitrefill/index.ts +13 -0
  95. package/src/capabilities.ts +7 -7
  96. package/src/context/context.test.ts +2 -2
  97. package/src/engine/answer.ts +66 -0
  98. package/src/engine.ts +185 -194
  99. package/src/evidence.ts +1 -1
  100. package/src/flashnet/index.ts +14 -0
  101. package/src/funnel.mind.test.ts +6 -5
  102. package/src/guards.test.ts +12 -0
  103. package/src/guards.ts +20 -0
  104. package/src/index.ts +11 -107
  105. package/src/kaleidoswap/contract.test.ts +32 -3
  106. package/src/kaleidoswap/contract.ts +65 -17
  107. package/src/kaleidoswap/index.ts +20 -0
  108. package/src/knowledge/index.ts +14 -0
  109. package/src/lsps1/index.ts +13 -0
  110. package/src/providers/types.ts +3 -3
  111. package/src/qvac/index.ts +0 -8
  112. package/src/qvac/provider.test.ts +17 -17
  113. package/src/qvac/provider.ts +33 -31
  114. package/src/qvac/stream.ts +4 -3
  115. package/src/qvac/voice.ts +1 -1
  116. package/src/recipe/asset-send.ts +1 -1
  117. package/src/recipe/recipe.test.ts +1 -1
  118. package/src/skills/catalog.test.ts +215 -0
  119. package/src/skills/mcp-tools.snapshot.json +1938 -0
  120. package/src/submarine/index.ts +17 -0
  121. package/src/testing/mock-wallet.ts +6 -0
  122. package/src/tools/in-process.ts +2 -2
  123. package/src/wallet/contract.test.ts +20 -1
  124. package/src/wallet/contract.ts +36 -6
  125. package/dist/qvac/delegate.d.ts +0 -50
  126. package/dist/qvac/delegate.d.ts.map +0 -1
  127. package/dist/qvac/delegate.js +0 -53
  128. package/dist/qvac/delegate.js.map +0 -1
  129. package/skills/dca/SKILL.md +0 -48
  130. package/skills/kaleido-lsps/SKILL.md +0 -131
  131. package/skills/liquidity-optimizer/SKILL.md +0 -91
  132. package/src/qvac/delegate.test.ts +0 -68
  133. package/src/qvac/delegate.ts +0 -73
@@ -1,48 +1,30 @@
1
1
  ---
2
2
  name: submarine-swaps
3
- description: "Pay a Lightning invoice from Liquid funds (L-USDT or L-BTC) through a KaleidoSwap submarine swap on the /v2 maker, and follow its status. Triggers when the user wants to pay a Lightning invoice with Liquid USDT / L-USDT / L-BTC, asks what Liquid assets can pay Lightning, or asks about a submarine swap."
3
+ description: "Pay a Lightning invoice from Liquid funds (L-USDT or L-BTC) through a KaleidoSwap submarine swap, and follow its status."
4
4
  tools: kaleidoswap_submarine_pairs, kaleidoswap_submarine_create, kaleidoswap_submarine_fund, kaleidoswap_submarine_status
5
- triggers: l-usdt, liquid usdt, usdt on liquid, l-btc, liquid bitcoin, submarine, submarine swap, pay invoice with liquid, swap status
5
+ requires-tools: kaleidoswap_submarine_create
6
+ triggers: l-usdt, liquid usdt, usdt on liquid, l-btc, liquid bitcoin, submarine, submarine swap, pay invoice with liquid
6
7
  metadata:
7
8
  author: kaleidoswap
8
- version: "0.1.0"
9
+ version: "0.2.0"
9
10
  ---
11
+ # Submarine swaps
10
12
 
11
- # Submarine swaps — pay Lightning from Liquid
13
+ The user locks Liquid funds; the maker pays the Lightning invoice, then claims
14
+ the lockup. If the maker can't pay, the lockup is refundable. Plain "USDT" is
15
+ RGB USDT (`kaleido-trading`), not this skill.
12
16
 
13
- A submarine swap pays a Lightning (BOLT11) invoice with funds the user locks on
14
- Liquid. The KaleidoSwap maker pays the invoice, then claims the lockup. It is
15
- atomic: if the maker can't pay, the lockup is refunded to the user.
16
-
17
- ## Critical rules
18
-
19
- - Every amount, fee and status in your reply MUST come from a tool result in the
20
- CURRENT turn. Never guess the amount to lock — `kaleidoswap_submarine_create`
21
- returns it.
22
- - `kaleidoswap_submarine_fund` moves money. Call it ONLY after the user has
23
- confirmed the exact `expected_amount` returned by `kaleidoswap_submarine_create`.
24
- - `kaleidoswap_submarine_fund` takes only `swap_id`. Never try to pass an amount
25
- or an address.
26
- - A bare "USDT" means RGB USDT on the Lightning node, which uses the atomic swap
27
- tools, not this skill. Use this skill only for Liquid assets (L-USDT, L-BTC).
28
-
29
- ## Flow
30
-
31
- 1. `kaleidoswap_submarine_create { invoice, from_asset }` — from_asset is
32
- `L-USDT` (default) or `L-BTC`. Returns `swap_id`, `expected_amount` (smallest
33
- unit: L-USDT has 8 decimals, L-BTC is sats) and the fees.
17
+ ## Do
18
+ 1. `kaleidoswap_submarine_create` (`invoice`, `from_asset`) — `from_asset` is
19
+ `L-USDT` (default) or `L-BTC`. Returns `swap_id`, `expected_amount`
20
+ (smallest unit, 8 decimals for both) and fees. Nothing moves yet.
34
21
  2. Tell the user the amount and ask to confirm.
35
- 3. `kaleidoswap_submarine_fund { swap_id }`.
36
- 4. `kaleidoswap_submarine_status { swap_id }` — `transaction.claimed` means the
37
- invoice was paid. `failed: true` on a funded swap means the funds need a refund;
38
- say so and quote the `refund` field.
39
-
40
- Use `kaleidoswap_submarine_pairs` when the user asks what can pay a Lightning
41
- invoice, or for the limits and fees.
22
+ 3. `kaleidoswap_submarine_fund` (`swap_id` only; confirm-gated).
23
+ 4. `kaleidoswap_submarine_status` (`swap_id`) — `transaction.claimed` means paid;
24
+ `failed: true` on a funded swap needs a refund: quote the `refund` field.
25
+ - Limits and fees: `kaleidoswap_submarine_pairs {}`.
42
26
 
43
27
  ## Examples
44
-
45
- - "pay lntbs10u1p… with L-USDT" →
46
- `kaleidoswap_submarine_create { invoice: "lntbs10u1p…", from_asset: "L-USDT" }`
47
- - "did my Liquid payment go through? swap sub1" →
48
- `kaleidoswap_submarine_status { swap_id: "sub1" }`
28
+ - "Pay lntbs10u1p… with L-USDT" → `kaleidoswap_submarine_create {"invoice":"lntbs10u1p…","from_asset":"L-USDT"}`
29
+ - "Did swap sub1 go through?" → `kaleidoswap_submarine_status {"swap_id":"sub1"}`
30
+ - "What can pay Lightning from Liquid?" → `kaleidoswap_submarine_pairs {}`
@@ -1,49 +1,31 @@
1
1
  ---
2
2
  name: wallet-assistant
3
- description: Everyday wallet tasks on this phone — check the BTC/asset balance, create an invoice to receive, send a payment, look up a contact, or quote a swap (e.g. "how many sats is 10 USDT?"). Triggers when the user asks about their balance, wants to receive or send money, pay an invoice, pay a contact, or convert between BTC and supported assets.
4
- tools: get_balances, rln_get_balances, wdk_get_balances, spark_get_balance, rln_get_asset_balance, wdk_get_asset_balance, rln_list_assets, wdk_list_assets, rln_get_address, wdk_get_address, spark_get_address, spark_get_onchain_address, resolve_contact, send_payment, rln_send_btc, wdk_send_btc, rln_send_asset, wdk_send_asset, spark_send, spark_send_sats, rln_pay_invoice, wdk_pay_invoice, spark_pay_invoice, spark_pay_lightning_invoice, rln_create_ln_invoice, wdk_create_ln_invoice, spark_create_invoice, spark_create_lightning_invoice, rln_create_rgb_invoice, wdk_create_rgb_invoice, rln_list_payments, wdk_list_payments, kaleidoswap_get_quote
5
- triggers: balance, pay, send, receive, address, invoice, transactions, contact, funds, money
3
+ description: "Everyday wallet tasks in the app across all layers: balance, receive (invoice or address), send or pay, contacts, BTC price and fiat conversion."
4
+ tools: get_balances, create_invoice, send_payment, resolve_contact, get_price, fiat_to_sats, rln_list_assets, rln_pay_invoice, spark_pay_invoice
5
+ requires-tools: get_balances
6
+ triggers: balance, balances, pay, send, receive, address, invoice, contact, funds, money, price, how much
7
+ metadata:
8
+ author: kaleidoswap
9
+ version: "0.3.0"
6
10
  ---
7
-
8
11
  # Wallet assistant
9
12
 
10
- You operate the user's on-device multi-L2 Bitcoin wallet. ALWAYS use a tool to
11
- get real data — NEVER invent a balance, address, amount, price, or quote.
12
-
13
- ## Critical rules
14
-
15
- You have no knowledge of balances, addresses, invoices, prices, or quotes.
16
- Every value in your reply MUST come from a tool result returned in the CURRENT
17
- turn — do not reuse a number from a previous turn.
18
-
19
- NEVER mention the exact name of any tool (such as "kaleidoswap_get_quote") in
20
- your text reply to the user. Only use tools via the proper function call
21
- format when needed; describe what you are doing in plain language.
22
-
23
- When a tool returns multiple fields, **report all the load-bearing ones**:
24
- - The balance tool may return `{confirmed, pending, total}` — when `pending`
25
- is non-zero, report BOTH. `confirmed` is spendable; `pending` is settling
26
- and is NOT spendable yet. The user needs to know the difference.
27
- - The quote tool returns display strings (e.g. the to amount and fee in
28
- human readable form). Read these strings verbatim — do NOT do unit math
29
- yourself.
30
-
31
- ## Rules
32
-
33
- - **Balance / "how much do I have"** → use the balance tool, then state the
34
- number.
35
- - **Receive / "an invoice for N sats"** → use the invoice creation tool (for
36
- the appropriate layer) with the amount.
37
- - **"How many sats is N USDT?" / "What's 0.1 BTC in USDT?" / "convert N X
38
- to Y"** → use the quote/conversion tool and
39
- read the display fields from the result. Supported pairs: any of
40
- BTC/USDT/XAUT against each other. Fiat (EUR/USD/GBP) is NOT quoted by
41
- the maker — if the user asks "how much sats is 10 EUR", say so plainly
42
- and offer USDT as the closest analogue (USDT is a USD-pegged stablecoin).
43
- - **Pay a Lightning invoice** → use the lightning payment tool.
44
- - **Send to a person/amount** → first resolve the contact, then use the send
45
- payment tool with the amount in sats and the recipient. State the amount and
46
- destination; the app asks the user to confirm before it sends.
47
-
48
- Keep replies short, but never drop a balance component, a fee, or the
49
- quote's `rfq_id` when present.
13
+ The app routes these tools to the right layer (Spark, RLN, Arkade, Liquid).
14
+ `amount_sats` is satoshis; other amounts are display units of the asset.
15
+
16
+ ## Do
17
+ - Balance: `get_balances` (add `layer` for one layer). Report every component;
18
+ `pending` is not spendable yet.
19
+ - Receive: `create_invoice` with `asset` and optional `amount`; reply with the
20
+ full invoice or address returned.
21
+ - Send to a name: `resolve_contact` first, then `send_payment`. A BOLT11
22
+ invoice the user pasted: `rln_pay_invoice` or `spark_pay_invoice`. Every
23
+ send is confirm-gated; state amount and destination.
24
+ - Fiat: `fiat_to_sats` converts; `get_price` gives the BTC price.
25
+ - Swaps go to `kaleido-trading` (RGB) or `flashnet-swaps` (Spark tokens).
26
+
27
+ ## Examples
28
+ - "What's my balance?" → `get_balances {}`
29
+ - "Invoice for 2000 sats" → `create_invoice {"asset":"BTC","amount":2000}`
30
+ - "Send 5000 sats to bob" → `resolve_contact {"name":"bob"}` then `send_payment {"to":"<bob's address>","amount_sats":5000}`
31
+ - "How many sats is 10 EUR?" → `fiat_to_sats {"amount":10,"currency":"EUR"}`
@@ -0,0 +1,13 @@
1
+ /** Bitrefill (gift cards, top-ups, eSIMs): tool contract. */
2
+ export {
3
+ BITREFILL_TOOLS,
4
+ BITREFILL_SPEND_TOOLS,
5
+ isBitrefillSpendTool,
6
+ getBitrefillTool,
7
+ bindBitrefillTools,
8
+ } from './contract.js';
9
+ export type {
10
+ BitrefillToolDef,
11
+ BitrefillHandler,
12
+ BindBitrefillOptions,
13
+ } from './contract.js';
@@ -16,8 +16,8 @@ export interface CapabilityInput {
16
16
  modelCtxTokens: number;
17
17
  /** Whether an EmbeddingProvider is wired (QVAC embed, etc.). */
18
18
  hasEmbeddings?: boolean;
19
- /** Running inference on a remote provider (desktop/server) — relaxes limits. */
20
- delegated?: boolean;
19
+ /** Inference runs on a larger remote model (e.g. an OpenAI-compatible server) — relaxes limits. */
20
+ remote?: boolean;
21
21
  }
22
22
 
23
23
  export interface MindCapabilities {
@@ -27,7 +27,7 @@ export interface MindCapabilities {
27
27
  semanticMemory: boolean;
28
28
  /** Embedding-only dedup of near-duplicate memories (zero inference — mobile-safe). */
29
29
  dedupeMemory: boolean;
30
- /** LLM merge of near-duplicate memories (an extra inference — capable/delegated only). */
30
+ /** LLM merge of near-duplicate memories (an extra inference — capable/remote only). */
31
31
  mergeMemory: boolean;
32
32
  /** Retrieval-augmented generation (needs embeddings + enough RAM/context). */
33
33
  rag: boolean;
@@ -42,7 +42,7 @@ export interface MindCapabilities {
42
42
  const GiB = 1024 * 1024 * 1024;
43
43
 
44
44
  export function capabilityProfile(input: CapabilityInput): MindCapabilities {
45
- const ramGb = input.delegated ? Infinity : (input.ramBytes ?? 0) / GiB;
45
+ const ramGb = input.remote ? Infinity : (input.ramBytes ?? 0) / GiB;
46
46
  const ctx = input.modelCtxTokens;
47
47
  const hasEmb = !!input.hasEmbeddings;
48
48
 
@@ -54,13 +54,13 @@ export function capabilityProfile(input: CapabilityInput): MindCapabilities {
54
54
 
55
55
  // Consolidation: embedding-only dedup is cheap (no inference) — on wherever
56
56
  // semantic memory is. The LLM merge costs an extra inference, so reserve it
57
- // for delegated or roomy on-device setups; never run it on a tiny phone model.
57
+ // for remote or roomy on-device setups; never run it on a tiny phone model.
58
58
  const dedupeMemory = semanticMemory;
59
- const mergeMemory = dedupeMemory && (input.delegated || (ramGb >= 4 && ctx >= 4096));
59
+ const mergeMemory = dedupeMemory && (input.remote || (ramGb >= 4 && ctx >= 4096));
60
60
 
61
61
  // RAG is the expensive one: needs embeddings, a non-tiny context window, and
62
62
  // (on-device) enough RAM to hold an embedding model + index.
63
- const rag = hasEmb && ctx >= 4096 && (input.delegated || ramGb >= 3);
63
+ const rag = hasEmb && ctx >= 4096 && (input.remote || ramGb >= 3);
64
64
 
65
65
  // Scale how much we pull in with the available window.
66
66
  const topKMemory = budget >= 1500 ? 4 : budget >= 700 ? 3 : 2;
@@ -69,8 +69,8 @@ describe('capabilityProfile', () => {
69
69
  expect(c.mergeMemory).toBe(false); // never run merge inference on a tiny phone
70
70
  });
71
71
 
72
- it('desktop / delegated: RAG on, memory merge on', () => {
73
- const c = capabilityProfile({ modelCtxTokens: 8192, hasEmbeddings: true, delegated: true });
72
+ it('remote model: RAG on, memory merge on', () => {
73
+ const c = capabilityProfile({ modelCtxTokens: 8192, hasEmbeddings: true, remote: true });
74
74
  expect(c.rag).toBe(true);
75
75
  expect(c.topKRag).toBeGreaterThan(0);
76
76
  expect(c.semanticMemory).toBe(true);
@@ -0,0 +1,66 @@
1
+ /**
2
+ * How an agentic run ends: the Engine's fixed replies, and the checks every
3
+ * answer the model wrote goes through before the user sees it.
4
+ *
5
+ * An answer is tagged with where it came from. Fixed replies are final; only
6
+ * model text is rewritten (amount fixes) or replaced (payment data no tool
7
+ * returned). Keeping that rule in one place is what stops a fixed reply that
8
+ * quotes a readback from being mistaken for an invented invoice.
9
+ */
10
+ import type { ToolCallError } from '../providers/types.js';
11
+ import { findUngroundedPaymentData, fixSatsBtcConversions, ungroundedReply } from '../guards.js';
12
+ import { fixRgbBalanceUnits } from '../context/rgb-units.js';
13
+
14
+ export interface Answer {
15
+ text: string;
16
+ /** `engine`: one of the fixed replies below, never rewritten. */
17
+ source: 'model' | 'engine';
18
+ }
19
+
20
+ export const model = (text: string): Answer => ({ text, source: 'model' });
21
+ export const engine = (text: string): Answer => ({ text, source: 'engine' });
22
+
23
+ export const STOPPED_REPLY = 'I had to stop after several steps — please try a more specific request.';
24
+
25
+ export const TOOL_CALL_FAILED_REPLY =
26
+ "I couldn't put together a valid request for that. Please rephrase it with the exact values (asset, amount, recipient).";
27
+
28
+ export const REPEATED_CALL_REPLY = 'I could not get a different result from the wallet — please try a more specific request.';
29
+
30
+ export function cancelledReply(declined: string[]): string {
31
+ return `Cancelled — you declined: ${declined.join('; ')}. Nothing was sent or changed.`;
32
+ }
33
+
34
+ /** Fed back to the model after a tool call it emitted could not be parsed. */
35
+ export function toolErrorFeedback(errors: ToolCallError[], cutOff: boolean): string {
36
+ if (cutOff) {
37
+ return 'Your tool call was cut off because the output got too long. Make ONE tool call at a time with only the required arguments, or answer from the results you already have.';
38
+ }
39
+ const detail = errors.map((e) => e.message).join('; ');
40
+ return `Your tool call could not be read (${detail}). Call the tool again with valid JSON arguments that match its schema, or ask the user for the missing values.`;
41
+ }
42
+
43
+ export interface FinalizeOptions {
44
+ /** Recompute BTC figures and relabel RGB balances. */
45
+ fixAmounts: boolean;
46
+ /** Replace answers carrying payment data no tool returned and the user never typed. */
47
+ guardPaymentData: boolean;
48
+ /** What the answer may legitimately quote: the user's messages and the tool results. */
49
+ sources: unknown[];
50
+ /** Tool results, for relabelling RGB balances. */
51
+ toolResults: unknown[];
52
+ aborted: boolean;
53
+ }
54
+
55
+ /** The text the user sees. */
56
+ export function finalizeAnswer(answer: Answer, opts: FinalizeOptions): string {
57
+ if (!answer.text) return opts.aborted ? '' : STOPPED_REPLY;
58
+ if (answer.source === 'engine') return answer.text;
59
+ let text = answer.text;
60
+ if (opts.fixAmounts) text = fixRgbBalanceUnits(fixSatsBtcConversions(text), opts.toolResults);
61
+ if (opts.guardPaymentData) {
62
+ const ungrounded = findUngroundedPaymentData(text, opts.sources);
63
+ if (ungrounded.length) text = ungroundedReply(ungrounded);
64
+ }
65
+ return text;
66
+ }