ripperdoc 0.2.3__py3-none-any.whl → 0.2.4__py3-none-any.whl

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.
@@ -0,0 +1,433 @@
1
+ """Ask user question tool for interactive clarification.
2
+
3
+ This tool allows the AI to ask the user questions during execution,
4
+ enabling clarification of ambiguous instructions, gathering preferences,
5
+ and making decisions on implementation choices.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import asyncio
11
+ from textwrap import dedent
12
+ from typing import AsyncGenerator, Dict, List, Optional
13
+
14
+ from pydantic import BaseModel, Field
15
+
16
+ from ripperdoc.core.tool import (
17
+ Tool,
18
+ ToolOutput,
19
+ ToolResult,
20
+ ToolUseContext,
21
+ ValidationResult,
22
+ )
23
+ from ripperdoc.utils.log import get_logger
24
+
25
+ logger = get_logger()
26
+
27
+ TOOL_NAME = "AskUserQuestion"
28
+ OTHER_VALUE = "__other__"
29
+ HEADER_MAX_CHARS = 12
30
+
31
+ ASK_USER_QUESTION_PROMPT = dedent(
32
+ """\
33
+ Use this tool when you need to ask the user questions during execution. This allows you to:
34
+ 1. Gather user preferences or requirements
35
+ 2. Clarify ambiguous instructions
36
+ 3. Get decisions on implementation choices as you work
37
+ 4. Offer choices to the user about what direction to take.
38
+
39
+ Usage notes:
40
+ - Users will always be able to select "Other" to provide custom text input
41
+ - Use multiSelect: true to allow multiple answers to be selected for a question
42
+ - If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label
43
+ """
44
+ )
45
+
46
+
47
+ class OptionInput(BaseModel):
48
+ """Single option for a question."""
49
+
50
+ label: str = Field(
51
+ description="The display text for this option that the user will see and select. "
52
+ "Should be concise (1-5 words) and clearly describe the choice."
53
+ )
54
+ description: str = Field(
55
+ description="Explanation of what this option means or what will happen if chosen. "
56
+ "Useful for providing context about trade-offs or implications."
57
+ )
58
+
59
+
60
+ class QuestionInput(BaseModel):
61
+ """Single question to ask the user."""
62
+
63
+ question: str = Field(
64
+ description="The complete question to ask the user. Should be clear, specific, and end "
65
+ 'with a question mark. Example: "Which library should we use for date formatting?" '
66
+ 'If multiSelect is true, phrase it accordingly, e.g. "Which features do you want to enable?"'
67
+ )
68
+ header: str = Field(
69
+ description=f"Very short label displayed as a chip/tag (max {HEADER_MAX_CHARS} chars). "
70
+ 'Examples: "Auth method", "Library", "Approach".'
71
+ )
72
+ options: List[OptionInput] = Field(
73
+ min_length=2,
74
+ max_length=4,
75
+ description="The available choices for this question. Must have 2-4 options. "
76
+ "Each option should be a distinct, mutually exclusive choice (unless multiSelect is enabled). "
77
+ "There should be no 'Other' option, that will be provided automatically.",
78
+ )
79
+ multiSelect: bool = Field(
80
+ description="Set to true to allow the user to select multiple options instead of just one. "
81
+ "Use when choices are not mutually exclusive."
82
+ )
83
+
84
+
85
+ class AskUserQuestionToolInput(BaseModel):
86
+ """Input for the AskUserQuestion tool."""
87
+
88
+ questions: List[QuestionInput] = Field(
89
+ min_length=1,
90
+ max_length=4,
91
+ description="Questions to ask the user (1-4 questions)",
92
+ )
93
+ answers: Optional[Dict[str, str]] = Field(
94
+ default=None,
95
+ description="User answers collected by the permission component",
96
+ )
97
+
98
+
99
+ class AskUserQuestionToolOutput(BaseModel):
100
+ """Output from the AskUserQuestion tool."""
101
+
102
+ questions: List[QuestionInput]
103
+ answers: Dict[str, str]
104
+ cancelled: bool = False
105
+
106
+
107
+ def truncate_header(header: str) -> str:
108
+ """Truncate header to maximum characters."""
109
+ if len(header) <= HEADER_MAX_CHARS:
110
+ return header
111
+ return f"{header[: HEADER_MAX_CHARS - 1]}..."
112
+
113
+
114
+ def format_option_display(option: OptionInput, index: int) -> str:
115
+ """Format a single option for display."""
116
+ desc = f" - {option.description}" if option.description.strip() else ""
117
+ return f" {index}. {option.label}{desc}"
118
+
119
+
120
+ def format_question_prompt(
121
+ question: QuestionInput, question_num: int, total: int
122
+ ) -> str:
123
+ """Format a question for terminal display."""
124
+ header = truncate_header(question.header)
125
+ lines = [
126
+ "",
127
+ f"[{header}] Question {question_num}/{total}",
128
+ f" {question.question}",
129
+ "",
130
+ ]
131
+
132
+ for idx, opt in enumerate(question.options, start=1):
133
+ lines.append(format_option_display(opt, idx))
134
+
135
+ # Add "Other" option
136
+ lines.append(f" {len(question.options) + 1}. Other (type your own answer)")
137
+
138
+ if question.multiSelect:
139
+ lines.append("")
140
+ lines.append(
141
+ " Enter numbers separated by commas (e.g., 1,3), or 'o' for other: "
142
+ )
143
+ else:
144
+ lines.append("")
145
+ lines.append(" Enter choice (1-{}) or 'o' for other: ".format(len(question.options) + 1))
146
+
147
+ return "\n".join(lines)
148
+
149
+
150
+ async def prompt_user_for_answer(
151
+ question: QuestionInput, question_num: int, total: int
152
+ ) -> Optional[str]:
153
+ """Prompt user for an answer to a single question.
154
+
155
+ Returns the answer string, or None if cancelled.
156
+ """
157
+ loop = asyncio.get_running_loop()
158
+
159
+ def _prompt() -> Optional[str]:
160
+ try:
161
+ from prompt_toolkit import prompt as pt_prompt
162
+
163
+ prompt_text = format_question_prompt(question, question_num, total)
164
+ print(prompt_text, end="")
165
+
166
+ while True:
167
+ response = pt_prompt("").strip()
168
+
169
+ if not response:
170
+ print(" Please enter a valid choice.")
171
+ continue
172
+
173
+ if response.lower() in ("q", "quit", "cancel", "exit"):
174
+ return None
175
+
176
+ if response.lower() == "o" or response == str(len(question.options) + 1):
177
+ # Other option selected
178
+ print(" Enter your custom answer: ", end="")
179
+ custom = pt_prompt("")
180
+ if custom.strip():
181
+ return custom.strip()
182
+ print(" Custom answer cannot be empty.")
183
+ continue
184
+
185
+ if question.multiSelect:
186
+ # Parse comma-separated numbers
187
+ try:
188
+ indices = [int(x.strip()) for x in response.split(",")]
189
+ valid_range = range(1, len(question.options) + 2)
190
+ if all(i in valid_range for i in indices):
191
+ selected = []
192
+ for i in indices:
193
+ if i == len(question.options) + 1:
194
+ # Other option
195
+ print(" Enter your custom answer: ", end="")
196
+ custom = pt_prompt("")
197
+ if custom.strip():
198
+ selected.append(custom.strip())
199
+ else:
200
+ selected.append(question.options[i - 1].label)
201
+ if selected:
202
+ return ", ".join(selected)
203
+ print(
204
+ f" Invalid selection. Enter numbers from 1 to {len(question.options) + 1}."
205
+ )
206
+ except ValueError:
207
+ print(
208
+ " Invalid input. Enter numbers separated by commas."
209
+ )
210
+ else:
211
+ # Single selection
212
+ try:
213
+ choice = int(response)
214
+ if 1 <= choice <= len(question.options):
215
+ return question.options[choice - 1].label
216
+ elif choice == len(question.options) + 1:
217
+ # Other option
218
+ print(" Enter your custom answer: ", end="")
219
+ custom = pt_prompt("")
220
+ if custom.strip():
221
+ return custom.strip()
222
+ print(" Custom answer cannot be empty.")
223
+ else:
224
+ print(
225
+ f" Invalid choice. Enter a number from 1 to {len(question.options) + 1}."
226
+ )
227
+ except ValueError:
228
+ print(" Invalid input. Enter a number.")
229
+
230
+ except KeyboardInterrupt:
231
+ return None
232
+ except EOFError:
233
+ return None
234
+ except Exception as e:
235
+ logger.exception("[ask_user_question_tool] Error during prompt", extra={"error": str(e)})
236
+ return None
237
+
238
+ return await loop.run_in_executor(None, _prompt)
239
+
240
+
241
+ async def collect_answers(
242
+ questions: List[QuestionInput], initial_answers: Dict[str, str]
243
+ ) -> tuple[Dict[str, str], bool]:
244
+ """Collect answers for all questions.
245
+
246
+ Returns (answers_dict, cancelled_flag).
247
+ """
248
+ answers = dict(initial_answers)
249
+ total = len(questions)
250
+
251
+ for idx, question in enumerate(questions, start=1):
252
+ # Skip if already answered
253
+ if question.question in answers and answers[question.question]:
254
+ continue
255
+
256
+ answer = await prompt_user_for_answer(question, idx, total)
257
+ if answer is None:
258
+ return answers, True # Cancelled
259
+ answers[question.question] = answer
260
+
261
+ return answers, False
262
+
263
+
264
+ class AskUserQuestionTool(Tool[AskUserQuestionToolInput, AskUserQuestionToolOutput]):
265
+ """Tool for asking the user questions interactively."""
266
+
267
+ @property
268
+ def name(self) -> str:
269
+ return TOOL_NAME
270
+
271
+ async def description(self) -> str:
272
+ return (
273
+ "Asks the user multiple choice questions to gather information, "
274
+ "clarify ambiguity, understand preferences, make decisions or offer them choices."
275
+ )
276
+
277
+ @property
278
+ def input_schema(self) -> type[AskUserQuestionToolInput]:
279
+ return AskUserQuestionToolInput
280
+
281
+ async def prompt(self, safe_mode: bool = False) -> str: # noqa: ARG002
282
+ return ASK_USER_QUESTION_PROMPT
283
+
284
+ def user_facing_name(self) -> str:
285
+ return ""
286
+
287
+ def is_read_only(self) -> bool:
288
+ return True
289
+
290
+ def is_concurrency_safe(self) -> bool:
291
+ return True
292
+
293
+ def needs_permissions(
294
+ self, input_data: Optional[AskUserQuestionToolInput] = None # noqa: ARG002
295
+ ) -> bool:
296
+ return False
297
+
298
+ async def validate_input(
299
+ self,
300
+ input_data: AskUserQuestionToolInput,
301
+ context: Optional[ToolUseContext] = None, # noqa: ARG002
302
+ ) -> ValidationResult:
303
+ """Validate that question texts and option labels are unique."""
304
+ seen_questions: set[str] = set()
305
+
306
+ for question in input_data.questions:
307
+ if question.question in seen_questions:
308
+ return ValidationResult(
309
+ result=False, message="Question texts must be unique"
310
+ )
311
+ seen_questions.add(question.question)
312
+
313
+ option_labels: set[str] = set()
314
+ for option in question.options:
315
+ if option.label in option_labels:
316
+ return ValidationResult(
317
+ result=False,
318
+ message=f'Option labels for "{question.question}" must be unique',
319
+ )
320
+ option_labels.add(option.label)
321
+
322
+ return ValidationResult(result=True)
323
+
324
+ def render_result_for_assistant(self, output: AskUserQuestionToolOutput) -> str:
325
+ """Render the tool output for the AI assistant."""
326
+ if output.cancelled:
327
+ return "User declined to answer your questions."
328
+
329
+ if not output.answers:
330
+ return "User did not provide any answers."
331
+
332
+ serialized = ", ".join(
333
+ f'"{question}"="{answer}"' for question, answer in output.answers.items()
334
+ )
335
+ return (
336
+ f"User has answered your questions: {serialized}. "
337
+ "You can now continue with the user's answers in mind."
338
+ )
339
+
340
+ def render_tool_use_message(
341
+ self, input_data: AskUserQuestionToolInput, verbose: bool = False # noqa: ARG002
342
+ ) -> str:
343
+ """Render the tool use message for display."""
344
+ question_count = len(input_data.questions)
345
+ if question_count == 1:
346
+ return f"Asking user: {input_data.questions[0].question}"
347
+ return f"Asking user {question_count} questions"
348
+
349
+ async def call(
350
+ self,
351
+ input_data: AskUserQuestionToolInput,
352
+ context: ToolUseContext,
353
+ ) -> AsyncGenerator[ToolOutput, None]:
354
+ """Execute the tool to ask user questions."""
355
+ questions = input_data.questions
356
+ initial_answers = input_data.answers or {}
357
+
358
+ # Pause UI spinner before user interaction
359
+ if context.pause_ui:
360
+ try:
361
+ context.pause_ui()
362
+ except Exception:
363
+ logger.debug("[ask_user_question_tool] Failed to pause UI")
364
+
365
+ try:
366
+ # Display introduction
367
+ loop = asyncio.get_running_loop()
368
+
369
+ def _print_intro() -> None:
370
+ print("\n" + "=" * 60)
371
+ print("I need a few answers to proceed:")
372
+ print("=" * 60)
373
+
374
+ await loop.run_in_executor(None, _print_intro)
375
+
376
+ # Collect answers
377
+ answers, cancelled = await collect_answers(questions, initial_answers)
378
+
379
+ if cancelled:
380
+ output = AskUserQuestionToolOutput(
381
+ questions=questions,
382
+ answers=answers,
383
+ cancelled=True,
384
+ )
385
+ yield ToolResult(
386
+ data=output,
387
+ result_for_assistant=self.render_result_for_assistant(output),
388
+ )
389
+ return
390
+
391
+ # Display summary
392
+ def _print_summary() -> None:
393
+ print("\n" + "-" * 40)
394
+ print("Your answers:")
395
+ for q, a in answers.items():
396
+ print(f" - {q}")
397
+ print(f" -> {a}")
398
+ print("-" * 40 + "\n")
399
+
400
+ await loop.run_in_executor(None, _print_summary)
401
+
402
+ output = AskUserQuestionToolOutput(
403
+ questions=questions,
404
+ answers=answers,
405
+ cancelled=False,
406
+ )
407
+ yield ToolResult(
408
+ data=output,
409
+ result_for_assistant=self.render_result_for_assistant(output),
410
+ )
411
+
412
+ except Exception as exc:
413
+ logger.exception(
414
+ "[ask_user_question_tool] Error collecting answers",
415
+ extra={"error": str(exc)},
416
+ )
417
+ output = AskUserQuestionToolOutput(
418
+ questions=questions,
419
+ answers={},
420
+ cancelled=True,
421
+ )
422
+ yield ToolResult(
423
+ data=output,
424
+ result_for_assistant="Error while collecting user answers: " + str(exc),
425
+ )
426
+
427
+ finally:
428
+ # Resume UI spinner after user interaction
429
+ if context.resume_ui:
430
+ try:
431
+ context.resume_ui()
432
+ except Exception:
433
+ logger.debug("[ask_user_question_tool] Failed to resume UI")
@@ -47,6 +47,12 @@ _background_loop: Optional[asyncio.AbstractEventLoop] = None
47
47
  _background_thread: Optional[threading.Thread] = None
48
48
  _loop_lock = threading.Lock()
49
49
  _shutdown_registered = False
50
+ def _safe_log_exception(message: str, **extra: Any) -> None:
51
+ """Log an exception but never let logging failures bubble up."""
52
+ try:
53
+ logger.exception(message, extra=extra)
54
+ except Exception:
55
+ pass
50
56
 
51
57
 
52
58
  def _ensure_background_loop() -> asyncio.AbstractEventLoop:
@@ -301,11 +307,8 @@ def list_background_tasks() -> List[str]:
301
307
  return list(_tasks.keys())
302
308
 
303
309
 
304
- def shutdown_background_shell() -> None:
305
- """Stop background tasks/loop to avoid asyncio 'Event loop is closed' warnings."""
306
- global _background_loop, _background_thread
307
-
308
- loop = _background_loop
310
+ async def _shutdown_loop(loop: asyncio.AbstractEventLoop) -> None:
311
+ """Drain running background processes before stopping the loop."""
309
312
  with _tasks_lock:
310
313
  tasks = list(_tasks.values())
311
314
  _tasks.clear()
@@ -313,21 +316,68 @@ def shutdown_background_shell() -> None:
313
316
  for task in tasks:
314
317
  try:
315
318
  task.killed = True
316
- task.process.kill()
319
+ with contextlib.suppress(ProcessLookupError):
320
+ task.process.kill()
321
+ try:
322
+ with contextlib.suppress(ProcessLookupError):
323
+ await asyncio.wait_for(task.process.wait(), timeout=1.5)
324
+ except asyncio.TimeoutError:
325
+ with contextlib.suppress(ProcessLookupError, PermissionError):
326
+ task.process.kill()
327
+ with contextlib.suppress(asyncio.TimeoutError, ProcessLookupError):
328
+ await asyncio.wait_for(task.process.wait(), timeout=0.5)
329
+ task.exit_code = task.process.returncode or -1
317
330
  except Exception:
318
- pass
319
- for reader in task.reader_tasks:
320
- if loop and loop.is_running():
321
- loop.call_soon_threadsafe(reader.cancel)
322
- task.done_event.set()
331
+ _safe_log_exception(
332
+ "Error shutting down background task",
333
+ task_id=task.id,
334
+ command=task.command,
335
+ )
336
+ finally:
337
+ await _finalize_reader_tasks(task.reader_tasks)
338
+ task.done_event.set()
323
339
 
324
- if loop and loop.is_running():
325
- try:
326
- loop.call_soon_threadsafe(loop.stop)
327
- except Exception:
328
- pass
329
- if _background_thread and _background_thread.is_alive():
330
- _background_thread.join(timeout=2)
340
+ current = asyncio.current_task()
341
+ pending = [t for t in asyncio.all_tasks(loop) if t is not current]
342
+ for pending_task in pending:
343
+ pending_task.cancel()
344
+ if pending:
345
+ with contextlib.suppress(Exception):
346
+ await asyncio.gather(*pending, return_exceptions=True)
347
+
348
+ with contextlib.suppress(Exception):
349
+ await loop.shutdown_asyncgens()
350
+
351
+
352
+ def shutdown_background_shell() -> None:
353
+ """Stop background tasks/loop to avoid asyncio 'Event loop is closed' warnings."""
354
+ global _background_loop, _background_thread
355
+
356
+ loop = _background_loop
357
+ thread = _background_thread
358
+
359
+ if not loop or loop.is_closed():
360
+ _background_loop = None
361
+ _background_thread = None
362
+ return
331
363
 
332
- _background_loop = None
333
- _background_thread = None
364
+ try:
365
+ if loop.is_running():
366
+ try:
367
+ fut = asyncio.run_coroutine_threadsafe(_shutdown_loop(loop), loop)
368
+ fut.result(timeout=3)
369
+ except Exception:
370
+ logger.debug("Failed to cleanly shutdown background loop", exc_info=True)
371
+ try:
372
+ loop.call_soon_threadsafe(loop.stop)
373
+ except Exception:
374
+ logger.debug("Failed to stop background loop", exc_info=True)
375
+ else:
376
+ loop.run_until_complete(_shutdown_loop(loop))
377
+ finally:
378
+ if thread and thread.is_alive():
379
+ thread.join(timeout=2)
380
+ with contextlib.suppress(Exception):
381
+ loop.close()
382
+ _background_loop = None
383
+ _background_thread = None