@fangcuntai/fangcuntai-mcp 1.6.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 +182 -0
- package/dist/client/api-client.d.ts +27 -0
- package/dist/client/api-client.js +102 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +20 -0
- package/dist/context/family-context.d.ts +55 -0
- package/dist/context/family-context.js +183 -0
- package/dist/context/write-lock.d.ts +31 -0
- package/dist/context/write-lock.js +87 -0
- package/dist/errors/user-messages.d.ts +2 -0
- package/dist/errors/user-messages.js +30 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +21 -0
- package/dist/tools/account.d.ts +4 -0
- package/dist/tools/account.js +14 -0
- package/dist/tools/context.d.ts +3 -0
- package/dist/tools/context.js +57 -0
- package/dist/tools/drafts.d.ts +3 -0
- package/dist/tools/drafts.js +147 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.js +14 -0
- package/dist/tools/ledger.d.ts +18 -0
- package/dist/tools/ledger.js +392 -0
- package/dist/tools/notes.d.ts +3 -0
- package/dist/tools/notes.js +307 -0
- package/dist/tools/safety.d.ts +2 -0
- package/dist/tools/safety.js +24 -0
- package/dist/utils/money.d.ts +10 -0
- package/dist/utils/money.js +28 -0
- package/dist/utils/response.d.ts +5 -0
- package/dist/utils/response.js +21 -0
- package/package.json +50 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { currentLedgerBookId, ensureFamilyId, familyPath, getWorkspaceContext, ledgerBookQuery, requireEntitlement, requireTokenScope, selectLedgerBook, TOKEN_SCOPE_READ_LEDGER, TOKEN_SCOPE_READ_STATS, TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER, } from "../context/family-context.js";
|
|
3
|
+
import { createPending, pendingPublicView, takePending, } from "../context/write-lock.js";
|
|
4
|
+
import { fenToYuan, mapItemsWithYuan, yuanToFen } from "../utils/money.js";
|
|
5
|
+
import { okJson, runTool } from "../utils/response.js";
|
|
6
|
+
const optionalBookId = z
|
|
7
|
+
.number()
|
|
8
|
+
.int()
|
|
9
|
+
.positive()
|
|
10
|
+
.optional()
|
|
11
|
+
.describe("可选;覆盖当前会话选中的账本。不传则用 select_ledger_book 的选择或默认账本");
|
|
12
|
+
function parseRecordDate(date) {
|
|
13
|
+
if (!date)
|
|
14
|
+
return Math.floor(Date.now() / 1000);
|
|
15
|
+
const d = new Date(`${date}T12:00:00`);
|
|
16
|
+
if (Number.isNaN(d.getTime()))
|
|
17
|
+
throw new Error("date 格式无效,请使用 YYYY-MM-DD");
|
|
18
|
+
return Math.floor(d.getTime() / 1000);
|
|
19
|
+
}
|
|
20
|
+
/** 准备记一笔:须先 unlock_writes。不打入账 API。 */
|
|
21
|
+
export async function prepareCreateLedgerEntry(client, input) {
|
|
22
|
+
const familyId = await ensureFamilyId(client);
|
|
23
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
24
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法记一笔");
|
|
25
|
+
const recordDate = parseRecordDate(input.date);
|
|
26
|
+
const bookId = currentLedgerBookId(input.ledger_book_id);
|
|
27
|
+
const externalOrderId = (input.external_order_id ?? "").trim();
|
|
28
|
+
if (externalOrderId) {
|
|
29
|
+
try {
|
|
30
|
+
await requireTokenScope(client, [TOKEN_SCOPE_READ_LEDGER]);
|
|
31
|
+
const existing = await client.get(familyPath(familyId, "/ledger"), {
|
|
32
|
+
page: 1,
|
|
33
|
+
page_size: 1,
|
|
34
|
+
external_order_id: externalOrderId,
|
|
35
|
+
...ledgerBookQuery(input.ledger_book_id),
|
|
36
|
+
});
|
|
37
|
+
const items = Array.isArray(existing.items) ? existing.items : [];
|
|
38
|
+
if (items.length > 0) {
|
|
39
|
+
return {
|
|
40
|
+
already_exists: true,
|
|
41
|
+
hint: "本账本已有相同外部订单号,不会重复入账。",
|
|
42
|
+
entry: items[0],
|
|
43
|
+
workspace: await getWorkspaceContext(client),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// 无查询权限或预检失败:入账时由 API 40908 去重
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const workspace = await getWorkspaceContext(client);
|
|
52
|
+
const pending = createPending("create_ledger", familyId, {
|
|
53
|
+
action: "create_ledger_entry",
|
|
54
|
+
type: input.type ?? "expense",
|
|
55
|
+
amount_yuan: input.amount_yuan,
|
|
56
|
+
category: input.category ?? "other",
|
|
57
|
+
description: input.description ?? "",
|
|
58
|
+
date: input.date ?? null,
|
|
59
|
+
currency: input.currency ?? null,
|
|
60
|
+
ledger_book_id: bookId || null,
|
|
61
|
+
tags: input.tags ?? [],
|
|
62
|
+
external_order_id: externalOrderId || null,
|
|
63
|
+
source: "desktop_mcp",
|
|
64
|
+
family_id: familyId,
|
|
65
|
+
}, {
|
|
66
|
+
type: input.type ?? "expense",
|
|
67
|
+
amount_yuan: input.amount_yuan,
|
|
68
|
+
category: input.category ?? "other",
|
|
69
|
+
description: input.description ?? "",
|
|
70
|
+
record_date: recordDate,
|
|
71
|
+
currency: input.currency,
|
|
72
|
+
ledger_book_id: bookId,
|
|
73
|
+
tags: input.tags,
|
|
74
|
+
external_order_id: externalOrderId || undefined,
|
|
75
|
+
});
|
|
76
|
+
return { ...pendingPublicView(pending), workspace };
|
|
77
|
+
}
|
|
78
|
+
/** 确认记一笔:一次性 token,POST 金额为分,并标记 source=desktop_mcp。 */
|
|
79
|
+
export async function confirmCreateLedgerEntry(client, confirmToken) {
|
|
80
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
81
|
+
const pending = takePending(confirmToken, "create_ledger");
|
|
82
|
+
const familyId = pending.familyId;
|
|
83
|
+
const exec = pending.exec;
|
|
84
|
+
const bookId = Number(exec.ledger_book_id ?? 0);
|
|
85
|
+
const payload = {
|
|
86
|
+
type: exec.type,
|
|
87
|
+
amount: yuanToFen(Number(exec.amount_yuan)),
|
|
88
|
+
category: exec.category ?? "other",
|
|
89
|
+
description: exec.description ?? "",
|
|
90
|
+
record_date: exec.record_date,
|
|
91
|
+
source: "desktop_mcp",
|
|
92
|
+
};
|
|
93
|
+
if (exec.currency)
|
|
94
|
+
payload.currency = exec.currency;
|
|
95
|
+
if (bookId > 0)
|
|
96
|
+
payload.ledger_book_id = bookId;
|
|
97
|
+
if (Array.isArray(exec.tags) && exec.tags.length)
|
|
98
|
+
payload.tags = exec.tags;
|
|
99
|
+
if (typeof exec.external_order_id === "string" && exec.external_order_id.trim()) {
|
|
100
|
+
payload.external_order_id = exec.external_order_id.trim();
|
|
101
|
+
}
|
|
102
|
+
const path = bookId > 0
|
|
103
|
+
? `${familyPath(familyId, "/ledger")}?ledger_book_id=${bookId}`
|
|
104
|
+
: familyPath(familyId, "/ledger");
|
|
105
|
+
const data = await client.post(path, payload);
|
|
106
|
+
return {
|
|
107
|
+
...data,
|
|
108
|
+
amount_yuan: exec.amount_yuan,
|
|
109
|
+
source: "desktop_mcp",
|
|
110
|
+
hint: "已入账(来源:桌面助手 MCP)。",
|
|
111
|
+
workspace: await getWorkspaceContext(client),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function registerLedgerTools(server, client) {
|
|
115
|
+
server.tool("list_ledger_books", "列出当前家庭的账本(含币种、是否默认)。切换账本前先调用,再用 select_ledger_book。", {}, async () => runTool(async () => {
|
|
116
|
+
client.setToolName("list_ledger_books");
|
|
117
|
+
const familyId = await ensureFamilyId(client);
|
|
118
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法查看账本");
|
|
119
|
+
const data = await client.get(familyPath(familyId, "/ledger-books"));
|
|
120
|
+
const workspace = await getWorkspaceContext(client);
|
|
121
|
+
return okJson({ books: data, workspace });
|
|
122
|
+
}));
|
|
123
|
+
server.tool("select_ledger_book", "选择当前会话操作的账本。ledger_book_id=0 表示回到该家庭默认账本。", {
|
|
124
|
+
ledger_book_id: z
|
|
125
|
+
.number()
|
|
126
|
+
.int()
|
|
127
|
+
.min(0)
|
|
128
|
+
.describe("账本 ID;传 0 使用默认账本"),
|
|
129
|
+
}, async ({ ledger_book_id }) => runTool(async () => {
|
|
130
|
+
client.setToolName("select_ledger_book");
|
|
131
|
+
const workspace = await selectLedgerBook(client, ledger_book_id);
|
|
132
|
+
return okJson({
|
|
133
|
+
ok: true,
|
|
134
|
+
hint: workspace.using_default_ledger_book
|
|
135
|
+
? "已使用当前家庭默认账本。"
|
|
136
|
+
: `已选中账本 ${workspace.active_ledger_book_id}。`,
|
|
137
|
+
workspace,
|
|
138
|
+
});
|
|
139
|
+
}));
|
|
140
|
+
server.tool("get_ledger_summary", "查询指定月份记账摘要(收入/支出,单位:元)。month 格式 YYYY-MM,留空为本月。", {
|
|
141
|
+
month: z.string().optional().describe("YYYY-MM,留空为本月"),
|
|
142
|
+
ledger_book_id: optionalBookId,
|
|
143
|
+
}, async ({ month, ledger_book_id }) => runTool(async () => {
|
|
144
|
+
client.setToolName("get_ledger_summary");
|
|
145
|
+
await requireTokenScope(client, [TOKEN_SCOPE_READ_STATS]);
|
|
146
|
+
const familyId = await ensureFamilyId(client);
|
|
147
|
+
const data = await client.get(familyPath(familyId, "/ledger/summary"), { month, ...ledgerBookQuery(ledger_book_id) });
|
|
148
|
+
const normalized = {
|
|
149
|
+
...data,
|
|
150
|
+
month: month ?? null,
|
|
151
|
+
total_income_yuan: fenToYuan(Number(data.total_income ?? 0)),
|
|
152
|
+
total_expense_yuan: fenToYuan(Number(data.total_expense ?? 0)),
|
|
153
|
+
workspace: await getWorkspaceContext(client),
|
|
154
|
+
};
|
|
155
|
+
return okJson(normalized);
|
|
156
|
+
}));
|
|
157
|
+
server.tool("list_ledger_entries", "列出记账流水。可按月份、类型、外部订单号筛选;金额会附带 amount_yuan(元)。", {
|
|
158
|
+
page: z.number().int().min(1).optional(),
|
|
159
|
+
page_size: z.number().int().min(1).max(100).optional(),
|
|
160
|
+
month: z.string().optional().describe("YYYY-MM"),
|
|
161
|
+
type: z.enum(["expense", "income"]).optional(),
|
|
162
|
+
ledger_book_id: optionalBookId,
|
|
163
|
+
external_order_id: z
|
|
164
|
+
.string()
|
|
165
|
+
.optional()
|
|
166
|
+
.describe("外部订单号精确查找(支付宝/微信/银行流水号等)"),
|
|
167
|
+
}, async ({ page, page_size, month, type, ledger_book_id, external_order_id }) => runTool(async () => {
|
|
168
|
+
client.setToolName("list_ledger_entries");
|
|
169
|
+
await requireTokenScope(client, [TOKEN_SCOPE_READ_LEDGER]);
|
|
170
|
+
const familyId = await ensureFamilyId(client);
|
|
171
|
+
const data = await client.get(familyPath(familyId, "/ledger"), {
|
|
172
|
+
page,
|
|
173
|
+
page_size,
|
|
174
|
+
month,
|
|
175
|
+
type,
|
|
176
|
+
external_order_id,
|
|
177
|
+
...ledgerBookQuery(ledger_book_id),
|
|
178
|
+
});
|
|
179
|
+
return okJson({
|
|
180
|
+
...mapItemsWithYuan(data),
|
|
181
|
+
workspace: await getWorkspaceContext(client),
|
|
182
|
+
});
|
|
183
|
+
}));
|
|
184
|
+
server.tool("get_ledger_trends", "查询记账趋势(按月对比)。", {
|
|
185
|
+
months: z.number().int().min(1).max(24).optional().describe("对比月数,默认由后端决定"),
|
|
186
|
+
ledger_book_id: optionalBookId,
|
|
187
|
+
}, async ({ months, ledger_book_id }) => runTool(async () => {
|
|
188
|
+
client.setToolName("get_ledger_trends");
|
|
189
|
+
await requireTokenScope(client, [TOKEN_SCOPE_READ_STATS]);
|
|
190
|
+
const familyId = await ensureFamilyId(client);
|
|
191
|
+
const data = await client.get(familyPath(familyId, "/ledger/trends"), {
|
|
192
|
+
months,
|
|
193
|
+
...ledgerBookQuery(ledger_book_id),
|
|
194
|
+
});
|
|
195
|
+
return okJson(data);
|
|
196
|
+
}));
|
|
197
|
+
server.tool("list_ledger_categories", "列出记账分类(创建记账前可查阅合法分类)。", { ledger_book_id: optionalBookId }, async ({ ledger_book_id }) => runTool(async () => {
|
|
198
|
+
client.setToolName("list_ledger_categories");
|
|
199
|
+
await requireTokenScope(client, [
|
|
200
|
+
TOKEN_SCOPE_READ_LEDGER,
|
|
201
|
+
TOKEN_SCOPE_READ_STATS,
|
|
202
|
+
TOKEN_SCOPE_WRITE_DRAFT,
|
|
203
|
+
TOKEN_SCOPE_WRITE_LEDGER,
|
|
204
|
+
]);
|
|
205
|
+
const familyId = await ensureFamilyId(client);
|
|
206
|
+
const data = await client.get(familyPath(familyId, "/ledger/categories"), {
|
|
207
|
+
...ledgerBookQuery(ledger_book_id),
|
|
208
|
+
});
|
|
209
|
+
return okJson(data);
|
|
210
|
+
}));
|
|
211
|
+
server.tool("prepare_create_ledger_entry", "准备记一笔(不入账)。须先 unlock_writes。返回 summary + confirm_token,向用户确认后再 confirm_create_ledger_entry。入账会标记 source=desktop_mcp。可传 external_order_id(外部订单号);若本账本已有相同订单号,直接返回 already_exists,不再入账。", {
|
|
212
|
+
type: z.enum(["expense", "income"]).default("expense"),
|
|
213
|
+
amount_yuan: z.number().positive().describe("金额(元)"),
|
|
214
|
+
category: z.string().optional().describe("分类 key,如 food / education"),
|
|
215
|
+
description: z.string().optional(),
|
|
216
|
+
date: z.string().optional().describe("YYYY-MM-DD,默认今天"),
|
|
217
|
+
currency: z.string().optional().describe("默认继承账本币种;传入须与账本一致"),
|
|
218
|
+
ledger_book_id: optionalBookId,
|
|
219
|
+
tags: z.array(z.string()).optional(),
|
|
220
|
+
external_order_id: z
|
|
221
|
+
.string()
|
|
222
|
+
.optional()
|
|
223
|
+
.describe("外部订单号,非必填。同一账本内已存在则不会重复入账"),
|
|
224
|
+
}, async (input) => runTool(async () => {
|
|
225
|
+
client.setToolName("prepare_create_ledger_entry");
|
|
226
|
+
return okJson(await prepareCreateLedgerEntry(client, input));
|
|
227
|
+
}));
|
|
228
|
+
server.tool("confirm_create_ledger_entry", "确认记一笔并正式入账。必须传入 prepare_create_ledger_entry 返回的 confirm_token。", {
|
|
229
|
+
confirm_token: z.string().min(1),
|
|
230
|
+
}, async ({ confirm_token }) => runTool(async () => {
|
|
231
|
+
client.setToolName("confirm_create_ledger_entry");
|
|
232
|
+
return okJson(await confirmCreateLedgerEntry(client, confirm_token));
|
|
233
|
+
}));
|
|
234
|
+
server.tool("prepare_delete_ledger_entry", "准备删除记账(移入回收站,可恢复)。须先 unlock_writes。会拉取条目摘要供用户确认。", {
|
|
235
|
+
entry_id: z.number().int().positive().describe("记账条目 ID"),
|
|
236
|
+
ledger_book_id: optionalBookId,
|
|
237
|
+
}, async ({ entry_id, ledger_book_id }) => runTool(async () => {
|
|
238
|
+
client.setToolName("prepare_delete_ledger_entry");
|
|
239
|
+
const familyId = await ensureFamilyId(client);
|
|
240
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
241
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法删除记账");
|
|
242
|
+
const bookQuery = ledgerBookQuery(ledger_book_id);
|
|
243
|
+
const entry = await client.get(familyPath(familyId, `/ledger/${entry_id}`), bookQuery);
|
|
244
|
+
const amount = Number(entry.amount ?? 0);
|
|
245
|
+
const pending = createPending("delete_ledger", familyId, {
|
|
246
|
+
action: "delete_ledger_entry",
|
|
247
|
+
entry_id,
|
|
248
|
+
type: entry.type,
|
|
249
|
+
amount_yuan: fenToYuan(amount),
|
|
250
|
+
category: entry.category,
|
|
251
|
+
category_name: entry.category_name,
|
|
252
|
+
description: entry.description,
|
|
253
|
+
record_date: entry.record_date,
|
|
254
|
+
source: entry.source ?? null,
|
|
255
|
+
note: "删除后进入回收站,可用 list_ledger_trash / restore_ledger_entry 恢复",
|
|
256
|
+
}, { entry_id, ledger_book_id: bookQuery.ledger_book_id });
|
|
257
|
+
return okJson({
|
|
258
|
+
...pendingPublicView(pending),
|
|
259
|
+
workspace: await getWorkspaceContext(client),
|
|
260
|
+
});
|
|
261
|
+
}));
|
|
262
|
+
server.tool("confirm_delete_ledger_entry", "确认删除:将记账移入回收站(非永久删除)。须传入 prepare_delete_ledger_entry 的 confirm_token。", {
|
|
263
|
+
confirm_token: z.string().min(1),
|
|
264
|
+
}, async ({ confirm_token }) => runTool(async () => {
|
|
265
|
+
client.setToolName("confirm_delete_ledger_entry");
|
|
266
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
267
|
+
const pending = takePending(confirm_token, "delete_ledger");
|
|
268
|
+
const entryId = Number(pending.exec.entry_id);
|
|
269
|
+
const bookId = pending.exec.ledger_book_id;
|
|
270
|
+
await client.delete(familyPath(pending.familyId, `/ledger/${entryId}`), {
|
|
271
|
+
...(bookId ? { ledger_book_id: bookId } : {}),
|
|
272
|
+
});
|
|
273
|
+
return okJson({
|
|
274
|
+
ok: true,
|
|
275
|
+
entry_id: entryId,
|
|
276
|
+
trashed: true,
|
|
277
|
+
hint: "已移入回收站。可用 list_ledger_trash 查看,restore_ledger_entry 恢复。",
|
|
278
|
+
workspace: await getWorkspaceContext(client),
|
|
279
|
+
});
|
|
280
|
+
}));
|
|
281
|
+
server.tool("list_ledger_trash", "查看记账回收站(已删除、可恢复的流水)。", {
|
|
282
|
+
page: z.number().int().min(1).optional(),
|
|
283
|
+
page_size: z.number().int().min(1).max(100).optional(),
|
|
284
|
+
ledger_book_id: optionalBookId,
|
|
285
|
+
}, async ({ page, page_size, ledger_book_id }) => runTool(async () => {
|
|
286
|
+
client.setToolName("list_ledger_trash");
|
|
287
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
288
|
+
const familyId = await ensureFamilyId(client);
|
|
289
|
+
const data = await client.get(familyPath(familyId, "/ledger/trash"), { page, page_size, ...ledgerBookQuery(ledger_book_id) });
|
|
290
|
+
return okJson({
|
|
291
|
+
...mapItemsWithYuan(data),
|
|
292
|
+
workspace: await getWorkspaceContext(client),
|
|
293
|
+
});
|
|
294
|
+
}));
|
|
295
|
+
server.tool("restore_ledger_entry", "从回收站恢复一笔记账。恢复属于补救操作,无需 unlock_writes。", {
|
|
296
|
+
entry_id: z.number().int().positive(),
|
|
297
|
+
ledger_book_id: optionalBookId,
|
|
298
|
+
}, async ({ entry_id, ledger_book_id }) => runTool(async () => {
|
|
299
|
+
client.setToolName("restore_ledger_entry");
|
|
300
|
+
const familyId = await ensureFamilyId(client);
|
|
301
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
302
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法恢复记账");
|
|
303
|
+
const bookId = currentLedgerBookId(ledger_book_id);
|
|
304
|
+
const path = bookId > 0
|
|
305
|
+
? `${familyPath(familyId, `/ledger/${entry_id}/restore`)}?ledger_book_id=${bookId}`
|
|
306
|
+
: familyPath(familyId, `/ledger/${entry_id}/restore`);
|
|
307
|
+
const data = await client.post(path);
|
|
308
|
+
return okJson({
|
|
309
|
+
...data,
|
|
310
|
+
hint: "已从回收站恢复。",
|
|
311
|
+
workspace: await getWorkspaceContext(client),
|
|
312
|
+
});
|
|
313
|
+
}));
|
|
314
|
+
server.tool("prepare_empty_ledger_trash", "准备清空当前账本的记账回收站(永久删除,不可恢复)。须先 unlock_writes。会返回将删除的条数供用户确认。", {
|
|
315
|
+
ledger_book_id: optionalBookId,
|
|
316
|
+
}, async ({ ledger_book_id }) => runTool(async () => {
|
|
317
|
+
client.setToolName("prepare_empty_ledger_trash");
|
|
318
|
+
const familyId = await ensureFamilyId(client);
|
|
319
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
320
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法清空回收站");
|
|
321
|
+
const bookQuery = ledgerBookQuery(ledger_book_id);
|
|
322
|
+
const trash = await client.get(familyPath(familyId, "/ledger/trash"), { page: 1, page_size: 1, ...bookQuery });
|
|
323
|
+
const count = Number(trash.total ?? 0);
|
|
324
|
+
const pending = createPending("empty_ledger_trash", familyId, {
|
|
325
|
+
action: "empty_ledger_trash",
|
|
326
|
+
count,
|
|
327
|
+
note: "将永久删除这些记账,无法恢复",
|
|
328
|
+
}, { ledger_book_id: bookQuery.ledger_book_id });
|
|
329
|
+
return okJson({
|
|
330
|
+
...pendingPublicView(pending),
|
|
331
|
+
workspace: await getWorkspaceContext(client),
|
|
332
|
+
});
|
|
333
|
+
}));
|
|
334
|
+
server.tool("confirm_empty_ledger_trash", "确认清空记账回收站(永久删除)。须传入 prepare_empty_ledger_trash 的 confirm_token。", {
|
|
335
|
+
confirm_token: z.string().min(1),
|
|
336
|
+
}, async ({ confirm_token }) => runTool(async () => {
|
|
337
|
+
client.setToolName("confirm_empty_ledger_trash");
|
|
338
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_LEDGER]);
|
|
339
|
+
const pending = takePending(confirm_token, "empty_ledger_trash");
|
|
340
|
+
const bookId = pending.exec.ledger_book_id;
|
|
341
|
+
const data = await client.delete(familyPath(pending.familyId, "/ledger/trash"), bookId ? { ledger_book_id: bookId } : undefined);
|
|
342
|
+
return okJson({
|
|
343
|
+
...data,
|
|
344
|
+
hint: "记账回收站已清空,无法恢复。",
|
|
345
|
+
workspace: await getWorkspaceContext(client),
|
|
346
|
+
});
|
|
347
|
+
}));
|
|
348
|
+
server.tool("create_ledger_draft", "创建记账草稿(不会直接入账,无需解锁)。正式入账请 unlock_writes → prepare_confirm_ai_draft → confirm_ai_draft。", {
|
|
349
|
+
type: z.enum(["expense", "income"]).default("expense"),
|
|
350
|
+
amount_yuan: z.number().positive().describe("金额(元)"),
|
|
351
|
+
category: z.string().optional().describe("分类 key,如 food / education"),
|
|
352
|
+
description: z.string().optional(),
|
|
353
|
+
date: z.string().optional().describe("YYYY-MM-DD,默认今天"),
|
|
354
|
+
currency: z.string().optional().describe("默认 CNY"),
|
|
355
|
+
ledger_book_id: optionalBookId,
|
|
356
|
+
external_order_id: z
|
|
357
|
+
.string()
|
|
358
|
+
.optional()
|
|
359
|
+
.describe("外部订单号,非必填。确认入账时同一账本内不可重复"),
|
|
360
|
+
}, async (input) => runTool(async () => {
|
|
361
|
+
client.setToolName("create_ledger_draft");
|
|
362
|
+
const familyId = await ensureFamilyId(client);
|
|
363
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT]);
|
|
364
|
+
await requireEntitlement(client, familyId, "can_use_ledger", "当前角色没有记账权限,无法创建记账草稿");
|
|
365
|
+
const recordDate = parseRecordDate(input.date);
|
|
366
|
+
const bookId = currentLedgerBookId(input.ledger_book_id);
|
|
367
|
+
const payload = {
|
|
368
|
+
type: input.type,
|
|
369
|
+
amount: yuanToFen(input.amount_yuan),
|
|
370
|
+
category: input.category ?? "other",
|
|
371
|
+
description: input.description ?? "",
|
|
372
|
+
record_date: recordDate,
|
|
373
|
+
currency: input.currency ?? "CNY",
|
|
374
|
+
source: "desktop_mcp",
|
|
375
|
+
};
|
|
376
|
+
if (bookId > 0)
|
|
377
|
+
payload.ledger_book_id = bookId;
|
|
378
|
+
if (input.external_order_id?.trim()) {
|
|
379
|
+
payload.external_order_id = input.external_order_id.trim();
|
|
380
|
+
}
|
|
381
|
+
const data = await client.post(familyPath(familyId, "/ai-drafts"), {
|
|
382
|
+
draft_type: "ledger",
|
|
383
|
+
payload,
|
|
384
|
+
session_id: "mcp",
|
|
385
|
+
});
|
|
386
|
+
return okJson({
|
|
387
|
+
...data,
|
|
388
|
+
hint: "草稿已创建。确认入账:unlock_writes → prepare_confirm_ai_draft → confirm_ai_draft;或 discard_ai_draft 丢弃。",
|
|
389
|
+
workspace: await getWorkspaceContext(client),
|
|
390
|
+
});
|
|
391
|
+
}));
|
|
392
|
+
}
|