ownledger 2.1.1 → 2.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.
@@ -19,14 +19,14 @@ import { yamlText } from "../engine/write.js";
19
19
  import { dashboardOf } from "./api.js";
20
20
  import { cardList } from "../modules/cards/index.js";
21
21
  import { cashback, closeOnOrAfter, feeWaiver, lineKind, statements } from "../modules/cards/statements.js";
22
- import { cardsData, STATEMENTS_FILE, termsOf, recordFor } from "../modules/cards/terms.js";
22
+ import { STATEMENTS_FILE, termsOf, recordFor, withBankAmounts } from "../modules/cards/terms.js";
23
23
  import { bestCard } from "./bestcard.js";
24
24
  import { openWalletClient } from "./openwallet.js";
25
25
  const out = (v) => ({ content: [{ type: "text", text: typeof v === "string" ? v : JSON.stringify(v) }] });
26
26
  const fail = (msg) => ({ content: [{ type: "text", text: msg }], isError: true });
27
27
  const INSTRUCTIONS = `ownledger: the owner's personal finance ledger, kept in their private GitHub repo.
28
28
  Amounts are whole numbers in the base currency (see about). Answers are computed here; never add up raw rows yourself when a tool computes it.
29
- Writes: the add_/edit_/void_/set_/remove_ tools only PREVIEW. Show the owner the preview (what, amounts, accounts), wait for their explicit go, then call apply with the preview_id. Never invent an amount, date, account or category: ask. Call rules once before the first write.
29
+ Writes: the add_/edit_/void_/set_/remove_ tools and batch only PREVIEW. Show the owner the preview (what, amounts, accounts), wait for their explicit go, then call apply with the preview_id. Never invent an amount, date, account or category: ask. Call rules once before the first write. For more than ~3 transaction writes at once, use batch: one preview, one go, one commit.
30
30
  Dashboard: when the owner asks to "track", "watch" or "put on my dashboard" something, offer a widget (add_widget), using the categories and accounts from rules.
31
31
  Cards: for "which card should I use", call best_card; statements, cashback and fee waivers come from cards and card_statement.`;
32
32
  const OPENWALLET_LINE = `
@@ -92,18 +92,22 @@ export function buildServer(env, props, src = sourceFor(env), deps = {}) {
92
92
  return fail(`error: ${e.message}`);
93
93
  }
94
94
  };
95
- const preview = async (build) => {
95
+ const preview = async (build, show = 20) => {
96
96
  const s = await snap();
97
97
  const p = build(s.files);
98
98
  const id = await savePending(env, { baseSha: s.sha, files: p.files, message: p.message, summary: p.summary, created: new Date().toISOString() });
99
99
  const shown = [];
100
- for (const t of [...(p.change.addTransactions ?? []), ...(p.change.replaceTransactions ?? [])].slice(0, 20))
100
+ const all = [...(p.change.addTransactions ?? []), ...(p.change.replaceTransactions ?? [])];
101
+ for (const t of all.slice(0, show))
101
102
  shown.push(compactTx(t));
103
+ const b = p;
102
104
  return {
103
105
  preview_id: id,
104
106
  will: p.summary,
105
107
  commit_message: p.message,
106
108
  ...(shown.length ? { transactions: shown } : {}),
109
+ ...(all.length > shown.length ? { not_shown: all.length - shown.length } : {}),
110
+ ...(b.balanceChange ? { balance_change: b.balanceChange } : {}),
107
111
  ...(p.warnings.length ? { warnings: p.warnings.map((w) => w.message) } : {}),
108
112
  next: "Show this to the owner. Only after their explicit go, call apply with this preview_id.",
109
113
  };
@@ -327,6 +331,36 @@ export function buildServer(env, props, src = sourceFor(env), deps = {}) {
327
331
  }));
328
332
  server.registerTool("edit_transaction", { description: "PREVIEW changes to a transaction by id (only the fields given change).", inputSchema: z.object({ id: z.string(), ...txFields, date: z.string().optional(), reason }), annotations: pv }, wrap(async ({ id, reason, ...patch }) => preview((f) => A.editTransaction(f, id, patch, meta(reason)))));
329
333
  server.registerTool("void_transaction", { description: "PREVIEW voiding a transaction (kept in history, excluded everywhere).", inputSchema: z.object({ id: z.string(), reason }), annotations: pv }, wrap(async ({ id, reason }) => preview((f) => A.voidTransaction(f, id, reason, meta(reason)))));
334
+ const batchOp = z.object(txFields).partial().extend({
335
+ op: z.enum(["add", "edit", "void", "statement_amount"]),
336
+ id: z.string().optional().describe("edit / void: the transaction id"),
337
+ amount: z.number().int().optional().describe("add/edit: positive, whole base-currency units; statement_amount: the bank's amount (0 allowed)"),
338
+ statement_date: z.string().optional().describe("statement_amount: the statement's closing date yyyy-mm-dd (with account and amount: the bank's amount, like add_statement_amount)"),
339
+ reason: z.string().trim().max(A.REASON_MAX).optional().describe("optional: this op's own reason, added to the batch's"),
340
+ });
341
+ server.registerTool("batch", {
342
+ description: `PREVIEW many adds, edits and voids as ONE change (writes nothing): one preview, one go, one commit. Use it for more than ~3 writes (e.g. fixes from a statement check). Each op takes the same fields as add_transaction / edit_transaction / void_transaction plus op; op statement_amount (account, statement_date, amount) saves the bank's amount for a card statement like add_statement_amount. Ops run in order; if any op fails, the whole batch is refused and the reply names the op. Max ${A.BATCH_MAX} ops.`,
343
+ inputSchema: z.object({ ops: z.array(batchOp).min(1).max(A.BATCH_MAX), reason }),
344
+ annotations: pv,
345
+ }, wrap(async ({ ops, reason }) => {
346
+ const s = await snap();
347
+ const p = poolOf(s.ledger, dashboardOf(s.files, props.login)?.prefs?.pool, now());
348
+ const list = ops.map((o, i) => {
349
+ if (o.op === "statement_amount") {
350
+ if (!o.account || !o.statement_date || o.amount === undefined)
351
+ throw new A.ActionError(`batch refused: op ${i + 1} (statement_amount) needs account, statement_date and amount`);
352
+ return o;
353
+ }
354
+ if (o.op !== "add" && !o.id)
355
+ throw new A.ActionError(`batch refused: op ${i + 1} (${o.op}) needs an id`);
356
+ if (o.op === "add" && !o.date)
357
+ throw new A.ActionError(`batch refused: op ${i + 1} (add) needs a date`);
358
+ if (o.op === "add" && !o.lines?.length && !o.account && p.on && p.default_account)
359
+ return { ...o, account: p.default_account };
360
+ return o;
361
+ });
362
+ return preview((f) => A.batch(f, list, meta(reason)), A.BATCH_MAX);
363
+ }));
330
364
  const accountFields = {
331
365
  name: z.string(),
332
366
  type: z.string().describe("cash, bank, ewallet, credit_card, bnpl, deposit, savings_goal, gold, fund, stock, etf, crypto, insurance, loan_given, loan_taken, other (or a new one)"),
@@ -410,9 +444,7 @@ export function buildServer(env, props, src = sourceFor(env), deps = {}) {
410
444
  const { ledger } = A.loadForAction(f);
411
445
  if (!ledger.accounts.some((a) => a.id === account))
412
446
  throw new A.ActionError(`no account ${account}`);
413
- const list = cardsData(ledger).statements.filter((x) => !(x.account === account && x.statement_date === statement_date));
414
- list.push({ account, statement_date, amount });
415
- list.sort((x, y) => (x.statement_date < y.statement_date ? -1 : x.statement_date > y.statement_date ? 1 : x.account < y.account ? -1 : 1));
447
+ const list = withBankAmounts(ledger, [{ account, statement_date, amount }]);
416
448
  return A.plan(f, { putFiles: { [STATEMENTS_FILE]: yamlText(list) } }, `Card statement: ${account} ${statement_date}`);
417
449
  })));
418
450
  server.registerTool("add_instalment", { description: "PREVIEW turning a card purchase into a 0% instalment plan (trả góp 0%): each statement then counts only the monthly amount.", inputSchema: z.object({ transaction: z.string().describe("id of the purchase on the card"), count: z.number().int().min(2).max(60), fee: z.number().int().min(0).optional().describe("conversion fee per month, if any"), id: z.string().optional(), name: z.string().optional(), first_statement: z.string().optional().describe("yyyy-mm-dd; default the first statement after the purchase") }), annotations: pv }, wrap(async ({ transaction, count, fee, id, name, first_statement }) => preview((f) => {
@@ -1 +1 @@
1
- export declare const VERSION = "2.1.1";
1
+ export declare const VERSION = "2.3.0";
@@ -1,2 +1,2 @@
1
1
  // generated by scripts/version.mjs
2
- export const VERSION = "2.1.1";
2
+ export const VERSION = "2.3.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ownledger",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "Personal finance ledger used through AI chat (MCP). Your data in your own private GitHub repo, served by your own Cloudflare Worker.",
5
5
  "license": "MIT",
6
6
  "author": "phucbm",