# Before All

```lua
local a_utils = require("ask_utils")

-- === Prep the prompt file
local prompt_absolute_dir = CTX.WORKSPACE_AIPACK_DIR .. "/.prompt/" .. CTX.PACK_IDENTITY
local prompt_file_path = prompt_absolute_dir .. "/ask-prompt.md"
local prompt_file_rel_path = aip.path.diff(prompt_file_path, CTX.WORKSPACE_DIR)

local prompt_template = a_utils.template()

if not aip.path.exists(prompt_file_path) then
    aip.file.save(prompt_file_path, prompt_template)

    -- Open it with VSCode
    -- Call with pcall to prevent failure if the code is not available
    -- TODO: Enhance this part (should notify the user)
    pcall(aip.cmd.exec,"code", {prompt_file_path} )
end

-- === Load prompt file
local prompt_file_content = aip.file.load(prompt_file_path).content
if not prompt_file_content:find("%S") then
    aip.file.save(prompt_file_path, prompt_template)
    prompt_file_content = aip.file.load(prompt_file_path).content
end

-- === Prep prompt_content
-- Note important to split on `====\n` otherwise, the doc can be split. 
local first_part, second_part = aip.text.split_first(prompt_file_content, "====\n")
-- extract the eventual #!meta params
local meta, inst = aip.md.extract_meta(first_part)
-- extract the eventual ">" lines
local _line_blocks, prompt_content = aip.text.extract_line_blocks(inst, {starts_with = ">", extrude = "content"})
-- Teram 
prompt_content = aip.text.trim(prompt_content)

print("\nOPEN your @ask-aipack prompt file: " .. prompt_file_rel_path .. "\n")

if not prompt_content:find("%S") then
    return aipack.skip("Empty prompt. Enter you question and replay")
end

-- == Compute the agent options
local options = {}
options.model             = meta.model
options.temperature       = meta.temperature
options.model_aliases     = meta.model_aliases
options.input_concurrency = meta.input_concurrency

return aip.flow.before_all_response({
    inputs     = Nil, -- no need for inputs for this agent, so make sure none
    options    = options,
    before_all = {
        first_part          = first_part,
        second_part         = second_part,
        prompt_content      = prompt_content,
        prompt_file_path    = prompt_file_path,

        prompt_absolute_dir = prompt_absolute_dir, 
    }
})

```

# Data

```lua
-- === Load the knowledge/doc files
-- The `list_load` function will list the files from the aipack parent directory and load each of them
-- so, each item is a FileRecord
local doc_globs = {"core@doc/**/*.md"}
local doc_files = aip.file.list_load(doc_globs, {absolute = true})
print("Including " .. #doc_files .. " documentation files")

-- Agument with the doc path diff
-- NOTE: Vscode cannot open absolute paths, at least when out of workspace
--       So, doing relative path with the ../../ does seems to work
for _, doc_file in ipairs(doc_files) do
    doc_file.path_diff = aip.path.diff(doc_file.path, before_all.prompt_absolute_dir)
end

-- === Return the data

return {
    first_part       = before_all.first_part,
    second_part      = before_all.second_part,
    prompt_content   = before_all.prompt_content,
    prompt_file_path = before_all.prompt_file_path,
    doc_files        = doc_files
}
```

# System

Your goal is to answer **aipack** questions in a concise and precise manner, with some code examples when appropriate.

**aipack** is a command line utility that allows you to write agents in a markdown file, with full control over the prompt, using a multi-stage approach.

Here are some documentation files, separated with `=== ../../path/to/document.md` for you to refer to in your answer (keep it as such when given a reference)

{{#each data.doc_files}}

=== {{this.path_diff}}
{{this.content}}

{{/each}}


# System

- When the user asks a question, provide the answer.
- Give me the file path of where the answer was found.
- Format the answer the way shown in the <ANSWER-FORMAT> tag (but without the encompassing markdown code block or tag)

<ANSWER-FORMAT>
## QUESTION: user_question_summarized

_answer_
</ANSWER-FORMAT>

- When using bullet point, use the `-` character
- For the `user_question_summarized` text, if the question is short, put it there; if it is long or contains special characters, summarize the question and put it there.
- For the `# Instruction` and `# System`, the content is just normal markdown, not in a markdown code block, except when putting some code, where it is appropriate to put them in the appropriate code block.
- If it is appropriate, do not hesitate to use bullet points to help clarify and conciseness.
- When the user asks to generate a stage of an aip file, do not wrap it with a `lua` code block, just `# stage_name` and then put the Lua code in the `lua` code block.
- When providing the answer, do not surround it in a markdown code block.
- For the `# Instruction`, `# System`, and `# Assistant` sections, do not put the template in markdown code block handlebars, as this is not needed by aipack. Just the text with the handlebars characters, that will work nicely.
- Remember, a `.aipack` file is a markdown file, so when giving aipack examples, do not surround the aipack stages example with a markdown code block. Just put them in lines.
- When giving the reference to the doc file, just hgive the path given in the prompt, with the format `[doc/file_name](file_path)`. Do not escale the / of the filepath. Leave them as is. 

Very important, 
- Do not wrap the whole answer in some markdown code block, just give the as when giving back an `.aip` markdown, put it in a **four** backtics md code block, with `md` as the language (since .aip are markdown files) so that the three backtick for the lua code block inside works nicely. 
So, like this: 

````md
# Data 
```lua
-- some lua
```
...
````

# User

Before we start, when giving back an agent (e.g., `.aip`) markdown, put it in a **four** backticks markdown code block with lang `md`, so that the three backticks for the Lua code block inside this content works nicely.

After that, show a `sh` block with the example of the command I need to run.

# User

Here is the user question:

{{data.prompt_content}}

# Output

```lua

-- Extract content from the AI response
local answer = ai_response.content

-- === Cleanup answer
local answer = aip.text.trim(answer)

-- === Rebuild prompt file
new_content = aip.text.trim_end(data.first_part)
new_content = new_content .. "\n\n====\n"

new_content = new_content .. "\n" .. answer .. "\n\n> genai info: " .. ai_response.info

if data.second_part then
    new_content = new_content .. "\n\n====\n\n" .. aip.text.trim(data.second_part)
end

-- Save the new content back to the file
aip.file.save(data.prompt_file_path, new_content)

return "core@ask-aipack done"

```