Spaces:
Sleeping
Sleeping
File size: 27,396 Bytes
1397957 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | """
Session prompt handling with agentic loop support.
"""
from typing import Optional, List, Dict, Any, AsyncIterator, Literal
from pydantic import BaseModel
import asyncio
import json
from .session import Session
from .message import Message, MessagePart, AssistantMessage
from .processor import SessionProcessor
from ..provider import get_provider, list_providers
from ..provider.provider import Message as ProviderMessage, StreamChunk, ToolCall
from ..tool import get_tool, get_tools_schema, ToolContext, get_registry
from ..core.config import settings
from ..core.bus import Bus, PART_UPDATED, PartPayload, STEP_STARTED, STEP_FINISHED, StepPayload, TOOL_STATE_CHANGED, ToolStatePayload
from ..agent import get as get_agent, default_agent, get_system_prompt, is_tool_allowed, AgentInfo, get_prompt_for_provider
class PromptInput(BaseModel):
content: str
provider_id: Optional[str] = None
model_id: Optional[str] = None
system: Optional[str] = None
temperature: Optional[float] = None
max_tokens: Optional[int] = None
tools_enabled: bool = True
# Agentic loop options
auto_continue: Optional[bool] = None # None = use agent default
max_steps: Optional[int] = None # None = use agent default
class LoopState(BaseModel):
step: int = 0
max_steps: int = 50
auto_continue: bool = True
stop_reason: Optional[str] = None
paused: bool = False
pause_reason: Optional[str] = None
import re
FAKE_TOOL_CALL_PATTERN = re.compile(
r'\[Called\s+tool:\s*(\w+)\s*\(\s*(\{[^}]*\}|\{[^)]*\}|[^)]*)\s*\)\]',
re.IGNORECASE
)
class SessionPrompt:
_active_sessions: Dict[str, asyncio.Task] = {}
_loop_states: Dict[str, LoopState] = {}
@classmethod
async def prompt(
cls,
session_id: str,
input: PromptInput,
user_id: Optional[str] = None
) -> AsyncIterator[StreamChunk]:
session = await Session.get(session_id, user_id)
# Get agent configuration
agent_id = session.agent_id or "build"
agent = get_agent(agent_id) or default_agent()
# Determine loop settings
auto_continue = input.auto_continue if input.auto_continue is not None else agent.auto_continue
max_steps = input.max_steps if input.max_steps is not None else agent.max_steps
if auto_continue:
async for chunk in cls._agentic_loop(session_id, input, agent, max_steps, user_id):
yield chunk
else:
async for chunk in cls._single_turn(session_id, input, agent, user_id=user_id):
yield chunk
@classmethod
async def _agentic_loop(
cls,
session_id: str,
input: PromptInput,
agent: AgentInfo,
max_steps: int,
user_id: Optional[str] = None
) -> AsyncIterator[StreamChunk]:
state = LoopState(step=0, max_steps=max_steps, auto_continue=True)
cls._loop_states[session_id] = state
# SessionProcessor ๊ฐ์ ธ์ค๊ธฐ
processor = SessionProcessor.get_or_create(session_id, max_steps=max_steps)
try:
while processor.should_continue() and not state.paused:
state.step += 1
# ์คํ
์์
step_info = processor.start_step()
await Bus.publish(STEP_STARTED, StepPayload(
session_id=session_id,
step=state.step,
max_steps=max_steps
))
print(f"[AGENTIC LOOP] Starting step {state.step}, stop_reason={state.stop_reason}", flush=True)
turn_input = input if state.step == 1 else PromptInput(
content="",
provider_id=input.provider_id,
model_id=input.model_id,
temperature=input.temperature,
max_tokens=input.max_tokens,
tools_enabled=input.tools_enabled,
auto_continue=False,
)
if state.step > 1:
yield StreamChunk(type="step", text=f"Step {state.step}")
# Track tool calls in this turn
has_tool_calls_this_turn = False
async for chunk in cls._single_turn(
session_id,
turn_input,
agent,
is_continuation=(state.step > 1),
user_id=user_id
):
yield chunk
if chunk.type == "tool_call" and chunk.tool_call:
has_tool_calls_this_turn = True
print(f"[AGENTIC LOOP] tool_call: {chunk.tool_call.name}", flush=True)
if chunk.tool_call.name == "question" and agent.pause_on_question:
state.paused = True
state.pause_reason = "question"
# question tool์ด ์๋ฃ๋๋ฉด (๋ต๋ณ ๋ฐ์) pause ํด์
elif chunk.type == "tool_result":
if state.paused and state.pause_reason == "question":
state.paused = False
state.pause_reason = None
elif chunk.type == "done":
state.stop_reason = chunk.stop_reason
print(f"[AGENTIC LOOP] done: stop_reason={chunk.stop_reason}", flush=True)
# ์คํ
์๋ฃ
step_status = "completed"
if processor.is_doom_loop():
step_status = "doom_loop"
print(f"[AGENTIC LOOP] Doom loop detected! Stopping execution.", flush=True)
yield StreamChunk(type="text", text=f"\n[๊ฒฝ๊ณ : ๋์ผ ๋๊ตฌ ๋ฐ๋ณต ํธ์ถ ๊ฐ์ง, ๋ฃจํ๋ฅผ ์ค๋จํฉ๋๋ค]\n")
processor.finish_step(status=step_status)
await Bus.publish(STEP_FINISHED, StepPayload(
session_id=session_id,
step=state.step,
max_steps=max_steps
))
print(f"[AGENTIC LOOP] End of step {state.step}: stop_reason={state.stop_reason}, has_tool_calls={has_tool_calls_this_turn}", flush=True)
# Doom loop ๊ฐ์ง ์ ์ค๋จ
if processor.is_doom_loop():
break
# If this turn had no new tool calls (just text response), we're done
if state.stop_reason != "tool_calls":
print(f"[AGENTIC LOOP] Breaking: stop_reason != tool_calls", flush=True)
break
# Loop ์ข
๋ฃ ํ ์ํ ๋ฉ์์ง๋ง ์ถ๋ ฅ (summary LLM ํธ์ถ ์์!)
if state.paused:
yield StreamChunk(type="text", text=f"\n[Paused: {state.pause_reason}]\n")
elif state.step >= state.max_steps:
yield StreamChunk(type="text", text=f"\n[Max steps ({state.max_steps}) reached]\n")
# else: ์์ฐ์ค๋ฝ๊ฒ ์ข
๋ฃ (์ถ๊ฐ ์ถ๋ ฅ ์์)
finally:
if session_id in cls._loop_states:
del cls._loop_states[session_id]
# SessionProcessor ์ ๋ฆฌ
SessionProcessor.remove(session_id)
@classmethod
def _infer_provider_from_model(cls, model_id: str) -> str:
"""model_id์์ provider_id๋ฅผ ์ถ๋ก """
# LiteLLM prefix ๊ธฐ๋ฐ ๋ชจ๋ธ์ litellm provider ์ฌ์ฉ
litellm_prefixes = ["gemini/", "groq/", "deepseek/", "openrouter/", "zai/"]
for prefix in litellm_prefixes:
if model_id.startswith(prefix):
return "litellm"
# Claude ๋ชจ๋ธ
if model_id.startswith("claude-"):
return "litellm"
# GPT/O1 ๋ชจ๋ธ
if model_id.startswith("gpt-") or model_id.startswith("o1"):
return "litellm"
# ๊ธฐ๋ณธ๊ฐ
return settings.default_provider
@classmethod
async def _single_turn(
cls,
session_id: str,
input: PromptInput,
agent: AgentInfo,
is_continuation: bool = False,
user_id: Optional[str] = None
) -> AsyncIterator[StreamChunk]:
session = await Session.get(session_id, user_id)
model_id = input.model_id or session.model_id or settings.default_model
# provider_id๊ฐ ๋ช
์๋์ง ์์ผ๋ฉด model_id์์ ์ถ๋ก
if input.provider_id:
provider_id = input.provider_id
elif session.provider_id:
provider_id = session.provider_id
else:
provider_id = cls._infer_provider_from_model(model_id)
print(f"[Prompt DEBUG] input.provider_id={input.provider_id}, session.provider_id={session.provider_id}", flush=True)
print(f"[Prompt DEBUG] Final provider_id={provider_id}, model_id={model_id}", flush=True)
provider = get_provider(provider_id)
print(f"[Prompt DEBUG] Got provider: {provider}", flush=True)
if not provider:
yield StreamChunk(type="error", error=f"Provider not found: {provider_id}")
return
# Only create user message if there's content (not a continuation)
if input.content and not is_continuation:
user_msg = await Message.create_user(session_id, input.content, user_id)
assistant_msg = await Message.create_assistant(session_id, provider_id, model_id, user_id)
# Build message history
history = await Message.list(session_id, user_id=user_id)
messages = cls._build_messages(history[:-1], include_tool_results=True)
# Build system prompt with provider-specific optimization
system_prompt = cls._build_system_prompt(agent, provider_id, input.system)
# Get tools schema
tools_schema = get_tools_schema() if input.tools_enabled else None
current_text_part: Optional[MessagePart] = None
accumulated_text = ""
# reasoning ์ ์ฅ์ ์ํ ๋ณ์
current_reasoning_part: Optional[MessagePart] = None
accumulated_reasoning = ""
try:
async for chunk in provider.stream(
model_id=model_id,
messages=messages,
tools=tools_schema,
system=system_prompt,
temperature=input.temperature or agent.temperature,
max_tokens=input.max_tokens or agent.max_tokens,
):
if chunk.type == "text":
accumulated_text += chunk.text or ""
if current_text_part is None:
current_text_part = await Message.add_part(
assistant_msg.id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=assistant_msg.id,
type="text",
content=accumulated_text
),
user_id
)
else:
await Message.update_part(
session_id,
assistant_msg.id,
current_text_part.id,
{"content": accumulated_text},
user_id
)
yield chunk
elif chunk.type == "tool_call":
tc = chunk.tool_call
if tc:
# Check permission
permission = is_tool_allowed(agent, tc.name)
if permission == "deny":
yield StreamChunk(
type="tool_result",
text=f"Error: Tool '{tc.name}' is not allowed for this agent"
)
continue
tool_part = await Message.add_part(
assistant_msg.id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=assistant_msg.id,
type="tool_call",
tool_call_id=tc.id,
tool_name=tc.name,
tool_args=tc.arguments,
tool_status="running" # ์คํ ์ค ์ํ
),
user_id
)
# IMPORTANT: Yield tool_call FIRST so frontend can show UI
# This is critical for interactive tools like 'question'
yield chunk
# ๋๊ตฌ ์คํ ์์ ์ด๋ฒคํธ ๋ฐํ
await Bus.publish(TOOL_STATE_CHANGED, ToolStatePayload(
session_id=session_id,
message_id=assistant_msg.id,
part_id=tool_part.id,
tool_name=tc.name,
status="running"
))
# Execute tool (may block for user input, e.g., question tool)
tool_result, tool_status = await cls._execute_tool(
session_id,
assistant_msg.id,
tc.id,
tc.name,
tc.arguments,
user_id
)
# tool_call ํํธ์ status๋ฅผ completed/error๋ก ์
๋ฐ์ดํธ
await Message.update_part(
session_id,
assistant_msg.id,
tool_part.id,
{"tool_status": tool_status},
user_id
)
# ๋๊ตฌ ์๋ฃ ์ด๋ฒคํธ ๋ฐํ
await Bus.publish(TOOL_STATE_CHANGED, ToolStatePayload(
session_id=session_id,
message_id=assistant_msg.id,
part_id=tool_part.id,
tool_name=tc.name,
status=tool_status
))
yield StreamChunk(
type="tool_result",
text=tool_result
)
else:
yield chunk
elif chunk.type == "reasoning":
# reasoning ์ ์ฅ (๊ธฐ์กด์๋ yield๋ง ํ์)
accumulated_reasoning += chunk.text or ""
if current_reasoning_part is None:
current_reasoning_part = await Message.add_part(
assistant_msg.id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=assistant_msg.id,
type="reasoning",
content=accumulated_reasoning
),
user_id
)
else:
await Message.update_part(
session_id,
assistant_msg.id,
current_reasoning_part.id,
{"content": accumulated_reasoning},
user_id
)
yield chunk
elif chunk.type == "done":
if chunk.usage:
await Message.set_usage(session_id, assistant_msg.id, chunk.usage, user_id)
yield chunk
elif chunk.type == "error":
await Message.set_error(session_id, assistant_msg.id, chunk.error or "Unknown error", user_id)
yield chunk
await Session.touch(session_id)
except Exception as e:
error_msg = str(e)
await Message.set_error(session_id, assistant_msg.id, error_msg, user_id)
yield StreamChunk(type="error", error=error_msg)
@classmethod
def _detect_fake_tool_call(cls, text: str) -> Optional[Dict[str, Any]]:
"""
Detect if the model wrote a fake tool call as text instead of using actual tool calling.
Returns parsed tool call info if detected, None otherwise.
Patterns detected:
- [Called tool: toolname({...})]
- [Called tool: toolname({'key': 'value'})]
"""
if not text:
return None
match = FAKE_TOOL_CALL_PATTERN.search(text)
if match:
tool_name = match.group(1)
args_str = match.group(2).strip()
# Try to parse arguments
args = {}
if args_str:
try:
# Handle both JSON and Python dict formats
args_str = args_str.replace("'", '"') # Convert Python dict to JSON
args = json.loads(args_str)
except json.JSONDecodeError:
# Try to extract key-value pairs manually
# Pattern: 'key': 'value' or "key": "value"
kv_pattern = re.compile(r'["\']?(\w+)["\']?\s*:\s*["\']([^"\']+)["\']')
for kv_match in kv_pattern.finditer(args_str):
args[kv_match.group(1)] = kv_match.group(2)
return {
"name": tool_name,
"arguments": args
}
return None
@classmethod
def _build_system_prompt(
cls,
agent: AgentInfo,
provider_id: str,
custom_system: Optional[str] = None
) -> Optional[str]:
"""Build the complete system prompt.
Args:
agent: The agent configuration
provider_id: The provider identifier for selecting optimized prompt
custom_system: Optional custom system prompt to append
Returns:
The complete system prompt, or None if empty
"""
parts = []
# Add provider-specific system prompt (optimized for Claude/Gemini/etc.)
provider_prompt = get_prompt_for_provider(provider_id)
if provider_prompt:
parts.append(provider_prompt)
# Add agent-specific prompt (if defined and different from provider prompt)
agent_prompt = get_system_prompt(agent)
if agent_prompt and agent_prompt != provider_prompt:
parts.append(agent_prompt)
# Add custom system prompt
if custom_system:
parts.append(custom_system)
return "\n\n".join(parts) if parts else None
@classmethod
def _build_messages(
cls,
history: List,
include_tool_results: bool = True
) -> List[ProviderMessage]:
"""Build message list for LLM including tool calls and results.
Proper tool calling flow:
1. User message
2. Assistant message (may include tool calls)
3. Tool results (as user message with tool context)
4. Assistant continues
"""
messages = []
for msg in history:
if msg.role == "user":
# Skip empty user messages (continuations)
if msg.content:
messages.append(ProviderMessage(role="user", content=msg.content))
elif msg.role == "assistant":
# Collect all parts
text_parts = []
tool_calls = []
tool_results = []
for part in getattr(msg, "parts", []):
if part.type == "text" and part.content:
text_parts.append(part.content)
elif part.type == "tool_call" and include_tool_results:
tool_calls.append({
"id": part.tool_call_id,
"name": part.tool_name,
"arguments": part.tool_args or {}
})
elif part.type == "tool_result" and include_tool_results:
tool_results.append({
"tool_call_id": part.tool_call_id,
"output": part.tool_output or ""
})
# Build assistant content - only text, NO tool call summaries
# IMPORTANT: Do NOT include "[Called tool: ...]" patterns as this causes
# models like Gemini to mimic the pattern instead of using actual tool calls
assistant_content_parts = []
if text_parts:
assistant_content_parts.append("".join(text_parts))
if assistant_content_parts:
messages.append(ProviderMessage(
role="assistant",
content="\n".join(assistant_content_parts)
))
# Add tool results as user message (simulating tool response)
if tool_results:
result_content = []
for result in tool_results:
result_content.append(f"Tool result:\n{result['output']}")
messages.append(ProviderMessage(
role="user",
content="\n\n".join(result_content)
))
return messages
@classmethod
async def _execute_tool(
cls,
session_id: str,
message_id: str,
tool_call_id: str,
tool_name: str,
tool_args: Dict[str, Any],
user_id: Optional[str] = None
) -> tuple[str, str]:
"""Execute a tool and store the result. Returns (output, status)."""
# SessionProcessor๋ฅผ ํตํ doom loop ๊ฐ์ง
# tool_args๋ ์ ๋ฌํ์ฌ ๊ฐ์ ๋๊ตฌ + ๊ฐ์ ์ธ์์ผ ๋๋ง doom loop์ผ๋ก ํ๋จ
processor = SessionProcessor.get_or_create(session_id)
is_doom_loop = processor.record_tool_call(tool_name, tool_args)
if is_doom_loop:
error_output = f"Error: Doom loop detected - tool '{tool_name}' called repeatedly"
await Message.add_part(
message_id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=message_id,
type="tool_result",
tool_call_id=tool_call_id,
tool_output=error_output
),
user_id
)
return error_output, "error"
# Registry์์ ๋๊ตฌ ๊ฐ์ ธ์ค๊ธฐ
registry = get_registry()
tool = registry.get(tool_name)
if not tool:
error_output = f"Error: Tool '{tool_name}' not found"
await Message.add_part(
message_id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=message_id,
type="tool_result",
tool_call_id=tool_call_id,
tool_output=error_output
),
user_id
)
return error_output, "error"
ctx = ToolContext(
session_id=session_id,
message_id=message_id,
tool_call_id=tool_call_id,
)
try:
result = await tool.execute(tool_args, ctx)
# ์ถ๋ ฅ ๊ธธ์ด ์ ํ ์ ์ฉ
truncated_output = tool.truncate_output(result.output)
output = f"[{result.title}]\n{truncated_output}"
status = "completed"
except Exception as e:
output = f"Error executing tool: {str(e)}"
status = "error"
await Message.add_part(
message_id,
session_id,
MessagePart(
id="",
session_id=session_id,
message_id=message_id,
type="tool_result",
tool_call_id=tool_call_id,
tool_output=output
),
user_id
)
return output, status
@classmethod
def cancel(cls, session_id: str) -> bool:
"""Cancel an active session."""
cancelled = False
if session_id in cls._active_sessions:
cls._active_sessions[session_id].cancel()
del cls._active_sessions[session_id]
cancelled = True
if session_id in cls._loop_states:
cls._loop_states[session_id].paused = True
cls._loop_states[session_id].pause_reason = "cancelled"
del cls._loop_states[session_id]
cancelled = True
return cancelled
@classmethod
def get_loop_state(cls, session_id: str) -> Optional[LoopState]:
"""Get the current loop state for a session."""
return cls._loop_states.get(session_id)
@classmethod
async def resume(cls, session_id: str) -> AsyncIterator[StreamChunk]:
state = cls._loop_states.get(session_id)
if not state or not state.paused:
yield StreamChunk(type="error", error="No paused loop to resume")
return
state.paused = False
state.pause_reason = None
session = await Session.get(session_id)
agent_id = session.agent_id or "build"
agent = get_agent(agent_id) or default_agent()
continue_input = PromptInput(content="")
while state.stop_reason == "tool_calls" and not state.paused and state.step < state.max_steps:
state.step += 1
yield StreamChunk(type="text", text=f"\n[Resuming... step {state.step}/{state.max_steps}]\n")
async for chunk in cls._single_turn(session_id, continue_input, agent, is_continuation=True):
yield chunk
if chunk.type == "tool_call" and chunk.tool_call:
if chunk.tool_call.name == "question" and agent.pause_on_question:
state.paused = True
state.pause_reason = "question"
elif chunk.type == "done":
state.stop_reason = chunk.stop_reason
|