summaryrefslogtreecommitdiff
path: root/agent.sh
blob: 0b4fa7c1091259cb4fe086199e32d64bb1c9e0f4 (plain)
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
#!/bin/bash

[ ! -f .env ] || export $(grep -v '^#' .env | xargs)
[ ! -f .teagent ] || export $(grep -v '^#' .teagent | xargs)
if [ -z "$REPO" ]; then
  exit 1
fi
# Default 5 minute maximum
TIMEOUT_MAX="${TIMEOUT_MAX:-300}"
N_RETRIES="${RETRY_MAX:-5}"

teaissue() {
  tea issues create --title "$1 (TeAgent)" --body "$2" --login teagent --repo "${REPO}"
}

SERVER="${OLLAMA_HOST:-http://localhost:11434}"
MODEL="${OLLAMA_MODEL:-llama3.2}"

read -p "Source file listing cmd: " -e -i "find src -type f -iname \\\\*.py" source_file_fn
SOURCE_FILES=`eval "${source_file_fn}"`

# Select what files
FILE_SELECTIONS=()
INDEX=1
for source_file in ${SOURCE_FILES}; do
  if [ ! -s "${source_file}" ]; then
    continue
  fi
  FILE_SELECTIONS+=("${source_file}" "" on)
  INDEX=$((INDEX+1))
done

FILE_CHOICES=$(dialog --clear \
  --title "Files to process" \
  --checklist "Choose files:" \
  0 0 0 \
  "${FILE_SELECTIONS[@]}" \
  2>&1 >/dev/tty)
if [ "$?" -ne "0" ]; then
  exit 1
fi

TASK_SELECTIONS=(
  1 "Clean" off
  2 "UnitTest" off
  3 "Cancel" on
)

TASK_CHOICE=$(dialog --clear \
  --title "Agent Task" \
  --radiolist "Select Task:" \
  15 40 3 \
  "${TASK_SELECTIONS[@]}" \
  2>&1 >/dev/tty)
if [ "$?" -ne "0" ]; then
  exit 1
fi

case $TASK_CHOICE in
  1)
    TASK_NAME="Tests"
    PROMPT_BASE=$(cat << EOF
Please clean up the following code, leaving ample documentation.
If the code seems clean already, simply write: DONE
EOF
)
    ;;
  2)
    TASK_NAME="Cleanup"
    PROMPT_BASE=$(cat << EOF
Please write tests for the following source, leaving ample documentation.
The testing backend of choice is pytest.
If there don't seem to be any natural tests, simply write: DONE
EOF
)
      ;;
  3)
    echo Bye!
    exit 0
    ;;
  *)
    echo "Invalid choice!"
    exit 1
    ;;
esac

for source_file in $FILE_CHOICES; do
  if [ ! -s "${source_file}" ]; then
    continue
  fi
  echo $source_file;
  SOURCE=$(<"${source_file}")
  PROMPT="${PROMPT_BASE}
${SOURCE}"
  TRIES=0
  while :; do
    echo Sending over for "${source_file}"
    RESPONSE=$(timeout "${TIMEOUT_MAX}" curl -s "$SERVER/api/generate" \
      -H "Content-Type: application/json" \
      -d "$(jq -n --arg model "$MODEL" --arg prompt "$PROMPT" \
            '{model:$model, prompt:$prompt, stream:false}')" \
            | jq -r '.response')
    TRIES="$((TRIES+1))"
    if [ "$?" -eq "0" ]; then
      echo "Response success!"
      break
    fi
    if [ "$TRIES" -ge "$N_RETRIES" ]; then
      echo "Response timeout!"
      break
    fi
  done
  if [ ! -z "${RESPONSE}" ]; then
    teaissue "${TASK_NAME} ${source_file}" "${RESPONSE}"
  fi
done