@goodpeanuts/dsh-sui 0.1.2-fix.1 → 0.1.2-fix.2

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 CHANGED
@@ -45,7 +45,7 @@ npx --yes --package=@goodpeanuts/dsh-sui feidee sync recent --book '上一步返
45
45
 
46
46
  ### 会话草稿与待入账
47
47
 
48
- 会话输入框上方的“账本授权”用于选择当前会话可访问的账本,即使还没有草稿也会显示。点击后选择账本并“确认授权”;可勾选设为默认,供尚未授权的会话自动使用。再次打开可切换当前会话账本或清除默认账本;清除默认不影响已经授权的会话。切换前须先处理当前草稿,防止旧草稿失去授权。没有可选账本时,先打开账本页面登录并同步。
48
+ 在账本页面的“数据与同步 → 账本授权”选择默认账本,勾选后续会话自动使用并确认。普通会话没有常驻授权按钮,也不会因打开会话而获得授权。未设置默认账本时,首次需要账本访问的财务工具会通过宿主原生提问请求用户选择账本,可选择以后自动使用或仅本次会话;跳过、取消或自定义文本不会授予访问。后续使用默认账本无需重复确认。可在授权设置中停止为新会话自动授权;已有会话授权与草稿不受影响。草稿工作台内提供“切换账本”,切换前须先处理当前非空草稿。没有可选账本时,先登录并同步。实际入账仍需单独人工确认。
49
49
 
50
50
  模型通过受限 finance 工具读取账本、维护当前会话的一份流水草稿。用户和 Agent 共同编辑同一份字段;缺项由必填字段和校验结果派生,未知信息通过宿主提问,不写入草稿问题状态。
51
51
 
@@ -0,0 +1,96 @@
1
+ import { FinancePublicError } from "../packages/finance/src/public-error.mjs";
2
+ import { createFinanceTrustedControlRoute } from "../packages/finance-module/src/routes.mjs";
3
+ import "../packages/finance-module/src/index.mjs";
4
+ import "../packages/finance-module/src/tool-contracts.mjs";
5
+ //#region src/capabilities/book-authorization.ts
6
+ /** Human-owned host questions are the only source of a contextual authorization. */
7
+ function createBookAuthorization(options) {
8
+ const pending = /* @__PURE__ */ new Map();
9
+ const lifetime = new AbortController();
10
+ const authorize = (context, exec) => {
11
+ const existing = pending.get(context.subjectId);
12
+ if (existing) return existing;
13
+ const operation = requestAuthorization(context, exec).finally(() => {
14
+ if (pending.get(context.subjectId) === operation) pending.delete(context.subjectId);
15
+ });
16
+ pending.set(context.subjectId, operation);
17
+ return operation;
18
+ };
19
+ const requestAuthorization = async (context, exec) => {
20
+ const ask = options.questions();
21
+ if (!ask) throw failure("finance_grant_missing", "请打开账本页面的“数据与同步 → 账本授权”设置默认账本,再继续当前任务。");
22
+ const signal = exec.signal ? AbortSignal.any([exec.signal, lifetime.signal]) : lifetime.signal;
23
+ signal.throwIfAborted();
24
+ const { service, interaction } = options.runtime();
25
+ const books = service.describeBooks(context).books;
26
+ if (!Array.isArray(books) || !books.length) throw failure("finance_grant_missing", "尚无可用账本,请先在账本页面登录并同步,再设置账本授权。");
27
+ const route = createFinanceTrustedControlRoute(service, interaction);
28
+ const base = `/v1/finance/subjects/${context.subjectId}/grant/selection`;
29
+ const selection = (await route.handle({
30
+ method: "POST",
31
+ pathname: base,
32
+ query: {},
33
+ body: async () => ({})
34
+ })).body;
35
+ const respond = async (body) => route.handle({
36
+ method: "POST",
37
+ pathname: `${base}/${selection.request.id}`,
38
+ query: {},
39
+ body: async () => body
40
+ });
41
+ const labels = selection.books.map((book, index) => {
42
+ const name = book.name?.trim() || "未命名账本";
43
+ return selection.books.filter((other) => (other.name?.trim() || "未命名账本") === name).length > 1 ? `${name}(${index + 1})` : name;
44
+ });
45
+ try {
46
+ const answer = await ask({
47
+ ...exec.agent ? { agent: exec.agent } : {},
48
+ signal,
49
+ questions: [{
50
+ id: "book",
51
+ header: "账本授权",
52
+ question: "本次任务使用哪个账本?授权后可读取账本并维护草稿,实际入账仍需人工确认。",
53
+ options: labels.map((label) => ({ label }))
54
+ }, {
55
+ id: "remember",
56
+ header: "后续使用",
57
+ question: "后续会话使用财务功能时,是否自动使用所选账本?",
58
+ options: [{
59
+ label: "以后自动使用",
60
+ description: "设置为默认账本,之后使用财务功能时无需重复授权。"
61
+ }, {
62
+ label: "仅本次会话",
63
+ description: "只授权当前会话,保留原有默认设置。"
64
+ }]
65
+ }]
66
+ });
67
+ signal.throwIfAborted();
68
+ const chosen = answer.answers.find((item) => item.id === "book");
69
+ const remember = answer.answers.find((item) => item.id === "remember");
70
+ const index = chosen?.selected.length === 1 ? labels.indexOf(chosen.selected[0]) : -1;
71
+ if (index < 0 || chosen?.custom || remember?.custom || remember?.selected.length !== 1 || !["以后自动使用", "仅本次会话"].includes(remember.selected[0])) throw failure("finance_grant_cancelled", "用户没有完成账本授权,本次操作已停止。不要重复请求授权;等待用户明确要求继续。");
72
+ if ((await respond({
73
+ decision: "confirm",
74
+ bookId: selection.books[index].id,
75
+ makeDefault: remember.selected[0] === "以后自动使用"
76
+ }))?.status !== 200) throw failure("finance_grant_changed", "账本授权已变化,请重新确认当前会话使用的账本。");
77
+ } catch (error) {
78
+ await respond({ decision: "cancel" }).catch(() => void 0);
79
+ throw error;
80
+ }
81
+ };
82
+ return {
83
+ authorize,
84
+ dispose() {
85
+ lifetime.abort();
86
+ }
87
+ };
88
+ }
89
+ function failure(code, message) {
90
+ return new FinancePublicError(JSON.stringify({
91
+ code,
92
+ message
93
+ }));
94
+ }
95
+ //#endregion
96
+ export { createBookAuthorization };
@@ -3,7 +3,7 @@ import { financeCallContext } from "../session/call-context.mjs";
3
3
  /** Wire-format tool names every provider route accepts (OpenAI function-name charset). */
4
4
  const TOOL_NAME_PATTERN = /^[A-Za-z0-9_-]+$/;
5
5
  /** Project only the explicitly exported business tool set. */
6
- function projectFinanceTools(definitions) {
6
+ function projectFinanceTools(definitions, authorize) {
7
7
  const names = /* @__PURE__ */ new Set();
8
8
  return definitions.map((definition) => {
9
9
  if (!TOOL_NAME_PATTERN.test(definition.name) || names.has(definition.name)) throw new Error("finance_tool_name_invalid");
@@ -23,7 +23,17 @@ function projectFinanceTools(definitions) {
23
23
  },
24
24
  async execute(args, exec) {
25
25
  const context = financeCallContext(definition.name, exec);
26
- return await definition.execute(context, args);
26
+ try {
27
+ return await definition.execute(context, args);
28
+ } catch (error) {
29
+ let code;
30
+ try {
31
+ code = JSON.parse(error instanceof Error ? error.message : "").code;
32
+ } catch {}
33
+ if (!authorize || code !== "finance_grant_missing" && code !== "finance_grant_book_mismatch") throw error;
34
+ await authorize(context, exec);
35
+ return await definition.execute(context, args);
36
+ }
27
37
  }
28
38
  };
29
39
  });