@blackbelt-technology/pi-dashboard-extension 0.7.0 → 0.8.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.
Files changed (216) hide show
  1. package/.pi/skills/browser/SKILL.md +28 -14
  2. package/.pi/skills/browser/UPSTREAM.md +10 -1
  3. package/.pi/skills/browser/references/electron.agent.md +57 -0
  4. package/.pi/skills/browser/references/own-browser.md +299 -0
  5. package/.pi/skills/browser/references/web.agent.md +323 -0
  6. package/.pi/skills/browser/scripts/check-panerelay.sh +157 -0
  7. package/.pi/skills/doctor/SKILL.md +3 -9
  8. package/.pi/skills/doctor/_lib/checks.ts +14 -68
  9. package/.pi/skills/doctor/_lib/derive-tokens.ts +1 -0
  10. package/.pi/skills/doctor/modules/apple-tools.knowledge.hash +1 -0
  11. package/.pi/skills/doctor/modules/apple-tools.md +51 -0
  12. package/.pi/skills/doctor/modules/oauth-redirect-base.knowledge.hash +1 -0
  13. package/.pi/skills/doctor/modules/oauth-redirect-base.md +71 -0
  14. package/.pi/skills/doctor/modules/pi-resolution.knowledge.hash +1 -1
  15. package/.pi/skills/doctor/modules/pi-resolution.md +21 -1
  16. package/.pi/skills/pi-dashboard/references/api-reference.agent.md +69 -0
  17. package/.pi/skills/project-init/SKILL.md +1 -7
  18. package/.pi/skills/project-init/dox-doctrine.md +42 -0
  19. package/.pi/skills/project-init/profiles/docs/profile.json +1 -1
  20. package/README.md +19 -0
  21. package/agents/DoxTriage.md +80 -0
  22. package/package.json +22 -11
  23. package/src/agent-settled.ts +27 -9
  24. package/src/auto-session-namer.ts +486 -70
  25. package/src/autostart-guard.ts +99 -0
  26. package/src/autostart-lock.ts +258 -0
  27. package/src/bridge-context.ts +86 -5
  28. package/src/bridge-ticket-client.ts +105 -0
  29. package/src/bridge.ts +917 -213
  30. package/src/command-handler.ts +332 -57
  31. package/src/connect-target.ts +130 -0
  32. package/src/connection.ts +371 -7
  33. package/src/endpoint-resolution.ts +221 -0
  34. package/src/flow-event-wiring.ts +125 -3
  35. package/src/followup-buffer.ts +217 -0
  36. package/src/instance-verification.ts +125 -0
  37. package/src/local-token-header.ts +44 -0
  38. package/src/model-refresh.ts +95 -0
  39. package/src/notify-proxy.ts +33 -0
  40. package/src/pi-env.d.ts +4 -0
  41. package/src/pi-retry-settings.ts +91 -0
  42. package/src/provider-register.ts +86 -21
  43. package/src/remote-registration-gate.ts +102 -0
  44. package/src/retry-tracker.ts +265 -86
  45. package/src/role-manager.ts +20 -0
  46. package/src/role-model-tools.ts +38 -3
  47. package/src/server-auto-start.ts +144 -2
  48. package/src/server-pin-store.ts +92 -0
  49. package/src/session-move.ts +275 -0
  50. package/src/session-sync.ts +24 -5
  51. package/src/subagent-forward-sites.ts +69 -0
  52. package/src/subagent-frame-buffer.ts +24 -3
  53. package/src/subagent-frame-strip.ts +109 -0
  54. package/src/subagent-tick-throttle.ts +250 -0
  55. package/src/transcript-backfill.ts +151 -0
  56. package/src/transcript-request-guard.ts +76 -0
  57. package/src/transport-diagnostics.ts +76 -0
  58. package/src/turn-actionability.ts +20 -0
  59. package/src/ui-stale-guard.ts +58 -0
  60. package/.pi/skills/doctor/AGENTS.md +0 -27
  61. package/src/AGENTS.md +0 -57
  62. package/src/__tests__/abort-latch.test.ts +0 -97
  63. package/src/__tests__/agent-settled.test.ts +0 -82
  64. package/src/__tests__/artifact-roots.test.ts +0 -83
  65. package/src/__tests__/ask-user-attachments.test.ts +0 -106
  66. package/src/__tests__/ask-user-schema-discriminator.test.ts +0 -141
  67. package/src/__tests__/ask-user-tool.test.ts +0 -571
  68. package/src/__tests__/auto-session-namer.test.ts +0 -339
  69. package/src/__tests__/bridge-default-model-apply.test.ts +0 -242
  70. package/src/__tests__/bridge-default-model-gate.test.ts +0 -50
  71. package/src/__tests__/bridge-entry-id-pi-070.test.ts +0 -174
  72. package/src/__tests__/bridge-followup-chat-order.test.ts +0 -215
  73. package/src/__tests__/bridge-followup-multi-entry.test.ts +0 -202
  74. package/src/__tests__/bridge-followup-mutation.test.ts +0 -217
  75. package/src/__tests__/bridge-followup-queue-drain.test.ts +0 -266
  76. package/src/__tests__/bridge-hasui-flip.test.ts +0 -109
  77. package/src/__tests__/bridge-no-queue-mutation.test.ts +0 -112
  78. package/src/__tests__/bridge-plugin-emit-event.test.ts +0 -44
  79. package/src/__tests__/bridge-queue-update-forward.test.ts +0 -77
  80. package/src/__tests__/bridge-resume-disconnect.test.ts +0 -333
  81. package/src/__tests__/bridge-shadow-queue-gate.test.ts +0 -299
  82. package/src/__tests__/bridge-shutdown-reset.test.ts +0 -159
  83. package/src/__tests__/bridge-slash-command-routing.test.ts +0 -458
  84. package/src/__tests__/bridge-stop-after-turn.test.ts +0 -72
  85. package/src/__tests__/bridge-system-followup.test.ts +0 -163
  86. package/src/__tests__/bridge-thinking-level-select.test.ts +0 -68
  87. package/src/__tests__/browser-skill-registered.test.ts +0 -84
  88. package/src/__tests__/build-provider-catalogue.test.ts +0 -210
  89. package/src/__tests__/command-handler.test.ts +0 -1180
  90. package/src/__tests__/commit-draft.test.ts +0 -100
  91. package/src/__tests__/connection-dropped-frames.test.ts +0 -78
  92. package/src/__tests__/connection-suppress-auto-start.test.ts +0 -97
  93. package/src/__tests__/connection.test.ts +0 -344
  94. package/src/__tests__/credentials-updated.test.ts +0 -26
  95. package/src/__tests__/custom-provider-apikey-roundtrip.test.ts +0 -128
  96. package/src/__tests__/cwd-missing-probe.test.ts +0 -82
  97. package/src/__tests__/dashboard-commands-shipped.test.ts +0 -53
  98. package/src/__tests__/dashboard-context-injector.test.ts +0 -190
  99. package/src/__tests__/dashboard-default-adapter.test.ts +0 -77
  100. package/src/__tests__/dev-build.test.ts +0 -79
  101. package/src/__tests__/doctor/checks.test.ts +0 -121
  102. package/src/__tests__/doctor/modules.test.ts +0 -64
  103. package/src/__tests__/doctor/provenance-hash.test.ts +0 -94
  104. package/src/__tests__/doctor/router.test.ts +0 -143
  105. package/src/__tests__/empty-actionable-guard-config.test.ts +0 -32
  106. package/src/__tests__/empty-actionable-guard.test.ts +0 -73
  107. package/src/__tests__/enrich-model-metadata.test.ts +0 -222
  108. package/src/__tests__/event-forwarder.test.ts +0 -119
  109. package/src/__tests__/extension-slash-command-detection.test.ts +0 -143
  110. package/src/__tests__/filter-enabled-models.test.ts +0 -151
  111. package/src/__tests__/fixtures/turn-actionability-fixtures.ts +0 -69
  112. package/src/__tests__/fork-entryid-timing.test.ts +0 -88
  113. package/src/__tests__/git-info-status.test.ts +0 -73
  114. package/src/__tests__/git-link-builder.test.ts +0 -102
  115. package/src/__tests__/git-poll.test.ts +0 -69
  116. package/src/__tests__/markdown-image-inliner.test.ts +0 -392
  117. package/src/__tests__/model-resolve.test.ts +0 -379
  118. package/src/__tests__/multiselect-dashboard-routing.test.ts +0 -203
  119. package/src/__tests__/multiselect-list.test.ts +0 -137
  120. package/src/__tests__/multiselect-polyfill.test.ts +0 -92
  121. package/src/__tests__/no-session-replacement-calls.test.ts +0 -99
  122. package/src/__tests__/no-tui-multiselect-arm-regression.test.ts +0 -81
  123. package/src/__tests__/openspec-activity-detector.test.ts +0 -337
  124. package/src/__tests__/openspec-cli-shim.test.ts +0 -145
  125. package/src/__tests__/openspec-poller.test.ts +0 -124
  126. package/src/__tests__/pi-version-tracker.test.ts +0 -109
  127. package/src/__tests__/process-metrics.test.ts +0 -47
  128. package/src/__tests__/process-scanner-kill.test.ts +0 -61
  129. package/src/__tests__/process-scanner.test.ts +0 -292
  130. package/src/__tests__/project-init-detect-stack.test.ts +0 -61
  131. package/src/__tests__/project-init-dox-kb-config.test.ts +0 -53
  132. package/src/__tests__/project-init-profiles.test.ts +0 -84
  133. package/src/__tests__/project-init-scaffold.test.ts +0 -139
  134. package/src/__tests__/project-init-seed-doctrine.test.ts +0 -100
  135. package/src/__tests__/project-trust.test.ts +0 -92
  136. package/src/__tests__/prompt-bus-wiring.test.ts +0 -791
  137. package/src/__tests__/prompt-bus.test.ts +0 -513
  138. package/src/__tests__/prompt-expander.test.ts +0 -398
  139. package/src/__tests__/provider-register-native-models.test.ts +0 -182
  140. package/src/__tests__/provider-register-reload.test.ts +0 -498
  141. package/src/__tests__/provider-register-thinking-levels.test.ts +0 -104
  142. package/src/__tests__/retry-tracker.test.ts +0 -152
  143. package/src/__tests__/role-manager.test.ts +0 -585
  144. package/src/__tests__/role-model-tools-registry-readiness.test.ts +0 -114
  145. package/src/__tests__/role-model-tools.test.ts +0 -229
  146. package/src/__tests__/search-files-completeness.test.ts +0 -111
  147. package/src/__tests__/search-files-ranking.test.ts +0 -168
  148. package/src/__tests__/server-auto-start.test.ts +0 -375
  149. package/src/__tests__/server-launcher-launch.test.ts +0 -96
  150. package/src/__tests__/server-launcher.test.ts +0 -101
  151. package/src/__tests__/server-probe.test.ts +0 -25
  152. package/src/__tests__/session-switch.test.ts +0 -139
  153. package/src/__tests__/session-sync.test.ts +0 -243
  154. package/src/__tests__/single-derivation-guard.test.ts +0 -39
  155. package/src/__tests__/source-detector.test.ts +0 -88
  156. package/src/__tests__/stats-extractor.test.ts +0 -92
  157. package/src/__tests__/subagent-frame-buffer.test.ts +0 -239
  158. package/src/__tests__/tool-result-image-inliner.test.ts +0 -206
  159. package/src/__tests__/tui-prompt-adapter.test.ts +0 -213
  160. package/src/__tests__/turn-actionability.test.ts +0 -86
  161. package/src/__tests__/ui-decorators.test.ts +0 -309
  162. package/src/__tests__/ui-modules.test.ts +0 -293
  163. package/src/__tests__/vcs-info.test.ts +0 -265
  164. package/src/__tests__/visibility-intent.test.ts +0 -43
  165. package/src/__tests__/watchdog.test.ts +0 -161
  166. package/src/abort-latch.ts.AGENTS.md +0 -3
  167. package/src/agent-settled.ts.AGENTS.md +0 -3
  168. package/src/artifact-roots.ts.AGENTS.md +0 -3
  169. package/src/ask-user-attachments.ts.AGENTS.md +0 -3
  170. package/src/ask-user-tool.ts.AGENTS.md +0 -3
  171. package/src/auto-session-namer.ts.AGENTS.md +0 -3
  172. package/src/bridge-context.ts.AGENTS.md +0 -3
  173. package/src/bridge-default-model-gate.ts.AGENTS.md +0 -3
  174. package/src/bridge.ts.AGENTS.md +0 -3
  175. package/src/command-handler.ts.AGENTS.md +0 -3
  176. package/src/commit-draft-agent.ts.AGENTS.md +0 -3
  177. package/src/commit-draft.ts.AGENTS.md +0 -3
  178. package/src/connection.ts.AGENTS.md +0 -3
  179. package/src/dashboard-context-injector.ts.AGENTS.md +0 -3
  180. package/src/dashboard-default-adapter.ts.AGENTS.md +0 -3
  181. package/src/dev-build.ts.AGENTS.md +0 -3
  182. package/src/empty-actionable-guard-config.ts.AGENTS.md +0 -3
  183. package/src/empty-actionable-guard.ts.AGENTS.md +0 -3
  184. package/src/event-forwarder.ts.AGENTS.md +0 -3
  185. package/src/flow-event-wiring.ts.AGENTS.md +0 -3
  186. package/src/git-link-builder.ts.AGENTS.md +0 -3
  187. package/src/git-poll.ts.AGENTS.md +0 -3
  188. package/src/hasui-flip.ts.AGENTS.md +0 -3
  189. package/src/markdown-image-inliner.ts.AGENTS.md +0 -3
  190. package/src/model-tracker.ts.AGENTS.md +0 -3
  191. package/src/multiselect-decode.ts.AGENTS.md +0 -3
  192. package/src/multiselect-list.ts.AGENTS.md +0 -3
  193. package/src/multiselect-polyfill.ts.AGENTS.md +0 -3
  194. package/src/openspec-cli-shim.ts.AGENTS.md +0 -7
  195. package/src/process-metrics.ts.AGENTS.md +0 -3
  196. package/src/process-scanner.ts.AGENTS.md +0 -3
  197. package/src/project-init/AGENTS.md +0 -12
  198. package/src/project-trust.ts.AGENTS.md +0 -3
  199. package/src/prompt-bus.ts.AGENTS.md +0 -3
  200. package/src/prompt-expander.ts.AGENTS.md +0 -3
  201. package/src/provider-register.ts.AGENTS.md +0 -3
  202. package/src/retry-tracker.ts.AGENTS.md +0 -3
  203. package/src/role-manager.ts.AGENTS.md +0 -3
  204. package/src/role-model-tools.ts.AGENTS.md +0 -3
  205. package/src/server-auto-start.ts.AGENTS.md +0 -3
  206. package/src/server-launcher.ts.AGENTS.md +0 -3
  207. package/src/session-sync.ts.AGENTS.md +0 -3
  208. package/src/slash-dispatch.ts.AGENTS.md +0 -3
  209. package/src/source-detector.ts.AGENTS.md +0 -3
  210. package/src/subagent-frame-buffer.ts.AGENTS.md +0 -3
  211. package/src/tool-result-image-inliner.ts.AGENTS.md +0 -3
  212. package/src/tui-prompt-adapter.ts.AGENTS.md +0 -3
  213. package/src/turn-actionability.ts.AGENTS.md +0 -3
  214. package/src/ui-modules.ts.AGENTS.md +0 -3
  215. package/src/vcs-info.ts.AGENTS.md +0 -3
  216. package/src/visibility-intent.ts.AGENTS.md +0 -3
@@ -1,571 +0,0 @@
1
- import { describe, it, expect, vi } from "vitest";
2
-
3
- // Mock modules before importing
4
- vi.mock("typebox", () => ({
5
- Type: {
6
- Object: vi.fn(() => ({})),
7
- String: vi.fn(() => ({})),
8
- Optional: vi.fn((x: any) => x),
9
- Array: vi.fn(() => ({})),
10
- Union: vi.fn(() => ({})),
11
- Literal: vi.fn(() => ({})),
12
- },
13
- }));
14
-
15
- vi.mock("@earendil-works/pi-ai", () => ({
16
- StringEnum: vi.fn(() => ({})),
17
- }));
18
-
19
- import { registerAskUserTool } from "../ask-user-tool.js";
20
-
21
- function createMockPi() {
22
- return {
23
- registerTool: vi.fn(),
24
- };
25
- }
26
-
27
- describe("registerAskUserTool", () => {
28
- it("registers ask_user tool", () => {
29
- const pi = createMockPi();
30
- registerAskUserTool(pi as any);
31
-
32
- expect(pi.registerTool).toHaveBeenCalledTimes(1);
33
- expect(pi.registerTool.mock.calls[0][0].name).toBe("ask_user");
34
- });
35
-
36
- it("registers with correct methods", () => {
37
- const pi = createMockPi();
38
- registerAskUserTool(pi as any);
39
-
40
- const tool = pi.registerTool.mock.calls[0][0];
41
- expect(tool.name).toBe("ask_user");
42
- expect(tool.execute).toBeTypeOf("function");
43
- expect(tool.promptGuidelines).toBeDefined();
44
- expect(tool.promptGuidelines.length).toBeGreaterThan(0);
45
- });
46
-
47
- it("description instructs agents not to add a Select all option", () => {
48
- const pi = createMockPi();
49
- registerAskUserTool(pi as any);
50
- const tool = pi.registerTool.mock.calls[0][0];
51
- expect(tool.description).toMatch(/UI provides a Select all/i);
52
- });
53
-
54
- describe("message passthrough", () => {
55
- function getToolAndMockCtx() {
56
- const pi = createMockPi();
57
- registerAskUserTool(pi as any);
58
- const tool = pi.registerTool.mock.calls[0][0];
59
- // `custom` stands in for the multiselect polyfill: it invokes the factory
60
- // with a `done` callback; the factory-returned component exposes
61
- // onConfirm/onCancel. We auto-confirm with ["A"] to preserve the legacy
62
- // mock return value that the multiselect assertions expected.
63
- const custom = vi.fn().mockImplementation(async (factory: any) => {
64
- return await new Promise<unknown>((resolve) => {
65
- const component: any = factory({}, {}, {}, (r: unknown) => resolve(r));
66
- component?.onConfirm?.(["A"]);
67
- });
68
- });
69
- const ctx = {
70
- ui: {
71
- confirm: vi.fn().mockResolvedValue(true),
72
- select: vi.fn().mockResolvedValue("A"),
73
- input: vi.fn().mockResolvedValue("hello"),
74
- custom,
75
- },
76
- };
77
- return { tool, ctx, custom };
78
- }
79
-
80
- it("passes message through opts for input", async () => {
81
- const { tool, ctx } = getToolAndMockCtx();
82
- await tool.execute("id", { method: "input", title: "Q", message: "Details here" }, undefined, undefined, ctx);
83
- // toolCallId is also threaded through opts since change
84
- // `fix-interactive-ui-reorder`. Asserts both fields without
85
- // pinning property order.
86
- expect(ctx.ui.input).toHaveBeenCalledWith(
87
- "Q",
88
- undefined,
89
- expect.objectContaining({ message: "Details here", toolCallId: "id" }),
90
- );
91
- });
92
-
93
- it("passes message through opts for select", async () => {
94
- const { tool, ctx } = getToolAndMockCtx();
95
- await tool.execute("id", { method: "select", title: "Pick", message: "Context", options: ["A", "B"] }, undefined, undefined, ctx);
96
- expect(ctx.ui.select).toHaveBeenCalledWith(
97
- "Pick",
98
- ["A", "B"],
99
- expect.objectContaining({ message: "Context", toolCallId: "id" }),
100
- );
101
- });
102
-
103
- it("dispatches multiselect through the polyfill via ctx.ui.custom", async () => {
104
- const { tool, ctx } = getToolAndMockCtx();
105
- const result = await tool.execute(
106
- "id",
107
- { method: "multiselect", title: "Multi", message: "Info", options: ["A"] },
108
- undefined,
109
- undefined,
110
- ctx,
111
- );
112
- // Polyfill routes via custom(factory); multiselect is not called directly.
113
- expect(ctx.ui.custom).toHaveBeenCalledTimes(1);
114
- expect(result.details.method).toBe("multiselect");
115
- expect(result.details.result).toEqual(["A"]);
116
- });
117
-
118
- it("passes only toolCallId through opts when message is undefined", async () => {
119
- const { tool, ctx } = getToolAndMockCtx();
120
- await tool.execute("id", { method: "input", title: "Q" }, undefined, undefined, ctx);
121
- // Even with no `message`, the wrapper still attaches toolCallId so
122
- // the resulting prompt_request can be paired by the client reducer.
123
- expect(ctx.ui.input).toHaveBeenCalledWith(
124
- "Q",
125
- undefined,
126
- expect.objectContaining({ toolCallId: "id" }),
127
- );
128
- });
129
-
130
- it("falls back to message when title is missing", async () => {
131
- const { tool, ctx } = getToolAndMockCtx();
132
- await tool.execute("id", { method: "input", message: "Detailed question" }, undefined, undefined, ctx);
133
- expect(ctx.ui.input).toHaveBeenCalledWith(
134
- "Detailed question",
135
- undefined,
136
- expect.objectContaining({ message: "Detailed question", toolCallId: "id" }),
137
- );
138
- });
139
-
140
- it("falls back to 'Question' when both title and message are missing", async () => {
141
- const { tool, ctx } = getToolAndMockCtx();
142
- await tool.execute("id", { method: "confirm" }, undefined, undefined, ctx);
143
- // confirm now also threads toolCallId via 3rd arg.
144
- expect(ctx.ui.confirm).toHaveBeenCalledWith(
145
- "Question",
146
- "",
147
- expect.objectContaining({ toolCallId: "id" }),
148
- );
149
- });
150
-
151
- it("parses options from JSON string", async () => {
152
- const { tool, ctx } = getToolAndMockCtx();
153
- await tool.execute("id", { method: "select", title: "Pick", options: '["A", "B"]' }, undefined, undefined, ctx);
154
- // No message, no other opts — only toolCallId.
155
- expect(ctx.ui.select).toHaveBeenCalledWith(
156
- "Pick",
157
- ["A", "B"],
158
- expect.objectContaining({ toolCallId: "id" }),
159
- );
160
- });
161
-
162
- it("throws when select reaches execute with unparseable options string", async () => {
163
- const { tool, ctx } = getToolAndMockCtx();
164
- await expect(
165
- tool.execute("id", { method: "select", title: "Pick", options: "not json" }, undefined, undefined, ctx),
166
- ).rejects.toThrow(/options/i);
167
- expect(ctx.ui.select).not.toHaveBeenCalled();
168
- });
169
-
170
- it("throws when select is invoked with empty options array", async () => {
171
- const { tool, ctx } = getToolAndMockCtx();
172
- await expect(
173
- tool.execute("id", { method: "select", title: "Pick", options: [] }, undefined, undefined, ctx),
174
- ).rejects.toThrow(/options.*input/is);
175
- expect(ctx.ui.select).not.toHaveBeenCalled();
176
- });
177
-
178
- it("throws when multiselect is invoked without options", async () => {
179
- const { tool, ctx } = getToolAndMockCtx();
180
- await expect(
181
- tool.execute("id", { method: "multiselect", title: "Pick" }, undefined, undefined, ctx),
182
- ).rejects.toThrow(/options/i);
183
- expect(ctx.ui.custom).not.toHaveBeenCalled();
184
- });
185
- });
186
-
187
- describe("image attachments (multiline paste)", () => {
188
- function getTool() {
189
- const pi = createMockPi();
190
- registerAskUserTool(pi as any);
191
- return pi.registerTool.mock.calls[0][0];
192
- }
193
-
194
- it("standalone input prefers ctx.ui.inputWithImages and surfaces attachment paths", async () => {
195
- const tool = getTool();
196
- const inputWithImages = vi.fn().mockResolvedValue({
197
- value: "see attached",
198
- attachments: [{ path: "/home/u/.pi/dashboard/attachments/s/abc.png", mimeType: "image/png", bytes: 42 }],
199
- });
200
- const ctx = { ui: { inputWithImages, input: vi.fn().mockResolvedValue("unused") } };
201
- const res = await tool.execute("id", { method: "input", title: "Paste" }, undefined, undefined, ctx);
202
- expect(inputWithImages).toHaveBeenCalledTimes(1);
203
- expect(ctx.ui.input).not.toHaveBeenCalled();
204
- expect(res.content[0].text).toContain('"attachments"');
205
- expect(res.content[0].text).toContain("abc.png");
206
- expect(res.details.result).toEqual({
207
- value: "see attached",
208
- attachments: [{ path: "/home/u/.pi/dashboard/attachments/s/abc.png", mimeType: "image/png", bytes: 42 }],
209
- });
210
- });
211
-
212
- it("standalone input falls back to ctx.ui.input (text-only) when inputWithImages absent", async () => {
213
- const tool = getTool();
214
- const ctx = { ui: { input: vi.fn().mockResolvedValue("plain text") } };
215
- const res = await tool.execute("id", { method: "input", title: "Q" }, undefined, undefined, ctx);
216
- expect(ctx.ui.input).toHaveBeenCalledTimes(1);
217
- expect(res.content[0].text).toBe('User responded: "plain text"');
218
- });
219
-
220
- it("batch input answer with attachments flows into the summary + details", async () => {
221
- const tool = getTool();
222
- const batch = vi.fn().mockResolvedValue([
223
- { value: "the error", attachments: [{ path: "/a/x.png", mimeType: "image/png", bytes: 9 }] },
224
- { confirmed: true },
225
- ]);
226
- const ctx = { ui: { batch } };
227
- const res = await tool.execute(
228
- "id",
229
- { method: "batch", title: "Qs", questions: [
230
- { method: "input", title: "Paste the error" },
231
- { method: "confirm", title: "Proceed?" },
232
- ] },
233
- undefined,
234
- undefined,
235
- ctx,
236
- );
237
- expect(batch).toHaveBeenCalledTimes(1);
238
- // input entry carries the {value, attachments} object; confirm stays boolean.
239
- expect(res.details.results[0]).toEqual({ value: "the error", attachments: [{ path: "/a/x.png", mimeType: "image/png", bytes: 9 }] });
240
- expect(res.details.results[1]).toBe(true);
241
- expect(res.content[0].text).toContain("x.png");
242
- });
243
- });
244
-
245
- describe("prepareArguments", () => {
246
- function getTool() {
247
- const pi = createMockPi();
248
- registerAskUserTool(pi as any);
249
- return pi.registerTool.mock.calls[0][0];
250
- }
251
-
252
- it("leaves empty {} args untouched (no synthetic method) so schema rejection still fires", () => {
253
- // Regression test for the Opus-emits-empty-args bug seen in session 019dd05c.
254
- // Our rescue layer must NOT silently fabricate a method/title when there is
255
- // nothing to rescue — the framework's schema validator must still reject {}
256
- // so the model is forced to retry with valid args.
257
- const tool = getTool();
258
- const result = tool.prepareArguments({});
259
- expect(result.method).toBeUndefined();
260
- expect(result.title).toBeUndefined();
261
- expect(result.questions).toBeUndefined();
262
- expect(Object.keys(result).filter((k) => k !== "__normalizations")).toHaveLength(0);
263
- });
264
-
265
- it("parses stringified options array", () => {
266
- const tool = getTool();
267
- const result = tool.prepareArguments({ method: "select", title: "Pick", options: '["A", "B"]' });
268
- expect(result.options).toEqual(["A", "B"]);
269
- });
270
-
271
- it("leaves real array options unchanged", () => {
272
- const tool = getTool();
273
- const result = tool.prepareArguments({ method: "select", title: "Pick", options: ["A", "B"] });
274
- expect(result.options).toEqual(["A", "B"]);
275
- });
276
-
277
- it("leaves malformed string as-is", () => {
278
- const tool = getTool();
279
- const result = tool.prepareArguments({ method: "select", title: "Pick", options: "not json" });
280
- expect(result.options).toBe("not json");
281
- });
282
-
283
- it("unwraps stringified params wrapper", () => {
284
- const tool = getTool();
285
- const result = tool.prepareArguments({
286
- method: "select",
287
- params: '{"title":"X","options":["a","b"]}',
288
- });
289
- expect(result.method).toBe("select");
290
- expect(result.title).toBe("X");
291
- expect(result.options).toEqual(["a", "b"]);
292
- expect(result.params).toBeUndefined();
293
- });
294
-
295
- it("unwraps object-form params wrapper", () => {
296
- const tool = getTool();
297
- const result = tool.prepareArguments({
298
- method: "select",
299
- params: { title: "X", options: ["a", "b"] },
300
- });
301
- expect(result.method).toBe("select");
302
- expect(result.title).toBe("X");
303
- expect(result.options).toEqual(["a", "b"]);
304
- expect(result.params).toBeUndefined();
305
- });
306
-
307
- it("copies question into title when title is absent", () => {
308
- const tool = getTool();
309
- const result = tool.prepareArguments({ method: "input", question: "Your name?" });
310
- expect(result.title).toBe("Your name?");
311
- });
312
-
313
- it("does not overwrite explicit title with question", () => {
314
- const tool = getTool();
315
- const result = tool.prepareArguments({ method: "input", title: "T", question: "Q" });
316
- expect(result.title).toBe("T");
317
- });
318
-
319
- it("top-level fields win over params wrapper", () => {
320
- const tool = getTool();
321
- const result = tool.prepareArguments({
322
- method: "select",
323
- title: "OuterTitle",
324
- params: { title: "InnerTitle", options: ["a", "b"] },
325
- });
326
- expect(result.title).toBe("OuterTitle");
327
- expect(result.options).toEqual(["a", "b"]);
328
- });
329
-
330
- it("rescues options JSON string inside params wrapper", () => {
331
- const tool = getTool();
332
- const result = tool.prepareArguments({
333
- method: "select",
334
- params: '{"title":"X","options":"[\\"a\\",\\"b\\"]"}',
335
- });
336
- expect(result.options).toEqual(["a", "b"]);
337
- });
338
-
339
- // ── batch rescue ────────────────────────────────────────────────
340
-
341
- it("parses stringified questions array and synthesizes method=batch", () => {
342
- const tool = getTool();
343
- const result = tool.prepareArguments({
344
- questions:
345
- '[{"title":"Pick","method":"select","options":["a","b"]}]',
346
- });
347
- expect(result.method).toBe("batch");
348
- expect(Array.isArray(result.questions)).toBe(true);
349
- expect(result.questions).toHaveLength(1);
350
- expect(result.title).toBe("Pick");
351
- });
352
-
353
- it("backfills missing outer title on explicit method=batch call", () => {
354
- const tool = getTool();
355
- const result = tool.prepareArguments({
356
- method: "batch",
357
- questions: [
358
- { method: "confirm", question: "Proceed?" },
359
- { method: "select", question: "Scope?", options: ["A", "B"] },
360
- ],
361
- });
362
- expect(result.title).toBe("Proceed?");
363
- expect(result.questions[0].title).toBe("Proceed?"); // sub-question rename also fired
364
- expect(result.questions[1].title).toBe("Scope?");
365
- });
366
-
367
- it("bare questions array with no method synthesizes method=batch and pulls title", () => {
368
- const tool = getTool();
369
- const result = tool.prepareArguments({
370
- questions: [{ method: "confirm", title: "Proceed?" }],
371
- });
372
- expect(result.method).toBe("batch");
373
- expect(result.title).toBe("Proceed?");
374
- });
375
-
376
- it("pulls title from question or header if sub-question lacks title", () => {
377
- const tool = getTool();
378
- const result = tool.prepareArguments({
379
- questions: [{ method: "input", question: "Your name?" }],
380
- });
381
- expect(result.method).toBe("batch");
382
- expect(result.title).toBe("Your name?");
383
- });
384
-
385
- it("flattens input_type wrapper inside a sub-question", () => {
386
- const tool = getTool();
387
- const result = tool.prepareArguments({
388
- method: "batch",
389
- title: "T",
390
- questions: [
391
- {
392
- title: "Pick",
393
- input_type: { method: "select", options: ["a", "b"] },
394
- },
395
- ],
396
- });
397
- const sq = result.questions[0];
398
- expect(sq.method).toBe("select");
399
- expect(sq.options).toEqual(["a", "b"]);
400
- expect(sq.input_type).toBeUndefined();
401
- });
402
-
403
- it("converts {label, value} options to labels and records a warning", () => {
404
- const tool = getTool();
405
- const result = tool.prepareArguments({
406
- method: "batch",
407
- title: "T",
408
- questions: [
409
- {
410
- method: "select",
411
- title: "Pick",
412
- options: [
413
- { label: "Sync now", value: "sync" },
414
- { label: "Skip", value: "skip" },
415
- ],
416
- },
417
- ],
418
- });
419
- expect(result.questions[0].options).toEqual(["Sync now", "Skip"]);
420
- const warnings = (result as any).__normalizations as string[];
421
- expect(warnings).toBeDefined();
422
- expect(warnings.length).toBeGreaterThan(0);
423
- expect(warnings[0]).toMatch(/label.*value/);
424
- });
425
-
426
- it("renames sub-question header to title", () => {
427
- const tool = getTool();
428
- const result = tool.prepareArguments({
429
- method: "batch",
430
- title: "T",
431
- questions: [{ method: "input", header: "Enter name" }],
432
- });
433
- expect(result.questions[0].title).toBe("Enter name");
434
- });
435
- });
436
-
437
- describe("batch execution", () => {
438
- function getToolAndMockCtx(batchImpl?: any) {
439
- const pi = createMockPi();
440
- registerAskUserTool(pi as any);
441
- const tool = pi.registerTool.mock.calls[0][0];
442
- const batch =
443
- batchImpl ??
444
- vi.fn().mockResolvedValue([
445
- { value: "hello" },
446
- { value: "A" },
447
- { confirmed: true },
448
- ]);
449
- const ctx = {
450
- ui: {
451
- confirm: vi.fn().mockResolvedValue(true),
452
- select: vi.fn().mockResolvedValue("A"),
453
- input: vi.fn().mockResolvedValue("hello"),
454
- custom: vi.fn(),
455
- batch,
456
- },
457
- };
458
- return { tool, ctx };
459
- }
460
-
461
- it("issues exactly ONE ctx.ui.batch request with all questions (no per-question dispatch)", async () => {
462
- const { tool, ctx } = getToolAndMockCtx();
463
- const result = await tool.execute(
464
- "id",
465
- {
466
- method: "batch",
467
- title: "Setup",
468
- questions: [
469
- { method: "input", title: "Name?" },
470
- { method: "select", title: "Lang?", options: ["TS", "Py"] },
471
- { method: "confirm", title: "Init git?" },
472
- ],
473
- },
474
- undefined,
475
- undefined,
476
- ctx,
477
- );
478
- expect(ctx.ui.batch).toHaveBeenCalledTimes(1);
479
- // No per-question dispatch.
480
- expect(ctx.ui.input).not.toHaveBeenCalled();
481
- expect(ctx.ui.select).not.toHaveBeenCalled();
482
- expect(ctx.ui.confirm).not.toHaveBeenCalled();
483
- // questions[] passed through to the single request.
484
- const passedQuestions = ctx.ui.batch.mock.calls[0][1];
485
- expect(passedQuestions).toHaveLength(3);
486
- expect(passedQuestions[1]).toMatchObject({ method: "select", options: ["TS", "Py"] });
487
- // Answers mapped index-aligned to the legacy result shape.
488
- expect(result.details.method).toBe("batch");
489
- expect(result.details.results).toEqual(["hello", "A", true]);
490
- expect(result.details.cancelled).toBe(false);
491
- });
492
-
493
- it("maps a multiselect sub-question answer to {values} → string[]", async () => {
494
- const batch = vi.fn().mockResolvedValue([{ values: ["ESLint", "Vitest"] }]);
495
- const { tool, ctx } = getToolAndMockCtx(batch);
496
- const result = await tool.execute(
497
- "id",
498
- {
499
- method: "batch",
500
- title: "Setup",
501
- questions: [{ method: "multiselect", title: "Tooling", options: ["ESLint", "Vitest", "Husky"] }],
502
- },
503
- undefined,
504
- undefined,
505
- ctx,
506
- );
507
- expect(result.details.results).toEqual([["ESLint", "Vitest"]]);
508
- });
509
-
510
- it("returns a cancel summary with cancelled=true when ctx.ui.batch resolves undefined", async () => {
511
- const batch = vi.fn().mockResolvedValue(undefined);
512
- const { tool, ctx } = getToolAndMockCtx(batch);
513
- const result = await tool.execute(
514
- "id",
515
- {
516
- method: "batch",
517
- title: "T",
518
- questions: [
519
- { method: "input", title: "Q1" },
520
- { method: "select", title: "Q2", options: ["a", "b"] },
521
- ],
522
- },
523
- undefined,
524
- undefined,
525
- ctx,
526
- );
527
- expect(result.details.cancelled).toBe(true);
528
- expect(result.details.results).toEqual([]);
529
- expect(result.content[0].text).toMatch(/cancelled batch/i);
530
- });
531
-
532
- it("surfaces __normalizations warnings in details.warnings", async () => {
533
- const { tool, ctx } = getToolAndMockCtx();
534
- const prepared = tool.prepareArguments({
535
- method: "batch",
536
- title: "T",
537
- questions: [
538
- {
539
- method: "select",
540
- title: "Pick",
541
- options: [
542
- { label: "A", value: "a" },
543
- { label: "B", value: "b" },
544
- ],
545
- },
546
- ],
547
- });
548
- const result = await tool.execute("id", prepared, undefined, undefined, ctx);
549
- expect(result.details.warnings).toBeDefined();
550
- expect(result.details.warnings.length).toBeGreaterThan(0);
551
- expect(result.details.warnings[0]).toMatch(/label.*value/);
552
- });
553
-
554
- it("throws if a batch sub-question is select with empty options", async () => {
555
- const { tool, ctx } = getToolAndMockCtx();
556
- await expect(
557
- tool.execute(
558
- "id",
559
- {
560
- method: "batch",
561
- title: "T",
562
- questions: [{ method: "select", title: "Pick", options: [] }],
563
- },
564
- undefined,
565
- undefined,
566
- ctx,
567
- ),
568
- ).rejects.toThrow(/options/i);
569
- });
570
- });
571
- });