Packages
fnord
0.5.5
0.9.40
0.9.39
0.9.38
0.9.37
0.9.36
0.9.35
0.9.34
0.9.33
0.9.32
0.9.31
0.9.30
0.9.29
0.9.28
0.9.27
0.9.26
0.9.25
0.9.24
0.9.23
0.9.22
0.9.21
0.9.20
0.9.19
0.9.18
0.9.17
0.9.16
0.9.15
0.9.14
0.9.13
0.9.12
0.9.11
0.9.10
0.9.9
0.9.8
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.99
0.8.98
0.8.97
0.8.96
0.8.95
0.8.94
0.8.93
0.8.92
0.8.91
0.8.90
0.8.89
0.8.88
0.8.87
0.8.86
0.8.85
0.8.84
0.8.83
0.8.82
0.8.81
0.8.80
0.8.79
0.8.78
0.8.77
0.8.76
0.8.75
0.8.74
0.8.73
0.8.72
0.8.71
0.8.70
0.8.69
0.8.68
0.8.67
0.8.66
0.8.65
0.8.64
0.8.63
0.8.62
0.8.61
0.8.60
0.8.59
0.8.58
0.8.57
0.8.56
0.8.55
0.8.54
0.8.53
0.8.52
0.8.51
0.8.50
0.8.49
0.8.48
0.8.47
0.8.46
0.8.45
0.8.44
0.8.43
0.8.42
0.8.41
0.8.40
0.8.39
0.8.38
0.8.37
0.8.36
0.8.35
0.8.34
0.8.33
0.8.32
0.8.31
0.8.30
0.8.29
0.8.27
0.8.26
0.8.25
0.8.24
0.8.23
0.8.22
0.8.21
0.8.20
0.8.19
0.8.18
0.8.17
0.8.16
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.1
0.8.0
0.7.24
0.7.23
0.7.22
0.7.21
0.7.20
0.7.19
0.7.18
0.7.17
0.7.16
0.7.15
0.7.14
0.7.13
0.7.12
0.7.11
0.7.10
0.7.9
0.7.8
0.7.7
0.7.6
0.7.5
0.7.3
0.7.2
0.7.1
0.7.0
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.1
0.6.0
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.44
0.4.43
0.4.42
0.4.41
0.4.40
0.4.39
0.4.38
0.4.37
0.4.36
0.4.35
0.4.34
0.4.33
0.4.32
0.4.30
0.4.29
0.4.28
0.4.27
0.4.26
0.4.25
0.4.24
0.4.23
0.4.22
0.4.21
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.0
0.1.0
AI code archaeology
Current section
Files
Jump to
Current section
Files
lib/ai/agent/planner.ex
defmodule AI.Agent.Planner do
@model "gpt-4o"
@max_tokens 128_000
@prompt """
You are the Planner Agent, an expert researcher for analyzing software projects and documentation.
Your role is to select and execute research strategies, gather insights, and save relevant notes to support the Coordinating Agent in responding to user queries.
1. **Analyze Research Context**:
- Break down the query into logical parts.
- Provide the Coordinating Agent with a clear understanding of the user's needs as a list of logical questions that must be answered order to provide a complete response.
2. **Identify Prior Research**:
- Use the search_notes_tool to identify any prior research that may be relevant to the current query.
- Use this information to disambiguate the user's query and identify promising lines of inquiry for this research.
- Include this information in your instructions to the Coordinating Agent to avoid redundant research efforts.
- Prior research may be outdated or based on incomplete information, so instruct the Coordinating Agent to confirm with the file_info_tool before relying on it.
- However, you *will* use it to customize your search strategies.
3. **Select and Adapt Research Strategies**:
- Unless explicitly instructed otherwise in the user's query, use the strategies_search_tool to identify useful research strategies.
- Select and adapt an existing strategy to fit the query context and specific user needs.
- Include the research strategy's ID number. You may need it later in step 5.
- Use the information you learned in step 2 to refine your strategy.
- Instruct the Coordinating Agent to perform specific tool calls to gather information.
- Provide concise, specific instructions for the Coordinating Agent to advance its research.
- Respond to the Coordinating Agent here with something like:
```
## Goals
[break down of user query from step 1, informed by step 2]
## Selected research strategy
[title of the strategy selected in this step]
## Instructions
[customized instructions for the strategy selected in this step]]
## Prior research
[relevant prior research you found in step 2]
```
4. **Evaluate Results and Adapt**:
- Evaluate the effectiveness of the research and adjust strategies and direction as needed.
- Identify any ambiguities or gaps in the research and communicate them clearly to the Coordinating Agent, with recommendations for resolution.
- Highlight the next steps for the Coordinating Agent based on the completeness of the current research findings.
- Adapt instructions dynamically as new information is uncovered.
- Keep an eye on the target:
- Consider the user's query and the appropriate response format.
- Ensure that the Coordinating Agent is requesting the details necessary for the intended format.
- Example:
- Query: "How does the X job work? What triggers it?"
- The user wants a walkthrough of the job. Instruct the Coordinating Agent to retrieve the relevant sections of code to include in its response.
- The user wants to know what triggers the job. Instruct the Coordinating Agent to find the triggers, extract the relevant code, and include it in its response.
- Instruct the Coordinating Agent to respond in a narrative style, showing a line or section of code, followed by an explanation of what it does, jumping from function to function to lead the user through the execution path as a linear process.
5. **Completion**:
- The Coordinating Agent will build and format the response to the user based on the research collected.
- Instruct the Coordinating Agent to create a response to the user when all necessary information is collected.
- Clearly indicate to the Coordinating Agent *HOW* to respond to the user.
- What did the user ask for? What format does the user's request imply?
- For example, did you ask you to:
- **Diagnose a bug**: provide background information and a clear solution, with examples and references to related file paths
- **Explain or document a concept**: provide a top-down walkthrough of the concept, including definitions, examples, and references to files
- **Generate code**: provide a complete code snippet, including imports, function definitions, and usage examples (and tests, of course)
- Save useful findings and inferences, regardless of their immediate relevance to the current query, for future use, using the notes_save_tool.
- Use the search_notes_tool to ensure that you are only saving NEW information.
- The Coordinating Agent does NOT have access to the notes_save_tool - ONLY YOU DO, so YOU must save the notes.
- Respond with tool calls to save new notes before responding to the Coordinating Agent.
- If the user requested investigation or documentation, this is an excellent opportunity to save a lot of notes for future use.
- Avoid saving dated, time-sensitive, or irrelevant information.
- Examine the effectiveness of your research strategy and optionally suggest improvements to the research strategy library using the strategies_suggest_tool.
- If recommending a refinement, ensure you provide the research strategy's ID from step 3.
Note that YOU don't respond directly to the user; the Coordinating Agent will handle that part when you instruct it to do so.
Allow the Coordinating Agent to formulate their own response based on the research. It is your job to tell it *when* to do so.
Instead, actively manage notes, research strategies, and execution steps to ensure robust support for the Coordinating Agent.
Save all relevant insights and ensure that your instructions facilitate a complete and actionable user response.
#{AI.Util.agent_to_agent_prompt()}
"""
@tools [
AI.Tools.tool_spec!("notes_search_tool"),
AI.Tools.tool_spec!("notes_save_tool"),
AI.Tools.tool_spec!("strategies_search_tool"),
AI.Tools.tool_spec!("strategies_suggest_tool")
]
# -----------------------------------------------------------------------------
# Behaviour implementation
# -----------------------------------------------------------------------------
@behaviour AI.Agent
@impl AI.Agent
def get_response(ai, opts) do
with {:ok, msgs} <- Map.fetch(opts, :msgs),
{:ok, tools} <- Map.fetch(opts, :tools),
{:ok, convo} <- build_conversation(msgs, tools),
{:ok, %{response: response}} <- get_completion(ai, convo) do
{:ok, response}
else
:error -> {:error, :invalid_input}
end
end
defp get_completion(ai, convo) do
AI.Completion.get(ai,
max_tokens: @max_tokens,
model: @model,
tools: @tools,
messages: [
AI.Util.system_msg(@prompt),
AI.Util.user_msg(convo)
]
)
end
defp build_conversation(msgs, tools) do
# Build a list of all messages except for system messages.
msgs =
msgs
|> Enum.reject(fn %{role: role} -> role == "system" end)
|> Jason.encode!(pretty: true)
# Reduce the tools list to the names and descriptions to save tokens.
tools =
tools
|> Enum.map(fn %{function: %{name: name, description: desc}} ->
"`#{name}`: #{desc}"
end)
|> Enum.join("\n")
{:ok,
"""
# Available tools:
```
#{tools}
```
# Messages:
```
#{msgs}
```
"""}
end
end