mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 05:58:08 +01:00
merged old agent goal in with keynote goal
This commit is contained in:
@@ -5,7 +5,10 @@ from temporalio.exceptions import ActivityError
|
||||
from temporalio.common import RetryPolicy
|
||||
|
||||
from models.data_types import ConversationHistory, ToolPromptInput
|
||||
from prompts.agent_prompt_generators import generate_missing_args_prompt, generate_tool_completion_prompt
|
||||
from prompts.agent_prompt_generators import (
|
||||
generate_missing_args_prompt,
|
||||
generate_tool_completion_prompt,
|
||||
)
|
||||
from shared.config import TEMPORAL_LEGACY_TASK_QUEUE
|
||||
|
||||
# Constants from original file
|
||||
@@ -14,12 +17,13 @@ TOOL_ACTIVITY_SCHEDULE_TO_CLOSE_TIMEOUT = timedelta(minutes=30)
|
||||
LLM_ACTIVITY_START_TO_CLOSE_TIMEOUT = timedelta(seconds=10)
|
||||
LLM_ACTIVITY_SCHEDULE_TO_CLOSE_TIMEOUT = timedelta(minutes=30)
|
||||
|
||||
|
||||
async def handle_tool_execution(
|
||||
current_tool: str,
|
||||
tool_data: Dict[str, Any],
|
||||
tool_results: list,
|
||||
add_message_callback: callable,
|
||||
prompt_queue: Deque[str]
|
||||
prompt_queue: Deque[str],
|
||||
) -> None:
|
||||
"""Execute a tool after confirmation and handle its result."""
|
||||
workflow.logger.info(f"Confirmed. Proceeding with tool: {current_tool}")
|
||||
@@ -50,11 +54,12 @@ async def handle_tool_execution(
|
||||
add_message_callback("tool_result", dynamic_result)
|
||||
prompt_queue.append(generate_tool_completion_prompt(current_tool, dynamic_result))
|
||||
|
||||
|
||||
async def handle_missing_args(
|
||||
current_tool: str,
|
||||
args: Dict[str, Any],
|
||||
tool_data: Dict[str, Any],
|
||||
prompt_queue: Deque[str]
|
||||
prompt_queue: Deque[str],
|
||||
) -> bool:
|
||||
"""Check for missing arguments and handle them if found."""
|
||||
missing_args = [key for key, value in args.items() if value is None]
|
||||
@@ -69,13 +74,15 @@ async def handle_missing_args(
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def format_history(conversation_history: ConversationHistory) -> str:
|
||||
"""Format the conversation history into a single string."""
|
||||
return " ".join(
|
||||
str(msg["response"]) for msg in conversation_history["messages"]
|
||||
)
|
||||
return " ".join(str(msg["response"]) for msg in conversation_history["messages"])
|
||||
|
||||
def prompt_with_history(conversation_history: ConversationHistory, prompt: str) -> tuple[str, str]:
|
||||
|
||||
def prompt_with_history(
|
||||
conversation_history: ConversationHistory, prompt: str
|
||||
) -> tuple[str, str]:
|
||||
"""Generate a context-aware prompt with conversation history."""
|
||||
history_string = format_history(conversation_history)
|
||||
context_instructions = (
|
||||
@@ -86,16 +93,19 @@ def prompt_with_history(conversation_history: ConversationHistory, prompt: str)
|
||||
)
|
||||
return (context_instructions, prompt)
|
||||
|
||||
|
||||
async def continue_as_new_if_needed(
|
||||
conversation_history: ConversationHistory,
|
||||
prompt_queue: Deque[str],
|
||||
agent_goal: Any,
|
||||
max_turns: int,
|
||||
add_message_callback: callable
|
||||
add_message_callback: callable,
|
||||
) -> None:
|
||||
"""Handle workflow continuation if message limit is reached."""
|
||||
if len(conversation_history["messages"]) >= max_turns:
|
||||
summary_context, summary_prompt = prompt_summary_with_history(conversation_history)
|
||||
summary_context, summary_prompt = prompt_summary_with_history(
|
||||
conversation_history
|
||||
)
|
||||
summary_input = ToolPromptInput(
|
||||
prompt=summary_prompt, context_instructions=summary_context
|
||||
)
|
||||
@@ -104,21 +114,24 @@ async def continue_as_new_if_needed(
|
||||
summary_input,
|
||||
schedule_to_close_timeout=LLM_ACTIVITY_SCHEDULE_TO_CLOSE_TIMEOUT,
|
||||
)
|
||||
workflow.logger.info(
|
||||
f"Continuing as new after {max_turns} turns."
|
||||
)
|
||||
workflow.logger.info(f"Continuing as new after {max_turns} turns.")
|
||||
add_message_callback("conversation_summary", conversation_summary)
|
||||
workflow.continue_as_new(
|
||||
args=[{
|
||||
"tool_params": {
|
||||
"conversation_summary": conversation_summary,
|
||||
"prompt_queue": prompt_queue,
|
||||
},
|
||||
"agent_goal": agent_goal,
|
||||
}]
|
||||
args=[
|
||||
{
|
||||
"tool_params": {
|
||||
"conversation_summary": conversation_summary,
|
||||
"prompt_queue": prompt_queue,
|
||||
},
|
||||
"agent_goal": agent_goal,
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
def prompt_summary_with_history(conversation_history: ConversationHistory) -> tuple[str, str]:
|
||||
|
||||
def prompt_summary_with_history(
|
||||
conversation_history: ConversationHistory,
|
||||
) -> tuple[str, str]:
|
||||
"""Generate a prompt for summarizing the conversation.
|
||||
Used only for continue as new of the workflow."""
|
||||
history_string = format_history(conversation_history)
|
||||
@@ -127,4 +140,4 @@ def prompt_summary_with_history(conversation_history: ConversationHistory) -> tu
|
||||
"Please produce a two sentence summary of this conversation. "
|
||||
'Put the summary in the format { "summary": "<plain text>" }'
|
||||
)
|
||||
return (context_instructions, actual_prompt)
|
||||
return (context_instructions, actual_prompt)
|
||||
|
||||
Reference in New Issue
Block a user