#!/bin/bash
# AskFile - Ask ollie about the current file in acme
#
# Usage: Middle-click AskFile in the tag
# Sends: "Describe and explain this file: <filename>"

set -euo pipefail

if [[ -z "${winid:-}" ]]; then
    echo "AskFile must be run from within acme" >&2
    exit 1
fi

# Get the window name (filename) from the tag
filename=$(9p read acme/$winid/tag | awk '{print $1}')

if [[ -z "$filename" || "$filename" == *"+Errors" || "$filename" == "+*" ]]; then
    echo "No file associated with this window" >&2
    exit 1
fi

# Ensure acme session/agent exists
eval $(acme-ollie-ensure)

# Build and send the prompt
prompt="Read-only task. Inspect the complete file $filename and describe it. Do not edit files. Report the file's purpose, main components, important data flow, dependencies, and relevant tests or checks. Do not guess details that are not present in the file or repository.

File: $filename

Give a concise implementation-oriented overview."
echo "$prompt" | o "$OLLIE_SESSION/$OLLIE_AGENT" prompt
