Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# ShellClaw Environment Variables
# Copy to .env and fill in your values
# NEVER commit the .env file
# Copy this file to ~/.shellclaw/.env with mode 0600.
# Remove the first # from each variable you use. Set its value.
# Keep your .env file outside the repository.
#
# See config.example.toml for TOML equivalents. Env vars override TOML where supported.

# --- LLM Providers (at least one required for cloud path) ---
ANTHROPIC_API_KEY=sk-ant-your-key-here
# ANTHROPIC_API_KEY=sk-ant-your-key-here
# OPENAI_API_KEY=sk-your-key-here
# OPENROUTER_API_KEY=sk-or-your-key-here

Expand All @@ -22,7 +23,7 @@ ANTHROPIC_API_KEY=sk-ant-your-key-here
# SHELLCLAW_LOCAL_MODEL=tinyllama-1.1b-q4

# --- Channels ---
TELEGRAM_BOT_TOKEN=123456:ABC-your-token-here
# TELEGRAM_BOT_TOKEN=123456:ABC-your-token-here
# DISCORD_BOT_TOKEN=your-discord-token-here

# --- Gateway (optional overrides) ---
Expand All @@ -37,7 +38,7 @@ TELEGRAM_BOT_TOKEN=123456:ABC-your-token-here

# --- ASAP Protocol (optional) ---
# SHELLCLAW_ASAP_PUBLIC_BASE_URL=https://your-host.example.com
# SHELLCLAW_ASAP_DESCRIPTION=Override manifest description
# SHELLCLAW_ASAP_DESCRIPTION="Override manifest description"
# SHELLCLAW_ASAP_REGISTRY_URL=https://raw.githubusercontent.com/asap-protocol/asap-protocol/main/registry.json
# SHELLCLAW_ASAP_REVOCATION_LIST_URL=https://registry.asap.agent/revoked_agents.json

Expand Down
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,50 @@ ShellClaw is **not another OpenClaw clone** in a different language. It is a **h

**Build:** `make shellclaw` → binary at `build/shellclaw`. `make test` → builds and runs all tests in `build/`.

**Run:** `./build/shellclaw`
### First run

On Linux, run the installer as your user.

```bash
make shellclaw
./scripts/install.sh
```

The installer places the binary in `~/.local/bin` and creates the configuration in `~/.shellclaw`.
If you install without the Linux script, create the configuration files yourself.
```bash
install -d -m 0700 "$HOME/.shellclaw"
install -m 0600 config.example.toml "$HOME/.shellclaw/config.toml"
install -m 0600 .env.example "$HOME/.shellclaw/.env"
```

Edit `~/.shellclaw/config.toml` to select your model. For the default Anthropic model, set `ANTHROPIC_API_KEY` in `~/.shellclaw/.env`.
Use plain variable assignments so that Bash and systemd can both read the file.

The terminal reads variables from the process environment. Source only the private file that you created and trust.

```bash
(
set -a
. "$HOME/.shellclaw/.env"
set +a
"$HOME/.local/bin/shellclaw" -m "Reply with hello."
)
```

If you used the manual steps, use `./build/shellclaw` in the last command.

### Linux user service

After you configure a provider, start the service. systemd reads the same `~/.shellclaw/.env` file.

```bash
systemctl --user daemon-reload
systemctl --user enable --now shellclaw.service
```

To change the install paths, set `SHELLCLAW_INSTALL_BIN` or `SHELLCLAW_HOME` before you run the installer.
Use the paths printed by the installer when you run it from the terminal. For a custom configuration directory, pass `--config` with the full `config.toml` path.

**Quality checks:**
- `make static` — cppcheck on `src/` (requires cppcheck)
Expand All @@ -59,7 +102,9 @@ ShellClaw is **not another OpenClaw clone** in a different language. It is a **h

See [CONTRIBUTING.md](CONTRIBUTING.md) for PR workflow, coding standards, and the pre-tag `gpio-mockup` release ritual.

**Configuration:** copy [`config.example.toml`](config.example.toml) to `~/.shellclaw/config.toml` and [`.env.example`](.env.example) to `.env`. Phase 3+ keys (ASAP, sandbox, gateway) and the Jetson `[hardware]` block are documented there. Install **libwebsockets** (`pkg-config` must find it) to build the gateway and run `GATEWAY=1 make test_gateway_http`.
[`config.example.toml`](config.example.toml) documents the configuration keys.
Install libwebsockets to build the gateway and run `GATEWAY=1 make test_gateway_http`.
Make sure that `pkg-config` can find libwebsockets.

**Jetson install (v1.0):** `./scripts/install.sh`, `./scripts/build_llama_jetson.sh`, `./scripts/download_model.sh phi3` — details in [`docs/HARDWARE_JETSON.md`](docs/HARDWARE_JETSON.md) and [`docs/LOCAL_INFERENCE.md`](docs/LOCAL_INFERENCE.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ SFIO pins (I2C/UART/SPI) are rejected by GPIO tools with a pinmux error — use

ShellClaw does **not** embed llama.cpp. A separate **`llama-server`** process serves OpenAI-compatible chat completions; `providers/local.c` probes `GET /health` and `GET /v1/models` at startup.

- **Jetson:** CUDA build via `scripts/build_llama_jetson.sh`; systemd unit `llama-server.service` + `/etc/shellclaw/llama-server.env`.
- **Jetson:** CUDA build via `scripts/build_llama_jetson.sh`; systemd unit `llama-server.service` + `~/.shellclaw/llama-server.env`.
- **RPi:** CPU build via `scripts/build_llama_rpi.sh` (validated in Phase 6).

Details: [LOCAL_INFERENCE.md](LOCAL_INFERENCE.md).
Expand Down
8 changes: 6 additions & 2 deletions docs/LOCAL_INFERENCE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Local inference (llama.cpp)

ShellClaw uses an external **`llama-server`** process (from [llama.cpp](https://github.com/ggml-org/llama.cpp)) as an OpenAI-compatible chat backend. The agent binary talks HTTP only — it does not link CUDA or GGML. Configuration: `[providers.local]` in `~/.shellclaw/config.toml` and `/etc/shellclaw/llama-server.env` for the systemd unit.
ShellClaw uses an external **`llama-server`** process (from [llama.cpp](https://github.com/ggml-org/llama.cpp)) as an OpenAI-compatible chat backend. The agent binary talks HTTP only — it does not link CUDA or GGML. Configuration: `[providers.local]` in `~/.shellclaw/config.toml` and `~/.shellclaw/llama-server.env` for the systemd user unit.

For software architecture, see [ARCHITECTURE.md](ARCHITECTURE.md). For Jetson setup order, see [HARDWARE_JETSON.md](HARDWARE_JETSON.md).

Expand Down Expand Up @@ -66,9 +66,13 @@ Verify: `llama-server --version` must **not** report CUDA/GPU.

## systemd integration

`./scripts/install.sh` copies board-specific env to `/etc/shellclaw/llama-server.env` and installs user units:
Run `./scripts/install.sh` to install the user units and `~/.shellclaw/llama-server.env`.
To use a different directory for `llama-server.env`, set `SHELLCLAW_ETC_DIR` before you run the installer.
Set `MODEL` in that file to the full path to your downloaded model.
Start both services after you install the model and `llama-server`.

```bash
systemctl --user daemon-reload
systemctl --user enable --now llama-server.service shellclaw.service
```

Expand Down
199 changes: 143 additions & 56 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
#!/usr/bin/env bash
# Install helper: copy systemd user units next to ~/.config/systemd/user and print enable steps.
#
# Purpose: `./scripts/install.sh` — aligns with Phase 4 PRD §4.4.30; Phase 5 Task 3.5 adds
# board-specific llama-server env under /etc/shellclaw (override via SHELLCLAW_ETC_DIR).
# Install ShellClaw and its systemd user units.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
UNIT_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user"
INSTALL_BIN="${SHELLCLAW_INSTALL_BIN:-${HOME%/}/.local/bin/shellclaw}"
SHELLCLAW_HOME="${SHELLCLAW_HOME:-${HOME%/}/.shellclaw}"
LLAMA_ENV_DIR="${SHELLCLAW_ETC_DIR:-/etc/shellclaw}"
BUILT_BIN="${ROOT}/build/shellclaw"
STATE_DIR="${SHELLCLAW_HOME:-${HOME%/}/.shellclaw}"
LLAMA_ENV_DIR="${SHELLCLAW_ETC_DIR:-${STATE_DIR}}"
LLAMA_ENV_DEST="${LLAMA_ENV_DIR}/llama-server.env"
LEGACY_LLAMA_ENV="/etc/shellclaw/llama-server.env"
JETSON_ENV="${ROOT}/systemd/llama-server.jetson.env"
RPI_ENV="${ROOT}/systemd/llama-server.rpi.env"

install_llama_env_file() {
local src_template="$1"
mkdir -p "${LLAMA_ENV_DIR}"
if [[ -w "${LLAMA_ENV_DIR}" ]]; then
install -m 0644 "${src_template}" "${LLAMA_ENV_DEST}"
elif [[ "${LLAMA_ENV_DIR}" == /etc/shellclaw ]] && command -v sudo >/dev/null 2>&1; then
sudo install -m 0644 "${src_template}" "${LLAMA_ENV_DEST}"
else
echo "Cannot write ${LLAMA_ENV_DEST}; set SHELLCLAW_ETC_DIR or run with sufficient permissions." >&2
validate_path() {
if [[ "$1" != /* || "$1" == *[[:cntrl:]]* ]]; then
printf 'Use an absolute path without control characters for %s.\n' "$2" >&2
exit 1
fi
}

validate_env_dir() {
validate_path "$1" "$2"
if [[ "$1" == *[\*\?\[\\]* ]]; then
printf 'Use a path without wildcard characters for %s.\n' "$2" >&2
exit 1
fi
}

quote_unit_value() {
local value="${1//%/%%}"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
printf '"%s"' "${value}"
}

render_unit() {
local line
while IFS= read -r line; do
case "${line}" in
Environment=SHELLCLAW_HOME=*)
printf 'Environment=%s\n' "$(quote_unit_value "SHELLCLAW_HOME=${STATE_DIR}")"
;;
EnvironmentFile=-%h/.shellclaw/.env)
printf 'EnvironmentFile=-%s/.env\n' "${STATE_DIR//%/%%}"
;;
EnvironmentFile=%h/.shellclaw/llama-server.env)
printf 'EnvironmentFile=%s\n' "${LLAMA_ENV_DEST//%/%%}"
;;
ExecStart=%h/.local/bin/shellclaw*)
# systemd limits executable names. env passes the full path as an argument.
printf 'ExecStart=:/usr/bin/env -- %s --config %s%s\n' "$(quote_unit_value "${INSTALL_BIN}")" \
"$(quote_unit_value "${STATE_DIR}/config.toml")" "${line#*%h/.shellclaw/config.toml}"
;;
*) printf '%s\n' "${line}" ;;
esac
done <"$1"
}

install_config() {
local line
local default_prefix='~/.shellclaw/'
local state_prefix="${STATE_DIR//\\/\\\\}"
state_prefix="${state_prefix//\"/\\\"}/"
while IFS= read -r line; do
printf '%s\n' "${line//"${default_prefix}"/"${state_prefix}"}"
done <"${ROOT}/config.example.toml" >"${STATE_DIR}/config.toml"
}

detect_board_id() {
if [[ ! -x "${INSTALL_BIN}" ]]; then
echo "unknown"
Expand Down Expand Up @@ -60,6 +101,14 @@ board_llama_env_source() {
esac
}

install_llama_env() (
local stage
stage="$(mktemp "${LLAMA_ENV_DIR}/.llama-server.env.XXXXXX")"
trap 'rm -f -- "${stage}"' EXIT
install -m 0600 "$1" "${stage}"
mv -n -- "${stage}" "${LLAMA_ENV_DEST}"
)

board_download_model_key() {
local board="$1"
case "${board}" in
Expand All @@ -83,56 +132,94 @@ maybe_prompt_download_model() {
return 0
fi
local reply=""
read -r -p "Download default GGUF now via scripts/download_model.sh (${model_key})? [y/N] " reply
read -r -p "Download the default model (${model_key})? [y/N] " reply || return 0
if [[ "${reply}" =~ ^[Yy]$ ]]; then
bash "${ROOT}/scripts/download_model.sh" "${model_key}"
fi
}

mkdir -p "${UNIT_DIR}"
install -m 0644 "${ROOT}/systemd/shellclaw.service" "${UNIT_DIR}/shellclaw.service"
install -m 0644 "${ROOT}/systemd/llama-server.service" "${UNIT_DIR}/llama-server.service"
validate_path "${UNIT_DIR}" XDG_CONFIG_HOME
validate_path "${INSTALL_BIN}" SHELLCLAW_INSTALL_BIN
validate_env_dir "${STATE_DIR}" SHELLCLAW_HOME
validate_env_dir "${LLAMA_ENV_DIR}" SHELLCLAW_ETC_DIR

board_id="$(detect_board_id)"
env_src="$(board_llama_env_source "${board_id}")"
install_llama_env_file "${env_src}"

case "${board_id}" in
jetson_orin_nano)
echo "Installed llama-server env (Jetson) at ${LLAMA_ENV_DEST}"
;;
rpi_zero2w)
echo "Installed llama-server env (RPi) at ${LLAMA_ENV_DEST}"
;;
stub)
echo "Board stub: installed Jetson llama-server env template at ${LLAMA_ENV_DEST} (dev/CI)"
;;
unknown|*)
echo "Board unknown: installed Jetson llama-server env template at ${LLAMA_ENV_DEST}"
;;
esac
directories=("${STATE_DIR}" "${UNIT_DIR}")
if [[ ! -e "${LLAMA_ENV_DEST}" ]]; then
directories+=("${LLAMA_ENV_DIR}")
fi
selected_bin="${INSTALL_BIN}"
copy_binary=0
if [[ ! -x "${INSTALL_BIN}" || ( -z "${SHELLCLAW_INSTALL_BIN:-}" && -x "${BUILT_BIN}" ) ]]; then
if [[ ! -x "${BUILT_BIN}" ]]; then
echo "Run make shellclaw before installation." >&2
exit 1
fi
selected_bin="${BUILT_BIN}"
copy_binary=1
directories+=("$(dirname "${INSTALL_BIN}")")
fi

maybe_prompt_download_model "${board_id}"
# Pass --headless before --help so the binary must parse both options.
if ! "${selected_bin}" --headless --help >/dev/null 2>&1; then
printf 'ShellClaw at %s does not accept --headless.\n' "${selected_bin}" >&2
echo 'Run make shellclaw. If you set SHELLCLAW_INSTALL_BIN, use a compatible binary.' >&2
exit 1
fi

echo "Installed units into ${UNIT_DIR}"
echo ""
if [[ ! -x "${INSTALL_BIN}" ]]; then
echo "Warning: ShellClaw binary not found or not executable at ${INSTALL_BIN}" >&2
echo "Build or copy the binary before starting the service (see README)." >&2
echo ""
llama_env_source=""
if [[ ! -e "${LLAMA_ENV_DEST}" && -z "${SHELLCLAW_ETC_DIR:-}" &&
( -e "${LEGACY_LLAMA_ENV}" || -L "${LEGACY_LLAMA_ENV}" ) ]]; then
if [[ ! -f "${LEGACY_LLAMA_ENV}" || ! -r "${LEGACY_LLAMA_ENV}" ]]; then
printf 'Cannot read the previous llama-server environment at %s.\n' "${LEGACY_LLAMA_ENV}" >&2
exit 1
fi
llama_env_source="${LEGACY_LLAMA_ENV}"
fi
echo "Environment (override before enable if needed):"
echo " SHELLCLAW_INSTALL_BIN=${INSTALL_BIN}"
echo " SHELLCLAW_HOME=${SHELLCLAW_HOME}"
echo " SHELLCLAW_ETC_DIR=${LLAMA_ENV_DIR} (llama-server.env)"
echo ""
echo "Next:"
echo ' Prefix `systemctl` with `sudo` only if you use system-wide systemd instead of `--user`.'

umask 077
mkdir -p "${directories[@]}"
for dir in "${directories[@]}"; do
if [[ ! -w "${dir}" || ! -x "${dir}" ]]; then
printf 'Cannot write to %s.\n' "${dir}" >&2
exit 1
fi
done
for path in "${INSTALL_BIN}" "${STATE_DIR}/config.toml" "${STATE_DIR}/.env" "${LLAMA_ENV_DEST}" \
"${UNIT_DIR}/shellclaw.service" "${UNIT_DIR}/llama-server.service"; do
if [[ ( -e "${path}" || -L "${path}" ) && ! -f "${path}" ]]; then
printf 'Expected a regular file at %s.\n' "${path}" >&2
exit 1
fi
done

if ((copy_binary)); then
install -m 0755 "${selected_bin}" "${INSTALL_BIN}"
fi
if [[ ! -e "${STATE_DIR}/config.toml" ]]; then
install_config
fi
if [[ ! -e "${STATE_DIR}/.env" ]]; then
install -m 0600 "${ROOT}/.env.example" "${STATE_DIR}/.env"
fi
board_id="$(detect_board_id)"
if [[ ! -e "${LLAMA_ENV_DEST}" ]]; then
install_llama_env "${llama_env_source:-$(board_llama_env_source "${board_id}")}"
fi
unit_stage="$(mktemp -d)"
trap 'rm -rf -- "${unit_stage}"' EXIT
for unit in shellclaw.service llama-server.service; do
render_unit "${ROOT}/systemd/${unit}" >"${unit_stage}/${unit}"
install -m 0644 "${unit_stage}/${unit}" "${UNIT_DIR}/${unit}"
done

echo "Installed user units in ${UNIT_DIR}."
echo "ShellClaw binary is at ${INSTALL_BIN}."
echo "Edit ${STATE_DIR}/config.toml before you start ShellClaw."
echo "For a cloud provider, set its API key in ${STATE_DIR}/.env."
echo " systemctl --user daemon-reload"
echo " systemctl --user enable --now shellclaw.service"
echo "Optional (local inference):"
echo " Build llama-server: ./scripts/build_llama_jetson.sh or ./scripts/build_llama_rpi.sh"
echo " Download model: ./scripts/download_model.sh phi3 # Jetson default"
echo " ./scripts/download_model.sh tinyllama # RPi default"
echo "If you use local inference, build llama-server and download a model."
echo "Set MODEL in ${LLAMA_ENV_DEST} to the model path."
echo " systemctl --user enable --now llama-server.service"
echo " (uses ${LLAMA_ENV_DEST}; restart after editing MODEL=)"

maybe_prompt_download_model "${board_id}"
5 changes: 4 additions & 1 deletion src/channels/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ int channel_send_chunked(const char *text, size_t max_bytes,
channel_chunk_post_fn post, void *ctx,
channel_chunk_fail_t *fail);

/** Poll result when the channel can no longer receive input. */
#define CHANNEL_POLL_CLOSED 2

/**
* Channel vtable: init, poll, send, cleanup. CLI and Telegram implement this.
*/
Expand All @@ -107,7 +110,7 @@ typedef struct channel {
* Poll for incoming message. Block up to timeout_ms.
* @param out Filled on success; caller must channel_incoming_msg_clear().
* @param timeout_ms Max wait in milliseconds.
* @return 1 if message received, 0 if timeout/no message, -1 on error.
* @return 1 for a message, 0 for no message, CHANNEL_POLL_CLOSED for closed input, or -1 on error.
*/
int (*poll)(channel_incoming_msg_t *out, int timeout_ms);
/**
Expand Down
Loading
Loading