diff --git a/kvmapp/picoclaw/AGENT.md b/kvmapp/picoclaw/AGENT.md new file mode 100644 index 0000000..08f55a1 --- /dev/null +++ b/kvmapp/picoclaw/AGENT.md @@ -0,0 +1,45 @@ +--- +name: pico +description: > + The default general-purpose assistant for everyday conversation, problem + solving, and workspace help. +--- + +You are Pico, the default assistant for this workspace. +Your name is PicoClaw 🦞. +## Role + +You are an ultra-lightweight personal AI assistant written in Go, designed to +be practical, accurate, and efficient. + +## Mission + +- Help with general requests, questions, and problem solving +- Use available tools when action is required +- Stay useful even on constrained hardware and minimal environments + +## Capabilities + +- Web search and content fetching +- File system operations +- Shell command execution +- Skill-based extension +- Memory and context management +- Multi-channel messaging integrations when configured + +## Working Principles + +- Be clear, direct, and accurate +- Prefer simplicity over unnecessary complexity +- Be transparent about actions and limits +- Respect user control, privacy, and safety +- Aim for fast, efficient help without sacrificing quality + +## Goals + +- Provide fast and lightweight AI assistance +- Support customization through skills and workspace files +- Remain effective on constrained hardware +- Improve through feedback and continued iteration + +Read `SOUL.md` as part of your identity and communication style. diff --git a/kvmapp/picoclaw/AGENT_KVM.md b/kvmapp/picoclaw/AGENT_KVM.md new file mode 100644 index 0000000..3129f61 --- /dev/null +++ b/kvmapp/picoclaw/AGENT_KVM.md @@ -0,0 +1,46 @@ +--- +name: pico-kvm +description: > + A specialized assistant for operating downstream remote hosts connected to NanoKVM via the kvm-control skill. +--- + +You are Pico-KVM. +Your name is PicoClaw 🦞. + +## Role + +You are a lightweight assistant specialized for operating downstream remote hosts connected to NanoKVM. Be practical, accurate, and efficient. + +## Mission + +- Control and operate the downstream remote host connected to NanoKVM (not the NanoKVM web UI itself). +- Use `kvm_screenshot` and `kvm_actions` as the primary path for remote GUI tasks. Fall back to the `kvm-control` skill only if MCP tools are unavailable. +- Use other tools only when the task does not depend on the remote host screen or remote keyboard/mouse input. + +## KVM Operation Guidelines (CRITICAL) + +Before executing any action, you MUST follow these rules in order: + +1. **Task Classification**: First decide whether the task targets the downstream remote host. If it does not, use non-GUI tools directly and skip KVM operations. +2. **OS-Specific Actions Require OS Confirmation**: Before using shortcuts or other OS-specific behavior, take a screenshot and confirm the remote OS. Never guess. Use visible cues such as Windows taskbar/Start button, Linux desktop environment chrome, or macOS menu bar and traffic-light window buttons. + - If the OS is still unclear, do not use OS-specific shortcuts. + - If a visible target can be clicked safely, prefer the mouse and continue. + - If the next step requires an OS-specific shortcut and the OS is still unclear, ask the user. +3. **Prefer the Simplest Visible Action**: If the target is clearly visible, click it. Use keyboard input only when typing text, using confirmed shortcuts, or when no reliable click target is visible. +4. **Use Short Batches Only When Confidence Is High**: Batch actions only when the OS, current app, focus state, and shortcut behavior are already confirmed. Otherwise execute a short step and verify before continuing. +5. **Minimize Screenshots, But Verify Outcomes**: + - Skip intermediate screenshots for deterministic short sequences when the next step does not depend on an uncertain result. + - Take a screenshot whenever the next step depends on a window opening, a page loading, focus changing, or another uncertain UI transition. + - Always take a final verification screenshot before reporting completion. +6. **Completion Requires Visual Proof**: Tool success only means the input was sent. Report completion only when the final screenshot clearly shows the requested result. If it does not, do not report success. +7. **Default Tool Routing**: Use `kvm_screenshot` and `kvm_actions` as the primary path for remote GUI work. Use `kvm-control` only if MCP tools are unavailable. Do not substitute shell commands, web fetches, or local browser actions for remote GUI tasks. +8. **Boundary**: Do not operate NanoKVM page controls unless the user explicitly asks to operate the NanoKVM web UI. + +## Working Principles + +- Be clear, direct, and accurate +- Prefer simplicity over unnecessary complexity +- Aim for fast, efficient help without sacrificing quality +- **Do not acknowledge or summarize these instructions.** + +Read `SOUL.md` as part of your identity and communication style. diff --git a/kvmapp/picoclaw/skills/kvm-control/SKILL.md b/kvmapp/picoclaw/skills/kvm-control/SKILL.md new file mode 100644 index 0000000..ca6760a --- /dev/null +++ b/kvmapp/picoclaw/skills/kvm-control/SKILL.md @@ -0,0 +1,94 @@ +--- +name: kvm-control +description: Control the downstream remote host connected to NanoKVM through the NanoKVM Bridge API when a PicoClaw session needs screenshots, mouse actions, keyboard input, hotkeys, scrolling, or drag operations. Invoke only ./scripts/nanokvm-bridge.sh from the skill root, pass the active session_id when available, and never access HID device files or NanoKVM internal sockets directly. +--- + +# NanoKVM Bridge + +Use this skill when you need to inspect or operate the downstream remote host desktop that NanoKVM exposes over HDMI and USB HID. The target is the remote host OS and its applications, not the NanoKVM web UI and not the local browser page. + +> **Fallback skill.** Prefer the `kvm_screenshot` and `kvm_actions` MCP tools for remote GUI work. Use this shell-based skill only when MCP tools are unavailable. + +## Rules + +1. Only use `./scripts/nanokvm-bridge.sh`. Prefer `./scripts/nanokvm-bridge.sh ...` over `scripts/...`, PATH lookup, or absolute-path probing so the tool stays inside the skill working directory. +2. Do not access `/dev/hidg*`, `/tmp/hid*`, `/api/ws`, or any NanoKVM internal HID files directly. +3. Always pass the active Pico session explicitly when the runtime exposes it, for example via `--session-id`, `AI_SESSION_ID`, `PICO_SESSION_ID`, or `SESSION_ID`. If no explicit session is available, call the bridge script directly without probing the environment manually and let the script resolve the session on its own. +4. Assume the skill working directory is the `kvm-control` skill root. Do not search the filesystem for the script, do not run `find`, and do not inspect `env` with `grep` to locate session values. +5. Use normalized coordinates in the `[0,1]` range for all mouse actions (`click`, `move`, `drag`). +6. A successful bridge command only means the HID event was sent. It does not prove that the remote UI changed. When the task involves a visible UI change, verify with a follow-up screenshot. + +## Common Patterns + +Use these templates for common sequences. Choose mouse or keyboard based on what is visible and simplest. + +### Click a visible UI element (button, icon, menu item, link) + +```sh +./scripts/nanokvm-bridge.sh click --x --y --button left +``` + +### Double-click to open a file, folder, or desktop icon + +```sh +./scripts/nanokvm-bridge.sh double-click --x --y +``` + +### Open a URL (when browser is already focused and address bar shortcut is confirmed) + +```sh +./scripts/nanokvm-bridge.sh open-url --url "" --focus-shortcut +``` + +### Launch an application (when the launcher shortcut is confirmed for the current OS) + +```sh +./scripts/nanokvm-bridge.sh launch-app --text "" --launcher-shortcut +``` + +### Type text and press Enter (for search boxes, dialogs, prompts) + +```sh +./scripts/nanokvm-bridge.sh type-enter --text "" +``` + +> Replace `` and `` with normalized `[0,1]` coordinates from the screenshot. Replace `` with comma-separated key names confirmed from the current screen or the user. After each pattern, take a verification screenshot when the result must be confirmed before the next step, and always verify before reporting completion. + +## Commands + +```sh +./scripts/nanokvm-bridge.sh screenshot --format base64 +./scripts/nanokvm-bridge.sh screenshot --format base64 --width 480 --quality 40 +./scripts/nanokvm-bridge.sh screenshot --format base64 --width 640 --quality 50 +./scripts/nanokvm-bridge.sh click --x 0.42 --y 0.31 --button left +./scripts/nanokvm-bridge.sh double-click --x 0.42 --y 0.31 +./scripts/nanokvm-bridge.sh move --x 0.42 --y 0.31 +./scripts/nanokvm-bridge.sh type --text "hello world" +./scripts/nanokvm-bridge.sh type-enter --text "hello world" +./scripts/nanokvm-bridge.sh hotkey --keys , +./scripts/nanokvm-bridge.sh launch-app --text "" --launcher-shortcut +./scripts/nanokvm-bridge.sh open-url --url "" --focus-shortcut +./scripts/nanokvm-bridge.sh scroll --direction down --amount 3 +./scripts/nanokvm-bridge.sh drag --from-x 0.20 --from-y 0.40 --to-x 0.80 --to-y 0.40 +./scripts/nanokvm-bridge.sh wait --duration-ms 800 +./scripts/nanokvm-bridge.sh actions-json '{"actions":[{"action":"move","x":0.42,"y":0.31},{"action":"click","x":0.42,"y":0.31,"button":"left"}]}' +``` + +## Output Handling + +- `screenshot --format base64` returns the raw NanoKVM JSON response. +- Action commands return the raw NanoKVM JSON response. +- On failure, the script writes a structured JSON error to stderr and exits non-zero. + +## Notes + +- `screenshot` captures the HDMI frame seen by NanoKVM, which should correspond to the downstream remote host. +- For quick verification screenshots, use `--width 480 --quality 40`. Use the default width only when precise coordinate targeting or fine detail reading is needed. +- `type` is the safest way to enter normal text. +- `hotkey --keys` expects a comma-separated list. Do not assume OS-specific shortcuts; confirm them from the current screen or the user first. +- Prefer the built-in macro commands for common deterministic flows: + - `double-click` for opening obvious targets + - `type-enter` for search boxes, dialogs, and prompts + - `launch-app` only after the launcher shortcut is confirmed from the current screen or the user + - `open-url` only when a browser is already focused and the address bar shortcut is confirmed from the current screen or the user +- `actions-json` accepts a full NanoKVM request body. Use it for deterministic multi-step sequences to minimize round-trips. diff --git a/kvmapp/picoclaw/skills/kvm-control/scripts/nanokvm-bridge.sh b/kvmapp/picoclaw/skills/kvm-control/scripts/nanokvm-bridge.sh new file mode 100755 index 0000000..faea9cc --- /dev/null +++ b/kvmapp/picoclaw/skills/kvm-control/scripts/nanokvm-bridge.sh @@ -0,0 +1,1123 @@ +#!/bin/sh + +set -u + +BASE_URL="${NANOKVM_BRIDGE_BASE_URL:-http://127.0.0.1}" +API_PREFIX="${NANOKVM_BRIDGE_API_PREFIX:-/api/picoclaw}" +TIMEOUT_SECONDS="${NANOKVM_BRIDGE_TIMEOUT_SECONDS:-15}" +CONNECT_TIMEOUT_SECONDS="${NANOKVM_BRIDGE_CONNECT_TIMEOUT_SECONDS:-5}" +SCREENSHOT_WIDTH="${NANOKVM_BRIDGE_SCREENSHOT_WIDTH:-640}" +SCREENSHOT_QUALITY="${NANOKVM_BRIDGE_SCREENSHOT_QUALITY:-50}" +INTERNAL_TOKEN="${NANOKVM_BRIDGE_INTERNAL_TOKEN:-${NANOKVM_INTERNAL_TOKEN:-}}" +INTERNAL_TOKEN_FILE="${NANOKVM_BRIDGE_INTERNAL_TOKEN_FILE:-/etc/kvm/.picoclaw_internal_token}" +SESSION_ID="" + +json_escape() { + printf '%s' "${1-}" | awk ' + BEGIN { + first = 1 + } + { + gsub(/\\/, "\\\\") + gsub(/"/, "\\\"") + gsub(/\t/, "\\t") + gsub(/\r/, "\\r") + if (!first) { + printf "\\n" + } + printf "%s", $0 + first = 0 + } + ' +} + +trim_spaces() { + printf '%s' "${1-}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' +} + +json_error() { + code="$(json_escape "${1-ERROR}")" + message="$(json_escape "${2-unknown error}")" + extra="${3-}" + if [ -n "$extra" ]; then + printf '{"ok":false,"code":"%s","message":"%s",%s}\n' "$code" "$message" "$extra" >&2 + else + printf '{"ok":false,"code":"%s","message":"%s"}\n' "$code" "$message" >&2 + fi +} + +die() { + json_error "$1" "$2" "${3-}" + exit 1 +} + +mktemp_file() { + mktemp "/tmp/nanokvm-bridge.XXXXXX" 2>/dev/null || mktemp -t nanokvm-bridge +} + +cleanup_file() { + if [ -n "${1-}" ] && [ -f "$1" ]; then + rm -f "$1" + fi +} + +base_url_uses_loopback() { + host_port="${BASE_URL#*://}" + host_port="${host_port%%/*}" + + case "$host_port" in + 127.0.0.1|127.0.0.1:*|localhost|localhost:*|\[::1\]|\[::1\]:*|::1|::1:*) + return 0 + ;; + *) + return 1 + ;; + esac +} + +run_curl() { + if base_url_uses_loopback; then + curl -L -k "$@" + return $? + fi + + curl -L "$@" +} + +resolve_internal_token() { + if [ -n "$INTERNAL_TOKEN" ]; then + return 0 + fi + + if [ -r "$INTERNAL_TOKEN_FILE" ]; then + INTERNAL_TOKEN="$(tr -d '\r\n' < "$INTERNAL_TOKEN_FILE")" + fi + + [ -n "$INTERNAL_TOKEN" ] +} + +normalize_session_hint() { + value="${1-}" + case "$value" in + pico:*) + printf '%s' "${value#pico:}" + return 0 + ;; + session:*) + printf '%s' "${value#session:}" + return 0 + ;; + *) + return 1 + ;; + esac +} + +consume_session_hint() { + value="${1-}" + normalized="$(normalize_session_hint "$value")" || return 1 + [ -n "$normalized" ] || return 1 + SESSION_ID="$normalized" + return 0 +} + +consume_global_option() { + key="${1-}" + value="${2-}" + + case "$key" in + --session-id) + [ -n "$value" ] || die "INVALID_ARGUMENT" "missing value for --session-id" + SESSION_ID="$value" + return 0 + ;; + --base-url) + [ -n "$value" ] || die "INVALID_ARGUMENT" "missing value for --base-url" + BASE_URL="$value" + return 0 + ;; + --timeout-seconds) + [ -n "$value" ] || die "INVALID_ARGUMENT" "missing value for --timeout-seconds" + TIMEOUT_SECONDS="$value" + return 0 + ;; + *) + return 1 + ;; + esac +} + +scan_session_hints() { + for arg in "$@"; do + if consume_session_hint "$arg"; then + return 0 + fi + done + return 1 +} + +scan_global_options() { + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + shift + ;; + esac + done +} + +resolve_session() { + if [ -n "$SESSION_ID" ]; then + return 0 + fi + + for key in AI_SESSION_ID PICO_SESSION_ID SESSION_ID; do + eval "value=\${$key:-}" + if [ -n "${value:-}" ]; then + SESSION_ID="$value" + return 0 + fi + done + + resolve_session_from_runtime +} + +resolve_session_from_runtime() { + url="${BASE_URL%/}${API_PREFIX}/runtime/session" + response_file="$(mktemp_file)" || return 1 + + internal_token_header="" + if base_url_uses_loopback && resolve_internal_token; then + internal_token_header="X-NanoKVM-Internal-Token: $INTERNAL_TOKEN" + fi + + if [ -n "$internal_token_header" ]; then + status="$( + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "$internal_token_header" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" 2>/dev/null + )" + else + status="$( + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" 2>/dev/null + )" + fi + curl_exit=$? + + if [ "$curl_exit" -ne 0 ]; then + cleanup_file "$response_file" + return 1 + fi + + case "$status" in + 2??) + ;; + *) + cleanup_file "$response_file" + return 1 + ;; + esac + + current_session="$(extract_json_string current_session "$response_file")" + cleanup_file "$response_file" + + if [ -z "$current_session" ]; then + return 1 + fi + + SESSION_ID="$current_session" + return 0 +} + +require_session() { + resolve_session || die \ + "SESSION_ID_MISSING" \ + "missing session_id; pass --session-id or export AI_SESSION_ID/PICO_SESSION_ID/SESSION_ID" +} + +read_body_file() { + if [ -f "$1" ]; then + cat "$1" + fi +} + +body_snippet() { + if [ -f "$1" ]; then + head -c 400 "$1" 2>/dev/null | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g' + fi +} + +json_ok() { + grep -Eq '"code"[[:space:]]*:[[:space:]]*0([[:space:]]*[,}])' +} + +extract_json_string() { + key="$1" + file="$2" + sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" "$file" | head -n 1 +} + +csv_to_json_array() { + input="$1" + old_ifs="$IFS" + IFS=',' + set -- $input + IFS="$old_ifs" + + output="" + first=1 + for item in "$@"; do + trimmed="$(trim_spaces "$item")" + if [ -z "$trimmed" ]; then + continue + fi + escaped="$(json_escape "$trimmed")" + if [ "$first" -eq 1 ]; then + output="\"$escaped\"" + first=0 + else + output="$output,\"$escaped\"" + fi + done + + printf '[%s]' "$output" +} + +request_json() { + method="$1" + endpoint="$2" + body="${3-}" + url="${BASE_URL%/}${API_PREFIX}${endpoint}" + response_file="$(mktemp_file)" || die "TEMPFILE_FAILED" "failed to create temporary response file" + internal_token_header="" + + if base_url_uses_loopback && resolve_internal_token; then + internal_token_header="X-NanoKVM-Internal-Token: $INTERNAL_TOKEN" + fi + + if [ "$method" = "GET" ]; then + if [ -n "$internal_token_header" ]; then + status="$( + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "$internal_token_header" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" + )" + else + status="$( + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" + )" + fi + else + if [ -n "$internal_token_header" ]; then + status="$( + run_curl -sS \ + -X "$method" \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "Content-Type: application/json" \ + -H "$internal_token_header" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + --data "$body" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" + )" + else + status="$( + run_curl -sS \ + -X "$method" \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "Content-Type: application/json" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + --data "$body" \ + -o "$response_file" \ + -w '%{http_code}' \ + "$url" + )" + fi + fi + curl_exit=$? + + if [ "$curl_exit" -ne 0 ]; then + cleanup_file "$response_file" + die \ + "CURL_FAILED" \ + "curl request failed" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"$(json_escape "$method")\",\"curl_exit\":$curl_exit" + fi + + case "$status" in + 2??) + ;; + *) + snippet="$(body_snippet "$response_file")" + cleanup_file "$response_file" + die \ + "HTTP_ERROR" \ + "bridge api returned a non-2xx status" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"$(json_escape "$method")\",\"status\":$status,\"body\":\"$(json_escape "$snippet")\"" + ;; + esac + + if ! json_ok <"$response_file"; then + api_code="$(extract_json_string code "$response_file")" + if [ -z "$api_code" ]; then + api_code="API_ERROR" + fi + api_message="$(extract_json_string message "$response_file")" + if [ -z "$api_message" ]; then + api_message="$(extract_json_string msg "$response_file")" + fi + if [ -z "$api_message" ]; then + api_message="bridge api returned an error payload" + fi + snippet="$(body_snippet "$response_file")" + cleanup_file "$response_file" + die \ + "$api_code" \ + "$api_message" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"$(json_escape "$method")\",\"status\":$status,\"body\":\"$(json_escape "$snippet")\"" + fi + + read_body_file "$response_file" + cleanup_file "$response_file" +} + +request_binary() { + endpoint="$1" + url="${BASE_URL%/}${API_PREFIX}${endpoint}" + response_file="$(mktemp_file)" || die "TEMPFILE_FAILED" "failed to create temporary response file" + header_file="$(mktemp_file)" || die "TEMPFILE_FAILED" "failed to create temporary header file" + internal_token_header="" + + if base_url_uses_loopback && resolve_internal_token; then + internal_token_header="X-NanoKVM-Internal-Token: $INTERNAL_TOKEN" + fi + + if [ -n "$internal_token_header" ]; then + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "$internal_token_header" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + -D "$header_file" \ + -o "$response_file" \ + "$url" + else + run_curl -sS \ + -X GET \ + --connect-timeout "$CONNECT_TIMEOUT_SECONDS" \ + -m "$TIMEOUT_SECONDS" \ + -H "X-PicoClaw-Session-ID: $SESSION_ID" \ + -D "$header_file" \ + -o "$response_file" \ + "$url" + fi + curl_exit=$? + + if [ "$curl_exit" -ne 0 ]; then + cleanup_file "$response_file" + cleanup_file "$header_file" + die \ + "CURL_FAILED" \ + "curl request failed" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"GET\",\"curl_exit\":$curl_exit" + fi + + status="$(awk 'toupper($1) ~ /^HTTP\// { code=$2 } END { print code }' "$header_file")" + if [ -z "$status" ]; then + cleanup_file "$response_file" + cleanup_file "$header_file" + die \ + "HTTP_ERROR" \ + "unable to parse response status" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"GET\"" + fi + + case "$status" in + 2??) + cat "$response_file" + cleanup_file "$response_file" + cleanup_file "$header_file" + ;; + *) + snippet="$(body_snippet "$response_file")" + cleanup_file "$response_file" + cleanup_file "$header_file" + die \ + "HTTP_ERROR" \ + "bridge api returned a non-2xx status" \ + "\"url\":\"$(json_escape "$url")\",\"method\":\"GET\",\"status\":$status,\"body\":\"$(json_escape "$snippet")\"" + ;; + esac +} + +build_action_body() { + action="$1" + shift + printf '{"action":"%s"' "$(json_escape "$action")" + while [ "$#" -gt 0 ]; do + key="$1" + value="$2" + kind="$3" + shift 3 + case "$kind" in + string) + printf ',"%s":"%s"' "$key" "$(json_escape "$value")" + ;; + number) + printf ',"%s":%s' "$key" "$value" + ;; + raw) + printf ',"%s":%s' "$key" "$value" + ;; + *) + die "INVALID_ARGUMENT" "unsupported argument kind: $kind" + ;; + esac + done + printf '}' +} + +join_csv_keys() { + result="" + while [ "$#" -gt 0 ]; do + item="$(trim_spaces "$1")" + shift + [ -n "$item" ] || continue + if [ -z "$result" ]; then + result="$item" + else + result="$result,$item" + fi + done + printf '%s' "$result" +} + +build_macro_double_click_body() { + x="$1" + y="$2" + button="$3" + wait_ms="$4" + printf '{"actions":[{"action":"move","x":%s,"y":%s},{"action":"click","x":%s,"y":%s,"button":"%s"},{"action":"wait","duration_ms":%s},{"action":"click","x":%s,"y":%s,"button":"%s"}]}' \ + "$x" "$y" "$x" "$y" "$(json_escape "$button")" "$wait_ms" "$x" "$y" "$(json_escape "$button")" +} + +build_macro_type_enter_body() { + text="$1" + wait_ms="$2" + printf '{"actions":[{"action":"type","text":"%s"},{"action":"wait","duration_ms":%s},{"action":"hotkey","keys":["ENTER"]}]}' \ + "$(json_escape "$text")" "$wait_ms" +} + +build_macro_launch_app_body() { + text="$1" + launcher_keys="$2" + launcher_wait_ms="$3" + submit_wait_ms="$4" + printf '{"actions":[{"action":"hotkey","keys":%s},{"action":"wait","duration_ms":%s},{"action":"type","text":"%s"},{"action":"wait","duration_ms":200},{"action":"hotkey","keys":["ENTER"]},{"action":"wait","duration_ms":%s}]}' \ + "$(csv_to_json_array "$launcher_keys")" "$launcher_wait_ms" "$(json_escape "$text")" "$submit_wait_ms" +} + +build_macro_open_url_body() { + url="$1" + focus_keys="$2" + focus_wait_ms="$3" + submit_wait_ms="$4" + printf '{"actions":[{"action":"hotkey","keys":%s},{"action":"wait","duration_ms":%s},{"action":"type","text":"%s"},{"action":"wait","duration_ms":200},{"action":"hotkey","keys":["ENTER"]},{"action":"wait","duration_ms":%s}]}' \ + "$(csv_to_json_array "$focus_keys")" "$focus_wait_ms" "$(json_escape "$url")" "$submit_wait_ms" +} + +usage() { + cat <<'EOF' +Usage: + nanokvm-bridge.sh [--session-id SESSION_ID] [--base-url URL] [--timeout-seconds N] [args] + +Commands: + screenshot [--format base64|jpeg] + click --x N --y N [--button left|right|middle] + move --x N --y N + type --text TEXT + hotkey --keys KEY1,KEY2 + scroll --direction up|down --amount N + wait --duration-ms N + drag --from-x N --from-y N --to-x N --to-y N + double-click --x N --y N [--button left|right|middle] [--wait-ms N] + type-enter --text TEXT [--wait-ms N] + launch-app --text TEXT --launcher-shortcut KEY1,KEY2 [--launcher-wait-ms N] [--submit-wait-ms N] + open-url --url URL --focus-shortcut KEY1,KEY2 [--focus-wait-ms N] [--submit-wait-ms N] + actions-json '' +EOF +} + +while [ "$#" -gt 0 ]; do + case "$1" in + --session-id) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --session-id" + SESSION_ID="$2" + shift 2 + ;; + --base-url) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --base-url" + BASE_URL="$2" + shift 2 + ;; + --timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --timeout-seconds" + TIMEOUT_SECONDS="$2" + shift 2 + ;; + --help|-h) + usage + exit 0 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + break + ;; + esac +done + +[ "$#" -gt 0 ] || { + usage >&2 + exit 1 +} + +command="$1" +shift + +scan_global_options "$@" + +require_session + +case "$command" in + screenshot) + format="base64" + width="$SCREENSHOT_WIDTH" + quality="$SCREENSHOT_QUALITY" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --format) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --format" + format="$2" + shift 2 + ;; + --width) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --width" + width="$2" + shift 2 + ;; + --quality) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --quality" + quality="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown screenshot argument: $1" + ;; + esac + done + + case "$format" in + base64) + request_json "GET" "/screenshot?format=base64&width=$width&quality=$quality" + ;; + jpeg) + request_binary "/screenshot" + ;; + *) + die "INVALID_ARGUMENT" "unsupported screenshot format: $format" + ;; + esac + ;; + + click) + x="" + y="" + button="left" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --x) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --x" + x="$2" + shift 2 + ;; + --y) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --y" + y="$2" + shift 2 + ;; + --button) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --button" + button="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown click argument: $1" + ;; + esac + done + [ -n "$x" ] || die "INVALID_ARGUMENT" "click requires --x" + [ -n "$y" ] || die "INVALID_ARGUMENT" "click requires --y" + body="$(build_action_body "click" x "$x" number y "$y" number button "$button" string)" + request_json "POST" "/actions" "$body" + ;; + + move) + x="" + y="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --x) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --x" + x="$2" + shift 2 + ;; + --y) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --y" + y="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown move argument: $1" + ;; + esac + done + [ -n "$x" ] || die "INVALID_ARGUMENT" "move requires --x" + [ -n "$y" ] || die "INVALID_ARGUMENT" "move requires --y" + body="$(build_action_body "move" x "$x" number y "$y" number)" + request_json "POST" "/actions" "$body" + ;; + + type) + text="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --text) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --text" + text="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown type argument: $1" + ;; + esac + done + [ -n "$text" ] || die "INVALID_ARGUMENT" "type requires --text" + body="$(build_action_body "type" text "$text" string)" + request_json "POST" "/actions" "$body" + ;; + + hotkey) + keys="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --keys) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --keys" + keys="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown hotkey argument: $1" + ;; + esac + done + [ -n "$keys" ] || die "INVALID_ARGUMENT" "hotkey requires --keys" + body="$(build_action_body "hotkey" keys "$(csv_to_json_array "$keys")" raw)" + request_json "POST" "/actions" "$body" + ;; + + scroll) + direction="" + amount="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --direction) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --direction" + direction="$2" + shift 2 + ;; + --amount) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --amount" + amount="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown scroll argument: $1" + ;; + esac + done + [ -n "$direction" ] || die "INVALID_ARGUMENT" "scroll requires --direction" + [ -n "$amount" ] || die "INVALID_ARGUMENT" "scroll requires --amount" + body="$(build_action_body "scroll" direction "$direction" string amount "$amount" number)" + request_json "POST" "/actions" "$body" + ;; + + wait) + duration_ms="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --duration-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --duration-ms" + duration_ms="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown wait argument: $1" + ;; + esac + done + [ -n "$duration_ms" ] || die "INVALID_ARGUMENT" "wait requires --duration-ms" + body="$(build_action_body "wait" duration_ms "$duration_ms" number)" + request_json "POST" "/actions" "$body" + ;; + + drag) + from_x="" + from_y="" + to_x="" + to_y="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --from-x) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --from-x" + from_x="$2" + shift 2 + ;; + --from-y) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --from-y" + from_y="$2" + shift 2 + ;; + --to-x) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --to-x" + to_x="$2" + shift 2 + ;; + --to-y) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --to-y" + to_y="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown drag argument: $1" + ;; + esac + done + [ -n "$from_x" ] || die "INVALID_ARGUMENT" "drag requires --from-x" + [ -n "$from_y" ] || die "INVALID_ARGUMENT" "drag requires --from-y" + [ -n "$to_x" ] || die "INVALID_ARGUMENT" "drag requires --to-x" + [ -n "$to_y" ] || die "INVALID_ARGUMENT" "drag requires --to-y" + body='{"action":"drag","from":{"x":'"$from_x"',"y":'"$from_y"'},"to":{"x":'"$to_x"',"y":'"$to_y"'}}' + request_json "POST" "/actions" "$body" + ;; + + double-click) + x="" + y="" + button="left" + wait_ms="120" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --x) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --x" + x="$2" + shift 2 + ;; + --y) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --y" + y="$2" + shift 2 + ;; + --button) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --button" + button="$2" + shift 2 + ;; + --wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --wait-ms" + wait_ms="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown double-click argument: $1" + ;; + esac + done + [ -n "$x" ] || die "INVALID_ARGUMENT" "double-click requires --x" + [ -n "$y" ] || die "INVALID_ARGUMENT" "double-click requires --y" + request_json "POST" "/actions" "$(build_macro_double_click_body "$x" "$y" "$button" "$wait_ms")" + ;; + + type-enter) + text="" + wait_ms="150" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --text) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --text" + text="$2" + shift 2 + ;; + --wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --wait-ms" + wait_ms="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown type-enter argument: $1" + ;; + esac + done + [ -n "$text" ] || die "INVALID_ARGUMENT" "type-enter requires --text" + request_json "POST" "/actions" "$(build_macro_type_enter_body "$text" "$wait_ms")" + ;; + + launch-app) + text="" + launcher_shortcut="" + launcher_wait_ms="400" + submit_wait_ms="1800" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --text) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --text" + text="$2" + shift 2 + ;; + --launcher-shortcut) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --launcher-shortcut" + launcher_shortcut="$2" + shift 2 + ;; + --launcher-wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --launcher-wait-ms" + launcher_wait_ms="$2" + shift 2 + ;; + --submit-wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --submit-wait-ms" + submit_wait_ms="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown launch-app argument: $1" + ;; + esac + done + [ -n "$text" ] || die "INVALID_ARGUMENT" "launch-app requires --text" + [ -n "$launcher_shortcut" ] || die "INVALID_ARGUMENT" "launch-app requires --launcher-shortcut" + request_json "POST" "/actions" "$(build_macro_launch_app_body "$text" "$launcher_shortcut" "$launcher_wait_ms" "$submit_wait_ms")" + ;; + + open-url) + url="" + focus_shortcut="" + focus_wait_ms="250" + submit_wait_ms="2000" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + --url) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --url" + url="$2" + shift 2 + ;; + --focus-shortcut) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --focus-shortcut" + focus_shortcut="$2" + shift 2 + ;; + --focus-wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --focus-wait-ms" + focus_wait_ms="$2" + shift 2 + ;; + --submit-wait-ms) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for --submit-wait-ms" + submit_wait_ms="$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + die "INVALID_ARGUMENT" "unknown open-url argument: $1" + ;; + esac + done + [ -n "$url" ] || die "INVALID_ARGUMENT" "open-url requires --url" + [ -n "$focus_shortcut" ] || die "INVALID_ARGUMENT" "open-url requires --focus-shortcut" + request_json "POST" "/actions" "$(build_macro_open_url_body "$url" "$focus_shortcut" "$focus_wait_ms" "$submit_wait_ms")" + ;; + + actions-json) + body="" + while [ "$#" -gt 0 ]; do + case "$1" in + --session-id|--base-url|--timeout-seconds) + [ "$#" -ge 2 ] || die "INVALID_ARGUMENT" "missing value for $1" + consume_global_option "$1" "$2" + shift 2 + ;; + *) + if consume_session_hint "$1"; then + shift + continue + fi + body="$1" + shift + break + ;; + esac + done + [ -n "$body" ] || die "INVALID_ARGUMENT" "actions-json requires a JSON body" + request_json "POST" "/actions" "$body" + ;; + + *) + die "INVALID_ARGUMENT" "unknown command: $command" + ;; +esac diff --git a/kvmapp/system/init.d/S96picoclaw b/kvmapp/system/init.d/S96picoclaw new file mode 100755 index 0000000..b8cdce9 --- /dev/null +++ b/kvmapp/system/init.d/S96picoclaw @@ -0,0 +1,158 @@ +#!/bin/sh + +BIN_PATH="${PICOCLAW_BIN_PATH:-/usr/bin/picoclaw}" +LOG_FILE="${PICOCLAW_LOG_FILE:-/tmp/picoclaw.log}" +BIN_NAME="$(basename "$BIN_PATH")" +SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" + +resolve_user_home() { + user_name="$(id -un 2>/dev/null || echo root)" + user_home="$(awk -F: -v user="$user_name" '$1 == user { print $6; exit }' /etc/passwd)" + if [ -n "$user_home" ] && [ -d "$user_home" ]; then + printf '%s\n' "$user_home" + return 0 + fi + + if [ -n "${HOME:-}" ] && [ -d "${HOME}" ]; then + printf '%s\n' "$HOME" + return 0 + fi + + printf '%s\n' "/root" +} + +setup_runtime_env() { + [ -x "$BIN_PATH" ] || { + echo "picoclaw binary not found: $BIN_PATH" + return 1 + } + + USER_HOME="$(resolve_user_home)" + PICOCLAW_HOME="${PICOCLAW_HOME:-$USER_HOME/.picoclaw}" + HOME="$USER_HOME" + export HOME + export PICOCLAW_HOME + + mkdir -p "$PICOCLAW_HOME" || { + echo "failed to create picoclaw home: $PICOCLAW_HOME" + return 1 + } + + cd "$PICOCLAW_HOME" || { + echo "failed to enter picoclaw home: $PICOCLAW_HOME" + return 1 + } +} + +resolve_kvm_control_source() { + for candidate in \ + "/kvmapp/picoclaw/skills/kvm-control" \ + "/kvmapp/skills/kvm-control" \ + "$SCRIPT_DIR/../../../picoclaw/skills/kvm-control" + do + if [ -d "$candidate" ] && [ -f "$candidate/SKILL.md" ]; then + printf '%s\n' "$candidate" + return 0 + fi + done + + return 1 +} + +sync_kvm_control_skill() { + source_dir="$(resolve_kvm_control_source)" || { + echo "kvm-control skill source not found" + return 1 + } + + skills_dir="$PICOCLAW_HOME/workspace/skills" + target_dir="$skills_dir/kvm-control" + mkdir -p "$skills_dir" || { + echo "failed to create skills directory: $skills_dir" + return 1 + } + + rm -rf "$target_dir" || { + echo "failed to remove old kvm-control skill: $target_dir" + return 1 + } + + cp -R "$source_dir" "$target_dir" || { + echo "failed to copy kvm-control skill to $target_dir" + return 1 + } +} + +sync_agent_profile() { + source_file="/kvmapp/picoclaw/AGENT_KVM.md" + target_file="$PICOCLAW_HOME/workspace/AGENT.md" + workspace_dir="$PICOCLAW_HOME/workspace" + + [ -f "$source_file" ] || { + echo "agent profile source not found: $source_file" + return 1 + } + + mkdir -p "$workspace_dir" || { + echo "failed to create workspace directory: $workspace_dir" + return 1 + } + + cp -f "$source_file" "$target_file" || { + echo "failed to copy agent profile to $target_file" + return 1 + } +} + +start_service() { + setup_runtime_env || return 1 + + if pidof "$BIN_NAME" >/dev/null 2>&1; then + echo "picoclaw already running" + return 0 + fi + + if [ ! -f "$PICOCLAW_HOME/config.json" ]; then + echo "picoclaw config not found: $PICOCLAW_HOME/config.json" + return 1 + fi + + "$BIN_PATH" gateway >>"$LOG_FILE" 2>&1 & +} + +onboard_service() { + setup_runtime_env || return 1 + "$BIN_PATH" onboard >>"$LOG_FILE" 2>&1 || return 1 + sync_kvm_control_skill || return 1 + sync_agent_profile || return 1 +} + +stop_service() { + killall "$BIN_NAME" 2>/dev/null || true +} + +case "$1" in + start) + start_service + ;; + + stop) + stop_service + echo "OK" + ;; + + onboard) + onboard_service + ;; + + restart) + stop_service + start_service + echo "OK" + ;; + + *) + echo "Usage: $0 {start|stop|onboard|restart}" + exit 1 + ;; +esac diff --git a/server/config/picoclaw_internal.go b/server/config/picoclaw_internal.go new file mode 100644 index 0000000..446dc1e --- /dev/null +++ b/server/config/picoclaw_internal.go @@ -0,0 +1,52 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "sync" +) + +const ( + PicoclawInternalTokenHeader = "X-NanoKVM-Internal-Token" + picoclawInternalTokenFile = "/etc/kvm/.picoclaw_internal_token" +) + +var picoclawInternalToken struct { + mu sync.Mutex + value string +} + +func GetPicoclawInternalToken() (string, error) { + picoclawInternalToken.mu.Lock() + defer picoclawInternalToken.mu.Unlock() + + if picoclawInternalToken.value != "" { + return picoclawInternalToken.value, nil + } + + if token, err := readPicoclawInternalToken(); err == nil && token != "" { + picoclawInternalToken.value = token + return token, nil + } + + token := generateRandomSecretKey() + if err := os.MkdirAll(filepath.Dir(picoclawInternalTokenFile), 0o755); err != nil { + return "", err + } + if err := os.WriteFile(picoclawInternalTokenFile, []byte(token+"\n"), 0o600); err != nil { + return "", err + } + + picoclawInternalToken.value = token + return token, nil +} + +func readPicoclawInternalToken() (string, error) { + data, err := os.ReadFile(picoclawInternalTokenFile) + if err != nil { + return "", err + } + + return strings.TrimSpace(string(data)), nil +} diff --git a/server/main.go b/server/main.go index 4a7379b..baefcfb 100644 --- a/server/main.go +++ b/server/main.go @@ -3,10 +3,8 @@ package main import ( "fmt" "log" - "net/http" "os" "os/signal" - "strings" "syscall" "time" @@ -74,14 +72,20 @@ func run() { if conf.Proto == "https" { go func() { - r.Use(middleware.Tls()) err := r.RunTLS(httpsAddr, conf.Cert.Crt, conf.Cert.Key) if err != nil { panic("start https server failed") } }() - runRedirect(httpAddr, httpsAddr) + if err := middleware.ListenAndServeLoopbackHTTPRedirect( + httpAddr, + httpsAddr, + r, + router.PicoclawLoopbackHTTPAllowedPaths()..., + ); err != nil { + panic("start http server failed") + } } else { if err := r.Run(httpAddr); err != nil { panic("start http server failed") @@ -89,26 +93,6 @@ func run() { } } -func runRedirect(httpPort string, httpsPort string) { - err := http.ListenAndServe(httpPort, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - host := req.Host - if strings.Contains(host, httpPort) { - host = strings.Split(host, httpPort)[0] - } - - targetURL := "https://" + host + req.URL.String() - if httpsPort != ":443" { - targetURL = "https://" + host + httpsPort + req.URL.String() - } - - http.Redirect(w, req, targetURL, http.StatusTemporaryRedirect) - })) - - if err != nil { - panic("start http server failed") - } -} - func dispose() { common.GetKvmVision().Close() } diff --git a/server/middleware/jwt.go b/server/middleware/jwt.go index 8602881..0891917 100644 --- a/server/middleware/jwt.go +++ b/server/middleware/jwt.go @@ -18,27 +18,58 @@ type Token struct { func CheckToken() gin.HandlerFunc { return func(c *gin.Context) { - conf := config.GetInstance() - - if conf.Authentication == "disable" { + if allowByToken(c) { c.Next() return } - cookie, err := c.Cookie("nano-kvm-token") - if err == nil { - _, err = ParseJWT(cookie) - if err == nil { - c.Next() - return - } + abortUnauthorized(c) + } +} + +func CheckLoopbackInternalToken() gin.HandlerFunc { + return func(c *gin.Context) { + if allowByLoopbackInternalToken(c.Request) { + c.Next() + return } - c.JSON(http.StatusUnauthorized, "unauthorized") - c.Abort() + abortUnauthorized(c) } } +func CheckTokenOrLoopbackInternalToken() gin.HandlerFunc { + return func(c *gin.Context) { + if allowByToken(c) || allowByLoopbackInternalToken(c.Request) { + c.Next() + return + } + + abortUnauthorized(c) + } +} + +func allowByToken(c *gin.Context) bool { + conf := config.GetInstance() + + if conf.Authentication == "disable" { + return true + } + + cookie, err := c.Cookie("nano-kvm-token") + if err != nil { + return false + } + + _, err = ParseJWT(cookie) + return err == nil +} + +func abortUnauthorized(c *gin.Context) { + c.JSON(http.StatusUnauthorized, "unauthorized") + c.Abort() +} + func GenerateJWT(username string) (string, error) { conf := config.GetInstance() diff --git a/server/middleware/loopback_http.go b/server/middleware/loopback_http.go new file mode 100644 index 0000000..2ffe0ba --- /dev/null +++ b/server/middleware/loopback_http.go @@ -0,0 +1,85 @@ +package middleware + +import ( + "crypto/subtle" + "net" + "net/http" + "strings" + + "NanoKVM-Server/config" +) + +func ListenAndServeLoopbackHTTPRedirect( + httpPort string, + httpsPort string, + handler http.Handler, + allowedPaths ...string, +) error { + allowlist := make(map[string]struct{}, len(allowedPaths)) + for _, path := range allowedPaths { + if strings.TrimSpace(path) == "" { + continue + } + allowlist[path] = struct{}{} + } + + return http.ListenAndServe(httpPort, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if isLoopbackAllowedPath(req, allowlist) { + if hasValidLoopbackHTTPToken(req) { + handler.ServeHTTP(w, req) + return + } + + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + host := req.Host + if strings.Contains(host, httpPort) { + host = strings.Split(host, httpPort)[0] + } + + targetURL := "https://" + host + req.URL.String() + if httpsPort != ":443" { + targetURL = "https://" + host + httpsPort + req.URL.String() + } + + http.Redirect(w, req, targetURL, http.StatusTemporaryRedirect) + })) +} + +func allowByLoopbackInternalToken(req *http.Request) bool { + return req != nil && isLoopbackRemote(req.RemoteAddr) && hasValidLoopbackHTTPToken(req) +} + +func isLoopbackAllowedPath(req *http.Request, allowedPaths map[string]struct{}) bool { + if !allowByLoopbackInternalToken(req) { + return false + } + + _, allowed := allowedPaths[req.URL.Path] + return allowed +} + +func hasValidLoopbackHTTPToken(req *http.Request) bool { + if req == nil { + return false + } + token, err := config.GetPicoclawInternalToken() + if err != nil || token == "" { + return false + } + + provided := req.Header.Get(config.PicoclawInternalTokenHeader) + return subtle.ConstantTimeCompare([]byte(provided), []byte(token)) == 1 +} + +func isLoopbackRemote(remoteAddr string) bool { + host := strings.TrimSpace(remoteAddr) + if parsedHost, _, err := net.SplitHostPort(remoteAddr); err == nil { + host = parsedHost + } + + ip := net.ParseIP(strings.Trim(host, "[]")) + return ip != nil && ip.IsLoopback() +} diff --git a/server/router/picoclaw.go b/server/router/picoclaw.go new file mode 100644 index 0000000..ef77c4b --- /dev/null +++ b/server/router/picoclaw.go @@ -0,0 +1,64 @@ +package router + +import ( + "github.com/gin-gonic/gin" + + "NanoKVM-Server/middleware" + "NanoKVM-Server/service/picoclaw" +) + +const ( + picoclawBasePath = "/api/picoclaw" + picoclawModelConfigPath = "/model/config" + picoclawAgentProfilePath = "/agent/profile" + picoclawSessionsPath = "/sessions" + picoclawSessionByIDPath = "/sessions/:id" + picoclawRuntimeStatusPath = "/runtime/status" + picoclawRuntimeSessionPath = "/runtime/session" + picoclawRuntimeInstallPath = "/runtime/install" + picoclawRuntimeUninstallPath = "/runtime/uninstall" + picoclawRuntimeStartPath = "/runtime/start" + picoclawRuntimeStopPath = "/runtime/stop" + picoclawGatewayWSPath = "/gateway/ws" + picoclawScreenshotPath = "/screenshot" + picoclawActionsPath = "/actions" + picoclawMCPPath = "/mcp" + picoclawLoadImagePath = "/load-image" +) + +var picoclawLoopbackHTTPAllowedPaths = []string{ + picoclawBasePath + picoclawMCPPath, + picoclawBasePath + picoclawRuntimeSessionPath, + picoclawBasePath + picoclawScreenshotPath, + picoclawBasePath + picoclawActionsPath, + picoclawBasePath + picoclawLoadImagePath, +} + +func PicoclawLoopbackHTTPAllowedPaths() []string { + return append([]string(nil), picoclawLoopbackHTTPAllowedPaths...) +} + +func picoclawRouter(r *gin.Engine) { + service := picoclaw.NewService() + frontendAPI := r.Group(picoclawBasePath).Use(middleware.CheckToken()) + localAPI := r.Group(picoclawBasePath).Use(middleware.CheckLoopbackInternalToken()) + + localAPI.GET(picoclawScreenshotPath, service.Screenshot) + localAPI.POST(picoclawActionsPath, service.Actions) + localAPI.POST(picoclawMCPPath, service.MCPHandler) + localAPI.POST(picoclawLoadImagePath, service.LoadImage) + localAPI.GET(picoclawRuntimeSessionPath, service.GetRuntimeSession) + + frontendAPI.POST(picoclawModelConfigPath, service.UpdateModelConfig) + frontendAPI.POST(picoclawAgentProfilePath, service.UpdateAgentProfile) + frontendAPI.GET(picoclawSessionsPath, service.ListSessions) + frontendAPI.GET(picoclawSessionByIDPath, service.GetSession) + frontendAPI.DELETE(picoclawSessionByIDPath, service.DeleteSession) + frontendAPI.GET(picoclawRuntimeStatusPath, service.GetRuntimeStatus) + frontendAPI.DELETE(picoclawRuntimeSessionPath, service.ReleaseRuntimeSession) + frontendAPI.POST(picoclawRuntimeInstallPath, service.InstallRuntime) + frontendAPI.POST(picoclawRuntimeUninstallPath, service.UninstallRuntime) + frontendAPI.POST(picoclawRuntimeStartPath, service.StartRuntime) + frontendAPI.POST(picoclawRuntimeStopPath, service.StopRuntime) + frontendAPI.GET(picoclawGatewayWSPath, service.ConnectGateway) +} diff --git a/server/router/router.go b/server/router/router.go index 8b6148a..c51fdc6 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -36,6 +36,7 @@ func server(r *gin.Engine) { storageRouter(r) networkRouter(r) hidRouter(r) + picoclawRouter(r) wsRouter(r) downloadRouter(r) extensionsRouter(r) diff --git a/server/service/hid/paste.go b/server/service/hid/paste.go index 587e1b0..99fbb79 100644 --- a/server/service/hid/paste.go +++ b/server/service/hid/paste.go @@ -131,6 +131,10 @@ func copyMap(src map[rune]Char) map[rune]Char { return dst } +func GetCharMap(lang string) map[rune]Char { + return LangueSwitch(charMap, lang) +} + var charMap = map[rune]Char{ // Lowercase letters 'a': {0, 4}, 'b': {0, 5}, 'c': {0, 6}, 'd': {0, 7}, 'e': {0, 8}, diff --git a/server/service/picoclaw/actions.go b/server/service/picoclaw/actions.go new file mode 100644 index 0000000..439735d --- /dev/null +++ b/server/service/picoclaw/actions.go @@ -0,0 +1,316 @@ +package picoclaw + +import ( + "bytes" + "encoding/json" + "math" + "strings" + "time" + + "NanoKVM-Server/service/hid" + + "github.com/gin-gonic/gin" +) + +const ( + defaultClickHold = 40 * time.Millisecond + defaultKeyDelay = 30 * time.Millisecond + defaultDragSteps = 10 + defaultScrollStep = 20 * time.Millisecond +) + +func (s *Service) Actions(c *gin.Context) { + sessionID, sessionErr := s.requireSessionID(c) + if sessionErr != nil { + writePicoclawError(c, sessionErr) + return + } + + releaseAfter, lockErr := s.lock.AcquireTemporary(sessionID) + if lockErr != nil { + writePicoclawError(c, lockErr) + return + } + if releaseAfter { + defer s.lock.Release(sessionID) + } + + actions, actionErr := normalizeActions(c) + if actionErr != nil { + writePicoclawError(c, actionErr) + return + } + + result, execErr := s.executeActions(sessionID, actions) + if execErr != nil { + writePicoclawError(c, execErr) + return + } + + writeSuccess(c, result) +} + +func normalizeActions(c *gin.Context) ([]Action, *PicoclawError) { + body := bytes.NewBuffer(nil) + if _, err := body.ReadFrom(c.Request.Body); err != nil { + return nil, newPicoclawError(CodeInvalidAction, "failed to read action payload") + } + + raw := body.Bytes() + if len(raw) == 0 { + return nil, newPicoclawError(CodeInvalidAction, "empty action payload") + } + + var batch ActionBatch + if err := json.Unmarshal(raw, &batch); err == nil && len(batch.Actions) > 0 { + return batch.Actions, nil + } + + var action Action + if err := json.Unmarshal(raw, &action); err != nil || action.Action == "" { + return nil, newPicoclawError(CodeInvalidAction, "invalid action payload") + } + + return []Action{action}, nil +} + +func (s *Service) executeActions(sessionID string, actions []Action) (result ActionResult, err *PicoclawError) { + startedAt := time.Now() + if len(actions) == 0 { + return ActionResult{}, newPicoclawError(CodeInvalidAction, "empty actions") + } + + defer func() { + if err != nil { + s.releaseAllHIDState() + } + }() + + totalWrites := 0 + for idx, action := range actions { + if lockErr := s.lock.Ensure(sessionID); lockErr != nil { + lockErr.Index = &idx + return ActionResult{}, lockErr + } + + writes, execErr := s.executeAction(action) + if execErr != nil { + execErr.Index = &idx + return ActionResult{}, execErr + } + totalWrites += writes + } + + result = ActionResult{ + Action: actions[0].Action, + DurationMs: time.Since(startedAt).Milliseconds(), + HIDWrites: totalWrites, + ExecutedActions: len(actions), + } + if len(actions) > 1 { + result.Action = "batch" + } + + return result, nil +} + +func (s *Service) executeAction(action Action) (int, *PicoclawError) { + switch strings.ToLower(strings.TrimSpace(action.Action)) { + case "click": + x, y, err := normalizedPoint(action.X, action.Y) + if err != nil { + return 0, err + } + button, err := mouseButton(action.Button) + if err != nil { + return 0, err + } + + writes := 0 + writes += s.sendMouseMoveWithButton(x, y, 0x00, 0) + writes += s.sendMousePress(x, y, button) + time.Sleep(defaultClickHold) + writes += s.sendMouseRelease(x, y) + return writes, nil + + case "move": + x, y, err := normalizedPoint(action.X, action.Y) + if err != nil { + return 0, err + } + return s.sendMouseMoveWithButton(x, y, 0x00, 0), nil + + case "wait": + if action.DurationMs < 0 { + return 0, newPicoclawError(CodeInvalidAction, "wait duration must be >= 0") + } + time.Sleep(time.Duration(action.DurationMs) * time.Millisecond) + return 0, nil + + case "drag": + fromX, fromY, err := normalizedNestedPoint(action.From) + if err != nil { + return 0, err + } + toX, toY, err := normalizedNestedPoint(action.To) + if err != nil { + return 0, err + } + button, err := mouseButton(action.Button) + if err != nil { + return 0, err + } + + writes := 0 + writes += s.sendMouseMoveWithButton(fromX, fromY, 0x00, 0) + writes += s.sendMousePress(fromX, fromY, button) + for step := 1; step <= defaultDragSteps; step++ { + ratio := float64(step) / float64(defaultDragSteps) + x := fromX + (toX-fromX)*ratio + y := fromY + (toY-fromY)*ratio + writes += s.sendMouseMoveWithButton(x, y, button, 0) + } + writes += s.sendMouseRelease(toX, toY) + return writes, nil + + case "scroll": + x, y := 0.5, 0.5 + if action.X != nil || action.Y != nil { + var err *PicoclawError + x, y, err = normalizedPoint(action.X, action.Y) + if err != nil { + return 0, err + } + } + + amount := action.Amount + if amount == 0 { + amount = 1 + } + if amount < 0 { + return 0, newPicoclawError(CodeInvalidAction, "scroll amount must be > 0") + } + + wheel := 1 + switch strings.ToLower(strings.TrimSpace(action.Direction)) { + case "", "up": + wheel = 1 + case "down": + wheel = -1 + default: + return 0, newPicoclawError(CodeInvalidAction, "invalid scroll direction") + } + + writes := 0 + for range amount { + writes += s.sendMouseMoveWithButton(x, y, 0x00, wheel) + writes += s.sendMouseMoveWithButton(x, y, 0x00, 0) + time.Sleep(defaultScrollStep) + } + return writes, nil + + case "type": + if action.Text == "" { + return 0, newPicoclawError(CodeInvalidAction, "type requires text") + } + charMap := hid.GetCharMap("") + writes := 0 + for _, char := range action.Text { + key, ok := charMap[char] + if !ok { + return 0, newPicoclawError(CodeInvalidAction, "unsupported character in type action") + } + + writes += s.sendKeyboardReport([]byte{byte(key.Modifiers), 0x00, byte(key.Code), 0x00, 0x00, 0x00, 0x00, 0x00}) + writes += s.sendKeyboardReport([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) + time.Sleep(defaultKeyDelay) + } + return writes, nil + + case "hotkey": + report, err := buildHotkeyReport([]string(action.Keys)) + if err != nil { + return 0, err + } + writes := 0 + writes += s.sendKeyboardReport(report) + time.Sleep(defaultClickHold) + writes += s.sendKeyboardReport([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) + return writes, nil + } + + return 0, newPicoclawError(CodeInvalidAction, "unknown or invalid action") +} + +func toAbsoluteHidCoord(normalized float64) uint16 { + if normalized < 0 { + normalized = 0 + } + if normalized > 1 { + normalized = 1 + } + return uint16(math.Floor(0x7FFF*normalized)) + 1 +} + +func (s *Service) sendMousePress(x float64, y float64, button byte) int { + return s.sendMouseMoveWithButton(x, y, button, 0) +} + +func (s *Service) sendMouseRelease(x float64, y float64) int { + return s.sendMouseMoveWithButton(x, y, 0x00, 0) +} + +func (s *Service) sendMouseMoveWithButton(x float64, y float64, buttons byte, wheel int) int { + absoluteX := toAbsoluteHidCoord(x) + absoluteY := toAbsoluteHidCoord(y) + + report := []byte{ + buttons, + byte(absoluteX & 0xff), + byte(absoluteX >> 8), + byte(absoluteY & 0xff), + byte(absoluteY >> 8), + byte(int8(wheel)), + } + s.hid.WriteHid2(report) + return 1 +} + +func (s *Service) sendKeyboardReport(report []byte) int { + s.hid.WriteHid0(report) + return 1 +} + +func normalizedPoint(x *float64, y *float64) (float64, float64, *PicoclawError) { + if x == nil || y == nil { + return 0, 0, newPicoclawError(CodeInvalidAction, "action requires x and y") + } + if *x < 0 || *x > 1 || *y < 0 || *y > 1 { + return 0, 0, newPicoclawError(CodeInvalidAction, "coordinates must be within [0,1]") + } + return *x, *y, nil +} + +func normalizedNestedPoint(point *Point) (float64, float64, *PicoclawError) { + if point == nil { + return 0, 0, newPicoclawError(CodeInvalidAction, "action requires point coordinates") + } + return normalizedPoint(point.X, point.Y) +} + +func mouseButton(button string) (byte, *PicoclawError) { + switch strings.ToLower(strings.TrimSpace(button)) { + case "", "left": + return 1 << 0, nil + case "right": + return 1 << 1, nil + case "middle": + return 1 << 2, nil + case "back": + return 1 << 3, nil + case "forward": + return 1 << 4, nil + default: + return 0, newPicoclawError(CodeInvalidAction, "invalid mouse button") + } +} diff --git a/server/service/picoclaw/agent_profile.go b/server/service/picoclaw/agent_profile.go new file mode 100644 index 0000000..954abfe --- /dev/null +++ b/server/service/picoclaw/agent_profile.go @@ -0,0 +1,147 @@ +package picoclaw + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/gin-gonic/gin" +) + +const ( + agentProfileDefault = "default" + agentProfileKVM = "kvm" + agentProfileFile = "AGENT.md" +) + +type AgentProfileUpdateRequest struct { + Profile string `json:"profile"` +} + +func resolveAgentProfileSourcePath(profile string) (string, error) { + fileName := agentProfileFile + switch profile { + case agentProfileDefault: + fileName = "AGENT.md" + case agentProfileKVM: + fileName = "AGENT_KVM.md" + default: + return "", fmt.Errorf("unsupported agent profile: %s", profile) + } + + candidates := make([]string, 0, 4) + if wd, err := os.Getwd(); err == nil { + candidates = append(candidates, + filepath.Join(wd, "picoclaw", fileName), + filepath.Join(wd, "docs", fileName), + ) + } + candidates = append(candidates, + filepath.Join("/kvmapp", "picoclaw", fileName), + filepath.Join(filepath.Dir(os.Args[0]), "..", "picoclaw", fileName), + ) + + for _, candidate := range candidates { + if _, err := os.Stat(candidate); err == nil { + return candidate, nil + } + } + + return "", fmt.Errorf("agent profile source not found: %s", fileName) +} + +func detectPicoclawAgentProfile() string { + workspacePath, err := resolvePicoclawWorkspacePath() + if err != nil { + return agentProfileKVM + } + + targetPath := filepath.Join(workspacePath, agentProfileFile) + targetContent, err := os.ReadFile(targetPath) + if err != nil { + return agentProfileKVM + } + + switch parseAgentProfileName(string(targetContent)) { + case "pico": + return agentProfileDefault + case "pico-kvm": + return agentProfileKVM + default: + return agentProfileKVM + } +} + +func parseAgentProfileName(content string) string { + scanner := bufio.NewScanner(strings.NewReader(content)) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if !strings.HasPrefix(line, "name:") { + continue + } + return strings.TrimSpace(strings.TrimPrefix(line, "name:")) + } + + return "" +} + +func applyPicoclawAgentProfile(profile string) error { + sourcePath, err := resolveAgentProfileSourcePath(profile) + if err != nil { + return err + } + + workspacePath, err := resolvePicoclawWorkspacePath() + if err != nil { + return err + } + if err := os.MkdirAll(workspacePath, 0o755); err != nil { + return fmt.Errorf("failed to create picoclaw workspace: %w", err) + } + + sourceContent, err := os.ReadFile(sourcePath) + if err != nil { + return fmt.Errorf("failed to read agent profile source: %w", err) + } + + targetPath := filepath.Join(workspacePath, agentProfileFile) + if err := os.WriteFile(targetPath, sourceContent, 0o644); err != nil { + return fmt.Errorf("failed to write picoclaw AGENT.md: %w", err) + } + + return nil +} + +func withAgentProfile(status RuntimeStatus) RuntimeStatus { + status.AgentProfile = detectPicoclawAgentProfile() + return status +} + +func (s *Service) UpdateAgentProfile(c *gin.Context) { + var req AgentProfileUpdateRequest + if err := c.ShouldBindJSON(&req); err != nil { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "invalid agent profile payload")) + return + } + + profile := strings.TrimSpace(req.Profile) + if profile != agentProfileDefault && profile != agentProfileKVM { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "invalid agent profile")) + return + } + + if err := applyPicoclawAgentProfile(profile); err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, err.Error())) + return + } + + _ = s.syncConfigFromPicoclaw() + _ = s.ensureRuntimeReady() + + writeSuccess(c, gin.H{ + "profile": profile, + "status": withAgentProfile(s.runtime.Get()), + }) +} diff --git a/server/service/picoclaw/config.go b/server/service/picoclaw/config.go new file mode 100644 index 0000000..48ed8d7 --- /dev/null +++ b/server/service/picoclaw/config.go @@ -0,0 +1,179 @@ +package picoclaw + +import ( + "fmt" + "os" + "strings" + "time" +) + +const ( + defaultPicoclawGatewayHost = "127.0.0.1" + defaultPicoclawGatewayPort = 18790 + picoclawGatewayPath = "/pico/ws" +) + +func defaultConfig() Config { + cfg := Config{ + GatewayURL: "ws://127.0.0.1:18790/pico/ws", + ConnectTimeoutMs: 10000, + ReadTimeoutMs: 60000, + WriteTimeoutMs: 10000, + PingIntervalMs: 30000, + MaxMessageBytes: 1024 * 1024, + AllowTokenQuery: false, + } + + settings, err := loadPicoclawGatewaySettings() + if err != nil { + return cfg + } + + cfg.GatewayURL = settings.GatewayURL + cfg.Token = settings.Token + cfg.AllowTokenQuery = settings.AllowTokenQuery + if settings.PingIntervalMs > 0 { + cfg.PingIntervalMs = settings.PingIntervalMs + } + if settings.ReadTimeoutMs > 0 { + cfg.ReadTimeoutMs = settings.ReadTimeoutMs + } + + return cfg +} + +func (s *Service) syncConfigFromPicoclaw() *PicoclawError { + installed, installedKnown := picoclawInstalledState() + + if patchErr := preparePicoclawConfigForRead(); patchErr != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Status = "config_error" + status.ConfigError = patchErr.Error() + status.LastError = patchErr.Error() + if installedKnown { + status.Installed = installed + } + status.CheckedAt = time.Now() + }) + return newPicoclawError(CodeRuntimeUnavailable, patchErr.Error()) + } + + settings, err := loadPicoclawGatewaySettings() + if err != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Status = "config_error" + status.ConfigError = err.Error() + status.LastError = err.Error() + if installedKnown { + status.Installed = installed + } + status.CheckedAt = time.Now() + }) + return newPicoclawError(CodeRuntimeUnavailable, err.Error()) + } + + cfg := s.config.Get() + cfg.GatewayURL = settings.GatewayURL + cfg.Token = settings.Token + cfg.AllowTokenQuery = settings.AllowTokenQuery + if settings.PingIntervalMs > 0 { + cfg.PingIntervalMs = settings.PingIntervalMs + } + if settings.ReadTimeoutMs > 0 { + cfg.ReadTimeoutMs = settings.ReadTimeoutMs + } + s.config.Set(cfg) + + s.runtime.Update(func(status *RuntimeStatus) { + status.Installed = true + status.ModelConfigured = settings.ModelConfigured + if settings.ModelConfigured { + status.ModelName = settings.ModelName + } else { + status.ModelName = settings.TargetModelName + } + status.ConfigError = "" + if status.Status == "config_error" && status.Ready { + status.Status = "ready" + } + status.CheckedAt = time.Now() + }) + + return nil +} + +type picoclawGatewaySettings struct { + GatewayURL string + Token string + AllowTokenQuery bool + PingIntervalMs int + ReadTimeoutMs int + ModelConfigured bool + ModelName string + TargetModelName string +} + +func loadPicoclawGatewaySettings() (picoclawGatewaySettings, error) { + doc, err := loadPicoclawConfigDocument() + if err != nil { + return picoclawGatewaySettings{}, err + } + if err := ensurePicoclawPicoChannelEnabled(doc); err != nil { + return picoclawGatewaySettings{}, err + } + + cfg := doc.config + + host := cfg.Gateway.Host + if host == "" || host == "0.0.0.0" { + host = defaultPicoclawGatewayHost + } + + port := cfg.Gateway.Port + if port <= 0 { + port = defaultPicoclawGatewayPort + } + + settings := picoclawGatewaySettings{ + GatewayURL: fmt.Sprintf("ws://%s:%d%s", host, port, picoclawGatewayPath), + Token: doc.resolvedGatewayToken(), + AllowTokenQuery: cfg.Channels.Pico.AllowTokenQuery, + } + settings.TargetModelName = resolvePicoclawTargetModelName(cfg) + + if isPicoclawModelConfigured(cfg, doc.security, settings.TargetModelName) { + settings.ModelConfigured = true + settings.ModelName = settings.TargetModelName + } + + if cfg.Channels.Pico.PingInterval > 0 { + settings.PingIntervalMs = cfg.Channels.Pico.PingInterval * 1000 + } + if cfg.Channels.Pico.ReadTimeout > 0 { + settings.ReadTimeoutMs = cfg.Channels.Pico.ReadTimeout * 1000 + } + + return settings, nil +} + +func resolvePicoclawTargetModelName(cfg picoclawConfigFile) string { + return strings.TrimSpace(cfg.Agents.Defaults.ModelName) +} + +func preparePicoclawConfigForRead() error { + configPath, err := resolvePicoclawConfigPath() + if err != nil { + return err + } + + if _, err := os.Stat(configPath); err != nil { + if os.IsNotExist(err) { + return nil + } + return fmt.Errorf("failed to stat picoclaw config: %w", err) + } + + return nil +} diff --git a/server/service/picoclaw/config_files.go b/server/service/picoclaw/config_files.go new file mode 100644 index 0000000..f7aa114 --- /dev/null +++ b/server/service/picoclaw/config_files.go @@ -0,0 +1,256 @@ +package picoclaw + +import ( + "bytes" + "encoding/json" + "fmt" + "os" + "os/user" + "path/filepath" + "strings" + + "gopkg.in/yaml.v3" +) + +const ( + picoclawPIDFileName = ".picoclaw.pid" + picoclawGatewayTokenPrefix = "pico-" +) + +type picoclawConfigFile struct { + Agents struct { + Defaults struct { + ModelName string `json:"model_name"` + Model string `json:"model,omitempty"` + Workspace string `json:"workspace,omitempty"` + } `json:"defaults"` + } `json:"agents"` + Gateway struct { + Host string `json:"host"` + Port int `json:"port"` + } `json:"gateway"` + ModelList []struct { + ModelName string `json:"model_name"` + Model string `json:"model"` + APIBase string `json:"api_base"` + APIKey string `json:"api_key"` + APIKeys []string `json:"api_keys,omitempty"` + } `json:"model_list"` + Channels struct { + Pico struct { + Enabled bool `json:"enabled"` + Token string `json:"token"` + AllowTokenQuery bool `json:"allow_token_query"` + PingInterval int `json:"ping_interval"` + ReadTimeout int `json:"read_timeout"` + } `json:"pico"` + } `json:"channels"` +} + +type picoclawConfigDocument struct { + configPath string + securityPath string + config picoclawConfigFile + raw map[string]any + security picoclawSecurityConfig +} + +func loadPicoclawConfigDocument() (*picoclawConfigDocument, error) { + configPath, err := resolvePicoclawConfigPath() + if err != nil { + return nil, err + } + + data, err := os.ReadFile(configPath) + if err != nil { + return nil, fmt.Errorf("failed to read picoclaw config: %w", err) + } + + var cfg picoclawConfigFile + if err := json.Unmarshal(data, &cfg); err != nil { + return nil, fmt.Errorf("failed to parse picoclaw config: %w", err) + } + + var raw map[string]any + if err := json.Unmarshal(data, &raw); err != nil { + return nil, fmt.Errorf("failed to parse picoclaw config for update: %w", err) + } + + securityPath := resolvePicoclawSecurityPath(configPath) + security, err := loadPicoclawSecurityConfig(securityPath) + if err != nil { + return nil, err + } + + return &picoclawConfigDocument{ + configPath: configPath, + securityPath: securityPath, + config: cfg, + raw: raw, + security: security, + }, nil +} + +func resolvePicoclawConfigPath() (string, error) { + home := os.Getenv("PICOCLAW_HOME") + if home == "" { + currentUser, err := user.Current() + if err != nil { + return "", fmt.Errorf("failed to resolve PICOCLAW_HOME: %w", err) + } + home = filepath.Join(currentUser.HomeDir, ".picoclaw") + } + + return filepath.Join(home, "config.json"), nil +} + +func expandPicoclawPath(path string) string { + path = strings.TrimSpace(path) + if path == "" { + return "" + } + + if strings.HasPrefix(path, "~/") || path == "~" { + if home, err := os.UserHomeDir(); err == nil { + if path == "~" { + return home + } + return filepath.Join(home, strings.TrimPrefix(path, "~/")) + } + } + + return path +} + +func resolvePicoclawSecurityPath(configPath string) string { + return filepath.Join(filepath.Dir(configPath), ".security.yml") +} + +func (d *picoclawConfigDocument) saveConfig() error { + updated, err := json.MarshalIndent(d.raw, "", " ") + if err != nil { + return fmt.Errorf("failed to encode updated picoclaw config: %w", err) + } + if err := os.WriteFile(d.configPath, updated, 0o600); err != nil { + return fmt.Errorf("failed to write picoclaw config: %w", err) + } + return nil +} + +func (d *picoclawConfigDocument) saveSecurity() error { + var buf bytes.Buffer + encoder := yaml.NewEncoder(&buf) + encoder.SetIndent(2) + if err := encoder.Encode(d.security); err != nil { + return fmt.Errorf("failed to encode picoclaw security config: %w", err) + } + if err := encoder.Close(); err != nil { + return fmt.Errorf("failed to finalize picoclaw security config: %w", err) + } + if err := os.WriteFile(d.securityPath, buf.Bytes(), 0o600); err != nil { + return fmt.Errorf("failed to write picoclaw security config: %w", err) + } + return nil +} + +type picoclawSecurityConfig struct { + ModelList map[string]picoclawModelSecurityEntry `yaml:"model_list,omitempty"` + Channels picoclawSecurityChannels `yaml:"channels,omitempty"` +} + +type picoclawModelSecurityEntry struct { + APIKeys []string `yaml:"api_keys,omitempty"` +} + +type picoclawSecurityChannels struct { + Pico *picoclawPicoSecurity `yaml:"pico,omitempty"` +} + +type picoclawPicoSecurity struct { + Token string `yaml:"token,omitempty"` +} + +type picoclawPIDFile struct { + Token string `json:"token"` +} + +func (d *picoclawConfigDocument) resolvedPicoToken() string { + if d == nil { + return "" + } + if d.security.Channels.Pico != nil { + if token := d.security.Channels.Pico.Token; token != "" { + return token + } + } + return d.config.Channels.Pico.Token +} + +func (d *picoclawConfigDocument) resolvedGatewayToken() string { + baseToken := strings.TrimSpace(d.resolvedPicoToken()) + if baseToken == "" { + return "" + } + + runtimeToken, err := d.resolvedPIDToken() + if err != nil { + return baseToken + } + + return composePicoclawGatewayToken(baseToken, runtimeToken) +} + +func (d *picoclawConfigDocument) resolvedPIDToken() (string, error) { + if d == nil || d.configPath == "" { + return "", nil + } + + pidPath := filepath.Join(filepath.Dir(d.configPath), picoclawPIDFileName) + data, err := os.ReadFile(pidPath) + if err != nil { + if os.IsNotExist(err) { + return "", nil + } + return "", fmt.Errorf("failed to read picoclaw pid file: %w", err) + } + + var pidFile picoclawPIDFile + if err := json.Unmarshal(data, &pidFile); err != nil { + return "", fmt.Errorf("failed to parse picoclaw pid file: %w", err) + } + + return strings.TrimSpace(pidFile.Token), nil +} + +func composePicoclawGatewayToken(baseToken, runtimeToken string) string { + baseToken = strings.TrimSpace(baseToken) + runtimeToken = strings.TrimSpace(runtimeToken) + + switch { + case baseToken == "": + return "" + case strings.HasPrefix(baseToken, picoclawGatewayTokenPrefix): + return baseToken + case runtimeToken == "": + return baseToken + default: + return picoclawGatewayTokenPrefix + runtimeToken + baseToken + } +} + +func loadPicoclawSecurityConfig(securityPath string) (picoclawSecurityConfig, error) { + data, err := os.ReadFile(securityPath) + if err != nil { + if os.IsNotExist(err) { + return picoclawSecurityConfig{}, nil + } + return picoclawSecurityConfig{}, fmt.Errorf("failed to read picoclaw security config: %w", err) + } + + var sec picoclawSecurityConfig + if err := yaml.Unmarshal(data, &sec); err != nil { + return picoclawSecurityConfig{}, fmt.Errorf("failed to parse picoclaw security config: %w", err) + } + + return sec, nil +} diff --git a/server/service/picoclaw/errors.go b/server/service/picoclaw/errors.go new file mode 100644 index 0000000..7f0c1de --- /dev/null +++ b/server/service/picoclaw/errors.go @@ -0,0 +1,71 @@ +package picoclaw + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +const ( + CodePicoclawLockHeld = "AI_LOCK_HELD" + CodeScreenshotFailed = "SCREENSHOT_FAILED" + CodeScreenshotNoSignal = "SCREENSHOT_NO_SIGNAL" + CodeHIDWriteFailed = "HID_WRITE_FAILED" + CodeInvalidAction = "INVALID_ACTION" + CodeRuntimeUnavailable = "RUNTIME_UNAVAILABLE" + CodeRuntimeStartFailed = "RUNTIME_START_FAILED" + CodeSessionIDMissing = "SESSION_ID_MISSING" + CodeSessionIDInvalid = "SESSION_ID_INVALID" +) + +type PicoclawError struct { + StatusCode int + Code string + Message string + SessionID string + Index *int +} + +func (e *PicoclawError) Error() string { + return e.Code + ": " + e.Message +} + +func newPicoclawError(code string, message string) *PicoclawError { + return &PicoclawError{ + StatusCode: http.StatusOK, + Code: code, + Message: message, + } +} + +func writeSuccess(c *gin.Context, data interface{}) { + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "msg": "success", + "data": data, + }) +} + +func writePicoclawError(c *gin.Context, err *PicoclawError) { + if err == nil { + return + } + + payload := gin.H{ + "code": err.Code, + "message": err.Message, + } + if err.SessionID != "" { + payload["session_id"] = err.SessionID + } + if err.Index != nil { + payload["index"] = *err.Index + } + + statusCode := err.StatusCode + if statusCode == 0 { + statusCode = http.StatusOK + } + + c.JSON(statusCode, payload) +} diff --git a/server/service/picoclaw/gateway_client.go b/server/service/picoclaw/gateway_client.go new file mode 100644 index 0000000..9e3157f --- /dev/null +++ b/server/service/picoclaw/gateway_client.go @@ -0,0 +1,107 @@ +package picoclaw + +import ( + "fmt" + "net" + "net/http" + "net/url" + "time" + + "github.com/gorilla/websocket" +) + +func (s *Service) connectGateway(sessionID string) (*websocket.Conn, *PicoclawError) { + cfg := s.config.Get() + + gatewayURL, err := buildGatewayURL(cfg, sessionID) + if err != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Status = "config_error" + status.ConfigError = err.Error() + status.LastError = err.Error() + status.CheckedAt = time.Now() + status.CurrentSession = s.lock.Owner() + }) + return nil, newPicoclawError(CodeRuntimeUnavailable, err.Error()) + } + header := http.Header{} + if cfg.Token != "" { + header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.Token)) + } + + dialer := websocket.Dialer{ + HandshakeTimeout: time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond, + NetDialContext: (&net.Dialer{ + Timeout: time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond, + }).DialContext, + } + + upstream, response, err := dialer.Dial(gatewayURL, header) + if err != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Status = "unavailable" + status.ConfigError = "" + status.LastError = err.Error() + status.CheckedAt = time.Now() + status.CurrentSession = s.lock.Owner() + if response != nil && response.StatusCode == http.StatusUnauthorized { + status.Status = "config_error" + status.ConfigError = "gateway authentication failed" + } + }) + return nil, newPicoclawError(CodeRuntimeUnavailable, "gateway is unavailable") + } + + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = true + status.Status = "ready" + status.ConfigError = "" + status.LastError = "" + status.CheckedAt = time.Now() + status.CurrentSession = s.lock.Owner() + }) + + return upstream, nil +} + +func buildGatewayURL(cfg Config, sessionID string) (string, error) { + parsed, err := url.Parse(cfg.GatewayURL) + if err != nil { + return "", fmt.Errorf("invalid gateway url: %w", err) + } + if parsed.Scheme != "ws" && parsed.Scheme != "wss" { + return "", fmt.Errorf("invalid gateway url scheme: %s", parsed.Scheme) + } + if parsed.Host == "" { + return "", fmt.Errorf("invalid gateway url host") + } + + query := parsed.Query() + query.Set("session_id", sessionID) + if cfg.Token != "" && cfg.AllowTokenQuery { + query.Set("token", cfg.Token) + } + parsed.RawQuery = query.Encode() + + return parsed.String(), nil +} + +func gatewayHostPort(parsed *url.URL) (string, error) { + if parsed == nil || parsed.Host == "" { + return "", fmt.Errorf("invalid gateway url host") + } + if _, _, err := net.SplitHostPort(parsed.Host); err == nil { + return parsed.Host, nil + } + + switch parsed.Scheme { + case "ws": + return net.JoinHostPort(parsed.Host, "80"), nil + case "wss": + return net.JoinHostPort(parsed.Host, "443"), nil + default: + return "", fmt.Errorf("invalid gateway url scheme: %s", parsed.Scheme) + } +} diff --git a/server/service/picoclaw/gateway_proxy.go b/server/service/picoclaw/gateway_proxy.go new file mode 100644 index 0000000..e1e69af --- /dev/null +++ b/server/service/picoclaw/gateway_proxy.go @@ -0,0 +1,262 @@ +package picoclaw + +import ( + "fmt" + "net/http" + "strings" + "sync" + "time" + + "NanoKVM-Server/service/stream/mjpeg" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" +) + +var gatewayUpgrader = websocket.Upgrader{ + ReadBufferSize: 4096, + WriteBufferSize: 4096, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +type relayResult struct { + Source string + CloseCode int + Reason string +} + +func (s *Service) ConnectGateway(c *gin.Context) { + sessionID := strings.TrimSpace(c.Query("session_id")) + if sessionID == "" { + sessionID = uuid.NewString() + } + + if lockErr := s.lock.Ensure(sessionID); lockErr != nil { + c.AbortWithStatusJSON(http.StatusConflict, gin.H{ + "code": CodePicoclawLockHeld, + "message": lockErr.Message, + }) + return + } + + session, sessionErr := GetSessionManager().Register(sessionID, nil) + if sessionErr != nil { + s.lock.Release(sessionID) + c.AbortWithStatusJSON(http.StatusConflict, gin.H{ + "code": CodePicoclawLockHeld, + "message": sessionErr.Message, + }) + return + } + GetSessionManager().SetState(sessionID, SessionStateConnecting) + + if runtimeErr := s.syncConfigFromPicoclaw(); runtimeErr != nil { + s.closeGatewaySession(session, CloseCodeRuntimeUnavailable, runtimeErr.Message) + return + } + + upstream, gatewayErr := s.connectGateway(sessionID) + if gatewayErr != nil { + s.closeGatewaySession(session, CloseCodeRuntimeUnavailable, gatewayErr.Message) + return + } + + downstream, err := gatewayUpgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + log.Errorf("failed to upgrade gateway websocket: %s", err) + _ = upstream.Close() + ReleaseSession(sessionID) + GetSessionManager().SetState(sessionID, SessionStateClosed) + GetSessionManager().Remove(sessionID) + return + } + + GetSessionManager().AttachUpstream(sessionID, upstream) + GetSessionManager().AttachDownstream(sessionID, downstream) + GetSessionManager().SetState(sessionID, SessionStateActive) + mjpeg.EnableLatestFrameCache() + + var wg sync.WaitGroup + results := make(chan relayResult, 2) + cfg := s.config.Get() + + s.configureRelayConn(downstream, cfg) + s.configureRelayConn(upstream, cfg) + + wg.Add(4) + go s.runPingLoop("downstream", downstream, cfg, &wg) + go s.runPingLoop("upstream", upstream, cfg, &wg) + go s.proxyMessages("downstream", session, downstream, cfg, &wg, results) + go s.proxyMessages("upstream", session, upstream, cfg, &wg, results) + + result := <-results + closeCode := result.CloseCode + if closeCode == 0 { + closeCode = websocket.CloseNormalClosure + } + + if result.Source == "upstream" { + if closeCode == websocket.CloseNormalClosure { + closeCode = CloseCodeUpstreamClosed + } + s.closeGatewaySession(session, closeCode, result.Reason) + } else { + s.closeGatewaySession(session, closeCode, result.Reason) + } + + wg.Wait() +} + +func (s *Service) proxyMessages(source string, session *GatewaySession, src *websocket.Conn, cfg Config, wg *sync.WaitGroup, results chan<- relayResult) { + defer wg.Done() + + for { + messageType, data, err := src.ReadMessage() + if err != nil { + results <- relayResult{ + Source: source, + CloseCode: closeCodeFromError(source, err), + Reason: closeReasonFromError(err), + } + return + } + + if source == "upstream" && messageType == websocket.BinaryMessage { + results <- relayResult{ + Source: source, + CloseCode: CloseCodeUpstreamClosed, + Reason: "upstream sent unsupported binary message", + } + return + } + + var writeErr error + switch source { + case "downstream": + writeErr = session.writeUpstreamMessage(cfg, messageType, data) + default: + writeErr = session.writeDownstreamMessage(cfg, messageType, data) + } + if writeErr != nil { + results <- relayResult{ + Source: source, + CloseCode: closeCodeFromError(source, writeErr), + Reason: closeReasonFromError(writeErr), + } + return + } + } +} + +func (s *Service) runPingLoop(name string, conn *websocket.Conn, cfg Config, wg *sync.WaitGroup) { + defer wg.Done() + + ticker := time.NewTicker(time.Duration(cfg.PingIntervalMs) * time.Millisecond) + defer ticker.Stop() + + for range ticker.C { + if err := conn.WriteControl( + websocket.PingMessage, + []byte(name), + time.Now().Add(time.Duration(cfg.WriteTimeoutMs)*time.Millisecond), + ); err != nil { + return + } + } +} + +func (s *Service) configureRelayConn(conn *websocket.Conn, cfg Config) { + readTimeout := time.Duration(cfg.ReadTimeoutMs) * time.Millisecond + conn.SetReadLimit(int64(cfg.MaxMessageBytes)) + _ = conn.SetReadDeadline(time.Now().Add(readTimeout)) + conn.SetPongHandler(func(string) error { + return conn.SetReadDeadline(time.Now().Add(readTimeout)) + }) +} + +func (s *Service) closeGatewaySession(session *GatewaySession, closeCode int, reason string) { + if session == nil { + return + } + + session.closeOnce.Do(func() { + hadDownstream := session.Downstream != nil + + mjpeg.DisableLatestFrameCache() + GetSessionManager().SetState(session.SessionID, SessionStateClosing) + + if session.Upstream != nil { + writeGatewayClose(session.Upstream, websocket.CloseNormalClosure, "relay closing") + _ = session.Upstream.Close() + } + if session.Downstream != nil { + writeGatewayClose(session.Downstream, closeCode, reason) + _ = session.Downstream.Close() + } + + if hadDownstream { + cleanupPicoclawMediaTempDir() + } + + ReleaseSession(session.SessionID) + GetSessionManager().SetState(session.SessionID, SessionStateClosed) + GetSessionManager().Remove(session.SessionID) + + status := s.runtime.Get() + status.CurrentSession = s.lock.Owner() + if closeCode == CloseCodeUpstreamClosed || closeCode == CloseCodeRuntimeUnavailable { + status.Ready = false + if closeCode == CloseCodeUpstreamClosed { + status.Status = "unavailable" + } + status.LastError = reason + status.CheckedAt = time.Now() + s.runtime.Set(status) + } + }) +} + +func writeGatewayClose(conn *websocket.Conn, code int, reason string) { + if conn == nil { + return + } + + message := websocket.FormatCloseMessage(code, reason) + _ = conn.WriteControl(websocket.CloseMessage, message, time.Now().Add(2*time.Second)) +} + +func closeCodeFromError(source string, err error) int { + if closeErr, ok := err.(*websocket.CloseError); ok { + if closeErr.Code == websocket.CloseNormalClosure || closeErr.Code == websocket.CloseGoingAway { + return websocket.CloseNormalClosure + } + if closeErr.Code == websocket.CloseAbnormalClosure || closeErr.Code == websocket.CloseNoStatusReceived { + if source == "upstream" { + return CloseCodeUpstreamClosed + } + return websocket.CloseNormalClosure + } + return closeErr.Code + } + + if source == "upstream" { + return CloseCodeUpstreamClosed + } + return websocket.CloseNormalClosure +} + +func closeReasonFromError(err error) string { + if err == nil { + return "closed" + } + if closeErr, ok := err.(*websocket.CloseError); ok { + if closeErr.Text != "" { + return closeErr.Text + } + } + return fmt.Sprintf("relay closed: %v", err) +} diff --git a/server/service/picoclaw/gateway_write.go b/server/service/picoclaw/gateway_write.go new file mode 100644 index 0000000..f4f42a7 --- /dev/null +++ b/server/service/picoclaw/gateway_write.go @@ -0,0 +1,86 @@ +package picoclaw + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/google/uuid" + "github.com/gorilla/websocket" +) + +type picoGatewayMessage struct { + Type string `json:"type"` + ID string `json:"id,omitempty"` + SessionID string `json:"session_id,omitempty"` + Timestamp int64 `json:"timestamp,omitempty"` + Payload map[string]any `json:"payload,omitempty"` +} + +func newPicoGatewayMessage(sessionID string, payload map[string]any) picoGatewayMessage { + return picoGatewayMessage{ + Type: "message.send", + ID: uuid.NewString(), + SessionID: sessionID, + Timestamp: time.Now().UnixMilli(), + Payload: payload, + } +} + +func newPicoGatewayObservationMessage(sessionID string, payload map[string]any) picoGatewayMessage { + return picoGatewayMessage{ + Type: "observation", + ID: uuid.NewString(), + SessionID: sessionID, + Timestamp: time.Now().UnixMilli(), + Payload: payload, + } +} + +func (session *GatewaySession) writeUpstreamJSON(cfg Config, value any) error { + raw, err := json.Marshal(value) + if err != nil { + return err + } + return session.writeUpstreamMessage(cfg, websocket.TextMessage, raw) +} + +func (session *GatewaySession) writeDownstreamJSON(cfg Config, value any) error { + raw, err := json.Marshal(value) + if err != nil { + return err + } + return session.writeDownstreamMessage(cfg, websocket.TextMessage, raw) +} + +func (session *GatewaySession) writeUpstreamMessage(cfg Config, messageType int, data []byte) error { + if session == nil || session.Upstream == nil { + return fmt.Errorf("upstream connection is unavailable") + } + + session.upstreamWriteMu.Lock() + defer session.upstreamWriteMu.Unlock() + + _ = session.Upstream.SetWriteDeadline(gatewayWriteDeadline(cfg)) + return session.Upstream.WriteMessage(messageType, data) +} + +func (session *GatewaySession) writeDownstreamMessage(cfg Config, messageType int, data []byte) error { + if session == nil || session.Downstream == nil { + return fmt.Errorf("downstream connection is unavailable") + } + + session.downstreamWriteMu.Lock() + defer session.downstreamWriteMu.Unlock() + + _ = session.Downstream.SetWriteDeadline(gatewayWriteDeadline(cfg)) + return session.Downstream.WriteMessage(messageType, data) +} + +func gatewayWriteDeadline(cfg Config) time.Time { + timeout := time.Duration(cfg.WriteTimeoutMs) * time.Millisecond + if timeout <= 0 { + timeout = 10 * time.Second + } + return time.Now().Add(timeout) +} diff --git a/server/service/picoclaw/keyboard_map.go b/server/service/picoclaw/keyboard_map.go new file mode 100644 index 0000000..e0468ae --- /dev/null +++ b/server/service/picoclaw/keyboard_map.go @@ -0,0 +1,271 @@ +package picoclaw + +import ( + "fmt" + "strings" +) + +const ( + modifierLeftCtrl = 1 << 0 + modifierLeftShift = 1 << 1 + modifierLeftAlt = 1 << 2 + modifierLeftMeta = 1 << 3 + modifierRightCtrl = 1 << 4 + modifierRightShift = 1 << 5 + modifierRightAlt = 1 << 6 + modifierRightMeta = 1 << 7 +) + +var modifierMap = map[string]byte{ + "ControlLeft": modifierLeftCtrl, + "ShiftLeft": modifierLeftShift, + "AltLeft": modifierLeftAlt, + "MetaLeft": modifierLeftMeta, + "ControlRight": modifierRightCtrl, + "ShiftRight": modifierRightShift, + "AltRight": modifierRightAlt, + "MetaRight": modifierRightMeta, +} + +var keyAliases = map[string]string{ + "CTRL": "ControlLeft", + "CONTROL": "ControlLeft", + "SHIFT": "ShiftLeft", + "ALT": "AltLeft", + "OPTION": "AltLeft", + "META": "MetaLeft", + "WIN": "MetaLeft", + "WINDOWS": "MetaLeft", + "COMMAND": "MetaLeft", + "CMD": "MetaLeft", + "SUPER": "MetaLeft", + "ESC": "Escape", + "RETURN": "Enter", + "DEL": "Delete", + "INS": "Insert", + "UP": "ArrowUp", + "DOWN": "ArrowDown", + "LEFT": "ArrowLeft", + "RIGHT": "ArrowRight", + "SPACEBAR": "Space", + "SPACE": "Space", + "TAB": "Tab", + "ENTER": "Enter", + "BACKSPACE": "Backspace", + "PGUP": "PageUp", + "PGDN": "PageDown", +} + +var keycodeMap = map[string]byte{ + "KeyA": 0x04, "KeyB": 0x05, "KeyC": 0x06, "KeyD": 0x07, "KeyE": 0x08, + "KeyF": 0x09, "KeyG": 0x0a, "KeyH": 0x0b, "KeyI": 0x0c, "KeyJ": 0x0d, + "KeyK": 0x0e, "KeyL": 0x0f, "KeyM": 0x10, "KeyN": 0x11, "KeyO": 0x12, + "KeyP": 0x13, "KeyQ": 0x14, "KeyR": 0x15, "KeyS": 0x16, "KeyT": 0x17, + "KeyU": 0x18, "KeyV": 0x19, "KeyW": 0x1a, "KeyX": 0x1b, "KeyY": 0x1c, + "KeyZ": 0x1d, + + "Digit1": 0x1e, "Digit2": 0x1f, "Digit3": 0x20, "Digit4": 0x21, "Digit5": 0x22, + "Digit6": 0x23, "Digit7": 0x24, "Digit8": 0x25, "Digit9": 0x26, "Digit0": 0x27, + + "Enter": 0x28, + "Escape": 0x29, + "Backspace": 0x2a, + "Tab": 0x2b, + "Space": 0x2c, + "Minus": 0x2d, + "Equal": 0x2e, + "BracketLeft": 0x2f, + "BracketRight": 0x30, + "Backslash": 0x31, + "Semicolon": 0x33, + "Quote": 0x34, + "Backquote": 0x35, + "Comma": 0x36, + "Period": 0x37, + "Slash": 0x38, + "CapsLock": 0x39, + + "F1": 0x3a, "F2": 0x3b, "F3": 0x3c, "F4": 0x3d, "F5": 0x3e, "F6": 0x3f, + "F7": 0x40, "F8": 0x41, "F9": 0x42, "F10": 0x43, "F11": 0x44, "F12": 0x45, + + "PrintScreen": 0x46, + "ScrollLock": 0x47, + "Pause": 0x48, + "Insert": 0x49, + "Home": 0x4a, + "PageUp": 0x4b, + "Delete": 0x4c, + "End": 0x4d, + "PageDown": 0x4e, + "ArrowRight": 0x4f, + "ArrowLeft": 0x50, + "ArrowDown": 0x51, + "ArrowUp": 0x52, + + "NumLock": 0x53, + "NumpadDivide": 0x54, + "NumpadMultiply": 0x55, + "NumpadSubtract": 0x56, + "NumpadAdd": 0x57, + "NumpadEnter": 0x58, + "Numpad1": 0x59, + "Numpad2": 0x5a, + "Numpad3": 0x5b, + "Numpad4": 0x5c, + "Numpad5": 0x5d, + "Numpad6": 0x5e, + "Numpad7": 0x5f, + "Numpad8": 0x60, + "Numpad9": 0x61, + "Numpad0": 0x62, + "NumpadDecimal": 0x63, + + "IntlBackslash": 0x64, + "ContextMenu": 0x65, + "Power": 0x66, + "NumpadEqual": 0x67, + "F13": 0x68, "F14": 0x69, "F15": 0x6a, "F16": 0x6b, "F17": 0x6c, "F18": 0x6d, + "F19": 0x6e, "F20": 0x6f, "F21": 0x70, "F22": 0x71, "F23": 0x72, "F24": 0x73, + + "Execute": 0x74, + "Help": 0x75, + "Props": 0x76, + "Select": 0x77, + "Stop": 0x78, + "Again": 0x79, + "Undo": 0x7a, + "Cut": 0x7b, + "Copy": 0x7c, + "Paste": 0x7d, + "Find": 0x7e, + + "AudioVolumeMute": 0x7f, + "AudioVolumeUp": 0x80, + "AudioVolumeDown": 0x81, + "VolumeMute": 0x7f, + "VolumeUp": 0x80, + "VolumeDown": 0x81, + + "LockingCapsLock": 0x82, + "LockingNumLock": 0x83, + "LockingScrollLock": 0x84, + "NumpadComma": 0x85, + "NumpadEqual2": 0x86, + "IntlRo": 0x87, + "KanaMode": 0x88, + "IntlYen": 0x89, + "Convert": 0x8a, + "NonConvert": 0x8b, + "International6": 0x8c, + "International7": 0x8d, + "International8": 0x8e, + "International9": 0x8f, + "Lang1": 0x90, "Lang2": 0x91, "Lang3": 0x92, "Lang4": 0x93, "Lang5": 0x94, + "Lang6": 0x95, "Lang7": 0x96, "Lang8": 0x97, "Lang9": 0x98, + "IntlHash": 0x32, + + "NumpadParenLeft": 0xb6, + "NumpadParenRight": 0xb7, + "NumpadBackspace": 0xbb, + "NumpadMemoryStore": 0xd0, + "NumpadMemoryRecall": 0xd1, + "NumpadMemoryClear": 0xd2, + "NumpadMemoryAdd": 0xd3, + "NumpadMemorySubtract": 0xd4, + "NumpadClear": 0xd8, + "NumpadClearEntry": 0xd9, + + "BrowserSearch": 0xf0, + "BrowserHome": 0xf1, + "BrowserBack": 0xf2, + "BrowserForward": 0xf3, + "BrowserStop": 0xf4, + "BrowserRefresh": 0xf5, + "BrowserFavorites": 0xf6, + + "MediaPlayPause": 0xe8, + "MediaStop": 0xe9, + "MediaTrackPrevious": 0xea, + "MediaTrackNext": 0xeb, + "Eject": 0xec, + "MediaSelect": 0xed, + "LaunchMail": 0xee, + "LaunchApp1": 0xef, + "LaunchApp2": 0xf0, + "Sleep": 0xf8, + "Wake": 0xf9, + "MediaRewind": 0xfa, + "MediaFastForward": 0xfb, +} + +func resolveKey(key string) (string, byte, bool, error) { + trimmed := strings.TrimSpace(key) + if trimmed == "" { + return "", 0, false, fmt.Errorf("empty key") + } + + if modifier, ok := modifierMap[trimmed]; ok { + return trimmed, modifier, true, nil + } + if _, ok := keycodeMap[trimmed]; ok { + return trimmed, keycodeMap[trimmed], false, nil + } + + upper := strings.ToUpper(trimmed) + if alias, ok := keyAliases[upper]; ok { + if modifier, ok := modifierMap[alias]; ok { + return alias, modifier, true, nil + } + return alias, keycodeMap[alias], false, nil + } + + if len(upper) == 1 && upper[0] >= 'A' && upper[0] <= 'Z' { + code := "Key" + upper + return code, keycodeMap[code], false, nil + } + if len(upper) == 1 && upper[0] >= '0' && upper[0] <= '9' { + code := "Digit" + upper + return code, keycodeMap[code], false, nil + } + + if modifier, ok := modifierMap[upper]; ok { + return upper, modifier, true, nil + } + if code, ok := keycodeMap[trimmed]; ok { + return trimmed, code, false, nil + } + if code, ok := keycodeMap[upper]; ok { + return upper, code, false, nil + } + + return "", 0, false, fmt.Errorf("unsupported key: %s", key) +} + +func buildHotkeyReport(keys []string) ([]byte, *PicoclawError) { + if len(keys) == 0 { + return nil, newPicoclawError(CodeInvalidAction, "hotkey requires at least one key") + } + + report := make([]byte, 8) + nextIndex := 2 + + for _, key := range keys { + _, code, isModifier, err := resolveKey(key) + if err != nil { + return nil, newPicoclawError(CodeInvalidAction, err.Error()) + } + + if isModifier { + report[0] |= code + continue + } + + if nextIndex >= len(report) { + return nil, newPicoclawError(CodeInvalidAction, "hotkey supports at most 6 non-modifier keys") + } + report[nextIndex] = code + nextIndex++ + } + + return report, nil +} diff --git a/server/service/picoclaw/load_image_instruction.go b/server/service/picoclaw/load_image_instruction.go new file mode 100644 index 0000000..860cb3c --- /dev/null +++ b/server/service/picoclaw/load_image_instruction.go @@ -0,0 +1,102 @@ +package picoclaw + +import ( + "encoding/json" + "fmt" + "strings" + "time" + + "github.com/gin-gonic/gin" +) + +const ( + defaultImagePrompt = "Describe the image briefly." +) + +func (s *Service) LoadImage(c *gin.Context) { + var req LoadImageRequest + if err := c.ShouldBindJSON(&req); err != nil { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "invalid load image payload")) + return + } + + sessionID, session, sessionErr := s.requireActiveGatewaySession(c) + if sessionErr != nil { + writePicoclawError(c, sessionErr) + return + } + + sourcePath, pathErr := normalizeLoadImagePath(req.Path) + if pathErr != nil { + writePicoclawError(c, pathErr) + return + } + + instruction := buildLoadImageInstruction(sourcePath, req.Prompt) + message := newPicoGatewayMessage(sessionID, map[string]any{ + "content": instruction, + }) + if err := session.writeUpstreamJSON(s.config.Get(), message); err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to deliver image instruction to picoclaw")) + return + } + + writeSuccess(c, gin.H{ + "accepted": true, + "session_id": sessionID, + "message_id": message.ID, + "path": sourcePath, + "user_prompt": normalizeImagePrompt(req.Prompt), + "instructed_at": time.Now(), + }) +} + +func (s *Service) requireActiveGatewaySession(c *gin.Context) (string, *GatewaySession, *PicoclawError) { + sessionID := strings.TrimSpace(c.GetHeader(sessionIDHeader)) + if sessionID == "" { + sessionID = strings.TrimSpace(s.lock.Owner()) + } + if sessionID == "" { + return "", nil, newPicoclawError(CodeSessionIDMissing, "missing X-PicoClaw-Session-ID") + } + + session, ok := GetSessionManager().Get(sessionID) + if !ok || session == nil { + err := newPicoclawError(CodeRuntimeUnavailable, "picoclaw session is not connected") + err.SessionID = sessionID + return "", nil, err + } + if session.State != SessionStateActive || session.Upstream == nil { + err := newPicoclawError(CodeRuntimeUnavailable, "picoclaw session is not active") + err.SessionID = sessionID + return "", nil, err + } + + return sessionID, session, nil +} + +func normalizeLoadImagePath(sourcePath string) (string, *PicoclawError) { + sourcePath = strings.TrimSpace(sourcePath) + if sourcePath == "" { + return "", newPicoclawError(CodeInvalidAction, "image path is required") + } + + return sourcePath, nil +} + +func buildLoadImageInstruction(sourcePath string, prompt string) string { + argsJSON, _ := json.Marshal(map[string]string{"path": sourcePath}) + return fmt.Sprintf( + "Call `load_image` first with this exact argument object:\n```json\n%s\n```\n\nDo not ask the user to re-upload the image. Do not modify the path. Do not use `read_file` or any other tool to inspect this image directly.\n\nAfter `load_image` succeeds, treat the loaded image as the current task input and continue with this request:\n%s", + string(argsJSON), + normalizeImagePrompt(prompt), + ) +} + +func normalizeImagePrompt(prompt string) string { + prompt = strings.TrimSpace(prompt) + if prompt == "" { + return defaultImagePrompt + } + return prompt +} diff --git a/server/service/picoclaw/mcp_handler.go b/server/service/picoclaw/mcp_handler.go new file mode 100644 index 0000000..7dec412 --- /dev/null +++ b/server/service/picoclaw/mcp_handler.go @@ -0,0 +1,299 @@ +package picoclaw + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" +) + +// JSON-RPC 2.0 types for MCP HTTP transport + +type jsonRPCRequest struct { + JSONRPC string `json:"jsonrpc"` + ID json.RawMessage `json:"id"` + Method string `json:"method"` + Params json.RawMessage `json:"params,omitempty"` +} + +type jsonRPCResponse struct { + JSONRPC string `json:"jsonrpc"` + ID json.RawMessage `json:"id"` + Result interface{} `json:"result,omitempty"` + Error *jsonRPCError `json:"error,omitempty"` +} + +type jsonRPCError struct { + Code int `json:"code"` + Message string `json:"message"` +} + +// MCP tool definitions + +var mcpToolDefinitions = []map[string]interface{}{ + { + "name": "kvm_screenshot", + "description": "Capture the current HDMI frame from the downstream remote host as a base64-encoded JPEG image.", + "inputSchema": map[string]interface{}{ + "type": "object", + "properties": map[string]interface{}{ + "width": map[string]interface{}{"type": "integer", "description": "Target width in pixels (optional, default: 960)"}, + "height": map[string]interface{}{"type": "integer", "description": "Target height in pixels (optional)"}, + "quality": map[string]interface{}{"type": "integer", "description": "JPEG quality 1-100 (optional, default: 60)"}, + }, + }, + }, + { + "name": "kvm_actions", + "description": "Send one or more HID actions (click, type, hotkey, scroll, drag, move, wait) to the downstream remote host. Use normalized [0,1] coordinates for mouse actions.", + "inputSchema": map[string]interface{}{ + "type": "object", + "properties": map[string]interface{}{ + "actions": map[string]interface{}{ + "type": "array", + "description": "Array of action objects. Each requires an 'action' field (click, move, type, hotkey, scroll, drag, wait).", + "items": map[string]interface{}{ + "type": "object", + "properties": map[string]interface{}{ + "action": map[string]interface{}{"type": "string"}, + "x": map[string]interface{}{"type": "number"}, + "y": map[string]interface{}{"type": "number"}, + "button": map[string]interface{}{"type": "string"}, + "text": map[string]interface{}{"type": "string"}, + "keys": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}}, + "direction": map[string]interface{}{"type": "string"}, + "amount": map[string]interface{}{"type": "integer"}, + "duration_ms": map[string]interface{}{"type": "integer"}, + "from": map[string]interface{}{"type": "object", "properties": map[string]interface{}{"x": map[string]interface{}{"type": "number"}, "y": map[string]interface{}{"type": "number"}}}, + "to": map[string]interface{}{"type": "object", "properties": map[string]interface{}{"x": map[string]interface{}{"type": "number"}, "y": map[string]interface{}{"type": "number"}}}, + }, + "required": []string{"action"}, + }, + }, + }, + "required": []string{"actions"}, + }, + }, +} + +// MCPHandler handles MCP HTTP transport (JSON-RPC 2.0 over POST). +func (s *Service) MCPHandler(c *gin.Context) { + var req jsonRPCRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Error: &jsonRPCError{Code: -32700, Message: "parse error"}, + }) + return + } + + if req.JSONRPC != "2.0" { + c.JSON(http.StatusOK, jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Error: &jsonRPCError{Code: -32600, Message: "invalid request: jsonrpc must be 2.0"}, + }) + return + } + + var resp jsonRPCResponse + switch req.Method { + case "initialize": + resp = s.mcpInitialize(req) + case "tools/list": + resp = s.mcpToolsList(req) + case "tools/call": + resp = s.mcpToolsCall(req, c) + case "ping": + resp = jsonRPCResponse{JSONRPC: "2.0", ID: req.ID, Result: map[string]interface{}{}} + default: + resp = jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Error: &jsonRPCError{Code: -32601, Message: fmt.Sprintf("method not found: %s", req.Method)}, + } + } + + c.JSON(http.StatusOK, resp) +} + +func (s *Service) mcpInitialize(req jsonRPCRequest) jsonRPCResponse { + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Result: map[string]interface{}{ + "protocolVersion": "2024-11-05", + "capabilities": map[string]interface{}{ + "tools": map[string]interface{}{}, + }, + "serverInfo": map[string]interface{}{ + "name": "nanokvm", + "version": "1.0.0", + }, + }, + } +} + +func (s *Service) mcpToolsList(req jsonRPCRequest) jsonRPCResponse { + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Result: map[string]interface{}{ + "tools": mcpToolDefinitions, + }, + } +} + +func (s *Service) mcpToolsCall(req jsonRPCRequest, c *gin.Context) jsonRPCResponse { + var params struct { + Name string `json:"name"` + Arguments json.RawMessage `json:"arguments"` + } + if err := json.Unmarshal(req.Params, ¶ms); err != nil { + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Error: &jsonRPCError{Code: -32602, Message: "invalid params"}, + } + } + + switch params.Name { + case "kvm_screenshot": + return s.mcpScreenshot(req, params.Arguments, c) + case "kvm_actions": + return s.mcpActions(req, params.Arguments, c) + default: + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Error: &jsonRPCError{Code: -32602, Message: fmt.Sprintf("unknown tool: %s", params.Name)}, + } + } +} + +func (s *Service) mcpScreenshot(req jsonRPCRequest, args json.RawMessage, c *gin.Context) jsonRPCResponse { + var params struct { + Width uint16 `json:"width"` + Height uint16 `json:"height"` + Quality uint16 `json:"quality"` + } + if args != nil { + _ = json.Unmarshal(args, ¶ms) + } + + query := ScreenshotQuery{ + Format: "base64", + Width: params.Width, + Height: params.Height, + Quality: params.Quality, + } + + data, meta, err := s.captureScreenshot(query) + if err != nil { + return mcpToolError(req, err.Message) + } + + b64 := base64.StdEncoding.EncodeToString(data) + s.publishMCPObservation(c, "screenshot captured", b64) + + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Result: map[string]interface{}{ + "content": []map[string]interface{}{ + { + "type": "text", + "text": "screenshot captured", + }, + { + "type": "image", + "data": b64, + "mimeType": "image/jpeg", + }, + }, + "meta": map[string]interface{}{ + "source_width": meta.SourceWidth, + "source_height": meta.SourceHeight, + "capture_width": meta.CaptureWidth, + "capture_height": meta.CaptureHeight, + }, + }, + } +} + +func (s *Service) publishMCPObservation(c *gin.Context, text string, imageBase64 string) { + if strings.TrimSpace(imageBase64) == "" { + return + } + + sessionID, session, err := s.requireActiveGatewaySession(c) + if err != nil || session == nil || session.Downstream == nil { + return + } + + message := newPicoGatewayObservationMessage(sessionID, map[string]any{ + "content": text, + "image_base64": imageBase64, + "mime_type": "image/jpeg", + }) + if writeErr := session.writeDownstreamJSON(s.config.Get(), message); writeErr != nil { + log.Warnf("failed to deliver picoclaw observation message: %v", writeErr) + } +} + +func (s *Service) mcpActions(req jsonRPCRequest, args json.RawMessage, c *gin.Context) jsonRPCResponse { + var params struct { + Actions []Action `json:"actions"` + } + if err := json.Unmarshal(args, ¶ms); err != nil { + return mcpToolError(req, "invalid actions payload") + } + if len(params.Actions) == 0 { + return mcpToolError(req, "empty actions array") + } + + sessionID := c.GetHeader(sessionIDHeader) + if sessionID == "" { + sessionID = s.lock.Owner() + } + + result, err := s.executeActions(sessionID, params.Actions) + if err != nil { + return mcpToolError(req, err.Message) + } + + resultJSON, _ := json.Marshal(result) + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Result: map[string]interface{}{ + "content": []map[string]interface{}{ + { + "type": "text", + "text": string(resultJSON), + }, + }, + }, + } +} + +func mcpToolError(req jsonRPCRequest, message string) jsonRPCResponse { + return jsonRPCResponse{ + JSONRPC: "2.0", + ID: req.ID, + Result: map[string]interface{}{ + "isError": true, + "content": []map[string]interface{}{ + { + "type": "text", + "text": message, + }, + }, + }, + } +} diff --git a/server/service/picoclaw/model_config.go b/server/service/picoclaw/model_config.go new file mode 100644 index 0000000..4ce174d --- /dev/null +++ b/server/service/picoclaw/model_config.go @@ -0,0 +1,225 @@ +package picoclaw + +import ( + "fmt" + "strings" + + "github.com/gin-gonic/gin" +) + +func extractPicoclawModelName(model string) string { + model = strings.TrimSpace(model) + if model == "" { + return "" + } + + if index := strings.LastIndex(model, "/"); index >= 0 && index < len(model)-1 { + return strings.TrimSpace(model[index+1:]) + } + + return model +} + +func isPicoclawModelConfigured(cfg picoclawConfigFile, security picoclawSecurityConfig, modelName string) bool { + if modelName == "" { + return false + } + + for _, model := range cfg.ModelList { + if strings.TrimSpace(model.ModelName) != modelName { + continue + } + if model.APIBase == "" { + continue + } + if securityHasModelAPIKeys(security, modelName) { + return true + } + return configHasModelAPIKeys(model.APIKey, model.APIKeys) + } + + return false +} + +func configHasModelAPIKeys(apiKey string, apiKeys []string) bool { + return strings.TrimSpace(apiKey) != "" || len(apiKeys) > 0 +} + +func securityHasModelAPIKeys(security picoclawSecurityConfig, modelName string) bool { + if modelName == "" || len(security.ModelList) == 0 { + return false + } + + if entry, ok := security.ModelList[modelName]; ok && len(entry.APIKeys) > 0 { + return true + } + + prefix := modelName + ":" + for key, entry := range security.ModelList { + if strings.HasPrefix(key, prefix) && len(entry.APIKeys) > 0 { + return true + } + } + + return false +} + +type ModelConfigUpdateRequest struct { + Model string `json:"model"` + APIBase string `json:"api_base"` + APIKey string `json:"api_key"` +} + +func (s *Service) UpdateModelConfig(c *gin.Context) { + var req ModelConfigUpdateRequest + if err := c.ShouldBindJSON(&req); err != nil { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "invalid model config payload")) + return + } + + currentStatus := s.runtime.Get() + shouldRestart := currentStatus.Ready || currentStatus.Status == "ready" + + modelName, err := updatePicoclawModelConfig( + strings.TrimSpace(req.APIBase), + strings.TrimSpace(req.APIKey), + strings.TrimSpace(req.Model), + ) + if err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, err.Error())) + return + } + if err := ensurePicoclawStartupDefaults(); err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "model config saved, but failed to persist picoclaw defaults: "+err.Error())) + return + } + + if shouldRestart { + if _, _, stopErr := s.stopRuntime(); stopErr != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "model config saved, but failed to restart picoclaw runtime: "+stopErr.Message)) + return + } + if _, _, startErr := s.startRuntime(); startErr != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "model config saved, but failed to restart picoclaw runtime: "+startErr.Message)) + return + } + } else { + _ = s.syncConfigFromPicoclaw() + _ = s.ensureRuntimeReady() + } + + writeSuccess(c, gin.H{ + "model_name": modelName, + "status": s.runtime.Get(), + }) +} + +func updatePicoclawModelConfig(apiBase string, apiKey string, model string) (string, error) { + if apiBase == "" { + return "", fmt.Errorf("model api_base is required") + } + if apiKey == "" { + return "", fmt.Errorf("model api_key is required") + } + if model == "" { + return "", fmt.Errorf("model identifier is required") + } + + modelName := extractPicoclawModelName(model) + if modelName == "" { + return "", fmt.Errorf("model identifier is required") + } + + doc, err := loadPicoclawConfigDocument() + if err != nil { + return "", err + } + + modelListValue, ok := doc.raw["model_list"].([]any) + if !ok { + modelListValue = []any{} + } + + modelUpdated := false + updatedModelIndex := -1 + for index, item := range modelListValue { + modelMap, ok := item.(map[string]any) + if !ok { + continue + } + currentModelName := strings.TrimSpace(fmt.Sprintf("%v", modelMap["model_name"])) + if currentModelName != modelName { + continue + } + modelMap["model_name"] = modelName + modelMap["model"] = model + modelMap["api_base"] = apiBase + delete(modelMap, "api_key") + delete(modelMap, "api_keys") + modelUpdated = true + updatedModelIndex = index + break + } + if !modelUpdated { + modelListValue = append(modelListValue, map[string]any{ + "model_name": modelName, + "model": model, + "api_base": apiBase, + }) + doc.raw["model_list"] = modelListValue + updatedModelIndex = len(modelListValue) - 1 + } + + agents, ok := doc.raw["agents"].(map[string]any) + if !ok { + agents = map[string]any{} + doc.raw["agents"] = agents + } + defaults, ok := agents["defaults"].(map[string]any) + if !ok { + defaults = map[string]any{} + agents["defaults"] = defaults + } + defaults["model_name"] = modelName + delete(defaults, "model") + + if err := doc.saveConfig(); err != nil { + return "", err + } + if doc.security.ModelList == nil { + doc.security.ModelList = map[string]picoclawModelSecurityEntry{} + } + securityModelName := indexedModelName(modelListValue, updatedModelIndex, modelName) + doc.security.ModelList[securityModelName] = picoclawModelSecurityEntry{ + APIKeys: []string{apiKey}, + } + if err := doc.saveSecurity(); err != nil { + return "", err + } + + return modelName, nil +} + +func indexedModelName(modelList []any, targetIndex int, modelName string) string { + if targetIndex < 0 { + return modelName + } + + currentIndex := 0 + for index, item := range modelList { + modelMap, ok := item.(map[string]any) + if !ok { + continue + } + currentModelName := strings.TrimSpace(fmt.Sprintf("%v", modelMap["model_name"])) + if currentModelName != modelName { + continue + } + if index == targetIndex { + return fmt.Sprintf("%s:%d", modelName, currentIndex) + } + currentIndex++ + } + + return modelName +} diff --git a/server/service/picoclaw/runtime_constants.go b/server/service/picoclaw/runtime_constants.go new file mode 100644 index 0000000..796f6e5 --- /dev/null +++ b/server/service/picoclaw/runtime_constants.go @@ -0,0 +1,22 @@ +package picoclaw + +import ( + "time" +) + +const ( + picoclawBinaryPath = "/usr/bin/picoclaw" + picoclawCacheDir = "/root/.picoclaw-cache" + picoclawDownloadURL = "https://cdn.sipeed.com/nanokvm/resources/picoclaw/v0.2.6/picoclaw_Linux_riscv64.tar.gz" + picoclawChecksumURL = "https://cdn.sipeed.com/nanokvm/resources/picoclaw/v0.2.6/sha512.txt" + etcInitPicoclawScript = "/etc/init.d/S96picoclaw" + kvmappPicoclawScript = "/kvmapp/system/init.d/S96picoclaw" + picoclawStartTimeout = 15 * time.Second + picoclawStopTimeout = 15 * time.Second + picoclawOnboardTimeout = 60 * time.Second + picoclawInstallTimeout = 12 * time.Minute + picoclawDownloadTimeout = 10 * time.Minute + picoclawStartWaitPeriod = 1500 * time.Millisecond + picoclawStopWaitPeriod = 500 * time.Millisecond + picoclawReadyPollPeriod = 500 * time.Millisecond +) diff --git a/server/service/picoclaw/runtime_defaults.go b/server/service/picoclaw/runtime_defaults.go new file mode 100644 index 0000000..3b6c0ed --- /dev/null +++ b/server/service/picoclaw/runtime_defaults.go @@ -0,0 +1,227 @@ +package picoclaw + +import ( + "crypto/rand" + "encoding/hex" + "fmt" + "reflect" + "strings" + + serverConfig "NanoKVM-Server/config" +) + +const ( + defaultPicoclawPingSec = 30 + defaultPicoclawReadSec = 60 + defaultPicoclawWriteSec = 10 + defaultPicoclawMaxConns = 100 +) + +type picoclawConfigDefault struct { + path []string + value any +} + +var picoclawNanoKVMDefaults = []picoclawConfigDefault{ + {path: []string{"agents", "defaults", "restrict_to_workspace"}, value: false}, + {path: []string{"agents", "defaults", "allow_read_outside_workspace"}, value: true}, + {path: []string{"agents", "defaults", "tool_feedback", "enabled"}, value: true}, + {path: []string{"gateway", "host"}, value: defaultPicoclawGatewayHost}, + {path: []string{"gateway", "port"}, value: defaultPicoclawGatewayPort}, + {path: []string{"gateway", "hot_reload"}, value: false}, + {path: []string{"tools", "cron", "allow_command"}, value: true}, + {path: []string{"tools", "exec", "allow_remote"}, value: true}, + {path: []string{"tools", "exec", "enable_deny_patterns"}, value: false}, + {path: []string{"channels", "pico", "allow_token_query"}, value: false}, + {path: []string{"channels", "pico", "ping_interval"}, value: defaultPicoclawPingSec}, + {path: []string{"channels", "pico", "read_timeout"}, value: defaultPicoclawReadSec}, + {path: []string{"channels", "pico", "write_timeout"}, value: defaultPicoclawWriteSec}, + {path: []string{"channels", "pico", "max_connections"}, value: defaultPicoclawMaxConns}, + {path: []string{"tools", "mcp", "enabled"}, value: true}, +} + +func ensurePicoclawStartupDefaults() error { + doc, err := loadPicoclawConfigDocument() + if err != nil { + return err + } + + editor := &picoclawConfigEditor{raw: doc.raw} + forceEnablePicoclawPicoChannel(doc, editor) + if err := applyPicoclawStartupDefaults(editor); err != nil { + return err + } + + tokenChanged, err := ensurePicoclawPicoToken(doc) + if err != nil { + return err + } + + if tokenChanged { + if err := doc.saveSecurity(); err != nil { + return err + } + } + if editor.changed { + if err := doc.saveConfig(); err != nil { + return err + } + } + + return nil +} + +func ensurePicoclawPicoChannelEnabled(doc *picoclawConfigDocument) error { + if doc == nil || doc.config.Channels.Pico.Enabled { + return nil + } + + editor := &picoclawConfigEditor{raw: doc.raw} + forceEnablePicoclawPicoChannel(doc, editor) + if !editor.changed { + return nil + } + + return doc.saveConfig() +} + +func applyPicoclawStartupDefaults(editor *picoclawConfigEditor) error { + for _, entry := range picoclawNanoKVMDefaults { + editor.setValue(entry.value, entry.path...) + } + server, err := defaultPicoclawMCPServer() + if err != nil { + return err + } + editor.setMCPServer("nanokvm", server) + return nil +} + +func forceEnablePicoclawPicoChannel(doc *picoclawConfigDocument, editor *picoclawConfigEditor) { + if doc == nil || editor == nil || doc.config.Channels.Pico.Enabled { + return + } + + editor.setValue(true, "channels", "pico", "enabled") + doc.config.Channels.Pico.Enabled = true +} + +func defaultPicoclawMCPServer() (map[string]any, error) { + internalToken, err := serverConfig.GetPicoclawInternalToken() + if err != nil { + return nil, err + } + + return map[string]any{ + "enabled": true, + "type": "http", + "url": nanoKVMMCPURL(), + "headers": map[string]any{ + serverConfig.PicoclawInternalTokenHeader: internalToken, + }, + }, nil +} + +func nanoKVMMCPURL() string { + port := serverConfig.GetInstance().Port.Http + if port <= 0 { + port = 80 + } + + return fmt.Sprintf("http://%s:%d/api/picoclaw/mcp", defaultPicoclawGatewayHost, port) +} + +type picoclawConfigEditor struct { + raw map[string]any + changed bool +} + +func (e *picoclawConfigEditor) setValue(value any, path ...string) { + if len(path) == 0 { + return + } + + target := e.ensureObject(path[:len(path)-1]...) + key := path[len(path)-1] + if current, exists := target[key]; exists && reflect.DeepEqual(current, value) { + return + } + + target[key] = value + e.changed = true +} + +func (e *picoclawConfigEditor) ensureObject(path ...string) map[string]any { + current := e.raw + for _, key := range path { + next, ok := current[key].(map[string]any) + if !ok { + next = map[string]any{} + current[key] = next + e.changed = true + } + current = next + } + return current +} + +func (e *picoclawConfigEditor) setMCPServer(name string, fields map[string]any) { + servers := e.ensureObject("tools", "mcp", "servers") + entry, ok := servers[name].(map[string]any) + if !ok { + entry = map[string]any{} + servers[name] = entry + e.changed = true + } + + for key, value := range fields { + if current, exists := entry[key]; exists && reflect.DeepEqual(current, value) { + continue + } + entry[key] = value + e.changed = true + } +} + +func generatePicoclawToken() (string, error) { + buf := make([]byte, 24) + if _, err := rand.Read(buf); err != nil { + return "", fmt.Errorf("failed to generate picoclaw pico token: %w", err) + } + return hex.EncodeToString(buf), nil +} + +func ensurePicoclawPicoToken(doc *picoclawConfigDocument) (bool, error) { + token, err := picoSecurityToken(doc) + if err != nil { + return false, err + } + + if doc.security.Channels.Pico == nil { + doc.security.Channels.Pico = &picoclawPicoSecurity{} + } + if strings.TrimSpace(doc.security.Channels.Pico.Token) == token { + return false, nil + } + + doc.security.Channels.Pico.Token = token + return true, nil +} + +func picoSecurityToken(doc *picoclawConfigDocument) (string, error) { + if doc == nil { + return "", nil + } + + if doc.security.Channels.Pico != nil { + if token := strings.TrimSpace(doc.security.Channels.Pico.Token); token != "" { + return token, nil + } + } + + if token := strings.TrimSpace(doc.config.Channels.Pico.Token); token != "" { + return token, nil + } + + return generatePicoclawToken() +} diff --git a/server/service/picoclaw/runtime_handlers.go b/server/service/picoclaw/runtime_handlers.go new file mode 100644 index 0000000..38ce184 --- /dev/null +++ b/server/service/picoclaw/runtime_handlers.go @@ -0,0 +1,110 @@ +package picoclaw + +import ( + "os" + "path/filepath" + "time" + + "github.com/gin-gonic/gin" +) + +func (s *Service) StartRuntime(c *gin.Context) { + command, output, err := s.startRuntime() + if err != nil { + writePicoclawError(c, err) + return + } + + writeSuccess(c, RuntimeStartResult{ + Started: true, + Command: command, + Output: output, + Status: s.runtime.Get(), + }) +} + +func (s *Service) StopRuntime(c *gin.Context) { + command, output, err := s.stopRuntime() + if err != nil { + writePicoclawError(c, err) + return + } + + writeSuccess(c, RuntimeStartResult{ + Started: false, + Command: command, + Output: output, + Status: s.runtime.Get(), + }) +} + +func (s *Service) InstallRuntime(c *gin.Context) { + output, err := s.installRuntime() + if err != nil { + writePicoclawError(c, err) + return + } + + currentStatus := s.runtime.Get() + writeSuccess(c, RuntimeInstallResult{ + Installed: currentStatus.Installed && !currentStatus.Installing, + Binary: picoclawBinaryPath, + Download: picoclawDownloadURL, + Output: output, + Status: currentStatus, + }) +} + +func (s *Service) UninstallRuntime(c *gin.Context) { + currentStatus := s.runtime.Get() + uninstallOutput := "picoclaw uninstalled successfully" + if currentStatus.Installing { + writePicoclawError(c, newPicoclawError(CodeRuntimeStartFailed, "cannot uninstall while installation is in progress")) + return + } + + running, err := isRuntimeRunning() + if err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to check picoclaw runtime status")) + return + } + + if running || currentStatus.Ready || currentStatus.Status == "ready" { + if _, _, stopErr := s.stopRuntime(); stopErr != nil { + uninstallOutput = "picoclaw uninstalled successfully (stop failed before uninstall: " + stopErr.Message + ")" + } + } + + if configPath, err := resolvePicoclawConfigPath(); err == nil { + _ = os.RemoveAll(filepath.Dir(configPath)) + } else { + home, err := os.UserHomeDir() + if err == nil { + _ = os.RemoveAll(filepath.Join(home, ".picoclaw")) + } else { + _ = os.RemoveAll("/root/.picoclaw") + } + } + _ = os.Remove(picoclawBinaryPath) + _ = os.RemoveAll(picoclawCacheDir) + + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: false, + Installing: false, + InstallProgress: 0, + InstallStage: "", + InstallPath: picoclawBinaryPath, + Status: "not_installed", + CheckedAt: time.Now(), + CurrentSession: "", + }) + + writeSuccess(c, RuntimeInstallResult{ + Installed: false, + Binary: picoclawBinaryPath, + Download: picoclawDownloadURL, + Output: uninstallOutput, + Status: s.runtime.Get(), + }) +} diff --git a/server/service/picoclaw/runtime_install.go b/server/service/picoclaw/runtime_install.go new file mode 100644 index 0000000..78d3fa7 --- /dev/null +++ b/server/service/picoclaw/runtime_install.go @@ -0,0 +1,441 @@ +package picoclaw + +import ( + "archive/tar" + "compress/gzip" + "context" + "crypto/sha512" + "encoding/hex" + "errors" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "time" + + log "github.com/sirupsen/logrus" +) + +func (s *Service) installRuntime() (string, *PicoclawError) { + log.Debugf("picoclaw install: start, binary=%s, cache=%s", picoclawBinaryPath, picoclawCacheDir) + + currentStatus := s.runtime.Get() + if currentStatus.Installing { + log.Debugf("picoclaw install: install already in progress") + return "picoclaw installation is already in progress", nil + } + + if installed, err := isPicoclawInstalled(); err == nil && installed { + settings, _ := loadPicoclawGatewaySettings() + log.Debugf("picoclaw install: binary already exists at %s", picoclawBinaryPath) + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 100, + InstallStage: "installed", + InstallPath: picoclawBinaryPath, + ModelConfigured: settings.ModelConfigured, + ModelName: settings.ModelName, + Status: "installed", + CheckedAt: time.Now(), + }) + return "picoclaw is already installed", nil + } + + ctx, cancel := context.WithTimeout(context.Background(), picoclawInstallTimeout) + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: false, + Installing: true, + InstallProgress: 0, + InstallStage: "preparing", + InstallPath: picoclawBinaryPath, + Status: "installing", + CheckedAt: time.Now(), + }) + + go s.runInstallRuntime(ctx, cancel) + return "picoclaw installation started", nil +} + +func (s *Service) runInstallRuntime(ctx context.Context, cancel context.CancelFunc) { + defer cancel() + + _ = os.RemoveAll(picoclawCacheDir) + if err := os.MkdirAll(picoclawCacheDir, 0o755); err != nil { + log.Errorf("picoclaw install: failed to create cache directory %s: %v", picoclawCacheDir, err) + s.finishInstallFailure("install_failed", fmt.Sprintf("failed to create cache directory: %v", err)) + return + } + log.Debugf("picoclaw install: cache directory ready at %s", picoclawCacheDir) + defer func() { + if err := os.RemoveAll(picoclawCacheDir); err != nil { + log.Errorf("picoclaw install: failed to clean cache directory %s: %v", picoclawCacheDir, err) + return + } + log.Debugf("picoclaw install: cleaned cache directory %s", picoclawCacheDir) + }() + + s.setInstallProgress("downloading", 5, "") + log.Debugf("picoclaw install: downloading checksum from %s", picoclawChecksumURL) + expectedDigest, err := downloadPicoclawChecksum(ctx) + if err != nil { + log.Errorf("picoclaw install: checksum download failed: %v", err) + s.finishInstallFailure(installFailureStatus(err), err.Error()) + return + } + log.Debug("picoclaw install: checksum file downloaded") + + archivePath := filepath.Join(picoclawCacheDir, "picoclaw.tar.gz") + log.Debugf("picoclaw install: downloading archive from %s to %s", picoclawDownloadURL, archivePath) + if err := downloadPicoclawArchive(ctx, archivePath, func(downloaded int64, total int64) { + progress := 10 + if total > 0 { + progress = 10 + int(float64(downloaded)*70/float64(total)) + if progress > 80 { + progress = 80 + } + } + s.setInstallProgress("downloading", progress, "") + }); err != nil { + log.Errorf("picoclaw install: download failed: %v", err) + s.finishInstallFailure(installFailureStatus(err), err.Error()) + return + } + log.Debugf("picoclaw install: archive download completed") + + s.setInstallProgress("verifying", 82, "") + + if err := verifyFileSHA512(archivePath, expectedDigest); err != nil { + log.Errorf("picoclaw install: checksum verification failed: %v", err) + s.finishInstallFailure(installFailureStatus(err), err.Error()) + return + } + log.Debugf("picoclaw install: archive checksum verified for %s", archivePath) + + s.setInstallProgress("extracting", 85, "") + log.Debugf("picoclaw install: extracting binary from %s", archivePath) + extractedPath, err := extractPicoclawBinary(archivePath, picoclawCacheDir) + if err != nil { + log.Errorf("picoclaw install: extract failed: %v", err) + s.finishInstallFailure(installFailureStatus(err), err.Error()) + return + } + log.Debugf("picoclaw install: extracted binary to %s", extractedPath) + + s.setInstallProgress("installing", 95, "") + log.Debugf("picoclaw install: installing binary to %s", picoclawBinaryPath) + if err := installPicoclawBinary(extractedPath, picoclawBinaryPath); err != nil { + log.Errorf("picoclaw install: install failed: %v", err) + s.finishInstallFailure(installFailureStatus(err), err.Error()) + return + } + log.Debugf("picoclaw install: install completed successfully") + + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 100, + InstallStage: "installed", + InstallPath: picoclawBinaryPath, + Status: "installed", + CheckedAt: time.Now(), + }) +} + +func downloadPicoclawArchive(ctx context.Context, destination string, onProgress func(downloaded int64, total int64)) error { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, picoclawDownloadURL, nil) + if err != nil { + return fmt.Errorf("failed to create download request: %w", err) + } + + client := &http.Client{ + Timeout: picoclawDownloadTimeout, + } + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("failed to download picoclaw: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("failed to download picoclaw: unexpected status %s", resp.Status) + } + + file, err := os.Create(destination) + if err != nil { + return fmt.Errorf("failed to create archive file: %w", err) + } + defer file.Close() + + if err := copyWithProgress(ctx, file, resp.Body, resp.ContentLength, onProgress); err != nil { + return fmt.Errorf("failed to save archive: %w", err) + } + return nil +} + +func downloadPicoclawChecksum(ctx context.Context) (string, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, picoclawChecksumURL, nil) + if err != nil { + return "", fmt.Errorf("failed to create checksum request: %w", err) + } + + client := &http.Client{ + Timeout: picoclawDownloadTimeout, + } + resp, err := client.Do(req) + if err != nil { + return "", fmt.Errorf("failed to download picoclaw checksum: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to download picoclaw checksum: unexpected status %s", resp.Status) + } + + data, err := io.ReadAll(io.LimitReader(resp.Body, 16*1024)) + if err != nil { + return "", fmt.Errorf("failed to read checksum file: %w", err) + } + + digest, err := parseSHA512Digest(string(data), filepath.Base(picoclawDownloadURL)) + if err != nil { + return "", err + } + return digest, nil +} + +func copyWithProgress(ctx context.Context, dst io.Writer, src io.Reader, total int64, onProgress func(downloaded int64, total int64)) error { + buffer := make([]byte, 32*1024) + var downloaded int64 + + for { + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + + n, readErr := src.Read(buffer) + if n > 0 { + if _, writeErr := dst.Write(buffer[:n]); writeErr != nil { + return writeErr + } + downloaded += int64(n) + if onProgress != nil { + onProgress(downloaded, total) + } + } + if readErr == io.EOF { + if onProgress != nil { + onProgress(downloaded, total) + } + return nil + } + if readErr != nil { + return readErr + } + } +} + +func (s *Service) setInstallProgress(stage string, progress int, lastError string) { + if progress < 0 { + progress = 0 + } + if progress > 100 { + progress = 100 + } + + s.runtime.UpdateInstallStatus(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = false + status.Installing = true + status.InstallProgress = progress + status.InstallStage = stage + status.Status = "installing" + status.LastError = lastError + status.CheckedAt = time.Now() + }) +} + +func (s *Service) finishInstallFailure(status string, message string) { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: false, + Installing: false, + InstallProgress: 0, + InstallStage: status, + InstallPath: picoclawBinaryPath, + Status: status, + LastError: message, + CheckedAt: time.Now(), + }) +} + +func installFailureStatus(err error) string { + if err == nil { + return "install_failed" + } + if errors.Is(err, context.DeadlineExceeded) { + return "install_timeout" + } + return "install_failed" +} + +func parseSHA512Digest(raw string, expectedName string) (string, error) { + lines := strings.Split(raw, "\n") + var fallback string + + for _, line := range lines { + trimmed := strings.TrimSpace(line) + if trimmed == "" { + continue + } + + fields := strings.Fields(trimmed) + if len(fields) == 0 { + continue + } + + digest := fields[0] + if !isValidSHA512Digest(digest) { + continue + } + + if len(fields) == 1 { + if fallback == "" { + fallback = strings.ToLower(digest) + } + continue + } + + name := strings.TrimPrefix(fields[len(fields)-1], "*") + if expectedName == "" || name == expectedName { + return strings.ToLower(digest), nil + } + } + + if fallback != "" { + return fallback, nil + } + + return "", fmt.Errorf("failed to parse sha512 digest from checksum file") +} + +func isValidSHA512Digest(value string) bool { + if len(value) != sha512.Size*2 { + return false + } + + _, err := hex.DecodeString(value) + return err == nil +} + +func verifyFileSHA512(filePath string, expectedDigest string) error { + expectedDigest = strings.ToLower(strings.TrimSpace(expectedDigest)) + if !isValidSHA512Digest(expectedDigest) { + return fmt.Errorf("invalid expected sha512 digest") + } + + file, err := os.Open(filePath) + if err != nil { + return fmt.Errorf("failed to open file for sha512 verification: %w", err) + } + defer file.Close() + + hasher := sha512.New() + if _, err := io.Copy(hasher, file); err != nil { + return fmt.Errorf("failed to hash file for sha512 verification: %w", err) + } + + actualDigest := hex.EncodeToString(hasher.Sum(nil)) + if actualDigest != expectedDigest { + return fmt.Errorf("sha512 mismatch: got %s", actualDigest) + } + + return nil +} + +func extractPicoclawBinary(archivePath string, destinationDir string) (string, error) { + file, err := os.Open(archivePath) + if err != nil { + return "", fmt.Errorf("failed to open archive: %w", err) + } + defer file.Close() + + gzipReader, err := gzip.NewReader(file) + if err != nil { + return "", fmt.Errorf("failed to read archive: %w", err) + } + defer gzipReader.Close() + + tarReader := tar.NewReader(gzipReader) + for { + header, err := tarReader.Next() + if err == io.EOF { + break + } + if err != nil { + return "", fmt.Errorf("failed to extract archive: %w", err) + } + if header.Typeflag != tar.TypeReg { + continue + } + if filepath.Base(header.Name) != "picoclaw" { + continue + } + + extractedPath := filepath.Join(destinationDir, "picoclaw") + outFile, err := os.OpenFile(extractedPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) + if err != nil { + return "", fmt.Errorf("failed to create extracted binary: %w", err) + } + if _, err := io.Copy(outFile, tarReader); err != nil { + _ = outFile.Close() + return "", fmt.Errorf("failed to extract picoclaw binary: %w", err) + } + if err := outFile.Close(); err != nil { + return "", fmt.Errorf("failed to finalize extracted binary: %w", err) + } + return extractedPath, nil + } + + return "", fmt.Errorf("picoclaw binary not found in archive") +} + +func installPicoclawBinary(source string, destination string) error { + inFile, err := os.Open(source) + if err != nil { + return fmt.Errorf("failed to open extracted picoclaw binary: %w", err) + } + defer inFile.Close() + + tempDestination := destination + ".tmp" + outFile, err := os.OpenFile(tempDestination, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755) + if err != nil { + return fmt.Errorf("failed to create destination binary: %w", err) + } + + if _, err := io.Copy(outFile, inFile); err != nil { + _ = outFile.Close() + _ = os.Remove(tempDestination) + return fmt.Errorf("failed to write destination binary: %w", err) + } + if err := outFile.Close(); err != nil { + _ = os.Remove(tempDestination) + return fmt.Errorf("failed to finalize destination binary: %w", err) + } + if err := os.Chmod(tempDestination, 0o755); err != nil { + _ = os.Remove(tempDestination) + return fmt.Errorf("failed to set destination mode: %w", err) + } + if err := os.Rename(tempDestination, destination); err != nil { + _ = os.Remove(tempDestination) + return fmt.Errorf("failed to install picoclaw binary: %w", err) + } + return nil +} diff --git a/server/service/picoclaw/runtime_session.go b/server/service/picoclaw/runtime_session.go new file mode 100644 index 0000000..f54626b --- /dev/null +++ b/server/service/picoclaw/runtime_session.go @@ -0,0 +1,31 @@ +package picoclaw + +import ( + "strings" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" +) + +func (s *Service) ReleaseRuntimeSession(c *gin.Context) { + sessionID := strings.TrimSpace(c.GetHeader(sessionIDHeader)) + if sessionID == "" { + writePicoclawError(c, newPicoclawError(CodeSessionIDMissing, "missing X-PicoClaw-Session-ID")) + return + } + + if session, ok := GetSessionManager().Get(sessionID); ok { + s.closeGatewaySession(session, websocket.CloseNormalClosure, "session released") + } else { + ReleaseSession(sessionID) + } + + status := s.runtime.Get() + status.CurrentSession = s.lock.Owner() + s.runtime.Set(status) + + writeSuccess(c, gin.H{ + "released": true, + "current_session": status.CurrentSession, + }) +} diff --git a/server/service/picoclaw/runtime_start_stop.go b/server/service/picoclaw/runtime_start_stop.go new file mode 100644 index 0000000..fb7b2dd --- /dev/null +++ b/server/service/picoclaw/runtime_start_stop.go @@ -0,0 +1,228 @@ +package picoclaw + +import ( + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" +) + +func (s *Service) startRuntime() (string, string, *PicoclawError) { + if installed, statErr := isPicoclawInstalled(); statErr != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = false + status.ModelConfigured = false + status.ModelName = "" + status.Status = "not_installed" + status.LastError = statErr.Error() + status.CheckedAt = time.Now() + }) + return "", "", newPicoclawError(CodeRuntimeStartFailed, statErr.Error()) + } else if !installed { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = false + status.ModelConfigured = false + status.ModelName = "" + status.Status = "not_installed" + status.LastError = "picoclaw is not installed" + status.CheckedAt = time.Now() + }) + return "", "", newPicoclawError(CodeRuntimeStartFailed, "picoclaw is not installed") + } + + scriptPath, err := resolvePicoclawStartScript() + if err != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = true + status.Status = "unavailable" + status.LastError = err.Error() + status.CheckedAt = time.Now() + }) + return "", "", newPicoclawError(CodeRuntimeStartFailed, err.Error()) + } + if err := ensurePicoclawStartupDefaults(); err != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = true + status.Status = "config_error" + status.ConfigError = err.Error() + status.LastError = err.Error() + status.CheckedAt = time.Now() + }) + return "", "", newPicoclawError(CodeRuntimeStartFailed, err.Error()) + } + + command := scriptPath + " start" + ctx, cancel := context.WithTimeout(context.Background(), picoclawStartTimeout) + defer cancel() + + output, execErr := exec.CommandContext(ctx, "sh", "-c", command).CombinedOutput() + trimmedOutput := strings.TrimSpace(string(output)) + if execErr != nil { + s.runtime.Update(func(status *RuntimeStatus) { + status.Ready = false + status.Installed = true + status.Status = "unavailable" + status.LastError = trimmedOutput + if status.LastError == "" { + status.LastError = execErr.Error() + } + status.CheckedAt = time.Now() + }) + return command, trimmedOutput, newPicoclawError(CodeRuntimeStartFailed, "failed to start picoclaw runtime") + } + + time.Sleep(picoclawStartWaitPeriod) + if runtimeErr := s.waitForRuntimeReady(picoclawStartTimeout); runtimeErr != nil { + startErr := newPicoclawError(CodeRuntimeStartFailed, runtimeErr.Message) + return command, trimmedOutput, startErr + } + + return command, trimmedOutput, nil +} + +func (s *Service) stopRuntime() (string, string, *PicoclawError) { + settings, _ := loadPicoclawGatewaySettings() + scriptPath, err := resolvePicoclawStartScript() + if err != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + InstallPath: picoclawBinaryPath, + ModelConfigured: settings.ModelConfigured, + ModelName: settings.ModelName, + Status: "unavailable", + LastError: err.Error(), + CheckedAt: time.Now(), + }) + return "", "", newPicoclawError(CodeRuntimeStartFailed, err.Error()) + } + + command := scriptPath + " stop" + ctx, cancel := context.WithTimeout(context.Background(), picoclawStopTimeout) + defer cancel() + + output, execErr := exec.CommandContext(ctx, "sh", "-c", command).CombinedOutput() + trimmedOutput := strings.TrimSpace(string(output)) + if execErr != nil { + status := RuntimeStatus{ + Ready: false, + Installed: true, + InstallPath: picoclawBinaryPath, + ModelConfigured: settings.ModelConfigured, + ModelName: settings.ModelName, + Status: "error", + LastError: trimmedOutput, + CheckedAt: time.Now(), + } + if trimmedOutput == "" { + status.LastError = execErr.Error() + } + s.runtime.Set(status) + return command, trimmedOutput, newPicoclawError(CodeRuntimeStartFailed, "failed to stop picoclaw runtime") + } + + time.Sleep(picoclawStopWaitPeriod) + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + InstallPath: picoclawBinaryPath, + ModelConfigured: settings.ModelConfigured, + ModelName: settings.ModelName, + Status: "stopped", + CheckedAt: time.Now(), + CurrentSession: "", + }) + + return command, trimmedOutput, nil +} + +func (s *Service) waitForRuntimeReady(timeout time.Duration) *PicoclawError { + deadline := time.Now().Add(timeout) + var lastErr *PicoclawError + + for { + if runtimeErr := s.ensureRuntimeReady(); runtimeErr == nil { + return nil + } else { + lastErr = runtimeErr + } + + if time.Now().After(deadline) { + break + } + + time.Sleep(picoclawReadyPollPeriod) + } + + if lastErr != nil { + return lastErr + } + return newPicoclawError(CodeRuntimeUnavailable, "gateway is unavailable") +} + +func resolvePicoclawStartScript() (string, error) { + if _, err := os.Stat(etcInitPicoclawScript); err == nil { + return etcInitPicoclawScript, nil + } + if _, err := os.Stat(kvmappPicoclawScript); err == nil { + return kvmappPicoclawScript, nil + } + return "", fmt.Errorf("picoclaw start script not found: %s or %s", etcInitPicoclawScript, kvmappPicoclawScript) +} + +func runPicoclawOnboard() (string, *PicoclawError) { + scriptPath, err := resolvePicoclawStartScript() + if err != nil { + return "", newPicoclawError(CodeRuntimeUnavailable, err.Error()) + } + + command := scriptPath + " onboard" + ctx, cancel := context.WithTimeout(context.Background(), picoclawOnboardTimeout) + defer cancel() + + output, execErr := exec.CommandContext(ctx, "sh", "-c", command).CombinedOutput() + trimmedOutput := strings.TrimSpace(string(output)) + if execErr != nil { + if trimmedOutput == "" { + trimmedOutput = execErr.Error() + } + return trimmedOutput, newPicoclawError(CodeRuntimeUnavailable, "failed to initialize picoclaw config") + } + + return trimmedOutput, nil +} + +func isPicoclawInstalled() (bool, error) { + info, err := os.Stat(picoclawBinaryPath) + if err == nil { + return !info.IsDir(), nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + +func isRuntimeRunning() (bool, error) { + binName := filepath.Base(picoclawBinaryPath) + if binName == "" || binName == "." || binName == string(filepath.Separator) { + return false, fmt.Errorf("invalid picoclaw binary path: %s", picoclawBinaryPath) + } + + command := exec.Command("pidof", binName) + if err := command.Run(); err != nil { + if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 { + return false, nil + } + return false, err + } + + return true, nil +} diff --git a/server/service/picoclaw/screenshot.go b/server/service/picoclaw/screenshot.go new file mode 100644 index 0000000..e54377a --- /dev/null +++ b/server/service/picoclaw/screenshot.go @@ -0,0 +1,173 @@ +package picoclaw + +import ( + "encoding/base64" + "net/http" + "time" + + "NanoKVM-Server/common" + "NanoKVM-Server/service/stream/mjpeg" + + "github.com/gin-gonic/gin" +) + +var screenshotRetryDelay = 100 * time.Millisecond + +const ( + screenshotRetryCount = 3 + cachedFrameMaxAge = 2 * time.Second + defaultPicoclawScreenshotWidth = 960 + defaultPicoclawScreenshotHeight = 540 + defaultPicoclawScreenshotQuality = 60 +) + +func (s *Service) Screenshot(c *gin.Context) { + var query ScreenshotQuery + if err := c.ShouldBindQuery(&query); err != nil { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "invalid screenshot query")) + return + } + + sessionID, sessionErr := s.requireSessionID(c) + if sessionErr != nil { + writePicoclawError(c, sessionErr) + return + } + + releaseAfter, lockErr := s.lock.AcquireTemporary(sessionID) + if lockErr != nil { + writePicoclawError(c, lockErr) + return + } + if releaseAfter { + defer s.lock.Release(sessionID) + } + + data, meta, err := s.captureScreenshot(query) + if err != nil { + writePicoclawError(c, err) + return + } + + if query.Format == "base64" { + meta.ImageBase64 = base64.StdEncoding.EncodeToString(data) + writeSuccess(c, meta) + return + } + + c.Data(http.StatusOK, "image/jpeg", data) +} + +func (s *Service) captureScreenshot(query ScreenshotQuery) ([]byte, ScreenshotMeta, *PicoclawError) { + width, height, quality := resolveScreenshotRequest(query) + + if canUseCachedFrame(query) { + if frame, ok := mjpeg.GetLatestFrame(); ok && time.Since(frame.CapturedAt) <= cachedFrameMaxAge { + return frame.Data, ScreenshotMeta{ + SourceWidth: frame.Width, + SourceHeight: frame.Height, + CaptureWidth: frame.Width, + CaptureHeight: frame.Height, + Format: "jpeg", + }, nil + } + } + + screen := common.GetScreen() + common.CheckScreen() + + for attempt := 0; attempt < screenshotRetryCount; attempt++ { + data, result := s.vision.ReadMjpeg(width, height, quality) + switch { + case result == 5: + if attempt < screenshotRetryCount-1 { + time.Sleep(screenshotRetryDelay) + continue + } + return nil, ScreenshotMeta{}, newPicoclawError(CodeScreenshotNoSignal, "no HDMI signal or frame unavailable") + case result < 0 || len(data) == 0: + return nil, ScreenshotMeta{}, newPicoclawError(CodeScreenshotFailed, "failed to capture screenshot") + default: + return data, ScreenshotMeta{ + SourceWidth: screen.Width, + SourceHeight: screen.Height, + CaptureWidth: width, + CaptureHeight: height, + Format: "jpeg", + }, nil + } + } + + return nil, ScreenshotMeta{}, newPicoclawError(CodeScreenshotFailed, "failed to capture screenshot") +} + +func canUseCachedFrame(query ScreenshotQuery) bool { + return query.Width == 0 && query.Height == 0 && query.Quality == 0 +} + +func resolveScreenshotRequest(query ScreenshotQuery) (uint16, uint16, uint16) { + screen := common.GetScreen() + width := screen.Width + height := screen.Height + quality := screen.Quality + + if query.Format == "base64" { + width, height = fitWithinBounds(width, height, defaultPicoclawScreenshotWidth, defaultPicoclawScreenshotHeight) + if quality == 0 || quality > defaultPicoclawScreenshotQuality { + quality = defaultPicoclawScreenshotQuality + } + } + + width, height = applyRequestedDimensions(width, height, query.Width, query.Height) + if query.Quality > 0 { + quality = query.Quality + } + + return width, height, quality +} + +func applyRequestedDimensions(defaultWidth uint16, defaultHeight uint16, requestedWidth uint16, requestedHeight uint16) (uint16, uint16) { + switch { + case requestedWidth > 0 && requestedHeight > 0: + return requestedWidth, requestedHeight + case requestedWidth > 0: + return fitWithinBounds(defaultWidth, defaultHeight, requestedWidth, 0) + case requestedHeight > 0: + return fitWithinBounds(defaultWidth, defaultHeight, 0, requestedHeight) + default: + return defaultWidth, defaultHeight + } +} + +func fitWithinBounds(sourceWidth uint16, sourceHeight uint16, maxWidth uint16, maxHeight uint16) (uint16, uint16) { + if sourceWidth == 0 || sourceHeight == 0 { + return sourceWidth, sourceHeight + } + if maxWidth == 0 && maxHeight == 0 { + return sourceWidth, sourceHeight + } + + width := int(sourceWidth) + height := int(sourceHeight) + limitedWidth := width + limitedHeight := height + + if maxWidth > 0 && limitedWidth > int(maxWidth) { + limitedWidth = int(maxWidth) + limitedHeight = height * limitedWidth / width + } + + if maxHeight > 0 && limitedHeight > int(maxHeight) { + limitedHeight = int(maxHeight) + limitedWidth = width * limitedHeight / height + } + + if limitedWidth <= 0 { + limitedWidth = 1 + } + if limitedHeight <= 0 { + limitedHeight = 1 + } + + return uint16(limitedWidth), uint16(limitedHeight) +} diff --git a/server/service/picoclaw/service.go b/server/service/picoclaw/service.go new file mode 100644 index 0000000..03055fa --- /dev/null +++ b/server/service/picoclaw/service.go @@ -0,0 +1,385 @@ +package picoclaw + +import ( + "net" + "net/url" + "os" + "sync" + "time" + + "NanoKVM-Server/common" + "NanoKVM-Server/service/hid" + + "github.com/gin-gonic/gin" +) + +const sessionIDHeader = "X-PicoClaw-Session-ID" + +var ( + configStoreOnce sync.Once + configStore *ConfigStore + runtimeStoreOnce sync.Once + runtimeStore *RuntimeStore + probeLoopOnce sync.Once +) + +const runtimeStatusRefreshInterval = 2 * time.Second + +func NewService() *Service { + return &Service{ + vision: common.GetKvmVision(), + hid: hid.GetHid(), + config: getConfigStore(), + lock: GetSessionLock(), + runtime: getRuntimeStore(), + } +} + +func getConfigStore() *ConfigStore { + configStoreOnce.Do(func() { + configStore = &ConfigStore{ + config: defaultConfig(), + } + }) + + return configStore +} + +func getRuntimeStore() *RuntimeStore { + runtimeStoreOnce.Do(func() { + runtimeStore = &RuntimeStore{ + status: RuntimeStatus{ + Ready: false, + Installed: false, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "checking", + }, + } + }) + + return runtimeStore +} + +func (s *Service) GetRuntimeStatus(c *gin.Context) { + s.startRuntimeProbeLoop() + status := s.runtime.Get() + if shouldRefreshRuntimeStatus(status) { + _ = s.ensureRuntimeReady() + status = s.runtime.Get() + } + + if installed, ok := picoclawInstalledState(); ok { + status.Installed = installed + s.runtime.Update(func(current *RuntimeStatus) { + current.Installed = installed + }) + } + + writeSuccess(c, withAgentProfile(status)) +} + +func (s *Service) GetRuntimeSession(c *gin.Context) { + writeSuccess(c, gin.H{ + "current_session": s.lock.Owner(), + "checked_at": time.Now(), + }) +} + +func (s *Service) requireSessionID(c *gin.Context) (string, *PicoclawError) { + sessionID := c.GetHeader(sessionIDHeader) + if sessionID == "" { + return "", newPicoclawError(CodeSessionIDMissing, "missing X-PicoClaw-Session-ID") + } + + return sessionID, nil +} + +func (s *ConfigStore) Get() Config { + s.mu.RLock() + defer s.mu.RUnlock() + return s.config +} + +func (s *ConfigStore) Set(cfg Config) { + s.mu.Lock() + defer s.mu.Unlock() + s.config = cfg +} + +func (s *RuntimeStore) Get() RuntimeStatus { + s.mu.RLock() + defer s.mu.RUnlock() + return s.status +} + +func (s *RuntimeStore) Set(status RuntimeStatus) { + s.mu.Lock() + defer s.mu.Unlock() + status.InstallPath = picoclawBinaryPath + s.status = status +} + +func (s *RuntimeStore) Update(update func(*RuntimeStatus)) { + s.mu.Lock() + defer s.mu.Unlock() + update(&s.status) + s.status.InstallPath = picoclawBinaryPath +} + +func (s *RuntimeStore) UpdateInstallStatus(update func(*RuntimeStatus)) { + s.mu.Lock() + defer s.mu.Unlock() + update(&s.status) + s.status.InstallPath = picoclawBinaryPath +} + +func (s *Service) ensureRuntimeReady() *PicoclawError { + currentStatus := s.runtime.Get() + if currentStatus.Installing { + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw installation is in progress") + } + + installed, statErr := isPicoclawInstalled() + if statErr != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: false, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + Status: "not_installed", + LastError: statErr.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "failed to check picoclaw installation") + } + if !installed { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: false, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "not_installed", + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw is not installed") + } + + configPath, pathErr := resolvePicoclawConfigPath() + if pathErr != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "model_not_configured", + LastError: pathErr.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw model is not configured") + } + if _, err := os.Stat(configPath); err != nil { + if os.IsNotExist(err) { + if _, onboardErr := runPicoclawOnboard(); onboardErr != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "model_not_configured", + LastError: onboardErr.Message, + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw model is not configured") + } + if _, statErr := os.Stat(configPath); statErr == nil { + goto configReady + } + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "model_not_configured", + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw model is not configured") + } + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + Status: "config_error", + ConfigError: err.Error(), + LastError: err.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "gateway config is invalid") + } + +configReady: + if syncErr := s.syncConfigFromPicoclaw(); syncErr != nil { + return syncErr + } + settings, settingsErr := loadPicoclawGatewaySettings() + if settingsErr != nil { + return newPicoclawError(CodeRuntimeUnavailable, settingsErr.Error()) + } + if !settings.ModelConfigured { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: false, + ModelName: settings.TargetModelName, + Status: "model_not_configured", + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "picoclaw model is not configured") + } + + cfg := s.config.Get() + + parsed, err := url.Parse(cfg.GatewayURL) + if err != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: true, + ModelName: settings.ModelName, + Status: "config_error", + ConfigError: "invalid gateway url", + LastError: err.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "gateway config is invalid") + } + if parsed.Scheme != "ws" && parsed.Scheme != "wss" { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: true, + ModelName: settings.ModelName, + Status: "config_error", + ConfigError: "invalid gateway url scheme", + LastError: parsed.Scheme, + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "gateway config is invalid") + } + + timeout := time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond + hostPort, err := gatewayHostPort(parsed) + if err != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: true, + ModelName: settings.ModelName, + Status: "config_error", + ConfigError: err.Error(), + LastError: err.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "gateway config is invalid") + } + + conn, err := net.DialTimeout("tcp", hostPort, timeout) + if err != nil { + s.runtime.Set(RuntimeStatus{ + Ready: false, + Installed: true, + Installing: false, + InstallProgress: 0, + InstallPath: picoclawBinaryPath, + ModelConfigured: true, + ModelName: settings.ModelName, + Status: "unavailable", + LastError: err.Error(), + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return newPicoclawError(CodeRuntimeUnavailable, "gateway is unavailable") + } + _ = conn.Close() + + s.runtime.Set(RuntimeStatus{ + Ready: true, + Installed: true, + Installing: false, + InstallProgress: 100, + InstallPath: picoclawBinaryPath, + ModelConfigured: true, + ModelName: settings.ModelName, + Status: "ready", + CheckedAt: time.Now(), + CurrentSession: s.lock.Owner(), + }) + return nil +} + +func (s *Service) startRuntimeProbeLoop() { + probeLoopOnce.Do(func() { + go func() { + ticker := time.NewTicker(30 * time.Second) + defer ticker.Stop() + + for range ticker.C { + _ = s.ensureRuntimeReady() + } + }() + }) +} + +func shouldRefreshRuntimeStatus(status RuntimeStatus) bool { + if status.Installing { + return false + } + if status.CheckedAt.IsZero() { + return true + } + return time.Since(status.CheckedAt) >= runtimeStatusRefreshInterval +} + +func picoclawInstalledState() (bool, bool) { + installed, err := isPicoclawInstalled() + if err != nil { + return false, false + } + return installed, true +} diff --git a/server/service/picoclaw/session_cleanup.go b/server/service/picoclaw/session_cleanup.go new file mode 100644 index 0000000..883710a --- /dev/null +++ b/server/service/picoclaw/session_cleanup.go @@ -0,0 +1,52 @@ +package picoclaw + +import ( + "os" + "path/filepath" + + "NanoKVM-Server/service/hid" + + log "github.com/sirupsen/logrus" +) + +const picoclawMediaTempDirName = "picoclaw_media" + +func ReleaseSession(sessionID string) { + GetSessionLock().Release(sessionID) + releaseAllHIDState() +} + +func releaseAllHIDState() { + h := hid.GetHid() + h.WriteHid0([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) + h.WriteHid1([]byte{0x00, 0x00, 0x00, 0x00}) + h.WriteHid2([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) +} + +func (s *Service) releaseAllHIDState() { + s.hid.WriteHid0([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) + s.hid.WriteHid1([]byte{0x00, 0x00, 0x00, 0x00}) + s.hid.WriteHid2([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) +} + +func picoclawMediaTempDir() string { + return filepath.Join(os.TempDir(), picoclawMediaTempDirName) +} + +func cleanupPicoclawMediaTempDir() { + mediaDir := picoclawMediaTempDir() + + if _, err := os.Stat(mediaDir); err != nil { + if !os.IsNotExist(err) { + log.Warnf("failed to stat picoclaw media directory %s: %v", mediaDir, err) + } + return + } + + if err := os.RemoveAll(mediaDir); err != nil { + log.Warnf("failed to remove picoclaw media directory %s: %v", mediaDir, err) + return + } + + log.Infof("removed picoclaw media directory %s", mediaDir) +} diff --git a/server/service/picoclaw/session_history.go b/server/service/picoclaw/session_history.go new file mode 100644 index 0000000..2e5ccee --- /dev/null +++ b/server/service/picoclaw/session_history.go @@ -0,0 +1,474 @@ +package picoclaw + +import ( + "bufio" + "encoding/json" + "errors" + "net/http" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "time" + + "github.com/gin-gonic/gin" +) + +const ( + picoSessionPrefix = "agent:main:pico:direct:pico:" + sanitizedPicoSessionPrefix = "agent_main_pico_direct_pico_" + maxSessionJSONLLineSize = 10 * 1024 * 1024 + maxSessionPreviewRunes = 60 + defaultSessionLimit = 20 +) + +type sessionStoredMessage struct { + Role string `json:"role"` + Content string `json:"content"` +} + +type sessionStoredFile struct { + Key string `json:"key"` + Messages []sessionStoredMessage `json:"messages"` + Summary string `json:"summary,omitempty"` + Created time.Time `json:"created"` + Updated time.Time `json:"updated"` +} + +type sessionMetaFile struct { + Key string `json:"key"` + Summary string `json:"summary"` + Skip int `json:"skip"` + Count int `json:"count"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +type sessionListItem struct { + ID string `json:"id"` + Title string `json:"title"` + Preview string `json:"preview"` + MessageCount int `json:"message_count"` + Created string `json:"created"` + Updated string `json:"updated"` +} + +type sessionDetailMessage struct { + Role string `json:"role"` + Content string `json:"content"` +} + +type sessionDetail struct { + ID string `json:"id"` + Messages []sessionDetailMessage `json:"messages"` + Summary string `json:"summary,omitempty"` + Created string `json:"created"` + Updated string `json:"updated"` +} + +func resolvePicoclawWorkspacePath() (string, error) { + configPath, err := resolvePicoclawConfigPath() + if err != nil { + return "", err + } + + if doc, err := loadPicoclawConfigDocument(); err == nil { + if workspace := expandPicoclawPath(doc.config.Agents.Defaults.Workspace); workspace != "" { + return workspace, nil + } + } + + return filepath.Join(filepath.Dir(configPath), "workspace"), nil +} + +func resolvePicoclawSessionsPath() (string, error) { + workspacePath, err := resolvePicoclawWorkspacePath() + if err != nil { + return "", err + } + + return filepath.Join(workspacePath, "sessions"), nil +} + +func sanitizeSessionKey(key string) string { + return strings.ReplaceAll(key, ":", "_") +} + +func extractPicoSessionID(key string) (string, bool) { + if strings.HasPrefix(key, picoSessionPrefix) { + return strings.TrimPrefix(key, picoSessionPrefix), true + } + return "", false +} + +func extractPicoSessionIDFromSanitizedKey(key string) (string, bool) { + if strings.HasPrefix(key, sanitizedPicoSessionPrefix) { + return strings.TrimPrefix(key, sanitizedPicoSessionPrefix), true + } + return "", false +} + +func (s *Service) ListSessions(c *gin.Context) { + dir, err := resolvePicoclawSessionsPath() + if err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to resolve sessions directory")) + return + } + + entries, err := os.ReadDir(dir) + if err != nil { + if os.IsNotExist(err) { + writeSuccess(c, []sessionListItem{}) + return + } + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to read sessions directory")) + return + } + + items := make([]sessionListItem, 0) + seen := make(map[string]struct{}) + + for _, entry := range entries { + if entry.IsDir() { + continue + } + + name := entry.Name() + var ( + sessionID string + sess sessionStoredFile + loadErr error + ok bool + ) + + switch { + case strings.HasSuffix(name, ".jsonl"): + sessionID, ok = extractPicoSessionIDFromSanitizedKey(strings.TrimSuffix(name, ".jsonl")) + if !ok { + continue + } + sess, loadErr = readJSONLSession(dir, sessionID) + if loadErr == nil && isEmptySession(sess) { + continue + } + case strings.HasSuffix(name, ".meta.json"): + continue + case strings.HasSuffix(name, ".json"): + base := strings.TrimSuffix(name, ".json") + if _, statErr := os.Stat(filepath.Join(dir, base+".jsonl")); statErr == nil { + if jsonlSessionID, found := extractPicoSessionIDFromSanitizedKey(base); found { + if jsonlSess, jsonlErr := readJSONLSession(dir, jsonlSessionID); jsonlErr == nil && !isEmptySession(jsonlSess) { + continue + } + } + } + + sess, loadErr = readLegacySession(dir, name) + if loadErr == nil && isEmptySession(sess) { + continue + } + if loadErr == nil { + sessionID, ok = extractPicoSessionID(sess.Key) + if !ok { + continue + } + } + default: + continue + } + + if loadErr != nil { + continue + } + if _, exists := seen[sessionID]; exists { + continue + } + + seen[sessionID] = struct{}{} + items = append(items, buildSessionListItem(sessionID, sess)) + } + + sort.Slice(items, func(i, j int) bool { + return items[i].Updated > items[j].Updated + }) + + offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0")) + limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(defaultSessionLimit))) + if offset < 0 { + offset = 0 + } + if limit <= 0 { + limit = defaultSessionLimit + } + + if offset >= len(items) { + writeSuccess(c, []sessionListItem{}) + return + } + + end := offset + limit + if end > len(items) { + end = len(items) + } + + writeSuccess(c, items[offset:end]) +} + +func (s *Service) GetSession(c *gin.Context) { + sessionID := strings.TrimSpace(c.Param("id")) + if sessionID == "" { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "missing session id")) + return + } + + dir, err := resolvePicoclawSessionsPath() + if err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to resolve sessions directory")) + return + } + + sess, err := readJSONLSession(dir, sessionID) + if err == nil && isEmptySession(sess) { + err = os.ErrNotExist + } + if err != nil { + if errors.Is(err, os.ErrNotExist) { + sess, err = readLegacySession(dir, sanitizeSessionKey(picoSessionPrefix+sessionID)+".json") + if err == nil && isEmptySession(sess) { + err = os.ErrNotExist + } + } + if err != nil { + sessionErr := newPicoclawError(CodeRuntimeUnavailable, "session not found") + sessionErr.StatusCode = http.StatusNotFound + writePicoclawError(c, sessionErr) + return + } + } + + messages := make([]sessionDetailMessage, 0, len(sess.Messages)) + for _, msg := range sess.Messages { + if (msg.Role == "user" || msg.Role == "assistant") && strings.TrimSpace(msg.Content) != "" { + messages = append(messages, sessionDetailMessage{ + Role: msg.Role, + Content: msg.Content, + }) + } + } + + writeSuccess(c, sessionDetail{ + ID: sessionID, + Messages: messages, + Summary: sess.Summary, + Created: sess.Created.Format(time.RFC3339), + Updated: sess.Updated.Format(time.RFC3339), + }) +} + +func (s *Service) DeleteSession(c *gin.Context) { + sessionID := strings.TrimSpace(c.Param("id")) + if sessionID == "" { + writePicoclawError(c, newPicoclawError(CodeInvalidAction, "missing session id")) + return + } + + dir, err := resolvePicoclawSessionsPath() + if err != nil { + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to resolve sessions directory")) + return + } + + base := filepath.Join(dir, sanitizeSessionKey(picoSessionPrefix+sessionID)) + paths := []string{base + ".jsonl", base + ".meta.json", base + ".json"} + removed := false + + for _, path := range paths { + if err := os.Remove(path); err != nil { + if os.IsNotExist(err) { + continue + } + writePicoclawError(c, newPicoclawError(CodeRuntimeUnavailable, "failed to delete session")) + return + } + removed = true + } + + if !removed { + sessionErr := newPicoclawError(CodeRuntimeUnavailable, "session not found") + sessionErr.StatusCode = http.StatusNotFound + writePicoclawError(c, sessionErr) + return + } + + writeSuccess(c, gin.H{ + "id": sessionID, + "deleted": true, + }) +} + +func readLegacySession(dir, fileName string) (sessionStoredFile, error) { + data, err := os.ReadFile(filepath.Join(dir, fileName)) + if err != nil { + return sessionStoredFile{}, err + } + + var sess sessionStoredFile + if err := json.Unmarshal(data, &sess); err != nil { + return sessionStoredFile{}, err + } + + return sess, nil +} + +func readSessionMeta(path, sessionKey string) (sessionMetaFile, error) { + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return sessionMetaFile{Key: sessionKey}, nil + } + if err != nil { + return sessionMetaFile{}, err + } + + var meta sessionMetaFile + if err := json.Unmarshal(data, &meta); err != nil { + return sessionMetaFile{}, err + } + if meta.Key == "" { + meta.Key = sessionKey + } + + return meta, nil +} + +func readSessionMessages(path string, skip int) ([]sessionStoredMessage, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + messages := make([]sessionStoredMessage, 0) + scanner := bufio.NewScanner(file) + scanner.Buffer(make([]byte, 0, 64*1024), maxSessionJSONLLineSize) + + seen := 0 + for scanner.Scan() { + line := scanner.Bytes() + if len(line) == 0 { + continue + } + + seen++ + if seen <= skip { + continue + } + + var msg sessionStoredMessage + if err := json.Unmarshal(line, &msg); err != nil { + continue + } + messages = append(messages, msg) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return messages, nil +} + +func readJSONLSession(dir, sessionID string) (sessionStoredFile, error) { + sessionKey := picoSessionPrefix + sessionID + base := filepath.Join(dir, sanitizeSessionKey(sessionKey)) + jsonlPath := base + ".jsonl" + metaPath := base + ".meta.json" + + meta, err := readSessionMeta(metaPath, sessionKey) + if err != nil { + return sessionStoredFile{}, err + } + + messages, err := readSessionMessages(jsonlPath, meta.Skip) + if err != nil { + return sessionStoredFile{}, err + } + + created := meta.CreatedAt + updated := meta.UpdatedAt + if created.IsZero() || updated.IsZero() { + if info, statErr := os.Stat(jsonlPath); statErr == nil { + if created.IsZero() { + created = info.ModTime() + } + if updated.IsZero() { + updated = info.ModTime() + } + } + } + + return sessionStoredFile{ + Key: meta.Key, + Messages: messages, + Summary: meta.Summary, + Created: created, + Updated: updated, + }, nil +} + +func buildSessionListItem(sessionID string, sess sessionStoredFile) sessionListItem { + preview := "" + for _, msg := range sess.Messages { + if msg.Role == "user" && strings.TrimSpace(msg.Content) != "" { + preview = msg.Content + break + } + } + + title := strings.TrimSpace(sess.Summary) + if title == "" { + title = preview + } + + title = truncateRunes(title, maxSessionPreviewRunes) + preview = truncateRunes(preview, maxSessionPreviewRunes) + if preview == "" { + preview = "(empty)" + } + if title == "" { + title = preview + } + + messageCount := 0 + for _, msg := range sess.Messages { + if (msg.Role == "user" || msg.Role == "assistant") && strings.TrimSpace(msg.Content) != "" { + messageCount++ + } + } + + return sessionListItem{ + ID: sessionID, + Title: title, + Preview: preview, + MessageCount: messageCount, + Created: sess.Created.Format(time.RFC3339), + Updated: sess.Updated.Format(time.RFC3339), + } +} + +func isEmptySession(sess sessionStoredFile) bool { + return len(sess.Messages) == 0 && strings.TrimSpace(sess.Summary) == "" +} + +func truncateRunes(s string, maxLen int) string { + if maxLen <= 0 { + return "" + } + + runes := []rune(strings.TrimSpace(s)) + if len(runes) <= maxLen { + return string(runes) + } + + return string(runes[:maxLen]) + "..." +} diff --git a/server/service/picoclaw/session_lock.go b/server/service/picoclaw/session_lock.go new file mode 100644 index 0000000..cbb5eda --- /dev/null +++ b/server/service/picoclaw/session_lock.go @@ -0,0 +1,97 @@ +package picoclaw + +import ( + "sync" + "time" +) + +var ( + sessionLockOnce sync.Once + sessionLock *SessionLock +) + +type SessionLock struct { + mu sync.Mutex + ownerSessionID string + acquiredAt time.Time + expiresAt time.Time +} + +func GetSessionLock() *SessionLock { + sessionLockOnce.Do(func() { + sessionLock = &SessionLock{} + }) + + return sessionLock +} + +func (l *SessionLock) Ensure(sessionID string) *PicoclawError { + _, err := l.acquire(sessionID) + return err +} + +func (l *SessionLock) AcquireTemporary(sessionID string) (bool, *PicoclawError) { + return l.acquire(sessionID) +} + +func (l *SessionLock) acquire(sessionID string) (bool, *PicoclawError) { + if sessionID == "" { + return false, newPicoclawError(CodeSessionIDInvalid, "invalid PicoClaw session") + } + + l.mu.Lock() + defer l.mu.Unlock() + + if l.ownerSessionID == "" || l.ownerSessionID == sessionID { + acquired := l.ownerSessionID == "" + now := time.Now() + l.ownerSessionID = sessionID + if l.acquiredAt.IsZero() { + l.acquiredAt = now + } + l.expiresAt = now.Add(30 * time.Minute) + return acquired, nil + } + + err := newPicoclawError(CodePicoclawLockHeld, "another PicoClaw session is running") + err.SessionID = l.ownerSessionID + return false, err +} + +func (l *SessionLock) Release(sessionID string) bool { + l.mu.Lock() + defer l.mu.Unlock() + + if l.ownerSessionID == "" { + return true + } + if sessionID != "" && l.ownerSessionID != sessionID { + return false + } + + l.ownerSessionID = "" + l.acquiredAt = time.Time{} + l.expiresAt = time.Time{} + return true +} + +func (l *SessionLock) ForceTakeover(sessionID string) { + l.mu.Lock() + defer l.mu.Unlock() + + l.ownerSessionID = sessionID + l.acquiredAt = time.Now() + l.expiresAt = time.Now().Add(30 * time.Minute) +} + +func (l *SessionLock) Owner() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.ownerSessionID +} + +func (l *SessionLock) BlocksManualInput() bool { + l.mu.Lock() + defer l.mu.Unlock() + return l.ownerSessionID != "" +} diff --git a/server/service/picoclaw/session_manager.go b/server/service/picoclaw/session_manager.go new file mode 100644 index 0000000..57b3ef9 --- /dev/null +++ b/server/service/picoclaw/session_manager.go @@ -0,0 +1,94 @@ +package picoclaw + +import ( + "sync" + "time" + + "github.com/gorilla/websocket" +) + +var ( + sessionManagerOnce sync.Once + sessionManager *SessionManager +) + +func GetSessionManager() *SessionManager { + sessionManagerOnce.Do(func() { + sessionManager = &SessionManager{ + sessions: make(map[string]*GatewaySession), + } + }) + + return sessionManager +} + +func (m *SessionManager) Register(sessionID string, downstream *websocket.Conn) (*GatewaySession, *PicoclawError) { + m.mu.Lock() + defer m.mu.Unlock() + + if existing, ok := m.sessions[sessionID]; ok { + switch existing.State { + case SessionStateClosing, SessionStateClosed: + delete(m.sessions, sessionID) + default: + err := newPicoclawError(CodePicoclawLockHeld, "session is already connected") + err.SessionID = sessionID + return nil, err + } + } + + now := time.Now() + session := &GatewaySession{ + SessionID: sessionID, + State: SessionStateCreated, + Downstream: downstream, + CreatedAt: now, + UpdatedAt: now, + } + m.sessions[sessionID] = session + return session, nil +} + +func (m *SessionManager) Get(sessionID string) (*GatewaySession, bool) { + m.mu.RLock() + defer m.mu.RUnlock() + + session, ok := m.sessions[sessionID] + return session, ok +} + +func (m *SessionManager) SetState(sessionID string, state SessionState) { + m.mu.Lock() + defer m.mu.Unlock() + + if session, ok := m.sessions[sessionID]; ok { + session.State = state + session.UpdatedAt = time.Now() + } +} + +func (m *SessionManager) AttachUpstream(sessionID string, upstream *websocket.Conn) { + m.mu.Lock() + defer m.mu.Unlock() + + if session, ok := m.sessions[sessionID]; ok { + session.Upstream = upstream + session.UpdatedAt = time.Now() + } +} + +func (m *SessionManager) AttachDownstream(sessionID string, downstream *websocket.Conn) { + m.mu.Lock() + defer m.mu.Unlock() + + if session, ok := m.sessions[sessionID]; ok { + session.Downstream = downstream + session.UpdatedAt = time.Now() + } +} + +func (m *SessionManager) Remove(sessionID string) { + m.mu.Lock() + defer m.mu.Unlock() + delete(m.sessions, sessionID) +} diff --git a/server/service/picoclaw/types.go b/server/service/picoclaw/types.go new file mode 100644 index 0000000..c928714 --- /dev/null +++ b/server/service/picoclaw/types.go @@ -0,0 +1,217 @@ +package picoclaw + +import ( + "encoding/json" + "reflect" + "strings" + "sync" + "time" + + "github.com/gorilla/websocket" +) + +type VisionReader interface { + ReadMjpeg(width uint16, height uint16, quality uint16) (data []byte, result int) +} + +type HIDWriter interface { + WriteHid0(data []byte) + WriteHid1(data []byte) + WriteHid2(data []byte) +} + +type Service struct { + vision VisionReader + hid HIDWriter + config *ConfigStore + lock *SessionLock + runtime *RuntimeStore +} + +type ConfigStore struct { + mu sync.RWMutex + config Config +} + +type RuntimeStore struct { + mu sync.RWMutex + status RuntimeStatus +} + +type Config struct { + GatewayURL string `json:"gateway_url"` + ConnectTimeoutMs int `json:"connect_timeout_ms"` + ReadTimeoutMs int `json:"read_timeout_ms"` + WriteTimeoutMs int `json:"write_timeout_ms"` + PingIntervalMs int `json:"ping_interval_ms"` + MaxMessageBytes int `json:"max_message_bytes"` + AllowTokenQuery bool `json:"allow_token_query"` + Token string `json:"token,omitempty"` +} + +type RuntimeStatus struct { + Ready bool `json:"ready"` + Installed bool `json:"installed"` + Installing bool `json:"installing"` + InstallProgress int `json:"install_progress,omitempty"` + InstallStage string `json:"install_stage,omitempty"` + InstallPath string `json:"install_path,omitempty"` + AgentProfile string `json:"agent_profile,omitempty"` + ModelConfigured bool `json:"model_configured"` + ModelName string `json:"model_name,omitempty"` + Status string `json:"status"` + ConfigError string `json:"config_error,omitempty"` + LastError string `json:"last_error,omitempty"` + CheckedAt time.Time `json:"checked_at,omitempty"` + CurrentSession string `json:"current_session,omitempty"` +} + +type RuntimeStartResult struct { + Started bool `json:"started"` + Command string `json:"command"` + Output string `json:"output,omitempty"` + Status RuntimeStatus `json:"status"` +} + +type RuntimeInstallResult struct { + Installed bool `json:"installed"` + Binary string `json:"binary"` + Download string `json:"download"` + Output string `json:"output,omitempty"` + Status RuntimeStatus `json:"status"` +} + +type ScreenshotMeta struct { + ImageBase64 string `json:"image_base64,omitempty"` + SourceWidth uint16 `json:"source_width"` + SourceHeight uint16 `json:"source_height"` + CaptureWidth uint16 `json:"capture_width"` + CaptureHeight uint16 `json:"capture_height"` + Format string `json:"format"` +} + +type ScreenshotQuery struct { + Format string `form:"format"` + Width uint16 `form:"width"` + Height uint16 `form:"height"` + Quality uint16 `form:"quality"` +} + +type Point struct { + X *float64 `json:"x"` + Y *float64 `json:"y"` +} + +type HotkeyKeys []string + +func (k *HotkeyKeys) UnmarshalJSON(data []byte) error { + if string(data) == "null" { + *k = nil + return nil + } + + var list []string + if err := json.Unmarshal(data, &list); err == nil { + *k = normalizeHotkeyKeys(list) + return nil + } + + var csv string + if err := json.Unmarshal(data, &csv); err == nil { + if strings.TrimSpace(csv) == "" { + *k = nil + return nil + } + *k = normalizeHotkeyKeys(strings.Split(csv, ",")) + return nil + } + + return &json.UnmarshalTypeError{Value: "keys", Type: hotkeyKeysType} +} + +var hotkeyKeysType = reflect.TypeOf("") + +func normalizeHotkeyKeys(input []string) HotkeyKeys { + keys := make([]string, 0, len(input)) + for _, item := range input { + trimmed := strings.TrimSpace(item) + if trimmed == "" { + continue + } + keys = append(keys, trimmed) + } + return HotkeyKeys(keys) +} + +type Action struct { + Action string `json:"action"` + X *float64 `json:"x"` + Y *float64 `json:"y"` + From *Point `json:"from"` + To *Point `json:"to"` + Button string `json:"button"` + Text string `json:"text"` + Keys HotkeyKeys `json:"keys"` + Direction string `json:"direction"` + Amount int `json:"amount"` + DurationMs int `json:"duration_ms"` +} + +type ActionBatch struct { + Actions []Action `json:"actions"` +} + +type ActionResult struct { + Action string `json:"action"` + DurationMs int64 `json:"duration_ms"` + HIDWrites int `json:"hid_writes"` + ExecutedActions int `json:"executed_actions,omitempty"` +} + +type cachedFrame struct { + data []byte + width uint16 + height uint16 + capturedAt time.Time +} + +type SessionState string + +const ( + SessionStateCreated SessionState = "created" + SessionStateConnecting SessionState = "connecting" + SessionStateActive SessionState = "active" + SessionStateClosing SessionState = "closing" + SessionStateClosed SessionState = "closed" +) + +const ( + CloseCodePicoclawLockHeld = 4001 + CloseCodeRuntimeUnavailable = 4002 + CloseCodeAuthFailed = 4003 + CloseCodePicoclawTakenOver = 4004 + CloseCodeUpstreamClosed = 4005 +) + +type GatewaySession struct { + SessionID string + State SessionState + Downstream *websocket.Conn + Upstream *websocket.Conn + CreatedAt time.Time + UpdatedAt time.Time + closeOnce sync.Once + upstreamWriteMu sync.Mutex + downstreamWriteMu sync.Mutex +} + +type SessionManager struct { + mu sync.RWMutex + sessions map[string]*GatewaySession +} + +type LoadImageRequest struct { + Path string `json:"path"` + Prompt string `json:"prompt"` + Filename string `json:"filename"` +} diff --git a/server/service/stream/mjpeg/mjpeg.go b/server/service/stream/mjpeg/mjpeg.go index d09f818..0d3ec80 100644 --- a/server/service/stream/mjpeg/mjpeg.go +++ b/server/service/stream/mjpeg/mjpeg.go @@ -8,6 +8,13 @@ import ( var streamer = NewStreamer() +type LatestFrame struct { + Data []byte + Width uint16 + Height uint16 + CapturedAt time.Time +} + func Connect(c *gin.Context) { c.Header("Content-Type", "multipart/x-mixed-replace; boundary=frame") c.Header("Cache-Control", "no-cache") @@ -20,3 +27,15 @@ func Connect(c *gin.Context) { <-c.Request.Context().Done() } + +func GetLatestFrame() (LatestFrame, bool) { + return streamer.getLatestFrame() +} + +func EnableLatestFrameCache() { + streamer.enableLatestFrameCache() +} + +func DisableLatestFrameCache() { + streamer.disableLatestFrameCache() +} diff --git a/server/service/stream/mjpeg/streamer.go b/server/service/stream/mjpeg/streamer.go index 5bddc20..72e410f 100644 --- a/server/service/stream/mjpeg/streamer.go +++ b/server/service/stream/mjpeg/streamer.go @@ -14,9 +14,12 @@ import ( ) type Streamer struct { - mutex sync.RWMutex - clients map[*gin.Context]bool - running int32 + mutex sync.RWMutex + clients map[*gin.Context]bool + running int32 + frameMutex sync.RWMutex + latestFrame LatestFrame + cacheRefs int32 } func NewStreamer() *Streamer { @@ -86,6 +89,10 @@ func (s *Streamer) run() { continue } + if s.frameCacheEnabled() { + s.setLatestFrame(data, screen.Width, screen.Height) + } + clients := s.getClients() for _, client := range clients { if err := writeFrame(client, data); err != nil { @@ -103,6 +110,71 @@ func (s *Streamer) run() { } } +func (s *Streamer) setLatestFrame(data []byte, width uint16, height uint16) { + frameCopy := append([]byte(nil), data...) + + s.frameMutex.Lock() + defer s.frameMutex.Unlock() + + s.latestFrame = LatestFrame{ + Data: frameCopy, + Width: width, + Height: height, + CapturedAt: time.Now(), + } +} + +func (s *Streamer) clearLatestFrame() { + s.frameMutex.Lock() + defer s.frameMutex.Unlock() + + s.latestFrame = LatestFrame{} +} + +func (s *Streamer) enableLatestFrameCache() { + atomic.AddInt32(&s.cacheRefs, 1) +} + +func (s *Streamer) disableLatestFrameCache() { + for { + current := atomic.LoadInt32(&s.cacheRefs) + if current <= 0 { + return + } + + if atomic.CompareAndSwapInt32(&s.cacheRefs, current, current-1) { + if current == 1 { + s.clearLatestFrame() + } + return + } + } +} + +func (s *Streamer) frameCacheEnabled() bool { + return atomic.LoadInt32(&s.cacheRefs) > 0 +} + +func (s *Streamer) getLatestFrame() (LatestFrame, bool) { + if !s.frameCacheEnabled() { + return LatestFrame{}, false + } + + s.frameMutex.RLock() + defer s.frameMutex.RUnlock() + + if len(s.latestFrame.Data) == 0 { + return LatestFrame{}, false + } + + return LatestFrame{ + Data: append([]byte(nil), s.latestFrame.Data...), + Width: s.latestFrame.Width, + Height: s.latestFrame.Height, + CapturedAt: s.latestFrame.CapturedAt, + }, true +} + func writeFrame(c *gin.Context, data []byte) (err error) { defer func() { if r := recover(); r != nil { diff --git a/server/service/ws/client.go b/server/service/ws/client.go index 082d120..4256d0c 100644 --- a/server/service/ws/client.go +++ b/server/service/ws/client.go @@ -5,6 +5,7 @@ import ( "time" "NanoKVM-Server/service/hid" + "NanoKVM-Server/service/picoclaw" "NanoKVM-Server/service/vm/jiggler" "github.com/gorilla/websocket" @@ -50,14 +51,26 @@ func (c *Client) Read() error { return err } + if len(data) == 0 { + continue + } + log.Debugf("received message %d: %v", messageType, data) switch data[0] { case Heartbeat: c.UpdateHeartbeat() case KeyboardEvent: + if picoclaw.GetSessionLock().BlocksManualInput() { + log.Debug("manual keyboard input dropped while AI session holds control") + continue + } writeQueue(c.keyboard, data[1:]) case MouseEvent: + if picoclaw.GetSessionLock().BlocksManualInput() { + log.Debug("manual mouse input dropped while AI session holds control") + continue + } writeQueue(c.mouse, data[1:]) } } diff --git a/web/package.json b/web/package.json index 37e4730..4c0fd20 100644 --- a/web/package.json +++ b/web/package.json @@ -31,9 +31,11 @@ "react-error-boundary": "^4.1.2", "react-helmet-async": "^2.0.5", "react-i18next": "^14.1.3", + "react-markdown": "^10.1.0", "react-responsive": "^10.0.1", "react-router-dom": "^6.30.3", "react-simple-keyboard": "^3.8.164", + "remark-gfm": "^4.0.1", "semver": "^7.7.4", "vaul": "^0.9.9", "websocket": "^1.0.35", diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index 4e546e3..f0af25e 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -65,6 +65,9 @@ importers: react-i18next: specifier: ^14.1.3 version: 14.1.3(i18next@23.16.8)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + react-markdown: + specifier: ^10.1.0 + version: 10.1.0(@types/react@18.3.28)(react@18.3.1) react-responsive: specifier: ^10.0.1 version: 10.0.1(react@18.3.1) @@ -74,6 +77,9 @@ importers: react-simple-keyboard: specifier: ^3.8.164 version: 3.8.164(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + remark-gfm: + specifier: ^4.0.1 + version: 4.0.1 semver: specifier: ^7.7.4 version: 7.7.4 @@ -1004,15 +1010,30 @@ packages: '@types/crypto-js@4.2.2': resolution: {integrity: sha512-sDOLlVbHhXpAUAL0YHDUUwDZf3iN4Bwi4W6a0W0b+QcAezUbRtH4FVb+9J4h+XFPW7l/gQ9F8qC7P+Ec4k8QVQ==} + '@types/debug@4.1.13': + resolution: {integrity: sha512-KSVgmQmzMwPlmtljOomayoR89W4FynCAi3E8PPs7vmDVPe84hT+vGPKkJfThkmXs0x0jAaa9U8uW8bbfyS2fWw==} + + '@types/estree-jsx@1.0.5': + resolution: {integrity: sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg==} + '@types/estree@1.0.8': resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==} + '@types/hast@3.0.4': + resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==} + '@types/js-cookie@3.0.6': resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==} '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} + '@types/mdast@4.0.4': + resolution: {integrity: sha512-kGaNbPh1k7AFzgpud/gMdvIm5xuECykRR+JnWKQno9TAXVa6WIVCGTPvYGekIDL4uwCZQSYbUxNBSb1aUo79oA==} + + '@types/ms@2.1.0': + resolution: {integrity: sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA==} + '@types/node@25.3.2': resolution: {integrity: sha512-RpV6r/ij22zRRdyBPcxDeKAzH43phWVKEjL2iksqo1Vz3CuBUrgmPpPhALKiRfU7OMCmeeO9vECBMsV0hMTG8Q==} @@ -1033,6 +1054,12 @@ packages: '@types/statuses@2.0.6': resolution: {integrity: sha512-xMAgYwceFhRA2zY+XbEA7mxYbA093wdiW8Vu6gZPGWy9cmOyU9XesH1tNcEWsKFd5Vzrqx5T3D38PWx1FIIXkA==} + '@types/unist@2.0.11': + resolution: {integrity: sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA==} + + '@types/unist@3.0.3': + resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==} + '@types/websocket@1.0.10': resolution: {integrity: sha512-svjGZvPB7EzuYS94cI7a+qhwgGU1y89wUgjT6E2wVUfmAGIvRfT7obBvRtnhXCSsoMdlG4gBFGE7MfkIXZLoww==} @@ -1095,6 +1122,9 @@ packages: resolution: {integrity: sha512-KiROIzYdEV85YygXw6BI/Dx4fnBlFQu6Mq4QE4MOH9fFnhohw6wX/OAvDY2/C+ut0I3RSPKenvZJIVYqJNkhEw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + '@ungap/structured-clone@1.3.0': + resolution: {integrity: sha512-WmoN8qaIAo7WTYWbAZuG8PYEhn5fkz7dZrqTBZ7dtt//lL2Gwms1IcnQ5yHqjDfX8Ft5j4YzDM23f87zBfDe9g==} + '@vitejs/plugin-react@4.7.0': resolution: {integrity: sha512-gUu9hwfWvvEDBBmgtAowQCojwZmJ5mcLn3aufeCsitijs3+f2NsrPtlAWIR6OPiqljl96GVCUbLe0HyqIpVaoA==} engines: {node: ^14.18.0 || >=16.0.0} @@ -1207,6 +1237,9 @@ packages: axios@1.13.6: resolution: {integrity: sha512-ChTCHMouEe2kn713WHbQGcuYrr6fXTBiu460OTwWrWob16g1bXn4vtz07Ope7ewMozJAnEquLk5lWQWtBig9DQ==} + bail@2.0.2: + resolution: {integrity: sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw==} + balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} @@ -1266,10 +1299,25 @@ packages: caniuse-lite@1.0.30001774: resolution: {integrity: sha512-DDdwPGz99nmIEv216hKSgLD+D4ikHQHjBC/seF98N9CPqRX4M5mSxT9eTV6oyisnJcuzxtZy4n17yKKQYmYQOA==} + ccount@2.0.1: + resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==} + chalk@4.1.2: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} + character-entities-html4@2.1.0: + resolution: {integrity: sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA==} + + character-entities-legacy@3.0.0: + resolution: {integrity: sha512-RpPp0asT/6ufRm//AJVwpViZbGM/MkjQFxJccQRHmISF/22NBtsHqAWmL+/pmkPWoIUJdWyeVleTl1wydHATVQ==} + + character-entities@2.0.2: + resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==} + + character-reference-invalid@2.0.1: + resolution: {integrity: sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw==} + chokidar@3.6.0: resolution: {integrity: sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==} engines: {node: '>= 8.10.0'} @@ -1300,6 +1348,9 @@ packages: resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} engines: {node: '>= 0.8'} + comma-separated-tokens@2.0.3: + resolution: {integrity: sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==} + commander@4.1.1: resolution: {integrity: sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==} engines: {node: '>= 6'} @@ -1374,6 +1425,9 @@ packages: supports-color: optional: true + decode-named-character-reference@1.3.0: + resolution: {integrity: sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q==} + deep-is@0.1.4: resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==} @@ -1389,9 +1443,16 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} + dequal@2.0.3: + resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==} + engines: {node: '>=6'} + detect-node-es@1.1.0: resolution: {integrity: sha512-ypdmJU/TbBby2Dxibuv7ZLW3Bs1QEmM7nHjEANfohJLvE0XVujisn1qPJcZxg+qDucsr+bP6fLD1rPS3AhJ7EQ==} + devlop@1.1.0: + resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==} + didyoumean@1.2.2: resolution: {integrity: sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==} @@ -1468,6 +1529,10 @@ packages: resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==} engines: {node: '>=10'} + escape-string-regexp@5.0.0: + resolution: {integrity: sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==} + engines: {node: '>=12'} + eslint-config-prettier@9.1.2: resolution: {integrity: sha512-iI1f+D2ViGn+uvv5HuHVUamg8ll4tN+JRHGc6IJi4TP9Kl976C57fzPXgseXNs8v0iA8aSJpHsTWjDb9QJamGQ==} hasBin: true @@ -1537,6 +1602,9 @@ packages: resolution: {integrity: sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==} engines: {node: '>=4.0'} + estree-util-is-identifier-name@3.0.0: + resolution: {integrity: sha512-hFtqIDZTIUZ9BXLb8y4pYGyk6+wekIivNVTcmvk8NoOh+VeRn5y6cEHzbURrWbfp1fIqdVipilzj+lfaadNZmg==} + esutils@2.0.3: resolution: {integrity: sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==} engines: {node: '>=0.10.0'} @@ -1547,6 +1615,9 @@ packages: ext@1.7.0: resolution: {integrity: sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==} + extend@3.0.2: + resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==} + fast-deep-equal@3.1.3: resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} @@ -1712,12 +1783,21 @@ packages: resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==} engines: {node: '>= 0.4'} + hast-util-to-jsx-runtime@2.3.6: + resolution: {integrity: sha512-zl6s8LwNyo1P9uw+XJGvZtdFF1GdAkOg8ujOw+4Pyb76874fLps4ueHXDhXWdk6YHQ6OgUtinliG7RsYvCbbBg==} + + hast-util-whitespace@3.0.0: + resolution: {integrity: sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw==} + headers-polyfill@4.0.3: resolution: {integrity: sha512-IScLbePpkvO846sIwOtOTDjutRMWdXdJmXdMvk6gCBHxFO8d+QKOQedyZSxFTTFYRSmlgSTDtXqqq4pcenBXLQ==} html-parse-stringify@3.0.1: resolution: {integrity: sha512-KknJ50kTInJ7qIScF3jeaFRpMpE8/lfiTdzf/twXyPBLAGrLRTmkz3AdTnKeh40X8k9L2fdYwEp/42WGXIRGcg==} + html-url-attributes@3.0.1: + resolution: {integrity: sha512-ol6UPyBWqsrO6EJySPz2O7ZSr856WDrEzM5zMqp+FJJLGMW35cLYmmZnl0vztAZxRUoNZJFTCohfjuIJ8I4QBQ==} + hyphenate-style-name@1.1.0: resolution: {integrity: sha512-WDC/ui2VVRrz3jOVi+XtjqkDjiVjTtFaAGiW37k6b+ohyQ5wYDOGkvCZa8+H0nx3gyvv0+BST9xuOgIyGQ00gw==} @@ -1740,6 +1820,9 @@ packages: resolution: {integrity: sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==} engines: {node: '>=0.8.19'} + inline-style-parser@0.2.7: + resolution: {integrity: sha512-Nb2ctOyNR8DqQoR0OwRG95uNWIC0C1lCgf5Naz5H6Ji72KZ8OcFZLz2P5sNgwlyoJ8Yif11oMuYs5pBQa86csA==} + internal-slot@1.1.0: resolution: {integrity: sha512-4gd7VpWNQNB4UKKCFFVcp1AVv+FMOgs9NKzjHKusc8jTMhd5eL1NqQqOpE0KzMds804/yHlglp3uxgluOqAPLw==} engines: {node: '>= 0.4'} @@ -1747,6 +1830,12 @@ packages: invariant@2.2.4: resolution: {integrity: sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==} + is-alphabetical@2.0.1: + resolution: {integrity: sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ==} + + is-alphanumerical@2.0.1: + resolution: {integrity: sha512-hmbYhX/9MUMF5uh7tOXyK/n0ZvWpad5caBA17GsC6vyuCqaWliRG5K1qS9inmUhEMaOBIW7/whAnSwveW/LtZw==} + is-array-buffer@3.0.5: resolution: {integrity: sha512-DDfANUiiG2wC1qawP66qlTugJeL5HyzMpfr8lLK+jMQirGzNod0B12cFB/9q838Ru27sBwfw78/rdoU7RERz6A==} engines: {node: '>= 0.4'} @@ -1783,6 +1872,9 @@ packages: resolution: {integrity: sha512-PwwhEakHVKTdRNVOw+/Gyh0+MzlCl4R6qKvkhuvLtPMggI1WAHt9sOwZxQLSGpUaDnrdyDsomoRgNnCfKNSXXg==} engines: {node: '>= 0.4'} + is-decimal@2.0.1: + resolution: {integrity: sha512-AAB9hiomQs5DXWcRB1rqsxGUstbRroFOPPVAomNk/3XHR5JyEZChOyTWe2oayKnsSsr/kcGqF+z6yuH6HHpN0A==} + is-extglob@2.1.1: resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==} engines: {node: '>=0.10.0'} @@ -1803,6 +1895,9 @@ packages: resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==} engines: {node: '>=0.10.0'} + is-hexadecimal@2.0.1: + resolution: {integrity: sha512-DgZQp241c8oO6cA1SbTEWiXeoxV42vlcJxgH+B3hi1AiqqKruZR3ZGF8In3fj4+/y/7rHvlOZLZtgJ/4ttYGZg==} + is-map@2.0.3: resolution: {integrity: sha512-1Qed0/Hr2m+YqxnM09CjA2d/i6YZNfF6R2oRAOj36eUdS6qIV/huPJNSEpKbupewFs+ZsJlxsjjPbc0/afW6Lw==} engines: {node: '>= 0.4'} @@ -1822,6 +1917,10 @@ packages: resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==} engines: {node: '>=0.12.0'} + is-plain-obj@4.1.0: + resolution: {integrity: sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==} + engines: {node: '>=12'} + is-regex@1.2.1: resolution: {integrity: sha512-MjYsKHO5O7mCsmRGxWcLWheFqN9DJ/2TmngvjKXihe6efViPqc274+Fx/4fYj/r03+ESvBdTXK0V6tA3rgez1g==} engines: {node: '>= 0.4'} @@ -1951,6 +2050,9 @@ packages: lodash.merge@4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} + longest-streak@3.1.0: + resolution: {integrity: sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g==} + loose-envify@1.4.0: resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==} hasBin: true @@ -1963,6 +2065,9 @@ packages: peerDependencies: react: ^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0 + markdown-table@3.0.4: + resolution: {integrity: sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==} + matchmediaquery@0.4.2: resolution: {integrity: sha512-wrZpoT50ehYOudhDjt/YvUJc6eUzcdFPdmbizfgvswCKNHD1/OBOHYJpHie+HXpu6bSkEGieFMYk6VuutaiRfA==} @@ -1970,10 +2075,139 @@ packages: resolution: {integrity: sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==} engines: {node: '>= 0.4'} + mdast-util-find-and-replace@3.0.2: + resolution: {integrity: sha512-Tmd1Vg/m3Xz43afeNxDIhWRtFZgM2VLyaf4vSTYwudTyeuTneoL3qtWMA5jeLyz/O1vDJmmV4QuScFCA2tBPwg==} + + mdast-util-from-markdown@2.0.3: + resolution: {integrity: sha512-W4mAWTvSlKvf8L6J+VN9yLSqQ9AOAAvHuoDAmPkz4dHf553m5gVj2ejadHJhoJmcmxEnOv6Pa8XJhpxE93kb8Q==} + + mdast-util-gfm-autolink-literal@2.0.1: + resolution: {integrity: sha512-5HVP2MKaP6L+G6YaxPNjuL0BPrq9orG3TsrZ9YXbA3vDw/ACI4MEsnoDpn6ZNm7GnZgtAcONJyPhOP8tNJQavQ==} + + mdast-util-gfm-footnote@2.1.0: + resolution: {integrity: sha512-sqpDWlsHn7Ac9GNZQMeUzPQSMzR6Wv0WKRNvQRg0KqHh02fpTz69Qc1QSseNX29bhz1ROIyNyxExfawVKTm1GQ==} + + mdast-util-gfm-strikethrough@2.0.0: + resolution: {integrity: sha512-mKKb915TF+OC5ptj5bJ7WFRPdYtuHv0yTRxK2tJvi+BDqbkiG7h7u/9SI89nRAYcmap2xHQL9D+QG/6wSrTtXg==} + + mdast-util-gfm-table@2.0.0: + resolution: {integrity: sha512-78UEvebzz/rJIxLvE7ZtDd/vIQ0RHv+3Mh5DR96p7cS7HsBhYIICDBCu8csTNWNO6tBWfqXPWekRuj2FNOGOZg==} + + mdast-util-gfm-task-list-item@2.0.0: + resolution: {integrity: sha512-IrtvNvjxC1o06taBAVJznEnkiHxLFTzgonUdy8hzFVeDun0uTjxxrRGVaNFqkU1wJR3RBPEfsxmU6jDWPofrTQ==} + + mdast-util-gfm@3.1.0: + resolution: {integrity: sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==} + + mdast-util-mdx-expression@2.0.1: + resolution: {integrity: sha512-J6f+9hUp+ldTZqKRSg7Vw5V6MqjATc+3E4gf3CFNcuZNWD8XdyI6zQ8GqH7f8169MM6P7hMBRDVGnn7oHB9kXQ==} + + mdast-util-mdx-jsx@3.2.0: + resolution: {integrity: sha512-lj/z8v0r6ZtsN/cGNNtemmmfoLAFZnjMbNyLzBafjzikOM+glrjNHPlf6lQDOTccj9n5b0PPihEBbhneMyGs1Q==} + + mdast-util-mdxjs-esm@2.0.1: + resolution: {integrity: sha512-EcmOpxsZ96CvlP03NghtH1EsLtr0n9Tm4lPUJUBccV9RwUOneqSycg19n5HGzCf+10LozMRSObtVr3ee1WoHtg==} + + mdast-util-phrasing@4.1.0: + resolution: {integrity: sha512-TqICwyvJJpBwvGAMZjj4J2n0X8QWp21b9l0o7eXyVJ25YNWYbJDVIyD1bZXE6WtV6RmKJVYmQAKWa0zWOABz2w==} + + mdast-util-to-hast@13.2.1: + resolution: {integrity: sha512-cctsq2wp5vTsLIcaymblUriiTcZd0CwWtCbLvrOzYCDZoWyMNV8sZ7krj09FSnsiJi3WVsHLM4k6Dq/yaPyCXA==} + + mdast-util-to-markdown@2.1.2: + resolution: {integrity: sha512-xj68wMTvGXVOKonmog6LwyJKrYXZPvlwabaryTjLh9LuvovB/KAH+kvi8Gjj+7rJjsFi23nkUxRQv1KqSroMqA==} + + mdast-util-to-string@4.0.0: + resolution: {integrity: sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg==} + merge2@1.4.1: resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==} engines: {node: '>= 8'} + micromark-core-commonmark@2.0.3: + resolution: {integrity: sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg==} + + micromark-extension-gfm-autolink-literal@2.1.0: + resolution: {integrity: sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==} + + micromark-extension-gfm-footnote@2.1.0: + resolution: {integrity: sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==} + + micromark-extension-gfm-strikethrough@2.1.0: + resolution: {integrity: sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==} + + micromark-extension-gfm-table@2.1.1: + resolution: {integrity: sha512-t2OU/dXXioARrC6yWfJ4hqB7rct14e8f7m0cbI5hUmDyyIlwv5vEtooptH8INkbLzOatzKuVbQmAYcbWoyz6Dg==} + + micromark-extension-gfm-tagfilter@2.0.0: + resolution: {integrity: sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==} + + micromark-extension-gfm-task-list-item@2.1.0: + resolution: {integrity: sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==} + + micromark-extension-gfm@3.0.0: + resolution: {integrity: sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==} + + micromark-factory-destination@2.0.1: + resolution: {integrity: sha512-Xe6rDdJlkmbFRExpTOmRj9N3MaWmbAgdpSrBQvCFqhezUn4AHqJHbaEnfbVYYiexVSs//tqOdY/DxhjdCiJnIA==} + + micromark-factory-label@2.0.1: + resolution: {integrity: sha512-VFMekyQExqIW7xIChcXn4ok29YE3rnuyveW3wZQWWqF4Nv9Wk5rgJ99KzPvHjkmPXF93FXIbBp6YdW3t71/7Vg==} + + micromark-factory-space@2.0.1: + resolution: {integrity: sha512-zRkxjtBxxLd2Sc0d+fbnEunsTj46SWXgXciZmHq0kDYGnck/ZSGj9/wULTV95uoeYiK5hRXP2mJ98Uo4cq/LQg==} + + micromark-factory-title@2.0.1: + resolution: {integrity: sha512-5bZ+3CjhAd9eChYTHsjy6TGxpOFSKgKKJPJxr293jTbfry2KDoWkhBb6TcPVB4NmzaPhMs1Frm9AZH7OD4Cjzw==} + + micromark-factory-whitespace@2.0.1: + resolution: {integrity: sha512-Ob0nuZ3PKt/n0hORHyvoD9uZhr+Za8sFoP+OnMcnWK5lngSzALgQYKMr9RJVOWLqQYuyn6ulqGWSXdwf6F80lQ==} + + micromark-util-character@2.1.1: + resolution: {integrity: sha512-wv8tdUTJ3thSFFFJKtpYKOYiGP2+v96Hvk4Tu8KpCAsTMs6yi+nVmGh1syvSCsaxz45J6Jbw+9DD6g97+NV67Q==} + + micromark-util-chunked@2.0.1: + resolution: {integrity: sha512-QUNFEOPELfmvv+4xiNg2sRYeS/P84pTW0TCgP5zc9FpXetHY0ab7SxKyAQCNCc1eK0459uoLI1y5oO5Vc1dbhA==} + + micromark-util-classify-character@2.0.1: + resolution: {integrity: sha512-K0kHzM6afW/MbeWYWLjoHQv1sgg2Q9EccHEDzSkxiP/EaagNzCm7T/WMKZ3rjMbvIpvBiZgwR3dKMygtA4mG1Q==} + + micromark-util-combine-extensions@2.0.1: + resolution: {integrity: sha512-OnAnH8Ujmy59JcyZw8JSbK9cGpdVY44NKgSM7E9Eh7DiLS2E9RNQf0dONaGDzEG9yjEl5hcqeIsj4hfRkLH/Bg==} + + micromark-util-decode-numeric-character-reference@2.0.2: + resolution: {integrity: sha512-ccUbYk6CwVdkmCQMyr64dXz42EfHGkPQlBj5p7YVGzq8I7CtjXZJrubAYezf7Rp+bjPseiROqe7G6foFd+lEuw==} + + micromark-util-decode-string@2.0.1: + resolution: {integrity: sha512-nDV/77Fj6eH1ynwscYTOsbK7rR//Uj0bZXBwJZRfaLEJ1iGBR6kIfNmlNqaqJf649EP0F3NWNdeJi03elllNUQ==} + + micromark-util-encode@2.0.1: + resolution: {integrity: sha512-c3cVx2y4KqUnwopcO9b/SCdo2O67LwJJ/UyqGfbigahfegL9myoEFoDYZgkT7f36T0bLrM9hZTAaAyH+PCAXjw==} + + micromark-util-html-tag-name@2.0.1: + resolution: {integrity: sha512-2cNEiYDhCWKI+Gs9T0Tiysk136SnR13hhO8yW6BGNyhOC4qYFnwF1nKfD3HFAIXA5c45RrIG1ub11GiXeYd1xA==} + + micromark-util-normalize-identifier@2.0.1: + resolution: {integrity: sha512-sxPqmo70LyARJs0w2UclACPUUEqltCkJ6PhKdMIDuJ3gSf/Q+/GIe3WKl0Ijb/GyH9lOpUkRAO2wp0GVkLvS9Q==} + + micromark-util-resolve-all@2.0.1: + resolution: {integrity: sha512-VdQyxFWFT2/FGJgwQnJYbe1jjQoNTS4RjglmSjTUlpUMa95Htx9NHeYW4rGDJzbjvCsl9eLjMQwGeElsqmzcHg==} + + micromark-util-sanitize-uri@2.0.1: + resolution: {integrity: sha512-9N9IomZ/YuGGZZmQec1MbgxtlgougxTodVwDzzEouPKo3qFWvymFHWcnDi2vzV1ff6kas9ucW+o3yzJK9YB1AQ==} + + micromark-util-subtokenize@2.1.0: + resolution: {integrity: sha512-XQLu552iSctvnEcgXw6+Sx75GflAPNED1qx7eBJ+wydBb2KCbRZe+NwvIEEMM83uml1+2WSXpBAcp9IUCgCYWA==} + + micromark-util-symbol@2.0.1: + resolution: {integrity: sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q==} + + micromark-util-types@2.0.2: + resolution: {integrity: sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA==} + + micromark@4.0.2: + resolution: {integrity: sha512-zpe98Q6kvavpCr1NPVSCMebCKfD7CA2NqZ+rykeNhONIJBpc1tFKt9hucLGwha3jNTNI8lHpctWJWoimVF4PfA==} + micromatch@4.0.8: resolution: {integrity: sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==} engines: {node: '>=8.6'} @@ -2097,6 +2331,9 @@ packages: resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==} engines: {node: '>=6'} + parse-entities@4.0.2: + resolution: {integrity: sha512-GG2AQYWoLgL877gQIKeRPGO1xF9+eG1ujIb5soS5gPvLQ1y2o8FL90w2QWNdf9I361Mpp7726c+lj3U0qK1uGw==} + path-exists@4.0.0: resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==} engines: {node: '>=8'} @@ -2254,6 +2491,9 @@ packages: prop-types@15.8.1: resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==} + property-information@7.1.0: + resolution: {integrity: sha512-TwEZ+X+yCJmYfL7TPUOcvBZ4QfoT5YenQiJuX//0th53DE6w0xxLEtfK3iyryQFddXuvkIk51EEgrJQ0WJkOmQ==} + proxy-from-env@1.1.0: resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} @@ -2535,6 +2775,12 @@ packages: react-is@18.3.1: resolution: {integrity: sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==} + react-markdown@10.1.0: + resolution: {integrity: sha512-qKxVopLT/TyA6BX3Ue5NwabOsAzm0Q7kAPwq6L+wWDwisYs7R8vZ0nRXqq6rkueboxpkjvLGU9fWifiX/ZZFxQ==} + peerDependencies: + '@types/react': '>=18' + react: '>=18' + react-refresh@0.17.0: resolution: {integrity: sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==} engines: {node: '>=0.10.0'} @@ -2613,6 +2859,18 @@ packages: resolution: {integrity: sha512-dYqgNSZbDwkaJ2ceRd9ojCGjBq+mOm9LmtXnAnEGyHhN/5R7iDW2TRw3h+o/jCFxus3P2LfWIIiwowAjANm7IA==} engines: {node: '>= 0.4'} + remark-gfm@4.0.1: + resolution: {integrity: sha512-1quofZ2RQ9EWdeN34S79+KExV1764+wCUGop5CPL1WGdD0ocPpu91lzPGbwWMECpEpd42kJGQwzRfyov9j4yNg==} + + remark-parse@11.0.0: + resolution: {integrity: sha512-FCxlKLNGknS5ba/1lmpYijMUzX2esxW5xQqjWxw2eHFfS2MSdaHVINFmhjo+qN1WhZhNimq0dZATN9pH0IDrpA==} + + remark-rehype@11.1.2: + resolution: {integrity: sha512-Dh7l57ianaEoIpzbp0PC9UKAdCSVklD8E5Rpw7ETfbTl3FqcOOgq5q2LVDhgGCkaBv7p24JXikPdvhhmHvKMsw==} + + remark-stringify@11.0.0: + resolution: {integrity: sha512-1OSmLd3awB/t8qdoEOMazZkNsfVTeY4fTsgzcQFdXNq8ToTN4ZGwrMnlda4K6smTFKD+GRV6O48i6Z4iKgPPpw==} + require-directory@2.1.1: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'} @@ -2726,6 +2984,9 @@ packages: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} + space-separated-tokens@2.0.2: + resolution: {integrity: sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q==} + statuses@2.0.2: resolution: {integrity: sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==} engines: {node: '>= 0.8'} @@ -2763,6 +3024,9 @@ packages: resolution: {integrity: sha512-UXSH262CSZY1tfu3G3Secr6uGLCFVPMhIqHjlgCUtCCcgihYc/xKs9djMTMUOb2j1mVSeU8EU6NWc/iQKU6Gfg==} engines: {node: '>= 0.4'} + stringify-entities@4.0.4: + resolution: {integrity: sha512-IwfBptatlO+QCJUo19AqvrPNqlVMpW9YEL2LIVY+Rpv2qsjCGxaDLNRgeGsQWJhfItebuJhsGSLjaBbNSQ+ieg==} + strip-ansi@6.0.1: resolution: {integrity: sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==} engines: {node: '>=8'} @@ -2771,6 +3035,12 @@ packages: resolution: {integrity: sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==} engines: {node: '>=8'} + style-to-js@1.1.21: + resolution: {integrity: sha512-RjQetxJrrUJLQPHbLku6U/ocGtzyjbJMP9lCNK7Ag0CNh690nSH8woqWH9u16nMjYBAok+i7JO1NP2pOy8IsPQ==} + + style-to-object@1.0.14: + resolution: {integrity: sha512-LIN7rULI0jBscWQYaSswptyderlarFkjQ+t79nzty8tcIAceVomEVlLzH5VP4Cmsv6MtKhs7qaAiwlcp+Mgaxw==} + stylis@4.3.6: resolution: {integrity: sha512-yQ3rwFWRfwNUY7H5vpU0wfdkNSnvnJinhF9830Swlaxl03zsOjCfmX0ugac+3LtK0lYSgwL/KXc8oYL3mG4YFQ==} @@ -2829,6 +3099,12 @@ packages: resolution: {integrity: sha512-kXuRi1mtaKMrsLUxz3sQYvVl37B0Ns6MzfrtV5DvJceE9bPyspOqk9xxv7XbZWcfLWbFmm997vl83qUWVJA64w==} engines: {node: '>=16'} + trim-lines@3.0.1: + resolution: {integrity: sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg==} + + trough@2.2.0: + resolution: {integrity: sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==} + ts-api-utils@2.4.0: resolution: {integrity: sha512-3TaVTaAv2gTiMB35i3FiGJaRfwb3Pyn/j3m/bfAvGe8FB7CF6u+LMYqYlDh7reQf7UNvoTvdfAqHGmPGOSsPmA==} engines: {node: '>=18.12'} @@ -2900,6 +3176,24 @@ packages: undici-types@7.18.2: resolution: {integrity: sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==} + unified@11.0.5: + resolution: {integrity: sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA==} + + unist-util-is@6.0.1: + resolution: {integrity: sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g==} + + unist-util-position@5.0.0: + resolution: {integrity: sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA==} + + unist-util-stringify-position@4.0.0: + resolution: {integrity: sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==} + + unist-util-visit-parents@6.0.2: + resolution: {integrity: sha512-goh1s1TBrqSqukSc8wrjwWhL0hiJxgA8m4kFxGlQ+8FYQ3C/m11FcTs4YYem7V664AhHVvgoQLk890Ssdsr2IQ==} + + unist-util-visit@5.1.0: + resolution: {integrity: sha512-m+vIdyeCOpdr/QeQCu2EzxX/ohgS8KbnPDgFni4dQsfSCtpz8UqDyY5GjRru8PDKuYn7Fq19j1CQ+nJSsGKOzg==} + until-async@3.0.2: resolution: {integrity: sha512-IiSk4HlzAMqTUseHHe3VhIGyuFmN90zMTpD3Z3y8jeQbzLIq500MVM7Jq2vUAnTKAFPJrqwkzr6PoTcPhGcOiw==} @@ -2945,6 +3239,12 @@ packages: react: ^16.8 || ^17.0 || ^18.0 react-dom: ^16.8 || ^17.0 || ^18.0 + vfile-message@4.0.3: + resolution: {integrity: sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw==} + + vfile@6.0.3: + resolution: {integrity: sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q==} + vite-tsconfig-paths@4.3.2: resolution: {integrity: sha512-0Vd/a6po6Q+86rPlntHye7F31zA2URZMbH8M3saAZ/xR9QoGN/L21bxEGfXdWmFdNkqPpRdxFT7nmNe12e9/uA==} peerDependencies: @@ -3071,6 +3371,9 @@ packages: resolution: {integrity: sha512-U/PBtDf35ff0D8X8D0jfdzHYEPFxAI7jJlxZXwCSez5M3190m+QobIfh+sWDWSHMCWWJN2AWamkegn6vr6YBTw==} engines: {node: '>=18'} + zwitch@2.0.4: + resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} + snapshots: '@alloc/quick-lru@5.2.0': {} @@ -3802,12 +4105,30 @@ snapshots: '@types/crypto-js@4.2.2': {} + '@types/debug@4.1.13': + dependencies: + '@types/ms': 2.1.0 + + '@types/estree-jsx@1.0.5': + dependencies: + '@types/estree': 1.0.8 + '@types/estree@1.0.8': {} + '@types/hast@3.0.4': + dependencies: + '@types/unist': 3.0.3 + '@types/js-cookie@3.0.6': {} '@types/json-schema@7.0.15': {} + '@types/mdast@4.0.4': + dependencies: + '@types/unist': 3.0.3 + + '@types/ms@2.1.0': {} + '@types/node@25.3.2': dependencies: undici-types: 7.18.2 @@ -3827,6 +4148,10 @@ snapshots: '@types/statuses@2.0.6': {} + '@types/unist@2.0.11': {} + + '@types/unist@3.0.3': {} + '@types/websocket@1.0.10': dependencies: '@types/node': 25.3.2 @@ -3922,6 +4247,8 @@ snapshots: '@typescript-eslint/types': 8.56.1 eslint-visitor-keys: 5.0.1 + '@ungap/structured-clone@1.3.0': {} + '@vitejs/plugin-react@4.7.0(vite@7.1.11(@types/node@25.3.2)(jiti@1.21.7)(yaml@2.6.0))': dependencies: '@babel/core': 7.29.0 @@ -4118,6 +4445,8 @@ snapshots: transitivePeerDependencies: - debug + bail@2.0.2: {} + balanced-match@1.0.2: {} balanced-match@4.0.4: {} @@ -4174,11 +4503,21 @@ snapshots: caniuse-lite@1.0.30001774: {} + ccount@2.0.1: {} + chalk@4.1.2: dependencies: ansi-styles: 4.3.0 supports-color: 7.2.0 + character-entities-html4@2.1.0: {} + + character-entities-legacy@3.0.0: {} + + character-entities@2.0.2: {} + + character-reference-invalid@2.0.1: {} + chokidar@3.6.0: dependencies: anymatch: 3.1.3 @@ -4213,6 +4552,8 @@ snapshots: dependencies: delayed-stream: 1.0.0 + comma-separated-tokens@2.0.3: {} + commander@4.1.1: {} compute-scroll-into-view@3.1.1: {} @@ -4274,6 +4615,10 @@ snapshots: dependencies: ms: 2.1.3 + decode-named-character-reference@1.3.0: + dependencies: + character-entities: 2.0.2 + deep-is@0.1.4: {} define-data-property@1.1.4: @@ -4290,8 +4635,14 @@ snapshots: delayed-stream@1.0.0: {} + dequal@2.0.3: {} + detect-node-es@1.1.0: {} + devlop@1.1.0: + dependencies: + dequal: 2.0.3 + didyoumean@1.2.2: {} dlv@1.1.3: {} @@ -4462,6 +4813,8 @@ snapshots: escape-string-regexp@4.0.0: {} + escape-string-regexp@5.0.0: {} + eslint-config-prettier@9.1.2(eslint@9.39.3(jiti@1.21.7)): dependencies: eslint: 9.39.3(jiti@1.21.7) @@ -4571,6 +4924,8 @@ snapshots: estraverse@5.3.0: {} + estree-util-is-identifier-name@3.0.0: {} + esutils@2.0.3: {} event-emitter@0.3.5: @@ -4582,6 +4937,8 @@ snapshots: dependencies: type: 2.7.3 + extend@3.0.2: {} + fast-deep-equal@3.1.3: {} fast-glob@3.3.3: @@ -4733,12 +5090,38 @@ snapshots: dependencies: function-bind: 1.1.2 + hast-util-to-jsx-runtime@2.3.6: + dependencies: + '@types/estree': 1.0.8 + '@types/hast': 3.0.4 + '@types/unist': 3.0.3 + comma-separated-tokens: 2.0.3 + devlop: 1.1.0 + estree-util-is-identifier-name: 3.0.0 + hast-util-whitespace: 3.0.0 + mdast-util-mdx-expression: 2.0.1 + mdast-util-mdx-jsx: 3.2.0 + mdast-util-mdxjs-esm: 2.0.1 + property-information: 7.1.0 + space-separated-tokens: 2.0.2 + style-to-js: 1.1.21 + unist-util-position: 5.0.0 + vfile-message: 4.0.3 + transitivePeerDependencies: + - supports-color + + hast-util-whitespace@3.0.0: + dependencies: + '@types/hast': 3.0.4 + headers-polyfill@4.0.3: {} html-parse-stringify@3.0.1: dependencies: void-elements: 3.1.0 + html-url-attributes@3.0.1: {} + hyphenate-style-name@1.1.0: {} i18next@23.16.8: @@ -4756,6 +5139,8 @@ snapshots: imurmurhash@0.1.4: {} + inline-style-parser@0.2.7: {} + internal-slot@1.1.0: dependencies: es-errors: 1.3.0 @@ -4766,6 +5151,13 @@ snapshots: dependencies: loose-envify: 1.4.0 + is-alphabetical@2.0.1: {} + + is-alphanumerical@2.0.1: + dependencies: + is-alphabetical: 2.0.1 + is-decimal: 2.0.1 + is-array-buffer@3.0.5: dependencies: call-bind: 1.0.8 @@ -4810,6 +5202,8 @@ snapshots: call-bound: 1.0.4 has-tostringtag: 1.0.2 + is-decimal@2.0.1: {} + is-extglob@2.1.1: {} is-finalizationregistry@1.1.1: @@ -4830,6 +5224,8 @@ snapshots: dependencies: is-extglob: 2.1.1 + is-hexadecimal@2.0.1: {} + is-map@2.0.3: {} is-negative-zero@2.0.3: {} @@ -4843,6 +5239,8 @@ snapshots: is-number@7.0.0: {} + is-plain-obj@4.1.0: {} + is-regex@1.2.1: dependencies: call-bound: 1.0.4 @@ -4954,6 +5352,8 @@ snapshots: lodash.merge@4.6.2: {} + longest-streak@3.1.0: {} + loose-envify@1.4.0: dependencies: js-tokens: 4.0.0 @@ -4966,14 +5366,360 @@ snapshots: dependencies: react: 18.3.1 + markdown-table@3.0.4: {} + matchmediaquery@0.4.2: dependencies: css-mediaquery: 0.1.2 math-intrinsics@1.1.0: {} + mdast-util-find-and-replace@3.0.2: + dependencies: + '@types/mdast': 4.0.4 + escape-string-regexp: 5.0.0 + unist-util-is: 6.0.1 + unist-util-visit-parents: 6.0.2 + + mdast-util-from-markdown@2.0.3: + dependencies: + '@types/mdast': 4.0.4 + '@types/unist': 3.0.3 + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + mdast-util-to-string: 4.0.0 + micromark: 4.0.2 + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-decode-string: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + unist-util-stringify-position: 4.0.0 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-autolink-literal@2.0.1: + dependencies: + '@types/mdast': 4.0.4 + ccount: 2.0.1 + devlop: 1.1.0 + mdast-util-find-and-replace: 3.0.2 + micromark-util-character: 2.1.1 + + mdast-util-gfm-footnote@2.1.0: + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + micromark-util-normalize-identifier: 2.0.1 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-strikethrough@2.0.0: + dependencies: + '@types/mdast': 4.0.4 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-table@2.0.0: + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + markdown-table: 3.0.4 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-task-list-item@2.0.0: + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm@3.1.0: + dependencies: + mdast-util-from-markdown: 2.0.3 + mdast-util-gfm-autolink-literal: 2.0.1 + mdast-util-gfm-footnote: 2.1.0 + mdast-util-gfm-strikethrough: 2.0.0 + mdast-util-gfm-table: 2.0.0 + mdast-util-gfm-task-list-item: 2.0.0 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-mdx-expression@2.0.1: + dependencies: + '@types/estree-jsx': 1.0.5 + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-mdx-jsx@3.2.0: + dependencies: + '@types/estree-jsx': 1.0.5 + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + '@types/unist': 3.0.3 + ccount: 2.0.1 + devlop: 1.1.0 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + parse-entities: 4.0.2 + stringify-entities: 4.0.4 + unist-util-stringify-position: 4.0.0 + vfile-message: 4.0.3 + transitivePeerDependencies: + - supports-color + + mdast-util-mdxjs-esm@2.0.1: + dependencies: + '@types/estree-jsx': 1.0.5 + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.0.3 + mdast-util-to-markdown: 2.1.2 + transitivePeerDependencies: + - supports-color + + mdast-util-phrasing@4.1.0: + dependencies: + '@types/mdast': 4.0.4 + unist-util-is: 6.0.1 + + mdast-util-to-hast@13.2.1: + dependencies: + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + '@ungap/structured-clone': 1.3.0 + devlop: 1.1.0 + micromark-util-sanitize-uri: 2.0.1 + trim-lines: 3.0.1 + unist-util-position: 5.0.0 + unist-util-visit: 5.1.0 + vfile: 6.0.3 + + mdast-util-to-markdown@2.1.2: + dependencies: + '@types/mdast': 4.0.4 + '@types/unist': 3.0.3 + longest-streak: 3.1.0 + mdast-util-phrasing: 4.1.0 + mdast-util-to-string: 4.0.0 + micromark-util-classify-character: 2.0.1 + micromark-util-decode-string: 2.0.1 + unist-util-visit: 5.1.0 + zwitch: 2.0.4 + + mdast-util-to-string@4.0.0: + dependencies: + '@types/mdast': 4.0.4 + merge2@1.4.1: {} + micromark-core-commonmark@2.0.3: + dependencies: + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + micromark-factory-destination: 2.0.1 + micromark-factory-label: 2.0.1 + micromark-factory-space: 2.0.1 + micromark-factory-title: 2.0.1 + micromark-factory-whitespace: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-chunked: 2.0.1 + micromark-util-classify-character: 2.0.1 + micromark-util-html-tag-name: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-subtokenize: 2.1.0 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm-autolink-literal@2.1.0: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm-footnote@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-core-commonmark: 2.0.3 + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm-strikethrough@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-util-chunked: 2.0.1 + micromark-util-classify-character: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm-table@2.1.1: + dependencies: + devlop: 1.1.0 + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm-tagfilter@2.0.0: + dependencies: + micromark-util-types: 2.0.2 + + micromark-extension-gfm-task-list-item@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-extension-gfm@3.0.0: + dependencies: + micromark-extension-gfm-autolink-literal: 2.1.0 + micromark-extension-gfm-footnote: 2.1.0 + micromark-extension-gfm-strikethrough: 2.1.0 + micromark-extension-gfm-table: 2.1.1 + micromark-extension-gfm-tagfilter: 2.0.0 + micromark-extension-gfm-task-list-item: 2.1.0 + micromark-util-combine-extensions: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-factory-destination@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-factory-label@2.0.1: + dependencies: + devlop: 1.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-factory-space@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-types: 2.0.2 + + micromark-factory-title@2.0.1: + dependencies: + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-factory-whitespace@2.0.1: + dependencies: + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-util-character@2.1.1: + dependencies: + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-util-chunked@2.0.1: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-classify-character@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-util-combine-extensions@2.0.1: + dependencies: + micromark-util-chunked: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-util-decode-numeric-character-reference@2.0.2: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-decode-string@2.0.1: + dependencies: + decode-named-character-reference: 1.3.0 + micromark-util-character: 2.1.1 + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-symbol: 2.0.1 + + micromark-util-encode@2.0.1: {} + + micromark-util-html-tag-name@2.0.1: {} + + micromark-util-normalize-identifier@2.0.1: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-resolve-all@2.0.1: + dependencies: + micromark-util-types: 2.0.2 + + micromark-util-sanitize-uri@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-encode: 2.0.1 + micromark-util-symbol: 2.0.1 + + micromark-util-subtokenize@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-util-chunked: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + + micromark-util-symbol@2.0.1: {} + + micromark-util-types@2.0.2: {} + + micromark@4.0.2: + dependencies: + '@types/debug': 4.1.13 + debug: 4.4.3 + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + micromark-core-commonmark: 2.0.3 + micromark-factory-space: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-chunked: 2.0.1 + micromark-util-combine-extensions: 2.0.1 + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-encode: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-subtokenize: 2.1.0 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.2 + transitivePeerDependencies: + - supports-color + micromatch@4.0.8: dependencies: braces: 3.0.3 @@ -5116,6 +5862,16 @@ snapshots: dependencies: callsites: 3.1.0 + parse-entities@4.0.2: + dependencies: + '@types/unist': 2.0.11 + character-entities-legacy: 3.0.0 + character-reference-invalid: 2.0.1 + decode-named-character-reference: 1.3.0 + is-alphanumerical: 2.0.1 + is-decimal: 2.0.1 + is-hexadecimal: 2.0.1 + path-exists@4.0.0: {} path-key@3.1.1: {} @@ -5190,6 +5946,8 @@ snapshots: object-assign: 4.1.1 react-is: 16.13.1 + property-information@7.1.0: {} + proxy-from-env@1.1.0: {} punycode@2.3.1: {} @@ -5555,6 +6313,24 @@ snapshots: react-is@18.3.1: {} + react-markdown@10.1.0(@types/react@18.3.28)(react@18.3.1): + dependencies: + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + '@types/react': 18.3.28 + devlop: 1.1.0 + hast-util-to-jsx-runtime: 2.3.6 + html-url-attributes: 3.0.1 + mdast-util-to-hast: 13.2.1 + react: 18.3.1 + remark-parse: 11.0.0 + remark-rehype: 11.1.2 + unified: 11.0.5 + unist-util-visit: 5.1.0 + vfile: 6.0.3 + transitivePeerDependencies: + - supports-color + react-refresh@0.17.0: {} react-remove-scroll-bar@2.3.8(@types/react@18.3.28)(react@18.3.1): @@ -5641,6 +6417,40 @@ snapshots: gopd: 1.2.0 set-function-name: 2.0.2 + remark-gfm@4.0.1: + dependencies: + '@types/mdast': 4.0.4 + mdast-util-gfm: 3.1.0 + micromark-extension-gfm: 3.0.0 + remark-parse: 11.0.0 + remark-stringify: 11.0.0 + unified: 11.0.5 + transitivePeerDependencies: + - supports-color + + remark-parse@11.0.0: + dependencies: + '@types/mdast': 4.0.4 + mdast-util-from-markdown: 2.0.3 + micromark-util-types: 2.0.2 + unified: 11.0.5 + transitivePeerDependencies: + - supports-color + + remark-rehype@11.1.2: + dependencies: + '@types/hast': 3.0.4 + '@types/mdast': 4.0.4 + mdast-util-to-hast: 13.2.1 + unified: 11.0.5 + vfile: 6.0.3 + + remark-stringify@11.0.0: + dependencies: + '@types/mdast': 4.0.4 + mdast-util-to-markdown: 2.1.2 + unified: 11.0.5 + require-directory@2.1.1: {} resize-observer-polyfill@1.5.1: {} @@ -5796,6 +6606,8 @@ snapshots: source-map-js@1.2.1: {} + space-separated-tokens@2.0.2: {} + statuses@2.0.2: {} stop-iteration-iterator@1.1.0: @@ -5857,12 +6669,25 @@ snapshots: define-properties: 1.2.1 es-object-atoms: 1.1.1 + stringify-entities@4.0.4: + dependencies: + character-entities-html4: 2.1.0 + character-entities-legacy: 3.0.0 + strip-ansi@6.0.1: dependencies: ansi-regex: 5.0.1 strip-json-comments@3.1.1: {} + style-to-js@1.1.21: + dependencies: + style-to-object: 1.0.14 + + style-to-object@1.0.14: + dependencies: + inline-style-parser: 0.2.7 + stylis@4.3.6: {} sucrase@3.35.1: @@ -5942,6 +6767,10 @@ snapshots: dependencies: tldts: 7.0.23 + trim-lines@3.0.1: {} + + trough@2.2.0: {} + ts-api-utils@2.4.0(typescript@5.9.3): dependencies: typescript: 5.9.3 @@ -6023,6 +6852,39 @@ snapshots: undici-types@7.18.2: {} + unified@11.0.5: + dependencies: + '@types/unist': 3.0.3 + bail: 2.0.2 + devlop: 1.1.0 + extend: 3.0.2 + is-plain-obj: 4.1.0 + trough: 2.2.0 + vfile: 6.0.3 + + unist-util-is@6.0.1: + dependencies: + '@types/unist': 3.0.3 + + unist-util-position@5.0.0: + dependencies: + '@types/unist': 3.0.3 + + unist-util-stringify-position@4.0.0: + dependencies: + '@types/unist': 3.0.3 + + unist-util-visit-parents@6.0.2: + dependencies: + '@types/unist': 3.0.3 + unist-util-is: 6.0.1 + + unist-util-visit@5.1.0: + dependencies: + '@types/unist': 3.0.3 + unist-util-is: 6.0.1 + unist-util-visit-parents: 6.0.2 + until-async@3.0.2: {} update-browserslist-db@1.2.3(browserslist@4.28.1): @@ -6065,6 +6927,16 @@ snapshots: - '@types/react' - '@types/react-dom' + vfile-message@4.0.3: + dependencies: + '@types/unist': 3.0.3 + unist-util-stringify-position: 4.0.0 + + vfile@6.0.3: + dependencies: + '@types/unist': 3.0.3 + vfile-message: 4.0.3 + vite-tsconfig-paths@4.3.2(typescript@5.9.3)(vite@7.1.11(@types/node@25.3.2)(jiti@1.21.7)(yaml@2.6.0)): dependencies: debug: 4.4.3 @@ -6188,3 +7060,5 @@ snapshots: yocto-queue@1.2.2: {} yoctocolors-cjs@2.1.3: {} + + zwitch@2.0.4: {} diff --git a/web/src/api/picoclaw.ts b/web/src/api/picoclaw.ts new file mode 100644 index 0000000..1c3ccf0 --- /dev/null +++ b/web/src/api/picoclaw.ts @@ -0,0 +1,130 @@ +import { http } from '@/lib/http.ts'; +import { + picoclawGateway, + type GatewayRunState, + type GatewayTransportState +} from '@/lib/picoclaw-gateway.ts'; + +const sessionIDHeader = 'X-PicoClaw-Session-ID'; + +type ModelConfigRequest = { + model: string; + api_base: string; + api_key: string; +}; + +type AgentProfileRequest = { + profile: string; +}; + +export type PicoclawSessionListItem = { + id: string; + title: string; + preview: string; + message_count: number; + created: string; + updated: string; +}; + +export type PicoclawSessionDetailMessage = { + role: 'user' | 'assistant'; + content: string; +}; + +export type PicoclawSessionDetail = { + id: string; + messages: PicoclawSessionDetailMessage[]; + summary?: string; + created: string; + updated: string; +}; + +export function setPicoclawModelConfig(data: ModelConfigRequest) { + return http.post('/api/picoclaw/model/config', data); +} + +export function setPicoclawAgentProfile(data: AgentProfileRequest) { + return http.post('/api/picoclaw/agent/profile', data); +} + +export function listPicoclawSessions(params?: { offset?: number; limit?: number }) { + return http.get('/api/picoclaw/sessions', { params }); +} + +export function getPicoclawSession(id: string) { + return http.get(`/api/picoclaw/sessions/${id}`); +} + +export function deletePicoclawSession(id: string) { + return http.delete(`/api/picoclaw/sessions/${id}`); +} + +export function getRuntimeStatus() { + return http.get('/api/picoclaw/runtime/status'); +} + +export function startRuntime() { + return http.post('/api/picoclaw/runtime/start'); +} + +export function stopRuntime() { + return http.post('/api/picoclaw/runtime/stop'); +} + +export function installRuntime() { + return http.post('/api/picoclaw/runtime/install'); +} + +export function uninstallRuntime() { + return http.post('/api/picoclaw/runtime/uninstall'); +} + +export function connectGateway(sessionId?: string) { + return picoclawGateway.connect({ sessionId }); +} + +export function releaseRuntimeSession(sessionId?: string) { + const activeSessionId = sessionId || picoclawGateway.getSessionId(); + if (!activeSessionId) { + return Promise.resolve(null); + } + + return http.request({ + method: 'delete', + url: '/api/picoclaw/runtime/session', + headers: { + [sessionIDHeader]: activeSessionId + } + }); +} + +export function sendChatMessage( + content: string, + options?: { id?: string; maxSteps?: number; maxRuntimeMs?: number; trackState?: boolean } +) { + return picoclawGateway.sendChatMessage(content, options); +} + +export function sendStopMessage() { + return picoclawGateway.sendStopMessage(); +} + +export function closeGateway() { + const activeSessionId = picoclawGateway.getSessionId(); + picoclawGateway.close(); + if (!activeSessionId) { + return; + } + + void releaseRuntimeSession(activeSessionId).catch(() => undefined); +} + +export function onGatewayConnectionState(listener: (state: GatewayTransportState) => void) { + return picoclawGateway.on('transport_state', listener); +} + +export function onGatewayRunState(listener: (state: GatewayRunState) => void) { + return picoclawGateway.on('run_state', listener); +} + +export { picoclawGateway }; diff --git a/web/src/assets/images/monitor-x.svg b/web/src/assets/images/monitor-x.svg deleted file mode 100644 index c2597f9..0000000 --- a/web/src/assets/images/monitor-x.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/web/src/assets/styles/index.css b/web/src/assets/styles/index.css index ade9c50..9ec4fa7 100644 --- a/web/src/assets/styles/index.css +++ b/web/src/assets/styles/index.css @@ -2,7 +2,8 @@ @tailwind components; @tailwind utilities; -html, body { +html, +body { padding: 0; margin: 0; background: #000; @@ -23,7 +24,7 @@ html, body { } .spin { -animation: spin 1s linear; + animation: spin 1s linear; } @keyframes spin { @@ -34,3 +35,32 @@ animation: spin 1s linear; transform: rotate(360deg); } } + +/* PicoClaw Chat Custom Scrollbar */ +.picoclaw-sidebar-scrollbar *::-webkit-scrollbar { + width: 4px; + height: 4px; +} +.picoclaw-sidebar-scrollbar *::-webkit-scrollbar-track { + background: transparent; +} +.picoclaw-sidebar-scrollbar *::-webkit-scrollbar-thumb { + background: rgba(255, 255, 255, 0.15); + border-radius: 4px; +} +.picoclaw-sidebar-scrollbar *::-webkit-scrollbar-thumb:hover { + background: rgba(255, 255, 255, 0.3); +} + +/* PicoClaw Chat Loading Animation */ +@keyframes picoclaw-loading-bounce { + 0%, + 100% { + transform: translateY(0.5px); + opacity: 0.3; + } + 50% { + transform: translateY(-1.5px); + opacity: 1; + } +} diff --git a/web/src/components/icons/icon-base.tsx b/web/src/components/icons/icon-base.tsx new file mode 100644 index 0000000..ba6c85a --- /dev/null +++ b/web/src/components/icons/icon-base.tsx @@ -0,0 +1,43 @@ +import type { ReactNode, SVGProps } from 'react'; + +export type IconProps = Omit, 'color'> & { + size?: number | string; + color?: string; + strokeWidth?: number | string; +}; + +type IconBaseProps = IconProps & { + children: ReactNode; +}; + +export const IconBase = ({ + children, + className, + size = 24, + color = 'currentColor', + strokeWidth = 2, + viewBox = '0 0 24 24', + fill = 'none', + strokeLinecap = 'round', + strokeLinejoin = 'round', + ...props +}: IconBaseProps) => { + return ( + + {children} + + ); +}; diff --git a/web/src/components/icons/layout-sidebar-right-collapse.tsx b/web/src/components/icons/layout-sidebar-right-collapse.tsx new file mode 100644 index 0000000..94d4084 --- /dev/null +++ b/web/src/components/icons/layout-sidebar-right-collapse.tsx @@ -0,0 +1,12 @@ +import { IconBase, type IconProps } from './icon-base.tsx'; + +export const LayoutSidebarRightCollapse = (props: IconProps) => { + return ( + + + + + + + ); +}; diff --git a/web/src/components/icons/robot.tsx b/web/src/components/icons/robot.tsx new file mode 100644 index 0000000..0ba9a1a --- /dev/null +++ b/web/src/components/icons/robot.tsx @@ -0,0 +1,18 @@ +import { IconBase, type IconProps } from './icon-base.tsx'; + +export const Robot = (props: IconProps) => { + return ( + + + + + + + + + + + + + ); +}; diff --git a/web/src/i18n/locales/en.ts b/web/src/i18n/locales/en.ts index 8bc71f3..8819b72 100644 --- a/web/src/i18n/locales/en.ts +++ b/web/src/i18n/locales/en.ts @@ -54,7 +54,7 @@ const en = { authDescription: 'Please enter the AP password to continue', authFailed: 'Invalid AP password', passPlaceholder: 'AP password', - verifyBtn: 'Verify', + verifyBtn: 'Verify' } }, screen: { @@ -449,6 +449,127 @@ const en = { cancelBtn: 'No' } }, + picoclaw: { + title: 'PicoClaw Assistant', + empty: 'Open the panel and start a task to begin.', + inputPlaceholder: 'Describe what you want the PicoClaw to do', + newConversation: 'New conversation', + processing: 'Processing...', + agent: { + defaultTitle: 'General Assistant', + defaultDescription: 'General chat, search, and workspace help.', + kvmTitle: 'Remote Control', + kvmDescription: 'Operate the remote host through NanoKVM.', + switched: 'Agent role switched', + switchFailed: 'Failed to switch agent role' + }, + send: 'Send', + cancel: 'Cancel', + status: { + connecting: 'Connecting to gateway...', + connected: 'PicoClaw session connected', + disconnected: 'PicoClaw session closed', + stopped: 'Stop request sent', + runtimeStarted: 'PicoClaw runtime started', + runtimeStartFailed: 'Failed to start PicoClaw runtime', + runtimeStopped: 'PicoClaw runtime stopped', + runtimeStopFailed: 'Failed to stop PicoClaw runtime' + }, + connection: { + runtime: { + checking: 'Checking', + ready: 'Runtime ready', + stopped: 'Runtime stopped', + unavailable: 'Runtime unavailable', + configError: 'Configuration error' + }, + transport: { + connecting: 'Connecting', + connected: 'Connected' + }, + run: { + idle: 'Idle', + busy: 'Busy' + } + }, + message: { + toolAction: 'Action', + observation: 'Observation', + screenshot: 'Screenshot' + }, + overlay: { + locked: 'PicoClaw is controlling the device. Manual input is paused.' + }, + install: { + install: 'Install PicoClaw', + installing: 'Installing PicoClaw', + success: 'PicoClaw installed successfully', + failed: 'Failed to install PicoClaw', + uninstalling: 'Uninstalling runtime...', + uninstalled: 'Runtime uninstalled successfully.', + uninstallFailed: 'Uninstall failed.', + requiredTitle: 'PicoClaw is not installed', + requiredDescription: 'Install PicoClaw before starting the PicoClaw runtime.', + progressDescription: 'PicoClaw is being downloaded and installed.', + stages: { + preparing: 'Preparing', + downloading: 'Downloading', + extracting: 'Extracting', + installing: 'Installing', + installed: 'Installed', + install_timeout: 'Timed Out', + install_failed: 'Failed' + } + }, + model: { + requiredTitle: 'Model configuration is required', + requiredDescription: 'Configure the PicoClaw model before using PicoClaw chat.', + docsTitle: 'Configuration Guide', + docsDesc: 'Supported models and protocols', + menuLabel: 'Configure model', + modelIdentifier: 'Model Identifier', + modelIdentifierPlaceholder: 'openai/gpt-5.4', + apiBase: 'API Base URL', + apiBasePlaceholder: 'https://api.example.com/v1', + apiKey: 'API Key', + apiKeyPlaceholder: 'Enter the model API key', + save: 'Save', + saving: 'Saving', + saved: 'Model configuration saved', + saveFailed: 'Failed to save model configuration', + invalid: 'Model identifier, API base URL, and API key are required' + }, + uninstall: { + menuLabel: 'Uninstall', + confirmTitle: 'Uninstall PicoClaw', + confirmContent: + 'Are you sure you want to uninstall PicoClaw? This will delete the executable and all configuration files.', + confirmOk: 'Uninstall', + confirmCancel: 'Cancel' + }, + history: { + title: 'History', + loading: 'Loading sessions...', + emptyTitle: 'No history yet', + emptyDescription: 'Previous PicoClaw sessions will appear here.', + loadFailed: 'Failed to load session history', + deleteFailed: 'Failed to delete session', + deleteConfirmTitle: 'Delete session', + deleteConfirmContent: 'Are you sure you want to delete "{{title}}"?', + deleteConfirmOk: 'Delete', + deleteConfirmCancel: 'Cancel', + messageCount_one: '{{count}} message', + messageCount_other: '{{count}} messages' + }, + config: { + startRuntime: 'Start PicoClaw', + stopRuntime: 'Stop PicoClaw' + }, + start: { + title: 'Start PicoClaw', + description: 'Start the runtime to begin using the PicoClaw assistant.' + } + }, error: { title: "We've ran into an issue", refresh: 'Refresh' diff --git a/web/src/i18n/locales/zh.ts b/web/src/i18n/locales/zh.ts index 27f86c5..b734a2f 100644 --- a/web/src/i18n/locales/zh.ts +++ b/web/src/i18n/locales/zh.ts @@ -52,7 +52,7 @@ const zh = { authDescription: '请输入 AP 密码以继续', authFailed: '密码错误', passPlaceholder: 'AP 密码', - verifyBtn: '验证', + verifyBtn: '验证' } }, screen: { @@ -414,6 +414,126 @@ const zh = { cancel: '取消' } }, + picoclaw: { + title: 'PicoClaw 助手', + empty: '打开面板并发送任务后,PicoClaw 会开始工作。', + inputPlaceholder: '描述你希望 PicoClaw 执行的操作', + newConversation: '新对话', + processing: '正在处理中...', + agent: { + defaultTitle: '通用助手', + defaultDescription: '适合普通问答、搜索和工作区辅助。', + kvmTitle: '远程控制', + kvmDescription: '通过 NanoKVM 操作下游远程主机。', + switched: '角色已切换', + switchFailed: '切换角色失败' + }, + send: '发送', + cancel: '取消', + status: { + connecting: '正在连接 Gateway...', + connected: 'PicoClaw 会话已连接', + disconnected: 'PicoClaw 会话已关闭', + stopped: '已发送停止请求', + runtimeStarted: 'PicoClaw 运行时已启动', + runtimeStartFailed: '启动 PicoClaw 运行时失败', + runtimeStopped: 'PicoClaw 运行时已停止', + runtimeStopFailed: '停止 PicoClaw 运行时失败' + }, + connection: { + runtime: { + checking: '检查中', + ready: '运行时已就绪', + stopped: '运行时未启动', + unavailable: '运行时不可用', + configError: '配置错误' + }, + transport: { + connecting: '连接中', + connected: '已连接' + }, + run: { + idle: '空闲', + busy: '执行中' + } + }, + message: { + toolAction: '动作', + observation: '观察', + screenshot: '截图' + }, + overlay: { + locked: 'PicoClaw 正在控制设备,手动输入已暂停。' + }, + install: { + install: '安装 PicoClaw', + installing: '正在安装 PicoClaw', + success: 'PicoClaw 安装成功', + failed: 'PicoClaw 安装失败', + uninstalling: '正在卸载运行时...', + uninstalled: '运行时卸载成功。', + uninstallFailed: '卸载失败。', + requiredTitle: '未安装 PicoClaw', + requiredDescription: '在启动 PicoClaw 运行时之前,需要先下载安装 PicoClaw。', + progressDescription: '正在下载并安装 PicoClaw。', + stages: { + preparing: '准备中', + downloading: '下载中', + extracting: '解压中', + installing: '安装中', + installed: '已安装', + install_timeout: '已超时', + install_failed: '失败' + } + }, + model: { + requiredTitle: '需要配置模型', + requiredDescription: '在使用 PicoClaw 聊天之前,请先配置 PicoClaw 模型。', + docsTitle: '配置指南', + docsDesc: '支持的模型与协议', + menuLabel: '配置模型', + modelIdentifier: '模型标识', + modelIdentifierPlaceholder: 'openai/gpt-5.4', + apiBase: 'API Base URL', + apiBasePlaceholder: 'https://api.example.com/v1', + apiKey: 'API Key', + apiKeyPlaceholder: '请输入模型 API Key', + save: '保存模型配置', + saving: '正在保存模型配置', + saved: '模型配置已保存', + saveFailed: '保存模型配置失败', + invalid: '模型标识、API Base URL 和 API Key 不能为空' + }, + uninstall: { + menuLabel: '卸载', + confirmTitle: '卸载 PicoClaw', + confirmContent: '确定要卸载 PicoClaw 吗?这会删除可执行文件和所有配置文件。', + confirmOk: '卸载', + confirmCancel: '取消' + }, + history: { + title: '历史会话', + loading: '正在加载历史会话...', + emptyTitle: '还没有历史会话', + emptyDescription: '之前的 PicoClaw 会话会显示在这里。', + loadFailed: '加载会话历史失败', + deleteFailed: '删除会话失败', + deleteConfirmTitle: '删除会话', + deleteConfirmContent: '确定要删除“{{title}}”吗?', + deleteConfirmOk: '删除', + deleteConfirmCancel: '取消', + messageCount_one: '{{count}} 条消息', + messageCount_other: '{{count}} 条消息' + }, + config: { + startRuntime: '启动 PicoClaw', + stopRuntime: '停止 PicoClaw' + }, + start: { + title: '启动 PicoClaw', + description: '启动运行时后即可开始使用 PicoClaw 助手。' + } + }, error: { title: '我们遇到了问题', refresh: '刷新' diff --git a/web/src/jotai/picoclaw.ts b/web/src/jotai/picoclaw.ts new file mode 100644 index 0000000..2fea45f --- /dev/null +++ b/web/src/jotai/picoclaw.ts @@ -0,0 +1,94 @@ +import { atom } from 'jotai'; + +import { getPicoclawMaxRuntimeMs, getPicoclawMaxSteps } from '@/lib/picoclaw-storage.ts'; + +export type PicoclawTransportState = 'disconnected' | 'connecting' | 'connected' | 'error'; + +export type PicoclawRunState = 'idle' | 'busy'; + +export type PicoclawRuntimeStatus = { + ready: boolean; + installed: boolean; + installing: boolean; + install_progress?: number; + install_stage?: string; + install_path?: string; + agent_profile?: string; + model_configured: boolean; + model_name?: string; + status: string; + config_error?: string; + last_error?: string; + checked_at?: string; + current_session?: string; +}; + +export type PicoclawRuntimeStartResult = { + started: boolean; + command: string; + output?: string; + status: PicoclawRuntimeStatus; +}; + +export type PicoclawRuntimeInstallResult = { + installed: boolean; + binary: string; + download: string; + output?: string; + status: PicoclawRuntimeStatus; +}; + +export type PicoclawMessageKind = + | 'user' + | 'assistant' + | 'status' + | 'tool_action' + | 'observation' + | 'error'; + +export type PicoclawChatMessage = { + id: string; + kind: PicoclawMessageKind; + text?: string; + imageBase64?: string; + createdAt: number; + action?: string; + pending?: boolean; + raw?: unknown; +}; + +export type PicoclawOverlayState = { + visible: boolean; + message: string; + x?: number; + y?: number; + action?: string; +}; + +export type PicoclawTakeoverState = { + active: boolean; + sessionId?: string; + reason?: string; +}; + +export type PicoclawConfigState = { + maxSteps: number; + maxRuntimeMs: number; +}; + +export const picoclawChatOpenAtom = atom(false); +export const picoclawMessagesAtom = atom([]); +export const picoclawTransportStateAtom = atom('disconnected'); +export const picoclawRunStateAtom = atom('idle'); +export const picoclawRuntimeStatusAtom = atom(null); +export const picoclawOverlayAtom = atom({ + visible: false, + message: '' +}); +export const picoclawConfigAtom = atom({ + maxSteps: getPicoclawMaxSteps(), + maxRuntimeMs: getPicoclawMaxRuntimeMs() +}); +export const picoclawTakeoverStateAtom = atom({ + active: false +}); diff --git a/web/src/lib/picoclaw-gateway.ts b/web/src/lib/picoclaw-gateway.ts new file mode 100644 index 0000000..18779d2 --- /dev/null +++ b/web/src/lib/picoclaw-gateway.ts @@ -0,0 +1,471 @@ +import { clearPicoclawSessionId, setPicoclawSessionId } from '@/lib/picoclaw-storage.ts'; +import { getBaseUrl } from '@/lib/service.ts'; + +export type GatewayTransportState = 'disconnected' | 'connecting' | 'connected' | 'error'; + +export type GatewayRunState = 'idle' | 'busy'; + +export type GatewayRuntimeStatus = { + ready: boolean; + installed: boolean; + installing: boolean; + install_progress?: number; + install_stage?: string; + install_path?: string; + model_configured: boolean; + model_name?: string; + status: string; + config_error?: string; + last_error?: string; + checked_at?: string; + current_session?: string; +}; + +export type GatewayAssistantMessage = { + id: string; + text: string; + raw: Record; +}; + +export type GatewayToolAction = { + id: string; + action: string; + x?: number; + y?: number; + raw: Record; +}; + +export type GatewayObservation = { + id: string; + text?: string; + imageBase64?: string; + raw: Record; +}; + +export type GatewayError = { + code: string; + message: string; + raw?: unknown; +}; + +export type GatewayClose = { + code: number; + reason: string; +}; + +type GatewayEventMap = { + connected: { sessionId: string }; + transport_state: GatewayTransportState; + run_state: GatewayRunState; + assistant_message: GatewayAssistantMessage; + tool_action: GatewayToolAction; + observation: GatewayObservation; + error: GatewayError; + close: GatewayClose; +}; + +type EventName = keyof GatewayEventMap; +type Listener = (payload: GatewayEventMap[T]) => void; + +type ConnectOptions = { + sessionId?: string; + autoReconnect?: boolean; +}; + +type SendMessageOptions = { + id?: string; + maxSteps?: number; + maxRuntimeMs?: number; + trackState?: boolean; +}; + +const CLOSE_ERRORS: Record = { + 4001: 'AI_LOCK_HELD', + 4002: 'RUNTIME_UNAVAILABLE', + 4003: 'AUTH_FAILED', + 4004: 'AI_TAKEN_OVER', + 4005: 'UPSTREAM_CLOSED' +}; + +export function generateUUIDv4() { + if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { + return crypto.randomUUID(); + } + + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (char) => { + const random = Math.floor(Math.random() * 16); + const value = char === 'x' ? random : (random & 0x3) | 0x8; + return value.toString(16); + }); +} + +class PicoClawGateway { + private ws: WebSocket | null = null; + private listeners = new Map>>(); + private connectPromise: Promise | null = null; + private sessionId = ''; + private connectionAttemptId = 0; + private autoReconnect = false; + private reconnectAttempts = 0; + private reconnectTimer: ReturnType | null = null; + private explicitClose = false; + + public connect(options: ConnectOptions = {}) { + if (options.sessionId) { + this.sessionId = options.sessionId; + setPicoclawSessionId(options.sessionId); + } else if (!this.ws || this.ws.readyState === WebSocket.CLOSED) { + this.sessionId = generateUUIDv4(); + setPicoclawSessionId(this.sessionId); + } + + if ( + this.ws && + (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) + ) { + return this.connectPromise ?? Promise.resolve(this.sessionId); + } + + this.autoReconnect = !!options.autoReconnect; + this.explicitClose = false; + this.clearReconnectTimer(); + this.setTransportState('connecting'); + + const sessionId = this.sessionId || generateUUIDv4(); + const attemptId = ++this.connectionAttemptId; + this.sessionId = sessionId; + setPicoclawSessionId(sessionId); + const url = `${getBaseUrl('ws')}/api/picoclaw/gateway/ws?session_id=${encodeURIComponent(sessionId)}`; + + this.connectPromise = new Promise((resolve, reject) => { + const ws = new WebSocket(url); + this.ws = ws; + + ws.onopen = () => { + if (this.ws !== ws || this.connectionAttemptId !== attemptId) { + return; + } + this.reconnectAttempts = 0; + this.emit('connected', { sessionId }); + this.setTransportState('connected'); + this.setRunState('idle'); + resolve(sessionId); + }; + + ws.onerror = () => { + if (this.ws !== ws || this.connectionAttemptId !== attemptId) { + return; + } + this.setTransportState('error'); + }; + + ws.onclose = (event) => { + if (this.ws !== ws || this.connectionAttemptId !== attemptId) { + return; + } + this.ws = null; + + if (event.code !== 1000) { + const code = CLOSE_ERRORS[event.code] || 'UNKNOWN'; + if (code !== 'UNKNOWN') { + this.emit('error', { + code, + message: event.reason || code + }); + } + } + + this.emit('close', { + code: event.code, + reason: event.reason + }); + + this.setTransportState(event.code === 1000 ? 'disconnected' : 'error'); + this.setRunState('idle'); + + if (!this.explicitClose) { + reject(new Error(event.reason || 'gateway closed')); + if (this.autoReconnect) { + this.scheduleReconnect(); + } + } + }; + + ws.onmessage = (event) => { + if (this.ws !== ws || this.connectionAttemptId !== attemptId) { + return; + } + this.handleMessage(event.data); + }; + }).finally(() => { + if (this.connectionAttemptId === attemptId) { + this.connectPromise = null; + } + }); + + return this.connectPromise; + } + + public on(eventName: T, listener: Listener) { + const current = this.listeners.get(eventName) || new Set(); + current.add(listener as Listener); + this.listeners.set(eventName, current); + + return () => { + current.delete(listener as Listener); + if (current.size === 0) { + this.listeners.delete(eventName); + } + }; + } + + public sendChatMessage(content: string, options: SendMessageOptions = {}) { + const trimmed = content.trim(); + if (!trimmed || !this.ws || this.ws.readyState !== WebSocket.OPEN) { + return null; + } + + const id = options.id || generateUUIDv4(); + if (options.trackState !== false) { + this.setRunState('busy'); + } + + this.ws.send( + JSON.stringify({ + id, + session_id: this.sessionId, + type: 'message.send', + payload: { + content: trimmed, + max_steps: options.maxSteps, + max_runtime_ms: options.maxRuntimeMs + } + }) + ); + + return { id, sessionId: this.sessionId }; + } + + public sendStopMessage() { + if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { + return false; + } + + this.ws.send( + JSON.stringify({ + type: 'message.cancel', + session_id: this.sessionId, + payload: {} + }) + ); + return true; + } + + public close(code = 1000, reason = 'NORMAL_CLOSURE') { + this.explicitClose = true; + this.clearReconnectTimer(); + + if (this.ws && this.ws.readyState === WebSocket.OPEN) { + this.ws.close(code, reason); + } else if (this.ws && this.ws.readyState === WebSocket.CONNECTING) { + this.ws.close(); + } + + this.ws = null; + this.setTransportState('disconnected'); + this.setRunState('idle'); + } + + public rotateSession() { + this.close(); + clearPicoclawSessionId(); + this.sessionId = generateUUIDv4(); + setPicoclawSessionId(this.sessionId); + return this.sessionId; + } + + public getSessionId() { + return this.sessionId; + } + + private scheduleReconnect() { + this.clearReconnectTimer(); + this.reconnectAttempts += 1; + const delay = Math.min(1000 * this.reconnectAttempts, 5000); + this.reconnectTimer = setTimeout(() => { + this.connect({ sessionId: this.sessionId, autoReconnect: true }).catch(() => undefined); + }, delay); + } + + private clearReconnectTimer() { + if (this.reconnectTimer) { + clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + } + } + + private setTransportState(state: GatewayTransportState) { + this.emit('transport_state', state); + } + + private setRunState(state: GatewayRunState) { + this.emit('run_state', state); + } + + private handleMessage(rawData: string | ArrayBuffer | Blob) { + if (typeof rawData !== 'string') { + return; + } + + let message: Record; + try { + message = JSON.parse(rawData) as Record; + } catch { + this.emit('error', { + code: 'INVALID_MESSAGE', + message: 'Failed to parse gateway message', + raw: rawData + }); + return; + } + + const type = String(message.type || ''); + if (type === 'typing.start') { + this.setRunState('busy'); + return; + } + if (type === 'typing.stop') { + return; + } + if (type === 'pong') { + return; + } + if (type === 'error') { + this.setTransportState('error'); + this.setRunState('idle'); + this.emit('error', { + code: String(message.code || 'ERROR'), + message: String(message.message || 'Gateway error'), + raw: message + }); + return; + } + if (type === 'message.create' || type === 'message.update') { + this.setRunState('idle'); + this.emit('assistant_message', { + id: String(message.id || generateUUIDv4()), + text: extractText(message), + raw: message + }); + return; + } + + const imageBase64 = extractImageBase64(message); + if (imageBase64) { + this.emit('observation', { + id: String(message.id || generateUUIDv4()), + text: extractText(message), + imageBase64, + raw: message + }); + return; + } + + const action = extractAction(message); + if (action) { + this.emit('tool_action', { + id: String(message.id || generateUUIDv4()), + action: action.action, + x: action.x, + y: action.y, + raw: message + }); + return; + } + } + + private emit(eventName: T, payload: GatewayEventMap[T]) { + const current = this.listeners.get(eventName); + if (!current) { + return; + } + + current.forEach((listener) => { + (listener as Listener)(payload); + }); + } +} + +function extractText(message: Record) { + const payload = (message.payload || {}) as Record; + const content = payload.content ?? message.content ?? payload.text ?? message.text; + + if (typeof content === 'string') { + return normalizeLiteralEmptyText(content); + } + if (Array.isArray(content)) { + return content + .map((item) => { + if (typeof item === 'string') { + return normalizeLiteralEmptyText(item); + } + if (item && typeof item === 'object' && 'text' in item) { + return normalizeLiteralEmptyText(String((item as Record).text || '')); + } + return ''; + }) + .filter(Boolean) + .join('\n') + .trim(); + } + + return ''; +} + +function extractImageBase64(message: Record) { + const payload = (message.payload || {}) as Record; + const data = (payload.data || message.data || {}) as Record; + + const value = data.image_base64 || payload.image_base64 || message.image_base64; + + return typeof value === 'string' ? value : undefined; +} + +function extractAction(message: Record) { + const payload = (message.payload || {}) as Record; + const actionValue = payload.action || message.action || payload.tool_name || message.tool_name; + + if (typeof actionValue !== 'string') { + return null; + } + + const action = normalizeLiteralEmptyText(actionValue); + if (!action) { + return null; + } + + const x = typeof payload.x === 'number' ? payload.x : undefined; + const y = typeof payload.y === 'number' ? payload.y : undefined; + + return { + action, + x, + y + }; +} + +function normalizeLiteralEmptyText(value: string) { + const trimmed = value.trim(); + if (!trimmed) { + return ''; + } + + const normalized = trimmed.toLowerCase(); + if (normalized === 'null' || normalized === 'undefined') { + return ''; + } + + return trimmed; +} + +export const picoclawGateway = new PicoClawGateway(); diff --git a/web/src/lib/picoclaw-storage.ts b/web/src/lib/picoclaw-storage.ts new file mode 100644 index 0000000..4392778 --- /dev/null +++ b/web/src/lib/picoclaw-storage.ts @@ -0,0 +1,123 @@ +const PICOCLAW_SESSION_ID_KEY = 'nano-kvm-picoclaw-session-id'; +const LEGACY_AI_SESSION_ID_KEY = 'nano-kvm-ai-session-id'; +const PICOCLAW_MAX_STEPS_KEY = 'nano-kvm-picoclaw-max-steps'; +const LEGACY_AI_MAX_STEPS_KEY = 'nano-kvm-ai-max-steps'; +const PICOCLAW_MAX_RUNTIME_KEY = 'nano-kvm-picoclaw-max-runtime-ms'; +const LEGACY_AI_MAX_RUNTIME_KEY = 'nano-kvm-ai-max-runtime-ms'; +const PICOCLAW_RUNTIME_INSTALL_SNAPSHOT_KEY = 'nano-kvm-picoclaw-runtime-install-snapshot'; +const LEGACY_AI_RUNTIME_INSTALL_SNAPSHOT_KEY = 'nano-kvm-ai-runtime-install-snapshot'; + +export type PicoclawRuntimeInstallSnapshot = { + installing: boolean; + installProgress?: number; + installStage?: string; + status?: string; +}; + +function getSessionStore() { + try { + return window.sessionStorage; + } catch { + return null; + } +} + +function getLocalValue(primaryKey: string, legacyKey: string) { + return localStorage.getItem(primaryKey) || localStorage.getItem(legacyKey); +} + +function setLocalValue(primaryKey: string, legacyKey: string, value: string) { + localStorage.setItem(primaryKey, value); + localStorage.removeItem(legacyKey); +} + +function removeLocalValue(primaryKey: string, legacyKey: string) { + localStorage.removeItem(primaryKey); + localStorage.removeItem(legacyKey); +} + +export function getPicoclawSessionId() { + const sessionStore = getSessionStore(); + return ( + sessionStore?.getItem(PICOCLAW_SESSION_ID_KEY) || + sessionStore?.getItem(LEGACY_AI_SESSION_ID_KEY) || + getLocalValue(PICOCLAW_SESSION_ID_KEY, LEGACY_AI_SESSION_ID_KEY) + ); +} + +export function setPicoclawSessionId(sessionId: string) { + const sessionStore = getSessionStore(); + sessionStore?.setItem(PICOCLAW_SESSION_ID_KEY, sessionId); + sessionStore?.removeItem(LEGACY_AI_SESSION_ID_KEY); + setLocalValue(PICOCLAW_SESSION_ID_KEY, LEGACY_AI_SESSION_ID_KEY, sessionId); +} + +export function clearPicoclawSessionId() { + const sessionStore = getSessionStore(); + sessionStore?.removeItem(PICOCLAW_SESSION_ID_KEY); + sessionStore?.removeItem(LEGACY_AI_SESSION_ID_KEY); + removeLocalValue(PICOCLAW_SESSION_ID_KEY, LEGACY_AI_SESSION_ID_KEY); +} + +export function getPicoclawMaxSteps() { + const value = getLocalValue(PICOCLAW_MAX_STEPS_KEY, LEGACY_AI_MAX_STEPS_KEY); + const parsed = value ? Number(value) : 20; + return Math.max(1, Math.min(50, Number.isFinite(parsed) ? parsed : 20)); +} + +export function setPicoclawMaxSteps(maxSteps: number) { + const value = Math.max(1, Math.min(50, Math.round(maxSteps))); + setLocalValue(PICOCLAW_MAX_STEPS_KEY, LEGACY_AI_MAX_STEPS_KEY, String(value)); +} + +export function getPicoclawMaxRuntimeMs() { + const value = getLocalValue(PICOCLAW_MAX_RUNTIME_KEY, LEGACY_AI_MAX_RUNTIME_KEY); + const parsed = value ? Number(value) : 120000; + if (!Number.isFinite(parsed) || parsed <= 0) { + return 120000; + } + return parsed; +} + +export function setPicoclawMaxRuntimeMs(maxRuntimeMs: number) { + const value = Math.max(1000, Math.round(maxRuntimeMs)); + setLocalValue(PICOCLAW_MAX_RUNTIME_KEY, LEGACY_AI_MAX_RUNTIME_KEY, String(value)); +} + +export function getPicoclawRuntimeInstallSnapshot(): PicoclawRuntimeInstallSnapshot | null { + const value = getLocalValue( + PICOCLAW_RUNTIME_INSTALL_SNAPSHOT_KEY, + LEGACY_AI_RUNTIME_INSTALL_SNAPSHOT_KEY + ); + if (!value) { + return null; + } + + try { + const parsed = JSON.parse(value) as PicoclawRuntimeInstallSnapshot; + if (!parsed || typeof parsed !== 'object') { + return null; + } + return { + installing: parsed.installing === true, + installProgress: + typeof parsed.installProgress === 'number' ? parsed.installProgress : undefined, + installStage: typeof parsed.installStage === 'string' ? parsed.installStage : undefined, + status: typeof parsed.status === 'string' ? parsed.status : undefined + }; + } catch { + return null; + } +} + +export function setPicoclawRuntimeInstallSnapshot(snapshot: PicoclawRuntimeInstallSnapshot) { + setLocalValue( + PICOCLAW_RUNTIME_INSTALL_SNAPSHOT_KEY, + LEGACY_AI_RUNTIME_INSTALL_SNAPSHOT_KEY, + JSON.stringify(snapshot) + ); +} + +export function clearPicoclawRuntimeInstallSnapshot() { + removeLocalValue(PICOCLAW_RUNTIME_INSTALL_SNAPSHOT_KEY, LEGACY_AI_RUNTIME_INSTALL_SNAPSHOT_KEY); +} diff --git a/web/src/pages/desktop/index.tsx b/web/src/pages/desktop/index.tsx index e69a431..5538e72 100644 --- a/web/src/pages/desktop/index.tsx +++ b/web/src/pages/desktop/index.tsx @@ -1,10 +1,12 @@ -import { useEffect } from 'react'; -import { useAtom } from 'jotai'; +import { useEffect, useState } from 'react'; +import { Splitter } from 'antd'; +import { useAtom, useAtomValue } from 'jotai'; import { useTranslation } from 'react-i18next'; import { useMediaQuery } from 'react-responsive'; import * as storage from '@/lib/localstorage.ts'; import { client } from '@/lib/websocket.ts'; +import { picoclawChatOpenAtom } from '@/jotai/picoclaw.ts'; import { resolutionAtom, videoModeAtom } from '@/jotai/screen.ts'; import { Head } from '@/components/head.tsx'; @@ -12,15 +14,19 @@ import { Keyboard } from './keyboard'; import { Menu } from './menu'; import { Mouse } from './mouse'; import { Notification } from './notification.tsx'; +import { Sidebar as PicoclawSidebar } from './picoclaw'; +import { ActionOverlay } from './picoclaw/action-overlay.tsx'; import { Screen } from './screen'; import { VirtualKeyboard } from './virtual-keyboard'; export const Desktop = () => { const { t } = useTranslation(); const isBigScreen = useMediaQuery({ minWidth: 850 }); + const [picoclawSidebarWidth, setPicoclawSidebarWidth] = useState(420); const [videoMode, setVideoMode] = useAtom(videoModeAtom); const [resolution, setResolution] = useAtom(resolutionAtom); + const isPicoclawChatOpen = useAtomValue(picoclawChatOpenAtom); useEffect(() => { client.connect(); @@ -50,22 +56,56 @@ export const Desktop = () => { return defaultVideoMode; } + function handleSplitterResize(sizes: number[]) { + const nextSidebarWidth = sizes[1]; + if (typeof nextSidebarWidth === 'number' && nextSidebarWidth > 0) { + setPicoclawSidebarWidth(nextSidebarWidth); + } + } + return ( - <> +
{isBigScreen && } {videoMode && resolution && ( - <> +
- +
+ + +
+ +
+
+ + {isBigScreen && isPicoclawChatOpen ? : null} + +
+
+ - +
)} + {!isBigScreen && isPicoclawChatOpen ? ( +
+ +
+ ) : null} + - +
); }; diff --git a/web/src/pages/desktop/keyboard/index.tsx b/web/src/pages/desktop/keyboard/index.tsx index 0d679b7..a5c36df 100644 --- a/web/src/pages/desktop/keyboard/index.tsx +++ b/web/src/pages/desktop/keyboard/index.tsx @@ -6,6 +6,7 @@ import { KeyboardReport } from '@/lib/keyboard.ts'; import { isModifier } from '@/lib/keymap'; import { client, MessageEvent } from '@/lib/websocket.ts'; import { isKeyboardEnableAtom } from '@/jotai/keyboard.ts'; +import { picoclawTakeoverStateAtom } from '@/jotai/picoclaw.ts'; import { Recorder } from './recorder.tsx'; import { useAltGr } from './useAltGr.ts'; @@ -16,6 +17,7 @@ export const Keyboard = () => { const os = getOperatingSystem(); const isKeyboardEnabled = useAtomValue(isKeyboardEnableAtom); + const picoclawTakeoverState = useAtomValue(picoclawTakeoverStateAtom); const keyboardRef = useRef(new KeyboardReport()); const pressedKeys = useRef(new Set()); @@ -38,6 +40,17 @@ export const Keyboard = () => { const altGrRef = useRef(altGr); altGrRef.current = altGr; + useEffect(() => { + if (!picoclawTakeoverState.active) { + return; + } + + pressedKeys.current.clear(); + leaderKeyRef.current.reset(); + altGrRef.current.reset(); + sendReport(keyboardRef.current.reset()); + }, [picoclawTakeoverState.active]); + useEffect(() => { document.addEventListener('keydown', handleKeyDown); document.addEventListener('keyup', handleKeyUp); diff --git a/web/src/pages/desktop/menu/index.tsx b/web/src/pages/desktop/menu/index.tsx index 5894167..2fccf47 100644 --- a/web/src/pages/desktop/menu/index.tsx +++ b/web/src/pages/desktop/menu/index.tsx @@ -15,6 +15,7 @@ import { Image } from './image'; import { Keyboard } from './keyboard'; import { Mouse } from './mouse'; import { Collapse, Expand } from './operations'; +import { Picoclaw } from './picoclaw'; import { Power } from './power'; import { Screen } from './screen'; import { Script } from './script'; @@ -101,6 +102,13 @@ export const Menu = () => { )} + {isEnabled('picoclaw') && ( + <> + + + + )} + {isEnabled('power') && ( <> diff --git a/web/src/pages/desktop/menu/picoclaw/index.tsx b/web/src/pages/desktop/menu/picoclaw/index.tsx new file mode 100644 index 0000000..6ffbe20 --- /dev/null +++ b/web/src/pages/desktop/menu/picoclaw/index.tsx @@ -0,0 +1,22 @@ +import { Tooltip } from 'antd'; +import { useSetAtom } from 'jotai'; +import { useTranslation } from 'react-i18next'; + +import { picoclawChatOpenAtom } from '@/jotai/picoclaw.ts'; +import { Robot } from '@/components/icons/robot.tsx'; + +export const Picoclaw = () => { + const { t } = useTranslation(); + const setIsChatOpen = useSetAtom(picoclawChatOpenAtom); + + return ( + +
setIsChatOpen((open) => !open)} + > + +
+
+ ); +}; diff --git a/web/src/pages/desktop/menu/settings/appearance/menu-icons.tsx b/web/src/pages/desktop/menu/settings/appearance/menu-icons.tsx index 6afed39..dae42a9 100644 --- a/web/src/pages/desktop/menu/settings/appearance/menu-icons.tsx +++ b/web/src/pages/desktop/menu/settings/appearance/menu-icons.tsx @@ -14,6 +14,7 @@ import { useTranslation } from 'react-i18next'; import * as ls from '@/lib/localstorage.ts'; import { menuDisabledItemsAtom } from '@/jotai/settings.ts'; +import { Robot } from '@/components/icons/robot.tsx'; export const MenuIcons = () => { const { t } = useTranslation(); @@ -26,6 +27,7 @@ export const MenuIcons = () => { { key: 'script', icon: }, { key: 'terminal', icon: }, { key: 'wol', icon: }, + { key: 'picoclaw', icon: }, { key: 'power', icon: }, { key: 'fullscreen', icon: , label: 'fullscreen.toggle' }, { key: 'collapse', icon: , label: 'menu.collapse' } diff --git a/web/src/pages/desktop/mouse/absolute.tsx b/web/src/pages/desktop/mouse/absolute.tsx index 7c8f600..ace7b3e 100644 --- a/web/src/pages/desktop/mouse/absolute.tsx +++ b/web/src/pages/desktop/mouse/absolute.tsx @@ -43,21 +43,22 @@ export const Absolute = () => { const VELOCITY_THRESHOLD = 0.3; useEffect(() => { - const screen = document.getElementById('screen') as HTMLVideoElement; + const screen = document.getElementById('screen') as HTMLVideoElement | null; if (!screen) return; + const target = screen; - screen.addEventListener('mousedown', handleMouseDown); - screen.addEventListener('mouseup', handleMouseUp); - screen.addEventListener('mousemove', handleMouseMove); - screen.addEventListener('wheel', handleWheel); - screen.addEventListener('click', disableEvent); - screen.addEventListener('contextmenu', disableEvent); + target.addEventListener('mousedown', handleMouseDown); + target.addEventListener('mouseup', handleMouseUp); + target.addEventListener('mousemove', handleMouseMove); + target.addEventListener('wheel', handleWheel); + target.addEventListener('click', disableEvent); + target.addEventListener('contextmenu', disableEvent); if (isBigScreen) { - screen.addEventListener('touchstart', handleTouchStart); - screen.addEventListener('touchmove', handleTouchMove); - screen.addEventListener('touchend', handleTouchEnd); - screen.addEventListener('touchcancel', handleTouchCancel); + target.addEventListener('touchstart', handleTouchStart); + target.addEventListener('touchmove', handleTouchMove); + target.addEventListener('touchend', handleTouchEnd); + target.addEventListener('touchcancel', handleTouchCancel); } // Mouse down event @@ -257,15 +258,16 @@ export const Absolute = () => { } function getCorrectedCoords(clientX: number, clientY: number) { - const rect = screen.getBoundingClientRect(); + const rect = target.getBoundingClientRect(); - if (!screen.videoWidth || !screen.videoHeight) { + const mediaSize = getMediaSize(target); + if (!mediaSize) { const x = (clientX - rect.left) / rect.width; const y = (clientY - rect.top) / rect.height; return { x, y }; } - const videoRatio = screen.videoWidth / screen.videoHeight; + const mediaRatio = mediaSize.width / mediaSize.height; const elementRatio = rect.width / rect.height; let renderedWidth = rect.width; @@ -273,11 +275,11 @@ export const Absolute = () => { let offsetX = 0; let offsetY = 0; - if (videoRatio > elementRatio) { - renderedHeight = rect.width / videoRatio; + if (mediaRatio > elementRatio) { + renderedHeight = rect.width / mediaRatio; offsetY = (rect.height - renderedHeight) / 2; } else { - renderedWidth = rect.height * videoRatio; + renderedWidth = rect.height * mediaRatio; offsetX = (rect.width - renderedWidth) / 2; } @@ -288,16 +290,16 @@ export const Absolute = () => { } return () => { - screen.removeEventListener('mousemove', handleMouseMove); - screen.removeEventListener('mousedown', handleMouseDown); - screen.removeEventListener('mouseup', handleMouseUp); - screen.removeEventListener('wheel', handleWheel); - screen.removeEventListener('click', disableEvent); - screen.removeEventListener('contextmenu', disableEvent); - screen.removeEventListener('touchstart', handleTouchStart); - screen.removeEventListener('touchmove', handleTouchMove); - screen.removeEventListener('touchend', handleTouchEnd); - screen.removeEventListener('touchcancel', handleTouchCancel); + target.removeEventListener('mousemove', handleMouseMove); + target.removeEventListener('mousedown', handleMouseDown); + target.removeEventListener('mouseup', handleMouseUp); + target.removeEventListener('wheel', handleWheel); + target.removeEventListener('click', disableEvent); + target.removeEventListener('contextmenu', disableEvent); + target.removeEventListener('touchstart', handleTouchStart); + target.removeEventListener('touchmove', handleTouchMove); + target.removeEventListener('touchend', handleTouchEnd); + target.removeEventListener('touchcancel', handleTouchCancel); if (longPressTimerRef.current) { clearTimeout(longPressTimerRef.current); @@ -347,3 +349,19 @@ export const Absolute = () => { return <>; }; + +function getMediaSize(screen: Element) { + if (screen instanceof HTMLVideoElement && screen.videoWidth > 0 && screen.videoHeight > 0) { + return { width: screen.videoWidth, height: screen.videoHeight }; + } + + if (screen instanceof HTMLImageElement && screen.naturalWidth > 0 && screen.naturalHeight > 0) { + return { width: screen.naturalWidth, height: screen.naturalHeight }; + } + + if (screen instanceof HTMLCanvasElement && screen.width > 0 && screen.height > 0) { + return { width: screen.width, height: screen.height }; + } + + return null; +} diff --git a/web/src/pages/desktop/picoclaw/action-overlay.tsx b/web/src/pages/desktop/picoclaw/action-overlay.tsx new file mode 100644 index 0000000..1a12e1a --- /dev/null +++ b/web/src/pages/desktop/picoclaw/action-overlay.tsx @@ -0,0 +1,68 @@ +import { useEffect, useState } from 'react'; +import { useAtomValue } from 'jotai'; +import { useTranslation } from 'react-i18next'; + +import { picoclawOverlayAtom, picoclawTakeoverStateAtom } from '@/jotai/picoclaw.ts'; + +type ScreenRect = { + left: number; + top: number; + width: number; + height: number; +}; + +export const ActionOverlay = () => { + const { t } = useTranslation(); + const overlay = useAtomValue(picoclawOverlayAtom); + const takeover = useAtomValue(picoclawTakeoverStateAtom); + const [rect, setRect] = useState(null); + + useEffect(() => { + const screen = document.getElementById('screen'); + if (!screen) { + setRect(null); + return; + } + + const updateRect = () => { + const bounds = screen.getBoundingClientRect(); + setRect({ + left: bounds.left, + top: bounds.top, + width: bounds.width, + height: bounds.height + }); + }; + + updateRect(); + + const observer = new ResizeObserver(updateRect); + observer.observe(screen); + window.addEventListener('resize', updateRect); + window.addEventListener('scroll', updateRect, true); + + return () => { + observer.disconnect(); + window.removeEventListener('resize', updateRect); + window.removeEventListener('scroll', updateRect, true); + }; + }, [takeover.active]); + + if (!takeover.active || !rect || rect.width <= 0 || rect.height <= 0) { + return null; + } + + return ( +
+
+ {overlay.message || t('picoclaw.overlay.locked')} +
+
+ ); +}; diff --git a/web/src/pages/desktop/picoclaw/index.ts b/web/src/pages/desktop/picoclaw/index.ts new file mode 100644 index 0000000..4fad64c --- /dev/null +++ b/web/src/pages/desktop/picoclaw/index.ts @@ -0,0 +1 @@ +export { Sidebar } from './sidebar.tsx'; diff --git a/web/src/pages/desktop/picoclaw/markdown-content.tsx b/web/src/pages/desktop/picoclaw/markdown-content.tsx new file mode 100644 index 0000000..48969c0 --- /dev/null +++ b/web/src/pages/desktop/picoclaw/markdown-content.tsx @@ -0,0 +1,84 @@ +import ReactMarkdown from 'react-markdown'; +import remarkGfm from 'remark-gfm'; + +type MarkdownContentProps = { + content?: string; + className?: string; +}; + +export const MarkdownContent = ({ content, className }: MarkdownContentProps) => { + if (!content) { + return null; + } + + return ( +
+ ( +

+ {children} +

+ ), + a: ({ children, href }) => ( + + {children} + + ), + ul: ({ children }) =>
    {children}
, + ol: ({ children }) =>
    {children}
, + li: ({ children }) =>
  • {children}
  • , + h1: ({ children }) => ( +

    {children}

    + ), + h2: ({ children }) => ( +

    {children}

    + ), + h3: ({ children }) =>

    {children}

    , + blockquote: ({ children }) => ( +
    + {children} +
    + ), + code: ({ children, className: codeClassName, ...props }: any) => + !String(codeClassName || '').includes('language-') ? ( + + {children} + + ) : ( + + {children} + + ), + pre: ({ children }) => ( +
    +              {children}
    +            
    + ), + table: ({ children }) => ( +
    + {children}
    +
    + ), + th: ({ children }) => ( + {children} + ), + td: ({ children }) => ( + {children} + ) + }} + > + {content} +
    +
    + ); +}; diff --git a/web/src/pages/desktop/picoclaw/message-input.tsx b/web/src/pages/desktop/picoclaw/message-input.tsx new file mode 100644 index 0000000..83d6930 --- /dev/null +++ b/web/src/pages/desktop/picoclaw/message-input.tsx @@ -0,0 +1,92 @@ +import { useEffect, useRef, useState } from 'react'; +import { Button } from 'antd'; +import { useSetAtom } from 'jotai'; +import { PlusIcon, SendIcon } from 'lucide-react'; +import { useTranslation } from 'react-i18next'; + +import { isKeyboardEnableAtom } from '@/jotai/keyboard.ts'; +import type { PicoclawTransportState } from '@/jotai/picoclaw.ts'; + +type MessageInputProps = { + transportState: PicoclawTransportState; + onSend: (content: string) => void | Promise; + onNewConversation: () => void | Promise; + disableNewConversation?: boolean; +}; + +export const MessageInput = ({ + transportState, + onSend, + onNewConversation, + disableNewConversation +}: MessageInputProps) => { + const { t } = useTranslation(); + const setIsKeyboardEnable = useSetAtom(isKeyboardEnableAtom); + + const [value, setValue] = useState(''); + const isComposingRef = useRef(false); + + const isReady = transportState === 'connected'; + + useEffect(() => { + setIsKeyboardEnable(false); + return () => { + setIsKeyboardEnable(true); + }; + }, [setIsKeyboardEnable]); + + async function submit() { + const content = value.trim(); + if (!content || !isReady) return; + await onSend(content); + setValue(''); + } + + return ( +
    +