Prompts the user for a HITL function call and returns the response.
(
fc_id: str, fc_name: str, args: dict[str, Any]
)
| 137 | |
| 138 | |
| 139 | def _prompt_for_function_call( |
| 140 | fc_id: str, fc_name: str, args: dict[str, Any] |
| 141 | ) -> types.Content: |
| 142 | """Prompts the user for a HITL function call and returns the response.""" |
| 143 | if fc_name == _REQUEST_INPUT: |
| 144 | message = args.get('message') or 'Input requested' |
| 145 | schema = args.get('response_schema') |
| 146 | click.echo(f'[HITL input] {message}') |
| 147 | if schema: |
| 148 | click.echo(f' Schema: {json.dumps(schema)}') |
| 149 | elif fc_name == _REQUEST_CONFIRMATION: |
| 150 | tool_confirmation = args.get('toolConfirmation', {}) |
| 151 | hint = tool_confirmation.get('hint', '') |
| 152 | original_fc = args.get('originalFunctionCall', {}) |
| 153 | original_name = original_fc.get('name', 'unknown') |
| 154 | click.echo(f'[HITL confirm] {hint or f"Confirm {original_name}?"}') |
| 155 | click.echo(' Type "yes" to confirm, anything else to reject.') |
| 156 | else: |
| 157 | click.echo(f'[HITL] Waiting for input for {fc_name}({args})') |
| 158 | |
| 159 | user_input = input('[user]: ') |
| 160 | |
| 161 | # Build the FunctionResponse. |
| 162 | if fc_name == _REQUEST_CONFIRMATION: |
| 163 | confirmed = _is_positive_response(user_input) |
| 164 | response = {'confirmed': confirmed} |
| 165 | else: |
| 166 | # Try to parse as JSON, fall back to wrapping as {"result": value}. |
| 167 | try: |
| 168 | parsed = json.loads(user_input) |
| 169 | response = parsed if isinstance(parsed, dict) else {'result': parsed} |
| 170 | except (json.JSONDecodeError, ValueError): |
| 171 | response = {'result': user_input} |
| 172 | |
| 173 | return types.Content( |
| 174 | role='user', |
| 175 | parts=[ |
| 176 | types.Part( |
| 177 | function_response=types.FunctionResponse( |
| 178 | id=fc_id, |
| 179 | name=fc_name, |
| 180 | response=response, |
| 181 | ) |
| 182 | ) |
| 183 | ], |
| 184 | ) |
| 185 | |
| 186 | |
| 187 | async def run_interactively( |
no test coverage detected