MCPcopy Create free account
hub / github.com/Louisym/MiniCC / run_turn

Method run_turn

tutorials/05_agentic_loop_complete.py:261–344  ·  view source on GitHub ↗

执行一个完整的对话轮次。 这就是 Tutorial 01 里的 run_turn,但现在是完整版! 对应源码: conversation.rs:170-283

(
        self,
        user_input: str,
        prompter: Optional[PermissionPrompter] = None,
    )

Source from the content-addressed store, hash-verified

259 self.usage_tracker = UsageTracker()
260
261 def run_turn(
262 self,
263 user_input: str,
264 prompter: Optional[PermissionPrompter] = None,
265 ) -> TurnSummary:
266 """
267 执行一个完整的对话轮次。
268
269 这就是 Tutorial 01 里的 run_turn,但现在是完整版!
270
271 对应源码: conversation.rs:170-283
272 """
273 # 步骤 1: 添加用户消息
274 self.session.messages.append(ConversationMessage.user_text(user_input))
275
276 assistant_messages: list[ConversationMessage] = []
277 tool_results: list[ConversationMessage] = []
278 iterations = 0
279
280 # 步骤 2: Agentic Loop
281 while True:
282 iterations += 1
283 if iterations > self.max_iterations:
284 raise RuntimeError("exceeded maximum iterations")
285
286 # 步骤 2a: 调用 API
287 request = ApiRequest(
288 system_prompt=self.system_prompt,
289 messages=self.session.messages,
290 )
291 events = self.api_client.stream(request)
292
293 # 步骤 2b: 解析 AI 回复
294 assistant_msg, usage = self._build_assistant_message(events)
295 if usage:
296 self.usage_tracker.record(usage)
297
298 self.session.messages.append(assistant_msg)
299 assistant_messages.append(assistant_msg)
300
301 # 步骤 2c: 提取工具调用请求
302 pending_tool_uses = [
303 block for block in assistant_msg.blocks
304 if isinstance(block, ToolUseBlock)
305 ]
306
307 # 没有工具调用 → 循环结束
308 if not pending_tool_uses:
309 break
310
311 # 步骤 2d: 逐个执行工具
312 for tool_use in pending_tool_uses:
313 # 先检查权限!
314 allowed, reason = self.permission_policy.authorize(
315 tool_use.name, tool_use.input, prompter,
316 )
317
318 if allowed:

Callers 1

mainFunction · 0.95

Calls 12

RuntimeErrorClass · 0.85
appendMethod · 0.80
ApiRequestClass · 0.70
TurnSummaryClass · 0.70
user_textMethod · 0.45
streamMethod · 0.45
recordMethod · 0.45
authorizeMethod · 0.45
executeMethod · 0.45
tool_resultMethod · 0.45
cumulative_usageMethod · 0.45

Tested by

no test coverage detected