@kaleidorg/mind 0.1.0 → 0.3.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 +38 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +41 -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 +1 -0
- 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 +117 -0
- package/dist/funnel.d.ts.map +1 -0
- package/dist/funnel.js +195 -0
- package/dist/funnel.js.map +1 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -1
- package/dist/kaleidoswap/contract.d.ts +72 -0
- package/dist/kaleidoswap/contract.d.ts.map +1 -0
- package/dist/kaleidoswap/contract.js +125 -0
- package/dist/kaleidoswap/contract.js.map +1 -0
- 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/btc-map.d.ts +87 -0
- package/dist/knowledge/btc-map.d.ts.map +1 -0
- package/dist/knowledge/btc-map.js +365 -0
- package/dist/knowledge/btc-map.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/lsps1/contract.d.ts +55 -0
- package/dist/lsps1/contract.d.ts.map +1 -0
- package/dist/lsps1/contract.js +91 -0
- package/dist/lsps1/contract.js.map +1 -0
- package/dist/memory/store.d.ts +40 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +143 -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 +68 -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/kaleidoswap-atomic.d.ts +27 -0
- package/dist/recipe/kaleidoswap-atomic.d.ts.map +1 -0
- package/dist/recipe/kaleidoswap-atomic.js +111 -0
- package/dist/recipe/kaleidoswap-atomic.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 +106 -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/registry.d.ts.map +1 -1
- package/dist/skills/registry.js +20 -2
- package/dist/skills/registry.js.map +1 -1
- 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/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/confirm.d.ts +12 -0
- package/dist/wallet/confirm.d.ts.map +1 -0
- package/dist/wallet/confirm.js +67 -0
- package/dist/wallet/confirm.js.map +1 -0
- 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 +10 -5
- package/skills/README.md +6 -1
- package/skills/kaleido-lsps/SKILL.md +56 -0
- package/skills/kaleido-trading/SKILL.md +85 -18
- package/skills/merchant-finder/SKILL.md +87 -0
- package/skills/paid-data/SKILL.md +12 -0
- package/skills/wallet-assistant/SKILL.md +38 -0
- package/src/capabilities.ts +79 -0
- package/src/context/budget.ts +46 -0
- package/src/context/builder.ts +100 -0
- package/src/context/context.test.ts +87 -0
- package/src/engine.ts +6 -0
- 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 +285 -0
- package/src/index.ts +128 -0
- package/src/kaleidoswap/contract.test.ts +147 -0
- package/src/kaleidoswap/contract.ts +212 -0
- package/src/knowledge/bitcoin-copilot.ts +177 -0
- package/src/knowledge/btc-map.test.ts +188 -0
- package/src/knowledge/btc-map.ts +446 -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/lsps1/contract.test.ts +81 -0
- package/src/lsps1/contract.ts +132 -0
- package/src/memory/memory.test.ts +140 -0
- package/src/memory/store.ts +174 -0
- package/src/memory/tool.ts +76 -0
- package/src/memory/types.ts +76 -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/kaleidoswap-atomic.test.ts +138 -0
- package/src/recipe/kaleidoswap-atomic.ts +117 -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 +134 -0
- package/src/recipe/swap.ts +74 -0
- package/src/recipe/types.ts +76 -0
- package/src/skills/registry.ts +21 -2
- package/src/skills/skills.test.ts +42 -0
- package/src/tools/cli.test.ts +53 -0
- package/src/tools/cli.ts +98 -0
- package/src/tools/mcp.ts +3 -2
- package/src/wallet/confirm.test.ts +57 -0
- package/src/wallet/confirm.ts +74 -0
- package/src/wallet/contract.test.ts +89 -0
- package/src/wallet/contract.ts +157 -0
- package/skills/kaleido-wallet/SKILL.md +0 -28
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
export interface CapabilityInput {
|
|
10
|
+
/** Total device RAM in bytes (e.g. react-native-device-info getTotalMemory). */
|
|
11
|
+
ramBytes?: number;
|
|
12
|
+
/** The loaded model's context window in tokens (modelConfig.ctx_size). */
|
|
13
|
+
modelCtxTokens: number;
|
|
14
|
+
/** Whether an EmbeddingProvider is wired (QVAC embed, etc.). */
|
|
15
|
+
hasEmbeddings?: boolean;
|
|
16
|
+
/** Running inference on a remote provider (desktop/server) — relaxes limits. */
|
|
17
|
+
delegated?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface MindCapabilities {
|
|
20
|
+
/** Long-term memory (cheap — on unless the window is tiny). */
|
|
21
|
+
memory: boolean;
|
|
22
|
+
/** Semantic recall for memory (needs embeddings). */
|
|
23
|
+
semanticMemory: boolean;
|
|
24
|
+
/** Embedding-only dedup of near-duplicate memories (zero inference — mobile-safe). */
|
|
25
|
+
dedupeMemory: boolean;
|
|
26
|
+
/** LLM merge of near-duplicate memories (an extra inference — capable/delegated only). */
|
|
27
|
+
mergeMemory: 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
|
+
export declare function capabilityProfile(input: CapabilityInput): MindCapabilities;
|
|
38
|
+
//# sourceMappingURL=capabilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capabilities.d.ts","sourceRoot":"","sources":["../src/capabilities.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,MAAM,WAAW,eAAe;IAC9B,gFAAgF;IAChF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0EAA0E;IAC1E,cAAc,EAAE,MAAM,CAAC;IACvB,gEAAgE;IAChE,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,gFAAgF;IAChF,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,+DAA+D;IAC/D,MAAM,EAAE,OAAO,CAAC;IAChB,qDAAqD;IACrD,cAAc,EAAE,OAAO,CAAC;IACxB,sFAAsF;IACtF,YAAY,EAAE,OAAO,CAAC;IACtB,0FAA0F;IAC1F,WAAW,EAAE,OAAO,CAAC;IACrB,8EAA8E;IAC9E,GAAG,EAAE,OAAO,CAAC;IACb,gDAAgD;IAChD,mBAAmB,EAAE,MAAM,CAAC;IAC5B,4CAA4C;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,OAAO,EAAE,MAAM,CAAC;CACjB;AAID,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,eAAe,GAAG,gBAAgB,CAmC1E"}
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
import { contextBudgetTokens } from './context/budget.js';
|
|
10
|
+
const GiB = 1024 * 1024 * 1024;
|
|
11
|
+
export function capabilityProfile(input) {
|
|
12
|
+
const ramGb = input.delegated ? Infinity : (input.ramBytes ?? 0) / GiB;
|
|
13
|
+
const ctx = input.modelCtxTokens;
|
|
14
|
+
const hasEmb = !!input.hasEmbeddings;
|
|
15
|
+
const budget = contextBudgetTokens(ctx);
|
|
16
|
+
// Memory: on whenever there's room for a few lines. Semantic needs embeddings.
|
|
17
|
+
const memory = budget >= 256;
|
|
18
|
+
const semanticMemory = memory && hasEmb;
|
|
19
|
+
// Consolidation: embedding-only dedup is cheap (no inference) — on wherever
|
|
20
|
+
// semantic memory is. The LLM merge costs an extra inference, so reserve it
|
|
21
|
+
// for delegated or roomy on-device setups; never run it on a tiny phone model.
|
|
22
|
+
const dedupeMemory = semanticMemory;
|
|
23
|
+
const mergeMemory = dedupeMemory && (input.delegated || (ramGb >= 4 && ctx >= 4096));
|
|
24
|
+
// RAG is the expensive one: needs embeddings, a non-tiny context window, and
|
|
25
|
+
// (on-device) enough RAM to hold an embedding model + index.
|
|
26
|
+
const rag = hasEmb && ctx >= 4096 && (input.delegated || ramGb >= 3);
|
|
27
|
+
// Scale how much we pull in with the available window.
|
|
28
|
+
const topKMemory = budget >= 1500 ? 4 : budget >= 700 ? 3 : 2;
|
|
29
|
+
const topKRag = rag ? (budget >= 2500 ? 5 : 3) : 0;
|
|
30
|
+
return {
|
|
31
|
+
memory,
|
|
32
|
+
semanticMemory,
|
|
33
|
+
dedupeMemory,
|
|
34
|
+
mergeMemory,
|
|
35
|
+
rag,
|
|
36
|
+
contextBudgetTokens: budget,
|
|
37
|
+
topKMemory,
|
|
38
|
+
topKRag,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=capabilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capabilities.js","sourceRoot":"","sources":["../src/capabilities.ts"],"names":[],"m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C,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEnD,OAAO;QACL,MAAM;QACN,cAAc;QACd,YAAY;QACZ,WAAW;QACX,GAAG;QACH,mBAAmB,EAAE,MAAM;QAC3B,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
/** Rough token count for a string (no tokenizer — ~4 chars/token). */
|
|
12
|
+
export declare function estimateTokens(text: string): number;
|
|
13
|
+
/** Trim text to at most `maxTokens` (on a word boundary where possible). */
|
|
14
|
+
export declare function clampToTokens(text: string, maxTokens: number): string;
|
|
15
|
+
export interface BudgetReserves {
|
|
16
|
+
/** Tokens to leave for the model's reply. Default 512. */
|
|
17
|
+
output?: number;
|
|
18
|
+
/** Tokens to budget for tool schemas the engine sends. Default 600. */
|
|
19
|
+
tools?: number;
|
|
20
|
+
/** Tokens to budget for the running conversation (user + history). Default 768. */
|
|
21
|
+
conversation?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Tokens available for the *injected system context* (soul + instructions +
|
|
25
|
+
* skill + memory + RAG), given the model's context window and what else must
|
|
26
|
+
* fit. Never negative; clamped to a sane floor so something always gets in.
|
|
27
|
+
*/
|
|
28
|
+
export declare function contextBudgetTokens(ctxSize: number, reserves?: BudgetReserves): number;
|
|
29
|
+
//# sourceMappingURL=budget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.d.ts","sourceRoot":"","sources":["../../src/context/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,sEAAsE;AACtE,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,4EAA4E;AAC5E,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAMrE;AAED,MAAM,WAAW,cAAc;IAC7B,0DAA0D;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAmB,GAAG,MAAM,CAM1F"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
/** Rough token count for a string (no tokenizer — ~4 chars/token). */
|
|
12
|
+
export function estimateTokens(text) {
|
|
13
|
+
return Math.ceil(text.length / 4);
|
|
14
|
+
}
|
|
15
|
+
/** Trim text to at most `maxTokens` (on a word boundary where possible). */
|
|
16
|
+
export function clampToTokens(text, maxTokens) {
|
|
17
|
+
const maxChars = maxTokens * 4;
|
|
18
|
+
if (text.length <= maxChars)
|
|
19
|
+
return text;
|
|
20
|
+
const cut = text.slice(0, maxChars);
|
|
21
|
+
const sp = cut.lastIndexOf(' ');
|
|
22
|
+
return (sp > maxChars * 0.6 ? cut.slice(0, sp) : cut).trimEnd() + '…';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Tokens available for the *injected system context* (soul + instructions +
|
|
26
|
+
* skill + memory + RAG), given the model's context window and what else must
|
|
27
|
+
* fit. Never negative; clamped to a sane floor so something always gets in.
|
|
28
|
+
*/
|
|
29
|
+
export function contextBudgetTokens(ctxSize, reserves = {}) {
|
|
30
|
+
const output = reserves.output ?? 512;
|
|
31
|
+
const tools = reserves.tools ?? 600;
|
|
32
|
+
const conversation = reserves.conversation ?? 768;
|
|
33
|
+
const available = ctxSize - output - tools - conversation;
|
|
34
|
+
return Math.max(256, available);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sourceRoot":"","sources":["../../src/context/budget.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,sEAAsE;AACtE,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,SAAiB;IAC3D,MAAM,QAAQ,GAAG,SAAS,GAAG,CAAC,CAAC;IAC/B,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,IAAI,CAAC;IACzC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IACpC,MAAM,EAAE,GAAG,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,GAAG,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,GAAG,GAAG,CAAC;AACxE,CAAC;AAWD;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAe,EAAE,WAA2B,EAAE;IAChF,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,IAAI,GAAG,CAAC;IACtC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,IAAI,GAAG,CAAC;IACpC,MAAM,YAAY,GAAG,QAAQ,CAAC,YAAY,IAAI,GAAG,CAAC;IAClD,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,GAAG,YAAY,CAAC;IAC1D,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextBuilder — assembles the system prompt for a turn from, in priority
|
|
3
|
+
* order: the agent's identity (soul) → operating instructions → the active
|
|
4
|
+
* skill playbook → auto-recalled memories → auto-retrieved knowledge, trimmed
|
|
5
|
+
* to a token budget so it never overflows a small model.
|
|
6
|
+
*
|
|
7
|
+
* Memory and RAG can ALSO be tools (the model pulls them in itself). Use this
|
|
8
|
+
* for the always-on slice (identity + the most relevant memory/snippet); use
|
|
9
|
+
* the tools (`recall`, `search_knowledge`) for deeper, on-demand lookups.
|
|
10
|
+
*/
|
|
11
|
+
import type { AgentProfile, MemoryStore } from '../memory/types.js';
|
|
12
|
+
import type { Retriever } from '../rag/retriever.js';
|
|
13
|
+
export interface ContextBuilderOptions {
|
|
14
|
+
profile: AgentProfile;
|
|
15
|
+
/** Auto-recall relevant memories into context. */
|
|
16
|
+
memory?: MemoryStore;
|
|
17
|
+
/** Auto-retrieve relevant knowledge into context. */
|
|
18
|
+
retriever?: Retriever;
|
|
19
|
+
/** Max tokens for the whole assembled system prompt (see contextBudgetTokens). */
|
|
20
|
+
budgetTokens?: number;
|
|
21
|
+
/** Memories to recall (default 3). */
|
|
22
|
+
topKMemory?: number;
|
|
23
|
+
/** Knowledge chunks to retrieve (default 0 — prefer the search_knowledge tool). */
|
|
24
|
+
topKRag?: number;
|
|
25
|
+
}
|
|
26
|
+
export interface BuildInput {
|
|
27
|
+
/** The user's message — drives memory recall + retrieval. */
|
|
28
|
+
query: string;
|
|
29
|
+
/** A composed skill playbook to splice in (from SkillRegistry.compose). */
|
|
30
|
+
skillSystem?: string;
|
|
31
|
+
}
|
|
32
|
+
export declare class ContextBuilder {
|
|
33
|
+
private readonly opts;
|
|
34
|
+
constructor(opts: ContextBuilderOptions);
|
|
35
|
+
build(input: BuildInput): Promise<{
|
|
36
|
+
system: string;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/context/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGrD,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,YAAY,CAAC;IACtB,kDAAkD;IAClD,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,qDAAqD;IACrD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sCAAsC;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mFAAmF;IACnF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACzB,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,EAAE,qBAAqB;IAElD,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;CA4D5D"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ContextBuilder — assembles the system prompt for a turn from, in priority
|
|
3
|
+
* order: the agent's identity (soul) → operating instructions → the active
|
|
4
|
+
* skill playbook → auto-recalled memories → auto-retrieved knowledge, trimmed
|
|
5
|
+
* to a token budget so it never overflows a small model.
|
|
6
|
+
*
|
|
7
|
+
* Memory and RAG can ALSO be tools (the model pulls them in itself). Use this
|
|
8
|
+
* for the always-on slice (identity + the most relevant memory/snippet); use
|
|
9
|
+
* the tools (`recall`, `search_knowledge`) for deeper, on-demand lookups.
|
|
10
|
+
*/
|
|
11
|
+
import { clampToTokens, estimateTokens } from './budget.js';
|
|
12
|
+
export class ContextBuilder {
|
|
13
|
+
opts;
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
}
|
|
17
|
+
async build(input) {
|
|
18
|
+
const { profile } = this.opts;
|
|
19
|
+
const budget = this.opts.budgetTokens ?? 1024;
|
|
20
|
+
const sections = [];
|
|
21
|
+
let used = 0;
|
|
22
|
+
const add = (text, { force = false } = {}) => {
|
|
23
|
+
const t = text.trim();
|
|
24
|
+
if (!t)
|
|
25
|
+
return;
|
|
26
|
+
const cost = estimateTokens(t) + 1;
|
|
27
|
+
if (!force && used + cost > budget) {
|
|
28
|
+
// Try to fit a trimmed version of optional sections.
|
|
29
|
+
const room = budget - used - 1;
|
|
30
|
+
if (room < 40)
|
|
31
|
+
return;
|
|
32
|
+
const trimmed = clampToTokens(t, room);
|
|
33
|
+
sections.push(trimmed);
|
|
34
|
+
used += estimateTokens(trimmed) + 1;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
sections.push(t);
|
|
38
|
+
used += cost;
|
|
39
|
+
};
|
|
40
|
+
// 1. Identity (always) — name + soul.
|
|
41
|
+
add(`# ${profile.name}\n${profile.soul}`, { force: true });
|
|
42
|
+
// 2. Operating instructions (always, if any).
|
|
43
|
+
if (profile.instructions)
|
|
44
|
+
add(`## Instructions\n${profile.instructions}`, { force: true });
|
|
45
|
+
// 3. Active skill playbook.
|
|
46
|
+
if (input.skillSystem)
|
|
47
|
+
add(input.skillSystem);
|
|
48
|
+
// 4. Auto-recalled memory.
|
|
49
|
+
const kMem = this.opts.topKMemory ?? 3;
|
|
50
|
+
if (this.opts.memory && kMem > 0) {
|
|
51
|
+
try {
|
|
52
|
+
const mems = await this.opts.memory.search({ text: input.query, limit: kMem });
|
|
53
|
+
if (mems.length) {
|
|
54
|
+
add(`## What you remember\n${mems.map((m) => `- (${m.kind}) ${m.text}`).join('\n')}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
/* memory is best-effort */
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// 5. Auto-retrieved knowledge (opt-in; default prefers the tool).
|
|
62
|
+
const kRag = this.opts.topKRag ?? 0;
|
|
63
|
+
if (this.opts.retriever && kRag > 0) {
|
|
64
|
+
try {
|
|
65
|
+
const hits = await this.opts.retriever.search(input.query, kRag);
|
|
66
|
+
if (hits.length) {
|
|
67
|
+
add(`## Relevant context\n${hits.map((h) => h.text).join('\n\n')}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
/* retrieval is best-effort */
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { system: sections.join('\n\n') };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../src/context/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aA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MAAM,EAAE,CAAC;oBAChB,GAAG,CAAC,wBAAwB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBACtE,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,8BAA8B;YAChC,CAAC;QACH,CAAC;QAED,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;IAC3C,CAAC;CACF"}
|
package/dist/engine.d.ts
CHANGED
|
@@ -35,6 +35,15 @@ export interface AgenticOptions {
|
|
|
35
35
|
name: string;
|
|
36
36
|
arguments: Record<string, unknown>;
|
|
37
37
|
}, turn: number) => void;
|
|
38
|
+
/**
|
|
39
|
+
* Fired after a tool returns (success OR error — errors arrive as `{error}`).
|
|
40
|
+
* Useful for surfacing the raw response back to the user in a debug UI.
|
|
41
|
+
*/
|
|
42
|
+
onToolResult?: (event: {
|
|
43
|
+
name: string;
|
|
44
|
+
arguments: Record<string, unknown>;
|
|
45
|
+
result: unknown;
|
|
46
|
+
}, turn: number) => void;
|
|
38
47
|
/** Human-in-the-loop gate for tools flagged requiresConfirmation. */
|
|
39
48
|
onConfirm?: (call: {
|
|
40
49
|
name: string;
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,WAAW,CAAC;IACtB,KAAK,EA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
|
79
79
|
}
|
|
80
80
|
executed.push({ name: call.name, arguments: call.arguments, result });
|
|
81
|
+
opts.onToolResult?.({ name: call.name, arguments: call.arguments, result }, turn);
|
|
81
82
|
history.push({
|
|
82
83
|
role: 'tool',
|
|
83
84
|
content: typeof result === 'string' ? result : JSON.stringify(result),
|
package/dist/engine.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAiDH,MAAM,OAAO,MAAM;IACA,QAAQ,CAAc;IACtB,QAAQ,CAAe;IACvB,aAAa,CAAU;IACvB,eAAe,CAAS;IAEzC,YAAY,IAAmB;QAC7B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,IAAI,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,QAAmB,EAAE,OAAuB,EAAE;QAC7D,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,eAAe,CAAC;QACvD,MAAM,SAAS,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;QAC5D,MAAM,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC;QAE1D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAC7B,MAAM,OAAO,GAAc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OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,EAAE,KAAK,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QACrE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tier-0 deterministic fast-path — answer the common, unambiguous wallet asks
|
|
3
|
+
* with NO LLM at all. "balance", "receive address", "btc price" map straight to
|
|
4
|
+
* a single tool call. The model is reserved for genuine ambiguity.
|
|
5
|
+
*
|
|
6
|
+
* This is the biggest mobile UX lever: ~60-80% of wallet requests are simple,
|
|
7
|
+
* and our eval showed tiny models are slow + weak at args — so skip them here.
|
|
8
|
+
*
|
|
9
|
+
* Matchers are intentionally CONSERVATIVE: when in doubt, return null and let
|
|
10
|
+
* the recipe / agentic loop handle it. Under-firing is fine; mis-firing is not.
|
|
11
|
+
*
|
|
12
|
+
* Pure data — no deps. The host executes the returned tool + renders the result.
|
|
13
|
+
*/
|
|
14
|
+
export interface FastIntent {
|
|
15
|
+
name: string;
|
|
16
|
+
/** Contract tool to call when this intent matches. */
|
|
17
|
+
tool: string;
|
|
18
|
+
/** True only when this intent UNAMBIGUOUSLY matches the text. */
|
|
19
|
+
match: (text: string) => boolean;
|
|
20
|
+
/** Optional args derived from the text (default: none). */
|
|
21
|
+
args?: (text: string) => Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
export interface FastHit {
|
|
24
|
+
intent: FastIntent;
|
|
25
|
+
tool: string;
|
|
26
|
+
args: Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
export declare class FastPath {
|
|
29
|
+
private intents;
|
|
30
|
+
constructor(intents?: FastIntent[]);
|
|
31
|
+
add(intent: FastIntent): void;
|
|
32
|
+
list(): FastIntent[];
|
|
33
|
+
/** The first unambiguously-matching intent, or null. */
|
|
34
|
+
select(text: string): FastHit | null;
|
|
35
|
+
}
|
|
36
|
+
/** Default wallet read intents (balance / receive address / price). */
|
|
37
|
+
export declare const WALLET_FAST_INTENTS: FastIntent[];
|
|
38
|
+
//# sourceMappingURL=fastpath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fastpath.d.ts","sourceRoot":"","sources":["../../src/fastpath/fastpath.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,iEAAiE;IACjE,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC;IACjC,2DAA2D;IAC3D,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClD;AAED,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC/B;AAED,qBAAa,QAAQ;IACnB,OAAO,CAAC,OAAO,CAAe;gBAClB,OAAO,GAAE,UAAU,EAAO;IAGtC,GAAG,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAG7B,IAAI,IAAI,UAAU,EAAE;IAGpB,wDAAwD;IACxD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;CAIrC;AAMD,uEAAuE;AACvE,eAAO,MAAM,mBAAmB,EAAE,UAAU,EAgB3C,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tier-0 deterministic fast-path — answer the common, unambiguous wallet asks
|
|
3
|
+
* with NO LLM at all. "balance", "receive address", "btc price" map straight to
|
|
4
|
+
* a single tool call. The model is reserved for genuine ambiguity.
|
|
5
|
+
*
|
|
6
|
+
* This is the biggest mobile UX lever: ~60-80% of wallet requests are simple,
|
|
7
|
+
* and our eval showed tiny models are slow + weak at args — so skip them here.
|
|
8
|
+
*
|
|
9
|
+
* Matchers are intentionally CONSERVATIVE: when in doubt, return null and let
|
|
10
|
+
* the recipe / agentic loop handle it. Under-firing is fine; mis-firing is not.
|
|
11
|
+
*
|
|
12
|
+
* Pure data — no deps. The host executes the returned tool + renders the result.
|
|
13
|
+
*/
|
|
14
|
+
export class FastPath {
|
|
15
|
+
intents;
|
|
16
|
+
constructor(intents = []) {
|
|
17
|
+
this.intents = [...intents];
|
|
18
|
+
}
|
|
19
|
+
add(intent) {
|
|
20
|
+
this.intents.push(intent);
|
|
21
|
+
}
|
|
22
|
+
list() {
|
|
23
|
+
return [...this.intents];
|
|
24
|
+
}
|
|
25
|
+
/** The first unambiguously-matching intent, or null. */
|
|
26
|
+
select(text) {
|
|
27
|
+
const intent = this.intents.find((i) => i.match(text));
|
|
28
|
+
return intent ? { intent, tool: intent.tool, args: intent.args?.(text) ?? {} } : null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// A "spend or compound" guard — never fast-path anything that moves money or
|
|
32
|
+
// chains another action ("send", "pay", "and then", "swap").
|
|
33
|
+
const ACTIONY = /\b(send|pay|transfer|swap|buy|sell|then|after that)\b/i;
|
|
34
|
+
/** Default wallet read intents (balance / receive address / price). */
|
|
35
|
+
export const WALLET_FAST_INTENTS = [
|
|
36
|
+
{
|
|
37
|
+
name: 'balance',
|
|
38
|
+
tool: 'get_balances',
|
|
39
|
+
match: (t) => !ACTIONY.test(t) && /\b(balance|funds|how much (do i|have i|i have)|how much.* (do i have|in my wallet))\b/i.test(t),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'address',
|
|
43
|
+
tool: 'spark_get_address',
|
|
44
|
+
match: (t) => !ACTIONY.test(t) && /\b(receive address|deposit address|my address|an address|get .*address|where.* receive)\b/i.test(t),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'price',
|
|
48
|
+
tool: 'get_price',
|
|
49
|
+
match: (t) => !ACTIONY.test(t) && /\b(btc price|bitcoin price|price of (btc|bitcoin)|how much is (a |one )?(btc|bitcoin))\b/i.test(t),
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
//# sourceMappingURL=fastpath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fastpath.js","sourceRoot":"","sources":["../../src/fastpath/fastpath.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAkBH,MAAM,OAAO,QAAQ;IACX,OAAO,CAAe;IAC9B,YAAY,UAAwB,EAAE;QACpC,IAAI,CAAC,OAAO,GAAG,CAAC,GAAG,OAAO,CAAC,CAAC;IAC9B,CAAC;IACD,GAAG,CAAC,MAAkB;QACpB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IACD,IAAI;QACF,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IACD,wDAAwD;IACxD,MAAM,CAAC,IAAY;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;QACvD,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACxF,CAAC;CACF;AAED,6EAA6E;AAC7E,6DAA6D;AAC7D,MAAM,OAAO,GAAG,wDAAwD,CAAC;AAEzE,uEAAuE;AACvE,MAAM,CAAC,MAAM,mBAAmB,GAAiB;IAC/C;QACE,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,cAAc;QACpB,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,wFAAwF,CAAC,IAAI,CAAC,CAAC,CAAC;KACnI;IACD;QACE,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,mBAAmB;QACzB,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,4FAA4F,CAAC,IAAI,CAAC,CAAC,CAAC;KACvI;IACD;QACE,IAAI,EAAE,OAAO;QACb,IAAI,EAAE,WAAW;QACjB,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,2FAA2F,CAAC,IAAI,CAAC,CAAC,CAAC;KACtI;CACF,CAAC"}
|
package/dist/funnel.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Funnel — the tiered agent loop (T0 fast-path → T2 recipe → T1 agentic).
|
|
3
|
+
*
|
|
4
|
+
* This is the mobile-optimized funnel from the roadmap, lifted out of the
|
|
5
|
+
* hosts so every surface (rate chat + voice, desktop provider, agent) runs
|
|
6
|
+
* the SAME routing:
|
|
7
|
+
*
|
|
8
|
+
* request
|
|
9
|
+
* ├─ T0 deterministic fast-path (no LLM) balance / address / price
|
|
10
|
+
* ├─ T2 recipe multi-step (~1 inference) "pay bob 3 EUR"
|
|
11
|
+
* └─ T1 skill-scoped agentic loop everything else
|
|
12
|
+
*
|
|
13
|
+
* Hosts inject the provider, the tool registry, and a `getSettings` closure
|
|
14
|
+
* read fresh each turn — so user-tunable settings (persona, history length,
|
|
15
|
+
* memory/RAG toggles, disabled skills) never require rebuilding the funnel
|
|
16
|
+
* or dropping host state like an embedded RAG index.
|
|
17
|
+
*
|
|
18
|
+
* Safety is unchanged from the Engine: spend tools are confirmation-gated by
|
|
19
|
+
* the contract; with no `onConfirm` the gate fails closed.
|
|
20
|
+
*/
|
|
21
|
+
import type { ToolRegistry } from './tools/registry.js';
|
|
22
|
+
import type { FastIntent } from './fastpath/fastpath.js';
|
|
23
|
+
import type { Recipe } from './recipe/types.js';
|
|
24
|
+
import type { Skill } from './skills/types.js';
|
|
25
|
+
import type { LLMProvider } from './providers/types.js';
|
|
26
|
+
import type { ConfirmDecision, Message, ToolResult } from './types.js';
|
|
27
|
+
/** Base system prompt for the wallet assistant. Hosts may override. */
|
|
28
|
+
export declare const DEFAULT_WALLET_SYSTEM: string;
|
|
29
|
+
/** Per-user agent settings, read fresh each turn via `getSettings`. */
|
|
30
|
+
export interface FunnelSettings {
|
|
31
|
+
/** Extra instructions appended to the system prompt. */
|
|
32
|
+
persona?: string;
|
|
33
|
+
/** Most recent history messages to keep in the prompt (default 8). */
|
|
34
|
+
historyLength?: number;
|
|
35
|
+
/** Expose the remember/recall tools (default true). */
|
|
36
|
+
memoryEnabled?: boolean;
|
|
37
|
+
/** Expose the search_knowledge tool (default true). */
|
|
38
|
+
ragEnabled?: boolean;
|
|
39
|
+
/** Skill names the user turned off. */
|
|
40
|
+
disabledSkills?: string[];
|
|
41
|
+
}
|
|
42
|
+
export interface FunnelCallbacks {
|
|
43
|
+
history?: Message[];
|
|
44
|
+
/** The live requestId of the agentic run (so a stop button can cancel it). */
|
|
45
|
+
onStart?: (requestId: string) => void;
|
|
46
|
+
onToken?: (token: string, turn: number) => void;
|
|
47
|
+
/** A recipe step is executing (deterministic tier). */
|
|
48
|
+
onStep?: (name: string) => void;
|
|
49
|
+
/** The model requested a tool (agentic tier), before it executes. */
|
|
50
|
+
onToolCall?: (call: {
|
|
51
|
+
name: string;
|
|
52
|
+
arguments: Record<string, unknown>;
|
|
53
|
+
}, info: {
|
|
54
|
+
requiresConfirmation: boolean;
|
|
55
|
+
}) => void;
|
|
56
|
+
/** A tool returned a result (agentic tier). Errors arrive as `{error}`. */
|
|
57
|
+
onToolResult?: (event: {
|
|
58
|
+
name: string;
|
|
59
|
+
arguments: Record<string, unknown>;
|
|
60
|
+
result: unknown;
|
|
61
|
+
}) => void;
|
|
62
|
+
onConfirm?: (call: {
|
|
63
|
+
name: string;
|
|
64
|
+
arguments: Record<string, unknown>;
|
|
65
|
+
}) => Promise<ConfirmDecision>;
|
|
66
|
+
}
|
|
67
|
+
export interface FunnelResult {
|
|
68
|
+
text: string;
|
|
69
|
+
tier: 'fast' | 'recipe' | 'agentic';
|
|
70
|
+
/** Fast tier only: the matched intent + raw tool result (e.g. for a balance card). */
|
|
71
|
+
intent?: string;
|
|
72
|
+
data?: unknown;
|
|
73
|
+
/** Agentic tier only: executed tool calls + reasoning turns. */
|
|
74
|
+
toolCalls?: ToolResult[];
|
|
75
|
+
turns?: number;
|
|
76
|
+
}
|
|
77
|
+
export interface FunnelOptions {
|
|
78
|
+
provider: LLMProvider;
|
|
79
|
+
/** ALL tool sources merged — wallet, memory, RAG, merchant, L402, … */
|
|
80
|
+
tools: ToolRegistry;
|
|
81
|
+
/** Skills available to the agentic tier (disabled ones filtered per turn). */
|
|
82
|
+
skills?: Skill[];
|
|
83
|
+
/** Recipes for the T2 tier. Default: asset-send, payments, receive. */
|
|
84
|
+
recipes?: Recipe[];
|
|
85
|
+
/** Fast-path intents for the T0 tier. Default: WALLET_FAST_INTENTS. */
|
|
86
|
+
fastIntents?: FastIntent[];
|
|
87
|
+
/** Base system prompt (persona is appended). Default: DEFAULT_WALLET_SYSTEM. */
|
|
88
|
+
system?: string;
|
|
89
|
+
/** Max reasoning↔tool rounds in the agentic tier. Default 5. */
|
|
90
|
+
maxTurns?: number;
|
|
91
|
+
/** User settings, read fresh each turn. */
|
|
92
|
+
getSettings?: () => FunnelSettings;
|
|
93
|
+
/** Render a fast-path tool result as user-facing text. Default: built-in. */
|
|
94
|
+
renderFast?: (intent: string, result: unknown) => string;
|
|
95
|
+
/** Diagnostics sink (tier routing, tool calls). Default: silent. */
|
|
96
|
+
log?: (message: string) => void;
|
|
97
|
+
}
|
|
98
|
+
export declare class Funnel {
|
|
99
|
+
private readonly provider;
|
|
100
|
+
private readonly registry;
|
|
101
|
+
private readonly engine;
|
|
102
|
+
private readonly fastPath;
|
|
103
|
+
private readonly recipes;
|
|
104
|
+
private readonly allSkills;
|
|
105
|
+
private readonly system;
|
|
106
|
+
private readonly getSettings;
|
|
107
|
+
private readonly renderFast;
|
|
108
|
+
private readonly log;
|
|
109
|
+
/** Skill registry, rebuilt only when the disabled-skills set changes. */
|
|
110
|
+
private skillsCache;
|
|
111
|
+
constructor(opts: FunnelOptions);
|
|
112
|
+
/** Skills currently enabled (e.g. for a skills sheet). */
|
|
113
|
+
listSkills(): Skill[];
|
|
114
|
+
private skillsFor;
|
|
115
|
+
runTurn(text: string, cbs?: FunnelCallbacks): Promise<FunnelResult>;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=funnel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"funnel.d.ts","sourceRoot":"","sources":["../src/funnel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAKzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEvE,uEAAuE;AACvE,eAAO,MAAM,qBAAqB,QAwBtB,CAAC;AAQb,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC7B,wDAAwD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