Execute one agent turn. Parameters ---------- prompt: User message or OSINT target description. on_tool_call: Optional async callback invoked before each tool execution. Signature: ``async def on_tool_call(name: str, input
(
self,
prompt: str,
on_tool_call: Any = None,
)
| 499 | self.history = [] |
| 500 | |
| 501 | async def run( |
| 502 | self, |
| 503 | prompt: str, |
| 504 | on_tool_call: Any = None, |
| 505 | ) -> AgentResponse: |
| 506 | """ |
| 507 | Execute one agent turn. |
| 508 | |
| 509 | Parameters |
| 510 | ---------- |
| 511 | prompt: |
| 512 | User message or OSINT target description. |
| 513 | on_tool_call: |
| 514 | Optional async callback invoked before each tool execution. |
| 515 | Signature: ``async def on_tool_call(name: str, input: dict) -> None`` |
| 516 | |
| 517 | Returns |
| 518 | ------- |
| 519 | AgentResponse |
| 520 | Final text response and list of tool calls made. |
| 521 | """ |
| 522 | self.history.append({"role": "user", "content": prompt}) |
| 523 | ctx = _AgentRunContext( |
| 524 | messages=list(self.history), |
| 525 | tool_calls=[], |
| 526 | on_tool_call=on_tool_call, |
| 527 | ) |
| 528 | try: |
| 529 | while True: |
| 530 | response = self.client.messages.create( |
| 531 | model=self.model, |
| 532 | max_tokens=_MAX_TOKENS, |
| 533 | system=SYSTEM_PROMPT, |
| 534 | tools=TOOL_DEFINITIONS, # type: ignore[arg-type] |
| 535 | messages=ctx.messages, # type: ignore[arg-type] |
| 536 | ) |
| 537 | if response.stop_reason == "end_turn": |
| 538 | text = _extract_first_text(response.content) |
| 539 | self.history.append({"role": "assistant", "content": response.content}) |
| 540 | return AgentResponse(content=text, tool_calls=ctx.tool_calls) |
| 541 | if response.stop_reason == "tool_use": |
| 542 | ctx.messages.append({"role": "assistant", "content": response.content}) |
| 543 | await _process_tool_turn(ctx, response.content) |
| 544 | else: |
| 545 | break |
| 546 | except anthropic.AuthenticationError: |
| 547 | return AgentResponse( |
| 548 | content="", |
| 549 | error="Invalid API key. Run 'openosint config' to update it.", |
| 550 | ) |
| 551 | except anthropic.APIConnectionError: |
| 552 | return AgentResponse( |
| 553 | content="", |
| 554 | error="Cannot reach the Anthropic API. Check your internet connection.", |
| 555 | ) |
| 556 | except Exception as exc: |
| 557 | logger.exception("Unexpected error in Anthropic agent loop.") |
| 558 | return AgentResponse(content="", error=str(exc)) |
no test coverage detected