@emmaneugene/pi-cursor-sdk 0.4.3 → 0.5.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 (36) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +19 -39
  3. package/dist/bundled-context-windows.js +6 -13
  4. package/dist/context.js +0 -3
  5. package/dist/cursor-config.js +0 -3
  6. package/dist/cursor-provider-turn-prepare.js +0 -1
  7. package/dist/cursor-state.js +3 -16
  8. package/dist/index.js +0 -2
  9. package/dist/model-discovery.js +11 -47
  10. package/docs/cursor-dogfood-checklist.md +1 -1
  11. package/docs/cursor-live-smoke-checklist.md +1 -2
  12. package/docs/cursor-model-ux-spec.md +80 -151
  13. package/docs/cursor-native-tool-replay.md +2 -3
  14. package/docs/cursor-native-tool-visual-audit.md +1 -1
  15. package/docs/cursor-testing-lessons.md +1 -1
  16. package/docs/cursor-tool-surfaces.md +3 -6
  17. package/docs/platform-smoke-implementation.md +1 -1
  18. package/docs/platform-smoke.md +1 -1
  19. package/package.json +1 -1
  20. package/scripts/isolated-cursor-smoke.sh +1 -1
  21. package/scripts/lib/local-resume-smoke-harness.mjs +2 -1
  22. package/scripts/local-resume-smoke.mjs +1 -1
  23. package/scripts/platform-smoke/card-detect.mjs +1 -1
  24. package/scripts/refresh-cursor-model-snapshots.mjs +3 -3
  25. package/shared/cursor-model-selection-identities.d.mts +1 -4
  26. package/shared/cursor-model-selection-identities.mjs +21 -74
  27. package/src/bundled-context-windows.ts +6 -13
  28. package/src/context.ts +1 -5
  29. package/src/cursor-config.ts +9 -13
  30. package/src/cursor-provider-turn-prepare.ts +0 -1
  31. package/src/cursor-state.ts +3 -22
  32. package/src/index.ts +0 -3
  33. package/src/model-discovery.ts +16 -62
  34. package/dist/cursor-question-tool.js +0 -194
  35. package/node_modules/cross-spawn/node_modules/which/CHANGELOG.md +0 -166
  36. package/src/cursor-question-tool.ts +0 -267
@@ -1,194 +0,0 @@
1
- import { Text } from "@earendil-works/pi-tui";
2
- import { Type } from "typebox";
3
- import { arePiToolsDisabled } from "./cursor-active-tools.js";
4
- import { parseEnvBoolean } from "./cursor-env-boolean.js";
5
- import { isCursorModel } from "./cursor-model.js";
6
- import { registerCursorModelLifecycle } from "./cursor-model-lifecycle.js";
7
- import { resolveCursorPiToolBridgeEnabled } from "./cursor-pi-tool-bridge-env.js";
8
- export const CURSOR_ASK_QUESTION_TOOL_NAME = "cursor_ask_question";
9
- export const CURSOR_ASK_QUESTION_ENV = "PI_CURSOR_ASK_QUESTION";
10
- export function resolveCursorAskQuestionEnabled(env = process.env) {
11
- return parseEnvBoolean(env[CURSOR_ASK_QUESTION_ENV], true);
12
- }
13
- /** Package-namespaced event while `cursor_ask_question` awaits pi UI input. */
14
- export const CURSOR_ASK_QUESTION_BLOCKED_EVENT = "pi-cursor-sdk:ask-question:blocked";
15
- const QuestionOptionSchema = Type.Union([
16
- Type.String(),
17
- Type.Object({
18
- label: Type.String({ description: "User-facing option label" }),
19
- value: Type.Optional(Type.String({ description: "Optional value returned to Cursor; defaults to label" })),
20
- description: Type.Optional(Type.String({ description: "Optional helper text shown by compatible pi UIs" })),
21
- }),
22
- ]);
23
- const QuestionSchema = Type.Object({
24
- id: Type.Optional(Type.String({ description: "Stable question identifier" })),
25
- question: Type.Optional(Type.String({ description: "Question to ask the user" })),
26
- prompt: Type.Optional(Type.String({ description: "Alias for question" })),
27
- options: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Choices the user can select" })),
28
- choices: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Alias for options" })),
29
- allowCustom: Type.Optional(Type.Boolean({ description: "Allow a typed answer in addition to listed options; defaults to true" })),
30
- });
31
- const CursorAskQuestionParamsSchema = Type.Object({
32
- question: Type.Optional(Type.String({ description: "Question to ask the user" })),
33
- prompt: Type.Optional(Type.String({ description: "Alias for question" })),
34
- options: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Choices the user can select" })),
35
- choices: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Alias for options" })),
36
- allowCustom: Type.Optional(Type.Boolean({ description: "Allow a typed answer in addition to listed options; defaults to true" })),
37
- questions: Type.Optional(Type.Array(QuestionSchema, { description: "Ask multiple questions sequentially" })),
38
- });
39
- function normalizeOption(option, index) {
40
- if (typeof option === "string") {
41
- const trimmed = option.trim();
42
- return trimmed ? { label: trimmed, value: trimmed } : undefined;
43
- }
44
- const label = option.label?.trim() || option.value?.trim() || `Option ${index + 1}`;
45
- return {
46
- label,
47
- value: option.value?.trim() || label,
48
- ...(option.description?.trim() ? { description: option.description.trim() } : {}),
49
- };
50
- }
51
- function normalizeOptions(options) {
52
- return (options ?? []).map(normalizeOption).filter((option) => option !== undefined);
53
- }
54
- function normalizeQuestion(raw, index) {
55
- const question = raw.question?.trim() || raw.prompt?.trim();
56
- if (!question)
57
- return undefined;
58
- return {
59
- id: raw.id?.trim() || `question_${index + 1}`,
60
- question,
61
- options: normalizeOptions(raw.options ?? raw.choices),
62
- allowCustom: raw.allowCustom !== false,
63
- };
64
- }
65
- function normalizeQuestions(params) {
66
- const rawQuestions = Array.isArray(params.questions) && params.questions.length > 0 ? params.questions : [params];
67
- return rawQuestions.map(normalizeQuestion).filter((question) => question !== undefined);
68
- }
69
- function summarizeAnswers(answers) {
70
- if (answers.length === 0)
71
- return "No answer was collected.";
72
- if (answers.length === 1) {
73
- const [answer] = answers;
74
- return answer.cancelled || answer.answer === null ? "User cancelled the question." : `User answered: ${answer.answer}`;
75
- }
76
- return [
77
- "User answered:",
78
- ...answers.map((answer) => {
79
- const value = answer.cancelled || answer.answer === null ? "cancelled" : answer.answer;
80
- return `- ${answer.id}: ${value}`;
81
- }),
82
- ].join("\n");
83
- }
84
- function buildDetails(questions, answers, uiAvailable) {
85
- return {
86
- questions,
87
- answers,
88
- uiAvailable,
89
- cancelled: answers.some((answer) => answer.cancelled),
90
- };
91
- }
92
- async function askOneQuestion(question, ctx) {
93
- if (question.options.length > 0) {
94
- const labels = question.options.map((option) => option.description ? `${option.label} — ${option.description}` : option.label);
95
- const customLabel = "Type a custom answer";
96
- const choices = question.allowCustom ? [...labels, customLabel] : labels;
97
- const selected = await ctx.ui.select(question.question, choices);
98
- if (!selected) {
99
- return { id: question.id, question: question.question, answer: null, wasCustom: false, cancelled: true };
100
- }
101
- if (selected === customLabel) {
102
- const customAnswer = await ctx.ui.input(question.question, "Type your answer");
103
- const trimmed = customAnswer?.trim();
104
- return trimmed
105
- ? { id: question.id, question: question.question, answer: trimmed, value: trimmed, wasCustom: true, cancelled: false }
106
- : { id: question.id, question: question.question, answer: null, wasCustom: true, cancelled: true };
107
- }
108
- const selectedIndex = labels.indexOf(selected);
109
- const selectedOption = selectedIndex >= 0 ? question.options[selectedIndex] : undefined;
110
- const answer = selectedOption?.label ?? selected;
111
- return {
112
- id: question.id,
113
- question: question.question,
114
- answer,
115
- value: selectedOption?.value ?? answer,
116
- wasCustom: false,
117
- cancelled: false,
118
- };
119
- }
120
- const answer = await ctx.ui.input(question.question, "Type your answer");
121
- const trimmed = answer?.trim();
122
- return trimmed
123
- ? { id: question.id, question: question.question, answer: trimmed, value: trimmed, wasCustom: true, cancelled: false }
124
- : { id: question.id, question: question.question, answer: null, wasCustom: true, cancelled: true };
125
- }
126
- function syncCursorQuestionToolForModel(pi, model) {
127
- const activeToolNames = new Set(pi.getActiveTools());
128
- const shouldBeActive = !arePiToolsDisabled(pi) && isCursorModel(model) && resolveCursorPiToolBridgeEnabled();
129
- const alreadyActive = activeToolNames.has(CURSOR_ASK_QUESTION_TOOL_NAME);
130
- if (shouldBeActive === alreadyActive)
131
- return;
132
- if (shouldBeActive) {
133
- activeToolNames.add(CURSOR_ASK_QUESTION_TOOL_NAME);
134
- }
135
- else {
136
- activeToolNames.delete(CURSOR_ASK_QUESTION_TOOL_NAME);
137
- }
138
- pi.setActiveTools([...activeToolNames]);
139
- }
140
- function emitCursorAskQuestionBlockedEvent(pi, payload) {
141
- pi.events.emit(CURSOR_ASK_QUESTION_BLOCKED_EVENT, payload);
142
- }
143
- export function registerCursorQuestionTool(pi) {
144
- if (!resolveCursorAskQuestionEnabled())
145
- return;
146
- pi.registerTool({
147
- name: CURSOR_ASK_QUESTION_TOOL_NAME,
148
- label: "Cursor question",
149
- description: "Ask the user a clarifying question from Cursor. Use when user preferences materially affect the next step; provide options when possible.",
150
- promptSnippet: "Ask the user a clarifying question through pi UI when material choices affect Cursor's next step",
151
- executionMode: "sequential",
152
- parameters: CursorAskQuestionParamsSchema,
153
- promptGuidelines: [
154
- "Use cursor_ask_question only when running a Cursor model and user input would materially change the plan, scope, platform, or implementation path.",
155
- "Prefer cursor_ask_question with 2-4 concrete options instead of guessing when Cursor plan mode needs user choices.",
156
- ],
157
- async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
158
- const questions = normalizeQuestions(params);
159
- if (questions.length === 0) {
160
- throw new Error("No valid question was provided.");
161
- }
162
- if (!ctx.hasUI) {
163
- throw new Error("Cannot ask the user because pi UI is unavailable. Make a reasonable default choice and state the assumption before proceeding.");
164
- }
165
- // Emit a package-namespaced blocked signal while the questionnaire
166
- // awaits input so consumers (e.g. Herdr) can map it to blocked/working.
167
- emitCursorAskQuestionBlockedEvent(pi, { active: true });
168
- try {
169
- const answers = [];
170
- for (const question of questions) {
171
- const answer = await askOneQuestion(question, ctx);
172
- answers.push(answer);
173
- if (answer.cancelled)
174
- break;
175
- }
176
- return {
177
- content: [{ type: "text", text: summarizeAnswers(answers) }],
178
- details: buildDetails(questions, answers, true),
179
- };
180
- }
181
- finally {
182
- emitCursorAskQuestionBlockedEvent(pi, { active: false });
183
- }
184
- },
185
- renderCall(args, theme) {
186
- const questions = normalizeQuestions(args);
187
- const label = questions[0]?.question ?? "Ask the user";
188
- return new Text(theme.fg("toolTitle", theme.bold("cursor question ")) + theme.fg("muted", label), 0, 0);
189
- },
190
- });
191
- registerCursorModelLifecycle(pi, (ctx) => {
192
- syncCursorQuestionToolForModel(pi, ctx.model);
193
- });
194
- }
@@ -1,166 +0,0 @@
1
- # Changes
2
-
3
-
4
- ## 2.0.2
5
-
6
- * Rename bin to `node-which`
7
-
8
- ## 2.0.1
9
-
10
- * generate changelog and publish on version bump
11
- * enforce 100% test coverage
12
- * Promise interface
13
-
14
- ## 2.0.0
15
-
16
- * Parallel tests, modern JavaScript, and drop support for node < 8
17
-
18
- ## 1.3.1
19
-
20
- * update deps
21
- * update travis
22
-
23
- ## v1.3.0
24
-
25
- * Add nothrow option to which.sync
26
- * update tap
27
-
28
- ## v1.2.14
29
-
30
- * appveyor: drop node 5 and 0.x
31
- * travis-ci: add node 6, drop 0.x
32
-
33
- ## v1.2.13
34
-
35
- * test: Pass missing option to pass on windows
36
- * update tap
37
- * update isexe to 2.0.0
38
- * neveragain.tech pledge request
39
-
40
- ## v1.2.12
41
-
42
- * Removed unused require
43
-
44
- ## v1.2.11
45
-
46
- * Prevent changelog script from being included in package
47
-
48
- ## v1.2.10
49
-
50
- * Use env.PATH only, not env.Path
51
-
52
- ## v1.2.9
53
-
54
- * fix for paths starting with ../
55
- * Remove unused `is-absolute` module
56
-
57
- ## v1.2.8
58
-
59
- * bullet items in changelog that contain (but don't start with) #
60
-
61
- ## v1.2.7
62
-
63
- * strip 'update changelog' changelog entries out of changelog
64
-
65
- ## v1.2.6
66
-
67
- * make the changelog bulleted
68
-
69
- ## v1.2.5
70
-
71
- * make a changelog, and keep it up to date
72
- * don't include tests in package
73
- * Properly handle relative-path executables
74
- * appveyor
75
- * Attach error code to Not Found error
76
- * Make tests pass on Windows
77
-
78
- ## v1.2.4
79
-
80
- * Fix typo
81
-
82
- ## v1.2.3
83
-
84
- * update isexe, fix regression in pathExt handling
85
-
86
- ## v1.2.2
87
-
88
- * update deps, use isexe module, test windows
89
-
90
- ## v1.2.1
91
-
92
- * Sometimes windows PATH entries are quoted
93
- * Fixed a bug in the check for group and user mode bits. This bug was introduced during refactoring for supporting strict mode.
94
- * doc cli
95
-
96
- ## v1.2.0
97
-
98
- * Add support for opt.all and -as cli flags
99
- * test the bin
100
- * update travis
101
- * Allow checking for multiple programs in bin/which
102
- * tap 2
103
-
104
- ## v1.1.2
105
-
106
- * travis
107
- * Refactored and fixed undefined error on Windows
108
- * Support strict mode
109
-
110
- ## v1.1.1
111
-
112
- * test +g exes against secondary groups, if available
113
- * Use windows exe semantics on cygwin & msys
114
- * cwd should be first in path on win32, not last
115
- * Handle lower-case 'env.Path' on Windows
116
- * Update docs
117
- * use single-quotes
118
-
119
- ## v1.1.0
120
-
121
- * Add tests, depend on is-absolute
122
-
123
- ## v1.0.9
124
-
125
- * which.js: root is allowed to execute files owned by anyone
126
-
127
- ## v1.0.8
128
-
129
- * don't use graceful-fs
130
-
131
- ## v1.0.7
132
-
133
- * add license to package.json
134
-
135
- ## v1.0.6
136
-
137
- * isc license
138
-
139
- ## 1.0.5
140
-
141
- * Awful typo
142
-
143
- ## 1.0.4
144
-
145
- * Test for path absoluteness properly
146
- * win: Allow '' as a pathext if cmd has a . in it
147
-
148
- ## 1.0.3
149
-
150
- * Remove references to execPath
151
- * Make `which.sync()` work on Windows by honoring the PATHEXT variable.
152
- * Make `isExe()` always return true on Windows.
153
- * MIT
154
-
155
- ## 1.0.2
156
-
157
- * Only files can be exes
158
-
159
- ## 1.0.1
160
-
161
- * Respect the PATHEXT env for win32 support
162
- * should 0755 the bin
163
- * binary
164
- * guts
165
- * package
166
- * 1st
@@ -1,267 +0,0 @@
1
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { Text } from "@earendil-works/pi-tui";
3
- import { Type } from "typebox";
4
- import { arePiToolsDisabled } from "./cursor-active-tools.js";
5
- import { parseEnvBoolean } from "./cursor-env-boolean.js";
6
- import { isCursorModel } from "./cursor-model.js";
7
- import { registerCursorModelLifecycle, type CursorModelLifecycleExtensionApi } from "./cursor-model-lifecycle.js";
8
- import { resolveCursorPiToolBridgeEnabled } from "./cursor-pi-tool-bridge-env.js";
9
-
10
- export const CURSOR_ASK_QUESTION_TOOL_NAME = "cursor_ask_question";
11
- export const CURSOR_ASK_QUESTION_ENV = "PI_CURSOR_ASK_QUESTION";
12
-
13
- export function resolveCursorAskQuestionEnabled(env: Record<string, string | undefined> = process.env): boolean {
14
- return parseEnvBoolean(env[CURSOR_ASK_QUESTION_ENV], true);
15
- }
16
-
17
- /** Package-namespaced event while `cursor_ask_question` awaits pi UI input. */
18
- export const CURSOR_ASK_QUESTION_BLOCKED_EVENT = "pi-cursor-sdk:ask-question:blocked";
19
-
20
- export interface CursorAskQuestionBlockedEventPayload {
21
- active: boolean;
22
- }
23
-
24
- interface CursorQuestionOption {
25
- label: string;
26
- value: string;
27
- description?: string;
28
- }
29
-
30
- interface CursorQuestion {
31
- id: string;
32
- question: string;
33
- options: CursorQuestionOption[];
34
- allowCustom: boolean;
35
- }
36
-
37
- interface CursorQuestionAnswer {
38
- id: string;
39
- question: string;
40
- answer: string | null;
41
- value?: string;
42
- wasCustom: boolean;
43
- cancelled: boolean;
44
- }
45
-
46
- interface CursorQuestionDetails {
47
- questions: CursorQuestion[];
48
- answers: CursorQuestionAnswer[];
49
- uiAvailable: boolean;
50
- cancelled: boolean;
51
- }
52
-
53
- interface CursorQuestionToolExtensionApi
54
- extends Pick<ExtensionAPI, "getActiveTools" | "registerTool" | "setActiveTools" | "events">,
55
- CursorModelLifecycleExtensionApi {}
56
-
57
- type RawQuestionOption = string | { label?: string; value?: string; description?: string };
58
-
59
- type RawQuestion = {
60
- id?: string;
61
- question?: string;
62
- prompt?: string;
63
- options?: RawQuestionOption[];
64
- choices?: RawQuestionOption[];
65
- allowCustom?: boolean;
66
- };
67
-
68
- type CursorAskQuestionParams = RawQuestion & {
69
- questions?: RawQuestion[];
70
- };
71
-
72
- const QuestionOptionSchema = Type.Union([
73
- Type.String(),
74
- Type.Object({
75
- label: Type.String({ description: "User-facing option label" }),
76
- value: Type.Optional(Type.String({ description: "Optional value returned to Cursor; defaults to label" })),
77
- description: Type.Optional(Type.String({ description: "Optional helper text shown by compatible pi UIs" })),
78
- }),
79
- ]);
80
-
81
- const QuestionSchema = Type.Object({
82
- id: Type.Optional(Type.String({ description: "Stable question identifier" })),
83
- question: Type.Optional(Type.String({ description: "Question to ask the user" })),
84
- prompt: Type.Optional(Type.String({ description: "Alias for question" })),
85
- options: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Choices the user can select" })),
86
- choices: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Alias for options" })),
87
- allowCustom: Type.Optional(Type.Boolean({ description: "Allow a typed answer in addition to listed options; defaults to true" })),
88
- });
89
-
90
- const CursorAskQuestionParamsSchema = Type.Object({
91
- question: Type.Optional(Type.String({ description: "Question to ask the user" })),
92
- prompt: Type.Optional(Type.String({ description: "Alias for question" })),
93
- options: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Choices the user can select" })),
94
- choices: Type.Optional(Type.Array(QuestionOptionSchema, { description: "Alias for options" })),
95
- allowCustom: Type.Optional(Type.Boolean({ description: "Allow a typed answer in addition to listed options; defaults to true" })),
96
- questions: Type.Optional(Type.Array(QuestionSchema, { description: "Ask multiple questions sequentially" })),
97
- });
98
-
99
- function normalizeOption(option: RawQuestionOption, index: number): CursorQuestionOption | undefined {
100
- if (typeof option === "string") {
101
- const trimmed = option.trim();
102
- return trimmed ? { label: trimmed, value: trimmed } : undefined;
103
- }
104
- const label = option.label?.trim() || option.value?.trim() || `Option ${index + 1}`;
105
- return {
106
- label,
107
- value: option.value?.trim() || label,
108
- ...(option.description?.trim() ? { description: option.description.trim() } : {}),
109
- };
110
- }
111
-
112
- function normalizeOptions(options: RawQuestionOption[] | undefined): CursorQuestionOption[] {
113
- return (options ?? []).map(normalizeOption).filter((option): option is CursorQuestionOption => option !== undefined);
114
- }
115
-
116
- function normalizeQuestion(raw: RawQuestion, index: number): CursorQuestion | undefined {
117
- const question = raw.question?.trim() || raw.prompt?.trim();
118
- if (!question) return undefined;
119
- return {
120
- id: raw.id?.trim() || `question_${index + 1}`,
121
- question,
122
- options: normalizeOptions(raw.options ?? raw.choices),
123
- allowCustom: raw.allowCustom !== false,
124
- };
125
- }
126
-
127
- function normalizeQuestions(params: CursorAskQuestionParams): CursorQuestion[] {
128
- const rawQuestions = Array.isArray(params.questions) && params.questions.length > 0 ? params.questions : [params];
129
- return rawQuestions.map(normalizeQuestion).filter((question): question is CursorQuestion => question !== undefined);
130
- }
131
-
132
- function summarizeAnswers(answers: CursorQuestionAnswer[]): string {
133
- if (answers.length === 0) return "No answer was collected.";
134
- if (answers.length === 1) {
135
- const [answer] = answers;
136
- return answer.cancelled || answer.answer === null ? "User cancelled the question." : `User answered: ${answer.answer}`;
137
- }
138
- return [
139
- "User answered:",
140
- ...answers.map((answer) => {
141
- const value = answer.cancelled || answer.answer === null ? "cancelled" : answer.answer;
142
- return `- ${answer.id}: ${value}`;
143
- }),
144
- ].join("\n");
145
- }
146
-
147
- function buildDetails(questions: CursorQuestion[], answers: CursorQuestionAnswer[], uiAvailable: boolean): CursorQuestionDetails {
148
- return {
149
- questions,
150
- answers,
151
- uiAvailable,
152
- cancelled: answers.some((answer) => answer.cancelled),
153
- };
154
- }
155
-
156
- async function askOneQuestion(question: CursorQuestion, ctx: { ui: ExtensionContext["ui"] }): Promise<CursorQuestionAnswer> {
157
- if (question.options.length > 0) {
158
- const labels = question.options.map((option) => option.description ? `${option.label} — ${option.description}` : option.label);
159
- const customLabel = "Type a custom answer";
160
- const choices = question.allowCustom ? [...labels, customLabel] : labels;
161
- const selected = await ctx.ui.select(question.question, choices);
162
- if (!selected) {
163
- return { id: question.id, question: question.question, answer: null, wasCustom: false, cancelled: true };
164
- }
165
- if (selected === customLabel) {
166
- const customAnswer = await ctx.ui.input(question.question, "Type your answer");
167
- const trimmed = customAnswer?.trim();
168
- return trimmed
169
- ? { id: question.id, question: question.question, answer: trimmed, value: trimmed, wasCustom: true, cancelled: false }
170
- : { id: question.id, question: question.question, answer: null, wasCustom: true, cancelled: true };
171
- }
172
- const selectedIndex = labels.indexOf(selected);
173
- const selectedOption = selectedIndex >= 0 ? question.options[selectedIndex] : undefined;
174
- const answer = selectedOption?.label ?? selected;
175
- return {
176
- id: question.id,
177
- question: question.question,
178
- answer,
179
- value: selectedOption?.value ?? answer,
180
- wasCustom: false,
181
- cancelled: false,
182
- };
183
- }
184
-
185
- const answer = await ctx.ui.input(question.question, "Type your answer");
186
- const trimmed = answer?.trim();
187
- return trimmed
188
- ? { id: question.id, question: question.question, answer: trimmed, value: trimmed, wasCustom: true, cancelled: false }
189
- : { id: question.id, question: question.question, answer: null, wasCustom: true, cancelled: true };
190
- }
191
-
192
- function syncCursorQuestionToolForModel(pi: Pick<ExtensionAPI, "getActiveTools" | "setActiveTools">, model: ExtensionContext["model"]): void {
193
- const activeToolNames = new Set(pi.getActiveTools());
194
- const shouldBeActive = !arePiToolsDisabled(pi) && isCursorModel(model) && resolveCursorPiToolBridgeEnabled();
195
- const alreadyActive = activeToolNames.has(CURSOR_ASK_QUESTION_TOOL_NAME);
196
- if (shouldBeActive === alreadyActive) return;
197
- if (shouldBeActive) {
198
- activeToolNames.add(CURSOR_ASK_QUESTION_TOOL_NAME);
199
- } else {
200
- activeToolNames.delete(CURSOR_ASK_QUESTION_TOOL_NAME);
201
- }
202
- pi.setActiveTools([...activeToolNames]);
203
- }
204
-
205
- function emitCursorAskQuestionBlockedEvent(
206
- pi: Pick<ExtensionAPI, "events">,
207
- payload: CursorAskQuestionBlockedEventPayload,
208
- ): void {
209
- pi.events.emit(CURSOR_ASK_QUESTION_BLOCKED_EVENT, payload);
210
- }
211
-
212
- export function registerCursorQuestionTool(pi: CursorQuestionToolExtensionApi): void {
213
- if (!resolveCursorAskQuestionEnabled()) return;
214
-
215
- pi.registerTool({
216
- name: CURSOR_ASK_QUESTION_TOOL_NAME,
217
- label: "Cursor question",
218
- description:
219
- "Ask the user a clarifying question from Cursor. Use when user preferences materially affect the next step; provide options when possible.",
220
- promptSnippet: "Ask the user a clarifying question through pi UI when material choices affect Cursor's next step",
221
- executionMode: "sequential",
222
- parameters: CursorAskQuestionParamsSchema,
223
- promptGuidelines: [
224
- "Use cursor_ask_question only when running a Cursor model and user input would materially change the plan, scope, platform, or implementation path.",
225
- "Prefer cursor_ask_question with 2-4 concrete options instead of guessing when Cursor plan mode needs user choices.",
226
- ],
227
- async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
228
- const questions = normalizeQuestions(params as CursorAskQuestionParams);
229
- if (questions.length === 0) {
230
- throw new Error("No valid question was provided.");
231
- }
232
- if (!ctx.hasUI) {
233
- throw new Error(
234
- "Cannot ask the user because pi UI is unavailable. Make a reasonable default choice and state the assumption before proceeding.",
235
- );
236
- }
237
-
238
- // Emit a package-namespaced blocked signal while the questionnaire
239
- // awaits input so consumers (e.g. Herdr) can map it to blocked/working.
240
- emitCursorAskQuestionBlockedEvent(pi, { active: true });
241
- try {
242
- const answers: CursorQuestionAnswer[] = [];
243
- for (const question of questions) {
244
- const answer = await askOneQuestion(question, ctx);
245
- answers.push(answer);
246
- if (answer.cancelled) break;
247
- }
248
-
249
- return {
250
- content: [{ type: "text" as const, text: summarizeAnswers(answers) }],
251
- details: buildDetails(questions, answers, true),
252
- };
253
- } finally {
254
- emitCursorAskQuestionBlockedEvent(pi, { active: false });
255
- }
256
- },
257
- renderCall(args, theme) {
258
- const questions = normalizeQuestions(args as CursorAskQuestionParams);
259
- const label = questions[0]?.question ?? "Ask the user";
260
- return new Text(theme.fg("toolTitle", theme.bold("cursor question ")) + theme.fg("muted", label), 0, 0);
261
- },
262
- });
263
-
264
- registerCursorModelLifecycle(pi, (ctx) => {
265
- syncCursorQuestionToolForModel(pi, ctx.model);
266
- });
267
- }