-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnexus
More file actions
executable file
·163 lines (139 loc) · 5.47 KB
/
nexus
File metadata and controls
executable file
·163 lines (139 loc) · 5.47 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# ai.sh v34 - Termux Ollama Orchestrator (Full Unrestricted File I/O, Beta Milestone)
set -euo pipefail
IFS=$'\n\t'
# --- CONFIG ---
AI_HOME="${AI_HOME:-$HOME/.ai_agent}"
PROJECTS_DIR="${PROJECTS_DIR:-$HOME/ai_projects}"
LOG_FILE="$AI_HOME/ai.log"
LOG_LEVEL="${LOG_LEVEL:-DEBUG}"
CORE_DB="$AI_HOME/agent_core.db"
MESSENGER_MODEL="gpt-oss:20b-cloud"
PLANNER_MODELS=("core:latest" "2244:latest" "code:latest" "loop:latest" "coin:latest")
EXECUTOR_MODEL="deepseek-v3.1:671b-cloud"
OLLAMA_BIN="$(command -v ollama || echo 'ollama')"
MAX_AGENT_LOOPS=7
SWAP_DIR="$AI_HOME/swap"
HMAC_SECRET_KEY="$AI_HOME/secret.key"
UNRESTRICTED_ACCESS=true # Beta: allow any file path
# --- LOGGING ---
log_to_file(){ echo "[$(date '+%F %T')] [$1] $2" >> "$LOG_FILE"; }
log_info(){ [[ "$LOG_LEVEL" =~ ^(DEBUG|INFO)$ ]] && echo -e "[INFO] $*" >&2 && log_to_file "INFO" "$*"; }
log_warn(){ echo -e "[WARN] $*" >&2 && log_to_file "WARN" "$*"; }
log_error(){ echo -e "[ERROR] $*" >&2 && log_to_file "ERROR" "$*"; }
log_success(){ echo -e "[OK] $*" >&2 && log_to_file "SUCCESS" "$*"; }
log_debug(){ [[ "$LOG_LEVEL" == "DEBUG" ]] && echo -e "[DEBUG] $*" >&2 && log_to_file "DEBUG" "$*"; }
# --- ENV INIT ---
init_environment(){
mkdir -p "$AI_HOME" "$PROJECTS_DIR" "$SWAP_DIR"
[[ -f "$HMAC_SECRET_KEY" ]] || openssl rand -hex 32 > "$HMAC_SECRET_KEY"
chmod 600 "$HMAC_SECRET_KEY"
}
hash_string(){ echo -n "$1" | sha256sum | awk '{print $1}'; }
calculate_hmac(){ local data="$1"; local key; key=$(<"$HMAC_SECRET_KEY"); echo -n "$data" | openssl dgst -sha256 -hmac "$key" | awk '{print $2}'; }
# --- AI WORKER ---
run_worker_fast(){
local model="$1"; shift
local input="$*"
if [[ "$model" == "2244:latest" ]] && (( ${#input} > 10 )); then
log_warn "Input too long for $model; truncating to 10 chars."
input="${input:0:10}"
fi
log_debug "Running $model (len=${#input}): $input"
"$OLLAMA_BIN" run "$model" "$input"
}
# --- FILE ACCESS TOOLS ---
file_write(){
local path="$1"; shift
local content="$*"
if [[ "$UNRESTRICTED_ACCESS" != true ]]; then
log_warn "Restricted mode: file_write limited to $PROJECTS_DIR"
path="$PROJECTS_DIR/$path"
fi
echo "$content" > "$path"
local hmac=$(calculate_hmac "$content")
log_info "Wrote file $path (HMAC $hmac)"
echo "$hmac"
}
file_read(){
local path="$1"
if [[ "$UNRESTRICTED_ACCESS" != true ]]; then
log_warn "Restricted mode: file_read limited to $PROJECTS_DIR"
path="$PROJECTS_DIR/$path"
fi
if [[ ! -f "$path" ]]; then
log_warn "File not found: $path"; return 1
fi
local content; content=$(<"$path")
local hmac=$(calculate_hmac "$content")
log_info "Read file $path (HMAC $hmac)"
echo "$content"
}
# --- CACHE / STORAGE ---
store_output_fast(){
local content="$*"
local ref=$(hash_string "$content")
file_write "$PROJECTS_DIR/$ref.txt" "$content" >/dev/null
echo "$ref"
}
get_cached_response(){
local prompt="$*"; local ref; ref=$(hash_string "$prompt")
[[ -f "$PROJECTS_DIR/$ref.txt" ]] && <"$PROJECTS_DIR/$ref.txt" || echo ""
}
# --- AGI LOOP ---
run_agi_workflow(){
local user_prompt="$*"
local task_id=$(hash_string "$user_prompt$(date +%s%N)" | cut -c1-16)
local project_dir="$PROJECTS_DIR/task-$task_id"
mkdir -p "$project_dir"
log_success "Project workspace: $project_dir (Task ID: $task_id)"
local cached_ref=$(get_cached_response "$user_prompt")
if [[ -n "$cached_ref" ]]; then
log_success "Returning cached response."
echo "$cached_ref"
return
fi
local conversation_history="Initial Request: $user_prompt"
local status="IN_PROGRESS"
for ((i=1;i<=MAX_AGENT_LOOPS;i++)); do
log_info "AGI Loop $i/$MAX_AGENT_LOOPS"
# Messenger
local messenger_output=$(run_worker_fast "$MESSENGER_MODEL" "You are Messenger. Analyze: $user_prompt")
log_debug "Messenger output: $messenger_output"
# Planners
local planner_outputs=()
for model in "${PLANNER_MODELS[@]}"; do
local planner_output=$(run_worker_fast "$model" "Planner input: $messenger_output")
planner_outputs+=("$planner_output")
log_debug "Planner ($model): $planner_output"
done
# Executor
local executor_input="You are Executor. Merge plans:\n${planner_outputs[*]}"
local final_plan=$(run_worker_fast "$EXECUTOR_MODEL" "$executor_input")
log_debug "Executor output: $final_plan"
# Write loop summary (unrestricted)
file_write "$project_dir/loop-$i.txt" "$final_plan" >/dev/null
conversation_history="$conversation_history\n--- Loop $i ---\n$final_plan"
if [[ "$final_plan" == *"[FINAL_ANSWER]"* ]]; then
status="SUCCESS"
break
fi
done
# Store final output (unrestricted)
local final_ref=$(store_output_fast "$conversation_history")
log_success "AGI workflow complete (Status: $status), saved to $PROJECTS_DIR/$final_ref.txt"
echo -e "\n--- Final Answer ---\n$conversation_history"
}
# --- ENTRY POINT ---
main(){
init_environment
local cmd="${1:-}"; shift || true
case "$cmd" in
--setup|-s) log_info "Setup mode: Termux deps handled externally." ;;
--help|-h) echo "Usage: $0 [prompt]"; exit 0 ;;
serve) log_info "Serve mode not implemented for Termux v12"; exit 0 ;;
"") log_info "No prompt given."; exit 0 ;;
*) run_agi_workflow "$cmd" "$@" ;;
esac
}
main "$@"