@kaleidorg/mind 0.5.1 → 0.6.1

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 (174) hide show
  1. package/dist/autonomy/index.d.ts +21 -0
  2. package/dist/autonomy/index.d.ts.map +1 -0
  3. package/dist/autonomy/index.js +16 -0
  4. package/dist/autonomy/index.js.map +1 -0
  5. package/dist/autonomy/prompt.d.ts +21 -0
  6. package/dist/autonomy/prompt.d.ts.map +1 -0
  7. package/dist/autonomy/prompt.js +37 -0
  8. package/dist/autonomy/prompt.js.map +1 -0
  9. package/dist/autonomy/risk.d.ts +53 -0
  10. package/dist/autonomy/risk.d.ts.map +1 -0
  11. package/dist/autonomy/risk.js +74 -0
  12. package/dist/autonomy/risk.js.map +1 -0
  13. package/dist/autonomy/run-state.d.ts +39 -0
  14. package/dist/autonomy/run-state.d.ts.map +1 -0
  15. package/dist/autonomy/run-state.js +118 -0
  16. package/dist/autonomy/run-state.js.map +1 -0
  17. package/dist/autonomy/scheduler.d.ts +18 -0
  18. package/dist/autonomy/scheduler.d.ts.map +1 -0
  19. package/dist/autonomy/scheduler.js +113 -0
  20. package/dist/autonomy/scheduler.js.map +1 -0
  21. package/dist/autonomy/task-store.d.ts +44 -0
  22. package/dist/autonomy/task-store.d.ts.map +1 -0
  23. package/dist/autonomy/task-store.js +139 -0
  24. package/dist/autonomy/task-store.js.map +1 -0
  25. package/dist/autonomy/types.d.ts +164 -0
  26. package/dist/autonomy/types.d.ts.map +1 -0
  27. package/dist/autonomy/types.js +20 -0
  28. package/dist/autonomy/types.js.map +1 -0
  29. package/dist/bitrefill/contract.d.ts +60 -0
  30. package/dist/bitrefill/contract.d.ts.map +1 -0
  31. package/dist/bitrefill/contract.js +119 -0
  32. package/dist/bitrefill/contract.js.map +1 -0
  33. package/dist/context/compress.d.ts +65 -0
  34. package/dist/context/compress.d.ts.map +1 -0
  35. package/dist/context/compress.js +181 -0
  36. package/dist/context/compress.js.map +1 -0
  37. package/dist/engine.d.ts +20 -0
  38. package/dist/engine.d.ts.map +1 -1
  39. package/dist/engine.js +23 -4
  40. package/dist/engine.js.map +1 -1
  41. package/dist/evidence.d.ts +62 -0
  42. package/dist/evidence.d.ts.map +1 -0
  43. package/dist/evidence.js +47 -0
  44. package/dist/evidence.js.map +1 -0
  45. package/dist/flashnet/contract.d.ts +56 -0
  46. package/dist/flashnet/contract.d.ts.map +1 -0
  47. package/dist/flashnet/contract.js +100 -0
  48. package/dist/flashnet/contract.js.map +1 -0
  49. package/dist/funnel.d.ts +11 -0
  50. package/dist/funnel.d.ts.map +1 -1
  51. package/dist/funnel.js +62 -7
  52. package/dist/funnel.js.map +1 -1
  53. package/dist/index.d.ts +12 -1
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +11 -0
  56. package/dist/index.js.map +1 -1
  57. package/dist/kaleidoswap/contract.js +1 -1
  58. package/dist/kaleidoswap/contract.js.map +1 -1
  59. package/dist/knowledge/bitcoin-copilot.d.ts.map +1 -1
  60. package/dist/knowledge/bitcoin-copilot.js +85 -2
  61. package/dist/knowledge/bitcoin-copilot.js.map +1 -1
  62. package/dist/providers/types.d.ts +17 -0
  63. package/dist/providers/types.d.ts.map +1 -1
  64. package/dist/qvac/index.d.ts +1 -1
  65. package/dist/qvac/index.d.ts.map +1 -1
  66. package/dist/qvac/index.js.map +1 -1
  67. package/dist/qvac/parse.d.ts +18 -0
  68. package/dist/qvac/parse.d.ts.map +1 -1
  69. package/dist/qvac/parse.js +1 -0
  70. package/dist/qvac/parse.js.map +1 -1
  71. package/dist/qvac/provider.d.ts +16 -0
  72. package/dist/qvac/provider.d.ts.map +1 -1
  73. package/dist/qvac/provider.js +40 -1
  74. package/dist/qvac/provider.js.map +1 -1
  75. package/dist/qvac/stream.d.ts +22 -0
  76. package/dist/qvac/stream.d.ts.map +1 -1
  77. package/dist/qvac/stream.js +33 -1
  78. package/dist/qvac/stream.js.map +1 -1
  79. package/dist/recipe/buy-asset-channel.d.ts +1 -1
  80. package/dist/recipe/buy-asset-channel.d.ts.map +1 -1
  81. package/dist/recipe/buy-asset-channel.js +4 -3
  82. package/dist/recipe/buy-asset-channel.js.map +1 -1
  83. package/dist/recipe/flashnet-swap.d.ts +35 -0
  84. package/dist/recipe/flashnet-swap.d.ts.map +1 -0
  85. package/dist/recipe/flashnet-swap.js +239 -0
  86. package/dist/recipe/flashnet-swap.js.map +1 -0
  87. package/dist/recipe/kaleidoswap-atomic.d.ts +1 -1
  88. package/dist/recipe/kaleidoswap-atomic.d.ts.map +1 -1
  89. package/dist/recipe/kaleidoswap-atomic.js +42 -20
  90. package/dist/recipe/kaleidoswap-atomic.js.map +1 -1
  91. package/dist/recipe/kaleidoswap-channel-order.d.ts.map +1 -1
  92. package/dist/recipe/kaleidoswap-channel-order.js +31 -10
  93. package/dist/recipe/kaleidoswap-channel-order.js.map +1 -1
  94. package/dist/recipe/kaleidoswap-price.d.ts.map +1 -1
  95. package/dist/recipe/kaleidoswap-price.js +7 -1
  96. package/dist/recipe/kaleidoswap-price.js.map +1 -1
  97. package/dist/recipe/runner.d.ts.map +1 -1
  98. package/dist/recipe/runner.js +43 -3
  99. package/dist/recipe/runner.js.map +1 -1
  100. package/dist/recipe/swap.d.ts.map +1 -1
  101. package/dist/recipe/swap.js +14 -1
  102. package/dist/recipe/swap.js.map +1 -1
  103. package/dist/tools/mcp.d.ts +19 -0
  104. package/dist/tools/mcp.d.ts.map +1 -1
  105. package/dist/tools/mcp.js +51 -9
  106. package/dist/tools/mcp.js.map +1 -1
  107. package/dist/wallet/confirm.d.ts.map +1 -1
  108. package/dist/wallet/confirm.js +1 -0
  109. package/dist/wallet/confirm.js.map +1 -1
  110. package/dist/wallet/contract.d.ts.map +1 -1
  111. package/dist/wallet/contract.js +20 -4
  112. package/dist/wallet/contract.js.map +1 -1
  113. package/package.json +5 -4
  114. package/skills/bitrefill/SKILL.md +152 -52
  115. package/skills/channel-manager/SKILL.md +59 -0
  116. package/skills/dca/SKILL.md +48 -0
  117. package/skills/flashnet-swaps/SKILL.md +158 -0
  118. package/skills/kaleido-lsps/SKILL.md +34 -17
  119. package/skills/kaleido-trading/SKILL.md +37 -13
  120. package/skills/liquidity-optimizer/SKILL.md +91 -0
  121. package/skills/merchant-finder/SKILL.md +2 -2
  122. package/skills/portfolio-manager/SKILL.md +67 -0
  123. package/skills/rgb-lightning-node/SKILL.md +38 -11
  124. package/skills/spark-wallet/SKILL.md +235 -0
  125. package/skills/wallet-assistant/SKILL.md +2 -2
  126. package/src/autonomy/autonomy.test.ts +348 -0
  127. package/src/autonomy/index.ts +50 -0
  128. package/src/autonomy/prompt.ts +48 -0
  129. package/src/autonomy/risk.ts +139 -0
  130. package/src/autonomy/run-state.ts +144 -0
  131. package/src/autonomy/scheduler.ts +120 -0
  132. package/src/autonomy/task-store.ts +167 -0
  133. package/src/autonomy/types.ts +186 -0
  134. package/src/bitrefill/contract.test.ts +89 -0
  135. package/src/bitrefill/contract.ts +190 -0
  136. package/src/context/compress.test.ts +120 -0
  137. package/src/context/compress.ts +230 -0
  138. package/src/engine.test.ts +34 -0
  139. package/src/engine.ts +35 -4
  140. package/src/evidence.test.ts +80 -0
  141. package/src/evidence.ts +114 -0
  142. package/src/flashnet/contract.test.ts +101 -0
  143. package/src/flashnet/contract.ts +164 -0
  144. package/src/funnel.mind.test.ts +390 -0
  145. package/src/funnel.ts +73 -8
  146. package/src/index.ts +92 -1
  147. package/src/kaleidoswap/contract.ts +1 -1
  148. package/src/knowledge/bitcoin-copilot.ts +96 -2
  149. package/src/providers/types.ts +18 -0
  150. package/src/qvac/index.ts +1 -0
  151. package/src/qvac/parse.ts +20 -0
  152. package/src/qvac/provider.test.ts +17 -0
  153. package/src/qvac/provider.ts +62 -2
  154. package/src/qvac/stream.test.ts +36 -0
  155. package/src/qvac/stream.ts +54 -1
  156. package/src/recipe/buy-asset-channel.test.ts +5 -0
  157. package/src/recipe/buy-asset-channel.ts +6 -3
  158. package/src/recipe/flashnet-swap.test.ts +114 -0
  159. package/src/recipe/flashnet-swap.ts +266 -0
  160. package/src/recipe/kaleidoswap-atomic.test.ts +24 -3
  161. package/src/recipe/kaleidoswap-atomic.ts +39 -20
  162. package/src/recipe/kaleidoswap-channel-order.test.ts +38 -0
  163. package/src/recipe/kaleidoswap-channel-order.ts +27 -9
  164. package/src/recipe/kaleidoswap-price.ts +7 -1
  165. package/src/recipe/recipe.test.ts +21 -0
  166. package/src/recipe/runner.ts +46 -3
  167. package/src/recipe/swap.ts +16 -1
  168. package/src/tools/mcp.live.test.ts +116 -0
  169. package/src/tools/mcp.parse.test.ts +37 -0
  170. package/src/tools/mcp.ts +55 -9
  171. package/src/wallet/confirm.test.ts +8 -0
  172. package/src/wallet/confirm.ts +1 -0
  173. package/src/wallet/contract.test.ts +10 -0
  174. package/src/wallet/contract.ts +26 -4
@@ -0,0 +1,139 @@
1
+ /**
2
+ * TaskStore implementation — in-memory, with optional injected persistence.
3
+ * Pure TS, zero deps. Port of kaleidoagent's tasks-store.ts, but storage-agnostic
4
+ * (host injects IO: fs on a Node sidecar, SQLite on the desktop, AsyncStorage on RN).
5
+ *
6
+ * const store = new InMemoryTaskStore(); // ephemeral
7
+ * const store = new InMemoryTaskStore({ io }); // persisted
8
+ */
9
+ import { ZERO_ALLOCATION, } from './types.js';
10
+ export class InMemoryTaskStore {
11
+ tasks = [];
12
+ hydrated = false;
13
+ counter = 0;
14
+ io;
15
+ now;
16
+ constructor(opts = {}) {
17
+ this.io = opts.io;
18
+ this.now = opts.now ?? (() => Date.now());
19
+ }
20
+ async hydrate() {
21
+ if (this.hydrated)
22
+ return;
23
+ this.hydrated = true;
24
+ if (this.io) {
25
+ try {
26
+ this.tasks = await this.io.load();
27
+ this.counter = this.tasks.length;
28
+ }
29
+ catch {
30
+ this.tasks = [];
31
+ }
32
+ }
33
+ }
34
+ async persist() {
35
+ if (this.io)
36
+ await this.io.save(this.tasks);
37
+ }
38
+ async list() {
39
+ await this.hydrate();
40
+ return [...this.tasks];
41
+ }
42
+ async get(id) {
43
+ await this.hydrate();
44
+ return this.tasks.find((t) => t.id === id) ?? null;
45
+ }
46
+ async create(input) {
47
+ await this.hydrate();
48
+ const task = this.materialize(input);
49
+ this.tasks.push(task);
50
+ await this.persist();
51
+ return task;
52
+ }
53
+ async update(id, patch) {
54
+ await this.hydrate();
55
+ const existing = this.tasks.find((t) => t.id === id);
56
+ if (!existing)
57
+ return null;
58
+ // id + createdAt are immutable; strip them defensively even if cast in.
59
+ const { id: _id, createdAt: _createdAt, ...safe } = patch;
60
+ const updated = { ...existing, ...safe };
61
+ this.tasks = this.tasks.map((t) => (t.id === id ? updated : t));
62
+ await this.persist();
63
+ return updated;
64
+ }
65
+ async remove(id) {
66
+ await this.hydrate();
67
+ const before = this.tasks.length;
68
+ this.tasks = this.tasks.filter((t) => t.id !== id);
69
+ const removed = this.tasks.length < before;
70
+ if (removed)
71
+ await this.persist();
72
+ return removed;
73
+ }
74
+ async seedDefaults(seeds) {
75
+ await this.hydrate();
76
+ const present = new Set(this.tasks.map((t) => t.id));
77
+ const added = seeds
78
+ .filter((s) => !present.has(s.id))
79
+ .map((s) => this.materialize(s));
80
+ if (added.length) {
81
+ // Seeds first, so the default loops sort to the top of the list.
82
+ this.tasks = [...added, ...this.tasks];
83
+ await this.persist();
84
+ }
85
+ return added;
86
+ }
87
+ /** Fill defaults + assign id/createdAt for a new or seed task. */
88
+ materialize(input) {
89
+ return {
90
+ id: input.id ?? `task_${this.now()}_${++this.counter}`,
91
+ name: input.name,
92
+ description: input.description,
93
+ skill: input.skill,
94
+ scheduleSec: input.scheduleSec,
95
+ runOnStartup: input.runOnStartup ?? false,
96
+ allocation: input.allocation ?? { ...ZERO_ALLOCATION },
97
+ enabled: input.enabled,
98
+ createdAt: input.createdAt ?? this.now(),
99
+ lastRunAt: input.lastRunAt ?? null,
100
+ };
101
+ }
102
+ }
103
+ /**
104
+ * The three default loops nanobot seeded — ready to pass to `seedDefaults`.
105
+ * Disabled by default: a wallet agent must be turned on deliberately, never
106
+ * auto-arm itself on first launch.
107
+ */
108
+ export function defaultTaskSeeds(opts = {}) {
109
+ return [
110
+ {
111
+ id: 'heartbeat',
112
+ name: 'Heartbeat',
113
+ description: 'Node health check, channel audit, RGB transfer flush',
114
+ skill: 'channel-manager',
115
+ scheduleSec: opts.heartbeatSec ?? 300,
116
+ runOnStartup: true,
117
+ enabled: false,
118
+ },
119
+ {
120
+ id: 'rebalance',
121
+ name: 'Portfolio Rebalance',
122
+ description: 'Detect allocation drift and execute rebalancing swaps',
123
+ skill: 'portfolio-manager',
124
+ scheduleSec: opts.rebalanceSec ?? 3600,
125
+ runOnStartup: false,
126
+ enabled: false,
127
+ },
128
+ {
129
+ id: 'daily_summary',
130
+ name: 'Daily Summary',
131
+ description: 'Full portfolio snapshot and market report',
132
+ skill: 'portfolio-manager',
133
+ scheduleSec: opts.dailySummarySec ?? 86_400,
134
+ runOnStartup: false,
135
+ enabled: false,
136
+ },
137
+ ];
138
+ }
139
+ //# sourceMappingURL=task-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task-store.js","sourceRoot":"","sources":["../../src/autonomy/task-store.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,eAAe,GAMhB,MAAM,YAAY,CAAC;AASpB,MAAM,OAAO,iBAAiB;IACpB,KAAK,GAAgB,EAAE,CAAC;IACxB,QAAQ,GAAG,KAAK,CAAC;IACjB,OAAO,GAAG,CAAC,CAAC;IACH,EAAE,CAAe;IACjB,GAAG,CAAe;IAEnC,YAAY,OAAyB,EAAE;QACrC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;QAClB,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5C,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO;QAC1B,IAA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W;YACf,IAAI,EAAE,WAAW;YACjB,WAAW,EAAE,sDAAsD;YACnE,KAAK,EAAE,iBAAiB;YACxB,WAAW,EAAE,IAAI,CAAC,YAAY,IAAI,GAAG;YACrC,YAAY,EAAE,IAAI;YAClB,OAAO,EAAE,KAAK;SACf;QACD;YACE,EAAE,EAAE,WAAW;YACf,IAAI,EAAE,qBAAqB;YAC3B,WAAW,EAAE,uDAAuD;YACpE,KAAK,EAAE,mBAAmB;YAC1B,WAAW,EAAE,IAAI,CAAC,YAAY,IAAI,IAAI;YACtC,YAAY,EAAE,KAAK;YACnB,OAAO,EAAE,KAAK;SACf;QACD;YACE,EAAE,EAAE,eAAe;YACnB,IAAI,EAAE,eAAe;YACrB,WAAW,EAAE,2CAA2C;YACxD,KAAK,EAAE,mBAAmB;YAC1B,WAAW,EAAE,IAAI,CAAC,eAAe,IAAI,MAAM;YAC3C,YAAY,EAAE,KAAK;YACnB,OAAO,EAAE,KAAK;SACf;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Autonomy — the agent's task brain.
3
+ *
4
+ * This is the half of "the agent's memory" the {@link ../memory/types MemoryStore}
5
+ * (soul + facts) does NOT cover: the *operational* state nanobot kept across
6
+ * `tasks.json` + `cron/jobs.json` + its run history. Lifted into core so every
7
+ * host (desktop sidecar, agent, cli) runs the same autonomous loop — storage
8
+ * and timers are injected (fs/SQLite on Node, AsyncStorage on RN), the logic is
9
+ * pure TS with zero runtime deps.
10
+ *
11
+ * Three pieces:
12
+ * - TaskStore — the registry of scheduled/manual tasks (was tasks-store.ts)
13
+ * - TaskRunLog — what each task did, when, and what it cost (was agent-state.ts)
14
+ * - Scheduler — fires due tasks on their interval (was nanobot cron)
15
+ *
16
+ * Spend safety lives alongside in {@link ./risk}.
17
+ */
18
+ /** Capital earmarked for a task — isolates how much a single task may touch. */
19
+ export interface TaskAllocation {
20
+ /** Satoshis of BTC the task may spend. */
21
+ btcSat: number;
22
+ /** USDT (display units) the task may spend. */
23
+ usdt: number;
24
+ /** XAUT (display units) the task may spend. */
25
+ xaut: number;
26
+ }
27
+ /** A zero allocation — the default for read-only / monitoring tasks. */
28
+ export declare const ZERO_ALLOCATION: TaskAllocation;
29
+ /**
30
+ * A scheduled (or manual) autonomous task — the unit nanobot stored in
31
+ * `tasks.json`. A task names a skill to run on an interval, with an optional
32
+ * capital budget and an enable switch.
33
+ */
34
+ export interface AgentTask {
35
+ id: string;
36
+ name: string;
37
+ description: string;
38
+ /** Skill that scopes the run, e.g. 'portfolio-manager'. */
39
+ skill: string;
40
+ /** Seconds between runs. 0 = manual-only (never auto-fires). */
41
+ scheduleSec: number;
42
+ /** Run once immediately when the scheduler starts. */
43
+ runOnStartup: boolean;
44
+ /** Capital this task is allowed to move. */
45
+ allocation: TaskAllocation;
46
+ enabled: boolean;
47
+ /** Epoch ms. */
48
+ createdAt: number;
49
+ /** Epoch ms of the last run, or null if never run. */
50
+ lastRunAt: number | null;
51
+ }
52
+ /**
53
+ * What `create` accepts — id/createdAt/lastRunAt are filled by the store, and
54
+ * allocation/runOnStartup default when omitted.
55
+ */
56
+ export type NewTask = Omit<AgentTask, 'id' | 'createdAt' | 'lastRunAt' | 'allocation' | 'runOnStartup'> & Partial<Pick<AgentTask, 'id' | 'createdAt' | 'lastRunAt' | 'allocation' | 'runOnStartup'>>;
57
+ /** A default/seed task — carries a stable id so seeding is idempotent. */
58
+ export type TaskSeed = NewTask & {
59
+ id: string;
60
+ };
61
+ /** The task registry. Mirrors {@link ../memory/types.MemoryStore}'s shape. */
62
+ export interface TaskStore {
63
+ list(): Promise<AgentTask[]>;
64
+ get(id: string): Promise<AgentTask | null>;
65
+ create(input: NewTask): Promise<AgentTask>;
66
+ /** Patch a task. id/createdAt are immutable. Returns null if not found. */
67
+ update(id: string, patch: Partial<Omit<AgentTask, 'id' | 'createdAt'>>): Promise<AgentTask | null>;
68
+ remove(id: string): Promise<boolean>;
69
+ /** Insert any seed whose id isn't already present. Returns the ones added. */
70
+ seedDefaults(seeds: TaskSeed[]): Promise<AgentTask[]>;
71
+ }
72
+ /** Injected persistence — load once, save on every mutation. */
73
+ export interface TaskStoreIO {
74
+ load(): Promise<AgentTask[]>;
75
+ save(tasks: AgentTask[]): Promise<void>;
76
+ }
77
+ /** Token + dollar cost of a single run. */
78
+ export interface TaskRunCost {
79
+ usd: number;
80
+ inputTokens: number;
81
+ outputTokens: number;
82
+ }
83
+ /** Aggregated stats for one task across all its runs. */
84
+ export interface TaskStats {
85
+ runs: number;
86
+ errors: number;
87
+ lastRunAt: number | null;
88
+ lastDurationMs: number | null;
89
+ lastToolCalls: number | null;
90
+ lastError: string | null;
91
+ /** First ~800 chars of the last final response. */
92
+ lastText: string | null;
93
+ }
94
+ /** One completed run, newest-first in the recent ring buffer. */
95
+ export interface TaskRunRecord {
96
+ taskId: string;
97
+ taskName: string;
98
+ /** Epoch ms the run started. */
99
+ startedAt: number;
100
+ durationMs: number;
101
+ toolCalls: number;
102
+ ok: boolean;
103
+ error: string | null;
104
+ /** Final response text (truncated by the log). */
105
+ text: string;
106
+ cost: TaskRunCost;
107
+ }
108
+ /** A serializable point-in-time view of the run log, for persistence. */
109
+ export interface RunLogSnapshot {
110
+ stats: Record<string, TaskStats>;
111
+ recent: TaskRunRecord[];
112
+ cumulative: TaskRunCost;
113
+ }
114
+ /** Injected persistence for the run log. */
115
+ export interface RunLogIO {
116
+ load(): Promise<RunLogSnapshot | null>;
117
+ save(snapshot: RunLogSnapshot): Promise<void>;
118
+ }
119
+ /** The result of running a task once. The host's `run` callback returns this. */
120
+ export interface TaskRunOutcome {
121
+ ok: boolean;
122
+ text?: string;
123
+ toolCalls?: number;
124
+ error?: string;
125
+ cost?: Partial<TaskRunCost>;
126
+ }
127
+ /** Host-provided runner — typically wraps `Funnel.runTurn(buildTaskPrompt(task))`. */
128
+ export type RunTask = (task: AgentTask) => Promise<TaskRunOutcome>;
129
+ /** Opaque timer handle so the scheduler stays platform-agnostic. */
130
+ export type TimerHandle = unknown;
131
+ export interface SchedulerOptions {
132
+ store: TaskStore;
133
+ /** Runs a task and resolves with its outcome. Errors are caught by the scheduler. */
134
+ run: RunTask;
135
+ /** Notified after every run (success or error) — wire to a {@link TaskRunLog}. */
136
+ onOutcome?: (task: AgentTask, outcome: TaskRunOutcome, durationMs: number) => void;
137
+ /** Diagnostics sink. Default: silent. */
138
+ log?: (msg: string) => void;
139
+ /** Injectable clock. Default: Date.now. */
140
+ now?: () => number;
141
+ /** How often `start()` evaluates due tasks, ms. Default 30_000. */
142
+ tickMs?: number;
143
+ /** Max tasks running at once. Default 1 (serial — safest for a wallet). */
144
+ concurrency?: number;
145
+ /** Injectable timer. Default: setInterval. */
146
+ setTimer?: (fn: () => void, ms: number) => TimerHandle;
147
+ /** Injectable timer-clear. Default: clearInterval. */
148
+ clearTimer?: (handle: TimerHandle) => void;
149
+ }
150
+ export interface TaskScheduler {
151
+ /** Begin firing due tasks; runs `runOnStartup` tasks immediately. Idempotent. */
152
+ start(): void;
153
+ /** Stop firing. In-flight runs finish; no new ones start. */
154
+ stop(): void;
155
+ /** Evaluate all tasks once and run those that are due. Safe to call directly (tests). */
156
+ tick(): Promise<void>;
157
+ /** Force-run a task now regardless of schedule/enabled. Null if unknown/already running. */
158
+ runNow(id: string): Promise<TaskRunOutcome | null>;
159
+ /** Ids of tasks currently running. */
160
+ active(): string[];
161
+ /** Whether `start()` is in effect. */
162
+ isRunning(): boolean;
163
+ }
164
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/autonomy/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,gFAAgF;AAChF,MAAM,WAAW,cAAc;IAC7B,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAC;IACf,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAC;IACb,+CAA+C;IAC/C,IAAI,EAAE,MAAM,CAAC;CACd;AAED,wEAAw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,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,KAAK,WAAW,CAAC;IACvD,sDAAsD;IACtD,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,aAAa;IAC5B,iFAAiF;IACjF,KAAK,IAAI,IAAI,CAAC;IACd,6DAA6D;IAC7D,IAAI,IAAI,IAAI,CAAC;IACb,yFAAyF;IACzF,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,4FAA4F;IAC5F,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACnD,sCAAsC;IACtC,MAAM,IAAI,MAAM,EAAE,CAAC;IACnB,sCAAsC;IACtC,SAAS,IAAI,OAAO,CAAC;CACtB"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Autonomy — the agent's task brain.
3
+ *
4
+ * This is the half of "the agent's memory" the {@link ../memory/types MemoryStore}
5
+ * (soul + facts) does NOT cover: the *operational* state nanobot kept across
6
+ * `tasks.json` + `cron/jobs.json` + its run history. Lifted into core so every
7
+ * host (desktop sidecar, agent, cli) runs the same autonomous loop — storage
8
+ * and timers are injected (fs/SQLite on Node, AsyncStorage on RN), the logic is
9
+ * pure TS with zero runtime deps.
10
+ *
11
+ * Three pieces:
12
+ * - TaskStore — the registry of scheduled/manual tasks (was tasks-store.ts)
13
+ * - TaskRunLog — what each task did, when, and what it cost (was agent-state.ts)
14
+ * - Scheduler — fires due tasks on their interval (was nanobot cron)
15
+ *
16
+ * Spend safety lives alongside in {@link ./risk}.
17
+ */
18
+ /** A zero allocation — the default for read-only / monitoring tasks. */
19
+ export const ZERO_ALLOCATION = { btcSat: 0, usdt: 0, xaut: 0 };
20
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/autonomy/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAYH,wEAAwE;AACxE,MAAM,CAAC,MAAM,eAAe,GAAmB,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Canonical Bitrefill tool contract — gift cards, mobile top-ups, eSIMs.
3
+ *
4
+ * Same pattern as the LSPS1 contract: the tool *names + schemas* live here so
5
+ * every host (CLI REST adapter, desktop MCP, mobile WDK adapter) exposes the
6
+ * exact same surface to the agent. Only the transport differs.
7
+ *
8
+ * - CLI / desktop server → REST against `https://api.bitrefill.com/v2`
9
+ * (see `apps/cli/src/bitrefillTools.ts`).
10
+ * - Desktop sidecar → the remote MCP at `api.bitrefill.com/mcp` already
11
+ * exposes equivalent tools under different names; a binder there can
12
+ * rename them to this contract for parity.
13
+ *
14
+ * `bitrefill_create_invoice` is the spend — confirmation-gated by the contract.
15
+ * Everything else is read-only (search, product details, balance, invoice/order
16
+ * status). Invoice creation supports `payment_method:"balance"` (instant, pulls
17
+ * from pre-funded account) or `lightning|bitcoin|usdc_base|...` (the response
18
+ * carries a payment URI/invoice; the user pays out-of-band, then the order is
19
+ * fulfilled).
20
+ *
21
+ * Pure data — no deps, no fetch, RN-safe.
22
+ */
23
+ import type { ToolDef } from '../types.js';
24
+ import { InProcessToolSource } from '../tools/in-process.js';
25
+ export interface BitrefillToolDef extends ToolDef {
26
+ /** Moves real money → confirmation-gated. */
27
+ spend?: boolean;
28
+ }
29
+ /**
30
+ * The canonical Bitrefill tool list. Each host's binder translates these
31
+ * args into the Bitrefill REST body (CLI) or MCP/CLI call (other hosts).
32
+ */
33
+ export declare const BITREFILL_TOOLS: BitrefillToolDef[];
34
+ /** All Bitrefill tool names that move money (confirmation-gated). */
35
+ export declare const BITREFILL_SPEND_TOOLS: Set<string>;
36
+ export declare function isBitrefillSpendTool(name: string): boolean;
37
+ export declare function getBitrefillTool(name: string): BitrefillToolDef | undefined;
38
+ /** A handler bound to one Bitrefill tool. */
39
+ export type BitrefillHandler = (args: Record<string, unknown>) => Promise<unknown>;
40
+ export interface BindBitrefillOptions {
41
+ /** Skip tools without a handler instead of throwing (default false). */
42
+ allowMissing?: boolean;
43
+ /** ToolSource id for the registry (default 'bitrefill'). */
44
+ id?: string;
45
+ }
46
+ /**
47
+ * Bind Bitrefill contract tools to in-process handlers → an InProcessToolSource.
48
+ *
49
+ * const source = bindBitrefillTools({
50
+ * bitrefill_search: async (args) => api.search(args),
51
+ * bitrefill_get_product: async ({ product_id }) => api.product(product_id),
52
+ * bitrefill_get_balance: async () => api.balance(),
53
+ * bitrefill_create_invoice: async (args) => api.createInvoice(args),
54
+ * bitrefill_get_invoice: async ({ invoice_id }) => api.invoice(invoice_id),
55
+ * bitrefill_get_order: async ({ order_id }) => api.order(order_id),
56
+ * });
57
+ * tools.register(source);
58
+ */
59
+ export declare function bindBitrefillTools(handlers: Record<string, BitrefillHandler>, opts?: BindBitrefillOptions): InProcessToolSource;
60
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/bitrefill/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAG7D,MAAM,WAAW,gBAAiB,SAAQ,OAAO;IAC/C,6CAA6C;IAC7C,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAuBD;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,gBAAgB,EA0E7C,CAAC;AAEF,qEAAqE;AACrE,eAAO,MAAM,qBAAqB,EAAE,GAAG,CAAC,MAAM,CAE7C,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE1D;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAE3E;AAED,6CAA6C;AAC7C,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAEnF,MAAM,WAAW,oBAAoB;IACnC,wEAAwE;IACxE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,4DAA4D;IAC5D,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAC1C,IAAI,GAAE,oBAAyB,GAC9B,mBAAmB,CAiBrB"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Canonical Bitrefill tool contract — gift cards, mobile top-ups, eSIMs.
3
+ *
4
+ * Same pattern as the LSPS1 contract: the tool *names + schemas* live here so
5
+ * every host (CLI REST adapter, desktop MCP, mobile WDK adapter) exposes the
6
+ * exact same surface to the agent. Only the transport differs.
7
+ *
8
+ * - CLI / desktop server → REST against `https://api.bitrefill.com/v2`
9
+ * (see `apps/cli/src/bitrefillTools.ts`).
10
+ * - Desktop sidecar → the remote MCP at `api.bitrefill.com/mcp` already
11
+ * exposes equivalent tools under different names; a binder there can
12
+ * rename them to this contract for parity.
13
+ *
14
+ * `bitrefill_create_invoice` is the spend — confirmation-gated by the contract.
15
+ * Everything else is read-only (search, product details, balance, invoice/order
16
+ * status). Invoice creation supports `payment_method:"balance"` (instant, pulls
17
+ * from pre-funded account) or `lightning|bitcoin|usdc_base|...` (the response
18
+ * carries a payment URI/invoice; the user pays out-of-band, then the order is
19
+ * fulfilled).
20
+ *
21
+ * Pure data — no deps, no fetch, RN-safe.
22
+ */
23
+ import { InProcessToolSource } from '../tools/in-process.js';
24
+ function t(name, description, properties = {}, required = [], spend = false) {
25
+ return {
26
+ name,
27
+ description,
28
+ spend,
29
+ requiresConfirmation: spend,
30
+ parameters: { type: 'object', properties, required },
31
+ };
32
+ }
33
+ /**
34
+ * The canonical Bitrefill tool list. Each host's binder translates these
35
+ * args into the Bitrefill REST body (CLI) or MCP/CLI call (other hosts).
36
+ */
37
+ export const BITREFILL_TOOLS = [
38
+ t('bitrefill_search', "Search Bitrefill's product catalog by keyword (brand, country, type). Returns up to ~20 matches with `id`, `name`, `country`, `category` and `denominations`. The model picks the right product id and then calls `bitrefill_get_product` for the package list.", {
39
+ query: { type: 'string', description: 'Search keyword. e.g. "amazon", "steam", "vodafone uk", "esim europe".' },
40
+ country: { type: 'string', description: 'OPTIONAL — ISO country code to scope results (e.g. "US", "GB", "DE"). Many brands are country-specific.' },
41
+ limit: { type: 'number', description: 'OPTIONAL — max results (1–25, default 10).' },
42
+ }, ['query']),
43
+ t('bitrefill_get_product', "Get full details for one product, including its `packages` array (each package = a denomination with `id`, `value`, `price`, `currency`). Use the package `id` (NOT the bare value) when creating an invoice.", {
44
+ product_id: { type: 'string', description: 'Product slug from bitrefill_search, e.g. "amazon-us", "steam-us".' },
45
+ }, ['product_id']),
46
+ t('bitrefill_get_balance', "Get the user's Bitrefill account balance (the pre-funded pool used by `payment_method:\"balance\"`). Returns `{ balance, currency }`. No args."),
47
+ t('bitrefill_create_invoice', "SPEND: confirmation-gated. Create an invoice for one or more products. Pass `payment_method:\"balance\"` + `auto_pay:true` for instant fulfillment from the account balance (lowest blast radius). For Lightning/on-chain, omit `auto_pay`, set `payment_method:\"lightning\"` (etc.) and `refund_address` — the response carries the payment URI; poll `bitrefill_get_invoice` until status=\"complete\" and then read the order. Up to 20 line items per invoice.", {
48
+ products: {
49
+ type: 'array',
50
+ description: 'Line items. Each: { product_id, package_id, quantity }. Get `package_id` from bitrefill_get_product (NOT the bare denomination value).',
51
+ items: {
52
+ type: 'object',
53
+ properties: {
54
+ product_id: { type: 'string' },
55
+ package_id: { type: 'string' },
56
+ quantity: { type: 'number' },
57
+ },
58
+ required: ['product_id', 'package_id', 'quantity'],
59
+ },
60
+ },
61
+ payment_method: {
62
+ type: 'string',
63
+ description: 'How to pay: "balance" (account balance, instant), "lightning", "bitcoin", "usdc_base" (x402), "usdc_polygon", "usdt_tron", etc.',
64
+ enum: ['balance', 'lightning', 'bitcoin', 'usdc_base', 'usdc_polygon', 'usdc_ethereum', 'usdt_tron', 'usdt_ethereum'],
65
+ },
66
+ auto_pay: { type: 'boolean', description: 'Required true with `payment_method:"balance"` for instant settlement. Omit for crypto methods.' },
67
+ refund_address: { type: 'string', description: 'REQUIRED for non-balance crypto methods — refund destination if the invoice expires or partially pays.' },
68
+ email: { type: 'string', description: 'OPTIONAL — delivery / receipt email. Defaults to the account email when authenticated.' },
69
+ webhook_url: { type: 'string', description: 'OPTIONAL — URL Bitrefill calls when the order is delivered.' },
70
+ }, ['products', 'payment_method'],
71
+ /* spend */ true),
72
+ t('bitrefill_get_invoice', "Get the invoice's current status: `unpaid`, `pending`, `paid`, `complete`, `expired`, `failed`. For crypto payment methods, poll this until `complete`; then call `bitrefill_get_order` for redemption details.", {
73
+ invoice_id: { type: 'string', description: 'Invoice id returned by bitrefill_create_invoice.' },
74
+ }, ['invoice_id']),
75
+ t('bitrefill_get_order', "Get an order's redemption details once delivered. Returns `redemption_info` containing the code, PIN (for prepaid cards), redemption link, instructions. ONLY call after the corresponding invoice status is `complete`. Treat the returned code as cash — never paste it in shared chats.", {
76
+ order_id: { type: 'string', description: 'Order id from a completed invoice (`order_id` on the invoice or in its `orders[]`).' },
77
+ }, ['order_id']),
78
+ ];
79
+ /** All Bitrefill tool names that move money (confirmation-gated). */
80
+ export const BITREFILL_SPEND_TOOLS = new Set(BITREFILL_TOOLS.filter((t) => t.spend).map((t) => t.name));
81
+ export function isBitrefillSpendTool(name) {
82
+ return BITREFILL_SPEND_TOOLS.has(name);
83
+ }
84
+ export function getBitrefillTool(name) {
85
+ return BITREFILL_TOOLS.find((t) => t.name === name);
86
+ }
87
+ /**
88
+ * Bind Bitrefill contract tools to in-process handlers → an InProcessToolSource.
89
+ *
90
+ * const source = bindBitrefillTools({
91
+ * bitrefill_search: async (args) => api.search(args),
92
+ * bitrefill_get_product: async ({ product_id }) => api.product(product_id),
93
+ * bitrefill_get_balance: async () => api.balance(),
94
+ * bitrefill_create_invoice: async (args) => api.createInvoice(args),
95
+ * bitrefill_get_invoice: async ({ invoice_id }) => api.invoice(invoice_id),
96
+ * bitrefill_get_order: async ({ order_id }) => api.order(order_id),
97
+ * });
98
+ * tools.register(source);
99
+ */
100
+ export function bindBitrefillTools(handlers, opts = {}) {
101
+ const bound = [];
102
+ for (const def of BITREFILL_TOOLS) {
103
+ const handler = handlers[def.name];
104
+ if (!handler) {
105
+ if (opts.allowMissing)
106
+ continue;
107
+ throw new Error(`bindBitrefillTools: no handler for "${def.name}"`);
108
+ }
109
+ bound.push({
110
+ name: def.name,
111
+ description: def.description,
112
+ parameters: def.parameters,
113
+ requiresConfirmation: def.requiresConfirmation,
114
+ handler,
115
+ });
116
+ }
117
+ return new InProcessToolSource(opts.id ?? 'bitrefill', bound);
118
+ }
119
+ //# sourceMappingURL=contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/bitrefill/contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAGH,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAa7D,SAAS,CAAC,CACR,IAAY,EACZ,WAAmB,EACnB,aAAoB,EAAE,EACtB,WAAqB,EAAE,EACvB,KAAK,GAAG,KAAK;IAEb,OAAO;QACL,IAAI;QACJ,WAAW;QACX,KAAK;QACL,oBAAoB,EAAE,KAAK;QAC3B,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE;KACrD,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAuB;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AAI,eAAe,EAAE,CAAC;QAClC,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACnC,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,IAAI,IAAI,CAAC,YAAY;gBAAE,SAAS;YAChC,MAAM,IAAI,KAAK,CAAC,uCAAuC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC;QACtE,CAAC;QACD,KAAK,CAAC,IAAI,CAAC;YACT,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,WAAW,EAAE,GAAG,CAAC,WAAW;YAC5B,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,oBAAoB,EAAE,GAAG,CAAC,oBAAoB;YAC9C,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAI,mBAAmB,CAAC,IAAI,CAAC,EAAE,IAAI,WAAW,EAAE,KAAK,CAAC,CAAC;AAChE,CAAC"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Tool-output compression — the "fit more into a tiny window" part.
3
+ *
4
+ * Tool results are the single biggest, most repetitive thing the engine pushes
5
+ * into a small on-device model's context. A merchant search returns 40 near
6
+ * identical rows; a tx history returns hundreds; a swap quote nests config the
7
+ * model never reads. Every round, the *raw* `JSON.stringify(result)` is fed back
8
+ * into history — so on a 2k-window 0.6B model the conversation drowns in JSON
9
+ * the model didn't need, crowding out the system prompt, the skill, and the
10
+ * actual question.
11
+ *
12
+ * `compressToolResult` is a structural crusher (the idea behind Headroom's
13
+ * SmartCrusher/ToolCrusher, reimplemented natively — no dependency, no network,
14
+ * no proxy, so it stays on-device and private). It walks the JSON and:
15
+ *
16
+ * • dedupes identical array items, then keeps the first/last few and replaces
17
+ * the middle with an honest `{ "__elided__": N }` marker,
18
+ * • caps nesting depth (deep config → a one-line summary),
19
+ * • truncates long *prose* strings (logs, descriptions),
20
+ *
21
+ * and never regresses: if crushing doesn't actually save tokens, the original
22
+ * is returned untouched.
23
+ *
24
+ * SAFETY: it never touches numbers, never elides whole objects, never truncates
25
+ * whitespace-free strings (addresses, BOLT11 invoices, txids, pubkeys), and
26
+ * never touches a value under a money/identity key (see DEFAULT_PRESERVE_KEYS).
27
+ * Amounts and recipients reach the model intact — the confirm readback is built
28
+ * deterministically from the resolved call anyway, but this keeps the model's
29
+ * own view honest too. Small results (below `minTokens`) are passed through
30
+ * verbatim so nothing changes for the common case.
31
+ */
32
+ /** Keys whose values are never elided or truncated — amounts, ids, recipients. */
33
+ export declare const DEFAULT_PRESERVE_KEYS: readonly string[];
34
+ export interface ToolCrushOptions {
35
+ /** Don't compress results estimated below this many tokens. Default 200. */
36
+ minTokens?: number;
37
+ /** Max items kept in any array before the middle is elided. Default 8. */
38
+ maxArrayItems?: number;
39
+ /** Max nesting depth kept verbatim; deeper is summarized. Default 6. */
40
+ maxDepth?: number;
41
+ /** Max chars for a single prose string before truncation (0 disables). Default 600. */
42
+ maxStringLength?: number;
43
+ /** Dedupe identical array items before eliding. Default true. */
44
+ dedupe?: boolean;
45
+ /** Keys whose values are never elided/truncated (defaults to DEFAULT_PRESERVE_KEYS). */
46
+ preserveKeys?: readonly string[];
47
+ }
48
+ export interface CrushResult {
49
+ /** Serialized, compressed content — ready to push into history. */
50
+ content: string;
51
+ /** Estimated tokens of the original serialization. */
52
+ originalTokens: number;
53
+ /** Estimated tokens after crushing. */
54
+ compressedTokens: number;
55
+ /** Total array items dropped across the whole structure. */
56
+ elided: number;
57
+ /** False when the original was returned untouched (too small, or no win). */
58
+ changed: boolean;
59
+ }
60
+ /**
61
+ * Crush a tool result for inclusion in model context. Returns the original,
62
+ * verbatim, when it's small or when crushing wouldn't save tokens.
63
+ */
64
+ export declare function compressToolResult(value: unknown, opts?: ToolCrushOptions): CrushResult;
65
+ //# sourceMappingURL=compress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compress.d.ts","sourceRoot":"","sources":["../../src/context/compress.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAIH,kFAAkF;AAClF,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAiClD,CAAC;AAEF,MAAM,WAAW,gBAAgB;IAC/B,4EAA4E;IAC5E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0EAA0E;IAC1E,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,iEAAiE;IACjE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,wFAAwF;IACxF,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAClC;AAED,MAAM,WAAW,WAAW;IAC1B,mEAAmE;IACnE,OAAO,EAAE,MAAM,CAAC;IAChB,sDAAsD;IACtD,cAAc,EAAE,MAAM,CAAC;IACvB,uCAAuC;IACvC,gBAAgB,EAAE,MAAM,CAAC;IACzB,4DAA4D;IAC5D,MAAM,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,OAAO,EAAE,OAAO,CAAC;CAClB;AA4BD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,gBAAqB,GAAG,WAAW,CA+B3F"}