|
|
|
|
@@ -11,6 +11,104 @@
|
|
|
|
|
agentic: 'You are an autonomous coding agent with FULL control of an Android device. You have a real terminal with shell access, can read/write any file, build APKs, install apps, and execute any command.\n\n## Your Tools (use these EXACT formats):\n\n### Write a file:\n[CREATE_FILE path/to/file.ext]\nfile contents here\n[/CREATE_FILE]\n\n### Run a shell command:\n[RUN_COMMAND]\ncommand here\n[/RUN_COMMAND]\n\n### Build Android APK:\n[BUILD_APK project_name]\n\n### Install APK on device:\n[INSTALL_APK /path/to/file.apk]\n\n### List files:\n[RUN_COMMAND]\nfind . -type f | head -50\n[/RUN_COMMAND]\n\n## IMPORTANT RULES:\n1. ALWAYS use [CREATE_FILE] for EVERY file — the system auto-saves them to the device\n2. ALWAYS use [BUILD_APK] after writing files — the system auto-compiles\n3. ALWAYS use [INSTALL_APK] after building — the system auto-installs\n4. NEVER say "I installed it" unless you used [INSTALL_APK] — the system executes your tags automatically\n5. If a build fails, you will see the error output — FIX the code and try again\n6. Generate COMPLETE files — never use "// ... existing code ..."\n7. For Java: use package ai.z.app, target SDK 36, compile SDK 36\n8. You can run ANY shell command: ls, cat, mkdir, chmod, cp, grep, find, etc.\n9. If Termux tools are available, use: aapt2, d8, ecj, javac, apksigner\n10. Write ALL files first, THEN build, THEN install. Always in that order.\n11. When the user asks to build an app, generate EVERY file needed for a complete working app.\n12. CRITICAL: When you have FULLY completed the user\'s entire task (all files written, all builds done, all installs done), output [TASK_COMPLETE] on a line by itself. This is MANDATORY — the system uses it to know you are done.\n13. If your response is cut off or you haven\'t finished all work, do NOT output [TASK_COMPLETE]. The system will automatically continue you.\n14. NEVER output [TASK_COMPLETE] unless the ENTIRE task is truly done. If there are more files to write, commands to run, or builds to perform, keep working.'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var BUILD_SCRIPT = [
|
|
|
|
|
'#!/bin/sh',
|
|
|
|
|
'set -e',
|
|
|
|
|
'PROJECT_DIR=$(pwd)',
|
|
|
|
|
'BUILD_DIR="$PROJECT_DIR/build"',
|
|
|
|
|
'',
|
|
|
|
|
'AAPT2=$(command -v aapt2 2>/dev/null)',
|
|
|
|
|
'ECJ=$(command -v ecj 2>/dev/null)',
|
|
|
|
|
'D8=$(command -v d8 2>/dev/null)',
|
|
|
|
|
'DX=$(command -v dx 2>/dev/null)',
|
|
|
|
|
'APKSIGNER=$(command -v apksigner 2>/dev/null)',
|
|
|
|
|
'',
|
|
|
|
|
'if [ -z "$AAPT2" ]; then echo "[BUILD FAILED] aapt2 not found. Install: pkg install aapt2"; exit 1; fi',
|
|
|
|
|
'if [ -z "$ECJ" ]; then echo "[BUILD FAILED] ecj not found. Install: pkg install ecj"; exit 1; fi',
|
|
|
|
|
'if [ -z "$D8" ] && [ -z "$DX" ]; then echo "[BUILD FAILED] d8/dx not found. Install: pkg install d8"; exit 1; fi',
|
|
|
|
|
'',
|
|
|
|
|
'ANDROID_JAR=$(dirname "$AAPT2")/../share/aapt2/android.jar',
|
|
|
|
|
'if [ ! -f "$ANDROID_JAR" ]; then echo "[BUILD FAILED] android.jar not found at $ANDROID_JAR"; exit 1; fi',
|
|
|
|
|
'',
|
|
|
|
|
'rm -rf "$BUILD_DIR"',
|
|
|
|
|
'mkdir -p "$BUILD_DIR/gen" "$BUILD_DIR/classes" "$BUILD_DIR/apk"',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Starting build..."',
|
|
|
|
|
'',
|
|
|
|
|
'if [ -d "app/src/main/res" ] && [ "$(find app/src/main/res -type f 2>/dev/null | head -1)" ]; then',
|
|
|
|
|
' echo "[*] Compiling resources..."',
|
|
|
|
|
' "$AAPT2" compile --dir app/src/main/res -o "$BUILD_DIR/compiled_resources.zip" 2>&1 || { echo "[BUILD FAILED] Resource compilation failed"; exit 1; }',
|
|
|
|
|
' RES_ARG="-R $BUILD_DIR/compiled_resources.zip"',
|
|
|
|
|
'else',
|
|
|
|
|
' echo "[*] No resources, skipping..."',
|
|
|
|
|
' RES_ARG=""',
|
|
|
|
|
'fi',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Linking APK..."',
|
|
|
|
|
'"$AAPT2" link -o "$BUILD_DIR/app.unsigned.apk" \\',
|
|
|
|
|
' -I "$ANDROID_JAR" \\',
|
|
|
|
|
' --manifest app/src/main/AndroidManifest.xml \\',
|
|
|
|
|
' $RES_ARG \\',
|
|
|
|
|
' --java "$BUILD_DIR/gen" 2>&1 || { echo "[BUILD FAILED] APK linking failed"; exit 1; }',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Compiling Java..."',
|
|
|
|
|
'rm -f "$BUILD_DIR/sources.txt"',
|
|
|
|
|
'find app/src/main/java -name "*.java" >> "$BUILD_DIR/sources.txt" 2>/dev/null',
|
|
|
|
|
'find "$BUILD_DIR/gen" -name "*.java" >> "$BUILD_DIR/sources.txt" 2>/dev/null',
|
|
|
|
|
'if [ ! -s "$BUILD_DIR/sources.txt" ]; then echo "[BUILD FAILED] No Java source files found"; exit 1; fi',
|
|
|
|
|
'"$ECJ" -source 11 -target 11 -classpath "$ANDROID_JAR" -d "$BUILD_DIR/classes" @"$BUILD_DIR/sources.txt" 2>&1 || { echo "[BUILD FAILED] Java compilation failed"; exit 1; }',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Converting to DEX..."',
|
|
|
|
|
'find "$BUILD_DIR/classes" -name "*.class" > "$BUILD_DIR/classfiles.txt"',
|
|
|
|
|
'if [ ! -s "$BUILD_DIR/classfiles.txt" ]; then echo "[BUILD FAILED] No class files compiled"; exit 1; fi',
|
|
|
|
|
'if [ -n "$D8" ]; then',
|
|
|
|
|
' "$D8" --output "$BUILD_DIR" @"$BUILD_DIR/classfiles.txt" 2>&1 || { echo "[BUILD FAILED] D8 failed"; exit 1; }',
|
|
|
|
|
'else',
|
|
|
|
|
' "$DX" --output "$BUILD_DIR/classes.dex" @"$BUILD_DIR/classfiles.txt" 2>&1 || { echo "[BUILD FAILED] DX failed"; exit 1; }',
|
|
|
|
|
'fi',
|
|
|
|
|
'if [ ! -f "$BUILD_DIR/classes.dex" ]; then echo "[BUILD FAILED] classes.dex not found"; exit 1; fi',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Packaging..."',
|
|
|
|
|
'cd "$BUILD_DIR/apk"',
|
|
|
|
|
'unzip -o ../app.unsigned.apk > /dev/null 2>&1',
|
|
|
|
|
'cp ../classes.dex .',
|
|
|
|
|
'rm -rf META-INF',
|
|
|
|
|
'zip -r ../app.unaligned.apk . > /dev/null 2>&1',
|
|
|
|
|
'cd "$PROJECT_DIR"',
|
|
|
|
|
'',
|
|
|
|
|
'echo "[*] Signing..."',
|
|
|
|
|
'KEYSTORE="$HOME/.android-debug.keystore"',
|
|
|
|
|
'if [ ! -f "$KEYSTORE" ]; then',
|
|
|
|
|
' KEYTOOL=$(command -v keytool 2>/dev/null)',
|
|
|
|
|
' if [ -n "$KEYTOOL" ]; then',
|
|
|
|
|
' "$KEYTOOL" -genkeypair -v -keystore "$KEYSTORE" -storepass android \\',
|
|
|
|
|
' -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 \\',
|
|
|
|
|
' -validity 10000 -dname "CN=Debug,O=Debug,C=US" 2>/dev/null',
|
|
|
|
|
' else',
|
|
|
|
|
' openssl genrsa -out /tmp/dbg.key 2048 2>/dev/null && \\',
|
|
|
|
|
' openssl req -new -key /tmp/dbg.key -out /tmp/dbg.csr -subj "/CN=Debug/O=Debug/C=US" 2>/dev/null && \\',
|
|
|
|
|
' openssl x509 -req -days 10000 -in /tmp/dbg.csr -signkey /tmp/dbg.key -out /tmp/dbg.crt 2>/dev/null && \\',
|
|
|
|
|
' openssl pkcs12 -export -in /tmp/dbg.crt -inkey /tmp/dbg.key -out "$KEYSTORE" -name androiddebugkey -password pass:android 2>/dev/null && \\',
|
|
|
|
|
' rm -f /tmp/dbg.key /tmp/dbg.csr /tmp/dbg.crt',
|
|
|
|
|
' fi',
|
|
|
|
|
'fi',
|
|
|
|
|
'',
|
|
|
|
|
'if [ -f "$KEYSTORE" ] && [ -n "$APKSIGNER" ]; then',
|
|
|
|
|
' "$APKSIGNER" sign --ks "$KEYSTORE" --ks-pass pass:android --ks-key-alias androiddebugkey --key-pass pass:android "$BUILD_DIR/app.unaligned.apk" 2>&1 || true',
|
|
|
|
|
' mv "$BUILD_DIR/app.unaligned.apk" "$BUILD_DIR/app-signed.apk"',
|
|
|
|
|
'elif [ -n "$APKSIGNER" ]; then',
|
|
|
|
|
' mv "$BUILD_DIR/app.unaligned.apk" "$BUILD_DIR/app-signed.apk"',
|
|
|
|
|
' echo "[!] Warning: APK unsigned - no keystore"',
|
|
|
|
|
'else',
|
|
|
|
|
' mv "$BUILD_DIR/app.unaligned.apk" "$BUILD_DIR/app-signed.apk"',
|
|
|
|
|
' echo "[!] Warning: APK unsigned - no apksigner"',
|
|
|
|
|
'fi',
|
|
|
|
|
'',
|
|
|
|
|
'APK_PATH="$BUILD_DIR/app-signed.apk"',
|
|
|
|
|
'APK_SIZE=$(du -h "$APK_PATH" 2>/dev/null | cut -f1)',
|
|
|
|
|
'echo "[BUILD OK] APK: $APK_PATH ($APK_SIZE)"',
|
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
|
|
var state = {
|
|
|
|
|
apiKey: '',
|
|
|
|
|
baseUrl: DEFAULT_BASE_URL,
|
|
|
|
|
@@ -1802,6 +1900,34 @@
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function ensureBuildTools() {
|
|
|
|
|
var check = await shellExec('command -v aapt2 && command -v ecj && (command -v d8 || command -v dx)', termState.homeDir, false);
|
|
|
|
|
if (check.exitCode === 0) return true;
|
|
|
|
|
|
|
|
|
|
termPrint('[*] Installing build tools...', 'info');
|
|
|
|
|
showStatusToast('Installing build tools (first time)...', 'info');
|
|
|
|
|
|
|
|
|
|
var installResult = await shellExec(
|
|
|
|
|
'apt update -y 2>&1 && apt install -y aapt2 ecj dx apksigner 2>&1 || pkg install -y aapt2 ecj dx apksigner 2>&1',
|
|
|
|
|
termState.homeDir, false
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (installResult.output) {
|
|
|
|
|
termPrint(installResult.output.replace(/\n$/, ''), '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var recheck = await shellExec('command -v aapt2 && command -v ecj', termState.homeDir, false);
|
|
|
|
|
if (recheck.exitCode === 0) {
|
|
|
|
|
termPrint('[OK] Build tools installed', 'success');
|
|
|
|
|
showStatusToast('Build tools installed!', 'success');
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
termPrint('[!] Failed to install build tools automatically', 'err');
|
|
|
|
|
termPrint('Please run manually: pkg install aapt2 ecj dx apksigner', 'warning');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function autoExecuteActions(actions, conv) {
|
|
|
|
|
var hasFiles = actions.some(function(a) { return a.type === 'create_file'; });
|
|
|
|
|
var hasBuild = actions.some(function(a) { return a.type === 'build_apk'; });
|
|
|
|
|
@@ -1841,6 +1967,13 @@
|
|
|
|
|
|
|
|
|
|
if (hasBuild) {
|
|
|
|
|
showStatusToast('Building APK...', 'info');
|
|
|
|
|
var toolsReady = await ensureBuildTools();
|
|
|
|
|
if (!toolsReady) {
|
|
|
|
|
var buildFail = '[BUILD FAILED] Build tools not available. Install: pkg install aapt2 ecj dx apksigner';
|
|
|
|
|
resultLog.push(buildFail);
|
|
|
|
|
await agenticRetryOnError(buildFail, conv);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var buildResult = await autoBuildApk(actions);
|
|
|
|
|
resultLog.push(buildResult);
|
|
|
|
|
if (buildResult.indexOf('[BUILD FAILED]') >= 0) {
|
|
|
|
|
@@ -1884,56 +2017,14 @@
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var toolsDir = termState.toolsDir || (termState.homeDir + '/tools');
|
|
|
|
|
var buildScriptPath = toolsDir + '/build.sh';
|
|
|
|
|
await shellMkdirs(toolsDir);
|
|
|
|
|
await shellWriteFile(buildScriptPath, BUILD_SCRIPT);
|
|
|
|
|
await shellExec('chmod +x ' + buildScriptPath, termState.homeDir, false);
|
|
|
|
|
|
|
|
|
|
termPrint('\n--- Building APK ---', 'info');
|
|
|
|
|
|
|
|
|
|
var buildScript =
|
|
|
|
|
'cd ' + projectDir + ' && ' +
|
|
|
|
|
'AAPT2=$(which aapt2 2>/dev/null) && ' +
|
|
|
|
|
'D8=$(which d8 2>/dev/null) && ' +
|
|
|
|
|
'ECJ=$(which ecj 2>/dev/null) && ' +
|
|
|
|
|
'if [ -z "$AAPT2" ]; then echo "[BUILD FAILED] aapt2 not found. Install via: pkg install aapt2"; exit 1; fi && ' +
|
|
|
|
|
'if [ -z "$ECJ" ]; then echo "[BUILD FAILED] ecj not found. Install via: pkg install ecj"; exit 1; fi && ' +
|
|
|
|
|
'mkdir -p build/gen build/classes && ' +
|
|
|
|
|
'echo "[*] Compiling resources..." && ' +
|
|
|
|
|
'$AAPT2 compile --dir app/src/main/res -o build/compiled_resources.zip 2>&1 && ' +
|
|
|
|
|
'echo "[*] Linking APK..." && ' +
|
|
|
|
|
'$AAPT2 link -o build/app.unsigned.apk ' +
|
|
|
|
|
' -I $(dirname $(which aapt2))/../share/aapt2/android.jar ' +
|
|
|
|
|
' --manifest app/src/main/AndroidManifest.xml ' +
|
|
|
|
|
' -R build/compiled_resources.zip ' +
|
|
|
|
|
' --java build/gen 2>&1 && ' +
|
|
|
|
|
'echo "[*] Compiling Java..." && ' +
|
|
|
|
|
'find app/src/main/java -name "*.java" > build/sources.txt 2>/dev/null && ' +
|
|
|
|
|
'ANDROID_JAR=$(dirname $(which aapt2))/../share/aapt2/android.jar && ' +
|
|
|
|
|
'$ECJ -source 11 -target 11 -classpath $ANDROID_JAR -d build/classes @build/sources.txt 2>&1 && ' +
|
|
|
|
|
'echo "[*] DEX..." && ' +
|
|
|
|
|
'if [ -n "$D8" ]; then ' +
|
|
|
|
|
' $D8 --output build/ build/classes/**/*.class 2>&1; ' +
|
|
|
|
|
'else ' +
|
|
|
|
|
' DX=$(which dx 2>/dev/null) && ' +
|
|
|
|
|
' if [ -n "$DX" ]; then $DX --output build/classes.dex build/classes/ 2>&1; ' +
|
|
|
|
|
' else echo "[BUILD FAILED] d8/dx not found. Install via: pkg install dx"; exit 1; fi; ' +
|
|
|
|
|
'fi && ' +
|
|
|
|
|
'echo "[*] Packaging..." && ' +
|
|
|
|
|
'cd build && ' +
|
|
|
|
|
'if [ -f classes.dex ]; then cp classes.dex .; fi && ' +
|
|
|
|
|
'mkdir -p apk_staging && cd apk_staging && ' +
|
|
|
|
|
'unzip -o ../app.unsigned.apk 2>&1 && ' +
|
|
|
|
|
'cp ../classes.dex . 2>/dev/null; cp ../build/classes.dex . 2>/dev/null && ' +
|
|
|
|
|
'zip -r ../app.unaligned.apk . 2>&1 && cd .. && rm -rf apk_staging && ' +
|
|
|
|
|
'echo "[*] Signing..." && ' +
|
|
|
|
|
'APKSIGNER=$(which apksigner 2>/dev/null) && ' +
|
|
|
|
|
'if [ -n "$APKSIGNER" ]; then ' +
|
|
|
|
|
' $APKSIGNER sign --ks /dev/null --ks-pass pass:dummy app.unaligned.apk 2>&1 || ' +
|
|
|
|
|
' cp app.unaligned.apk app-signed.apk; ' +
|
|
|
|
|
'else ' +
|
|
|
|
|
' cp app.unaligned.apk app-signed.apk; ' +
|
|
|
|
|
'fi && ' +
|
|
|
|
|
'APK_PATH="' + projectDir + '/build/app-signed.apk" && ' +
|
|
|
|
|
'APK_SIZE=$(du -h app-signed.apk 2>/dev/null | cut -f1) && ' +
|
|
|
|
|
'echo "[BUILD OK] APK: $APK_PATH ($APK_SIZE)"';
|
|
|
|
|
|
|
|
|
|
var result = await shellExec(buildScript, termState.homeDir, false);
|
|
|
|
|
var result = await shellExec('cd ' + projectDir + ' && sh ' + buildScriptPath, termState.homeDir, false);
|
|
|
|
|
var output = result.output || '';
|
|
|
|
|
termPrint(output.replace(/\n$/, ''), result.exitCode === 0 ? '' : 'err');
|
|
|
|
|
|
|
|
|
|
@@ -1952,7 +2043,7 @@
|
|
|
|
|
} else if (output.indexOf('[BUILD FAILED]') >= 0) {
|
|
|
|
|
return '[BUILD FAILED] ' + output;
|
|
|
|
|
} else if (result.exitCode !== 0) {
|
|
|
|
|
return '[BUILD FAILED] exit=' + result.exitCode + '\n' + output.substring(0, 1500);
|
|
|
|
|
return '[BUILD FAILED] exit=' + result.exitCode + '\n' + output.substring(0, 4000);
|
|
|
|
|
}
|
|
|
|
|
return output.substring(0, 500);
|
|
|
|
|
}
|
|
|
|
|
@@ -1971,7 +2062,7 @@
|
|
|
|
|
if (!conv || !state.apiKey) return;
|
|
|
|
|
|
|
|
|
|
var fixMessage = 'The build failed with this error:\n\n```\n' +
|
|
|
|
|
errorOutput.substring(0, 2000) +
|
|
|
|
|
errorOutput.substring(0, 4000) +
|
|
|
|
|
'\n```\n\nPlease fix the code and rebuild. Write ALL corrected files and use [BUILD_APK] again.';
|
|
|
|
|
|
|
|
|
|
if (state.keepAwake) setWakeLock(true);
|
|
|
|
|
@@ -2136,7 +2227,7 @@
|
|
|
|
|
var result = await Bootstrap.install();
|
|
|
|
|
statusEl.innerHTML = '<p style="color:var(--success);font-size:16px;font-weight:700">✔ Termux environment installed!</p>' +
|
|
|
|
|
'<p>Full Linux shell with bash, coreutils, and package manager ready.</p>' +
|
|
|
|
|
'<p>Install build tools: open Terminal → type <code>pkg install aapt2 ecj dx apksigner</code></p>';
|
|
|
|
|
'<p id="devsetup-tools-status">Installing build tools (aapt2, ecj, d8, apksigner)...</p>';
|
|
|
|
|
btn.querySelector('.btn-text').textContent = 'Installed';
|
|
|
|
|
btn.querySelector('.btn-loader').style.display = 'none';
|
|
|
|
|
|
|
|
|
|
@@ -2150,6 +2241,14 @@
|
|
|
|
|
termState.cwd = env.CWD || env.HOME;
|
|
|
|
|
}
|
|
|
|
|
updateCwdDisplay();
|
|
|
|
|
|
|
|
|
|
var toolsOk = await ensureBuildTools();
|
|
|
|
|
var toolsStatus = $('#devsetup-tools-status');
|
|
|
|
|
if (toolsStatus) {
|
|
|
|
|
toolsStatus.innerHTML = toolsOk
|
|
|
|
|
? '<span style="color:var(--success)">✔ Build tools installed (aapt2, ecj, d8, apksigner)</span>'
|
|
|
|
|
: '<span style="color:var(--warning)">Build tools not installed. Run: pkg install aapt2 ecj dx apksigner</span>';
|
|
|
|
|
}
|
|
|
|
|
} catch(e) {
|
|
|
|
|
statusEl.innerHTML = '<p style="color:var(--danger)">Install failed: ' + e.message + '</p>' +
|
|
|
|
|
'<p>Check your internet connection and try again.</p>';
|
|
|
|
|
|