Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0b0eddf3a | ||
|
|
50abcf1a91 | ||
|
|
9bf7707912 | ||
|
|
7e1dbd9afa | ||
|
|
2d1052fd5f | ||
|
|
e238f5f37d | ||
|
|
adf3e9f024 | ||
|
|
6e81513e36 | ||
|
|
c7b32c3fd6 | ||
|
|
32b4afd772 |
+103
-63
@@ -11,13 +11,11 @@ concurrency:
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
linux:
|
||||
# Verification: TinyCC builds and tests everything in seconds. tcc warns on
|
||||
# `#pragma GCC`, so no -Werror here; the gcc release builds below carry it.
|
||||
tcc-check:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc, clang]
|
||||
steps:
|
||||
- name: Checkout
|
||||
env:
|
||||
@@ -34,60 +32,38 @@ jobs:
|
||||
$SUDO bash scripts/ci_pkg_mirror.sh
|
||||
$SUDO apt-get update
|
||||
$SUDO apt-get install -y --no-install-recommends \
|
||||
build-essential clang cmake pkg-config python3 \
|
||||
build-essential pkg-config python3 curl ca-certificates \
|
||||
libcurl4-openssl-dev libncurses-dev
|
||||
- name: Build (warnings are errors)
|
||||
run: make CC=${{ matrix.cc }} CFLAGS="-std=c11 -Wall -Wextra -O2 -g -Werror" -j"$(nproc)"
|
||||
- name: Build TinyCC
|
||||
run: bash scripts/ci_tcc.sh
|
||||
- name: Build
|
||||
run: make CC=tcc AR=ar CFLAGS="-std=c11 -Wall -O2 -g" -j"$(nproc)"
|
||||
- name: Unit tests
|
||||
run: |
|
||||
# Same -Werror as the build step so undeclared libc helpers cannot
|
||||
# silently return int and truncate pointers (see test_subagent).
|
||||
make CC=${{ matrix.cc }} \
|
||||
CFLAGS="-std=c11 -Wall -Wextra -O2 -g -Werror" check
|
||||
- name: CMake build + ctest
|
||||
run: |
|
||||
cmake -B build -DCMAKE_C_COMPILER=${{ matrix.cc }} -DCST_BUILD_TUI=ON
|
||||
cmake --build build -j"$(nproc)"
|
||||
ctest --test-dir build --output-on-failure
|
||||
run: make CC=tcc AR=ar CFLAGS="-std=c11 -Wall -O2 -g" check
|
||||
- name: Smoke test
|
||||
run: |
|
||||
./cst-agent --help
|
||||
./cst-agent --workspace "$PWD" --list-sessions
|
||||
./cst-agent --demo-ctl --workspace .
|
||||
- name: WebUI end-to-end (recorded stream, no API key)
|
||||
run: sh scripts/e2e_webui.sh
|
||||
|
||||
musl:
|
||||
# Release: static musl binaries, gcc with -Werror.
|
||||
# dist/cst-agent-VERSION-linux-ARCH.tar.gz
|
||||
linux:
|
||||
needs: tcc-check
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
container: alpine:3.20
|
||||
steps:
|
||||
- name: Install tools + mirror
|
||||
run: |
|
||||
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' \
|
||||
/etc/apk/repositories
|
||||
apk add --no-cache curl git bash
|
||||
- name: Checkout
|
||||
env:
|
||||
GITHUB_SERVER_URL: ${{ github.server_url }}
|
||||
GITHUB_REPOSITORY: ${{ github.repository }}
|
||||
GITHUB_SHA: ${{ github.sha }}
|
||||
run: |
|
||||
curl -fsSL "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/raw/commit/$GITHUB_SHA/scripts/ci_checkout.sh" \
|
||||
-o /tmp/ci_checkout.sh
|
||||
bash /tmp/ci_checkout.sh
|
||||
- name: Install build dependencies
|
||||
run: |
|
||||
# linux-headers provides linux/audit.h for the seccomp probe path
|
||||
apk add --no-cache build-base pkgconf curl-dev ncurses-dev \
|
||||
linux-headers git nodejs bash python3
|
||||
- name: Build + unit tests
|
||||
run: |
|
||||
make CFLAGS="-std=c11 -Wall -Wextra -O2 -Werror" -j"$(nproc)"
|
||||
make check
|
||||
|
||||
# MinGW-w64 UCRT cross build → dist/cst-agent-*-windows-x86_64.zip
|
||||
windows-cross:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 120
|
||||
timeout-minutes: 60
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- arch: x86_64
|
||||
check: 1
|
||||
- arch: x86
|
||||
check: 1
|
||||
- arch: aarch64
|
||||
check: 0
|
||||
steps:
|
||||
- name: Checkout
|
||||
env:
|
||||
@@ -103,20 +79,29 @@ jobs:
|
||||
SUDO=$(command -v sudo || true)
|
||||
$SUDO bash scripts/ci_pkg_mirror.sh
|
||||
$SUDO apt-get update
|
||||
# ncurses-bin/-term: tic, infocmp and the fallback terminal entries
|
||||
$SUDO apt-get install -y --no-install-recommends \
|
||||
ca-certificates curl git cmake ninja-build make pkg-config \
|
||||
python3 zip unzip perl xz-utils
|
||||
- name: Cross-build Windows zip
|
||||
run: bash scripts/ci_windows_cross.sh
|
||||
- name: Verify zip
|
||||
build-essential cmake ninja-build pkg-config python3 perl \
|
||||
curl ca-certificates xz-utils bzip2 file ncurses-bin ncurses-term
|
||||
if [ "${{ matrix.arch }}" = aarch64 ]; then
|
||||
$SUDO apt-get install -y --no-install-recommends qemu-user-static
|
||||
fi
|
||||
- name: Build (static, -Werror)
|
||||
env:
|
||||
CST_LINUX_CHECK: ${{ matrix.check }}
|
||||
run: bash scripts/package_linux.sh ${{ matrix.arch }}
|
||||
- name: Smoke test
|
||||
run: |
|
||||
ls -lah dist/windows-x86_64/
|
||||
ls -lah dist/cst-agent-*-windows-x86_64.zip
|
||||
cat dist/cst-agent-*-windows-x86_64.zip.sha256
|
||||
unzip -l dist/cst-agent-*-windows-x86_64.zip | head -20
|
||||
# Auto-publish to Gitea Releases (v$CST_VERSION from include/cst_version.h).
|
||||
# Prefer repo secret RELEASE_TOKEN (write:repository); fall back to the
|
||||
# job token. Skipped on pull_request and when no token is available.
|
||||
d=$(ls -d dist/cst-agent-*-linux-${{ matrix.arch }})
|
||||
file "$d/cst-agent"
|
||||
run=""
|
||||
[ "${{ matrix.arch }}" = aarch64 ] && run=qemu-aarch64-static
|
||||
$run "$d/cst-agent" --version
|
||||
$run "$d/cst-agent" --help >/dev/null
|
||||
$run "$d/cst-agent" --workspace "$PWD" --list-sessions
|
||||
cat dist/cst-agent-*-linux-${{ matrix.arch }}.tar.gz.sha256
|
||||
# Release vVERSION (from include/cst_version.h) on Gitea. Prefer repo
|
||||
# secret RELEASE_TOKEN (write:repository); fall back to the job token.
|
||||
- name: Publish Gitea Release
|
||||
if: github.event_name == 'push'
|
||||
env:
|
||||
@@ -132,4 +117,59 @@ jobs:
|
||||
echo "::warning::no RELEASE_TOKEN / GITHUB_TOKEN; skip auto-release"
|
||||
exit 0
|
||||
fi
|
||||
bash scripts/release_windows.sh
|
||||
bash scripts/release.sh dist/cst-agent-*-linux-${{ matrix.arch }}.tar.gz
|
||||
|
||||
# Release: mingw-w64 UCRT gcc (Debian trixie) with -Werror.
|
||||
# dist/cst-agent-VERSION-windows-x86_64.zip and -setup.exe (NSIS)
|
||||
windows:
|
||||
needs: tcc-check
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 120
|
||||
container: debian:trixie
|
||||
steps:
|
||||
- name: Install tools
|
||||
run: |
|
||||
# No CA certificates in the image yet, so the mirror is plain http.
|
||||
sed -i 's#https\?://deb.debian.org#http://mirrors.tuna.tsinghua.edu.cn#g' \
|
||||
/etc/apt/sources.list.d/debian.sources
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends \
|
||||
ca-certificates curl git build-essential cmake ninja-build make \
|
||||
pkg-config python3 perl zip unzip xz-utils patch file \
|
||||
gcc-mingw-w64-ucrt64 binutils-mingw-w64-ucrt64 nsis
|
||||
- name: Checkout
|
||||
env:
|
||||
GITHUB_SERVER_URL: ${{ github.server_url }}
|
||||
GITHUB_REPOSITORY: ${{ github.repository }}
|
||||
GITHUB_SHA: ${{ github.sha }}
|
||||
run: |
|
||||
curl -fsSL "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/raw/commit/$GITHUB_SHA/scripts/ci_checkout.sh" \
|
||||
-o /tmp/ci_checkout.sh
|
||||
bash /tmp/ci_checkout.sh
|
||||
- name: Cross-build zip and installer
|
||||
env:
|
||||
CST_WIN_CFLAGS: -Wall -Wextra -Werror
|
||||
run: bash scripts/ci_windows_cross.sh
|
||||
- name: Verify
|
||||
run: |
|
||||
ls -lah dist/windows-x86_64/
|
||||
file dist/windows-x86_64/cst-agent.exe dist/cst-agent-*-windows-x86_64-setup.exe
|
||||
cat dist/cst-agent-*-windows-x86_64.zip.sha256 dist/cst-agent-*-windows-x86_64-setup.exe.sha256
|
||||
unzip -l dist/cst-agent-*-windows-x86_64.zip
|
||||
- name: Publish Gitea Release
|
||||
if: github.event_name == 'push'
|
||||
env:
|
||||
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
CST_GITEA: ${{ github.server_url }}
|
||||
CST_REPO: ${{ github.repository }}
|
||||
GITHUB_REF: ${{ github.ref }}
|
||||
CI: "true"
|
||||
run: |
|
||||
export GITEA_TOKEN="${RELEASE_TOKEN:-${GITHUB_TOKEN:-}}"
|
||||
if [[ -z "${GITEA_TOKEN}" ]]; then
|
||||
echo "::warning::no RELEASE_TOKEN / GITHUB_TOKEN; skip auto-release"
|
||||
exit 0
|
||||
fi
|
||||
bash scripts/release.sh dist/cst-agent-*-windows-x86_64.zip \
|
||||
dist/cst-agent-*-windows-x86_64-setup.exe
|
||||
|
||||
+5
-1
@@ -26,8 +26,12 @@ third_party/ncurses-*.tar.gz
|
||||
third_party/busybox-w32/
|
||||
third_party/busybox-src/
|
||||
third_party/prefix-mingw/
|
||||
third_party/prefix-linux-*/
|
||||
third_party/build-linux-*/
|
||||
third_party/src/
|
||||
vendor/sashidown/*.o
|
||||
vendor/sashidown/src/*.o
|
||||
vendor/sashidown/libsashidown.a
|
||||
vendor/sashidown/libsashidown-mingw.a
|
||||
.gen/models_builtin.inc
|
||||
.gen/
|
||||
.build-tmp/
|
||||
|
||||
+76
-12
@@ -5,6 +5,7 @@ set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
|
||||
option(CST_BUILD_TUI "Build ncurses/PDCurses TUI" ON)
|
||||
option(CST_BUILD_WEBUI "Build the HTTP/SSE WebUI (--web)" ON)
|
||||
option(CST_NATIVE_ARCH "Pass -march=native to sashidown" OFF)
|
||||
option(CST_BUNDLE_BUSYBOX "Install placeholder/scripts for busybox beside binary" ON)
|
||||
|
||||
@@ -94,6 +95,7 @@ set(CST_CORE_SOURCES
|
||||
src/subagent.c
|
||||
src/attach.c
|
||||
src/clipboard.c
|
||||
src/pdf_text.c
|
||||
src/session.c
|
||||
src/tools/tools.c
|
||||
src/tools/patch.c
|
||||
@@ -110,26 +112,73 @@ set(CST_CORE_SOURCES
|
||||
src/agent_store.c
|
||||
src/json_baseline.c
|
||||
src/creds.c
|
||||
src/provider_switch.c
|
||||
src/mm_tool.c
|
||||
src/models.c
|
||||
src/model_registry.c
|
||||
src/metrics.c
|
||||
src/net.c
|
||||
src/http_srv.c
|
||||
src/web_md.c
|
||||
)
|
||||
|
||||
# Built-in model data as a C byte list (included by model_registry.c).
|
||||
set(CST_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen")
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/models_builtin.json")
|
||||
|
||||
# The WebUI shell (web/*) as C byte arrays, the same trick as the model data.
|
||||
file(GLOB CST_WEB_ASSETS "${CMAKE_CURRENT_SOURCE_DIR}/web/*")
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CST_WEB_ASSETS})
|
||||
set(_cst_web_inc "/* Generated by CMake from web/ - do not edit. */\n")
|
||||
set(_cst_web_table "")
|
||||
foreach(_f ${CST_WEB_ASSETS})
|
||||
if(IS_DIRECTORY "${_f}")
|
||||
continue()
|
||||
endif()
|
||||
get_filename_component(_name "${_f}" NAME)
|
||||
string(REGEX REPLACE "[^A-Za-z0-9]" "_" _var "wui_asset_${_name}")
|
||||
file(READ "${_f}" _hex HEX)
|
||||
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," _hex "${_hex}")
|
||||
set(_ctype "application/octet-stream")
|
||||
if(_name MATCHES "\\.html$")
|
||||
set(_ctype "text/html; charset=utf-8")
|
||||
elseif(_name MATCHES "\\.js$")
|
||||
set(_ctype "text/javascript; charset=utf-8")
|
||||
elseif(_name MATCHES "\\.css$")
|
||||
set(_ctype "text/css; charset=utf-8")
|
||||
elseif(_name MATCHES "\\.svg$")
|
||||
set(_ctype "image/svg+xml")
|
||||
endif()
|
||||
string(APPEND _cst_web_inc "static const unsigned char ${_var}[] = {${_hex}0x00};\n")
|
||||
string(APPEND _cst_web_table " {\"${_name}\", \"${_ctype}\", ${_var}, sizeof(${_var}) - 1},\n")
|
||||
endforeach()
|
||||
string(APPEND _cst_web_inc "static const wui_asset_t wui_assets[] = {\n${_cst_web_table}};\n")
|
||||
string(APPEND _cst_web_inc "static const int wui_asset_count = (int)(sizeof(wui_assets) / sizeof(wui_assets[0]));\n")
|
||||
file(WRITE "${CST_GEN_DIR}/web_assets.inc" "${_cst_web_inc}")
|
||||
if(NOT CST_BUILD_WEBUI)
|
||||
list(APPEND CST_CORE_SOURCES src/webui_stub.c)
|
||||
else()
|
||||
list(APPEND CST_CORE_SOURCES src/webui.c)
|
||||
endif()
|
||||
|
||||
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/models_builtin.json" _cst_models_hex HEX)
|
||||
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," _cst_models_hex "${_cst_models_hex}")
|
||||
file(WRITE "${CST_GEN_DIR}/models_builtin.inc" "${_cst_models_hex}\n")
|
||||
include_directories("${CST_GEN_DIR}")
|
||||
|
||||
if(NOT CST_BUILD_WEBUI)
|
||||
add_compile_definitions(CST_NO_WEBUI=1)
|
||||
endif()
|
||||
if(WIN32)
|
||||
# MinGW has no <regex.h>: musl's TRE engine, UTF-8 aware (vendor/tre-regex)
|
||||
include_directories(vendor/tre-regex)
|
||||
list(APPEND CST_CORE_SOURCES
|
||||
set(_CST_TRE_SOURCES
|
||||
vendor/tre-regex/regcomp.c vendor/tre-regex/regexec.c
|
||||
vendor/tre-regex/regerror.c vendor/tre-regex/tre-mem.c)
|
||||
list(APPEND CST_CORE_SOURCES ${_CST_TRE_SOURCES})
|
||||
# Upstream code, kept as is: CST_WIN_CFLAGS=-Werror must not apply to it.
|
||||
set_source_files_properties(${_CST_TRE_SOURCES} PROPERTIES COMPILE_OPTIONS "-w")
|
||||
endif()
|
||||
|
||||
set(CST_SOURCES ${CST_CORE_SOURCES} src/main.c)
|
||||
@@ -231,7 +280,7 @@ endif()
|
||||
enable_testing()
|
||||
set(_CST_SMALL_TESTS test_ctl_frontier test_ctl_heredoc test_json_baseline
|
||||
test_replay_starvation test_replay_real test_mm_tool test_textsel
|
||||
test_markdown)
|
||||
test_pdf_text test_markdown)
|
||||
foreach(t ${_CST_SMALL_TESTS})
|
||||
if(t STREQUAL test_textsel)
|
||||
add_executable(${t} tests/${t}.c src/textsel.c src/os_compat.c src/os_win.c
|
||||
@@ -239,6 +288,12 @@ foreach(t ${_CST_SMALL_TESTS})
|
||||
if(WIN32)
|
||||
target_link_libraries(${t} PRIVATE advapi32 user32)
|
||||
endif()
|
||||
elseif(t STREQUAL test_pdf_text)
|
||||
add_executable(${t} tests/${t}.c src/pdf_text.c src/os_compat.c src/os_win.c
|
||||
src/util.c)
|
||||
if(WIN32)
|
||||
target_link_libraries(${t} PRIVATE advapi32 user32)
|
||||
endif()
|
||||
elseif(t STREQUAL test_markdown)
|
||||
add_executable(${t} tests/${t}.c)
|
||||
target_include_directories(${t} PRIVATE "${SD_DIR}/include")
|
||||
@@ -262,24 +317,33 @@ foreach(t ${_CST_SMALL_TESTS})
|
||||
target_include_directories(${t} PRIVATE include "${CJSON_DIR}")
|
||||
target_compile_definitions(${t} PRIVATE _GNU_SOURCE)
|
||||
if(NOT WIN32)
|
||||
target_link_libraries(${t} PRIVATE m)
|
||||
target_link_libraries(${t} PRIVATE m Threads::Threads)
|
||||
endif()
|
||||
cst_win_exe(${t})
|
||||
add_test(NAME ${t} COMMAND ${t} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
endforeach()
|
||||
|
||||
# Tests that link the whole core (no TUI).
|
||||
foreach(t test_llm_body test_prefix_stable test_sse_decode test_apply_patch
|
||||
test_session_roundtrip test_attach
|
||||
test_shell test_diff test_tools_fs test_registry
|
||||
test_undo test_config test_models test_model_registry test_sandbox
|
||||
test_keymap test_shell_compact test_skills
|
||||
test_plugins test_mcp test_subagent test_lowil
|
||||
test_win_utf8 test_os_dir test_tmpdir)
|
||||
set(_CST_CORE_TESTS test_llm_body test_prefix_stable test_sse_decode
|
||||
test_apply_patch test_session_roundtrip test_attach
|
||||
test_shell test_diff test_tools_fs test_registry
|
||||
test_undo test_config test_models test_model_registry test_sandbox
|
||||
test_keymap test_shell_compact test_skills
|
||||
test_plugins test_mcp test_subagent test_lowil
|
||||
test_win_utf8 test_os_dir test_tmpdir)
|
||||
if(CST_BUILD_WEBUI)
|
||||
# test_webui drives the real server; the stub has nothing to test.
|
||||
list(APPEND _CST_CORE_TESTS test_webui)
|
||||
endif()
|
||||
foreach(t ${_CST_CORE_TESTS})
|
||||
add_executable(${t} tests/${t}.c ${CST_CORE_SOURCES})
|
||||
target_include_directories(${t} PRIVATE include "${CJSON_DIR}")
|
||||
# The core now contains src/web_md.c, which needs sashidown's header and
|
||||
# archive exactly like the Makefile build does.
|
||||
target_include_directories(${t} PRIVATE include "${CJSON_DIR}" "${SD_DIR}/include")
|
||||
target_compile_definitions(${t} PRIVATE _GNU_SOURCE)
|
||||
target_link_libraries(${t} PRIVATE CURL::libcurl Threads::Threads)
|
||||
target_link_libraries(${t} PRIVATE
|
||||
"${SASHIDOWN_A}" CURL::libcurl Threads::Threads)
|
||||
add_dependencies(${t} sashidown)
|
||||
if(NOT WIN32)
|
||||
target_link_libraries(${t} PRIVATE m)
|
||||
else()
|
||||
|
||||
@@ -7,6 +7,7 @@ PREFIX ?= /usr/local
|
||||
DESTDIR ?=
|
||||
UNAME_S := $(shell uname -s 2>/dev/null)
|
||||
UNAME_O := $(shell uname -o 2>/dev/null)
|
||||
comma := ,
|
||||
|
||||
# Android (Termux): pthreads live in libc.
|
||||
PTHREAD_LIBS := -lpthread
|
||||
@@ -26,6 +27,15 @@ CPPFLAGS += -D_DARWIN_C_SOURCE
|
||||
endif
|
||||
PKGC = $(PKG_ENV) $(PKG_CONFIG)
|
||||
|
||||
# STATIC=1: fully static link (the musl release builds).
|
||||
STATIC ?= 0
|
||||
LINK_STATIC :=
|
||||
ifeq ($(STATIC),1)
|
||||
PKGC += --static
|
||||
LINK_STATIC := -static
|
||||
endif
|
||||
LDFLAGS += $(LINK_STATIC)
|
||||
|
||||
# Wide-character curses: ncursesw, else ncurses (macOS/BSD build it wide).
|
||||
CURSES_PC := $(shell for p in ncursesw ncurses; do $(PKGC) --exists $$p 2>/dev/null && { echo $$p; exit 0; }; done)
|
||||
ifneq ($(CURSES_PC),)
|
||||
@@ -43,12 +53,37 @@ ifeq ($(strip $(CURL_LIBS)),)
|
||||
CURL_LIBS := -lcurl
|
||||
endif
|
||||
|
||||
# WebUI (HTTP + SSE front end): src/net.c src/http_srv.c src/web_md.c
|
||||
# src/webui.c. CST_WEBUI=0 swaps in the stub (--web then prints a hint).
|
||||
CST_WEBUI ?= 1
|
||||
|
||||
PKG_CFLAGS := $(CURL_CFLAGS) $(CURSES_CFLAGS)
|
||||
PKG_LIBS := $(CURL_LIBS) $(CURSES_LIBS)
|
||||
|
||||
CPPFLAGS += -Iinclude -I.gen -I$(SD_DIR)/include -I$(CJSON_DIR) -D_XOPEN_SOURCE_EXTENDED $(PKG_CFLAGS)
|
||||
# tcc writes no rpath of its own, so shared libs found via pkg-config -L
|
||||
# (Nix store, /opt prefixes) would not load at run time.
|
||||
RPATH_LIBS :=
|
||||
ifneq ($(findstring tcc,$(notdir $(CC))),)
|
||||
ifneq ($(STATIC),1)
|
||||
RPATH_LIBS := $(patsubst -L%,-Wl$(comma)-rpath$(comma)%,$(filter -L%,$(PKG_LIBS)))
|
||||
endif
|
||||
endif
|
||||
|
||||
# Keep POSIX declarations visible for shared test helpers (notably symlink()).
|
||||
CPPFLAGS += -Iinclude -I.gen -I$(SD_DIR)/include -I$(CJSON_DIR) -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE_EXTENDED $(PKG_CFLAGS)
|
||||
LDFLAGS += -L$(SD_DIR)
|
||||
LDLIBS += -lsashidown -lm $(PKG_LIBS) $(PTHREAD_LIBS)
|
||||
LDLIBS += -lsashidown -lm $(PKG_LIBS) $(PTHREAD_LIBS) $(RPATH_LIBS)
|
||||
|
||||
WEB_DIR := web
|
||||
WEB_ASSETS := $(wildcard $(WEB_DIR)/*.html) $(wildcard $(WEB_DIR)/*.js) \
|
||||
$(wildcard $(WEB_DIR)/*.css) $(wildcard $(WEB_DIR)/*.svg)
|
||||
ifeq ($(CST_WEBUI),0)
|
||||
WEB_SRC := src/net.c src/http_srv.c src/web_md.c src/webui_stub.c
|
||||
# The stub body is guarded by this, so the switch has to define it too.
|
||||
CPPFLAGS += -DCST_NO_WEBUI=1
|
||||
else
|
||||
WEB_SRC := src/net.c src/http_srv.c src/web_md.c src/webui.c
|
||||
endif
|
||||
|
||||
CORE_SRC = \
|
||||
$(CJSON_DIR)/cJSON.c \
|
||||
@@ -61,6 +96,7 @@ CORE_SRC = \
|
||||
src/keymap.c \
|
||||
src/skills.c \
|
||||
src/extensions.c src/plugins.c src/mcp.c src/subagent.c src/attach.c src/clipboard.c \
|
||||
src/pdf_text.c \
|
||||
src/session.c \
|
||||
src/tools/tools.c \
|
||||
src/tools/patch.c \
|
||||
@@ -76,15 +112,16 @@ CORE_SRC = \
|
||||
src/agent_loop.c \
|
||||
src/agent_store.c \
|
||||
src/json_baseline.c \
|
||||
src/creds.c \
|
||||
src/creds.c src/provider_switch.c \
|
||||
src/mm_tool.c \
|
||||
src/models.c src/model_registry.c \
|
||||
src/metrics.c
|
||||
src/metrics.c \
|
||||
$(WEB_SRC)
|
||||
|
||||
SRC = $(CORE_SRC) src/textsel.c src/tui.c src/main.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
HDRS = $(wildcard include/*.h) $(wildcard src/tools/*.h) $(CJSON_DIR)/cJSON.h \
|
||||
.gen/models_builtin.inc
|
||||
.gen/models_builtin.inc .gen/web_assets.inc
|
||||
|
||||
TESTS = \
|
||||
tests/test_ctl_frontier \
|
||||
@@ -99,6 +136,7 @@ TESTS = \
|
||||
tests/test_apply_patch \
|
||||
tests/test_session_roundtrip \
|
||||
tests/test_attach \
|
||||
tests/test_pdf_text \
|
||||
tests/test_shell \
|
||||
tests/test_diff \
|
||||
tests/test_tools_fs \
|
||||
@@ -120,8 +158,16 @@ TESTS = \
|
||||
tests/test_win_utf8 \
|
||||
tests/test_os_dir \
|
||||
tests/test_tmpdir
|
||||
# test_webui drives the real server; there is nothing to test with the stub.
|
||||
ifneq ($(CST_WEBUI),0)
|
||||
TESTS += tests/test_webui
|
||||
endif
|
||||
|
||||
TEST_LIBS = -lm $(CURL_LIBS) $(PTHREAD_LIBS)
|
||||
TEST_SYS_LIBS = -lm $(PTHREAD_LIBS) $(LINK_STATIC)
|
||||
TEST_LIBS = -L$(SD_DIR) -lsashidown $(CURL_LIBS) $(RPATH_LIBS) $(TEST_SYS_LIBS)
|
||||
# cst_test_util.h has static os_* helpers; gcc drops them when unused, tcc
|
||||
# keeps them, so the small tests link the OS layer too.
|
||||
TEST_OS_SRC = src/os_compat.c src/os_win.c src/util.c
|
||||
|
||||
.PHONY: all clean check demo sashidown-lib FORCE cmake-gcc cmake-clang asan install uninstall
|
||||
|
||||
@@ -153,32 +199,40 @@ cst-agent: $(OBJ) $(SD_DIR)/libsashidown.a
|
||||
@mkdir -p .gen
|
||||
od -An -v -tx1 $< | sed 's/\([0-9a-f][0-9a-f]\)/0x\1,/g' > $@.tmp && mv $@.tmp $@
|
||||
|
||||
# The WebUI shell is embedded the same way, one array per file.
|
||||
.gen/web_assets.inc: $(WEB_ASSETS) scripts/embed_web.sh
|
||||
@mkdir -p .gen
|
||||
@sh scripts/embed_web.sh $@ $(WEB_DIR)
|
||||
|
||||
%.o: %.c $(HDRS)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
|
||||
|
||||
src/tui.o: src/tui.c $(HDRS) $(SD_DIR)/include/sashidown.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
|
||||
|
||||
tests/test_ctl_frontier: tests/test_ctl_frontier.c src/ctl_parse.c include/ctl.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_ctl_frontier.c src/ctl_parse.c
|
||||
tests/test_ctl_frontier: tests/test_ctl_frontier.c src/ctl_parse.c include/ctl.h $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_ctl_frontier.c src/ctl_parse.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_ctl_heredoc: tests/test_ctl_heredoc.c src/ctl_parse.c include/ctl.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_ctl_heredoc.c src/ctl_parse.c
|
||||
tests/test_ctl_heredoc: tests/test_ctl_heredoc.c src/ctl_parse.c include/ctl.h $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_ctl_heredoc.c src/ctl_parse.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_json_baseline: tests/test_json_baseline.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(HDRS)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_json_baseline.c src/json_baseline.c $(CJSON_DIR)/cJSON.c -lm
|
||||
tests/test_json_baseline: tests/test_json_baseline.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(HDRS) $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_json_baseline.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_replay_starvation: tests/test_replay_starvation.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_replay_starvation.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c -lm
|
||||
tests/test_replay_starvation: tests/test_replay_starvation.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_replay_starvation.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_replay_real: tests/test_replay_real.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_replay_real.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c -lm
|
||||
tests/test_replay_real: tests/test_replay_real.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_replay_real.c src/ctl_parse.c src/json_baseline.c $(CJSON_DIR)/cJSON.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_mm_tool: tests/test_mm_tool.c src/mm_tool.c include/mm_tool.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_mm_tool.c src/mm_tool.c
|
||||
tests/test_mm_tool: tests/test_mm_tool.c src/mm_tool.c include/mm_tool.h $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_mm_tool.c src/mm_tool.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_textsel: tests/test_textsel.c src/textsel.c src/os_compat.c src/os_win.c src/util.c include/cst_textsel.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_textsel.c src/textsel.c src/os_compat.c src/os_win.c src/util.c
|
||||
tests/test_textsel: tests/test_textsel.c src/textsel.c $(TEST_OS_SRC) include/cst_textsel.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_textsel.c src/textsel.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_pdf_text: tests/test_pdf_text.c src/pdf_text.c include/cst_pdf.h $(TEST_OS_SRC)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_pdf_text.c src/pdf_text.c $(TEST_OS_SRC) $(TEST_SYS_LIBS)
|
||||
|
||||
tests/test_markdown: tests/test_markdown.c $(SD_DIR)/libsashidown.a
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ tests/test_markdown.c $(LDFLAGS) -lsashidown -lm
|
||||
@@ -186,10 +240,14 @@ tests/test_markdown: tests/test_markdown.c $(SD_DIR)/libsashidown.a
|
||||
tests/test_%: tests/test_%.c $(CORE_SRC) $(HDRS)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $< $(CORE_SRC) $(TEST_LIBS)
|
||||
|
||||
$(TESTS): sashidown-lib
|
||||
|
||||
check: $(TESTS)
|
||||
@set -e; for t in $(TESTS); do echo "== $$t"; ./$$t; done
|
||||
|
||||
# AddressSanitizer + UBSan build of the agent and all tests.
|
||||
# Under a ptrace-based sandbox (some Termux setups) LeakSanitizer aborts with
|
||||
# "Tracer caught signal 11"; add ASAN_OPTIONS=detect_leaks=0 there.
|
||||
asan:
|
||||
$(MAKE) clean
|
||||
$(MAKE) sashidown-lib
|
||||
@@ -199,7 +257,7 @@ demo: cst-agent
|
||||
./cst-agent --demo-ctl --workspace .
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) cst-agent $(TESTS) .gen/models_builtin.inc
|
||||
rm -f $(OBJ) cst-agent $(TESTS) .gen/models_builtin.inc .gen/web_assets.inc
|
||||
|
||||
install: cst-agent
|
||||
install -d "$(DESTDIR)$(PREFIX)/bin"
|
||||
|
||||
@@ -27,6 +27,10 @@ cst-agent --version
|
||||
cd ~/my-project && cst-agent
|
||||
```
|
||||
|
||||
不想编译:[Release](https://git.whcself.xyz/WHC/CST-agent/releases) 里有静态链接的 Linux 单文件
|
||||
(`cst-agent-VERSION-linux-{x86_64,x86,aarch64}.tar.gz`,musl,任何发行版都能直接跑),解压后把 `cst-agent`
|
||||
放进 `PATH` 即可;每个文件旁边有 `.sha256`。
|
||||
|
||||
Windows 10 1903+(PowerShell,不需要管理员):
|
||||
|
||||
```powershell
|
||||
@@ -34,7 +38,8 @@ irm https://git.whcself.xyz/WHC/CST-agent/raw/branch/main/scripts/install_window
|
||||
# 新开终端后:cd 到项目目录运行 cst-agent;升级用 cst-agent-update
|
||||
```
|
||||
|
||||
详见 [docs/README-windows.md](docs/README-windows.md)。
|
||||
也可以下载 Release 里的安装包 `cst-agent-VERSION-windows-x86_64-setup.exe`(按用户安装、自动加 PATH、
|
||||
开始菜单有 WebUI 入口),或便携版 zip。详见 [docs/README-windows.md](docs/README-windows.md)。
|
||||
|
||||
一键脚本可以重复运行:已有仓库会 `git pull --ff-only`,代码有更新时干净重编,然后重新安装;
|
||||
同时装一个 `cst-agent-update` 命令(等同重跑脚本)。NixOS 上会自动进 `nix-shell` 编译。
|
||||
@@ -49,7 +54,8 @@ irm https://git.whcself.xyz/WHC/CST-agent/raw/branch/main/scripts/install_window
|
||||
| Linux | [`scripts/bootstrap_linux.sh`](scripts/bootstrap_linux.sh) |
|
||||
| Termux | [`scripts/bootstrap_termux.sh`](scripts/bootstrap_termux.sh) |
|
||||
| Windows 安装 / 升级 | [`scripts/install_windows.ps1`](scripts/install_windows.ps1)(从 Release 安装),之后 `cst-agent-update` |
|
||||
| Windows 交叉构建(在 Linux 上) | [`scripts/bootstrap_windows.sh`](scripts/bootstrap_windows.sh) → `dist/windows-x86_64/` |
|
||||
| Linux 静态发行包(musl 交叉) | [`scripts/package_linux.sh`](scripts/package_linux.sh) `x86_64\|x86\|aarch64` → `dist/*.tar.gz` |
|
||||
| Windows 交叉构建(在 Linux 上) | [`scripts/bootstrap_windows.sh`](scripts/bootstrap_windows.sh) → `dist/windows-x86_64/`、zip 和安装包 |
|
||||
| Windows 原生构建(MSYS2 UCRT64) | [`scripts/bootstrap_msys2.sh`](scripts/bootstrap_msys2.sh) → `dist/windows-x86_64/` |
|
||||
|
||||
### 迷你验收工程(论文案例)
|
||||
@@ -86,6 +92,7 @@ cd CST-agent
|
||||
make # 依赖 pkg-config 找 libcurl / ncursesw(找不到时退回 ncurses)
|
||||
make check # 单元测试 + 截断实验(含真实录制流)
|
||||
make asan # AddressSanitizer + UBSan 构建并跑全部测试
|
||||
make CC=tcc AR=ar check # TinyCC:整套构建 + 测试十几秒(CI 的校验就是这条)
|
||||
make install PREFIX=$HOME/.local # 或 sudo make install(默认 /usr/local)
|
||||
```
|
||||
|
||||
@@ -111,7 +118,7 @@ cmake -B build-hd -DCST_BUILD_TUI=OFF && cmake --build build-hd
|
||||
./scripts/bootstrap_windows.sh
|
||||
```
|
||||
|
||||
依赖:`gcc` 或 `clang`、`libcurl`、TUI 需 `ncursesw`(Windows 版用打过补丁的 ncurses 6.6,脚本自动编译)。
|
||||
依赖:`gcc`、`clang` 或 `tcc`(0.9.27 mob 以上)、`libcurl`、TUI 需 `ncursesw`(Windows 版用打过补丁的 ncurses 6.6,脚本自动编译)。
|
||||
Sashidown(`vendor/sashidown`)与 cJSON(`vendor/cjson`,MIT)已随仓库提供。
|
||||
API Key:TUI 里输入并按 provider 保存到配置目录的 `credentials`(权限 0600);`/key` 更换,`--ask-key` 在命令行输入。
|
||||
`--api-key` 只用于本次进程、不落盘;`DEEPSEEK_API_KEY` 等环境变量和 `CST_API_KEY` 仅作为没有保存 key 时的兜底(方便 CI)。
|
||||
@@ -120,12 +127,29 @@ API Key:TUI 里输入并按 provider 保存到配置目录的 `credentials`(
|
||||
|
||||
| OS | Arch | 编译器 | Headless | TUI | Shell |
|
||||
|---|---|---|---|---|---|
|
||||
| Linux(glibc / musl) | x86_64 / aarch64 | GCC, Clang | yes | ncursesw | `/bin/sh` 或旁路 `busybox` |
|
||||
| Linux(glibc / musl) | x86_64 / x86 / aarch64 | GCC, Clang, TinyCC | yes | ncursesw | `/bin/sh` 或旁路 `busybox` |
|
||||
| WSL2 | x86_64 | 同 Linux | yes | ncursesw | `/bin/sh`(工作区建议放在 Linux 文件系统内) |
|
||||
| macOS | arm64 / x86_64 | Apple Clang | yes | ncurses(Homebrew) | `/bin/sh` |
|
||||
| Android Termux | aarch64 / … | Clang (Termux) | yes | ncurses(Termux 包) | `$PREFIX/bin/sh`;可旁路 busybox |
|
||||
| Windows 10 1903+ | x86_64 | MinGW-w64 GCC(UCRT) | yes | ncurses 6.6(Windows 控制台) | **捆绑 busybox-w32** |
|
||||
|
||||
### WebUI(`--web`)
|
||||
|
||||
同一个二进制内置一个 HTTP + SSE 前端:`cst-agent --web`(默认 `http://127.0.0.1:8787/`)。
|
||||
它和 TUI 是同一引擎的两个前端,浏览器里能流式看回答、点审批、切会话、贴图。
|
||||
协议是一份稳定的 JSON + SSE 接口,供以后的专用客户端复用。详见 [docs/webui.md](docs/webui.md)。
|
||||
|
||||
```bash
|
||||
cst-agent --web # 本机浏览器打开 http://127.0.0.1:8787/
|
||||
cst-agent --web --web-port 0 # 端口交给系统,启动日志里给 URL
|
||||
cst-agent --web --web-dev ./web # 开发前端:直接读目录,改完刷新
|
||||
cst-agent --web --web-host 0.0.0.0 --web-token 口令 # 手机/局域网访问
|
||||
sh scripts/e2e_webui.sh # 用录制流跑完整轮次,不需要 API key
|
||||
```
|
||||
|
||||
不带 TUI 的构建也可以开 WebUI(`cmake -DCST_BUILD_WEBUI=ON -DCST_BUILD_TUI=OFF`);
|
||||
`-DCST_BUILD_WEBUI=OFF` 时 `--web` 只打印一条提示。
|
||||
|
||||
### Termux
|
||||
|
||||
```bash
|
||||
@@ -147,6 +171,18 @@ make && make install # 装到 Termux 的 $PREFIX/bin,之后任意目录
|
||||
临时目录:依次试 `$TMPDIR`、`$PREFIX/tmp`、`/tmp`、`/var/tmp`、`~/.cache/cst-agent/tmp`,用第一个真能建文件的,
|
||||
并写回 `TMPDIR` 给 shell 子进程和沙箱用(Termux 的 `/tmp` 常常不存在或被 SELinux 拒绝)。
|
||||
|
||||
### Linux 静态发行包
|
||||
|
||||
```bash
|
||||
scripts/package_linux.sh x86_64 # 或 x86 / aarch64;CST_LINUX_CHECK=1 顺便跑 make check
|
||||
```
|
||||
|
||||
用 Bootlin 的 musl 交叉 gcc(固定版本、校验 sha256)静态编译 mbedTLS、curl 8.22(只开 HTTP/HTTPS)和宽字符 ncurses,
|
||||
再以 `STATIC=1` 和 `-Werror` 编出 `cst-agent`,产物是 `dist/cst-agent-VERSION-linux-ARCH.tar.gz`(+ `.sha256`)。
|
||||
宿主需要 `cmake ninja make perl` 以及 `tic`/`infocmp` 和终端条目(Debian:`ncurses-bin ncurses-term`)。
|
||||
ncurses 会去常见目录找 terminfo,找不到时用编进去的 `xterm-256color`、`xterm`、`screen-256color`、`tmux-256color`、`linux`、`vt100`。
|
||||
CA 证书:`CST_CA_BUNDLE` → `SSL_CERT_FILE` → 各发行版的常见位置(Debian/Ubuntu、Fedora/RHEL、openSUSE、Alpine)。
|
||||
|
||||
### Windows 发行包
|
||||
|
||||
见 [docs/README-windows.md](docs/README-windows.md)(安装、编码、沙箱、构建、已知限制)。
|
||||
@@ -155,10 +191,17 @@ make && make install # 装到 Termux 的 $PREFIX/bin,之后任意目录
|
||||
./scripts/bootstrap_windows.sh # Linux 交叉编译;Windows 上在 MSYS2 UCRT64 里用 scripts/bootstrap_msys2.sh
|
||||
```
|
||||
|
||||
产物:`dist/windows-x86_64/`(静态链接的 `cst-agent.exe` + `busybox.exe`,无其他 DLL)和
|
||||
`dist/cst-agent-VERSION-windows-x86_64.zip`。CI 在 `main` / `v*` tag 推送且交叉构建成功后会自动上传到
|
||||
Gitea Release `vVERSION`(同名资源会被替换);仓库可设 `RELEASE_TOKEN`(`write:repository`),否则用任务自带 token。
|
||||
本地:`GITEA_TOKEN=... scripts/release_windows.sh`。
|
||||
产物:`dist/windows-x86_64/`(静态链接的 `cst-agent.exe` + `busybox.exe`,无其他 DLL)、
|
||||
`dist/cst-agent-VERSION-windows-x86_64.zip`,以及装了 `makensis` 时的安装包 `dist/cst-agent-VERSION-windows-x86_64-setup.exe`。
|
||||
|
||||
### CI 与 Release
|
||||
|
||||
CI 用 TinyCC 校验(构建、`make check`、WebUI 端到端),通过后用 gcc(`-Werror`)出发行包:
|
||||
Linux x86_64 / x86 / aarch64 静态包(x86 两种跑 `make check`,aarch64 用 qemu 冒烟),
|
||||
Windows x86_64 的 zip 和安装包(Debian 的 mingw-w64 UCRT gcc + NSIS)。
|
||||
推送到 `main` 或 `v*` tag 时每个任务把自己的产物上传到 Gitea Release `vVERSION`(同名资源会被替换);
|
||||
仓库可设 `RELEASE_TOKEN`(`write:repository`),否则用任务自带 token。
|
||||
本地:`GITEA_TOKEN=... scripts/release.sh dist/cst-agent-*.tar.gz dist/cst-agent-*.zip dist/cst-agent-*-setup.exe`。
|
||||
|
||||
环境变量:`CST_SHELL`、`CST_EXE_DIR`、`CST_AUTO_APPROVE` 等。
|
||||
|
||||
@@ -190,18 +233,20 @@ Gitea Release `vVERSION`(同名资源会被替换);仓库可设 `RELEASE_T
|
||||
|---|---|
|
||||
| `CST_PROTOCOL` / `--protocol` | 工具调用协议(`native-eos`) |
|
||||
| `CST_TOOLS` / `--tools` | 工具集:`default`(全部)/ `minimal`(论文 4 件套) |
|
||||
| `CST_MODE` / `--mode` | 工作模式:`coding`(默认,代码口径)/ `general`(通用助手口径) |
|
||||
| `CST_EFFORT` / `--effort` | 推理强度 `low` / `medium` / `high` / `max` |
|
||||
| `CST_CONFIRM` / `--confirm` | 哪些类别要确认:`shell,write,net`(默认)或 `none` |
|
||||
| `CST_MAX_STEPS` / `--max-steps` | 每个用户回合最多调用模型几次(30) |
|
||||
| `CST_TOOL_OUT_CAP` | 单个工具输出发给模型的字节上限(16384,保留头 60% + 尾 40%) |
|
||||
| `CST_CTX_WINDOW` | 发给模型的最近非 system 消息条数(80) |
|
||||
| `CST_CTX_TOKENS` / `CST_AUTO_COMPACT=0` | 覆盖模型上下文窗口大小 / 关闭自动压缩(用到 80% 时自动 `/compact`) |
|
||||
| `CST_CTX_TOKENS` / `CST_AUTO_COMPACT=0` | 覆盖模型上下文窗口大小 / 关闭自动压缩(用到 75% 时自动 `/compact`) |
|
||||
| `CST_NO_PROJECT_NOTES=1` | 不把工作区的 `AGENTS.md` 注入 system prompt |
|
||||
| `NO_COLOR` / `CST_ASCII=1` | 关闭颜色 / 强制 ASCII 图形(非 UTF-8 终端自动退化) |
|
||||
| `CST_SHELL_TIMEOUT` | `run_shell` 超时秒数(120),超时整组进程被杀,exit=124 |
|
||||
| `CST_SHELL_TIMEOUT` | `run_shell` 超时秒数(默认不限),设了才在超时后整组杀进程,exit=124 |
|
||||
| `CST_METRICS=0` / `CST_METRICS_PATH` | 关闭 / 指定计时埋点 JSONL(默认 `.agent/metrics/<session>.jsonl`) |
|
||||
| `CST_RECORD_SSE=DIR` / `CST_REPLAY_SSE=DIR` | 录制 / 回放原始 SSE 流(`NNNN.sse` + `NNNN.req.json`) |
|
||||
| `CST_APPROVAL=ask\|auto\|yolo` / `CST_SANDBOX=off\|landlock\|seccomp` | 审批模式 / 关闭沙箱(回落到只读白名单规则)或只用指定沙箱 |
|
||||
| `CST_CA_BUNDLE` | HTTPS 用的 CA 证书文件(默认:`SSL_CERT_FILE`、libcurl 自带路径、常见发行版位置;Windows 用系统证书库) |
|
||||
| `CST_MODELS`、`CST_AUTO_APPROVE=1`、`CST_SHELL`、`CST_EXE_DIR` | 模型列表、自动批准(= yolo)、shell、旁路 busybox |
|
||||
|
||||
## Run
|
||||
@@ -232,6 +277,7 @@ cst-agent --headless --prompt 'List files'
|
||||
| `-y` / `--yolo` | 等同 `--approval yolo`,只对本次运行有效 |
|
||||
| `-C LIST` / `--confirm LIST` | 需要确认的类别:`shell,write,net` 的任意组合或 `none` |
|
||||
| `-T PROFILE` / `--tools PROFILE` | `default` 或 `minimal` |
|
||||
| `-M MODE` / `--mode MODE` | 工作模式 `coding`(默认)/ `general`,也读 `CST_MODE`,并记进 `settings.json` |
|
||||
| `-P NAME` / `-m NAME` / `-e LEVEL` | 本次使用的 provider / 模型 / 推理强度(不传则用上次的) |
|
||||
| `-a` / `--ask-key` | 在命令行输入并保存当前 provider 的 key |
|
||||
|
||||
@@ -253,6 +299,8 @@ cst-agent --headless --prompt 'List files'
|
||||
| `delete_file` / `move_file` | 写 | 删除 / 移动(重命名)文件 |
|
||||
| `run_shell` | shell | 非交互命令,超时整组进程被杀,Esc 可中断 |
|
||||
| `web_fetch` | 网络 | 抓取 URL,HTML 转纯文本 |
|
||||
| `clipboard_read` | 读 | 读系统剪贴板里的文本(`max_bytes` 默认 64 KiB,超出截断并说明) |
|
||||
| `clipboard_write` | 写 | 把文本放进系统剪贴板(按写类权限确认;本机走 pbcopy / wl-copy / xclip / Win32 API) |
|
||||
| `todo_write` | 元 | 维护任务清单(TUI 右侧面板,Ctrl+T 开关) |
|
||||
| `ask_user` | 询问 | 向用户提问(可给选项);headless 下返回错误让模型自行决定 |
|
||||
| `skill` | 元 | 加载一个 skill 的说明(`SKILL.md`)或其中的文件;有 skill 时才出现 |
|
||||
@@ -264,6 +312,26 @@ cst-agent --headless --prompt 'List files'
|
||||
`--tools minimal` 只保留 `read_file` / `run_shell` / `write_file` / `apply_patch`(论文实验口径)。
|
||||
TUI 中 `/tools off NAME`、`/tools on NAME` 可临时开关单个工具。
|
||||
|
||||
### 工作模式(coding / general)
|
||||
|
||||
`--mode general`(或 TUI 里 `/mode general`、WebUI 运行设置里的「工作模式」、`CST_MODE=general`)把默认口径
|
||||
从"代码助手"换成"通用助手":工作流从改代码/跑测试换成"先弄清要什么、再动手、给出处和不确定的地方",
|
||||
工具说明从"仓库"改成"文件与数据",并说明 `web_fetch` 可以拿外部资料;`/status` 第一行显示当前模式,
|
||||
切换后 history 里会留一条系统提示。默认仍是 `coding`,选择记在 `settings.json`;
|
||||
`--tools minimal`(论文口径)的提示词按字节冻结,不受模式影响。
|
||||
|
||||
### 剪贴板与文档附件
|
||||
|
||||
- **剪贴板**:`clipboard_read` / `clipboard_write` 两个工具(写剪贴板是不可逆发射,要确认);
|
||||
TUI 里 `/attach paste` 在剪贴板里没有图片时,把剪贴板的**文本**放进输入框(丢掉 CR 与控制字节,尊重输入长度上限)。
|
||||
- **附件**(`/attach <path>`、拖入终端、剪贴板图片都算):图片按原样存进会话目录,发给模型时是 image part;
|
||||
**PDF 在摄取时就抽出文本层**(零依赖解析器:Flate / ASCIIHex / ASCII85、对象流、CID 字体 ToUnicode、
|
||||
内联图像跳过),存的是文本、`mime` 仍是 `application/pdf`;其余文件必须是 UTF-8 文本。
|
||||
文档以 **text part 内联**发出,所以不受 `CST_NO_IMAGES` 或非视觉模型影响;图片照旧会被换成一行"未发送"说明。
|
||||
上限:单个附件 ≤16 MB(文档文本 ≤4 MiB),每条消息最多 8 个。
|
||||
加密 PDF、扫描件(无文本层)会被明确拒绝并说明原因;
|
||||
`view_image` 仍只收图片,文本/PDF 走 `/attach`。
|
||||
|
||||
### 权限、撤销与项目说明
|
||||
|
||||
- **审批模式**(Shift+Tab 循环切换,`/approval`,`--approval`,`CST_APPROVAL`):
|
||||
@@ -277,16 +345,16 @@ TUI 中 `/tools off NAME`、`/tools on NAME` 可临时开关单个工具。
|
||||
`ask` / `auto` 的选择会记在 `settings.json`;`yolo` 只在本次运行有效。headless 行为不变(全部自动)。
|
||||
- **沙箱**(C 实现,`src/sandbox_exec.c`):
|
||||
- Linux:Landlock(内核 5.13+,直接 syscall,无需 libseccomp)。子进程只能写工作区、临时目录、`/dev/null`、`/dev/tty`;
|
||||
读和执行不受限。Landlock ABI ≥ 4(内核 6.7+)时同时禁止 TCP 连接和监听。
|
||||
读、执行和联网不受限。
|
||||
- Linux 没有 Landlock 时(Android / Termux、WSL、老发行版;内核 5.5+,x86_64 / aarch64):seccomp user-notify
|
||||
(界面显示 `auto · seccomp`)。子进程的写类系统调用(带写标志的 open、创建 / 删除 / 改名 / 链接 / chmod 等)先交给
|
||||
agent 里的监督线程,按真实路径判断是否在工作区、临时目录或 `/dev/null` 等之内,不在就返回 `EACCES`;IPv4/IPv6 socket 一律拒绝。
|
||||
agent 里的监督线程,按真实路径判断是否在工作区、临时目录或 `/dev/null` 等之内,不在就返回 `EACCES`。联网不受限。
|
||||
读、执行和已打开文件的写入不检查。它防的是误操作而不是恶意程序:检查之后内核会再读一次路径,多线程程序可以趁机换掉。
|
||||
探测还会确认能读直接子进程的内存;`ptrace_scope=2` 且无 `CAP_SYS_PTRACE` 时 seccomp 不会启用。
|
||||
比 shell 活得久的后台进程:桌面发行版开着 Yama(`ptrace_scope` ≥ 1)时它们的受检写入会被拒;命令结束后一律失败。
|
||||
- macOS:`sandbox_init` profile,同样只放行工作区和临时目录写入,禁网。
|
||||
- macOS:`sandbox_init` profile,同样只放行工作区和临时目录写入,不限制联网。
|
||||
- Windows 10 1903+:Low 完整性级别的受限令牌 + Job Object(界面显示 `auto · lowil`)。第一次在工作区运行时
|
||||
给它打上 Low 标签,之后子进程只能写工作区和沙箱临时目录,读不到凭据文件;不限制联网,所以联网命令仍会询问。
|
||||
给它打上 Low 标签,之后子进程只能写工作区和沙箱临时目录,读不到凭据文件;不限制联网。
|
||||
`/sandbox` 查看、`/sandbox reset-labels` 撤销标签,详见 [docs/README-windows.md](docs/README-windows.md)。
|
||||
- 更老的内核:没有沙箱,自动回落到规则判断(界面显示 `auto · rules`)。
|
||||
- `CST_SANDBOX=off` 强制关闭沙箱;`CST_SANDBOX=landlock` / `seccomp` / `lowil` 只用指定的那种。
|
||||
@@ -302,7 +370,7 @@ TUI 中 `/tools off NAME`、`/tools on NAME` 可临时开关单个工具。
|
||||
`sudo`、`rm`、解释器(`python3 -c …`)等危险前缀不会被建议成规则。
|
||||
- **撤销**:每轮写文件前自动快照到 `.agent/undo/`,`/undo` 恢复上一轮改动的全部文件(新建的文件会被删除)。
|
||||
- **AGENTS.md**:工作区根目录的 `AGENTS.md` 会注入 system prompt;`/init` 让 agent 自己生成一份。
|
||||
- **自动压缩**:上一轮 prompt 用到模型上下文 80% 时,下一轮开始前自动总结历史(`CST_AUTO_COMPACT=0` 关闭)。
|
||||
- **自动压缩**:上一轮 prompt 用到模型上下文 75% 时,下一轮开始前自动总结历史(`CST_AUTO_COMPACT=0` 关闭)。
|
||||
|
||||
### 扩展:Skills、插件、MCP、子 agent
|
||||
|
||||
@@ -382,6 +450,7 @@ Thought(推理过程)默认折叠成一行“Thought · 3.2s”,生成中
|
||||
| `/provider [name]`、`/model [name]`、`/effort [level]` | 切换 provider(缺 key 时当场输入并用 `/models` 校验)、模型(列表优先显示 API 实际返回的模型,标 `(api)`)、推理强度;都会记住 |
|
||||
| `/key` | 输入或更换当前 provider 的 API key(遮罩输入并保存) |
|
||||
| `/protocol [name]` | 切换工具调用协议 |
|
||||
| `/mode [coding\|general]` | 工作模式(通用助手 / 代码助手口径) |
|
||||
| `/tools [on\|off NAME\|minimal\|default]` | 查看或开关工具 |
|
||||
| `/approval [ask\|auto\|yolo]` *、`/yolo` * | 审批模式;本次开关 yolo |
|
||||
| `/permissions [reset]` *、`/max-steps [N]` * | 权限规则、每轮步数 |
|
||||
@@ -391,6 +460,8 @@ Thought(推理过程)默认折叠成一行“Thought · 3.2s”,生成中
|
||||
| `/diff [path]` * | `git status` + `git diff` |
|
||||
| `/compact` | 手动压缩上下文 |
|
||||
| `/export [file]` *、`/copy` * | 导出 Markdown 记录;复制上一条回复(OSC 52,需终端支持) |
|
||||
| `/attach [path\|paste\|list\|clear]` | 给下一条消息带附件:图片、PDF(自动取文本层)、文本文件;`paste` 优先取剪贴板图片,没有就把剪贴板文本放进输入框 |
|
||||
| `/web` * | 后台起一个 WebUI 进程(同 workspace、新会话,日志写到 `.agent/webui.log`) |
|
||||
| `/init` | 让 agent 写 `AGENTS.md` |
|
||||
| `/details` *、`/thinking [on\|off]`、`/keys` * | 展开全部 tool 输出;Thought 默认展开/折叠;快捷键列表 |
|
||||
| `/skills [reload\|list]`、`/skill <name> [任务]` | 浏览 skills;带着某个 skill 开始一轮 |
|
||||
|
||||
@@ -2,17 +2,25 @@
|
||||
# ./scripts/build_third_party_mingw.sh curl
|
||||
# cmake -B build-win -DCMAKE_TOOLCHAIN_FILE=cmake/mingw-w64-x86_64.cmake \
|
||||
# -DCST_BUILD_TUI=OFF
|
||||
# $MINGW_HOST picks the tool prefix (default x86_64-w64-mingw32; Debian's UCRT
|
||||
# cross gcc is x86_64-w64-mingw32ucrt).
|
||||
set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(CMAKE_SYSTEM_PROCESSOR x86_64)
|
||||
|
||||
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
|
||||
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
|
||||
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)
|
||||
if(DEFINED ENV{MINGW_HOST} AND NOT "$ENV{MINGW_HOST}" STREQUAL "")
|
||||
set(_MINGW_HOST "$ENV{MINGW_HOST}")
|
||||
else()
|
||||
set(_MINGW_HOST x86_64-w64-mingw32)
|
||||
endif()
|
||||
|
||||
set(CMAKE_C_COMPILER ${_MINGW_HOST}-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${_MINGW_HOST}-g++)
|
||||
set(CMAKE_RC_COMPILER ${_MINGW_HOST}-windres)
|
||||
|
||||
get_filename_component(_AGENT_ROOT "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
|
||||
set(_MINGW_PREFIX "${_AGENT_ROOT}/third_party/prefix-mingw")
|
||||
list(APPEND CMAKE_PREFIX_PATH "${_MINGW_PREFIX}")
|
||||
set(CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32" "${_MINGW_PREFIX}")
|
||||
set(CMAKE_FIND_ROOT_PATH "/usr/${_MINGW_HOST}" "${_MINGW_PREFIX}")
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
|
||||
|
||||
+33
-9
@@ -5,6 +5,20 @@
|
||||
|
||||
## 安装
|
||||
|
||||
### 安装包
|
||||
|
||||
从 [Release](https://git.whcself.xyz/WHC/CST-agent/releases) 下载
|
||||
`cst-agent-VERSION-windows-x86_64-setup.exe` 运行即可:按当前用户安装到
|
||||
`%LOCALAPPDATA%\Programs\cst-agent`(不需要管理员),把这个目录加进**用户** PATH,
|
||||
开始菜单里放一个“CST-Agent WebUI”(在用户目录启动 `cst-agent --web --web-open`)。
|
||||
系统低于 Windows 10 1903 时安装包会直接拒绝。
|
||||
|
||||
卸载:设置 → 应用 → CST-Agent,或运行安装目录里的 `uninstall.exe`;会去掉 PATH 里的这一项。
|
||||
API key 和设置在 `%APPDATA%\cst-agent`,卸载时保留。
|
||||
安装包装的文件和 zip 一样,所以之后也能用 `cst-agent-update` 升级,或者重新运行新版安装包覆盖。
|
||||
|
||||
### PowerShell 脚本
|
||||
|
||||
在 PowerShell 里(Windows PowerShell 5.1 或 PowerShell 7 都行):
|
||||
|
||||
```powershell
|
||||
@@ -111,9 +125,8 @@ windows-x86_64/
|
||||
以及 Windows、Program Files、ProgramData 下面的目录打标签:在这些地方启动时,命令不会运行,
|
||||
界面会问是否在沙箱外重跑。请在项目目录里启动。
|
||||
- `/sandbox` 列出打过标签的目录;`/sandbox reset-labels` 恢复它们原来的标签。
|
||||
- 沙箱里可以读、执行用户能访问的文件(凭据文件除外),**不限制联网**。所以像联网的命令
|
||||
(`curl`、`wget`、`ssh`、`git clone/fetch/pull/push`、`npm install`、`pip install`、
|
||||
`winget install`、`Invoke-WebRequest` 等)在 `auto` 模式下仍然会弹窗确认。
|
||||
- 沙箱里可以读、执行用户能访问的文件(凭据文件除外),**不限制联网**:`curl`、`git pull`、
|
||||
`npm install` 等联网命令在 `auto` 模式下也直接在沙箱里运行。
|
||||
- 命令像是被拦了(`Access is denied`、`拒绝访问`、`Permission denied` 等)时,会问
|
||||
“Rerun outside the sandbox?”:`y` 在沙箱外重跑一次,`p` 保存命令前缀规则并重跑,`n` 保留结果。
|
||||
- MSYS2 / Cygwin 的程序(包括 Git for Windows 自带的 `usr\bin` 工具)在 Low 级别下无法初始化
|
||||
@@ -124,7 +137,9 @@ windows-x86_64/
|
||||
|
||||
## 从源码构建
|
||||
|
||||
两种方式产物一样:`dist/windows-x86_64/` 和 `dist/cst-agent-VERSION-windows-x86_64.zip`。
|
||||
两种方式产物一样:`dist/windows-x86_64/` 和 `dist/cst-agent-VERSION-windows-x86_64.zip`;
|
||||
装了 `makensis`(NSIS)时还有安装包 `dist/cst-agent-VERSION-windows-x86_64-setup.exe`
|
||||
(脚本在 `scripts/windows/cst-agent.nsi`)。
|
||||
第三方依赖(curl、ncurses、busybox-w32)都按固定版本从源码编译、静态链接。
|
||||
|
||||
### 在 Linux 上交叉编译
|
||||
@@ -135,8 +150,16 @@ cd CST-agent
|
||||
scripts/bootstrap_windows.sh
|
||||
```
|
||||
|
||||
NixOS / 有 Nix 时自动进 `shell-win.nix`(`pkgsCross.ucrt64`);否则需要 UCRT 版的
|
||||
`x86_64-w64-mingw32-gcc`(带 winpthreads)、cmake、ninja、git、perl。
|
||||
NixOS / 有 Nix 时自动进 `shell-win.nix`(`pkgsCross.ucrt64`,带 NSIS);否则需要 UCRT 版的
|
||||
mingw-w64 gcc(带 winpthreads)、cmake、ninja、git、perl。工具前缀由 `MINGW_HOST` 指定,默认
|
||||
`x86_64-w64-mingw32`;Debian trixie 起的 `gcc-mingw-w64-ucrt64` 是 `x86_64-w64-mingw32ucrt`:
|
||||
|
||||
```bash
|
||||
sudo apt install gcc-mingw-w64-ucrt64 binutils-mingw-w64-ucrt64 nsis cmake ninja-build perl
|
||||
MINGW_HOST=x86_64-w64-mingw32ucrt scripts/bootstrap_windows.sh
|
||||
```
|
||||
|
||||
`CST_WIN_CFLAGS` 给 `cst-agent.exe` 追加编译选项(CI 用 `-Wall -Wextra -Werror`)。
|
||||
|
||||
### 在 Windows 上用 MSYS2 原生编译
|
||||
|
||||
@@ -160,17 +183,18 @@ scripts/bootstrap_msys2.sh
|
||||
|
||||
### 发布
|
||||
|
||||
CI:推 `main` 或 `v*` tag 且 `windows-cross` 成功后,自动把 zip / `.sha256` 发到
|
||||
CI:推 `main` 或 `v*` tag 且 `windows` 任务成功后,自动把 zip、安装包和各自的 `.sha256` 发到
|
||||
Gitea Release `vVERSION`(版本来自 `include/cst_version.h`;同名资源替换)。仓库 Secrets
|
||||
可设 `RELEASE_TOKEN`(`write:repository`),没有则用 Actions 自带 token。
|
||||
|
||||
本地:
|
||||
|
||||
```bash
|
||||
GITEA_TOKEN=... scripts/release_windows.sh --build
|
||||
scripts/bootstrap_windows.sh
|
||||
GITEA_TOKEN=... scripts/release.sh dist/cst-agent-*-windows-x86_64.zip dist/cst-agent-*-windows-x86_64-setup.exe
|
||||
```
|
||||
|
||||
创建(或复用)`vVERSION` Release,上传 zip 和 `.sha256`。token 只从环境变量读取,经 stdin
|
||||
创建(或复用)`vVERSION` Release,上传给出的文件和旁边的 `.sha256`。token 只从环境变量读取,经 stdin
|
||||
交给 curl,不会出现在命令行参数、磁盘或输出里。发版前先 bump `cst_version.h`;推
|
||||
`vX.Y.Z` tag 时必须与头文件一致。
|
||||
|
||||
|
||||
+11
-10
@@ -21,11 +21,11 @@ The Makefile picks `ncursesw`, then `ncurses`, through pkg-config. On macOS it
|
||||
adds Homebrew's keg-only `ncurses`/`curl` to `PKG_CONFIG_PATH`; without
|
||||
Homebrew it falls back to the system `-lncurses` (wide-character capable).
|
||||
|
||||
Verified so far: NixOS x86_64 with gcc and clang (`-Werror`, `make check`,
|
||||
`make asan`, ctest), and every source compiled `-fsyntax-only` with
|
||||
`x86_64-unknown-linux-musl-gcc` without warnings. Full musl, macOS and WSL
|
||||
builds run in CI (`.gitea/workflows/ci.yml`; the macOS job needs a runner and
|
||||
the `HAVE_MACOS_RUNNER` variable) or by hand with the checklist below.
|
||||
Verified so far: NixOS x86_64 with gcc, clang and TinyCC (`-Werror` except
|
||||
tcc, `make check`, `make asan`, ctest). CI (`.gitea/workflows/ci.yml`) checks
|
||||
with TinyCC on Debian and builds the static musl releases for x86_64, x86 and
|
||||
aarch64 with gcc `-Werror` (`make check` on both x86 targets, a qemu smoke
|
||||
test on aarch64). macOS and WSL are checked by hand with the list below.
|
||||
|
||||
## Files
|
||||
|
||||
@@ -42,13 +42,14 @@ the `HAVE_MACOS_RUNNER` variable) or by hand with the checklist below.
|
||||
In the default `auto` approval mode, `run_shell` runs inside an OS sandbox
|
||||
when one is available. Commands may read and execute anything but may only
|
||||
write beneath the workspace, the temp directory, `/dev/null` and `/dev/tty`.
|
||||
The network is not restricted.
|
||||
|
||||
| Platform | Sandbox | Notes |
|
||||
|---|---|---|
|
||||
| Linux 5.13+ | Landlock (raw syscalls, no extra library) | ABI ≥ 4 (6.7+) also blocks TCP connect/bind |
|
||||
| Linux 5.5+ without Landlock (Android / Termux, WSL, older distros), x86_64 / aarch64 | seccomp user-notify | refuses IPv4/IPv6 sockets; see below |
|
||||
| macOS | `sandbox_init` profile | also denies network |
|
||||
| Windows 10 1903+ | Low integrity restricted token + Job Object | writes need a Low label (given to the workspace once); network is not blocked, so network commands still ask; see `README-windows.md` |
|
||||
| Linux 5.13+ | Landlock (raw syscalls, no extra library) | |
|
||||
| Linux 5.5+ without Landlock (Android / Termux, WSL, older distros), x86_64 / aarch64 | seccomp user-notify | see below |
|
||||
| macOS | `sandbox_init` profile | |
|
||||
| Windows 10 1903+ | Low integrity restricted token + Job Object | writes need a Low label (given to the workspace once); see `README-windows.md` |
|
||||
| Older kernels | none | falls back to the read-only rules |
|
||||
|
||||
The status bar and sidebar show `auto · landlock`, `auto · seccomp`,
|
||||
@@ -165,7 +166,7 @@ Chinese VMs as Administrator, a standard user and a Chinese user name):
|
||||
keep CRLF and BOM; `run_shell` output in Chinese is readable on a
|
||||
936 system
|
||||
- [ ] `auto · lowil`: first command reports the label; `echo x > C:\outside`
|
||||
asks to rerun; `curl ...` asks before running; `/sandbox reset-labels`
|
||||
asks to rerun; `curl ...` runs without asking; `/sandbox reset-labels`
|
||||
restores the label
|
||||
- [ ] Esc during `sleep 30 & sleep 30` returns at once and leaves no
|
||||
`busybox.exe` behind
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ export OPENAI_MODEL='your-model'
|
||||
| `CST_MAX_STEPS` | 每个用户回合最多调用模型几次(默认 30) |
|
||||
| `CST_TOOL_OUT_CAP` | 单个工具输出发给模型的字节上限(默认 16384) |
|
||||
| `CST_CTX_WINDOW` | 发给模型的最近非 system 消息条数(默认 80) |
|
||||
| `CST_SHELL_TIMEOUT` | `run_shell` 超时秒数(默认 120) |
|
||||
| `CST_SHELL_TIMEOUT` | `run_shell` 超时秒数(默认不限,Ctrl+C / 停止按钮仍可随时中断) |
|
||||
| `CST_METRICS=0` / `CST_METRICS_PATH` | 关闭 / 指定计时埋点 JSONL |
|
||||
| `CST_RECORD_SSE` / `CST_REPLAY_SSE` | 录制 / 回放原始 SSE 流目录;`CST_REPLAY_CHUNK` 控制回放分块字节数 |
|
||||
| `CST_MODELS` | `/model` 可选列表,`:` 或 `,` 分隔 |
|
||||
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
# WebUI(内置 HTTP + SSE 前端)
|
||||
|
||||
`cst-agent --web` 让同一个进程同时提供命令行以外的前端:浏览器(或任何 HTTP 客户端)
|
||||
连上后能看到流式回答、工具卡片、审批弹窗和任务清单。它是 TUI 之外的第二个前端,
|
||||
**引擎没有为它改一行**——两边用的都是 `cst_agent_hooks_t` 这组回调。
|
||||
|
||||
这份文档同时是**引擎 API v1 的规范**:以后专门客户端(Tauri / Qt / 移动端)直接照它实现,
|
||||
不需要读 C 代码。
|
||||
|
||||
## 快速开始
|
||||
|
||||
```bash
|
||||
# 默认只监听 127.0.0.1:8787
|
||||
cst-agent --web
|
||||
|
||||
# 指定端口;0 = 让系统分配(启动日志里会打印实际 URL)
|
||||
cst-agent --web --web-port 8787
|
||||
|
||||
# 让手机/另一台机器访问(必须给 token,否则拒绝启动在非 loopback 上)
|
||||
cst-agent --web --web-host 0.0.0.0 --web-token 我自己定的口令
|
||||
|
||||
# 开发前端:直接从目录读 web/,改完刷新页面即可,不用重新编译
|
||||
cst-agent --web --web-dev ./web
|
||||
|
||||
# 启动后自动打开浏览器
|
||||
cst-agent --web --web-open
|
||||
|
||||
# 启动即跑一轮(不点页面也能用)
|
||||
cst-agent --web -p "看看这个仓库有什么问题"
|
||||
```
|
||||
|
||||
启动日志:
|
||||
|
||||
```
|
||||
webui: http://127.0.0.1:8787/
|
||||
webui: token 3f9a… (append #token=3f9a… to the URL)
|
||||
```
|
||||
|
||||
带 token 时把 `#token=…` 贴到浏览器地址栏后面即可(`#` 之后不会发给服务器,
|
||||
页面会用 `?token=` 再请求一次)。
|
||||
|
||||
环境变量:`CST_WEB_PORT`、`CST_WEB_HOST`、`CST_WEB_TOKEN`、`CST_WEB_ROOT`。
|
||||
|
||||
退出:终端里 Ctrl+C,或 `POST /api/quit`。
|
||||
|
||||
## 安全模型
|
||||
|
||||
| 场景 | 行为 |
|
||||
|---|---|
|
||||
| 默认 `127.0.0.1` | 不需要 token;`Host` 必须是 loopback(防 DNS rebinding);带 `Origin` 的请求必须来自本页面自己(`http://<Host>`),否则 401 —— 防止任意网页跨站 POST 驱动 agent |
|
||||
| 静态资源 | 页面本身与 `app.js`/`style.css` 不需要 token;`/api/attach/<rel>` 图片仍要(页面会把 token 写进 `SameSite=Strict` cookie) |
|
||||
| 请求解析 | 鉴权与大小检查在读请求体**之前**完成;`Content-Length` 必须是纯数字且只有一个值;请求头整体 15 s 内收完 |
|
||||
| `--web-host 0.0.0.0` | 没给 `--web-token` 时自动生成随机 token 并打印;所有 `/api/*` 都要 token |
|
||||
| token 传递 | `Authorization: Bearer <t>`、`Cookie: cst_token=<t>` 或 `?token=<t>`(`EventSource` 不能设 header,所以查询参数是必需的一条路) |
|
||||
| 资源 | 只有白名单路由;路径里的 `..`/编码 NUL 一律 400;静态资源来自二进制内嵌(或 `--web-dev DIR`) |
|
||||
| 输出渲染 | 模型与工具输出都当不可信内容:Markdown 由服务端渲染,并经过标签/属性白名单(`javascript:` 链接、站外图片、`<script>` 全部丢弃) |
|
||||
| 请求体 | 默认 1 MiB,图片上传 32 MiB;超限 413 |
|
||||
|
||||
**不要**把 WebUI 直接暴露到公网:没有 TLS,token 会以明文出现在网络里。需要远程访问时
|
||||
放在反向代理(https)后面,并确认 `X-Accel-Buffering: no` 之类的缓冲设置不会掐断 SSE。
|
||||
|
||||
## 引擎 API(协议 `cst-webui/1`)
|
||||
|
||||
所有响应都是 JSON(`Content-Type: application/json`)。`GET /api/state` 的
|
||||
`protocol` 字段给出版本号,客户端应当先看它再决定支持哪些端点。
|
||||
|
||||
### 事件流
|
||||
|
||||
```
|
||||
GET /api/events?since=<seq> → text/event-stream (chunked)
|
||||
```
|
||||
|
||||
* 每条事件都有 `id: <seq>`(单调递增)与 `data: {"type":…,"seq":…, …}`。
|
||||
* 断线重连带上 `since=<最后收到的 seq>`(浏览器用 `Last-Event-ID` 也行),
|
||||
服务端保留最近 2000 条,超出后从可用的最老一条开始;比当前 seq 还大(服务端重启过)
|
||||
就当作新连接。
|
||||
* 新连接(不带 `since`)只收之后的事件;但若有尚未回答的 `permission`/`ask_user`,
|
||||
会从那条事件开始补发,刷新页面不会丢掉正在等的问题。
|
||||
* 心跳是 SSE 注释 `: ping`(每秒一次,客户端忽略)。
|
||||
* `state` 事件在状态变化时整份重发,客户端可以直接替换本地状态。
|
||||
|
||||
事件类型:
|
||||
|
||||
| type | 字段 | 说明 |
|
||||
|---|---|---|
|
||||
| `state` | 见下表 | 会话/模型/审批/todo/用量快照 |
|
||||
| `status` | `message` | 引擎自己的一句话状态("calling model (step 2)…") |
|
||||
| `assistant_html` | `text` | **已渲染且已消毒的 HTML 片段**,按顺序追加 |
|
||||
| `assistant_delta` | `text` | 原始文本增量(逐字节真相,做复制/导出时用) |
|
||||
| `think_delta` | `text` | 推理过程增量(Thought) |
|
||||
| `tool` | `id`,`name`,`args`,`phase`,`detail` | `phase` = `open`/`ready`/`run`/`progress`/`done`;`progress` 是子代理的最近几步(`detail` 形如 `explore · grep foo · read_file x`),可以出现多次 |
|
||||
| `tool_result` | `id`,`name`,`out`,`exit`,`bytes`,`truncated` | 完整输出被截到 32 KiB,`truncated=true` |
|
||||
| `todo` | `text` | `todo_write` 替换后的清单(一行一项) |
|
||||
| `permission` | `id`,`tool`,`detail`,`preview`,`sandbox`,`timeout` | 需要回答(见下);`timeout` 秒后按拒绝处理 |
|
||||
| `permission_done` | `id`,`answer` | 问题已结束(超时/取消也会发) |
|
||||
| `ask_user` | `id`,`question`,`options[]` | 需要回答 |
|
||||
| `session` | `id` | 切换/新建了会话,客户端应重新拉 `/api/session` |
|
||||
| `turn_end` | `rc`,`error` | 本轮结束(`rc=0` 正常,非 0 见 `error`) |
|
||||
|
||||
`state` 字段:`workspace` `session` `provider` `model` `effort` `approval`
|
||||
(`ask|auto|yolo`) `plan_mode` `protocol_tools` `status_busy` `todo`
|
||||
`context_tokens` `undo_available` `pending_images[]` `usage{requests,prompt_tokens,
|
||||
completion_tokens,cached_tokens,cache_miss_tokens,cost,priced,currency}`
|
||||
`turn_usage{…}`(上一轮,字段同 `usage`)`api_base` `key_set` `key_from`
|
||||
(`saved` / 环境变量名 / `session` / 空,**从不含 key 本身**)
|
||||
`model_info{label,context,vision,reasoning,source,price?}` `effort_applies`
|
||||
`sees_images`(1 / 0 / -1 未知)`max_steps` `tool_profile` `perm_timeout`
|
||||
`show_thinking`(思考过程默认展开,存在 `settings.json`,和 TUI 的 `/thinking` 是同一个开关)。
|
||||
`cached_tokens` / `cache_miss_tokens` 来自上游的 prompt cache 统计,前端用它算命中率。
|
||||
|
||||
`think_delta` / `assistant_delta` 的 `text` 总是完整的 UTF-8:上游在多字节字符中间
|
||||
断开时,残缺的尾巴留到下一块再发。
|
||||
|
||||
### 请求
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|---|---|---|
|
||||
| GET | `/api/state` | 上面那份快照 |
|
||||
| GET | `/api/session?from=N` | 会话消息(`{messages:[…]}`,形状与 `.agent/sessions/*.jsonl` 一致,另加的字段见下) |
|
||||
| GET | `/api/sessions` | `{current, sessions:[{id,title,messages,mtime}]}` |
|
||||
| POST | `/api/sessions/new` | `{}` → `{id}` |
|
||||
| POST | `/api/sessions/switch` | `{"id":"…"}` |
|
||||
| POST | `/api/sessions/rename` | `{"id":"…"?,"title":"…"}`(不带 `id` = 当前会话);标题按字符截到 120 字节,空串 = 恢复默认(第一句提问);没这个会话 → 404 |
|
||||
| POST | `/api/sessions/delete` | `{"id":"…"}` → `{ok,current}`;删的是当前会话时先开一个新会话(并发 `session` 事件);没这个会话 → 404 |
|
||||
| POST | `/api/retry` | `{}` = 重新生成最后一轮;`{"text":"…"}` = 把最后一句提问换成新文字再跑(编辑重发)。先把最后一轮从会话里退掉,再开新一轮 → 202 `{ok,text}`;没有可重来的轮次 / 轮次在跑 → 409;空串或非字符串 → 400 |
|
||||
| GET | `/api/export` | 当前会话的 Markdown(`text/markdown`,与 TUI `/export` 同一格式) |
|
||||
| GET | `/api/diff[?path=p]` | 工作区的真实 diff:`{git,path,files:[{status,path}],diff,truncated}`。`files` 来自 `git status --short`(含未跟踪),`diff` 是 `git diff`(未暂存部分,限 512 KiB);不是 git 仓库 / 没装 git → `git:false`;10 s 超时 |
|
||||
| GET | `/api/settings` | 运行设置:`protocol`,`protocols[]`,`max_steps`,`tool_profile`,`profiles[]`,`confirm{shell,write,net}`,`approval`,`tools[{name,perm,on,desc,…}]`,`sandbox{kind,active,labels?}` |
|
||||
| POST | `/api/settings` | `{protocol?,max_steps?,tool_profile?,tools?:{名字:bool},confirm?:{shell?,write?,net?}}`;整体校验后才生效(任何一项不对 → 400,什么都不改),成功后发 `state` |
|
||||
| GET | `/api/permissions` | 已保存的"总是允许"规则:`{text,tools[],shell_prefixes[]}`(来自 `<workspace>/.agent/permissions.json`) |
|
||||
| DELETE | `/api/permissions` | 清空这些规则 |
|
||||
| POST | `/api/init` | `{}` → 202 `{ok,text}`:开一轮,让 agent 分析仓库写 `AGENTS.md`(与 TUI `/init` 同一段提示词) |
|
||||
| POST | `/api/skill` | `{"name":"…","task":"…"?}` → 202 `{ok,text}`:把 skill 正文内联进这条消息再开一轮(同 TUI `/skill`);没有这个 skill → 404;缺 `name` / 超过 63 字节 / `task` 不是字符串 → 400 |
|
||||
| GET | `/api/extensions` | `{enabled,trusted,report,task_tool,mcp:[{name,source,command,state,tools,error}],plugins:[{name,source,desc,tools,mcp,skills,error}],skills:[{name,source,desc}],roles:[{name,source,desc,model,read_only}],runs:[{file,role,desc,status,steps,tools,ms}]}`;`state` = `starting/ready/failed/exited/off`,`runs` 是当前会话的子代理运行 |
|
||||
| POST | `/api/extensions/reload` | 停掉 MCP 服务器和插件,重新读取它们和 skills → `{ok,trusted,report}` |
|
||||
| POST | `/api/extensions/trust` | `{"on":true\|false}`:信任(或取消)当前工作区——决定它的 `.agent/plugins`、`.agent/mcp.json` 是否加载;写进 `settings.json` 后重新加载,回复同上 |
|
||||
| POST | `/api/workspace` | `{"path":"/abs/dir"}` —— 换工作目录(权限规则/埋点/AGENTS.md 跟着换,扩展按新目录重新加载),并**在新目录里开一个新会话**(会话是按目录存的);返回 `{id}`,坏路径 → 400 |
|
||||
| POST | `/api/turn` | `{"text":"…"}` → 202 `{ok:true}`;已有轮次在跑时 **409** |
|
||||
| POST | `/api/cancel` | `{}` —— 取消当前轮(不会杀进程);挂起的权限问题按拒绝、提问按忽略处理 |
|
||||
| POST | `/api/permission` | `{"id":N,"answer":0\|1\|2\|3}` → deny / once / always-tool / always-prefix |
|
||||
| POST | `/api/ask_user` | `{"id":N,"text":"…"}` 或 `{"id":N,"dismiss":true}` |
|
||||
| POST | `/api/config` | `{model?,effort?,approval?,plan_mode?,show_thinking?}`;先整体校验再生效,model/effort/approval(`yolo` 除外)写进 `settings.json`;当前模型不吃 effort 时回 `warning`;带 `provider` → 400(改用 `/api/provider`) |
|
||||
| GET | `/api/providers` | `{current, providers:[{name,api_base,model,has_key,key_from,key_env,source,current}]}` |
|
||||
| GET | `/api/models[?refresh=1]` | 当前提供商的模型:`{provider,current,api_base,key_set,api_count,models:[{id,api,label,context,vision,reasoning,source,price?}]}`;先用缓存,过期/缺失/`refresh=1` 时向 `/v1/models` 拉;key 被拒 → 401(仍带内置模型和 `auth:true`),其它失败放在 `api_error` |
|
||||
| POST | `/api/provider` | `{"name":"…","key":"…"?}`:切换提供商。缺 key → 400 `{need_key:true}`;key 被拒 → 401;没这个提供商 → 404;成功 → `{ok,provider,model,warning?}`,key 验证后存进 `credentials`(0600) |
|
||||
| POST | `/api/key` | `{"key":"…"}`:换当前提供商的 key,验证与回复同上 |
|
||||
| POST | `/api/undo` | 回滚最近一轮的文件改动,回 200 + 报告文本 |
|
||||
| POST | `/api/compact` | 手动压缩上下文 |
|
||||
| POST | `/api/attach?name=x.png` | 请求体是图片原始字节(≤32 MiB),进入"待发送"列表 |
|
||||
| DELETE | `/api/attach` | 清空待发送图片 |
|
||||
| GET | `/api/attach/<rel>` | 读回已存进会话的图片(`rel` 由消息的 `attach[].rel` 给出) |
|
||||
| POST | `/api/quit` | 关闭进程 |
|
||||
|
||||
未命中路由 → `404 {"error":"not found"}`;需要 token 而没给 → `401`。
|
||||
轮次运行中(或另一个修改正在进行)时,`sessions/new`、`sessions/switch`、`sessions/delete`、
|
||||
`workspace`、`undo`、`compact`、`attach`(上传/清空)、`provider`、`key`、`retry`、`diff`、
|
||||
`POST /api/settings`、`DELETE /api/permissions`、`init`、`skill`、`extensions/reload`、
|
||||
`extensions/trust` 以及改 `model/effort` 的 `config` 一律 **409**;
|
||||
`approval`/`plan_mode`/`show_thinking` 可以中途改,`sessions/rename`、`export` 与
|
||||
`GET /api/extensions` 在轮次进行中也能用。
|
||||
|
||||
`/api/session` 的每条消息在会话文件的字段之外还带:
|
||||
|
||||
| 字段 | 出现在 | 说明 |
|
||||
|---|---|---|
|
||||
| `html` | assistant | 已消毒的 Markdown 渲染结果(文本协议时只渲染说明文字,不含调用帧) |
|
||||
| `text` | 文本协议的 assistant | 去掉调用帧后的说明文字(`content` 仍是原文) |
|
||||
| `calls` | 带工具调用的 assistant | `[{id,name,args}]`:原生协议来自 `tool_calls`,文本协议从 `content` 解析 |
|
||||
| `tool_result` | 工具结果 | `true`;原生协议 role 是 `tool`,文本协议 role 是 `user`(凭 `tool_call_id` 区分) |
|
||||
| `out`,`exit`,`truncated` | 工具结果 | 去掉 `[tool …]` / `result … end` 外框后的输出(截到 32 KiB,按字符边界)与退出码 |
|
||||
| `note` | 引擎插入的提示 | `true`;如中途切换计划模式时的 `[cst] …` 说明,不是用户说的话 |
|
||||
|
||||
客户端据此在刷新后重建"本轮工具调用"列表:`calls` 建行,`tool_call_id` 相同的结果填进去。
|
||||
|
||||
key 只出现在请求体里:回复、事件、日志都不会带它。`/api/provider` 与 `/api/key`
|
||||
跟其它 `/api/*` 一样受 loopback 同源检查或 token 保护。
|
||||
|
||||
### 客户端接入示例(10 行)
|
||||
|
||||
```python
|
||||
import json, urllib.request
|
||||
base = "http://127.0.0.1:8787"
|
||||
def api(path, body=None, token=None):
|
||||
req = urllib.request.Request(base + path, method="POST" if body is not None else "GET")
|
||||
if token: req.add_header("Authorization", "Bearer " + token)
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
return urllib.request.urlopen(req, data).read()
|
||||
api("/api/turn", {"text": "hello"}) # 之后读 /api/events 收流
|
||||
```
|
||||
|
||||
### 空 body 的 POST
|
||||
|
||||
`POST /api/cancel`、`/api/undo`、`/api/compact`、`/api/quit` 不需要 body。
|
||||
没有 `Content-Length` 头的 POST 按 RFC 9112 §6.3 视为**空 body**(`curl -X POST`
|
||||
正是这样发的),但该连接不再复用(服务端随后关闭它):后面是否跟着未声明的
|
||||
字节无法判断。
|
||||
|
||||
## 前端实现(web/)
|
||||
|
||||
没有构建步骤:`web/index.html` + `web/app.js` + `web/style.css` 是纯 ES module,
|
||||
编译时被 `scripts/embed_web.sh` 塞进二进制(`.gen/web_assets.inc`,跟内置模型数据同一招),
|
||||
所以 Windows 发布包依旧是单个 exe。`--web-dev DIR` 可以绕过内嵌版本直接读目录。
|
||||
|
||||
Markdown 在**服务端**渲染(复用 TUI 用的 sashidown 流式 API),客户端只贴 HTML;
|
||||
因此不需要引入任何 JS 依赖,也顺带保证了"只发送已确认的 HTML"。
|
||||
|
||||
界面本身:左栏可收(顶栏按钮 / `?side=0`),右栏可收(`?panel=0`),主题 `?theme=dark|light`;
|
||||
三个开关都会记进 `localStorage`,带查询参数时以参数为准。空状态只留一句提示,第一轮
|
||||
对话开始后自动隐去;右栏只给三件事——任务清单、本轮文件改动行数(`+N −M`)、上下文
|
||||
占用与 prompt cache 命中率。布局类在页面脚本之前就写进 `<body>`,避免首帧闪一下再收起。
|
||||
|
||||
对话列:正文、过程块、提示行和输入框共用同一条左边线,正文宽度上限 720px(`--content`);
|
||||
左右两栏随屏宽在 `clamp()` 范围内变宽,超宽屏下中间列不会无限拉开。
|
||||
|
||||
过程时间线:一段**连续**的思考和工具调用合成一个过程块,折叠时只有一行
|
||||
("思考 4 次 · 工具 6 次";运行中显示当前动作,比如"执行 run_shell…"),展开后是一条
|
||||
细竖线串起的时间线,每次思考、每个工具调用各占一行,思考只露第一行,点开看全文。
|
||||
出现可见的正文(或引擎提示、下一句提问)时过程块就此结束,后面的思考/工具另起一块——
|
||||
历史重建(`/api/session`)和实时事件流用的是同一条规则,所以刷新前后看到的一样。
|
||||
`/thinking on|off`(`show_thinking`)决定过程块和其中的思考默认展开还是收起,流式输出时也照此执行;
|
||||
`/details` 只管工具行是否展开。
|
||||
|
||||
图片:提问附带的图片显示在气泡**上方**、靠右,一张保持原比例(最长 260px),多张是
|
||||
152px 的圆角方块;点开是大图预览(Esc 关闭,←/→ 切换)。从本页上传/粘贴/拖入的图片在
|
||||
输入框上方显示缩略图,发送后立刻出现在新提问上(本地预览,本轮结束后换成 `/api/attach/<rel>`);
|
||||
从别的客户端加的待发图片仍然显示为文字条。
|
||||
|
||||
### 命令与快捷键
|
||||
|
||||
输入框以 `/` 开头时是命令,在页面里执行,不发给模型:打 `/` 弹出列表(↑↓ 选,Tab 补全,
|
||||
Enter 执行),`/model`、`/effort`、`/approval`、`/provider`、`/skill`、`/thinking`、`/details`
|
||||
还会补全参数。`/usr/bin 是什么` 这种第一个词里带 `/` 的照常发送;未知命令留在输入框里并提示,
|
||||
开头加个空格即可当普通消息发出。
|
||||
|
||||
| 命令 | 作用 |
|
||||
|---|---|
|
||||
| `/new` `/sessions` `/rename [标题]` | 新建 / 会话列表 / 重命名 |
|
||||
| `/retry` `/edit` `/stop` `/undo` `/compact` | 重新生成 / 编辑重发 / 停止 / 撤销写入 / 压缩上下文 |
|
||||
| `/export` `/copy` `/diff [文件]` | 导出 Markdown / 复制最后一条回答 / 工作区 diff |
|
||||
| `/model [id]` `/effort [级别]` `/provider [名字]` `/key` | 模型、推理强度、提供商、API key |
|
||||
| `/approval [ask\|auto\|yolo]` `/plan` `/build` | 审批权限、计划/构建模式 |
|
||||
| `/settings`(`/protocol` `/tools` `/max-steps` `/permissions`) | 运行设置 |
|
||||
| `/init` `/skill 名字 [任务]` `/skills` `/extensions`(`/mcp` `/plugins` `/agents`) | 写 AGENTS.md、用 skill、扩展状态 |
|
||||
| `/thinking [on\|off]` `/details [on\|off]` | 思考过程默认展开(存 `settings.json`)、工具输出全部展开(存浏览器) |
|
||||
| `/theme` `/palette` `/help` | 主题、命令面板、帮助 |
|
||||
|
||||
快捷键:Ctrl+P 命令面板,Ctrl+K 会话列表,Shift+Tab 循环审批(每次询问 → 自动 → 完全放行),
|
||||
F1 或 `?` 帮助,Esc 关闭弹窗 / 取消编辑 / 停止当前一轮,Ctrl+B / Ctrl+J 收起左栏 / 右栏。
|
||||
|
||||
引擎的英文状态、错误和会话里的 `[cst]` 提示在页面上换成中文;一轮失败时(`turn_end.rc≠0`)
|
||||
显示可读的原因(比如"API key 无效或已过期(HTTP 401)"、"用满了 30 步上限"),不再是 `rc=-1`。
|
||||
|
||||
TUI 里打 `/web` 会另起一个 WebUI 进程(`--web --workspace <当前目录> --new`),
|
||||
日志在 `<workspace>/.agent/webui.log`:TUI 不能被 `cst_webui_run()` 的阻塞循环占住,
|
||||
而且两个进程同时写一个会话文件会把它写坏,所以子进程总是开新会话。
|
||||
|
||||
自动压缩的门槛是模型上下文窗口的 **75%**(`CST_CTX_BUDGET` 可覆盖);到点后下一轮
|
||||
开始前先总结历史,而不是等到塞满。
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
make check # 含 tests/test_webui(HTTP/SSE/鉴权/消毒)
|
||||
sh scripts/e2e_webui.sh # 用录制流跑完整轮次 + 权限往返,不需要 API key
|
||||
# (含 e2e_webui_settings.py:假 /v1/models、换提供商/key、中文思考流;
|
||||
# e2e_webui_sessions.py:重新生成/编辑重发/导出/重命名/删除/历史字段;
|
||||
# e2e_webui_workspace.py:diff、运行设置、权限规则;
|
||||
# e2e_webui_ext.py:/init、/skill、扩展、信任、思考开关)
|
||||
sh scripts/e2e_webui.sh tests/fixtures/sse/deepseek-ctl-heredoc ctl-heredoc
|
||||
# (deepseek-ctl 录的是一次契约错误连发的失败运行,不能拿来跑这个)
|
||||
```
|
||||
|
||||
手工验收清单(浏览器):
|
||||
|
||||
1. 打开首页 → 顶部显示 workspace/模型/审批模式,右下状态"就绪"。
|
||||
2. 输入一句话 → 助手气泡逐字出现;工具卡片自动展开、结束后带 `exit=` 标记。
|
||||
3. 切到 `ask` 审批 → 写文件时弹窗;点"允许一次"后继续,点"拒绝"则该工具返回失败但对话继续。
|
||||
4. `会话` 按钮:新建/切换后历史正确重建(刷新页面也一样):连续的思考和工具调用折叠成一行
|
||||
过程块,展开是时间线,工具行点开能看到参数和输出;搜索、重命名、删除可用。
|
||||
`/thinking off` 时流式输出的思考也保持收起。
|
||||
4a. 最后一句提问上有"编辑重发",最后一个回答上有"重新生成";右栏文件行点开是真实 diff。
|
||||
5. 拖入一张图片 → 输入框上方出现缩略图,发送后图片立刻出现在提问气泡上方,点开是大图预览。
|
||||
6. 关掉标签页再打开 → 事件流自动续传,不丢已完成的消息。
|
||||
|
||||
## 限制与已知问题
|
||||
|
||||
* 同一会话同时只能有一轮(`POST /api/turn` 返回 409);TUI 与 WebUI 目前是
|
||||
**二选一**(`--web` 时不启动 TUI),同进程并存留待后续的 hooks 分发层。
|
||||
* 一个会话文件只能由一个进程写:不要同时用 TUI 打开 WebUI 正在用的那个 workspace。
|
||||
* 没有 WebSocket;服务端→客户端用 SSE,客户端→服务端用 POST。够用且穿透代理更容易。
|
||||
* 事件环只有 2000 条:断线太久(超过这个窗口)的客户端会从当前可用的最老事件开始,
|
||||
这时应当重新拉一次 `/api/session`。
|
||||
+35
-6
@@ -47,15 +47,18 @@ int cst_protocol_is_text(cst_protocol_t p);
|
||||
/* Eager protocols execute a call during the stream, at confirmation time. */
|
||||
int cst_protocol_is_eager(cst_protocol_t p);
|
||||
|
||||
/* One image carried by a message (see cst_attach.h). The bytes live beside
|
||||
* the session; this is the metadata plus the name of the stored copy. */
|
||||
/* One file carried by a message (see cst_attach.h): an image, or a document
|
||||
* whose stored payload is its text. The payload lives beside the session;
|
||||
* this is the metadata plus the name of the stored copy. */
|
||||
typedef struct {
|
||||
char id[32]; /* unique inside the session ("img-1a2b3c4d") */
|
||||
char name[256]; /* display name (original file name) */
|
||||
char mime[32]; /* image/png, image/jpeg, image/gif, image/webp, image/bmp */
|
||||
char mime[32]; /* image/png, ...; application/pdf for a PDF whose text
|
||||
* layer the stored file holds; text/plain for text */
|
||||
char rel[256]; /* file name inside the session's attachment directory */
|
||||
long long bytes; /* stored size */
|
||||
long long bytes; /* stored size (the text, for documents) */
|
||||
int width, height; /* pixels, 0 when the header could not be read */
|
||||
int pages; /* PDF page count, 0 for anything else */
|
||||
char detail[8]; /* auto | low | high (wire hint, "" = auto) */
|
||||
char origin[32]; /* user | view_image | mcp (for display only) */
|
||||
} cst_attachment_t;
|
||||
@@ -82,6 +85,7 @@ typedef struct {
|
||||
char model[128];
|
||||
char provider[32];
|
||||
char effort[16]; /* low | medium | high | max */
|
||||
char mode[16]; /* coding | general (CST_DEFAULT_MODE) */
|
||||
cst_protocol_t protocol;
|
||||
int max_steps; /* model calls per user turn */
|
||||
size_t tool_out_cap; /* bytes of one tool output sent to the model */
|
||||
@@ -114,6 +118,14 @@ typedef enum {
|
||||
const char *cst_approval_name(int a);
|
||||
int cst_approval_parse(const char *s, int *out);
|
||||
|
||||
/* Working mode: how the system prompt frames the agent. "coding" (default)
|
||||
* is code framed; "general" is for tasks that are not about a codebase
|
||||
* (documents, data, research, everyday files). Orthogonal to plan mode. */
|
||||
#define CST_DEFAULT_MODE "coding"
|
||||
int cst_mode_valid(const char *m);
|
||||
/* Canonical name of m, or the default when m is unknown / empty. */
|
||||
const char *cst_mode_name(const char *m);
|
||||
|
||||
typedef struct cst_session cst_session_t;
|
||||
typedef struct cst_agent cst_agent_t;
|
||||
|
||||
@@ -130,6 +142,12 @@ const cst_message_t *cst_session_get(const cst_session_t *s, int i);
|
||||
const char *cst_session_path(const cst_session_t *s);
|
||||
int cst_session_clear(cst_session_t *s, int keep_system);
|
||||
int cst_session_rewrite(cst_session_t *s, const cst_message_t *msgs, int n);
|
||||
/* Tool output: role=tool, or a text-protocol user message answering a call. */
|
||||
int cst_message_is_tool_result(const cst_message_t *m);
|
||||
/* The transcript as Markdown, malloc'd (NULL on OOM). Tool output and call
|
||||
* arguments are cut at 4000 bytes. Takes the session lock. */
|
||||
char *cst_session_export_md(cst_session_t *s, const char *title,
|
||||
const char *sid, const char *model);
|
||||
/* Readers on another thread must hold the lock while using pointers returned
|
||||
* by cst_session_get (append may reallocate). Append locks internally. */
|
||||
void cst_session_lock(cst_session_t *s);
|
||||
@@ -272,6 +290,8 @@ int cst_llm_list_models(const char *api_base, const char *api_key,
|
||||
char *cst_llm_build_body(const cst_llm_request_t *req);
|
||||
/* Reset the CST_RECORD_SSE / CST_REPLAY_SSE sequence counter. */
|
||||
void cst_llm_trace_reset(void);
|
||||
/* Set CURLOPT_CAINFO on a CURL * when a CA bundle file is known. */
|
||||
void cst_curl_apply_ca(void *curl);
|
||||
|
||||
/* on_permission answers */
|
||||
#define CST_PERM_DENY 0
|
||||
@@ -321,6 +341,12 @@ cst_agent_config_t *cst_agent_config_mut(cst_agent_t *a);
|
||||
void cst_agent_request_cancel(cst_agent_t *a);
|
||||
int cst_agent_new_session(cst_agent_t *a, char *id_out, size_t id_sz);
|
||||
int cst_agent_switch_session(cst_agent_t *a, const char *id);
|
||||
/* Re-root the agent at another directory. Per-workspace state (permission
|
||||
* rules, metrics, project notes) follows, the system prompt is rebuilt and the
|
||||
* token budget is re-measured. The session is not touched: the caller decides
|
||||
* whether to keep it or start a new one. */
|
||||
int cst_agent_set_workspace(cst_agent_t *a, const char *path, char *err,
|
||||
size_t err_sz);
|
||||
int cst_agent_compact(cst_agent_t *a, char *err, size_t err_sz);
|
||||
/* Bare id of the current session (file name without .jsonl). */
|
||||
void cst_agent_session_id(const cst_agent_t *a, char *out, size_t out_sz);
|
||||
@@ -392,13 +418,14 @@ int cst_creds_resolve(const char *provider, char *out, size_t out_sz,
|
||||
/* Hidden console prompt (no echo). */
|
||||
int cst_creds_prompt_console(const char *provider, char *out, size_t out_sz);
|
||||
|
||||
/* Last used provider, model per provider and effort, in
|
||||
/* Last used provider, model per provider, effort and working mode, in
|
||||
* <config>/settings.json. */
|
||||
#define CST_SETTINGS_MAX_MODELS 16
|
||||
#define CST_SETTINGS_MAX_TRUSTED 32
|
||||
typedef struct {
|
||||
char provider[32];
|
||||
char effort[16];
|
||||
char mode[16]; /* coding | general, "" = not chosen yet */
|
||||
char approval[8]; /* "" = not chosen yet */
|
||||
int show_thinking; /* TUI: Thought blocks start expanded */
|
||||
/* workspaces whose .agent/plugins and .agent/mcp.json may run */
|
||||
@@ -413,11 +440,13 @@ typedef struct {
|
||||
int cst_settings_load(cst_settings_t *s);
|
||||
int cst_settings_save(const cst_settings_t *s);
|
||||
const char *cst_settings_model(const cst_settings_t *s, const char *provider);
|
||||
/* Record cfg's provider, model and effort as the last used ones. */
|
||||
/* Record cfg's provider, model, effort and mode as the last used ones. */
|
||||
int cst_settings_remember(const cst_agent_config_t *cfg);
|
||||
/* Record an approval mode the user picked (CST_APPROVAL_*). */
|
||||
int cst_settings_remember_approval(int approval);
|
||||
int cst_settings_remember_thinking(int show_thinking);
|
||||
/* Record the working mode the user picked (coding | general). */
|
||||
int cst_settings_remember_mode(const char *mode);
|
||||
/* Workspace trust for project plugins / MCP servers (by real path). */
|
||||
int cst_settings_trusted(const char *workspace);
|
||||
int cst_settings_trust(const char *workspace, int on);
|
||||
|
||||
+24
-14
@@ -9,16 +9,24 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* --- image attachments ---------------------------------------------------
|
||||
* An attachment is a validated copy of an image file, stored beside the
|
||||
* session so the bytes a model is shown stay the bytes that were picked.
|
||||
* Only the metadata travels in the JSONL; the bytes are encoded into the
|
||||
* request when it is built. */
|
||||
/* --- attachments ---------------------------------------------------------
|
||||
* An attachment is a validated copy of a file, stored beside the session so
|
||||
* what a model is shown stays what was picked. Images are stored as they are
|
||||
* and encoded into the request when it is built; a PDF is reduced to its text
|
||||
* layer and a text file is stored as is, both of which travel inline as text
|
||||
* (so a text-only model still gets them). Only the metadata goes in the
|
||||
* JSONL. */
|
||||
|
||||
/* Supported: PNG, JPEG, GIF, WebP, BMP (static images). */
|
||||
#define CST_ATTACH_MAX_BYTES (16 * 1024 * 1024) /* one stored image */
|
||||
/* Supported: PNG, JPEG, GIF, WebP, BMP (static images), PDF, UTF-8 text. */
|
||||
#define CST_ATTACH_MAX_BYTES (16 * 1024 * 1024) /* one picked file */
|
||||
#define CST_ATTACH_MAX_TOTAL (32 * 1024 * 1024) /* per request */
|
||||
#define CST_ATTACH_MAX_COUNT 8 /* per message */
|
||||
/* Text stored and inlined per document (a PDF also caps its text here). */
|
||||
#define CST_ATTACH_TEXT_MAX (4 * 1024 * 1024)
|
||||
|
||||
/* Images go on the wire as image parts; everything else is inlined as text. */
|
||||
int cst_attach_is_image(const cst_attachment_t *a);
|
||||
int cst_attach_is_text(const cst_attachment_t *a);
|
||||
|
||||
/* Magic-byte test: 0 and a canonical mime when data holds a supported
|
||||
* image, -1 otherwise (an extension alone never decides). */
|
||||
@@ -35,23 +43,25 @@ int cst_attach_dir(const char *session_path, char *out, size_t out_sz);
|
||||
int cst_attach_full_path(const char *session_path, const cst_attachment_t *a,
|
||||
char *out, size_t out_sz);
|
||||
|
||||
/* Read src_path, verify it is a supported image within limits, and copy it
|
||||
* into the session's attachment directory. Returns a malloc'd entry the
|
||||
* caller owns (cst_attach_free), or NULL with err filled. */
|
||||
/* Read src_path and copy it into the session's attachment directory: an image
|
||||
* as it is, a PDF as its extracted text, anything else only when it is text.
|
||||
* Returns a malloc'd entry the caller owns (cst_attach_free), or NULL with
|
||||
* err filled. */
|
||||
cst_attachment_t *cst_attach_import_file(const char *session_path,
|
||||
const char *src_path, char *err,
|
||||
size_t err_sz);
|
||||
/* Store already-read bytes (clipboard, downloads); name is display-only. */
|
||||
/* Store already-read image bytes (clipboard, downloads); name is
|
||||
* display-only. */
|
||||
cst_attachment_t *cst_attach_import_bytes(const char *session_path,
|
||||
const char *name, const char *data,
|
||||
size_t n, char *err, size_t err_sz);
|
||||
|
||||
/* Validate and read back a stored image, or NULL when it is gone. */
|
||||
/* Validate and read back a stored payload, or NULL when it is gone. */
|
||||
char *cst_attach_read(const char *session_path, const cst_attachment_t *a,
|
||||
size_t *out_len, char *err, size_t err_sz);
|
||||
/* "data:image/png;base64,...." for the wire; NULL when the file is gone. */
|
||||
/* "data:image/png;base64,...." for the wire; images only, NULL otherwise. */
|
||||
char *cst_attach_data_url(const char *session_path, const cst_attachment_t *a);
|
||||
/* "1920x1080 PNG 412 KB" style one-liner for prompts and the UI. */
|
||||
/* "1920x1080 png 412 KB" / "pdf 1.2 MB, 12 pages" one-liner for prompts. */
|
||||
void cst_attach_describe(const cst_attachment_t *a, char *out, size_t out_sz);
|
||||
|
||||
void cst_attach_free(cst_attachment_t *a);
|
||||
|
||||
@@ -13,6 +13,20 @@ extern "C" {
|
||||
* ("no image on the clipboard", "install wl-clipboard", ...). */
|
||||
int cst_clipboard_image(char **data, size_t *n, char *err, size_t err_sz);
|
||||
|
||||
/* Cap on text moved through the clipboard (read and write). */
|
||||
#define CST_CLIPBOARD_TEXT_MAX (1024 * 1024)
|
||||
|
||||
/* UTF-8 text on the system clipboard: Wayland (wl-paste), X11 (xclip),
|
||||
* macOS (pbpaste), Windows (CF_UNICODETEXT). Returns 0 with *data malloc'd
|
||||
* and NUL-terminated, or -1 with err filled in ("no text on the clipboard",
|
||||
* "reading the clipboard needs wl-clipboard", ...). */
|
||||
int cst_clipboard_text(char **data, size_t *n, char *err, size_t err_sz);
|
||||
|
||||
/* Replace the clipboard with UTF-8 text: wl-copy / xclip / pbcopy, or the
|
||||
* Windows clipboard API. Returns 0 on success, else -1 with err. */
|
||||
int cst_clipboard_set_text(const char *text, size_t n, char *err,
|
||||
size_t err_sz);
|
||||
|
||||
/* Windows CF_DIB bytes (BITMAPINFOHEADER + pixels) as an image file: a PNG
|
||||
* for uncompressed 24/32-bit bitmaps, else a BMP. NULL when malformed. */
|
||||
char *cst_dib_to_image(const void *dib, size_t sz, size_t *n);
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#ifndef CST_HTTP_H
|
||||
#define CST_HTTP_H
|
||||
|
||||
/* A small HTTP/1.1 server for the built-in WebUI (src/http_srv.c).
|
||||
*
|
||||
* Only what a local single-page app needs: exact-match routes, keep-alive,
|
||||
* fixed-length bodies and chunked streaming responses (SSE). No TLS, no
|
||||
* HTTP/2, no chunked *request* bodies, no CGI. Every connection is served
|
||||
* by its own thread; a handler may keep the connection as long as it wants
|
||||
* (that is how the event stream works). */
|
||||
|
||||
#include "cst_net.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct cst_http_conn cst_http_conn_t;
|
||||
|
||||
typedef struct {
|
||||
char method[8]; /* GET / POST / DELETE ... */
|
||||
char path[1024]; /* percent-decoded, without the query string */
|
||||
char query[1024]; /* raw, without '?' */
|
||||
const char *body; /* NUL-terminated; may hold binary bytes */
|
||||
size_t body_len;
|
||||
/* Headers of interest, NULL when absent (pointers into the request). */
|
||||
const char *authorization;
|
||||
const char *cookie;
|
||||
const char *origin;
|
||||
const char *host;
|
||||
const char *content_type;
|
||||
const char *user_agent;
|
||||
const char *last_event_id; /* an EventSource reconnecting */
|
||||
int keep_alive;
|
||||
} cst_http_req_t;
|
||||
|
||||
/* 0 = handled (a response was sent), -1 = not found (the server replies). */
|
||||
typedef int (*cst_http_fn)(cst_http_conn_t *c, void *ud);
|
||||
|
||||
typedef struct {
|
||||
const char *method; /* "*" matches any */
|
||||
const char *path; /* exact; "*" = fallback (try last) */
|
||||
cst_http_fn fn;
|
||||
void *ud;
|
||||
int auth; /* ask the auth hook before the handler */
|
||||
size_t max_body; /* request body cap; 0 = server default */
|
||||
} cst_http_route_t;
|
||||
|
||||
typedef struct {
|
||||
const char *host; /* NULL/"" = 127.0.0.1 */
|
||||
int port; /* 0 = pick a free port */
|
||||
size_t max_body; /* default 1 MiB */
|
||||
int max_conn; /* default 32 */
|
||||
int read_timeout_ms; /* waiting for the request headers (default 15000) */
|
||||
int idle_timeout_ms; /* keep-alive pause between requests (default 60000) */
|
||||
const cst_http_route_t *routes;
|
||||
int nroutes;
|
||||
/* 0 = allow, -1 = the server answers 401 before the handler runs. */
|
||||
int (*auth)(const cst_http_req_t *req, void *ud);
|
||||
void *auth_ud;
|
||||
/* One line per request, e.g. "GET /api/state -> 200". */
|
||||
void (*on_log)(const char *line, void *ud);
|
||||
void *log_ud;
|
||||
} cst_http_opts_t;
|
||||
|
||||
typedef struct cst_http_server cst_http_server_t;
|
||||
|
||||
/* Start listening. NULL on failure with a reason in err. */
|
||||
cst_http_server_t *cst_http_start(const cst_http_opts_t *o, char *err,
|
||||
size_t err_sz);
|
||||
int cst_http_port(const cst_http_server_t *s);
|
||||
/* Non-zero once stop() was called: long-lived handlers must return. */
|
||||
int cst_http_stopping(const cst_http_server_t *s);
|
||||
/* Close the listener and every connection, then wait (30 s at most) for the
|
||||
* handler threads. Returns how many still run: their ud must stay alive. */
|
||||
int cst_http_stop(cst_http_server_t *s);
|
||||
/* Does nothing while handlers still run on s. */
|
||||
void cst_http_free(cst_http_server_t *s);
|
||||
|
||||
/* --- inside a handler --- */
|
||||
const cst_http_req_t *cst_http_request(cst_http_conn_t *c);
|
||||
int cst_http_send(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const void *body, size_t n);
|
||||
int cst_http_send_str(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const char *s);
|
||||
/* json may be NULL (then "null" is sent); it is copied, not owned. */
|
||||
int cst_http_send_json(cst_http_conn_t *c, int status, const char *json);
|
||||
/* {"error":"..."} */
|
||||
int cst_http_send_err(cst_http_conn_t *c, int status, const char *msg);
|
||||
/* True once a response (or stream) began for this request. */
|
||||
int cst_http_sent(const cst_http_conn_t *c);
|
||||
|
||||
/* Chunked response: begin(), any number of write()s, end(). */
|
||||
int cst_http_stream_begin(cst_http_conn_t *c, const char *ctype);
|
||||
int cst_http_stream_write(cst_http_conn_t *c, const void *data, size_t n);
|
||||
int cst_http_stream_end(cst_http_conn_t *c);
|
||||
|
||||
/* Query parameter, percent-decoded ('+' = space). 0 when found. */
|
||||
int cst_http_query(const cst_http_req_t *req, const char *key, char *out,
|
||||
size_t out_sz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CST_HTTP_H */
|
||||
@@ -0,0 +1,68 @@
|
||||
#ifndef CST_NET_H
|
||||
#define CST_NET_H
|
||||
|
||||
/* Minimal TCP sockets for the embedded HTTP server (src/net.c).
|
||||
*
|
||||
* The point of this layer is that src/http_srv.c and src/webui.c never
|
||||
* include a socket header: Unix file descriptors and Windows SOCKETs differ
|
||||
* in type, close() and error reporting, and Windows needs WSAStartup before
|
||||
* anything works. */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef intptr_t os_socket_t;
|
||||
#define OS_SOCKET_BAD ((os_socket_t)-1)
|
||||
#else
|
||||
typedef int os_socket_t;
|
||||
#define OS_SOCKET_BAD (-1)
|
||||
#endif
|
||||
|
||||
/* Longest "address:port" os_net_peer writes (IPv6 hosts are bracketed so a
|
||||
* URL built from it stays unambiguous). */
|
||||
#define OS_NET_ADDR_MAX 64
|
||||
|
||||
/* Winsock startup (no-op on Unix, which also ignores SIGPIPE so a client
|
||||
* that hangs up cannot kill the process). 0 / -1. */
|
||||
int os_net_init(void);
|
||||
void os_net_cleanup(void);
|
||||
|
||||
/* Bind and listen on host:port. host may be NULL/"" (then "127.0.0.1"),
|
||||
* "0.0.0.0"/"::" (every interface) or any literal address / name; port 0
|
||||
* picks a free one. Returns the listening socket, or OS_SOCKET_BAD with a
|
||||
* short reason in err. */
|
||||
os_socket_t os_net_listen(const char *host, int port, char *err, size_t err_sz);
|
||||
/* The port actually bound (0 when unknown). */
|
||||
int os_net_port(os_socket_t s);
|
||||
/* A one-line "host:port" for messages. */
|
||||
void os_net_peer(os_socket_t s, char *out, size_t out_sz);
|
||||
|
||||
/* Connect to host:port, waiting up to timeout_ms: the socket, or
|
||||
* OS_SOCKET_BAD. Used by the tests and by anything that drives the WebUI
|
||||
* API from inside the process. */
|
||||
os_socket_t os_net_connect(const char *host, int port, int timeout_ms);
|
||||
|
||||
/* Accept one connection, waiting up to timeout_ms (<0 = forever).
|
||||
* Returns the socket, OS_SOCKET_BAD on error, or OS_SOCKET_BAD with
|
||||
* *timed_out set when the wait ran out. */
|
||||
os_socket_t os_net_accept(os_socket_t s, int timeout_ms, int *timed_out);
|
||||
|
||||
/* Read at most n bytes, waiting up to timeout_ms: bytes read, 0 at EOF,
|
||||
* -1 on error, -2 when nothing arrived in time. */
|
||||
long os_net_recv(os_socket_t s, void *buf, size_t n, int timeout_ms);
|
||||
/* Write everything (timeout_ms per call): 0 / -1. */
|
||||
int os_net_send_all(os_socket_t s, const void *buf, size_t n, int timeout_ms);
|
||||
/* Wake up a thread blocked in recv/send on this socket (shutdown, no close). */
|
||||
void os_net_shutdown(os_socket_t s);
|
||||
void os_net_close(os_socket_t s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CST_NET_H */
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef CST_PDF_H
|
||||
#define CST_PDF_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Text layer of a PDF, in page order, with no external dependency (no
|
||||
* poppler, no zlib): classic objects and object streams, FlateDecode plus
|
||||
* ASCIIHex/ASCII85, PNG/TIFF predictors, Tj/TJ/'/" text with per-font
|
||||
* ToUnicode CMaps and a WinAnsi / Latin-1 fallback. Scanned pages have no
|
||||
* text layer and are reported as such.
|
||||
* Returns a malloc'd NUL-terminated UTF-8 string, or NULL with err filled in
|
||||
* ("PDF is encrypted ...", "no extractable text ..."). *pages (optional) gets
|
||||
* the page count; the result is truncated at CST_PDF_TEXT_MAX bytes. */
|
||||
char *cst_pdf_text(const void *data, size_t n, int *pages, char *err,
|
||||
size_t err_sz);
|
||||
|
||||
#define CST_PDF_TEXT_MAX (4u * 1024u * 1024u)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef CST_PROVIDER_SWITCH_H
|
||||
#define CST_PROVIDER_SWITCH_H
|
||||
|
||||
/* Model lists, API keys and provider switches, shared by the TUI and the
|
||||
* WebUI so both front ends validate and persist the same way.
|
||||
*
|
||||
* A key is checked with GET /models: a 401/403 (CST_MODELS_AUTH) rejects
|
||||
* it, any other failure (offline, no /models route) lets it through
|
||||
* unverified. A key that passes is saved to <config>/credentials. Keys are
|
||||
* never copied into err. */
|
||||
|
||||
#include "cst_agent.h"
|
||||
#include "cst_models.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CST_PS_MAX_MODELS 512
|
||||
#define CST_PS_UNKNOWN (-1) /* no such provider */
|
||||
#define CST_PS_NEED_KEY (-10) /* no key given, none saved or in the env */
|
||||
/* Non-negative results are flags: */
|
||||
#define CST_PS_UNVERIFIED 1 /* /models failed for another reason; err says */
|
||||
#define CST_PS_UNSAVED 2 /* the credentials file could not be written */
|
||||
|
||||
typedef struct {
|
||||
char id[128];
|
||||
int api; /* listed by the provider's /models (else built-in only) */
|
||||
cst_model_info_t info;
|
||||
} cst_ps_model_t;
|
||||
|
||||
/* GET /models; a good answer refreshes models-cache/<provider>.json. The
|
||||
* count, CST_MODELS_AUTH, or another negative value with err set. */
|
||||
int cst_ps_fetch(const char *provider, const char *api_base, const char *key,
|
||||
char ids[][128], int max, char *err, size_t err_sz);
|
||||
/* The cached list: count, or -1; *fresh when younger than the TTL. */
|
||||
int cst_ps_cached(const char *provider, char ids[][128], int max, int *fresh);
|
||||
|
||||
/* Base URL for provider: cfg's own when it is the current provider (it may
|
||||
* be overridden), else the preset's; "" when unknown. */
|
||||
const char *cst_ps_base(const cst_agent_config_t *cfg, const char *provider);
|
||||
|
||||
/* The model picker list for cfg's provider: the API's ids (the cache while
|
||||
* it is fresh, else a fetch; refresh always fetches) followed by the
|
||||
* built-in names it did not list. *api_rc gets the fetch result (count,
|
||||
* CST_MODELS_AUTH, -1 with err) or the cached count. Returns the count. */
|
||||
int cst_ps_models(const cst_agent_config_t *cfg, int refresh,
|
||||
cst_ps_model_t *out, int max, int *api_rc, char *err,
|
||||
size_t err_sz);
|
||||
|
||||
/* Checks key against /models and saves it: CST_MODELS_AUTH (err set), or
|
||||
* the CST_PS_* flags. When ids is given it gets the fetched list and *nids
|
||||
* the raw fetch result. */
|
||||
int cst_ps_verify_save(const char *provider, const char *api_base,
|
||||
const char *key, char ids[][128], int max, int *nids,
|
||||
char *err, size_t err_sz);
|
||||
|
||||
/* Point cfg at provider p with key: base URL, the remembered model (else
|
||||
* the preset's), settings saved. */
|
||||
void cst_ps_apply_provider(cst_agent_config_t *cfg, const cst_provider_t *p,
|
||||
const char *key);
|
||||
|
||||
/* The whole switch without a prompt: key NULL/"" uses the saved or env
|
||||
* key (else CST_PS_NEED_KEY); a given key is verified and saved first. A
|
||||
* preset with no model takes the first one the API lists. The CST_PS_*
|
||||
* flags, or CST_PS_UNKNOWN / CST_PS_NEED_KEY / CST_MODELS_AUTH. */
|
||||
int cst_ps_switch_provider(cst_agent_config_t *cfg, const char *name,
|
||||
const char *key, char *err, size_t err_sz);
|
||||
|
||||
/* Replace the current provider's key: verified, saved, put in cfg. Same
|
||||
* results as cst_ps_verify_save. */
|
||||
int cst_ps_set_key(cst_agent_config_t *cfg, const char *key, char *err,
|
||||
size_t err_sz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -2,20 +2,19 @@
|
||||
#define CST_SANDBOX_H
|
||||
|
||||
/*
|
||||
* OS sandbox for run_shell: reads and exec stay allowed everywhere, writes
|
||||
* are limited to the workspace, the temp dir, /dev/null and /dev/tty, and
|
||||
* the network is cut where the OS can do it.
|
||||
* Linux Landlock (network needs ABI 4, kernel 6.7+)
|
||||
* OS sandbox for run_shell: reads, exec and the network stay allowed;
|
||||
* writes are limited to the workspace, the temp dir, /dev/null and /dev/tty.
|
||||
* Linux Landlock
|
||||
* Linux seccomp user-notify when Landlock is missing (Android/Termux,
|
||||
* older kernels): a supervisor thread checks the path of every
|
||||
* write-type syscall and IPv4/IPv6 sockets are refused. Guards
|
||||
* write-type syscall. Guards
|
||||
* against mistakes, not hostile code (the path is checked before
|
||||
* the kernel reads it again).
|
||||
* macOS sandbox_init (Seatbelt profile)
|
||||
* Windows Low integrity level: a restricted token (no admin privileges,
|
||||
* Low IL) can only write objects labeled Low, so the workspace and
|
||||
* a temp dir get that label; the credentials file is labeled
|
||||
* no-read-up. The network stays open (callers ask first).
|
||||
* no-read-up.
|
||||
* other none: callers fall back to rule-based approval
|
||||
*/
|
||||
|
||||
@@ -42,7 +41,7 @@ int cst_sandbox_probe(void);
|
||||
int cst_sandbox_reprobe(void);
|
||||
/* "landlock" | "seatbelt" | "seccomp" | "lowil" | "none" */
|
||||
const char *cst_sandbox_name(int kind);
|
||||
/* Landlock ABI version (0 when unavailable); ≥4 also blocks TCP. */
|
||||
/* Landlock ABI version (0 when unavailable). */
|
||||
int cst_sandbox_landlock_abi(void);
|
||||
|
||||
typedef struct {
|
||||
@@ -69,7 +68,7 @@ int cst_sandbox_run_shell(const char *workspace, const char *cmd,
|
||||
size_t max_out, os_run_result_t *r);
|
||||
|
||||
/* Output of a failed sandboxed run that looks like the sandbox got in the
|
||||
* way (EACCES / EPERM / EROFS / blocked network). */
|
||||
* way (EACCES / EPERM / EROFS). */
|
||||
int cst_sandbox_blocked(const char *out, size_t n, int exit_code);
|
||||
|
||||
/* lowil: directories this user's cst-agent labeled Low (recorded in the
|
||||
|
||||
@@ -45,6 +45,8 @@ char *cst_skill_read(const char *workspace, const char *name,
|
||||
* task. NULL when the skill does not exist. malloc'd. */
|
||||
char *cst_skill_prompt(const char *workspace, const char *name,
|
||||
const char *task);
|
||||
/* User message for `/init`: have the agent write AGENTS.md. */
|
||||
const char *cst_init_prompt(void);
|
||||
/* Register the `skill` tool (idempotent). */
|
||||
int cst_skills_register_tool(void);
|
||||
|
||||
|
||||
@@ -27,6 +27,17 @@ size_t cst_utf8_floor(const char *s, size_t n);
|
||||
char *cst_utf8_sanitize(const char *s, size_t n, size_t *out_len);
|
||||
int cst_utf8_valid(const char *s, size_t n);
|
||||
|
||||
/* Re-chunks a byte stream so no piece ends inside a UTF-8 sequence: the
|
||||
* incomplete tail (at most 3 bytes) waits in the hold for the next feed. */
|
||||
typedef struct {
|
||||
char b[4];
|
||||
size_t n;
|
||||
} cst_utf8_hold_t;
|
||||
/* Returns the complete prefix of hold+s[0..n) (malloc'd, NUL-terminated,
|
||||
* *out_len may be 0), or NULL on OOM. */
|
||||
char *cst_utf8_hold_feed(cst_utf8_hold_t *h, const char *s, size_t n,
|
||||
size_t *out_len);
|
||||
|
||||
/* Text files may start with a UTF-8 BOM (Windows editors): its length (0/3),
|
||||
* and an in-place strip of the NUL-terminated s (*len updated when given). */
|
||||
size_t cst_bom_len(const char *s, size_t n);
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
#define CST_VERSION_H
|
||||
|
||||
#define CST_VERSION_MAJOR 0
|
||||
#define CST_VERSION_MINOR 7
|
||||
#define CST_VERSION_PATCH 0
|
||||
#define CST_VERSION_MINOR 8
|
||||
#define CST_VERSION_PATCH 6
|
||||
|
||||
#define CST_VERSION_STR_(x) #x
|
||||
#define CST_VERSION_STR(x) CST_VERSION_STR_(x)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef CST_WEB_MD_H
|
||||
#define CST_WEB_MD_H
|
||||
|
||||
/* Markdown for the WebUI: the same sashidown renderer the TUI uses, but
|
||||
* HTML output with a strict allowlist.
|
||||
*
|
||||
* sashidown renders straight to HTML and escapes text, quotes and '&', so
|
||||
* no raw markup can reach the browser. Two holes remain, and both matter
|
||||
* because every byte rendered here comes from a model or a tool: a link's
|
||||
* href keeps any scheme ("javascript:") and an image's src may point
|
||||
* anywhere (a tracking pixel, or worse). cst_web_md_* therefore rewrites
|
||||
* tags instead of passing them through: unknown tags are dropped, hrefs
|
||||
* are limited to http/https/mailto/relative, and images to the session's
|
||||
* own attachment endpoint.
|
||||
*
|
||||
* The streaming form renders confirmed HTML as the model talks, which is
|
||||
* the CST property the project is built on: nothing is emitted before it
|
||||
* is safe to emit. A tag split across two deltas is held back until its
|
||||
* '>' arrives. */
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Called with chunks of sanitized HTML; return non-zero to stop. */
|
||||
typedef int (*cst_web_md_emit_fn)(const char *html, size_t len, void *ud);
|
||||
|
||||
typedef struct cst_web_md cst_web_md_t;
|
||||
|
||||
cst_web_md_t *cst_web_md_new(cst_web_md_emit_fn emit, void *ud);
|
||||
/* Feed model output. Returns 0, or -1 on OOM. */
|
||||
int cst_web_md_feed(cst_web_md_t *s, const char *data, size_t len);
|
||||
/* Flush the pending input and close the renderer. */
|
||||
int cst_web_md_finish(cst_web_md_t *s);
|
||||
void cst_web_md_free(cst_web_md_t *s);
|
||||
|
||||
/* One-shot: markdown -> sanitized HTML (malloc'd, never NULL on success). */
|
||||
char *cst_web_md_html(const char *md, size_t len);
|
||||
/* The sanitizer alone (tests, and any HTML that did not come from here). */
|
||||
char *cst_web_md_sanitize(const char *html, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CST_WEB_MD_H */
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef CST_WEBUI_H
|
||||
#define CST_WEBUI_H
|
||||
|
||||
/* The built-in WebUI: an HTTP + SSE front end for one agent process.
|
||||
*
|
||||
* It is a second implementation of the same interface the TUI uses
|
||||
* (cst_agent_hooks_t), which is why the engine needs no changes: the turn
|
||||
* runs on a worker thread and every live event goes out as a Server-Sent
|
||||
* Event. The protocol is documented in docs/webui.md and is meant to be
|
||||
* the contract a dedicated client speaks later. */
|
||||
|
||||
#include "cst_agent.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* What --web listens on when --web-port/CST_WEB_PORT says nothing (the
|
||||
* README and --help quote this number, so keep them in sync). */
|
||||
#define CST_WEBUI_DEFAULT_PORT 8787
|
||||
|
||||
typedef struct {
|
||||
const char *host; /* NULL = 127.0.0.1; "0.0.0.0" = every interface */
|
||||
int port; /* 0 = pick a free port */
|
||||
const char *token; /* NULL = generate one when needed */
|
||||
const char *root; /* serve the UI from this directory instead of the
|
||||
copy embedded in the binary (development) */
|
||||
int open_browser; /* best-effort xdg-open / open / start */
|
||||
/* Prompt to run once at startup (--web -p ...); NULL = wait for the UI. */
|
||||
const char *prompt;
|
||||
int perm_timeout_s; /* how long a permission question waits (0 = 600) */
|
||||
} cst_webui_opts_t;
|
||||
|
||||
/* Serve until interrupted (Ctrl+C) or the event stream asks to quit.
|
||||
* Returns the process exit code. */
|
||||
int cst_webui_run(cst_agent_t *agent, const cst_webui_opts_t *opts);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CST_WEBUI_H */
|
||||
+4
-3
@@ -114,9 +114,10 @@ int os_win_get_label(const char *path, char *out, size_t n);
|
||||
int os_clipboard_set(const char *utf8, size_t n);
|
||||
#endif
|
||||
|
||||
/* Child processes get stdin=/dev/null, their own process group, and are
|
||||
* killed after the timeout (CST_SHELL_TIMEOUT seconds, default 120) or when
|
||||
* *flag becomes non-zero. Exit code 124 = timeout, 130 = cancelled. */
|
||||
/* Child processes get stdin=/dev/null and their own process group. They run
|
||||
* with no time limit unless one is asked for (CST_SHELL_TIMEOUT seconds, or
|
||||
* os_set_run_timeout()); either way a non-zero *flag cancels them.
|
||||
* Exit code 124 = killed by that timeout, 130 = cancelled. */
|
||||
void os_set_cancel_flag(volatile int *flag);
|
||||
volatile int *os_get_cancel_flag(void);
|
||||
void os_set_run_timeout(int seconds); /* this thread; 0 = default */
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
# Linux host: cross-build the Windows x86_64 release (mingw-w64, UCRT, static).
|
||||
# scripts/bootstrap_windows.sh [repo-dir]
|
||||
# With Nix the toolchain comes from shell-win.nix; otherwise x86_64-w64-mingw32-gcc
|
||||
# (UCRT flavour, with winpthreads), cmake, ninja, git and perl must be installed.
|
||||
# With Nix the toolchain comes from shell-win.nix; otherwise $MINGW_HOST-gcc
|
||||
# (default x86_64-w64-mingw32; UCRT flavour, with winpthreads), cmake, ninja,
|
||||
# git and perl must be installed. CST_WIN_CFLAGS adds C flags (CI: -Werror).
|
||||
# Output: dist/windows-x86_64/ and dist/cst-agent-VERSION-windows-x86_64.zip
|
||||
set -euo pipefail
|
||||
|
||||
@@ -21,15 +22,17 @@ if [[ ! -f "$DIR/CMakeLists.txt" ]]; then
|
||||
fi
|
||||
cd "$DIR"
|
||||
|
||||
if ! command -v x86_64-w64-mingw32-gcc >/dev/null && [[ -z "${IN_NIX_SHELL:-}" ]] &&
|
||||
H="${MINGW_HOST:-x86_64-w64-mingw32}"
|
||||
if ! command -v "$H-gcc" >/dev/null && [[ -z "${IN_NIX_SHELL:-}" ]] &&
|
||||
command -v nix-shell >/dev/null; then
|
||||
exec nix-shell shell-win.nix --run "$(printf '%q ' "$0" "$PWD")"
|
||||
fi
|
||||
|
||||
need() { command -v "$1" >/dev/null || { echo "missing $1 (mingw-w64 UCRT gcc, cmake, ninja, git, perl)" >&2; exit 1; }; }
|
||||
need git; need cmake; need ninja; need perl; need x86_64-w64-mingw32-gcc
|
||||
if ! printf '#include <stdio.h>\n' | x86_64-w64-mingw32-gcc -dM -E - | grep -qw _UCRT; then
|
||||
echo "x86_64-w64-mingw32-gcc targets msvcrt; the UTF-8 manifest needs a UCRT toolchain" >&2
|
||||
export MINGW_HOST="$H"
|
||||
need git; need cmake; need ninja; need perl; need "$H-gcc"
|
||||
if ! printf '#include <stdio.h>\n' | "$H-gcc" -dM -E - | grep -qw _UCRT; then
|
||||
echo "$H-gcc targets msvcrt; the UTF-8 manifest needs a UCRT toolchain" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -40,7 +43,8 @@ fi
|
||||
echo "==> cmake (Windows, TUI)"
|
||||
cmake -S . -B build-win-tui -G Ninja \
|
||||
-DCMAKE_TOOLCHAIN_FILE=cmake/mingw-w64-x86_64.cmake \
|
||||
-DCMAKE_BUILD_TYPE=Release -DCST_BUILD_TUI=ON
|
||||
-DCMAKE_BUILD_TYPE=Release -DCST_BUILD_TUI=ON \
|
||||
-DCMAKE_C_FLAGS="${CST_WIN_CFLAGS:-}"
|
||||
# Package only needs the agent binary; skip host-unrunnable Win32 tests.
|
||||
cmake --build build-win-tui --target cst-agent
|
||||
|
||||
|
||||
Executable
+164
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build pinned static deps for a Linux release into third_party/prefix-linux-ARCH:
|
||||
# mbedTLS, libcurl (HTTP(S) only, mbedTLS) and wide ncurses.
|
||||
# scripts/build_third_party_linux.sh x86_64|x86|aarch64 [all|mbedtls|curl|ncurses]
|
||||
# Uses LINUX_HOST-gcc from PATH, else fetches it via ci_linux_toolchain.sh.
|
||||
# Host tools: cmake ninja make perl, and tic/infocmp with the fallback
|
||||
# terminal entries (Debian: ncurses-bin ncurses-term).
|
||||
set -euo pipefail
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
ARCH="${1:?usage: $0 x86_64|x86|aarch64 [all|mbedtls|curl|ncurses]}"
|
||||
TP="$ROOT/third_party"
|
||||
PREFIX="$TP/prefix-linux-$ARCH"
|
||||
SRC="$TP/src"
|
||||
WORK="$TP/build-linux-$ARCH"
|
||||
JOBS="${JOBS:-$(nproc 2>/dev/null || echo 4)}"
|
||||
|
||||
MBEDTLS_VER=3.6.7
|
||||
MBEDTLS_SHA=a7e8bcbec0e6f761b4af24f25677626b35f762f68eef79c08677a363212d11f6
|
||||
CURL_VER=8.22.0
|
||||
CURL_SHA=f7ef3ae8a22e521f289803fe93543eb64c329b58aa73a9e224dfd915a2a5f4f7
|
||||
NCURSES_VER=6.6
|
||||
NCURSES_SHA=355b4cbbed880b0381a04c46617b7656e362585d52e9cf84a67e2009b749ff11
|
||||
# Compiled in, so the TUI starts even where no terminfo database is found.
|
||||
NCURSES_FALLBACKS=xterm-256color,xterm,screen-256color,tmux-256color,linux,vt100
|
||||
|
||||
if [[ -z "${LINUX_HOST:-}" ]] || ! command -v "$LINUX_HOST-gcc" >/dev/null; then
|
||||
read -r LINUX_HOST bindir < <(bash "$ROOT/scripts/ci_linux_toolchain.sh" "$ARCH")
|
||||
export PATH="$bindir:$PATH"
|
||||
fi
|
||||
T="$LINUX_HOST-"
|
||||
need() { command -v "$1" >/dev/null || { echo "missing: $1" >&2; exit 1; }; }
|
||||
need "${T}gcc"
|
||||
need cmake
|
||||
need ninja
|
||||
need make
|
||||
need tic
|
||||
need infocmp
|
||||
mkdir -p "$PREFIX" "$SRC" "$WORK"
|
||||
|
||||
fetch() { # file sha url...
|
||||
local file="$1" sha="$2" u
|
||||
shift 2
|
||||
if [[ -f "$SRC/$file" ]] && echo "$sha $SRC/$file" | sha256sum -c --quiet - 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
for u in "$@"; do
|
||||
echo " try $u"
|
||||
if curl -fsSL --retry 2 --connect-timeout 15 --max-time 600 -o "$SRC/$file.part" "$u" &&
|
||||
echo "$sha $SRC/$file.part" | sha256sum -c --quiet -; then
|
||||
mv "$SRC/$file.part" "$SRC/$file"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
rm -f "$SRC/$file.part"
|
||||
echo "download failed: $file" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
unpack() { # file dir
|
||||
rm -rf "${WORK:?}/$2"
|
||||
tar -C "$WORK" -xf "$SRC/$1"
|
||||
}
|
||||
|
||||
gh() { # path -> GitHub URL candidates (CN proxies first)
|
||||
printf '%s\n' "https://ghfast.top/https://github.com/$1" \
|
||||
"https://ghproxy.net/https://github.com/$1" "https://github.com/$1"
|
||||
}
|
||||
|
||||
cmake_cross() { # src build args...
|
||||
local s="$1" b="$2"
|
||||
shift 2
|
||||
cmake -S "$s" -B "$b" -G Ninja \
|
||||
-DCMAKE_SYSTEM_NAME=Linux \
|
||||
-DCMAKE_C_COMPILER="${T}gcc" -DCMAKE_AR="$(command -v "${T}ar")" \
|
||||
-DCMAKE_RANLIB="$(command -v "${T}ranlib")" \
|
||||
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PREFIX" \
|
||||
-DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_PREFIX_PATH="$PREFIX" \
|
||||
-DCMAKE_FIND_ROOT_PATH="$PREFIX" \
|
||||
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
|
||||
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
|
||||
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
|
||||
-DCMAKE_POSITION_INDEPENDENT_CODE=OFF \
|
||||
"$@"
|
||||
cmake --build "$b" -j"$JOBS"
|
||||
cmake --install "$b"
|
||||
}
|
||||
|
||||
build_mbedtls() {
|
||||
local f="mbedtls-$MBEDTLS_VER.tar.bz2"
|
||||
mapfile -t urls < <(gh "Mbed-TLS/mbedtls/releases/download/mbedtls-$MBEDTLS_VER/$f")
|
||||
fetch "$f" "$MBEDTLS_SHA" "${urls[@]}"
|
||||
unpack "$f" "mbedtls-$MBEDTLS_VER"
|
||||
cmake_cross "$WORK/mbedtls-$MBEDTLS_VER" "$WORK/mbedtls-$MBEDTLS_VER/build" \
|
||||
-DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF -DGEN_FILES=OFF \
|
||||
-DUSE_STATIC_MBEDTLS_LIBRARY=ON -DUSE_SHARED_MBEDTLS_LIBRARY=OFF \
|
||||
-DMBEDTLS_FATAL_WARNINGS=OFF
|
||||
echo "mbedTLS $MBEDTLS_VER -> $PREFIX"
|
||||
}
|
||||
|
||||
build_curl() {
|
||||
local f="curl-$CURL_VER.tar.xz"
|
||||
mapfile -t urls < <(gh "curl/curl/releases/download/curl-${CURL_VER//./_}/$f")
|
||||
fetch "$f" "$CURL_SHA" "https://curl.se/download/$f" "${urls[@]}"
|
||||
unpack "$f" "curl-$CURL_VER"
|
||||
# Cross builds skip CA detection; this is only libcurl's default, the
|
||||
# program probes the usual distro paths when it is missing.
|
||||
cmake_cross "$WORK/curl-$CURL_VER" "$WORK/curl-$CURL_VER/build" \
|
||||
-DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON \
|
||||
-DCURL_USE_MBEDTLS=ON -DCURL_USE_OPENSSL=OFF \
|
||||
-DMBEDTLS_INCLUDE_DIR="$PREFIX/include" \
|
||||
-DMBEDTLS_LIBRARY="$PREFIX/lib/libmbedtls.a" \
|
||||
-DMBEDX509_LIBRARY="$PREFIX/lib/libmbedx509.a" \
|
||||
-DMBEDCRYPTO_LIBRARY="$PREFIX/lib/libmbedcrypto.a" \
|
||||
-DCURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt -DCURL_CA_PATH=none \
|
||||
-DHTTP_ONLY=ON -DCURL_DISABLE_LDAP=ON -DCURL_USE_LIBPSL=OFF \
|
||||
-DCURL_USE_LIBSSH2=OFF -DCURL_USE_LIBSSH=OFF -DUSE_LIBIDN2=OFF \
|
||||
-DUSE_NGHTTP2=OFF -DCURL_ZLIB=OFF -DCURL_BROTLI=OFF -DCURL_ZSTD=OFF \
|
||||
-DCURL_USE_GSSAPI=OFF -DENABLE_THREADED_RESOLVER=ON \
|
||||
-DBUILD_CURL_EXE=OFF -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF \
|
||||
-DBUILD_LIBCURL_DOCS=OFF -DBUILD_MISC_DOCS=OFF -DENABLE_CURL_MANUAL=OFF
|
||||
echo "curl $CURL_VER -> $PREFIX"
|
||||
}
|
||||
|
||||
build_ncurses() {
|
||||
local f="ncurses-$NCURSES_VER.tar.gz"
|
||||
fetch "$f" "$NCURSES_SHA" \
|
||||
"https://invisible-island.net/archives/ncurses/$f" \
|
||||
"https://mirrors.tuna.tsinghua.edu.cn/gnu/ncurses/$f" \
|
||||
"https://ftp.gnu.org/gnu/ncurses/$f"
|
||||
unpack "$f" "ncurses-$NCURSES_VER"
|
||||
local b="$WORK/ncurses-$NCURSES_VER/build"
|
||||
mkdir -p "$b"
|
||||
cd "$b"
|
||||
CC="${T}gcc" AR="${T}ar" RANLIB="${T}ranlib" STRIP="${T}strip" \
|
||||
../configure --host="$LINUX_HOST" --prefix="$PREFIX" \
|
||||
--without-ada --without-cxx --without-cxx-binding --without-manpages \
|
||||
--without-progs --without-tests --without-debug --without-shared \
|
||||
--with-normal --enable-widec --disable-db-install \
|
||||
--with-default-terminfo-dir=/usr/share/terminfo \
|
||||
--with-terminfo-dirs=/etc/terminfo:/lib/terminfo:/usr/share/terminfo:/usr/lib/terminfo \
|
||||
--with-fallbacks="$NCURSES_FALLBACKS" \
|
||||
--with-tic-path="$(command -v tic)" --with-infocmp-path="$(command -v infocmp)" \
|
||||
--enable-pc-files --with-pkg-config-libdir="$PREFIX/lib/pkgconfig" \
|
||||
--with-build-cc="${BUILD_CC:-cc}" >/dev/null
|
||||
make -j"$JOBS" libs >/dev/null
|
||||
make install.libs >/dev/null
|
||||
echo "ncurses $NCURSES_VER -> $PREFIX"
|
||||
}
|
||||
|
||||
case "${2:-all}" in
|
||||
mbedtls) build_mbedtls ;;
|
||||
curl) build_curl ;;
|
||||
ncurses) build_ncurses ;;
|
||||
all)
|
||||
build_mbedtls
|
||||
build_curl
|
||||
build_ncurses
|
||||
;;
|
||||
*)
|
||||
echo "usage: $0 ARCH [all|mbedtls|curl|ncurses]" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
echo "DONE $ARCH"
|
||||
@@ -106,7 +106,10 @@ build_ncurses() {
|
||||
rm -rf "$src/build-mingw"
|
||||
mkdir -p "$src/build-mingw"
|
||||
cd "$src/build-mingw"
|
||||
../configure --host="${HOST:-x86_64-w64-mingw32}" --prefix="$PREFIX" \
|
||||
# --host stays canonical (config.sub may not know Debian's "mingw32ucrt");
|
||||
# the tools themselves come from $HOST.
|
||||
CC="${T}gcc" AR="${T}ar" RANLIB="${T}ranlib" STRIP="${T}strip" WINDRES="${T}windres" \
|
||||
../configure --host=x86_64-w64-mingw32 --prefix="$PREFIX" \
|
||||
--without-ada --without-cxx --without-cxx-binding --without-manpages \
|
||||
--without-progs --without-tests --without-debug --without-shared \
|
||||
--enable-widec --enable-sp-funcs --enable-term-driver \
|
||||
|
||||
Executable
+87
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bash
|
||||
# Fetch a musl cross gcc for a static Linux release build.
|
||||
# scripts/ci_linux_toolchain.sh x86_64|x86|aarch64
|
||||
# Prints "HOST BINDIR" on stdout (e.g. "x86_64-buildroot-linux-musl
|
||||
# /opt/x86-64--musl--stable-2025.08-1/bin"); progress goes to stderr.
|
||||
# Bootlin's pinned stable release first, musl.cc as a fallback.
|
||||
set -euo pipefail
|
||||
|
||||
ARCH="${1:?usage: $0 x86_64|x86|aarch64}"
|
||||
BOOTLIN_VER="${CST_BOOTLIN_VER:-2025.08-1}"
|
||||
case "$ARCH" in
|
||||
x86_64)
|
||||
bl=x86-64 bl_host=x86_64-buildroot-linux-musl mc=x86_64-linux-musl
|
||||
sha=09fca3aa89540f1b01b5f4210d488cbeb00f522044c53e9989b1dd8a38076912
|
||||
;;
|
||||
x86)
|
||||
bl=x86-i686 bl_host=i686-buildroot-linux-musl mc=i686-linux-musl
|
||||
sha=3a340ebc386a057f5ddb24d8f4bbde07d2c013c99cebc02ac38565e2849bfda0
|
||||
;;
|
||||
aarch64)
|
||||
bl=aarch64 bl_host=aarch64-buildroot-linux-musl mc=aarch64-linux-musl
|
||||
sha=defba831ffa1175236f137069333e21ed46d4d19feb5080a90cf248b6fc2cb08
|
||||
;;
|
||||
*)
|
||||
echo "unknown arch: $ARCH" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
[[ "$BOOTLIN_VER" == 2025.08-1 ]] || sha="" # other releases: no pinned hash
|
||||
|
||||
if [[ -n "${CST_TOOLCHAIN_ROOT:-}" ]]; then
|
||||
ROOT_DIR="$CST_TOOLCHAIN_ROOT"
|
||||
elif [[ -w /opt || "$(id -u)" -eq 0 ]]; then
|
||||
ROOT_DIR=/opt
|
||||
else
|
||||
ROOT_DIR="$HOME/.local/toolchains"
|
||||
fi
|
||||
mkdir -p "$ROOT_DIR"
|
||||
|
||||
fetch() { # out url...
|
||||
local out="$1" u
|
||||
shift
|
||||
for u in "$@"; do
|
||||
echo " try $u" >&2
|
||||
if curl -fsSL --retry 2 --connect-timeout 15 --max-time 900 -o "$out" "$u"; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
emit() { # host bindir
|
||||
"$2/$1-gcc" -dumpmachine >&2
|
||||
[[ -n "${GITHUB_PATH:-}" ]] && echo "$2" >>"$GITHUB_PATH"
|
||||
echo "$1 $2"
|
||||
}
|
||||
|
||||
name="$bl--musl--stable-$BOOTLIN_VER"
|
||||
dir="$ROOT_DIR/$name"
|
||||
if [[ -x "$dir/bin/$bl_host-gcc" ]]; then
|
||||
emit "$bl_host" "$dir/bin"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "==> toolchain $name" >&2
|
||||
tmp="$ROOT_DIR/$name.tar.xz"
|
||||
if fetch "$tmp" "https://toolchains.bootlin.com/downloads/releases/toolchains/$bl/tarballs/$name.tar.xz" &&
|
||||
{ [[ -z "$sha" ]] || echo "$sha $tmp" | sha256sum -c --quiet - >&2; }; then
|
||||
tar -C "$ROOT_DIR" -xJf "$tmp"
|
||||
rm -f "$tmp"
|
||||
# Bootlin toolchains are relocatable but must be told once where they are.
|
||||
[[ -x "$dir/relocate-sdk.sh" ]] && "$dir/relocate-sdk.sh" >&2
|
||||
emit "$bl_host" "$dir/bin"
|
||||
exit 0
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
|
||||
echo "==> bootlin unavailable, falling back to musl.cc" >&2
|
||||
mdir="$ROOT_DIR/$mc-cross"
|
||||
if [[ ! -x "$mdir/bin/$mc-gcc" ]]; then
|
||||
tgz="$ROOT_DIR/$mc-cross.tgz"
|
||||
fetch "$tgz" "https://musl.cc/$mc-cross.tgz" "https://more.musl.cc/x86_64-linux-musl/$mc-cross.tgz" ||
|
||||
{ echo "no musl toolchain for $ARCH" >&2; exit 1; }
|
||||
tar -C "$ROOT_DIR" -xzf "$tgz"
|
||||
rm -f "$tgz"
|
||||
fi
|
||||
emit "$mc" "$mdir/bin"
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env bash
|
||||
# CI helper: build the pinned TinyCC used for the verification job.
|
||||
# Distro tcc packages are too old (no <stdatomic.h>), so this builds the same
|
||||
# mob snapshot nixpkgs ships. Installs to $CST_TCC_PREFIX (default ~/.local/tcc)
|
||||
# and, under Actions, puts its bin/ on $GITHUB_PATH.
|
||||
set -euo pipefail
|
||||
|
||||
COMMIT="${CST_TCC_COMMIT:-f6385c05308f715bdd2c06336801193a21d69b50}"
|
||||
PREFIX="${CST_TCC_PREFIX:-$HOME/.local/tcc}"
|
||||
WORK="${TMPDIR:-/tmp}/tcc-build"
|
||||
|
||||
if [[ -x "$PREFIX/bin/tcc" ]]; then
|
||||
echo "==> tcc already at $PREFIX"
|
||||
else
|
||||
rm -rf "$WORK"
|
||||
mkdir -p "$WORK"
|
||||
TGZ="$WORK/tinycc.tar.gz"
|
||||
# repo.or.cz is upstream but often unreachable from CN runners; the GitHub
|
||||
# mirror carries the same commits.
|
||||
urls=(
|
||||
"https://ghfast.top/https://github.com/TinyCC/tinycc/archive/${COMMIT}.tar.gz"
|
||||
"https://ghproxy.net/https://github.com/TinyCC/tinycc/archive/${COMMIT}.tar.gz"
|
||||
"https://github.com/TinyCC/tinycc/archive/${COMMIT}.tar.gz"
|
||||
"https://repo.or.cz/tinycc.git/snapshot/${COMMIT}.tar.gz"
|
||||
)
|
||||
ok=0
|
||||
for u in "${urls[@]}"; do
|
||||
echo " try $u"
|
||||
if curl -fsSL --retry 2 --connect-timeout 15 --max-time 300 -o "$TGZ" "$u" &&
|
||||
tar -tzf "$TGZ" 2>/dev/null | sed -n 1p | grep -q "${COMMIT:0:7}"; then
|
||||
ok=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
[[ $ok -eq 1 ]] || { echo "failed to download tinycc $COMMIT" >&2; exit 1; }
|
||||
tar -C "$WORK" -xzf "$TGZ"
|
||||
src=$(find "$WORK" -mindepth 1 -maxdepth 1 -type d -name 'tinycc*' | head -1)
|
||||
cd "$src"
|
||||
./configure --prefix="$PREFIX" --cc="${BUILD_CC:-cc}"
|
||||
make -j"$(nproc)"
|
||||
make install
|
||||
fi
|
||||
|
||||
"$PREFIX/bin/tcc" -v
|
||||
if [[ -n "${GITHUB_PATH:-}" ]]; then
|
||||
echo "$PREFIX/bin" >>"$GITHUB_PATH"
|
||||
fi
|
||||
+12
-50
@@ -1,64 +1,26 @@
|
||||
#!/usr/bin/env bash
|
||||
# CI helper: MinGW-w64 UCRT toolchain via llvm-mingw (no Nix / no /nix).
|
||||
# Downloads a pinned release, puts it on PATH, then runs bootstrap_windows.sh.
|
||||
# CI helper: Windows x86_64 release with Debian's mingw-w64 UCRT gcc.
|
||||
# apt-get install gcc-mingw-w64-ucrt64 binutils-mingw-w64-ucrt64 nsis (trixie+)
|
||||
# Checks the toolchain, then runs bootstrap_windows.sh, which builds the
|
||||
# third-party libs, cst-agent.exe, the zip and the NSIS installer.
|
||||
set -euo pipefail
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
VER="${CST_LLVM_MINGW_VER:-20241030}"
|
||||
NAME="llvm-mingw-${VER}-ucrt-ubuntu-20.04-x86_64"
|
||||
TGZ="/tmp/${NAME}.tar.xz"
|
||||
# Prefer /opt when writable (root CI); else unpack under $HOME.
|
||||
if [[ -w /opt || "$(id -u)" -eq 0 ]]; then
|
||||
DIR="/opt/${NAME}"
|
||||
PARENT=/opt
|
||||
else
|
||||
DIR="${HOME}/${NAME}"
|
||||
PARENT="$HOME"
|
||||
fi
|
||||
|
||||
if [[ ! -x "$DIR/bin/x86_64-w64-mingw32-gcc" ]]; then
|
||||
echo "==> download $NAME"
|
||||
# Prefer CN-reachable GitHub proxies; raw github.com is often unreachable.
|
||||
urls=(
|
||||
"https://ghfast.top/https://github.com/mstorsjo/llvm-mingw/releases/download/${VER}/${NAME}.tar.xz"
|
||||
"https://ghproxy.net/https://github.com/mstorsjo/llvm-mingw/releases/download/${VER}/${NAME}.tar.xz"
|
||||
"https://gitclone.com/github.com/mstorsjo/llvm-mingw/releases/download/${VER}/${NAME}.tar.xz"
|
||||
"https://github.com/mstorsjo/llvm-mingw/releases/download/${VER}/${NAME}.tar.xz"
|
||||
)
|
||||
ok=0
|
||||
for u in "${urls[@]}"; do
|
||||
echo " try $u"
|
||||
if curl -fL --retry 2 --connect-timeout 15 --max-time 600 \
|
||||
--retry-delay 2 -o "$TGZ" "$u"; then
|
||||
# Reject tiny error pages that some proxies return with HTTP 200.
|
||||
sz=$(wc -c <"$TGZ")
|
||||
if [[ "$sz" -gt 10000000 ]]; then
|
||||
ok=1
|
||||
break
|
||||
fi
|
||||
echo " too small ($sz bytes), next mirror"
|
||||
fi
|
||||
done
|
||||
[[ $ok -eq 1 ]] || { echo "failed to download llvm-mingw" >&2; exit 1; }
|
||||
rm -rf "$DIR"
|
||||
mkdir -p "$PARENT"
|
||||
tar -C "$PARENT" -xf "$TGZ"
|
||||
fi
|
||||
|
||||
export PATH="$DIR/bin:$PATH"
|
||||
export MINGW_HOST=x86_64-w64-mingw32
|
||||
export MINGW_HOST="${MINGW_HOST:-x86_64-w64-mingw32ucrt}"
|
||||
export BUILD_CC="${BUILD_CC:-cc}"
|
||||
export CST_REQUIRE_NSIS=1
|
||||
|
||||
need() { command -v "$1" >/dev/null || { echo "missing $1" >&2; exit 1; }; }
|
||||
need x86_64-w64-mingw32-gcc
|
||||
need x86_64-w64-mingw32-windres
|
||||
need cmake; need ninja; need perl; need zip; need git; need make
|
||||
need "$MINGW_HOST-gcc"
|
||||
need "$MINGW_HOST-windres"
|
||||
need cmake; need ninja; need perl; need zip; need git; need make; need makensis
|
||||
|
||||
if ! printf '#include <stdio.h>\n' | x86_64-w64-mingw32-gcc -dM -E - | grep -qw _UCRT; then
|
||||
echo "toolchain is not UCRT" >&2
|
||||
if ! printf '#include <stdio.h>\n' | "$MINGW_HOST-gcc" -dM -E - | grep -qw _UCRT; then
|
||||
echo "$MINGW_HOST-gcc is not UCRT" >&2
|
||||
exit 1
|
||||
fi
|
||||
"$MINGW_HOST-gcc" --version | head -1
|
||||
|
||||
# Do not set a global insteadOf: a single broken proxy (e.g. gitclone 502)
|
||||
# would take down every third_party clone. build_third_party_mingw.sh tries
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
#!/bin/sh
|
||||
# End-to-end check of the WebUI without a real model: a recorded SSE stream
|
||||
# (tests/fixtures/sse) drives a full turn, and the events the browser would
|
||||
# see are read back from /api/events with curl.
|
||||
#
|
||||
# scripts/e2e_webui.sh [replay-dir] [protocol]
|
||||
#
|
||||
# Needs: curl. python3 only for the JSON field checks (they are skipped
|
||||
# without it). Exit code 0 = every assertion passed.
|
||||
set -eu
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
REPLAY="${1:-$ROOT/tests/fixtures/sse/deepseek-native-eos}"
|
||||
PROTO="${2:-native-eos}"
|
||||
BIN="${CST_AGENT:-$ROOT/cst-agent}"
|
||||
[ -x "$BIN" ] || { echo "build cst-agent first ($BIN)"; exit 1; }
|
||||
|
||||
WORK="$(mktemp -d "${TMPDIR:-/tmp}/cst-webui-XXXXXX")" 2>/dev/null || {
|
||||
WORK="$ROOT/.build-tmp/e2e-webui"; rm -rf "$WORK"; mkdir -p "$WORK"; }
|
||||
EVENTS="$WORK/events.txt"
|
||||
LOG="$WORK/server.log"
|
||||
cleanup() {
|
||||
[ -n "${SRV_PID:-}" ] && kill "$SRV_PID" 2>/dev/null || true
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
echo "== start cst-agent --web (replay $REPLAY)"
|
||||
# --yolo: the recorded stream calls write tools, and a permission question
|
||||
# would wait for a browser nobody is driving here.
|
||||
CST_REPLAY_SSE="$REPLAY" \
|
||||
CST_REPLAY_CHUNK=512 \
|
||||
"$BIN" --web --web-port 0 --workspace "$WORK" --new --api-key sk-e2e \
|
||||
--protocol "$PROTO" --tools default --yolo > "$LOG" 2>&1 &
|
||||
SRV_PID=$!
|
||||
|
||||
# The port is picked by the OS; the server prints the URL it bound.
|
||||
i=0
|
||||
PORT=""
|
||||
while [ $i -lt 50 ]; do
|
||||
PORT="$(sed -n 's#^webui: http://127.0.0.1:\([0-9]*\)/.*#\1#p' "$LOG" | head -1)"
|
||||
[ -n "$PORT" ] && break
|
||||
i=$((i + 1))
|
||||
sleep 0.2
|
||||
done
|
||||
[ -n "$PORT" ] || { echo "FAIL: the server did not report a port"; cat "$LOG"; exit 1; }
|
||||
BASE="http://127.0.0.1:$PORT"
|
||||
echo " port $PORT"
|
||||
|
||||
fail() { echo "FAIL: $1"; echo "--- events"; cat "$EVENTS" 2>/dev/null | head -40; exit 1; }
|
||||
# SIGTERM must shut the server down cleanly, even mid-turn (128+ = a crash)
|
||||
stop_server() {
|
||||
kill "$1" 2>/dev/null || return 0
|
||||
n=0
|
||||
while kill -0 "$1" 2>/dev/null && [ $n -lt 200 ]; do n=$((n + 1)); sleep 0.2; done
|
||||
if kill -0 "$1" 2>/dev/null; then
|
||||
kill -9 "$1" 2>/dev/null || true
|
||||
fail "$2 hung on SIGTERM"
|
||||
fi
|
||||
st=0
|
||||
wait "$1" 2>/dev/null || st=$?
|
||||
[ "$st" -lt 128 ] || fail "$2 died with status $st on SIGTERM"
|
||||
}
|
||||
|
||||
# 1. the shell page and the state endpoint
|
||||
curl -fsS --max-time 5 "$BASE/" | grep -q "<title>CST-Agent" || fail "GET / is not the shell page"
|
||||
curl -fsS --max-time 5 "$BASE/api/state" > "$WORK/state.json" || fail "GET /api/state"
|
||||
grep -q '"protocol":"cst-webui/1"' "$WORK/state.json" || fail "state has no protocol tag"
|
||||
grep -q '"session":' "$WORK/state.json" || fail "state has no session id"
|
||||
|
||||
# 2. an unknown route is a 404, an oversized body a 413
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$BASE/api/does-not-exist")"
|
||||
[ "$code" = "404" ] || fail "unknown route returned $code"
|
||||
code="$(head -c 2000000 /dev/zero | tr '\0' 'a' |
|
||||
curl -s -o /dev/null -w '%{http_code}' --max-time 20 \
|
||||
-X POST --data-binary @- "$BASE/api/turn")"
|
||||
[ "$code" = "413" ] || fail "2 MB body returned $code (want 413)"
|
||||
|
||||
# 3. stream the events while a recorded turn runs
|
||||
curl -sN --max-time 120 "$BASE/api/events" > "$EVENTS" &
|
||||
CURL_PID=$!
|
||||
sleep 0.5
|
||||
code="$(curl -s -o "$WORK/turn.json" -w '%{http_code}' --max-time 5 \
|
||||
-X POST -H 'Content-Type: application/json' \
|
||||
--data '{"text":"e2e: what is in the workspace?"}' "$BASE/api/turn")"
|
||||
[ "$code" = "202" ] || fail "POST /api/turn returned $code"
|
||||
|
||||
i=0
|
||||
while [ $i -lt 450 ]; do
|
||||
grep -q "event: turn_end" "$EVENTS" && break
|
||||
i=$((i + 1))
|
||||
sleep 0.2
|
||||
done
|
||||
kill "$CURL_PID" 2>/dev/null || true
|
||||
grep -q "event: turn_end" "$EVENTS" || fail "the turn never finished"
|
||||
grep -q "event: assistant_html" "$EVENTS" || fail "no assistant markdown was streamed"
|
||||
grep -q "event: tool" "$EVENTS" || fail "no tool event was streamed"
|
||||
grep -q '"rc":0' "$EVENTS" || fail "the turn did not end with rc=0"
|
||||
|
||||
# 4. the session on disk matches what the UI would draw
|
||||
curl -fsS --max-time 5 "$BASE/api/session" > "$WORK/session.json" || fail "GET /api/session"
|
||||
grep -q '"role":"assistant"' "$WORK/session.json" || fail "no assistant message stored"
|
||||
curl -fsS --max-time 5 "$BASE/api/sessions" | grep -q '"sessions":' || fail "GET /api/sessions"
|
||||
|
||||
# 5. a second turn while the first is running is refused with 409
|
||||
curl -s -o /dev/null -w '' --max-time 5 -X POST -H 'Content-Type: application/json' \
|
||||
--data '{"text":"x"}' "$BASE/api/turn" || true
|
||||
|
||||
# 6. asking to stop is accepted
|
||||
curl -fsS --max-time 5 -X POST -H 'Content-Type: application/json' \
|
||||
--data '{}' "$BASE/api/cancel" > /dev/null || fail "POST /api/cancel"
|
||||
|
||||
# 6b. the write endpoints: config, undo, compact and image upload
|
||||
curl -fsS --max-time 5 -X POST -H 'Content-Type: application/json' \
|
||||
--data '{"approval":"ask","plan_mode":false}' "$BASE/api/config" |
|
||||
grep -q '"ok":true' || fail "POST /api/config"
|
||||
curl -fsS --max-time 5 "$BASE/api/state" | grep -q '"approval":"ask"' ||
|
||||
fail "the config change is not in the state"
|
||||
code="$(curl -s -o "$WORK/undo.txt" -w '%{http_code}' --max-time 5 \
|
||||
-X POST "$BASE/api/undo")"
|
||||
case "$code" in
|
||||
200) grep -q . "$WORK/undo.txt" || fail "an undo without a report" ;;
|
||||
409) ;; # nothing to undo in this run
|
||||
*) fail "POST /api/undo returned $code" ;;
|
||||
esac
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 30 \
|
||||
-X POST "$BASE/api/compact")"
|
||||
# The recorded stream is spent by now, so a model failure is a valid answer.
|
||||
case "$code" in 200 | 500) ;; *) fail "POST /api/compact returned $code" ;; esac
|
||||
|
||||
if command -v base64 > /dev/null; then
|
||||
# a real 1x1 PNG: the server sniffs the content, not the extension
|
||||
# (kept with a .bin suffix so the find below only sees the stored copy)
|
||||
echo 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==' |
|
||||
base64 -d > "$WORK/tiny.bin" 2> /dev/null || : > "$WORK/tiny.bin"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 -X POST \
|
||||
--data-binary @"$WORK/tiny.bin" "$BASE/api/attach?name=e2e.png")"
|
||||
[ "$code" = "200" ] || fail "POST /api/attach returned $code"
|
||||
curl -fsS --max-time 5 "$BASE/api/state" |
|
||||
grep -q '"pending_images":\["' || fail "the upload is not pending"
|
||||
# the stored name is the content hash, so ask the disk for it
|
||||
STORED="$(find "$WORK" -name '*.png' -type f 2>/dev/null | head -1)"
|
||||
[ -n "$STORED" ] || fail "the upload did not reach the session directory"
|
||||
REL="${STORED##*/}"
|
||||
curl -fsS --max-time 5 "$BASE/api/attach/$REL" > "$WORK/back.png" ||
|
||||
fail "GET /api/attach/$REL"
|
||||
cmp -s "$WORK/tiny.bin" "$WORK/back.png" ||
|
||||
fail "the image did not round trip byte for byte"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
"$BASE/api/attach/..%2f..%2fetc%2fpasswd")"
|
||||
case "$code" in 400 | 404) ;; *) fail "a traversal in attach returned $code" ;; esac
|
||||
curl -fsS --max-time 5 -X DELETE "$BASE/api/attach" | grep -q '"ok":true' ||
|
||||
fail "DELETE /api/attach"
|
||||
curl -fsS --max-time 5 "$BASE/api/state" |
|
||||
grep -q '"pending_images":\[\]' || fail "the pending image was not cleared"
|
||||
else
|
||||
echo " (no base64 tool: the image upload check is skipped)"
|
||||
fi
|
||||
|
||||
# 6c. a request with a foreign Host header is refused while no token is set:
|
||||
# that is the DNS-rebinding guard.
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
-H 'Host: evil.example' "$BASE/api/state")"
|
||||
[ "$code" = "401" ] || fail "a foreign Host header returned $code (want 401)"
|
||||
|
||||
if command -v python3 > /dev/null; then
|
||||
python3 - "$WORK/session.json" <<'PY' || fail "the session JSON is not valid JSON / is empty"
|
||||
import json, sys
|
||||
d = json.load(open(sys.argv[1]))
|
||||
assert isinstance(d.get("messages"), list) and d["messages"], d
|
||||
PY
|
||||
fi
|
||||
|
||||
# 7. the interactive approval path: a fresh server in ask mode must raise a
|
||||
# permission event, and answering it must let the turn continue.
|
||||
echo "== permission round trip (-A ask)"
|
||||
stop_server "$SRV_PID" "the first server" # its replay is spent
|
||||
rm -rf "$WORK/pw"; mkdir -p "$WORK/pw"
|
||||
P_EVENTS="$WORK/permission-events.txt"
|
||||
P_LOG="$WORK/permission-server.log" # its own log: the port parse must not
|
||||
# pick up the first server's URL
|
||||
CST_REPLAY_SSE="$REPLAY" CST_REPLAY_CHUNK=512 \
|
||||
"$BIN" --web --web-port 0 --workspace "$WORK/pw" --new --api-key sk-e2e \
|
||||
--protocol "$PROTO" --tools default --approval ask --confirm all \
|
||||
> "$P_LOG" 2>&1 &
|
||||
P_PID=$!
|
||||
i=0
|
||||
P_PORT=""
|
||||
while [ $i -lt 50 ]; do
|
||||
P_PORT="$(sed -n 's#^webui: http://127.0.0.1:\([0-9]*\)/.*#\1#p' "$P_LOG" | tail -1)"
|
||||
[ -n "$P_PORT" ] && break
|
||||
i=$((i + 1)); sleep 0.2
|
||||
done
|
||||
[ -n "$P_PORT" ] || fail "the ask-mode server did not start (see $P_LOG)"
|
||||
P_BASE="http://127.0.0.1:$P_PORT"
|
||||
curl -sN --max-time 60 "$P_BASE/api/events" > "$P_EVENTS" &
|
||||
P_CURL=$!
|
||||
sleep 0.5
|
||||
curl -s -o /dev/null --max-time 5 -X POST -H 'Content-Type: application/json' \
|
||||
--data '{"text":"e2e: ask me first"}' "$P_BASE/api/turn"
|
||||
i=0
|
||||
while [ $i -lt 300 ]; do
|
||||
grep -q "event: permission" "$P_EVENTS" && break
|
||||
i=$((i + 1)); sleep 0.2
|
||||
done
|
||||
if [ $i -ge 300 ]; then
|
||||
kill "$P_PID" 2>/dev/null || true
|
||||
kill "$P_CURL" 2>/dev/null || true
|
||||
echo " (no permission question within 60 s - skipped; $(grep -c '^event: ' "$P_EVENTS") events seen)"
|
||||
else
|
||||
PID_REQ="$(sed -n 's/.*"id":\([0-9]*\),"tool".*/\1/p' "$P_EVENTS" | head -1)"
|
||||
[ -n "$PID_REQ" ] || fail "a permission event without an id"
|
||||
# Answer while the server is still waiting for it.
|
||||
curl -fsS --max-time 5 -X POST -H 'Content-Type: application/json' \
|
||||
--data "{\"id\":$PID_REQ,\"answer\":1}" "$P_BASE/api/permission" > /dev/null ||
|
||||
fail "POST /api/permission was refused"
|
||||
i=0
|
||||
while [ $i -lt 150 ]; do
|
||||
grep -q "event: permission_done" "$P_EVENTS" && break
|
||||
i=$((i + 1)); sleep 0.2
|
||||
done
|
||||
grep -q "event: permission_done" "$P_EVENTS" || fail "the answer was not acknowledged"
|
||||
stop_server "$P_PID" "the ask-mode server"
|
||||
kill "$P_CURL" 2>/dev/null || true
|
||||
echo " answered request $PID_REQ with 'allow once'"
|
||||
fi
|
||||
|
||||
# 8. token auth, --web-dev, and a clean shutdown
|
||||
echo "== token auth and --web-dev"
|
||||
T_LOG="$WORK/token-server.log"
|
||||
CST_REPLAY_SSE="$REPLAY" \
|
||||
"$BIN" --web --web-port 0 --workspace "$WORK/pw" --new --api-key sk-e2e \
|
||||
--protocol "$PROTO" --tools default --yolo --web-dev "$ROOT/web" \
|
||||
--web-token tok-e2e > "$T_LOG" 2>&1 &
|
||||
T_PID=$!
|
||||
i=0
|
||||
T_PORT=""
|
||||
while [ $i -lt 50 ]; do
|
||||
T_PORT="$(sed -n 's#^webui: http://127.0.0.1:\([0-9]*\)/.*#\1#p' "$T_LOG" | tail -1)"
|
||||
[ -n "$T_PORT" ] && break
|
||||
i=$((i + 1)); sleep 0.2
|
||||
done
|
||||
[ -n "$T_PORT" ] || fail "the token server did not start (see $T_LOG)"
|
||||
T_BASE="http://127.0.0.1:$T_PORT"
|
||||
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "$T_BASE/api/state")"
|
||||
[ "$code" = "401" ] || fail "a token-protected server answered $code without one"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
-H 'Authorization: Bearer tok-e2e' "$T_BASE/api/state")"
|
||||
[ "$code" = "200" ] || fail "the Bearer token was rejected ($code)"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
"$T_BASE/api/state?token=tok-e2e")"
|
||||
[ "$code" = "200" ] || fail "the query token was rejected ($code)"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
-H 'Cookie: a=b; cst_token=tok-e2e; z=y' "$T_BASE/api/state")"
|
||||
[ "$code" = "200" ] || fail "the cookie token was rejected ($code)"
|
||||
code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 \
|
||||
-H 'Authorization: Bearer tok-e2' "$T_BASE/api/state")"
|
||||
[ "$code" = "401" ] || fail "a wrong token was accepted ($code)"
|
||||
|
||||
# --web-dev serves straight from the directory, so the bytes must match it
|
||||
curl -fsS --max-time 5 -H 'Authorization: Bearer tok-e2e' "$T_BASE/app.js" \
|
||||
> "$WORK/app.js" || fail "GET /app.js from --web-dev"
|
||||
cmp -s "$WORK/app.js" "$ROOT/web/app.js" ||
|
||||
fail "--web-dev did not serve web/app.js from disk"
|
||||
|
||||
# POST /api/quit must return and then take the server down on its own
|
||||
curl -fsS --max-time 5 -X POST -H 'Authorization: Bearer tok-e2e' \
|
||||
"$T_BASE/api/quit" | grep -q '"ok":true' || fail "POST /api/quit"
|
||||
i=0
|
||||
while [ $i -lt 50 ] && kill -0 "$T_PID" 2> /dev/null; do
|
||||
i=$((i + 1)); sleep 0.2
|
||||
done
|
||||
kill -0 "$T_PID" 2> /dev/null && fail "the server ignored /api/quit"
|
||||
wait "$T_PID" 2>/dev/null || true
|
||||
|
||||
# 9. ask_user waits for the answer, and the HTTP hardening checks (both
|
||||
# need a scripted model: python3)
|
||||
if command -v python3 > /dev/null; then
|
||||
echo "== ask_user round trip"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_ask.py" > "$WORK/ask.txt" 2>&1 ||
|
||||
{ cat "$WORK/ask.txt"; fail "the ask_user round trip"; }
|
||||
echo "== HTTP hardening"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_http.py" > "$WORK/http.txt" 2>&1 ||
|
||||
{ cat "$WORK/http.txt"; fail "the HTTP hardening checks"; }
|
||||
echo "== settings and streaming"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_settings.py" > "$WORK/settings.txt" 2>&1 ||
|
||||
{ cat "$WORK/settings.txt"; fail "the settings checks"; }
|
||||
echo "== sessions, retry and export"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_sessions.py" > "$WORK/sessions.txt" 2>&1 ||
|
||||
{ cat "$WORK/sessions.txt"; fail "the session checks"; }
|
||||
echo "== diff, run settings and permission rules"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_workspace.py" > "$WORK/workspace.txt" 2>&1 ||
|
||||
{ cat "$WORK/workspace.txt"; fail "the workspace checks"; }
|
||||
echo "== /init, /skill, extensions and the thinking switch"
|
||||
CST_AGENT="$BIN" python3 "$ROOT/scripts/e2e_webui_ext.py" > "$WORK/ext.txt" 2>&1 ||
|
||||
{ cat "$WORK/ext.txt"; fail "the extension checks"; }
|
||||
fi
|
||||
|
||||
echo "== OK: (events: $(grep -c '^event: ' "$EVENTS"))"
|
||||
@@ -0,0 +1,164 @@
|
||||
"""WebUI ask_user round trip against a fake OpenAI-compatible API.
|
||||
|
||||
python3 scripts/e2e_webui_ask.py (CST_AGENT= picks the binary)
|
||||
|
||||
Checks: the turn really waits for the answer, a page opened while the
|
||||
question is open still receives it, and Last-Event-ID resumes the stream.
|
||||
"""
|
||||
import json, os, shutil, subprocess, sys, tempfile, threading, time, urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
REQS = []
|
||||
|
||||
|
||||
class H(BaseHTTPRequestHandler):
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
body = json.dumps({"data": [{"id": "deepseek-flash"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_POST(self):
|
||||
n = int(self.headers.get("Content-Length", 0))
|
||||
REQS.append(json.loads(self.rfile.read(n)))
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
base = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash"}
|
||||
|
||||
def ev(o):
|
||||
self.wfile.write(b"data: " + json.dumps({**base, **o}).encode() + b"\n\n")
|
||||
self.wfile.flush()
|
||||
if len(REQS) == 1:
|
||||
args = json.dumps({"question": "Pick a colour?", "options": "red\nblue"})
|
||||
ev({"choices": [{"index": 0, "delta": {"role": "assistant", "tool_calls": [
|
||||
{"index": 0, "id": "call_1", "type": "function",
|
||||
"function": {"name": "ask_user", "arguments": args}}]}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}]})
|
||||
else:
|
||||
ev({"choices": [{"index": 0, "delta": {"role": "assistant", "content": "done"}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]})
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
class Sse:
|
||||
def __init__(self, base, last_id=None):
|
||||
self.lines = []
|
||||
req = urllib.request.Request(base + "/api/events")
|
||||
if last_id is not None:
|
||||
req.add_header("Last-Event-ID", str(last_id))
|
||||
self.r = urllib.request.urlopen(req, timeout=60)
|
||||
threading.Thread(target=self.pump, daemon=True).start()
|
||||
|
||||
def pump(self):
|
||||
try:
|
||||
for raw in self.r:
|
||||
self.lines.append(raw.decode())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def events(self, name):
|
||||
out = []
|
||||
for i, l in enumerate(self.lines):
|
||||
if l.strip() == "event: " + name and i + 1 < len(self.lines):
|
||||
out.append(json.loads(self.lines[i + 1][6:]))
|
||||
return out
|
||||
|
||||
def ids(self):
|
||||
return [int(l[4:]) for l in self.lines if l.startswith("id: ")]
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.r.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def wait(fn, t=15):
|
||||
end = time.time() + t
|
||||
while time.time() < end:
|
||||
v = fn()
|
||||
if v:
|
||||
return v
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
|
||||
def post(base, path, obj):
|
||||
req = urllib.request.Request(base + path, data=json.dumps(obj).encode(),
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=5) as r:
|
||||
return r.status
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code
|
||||
|
||||
|
||||
api = ThreadingHTTPServer(("127.0.0.1", 0), H)
|
||||
threading.Thread(target=api.serve_forever, daemon=True).start()
|
||||
ws = tempfile.mkdtemp(prefix="cst-ask-")
|
||||
log = open(ws + "/server.log", "w+")
|
||||
p = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws, "--new",
|
||||
"--api-key", "sk-x", "--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto"],
|
||||
stdout=log, stderr=subprocess.STDOUT)
|
||||
try:
|
||||
def port():
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return l.split(":")[3].split("/")[0]
|
||||
pt = wait(port)
|
||||
base = "http://127.0.0.1:%s" % pt
|
||||
a = Sse(base)
|
||||
time.sleep(0.3)
|
||||
check(post(base, "/api/turn", {"text": "ask me"}) == 202, "turn accepted")
|
||||
ask = wait(lambda: a.events("ask_user"))
|
||||
check(bool(ask), "ask_user event raised")
|
||||
time.sleep(1.5)
|
||||
check(len(REQS) == 1, "the turn waits for the answer (requests so far: %d)" % len(REQS))
|
||||
# a page opened now must still see the open question
|
||||
b = Sse(base)
|
||||
check(bool(wait(lambda: b.events("ask_user"), 5)), "a late page gets the open question")
|
||||
# reconnect with Last-Event-ID: nothing before that id is resent
|
||||
last = a.ids()[-1]
|
||||
c = Sse(base, last_id=last - 1)
|
||||
got = wait(lambda: c.ids(), 5)
|
||||
check(bool(got) and got[0] == last, "Last-Event-ID resumes after that id (%s, want %d)" % (got[:1] if got else got, last))
|
||||
if ask:
|
||||
check(post(base, "/api/ask_user", {"id": ask[0]["id"], "text": "blue"}) == 200, "answer accepted")
|
||||
end = wait(lambda: a.events("turn_end"), 20)
|
||||
check(bool(end) and end[0].get("rc") == 0, "turn ended rc=0")
|
||||
tool_msgs = [m for m in (REQS[1]["messages"] if len(REQS) > 1 else []) if m.get("role") == "tool"]
|
||||
check(bool(tool_msgs) and "blue" in json.dumps(tool_msgs), "the answer reached the model: %s" % json.dumps(tool_msgs)[:200])
|
||||
for s in (a, b, c):
|
||||
s.close()
|
||||
finally:
|
||||
p.terminate()
|
||||
try:
|
||||
p.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
p.kill()
|
||||
check(False, "server hung on SIGTERM")
|
||||
check(p.returncode is not None and p.returncode < 128 and p.returncode >= 0, "clean exit (%s)" % p.returncode)
|
||||
log.close()
|
||||
shutil.rmtree(ws, ignore_errors=True)
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,227 @@
|
||||
"""WebUI /init, /skill, extensions and the thinking switch against a fake API.
|
||||
|
||||
python3 scripts/e2e_webui_ext.py (CST_AGENT= picks the binary)
|
||||
|
||||
Checks: show_thinking is validated, lands in the state and in settings.json;
|
||||
/api/skill inlines the skill and 404s an unknown one; /api/init sends the
|
||||
AGENTS.md prompt; /api/extensions lists skills and the built-in roles;
|
||||
trust and reload work and write settings.json; the changing calls are a 409
|
||||
while a turn runs, the listing is not. HOME points at a temp dir.
|
||||
"""
|
||||
import json, os, shutil, subprocess, sys, tempfile, threading, time, urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
SEEN = [] # the full last user message of every request
|
||||
|
||||
|
||||
class Api(BaseHTTPRequestHandler):
|
||||
"""Answers "好的"; a prompt with "slow" takes 2 s."""
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
body = json.dumps({"data": [{"id": "deepseek-flash"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_POST(self):
|
||||
req = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
|
||||
users = [m.get("content") for m in req.get("messages", []) if m.get("role") == "user"]
|
||||
last = users[-1] if users and isinstance(users[-1], str) else ""
|
||||
SEEN.append(last)
|
||||
if "slow" in last:
|
||||
time.sleep(2)
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
for o in ({"delta": {"role": "assistant", "content": "好的"}},
|
||||
{"delta": {}, "finish_reason": "stop"}):
|
||||
chunk = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash",
|
||||
"choices": [dict(index=0, **o)]}
|
||||
self.wfile.write(b"data: " + json.dumps(chunk, ensure_ascii=False).encode() + b"\n\n")
|
||||
self.wfile.flush()
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
def wait(fn, t=15):
|
||||
end = time.time() + t
|
||||
while time.time() < end:
|
||||
v = fn()
|
||||
if v:
|
||||
return v
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
|
||||
def call(base, path, obj=None, method=None):
|
||||
data = None if obj is None else json.dumps(obj).encode()
|
||||
req = urllib.request.Request(base + path, data=data, method=method or ("POST" if data else "GET"),
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, json.loads(r.read() or b"{}")
|
||||
except urllib.error.HTTPError as e:
|
||||
try:
|
||||
return e.code, json.loads(e.read() or b"{}")
|
||||
except ValueError:
|
||||
return e.code, {}
|
||||
|
||||
|
||||
def skill(ws, name, desc, body):
|
||||
d = os.path.join(ws, ".agent", "skills", name)
|
||||
os.makedirs(d, exist_ok=True)
|
||||
with open(os.path.join(d, "SKILL.md"), "w", encoding="utf-8") as f:
|
||||
f.write("---\nname: %s\ndescription: %s\n---\n\n%s\n" % (name, desc, body))
|
||||
|
||||
|
||||
api = ThreadingHTTPServer(("127.0.0.1", 0), Api)
|
||||
threading.Thread(target=api.serve_forever, daemon=True).start()
|
||||
ws = tempfile.mkdtemp(prefix="cst-ext-")
|
||||
home = tempfile.mkdtemp(prefix="cst-home-")
|
||||
skill(ws, "demo", "演示用的 skill", "DEMO-INSTRUCTIONS: 先说你好。")
|
||||
env = dict(os.environ, HOME=home, XDG_CONFIG_HOME=home + "/.config",
|
||||
XDG_DATA_HOME=home + "/.local/share", XDG_CACHE_HOME=home + "/.cache")
|
||||
for k in ("DEEPSEEK_API_KEY", "OPENAI_API_KEY", "CST_API_KEY"):
|
||||
env.pop(k, None)
|
||||
log = open(ws + "/server.log", "w+")
|
||||
proc = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws, "--new", "--api-key", "sk-x",
|
||||
"--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto"],
|
||||
stdout=log, stderr=subprocess.STDOUT, env=env)
|
||||
|
||||
|
||||
def port():
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return l.split(":")[3].split("/")[0]
|
||||
|
||||
|
||||
def settings():
|
||||
try:
|
||||
with open(home + "/.config/cst-agent/settings.json", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except OSError:
|
||||
return {}
|
||||
|
||||
|
||||
def busy(base):
|
||||
return call(base, "/api/state")[1].get("status_busy")
|
||||
|
||||
|
||||
def turn(base, path, body):
|
||||
code, d = call(base, path, body)
|
||||
if code == 202:
|
||||
time.sleep(0.3)
|
||||
wait(lambda: not busy(base), 20)
|
||||
return code, d
|
||||
|
||||
|
||||
try:
|
||||
base = "http://127.0.0.1:%s" % wait(port)
|
||||
|
||||
# 1. the thinking switch
|
||||
st = call(base, "/api/state")[1]
|
||||
check(st.get("show_thinking") is False, "show_thinking starts off")
|
||||
check(call(base, "/api/config", {"show_thinking": "yes"})[0] == 400, "show_thinking must be a bool")
|
||||
code, _ = call(base, "/api/config", {"show_thinking": True})
|
||||
check(code == 200 and call(base, "/api/state")[1].get("show_thinking") is True, "show_thinking on")
|
||||
check(settings().get("show_thinking") is True, "show_thinking is saved in settings.json")
|
||||
call(base, "/api/config", {"show_thinking": False})
|
||||
check(settings() and not settings().get("show_thinking"), "and cleared again (%s)" % settings())
|
||||
|
||||
# 2. the extensions listing
|
||||
code, ex = call(base, "/api/extensions")
|
||||
check(code == 200 and ex.get("enabled") is True and ex.get("trusted") is False,
|
||||
"extensions: enabled, not trusted (%s)" % code)
|
||||
names = [s["name"] for s in ex.get("skills", [])]
|
||||
check(names == ["demo"] and ex["skills"][0].get("desc") == "演示用的 skill"
|
||||
and ex["skills"][0].get("source") == "workspace", "the workspace skill is listed (%s)" % ex.get("skills"))
|
||||
roles = [r["name"] for r in ex.get("roles", [])]
|
||||
check("explore" in roles and all(r.get("source") for r in ex["roles"]), "built-in roles are listed (%s)" % roles)
|
||||
check(ex.get("mcp") == [] and ex.get("plugins") == [] and ex.get("runs") == [], "no MCP, plugins or runs")
|
||||
check(ex.get("task_tool") is True and "1 skill" in ex.get("report", ""), "task tool on, report %r" % ex.get("report"))
|
||||
|
||||
# 3. /skill
|
||||
code, d = turn(base, "/api/skill", {"name": "demo", "task": "做点事"})
|
||||
check(code == 202 and "DEMO-INSTRUCTIONS" in d.get("text", "") and d["text"].endswith("做点事"),
|
||||
"skill turn accepted (%s)" % code)
|
||||
check(SEEN and "DEMO-INSTRUCTIONS" in SEEN[-1] and "做点事" in SEEN[-1], "the model saw the skill and the task")
|
||||
code, d = turn(base, "/api/skill", {"name": "demo"})
|
||||
check(code == 202 and SEEN[-1].endswith("Apply this skill now."), "a skill without a task")
|
||||
check(call(base, "/api/skill", {"name": "nope"})[0] == 404, "unknown skill: 404")
|
||||
check(call(base, "/api/skill", {"name": "../demo"})[0] == 404, "skill ../demo: 404")
|
||||
check(call(base, "/api/skill", {})[0] == 400, "skill without a name: 400")
|
||||
check(call(base, "/api/skill", {"name": "x" * 100})[0] == 400, "a long skill name: 400")
|
||||
check(call(base, "/api/skill", {"name": "demo", "task": 5})[0] == 400, "a non-string task: 400")
|
||||
|
||||
# 4. /init
|
||||
code, d = turn(base, "/api/init", {})
|
||||
check(code == 202 and "AGENTS.md" in d.get("text", ""), "init accepted (%s)" % code)
|
||||
check("AGENTS.md" in SEEN[-1], "the model saw the AGENTS.md prompt")
|
||||
|
||||
# 5. trust and reload
|
||||
code, d = call(base, "/api/extensions/trust", {"on": True})
|
||||
check(code == 200 and d.get("trusted") is True, "trust the workspace (%s %s)" % (code, d))
|
||||
tw = settings().get("trusted_workspaces", [])
|
||||
check(any(os.path.realpath(ws) == os.path.realpath(p) for p in tw), "settings.json lists it (%s)" % tw)
|
||||
check(call(base, "/api/extensions")[1].get("trusted") is True, "the listing says trusted")
|
||||
code, d = call(base, "/api/extensions/trust", {"on": False})
|
||||
check(code == 200 and d.get("trusted") is False and not settings().get("trusted_workspaces"),
|
||||
"untrust (%s)" % settings().get("trusted_workspaces"))
|
||||
check(call(base, "/api/extensions/trust", {"on": "yes"})[0] == 400, "trust needs a bool")
|
||||
skill(ws, "zeta", "第二个", "ZETA")
|
||||
code, d = call(base, "/api/extensions/reload", {})
|
||||
check(code == 200 and "2 skills" in d.get("report", ""), "reload sees the new skill (%s)" % d)
|
||||
check([s["name"] for s in call(base, "/api/extensions")[1]["skills"]] == ["demo", "zeta"], "both listed")
|
||||
|
||||
# 6. while a turn runs
|
||||
call(base, "/api/turn", {"text": "slow 慢一点"})
|
||||
time.sleep(0.4)
|
||||
check(call(base, "/api/extensions/reload", {})[0] == 409, "reload during a turn: 409")
|
||||
check(call(base, "/api/extensions/trust", {"on": True})[0] == 409, "trust during a turn: 409")
|
||||
check(call(base, "/api/skill", {"name": "demo"})[0] == 409, "skill during a turn: 409")
|
||||
check(call(base, "/api/init", {})[0] == 409, "init during a turn: 409")
|
||||
check(call(base, "/api/extensions")[0] == 200, "the listing works during a turn")
|
||||
check(call(base, "/api/config", {"show_thinking": True})[0] == 200, "the thinking switch works during a turn")
|
||||
wait(lambda: not busy(base), 20)
|
||||
check(not settings().get("trusted_workspaces"), "the refused trust changed nothing")
|
||||
|
||||
# 7. another workspace brings its own skills
|
||||
ws2 = tempfile.mkdtemp(prefix="cst-ext2-")
|
||||
skill(ws2, "other", "另一个工作区的", "OTHER")
|
||||
code, _ = call(base, "/api/workspace", {"path": ws2})
|
||||
ex = call(base, "/api/extensions")[1]
|
||||
check(code == 200 and [s["name"] for s in ex.get("skills", [])] == ["other"]
|
||||
and ex.get("report") == "1 skill", "a workspace switch reloads the extensions (%s)" % ex.get("report"))
|
||||
code, d = turn(base, "/api/skill", {"name": "other"})
|
||||
check(code == 202 and "OTHER" in SEEN[-1], "and its skill runs")
|
||||
shutil.rmtree(ws2, ignore_errors=True)
|
||||
finally:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
check(False, "server hung on SIGTERM")
|
||||
log.close()
|
||||
shutil.rmtree(ws, ignore_errors=True)
|
||||
shutil.rmtree(home, ignore_errors=True)
|
||||
api.shutdown()
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,234 @@
|
||||
"""WebUI HTTP hardening checks over raw sockets, against a scripted model.
|
||||
|
||||
python3 scripts/e2e_webui_http.py (CST_AGENT= picks the binary)
|
||||
|
||||
Cross-site requests, request parsing (Content-Length, NUL, long lines,
|
||||
keep-alive), auth before the body, changes refused while a turn runs,
|
||||
cancel waking a pending question, token mode and shutdown.
|
||||
"""
|
||||
import json, os, re, shutil, socket, subprocess, sys, tempfile, threading, time
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
REQS = []
|
||||
|
||||
|
||||
class Api(BaseHTTPRequestHandler):
|
||||
"""First call: ask_user. Later calls: a plain answer."""
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
body = json.dumps({"data": [{"id": "deepseek-flash"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_POST(self):
|
||||
n = int(self.headers.get("Content-Length", 0))
|
||||
REQS.append(json.loads(self.rfile.read(n)))
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
base = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash"}
|
||||
|
||||
def ev(o):
|
||||
self.wfile.write(b"data: " + json.dumps({**base, **o}).encode() + b"\n\n")
|
||||
self.wfile.flush()
|
||||
if len(REQS) == 1:
|
||||
args = json.dumps({"question": "Pick?", "options": "a\nb"})
|
||||
ev({"choices": [{"index": 0, "delta": {"role": "assistant", "tool_calls": [
|
||||
{"index": 0, "id": "call_1", "type": "function",
|
||||
"function": {"name": "ask_user", "arguments": args}}]}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}]})
|
||||
else:
|
||||
ev({"choices": [{"index": 0, "delta": {"role": "assistant", "content": "ok"}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]})
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
def raw(port, data, timeout=5, read_all=True):
|
||||
"""Send bytes, return everything read until EOF/timeout."""
|
||||
s = socket.create_connection(("127.0.0.1", port), timeout=timeout)
|
||||
s.sendall(data)
|
||||
out = b""
|
||||
try:
|
||||
while True:
|
||||
b = s.recv(65536)
|
||||
if not b:
|
||||
break
|
||||
out += b
|
||||
if not read_all:
|
||||
break
|
||||
except socket.timeout:
|
||||
pass
|
||||
s.close()
|
||||
return out.decode("latin1")
|
||||
|
||||
|
||||
def status(resp):
|
||||
# a pipelined response starts right after the previous body, not on a new line
|
||||
return [int(m) for m in re.findall(r"HTTP/1\.1 (\d{3}) ", resp)]
|
||||
|
||||
|
||||
def req(method, path, headers=None, body=b"", host=None):
|
||||
h = {"Host": host or "127.0.0.1:%d" % PORT, "Connection": "close"}
|
||||
h.update(headers or {})
|
||||
if body or method == "POST":
|
||||
h["Content-Length"] = str(len(body))
|
||||
head = "%s %s HTTP/1.1\r\n" % (method, path) + "".join("%s: %s\r\n" % kv for kv in h.items()) + "\r\n"
|
||||
return head.encode() + body
|
||||
|
||||
|
||||
def start(ws, *extra):
|
||||
log = open(os.path.join(ws, "server.log"), "w+")
|
||||
p = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws, "--new",
|
||||
"--api-key", "sk-x", "--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto", *extra],
|
||||
stdout=log, stderr=subprocess.STDOUT)
|
||||
end = time.time() + 10
|
||||
while time.time() < end:
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return p, int(l.split(":")[3].split("/")[0]), log
|
||||
time.sleep(0.1)
|
||||
raise SystemExit("server did not start")
|
||||
|
||||
|
||||
def stop(p, name):
|
||||
p.terminate()
|
||||
try:
|
||||
p.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
p.kill()
|
||||
check(False, name + " hung on SIGTERM")
|
||||
return
|
||||
check(0 <= p.returncode < 128, "%s exits cleanly (%s)" % (name, p.returncode))
|
||||
|
||||
|
||||
api = ThreadingHTTPServer(("127.0.0.1", 0), Api)
|
||||
threading.Thread(target=api.serve_forever, daemon=True).start()
|
||||
root = tempfile.mkdtemp(prefix="cst-http-")
|
||||
try:
|
||||
ws = os.path.join(root, "a")
|
||||
os.makedirs(ws)
|
||||
p, PORT, log = start(ws)
|
||||
me = "http://127.0.0.1:%d" % PORT
|
||||
cfg = b'{"approval":"ask"}'
|
||||
# 1. cross-site requests
|
||||
r = raw(PORT, req("POST", "/api/config", {"Origin": "http://evil.example", "Content-Type": "text/plain"}, cfg))
|
||||
check(status(r)[:1] == [401], "a cross-site POST is refused (%s)" % status(r))
|
||||
r = raw(PORT, req("POST", "/api/config", {"Origin": "null"}, cfg))
|
||||
check(status(r)[:1] == [401], "Origin: null is refused")
|
||||
r = raw(PORT, req("POST", "/api/config", {"Origin": me, "Content-Type": "application/json"}, b'{"approval":"auto"}'))
|
||||
check(status(r)[:1] == [200], "our own page may POST (%s)" % status(r))
|
||||
r = raw(PORT, req("POST", "/api/config", {}, b'{"approval":"auto"}'))
|
||||
check(status(r)[:1] == [200], "a client without Origin (curl) may POST")
|
||||
r = raw(PORT, req("GET", "/api/attach/img-0000.png", host="evil.example"))
|
||||
check(status(r)[:1] == [401], "session images need the Host check too (%s)" % status(r))
|
||||
r = raw(PORT, req("GET", "/app.js"))
|
||||
check(status(r)[:1] == [200], "assets are served")
|
||||
# 2. request parsing
|
||||
r = raw(PORT, req("GET", "/api/state%00x"))
|
||||
check(status(r)[:1] == [400], "an encoded NUL in the path is a 400 (%s)" % status(r))
|
||||
smuggle = b"GET /api/sessions HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"
|
||||
r = raw(PORT, ("POST /api/cancel HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nContent-Length: abc\r\n\r\n" % PORT).encode() + smuggle)
|
||||
check(status(r) == [400], "Content-Length: abc is a 400, nothing smuggled (%s)" % status(r))
|
||||
r = raw(PORT, ("POST /api/cancel HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nContent-Length: 0\r\nContent-Length: %d\r\n\r\n"
|
||||
% (PORT, len(smuggle))).encode() + smuggle)
|
||||
check(status(r) == [400], "two different Content-Lengths are a 400 (%s)" % status(r))
|
||||
r = raw(PORT, ("GET /%s HTTP/1.1\r\nHost: x\r\n\r\n" % ("a" * 9000)).encode())
|
||||
check(status(r)[:1] == [400], "an over-long request line is a 400 (%s)" % status(r))
|
||||
r = raw(PORT, req("GET", "/api/state?x=http://a/b"))
|
||||
check(status(r)[:1] == [200], "'://' in the query leaves the path alone (%s)" % status(r))
|
||||
r = raw(PORT, req("GET", "/api/state", {"Connection": "Close"}), timeout=3)
|
||||
check("Connection: close" in r, "Connection: Close (any case) closes")
|
||||
ka = ("GET /api/state HTTP/1.1\r\nHost: 127.0.0.1:%d\r\n\r\n" % PORT).encode()
|
||||
big = ("POST /api/turn HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nContent-Length: 5000000\r\n\r\n" % PORT).encode()
|
||||
r = raw(PORT, ka + big)
|
||||
check(status(r) == [200, 413], "a 413 after a keep-alive request still arrives (%s)" % status(r))
|
||||
# 3. a pending question: changes are refused, approval may change, cancel wakes it
|
||||
events = []
|
||||
|
||||
def sse():
|
||||
s = socket.create_connection(("127.0.0.1", PORT))
|
||||
s.sendall(("GET /api/events HTTP/1.1\r\nHost: 127.0.0.1:%d\r\n\r\n" % PORT).encode())
|
||||
buf = b""
|
||||
s.settimeout(60)
|
||||
try:
|
||||
while True:
|
||||
b = s.recv(65536)
|
||||
if not b:
|
||||
break
|
||||
buf += b
|
||||
events[:] = [l[7:] for l in buf.decode("latin1").split("\n") if l.startswith("event: ")]
|
||||
except Exception:
|
||||
pass
|
||||
threading.Thread(target=sse, daemon=True).start()
|
||||
time.sleep(0.4)
|
||||
r = raw(PORT, req("POST", "/api/turn", {"Content-Type": "application/json"}, b'{"text":"go"}'))
|
||||
check(status(r)[:1] == [202], "turn accepted")
|
||||
end = time.time() + 15
|
||||
while time.time() < end and "ask_user" not in events:
|
||||
time.sleep(0.1)
|
||||
check("ask_user" in events, "the question is open")
|
||||
for path, body in (("/api/sessions/new", b""), ("/api/workspace", json.dumps({"path": root}).encode()),
|
||||
("/api/compact", b""), ("/api/undo", b""), ("/api/config", b'{"model":"x"}')):
|
||||
r = raw(PORT, req("POST", path, {"Content-Type": "application/json"}, body))
|
||||
check(status(r)[:1] == [409], "%s is refused mid-turn (%s)" % (path, status(r)))
|
||||
r = raw(PORT, req("POST", "/api/config", {"Content-Type": "application/json"}, b'{"approval":"yolo"}'))
|
||||
check(status(r)[:1] == [200], "approval may change mid-turn")
|
||||
r = raw(PORT, req("POST", "/api/cancel", {"Content-Type": "application/json"}, b"{}"))
|
||||
end = time.time() + 10
|
||||
while time.time() < end and "turn_end" not in events:
|
||||
time.sleep(0.1)
|
||||
check("turn_end" in events, "cancel ends a turn parked on a question")
|
||||
r = raw(PORT, req("POST", "/api/sessions/new", {"Content-Type": "application/json"}))
|
||||
check(status(r)[:1] == [200], "after the turn, a new session is fine (%s)" % status(r))
|
||||
stop(p, "the server")
|
||||
|
||||
# 4. token mode: the page loads, the API and images do not without it
|
||||
ws2 = os.path.join(root, "b")
|
||||
os.makedirs(ws2)
|
||||
p, PORT, log = start(ws2, "--web-token", "tok-x")
|
||||
r = raw(PORT, req("GET", "/app.js"))
|
||||
check(status(r)[:1] == [200], "token mode: app.js loads without the token (%s)" % status(r))
|
||||
r = raw(PORT, req("GET", "/api/state"))
|
||||
check(status(r)[:1] == [401], "token mode: the API needs it")
|
||||
r = raw(PORT, req("GET", "/api/attach/img-0000.png"))
|
||||
check(status(r)[:1] == [401], "token mode: images need it")
|
||||
r = raw(PORT, req("GET", "/api/state", {"Cookie": "cst_token=tok-x"}))
|
||||
check(status(r)[:1] == [200], "token mode: the cookie works")
|
||||
t0 = time.time()
|
||||
r = raw(PORT, ("POST /api/attach HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nContent-Length: 33554431\r\n"
|
||||
"Expect: 100-continue\r\n\r\n" % PORT).encode(), timeout=5)
|
||||
check(status(r) == [401] and time.time() - t0 < 3,
|
||||
"no token, no body read: 401 at once (%s, %.1fs)" % (status(r), time.time() - t0))
|
||||
stop(p, "the token server")
|
||||
|
||||
# 5. option checks
|
||||
rc = subprocess.run([BIN, "--web", "--web-port", "70000", "--workspace", ws2],
|
||||
capture_output=True, timeout=10).returncode
|
||||
check(rc != 0, "--web-port 70000 is refused")
|
||||
rc = subprocess.run([BIN, "--web", "--web-token", "x" * 80, "--web-port", "0", "--workspace", ws2,
|
||||
"--api-key", "k"], capture_output=True, timeout=10).returncode
|
||||
check(rc != 0, "an over-long --web-token is refused")
|
||||
finally:
|
||||
shutil.rmtree(root, ignore_errors=True)
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,327 @@
|
||||
"""WebUI session management against a fake OpenAI-compatible API.
|
||||
|
||||
python3 scripts/e2e_webui_sessions.py (CST_AGENT= picks the binary)
|
||||
|
||||
Checks: retry runs the last prompt again and edit-and-resend replaces it
|
||||
(the model sees the new text, the session keeps one copy); retry is a 409
|
||||
while a turn runs and when there is nothing to retry; export returns the
|
||||
transcript as Markdown; rename, clear and delete work on the store, and
|
||||
deleting the open session moves to a new one; history messages carry the
|
||||
note / tool_result flags. HOME points at a temp dir.
|
||||
"""
|
||||
import json, os, shutil, subprocess, sys, tempfile, threading, time, urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
SEEN = []
|
||||
|
||||
|
||||
def last_prompt(msgs):
|
||||
"""The prompts are one line; a note before one shares its user message."""
|
||||
for m in reversed(msgs):
|
||||
c = m.get("content")
|
||||
if m.get("role") == "user" and isinstance(c, str):
|
||||
return c.rstrip("\n").split("\n")[-1]
|
||||
return ""
|
||||
|
||||
|
||||
class Api(BaseHTTPRequestHandler):
|
||||
"""Answers "回答:<last prompt>"; a prompt with "slow" takes 2 s."""
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
body = json.dumps({"data": [{"id": "deepseek-flash"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_POST(self):
|
||||
req = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
|
||||
msgs = req.get("messages", [])
|
||||
p = last_prompt(msgs)
|
||||
tail = msgs[-1] if msgs else {}
|
||||
tc = tail.get("content") if isinstance(tail.get("content"), str) else ""
|
||||
answered = tail.get("role") == "tool" or tc.startswith("result ")
|
||||
if not answered:
|
||||
SEEN.append(p)
|
||||
if "slow" in p:
|
||||
time.sleep(2)
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
if answered:
|
||||
chunks = ({"delta": {"role": "assistant", "content": "读完了"}},
|
||||
{"delta": {}, "finish_reason": "stop"})
|
||||
elif p.startswith("native"):
|
||||
args = json.dumps({"path": "hello.txt"})
|
||||
chunks = ({"delta": {"role": "assistant", "tool_calls": [
|
||||
{"index": 0, "id": "c1", "type": "function",
|
||||
"function": {"name": "read_file", "arguments": args}}]}},
|
||||
{"delta": {}, "finish_reason": "tool_calls"})
|
||||
elif p.startswith("ctl"):
|
||||
chunks = ({"delta": {"role": "assistant",
|
||||
"content": "我来读。\ncall t1 read_file\narg path 9\nhello.txtend\n"}},
|
||||
{"delta": {}, "finish_reason": "stop"})
|
||||
else:
|
||||
chunks = ({"delta": {"role": "assistant", "content": "回答:" + p}},
|
||||
{"delta": {}, "finish_reason": "stop"})
|
||||
for o in chunks:
|
||||
chunk = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash",
|
||||
"choices": [dict(index=0, **o)]}
|
||||
self.wfile.write(b"data: " + json.dumps(chunk, ensure_ascii=False).encode() + b"\n\n")
|
||||
self.wfile.flush()
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
def wait(fn, t=15):
|
||||
end = time.time() + t
|
||||
while time.time() < end:
|
||||
v = fn()
|
||||
if v:
|
||||
return v
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
|
||||
def call(base, path, obj=None, method=None, raw_reply=False):
|
||||
data = None if obj is None else json.dumps(obj).encode()
|
||||
req = urllib.request.Request(base + path, data=data, method=method or ("POST" if data else "GET"),
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20) as r:
|
||||
raw = r.read()
|
||||
if raw_reply:
|
||||
return r.status, r.headers.get("Content-Type", ""), raw
|
||||
return r.status, json.loads(raw or b"{}")
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
if raw_reply:
|
||||
return e.code, e.headers.get("Content-Type", ""), raw
|
||||
try:
|
||||
return e.code, json.loads(raw or b"{}")
|
||||
except ValueError:
|
||||
return e.code, {}
|
||||
|
||||
|
||||
class Sse:
|
||||
def __init__(self, base):
|
||||
self.lines = []
|
||||
self.r = urllib.request.urlopen(base + "/api/events", timeout=60)
|
||||
threading.Thread(target=self.pump, daemon=True).start()
|
||||
|
||||
def pump(self):
|
||||
try:
|
||||
for raw in self.r:
|
||||
self.lines.append(raw)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def count(self, name):
|
||||
return sum(1 for l in self.lines if l.strip() == b"event: " + name.encode())
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.r.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
api = ThreadingHTTPServer(("127.0.0.1", 0), Api)
|
||||
threading.Thread(target=api.serve_forever, daemon=True).start()
|
||||
ws = tempfile.mkdtemp(prefix="cst-sess-")
|
||||
home = tempfile.mkdtemp(prefix="cst-home-")
|
||||
env = dict(os.environ, HOME=home, XDG_CONFIG_HOME=home + "/.config",
|
||||
XDG_DATA_HOME=home + "/.local/share", XDG_CACHE_HOME=home + "/.cache")
|
||||
for k in ("DEEPSEEK_API_KEY", "OPENAI_API_KEY", "CST_API_KEY"):
|
||||
env.pop(k, None)
|
||||
log = open(ws + "/server.log", "w+")
|
||||
proc = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws, "--new", "--api-key", "sk-x",
|
||||
"--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto"],
|
||||
stdout=log, stderr=subprocess.STDOUT, env=env)
|
||||
|
||||
|
||||
def port():
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return l.split(":")[3].split("/")[0]
|
||||
|
||||
|
||||
def messages(base):
|
||||
return call(base, "/api/session")[1].get("messages", [])
|
||||
|
||||
|
||||
def prompts(base):
|
||||
return [m["content"] for m in messages(base)
|
||||
if m["role"] == "user" and not m.get("note") and not m.get("tool_result")]
|
||||
|
||||
|
||||
def answers(base):
|
||||
return [m["content"] for m in messages(base) if m["role"] == "assistant"]
|
||||
|
||||
|
||||
def turn(base, sse, path, body, want=202):
|
||||
n = sse.count("turn_end")
|
||||
code, d = call(base, path, body)
|
||||
if code == want == 202:
|
||||
wait(lambda: sse.count("turn_end") > n, 20)
|
||||
return code, d
|
||||
|
||||
|
||||
try:
|
||||
base = "http://127.0.0.1:%s" % wait(port)
|
||||
sse = Sse(base)
|
||||
time.sleep(0.3)
|
||||
|
||||
# 1. retry with nothing to retry, then a first turn
|
||||
code, d = call(base, "/api/retry", {})
|
||||
check(code == 409 and "nothing to retry" in d.get("error", ""), "retry on an empty session: 409 (%s)" % code)
|
||||
turn(base, sse, "/api/turn", {"text": "第一句"})
|
||||
check(prompts(base) == ["第一句"] and answers(base) == ["回答:第一句"], "first turn stored")
|
||||
|
||||
# 2. retry: same prompt again, one copy in the session
|
||||
code, d = turn(base, sse, "/api/retry", {})
|
||||
check(code == 202 and d.get("text") == "第一句", "retry accepted (%s %s)" % (code, d))
|
||||
check(SEEN[-1] == "第一句", "the model saw the same prompt again")
|
||||
check(prompts(base) == ["第一句"] and answers(base) == ["回答:第一句"],
|
||||
"after retry: one prompt, one answer (%s)" % prompts(base))
|
||||
|
||||
# 3. edit and resend: the new text replaces the old
|
||||
code, d = turn(base, sse, "/api/retry", {"text": "改过的问题"})
|
||||
check(code == 202 and d.get("text") == "改过的问题", "edit-and-resend accepted")
|
||||
check(SEEN[-1] == "改过的问题", "the model saw the edited prompt")
|
||||
check(prompts(base) == ["改过的问题"] and answers(base) == ["回答:改过的问题"],
|
||||
"after edit: %s / %s" % (prompts(base), answers(base)))
|
||||
check(call(base, "/api/retry", {"text": ""})[0] == 400, "an empty edit is a 400")
|
||||
check(call(base, "/api/retry", {"text": 5})[0] == 400, "a non-string edit is a 400")
|
||||
|
||||
# 4. a second turn, then retry only touches the last one
|
||||
turn(base, sse, "/api/turn", {"text": "第二句"})
|
||||
turn(base, sse, "/api/retry", {})
|
||||
check(prompts(base) == ["改过的问题", "第二句"] and len(answers(base)) == 2,
|
||||
"retry keeps earlier turns: %s" % prompts(base))
|
||||
|
||||
# 5. retry while a turn runs is a 409
|
||||
n = sse.count("turn_end")
|
||||
call(base, "/api/turn", {"text": "slow 请慢一点"})
|
||||
code, d = call(base, "/api/retry", {})
|
||||
check(code == 409, "retry during a turn: 409 (%s)" % code)
|
||||
wait(lambda: sse.count("turn_end") > n, 20)
|
||||
|
||||
# 6. a note (plan mode changed mid-session) is flagged, and retry skips it
|
||||
call(base, "/api/config", {"plan_mode": True})
|
||||
turn(base, sse, "/api/turn", {"text": "计划一下"})
|
||||
ms = messages(base)
|
||||
notes = [m for m in ms if m.get("note")]
|
||||
check(len(notes) >= 1 and all(m["role"] == "user" for m in notes),
|
||||
"a mid-session note is flagged note:true (%d)" % len(notes))
|
||||
turn(base, sse, "/api/retry", {})
|
||||
check(SEEN[-1] == "计划一下" and prompts(base)[-1] == "计划一下", "retry after a note reruns the prompt")
|
||||
call(base, "/api/config", {"plan_mode": False})
|
||||
|
||||
# 7. export
|
||||
code, ctype, raw = call(base, "/api/export", raw_reply=True)
|
||||
md = raw.decode("utf-8", "replace")
|
||||
check(code == 200 and ctype.startswith("text/markdown"), "export: %s %s" % (code, ctype))
|
||||
check(md.startswith("# ") and "## User" in md and "改过的问题" in md and "## Assistant" in md
|
||||
and "回答:第二句" in md and "第一句" not in md, "export has the transcript")
|
||||
|
||||
# 8. rename and clear
|
||||
st = call(base, "/api/state")[1]
|
||||
cur = st.get("session")
|
||||
code, _ = call(base, "/api/sessions/rename", {"title": "我的会话"})
|
||||
check(code == 200, "rename the open session")
|
||||
ss = call(base, "/api/sessions")[1]
|
||||
check(any(s["id"] == cur and s["title"] == "我的会话" for s in ss.get("sessions", [])),
|
||||
"the list shows the new title")
|
||||
code, _, raw = call(base, "/api/export", raw_reply=True)
|
||||
check(raw.decode("utf-8", "replace").startswith("# 我的会话"), "export uses the title")
|
||||
long = "长" * 100
|
||||
call(base, "/api/sessions/rename", {"id": cur, "title": long})
|
||||
t = [s["title"] for s in call(base, "/api/sessions")[1]["sessions"] if s["id"] == cur][0]
|
||||
check(0 < len(t.encode()) <= 120 and t == "长" * len(t), "a long title is cut on a character (%d bytes)"
|
||||
% len(t.encode()))
|
||||
call(base, "/api/sessions/rename", {"title": ""})
|
||||
t = [s["title"] for s in call(base, "/api/sessions")[1]["sessions"] if s["id"] == cur][0]
|
||||
check(t == "改过的问题", "an empty title falls back to the first prompt (%r)" % t)
|
||||
check(call(base, "/api/sessions/rename", {"id": "nope", "title": "x"})[0] == 404, "rename unknown: 404")
|
||||
check(call(base, "/api/sessions/rename", {"id": "../x", "title": "x"})[0] == 404, "rename ../x: 404")
|
||||
check(call(base, "/api/sessions/rename", {})[0] == 400, "rename without a title: 400")
|
||||
|
||||
# 9. delete another session, then the open one
|
||||
code, d = call(base, "/api/sessions/new", {})
|
||||
second = d.get("id")
|
||||
check(code == 200 and second and second != cur, "a second session")
|
||||
code, d = call(base, "/api/sessions/delete", {"id": cur})
|
||||
check(code == 200 and d.get("current") == second, "delete a closed session (%s %s)" % (code, d))
|
||||
ids = [s["id"] for s in call(base, "/api/sessions")[1]["sessions"]]
|
||||
check(cur not in ids and not os.path.exists(os.path.join(ws, ".agent", "sessions", cur + ".jsonl")),
|
||||
"it is gone from the list and the disk")
|
||||
n = sse.count("session")
|
||||
code, d = call(base, "/api/sessions/delete", {"id": second})
|
||||
check(code == 200 and d.get("current") and d["current"] != second, "delete the open session (%s)" % d)
|
||||
check(call(base, "/api/state")[1].get("session") == d.get("current"), "the state moved to the new one")
|
||||
check(wait(lambda: sse.count("session") > n, 5), "a session event was sent")
|
||||
check(call(base, "/api/sessions/delete", {"id": "nope"})[0] == 404, "delete unknown: 404")
|
||||
check(call(base, "/api/sessions/delete", {})[0] == 400, "delete without an id: 400")
|
||||
|
||||
# 10. history: calls and results come back apart, for native and ctl
|
||||
with open(os.path.join(ws, "hello.txt"), "w", encoding="utf-8") as f:
|
||||
f.write("你好世界\n")
|
||||
turn(base, sse, "/api/turn", {"text": "native 读文件"})
|
||||
ms = messages(base)
|
||||
a = [m for m in ms if m["role"] == "assistant" and m.get("calls")]
|
||||
r = [m for m in ms if m.get("tool_result")]
|
||||
check(len(a) == 1 and a[0]["calls"][0].get("name") == "read_file"
|
||||
and a[0]["calls"][0].get("args", {}).get("path") == "hello.txt" and a[0]["calls"][0].get("id") == "c1",
|
||||
"native: the call is listed with its args (%s)" % (a[0].get("calls") if a else ms))
|
||||
check(len(r) == 1 and r[0]["role"] == "tool" and r[0].get("tool_call_id") == "c1"
|
||||
and "你好世界" in r[0].get("out", "") and not r[0]["out"].startswith("[tool ")
|
||||
and r[0].get("exit") == 0 and r[0].get("truncated") is False,
|
||||
"native: the result carries out/exit without the header (%s)" % r)
|
||||
check(answers(base)[-1] == "读完了", "native: the turn finished")
|
||||
|
||||
check(call(base, "/api/settings", {"protocol": "ctl"})[0] == 200, "switch to the ctl protocol")
|
||||
turn(base, sse, "/api/turn", {"text": "ctl 读文件"})
|
||||
ms = messages(base)
|
||||
a = [m for m in ms if m["role"] == "assistant" and m.get("calls") and m["calls"][0].get("id") == "t1"]
|
||||
r = [m for m in ms if m.get("tool_result") and m.get("tool_call_id") == "t1"]
|
||||
check(len(a) == 1 and a[0].get("text", "").strip() == "我来读。" and "call t1" in a[0]["content"]
|
||||
and a[0]["calls"][0].get("args", {}).get("path") == "hello.txt"
|
||||
and "call t1" not in a[0].get("html", ""),
|
||||
"ctl: prose and calls are split (%s)" % (a[0] if a else ms[-4:]))
|
||||
check(len(r) == 1 and r[0]["role"] == "user" and "你好世界" in r[0].get("out", "")
|
||||
and not r[0]["out"].startswith("result ") and not r[0]["out"].endswith("end")
|
||||
and r[0].get("exit") == 0,
|
||||
"ctl: the result is flagged with out/exit (%s)" % r)
|
||||
check(r and r[0]["content"] not in prompts(base), "ctl: the result is not a prompt")
|
||||
call(base, "/api/settings", {"protocol": "native-eos"})
|
||||
sse.close()
|
||||
finally:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
check(False, "server hung on SIGTERM")
|
||||
log.close()
|
||||
shutil.rmtree(ws, ignore_errors=True)
|
||||
shutil.rmtree(home, ignore_errors=True)
|
||||
api.shutdown()
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,313 @@
|
||||
"""WebUI settings and streaming against fake OpenAI-compatible APIs.
|
||||
|
||||
python3 scripts/e2e_webui_settings.py (CST_AGENT= picks the binary)
|
||||
|
||||
Checks: Chinese reasoning streams as whole characters in few events; the
|
||||
provider list, the model list with capabilities, config validation and
|
||||
persistence; a wrong key is rejected, a good one switches the provider
|
||||
(requests then go to the new base URL) and is saved; and all of it
|
||||
survives a restart. HOME and XDG_CONFIG_HOME point at a temp dir, so no
|
||||
real credentials or settings are read or written.
|
||||
"""
|
||||
import json, os, shutil, stat, subprocess, sys, tempfile, threading, time, urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
THINK = ["好的,我先看看工作区里", "有哪些文件,然后用中文", "回答这个问题。😀 结束"]
|
||||
ANSWER = "这是中文回答。"
|
||||
GOOD_KEYS = {"sk-good", "sk-good2"}
|
||||
B_REQS = []
|
||||
|
||||
|
||||
def sse_reply(h, model, think, answer):
|
||||
h.send_response(200)
|
||||
h.send_header("Content-Type", "text/event-stream")
|
||||
h.end_headers()
|
||||
base = {"id": "x", "object": "chat.completion.chunk", "model": model}
|
||||
|
||||
def ev(o):
|
||||
h.wfile.write(b"data: " + json.dumps({**base, **o}, ensure_ascii=False).encode() + b"\n\n")
|
||||
h.wfile.flush()
|
||||
for part in think:
|
||||
ev({"choices": [{"index": 0, "delta": {"role": "assistant", "reasoning_content": part}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {"content": answer}}]})
|
||||
ev({"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]})
|
||||
h.wfile.write(b"data: [DONE]\n\n")
|
||||
h.wfile.flush()
|
||||
|
||||
|
||||
def send_json(h, code, obj):
|
||||
body = json.dumps(obj).encode()
|
||||
h.send_response(code)
|
||||
h.send_header("Content-Type", "application/json")
|
||||
h.send_header("Content-Length", str(len(body)))
|
||||
h.end_headers()
|
||||
h.wfile.write(body)
|
||||
|
||||
|
||||
class A(BaseHTTPRequestHandler):
|
||||
"""The starting provider: any key, one model, Chinese reasoning."""
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
send_json(self, 200, {"data": [{"id": "deepseek-flash"}]})
|
||||
|
||||
def do_POST(self):
|
||||
self.rfile.read(int(self.headers.get("Content-Length", 0)))
|
||||
sse_reply(self, "deepseek-flash", THINK, ANSWER)
|
||||
|
||||
|
||||
class B(BaseHTTPRequestHandler):
|
||||
"""The provider switched to: /models checks the key."""
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def key_ok(self):
|
||||
auth = self.headers.get("Authorization", "")
|
||||
return auth.startswith("Bearer ") and auth[7:] in GOOD_KEYS
|
||||
|
||||
def do_GET(self):
|
||||
if not self.key_ok():
|
||||
return send_json(self, 401, {"error": {"message": "invalid api key"}})
|
||||
send_json(self, 200, {"data": [{"id": "fake-vis"}, {"id": "fake-text"}]})
|
||||
|
||||
def do_POST(self):
|
||||
body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}")
|
||||
B_REQS.append({"auth": self.headers.get("Authorization", ""), "model": body.get("model")})
|
||||
if not self.key_ok():
|
||||
return send_json(self, 401, {"error": {"message": "invalid api key"}})
|
||||
sse_reply(self, body.get("model") or "fake-vis", [], "B 的回答")
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
class Sse:
|
||||
def __init__(self, base):
|
||||
self.lines = []
|
||||
self.r = urllib.request.urlopen(base + "/api/events", timeout=60)
|
||||
threading.Thread(target=self.pump, daemon=True).start()
|
||||
|
||||
def pump(self):
|
||||
try:
|
||||
for raw in self.r:
|
||||
self.lines.append(raw)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def events(self, name):
|
||||
out = []
|
||||
for i, l in enumerate(self.lines):
|
||||
if l.strip() == b"event: " + name.encode() and i + 1 < len(self.lines):
|
||||
out.append(self.lines[i + 1][6:])
|
||||
return out
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.r.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def wait(fn, t=15):
|
||||
end = time.time() + t
|
||||
while time.time() < end:
|
||||
v = fn()
|
||||
if v:
|
||||
return v
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
|
||||
def call(base, path, obj=None, method=None):
|
||||
data = None if obj is None else json.dumps(obj).encode()
|
||||
req = urllib.request.Request(base + path, data=data, method=method or ("POST" if data else "GET"),
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20) as r:
|
||||
raw = r.read()
|
||||
return r.status, json.loads(raw or b"{}"), raw
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
try:
|
||||
return e.code, json.loads(raw or b"{}"), raw
|
||||
except ValueError:
|
||||
return e.code, {}, raw
|
||||
|
||||
|
||||
def serve(handler):
|
||||
s = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
threading.Thread(target=s.serve_forever, daemon=True).start()
|
||||
return s
|
||||
|
||||
|
||||
api_a, api_b = serve(A), serve(B)
|
||||
ws = tempfile.mkdtemp(prefix="cst-set-")
|
||||
home = tempfile.mkdtemp(prefix="cst-home-")
|
||||
cfgdir = home + "/.config/cst-agent"
|
||||
os.makedirs(cfgdir)
|
||||
with open(cfgdir + "/models.json", "w") as f:
|
||||
json.dump({"providers": {"fakeb": {"api_base": "http://127.0.0.1:%d/v1" % api_b.server_address[1],
|
||||
"key_env": "FAKEB_API_KEY", "default_model": ""}},
|
||||
"models": [{"match": "fake-vis", "vision": True, "context": 200000},
|
||||
{"match": "fake-text", "vision": False}]}, f)
|
||||
env = dict(os.environ, HOME=home, XDG_CONFIG_HOME=home + "/.config",
|
||||
XDG_DATA_HOME=home + "/.local/share", XDG_CACHE_HOME=home + "/.cache")
|
||||
for k in ("DEEPSEEK_API_KEY", "FAKEB_API_KEY", "OPENAI_API_KEY", "CST_API_KEY"):
|
||||
env.pop(k, None)
|
||||
procs = []
|
||||
|
||||
|
||||
def start(args, tag):
|
||||
log = open("%s/%s.log" % (ws, tag), "w+")
|
||||
p = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws] + args,
|
||||
stdout=log, stderr=subprocess.STDOUT, env=env)
|
||||
procs.append((p, log))
|
||||
|
||||
def port():
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return l.split(":")[3].split("/")[0]
|
||||
pt = wait(port)
|
||||
return p, "http://127.0.0.1:%s" % pt
|
||||
|
||||
|
||||
def stop(p):
|
||||
p.terminate()
|
||||
try:
|
||||
p.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
p.kill()
|
||||
check(False, "server hung on SIGTERM")
|
||||
check(p.returncode is not None and 0 <= p.returncode < 128, "clean exit (%s)" % p.returncode)
|
||||
|
||||
|
||||
def leaked(raw):
|
||||
return any(k.encode() in raw for k in GOOD_KEYS | {"sk-bad", "sk-x"})
|
||||
|
||||
|
||||
try:
|
||||
p, base = start(["--new", "--api-key", "sk-x", "--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api_a.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto"], "first")
|
||||
a = Sse(base)
|
||||
time.sleep(0.3)
|
||||
|
||||
# 1. Chinese reasoning: whole characters, a handful of events
|
||||
code, _, _ = call(base, "/api/turn", {"text": "用中文想一想"})
|
||||
check(code == 202, "turn accepted")
|
||||
end = wait(lambda: a.events("turn_end"), 20)
|
||||
check(bool(end) and json.loads(end[0]).get("rc") == 0, "turn ended rc=0")
|
||||
raw = a.events("think_delta")
|
||||
ok = True
|
||||
for r in raw:
|
||||
try:
|
||||
r.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
ok = False
|
||||
check(ok, "every think_delta frame is valid UTF-8")
|
||||
text = "".join(json.loads(r.decode("utf-8", "replace"))["text"] for r in raw)
|
||||
want = "".join(THINK)
|
||||
check(text == want and "\ufffd" not in text, "the thinking text round trips: %r" % text[:60])
|
||||
check(0 < len(raw) <= 2 * len(THINK) + 2,
|
||||
"think_delta events: %d for %d bytes" % (len(raw), len(want.encode())))
|
||||
ans = "".join(json.loads(r)["text"] for r in a.events("assistant_delta"))
|
||||
check(ans == ANSWER, "the answer round trips: %r" % ans)
|
||||
|
||||
# 2. providers and models
|
||||
code, d, rawp = call(base, "/api/providers")
|
||||
names = {x["name"]: x for x in d.get("providers", [])}
|
||||
check(code == 200 and "deepseek" in names and "fakeb" in names, "provider list: %s" % sorted(names))
|
||||
check(names.get("deepseek", {}).get("current") and names["deepseek"].get("has_key")
|
||||
and names["deepseek"].get("key_from") == "session", "deepseek: current, key for this run")
|
||||
check(names.get("fakeb", {}).get("has_key") is False and names["fakeb"].get("source") == "user",
|
||||
"fakeb: no key yet, from models.json")
|
||||
check(not leaked(rawp), "no key in /api/providers")
|
||||
code, d, _ = call(base, "/api/models")
|
||||
ids = [m["id"] for m in d.get("models", [])]
|
||||
first = (d.get("models") or [{}])[0]
|
||||
check(code == 200 and "deepseek-flash" in ids and first.get("api") is True
|
||||
and "context" in first and "label" in first, "model list: %s" % ids[:5])
|
||||
|
||||
# 3. config: validation and persistence
|
||||
check(call(base, "/api/config", {"effort": "bogus"})[0] == 400, "a bad effort is a 400")
|
||||
check(call(base, "/api/config", {"provider": "fakeb"})[0] == 400, "provider via /api/config is a 400")
|
||||
check(call(base, "/api/config", {"model": ""})[0] == 400, "an empty model is a 400")
|
||||
code, d, _ = call(base, "/api/config", {"model": "deepseek-v4-pro", "effort": "high"})
|
||||
check(code == 200 and d.get("ok"), "model + effort accepted")
|
||||
code, s, _ = call(base, "/api/state")
|
||||
check(s.get("model") == "deepseek-v4-pro" and s.get("effort") == "high", "the state has them")
|
||||
check(s.get("effort_applies") is True and s.get("key_set") is True and "model_info" in s
|
||||
and s.get("perm_timeout", 0) > 0 and "turn_usage" in s, "the state has the new fields")
|
||||
st = open(cfgdir + "/settings.json").read() if os.path.exists(cfgdir + "/settings.json") else ""
|
||||
check("deepseek-v4-pro" in st and "high" in st, "settings.json remembers model and effort")
|
||||
|
||||
# 4. switching provider: no key, a wrong key, a good key
|
||||
code, d, _ = call(base, "/api/provider", {"name": "fakeb"})
|
||||
check(code == 400 and d.get("need_key"), "fakeb without a key: need_key (%s)" % code)
|
||||
code, d, rawp = call(base, "/api/provider", {"name": "fakeb", "key": "sk-bad"})
|
||||
check(code == 401 and "拒绝" in d.get("error", ""), "a wrong key is rejected (%s %s)" % (code, d.get("error")))
|
||||
check(not leaked(rawp), "no key in the rejection")
|
||||
check(call(base, "/api/state")[1].get("provider") == "deepseek", "still on deepseek")
|
||||
check(call(base, "/api/provider", {"name": "nope"})[0] == 404, "an unknown provider is a 404")
|
||||
code, d, rawp = call(base, "/api/provider", {"name": "fakeb", "key": "sk-good"})
|
||||
check(code == 200 and d.get("provider") == "fakeb" and d.get("model") == "fake-vis",
|
||||
"switched to fakeb, first listed model picked (%s %s)" % (code, d))
|
||||
check(not leaked(rawp), "no key in the switch reply")
|
||||
creds = cfgdir + "/credentials"
|
||||
cr = open(creds).read() if os.path.exists(creds) else ""
|
||||
check("fakeb.api_key=sk-good" in cr, "the key was saved to credentials")
|
||||
if os.name == "posix" and os.path.exists(creds):
|
||||
check(stat.S_IMODE(os.stat(creds).st_mode) == 0o600, "credentials is 0600")
|
||||
code, d, _ = call(base, "/api/models")
|
||||
ms = {m["id"]: m for m in d.get("models", [])}
|
||||
check("fake-vis" in ms and ms["fake-vis"].get("vision") is True and ms["fake-vis"].get("context") == 200000
|
||||
and ms.get("fake-text", {}).get("vision") is False, "fakeb models with capabilities")
|
||||
code, d, _ = call(base, "/api/config", {"effort": "low"})
|
||||
check(code == 200 and d.get("warning"), "effort on a model without it: saved with a warning")
|
||||
|
||||
# 5. a turn now goes to the new base URL with the new key
|
||||
n0 = len(B_REQS)
|
||||
code, _, _ = call(base, "/api/turn", {"text": "hi B"})
|
||||
check(code == 202, "turn on fakeb accepted")
|
||||
wait(lambda: len(a.events("turn_end")) >= 2, 20)
|
||||
check(len(B_REQS) > n0 and B_REQS[-1]["auth"] == "Bearer sk-good" and B_REQS[-1]["model"] == "fake-vis",
|
||||
"the request reached fakeb: %s" % (B_REQS[-1:] or "none"))
|
||||
|
||||
# 6. replacing the key
|
||||
code, d, _ = call(base, "/api/key", {"key": "sk-bad"})
|
||||
check(code == 401, "a wrong replacement key is rejected (%s)" % code)
|
||||
code, d, _ = call(base, "/api/key", {"key": "sk-good2"})
|
||||
check(code == 200 and d.get("ok"), "the replacement key is accepted")
|
||||
check("fakeb.api_key=sk-good2" in open(creds).read(), "the replacement key was saved")
|
||||
check(call(base, "/api/key", {})[0] == 400, "an empty key body is a 400")
|
||||
a.close()
|
||||
stop(p)
|
||||
|
||||
# 7. the next start remembers provider, model and key
|
||||
p, base = start(["--new"], "second")
|
||||
code, s, raws = call(base, "/api/state")
|
||||
check(s.get("provider") == "fakeb" and s.get("model") == "fake-vis" and s.get("key_set") is True
|
||||
and s.get("key_from") == "saved", "restart: %s/%s key %s" % (s.get("provider"), s.get("model"), s.get("key_from")))
|
||||
check(not leaked(raws), "no key in /api/state")
|
||||
stop(p)
|
||||
|
||||
logs = b"".join(open(lg.name, "rb").read() for _, lg in procs)
|
||||
check(not leaked(logs.replace(b"sk-x", b"")) , "no key in the server logs")
|
||||
finally:
|
||||
for p, log in procs:
|
||||
if p.poll() is None:
|
||||
p.kill()
|
||||
log.close()
|
||||
shutil.rmtree(ws, ignore_errors=True)
|
||||
shutil.rmtree(home, ignore_errors=True)
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,206 @@
|
||||
"""WebUI workspace views: git diff, run settings, saved permission rules.
|
||||
|
||||
python3 scripts/e2e_webui_workspace.py (CST_AGENT= picks the binary)
|
||||
|
||||
Checks: /api/diff lists git status and the working-tree diff (per file too),
|
||||
an option-looking path is only ever a path, and a directory without git
|
||||
says so; /api/settings reports protocol, tools, confirmations and the
|
||||
sandbox, rejects a bad body without applying any of it, applies a good one,
|
||||
and is a 409 while a turn runs; /api/permissions shows the saved rules and
|
||||
DELETE clears them. HOME points at a temp dir.
|
||||
"""
|
||||
import json, os, shutil, subprocess, sys, tempfile, threading, time, urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
BIN = os.environ.get("CST_AGENT", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "cst-agent"))
|
||||
|
||||
|
||||
class Api(BaseHTTPRequestHandler):
|
||||
"""A slow plain answer, so a turn is running for a while."""
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_POST(self):
|
||||
self.rfile.read(int(self.headers.get("Content-Length", 0)))
|
||||
time.sleep(2)
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
for o in ({"delta": {"role": "assistant", "content": "好"}}, {"delta": {}, "finish_reason": "stop"}):
|
||||
chunk = {"id": "x", "object": "chat.completion.chunk", "model": "m", "choices": [dict(index=0, **o)]}
|
||||
self.wfile.write(b"data: " + json.dumps(chunk, ensure_ascii=False).encode() + b"\n\n")
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def check(ok, msg):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + msg)
|
||||
fails += 0 if ok else 1
|
||||
|
||||
|
||||
def wait(fn, t=15):
|
||||
end = time.time() + t
|
||||
while time.time() < end:
|
||||
v = fn()
|
||||
if v:
|
||||
return v
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
|
||||
def call(base, path, obj=None, method=None):
|
||||
data = None if obj is None else json.dumps(obj).encode()
|
||||
req = urllib.request.Request(base + path, data=data, method=method or ("POST" if data else "GET"),
|
||||
headers={"Content-Type": "application/json"})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
return r.status, json.loads(r.read() or b"{}")
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
try:
|
||||
return e.code, json.loads(raw or b"{}")
|
||||
except ValueError:
|
||||
return e.code, {}
|
||||
|
||||
|
||||
def git(*args):
|
||||
return subprocess.run(["git", "-C", ws] + list(args), capture_output=True, text=True)
|
||||
|
||||
|
||||
api = ThreadingHTTPServer(("127.0.0.1", 0), Api)
|
||||
threading.Thread(target=api.serve_forever, daemon=True).start()
|
||||
ws = tempfile.mkdtemp(prefix="cst-ws-")
|
||||
plain = tempfile.mkdtemp(prefix="cst-nogit-")
|
||||
home = tempfile.mkdtemp(prefix="cst-home-")
|
||||
env = dict(os.environ, HOME=home, XDG_CONFIG_HOME=home + "/.config",
|
||||
XDG_DATA_HOME=home + "/.local/share", XDG_CACHE_HOME=home + "/.cache",
|
||||
GIT_CONFIG_NOSYSTEM="1")
|
||||
for k in ("DEEPSEEK_API_KEY", "OPENAI_API_KEY", "CST_API_KEY"):
|
||||
env.pop(k, None)
|
||||
have_git = shutil.which("git") is not None
|
||||
if have_git:
|
||||
git("init", "-q")
|
||||
git("config", "user.email", "e2e@example.com")
|
||||
git("config", "user.name", "e2e")
|
||||
open(ws + "/a.txt", "w").write("one\n")
|
||||
git("add", "a.txt")
|
||||
git("commit", "-q", "-m", "first")
|
||||
open(ws + "/a.txt", "w").write("one\n第二行\n")
|
||||
open(ws + "/new.txt", "w").write("new\n")
|
||||
os.makedirs(ws + "/.agent", exist_ok=True)
|
||||
json.dump({"tools": ["write_file"], "shell_prefixes": ["make"]}, open(ws + "/.agent/permissions.json", "w"))
|
||||
|
||||
log = open(home + "/server.log", "w+")
|
||||
proc = subprocess.Popen([BIN, "--web", "--web-port", "0", "--workspace", ws, "--new", "--api-key", "sk-x",
|
||||
"--provider", "deepseek", "--model", "deepseek-flash",
|
||||
"--api-base", "http://127.0.0.1:%d/v1" % api.server_address[1],
|
||||
"--protocol", "native-eos", "--approval", "auto"],
|
||||
stdout=log, stderr=subprocess.STDOUT, env=env)
|
||||
|
||||
|
||||
def port():
|
||||
log.seek(0)
|
||||
for l in log.read().splitlines():
|
||||
if l.startswith("webui: http://127.0.0.1:"):
|
||||
return l.split(":")[3].split("/")[0]
|
||||
|
||||
|
||||
try:
|
||||
base = "http://127.0.0.1:%s" % wait(port)
|
||||
|
||||
# 1. diff
|
||||
if have_git:
|
||||
code, d = call(base, "/api/diff")
|
||||
files = {f["path"]: f["status"] for f in d.get("files", [])}
|
||||
check(code == 200 and d.get("git") is True, "diff: git repo seen")
|
||||
check(files.get("a.txt") == " M" and files.get("new.txt") == "??", "status lists both files: %s" % files)
|
||||
check("+第二行" in d.get("diff", "") and "a.txt" in d.get("diff", ""), "the diff has the change")
|
||||
code, d = call(base, "/api/diff?path=a.txt")
|
||||
check(code == 200 and "+第二行" in d.get("diff", "") and d.get("path") == "a.txt", "diff of one file")
|
||||
code, d = call(base, "/api/diff?path=new.txt")
|
||||
check(code == 200 and d.get("diff") == "", "an untracked file has no diff")
|
||||
evil = ws + "/pwned"
|
||||
code, d = call(base, "/api/diff?path=" + urllib.request.quote("--output=" + evil))
|
||||
check(code == 200 and not os.path.exists(evil), "an option-looking path stays a path")
|
||||
else:
|
||||
print("SKIP diff checks: no git")
|
||||
|
||||
# 2. settings: what is there
|
||||
code, s = call(base, "/api/settings")
|
||||
names = [t["name"] for t in s.get("tools", [])]
|
||||
check(code == 200 and s.get("protocol") == "native-eos" and len(s.get("protocols", [])) == 5,
|
||||
"settings: protocol %s of %s" % (s.get("protocol"), s.get("protocols")))
|
||||
check("read_file" in names and all("perm" in t and "on" in t for t in s["tools"]), "tool list: %d tools" % len(names))
|
||||
check(set(s.get("confirm", {})) == {"shell", "write", "net"}, "confirm flags present")
|
||||
check(isinstance(s.get("sandbox", {}).get("kind"), str), "sandbox kind: %s" % s.get("sandbox"))
|
||||
victim = next((t["name"] for t in s["tools"] if t["perm"] == "net"), names[-1])
|
||||
|
||||
# 3. a bad body changes nothing
|
||||
for body, why in (({"protocol": "nope"}, "unknown protocol"), ({"max_steps": 0}, "max_steps 0"),
|
||||
({"max_steps": "5"}, "max_steps as text"), ({"tool_profile": "huge"}, "unknown profile"),
|
||||
({"tools": {"nope": False}}, "unknown tool"), ({"tools": {victim: "off"}}, "tool value not bool"),
|
||||
({"confirm": {"disk": True}}, "unknown confirm key"),
|
||||
({"max_steps": 9, "protocol": "nope"}, "mixed good and bad")):
|
||||
check(call(base, "/api/settings", body)[0] == 400, "400 for %s" % why)
|
||||
check(call(base, "/api/settings")[1].get("max_steps") == s.get("max_steps"), "nothing applied from a bad body")
|
||||
|
||||
# 4. a good body applies
|
||||
code, d = call(base, "/api/settings", {"protocol": "ctl", "max_steps": 7, "tool_profile": "minimal",
|
||||
"tools": {victim: False}, "confirm": {"shell": True, "net": False}})
|
||||
check(code == 200 and d.get("ok"), "settings accepted (%s %s)" % (code, d))
|
||||
code, s2 = call(base, "/api/settings")
|
||||
t2 = {t["name"]: t for t in s2.get("tools", [])}
|
||||
check(s2.get("protocol") == "ctl" and s2.get("max_steps") == 7 and s2.get("tool_profile") == "minimal",
|
||||
"protocol/steps/profile applied")
|
||||
check(t2.get(victim, {}).get("off") is True and t2[victim]["on"] is False, "%s switched off" % victim)
|
||||
check(s2["confirm"]["shell"] is True and s2["confirm"]["net"] is False, "confirm flags applied")
|
||||
st = call(base, "/api/state")[1]
|
||||
check(st.get("protocol_tools") == "ctl" and st.get("max_steps") == 7 and st.get("tool_profile") == "minimal",
|
||||
"the state shows them")
|
||||
call(base, "/api/settings", {"protocol": "native-eos", "tool_profile": "default", "tools": {victim: True}})
|
||||
t3 = {t["name"]: t for t in call(base, "/api/settings")[1]["tools"]}
|
||||
check(t3[victim]["off"] is False and t3[victim]["on"] is True, "%s back on" % victim)
|
||||
|
||||
# 5. permissions
|
||||
code, p = call(base, "/api/permissions")
|
||||
check(code == 200 and p.get("tools") == ["write_file"] and p.get("shell_prefixes") == ["make"]
|
||||
and "write_file" in p.get("rules", ""), "saved rules: %s %s" % (p.get("tools"), p.get("shell_prefixes")))
|
||||
code, _ = call(base, "/api/permissions", method="DELETE")
|
||||
check(code == 200, "DELETE /api/permissions")
|
||||
p = call(base, "/api/permissions")[1]
|
||||
check(p.get("tools") == [] and p.get("shell_prefixes") == [], "the rules are gone")
|
||||
saved = json.load(open(ws + "/.agent/permissions.json"))
|
||||
check(saved.get("tools") == [] and saved.get("shell_prefixes") == [], "and gone from the file")
|
||||
|
||||
# 6. while a turn runs: settings and the rule reset are 409, reads still work
|
||||
code, _ = call(base, "/api/turn", {"text": "慢慢来"})
|
||||
check(code == 202, "a slow turn started")
|
||||
check(call(base, "/api/settings", {"max_steps": 3})[0] == 409, "settings during a turn: 409")
|
||||
check(call(base, "/api/permissions", method="DELETE")[0] == 409, "rule reset during a turn: 409")
|
||||
check(call(base, "/api/settings")[0] == 200 and call(base, "/api/permissions")[0] == 200,
|
||||
"reads during a turn: 200")
|
||||
if have_git:
|
||||
check(call(base, "/api/diff")[0] == 200, "diff during a turn: 200")
|
||||
wait(lambda: not call(base, "/api/state")[1].get("status_busy"), 20)
|
||||
|
||||
# 7. a directory without git
|
||||
if have_git:
|
||||
code, _ = call(base, "/api/workspace", {"path": plain})
|
||||
code, d = call(base, "/api/diff")
|
||||
check(code == 200 and d.get("git") is False and "diff" not in d, "no git repo: git=false")
|
||||
finally:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(20)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
check(False, "server hung on SIGTERM")
|
||||
log.close()
|
||||
for d in (ws, plain, home):
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
api.shutdown()
|
||||
print("FAILED:", fails)
|
||||
sys.exit(1 if fails else 0)
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
# Embed web/ into .gen/web_assets.inc as C byte arrays (the same trick the
|
||||
# Makefile uses for src/models_builtin.json). POSIX sh on purpose: the musl
|
||||
# and busybox build environments have no bash. Usage:
|
||||
# sh scripts/embed_web.sh [out.inc] [web-dir]
|
||||
set -eu
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
OUT="${1:-$ROOT/.gen/web_assets.inc}"
|
||||
DIR="${2:-$ROOT/web}"
|
||||
|
||||
FILES="index.html app.js style.css favicon.svg"
|
||||
|
||||
mkdir -p "$(dirname "$OUT")"
|
||||
{
|
||||
echo "/* Generated by scripts/embed_web.sh from web/ - do not edit. */"
|
||||
for f in $FILES; do
|
||||
[ -f "$DIR/$f" ] || continue
|
||||
var="wui_asset_$(echo "$f" | tr -c 'a-zA-Z0-9' '_')"
|
||||
echo "static const unsigned char ${var}[] = {"
|
||||
od -An -v -tx1 "$DIR/$f" | sed 's/\([0-9a-f][0-9a-f]\)/0x\1,/g'
|
||||
echo "0x00};"
|
||||
done
|
||||
echo "static const wui_asset_t wui_assets[] = {"
|
||||
for f in $FILES; do
|
||||
[ -f "$DIR/$f" ] || continue
|
||||
var="wui_asset_$(echo "$f" | tr -c 'a-zA-Z0-9' '_')"
|
||||
case "$f" in
|
||||
*.html) ctype="text/html; charset=utf-8" ;;
|
||||
*.js) ctype="text/javascript; charset=utf-8" ;;
|
||||
*.css) ctype="text/css; charset=utf-8" ;;
|
||||
*.svg) ctype="image/svg+xml" ;;
|
||||
*) ctype="application/octet-stream" ;;
|
||||
esac
|
||||
echo " {\"$f\", \"$ctype\", ${var}, sizeof(${var}) - 1},"
|
||||
done
|
||||
echo "};"
|
||||
echo "static const int wui_asset_count = (int)(sizeof(wui_assets) / sizeof(wui_assets[0]));"
|
||||
} > "$OUT.tmp"
|
||||
mv "$OUT.tmp" "$OUT"
|
||||
echo "embedded $(wc -c < "$OUT") bytes of C into $OUT"
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env bash
|
||||
# Static musl build of cst-agent for one architecture, packed as
|
||||
# dist/cst-agent-VERSION-linux-ARCH.tar.gz (+ .sha256).
|
||||
# scripts/package_linux.sh x86_64|x86|aarch64
|
||||
# CST_LINUX_CHECK=1 also runs `make check` (only where the binaries can run).
|
||||
# Builds in the source tree, so the tree is cleaned first.
|
||||
set -euo pipefail
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
ARCH="${1:?usage: $0 x86_64|x86|aarch64}"
|
||||
PREFIX="$ROOT/third_party/prefix-linux-$ARCH"
|
||||
cd "$ROOT"
|
||||
|
||||
read -r LINUX_HOST bindir < <(bash scripts/ci_linux_toolchain.sh "$ARCH")
|
||||
export PATH="$bindir:$PATH" LINUX_HOST
|
||||
T="$LINUX_HOST-"
|
||||
|
||||
if [[ ! -f "$PREFIX/lib/libcurl.a" || ! -f "$PREFIX/lib/libncursesw.a" ]]; then
|
||||
bash scripts/build_third_party_linux.sh "$ARCH"
|
||||
fi
|
||||
|
||||
# Only the static prefix: no host .pc file may leak into the link.
|
||||
export PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig"
|
||||
unset PKG_CONFIG_PATH CC CFLAGS LDFLAGS
|
||||
mk=(make CC="${T}gcc" AR="${T}ar" STATIC=1
|
||||
CFLAGS="-std=c11 -Wall -Wextra -O2 -g -Werror")
|
||||
|
||||
make clean >/dev/null
|
||||
make -C vendor/sashidown clean >/dev/null 2>&1 || true
|
||||
"${mk[@]}" -j"$(nproc)" cst-agent
|
||||
if "${T}readelf" -l cst-agent | grep -q 'Requesting program interpreter'; then
|
||||
echo "cst-agent is dynamically linked" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${CST_LINUX_CHECK:-0}" == 1 ]]; then
|
||||
"${mk[@]}" check
|
||||
fi
|
||||
|
||||
ver="$(sed -n 's/^#define CST_VERSION_\(MAJOR\|MINOR\|PATCH\) \([0-9]*\)$/\2/p' include/cst_version.h | paste -sd.)"
|
||||
name="cst-agent-$ver-linux-$ARCH"
|
||||
rel="dist/$name"
|
||||
rm -rf "$rel"
|
||||
mkdir -p "$rel"
|
||||
cp -f cst-agent "$rel/cst-agent"
|
||||
"${T}strip" "$rel/cst-agent"
|
||||
cp -f README.md "$rel/README.md"
|
||||
echo "$ver" >"$rel/VERSION"
|
||||
tgz="dist/$name.tar.gz"
|
||||
tar -C dist -czf "$tgz" "$name"
|
||||
(cd dist && sha256sum "$name.tar.gz" >"$name.tar.gz.sha256")
|
||||
ls -la "$rel"
|
||||
echo "$ROOT/$tgz"
|
||||
@@ -36,3 +36,18 @@ rm -f "$zip"
|
||||
(cd "$ROOT/dist" && sha256sum "$(basename "$zip")" >"$(basename "$zip").sha256")
|
||||
ls -la "$REL"
|
||||
echo "$zip"
|
||||
|
||||
# NSIS installer from the same folder (CST_REQUIRE_NSIS=1: fail without makensis).
|
||||
setup="$ROOT/dist/cst-agent-$ver-windows-x86_64-setup.exe"
|
||||
if command -v makensis >/dev/null; then
|
||||
rm -f "$setup"
|
||||
makensis -V2 -WX -INPUTCHARSET UTF8 -DVERSION="$ver" -DSRCDIR="$REL" \
|
||||
-DOUTFILE="$setup" "$ROOT/scripts/windows/cst-agent.nsi"
|
||||
(cd "$ROOT/dist" && sha256sum "$(basename "$setup")" >"$(basename "$setup").sha256")
|
||||
echo "$setup"
|
||||
elif [[ "${CST_REQUIRE_NSIS:-0}" == 1 ]]; then
|
||||
echo "makensis not found" >&2
|
||||
exit 1
|
||||
else
|
||||
echo "makensis not found; skipping the installer" >&2
|
||||
fi
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
# Publish dist/cst-agent-VERSION-windows-x86_64.zip (+ .sha256) to the Gitea
|
||||
# release vVERSION, creating the release (and its tag at HEAD) when missing.
|
||||
# GITEA_TOKEN=... scripts/release_windows.sh [--build]
|
||||
# --build runs scripts/bootstrap_windows.sh first. The token is only read from
|
||||
# the environment and handed to curl on stdin: it never appears in argv, on
|
||||
# disk or in the output. Assets of the same name are replaced.
|
||||
# Upload release files to the Gitea release vVERSION, creating the release
|
||||
# (and its tag at HEAD) when missing.
|
||||
# GITEA_TOKEN=... scripts/release.sh FILE...
|
||||
# Each FILE's FILE.sha256 is uploaded too when it exists; assets of the same
|
||||
# name are replaced. Several CI jobs may call this at once: the first creates
|
||||
# the release, the others find it. The token is only read from the
|
||||
# environment and handed to curl on stdin: it never appears in argv, on disk
|
||||
# or in the output.
|
||||
#
|
||||
# CI: set GITEA_TOKEN (or GITHUB_TOKEN), CST_GITEA, CST_REPO. The remote-branch
|
||||
# check is skipped under GITEA_ACTIONS / GITHUB_ACTIONS / CI.
|
||||
@@ -19,16 +21,20 @@ TOKEN="${GITEA_TOKEN:-${GITHUB_TOKEN:-}}"
|
||||
echo "set GITEA_TOKEN (a Gitea token with write:repository)" >&2
|
||||
exit 1
|
||||
}
|
||||
[[ $# -gt 0 ]] || { echo "usage: $0 FILE..." >&2; exit 1; }
|
||||
command -v python3 >/dev/null || { echo "missing python3" >&2; exit 1; }
|
||||
|
||||
files=()
|
||||
for f in "$@"; do
|
||||
[[ -f "$f" ]] || { echo "missing $f" >&2; exit 1; }
|
||||
f="$(cd "$(dirname "$f")" && pwd)/$(basename "$f")"
|
||||
files+=("$f")
|
||||
[[ "$f" != *.sha256 && -f "$f.sha256" ]] && files+=("$f.sha256")
|
||||
done
|
||||
cd "$ROOT"
|
||||
[[ "${1:-}" == --build ]] && scripts/bootstrap_windows.sh "$ROOT"
|
||||
|
||||
ver="$(sed -n 's/^#define CST_VERSION_\(MAJOR\|MINOR\|PATCH\) \([0-9]*\)$/\2/p' include/cst_version.h | paste -sd.)"
|
||||
tag="v$ver"
|
||||
zip="dist/cst-agent-$ver-windows-x86_64.zip"
|
||||
for f in "$zip" "$zip.sha256"; do
|
||||
[[ -f "$f" ]] || { echo "missing $f (scripts/package_windows.sh, or --build)" >&2; exit 1; }
|
||||
done
|
||||
|
||||
# If this job was triggered by a version tag, it must match cst_version.h.
|
||||
if [[ "${GITHUB_REF:-}" == refs/tags/v* ]]; then
|
||||
@@ -51,6 +57,12 @@ json() { # CODE [ARGS...]: d = the JSON on stdin, ARGS in sys.argv[1:]
|
||||
python3 -c "import json,sys; d=json.load(sys.stdin); $code" "$@"
|
||||
}
|
||||
|
||||
BODY="Linux (static, musl, any distro): cst-agent-$ver-linux-{x86_64,x86,aarch64}.tar.gz -- extract and run ./cst-agent, or copy it onto PATH.
|
||||
|
||||
Windows x86_64: cst-agent-$ver-windows-x86_64-setup.exe installs per user (no admin rights) and adds PATH; cst-agent-$ver-windows-x86_64.zip is the portable build (install_windows.ps1 inside).
|
||||
|
||||
Every file has a .sha256 next to it."
|
||||
|
||||
if rel="$(api GET "/releases/tags/$tag" 2>/dev/null)"; then
|
||||
echo "==> release $tag exists"
|
||||
else
|
||||
@@ -62,12 +74,22 @@ else
|
||||
fi
|
||||
fi
|
||||
echo "==> creating release $tag at $head"
|
||||
body="$(python3 -c 'import json,sys; print(json.dumps({"tag_name": sys.argv[1], "target_commitish": sys.argv[2], "name": "CST-Agent " + sys.argv[1], "body": "Windows x86_64: unzip, or install with install_windows.ps1 from the zip.\\n\\nArtifacts: `cst-agent-VERSION-windows-x86_64.zip` + `.sha256`."}))' "$tag" "$head")"
|
||||
rel="$(api POST /releases -H 'Content-Type: application/json' -d "$body")"
|
||||
body="$(python3 -c 'import json,sys; print(json.dumps({"tag_name": sys.argv[1], "target_commitish": sys.argv[2], "name": "CST-Agent " + sys.argv[1], "body": sys.argv[3]}))' "$tag" "$head" "$BODY")"
|
||||
if ! rel="$(api POST /releases -H 'Content-Type: application/json' -d "$body" 2>/dev/null)"; then
|
||||
# Another job created it first (409).
|
||||
rel=""
|
||||
for _ in 1 2 3 4 5 6; do
|
||||
sleep 5
|
||||
rel="$(api GET "/releases/tags/$tag" 2>/dev/null)" && break
|
||||
rel=""
|
||||
done
|
||||
[[ -n "$rel" ]] || { echo "could not create or find release $tag" >&2; exit 1; }
|
||||
echo "==> release $tag created by another job"
|
||||
fi
|
||||
fi
|
||||
id="$(json 'print(d["id"])' <<<"$rel")"
|
||||
|
||||
for f in "$zip" "$zip.sha256"; do
|
||||
for f in "${files[@]}"; do
|
||||
name="$(basename "$f")"
|
||||
old="$(json "print(' '.join(str(a['id']) for a in d.get('assets') or [] if a['name'] == sys.argv[1]))" "$name" <<<"$rel")"
|
||||
for aid in $old; do
|
||||
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
import sys, time
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from harness import Term
|
||||
import fakeapi
|
||||
from keys import input_text, styled, check, FAILS, base
|
||||
|
||||
for term in ("xterm-256color", "screen-256color"):
|
||||
t = Term(args=("-b", base, "-k", "sk-test"), term=term)
|
||||
t.send("some text")
|
||||
t.send("\x1b", 0.4)
|
||||
check(term + " lone esc clears input", input_text(t) == [], t)
|
||||
t.send("alpha beta")
|
||||
t.send("\x1bb") # alt+b word left
|
||||
t.send("X")
|
||||
check(term + " alt+b word left", "alpha Xbeta" in input_text(t), t)
|
||||
t.send("\x1b\r") # alt+enter newline
|
||||
t.send("Y")
|
||||
check(term + " alt+enter newline", input_text(t)[-2:] == ["alpha X", "Ybeta"], t)
|
||||
t.send("\x03", 0.2)
|
||||
# picker with CSI form
|
||||
t.send("/model\r", 0.8)
|
||||
s0 = styled(t)
|
||||
t.arrow("down", "csi")
|
||||
s1 = styled(t)
|
||||
t.arrow("up", "csi")
|
||||
s2 = styled(t)
|
||||
check(term + " picker csi down", s0 != s1, t)
|
||||
check(term + " picker csi up", s0 == s2, t)
|
||||
t.send("\x1b", 0.4)
|
||||
# home/end variants
|
||||
t.send("middle")
|
||||
for seq in ("\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"):
|
||||
t.send("\x1b[F" + seq)
|
||||
t.send("<")
|
||||
t.send("\x08" if False else "")
|
||||
check(term + " home variants", input_text(t)[-1:] == ["<<<<middle"], t)
|
||||
for seq in ("\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"):
|
||||
t.send("\x1b[H" + seq)
|
||||
t.send(">")
|
||||
check(term + " end variants", input_text(t)[-1:] == ["<<<<middle>>>>"], t)
|
||||
t.close()
|
||||
print("FAILED:", len(FAILS))
|
||||
sys.exit(min(len(FAILS), 100))
|
||||
@@ -0,0 +1,43 @@
|
||||
import json, sys, threading, time
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
LOG = []
|
||||
|
||||
|
||||
class H(BaseHTTPRequestHandler):
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
body = json.dumps({"data": [{"id": "deepseek-flash"}, {"id": "deepseek-v4-pro"}]}).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_POST(self):
|
||||
n = int(self.headers.get("Content-Length", 0))
|
||||
raw = self.rfile.read(n)
|
||||
try:
|
||||
LOG.append(json.loads(raw))
|
||||
except Exception:
|
||||
LOG.append({"_raw": raw[:200].decode("latin1")})
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.end_headers()
|
||||
def ev(o):
|
||||
self.wfile.write(b"data: " + json.dumps(o).encode() + b"\n\n")
|
||||
self.wfile.flush()
|
||||
base = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash"}
|
||||
ev({**base, "choices": [{"index": 0, "delta": {"role": "assistant", "content": "reply-%d" % len(LOG)}}]})
|
||||
ev({**base, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
|
||||
"usage": {"prompt_tokens": 10, "completion_tokens": 2, "total_tokens": 12}})
|
||||
self.wfile.write(b"data: [DONE]\n\n")
|
||||
self.wfile.flush()
|
||||
|
||||
|
||||
def start():
|
||||
s = ThreadingHTTPServer(("127.0.0.1", 0), H)
|
||||
threading.Thread(target=s.serve_forever, daemon=True).start()
|
||||
return s, "http://127.0.0.1:%d/v1" % s.server_address[1]
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Drive the cst-agent TUI in a pty and read the screen back (pyte).
|
||||
|
||||
Run from the repo root after `make`:
|
||||
nix-shell -p python3Packages.pyte --run "python3 scripts/tui_keys/keys.py"
|
||||
nix-shell -p python3Packages.pyte --run "python3 scripts/tui_keys/esc.py"
|
||||
(or `pip install pyte`). A local fake OpenAI-compatible server answers the
|
||||
model calls, so no API key is needed. Exit status is the failure count.
|
||||
"""
|
||||
import os, pty, sys, time, select, fcntl, termios, struct, signal, shutil, tempfile
|
||||
import pyte
|
||||
|
||||
BIN = os.environ.get("CST_BIN") or os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)), "..", "..", "cst-agent")
|
||||
|
||||
|
||||
class Term:
|
||||
def __init__(self, cols=120, rows=36, kitty=False, env=None, args=(), home=None,
|
||||
ws=None, term="xterm-256color"):
|
||||
self.cols, self.rows, self.kitty = cols, rows, kitty
|
||||
self.screen = pyte.Screen(cols, rows)
|
||||
self.stream = pyte.ByteStream(self.screen)
|
||||
self.home = home or tempfile.mkdtemp(prefix="cstk_home_")
|
||||
self.ws = ws or tempfile.mkdtemp(prefix="cstk_ws_")
|
||||
e = dict(os.environ)
|
||||
e.update({"TERM": term, "HOME": self.home,
|
||||
"XDG_CONFIG_HOME": os.path.join(self.home, ".config"),
|
||||
"LANG": "C.UTF-8", "LC_ALL": "C.UTF-8",
|
||||
"CST_PROVIDER": "deepseek", "DEEPSEEK_API_KEY": "sk-dummy-test",
|
||||
"CST_METRICS": "0"})
|
||||
e.pop("NO_COLOR", None)
|
||||
if env:
|
||||
e.update(env)
|
||||
self.raw = b""
|
||||
pid, fd = pty.fork()
|
||||
if pid == 0:
|
||||
os.chdir(self.ws)
|
||||
os.execve(BIN, [BIN, "-N", *args], e)
|
||||
self.pid, self.fd = pid, fd
|
||||
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0))
|
||||
self.pump(1.5)
|
||||
|
||||
def pump(self, t=0.25):
|
||||
end = time.time() + t
|
||||
while True:
|
||||
left = end - time.time()
|
||||
if left <= 0:
|
||||
break
|
||||
r, _, _ = select.select([self.fd], [], [], left)
|
||||
if not r:
|
||||
continue
|
||||
try:
|
||||
d = os.read(self.fd, 65536)
|
||||
except OSError:
|
||||
break
|
||||
if not d:
|
||||
break
|
||||
self.raw += d
|
||||
if self.kitty and b"\x1b[?u" in d:
|
||||
os.write(self.fd, b"\x1b[?0u")
|
||||
self.stream.feed(d)
|
||||
|
||||
def send(self, b, t=0.15):
|
||||
if isinstance(b, str):
|
||||
b = b.encode()
|
||||
os.write(self.fd, b)
|
||||
self.pump(t)
|
||||
|
||||
def appcursor(self):
|
||||
return (1 << 5) in self.screen.mode # pyte DECCKM
|
||||
|
||||
def arrow(self, d, form=None, mod=None, t=0.15):
|
||||
c = {"up": "A", "down": "B", "right": "C", "left": "D"}[d]
|
||||
if mod:
|
||||
seq = "\x1b[1;%d%s" % (mod, c)
|
||||
else:
|
||||
f = form or ("ss3" if self.appcursor() else "csi")
|
||||
seq = ("\x1bO" if f == "ss3" else "\x1b[") + c
|
||||
self.send(seq, t)
|
||||
|
||||
def lines(self):
|
||||
return [l.rstrip() for l in self.screen.display]
|
||||
|
||||
def find(self, s):
|
||||
return [(i, l) for i, l in enumerate(self.lines()) if s in l]
|
||||
|
||||
def cursor(self):
|
||||
return self.screen.cursor.y, self.screen.cursor.x
|
||||
|
||||
def dump(self, title=""):
|
||||
print("=" * 20, title, "cursor", self.cursor())
|
||||
for i, l in enumerate(self.lines()):
|
||||
print("%2d|%s" % (i, l))
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
os.kill(self.pid, signal.SIGTERM)
|
||||
os.waitpid(self.pid, 0)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -0,0 +1,273 @@
|
||||
import os
|
||||
import sys, time
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from harness import Term
|
||||
import fakeapi
|
||||
|
||||
srv, base = fakeapi.start()
|
||||
FAILS = []
|
||||
VERBOSE = "-v" in sys.argv
|
||||
|
||||
|
||||
def check(name, cond, t=None):
|
||||
print(("PASS " if cond else "FAIL ") + name)
|
||||
if not cond:
|
||||
FAILS.append(name)
|
||||
if t:
|
||||
t.dump(name)
|
||||
|
||||
|
||||
def input_text(t):
|
||||
"""Input box rows: the bar rows right above the mode line."""
|
||||
L = t.lines()
|
||||
mode = [i for i, l in enumerate(L) if l.lstrip().startswith("┃ ") and
|
||||
("Build ·" in l or "Plan ·" in l)]
|
||||
if not mode:
|
||||
return []
|
||||
rows = []
|
||||
i = mode[-1] - 1
|
||||
while i >= 0:
|
||||
s = L[i].lstrip()
|
||||
if not s.startswith("┃"):
|
||||
break
|
||||
body = s[2:].split(" ")[0].rstrip() if s.startswith("┃ ") else ""
|
||||
if body == "" and rows:
|
||||
break
|
||||
if body:
|
||||
rows.append(body)
|
||||
i -= 1
|
||||
rows.reverse()
|
||||
if rows and (rows[0].startswith("Ask anything") or rows[0].startswith("Enter queues")):
|
||||
return []
|
||||
return rows
|
||||
|
||||
|
||||
def styled(t):
|
||||
"""Screen text plus the reverse/bg attributes, to see highlight moves."""
|
||||
out = []
|
||||
for y in range(t.rows):
|
||||
row = t.screen.buffer[y]
|
||||
out.append("".join("%s%s%s" % (row[x].data, int(row[x].reverse), row[x].bg[:3])
|
||||
for x in range(t.cols)))
|
||||
return out
|
||||
|
||||
|
||||
def clear(t):
|
||||
if [r for r in input_text(t) if r]:
|
||||
t.send("\x03", 0.2) # ctrl+c clears a non-empty input (twice on empty quits)
|
||||
|
||||
|
||||
def new(kitty=False, **kw):
|
||||
t = Term(kitty=kitty, args=("-b", base, "-k", "sk-test", "-m", "deepseek-flash"), **kw)
|
||||
return t
|
||||
|
||||
|
||||
def basic(t, form, tag):
|
||||
clear(t)
|
||||
t.send("hello world")
|
||||
for _ in range(5):
|
||||
t.arrow("left", form)
|
||||
t.send("X")
|
||||
check(f"{tag} left x5 insert", "hello Xworld" in input_text(t), t)
|
||||
t.arrow("right", form); t.arrow("right", form)
|
||||
t.send("Y")
|
||||
check(f"{tag} right x2 insert", "hello XwoYrld" in input_text(t), t)
|
||||
# boundaries
|
||||
for _ in range(30):
|
||||
t.arrow("left", form, t=0.02)
|
||||
t.pump(0.2)
|
||||
t.send("<")
|
||||
check(f"{tag} left past start", "<hello XwoYrld" in input_text(t), t)
|
||||
for _ in range(30):
|
||||
t.arrow("right", form, t=0.02)
|
||||
t.pump(0.2)
|
||||
t.send(">")
|
||||
check(f"{tag} right past end", "<hello XwoYrld>" in input_text(t), t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def cjk(t):
|
||||
clear(t)
|
||||
t.send("你好世界")
|
||||
y, x_end = t.cursor()
|
||||
t.arrow("left"); t.arrow("left")
|
||||
y2, x2 = t.cursor()
|
||||
check("cjk cursor moves 2 cells per char", x_end - x2 == 4, t)
|
||||
t.send("A")
|
||||
check("cjk insert between wide chars", "你好A世界" in input_text(t), t)
|
||||
t.arrow("right")
|
||||
t.send("B")
|
||||
check("cjk right over wide char", "你好A世B界" in input_text(t), t)
|
||||
clear(t)
|
||||
t.send("a😀b")
|
||||
t.arrow("left"); t.arrow("left")
|
||||
t.send("Z")
|
||||
check("emoji left", "aZ😀b" in input_text(t), t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def multiline(t):
|
||||
clear(t)
|
||||
t.send("line one is long")
|
||||
t.send("\x0e") # ctrl+n newline
|
||||
t.send("l2")
|
||||
t.send("\x0e")
|
||||
t.send("third line here")
|
||||
rows = input_text(t)
|
||||
check("multiline 3 rows", rows[-3:] == ["line one is long", "l2", "third line here"], t)
|
||||
for _ in range(4):
|
||||
t.arrow("left") # cursor before "here"
|
||||
t.arrow("up") # col 11 -> clamp to end of "l2"
|
||||
t.send("U")
|
||||
rows = input_text(t)
|
||||
check("up clamps column to short line", rows[-2] == "l2U", t)
|
||||
t.arrow("up") # col 3 on line one
|
||||
t.send("V")
|
||||
rows = input_text(t)
|
||||
check("up keeps column", rows[-3] == "linVe one is long", t)
|
||||
t.arrow("down"); t.arrow("down")
|
||||
t.send("W")
|
||||
rows = input_text(t)
|
||||
check("down twice to last line", "W" in rows[-1], t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def history(t):
|
||||
clear(t)
|
||||
t.send("first prompt\r", 1.5)
|
||||
t.send("second prompt\r", 1.5)
|
||||
t.arrow("up")
|
||||
check("hist up -> newest", input_text(t)[-1:] == ["second prompt"], t)
|
||||
t.arrow("up")
|
||||
check("hist up -> older", input_text(t)[-1:] == ["first prompt"], t)
|
||||
t.arrow("up")
|
||||
check("hist up at oldest stays", input_text(t)[-1:] == ["first prompt"], t)
|
||||
t.arrow("down")
|
||||
check("hist down -> newer", input_text(t)[-1:] == ["second prompt"], t)
|
||||
t.arrow("down")
|
||||
check("hist down past newest -> empty", input_text(t) in ([], [""]), t)
|
||||
# draft survives a trip through history
|
||||
t.send("my draft")
|
||||
t.arrow("up")
|
||||
t.arrow("down")
|
||||
check("draft restored after up/down", input_text(t)[-1:] == ["my draft"], t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def wrapped(t):
|
||||
clear(t)
|
||||
long = " ".join("w%02d" % i for i in range(40)) # ~160 chars, wraps
|
||||
t.send(long, 0.4)
|
||||
nrows = len(input_text(t))
|
||||
t.arrow("up")
|
||||
txt = "".join(input_text(t))
|
||||
check("up on wrapped line keeps the text", "w39" in txt and "w00" in txt, t)
|
||||
t.send("Q")
|
||||
rows = input_text(t)
|
||||
check("up on wrapped line moves one visual row", nrows >= 2 and "Q" in "".join(rows[:-1]), t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def completion(t):
|
||||
clear(t)
|
||||
t.send("/", 0.3)
|
||||
before = styled(t)
|
||||
t.arrow("down"); t.arrow("down")
|
||||
mid = styled(t)
|
||||
t.arrow("up")
|
||||
after = styled(t)
|
||||
check("completion popup reacts to down", before != mid, t)
|
||||
check("completion up differs from down-down", mid != after, t)
|
||||
check("completion arrows do not touch input", input_text(t)[-1:] == ["/"], t)
|
||||
t.send("\x1b", 0.3)
|
||||
clear(t)
|
||||
|
||||
|
||||
def modifiers(t):
|
||||
clear(t)
|
||||
t.send("alpha beta gamma")
|
||||
t.arrow("left", mod=5) # ctrl+left -> word left
|
||||
t.send("X")
|
||||
check("ctrl+left word", "alpha beta Xgamma" in input_text(t), t)
|
||||
t.arrow("left", mod=3) # alt+left
|
||||
t.arrow("left", mod=3)
|
||||
t.send("Y")
|
||||
check("alt+left word", "alpha Ybeta Xgamma" in input_text(t), t)
|
||||
t.arrow("right", mod=5)
|
||||
t.send("Z")
|
||||
check("ctrl+right word", "alpha YbetaZ Xgamma" in input_text(t), t)
|
||||
t.arrow("left", mod=2) # shift+left: should at least not insert junk
|
||||
t.send("S")
|
||||
txt = "".join(input_text(t))
|
||||
check("shift+left no junk", "[" not in txt and ";" not in txt and "1;2" not in txt, t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def split_and_burst(t, form):
|
||||
clear(t)
|
||||
t.send("abcdef")
|
||||
ss = "\x1bO" if form == "ss3" else "\x1b["
|
||||
# escape sequence split across writes (slow link)
|
||||
import os
|
||||
os.write(t.fd, ss[:1].encode()); time.sleep(0.008)
|
||||
os.write(t.fd, (ss[1:] + "D").encode()); t.pump(0.2)
|
||||
t.send("1")
|
||||
check(f"split {form} seq", "abcde1f" in input_text(t), t)
|
||||
# key-repeat burst in one write
|
||||
t.send((ss + "D") * 4, 0.3)
|
||||
t.send("2")
|
||||
check(f"burst {form} x4", "ab2cde1f" in input_text(t), t)
|
||||
clear(t)
|
||||
|
||||
|
||||
def picker(t):
|
||||
clear(t)
|
||||
t.send("/model\r", 0.8)
|
||||
s0 = styled(t)
|
||||
t.arrow("down")
|
||||
s1 = styled(t)
|
||||
t.arrow("up")
|
||||
s2 = styled(t)
|
||||
check("picker down changes selection", s0 != s1, t)
|
||||
check("picker up returns", s0 == s2, t)
|
||||
t.send("\x1b", 0.4)
|
||||
clear(t)
|
||||
|
||||
|
||||
def run(kitty, tag):
|
||||
print("#####", tag)
|
||||
t = new(kitty=kitty)
|
||||
try:
|
||||
basic(t, None, tag + " native")
|
||||
basic(t, "csi" if t.appcursor() else "ss3", tag + " other-form")
|
||||
cjk(t)
|
||||
multiline(t)
|
||||
modifiers(t)
|
||||
split_and_burst(t, "ss3")
|
||||
split_and_burst(t, "csi")
|
||||
completion(t)
|
||||
wrapped(t)
|
||||
history(t)
|
||||
picker(t)
|
||||
finally:
|
||||
t.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(False, "xterm")
|
||||
run(True, "kitty")
|
||||
for term in ("screen-256color", "tmux-256color", "linux", "vt220", "xterm"):
|
||||
print("#####", term)
|
||||
t = Term(args=("-b", base, "-k", "sk-test"), term=term)
|
||||
try:
|
||||
basic(t, "ss3", term + " ss3")
|
||||
basic(t, "csi", term + " csi")
|
||||
modifiers(t)
|
||||
multiline(t)
|
||||
cjk(t)
|
||||
finally:
|
||||
t.close()
|
||||
print("FAILED:", len(FAILS))
|
||||
for f in FAILS:
|
||||
print(" -", f)
|
||||
sys.exit(min(len(FAILS), 100))
|
||||
@@ -0,0 +1,65 @@
|
||||
"""TUI /provider and /key against a fake API whose /models checks the key.
|
||||
|
||||
nix-shell -p python3Packages.pyte --run "python3 scripts/tui_keys/provider.py"
|
||||
|
||||
A wrong key is asked for again, a good one is saved and switches the
|
||||
provider; /key replaces it. Same code path as the WebUI (provider_switch.c).
|
||||
"""
|
||||
import json, os, sys, threading
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from harness import Term
|
||||
from keys import check, base
|
||||
|
||||
GOOD = {"sk-good", "sk-good2"}
|
||||
|
||||
|
||||
class B(BaseHTTPRequestHandler):
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
auth = self.headers.get("Authorization", "")
|
||||
ok = auth.startswith("Bearer ") and auth[7:] in GOOD
|
||||
body = json.dumps({"data": [{"id": "fake-one"}]} if ok else {"error": {"message": "bad key"}}).encode()
|
||||
self.send_response(200 if ok else 401)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
|
||||
srv = ThreadingHTTPServer(("127.0.0.1", 0), B)
|
||||
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
||||
|
||||
t = Term(args=("-b", base, "-k", "sk-test"))
|
||||
cfg = os.path.join(t.home, ".config", "cst-agent")
|
||||
os.makedirs(cfg, exist_ok=True)
|
||||
t.close()
|
||||
with open(os.path.join(cfg, "models.json"), "w") as f:
|
||||
json.dump({"providers": {"fakeb": {"api_base": "http://127.0.0.1:%d/v1" % srv.server_address[1],
|
||||
"key_env": "FAKEB_API_KEY", "default_model": ""}}}, f)
|
||||
t = Term(args=("-b", base, "-k", "sk-test"), home=t.home, env={"FAKEB_API_KEY": ""})
|
||||
creds = os.path.join(cfg, "credentials")
|
||||
|
||||
|
||||
def screen():
|
||||
return "\n".join(t.lines())
|
||||
|
||||
|
||||
t.send("/provider fakeb\r", 0.8)
|
||||
check("a key prompt for fakeb", "API key for fakeb" in screen(), t)
|
||||
t.send("sk-bad\r", 1.5)
|
||||
check("a wrong key is asked for again", "rejected that key" in screen(), t)
|
||||
check("the wrong key is not saved", not os.path.exists(creds) or "sk-bad" not in open(creds).read(), t)
|
||||
t.send("sk-good\r", 2.0)
|
||||
check("switched to fakeb", "provider → fakeb" in screen(), t)
|
||||
check("the good key is saved", os.path.exists(creds) and "fakeb.api_key=sk-good" in open(creds).read(), t)
|
||||
check("no key on screen", "sk-good" not in screen() and "sk-bad" not in screen(), t)
|
||||
t.send("/key\r", 0.8)
|
||||
t.send("sk-good2\r", 2.0)
|
||||
check("/key replaces it", "fakeb.api_key=sk-good2" in open(creds).read(), t)
|
||||
t.close()
|
||||
from keys import FAILS
|
||||
print("FAILED:", len(FAILS))
|
||||
sys.exit(min(len(FAILS), 100))
|
||||
@@ -0,0 +1,151 @@
|
||||
; CST-Agent per-user installer (no admin rights).
|
||||
; makensis -INPUTCHARSET UTF8 -DVERSION=0.8.5 -DSRCDIR=dist/windows-x86_64 -DOUTFILE=setup.exe cst-agent.nsi
|
||||
; Installs the same files as the zip into %LOCALAPPDATA%\Programs\cst-agent,
|
||||
; adds that folder to the user PATH and registers an uninstaller under HKCU.
|
||||
Unicode true
|
||||
SetCompressor /SOLID lzma
|
||||
RequestExecutionLevel user
|
||||
|
||||
!ifndef VERSION
|
||||
!error "pass -DVERSION=x.y.z"
|
||||
!endif
|
||||
!ifndef SRCDIR
|
||||
!error "pass -DSRCDIR=<dist/windows-x86_64>"
|
||||
!endif
|
||||
!ifndef OUTFILE
|
||||
!define OUTFILE "cst-agent-${VERSION}-windows-x86_64-setup.exe"
|
||||
!endif
|
||||
|
||||
!define APP "CST-Agent"
|
||||
!define UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\cst-agent"
|
||||
|
||||
!include "MUI2.nsh"
|
||||
!include "LogicLib.nsh"
|
||||
!include "x64.nsh"
|
||||
|
||||
Name "${APP} ${VERSION}"
|
||||
OutFile "${OUTFILE}"
|
||||
InstallDir "$LOCALAPPDATA\Programs\cst-agent"
|
||||
InstallDirRegKey HKCU "${UNINST_KEY}" "InstallLocation"
|
||||
BrandingText "${APP} ${VERSION}"
|
||||
|
||||
VIProductVersion "${VERSION}.0"
|
||||
VIAddVersionKey /LANG=1033 "ProductName" "${APP}"
|
||||
VIAddVersionKey /LANG=1033 "ProductVersion" "${VERSION}"
|
||||
VIAddVersionKey /LANG=1033 "FileVersion" "${VERSION}"
|
||||
VIAddVersionKey /LANG=1033 "FileDescription" "${APP} installer"
|
||||
VIAddVersionKey /LANG=1033 "LegalCopyright" "WHC"
|
||||
|
||||
!define MUI_ABORTWARNING
|
||||
!define MUI_FINISHPAGE_RUN
|
||||
!define MUI_FINISHPAGE_RUN_TEXT "$(RunWebUI)"
|
||||
!define MUI_FINISHPAGE_RUN_FUNCTION LaunchWebUI
|
||||
|
||||
!insertmacro MUI_PAGE_DIRECTORY
|
||||
!insertmacro MUI_PAGE_INSTFILES
|
||||
!insertmacro MUI_PAGE_FINISH
|
||||
!insertmacro MUI_UNPAGE_CONFIRM
|
||||
!insertmacro MUI_UNPAGE_INSTFILES
|
||||
|
||||
!insertmacro MUI_LANGUAGE "English"
|
||||
!insertmacro MUI_LANGUAGE "SimpChinese"
|
||||
|
||||
LangString NeedX64 ${LANG_ENGLISH} "${APP} needs 64-bit Windows."
|
||||
LangString NeedX64 ${LANG_SIMPCHINESE} "${APP} 需要 64 位 Windows。"
|
||||
LangString NeedBuild ${LANG_ENGLISH} "${APP} needs Windows 10 1903 (build 18362) or newer; this is build $0."
|
||||
LangString NeedBuild ${LANG_SIMPCHINESE} "${APP} 需要 Windows 10 1903(内部版本 18362)或更新版本,当前为 $0。"
|
||||
LangString RunWebUI ${LANG_ENGLISH} "Open the ${APP} WebUI"
|
||||
LangString RunWebUI ${LANG_SIMPCHINESE} "打开 ${APP} WebUI"
|
||||
LangString PathFail ${LANG_ENGLISH} "Could not add $INSTDIR to the user PATH; add it by hand."
|
||||
LangString PathFail ${LANG_SIMPCHINESE} "未能把 $INSTDIR 加入用户 PATH,请手动添加。"
|
||||
|
||||
; PowerShell edits the user PATH: NSIS strings are capped at 1024 chars, and
|
||||
; .NET broadcasts the change so new terminals see it. The folder is passed in
|
||||
; the environment, so quotes in it cannot break the command.
|
||||
!macro EditUserPath ACTION
|
||||
System::Call 'Kernel32::SetEnvironmentVariable(t "CST_INSTDIR", t "$INSTDIR")'
|
||||
nsExec::ExecToLog `powershell -NoProfile -ExecutionPolicy Bypass -Command "$$d = $$env:CST_INSTDIR.TrimEnd('\'); $$p = [Environment]::GetEnvironmentVariable('Path', 'User'); $$parts = @([string]$$p -split ';' | Where-Object { $$_ -and $$_.TrimEnd('\') -ine $$d }); if ('${ACTION}' -eq 'add') { $$parts += $$d }; [Environment]::SetEnvironmentVariable('Path', ($$parts -join ';'), 'User')"`
|
||||
Pop $0
|
||||
!macroend
|
||||
|
||||
; A running exe cannot be overwritten but can be renamed out of the way.
|
||||
!macro MoveAside NAME
|
||||
Delete "$INSTDIR\${NAME}.old"
|
||||
ClearErrors
|
||||
Delete "$INSTDIR\${NAME}"
|
||||
${If} ${Errors}
|
||||
Rename "$INSTDIR\${NAME}" "$INSTDIR\${NAME}.old"
|
||||
${EndIf}
|
||||
!macroend
|
||||
|
||||
Function .onInit
|
||||
${IfNot} ${RunningX64}
|
||||
MessageBox MB_ICONSTOP "$(NeedX64)"
|
||||
Abort
|
||||
${EndIf}
|
||||
SetRegView 64
|
||||
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentBuildNumber"
|
||||
${If} $0 < 18362
|
||||
MessageBox MB_ICONSTOP "$(NeedBuild)"
|
||||
Abort
|
||||
${EndIf}
|
||||
FunctionEnd
|
||||
|
||||
Function LaunchWebUI
|
||||
SetOutPath "$PROFILE"
|
||||
Exec '"$INSTDIR\cst-agent.exe" --web --web-open'
|
||||
FunctionEnd
|
||||
|
||||
Section "Install"
|
||||
SetShellVarContext current
|
||||
SetOutPath "$INSTDIR"
|
||||
!insertmacro MoveAside "cst-agent.exe"
|
||||
!insertmacro MoveAside "busybox.exe"
|
||||
File "${SRCDIR}/cst-agent.exe"
|
||||
File "${SRCDIR}/busybox.exe"
|
||||
File "${SRCDIR}/LICENSE.busybox"
|
||||
File "${SRCDIR}/README.md"
|
||||
File "${SRCDIR}/cst-agent-update.cmd"
|
||||
File "${SRCDIR}/install_windows.ps1"
|
||||
File "${SRCDIR}/VERSION"
|
||||
WriteUninstaller "$INSTDIR\uninstall.exe"
|
||||
|
||||
!insertmacro EditUserPath "add"
|
||||
${If} $0 != 0
|
||||
DetailPrint "$(PathFail)"
|
||||
${EndIf}
|
||||
|
||||
; The WebUI's workspace is the shortcut's start folder.
|
||||
SetOutPath "$PROFILE"
|
||||
CreateShortCut "$SMPROGRAMS\${APP} WebUI.lnk" "$INSTDIR\cst-agent.exe" "--web --web-open" "$INSTDIR\cst-agent.exe" 0
|
||||
SetOutPath "$INSTDIR"
|
||||
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "DisplayName" "${APP}"
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "DisplayVersion" "${VERSION}"
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "Publisher" "WHC"
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "DisplayIcon" "$INSTDIR\cst-agent.exe"
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "InstallLocation" "$INSTDIR"
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "UninstallString" '"$INSTDIR\uninstall.exe"'
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "QuietUninstallString" '"$INSTDIR\uninstall.exe" /S'
|
||||
WriteRegStr HKCU "${UNINST_KEY}" "URLInfoAbout" "https://git.whcself.xyz/WHC/CST-agent"
|
||||
WriteRegDWORD HKCU "${UNINST_KEY}" "NoModify" 1
|
||||
WriteRegDWORD HKCU "${UNINST_KEY}" "NoRepair" 1
|
||||
SectionEnd
|
||||
|
||||
Section "Uninstall"
|
||||
SetShellVarContext current
|
||||
!insertmacro EditUserPath "remove"
|
||||
Delete "$SMPROGRAMS\${APP} WebUI.lnk"
|
||||
Delete "$INSTDIR\cst-agent.exe"
|
||||
Delete "$INSTDIR\cst-agent.exe.old"
|
||||
Delete "$INSTDIR\busybox.exe"
|
||||
Delete "$INSTDIR\busybox.exe.old"
|
||||
Delete "$INSTDIR\LICENSE.busybox"
|
||||
Delete "$INSTDIR\README.md"
|
||||
Delete "$INSTDIR\cst-agent-update.cmd"
|
||||
Delete "$INSTDIR\install_windows.ps1"
|
||||
Delete "$INSTDIR\VERSION"
|
||||
Delete "$INSTDIR\uninstall.exe"
|
||||
RMDir "$INSTDIR"
|
||||
DeleteRegKey HKCU "${UNINST_KEY}"
|
||||
SectionEnd
|
||||
@@ -15,6 +15,7 @@ win.mkShell {
|
||||
pkg-config
|
||||
zip
|
||||
curl
|
||||
nsis
|
||||
];
|
||||
buildInputs = [ win.windows.pthreads ];
|
||||
# mkShell exports CC/AR for the target; the build scripts call the prefixed tools directly.
|
||||
|
||||
+190
-38
@@ -177,8 +177,62 @@ static int tool_on(const cst_agent_config_t *cfg, const char *name) {
|
||||
return cst_tool_active(cfg, cst_tool_find(name));
|
||||
}
|
||||
|
||||
/* /mode general (cst_mode_name covers "" and unknown values). */
|
||||
static int general_mode(const cst_agent_config_t *cfg) {
|
||||
return cfg && strcmp(cst_mode_name(cfg->mode), "general") == 0;
|
||||
}
|
||||
|
||||
/* Workflow for /mode general: gather, then do the work, then check it. */
|
||||
static void put_workflow_general(cst_buf_t *b, const cst_agent_config_t *cfg) {
|
||||
cst_buf_puts(b, "\nWorkflow:\n1) Gather what you need");
|
||||
const char *explore[] = {"list_dir", "glob", "grep"};
|
||||
int n = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (tool_on(cfg, explore[i]))
|
||||
cst_buf_printf(b, "%s%s", n++ ? ", " : " (", explore[i]);
|
||||
}
|
||||
if (n)
|
||||
cst_buf_puts(b, ")");
|
||||
if (tool_on(cfg, "read_file"))
|
||||
cst_buf_puts(b, "; read big files in slices with read_file offset/limit, "
|
||||
"and use the documents the user attached");
|
||||
cst_buf_puts(b, ".\n2) Do the work");
|
||||
n = 0;
|
||||
if (tool_on(cfg, "write_file")) {
|
||||
cst_buf_puts(b, ": write_file for new files");
|
||||
n++;
|
||||
}
|
||||
if (tool_on(cfg, "edit_file"))
|
||||
cst_buf_printf(b, "%sedit_file for small exact replacements",
|
||||
n++ ? ", " : ": ");
|
||||
if (tool_on(cfg, "apply_patch"))
|
||||
cst_buf_printf(b, "%sapply_patch for multi-hunk diffs to existing files",
|
||||
n++ ? ", " : ": ");
|
||||
if (tool_on(cfg, "run_shell"))
|
||||
cst_buf_printf(b, "%srun_shell for commands, computations, data "
|
||||
"processing and other CLIs",
|
||||
n++ ? ", " : ": ");
|
||||
cst_buf_puts(b, ".\n");
|
||||
if (tool_on(cfg, "run_shell"))
|
||||
cst_buf_puts(b, "3) Check the result before saying it works (re-read it, "
|
||||
"run it, or verify the numbers); fix and re-run until it "
|
||||
"holds.\n");
|
||||
if (tool_on(cfg, "todo_write"))
|
||||
cst_buf_puts(b, "- For work with several steps keep a todo list with "
|
||||
"todo_write and update it as you go.\n");
|
||||
if (tool_on(cfg, "ask_user"))
|
||||
cst_buf_puts(b, "- Use ask_user only when a decision is truly the user's; "
|
||||
"otherwise choose sensibly and say so.\n");
|
||||
cst_buf_puts(b, "Finish with a short plain-text summary of the result.\n"
|
||||
"Never invent other tool names.\n");
|
||||
}
|
||||
|
||||
/* Workflow section for the default tool profile; names only active tools. */
|
||||
static void put_workflow(cst_buf_t *b, const cst_agent_config_t *cfg) {
|
||||
if (general_mode(cfg)) {
|
||||
put_workflow_general(b, cfg);
|
||||
return;
|
||||
}
|
||||
cst_buf_puts(b, "\nWorkflow:\n1) Inspect only what you need");
|
||||
const char *explore[] = {"list_dir", "glob", "grep"};
|
||||
int n = 0;
|
||||
@@ -235,6 +289,25 @@ static const char DEF_INTRO[] =
|
||||
"could not verify, say so.\n"
|
||||
"- Keep going until the task is done or truly blocked.\n\n";
|
||||
|
||||
/* Same role, framed for /mode general: the workspace may hold documents,
|
||||
* data or anything else, not just code. */
|
||||
static const char DEF_INTRO_GENERAL[] =
|
||||
"You are CST-Agent, a general-purpose assistant working in the user's "
|
||||
"local workspace. All tool paths are relative to the workspace root and "
|
||||
"shell commands run there; the material you work on may be code, "
|
||||
"documents, data or anything else.\n\n"
|
||||
"Principles:\n"
|
||||
"- Reply in the user's language. Be concise: lead with the result, no "
|
||||
"filler.\n"
|
||||
"- Understand before answering: read the files, documents and sources the "
|
||||
"task refers to instead of guessing; never invent content, numbers or "
|
||||
"tool results.\n"
|
||||
"- Do what was asked, at the scope asked: leave unrelated files, data and "
|
||||
"material alone.\n"
|
||||
"- Check your work when you can (re-read it, run it, cross-check the "
|
||||
"source); if you could not verify something, say so.\n"
|
||||
"- Keep going until the task is done or truly blocked.\n\n";
|
||||
|
||||
static void put_tool_use(cst_buf_t *b, const cst_agent_config_t *cfg) {
|
||||
cst_buf_puts(b, "\nTool use:\n");
|
||||
if (tool_on(cfg, "run_shell") &&
|
||||
@@ -248,18 +321,29 @@ static void put_tool_use(cst_buf_t *b, const cst_agent_config_t *cfg) {
|
||||
cst_buf_puts(b, "- Read a file before editing it and again after a failed "
|
||||
"edit. The `N|` prefixes in read_file output are not part "
|
||||
"of the file.\n");
|
||||
if (general_mode(cfg) && tool_on(cfg, "web_fetch"))
|
||||
cst_buf_puts(b, "- Use web_fetch for pages the user points you to; say so "
|
||||
"when a page could not be fetched instead of guessing its "
|
||||
"content.\n");
|
||||
cst_buf_puts(b, "- When a call fails, read the error and change approach "
|
||||
"instead of repeating the same call.\n");
|
||||
}
|
||||
|
||||
static void put_safety(cst_buf_t *b, const cst_agent_config_t *cfg) {
|
||||
cst_buf_puts(b, "\nSafety:\n"
|
||||
"- Do not commit, push or rewrite git history unless asked. "
|
||||
"Do not delete or overwrite files you did not create unless "
|
||||
"the task requires it. Never print secrets or API keys.\n");
|
||||
if (general_mode(cfg))
|
||||
cst_buf_puts(b, "\nSafety:\n"
|
||||
"- Do not delete or overwrite files or data you did not "
|
||||
"create unless the task requires it, and do not send the "
|
||||
"user's content anywhere the task did not ask for. Never "
|
||||
"print secrets or API keys.\n");
|
||||
else
|
||||
cst_buf_puts(b, "\nSafety:\n"
|
||||
"- Do not commit, push or rewrite git history unless asked. "
|
||||
"Do not delete or overwrite files you did not create unless "
|
||||
"the task requires it. Never print secrets or API keys.\n");
|
||||
if (tool_on(cfg, "run_shell"))
|
||||
cst_buf_puts(b, "- run_shell may run in a sandbox (writes only inside the "
|
||||
"workspace and temp dir, no network). If a command is "
|
||||
"workspace and temp dir; the network works). If a command is "
|
||||
"blocked the user is asked whether to rerun it outside; "
|
||||
"do not try to work around the sandbox.\n");
|
||||
}
|
||||
@@ -365,7 +449,9 @@ char *cst_agent_build_prompt(const cst_agent_config_t *cfg, cst_protocol_t p,
|
||||
const char *project_notes) {
|
||||
int minimal = !cfg || cfg->tool_profile == CST_TOOLS_MINIMAL;
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, minimal ? PROMPT_BASE : DEF_INTRO);
|
||||
cst_buf_puts(&b, minimal ? PROMPT_BASE
|
||||
: general_mode(cfg) ? DEF_INTRO_GENERAL
|
||||
: DEF_INTRO);
|
||||
if (cst_protocol_is_native(p))
|
||||
cst_buf_puts(&b, minimal ? SEC_NATIVE
|
||||
: "Call tools through the native "
|
||||
@@ -827,13 +913,9 @@ static void sandbox_escalate(turn_t *t, cst_tool_ctx_t *ctx,
|
||||
}
|
||||
if (ans == CST_PERM_DENY) {
|
||||
const char *note =
|
||||
cst_sandbox_probe() == CST_SANDBOX_LOWIL
|
||||
? "\n[cst: blocked by the sandbox (writes are limited to the "
|
||||
"workspace and temp dir); the user did not allow running it "
|
||||
"outside]\n"
|
||||
: "\n[cst: blocked by the sandbox (writes are limited to "
|
||||
"the workspace and temp dir, no network); the user did "
|
||||
"not allow running it outside]\n";
|
||||
"\n[cst: blocked by the sandbox (writes are limited to the "
|
||||
"workspace and temp dir); the user did not allow running it "
|
||||
"outside]\n";
|
||||
cst_buf_t b = {0};
|
||||
if (tr->out)
|
||||
cst_buf_append(&b, tr->out, tr->out_len);
|
||||
@@ -989,11 +1071,9 @@ static void exec_begin(turn_t *t, pcall_t *pc, exec_t *x) {
|
||||
if (def->perm == CST_TP_WRITE) {
|
||||
ask = 0;
|
||||
} else if (def->perm == CST_TP_SHELL) {
|
||||
int sb = cst_sandbox_probe();
|
||||
if (sb != CST_SANDBOX_NONE) {
|
||||
if (cst_sandbox_probe() != CST_SANDBOX_NONE) {
|
||||
ctx->sandbox = 1;
|
||||
/* the Low-IL sandbox does not stop network access */
|
||||
ask = sb == CST_SANDBOX_LOWIL && cmd && cst_shell_uses_network(cmd);
|
||||
ask = 0;
|
||||
} else if (cmd && cst_shell_is_safe(cmd)) {
|
||||
ask = 0;
|
||||
}
|
||||
@@ -1288,13 +1368,22 @@ static void on_mm(const ctl_event_t *ev, void *ud) {
|
||||
}
|
||||
|
||||
static void feed_visible(turn_t *t, const char *data, size_t len) {
|
||||
if (!len || t->stop)
|
||||
return;
|
||||
cst_buf_append(&t->raw, data, len);
|
||||
if (t->ctl)
|
||||
ctl_parser_feed(t->ctl, data, len);
|
||||
else if (t->mm)
|
||||
mm_scan_feed(t->mm, data, len);
|
||||
/* cut after each '\n' and '>': a call only becomes ready there, and
|
||||
* raw_mark must not take in what follows it in the same chunk */
|
||||
while (len && !t->stop) {
|
||||
size_t n = 0;
|
||||
while (n < len && data[n] != '\n' && data[n] != '>')
|
||||
n++;
|
||||
if (n < len)
|
||||
n++;
|
||||
cst_buf_append(&t->raw, data, n);
|
||||
if (t->ctl)
|
||||
ctl_parser_feed(t->ctl, data, n);
|
||||
else if (t->mm)
|
||||
mm_scan_feed(t->mm, data, n);
|
||||
data += n;
|
||||
len -= n;
|
||||
}
|
||||
}
|
||||
|
||||
static void think_out(turn_t *t, const char *s, size_t n) {
|
||||
@@ -1312,10 +1401,16 @@ static void on_delta(const char *data, size_t len, void *ud) {
|
||||
const char *want = t->in_think ? "</think>" : "<think>";
|
||||
size_t wl = strlen(want);
|
||||
if (t->taglen == 0 && c != '<') {
|
||||
/* the whole run up to the next '<' in one call: byte by byte, a
|
||||
* front end would get every CJK character in three invalid pieces */
|
||||
size_t j = i + 1;
|
||||
while (j < len && data[j] != '<')
|
||||
j++;
|
||||
if (t->in_think)
|
||||
think_out(t, &c, 1);
|
||||
think_out(t, data + i, j - i);
|
||||
else
|
||||
feed_visible(t, &c, 1);
|
||||
feed_visible(t, data + i, j - i);
|
||||
i = j - 1;
|
||||
continue;
|
||||
}
|
||||
t->tagbuf[t->taglen++] = c;
|
||||
@@ -2232,11 +2327,12 @@ int cst_agent_undo_available(const cst_agent_t *a) {
|
||||
* later changes reach the model as persisted notes appended to the history,
|
||||
* which keeps the cached prefix intact. */
|
||||
|
||||
#define PROMPT_VERSION 2
|
||||
#define PROMPT_VERSION 3
|
||||
|
||||
typedef struct {
|
||||
int plan;
|
||||
char approval[96];
|
||||
char mode[16];
|
||||
char branch[128];
|
||||
char day[16];
|
||||
char notes[24]; /* hash of AGENTS.md, "" = none */
|
||||
@@ -2246,6 +2342,7 @@ static void state_now(const cst_agent_t *a, pstate_t *s) {
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->plan = a->cfg.plan_mode;
|
||||
snprintf(s->approval, sizeof(s->approval), "%s", approval_desc(&a->cfg));
|
||||
snprintf(s->mode, sizeof(s->mode), "%s", cst_mode_name(a->cfg.mode));
|
||||
git_branch(a->cfg.workspace_root, s->branch, sizeof(s->branch));
|
||||
today(s->day, sizeof(s->day));
|
||||
if (a->notes && a->notes[0])
|
||||
@@ -2258,6 +2355,7 @@ static cJSON *state_json(const pstate_t *s) {
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(o, "plan", s->plan);
|
||||
cJSON_AddStringToObject(o, "approval", s->approval);
|
||||
cJSON_AddStringToObject(o, "mode", s->mode);
|
||||
cJSON_AddStringToObject(o, "branch", s->branch);
|
||||
cJSON_AddStringToObject(o, "day", s->day);
|
||||
cJSON_AddStringToObject(o, "notes", s->notes);
|
||||
@@ -2273,6 +2371,7 @@ static void state_parse(const cJSON *o, pstate_t *s) {
|
||||
if (cJSON_IsString(v)) \
|
||||
snprintf(s->field, sizeof(s->field), "%s", v->valuestring);
|
||||
STATE_STR("approval", approval)
|
||||
STATE_STR("mode", mode)
|
||||
STATE_STR("branch", branch)
|
||||
STATE_STR("day", day)
|
||||
STATE_STR("notes", notes)
|
||||
@@ -2284,6 +2383,7 @@ static char *system_meta(const cst_agent_t *a, const pstate_t *s) {
|
||||
cJSON_AddStringToObject(o, "protocol", cst_protocol_name(a->cfg.protocol));
|
||||
cJSON_AddNumberToObject(o, "pv", PROMPT_VERSION);
|
||||
cJSON_AddNumberToObject(o, "tools", a->cfg.tool_profile);
|
||||
cJSON_AddStringToObject(o, "mode", cst_mode_name(a->cfg.mode));
|
||||
cJSON_AddItemToObject(o, "state", state_json(s));
|
||||
char *out = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
@@ -2303,10 +2403,13 @@ static int system_current(const cst_message_t *m, const cst_agent_config_t *c) {
|
||||
const cJSON *pv = cJSON_GetObjectItemCaseSensitive(meta, "pv");
|
||||
const cJSON *pr = cJSON_GetObjectItemCaseSensitive(meta, "protocol");
|
||||
const cJSON *tp = cJSON_GetObjectItemCaseSensitive(meta, "tools");
|
||||
const cJSON *md = cJSON_GetObjectItemCaseSensitive(meta, "mode");
|
||||
int ok = cJSON_IsNumber(pv) && pv->valueint == PROMPT_VERSION &&
|
||||
cJSON_IsString(pr) &&
|
||||
strcmp(pr->valuestring, cst_protocol_name(c->protocol)) == 0 &&
|
||||
cJSON_IsNumber(tp) && tp->valueint == c->tool_profile;
|
||||
ok = ok && cJSON_IsString(md) &&
|
||||
strcmp(md->valuestring, cst_mode_name(c->mode)) == 0;
|
||||
cJSON_Delete(meta);
|
||||
return ok;
|
||||
}
|
||||
@@ -2384,6 +2487,14 @@ static void sync_notes(cst_agent_t *a) {
|
||||
"is available again.\n");
|
||||
if (strcmp(now.approval, was.approval) != 0)
|
||||
cst_buf_printf(&b, "[cst] Approval mode is now %s.\n", now.approval);
|
||||
if (strcmp(now.mode, was.mode) != 0)
|
||||
cst_buf_printf(&b,
|
||||
"[cst] Working mode is now %s%s. Earlier instructions about "
|
||||
"how to frame the work no longer apply.\n",
|
||||
now.mode,
|
||||
strcmp(now.mode, "general") == 0
|
||||
? " (general: documents, data, research)"
|
||||
: " (coding: repo, build, tests)");
|
||||
if (strcmp(now.branch, was.branch) != 0)
|
||||
cst_buf_printf(&b, "[cst] The git branch is now %s.\n",
|
||||
now.branch[0] ? now.branch : "(none)");
|
||||
@@ -2564,9 +2675,9 @@ static int can_continue(const cst_agent_t *a, const turn_t *t,
|
||||
return st->http_status > 0 && st->http_status < 400 && !st->client_stopped;
|
||||
}
|
||||
|
||||
/* Prompt size at which the session is compacted: CST_CTX_BUDGET, else 60% of
|
||||
* the context window capped at 256K tokens. Even a cached prompt is billed
|
||||
* on every step, so huge windows are not worth filling. */
|
||||
/* Prompt size at which the session is compacted: CST_CTX_BUDGET, else 75% of
|
||||
* the context window, capped at 256K tokens. Even a cached prompt is billed on
|
||||
* every step, so a 1M-token window is not worth filling. */
|
||||
static long ctx_budget(const cst_agent_t *a) {
|
||||
const char *e = getenv("CST_CTX_BUDGET");
|
||||
if (e && atol(e) > 0)
|
||||
@@ -2574,7 +2685,7 @@ static long ctx_budget(const cst_agent_t *a) {
|
||||
long win = cst_model_context_tokens(a->cfg.model);
|
||||
if (win <= 0)
|
||||
return 0;
|
||||
long b = win / 10 * 6;
|
||||
long b = win / 4 * 3;
|
||||
return b > 262144 ? 262144 : b;
|
||||
}
|
||||
|
||||
@@ -2939,13 +3050,18 @@ int cst_agent_new_session(cst_agent_t *a, char *id_out, size_t id_sz) {
|
||||
return -1;
|
||||
if (cst_agent_store_ensure(a->cfg.workspace_root) != 0)
|
||||
return -1;
|
||||
char id[128];
|
||||
if (cst_agent_store_new_id(id, sizeof(id)) != 0)
|
||||
return -1;
|
||||
char path[4096];
|
||||
if (cst_agent_store_path_for_id(a->cfg.workspace_root, id, path,
|
||||
sizeof(path)) != 0)
|
||||
return -1;
|
||||
char id[128], path[4096];
|
||||
/* ids are per second: never reopen (and append to) one that exists */
|
||||
for (int tries = 0;; tries++) {
|
||||
if (tries == 16 || cst_agent_store_new_id(id, sizeof(id)) != 0 ||
|
||||
cst_agent_store_path_for_id(a->cfg.workspace_root, id, path,
|
||||
sizeof(path)) != 0)
|
||||
return -1;
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if (!fp)
|
||||
break;
|
||||
fclose(fp);
|
||||
}
|
||||
if (agent_reopen_session(a, path) != 0)
|
||||
return -1;
|
||||
cst_agent_store_set_current(a->cfg.workspace_root, id);
|
||||
@@ -2954,6 +3070,42 @@ int cst_agent_new_session(cst_agent_t *a, char *id_out, size_t id_sz) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_agent_set_workspace(cst_agent_t *a, const char *path, char *err,
|
||||
size_t err_sz) {
|
||||
char dummy[8];
|
||||
if (!err || !err_sz) {
|
||||
err = dummy;
|
||||
err_sz = sizeof(dummy);
|
||||
}
|
||||
err[0] = 0;
|
||||
if (!a || !path || !path[0]) {
|
||||
snprintf(err, err_sz, "empty path");
|
||||
return -1;
|
||||
}
|
||||
char abs[sizeof(a->cfg.workspace_root)];
|
||||
if (os_realpath(path, abs, sizeof(abs)) != 0) {
|
||||
snprintf(err, err_sz, "no such directory: %s", path);
|
||||
return -1;
|
||||
}
|
||||
/* a file, or a tree we may not write to, fails here instead of halfway
|
||||
* through the next turn */
|
||||
if (cst_agent_store_ensure(abs) != 0) {
|
||||
snprintf(err, err_sz, "cannot use as a workspace: %s", abs);
|
||||
return -1;
|
||||
}
|
||||
snprintf(a->cfg.workspace_root, sizeof(a->cfg.workspace_root), "%s", abs);
|
||||
/* everything that is kept per workspace follows the new root */
|
||||
cst_perms_close(a->perms);
|
||||
a->perms = cst_perms_open(a->cfg.workspace_root);
|
||||
cst_metrics_close(a->metrics);
|
||||
a->metrics = cst_metrics_open(a->cfg.workspace_root,
|
||||
cst_session_path(a->session));
|
||||
cst_agent_reload_notes(a);
|
||||
ensure_system(a); /* the prompt names the directory */
|
||||
a->last_prompt_tokens = 0; /* the old budget was measured elsewhere */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_agent_switch_session(cst_agent_t *a, const char *id) {
|
||||
if (!a || !id || !id[0])
|
||||
return -1;
|
||||
|
||||
+2
-1
@@ -57,7 +57,8 @@ static void make_id(char *out, size_t n) {
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
os_localtime(&now, &tm);
|
||||
unsigned r = (unsigned)(os_getpid() ^ (unsigned)now);
|
||||
static unsigned seq;
|
||||
unsigned r = (unsigned)(os_getpid() ^ (unsigned)now) + 0x9e37u * seq++;
|
||||
snprintf(out, n, "%04d%02d%02d-%02d%02d%02d-%04x", tm.tm_year + 1900,
|
||||
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
r & 0xffff);
|
||||
|
||||
+129
-24
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "cst_attach.h"
|
||||
#include "cst_pdf.h"
|
||||
#include "cst_util.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
@@ -102,6 +103,9 @@ static int le16(const unsigned char *p) {
|
||||
}
|
||||
|
||||
static const char *mime_of_ext(const char *mime) {
|
||||
if (strcmp(mime, "application/pdf") == 0 ||
|
||||
strcmp(mime, "text/plain") == 0)
|
||||
return "txt"; /* a document is stored as the text it was reduced to */
|
||||
if (strcmp(mime, "image/png") == 0)
|
||||
return "png";
|
||||
if (strcmp(mime, "image/jpeg") == 0)
|
||||
@@ -352,23 +356,73 @@ static void make_id(const char *name, const char *data, size_t n, char *out,
|
||||
snprintf(out, out_sz, "img-%08x", (unsigned)(h & 0xFFFFFFFFu));
|
||||
}
|
||||
|
||||
/* n bytes, NUL-terminated: a text payload is handed around as a C string. */
|
||||
static char *memdup_z(const char *data, size_t n) {
|
||||
char *copy = malloc(n + 1);
|
||||
if (!copy)
|
||||
return NULL;
|
||||
memcpy(copy, data, n);
|
||||
copy[n] = 0;
|
||||
return copy;
|
||||
}
|
||||
|
||||
/* What a picked file is, plus the payload to store for it: an image stays as
|
||||
* it is, a PDF is reduced to its text layer (the mime still says PDF, the
|
||||
* stored file holds text), anything else must be UTF-8 text. The caller frees
|
||||
* the result; *out_len is its length. */
|
||||
static char *prepare(const char *data, size_t n, char *mime, size_t mime_sz,
|
||||
int *pages, size_t *out_len, char *err, size_t err_sz) {
|
||||
*pages = 0;
|
||||
*out_len = n;
|
||||
if (cst_attach_sniff(data, n, mime, mime_sz) == 0) {
|
||||
if (n > CST_ATTACH_MAX_BYTES) {
|
||||
snprintf(err, err_sz, "image is %.1f MB; the limit is %d MB",
|
||||
(double)n / (1024 * 1024), (int)(CST_ATTACH_MAX_BYTES >> 20));
|
||||
return NULL;
|
||||
}
|
||||
char *copy = memdup_z(data, n);
|
||||
if (!copy)
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return copy;
|
||||
}
|
||||
if (n >= 4 && memcmp(data, "%PDF", 4) == 0) {
|
||||
int pg = 0;
|
||||
char perr[256];
|
||||
char *text = cst_pdf_text(data, n, &pg, perr, sizeof(perr));
|
||||
if (!text) {
|
||||
snprintf(err, err_sz, "cannot read the PDF: %.200s", perr);
|
||||
return NULL;
|
||||
}
|
||||
snprintf(mime, mime_sz, "application/pdf");
|
||||
*pages = pg;
|
||||
*out_len = strlen(text);
|
||||
return text;
|
||||
}
|
||||
if (n > CST_ATTACH_TEXT_MAX) {
|
||||
snprintf(err, err_sz, "text is %.1f MB; the limit is %d MB",
|
||||
(double)n / (1024 * 1024), (int)(CST_ATTACH_TEXT_MAX >> 20));
|
||||
return NULL;
|
||||
}
|
||||
if (memchr(data, 0, n) || !cst_utf8_valid(data, n)) {
|
||||
snprintf(err, err_sz,
|
||||
"not an image (PNG, JPEG, GIF, WebP or BMP), a PDF or UTF-8 "
|
||||
"text; the content decides, not the extension");
|
||||
return NULL;
|
||||
}
|
||||
snprintf(mime, mime_sz, "text/plain");
|
||||
char *copy = memdup_z(data, n);
|
||||
if (!copy)
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return copy;
|
||||
}
|
||||
|
||||
/* Write one payload whose kind is already known. */
|
||||
static cst_attachment_t *store(const char *session_path, const char *name,
|
||||
const char *data, size_t n, const char *origin,
|
||||
char *err, size_t err_sz) {
|
||||
const char *data, size_t n, const char *mime,
|
||||
int pages, const char *origin, char *err,
|
||||
size_t err_sz) {
|
||||
if (!name || !name[0])
|
||||
name = "image";
|
||||
char mime[32];
|
||||
if (cst_attach_sniff(data, n, mime, sizeof(mime)) != 0) {
|
||||
snprintf(err, err_sz,
|
||||
"not a supported image (PNG, JPEG, GIF, WebP or BMP; "
|
||||
"checked by content, not by extension)");
|
||||
return NULL;
|
||||
}
|
||||
if (n > CST_ATTACH_MAX_BYTES) {
|
||||
snprintf(err, err_sz, "image is %.1f MB; the limit is %d MB",
|
||||
(double)n / (1024 * 1024), (int)(CST_ATTACH_MAX_BYTES >> 20));
|
||||
return NULL;
|
||||
}
|
||||
char dir[OS_PATH_MAX];
|
||||
if (cst_attach_dir(session_path, dir, sizeof(dir)) != 0) {
|
||||
snprintf(err, err_sz, "cannot create the attachment directory");
|
||||
@@ -386,7 +440,9 @@ static cst_attachment_t *store(const char *session_path, const char *name,
|
||||
snprintf(a->origin, sizeof(a->origin), "%s", origin ? origin : "user");
|
||||
snprintf(a->detail, sizeof(a->detail), "auto");
|
||||
a->bytes = (long long)n;
|
||||
cst_attach_dimensions(data, n, &a->width, &a->height);
|
||||
a->pages = pages;
|
||||
if (cst_attach_is_image(a))
|
||||
cst_attach_dimensions(data, n, &a->width, &a->height);
|
||||
|
||||
char full[OS_PATH_MAX];
|
||||
if (cst_attach_full_path(session_path, a, full, sizeof(full)) != 0) {
|
||||
@@ -419,14 +475,26 @@ cst_attachment_t *cst_attach_import_bytes(const char *session_path,
|
||||
}
|
||||
err[0] = 0;
|
||||
if (!session_path || !session_path[0]) {
|
||||
snprintf(err, err_sz, "no session to store the image in");
|
||||
snprintf(err, err_sz, "no session to store the file in");
|
||||
return NULL;
|
||||
}
|
||||
if (!data || !n) {
|
||||
snprintf(err, err_sz, "image is empty");
|
||||
snprintf(err, err_sz, "the file is empty");
|
||||
return NULL;
|
||||
}
|
||||
return store(session_path, name, data, n, "user", err, err_sz);
|
||||
char mime[32];
|
||||
if (cst_attach_sniff(data, n, mime, sizeof(mime)) != 0) {
|
||||
snprintf(err, err_sz,
|
||||
"not a supported image (PNG, JPEG, GIF, WebP or BMP; "
|
||||
"checked by content, not by extension)");
|
||||
return NULL;
|
||||
}
|
||||
if (n > CST_ATTACH_MAX_BYTES) {
|
||||
snprintf(err, err_sz, "image is %.1f MB; the limit is %d MB",
|
||||
(double)n / (1024 * 1024), (int)(CST_ATTACH_MAX_BYTES >> 20));
|
||||
return NULL;
|
||||
}
|
||||
return store(session_path, name, data, n, mime, 0, "user", err, err_sz);
|
||||
}
|
||||
|
||||
cst_attachment_t *cst_attach_import_file(const char *session_path,
|
||||
@@ -449,11 +517,30 @@ cst_attachment_t *cst_attach_import_file(const char *session_path,
|
||||
for (const char *p = src_path; *p; p++)
|
||||
if (*p == '/' || *p == '\\')
|
||||
leaf = p + 1;
|
||||
cst_attachment_t *a = store(session_path, leaf, data, n, "user", err, err_sz);
|
||||
char mime[32];
|
||||
int pages = 0;
|
||||
size_t pn = 0;
|
||||
char *payload =
|
||||
prepare(data, n, mime, sizeof(mime), &pages, &pn, err, err_sz);
|
||||
free(data);
|
||||
if (!payload)
|
||||
return NULL;
|
||||
cst_attachment_t *a =
|
||||
store(session_path, leaf, payload, pn, mime, pages, "user", err, err_sz);
|
||||
free(payload);
|
||||
return a;
|
||||
}
|
||||
|
||||
/* Images are opaque and travel as image parts; everything else is text the
|
||||
* store holds and the request inlines. */
|
||||
int cst_attach_is_image(const cst_attachment_t *a) {
|
||||
return a && strncmp(a->mime, "image/", 6) == 0;
|
||||
}
|
||||
|
||||
int cst_attach_is_text(const cst_attachment_t *a) {
|
||||
return a && !cst_attach_is_image(a);
|
||||
}
|
||||
|
||||
char *cst_attach_read(const char *session_path, const cst_attachment_t *a,
|
||||
size_t *out_len, char *err, size_t err_sz) {
|
||||
char dummy[8];
|
||||
@@ -470,11 +557,12 @@ char *cst_attach_read(const char *session_path, const cst_attachment_t *a,
|
||||
char *data = read_whole(full, out_len, CST_ATTACH_MAX_BYTES);
|
||||
if (!data) {
|
||||
snprintf(err, err_sz,
|
||||
"%.200s is missing; the image was deleted from the session "
|
||||
"store",
|
||||
"%.200s is missing; it was deleted from the session store",
|
||||
a->name);
|
||||
return NULL;
|
||||
}
|
||||
if (!cst_attach_is_image(a)) /* a document is text, nothing to verify */
|
||||
return data;
|
||||
char mime[32];
|
||||
if (cst_attach_sniff(data, *out_len, mime, sizeof(mime)) != 0) {
|
||||
snprintf(err, err_sz, "%.200s in the session store is no longer an image",
|
||||
@@ -486,6 +574,8 @@ char *cst_attach_read(const char *session_path, const cst_attachment_t *a,
|
||||
}
|
||||
|
||||
char *cst_attach_data_url(const char *session_path, const cst_attachment_t *a) {
|
||||
if (!cst_attach_is_image(a)) /* a document travels as text, not as a URL */
|
||||
return NULL;
|
||||
char err[256];
|
||||
size_t n = 0;
|
||||
char *data = cst_attach_read(session_path, a, &n, err, sizeof(err));
|
||||
@@ -509,10 +599,21 @@ void cst_attach_describe(const cst_attachment_t *a, char *out, size_t out_sz) {
|
||||
snprintf(out, out_sz, "(none)");
|
||||
return;
|
||||
}
|
||||
const char *kind = strncmp(a->mime, "image/", 6) == 0 ? a->mime + 6 : a->mime;
|
||||
const char *kind = a->mime;
|
||||
char dim[32] = "";
|
||||
if (a->width > 0 && a->height > 0)
|
||||
snprintf(dim, sizeof(dim), "%dx%d ", a->width, a->height);
|
||||
if (cst_attach_is_image(a)) {
|
||||
if (strncmp(a->mime, "image/", 6) == 0)
|
||||
kind = a->mime + 6;
|
||||
if (a->width > 0 && a->height > 0)
|
||||
snprintf(dim, sizeof(dim), "%dx%d ", a->width, a->height);
|
||||
} else if (strcmp(a->mime, "application/pdf") == 0) {
|
||||
kind = "pdf";
|
||||
if (a->pages > 0)
|
||||
snprintf(dim, sizeof(dim), "%d page%s ", a->pages,
|
||||
a->pages == 1 ? "" : "s");
|
||||
} else if (strcmp(a->mime, "text/plain") == 0 || !a->mime[0]) {
|
||||
kind = "text";
|
||||
}
|
||||
out[0] = 0;
|
||||
if (a->bytes >= 1024 * 1024)
|
||||
snprintf(out, out_sz, "%s%s %.1f MB", dim, kind,
|
||||
@@ -580,6 +681,8 @@ char *cst_attachments_to_json(const cst_attachment_t *list, int n) {
|
||||
cJSON_AddNumberToObject(o, "width", a->width);
|
||||
cJSON_AddNumberToObject(o, "height", a->height);
|
||||
}
|
||||
if (a->pages > 0)
|
||||
cJSON_AddNumberToObject(o, "pages", a->pages);
|
||||
if (a->detail[0] && strcmp(a->detail, "auto") != 0)
|
||||
cJSON_AddStringToObject(o, "detail", a->detail);
|
||||
if (a->origin[0])
|
||||
@@ -628,6 +731,8 @@ cst_attachment_t *cst_attachments_from_json(const void *array_json, int *n_out)
|
||||
a->width = cJSON_IsNumber(v) ? v->valueint : 0;
|
||||
v = cJSON_GetObjectItemCaseSensitive(it, "height");
|
||||
a->height = cJSON_IsNumber(v) ? v->valueint : 0;
|
||||
v = cJSON_GetObjectItemCaseSensitive(it, "pages");
|
||||
a->pages = cJSON_IsNumber(v) ? v->valueint : 0;
|
||||
v = cJSON_GetObjectItemCaseSensitive(it, "detail");
|
||||
snprintf(a->detail, sizeof(a->detail), "%s",
|
||||
cJSON_IsString(v) ? v->valuestring : "auto");
|
||||
|
||||
+169
@@ -390,6 +390,175 @@ int cst_clipboard_image(char **data, size_t *n, char *err, size_t err_sz) {
|
||||
|
||||
#endif
|
||||
|
||||
/* --- text clipboard ------------------------------------------------------ */
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
int cst_clipboard_text(char **data, size_t *n, char *err, size_t err_sz) {
|
||||
*data = NULL;
|
||||
*n = 0;
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) || !OpenClipboard(NULL)) {
|
||||
set_err(err, err_sz, "no text on the clipboard");
|
||||
return -1;
|
||||
}
|
||||
HANDLE h = GetClipboardData(CF_UNICODETEXT);
|
||||
const wchar_t *w = h ? GlobalLock(h) : NULL;
|
||||
if (!w) {
|
||||
if (h)
|
||||
GlobalUnlock(h);
|
||||
CloseClipboard();
|
||||
set_err(err, err_sz, "no text on the clipboard");
|
||||
return -1;
|
||||
}
|
||||
char *utf8 = os_wide_to_utf8(w);
|
||||
size_t len = utf8 ? strlen(utf8) : 0;
|
||||
GlobalUnlock(h);
|
||||
CloseClipboard();
|
||||
if (!utf8 || !len) {
|
||||
free(utf8);
|
||||
set_err(err, err_sz, "no text on the clipboard");
|
||||
return -1;
|
||||
}
|
||||
if (len > CST_CLIPBOARD_TEXT_MAX) {
|
||||
free(utf8);
|
||||
set_err(err, err_sz, "clipboard text is too large");
|
||||
return -1;
|
||||
}
|
||||
*data = utf8;
|
||||
*n = len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_clipboard_set_text(const char *text, size_t n, char *err,
|
||||
size_t err_sz) {
|
||||
if (!text || os_clipboard_set(text, n) != 0) {
|
||||
set_err(err, err_sz, "could not put text on the clipboard");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* stdout of a clipboard reader, capped and NUL-terminated. */
|
||||
static int clip_read(const char *cmd, const char *tool, char **data, size_t *n,
|
||||
char *err, size_t err_sz) {
|
||||
os_run_result_t r;
|
||||
memset(&r, 0, sizeof(r));
|
||||
if (os_run_shell_c(NULL, cmd, CST_CLIPBOARD_TEXT_MAX + 1, &r) != 0) {
|
||||
os_run_result_free(&r);
|
||||
set_err(err, err_sz, "could not read the clipboard");
|
||||
return -1;
|
||||
}
|
||||
if (r.exit_code != 0 || r.out_len == 0) {
|
||||
int missing = r.exit_code == 127;
|
||||
os_run_result_free(&r);
|
||||
if (missing) {
|
||||
char msg[96];
|
||||
snprintf(msg, sizeof(msg), "reading the clipboard needs %s", tool);
|
||||
set_err(err, err_sz, msg);
|
||||
} else {
|
||||
set_err(err, err_sz, "no text on the clipboard");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if (r.out_len > CST_CLIPBOARD_TEXT_MAX) {
|
||||
os_run_result_free(&r);
|
||||
set_err(err, err_sz, "clipboard text is too large");
|
||||
return -1;
|
||||
}
|
||||
char *copy = malloc(r.out_len + 1);
|
||||
if (!copy) {
|
||||
os_run_result_free(&r);
|
||||
set_err(err, err_sz, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
memcpy(copy, r.out, r.out_len);
|
||||
copy[r.out_len] = 0;
|
||||
os_run_result_free(&r);
|
||||
*data = copy;
|
||||
*n = r.out_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_clipboard_text(char **data, size_t *n, char *err, size_t err_sz) {
|
||||
*data = NULL;
|
||||
*n = 0;
|
||||
#ifdef __APPLE__
|
||||
return clip_read("pbpaste 2>/dev/null", "pbpaste", data, n, err, err_sz);
|
||||
#else
|
||||
const char *wl = getenv("WAYLAND_DISPLAY");
|
||||
if (wl && *wl)
|
||||
return clip_read("wl-paste --type text/plain --no-newline 2>/dev/null",
|
||||
"wl-clipboard (wl-paste)", data, n, err, err_sz);
|
||||
const char *x = getenv("DISPLAY");
|
||||
if (x && *x)
|
||||
return clip_read("xclip -selection clipboard -o 2>/dev/null", "xclip",
|
||||
data, n, err, err_sz);
|
||||
set_err(err, err_sz, "no clipboard here (no Wayland or X11 display)");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int cst_clipboard_set_text(const char *text, size_t n, char *err,
|
||||
size_t err_sz) {
|
||||
if (!text)
|
||||
return -1;
|
||||
if (n > CST_CLIPBOARD_TEXT_MAX) {
|
||||
set_err(err, err_sz, "text is too large for the clipboard");
|
||||
return -1;
|
||||
}
|
||||
const char *fmt = NULL, *tool = NULL;
|
||||
#ifdef __APPLE__
|
||||
fmt = "pbcopy < '%s'";
|
||||
tool = "pbcopy";
|
||||
#else
|
||||
if (getenv("WAYLAND_DISPLAY")) {
|
||||
fmt = "wl-copy < '%s'";
|
||||
tool = "wl-clipboard (wl-copy)";
|
||||
} else if (getenv("DISPLAY")) {
|
||||
fmt = "xclip -selection clipboard -i < '%s'";
|
||||
tool = "xclip";
|
||||
}
|
||||
#endif
|
||||
if (!fmt) {
|
||||
set_err(err, err_sz, "no clipboard here (no Wayland or X11 display)");
|
||||
return -1;
|
||||
}
|
||||
char tmp[OS_PATH_MAX];
|
||||
int fd = os_mkstemp(tmp, sizeof(tmp), "cst-clip");
|
||||
if (fd < 0) {
|
||||
set_err(err, err_sz, "could not create a temporary file");
|
||||
return -1;
|
||||
}
|
||||
FILE *fp = fdopen(fd, "wb");
|
||||
int ok = fp && fwrite(text, 1, n, fp) == n && fclose(fp) == 0;
|
||||
if (!ok) {
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
remove(tmp);
|
||||
set_err(err, err_sz, "could not write the text");
|
||||
return -1;
|
||||
}
|
||||
char cmd[OS_PATH_MAX + 64];
|
||||
snprintf(cmd, sizeof(cmd), fmt, tmp);
|
||||
os_run_result_t r;
|
||||
memset(&r, 0, sizeof(r));
|
||||
int rc = os_run_shell_c(NULL, cmd, 4096, &r);
|
||||
int code = r.exit_code;
|
||||
os_run_result_free(&r);
|
||||
remove(tmp);
|
||||
if (rc != 0 || code != 0) {
|
||||
char msg[96];
|
||||
snprintf(msg, sizeof(msg), "writing the clipboard needs %s", tool);
|
||||
set_err(err, err_sz, msg);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int hexval(int c) {
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
|
||||
+770
@@ -0,0 +1,770 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_http.h"
|
||||
#include "cst_util.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define HTTP_HDR_MAX 32768 /* one header block */
|
||||
#define HTTP_LINE_MAX 8192 /* one header line */
|
||||
#define HTTP_DEFAULT_BODY (1024 * 1024)
|
||||
#define HTTP_DEFAULT_CONN 32
|
||||
#define HTTP_IDLE_TIMEOUT 60000
|
||||
#define HTTP_READ_TIMEOUT 15000
|
||||
|
||||
struct cst_http_server {
|
||||
os_socket_t listener;
|
||||
int port;
|
||||
pthread_t accept_th;
|
||||
int accept_started;
|
||||
pthread_mutex_t mu;
|
||||
pthread_cond_t cv;
|
||||
int stopping;
|
||||
int nconn;
|
||||
cst_http_conn_t *conns_first;
|
||||
cst_http_opts_t o;
|
||||
cst_http_route_t *routes;
|
||||
int nroutes;
|
||||
};
|
||||
|
||||
struct cst_http_conn {
|
||||
cst_http_server_t *srv;
|
||||
os_socket_t sock;
|
||||
cst_http_req_t req;
|
||||
char rbuf[8192];
|
||||
size_t rlen, rpos;
|
||||
char *body; /* owned while the request is served */
|
||||
char hdrs[HTTP_HDR_MAX]; /* header values are copied here */
|
||||
size_t hdrs_used;
|
||||
int sent;
|
||||
int streaming;
|
||||
int failed;
|
||||
int nreq; /* requests served on this connection */
|
||||
int expect_continue; /* the client waits for "100 Continue" */
|
||||
int keep_alive_req; /* what the client asked for */
|
||||
cst_http_conn_t *prev, *next;
|
||||
};
|
||||
|
||||
static const char *status_text(int code) {
|
||||
switch (code) {
|
||||
case 200: return "OK";
|
||||
case 201: return "Created";
|
||||
case 202: return "Accepted";
|
||||
case 204: return "No Content";
|
||||
case 302: return "Found";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 405: return "Method Not Allowed";
|
||||
case 409: return "Conflict";
|
||||
case 411: return "Length Required";
|
||||
case 413: return "Payload Too Large";
|
||||
case 415: return "Unsupported Media Type";
|
||||
case 429: return "Too Many Requests";
|
||||
case 500: return "Internal Server Error";
|
||||
case 503: return "Service Unavailable";
|
||||
default: return "Error";
|
||||
}
|
||||
}
|
||||
|
||||
/* --- reading -------------------------------------------------------------- */
|
||||
|
||||
/* deadline: os_now_ms() value; the whole read must be done by then, so a
|
||||
* client trickling one byte at a time cannot hold the thread forever. */
|
||||
static int fill(cst_http_conn_t *c, double deadline) {
|
||||
if (c->rpos < c->rlen)
|
||||
return 1;
|
||||
double left = deadline - os_now_ms();
|
||||
if (left <= 0)
|
||||
return -2;
|
||||
long r = os_net_recv(c->sock, c->rbuf, sizeof(c->rbuf), (int)left + 1);
|
||||
if (r <= 0)
|
||||
return (int)r; /* 0 eof, -1 error, -2 timeout */
|
||||
c->rpos = 0;
|
||||
c->rlen = (size_t)r;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define RD_TOO_LONG (-3)
|
||||
|
||||
static int rd_line(cst_http_conn_t *c, char *out, size_t cap, double deadline) {
|
||||
size_t n = 0;
|
||||
for (;;) {
|
||||
int rc = fill(c, deadline);
|
||||
if (rc <= 0)
|
||||
return rc;
|
||||
while (c->rpos < c->rlen) {
|
||||
char ch = c->rbuf[c->rpos++];
|
||||
if (ch == '\n') {
|
||||
if (n && out[n - 1] == '\r')
|
||||
n--;
|
||||
out[n] = 0;
|
||||
return 1;
|
||||
}
|
||||
if (n + 2 >= cap)
|
||||
return RD_TOO_LONG;
|
||||
out[n++] = ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Exactly n bytes, using what the read buffer already holds. malloc'd. */
|
||||
static char *rd_body(cst_http_conn_t *c, size_t n, int timeout_ms) {
|
||||
char *buf = malloc(n + 1);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
size_t got = 0;
|
||||
while (got < n) {
|
||||
if (c->rpos < c->rlen) {
|
||||
size_t take = c->rlen - c->rpos;
|
||||
if (take > n - got)
|
||||
take = n - got;
|
||||
memcpy(buf + got, c->rbuf + c->rpos, take);
|
||||
c->rpos += take;
|
||||
got += take;
|
||||
continue;
|
||||
}
|
||||
long r = os_net_recv(c->sock, c->rbuf, sizeof(c->rbuf), timeout_ms);
|
||||
if (r <= 0) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
c->rpos = 0;
|
||||
c->rlen = (size_t)r;
|
||||
}
|
||||
buf[n] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* -1 when the input encodes a NUL (out then holds what came before it). */
|
||||
static int url_decode(const char *in, char *out, size_t cap, int plus_space) {
|
||||
size_t o = 0;
|
||||
for (const char *p = in; *p && o + 1 < cap; p++) {
|
||||
if (*p == '%' && p[1] && p[2]) {
|
||||
int hi = p[1] >= '0' && p[1] <= '9' ? p[1] - '0'
|
||||
: p[1] >= 'a' && p[1] <= 'f' ? p[1] - 'a' + 10
|
||||
: p[1] >= 'A' && p[1] <= 'F' ? p[1] - 'A' + 10
|
||||
: -1;
|
||||
int lo = p[2] >= '0' && p[2] <= '9' ? p[2] - '0'
|
||||
: p[2] >= 'a' && p[2] <= 'f' ? p[2] - 'a' + 10
|
||||
: p[2] >= 'A' && p[2] <= 'F' ? p[2] - 'A' + 10
|
||||
: -1;
|
||||
if (hi >= 0 && lo >= 0) {
|
||||
char ch = (char)((hi << 4) | lo);
|
||||
if (ch == 0) {
|
||||
out[o] = 0;
|
||||
return -1;
|
||||
}
|
||||
out[o++] = ch;
|
||||
p += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out[o++] = (*p == '+' && plus_space) ? ' ' : *p;
|
||||
}
|
||||
out[o] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Comma-separated header tokens, compared without case ("Close"). */
|
||||
static int has_token(const char *val, const char *tok) {
|
||||
size_t n = strlen(tok);
|
||||
for (const char *p = val; p && *p;) {
|
||||
while (*p == ' ' || *p == '\t' || *p == ',')
|
||||
p++;
|
||||
size_t i = 0;
|
||||
while (i < n && p[i] && (p[i] | 0x20) == (tok[i] | 0x20))
|
||||
i++;
|
||||
if (i == n && (p[i] == 0 || p[i] == ',' || p[i] == ' ' || p[i] == '\t'))
|
||||
return 1;
|
||||
p = strchr(p, ',');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_is(const char *line, const char *key) {
|
||||
size_t n = strlen(key);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char a = line[i], b = key[i];
|
||||
if (a >= 'A' && a <= 'Z')
|
||||
a = (char)(a - 'A' + 'a');
|
||||
if (b >= 'A' && b <= 'Z')
|
||||
b = (char)(b - 'A' + 'a');
|
||||
if (a != b || !a)
|
||||
return 0;
|
||||
}
|
||||
return line[n] == ':';
|
||||
}
|
||||
|
||||
/* Copy a header value into the connection (they must not point into the
|
||||
* line buffer, which is reused for the next header). */
|
||||
static const char *store_str(cst_http_conn_t *c, const char *val) {
|
||||
size_t n = strlen(val);
|
||||
if (c->hdrs_used + n + 1 > sizeof(c->hdrs))
|
||||
return NULL;
|
||||
char *dst = c->hdrs + c->hdrs_used;
|
||||
memcpy(dst, val, n + 1);
|
||||
c->hdrs_used += n + 1;
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* 1 = parsed, 0 = the client went away or waited too long, -1 = malformed. */
|
||||
static int read_request(cst_http_conn_t *c, size_t *want_body) {
|
||||
cst_http_server_t *s = c->srv;
|
||||
char line[HTTP_LINE_MAX];
|
||||
cst_http_req_t *r = &c->req;
|
||||
int first = c->nreq == 0; /* a keep-alive pause gets the idle timeout */
|
||||
memset(r, 0, sizeof(*r));
|
||||
c->hdrs_used = 0;
|
||||
c->expect_continue = 0;
|
||||
*want_body = 0;
|
||||
int rc = rd_line(c, line, sizeof(line),
|
||||
os_now_ms() + (first ? s->o.read_timeout_ms
|
||||
: s->o.idle_timeout_ms));
|
||||
if (rc == RD_TOO_LONG)
|
||||
return -1;
|
||||
if (rc <= 0)
|
||||
return 0;
|
||||
char *sp1 = strchr(line, ' ');
|
||||
if (!sp1)
|
||||
return -1;
|
||||
*sp1 = 0;
|
||||
char *target = sp1 + 1;
|
||||
char *version = strchr(target, ' ');
|
||||
if (version) {
|
||||
*version++ = 0;
|
||||
} else {
|
||||
version = (char *)"HTTP/1.0";
|
||||
}
|
||||
if (!line[0] || strlen(line) >= sizeof(r->method))
|
||||
return -1;
|
||||
snprintf(r->method, sizeof(r->method), "%s", line);
|
||||
r->keep_alive = strncmp(version, "HTTP/1.", 7) == 0 &&
|
||||
version[7] >= '1' && version[7] <= '9';
|
||||
/* absolute-form ("http://host/path") is legal: keep the path */
|
||||
if (target[0] != '/') {
|
||||
char *scheme = strstr(target, "://");
|
||||
target = scheme ? strchr(scheme + 3, '/') : NULL;
|
||||
}
|
||||
if (!target || target[0] != '/')
|
||||
return -1;
|
||||
{
|
||||
char raw[sizeof(r->path)];
|
||||
snprintf(raw, sizeof(raw), "%s", target);
|
||||
char *q = strchr(raw, '?');
|
||||
if (q) {
|
||||
*q = 0;
|
||||
snprintf(r->query, sizeof(r->query), "%s", q + 1);
|
||||
}
|
||||
if (url_decode(raw, r->path, sizeof(r->path), 0) != 0)
|
||||
return -1;
|
||||
size_t plen = strlen(r->path);
|
||||
if (plen == 0 || strstr(r->path, ".."))
|
||||
return -1;
|
||||
}
|
||||
size_t hdr_bytes = 0;
|
||||
size_t content_length = 0;
|
||||
int have_length = 0;
|
||||
double deadline = os_now_ms() + s->o.read_timeout_ms;
|
||||
for (;;) {
|
||||
rc = rd_line(c, line, sizeof(line), deadline);
|
||||
if (rc <= 0)
|
||||
return -1;
|
||||
if (!line[0])
|
||||
break;
|
||||
hdr_bytes += strlen(line) + 2;
|
||||
if (hdr_bytes > HTTP_HDR_MAX)
|
||||
return -1;
|
||||
const char *colon = strchr(line, ':');
|
||||
if (!colon)
|
||||
return -1;
|
||||
const char *val = colon + 1;
|
||||
while (*val == ' ' || *val == '\t')
|
||||
val++;
|
||||
if (key_is(line, "content-length")) {
|
||||
/* digits only, and one value: "5x" or two lengths would let the
|
||||
* rest of the body run as a second request behind a proxy */
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
unsigned long long v = strtoull(val, &end, 10);
|
||||
while (end && (*end == ' ' || *end == '\t'))
|
||||
end++;
|
||||
if (*val < '0' || *val > '9' || !end || *end || errno == ERANGE ||
|
||||
v > (unsigned long long)SIZE_MAX ||
|
||||
(have_length && content_length != (size_t)v))
|
||||
return -1;
|
||||
content_length = (size_t)v;
|
||||
have_length = 1;
|
||||
} else if (key_is(line, "transfer-encoding")) {
|
||||
return -1; /* chunked request bodies are not supported */
|
||||
} else if (key_is(line, "connection")) {
|
||||
if (has_token(val, "close"))
|
||||
r->keep_alive = 0;
|
||||
else if (has_token(val, "keep-alive"))
|
||||
r->keep_alive = 1;
|
||||
} else if (key_is(line, "expect")) {
|
||||
/* answered once auth and the size cap have passed */
|
||||
c->expect_continue = has_token(val, "100-continue");
|
||||
} else if (key_is(line, "authorization")) {
|
||||
r->authorization = store_str(c, val);
|
||||
} else if (key_is(line, "cookie")) {
|
||||
r->cookie = store_str(c, val);
|
||||
} else if (key_is(line, "origin")) {
|
||||
r->origin = store_str(c, val);
|
||||
} else if (key_is(line, "host")) {
|
||||
r->host = store_str(c, val);
|
||||
} else if (key_is(line, "content-type")) {
|
||||
r->content_type = store_str(c, val);
|
||||
} else if (key_is(line, "user-agent")) {
|
||||
r->user_agent = store_str(c, val);
|
||||
} else if (key_is(line, "last-event-id")) {
|
||||
r->last_event_id = store_str(c, val);
|
||||
}
|
||||
}
|
||||
if (have_length)
|
||||
*want_body = content_length;
|
||||
else if (strcmp(r->method, "POST") == 0)
|
||||
/* No Content-Length means no body (RFC 9112 6.3): curl -X POST sends
|
||||
* exactly that, and the zero-body endpoints (cancel, undo, quit) would
|
||||
* otherwise be unreachable from a hand-written client. Bytes that
|
||||
* follow are not ours to guess at, so the connection ends here. */
|
||||
r->keep_alive = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* --- writing -------------------------------------------------------------- */
|
||||
|
||||
static int send_head(cst_http_conn_t *c, int status, const char *ctype,
|
||||
size_t len, int chunked) {
|
||||
cst_buf_t out = {0};
|
||||
cst_buf_printf(&out, "HTTP/1.1 %d %s\r\nContent-Type: %s\r\n",
|
||||
status, status_text(status), ctype ? ctype : "text/plain");
|
||||
cst_buf_puts(&out, "Cache-Control: no-store\r\n");
|
||||
cst_buf_puts(&out, c->req.keep_alive ? "Connection: keep-alive\r\n"
|
||||
: "Connection: close\r\n");
|
||||
if (chunked) {
|
||||
cst_buf_puts(&out, "Transfer-Encoding: chunked\r\n");
|
||||
} else {
|
||||
cst_buf_printf(&out, "Content-Length: %zu\r\n", len);
|
||||
}
|
||||
cst_buf_puts(&out, "\r\n");
|
||||
int rc = out.buf ? os_net_send_all(c->sock, out.buf, out.len, 0) : -1;
|
||||
cst_buf_free(&out);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_send(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const void *body, size_t n) {
|
||||
if (!c || c->sent)
|
||||
return -1;
|
||||
c->sent = 1;
|
||||
if (send_head(c, status, ctype, n, 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
if (n && os_net_send_all(c->sock, body, n, 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_http_send_str(cst_http_conn_t *c, int status, const char *ctype,
|
||||
const char *s) {
|
||||
return cst_http_send(c, status, ctype, s ? s : "", s ? strlen(s) : 0);
|
||||
}
|
||||
|
||||
int cst_http_send_json(cst_http_conn_t *c, int status, const char *json) {
|
||||
return cst_http_send_str(c, status, "application/json; charset=utf-8",
|
||||
json ? json : "null");
|
||||
}
|
||||
|
||||
int cst_http_send_err(cst_http_conn_t *c, int status, const char *msg) {
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, "{\"error\":\"");
|
||||
for (const char *p = msg ? msg : "error"; *p; p++) {
|
||||
if (*p == '"' || *p == '\\')
|
||||
cst_buf_printf(&b, "\\%c", *p);
|
||||
else if ((unsigned char)*p < 0x20)
|
||||
cst_buf_printf(&b, "\\u%04x", (unsigned char)*p);
|
||||
else
|
||||
cst_buf_append(&b, p, 1);
|
||||
}
|
||||
cst_buf_puts(&b, "\"}");
|
||||
int rc = cst_http_send_json(c, status, b.buf);
|
||||
cst_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_sent(const cst_http_conn_t *c) { return c ? c->sent : 0; }
|
||||
|
||||
int cst_http_stream_begin(cst_http_conn_t *c, const char *ctype) {
|
||||
if (!c || c->sent)
|
||||
return -1;
|
||||
c->sent = 1;
|
||||
c->streaming = 1;
|
||||
if (send_head(c, 200, ctype, 0, 1) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cst_http_stream_write(cst_http_conn_t *c, const void *data, size_t n) {
|
||||
if (!c || !c->streaming || c->failed)
|
||||
return -1;
|
||||
if (n == 0)
|
||||
return 0;
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "%zx\r\n", n);
|
||||
cst_buf_append(&b, data, n);
|
||||
cst_buf_puts(&b, "\r\n");
|
||||
int rc = b.buf ? os_net_send_all(c->sock, b.buf, b.len, 0) : -1;
|
||||
cst_buf_free(&b);
|
||||
if (rc != 0)
|
||||
c->failed = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_http_stream_end(cst_http_conn_t *c) {
|
||||
if (!c || !c->streaming)
|
||||
return -1;
|
||||
c->streaming = 0;
|
||||
if (c->failed)
|
||||
return -1;
|
||||
const char *end = "0\r\n\r\n";
|
||||
if (os_net_send_all(c->sock, end, strlen(end), 0) != 0) {
|
||||
c->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const cst_http_req_t *cst_http_request(cst_http_conn_t *c) {
|
||||
return c ? &c->req : NULL;
|
||||
}
|
||||
|
||||
int cst_http_query(const cst_http_req_t *req, const char *key, char *out,
|
||||
size_t out_sz) {
|
||||
if (!req || !key || !out || out_sz == 0)
|
||||
return -1;
|
||||
size_t klen = strlen(key);
|
||||
const char *p = req->query;
|
||||
while (p && *p) {
|
||||
const char *amp = strchr(p, '&');
|
||||
size_t seg = amp ? (size_t)(amp - p) : strlen(p);
|
||||
const char *eq = memchr(p, '=', seg);
|
||||
size_t name_len = eq ? (size_t)(eq - p) : seg;
|
||||
if (name_len == klen && strncmp(p, key, klen) == 0) {
|
||||
char raw[1024];
|
||||
size_t vlen = eq ? seg - name_len - 1 : 0;
|
||||
if (vlen >= sizeof(raw))
|
||||
vlen = sizeof(raw) - 1;
|
||||
if (eq)
|
||||
memcpy(raw, eq + 1, vlen);
|
||||
raw[vlen] = 0;
|
||||
url_decode(raw, out, out_sz, 1);
|
||||
return 0;
|
||||
}
|
||||
p = amp ? amp + 1 : NULL;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* --- connections ---------------------------------------------------------- */
|
||||
|
||||
static const cst_http_route_t *find_route(const cst_http_server_t *s,
|
||||
const cst_http_req_t *req) {
|
||||
const cst_http_route_t *fallback = NULL;
|
||||
for (int i = 0; i < s->nroutes; i++) {
|
||||
const cst_http_route_t *r = &s->routes[i];
|
||||
if (strcmp(r->method, "*") != 0 && strcmp(r->method, req->method) != 0)
|
||||
continue;
|
||||
if (strcmp(r->path, "*") == 0) {
|
||||
fallback = r;
|
||||
continue;
|
||||
}
|
||||
if (strcmp(r->path, req->path) == 0)
|
||||
return r;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
/* Route, auth and the size cap are settled before the body is read: an
|
||||
* unauthenticated client must not make us buffer 32 MiB. */
|
||||
static void serve(cst_http_server_t *s, cst_http_conn_t *c, size_t want) {
|
||||
const cst_http_route_t *route = find_route(s, &c->req);
|
||||
size_t cap = route && route->max_body ? route->max_body : s->o.max_body;
|
||||
if (want > 0)
|
||||
c->req.keep_alive = 0; /* until the body is read, the stream is ours */
|
||||
if (!route) {
|
||||
cst_http_send_err(c, 404, "not found");
|
||||
return;
|
||||
}
|
||||
if (route->auth && s->o.auth && s->o.auth(&c->req, s->o.auth_ud) != 0) {
|
||||
cst_http_send_err(c, 401, "unauthorized");
|
||||
return;
|
||||
}
|
||||
if (want > cap) {
|
||||
cst_http_send_err(c, 413, "request body too large");
|
||||
return;
|
||||
}
|
||||
if (want > 0) {
|
||||
if (c->expect_continue) {
|
||||
const char *cont = "HTTP/1.1 100 Continue\r\n\r\n";
|
||||
os_net_send_all(c->sock, cont, strlen(cont), 5000);
|
||||
}
|
||||
char *body = rd_body(c, want, s->o.read_timeout_ms);
|
||||
if (!body) {
|
||||
c->failed = 1;
|
||||
return;
|
||||
}
|
||||
c->body = body;
|
||||
c->req.body = body;
|
||||
c->req.body_len = want;
|
||||
c->req.keep_alive = c->keep_alive_req;
|
||||
}
|
||||
int rc = route->fn(c, route->ud);
|
||||
if (!c->sent)
|
||||
cst_http_send_err(c, rc == 0 ? 500 : 404,
|
||||
rc == 0 ? "handler sent no response" : "not found");
|
||||
}
|
||||
|
||||
static void *conn_main(void *arg) {
|
||||
cst_http_conn_t *c = arg;
|
||||
cst_http_server_t *s = c->srv;
|
||||
for (;;) {
|
||||
size_t want = 0;
|
||||
int rc = read_request(c, &want);
|
||||
if (rc < 0) {
|
||||
/* malformed (bad request line, traversal, unsupported transfer
|
||||
* encoding): say so instead of closing silently */
|
||||
c->sent = 0;
|
||||
c->req.keep_alive = 0;
|
||||
cst_http_send_err(c, 400, "malformed request");
|
||||
break;
|
||||
}
|
||||
if (rc == 0)
|
||||
break;
|
||||
c->nreq++;
|
||||
c->sent = 0;
|
||||
c->streaming = 0;
|
||||
c->keep_alive_req = c->req.keep_alive;
|
||||
serve(s, c, want);
|
||||
if (s->o.on_log) {
|
||||
char line[512];
|
||||
snprintf(line, sizeof(line), "%s %.480s -> %d", c->req.method, c->req.path,
|
||||
c->failed ? 499 : (c->sent ? 200 : 500));
|
||||
s->o.on_log(line, s->o.log_ud);
|
||||
}
|
||||
free(c->body);
|
||||
c->body = NULL;
|
||||
c->req.body = NULL;
|
||||
c->req.body_len = 0;
|
||||
c->req.authorization = c->req.cookie = c->req.origin = NULL;
|
||||
c->req.host = c->req.content_type = c->req.user_agent = NULL;
|
||||
c->req.last_event_id = NULL;
|
||||
if (!c->req.keep_alive || s->stopping || c->failed)
|
||||
break;
|
||||
}
|
||||
/* unlink first: stop() shuts down every listed socket, and a closed fd
|
||||
* number may already belong to someone else */
|
||||
pthread_mutex_lock(&s->mu);
|
||||
if (c->prev)
|
||||
c->prev->next = c->next;
|
||||
else
|
||||
s->conns_first = c->next;
|
||||
if (c->next)
|
||||
c->next->prev = c->prev;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_shutdown(c->sock);
|
||||
os_net_close(c->sock);
|
||||
free(c);
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *accept_main(void *arg) {
|
||||
cst_http_server_t *s = arg;
|
||||
while (!s->stopping) {
|
||||
int timed_out = 0;
|
||||
os_socket_t sock = os_net_accept(s->listener, 500, &timed_out);
|
||||
if (sock == OS_SOCKET_BAD) {
|
||||
if (!timed_out) { /* EMFILE and friends: the listener stays readable */
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_nsec += 100000000;
|
||||
if (ts.tv_nsec >= 1000000000) {
|
||||
ts.tv_sec++;
|
||||
ts.tv_nsec -= 1000000000;
|
||||
}
|
||||
pthread_mutex_lock(&s->mu);
|
||||
if (!s->stopping)
|
||||
pthread_cond_timedwait(&s->cv, &s->mu, &ts);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (s->stopping) {
|
||||
os_net_close(sock);
|
||||
break;
|
||||
}
|
||||
pthread_mutex_lock(&s->mu);
|
||||
int over = s->nconn >= s->o.max_conn;
|
||||
if (!over)
|
||||
s->nconn++;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
if (over) {
|
||||
const char *busy = "HTTP/1.1 503 Service Unavailable\r\n"
|
||||
"Content-Length: 0\r\nConnection: close\r\n\r\n";
|
||||
os_net_send_all(sock, busy, strlen(busy), 2000);
|
||||
os_net_close(sock);
|
||||
continue;
|
||||
}
|
||||
cst_http_conn_t *c = calloc(1, sizeof(*c));
|
||||
if (!c) {
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(sock);
|
||||
continue;
|
||||
}
|
||||
c->srv = s;
|
||||
c->sock = sock;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
c->next = s->conns_first;
|
||||
if (c->next)
|
||||
c->next->prev = c;
|
||||
s->conns_first = c;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
pthread_t th;
|
||||
if (pthread_create(&th, NULL, conn_main, c) != 0) {
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->conns_first = c->next;
|
||||
if (c->next)
|
||||
c->next->prev = NULL;
|
||||
s->nconn--;
|
||||
pthread_cond_broadcast(&s->cv);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(sock);
|
||||
free(c);
|
||||
continue;
|
||||
}
|
||||
pthread_detach(th);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cst_http_server_t *cst_http_start(const cst_http_opts_t *o, char *err,
|
||||
size_t err_sz) {
|
||||
if (!o)
|
||||
return NULL;
|
||||
if (os_net_init() != 0) {
|
||||
if (err)
|
||||
snprintf(err, err_sz, "network init failed");
|
||||
return NULL;
|
||||
}
|
||||
cst_http_server_t *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
s->o = *o;
|
||||
s->o.max_body = o->max_body ? o->max_body : HTTP_DEFAULT_BODY;
|
||||
s->o.max_conn = o->max_conn ? o->max_conn : HTTP_DEFAULT_CONN;
|
||||
if (!s->o.read_timeout_ms)
|
||||
s->o.read_timeout_ms = HTTP_READ_TIMEOUT;
|
||||
if (!s->o.idle_timeout_ms)
|
||||
s->o.idle_timeout_ms = HTTP_IDLE_TIMEOUT;
|
||||
if (o->nroutes > 0) {
|
||||
s->routes = calloc((size_t)o->nroutes, sizeof(*s->routes));
|
||||
if (!s->routes) {
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(s->routes, o->routes, (size_t)o->nroutes * sizeof(*s->routes));
|
||||
s->nroutes = o->nroutes;
|
||||
}
|
||||
pthread_mutex_init(&s->mu, NULL);
|
||||
pthread_cond_init(&s->cv, NULL);
|
||||
s->listener = os_net_listen(o->host, o->port, err, err_sz);
|
||||
if (s->listener == OS_SOCKET_BAD) {
|
||||
free(s->routes);
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
s->port = os_net_port(s->listener);
|
||||
if (pthread_create(&s->accept_th, NULL, accept_main, s) != 0) {
|
||||
os_net_close(s->listener);
|
||||
free(s->routes);
|
||||
free(s);
|
||||
if (err)
|
||||
snprintf(err, err_sz, "cannot start the accept thread");
|
||||
return NULL;
|
||||
}
|
||||
s->accept_started = 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
int cst_http_port(const cst_http_server_t *s) { return s ? s->port : 0; }
|
||||
|
||||
int cst_http_stopping(const cst_http_server_t *s) {
|
||||
return s ? s->stopping : 1;
|
||||
}
|
||||
|
||||
int cst_http_stop(cst_http_server_t *s) {
|
||||
if (!s)
|
||||
return 0;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
s->stopping = 1;
|
||||
for (cst_http_conn_t *c = s->conns_first; c; c = c->next)
|
||||
os_net_shutdown(c->sock);
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_shutdown(s->listener);
|
||||
if (s->accept_started) {
|
||||
pthread_join(s->accept_th, NULL);
|
||||
s->accept_started = 0;
|
||||
}
|
||||
/* Handler threads own their connection until they return; the shutdown
|
||||
* above is what wakes them. Bounded wait so free() stays safe. */
|
||||
pthread_mutex_lock(&s->mu);
|
||||
for (int i = 0; i < 300 && s->nconn > 0; i++) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
ts.tv_nsec += 100000000; /* 100 ms */
|
||||
if (ts.tv_nsec >= 1000000000) {
|
||||
ts.tv_sec++;
|
||||
ts.tv_nsec -= 1000000000;
|
||||
}
|
||||
pthread_cond_timedwait(&s->cv, &s->mu, &ts);
|
||||
}
|
||||
int left = s->nconn;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
os_net_close(s->listener);
|
||||
return left;
|
||||
}
|
||||
|
||||
void cst_http_free(cst_http_server_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
pthread_mutex_lock(&s->mu);
|
||||
int left = s->nconn;
|
||||
pthread_mutex_unlock(&s->mu);
|
||||
if (left > 0)
|
||||
return; /* a handler still runs on it (see stop()): leak, not crash */
|
||||
pthread_mutex_destroy(&s->mu);
|
||||
pthread_cond_destroy(&s->cv);
|
||||
free(s->routes);
|
||||
free(s);
|
||||
}
|
||||
+159
-53
@@ -22,43 +22,75 @@
|
||||
#define ERRBODY_MAX 4096
|
||||
#define LLM_MAX_RETRIES 3
|
||||
|
||||
/* Point libcurl at a CA bundle on Windows.
|
||||
static int ca_file_ok(const char *path) {
|
||||
if (!path || !path[0])
|
||||
return 0;
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f)
|
||||
return 0;
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Point libcurl at a CA bundle.
|
||||
*
|
||||
* An OpenSSL-based libcurl keeps the path it was built with -- MSYS2's is
|
||||
* "/mingw64/etc/ssl/certs/ca-bundle.crt", which resolves to nothing once the
|
||||
* binary runs outside an MSYS2 shell, and that build ignores CURL_CA_BUNDLE.
|
||||
* Every HTTPS request then dies with CURLE_SSL_CACERT_BADFILE (77).
|
||||
* libcurl keeps the bundle path it was built with. On Windows an OpenSSL
|
||||
* build carries MSYS2's "/mingw64/etc/ssl/certs/ca-bundle.crt"; the static
|
||||
* Linux release is built on one distro and runs on all of them, whose bundles
|
||||
* live in different places. A path that does not exist fails every HTTPS
|
||||
* request with CURLE_SSL_CACERT_BADFILE (77).
|
||||
*
|
||||
* Order: $CST_CA_BUNDLE, then ca-bundle.crt beside the executable. If neither
|
||||
* exists nothing is set, so a libcurl linked against Schannel (the Windows
|
||||
* certificate store) behaves exactly as before. */
|
||||
static void llm_apply_ca_bundle(CURL *curl) {
|
||||
#if defined(_WIN32)
|
||||
* Order: $CST_CA_BUNDLE; on Windows ca-bundle.crt beside the executable (if
|
||||
* absent nothing is set, so Schannel uses the certificate store); elsewhere
|
||||
* $SSL_CERT_FILE, libcurl's own default if that file exists, then the usual
|
||||
* distro locations. Decided once per process. */
|
||||
void cst_curl_apply_ca(void *curl) {
|
||||
static char ca[OS_PATH_MAX];
|
||||
static int checked;
|
||||
if (!checked) {
|
||||
checked = 1;
|
||||
static atomic_int state; /* 0 unchecked, 1 checking, 2 done */
|
||||
int expect = 0;
|
||||
if (atomic_compare_exchange_strong(&state, &expect, 1)) {
|
||||
const char *env = getenv("CST_CA_BUNDLE");
|
||||
if (env && env[0]) {
|
||||
if (ca_file_ok(env)) {
|
||||
snprintf(ca, sizeof(ca), "%s", env);
|
||||
} else {
|
||||
char dir[OS_PATH_MAX];
|
||||
if (os_exe_dir(dir, sizeof(dir)) == 0)
|
||||
os_path_join(ca, sizeof(ca), dir, "ca-bundle.crt");
|
||||
}
|
||||
if (ca[0]) {
|
||||
FILE *f = fopen(ca, "rb");
|
||||
if (f)
|
||||
fclose(f);
|
||||
#if defined(_WIN32)
|
||||
char dir[OS_PATH_MAX], p[OS_PATH_MAX];
|
||||
if (os_exe_dir(dir, sizeof(dir)) == 0 &&
|
||||
os_path_join(p, sizeof(p), dir, "ca-bundle.crt") == 0 &&
|
||||
ca_file_ok(p))
|
||||
snprintf(ca, sizeof(ca), "%s", p);
|
||||
#else
|
||||
static const char *const known[] = {
|
||||
"/etc/ssl/certs/ca-certificates.crt", /* Debian, Ubuntu, Arch, Alpine */
|
||||
"/etc/pki/tls/certs/ca-bundle.crt", /* Fedora, RHEL */
|
||||
"/etc/ssl/cert.pem", /* macOS, BSD, Alpine */
|
||||
"/etc/ssl/ca-bundle.pem", /* openSUSE */
|
||||
"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
|
||||
};
|
||||
const char *pick = NULL;
|
||||
const char *sslenv = getenv("SSL_CERT_FILE");
|
||||
const curl_version_info_data *vi = curl_version_info(CURLVERSION_NOW);
|
||||
if (ca_file_ok(sslenv))
|
||||
pick = sslenv;
|
||||
else if (vi && ca_file_ok(vi->cainfo))
|
||||
pick = NULL; /* libcurl's built-in default already works */
|
||||
else
|
||||
ca[0] = '\0';
|
||||
for (size_t i = 0; i < sizeof(known) / sizeof(known[0]); i++)
|
||||
if (ca_file_ok(known[i])) {
|
||||
pick = known[i];
|
||||
break;
|
||||
}
|
||||
if (pick)
|
||||
snprintf(ca, sizeof(ca), "%s", pick);
|
||||
#endif
|
||||
}
|
||||
atomic_store(&state, 2);
|
||||
} else {
|
||||
while (atomic_load(&state) != 2)
|
||||
;
|
||||
}
|
||||
if (ca[0])
|
||||
curl_easy_setopt(curl, CURLOPT_CAINFO, ca);
|
||||
#else
|
||||
(void)curl;
|
||||
#endif
|
||||
curl_easy_setopt((CURL *)curl, CURLOPT_CAINFO, ca);
|
||||
}
|
||||
|
||||
#include "cst_attach.h"
|
||||
@@ -162,7 +194,14 @@ static void parts_add_text(cJSON *parts, cJSON *txt) {
|
||||
cJSON_Delete(txt);
|
||||
return;
|
||||
}
|
||||
cJSON_AddItemToArray(parts, txt);
|
||||
if (!txt->valuestring || !txt->valuestring[0]) {
|
||||
cJSON_Delete(txt);
|
||||
return;
|
||||
}
|
||||
cJSON *t = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(t, "type", "text");
|
||||
cJSON_AddItemToObject(t, "text", txt);
|
||||
cJSON_AddItemToArray(parts, t);
|
||||
}
|
||||
|
||||
/* One stored image as an image_url part, or NULL with why filled in. */
|
||||
@@ -216,28 +255,78 @@ static cJSON *image_part(const cst_llm_request_t *req,
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Append every attachment of m as image parts (or a note saying why not). */
|
||||
static void parts_add_images(const cst_llm_request_t *req, cJSON *parts,
|
||||
const cst_message_t *m, size_t *budget) {
|
||||
/* One stored document as a text part: its text, framed so the model knows
|
||||
* where it came from. Documents are plain text on the wire, so a text-only
|
||||
* model and CST_NO_IMAGES both still get them. */
|
||||
static cJSON *document_part(const cst_llm_request_t *req,
|
||||
const cst_attachment_t *a, size_t *budget,
|
||||
char *why, size_t why_sz) {
|
||||
why[0] = 0;
|
||||
if (!req->session_path || !req->session_path[0]) {
|
||||
snprintf(why, why_sz, "there is no session store to read it from");
|
||||
return NULL;
|
||||
}
|
||||
char err[256];
|
||||
size_t n = 0;
|
||||
char *text = cst_attach_read(req->session_path, a, &n, err, sizeof(err));
|
||||
if (!text) {
|
||||
snprintf(why, why_sz, "%s", err);
|
||||
return NULL;
|
||||
}
|
||||
int cut = 0;
|
||||
if (n > *budget) {
|
||||
n = *budget;
|
||||
cut = 1;
|
||||
}
|
||||
*budget -= n;
|
||||
char desc[128];
|
||||
cst_attach_describe(a, desc, sizeof(desc));
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "[document \"%.120s\" (%s)%s]\n", a->name, desc,
|
||||
cut ? " -- only the start is included" : "");
|
||||
cst_buf_append(&b, text, n);
|
||||
cst_buf_puts(&b, cut ? "\n[-- document truncated --]"
|
||||
: "\n[-- end of document --]");
|
||||
free(text);
|
||||
cJSON *p = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(p, "type", "text");
|
||||
cJSON_AddStringToObject(p, "text", b.buf ? b.buf : "");
|
||||
cst_buf_free(&b);
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Append every attachment of m: images as image parts (or a note saying why
|
||||
* not), documents inline as text. */
|
||||
static void parts_add_attachments(const cst_llm_request_t *req, cJSON *parts,
|
||||
const cst_message_t *m, size_t *budget,
|
||||
size_t *doc_budget, const char *img_off) {
|
||||
int n = m->nattach;
|
||||
if (n > CST_ATTACH_MAX_COUNT) {
|
||||
char note[160];
|
||||
snprintf(note, sizeof(note), "[%d of %d images sent]", CST_ATTACH_MAX_COUNT,
|
||||
n);
|
||||
snprintf(note, sizeof(note),
|
||||
"[only the first %d of %d attachments are sent]",
|
||||
CST_ATTACH_MAX_COUNT, n);
|
||||
parts_add_text(parts, cJSON_CreateString(note));
|
||||
n = CST_ATTACH_MAX_COUNT;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
const cst_attachment_t *a = &m->attach[i];
|
||||
int is_img = cst_attach_is_image(a);
|
||||
char why[256];
|
||||
cJSON *p = image_part(req, a, budget, why, sizeof(why));
|
||||
cJSON *p = NULL;
|
||||
if (is_img && img_off)
|
||||
snprintf(why, sizeof(why), "%s", img_off);
|
||||
else if (is_img)
|
||||
p = image_part(req, a, budget, why, sizeof(why));
|
||||
else
|
||||
p = document_part(req, a, doc_budget, why, sizeof(why));
|
||||
if (p) {
|
||||
cJSON_AddItemToArray(parts, p);
|
||||
continue;
|
||||
}
|
||||
char note[512];
|
||||
snprintf(note, sizeof(note), "[image \"%.120s\" not sent: %.200s]",
|
||||
a->name, why);
|
||||
snprintf(note, sizeof(note), "[%s \"%.120s\" not sent: %.200s]",
|
||||
is_img ? "image" : "document", a->name, why);
|
||||
parts_add_text(parts, cJSON_CreateString(note));
|
||||
}
|
||||
}
|
||||
@@ -245,8 +334,10 @@ static void parts_add_images(const cst_llm_request_t *req, cJSON *parts,
|
||||
/* Images a tool result produced are deferred: an assistant tool_call must be
|
||||
* answered by tool messages only, so they ride in one extra user message
|
||||
* after the run of tool messages ends. *pending collects it. */
|
||||
static void defer_images(const cst_llm_request_t *req, const cst_message_t *m,
|
||||
cJSON **pending, size_t *budget) {
|
||||
static void defer_attachments(const cst_llm_request_t *req,
|
||||
const cst_message_t *m, cJSON **pending,
|
||||
size_t *budget, size_t *doc_budget,
|
||||
const char *img_off) {
|
||||
cJSON *p = *pending;
|
||||
if (!p) {
|
||||
p = cJSON_CreateObject();
|
||||
@@ -254,15 +345,18 @@ static void defer_images(const cst_llm_request_t *req, const cst_message_t *m,
|
||||
cJSON_AddArrayToObject(p, "content");
|
||||
*pending = p;
|
||||
}
|
||||
parts_add_images(req, cJSON_GetObjectItemCaseSensitive(p, "content"), m,
|
||||
budget);
|
||||
parts_add_attachments(req, cJSON_GetObjectItemCaseSensitive(p, "content"), m,
|
||||
budget, doc_budget, img_off);
|
||||
}
|
||||
|
||||
/* m's text plus one line per image it would have carried (malloc'd). */
|
||||
/* m's text plus one line per image it would have carried (malloc'd); a
|
||||
* document is not affected (it is text and is sent as such). */
|
||||
static char *image_notes(const cst_message_t *m, const char *why) {
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, m->content ? m->content : "");
|
||||
for (int i = 0; i < m->nattach; i++) {
|
||||
if (!cst_attach_is_image(&m->attach[i]))
|
||||
continue;
|
||||
char d[128];
|
||||
cst_attach_describe(&m->attach[i], d, sizeof(d));
|
||||
cst_buf_printf(&b, "%s[image \"%.120s\" (%s) not sent: %s]",
|
||||
@@ -355,6 +449,7 @@ char *cst_llm_build_body(const cst_llm_request_t *req) {
|
||||
cJSON *prev_user = NULL; /* merge consecutive user messages */
|
||||
cJSON *pending = NULL; /* images from tool results, waiting for a break */
|
||||
size_t img_budget = CST_ATTACH_MAX_TOTAL;
|
||||
size_t doc_budget = CST_ATTACH_MAX_TOTAL;
|
||||
const char *noimg = getenv("CST_NO_IMAGES");
|
||||
const char *img_off =
|
||||
noimg && noimg[0] && noimg[0] != '0'
|
||||
@@ -368,15 +463,26 @@ char *cst_llm_build_body(const cst_llm_request_t *req) {
|
||||
cst_role_t role = m->role;
|
||||
if (role == CST_ROLE_TOOL && !m->tool_call_id[0])
|
||||
role = CST_ROLE_USER;
|
||||
int has_img = m->attach && m->nattach > 0;
|
||||
int n_att = m->attach ? m->nattach : 0;
|
||||
int has_img = 0, has_doc = 0;
|
||||
for (int k = 0; k < n_att; k++) {
|
||||
if (cst_attach_is_image(&m->attach[k]))
|
||||
has_img = 1;
|
||||
else
|
||||
has_doc = 1;
|
||||
}
|
||||
char *noted = NULL;
|
||||
if (has_img && img_off) {
|
||||
/* plain text: text-only APIs may refuse content arrays */
|
||||
noted = image_notes(m, img_off);
|
||||
if (noted)
|
||||
mm.content = noted;
|
||||
has_img = 0;
|
||||
if (!has_doc) {
|
||||
/* plain text: text-only APIs may refuse content arrays */
|
||||
noted = image_notes(m, img_off);
|
||||
if (noted)
|
||||
mm.content = noted;
|
||||
}
|
||||
}
|
||||
/* a document needs a content array even when no image can go out */
|
||||
int att_parts = has_img || has_doc;
|
||||
|
||||
/* Every tool_call must be answered by tool messages, so tool-produced
|
||||
* images only leave the queue when the run of tool results is over. */
|
||||
@@ -392,7 +498,7 @@ char *cst_llm_build_body(const cst_llm_request_t *req) {
|
||||
cJSON *pc = prev_user
|
||||
? cJSON_GetObjectItemCaseSensitive(prev_user, "content")
|
||||
: NULL;
|
||||
if (!has_img && !cJSON_IsArray(pc)) {
|
||||
if (!att_parts && !cJSON_IsArray(pc)) {
|
||||
cJSON *c = clean_string(m->content, 0);
|
||||
if (prev_user) {
|
||||
cst_buf_t b = {0};
|
||||
@@ -424,8 +530,8 @@ char *cst_llm_build_body(const cst_llm_request_t *req) {
|
||||
}
|
||||
cJSON *parts = content_parts(o);
|
||||
parts_add_text(parts, clean_string(m->content, 0));
|
||||
if (has_img)
|
||||
parts_add_images(req, parts, m, &img_budget);
|
||||
if (att_parts)
|
||||
parts_add_attachments(req, parts, m, &img_budget, &doc_budget, img_off);
|
||||
free(noted);
|
||||
continue;
|
||||
}
|
||||
@@ -459,8 +565,8 @@ char *cst_llm_build_body(const cst_llm_request_t *req) {
|
||||
cJSON_AddTrueToObject(o, "prefix");
|
||||
}
|
||||
cJSON_AddItemToArray(msgs, o);
|
||||
if (role == CST_ROLE_TOOL && has_img)
|
||||
defer_images(req, m, &pending, &img_budget);
|
||||
if (role == CST_ROLE_TOOL && n_att)
|
||||
defer_attachments(req, m, &pending, &img_budget, &doc_budget, img_off);
|
||||
free(noted);
|
||||
}
|
||||
if (pending)
|
||||
@@ -981,7 +1087,7 @@ int cst_llm_chat_stream(const cst_llm_request_t *req, char *err,
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
llm_apply_ca_bundle(curl);
|
||||
cst_curl_apply_ca(curl);
|
||||
sse_reset(&s);
|
||||
s.curl = curl;
|
||||
if (rec_dir && rec_dir[0]) {
|
||||
@@ -1226,7 +1332,7 @@ int cst_llm_list_models_meta(const char *api_base, const char *api_key,
|
||||
snprintf(err, err_sz, "curl init failed");
|
||||
return -1;
|
||||
}
|
||||
llm_apply_ca_bundle(curl);
|
||||
cst_curl_apply_ca(curl);
|
||||
char url[1024];
|
||||
size_t bl = strlen(api_base);
|
||||
while (bl && api_base[bl - 1] == '/')
|
||||
|
||||
+95
-11
@@ -3,6 +3,7 @@
|
||||
#include "cst_models.h"
|
||||
#include "cst_tools.h"
|
||||
#include "cst_version.h"
|
||||
#include "cst_webui.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
@@ -25,6 +26,8 @@ static void usage(const char *argv0) {
|
||||
" (last used)\n"
|
||||
" -m, --model NAME model (last used for the provider)\n"
|
||||
" -e, --effort LEVEL low | medium | high | max (last used)\n"
|
||||
" -M, --mode NAME coding (default, code framed) | general\n"
|
||||
" (documents, data, research; last used)\n"
|
||||
" -w, --workspace DIR project directory (default: current)\n"
|
||||
" -c, --continue reopen the last session (default)\n"
|
||||
" -N, --new start a new session\n"
|
||||
@@ -41,6 +44,13 @@ static void usage(const char *argv0) {
|
||||
" -T, --tools PROFILE default (all tools) | minimal (paper set)\n"
|
||||
" -n, --max-steps N model calls per turn (30)\n"
|
||||
" -H, --headless -p TEXT run one prompt without the TUI\n"
|
||||
" --web serve the UI over HTTP on 127.0.0.1:8787\n"
|
||||
" --web-port N port (default 8787, 0 = pick a free one)\n"
|
||||
" --web-host ADDR bind address (default 127.0.0.1; a\n"
|
||||
" non-loopback address requires a token)\n"
|
||||
" --web-token T require ?token=T / Authorization: Bearer T\n"
|
||||
" --web-dev DIR serve the UI from DIR (development)\n"
|
||||
" --web-open open the browser at startup\n"
|
||||
" -a, --ask-key enter and save the provider's API key\n"
|
||||
" -k, --api-key KEY use KEY for this run only (not saved)\n"
|
||||
" -b, --api-base URL OpenAI-compatible base URL (…/v1)\n"
|
||||
@@ -51,13 +61,13 @@ static void usage(const char *argv0) {
|
||||
"API keys: the TUI asks for a missing key and saves it per provider\n"
|
||||
" in the config dir (credentials, mode 0600); /key changes it.\n"
|
||||
" Fallbacks: <provider env> (DEEPSEEK_API_KEY, ...), CST_API_KEY.\n"
|
||||
"Settings: provider, models and effort are remembered in\n"
|
||||
"Settings: provider, models, effort and mode are remembered in\n"
|
||||
" settings.json next to the credentials.\n"
|
||||
"Env: CST_PROVIDER CST_MODEL CST_EFFORT CST_PROTOCOL CST_TOOLS\n"
|
||||
" CST_CONFIRM CST_MAX_STEPS CST_TOOL_OUT_CAP CST_CTX_WINDOW\n"
|
||||
" CST_CTX_TOKENS CST_AUTO_COMPACT=0 CST_SHELL_TIMEOUT\n"
|
||||
" CST_METRICS=0 CST_RECORD_SSE CST_REPLAY_SSE CST_MODELS\n"
|
||||
" CST_APPROVAL=ask|auto|yolo CST_AUTO_APPROVE=1 (=yolo)\n"
|
||||
"Env: CST_PROVIDER CST_MODEL CST_EFFORT CST_MODE CST_PROTOCOL\n"
|
||||
" CST_TOOLS CST_CONFIRM CST_MAX_STEPS CST_TOOL_OUT_CAP\n"
|
||||
" CST_CTX_WINDOW CST_CTX_TOKENS CST_AUTO_COMPACT=0\n"
|
||||
" CST_SHELL_TIMEOUT CST_METRICS=0 CST_RECORD_SSE CST_REPLAY_SSE\n"
|
||||
" CST_MODELS CST_APPROVAL=ask|auto|yolo CST_AUTO_APPROVE=1 (=yolo)\n"
|
||||
" CST_SANDBOX=off CST_SHELL CST_NO_PROJECT_NOTES=1 NO_COLOR\n"
|
||||
"TUI: type /help inside for commands and keys\n",
|
||||
argv0);
|
||||
@@ -247,6 +257,16 @@ static int demo_ctl_impl(const char *workspace) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 0..65535, digits only: atoi turned "70000" into port 4464, "abc" into 0. */
|
||||
static int parse_port(const char *s, int *out) {
|
||||
char *end = NULL;
|
||||
long v = strtol(s, &end, 10);
|
||||
if (!s[0] || *end || v < 0 || v > 65535)
|
||||
return -1;
|
||||
*out = (int)v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (os_console_init() != 0)
|
||||
fprintf(stderr, "warning: the process code page is not UTF-8 (Windows 10 "
|
||||
@@ -320,6 +340,19 @@ int main(int argc, char **argv) {
|
||||
const char *prompt = NULL;
|
||||
int new_session = 0, do_list = 0;
|
||||
const char *resume_id = NULL;
|
||||
/* WebUI (--web): a second front end on the same engine. */
|
||||
int web_mode = 0;
|
||||
cst_webui_opts_t web;
|
||||
memset(&web, 0, sizeof(web));
|
||||
web.port = CST_WEBUI_DEFAULT_PORT; /* 0 would mean "pick a free one" */
|
||||
const char *env = getenv("CST_WEB_PORT");
|
||||
if (env && env[0] && parse_port(env, &web.port) != 0) {
|
||||
fprintf(stderr, "CST_WEB_PORT=%s is not a port (0-65535)\n", env);
|
||||
return 1;
|
||||
}
|
||||
web.host = getenv("CST_WEB_HOST");
|
||||
web.token = getenv("CST_WEB_TOKEN");
|
||||
web.root = getenv("CST_WEB_ROOT");
|
||||
|
||||
static struct option opts[] = {
|
||||
{"workspace", required_argument, 0, 'w'},
|
||||
@@ -343,13 +376,20 @@ int main(int argc, char **argv) {
|
||||
{"approval", required_argument, 0, 'A'},
|
||||
{"confirm", required_argument, 0, 'C'},
|
||||
{"effort", required_argument, 0, 'e'},
|
||||
{"mode", required_argument, 0, 'M'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'V'},
|
||||
{"list-models", optional_argument, 0, 0x100},
|
||||
{"web", no_argument, 0, 0x101},
|
||||
{"web-port", required_argument, 0, 0x102},
|
||||
{"web-host", required_argument, 0, 0x103},
|
||||
{"web-token", required_argument, 0, 0x104},
|
||||
{"web-dev", required_argument, 0, 0x105},
|
||||
{"web-open", no_argument, 0, 0x106},
|
||||
{0, 0, 0, 0}};
|
||||
int c;
|
||||
while ((c = getopt_long(argc, argv, "w:s:b:k:m:P:r:n:T:Hp:dahVNcR:LyA:C:e:", opts, NULL)) !=
|
||||
-1) {
|
||||
while ((c = getopt_long(argc, argv, "w:s:b:k:m:P:r:n:T:Hp:dahVNcR:LyA:C:e:M:",
|
||||
opts, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'w':
|
||||
snprintf(cfg.workspace_root, sizeof(cfg.workspace_root), "%s", optarg);
|
||||
@@ -441,6 +481,14 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
snprintf(cfg.effort, sizeof(cfg.effort), "%s", optarg);
|
||||
break;
|
||||
case 'M':
|
||||
if (!cst_mode_valid(optarg)) {
|
||||
fprintf(stderr, "bad --mode %s (coding | general)\n", optarg);
|
||||
curl_global_cleanup();
|
||||
return 1;
|
||||
}
|
||||
snprintf(cfg.mode, sizeof(cfg.mode), "%s", optarg);
|
||||
break;
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
curl_global_cleanup();
|
||||
@@ -453,6 +501,33 @@ int main(int argc, char **argv) {
|
||||
curl_global_cleanup();
|
||||
return rc;
|
||||
}
|
||||
case 0x101:
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x102:
|
||||
if (parse_port(optarg, &web.port) != 0) {
|
||||
fprintf(stderr, "--web-port %s is not a port (0-65535)\n", optarg);
|
||||
curl_global_cleanup();
|
||||
return 1;
|
||||
}
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x103:
|
||||
web.host = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x104:
|
||||
web.token = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x105:
|
||||
web.root = optarg;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 0x106:
|
||||
web.open_browser = 1;
|
||||
web_mode = 1;
|
||||
break;
|
||||
case 'V':
|
||||
printf("cst-agent %s\n", CST_VERSION);
|
||||
curl_global_cleanup();
|
||||
@@ -493,6 +568,12 @@ int main(int argc, char **argv) {
|
||||
snprintf(cfg.effort, sizeof(cfg.effort), "%s",
|
||||
cst_effort_valid(e) ? e : CST_DEFAULT_EFFORT);
|
||||
}
|
||||
if (!cfg.mode[0]) {
|
||||
const char *m = getenv("CST_MODE");
|
||||
if (!cst_mode_valid(m))
|
||||
m = saved.mode;
|
||||
snprintf(cfg.mode, sizeof(cfg.mode), "%s", cst_mode_name(m));
|
||||
}
|
||||
if (!cfg.api_base[0]) {
|
||||
const char *env_base = getenv("CST_API_BASE");
|
||||
if (!env_base || !env_base[0])
|
||||
@@ -600,7 +681,7 @@ int main(int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (!cfg.api_key[0] && cfg.headless) {
|
||||
if (!cfg.api_key[0] && (cfg.headless || web_mode)) {
|
||||
fprintf(stderr,
|
||||
"no API key for %s: start cst-agent once to enter it, or run "
|
||||
"cst-agent --ask-key --provider %s\n",
|
||||
@@ -610,7 +691,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
/* Router presets have no fixed model: take the first one the API lists
|
||||
* (the TUI does the same after its /models probe). */
|
||||
if (!cfg.model[0] && cfg.headless) {
|
||||
if (!cfg.model[0] && (cfg.headless || web_mode)) {
|
||||
static cst_model_meta_t list[512];
|
||||
char err[256] = "";
|
||||
long age = 0;
|
||||
@@ -654,7 +735,10 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
int rc = 0;
|
||||
if (cfg.headless) {
|
||||
if (web_mode) {
|
||||
web.prompt = prompt; /* --web -p "...": run one turn at startup */
|
||||
rc = cst_webui_run(agent, &web);
|
||||
} else if (cfg.headless) {
|
||||
if (!prompt) {
|
||||
fprintf(stderr, "--headless requires --prompt\n");
|
||||
rc = 1;
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_agent.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
/* Provider and model data live in model_registry.c (cst_models.h). */
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
"models": [
|
||||
{"match": "deepseek", "context": 1000000, "vision": false,
|
||||
"price": {"currency": "¥", "miss": 2, "hit": 0.04, "out": 8, "offpeak_half": true}},
|
||||
{"match": "deepseek-flash", "vision": true},
|
||||
{"match": "deepseek-chat", "vision": true},
|
||||
{"match": "deepseek-reasoner", "vision": true},
|
||||
{"match": "deepseek-v4-pro",
|
||||
"price": {"currency": "¥", "miss": 9, "hit": 0.3, "out": 27, "offpeak_half": true}},
|
||||
{"match": "deepseek-vl", "vision": true},
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_net.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
|
||||
#undef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0600 /* WSAPoll */
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifndef MSG_NOSIGNAL
|
||||
#define MSG_NOSIGNAL 0
|
||||
#endif
|
||||
|
||||
#define NET_SEND_TIMEOUT_MS 20000
|
||||
|
||||
#ifdef _WIN32
|
||||
static void set_err_win(char *err, size_t err_sz, const char *what) {
|
||||
if (err && err_sz)
|
||||
snprintf(err, err_sz, "%s (winsock error %d)", what, WSAGetLastError());
|
||||
}
|
||||
#define SET_ERR(e, n, w) set_err_win(e, n, w)
|
||||
#define NET_FD(s) ((SOCKET)(s))
|
||||
static void net_close_raw(os_socket_t s) { closesocket((SOCKET)s); }
|
||||
static int net_last_would_block(void) {
|
||||
int e = WSAGetLastError();
|
||||
return e == WSAEWOULDBLOCK || e == WSAEINTR;
|
||||
}
|
||||
static int net_last_conn_reset(void) {
|
||||
int e = WSAGetLastError();
|
||||
return e == WSAECONNRESET || e == WSAECONNABORTED || e == WSAENOTSOCK;
|
||||
}
|
||||
#else
|
||||
static void set_err(char *err, size_t err_sz, const char *what) {
|
||||
if (err && err_sz)
|
||||
snprintf(err, err_sz, "%s", what);
|
||||
}
|
||||
#define SET_ERR(e, n, w) set_err(e, n, w)
|
||||
#define NET_FD(s) ((int)(s))
|
||||
static void net_close_raw(os_socket_t s) { close(s); }
|
||||
static int net_last_would_block(void) { return errno == EINTR; }
|
||||
static int net_last_conn_reset(void) {
|
||||
return errno == ECONNRESET || errno == EPIPE || errno == EBADF;
|
||||
}
|
||||
#endif
|
||||
|
||||
int os_net_init(void) {
|
||||
#ifdef _WIN32
|
||||
WSADATA w;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &w) != 0)
|
||||
return -1;
|
||||
#else
|
||||
/* A hung-up client must not kill the process with SIGPIPE. */
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_cleanup(void) {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
/* poll, not select: FD_SET past FD_SETSIZE writes out of bounds, and a
|
||||
* failed connect shows up as an error event, not as writable, on Windows. */
|
||||
static int wait_io(os_socket_t s, int want_write, int timeout_ms) {
|
||||
#ifdef _WIN32
|
||||
WSAPOLLFD p;
|
||||
p.fd = NET_FD(s);
|
||||
p.events = want_write ? POLLWRNORM : POLLRDNORM;
|
||||
p.revents = 0;
|
||||
int rc = WSAPoll(&p, 1, timeout_ms < 0 ? -1 : timeout_ms);
|
||||
#else
|
||||
struct pollfd p;
|
||||
p.fd = NET_FD(s);
|
||||
p.events = want_write ? POLLOUT : POLLIN;
|
||||
p.revents = 0;
|
||||
int rc = poll(&p, 1, timeout_ms < 0 ? -1 : timeout_ms);
|
||||
#endif
|
||||
if (rc > 0)
|
||||
return 1;
|
||||
if (rc == 0)
|
||||
return 0; /* timed out */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Host to bind: AF_INET unless the name is an IPv6 literal, so "localhost"
|
||||
* and "127.0.0.1" always mean the IPv4 loopback a browser will reach. */
|
||||
static int resolve_bind(const char *host, int port, struct sockaddr_storage *sa,
|
||||
socklen_t *sa_len) {
|
||||
memset(sa, 0, sizeof(*sa));
|
||||
int family = AF_INET;
|
||||
const char *h = host && host[0] ? host : "127.0.0.1";
|
||||
if (strchr(h, ':'))
|
||||
family = AF_INET6;
|
||||
struct addrinfo hints, *res = NULL;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = family;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
char portstr[16];
|
||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||
if (getaddrinfo(h, portstr, &hints, &res) != 0 || !res)
|
||||
return -1;
|
||||
memcpy(sa, res->ai_addr, res->ai_addrlen);
|
||||
*sa_len = (socklen_t)res->ai_addrlen;
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_socket_t os_net_listen(const char *host, int port, char *err, size_t err_sz) {
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t sa_len;
|
||||
if (resolve_bind(host, port, &sa, &sa_len) != 0) {
|
||||
SET_ERR(err, err_sz, "cannot resolve the bind address");
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
os_socket_t s = (os_socket_t)socket(sa.ss_family, SOCK_STREAM, 0);
|
||||
if (s == OS_SOCKET_BAD) {
|
||||
SET_ERR(err, err_sz, "cannot create a socket");
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
int one = 1;
|
||||
#ifdef _WIN32
|
||||
/* SO_REUSEADDR on Windows lets another process bind the same port and
|
||||
* take over our connections */
|
||||
setsockopt(NET_FD(s), SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char *)&one,
|
||||
sizeof(one));
|
||||
#else
|
||||
setsockopt(NET_FD(s), SOL_SOCKET, SO_REUSEADDR, (const char *)&one,
|
||||
sizeof(one));
|
||||
#endif
|
||||
#ifdef IPV6_V6ONLY
|
||||
if (sa.ss_family == AF_INET6)
|
||||
setsockopt(NET_FD(s), IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&one,
|
||||
sizeof(one));
|
||||
#endif
|
||||
if (bind(NET_FD(s), (const struct sockaddr *)&sa, sa_len) != 0) {
|
||||
SET_ERR(err, err_sz, "the address is already in use");
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
if (listen(NET_FD(s), 32) != 0) {
|
||||
SET_ERR(err, err_sz, "listen failed");
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int os_net_port(os_socket_t s) {
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t len = sizeof(sa);
|
||||
if (getsockname(NET_FD(s), (struct sockaddr *)&sa, &len) != 0)
|
||||
return 0;
|
||||
if (sa.ss_family == AF_INET)
|
||||
return ntohs(((struct sockaddr_in *)&sa)->sin_port);
|
||||
if (sa.ss_family == AF_INET6)
|
||||
return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_peer(os_socket_t s, char *out, size_t out_sz) {
|
||||
if (!out || out_sz == 0)
|
||||
return;
|
||||
out[0] = 0;
|
||||
struct sockaddr_storage sa;
|
||||
socklen_t len = sizeof(sa);
|
||||
if (getpeername(NET_FD(s), (struct sockaddr *)&sa, &len) != 0)
|
||||
return;
|
||||
char ip[64] = "";
|
||||
int port = 0;
|
||||
if (sa.ss_family == AF_INET) {
|
||||
if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&sa)->sin_addr, ip,
|
||||
sizeof(ip)))
|
||||
return;
|
||||
port = ntohs(((struct sockaddr_in *)&sa)->sin_port);
|
||||
} else if (sa.ss_family == AF_INET6) {
|
||||
if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&sa)->sin6_addr, ip,
|
||||
sizeof(ip)))
|
||||
return;
|
||||
port = ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
if (strchr(ip, ':'))
|
||||
snprintf(out, out_sz, "[%s]:%d", ip, port);
|
||||
else
|
||||
snprintf(out, out_sz, "%s:%d", ip, port);
|
||||
}
|
||||
|
||||
os_socket_t os_net_connect(const char *host, int port, int timeout_ms) {
|
||||
const char *h = host && host[0] ? host : "127.0.0.1";
|
||||
struct addrinfo hints, *res = NULL;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = strchr(h, ':') ? AF_INET6 : AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
char portstr[16];
|
||||
snprintf(portstr, sizeof(portstr), "%d", port);
|
||||
if (getaddrinfo(h, portstr, &hints, &res) != 0 || !res)
|
||||
return OS_SOCKET_BAD;
|
||||
os_socket_t s = (os_socket_t)socket(res->ai_family, SOCK_STREAM, 0);
|
||||
if (s == OS_SOCKET_BAD) {
|
||||
freeaddrinfo(res);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
/* Bounded wait: connect() non-blocking, poll, then read SO_ERROR. A dead
|
||||
* server must not hang the caller. */
|
||||
#ifdef _WIN32
|
||||
u_long nb = 1;
|
||||
ioctlsocket(NET_FD(s), FIONBIO, &nb);
|
||||
#else
|
||||
int fl = fcntl(s, F_GETFL, 0);
|
||||
fcntl(s, F_SETFL, fl | O_NONBLOCK);
|
||||
#endif
|
||||
int rc = connect(NET_FD(s), res->ai_addr, (int)res->ai_addrlen);
|
||||
freeaddrinfo(res);
|
||||
if (rc != 0) {
|
||||
#ifdef _WIN32
|
||||
int in_progress = WSAGetLastError() == WSAEWOULDBLOCK;
|
||||
#else
|
||||
int in_progress = errno == EINPROGRESS || errno == EINTR;
|
||||
#endif
|
||||
if (!in_progress || wait_io(s, 1, timeout_ms) <= 0) {
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
int soerr = 0;
|
||||
socklen_t elen = sizeof(soerr);
|
||||
if (getsockopt(NET_FD(s), SOL_SOCKET, SO_ERROR, (char *)&soerr, &elen) != 0 ||
|
||||
soerr != 0) {
|
||||
net_close_raw(s);
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
}
|
||||
#ifdef _WIN32
|
||||
nb = 0;
|
||||
ioctlsocket(NET_FD(s), FIONBIO, &nb);
|
||||
#else
|
||||
fcntl(s, F_SETFL, fl);
|
||||
#endif
|
||||
return s;
|
||||
}
|
||||
|
||||
os_socket_t os_net_accept(os_socket_t s, int timeout_ms, int *timed_out) {
|
||||
if (timed_out)
|
||||
*timed_out = 0;
|
||||
int w = wait_io(s, 0, timeout_ms);
|
||||
if (w == 0) {
|
||||
if (timed_out)
|
||||
*timed_out = 1;
|
||||
return OS_SOCKET_BAD;
|
||||
}
|
||||
if (w < 0)
|
||||
return OS_SOCKET_BAD;
|
||||
os_socket_t c = (os_socket_t)accept(NET_FD(s), NULL, NULL);
|
||||
if (c == OS_SOCKET_BAD)
|
||||
return OS_SOCKET_BAD;
|
||||
int one = 1;
|
||||
setsockopt(NET_FD(c), IPPROTO_TCP, TCP_NODELAY, (const char *)&one,
|
||||
sizeof(one));
|
||||
return c;
|
||||
}
|
||||
|
||||
long os_net_recv(os_socket_t s, void *buf, size_t n, int timeout_ms) {
|
||||
int w = wait_io(s, 0, timeout_ms);
|
||||
if (w == 0)
|
||||
return -2;
|
||||
if (w < 0)
|
||||
return -1;
|
||||
size_t want = n > (size_t)(1 << 20) ? (size_t)(1 << 20) : n;
|
||||
int r = (int)recv(NET_FD(s), (char *)buf, (int)want, 0);
|
||||
if (r == 0)
|
||||
return 0;
|
||||
if (r < 0)
|
||||
return net_last_conn_reset() ? 0 : (net_last_would_block() ? -2 : -1);
|
||||
return r;
|
||||
}
|
||||
|
||||
int os_net_send_all(os_socket_t s, const void *buf, size_t n, int timeout_ms) {
|
||||
const char *p = buf;
|
||||
size_t off = 0;
|
||||
while (off < n) {
|
||||
int w = wait_io(s, 1, timeout_ms ? timeout_ms : NET_SEND_TIMEOUT_MS);
|
||||
if (w <= 0)
|
||||
return -1;
|
||||
long r = (long)send(NET_FD(s), p + off, (int)(n - off), MSG_NOSIGNAL);
|
||||
if (r <= 0) {
|
||||
if (net_last_would_block())
|
||||
continue;
|
||||
return -1;
|
||||
}
|
||||
off += (size_t)r;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void os_net_shutdown(os_socket_t s) {
|
||||
#ifdef _WIN32
|
||||
shutdown((SOCKET)s, SD_BOTH);
|
||||
#else
|
||||
shutdown(s, SHUT_RDWR);
|
||||
#endif
|
||||
}
|
||||
|
||||
void os_net_close(os_socket_t s) {
|
||||
if (s == OS_SOCKET_BAD)
|
||||
return;
|
||||
net_close_raw(s);
|
||||
}
|
||||
+74
-14
@@ -13,6 +13,7 @@
|
||||
#include "os_internal.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -503,23 +504,78 @@ int os_resolve_shell(char *out, size_t out_sz) {
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Per thread: parallel tool runs and sub-agents each watch their own flag. */
|
||||
static _Thread_local volatile int *g_run_cancel;
|
||||
static _Thread_local int g_run_timeout_s;
|
||||
/* Per thread: parallel tool runs and sub-agents each watch their own flag.
|
||||
* Thread keys rather than _Thread_local, which tcc does not implement; each
|
||||
* value fits in the key's pointer slot, so nothing is allocated. */
|
||||
#ifdef _WIN32
|
||||
static INIT_ONCE g_run_once = INIT_ONCE_STATIC_INIT;
|
||||
static DWORD g_key_cancel = TLS_OUT_OF_INDEXES, g_key_timeout = TLS_OUT_OF_INDEXES;
|
||||
static BOOL CALLBACK run_keys_init(PINIT_ONCE once, PVOID param, PVOID *ctx) {
|
||||
(void)once;
|
||||
(void)param;
|
||||
(void)ctx;
|
||||
g_key_cancel = TlsAlloc();
|
||||
g_key_timeout = TlsAlloc();
|
||||
return TRUE;
|
||||
}
|
||||
static void *run_key_get(DWORD *key) {
|
||||
InitOnceExecuteOnce(&g_run_once, run_keys_init, NULL, NULL);
|
||||
if (*key == TLS_OUT_OF_INDEXES)
|
||||
return NULL;
|
||||
DWORD err = GetLastError(); /* TlsGetValue clears it */
|
||||
void *v = TlsGetValue(*key);
|
||||
SetLastError(err);
|
||||
return v;
|
||||
}
|
||||
static void run_key_set(DWORD *key, void *v) {
|
||||
InitOnceExecuteOnce(&g_run_once, run_keys_init, NULL, NULL);
|
||||
if (*key != TLS_OUT_OF_INDEXES)
|
||||
TlsSetValue(*key, v);
|
||||
}
|
||||
#else
|
||||
static pthread_once_t g_run_once = PTHREAD_ONCE_INIT;
|
||||
static pthread_key_t g_key_cancel, g_key_timeout;
|
||||
static int g_run_keys_ok;
|
||||
static void run_keys_init(void) {
|
||||
g_run_keys_ok = pthread_key_create(&g_key_cancel, NULL) == 0 &&
|
||||
pthread_key_create(&g_key_timeout, NULL) == 0;
|
||||
}
|
||||
static void *run_key_get(pthread_key_t *key) {
|
||||
pthread_once(&g_run_once, run_keys_init);
|
||||
return g_run_keys_ok ? pthread_getspecific(*key) : NULL;
|
||||
}
|
||||
static void run_key_set(pthread_key_t *key, void *v) {
|
||||
pthread_once(&g_run_once, run_keys_init);
|
||||
if (g_run_keys_ok)
|
||||
pthread_setspecific(*key, v);
|
||||
}
|
||||
#endif
|
||||
|
||||
void os_set_cancel_flag(volatile int *flag) { g_run_cancel = flag; }
|
||||
static volatile int *run_cancel(void) {
|
||||
return (volatile int *)run_key_get(&g_key_cancel);
|
||||
}
|
||||
static int run_timeout(void) {
|
||||
return (int)(intptr_t)run_key_get(&g_key_timeout);
|
||||
}
|
||||
|
||||
void os_set_run_timeout(int seconds) { g_run_timeout_s = seconds; }
|
||||
void os_set_cancel_flag(volatile int *flag) {
|
||||
run_key_set(&g_key_cancel, (void *)(uintptr_t)flag);
|
||||
}
|
||||
|
||||
int os_get_run_timeout(void) { return g_run_timeout_s; }
|
||||
void os_set_run_timeout(int seconds) {
|
||||
run_key_set(&g_key_timeout, (void *)(intptr_t)seconds);
|
||||
}
|
||||
|
||||
int os_get_run_timeout(void) { return run_timeout(); }
|
||||
|
||||
int os_run_timeout_effective(void) {
|
||||
if (g_run_timeout_s > 0)
|
||||
return g_run_timeout_s;
|
||||
int t = run_timeout();
|
||||
if (t > 0)
|
||||
return t;
|
||||
const char *e = getenv("CST_SHELL_TIMEOUT");
|
||||
if (e && atoi(e) > 0)
|
||||
return atoi(e);
|
||||
return 120;
|
||||
return 0; /* no limit: a build, a test suite or a long clone must finish */
|
||||
}
|
||||
|
||||
double os_now_ms(void) {
|
||||
@@ -662,7 +718,8 @@ int os_run_capture_ex(const char *cwd, const char *const argv[],
|
||||
int truncated = 0, killed = 0;
|
||||
int status = 0, reaped = 0, bg_killed = 0;
|
||||
double reaped_at = 0, killed_at = 0;
|
||||
double deadline = os_now_ms() + os_run_timeout_effective() * 1000.0;
|
||||
int timeout_s = os_run_timeout_effective();
|
||||
double deadline = timeout_s > 0 ? os_now_ms() + timeout_s * 1000.0 : 0;
|
||||
for (;;) {
|
||||
struct pollfd pfd = {pipefd[0], POLLIN, 0};
|
||||
int pr = poll(&pfd, 1, 100);
|
||||
@@ -692,8 +749,8 @@ int os_run_capture_ex(const char *cwd, const char *const argv[],
|
||||
}
|
||||
/* Checked on every pass, so a chatty command still stops promptly. */
|
||||
if (!reaped && !killed &&
|
||||
((g_run_cancel && *g_run_cancel) || now > deadline)) {
|
||||
killed = (g_run_cancel && *g_run_cancel) ? 2 : 1;
|
||||
(os_cancel_requested() || (deadline > 0 && now > deadline))) {
|
||||
killed = os_cancel_requested() ? 2 : 1;
|
||||
killed_at = now;
|
||||
kill(-pid, SIGTERM);
|
||||
}
|
||||
@@ -899,9 +956,12 @@ void os_run_parallel(os_task_fn fn, void *const args[], int n) {
|
||||
free(tasks);
|
||||
}
|
||||
|
||||
int os_cancel_requested(void) { return g_run_cancel && *g_run_cancel; }
|
||||
int os_cancel_requested(void) {
|
||||
volatile int *c = run_cancel();
|
||||
return c && *c;
|
||||
}
|
||||
|
||||
volatile int *os_get_cancel_flag(void) { return g_run_cancel; }
|
||||
volatile int *os_get_cancel_flag(void) { return run_cancel(); }
|
||||
|
||||
#ifndef _WIN32
|
||||
int os_proc_spawn(const char *cwd, const char *const argv[],
|
||||
|
||||
+4
-1
@@ -601,7 +601,10 @@ int os_run_capture_token(const char *cwd, const char *const argv[],
|
||||
buf[0] = 0;
|
||||
int truncated = 0, killed = 0, reaped = 0, bg_killed = 0;
|
||||
double reaped_at = 0, killed_at = 0;
|
||||
double deadline = os_now_ms() + os_run_timeout_effective() * 1000.0;
|
||||
/* no timeout unless one was asked for: a deadline far in the future keeps
|
||||
* every `now > deadline` check working without a second code path */
|
||||
int timeout_s = os_run_timeout_effective();
|
||||
double deadline = timeout_s > 0 ? os_now_ms() + timeout_s * 1000.0 : 1e18;
|
||||
int idle = 0; /* empty polls in a row: wait longer once output stalls */
|
||||
for (;;) {
|
||||
DWORD avail = 0;
|
||||
|
||||
+1901
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,193 @@
|
||||
#include "cst_provider_switch.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int cst_ps_fetch(const char *provider, const char *api_base, const char *key,
|
||||
char ids[][128], int max, char *err, size_t err_sz) {
|
||||
cst_model_meta_t *m = calloc((size_t)(max > 0 ? max : 1), sizeof(*m));
|
||||
if (!m) {
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
int r = cst_llm_list_models_meta(api_base, key, m, max, err, err_sz);
|
||||
if (r >= 0)
|
||||
cst_models_cache_save(provider, api_base, m, r);
|
||||
for (int i = 0; i < r; i++)
|
||||
snprintf(ids[i], 128, "%s", m[i].id);
|
||||
free(m);
|
||||
return r;
|
||||
}
|
||||
|
||||
int cst_ps_cached(const char *provider, char ids[][128], int max, int *fresh) {
|
||||
*fresh = 0;
|
||||
cst_model_meta_t *m = calloc((size_t)(max > 0 ? max : 1), sizeof(*m));
|
||||
if (!m)
|
||||
return -1;
|
||||
long age = 0;
|
||||
int n = cst_models_cache_load(provider, m, max, &age);
|
||||
for (int i = 0; i < n; i++)
|
||||
snprintf(ids[i], 128, "%s", m[i].id);
|
||||
free(m);
|
||||
*fresh = n > 0 && age >= 0 && age < CST_MODELS_CACHE_TTL;
|
||||
return n > 0 ? n : -1;
|
||||
}
|
||||
|
||||
const char *cst_ps_base(const cst_agent_config_t *cfg, const char *provider) {
|
||||
if (cfg && strcmp(cfg->provider, provider) == 0)
|
||||
return cfg->api_base;
|
||||
const cst_provider_t *p = cst_provider_find(provider);
|
||||
return p ? p->api_base : "";
|
||||
}
|
||||
|
||||
int cst_ps_models(const cst_agent_config_t *cfg, int refresh,
|
||||
cst_ps_model_t *out, int max, int *api_rc, char *err,
|
||||
size_t err_sz) {
|
||||
err[0] = 0;
|
||||
*api_rc = -1;
|
||||
char(*ids)[128] = calloc(CST_PS_MAX_MODELS, sizeof(*ids));
|
||||
if (!ids) {
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return 0;
|
||||
}
|
||||
int fresh = 0;
|
||||
int na = cst_ps_cached(cfg->provider, ids, CST_PS_MAX_MODELS, &fresh);
|
||||
*api_rc = na;
|
||||
if ((refresh || !fresh) && cfg->api_key[0]) {
|
||||
char(*got)[128] = calloc(CST_PS_MAX_MODELS, sizeof(*got));
|
||||
if (got) {
|
||||
int r = cst_ps_fetch(cfg->provider, cfg->api_base, cfg->api_key, got,
|
||||
CST_PS_MAX_MODELS, err, err_sz);
|
||||
*api_rc = r;
|
||||
if (r >= 0) {
|
||||
memcpy(ids, got, (size_t)r * sizeof(*ids));
|
||||
na = r;
|
||||
}
|
||||
free(got);
|
||||
}
|
||||
}
|
||||
int n = 0;
|
||||
for (int i = 0; i < na && n < max; i++, n++) {
|
||||
snprintf(out[n].id, sizeof(out[n].id), "%s", ids[i]);
|
||||
out[n].api = 1;
|
||||
cst_model_info(cfg->provider, ids[i], &out[n].info);
|
||||
}
|
||||
char builtin[32][128];
|
||||
int nb = cst_models_list(cfg->model, builtin, 32);
|
||||
for (int i = 0; i < nb && n < max; i++) {
|
||||
int dup = 0;
|
||||
for (int k = 0; k < na && !dup; k++)
|
||||
dup = strcmp(ids[k], builtin[i]) == 0;
|
||||
if (dup)
|
||||
continue;
|
||||
memcpy(out[n].id, builtin[i], sizeof(out[n].id));
|
||||
out[n].id[sizeof(out[n].id) - 1] = 0;
|
||||
out[n].api = 0;
|
||||
cst_model_info(cfg->provider, builtin[i], &out[n].info);
|
||||
n++;
|
||||
}
|
||||
free(ids);
|
||||
return n;
|
||||
}
|
||||
|
||||
int cst_ps_verify_save(const char *provider, const char *api_base,
|
||||
const char *key, char ids[][128], int max, int *nids,
|
||||
char *err, size_t err_sz) {
|
||||
err[0] = 0;
|
||||
char(*own)[128] = NULL;
|
||||
if (!ids) {
|
||||
own = calloc(CST_PS_MAX_MODELS, sizeof(*own));
|
||||
if (!own) {
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return CST_PS_UNVERIFIED | CST_PS_UNSAVED;
|
||||
}
|
||||
ids = own;
|
||||
max = CST_PS_MAX_MODELS;
|
||||
}
|
||||
int r = cst_ps_fetch(provider, api_base, key, ids, max, err, err_sz);
|
||||
free(own);
|
||||
if (nids)
|
||||
*nids = r;
|
||||
if (r == CST_MODELS_AUTH)
|
||||
return CST_MODELS_AUTH;
|
||||
int flags = r < 0 ? CST_PS_UNVERIFIED : 0;
|
||||
if (r >= 0)
|
||||
err[0] = 0;
|
||||
if (cst_creds_save(provider, key) != 0)
|
||||
flags |= CST_PS_UNSAVED;
|
||||
return flags;
|
||||
}
|
||||
|
||||
void cst_ps_apply_provider(cst_agent_config_t *cfg, const cst_provider_t *p,
|
||||
const char *key) {
|
||||
cst_settings_t s;
|
||||
cst_settings_load(&s);
|
||||
const char *model = cst_settings_model(&s, p->name);
|
||||
snprintf(cfg->provider, sizeof(cfg->provider), "%s", p->name);
|
||||
snprintf(cfg->api_base, sizeof(cfg->api_base), "%s", p->api_base);
|
||||
snprintf(cfg->model, sizeof(cfg->model), "%s", model ? model : p->model);
|
||||
snprintf(cfg->api_key, sizeof(cfg->api_key), "%s", key);
|
||||
cst_settings_remember(cfg);
|
||||
}
|
||||
|
||||
int cst_ps_switch_provider(cst_agent_config_t *cfg, const char *name,
|
||||
const char *key, char *err, size_t err_sz) {
|
||||
err[0] = 0;
|
||||
const cst_provider_t *p = name ? cst_provider_find(name) : NULL;
|
||||
if (!p) {
|
||||
snprintf(err, err_sz, "unknown provider '%.64s'", name ? name : "");
|
||||
return CST_PS_UNKNOWN;
|
||||
}
|
||||
char k[512];
|
||||
int flags = 0;
|
||||
char(*ids)[128] = calloc(CST_PS_MAX_MODELS, sizeof(*ids));
|
||||
int nids = -1;
|
||||
if (key && key[0]) {
|
||||
snprintf(k, sizeof(k), "%s", key);
|
||||
flags = cst_ps_verify_save(p->name, p->api_base, k, ids,
|
||||
ids ? CST_PS_MAX_MODELS : 0, &nids, err,
|
||||
err_sz);
|
||||
if (flags < 0) {
|
||||
memset(k, 0, sizeof(k));
|
||||
free(ids);
|
||||
return flags;
|
||||
}
|
||||
} else if (cst_creds_resolve(p->name, k, sizeof(k), NULL) != 0) {
|
||||
free(ids);
|
||||
snprintf(err, err_sz, "%s needs an API key", p->name);
|
||||
return CST_PS_NEED_KEY;
|
||||
}
|
||||
cst_ps_apply_provider(cfg, p, k);
|
||||
memset(k, 0, sizeof(k));
|
||||
if (!cfg->model[0] && ids) {
|
||||
/* a router preset: the first model the API lists */
|
||||
int fresh = 0;
|
||||
if (nids <= 0)
|
||||
nids = cst_ps_cached(p->name, ids, CST_PS_MAX_MODELS, &fresh);
|
||||
if (nids <= 0) {
|
||||
char e2[256];
|
||||
nids = cst_ps_fetch(p->name, cfg->api_base, cfg->api_key, ids,
|
||||
CST_PS_MAX_MODELS, e2, sizeof(e2));
|
||||
}
|
||||
if (nids > 0) {
|
||||
snprintf(cfg->model, sizeof(cfg->model), "%s", ids[0]);
|
||||
cst_settings_remember(cfg);
|
||||
}
|
||||
}
|
||||
free(ids);
|
||||
return flags;
|
||||
}
|
||||
|
||||
int cst_ps_set_key(cst_agent_config_t *cfg, const char *key, char *err,
|
||||
size_t err_sz) {
|
||||
if (!key || !key[0]) {
|
||||
snprintf(err, err_sz, "empty key");
|
||||
return CST_PS_NEED_KEY;
|
||||
}
|
||||
int r = cst_ps_verify_save(cfg->provider, cfg->api_base, key, NULL, 0, NULL,
|
||||
err, err_sz);
|
||||
if (r >= 0)
|
||||
snprintf(cfg->api_key, sizeof(cfg->api_key), "%s", key);
|
||||
return r;
|
||||
}
|
||||
+5
-19
@@ -86,8 +86,6 @@ static int g_abi;
|
||||
#define LL_FS_MAKE_SYM (1ULL << 12)
|
||||
#define LL_FS_REFER (1ULL << 13) /* ABI 2 */
|
||||
#define LL_FS_TRUNCATE (1ULL << 14) /* ABI 3 */
|
||||
#define LL_NET_BIND_TCP (1ULL << 0) /* ABI 4 */
|
||||
#define LL_NET_CONNECT_TCP (1ULL << 1) /* ABI 4 */
|
||||
|
||||
struct ll_ruleset_attr {
|
||||
uint64_t handled_access_fs;
|
||||
@@ -137,9 +135,7 @@ static int ll_prepare(const char *ws, const char *tmp, cst_sandbox_plan_t *p) {
|
||||
int abi = g_abi;
|
||||
struct ll_ruleset_attr attr;
|
||||
memset(&attr, 0, sizeof(attr));
|
||||
attr.handled_access_fs = ll_write_rights(abi);
|
||||
if (abi >= 4)
|
||||
attr.handled_access_net = LL_NET_BIND_TCP | LL_NET_CONNECT_TCP;
|
||||
attr.handled_access_fs = ll_write_rights(abi); /* network stays open */
|
||||
int fd = (int)syscall(__NR_landlock_create_ruleset, &attr, sizeof(attr), 0);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
@@ -193,8 +189,7 @@ static int sb_prepare(const char *ws, const char *tmp, cst_sandbox_plan_t *p) {
|
||||
"(version 1)(allow default)(deny file-write*)"
|
||||
"(allow file-write* (subpath \"%s\") (subpath \"%s\") "
|
||||
"(literal \"/dev/null\") (literal \"/dev/tty\") "
|
||||
"(literal \"/dev/dtracehelper\"))"
|
||||
"(deny network*)",
|
||||
"(literal \"/dev/dtracehelper\"))",
|
||||
qws, qtmp);
|
||||
return 0;
|
||||
}
|
||||
@@ -290,9 +285,8 @@ static const int SC_NOTIFY[] = {
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Foreign ABIs are refused, IPv4/IPv6 sockets fail with EACCES, opens
|
||||
* without write flags pass untouched, write-type calls go to the
|
||||
* supervisor. */
|
||||
/* Foreign ABIs are refused, opens without write flags pass untouched,
|
||||
* write-type calls go to the supervisor. */
|
||||
static void sc_build(sc_prog_t *p) {
|
||||
p->n = 0;
|
||||
SC_LD(offsetof(struct seccomp_data, arch));
|
||||
@@ -303,12 +297,6 @@ static void sc_build(sc_prog_t *p) {
|
||||
sc_emit(p, BPF_JMP | BPF_JGE | BPF_K, 0, 1, 0x40000000U); /* x32 */
|
||||
SC_RET(SECCOMP_RET_ERRNO | EPERM);
|
||||
#endif
|
||||
SC_JEQ(__NR_socket, 0, 5);
|
||||
SC_LD(SC_ARG(0));
|
||||
SC_JEQ(AF_INET, 2, 0);
|
||||
SC_JEQ(AF_INET6, 1, 0);
|
||||
SC_RET(SECCOMP_RET_ALLOW);
|
||||
SC_RET(SECCOMP_RET_ERRNO | EACCES);
|
||||
SC_JEQ(__NR_io_uring_setup, 0, 1);
|
||||
SC_RET(SECCOMP_RET_ERRNO | EPERM);
|
||||
SC_JEQ(__NR_openat, 0, 4);
|
||||
@@ -1446,9 +1434,7 @@ int cst_sandbox_blocked(const char *out, size_t n, int exit_code) {
|
||||
static const char *const SIGNS[] = {
|
||||
"Permission denied", "Read-only file system", "Operation not permitted",
|
||||
"EACCES", "EPERM", "EROFS",
|
||||
"[cst: sandbox", "Network is unreachable",
|
||||
/* curl when the socket itself was refused (seccomp) */
|
||||
"Could not connect to server", "Failed to connect to",
|
||||
"[cst: sandbox",
|
||||
/* localized strerror(EACCES/EPERM/EROFS) */
|
||||
"权限不够", "不允许的操作", "只读文件系统", "權限不足",
|
||||
"拒絕不符權限的操作", "許可がありません", "Keine Berechtigung",
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "cJSON.h"
|
||||
#include "cst_agent.h"
|
||||
#include "cst_attach.h"
|
||||
#include "cst_util.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
@@ -409,3 +410,65 @@ int cst_session_rewrite(cst_session_t *s, const cst_message_t *msgs, int n) {
|
||||
free(copy);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int cst_message_is_tool_result(const cst_message_t *m) {
|
||||
return m && (m->role == CST_ROLE_TOOL ||
|
||||
(m->role == CST_ROLE_USER && m->tool_call_id[0]));
|
||||
}
|
||||
|
||||
static void md_fence(cst_buf_t *b, const char *s, size_t max) {
|
||||
size_t n = strlen(s);
|
||||
if (n > max)
|
||||
n = cst_utf8_floor(s, max);
|
||||
cst_buf_puts(b, "````\n");
|
||||
cst_buf_append(b, s, n);
|
||||
cst_buf_puts(b, n < strlen(s) ? "\n…(truncated)\n````\n\n" : "\n````\n\n");
|
||||
}
|
||||
|
||||
char *cst_session_export_md(cst_session_t *s, const char *title,
|
||||
const char *sid, const char *model) {
|
||||
if (!s)
|
||||
return NULL;
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "# %s\n\nsession `%s` · model `%s`\n\n",
|
||||
title ? title : "", sid ? sid : "", model ? model : "");
|
||||
cst_session_lock(s);
|
||||
for (int i = 0; i < cst_session_count(s); i++) {
|
||||
const cst_message_t *m = cst_session_get(s, i);
|
||||
const char *c = m->content ? m->content : "";
|
||||
if (m->role == CST_ROLE_SYSTEM)
|
||||
continue;
|
||||
if (cst_message_is_tool_result(m)) {
|
||||
cst_buf_printf(&b, "**Tool result** `%s`\n\n", m->tool_name);
|
||||
md_fence(&b, c, 4000);
|
||||
} else if (cst_message_is_note(m)) {
|
||||
cst_buf_puts(&b, "> ");
|
||||
cst_buf_append(&b, c, strcspn(c, "\n"));
|
||||
cst_buf_puts(&b, "\n\n");
|
||||
} else if (m->role == CST_ROLE_USER) {
|
||||
cst_buf_puts(&b, "## User\n\n");
|
||||
cst_buf_puts(&b, c);
|
||||
cst_buf_puts(&b, "\n\n");
|
||||
for (int k = 0; k < m->nattach; k++) {
|
||||
char d[128];
|
||||
cst_attach_describe(&m->attach[k], d, sizeof(d));
|
||||
cst_buf_printf(&b, "- %s `%s` (%s)\n",
|
||||
cst_attach_is_image(&m->attach[k]) ? "image"
|
||||
: "document",
|
||||
m->attach[k].name, d);
|
||||
}
|
||||
if (m->nattach)
|
||||
cst_buf_puts(&b, "\n");
|
||||
} else {
|
||||
cst_buf_puts(&b, "## Assistant\n\n");
|
||||
cst_buf_puts(&b, c);
|
||||
cst_buf_puts(&b, "\n\n");
|
||||
if (m->tool_calls_json && m->tool_calls_json[0]) {
|
||||
cst_buf_puts(&b, "**Tool calls**\n\n");
|
||||
md_fence(&b, m->tool_calls_json, 4000);
|
||||
}
|
||||
}
|
||||
}
|
||||
cst_session_unlock(s);
|
||||
return cst_buf_steal(&b);
|
||||
}
|
||||
|
||||
+36
-2
@@ -28,11 +28,17 @@ int cst_settings_load(cst_settings_t *s) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
if (!fp)
|
||||
return -1;
|
||||
static _Thread_local char buf[65536];
|
||||
size_t n = fread(buf, 1, sizeof(buf) - 1, fp);
|
||||
enum { SETTINGS_MAX = 65536 };
|
||||
char *buf = malloc(SETTINGS_MAX);
|
||||
if (!buf) {
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
size_t n = fread(buf, 1, SETTINGS_MAX - 1, fp);
|
||||
fclose(fp);
|
||||
buf[n] = 0;
|
||||
cJSON *root = cJSON_Parse(buf + cst_bom_len(buf, n));
|
||||
free(buf);
|
||||
if (!cJSON_IsObject(root)) {
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
@@ -41,6 +47,8 @@ int cst_settings_load(cst_settings_t *s) {
|
||||
cJSON_GetObjectItemCaseSensitive(root, "provider"));
|
||||
copy_str(s->effort, sizeof(s->effort),
|
||||
cJSON_GetObjectItemCaseSensitive(root, "effort"));
|
||||
copy_str(s->mode, sizeof(s->mode),
|
||||
cJSON_GetObjectItemCaseSensitive(root, "mode"));
|
||||
copy_str(s->approval, sizeof(s->approval),
|
||||
cJSON_GetObjectItemCaseSensitive(root, "approval"));
|
||||
s->show_thinking =
|
||||
@@ -80,6 +88,8 @@ int cst_settings_save(const cst_settings_t *s) {
|
||||
cJSON_AddStringToObject(root, "provider", s->provider);
|
||||
if (s->effort[0])
|
||||
cJSON_AddStringToObject(root, "effort", s->effort);
|
||||
if (s->mode[0])
|
||||
cJSON_AddStringToObject(root, "mode", s->mode);
|
||||
if (s->approval[0])
|
||||
cJSON_AddStringToObject(root, "approval", s->approval);
|
||||
if (s->show_thinking)
|
||||
@@ -129,6 +139,21 @@ int cst_approval_parse(const char *s, int *out) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const char *const MODE_NAMES[] = {"coding", "general"};
|
||||
|
||||
int cst_mode_valid(const char *m) {
|
||||
for (size_t i = 0; m && m[0] && i < sizeof(MODE_NAMES) / sizeof(*MODE_NAMES);
|
||||
i++) {
|
||||
if (strcmp(m, MODE_NAMES[i]) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *cst_mode_name(const char *m) {
|
||||
return cst_mode_valid(m) ? m : CST_DEFAULT_MODE;
|
||||
}
|
||||
|
||||
const char *cst_settings_model(const cst_settings_t *s, const char *provider) {
|
||||
for (int i = 0; provider && i < s->nmodels; i++) {
|
||||
if (strcmp(s->models[i].provider, provider) == 0 && s->models[i].model[0])
|
||||
@@ -142,6 +167,8 @@ int cst_settings_remember(const cst_agent_config_t *cfg) {
|
||||
cst_settings_load(&s);
|
||||
snprintf(s.provider, sizeof(s.provider), "%s", cfg->provider);
|
||||
snprintf(s.effort, sizeof(s.effort), "%s", cfg->effort);
|
||||
if (cfg->mode[0])
|
||||
snprintf(s.mode, sizeof(s.mode), "%s", cfg->mode);
|
||||
int i = 0;
|
||||
while (i < s.nmodels && strcmp(s.models[i].provider, cfg->provider) != 0)
|
||||
i++;
|
||||
@@ -170,6 +197,13 @@ int cst_settings_remember_thinking(int show_thinking) {
|
||||
return cst_settings_save(&s);
|
||||
}
|
||||
|
||||
int cst_settings_remember_mode(const char *mode) {
|
||||
cst_settings_t s;
|
||||
cst_settings_load(&s);
|
||||
snprintf(s.mode, sizeof(s.mode), "%s", cst_mode_name(mode));
|
||||
return cst_settings_save(&s);
|
||||
}
|
||||
|
||||
static void real_ws(const char *ws, char *out, size_t n) {
|
||||
if (!ws || os_realpath(ws, out, n) != 0)
|
||||
snprintf(out, n, "%s", ws ? ws : "");
|
||||
|
||||
@@ -488,6 +488,16 @@ char *cst_skill_read(const char *ws, const char *name, const char *file,
|
||||
return cst_buf_steal(&b);
|
||||
}
|
||||
|
||||
const char *cst_init_prompt(void) {
|
||||
return "Analyze this repository and write an AGENTS.md file at the "
|
||||
"workspace root for future coding agents. Cover: what the project "
|
||||
"is, how to build, test and run it (exact commands), the directory "
|
||||
"layout, coding conventions you can infer from the code, and "
|
||||
"anything surprising. Explore with list_dir/glob/grep/read_file "
|
||||
"before writing. Keep it under 150 lines. If AGENTS.md already "
|
||||
"exists, improve it rather than starting over.";
|
||||
}
|
||||
|
||||
char *cst_skill_prompt(const char *ws, const char *name, const char *task) {
|
||||
int code = 0;
|
||||
char *body = cst_skill_read(ws, name, NULL, &code);
|
||||
|
||||
+5
-5
@@ -40,7 +40,7 @@ static const struct {
|
||||
"runs commands as needed.",
|
||||
"", 0},
|
||||
{"explore",
|
||||
"Fast read-only explorer: searches and reads the codebase to answer a "
|
||||
"Fast read-only explorer: searches and reads the workspace to answer a "
|
||||
"question (where is X, how does Y work, which files do Z). Several can "
|
||||
"run at once.",
|
||||
"You only read. Search broadly first (grep, glob, list_dir), then read "
|
||||
@@ -49,11 +49,11 @@ static const struct {
|
||||
"you looked for and did not find.",
|
||||
1},
|
||||
{"plan",
|
||||
"Read-only planner: investigates the code involved and returns a "
|
||||
"Read-only planner: investigates the material involved and returns a "
|
||||
"step-by-step implementation plan.",
|
||||
"You only read. Investigate the code involved, then return a concrete "
|
||||
"plan: the files to change and what to change in each, risks and edge "
|
||||
"cases, and how to verify. Sketch code only where it removes ambiguity.",
|
||||
"You only read. Investigate what is involved, then return a concrete "
|
||||
"plan: what to produce or change and where, risks and edge cases, and "
|
||||
"how to check the result. Sketch code only where it removes ambiguity.",
|
||||
1},
|
||||
};
|
||||
#define NBUILTIN ((int)(sizeof(BUILTIN) / sizeof(BUILTIN[0])))
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "cst_tools.h"
|
||||
#include "cst_clipboard.h"
|
||||
#include "cst_util.h"
|
||||
#include "tools_internal.h"
|
||||
|
||||
@@ -106,3 +107,70 @@ int cst_tool_ask_user(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
1);
|
||||
return tool_set_resultf(r, 0, "user answered: %s", ans);
|
||||
}
|
||||
|
||||
/* The system clipboard: what the user copied elsewhere. Reading needs no
|
||||
* permission (READ); writing replaces what the user has copied (WRITE, so
|
||||
* the confirmation frontier applies). */
|
||||
int cst_tool_clipboard_read(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r) {
|
||||
(void)ctx;
|
||||
char err[192];
|
||||
char *text = NULL;
|
||||
size_t n = 0;
|
||||
if (cst_clipboard_text(&text, &n, err, sizeof(err)) != 0)
|
||||
return tool_set_result(r, err, 1);
|
||||
long cap = tool_arg_long(c, "max_bytes", 65536);
|
||||
if (cap > 0 && (size_t)cap < n)
|
||||
n = (size_t)cap;
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "clipboard text (%zu bytes%s):\n", (size_t)n,
|
||||
(size_t)cap < (size_t)n ? ", truncated" : "");
|
||||
cst_buf_append(&b, text, n);
|
||||
if (b.len && b.buf[b.len - 1] != '\n')
|
||||
cst_buf_puts(&b, "\n");
|
||||
free(text);
|
||||
return tool_take_buf(r, &b, 0);
|
||||
}
|
||||
|
||||
int cst_tool_clipboard_write(const cst_tool_ctx_t *ctx,
|
||||
const ctl_tool_call_t *c, cst_tool_result_t *r) {
|
||||
(void)ctx;
|
||||
size_t n = 0;
|
||||
const char *text = tool_arg_n(c, "text", &n);
|
||||
if (!text)
|
||||
return tool_set_result(r, "error: missing text", 1);
|
||||
char err[192];
|
||||
if (cst_clipboard_set_text(text, n, err, sizeof(err)) != 0)
|
||||
return tool_set_result(r, err, 1);
|
||||
return tool_set_resultf(r, 0, "copied %zu bytes to the clipboard", n);
|
||||
}
|
||||
|
||||
/* Confirmation preview: what the clipboard would hold afterwards. Clipboard
|
||||
* text has no diff, so this is the text itself, cut to a readable size. */
|
||||
#define CLIP_PREVIEW_MAX 2000
|
||||
int cst_preview_clipboard_write(const cst_tool_ctx_t *ctx,
|
||||
const ctl_tool_call_t *c, char **out) {
|
||||
(void)ctx;
|
||||
size_t n = 0;
|
||||
const char *text = tool_arg_n(c, "text", &n);
|
||||
if (!text) {
|
||||
*out = strdup("error: missing text");
|
||||
return -1;
|
||||
}
|
||||
int lines = 0;
|
||||
for (size_t i = 0; i < n; i++)
|
||||
lines += text[i] == '\n';
|
||||
size_t cut = n;
|
||||
if (cut > CLIP_PREVIEW_MAX)
|
||||
cut = cst_utf8_floor(text, CLIP_PREVIEW_MAX);
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_printf(&b, "clipboard: replace with %zu bytes, %d line%s\n", n,
|
||||
lines + (n && text[n - 1] != '\n'), lines == 0 ? "" : "s");
|
||||
cst_buf_append(&b, text, cut);
|
||||
if (b.len && b.buf[b.len - 1] != '\n')
|
||||
cst_buf_puts(&b, "\n");
|
||||
if (cut < n)
|
||||
cst_buf_printf(&b, "… (%zu more bytes)\n", n - cut);
|
||||
*out = cst_buf_steal(&b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -267,6 +267,14 @@ int cst_tool_view_image(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_attachment_t *a = cst_attach_import_file(session, full, err, sizeof(err));
|
||||
if (!a)
|
||||
return tool_set_resultf(r, 1, "error: cannot show %.200s: %s", path, err);
|
||||
if (!cst_attach_is_image(a)) {
|
||||
cst_attach_free(a);
|
||||
return tool_set_resultf(
|
||||
r, 1,
|
||||
"error: %.200s is not an image; a text file or PDF reaches the model "
|
||||
"through /attach <path> (a PDF becomes its text layer)",
|
||||
path);
|
||||
}
|
||||
snprintf(a->origin, sizeof(a->origin), "view_image");
|
||||
const char *d = tool_arg(c, "detail");
|
||||
if (d && (strcmp(d, "low") == 0 || strcmp(d, "high") == 0))
|
||||
|
||||
+16
-2
@@ -66,8 +66,9 @@ static const cst_tool_def_t TOOLS[] = {
|
||||
{"detail", S, 0, "low or high (provider hint; default auto)"}},
|
||||
CST_TP_READ, 0, cst_tool_view_image, NULL, NO_DYN},
|
||||
{"run_shell",
|
||||
"Run a non-interactive shell command in the workspace root for builds, "
|
||||
"tests, git and other CLIs (stdin is empty; long runs are killed). "
|
||||
"Run a non-interactive shell command in the workspace root: builds, "
|
||||
"tests, git, scripts, computations, data processing, other CLIs "
|
||||
"(stdin is empty; long runs are killed). "
|
||||
"Returns stdout+stderr with colors stripped and long output cut to head "
|
||||
"+ tail. Prefer read_file/grep/glob/list_dir over cat/grep/find.",
|
||||
"Run a shell command in the workspace root (non-interactive, stdin is "
|
||||
@@ -158,6 +159,19 @@ static const cst_tool_def_t TOOLS[] = {
|
||||
{{"question", S, 1, "the question"},
|
||||
{"options", S, 0, "suggested answers, one per line"}},
|
||||
CST_TP_ASK, 0, cst_tool_ask_user, NULL, NO_DYN},
|
||||
{"clipboard_read",
|
||||
"Read the text on the system clipboard — what the user copied outside "
|
||||
"the workspace. Returns UTF-8 text.",
|
||||
NULL,
|
||||
{{"max_bytes", I, 0, "cap on the returned text (default 65536)"}},
|
||||
CST_TP_READ, 0, cst_tool_clipboard_read, NULL, NO_DYN},
|
||||
{"clipboard_write",
|
||||
"Replace the system clipboard with text, so the user can paste it into "
|
||||
"another app. Overwrites what they had copied.",
|
||||
NULL,
|
||||
{{"text", S, 1, "text to put on the clipboard"}},
|
||||
CST_TP_WRITE, 0, cst_tool_clipboard_write, cst_preview_clipboard_write,
|
||||
NO_DYN},
|
||||
};
|
||||
#define NTOOLS ((int)(sizeof(TOOLS) / sizeof(TOOLS[0])))
|
||||
|
||||
|
||||
+3
-2
@@ -659,9 +659,10 @@ static int plan_patch(const char *ws, const char *patch, patch_plan_t *pl,
|
||||
pl->full[i] = malloc(OS_PATH_MAX);
|
||||
if (!pl->full[i] ||
|
||||
cst_sandbox_resolve(ws, target, pl->full[i], OS_PATH_MAX) != 0) {
|
||||
if (dry && pl->full[i] && !pl->del[i] && is_new) {
|
||||
if (dry && pl->full[i] && !pl->del[i] && is_new &&
|
||||
strlen(target) < OS_PATH_MAX) {
|
||||
/* parent dirs do not exist yet; the write would create them */
|
||||
snprintf(pl->full[i], OS_PATH_MAX, "%s", target);
|
||||
memcpy(pl->full[i], target, strlen(target) + 1);
|
||||
} else {
|
||||
snprintf(err, err_sz, "error: path outside workspace: %.200s",
|
||||
target);
|
||||
|
||||
@@ -46,6 +46,13 @@ int cst_tool_todo_write(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r);
|
||||
int cst_tool_ask_user(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r);
|
||||
/* Clipboard text (system clipboard, not the workspace). */
|
||||
int cst_tool_clipboard_read(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r);
|
||||
int cst_tool_clipboard_write(const cst_tool_ctx_t *ctx,
|
||||
const ctl_tool_call_t *c, cst_tool_result_t *r);
|
||||
int cst_preview_clipboard_write(const cst_tool_ctx_t *ctx,
|
||||
const ctl_tool_call_t *c, char **out);
|
||||
int cst_tool_web_fetch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r);
|
||||
int cst_tool_view_image(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
|
||||
@@ -215,6 +215,7 @@ int cst_tool_web_fetch(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
memset(&f, 0, sizeof(f));
|
||||
char errbuf[CURL_ERROR_SIZE];
|
||||
errbuf[0] = 0;
|
||||
cst_curl_apply_ca(curl);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "cst_mcp.h"
|
||||
#include "cst_models.h"
|
||||
#include "cst_plugins.h"
|
||||
#include "cst_provider_switch.h"
|
||||
#include "cst_skills.h"
|
||||
#include "cst_subagent.h"
|
||||
#include "cst_textsel.h"
|
||||
@@ -151,6 +152,7 @@ static int cst_clock_gettime_mono(struct timespec *ts) {
|
||||
#ifdef NCURSES_VERSION
|
||||
static int g_kitty_on;
|
||||
|
||||
#ifndef _WIN32
|
||||
/* Bind the reports and ask whether the terminal speaks the protocol; the
|
||||
* flags are pushed once the reply arrives (see tui_wget_wch). */
|
||||
static void kitty_init(void) {
|
||||
@@ -181,6 +183,7 @@ static void kitty_restore(void) {
|
||||
g_kitty_on = 0;
|
||||
}
|
||||
}
|
||||
#endif /* !_WIN32 */
|
||||
#endif
|
||||
|
||||
/* wget_wch() that turns kitty key reports back into legacy input. */
|
||||
@@ -378,6 +381,7 @@ typedef struct {
|
||||
char *prompt_hist[PROMPT_HIST_CAP];
|
||||
int prompt_hist_n;
|
||||
int hist_browse;
|
||||
char *hist_draft; /* the live input while browsing */
|
||||
|
||||
/* Busy-turn FIFO */
|
||||
char *queue[QUEUE_CAP];
|
||||
@@ -1040,10 +1044,7 @@ static int find_tool_block(tui_t *t, const char *id) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int msg_is_tool_result(const cst_message_t *m) {
|
||||
return m && (m->role == CST_ROLE_TOOL ||
|
||||
(m->role == CST_ROLE_USER && m->tool_call_id[0]));
|
||||
}
|
||||
#define msg_is_tool_result cst_message_is_tool_result
|
||||
|
||||
/* Strip the "[tool …]" / "result <id> exit=N <n>" header and "end" trailer. */
|
||||
static void tool_result_view(const char *content, const char **body,
|
||||
@@ -2353,6 +2354,7 @@ static const char *const TIPS[] = {
|
||||
"Ctrl+P opens the command palette",
|
||||
"Type @ to mention a file in your message",
|
||||
"/effort sets reasoning effort: low, medium, high, max",
|
||||
"/mode general reframes the agent for non-code work",
|
||||
"/model lists the models your API key can use",
|
||||
"/undo reverts the file changes of the last turn",
|
||||
"/provider switches provider; keys are saved per provider",
|
||||
@@ -2831,7 +2833,10 @@ static void reload_hist_from_session(tui_t *t) {
|
||||
for (int k = 0; k < m->nattach; k++) {
|
||||
char d[128];
|
||||
cst_attach_describe(&m->attach[k], d, sizeof(d));
|
||||
cst_buf_printf(&ub, "\n[image %s · %s]", m->attach[k].name, d);
|
||||
cst_buf_printf(&ub, "\n[%s %s · %s]",
|
||||
cst_attach_is_image(&m->attach[k]) ? "image"
|
||||
: "document",
|
||||
m->attach[k].name, d);
|
||||
}
|
||||
block_set_body(b, ub.buf ? ub.buf : "", ub.len);
|
||||
cst_buf_free(&ub);
|
||||
@@ -3163,28 +3168,16 @@ static size_t word_right(const tui_t *t, size_t at) {
|
||||
return at;
|
||||
}
|
||||
|
||||
/* Move to the previous/next logical line keeping the column (in chars).
|
||||
* Returns 0 when already on the first/last line. */
|
||||
static size_t input_hit(const tui_t *t, int want_row, int want_col);
|
||||
|
||||
/* Move to the previous/next visual row (soft-wrapped or not) keeping the
|
||||
* screen column. Returns 0 when already on the first/last row. */
|
||||
static int input_line_move(tui_t *t, int dir) {
|
||||
size_t ls = line_start(t, t->cur);
|
||||
int col = 0;
|
||||
for (size_t i = ls; i < t->cur; i += next_len(t, i))
|
||||
col++;
|
||||
size_t target;
|
||||
if (dir < 0) {
|
||||
if (ls == 0)
|
||||
return 0;
|
||||
target = line_start(t, ls - 1);
|
||||
} else {
|
||||
size_t le = line_end(t, t->cur);
|
||||
if (le >= t->in_len)
|
||||
return 0;
|
||||
target = le + 1;
|
||||
}
|
||||
size_t te = line_end(t, target);
|
||||
while (col-- > 0 && target < te)
|
||||
target += next_len(t, target);
|
||||
t->cur = target;
|
||||
int cr, cc;
|
||||
int rows = input_layout(t, NULL, NULL, &cr, &cc);
|
||||
if (cr + dir < 0 || cr + dir >= rows)
|
||||
return 0;
|
||||
t->cur = input_hit(t, cr + dir, cc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -3897,46 +3890,11 @@ static void am_store(const char *provider, int r, char ids[][128],
|
||||
pthread_mutex_unlock(&g_am_mu);
|
||||
}
|
||||
|
||||
/* GET /models; a good answer also refreshes models-cache/<provider>.json. */
|
||||
static int fetch_models(const char *provider, const char *base,
|
||||
const char *key, char ids[][128], int max, char *err,
|
||||
size_t err_sz) {
|
||||
cst_model_meta_t *m = calloc((size_t)max, sizeof(*m));
|
||||
if (!m) {
|
||||
snprintf(err, err_sz, "out of memory");
|
||||
return -1;
|
||||
}
|
||||
int r = cst_llm_list_models_meta(base, key, m, max, err, err_sz);
|
||||
if (r >= 0)
|
||||
cst_models_cache_save(provider, base, m, r);
|
||||
for (int i = 0; i < r; i++)
|
||||
snprintf(ids[i], 128, "%s", m[i].id);
|
||||
free(m);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* The cached list for provider into ids: count, or -1; *fresh when it is
|
||||
* younger than CST_MODELS_CACHE_TTL. */
|
||||
static int cached_models(const char *provider, char ids[][128], int max,
|
||||
int *fresh) {
|
||||
*fresh = 0;
|
||||
cst_model_meta_t *m = calloc((size_t)max, sizeof(*m));
|
||||
if (!m)
|
||||
return -1;
|
||||
long age = 0;
|
||||
int n = cst_models_cache_load(provider, m, max, &age);
|
||||
for (int i = 0; i < n; i++)
|
||||
snprintf(ids[i], 128, "%s", m[i].id);
|
||||
free(m);
|
||||
*fresh = n > 0 && age >= 0 && age < CST_MODELS_CACHE_TTL;
|
||||
return n > 0 ? n : -1;
|
||||
}
|
||||
|
||||
static void *probe_thread(void *arg) {
|
||||
probe_arg_t *a = arg;
|
||||
static char ids[AM_MAX][128]; /* one probe runs per provider switch */
|
||||
char err[256];
|
||||
int r = fetch_models(a->provider, a->base, a->key, ids, AM_MAX, err,
|
||||
int r = cst_ps_fetch(a->provider, a->base, a->key, ids, AM_MAX, err,
|
||||
sizeof(err));
|
||||
if (r >= 0 || r == CST_MODELS_AUTH || !a->have_cache)
|
||||
am_store(a->provider, r, ids, err);
|
||||
@@ -3948,23 +3906,13 @@ static void *probe_thread(void *arg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Base URL for a provider: the live config for the current one (it may be
|
||||
* overridden), else the preset. */
|
||||
static const char *provider_base(const tui_t *t, const char *provider) {
|
||||
const cst_agent_config_t *cfg = cst_agent_config(t->agent);
|
||||
if (cfg && strcmp(cfg->provider, provider) == 0)
|
||||
return cfg->api_base;
|
||||
const cst_provider_t *p = cst_provider_find(provider);
|
||||
return p ? p->api_base : "";
|
||||
}
|
||||
|
||||
static void start_probe(tui_t *t) {
|
||||
const cst_agent_config_t *cfg = cst_agent_config(t->agent);
|
||||
if (!cfg || !cfg->api_key[0] || g_am_threads > 0)
|
||||
return;
|
||||
static char ids[AM_MAX][128];
|
||||
int fresh = 0;
|
||||
int nc = cached_models(cfg->provider, ids, AM_MAX, &fresh);
|
||||
int nc = cst_ps_cached(cfg->provider, ids, AM_MAX, &fresh);
|
||||
pthread_mutex_lock(&g_am_mu);
|
||||
snprintf(g_am.provider, sizeof(g_am.provider), "%s", cfg->provider);
|
||||
pthread_mutex_unlock(&g_am_mu);
|
||||
@@ -4050,13 +3998,14 @@ static int obtain_key(tui_t *t, const char *provider, char *out, size_t n,
|
||||
return 0;
|
||||
char dir[1024] = "the config directory";
|
||||
os_config_dir(dir, sizeof(dir));
|
||||
const char *base = provider_base(t, provider);
|
||||
const char *base = cst_ps_base(cst_agent_config(t->agent), provider);
|
||||
char title[128], hint[1300];
|
||||
snprintf(title, sizeof(title), "API key for %s", provider);
|
||||
snprintf(hint, sizeof(hint), "%.200s · saved to %.900s/credentials (only you "
|
||||
"can read it)",
|
||||
base, dir);
|
||||
char note[300] = "";
|
||||
int flags;
|
||||
for (;;) {
|
||||
if (secret_input_run(t, title, hint, out, n) != 0)
|
||||
return -1;
|
||||
@@ -4064,8 +4013,10 @@ static int obtain_key(tui_t *t, const char *provider, char *out, size_t n,
|
||||
draw_status(t);
|
||||
static char ids[AM_MAX][128];
|
||||
char err[256];
|
||||
int r = fetch_models(provider, base, out, ids, AM_MAX, err, sizeof(err));
|
||||
if (r == CST_MODELS_AUTH) {
|
||||
int r = -1;
|
||||
flags = cst_ps_verify_save(provider, base, out, ids, AM_MAX, &r, err,
|
||||
sizeof(err));
|
||||
if (flags == CST_MODELS_AUTH) {
|
||||
snprintf(hint, sizeof(hint), "%s rejected that key (%.200s) — paste "
|
||||
"another one",
|
||||
base, err);
|
||||
@@ -4075,13 +4026,13 @@ static int obtain_key(tui_t *t, const char *provider, char *out, size_t n,
|
||||
snprintf(g_am.provider, sizeof(g_am.provider), "%s", provider);
|
||||
pthread_mutex_unlock(&g_am_mu);
|
||||
am_store(provider, r, ids, err);
|
||||
if (r < 0)
|
||||
if (flags & CST_PS_UNVERIFIED)
|
||||
snprintf(note, sizeof(note), "could not verify the key (%.200s); "
|
||||
"saved anyway",
|
||||
err);
|
||||
break;
|
||||
}
|
||||
if (cst_creds_save(provider, out) != 0)
|
||||
if (flags & CST_PS_UNSAVED)
|
||||
set_status(t, "warning: could not save the key; it is used for this run "
|
||||
"only");
|
||||
else if (note[0])
|
||||
@@ -4313,7 +4264,7 @@ static void cmd_model(tui_t *t, char *arg, int *quit) {
|
||||
set_status(t, "fetching the model list…");
|
||||
draw_status(t);
|
||||
char err[256];
|
||||
na = fetch_models(cfg->provider, cfg->api_base, cfg->api_key, names,
|
||||
na = cst_ps_fetch(cfg->provider, cfg->api_base, cfg->api_key, names,
|
||||
AM_MAX, err, sizeof(err));
|
||||
pthread_mutex_lock(&g_am_mu);
|
||||
snprintf(g_am.provider, sizeof(g_am.provider), "%s", cfg->provider);
|
||||
@@ -4414,6 +4365,41 @@ static void cmd_effort(tui_t *t, char *arg, int *quit) {
|
||||
cfg->effort, cfg->provider, cfg->model);
|
||||
}
|
||||
|
||||
static void cmd_mode(tui_t *t, char *arg, int *quit) {
|
||||
(void)quit;
|
||||
cst_agent_config_t *cfg = cst_agent_config_mut(t->agent);
|
||||
static const char *const names[] = {"coding", "general"};
|
||||
static const char *const desc[] = {
|
||||
"code framed: repo, build, tests (default)",
|
||||
"documents, data, research, everyday files"};
|
||||
const char *want = NULL;
|
||||
if (arg && arg[0]) {
|
||||
if (!cst_mode_valid(arg)) {
|
||||
push_system_line(t, "Unknown mode '%s' (coding | general)", arg);
|
||||
return;
|
||||
}
|
||||
want = arg;
|
||||
} else {
|
||||
char items[2][128];
|
||||
int cur_idx = 0;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
snprintf(items[i], 128, "%-8s %s", names[i], desc[i]);
|
||||
if (strcmp(names[i], cst_mode_name(cfg->mode)) == 0)
|
||||
cur_idx = i;
|
||||
}
|
||||
int pick = picker_run(t, "Working mode", items, 2, cur_idx);
|
||||
if (pick < 0)
|
||||
return;
|
||||
want = names[pick];
|
||||
}
|
||||
snprintf(cfg->mode, sizeof(cfg->mode), "%s", want);
|
||||
cst_settings_remember_mode(cfg->mode);
|
||||
push_system_line(t, "(mode → %s%s)", cfg->mode,
|
||||
strcmp(cfg->mode, "general") == 0
|
||||
? "; the system prompt is rewritten on the next turn"
|
||||
: "");
|
||||
}
|
||||
|
||||
static void cmd_provider(tui_t *t, char *arg, int *quit) {
|
||||
(void)quit;
|
||||
cst_agent_config_t *cfg = cst_agent_config_mut(t->agent);
|
||||
@@ -4447,15 +4433,8 @@ static void cmd_provider(tui_t *t, char *arg, int *quit) {
|
||||
push_system_line(t, "(provider unchanged: %s needs an API key)", p->name);
|
||||
return;
|
||||
}
|
||||
cst_settings_t s;
|
||||
cst_settings_load(&s);
|
||||
const char *model = cst_settings_model(&s, p->name);
|
||||
snprintf(cfg->provider, sizeof(cfg->provider), "%s", p->name);
|
||||
snprintf(cfg->api_base, sizeof(cfg->api_base), "%s", p->api_base);
|
||||
snprintf(cfg->model, sizeof(cfg->model), "%s", model ? model : p->model);
|
||||
snprintf(cfg->api_key, sizeof(cfg->api_key), "%s", key);
|
||||
cst_ps_apply_provider(cfg, p, key);
|
||||
memset(key, 0, sizeof(key));
|
||||
cst_settings_remember(cfg);
|
||||
push_system_line(t, "(provider → %s · %s · model %s)", p->name, p->api_base,
|
||||
cfg->model);
|
||||
if (am_models(p->name, NULL, 0) < 0)
|
||||
@@ -4699,14 +4678,15 @@ static void cmd_status(tui_t *t, char *arg, int *quit) {
|
||||
: !same ? "this run only (--api-key / CST_API_KEY)"
|
||||
: strcmp(from, "saved") == 0 ? "saved (/key to replace)"
|
||||
: from);
|
||||
push_system_line(t, " protocol %s · tools %s · max steps %d",
|
||||
push_system_line(t, " protocol %s · tools %s · mode %s · max steps %d",
|
||||
cst_protocol_name(cfg->protocol),
|
||||
cst_tool_profile_name(cfg->tool_profile), cfg->max_steps);
|
||||
cst_tool_profile_name(cfg->tool_profile),
|
||||
cst_mode_name(cfg->mode), cfg->max_steps);
|
||||
char appr[64];
|
||||
approval_label(t, appr, sizeof(appr));
|
||||
push_system_line(t, " approval %s — %s", appr,
|
||||
approval_help(approval_mode(t)));
|
||||
push_system_line(t, " mode %s",
|
||||
push_system_line(t, " plan %s",
|
||||
cfg->plan_mode ? "Plan (read-only tools)" : "Build");
|
||||
const char *notes = cst_agent_notes(t->agent);
|
||||
push_system_line(t, " notes %s",
|
||||
@@ -4833,12 +4813,6 @@ static void cmd_diff(tui_t *t, char *arg, int *quit) {
|
||||
free(df.out);
|
||||
}
|
||||
|
||||
static void md_fence(FILE *fp, const char *s, size_t max) {
|
||||
size_t n = strlen(s);
|
||||
fprintf(fp, "````\n%.*s%s\n````\n\n", (int)(n > max ? max : n), s,
|
||||
n > max ? "\n…(truncated)" : "");
|
||||
}
|
||||
|
||||
static void cmd_export(tui_t *t, char *arg, int *quit) {
|
||||
(void)quit;
|
||||
const cst_agent_config_t *cfg = cst_agent_config(t->agent);
|
||||
@@ -4858,44 +4832,20 @@ static void cmd_export(tui_t *t, char *arg, int *quit) {
|
||||
*sl = 0;
|
||||
os_mkdir_p(dir, 0755);
|
||||
}
|
||||
FILE *fp = fopen(path, "wb");
|
||||
char *md = cst_session_export_md(cst_agent_session(t->agent), t->title, sid,
|
||||
cfg->model);
|
||||
FILE *fp = md ? fopen(path, "wb") : NULL;
|
||||
if (!fp) {
|
||||
free(md);
|
||||
push_system_line(t, "Error: cannot write %s", path);
|
||||
return;
|
||||
}
|
||||
fprintf(fp, "# %s\n\nsession `%s` · model `%s`\n\n", t->title, sid,
|
||||
cfg->model);
|
||||
cst_session_t *s = cst_agent_session(t->agent);
|
||||
cst_session_lock(s);
|
||||
for (int i = 0; i < cst_session_count(s); i++) {
|
||||
const cst_message_t *m = cst_session_get(s, i);
|
||||
const char *c = m->content ? m->content : "";
|
||||
if (m->role == CST_ROLE_SYSTEM)
|
||||
continue;
|
||||
if (msg_is_tool_result(m)) {
|
||||
fprintf(fp, "**Tool result** `%s`\n\n", m->tool_name);
|
||||
md_fence(fp, c, 4000);
|
||||
} else if (cst_message_is_note(m)) {
|
||||
fprintf(fp, "> %.*s\n\n", (int)strcspn(c, "\n"), c);
|
||||
} else if (m->role == CST_ROLE_USER) {
|
||||
fprintf(fp, "## User\n\n%s\n\n", c);
|
||||
for (int k = 0; k < m->nattach; k++) {
|
||||
char d[128];
|
||||
cst_attach_describe(&m->attach[k], d, sizeof(d));
|
||||
fprintf(fp, "- image `%s` (%s)\n", m->attach[k].name, d);
|
||||
}
|
||||
if (m->nattach)
|
||||
fputc('\n', fp);
|
||||
} else {
|
||||
fprintf(fp, "## Assistant\n\n%s\n\n", c);
|
||||
if (m->tool_calls_json && m->tool_calls_json[0]) {
|
||||
fprintf(fp, "**Tool calls**\n\n");
|
||||
md_fence(fp, m->tool_calls_json, 4000);
|
||||
}
|
||||
}
|
||||
fputs(md, fp);
|
||||
free(md);
|
||||
if (fclose(fp) != 0) {
|
||||
push_system_line(t, "Error: cannot write %s", path);
|
||||
return;
|
||||
}
|
||||
cst_session_unlock(s);
|
||||
fclose(fp);
|
||||
push_system_line(t, "(exported to %s)", path);
|
||||
}
|
||||
|
||||
@@ -4915,19 +4865,39 @@ static void report_attached(tui_t *t, int added) {
|
||||
cst_agent_config(t->agent)->model);
|
||||
}
|
||||
|
||||
/* /attach: stage images for the next user message. The bytes are copied into
|
||||
* the session's attachment directory now, so a picture picked from /tmp keeps
|
||||
* working after the file is gone. */
|
||||
/* /attach paste: the clipboard image if there is one, else its text goes
|
||||
* into the composer (CR dropped, control bytes kept out, INPUT_CAP respected). */
|
||||
static int paste_text_into_input(tui_t *t, const char *text, size_t n) {
|
||||
char *buf = malloc(n + 1);
|
||||
if (!buf)
|
||||
return 0;
|
||||
size_t k = 0;
|
||||
for (size_t i = 0; i < n && t->in_len + k + 1 < INPUT_CAP; i++) {
|
||||
unsigned char c = (unsigned char)text[i];
|
||||
if (c == '\r' || (c < 0x20 && c != '\n' && c != '\t'))
|
||||
continue;
|
||||
buf[k++] = (char)c;
|
||||
}
|
||||
if (k)
|
||||
input_insert(t, buf, k);
|
||||
free(buf);
|
||||
return (int)k;
|
||||
}
|
||||
|
||||
/* /attach: stage files for the next user message -- images, PDFs (reduced to
|
||||
* their text) and text. The payload is copied into the session's attachment
|
||||
* directory now, so a file picked from /tmp keeps working after it is gone. */
|
||||
static void cmd_attach(tui_t *t, char *arg, int *quit) {
|
||||
(void)quit;
|
||||
int n = cst_agent_pending_count(t->agent);
|
||||
if (!arg || !arg[0] || strcmp(arg, "list") == 0) {
|
||||
if (n == 0) {
|
||||
push_system_line(t, "No images picked. Usage: /attach <path>");
|
||||
push_system_line(t, "Nothing attached yet. Usage: /attach <path>, "
|
||||
"/attach paste, /attach list, /attach clear");
|
||||
return;
|
||||
}
|
||||
push_system_line(t, "%d image%s with the next message (sent once):", n,
|
||||
n > 1 ? "s" : "");
|
||||
push_system_line(t, "%d attachment%s with the next message (sent once):",
|
||||
n, n > 1 ? "s" : "");
|
||||
for (int i = 0; i < n; i++) {
|
||||
char line[256];
|
||||
if (cst_agent_pending_describe(t->agent, i, line, sizeof(line)) == 0)
|
||||
@@ -4938,7 +4908,7 @@ static void cmd_attach(tui_t *t, char *arg, int *quit) {
|
||||
}
|
||||
if (strcmp(arg, "clear") == 0 || strcmp(arg, "none") == 0) {
|
||||
cst_agent_pending_clear(t->agent);
|
||||
push_system_line(t, "Dropped the picked images.");
|
||||
push_system_line(t, "Dropped the attachments.");
|
||||
return;
|
||||
}
|
||||
char err[256];
|
||||
@@ -4946,7 +4916,24 @@ static void cmd_attach(tui_t *t, char *arg, int *quit) {
|
||||
char *img = NULL;
|
||||
size_t len = 0;
|
||||
if (cst_clipboard_image(&img, &len, err, sizeof(err)) != 0) {
|
||||
push_system_line(t, "Error: %s", err);
|
||||
/* no image: put the clipboard text in the composer instead */
|
||||
char *text = NULL;
|
||||
size_t tn = 0;
|
||||
char terr[192];
|
||||
if (cst_clipboard_text(&text, &tn, terr, sizeof(terr)) != 0) {
|
||||
push_system_line(t, "Error: %s", terr);
|
||||
return;
|
||||
}
|
||||
int added = paste_text_into_input(t, text, tn);
|
||||
free(text);
|
||||
if (added <= 0) {
|
||||
push_system_line(t, "(no room in the composer for the clipboard "
|
||||
"text)");
|
||||
return;
|
||||
}
|
||||
push_system_line(t, "(pasted %d bytes of clipboard text into the "
|
||||
"composer)", added);
|
||||
layout_maybe_grow_input(t);
|
||||
return;
|
||||
}
|
||||
char mime[32], name[32];
|
||||
@@ -4971,19 +4958,29 @@ static void cmd_attach(tui_t *t, char *arg, int *quit) {
|
||||
report_attached(t, 1);
|
||||
}
|
||||
|
||||
static int file_is_image(const char *path) {
|
||||
/* A file worth attaching: an image, a PDF, or something that reads as text.
|
||||
* Deciding here in the TUI only avoids turning a random dropped binary into
|
||||
* a pasted path; the store checks the content for real. */
|
||||
static int file_is_attachable(const char *path) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f)
|
||||
return 0;
|
||||
unsigned char head[16];
|
||||
unsigned char head[64];
|
||||
size_t got = fread(head, 1, sizeof(head), f);
|
||||
fclose(f);
|
||||
char mime[32];
|
||||
return cst_attach_sniff(head, got, mime, sizeof(mime)) == 0;
|
||||
if (cst_attach_sniff(head, got, mime, sizeof(mime)) == 0)
|
||||
return 1;
|
||||
if (got >= 4 && memcmp(head, "%PDF", 4) == 0)
|
||||
return 1;
|
||||
for (size_t i = 0; i < got; i++)
|
||||
if (head[i] == 0 || head[i] < 0x09)
|
||||
return 0;
|
||||
return got > 0;
|
||||
}
|
||||
|
||||
/* A paste that is nothing but image file paths (a file dropped on the
|
||||
* terminal) attaches those files instead of typing the paths. */
|
||||
/* A paste that is nothing but file paths (a file dropped on the terminal)
|
||||
* attaches those files instead of typing the paths. */
|
||||
static void paste_image_paths(tui_t *t) {
|
||||
size_t from = t->paste_at, to = t->cur;
|
||||
if (to <= from || to - from != t->in_len - t->paste_len0 ||
|
||||
@@ -4995,7 +4992,7 @@ static void paste_image_paths(tui_t *t) {
|
||||
if (n <= 0)
|
||||
return;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (!file_is_image(paths[i]))
|
||||
if (!file_is_attachable(paths[i]))
|
||||
return;
|
||||
input_erase(t, from, to);
|
||||
int added = 0;
|
||||
@@ -5072,14 +5069,7 @@ static void cmd_init(tui_t *t, char *arg, int *quit) {
|
||||
(void)arg;
|
||||
(void)quit;
|
||||
free(t->pending_prompt);
|
||||
t->pending_prompt = xstrdup(
|
||||
"Analyze this repository and write an AGENTS.md file at the workspace "
|
||||
"root for future coding agents. Cover: what the project is, how to "
|
||||
"build, test and run it (exact commands), the directory layout, coding "
|
||||
"conventions you can infer from the code, and anything surprising. "
|
||||
"Explore with list_dir/glob/grep/read_file before writing. Keep it "
|
||||
"under 150 lines. If AGENTS.md already exists, improve it rather than "
|
||||
"starting over.");
|
||||
t->pending_prompt = xstrdup(cst_init_prompt());
|
||||
}
|
||||
|
||||
static void cmd_compact(tui_t *t, char *arg, int *quit) {
|
||||
@@ -5375,6 +5365,151 @@ static void cmd_quit(tui_t *t, char *arg, int *quit) {
|
||||
*quit = 1;
|
||||
}
|
||||
|
||||
/* /web: the WebUI is a second front end for the same engine and is designed to
|
||||
* run as its own process, so this starts one on the same workspace and leaves
|
||||
* the TUI holding the terminal. The child gets a fresh session: two writers on
|
||||
* one session file is the one thing that would corrupt it. */
|
||||
#ifndef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <sys/wait.h> /* waitpid: collect the WebUI started by the last /web */
|
||||
#endif
|
||||
#ifndef CST_WEBUI_DEFAULT_PORT
|
||||
#define CST_WEBUI_DEFAULT_PORT 8787 /* keep in sync with include/cst_webui.h */
|
||||
#endif
|
||||
static void cmd_web(tui_t *t, char *arg, int *quit) {
|
||||
(void)arg;
|
||||
(void)quit;
|
||||
#ifdef _WIN32
|
||||
push_system_line(t, "open another terminal and run: cst-agent --web");
|
||||
#else
|
||||
static pid_t spawned = 0;
|
||||
if (spawned > 0) { /* collect the one we started last time */
|
||||
waitpid(spawned, NULL, WNOHANG);
|
||||
spawned = 0;
|
||||
}
|
||||
char dir[4096], exe[4096 + 16];
|
||||
if (os_exe_dir(dir, sizeof(dir)) != 0) {
|
||||
push_system_line(t, "cannot locate this binary: run cst-agent --web yourself");
|
||||
return;
|
||||
}
|
||||
snprintf(exe, sizeof(exe), "%s/cst-agent", dir);
|
||||
const cst_agent_config_t *cfg = cst_agent_config(t->agent);
|
||||
const char *ws = (cfg && cfg->workspace_root[0]) ? cfg->workspace_root : ".";
|
||||
char logf[4096 + 32];
|
||||
snprintf(logf, sizeof(logf), "%s/.agent", ws);
|
||||
os_mkdir_p(logf, 0755);
|
||||
snprintf(logf, sizeof(logf), "%s/.agent/webui.log", ws);
|
||||
/* The WebUI talks to what this session talks to (-b, -m, a /provider
|
||||
* switch...). The key goes through the environment, not argv (ps). */
|
||||
const char *argv[20];
|
||||
int na = 0;
|
||||
argv[na++] = "cst-agent";
|
||||
argv[na++] = "--web";
|
||||
argv[na++] = "--workspace";
|
||||
argv[na++] = ws;
|
||||
argv[na++] = "--new";
|
||||
if (cfg && cfg->provider[0])
|
||||
argv[na++] = "--provider", argv[na++] = cfg->provider;
|
||||
if (cfg && cfg->model[0])
|
||||
argv[na++] = "--model", argv[na++] = cfg->model;
|
||||
if (cfg && cfg->api_base[0])
|
||||
argv[na++] = "--api-base", argv[na++] = cfg->api_base;
|
||||
if (cfg && cfg->effort[0])
|
||||
argv[na++] = "--effort", argv[na++] = cfg->effort;
|
||||
if (cfg)
|
||||
argv[na++] = "--approval", argv[na++] = cst_approval_name(cfg->approval);
|
||||
argv[na] = NULL;
|
||||
size_t nenv = 0;
|
||||
while (environ[nenv])
|
||||
nenv++;
|
||||
char **envp = calloc(nenv + 2, sizeof(char *));
|
||||
char keyvar[600] = "";
|
||||
if (!envp) {
|
||||
push_system_line(t, "out of memory");
|
||||
return;
|
||||
}
|
||||
size_t ne = 0;
|
||||
for (size_t i = 0; i < nenv; i++)
|
||||
if (strncmp(environ[i], "CST_API_KEY=", 12) != 0)
|
||||
envp[ne++] = environ[i];
|
||||
if (cfg && cfg->api_key[0]) {
|
||||
snprintf(keyvar, sizeof(keyvar), "CST_API_KEY=%s", cfg->api_key);
|
||||
envp[ne++] = keyvar;
|
||||
}
|
||||
envp[ne] = NULL;
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
free(envp);
|
||||
push_system_line(t, "cannot start a second process");
|
||||
return;
|
||||
}
|
||||
if (pid == 0) {
|
||||
setsid();
|
||||
/* never inherit the TUI's terminal: its output would land on our screen */
|
||||
int in = open("/dev/null", O_RDONLY);
|
||||
if (in >= 0 && in != STDIN_FILENO) {
|
||||
dup2(in, STDIN_FILENO);
|
||||
close(in);
|
||||
}
|
||||
int fd = open(logf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd < 0)
|
||||
fd = open("/dev/null", O_WRONLY);
|
||||
if (fd >= 0) {
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
if (fd > 2)
|
||||
close(fd);
|
||||
}
|
||||
execve(exe, (char *const *)argv, envp);
|
||||
_exit(127);
|
||||
}
|
||||
free(envp);
|
||||
spawned = pid;
|
||||
/* Wait (2 s at most) for the URL it prints once it listens: the port may
|
||||
* come from CST_WEB_PORT, or it may not start at all. */
|
||||
char url[512] = "";
|
||||
int died = 0;
|
||||
for (int i = 0; i < 40 && !url[0] && !died; i++) {
|
||||
usleep(50000);
|
||||
FILE *fp = fopen(logf, "r");
|
||||
char line[512];
|
||||
while (fp && fgets(line, sizeof(line), fp)) {
|
||||
if (strncmp(line, "webui: http", 11) == 0) {
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
snprintf(url, sizeof(url), "%s", line + 7);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
if (!url[0] && waitpid(pid, NULL, WNOHANG) == pid) {
|
||||
died = 1;
|
||||
spawned = 0;
|
||||
}
|
||||
}
|
||||
/* its --new made the WebUI's session CURRENT; a plain `cst-agent` later
|
||||
* should still resume this one */
|
||||
char sid[128] = "";
|
||||
cst_agent_session_id(t->agent, sid, sizeof(sid));
|
||||
if (sid[0])
|
||||
cst_agent_store_set_current(ws, sid);
|
||||
char msg[4200];
|
||||
if (died)
|
||||
snprintf(msg, sizeof(msg), "the WebUI did not start (port busy?): see %s",
|
||||
logf);
|
||||
else if (url[0])
|
||||
snprintf(msg, sizeof(msg), "WebUI started in the background: %s (new session)",
|
||||
url);
|
||||
else
|
||||
snprintf(msg, sizeof(msg), "WebUI starting in the background "
|
||||
"(http://127.0.0.1:%d/ unless CST_WEB_PORT says "
|
||||
"otherwise)", CST_WEBUI_DEFAULT_PORT);
|
||||
push_system_line(t, msg);
|
||||
snprintf(msg, sizeof(msg), "its log: %s", logf);
|
||||
push_system_line(t, msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
static const slash_cmd_t SLASH[] = {
|
||||
{"help", "?", "", "list commands and keys", 1, cmd_help},
|
||||
{"new", "clear", "", "start a new session", 0, cmd_new},
|
||||
@@ -5393,6 +5528,8 @@ static const slash_cmd_t SLASH[] = {
|
||||
cmd_protocol},
|
||||
{"tools", "", "[on|off NAME|minimal]", "list or toggle tools", 0,
|
||||
cmd_tools},
|
||||
{"mode", "", "[coding|general]", "coding or general (non-code) framing", 1,
|
||||
cmd_mode},
|
||||
{"permissions", "perms", "[reset]", "show saved always-allow rules", 1,
|
||||
cmd_permissions},
|
||||
{"sandbox", "", "[reset-labels]", "shell sandbox and labeled folders", 1,
|
||||
@@ -5416,8 +5553,8 @@ static const slash_cmd_t SLASH[] = {
|
||||
{"export", "", "[file]", "write the transcript as Markdown", 1,
|
||||
cmd_export},
|
||||
{"copy", "", "", "copy the last reply (OSC 52)", 1, cmd_copy},
|
||||
{"attach", "image", "[path|paste|clear]",
|
||||
"send image(s) with the next message",
|
||||
{"attach", "image|file", "[path|paste|clear]",
|
||||
"send files (image, PDF, text) or clipboard text with the next message",
|
||||
1, cmd_attach},
|
||||
{"init", "", "", "have the agent write AGENTS.md", 0, cmd_init},
|
||||
{"details", "", "", "expand/collapse all tool output", 1, cmd_details},
|
||||
@@ -5433,6 +5570,8 @@ static const slash_cmd_t SLASH[] = {
|
||||
cmd_agents},
|
||||
{"skill", "", "<name> [task]", "run a task with a skill loaded", 0,
|
||||
cmd_skill},
|
||||
{"web", "", "", "start the WebUI in the background (127.0.0.1:8787)", 1,
|
||||
cmd_web},
|
||||
{"quit", "q exit", "", "quit", 1, cmd_quit},
|
||||
};
|
||||
|
||||
@@ -5854,7 +5993,7 @@ static int submit_input(tui_t *t, int busy) {
|
||||
static size_t input_hit(const tui_t *t, int want_row, int want_col) {
|
||||
int row = 0, col = IN_COL;
|
||||
int right = (t->in_w > 0 ? t->in_w : t->cols) - 2;
|
||||
size_t i = 0, best = t->in_len;
|
||||
size_t i = 0, best = t->in_len, last = 0;
|
||||
while (i < t->in_len) {
|
||||
if (row == want_row && col >= want_col)
|
||||
return i;
|
||||
@@ -5873,12 +6012,14 @@ static size_t input_hit(const tui_t *t, int want_row, int want_col) {
|
||||
utf8_decode(t->inbuf + i, t->in_len - i, &wc, &eaten);
|
||||
int cw = utf8_width(wc == L'\t' ? L' ' : wc);
|
||||
if (col + cw > right && col > IN_COL) {
|
||||
/* offset i would be drawn at the start of the next row */
|
||||
if (row == want_row)
|
||||
return i;
|
||||
return last;
|
||||
row++;
|
||||
col = IN_COL;
|
||||
}
|
||||
best = i + eaten;
|
||||
last = i;
|
||||
col += cw;
|
||||
i += eaten;
|
||||
}
|
||||
@@ -6395,29 +6536,65 @@ static int g_k_alt_up, g_k_alt_down, g_k_ctrl_home, g_k_ctrl_end,
|
||||
g_k_ctrl_up, g_k_ctrl_down, g_k_ctrl_left, g_k_ctrl_right, g_k_alt_left,
|
||||
g_k_alt_right, g_k_ctrl_dc, g_k_alt_dc;
|
||||
|
||||
static int ext_key(const char *cap) {
|
||||
/* The terminfo key, else the xterm sequence bound to our own code `own`. */
|
||||
static int ext_key(const char *cap, const char *xterm, int own) {
|
||||
char *s = tigetstr(cap);
|
||||
if (!s || s == (char *)-1)
|
||||
return 0;
|
||||
int k = key_defined(s);
|
||||
return k > 0 ? k : 0;
|
||||
int k = s && s != (char *)-1 ? key_defined(s) : 0;
|
||||
if (k > 0)
|
||||
return k;
|
||||
#ifndef _WIN32
|
||||
k = key_defined(xterm);
|
||||
if (k > 0)
|
||||
return k;
|
||||
if (k == 0 && define_key(xterm, own) == OK)
|
||||
return own;
|
||||
#else
|
||||
(void)xterm, (void)own;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(NCURSES_VERSION) && !defined(_WIN32)
|
||||
/* Terminfo lists one form per key, but terminals disagree: cursor keys come
|
||||
* as CSI or SS3 whatever DECCKM says (tmux, mosh, a wrong TERM), Home/End as
|
||||
* CSI H, SS3 H or CSI 1~/7~. Unknown forms would be read as Esc + text. */
|
||||
static void vt_keys_init(void) {
|
||||
static const struct {
|
||||
const char *seq;
|
||||
int key;
|
||||
} K[] = {
|
||||
{"\033[A", KEY_UP}, {"\033OA", KEY_UP}, {"\033[B", KEY_DOWN},
|
||||
{"\033OB", KEY_DOWN}, {"\033[C", KEY_RIGHT}, {"\033OC", KEY_RIGHT},
|
||||
{"\033[D", KEY_LEFT}, {"\033OD", KEY_LEFT}, {"\033[H", KEY_HOME},
|
||||
{"\033OH", KEY_HOME}, {"\033[1~", KEY_HOME}, {"\033[7~", KEY_HOME},
|
||||
{"\033[F", KEY_END}, {"\033OF", KEY_END}, {"\033[4~", KEY_END},
|
||||
{"\033[8~", KEY_END}, {"\033[2~", KEY_IC}, {"\033[3~", KEY_DC},
|
||||
{"\033[5~", KEY_PPAGE}, {"\033[6~", KEY_NPAGE}, {"\033[Z", KEY_BTAB},
|
||||
{"\033[1;2A", KEY_SR}, {"\033[1;2B", KEY_SF}, {"\033[1;2C", KEY_SRIGHT},
|
||||
{"\033[1;2D", KEY_SLEFT},
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(K) / sizeof(K[0]); i++)
|
||||
if (key_defined(K[i].seq) == 0)
|
||||
define_key(K[i].seq, K[i].key);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void keys_init(void) {
|
||||
#ifdef NCURSES_VERSION
|
||||
g_k_alt_up = ext_key("kUP3");
|
||||
g_k_alt_down = ext_key("kDN3");
|
||||
g_k_ctrl_home = ext_key("kHOM5");
|
||||
g_k_ctrl_end = ext_key("kEND5");
|
||||
g_k_ctrl_up = ext_key("kUP5");
|
||||
g_k_ctrl_down = ext_key("kDN5");
|
||||
g_k_ctrl_left = ext_key("kLFT5");
|
||||
g_k_ctrl_right = ext_key("kRIT5");
|
||||
g_k_alt_left = ext_key("kLFT3");
|
||||
g_k_alt_right = ext_key("kRIT3");
|
||||
g_k_ctrl_dc = ext_key("kDC5");
|
||||
g_k_alt_dc = ext_key("kDC3");
|
||||
int own = KEY_OWN_BASE + 4; /* up to KEY_KITTY_BASE - 1 */
|
||||
g_k_alt_up = ext_key("kUP3", "\033[1;3A", own++);
|
||||
g_k_alt_down = ext_key("kDN3", "\033[1;3B", own++);
|
||||
g_k_ctrl_home = ext_key("kHOM5", "\033[1;5H", own++);
|
||||
g_k_ctrl_end = ext_key("kEND5", "\033[1;5F", own++);
|
||||
g_k_ctrl_up = ext_key("kUP5", "\033[1;5A", own++);
|
||||
g_k_ctrl_down = ext_key("kDN5", "\033[1;5B", own++);
|
||||
g_k_ctrl_left = ext_key("kLFT5", "\033[1;5D", own++);
|
||||
g_k_ctrl_right = ext_key("kRIT5", "\033[1;5C", own++);
|
||||
g_k_alt_left = ext_key("kLFT3", "\033[1;3D", own++);
|
||||
g_k_alt_right = ext_key("kRIT3", "\033[1;3C", own++);
|
||||
g_k_ctrl_dc = ext_key("kDC5", "\033[3;5~", own++);
|
||||
g_k_alt_dc = ext_key("kDC3", "\033[3;3~", own++);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -6630,6 +6807,7 @@ static void tui_free(tui_t *t) {
|
||||
queue_free_all(t);
|
||||
for (int i = 0; i < t->prompt_hist_n; i++)
|
||||
free(t->prompt_hist[i]);
|
||||
free(t->hist_draft);
|
||||
for (int i = 0; i < t->nfiles; i++)
|
||||
free(t->files[i]);
|
||||
free(t->files);
|
||||
@@ -6720,6 +6898,7 @@ int cst_tui_run(cst_agent_t *agent) {
|
||||
/* the Windows console driver reads key events, not VT sequences */
|
||||
define_key("\033[200~", KEY_PASTE_BEGIN);
|
||||
define_key("\033[201~", KEY_PASTE_END);
|
||||
vt_keys_init();
|
||||
printf("\033[?2004h");
|
||||
fflush(stdout);
|
||||
kitty_init();
|
||||
@@ -7032,15 +7211,22 @@ int cst_tui_run(cst_agent_t *agent) {
|
||||
if (t.prompt_hist_n <= 0)
|
||||
continue;
|
||||
int idx = t.hist_browse;
|
||||
if (idx < 0) {
|
||||
free(t.hist_draft);
|
||||
t.hist_draft = strdup(t.inbuf);
|
||||
}
|
||||
idx = idx < 0 ? t.prompt_hist_n - 1 : (idx > 0 ? idx - 1 : 0);
|
||||
hist_load_into_input(&t, idx);
|
||||
} else {
|
||||
if (t.hist_browse < 0)
|
||||
continue;
|
||||
if (t.hist_browse + 1 >= t.prompt_hist_n)
|
||||
input_clear(&t);
|
||||
else
|
||||
if (t.hist_browse + 1 >= t.prompt_hist_n) {
|
||||
input_set(&t, t.hist_draft);
|
||||
free(t.hist_draft);
|
||||
t.hist_draft = NULL;
|
||||
} else {
|
||||
hist_load_into_input(&t, t.hist_browse + 1);
|
||||
}
|
||||
}
|
||||
after_edit(&t);
|
||||
continue;
|
||||
|
||||
+20
@@ -99,6 +99,26 @@ size_t cst_utf8_floor(const char *s, size_t n) {
|
||||
return i - 1; /* sequence incomplete: cut before lead byte */
|
||||
}
|
||||
|
||||
char *cst_utf8_hold_feed(cst_utf8_hold_t *h, const char *s, size_t n,
|
||||
size_t *out_len) {
|
||||
size_t have = h->n;
|
||||
char *all = malloc(have + n + 1);
|
||||
if (!all)
|
||||
return NULL;
|
||||
memcpy(all, h->b, have);
|
||||
if (n)
|
||||
memcpy(all + have, s, n);
|
||||
size_t len = have + n;
|
||||
size_t cut = cst_utf8_floor(all, len);
|
||||
if (len - cut > sizeof(h->b))
|
||||
cut = len;
|
||||
h->n = len - cut;
|
||||
memcpy(h->b, all + cut, h->n);
|
||||
all[cut] = 0;
|
||||
*out_len = cut;
|
||||
return all;
|
||||
}
|
||||
|
||||
static size_t utf8_seq_ok(const unsigned char *p, size_t left) {
|
||||
unsigned char c = p[0];
|
||||
size_t need;
|
||||
|
||||
+329
@@ -0,0 +1,329 @@
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_web_md.h"
|
||||
|
||||
#include "cst_util.h"
|
||||
#include "sashidown.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct cst_web_md {
|
||||
sashidown_stream_t *st;
|
||||
cst_web_md_emit_fn emit;
|
||||
void *ud;
|
||||
cst_buf_t in; /* HTML from sashidown, waiting for a complete tag */
|
||||
int failed;
|
||||
};
|
||||
|
||||
/* --- sanitizer ------------------------------------------------------------ */
|
||||
|
||||
static const char *ALLOWED_TAGS[] = {
|
||||
"p", "br", "hr", "h1", "h2", "h3", "h4",
|
||||
"h5", "h6", "strong", "em", "b", "i", "u",
|
||||
"s", "del", "code", "pre", "blockquote", "ul", "ol",
|
||||
"li", "table", "thead", "tbody", "tr", "th", "td",
|
||||
"a", "img", "span", "div", NULL};
|
||||
|
||||
static int tag_allowed(const char *name, size_t n) {
|
||||
for (int i = 0; ALLOWED_TAGS[i]; i++) {
|
||||
if (strlen(ALLOWED_TAGS[i]) == n && strncmp(name, ALLOWED_TAGS[i], n) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A URL we are willing to put in href: http(s), mailto, or a same-site
|
||||
* path. Anything else (javascript:, data:, file:, //evil) is dropped. */
|
||||
static int href_ok(const char *v, size_t n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (!strncmp(v, "http://", 7) || !strncmp(v, "https://", 8) ||
|
||||
!strncmp(v, "mailto:", 7))
|
||||
return 1;
|
||||
if (v[0] == '/' && !(n > 1 && (v[1] == '/' || v[1] == '\\')))
|
||||
return 1;
|
||||
if (v[0] == '#')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int src_ok(const char *v, size_t n) {
|
||||
if (n > 11 && !strncmp(v, "data:image/", 11))
|
||||
return 1;
|
||||
if (n > 12 && !strncmp(v, "/api/attach/", 12))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void attr_append(cst_buf_t *b, const char *name, size_t nlen,
|
||||
const char *val, size_t vlen) {
|
||||
cst_buf_puts(b, " ");
|
||||
cst_buf_append(b, name, nlen);
|
||||
cst_buf_puts(b, "=\"");
|
||||
/* a single-quoted or bare value may hold '"': it must not end ours */
|
||||
for (size_t i = 0; i < vlen; i++) {
|
||||
if (val[i] == '"')
|
||||
cst_buf_puts(b, """);
|
||||
else if (val[i] == '<')
|
||||
cst_buf_puts(b, "<");
|
||||
else if (val[i] == '>')
|
||||
cst_buf_puts(b, ">");
|
||||
else
|
||||
cst_buf_append(b, val + i, 1);
|
||||
}
|
||||
cst_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
/* Rewrite one tag (open, close or self-closing) into out; may emit nothing
|
||||
* when the tag is not allowed. Returns 0, or -1 on OOM. */
|
||||
static int sanitize_tag(const char *tag, size_t len, cst_buf_t *out) {
|
||||
/* <name ...> or </name> or <!-- ... --> */
|
||||
size_t i = 0;
|
||||
int closing = 0;
|
||||
if (len < 3 || tag[0] != '<')
|
||||
return 0;
|
||||
i = 1;
|
||||
if (i < len && tag[i] == '/') {
|
||||
closing = 1;
|
||||
i++;
|
||||
}
|
||||
size_t name_start = i;
|
||||
while (i < len && ((tag[i] >= 'a' && tag[i] <= 'z') ||
|
||||
(tag[i] >= 'A' && tag[i] <= 'Z') ||
|
||||
(tag[i] >= '0' && tag[i] <= '9')))
|
||||
i++;
|
||||
size_t name_len = i - name_start;
|
||||
if (name_len == 0 || !tag_allowed(tag + name_start, name_len))
|
||||
return 0; /* comments, <script>, <iframe>: dropped entirely */
|
||||
int is_img = name_len == 3 && strncmp(tag + name_start, "img", 3) == 0;
|
||||
int is_a = name_len == 1 && tag[name_start] == 'a';
|
||||
int self_closing = len > 2 && tag[len - 2] == '/';
|
||||
size_t start = out->len;
|
||||
cst_buf_puts(out, "<");
|
||||
if (closing)
|
||||
cst_buf_puts(out, "/");
|
||||
cst_buf_append(out, tag + name_start, name_len);
|
||||
if (!closing) {
|
||||
/* copy the allowed attributes */
|
||||
int keep_target = 0; /* a link is only useful with a usable href */
|
||||
size_t p = name_start + name_len;
|
||||
while (p < len && tag[p] != '>' && !(tag[p] == '/' && p + 1 < len &&
|
||||
tag[p + 1] == '>')) {
|
||||
while (p < len && (tag[p] == ' ' || tag[p] == '\t' || tag[p] == '\n'))
|
||||
p++;
|
||||
size_t an = p;
|
||||
while (p < len && tag[p] != '=' && tag[p] != ' ' && tag[p] != '\t' &&
|
||||
tag[p] != '\n' && tag[p] != '>' && tag[p] != '/')
|
||||
p++;
|
||||
size_t an_len = p - an;
|
||||
if (an_len == 0) {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
const char *aval = "";
|
||||
size_t av_len = 0;
|
||||
if (p < len && tag[p] == '=') {
|
||||
p++;
|
||||
char quote = 0;
|
||||
if (p < len && (tag[p] == '"' || tag[p] == '\''))
|
||||
quote = tag[p++];
|
||||
size_t as = p;
|
||||
while (p < len &&
|
||||
(quote ? tag[p] != quote : (tag[p] != ' ' && tag[p] != '>')))
|
||||
p++;
|
||||
aval = tag + as;
|
||||
av_len = p - as;
|
||||
if (quote && p < len)
|
||||
p++; /* closing quote */
|
||||
}
|
||||
int keep = 0;
|
||||
if (is_a && an_len == 4 && strncmp(tag + an, "href", 4) == 0) {
|
||||
keep = href_ok(aval, av_len);
|
||||
keep_target = keep;
|
||||
} else if (is_img && an_len == 3 && strncmp(tag + an, "src", 3) == 0) {
|
||||
keep = src_ok(aval, av_len);
|
||||
keep_target = keep;
|
||||
} else if (is_img && an_len == 3 && strncmp(tag + an, "alt", 3) == 0)
|
||||
keep = 1;
|
||||
else if (an_len == 5 && strncmp(tag + an, "class", 5) == 0 &&
|
||||
av_len > 9 && !strncmp(aval, "language-", 9))
|
||||
keep = 1;
|
||||
if (keep)
|
||||
attr_append(out, tag + an, an_len, aval, av_len);
|
||||
}
|
||||
if (is_a && keep_target) {
|
||||
cst_buf_puts(out, " target=\"_blank\" rel=\"noopener noreferrer\"");
|
||||
}
|
||||
if ((is_a && !keep_target) || (is_img && !keep_target)) {
|
||||
/* a link with an unusable href, or an image from nowhere: drop the
|
||||
* whole tag and keep only its text */
|
||||
out->len = start;
|
||||
if (out->buf)
|
||||
out->buf[start] = 0;
|
||||
return 0;
|
||||
}
|
||||
cst_buf_puts(out, self_closing ? " /" : "");
|
||||
}
|
||||
cst_buf_puts(out, ">");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Emit the text before every '<' and every tag through the sanitizer. */
|
||||
static void scan_chunk(cst_web_md_t *s, int final) {
|
||||
if (!s->emit || s->failed)
|
||||
return;
|
||||
size_t i = 0, emitted = 0;
|
||||
while (i < s->in.len) {
|
||||
if (s->in.buf[i] != '<') {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
size_t close = i;
|
||||
while (close < s->in.len && s->in.buf[close] != '>')
|
||||
close++;
|
||||
if (close >= s->in.len && !final)
|
||||
break; /* the tag may still be growing: keep it buffered */
|
||||
if (i > emitted && s->emit(s->in.buf + emitted, i - emitted, s->ud) != 0)
|
||||
s->failed = 1;
|
||||
size_t tag_len = close >= s->in.len ? s->in.len - i : close - i + 1;
|
||||
cst_buf_t tag = {0};
|
||||
if (sanitize_tag(s->in.buf + i, tag_len, &tag) != 0) {
|
||||
cst_buf_free(&tag);
|
||||
s->failed = 1;
|
||||
return;
|
||||
}
|
||||
if (tag.len && s->emit(tag.buf, tag.len, s->ud) != 0)
|
||||
s->failed = 1;
|
||||
cst_buf_free(&tag);
|
||||
i = i + tag_len;
|
||||
emitted = i;
|
||||
if (s->failed)
|
||||
break;
|
||||
}
|
||||
if (emitted > 0) {
|
||||
memmove(s->in.buf, s->in.buf + emitted, s->in.len - emitted);
|
||||
s->in.len -= emitted;
|
||||
s->in.buf[s->in.len] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Collector used by the one-shot sanitizer. */
|
||||
typedef struct {
|
||||
cst_buf_t *b;
|
||||
int failed;
|
||||
} md_collect_t;
|
||||
|
||||
static int collect_emit(const char *html, size_t len, void *ud) {
|
||||
md_collect_t *c = ud;
|
||||
if (cst_buf_append(c->b, html, len) != 0) {
|
||||
c->failed = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *cst_web_md_sanitize(const char *html, size_t len) {
|
||||
cst_web_md_t tmp;
|
||||
memset(&tmp, 0, sizeof(tmp));
|
||||
cst_buf_t out = {0};
|
||||
md_collect_t col = {&out, 0};
|
||||
if (cst_buf_append(&tmp.in, html ? html : "", len) != 0)
|
||||
return NULL;
|
||||
tmp.emit = collect_emit;
|
||||
tmp.ud = &col;
|
||||
scan_chunk(&tmp, 1);
|
||||
cst_buf_free(&tmp.in);
|
||||
if (col.failed) {
|
||||
cst_buf_free(&out);
|
||||
return NULL;
|
||||
}
|
||||
return cst_buf_steal(&out);
|
||||
}
|
||||
|
||||
/* --- sashidown glue ------------------------------------------------------- */
|
||||
|
||||
static int on_sashidown_html(const char *html, size_t len, void *user) {
|
||||
cst_web_md_t *s = user;
|
||||
if (cst_buf_append(&s->in, html, len) != 0) {
|
||||
s->failed = 1;
|
||||
return 1;
|
||||
}
|
||||
scan_chunk(s, 0);
|
||||
return s->failed ? 1 : 0;
|
||||
}
|
||||
|
||||
cst_web_md_t *cst_web_md_new(cst_web_md_emit_fn emit, void *ud) {
|
||||
cst_web_md_t *s = calloc(1, sizeof(*s));
|
||||
if (!s)
|
||||
return NULL;
|
||||
sashidown_stream_opts_t opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.on_html = on_sashidown_html;
|
||||
opts.user = s;
|
||||
s->st = sashidown_stream_create(&opts);
|
||||
if (!s->st) {
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
s->emit = emit;
|
||||
s->ud = ud;
|
||||
return s;
|
||||
}
|
||||
|
||||
int cst_web_md_feed(cst_web_md_t *s, const char *data, size_t len) {
|
||||
if (!s || !s->st)
|
||||
return -1;
|
||||
if (len && sashidown_stream_feed(s->st, data, len) != 0) {
|
||||
s->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return s->failed ? -1 : 0;
|
||||
}
|
||||
|
||||
int cst_web_md_finish(cst_web_md_t *s) {
|
||||
if (!s || !s->st)
|
||||
return -1;
|
||||
if (sashidown_stream_finish(s->st) != 0) {
|
||||
s->failed = 1;
|
||||
return -1;
|
||||
}
|
||||
scan_chunk(s, 1);
|
||||
return s->failed ? -1 : 0;
|
||||
}
|
||||
|
||||
void cst_web_md_free(cst_web_md_t *s) {
|
||||
if (!s)
|
||||
return;
|
||||
if (s->st)
|
||||
sashidown_stream_destroy(s->st);
|
||||
cst_buf_free(&s->in);
|
||||
free(s);
|
||||
}
|
||||
|
||||
char *cst_web_md_html(const char *md, size_t len) {
|
||||
if (!md)
|
||||
return NULL;
|
||||
size_t cap = 65536;
|
||||
char *out = NULL;
|
||||
for (int attempt = 0; attempt < 8; attempt++) {
|
||||
char *buf = malloc(cap);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
size_t out_len = 0;
|
||||
if (sashidown_convert(md, len, buf, cap, &out_len, SASHIDOWN_FMT_HTML) ==
|
||||
0) {
|
||||
out = cst_web_md_sanitize(buf, out_len);
|
||||
free(buf);
|
||||
return out;
|
||||
}
|
||||
free(buf);
|
||||
if (cap >= (size_t)8 * 1024 * 1024)
|
||||
break;
|
||||
cap *= 2;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
+2663
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
#include "cst_agent.h"
|
||||
#include "cst_webui.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef CST_NO_WEBUI
|
||||
int cst_webui_run(cst_agent_t *agent, const cst_webui_opts_t *opts) {
|
||||
(void)agent;
|
||||
(void)opts;
|
||||
fprintf(stderr,
|
||||
"the WebUI was not built into this binary (CST_BUILD_WEBUI=OFF).\n"
|
||||
"Use --headless --prompt '...' or build with make CST_WEBUI=1\n");
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <poll.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@@ -177,7 +178,7 @@ static CST_TEST_UNUSED void test_sleep_ms(int ms) {
|
||||
#ifdef _WIN32
|
||||
Sleep((DWORD)ms);
|
||||
#else
|
||||
usleep((useconds_t)ms * 1000);
|
||||
poll(NULL, 0, ms); /* usleep is hidden by -std=c11 on musl */
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -325,6 +325,100 @@ static void test_import_file(const char *root) {
|
||||
"missing file refused");
|
||||
}
|
||||
|
||||
/* Documents: text is stored as text, a PDF as its text layer while the mime
|
||||
* still says PDF, and neither is an image as far as the wire is concerned. */
|
||||
static void test_documents(const char *root) {
|
||||
char session[1024], src[1024];
|
||||
snprintf(session, sizeof(session), "%.900s/sessions/doc.jsonl", root);
|
||||
char err[256] = "";
|
||||
|
||||
const char *text = "# Notes\n\nFive painters, one canvas.\n";
|
||||
snprintf(src, sizeof(src), "%.900s/notes.md", root);
|
||||
FILE *f = fopen(src, "wb");
|
||||
EXPECT(f != NULL, "write the text file");
|
||||
if (f) {
|
||||
fwrite(text, 1, strlen(text), f);
|
||||
fclose(f);
|
||||
}
|
||||
cst_attachment_t *a = cst_attach_import_file(session, src, err, sizeof(err));
|
||||
EXPECT(a != NULL, "text file imported");
|
||||
if (a) {
|
||||
EXPECT(strcmp(a->mime, "text/plain") == 0, "text mime");
|
||||
EXPECT(!cst_attach_is_image(a) && cst_attach_is_text(a), "not an image");
|
||||
EXPECT(a->pages == 0, "no pages for text");
|
||||
EXPECT(strstr(a->rel, ".txt") != NULL, "text stored as .txt");
|
||||
EXPECT(a->bytes == (long long)strlen(text), "text size");
|
||||
size_t n = 0;
|
||||
char *back = cst_attach_read(session, a, &n, err, sizeof(err));
|
||||
EXPECT(back && n == strlen(text) && memcmp(back, text, n) == 0,
|
||||
"text reads back exactly");
|
||||
free(back);
|
||||
EXPECT(cst_attach_data_url(session, a) == NULL,
|
||||
"a document has no data url");
|
||||
char desc[128];
|
||||
cst_attach_describe(a, desc, sizeof(desc));
|
||||
EXPECT(strstr(desc, "text") != NULL, "describe says text");
|
||||
cst_attach_free(a);
|
||||
}
|
||||
|
||||
/* a one-page PDF with a text layer */
|
||||
static const char PDF[] =
|
||||
"%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R "
|
||||
">> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\n"
|
||||
"endobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT /F1 12 Tf 72 720 Td (Attached paper abstract) Tj ET\n"
|
||||
"endstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n";
|
||||
snprintf(src, sizeof(src), "%.900s/paper.pdf", root);
|
||||
f = fopen(src, "wb");
|
||||
EXPECT(f != NULL, "write the PDF");
|
||||
if (f) {
|
||||
fwrite(PDF, 1, strlen(PDF), f);
|
||||
fclose(f);
|
||||
}
|
||||
err[0] = 0;
|
||||
a = cst_attach_import_file(session, src, err, sizeof(err));
|
||||
EXPECT(a != NULL, "PDF imported");
|
||||
if (a) {
|
||||
EXPECT(strcmp(a->mime, "application/pdf") == 0, "the PDF keeps its mime");
|
||||
EXPECT(a->pages == 1, "page count comes from the extractor");
|
||||
EXPECT(strstr(a->rel, ".txt") != NULL, "the text layer is what is stored");
|
||||
EXPECT(cst_attach_is_text(a), "documents are text on the wire");
|
||||
char desc[128];
|
||||
cst_attach_describe(a, desc, sizeof(desc));
|
||||
EXPECT(strstr(desc, "pdf") != NULL && strstr(desc, "1 page") != NULL,
|
||||
"describe names the PDF and its pages");
|
||||
size_t n = 0;
|
||||
char *back = cst_attach_read(session, a, &n, err, sizeof(err));
|
||||
EXPECT(back && strstr(back, "Attached paper abstract") != NULL,
|
||||
"the extracted text reads back");
|
||||
free(back);
|
||||
cst_attach_free(a);
|
||||
}
|
||||
err[0] = 0;
|
||||
EXPECT(cst_attach_import_file(session, "/nonexistent/x.pdf", err,
|
||||
sizeof(err)) == NULL,
|
||||
"a missing PDF is refused");
|
||||
|
||||
/* neither image, PDF nor text: a binary blob */
|
||||
static const char binary[] = "\x01\x02\x03\x00\x04\x05";
|
||||
snprintf(src, sizeof(src), "%.900s/blob.bin", root);
|
||||
f = fopen(src, "wb");
|
||||
if (f) {
|
||||
fwrite(binary, 1, sizeof(binary), f);
|
||||
fclose(f);
|
||||
}
|
||||
err[0] = 0;
|
||||
EXPECT(cst_attach_import_file(session, src, err, sizeof(err)) == NULL,
|
||||
"a binary file is refused");
|
||||
EXPECT(strstr(err, "not an image") != NULL, "err says what was expected");
|
||||
}
|
||||
|
||||
static void test_json_roundtrip(void) {
|
||||
cst_attachment_t list[3];
|
||||
memset(list, 0, sizeof(list));
|
||||
@@ -344,6 +438,8 @@ static void test_json_roundtrip(void) {
|
||||
list[1].bytes = 4096;
|
||||
snprintf(list[1].detail, sizeof(list[1].detail), "high");
|
||||
snprintf(list[1].origin, sizeof(list[1].origin), "view_image");
|
||||
snprintf(list[1].mime, sizeof(list[1].mime), "application/pdf");
|
||||
list[1].pages = 12;
|
||||
|
||||
snprintf(list[2].id, sizeof(list[2].id), "img-3");
|
||||
snprintf(list[2].name, sizeof(list[2].name), "c.webp");
|
||||
@@ -365,6 +461,9 @@ static void test_json_roundtrip(void) {
|
||||
EXPECT(strcmp(back[1].detail, "high") == 0 &&
|
||||
strcmp(back[1].origin, "view_image") == 0,
|
||||
"detail and origin survive");
|
||||
EXPECT(strcmp(back[1].mime, "application/pdf") == 0 && back[1].pages == 12,
|
||||
"a document keeps its mime and page count");
|
||||
EXPECT(back[2].pages == 0, "pages default to 0");
|
||||
EXPECT(strcmp(back[2].mime, "image/webp") == 0 && back[2].width == 0,
|
||||
"entry without dimensions");
|
||||
EXPECT(strcmp(back[0].detail, "auto") == 0, "missing detail defaults");
|
||||
@@ -530,6 +629,7 @@ int main(void) {
|
||||
test_dib();
|
||||
test_store(root);
|
||||
test_import_file(root);
|
||||
test_documents(root);
|
||||
test_json_roundtrip();
|
||||
test_lists();
|
||||
test_agent_pending(root);
|
||||
|
||||
@@ -106,11 +106,13 @@ static void test_settings(void) {
|
||||
snprintf(cfg.provider, sizeof(cfg.provider), "openai");
|
||||
snprintf(cfg.model, sizeof(cfg.model), "gpt-5");
|
||||
snprintf(cfg.effort, sizeof(cfg.effort), "max");
|
||||
snprintf(cfg.mode, sizeof(cfg.mode), "general");
|
||||
CHECK(cst_settings_remember(&cfg) == 0);
|
||||
|
||||
CHECK(cst_settings_load(&s) == 0);
|
||||
CHECK(strcmp(s.provider, "openai") == 0);
|
||||
CHECK(strcmp(s.effort, "max") == 0);
|
||||
CHECK(strcmp(s.mode, "general") == 0);
|
||||
CHECK(strcmp(cst_settings_model(&s, "deepseek"), "deepseek-v4-pro") == 0);
|
||||
CHECK(strcmp(cst_settings_model(&s, "openai"), "gpt-5") == 0);
|
||||
CHECK(s.nmodels == 2);
|
||||
@@ -145,6 +147,24 @@ static void test_effort(void) {
|
||||
free(body);
|
||||
}
|
||||
|
||||
static void test_mode(void) {
|
||||
CHECK(cst_mode_valid("coding") && cst_mode_valid("general"));
|
||||
CHECK(!cst_mode_valid("code") && !cst_mode_valid(NULL));
|
||||
CHECK(!cst_mode_valid(""));
|
||||
CHECK(strcmp(cst_mode_name("general"), "general") == 0);
|
||||
CHECK(strcmp(cst_mode_name(NULL), CST_DEFAULT_MODE) == 0);
|
||||
CHECK(strcmp(cst_mode_name("nonsense"), "coding") == 0);
|
||||
|
||||
CHECK(cst_settings_remember_mode("general") == 0);
|
||||
cst_settings_t s;
|
||||
CHECK(cst_settings_load(&s) == 0);
|
||||
CHECK(strcmp(s.mode, "general") == 0);
|
||||
/* an unknown value is recorded as the default, not as itself */
|
||||
CHECK(cst_settings_remember_mode("junk") == 0);
|
||||
CHECK(cst_settings_load(&s) == 0);
|
||||
CHECK(strcmp(s.mode, "coding") == 0);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
CHECK(test_tmpdir(g_dir, sizeof(g_dir), "cst-config") == 0);
|
||||
setenv("XDG_CONFIG_HOME", g_dir, 1);
|
||||
@@ -158,6 +178,7 @@ int main(void) {
|
||||
test_creds();
|
||||
test_settings();
|
||||
test_effort();
|
||||
test_mode();
|
||||
|
||||
CHECK(test_rmrf(g_dir) == 0);
|
||||
printf("test_config: OK\n");
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cJSON.h"
|
||||
#include "cst_agent.h"
|
||||
#include "cst_attach.h"
|
||||
#include "cst_test_util.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -325,8 +327,197 @@ static void test_images_off(void) {
|
||||
cJSON_Delete(j);
|
||||
}
|
||||
|
||||
/* Every element of every content array must be a typed part object. */
|
||||
static int parts_typed(const cJSON *root) {
|
||||
const cJSON *m;
|
||||
cJSON_ArrayForEach(m, cJSON_GetObjectItemCaseSensitive(root, "messages")) {
|
||||
const cJSON *c = cJSON_GetObjectItemCaseSensitive(m, "content");
|
||||
if (!cJSON_IsArray(c))
|
||||
continue;
|
||||
const cJSON *p;
|
||||
cJSON_ArrayForEach(p, c) {
|
||||
const cJSON *t = cJSON_GetObjectItemCaseSensitive(p, "type");
|
||||
if (!cJSON_IsObject(p) || !cJSON_IsString(t))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void test_images_on(void) {
|
||||
char dir[1024], sess[1100], err[256];
|
||||
if (test_tmpdir(dir, sizeof(dir), "cst-body") != 0) {
|
||||
EXPECT(0, "scratch dir");
|
||||
return;
|
||||
}
|
||||
snprintf(sess, sizeof(sess), "%s/s.jsonl", dir);
|
||||
static const unsigned char png[32] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1A,
|
||||
'\n', 0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
||||
0, 0, 0, 2, 0, 0, 0, 2};
|
||||
cst_attachment_t *img = cst_attach_import_bytes(sess, "shot.png",
|
||||
(const char *)png,
|
||||
sizeof(png), err, sizeof(err));
|
||||
EXPECT(img != NULL, "image stored");
|
||||
if (!img)
|
||||
return;
|
||||
cst_attachment_t gone = *img;
|
||||
snprintf(gone.rel, sizeof(gone.rel), "missing.png");
|
||||
cst_message_t m[5];
|
||||
m[0] = msg(CST_ROLE_USER, "what is this");
|
||||
m[0].attach = img;
|
||||
m[0].nattach = 1;
|
||||
m[1] = msg(CST_ROLE_ASSISTANT, "");
|
||||
m[1].tool_calls_json = "[{\"id\":\"c1\",\"type\":\"function\",\"function\":"
|
||||
"{\"name\":\"view_image\",\"arguments\":\"{}\"}}]";
|
||||
m[2] = msg(CST_ROLE_TOOL, "view_image shot.png");
|
||||
snprintf(m[2].tool_call_id, sizeof(m[2].tool_call_id), "c1");
|
||||
m[2].attach = img;
|
||||
m[2].nattach = 1;
|
||||
m[3] = msg(CST_ROLE_USER, "and now?");
|
||||
m[4] = msg(CST_ROLE_USER, "");
|
||||
m[4].attach = &gone;
|
||||
m[4].nattach = 1;
|
||||
cst_llm_request_t r;
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.model = "deepseek-flash";
|
||||
r.msgs = m;
|
||||
r.nmsgs = 5;
|
||||
r.session_path = sess;
|
||||
cJSON *j = build_req(&r);
|
||||
EXPECT(parts_typed(j), "content arrays hold only typed parts");
|
||||
const cJSON *p0 = cJSON_GetObjectItemCaseSensitive(
|
||||
cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(j, "messages"), 0),
|
||||
"content");
|
||||
const cJSON *t0 = cJSON_GetObjectItemCaseSensitive(
|
||||
cJSON_GetArrayItem(p0, 0), "text");
|
||||
const cJSON *u1 = cJSON_GetObjectItemCaseSensitive(
|
||||
cJSON_GetObjectItemCaseSensitive(cJSON_GetArrayItem(p0, 1), "image_url"),
|
||||
"url");
|
||||
EXPECT(cJSON_IsString(t0) && strcmp(t0->valuestring, "what is this") == 0,
|
||||
"user text is a text part");
|
||||
EXPECT(cJSON_IsString(u1) &&
|
||||
strncmp(u1->valuestring, "data:image/png;base64,", 22) == 0,
|
||||
"image is a data URL part");
|
||||
char *s = cJSON_PrintUnformatted(j);
|
||||
EXPECT(s && strstr(s, "and now?") && strstr(s, "not sent"),
|
||||
"follow-up text and the missing-image note are parts too");
|
||||
free(s);
|
||||
cJSON_Delete(j);
|
||||
cst_attach_free(img);
|
||||
}
|
||||
|
||||
/* Every text part of message i, joined (malloc'd, "" when there is none). */
|
||||
static char *text_of(const cJSON *root, int i) {
|
||||
const cJSON *m =
|
||||
cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(root, "messages"), i);
|
||||
const cJSON *c = cJSON_GetObjectItemCaseSensitive(m, "content");
|
||||
if (cJSON_IsString(c)) {
|
||||
size_t n = strlen(c->valuestring);
|
||||
char *out = malloc(n + 1);
|
||||
if (out)
|
||||
memcpy(out, c->valuestring, n + 1);
|
||||
return out;
|
||||
}
|
||||
size_t len = 1;
|
||||
const cJSON *p, *t, *s;
|
||||
cJSON_ArrayForEach(p, c) {
|
||||
t = cJSON_GetObjectItemCaseSensitive(p, "type");
|
||||
s = cJSON_GetObjectItemCaseSensitive(p, "text");
|
||||
if (cJSON_IsString(t) && strcmp(t->valuestring, "text") == 0 &&
|
||||
cJSON_IsString(s))
|
||||
len += strlen(s->valuestring) + 1;
|
||||
}
|
||||
char *out = calloc(1, len);
|
||||
if (!out)
|
||||
return NULL;
|
||||
cJSON_ArrayForEach(p, c) {
|
||||
t = cJSON_GetObjectItemCaseSensitive(p, "type");
|
||||
s = cJSON_GetObjectItemCaseSensitive(p, "text");
|
||||
if (cJSON_IsString(t) && strcmp(t->valuestring, "text") == 0 &&
|
||||
cJSON_IsString(s)) {
|
||||
strcat(out, s->valuestring);
|
||||
strcat(out, "\n");
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* A document is inlined as text, so a text-only model and a message that
|
||||
* also carries an image both keep it; the image is what becomes a note. */
|
||||
static void test_document_parts(void) {
|
||||
char dir[1024], sess[1100], err[256], src[1200];
|
||||
if (test_tmpdir(dir, sizeof(dir), "cst-body-doc") != 0) {
|
||||
EXPECT(0, "scratch dir");
|
||||
return;
|
||||
}
|
||||
snprintf(sess, sizeof(sess), "%s/s.jsonl", dir);
|
||||
snprintf(src, sizeof(src), "%s/paper.pdf", dir);
|
||||
static const char PDF[] =
|
||||
"%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R "
|
||||
">> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\n"
|
||||
"endobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT /F1 12 Tf 72 720 Td (Inlined paper text) Tj ET\n"
|
||||
"endstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n";
|
||||
FILE *f = fopen(src, "wb");
|
||||
EXPECT(f != NULL, "write the PDF");
|
||||
if (!f)
|
||||
return;
|
||||
fwrite(PDF, 1, strlen(PDF), f);
|
||||
fclose(f);
|
||||
cst_attachment_t *doc = cst_attach_import_file(sess, src, err, sizeof(err));
|
||||
EXPECT(doc != NULL, "PDF stored");
|
||||
if (!doc)
|
||||
return;
|
||||
static const unsigned char png[32] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1A,
|
||||
'\n', 0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
||||
0, 0, 0, 2, 0, 0, 0, 2};
|
||||
cst_attachment_t *img = cst_attach_import_bytes(
|
||||
sess, "shot.png", (const char *)png, sizeof(png), err, sizeof(err));
|
||||
EXPECT(img != NULL, "image stored");
|
||||
if (!img) {
|
||||
cst_attach_free(doc);
|
||||
return;
|
||||
}
|
||||
cst_attachment_t both[2] = {*doc, *img};
|
||||
cst_message_t m[2];
|
||||
m[0] = msg(CST_ROLE_USER, "what does this say?");
|
||||
m[0].attach = both;
|
||||
m[0].nattach = 2;
|
||||
m[1] = msg(CST_ROLE_USER, "thanks");
|
||||
cst_llm_request_t r;
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.model = "deepseek-flash";
|
||||
r.msgs = m;
|
||||
r.nmsgs = 2;
|
||||
r.images_off = 1; /* a text-only model */
|
||||
r.session_path = sess;
|
||||
cJSON *j = build_req(&r);
|
||||
char *txt = text_of(j, 0);
|
||||
EXPECT(txt && strstr(txt, "[document \"paper.pdf\"") &&
|
||||
strstr(txt, "Inlined paper text") &&
|
||||
strstr(txt, "[-- end of document --]"),
|
||||
"the PDF text is inlined even when images are off");
|
||||
EXPECT(txt && strstr(txt, "[image \"shot.png\"") && strstr(txt, "not sent"),
|
||||
"the image beside it is still only a note");
|
||||
free(txt);
|
||||
char *s = cJSON_PrintUnformatted(j);
|
||||
EXPECT(s && !strstr(s, "image_url"), "no image part with images off");
|
||||
free(s);
|
||||
cJSON_Delete(j);
|
||||
cst_attach_free(doc);
|
||||
cst_attach_free(img);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_images_off();
|
||||
test_images_on();
|
||||
test_document_parts();
|
||||
test_reasoning_and_prefix();
|
||||
test_cache_hints();
|
||||
test_tools_snapshot();
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "cst_test_util.h"
|
||||
#include "os_compat.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -44,8 +45,14 @@ static void test_builtin(void) {
|
||||
EXPECT(mi.has_price && mi.price.miss == 9.0 && mi.price.offpeak_half,
|
||||
"longer match wins the price");
|
||||
EXPECT(strcmp(mi.source, "built-in") == 0, "source built-in");
|
||||
cst_model_info("deepseek", "deepseek-flash", &mi);
|
||||
EXPECT(mi.context == 1000000 && mi.vision == 1, "deepseek-flash takes images");
|
||||
cst_model_info("deepseek", "deepseek-chat", &mi);
|
||||
EXPECT(mi.vision == 1, "deepseek-chat is deepseek-flash");
|
||||
cst_model_info(NULL, "gpt-4o-mini-2024", &mi);
|
||||
EXPECT(mi.price.miss == 0.15 && mi.context == 128000 && mi.vision == 1,
|
||||
/* x87 (i686) compares the literal at long double precision: no == here */
|
||||
EXPECT(fabs(mi.price.miss - 0.15) < 1e-12 && mi.context == 128000 &&
|
||||
mi.vision == 1,
|
||||
"gpt-4o-mini over gpt-4o");
|
||||
cst_model_info(NULL, "Qwen/Qwen2.5-VL-72B-Instruct", &mi);
|
||||
EXPECT(mi.vision == 1 && mi.context == 131072, "glob *-vl* + qwen prefix");
|
||||
@@ -152,7 +159,7 @@ static void test_cache(void) {
|
||||
strcmp(mi.price.currency, "$") == 0,
|
||||
"API vision and USD price");
|
||||
cst_model_info("deepseek", "deepseek-flash", &mi);
|
||||
EXPECT(mi.vision == 0 && mi.price.miss == 2.0,
|
||||
EXPECT(mi.price.miss == 2.0 && strcmp(mi.price.currency, "¥") == 0,
|
||||
"another provider's cache is not used");
|
||||
EXPECT(cst_model_context_tokens("anthropic/claude-sonnet-4") == 1000000,
|
||||
"no provider: any cache, exact id");
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
/* Text extraction from PDFs: hand-made fixtures for the shapes that matter
|
||||
* (plain content, FlateDecode, CID fonts with ToUnicode CMaps, object
|
||||
* streams) plus the two error paths (encrypted, not a PDF). */
|
||||
|
||||
#include "cst_pdf.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int failures = 0;
|
||||
|
||||
#define CHECK(cond, what) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, what); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static char *extract(const char *pdf, int *pages, char *err, size_t err_sz) {
|
||||
return cst_pdf_text(pdf, strlen(pdf), pages, err, err_sz);
|
||||
}
|
||||
|
||||
static int has(const char *hay, const char *needle) {
|
||||
return hay && strstr(hay, needle) != NULL;
|
||||
}
|
||||
|
||||
/* --- 1. plain, uncompressed content, two lines via Td --------------- */
|
||||
|
||||
static const char PLAIN[] =
|
||||
"%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
|
||||
"/Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT\n/F1 12 Tf\n72 720 Td\n(Line one) Tj\n0 -14 Td\n"
|
||||
"(A \\(paren\\) and a \\\\ backslash) Tj\nET\n"
|
||||
"endstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n";
|
||||
|
||||
static void test_plain(void) {
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = extract(PLAIN, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(pages == 1, "one page");
|
||||
CHECK(has(t, "Line one"), "first line");
|
||||
CHECK(has(t, "A (paren) and a \\ backslash"), "escapes");
|
||||
CHECK(!has(t, "\r"), "no CR left");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* --- 2. FlateDecode content stream ---------------------------------- */
|
||||
|
||||
/* raw deflate (zlib, level 9, no header) of
|
||||
* "BT /F1 12 Tf 72 720 Td (Hello, compressed world) Tj ET\n" */
|
||||
static const unsigned char FLATE[] = {
|
||||
0x73, 0x0a, 0x51, 0xd0, 0x77, 0x33, 0x54, 0x30, 0x34, 0x52, 0x08, 0x49,
|
||||
0x53, 0x30, 0x37, 0x02, 0x22, 0x03, 0x85, 0x90, 0x14, 0x05, 0x0d, 0x8f,
|
||||
0xd4, 0x9c, 0x9c, 0x7c, 0x1d, 0x85, 0xe4, 0xfc, 0xdc, 0x82, 0xa2, 0xd4,
|
||||
0xe2, 0xe2, 0xd4, 0x14, 0x85, 0xf2, 0xfc, 0xa2, 0x9c, 0x14, 0x4d, 0x85,
|
||||
0x90, 0x2c, 0x05, 0xd7, 0x10, 0x2e, 0x00};
|
||||
|
||||
static void test_flate(void) {
|
||||
char pdf[2048];
|
||||
int n = snprintf(pdf, sizeof(pdf),
|
||||
"%%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font "
|
||||
"<< /F1 4 0 R >> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 >>\nendobj\n"
|
||||
"5 0 obj\n<< /Filter /FlateDecode >>\nstream\n");
|
||||
memcpy(pdf + n, FLATE, sizeof(FLATE));
|
||||
n += (int)sizeof(FLATE);
|
||||
n += snprintf(pdf + n, sizeof(pdf) - (size_t)n,
|
||||
"\nendstream\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n");
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = cst_pdf_text(pdf, (size_t)n, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(has(t, "Hello, compressed world"), "inflated text");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* 2b. zlib-framed FlateDecode (RFC 1950), which is what real writers emit:
|
||||
* zlib.compress(b"BT /F1 12 Tf 72 720 Td (Zlib framed hello) Tj ET\n"). */
|
||||
static const unsigned char ZLIB[] = {
|
||||
0x78, 0xda, 0x73, 0x0a, 0x51, 0xd0, 0x77, 0x33, 0x54, 0x30, 0x34, 0x52,
|
||||
0x08, 0x49, 0x53, 0x30, 0x37, 0x02, 0x22, 0x03, 0x85, 0x90, 0x14, 0x05,
|
||||
0x8d, 0xa8, 0x9c, 0xcc, 0x24, 0x85, 0xb4, 0xa2, 0xc4, 0xdc, 0xd4, 0x14,
|
||||
0x85, 0x8c, 0xd4, 0x9c, 0x9c, 0x7c, 0x4d, 0x85, 0x90, 0x2c, 0x05, 0xd7,
|
||||
0x10, 0x2e, 0x00, 0x33, 0x0f, 0x0d, 0x3a};
|
||||
|
||||
static void test_zlib_flate(void) {
|
||||
char pdf[2048];
|
||||
int n = snprintf(pdf, sizeof(pdf),
|
||||
"%%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font "
|
||||
"<< /F1 4 0 R >> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 >>\nendobj\n"
|
||||
"5 0 obj\n<< /Filter /FlateDecode >>\nstream\n");
|
||||
memcpy(pdf + n, ZLIB, sizeof(ZLIB));
|
||||
n += (int)sizeof(ZLIB);
|
||||
n += snprintf(pdf + n, sizeof(pdf) - (size_t)n,
|
||||
"\nendstream\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n");
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = cst_pdf_text(pdf, (size_t)n, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(has(t, "Zlib framed hello"), "zlib header skipped, text inflated");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* 2c. an inline image: its bytes are not text and must be skipped. */
|
||||
|
||||
static const char INLINE_IMG[] =
|
||||
"%PDF-1.4\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R "
|
||||
">> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT /F1 12 Tf 72 720 Td (Before the image) Tj ET\n"
|
||||
"q 20 0 0 20 72 700 cm\n"
|
||||
"BI /W 8 /H 8 /CS /RGB /BPC 8 /F /AHx ID\n"
|
||||
"00ff0000ff00ff00ff00ff00 8123<0001> EI Q\n"
|
||||
"BT /F1 12 Tf 72 680 Td (After the image) Tj ET\n"
|
||||
"endstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n";
|
||||
|
||||
static void test_inline_image(void) {
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = extract(INLINE_IMG, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(has(t, "Before the image"), "text before the inline image");
|
||||
CHECK(has(t, "After the image"), "text after it is still found");
|
||||
CHECK(!has(t, "00ff0000"), "the image bytes were skipped");
|
||||
CHECK(!has(t, "8123"), "its hex data was skipped too");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* --- 3. CID font with a ToUnicode CMap ------------------------------ */
|
||||
|
||||
static const char CID[] =
|
||||
"%PDF-1.5\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"3 0 obj\n<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R "
|
||||
">> >> /Contents 5 0 R >>\nendobj\n"
|
||||
"4 0 obj\n<< /Type /Font /Subtype /Type0 /Encoding /Identity-H "
|
||||
"/ToUnicode 6 0 R >>\nendobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT /F1 12 Tf 72 720 Td <0001000200030004> Tj ET\n"
|
||||
"endstream\nendobj\n"
|
||||
"6 0 obj\n<< >>\nstream\n"
|
||||
"1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n"
|
||||
"2 beginbfchar\n<0001> <4F60>\n<0002> <597D>\nendbfchar\n"
|
||||
"1 beginbfrange\n<0003> <0004> <0041>\nendbfrange\n"
|
||||
"endcmap\nendstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n";
|
||||
|
||||
static void test_cid(void) {
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = extract(CID, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(has(t, "\xe4\xbd\xa0\xe5\xa5\xbd"), "bfchar maps to 你好");
|
||||
CHECK(has(t, "AB"), "bfrange walks the code points");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* --- 4. objects inside an object stream ----------------------------- */
|
||||
|
||||
static void test_objstm(void) {
|
||||
char pdf[4096];
|
||||
const char *page = "<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 "
|
||||
"4 0 R >> >> /Contents 5 0 R >>";
|
||||
const char *font = "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>";
|
||||
/* the stream is "num1 off1 num2 off2" -- /N and /First are in the dict --
|
||||
* and offsets count from /First, which is where the bodies start, so
|
||||
* /First is the length of the pairs list itself */
|
||||
char hdr[64];
|
||||
int off4 = (int)strlen(page) + 1; /* object 4 follows the page */
|
||||
int first = snprintf(hdr, sizeof(hdr), "3 0 4 %d ", off4);
|
||||
int n = snprintf(pdf, sizeof(pdf),
|
||||
"%%PDF-1.5\n"
|
||||
"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
|
||||
"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
|
||||
"5 0 obj\n<< >>\nstream\n"
|
||||
"BT /F1 12 Tf 72 720 Td (Inside an object stream) Tj ET\n"
|
||||
"endstream\nendobj\n"
|
||||
"6 0 obj\n<< /Type /ObjStm /N 2 /First %d >>\nstream\n"
|
||||
"%s%s\n%s",
|
||||
first, hdr, page, font);
|
||||
n += snprintf(pdf + n, sizeof(pdf) - (size_t)n,
|
||||
"\nendstream\nendobj\n"
|
||||
"trailer\n<< /Root 1 0 R >>\n%%EOF\n");
|
||||
char err[256] = "";
|
||||
int pages = 0;
|
||||
char *t = cst_pdf_text(pdf, (size_t)n, &pages, err, sizeof(err));
|
||||
CHECK(t != NULL, err);
|
||||
CHECK(pages == 1, "one page through the object stream");
|
||||
CHECK(has(t, "Inside an object stream"), "text of the page in the ObjStm");
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* --- 5. error paths -------------------------------------------------- */
|
||||
|
||||
static void test_errors(void) {
|
||||
char err[256] = "";
|
||||
char *t = extract("%PDF-1.7\ntrailer\n<< /Root 1 0 R /Encrypt 9 0 R >>\n",
|
||||
NULL, err, sizeof(err));
|
||||
CHECK(t == NULL, "encrypted file is refused");
|
||||
CHECK(has(err, "encrypted"), "says it is encrypted");
|
||||
|
||||
err[0] = 0;
|
||||
t = extract("hello, not a pdf at all\n", NULL, err, sizeof(err));
|
||||
CHECK(t == NULL, "non-PDF is refused");
|
||||
CHECK(has(err, "not a PDF"), "says it is not a PDF");
|
||||
|
||||
err[0] = 0;
|
||||
t = extract("%PDF-1.4\n1 0 obj\n<< >>\nendobj\n", NULL, err, sizeof(err));
|
||||
CHECK(t == NULL, "a PDF without text is refused");
|
||||
CHECK(has(err, "no extractable text") || has(err, "no readable pages"),
|
||||
"explains the missing text");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_plain();
|
||||
test_flate();
|
||||
test_zlib_flate();
|
||||
test_inline_image();
|
||||
test_cid();
|
||||
test_objstm();
|
||||
test_errors();
|
||||
if (failures) {
|
||||
fprintf(stderr, "test_pdf_text: %d failure(s)\n", failures);
|
||||
return 1;
|
||||
}
|
||||
printf("test_pdf_text: OK\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -210,7 +210,7 @@ static void test_vision(const char *root) {
|
||||
snprintf(cfg.workspace_root, sizeof(cfg.workspace_root), "%s", root);
|
||||
snprintf(cfg.session_path, sizeof(cfg.session_path), "%s/blind.jsonl", dir);
|
||||
snprintf(cfg.api_base, sizeof(cfg.api_base), "http://replay/v1");
|
||||
snprintf(cfg.model, sizeof(cfg.model), "deepseek-flash");
|
||||
snprintf(cfg.model, sizeof(cfg.model), "deepseek-v4-pro");
|
||||
snprintf(cfg.provider, sizeof(cfg.provider), "deepseek");
|
||||
cfg.protocol = CST_PROTO_NATIVE_EOS;
|
||||
cfg.max_steps = 4;
|
||||
@@ -220,7 +220,7 @@ static void test_vision(const char *root) {
|
||||
EXPECT(a != NULL, "blind agent");
|
||||
if (!a)
|
||||
return;
|
||||
EXPECT(cst_agent_sees_images(a) == 0, "deepseek-flash is text only");
|
||||
EXPECT(cst_agent_sees_images(a) == 0, "deepseek-v4-pro is text only");
|
||||
EXPECT(cst_agent_attach_bytes(a, "shot.png", (const char *)png, pn, err,
|
||||
sizeof(err)) == 0,
|
||||
"attach to a blind model");
|
||||
|
||||
+5
-10
@@ -72,7 +72,7 @@ static void test_blocked(void) {
|
||||
CHECK(cst_sandbox_blocked(c, strlen(c), 1));
|
||||
const char *d = "curl: (7) Failed to connect to example.com port 443 after 9 "
|
||||
"ms: Could not connect to server\n";
|
||||
CHECK(cst_sandbox_blocked(d, strlen(d), 7));
|
||||
CHECK(!cst_sandbox_blocked(d, strlen(d), 7)); /* network is not sandboxed */
|
||||
const char *e = "Access is denied.\r\n";
|
||||
CHECK(cst_sandbox_blocked(e, strlen(e), 1));
|
||||
const char *f = "拒绝访问。\r\n";
|
||||
@@ -132,7 +132,7 @@ static int run(const char *ws, const char *cmd, os_run_result_t *r) {
|
||||
return r->exit_code;
|
||||
}
|
||||
|
||||
/* Child: enter the sandbox, then try a TCP connect to a local listener. */
|
||||
/* Child: enter the sandbox, then a TCP connect to a local listener works. */
|
||||
static void test_network(const char *ws) {
|
||||
int ls = socket(AF_INET, SOCK_STREAM, 0);
|
||||
CHECK(ls >= 0);
|
||||
@@ -152,12 +152,11 @@ static void test_network(const char *ws) {
|
||||
if (pid == 0) {
|
||||
if (cst_sandbox_enter(&plan) != 0)
|
||||
_exit(3);
|
||||
/* Landlock refuses the connect, seccomp already the socket */
|
||||
int s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (s < 0)
|
||||
_exit(errno == EACCES ? 0 : 2);
|
||||
_exit(errno == EACCES ? 1 : 2);
|
||||
int rc = connect(s, (struct sockaddr *)&a, sizeof(a));
|
||||
_exit(rc == 0 ? 1 : errno == EACCES ? 0 : 2);
|
||||
_exit(rc == 0 ? 0 : errno == EACCES ? 1 : 2);
|
||||
}
|
||||
int st = 0;
|
||||
waitpid(pid, &st, 0);
|
||||
@@ -337,11 +336,7 @@ static void test_confined(int kind) {
|
||||
CHECK(fp);
|
||||
fclose(fp);
|
||||
|
||||
if (kind == CST_SANDBOX_SECCOMP || cst_sandbox_landlock_abi() >= 4)
|
||||
test_network(ws);
|
||||
else
|
||||
printf("test_sandbox: landlock ABI %d < 4, network rule not tested\n",
|
||||
cst_sandbox_landlock_abi());
|
||||
test_network(ws);
|
||||
|
||||
snprintf(cmd, sizeof(cmd), "rm -rf '%s' '%s'", ws, out);
|
||||
CHECK(system(cmd) == 0);
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
/* WebUI: the socket/HTTP layer, the SSE framing, auth and the HTML
|
||||
* sanitizer. The full turn path is covered by scripts/e2e_webui.sh, which
|
||||
* runs a recorded stream through a real server. */
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
|
||||
#include "cst_http.h"
|
||||
#include "cst_net.h"
|
||||
#include "cst_util.h"
|
||||
#include "cst_web_md.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cst_test_util.h"
|
||||
|
||||
#define CHECK(c) \
|
||||
do { \
|
||||
if (!(c)) { \
|
||||
fprintf(stderr, "%s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, #c); \
|
||||
exit(1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* --- a tiny HTTP client --------------------------------------------------- */
|
||||
|
||||
static os_socket_t connect_to(int port) {
|
||||
char host[32];
|
||||
snprintf(host, sizeof(host), "127.0.0.1");
|
||||
os_socket_t s = os_net_connect(host, port, 3000);
|
||||
CHECK(s != OS_SOCKET_BAD);
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Sends data, reads until the peer closes (or 5 s pass). malloc'd. */
|
||||
static char *roundtrip(int port, const char *req, size_t *out_len) {
|
||||
os_socket_t s = connect_to(port);
|
||||
CHECK(os_net_send_all(s, req, strlen(req), 3000) == 0);
|
||||
cst_buf_t b = {0};
|
||||
char buf[4096];
|
||||
for (;;) {
|
||||
long n = os_net_recv(s, buf, sizeof(buf), 5000);
|
||||
if (n <= 0)
|
||||
break;
|
||||
CHECK(cst_buf_append(&b, buf, (size_t)n) == 0);
|
||||
}
|
||||
os_net_close(s);
|
||||
*out_len = b.len;
|
||||
return cst_buf_steal(&b);
|
||||
}
|
||||
|
||||
/* Same, but stops as soon as `needle` shows up. */
|
||||
|
||||
/* --- handlers ------------------------------------------------------------- */
|
||||
|
||||
static int h_hello(cst_http_conn_t *c, void *ud) {
|
||||
(void)ud;
|
||||
return cst_http_send_str(c, 200, "text/plain", "world");
|
||||
}
|
||||
|
||||
static int h_secret(cst_http_conn_t *c, void *ud) {
|
||||
(void)ud;
|
||||
return cst_http_send_str(c, 200, "text/plain", "secret");
|
||||
}
|
||||
|
||||
static int h_echo(cst_http_conn_t *c, void *ud) {
|
||||
(void)ud;
|
||||
const cst_http_req_t *r = cst_http_request(c);
|
||||
char head[64];
|
||||
snprintf(head, sizeof(head), "%zu:", r->body_len);
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, head);
|
||||
cst_buf_append(&b, r->body ? r->body : "", r->body_len);
|
||||
int rc = cst_http_send(c, 200, "text/plain", b.buf, b.len);
|
||||
cst_buf_free(&b);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int h_events(cst_http_conn_t *c, void *ud) {
|
||||
(void)ud;
|
||||
CHECK(cst_http_stream_begin(c, "text/event-stream") == 0);
|
||||
CHECK(cst_http_stream_write(c, "event: one\ndata: {\"n\":1}\n\n", 26) == 0);
|
||||
CHECK(cst_http_stream_write(c, "event: two\ndata: {\"n\":2}\n\n", 26) == 0);
|
||||
CHECK(cst_http_stream_end(c) == 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int auth_bearer(const cst_http_req_t *req, void *ud) {
|
||||
const char *want = ud;
|
||||
if (!req->authorization)
|
||||
return -1;
|
||||
if (strncmp(req->authorization, "Bearer ", 7) != 0)
|
||||
return -1;
|
||||
return strcmp(req->authorization + 7, want) == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
static void test_http(void) {
|
||||
static const cst_http_route_t routes[] = {
|
||||
{"GET", "/hello", h_hello, NULL, 0, 0},
|
||||
{"GET", "/secret", h_secret, NULL, 1, 0},
|
||||
{"GET", "/events", h_events, NULL, 0, 0},
|
||||
{"POST", "/echo", h_echo, NULL, 0, 4096},
|
||||
};
|
||||
cst_http_opts_t o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.host = "127.0.0.1";
|
||||
o.port = 0;
|
||||
o.max_body = 1024 * 1024;
|
||||
o.routes = routes;
|
||||
o.nroutes = (int)(sizeof(routes) / sizeof(routes[0]));
|
||||
o.auth = auth_bearer;
|
||||
o.auth_ud = (void *)"s3cret";
|
||||
char err[256] = "";
|
||||
cst_http_server_t *s = cst_http_start(&o, err, sizeof(err));
|
||||
CHECK(s != NULL);
|
||||
int port = cst_http_port(s);
|
||||
CHECK(port > 0);
|
||||
|
||||
size_t n = 0;
|
||||
char *resp = roundtrip(port, "GET /hello HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "200 OK") && strstr(resp, "world"));
|
||||
free(resp);
|
||||
|
||||
/* keep-alive: two requests, one connection */
|
||||
resp = roundtrip(port, "GET /hello HTTP/1.1\r\nHost: x\r\n\r\n"
|
||||
"GET /hello HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "200 OK"));
|
||||
CHECK(strstr(resp, "200 OK") != strrchr(resp, '2')); /* twice */
|
||||
{
|
||||
int hits = 0;
|
||||
for (const char *p = resp; (p = strstr(p, "200 OK")); p++)
|
||||
hits++;
|
||||
CHECK(hits == 2);
|
||||
}
|
||||
free(resp);
|
||||
|
||||
/* auth: without, then with the token */
|
||||
resp = roundtrip(port, "GET /secret HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "401"));
|
||||
free(resp);
|
||||
resp = roundtrip(port, "GET /secret HTTP/1.1\r\nHost: x\r\n"
|
||||
"Authorization: Bearer s3cret\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "200") && strstr(resp, "secret"));
|
||||
free(resp);
|
||||
|
||||
/* unknown route */
|
||||
resp = roundtrip(port, "GET /nope HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "404"));
|
||||
free(resp);
|
||||
/* traversal is rejected before any handler runs */
|
||||
resp = roundtrip(port, "GET /../etc/passwd HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "400") || strstr(resp, "404"));
|
||||
free(resp);
|
||||
|
||||
/* a body round trip, and the per-route cap */
|
||||
resp = roundtrip(port, "POST /echo HTTP/1.1\r\nHost: x\r\n"
|
||||
"Content-Length: 3\r\nConnection: close\r\n\r\nabc", &n);
|
||||
CHECK(strstr(resp, "200") && strstr(resp, "3:abc"));
|
||||
free(resp);
|
||||
{
|
||||
cst_buf_t b = {0};
|
||||
cst_buf_puts(&b, "POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 9999\r\n"
|
||||
"Connection: close\r\n\r\n");
|
||||
for (int i = 0; i < 9999; i++)
|
||||
cst_buf_puts(&b, "a");
|
||||
resp = roundtrip(port, b.buf, &n);
|
||||
CHECK(strstr(resp, "413"));
|
||||
free(resp);
|
||||
cst_buf_free(&b);
|
||||
}
|
||||
|
||||
/* chunked streaming: the client must see the decoded frames */
|
||||
resp = roundtrip(port, "GET /events HTTP/1.1\r\nHost: x\r\n"
|
||||
"Connection: close\r\n\r\n", &n);
|
||||
CHECK(strstr(resp, "Transfer-Encoding: chunked"));
|
||||
CHECK(strstr(resp, "event: one") && strstr(resp, "event: two"));
|
||||
CHECK(strstr(resp, "\r\n0\r\n\r\n")); /* terminator */
|
||||
free(resp);
|
||||
|
||||
cst_http_stop(s);
|
||||
cst_http_free(s);
|
||||
}
|
||||
|
||||
/* --- query parsing -------------------------------------------------------- */
|
||||
|
||||
static void test_query(void) {
|
||||
cst_http_req_t r;
|
||||
memset(&r, 0, sizeof(r));
|
||||
snprintf(r.query, sizeof(r.query), "token=a%%20b&since=17&x=");
|
||||
char buf[64];
|
||||
CHECK(cst_http_query(&r, "token", buf, sizeof(buf)) == 0);
|
||||
CHECK(strcmp(buf, "a b") == 0);
|
||||
CHECK(cst_http_query(&r, "since", buf, sizeof(buf)) == 0);
|
||||
CHECK(strcmp(buf, "17") == 0);
|
||||
CHECK(cst_http_query(&r, "nope", buf, sizeof(buf)) != 0);
|
||||
}
|
||||
|
||||
/* --- markdown + sanitizer ------------------------------------------------- */
|
||||
|
||||
static cst_buf_t g_md_out;
|
||||
|
||||
static int collect_md(const char *html, size_t len, void *ud) {
|
||||
(void)ud;
|
||||
return cst_buf_append(&g_md_out, html, len) == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
static void test_sanitize(void) {
|
||||
char *html = cst_web_md_html("**hi** [x](javascript:alert(1)) "
|
||||
"",
|
||||
strlen("**hi** [x](javascript:alert(1)) "
|
||||
""));
|
||||
CHECK(html != NULL);
|
||||
CHECK(strstr(html, "<strong>hi</strong>"));
|
||||
CHECK(strstr(html, "javascript:") == NULL);
|
||||
CHECK(strstr(html, "/api/attach/img-1.png"));
|
||||
free(html);
|
||||
|
||||
/* raw markup that sashidown would pass through gets stripped */
|
||||
const char *evil = "<p>ok</p><script>alert(1)</script>"
|
||||
"<img src=\"http://evil.example/x.png\" alt=\"a\">"
|
||||
"<a href=\"//evil.example\">l</a>";
|
||||
html = cst_web_md_sanitize(evil, strlen(evil));
|
||||
CHECK(html != NULL);
|
||||
CHECK(strstr(html, "ok"));
|
||||
CHECK(strstr(html, "<script") == NULL);
|
||||
CHECK(strstr(html, "evil.example/x.png") == NULL);
|
||||
CHECK(strstr(html, "//evil.example") == NULL);
|
||||
CHECK(strstr(html, "<a") == NULL); /* an unusable href drops the tag */
|
||||
free(html);
|
||||
|
||||
/* a quote inside a single-quoted or bare value cannot open a new attribute */
|
||||
const char *quoted = "<a href='http://x\" onmouseover=\"alert(1)'>q</a>"
|
||||
"<a href=http://y\"onmouseover=alert(2)//>r</a>";
|
||||
html = cst_web_md_sanitize(quoted, strlen(quoted));
|
||||
CHECK(html != NULL);
|
||||
CHECK(strstr(html, "\" onmouseover") == NULL);
|
||||
CHECK(strstr(html, "\"onmouseover") == NULL);
|
||||
CHECK(strstr(html, """) != NULL);
|
||||
free(html);
|
||||
|
||||
/* streaming: a javascript: link is dropped, a split tag never leaks */
|
||||
{
|
||||
cst_buf_free(&g_md_out);
|
||||
const char *src = "see [x](javascript:alert(1)) and **ok**";
|
||||
cst_web_md_t *md = cst_web_md_new(collect_md, NULL);
|
||||
CHECK(md != NULL);
|
||||
CHECK(cst_web_md_feed(md, src, 4) == 0); /* a deliberately split tag */
|
||||
CHECK(cst_web_md_feed(md, src + 4, strlen(src) - 4) == 0);
|
||||
CHECK(cst_web_md_finish(md) == 0);
|
||||
cst_web_md_free(md);
|
||||
const char *out = g_md_out.buf ? g_md_out.buf : "";
|
||||
CHECK(strstr(out, "javascript:") == NULL);
|
||||
CHECK(strstr(out, "href") == NULL);
|
||||
CHECK(strstr(out, "see"));
|
||||
CHECK(strstr(out, "<strong>ok</strong>") != NULL);
|
||||
cst_buf_free(&g_md_out);
|
||||
}
|
||||
{
|
||||
/* and the same text fed one byte at a time still comes out whole */
|
||||
cst_buf_free(&g_md_out);
|
||||
const char *md_src = "**bold** and [ok](https://example.com)";
|
||||
cst_web_md_t *md = cst_web_md_new(collect_md, NULL);
|
||||
CHECK(md != NULL);
|
||||
for (size_t i = 0; i < strlen(md_src); i++)
|
||||
CHECK(cst_web_md_feed(md, md_src + i, 1) == 0);
|
||||
CHECK(cst_web_md_finish(md) == 0);
|
||||
cst_web_md_free(md);
|
||||
CHECK(strstr(g_md_out.buf, "<strong>bold</strong>"));
|
||||
CHECK(strstr(g_md_out.buf, "https://example.com"));
|
||||
CHECK(strstr(g_md_out.buf, "noopener") != NULL);
|
||||
cst_buf_free(&g_md_out);
|
||||
}
|
||||
}
|
||||
|
||||
/* Streamed deltas cut anywhere must reach the browser as whole characters. */
|
||||
static void test_utf8_hold(void) {
|
||||
const char *src = "思考:先读 main.c,再改 😀 emoji。";
|
||||
size_t len = strlen(src);
|
||||
for (size_t step = 1; step <= 5; step++) {
|
||||
cst_utf8_hold_t h = {0};
|
||||
cst_buf_t out = {0};
|
||||
for (size_t i = 0; i < len; i += step) {
|
||||
size_t n = len - i < step ? len - i : step;
|
||||
size_t got = 0;
|
||||
char *piece = cst_utf8_hold_feed(&h, src + i, n, &got);
|
||||
CHECK(piece != NULL);
|
||||
CHECK(h.n < 4);
|
||||
CHECK(cst_utf8_valid(piece, got));
|
||||
CHECK(cst_buf_append(&out, piece, got) == 0);
|
||||
free(piece);
|
||||
}
|
||||
CHECK(h.n == 0);
|
||||
CHECK(out.len == len && memcmp(out.buf, src, len) == 0);
|
||||
cst_buf_free(&out);
|
||||
}
|
||||
/* a stray continuation byte is not held forever */
|
||||
cst_utf8_hold_t h = {0};
|
||||
size_t got = 0;
|
||||
char *piece = cst_utf8_hold_feed(&h, "\x80\x80\x80\x80", 4, &got);
|
||||
CHECK(piece && got == 4 && h.n == 0);
|
||||
free(piece);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
CHECK(os_net_init() == 0);
|
||||
test_utf8_hold();
|
||||
test_query();
|
||||
test_sanitize();
|
||||
test_http();
|
||||
printf("test_webui: OK\n");
|
||||
return 0;
|
||||
}
|
||||
Vendored
+5
-2
@@ -89,8 +89,11 @@ typedef struct {
|
||||
const unsigned char *json;
|
||||
size_t position;
|
||||
} error;
|
||||
/* cst-agent: per thread, parses run concurrently (sub-agents, MCP) */
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
/* cst-agent: per thread, parses run concurrently (sub-agents, MCP).
|
||||
* tcc has no thread-local storage; cst-agent never reads cJSON_GetErrorPtr. */
|
||||
#if defined(__TINYC__)
|
||||
static error global_error = { NULL, 0 };
|
||||
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
static _Thread_local error global_error = { NULL, 0 };
|
||||
#elif defined(__GNUC__)
|
||||
static __thread error global_error = { NULL, 0 };
|
||||
|
||||
+2557
File diff suppressed because it is too large
Load Diff
+250
@@ -0,0 +1,250 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN" data-theme="dark">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<meta name="color-scheme" content="dark light">
|
||||
<title>CST-Agent</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<!-- one icon set, drawn as a sprite so every glyph shares the same 24px grid,
|
||||
stroke weight and round caps: mixed emoji/unicode never looks like a family -->
|
||||
<svg id="atlas" aria-hidden="true" hidden>
|
||||
<symbol id="i-menu" viewBox="0 0 24 24"><path d="M4 7h16M4 12h16M4 17h16"/></symbol>
|
||||
<symbol id="i-panel-left" viewBox="0 0 24 24"><rect x="3.5" y="5" width="17" height="14" rx="3"/><path d="M9.5 5v14"/></symbol>
|
||||
<symbol id="i-panel-right" viewBox="0 0 24 24"><rect x="3.5" y="5" width="17" height="14" rx="3"/><path d="M14.5 5v14"/></symbol>
|
||||
<symbol id="i-chevron" viewBox="0 0 24 24"><path d="M6 9.5l6 5 6-5"/></symbol>
|
||||
<symbol id="i-plus" viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></symbol>
|
||||
<symbol id="i-refresh" viewBox="0 0 24 24"><path d="M20 12a8 8 0 1 1-2.6-5.9M20 4.5V9h-4.5"/></symbol>
|
||||
<symbol id="i-settings" viewBox="0 0 24 24"><path d="M4 6.5h5M15 6.5h5M4 12h7M17 12h3M4 17.5h5M15 17.5h5"/><circle cx="11.5" cy="6.5" r="2"/><circle cx="15" cy="12" r="2"/><circle cx="11.5" cy="17.5" r="2"/></symbol>
|
||||
<symbol id="i-moon" viewBox="0 0 24 24"><path d="M20 14.6A8.6 8.6 0 0 1 9.4 4 8.6 8.6 0 1 0 20 14.6z"/></symbol>
|
||||
<symbol id="i-sun" viewBox="0 0 24 24"><circle cx="12" cy="12" r="4.2"/><path d="M12 2.5v2.2M12 19.3v2.2M2.5 12h2.2M19.3 12h2.2M5.2 5.2l1.6 1.6M17.2 17.2l1.6 1.6M18.8 5.2l-1.6 1.6M6.8 17.2l-1.6 1.6"/></symbol>
|
||||
<symbol id="i-more" viewBox="0 0 24 24"><circle cx="5.5" cy="12" r="1.6" class="f"/><circle cx="12" cy="12" r="1.6" class="f"/><circle cx="18.5" cy="12" r="1.6" class="f"/></symbol>
|
||||
<symbol id="i-send" viewBox="0 0 24 24"><path d="M12 19.5V5M5.8 11.2 12 5l6.2 6.2"/></symbol>
|
||||
<symbol id="i-stop" viewBox="0 0 24 24"><rect x="7" y="7" width="10" height="10" rx="2.5" class="f"/></symbol>
|
||||
<symbol id="i-image" viewBox="0 0 24 24"><rect x="3.5" y="5" width="17" height="14" rx="3"/><path d="M4 15.5 8.6 11l4 4 2.8-2.8L20.5 16"/><circle cx="9" cy="9.4" r="1.5"/></symbol>
|
||||
<symbol id="i-clipboard" viewBox="0 0 24 24"><rect x="8.5" y="3.5" width="7" height="4" rx="1.6"/><path d="M15.5 5.5h3v15h-13v-15h3M9 12h6M9 15.5h4"/></symbol>
|
||||
<symbol id="i-list" viewBox="0 0 24 24"><path d="M4 6.8h2M4 12h2M4 17.2h2M9 6.8h11M9 12h11M9 17.2h11"/></symbol>
|
||||
<symbol id="i-copy" viewBox="0 0 24 24"><rect x="8.5" y="8.5" width="11" height="11" rx="2.6"/><path d="M15.5 8.5V7a2.5 2.5 0 0 0-2.5-2.5H6.5A2.5 2.5 0 0 0 4 7v6.5A2.5 2.5 0 0 0 6.5 16h1.5"/></symbol>
|
||||
<symbol id="i-compress" viewBox="0 0 24 24"><path d="M8 3.5V9H3M16 20.5V15h5M3 9h4M16 15h5"/></symbol>
|
||||
<symbol id="i-undo" viewBox="0 0 24 24"><path d="M9.5 5.5 4 11l5.5 5.5"/><path d="M4 11h10.2a5.2 5.2 0 0 1 0 10.4H8.5"/></symbol>
|
||||
<symbol id="i-close" viewBox="0 0 24 24"><path d="M6.5 6.5l11 11M17.5 6.5l-11 11"/></symbol>
|
||||
<symbol id="i-check" viewBox="0 0 24 24"><path d="M5 12.6l4.6 4.6L19 7.6"/></symbol>
|
||||
<symbol id="i-shield" viewBox="0 0 24 24"><path d="M12 3.5 5 6.2v5.4c0 4.2 2.9 7.5 7 8.9 4.1-1.4 7-4.7 7-8.9V6.2z"/></symbol>
|
||||
<symbol id="i-bolt" viewBox="0 0 24 24"><path d="M13.4 3 5.5 13.6H11l-.9 7.4L18.5 10H13z"/></symbol>
|
||||
<symbol id="i-alert" viewBox="0 0 24 24"><path d="M12 4 2.8 20h18.4z"/><path d="M12 10v4.4M12 17.2v.2"/></symbol>
|
||||
<symbol id="i-cpu" viewBox="0 0 24 24"><rect x="7" y="7" width="10" height="10" rx="2.6"/><path d="M10 3v4M14 3v4M10 17v4M14 17v4M3 10h4M3 14h4M17 10h4M17 14h4"/></symbol>
|
||||
<symbol id="i-lv1" viewBox="0 0 24 24"><path d="M5 18.5V13h3.4v5.5z" class="f"/></symbol>
|
||||
<symbol id="i-lv2" viewBox="0 0 24 24"><path d="M5 18.5V13h3.4v5.5zM10.3 18.5V9.6h3.4v8.9z" class="f"/></symbol>
|
||||
<symbol id="i-lv3" viewBox="0 0 24 24"><path d="M5 18.5V13h3.4v5.5zM10.3 18.5V9.6h3.4v8.9zM15.6 18.5V6.4h3.4v12.1z" class="f"/></symbol>
|
||||
<symbol id="i-lv4" viewBox="0 0 24 24"><path d="M4 18.5V13h2.6v5.5zM8.7 18.5V10h2.6v8.5zM13.4 18.5V6.8h2.6v11.7zM18.1 18.5V4h2.6v14.5z" class="f"/></symbol>
|
||||
<symbol id="i-file" viewBox="0 0 24 24"><path d="M13.5 3.5H7A2.5 2.5 0 0 0 4.5 6v12A2.5 2.5 0 0 0 7 20.5h10A2.5 2.5 0 0 0 19.5 18V9.5z"/><path d="M13.5 3.5v6h6"/></symbol>
|
||||
<symbol id="i-clock" viewBox="0 0 24 24"><circle cx="12" cy="12" r="8.5"/><path d="M12 7.2V12l3.2 2"/></symbol>
|
||||
<symbol id="i-edit" viewBox="0 0 24 24"><path d="M14.8 5.2l4 4L9 19H5v-4z"/><path d="M12.6 7.4l4 4"/></symbol>
|
||||
<symbol id="i-retry" viewBox="0 0 24 24"><path d="M4.5 12a7.5 7.5 0 0 1 13-5.1M19.5 12a7.5 7.5 0 0 1-13 5.1"/><path d="M17.8 3.6v3.6h-3.6M6.2 20.4v-3.6h3.6"/></symbol>
|
||||
<symbol id="i-left" viewBox="0 0 24 24"><path d="M14.5 6l-6 6 6 6"/></symbol>
|
||||
<symbol id="i-right" viewBox="0 0 24 24"><path d="M9.5 6l6 6-6 6"/></symbol>
|
||||
<symbol id="i-folder" viewBox="0 0 24 24"><path d="M3.5 6.5A1.5 1.5 0 0 1 5 5h4l2 2.5h8A1.5 1.5 0 0 1 20.5 9v9A1.5 1.5 0 0 1 19 19.5H5A1.5 1.5 0 0 1 3.5 18z"/></symbol>
|
||||
</svg>
|
||||
|
||||
<div id="app">
|
||||
|
||||
<!-- rail: identity, one primary action, sessions, identity again.
|
||||
Everything else is a popover anchored where it belongs. -->
|
||||
<aside id="side">
|
||||
<div class="side-inner">
|
||||
<div class="side-head">
|
||||
<div class="brand">
|
||||
<span class="brand-name">CST-Agent</span>
|
||||
</div>
|
||||
<button id="btn-collapse" class="icon" title="收起侧栏 (Ctrl+B)">
|
||||
<svg class="i"><use href="#i-panel-left"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button id="btn-new" class="new-task">
|
||||
<svg class="i"><use href="#i-plus"/></svg><span>新建任务</span>
|
||||
</button>
|
||||
|
||||
<div class="side-sec">
|
||||
<span>最近</span>
|
||||
<button id="btn-refresh-sessions" class="icon tiny" title="刷新">
|
||||
<svg class="i"><use href="#i-refresh"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
<input id="sess-search" class="sess-search" type="search" placeholder="搜索会话" autocomplete="off">
|
||||
<div id="sessions-list" class="sessions"></div>
|
||||
|
||||
<div class="side-foot">
|
||||
<div class="who">
|
||||
<span class="avatar">C</span>
|
||||
<span class="who-text">
|
||||
<b id="foot-ws">workspace</b>
|
||||
<i id="foot-session">-</i>
|
||||
</span>
|
||||
</div>
|
||||
<button id="btn-settings" class="icon" title="设置">
|
||||
<svg class="i"><use href="#i-settings"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<!-- main column -->
|
||||
<main id="main">
|
||||
<header id="topbar">
|
||||
<button id="btn-side" class="icon" title="展开侧栏 (Ctrl+B)">
|
||||
<svg class="i"><use href="#i-panel-left"/></svg>
|
||||
</button>
|
||||
<button id="btn-drawer" class="icon only-narrow" title="侧栏">
|
||||
<svg class="i"><use href="#i-menu"/></svg>
|
||||
</button>
|
||||
<button id="chip-model" class="chip model" title="模型与推理强度">
|
||||
<svg class="i"><use href="#i-cpu"/></svg><span class="lbl">-</span>
|
||||
<svg class="i chev"><use href="#i-chevron"/></svg>
|
||||
</button>
|
||||
<span class="grow"></span>
|
||||
<div class="seg" id="seg-mode" title="计划模式只读,不改文件">
|
||||
<button data-mode="build">构建</button>
|
||||
<button data-mode="plan">计划</button>
|
||||
</div>
|
||||
<button id="chip-approval" class="chip quiet" title="审批权限">
|
||||
<svg class="i"><use href="#i-shield"/></svg><span class="lbl">auto</span>
|
||||
<svg class="i chev"><use href="#i-chevron"/></svg>
|
||||
</button>
|
||||
<button id="btn-theme" class="icon" title="明暗切换">
|
||||
<svg class="i only-dark"><use href="#i-moon"/></svg>
|
||||
<svg class="i only-light"><use href="#i-sun"/></svg>
|
||||
</button>
|
||||
<button id="btn-panel" class="icon" title="运行详情 (Ctrl+J)">
|
||||
<svg class="i"><use href="#i-panel-right"/></svg>
|
||||
</button>
|
||||
<button id="btn-menu" class="icon" title="更多">
|
||||
<svg class="i"><use href="#i-more"/></svg>
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div id="scroll">
|
||||
<section id="empty">
|
||||
<p class="ready">准备好开始构建了吗</p>
|
||||
</section>
|
||||
<div id="log"></div>
|
||||
</div>
|
||||
|
||||
<button id="to-bottom" class="round" hidden title="回到最新">
|
||||
<svg class="i"><use href="#i-chevron"/></svg>
|
||||
</button>
|
||||
|
||||
<footer id="composer">
|
||||
<div id="pending" class="pending" hidden></div>
|
||||
<div id="edit-bar" class="edit-bar" hidden>
|
||||
<span>编辑上一条消息:发送后替换它并重新回答(文件改动保留,需要时先撤销)</span>
|
||||
<span class="grow"></span>
|
||||
<button id="btn-edit-cancel" class="link">取消</button>
|
||||
</div>
|
||||
|
||||
<!-- permission gate: inline, the turn really is waiting on it -->
|
||||
<div id="gate" class="gate" hidden>
|
||||
<div class="gate-head">
|
||||
<span class="gate-title" id="gate-title">需要确认</span>
|
||||
<span class="grow"></span>
|
||||
<span class="gate-timer" id="gate-timer"></span>
|
||||
</div>
|
||||
<pre id="gate-detail"></pre>
|
||||
<div class="gate-keys" id="gate-keys"></div>
|
||||
<div class="gate-row" id="gate-buttons"></div>
|
||||
</div>
|
||||
|
||||
<div id="slash" class="slash" role="listbox" hidden></div>
|
||||
<div class="box">
|
||||
<textarea id="input" rows="1" placeholder="给 CST-Agent 派个任务…(/ 打开命令,F1 看快捷键)"></textarea>
|
||||
<div class="box-row">
|
||||
<button id="btn-plus" class="icon att" title="添加图片或计划模式">
|
||||
<svg class="i"><use href="#i-plus"/></svg>
|
||||
</button>
|
||||
<button id="btn-undo" class="chip quiet" title="撤销上一次文件写入">
|
||||
<svg class="i"><use href="#i-undo"/></svg><span>撤销</span>
|
||||
</button>
|
||||
<span class="grow"></span>
|
||||
<button id="cost" class="cost" title="用量详情"></button>
|
||||
<button id="btn-stop" class="icon stop" hidden title="停止">
|
||||
<svg class="i"><use href="#i-stop"/></svg>
|
||||
</button>
|
||||
<button id="btn-send" class="round send" title="发送 (Enter)">
|
||||
<svg class="i"><use href="#i-send"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hint-line">
|
||||
<span id="status">就绪</span>
|
||||
<span class="grow"></span>
|
||||
<span id="usage" class="quiet"></span>
|
||||
</div>
|
||||
<input type="file" id="file" accept="image/*" multiple hidden>
|
||||
</footer>
|
||||
</main>
|
||||
|
||||
<!-- right panel: the run, not the chat -->
|
||||
<aside id="panel">
|
||||
<div class="panel-inner">
|
||||
<div class="panel-head">
|
||||
<span>运行详情</span>
|
||||
<span class="grow"></span>
|
||||
<button id="btn-panel-close" class="icon tiny" title="关闭">
|
||||
<svg class="i"><use href="#i-close"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<section class="card" id="card-todo">
|
||||
<h4>任务清单</h4>
|
||||
<div id="todo-body" class="muted">模型还没有给出任务清单。</div>
|
||||
</section>
|
||||
|
||||
<section class="card" id="card-files">
|
||||
<h4>文件改动 <span class="badge" id="files-count">0</span>
|
||||
<span class="grow"></span><button id="btn-diff" class="link" title="git status + git diff">查看 diff</button></h4>
|
||||
<div id="files-body" class="muted">本轮还没有改动文件。</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h4>上下文</h4>
|
||||
<div class="kv"><span>占用</span><b id="kv-ctx">0</b></div>
|
||||
<div class="kv"><span>缓存命中</span><b id="kv-cache">-</b></div>
|
||||
</section>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="backdrop"></div>
|
||||
<div id="pop" class="pop" hidden></div>
|
||||
<div id="modal" hidden><div id="modal-box"></div></div>
|
||||
<div id="lightbox" hidden>
|
||||
<img id="lb-img" alt="">
|
||||
<button id="lb-close" class="lb-btn" title="关闭 (Esc)"><svg class="i"><use href="#i-close"/></svg></button>
|
||||
<button id="lb-prev" class="lb-btn" title="上一张 (←)"><svg class="i"><use href="#i-left"/></svg></button>
|
||||
<button id="lb-next" class="lb-btn" title="下一张 (→)"><svg class="i"><use href="#i-right"/></svg></button>
|
||||
<span id="lb-count"></span>
|
||||
</div>
|
||||
<div id="toast" class="toast" role="status" aria-live="polite" hidden></div>
|
||||
<script>
|
||||
/* Layout is decided here, before app.js runs and before the first paint: the
|
||||
* rail must not flash open and then fold, and a broken script further down can
|
||||
* no longer take the layout down with it. */
|
||||
(() => {
|
||||
const q = new URLSearchParams(location.search);
|
||||
const pick = (key) => {
|
||||
const v = q.get(key);
|
||||
if (v === '0' || v === '1') return v === '0';
|
||||
return localStorage.getItem('cst.' + key) === '0';
|
||||
};
|
||||
if (pick('side')) document.body.classList.add('no-side');
|
||||
if (pick('panel')) document.body.classList.add('no-panel');
|
||||
const t = q.get('theme') || localStorage.getItem('cst.theme');
|
||||
if (t) document.documentElement.dataset.theme = t;
|
||||
})();
|
||||
</script>
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
+923
@@ -0,0 +1,923 @@
|
||||
/* CST-Agent WebUI - one stylesheet, no build step.
|
||||
*
|
||||
* Design rules
|
||||
* - one fact, one home: sessions in the rail, model/approval/plan in the
|
||||
* top bar, the run in the right panel, attachments in the composer;
|
||||
* - settings open as popovers anchored to the control that owns them,
|
||||
* with icon + label + one line of explanation + a check on the current
|
||||
* value (the pattern all three reference apps use);
|
||||
* - motion is short and eased: 120ms for feedback, 180ms for entrances,
|
||||
* nothing bounces, and prefers-reduced-motion turns it all off. */
|
||||
|
||||
/* Tokens are named the way the reference desktop app names them: four text
|
||||
* levels, three surfaces per zone (main / sidebar / composer), and borders
|
||||
* mixed out of the foreground rather than hard-coded greys - which is what
|
||||
* keeps the dark and the light sheet on the same visual rhythm. */
|
||||
:root {
|
||||
color-scheme: dark;
|
||||
|
||||
--text-primary: #f2f2f5;
|
||||
--text-secondary: #a9a9b4;
|
||||
--text-tertiary: #7d7d89;
|
||||
--text-quaternary: #5e5e69;
|
||||
--text-error: #f0616d;
|
||||
--text-success: #3ecf95;
|
||||
|
||||
--main-surface-primary: #0d0d0f;
|
||||
--main-surface-secondary: #141417;
|
||||
--main-surface-tertiary: #1d1d22;
|
||||
|
||||
--sidebar-surface-primary: #111114;
|
||||
--sidebar-surface-secondary: #18181c;
|
||||
--sidebar-surface-tertiary: #202026;
|
||||
|
||||
--composer-surface-primary: #16161a;
|
||||
--composer-surface-secondary: #202026;
|
||||
|
||||
--bg-code: #08080a;
|
||||
--bubble-user: var(--main-surface-tertiary);
|
||||
|
||||
/* every hover/active tint is alpha, so it lands correctly on any surface */
|
||||
--hover: color-mix(in srgb, var(--text-primary) 7%, transparent);
|
||||
--hover-strong: color-mix(in srgb, var(--text-primary) 13%, transparent);
|
||||
|
||||
--accent-primary: #3ecf95;
|
||||
--accent-fg: #04241a;
|
||||
--accent-weak: color-mix(in srgb, var(--accent-primary) 14%, transparent);
|
||||
--accent-line: color-mix(in srgb, var(--accent-primary) 55%, transparent);
|
||||
--link: var(--accent-primary);
|
||||
--violet: #8b7bff;
|
||||
--warn: #e0b341;
|
||||
--selection-bg: color-mix(in srgb, var(--accent-primary) 30%, transparent);
|
||||
--selection-fg: var(--text-primary);
|
||||
|
||||
--border-light: color-mix(in srgb, var(--text-primary) 14%, transparent);
|
||||
--border-medium: color-mix(in srgb, var(--text-primary) 24%, transparent);
|
||||
--border-heavy: color-mix(in srgb, var(--text-primary) 34%, transparent);
|
||||
|
||||
/* short aliases: the rest of the sheet keeps reading the same way */
|
||||
--bg: var(--main-surface-primary);
|
||||
--bg-soft: var(--main-surface-secondary);
|
||||
--bg-lift: var(--main-surface-tertiary);
|
||||
--bg-hover: var(--hover);
|
||||
--fg: var(--text-primary);
|
||||
--fg-dim: var(--text-secondary);
|
||||
--fg-faint: var(--text-tertiary);
|
||||
--fg-quat: var(--text-quaternary);
|
||||
--line: var(--border-light);
|
||||
--line-strong: var(--border-medium);
|
||||
--accent: var(--accent-primary);
|
||||
--danger: var(--text-error);
|
||||
|
||||
/* the side columns grow with the screen so a wide window does not turn into
|
||||
* one wide middle: the text stays near the run panel it is read against */
|
||||
--rail: clamp(240px, 14vw, 280px);
|
||||
--panel: clamp(296px, 20vw, 380px);
|
||||
--content: 720px; /* text column and composer share this edge */
|
||||
--radius: 12px;
|
||||
--radius-lg: 22px;
|
||||
--shadow: 0 16px 40px rgba(0, 0, 0, .45);
|
||||
--shadow-sm: 0 6px 18px rgba(0, 0, 0, .3);
|
||||
--mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
|
||||
--sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans CJK SC", sans-serif;
|
||||
--ease: cubic-bezier(.22, .61, .36, 1);
|
||||
--ease-pop: cubic-bezier(.2, .9, .3, 1.06); /* enters with a hint of overshoot */
|
||||
--t-fast: 120ms;
|
||||
--t: 180ms;
|
||||
}
|
||||
[data-theme="light"] {
|
||||
color-scheme: light;
|
||||
|
||||
--text-primary: #14141a;
|
||||
--text-secondary: #55555f;
|
||||
--text-tertiary: #7c7c86;
|
||||
--text-quaternary: #9c9ca6;
|
||||
--text-error: #c8303f;
|
||||
--text-success: #0f8f66;
|
||||
|
||||
--main-surface-primary: #ffffff;
|
||||
--main-surface-secondary: #f7f7f9;
|
||||
--main-surface-tertiary: #ffffff;
|
||||
|
||||
--sidebar-surface-primary: #fafafb;
|
||||
--sidebar-surface-secondary: #f1f1f4;
|
||||
--sidebar-surface-tertiary: #ffffff;
|
||||
|
||||
--composer-surface-primary: #ffffff;
|
||||
--composer-surface-secondary: #f4f4f6;
|
||||
|
||||
--bg-code: #f5f5f7;
|
||||
--bubble-user: #eef0f4;
|
||||
|
||||
--accent-primary: #0f9d70;
|
||||
--accent-fg: #ffffff;
|
||||
|
||||
--border-light: color-mix(in srgb, var(--text-primary) 12%, transparent);
|
||||
--border-medium: color-mix(in srgb, var(--text-primary) 20%, transparent);
|
||||
--border-heavy: color-mix(in srgb, var(--text-primary) 30%, transparent);
|
||||
|
||||
--warn: #a9761a;
|
||||
--shadow: 0 16px 40px rgba(20, 20, 35, .14);
|
||||
--shadow-sm: 0 6px 18px rgba(20, 20, 35, .1);
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body { height: 100%; margin: 0; }
|
||||
body {
|
||||
background: var(--bg); color: var(--fg);
|
||||
font: 15px/1.62 var(--sans);
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
overflow: hidden; /* columns scroll, never the page */
|
||||
}
|
||||
button, textarea, input { font: inherit; color: inherit; }
|
||||
button { cursor: pointer; background: none; border: 0; }
|
||||
button:disabled { cursor: default; }
|
||||
.grow { flex: 1 1 auto; }
|
||||
.quiet, .muted { color: var(--fg-dim); }
|
||||
.tiny { font-size: 12px; }
|
||||
[hidden] { display: none !important; }
|
||||
::selection { background: var(--selection-bg); color: var(--selection-fg); }
|
||||
|
||||
/* one icon system: everything is drawn on the same 24px grid, stroked with
|
||||
* the current text colour, so header, rail, popovers and composer match */
|
||||
svg.i {
|
||||
width: 18px; height: 18px; flex: 0 0 auto; display: block;
|
||||
fill: none; stroke: currentColor; stroke-width: 1.7;
|
||||
stroke-linecap: round; stroke-linejoin: round;
|
||||
}
|
||||
svg.i .f { fill: currentColor; stroke: none; }
|
||||
.icon svg.i { width: 18px; height: 18px; }
|
||||
.icon.tiny svg.i { width: 15px; height: 15px; }
|
||||
.chip svg.i { width: 15px; height: 15px; }
|
||||
.chip .chev { width: 13px; height: 13px; opacity: .6; }
|
||||
.new-task svg.i { width: 17px; height: 17px; }
|
||||
.send svg.i { width: 19px; height: 19px; stroke-width: 2; }
|
||||
svg.i.only-light { display: none; }
|
||||
[data-theme="light"] svg.i.only-dark { display: none; }
|
||||
[data-theme="light"] svg.i.only-light { display: block; }
|
||||
|
||||
/* the changed-file rows in the run panel: path, then +added / -removed */
|
||||
.fstat { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 13px; animation: fade-in var(--t) var(--ease) both; }
|
||||
.fstat .fimg { width: 15px; height: 15px; color: var(--text-quaternary); }
|
||||
.fstat .p {
|
||||
flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis;
|
||||
white-space: nowrap; font-family: var(--mono); font-size: 12px;
|
||||
}
|
||||
.fstat .a, .fstat .d { font-variant-numeric: tabular-nums; font-size: 12px; }
|
||||
.fstat .a { color: var(--text-success); }
|
||||
.fstat .d { color: var(--text-error); }
|
||||
|
||||
::-webkit-scrollbar { width: 10px; height: 10px; }
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: var(--line-strong); border-radius: 99px;
|
||||
border: 3px solid transparent; background-clip: content-box;
|
||||
}
|
||||
::-webkit-scrollbar-thumb:hover { background: var(--fg-faint); background-clip: content-box; }
|
||||
|
||||
/* --- frame ---------------------------------------------------------------- */
|
||||
#app {
|
||||
display: grid;
|
||||
grid-template-columns: var(--rail) minmax(0, 1fr) var(--panel);
|
||||
grid-template-rows: minmax(0, 1fr);
|
||||
height: 100dvh;
|
||||
/* the rail keeps its track and collapses to 0px, so the two states have the
|
||||
* same track count and the column width can actually be interpolated */
|
||||
transition: grid-template-columns 300ms var(--ease);
|
||||
}
|
||||
/* a zeroed track plus display:none used to slide #main into column one and
|
||||
* crush it to nothing; the rail now stays in flow and is clipped instead */
|
||||
body.no-side #app { grid-template-columns: 0px minmax(0, 1fr) var(--panel); }
|
||||
body.no-panel #app { grid-template-columns: var(--rail) minmax(0, 1fr) 0px; }
|
||||
body.no-side.no-panel #app { grid-template-columns: 0px minmax(0, 1fr) 0px; }
|
||||
/* the expand control only exists while the rail is folded away, so exactly one
|
||||
* toggle is ever on screen */
|
||||
#btn-side { display: none; }
|
||||
body.no-side #btn-side { display: inline-flex; }
|
||||
|
||||
.icon {
|
||||
width: 32px; height: 32px; border-radius: 9px; flex: 0 0 auto;
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
color: var(--fg-dim); font-size: 15px;
|
||||
transition: background var(--t-fast), color var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.icon:hover { background: var(--bg-hover); color: var(--fg); }
|
||||
.icon:active { transform: scale(.94); }
|
||||
.icon.tiny { width: 26px; height: 26px; font-size: 13px; }
|
||||
.round { border-radius: 50%; }
|
||||
.chip {
|
||||
display: inline-flex; align-items: center; gap: 5px; height: 30px;
|
||||
padding: 0 11px; border-radius: 99px; border: 1px solid transparent;
|
||||
background: none; color: var(--fg-dim); font-size: 13px;
|
||||
max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.chip:hover { background: var(--bg-hover); color: var(--fg); }
|
||||
.chip:active { transform: scale(.97); }
|
||||
.chip.model { color: var(--fg); font-weight: 600; background: var(--bg-lift); border-color: var(--line); }
|
||||
.chip.model:hover { border-color: var(--line-strong); }
|
||||
.chip.warn { color: var(--warn); }
|
||||
.chip .chev { font-size: 11px; opacity: .65; }
|
||||
.badge {
|
||||
min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px;
|
||||
background: var(--accent-weak); color: var(--accent);
|
||||
font-size: 11px; font-weight: 700;
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
}
|
||||
|
||||
/* --- rail ----------------------------------------------------------------- */
|
||||
#side {
|
||||
background: var(--sidebar-surface-primary); border-right: 1px solid var(--border-light);
|
||||
min-width: 0; overflow: hidden; /* clips the rail while its track collapses */
|
||||
}
|
||||
/* the contents keep their own width and slide out, so nothing reflows mid-flight
|
||||
* and the exit reads as one movement instead of a squeeze */
|
||||
.side-inner {
|
||||
width: var(--rail); height: 100%; min-width: 0;
|
||||
display: flex; flex-direction: column; padding: 12px 10px 10px;
|
||||
transition: transform 300ms var(--ease-pop), opacity 200ms var(--ease);
|
||||
}
|
||||
body.no-side .side-inner { transform: translateX(-18px); opacity: 0; }
|
||||
body.no-side #side { pointer-events: none; }
|
||||
.side-head { display: flex; align-items: center; gap: 2px; margin-bottom: 14px; }
|
||||
/* the wordmark is a label, not a control: no logo tile, nothing to click */
|
||||
.brand { flex: 1 1 auto; display: flex; align-items: center; min-width: 0; padding: 6px 8px; }
|
||||
.brand-name {
|
||||
font-weight: 700; letter-spacing: .2px; font-size: 15px;
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
}
|
||||
|
||||
.new-task {
|
||||
display: flex; align-items: center; gap: 9px; height: 40px; padding: 0 12px;
|
||||
margin-bottom: 18px; border-radius: 12px; font-weight: 600;
|
||||
background: var(--bg-lift); border: 1px solid var(--line);
|
||||
transition: border-color var(--t-fast), background var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.new-task:hover { border-color: var(--line-strong); background: var(--bg-hover); }
|
||||
.new-task:active { transform: scale(.985); }
|
||||
.new-task .plus { color: var(--accent); font-size: 16px; line-height: 1; }
|
||||
|
||||
.side-sec {
|
||||
display: flex; align-items: center; gap: 6px; padding: 0 8px 6px;
|
||||
color: var(--fg-faint); font-size: 11px; font-weight: 600;
|
||||
letter-spacing: .7px; text-transform: uppercase;
|
||||
}
|
||||
.sessions { flex: 1 1 auto; overflow-y: auto; min-height: 40px; scrollbar-gutter: stable; }
|
||||
.sess-item {
|
||||
display: flex; align-items: baseline; gap: 8px; width: 100%;
|
||||
padding: 7px 9px; border-radius: 9px; text-align: left;
|
||||
color: var(--text-secondary); font-size: 13.5px;
|
||||
animation: fade-in var(--t) var(--ease) both;
|
||||
transition: background var(--t-fast), color var(--t-fast);
|
||||
}
|
||||
.sess-item:hover { background: var(--hover); color: var(--text-primary); }
|
||||
.sess-item.on { background: var(--sidebar-surface-tertiary); color: var(--text-primary); font-weight: 600; }
|
||||
.sess-item.on::before {
|
||||
content: ""; width: 5px; height: 5px; border-radius: 50%;
|
||||
background: var(--accent); flex: 0 0 auto; align-self: center;
|
||||
}
|
||||
.sess-item .t { flex: 1 1 auto; min-width: 0; overflow: hidden; }
|
||||
.sess-item .ts { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
/* a title that does not fit fades at the edge and scrolls itself on hover, the
|
||||
* way the reference sidebar handles long names (its data-marquee-text pattern) */
|
||||
.sess-item .t.mq { mask-image: linear-gradient(90deg, #000 calc(100% - 18px), transparent); }
|
||||
.sess-item:hover .t.mq { mask-image: none; }
|
||||
.sess-item:hover .t.mq .ts {
|
||||
width: max-content; text-overflow: clip;
|
||||
animation: marquee 2.6s var(--ease) 160ms infinite alternate;
|
||||
}
|
||||
@keyframes marquee { from { transform: none; } to { transform: translateX(calc(-1 * var(--mq, 0px))); } }
|
||||
.sess-item .n { font-size: 11px; color: var(--text-quaternary); font-weight: 400; white-space: nowrap; }
|
||||
.side-foot {
|
||||
display: flex; align-items: center; gap: 6px; padding-top: 10px;
|
||||
margin-top: 6px; border-top: 1px solid var(--line);
|
||||
}
|
||||
.who { display: flex; align-items: center; gap: 9px; min-width: 0; flex: 1 1 auto; }
|
||||
.avatar {
|
||||
width: 28px; height: 28px; border-radius: 50%; flex: 0 0 auto;
|
||||
background: linear-gradient(135deg, var(--violet), var(--accent));
|
||||
color: #fff; font-weight: 800; font-size: 13px;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
}
|
||||
.who-text { min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
|
||||
.who-text b { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.who-text i { font-size: 11px; color: var(--fg-faint); font-style: normal; }
|
||||
|
||||
/* --- top bar -------------------------------------------------------------- */
|
||||
#main { display: flex; flex-direction: column; min-width: 0; position: relative; }
|
||||
#topbar { display: flex; align-items: center; gap: 4px; padding: 9px 12px; border-bottom: 1px solid var(--line); }
|
||||
.seg {
|
||||
display: flex; background: var(--bg-soft); border: 1px solid var(--line);
|
||||
border-radius: 99px; padding: 2px; position: relative;
|
||||
}
|
||||
.seg button {
|
||||
height: 25px; padding: 0 12px; white-space: nowrap; border-radius: 99px; color: var(--fg-dim); font-size: 12.5px;
|
||||
transition: color var(--t-fast), background var(--t);
|
||||
}
|
||||
.seg button.on { background: var(--bg-lift); color: var(--fg); font-weight: 600; }
|
||||
.only-narrow { display: none; }
|
||||
|
||||
#scroll {
|
||||
flex: 1 1 auto; overflow-y: auto; padding: 22px 0 10px;
|
||||
scroll-behavior: smooth; scrollbar-gutter: stable;
|
||||
}
|
||||
#log { max-width: calc(var(--content) + 40px); margin: 0 auto; padding: 0 20px; }
|
||||
#log:empty { display: none; }
|
||||
|
||||
/* --- empty state ---------------------------------------------------------- */
|
||||
/* the first screen is one line: no logo, no suggestion wall. It goes away the
|
||||
* moment the first message lands (see showEmpty). */
|
||||
#empty { margin: 22vh auto 0; padding: 0 20px; text-align: center; }
|
||||
#empty.gone { display: none; }
|
||||
#empty .ready {
|
||||
margin: 0; color: var(--text-tertiary); font-size: 15px; letter-spacing: .3px;
|
||||
animation: fade-in 320ms var(--ease) both;
|
||||
}
|
||||
|
||||
/* --- transcript ----------------------------------------------------------- */
|
||||
/* one left edge for everything (answer, process block, notes), the same edge
|
||||
* as the composer; prompts hang on the right edge with their images above */
|
||||
.msg { display: flex; flex-direction: column; align-items: stretch; padding: 14px 0 0; animation: fade-up var(--t) var(--ease) both; }
|
||||
.msg .body { min-width: 0; max-width: 100%; }
|
||||
.msg.user { align-items: flex-end; padding-top: 24px; }
|
||||
#log > .msg.user:first-child { padding-top: 6px; }
|
||||
.msg.user .body {
|
||||
background: var(--bubble-user); border-radius: 24px;
|
||||
padding: 11px 18px; max-width: min(76%, 560px); white-space: pre-wrap; word-break: break-word;
|
||||
}
|
||||
.msg.error .body { color: var(--danger); white-space: pre-wrap; }
|
||||
.msg.error .body::before {
|
||||
content: "!"; display: inline-flex; align-items: center; justify-content: center;
|
||||
width: 18px; height: 18px; margin-right: 8px; border-radius: 50%; vertical-align: 1px;
|
||||
background: color-mix(in srgb, var(--danger) 18%, transparent); font-size: 12px; font-weight: 800;
|
||||
}
|
||||
.body > :first-child, .live-html > :first-child { margin-top: 0; }
|
||||
.body > :last-child, .live-html > :last-child { margin-bottom: 0; }
|
||||
.body h1, .body h2, .body h3, .body h4 { line-height: 1.35; margin: 1.1em 0 .5em; }
|
||||
.body h1 { font-size: 1.45em; } .body h2 { font-size: 1.25em; } .body h3 { font-size: 1.1em; }
|
||||
.body p, .body ul, .body ol { margin: .55em 0; }
|
||||
.body ul, .body ol { padding-left: 1.4em; }
|
||||
.body li { margin: .22em 0; }
|
||||
.body a { color: var(--link); }
|
||||
.body code {
|
||||
font-family: var(--mono); font-size: .88em; background: var(--bg-code);
|
||||
border: 1px solid var(--line); border-radius: 6px; padding: .1em .35em;
|
||||
}
|
||||
.body pre {
|
||||
background: var(--bg-code); border: 1px solid var(--line);
|
||||
border-radius: 12px; padding: 12px 14px; overflow-x: auto; margin: .75em 0;
|
||||
}
|
||||
.body pre code { background: none; border: 0; padding: 0; font-size: 13px; }
|
||||
.body blockquote { margin: .75em 0; padding: .1em 0 .1em 12px; border-left: 3px solid var(--line-strong); color: var(--fg-dim); }
|
||||
.body table { border-collapse: collapse; margin: .75em 0; font-size: 13.5px; display: block; overflow-x: auto; }
|
||||
.body th, .body td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
|
||||
.body th { background: var(--bg-soft); }
|
||||
.body img { max-width: 100%; border-radius: 10px; }
|
||||
.body hr { border: 0; border-top: 1px solid var(--line); margin: 1.2em 0; }
|
||||
/* a caret while tokens are still arriving */
|
||||
.body.live > .live-html > :last-child::after {
|
||||
content: ""; display: inline-block; width: 7px; height: 14px;
|
||||
margin-left: 3px; vertical-align: -2px; border-radius: 2px;
|
||||
background: var(--accent); animation: blink 1.1s steps(2, start) infinite;
|
||||
}
|
||||
|
||||
/* a prompt's images: above the bubble and outside it, square tiles when there
|
||||
* are several, the picture's own shape when there is one */
|
||||
.attach-strip { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 12px; margin: 0 0 10px; max-width: 100%; }
|
||||
.attach-strip .thumb {
|
||||
display: block; width: 152px; height: 152px; padding: 0; overflow: hidden; cursor: zoom-in;
|
||||
border-radius: 20px; border: 1px solid var(--line); background: var(--bg-soft);
|
||||
animation: rise var(--t) var(--ease) both;
|
||||
transition: border-color var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.attach-strip .thumb:hover { border-color: var(--line-strong); }
|
||||
.attach-strip .thumb:active { transform: scale(.985); }
|
||||
.attach-strip .thumb img { display: block; width: 100%; height: 100%; object-fit: cover; }
|
||||
.attach-strip.one .thumb { width: auto; height: auto; }
|
||||
.attach-strip.one .thumb img {
|
||||
width: auto; height: auto; min-width: 72px; min-height: 72px;
|
||||
max-width: min(260px, 70vw); max-height: 260px; object-fit: contain;
|
||||
}
|
||||
|
||||
.turn-meta { display: flex; align-items: center; gap: 10px; padding: 8px 0 2px; }
|
||||
.turn-meta details > summary { cursor: pointer; list-style: none; color: var(--fg-faint); font-size: 12px; transition: color var(--t-fast); }
|
||||
.turn-meta details > summary::-webkit-details-marker { display: none; }
|
||||
.turn-meta details > summary:hover { color: var(--fg-dim); }
|
||||
/* the actions take a row only under messages that have text to act on */
|
||||
.msg-actions { display: flex; gap: 2px; padding-top: 4px; margin-left: -7px; opacity: 0; transition: opacity var(--t) var(--ease); }
|
||||
.msg.user .msg-actions { margin: 0 -7px 0 0; }
|
||||
.msg:hover .msg-actions, .msg-actions:focus-within { opacity: 1; }
|
||||
.act {
|
||||
width: 30px; height: 28px; border-radius: 8px; color: var(--fg-faint);
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
transition: background var(--t-fast), color var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.act svg.i { width: 16px; height: 16px; }
|
||||
.act:hover { background: var(--bg-hover); color: var(--fg); }
|
||||
.act:active { transform: scale(.94); }
|
||||
|
||||
/* --- process block: a run of thinking and tool calls, folded to one line --- */
|
||||
.proc { margin: 10px 0 0; }
|
||||
.proc-sum {
|
||||
display: inline-flex; align-items: center; gap: 7px; max-width: 100%; height: 28px;
|
||||
margin-left: -7px; padding: 0 10px 0 6px; border-radius: 8px;
|
||||
cursor: pointer; list-style: none; color: var(--fg-faint); font-size: 13px; user-select: none;
|
||||
transition: background var(--t-fast), color var(--t-fast);
|
||||
}
|
||||
.proc-sum::-webkit-details-marker { display: none; }
|
||||
.proc-sum:hover { background: var(--bg-hover); color: var(--fg-dim); }
|
||||
.proc-chev { width: 14px; height: 14px; transform: rotate(-90deg); transition: transform var(--t) var(--ease); }
|
||||
.proc[open] > .proc-sum .proc-chev { transform: none; }
|
||||
.proc-lbl { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.proc-meta { flex: 0 0 auto; color: var(--fg-quat); font-size: 12px; white-space: nowrap; }
|
||||
.proc-meta:empty { display: none; }
|
||||
/* the running block's label shimmers instead of spinning */
|
||||
.proc.run .proc-lbl {
|
||||
background: linear-gradient(90deg, var(--fg-faint) 0%, var(--fg) 45%, var(--fg-faint) 90%);
|
||||
background-size: 220% 100%; -webkit-background-clip: text; background-clip: text;
|
||||
color: transparent; animation: shimmer 1.8s linear infinite;
|
||||
}
|
||||
|
||||
/* the timeline: one hairline, one dot per entry */
|
||||
.tl { position: relative; margin: 2px 0 0; padding: 2px 0 2px 20px; animation: fade-in var(--t) var(--ease) both; }
|
||||
.tl::before {
|
||||
content: ""; position: absolute; left: 4px; top: 14px; bottom: 14px; width: 1px;
|
||||
background: var(--line);
|
||||
}
|
||||
.tl-item { position: relative; animation: fade-up var(--t) var(--ease) both; }
|
||||
.tl-item > summary {
|
||||
display: flex; align-items: center; gap: 8px; min-height: 28px; padding: 2px 0;
|
||||
cursor: pointer; list-style: none; font-size: 13px; color: var(--fg-dim);
|
||||
transition: color var(--t-fast);
|
||||
}
|
||||
.tl-item > summary::-webkit-details-marker { display: none; }
|
||||
.tl-item > summary:hover { color: var(--fg); }
|
||||
.tl-dot {
|
||||
position: absolute; left: -20px; top: 9px; width: 9px; height: 9px; border-radius: 50%;
|
||||
background: var(--bg); border: 1.5px solid var(--line-strong);
|
||||
}
|
||||
.tl-item.think .tl-dot { border-color: color-mix(in srgb, var(--violet) 75%, transparent); }
|
||||
.tl-name { flex: 0 0 auto; font-size: 12.5px; color: var(--fg-dim); }
|
||||
.tl-prev {
|
||||
flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
color: var(--fg-faint); font-size: 12.5px;
|
||||
}
|
||||
.tl-item.think[open] .tl-prev { visibility: hidden; }
|
||||
.think-text {
|
||||
color: var(--fg-dim); font-size: 13px; line-height: 1.65; white-space: pre-wrap; word-break: break-word;
|
||||
padding: 2px 0 8px; animation: fade-in var(--t) var(--ease) both;
|
||||
}
|
||||
|
||||
/* tool calls: one line each, open for arguments and output */
|
||||
.step-line .name { flex: 0 0 auto; font-family: var(--mono); font-size: 12.5px; color: var(--fg); }
|
||||
.step-line .args { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12px; color: var(--fg-faint); }
|
||||
.step-line .phase { flex: 0 0 auto; font-size: 11.5px; color: var(--fg-faint); white-space: nowrap; }
|
||||
.step-line.run .phase { color: var(--warn); }
|
||||
.step-line.run .tl-dot { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 30%, var(--bg)); animation: pulse 1.1s var(--ease) infinite; }
|
||||
.step-line.done .tl-dot { border-color: var(--accent-line); background: var(--accent-weak); }
|
||||
.step-line.bad .tl-dot { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 20%, var(--bg)); }
|
||||
.step-line.bad .phase, .step-line.bad .name { color: var(--danger); }
|
||||
/* a sub-agent's latest steps, next to its task */
|
||||
.step-line .step-detail {
|
||||
flex: 0 1 40%; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
font-family: var(--mono); font-size: 11.5px; color: var(--fg-faint);
|
||||
}
|
||||
.step-line.done .step-detail { display: none; }
|
||||
/* "[cst] plan mode is now ON" and other engine notes in the history */
|
||||
.note-line {
|
||||
padding: 8px 0 2px; font-size: 12px; color: var(--fg-faint);
|
||||
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
|
||||
}
|
||||
.note-line::before { content: "· "; }
|
||||
.step-body { padding: 0 0 6px; animation: fade-in var(--t) var(--ease) both; }
|
||||
.step-out, .step-args-full {
|
||||
font-family: var(--mono); font-size: 12px; background: var(--bg-code);
|
||||
border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px;
|
||||
margin: 4px 0 0; max-height: 300px; overflow: auto; white-space: pre-wrap; word-break: break-word;
|
||||
}
|
||||
|
||||
#to-bottom {
|
||||
position: absolute; right: 26px; bottom: 172px; z-index: 5;
|
||||
width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
|
||||
background: var(--bg-lift); border: 1px solid var(--line-strong); color: var(--fg-dim);
|
||||
box-shadow: var(--shadow-sm); font-size: 16px;
|
||||
animation: fade-in var(--t) var(--ease) both;
|
||||
transition: color var(--t-fast), transform 90ms var(--ease), background var(--t-fast);
|
||||
}
|
||||
#to-bottom:hover { color: var(--fg); background: var(--bg-hover); }
|
||||
#to-bottom:active { transform: scale(.94); }
|
||||
|
||||
/* --- composer ------------------------------------------------------------- */
|
||||
#composer { padding: 8px 20px 12px; }
|
||||
#composer > * { max-width: var(--content); margin: 0 auto; }
|
||||
.pending { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 6px 0 10px; }
|
||||
.pending .chip { background: var(--bg-soft); border-color: var(--line); }
|
||||
/* images waiting for the next prompt, the way they will look once sent */
|
||||
.pend-thumb { position: relative; width: 56px; height: 56px; flex: 0 0 auto; animation: rise var(--t) var(--ease) both; }
|
||||
.pend-thumb img {
|
||||
display: block; width: 100%; height: 100%; object-fit: cover; cursor: zoom-in;
|
||||
border-radius: 12px; border: 1px solid var(--line);
|
||||
}
|
||||
.pend-x {
|
||||
position: absolute; top: -7px; right: -7px; width: 20px; height: 20px; border-radius: 50%;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
background: var(--main-surface-tertiary); border: 1px solid var(--line-strong); color: var(--fg-dim);
|
||||
opacity: 0; transition: opacity var(--t-fast), color var(--t-fast);
|
||||
}
|
||||
.pend-x svg.i { width: 12px; height: 12px; stroke-width: 2.2; }
|
||||
.pend-thumb:hover .pend-x, .pend-x:focus-visible { opacity: 1; }
|
||||
.pend-x:hover { color: var(--fg); }
|
||||
.edit-bar {
|
||||
display: flex; align-items: center; gap: 8px; padding: 6px 10px; margin-bottom: 8px;
|
||||
border-radius: 10px; border: 1px solid var(--line); background: var(--bg-soft);
|
||||
color: var(--fg-dim); font-size: 12.5px;
|
||||
}
|
||||
.edit-bar[hidden] { display: none; }
|
||||
.msg.user .msg-actions { justify-content: flex-end; padding-left: 0; }
|
||||
.sess-search {
|
||||
width: calc(100% - 8px); margin: 0 4px 8px; height: 30px; padding: 0 10px;
|
||||
border-radius: 9px; border: 1px solid var(--line); background: var(--bg-soft);
|
||||
color: var(--fg); font-size: 13px;
|
||||
}
|
||||
.sess-item { cursor: pointer; }
|
||||
#modal-box .sess-list { max-height: min(56vh, 520px); overflow-y: auto; margin: 4px 0 10px; }
|
||||
#modal-box .sess button { flex: 0 0 auto; }
|
||||
.sess-item .more {
|
||||
flex: 0 0 auto; align-self: center; width: 22px; height: 22px; border-radius: 6px;
|
||||
display: grid; place-items: center; color: var(--fg-faint); opacity: 0;
|
||||
transition: opacity var(--t-fast), background var(--t-fast);
|
||||
}
|
||||
.sess-item:hover .more, .sess-item .more:focus-visible { opacity: 1; }
|
||||
.sess-item .more:hover { background: var(--bg-hover); color: var(--fg); }
|
||||
.sess-item .more .i { width: 16px; height: 16px; }
|
||||
#modal-box .sess button.danger { color: var(--danger, #e5484d); }
|
||||
button.fstat { width: 100%; text-align: left; border-radius: 7px; transition: background var(--t-fast); }
|
||||
button.fstat:hover { background: var(--bg-hover); }
|
||||
.danger-text { color: var(--danger); }
|
||||
#modal-box.wide { width: min(980px, 100%); }
|
||||
#modal-box h4.sub { margin: 16px 0 6px; font-size: 13px; color: var(--fg-dim); font-weight: 650; }
|
||||
#modal-box .field select, #modal-box .field input[type=number] {
|
||||
flex: 1 1 auto; min-width: 150px; background: var(--bg-lift); color: var(--fg);
|
||||
border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; outline: none;
|
||||
}
|
||||
#modal-box .field > span { width: 84px; }
|
||||
#modal-box .checks { display: flex; flex-wrap: wrap; gap: 14px; }
|
||||
#modal-box .check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; }
|
||||
#modal-box .tool-list { max-height: 34vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; padding: 4px; }
|
||||
#modal-box .tool-row {
|
||||
display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 7px;
|
||||
font-size: 12.5px; cursor: pointer;
|
||||
}
|
||||
#modal-box .tool-row:hover { background: var(--bg-hover); }
|
||||
#modal-box .tool-row.dim code { opacity: .55; }
|
||||
#modal-box .tool-row code { font-family: var(--mono); font-size: 12px; flex: 0 0 auto; }
|
||||
#modal-box .tool-row .perm { flex: 0 0 auto; color: var(--fg-faint); font-size: 11px; border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; }
|
||||
#modal-box .tool-row .desc { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg-dim); }
|
||||
#modal-box .diff-files { display: flex; flex-direction: column; gap: 1px; max-height: 24vh; overflow-y: auto; margin-bottom: 10px; }
|
||||
#modal-box .diff-file {
|
||||
display: flex; gap: 10px; align-items: center; text-align: left; padding: 5px 8px;
|
||||
border-radius: 7px; font-size: 12.5px;
|
||||
}
|
||||
#modal-box .diff-file:hover { background: var(--bg-hover); }
|
||||
#modal-box .diff-file.on { background: var(--accent-weak); }
|
||||
#modal-box .diff-file .st { flex: 0 0 52px; color: var(--fg-faint); font-size: 11.5px; }
|
||||
#modal-box .diff-file .p { font-family: var(--mono); overflow-wrap: anywhere; }
|
||||
pre.diff {
|
||||
margin: 0; padding: 10px 0; max-height: 52vh; overflow: auto; border-radius: 10px;
|
||||
background: var(--bg-soft); border: 1px solid var(--line);
|
||||
font-family: var(--mono); font-size: 12px; line-height: 1.5;
|
||||
}
|
||||
pre.diff span { display: block; padding: 0 12px; white-space: pre; min-height: 1.5em; }
|
||||
pre.diff .add { color: var(--text-success); background: color-mix(in srgb, var(--text-success) 10%, transparent); }
|
||||
pre.diff .del { color: var(--text-error); background: color-mix(in srgb, var(--text-error) 10%, transparent); }
|
||||
pre.diff .hunk { color: var(--accent); }
|
||||
pre.diff .h { color: var(--fg-faint); font-weight: 600; }
|
||||
|
||||
.box {
|
||||
border: 1px solid var(--border-medium); border-radius: var(--radius-lg);
|
||||
background: var(--composer-surface-primary); padding: 11px 13px 9px;
|
||||
transition: border-color var(--t), box-shadow var(--t), background var(--t);
|
||||
}
|
||||
.box:hover { border-color: var(--border-heavy); }
|
||||
.box:focus-within {
|
||||
border-color: var(--accent-line);
|
||||
box-shadow: 0 0 0 3px var(--accent-weak);
|
||||
}
|
||||
.box textarea {
|
||||
width: 100%; border: 0; background: none; resize: none; outline: none;
|
||||
max-height: 240px; min-height: 26px; padding: 2px 2px 8px;
|
||||
caret-color: var(--accent);
|
||||
}
|
||||
.box textarea::placeholder { color: var(--fg-faint); }
|
||||
.box-row { display: flex; align-items: center; gap: 6px; }
|
||||
.att { border: 1px solid var(--line); font-size: 17px; line-height: 1; }
|
||||
.cost { color: var(--fg-faint); font-size: 12px; padding: 0 4px; white-space: nowrap; }
|
||||
.cost:hover { color: var(--fg-dim); }
|
||||
.send {
|
||||
width: 36px; height: 36px; flex: 0 0 auto; font-size: 17px; font-weight: 800;
|
||||
background: var(--accent); color: var(--accent-fg);
|
||||
transition: filter var(--t-fast), transform 90ms var(--ease), opacity var(--t-fast);
|
||||
}
|
||||
.send:hover { filter: brightness(1.08); }
|
||||
.send:active { transform: scale(.94); }
|
||||
.send:disabled { opacity: .38; }
|
||||
.icon.stop { border: 1px solid var(--line); font-size: 12px; }
|
||||
.hint-line { display: flex; align-items: center; gap: 10px; padding: 7px 6px 0; font-size: 12px; color: var(--fg-faint); }
|
||||
|
||||
/* permission gate: inline, the turn really is waiting on it */
|
||||
.gate {
|
||||
border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent);
|
||||
border-radius: var(--radius); padding: 11px 13px; margin-bottom: 10px;
|
||||
background: color-mix(in srgb, var(--warn) 7%, var(--bg-soft));
|
||||
animation: rise var(--t) var(--ease) both;
|
||||
}
|
||||
.gate-head { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 14px; }
|
||||
.gate-timer { color: var(--warn); font-size: 12.5px; font-variant-numeric: tabular-nums; }
|
||||
.gate pre {
|
||||
margin: 8px 0 0; background: var(--bg-code); border: 1px solid var(--line);
|
||||
border-radius: 10px; padding: 9px 11px; max-height: 180px; overflow: auto;
|
||||
font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; word-break: break-word;
|
||||
}
|
||||
.gate-keys { padding: 8px 0 2px; color: var(--fg-dim); font-size: 12.5px; }
|
||||
.gate-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
|
||||
.gate-row button {
|
||||
height: 34px; padding: 0 14px; border-radius: 10px; font-weight: 600;
|
||||
border: 1px solid var(--line-strong); background: var(--bg-lift);
|
||||
transition: filter var(--t-fast), transform 90ms var(--ease), background var(--t-fast);
|
||||
}
|
||||
.gate-row button.ok { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
|
||||
.gate-row button.danger { color: var(--danger); }
|
||||
.gate-row button:hover { filter: brightness(1.12); }
|
||||
.gate-row button:active { transform: scale(.97); }
|
||||
|
||||
/* --- popovers: anchored, animated, and the only place settings live ------- */
|
||||
.pop {
|
||||
position: fixed; z-index: 45; min-width: 236px; max-width: 320px;
|
||||
background: var(--main-surface-tertiary); border: 1px solid var(--border-medium);
|
||||
border-radius: 14px; padding: 6px; box-shadow: var(--shadow);
|
||||
animation: pop-in 160ms var(--ease) both;
|
||||
}
|
||||
.pop-item {
|
||||
display: flex; align-items: flex-start; gap: 10px; width: 100%;
|
||||
padding: 9px 10px; border-radius: 10px; text-align: left;
|
||||
transition: background var(--t-fast);
|
||||
}
|
||||
.pop-item:hover { background: var(--bg-hover); }
|
||||
.pop-item .pi { width: 18px; flex: 0 0 auto; text-align: center; color: var(--fg-dim); font-size: 14px; line-height: 1.4; }
|
||||
/* title over description: two lines, never one run-on line */
|
||||
.pop-item .pm { min-width: 0; flex: 1 1 auto; display: flex; flex-direction: column; gap: 2px; }
|
||||
.pop-item .pt { font-size: 13.5px; font-weight: 600; line-height: 1.35; }
|
||||
.pop-item .pd { font-size: 11.5px; color: var(--fg-faint); line-height: 1.4; }
|
||||
.pop-item .pk { flex: 0 0 auto; color: var(--accent); font-size: 13px; }
|
||||
.pop-item.on { background: var(--accent-weak); }
|
||||
.pop-item.on .pi, .pop-item.on .pt { color: var(--accent); }
|
||||
.pop-item.danger .pi, .pop-item.danger .pt { color: var(--danger); }
|
||||
.pop-sep { height: 1px; background: var(--line); margin: 5px 4px; }
|
||||
.pop-title { padding: 8px 10px 4px; color: var(--fg-faint); font-size: 11.5px; font-weight: 600; letter-spacing: .4px; }
|
||||
.pop-input { display: flex; gap: 6px; padding: 4px 6px 6px; }
|
||||
.pop-input input {
|
||||
flex: 1 1 auto; min-width: 0; background: var(--bg-soft); border: 1px solid var(--line);
|
||||
border-radius: 9px; padding: 7px 9px; outline: none; font-size: 13px;
|
||||
caret-color: var(--accent);
|
||||
transition: border-color var(--t-fast);
|
||||
}
|
||||
.pop-input input:focus { border-color: var(--accent-line); }
|
||||
.pop-input button {
|
||||
height: 32px; padding: 0 12px; border-radius: 9px; font-weight: 600; font-size: 13px;
|
||||
background: var(--accent); color: var(--accent-fg);
|
||||
transition: filter var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.pop-input button:active { transform: scale(.96); }
|
||||
.kv { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 5px 10px; font-size: 13px; }
|
||||
.kv span { color: var(--fg-dim); }
|
||||
.kv b { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 0; overflow-wrap: anywhere; text-align: right; }
|
||||
.pop { max-height: calc(100vh - 16px); overflow-y: auto; }
|
||||
.pop:has(.pop-list) { min-width: min(360px, 94vw); max-width: min(440px, 94vw); }
|
||||
.pop-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
|
||||
.pop-list { max-height: min(46vh, 420px); overflow-y: auto; }
|
||||
.pop-list .pop-item .pt { overflow-wrap: anywhere; font-family: var(--mono); font-size: 12.5px; }
|
||||
.pop-empty { padding: 10px; color: var(--fg-faint); font-size: 12.5px; }
|
||||
.pop-note { padding: 2px 10px 6px; color: var(--fg-faint); font-size: 11.5px; line-height: 1.45; }
|
||||
.pop-note:empty { display: none; }
|
||||
.pop-note.bad { color: var(--danger); }
|
||||
.pop-note.warn { color: var(--warn); }
|
||||
.pop-item.dim { opacity: .55; }
|
||||
button.link { color: var(--link); padding: 0 4px; font-size: inherit; text-decoration: underline; }
|
||||
.ok-text { color: var(--text-success); }
|
||||
.warn-text { color: var(--warn); }
|
||||
|
||||
/* providers and the key dialog */
|
||||
#modal-box .prov {
|
||||
display: flex; align-items: center; gap: 10px; padding: 9px 8px;
|
||||
border-bottom: 1px solid var(--line); border-radius: 8px;
|
||||
}
|
||||
#modal-box .prov.on { background: var(--accent-weak); }
|
||||
#modal-box .prov .pm { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
|
||||
#modal-box .prov .pm span { overflow-wrap: anywhere; }
|
||||
#modal-box .prov-acts button {
|
||||
height: 30px; padding: 0 12px; border-radius: 9px; font-weight: 600; font-size: 13px;
|
||||
border: 1px solid var(--line-strong); background: var(--bg-lift); white-space: nowrap;
|
||||
}
|
||||
#modal-box .prov-acts button.ok { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
|
||||
#modal-box .key-input {
|
||||
width: 100%; margin-top: 12px; background: var(--bg-lift); border: 1px solid var(--line);
|
||||
border-radius: 10px; padding: 9px 11px; outline: none; font-family: var(--mono);
|
||||
}
|
||||
#modal-box .key-input:focus { border-color: var(--accent-line); }
|
||||
#modal-box .key-err { min-height: 18px; margin-top: 6px; color: var(--danger); font-size: 12.5px; }
|
||||
|
||||
/* one line of feedback for anything that failed or needs a look */
|
||||
.toast {
|
||||
position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
|
||||
max-width: min(560px, 92vw); padding: 10px 14px; border-radius: 12px;
|
||||
background: var(--main-surface-tertiary); border: 1px solid var(--border-medium);
|
||||
box-shadow: var(--shadow); font-size: 13px; line-height: 1.45; overflow-wrap: anywhere;
|
||||
animation: fade-up 160ms var(--ease) both;
|
||||
}
|
||||
.toast.err { border-color: color-mix(in srgb, var(--danger) 55%, transparent); color: var(--danger); }
|
||||
.toast.warn { border-color: color-mix(in srgb, var(--warn) 55%, transparent); color: var(--warn); }
|
||||
.toast.ok { border-color: var(--accent-line); color: var(--accent); }
|
||||
.toast[hidden] { display: none; }
|
||||
|
||||
/* --- right panel ---------------------------------------------------------- */
|
||||
/* the right panel mirrors the rail: fixed-width contents, clipped by the track,
|
||||
* so folding it in and out is one eased movement on each side */
|
||||
#panel { background: var(--main-surface-secondary); border-left: 1px solid var(--border-light); min-width: 0; overflow: hidden; }
|
||||
.panel-inner {
|
||||
width: var(--panel); height: 100%; overflow-y: auto; padding: 12px;
|
||||
transition: transform 300ms var(--ease-pop), opacity 200ms var(--ease);
|
||||
}
|
||||
body.no-panel .panel-inner { transform: translateX(18px); opacity: 0; }
|
||||
body.no-panel #panel { pointer-events: none; }
|
||||
.panel-head {
|
||||
display: flex; align-items: center; gap: 6px; padding: 2px 4px 10px;
|
||||
color: var(--fg-faint); font-size: 11px; font-weight: 600;
|
||||
letter-spacing: .7px; text-transform: uppercase;
|
||||
}
|
||||
.card { border-top: 1px solid var(--line); padding: 12px 2px; }
|
||||
.card:first-of-type { border-top: 0; }
|
||||
.card h4 { margin: 0 0 8px; font-size: 12.5px; font-weight: 650; display: flex; align-items: center; gap: 6px; }
|
||||
.card .kv { padding: 3px 0; }
|
||||
.todo-list { margin: 0; padding: 0; list-style: none; font-size: 13.5px; }
|
||||
.todo-list li { display: flex; gap: 8px; padding: 3px 0; animation: fade-in var(--t) var(--ease) both; transition: color var(--t-fast); }
|
||||
.todo-list .boxmark { color: var(--fg-faint); }
|
||||
.todo-list li.done { color: var(--fg-faint); text-decoration: line-through; }
|
||||
.todo-list li.doing { color: var(--fg); font-weight: 600; }
|
||||
/* --- modal sheet (session list) ------------------------------------------- */
|
||||
#modal {
|
||||
position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 40;
|
||||
display: flex; align-items: center; justify-content: center; padding: 16px;
|
||||
animation: fade-in var(--t) var(--ease) both;
|
||||
}
|
||||
#modal-box {
|
||||
width: min(560px, 100%); max-height: 82vh; overflow-y: auto;
|
||||
background: var(--main-surface-tertiary); border: 1px solid var(--border-medium);
|
||||
border-radius: 18px; padding: 16px 18px; box-shadow: var(--shadow);
|
||||
animation: pop-in 180ms var(--ease) both;
|
||||
}
|
||||
#modal-box h3 { margin: 0 0 12px; font-size: 17px; }
|
||||
#modal-box .field { display: flex; align-items: center; gap: 8px; margin: 9px 0; flex-wrap: wrap; }
|
||||
#modal-box .field > span { width: 70px; color: var(--fg-dim); font-size: 13px; }
|
||||
#modal-box .field input {
|
||||
flex: 1 1 auto; min-width: 150px; background: var(--bg-lift);
|
||||
border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; outline: none;
|
||||
}
|
||||
#modal-box .buttons { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
|
||||
#modal-box .buttons button {
|
||||
height: 34px; padding: 0 14px; border-radius: 10px; font-weight: 600;
|
||||
border: 1px solid var(--line-strong); background: var(--bg-lift);
|
||||
transition: filter var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
#modal-box .buttons button:active { transform: scale(.97); }
|
||||
#modal-box .buttons button.ok { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
|
||||
#modal-box .buttons button.danger { color: var(--danger); }
|
||||
#modal-box .sess {
|
||||
display: flex; align-items: center; gap: 8px; padding: 8px 6px;
|
||||
border-bottom: 1px solid var(--line); border-radius: 8px;
|
||||
transition: background var(--t-fast);
|
||||
}
|
||||
#modal-box .sess:hover { background: var(--bg-hover); }
|
||||
#modal-box .sess .id { font-family: var(--mono); font-size: 11.5px; color: var(--fg-faint); }
|
||||
#modal-box .sess .t { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }
|
||||
|
||||
/* --- commands: the list over the composer, the palette, the help sheet ---- */
|
||||
#composer { position: relative; }
|
||||
.slash {
|
||||
position: absolute; left: 20px; right: 20px; bottom: calc(100% - 6px); z-index: 20;
|
||||
max-height: min(46vh, 360px); overflow-y: auto; padding: 5px;
|
||||
background: var(--main-surface-tertiary); border: 1px solid var(--border-medium);
|
||||
border-radius: 14px; box-shadow: var(--shadow); animation: pop-in 140ms var(--ease) both;
|
||||
}
|
||||
.slash[hidden] { display: none; }
|
||||
.slash-item, .palette-item {
|
||||
display: flex; align-items: baseline; gap: 10px; width: 100%; padding: 7px 10px;
|
||||
border-radius: 9px; text-align: left; font-size: 13px;
|
||||
}
|
||||
.slash-item.on, .palette-item.on { background: var(--accent-weak); }
|
||||
.slash-item:hover, .palette-item:hover { background: var(--bg-hover); }
|
||||
.slash-item code, .palette-item code { font-family: var(--mono); font-size: 12.5px; color: var(--fg); flex: 0 0 auto; }
|
||||
.slash-item.on code, .palette-item.on code { color: var(--accent); }
|
||||
.slash-item .hint { font-family: var(--mono); font-size: 11.5px; color: var(--fg-faint); flex: 0 0 auto; }
|
||||
.slash-item .desc, .palette-item .desc {
|
||||
flex: 1 1 auto; min-width: 0; color: var(--fg-dim); font-size: 12.5px;
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
}
|
||||
#modal-box .palette-list { max-height: min(52vh, 460px); overflow-y: auto; margin-top: 8px; }
|
||||
kbd {
|
||||
font-family: var(--mono); font-size: 11px; color: var(--fg-dim); white-space: nowrap;
|
||||
border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 6px; padding: 1px 6px;
|
||||
background: var(--bg-soft);
|
||||
}
|
||||
.palette-item kbd { flex: 0 0 auto; }
|
||||
#modal-box .keys-grid {
|
||||
display: grid; grid-template-columns: max-content 1fr; gap: 7px 14px; align-items: baseline;
|
||||
font-size: 13px;
|
||||
}
|
||||
#modal-box .keys-grid kbd { justify-self: start; }
|
||||
#modal-box .keys-grid.cmds code { font-family: var(--mono); font-size: 12.5px; }
|
||||
#modal-box .keys-grid span { color: var(--fg-dim); }
|
||||
#modal-box .ext-list { display: flex; flex-direction: column; gap: 2px; }
|
||||
#modal-box .ext-row {
|
||||
display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 8px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
#modal-box .ext-row .pm { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
|
||||
#modal-box .ext-row .pm > div:last-child:not(.ext-title) { overflow-wrap: anywhere; }
|
||||
#modal-box .ext-title { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; font-size: 13.5px; }
|
||||
#modal-box .ext-badge {
|
||||
font-size: 11px; border: 1px solid var(--line-strong); border-radius: 6px; padding: 0 6px; color: var(--fg-dim);
|
||||
}
|
||||
#modal-box .ext-badge.ok { color: var(--accent); border-color: var(--accent-line); }
|
||||
#modal-box .ext-badge.bad { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 55%, transparent); }
|
||||
#modal-box .ext-row > button {
|
||||
flex: 0 0 auto; height: 30px; padding: 0 12px; border-radius: 9px; font-weight: 600; font-size: 13px;
|
||||
border: 1px solid var(--line-strong); background: var(--bg-lift); white-space: nowrap;
|
||||
}
|
||||
|
||||
/* --- narrow screens: rail and panel become drawers ------------------------ */
|
||||
#backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 30; display: none; animation: fade-in var(--t) var(--ease) both; }
|
||||
body.drawer-open #backdrop { display: block; }
|
||||
@media (max-width: 900px) {
|
||||
#app { grid-template-columns: minmax(0, 1fr) !important; transition: none; }
|
||||
#side, #panel {
|
||||
position: fixed; top: 0; bottom: 0; width: min(310px, 86vw); z-index: 31;
|
||||
display: block; transition: transform 260ms var(--ease-pop);
|
||||
}
|
||||
.side-inner { width: auto; transform: none !important; opacity: 1 !important; }
|
||||
#side { overflow: visible; pointer-events: auto; }
|
||||
#side { left: 0; transform: translateX(-102%); }
|
||||
#panel { right: 0; transform: translateX(102%); }
|
||||
body.side-open #side, body.panel-open #panel { transform: none; }
|
||||
.only-narrow { display: inline-flex; }
|
||||
#to-bottom { bottom: 150px; right: 16px; }
|
||||
.msg.user .body { max-width: 88%; }
|
||||
.attach-strip .thumb { width: 120px; height: 120px; }
|
||||
#lightbox { padding: 56px 12px; }
|
||||
}
|
||||
@media (max-width: 560px) {
|
||||
#chip-approval .lbl { display: none; }
|
||||
.seg button { padding: 0 9px; }
|
||||
}
|
||||
@media (min-width: 901px) { #backdrop { display: none !important; } }
|
||||
/* touch screens have no hover: the actions stay visible */
|
||||
@media (hover: none) {
|
||||
.msg-actions, .pend-x { opacity: 1; }
|
||||
}
|
||||
|
||||
/* --- lightbox ------------------------------------------------------------- */
|
||||
#lightbox {
|
||||
position: fixed; inset: 0; z-index: 70; padding: 56px 72px;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
background: rgba(0, 0, 0, .84); animation: fade-in var(--t) var(--ease) both;
|
||||
}
|
||||
#lb-img {
|
||||
max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 12px;
|
||||
box-shadow: 0 20px 60px rgba(0, 0, 0, .5); animation: pop-in 180ms var(--ease) both;
|
||||
}
|
||||
.lb-btn {
|
||||
position: absolute; width: 40px; height: 40px; border-radius: 50%;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
background: rgba(255, 255, 255, .1); color: #fff;
|
||||
transition: background var(--t-fast), transform 90ms var(--ease);
|
||||
}
|
||||
.lb-btn:hover { background: rgba(255, 255, 255, .2); }
|
||||
#lb-close { top: 14px; right: 14px; }
|
||||
#lb-prev { left: 14px; top: calc(50% - 20px); }
|
||||
#lb-next { right: 14px; top: calc(50% - 20px); }
|
||||
#lb-count {
|
||||
position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
|
||||
color: rgba(255, 255, 255, .75); font-size: 13px; font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
/* --- motion --------------------------------------------------------------- */
|
||||
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
|
||||
@keyframes fade-up { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
|
||||
@keyframes rise { from { opacity: 0; transform: translateY(10px) scale(.99); } to { opacity: 1; transform: none; } }
|
||||
@keyframes pop-in { from { opacity: 0; transform: translateY(-4px) scale(.97); } to { opacity: 1; transform: none; } }
|
||||
@keyframes pulse { 0%, 100% { opacity: .3; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }
|
||||
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
|
||||
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -120% 0; } }
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
*, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
|
||||
#scroll { scroll-behavior: auto; }
|
||||
}
|
||||
Reference in New Issue
Block a user