@artesian/tides-mcp 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/README.md +9 -8
- package/dist/tides-mcp.js +376 -173
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Tides MCP
|
|
2
2
|
|
|
3
|
-
This lets your AI assistant work with your Tides ledger. It runs on your computer. It opens your ledger there, so Tides' servers never see your finances.
|
|
3
|
+
This lets your AI assistant work with your Tides ledger. It runs on your computer. It opens your ledger there, so Tides' servers never see your finances. It is the only Tides MCP: the old address ending in `/mcp` no longer works.
|
|
4
4
|
|
|
5
|
-
**Warning.** Whatever the assistant reads is sent to the company that runs the assistant. Only
|
|
5
|
+
**Warning.** Whatever the assistant reads is sent to the company that runs the assistant. Only connect an assistant you trust with your finances.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -38,15 +38,16 @@ Add this to `~/.cursor/mcp.json`:
|
|
|
38
38
|
}
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
##
|
|
41
|
+
## Connect it
|
|
42
42
|
|
|
43
|
-
1. Ask the assistant to pair
|
|
44
|
-
2.
|
|
45
|
-
3.
|
|
43
|
+
1. Ask the assistant to connect to Tides, or run `tides-mcp pair` in a terminal.
|
|
44
|
+
2. Tides opens in your browser and shows this computer's name and a code. Check the code matches the one the assistant shows, then select Approve.
|
|
45
|
+
3. If the browser doesn't open, use the link the assistant gives you, or open Settings, then Tides MCP, and enter the code. Either way, do it within ten minutes.
|
|
46
|
+
4. If you asked the assistant, ask it to connect again so it picks up the approval. `tides-mcp pair` waits on its own.
|
|
46
47
|
|
|
47
|
-
The
|
|
48
|
+
The keys that connect this computer stay in your system keychain (macOS Keychain, Windows Credential Manager, or the Linux secret service). If there is no keychain, they are kept in `~/.config/tides-mcp/secrets.json`, readable only by you.
|
|
48
49
|
|
|
49
|
-
To remove this computer,
|
|
50
|
+
To remove this computer, disconnect it in Tides under Settings, then Tides MCP. Running `tides-mcp unpair` also clears its keys from this computer.
|
|
50
51
|
|
|
51
52
|
## Deleting and restoring
|
|
52
53
|
|
package/dist/tides-mcp.js
CHANGED
|
@@ -15531,10 +15531,33 @@ class StdioServerTransport {
|
|
|
15531
15531
|
});
|
|
15532
15532
|
}
|
|
15533
15533
|
}
|
|
15534
|
+
|
|
15535
|
+
// src/browser.ts
|
|
15536
|
+
import { spawn } from "node:child_process";
|
|
15537
|
+
var pairingLink = (serverUrl, code) => `${serverUrl}/settings?${new URLSearchParams({ pair: code })}`;
|
|
15538
|
+
function command(url, platform) {
|
|
15539
|
+
if (platform === "darwin")
|
|
15540
|
+
return ["open", [url]];
|
|
15541
|
+
if (platform === "win32")
|
|
15542
|
+
return ["cmd", ["/c", "start", "", url]];
|
|
15543
|
+
return ["xdg-open", [url]];
|
|
15544
|
+
}
|
|
15545
|
+
function browserOpener(platform = process.platform, run = spawn) {
|
|
15546
|
+
return (url) => {
|
|
15547
|
+
try {
|
|
15548
|
+
const [file, args] = command(url, platform);
|
|
15549
|
+
const child = run(file, args, { detached: true, stdio: "ignore" });
|
|
15550
|
+
child.on("error", () => {
|
|
15551
|
+
return;
|
|
15552
|
+
});
|
|
15553
|
+
child.unref();
|
|
15554
|
+
} catch {}
|
|
15555
|
+
};
|
|
15556
|
+
}
|
|
15534
15557
|
// package.json
|
|
15535
15558
|
var package_default = {
|
|
15536
15559
|
name: "@artesian/tides-mcp",
|
|
15537
|
-
version: "0.
|
|
15560
|
+
version: "0.3.0",
|
|
15538
15561
|
description: "Tides for your AI assistant: decrypts your ledger on this computer.",
|
|
15539
15562
|
type: "module",
|
|
15540
15563
|
bin: {
|
|
@@ -15694,7 +15717,14 @@ function deviceClient(http, token) {
|
|
|
15694
15717
|
}
|
|
15695
15718
|
});
|
|
15696
15719
|
},
|
|
15697
|
-
dismissRun: (id) => json("POST", `/api/v1/mail/runs/${id}/dismiss`)
|
|
15720
|
+
dismissRun: (id) => json("POST", `/api/v1/mail/runs/${id}/dismiss`),
|
|
15721
|
+
restoreRun: (id) => json("POST", `/api/v1/mail/runs/${id}/restore`),
|
|
15722
|
+
dismissInbox: (ids) => json("POST", "/api/v1/inbox/dismiss", {
|
|
15723
|
+
body: { ids }
|
|
15724
|
+
}),
|
|
15725
|
+
restoreInbox: (ids) => json("POST", "/api/v1/inbox/restore", {
|
|
15726
|
+
body: { ids }
|
|
15727
|
+
})
|
|
15698
15728
|
},
|
|
15699
15729
|
tides: {
|
|
15700
15730
|
dayPrices: (on) => json("GET", "/api/v1/market/day-prices", { query: { on } }),
|
|
@@ -15710,6 +15740,8 @@ function deviceClient(http, token) {
|
|
|
15710
15740
|
form.set("pin", JSON.stringify(options.pin));
|
|
15711
15741
|
return json("POST", "/api/v1/arrivals/upload", { body: form });
|
|
15712
15742
|
},
|
|
15743
|
+
health: () => json("GET", "/api/v1/health"),
|
|
15744
|
+
sens: (filter) => json("GET", "/api/v1/sens", { query: { ...filter, limit: 100 } }),
|
|
15713
15745
|
searchInstruments: (q) => json("GET", "/api/v1/instruments", { query: { q, limit: 10 } }),
|
|
15714
15746
|
watchlist: () => json("GET", "/api/v1/watchlist"),
|
|
15715
15747
|
watch: (instrumentId) => json("POST", "/api/v1/watchlist", { body: { instrumentId } }),
|
|
@@ -19372,7 +19404,7 @@ var POLL_HEADER = "x-tides-poll";
|
|
|
19372
19404
|
|
|
19373
19405
|
class PairingExpiredError extends Error {
|
|
19374
19406
|
constructor() {
|
|
19375
|
-
super("The
|
|
19407
|
+
super("The code expired before it was approved. Connect again and enter the new code within ten minutes.");
|
|
19376
19408
|
this.name = "PairingExpiredError";
|
|
19377
19409
|
}
|
|
19378
19410
|
}
|
|
@@ -19407,7 +19439,7 @@ async function startPairing(http, secrets, name, options = {}) {
|
|
|
19407
19439
|
}
|
|
19408
19440
|
|
|
19409
19441
|
// src/secrets.ts
|
|
19410
|
-
import { spawn } from "node:child_process";
|
|
19442
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
19411
19443
|
import {
|
|
19412
19444
|
chmod,
|
|
19413
19445
|
mkdir,
|
|
@@ -19425,7 +19457,7 @@ var defaultSecretsPath = () => join(homedir(), ".config", SERVICE, "secrets.json
|
|
|
19425
19457
|
var toHex = (text) => Buffer.from(text, "utf8").toString("hex");
|
|
19426
19458
|
var fromHex = (hex) => Buffer.from(hex.trim(), "hex").toString("utf8");
|
|
19427
19459
|
var spawnRun = (command, args, input) => new Promise((resolve, reject) => {
|
|
19428
|
-
const child =
|
|
19460
|
+
const child = spawn2(command, args, { windowsHide: true });
|
|
19429
19461
|
let stdout = "";
|
|
19430
19462
|
let stderr = "";
|
|
19431
19463
|
child.stdout.on("data", (chunk) => {
|
|
@@ -27135,6 +27167,58 @@ var EMPTY_COMPLETION_RESULT = {
|
|
|
27135
27167
|
}
|
|
27136
27168
|
};
|
|
27137
27169
|
|
|
27170
|
+
// ../../packages/app/src/account-match.ts
|
|
27171
|
+
function isDate(run) {
|
|
27172
|
+
if (!/^(19|20)\d{6}$/.test(run))
|
|
27173
|
+
return false;
|
|
27174
|
+
const iso = `${run.slice(0, 4)}-${run.slice(4, 6)}-${run.slice(6)}`;
|
|
27175
|
+
const date = new Date(`${iso}T00:00:00Z`);
|
|
27176
|
+
return !Number.isNaN(date.getTime()) && date.toISOString().startsWith(iso);
|
|
27177
|
+
}
|
|
27178
|
+
function numbersInName(name) {
|
|
27179
|
+
return (name.match(/\d+/g) ?? []).filter((run) => run.length >= 6 && run.length <= 16 && !isDate(run));
|
|
27180
|
+
}
|
|
27181
|
+
var digitsOf = (accountNumber) => {
|
|
27182
|
+
const digits = accountNumber?.replace(/[\s-]/g, "") ?? "";
|
|
27183
|
+
return /^\d{4,}$/.test(digits) ? digits : null;
|
|
27184
|
+
};
|
|
27185
|
+
function closeness(run, accountNumber) {
|
|
27186
|
+
const digits = digitsOf(accountNumber);
|
|
27187
|
+
if (!digits || !run.endsWith(digits))
|
|
27188
|
+
return 0;
|
|
27189
|
+
return digits.length === run.length ? Number.POSITIVE_INFINITY : digits.length;
|
|
27190
|
+
}
|
|
27191
|
+
function byFileName(accounts, fileName) {
|
|
27192
|
+
let best;
|
|
27193
|
+
let bestScore = 0;
|
|
27194
|
+
let tied = false;
|
|
27195
|
+
for (const run of numbersInName(fileName))
|
|
27196
|
+
for (const account of accounts) {
|
|
27197
|
+
const score = closeness(run, account.accountNumber);
|
|
27198
|
+
if (!score || account.id === best?.id)
|
|
27199
|
+
continue;
|
|
27200
|
+
if (score > bestScore) {
|
|
27201
|
+
best = account;
|
|
27202
|
+
bestScore = score;
|
|
27203
|
+
tied = false;
|
|
27204
|
+
} else if (score === bestScore)
|
|
27205
|
+
tied = true;
|
|
27206
|
+
}
|
|
27207
|
+
return tied ? undefined : best;
|
|
27208
|
+
}
|
|
27209
|
+
function matchAccount(accounts, input) {
|
|
27210
|
+
const ofKind = accounts.filter((account) => account.kind === input.kind);
|
|
27211
|
+
if (input.accountNumber) {
|
|
27212
|
+
const found = ofKind.find((account) => account.accountNumber === input.accountNumber);
|
|
27213
|
+
return found ? { accountId: found.id, why: "number" } : null;
|
|
27214
|
+
}
|
|
27215
|
+
const open = ofKind.filter((account) => account.closedOn === null);
|
|
27216
|
+
const named = input.fileName ? byFileName(open, input.fileName) : undefined;
|
|
27217
|
+
if (named)
|
|
27218
|
+
return { accountId: named.id, why: "file_name" };
|
|
27219
|
+
const used = input.recipeId ? open.find((account) => (account.recipes ?? []).includes(input.recipeId)) : undefined;
|
|
27220
|
+
return used ? { accountId: used.id, why: "recipe" } : null;
|
|
27221
|
+
}
|
|
27138
27222
|
// ../../packages/core/src/money/currency.ts
|
|
27139
27223
|
var builtIns = {
|
|
27140
27224
|
ZAR: 2,
|
|
@@ -27475,6 +27559,26 @@ var BudgetReset = discriminatedUnion("kind", [
|
|
|
27475
27559
|
day: int().min(1).max(31)
|
|
27476
27560
|
})
|
|
27477
27561
|
]).describe("When the period starts: weekday for weekly; day, last-day, working-day or nth-weekday for monthly; date for yearly. Days past a month's end move to its last day.");
|
|
27562
|
+
// ../../packages/core/src/colors.ts
|
|
27563
|
+
var COLORS = [
|
|
27564
|
+
"grey",
|
|
27565
|
+
"blue",
|
|
27566
|
+
"red",
|
|
27567
|
+
"yellow",
|
|
27568
|
+
"green",
|
|
27569
|
+
"pink",
|
|
27570
|
+
"purple",
|
|
27571
|
+
"cyan",
|
|
27572
|
+
"orange"
|
|
27573
|
+
];
|
|
27574
|
+
function freshColor(taken) {
|
|
27575
|
+
const free = COLORS.filter((color) => !taken.includes(color));
|
|
27576
|
+
const from = free.length ? free : COLORS;
|
|
27577
|
+
return from[Math.floor(Math.random() * from.length)];
|
|
27578
|
+
}
|
|
27579
|
+
|
|
27580
|
+
// ../../packages/schemas/src/color.ts
|
|
27581
|
+
var Color = _enum(COLORS);
|
|
27478
27582
|
// ../../packages/schemas/src/health.ts
|
|
27479
27583
|
var Health = object({ status: literal("ok") });
|
|
27480
27584
|
var HealthError = object({
|
|
@@ -27547,12 +27651,14 @@ var CategoryJson = object({
|
|
|
27547
27651
|
id: uuid2(),
|
|
27548
27652
|
name: string2(),
|
|
27549
27653
|
parentId: uuid2().nullable(),
|
|
27550
|
-
bucket: _enum(["needs", "wants", "savings"]).nullable()
|
|
27654
|
+
bucket: _enum(["needs", "wants", "savings"]).nullable(),
|
|
27655
|
+
color: Color.nullable()
|
|
27551
27656
|
});
|
|
27552
27657
|
var NewCategory = object({
|
|
27553
27658
|
name: string2().trim().min(1).max(60),
|
|
27554
27659
|
parentId: uuid2().nullish(),
|
|
27555
|
-
bucket: _enum(["needs", "wants", "savings"]).nullish()
|
|
27660
|
+
bucket: _enum(["needs", "wants", "savings"]).nullish(),
|
|
27661
|
+
color: Color.nullish()
|
|
27556
27662
|
});
|
|
27557
27663
|
var CategoryPatch = NewCategory.partial();
|
|
27558
27664
|
var TransactionJson = object({
|
|
@@ -27584,18 +27690,6 @@ var TransactionPatch = object({
|
|
|
27584
27690
|
notes: string2().max(2000).nullable().optional()
|
|
27585
27691
|
});
|
|
27586
27692
|
// ../../packages/schemas/src/views.ts
|
|
27587
|
-
var ACCOUNT_COLORS = [
|
|
27588
|
-
"grey",
|
|
27589
|
-
"blue",
|
|
27590
|
-
"red",
|
|
27591
|
-
"yellow",
|
|
27592
|
-
"green",
|
|
27593
|
-
"pink",
|
|
27594
|
-
"purple",
|
|
27595
|
-
"cyan",
|
|
27596
|
-
"orange"
|
|
27597
|
-
];
|
|
27598
|
-
var AccountColor = _enum(ACCOUNT_COLORS);
|
|
27599
27693
|
var AccountJson = object({
|
|
27600
27694
|
id: uuid2(),
|
|
27601
27695
|
name: string2(),
|
|
@@ -27605,7 +27699,7 @@ var AccountJson = object({
|
|
|
27605
27699
|
currency: string2(),
|
|
27606
27700
|
closedOn: IsoDate.nullable(),
|
|
27607
27701
|
opening: object({ amount: MoneyJson, on: IsoDate }).nullable(),
|
|
27608
|
-
color:
|
|
27702
|
+
color: Color.nullable().optional(),
|
|
27609
27703
|
excludeFromNetWorth: boolean2().optional(),
|
|
27610
27704
|
createdAt: string2()
|
|
27611
27705
|
});
|
|
@@ -27615,7 +27709,7 @@ var NewAccount = object({
|
|
|
27615
27709
|
kind: _enum(["bank", "broker"]),
|
|
27616
27710
|
accountNumber: string2().trim().min(1).max(50).nullish(),
|
|
27617
27711
|
currency: string2().refine(isCurrency, "Unsupported currency.").optional(),
|
|
27618
|
-
color:
|
|
27712
|
+
color: Color.nullable().optional(),
|
|
27619
27713
|
excludeFromNetWorth: boolean2().optional()
|
|
27620
27714
|
});
|
|
27621
27715
|
var AccountPatch = NewAccount.pick({
|
|
@@ -27675,8 +27769,6 @@ var BudgetStatusJson = object({
|
|
|
27675
27769
|
budgets: array(object({
|
|
27676
27770
|
budgetId: uuid2(),
|
|
27677
27771
|
name: string2(),
|
|
27678
|
-
categoryIds: array(uuid2()),
|
|
27679
|
-
categoryId: uuid2(),
|
|
27680
27772
|
category: string2(),
|
|
27681
27773
|
period: BudgetPeriod,
|
|
27682
27774
|
reset: BudgetReset.nullable(),
|
|
@@ -27809,7 +27901,11 @@ var HoldingJson = object({
|
|
|
27809
27901
|
value: MoneyJson.nullable(),
|
|
27810
27902
|
gain: MoneyJson.nullable()
|
|
27811
27903
|
});
|
|
27812
|
-
var LabelJson = object({
|
|
27904
|
+
var LabelJson = object({
|
|
27905
|
+
id: uuid2(),
|
|
27906
|
+
name: string2(),
|
|
27907
|
+
color: Color.nullable().optional()
|
|
27908
|
+
});
|
|
27813
27909
|
var LabelCountJson = LabelJson.extend({
|
|
27814
27910
|
documentCount: number2().int(),
|
|
27815
27911
|
transactionCount: number2().int(),
|
|
@@ -27873,7 +27969,7 @@ var PAYLOADS = {
|
|
|
27873
27969
|
closedOn: IsoDate.nullable(),
|
|
27874
27970
|
opening: object({ amount: MoneyJson, on: IsoDate }).nullable(),
|
|
27875
27971
|
bankBalance: object({ amount: MoneyJson, on: IsoDate }).nullable().optional(),
|
|
27876
|
-
color:
|
|
27972
|
+
color: Color.nullable().optional(),
|
|
27877
27973
|
excludeFromNetWorth: boolean2().optional(),
|
|
27878
27974
|
recipes: array(string2()).optional()
|
|
27879
27975
|
}),
|
|
@@ -27918,10 +28014,11 @@ var PAYLOADS = {
|
|
|
27918
28014
|
}),
|
|
27919
28015
|
budget: object({
|
|
27920
28016
|
name: string2(),
|
|
27921
|
-
categoryIds: array(string2()).
|
|
28017
|
+
categoryIds: array(string2()).optional(),
|
|
27922
28018
|
amount: MoneyJson,
|
|
27923
28019
|
period: BudgetPeriod2,
|
|
27924
|
-
reset: BudgetReset.nullable()
|
|
28020
|
+
reset: BudgetReset.nullable(),
|
|
28021
|
+
color: Color.nullable().optional()
|
|
27925
28022
|
}),
|
|
27926
28023
|
document: object({
|
|
27927
28024
|
fileId: Id.nullable(),
|
|
@@ -27956,7 +28053,11 @@ var PAYLOADS = {
|
|
|
27956
28053
|
set: RuleSet,
|
|
27957
28054
|
replace: boolean2()
|
|
27958
28055
|
}),
|
|
27959
|
-
document_type: object({
|
|
28056
|
+
document_type: object({
|
|
28057
|
+
name: string2(),
|
|
28058
|
+
takesTransaction: boolean2(),
|
|
28059
|
+
color: Color.nullable().optional()
|
|
28060
|
+
}),
|
|
27960
28061
|
correspondent: object({ name: string2() }),
|
|
27961
28062
|
instrument: object({
|
|
27962
28063
|
symbol: string2().min(1),
|
|
@@ -27975,7 +28076,10 @@ var PAYLOADS = {
|
|
|
27975
28076
|
exponent: int().min(0).max(4),
|
|
27976
28077
|
rate: object({ units: DecimalString, zar: DecimalString })
|
|
27977
28078
|
}),
|
|
27978
|
-
tag: object({
|
|
28079
|
+
tag: object({
|
|
28080
|
+
name: string2(),
|
|
28081
|
+
color: Color.nullable().optional()
|
|
28082
|
+
}),
|
|
27979
28083
|
import: object({
|
|
27980
28084
|
accountId: Id.nullable(),
|
|
27981
28085
|
fileId: Id.nullable(),
|
|
@@ -28241,7 +28345,13 @@ var Arrival = discriminatedUnion("kind", [
|
|
|
28241
28345
|
ImportFileArrival
|
|
28242
28346
|
]);
|
|
28243
28347
|
function openArrival(entry, publicKey, privateKey) {
|
|
28244
|
-
const
|
|
28348
|
+
const dismissed = entry.dismissedAt ? { dismissedAt: entry.dismissedAt } : {};
|
|
28349
|
+
const unreadable = (reason) => ({
|
|
28350
|
+
status: "unreadable",
|
|
28351
|
+
id: entry.id,
|
|
28352
|
+
reason,
|
|
28353
|
+
...dismissed
|
|
28354
|
+
});
|
|
28245
28355
|
let value;
|
|
28246
28356
|
try {
|
|
28247
28357
|
value = JSON.parse(new TextDecoder().decode(openSealed(entry.sealed, publicKey, privateKey)));
|
|
@@ -28254,7 +28364,7 @@ function openArrival(entry, publicKey, privateKey) {
|
|
|
28254
28364
|
if (typeof version === "number" && version < ARRIVAL_VERSION)
|
|
28255
28365
|
return unreadable("old_version");
|
|
28256
28366
|
const parsed = Arrival.safeParse(value);
|
|
28257
|
-
return parsed.success ? { status: "ok", id: entry.id, arrival: parsed.data } : unreadable("invalid");
|
|
28367
|
+
return parsed.success ? { status: "ok", id: entry.id, arrival: parsed.data, ...dismissed } : unreadable("invalid");
|
|
28258
28368
|
}
|
|
28259
28369
|
// ../../packages/ledger/src/codec.ts
|
|
28260
28370
|
var encoder = new TextEncoder;
|
|
@@ -28792,22 +28902,23 @@ async function setBudget(ctx, input) {
|
|
|
28792
28902
|
const limit = fromMoneyJson(input.amount);
|
|
28793
28903
|
if (limit.currency !== "ZAR" || limit.amountMinor <= 0n)
|
|
28794
28904
|
throw new Error("A budget is a positive amount in rand.");
|
|
28795
|
-
|
|
28796
|
-
|
|
28905
|
+
const name = input.name.trim();
|
|
28906
|
+
if (!name)
|
|
28907
|
+
throw new Error("A budget needs a name.");
|
|
28797
28908
|
const saved = await withRetry(ctx, () => {
|
|
28798
28909
|
const budgets = ctx.store.all("budget");
|
|
28799
|
-
const
|
|
28800
|
-
const
|
|
28910
|
+
const existing = input.budgetId ? budgets.find((b) => b.id === input.budgetId) : undefined;
|
|
28911
|
+
const color = input.color !== undefined ? input.color : existing ? existing.payload.color ?? null : freshColor(budgets.map((b) => b.payload.color));
|
|
28801
28912
|
return [
|
|
28802
28913
|
{
|
|
28803
28914
|
kind: "budget",
|
|
28804
28915
|
...existing ? { id: existing.id } : {},
|
|
28805
28916
|
payload: {
|
|
28806
|
-
name
|
|
28807
|
-
categoryIds: input.categoryIds,
|
|
28917
|
+
name,
|
|
28808
28918
|
amount: input.amount,
|
|
28809
28919
|
period: input.period,
|
|
28810
|
-
reset: input.reset ?? null
|
|
28920
|
+
reset: input.reset ?? null,
|
|
28921
|
+
color
|
|
28811
28922
|
}
|
|
28812
28923
|
}
|
|
28813
28924
|
];
|
|
@@ -29204,12 +29315,13 @@ function ruleTransaction(t, rules) {
|
|
|
29204
29315
|
const free = (field) => !t.handSet.includes(field);
|
|
29205
29316
|
const next = { ...t };
|
|
29206
29317
|
const categoryId = firstSet(matched, "categoryId");
|
|
29207
|
-
|
|
29208
|
-
|
|
29318
|
+
const applied = categoryId && free("category") ? categoryId : undefined;
|
|
29319
|
+
if (applied)
|
|
29320
|
+
next.categoryId = applied;
|
|
29209
29321
|
const description = firstSet(matched, "description");
|
|
29210
29322
|
if (description && free("description"))
|
|
29211
29323
|
next.description = description;
|
|
29212
|
-
const budgetId =
|
|
29324
|
+
const budgetId = matched.find(({ payload: { set } }) => set.budgetId !== undefined && (set.categoryId === undefined || set.categoryId === applied))?.payload.set.budgetId;
|
|
29213
29325
|
if (budgetId && free("budget"))
|
|
29214
29326
|
next.budgetId = budgetId;
|
|
29215
29327
|
const changed = next.categoryId !== t.categoryId || next.description !== t.description || next.budgetId !== t.budgetId;
|
|
@@ -29373,8 +29485,6 @@ async function budgetStatusOf(set, spentIn, options) {
|
|
|
29373
29485
|
return {
|
|
29374
29486
|
budgetId: budget.id,
|
|
29375
29487
|
name: budget.category,
|
|
29376
|
-
categoryIds: budget.categoryIds,
|
|
29377
|
-
categoryId: budget.categoryIds[0],
|
|
29378
29488
|
category: budget.category,
|
|
29379
29489
|
period: budget.period,
|
|
29380
29490
|
reset: budget.reset,
|
|
@@ -29646,28 +29756,11 @@ var uncategorised = (ctx, matches) => ctx.store.all("transaction").filter((row)
|
|
|
29646
29756
|
}));
|
|
29647
29757
|
async function deleteCategory(ctx, id) {
|
|
29648
29758
|
await ctx.api.deleteCategory(id);
|
|
29649
|
-
return withRetry(ctx, () =>
|
|
29650
|
-
const covering = ctx.store.all("budget").filter((b) => b.payload.categoryIds.includes(id));
|
|
29651
|
-
const trimmed = covering.filter((b) => b.payload.categoryIds.length > 1).map((b) => ({
|
|
29652
|
-
id: b.id,
|
|
29653
|
-
kind: "budget",
|
|
29654
|
-
payload: {
|
|
29655
|
-
...b.payload,
|
|
29656
|
-
categoryIds: b.payload.categoryIds.filter((c) => c !== id)
|
|
29657
|
-
}
|
|
29658
|
-
}));
|
|
29659
|
-
return {
|
|
29660
|
-
save: [...uncategorised(ctx, (c) => c === id), ...trimmed],
|
|
29661
|
-
remove: covering.filter((b) => b.payload.categoryIds.length === 1).map((b) => b.id)
|
|
29662
|
-
};
|
|
29663
|
-
});
|
|
29759
|
+
return withRetry(ctx, () => uncategorised(ctx, (c) => c === id));
|
|
29664
29760
|
}
|
|
29665
29761
|
async function clearCategories(ctx) {
|
|
29666
29762
|
const result = await ctx.api.clearCategories();
|
|
29667
|
-
const saved = await withRetry(ctx, () => (
|
|
29668
|
-
save: uncategorised(ctx, () => true),
|
|
29669
|
-
remove: ctx.store.all("budget").map((budget) => budget.id)
|
|
29670
|
-
}));
|
|
29763
|
+
const saved = await withRetry(ctx, () => uncategorised(ctx, () => true));
|
|
29671
29764
|
return { ...saved, ...result };
|
|
29672
29765
|
}
|
|
29673
29766
|
async function addStarterCategories(ctx) {
|
|
@@ -29778,13 +29871,12 @@ function bankOfFile(ledger, fileId) {
|
|
|
29778
29871
|
const correspondent = document?.payload.correspondentId ? ledger.get(document.payload.correspondentId) : null;
|
|
29779
29872
|
return bankFor(correspondent?.status === "ok" && correspondent.kind === "correspondent" ? correspondent.payload.name : null);
|
|
29780
29873
|
}
|
|
29781
|
-
function accountFor(ledger,
|
|
29874
|
+
function accountFor(ledger, stated, kind, choice, recipeId) {
|
|
29782
29875
|
const accounts = ledger.all("account");
|
|
29783
29876
|
if (choice)
|
|
29784
29877
|
return accounts.find((account) => account.id === choice.accountId && account.payload.kind === kind);
|
|
29785
|
-
|
|
29786
|
-
|
|
29787
|
-
return accounts.find((account) => account.payload.kind === kind && account.payload.accountNumber === accountNumber);
|
|
29878
|
+
const match = matchAccount(accounts.map((account) => ({ id: account.id, ...account.payload })), { ...stated, kind, recipeId });
|
|
29879
|
+
return match ? accounts.find((account) => account.id === match.accountId) : undefined;
|
|
29788
29880
|
}
|
|
29789
29881
|
function placeArrival(ledger, { id, arrival }, choice) {
|
|
29790
29882
|
let account;
|
|
@@ -29794,9 +29886,9 @@ function placeArrival(ledger, { id, arrival }, choice) {
|
|
|
29794
29886
|
claimed = Boolean(claim);
|
|
29795
29887
|
if (claim && !resumable)
|
|
29796
29888
|
return { status: "folded" };
|
|
29797
|
-
account = claim ? ledger.all("account").find((a) => a.id === claim.payload.accountId) : accountFor(ledger, arrival
|
|
29889
|
+
account = claim ? ledger.all("account").find((a) => a.id === claim.payload.accountId) : accountFor(ledger, arrival, "bank", choice, arrival.parser);
|
|
29798
29890
|
} else
|
|
29799
|
-
account = accountFor(ledger, arrival.accountNumber, "broker", choice, arrival.recipeId);
|
|
29891
|
+
account = accountFor(ledger, { accountNumber: arrival.accountNumber, fileName: null }, "broker", choice, arrival.recipeId);
|
|
29800
29892
|
return account ? { status: "account", account, claimed } : {
|
|
29801
29893
|
status: "needs_account",
|
|
29802
29894
|
accountNumber: arrival.accountNumber,
|
|
@@ -30167,7 +30259,7 @@ function oldArrivals(entries) {
|
|
|
30167
30259
|
return entries.flatMap((entry) => entry.status === "unreadable" && entry.reason === "old_version" ? [{ entryId: entry.id, message: OLD_VERSION }] : []);
|
|
30168
30260
|
}
|
|
30169
30261
|
function waitingToFold(entries, holding = false) {
|
|
30170
|
-
return entries.filter((entry) => entry.status === "ok" && entry.arrival.kind !== "held_mail" && entry.arrival.kind !== "held_source" && entry.arrival.kind !== "failed" && entry.arrival.kind !== "import_file" && (!holding || entry.arrival.kind !== "document" && entry.arrival.kind !== "statement"));
|
|
30262
|
+
return entries.filter((entry) => entry.status === "ok" && !entry.dismissedAt && entry.arrival.kind !== "held_mail" && entry.arrival.kind !== "held_source" && entry.arrival.kind !== "failed" && entry.arrival.kind !== "import_file" && (!holding || entry.arrival.kind !== "document" && entry.arrival.kind !== "statement"));
|
|
30171
30263
|
}
|
|
30172
30264
|
async function inboxStatus(ctx, keys, options = {}) {
|
|
30173
30265
|
const entries = options.entries ?? await openInbox(ctx, keys);
|
|
@@ -30195,7 +30287,7 @@ async function inboxStatus(ctx, keys, options = {}) {
|
|
|
30195
30287
|
});
|
|
30196
30288
|
}
|
|
30197
30289
|
for (const entry of entries) {
|
|
30198
|
-
if (entry.status !== "ok")
|
|
30290
|
+
if (entry.status !== "ok" || entry.dismissedAt)
|
|
30199
30291
|
continue;
|
|
30200
30292
|
const { arrival } = entry;
|
|
30201
30293
|
if (arrival.kind === "held_mail")
|
|
@@ -30318,8 +30410,14 @@ async function readArrivalFile(ctx, keys, fileId) {
|
|
|
30318
30410
|
await ctx.api.rewrapItemFile(fileId, wrapFileKey(fileId, fileKey, keys.ledgerKey));
|
|
30319
30411
|
return bytes;
|
|
30320
30412
|
}
|
|
30321
|
-
function heldMail(entries) {
|
|
30322
|
-
return entries.flatMap((entry) => entry.status === "ok" && entry.arrival.kind === "held_mail"
|
|
30413
|
+
function heldMail(entries, dismissed = false) {
|
|
30414
|
+
return entries.flatMap((entry) => entry.status === "ok" && entry.arrival.kind === "held_mail" && Boolean(entry.dismissedAt) === dismissed ? [
|
|
30415
|
+
{
|
|
30416
|
+
id: entry.id,
|
|
30417
|
+
mail: entry.arrival,
|
|
30418
|
+
dismissedAt: entry.dismissedAt ?? null
|
|
30419
|
+
}
|
|
30420
|
+
] : []);
|
|
30323
30421
|
}
|
|
30324
30422
|
async function runAgain(ctx, keys, entry) {
|
|
30325
30423
|
if (!entry.mail.fileId)
|
|
@@ -30346,13 +30444,12 @@ async function retryRuns(ctx, keys, runIds, allowed, options = {}) {
|
|
|
30346
30444
|
async function clearHeld(ctx, keys, entryIds, options = {}) {
|
|
30347
30445
|
const entries = options.entries ?? await openInbox(ctx, keys);
|
|
30348
30446
|
const chosen = heldMail(entries).filter((entry) => entryIds.includes(entry.id));
|
|
30349
|
-
|
|
30447
|
+
if (!chosen.length)
|
|
30448
|
+
return;
|
|
30449
|
+
for (const { mail } of chosen)
|
|
30350
30450
|
if (mail.runId)
|
|
30351
30451
|
await ctx.api.dismissRun(mail.runId).catch(() => {});
|
|
30352
|
-
|
|
30353
|
-
await ctx.api.deleteItemFile(mail.fileId);
|
|
30354
|
-
}
|
|
30355
|
-
await ctx.api.ackInbox(chosen.map((entry) => entry.id));
|
|
30452
|
+
await ctx.api.dismissInbox(chosen.map((entry) => entry.id));
|
|
30356
30453
|
}
|
|
30357
30454
|
async function retrySource(ctx, keys, entryId, options = {}) {
|
|
30358
30455
|
const entries = options.entries ?? await openInbox(ctx, keys);
|
|
@@ -30526,7 +30623,11 @@ function isSourceFile(doc, sources) {
|
|
|
30526
30623
|
// ../../packages/app/src/documents.ts
|
|
30527
30624
|
var NO_TYPE = "none";
|
|
30528
30625
|
var list = (value) => value ? value.split(",").filter(Boolean) : [];
|
|
30529
|
-
var label = (row) => ({
|
|
30626
|
+
var label = (row) => ({
|
|
30627
|
+
id: row.id,
|
|
30628
|
+
name: row.payload.name,
|
|
30629
|
+
..."color" in row.payload ? { color: row.payload.color ?? null } : {}
|
|
30630
|
+
});
|
|
30530
30631
|
function labelRows(ledger, field) {
|
|
30531
30632
|
if (field === "correspondent")
|
|
30532
30633
|
return ledger.correspondents;
|
|
@@ -30598,6 +30699,10 @@ function labelCounts(labels, documents, transactions, field) {
|
|
|
30598
30699
|
})).sort((a, b) => a.name.localeCompare(b.name));
|
|
30599
30700
|
}
|
|
30600
30701
|
var sameName = (a, b) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
30702
|
+
var freshTagColor = (ledger, changes) => freshColor([
|
|
30703
|
+
...ledger.tags.map((t) => t.payload.color),
|
|
30704
|
+
...changes.filter((c) => c.kind === "tag").map((c) => c.payload.color)
|
|
30705
|
+
]);
|
|
30601
30706
|
function editDocument(ledger, documentId, edit) {
|
|
30602
30707
|
const row = ledger.documents.find((d) => d.id === documentId);
|
|
30603
30708
|
if (!row)
|
|
@@ -30638,7 +30743,11 @@ function editDocument(ledger, documentId, edit) {
|
|
|
30638
30743
|
const known = ledger.tags.find((t) => sameName(t.payload.name, name)) ?? changes.find((c) => c.kind === "tag" && sameName(c.payload.name, name));
|
|
30639
30744
|
const id = known?.id ?? crypto.randomUUID();
|
|
30640
30745
|
if (!known)
|
|
30641
|
-
changes.push({
|
|
30746
|
+
changes.push({
|
|
30747
|
+
id,
|
|
30748
|
+
kind: "tag",
|
|
30749
|
+
payload: { name, color: freshTagColor(ledger, changes) }
|
|
30750
|
+
});
|
|
30642
30751
|
if (!ids.includes(id))
|
|
30643
30752
|
ids.push(id);
|
|
30644
30753
|
}
|
|
@@ -30683,7 +30792,11 @@ function editDocuments(ledger, ids, edit) {
|
|
|
30683
30792
|
if (known)
|
|
30684
30793
|
return known.id;
|
|
30685
30794
|
const id = crypto.randomUUID();
|
|
30686
|
-
changes.push(
|
|
30795
|
+
changes.push(kind === "tag" ? {
|
|
30796
|
+
id,
|
|
30797
|
+
kind,
|
|
30798
|
+
payload: { name, color: freshTagColor(ledger, changes) }
|
|
30799
|
+
} : { id, kind, payload: { name } });
|
|
30687
30800
|
return id;
|
|
30688
30801
|
};
|
|
30689
30802
|
const correspondentId = edit.correspondent === undefined ? undefined : edit.correspondent?.trim() ? labelId("correspondent", edit.correspondent) : null;
|
|
@@ -30825,14 +30938,18 @@ function taken(ctx, field, name, except) {
|
|
|
30825
30938
|
return labelRows(documentLedger(ctx.store), field).some((r) => r.id !== except && r.payload.name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
30826
30939
|
}
|
|
30827
30940
|
var clash = (field) => new Error(`That ${NOUN[field]} already exists.`);
|
|
30941
|
+
var colorOf = (row) => row.payload.color;
|
|
30828
30942
|
async function createLabel(ctx, input) {
|
|
30829
30943
|
const { field } = input;
|
|
30830
30944
|
const name = input.name.trim();
|
|
30831
30945
|
const saved = await withRetry(ctx, () => {
|
|
30832
30946
|
if (taken(ctx, field, name))
|
|
30833
30947
|
throw clash(field);
|
|
30948
|
+
if (field === "correspondent")
|
|
30949
|
+
return [{ kind: field, payload: { name } }];
|
|
30950
|
+
const color = input.color === undefined ? freshColor(labelRows(documentLedger(ctx.store), field).map(colorOf)) : input.color;
|
|
30834
30951
|
return [
|
|
30835
|
-
field === "document_type" ? { kind: field, payload: { name, takesTransaction: true } } : { kind: field, payload: { name } }
|
|
30952
|
+
field === "document_type" ? { kind: field, payload: { name, takesTransaction: true, color } } : { kind: field, payload: { name, color } }
|
|
30836
30953
|
];
|
|
30837
30954
|
});
|
|
30838
30955
|
return { ...saved, id: saved.saved[0] ?? "" };
|
|
@@ -30849,7 +30966,11 @@ async function renameLabel(ctx, input) {
|
|
|
30849
30966
|
{
|
|
30850
30967
|
kind: field,
|
|
30851
30968
|
id,
|
|
30852
|
-
payload: {
|
|
30969
|
+
payload: {
|
|
30970
|
+
...row.payload,
|
|
30971
|
+
name: input.name.trim(),
|
|
30972
|
+
...input.color === undefined ? {} : { color: input.color }
|
|
30973
|
+
}
|
|
30853
30974
|
}
|
|
30854
30975
|
];
|
|
30855
30976
|
});
|
|
@@ -31242,7 +31363,13 @@ function tagIdsFor(names, tags) {
|
|
|
31242
31363
|
const known = tags.find((t) => sameName2(t.payload.name, name)) ?? created.find((c) => sameName2(c.payload.name, name));
|
|
31243
31364
|
const id = known?.id ?? crypto.randomUUID();
|
|
31244
31365
|
if (!known)
|
|
31245
|
-
created.push({
|
|
31366
|
+
created.push({
|
|
31367
|
+
id,
|
|
31368
|
+
payload: {
|
|
31369
|
+
name,
|
|
31370
|
+
color: freshColor([...tags, ...created].map((t) => t.payload.color))
|
|
31371
|
+
}
|
|
31372
|
+
});
|
|
31246
31373
|
if (!ids.includes(id))
|
|
31247
31374
|
ids.push(id);
|
|
31248
31375
|
}
|
|
@@ -31500,36 +31627,6 @@ async function markTransfers(ctx, input) {
|
|
|
31500
31627
|
function movesBalance(row) {
|
|
31501
31628
|
return !row.payload.hidden || row.payload.transferCounterpartId !== null;
|
|
31502
31629
|
}
|
|
31503
|
-
// ../../packages/app/src/budget-of.ts
|
|
31504
|
-
function budgetOf(tx, budgets, parents = new Map) {
|
|
31505
|
-
if (tx.budgetId)
|
|
31506
|
-
return tx.budgetId;
|
|
31507
|
-
if (!tx.categoryId)
|
|
31508
|
-
return null;
|
|
31509
|
-
const covering = (id) => id ? budgets.find((b) => b.categoryIds.includes(id))?.id : undefined;
|
|
31510
|
-
return covering(tx.categoryId) ?? covering(parents.get(tx.categoryId)) ?? null;
|
|
31511
|
-
}
|
|
31512
|
-
// ../../packages/app/src/category-tree.ts
|
|
31513
|
-
var byName2 = (a, b) => a.name.localeCompare(b.name);
|
|
31514
|
-
function categoryTree(categories) {
|
|
31515
|
-
const ids = new Set(categories.map((c) => c.id));
|
|
31516
|
-
const topLevel = categories.filter((c) => !c.parentId || !ids.has(c.parentId)).sort(byName2);
|
|
31517
|
-
return topLevel.flatMap((parent) => [
|
|
31518
|
-
{ category: parent, parent: null },
|
|
31519
|
-
...categories.filter((c) => c.parentId === parent.id).sort(byName2).map((category) => ({ category, parent }))
|
|
31520
|
-
]);
|
|
31521
|
-
}
|
|
31522
|
-
function categoryForest(categories) {
|
|
31523
|
-
const tree = categoryTree(categories);
|
|
31524
|
-
return tree.filter((entry) => !entry.parent).map(({ category }) => ({
|
|
31525
|
-
...category,
|
|
31526
|
-
children: tree.filter((entry) => entry.parent?.id === category.id).map((entry) => entry.category)
|
|
31527
|
-
}));
|
|
31528
|
-
}
|
|
31529
|
-
function parentsOf(categories) {
|
|
31530
|
-
return new Map(categories.flatMap((c) => c.parentId ? [[c.id, c.parentId]] : []));
|
|
31531
|
-
}
|
|
31532
|
-
|
|
31533
31630
|
// ../../packages/app/src/totals.ts
|
|
31534
31631
|
function totalsRows(rows) {
|
|
31535
31632
|
return rows.map(({ payload: t }) => ({
|
|
@@ -31544,42 +31641,26 @@ function totalsRows(rows) {
|
|
|
31544
31641
|
}
|
|
31545
31642
|
|
|
31546
31643
|
// ../../packages/app/src/budgets.ts
|
|
31547
|
-
|
|
31548
|
-
|
|
31549
|
-
|
|
31550
|
-
|
|
31551
|
-
|
|
31552
|
-
|
|
31553
|
-
|
|
31554
|
-
|
|
31555
|
-
|
|
31556
|
-
|
|
31557
|
-
|
|
31558
|
-
categoryIds,
|
|
31559
|
-
category: budgetName(row.payload, names),
|
|
31560
|
-
limit: fromMoneyJson(row.payload.amount),
|
|
31561
|
-
period: row.payload.period,
|
|
31562
|
-
reset: normaliseReset(row.payload.period, row.payload.reset),
|
|
31563
|
-
createdAt: new Date(row.createdAt),
|
|
31564
|
-
updatedAt: new Date(row.updatedAt)
|
|
31565
|
-
}
|
|
31566
|
-
];
|
|
31567
|
-
}).sort((a, b) => a.category.localeCompare(b.category));
|
|
31644
|
+
function budgetsOf(budgets) {
|
|
31645
|
+
return budgets.map((row) => ({
|
|
31646
|
+
id: row.id,
|
|
31647
|
+
name: row.payload.name,
|
|
31648
|
+
category: row.payload.name,
|
|
31649
|
+
limit: fromMoneyJson(row.payload.amount),
|
|
31650
|
+
period: row.payload.period,
|
|
31651
|
+
reset: normaliseReset(row.payload.period, row.payload.reset),
|
|
31652
|
+
createdAt: new Date(row.createdAt),
|
|
31653
|
+
updatedAt: new Date(row.updatedAt)
|
|
31654
|
+
})).sort((a, b) => a.category.localeCompare(b.category));
|
|
31568
31655
|
}
|
|
31569
|
-
var byCreation = (a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id);
|
|
31570
31656
|
async function budgetStatus(budgets, rows, categories, options = {}) {
|
|
31571
31657
|
const today = options.today ?? todayInJohannesburg();
|
|
31572
|
-
const order = [...budgets].sort(byCreation).map((b) => ({ id: b.id, categoryIds: b.payload.categoryIds }));
|
|
31573
|
-
const parents = parentsOf(categories);
|
|
31574
31658
|
const shaped = totalsRows(rows).map((row, n) => ({
|
|
31575
31659
|
...row,
|
|
31576
|
-
budgetId:
|
|
31577
|
-
budgetId: rows[n]?.payload.budgetId ?? null,
|
|
31578
|
-
categoryId: row.categoryId
|
|
31579
|
-
}, order, parents)
|
|
31660
|
+
budgetId: rows[n]?.payload.budgetId ?? null
|
|
31580
31661
|
}));
|
|
31581
31662
|
const names = new Map(categories.map((c) => [c.id, c.name]));
|
|
31582
|
-
const status = await budgetStatusOf(budgetsOf(budgets
|
|
31663
|
+
const status = await budgetStatusOf(budgetsOf(budgets), (bounds, into) => budgetSpendOver(shaped, names, bounds, into), {
|
|
31583
31664
|
month: options.month ?? today.slice(0, 7),
|
|
31584
31665
|
today,
|
|
31585
31666
|
...options.rate ? { rate: options.rate } : {}
|
|
@@ -31611,6 +31692,26 @@ async function budgetStatus(budgets, rows, categories, options = {}) {
|
|
|
31611
31692
|
leftOut: status.leftOut
|
|
31612
31693
|
};
|
|
31613
31694
|
}
|
|
31695
|
+
// ../../packages/app/src/category-tree.ts
|
|
31696
|
+
var byName2 = (a, b) => a.name.localeCompare(b.name);
|
|
31697
|
+
function categoryTree(categories) {
|
|
31698
|
+
const ids = new Set(categories.map((c) => c.id));
|
|
31699
|
+
const topLevel = categories.filter((c) => !c.parentId || !ids.has(c.parentId)).sort(byName2);
|
|
31700
|
+
return topLevel.flatMap((parent) => [
|
|
31701
|
+
{ category: parent, parent: null },
|
|
31702
|
+
...categories.filter((c) => c.parentId === parent.id).sort(byName2).map((category) => ({ category, parent }))
|
|
31703
|
+
]);
|
|
31704
|
+
}
|
|
31705
|
+
function categoryForest(categories) {
|
|
31706
|
+
const tree = categoryTree(categories);
|
|
31707
|
+
return tree.filter((entry) => !entry.parent).map(({ category }) => ({
|
|
31708
|
+
...category,
|
|
31709
|
+
children: tree.filter((entry) => entry.parent?.id === category.id).map((entry) => entry.category)
|
|
31710
|
+
}));
|
|
31711
|
+
}
|
|
31712
|
+
function parentsOf(categories) {
|
|
31713
|
+
return new Map(categories.flatMap((c) => c.parentId ? [[c.id, c.parentId]] : []));
|
|
31714
|
+
}
|
|
31614
31715
|
// ../../packages/app/src/custom-instruments.ts
|
|
31615
31716
|
function latest(candidates, on) {
|
|
31616
31717
|
let best = null;
|
|
@@ -31869,12 +31970,10 @@ function documentSearch(store, filter) {
|
|
|
31869
31970
|
})
|
|
31870
31971
|
}, filter);
|
|
31871
31972
|
}
|
|
31872
|
-
function budgetList(store
|
|
31873
|
-
const names = new Map(categories.map((c) => [c.id, c.name]));
|
|
31973
|
+
function budgetList(store) {
|
|
31874
31974
|
return store.all("budget").map(({ id, payload }) => ({
|
|
31875
31975
|
id,
|
|
31876
|
-
name:
|
|
31877
|
-
categoryIds: payload.categoryIds,
|
|
31976
|
+
name: payload.name,
|
|
31878
31977
|
amount: payload.amount,
|
|
31879
31978
|
period: payload.period,
|
|
31880
31979
|
reset: payload.reset
|
|
@@ -31883,6 +31982,73 @@ function budgetList(store, categories) {
|
|
|
31883
31982
|
function currentBudgetStatus(store, categories, options) {
|
|
31884
31983
|
return budgetStatus(store.all("budget"), confirmed(store.all("transaction")), categories, options);
|
|
31885
31984
|
}
|
|
31985
|
+
// ../../packages/app/src/retire-budget-categories.ts
|
|
31986
|
+
var byCreation = (a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id);
|
|
31987
|
+
function retireBudgetCategories(input) {
|
|
31988
|
+
const retiring = input.budgets.filter((b) => b.payload.categoryIds !== undefined).sort(byCreation);
|
|
31989
|
+
if (!retiring.length)
|
|
31990
|
+
return { save: [], remove: [] };
|
|
31991
|
+
const names = new Map(input.categories.map((c) => [c.id, c.name]));
|
|
31992
|
+
const direct = new Map;
|
|
31993
|
+
for (const b of retiring)
|
|
31994
|
+
for (const id of b.payload.categoryIds ?? [])
|
|
31995
|
+
if (names.has(id) && !direct.has(id))
|
|
31996
|
+
direct.set(id, b.id);
|
|
31997
|
+
const parents = parentsOf(input.categories);
|
|
31998
|
+
const budgetFor = (categoryId) => {
|
|
31999
|
+
if (!categoryId)
|
|
32000
|
+
return;
|
|
32001
|
+
const parent = parents.get(categoryId);
|
|
32002
|
+
return direct.get(categoryId) ?? (parent ? direct.get(parent) : undefined);
|
|
32003
|
+
};
|
|
32004
|
+
const rules = input.rules.flatMap((r) => {
|
|
32005
|
+
if (r.payload.target !== "transaction" || r.payload.set.budgetId)
|
|
32006
|
+
return [];
|
|
32007
|
+
const budgetId = budgetFor(r.payload.set.categoryId);
|
|
32008
|
+
if (!budgetId)
|
|
32009
|
+
return [];
|
|
32010
|
+
return [
|
|
32011
|
+
{
|
|
32012
|
+
id: r.id,
|
|
32013
|
+
kind: "rule",
|
|
32014
|
+
payload: { ...r.payload, set: { ...r.payload.set, budgetId } }
|
|
32015
|
+
}
|
|
32016
|
+
];
|
|
32017
|
+
});
|
|
32018
|
+
const transactions = input.transactions.flatMap((t) => {
|
|
32019
|
+
if (t.payload.budgetId)
|
|
32020
|
+
return [];
|
|
32021
|
+
const budgetId = budgetFor(t.payload.categoryId);
|
|
32022
|
+
if (!budgetId)
|
|
32023
|
+
return [];
|
|
32024
|
+
return [
|
|
32025
|
+
{ id: t.id, kind: "transaction", payload: { ...t.payload, budgetId } }
|
|
32026
|
+
];
|
|
32027
|
+
});
|
|
32028
|
+
const remove = [];
|
|
32029
|
+
const budgets = retiring.flatMap((b) => {
|
|
32030
|
+
const { categoryIds = [], ...rest } = b.payload;
|
|
32031
|
+
const name = rest.name.trim() || categoryIds.flatMap((id) => names.get(id) ?? []).join(", ");
|
|
32032
|
+
if (!name) {
|
|
32033
|
+
remove.push(b.id);
|
|
32034
|
+
return [];
|
|
32035
|
+
}
|
|
32036
|
+
return [{ id: b.id, kind: "budget", payload: { ...rest, name } }];
|
|
32037
|
+
});
|
|
32038
|
+
return { save: [...rules, ...transactions, ...budgets], remove };
|
|
32039
|
+
}
|
|
32040
|
+
var budgetsNeedRetiring = (budgets) => budgets.some((b) => b.payload.categoryIds !== undefined);
|
|
32041
|
+
async function retireBudgets(ctx) {
|
|
32042
|
+
if (!budgetsNeedRetiring(ctx.store.all("budget")))
|
|
32043
|
+
return null;
|
|
32044
|
+
const categories = await ctx.api.categories();
|
|
32045
|
+
return withRetry(ctx, () => retireBudgetCategories({
|
|
32046
|
+
budgets: ctx.store.all("budget"),
|
|
32047
|
+
rules: ctx.store.all("rule"),
|
|
32048
|
+
transactions: ctx.store.all("transaction"),
|
|
32049
|
+
categories
|
|
32050
|
+
}));
|
|
32051
|
+
}
|
|
31886
32052
|
// ../../packages/app/src/triage-week.ts
|
|
31887
32053
|
var SAST_OFFSET_MS = 2 * 3600000;
|
|
31888
32054
|
// src/version-guard.ts
|
|
@@ -32065,9 +32231,9 @@ var Id4 = uuid2();
|
|
|
32065
32231
|
var budgetTools = [
|
|
32066
32232
|
tool({
|
|
32067
32233
|
name: "list_budgets",
|
|
32068
|
-
description: "Lists the budgets with their
|
|
32234
|
+
description: "Lists the budgets with their amount and period.",
|
|
32069
32235
|
input: {},
|
|
32070
|
-
run: async (_args, session) => json2(budgetList(session.ctx.store
|
|
32236
|
+
run: async (_args, session) => json2(budgetList(session.ctx.store))
|
|
32071
32237
|
}),
|
|
32072
32238
|
tool({
|
|
32073
32239
|
name: "budget_status",
|
|
@@ -32091,11 +32257,10 @@ var budgetTools = [
|
|
|
32091
32257
|
}),
|
|
32092
32258
|
tool({
|
|
32093
32259
|
name: "set_budget",
|
|
32094
|
-
description: "Creates or changes
|
|
32260
|
+
description: "Creates a budget, or changes the one budgetId names. The name is required and the amount is a positive rand amount. Spending counts towards a budget only when a transaction's budget is set, by hand or by a rule.",
|
|
32095
32261
|
input: {
|
|
32096
32262
|
budgetId: Id4.optional(),
|
|
32097
|
-
name: string2()
|
|
32098
|
-
categoryIds: array(Id4),
|
|
32263
|
+
name: string2(),
|
|
32099
32264
|
amount: MoneyJson,
|
|
32100
32265
|
period: BudgetPeriod,
|
|
32101
32266
|
reset: BudgetReset.nullable().optional()
|
|
@@ -32121,10 +32286,11 @@ var categoryTools = [
|
|
|
32121
32286
|
}),
|
|
32122
32287
|
tool({
|
|
32123
32288
|
name: "create_category",
|
|
32124
|
-
description: "Creates a category, optionally under a parent
|
|
32289
|
+
description: "Creates a category, optionally under a parent, in a bucket (needs, wants or savings) and with a colour. Without a colour it gets one not yet in use.",
|
|
32125
32290
|
input: NewCategory.shape,
|
|
32126
32291
|
run: async (input, session) => {
|
|
32127
|
-
const
|
|
32292
|
+
const color = input.color === undefined ? freshColor((await session.ctx.api.categories()).map((c) => c.color)) : input.color;
|
|
32293
|
+
const created = await session.api.createCategory({ ...input, color });
|
|
32128
32294
|
return json2({
|
|
32129
32295
|
summary: `Created ${created.name}.`,
|
|
32130
32296
|
changed: [{ kind: "category", id: created.id }]
|
|
@@ -32133,7 +32299,7 @@ var categoryTools = [
|
|
|
32133
32299
|
}),
|
|
32134
32300
|
tool({
|
|
32135
32301
|
name: "update_category",
|
|
32136
|
-
description: "Renames a category, moves it under another parent (null makes it top-level) or changes its bucket.",
|
|
32302
|
+
description: "Renames a category, moves it under another parent (null makes it top-level), or changes its bucket or colour (null clears it).",
|
|
32137
32303
|
input: { id: Id5, ...CategoryPatch.shape },
|
|
32138
32304
|
run: async ({ id, ...patch }, session) => {
|
|
32139
32305
|
const updated = await session.api.updateCategory(id, patch);
|
|
@@ -32145,7 +32311,7 @@ var categoryTools = [
|
|
|
32145
32311
|
}),
|
|
32146
32312
|
tool({
|
|
32147
32313
|
name: "delete_category",
|
|
32148
|
-
description: "Deletes a category. Its transactions become uncategorised
|
|
32314
|
+
description: "Deletes a category. Its transactions become uncategorised.",
|
|
32149
32315
|
input: { id: Id5 },
|
|
32150
32316
|
run: async ({ id }, session) => written(session, "Category deleted.", await deleteCategory(session.ctx, id), { also: [{ kind: "category", id }] })
|
|
32151
32317
|
}),
|
|
@@ -32164,7 +32330,7 @@ var categoryTools = [
|
|
|
32164
32330
|
}),
|
|
32165
32331
|
tool({
|
|
32166
32332
|
name: "clear_categories",
|
|
32167
|
-
description: "Deletes every category
|
|
32333
|
+
description: "Deletes every category, leaving all transactions uncategorised. Categories can't be brought back.",
|
|
32168
32334
|
input: {},
|
|
32169
32335
|
run: async (_args, session) => {
|
|
32170
32336
|
const { categories, ...saved } = await clearCategories(session.ctx);
|
|
@@ -32583,12 +32749,12 @@ var mailTools = [
|
|
|
32583
32749
|
}),
|
|
32584
32750
|
tool({
|
|
32585
32751
|
name: "delete_held_mail",
|
|
32586
|
-
description: "
|
|
32752
|
+
description: "Moves held emails (from inbox_status) to Recently deleted, where they stay for 30 days and can be restored in the web app.",
|
|
32587
32753
|
input: { entryIds: array(Id8).min(1) },
|
|
32588
32754
|
run: async ({ entryIds }, session) => {
|
|
32589
32755
|
await clearHeld(session.ctx, session.keys, entryIds);
|
|
32590
32756
|
return json2({
|
|
32591
|
-
summary: `${plural(entryIds.length, "email")} deleted.`,
|
|
32757
|
+
summary: `${plural(entryIds.length, "email")} moved to Recently deleted.`,
|
|
32592
32758
|
changed: entryIds.map((id) => ({ kind: "arrival", id }))
|
|
32593
32759
|
});
|
|
32594
32760
|
}
|
|
@@ -32607,6 +32773,33 @@ var mailTools = [
|
|
|
32607
32773
|
})
|
|
32608
32774
|
];
|
|
32609
32775
|
|
|
32776
|
+
// src/tools/market.ts
|
|
32777
|
+
var DAY_MS3 = 86400000;
|
|
32778
|
+
var marketTools = [
|
|
32779
|
+
tool({
|
|
32780
|
+
name: "get_sens",
|
|
32781
|
+
description: "JSE SENS announcements for a JSE code or an instrument id, newest first.",
|
|
32782
|
+
input: {
|
|
32783
|
+
ticker: string2().min(1).max(20).optional(),
|
|
32784
|
+
instrumentId: uuid2().optional(),
|
|
32785
|
+
days: number2().int().min(1).max(365).optional().describe("Look back this many days (default 30)")
|
|
32786
|
+
},
|
|
32787
|
+
run: async ({ ticker, instrumentId, days }, session) => {
|
|
32788
|
+
if (!ticker && !instrumentId)
|
|
32789
|
+
throw new Error("Pass a ticker or an instrumentId.");
|
|
32790
|
+
const since = session.ctx.now().getTime() - (days ?? 30) * DAY_MS3;
|
|
32791
|
+
const rows = await session.api.sens({ ticker, instrumentId });
|
|
32792
|
+
return json2(rows.filter((row) => Date.parse(row.publishedAt) >= since));
|
|
32793
|
+
}
|
|
32794
|
+
}),
|
|
32795
|
+
tool({
|
|
32796
|
+
name: "get_status",
|
|
32797
|
+
description: "Checks that Tides and its database are up.",
|
|
32798
|
+
input: {},
|
|
32799
|
+
run: async (_args, session) => json2(await session.api.health())
|
|
32800
|
+
})
|
|
32801
|
+
];
|
|
32802
|
+
|
|
32610
32803
|
// src/tools/rules.ts
|
|
32611
32804
|
var Id9 = uuid2();
|
|
32612
32805
|
var Rule = PAYLOADS.rule.omit({ order: true });
|
|
@@ -32976,6 +33169,7 @@ var ledgerTools = [
|
|
|
32976
33169
|
...tradeTools,
|
|
32977
33170
|
...inboxTools,
|
|
32978
33171
|
...watchlistTools,
|
|
33172
|
+
...marketTools,
|
|
32979
33173
|
...mailTools,
|
|
32980
33174
|
...deletedTools
|
|
32981
33175
|
];
|
|
@@ -32985,10 +33179,10 @@ function sessionTools(info) {
|
|
|
32985
33179
|
return [
|
|
32986
33180
|
{
|
|
32987
33181
|
name: "device_status",
|
|
32988
|
-
description: "Shows which Tides
|
|
33182
|
+
description: "Shows which Tides server this computer is connected to and its name, after checking the connection still works.",
|
|
32989
33183
|
async run(_args, session) {
|
|
32990
33184
|
await session.ctx.api.categories();
|
|
32991
|
-
return `
|
|
33185
|
+
return `Connected as "${session.deviceName}" to ${info.serverUrl}.`;
|
|
32992
33186
|
}
|
|
32993
33187
|
},
|
|
32994
33188
|
...ledgerTools
|
|
@@ -32998,21 +33192,21 @@ function sessionTools(info) {
|
|
|
32998
33192
|
// src/unlock.ts
|
|
32999
33193
|
class NotPairedError extends Error {
|
|
33000
33194
|
constructor() {
|
|
33001
|
-
super("This computer isn't
|
|
33195
|
+
super("This computer isn't connected to Tides yet. Run `tides-mcp pair`, then type the code in Tides under Settings → Tides MCP.");
|
|
33002
33196
|
this.name = "NotPairedError";
|
|
33003
33197
|
}
|
|
33004
33198
|
}
|
|
33005
33199
|
|
|
33006
33200
|
class RevokedError extends Error {
|
|
33007
33201
|
constructor() {
|
|
33008
|
-
super("Tides no longer accepts this
|
|
33202
|
+
super("Tides no longer accepts this computer: it was disconnected, or your account was reset. Connect it again with `tides-mcp pair`.");
|
|
33009
33203
|
this.name = "RevokedError";
|
|
33010
33204
|
}
|
|
33011
33205
|
}
|
|
33012
33206
|
|
|
33013
33207
|
class WrongKeysError extends Error {
|
|
33014
33208
|
constructor() {
|
|
33015
|
-
super("
|
|
33209
|
+
super("This computer couldn't open your Tides data, so it stopped. Ask me to connect again, and disconnect the old one in Tides under Settings → Tides MCP.");
|
|
33016
33210
|
this.name = "WrongKeysError";
|
|
33017
33211
|
}
|
|
33018
33212
|
}
|
|
@@ -33129,13 +33323,13 @@ async function startUp(secrets, http, version = VERSION, warn = (line) => proces
|
|
|
33129
33323
|
}
|
|
33130
33324
|
|
|
33131
33325
|
// src/server.ts
|
|
33132
|
-
var REVOKED_MESSAGE = "This
|
|
33326
|
+
var REVOKED_MESSAGE = "This computer was disconnected in Tides. Ask me to connect again.";
|
|
33133
33327
|
var reply = (text, isError = false, notes = []) => ({
|
|
33134
33328
|
content: [text, ...notes].map((line) => ({ type: "text", text: line })),
|
|
33135
33329
|
...isError ? { isError } : {}
|
|
33136
33330
|
});
|
|
33137
33331
|
function buildServer(state, http, secrets, options) {
|
|
33138
|
-
const server = new McpServer({ name: "tides", version:
|
|
33332
|
+
const server = new McpServer({ name: "tides", version: VERSION });
|
|
33139
33333
|
const initial = state();
|
|
33140
33334
|
let session = initial === "unreachable" ? null : initial;
|
|
33141
33335
|
let unlocking = null;
|
|
@@ -33150,7 +33344,7 @@ function buildServer(state, http, secrets, options) {
|
|
|
33150
33344
|
function registerUnpaired() {
|
|
33151
33345
|
clearTools();
|
|
33152
33346
|
registered.push(server.registerTool("pair_device", {
|
|
33153
|
-
description: "Pairs this computer with the user's Tides account. Call it once to get a code for the user to
|
|
33347
|
+
description: "Pairs this computer with the user's Tides account. Call it once to open Tides in the browser and get a link and code for the user to approve, then call it again after they have approved."
|
|
33154
33348
|
}, pairDevice));
|
|
33155
33349
|
}
|
|
33156
33350
|
async function activeSession() {
|
|
@@ -33192,6 +33386,7 @@ function buildServer(state, http, secrets, options) {
|
|
|
33192
33386
|
try {
|
|
33193
33387
|
const active = await activeSession();
|
|
33194
33388
|
await active.ctx.store.sync();
|
|
33389
|
+
await retireBudgets(active.ctx);
|
|
33195
33390
|
const text = await definition.run(args, active);
|
|
33196
33391
|
const notice = leftOutNotice(active.ctx.store);
|
|
33197
33392
|
return reply(text, false, notice ? [notice] : []);
|
|
@@ -33242,7 +33437,11 @@ function buildServer(state, http, secrets, options) {
|
|
|
33242
33437
|
}
|
|
33243
33438
|
if (first) {
|
|
33244
33439
|
current.shown = true;
|
|
33245
|
-
|
|
33440
|
+
const link = pairingLink(options.serverUrl, started.code);
|
|
33441
|
+
try {
|
|
33442
|
+
(options.openBrowser ?? browserOpener())(link);
|
|
33443
|
+
} catch {}
|
|
33444
|
+
return reply(`Open this link to approve: ${link} — or type the code ${started.code} in Tides under Settings → Tides MCP within ten minutes. Then ask me to connect again.`);
|
|
33246
33445
|
}
|
|
33247
33446
|
if (!current.approved) {
|
|
33248
33447
|
try {
|
|
@@ -33270,7 +33469,7 @@ function buildServer(state, http, secrets, options) {
|
|
|
33270
33469
|
});
|
|
33271
33470
|
return reply(error.message, true);
|
|
33272
33471
|
}
|
|
33273
|
-
return reply(error instanceof UnreachableError ? error.message : "
|
|
33472
|
+
return reply(error instanceof UnreachableError ? error.message : "Connected, but your Tides data could not be opened. Ask me to connect again to retry.", true);
|
|
33274
33473
|
}
|
|
33275
33474
|
if (pairing === current)
|
|
33276
33475
|
pairing = null;
|
|
@@ -33297,21 +33496,25 @@ async function main(argv) {
|
|
|
33297
33496
|
switch (command) {
|
|
33298
33497
|
case "pair": {
|
|
33299
33498
|
const pairing = await startPairing(http, secrets, hostname2());
|
|
33300
|
-
|
|
33499
|
+
const link = pairingLink(url, pairing.code);
|
|
33500
|
+
browserOpener()(link);
|
|
33501
|
+
say(`Open this link to approve:
|
|
33502
|
+
|
|
33503
|
+
${link}
|
|
33301
33504
|
|
|
33302
|
-
|
|
33505
|
+
or type the code ${pairing.code} in Tides under Settings → Tides MCP within ten minutes.
|
|
33303
33506
|
`);
|
|
33304
33507
|
await pairing.wait();
|
|
33305
|
-
say("
|
|
33508
|
+
say("Connected. Your AI assistant can use Tides now.");
|
|
33306
33509
|
return;
|
|
33307
33510
|
}
|
|
33308
33511
|
case "unpair":
|
|
33309
33512
|
await secrets.clear();
|
|
33310
|
-
say("This computer
|
|
33513
|
+
say("This computer is disconnected. Disconnect it in Tides under Settings → Tides MCP as well.");
|
|
33311
33514
|
return;
|
|
33312
33515
|
case "status": {
|
|
33313
33516
|
const session = await unlock(secrets, http);
|
|
33314
|
-
say(`
|
|
33517
|
+
say(`Connected to ${url} as "${session.deviceName}"; your data opens.`);
|
|
33315
33518
|
return;
|
|
33316
33519
|
}
|
|
33317
33520
|
case undefined: {
|