Compare commits

..
72 Commits
Author SHA1 Message Date
ParthSareen 4784290c9b agent: use platform shell tool names 2026-06-26 17:26:38 -07:00
ParthSareen b839fa49f0 agent: stabilize windows shell tool 2026-06-26 16:02:00 -07:00
ParthSareen 93bb382f9a agent: improve shell and tui resilience 2026-06-26 15:45:34 -07:00
ParthSareen 8ed7b66d31 ci: restore golangci-lint PR filtering 2026-06-26 13:25:25 -07:00
ParthSareen 276afbf3a7 chore(agent): tidy module dependencies 2026-06-26 12:26:59 -07:00
ParthSareen 2ae9c05fbd ci: run golangci-lint without PR diff filtering 2026-06-26 12:09:04 -07:00
ParthSareen 6aa08b2c94 cmd/tui/chat: guard chatRunDoneMsg send against context cancellation 2026-06-25 17:04:25 -07:00
ParthSareen b2bc6c822c chore(agent): address modelfile review feedback 2026-06-25 14:30:35 -07:00
ParthSareen 7703a00ade refactor(agent): introduce RunPolicy and drop --hidethinking
Replaces the scattered auto-approve plumbing (ApprovalManager.AutoApprove,
chatPermissionMode, opts.AutoApproveTools, and the approvalHandlerAutoApproves
type-switch) with a single RunPolicy value object carrying ToolMode
(Review/FullAccess/Disabled), an ApprovalPolicy strategy, and MaxToolRounds.
Mode is derived once per surface via resolveAgentRunPolicy, and the TUI toggles
it in place via RunPolicyState.

These two changes land together because they share struct-field plumbing in
agent_tui.go: the --hidethinking removal drops HideThinking from the same
gofmt-realigned AgentTUIOptions/runOptions literals that gain the Policy field,
so splitting them would produce intermediate states that do not build.

Also drops the now-unused PermissionNotice() and RunPolicyState.SetPolicy,
and simplifies the headless tool-resolution block.
2026-06-25 13:20:14 -07:00
ParthSareen 3052b16971 feat(chat): save raw request with /raw > filename
Adds /raw > <file> to write the JSON request preview to a file in the
working directory instead of opening the popup. Routes /raw through a
new handleRawCommand that distinguishes the popup form from the
redirect form, and extracts rawRequestPreviewJSON so the popup and the
save path share one preview build.
2026-06-25 13:19:24 -07:00
ParthSareen 9eed9466fb chore(tui): clean up chat lint 2026-06-24 16:25:21 -07:00
ParthSareen a0d99b4a02 refactor(agent): move chat store out of app 2026-06-24 16:11:45 -07:00
ParthSareen dd82c333ec build(ci): gate app store to desktop platforms 2026-06-24 16:00:44 -07:00
ParthSareen f8750975c1 app/store: repair agent schema migration 2026-06-24 15:43:49 -07:00
ParthSareen 64d3680ab6 cmd/tui/chat: wire cloud auth tick/poll dispatch, auto-open browser, use PollCloudAuth callback 2026-06-24 15:42:00 -07:00
ParthSareen 4c7d01cd4f cmd/tui/chat: skip checking spinner, use pre-computed badge to go straight to sign-in or upgrade 2026-06-24 15:38:47 -07:00
ParthSareen eacaa79706 cmd/tui/chat: show availability badge beside model name instead of plan in meta 2026-06-24 15:34:04 -07:00
ParthSareen 1e004ef209 cmd/tui/chat: add cloud model sign-in and plan-upgrade prompts to model picker 2026-06-24 15:23:57 -07:00
ParthSareen 4a34477557 cmd/tui/chat: add raw request debug view 2026-06-24 14:59:45 -07:00
ParthSareen eb76282573 app/store: add source column to hide agent chats from desktop app 2026-06-24 14:58:35 -07:00
ParthSareen c47ad4f228 cmd: extract multimodal compat check into applyMultiModalCompat helper 2026-06-24 14:51:55 -07:00
ParthSareen e785d4e8b9 agent: require approval for web_search and web_fetch tools 2026-06-24 14:48:59 -07:00
ParthSareen 2e9b631176 cmd/tui/chat: consolidate model and resume pickers into generic chatPicker 2026-06-24 14:48:50 -07:00
ParthSareen 087ffc697f cmd/tui: satisfy chat lint 2026-06-23 18:22:49 -07:00
ParthSareen 11f5c77b4b cmd: present cloud-tagged models with arch details in agent picker 2026-06-23 18:14:48 -07:00
ParthSareen e76f830221 cmd: restore local model arch details in picker description 2026-06-23 18:06:18 -07:00
ParthSareen 3b54956946 cmd/tui: split chat into subpackage and simplify status display 2026-06-23 18:02:33 -07:00
ParthSareen 8f882b054a agent: consolidate compaction helpers and drop trace sink 2026-06-23 14:24:36 -07:00
ParthSareen e098a6afaa refactor(agent): consolidate chat persistence 2026-06-23 13:59:32 -07:00
ParthSareen 183f03d997 agent: address review feedback 2026-06-23 13:31:18 -07:00
ParthSareen 5e9c48cc40 agent: ignore image bytes in prompt estimates 2026-06-23 10:51:58 -07:00
ParthSareen 6b389711f0 chore(agent): satisfy lint checks 2026-06-22 17:01:47 -07:00
ParthSareen f5a50a7c3f agent: harden compaction and tui state 2026-06-22 16:49:41 -07:00
ParthSareen 5104df202d chore(agent): polish chat runtime and rendering 2026-06-22 16:48:38 -07:00
ParthSareen c1ba011f64 agent: add event trace sink 2026-06-22 16:45:01 -07:00
ParthSareen 6d2bbd68e3 feat(agent): refine chat tui flow 2026-06-20 17:23:56 -07:00
ParthSareen 42467c2431 feat(agent): polish chat tui loading and approvals 2026-06-18 21:59:45 -07:00
ParthSareen 9c04e04e6b agent: address review feedback 2026-06-18 16:24:43 -07:00
ParthSareen 98b022f4f5 agent: collapse large pasted TUI input 2026-06-18 14:29:19 -07:00
ParthSareen d60a21fcaa chore(agent): compact small contexts aggressively 2026-06-18 02:16:20 -07:00
ParthSareen 97c93a93b4 chore(agent): harden compaction and runtime cleanup 2026-06-18 01:52:36 -07:00
ParthSareen a058a75099 agent: harden resume and context preflight 2026-06-18 01:23:15 -07:00
ParthSareen c359d22a08 chore: note empty compaction summaries 2026-06-18 00:26:24 -07:00
ParthSareen 0034967cfd feat: refine agent cli flow 2026-06-17 21:30:38 -07:00
ParthSareen 66757a973d chore(agent): handle windows file urls 2026-06-17 16:39:40 -07:00
ParthSareen a718ed68a1 chore(agent): make windows paths portable 2026-06-17 16:24:44 -07:00
ParthSareen 204f719b77 feat(agent): add root onboarding flow 2026-06-17 15:42:21 -07:00
ParthSareen 9618a45286 feat(agent): improve chat tui workflow 2026-06-17 14:02:21 -07:00
ParthSareen ed6a3e6c4e chore: untrack generated docs 2026-06-15 16:53:05 -07:00
ParthSareen 280b85f8a4 agent: harden tool loop and polish tui 2026-06-15 15:33:47 -07:00
ParthSareen 821a45ca6e agent: update compaction and context tracking 2026-06-12 16:28:58 -07:00
ParthSareen 676facf043 agent: update chat tui controls 2026-06-12 16:28:58 -07:00
ParthSareen 50184444da agent: add chat tui controls and legacy aliases 2026-06-12 16:28:58 -07:00
ParthSareen 632bd88937 agent: improve chat input controls 2026-06-12 16:28:58 -07:00
ParthSareen 2ba0f9b96f agent: add output copy commands 2026-06-12 16:28:58 -07:00
ParthSareen 5625904153 agent: window slash command suggestions 2026-06-12 16:28:58 -07:00
ParthSareen 96f82b5ff2 agent: add model picker slash command 2026-06-12 16:28:58 -07:00
ParthSareen 9da4d0c6f8 agent: restore web tool auto approval and TUI scrolling 2026-06-12 16:28:58 -07:00
ParthSareen edd30d7194 docs: add agent improvements report 2026-06-12 16:28:58 -07:00
ParthSareen f417732279 agent: bound markdown and compaction caches 2026-06-12 16:28:58 -07:00
ParthSareen 72d18c99af agent: validate bash working directory 2026-06-12 16:28:58 -07:00
ParthSareen 02befa963c agent: require approval for web tools 2026-06-12 16:28:58 -07:00
ParthSareen a743721ec8 agent: harden bash approval classification 2026-06-12 16:28:58 -07:00
ParthSareen 2b54b72207 tui: split chat implementation 2026-06-12 16:28:58 -07:00
ParthSareen 30f206e235 agent: merge model-aware chat store 2026-06-12 16:28:58 -07:00
ParthSareen 13006826f8 agent: batch streaming persistence 2026-06-12 16:28:58 -07:00
ParthSareen 76eb5a9dc9 tui: track live working directory 2026-06-12 16:28:58 -07:00
ParthSareen cb8af634b3 tui: surface approvals over modals 2026-06-12 16:28:58 -07:00
ParthSareen 66cdf46e7d cmd: remove orphaned repl 2026-06-12 16:28:58 -07:00
ParthSareen a7c8484bea agent: persist chat images 2026-06-12 16:28:58 -07:00
ParthSareen 3be6794929 agent: unify approval dispatch 2026-06-12 16:28:58 -07:00
ParthSareen 444af2a712 feat: promote agent loop to canonical run path 2026-06-12 16:28:58 -07:00
796 changed files with 75619 additions and 115240 deletions

No files matched your search

-366
View File
@@ -1,366 +0,0 @@
#!/usr/bin/env bash
# Prepare MLX runtime libraries for macOS CI unit tests.
#
# Building MLX is expensive, so to enable the MLX-specific unit tests this
# helper finds the newest Ollama release whose MLX_VERSION and MLX_C_VERSION
# match the current checkout, downloads that release's ollama-darwin.tgz, and
# extracts only mlx_metal_v* into build/lib/ollama.
#
# The payload also depends on Ollama's payload build rules (cmake glue and
# carried mlx/compat patches) and the xgrammar native wrapper
# (x/mlxrunner/xgrammar/native). Rule drift rebuilds the whole payload from
# source; wrapper-only drift rebuilds just libollama_xgrammar.dylib.
#
# If no release matches the MLX pins (e.g. right after a pin bump), the
# helper builds the minimal MLX payload for this platform: a single Metal
# variant using the superbuild's platform default (metal_v4 on macOS 26.2+
# SDKs, otherwise metal_v3), including a fresh libollama_xgrammar.dylib.
# Built payloads are cached in the release tarball's layout for later runs.
set -euo pipefail
repo="${OLLAMA_MLX_RELEASE_REPO:-ollama/ollama}"
scan_limit="${OLLAMA_MLX_RELEASE_SCAN_LIMIT:-50}"
cache_dir="${OLLAMA_MLX_DARWIN_CACHE:-.cache/mlx-darwin-release}"
target_dir="${OLLAMA_MLX_DARWIN_TARGET:-build/lib/ollama}"
ci_build_dir="${OLLAMA_MLX_CI_BUILD_DIR:-build/mlx-ci}"
tarball="${cache_dir}/ollama-darwin.tgz"
tag_file="${cache_dir}/matched-tag"
pins_file="${cache_dir}/matched-pins"
target_pins_file="${target_dir}/.mlx-release-pins"
# ${tag_file} value for a cached local build.
local_tag="local-build"
tmpdir=""
tmp_tarball=""
cleanup() {
[ -z "${tmpdir}" ] || rm -rf "${tmpdir}"
[ -z "${tmp_tarball}" ] || rm -f "${tmp_tarball}"
}
trap cleanup EXIT
warn() {
if [ -n "${GITHUB_ACTIONS:-}" ]; then
echo "::warning::$*"
else
echo "warning: $*" >&2
fi
}
read_pin() {
tr -d '[:space:]' <"$1"
}
# Native wrapper sources compiled into libollama_xgrammar.dylib — keep in
# sync with the ollama_xgrammar target in cmake/mlx/CMakeLists.txt.
xgrammar_native_dir=x/mlxrunner/xgrammar/native
# Payload build rules beyond the MLX_VERSION/MLX_C_VERSION pins.
payload_rule_files=(
"cmake/local.cmake"
"cmake/apply-git-patches.cmake"
"cmake/mlx/CMakeLists.txt"
"cmake/mlx/CMakePresets.json"
"x/mlxrunner/mlx/CMakeLists.txt"
)
# Build-rule inputs: the rule files plus carried MLX/MLX-C patch content.
rule_inputs() {
local file
for file in "${payload_rule_files[@]}"; do
printf '%s\n' "${file}"
done
if [ -d mlx/compat ]; then
find mlx/compat -type f | sort
fi
}
wrapper_inputs() {
find "${xgrammar_native_dir}" -type f | sort
}
payload_inputs() {
rule_inputs
wrapper_inputs
}
payload_fingerprint() {
local file
{
payload_inputs
while IFS= read -r file; do
cat "${file}" 2>/dev/null || true
done < <(payload_inputs)
} | shasum -a 256 | awk '{print $1}'
}
# True when the tag matches the checkout on all payload inputs.
tag_matches_payload() {
local tag="$1" file
while IFS= read -r file; do
if ! curl -fsSL "https://raw.githubusercontent.com/${repo}/${tag}/${file}" 2>/dev/null | cmp -s - "${file}"; then
return 1
fi
done < <(payload_inputs)
return 0
}
# True when the tag matches the checkout on the build rules.
tag_matches_rules() {
local tag="$1" file
while IFS= read -r file; do
if ! curl -fsSL "https://raw.githubusercontent.com/${repo}/${tag}/${file}" 2>/dev/null | cmp -s - "${file}"; then
return 1
fi
done < <(rule_inputs)
return 0
}
has_payload() {
local variant
for variant in "${target_dir}"/mlx_metal_v*; do
[ -d "${variant}" ] || continue
[ -f "${variant}/libmlx.dylib" ] && [ -f "${variant}/libmlxc.dylib" ] && return 0
done
return 1
}
has_matching_payload() {
[ -f "${target_pins_file}" ] || return 1
[ "$(cat "${target_pins_file}")" = "${current_pins}" ] || return 1
has_payload || return 1
# Every payload variant must carry libollama_xgrammar.dylib.
local variant
for variant in "${target_dir}"/mlx_metal_v*; do
[ -d "${variant}" ] || continue
[ -f "${variant}/libollama_xgrammar.dylib" ] || return 1
done
return 0
}
extract_payload() {
local tag="$1"
tmpdir="$(mktemp -d)"
tar -xzf "${tarball}" -C "${tmpdir}"
mkdir -p "${target_dir}"
rm -rf "${target_dir}"/mlx_metal_v*
local found=false
local src dest
for src in "${tmpdir}"/mlx_metal_v*; do
[ -d "${src}" ] || continue
found=true
dest="${target_dir}/$(basename "${src}")"
rm -rf "${dest}"
cp -R "${src}" "${dest}"
done
if [ "${found}" != true ] || ! has_payload; then
echo "Downloaded ${tarball} did not contain a usable MLX Metal payload" >&2
exit 1
fi
echo "${current_pins}" >"${target_pins_file}"
echo "Prepared MLX Darwin payload from ${repo} ${tag}:"
find "${target_dir}" -maxdepth 2 -type f \( -name 'libmlx.dylib' -o -name 'libmlxc.dylib' -o -name '*.metallib' \) -print
rm -rf "${tmpdir}"
tmpdir=""
}
# Cache the built payload in the release tarball's layout.
save_built_payload() {
local variant
local -a variants=()
for variant in "${target_dir}"/mlx_metal_v*; do
[ -d "${variant}" ] || continue
variants+=("$(basename "${variant}")")
done
tmp_tarball="${tarball}.tmp"
tar -czf "${tmp_tarball}" -C "${target_dir}" "${variants[@]}"
mv "${tmp_tarball}" "${tarball}"
tmp_tarball=""
echo "${local_tag}" >"${tag_file}"
echo "${current_pins}" >"${pins_file}"
echo "Cached the built payload in ${cache_dir}"
}
# Resolve the superbuild's platform-default MLX backend (metal_v3/metal_v4 on
# arm64; empty when the platform has no MLX backend, e.g. x86_64 macOS).
ci_mlx_backend() {
[ -f "${ci_build_dir}/CMakeCache.txt" ] || return 1
sed -n 's/^OLLAMA_MLX_BACKENDS:STRING=//p' "${ci_build_dir}/CMakeCache.txt"
}
# Configure the repo-root superbuild and fetch MLX/MLX-C sources at the
# pinned revisions (only the full payload build needs this).
build_ci_sources() {
cmake -S . -B "${ci_build_dir}" \
-DOLLAMA_LLAMA_BACKENDS= \
-DOLLAMA_PAYLOAD_INSTALL_PREFIX="$(dirname "$(dirname "${target_dir}")")"
cmake --build "${ci_build_dir}" --target ollama-mlx-sources
}
# Rebuild only libollama_xgrammar.dylib into the extracted payload. The
# target depends only on the pinned XGrammar sources and the native wrapper;
# the Metal toolchain and the superbuild are not involved. MLX is fetched
# only because the cmake/mlx project defines it — nothing from it is built.
build_ci_xgrammar() {
local lib variant
local xg_build_dir="${ci_build_dir}/xgrammar"
local -a configure_args=(-S cmake/mlx -B "${xg_build_dir}" -DOLLAMA_SOURCE_DIR="$(pwd)" -DMLX_BUILD_METAL=OFF)
if [ -n "${OLLAMA_XGRAMMAR_SOURCE:-}" ]; then
configure_args+=("-DFETCHCONTENT_SOURCE_DIR_XGRAMMAR=${OLLAMA_XGRAMMAR_SOURCE}")
fi
cmake "${configure_args[@]}"
cmake --build "${xg_build_dir}" --target ollama_xgrammar
lib="${xg_build_dir}/lib/ollama/libollama_xgrammar.dylib"
[ -f "${lib}" ] || {
echo "ollama_xgrammar build produced no library at ${lib}" >&2
exit 1
}
for variant in "${target_dir}"/mlx_metal_v*; do
[ -d "${variant}" ] || continue
cp -f "${lib}" "${variant}/libollama_xgrammar.dylib"
[ -f "${variant}/libollama_xgrammar.dylib" ] || {
echo "failed to install ${variant}/libollama_xgrammar.dylib" >&2
exit 1
}
done
echo "Rebuilt libollama_xgrammar.dylib from source into ${target_dir}"
}
# Build the minimal MLX payload for this platform: one Metal variant,
# whatever the superbuild defaults to here.
build_ci_payload() {
local backend variant
build_ci_sources
backend="$(ci_mlx_backend)"
case "${backend}" in
metal_v3 | metal_v4) ;;
*)
warn "no MLX backend applicable to this platform; MLX unit tests will be skipped"
exit 0
;;
esac
echo "Building the ${backend} payload for unit tests"
rm -rf "${target_dir}"/mlx_metal_v*
cmake --build "${ci_build_dir}" --target "ollama-mlx-${backend}"
for variant in "${target_dir}"/mlx_metal_v*; do
[ -d "${variant}" ] || continue
for lib in libmlx.dylib libmlxc.dylib libollama_xgrammar.dylib; do
[ -f "${variant}/${lib}" ] || {
echo "built payload is missing ${variant}/${lib}" >&2
exit 1
}
done
done
has_payload || {
echo "built payload is incomplete in ${target_dir}" >&2
exit 1
}
echo "${current_pins}" >"${target_pins_file}"
echo "Built MLX payload for unit tests:"
find "${target_dir}" -maxdepth 2 -type f \( -name 'libmlx.dylib' -o -name 'libmlxc.dylib' -o -name 'libollama_xgrammar.dylib' -o -name '*.metallib' \) -print
save_built_payload
}
if [ "$(uname -s)" != "Darwin" ]; then
warn "MLX Darwin payload setup is only supported on macOS"
exit 0
fi
export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-$(sysctl -n hw.ncpu)}"
current_mlx="$(read_pin MLX_VERSION)"
current_mlxc="$(read_pin MLX_C_VERSION)"
# The release tarball only depends on the MLX pins; the extracted payload's
# xgrammar library additionally depends on the tree's XGrammar inputs.
component_pins="${current_mlx} ${current_mlxc}"
current_pins="${component_pins} $(payload_fingerprint)"
if has_matching_payload; then
echo "MLX payload already present in ${target_dir}"
exit 0
fi
mkdir -p "${cache_dir}"
# Release tarballs are keyed on the MLX pins; local builds on the full fingerprint.
cached_pins="$(cat "${pins_file}" 2>/dev/null || true)"
if [ -s "${tarball}" ] && [ -f "${tag_file}" ] && { [ "${cached_pins}" = "${component_pins}" ] || [ "${cached_pins}" = "${current_pins}" ]; }; then
extract_payload "$(cat "${tag_file}")"
else
matched_tag=""
matched_url=""
while read -r tag; do
[ -n "${tag}" ] || continue
if ! tag_mlx="$(curl -fsSL "https://raw.githubusercontent.com/${repo}/${tag}/MLX_VERSION" | tr -d '[:space:]')"; then
continue
fi
if [ "${tag_mlx}" != "${current_mlx}" ]; then
continue
fi
if ! tag_mlxc="$(curl -fsSL "https://raw.githubusercontent.com/${repo}/${tag}/MLX_C_VERSION" | tr -d '[:space:]')"; then
continue
fi
if [ "${tag_mlxc}" != "${current_mlxc}" ]; then
continue
fi
url="https://github.com/${repo}/releases/download/${tag}/ollama-darwin.tgz"
if curl -fsIL "${url}" >/dev/null; then
matched_tag="${tag}"
matched_url="${url}"
break
fi
echo "MLX pins match ${tag}, but ${url} is not available"
done < <(
git ls-remote --tags --refs --sort=-version:refname "https://github.com/${repo}.git" 'v*' |
awk -v limit="${scan_limit}" '{ sub("refs/tags/", "", $2); print $2; if (limit > 0 && NR >= limit) exit }'
)
if [ -z "${matched_tag}" ]; then
echo "No release carries MLX_VERSION=${current_mlx} MLX_C_VERSION=${current_mlxc}"
build_ci_payload
exit 0
fi
tmp_tarball="${tarball}.tmp"
rm -f "${tmp_tarball}"
curl -fL --retry 3 --retry-delay 2 -o "${tmp_tarball}" "${matched_url}"
mv "${tmp_tarball}" "${tarball}"
tmp_tarball=""
echo "${matched_tag}" >"${tag_file}"
echo "${component_pins}" >"${pins_file}"
extract_payload "${matched_tag}"
fi
tag="$(cat "${tag_file}")"
if [ "${tag}" = "${local_tag}" ] || tag_matches_payload "${tag}"; then
exit 0
fi
if [ "$(uname -m)" != "arm64" ]; then
warn "MLX payload builds are only supported on arm64 macOS; MLX unit tests will be skipped"
exit 0
fi
if tag_matches_rules "$(cat "${tag_file}")"; then
# Only the xgrammar wrapper drifted; keep the rest of the release payload.
echo "Rebuilding libollama_xgrammar.dylib from source into ${target_dir}"
rm -f "${target_dir}"/mlx_metal_v*/libollama_xgrammar.dylib
build_ci_xgrammar
exit 0
fi
echo "Release payload build rules do not match this checkout"
build_ci_payload
+1 -65
View File
@@ -39,27 +39,11 @@ jobs:
APPLE_ID: ${{ vars.APPLE_ID }}
MACOS_SIGNING_KEY: ${{ secrets.MACOS_SIGNING_KEY }}
MACOS_SIGNING_KEY_PASSWORD: ${{ secrets.MACOS_SIGNING_KEY_PASSWORD }}
DEVELOPER_DIR: /Applications/Xcode_26.4.1.app/Contents/Developer
CGO_CFLAGS: '-mmacosx-version-min=14.0 -O3'
CGO_CXXFLAGS: '-mmacosx-version-min=14.0 -O3'
CGO_LDFLAGS: '-mmacosx-version-min=14.0 -O3'
steps:
- uses: actions/checkout@v4
- name: Select Xcode 26.4.1
shell: bash
run: |
set -euo pipefail
if [ ! -d "${DEVELOPER_DIR}" ]; then
echo "Missing ${DEVELOPER_DIR}"
ls -1 /Applications | grep '^Xcode' || true
exit 1
fi
sudo xcode-select -s "${DEVELOPER_DIR}"
sw_vers
xcodebuild -version
xcrun --sdk macosx --show-sdk-version
xcrun --find metal
- run: |
echo $MACOS_SIGNING_KEY | base64 --decode > certificate.p12
security create-keychain -p password build.keychain
@@ -77,8 +61,6 @@ jobs:
MLX_VERSION
MLX_C_VERSION
- run: |
cmake -S . -B build/go-license -DOLLAMA_LLAMA_BACKENDS= -DOLLAMA_MLX_BACKENDS= -DOLLAMA_PAYLOAD_INSTALL_PREFIX=dist/darwin-arm64 "-DOLLAMA_GO_LICENSE_TARGETS=darwin/amd64;darwin/arm64"
cmake --build build/go-license --target ollama-go-license
./scripts/build_darwin.sh
- name: Log build results
run: |
@@ -95,7 +77,6 @@ jobs:
windows-depends:
needs: setup-environment
strategy:
fail-fast: false
matrix:
os: [windows]
arch: [amd64]
@@ -127,22 +108,6 @@ jobs:
- '"nvvm"'
- '"nvptxcompiler"'
cuda-version: '13.0'
- os: windows
arch: amd64
preset: 'CUDA 13 ARM64'
build-steps: cuda13Arm64Cross
install: https://packages.nvidia.com/prerelease/cuda/13.4.0/local_installers/cuda_13.4.0_windows_x86_64.exe
cuda-components:
- '"cudart"'
- '"cudart_cross"'
- '"nvcc"'
- '"nvcc_cross"'
- '"cublas_cross"'
- '"cublas_dev"'
- '"crt"'
- '"nvvm"'
- '"nvptxcompiler"'
cuda-version: '13.4'
- os: windows
arch: amd64
preset: 'ROCm 7'
@@ -171,11 +136,6 @@ jobs:
- '"cufft_dev"'
- '"nvrtc"'
- '"nvrtc_dev"'
- '"cusolver"'
- '"cusolver_dev"'
- '"cusparse"'
- '"cusparse_dev"'
- '"nvjitlink"'
- '"crt"'
- '"nvvm"'
- '"nvptxcompiler"'
@@ -222,18 +182,8 @@ jobs:
name: Install CUDA ${{ matrix.cuda-version }}
run: |
$ErrorActionPreference = "Stop"
$ProgressPreference = 'SilentlyContinue'
if ("${{ steps.cache-install.outputs.cache-hit }}" -ne 'true') {
for ($attempt = 1; $attempt -le 3; $attempt++) {
try {
Invoke-WebRequest -Uri "${{ matrix.install }}" -OutFile "install.exe"
break
} catch {
if ($attempt -eq 3) { throw }
Write-Host "CUDA installer download attempt $attempt failed: $($_.Exception.Message); retrying in 15s"
Start-Sleep -Seconds 15
}
}
Invoke-WebRequest -Uri "${{ matrix.install }}" -OutFile "install.exe"
$subpackages = @(${{ join(matrix.cuda-components, ', ') }}) | Foreach-Object {"${_}_${{ matrix.cuda-version }}"}
Start-Process -FilePath .\install.exe -ArgumentList (@("-s") + $subpackages) -NoNewWindow -Wait
}
@@ -453,16 +403,6 @@ jobs:
do
[ -f "$payload" ] || { echo "missing $payload"; exit 1; }
done
- name: Collect Go licenses
shell: bash
run: |
set -euo pipefail
for arch in amd64 arm64; do
cmake -S . -B build/go-license -DOLLAMA_LLAMA_BACKENDS= -DOLLAMA_MLX_BACKENDS= \
"-DOLLAMA_PAYLOAD_INSTALL_PREFIX=dist/windows-${arch}" \
"-DOLLAMA_GO_LICENSE_TARGETS=windows/${arch}"
cmake --build build/go-license --target ollama-go-license
done
- run: |
./scripts/build_windows.ps1 deps sign installer zip
- name: Log contents after build
@@ -478,7 +418,6 @@ jobs:
linux-depends:
strategy:
fail-fast: false
matrix:
include:
- arch: amd64
@@ -560,7 +499,6 @@ jobs:
# and just assembles, runs the Go build, pushes the final image, and extracts release bundles.
docker-build-push:
strategy:
fail-fast: false
matrix:
include:
- os: linux
@@ -683,7 +621,6 @@ jobs:
lib/ollama/vulkan*) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}.tar.in ;;
lib/ollama/mlx*) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}-mlx.tar.in ;;
lib/ollama/include*) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}-mlx.tar.in ;;
lib/ollama/*_LICENSE|lib/ollama/*_NOTICE) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}.tar.in ;;
lib/ollama/cuda_jetpack5) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}-jetpack5.tar.in ;;
lib/ollama/cuda_jetpack6) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}-jetpack6.tar.in ;;
lib/ollama/rocm_v*) echo $COMPONENT >>ollama-${{ matrix.os }}-${{ matrix.arch }}-rocm.tar.in ;;
@@ -712,7 +649,6 @@ jobs:
# Merge Docker images for the same flavor into a single multi-arch manifest
docker-merge-push:
strategy:
fail-fast: false
matrix:
suffix: ['', '-rocm']
runs-on: linux
+2 -42
View File
@@ -57,10 +57,7 @@ jobs:
MLX_VERSION
MLX_C_VERSION
- name: Build unsigned Darwin runtime
run: |
cmake -S . -B build/go-license -DOLLAMA_LLAMA_BACKENDS= -DOLLAMA_MLX_BACKENDS= -DOLLAMA_PAYLOAD_INSTALL_PREFIX=dist/darwin-arm64 "-DOLLAMA_GO_LICENSE_TARGETS=darwin/amd64;darwin/arm64"
cmake --build build/go-license --target ollama-go-license
./scripts/build_darwin.sh build package
run: ./scripts/build_darwin.sh build package
- name: Log build results
run: ls -l dist/
- uses: actions/upload-artifact@v4
@@ -243,7 +240,6 @@ jobs:
lib/ollama/vulkan*) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}.tar.in ;;
lib/ollama/mlx*) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}-mlx.tar.in ;;
lib/ollama/include*) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}-mlx.tar.in ;;
lib/ollama/*_LICENSE|lib/ollama/*_NOTICE) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}.tar.in ;;
lib/ollama/cuda_jetpack5) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}-jetpack5.tar.in ;;
lib/ollama/cuda_jetpack6) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}-jetpack6.tar.in ;;
lib/ollama/rocm_v*) echo "${COMPONENT}" >>ollama-linux-${{ matrix.arch }}-rocm.tar.in ;;
@@ -325,22 +321,6 @@ jobs:
- '"nvvm"'
- '"nvptxcompiler"'
cuda-version: '13.0'
- os: windows
arch: amd64
preset: 'CUDA 13 ARM64'
build-steps: cuda13Arm64Cross
install: https://packages.nvidia.com/prerelease/cuda/13.4.0/local_installers/cuda_13.4.0_windows_x86_64.exe
cuda-components:
- '"cudart"'
- '"cudart_cross"'
- '"nvcc"'
- '"nvcc_cross"'
- '"cublas_cross"'
- '"cublas_dev"'
- '"crt"'
- '"nvvm"'
- '"nvptxcompiler"'
cuda-version: '13.4'
- os: windows
arch: amd64
preset: 'ROCm 7'
@@ -385,18 +365,8 @@ jobs:
name: Install CUDA ${{ matrix.cuda-version }}
run: |
$ErrorActionPreference = "Stop"
$ProgressPreference = 'SilentlyContinue'
if ("${{ steps.cache-install.outputs.cache-hit }}" -ne 'true') {
for ($attempt = 1; $attempt -le 3; $attempt++) {
try {
Invoke-WebRequest -Uri "${{ matrix.install }}" -OutFile "install.exe"
break
} catch {
if ($attempt -eq 3) { throw }
Write-Host "CUDA installer download attempt $attempt failed: $($_.Exception.Message); retrying in 15s"
Start-Sleep -Seconds 15
}
}
Invoke-WebRequest -Uri "${{ matrix.install }}" -OutFile "install.exe"
$subpackages = @(${{ join(matrix.cuda-components, ', ') }}) | Foreach-Object {"${_}_${{ matrix.cuda-version }}"}
Start-Process -FilePath .\install.exe -ArgumentList (@("-s") + $subpackages) -NoNewWindow -Wait
}
@@ -581,16 +551,6 @@ jobs:
do
[ -f "$payload" ] || { echo "missing $payload"; exit 1; }
done
- name: Collect Go licenses
shell: bash
run: |
set -euo pipefail
for arch in amd64 arm64; do
cmake -S . -B build/go-license -DOLLAMA_LLAMA_BACKENDS= -DOLLAMA_MLX_BACKENDS= \
"-DOLLAMA_PAYLOAD_INSTALL_PREFIX=dist/windows-${arch}" \
"-DOLLAMA_GO_LICENSE_TARGETS=windows/${arch}"
cmake --build build/go-license --target ollama-go-license
done
- name: Build unsigned Windows installer and zips
run: ./scripts/build_windows.ps1 deps installer zip
- name: Log contents after build
+7 -92
View File
@@ -23,7 +23,6 @@ jobs:
outputs:
changed: ${{ steps.changes.outputs.changed }}
app_changed: ${{ steps.changes.outputs.app_changed }}
go_mod_changed: ${{ steps.changes.outputs.go_mod_changed }}
enginehash: ${{ steps.changes.outputs.enginehash }}
steps:
- uses: actions/checkout@v4
@@ -53,11 +52,8 @@ jobs:
'ml/backend/ggml/ggml/**/*' \
'x/imagegen/mlx/**' \
'x/imagegen/mlx/**/*' \
'x/mlxrunner/xgrammar/native/**' \
'x/mlxrunner/xgrammar/native/**/*' \
'.github/**/*') | tee -a $GITHUB_OUTPUT
echo app_changed=$(changed 'app/**' 'app/**/*') | tee -a $GITHUB_OUTPUT
echo go_mod_changed=$(changed 'go.mod') | tee -a $GITHUB_OUTPUT
echo enginehash=$(cat LLAMA_CPP_VERSION)-$(cat MLX_VERSION)-$(cat MLX_C_VERSION) | tee -a $GITHUB_OUTPUT
patches:
@@ -120,7 +116,7 @@ jobs:
superbuild_target: ollama-mlx-cuda_v13
superbuild_dir: build/local-superbuild-mlx-cuda_v13
superbuild_args: '-DOLLAMA_MLX_BACKENDS=cuda_v13 -DCMAKE_CUDA_ARCHITECTURES=87 -DMLX_CUDA_ARCHITECTURES=80-virtual -DBLAS_INCLUDE_DIRS=/usr/include/x86_64-linux-gnu -DLAPACK_INCLUDE_DIRS=/usr/include/x86_64-linux-gnu'
expected_payload: lib/ollama/mlx_cuda_v13/libmlx.so lib/ollama/mlx_cuda_v13/libollama_xgrammar.so
expected_payload: lib/ollama/mlx_cuda_v13/libmlx.so
install-go: true
runs-on: linux
container: ${{ matrix.container }}
@@ -162,9 +158,7 @@ jobs:
run: |
cmake -S . -B "${{ matrix.superbuild_dir }}" ${{ matrix.superbuild_args }}
CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) cmake --build "${{ matrix.superbuild_dir }}" --target "${{ matrix.superbuild_target }}" -- -l $(nproc)
for f in ${{ matrix.expected_payload }}; do
test -e "${{ matrix.superbuild_dir }}/$f"
done
test -e "${{ matrix.superbuild_dir }}/${{ matrix.expected_payload }}"
- name: Verify local superbuild install
if: matrix.superbuild_target == 'ollama-local'
run: |
@@ -220,7 +214,7 @@ jobs:
superbuild_target: ollama-mlx-cuda_v13
superbuild_dir: build\local-superbuild-mlx-cuda_v13
superbuild_args: '-DOLLAMA_MLX_BACKENDS=cuda_v13 -DCMAKE_CUDA_ARCHITECTURES=80 -DMLX_CUDA_ARCHITECTURES=80-virtual'
expected_payload: lib\ollama\mlx_cuda_v13\mlx.dll lib\ollama\mlx_cuda_v13\ollama_xgrammar.dll
expected_payload: lib\ollama\mlx_cuda_v13\mlx.dll
install-go: true
cuda-components:
- '"cudart"'
@@ -231,11 +225,6 @@ jobs:
- '"cufft_dev"'
- '"nvrtc"'
- '"nvrtc_dev"'
- '"cusolver"'
- '"cusolver_dev"'
- '"cusparse"'
- '"cusparse_dev"'
- '"nvjitlink"'
- '"crt"'
- '"nvvm"'
- '"nvptxcompiler"'
@@ -345,10 +334,8 @@ jobs:
cmake -S . -B "${{ matrix.superbuild_dir }}" ${{ matrix.superbuild_args }}
$env:CMAKE_BUILD_PARALLEL_LEVEL = [Environment]::ProcessorCount
cmake --build "${{ matrix.superbuild_dir }}" --target "${{ matrix.superbuild_target }}" -- -l $([Environment]::ProcessorCount)
foreach ($f in "${{ matrix.expected_payload }}".Split(' ')) {
if (!(Test-Path "${{ matrix.superbuild_dir }}\$f")) {
throw "missing $f"
}
if (!(Test-Path "${{ matrix.superbuild_dir }}\${{ matrix.expected_payload }}")) {
throw "missing ${{ matrix.expected_payload }}"
}
env:
CMAKE_GENERATOR: Ninja
@@ -375,24 +362,6 @@ jobs:
- name: check that 'go mod tidy' is clean
run: go mod tidy --diff || (echo "Please run 'go mod tidy'." && exit 1)
go_license:
needs: [changes]
if: needs.changes.outputs.go_mod_changed == 'True'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Verify Go dependency licenses
run: |
# See cmake/generate_go_license.cmake for special case handling.
cmake -S . -B build/go-license \
-DOLLAMA_LLAMA_BACKENDS= \
-DOLLAMA_MLX_BACKENDS= \
"-DOLLAMA_GO_LICENSE_TARGETS=linux/amd64;linux/arm64;darwin/amd64;darwin/arm64;windows/amd64;windows/arm64"
cmake --build build/go-license --target ollama-go-license
test:
needs: [changes]
strategy:
@@ -414,16 +383,6 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '20'
- name: Cache MLX Darwin release payload
if: ${{ startsWith(matrix.os, 'macos') }}
uses: actions/cache@v4
with:
path: .cache/mlx-darwin-release
# Key on every payload input so a source-built payload survives pushes.
key: mlx-darwin-${{ hashFiles('MLX_VERSION', 'MLX_C_VERSION', 'cmake/local.cmake', 'cmake/apply-git-patches.cmake', 'cmake/mlx/CMakeLists.txt', 'cmake/mlx/CMakePresets.json', 'x/mlxrunner/mlx/CMakeLists.txt', 'mlx/compat/**', 'x/mlxrunner/xgrammar/native/**') }}
- name: Prepare MLX Darwin release payload
if: ${{ startsWith(matrix.os, 'macos') }}
run: .github/scripts/prepare_mlx_darwin.sh
- name: Install UI dependencies
working-directory: ./app/ui/app
run: npm ci
@@ -448,58 +407,14 @@ jobs:
- name: Run go generate
run: go generate ./...
- name: Verify UI generated types are current
if: ${{ startsWith(matrix.os, 'ubuntu') }}
run: git diff --exit-code -- app/ui/app/codegen/gotypes.gen.ts
- name: go test
if: always()
# Smoke-run each benchmark once to catch panics and bit rot; this does
# not assert timings. -benchtime without -bench is inert.
run: go test -count=1 -bench=. -benchtime=1x ./...
run: go test -count=1 -benchtime=1x ./...
- name: go test app with live updater tag
if: ${{ needs.changes.outputs.app_changed == 'True' && contains(fromJSON('["macos-latest","windows-latest"]'), matrix.os) }}
run: go test -count=1 -tags updater_live ./app/...
- uses: golangci/golangci-lint-action@v9
race:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
env:
CGO_ENABLED: '1'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache-dependency-path: |
go.sum
LLAMA_CPP_VERSION
MLX_VERSION
MLX_C_VERSION
- name: Cache MLX Darwin release payload
if: ${{ startsWith(matrix.os, 'macos') }}
uses: actions/cache@v4
with:
path: .cache/mlx-darwin-release
# Key on every payload input so a source-built payload survives pushes.
key: mlx-darwin-${{ hashFiles('MLX_VERSION', 'MLX_C_VERSION', 'cmake/local.cmake', 'cmake/apply-git-patches.cmake', 'cmake/mlx/CMakeLists.txt', 'cmake/mlx/CMakePresets.json', 'x/mlxrunner/mlx/CMakeLists.txt', 'mlx/compat/**', 'x/mlxrunner/xgrammar/native/**') }}
- name: Prepare MLX Darwin release payload
if: ${{ startsWith(matrix.os, 'macos') }}
run: .github/scripts/prepare_mlx_darwin.sh
- uses: actions/setup-node@v4
with:
node-version: '20'
# app/ui embeds app/dist, so the UI has to be built before app/... will
# even compile.
- name: Build UI
working-directory: ./app/ui/app
run: |
npm ci
npm run build
- name: go test -race
run: go test -race -count=1 ./...
only-new-issues: true
-4
View File
@@ -45,10 +45,6 @@ if(APPLE)
set(CMAKE_BUILD_RPATH "@loader_path")
set(CMAKE_INSTALL_RPATH "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
elseif(UNIX)
set(CMAKE_BUILD_RPATH "$ORIGIN")
set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
set(OLLAMA_BUILD_DIR ${CMAKE_BINARY_DIR}/lib/ollama)
+9 -25
View File
@@ -15,9 +15,9 @@ FROM scratch AS local-mlx
FROM scratch AS local-mlx-c
FROM --platform=linux/amd64 rocm/dev-almalinux-8:${ROCMVERSION}-complete AS base-amd64
RUN dnf install -y yum-utils ccache gcc-toolset-13-gcc gcc-toolset-13-gcc-c++ gcc-toolset-13-binutils \
RUN dnf install -y yum-utils ccache gcc-toolset-11-gcc gcc-toolset-11-gcc-c++ gcc-toolset-11-binutils \
&& yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-rhel8.repo
ENV PATH=/opt/rh/gcc-toolset-13/root/usr/bin:$PATH
ENV PATH=/opt/rh/gcc-toolset-11/root/usr/bin:$PATH
FROM --platform=linux/arm64 almalinux:8 AS base-arm64
# install epel-release for ccache
@@ -42,8 +42,8 @@ ENV LDFLAGS=-s
#
FROM base AS cpu-deps
RUN dnf install -y gcc-toolset-13-gcc gcc-toolset-13-gcc-c++
ENV PATH=/opt/rh/gcc-toolset-13/root/usr/bin:$PATH
RUN dnf install -y gcc-toolset-11-gcc gcc-toolset-11-gcc-c++
ENV PATH=/opt/rh/gcc-toolset-11/root/usr/bin:$PATH
FROM base AS cuda-12-deps
ARG CUDA12VERSION=12.8
@@ -74,7 +74,7 @@ RUN ln -s /usr/bin/python3 /usr/bin/python \
ENV VULKAN_SDK=/usr/local
#
# llama-server stages — rebuild when LLAMA_CPP_VERSION, llama/server/, llama/compat/, or cmake/ changes.
# llama-server stages — rebuild when LLAMA_CPP_VERSION, llama/server/, or llama/compat/ changes.
#
# CPU stage: llama-server + ggml-base + ggml-cpu variants → lib/ollama/
# GPU stages: GPU backend .so only → lib/ollama/<variant>/
@@ -84,7 +84,6 @@ FROM cpu-deps AS llama-server-cpu
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset cpu \
&& cmake --build build/llama-server-cpu -- -l $(nproc) \
@@ -92,8 +91,8 @@ RUN --mount=type=cache,target=/root/.ccache \
&& for lib in \
/usr/lib64/libgomp.so* \
/usr/lib64/libomp.so* \
/opt/rh/gcc-toolset-13/root/usr/lib64/libgomp.so* \
/opt/rh/gcc-toolset-13/root/usr/lib64/libomp.so*; do \
/opt/rh/gcc-toolset-11/root/usr/lib64/libgomp.so* \
/opt/rh/gcc-toolset-11/root/usr/lib64/libomp.so*; do \
[ -e "$lib" ] && cp -a "$lib" dist/lib/ollama/ || true; \
done
@@ -104,7 +103,6 @@ FROM cuda-12-deps AS llama-server-cuda_v12
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset llama_cuda_v12_linux \
&& cmake --build build/llama-server-cuda_v12 -- -l $(nproc) \
@@ -117,7 +115,6 @@ FROM cuda-13-deps AS llama-server-cuda_v13
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset llama_cuda_v13_linux \
&& cmake --build build/llama-server-cuda_v13 -- -l $(nproc) \
@@ -127,11 +124,10 @@ FROM scratch AS publish-llama-server-cuda_v13
COPY --from=llama-server-cuda_v13 dist/lib/ollama /lib/ollama/
FROM rocm-7-deps AS llama-server-rocm_v7_2
ENV CC=clang CXX=clang++ CXXFLAGS=--gcc-toolchain=/opt/rh/gcc-toolset-13/root/usr
ENV CC=clang CXX=clang++
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset rocm_v7_2_linux \
&& cmake --build build/llama-server-rocm_v7_2 -- -l $(nproc) \
@@ -145,7 +141,6 @@ FROM vulkan-deps AS llama-server-vulkan
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset vulkan \
&& cmake --build build/llama-server-vulkan -- -l $(nproc) \
@@ -170,7 +165,6 @@ ENV CMAKE_GENERATOR=Ninja
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset llama_cuda_jetpack5 \
&& cmake --build build/llama-server-cuda_jetpack5 -- -l $(nproc) \
@@ -191,7 +185,6 @@ ENV CMAKE_GENERATOR=Ninja
COPY LLAMA_CPP_VERSION .
COPY llama/server llama/server
COPY llama/compat llama/compat
COPY cmake cmake
RUN --mount=type=cache,target=/root/.ccache \
cmake -S llama/server --preset llama_cuda_jetpack6 \
&& cmake --build build/llama-server-cuda_jetpack6 -- -l $(nproc) \
@@ -220,8 +213,7 @@ ENV CGO_LDFLAGS="-L/usr/local/cuda-13/lib64 -L/usr/local/cuda-13/targets/x86_64-
WORKDIR /go/src/github.com/ollama/ollama
COPY CMakeLists.txt CMakePresets.json .
COPY cmake cmake
COPY x/mlxrunner/mlx x/mlxrunner/mlx
COPY x/mlxrunner/xgrammar/native x/mlxrunner/xgrammar/native
COPY x/imagegen/mlx x/imagegen/mlx
COPY go.mod go.sum .
COPY MLX_VERSION MLX_C_VERSION .
RUN curl -fsSL https://golang.org/dl/go$(awk '/^go/ { print $2 }' go.mod).linux-$(case $(uname -m) in x86_64) echo amd64 ;; aarch64) echo arm64 ;; esac).tar.gz | tar xz -C /usr/local
@@ -261,15 +253,9 @@ ENV CGO_CFLAGS="${CGO_CFLAGS}"
ENV CGO_CXXFLAGS="${CGO_CXXFLAGS}"
RUN --mount=type=cache,target=/root/.cache/go-build \
go build -trimpath -buildmode=pie -o /bin/ollama .
RUN --mount=type=cache,target=/root/.cache/go-build \
cmake -S . -B build/go-license \
-DOLLAMA_LLAMA_BACKENDS= \
-DOLLAMA_MLX_BACKENDS= \
&& cmake --build build/go-license --target ollama-go-license
FROM scratch AS publish-go
COPY --from=build /bin/ollama /bin/ollama
COPY --from=build /go/src/github.com/ollama/ollama/build/go-license/lib/ollama/GO_LICENSE /lib/ollama/GO_LICENSE
#
# Assembly stages — combine llama-server variants + GPU runtime libs
@@ -302,11 +288,9 @@ COPY --from=arm64 /lib/ollama /lib/ollama/
FROM ${TARGETARCH}-archive AS archive
COPY --from=build /bin/ollama /bin/ollama
COPY --from=build /go/src/github.com/ollama/ollama/build/go-license/lib/ollama/GO_LICENSE /lib/ollama/GO_LICENSE
FROM ${FLAVOR} AS image-archive
COPY --from=build /bin/ollama /bin/ollama
COPY --from=build /go/src/github.com/ollama/ollama/build/go-license/lib/ollama/GO_LICENSE /lib/ollama/GO_LICENSE
FROM ubuntu:24.04
ARG APT_MIRROR=http://archive.ubuntu.com/ubuntu
+1 -1
View File
@@ -1 +1 @@
b10760
b9509
+1 -1
View File
@@ -1 +1 @@
c74db5307cc8ce122f48d97ef951b30578674e7f
fba4470b89073180056c9ea46c443051375f7399
+1 -1
View File
@@ -1 +1 @@
37c26e5755da637255d57ea34b4879196a485301
2165dc08d7b33258260aa849d39f087d50e62962
+1 -1
View File
@@ -65,7 +65,7 @@ To launch a specific integration:
ollama launch claude
```
Supported integrations include [Claude Code](https://docs.ollama.com/integrations/claude-code), [Codex](https://docs.ollama.com/integrations/codex), [Copilot CLI](https://docs.ollama.com/integrations/copilot-cli), [DeepSeek Harness](https://docs.ollama.com/integrations/deepseek-harness), [Droid](https://docs.ollama.com/integrations/droid), and [OpenCode](https://docs.ollama.com/integrations/opencode).
Supported integrations include [Claude Code](https://docs.ollama.com/integrations/claude-code), [Codex](https://docs.ollama.com/integrations/codex), [Copilot CLI](https://docs.ollama.com/integrations/copilot-cli), [Droid](https://docs.ollama.com/integrations/droid), and [OpenCode](https://docs.ollama.com/integrations/opencode).
### AI assistant
+859 -152
View File
File diff suppressed because it is too large. Load diff
+354 -65
View File
@@ -8,88 +8,377 @@ import (
"github.com/ollama/ollama/api"
)
type mockTool struct {
name string
type recordingApprovalPrompter struct {
requests []ApprovalRequest
results []ApprovalResult
}
func (m mockTool) Name() string { return m.name }
func (m mockTool) Description() string { return "" }
func (m mockTool) Schema() api.ToolFunction {
return api.ToolFunction{Name: m.name}
}
type allowWithoutPromptPolicy struct{}
func (m mockTool) Execute(context.Context, ToolContext, map[string]any) (ToolResult, error) {
return ToolResult{}, nil
}
type approvalRequiredTestTool struct{}
func TestToolApprovalScopeUsesScopedTool(t *testing.T) {
shellTool := mockScopedTool{
mockTool: mockTool{name: "bash"},
scope: func(args map[string]any) string {
if cmd, ok := args["command"].(string); ok {
cmd = strings.TrimSpace(cmd)
if cmd != "" {
return "bash\x00" + cmd
}
}
return "bash"
},
func (p *recordingApprovalPrompter) PromptApproval(_ context.Context, request ApprovalRequest) (ApprovalResult, error) {
p.requests = append(p.requests, request)
if len(p.results) == 0 {
return ApprovalResult{Decision: ApprovalAllowOnce}, nil
}
plainTool := mockTool{name: "edit"}
result := p.results[0]
p.results = p.results[1:]
return result, nil
}
tests := []struct {
tool Tool
name string
args map[string]any
want string
}{
{shellTool, "bash", map[string]any{"command": " pwd "}, "bash\x00pwd"},
{shellTool, "bash", map[string]any{"command": "Get-ChildItem"}, "bash\x00Get-ChildItem"},
{plainTool, "edit", map[string]any{"path": "README.md"}, "edit"},
func (allowWithoutPromptPolicy) EvaluateApproval(context.Context, ApprovalRequest) ApprovalEvaluation {
return ApprovalEvaluation{Decision: ApprovalAllowOnce, Risk: ApprovalRiskLow}
}
func (approvalRequiredTestTool) Name() string {
return "approval_required"
}
func (approvalRequiredTestTool) Description() string {
return "requires approval"
}
func (approvalRequiredTestTool) Schema() api.ToolFunction {
return api.ToolFunction{Name: "approval_required"}
}
func (approvalRequiredTestTool) Execute(context.Context, ToolContext, map[string]any) (ToolResult, error) {
return ToolResult{Content: "ok"}, nil
}
func (approvalRequiredTestTool) RequiresApproval(map[string]any) bool {
return true
}
func TestApprovalManagerAllowsSafeToolsWithoutPrompt(t *testing.T) {
prompter := &recordingApprovalPrompter{}
manager := NewApprovalManager(ApprovalManagerOptions{Prompter: prompter})
result, err := manager.Approve(context.Background(), ApprovalRequest{
ToolName: "read",
Args: map[string]any{"path": "README.md"},
WorkingDir: t.TempDir(),
})
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
if got := toolApprovalScope(tt.tool, tt.name, tt.args); got != tt.want {
t.Fatalf("toolApprovalScope(%q) = %q, want %q", tt.name, got, tt.want)
if result.Decision != ApprovalAllowOnce {
t.Fatalf("decision = %q, want allow_once", result.Decision)
}
if len(prompter.requests) != 0 {
t.Fatalf("safe tool prompted: %#v", prompter.requests)
}
}
func TestApprovalManagerToolRequiredOverridePromptsInApprove(t *testing.T) {
prompter := &recordingApprovalPrompter{}
manager := NewApprovalManager(ApprovalManagerOptions{Policy: allowWithoutPromptPolicy{}, Prompter: prompter})
tool := approvalRequiredTestTool{}
request := ApprovalRequest{
ToolName: tool.Name(),
Args: map[string]any{},
ToolApprovalRequired: ToolRequiresApproval(tool, nil),
}
if !manager.RequiresApproval(context.Background(), tool, request) {
t.Fatal("tool-required approval should require a prompt")
}
result, err := manager.Approve(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalAllowOnce {
t.Fatalf("decision = %q, want allow_once", result.Decision)
}
if len(prompter.requests) != 1 {
t.Fatalf("prompts = %d, want 1", len(prompter.requests))
}
}
func TestApprovalManagerDeniesEscapingPath(t *testing.T) {
manager := NewApprovalManager(ApprovalManagerOptions{})
result, err := manager.Approve(context.Background(), ApprovalRequest{
ToolName: "edit",
Args: map[string]any{"path": "../outside.txt"},
WorkingDir: t.TempDir(),
})
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalDeny {
t.Fatalf("decision = %q, want deny", result.Decision)
}
if !strings.Contains(result.Reason, "path escapes working directory") {
t.Fatalf("reason = %q", result.Reason)
}
}
func TestApprovalManagerSanitizesEditSummary(t *testing.T) {
evaluation := evaluateEditApproval(ApprovalRequest{
ToolName: "edit",
Args: map[string]any{"path": "notes/\x1b[31mred\nfile.txt"},
WorkingDir: t.TempDir(),
})
if strings.ContainsAny(evaluation.Summary, "\n\r\x1b") {
t.Fatalf("summary contains control characters: %q", evaluation.Summary)
}
if !strings.Contains(evaluation.Summary, "notes/red file.txt") {
t.Fatalf("summary = %q, want sanitized path", evaluation.Summary)
}
}
func TestApprovalManagerPromptsForEdit(t *testing.T) {
prompter := &recordingApprovalPrompter{}
manager := NewApprovalManager(ApprovalManagerOptions{Prompter: prompter})
result, err := manager.Approve(context.Background(), ApprovalRequest{
ToolName: "edit",
Args: map[string]any{"path": "note.txt", "old_text": "old", "new_text": "new"},
WorkingDir: t.TempDir(),
})
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalAllowOnce {
t.Fatalf("decision = %q, want allow_once", result.Decision)
}
if len(prompter.requests) != 1 {
t.Fatalf("prompts = %d, want 1", len(prompter.requests))
}
request := prompter.requests[0]
if request.Risk != ApprovalRiskMedium {
t.Fatalf("risk = %q, want medium", request.Risk)
}
if !strings.Contains(strings.Join(request.Reasons, " "), "writes to a file") {
t.Fatalf("reasons = %#v", request.Reasons)
}
}
func TestApprovalManagerHeadlessDeniesPromptRequiredTools(t *testing.T) {
manager := NewApprovalManager(ApprovalManagerOptions{})
result, err := manager.Approve(context.Background(), ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": "pwd"},
})
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalDeny {
t.Fatalf("decision = %q, want deny", result.Decision)
}
if !strings.Contains(result.Reason, "--auto-approve-tools") {
t.Fatalf("reason = %q", result.Reason)
}
}
func TestApprovalManagerSessionAllowList(t *testing.T) {
prompter := &recordingApprovalPrompter{
results: []ApprovalResult{{Decision: ApprovalAllowSession}},
}
manager := NewApprovalManager(ApprovalManagerOptions{Prompter: prompter})
request := ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": "go test ./agent"},
}
result, err := manager.Approve(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalAllowSession {
t.Fatalf("decision = %q, want allow_session", result.Decision)
}
result, err = manager.Approve(context.Background(), request)
if err != nil {
t.Fatal(err)
}
if result.Decision != ApprovalAllowOnce {
t.Fatalf("second decision = %q, want allow_once", result.Decision)
}
if len(prompter.requests) != 1 {
t.Fatalf("prompts = %d, want 1", len(prompter.requests))
}
}
func TestBashApprovalClassifiesHighRiskShell(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": "cd / && rm -rf tmp"},
})
if !evaluation.RequirePrompt {
t.Fatal("bash should require prompt")
}
if evaluation.Risk != ApprovalRiskHigh {
t.Fatalf("risk = %q, want high", evaluation.Risk)
}
reasons := strings.Join(evaluation.Reasons, " ")
for _, want := range []string{"changes directory", "control operator", "removes files"} {
if !strings.Contains(reasons, want) {
t.Fatalf("reasons = %#v, want %q", evaluation.Reasons, want)
}
}
}
type mockScopedTool struct {
mockTool
scope func(args map[string]any) string
}
func TestPowerShellApprovalUsesShellPolicy(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: "powershell",
Args: map[string]any{"command": "Remove-Item -Recurse tmp"},
})
func (m mockScopedTool) ApprovalScope(args map[string]any) string {
return m.scope(args)
}
func TestSessionApplyApprovalScopes(t *testing.T) {
session := &Session{}
result := Approval{AllowScopes: []string{"edit", "bash\x00pwd", " "}}
session.applyApproval(&result)
if !result.Allow {
t.Fatal("scoped approval should allow the current request")
if !evaluation.RequirePrompt {
t.Fatal("powershell should require prompt")
}
if !session.allows("edit") || !session.allows("bash\x00pwd") {
t.Fatal("scoped approval was not saved")
if evaluation.Summary != "PowerShell wants to run a command" {
t.Fatalf("summary = %q", evaluation.Summary)
}
if session.allows("bash") || session.allows("bash\x00ls") {
t.Fatal("shell approval was too broad")
}
if session.ApprovalState.AllGranted() {
t.Fatal("allow all = true, want false for scoped approval")
if evaluation.SessionKey != "powershell:Remove-Item -Recurse tmp" {
t.Fatalf("session key = %q", evaluation.SessionKey)
}
}
func TestSessionApplyApprovalAllowAll(t *testing.T) {
session := &Session{}
result := Approval{AllowAll: true}
func TestBashApprovalClassifiesDynamicShellEvasions(t *testing.T) {
tests := []struct {
name string
cmd string
reason string
}{
{name: "function declaration", cmd: "f() { rm -rf /; } && f", reason: "defines shell functions"},
{name: "eval", cmd: `eval "$cmd"`, reason: "evaluates shell code"},
{name: "variable command name", cmd: "$DANGER --flag", reason: "dynamic command name"},
{name: "command substitution command name", cmd: "$(echo rm) -rf /", reason: "dynamic command name"},
}
session.applyApproval(&result)
if !result.Allow || !session.allows("anything") {
t.Fatalf("allow all = %v result = %#v, want allow all", session.ApprovalState.AllGranted(), result)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": tt.cmd},
})
if !evaluation.RequirePrompt {
t.Fatal("bash evasion should require prompt")
}
if evaluation.Risk != ApprovalRiskHigh {
t.Fatalf("risk = %q, want high", evaluation.Risk)
}
if reasons := strings.Join(evaluation.Reasons, " "); !strings.Contains(reasons, tt.reason) {
t.Fatalf("reasons = %#v, want %q", evaluation.Reasons, tt.reason)
}
})
}
}
func TestBashApprovalClassifiesDestructiveGitWithGlobalOptions(t *testing.T) {
tests := []struct {
name string
cmd string
reason string
}{
{name: "git reset hard after cwd", cmd: "git -C /tmp reset --hard", reason: "runs destructive git reset"},
{name: "git reset hard after config", cmd: "git -c core.autocrlf=false reset --hard", reason: "runs destructive git reset"},
{name: "git clean after work tree", cmd: "git --work-tree=/tmp clean -fdx", reason: "runs destructive git clean"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": tt.cmd},
})
if evaluation.Risk != ApprovalRiskHigh {
t.Fatalf("risk = %q, want high", evaluation.Risk)
}
if reasons := strings.Join(evaluation.Reasons, " "); !strings.Contains(reasons, tt.reason) {
t.Fatalf("reasons = %#v, want %q", evaluation.Reasons, tt.reason)
}
})
}
}
func TestBashApprovalClassifiesFindMutations(t *testing.T) {
tests := []struct {
name string
cmd string
reason string
}{
{name: "delete", cmd: "find . -name '*.tmp' -delete", reason: "deletes files via find"},
{name: "exec", cmd: `find . -name '*.tmp' -exec rm -rf {} \;`, reason: "executes commands via find"},
{name: "exec nested destructive command", cmd: `find . -name '*.tmp' -exec rm -rf {} \;`, reason: "removes files destructively"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: "bash",
Args: map[string]any{"command": tt.cmd},
})
if evaluation.Risk != ApprovalRiskHigh {
t.Fatalf("risk = %q, want high", evaluation.Risk)
}
if reasons := strings.Join(evaluation.Reasons, " "); !strings.Contains(reasons, tt.reason) {
t.Fatalf("reasons = %#v, want %q", evaluation.Reasons, tt.reason)
}
})
}
}
func TestWebApprovalRequiresPrompt(t *testing.T) {
tests := []struct {
name string
tool string
args map[string]any
summary string
}{
{
name: "search",
tool: "web_search",
args: map[string]any{"query": "Ollama agents"},
summary: "Web Search wants to search for \"Ollama agents\"",
},
{
name: "fetch",
tool: "web_fetch",
args: map[string]any{"url": "https://ollama.com"},
summary: "Web Fetch wants to fetch https://ollama.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: tt.tool,
Args: tt.args,
})
if !evaluation.RequirePrompt {
t.Fatal("web tool should require prompt")
}
if evaluation.Decision != "" && evaluation.Decision != ApprovalAllowOnce {
t.Fatalf("decision = %q, want allow once", evaluation.Decision)
}
if evaluation.Risk != ApprovalRiskMedium {
t.Fatalf("risk = %q, want medium", evaluation.Risk)
}
if evaluation.Summary != tt.summary {
t.Fatalf("summary = %q, want %q", evaluation.Summary, tt.summary)
}
})
}
}
func TestWebApprovalDeniesMissingArgs(t *testing.T) {
for _, tool := range []string{"web_search", "web_fetch"} {
evaluation := DefaultApprovalPolicy{}.EvaluateApproval(context.Background(), ApprovalRequest{
ToolName: tool,
Args: map[string]any{},
})
if evaluation.Decision != ApprovalDeny {
t.Fatalf("%s missing args decision = %q, want deny", tool, evaluation.Decision)
}
if evaluation.Risk != ApprovalRiskHigh {
t.Fatalf("%s missing args risk = %q, want high", tool, evaluation.Risk)
}
}
}
+66 -97
View File
@@ -12,7 +12,9 @@ import (
)
// Compaction wire-format. These constants and helpers are the single canonical
// definition of how a compacted turn is represented in message history.
// definition of how a compacted turn is represented in message history; both
// the in-memory compactor (this package) and the on-disk chat store
// (package store) build and detect summaries through them.
const (
CompactionSummaryMessagePrefix = "Conversation summary:\n"
CompactionToolName = "summary"
@@ -26,25 +28,18 @@ const (
defaultCompactionThreshold = 0.8
compactOnlySummaryContextTokens = 16000
maxCompactionSummaryRunes = 16 * 1024
maxCompactionSummaryBytes = 16 * 1024
compactionSummaryTruncated = "\n\n[summary truncated]"
compactionSystemPrompt = "Summarize the archived part of an Ollama agent conversation. Preserve user goals, decisions, files, commands, tool results, and unresolved tasks needed to continue. Omit private reasoning and return only the summary."
compactionSystemPrompt = "Summarize the archived part of an Ollama CLI agent conversation. Preserve user goals, decisions, files, commands, tool results, and unresolved tasks needed to continue. Omit private reasoning and return only the summary."
)
type Compactor interface {
MaybeCompact(context.Context, CompactionRequest) (CompactionResult, error)
}
// ContextWindowTokens returns the effective context window size in
// tokens, resolving runtime options against configured defaults.
ContextWindowTokens(options map[string]any) int
// Threshold returns the compaction threshold as a fraction of the
// context window (e.g. 0.8 means compact at 80% capacity).
Threshold() float64
// ShouldCompact reports whether a compaction should run and returns the
// trigger reason. An empty trigger means compaction is not needed.
ShouldCompact(req CompactionRequest) (trigger string, should bool)
type CompactionStore interface {
ArchiveForCompaction(context.Context, string, int, string, bool) error
}
type CompactionOptions struct {
@@ -84,9 +79,14 @@ type CompactionResult struct {
type SimpleCompactor struct {
Client ChatClient
Store CompactionStore
Options CompactionOptions
}
func NewSimpleCompactor(client ChatClient, store CompactionStore, opts CompactionOptions) *SimpleCompactor {
return &SimpleCompactor{Client: client, Store: store, Options: opts}
}
func (c *SimpleCompactor) MaybeCompact(ctx context.Context, req CompactionRequest) (CompactionResult, error) {
result := CompactionResult{Messages: req.Messages}
if c == nil {
@@ -106,7 +106,7 @@ func (c *SimpleCompactor) MaybeCompact(ctx context.Context, req CompactionReques
if req.KeepUserTurns != nil {
keepUserTurns = *req.KeepUserTurns
}
prefix, previousSummary, archive, suffix, _, ok := splitCompactionMessages(req.Messages, keepUserTurns)
prefix, previousSummary, archive, suffix, keptUserTurns, ok := splitCompactionMessages(req.Messages, keepUserTurns)
if !ok || len(archive) == 0 {
result.Reason = "nothing to compact"
return result, nil
@@ -127,10 +127,19 @@ func (c *SimpleCompactor) MaybeCompact(ctx context.Context, req CompactionReques
summary = truncateCompactionSummary(strings.TrimSpace(summary))
}
if summary == "" {
// TODO(parthsareen): Investigate models that stream compaction output
// without final content, such as thinking-only summaries.
result.Reason = "summary was empty"
return result, nil
}
if c.Store != nil && req.ChatID != "" {
if err := c.Store.ArchiveForCompaction(ctx, req.ChatID, keptUserTurns, summary, req.ContinueTask); err != nil {
result.Reason = err.Error()
return result, err
}
}
compacted := make([]api.Message, 0, len(prefix)+len(suffix)+2)
compacted = append(compacted, prefix...)
compacted = append(compacted, CompactionSummaryMessages(summary, req.ContinueTask)...)
@@ -150,6 +159,10 @@ func (c *SimpleCompactor) shouldCompact(req CompactionRequest) bool {
if req.Latest.PromptEvalCount > 0 && req.Latest.PromptEvalCount >= threshold {
return true
}
// TODO(parthsareen): If the newest kept user turn contains the oversized
// tool output, compaction can remove older history but still leave the next
// prompt above the safety threshold. Pair this estimate trigger with
// context-aware tool-output paging/range reads so the kept suffix can shrink.
return estimateCompactionRequestTokens(req) >= threshold
}
@@ -157,48 +170,6 @@ func (c *SimpleCompactor) contextWindowTokens(options map[string]any) int {
return ResolveContextWindowTokens(options, c.Options.ContextWindowTokens)
}
// ContextWindowTokens resolves the effective context window from runtime
// options or configured defaults. Satisfies the Compactor interface.
func (c *SimpleCompactor) ContextWindowTokens(options map[string]any) int {
if c == nil {
return 0
}
return c.contextWindowTokens(options)
}
func (c *SimpleCompactor) threshold() float64 {
return ResolveCompactionThreshold(c.Options.Threshold)
}
// Threshold returns the configured compaction threshold fraction. Satisfies
// the Compactor interface.
func (c *SimpleCompactor) Threshold() float64 {
if c == nil {
return 0
}
return c.threshold()
}
// ShouldCompact reports whether compaction is due and the trigger reason.
// Satisfies the Compactor interface.
func (c *SimpleCompactor) ShouldCompact(req CompactionRequest) (string, bool) {
if c == nil {
return "", false
}
if req.Force {
return "force", true
}
if c.shouldCompact(req) {
contextWindow := c.contextWindowTokens(req.Options)
threshold := int(float64(contextWindow) * c.threshold())
if req.Latest.PromptEvalCount > 0 && req.Latest.PromptEvalCount >= threshold {
return "prompt_eval", true
}
return "estimate", true
}
return "", false
}
func (c *SimpleCompactor) keepUserTurns(options map[string]any) int {
contextWindow := c.contextWindowTokens(options)
if contextWindow > 0 && contextWindow < compactOnlySummaryContextTokens {
@@ -210,6 +181,10 @@ func (c *SimpleCompactor) keepUserTurns(options map[string]any) int {
return defaultCompactionKeepUserTurns
}
func (c *SimpleCompactor) threshold() float64 {
return ResolveCompactionThreshold(c.Options.Threshold)
}
func ResolveContextWindowTokens(options map[string]any, configured int) int {
if n := intOption(options, "num_ctx"); n > 0 {
return n
@@ -313,7 +288,8 @@ func compactionSummaryMessageForTask(summary string, continueTask bool) string {
// CompactionSummaryMessages renders a compaction summary as the assistant
// tool-call plus tool-result pair that represents a compacted turn in the
// message history.
// message history. This is the canonical builder used by both the compactor
// and the chat store.
func CompactionSummaryMessages(summary string, continueTask bool) []api.Message {
return []api.Message{
{
@@ -350,10 +326,21 @@ func (c *SimpleCompactor) compactionPromptBodyBudgetTokens(options map[string]an
}
func truncateCompactionSummary(summary string) string {
return Truncate(summary, TruncateConfig{
MaxRunes: maxCompactionSummaryRunes,
Label: "summary",
})
if len(summary) <= maxCompactionSummaryBytes {
return summary
}
limit := maxCompactionSummaryBytes - len(compactionSummaryTruncated)
if limit < 0 {
limit = 0
}
var b strings.Builder
for _, r := range summary {
if b.Len()+len(string(r)) > limit {
break
}
b.WriteRune(r)
}
return strings.TrimSpace(b.String()) + compactionSummaryTruncated
}
func estimateCompactionTokens(text string) int {
@@ -361,7 +348,23 @@ func estimateCompactionTokens(text string) int {
if text == "" {
return 0
}
return ApproximateTokens(len([]rune(text)))
return max(1, (len([]rune(text))+3)/4)
}
// EstimateTokens returns the agent's lightweight token estimate for UI hints.
func EstimateTokens(text string) int {
return estimateCompactionTokens(text)
}
// EstimatePromptTokens returns the agent's lightweight estimate for the prompt
// payload sent to /api/chat.
func EstimatePromptTokens(systemPrompt string, messages []api.Message, tools api.Tools, format string) int {
return estimateCompactionRequestTokens(CompactionRequest{
SystemPrompt: systemPrompt,
Messages: messages,
Tools: tools,
Format: format,
})
}
func estimateMessagesTokens(messages []api.Message) int {
@@ -409,40 +412,6 @@ func estimateCompactionRequestTokens(req CompactionRequest) int {
return total
}
func (s *Session) estimateRunPromptTokens(opts RunOptions, messages []api.Message) int {
return estimateCompactionRequestTokens(CompactionRequest{
SystemPrompt: opts.SystemPrompt,
Messages: messages,
Tools: s.availableTools(),
Format: opts.Format,
Options: opts.Options,
})
}
func (s *Session) checkPreflightPromptBudget(opts RunOptions, messages []api.Message) error {
contextWindow := s.contextWindowTokens(opts)
if contextWindow <= 0 {
return nil
}
estimated := s.estimateRunPromptTokens(opts, messages)
if estimated < contextWindow {
return nil
}
return fmt.Errorf("prompt is too large for the current context (~%d/%d tokens). Reduce the system prompt or message history, compact the conversation, or use a model with a larger context", estimated, contextWindow)
}
func (s *Session) checkPostCompactionPromptBudget(opts RunOptions, messages []api.Message) error {
contextWindow := s.contextWindowTokens(opts)
if contextWindow <= 0 {
return nil
}
estimated := s.estimateRunPromptTokens(opts, messages)
if estimated < contextWindow {
return nil
}
return fmt.Errorf("history is still too large after compaction (~%d/%d tokens). Start a fresh request, reduce the system prompt or history, or use a model with a larger context", estimated, contextWindow)
}
func sanitizeMessagesForEstimate(messages []api.Message) []api.Message {
requestMessages := sanitizeMessagesForRequest(messages)
for i := range requestMessages {
+81 -38
View File
@@ -9,6 +9,21 @@ import (
"github.com/ollama/ollama/api"
)
type compactionStore struct {
chatID string
keepUserTurns int
summary string
continueTask bool
}
func (s *compactionStore) ArchiveForCompaction(_ context.Context, chatID string, keepUserTurns int, summary string, continueTask bool) error {
s.chatID = chatID
s.keepUserTurns = keepUserTurns
s.summary = summary
s.continueTask = continueTask
return nil
}
type scriptedCompactionClient struct {
responses [][]api.ChatResponse
errs []error
@@ -56,11 +71,12 @@ func TestSimpleCompactorSummarizesOldMessages(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 16000,
KeepUserTurns: 2,
Threshold: 0.5,
}}
})
messages := []api.Message{
{Role: "system", Content: "stay pinned"},
@@ -97,6 +113,9 @@ func TestSimpleCompactorSummarizesOldMessages(t *testing.T) {
if compacted[3].Content != "recent one" || compacted[5].Content != "recent two" {
t.Fatalf("recent turns were not kept: %#v", compacted)
}
if store.chatID != "chat-1" || store.keepUserTurns != 2 || store.summary != "summary" || store.continueTask {
t.Fatalf("archive call = %#v", store)
}
if len(client.requests) != 1 {
t.Fatalf("summary requests = %d, want 1", len(client.requests))
}
@@ -111,11 +130,12 @@ func TestSimpleCompactorKeepsOnlySummaryForSmallContext(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "small context summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: compactOnlySummaryContextTokens - 1,
KeepUserTurns: 3,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -135,6 +155,9 @@ func TestSimpleCompactorKeepsOnlySummaryForSmallContext(t *testing.T) {
if !result.Compacted {
t.Fatal("expected compaction")
}
if store.keepUserTurns != 0 {
t.Fatalf("keepUserTurns = %d, want 0 for small context", store.keepUserTurns)
}
if len(result.Messages) != 3 {
t.Fatalf("messages = %#v, want system plus compaction summary pair", result.Messages)
}
@@ -153,11 +176,12 @@ func TestSimpleCompactorAddsContinueTaskInstructionOnlyToToolResult(t *testing.T
{Message: api.Message{Role: "assistant", Content: "summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -183,20 +207,24 @@ func TestSimpleCompactorAddsContinueTaskInstructionOnlyToToolResult(t *testing.T
if got := CompactionSummaryText(content); got != "summary" {
t.Fatalf("visible summary text = %q", got)
}
if !store.continueTask || store.summary != "summary" {
t.Fatalf("archive call = %#v", store)
}
}
func TestSimpleCompactorTruncatesOversizedSummary(t *testing.T) {
longSummary := strings.Repeat("x", maxCompactionSummaryRunes+1024)
longSummary := strings.Repeat("x", maxCompactionSummaryBytes+1024)
client := &fakeClient{
responses: [][]api.ChatResponse{{
{Message: api.Message{Role: "assistant", Content: longSummary}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -214,13 +242,16 @@ func TestSimpleCompactorTruncatesOversizedSummary(t *testing.T) {
if !result.Compacted {
t.Fatal("expected compaction")
}
if runeCount := len([]rune(result.Summary)); runeCount > maxCompactionSummaryRunes+200 {
t.Fatalf("summary runes = %d, want <= %d (plus marker)", runeCount, maxCompactionSummaryRunes)
if len(result.Summary) > maxCompactionSummaryBytes {
t.Fatalf("summary bytes = %d, want <= %d", len(result.Summary), maxCompactionSummaryBytes)
}
if !strings.Contains(result.Summary, "[summary truncated:") {
t.Fatalf("summary missing truncation marker: %q", result.Summary)
if !strings.HasSuffix(result.Summary, compactionSummaryTruncated) {
t.Fatalf("summary missing truncation marker")
}
if !strings.Contains(result.Messages[1].Content, "[summary truncated:") {
if store.summary != result.Summary {
t.Fatalf("stored summary mismatch")
}
if !strings.Contains(result.Messages[1].Content, compactionSummaryTruncated) {
t.Fatalf("compacted message missing truncation marker: %#v", result.Messages)
}
}
@@ -232,11 +263,11 @@ func TestSimpleCompactorRetriesEmptySummaryWithThinkFalse(t *testing.T) {
{{Message: api.Message{Role: "assistant", Content: "fallback summary"}}},
},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
Model: "model",
@@ -275,11 +306,11 @@ func TestSimpleCompactorIgnoresUnsupportedThinkFalseFallback(t *testing.T) {
api.StatusError{StatusCode: http.StatusBadRequest, ErrorMessage: "model does not support thinking"},
},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
Model: "model",
@@ -317,11 +348,11 @@ func TestSimpleCompactorFallsBackToUnsetThinkWhenThinkFalseUnsupported(t *testin
nil,
},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
thinkHigh := &api.ThinkValue{Value: "high"}
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
@@ -360,11 +391,12 @@ func TestSimpleCompactorKeepsFewerTurnsForShortChats(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "short summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 16000,
KeepUserTurns: 3,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -382,6 +414,9 @@ func TestSimpleCompactorKeepsFewerTurnsForShortChats(t *testing.T) {
if !result.Compacted {
t.Fatal("expected compaction")
}
if store.keepUserTurns != 1 {
t.Fatalf("kept user turns = %d, want 1", store.keepUserTurns)
}
if len(result.Messages) != 3 {
t.Fatalf("messages = %#v, want compaction tool pair plus latest request", result.Messages)
}
@@ -397,11 +432,12 @@ func TestSimpleCompactorCanArchiveWholeShortChat(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "whole summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 3,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -418,6 +454,9 @@ func TestSimpleCompactorCanArchiveWholeShortChat(t *testing.T) {
if !result.Compacted {
t.Fatal("expected compaction")
}
if store.keepUserTurns != 0 {
t.Fatalf("kept user turns = %d, want 0", store.keepUserTurns)
}
if len(result.Messages) != 2 {
t.Fatalf("messages = %#v, want only compaction tool pair", result.Messages)
}
@@ -426,10 +465,10 @@ func TestSimpleCompactorCanArchiveWholeShortChat(t *testing.T) {
func TestSimpleCompactorSkipsBelowThreshold(t *testing.T) {
client := &fakeClient{}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
Threshold: 0.8,
}}
})
messages := []api.Message{
{Role: "user", Content: "one"},
@@ -466,11 +505,11 @@ func TestSimpleCompactorUsesEstimatedMessagesWhenPromptEvalMissing(t *testing.T)
{Message: api.Message{Role: "assistant", Content: "estimated summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.8,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
Model: "model",
@@ -499,10 +538,10 @@ func TestSimpleCompactorUsesEstimatedMessagesWhenPromptEvalMissing(t *testing.T)
}
func TestSimpleCompactorEstimateIncludesRequestPreamble(t *testing.T) {
compactor := &SimpleCompactor{Client: nil, Options: CompactionOptions{
compactor := NewSimpleCompactor(nil, nil, CompactionOptions{
ContextWindowTokens: 100,
Threshold: 0.8,
}}
})
if !compactor.shouldCompact(CompactionRequest{
SystemPrompt: strings.Repeat("system ", 360),
@@ -635,11 +674,11 @@ func TestSimpleCompactorForceCompactsWithoutPromptEvalCount(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "forced summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 100,
KeepUserTurns: 1,
Threshold: 0.8,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
Model: "model",
@@ -667,10 +706,11 @@ func TestSimpleCompactorDefaultsToKeepingThreeUserTurns(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
store := &compactionStore{}
compactor := NewSimpleCompactor(client, store, CompactionOptions{
ContextWindowTokens: 16000,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
ChatID: "chat-1",
@@ -692,6 +732,9 @@ func TestSimpleCompactorDefaultsToKeepingThreeUserTurns(t *testing.T) {
if !result.Compacted {
t.Fatal("expected compaction")
}
if store.keepUserTurns != 3 {
t.Fatalf("keepUserTurns = %d, want 3", store.keepUserTurns)
}
assertCompactionSummaryPair(t, result.Messages[:2])
if got := result.Messages[2].Content; got != "one" {
t.Fatalf("first kept turn = %q, want one", got)
@@ -704,11 +747,11 @@ func TestSimpleCompactorCarriesPreviousSummary(t *testing.T) {
{Message: api.Message{Role: "assistant", Content: "new summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 16000,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
result, err := compactor.MaybeCompact(context.Background(), CompactionRequest{
Model: "model",
@@ -737,11 +780,11 @@ func TestSimpleCompactorCarriesPreviousToolSummaryAndPlacesNewSummaryBeforeKeptS
{Message: api.Message{Role: "assistant", Content: "new summary"}},
}},
}
compactor := &SimpleCompactor{Client: client, Options: CompactionOptions{
compactor := NewSimpleCompactor(client, nil, CompactionOptions{
ContextWindowTokens: 16000,
KeepUserTurns: 1,
Threshold: 0.5,
}}
})
messages := []api.Message{
{Role: "user", Content: "kept before old summary"},
+46 -127
View File
@@ -2,7 +2,7 @@ package agent
import (
"context"
"errors"
"time"
"github.com/ollama/ollama/api"
)
@@ -10,79 +10,66 @@ import (
type EventType string
const (
EventMessageStarted EventType = "message_started"
EventMessageDelta EventType = "message_delta"
EventThinkingDelta EventType = "thinking_delta"
EventToolCallDetected EventType = "tool_call_detected"
EventToolStarted EventType = "tool_started"
EventToolFinished EventType = "tool_finished"
EventToolsUnavailable EventType = "tools_unavailable"
EventCompactionStarted EventType = "compaction_started"
EventCompactionProgress EventType = "compaction_progress"
EventCompacted EventType = "compacted"
EventCompactionSkipped EventType = "compaction_skipped"
EventLoopStep EventType = "loop_step"
EventRequestBuilt EventType = "request_built"
EventModelStreamDone EventType = "model_stream_done"
EventRunFinished EventType = "run_finished"
EventError EventType = "error"
)
// ToolStatus is the typed lifecycle state for a tool call, carried on
// Event.ToolStatus for tool events.
type ToolStatus string
const (
ToolStatusRunning ToolStatus = "running"
ToolStatusDone ToolStatus = "done"
ToolStatusFailed ToolStatus = "failed"
ToolStatusDenied ToolStatus = "denied"
ToolStatusDisabled ToolStatus = "disabled"
ToolStatusSkipped ToolStatus = "skipped"
)
// RunStatus is the typed terminal outcome of a run, carried on Event.Status for
// run_finished events.
type RunStatus string
const (
RunStatusDone RunStatus = "done"
RunStatusDenied RunStatus = "denied"
RunStatusCanceled RunStatus = "canceled"
)
// CompactionTrigger is the typed reason a compaction ran or was attempted,
// carried on Event.CompactionTrigger for compaction events.
type CompactionTrigger string
const (
CompactionTriggerForce CompactionTrigger = "force"
CompactionTriggerPromptEval CompactionTrigger = "prompt_eval"
CompactionTriggerEstimate CompactionTrigger = "estimate"
CompactionTriggerToolOutput CompactionTrigger = "tool_output"
CompactionTriggerError CompactionTrigger = "error"
CompactionTriggerDue CompactionTrigger = "due"
)
type Event struct {
Type EventType `json:"type"`
RunID string `json:"runId,omitempty"`
ChatID string `json:"chatId,omitempty"`
Model string `json:"model,omitempty"`
Status RunStatus `json:"status,omitempty"`
ToolStatus ToolStatus `json:"toolStatus,omitempty"`
CompactionTrigger CompactionTrigger `json:"compactionTrigger,omitempty"`
ToolCallID string `json:"toolCallId,omitempty"`
ToolName string `json:"toolName,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Content string `json:"content,omitempty"`
Thinking string `json:"thinking,omitempty"`
ToolCalls []api.ToolCall `json:"toolCalls,omitempty"`
Messages []api.Message `json:"messages,omitempty"`
Args map[string]any `json:"args,omitempty"`
Tokens int `json:"tokens,omitempty"`
Error string `json:"error,omitempty"`
Type EventType `json:"type"`
RunID string `json:"runId,omitempty"`
ChatID string `json:"chatId,omitempty"`
MessageID string `json:"messageId,omitempty"`
Model string `json:"model,omitempty"`
Status string `json:"status,omitempty"`
ToolCallID string `json:"toolCallId,omitempty"`
ToolName string `json:"toolName,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Content string `json:"content,omitempty"`
Thinking string `json:"thinking,omitempty"`
ToolCalls []api.ToolCall `json:"toolCalls,omitempty"`
Messages []api.Message `json:"messages,omitempty"`
Args map[string]any `json:"args,omitempty"`
Tokens int `json:"tokens,omitempty"`
PromptTokens int `json:"promptTokens,omitempty"`
Error string `json:"error,omitempty"`
StartedAt time.Time `json:"startedAt,omitempty"`
FinishedAt time.Time `json:"finishedAt,omitempty"`
Response *api.ChatResponse `json:"-"`
}
type EventSink interface {
Emit(Event) error
}
type MultiEventSink []EventSink
func (s MultiEventSink) Emit(event Event) error {
var firstErr error
for _, sink := range s {
if sink == nil {
continue
}
if err := sink.Emit(event); err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}
type EventSinkFunc func(Event) error
func (fn EventSinkFunc) Emit(event Event) error {
@@ -92,83 +79,15 @@ func (fn EventSinkFunc) Emit(event Event) error {
return fn(event)
}
// eventMetadata carries the run identification fields shared by all events.
type eventMetadata struct {
runID string
chatID string
model string
}
func newEventMetadata(runID string, opts RunOptions) eventMetadata {
return eventMetadata{runID: runID, chatID: opts.ChatID, model: opts.Model}
}
func newMessageDelta(m eventMetadata, content string) Event {
return Event{Type: EventMessageDelta, RunID: m.runID, ChatID: m.chatID, Model: m.model, Content: content}
}
func newThinkingDelta(m eventMetadata, thinking string) Event {
return Event{Type: EventThinkingDelta, RunID: m.runID, ChatID: m.chatID, Model: m.model, Thinking: thinking}
}
func newToolCallDetected(m eventMetadata, calls []api.ToolCall) Event {
return Event{Type: EventToolCallDetected, RunID: m.runID, ChatID: m.chatID, Model: m.model, ToolCalls: calls}
}
func newToolStarted(m eventMetadata, callID, toolName, workingDir string, args map[string]any) Event {
return Event{Type: EventToolStarted, RunID: m.runID, ChatID: m.chatID, Model: m.model, ToolStatus: ToolStatusRunning, ToolCallID: callID, ToolName: toolName, WorkingDir: workingDir, Args: args}
}
func newToolFinished(m eventMetadata, status ToolStatus, callID, toolName, workingDir string, args map[string]any, content, errMsg string) Event {
ev := Event{Type: EventToolFinished, RunID: m.runID, ChatID: m.chatID, Model: m.model, ToolStatus: status, ToolCallID: callID, ToolName: toolName, WorkingDir: workingDir, Args: args, Content: content}
if errMsg != "" {
ev.Error = errMsg
}
return ev
}
func newRunFinished(m eventMetadata, status RunStatus) Event {
return Event{Type: EventRunFinished, RunID: m.runID, ChatID: m.chatID, Model: m.model, Status: status}
}
func newErrorEvent(m eventMetadata, errMsg string) Event {
return Event{Type: EventError, RunID: m.runID, ChatID: m.chatID, Model: m.model, Error: errMsg}
}
func newCompactionProgress(m eventMetadata, tokens int) Event {
return Event{Type: EventCompactionProgress, RunID: m.runID, ChatID: m.chatID, Model: m.model, Tokens: tokens}
}
func newCompactionStarted(m eventMetadata, trigger CompactionTrigger) Event {
return Event{Type: EventCompactionStarted, RunID: m.runID, ChatID: m.chatID, Model: m.model, CompactionTrigger: trigger}
}
func newCompactionSkipped(m eventMetadata, trigger CompactionTrigger, content string) Event {
return Event{Type: EventCompactionSkipped, RunID: m.runID, ChatID: m.chatID, Model: m.model, CompactionTrigger: trigger, Content: content}
}
func newCompacted(m eventMetadata, messages []api.Message, trigger CompactionTrigger, content string) Event {
return Event{Type: EventCompacted, RunID: m.runID, ChatID: m.chatID, Model: m.model, CompactionTrigger: trigger, Content: content, Messages: messages}
}
func (s *Session) emit(event Event) error {
if s == nil {
func emit(sink EventSink, event Event) error {
if sink == nil {
return nil
}
var errs []error
for _, sink := range s.EventSinks {
if sink == nil {
continue
}
if err := sink.Emit(event); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
return sink.Emit(event)
}
func (s *Session) emitIgnoringCanceled(ctx context.Context, event Event) error {
err := s.emit(event)
func emitIgnoringCanceled(ctx context.Context, sink EventSink, event Event) error {
err := emit(sink, event)
if err != nil && ctx != nil && ctx.Err() != nil {
//nolint:nilerr // Event sinks may close during cancellation; cancellation is not a user-facing emit failure.
return nil
+92
View File
@@ -0,0 +1,92 @@
package agent
import "sync"
type ToolMode int
const (
ToolModeReview ToolMode = iota
ToolModeFullAccess
ToolModeDisabled
)
type RunPolicy struct {
ToolMode ToolMode
ApprovalPolicy ApprovalPolicy
// MaxToolRounds limits consecutive model/tool cycles.
// Zero uses the default guard; negative disables the guard for tests or
// special callers.
MaxToolRounds int
}
func (p RunPolicy) UsesTools() bool {
switch p.ToolMode {
case ToolModeReview, ToolModeFullAccess:
return true
default:
return false
}
}
func (p RunPolicy) Tools(registry *Registry) *Registry {
if !p.UsesTools() {
return nil
}
return registry
}
func (p RunPolicy) ApprovalHandler(prompter ApprovalPrompter) ApprovalHandler {
if p.ToolMode == ToolModeFullAccess {
return AutoAllowApproval{}
}
policy := p.ApprovalPolicy
if policy == nil {
policy = DefaultApprovalPolicy{}
}
return NewApprovalManager(ApprovalManagerOptions{
Policy: policy,
Prompter: prompter,
})
}
func (p RunPolicy) ReviewApprovalHandler(prompter ApprovalPrompter) ApprovalHandler {
policy := p.ApprovalPolicy
if policy == nil {
policy = DefaultApprovalPolicy{}
}
return NewApprovalManager(ApprovalManagerOptions{
Policy: policy,
Prompter: prompter,
})
}
type RunPolicyState struct {
mu sync.Mutex
policy RunPolicy
}
func NewRunPolicyState(policy RunPolicy) *RunPolicyState {
return &RunPolicyState{policy: policy}
}
func (s *RunPolicyState) Policy() RunPolicy {
if s == nil {
return RunPolicy{}
}
s.mu.Lock()
defer s.mu.Unlock()
return s.policy
}
func (s *RunPolicyState) ToolMode() ToolMode {
return s.Policy().ToolMode
}
func (s *RunPolicyState) SetToolMode(mode ToolMode) {
if s == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.policy.ToolMode = mode
}
+12 -13
View File
@@ -28,27 +28,29 @@ type ApprovalRequired interface {
RequiresApproval(map[string]any) bool
}
// ScopedTool is implemented by tools that need per-invocation approval
// scoping beyond the tool name (e.g. shell commands scoped to the exact
// command string). Tools that don't implement this are scoped by name only.
type ScopedTool interface {
ApprovalScope(args map[string]any) string
}
type Registry struct {
tools map[string]Tool
}
func NewRegistry() *Registry {
return &Registry{tools: make(map[string]Tool)}
}
func (r *Registry) Register(tool Tool) {
if r == nil || tool == nil {
return
}
if r.tools == nil {
r.tools = make(map[string]Tool)
}
r.tools[tool.Name()] = tool
}
func (r *Registry) Has(name string) bool {
if r == nil {
return false
}
_, ok := r.tools[name]
return ok
}
func (r *Registry) Get(name string) (Tool, bool) {
if r == nil {
return nil, false
@@ -70,9 +72,6 @@ func (r *Registry) Names() []string {
}
func (r *Registry) Tools() api.Tools {
if r == nil {
return nil
}
names := r.Names()
apiTools := make(api.Tools, 0, len(names))
for _, name := range names {
+64
View File
@@ -0,0 +1,64 @@
package agent
import (
"encoding/json"
"strings"
"github.com/ollama/ollama/api"
)
// ChatRequestPreview is the request body plus the estimated prompt tokens for it.
type ChatRequestPreview struct {
Request api.ChatRequest
PromptTokens int
}
// BuildChatRequestPreview builds the chat request shape used for a run and its estimated prompt tokens.
func BuildChatRequestPreview(opts RunOptions, messages []api.Message, tools api.Tools) ChatRequestPreview {
return ChatRequestPreview{
Request: buildChatRequest(opts, messages, tools),
PromptTokens: EstimateChatRequestPromptTokens(opts, messages, tools),
}
}
// EstimateChatRequestPromptTokens estimates the prompt tokens for a chat request before sending it.
func EstimateChatRequestPromptTokens(opts RunOptions, messages []api.Message, tools api.Tools) int {
return estimateCompactionRequestTokens(CompactionRequest{
SystemPrompt: opts.SystemPrompt,
Messages: sanitizeMessagesForRequest(messages),
Tools: tools,
Format: opts.Format,
Options: opts.Options,
})
}
func buildChatRequest(opts RunOptions, messages []api.Message, tools api.Tools) api.ChatRequest {
requestMessages := sanitizeMessagesForRequest(messages)
if strings.TrimSpace(opts.SystemPrompt) != "" {
withSystem := make([]api.Message, 0, len(requestMessages)+1)
withSystem = append(withSystem, api.Message{Role: "system", Content: opts.SystemPrompt})
requestMessages = append(withSystem, requestMessages...)
}
req := api.ChatRequest{
Model: opts.Model,
Messages: requestMessages,
Format: json.RawMessage(chatRequestFormat(opts.Format)),
Options: opts.Options,
Think: opts.Think,
}
if opts.KeepAlive != nil {
req.KeepAlive = opts.KeepAlive
}
if len(tools) > 0 {
req.Tools = tools
}
return req
}
func chatRequestFormat(format string) string {
if format == "json" {
return `"` + format + `"`
}
return format
}
+46
View File
@@ -0,0 +1,46 @@
package agent
import (
"testing"
"github.com/ollama/ollama/api"
)
func TestBuildChatRequestPreviewBuildsModelRequest(t *testing.T) {
tools := api.Tools{{
Type: "function",
Function: api.ToolFunction{
Name: "read",
Description: "read a file",
Parameters: api.ToolFunctionParameters{Type: "object"},
},
}}
preview := BuildChatRequestPreview(RunOptions{
Model: "llama3.2",
SystemPrompt: "You are Ollama.",
Format: "json",
Options: map[string]any{"temperature": 0.1},
}, []api.Message{{Role: "user", Content: "hello"}}, tools)
if preview.Request.Model != "llama3.2" {
t.Fatalf("model = %q, want llama3.2", preview.Request.Model)
}
if got := string(preview.Request.Format); got != `"json"` {
t.Fatalf("format = %q, want quoted json", got)
}
if len(preview.Request.Messages) != 2 {
t.Fatalf("messages = %d, want 2", len(preview.Request.Messages))
}
if preview.Request.Messages[0].Role != "system" || preview.Request.Messages[0].Content != "You are Ollama." {
t.Fatalf("system message = %#v", preview.Request.Messages[0])
}
if preview.Request.Messages[1].Role != "user" || preview.Request.Messages[1].Content != "hello" {
t.Fatalf("user message = %#v", preview.Request.Messages[1])
}
if len(preview.Request.Tools) != 1 {
t.Fatalf("tools = %d, want 1", len(preview.Request.Tools))
}
if preview.PromptTokens <= 0 {
t.Fatalf("prompt tokens = %d, want positive", preview.PromptTokens)
}
}
+552 -611
View File
File diff suppressed because it is too large. Load diff
+315 -667
View File
File diff suppressed because it is too large. Load diff
-57
View File
@@ -1,57 +0,0 @@
package agent
import (
"context"
"strings"
"github.com/google/uuid"
"github.com/ollama/ollama/api"
)
// activateSkill loads opts.SkillName from the catalog and injects a synthetic
// assistant tool call plus tool result before the first model request, so the
// transcript looks like a real skill tool invocation. It emits the same
// tool_call_detected -> tool_started -> tool_finished lifecycle the model path
// uses, and returns the messages to prepend. A blank SkillName is a no-op.
func (s *Session) activateSkill(ctx context.Context, runID string, opts RunOptions) ([]api.Message, error) {
name := strings.TrimSpace(opts.SkillName)
if name == "" {
return nil, nil
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
skill, err := s.Skills.Load(name)
if err != nil {
return nil, err
}
args := api.NewToolCallFunctionArguments()
args.Set("name", skill.Name)
call := api.ToolCall{
ID: "call_skill_" + uuid.NewString(),
Function: api.ToolCallFunction{Name: "skill", Arguments: args},
}
result := api.Message{
Role: "tool",
ToolName: "skill",
ToolCallID: call.ID,
Content: skill.Content(),
}
meta := newEventMetadata(runID, opts)
if err := s.emit(newToolCallDetected(meta, []api.ToolCall{call})); err != nil {
return nil, err
}
if err := s.emit(newToolStarted(meta, call.ID, "skill", s.currentWorkingDir(), args.ToMap())); err != nil {
return nil, err
}
if err := s.emitIgnoringCanceled(ctx, newToolFinished(meta, ToolStatusDone, call.ID, "skill", s.currentWorkingDir(), args.ToMap(), result.Content, "")); err != nil {
return nil, err
}
return []api.Message{
{Role: "assistant", ToolCalls: []api.ToolCall{call}},
result,
}, nil
}
-74
View File
@@ -1,74 +0,0 @@
package agent
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/ollama/ollama/api"
)
type skillTestClient struct{ requests []*api.ChatRequest }
func (c *skillTestClient) Chat(_ context.Context, req *api.ChatRequest, fn api.ChatResponseFunc) error {
c.requests = append(c.requests, req)
return fn(api.ChatResponse{Message: api.Message{Role: "assistant", Content: "Done."}})
}
func testSkillCatalog(t *testing.T) *SkillCatalog {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "release-notes")
if err := os.Mkdir(path, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "SKILL.md"), []byte("---\nname: release-notes\ndescription: Draft release notes.\n---\nUse concise bullets."), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := DiscoverSkills(dir)
if err != nil {
t.Fatal(err)
}
return catalog
}
func TestSessionSkillActivationPreservesCallAndResultOrder(t *testing.T) {
catalog := testSkillCatalog(t)
client := &skillTestClient{}
events := &recordingEventSink{}
result, err := (&Session{Client: client, Skills: catalog, EventSinks: []EventSink{events}}).Run(context.Background(), RunOptions{
Model: "test",
NewMessages: []api.Message{{Role: "user", Content: "draft release notes"}},
SkillName: "release-notes",
})
if err != nil {
t.Fatal(err)
}
if len(result.Messages) != 4 {
t.Fatalf("transcript = %#v", result.Messages)
}
call, toolTranscript := result.Messages[1], result.Messages[2]
if call.Role != "assistant" || len(call.ToolCalls) != 1 || call.ToolCalls[0].Function.Name != "skill" || !strings.HasPrefix(call.ToolCalls[0].ID, "call_skill_") {
t.Fatalf("call message = %#v", call)
}
if toolTranscript.Role != "tool" || toolTranscript.ToolName != "skill" || toolTranscript.ToolCallID != call.ToolCalls[0].ID || !strings.Contains(toolTranscript.Content, "Use concise bullets.") {
t.Fatalf("tool result = %#v", toolTranscript)
}
if len(client.requests) != 1 || len(client.requests[0].Messages) != 3 || client.requests[0].Messages[2].ToolCallID != call.ToolCalls[0].ID {
t.Fatalf("model request did not preserve transcript: %#v", client.requests)
}
var skillEvents []EventType
for _, event := range events.events {
if event.ToolName == "skill" || event.Type == EventToolCallDetected {
skillEvents = append(skillEvents, event.Type)
}
}
if len(skillEvents) < 3 {
t.Fatalf("skill event order = %#v, want tool_call_detected,tool_started,tool_finished", skillEvents)
}
if got, want := strings.Join([]string{string(skillEvents[0]), string(skillEvents[1]), string(skillEvents[2])}, ","), "tool_call_detected,tool_started,tool_finished"; got != want {
t.Fatalf("skill event order = %#v, want %s", skillEvents, want)
}
}
-813
View File
@@ -1,813 +0,0 @@
package agent
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/yaml.v3"
)
const (
// SkillsDirEnv overrides the user-level Ollama-owned skills directory. The
// cross-client .agents/skills/ convention and project-level .ollama/skills/
// are also scanned (see LoadDefaultSkills); on a name collision, Ollama-owned
// directories take precedence over .agents/skills/, and project-level takes
// precedence over user-level.
SkillsDirEnv = "OLLAMA_SKILLS"
skillFilename = "SKILL.md"
maxSkillBytes = 1 << 20
bundledSkillCreatorName = "skill-creator"
bundledSkillCreatorContent = `---
name: skill-creator
description: Create or improve reusable skills. Use when the user wants a reusable skill, asks how to author SKILL.md, or needs help installing a skill.
---
# Create a skill
Create a focused, reusable instruction package. Treat a skill as guidance for the model, not as a way to gain new permissions or bypass safety controls.
## Choose the location
Create user skills beside this one. The skill directory shown in the loaded skill context is this skill's location; its parent is the user skill root. This bundled skill normally lives at ~/.ollama/skills/skill-creator, so new user skills normally go at ~/.ollama/skills/<skill-name>/SKILL.md.
Use a project-local skill directory only when the user asks to keep the skill with that project. Do not overwrite an existing skill without the user's approval. New and changed skills are discovered when the agent starts, so tell the user to begin a new agent session afterward.
## Follow the required shape
Use the directory name as the skill name. Use lowercase letters, numbers, and single hyphens only. Keep the name short and no longer than 64 characters.
Every skill needs a SKILL.md with YAML frontmatter followed by Markdown instructions:
~~~md
---
name: release-notes
description: Draft concise release notes from completed changes. Use when the user asks for a changelog, release notes, or GitHub release copy.
---
# Draft release notes
Write the workflow here.
~~~
Require a non-empty description that says both what the skill does and when to use it. Keep the body procedural and concise. Put detailed schemas, long examples, and variant-specific guidance in references/ only when the skill needs them.
Use scripts/ for repeatable or fragile operations that benefit from deterministic execution. Use assets/ for files that belong in generated output. Do not add README files, changelogs, or setup notes that do not help the model perform the task.
## Create safely
1. Identify the repeated task, expected inputs, and useful output.
2. Choose the smallest name and description that reliably trigger the skill.
3. Create the folder and SKILL.md; add resources only when they remove real repeated work.
4. Re-read the completed file and verify its frontmatter, directory-name match, and relative resource paths.
5. Tell the user where it was created and that a new agent session will discover it.
Skills provide instructions only. They do not grant filesystem, network, shell, or approval privileges, and they do not make a tool available. Use only the tools that are actually available, follow their normal approval rules, and ask before actions that need user authorization.
`
)
var skillName = regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
// SkillsDir returns the canonical runtime-owned skill directory.
func SkillsDir() (string, error) {
if path := strings.TrimSpace(os.Getenv(SkillsDirEnv)); path != "" {
return filepath.Abs(path)
}
if xdg := strings.TrimSpace(os.Getenv("XDG_CONFIG_HOME")); xdg != "" {
return filepath.Join(xdg, "ollama", "skills"), nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".ollama", "skills"), nil
}
// Skill is a validated, loadable instruction set. It never grants tool
// permissions; it is supplied to the model as ordinary tool-result content.
type Skill struct {
Name string
Description string
Instructions string
Path string
}
func (s Skill) Content() string {
var b strings.Builder
fmt.Fprintf(&b, "<skill name=%q>\n%s\n", s.Name, strings.TrimSpace(s.Instructions))
if s.Path != "" {
dir := filepath.Dir(s.Path)
fmt.Fprintf(&b, "Skill directory: %s\n", dir)
b.WriteString("Relative paths in this skill are relative to the skill directory.\n")
}
if resources := s.resources(); len(resources) > 0 {
b.WriteString("<skill_resources>\n")
for _, r := range resources {
fmt.Fprintf(&b, " <file>%s</file>\n", r)
}
b.WriteString("</skill_resources>\n")
}
b.WriteString("</skill>")
return b.String()
}
// resources lists bundled files one level deep under scripts/, references/,
// and assets/ without reading them, so the model can load them on demand.
func (s Skill) resources() []string {
if s.Path == "" {
return nil
}
dir := filepath.Dir(s.Path)
var resources []string
for _, sub := range []string{"scripts", "references", "assets"} {
entries, err := os.ReadDir(filepath.Join(dir, sub))
if err != nil {
continue
}
for _, e := range entries {
if e.IsDir() {
continue
}
resources = append(resources, sub+"/"+e.Name())
}
}
sort.Strings(resources)
return resources
}
// SkillCatalog contains valid skills and diagnostics for ignored invalid
// entries, so one malformed skill cannot hide the rest.
type SkillCatalog struct {
dir string
skills map[string]Skill
diagnostics []error
}
func DiscoverSkills(dir string) (*SkillCatalog, error) {
dir, err := filepath.Abs(strings.TrimSpace(dir))
if err != nil {
return nil, err
}
catalog := &SkillCatalog{dir: dir, skills: make(map[string]Skill)}
entries, err := os.ReadDir(dir)
if errors.Is(err, fs.ErrNotExist) {
return catalog, nil
}
if err != nil {
return nil, fmt.Errorf("read skills directory: %w", err)
}
for _, entry := range entries {
name := entry.Name()
// Follow symlinks so users can point at shared skill repositories.
// The link name (not the target) is the canonical skill name.
info, err := os.Stat(filepath.Join(dir, name))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
catalog.diagnostics = append(catalog.diagnostics, fmt.Errorf("skill %q: %w", name, err))
continue
}
if !info.IsDir() {
continue
}
if !skillName.MatchString(name) {
catalog.diagnostics = append(catalog.diagnostics, fmt.Errorf("invalid skill directory %q", name))
continue
}
skill, err := parseSkill(filepath.Join(dir, name, skillFilename), name)
if errors.Is(err, fs.ErrNotExist) {
continue
}
if err != nil {
catalog.diagnostics = append(catalog.diagnostics, err)
continue
}
catalog.skills[skill.Name] = skill
}
return catalog, nil
}
// LoadDefaultSkills discovers skills from the spec's scopes, merged with
// deterministic precedence. Roots are scanned lowest-precedence first so later
// roots override earlier ones on name collisions (recording a diagnostic):
//
// 1. ~/.agents/skills/ (user, cross-client)
// 2. user Ollama skills dir (user, Ollama-owned; SkillsDir)
// 3. <project>/.agents/skills/ (project, cross-client)
// 4. <project>/.ollama/skills/ (project, Ollama-owned)
//
// Project-level overrides user-level, and within a scope Ollama-owned
// directories override .agents/skills/. projectDir is the agent's working
// directory at startup (discovery is a session-start snapshot per the spec).
func LoadDefaultSkills(projectDir string) (*SkillCatalog, error) {
roots, err := defaultSkillRoots(projectDir)
if err != nil {
return nil, err
}
catalog := &SkillCatalog{skills: make(map[string]Skill)}
bundled, err := bundledSkillCreator()
if err != nil {
return nil, err
}
catalog.skills[bundled.Name] = bundled
if err := installBundledSkillCreator(); err != nil {
catalog.diagnostics = append(catalog.diagnostics, err)
}
for _, root := range roots {
sub, err := DiscoverSkills(root.path)
if err != nil {
catalog.diagnostics = append(catalog.diagnostics, fmt.Errorf("discover skills in %s: %w", root.path, err))
continue
}
catalog.diagnostics = append(catalog.diagnostics, sub.diagnostics...)
for _, skill := range sub.skills {
// Name collisions across roots are expected precedence resolution,
// not errors: later (higher-precedence) roots legitimately override
// earlier ones. The skill is still loaded; no diagnostic needed.
catalog.skills[skill.Name] = skill
}
}
return catalog, nil
}
func bundledSkillCreator() (Skill, error) {
skill, err := parseSkillContent("", bundledSkillCreatorName, bundledSkillCreatorContent)
if err != nil {
return Skill{}, fmt.Errorf("load bundled %s skill: %w", bundledSkillCreatorName, err)
}
return skill, nil
}
func installBundledSkillCreator() error {
dir, err := SkillsDir()
if err != nil {
return fmt.Errorf("resolve bundled skill directory: %w", err)
}
path := filepath.Join(dir, bundledSkillCreatorName, skillFilename)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create bundled skill directory: %w", err)
}
contents, err := os.ReadFile(path)
if err == nil && string(contents) == bundledSkillCreatorContent {
return nil
}
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("read bundled skill: %w", err)
}
if err := os.WriteFile(path, []byte(bundledSkillCreatorContent), 0o644); err != nil {
return fmt.Errorf("write bundled skill: %w", err)
}
return nil
}
type skillRoot struct {
path string
}
// SkillImportResult describes one import attempt. Failed skills do not prevent
// other valid skills in the same source root from being imported.
type SkillImportResult struct {
Source string
SourceDir string
Destination string
Imported []string
Existing []string
Failures []SkillImportFailure
}
// SkillImportFailure identifies a source skill that was deliberately skipped.
// The destination is never changed for a failed skill.
type SkillImportFailure struct {
Name string
Err error
}
// ImportSkills imports skills from a conventional coding-agent source into the
// canonical Ollama skills directory. Supported sources are codex, claude, and
// pi. Existing skills are left untouched: an identical directory is reported
// as existing, and a differing one is reported as a conflict.
func ImportSkills(source string) (SkillImportResult, error) {
home, err := os.UserHomeDir()
if err != nil {
return SkillImportResult{}, fmt.Errorf("resolve home directory: %w", err)
}
destination, err := SkillsDir()
if err != nil {
return SkillImportResult{}, fmt.Errorf("resolve Ollama skills directory: %w", err)
}
return importSkillsFromRoots(source, conventionalSkillImportRoots(home), destination)
}
func conventionalSkillImportRoots(home string) map[string]string {
return map[string]string{
"codex": filepath.Join(home, ".codex", "skills"),
"claude": filepath.Join(home, ".claude", "skills"),
"pi": filepath.Join(home, ".pi", "agent", "skills"),
}
}
func importSkillsFromRoots(source string, roots map[string]string, destination string) (SkillImportResult, error) {
source = strings.ToLower(strings.TrimSpace(source))
sourceDir, ok := roots[source]
if !ok {
return SkillImportResult{}, fmt.Errorf("unknown skill source %q", source)
}
return importSkillsFromDir(source, sourceDir, destination)
}
func importSkillsFromDir(source, sourceDir, destination string) (SkillImportResult, error) {
result := SkillImportResult{Source: source, SourceDir: sourceDir, Destination: destination}
info, err := os.Lstat(sourceDir)
if errors.Is(err, fs.ErrNotExist) {
return result, nil
}
if err != nil {
return result, fmt.Errorf("inspect %s skills directory: %w", source, err)
}
if info.Mode()&os.ModeSymlink != 0 {
return result, fmt.Errorf("inspect %s skills directory: symlinks are not supported", source)
}
if !info.IsDir() {
return result, fmt.Errorf("inspect %s skills directory: not a directory", source)
}
entries, err := os.ReadDir(sourceDir)
if err != nil {
return result, fmt.Errorf("read %s skills directory: %w", source, err)
}
for _, entry := range entries {
name := entry.Name()
path := filepath.Join(sourceDir, name)
if entry.Type()&os.ModeSymlink != 0 {
result.Failures = append(result.Failures, SkillImportFailure{Name: name, Err: errors.New("symlinked skill directories are not supported")})
continue
}
info, err := entry.Info()
if err != nil {
result.Failures = append(result.Failures, SkillImportFailure{Name: name, Err: fmt.Errorf("inspect source: %w", err)})
continue
}
if !info.IsDir() {
continue
}
if !skillName.MatchString(name) {
result.Failures = append(result.Failures, SkillImportFailure{Name: name, Err: errors.New("invalid skill directory name")})
continue
}
if err := validateImportSkill(path, name); err != nil {
result.Failures = append(result.Failures, SkillImportFailure{Name: name, Err: err})
continue
}
state, err := importSkillDirectory(path, filepath.Join(destination, name))
if err != nil {
result.Failures = append(result.Failures, SkillImportFailure{Name: name, Err: err})
continue
}
if state == skillImportExisting {
result.Existing = append(result.Existing, name)
} else {
result.Imported = append(result.Imported, name)
}
}
return result, nil
}
func validateImportSkill(dir, name string) error {
manifest := filepath.Join(dir, skillFilename)
info, err := os.Lstat(manifest)
if err != nil {
return fmt.Errorf("inspect %s: %w", skillFilename, err)
}
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
return fmt.Errorf("%s must be a regular, non-symlinked file", skillFilename)
}
if _, err := parseSkill(manifest, name); err != nil {
return err
}
return walkImportTree(dir, func(path string, entry fs.DirEntry, info fs.FileInfo) error {
if info.IsDir() || path == dir {
return nil
}
if !info.Mode().IsRegular() {
return fmt.Errorf("only regular files may be imported: %s", path)
}
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("read %s: %w", path, err)
}
return file.Close()
})
}
func walkImportTree(root string, visit func(string, fs.DirEntry, fs.FileInfo) error) error {
return filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(root, path)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return fmt.Errorf("unsafe skill path %q", path)
}
if entry.Type()&os.ModeSymlink != 0 {
return fmt.Errorf("symlinks may not be imported: %s", path)
}
info, err := entry.Info()
if err != nil {
return err
}
return visit(path, entry, info)
})
}
type skillImportState int
const (
skillImportCopied skillImportState = iota
skillImportExisting
)
func importSkillDirectory(source, destination string) (skillImportState, error) {
if info, err := os.Lstat(destination); err == nil {
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
return 0, errors.New("destination exists but is not a regular directory")
}
same, err := sameImportTree(source, destination)
if err != nil {
return 0, fmt.Errorf("inspect existing destination: %w", err)
}
if same {
return skillImportExisting, nil
}
return 0, errors.New("destination skill already exists with different contents")
} else if !errors.Is(err, fs.ErrNotExist) {
return 0, fmt.Errorf("inspect destination: %w", err)
}
if err := ensureImportDestination(filepath.Dir(destination)); err != nil {
return 0, err
}
stage, err := os.MkdirTemp(filepath.Dir(destination), "."+filepath.Base(destination)+".import-")
if err != nil {
return 0, fmt.Errorf("create import staging directory: %w", err)
}
defer os.RemoveAll(stage)
if err := copyImportTree(source, stage); err != nil {
return 0, err
}
if _, err := os.Lstat(destination); err == nil {
return 0, errors.New("destination skill was created during import")
} else if !errors.Is(err, fs.ErrNotExist) {
return 0, fmt.Errorf("inspect destination before install: %w", err)
}
if err := os.Rename(stage, destination); err != nil {
return 0, fmt.Errorf("install imported skill: %w", err)
}
return skillImportCopied, nil
}
func ensureImportDestination(dir string) error {
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("create Ollama skills directory: %w", err)
}
info, err := os.Lstat(dir)
if err != nil {
return fmt.Errorf("inspect Ollama skills directory: %w", err)
}
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
return errors.New("Ollama skills directory must be a regular, non-symlinked directory")
}
return nil
}
func copyImportTree(source, destination string) error {
return walkImportTree(source, func(path string, entry fs.DirEntry, info fs.FileInfo) error {
rel, err := filepath.Rel(source, path)
if err != nil {
return err
}
target := destination
if rel != "." {
target = filepath.Join(destination, rel)
}
if info.IsDir() {
if rel == "." {
return nil
}
return os.Mkdir(target, info.Mode().Perm())
}
if !info.Mode().IsRegular() {
return fmt.Errorf("only regular files may be imported: %s", path)
}
return copyImportFile(path, target, info.Mode().Perm())
})
}
func copyImportFile(source, destination string, mode fs.FileMode) error {
in, err := os.Open(source)
if err != nil {
return fmt.Errorf("read %s: %w", source, err)
}
defer in.Close()
out, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode)
if err != nil {
return fmt.Errorf("create %s: %w", destination, err)
}
_, copyErr := io.Copy(out, in)
closeErr := out.Close()
if copyErr != nil {
return fmt.Errorf("copy %s: %w", source, copyErr)
}
if closeErr != nil {
return fmt.Errorf("write %s: %w", destination, closeErr)
}
return nil
}
func sameImportTree(source, destination string) (bool, error) {
seen := make(map[string]struct{})
same := true
err := walkImportTree(source, func(path string, entry fs.DirEntry, info fs.FileInfo) error {
rel, err := filepath.Rel(source, path)
if err != nil {
return err
}
seen[rel] = struct{}{}
other := destination
if rel != "." {
other = filepath.Join(destination, rel)
}
otherInfo, err := os.Lstat(other)
if errors.Is(err, fs.ErrNotExist) {
same = false
return nil
}
if err != nil {
return err
}
if otherInfo.Mode()&os.ModeSymlink != 0 || otherInfo.IsDir() != info.IsDir() || (!info.IsDir() && !otherInfo.Mode().IsRegular()) {
same = false
return nil
}
if info.Mode().IsRegular() {
equal, err := sameImportFile(path, other)
if err != nil {
return err
}
if !equal {
same = false
}
}
return nil
})
if err != nil || !same {
return same, err
}
err = walkImportTree(destination, func(path string, entry fs.DirEntry, info fs.FileInfo) error {
rel, err := filepath.Rel(destination, path)
if err != nil {
return err
}
if _, ok := seen[rel]; !ok {
same = false
}
return nil
})
return same, err
}
func sameImportFile(first, second string) (bool, error) {
a, err := os.Open(first)
if err != nil {
return false, err
}
defer a.Close()
b, err := os.Open(second)
if err != nil {
return false, err
}
defer b.Close()
left := make([]byte, 32*1024)
right := make([]byte, len(left))
for {
n, errA := a.Read(left)
m, errB := b.Read(right)
if n != m || !bytes.Equal(left[:n], right[:m]) {
return false, nil
}
if errA == io.EOF && errB == io.EOF {
return true, nil
}
if errA != nil && errA != io.EOF {
return false, errA
}
if errB != nil && errB != io.EOF {
return false, errB
}
if errA == io.EOF || errB == io.EOF {
return false, nil
}
}
}
// defaultSkillRoots returns skill directories ordered lowest- to
// highest-precedence. Non-existent directories are scanned harmlessly
// (DiscoverSkills skips them).
func defaultSkillRoots(projectDir string) ([]skillRoot, error) {
var roots []skillRoot
if home, err := os.UserHomeDir(); err == nil && home != "" {
roots = append(roots, skillRoot{path: filepath.Join(home, ".agents", "skills")})
}
userOllama, err := SkillsDir()
if err != nil {
return nil, err
}
roots = append(roots, skillRoot{path: userOllama})
projectDir = strings.TrimSpace(projectDir)
if projectDir != "" {
if abs, err := filepath.Abs(projectDir); err == nil {
roots = append(roots,
skillRoot{path: filepath.Join(abs, ".agents", "skills")},
skillRoot{path: filepath.Join(abs, ".ollama", "skills")},
)
}
}
return roots, nil
}
func (c *SkillCatalog) Dir() string {
if c == nil {
return ""
}
return c.dir
}
func (c *SkillCatalog) List() []Skill {
if c == nil {
return nil
}
list := make([]Skill, 0, len(c.skills))
for _, skill := range c.skills {
list = append(list, skill)
}
sort.Slice(list, func(i, j int) bool { return list[i].Name < list[j].Name })
return list
}
func (c *SkillCatalog) Diagnostics() []error {
if c == nil {
return nil
}
return append([]error(nil), c.diagnostics...)
}
// ExcludeNames removes skills whose names are reserved by a caller. It returns
// the excluded names in sorted order.
func (c *SkillCatalog) ExcludeNames(names []string) []string {
if c == nil {
return nil
}
reserved := make(map[string]struct{}, len(names))
for _, name := range names {
name = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(name)), "/")
if name != "" {
reserved[name] = struct{}{}
}
}
var excluded []string
for name := range c.skills {
if _, ok := reserved[name]; !ok {
continue
}
delete(c.skills, name)
excluded = append(excluded, name)
}
sort.Strings(excluded)
return excluded
}
func (c *SkillCatalog) Load(name string) (Skill, error) {
name = strings.TrimSpace(name)
if !skillName.MatchString(name) {
return Skill{}, fmt.Errorf("invalid skill name %q", name)
}
if c == nil {
return Skill{}, errors.New("skills are unavailable")
}
skill, ok := c.skills[name]
if !ok {
return Skill{}, fmt.Errorf("skill %q not found in %s", name, c.dir)
}
return skill, nil
}
// SystemContext advertises the catalog without expanding full instructions in
// every request. The skill call is the explicit loading boundary.
func (c *SkillCatalog) SystemContext() string {
list := c.List()
if len(list) == 0 {
return ""
}
lines := []string{"<available_skills>"}
for _, skill := range list {
description := skill.Description
if description == "" {
description = "No description provided."
}
lines = append(lines, fmt.Sprintf("- %s: %s", skill.Name, description))
}
lines = append(lines, "</available_skills>", "Load a matching skill with the skill tool before following its instructions. Skills only provide instructions; use ordinary tools for filesystem or network access, with their normal approval rules.")
return strings.Join(lines, "\n")
}
func parseSkill(path, directoryName string) (Skill, error) {
// Stat (not Lstat) so a symlinked SKILL.md resolves to its target file.
info, err := os.Stat(path)
if err != nil {
return Skill{}, err
}
if !info.Mode().IsRegular() {
return Skill{}, fmt.Errorf("skill %q: %s is not a regular file", directoryName, skillFilename)
}
if info.Size() > maxSkillBytes {
return Skill{}, fmt.Errorf("skill %q: %s exceeds %d bytes", directoryName, skillFilename, maxSkillBytes)
}
data, err := os.ReadFile(path)
if err != nil {
return Skill{}, fmt.Errorf("read skill %q: %w", directoryName, err)
}
return parseSkillContent(path, directoryName, string(data))
}
func parseSkillContent(path, directoryName, input string) (Skill, error) {
instructions := strings.TrimSpace(input)
if instructions == "" {
return Skill{}, fmt.Errorf("skill %q: %s is empty", directoryName, skillFilename)
}
if !strings.HasPrefix(instructions, "---\n") && !strings.HasPrefix(instructions, "---\r\n") {
return Skill{}, fmt.Errorf("skill %q: missing YAML front matter", directoryName)
}
metadata, body, err := skillFrontMatter(instructions)
if err != nil {
return Skill{}, fmt.Errorf("skill %q: %w", directoryName, err)
}
if metadata.Name == "" {
return Skill{}, fmt.Errorf("skill %q: front matter requires name", directoryName)
}
if metadata.Description == "" {
return Skill{}, fmt.Errorf("skill %q: front matter requires description", directoryName)
}
if !skillName.MatchString(metadata.Name) {
return Skill{}, fmt.Errorf("skill %q: invalid front matter name %q", directoryName, metadata.Name)
}
if metadata.Name != directoryName {
return Skill{}, fmt.Errorf("skill %q: front matter name %q must match directory name", directoryName, metadata.Name)
}
skill := Skill{Name: metadata.Name, Description: metadata.Description, Path: path}
instructions = body
if strings.TrimSpace(instructions) == "" {
return Skill{}, fmt.Errorf("skill %q: instructions are empty", directoryName)
}
skill.Instructions = strings.TrimSpace(instructions)
return skill, nil
}
type skillFrontMatterMetadata struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Metadata map[string]any `yaml:"metadata"`
}
func skillFrontMatter(input string) (skillFrontMatterMetadata, string, error) {
input = strings.ReplaceAll(input, "\r\n", "\n")
lines := strings.Split(input, "\n")
if len(lines) < 3 || lines[0] != "---" {
return skillFrontMatterMetadata{}, "", errors.New("invalid front matter")
}
for i := 1; i < len(lines); i++ {
if lines[i] == "---" {
var metadata skillFrontMatterMetadata
if err := yaml.Unmarshal([]byte(strings.Join(lines[1:i], "\n")), &metadata); err != nil {
return skillFrontMatterMetadata{}, "", fmt.Errorf("parse YAML front matter: %w", err)
}
metadata.Name = strings.TrimSpace(metadata.Name)
metadata.Description = strings.TrimSpace(metadata.Description)
return metadata, strings.Join(lines[i+1:], "\n"), nil
}
}
return skillFrontMatterMetadata{}, "", errors.New("front matter is not closed")
}
+165
View File
@@ -0,0 +1,165 @@
package skills
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
)
type ImportResult struct {
Source string
Skill Skill
From string
To string
Skipped bool
Error string
}
type skillDirCandidate struct {
Dir string
Skipped bool
Error string
}
func Import(source string, force bool) ([]ImportResult, error) {
dest, err := DefaultDir()
if err != nil {
return nil, err
}
return ImportToDir(source, dest, force)
}
func ImportToDir(source, dest string, force bool) ([]ImportResult, error) {
roots, err := SourceDirs(source)
if err != nil {
return nil, err
}
if err := os.MkdirAll(dest, 0o755); err != nil {
return nil, fmt.Errorf("create skills directory: %w", err)
}
var results []ImportResult
for _, root := range roots {
candidates, err := skillDirs(root)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
results = append(results, ImportResult{Source: source, From: root, Skipped: true, Error: err.Error()})
continue
}
for _, candidate := range candidates {
result := ImportResult{Source: source, From: candidate.Dir}
if candidate.Skipped {
result.Skipped = true
result.Error = candidate.Error
results = append(results, result)
continue
}
skill, err := ReadMetadata(filepath.Join(candidate.Dir, SkillFile))
if err != nil {
result.Skipped = true
result.Error = err.Error()
results = append(results, result)
continue
}
result.Skill = skill
result.To = filepath.Join(dest, skill.Name)
copyResult, err := copyDir(candidate.Dir, result.To, force)
if errors.Is(err, os.ErrExist) {
result.Skipped = true
result.Error = "already exists"
} else if err != nil {
result.Skipped = true
result.Error = err.Error()
} else if len(copyResult.Skipped) > 0 {
result.Error = "skipped symlinks: " + strings.Join(copyResult.Skipped, ", ")
}
results = append(results, result)
}
}
slices.SortFunc(results, func(a, b ImportResult) int {
return strings.Compare(a.Skill.Name+a.From, b.Skill.Name+b.From)
})
return results, nil
}
func SourceDirs(source string) ([]string, error) {
source = strings.ToLower(strings.TrimSpace(source))
if source == "" {
source = "all"
}
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("resolve home directory: %w", err)
}
dirs := map[string][]string{
"claude": {filepath.Join(home, ".claude", "skills")},
"codex": {filepath.Join(home, ".codex", "skills")},
"pi": {filepath.Join(home, ".pi", "skills"), filepath.Join(home, ".agents", "skills")},
"agents": {filepath.Join(home, ".agents", "skills")},
}
if source == "all" {
var all []string
for _, name := range []string{"claude", "codex", "pi"} {
all = append(all, dirs[name]...)
}
return uniqueStrings(all), nil
}
if roots, ok := dirs[source]; ok {
return roots, nil
}
return nil, fmt.Errorf("unknown skill source %q (use claude, codex, pi, agents, or all)", source)
}
func skillDirs(root string) ([]skillDirCandidate, error) {
entries, err := os.ReadDir(root)
if err != nil {
return nil, err
}
var dirs []skillDirCandidate
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), ".") {
continue
}
dir := filepath.Join(root, entry.Name())
if entry.Type()&os.ModeSymlink != 0 {
dirs = append(dirs, skillDirCandidate{
Dir: dir,
Skipped: true,
Error: "symlinked skill directories are not supported",
})
continue
}
if !entry.IsDir() {
continue
}
if _, err := os.Stat(filepath.Join(dir, SkillFile)); err == nil {
dirs = append(dirs, skillDirCandidate{Dir: dir})
}
}
slices.SortFunc(dirs, func(a, b skillDirCandidate) int {
return strings.Compare(a.Dir, b.Dir)
})
return dirs, nil
}
func uniqueStrings(values []string) []string {
seen := make(map[string]bool)
var out []string
for _, value := range values {
if seen[value] {
continue
}
seen[value] = true
out = append(out, value)
}
return out
}
+319
View File
@@ -0,0 +1,319 @@
package skills
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"gopkg.in/yaml.v3"
)
const (
SkillFile = "SKILL.md"
maxSkillFileBytes = 1 << 20
)
var validName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,63}$`)
type Skill struct {
Name string
Description string
Dir string
File string
}
type Catalog struct {
Dir string
Skills []Skill
Warnings []string
}
type frontmatter struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
}
func DefaultDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("resolve home directory: %w", err)
}
return filepath.Join(home, ".ollama", "skills"), nil
}
func LoadDefault() (*Catalog, error) {
dir, err := DefaultDir()
if err != nil {
return nil, err
}
return Load(dir)
}
func Load(dir string) (*Catalog, error) {
catalog := &Catalog{Dir: dir}
entries, err := os.ReadDir(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return catalog, nil
}
return nil, fmt.Errorf("read skills directory: %w", err)
}
seen := make(map[string]string)
for _, entry := range entries {
if !entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
continue
}
skillDir := filepath.Join(dir, entry.Name())
skill, err := ReadMetadata(filepath.Join(skillDir, SkillFile))
if err != nil {
catalog.Warnings = append(catalog.Warnings, fmt.Sprintf("%s: %v", skillDir, err))
continue
}
skill.Dir = skillDir
skill.File = filepath.Join(skillDir, SkillFile)
if previous, ok := seen[skill.Name]; ok {
catalog.Warnings = append(catalog.Warnings, fmt.Sprintf("%s: duplicate skill name %q already loaded from %s", skillDir, skill.Name, previous))
continue
}
seen[skill.Name] = skillDir
catalog.Skills = append(catalog.Skills, skill)
}
slices.SortFunc(catalog.Skills, func(a, b Skill) int {
return strings.Compare(a.Name, b.Name)
})
return catalog, nil
}
func ReadMetadata(path string) (Skill, error) {
data, err := readSkillFile(path)
if err != nil {
return Skill{}, err
}
meta, _, err := parseSkillFile(data)
if err != nil {
return Skill{}, err
}
if err := validateMetadata(meta); err != nil {
return Skill{}, err
}
return Skill{Name: meta.Name, Description: meta.Description}, nil
}
func (c *Catalog) Empty() bool {
return c == nil || len(c.Skills) == 0
}
func (c *Catalog) Find(name string) (Skill, bool) {
if c == nil {
return Skill{}, false
}
name = NormalizeName(name)
for _, skill := range c.Skills {
if skill.Name == name {
return skill, true
}
}
return Skill{}, false
}
func (c *Catalog) SummaryMarkdown() string {
if c.Empty() {
return "No skills are installed."
}
var b strings.Builder
b.WriteString("Installed skills:\n\n")
for _, skill := range c.Skills {
b.WriteString("- **")
b.WriteString(skill.Name)
b.WriteString("**: ")
b.WriteString(skill.Description)
b.WriteByte('\n')
}
return strings.TrimRight(b.String(), "\n")
}
func (c *Catalog) SystemPrompt(toolAvailable bool) string {
if c.Empty() {
return ""
}
var b strings.Builder
b.WriteString("Agent skills are available. Skills are reusable instruction packages stored under ")
b.WriteString(c.Dir)
b.WriteString(".\n")
b.WriteString("Use a skill when its description matches the user's task. Load only metadata up front; load full instructions only when needed.\n")
if toolAvailable {
b.WriteString("To load a skill, call the skill tool with the skill name. After loading SKILL.md, follow it. Resolve relative references from the returned skill directory.\n")
} else {
b.WriteString("This model cannot call tools in this session. Follow any skill instructions that are explicitly provided by the user or system.\n")
}
b.WriteString("\nAvailable skills:\n")
for _, skill := range c.Skills {
b.WriteString("- ")
b.WriteString(skill.Name)
b.WriteString(": ")
b.WriteString(skill.Description)
b.WriteByte('\n')
}
return strings.TrimRight(b.String(), "\n")
}
func (s Skill) Read() (string, error) {
if s.File == "" {
return "", fmt.Errorf("skill %q has no %s path", s.Name, SkillFile)
}
data, err := readSkillFile(s.File)
if err != nil {
return "", err
}
return string(data), nil
}
func readSkillFile(path string) ([]byte, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, err
}
if info.Mode()&os.ModeSymlink != 0 {
return nil, fmt.Errorf("%s must not be a symlink", SkillFile)
}
if !info.Mode().IsRegular() {
return nil, fmt.Errorf("%s must be a regular file", SkillFile)
}
if info.Size() > maxSkillFileBytes {
return nil, fmt.Errorf("%s exceeds %d bytes", SkillFile, maxSkillFileBytes)
}
return os.ReadFile(path)
}
func NormalizeName(name string) string {
name = strings.TrimSpace(name)
name = strings.TrimPrefix(name, "/")
return strings.ToLower(name)
}
func parseSkillFile(data []byte) (frontmatter, string, error) {
text := strings.ReplaceAll(string(data), "\r\n", "\n")
if !strings.HasPrefix(text, "---\n") {
return frontmatter{}, "", fmt.Errorf("%s must start with YAML frontmatter", SkillFile)
}
rest := text[len("---\n"):]
end := strings.Index(rest, "\n---")
if end < 0 {
return frontmatter{}, "", fmt.Errorf("%s frontmatter is not closed", SkillFile)
}
var meta frontmatter
if err := yaml.Unmarshal([]byte(rest[:end]), &meta); err != nil {
return frontmatter{}, "", fmt.Errorf("parse frontmatter: %w", err)
}
body := rest[end+len("\n---"):]
body = strings.TrimPrefix(body, "\n")
return meta, body, nil
}
func validateMetadata(meta frontmatter) error {
if !validName.MatchString(meta.Name) {
return fmt.Errorf("invalid skill name %q", meta.Name)
}
if strings.TrimSpace(meta.Description) == "" {
return fmt.Errorf("skill %q has empty description", meta.Name)
}
if len([]rune(meta.Description)) > 1024 {
return fmt.Errorf("skill %q description exceeds 1024 characters", meta.Name)
}
return nil
}
type copyDirResult struct {
Skipped []string
}
func copyDir(src, dst string, force bool) (copyDirResult, error) {
if _, err := os.Stat(dst); err == nil && !force {
return copyDirResult{}, fs.ErrExist
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
return copyDirResult{}, err
}
parent := filepath.Dir(dst)
if err := os.MkdirAll(parent, 0o755); err != nil {
return copyDirResult{}, err
}
tmp, err := os.MkdirTemp(parent, "."+filepath.Base(dst)+".tmp-*")
if err != nil {
return copyDirResult{}, err
}
moved := false
defer func() {
if !moved {
_ = os.RemoveAll(tmp)
}
}()
var result copyDirResult
if err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
if rel == "." {
return nil
}
target := filepath.Join(tmp, rel)
if d.Type()&os.ModeSymlink != 0 {
result.Skipped = append(result.Skipped, rel)
return nil
}
if d.IsDir() {
info, err := d.Info()
if err != nil {
return err
}
return os.MkdirAll(target, info.Mode().Perm())
}
info, err := d.Info()
if err != nil {
return err
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
return os.WriteFile(target, data, info.Mode().Perm())
}); err != nil {
return result, err
}
if force {
if err := os.RemoveAll(dst); err != nil {
return result, err
}
}
if _, err := os.Stat(dst); err == nil {
return result, fs.ErrExist
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
return result, err
}
if err := os.Rename(tmp, dst); err != nil {
return result, err
}
moved = true
return result, nil
}
+157
View File
@@ -0,0 +1,157 @@
package skills
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestLoadCatalogReadsSkillMetadata(t *testing.T) {
dir := t.TempDir()
writeSkill(t, filepath.Join(dir, "go-code"), "go-code", "Write idiomatic Go code.")
catalog, err := Load(dir)
if err != nil {
t.Fatal(err)
}
if len(catalog.Skills) != 1 {
t.Fatalf("skills = %d, want 1: %#v", len(catalog.Skills), catalog)
}
if got := catalog.Skills[0].Name; got != "go-code" {
t.Fatalf("skill name = %q", got)
}
if prompt := catalog.SystemPrompt(true); !strings.Contains(prompt, "go-code: Write idiomatic Go code.") || !strings.Contains(prompt, "call the skill tool") {
t.Fatalf("system prompt missing skill metadata: %q", prompt)
}
}
func TestLoadCatalogSkipsInvalidSkills(t *testing.T) {
dir := t.TempDir()
writeSkill(t, filepath.Join(dir, "bad"), "Bad_Name", "bad")
catalog, err := Load(dir)
if err != nil {
t.Fatal(err)
}
if len(catalog.Skills) != 0 {
t.Fatalf("skills = %#v, want none", catalog.Skills)
}
if len(catalog.Warnings) == 0 {
t.Fatal("expected invalid skill warning")
}
}
func TestImportToDirCopiesCanonicalSkill(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
src := filepath.Join(home, ".claude", "skills", "go-code")
writeSkill(t, src, "go-code", "Write idiomatic Go code.")
if err := os.WriteFile(filepath.Join(src, "notes.md"), []byte("notes"), 0o644); err != nil {
t.Fatal(err)
}
dest := filepath.Join(home, ".ollama", "skills")
results, err := ImportToDir("claude", dest, false)
if err != nil {
t.Fatal(err)
}
if len(results) != 1 || results[0].Skipped {
t.Fatalf("results = %#v", results)
}
if _, err := os.Stat(filepath.Join(dest, "go-code", "notes.md")); err != nil {
t.Fatal(err)
}
}
func TestReadMetadataRejectsSymlink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink permissions vary on Windows")
}
dir := t.TempDir()
real := filepath.Join(dir, "real.md")
if err := os.WriteFile(real, []byte("---\nname: go-code\ndescription: Write idiomatic Go code.\n---\n"), 0o644); err != nil {
t.Fatal(err)
}
link := filepath.Join(dir, SkillFile)
if err := os.Symlink(real, link); err != nil {
t.Fatal(err)
}
if _, err := ReadMetadata(link); err == nil || !strings.Contains(err.Error(), "must not be a symlink") {
t.Fatalf("ReadMetadata error = %v, want symlink rejection", err)
}
}
func TestImportToDirReportsSymlinkedSkillDirectory(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink permissions vary on Windows")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
srcRoot := filepath.Join(home, ".claude", "skills")
real := filepath.Join(home, "elsewhere", "go-code")
writeSkill(t, real, "go-code", "Write idiomatic Go code.")
if err := os.MkdirAll(srcRoot, 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(real, filepath.Join(srcRoot, "go-code")); err != nil {
t.Fatal(err)
}
results, err := ImportToDir("claude", filepath.Join(home, ".ollama", "skills"), false)
if err != nil {
t.Fatal(err)
}
if len(results) != 1 || !results[0].Skipped {
t.Fatalf("results = %#v, want one skipped symlink directory", results)
}
if !strings.Contains(results[0].Error, "symlinked skill directories") {
t.Fatalf("error = %q, want symlink directory warning", results[0].Error)
}
}
func TestImportToDirReportsSkippedSymlinkEntries(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink permissions vary on Windows")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
src := filepath.Join(home, ".claude", "skills", "go-code")
writeSkill(t, src, "go-code", "Write idiomatic Go code.")
target := filepath.Join(home, "outside.md")
if err := os.WriteFile(target, []byte("outside"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, filepath.Join(src, "outside.md")); err != nil {
t.Fatal(err)
}
results, err := ImportToDir("claude", filepath.Join(home, ".ollama", "skills"), false)
if err != nil {
t.Fatal(err)
}
if len(results) != 1 || results[0].Skipped {
t.Fatalf("results = %#v, want one imported skill", results)
}
if !strings.Contains(results[0].Error, "skipped symlinks: outside.md") {
t.Fatalf("error = %q, want skipped symlink warning", results[0].Error)
}
if _, err := os.Lstat(filepath.Join(home, ".ollama", "skills", "go-code", "outside.md")); !os.IsNotExist(err) {
t.Fatalf("copied symlink err = %v, want missing symlink", err)
}
}
func writeSkill(t *testing.T, dir, name, description string) {
t.Helper()
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
content := "---\nname: " + name + "\ndescription: " + description + "\n---\n\n# " + name + "\n\nUse this skill.\n"
if err := os.WriteFile(filepath.Join(dir, SkillFile), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
-516
View File
@@ -1,516 +0,0 @@
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
func writeCatalogSkill(t *testing.T, dir, name, content string) {
t.Helper()
path := filepath.Join(dir, name)
if err := os.MkdirAll(path, 0o755); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(content, "---") {
content = "---\nname: " + name + "\ndescription: Test skill.\n---\n" + content
}
if err := os.WriteFile(filepath.Join(path, skillFilename), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func writeImportFixtureSkill(t *testing.T, dir string) {
t.Helper()
contents, err := os.ReadFile(filepath.Join("testdata", "import", "release-notes", skillFilename))
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "release-notes", skillFilename)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, contents, 0o644); err != nil {
t.Fatal(err)
}
}
func TestDiscoverAndLoadSkills(t *testing.T) {
dir := t.TempDir()
writeCatalogSkill(t, dir, "release-notes", "---\nname: release-notes\ndescription: Draft concise release notes.\nmetadata:\n author: Ollama\n labels:\n - release\n - docs\n---\n# Release notes\n\nUse short bullets.")
catalog, err := DiscoverSkills(dir)
if err != nil {
t.Fatal(err)
}
list := catalog.List()
if len(list) != 1 || list[0].Name != "release-notes" || list[0].Description != "Draft concise release notes." {
t.Fatalf("skills = %#v", list)
}
skill, err := catalog.Load("release-notes")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(skill.Content(), `<skill name="release-notes">`) || !strings.Contains(skill.Content(), "Use short bullets.") {
t.Fatalf("skill content = %q", skill.Content())
}
if context := catalog.SystemContext(); !strings.Contains(context, "release-notes: Draft concise release notes.") || !strings.Contains(context, "normal approval rules") {
t.Fatalf("system context = %q", context)
}
}
func TestDiscoverSkillsSkipsMalformedEntries(t *testing.T) {
dir := t.TempDir()
writeCatalogSkill(t, dir, "valid", "do the useful thing")
writeCatalogSkill(t, dir, "mismatched", "---\nname: whatever\ndescription: wrong name\n---\nbody")
// Genuinely malformed front matter (a line without a key:value pair) is still rejected.
writeCatalogSkill(t, dir, "broken", "---\nname: broken\ndescription\n---\nnope")
writeCatalogSkill(t, dir, "missing-name", "---\ndescription: missing name\n---\nbody")
writeCatalogSkill(t, dir, "missing-description", "---\nname: missing-description\n---\nbody")
writeCatalogSkill(t, dir, "bad-name", "---\nname: bad_name\ndescription: invalid name\n---\nbody")
writeCatalogSkill(t, dir, "under_score", "---\nname: under_score\ndescription: invalid directory\n---\nbody")
if err := os.MkdirAll(filepath.Join(dir, "no-front-matter"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "no-front-matter", skillFilename), []byte("body"), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := DiscoverSkills(dir)
if err != nil {
t.Fatal(err)
}
if got, want := len(catalog.List()), 1; got != want {
t.Fatalf("valid skills = %d, want %d", got, want)
}
if got, want := len(catalog.Diagnostics()), 7; got != want {
t.Fatalf("diagnostics = %d, want %d: %#v", got, want, catalog.Diagnostics())
}
if _, err := catalog.Load("broken"); err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("load broken error = %v", err)
}
if _, err := catalog.Load("../valid"); err == nil || !strings.Contains(err.Error(), "invalid skill name") {
t.Fatalf("unsafe name error = %v", err)
}
}
func TestDiscoverSkillsFollowsSymlinks(t *testing.T) {
dir := t.TempDir()
target := t.TempDir()
writeCatalogSkill(t, target, "shared", "---\nname: shared\ndescription: From a linked repo.\n---\nshared instructions")
if err := os.Symlink(filepath.Join(target, "shared"), filepath.Join(dir, "shared")); err != nil {
t.Skipf("symlink not supported: %v", err)
}
catalog, err := DiscoverSkills(dir)
if err != nil {
t.Fatal(err)
}
list := catalog.List()
if len(list) != 1 || list[0].Name != "shared" || list[0].Description != "From a linked repo." {
t.Fatalf("symlinked skills = %#v", list)
}
if !strings.Contains(list[0].Content(), "shared instructions") {
t.Fatalf("symlinked skill content = %q", list[0].Content())
}
}
func TestLoadDefaultSkillsContinuesAfterBadRoot(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
project := t.TempDir()
writeCatalogSkill(t, filepath.Join(project, ".ollama", "skills"), "release-notes", "project instructions")
badRoot := filepath.Join(t.TempDir(), "not-a-directory")
if err := os.WriteFile(badRoot, []byte("not a directory"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv(SkillsDirEnv, badRoot)
catalog, err := LoadDefaultSkills(project)
if err != nil {
t.Fatal(err)
}
if _, err := catalog.Load("release-notes"); err != nil {
t.Fatalf("valid skill was hidden by bad root: %v", err)
}
if _, err := catalog.Load(bundledSkillCreatorName); err != nil {
t.Fatalf("bundled skill was hidden by bad root: %v", err)
}
var foundDiagnostic bool
for _, diagnostic := range catalog.Diagnostics() {
if strings.Contains(diagnostic.Error(), badRoot) {
foundDiagnostic = true
break
}
}
if !foundDiagnostic {
t.Fatalf("diagnostics = %#v, want bad root %q", catalog.Diagnostics(), badRoot)
}
}
func TestLoadDefaultSkillsInstallsBundledSkillCreator(t *testing.T) {
dir := t.TempDir()
t.Setenv(SkillsDirEnv, dir)
catalog, err := LoadDefaultSkills("")
if err != nil {
t.Fatal(err)
}
skill, err := catalog.Load(bundledSkillCreatorName)
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, bundledSkillCreatorName, skillFilename)
contents, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(contents) != bundledSkillCreatorContent {
t.Fatalf("installed skill = %q, want bundled contents", contents)
}
if skill.Path != path {
t.Fatalf("skill path = %q, want %q", skill.Path, path)
}
if !strings.Contains(skill.Content(), "Skill directory: "+filepath.Dir(path)) {
t.Fatalf("skill content does not identify its directory: %q", skill.Content())
}
}
func TestLoadDefaultSkillsUpdatesExistingSkillCreator(t *testing.T) {
dir := t.TempDir()
t.Setenv(SkillsDirEnv, dir)
writeCatalogSkill(t, dir, bundledSkillCreatorName, "custom instructions")
if _, err := LoadDefaultSkills(""); err != nil {
t.Fatal(err)
}
contents, err := os.ReadFile(filepath.Join(dir, bundledSkillCreatorName, skillFilename))
if err != nil {
t.Fatal(err)
}
if string(contents) != bundledSkillCreatorContent {
t.Fatalf("installed skill = %q, want bundled contents", contents)
}
}
func TestSkillsDirUsesOverrideAndXDG(t *testing.T) {
base := t.TempDir()
override := filepath.Join(base, "skills-override")
t.Setenv(SkillsDirEnv, override)
got, err := SkillsDir()
if err != nil {
t.Fatal(err)
}
want, err := filepath.Abs(override)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("SkillsDir override = %q, want %q", got, want)
}
t.Setenv(SkillsDirEnv, "")
xdg := filepath.Join(base, "xdg")
t.Setenv("XDG_CONFIG_HOME", xdg)
if got, err := SkillsDir(); err != nil || got != filepath.Join(xdg, "ollama", "skills") {
t.Fatalf("SkillsDir xdg = %q, want %q, %v", got, filepath.Join(xdg, "ollama", "skills"), err)
}
t.Setenv("XDG_CONFIG_HOME", "")
home := filepath.Join(base, "home")
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
if got, err := SkillsDir(); err != nil || got != filepath.Join(home, ".ollama", "skills") {
t.Fatalf("SkillsDir default = %q, want %q, %v", got, filepath.Join(home, ".ollama", "skills"), err)
}
}
func TestLoadDefaultSkillsPrecedenceAndCollisions(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home) // Windows: os.UserHomeDir uses %USERPROFILE%
userOllama := t.TempDir()
t.Setenv(SkillsDirEnv, userOllama)
userAgents := filepath.Join(home, ".agents", "skills")
project := t.TempDir()
projectAgents := filepath.Join(project, ".agents", "skills")
projectOllama := filepath.Join(project, ".ollama", "skills")
// release-notes exists in all four roots; project ollama must win.
writeCatalogSkill(t, userAgents, "release-notes", "from user agents")
writeCatalogSkill(t, userOllama, "release-notes", "from user ollama")
writeCatalogSkill(t, projectOllama, "release-notes", "from project ollama")
// code-review exists in both project roots; project ollama beats project agents.
writeCatalogSkill(t, projectAgents, "code-review", "from project agents")
writeCatalogSkill(t, projectOllama, "code-review", "from project ollama")
// unique appears only in user ollama (via env override).
writeCatalogSkill(t, userOllama, "unique", "only here")
catalog, err := LoadDefaultSkills(project)
if err != nil {
t.Fatal(err)
}
rn, err := catalog.Load("release-notes")
if err != nil || !strings.Contains(rn.Instructions, "from project ollama") || !strings.Contains(rn.Path, ".ollama") {
t.Fatalf("release-notes = %#v, want project ollama to win", rn)
}
cr, err := catalog.Load("code-review")
if err != nil || !strings.Contains(cr.Instructions, "from project ollama") {
t.Fatalf("code-review = %#v, want project ollama to win over project agents", cr)
}
if _, err := catalog.Load("unique"); err != nil {
t.Fatalf("unique should load from user ollama: %v", err)
}
// Collisions are resolved silently by precedence — no diagnostics.
for _, d := range catalog.Diagnostics() {
if strings.Contains(d.Error(), "shadows") {
t.Fatalf("unexpected shadow diagnostic: %v", d)
}
}
}
func TestSkillCatalogExcludeNames(t *testing.T) {
dir := t.TempDir()
for _, name := range []string{"release-notes", "system", "exit"} {
writeCatalogSkill(t, dir, name, "instructions")
}
catalog, err := DiscoverSkills(dir)
if err != nil {
t.Fatal(err)
}
if got, want := strings.Join(catalog.ExcludeNames([]string{"/system", "EXIT"}), ","), "exit,system"; got != want {
t.Fatalf("excluded skills = %q, want %q", got, want)
}
if _, err := catalog.Load("system"); err == nil {
t.Fatal("excluded system skill should not load")
}
if _, err := catalog.Load("exit"); err == nil {
t.Fatal("excluded exit skill should not load")
}
if _, err := catalog.Load("release-notes"); err != nil {
t.Fatalf("non-conflicting skill should remain available: %v", err)
}
}
func TestSkillContentListsDirectoryAndResources(t *testing.T) {
root := t.TempDir()
skillDir := filepath.Join(root, "pdf-processing")
if err := os.MkdirAll(filepath.Join(skillDir, "scripts"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(skillDir, "references"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("---\nname: pdf-processing\ndescription: Handle PDFs.\n---\nHandle PDFs."), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillDir, "scripts", "extract.py"), []byte("#!/usr/bin/env python3"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillDir, "references", "ref.md"), []byte("ref"), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := DiscoverSkills(root)
if err != nil {
t.Fatal(err)
}
skill, err := catalog.Load("pdf-processing")
if err != nil {
t.Fatal(err)
}
content := skill.Content()
if !strings.Contains(content, "Skill directory:") || !strings.Contains(content, skillDir) {
t.Fatalf("content missing skill directory: %q", content)
}
if !strings.Contains(content, "<file>scripts/extract.py</file>") || !strings.Contains(content, "<file>references/ref.md</file>") {
t.Fatalf("content missing resource listing: %q", content)
}
}
func TestImportSkillsCopiesFixtureAndIsIdempotent(t *testing.T) {
source := t.TempDir()
destination := t.TempDir()
writeImportFixtureSkill(t, source)
writeCatalogSkill(t, source, "broken", "---\nname: another-skill\ndescription: Deliberately invalid.\n---\nIgnore this.")
if err := os.MkdirAll(filepath.Join(source, "release-notes", "references"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(source, "release-notes", "references", "style.txt"), []byte("Keep it short.\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(source, "release-notes", "scripts"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(source, "release-notes", "scripts", "prepare.sh"), []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(source, "ignored.md"), []byte("Ignored root file.\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := importSkillsFromDir("codex", source, destination)
if err != nil {
t.Fatal(err)
}
if got, want := strings.Join(result.Imported, ","), "release-notes"; got != want {
t.Fatalf("imported = %q, want %q", got, want)
}
catalog, err := DiscoverSkills(destination)
if err != nil {
t.Fatal(err)
}
skill, err := catalog.Load("release-notes")
if err != nil || skill.Description != "Draft concise release notes." {
t.Fatalf("imported skill = %#v, %v", skill, err)
}
if got := len(result.Failures); got != 1 || result.Failures[0].Name != "broken" {
t.Fatalf("failures = %#v, want broken fixture failure", result.Failures)
}
for _, file := range []string{skillFilename, filepath.Join("references", "style.txt"), filepath.Join("scripts", "prepare.sh")} {
if _, err := os.Stat(filepath.Join(destination, "release-notes", file)); err != nil {
t.Fatalf("imported fixture file %q: %v", file, err)
}
}
result, err = importSkillsFromDir("codex", source, destination)
if err != nil {
t.Fatal(err)
}
if got, want := strings.Join(result.Existing, ","), "release-notes"; got != want {
t.Fatalf("existing = %q, want %q", got, want)
}
if len(result.Imported) != 0 {
t.Fatalf("repeated import copied skills: %#v", result.Imported)
}
}
func TestImportSkillsLeavesConflictsAndUnsafeSourcesUntouched(t *testing.T) {
source := t.TempDir()
destination := t.TempDir()
writeCatalogSkill(t, source, "release-notes", "source instructions")
writeCatalogSkill(t, destination, "release-notes", "existing instructions")
writeCatalogSkill(t, source, "nested-link", "safe manifest")
if err := os.Symlink(filepath.Join(source, "release-notes", skillFilename), filepath.Join(source, "nested-link", "reference")); err != nil {
t.Skipf("symlink not supported: %v", err)
}
if err := os.Symlink(filepath.Join(source, "release-notes"), filepath.Join(source, "linked-skill")); err != nil {
t.Skipf("symlink not supported: %v", err)
}
result, err := importSkillsFromDir("codex", source, destination)
if err != nil {
t.Fatal(err)
}
if len(result.Imported) != 0 || len(result.Existing) != 0 {
t.Fatalf("unexpected successful import: %#v", result)
}
if got, err := os.ReadFile(filepath.Join(destination, "release-notes", skillFilename)); err != nil || !strings.Contains(string(got), "existing instructions") {
t.Fatalf("conflicting destination changed: %q, %v", got, err)
}
failed := make(map[string]bool)
for _, failure := range result.Failures {
failed[failure.Name] = true
}
for _, name := range []string{"release-notes", "nested-link", "linked-skill"} {
if !failed[name] {
t.Fatalf("missing failure for %q: %#v", name, result.Failures)
}
}
}
func TestImportSkillsRejectsSymlinkedRoot(t *testing.T) {
root := t.TempDir()
source := filepath.Join(t.TempDir(), "codex-skills")
if err := os.Symlink(root, source); err != nil {
t.Skipf("symlink not supported: %v", err)
}
result, err := importSkillsFromDir("codex", source, t.TempDir())
if err == nil || !strings.Contains(err.Error(), "symlinks are not supported") {
t.Fatalf("symlinked root error = %v", err)
}
if len(result.Imported) != 0 || len(result.Existing) != 0 || len(result.Failures) != 0 {
t.Fatalf("symlinked root result = %#v", result)
}
}
func TestImportSkillsMissingRootAndConfiguredRoots(t *testing.T) {
result, err := importSkillsFromDir("codex", filepath.Join(t.TempDir(), "missing"), t.TempDir())
if err != nil {
t.Fatal(err)
}
if len(result.Imported) != 0 || len(result.Existing) != 0 || len(result.Failures) != 0 {
t.Fatalf("missing root result = %#v", result)
}
destination := t.TempDir()
rootBase := t.TempDir()
roots := map[string]string{
"codex": filepath.Join(rootBase, "codex"),
"claude": filepath.Join(rootBase, "claude"),
"pi": filepath.Join(rootBase, "pi"),
}
for _, test := range []struct {
source string
root string
name string
}{
{source: "codex", root: roots["codex"], name: "from-codex"},
{source: "claude", root: roots["claude"], name: "from-claude"},
{source: "pi", root: roots["pi"], name: "from-pi"},
} {
t.Run(test.source, func(t *testing.T) {
writeCatalogSkill(t, test.root, test.name, "from "+test.source)
result, err = importSkillsFromRoots(test.source, roots, destination)
if err != nil {
t.Fatal(err)
}
if result.SourceDir != test.root {
t.Fatalf("source dir = %q, want %q", result.SourceDir, test.root)
}
if _, err := os.Stat(filepath.Join(destination, test.name, skillFilename)); err != nil {
t.Fatalf("conventional source was not imported: %v", err)
}
})
}
if _, err := importSkillsFromRoots("unknown", roots, destination); err == nil || !strings.Contains(err.Error(), "unknown skill source") {
t.Fatalf("unknown source error = %v", err)
}
}
func TestConventionalSkillImportRoots(t *testing.T) {
home := t.TempDir()
roots := conventionalSkillImportRoots(home)
for source, want := range map[string]string{
"codex": filepath.Join(home, ".codex", "skills"),
"claude": filepath.Join(home, ".claude", "skills"),
"pi": filepath.Join(home, ".pi", "agent", "skills"),
} {
if got := roots[source]; got != want {
t.Fatalf("%s root = %q, want %q", source, got, want)
}
}
}
func TestImportSkillsRejectsUnreadableManifest(t *testing.T) {
source := t.TempDir()
writeCatalogSkill(t, source, "private", "do not read")
manifest := filepath.Join(source, "private", skillFilename)
if err := os.Chmod(manifest, 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(manifest, 0o644) })
if _, err := os.ReadFile(manifest); err == nil {
t.Skip("test user can read a mode-000 file")
}
result, err := importSkillsFromDir("codex", source, t.TempDir())
if err != nil {
t.Fatal(err)
}
if len(result.Failures) != 1 || result.Failures[0].Name != "private" {
t.Fatalf("failures = %#v", result.Failures)
}
}
+991
View File
@@ -0,0 +1,991 @@
package store
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/ollama/ollama/agent"
"github.com/ollama/ollama/api"
_ "github.com/mattn/go-sqlite3"
)
type Store struct {
DBPath string
dbMu sync.Mutex
db *database
}
type database struct {
conn *sql.DB
}
type AgentChat struct {
ID string
Title string
Model string
CreatedAt time.Time
Messages []api.Message
}
type ChatSummary struct {
ID string
Title string
Model string
CreatedAt time.Time
UpdatedAt time.Time
MessageCount int
ApproxBytes int64
}
func New(path string) (*Store, error) {
store := &Store{DBPath: path}
if err := store.ensureDB(); err != nil {
return nil, err
}
return store, nil
}
func (s *Store) ensureDB() error {
if s.db != nil {
return nil
}
s.dbMu.Lock()
defer s.dbMu.Unlock()
if s.db != nil {
return nil
}
dbPath := s.DBPath
if dbPath == "" {
dbPath = defaultDBPath()
}
if err := os.MkdirAll(filepath.Dir(dbPath), 0o755); err != nil {
return fmt.Errorf("create database directory: %w", err)
}
db, err := newDatabase(dbPath)
if err != nil {
return err
}
s.db = db
return nil
}
func (s *Store) Close() error {
if s == nil || s.db == nil {
return nil
}
err := s.db.Close()
s.db = nil
return err
}
func defaultDBPath() string {
switch runtime.GOOS {
case "windows":
return filepath.Join(os.Getenv("LOCALAPPDATA"), "Ollama", "db.sqlite")
case "darwin":
return filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "Ollama", "db.sqlite")
default:
return filepath.Join(os.Getenv("HOME"), ".ollama", "db.sqlite")
}
}
func newDatabase(dbPath string) (*database, error) {
conn, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=on&_journal_mode=WAL&_busy_timeout=5000&_txlock=immediate")
if err != nil {
return nil, fmt.Errorf("open database: %w", err)
}
if err := conn.Ping(); err != nil {
conn.Close()
return nil, fmt.Errorf("ping database: %w", err)
}
db := &database{conn: conn}
if err := db.init(); err != nil {
conn.Close()
return nil, fmt.Errorf("initialize database: %w", err)
}
return db, nil
}
func (db *database) Close() error {
_, _ = db.conn.Exec("PRAGMA wal_checkpoint(TRUNCATE);")
return db.conn.Close()
}
func (db *database) init() error {
if _, err := db.conn.Exec("PRAGMA foreign_keys = ON"); err != nil {
return fmt.Errorf("enable foreign keys: %w", err)
}
if _, err := db.conn.Exec(`
CREATE TABLE IF NOT EXISTS chats (
id TEXT PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
model_name TEXT NOT NULL DEFAULT '',
source TEXT NOT NULL DEFAULT 'app',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
browser_state TEXT
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL DEFAULT '',
thinking TEXT NOT NULL DEFAULT '',
images TEXT NOT NULL DEFAULT '[]',
stream BOOLEAN NOT NULL DEFAULT 0,
model_name TEXT,
model_cloud BOOLEAN,
model_ollama_host BOOLEAN,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
thinking_time_start TIMESTAMP,
thinking_time_end TIMESTAMP,
tool_result TEXT,
tool_name TEXT NOT NULL DEFAULT '',
tool_call_id TEXT NOT NULL DEFAULT '',
archived BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS tool_calls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER NOT NULL,
type TEXT NOT NULL,
tool_call_id TEXT NOT NULL DEFAULT '',
function_name TEXT NOT NULL,
function_arguments TEXT NOT NULL,
function_result TEXT,
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS compactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
summary TEXT NOT NULL,
archived_message_ids TEXT NOT NULL DEFAULT '[]',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
`); err != nil {
return err
}
return db.ensureAgentSchema()
}
func (db *database) ensureAgentSchema() error {
for _, stmt := range []struct {
sql string
msg string
}{
{`ALTER TABLE chats ADD COLUMN model_name TEXT NOT NULL DEFAULT ''`, "add chats.model_name"},
{`ALTER TABLE chats ADD COLUMN source TEXT NOT NULL DEFAULT 'app'`, "add chats.source"},
{`ALTER TABLE messages ADD COLUMN images TEXT NOT NULL DEFAULT '[]'`, "add messages.images"},
{`ALTER TABLE messages ADD COLUMN tool_name TEXT NOT NULL DEFAULT ''`, "add messages.tool_name"},
{`ALTER TABLE messages ADD COLUMN tool_call_id TEXT NOT NULL DEFAULT ''`, "add messages.tool_call_id"},
{`ALTER TABLE messages ADD COLUMN archived BOOLEAN NOT NULL DEFAULT 0`, "add messages.archived"},
{`ALTER TABLE tool_calls ADD COLUMN tool_call_id TEXT NOT NULL DEFAULT ''`, "add tool_calls.tool_call_id"},
} {
_, err := db.conn.Exec(stmt.sql)
if err != nil && !duplicateColumnError(err) {
return fmt.Errorf("%s: %w", stmt.msg, err)
}
}
_, err := db.conn.Exec(`
CREATE INDEX IF NOT EXISTS idx_messages_chat_id ON messages(chat_id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_id ON messages(chat_id, id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_archived ON messages(chat_id, archived, id);
CREATE INDEX IF NOT EXISTS idx_tool_calls_message_id ON tool_calls(message_id);
CREATE TABLE IF NOT EXISTS compactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
summary TEXT NOT NULL,
archived_message_ids TEXT NOT NULL DEFAULT '[]',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_compactions_chat_id ON compactions(chat_id, id);
`)
if err != nil {
return fmt.Errorf("create agent chat persistence tables: %w", err)
}
return nil
}
func duplicateColumnError(err error) bool {
return err != nil && strings.Contains(strings.ToLower(err.Error()), "duplicate column")
}
func (s *Store) EnsureChat(ctx context.Context, id string, title string) error {
if id == "" {
return fmt.Errorf("chat id is required")
}
if err := s.ensureDB(); err != nil {
return err
}
_, err := s.db.conn.ExecContext(ctx, `
INSERT INTO chats (id, title, created_at, source)
VALUES (?, ?, ?, 'agent')
ON CONFLICT(id) DO UPDATE SET
title = CASE
WHEN excluded.title != '' THEN excluded.title
ELSE chats.title
END,
source = 'agent'
`, id, title, time.Now())
if err != nil {
return fmt.Errorf("ensure chat: %w", err)
}
return nil
}
func (s *Store) SetChatModel(ctx context.Context, chatID string, model string) error {
chatID = strings.TrimSpace(chatID)
model = strings.TrimSpace(model)
if chatID == "" {
return fmt.Errorf("chat id is required")
}
if model == "" {
return fmt.Errorf("model is required")
}
if err := s.EnsureChat(ctx, chatID, ""); err != nil {
return err
}
if _, err := s.db.conn.ExecContext(ctx, `UPDATE chats SET model_name = ? WHERE id = ?`, model, chatID); err != nil {
return fmt.Errorf("set chat model: %w", err)
}
return nil
}
func (s *Store) AppendAgentMessage(ctx context.Context, chatID string, msg api.Message, model string) error {
if err := s.EnsureChat(ctx, chatID, ""); err != nil {
return err
}
tx, err := s.db.conn.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer tx.Rollback()
messageID, err := insertAgentMessage(ctx, tx, chatID, msg, model)
if err != nil {
return err
}
for _, toolCall := range msg.ToolCalls {
if err := insertAgentToolCall(ctx, tx, messageID, toolCall); err != nil {
return err
}
}
if msg.Role == "user" && strings.TrimSpace(msg.Content) != "" {
if err := maybeSetAgentTitle(ctx, tx, chatID, msg.Content); err != nil {
return err
}
}
return tx.Commit()
}
func (s *Store) UpdateLastAgentMessage(ctx context.Context, chatID string, msg api.Message, model string) error {
if err := s.ensureDB(); err != nil {
return err
}
tx, err := s.db.conn.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer tx.Rollback()
var messageID int64
if err := tx.QueryRowContext(ctx, `SELECT COALESCE(MAX(id), 0) FROM messages WHERE chat_id = ? AND archived = 0`, chatID).Scan(&messageID); err != nil {
return fmt.Errorf("get last message id: %w", err)
}
if messageID == 0 {
return fmt.Errorf("no message found to update")
}
now := time.Now()
modelName := sql.NullString{}
if model != "" {
modelName = sql.NullString{String: model, Valid: true}
}
imagesJSON, err := marshalAgentMessageImages(msg.Images)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, `
UPDATE messages
SET role = ?, content = ?, thinking = ?, images = ?, tool_name = ?, tool_call_id = ?, model_name = ?, updated_at = ?
WHERE id = ?
`, msg.Role, msg.Content, msg.Thinking, imagesJSON, msg.ToolName, msg.ToolCallID, modelName, now, messageID)
if err != nil {
return fmt.Errorf("update last message: %w", err)
}
if _, err := tx.ExecContext(ctx, `DELETE FROM tool_calls WHERE message_id = ?`, messageID); err != nil {
return fmt.Errorf("delete old tool calls: %w", err)
}
for _, toolCall := range msg.ToolCalls {
if err := insertAgentToolCall(ctx, tx, messageID, toolCall); err != nil {
return err
}
}
return tx.Commit()
}
func (s *Store) AgentChat(ctx context.Context, id string) (*AgentChat, error) {
if err := s.ensureDB(); err != nil {
return nil, err
}
var chat AgentChat
var chatModel string
if err := s.db.conn.QueryRowContext(ctx, `
SELECT id, title, model_name, created_at FROM chats WHERE id = ?
`, id).Scan(&chat.ID, &chat.Title, &chatModel, &chat.CreatedAt); err != nil {
return nil, err
}
if strings.TrimSpace(chatModel) != "" {
chat.Model = chatModel
} else {
model, err := latestAgentModelForChat(ctx, s.db.conn, id)
if err != nil {
return nil, err
}
chat.Model = model
}
rows, err := s.db.conn.QueryContext(ctx, `
SELECT id, role, content, thinking, images, tool_name, tool_call_id FROM messages WHERE chat_id = ? AND archived = 0 ORDER BY id ASC
`, id)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var messageID int64
var msg api.Message
var imagesJSON string
if err := rows.Scan(&messageID, &msg.Role, &msg.Content, &msg.Thinking, &imagesJSON, &msg.ToolName, &msg.ToolCallID); err != nil {
return nil, err
}
images, err := unmarshalAgentMessageImages(imagesJSON)
if err != nil {
return nil, err
}
msg.Images = images
toolCalls, err := getAgentToolCalls(ctx, s.db.conn, messageID)
if err != nil {
return nil, err
}
msg.ToolCalls = toolCalls
chat.Messages = append(chat.Messages, msg)
}
if err := rows.Err(); err != nil {
return nil, err
}
summary, err := latestCompactionSummary(ctx, s.db.conn, id)
if err != nil {
return nil, err
}
if summary != "" && !messagesContainCompactionSummary(chat.Messages) {
chat.Messages = insertCompactionSummaryAfterLeadingSystemMessages(chat.Messages, agent.CompactionSummaryMessages(summary, false))
} else {
chat.Messages = moveCompactionSummaryBeforeKeptMessages(chat.Messages)
}
chat.Messages = repairDanglingToolCalls(chat.Messages)
return &chat, nil
}
func (s *Store) LatestChat(ctx context.Context) (*AgentChat, error) {
if err := s.ensureDB(); err != nil {
return nil, err
}
var chatID string
query := fmt.Sprintf(`
SELECT c.id
FROM chats c
JOIN messages m ON m.chat_id = c.id AND m.archived = 0
WHERE c.source = 'agent'
GROUP BY c.id
HAVING %[1]s IS NOT NULL
ORDER BY MAX(m.updated_at) DESC, MAX(m.id) DESC
LIMIT 1
`, currentAgentModelSelectExpr("c"))
if err := s.db.conn.QueryRowContext(ctx, query).Scan(&chatID); err != nil {
return nil, err
}
return s.AgentChat(ctx, chatID)
}
func (s *Store) LatestChatForModel(ctx context.Context, model string) (*AgentChat, error) {
if strings.TrimSpace(model) == "" {
return nil, fmt.Errorf("model is required")
}
if err := s.ensureDB(); err != nil {
return nil, err
}
var chatID string
query := fmt.Sprintf(`
SELECT c.id
FROM chats c
JOIN messages m ON m.chat_id = c.id AND m.archived = 0
WHERE c.source = 'agent'
GROUP BY c.id
HAVING %[1]s = ?
ORDER BY MAX(m.updated_at) DESC, MAX(m.id) DESC
LIMIT 1
`, currentAgentModelSelectExpr("c"))
if err := s.db.conn.QueryRowContext(ctx, query, model).Scan(&chatID); err != nil {
return nil, err
}
return s.AgentChat(ctx, chatID)
}
func (s *Store) ListChats(ctx context.Context, limit int) ([]ChatSummary, error) {
if err := s.ensureDB(); err != nil {
return nil, err
}
if limit <= 0 {
limit = 50
}
query := fmt.Sprintf(`
SELECT
c.id,
c.title,
c.created_at,
MAX(m.updated_at) AS updated_at,
COUNT(m.id) AS message_count,
COALESCE(SUM(
LENGTH(m.role) +
LENGTH(m.content) +
LENGTH(m.thinking) +
LENGTH(m.tool_name) +
LENGTH(m.tool_call_id)
), 0) AS approx_bytes,
%[1]s AS current_model
FROM chats c
JOIN messages m ON m.chat_id = c.id AND m.archived = 0
WHERE c.source = 'agent'
GROUP BY c.id
ORDER BY updated_at DESC, MAX(m.id) DESC
LIMIT ?
`, currentAgentModelSelectExpr("c"))
rows, err := s.db.conn.QueryContext(ctx, query, limit)
if err != nil {
return nil, fmt.Errorf("list chats: %w", err)
}
defer rows.Close()
var summaries []ChatSummary
for rows.Next() {
var summary ChatSummary
var updatedAt string
var modelName sql.NullString
if err := rows.Scan(&summary.ID, &summary.Title, &summary.CreatedAt, &updatedAt, &summary.MessageCount, &summary.ApproxBytes, &modelName); err != nil {
return nil, fmt.Errorf("scan chat summary: %w", err)
}
if modelName.Valid {
summary.Model = modelName.String
}
summary.UpdatedAt, err = parseAgentSQLiteTime(updatedAt)
if err != nil {
return nil, fmt.Errorf("parse chat updated_at: %w", err)
}
summaries = append(summaries, summary)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("read chat summaries: %w", err)
}
return summaries, nil
}
func (s *Store) ListUserMessages(ctx context.Context, limit int) ([]string, error) {
if err := s.ensureDB(); err != nil {
return nil, err
}
if limit <= 0 {
limit = 50
}
rows, err := s.db.conn.QueryContext(ctx, `
SELECT content
FROM (
SELECT id, content
FROM messages
WHERE role = 'user'
AND archived = 0
AND TRIM(content) != ''
AND content NOT LIKE ?
ORDER BY id DESC
LIMIT ?
)
ORDER BY id ASC
`, agent.CompactionSummaryMessagePrefix+"%", limit)
if err != nil {
return nil, fmt.Errorf("list user messages: %w", err)
}
defer rows.Close()
var messages []string
for rows.Next() {
var content string
if err := rows.Scan(&content); err != nil {
return nil, fmt.Errorf("scan user message: %w", err)
}
messages = append(messages, content)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("read user messages: %w", err)
}
return messages, nil
}
func (s *Store) ArchiveForCompaction(ctx context.Context, chatID string, keepUserTurns int, summary string, continueTask bool) error {
return s.archiveForCompaction(ctx, chatID, keepUserTurns, summary, continueTask)
}
func (s *Store) archiveForCompaction(ctx context.Context, chatID string, keepUserTurns int, summary string, continueTask bool) error {
if err := s.ensureDB(); err != nil {
return err
}
if chatID == "" {
return fmt.Errorf("chat id is required")
}
if keepUserTurns < 0 {
return fmt.Errorf("keep user turns must be non-negative")
}
if strings.TrimSpace(summary) == "" {
return fmt.Errorf("summary is required")
}
tx, err := s.db.conn.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer tx.Rollback()
var keepStartID int64
if keepUserTurns == 0 {
if err := tx.QueryRowContext(ctx, `
SELECT COALESCE(MAX(id) + 1, 0)
FROM messages
WHERE chat_id = ? AND archived = 0
`, chatID).Scan(&keepStartID); err != nil {
return fmt.Errorf("find compaction boundary: %w", err)
}
if keepStartID == 0 {
return nil
}
} else {
if err := tx.QueryRowContext(ctx, `
SELECT id
FROM messages
WHERE chat_id = ? AND archived = 0 AND role = 'user'
ORDER BY id DESC
LIMIT 1 OFFSET ?
`, chatID, keepUserTurns-1).Scan(&keepStartID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil
}
return fmt.Errorf("find compaction boundary: %w", err)
}
}
rows, err := tx.QueryContext(ctx, `
SELECT m.id
FROM messages m
WHERE m.chat_id = ? AND m.archived = 0 AND (
m.id < ?
OR m.tool_name = ?
OR EXISTS (
SELECT 1 FROM tool_calls tc
WHERE tc.message_id = m.id AND tc.function_name = ?
)
)
ORDER BY id ASC
`, chatID, keepStartID, agent.CompactionToolName, agent.CompactionToolName)
if err != nil {
return fmt.Errorf("list archived messages: %w", err)
}
var archivedIDs []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
rows.Close()
return fmt.Errorf("scan archived message id: %w", err)
}
archivedIDs = append(archivedIDs, id)
}
if err := rows.Err(); err != nil {
rows.Close()
return fmt.Errorf("read archived message ids: %w", err)
}
rows.Close()
if len(archivedIDs) == 0 {
return nil
}
idsJSON, err := json.Marshal(archivedIDs)
if err != nil {
return fmt.Errorf("marshal archived message ids: %w", err)
}
if _, err := tx.ExecContext(ctx, `
INSERT INTO compactions (chat_id, summary, archived_message_ids, created_at)
VALUES (?, ?, ?, ?)
`, chatID, summary, string(idsJSON), time.Now()); err != nil {
return fmt.Errorf("insert compaction: %w", err)
}
if _, err := tx.ExecContext(ctx, `
UPDATE messages
SET archived = 1
WHERE chat_id = ? AND archived = 0 AND (
id < ?
OR tool_name = ?
OR EXISTS (
SELECT 1 FROM tool_calls
WHERE tool_calls.message_id = messages.id AND tool_calls.function_name = ?
)
)
`, chatID, keepStartID, agent.CompactionToolName, agent.CompactionToolName); err != nil {
return fmt.Errorf("archive messages: %w", err)
}
for _, msg := range agent.CompactionSummaryMessages(summary, continueTask) {
messageID, err := insertAgentMessage(ctx, tx, chatID, msg, "")
if err != nil {
return err
}
for _, toolCall := range msg.ToolCalls {
if err := insertAgentToolCall(ctx, tx, messageID, toolCall); err != nil {
return err
}
}
}
return tx.Commit()
}
func parseAgentSQLiteTime(value string) (time.Time, error) {
value = strings.TrimSpace(value)
if value == "" {
return time.Time{}, nil
}
for _, layout := range []string{
time.RFC3339Nano,
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999Z07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05-07:00",
"2006-01-02 15:04:05Z07:00",
"2006-01-02 15:04:05",
} {
t, err := time.Parse(layout, value)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unsupported time format %q", value)
}
func latestAgentModelForChat(ctx context.Context, db *sql.DB, chatID string) (string, error) {
var modelName string
if err := db.QueryRowContext(ctx, `
SELECT model_name
FROM messages
WHERE chat_id = ? AND archived = 0 AND model_name IS NOT NULL AND model_name != ''
ORDER BY updated_at DESC, id DESC
LIMIT 1
`, chatID).Scan(&modelName); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return "", err
}
return modelName, nil
}
func currentAgentModelSelectExpr(chatAlias string) string {
return fmt.Sprintf(`COALESCE(
NULLIF(%[1]s.model_name, ''),
(
SELECT lm.model_name
FROM messages lm
WHERE lm.chat_id = %[1]s.id AND lm.archived = 0 AND lm.model_name IS NOT NULL AND lm.model_name != ''
ORDER BY lm.updated_at DESC, lm.id DESC
LIMIT 1
)
)`, chatAlias)
}
func messagesContainCompactionSummary(messages []api.Message) bool {
for _, msg := range messages {
if agent.IsCompactionSummary(msg) {
return true
}
}
return false
}
func moveCompactionSummaryBeforeKeptMessages(messages []api.Message) []api.Message {
start := -1
end := -1
for i, msg := range messages {
if agent.IsCompactionToolCall(msg) {
start = i
end = i + 1
if end < len(messages) && agent.IsCompactionToolResult(messages[end]) {
end++
}
}
if start >= 0 {
break
}
}
if start <= 0 || end <= start {
return messages
}
insertAt := leadingSystemMessageCount(messages[:start])
reordered := make([]api.Message, 0, len(messages))
reordered = append(reordered, messages[:insertAt]...)
reordered = append(reordered, messages[start:end]...)
reordered = append(reordered, messages[insertAt:start]...)
reordered = append(reordered, messages[end:]...)
return reordered
}
func insertCompactionSummaryAfterLeadingSystemMessages(messages, summary []api.Message) []api.Message {
insertAt := leadingSystemMessageCount(messages)
reordered := make([]api.Message, 0, len(messages)+len(summary))
reordered = append(reordered, messages[:insertAt]...)
reordered = append(reordered, summary...)
reordered = append(reordered, messages[insertAt:]...)
return reordered
}
func leadingSystemMessageCount(messages []api.Message) int {
for i, msg := range messages {
if msg.Role != "system" {
return i
}
}
return len(messages)
}
type pendingToolCall struct {
key string
call api.ToolCall
}
func repairDanglingToolCalls(messages []api.Message) []api.Message {
var pending []pendingToolCall
pendingByKey := map[string]struct{}{}
repaired := make([]api.Message, 0, len(messages))
flushPending := func() {
for _, pendingCall := range pending {
if _, ok := pendingByKey[pendingCall.key]; !ok {
continue
}
repaired = append(repaired, api.Message{
Role: "tool",
Content: "Tool execution interrupted before a result was recorded.",
ToolName: pendingCall.call.Function.Name,
ToolCallID: pendingCall.call.ID,
})
}
pending = nil
pendingByKey = map[string]struct{}{}
}
for _, msg := range messages {
if len(pendingByKey) > 0 && msg.Role != "tool" {
flushPending()
}
repaired = append(repaired, msg)
switch msg.Role {
case "assistant":
for _, call := range msg.ToolCalls {
key := agentToolCallKey(call, len(pending))
pending = append(pending, pendingToolCall{key: key, call: call})
pendingByKey[key] = struct{}{}
}
case "tool":
if key := msg.ToolCallID; key != "" {
delete(pendingByKey, key)
} else if msg.ToolName != "" {
for _, pendingCall := range pending {
if pendingCall.call.ID == "" && pendingCall.call.Function.Name == msg.ToolName {
delete(pendingByKey, pendingCall.key)
break
}
}
}
if len(pendingByKey) == 0 {
pending = nil
}
}
}
if len(pendingByKey) > 0 {
flushPending()
}
return repaired
}
func agentToolCallKey(call api.ToolCall, index int) string {
if call.ID != "" {
return call.ID
}
return fmt.Sprintf("#%d:%s", index, call.Function.Name)
}
func insertAgentMessage(ctx context.Context, tx *sql.Tx, chatID string, msg api.Message, model string) (int64, error) {
now := time.Now()
modelName := sql.NullString{}
if model != "" {
modelName = sql.NullString{String: model, Valid: true}
}
imagesJSON, err := marshalAgentMessageImages(msg.Images)
if err != nil {
return 0, err
}
result, err := tx.ExecContext(ctx, `
INSERT INTO messages (chat_id, role, content, thinking, images, tool_name, tool_call_id, model_name, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, chatID, msg.Role, msg.Content, msg.Thinking, imagesJSON, msg.ToolName, msg.ToolCallID, modelName, now, now)
if err != nil {
return 0, fmt.Errorf("insert message: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return 0, fmt.Errorf("get message id: %w", err)
}
return id, nil
}
func marshalAgentMessageImages(images []api.ImageData) (string, error) {
if len(images) == 0 {
return "[]", nil
}
data, err := json.Marshal(images)
if err != nil {
return "", fmt.Errorf("marshal message images: %w", err)
}
return string(data), nil
}
func unmarshalAgentMessageImages(value string) ([]api.ImageData, error) {
value = strings.TrimSpace(value)
if value == "" || value == "null" {
return nil, nil
}
var images []api.ImageData
if err := json.Unmarshal([]byte(value), &images); err != nil {
return nil, fmt.Errorf("unmarshal message images: %w", err)
}
return images, nil
}
func insertAgentToolCall(ctx context.Context, tx *sql.Tx, messageID int64, call api.ToolCall) error {
args, err := json.Marshal(call.Function.Arguments)
if err != nil {
return fmt.Errorf("marshal tool arguments: %w", err)
}
_, err = tx.ExecContext(ctx, `
INSERT INTO tool_calls (message_id, type, tool_call_id, function_name, function_arguments)
VALUES (?, ?, ?, ?, ?)
`, messageID, "function", call.ID, call.Function.Name, string(args))
if err != nil {
return fmt.Errorf("insert tool call: %w", err)
}
return nil
}
func getAgentToolCalls(ctx context.Context, db *sql.DB, messageID int64) ([]api.ToolCall, error) {
rows, err := db.QueryContext(ctx, `
SELECT tool_call_id, function_name, function_arguments FROM tool_calls WHERE message_id = ? ORDER BY id ASC
`, messageID)
if err != nil {
return nil, err
}
defer rows.Close()
var calls []api.ToolCall
for rows.Next() {
var id, name, argsJSON string
if err := rows.Scan(&id, &name, &argsJSON); err != nil {
return nil, err
}
var args api.ToolCallFunctionArguments
if err := json.Unmarshal([]byte(argsJSON), &args); err != nil {
return nil, err
}
calls = append(calls, api.ToolCall{
ID: id,
Function: api.ToolCallFunction{
Name: name,
Arguments: args,
},
})
}
return calls, rows.Err()
}
func latestCompactionSummary(ctx context.Context, db *sql.DB, chatID string) (string, error) {
var summary string
if err := db.QueryRowContext(ctx, `
SELECT summary
FROM compactions
WHERE chat_id = ?
ORDER BY id DESC
LIMIT 1
`, chatID).Scan(&summary); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return "", fmt.Errorf("get latest compaction summary: %w", err)
}
return summary, nil
}
func maybeSetAgentTitle(ctx context.Context, tx *sql.Tx, chatID string, content string) error {
title := strings.TrimSpace(content)
if len([]rune(title)) > 64 {
title = string([]rune(title)[:64])
}
_, err := tx.ExecContext(ctx, `
UPDATE chats
SET title = CASE WHEN title = '' THEN ? ELSE title END
WHERE id = ?
`, title, chatID)
return err
}
+466
View File
@@ -0,0 +1,466 @@
package store
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/ollama/ollama/agent"
"github.com/ollama/ollama/api"
)
func newTestAgentStore(t *testing.T) *Store {
t.Helper()
t.Setenv("HOME", t.TempDir())
t.Setenv("LOCALAPPDATA", t.TempDir())
store, err := New(filepath.Join(t.TempDir(), "db.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := store.Close(); err != nil {
t.Fatal(err)
}
})
return store
}
func TestAgentStoreWritesSharedChatRows(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
if err := store.EnsureChat(ctx, "chat-1", ""); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{Role: "user", Content: "hello from cli"}, ""); err != nil {
t.Fatal(err)
}
args := api.NewToolCallFunctionArguments()
args.Set("command", "pwd")
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{
Role: "assistant",
Content: "I'll check.",
ToolCalls: []api.ToolCall{{
ID: "call-1",
Function: api.ToolCallFunction{
Name: "bash",
Arguments: args,
},
}},
}, "llama3.2"); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{
Role: "tool",
Content: "cwd",
ToolName: "bash",
ToolCallID: "call-1",
}, ""); err != nil {
t.Fatal(err)
}
agentChat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(agentChat.Messages) != 3 {
t.Fatalf("messages = %d, want 3", len(agentChat.Messages))
}
if agentChat.Title != "hello from cli" {
t.Fatalf("title = %q, want %q", agentChat.Title, "hello from cli")
}
if got := agentChat.Messages[1].ToolCalls[0].Function.Name; got != "bash" {
t.Fatalf("tool name = %q, want bash", got)
}
if got := agentChat.Messages[1].ToolCalls[0].ID; got != "call-1" {
t.Fatalf("tool call id = %q, want call-1", got)
}
if agentChat.Messages[2].Role != "tool" || agentChat.Messages[2].ToolCallID != "call-1" {
t.Fatalf("tool result = %#v", agentChat.Messages[2])
}
var source string
if err := store.db.conn.QueryRowContext(ctx, `SELECT source FROM chats WHERE id = ?`, "chat-1").Scan(&source); err != nil {
t.Fatal(err)
}
if source != "agent" {
t.Fatalf("source = %q, want agent", source)
}
}
func TestAgentStoreRepairsDanglingToolCallsOnResume(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
args := api.NewToolCallFunctionArguments()
args.Set("command", "pwd")
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{Role: "user", Content: "start"}, ""); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{
Role: "assistant",
ToolCalls: []api.ToolCall{{
ID: "call-1",
Function: api.ToolCallFunction{
Name: "bash",
Arguments: args,
},
}},
}, "llama3.2"); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{Role: "user", Content: "after restart"}, ""); err != nil {
t.Fatal(err)
}
agentChat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(agentChat.Messages) != 4 {
t.Fatalf("messages = %#v, want synthetic tool result inserted", agentChat.Messages)
}
repair := agentChat.Messages[2]
if repair.Role != "tool" || repair.ToolName != "bash" || repair.ToolCallID != "call-1" || !strings.Contains(repair.Content, "interrupted") {
t.Fatalf("repair message = %#v", repair)
}
if agentChat.Messages[3].Role != "user" || agentChat.Messages[3].Content != "after restart" {
t.Fatalf("message after repair = %#v", agentChat.Messages[3])
}
}
func TestAgentStoreRoundTripsToolMetadataAndImages(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
first := api.ImageData([]byte("first image"))
second := api.ImageData([]byte{0, 1, 2, 3})
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{
Role: "tool",
Content: "tool output",
Images: []api.ImageData{first, second},
ToolName: "bash",
ToolCallID: "call-1",
}, ""); err != nil {
t.Fatal(err)
}
chat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(chat.Messages) != 1 {
t.Fatalf("messages = %d, want 1", len(chat.Messages))
}
msg := chat.Messages[0]
if msg.ToolName != "bash" || msg.ToolCallID != "call-1" {
t.Fatalf("tool metadata = %#v", msg)
}
if len(msg.Images) != 2 || !bytes.Equal(msg.Images[0], first) || !bytes.Equal(msg.Images[1], second) {
t.Fatalf("images = %#v, want %#v", msg.Images, []api.ImageData{first, second})
}
updated := api.ImageData([]byte("updated image"))
if err := store.UpdateLastAgentMessage(ctx, "chat-1", api.Message{
Role: "user",
Content: "updated",
Images: []api.ImageData{updated},
}, ""); err != nil {
t.Fatal(err)
}
chat, err = store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(chat.Messages) != 1 || len(chat.Messages[0].Images) != 1 || !bytes.Equal(chat.Messages[0].Images[0], updated) {
t.Fatalf("updated images = %#v", chat.Messages)
}
}
func TestAgentStoreLatestAndListChats(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
if err := store.AppendAgentMessage(ctx, "chat-old", api.Message{Role: "user", Content: "old topic"}, ""); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-old", api.Message{Role: "assistant", Content: "old answer"}, "llama3.2"); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-new", api.Message{Role: "user", Content: "new topic"}, ""); err != nil {
t.Fatal(err)
}
if err := store.AppendAgentMessage(ctx, "chat-new", api.Message{Role: "assistant", Content: "new answer"}, "qwen3"); err != nil {
t.Fatal(err)
}
chat, err := store.LatestChat(ctx)
if err != nil {
t.Fatal(err)
}
if chat.ID != "chat-new" || chat.Model != "qwen3" {
t.Fatalf("latest chat = %#v, want chat-new with qwen3", chat)
}
chat, err = store.LatestChatForModel(ctx, "llama3.2")
if err != nil {
t.Fatal(err)
}
if chat.ID != "chat-old" {
t.Fatalf("llama latest chat = %q, want chat-old", chat.ID)
}
if _, err := store.LatestChatForModel(ctx, "missing"); !errors.Is(err, sql.ErrNoRows) {
t.Fatalf("missing model err = %v, want sql.ErrNoRows", err)
}
summaries, err := store.ListChats(ctx, 10)
if err != nil {
t.Fatal(err)
}
if len(summaries) != 2 {
t.Fatalf("summaries = %d, want 2", len(summaries))
}
if summaries[0].ID != "chat-new" || summaries[0].Title != "new topic" || summaries[0].Model != "qwen3" {
t.Fatalf("newest summary = %#v", summaries[0])
}
if summaries[1].ID != "chat-old" || summaries[1].Model != "llama3.2" {
t.Fatalf("older summary = %#v", summaries[1])
}
future := time.Date(2099, 1, 1, 12, 0, 0, 0, time.UTC)
if _, err := store.db.conn.ExecContext(ctx, `
INSERT INTO chats (id, title, created_at)
VALUES (?, ?, ?)
`, "chat-archived", "archived topic", future); err != nil {
t.Fatal(err)
}
if _, err := store.db.conn.ExecContext(ctx, `
INSERT INTO messages (chat_id, role, content, model_name, created_at, updated_at, archived)
VALUES (?, ?, ?, ?, ?, ?, 1)
`, "chat-archived", "assistant", "archived answer", "ghost-model", future, future); err != nil {
t.Fatal(err)
}
chat, err = store.LatestChat(ctx)
if err != nil {
t.Fatal(err)
}
if chat.ID != "chat-new" {
t.Fatalf("latest chat = %q, want chat-new after archived future row", chat.ID)
}
if _, err := store.LatestChatForModel(ctx, "ghost-model"); !errors.Is(err, sql.ErrNoRows) {
t.Fatalf("archived model err = %v, want sql.ErrNoRows", err)
}
summaries, err = store.ListChats(ctx, 10)
if err != nil {
t.Fatal(err)
}
if len(summaries) != 2 {
t.Fatalf("summaries = %d, want archived-only chat hidden", len(summaries))
}
}
func TestAgentStoreUpdateLastMessageIgnoresArchivedRows(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{Role: "assistant", Content: "active"}, "llama3.2"); err != nil {
t.Fatal(err)
}
future := time.Date(2099, 1, 1, 12, 0, 0, 0, time.UTC)
if _, err := store.db.conn.ExecContext(ctx, `
INSERT INTO messages (chat_id, role, content, created_at, updated_at, archived)
VALUES (?, ?, ?, ?, ?, 1)
`, "chat-1", "assistant", "archived", future, future); err != nil {
t.Fatal(err)
}
if err := store.UpdateLastAgentMessage(ctx, "chat-1", api.Message{Role: "assistant", Content: "active updated"}, "llama3.2"); err != nil {
t.Fatal(err)
}
chat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(chat.Messages) != 1 || chat.Messages[0].Content != "active updated" {
t.Fatalf("active messages = %#v, want updated active message only", chat.Messages)
}
var archivedContent string
if err := store.db.conn.QueryRowContext(ctx, `
SELECT content
FROM messages
WHERE chat_id = ? AND archived = 1
ORDER BY id DESC
LIMIT 1
`, "chat-1").Scan(&archivedContent); err != nil {
t.Fatal(err)
}
if archivedContent != "archived" {
t.Fatalf("archived content = %q, want archived", archivedContent)
}
}
func TestAgentStoreListUserMessages(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
for _, msg := range []api.Message{
{Role: "user", Content: "old prompt"},
{Role: "assistant", Content: "not user"},
{Role: "user", Content: "middle prompt"},
{Role: "user", Content: " "},
{Role: "user", Content: agent.CompactionSummaryMessagePrefix + "old context"},
{Role: "user", Content: "new prompt"},
} {
if err := store.AppendAgentMessage(ctx, "chat-1", msg, ""); err != nil {
t.Fatal(err)
}
}
if _, err := store.db.conn.ExecContext(ctx, `UPDATE messages SET archived = 1 WHERE content = ?`, "middle prompt"); err != nil {
t.Fatal(err)
}
messages, err := store.ListUserMessages(ctx, 2)
if err != nil {
t.Fatal(err)
}
want := []string{"old prompt", "new prompt"}
if !slices.Equal(messages, want) {
t.Fatalf("messages = %#v, want %#v", messages, want)
}
}
func TestAgentStoreRepairsPreAgentSchema(t *testing.T) {
path := filepath.Join(t.TempDir(), "db.sqlite")
db, err := sql.Open("sqlite3", path)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`
CREATE TABLE chats (
id TEXT PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
browser_state TEXT
);
CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL DEFAULT '',
thinking TEXT NOT NULL DEFAULT '',
stream BOOLEAN NOT NULL DEFAULT 0,
model_name TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE TABLE tool_calls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER NOT NULL,
type TEXT NOT NULL,
function_name TEXT NOT NULL,
function_arguments TEXT NOT NULL,
function_result TEXT,
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
);
`); err != nil {
t.Fatal(err)
}
if err := db.Close(); err != nil {
t.Fatal(err)
}
store, err := New(path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := store.Close(); err != nil {
t.Fatal(err)
}
})
ctx := context.Background()
if err := store.AppendAgentMessage(ctx, "chat-1", api.Message{Role: "user", Content: "hello"}, "llama3.2"); err != nil {
t.Fatal(err)
}
chat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if chat.Model != "llama3.2" || len(chat.Messages) != 1 || chat.Messages[0].Content != "hello" {
t.Fatalf("chat = %#v", chat)
}
var source string
if err := store.db.conn.QueryRowContext(ctx, `SELECT source FROM chats WHERE id = ?`, "chat-1").Scan(&source); err != nil {
t.Fatal(err)
}
if source != "agent" {
t.Fatalf("source = %q, want agent", source)
}
}
func TestAgentStoreArchivesCompactedMessages(t *testing.T) {
store := newTestAgentStore(t)
ctx := context.Background()
for _, msg := range []api.Message{
{Role: "user", Content: "old request"},
{Role: "assistant", Content: "old answer"},
{Role: "user", Content: "recent request"},
} {
if err := store.AppendAgentMessage(ctx, "chat-1", msg, ""); err != nil {
t.Fatal(err)
}
}
if err := store.ArchiveForCompaction(ctx, "chat-1", 1, "summary", true); err != nil {
t.Fatal(err)
}
agentChat, err := store.AgentChat(ctx, "chat-1")
if err != nil {
t.Fatal(err)
}
if len(agentChat.Messages) != 3 {
t.Fatalf("active messages = %#v, want compaction pair plus latest request", agentChat.Messages)
}
if agentChat.Messages[0].Role != "assistant" || len(agentChat.Messages[0].ToolCalls) != 1 || agentChat.Messages[0].ToolCalls[0].Function.Name != agent.CompactionToolName {
t.Fatalf("summary tool call = %#v", agentChat.Messages[0])
}
content := agentChat.Messages[1].Content
if !strings.Contains(content, agent.CompactionContinueInstruction) {
t.Fatalf("summary tool result missing continuation instruction: %q", content)
}
if agentChat.Messages[2].Content != "recent request" {
t.Fatalf("kept message = %#v, want recent request", agentChat.Messages[2])
}
var idsJSON string
if err := store.db.conn.QueryRowContext(ctx, `
SELECT archived_message_ids FROM compactions WHERE chat_id = ?
`, "chat-1").Scan(&idsJSON); err != nil {
t.Fatal(err)
}
var ids []int64
if err := json.Unmarshal([]byte(idsJSON), &ids); err != nil {
t.Fatal(err)
}
if len(ids) != 2 {
t.Fatalf("archived ids = %v, want 2 ids", ids)
}
}
-8
View File
@@ -1,8 +0,0 @@
---
name: release-notes
description: Draft concise release notes.
---
# Release notes
Use short bullets.
+91
View File
@@ -0,0 +1,91 @@
package agent
import (
"fmt"
"sort"
"strconv"
"strings"
)
const maxToolInvocationCommandRunes = 100
// ToolDisplayName returns the user-facing label for a tool name.
func ToolDisplayName(name string) string {
switch name {
case "web_search":
return "Web Search"
case "web_fetch":
return "Web Fetch"
case "bash":
return "Bash"
case "powershell":
return "PowerShell"
case "read":
return "Read"
case "list":
return "List"
case "edit":
return "Edit"
case "skill":
return "Skill"
default:
if name == "" {
return "Tool"
}
return name
}
}
// ToolInvocationLabel returns a compact user-facing label for a tool call.
func ToolInvocationLabel(name string, args map[string]any) string {
displayName := ToolDisplayName(name)
for _, key := range []string{"query", "url", "command", "path", "name"} {
if value, ok := displayStringArg(args, key); ok {
if IsShellToolName(name) && key == "command" {
value = truncateDisplayRunes(value, maxToolInvocationCommandRunes)
}
return fmt.Sprintf("%s(%s)", displayName, strconv.Quote(value))
}
}
if len(args) == 0 {
return displayName
}
return fmt.Sprintf("%s(%s)", displayName, formatDisplayArgs(args))
}
// IsShellToolName reports whether name identifies a platform shell tool.
func IsShellToolName(name string) bool {
return name == "bash" || name == "powershell"
}
func displayStringArg(args map[string]any, key string) (string, bool) {
value, ok := args[key].(string)
if !ok || strings.TrimSpace(value) == "" {
return "", false
}
return value, true
}
func truncateDisplayRunes(value string, limit int) string {
runes := []rune(value)
if limit <= 0 || len(runes) <= limit {
return value
}
return string(runes[:limit]) + "..."
}
func formatDisplayArgs(args map[string]any) string {
keys := make([]string, 0, len(args))
for key := range args {
keys = append(keys, key)
}
sort.Strings(keys)
parts := make([]string, 0, len(keys))
for _, key := range keys {
value := fmt.Sprintf("%v", args[key])
value = truncateDisplayRunes(value, 100)
parts = append(parts, fmt.Sprintf("%s=%s", key, strconv.Quote(value)))
}
return strings.Join(parts, ", ")
}
+33
View File
@@ -0,0 +1,33 @@
package agent
import (
"strings"
"testing"
)
func TestToolInvocationLabelTruncatesLongBashCommand(t *testing.T) {
command := strings.Repeat("a", 101)
label := ToolInvocationLabel("bash", map[string]any{"command": command})
want := `Bash("` + strings.Repeat("a", 100) + `...")`
if label != want {
t.Fatalf("label = %q, want %q", label, want)
}
}
func TestToolInvocationLabelCountsRunes(t *testing.T) {
command := strings.Repeat("界", 101)
label := ToolInvocationLabel("bash", map[string]any{"command": command})
want := `Bash("` + strings.Repeat("界", 100) + `...")`
if label != want {
t.Fatalf("label = %q, want %q", label, want)
}
}
func TestToolInvocationLabelTruncatesLongPowerShellCommand(t *testing.T) {
command := strings.Repeat("a", 101)
label := ToolInvocationLabel("powershell", map[string]any{"command": command})
want := `PowerShell("` + strings.Repeat("a", 100) + `...")`
if label != want {
t.Fatalf("label = %q, want %q", label, want)
}
}
+23 -272
View File
@@ -2,7 +2,6 @@ package tools
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
@@ -10,7 +9,6 @@ import (
"runtime"
"strings"
"time"
"unicode/utf8"
"github.com/ollama/ollama/agent"
"github.com/ollama/ollama/api"
@@ -18,12 +16,15 @@ import (
const (
bashTimeout = 3 * time.Minute
bashWaitDelay = 1 * time.Second
maxBashOutputBytes = 60_000
)
type Bash struct{}
func NewBash() *Bash {
return &Bash{}
}
func (b *Bash) Name() string {
return shellToolName()
}
@@ -53,31 +54,11 @@ func (b *Bash) RequiresApproval(map[string]any) bool {
return true
}
// ApprovalScope scopes shell approval to the exact, trimmed command string
// using a NUL separator: "<tool>\x00<command>". "Always allow this command"
// matches ONLY that precise string — any whitespace, quoting, or casing
// variant re-prompts. The NUL separator is safe because a shell command
// string cannot contain a literal NUL.
func (b *Bash) ApprovalScope(args map[string]any) string {
name := b.Name()
if command, ok := args["command"].(string); ok {
command = strings.TrimSpace(command)
if command != "" {
return name + "\x00" + command
}
}
return name
}
func (b *Bash) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
// TODO: use shared agent.RequiredStringArg for the "command" parameter (see agent package cleanup plan).
command, ok := args["command"].(string)
if !ok || strings.TrimSpace(command) == "" {
return agent.ToolResult{}, fmt.Errorf("command parameter is required")
}
if err := rejectUnsafeShellCommand(command); err != nil {
return agent.ToolResult{}, err
}
ctx, cancel := context.WithTimeout(ctx, bashTimeout)
defer cancel()
@@ -91,7 +72,6 @@ func (b *Bash) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
defer os.Remove(cwdPath)
cmd := newBashCommand(ctx, command, cwdPath)
cmd.WaitDelay = bashWaitDelay
cmd.Cancel = func() error {
return killBashCommand(cmd)
}
@@ -122,17 +102,13 @@ func (b *Bash) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return agent.ToolResult{Content: bashContentWithError(sb.String(), "Error: command timed out after "+bashTimeout.String()), WorkingDir: finalWorkingDir}, nil
return agent.ToolResult{Content: sb.String() + "\n\nError: command timed out after " + bashTimeout.String(), WorkingDir: finalWorkingDir}, nil
}
if ctx.Err() == context.Canceled {
return agent.ToolResult{Content: bashContentWithError(sb.String(), "Error: command was canceled"), WorkingDir: finalWorkingDir}, nil
}
if errors.Is(err, exec.ErrWaitDelay) {
_ = killBashCommand(cmd)
return agent.ToolResult{Content: bashContentWithError(sb.String(), "Error: command output pipes did not close after "+bashWaitDelay.String()), WorkingDir: finalWorkingDir}, nil
return agent.ToolResult{Content: sb.String() + "\n\nError: command was canceled", WorkingDir: finalWorkingDir}, nil
}
if exitErr, ok := err.(*exec.ExitError); ok {
return agent.ToolResult{Content: bashContentWithError(sb.String(), fmt.Sprintf("Exit code: %d", exitErr.ExitCode())), WorkingDir: finalWorkingDir}, nil
return agent.ToolResult{Content: sb.String() + fmt.Sprintf("\n\nExit code: %d", exitErr.ExitCode()), WorkingDir: finalWorkingDir}, nil
}
return agent.ToolResult{Content: sb.String(), WorkingDir: finalWorkingDir}, fmt.Errorf("executing command: %w", err)
}
@@ -143,225 +119,6 @@ func (b *Bash) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
return agent.ToolResult{Content: sb.String(), WorkingDir: finalWorkingDir}, nil
}
func bashContentWithError(content, msg string) string {
if content == "" {
return msg
}
return content + "\n\n" + msg
}
// rejectUnsafeShellCommand applies a best-effort blocklist for obviously
// destructive or credential-exfiltrating commands. It is defense-in-depth
// ONLY: the interactive approval prompt is the real security control, and
// this check must not be relied upon as a sandbox. Sophisticated or novel
// dangerous commands (e.g. find / -delete, dd, fork bombs, custom binaries)
// are NOT caught here and will simply be routed through approval like any
// other command. Keep the approval prompt as the gate.
func rejectUnsafeShellCommand(command string) error {
switch {
case hasUnsafeRecursiveDelete(command):
return fmt.Errorf("refusing to run unsafe command: recursive delete target is too broad")
case readsCredentialPath(command):
return fmt.Errorf("refusing to run unsafe command: credential file reads are not allowed")
default:
return nil
}
}
func hasUnsafeRecursiveDelete(command string) bool {
// Check each command segment independently. shellSafetyText flattens
// separators (; & | newlines) to spaces, which would otherwise let the
// rm target scan bleed across command boundaries — e.g.
// "rm -rf build && echo ~/.ssh/config" flattened to one token stream
// would treat the unrelated ~/.ssh/config (a ~/-prefixed "unsafe
// target") as an rm argument. Splitting on separators first restores
// command boundaries while still catching multi-target single commands
// like "rm -rf build /etc".
for _, segment := range shellSegments(command) {
fields := shellSafetyFields(segment)
for i, field := range fields {
if isRMCommand(field) && rmCommandDeletesUnsafeTarget(fields[i+1:]) {
return true
}
if isPowerShellDeleteCommand(field) && powerShellDeleteCommandDeletesUnsafeTarget(fields[i+1:]) {
return true
}
}
}
return false
}
// shellSegments splits a command on shell control operators (;, &, |, &&,
// ||) and newlines, returning the individual command segments. It operates on
// the lowercased raw command before quote/separator normalization so that
// command boundaries are preserved for per-segment checks. Subshell parens are
// intentionally NOT treated as separators: splitting on them would fragment
// command substitutions like "rm -rf $(echo /)" into "rm -rf $" and "echo /",
// hiding the destructive "/" target from the per-segment scan. Empty segments
// are dropped.
func shellSegments(command string) []string {
command = strings.ToLower(command)
var segments []string
for _, segment := range strings.FieldsFunc(command, func(r rune) bool {
switch r {
case ';', '&', '|', '\n', '\r':
return true
}
return false
}) {
if segment = strings.TrimSpace(segment); segment != "" {
segments = append(segments, segment)
}
}
return segments
}
func rmCommandDeletesUnsafeTarget(fields []string) bool {
var flags string
for _, field := range fields {
if field == "--" {
continue
}
if strings.HasPrefix(field, "-") {
flags += field
continue
}
if strings.Contains(flags, "r") && strings.Contains(flags, "f") && isUnsafeDeleteTarget(field) {
return true
}
}
return false
}
func powerShellDeleteCommandDeletesUnsafeTarget(fields []string) bool {
var recurse, force bool
var targets []string
for _, field := range fields {
switch field {
case "-r", "-recurse", "-recursive":
recurse = true
case "-f", "-force":
force = true
default:
if !strings.HasPrefix(field, "-") {
targets = append(targets, field)
}
}
}
if !recurse || !force {
return false
}
for _, target := range targets {
if isUnsafeDeleteTarget(target) {
return true
}
}
return false
}
func readsCredentialPath(command string) bool {
fields := shellSafetyFields(command)
if !hasCredentialReadVerb(fields) {
return false
}
normalized := shellSafetyText(command)
for _, fragment := range []string{
"/.ssh/id_rsa",
"/.ssh/id_dsa",
"/.ssh/id_ecdsa",
"/.ssh/id_ed25519",
"/.ssh/config",
"/.ssh/known_hosts",
"/.aws/credentials",
"/.aws/config",
"/.config/gcloud/application_default_credentials.json",
"/.kube/config",
"/.netrc",
"/.npmrc",
"/.docker/config.json",
"/.config/gh/hosts.yml",
"/.gnupg/",
"/etc/shadow",
} {
if strings.Contains(normalized, fragment) {
return true
}
}
return false
}
func hasCredentialReadVerb(fields []string) bool {
for _, field := range fields {
switch field {
case "cat", "less", "more", "head", "tail", "type", "get-content", "gc", "select-string", "grep", "rg", "sed", "awk":
return true
case "env", "printenv":
return true
}
}
return false
}
func isRMCommand(field string) bool {
return field == "rm" || strings.HasSuffix(field, "/rm")
}
func isPowerShellDeleteCommand(field string) bool {
switch field {
case "remove-item", "del", "erase", "rd", "rmdir":
return true
default:
return false
}
}
func isUnsafeDeleteTarget(target string) bool {
if target == "." || target == "./" || target == "*" {
return true
}
if target == "/*" {
return true
}
target = strings.TrimSuffix(target, "/*")
for _, prefix := range []string{"~/", "$home/", "${home}/", "$env:home/", "$env:userprofile/", "%userprofile%/"} {
if strings.HasPrefix(target, prefix) {
return true
}
}
for _, prefix := range []string{"/etc/", "/bin/", "/sbin/", "/usr/", "/var/", "/lib/", "/library/", "/system/", "/applications/", "c:/windows/", "c:/program files/"} {
if strings.HasPrefix(target, prefix) {
return true
}
}
for _, exact := range []string{"/", "~", "$home", "${home}", "$env:home", "$env:userprofile", "%userprofile%", "c:", "c:/", "/etc", "/bin", "/sbin", "/usr", "/var", "/lib", "/library", "/system", "/applications", "c:/windows", "c:/program files"} {
if target == exact {
return true
}
}
return false
}
func shellSafetyFields(command string) []string {
return strings.Fields(shellSafetyText(command))
}
func shellSafetyText(command string) string {
command = strings.ToLower(command)
return strings.NewReplacer(
"\\", "/",
"\n", " ",
"\t", " ",
";", " ",
"&", " ",
"|", " ",
"(", " ",
")", " ",
"\"", "",
"'", "",
"`", "",
).Replace(command)
}
func readFinalWorkingDir(path string) string {
content, err := os.ReadFile(path)
if err != nil {
@@ -395,9 +152,13 @@ func isASCIIAlpha(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
}
func shellQuote(value string) string {
return "'" + strings.ReplaceAll(value, "'", "'\\''") + "'"
}
type boundedOutput struct {
Limit int
buf []byte
buf strings.Builder
omitted int
}
@@ -406,45 +167,35 @@ func (b *boundedOutput) Write(p []byte) (int, error) {
b.omitted += len(p)
return len(p), nil
}
remaining := b.Limit - len(b.buf)
remaining := b.Limit - b.buf.Len()
if remaining <= 0 {
b.omitted += len(p)
return len(p), nil
}
if len(p) <= remaining {
b.buf = append(b.buf, p...)
b.buf.Write(p)
return len(p), nil
}
writeLen := utf8SafePrefixLen(p[:remaining])
b.buf = append(b.buf, p[:writeLen]...)
b.omitted += len(p) - writeLen
b.buf.Write(p[:remaining])
b.omitted += len(p) - remaining
return len(p), nil
}
func (b *boundedOutput) Len() int {
return len(b.buf) + b.omitted
return b.buf.Len() + b.omitted
}
func (b *boundedOutput) String(label string) string {
safeLen := utf8SafePrefixLen(b.buf)
content := string(b.buf[:safeLen])
omitted := b.omitted + len(b.buf) - safeLen
if omitted == 0 {
content := b.buf.String()
if b.omitted == 0 {
return content
}
return content + agent.TruncMarker(label, safeLen, 0, omitted, false, "")
return content + fmt.Sprintf("\n\n[%s truncated: omitted ~%d tokens]", label, approximateTokensFromBytes(b.omitted))
}
func utf8SafePrefixLen(p []byte) int {
if len(p) == 0 {
func approximateTokensFromBytes(n int) int {
if n <= 0 {
return 0
}
for i := 0; i < len(p); {
r, size := utf8.DecodeRune(p[i:])
if r == utf8.RuneError && size == 1 {
return i
}
i += size
}
return len(p)
return max(1, (n+3)/4)
}
+4 -134
View File
@@ -7,7 +7,6 @@ import (
"runtime"
"strings"
"testing"
"unicode/utf8"
"github.com/ollama/ollama/agent"
)
@@ -19,7 +18,7 @@ func TestBashReportsFinalWorkingDir(t *testing.T) {
t.Fatal(err)
}
result, err := (&Bash{}).Execute(context.Background(), agent.ToolContext{WorkingDir: root}, map[string]any{
result, err := NewBash().Execute(context.Background(), agent.ToolContext{WorkingDir: root}, map[string]any{
"command": shellTestCommand("cd sub && pwd", "Set-Location sub; Get-Location"),
})
if err != nil {
@@ -38,13 +37,13 @@ func TestBashReportsFinalWorkingDir(t *testing.T) {
}
func TestBashBoundsOutputWhileRunning(t *testing.T) {
result, err := (&Bash{}).Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
result, err := NewBash().Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"command": shellTestCommand("yes x | head -c 70000", "[Console]::Out.Write(('x' * 70000))"),
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(result.Content, "[stdout truncated: showing first ~") || !strings.Contains(result.Content, "omitted ~") || !strings.Contains(result.Content, " tokens.]") {
if !strings.Contains(result.Content, "[stdout truncated: omitted ~") || !strings.Contains(result.Content, " tokens]") {
t.Fatalf("content = %q, want stdout truncation marker", result.Content)
}
if count, want := strings.Count(result.Content, "x"), shellTestCapturedXCount(); count != want {
@@ -55,83 +54,11 @@ func TestBashBoundsOutputWhileRunning(t *testing.T) {
}
}
func TestBoundedOutputTruncatesAtUTF8Boundary(t *testing.T) {
var out boundedOutput
out.Limit = len([]byte("abc")) + 1
if _, err := out.Write([]byte("abcédef")); err != nil {
t.Fatal(err)
}
content := out.String("stdout")
if !utf8.ValidString(content) {
t.Fatalf("content is not valid UTF-8: %q", content)
}
if strings.ContainsRune(content, utf8.RuneError) {
t.Fatalf("content contains replacement rune: %q", content)
}
if !strings.HasPrefix(content, "abc\n\n[stdout truncated:") {
t.Fatalf("content = %q, want complete ASCII prefix and truncation marker", content)
}
}
func TestBoundedOutputKeepsCompleteUTF8AtBoundary(t *testing.T) {
var out boundedOutput
out.Limit = len([]byte("abcé"))
if _, err := out.Write([]byte("abcédef")); err != nil {
t.Fatal(err)
}
if content := out.String("stdout"); !strings.HasPrefix(content, "abcé\n\n[stdout truncated:") {
t.Fatalf("content = %q, want complete UTF-8 prefix", content)
}
}
func TestBoundedOutputTrimsTrailingPartialUTF8(t *testing.T) {
var out boundedOutput
out.Limit = 4
if _, err := out.Write([]byte{'a', 'b', 'c', 0xc3}); err != nil {
t.Fatal(err)
}
if _, err := out.Write([]byte{0xa9}); err != nil {
t.Fatal(err)
}
if content := out.String("stdout"); !utf8.ValidString(content) || !strings.HasPrefix(content, "abc\n\n[stdout truncated:") {
t.Fatalf("content = %q, want valid UTF-8 with partial suffix trimmed", content)
}
}
func TestUTF8SafePrefixRejectsMalformedLeadByte(t *testing.T) {
input := []byte{'a', 0xc0, 0x80, 'b'}
if got := utf8SafePrefixLen(input); got != 1 {
t.Fatalf("safe prefix length = %d, want 1", got)
}
}
func TestBoundedOutputDropsMalformedUTF8(t *testing.T) {
var out boundedOutput
out.Limit = 4
if _, err := out.Write([]byte{'a', 0xc0, 0x80, 'b'}); err != nil {
t.Fatal(err)
}
content := out.String("stdout")
if !utf8.ValidString(content) {
t.Fatalf("content is not valid UTF-8: %q", content)
}
if strings.ContainsRune(content, utf8.RuneError) {
t.Fatalf("content contains replacement rune: %q", content)
}
if !strings.HasPrefix(content, "a\n\n[stdout truncated:") {
t.Fatalf("content = %q, want valid prefix and truncation marker", content)
}
}
func TestBashReportsCanceledCommand(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
result, err := (&Bash{}).Execute(ctx, agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
result, err := NewBash().Execute(ctx, agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"command": shellTestCommand("sleep 10", "Start-Sleep -Seconds 10"),
})
if err != nil {
@@ -145,63 +72,6 @@ func TestBashReportsCanceledCommand(t *testing.T) {
}
}
func TestRejectUnsafeShellCommand(t *testing.T) {
tests := []struct {
name string
command string
wantErr bool
}{
{name: "rm root", command: "rm -rf /", wantErr: true},
{name: "sudo rm root", command: "sudo rm -rf -- /", wantErr: true},
{name: "rm home", command: "rm -fr $HOME", wantErr: true},
{name: "rm root wildcard", command: "rm -rf /*", wantErr: true},
{name: "rm system subdir", command: "rm -rf /etc/ssh", wantErr: true},
{name: "rm cwd", command: "rm -rf .", wantErr: true},
{name: "powershell remove root", command: `Remove-Item -Recurse -Force C:\`, wantErr: true},
{name: "powershell remove system subdir", command: `Remove-Item -Recurse -Force C:\Windows\Temp`, wantErr: true},
{name: "ssh private key", command: "cat ~/.ssh/id_rsa", wantErr: true},
{name: "aws credentials", command: "Get-Content $HOME/.aws/credentials", wantErr: true},
{name: "shadow", command: "head /etc/shadow", wantErr: true},
{name: "netrc", command: "cat ~/.netrc", wantErr: true},
{name: "docker config", command: "cat ~/.docker/config.json", wantErr: true},
{name: "gnupg dir", command: "cat ~/.gnupg/private-keys-v1.d/key", wantErr: true},
{name: "gh hosts", command: "cat ~/.config/gh/hosts.yml", wantErr: true},
{name: "ssh config", command: "cat ~/.ssh/config", wantErr: true},
{name: "printenv dump", command: "printenv", wantErr: false},
{name: "delete build dir", command: "rm -rf build", wantErr: false},
{name: "read project file", command: "cat README.md", wantErr: false},
{name: "mention key text", command: "rg id_rsa docs", wantErr: false},
{name: "env example", command: "cat .env.example", wantErr: false},
{name: "rm build then unrelated tilde path", command: "rm -rf build && echo ~/.ssh/config", wantErr: false},
{name: "rm build then unrelated slash path", command: "rm -rf build; cat /etc/passwd", wantErr: false},
{name: "rm build then unrelated star glob", command: "rm -rf build && ls *.go", wantErr: false},
{name: "rm multiple targets one unsafe", command: "rm -rf build /etc", wantErr: true},
{name: "rm unsafe then safe piped", command: "rm -rf / | tee log", wantErr: true},
{name: "rm unsafe via command substitution", command: "rm -rf $(echo /)", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := rejectUnsafeShellCommand(tt.command)
if tt.wantErr && err == nil {
t.Fatal("expected unsafe command to be rejected")
}
if !tt.wantErr && err != nil {
t.Fatalf("command rejected: %v", err)
}
})
}
}
func TestBashRejectsUnsafeCommandBeforeExecution(t *testing.T) {
_, err := (&Bash{}).Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"command": "rm -rf /",
})
if err == nil || !strings.Contains(err.Error(), "refusing to run unsafe command") {
t.Fatalf("err = %v, want unsafe command rejection", err)
}
}
func shellTestCommand(unix, windows string) string {
if runtime.GOOS == "windows" {
return windows
-5
View File
@@ -5,7 +5,6 @@ package tools
import (
"context"
"os/exec"
"strings"
"syscall"
)
@@ -28,10 +27,6 @@ func newBashCommand(ctx context.Context, command, cwdPath string) *exec.Cmd {
return cmd
}
func shellQuote(value string) string {
return "'" + strings.ReplaceAll(value, "'", "'\\''") + "'"
}
func configureBashCommand(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
-24
View File
@@ -3,13 +3,8 @@
package tools
import (
"context"
"os/exec"
"strings"
"testing"
"time"
"github.com/ollama/ollama/agent"
)
func TestConfigureBashCommandSetsProcessGroup(t *testing.T) {
@@ -19,22 +14,3 @@ func TestConfigureBashCommandSetsProcessGroup(t *testing.T) {
t.Fatalf("configureBashCommand should start bash in a new process group")
}
}
func TestBashWaitDelayBoundsBackgroundOutputPipe(t *testing.T) {
start := time.Now()
result, err := (&Bash{}).Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"command": "sleep 5 & echo done",
})
if err != nil {
t.Fatal(err)
}
if elapsed := time.Since(start); elapsed > bashWaitDelay+2*time.Second {
t.Fatalf("command elapsed = %s, want bounded near %s", elapsed, bashWaitDelay)
}
if !strings.Contains(result.Content, "done") {
t.Fatalf("content = %q, want command output", result.Content)
}
if !strings.Contains(result.Content, "output pipes did not close") {
t.Fatalf("content = %q, want wait delay message", result.Content)
}
}
+1 -1
View File
@@ -62,7 +62,7 @@ func powerShellCommandScript(command, cwdPath string) string {
"} finally {",
" try { [System.IO.File]::WriteAllText(" + cwdPath + ", (Get-Location).ProviderPath, [System.Text.Encoding]::UTF8) } catch {}",
"}",
"} | Out-String -Stream -Width 4096",
"} | Out-String -Stream",
"exit $__ollama_status",
}, "\n")
}
-15
View File
@@ -1,15 +0,0 @@
//go:build windows
package tools
import (
"strings"
"testing"
)
func TestPowerShellCommandScriptUsesWideOutString(t *testing.T) {
script := powerShellCommandScript("Get-ChildItem", `C:\cwd.txt`)
if !strings.Contains(script, "Out-String -Stream -Width 4096") {
t.Fatalf("script = %q, want explicit Out-String width", script)
}
}
+164 -298
View File
@@ -2,14 +2,11 @@ package tools
import (
"bufio"
"cmp"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
@@ -23,6 +20,10 @@ const (
type Read struct{}
func NewRead() *Read {
return &Read{}
}
func (r *Read) Name() string {
return "read"
}
@@ -37,14 +38,22 @@ func (r *Read) Schema() api.ToolFunction {
Type: api.PropertyType{"string"},
Description: "Path to the file to read, relative to the working directory.",
})
props.Set("start", api.ToolProperty{
props.Set("start_line", api.ToolProperty{
Type: api.PropertyType{"integer"},
Description: "Optional 1-based line to start reading from.",
})
props.Set("end", api.ToolProperty{
props.Set("end_line", api.ToolProperty{
Type: api.PropertyType{"integer"},
Description: "Optional 1-based inclusive line to stop reading at.",
})
props.Set("line_count", api.ToolProperty{
Type: api.PropertyType{"integer"},
Description: "Optional maximum number of lines to read, starting at start_line or line 1.",
})
props.Set("line_range", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: `Optional 1-based inclusive range like "10-40", "10:40", "10..40", "10-", or "10".`,
})
return api.ToolFunction{
Name: r.Name(),
Description: r.Description(),
@@ -56,18 +65,13 @@ func (r *Read) Schema() api.ToolFunction {
}
}
func (r *Read) RequiresApproval(map[string]any) bool {
return true
}
func (r *Read) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
// TODO: use shared agent.RequiredStringArg / agent.OptionalIntArg for args (see agent package cleanup plan).
path, ok := args["path"].(string)
if !ok || strings.TrimSpace(path) == "" {
return agent.ToolResult{}, fmt.Errorf("path parameter is required")
}
file, info, err := openRegularFile(toolCtx.WorkingDir, path, true)
file, info, err := openRegularFile(toolCtx.WorkingDir, path)
if err != nil {
return agent.ToolResult{}, err
}
@@ -92,7 +96,7 @@ func (r *Read) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
content, err = readLineSelection(file, selection)
} else {
var contentBytes []byte
contentBytes, err = readAllWithinLimit(file, maxReadBytes)
contentBytes, err = io.ReadAll(file)
content = string(contentBytes)
}
if err != nil {
@@ -103,42 +107,35 @@ func (r *Read) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
type Edit struct{}
func NewEdit() *Edit {
return &Edit{}
}
func (e *Edit) Name() string {
return "edit"
}
func (e *Edit) Description() string {
return "Edit a text file in the current working directory by replacing exact text. Pass multiple edits to change separate parts of the file in one call."
return "Edit a text file in the current working directory by replacing exact text."
}
func (e *Edit) Schema() api.ToolFunction {
editProps := api.NewToolPropertiesMap()
editProps.Set("old_text", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Exact text for one targeted replacement. Must match the original file exactly once and must not overlap with any other edit's old_text.",
})
editProps.Set("new_text", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Replacement text for this targeted edit.",
})
props := api.NewToolPropertiesMap()
props.Set("path", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Path to the file to edit, relative to the working directory.",
})
props.Set("edits", api.ToolProperty{
Type: api.PropertyType{"array"},
Items: api.ToolProperty{
Type: api.PropertyType{"object"},
Properties: editProps,
Required: []string{"old_text", "new_text"},
},
Description: "One or more exact-text replacements. Each is matched against the original file, not against the output of earlier edits. Keep old_text as small as possible while still unique in the file; merge changes to the same or adjacent lines into a single edit.",
props.Set("old_text", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Exact text to replace.",
})
props.Set("new_text", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Replacement text.",
})
props.Set("replace_all", api.ToolProperty{
Type: api.PropertyType{"boolean"},
Description: "Replace every occurrence. Defaults to false; only applies when a single edit is provided.",
Description: "Replace every occurrence. Defaults to false and requires old_text to match exactly once.",
})
return api.ToolFunction{
Name: e.Name(),
@@ -146,7 +143,7 @@ func (e *Edit) Schema() api.ToolFunction {
Parameters: api.ToolFunctionParameters{
Type: "object",
Properties: props,
Required: []string{"path", "edits"},
Required: []string{"path", "old_text", "new_text"},
},
}
}
@@ -156,22 +153,28 @@ func (e *Edit) RequiresApproval(map[string]any) bool {
}
func (e *Edit) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
// TODO: use shared agent.RequiredStringArg / agent.OptionalBoolArg for args (see agent package cleanup plan).
path, ok := args["path"].(string)
if !ok || strings.TrimSpace(path) == "" {
return agent.ToolResult{}, fmt.Errorf("path parameter is required")
}
edits, replaceAll, err := parseEditArgs(args)
if err != nil {
return agent.ToolResult{}, err
oldText, ok := args["old_text"].(string)
if !ok || oldText == "" {
return agent.ToolResult{}, fmt.Errorf("old_text parameter is required")
}
newText, ok := args["new_text"].(string)
if !ok {
return agent.ToolResult{}, fmt.Errorf("new_text parameter is required")
}
replaceAll, _ := args["replace_all"].(bool)
if err := rejectFinalSymlink(toolCtx.WorkingDir, path); err != nil {
return agent.ToolResult{}, err
}
file, info, err := openRegularFile(toolCtx.WorkingDir, path, false)
file, info, err := openRegularFile(toolCtx.WorkingDir, path)
if err != nil {
return agent.ToolResult{}, err
}
@@ -187,7 +190,7 @@ func (e *Edit) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
default:
}
contentBytes, err := readAllWithinLimit(file, maxReadBytes)
contentBytes, err := io.ReadAll(file)
if closeErr := file.Close(); err == nil && closeErr != nil {
err = closeErr
}
@@ -195,56 +198,19 @@ func (e *Edit) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
return agent.ToolResult{}, err
}
content := string(contentBytes)
var updated string
replacements := 0
if replaceAll {
matches := strings.Count(content, edits[0].OldText)
if matches == 0 {
return agent.ToolResult{}, fmt.Errorf("old_text was not found in %s", path)
}
updated = strings.ReplaceAll(content, edits[0].OldText, edits[0].NewText)
replacements = matches
} else {
// Every edit is matched against the original file content rather
// than the output of earlier edits, so each edit must match exactly
// once and edits must target disjoint regions.
matched := make([]editMatch, 0, len(edits))
for i, edit := range edits {
count := strings.Count(content, edit.OldText)
if count == 0 {
return agent.ToolResult{}, editNotFoundError(path, i, len(edits))
}
if count > 1 {
return agent.ToolResult{}, editAmbiguousError(path, i, len(edits), count)
}
matched = append(matched, editMatch{
editIndex: i,
offset: strings.Index(content, edit.OldText),
length: len(edit.OldText),
newText: edit.NewText,
})
replacements++
}
slices.SortFunc(matched, func(a, b editMatch) int { return cmp.Compare(a.offset, b.offset) })
for i := 1; i < len(matched); i++ {
prev, cur := matched[i-1], matched[i]
if prev.offset+prev.length > cur.offset {
return agent.ToolResult{}, fmt.Errorf("edits[%d] and edits[%d] overlap in %s; merge them into one edit or target disjoint text", prev.editIndex, cur.editIndex, path)
}
}
// Apply from the end of the file backwards so earlier offsets stay valid.
updated = content
for i := len(matched) - 1; i >= 0; i-- {
m := matched[i]
updated = updated[:m.offset] + m.newText + updated[m.offset+m.length:]
}
matches := strings.Count(content, oldText)
if matches == 0 {
return agent.ToolResult{}, fmt.Errorf("old_text was not found in %s", path)
}
if matches > 1 && !replaceAll {
return agent.ToolResult{}, fmt.Errorf("old_text matched %d times in %s; set replace_all to true to replace every match", matches, path)
}
if updated == content {
return agent.ToolResult{}, fmt.Errorf("edit produced no changes in %s; replacement text is identical to the original", path)
var updated string
if replaceAll {
updated = strings.ReplaceAll(content, oldText, newText)
} else {
updated = strings.Replace(content, oldText, newText, 1)
}
if len(updated) > maxReadBytes {
return agent.ToolResult{}, fmt.Errorf("edited content is too large (%d bytes)", len(updated))
@@ -254,116 +220,7 @@ func (e *Edit) Execute(ctx context.Context, toolCtx agent.ToolContext, args map[
return agent.ToolResult{}, err
}
return agent.ToolResult{Content: fmt.Sprintf("Updated %s (%d edit%s, %d replacement%s).", path, len(edits), plural(len(edits)), replacements, plural(replacements))}, nil
}
// editReplacement is one targeted replacement within an edit call.
type editReplacement struct {
OldText string
NewText string
}
// editMatch locates one editReplacement within the original file content.
type editMatch struct {
editIndex int
offset int
length int
newText string
}
// parseEditArgs normalizes edit arguments from a tool call into a list of
// replacements. It accepts the `edits` array form and tolerates legacy
// top-level old_text/new_text args as well as stringified JSON, mirroring
// the pi coding agent's argument handling.
func parseEditArgs(args map[string]any) ([]editReplacement, bool, error) {
replaceAll, _ := args["replace_all"].(bool)
var edits []editReplacement
if raw, ok := args["edits"]; ok {
parsed, err := parseEditArray(raw)
if err != nil {
return nil, false, err
}
edits = parsed
}
// Fold a legacy top-level old_text/new_text pair into edits.
if oldText, ok := args["old_text"].(string); ok {
newText, ok := args["new_text"].(string)
if !ok {
return nil, false, fmt.Errorf("new_text parameter is required")
}
edits = append(edits, editReplacement{OldText: oldText, NewText: newText})
}
if len(edits) == 0 {
return nil, false, fmt.Errorf("edits parameter is required")
}
for i, edit := range edits {
if edit.OldText == "" {
if len(edits) == 1 {
return nil, false, fmt.Errorf("old_text parameter is required")
}
return nil, false, fmt.Errorf("edits[%d].old_text must not be empty", i)
}
}
if replaceAll && len(edits) != 1 {
return nil, false, fmt.Errorf("replace_all only applies to a single edit")
}
return edits, replaceAll, nil
}
func parseEditArray(raw any) ([]editReplacement, error) {
if s, ok := raw.(string); ok {
// Some models serialize array arguments as a JSON string.
if err := json.Unmarshal([]byte(s), &raw); err != nil {
return nil, fmt.Errorf("edits must be an array of {old_text, new_text} objects")
}
}
items, ok := raw.([]any)
if !ok {
return nil, fmt.Errorf("edits must be an array of {old_text, new_text} objects")
}
edits := make([]editReplacement, 0, len(items))
for i, item := range items {
entry, ok := item.(map[string]any)
if !ok {
return nil, fmt.Errorf("edits[%d] must be an object with old_text and new_text", i)
}
oldText, oldOK := editTextArg(entry, "old_text", "oldText")
newText, newOK := editTextArg(entry, "new_text", "newText")
if !oldOK || !newOK {
return nil, fmt.Errorf("edits[%d] must be an object with old_text and new_text", i)
}
edits = append(edits, editReplacement{OldText: oldText, NewText: newText})
}
return edits, nil
}
// editTextArg reads the first present string key, tolerating both snake_case
// and camelCase spellings that models emit.
func editTextArg(entry map[string]any, keys ...string) (string, bool) {
for _, key := range keys {
if value, ok := entry[key].(string); ok {
return value, true
}
}
return "", false
}
func editNotFoundError(path string, editIndex, totalEdits int) error {
if totalEdits == 1 {
return fmt.Errorf("old_text was not found in %s", path)
}
return fmt.Errorf("edits[%d].old_text was not found in %s", editIndex, path)
}
func editAmbiguousError(path string, editIndex, totalEdits, occurrences int) error {
if totalEdits == 1 {
return fmt.Errorf("old_text matched %d times in %s; set replace_all to true to replace every match", occurrences, path)
}
return fmt.Errorf("edits[%d].old_text matched %d times in %s; each edit must match exactly once, so provide more surrounding context", editIndex, occurrences, path)
return agent.ToolResult{Content: fmt.Sprintf("Updated %s (%d replacement%s).", path, matches, plural(matches))}, nil
}
func cleanRelativePath(path string) (string, error) {
@@ -381,39 +238,7 @@ func cleanRelativePath(path string) (string, error) {
return cleaned, nil
}
func openRegularFile(workingDir, path string, allowAbsolute bool) (*os.File, os.FileInfo, error) {
path = strings.TrimSpace(path)
if path == "" {
return nil, nil, fmt.Errorf("path parameter is required")
}
if allowAbsolute && filepath.IsAbs(path) {
cleaned := filepath.Clean(path)
info, err := os.Lstat(cleaned)
if err != nil {
return nil, nil, err
}
if info.Mode()&os.ModeSymlink != 0 {
return nil, nil, fmt.Errorf("%s is a symlink; read the target file directly", path)
}
if err := rejectNonRegularFile(path, info); err != nil {
return nil, nil, err
}
file, err := os.Open(cleaned)
if err != nil {
return nil, nil, err
}
info, err = file.Stat()
if err != nil {
file.Close()
return nil, nil, err
}
if err := rejectNonRegularFile(path, info); err != nil {
file.Close()
return nil, nil, err
}
return file, info, nil
}
func openRegularFile(workingDir, path string) (*os.File, os.FileInfo, error) {
rel, err := cleanRelativePath(path)
if err != nil {
return nil, nil, err
@@ -424,9 +249,6 @@ func openRegularFile(workingDir, path string, allowAbsolute bool) (*os.File, os.
}
defer root.Close()
if _, err := regularRootFileInfo(root, rel, path); err != nil {
return nil, nil, err
}
file, err := root.Open(rel)
if err != nil {
return nil, nil, rootPathError(err)
@@ -436,43 +258,13 @@ func openRegularFile(workingDir, path string, allowAbsolute bool) (*os.File, os.
file.Close()
return nil, nil, err
}
if err := rejectNonRegularFile(path, info); err != nil {
if info.IsDir() {
file.Close()
return nil, nil, err
return nil, nil, fmt.Errorf("%s is a directory", path)
}
return file, info, nil
}
func regularRootFileInfo(root *os.Root, rel, path string) (os.FileInfo, error) {
info, err := root.Lstat(rel)
if err != nil {
return nil, rootPathError(err)
}
// Reject symlinks outright. os.Root.Open follows symlinks via openat
// without O_NOFOLLOW, so a symlink inside the working root that points
// outside it (e.g. ./notes -> ~/.ssh/id_rsa) would otherwise be read
// transparently, bypassing the working-directory confinement that the
// bash denylist enforces for direct credential reads. The caller must
// operate on the real target file instead.
if info.Mode()&os.ModeSymlink != 0 {
return nil, fmt.Errorf("%s is a symlink; read the target file directly", path)
}
if err := rejectNonRegularFile(path, info); err != nil {
return nil, err
}
return info, nil
}
func rejectNonRegularFile(path string, info os.FileInfo) error {
if info.IsDir() {
return fmt.Errorf("%s is a directory", path)
}
if !info.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", path)
}
return nil
}
func writeFileAtomic(workingDir, path string, data []byte, perm os.FileMode) error {
rel, err := cleanRelativePath(path)
if err != nil {
@@ -502,14 +294,6 @@ func writeFileAtomic(workingDir, path string, data []byte, perm os.FileMode) err
if err != nil {
return rootPathError(err)
}
if err := file.Chmod(perm); err != nil {
closeErr := file.Close()
_ = root.Remove(candidate)
if closeErr != nil {
return closeErr
}
return err
}
writeErr := writeAllAndSync(file, data)
closeErr := file.Close()
if writeErr != nil || closeErr != nil {
@@ -573,20 +357,6 @@ func writeAllAndSync(file *os.File, data []byte) error {
return file.Sync()
}
func readAllWithinLimit(reader io.Reader, limit int) ([]byte, error) {
if limit < 0 {
limit = 0
}
content, err := io.ReadAll(io.LimitReader(reader, int64(limit)+1))
if err != nil {
return nil, err
}
if len(content) > limit {
return nil, fmt.Errorf("content is too large (%d byte limit)", limit)
}
return content, nil
}
func workingDirAbs(workingDir string) (string, error) {
base := workingDir
if base == "" {
@@ -619,47 +389,139 @@ type readSelection struct {
func readSelectionFromArgs(args map[string]any) (readSelection, error) {
selection := readSelection{start: 1}
var startSet, endSet bool
if start, ok, err := intReadArg(args, "start"); err != nil {
for _, key := range []string{"line_range", "range", "lines"} {
if lineRange, ok := stringReadArg(args, key); ok {
start, end, err := parseLineRange(lineRange)
if err != nil {
return readSelection{}, err
}
selection.enabled = true
if start > 0 {
selection.start = start
startSet = true
}
if end > 0 {
selection.end = end
endSet = true
}
break
}
}
if start, ok, err := intReadArg(args, "start_line"); err != nil {
return readSelection{}, err
} else if ok {
selection.enabled = true
selection.start = start
startSet = true
}
if end, ok, err := intReadArg(args, "end"); err != nil {
if end, ok, err := intReadArg(args, "end_line"); err != nil {
return readSelection{}, err
} else if ok {
selection.enabled = true
selection.end = end
endSet = true
}
lineCount, countSet, err := readLineCountArg(args)
if err != nil {
return readSelection{}, err
}
if countSet {
selection.enabled = true
if !startSet {
selection.start = 1
}
if !endSet {
selection.end = selection.start + lineCount - 1
}
}
if !selection.enabled {
return selection, nil
}
if selection.start < 1 {
return readSelection{}, fmt.Errorf("start must be greater than 0")
return readSelection{}, fmt.Errorf("start_line must be greater than 0")
}
if selection.end > 0 && selection.end < selection.start {
return readSelection{}, fmt.Errorf("end must be greater than or equal to start")
return readSelection{}, fmt.Errorf("end_line must be greater than or equal to start_line")
}
return selection, nil
}
func readLineCountArg(args map[string]any) (int, bool, error) {
for _, key := range []string{"line_count", "num_lines"} {
value, ok, err := intReadArg(args, key)
if err != nil || ok {
if ok && value < 1 {
return 0, false, fmt.Errorf("%s must be greater than 0", key)
}
return value, ok, err
}
}
return 0, false, nil
}
func parseLineRange(value string) (int, int, error) {
value = strings.TrimSpace(value)
if value == "" {
return 0, 0, nil
}
value = strings.TrimPrefix(value, "lines")
value = strings.TrimPrefix(value, "line")
value = strings.TrimSpace(value)
for _, sep := range []string{"..", ":", ","} {
value = strings.ReplaceAll(value, sep, "-")
}
parts := strings.Split(value, "-")
if len(parts) > 2 {
return 0, 0, fmt.Errorf("line_range must look like 10-40, 10:40, 10..40, 10-, or 10")
}
start, end := 0, 0
var err error
if strings.TrimSpace(parts[0]) != "" {
start, err = strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil || start < 1 {
return 0, 0, fmt.Errorf("line_range start must be a positive line number")
}
}
if len(parts) == 1 {
return start, start, nil
}
if strings.TrimSpace(parts[1]) != "" {
end, err = strconv.Atoi(strings.TrimSpace(parts[1]))
if err != nil || end < 1 {
return 0, 0, fmt.Errorf("line_range end must be a positive line number")
}
}
if start == 0 && end == 0 {
return 0, 0, fmt.Errorf("line_range must include at least one line number")
}
if start == 0 {
start = 1
}
if end > 0 && end < start {
return 0, 0, fmt.Errorf("line_range end must be greater than or equal to start")
}
return start, end, nil
}
func readLineSelection(file *os.File, selection readSelection) (string, error) {
reader := bufio.NewReader(file)
var b strings.Builder
for lineNo := 1; ; {
line, err := reader.ReadSlice('\n')
for lineNo := 1; ; lineNo++ {
line, err := reader.ReadString('\n')
if lineNo >= selection.start && (selection.end == 0 || lineNo <= selection.end) {
if b.Len()+len(line) > maxReadBytes {
return "", fmt.Errorf("selected content is too large (%d byte limit)", maxReadBytes)
}
b.Write(line)
b.WriteString(line)
}
if err != nil {
if err == bufio.ErrBufferFull {
continue
}
if err == io.EOF {
break
}
@@ -668,11 +530,15 @@ func readLineSelection(file *os.File, selection readSelection) (string, error) {
if selection.end > 0 && lineNo >= selection.end {
break
}
lineNo++
}
return b.String(), nil
}
func stringReadArg(args map[string]any, key string) (string, bool) {
value, ok := args[key].(string)
return value, ok && strings.TrimSpace(value) != ""
}
func intReadArg(args map[string]any, key string) (int, bool, error) {
value, ok := args[key]
if !ok {
+38 -354
View File
@@ -2,7 +2,6 @@ package tools
import (
"context"
"io"
"os"
"path/filepath"
"strings"
@@ -18,7 +17,7 @@ func TestEditReplacesUniqueText(t *testing.T) {
t.Fatal(err)
}
result, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
result, err := NewEdit().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"old_text": "hello",
"new_text": "hi",
@@ -46,7 +45,7 @@ func TestEditRequiresUniqueMatchByDefault(t *testing.T) {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
_, err := NewEdit().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"old_text": "same",
"new_text": "other",
@@ -59,242 +58,9 @@ func TestEditRequiresUniqueMatchByDefault(t *testing.T) {
}
}
func TestEditAppliesMultipleEdits(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("alpha beta gamma delta\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "beta", "new_text": "BETA"},
map[string]any{"old_text": "delta", "new_text": "DELTA"},
},
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(result.Content, "2 edits, 2 replacements") {
t.Fatalf("result = %q", result.Content)
}
content, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(content) != "alpha BETA gamma DELTA\n" {
t.Fatalf("content = %q", content)
}
}
func TestEditMatchesEditsAgainstOriginalContent(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("abc def\n"), 0o644); err != nil {
t.Fatal(err)
}
// edits[1] must target the original "def", not the one introduced by edits[0].
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "abc", "new_text": "def"},
map[string]any{"old_text": "def", "new_text": "ghi"},
},
})
if err != nil {
t.Fatal(err)
}
content, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(content) != "def ghi\n" {
t.Fatalf("content = %q", content)
}
}
func TestEditRejectsOverlappingEdits(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("abc\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "ab", "new_text": "x"},
map[string]any{"old_text": "bc", "new_text": "y"},
},
})
if err == nil {
t.Fatal("expected overlapping edits to fail")
}
if !strings.Contains(err.Error(), "overlap") {
t.Fatalf("err = %v", err)
}
}
func TestEditMultipleEditsNotFoundIndexed(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello world\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "hello", "new_text": "hi"},
map[string]any{"old_text": "missing", "new_text": "x"},
},
})
if err == nil {
t.Fatal("expected missing edit to fail")
}
if !strings.Contains(err.Error(), "edits[1]") {
t.Fatalf("err = %v", err)
}
}
func TestEditMultipleEditsAmbiguousIndexed(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello same same\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "hello", "new_text": "hi"},
map[string]any{"old_text": "same", "new_text": "x"},
},
})
if err == nil {
t.Fatal("expected ambiguous edit to fail")
}
if !strings.Contains(err.Error(), "edits[1]") || !strings.Contains(err.Error(), "matched 2 times") {
t.Fatalf("err = %v", err)
}
}
func TestEditRejectsEmptyEdits(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
for name, args := range map[string]map[string]any{
"missing edits": {"path": "note.txt"},
"empty edits": {"path": "note.txt", "edits": []any{}},
} {
if _, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, args); err == nil {
t.Fatalf("%s: expected error", name)
} else if !strings.Contains(err.Error(), "edits parameter is required") {
t.Fatalf("%s: err = %v", name, err)
}
}
}
func TestEditRejectsEmptyOldTextInArray(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": []any{
map[string]any{"old_text": "hello", "new_text": "hi"},
map[string]any{"old_text": "", "new_text": "x"},
},
})
if err == nil {
t.Fatal("expected empty old_text to fail")
}
if !strings.Contains(err.Error(), "edits[1].old_text must not be empty") {
t.Fatalf("err = %v", err)
}
}
func TestEditAcceptsJSONStringEdits(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello world\n"), 0o644); err != nil {
t.Fatal(err)
}
// Some models serialize array arguments as a JSON string.
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"edits": `[{"oldText": "hello", "newText": "hi"}, {"oldText": "world", "newText": "earth"}]`,
})
if err != nil {
t.Fatal(err)
}
content, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(content) != "hi earth\n" {
t.Fatalf("content = %q", content)
}
}
func TestEditRejectsReplaceAllWithMultipleEdits(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("a b c\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"replace_all": true,
"edits": []any{
map[string]any{"old_text": "a", "new_text": "x"},
map[string]any{"old_text": "b", "new_text": "y"},
},
})
if err == nil {
t.Fatal("expected replace_all with multiple edits to fail")
}
if !strings.Contains(err.Error(), "replace_all") {
t.Fatalf("err = %v", err)
}
}
func TestEditRejectsNoChange(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"old_text": "hello",
"new_text": "hello",
})
if err == nil {
t.Fatal("expected no-change edit to fail")
}
if !strings.Contains(err.Error(), "no changes") {
t.Fatalf("err = %v", err)
}
}
func TestEditRejectsEscapingPath(t *testing.T) {
dir := t.TempDir()
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
_, err := NewEdit().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "../outside.txt",
"old_text": "old",
"new_text": "new",
@@ -317,7 +83,7 @@ func TestEditRejectsSymlinkEscape(t *testing.T) {
t.Skipf("symlinks unavailable: %v", err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
_, err := NewEdit().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": filepath.Join("link", "note.txt"),
"old_text": "old",
"new_text": "new",
@@ -349,7 +115,7 @@ func TestEditRejectsFinalSymlink(t *testing.T) {
t.Skipf("symlinks unavailable: %v", err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
_, err := NewEdit().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "link.txt",
"old_text": "old",
"new_text": "new",
@@ -386,7 +152,7 @@ func TestReadRejectsParentOutsideCurrentWorkingDir(t *testing.T) {
t.Fatal(err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: subdir}, map[string]any{
_, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: subdir}, map[string]any{
"path": "../note.txt",
})
if err == nil {
@@ -397,12 +163,6 @@ func TestReadRejectsParentOutsideCurrentWorkingDir(t *testing.T) {
}
}
func TestReadRequiresApproval(t *testing.T) {
if !agent.ToolRequiresApproval((&Read{}), map[string]any{"path": "note.txt"}) {
t.Fatal("read should require approval")
}
}
func TestReadDefaultsToEntireFile(t *testing.T) {
dir := t.TempDir()
content := "one\ntwo\nthree\n"
@@ -410,7 +170,7 @@ func TestReadDefaultsToEntireFile(t *testing.T) {
t.Fatal(err)
}
result, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
result, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
})
if err != nil {
@@ -421,57 +181,15 @@ func TestReadDefaultsToEntireFile(t *testing.T) {
}
}
func TestReadAllowsAbsolutePath(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
content := "one\ntwo\nthree\n"
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
result, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"path": path,
})
if err != nil {
t.Fatal(err)
}
if result.Content != content {
t.Fatalf("content = %q", result.Content)
}
}
func TestReadRejectsAbsoluteSymlink(t *testing.T) {
dir := t.TempDir()
target := filepath.Join(dir, "target.txt")
if err := os.WriteFile(target, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
link := filepath.Join(dir, "alias")
if err := os.Symlink(target, link); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: t.TempDir()}, map[string]any{
"path": link,
})
if err == nil {
t.Fatal("expected absolute symlink to be rejected")
}
if !strings.Contains(err.Error(), "symlink") {
t.Fatalf("err = %v, want symlink rejection", err)
}
}
func TestReadStartEnd(t *testing.T) {
func TestReadLineRange(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("one\ntwo\nthree\nfour\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"start": 2,
"end": 3,
result, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"line_range": "2-3",
})
if err != nil {
t.Fatal(err)
@@ -481,15 +199,34 @@ func TestReadStartEnd(t *testing.T) {
}
}
func TestReadStartOnly(t *testing.T) {
func TestReadLinesAliasAsRange(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("one\ntwo\nthree\nfour\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
result, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"start": 3,
"lines": "2-3",
})
if err != nil {
t.Fatal(err)
}
if result.Content != "two\nthree\n" {
t.Fatalf("content = %q", result.Content)
}
}
func TestReadLineCountFromStartLine(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("one\ntwo\nthree\nfour\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"start_line": 3,
"line_count": 2,
})
if err != nil {
t.Fatal(err)
@@ -499,73 +236,20 @@ func TestReadStartOnly(t *testing.T) {
}
}
func TestReadEndOnly(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("one\ntwo\nthree\nfour\n"), 0o644); err != nil {
t.Fatal(err)
}
result, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"end": 2,
})
if err != nil {
t.Fatal(err)
}
if result.Content != "one\ntwo\n" {
t.Fatalf("content = %q", result.Content)
}
}
func TestReadSelectionRejectsHugeSingleLine(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte(strings.Repeat("x", maxReadBytes+1)), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"start": 1,
"end": 1,
})
if err == nil {
t.Fatal("expected huge selected line to fail")
}
if !strings.Contains(err.Error(), "selected content is too large") {
t.Fatalf("err = %v", err)
}
}
func TestReadAllWithinLimitRejectsGrowingRead(t *testing.T) {
reader := io.MultiReader(
strings.NewReader(strings.Repeat("x", maxReadBytes)),
strings.NewReader("x"),
)
_, err := readAllWithinLimit(reader, maxReadBytes)
if err == nil {
t.Fatal("expected over-limit read to fail")
}
if !strings.Contains(err.Error(), "content is too large") {
t.Fatalf("err = %v", err)
}
}
func TestReadRejectsInvalidRange(t *testing.T) {
func TestReadRejectsInvalidLineRange(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("one\ntwo\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"start": 4,
"end": 2,
_, err := NewRead().Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"line_range": "4-2",
})
if err == nil {
t.Fatal("expected invalid range to fail")
}
if !strings.Contains(err.Error(), "end must") {
if !strings.Contains(err.Error(), "line_range end") {
t.Fatalf("err = %v", err)
}
}
-121
View File
@@ -1,121 +0,0 @@
//go:build !windows
package tools
import (
"context"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"github.com/ollama/ollama/agent"
)
func TestOpenRegularFileRejectsFIFO(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "pipe")
if err := syscall.Mkfifo(path, 0o600); err != nil {
t.Skipf("mkfifo unavailable: %v", err)
}
done := make(chan error, 1)
go func() {
file, _, err := openRegularFile(dir, "pipe", false)
if file != nil {
file.Close()
}
done <- err
}()
select {
case err := <-done:
if err == nil {
t.Fatal("expected FIFO to be rejected")
}
if !strings.Contains(err.Error(), "not a regular file") {
t.Fatalf("err = %v", err)
}
case <-time.After(time.Second):
t.Fatal("openRegularFile blocked on FIFO")
}
}
func TestEditPreservesModeDespiteUmask(t *testing.T) {
oldUmask := syscall.Umask(0o077)
defer syscall.Umask(oldUmask)
dir := t.TempDir()
path := filepath.Join(dir, "note.txt")
if err := os.WriteFile(path, []byte("hello\n"), 0o666); err != nil {
t.Fatal(err)
}
if err := os.Chmod(path, 0o666); err != nil {
t.Fatal(err)
}
_, err := (&Edit{}).Execute(context.Background(), agent.ToolContext{WorkingDir: dir}, map[string]any{
"path": "note.txt",
"old_text": "hello",
"new_text": "hi",
})
if err != nil {
t.Fatal(err)
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if got := info.Mode().Perm(); got != 0o666 {
t.Fatalf("mode = %#o, want 0666", got)
}
}
func TestReadRejectsSymlinkEscapingWorkingDir(t *testing.T) {
root := t.TempDir()
secret := filepath.Join(t.TempDir(), "secret.txt")
if err := os.WriteFile(secret, []byte("top secret\n"), 0o600); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "notes")
if err := os.Symlink(secret, link); err != nil {
t.Fatal(err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: root}, map[string]any{
"path": "notes",
})
if err == nil {
t.Fatal("expected symlink escaping working dir to be rejected")
}
if !strings.Contains(err.Error(), "symlink") {
t.Fatalf("err = %v, want symlink rejection", err)
}
}
func TestReadRejectsSymlinkInsideWorkingDirToOutside(t *testing.T) {
root := t.TempDir()
target := filepath.Join(root, "real.txt")
if err := os.WriteFile(target, []byte("hello\n"), 0o644); err != nil {
t.Fatal(err)
}
// A symlink to a sibling file still resolves inside the root; Read must
// reject it regardless, consistent with Edit's rejectFinalSymlink.
link := filepath.Join(root, "alias")
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
_, err := (&Read{}).Execute(context.Background(), agent.ToolContext{WorkingDir: root}, map[string]any{
"path": "alias",
})
if err == nil {
t.Fatal("expected symlink to be rejected even when target is inside root")
}
if !strings.Contains(err.Error(), "symlink") {
t.Fatalf("err = %v, want symlink rejection", err)
}
}
+118 -22
View File
@@ -2,40 +2,136 @@ package tools
import (
"context"
"errors"
"fmt"
"strings"
"github.com/ollama/ollama/agent"
"github.com/ollama/ollama/agent/skills"
"github.com/ollama/ollama/api"
)
// Skill is the model-facing adapter for the core agent skill catalog.
// Model-initiated loads require approval because a skill's instructions can
// influence the rest of the run. Explicit user activation is handled by the
// session's synthetic skill call and bypasses this adapter.
type Skill struct{ Catalog *agent.SkillCatalog }
func (t *Skill) Name() string { return "skill" }
func (t *Skill) Description() string {
return "Load a named Ollama skill and return its instructions."
type Skill struct {
catalog *skills.Catalog
}
func (t *Skill) Schema() api.ToolFunction {
func NewSkill(catalog *skills.Catalog) *Skill {
return &Skill{catalog: catalog}
}
func (s *Skill) Name() string {
return "skill"
}
func (s *Skill) Description() string {
return "Load the full SKILL.md instructions for an installed agent skill by name."
}
func (s *Skill) Schema() api.ToolFunction {
props := api.NewToolPropertiesMap()
props.Set("name", api.ToolProperty{Type: api.PropertyType{"string"}, Description: "Name of the skill to load."})
return api.ToolFunction{Name: t.Name(), Description: t.Description(), Parameters: api.ToolFunctionParameters{Type: "object", Properties: props, Required: []string{"name"}}}
props.Set("name", api.ToolProperty{
Type: api.PropertyType{"string"},
Description: "Name of the skill to load.",
})
return api.ToolFunction{
Name: s.Name(),
Description: s.Description(),
Parameters: api.ToolFunctionParameters{
Type: "object",
Required: []string{"name"},
Properties: props,
},
}
}
func (t *Skill) RequiresApproval(map[string]any) bool { return true }
func (t *Skill) Execute(_ context.Context, _ agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
name, ok := args["name"].(string)
if !ok {
return agent.ToolResult{}, errors.New("name parameter is required")
func (s *Skill) Execute(_ context.Context, _ agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
name, _ := args["name"].(string)
name = skills.NormalizeName(name)
if name == "" {
return agent.ToolResult{}, fmt.Errorf("name parameter is required")
}
skill, err := t.Catalog.Load(name)
if s.catalog == nil || s.catalog.Empty() {
return agent.ToolResult{}, fmt.Errorf("no skills are installed")
}
skill, ok := s.catalog.Find(name)
if !ok {
return agent.ToolResult{}, fmt.Errorf("unknown skill: %s", name)
}
content, err := SkillResultContent(skill)
if err != nil {
return agent.ToolResult{}, err
}
return agent.ToolResult{Content: skill.Content()}, nil
return agent.ToolResult{Content: content}, nil
}
func ManualSkillMessages(skill skills.Skill, request string, ordinal int) ([]api.Message, error) {
content, err := SkillResultContent(skill)
if err != nil {
return nil, err
}
args := api.NewToolCallFunctionArguments()
args.Set("name", skill.Name)
callID := manualSkillToolCallID(skill.Name, ordinal)
userContent := strings.TrimSpace(request)
if userContent == "" {
userContent = fmt.Sprintf("Use the %s skill.", skill.Name)
}
return []api.Message{
{Role: "user", Content: userContent},
{
Role: "assistant",
ToolCalls: []api.ToolCall{{
ID: callID,
Function: api.ToolCallFunction{
Name: "skill",
Arguments: args,
},
}},
},
{Role: "tool", ToolName: "skill", ToolCallID: callID, Content: content},
}, nil
}
func SkillResultContent(skill skills.Skill) (string, error) {
content, err := skill.Read()
if err != nil {
return "", err
}
var b strings.Builder
b.WriteString("Loaded skill: ")
b.WriteString(skill.Name)
b.WriteByte('\n')
b.WriteString("Skill directory: ")
b.WriteString(skill.Dir)
b.WriteString("\nResolve relative file references from the skill directory.\n\n")
b.WriteString(content)
return b.String(), nil
}
func manualSkillToolCallID(skillName string, ordinal int) string {
name := strings.Trim(strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z':
return r
case r >= 'A' && r <= 'Z':
return r
case r >= '0' && r <= '9':
return r
case r == '-' || r == '_':
return r
default:
return '-'
}
}, skillName), "-")
if name == "" {
name = "skill"
}
if ordinal <= 0 {
return "manual-skill-" + name
}
return fmt.Sprintf("manual-skill-%d-%s", ordinal, name)
}
+54 -136
View File
@@ -8,156 +8,74 @@ import (
"testing"
"github.com/ollama/ollama/agent"
"github.com/ollama/ollama/api"
"github.com/ollama/ollama/agent/skills"
)
func TestSkillLoadsCoreCatalogWithApproval(t *testing.T) {
catalog := testSkillCatalog(t)
tool := &Skill{Catalog: catalog}
if !agent.ToolRequiresApproval(tool, map[string]any{"name": "release-notes"}) {
t.Fatal("model-initiated skill loading should require approval")
}
result, err := tool.Execute(context.Background(), agent.ToolContext{}, map[string]any{"name": "release-notes"})
if err != nil || !strings.Contains(result.Content, "Use concise bullets.") {
t.Fatalf("tool result = %#v, %v", result, err)
}
}
func TestModelSkillLoadRequiresApproval(t *testing.T) {
for _, tt := range []struct {
name string
approval agent.Approval
prompt bool
wantCalls int
wantPrompts int
wantResult string
}{
{name: "rejected", approval: agent.Approval{Reason: "Skill loading denied."}, prompt: true, wantCalls: 1, wantPrompts: 1, wantResult: "Skill loading denied."},
{name: "approved", approval: agent.Approval{Allow: true}, prompt: true, wantCalls: 2, wantPrompts: 1, wantResult: "Use concise bullets."},
{name: "headless denied", wantCalls: 1, wantResult: "Tool execution requires approval"},
} {
t.Run(tt.name, func(t *testing.T) {
catalog := testSkillCatalog(t)
args := api.NewToolCallFunctionArguments()
args.Set("name", "release-notes")
client := &skillTestClient{responses: [][]api.ChatResponse{
{{Message: api.Message{Role: "assistant", ToolCalls: []api.ToolCall{{
ID: "call_skill_1",
Function: api.ToolCallFunction{Name: "skill", Arguments: args},
}}}}},
{{Message: api.Message{Role: "assistant", Content: "done"}}},
}}
var prompter *skillApprovalPrompter
var approvalPrompter agent.ApprovalPrompter
if tt.prompt {
prompter = &skillApprovalPrompter{result: tt.approval}
approvalPrompter = prompter
}
registry := &agent.Registry{}
registry.Register(&Skill{Catalog: catalog})
result, err := (&agent.Session{
Client: client,
Tools: registry,
ApprovalPrompter: approvalPrompter,
}).Run(context.Background(), agent.RunOptions{
Model: "test",
NewMessages: []api.Message{{Role: "user", Content: "load the release-notes skill"}},
})
if err != nil {
t.Fatal(err)
}
if tt.prompt {
if got := len(prompter.requests); got != tt.wantPrompts {
t.Fatalf("approval prompts = %d, want %d", got, tt.wantPrompts)
}
request := prompter.requests[0]
if len(request.Calls) != 1 || request.Calls[0].ToolName != "skill" || request.Calls[0].ApprovalScope != "skill" || request.Calls[0].Args["name"] != "release-notes" {
t.Fatalf("approval request = %#v", request)
}
}
if got := client.calls; got != tt.wantCalls {
t.Fatalf("model calls = %d, want %d", got, tt.wantCalls)
}
var toolResult string
for _, message := range result.Messages {
if message.Role == "tool" && message.ToolCallID == "call_skill_1" {
toolResult = message.Content
break
}
}
if !strings.Contains(toolResult, tt.wantResult) {
t.Fatalf("skill tool result = %q, want it to contain %q", toolResult, tt.wantResult)
}
})
}
}
func TestExplicitSkillActivationBypassesApproval(t *testing.T) {
catalog := testSkillCatalog(t)
client := &skillTestClient{responses: [][]api.ChatResponse{{{Message: api.Message{Role: "assistant", Content: "done"}}}}}
prompter := &skillApprovalPrompter{result: agent.Approval{}}
result, err := (&agent.Session{
Client: client,
Skills: catalog,
ApprovalPrompter: prompter,
}).Run(context.Background(), agent.RunOptions{
Model: "test",
NewMessages: []api.Message{{Role: "user", Content: "draft release notes"}},
SkillName: "release-notes",
})
if err != nil {
t.Fatal(err)
}
if len(prompter.requests) != 0 {
t.Fatalf("explicit activation prompted for approval: %#v", prompter.requests)
}
if len(result.Messages) != 4 || result.Messages[2].ToolName != "skill" || !strings.Contains(result.Messages[2].Content, "Use concise bullets.") {
t.Fatalf("synthetic skill activation = %#v", result.Messages)
}
}
func testSkillCatalog(t *testing.T) *agent.SkillCatalog {
t.Helper()
func TestSkillToolLoadsSkill(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "release-notes")
if err := os.Mkdir(path, 0o755); err != nil {
skillDir := filepath.Join(dir, "go-code")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(path, "SKILL.md"), []byte("---\nname: release-notes\ndescription: Draft release notes.\n---\nUse concise bullets."), 0o644); err != nil {
if err := os.WriteFile(filepath.Join(skillDir, skills.SkillFile), []byte("---\nname: go-code\ndescription: Write Go code.\n---\n\n# Go Code\n"), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := agent.DiscoverSkills(dir)
catalog, err := skills.Load(dir)
if err != nil {
t.Fatal(err)
}
return catalog
}
type skillTestClient struct {
responses [][]api.ChatResponse
calls int
}
func (c *skillTestClient) Chat(_ context.Context, _ *api.ChatRequest, fn api.ChatResponseFunc) error {
if c.calls >= len(c.responses) {
return nil
result, err := NewSkill(catalog).Execute(context.Background(), agent.ToolContext{}, map[string]any{"name": "go-code"})
if err != nil {
t.Fatal(err)
}
for _, response := range c.responses[c.calls] {
if err := fn(response); err != nil {
return err
}
if !strings.Contains(result.Content, "Loaded skill: go-code") || !strings.Contains(result.Content, "# Go Code") {
t.Fatalf("content = %q", result.Content)
}
c.calls++
return nil
}
type skillApprovalPrompter struct {
requests []agent.ApprovalRequest
result agent.Approval
}
func TestManualSkillMessagesUseToolCallShape(t *testing.T) {
dir := t.TempDir()
skillDir := filepath.Join(dir, "go-code")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skillDir, skills.SkillFile), []byte("---\nname: go-code\ndescription: Write Go code.\n---\n\n# Go Code\n"), 0o644); err != nil {
t.Fatal(err)
}
catalog, err := skills.Load(dir)
if err != nil {
t.Fatal(err)
}
skill, ok := catalog.Find("go-code")
if !ok {
t.Fatal("skill not found")
}
func (p *skillApprovalPrompter) PromptApproval(_ context.Context, request agent.ApprovalRequest) (agent.Approval, error) {
p.requests = append(p.requests, request)
return p.result, nil
messages, err := ManualSkillMessages(skill, "write a test", 7)
if err != nil {
t.Fatal(err)
}
if len(messages) != 3 {
t.Fatalf("messages = %d, want 3", len(messages))
}
if messages[0].Role != "user" || messages[0].Content != "write a test" {
t.Fatalf("user message = %#v", messages[0])
}
if messages[1].Role != "assistant" || len(messages[1].ToolCalls) != 1 {
t.Fatalf("assistant tool call = %#v", messages[1])
}
call := messages[1].ToolCalls[0]
if call.ID != "manual-skill-7-go-code" || call.Function.Name != "skill" {
t.Fatalf("tool call = %#v", call)
}
if name, _ := call.Function.Arguments.Get("name"); name != "go-code" {
t.Fatalf("tool args = %s", call.Function.Arguments.String())
}
if messages[2].Role != "tool" || messages[2].ToolName != "skill" || messages[2].ToolCallID != call.ID {
t.Fatalf("tool result metadata = %#v", messages[2])
}
if !strings.Contains(messages[2].Content, "Loaded skill: go-code") || !strings.Contains(messages[2].Content, "# Go Code") {
t.Fatalf("tool result = %q", messages[2].Content)
}
}
+33 -24
View File
@@ -13,16 +13,23 @@ import (
internalcloud "github.com/ollama/ollama/internal/cloud"
)
var (
ErrWebSearchAuthRequired = errors.New("web search requires authentication")
ErrWebFetchAuthRequired = errors.New("web fetch requires authentication")
)
const (
maxWebFetchContentRunes = 60_000
webSearchTimeout = 15 * time.Second
webFetchTimeout = 30 * time.Second
)
var ErrWebAuthRequired = errors.New("Not authenticated. Run `ollama signin` and try again.")
type WebSearch struct{}
func NewWebSearch() *WebSearch {
return &WebSearch{}
}
func (w *WebSearch) Name() string {
return "web_search"
}
@@ -48,12 +55,7 @@ func (w *WebSearch) Schema() api.ToolFunction {
}
}
func (w *WebSearch) RequiresApproval(map[string]any) bool {
return true
}
func (w *WebSearch) Execute(ctx context.Context, _ agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
// TODO: use shared agent.RequiredStringArg for the "query" parameter (see agent package cleanup plan).
if internalcloud.Disabled() {
return agent.ToolResult{}, errors.New(internalcloud.DisabledError("web search is unavailable"))
}
@@ -74,7 +76,7 @@ func (w *WebSearch) Execute(ctx context.Context, _ agent.ToolContext, args map[s
if err != nil {
var authErr api.AuthorizationError
if errors.As(err, &authErr) {
return agent.ToolResult{}, ErrWebAuthRequired
return agent.ToolResult{}, ErrWebSearchAuthRequired
}
return agent.ToolResult{}, err
}
@@ -101,6 +103,10 @@ func (w *WebSearch) Execute(ctx context.Context, _ agent.ToolContext, args map[s
type WebFetch struct{}
func NewWebFetch() *WebFetch {
return &WebFetch{}
}
func (w *WebFetch) Name() string {
return "web_fetch"
}
@@ -126,12 +132,7 @@ func (w *WebFetch) Schema() api.ToolFunction {
}
}
func (w *WebFetch) RequiresApproval(map[string]any) bool {
return true
}
func (w *WebFetch) Execute(ctx context.Context, _ agent.ToolContext, args map[string]any) (agent.ToolResult, error) {
// TODO: use shared agent.RequiredStringArg for the "url" parameter (see agent package cleanup plan).
if internalcloud.Disabled() {
return agent.ToolResult{}, errors.New(internalcloud.DisabledError("web fetch is unavailable"))
}
@@ -139,13 +140,9 @@ func (w *WebFetch) Execute(ctx context.Context, _ agent.ToolContext, args map[st
if !ok || strings.TrimSpace(urlStr) == "" {
return agent.ToolResult{}, fmt.Errorf("url parameter is required")
}
parsed, err := url.Parse(urlStr)
if err != nil {
if _, err := url.Parse(urlStr); err != nil {
return agent.ToolResult{}, fmt.Errorf("invalid URL: %w", err)
}
if scheme := strings.ToLower(parsed.Scheme); scheme != "http" && scheme != "https" {
return agent.ToolResult{}, fmt.Errorf("unsupported URL scheme %q: only http and https are allowed", parsed.Scheme)
}
client, err := api.ClientFromEnvironment()
if err != nil {
@@ -159,7 +156,7 @@ func (w *WebFetch) Execute(ctx context.Context, _ agent.ToolContext, args map[st
if err != nil {
var authErr api.AuthorizationError
if errors.As(err, &authErr) {
return agent.ToolResult{}, ErrWebAuthRequired
return agent.ToolResult{}, ErrWebFetchAuthRequired
}
return agent.ToolResult{}, err
}
@@ -178,9 +175,21 @@ func (w *WebFetch) Execute(ctx context.Context, _ agent.ToolContext, args map[st
}
func truncateWebFetchContent(content string) string {
return agent.Truncate(content, agent.TruncateConfig{
MaxRunes: maxWebFetchContentRunes,
Label: "tool output",
Hint: "Use a narrower request or search query if more detail is needed.",
})
runes := []rune(content)
if len(runes) <= maxWebFetchContentRunes {
return content
}
omitted := len(runes) - maxWebFetchContentRunes
return string(runes[:maxWebFetchContentRunes]) + fmt.Sprintf(
"\n\n[tool output truncated: showing first ~%d tokens; omitted ~%d tokens. Use a narrower request or search query if more detail is needed.]",
approximateToolTokensFromRunes(maxWebFetchContentRunes),
approximateToolTokensFromRunes(omitted),
)
}
func approximateToolTokensFromRunes(n int) int {
if n <= 0 {
return 0
}
return max(1, (n+3)/4)
}
+6 -161
View File
@@ -2,166 +2,25 @@ package tools
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
coreagent "github.com/ollama/ollama/agent"
"github.com/ollama/ollama/api"
"github.com/ollama/ollama/envconfig"
internalcloud "github.com/ollama/ollama/internal/cloud"
)
func TestWebToolsRequireApproval(t *testing.T) {
if !coreagent.ToolRequiresApproval((&WebSearch{}), map[string]any{"query": "ollama"}) {
t.Fatal("web search should require approval")
func TestWebToolsDoNotRequireApproval(t *testing.T) {
if coreagent.ToolRequiresApproval(NewWebSearch(), map[string]any{"query": "ollama"}) {
t.Fatal("web search should not require approval")
}
if !coreagent.ToolRequiresApproval((&WebFetch{}), map[string]any{"url": "https://ollama.com"}) {
t.Fatal("web fetch should require approval")
}
}
var webToolCases = []struct {
name string
tool coreagent.Tool
args map[string]any
path string
operation string
}{
{"search", &WebSearch{}, map[string]any{"query": "ollama"}, "/api/experimental/web_search", "web search is unavailable"},
{"fetch", &WebFetch{}, map[string]any{"url": "https://ollama.com"}, "/api/experimental/web_fetch", "web fetch is unavailable"},
}
// enableWebToolsForTest isolates web tool tests from the runner's cloud
// policy. In particular, Windows can inherit both OLLAMA_NO_CLOUD and a
// server.json from USERPROFILE.
func enableWebToolsForTest(t *testing.T) {
t.Helper()
// Register before t.Setenv so the cache is refreshed after t.Setenv has
// restored the runner's environment during cleanup.
t.Cleanup(envconfig.ReloadServerConfig)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("OLLAMA_NO_CLOUD", "")
envconfig.ReloadServerConfig()
}
// runWebTool executes tool against a stub server that responds to every
// request with status and body, returning the resulting error.
func runWebTool(t *testing.T, tool coreagent.Tool, args map[string]any, path string, status int, body string) error {
t.Helper()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != path {
t.Fatalf("path = %q, want %q", r.URL.Path, path)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
t.Cleanup(ts.Close)
t.Setenv("OLLAMA_HOST", ts.URL)
_, err := tool.Execute(t.Context(), coreagent.ToolContext{}, args)
return err
}
func TestWebToolsReportAuthenticationError(t *testing.T) {
enableWebToolsForTest(t)
for _, tt := range webToolCases {
t.Run(tt.name, func(t *testing.T) {
err := runWebTool(t, tt.tool, tt.args, tt.path, http.StatusUnauthorized,
`{"error":"unauthorized","signin_url":"https://ollama.com/signin"}`)
if !errors.Is(err, ErrWebAuthRequired) {
t.Fatalf("error = %v, want %v", err, ErrWebAuthRequired)
}
})
}
}
func TestWebToolsPreserveNonAuthenticationErrors(t *testing.T) {
enableWebToolsForTest(t)
for _, tt := range webToolCases {
t.Run(tt.name, func(t *testing.T) {
err := runWebTool(t, tt.tool, tt.args, tt.path, http.StatusTooManyRequests,
`{"error":"web search quota exceeded"}`)
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "web search quota exceeded") {
t.Fatalf("error = %q, want original error message", err)
}
})
}
}
func TestWebToolsIgnoreInheritedCloudPolicy(t *testing.T) {
// This cleanup is registered before the test environment, so it restores
// the server config cache after t.Setenv restores the runner's values.
t.Cleanup(envconfig.ReloadServerConfig)
home := t.TempDir()
configPath := filepath.Join(home, ".ollama", "server.json")
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(configPath, []byte(`{"disable_ollama_cloud":true}`), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("OLLAMA_NO_CLOUD", "1")
envconfig.ReloadServerConfig()
enableWebToolsForTest(t)
err := runWebTool(t, &WebSearch{}, map[string]any{"query": "ollama"}, "/api/experimental/web_search", http.StatusUnauthorized,
`{"error":"unauthorized","signin_url":"https://ollama.com/signin"}`)
if !errors.Is(err, ErrWebAuthRequired) {
t.Fatalf("error = %v, want %v", err, ErrWebAuthRequired)
}
}
func TestWebFetchRejectsUnsupportedScheme(t *testing.T) {
enableWebToolsForTest(t)
tests := []struct {
name string
url string
wantErr bool
}{
{name: "file scheme", url: "file:///etc/passwd", wantErr: true},
{name: "data scheme", url: "data:text/plain,secret", wantErr: true},
{name: "ftp scheme", url: "ftp://example.com/secret", wantErr: true},
{name: "http allowed", url: "http://example.com", wantErr: false},
{name: "https allowed", url: "https://example.com", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := (&WebFetch{}).Execute(t.Context(), coreagent.ToolContext{}, map[string]any{"url": tt.url})
if tt.wantErr && err == nil {
t.Fatal("expected unsupported scheme to be rejected")
}
// For allowed schemes we expect an error only from the missing
// server/auth path, not from scheme validation. The http/https
// cases reach the client and may fail on connection/auth; we only
// assert that the error is NOT a scheme error.
if !tt.wantErr && err != nil && strings.Contains(err.Error(), "unsupported URL scheme") {
t.Fatalf("http/https rejected as unsupported: %v", err)
}
})
if coreagent.ToolRequiresApproval(NewWebFetch(), map[string]any{"url": "https://ollama.com"}) {
t.Fatal("web fetch should not require approval")
}
}
func TestWebFetchBoundsContentBeforeReturning(t *testing.T) {
enableWebToolsForTest(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/experimental/web_fetch" {
t.Fatalf("path = %q, want /api/experimental/web_fetch", r.URL.Path)
@@ -183,7 +42,7 @@ func TestWebFetchBoundsContentBeforeReturning(t *testing.T) {
defer ts.Close()
t.Setenv("OLLAMA_HOST", ts.URL)
result, err := (&WebFetch{}).Execute(t.Context(), coreagent.ToolContext{}, map[string]any{
result, err := NewWebFetch().Execute(t.Context(), coreagent.ToolContext{}, map[string]any{
"url": "https://ollama.com",
})
if err != nil {
@@ -198,17 +57,3 @@ func TestWebFetchBoundsContentBeforeReturning(t *testing.T) {
t.Fatalf("captured content count = %d, want %d", count, maxWebFetchContentRunes)
}
}
func TestWebToolsRejectWhenCloudDisabled(t *testing.T) {
t.Setenv("OLLAMA_NO_CLOUD", "1")
for _, tt := range webToolCases {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.tool.Execute(t.Context(), coreagent.ToolContext{}, tt.args)
want := internalcloud.DisabledError(tt.operation)
if err == nil || err.Error() != want {
t.Fatalf("error = %v, want %q", err, want)
}
})
}
}
+19 -59
View File
@@ -217,31 +217,8 @@ type MessagesResponse struct {
// Usage contains token usage information
type Usage struct {
InputTokens int `json:"input_tokens"`
CacheReadInputTokens *int `json:"cache_read_input_tokens,omitempty"`
OutputTokens int `json:"output_tokens"`
}
// UsageFromMetrics separates total prompt tokens into uncached and cache-read counts.
func UsageFromMetrics(metrics api.Metrics) Usage {
total := max(0, metrics.PromptEvalCount)
var cached *int
if metrics.PromptEvalCachedCount != nil {
count := min(max(0, *metrics.PromptEvalCachedCount), total)
cached = &count
}
return Usage{
InputTokens: total - intValue(cached),
CacheReadInputTokens: cached,
OutputTokens: metrics.EvalCount,
}
}
func intValue(v *int) int {
if v == nil {
return 0
}
return *v
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
// Streaming event types
@@ -296,9 +273,8 @@ type MessageDelta struct {
// DeltaUsage contains cumulative token usage
type DeltaUsage struct {
InputTokens int `json:"input_tokens"`
CacheReadInputTokens *int `json:"cache_read_input_tokens,omitempty"`
OutputTokens int `json:"output_tokens"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
// MessageStopEvent signals the end of the message
@@ -712,7 +688,10 @@ func ToMessagesResponse(id string, r api.ChatResponse) MessagesResponse {
Model: r.Model,
Content: content,
StopReason: stopReason,
Usage: UsageFromMetrics(r.Metrics),
Usage: Usage{
InputTokens: r.Metrics.PromptEvalCount,
OutputTokens: r.Metrics.EvalCount,
},
}
}
@@ -742,7 +721,6 @@ type StreamConverter struct {
firstWrite bool
contentIndex int
inputTokens int
cacheReadTokens *int
outputTokens int
estimatedInputTokens int // Estimated tokens from request (used when actual metrics are 0)
thinkingStarted bool
@@ -774,10 +752,8 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
if c.firstWrite {
c.firstWrite = false
// Use actual metrics if available, otherwise use estimate
usage := UsageFromMetrics(r.Metrics)
c.inputTokens = usage.InputTokens
c.cacheReadTokens = usage.CacheReadInputTokens
if c.inputTokens == 0 && intValue(c.cacheReadTokens) == 0 && c.estimatedInputTokens > 0 {
c.inputTokens = r.Metrics.PromptEvalCount
if c.inputTokens == 0 && c.estimatedInputTokens > 0 {
c.inputTokens = c.estimatedInputTokens
}
@@ -792,9 +768,8 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
Model: c.Model,
Content: []ContentBlock{},
Usage: Usage{
InputTokens: c.inputTokens,
CacheReadInputTokens: c.cacheReadTokens,
OutputTokens: 0,
InputTokens: c.inputTokens,
OutputTokens: 0,
},
},
},
@@ -802,18 +777,6 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
}
if r.Message.Thinking != "" && !c.thinkingDone {
if c.textStarted {
events = append(events, StreamEvent{
Event: "content_block_stop",
Data: ContentBlockStopEvent{
Type: "content_block_stop",
Index: c.contentIndex,
},
})
c.contentIndex++
c.textStarted = false
}
if !c.thinkingStarted {
c.thinkingStarted = true
events = append(events, StreamEvent{
@@ -975,10 +938,8 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
})
}
usage := UsageFromMetrics(r.Metrics)
c.inputTokens = usage.InputTokens
c.cacheReadTokens = usage.CacheReadInputTokens
c.outputTokens = usage.OutputTokens
c.inputTokens = r.Metrics.PromptEvalCount
c.outputTokens = r.Metrics.EvalCount
stopReason := mapStopReason(r.DoneReason, len(c.toolCallsSent) > 0)
events = append(events, StreamEvent{
@@ -989,9 +950,8 @@ func (c *StreamConverter) Process(r api.ChatResponse) []StreamEvent {
StopReason: stopReason,
},
Usage: DeltaUsage{
InputTokens: c.inputTokens,
CacheReadInputTokens: c.cacheReadTokens,
OutputTokens: c.outputTokens,
InputTokens: c.inputTokens,
OutputTokens: c.outputTokens,
},
},
})
@@ -1103,7 +1063,7 @@ type CountTokensRequest struct {
// EstimateInputTokens estimates input tokens from a MessagesRequest (reuses CountTokensRequest logic)
func EstimateInputTokens(req MessagesRequest) int {
return EstimateCountTokens(CountTokensRequest{
return estimateTokens(CountTokensRequest{
Model: req.Model,
Messages: req.Messages,
System: req.System,
@@ -1117,10 +1077,10 @@ type CountTokensResponse struct {
InputTokens int `json:"input_tokens"`
}
// EstimateCountTokens returns a rough estimate of tokens (len/4).
// estimateTokens returns a rough estimate of tokens (len/4).
// TODO: Replace with actual tokenization via Tokenize API for accuracy.
// Current len/4 heuristic is a rough approximation (~4 chars/token average).
func EstimateCountTokens(req CountTokensRequest) int {
func estimateTokens(req CountTokensRequest) int {
var totalLen int
// Count system prompt
+11 -276
View File
@@ -3,7 +3,6 @@ package anthropic
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"testing"
@@ -16,10 +15,6 @@ const (
testImage = `iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=`
)
func testIntPtr(v int) *int {
return &v
}
// textContent is a convenience for constructing []ContentBlock with a single text block in tests.
func textContent(s string) []ContentBlock {
return []ContentBlock{{Type: "text", Text: &s}}
@@ -34,61 +29,6 @@ func makeArgs(kvs ...any) api.ToolCallFunctionArguments {
return args
}
func TestUsageFromMetricsBoundsCacheReads(t *testing.T) {
tests := []struct {
name string
metrics api.Metrics
want Usage
}{
{
name: "negative counts",
metrics: api.Metrics{PromptEvalCount: -1, PromptEvalCachedCount: testIntPtr(-2), EvalCount: 3},
want: Usage{CacheReadInputTokens: testIntPtr(0), OutputTokens: 3},
},
{
name: "cache reads exceed prompt",
metrics: api.Metrics{PromptEvalCount: 3, PromptEvalCachedCount: testIntPtr(5), EvalCount: 2},
want: Usage{CacheReadInputTokens: testIntPtr(3), OutputTokens: 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := cmp.Diff(tt.want, UsageFromMetrics(tt.metrics)); diff != "" {
t.Errorf("usage mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestUsageCacheReadJSON(t *testing.T) {
tests := []struct {
name string
count *int
want string
}{
{name: "unreported", want: `{"input_tokens":10,"output_tokens":2}`},
{name: "zero", count: testIntPtr(0), want: `{"input_tokens":10,"cache_read_input_tokens":0,"output_tokens":2}`},
{name: "positive", count: testIntPtr(4), want: `{"input_tokens":6,"cache_read_input_tokens":4,"output_tokens":2}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(UsageFromMetrics(api.Metrics{
PromptEvalCount: 10,
PromptEvalCachedCount: tt.count,
EvalCount: 2,
}))
if err != nil {
t.Fatal(err)
}
if got := string(data); got != tt.want {
t.Errorf("json = %s, want %s", got, tt.want)
}
})
}
}
func TestFromMessagesRequest_Basic(t *testing.T) {
req := MessagesRequest{
Model: "test-model",
@@ -204,118 +144,6 @@ func TestFromMessagesRequest_WithOptions(t *testing.T) {
}
}
func TestFromMessagesRequest_ClaudeAutoModeClassifierFixtures(t *testing.T) {
tests := []struct {
name string
request string
model string
maxTokens int
wantStop []string
wantSystem string
wantUser string
}{
{
name: "stage one local model",
request: `{
"model": "qwen3.5:latest",
"max_tokens": 2112,
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "<transcript>\n"},
{"type": "text", "text": "User: Run the safe test.\n"},
{"type": "text", "text": "Bash go test ./safe\n"},
{"type": "text", "text": "</transcript>\n"},
{"type": "text", "text": "Return only the stage-one block verdict."}
]
}],
"system": [
{
"type": "text",
"text": "Synthetic policy fixture. Evaluate whether the proposed action needs further review.",
"cache_control": {"type": "ephemeral"}
},
{"type": "text", "text": "Synthetic session context."}
],
"stop_sequences": ["</block>"]
}`,
model: "qwen3.5:latest",
maxTokens: 2112,
wantStop: []string{"</block>"},
wantSystem: "Synthetic policy fixture. Evaluate whether the proposed action needs further review.Synthetic session context.",
wantUser: "<transcript>\nUser: Run the safe test.\nBash go test ./safe\n</transcript>\nReturn only the stage-one block verdict.",
},
{
name: "stage two cloud model",
request: `{
"model": "glm-5.2:cloud",
"max_tokens": 10240,
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "<transcript>\n"},
{"type": "text", "text": "User: Send the fixture to an external host.\n"},
{"type": "text", "text": "Bash upload fixture.txt\n"},
{"type": "text", "text": "</transcript>\n"},
{"type": "text", "text": "Return the stage-two block verdict and reason."}
]
}],
"system": [
{
"type": "text",
"text": "Synthetic policy fixture. Evaluate whether the proposed action must be denied.",
"cache_control": {"type": "ephemeral"}
},
{"type": "text", "text": "Synthetic session context."}
]
}`,
model: "glm-5.2:cloud",
maxTokens: 10240,
wantSystem: "Synthetic policy fixture. Evaluate whether the proposed action must be denied.Synthetic session context.",
wantUser: "<transcript>\nUser: Send the fixture to an external host.\nBash upload fixture.txt\n</transcript>\nReturn the stage-two block verdict and reason.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var request MessagesRequest
if err := json.Unmarshal([]byte(tt.request), &request); err != nil {
t.Fatal(err)
}
converted, err := FromMessagesRequest(request)
if err != nil {
t.Fatal(err)
}
if converted.Model != tt.model {
t.Fatalf("model = %q, want exact selected model %q", converted.Model, tt.model)
}
if converted.Stream == nil || *converted.Stream {
t.Fatalf("stream = %v, want explicit non-streaming conversion", converted.Stream)
}
if len(converted.Tools) != 0 {
t.Fatalf("tools = %v, want tool-free classifier request", converted.Tools)
}
if got := converted.Options["num_predict"]; got != tt.maxTokens {
t.Fatalf("num_predict = %v, want %d", got, tt.maxTokens)
}
gotStop, _ := converted.Options["stop"].([]string)
if diff := cmp.Diff(tt.wantStop, gotStop); diff != "" {
t.Fatalf("stop sequences mismatch (-want +got):\n%s", diff)
}
if len(converted.Messages) != 2 {
t.Fatalf("messages = %+v, want system and user messages", converted.Messages)
}
if got := converted.Messages[0]; got.Role != "system" || got.Content != tt.wantSystem {
t.Fatalf("system message = %+v", got)
}
if got := converted.Messages[1]; got.Role != "user" || got.Content != tt.wantUser {
t.Fatalf("user message = %+v", got)
}
})
}
}
func TestFromMessagesRequest_WithImage(t *testing.T) {
imgData, _ := base64.StdEncoding.DecodeString(testImage)
@@ -920,9 +748,8 @@ func TestToMessagesResponse_Basic(t *testing.T) {
Done: true,
DoneReason: "stop",
Metrics: api.Metrics{
PromptEvalCount: 10,
PromptEvalCachedCount: testIntPtr(4),
EvalCount: 5,
PromptEvalCount: 10,
EvalCount: 5,
},
}
@@ -946,51 +773,9 @@ func TestToMessagesResponse_Basic(t *testing.T) {
if result.StopReason != "end_turn" {
t.Errorf("expected stop_reason 'end_turn', got %q", result.StopReason)
}
if result.Usage.InputTokens != 6 || intValue(result.Usage.CacheReadInputTokens) != 4 || result.Usage.OutputTokens != 5 {
if result.Usage.InputTokens != 10 || result.Usage.OutputTokens != 5 {
t.Errorf("unexpected usage: %+v", result.Usage)
}
data, err := json.Marshal(result.Usage)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), `"cache_read_input_tokens":4`) {
t.Errorf("unexpected usage json: %s", data)
}
}
func TestToMessagesResponse_PreservesClaudeAutoClassifierOutput(t *testing.T) {
for _, output := range []string{
"<block>no",
"<block>yes</block><category>Synthetic risk</category><reason>Denied by the synthetic fixture.</reason>",
"malformed classifier output",
} {
t.Run(output, func(t *testing.T) {
result := ToMessagesResponse("msg_classifier", api.ChatResponse{
Model: "qwen3.5:latest",
Message: api.Message{
Role: "assistant",
Content: output,
},
Done: true,
DoneReason: "stop",
Metrics: api.Metrics{
PromptEvalCount: 24644,
EvalCount: 300,
},
})
if result.Model != "qwen3.5:latest" || len(result.Content) != 1 || result.Content[0].Text == nil || *result.Content[0].Text != output {
t.Fatalf("classifier response = %+v, want opaque output on the selected model", result)
}
if result.StopReason != "end_turn" {
t.Fatalf("stop reason = %q, want end_turn", result.StopReason)
}
if result.Usage.InputTokens != 24644 || result.Usage.OutputTokens != 300 {
t.Fatalf("usage = %+v", result.Usage)
}
})
}
}
func TestToMessagesResponse_WithToolCalls(t *testing.T) {
@@ -1140,7 +925,7 @@ func TestStreamConverter_Basic(t *testing.T) {
Role: "assistant",
Content: "Hello",
},
Metrics: api.Metrics{PromptEvalCount: 10, PromptEvalCachedCount: testIntPtr(4)},
Metrics: api.Metrics{PromptEvalCount: 10},
}
events1 := conv.Process(resp1)
@@ -1168,7 +953,7 @@ func TestStreamConverter_Basic(t *testing.T) {
},
Done: true,
DoneReason: "stop",
Metrics: api.Metrics{PromptEvalCount: 10, PromptEvalCachedCount: testIntPtr(4), EvalCount: 5},
Metrics: api.Metrics{PromptEvalCount: 10, EvalCount: 5},
}
events2 := conv.Process(resp2)
@@ -1186,7 +971,7 @@ func TestStreamConverter_Basic(t *testing.T) {
t.Errorf("unexpected stop reason: %+v", data.Delta.StopReason)
}
if data.Usage.InputTokens != 6 || intValue(data.Usage.CacheReadInputTokens) != 4 || data.Usage.OutputTokens != 5 {
if data.Usage.InputTokens != 10 || data.Usage.OutputTokens != 5 {
t.Errorf("unexpected usage: %+v", data.Usage)
}
} else {
@@ -1355,56 +1140,6 @@ func TestStreamConverter_ThinkingDirectlyFollowedByToolCall(t *testing.T) {
}
}
func TestStreamConverter_TextBeforeThinking(t *testing.T) {
conv := NewStreamConverter("msg_123", "test-model", 0)
responses := []api.ChatResponse{
{Message: api.Message{Role: "assistant", Content: "---\n"}},
{Message: api.Message{Role: "assistant", Thinking: "Let me think."}},
{
Message: api.Message{Role: "assistant", Content: "The answer."},
Done: true,
DoneReason: "stop",
Metrics: api.Metrics{PromptEvalCount: 10, EvalCount: 5},
},
}
var got []string
for _, response := range responses {
for _, event := range conv.Process(response) {
switch data := event.Data.(type) {
case ContentBlockStartEvent:
got = append(got, fmt.Sprintf("%s:%s:%d", event.Event, data.ContentBlock.Type, data.Index))
case ContentBlockDeltaEvent:
got = append(got, fmt.Sprintf("%s:%s:%d", event.Event, data.Delta.Type, data.Index))
case ContentBlockStopEvent:
got = append(got, fmt.Sprintf("%s:%d", event.Event, data.Index))
default:
got = append(got, event.Event)
}
}
}
want := []string{
"message_start",
"content_block_start:text:0",
"content_block_delta:text_delta:0",
"content_block_stop:0",
"content_block_start:thinking:1",
"content_block_delta:thinking_delta:1",
"content_block_stop:1",
"content_block_start:text:2",
"content_block_delta:text_delta:2",
"content_block_stop:2",
"message_delta",
"message_stop",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("unexpected stream events (-want +got):\n%s", diff)
}
}
func TestStreamConverter_ToolCallWithUnmarshalableArgs(t *testing.T) {
// Test that unmarshalable arguments (like channels) are handled gracefully
// and don't cause a panic or corrupt stream
@@ -1760,7 +1495,7 @@ func TestEstimateTokens_SimpleMessage(t *testing.T) {
},
}
tokens := EstimateCountTokens(req)
tokens := estimateTokens(req)
// "user" (4) + "Hello, world!" (13) = 17 chars / 4 = 4 tokens
if tokens < 1 {
@@ -1781,7 +1516,7 @@ func TestEstimateTokens_WithSystemPrompt(t *testing.T) {
},
}
tokens := EstimateCountTokens(req)
tokens := estimateTokens(req)
// System prompt adds to count
if tokens < 5 {
@@ -1804,7 +1539,7 @@ func TestEstimateTokens_WithTools(t *testing.T) {
},
}
tokens := EstimateCountTokens(req)
tokens := estimateTokens(req)
// Tools add significant content
if tokens < 10 {
@@ -1833,7 +1568,7 @@ func TestEstimateTokens_WithThinking(t *testing.T) {
},
}
tokens := EstimateCountTokens(req)
tokens := estimateTokens(req)
// Thinking content should be counted
if tokens < 10 {
@@ -1847,7 +1582,7 @@ func TestEstimateTokens_EmptyContent(t *testing.T) {
Messages: []MessageParam{},
}
tokens := EstimateCountTokens(req)
tokens := estimateTokens(req)
if tokens != 0 {
t.Errorf("expected 0 tokens for empty content, got %d", tokens)
-59
View File
@@ -2,7 +2,6 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -389,64 +388,6 @@ func TestClientWebSearchExperimentalUsesLocalRoute(t *testing.T) {
}
}
func TestClientWebSearchExperimentalErrors(t *testing.T) {
tests := []struct {
name string
status int
body string
assertError func(*testing.T, error)
}{
{
name: "unauthorized retains sign in URL",
status: http.StatusUnauthorized,
body: `{"error":"unauthorized","signin_url":"https://ollama.com/signin/example"}`,
assertError: func(t *testing.T, err error) {
t.Helper()
var authErr AuthorizationError
if !errors.As(err, &authErr) {
t.Fatalf("error = %T, want AuthorizationError", err)
}
if authErr.StatusCode != http.StatusUnauthorized || authErr.SigninURL != "https://ollama.com/signin/example" {
t.Fatalf("authorization error = %#v", authErr)
}
},
},
{
name: "rate limit retains status",
status: http.StatusTooManyRequests,
body: `{"error":"rate limit exceeded"}`,
assertError: func(t *testing.T, err error) {
t.Helper()
var statusErr StatusError
if !errors.As(err, &statusErr) {
t.Fatalf("error = %T, want StatusError", err)
}
if statusErr.StatusCode != http.StatusTooManyRequests || statusErr.ErrorMessage != "rate limit exceeded" {
t.Fatalf("status error = %#v", statusErr)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.status)
_, _ = w.Write([]byte(tt.body))
}))
defer ts.Close()
client := NewClient(&url.URL{Scheme: "http", Host: ts.Listener.Addr().String()}, http.DefaultClient)
_, err := client.WebSearchExperimental(t.Context(), &WebSearchRequest{Query: "ollama"})
if err == nil {
t.Fatal("expected error")
}
tt.assertError(t, err)
})
}
}
func TestClientWebFetchExperimentalUsesLocalRoute(t *testing.T) {
var gotPath string
var gotMethod string
-74
View File
@@ -1,74 +0,0 @@
package api
import (
"encoding/json"
"io"
"os"
"strings"
"testing"
"time"
)
func TestMetricsCachedPromptJSON(t *testing.T) {
tests := []struct {
name string
count *int
want string
}{
{name: "unreported", want: `{}`},
{name: "zero", count: testIntPtr(0), want: `{"prompt_eval_cached_count":0}`},
{name: "positive", count: testIntPtr(4), want: `{"prompt_eval_cached_count":4}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(Metrics{PromptEvalCachedCount: tt.count})
if err != nil {
t.Fatal(err)
}
if got := string(data); got != tt.want {
t.Errorf("json = %s, want %s", got, tt.want)
}
var metrics Metrics
if err := json.Unmarshal(data, &metrics); err != nil {
t.Fatal(err)
}
if tt.count == nil {
if metrics.PromptEvalCachedCount != nil {
t.Errorf("cached count = %v, want nil", metrics.PromptEvalCachedCount)
}
} else if metrics.PromptEvalCachedCount == nil || *metrics.PromptEvalCachedCount != *tt.count {
t.Errorf("cached count = %v, want %d", metrics.PromptEvalCachedCount, *tt.count)
}
})
}
}
func TestMetricsSummaryCachedPromptTokens(t *testing.T) {
read, write, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
original := os.Stderr
os.Stderr = write
t.Cleanup(func() { os.Stderr = original })
(&Metrics{
PromptEvalCount: 10,
PromptEvalCachedCount: testIntPtr(4),
PromptEvalDuration: time.Second,
}).Summary()
write.Close()
os.Stderr = original
output, err := io.ReadAll(read)
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"prompt eval count: 10 token(s)", "prompt eval cached: 4 token(s)", "prompt eval rate: 6.00 tokens/s"} {
if !strings.Contains(string(output), want) {
t.Errorf("summary missing %q:\n%s", want, output)
}
}
}
+44 -58
View File
@@ -127,6 +127,20 @@ type GenerateRequest struct {
// each with an associated log probability. Only applies when Logprobs is true.
// Valid values are 0-20. Default is 0 (only return the selected token's logprob).
TopLogprobs int `json:"top_logprobs,omitempty"`
// Experimental: Image generation fields (may change or be removed)
// Width is the width of the generated image in pixels.
// Only used for image generation models.
Width int32 `json:"width,omitempty"`
// Height is the height of the generated image in pixels.
// Only used for image generation models.
Height int32 `json:"height,omitempty"`
// Steps is the number of diffusion steps for image generation.
// Only used for image generation models.
Steps int32 `json:"steps,omitempty"`
}
// ChatRequest describes a request sent by [Client.Chat].
@@ -555,13 +569,12 @@ type DebugInfo struct {
}
type Metrics struct {
TotalDuration time.Duration `json:"total_duration,omitempty"`
LoadDuration time.Duration `json:"load_duration,omitempty"`
PromptEvalCount int `json:"prompt_eval_count,omitempty"`
PromptEvalCachedCount *int `json:"prompt_eval_cached_count,omitempty"`
PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
EvalCount int `json:"eval_count,omitempty"`
EvalDuration time.Duration `json:"eval_duration,omitempty"`
TotalDuration time.Duration `json:"total_duration,omitempty"`
LoadDuration time.Duration `json:"load_duration,omitempty"`
PromptEvalCount int `json:"prompt_eval_count,omitempty"`
PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
EvalCount int `json:"eval_count,omitempty"`
EvalDuration time.Duration `json:"eval_duration,omitempty"`
}
// Options specified in [GenerateRequest]. If you add a new option here, also
@@ -693,11 +706,8 @@ type CreateRequest struct {
// Messages is a list of messages added to the model before chat and generation requests.
Messages []Message `json:"messages,omitempty"`
// Renderer is the name of the renderer used when constructing a request to the model.
Renderer string `json:"renderer,omitempty"`
// Parser is the name of the parser used to parse the output of the request.
Parser string `json:"parser,omitempty"`
Parser string `json:"parser,omitempty"`
// Requires is the minimum version of Ollama required by the model.
Requires string `json:"requires,omitempty"`
@@ -801,46 +811,17 @@ type ListResponse struct {
// ModelRecommendationsResponse is the response from [Client.ModelRecommendationsExperimental].
type ModelRecommendationsResponse struct {
Recommendations []ModelRecommendation `json:"recommendations"`
Mappings *ModelRecommendationMappings `json:"mappings,omitempty"`
Recommendations []ModelRecommendation `json:"recommendations"`
}
// ModelRecommendationMapping defines one app-specific route preference.
type ModelRecommendationMapping struct {
Model string `json:"model"`
RequiredPlan string `json:"required_plan,omitempty"`
}
// ModelRecommendationMappings defines the app-specific model routes.
type ModelRecommendationMappings map[string]ModelRecommendationMapping
// ModelRecommendation is a single recommendation entry in [ModelRecommendationsResponse].
type ModelRecommendation struct {
Model string `json:"model"`
Description string `json:"description"`
ContextLength int `json:"context_length,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
VRAMBytes int64 `json:"vram_bytes,omitempty"`
RequiredPlan string `json:"required_plan,omitempty"`
Thinking *ModelRecommendationThinking `json:"thinking,omitempty"`
}
// ModelRecommendationThinking advertises the exact values accepted by
// Ollama's think field and the model's default. Values may be booleans for
// binary thinking controls or strings for adjustable effort levels.
type ModelRecommendationThinking struct {
Values []any `json:"values,omitempty"`
Default any `json:"default,omitempty"`
}
// Clone returns an independent copy.
func (t *ModelRecommendationThinking) Clone() *ModelRecommendationThinking {
if t == nil {
return nil
}
clone := *t
clone.Values = append([]any(nil), t.Values...)
return &clone
Model string `json:"model"`
Description string `json:"description"`
ContextLength int `json:"context_length,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
VRAMBytes int64 `json:"vram_bytes,omitempty"`
RequiredPlan string `json:"required_plan,omitempty"`
}
// ProcessResponse is the response from [Client.Process].
@@ -957,6 +938,20 @@ type GenerateResponse struct {
// Logprobs contains log probability information for the generated tokens,
// if requested via the Logprobs parameter.
Logprobs []Logprob `json:"logprobs,omitempty"`
// Experimental: Image generation fields (may change or be removed)
// Image contains a base64-encoded generated image.
// Only present for image generation models.
Image string `json:"image,omitempty"`
// Completed is the number of completed steps in image generation.
// Only present for image generation models during streaming.
Completed int64 `json:"completed,omitempty"`
// Total is the total number of steps for image generation.
// Only present for image generation models during streaming.
Total int64 `json:"total,omitempty"`
}
// ModelDetails provides details about a model.
@@ -1003,18 +998,9 @@ func (m *Metrics) Summary() {
fmt.Fprintf(os.Stderr, "prompt eval count: %d token(s)\n", m.PromptEvalCount)
}
cached := 0
if m.PromptEvalCachedCount != nil {
cached = *m.PromptEvalCachedCount
}
if cached > 0 {
fmt.Fprintf(os.Stderr, "prompt eval cached: %d token(s)\n", cached)
}
if m.PromptEvalDuration > 0 {
fmt.Fprintf(os.Stderr, "prompt eval duration: %s\n", m.PromptEvalDuration)
uncached := max(0, m.PromptEvalCount-cached)
fmt.Fprintf(os.Stderr, "prompt eval rate: %.2f tokens/s\n", float64(uncached)/m.PromptEvalDuration.Seconds())
fmt.Fprintf(os.Stderr, "prompt eval rate: %.2f tokens/s\n", float64(m.PromptEvalCount)/m.PromptEvalDuration.Seconds())
}
if m.EvalCount > 0 {
@@ -1144,7 +1130,7 @@ func DefaultOptions() Options {
TopP: 0.9,
TypicalP: 1.0,
RepeatLastN: 64,
RepeatPenalty: 1.0,
RepeatPenalty: 1.1,
PresencePenalty: 0.0,
FrequencyPenalty: 0.0,
Seed: -1,
-19
View File
@@ -4,12 +4,9 @@ import (
"encoding/json"
"errors"
"math"
"reflect"
"strings"
"testing"
"time"
"github.com/ollama/ollama/types/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -216,22 +213,6 @@ func TestMainGPUParsingFromJSON(t *testing.T) {
}
}
func TestGenerationDefaultMappingsAreOptions(t *testing.T) {
jsonOpts := make(map[string]struct{})
for _, field := range reflect.VisibleFields(reflect.TypeOf(Options{})) {
jsonTag := strings.Split(field.Tag.Get("json"), ",")[0]
if jsonTag != "" {
jsonOpts[jsonTag] = struct{}{}
}
}
for _, option := range model.GenerationDefaultOptions() {
if _, ok := jsonOpts[option]; !ok {
t.Fatalf("%s should be defined on api.Options", option)
}
}
}
func TestUseMmapFormatParams(t *testing.T) {
tr := true
fa := false
+22 -64
View File
@@ -146,10 +146,15 @@ func main() {
// Do this after logging is set up so we can debug issues
if runtime.GOOS == "windows" && urlSchemeRequest != "" {
slog.Debug("checking for existing instance", "url", urlSchemeRequest)
// This exits after forwarding the request when another instance is
// running. First-instance requests are handled later by osRun, after the
// Windows UI dependencies are initialized and from the primary thread.
checkAndHandleExistingInstance(urlSchemeRequest)
if checkAndHandleExistingInstance(urlSchemeRequest) {
// The function will exit if it successfully sends to another instance
// If we reach here, we're the first/only instance
} else {
// No existing instance found, handle the URL scheme in this instance
go func() {
handleURLSchemeInCurrentInstance(urlSchemeRequest)
}()
}
}
// Detect if this is a first start after an upgrade, in
@@ -175,9 +180,7 @@ func main() {
// Check if another instance is already running
// On Windows, focus the existing instance; on other platforms, kill it
if !handleExistingInstance(startHidden) {
return
}
handleExistingInstance(startHidden)
// on macOS, offer the user to create a symlink
// from /usr/local/bin/ollama to the app bundle
@@ -202,12 +205,6 @@ func main() {
uiServerPort = port
st := &store.Store{}
if devMode {
if dbPath := strings.TrimSpace(os.Getenv("OLLAMA_APP_DB_PATH")); dbPath != "" {
st.DBPath = dbPath
slog.Debug("using development app database", "path", dbPath)
}
}
appStore = st
// Enable CORS in development mode
@@ -327,11 +324,11 @@ func main() {
quit()
}()
if urlSchemeRequest != "" && runtime.GOOS != "windows" {
if urlSchemeRequest != "" {
go func() {
handleURLSchemeInCurrentInstance(urlSchemeRequest)
}()
} else if urlSchemeRequest == "" {
} else {
slog.Debug("no URL scheme request to handle")
}
@@ -346,13 +343,7 @@ func main() {
}
}()
settings, settingsErr := st.Settings()
showOnboarding := shouldShowOnboarding(settings, settingsErr)
if settingsErr != nil {
slog.Error("failed to load onboarding state", "error", settingsErr)
}
osRun(cancel, hasCompletedFirstRun, startHidden, showOnboarding, urlSchemeRequest)
osRun(cancel, hasCompletedFirstRun, startHidden)
slog.Info("shutting down desktop server")
if err := srv.Close(); err != nil {
@@ -364,33 +355,6 @@ func main() {
<-done
}
func shouldShowOnboarding(settings store.Settings, err error) bool {
return err != nil || settings.OnboardingVersion < store.CurrentOnboardingVersion
}
func runInitialWindowsUI(
startHidden bool,
showOnboarding bool,
urlSchemeRequest string,
startHiddenFn func(),
handleURLFn func(string),
showUIFn func(string),
) {
if urlSchemeRequest != "" {
handleURLFn(urlSchemeRequest)
return
}
if startHidden {
startHiddenFn()
return
}
if showOnboarding {
showUIFn("/")
return
}
showUIFn("/connect")
}
func startHiddenTasks() {
// If an upgrade is ready and we're in hidden mode, perform it at startup.
// If we're not in hidden mode, we want to start as fast as possible and not
@@ -411,7 +375,7 @@ func startHiddenTasks() {
return
}
if err := updater.DoUpgradeAtStartup(); err != nil { //nolint:staticcheck,nolintlint // DoUpgradeAtStartup may always return non-nil on Windows
if err := updater.DoUpgradeAtStartup(); err != nil {
slog.Info("unable to perform upgrade at startup", "error", err)
// Make sure the restart to upgrade menu shows so we can attempt an interactive upgrade to get authorization
UpdateAvailable("")
@@ -468,7 +432,7 @@ func checkUserLoggedIn(uiServerPort int) bool {
func handleConnectURLScheme() {
if checkUserLoggedIn(uiServerPort) {
slog.Info("user is already logged in, opening app instead")
openUI("/")
showWindow(wv.webview.Window())
return
}
@@ -527,23 +491,17 @@ func parseURLScheme(urlSchemeRequest string) (isConnect bool, err error) {
// handleURLSchemeInCurrentInstance processes URL scheme requests in the current instance
func handleURLSchemeInCurrentInstance(urlSchemeRequest string) {
err := dispatchURLSchemeRequest(urlSchemeRequest, handleConnectURLScheme, func() {
openUI("/")
})
if err != nil {
slog.Error("failed to parse URL scheme request", "url", urlSchemeRequest, "error", err)
}
}
func dispatchURLSchemeRequest(urlSchemeRequest string, connect, open func()) error {
isConnect, err := parseURLScheme(urlSchemeRequest)
if err != nil {
return err
slog.Error("failed to parse URL scheme request", "url", urlSchemeRequest, "error", err)
return
}
if isConnect {
connect()
handleConnectURLScheme()
} else {
open()
if wv.webview != nil {
showWindow(wv.webview.Window())
}
}
return nil
}
+17 -1778
View File
File diff suppressed because it is too large. Load diff
+2 -40
View File
@@ -1,7 +1,5 @@
#import <Cocoa/Cocoa.h>
#import <Security/Security.h>
#include <stddef.h>
#include <stdint.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
@@ -18,12 +16,8 @@ enum AppMove
MoveError,
};
void run(bool showOnboarding, bool startHidden);
typedef struct {
int pid;
int64_t started_at;
} AppProcessIdentity;
bool otherOllamaProcesses(AppProcessIdentity **processes, size_t *count);
void run(bool firstTimeRun, bool startHidden);
void killOtherInstances();
enum AppMove askToMoveToApplications();
int createSymlinkWithAuthorization();
int installSymlink(const char *cliPath);
@@ -31,7 +25,6 @@ extern void Restart();
// extern void Quit();
void StartUI(const char *path);
void ShowUI();
bool IsOnboardingActive(void);
void StopUI();
void StartUpdate();
void darwinStartHiddenTasks();
@@ -45,37 +38,6 @@ void setWindowDelegate(void *window);
void showWindow(uintptr_t wndPtr);
void hideWindow(uintptr_t wndPtr);
void styleWindow(uintptr_t wndPtr);
void setWindowResizable(uintptr_t wndPtr, bool resizable);
void drag(uintptr_t wndPtr);
void doubleClick(uintptr_t wndPtr);
void handleConnectURL();
bool SetClaudeGatewayInstalled(bool installed, bool restartClaude);
bool HasUsedClaudeDesktopIntegration(void);
bool RestoreClaudeGatewayForShutdown(void);
bool IsClaudeGatewayConfigured(void);
bool IsClaudeDesktopInstalled(void);
bool IsClaudeDesktopRunning(void);
bool IsCodexDesktopInstalled(void);
bool IsCodexDesktopConnected(void);
bool IsCodexDesktopRunning(void);
unsigned long long CodexDesktopRequestCount(void);
bool SetCodexDesktopConnected(bool connected, bool restartConfirmed);
bool ClaudeGatewayStartFailed(void);
bool ClaudeGatewayPortConflict(void);
char *ClaudeGatewayErrorMessage(void);
int ClaudeGatewayPort(void);
void RefreshClaudeProxyMenu(void);
void updateClaudeProxyMenu(unsigned long long routed);
bool ShowAppsInMenu(void);
void SetShowAppsInMenu(bool visible);
enum ClaudeInstallResult
{
ClaudeInstallCancelled,
ClaudeInstallerOpened,
ClaudeInstallFailed,
};
enum ClaudeInstallResult installClaudeDesktop(void);
enum ClaudeInstallResult installCodexDesktop(void);
char *ClaudeDesktopDownloadRequest(char **authorization);
bool InstallClaudeDesktopArchive(const char *archivePath);
bool InstallCodexDesktopDiskImage(const char *imagePath);
+75 -1436
View File
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-140
View File
@@ -1,140 +0,0 @@
//go:build windows || darwin
package main
import (
"errors"
"testing"
"github.com/ollama/ollama/app/store"
)
func TestShouldShowOnboarding(t *testing.T) {
tests := []struct {
name string
settings store.Settings
err error
want bool
}{
{
name: "fresh install",
settings: store.Settings{OnboardingVersion: 0},
want: true,
},
{
name: "completed onboarding",
settings: store.Settings{OnboardingVersion: store.CurrentOnboardingVersion},
want: false,
},
{
name: "settings failure",
err: errors.New("settings unavailable"),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldShowOnboarding(tt.settings, tt.err); got != tt.want {
t.Fatalf("shouldShowOnboarding() = %v, want %v", got, tt.want)
}
})
}
}
func TestDispatchURLSchemeRequest(t *testing.T) {
tests := []struct {
name string
request string
wantConnect bool
wantOpen bool
wantErr bool
}{
{name: "bare URL opens app", request: "ollama://", wantOpen: true},
{name: "connect URL starts connection", request: "ollama://connect", wantConnect: true},
{name: "unsupported URL", request: "ollama://unsupported", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
connected := false
opened := false
err := dispatchURLSchemeRequest(
tt.request,
func() { connected = true },
func() { opened = true },
)
if (err != nil) != tt.wantErr {
t.Fatalf("dispatchURLSchemeRequest() error = %v, wantErr %v", err, tt.wantErr)
}
if connected != tt.wantConnect {
t.Errorf("connect called = %v, want %v", connected, tt.wantConnect)
}
if opened != tt.wantOpen {
t.Errorf("open called = %v, want %v", opened, tt.wantOpen)
}
})
}
}
func TestRunInitialWindowsUIWithBareURL(t *testing.T) {
hiddenCalls := 0
urlCalls := 0
onboardingCalls := 0
openCalls := 0
runInitialWindowsUI(
false,
true,
"ollama://",
func() { hiddenCalls++ },
func(request string) {
urlCalls++
if err := dispatchURLSchemeRequest(request, func() {}, func() { openCalls++ }); err != nil {
t.Fatalf("dispatchURLSchemeRequest() error = %v", err)
}
},
func(path string) {
onboardingCalls++
},
)
if urlCalls != 1 {
t.Fatalf("URL handled %d times, want 1", urlCalls)
}
if openCalls != 1 {
t.Errorf("app opened %d times, want 1", openCalls)
}
if hiddenCalls != 0 {
t.Errorf("hidden startup called %d times, want 0", hiddenCalls)
}
if onboardingCalls != 0 {
t.Errorf("onboarding opened %d times, want 0", onboardingCalls)
}
}
func TestRunInitialWindowsUIRoutesInteractiveLaunch(t *testing.T) {
for _, tt := range []struct {
name string
showOnboarding bool
wantPath string
}{
{name: "fresh install preserves onboarding", showOnboarding: true, wantPath: "/"},
{name: "returning launch opens apps", wantPath: "/connect"},
} {
t.Run(tt.name, func(t *testing.T) {
var gotPath string
runInitialWindowsUI(
false,
tt.showOnboarding,
"",
func() { t.Fatal("unexpected hidden startup") },
func(string) { t.Fatal("unexpected URL handling") },
func(path string) { gotPath = path },
)
if gotPath != tt.wantPath {
t.Fatalf("initial UI path = %q, want %q", gotPath, tt.wantPath)
}
})
}
}
+30 -23
View File
@@ -74,12 +74,11 @@ func maybeMoveAndRestart() appMove {
}
// handleExistingInstance checks for existing instances and optionally focuses them
func handleExistingInstance(startHidden bool) bool {
func handleExistingInstance(startHidden bool) {
if wintray.CheckAndFocusExistingInstance(!startHidden) {
slog.Info("existing instance found, exiting")
os.Exit(0)
}
return true
}
func installSymlink() {}
@@ -96,15 +95,11 @@ func (ac *appCallbacks) UIRun(path string) {
}
func (*appCallbacks) UIShow() {
openUI("/")
}
func openUI(path string) {
if wv.IsRunning() && wv.webview != nil {
if wv.webview != nil {
showWindow(wv.webview.Window())
return
} else {
wv.Run("/")
}
wv.Run(path)
}
func (*appCallbacks) UITerminate() {
@@ -115,10 +110,6 @@ func (*appCallbacks) UIRunning() bool {
return wv.IsRunning()
}
func (*appCallbacks) UIOnboarding() bool {
return wv.OnboardingActive()
}
func (app *appCallbacks) Quit() {
app.t.Quit()
wv.Terminate()
@@ -135,7 +126,7 @@ func (app *appCallbacks) DoUpdate() {
app.shutdown()
if err := updater.DoUpgrade(true); err != nil { //nolint:staticcheck,nolintlint // DoUpgrade may always return non-nil on Windows
if err := updater.DoUpgrade(true); err != nil {
slog.Warn(fmt.Sprintf("upgrade attempt failed: %s", err))
}
}
@@ -147,7 +138,19 @@ func (app *appCallbacks) HandleURLScheme(urlScheme string) {
// handleURLSchemeRequest processes URL scheme requests from other instances
func handleURLSchemeRequest(urlScheme string) {
handleURLSchemeInCurrentInstance(urlScheme)
isConnect, err := parseURLScheme(urlScheme)
if err != nil {
slog.Error("failed to parse URL scheme request", "url", urlScheme, "error", err)
return
}
if isConnect {
handleConnectURLScheme()
} else {
if wv.webview != nil {
showWindow(wv.webview.Window())
}
}
}
func UpdateAvailable(ver string) error {
@@ -158,7 +161,7 @@ func UpdateAvailable(ver string) error {
return app.t.UpdateAvailable(ver)
}
func osRun(shutdown func(), hasCompletedFirstRun, startHidden, showOnboarding bool, urlSchemeRequest string) {
func osRun(shutdown func(), hasCompletedFirstRun, startHidden bool) {
var err error
app.shutdown = shutdown
app.t, err = wintray.NewTray(app)
@@ -202,8 +205,10 @@ func osRun(shutdown func(), hasCompletedFirstRun, startHidden, showOnboarding bo
}
}
}
runInitialWindowsUI(startHidden, showOnboarding, urlSchemeRequest, startHiddenTasks, handleURLSchemeInCurrentInstance, func(path string) {
ptr := wv.Run(path)
if startHidden {
startHiddenTasks()
} else {
ptr := wv.Run("/")
// Set the window icon using the tray icon
if ptr != nil {
@@ -220,7 +225,7 @@ func osRun(shutdown func(), hasCompletedFirstRun, startHidden, showOnboarding bo
}
centerWindow(ptr)
})
}
if !hasCompletedFirstRun {
// Only create the login shortcut on first start
@@ -403,8 +408,6 @@ func hideWindow(ptr unsafe.Pointer) {
}
}
func setOnboardingWindowStyle(_ unsafe.Pointer, _ bool) {}
func runInBackground() {
exe, err := os.Executable()
if err != nil {
@@ -429,13 +432,17 @@ func drag(ptr unsafe.Pointer) {}
func doubleClick(ptr unsafe.Pointer) {}
// checkAndHandleExistingInstance checks if another instance is running and sends the URL to it
func checkAndHandleExistingInstance(urlSchemeRequest string) {
func checkAndHandleExistingInstance(urlSchemeRequest string) bool {
if urlSchemeRequest == "" {
return
return false
}
// Try to send URL to existing instance using wintray messaging
if wintray.CheckAndSendToExistingInstance(urlSchemeRequest) {
os.Exit(0)
return true
}
// No existing instance, we'll handle it ourselves
return false
}
@@ -1,98 +0,0 @@
//go:build darwin
package main
import (
"errors"
"github.com/ollama/ollama/app/webview"
"github.com/ollama/ollama/cmd/launch"
)
func bindClaudeDesktop(wv webview.WebView) {
wv.Bind("getClaudeDesktopStatus", func() claudeDesktopStatus {
return getClaudeDesktopConnectionStatus()
})
wv.Bind("getClaudeDesktopConnectionSummary", func() claudeDesktopStatus {
return getClaudeDesktopConnectionSummary()
})
wv.Bind("getClaudeDesktopRequestCount", func() uint64 {
return claudeDesktopRequestCount()
})
wv.Bind("setClaudeDesktopConnected", func(enabled, restartConfirmed bool) claudeDesktopActionResult {
err := setClaudeDesktopConnection(enabled, restartConfirmed)
result := claudeDesktopActionResult{
Status: getClaudeDesktopConnectionSummary(),
}
if err != nil {
result.Error = err.Error()
}
return result
})
wv.Bind("prepareClaudeDesktopConnection", func() claudeDesktopActionResult {
err := prepareClaudeDesktopConnection()
result := claudeDesktopActionResult{
Status: getClaudeDesktopConnectionSummary(),
}
if err != nil {
result.Error = err.Error()
}
return result
})
wv.Bind("openClaudeDesktop", func() string {
if err := openClaudeDesktopApplication(); err != nil {
return err.Error()
}
return ""
})
wv.Bind("installClaudeDesktop", func() claudeDesktopInstallResult {
return requestClaudeDesktopInstall()
})
wv.Bind("applyClaudeDesktopMappings", func(mappings map[string]string, restartConfirmed bool) claudeDesktopActionResult {
applied, err := applyClaudeDesktopMappings(mappings, restartConfirmed)
result := claudeDesktopActionResult{
Status: getClaudeDesktopConnectionStatus(),
MappingsApplied: applied,
}
if err != nil {
result.Error = err.Error()
result.RestartConfirmationRequired = errors.Is(err, launch.ErrClaudeDesktopRestartConfirmationRequired)
}
return result
})
wv.Bind("resetClaudeDesktopMappings", func(restartConfirmed bool) claudeDesktopActionResult {
applied, err := resetClaudeDesktopMappings(restartConfirmed)
result := claudeDesktopActionResult{
Status: getClaudeDesktopConnectionStatus(),
MappingsApplied: applied,
}
if err != nil {
result.Error = err.Error()
result.RestartConfirmationRequired = errors.Is(err, launch.ErrClaudeDesktopRestartConfirmationRequired)
}
return result
})
wv.Bind("setClaudeDesktopAutoMode", func(enabled, restartConfirmed bool) claudeDesktopActionResult {
err := setClaudeDesktopAutoMode(enabled, restartConfirmed)
result := claudeDesktopActionResult{Status: getClaudeDesktopConnectionStatus()}
if err != nil {
result.Error = err.Error()
result.RestartConfirmationRequired = errors.Is(err, launch.ErrClaudeDesktopRestartConfirmationRequired)
}
return result
})
wv.Bind("getShowAppsInMenu", func() bool {
return getShowAppsInMenu()
})
wv.Bind("setShowAppsInMenu", func(visible bool) {
setShowAppsInMenu(visible)
})
}
@@ -1,7 +0,0 @@
//go:build windows
package main
import "github.com/ollama/ollama/app/webview"
func bindClaudeDesktop(_ webview.WebView) {}
@@ -1,252 +0,0 @@
//go:build darwin
package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
maxClaudeDesktopArchiveBytes = 1 << 30
maxClaudeDesktopExtractBytes = 2 << 30
maxClaudeDesktopArchiveFiles = 100_000
claudeDesktopBundleID = "com.anthropic.claudefordesktop"
claudeDesktopTeamID = "Q6L2SF6YDW"
)
var errClaudeDesktopDestinationExists = errors.New("Claude Desktop installation destination already exists")
func claudeDesktopInstallDestinations() []string {
destinations := []string{"/Applications/Claude.app"}
if home, err := os.UserHomeDir(); err == nil {
destinations = append(destinations, filepath.Join(home, "Applications", "Claude.app"))
}
return destinations
}
func installClaudeDesktopZip(archivePath string, destinations []string, verify func(string) error) (string, error) {
if len(destinations) == 0 {
return "", errors.New("Claude Desktop installation destination is required")
}
if verify == nil {
return "", errors.New("Claude Desktop bundle verifier is required")
}
info, err := os.Stat(archivePath)
if err != nil {
return "", fmt.Errorf("stat Claude Desktop archive: %w", err)
}
if !info.Mode().IsRegular() {
return "", errors.New("Claude Desktop archive is not a regular file")
}
if info.Size() > maxClaudeDesktopArchiveBytes {
return "", fmt.Errorf("Claude Desktop archive exceeds %d bytes", maxClaudeDesktopArchiveBytes)
}
workDir, err := os.MkdirTemp("", "ollama-claude-install-")
if err != nil {
return "", fmt.Errorf("create Claude Desktop installation directory: %w", err)
}
defer os.RemoveAll(workDir)
if err := extractClaudeDesktopZip(archivePath, workDir); err != nil {
return "", err
}
bundlePath := filepath.Join(workDir, "Claude.app")
if err := validateClaudeDesktopBundle(bundlePath); err != nil {
return "", err
}
if err := verify(bundlePath); err != nil {
return "", fmt.Errorf("verify Claude Desktop signature: %w", err)
}
var permissionErr error
for _, destination := range destinations {
if strings.TrimSpace(destination) == "" {
continue
}
if _, err := os.Stat(destination); err == nil {
return "", fmt.Errorf("%w: %s", errClaudeDesktopDestinationExists, destination)
} else if !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("check Claude Desktop destination %s: %w", destination, err)
}
if err := os.MkdirAll(filepath.Dir(destination), 0o755); err != nil {
if errors.Is(err, os.ErrPermission) {
permissionErr = err
continue
}
return "", fmt.Errorf("create Claude Desktop destination: %w", err)
}
if err := os.Rename(bundlePath, destination); err != nil {
if errors.Is(err, os.ErrPermission) {
permissionErr = err
continue
}
return "", fmt.Errorf("move Claude Desktop to %s: %w", destination, err)
}
return destination, nil
}
if permissionErr != nil {
return "", fmt.Errorf("install Claude Desktop in Applications: %w", permissionErr)
}
return "", errors.New("Claude Desktop installation destination is required")
}
func extractClaudeDesktopZip(archivePath, destination string) error {
reader, err := zip.OpenReader(archivePath)
if err != nil {
return fmt.Errorf("open Claude Desktop archive: %w", err)
}
defer reader.Close()
if len(reader.File) == 0 {
return errors.New("Claude Desktop archive is empty")
}
if len(reader.File) > maxClaudeDesktopArchiveFiles {
return fmt.Errorf("Claude Desktop archive contains more than %d files", maxClaudeDesktopArchiveFiles)
}
var expanded uint64
for _, file := range reader.File {
clean, err := safeClaudeDesktopArchivePath(file.Name)
if err != nil {
return err
}
expanded += file.UncompressedSize64
if expanded > maxClaudeDesktopExtractBytes {
return fmt.Errorf("Claude Desktop archive expands beyond %d bytes", maxClaudeDesktopExtractBytes)
}
path := filepath.Join(destination, filepath.FromSlash(clean))
switch {
case file.FileInfo().IsDir():
if err := os.MkdirAll(path, file.Mode().Perm()); err != nil {
return fmt.Errorf("create Claude Desktop archive directory: %w", err)
}
case file.Mode()&os.ModeSymlink != 0:
target, err := readClaudeDesktopZipFile(file, 16<<10)
if err != nil {
return fmt.Errorf("read Claude Desktop archive symlink: %w", err)
}
if err := validateClaudeDesktopSymlink(clean, string(target)); err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create Claude Desktop archive directory: %w", err)
}
if err := os.Symlink(string(target), path); err != nil {
return fmt.Errorf("create Claude Desktop archive symlink: %w", err)
}
case file.Mode().IsRegular():
if err := extractClaudeDesktopZipFile(file, path); err != nil {
return err
}
default:
return fmt.Errorf("Claude Desktop archive contains unsupported file %q", file.Name)
}
}
return nil
}
func safeClaudeDesktopArchivePath(name string) (string, error) {
if strings.ContainsRune(name, '\x00') || filepath.IsAbs(name) {
return "", fmt.Errorf("Claude Desktop archive contains unsafe path %q", name)
}
clean := filepath.ToSlash(filepath.Clean(name))
if clean != "Claude.app" && !strings.HasPrefix(clean, "Claude.app/") {
return "", fmt.Errorf("Claude Desktop archive contains unexpected path %q", name)
}
return clean, nil
}
func validateClaudeDesktopSymlink(name, target string) error {
if target == "" || filepath.IsAbs(target) {
return fmt.Errorf("Claude Desktop archive contains unsafe symlink %q", name)
}
resolved := filepath.Clean(filepath.Join(filepath.Dir(name), target))
resolved = filepath.ToSlash(resolved)
if resolved != "Claude.app" && !strings.HasPrefix(resolved, "Claude.app/") {
return fmt.Errorf("Claude Desktop archive symlink %q escapes Claude.app", name)
}
return nil
}
func extractClaudeDesktopZipFile(file *zip.File, path string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("create Claude Desktop archive directory: %w", err)
}
input, err := file.Open()
if err != nil {
return fmt.Errorf("open Claude Desktop archive file: %w", err)
}
output, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, file.Mode().Perm())
if err != nil {
input.Close()
return fmt.Errorf("create Claude Desktop archive file: %w", err)
}
_, copyErr := io.Copy(output, input)
inputErr := input.Close()
outputErr := output.Close()
if copyErr != nil {
return fmt.Errorf("extract Claude Desktop archive file: %w", copyErr)
}
if inputErr != nil {
return fmt.Errorf("close Claude Desktop archive file: %w", inputErr)
}
if outputErr != nil {
return fmt.Errorf("close extracted Claude Desktop file: %w", outputErr)
}
return nil
}
func readClaudeDesktopZipFile(file *zip.File, limit int64) ([]byte, error) {
reader, err := file.Open()
if err != nil {
return nil, err
}
defer reader.Close()
data, err := io.ReadAll(io.LimitReader(reader, limit+1))
if err != nil {
return nil, err
}
if int64(len(data)) > limit {
return nil, fmt.Errorf("archive entry exceeds %d bytes", limit)
}
return data, nil
}
func validateClaudeDesktopBundle(bundlePath string) error {
info, err := os.Stat(bundlePath)
if err != nil || !info.IsDir() {
return errors.New("Claude Desktop archive does not contain Claude.app")
}
executable := filepath.Join(bundlePath, "Contents", "MacOS", "Claude")
info, err = os.Stat(executable)
if err != nil {
return fmt.Errorf("Claude Desktop executable is missing: %w", err)
}
if !info.Mode().IsRegular() || info.Mode()&0o111 == 0 {
return errors.New("Claude Desktop executable is not executable")
}
return nil
}
func verifyClaudeDesktopBundle(bundlePath string) error {
if output, err := exec.Command("/usr/bin/codesign", "--verify", "--deep", "--strict", bundlePath).CombinedOutput(); err != nil {
return fmt.Errorf("codesign verification failed: %w: %s", err, strings.TrimSpace(string(output)))
}
output, err := exec.Command("/usr/bin/codesign", "-d", "--verbose=4", bundlePath).CombinedOutput()
if err != nil {
return fmt.Errorf("read code signature: %w: %s", err, strings.TrimSpace(string(output)))
}
details := string(output)
if !strings.Contains(details, "Identifier="+claudeDesktopBundleID) ||
!strings.Contains(details, "TeamIdentifier="+claudeDesktopTeamID) {
return fmt.Errorf("unexpected Claude Desktop signing identity")
}
return nil
}
@@ -1,162 +0,0 @@
//go:build darwin
package main
import (
"archive/zip"
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestInstallClaudeDesktopZip(t *testing.T) {
archive := writeClaudeDesktopTestZip(t, map[string]claudeDesktopTestZipEntry{
"Claude.app/": {directory: true},
"Claude.app/Contents/": {directory: true},
"Claude.app/Contents/MacOS/": {directory: true},
"Claude.app/Contents/MacOS/Claude": {body: "binary", mode: 0o755},
"Claude.app/Contents/Resources/": {directory: true},
"Claude.app/Contents/Resources/link": {body: "../MacOS/Claude", mode: os.ModeSymlink | 0o777},
})
destination := filepath.Join(t.TempDir(), "Applications", "Claude.app")
var verified string
installed, err := installClaudeDesktopZip(archive, []string{destination}, func(bundle string) error {
verified = bundle
return nil
})
if err != nil {
t.Fatal(err)
}
if installed != destination || verified == "" {
t.Fatalf("installed = %q, verified = %q", installed, verified)
}
info, err := os.Stat(filepath.Join(installed, "Contents", "MacOS", "Claude"))
if err != nil {
t.Fatal(err)
}
if info.Mode()&0o111 == 0 {
t.Fatal("installed Claude executable is not executable")
}
if target, err := os.Readlink(filepath.Join(installed, "Contents", "Resources", "link")); err != nil || target != "../MacOS/Claude" {
t.Fatalf("symlink target = %q, err = %v", target, err)
}
}
func TestInstallClaudeDesktopZipRejectsUnsafeArchives(t *testing.T) {
for _, test := range []struct {
name string
entries map[string]claudeDesktopTestZipEntry
}{
{name: "path traversal", entries: map[string]claudeDesktopTestZipEntry{"../Claude.app/Contents/MacOS/Claude": {body: "binary", mode: 0o755}}},
{name: "unexpected root", entries: map[string]claudeDesktopTestZipEntry{"README": {body: "nope", mode: 0o644}}},
{name: "escaping symlink", entries: map[string]claudeDesktopTestZipEntry{
"Claude.app/Contents/MacOS/Claude": {body: "binary", mode: 0o755},
"Claude.app/escape": {body: "../../outside", mode: os.ModeSymlink | 0o777},
}},
} {
t.Run(test.name, func(t *testing.T) {
archive := writeClaudeDesktopTestZip(t, test.entries)
destination := filepath.Join(t.TempDir(), "Claude.app")
if _, err := installClaudeDesktopZip(archive, []string{destination}, func(string) error { return nil }); err == nil {
t.Fatal("installClaudeDesktopZip succeeded")
}
if _, err := os.Stat(destination); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("unsafe archive created destination: %v", err)
}
})
}
}
func TestInstallClaudeDesktopZipVerifiesBeforeMove(t *testing.T) {
archive := writeClaudeDesktopTestZip(t, map[string]claudeDesktopTestZipEntry{
"Claude.app/Contents/MacOS/Claude": {body: "binary", mode: 0o755},
})
destination := filepath.Join(t.TempDir(), "Claude.app")
wantErr := errors.New("invalid signature")
if _, err := installClaudeDesktopZip(archive, []string{destination}, func(string) error { return wantErr }); !errors.Is(err, wantErr) {
t.Fatalf("error = %v, want %v", err, wantErr)
}
if _, err := os.Stat(destination); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("invalid bundle created destination: %v", err)
}
}
func TestInstallClaudeDesktopZipDoesNotOverwrite(t *testing.T) {
archive := writeClaudeDesktopTestZip(t, map[string]claudeDesktopTestZipEntry{
"Claude.app/Contents/MacOS/Claude": {body: "binary", mode: 0o755},
})
destination := filepath.Join(t.TempDir(), "Claude.app")
if err := os.MkdirAll(destination, 0o755); err != nil {
t.Fatal(err)
}
if _, err := installClaudeDesktopZip(archive, []string{destination}, func(string) error { return nil }); !errors.Is(err, errClaudeDesktopDestinationExists) {
t.Fatalf("error = %v, want destination exists", err)
}
}
func TestInstallClaudeDesktopZipRealArchive(t *testing.T) {
archive := os.Getenv("OLLAMA_TEST_CLAUDE_DESKTOP_ZIP")
if archive == "" {
t.Skip("set OLLAMA_TEST_CLAUDE_DESKTOP_ZIP to a downloaded Claude Desktop ZIP")
}
destination := filepath.Join(t.TempDir(), "Applications", "Claude.app")
installed, err := installClaudeDesktopZip(
archive,
[]string{destination},
verifyClaudeDesktopBundle,
)
if err != nil {
t.Fatal(err)
}
if installed != destination {
t.Fatalf("installed = %q, want %q", installed, destination)
}
}
type claudeDesktopTestZipEntry struct {
body string
mode os.FileMode
directory bool
}
func writeClaudeDesktopTestZip(t *testing.T, entries map[string]claudeDesktopTestZipEntry) string {
t.Helper()
path := filepath.Join(t.TempDir(), "Claude.zip")
file, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
writer := zip.NewWriter(file)
for name, entry := range entries {
header := &zip.FileHeader{Name: name, Method: zip.Deflate}
if entry.directory {
header.SetMode(os.ModeDir | 0o755)
} else {
header.SetMode(entry.mode)
}
item, err := writer.CreateHeader(header)
if err != nil {
t.Fatal(err)
}
if _, err := item.Write([]byte(entry.body)); err != nil {
t.Fatal(err)
}
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
return path
}
func TestSafeClaudeDesktopArchivePath(t *testing.T) {
for _, name := range []string{"Claude.app", "Claude.app/Contents/MacOS/Claude"} {
if got, err := safeClaudeDesktopArchivePath(name); err != nil || got != strings.TrimSuffix(name, "/") {
t.Fatalf("safeClaudeDesktopArchivePath(%q) = %q, %v", name, got, err)
}
}
}
-56
View File
@@ -1,56 +0,0 @@
//go:build darwin
package main
import "github.com/ollama/ollama/internal/proxy"
type claudeDesktopInstallResult string
const (
claudeDesktopInstallCancelled claudeDesktopInstallResult = "cancelled"
claudeDesktopInstallerOpened claudeDesktopInstallResult = "opened"
claudeDesktopInstallFailed claudeDesktopInstallResult = "failed"
)
type claudeDesktopStatus struct {
Supported bool `json:"supported"`
Used bool `json:"used"`
Installed bool `json:"installed"`
Configured bool `json:"configured"`
Connected bool `json:"connected"`
Running bool `json:"running"`
StartFailed bool `json:"startFailed"`
PortConflict bool `json:"portConflict"`
GatewayPort int `json:"gatewayPort,omitempty"`
RoutedRequests uint64 `json:"routedRequests"`
Error string `json:"error,omitempty"`
AutoMode bool `json:"autoMode"`
ModelSource string `json:"modelSource,omitempty"`
Models []claudeDesktopModelStatus `json:"models,omitempty"`
Mappings []claudeDesktopMappingStatus `json:"mappings,omitempty"`
}
type claudeDesktopMappingStatus struct {
RouteID string `json:"routeId"`
RouteName string `json:"routeName"`
Model string `json:"model,omitempty"`
}
type claudeDesktopModelStatus struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description,omitempty"`
Cloud bool `json:"cloud"`
Selected bool `json:"selected"`
AutoMode bool `json:"autoMode"`
Availability proxy.ClaudeDesktopAvailability `json:"availability"`
Reason proxy.ClaudeDesktopAccessReason `json:"reason,omitempty"`
RequiredPlan string `json:"requiredPlan,omitempty"`
}
type claudeDesktopActionResult struct {
Status claudeDesktopStatus `json:"status"`
Error string `json:"error,omitempty"`
MappingsApplied bool `json:"mappingsApplied,omitempty"`
RestartConfirmationRequired bool `json:"restartConfirmationRequired,omitempty"`
}
-75
View File
@@ -1,75 +0,0 @@
//go:build darwin
package main
import (
"errors"
"log/slog"
"github.com/ollama/ollama/app/webview"
)
func codexDesktopModelRefreshError(settings codexDesktopModelsSettings) string {
if len(settings.Selected) > 0 {
return "Couldnt refresh available models. Your saved models are unchanged."
}
return "Couldnt refresh available models. Try again."
}
func bindCodexDesktop(wv webview.WebView) {
wv.Bind("getCodexDesktopStatus", func() codexDesktopStatus {
return getCodexDesktopStatus()
})
wv.Bind("getCodexDesktopRequestCount", func() uint64 {
return codexDesktop.OllamaRequestCount()
})
wv.Bind("setCodexDesktopConnected", func(enabled, restartConfirmed bool) codexDesktopActionResult {
err := setCodexDesktopConnection(enabled, restartConfirmed)
result := codexDesktopActionResult{Status: getCodexDesktopStatus()}
if errors.Is(err, errCodexDesktopRestartConfirmationRequired) {
result.RestartConfirmationRequired = true
} else if err != nil {
result.Error = err.Error()
slog.Warn("failed to change ChatGPT integration from Settings", "connected", enabled, "error", err)
}
return result
})
wv.Bind("installCodexDesktop", func() codexDesktopInstallResult {
return requestCodexDesktopInstall()
})
wv.Bind("getCodexDesktopModelsSettings", func() codexDesktopModelsSettingsResult {
settings, err := getCodexDesktopModelsSettings()
result := codexDesktopModelsSettingsResult{Settings: settings}
if err != nil {
result.Warning = codexDesktopModelRefreshError(settings)
slog.Warn("failed to refresh available ChatGPT models", "error", err)
}
return result
})
wv.Bind("applyCodexDesktopModels", func(models []string, restartConfirmed bool) codexDesktopModelsSettingsResult {
err := applyCodexDesktopModels(models, restartConfirmed)
settings, statusErr := getCodexDesktopModelsSettings()
result := codexDesktopModelsSettingsResult{Settings: settings}
if errors.Is(err, errCodexDesktopRestartConfirmationRequired) {
result.RestartConfirmationRequired = true
} else if err != nil {
result.Error = err.Error()
} else if statusErr != nil {
result.Warning = codexDesktopModelRefreshError(settings)
slog.Warn("failed to refresh available ChatGPT models after applying settings", "error", statusErr)
}
return result
})
wv.Bind("resetCodexDesktopModels", func() codexDesktopModelsSettingsResult {
err := resetCodexDesktopModels()
settings, statusErr := getCodexDesktopModelsSettings()
result := codexDesktopModelsSettingsResult{Settings: settings}
if err != nil {
result.Error = err.Error()
} else if statusErr != nil {
result.Warning = codexDesktopModelRefreshError(settings)
slog.Warn("failed to refresh available ChatGPT models after resetting settings", "error", statusErr)
}
return result
})
}
@@ -1,7 +0,0 @@
//go:build windows
package main
import "github.com/ollama/ollama/app/webview"
func bindCodexDesktop(_ webview.WebView) {}
-979
View File
@@ -1,979 +0,0 @@
//go:build darwin
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
"sync"
"time"
"github.com/ollama/ollama/api"
appui "github.com/ollama/ollama/app/ui"
"github.com/ollama/ollama/cmd/config"
"github.com/ollama/ollama/cmd/launch"
"github.com/ollama/ollama/internal/modelref"
"github.com/ollama/ollama/internal/proxy"
modelpkg "github.com/ollama/ollama/types/model"
)
const (
codexDesktopIntegrationName = "chatgpt"
codexDesktopMaxModels = 5
codexDesktopRecommendationsMaxBody = 1 << 20
)
var errCodexDesktopRestartConfirmationRequired = launch.ErrCodexAppRestartConfirmationRequired
type codexDesktopController interface {
Installed() bool
OllamaConfigured() bool
Running() bool
OllamaRequestCount() uint64
UseOllamaFromDesktop(string, []launch.LaunchModel, bool) error
UpdateOllamaModelsFromDesktop(string, []launch.LaunchModel, bool) error
RestoreFromDesktop(bool) error
RestartFromDesktop(bool) error
Onboard() error
}
var (
codexDesktop codexDesktopController = &launch.CodexApp{}
codexDesktopClientFactory = api.ClientFromEnvironment
codexDesktopLoadModels = loadCodexDesktopModels
codexDesktopLoadConnectionModels = loadCodexDesktopConnectionModels
codexDesktopCloudModels = loadCodexDesktopAccountCloudModels
codexDesktopRecommendations = loadCodexDesktopRecommendations
codexDesktopAccessState = currentClaudeDesktopAccessState
codexDesktopRecommendationsClient = &http.Client{Timeout: 3 * time.Second}
codexDesktopRecommendationsEndpoint = func() string {
return strings.TrimRight(appui.OllamaDotCom, "/") + "/api/experimental/model-recommendations?app=codex-desktop"
}
codexDesktopModelLoadAttempts = 20
codexDesktopModelRetryWait = 250 * time.Millisecond
codexDesktopMu sync.Mutex
)
type codexDesktopStatus struct {
Supported bool `json:"supported"`
Installed bool `json:"installed"`
Connected bool `json:"connected"`
Running bool `json:"running"`
Model string `json:"model,omitempty"`
Models []string `json:"models,omitempty"`
MaxModels int `json:"maxModels"`
Requests uint64 `json:"requests"`
}
type codexDesktopActionResult struct {
Status codexDesktopStatus `json:"status"`
Error string `json:"error,omitempty"`
RestartConfirmationRequired bool `json:"restartConfirmationRequired,omitempty"`
}
type codexDesktopInstallResult string
const (
codexDesktopInstallCancelled codexDesktopInstallResult = "cancelled"
codexDesktopInstallerOpened codexDesktopInstallResult = "opened"
codexDesktopInstallFailed codexDesktopInstallResult = "failed"
)
type codexDesktopModelsSettings struct {
Supported bool `json:"supported"`
Installed bool `json:"installed"`
Connected bool `json:"connected"`
Running bool `json:"running"`
// UsesDefaults keeps recommendations implicit without overwriting saved choices.
UsesDefaults bool `json:"usesDefaults"`
Selected []string `json:"selected"`
Available []string `json:"available"`
Models []codexDesktopModelStatus `json:"models"`
MaxModels int `json:"maxModels"`
}
type codexDesktopModelStatus struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description,omitempty"`
Recommended bool `json:"recommended,omitempty"`
Selected bool `json:"selected"`
Availability string `json:"availability"`
Reason string `json:"reason,omitempty"`
RequiredPlan string `json:"requiredPlan,omitempty"`
}
type codexDesktopModelsSettingsResult struct {
Settings codexDesktopModelsSettings `json:"settings"`
Error string `json:"error,omitempty"`
Warning string `json:"warning,omitempty"`
RestartConfirmationRequired bool `json:"restartConfirmationRequired,omitempty"`
}
type codexDesktopModelInventory struct {
Available []launch.LaunchModel
Catalog []codexDesktopCatalogModel
Defaults []launch.LaunchModel
DefaultPrimary string
}
type codexDesktopCatalogModel struct {
Model launch.LaunchModel
DisplayName string
Description string
Recommended bool
Availability proxy.ClaudeDesktopAvailability
Reason proxy.ClaudeDesktopAccessReason
RequiredPlan string
}
func getCodexDesktopStatus() codexDesktopStatus {
connected := codexDesktop.OllamaConfigured()
requests := uint64(0)
if connected {
requests = codexDesktop.OllamaRequestCount()
}
var models []string
if saved, err := config.LoadIntegration(codexDesktopIntegrationName); err == nil && len(saved.Models) > 0 {
models = append([]string(nil), saved.Models...)
}
model := ""
if len(models) > 0 {
model = models[0]
}
return codexDesktopStatus{
Supported: true,
Installed: codexDesktop.Installed(),
Connected: connected,
Running: codexDesktop.Running(),
Model: model,
Models: models,
MaxModels: codexDesktopMaxModels,
Requests: requests,
}
}
func setCodexDesktopConnection(enabled, restartConfirmed bool) error {
codexDesktopMu.Lock()
defer codexDesktopMu.Unlock()
if enabled == codexDesktop.OllamaConfigured() {
return nil
}
if !enabled {
if codexDesktop.Running() && !restartConfirmed {
return errCodexDesktopRestartConfirmationRequired
}
return codexDesktop.RestoreFromDesktop(restartConfirmed)
}
if !codexDesktop.Installed() {
return errors.New("ChatGPT is not installed")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
savedSelection := config.IntegrationModels(codexDesktopIntegrationName)
primary, models, err := codexDesktopLoadConnectionModels(ctx, savedSelection)
if err != nil {
return err
}
// Validate before requesting a restart; do not change the profile without consent.
if codexDesktop.Running() && !restartConfirmed {
return errCodexDesktopRestartConfirmationRequired
}
previous := config.IntegrationModels(codexDesktopIntegrationName)
if err := config.SaveIntegration(codexDesktopIntegrationName, savedSelection); err != nil {
return fmt.Errorf("save ChatGPT integration: %w", err)
}
if err := codexDesktop.Onboard(); err != nil {
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
return fmt.Errorf("save ChatGPT integration state: %w", err)
}
if err := codexDesktop.UseOllamaFromDesktop(primary, models, restartConfirmed); err != nil {
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
if errors.Is(err, errCodexDesktopRestartConfirmationRequired) {
return err
}
if codexDesktop.OllamaConfigured() {
if restoreErr := codexDesktop.RestoreFromDesktop(true); restoreErr != nil {
return errors.Join(err, fmt.Errorf("restore ChatGPT after failed update: %w", restoreErr))
}
}
return err
}
return nil
}
func getCodexDesktopModelsSettings() (codexDesktopModelsSettings, error) {
settings := codexDesktopModelsSettings{
Supported: true,
Installed: codexDesktop.Installed(),
Connected: codexDesktop.OllamaConfigured(),
Running: codexDesktop.Running(),
Selected: []string{},
Available: []string{},
Models: []codexDesktopModelStatus{},
MaxModels: codexDesktopMaxModels,
}
// Keep restart available when inventory cannot be loaded.
settings.Selected = config.IntegrationModels(codexDesktopIntegrationName)
settings.UsesDefaults = len(settings.Selected) == 0
if len(settings.Selected) > codexDesktopMaxModels {
settings.Selected = settings.Selected[:codexDesktopMaxModels]
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
inventory, err := loadCodexDesktopModelInventory(ctx)
if err != nil {
return settings, err
}
settings.Available = codexDesktopModelNames(inventory.Available)
if len(settings.Selected) == 0 {
settings.Selected = codexDesktopModelNames(codexDesktopDefaultModels(inventory))
}
settings.Models = codexDesktopModelStatuses(inventory, settings.Selected)
return settings, nil
}
func applyCodexDesktopModels(selected []string, restartConfirmed bool) error {
codexDesktopMu.Lock()
defer codexDesktopMu.Unlock()
return applyCodexDesktopModelsLocked(selected, restartConfirmed, true)
}
func resetCodexDesktopModels() error {
codexDesktopMu.Lock()
defer codexDesktopMu.Unlock()
// Reset preferences without enabling the integration or restarting ChatGPT.
if len(config.IntegrationModels(codexDesktopIntegrationName)) == 0 && !codexDesktop.OllamaConfigured() {
return nil
}
if err := config.SaveIntegration(codexDesktopIntegrationName, nil); err != nil {
return fmt.Errorf("reset ChatGPT models: %w", err)
}
return nil
}
func applyCodexDesktopModelsLocked(selected []string, restartConfirmed, openWhenStopped bool) error {
previous := config.IntegrationModels(codexDesktopIntegrationName)
savedSelection := append([]string(nil), selected...)
wasConfigured := codexDesktop.OllamaConfigured()
selectionUnchanged := slices.Equal(savedSelection, previous)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
primary, models, err := codexDesktopLoadModels(ctx, selected)
if err != nil {
if openWhenStopped && wasConfigured && selectionUnchanged {
return codexDesktop.RestartFromDesktop(restartConfirmed)
}
return err
}
if !wasConfigured && !openWhenStopped {
if err := config.SaveIntegration(codexDesktopIntegrationName, savedSelection); err != nil {
return fmt.Errorf("save ChatGPT models: %w", err)
}
return nil
}
running := codexDesktop.Running()
if running && !restartConfirmed {
return errCodexDesktopRestartConfirmationRequired
}
if err := config.SaveIntegration(codexDesktopIntegrationName, savedSelection); err != nil {
return fmt.Errorf("save ChatGPT models: %w", err)
}
updateModels := codexDesktop.UseOllamaFromDesktop
if !openWhenStopped {
updateModels = codexDesktop.UpdateOllamaModelsFromDesktop
}
if err := updateModels(primary, models, restartConfirmed); err == nil {
return nil
} else if errors.Is(err, errCodexDesktopRestartConfirmationRequired) {
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
return err
} else if !wasConfigured {
if codexDesktop.OllamaConfigured() {
if restoreErr := codexDesktop.RestoreFromDesktop(true); restoreErr != nil {
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
return errors.Join(err, fmt.Errorf("restore ChatGPT after failed update: %w", restoreErr))
}
}
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
return fmt.Errorf("start ChatGPT with selected Ollama models: %w", err)
} else {
applyErr := err
_ = config.SaveIntegration(codexDesktopIntegrationName, previous)
rollbackCtx, rollbackCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer rollbackCancel()
rollbackPrimary, rollbackModels, rollbackErr := codexDesktopLoadModels(rollbackCtx, previous)
if rollbackErr == nil {
rollbackErr = updateModels(rollbackPrimary, rollbackModels, true)
}
if rollbackErr != nil {
// Restore the original profile if the previous selection is no longer usable.
if restoreErr := codexDesktop.RestoreFromDesktop(true); restoreErr != nil {
return errors.Join(
fmt.Errorf("apply ChatGPT models: %v; restore previous Ollama profile: %w", applyErr, rollbackErr),
fmt.Errorf("restore normal ChatGPT profile: %w", restoreErr),
)
}
return fmt.Errorf("apply ChatGPT models: %v; restore previous Ollama profile: %v; restored the normal ChatGPT profile", applyErr, rollbackErr)
}
return fmt.Errorf("apply ChatGPT models: %w", applyErr)
}
}
func loadCodexDesktopModels(ctx context.Context, selected []string) (string, []launch.LaunchModel, error) {
inventory, err := loadCodexDesktopModelInventory(ctx)
if err != nil {
return "", nil, err
}
if len(selected) == 0 {
selected = codexDesktopModelNames(codexDesktopDefaultModels(inventory))
}
_, models, err := selectCodexDesktopModels(selected, inventory.Available)
if err != nil {
return "", nil, err
}
primary := codexDesktopPreferredPrimary(inventory.DefaultPrimary, models)
return primary, hydrateCodexDesktopModelCapabilities(ctx, models), nil
}
func loadCodexDesktopConnectionModels(ctx context.Context, selected []string) (string, []launch.LaunchModel, error) {
inventory, err := loadCodexDesktopModelInventory(ctx)
if err != nil {
return "", nil, err
}
defaults := codexDesktopDefaultModels(inventory)
if len(selected) == 0 {
selected = codexDesktopModelNames(defaults)
}
_, models, err := reconcileCodexDesktopModels(selected, inventory.Available, defaults)
if err != nil {
return "", nil, err
}
primary := codexDesktopPreferredPrimary(inventory.DefaultPrimary, models)
return primary, hydrateCodexDesktopModelCapabilities(ctx, models), nil
}
// /api/show supplies capabilities and family metadata without replacing recommended thinking controls.
func hydrateCodexDesktopModelCapabilities(ctx context.Context, models []launch.LaunchModel) []launch.LaunchModel {
client, err := codexDesktopClientFactory()
if err != nil {
return models
}
hydrated := append([]launch.LaunchModel(nil), models...)
for i := range hydrated {
response, err := client.Show(ctx, &api.ShowRequest{Model: hydrated[i].Name})
if err != nil {
continue
}
if len(response.Capabilities) > 0 {
hydrated[i].Capabilities = append([]modelpkg.Capability(nil), response.Capabilities...)
}
if response.Details.Family != "" || len(response.Details.Families) > 0 {
hydrated[i].Details = response.Details
}
}
return hydrated
}
func loadCodexDesktopAvailableModels(ctx context.Context) ([]launch.LaunchModel, error) {
inventory, err := loadCodexDesktopModelInventory(ctx)
return inventory.Available, err
}
func loadCodexDesktopModelInventory(ctx context.Context) (codexDesktopModelInventory, error) {
client, err := codexDesktopClientFactory()
if err != nil {
return codexDesktopModelInventory{}, err
}
recommendations, recommendationsErr := codexDesktopRecommendations(ctx)
if recommendationsErr != nil {
slog.Debug("could not load ChatGPT model recommendations", "error", recommendationsErr)
}
var access proxy.ClaudeDesktopAccessState
accessKnown := false
var last codexDesktopModelInventory
for attempt := range codexDesktopModelLoadAttempts {
if !accessKnown {
resolved, accessErr := codexDesktopAccessState(ctx)
if accessErr == nil {
access = resolved
accessKnown = true
} else {
slog.Debug("could not determine ChatGPT model access", "error", accessErr)
}
}
var listed []api.ListModelResponse
listKnown := false
if response, listErr := client.List(ctx); listErr == nil {
listed = response.Models
listKnown = true
}
var accountCloud []string
cloudKnown := false
if names, cloudErr := codexDesktopCloudModels(ctx); cloudErr == nil {
accountCloud = names
cloudKnown = true
}
last = buildCodexDesktopModelInventory(recommendations, listed, accountCloud, access, accessKnown, listKnown, cloudKnown)
// Retry access lookup failures even when recommendations are available.
if len(last.Available) > 0 && (accessKnown || attempt+1 == codexDesktopModelLoadAttempts) {
return last, nil
}
if attempt+1 == codexDesktopModelLoadAttempts {
break
}
timer := time.NewTimer(codexDesktopModelRetryWait)
select {
case <-ctx.Done():
timer.Stop()
return codexDesktopModelInventory{}, ctx.Err()
case <-timer.C:
}
}
if len(last.Catalog) > 0 {
return last, nil
}
return codexDesktopModelInventory{}, errors.New("no Ollama models are available for ChatGPT")
}
func loadCodexDesktopRecommendations(ctx context.Context) ([]api.ModelRecommendation, error) {
req, err := newSignedOllamaRequest(ctx, http.MethodGet, codexDesktopRecommendationsEndpoint())
if err != nil {
return nil, fmt.Errorf("prepare ChatGPT model recommendations request: %w", err)
}
resp, err := codexDesktopRecommendationsClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetch ChatGPT model recommendations: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, codexDesktopRecommendationsMaxBody))
return nil, fmt.Errorf("fetch ChatGPT model recommendations: status %d", resp.StatusCode)
}
var payload api.ModelRecommendationsResponse
decoder := json.NewDecoder(io.LimitReader(resp.Body, codexDesktopRecommendationsMaxBody+1))
if err := decoder.Decode(&payload); err != nil {
return nil, fmt.Errorf("decode ChatGPT model recommendations: %w", err)
}
if len(payload.Recommendations) == 0 {
return nil, errors.New("ChatGPT model recommendations are empty")
}
return payload.Recommendations, nil
}
func loadCodexDesktopAccountCloudModels(ctx context.Context) ([]string, error) {
models, err := currentClaudeDesktopCloudModels(ctx)
if err != nil {
return nil, err
}
names := make([]string, 0, len(models))
for _, model := range models {
name := strings.TrimSpace(model.OllamaModel)
if name == "" {
name = strings.TrimSpace(model.Name)
}
if name != "" {
names = append(names, name)
}
}
return names, nil
}
func buildCodexDesktopModelInventory(
recommendations []api.ModelRecommendation,
listed []api.ListModelResponse,
accountCloud []string,
access proxy.ClaudeDesktopAccessState,
accessKnown, localInventoryKnown, cloudInventoryKnown bool,
) codexDesktopModelInventory {
actual := codexDesktopAvailableModels(listed, accountCloud)
actualByName := make(map[string]launch.LaunchModel, len(actual))
for _, model := range actual {
actualByName[codexDesktopModelKey(model.Name)] = model
}
seen := make(map[string]bool, len(actual)+len(recommendations))
recommended := make([]codexDesktopCatalogModel, 0, len(recommendations))
for _, recommendation := range recommendations {
route := codexDesktopRecommendationRoute(recommendation)
key := codexDesktopModelKey(route)
if key == "" || seen[key] {
continue
}
seen[key] = true
model, present := actualByName[key]
if !present {
model = launch.LaunchModel{Name: route, Remote: codexDesktopCloudModel(route)}
}
if recommendation.ContextLength > 0 {
model.ContextLength = recommendation.ContextLength
}
if recommendation.MaxOutputTokens > 0 {
model.MaxOutputTokens = recommendation.MaxOutputTokens
}
if recommendation.Thinking != nil {
model.Thinking = recommendation.Thinking.Clone()
}
availability, reason := codexDesktopRecommendationAccess(
model,
present,
strings.TrimSpace(recommendation.RequiredPlan),
access,
accessKnown,
localInventoryKnown,
)
entry := codexDesktopCatalogModel{
Model: model,
DisplayName: strings.TrimSpace(recommendation.Model),
Description: strings.TrimSpace(recommendation.Description),
Recommended: true,
Availability: availability,
Reason: reason,
RequiredPlan: strings.TrimSpace(recommendation.RequiredPlan),
}
recommended = append(recommended, entry)
}
extras := make([]codexDesktopCatalogModel, 0, len(actual))
for _, model := range actual {
key := codexDesktopModelKey(model.Name)
if key == "" || seen[key] {
continue
}
seen[key] = true
availability, reason := codexDesktopInventoryModelAccess(model, access, accessKnown, cloudInventoryKnown)
extras = append(extras, codexDesktopCatalogModel{
Model: model,
DisplayName: model.Name,
Availability: availability,
Reason: reason,
})
}
// Sort recommendations only; preserve saved list order.
slices.SortStableFunc(recommended, func(a, b codexDesktopCatalogModel) int {
return codexDesktopRecommendationPriority(a.Model.Name) - codexDesktopRecommendationPriority(b.Model.Name)
})
catalog := make([]codexDesktopCatalogModel, 0, len(recommended)+len(extras))
catalog = append(catalog, recommended...)
catalog = append(catalog, extras...)
available := make([]launch.LaunchModel, 0, len(catalog))
for _, entry := range catalog {
// Recommendations remain configurable regardless of current availability.
if entry.Recommended || entry.Availability == proxy.ClaudeDesktopAvailabilityAvailable {
available = append(available, entry.Model)
}
}
defaults := codexDesktopRecommendationDefaults(catalog)
return codexDesktopModelInventory{
Available: available,
Catalog: catalog,
Defaults: defaults,
DefaultPrimary: codexDesktopDefaultPrimary(catalog, defaults, access, accessKnown),
}
}
func codexDesktopRecommendationPriority(name string) int {
switch codexDesktopModelKey(name) {
case "kimi-k3:cloud":
return 0
case "glm-5.3:cloud":
return 1
case "glm-5.3-flash:cloud":
return 2
case "deepseek-v4-flash:cloud":
return 3
case "gemma4:31b:cloud":
return 4
default:
return 5
}
}
func codexDesktopRecommendationRoute(recommendation api.ModelRecommendation) string {
name := strings.TrimSpace(recommendation.Model)
if name != "" && recommendation.RequiredPlan != "" && !modelref.HasExplicitCloudSource(name) {
name += ":cloud"
}
return name
}
func codexDesktopRecommendationAccess(
model launch.LaunchModel,
present bool,
requiredPlan string,
access proxy.ClaudeDesktopAccessState,
accessKnown, localInventoryKnown bool,
) (proxy.ClaudeDesktopAvailability, proxy.ClaudeDesktopAccessReason) {
if !model.Remote {
if !localInventoryKnown {
return proxy.ClaudeDesktopAvailabilityUnknown, proxy.ClaudeDesktopAccessVerificationUnavailable
}
if present {
return proxy.ClaudeDesktopAvailabilityAvailable, ""
}
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessModelNotInstalled
}
if !accessKnown {
return proxy.ClaudeDesktopAvailabilityUnknown, proxy.ClaudeDesktopAccessVerificationUnavailable
}
if access.Cloud == proxy.ClaudeDesktopCloudOff {
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessCloudOff
}
if access.Cloud != proxy.ClaudeDesktopCloudOn || access.Account == proxy.ClaudeDesktopAccountUnknown {
return proxy.ClaudeDesktopAvailabilityUnknown, proxy.ClaudeDesktopAccessVerificationUnavailable
}
if access.Account == proxy.ClaudeDesktopAccountSignedOut {
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessSignInRequired
}
if !codexDesktopPlanSatisfies(access.Plan, requiredPlan) {
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessUpgradeRequired
}
// Recommended cloud models need not appear in /api/tags.
return proxy.ClaudeDesktopAvailabilityAvailable, ""
}
func codexDesktopInventoryModelAccess(
model launch.LaunchModel,
access proxy.ClaudeDesktopAccessState,
accessKnown, cloudInventoryKnown bool,
) (proxy.ClaudeDesktopAvailability, proxy.ClaudeDesktopAccessReason) {
if !model.Remote {
return proxy.ClaudeDesktopAvailabilityAvailable, ""
}
if !accessKnown {
return proxy.ClaudeDesktopAvailabilityUnknown, proxy.ClaudeDesktopAccessVerificationUnavailable
}
if access.Cloud == proxy.ClaudeDesktopCloudOff {
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessCloudOff
}
if !cloudInventoryKnown || access.Cloud != proxy.ClaudeDesktopCloudOn || access.Account == proxy.ClaudeDesktopAccountUnknown {
return proxy.ClaudeDesktopAvailabilityUnknown, proxy.ClaudeDesktopAccessVerificationUnavailable
}
if access.Account == proxy.ClaudeDesktopAccountSignedOut {
return proxy.ClaudeDesktopAvailabilityUnavailable, proxy.ClaudeDesktopAccessSignInRequired
}
return proxy.ClaudeDesktopAvailabilityAvailable, ""
}
func codexDesktopPlanSatisfies(plan, required string) bool {
plan = strings.ToLower(strings.TrimSpace(plan))
required = strings.ToLower(strings.TrimSpace(required))
if required == "" || required == "free" {
return true
}
return plan != "" && plan != "free"
}
func codexDesktopRecommendationDefaults(catalog []codexDesktopCatalogModel) []launch.LaunchModel {
defaults := make([]launch.LaunchModel, 0, codexDesktopMaxModels)
for _, entry := range catalog {
if !entry.Recommended {
continue
}
defaults = append(defaults, entry.Model)
if len(defaults) == codexDesktopMaxModels {
return defaults
}
}
if len(defaults) > 0 {
return defaults
}
for _, entry := range catalog {
if entry.Availability != proxy.ClaudeDesktopAvailabilityAvailable {
continue
}
defaults = append(defaults, entry.Model)
if len(defaults) == codexDesktopMaxModels {
break
}
}
return defaults
}
func codexDesktopDefaultPrimary(
catalog []codexDesktopCatalogModel,
defaults []launch.LaunchModel,
access proxy.ClaudeDesktopAccessState,
accessKnown bool,
) string {
if len(defaults) == 0 {
return ""
}
// Choose the starting model independently of picker order.
if accessKnown && access.Account == proxy.ClaudeDesktopAccountSignedIn && codexDesktopPlanSatisfies(access.Plan, "pro") {
return codexDesktopPreferredPrimary("glm-5.3-flash:cloud", defaults)
}
for _, entry := range catalog {
if !entry.Recommended {
continue
}
required := strings.ToLower(strings.TrimSpace(entry.RequiredPlan))
if required == "" || required == "free" {
return entry.Model.Name
}
}
return defaults[0].Name
}
func codexDesktopPreferredPrimary(preferred string, models []launch.LaunchModel) string {
preferredKey := codexDesktopModelKey(preferred)
for _, model := range models {
if preferredKey != "" && codexDesktopModelKey(model.Name) == preferredKey {
return model.Name
}
}
if len(models) > 0 {
return models[0].Name
}
return ""
}
func codexDesktopModelStatuses(inventory codexDesktopModelInventory, selected []string) []codexDesktopModelStatus {
selectedSet := make(map[string]bool, len(selected))
for _, name := range selected {
selectedSet[codexDesktopModelKey(name)] = true
}
statuses := make([]codexDesktopModelStatus, 0, len(inventory.Catalog)+len(selected))
seen := make(map[string]bool, cap(statuses))
for _, entry := range inventory.Catalog {
key := codexDesktopModelKey(entry.Model.Name)
seen[key] = true
displayName := entry.DisplayName
if displayName == "" {
displayName = entry.Model.Name
}
statuses = append(statuses, codexDesktopModelStatus{
Name: entry.Model.Name,
DisplayName: displayName,
Description: entry.Description,
Recommended: entry.Recommended,
Selected: selectedSet[key],
Availability: string(entry.Availability),
Reason: string(entry.Reason),
RequiredPlan: entry.RequiredPlan,
})
}
for _, name := range selected {
key := codexDesktopModelKey(name)
if key == "" || seen[key] {
continue
}
seen[key] = true
statuses = append(statuses, codexDesktopModelStatus{
Name: name,
DisplayName: name,
Selected: true,
Availability: string(proxy.ClaudeDesktopAvailabilityUnknown),
Reason: string(proxy.ClaudeDesktopAccessVerificationUnavailable),
})
}
return statuses
}
func buildCodexDesktopModels(selected []string, listed []api.ListModelResponse, accountCloud []string) (string, []launch.LaunchModel, error) {
available := codexDesktopAvailableModels(listed, accountCloud)
return selectCodexDesktopModels(selected, available)
}
func codexDesktopAvailableModels(listed []api.ListModelResponse, accountCloud []string) []launch.LaunchModel {
installed := make(map[string]api.ListModelResponse, len(listed))
for _, model := range listed {
for _, name := range []string{model.Name, model.Model} {
if key := codexDesktopModelKey(name); key != "" {
installed[key] = model
}
}
}
accountCloudSet := make(map[string]bool, len(accountCloud))
for _, name := range accountCloud {
if key := codexDesktopModelKey(name); key != "" {
accountCloudSet[key] = true
}
}
models := make([]launch.LaunchModel, 0, len(listed)+len(accountCloud))
seen := make(map[string]bool, cap(models))
add := func(model launch.LaunchModel) {
model.Name = strings.TrimSpace(model.Name)
key := codexDesktopModelKey(model.Name)
if key == "" || seen[key] {
return
}
seen[key] = true
models = append(models, model)
}
for _, model := range listed {
if codexDesktopListedModelIsCloud(model) && !accountCloudSet[codexDesktopModelKey(model.Name)] && !accountCloudSet[codexDesktopModelKey(model.Model)] {
continue
}
add(codexDesktopLaunchModel(model))
}
for _, name := range accountCloud {
key := codexDesktopModelKey(name)
if listedModel, ok := installed[key]; ok {
add(codexDesktopLaunchModel(listedModel))
continue
}
add(launch.LaunchModel{Name: strings.TrimSpace(name), Remote: true})
}
return models
}
func codexDesktopDefaultModels(inventory codexDesktopModelInventory) []launch.LaunchModel {
if inventory.Catalog != nil || inventory.Defaults != nil {
return append([]launch.LaunchModel(nil), inventory.Defaults...)
}
return append([]launch.LaunchModel(nil), inventory.Available[:min(len(inventory.Available), codexDesktopMaxModels)]...)
}
func codexDesktopListedModelIsCloud(model api.ListModelResponse) bool {
return model.RemoteModel != "" || model.RemoteHost != "" ||
codexDesktopCloudModel(model.Name) ||
codexDesktopCloudModel(model.Model)
}
func selectCodexDesktopModels(selected []string, available []launch.LaunchModel) (string, []launch.LaunchModel, error) {
byName := make(map[string]launch.LaunchModel, len(available))
for _, model := range available {
byName[codexDesktopModelKey(model.Name)] = model
}
if len(selected) > codexDesktopMaxModels {
return "", nil, fmt.Errorf("choose up to %d models for ChatGPT", codexDesktopMaxModels)
}
resolved := make([]launch.LaunchModel, 0, codexDesktopMaxModels)
seen := make(map[string]bool, codexDesktopMaxModels)
for _, name := range selected {
name = strings.TrimSpace(name)
key := codexDesktopModelKey(name)
if key == "" || seen[key] {
continue
}
model, ok := byName[key]
if !ok {
return "", nil, fmt.Errorf("ChatGPT model %q is not available", name)
}
seen[key] = true
resolved = append(resolved, model)
}
if len(selected) == 0 {
for _, model := range available {
if len(resolved) == codexDesktopMaxModels {
break
}
key := codexDesktopModelKey(model.Name)
if key == "" || seen[key] {
continue
}
seen[key] = true
resolved = append(resolved, model)
}
}
if len(resolved) == 0 {
return "", nil, errors.New("choose at least one available Ollama model for ChatGPT")
}
return resolved[0].Name, resolved, nil
}
// Reopening tolerates stale selections; explicit Settings changes use strict validation.
func reconcileCodexDesktopModels(selected []string, available, defaults []launch.LaunchModel) (string, []launch.LaunchModel, error) {
if len(selected) == 0 {
if len(defaults) > 0 {
return selectCodexDesktopModels(codexDesktopModelNames(defaults), available)
}
return selectCodexDesktopModels(nil, available)
}
byName := make(map[string]launch.LaunchModel, len(available))
for _, model := range available {
byName[codexDesktopModelKey(model.Name)] = model
}
resolved := make([]launch.LaunchModel, 0, min(len(selected), codexDesktopMaxModels))
seen := make(map[string]bool, codexDesktopMaxModels)
for _, name := range selected {
key := codexDesktopModelKey(name)
model, ok := byName[key]
if key == "" || !ok || seen[key] {
continue
}
seen[key] = true
resolved = append(resolved, model)
if len(resolved) == codexDesktopMaxModels {
break
}
}
if len(resolved) == 0 {
if len(defaults) > 0 {
return selectCodexDesktopModels(codexDesktopModelNames(defaults), available)
}
return selectCodexDesktopModels(nil, available)
}
return resolved[0].Name, resolved, nil
}
func codexDesktopModelNames(models []launch.LaunchModel) []string {
names := make([]string, 0, len(models))
for _, model := range models {
if name := strings.TrimSpace(model.Name); name != "" {
names = append(names, name)
}
}
return names
}
func codexDesktopLaunchModel(model api.ListModelResponse) launch.LaunchModel {
name := strings.TrimSpace(model.Name)
if name == "" {
name = strings.TrimSpace(model.Model)
}
return launch.LaunchModel{
Name: name,
Remote: model.RemoteModel != "" || model.RemoteHost != "" || codexDesktopCloudModel(name),
Capabilities: append([]modelpkg.Capability(nil), model.Capabilities...),
ContextLength: model.Details.ContextLength,
EmbeddingLength: model.Details.EmbeddingLength,
Size: model.Size,
Details: model.Details,
}
}
func codexDesktopModelKey(name string) string {
name = strings.TrimSpace(name)
parsed, err := modelref.ParseRef(name)
if err != nil {
return strings.TrimSuffix(name, ":latest")
}
base := strings.TrimSuffix(strings.TrimSpace(parsed.Base), ":latest")
if parsed.Source == modelref.ModelSourceCloud {
return base + ":cloud"
}
return base
}
func codexDesktopCloudModel(name string) bool {
name = strings.ToLower(strings.TrimSpace(name))
return strings.HasSuffix(name, ":cloud") || strings.HasSuffix(name, "-cloud")
}
File diff suppressed because it is too large. Load diff
@@ -1,212 +0,0 @@
//go:build darwin
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
maxCodexDesktopDiskImageBytes = 2 << 30
codexDesktopBundleID = "com.openai.codex"
codexDesktopTeamID = "2DC432GLL2"
)
var errCodexDesktopDestinationExists = errors.New("ChatGPT installation destination already exists")
func codexDesktopInstallDestinations() []string {
destinations := []string{"/Applications/ChatGPT.app"}
if home, err := os.UserHomeDir(); err == nil {
destinations = append(destinations, filepath.Join(home, "Applications", "ChatGPT.app"))
}
return destinations
}
func installCodexDesktopDiskImage(imagePath string, destinations []string, verify func(string) error) (installedPath string, err error) {
if len(destinations) == 0 {
return "", errors.New("ChatGPT installation destination is required")
}
if verify == nil {
return "", errors.New("ChatGPT bundle verifier is required")
}
info, err := os.Stat(imagePath)
if err != nil {
return "", fmt.Errorf("stat ChatGPT disk image: %w", err)
}
if !info.Mode().IsRegular() {
return "", errors.New("ChatGPT disk image is not a regular file")
}
if info.Size() > maxCodexDesktopDiskImageBytes {
return "", fmt.Errorf("ChatGPT disk image exceeds %d bytes", maxCodexDesktopDiskImageBytes)
}
workDir, err := os.MkdirTemp("", "ollama-chatgpt-install-")
if err != nil {
return "", fmt.Errorf("create ChatGPT installation directory: %w", err)
}
defer os.RemoveAll(workDir)
mountPath := filepath.Join(workDir, "volume")
if err := os.Mkdir(mountPath, 0o700); err != nil {
return "", fmt.Errorf("create ChatGPT mount point: %w", err)
}
output, err := exec.Command(
"/usr/bin/hdiutil",
"attach",
"-nobrowse",
"-readonly",
"-mountpoint",
mountPath,
imagePath,
).CombinedOutput()
if err != nil {
return "", fmt.Errorf("mount ChatGPT disk image: %w: %s", err, strings.TrimSpace(string(output)))
}
defer func() {
detachOutput, detachErr := exec.Command("/usr/bin/hdiutil", "detach", mountPath).CombinedOutput()
if detachErr == nil {
return
}
forceOutput, forceErr := exec.Command("/usr/bin/hdiutil", "detach", "-force", mountPath).CombinedOutput()
if forceErr != nil && err == nil {
err = fmt.Errorf(
"unmount ChatGPT disk image: %v: %s; force detach: %v: %s",
detachErr,
strings.TrimSpace(string(detachOutput)),
forceErr,
strings.TrimSpace(string(forceOutput)),
)
}
}()
bundlePath, err := codexDesktopBundleOnVolume(mountPath)
if err != nil {
return "", err
}
return installCodexDesktopBundle(bundlePath, destinations, verify)
}
func codexDesktopBundleOnVolume(mountPath string) (string, error) {
for _, name := range []string{"ChatGPT.app", "Codex.app"} {
bundlePath := filepath.Join(mountPath, name)
info, err := os.Lstat(bundlePath)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return "", fmt.Errorf("inspect ChatGPT bundle: %w", err)
}
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
return "", fmt.Errorf("ChatGPT disk image contains an invalid %s", name)
}
return bundlePath, nil
}
return "", errors.New("ChatGPT disk image does not contain ChatGPT.app")
}
func installCodexDesktopBundle(bundlePath string, destinations []string, verify func(string) error) (string, error) {
if err := validateCodexDesktopBundle(bundlePath); err != nil {
return "", err
}
if err := verify(bundlePath); err != nil {
return "", fmt.Errorf("verify ChatGPT signature: %w", err)
}
var permissionErr error
for _, destination := range destinations {
if strings.TrimSpace(destination) == "" {
continue
}
if _, err := os.Lstat(destination); err == nil {
return "", fmt.Errorf("%w: %s", errCodexDesktopDestinationExists, destination)
} else if !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("check ChatGPT destination %s: %w", destination, err)
}
parent := filepath.Dir(destination)
if err := os.MkdirAll(parent, 0o755); err != nil {
if errors.Is(err, os.ErrPermission) {
permissionErr = err
continue
}
return "", fmt.Errorf("create ChatGPT destination: %w", err)
}
stageDir, err := os.MkdirTemp(parent, ".ollama-chatgpt-install-")
if err != nil {
if errors.Is(err, os.ErrPermission) {
permissionErr = err
continue
}
return "", fmt.Errorf("create staged ChatGPT destination: %w", err)
}
stagedBundle := filepath.Join(stageDir, "ChatGPT.app")
copyOutput, copyErr := exec.Command("/usr/bin/ditto", bundlePath, stagedBundle).CombinedOutput()
if copyErr == nil {
copyErr = validateCodexDesktopBundle(stagedBundle)
}
if copyErr == nil {
copyErr = verify(stagedBundle)
}
if copyErr == nil {
copyErr = os.Rename(stagedBundle, destination)
}
removeErr := os.RemoveAll(stageDir)
if copyErr != nil {
if errors.Is(copyErr, os.ErrPermission) {
permissionErr = copyErr
continue
}
return "", fmt.Errorf("install ChatGPT in %s: %w: %s", parent, copyErr, strings.TrimSpace(string(copyOutput)))
}
if removeErr != nil {
return "", fmt.Errorf("remove staged ChatGPT destination: %w", removeErr)
}
return destination, nil
}
if permissionErr != nil {
return "", fmt.Errorf("install ChatGPT in Applications: %w", permissionErr)
}
return "", errors.New("ChatGPT installation destination is required")
}
func validateCodexDesktopBundle(bundlePath string) error {
info, err := os.Stat(bundlePath)
if err != nil || !info.IsDir() {
return errors.New("ChatGPT disk image does not contain a valid app bundle")
}
for _, executableName := range []string{"ChatGPT", "Codex"} {
executable := filepath.Join(bundlePath, "Contents", "MacOS", executableName)
info, err = os.Stat(executable)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return fmt.Errorf("inspect ChatGPT executable: %w", err)
}
if info.Mode().IsRegular() && info.Mode()&0o111 != 0 {
return nil
}
return errors.New("ChatGPT executable is not executable")
}
return errors.New("ChatGPT executable is missing")
}
func verifyCodexDesktopBundle(bundlePath string) error {
if output, err := exec.Command("/usr/bin/codesign", "--verify", "--deep", "--strict", bundlePath).CombinedOutput(); err != nil {
return fmt.Errorf("codesign verification failed: %w: %s", err, strings.TrimSpace(string(output)))
}
output, err := exec.Command("/usr/bin/codesign", "-d", "--verbose=4", bundlePath).CombinedOutput()
if err != nil {
return fmt.Errorf("read code signature: %w: %s", err, strings.TrimSpace(string(output)))
}
details := string(output)
if !strings.Contains(details, "Identifier="+codexDesktopBundleID) ||
!strings.Contains(details, "TeamIdentifier="+codexDesktopTeamID) {
return errors.New("unexpected ChatGPT signing identity")
}
return nil
}
@@ -1,120 +0,0 @@
//go:build darwin
package main
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestInstallCodexDesktopBundle(t *testing.T) {
bundle := writeCodexDesktopTestBundle(t, "ChatGPT.app", "ChatGPT")
destination := filepath.Join(t.TempDir(), "Applications", "ChatGPT.app")
verified := 0
installed, err := installCodexDesktopBundle(bundle, []string{destination}, func(string) error {
verified++
return nil
})
if err != nil {
t.Fatal(err)
}
if installed != destination {
t.Fatalf("installed = %q, want %q", installed, destination)
}
if verified != 2 {
t.Fatalf("signature verification count = %d, want 2", verified)
}
info, err := os.Stat(filepath.Join(installed, "Contents", "MacOS", "ChatGPT"))
if err != nil {
t.Fatal(err)
}
if info.Mode()&0o111 == 0 {
t.Fatal("installed ChatGPT executable is not executable")
}
}
func TestInstallCodexDesktopBundleAcceptsCodexNamedSource(t *testing.T) {
bundle := writeCodexDesktopTestBundle(t, "Codex.app", "Codex")
destination := filepath.Join(t.TempDir(), "Applications", "ChatGPT.app")
if _, err := installCodexDesktopBundle(bundle, []string{destination}, func(string) error { return nil }); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(destination, "Contents", "MacOS", "Codex")); err != nil {
t.Fatal(err)
}
}
func TestInstallCodexDesktopBundleVerifiesBeforeCopy(t *testing.T) {
bundle := writeCodexDesktopTestBundle(t, "ChatGPT.app", "ChatGPT")
destination := filepath.Join(t.TempDir(), "ChatGPT.app")
wantErr := errors.New("invalid signature")
if _, err := installCodexDesktopBundle(bundle, []string{destination}, func(string) error { return wantErr }); !errors.Is(err, wantErr) {
t.Fatalf("error = %v, want %v", err, wantErr)
}
if _, err := os.Stat(destination); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("invalid bundle created destination: %v", err)
}
}
func TestInstallCodexDesktopBundleDoesNotOverwrite(t *testing.T) {
bundle := writeCodexDesktopTestBundle(t, "ChatGPT.app", "ChatGPT")
destination := filepath.Join(t.TempDir(), "ChatGPT.app")
if err := os.MkdirAll(destination, 0o755); err != nil {
t.Fatal(err)
}
if _, err := installCodexDesktopBundle(bundle, []string{destination}, func(string) error { return nil }); !errors.Is(err, errCodexDesktopDestinationExists) {
t.Fatalf("error = %v, want destination exists", err)
}
}
func TestInstallCodexDesktopBundleDoesNotOverwriteBrokenSymlink(t *testing.T) {
bundle := writeCodexDesktopTestBundle(t, "ChatGPT.app", "ChatGPT")
destination := filepath.Join(t.TempDir(), "ChatGPT.app")
if err := os.Symlink(filepath.Join(t.TempDir(), "missing"), destination); err != nil {
t.Fatal(err)
}
if _, err := installCodexDesktopBundle(bundle, []string{destination}, func(string) error { return nil }); !errors.Is(err, errCodexDesktopDestinationExists) {
t.Fatalf("error = %v, want destination exists", err)
}
}
func TestCodexDesktopBundleOnVolumeRejectsSymlink(t *testing.T) {
volume := t.TempDir()
target := writeCodexDesktopTestBundle(t, "ChatGPT.app", "ChatGPT")
if err := os.Symlink(target, filepath.Join(volume, "ChatGPT.app")); err != nil {
t.Fatal(err)
}
if _, err := codexDesktopBundleOnVolume(volume); err == nil {
t.Fatal("codexDesktopBundleOnVolume accepted a symlink")
}
}
func TestInstallCodexDesktopDiskImageRealArchive(t *testing.T) {
image := os.Getenv("OLLAMA_TEST_CODEX_DESKTOP_DMG")
if image == "" {
t.Skip("set OLLAMA_TEST_CODEX_DESKTOP_DMG to the official ChatGPT DMG")
}
destination := filepath.Join(t.TempDir(), "Applications", "ChatGPT.app")
installed, err := installCodexDesktopDiskImage(image, []string{destination}, verifyCodexDesktopBundle)
if err != nil {
t.Fatal(err)
}
if installed != destination {
t.Fatalf("installed = %q, want %q", installed, destination)
}
}
func writeCodexDesktopTestBundle(t *testing.T, appName, executableName string) string {
t.Helper()
bundle := filepath.Join(t.TempDir(), appName)
executable := filepath.Join(bundle, "Contents", "MacOS", executableName)
if err := os.MkdirAll(filepath.Dir(executable), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(executable, []byte("binary"), 0o755); err != nil {
t.Fatal(err)
}
return bundle
}
+80 -98
View File
@@ -16,7 +16,6 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
@@ -25,21 +24,11 @@ import (
"github.com/ollama/ollama/app/webview"
)
const (
defaultWindowWidth = 1360
defaultWindowHeight = 960
onboardingWindowWidth = 900
onboardingWindowHeight = 660
minimumWindowWidth = onboardingWindowWidth
minimumWindowHeight = onboardingWindowHeight
)
type Webview struct {
port int
token string
webview webview.WebView
mutex sync.Mutex
onboarding atomic.Bool
port int
token string
webview webview.WebView
mutex sync.Mutex
Store *store.Store
}
@@ -99,38 +88,85 @@ func (w *Webview) Run(path string) unsafe.Pointer {
// Windows-specific scrollbar styling
if runtime.GOOS == "windows" {
init += `
// Keep Edge WebView2 scrollbars aligned with the system theme.
// Fix scrollbar styling for Edge WebView2 on Windows only
function updateScrollbarStyles() {
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const existingStyle = document.getElementById('scrollbar-style');
if (existingStyle) existingStyle.remove();
const style = document.createElement('style');
style.id = 'scrollbar-style';
style.textContent = ` + "`" + `
::-webkit-scrollbar { width: 6px !important; height: 6px !important; }
::-webkit-scrollbar-track { background: #f0f0f0 !important; }
::-webkit-scrollbar-thumb { background: #c0c0c0 !important; border-radius: 6px !important; }
::-webkit-scrollbar-thumb:hover { background: #a0a0a0 !important; }
::-webkit-scrollbar-corner { background: #f0f0f0 !important; }
@media (prefers-color-scheme: dark) {
if (isDark) {
style.textContent = ` + "`" + `
::-webkit-scrollbar { width: 6px !important; height: 6px !important; }
::-webkit-scrollbar-track { background: #1a1a1a !important; }
::-webkit-scrollbar-thumb { background: #404040 !important; }
::-webkit-scrollbar-thumb { background: #404040 !important; border-radius: 6px !important; }
::-webkit-scrollbar-thumb:hover { background: #505050 !important; }
::-webkit-scrollbar-corner { background: #1a1a1a !important; }
}
::-webkit-scrollbar-button {
background: transparent !important;
border: none !important;
width: 0px !important;
height: 0px !important;
margin: 0 !important;
padding: 0 !important;
}
` + "`" + `;
::-webkit-scrollbar-button {
background: transparent !important;
border: none !important;
width: 0px !important;
height: 0px !important;
margin: 0 !important;
padding: 0 !important;
}
::-webkit-scrollbar-button:vertical:start:decrement {
background: transparent !important;
height: 0px !important;
}
::-webkit-scrollbar-button:vertical:end:increment {
background: transparent !important;
height: 0px !important;
}
::-webkit-scrollbar-button:horizontal:start:decrement {
background: transparent !important;
width: 0px !important;
}
::-webkit-scrollbar-button:horizontal:end:increment {
background: transparent !important;
width: 0px !important;
}
` + "`" + `;
} else {
style.textContent = ` + "`" + `
::-webkit-scrollbar { width: 6px !important; height: 6px !important; }
::-webkit-scrollbar-track { background: #f0f0f0 !important; }
::-webkit-scrollbar-thumb { background: #c0c0c0 !important; border-radius: 6px !important; }
::-webkit-scrollbar-thumb:hover { background: #a0a0a0 !important; }
::-webkit-scrollbar-corner { background: #f0f0f0 !important; }
::-webkit-scrollbar-button {
background: transparent !important;
border: none !important;
width: 0px !important;
height: 0px !important;
margin: 0 !important;
padding: 0 !important;
}
::-webkit-scrollbar-button:vertical:start:decrement {
background: transparent !important;
height: 0px !important;
}
::-webkit-scrollbar-button:vertical:end:increment {
background: transparent !important;
height: 0px !important;
}
::-webkit-scrollbar-button:horizontal:start:decrement {
background: transparent !important;
width: 0px !important;
}
::-webkit-scrollbar-button:horizontal:end:increment {
background: transparent !important;
width: 0px !important;
}
` + "`" + `;
}
document.head.appendChild(style);
}
window.addEventListener('load', updateScrollbarStyles);
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateScrollbarStyles);
`
}
// on windows make ctrl+n open new chat
@@ -151,32 +187,15 @@ func (w *Webview) Run(path string) unsafe.Pointer {
`
}
init += fmt.Sprintf(`
window.OLLAMA_PLATFORM = %q;
init += `
window.OLLAMA_WEBSEARCH = true;
`, runtime.GOOS)
`
wv.Init(init)
// Add keyboard handler for zoom
wv.Init(`
window.addEventListener('keydown', function(e) {
const isZoomShortcut = (e.metaKey || e.ctrlKey) && (
e.key === '+' || e.key === '=' || e.key === '-' ||
e.key === '_' || e.key === '0' ||
e.code === 'NumpadAdd' || e.code === 'NumpadSubtract'
);
// Keep fixed-scale onboarding and apps pages at their intended size.
const isFixedScalePage =
window.location.pathname === '/onboarding' ||
window.location.pathname === '/connect';
if (isFixedScalePage && isZoomShortcut) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
// CMD/Ctrl + Plus/Equals (zoom in)
if ((e.metaKey || e.ctrlKey) && (e.key === '+' || e.key === '=')) {
e.preventDefault();
@@ -218,42 +237,10 @@ func (w *Webview) Run(path string) unsafe.Pointer {
showWindow(wv.Window())
})
wv.Bind("activateOllama", func() {
showWindow(wv.Window())
})
bindClaudeDesktop(wv)
bindCodexDesktop(wv)
wv.Bind("close", func() {
hideWindow(wv.Window())
})
wv.Bind("setOnboardingWindow", func(enabled bool) {
w.onboarding.Store(enabled)
wv.Dispatch(func() {
if enabled {
wv.SetSize(onboardingWindowWidth, onboardingWindowHeight, webview.HintFixed)
setOnboardingWindowStyle(wv.Window(), true)
return
}
width, height := defaultWindowWidth, defaultWindowHeight
if w.Store != nil {
storedWidth, storedHeight, err := w.Store.WindowSize()
if err != nil {
slog.Error("failed to restore window size", "error", err)
} else if storedWidth > 0 && storedHeight > 0 {
width, height = storedWidth, storedHeight
}
}
wv.SetSize(width, height, webview.HintNone)
wv.SetSize(minimumWindowWidth, minimumWindowHeight, webview.HintMin)
setOnboardingWindowStyle(wv.Window(), false)
})
})
// Webviews do not allow access to the file system by default, so we need to
// bind file system operations here
wv.Bind("selectModelsDirectory", func() {
@@ -463,18 +450,18 @@ func (w *Webview) Run(path string) unsafe.Pointer {
}()
}
width, height := defaultWindowWidth, defaultWindowHeight
if w.Store != nil {
storedWidth, storedHeight, err := w.Store.WindowSize()
width, height, err := w.Store.WindowSize()
if err != nil {
slog.Error("failed to get window size", "error", err)
}
if storedWidth > 0 && storedHeight > 0 {
width, height = storedWidth, storedHeight
if width > 0 && height > 0 {
wv.SetSize(width, height, webview.HintNone)
} else {
wv.SetSize(800, 600, webview.HintNone)
}
}
wv.SetSize(width, height, webview.HintNone)
wv.SetSize(minimumWindowWidth, minimumWindowHeight, webview.HintMin)
wv.SetSize(800, 600, webview.HintMin)
w.webview = wv
w.webview.Navigate(url)
@@ -489,7 +476,6 @@ func (w *Webview) Run(path string) unsafe.Pointer {
}
func (w *Webview) Terminate() {
w.onboarding.Store(false)
w.mutex.Lock()
if w.webview == nil {
w.mutex.Unlock()
@@ -503,10 +489,6 @@ func (w *Webview) Terminate() {
wv.Destroy()
}
func (w *Webview) OnboardingActive() bool {
return w.onboarding.Load()
}
func (w *Webview) IsRunning() bool {
w.mutex.Lock()
defer w.mutex.Unlock()
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 320"><path d="m297.06 130.97c7.26-21.79 4.76-45.66-6.85-65.48-17.46-30.4-52.56-46.04-86.84-38.68-15.25-17.18-37.16-26.95-60.13-26.81-35.04-.08-66.13 22.48-76.91 55.82-22.51 4.61-41.94 18.7-53.31 38.67-17.59 30.32-13.58 68.54 9.92 94.54-7.26 21.79-4.76 45.66 6.85 65.48 17.46 30.4 52.56 46.04 86.84 38.68 15.24 17.18 37.16 26.95 60.13 26.8 35.06.09 66.16-22.49 76.94-55.86 22.51-4.61 41.94-18.7 53.31-38.67 17.57-30.32 13.55-68.51-9.94-94.51zm-120.28 168.11c-14.03.02-27.62-4.89-38.39-13.88.49-.26 1.34-.73 1.89-1.07l63.72-36.8c3.26-1.85 5.26-5.32 5.24-9.07v-89.83l26.93 15.55c.29.14.48.42.52.74v74.39c-.04 33.08-26.83 59.9-59.91 59.97zm-128.84-55.03c-7.03-12.14-9.56-26.37-7.15-40.18.47.28 1.3.79 1.89 1.13l63.72 36.8c3.23 1.89 7.23 1.89 10.47 0l77.79-44.92v31.1c.02.32-.13.63-.38.83l-64.41 37.19c-28.69 16.52-65.33 6.7-81.92-21.95zm-16.77-139.09c7-12.16 18.05-21.46 31.21-26.29 0 .55-.03 1.52-.03 2.2v73.61c-.02 3.74 1.98 7.21 5.23 9.06l77.79 44.91-26.93 15.55c-.27.18-.61.21-.91.08l-64.42-37.22c-28.63-16.58-38.45-53.21-21.95-81.89zm221.26 51.49-77.79-44.92 26.93-15.54c.27-.18.61-.21.91-.08l64.42 37.19c28.68 16.57 38.51 53.26 21.94 81.94-7.01 12.14-18.05 21.44-31.2 26.28v-75.81c.03-3.74-1.96-7.2-5.2-9.06zm26.8-40.34c-.47-.29-1.3-.79-1.89-1.13l-63.72-36.8c-3.23-1.89-7.23-1.89-10.47 0l-77.79 44.92v-31.1c-.02-.32.13-.63.38-.83l64.41-37.16c28.69-16.55 65.37-6.7 81.91 22 6.99 12.12 9.52 26.31 7.15 40.1zm-168.51 55.43-26.94-15.55c-.29-.14-.48-.42-.52-.74v-74.39c.02-33.12 26.89-59.96 60.01-59.94 14.01 0 27.57 4.92 38.34 13.88-.49.26-1.33.73-1.89 1.07l-63.72 36.8c-3.26 1.85-5.26 5.31-5.24 9.06l-.04 89.79zm14.63-31.54 34.65-20.01 34.65 20v40.01l-34.65 20-34.65-20z"/></svg>

Before

Width:  |  Height:  |  Size: 1.7 KiB

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Pixelmator Pro 3.6.17 -->
<svg width="1200" height="1200" viewBox="0 0 1200 1200" xmlns="http://www.w3.org/2000/svg">
<g id="g314">
<path id="path147" fill="#d97757" stroke="none" d="M 233.959793 800.214905 L 468.644287 668.536987 L 472.590637 657.100647 L 468.644287 650.738403 L 457.208069 650.738403 L 417.986633 648.322144 L 283.892639 644.69812 L 167.597321 639.865845 L 54.926208 633.825623 L 26.577238 627.785339 L 3.3e-05 592.751709 L 2.73832 575.27533 L 26.577238 559.248352 L 60.724873 562.228149 L 136.187973 567.382629 L 249.422867 575.194763 L 331.570496 580.026978 L 453.261841 592.671082 L 472.590637 592.671082 L 475.328857 584.859009 L 468.724915 580.026978 L 463.570557 575.194763 L 346.389313 495.785217 L 219.543671 411.865906 L 153.100723 363.543762 L 117.181267 339.060425 L 99.060455 316.107361 L 91.248367 266.01355 L 123.865784 230.093994 L 167.677887 233.073853 L 178.872513 236.053772 L 223.248367 270.201477 L 318.040283 343.570496 L 441.825592 434.738342 L 459.946411 449.798706 L 467.194672 444.64447 L 468.080597 441.020203 L 459.946411 427.409485 L 392.617493 305.718323 L 320.778564 181.932983 L 288.80542 130.630859 L 280.348999 99.865845 C 277.369171 87.221436 275.194641 76.590698 275.194641 63.624268 L 312.322174 13.20813 L 332.8591 6.604126 L 382.389313 13.20813 L 403.248352 31.328979 L 434.013519 101.71814 L 483.865753 212.537048 L 561.181274 363.221497 L 583.812134 407.919434 L 595.892639 449.315491 L 600.40271 461.959839 L 608.214783 461.959839 L 608.214783 454.711609 L 614.577271 369.825623 L 626.335632 265.61084 L 637.771851 131.516846 L 641.718201 93.745117 L 660.402832 48.483276 L 697.530334 24.000122 L 726.52356 37.852417 L 750.362549 72 L 747.060486 94.067139 L 732.886047 186.201416 L 705.100708 330.52356 L 686.979919 427.167847 L 697.530334 427.167847 L 709.61084 415.087341 L 758.496704 350.174561 L 840.644348 247.490051 L 876.885925 206.738342 L 919.167847 161.71814 L 946.308838 140.29541 L 997.61084 140.29541 L 1035.38269 196.429626 L 1018.469849 254.416199 L 965.637634 321.422852 L 921.825562 378.201538 L 859.006714 462.765259 L 819.785278 530.41626 L 823.409424 535.812073 L 832.75177 534.92627 L 974.657776 504.724915 L 1051.328979 490.872559 L 1142.818848 475.167786 L 1184.214844 494.496582 L 1188.724854 514.147644 L 1172.456421 554.335693 L 1074.604126 578.496765 L 959.838989 601.449829 L 788.939636 641.879272 L 786.845764 643.409485 L 789.261841 646.389343 L 866.255127 653.637634 L 899.194702 655.409424 L 979.812134 655.409424 L 1129.932861 666.604187 L 1169.154419 692.537109 L 1192.671265 724.268677 L 1188.724854 748.429688 L 1128.322144 779.194641 L 1046.818848 759.865845 L 856.590759 714.604126 L 791.355774 698.335754 L 782.335693 698.335754 L 782.335693 703.731567 L 836.69812 756.885986 L 936.322205 846.845581 L 1061.073975 962.81897 L 1067.436279 991.490112 L 1051.409424 1014.120911 L 1034.496704 1011.704712 L 924.885986 929.234924 L 882.604126 892.107544 L 786.845764 811.48999 L 780.483276 811.48999 L 780.483276 819.946289 L 802.550415 852.241699 L 919.087341 1027.409424 L 925.127625 1081.127686 L 916.671204 1098.604126 L 886.469849 1109.154419 L 853.288696 1103.114136 L 785.073914 1007.355835 L 714.684631 899.516785 L 657.906067 802.872498 L 650.979858 806.81897 L 617.476624 1167.704834 L 601.771851 1186.147705 L 565.530212 1200 L 535.328857 1177.046997 L 519.302124 1139.919556 L 535.328857 1066.550537 L 554.657776 970.792053 L 570.362488 894.68457 L 584.536926 800.134277 L 592.993347 768.724976 L 592.429626 766.630859 L 585.503479 767.516968 L 514.22821 865.369263 L 405.825531 1011.865906 L 320.053711 1103.677979 L 299.516815 1111.812256 L 263.919525 1093.369263 L 267.221497 1060.429688 L 287.114136 1031.114136 L 405.825531 880.107361 L 477.422913 786.52356 L 523.651062 732.483276 L 523.328918 724.671265 L 520.590698 724.671265 L 205.288605 929.395935 L 149.154434 936.644409 L 124.993355 914.01355 L 127.973183 876.885986 L 139.409409 864.80542 L 234.201385 799.570435 L 233.879227 799.8927 Z"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 4.0 KiB

+2 -2
View File
@@ -143,13 +143,13 @@ func utf16ptr(utf16 []uint16) *uint16 {
func utf16slice(ptr *uint16) []uint16 { //nolint:unused
hdr := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(ptr)), Len: 1, Cap: 1}
slice := *(*[]uint16)(unsafe.Pointer(&hdr)) //nolint:govet
slice := *((*[]uint16)(unsafe.Pointer(&hdr))) //nolint:govet
i := 0
for slice[len(slice)-1] != 0 {
i++
}
hdr.Len = i
slice = *(*[]uint16)(unsafe.Pointer(&hdr)) //nolint:govet
slice = *((*[]uint16)(unsafe.Pointer(&hdr))) //nolint:govet
return slice
}
+1 -1
View File
@@ -365,7 +365,7 @@ time=2025-06-30T09:25:56.197-07:00 level=DEBUG source=ggml.go:155 msg="key not f
if err != nil {
t.Fatalf("failed to write log file %s: %s", serverLogPath, err)
}
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Millisecond)
defer cancel()
info, err := GetInferenceInfo(ctx)
if err != nil {
+185 -59
View File
@@ -14,7 +14,7 @@ import (
// currentSchemaVersion defines the current database schema version.
// Increment this when making schema changes that require migrations.
const currentSchemaVersion = 18
const currentSchemaVersion = 17
// database wraps the SQLite connection.
// SQLite handles its own locking for concurrent access:
@@ -82,14 +82,12 @@ func (db *database) init() error {
websearch_enabled BOOLEAN NOT NULL DEFAULT 0,
selected_model TEXT NOT NULL DEFAULT '',
sidebar_open BOOLEAN NOT NULL DEFAULT 0,
last_home_view TEXT NOT NULL DEFAULT 'chat',
onboarding_version INTEGER NOT NULL DEFAULT 0,
last_home_view TEXT NOT NULL DEFAULT 'launch',
think_enabled BOOLEAN NOT NULL DEFAULT 0,
think_level TEXT NOT NULL DEFAULT '',
cloud_setting_migrated BOOLEAN NOT NULL DEFAULT 0,
remote TEXT NOT NULL DEFAULT '', -- deprecated
auto_update_enabled BOOLEAN NOT NULL DEFAULT 1,
claude_desktop_used BOOLEAN NOT NULL DEFAULT 0,
schema_version INTEGER NOT NULL DEFAULT %d
);
@@ -99,6 +97,8 @@ func (db *database) init() error {
CREATE TABLE IF NOT EXISTS chats (
id TEXT PRIMARY KEY,
title TEXT NOT NULL DEFAULT '',
model_name TEXT NOT NULL DEFAULT '',
source TEXT NOT NULL DEFAULT 'app',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
browser_state TEXT
);
@@ -109,6 +109,7 @@ func (db *database) init() error {
role TEXT NOT NULL,
content TEXT NOT NULL DEFAULT '',
thinking TEXT NOT NULL DEFAULT '',
images TEXT NOT NULL DEFAULT '[]',
stream BOOLEAN NOT NULL DEFAULT 0,
model_name TEXT,
model_cloud BOOLEAN, -- deprecated
@@ -118,15 +119,21 @@ func (db *database) init() error {
thinking_time_start TIMESTAMP,
thinking_time_end TIMESTAMP,
tool_result TEXT,
tool_name TEXT NOT NULL DEFAULT '',
tool_call_id TEXT NOT NULL DEFAULT '',
archived BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id ON messages(chat_id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_id ON messages(chat_id, id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_archived ON messages(chat_id, archived, id);
CREATE TABLE IF NOT EXISTS tool_calls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER NOT NULL,
type TEXT NOT NULL,
tool_call_id TEXT NOT NULL DEFAULT '',
function_name TEXT NOT NULL,
function_arguments TEXT NOT NULL,
function_result TEXT,
@@ -135,6 +142,17 @@ func (db *database) init() error {
CREATE INDEX IF NOT EXISTS idx_tool_calls_message_id ON tool_calls(message_id);
CREATE TABLE IF NOT EXISTS compactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
summary TEXT NOT NULL,
archived_message_ids TEXT NOT NULL DEFAULT '[]',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_compactions_chat_id ON compactions(chat_id, id);
CREATE TABLE IF NOT EXISTS attachments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER NOT NULL,
@@ -274,17 +292,11 @@ func (db *database) migrate() error {
}
version = 16
case 16:
// Existing users should not be shown onboarding after an upgrade.
// add agent chat metadata, message archiving, and compaction tables
if err := db.migrateV16ToV17(); err != nil {
return fmt.Errorf("migrate v16 to v17: %w", err)
}
version = 17
case 17:
// Remember that Claude Desktop has been connected at least once.
if err := db.migrateV17ToV18(); err != nil {
return fmt.Errorf("migrate v17 to v18: %w", err)
}
version = 18
default:
// If we have a version we don't recognize, just set it to current
// This might happen during development
@@ -292,6 +304,10 @@ func (db *database) migrate() error {
}
}
if err := db.ensureCurrentSchema(); err != nil {
return fmt.Errorf("ensure current schema: %w", err)
}
return nil
}
@@ -541,7 +557,7 @@ func (db *database) migrateV14ToV15() error {
// migrateV15ToV16 adds the last_home_view column to the settings table
func (db *database) migrateV15ToV16() error {
_, err := db.conn.Exec(`ALTER TABLE settings ADD COLUMN last_home_view TEXT NOT NULL DEFAULT 'chat'`)
_, err := db.conn.Exec(`ALTER TABLE settings ADD COLUMN last_home_view TEXT NOT NULL DEFAULT 'launch'`)
if err != nil && !duplicateColumnError(err) {
return fmt.Errorf("add last_home_view column: %w", err)
}
@@ -554,38 +570,128 @@ func (db *database) migrateV15ToV16() error {
return nil
}
// migrateV16ToV17 adds versioned onboarding state. The schema default stays at
// zero for genuinely new installs, while all existing rows are marked complete
// and moved off the retired launch home view.
// migrateV16ToV17 adds the agent chat persistence fields to the app database.
func (db *database) migrateV16ToV17() error {
_, err := db.conn.Exec(`ALTER TABLE settings ADD COLUMN onboarding_version INTEGER NOT NULL DEFAULT 0`)
if err != nil && !duplicateColumnError(err) {
return fmt.Errorf("add onboarding_version column: %w", err)
for _, stmt := range []struct {
sql string
msg string
}{
{`ALTER TABLE chats ADD COLUMN model_name TEXT NOT NULL DEFAULT ''`, "add chats.model_name"},
{`ALTER TABLE chats ADD COLUMN source TEXT NOT NULL DEFAULT 'app'`, "add chats.source"},
{`ALTER TABLE messages ADD COLUMN images TEXT NOT NULL DEFAULT '[]'`, "add messages.images"},
{`ALTER TABLE messages ADD COLUMN tool_name TEXT NOT NULL DEFAULT ''`, "add messages.tool_name"},
{`ALTER TABLE messages ADD COLUMN tool_call_id TEXT NOT NULL DEFAULT ''`, "add messages.tool_call_id"},
{`ALTER TABLE messages ADD COLUMN archived BOOLEAN NOT NULL DEFAULT 0`, "add messages.archived"},
{`ALTER TABLE tool_calls ADD COLUMN tool_call_id TEXT NOT NULL DEFAULT ''`, "add tool_calls.tool_call_id"},
} {
_, err := db.conn.Exec(stmt.sql)
if err != nil && !duplicateColumnError(err) {
return fmt.Errorf("%s: %w", stmt.msg, err)
}
}
_, err = db.conn.Exec(`UPDATE settings SET onboarding_version = 1, last_home_view = 'chat', schema_version = 17`)
_, err := db.conn.Exec(`
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_id ON messages(chat_id, id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_archived ON messages(chat_id, archived, id);
CREATE TABLE IF NOT EXISTS compactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id TEXT NOT NULL,
summary TEXT NOT NULL,
archived_message_ids TEXT NOT NULL DEFAULT '[]',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_compactions_chat_id ON compactions(chat_id, id);
UPDATE settings SET schema_version = 17;
`)
if err != nil {
return fmt.Errorf("complete onboarding for existing users: %w", err)
return fmt.Errorf("create agent chat persistence tables: %w", err)
}
return nil
}
// migrateV17ToV18 adds durable Claude Desktop integration history.
func (db *database) migrateV17ToV18() error {
_, err := db.conn.Exec(`ALTER TABLE settings ADD COLUMN claude_desktop_used BOOLEAN NOT NULL DEFAULT 0`)
if err != nil && !duplicateColumnError(err) {
return fmt.Errorf("add claude_desktop_used column: %w", err)
}
_, err = db.conn.Exec(`UPDATE settings SET schema_version = 18`)
func (db *database) ensureCurrentSchema() error {
complete, err := db.agentPersistenceSchemaComplete()
if err != nil {
return fmt.Errorf("update schema version: %w", err)
return err
}
if complete {
return nil
}
if err := db.migrateV16ToV17(); err != nil {
return err
}
complete, err = db.agentPersistenceSchemaComplete()
if err != nil {
return err
}
if !complete {
return fmt.Errorf("agent persistence schema is incomplete")
}
return nil
}
func (db *database) agentPersistenceSchemaComplete() (bool, error) {
for _, table := range []string{"compactions"} {
exists, err := db.tableExists(table)
if err != nil || !exists {
return false, err
}
}
for _, column := range []struct {
table string
name string
}{
{"chats", "model_name"},
{"chats", "source"},
{"messages", "images"},
{"messages", "tool_name"},
{"messages", "tool_call_id"},
{"messages", "archived"},
{"tool_calls", "tool_call_id"},
} {
exists, err := db.columnExists(column.table, column.name)
if err != nil || !exists {
return false, err
}
}
return true, nil
}
func (db *database) tableExists(table string) (bool, error) {
var count int
if err := db.conn.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`, table).Scan(&count); err != nil {
return false, err
}
return count > 0, nil
}
func (db *database) columnExists(table, column string) (bool, error) {
rows, err := db.conn.Query(fmt.Sprintf("PRAGMA table_info(%s)", table))
if err != nil {
return false, err
}
defer rows.Close()
for rows.Next() {
var cid int
var name, dataType sql.NullString
var notNull, primaryKey int
var defaultValue sql.NullString
if err := rows.Scan(&cid, &name, &dataType, &notNull, &defaultValue, &primaryKey); err != nil {
return false, err
}
if name.String == column {
return true, nil
}
}
if err := rows.Err(); err != nil {
return false, err
}
return false, nil
}
// cleanupOrphanedData removes orphaned records that may exist due to the foreign key bug
func (db *database) cleanupOrphanedData() error {
_, err := db.conn.Exec(`
@@ -630,18 +736,21 @@ func (db *database) getAllChats() ([]Chat, error) {
c.id,
c.title,
c.created_at,
COALESCE(first_msg.content, '') as first_user_content,
COALESCE(datetime(MAX(m.updated_at)), datetime(c.created_at)) as last_updated
COALESCE((
SELECT fm.content
FROM messages fm
WHERE fm.chat_id = c.id
AND fm.role = 'user'
AND fm.archived = 0
ORDER BY fm.id ASC
LIMIT 1
), '') as first_user_content,
COALESCE(MAX(m.updated_at), c.created_at) as last_updated
FROM chats c
LEFT JOIN (
SELECT chat_id, content, MIN(id) as min_id
FROM messages
WHERE role = 'user'
GROUP BY chat_id
) first_msg ON c.id = first_msg.chat_id
LEFT JOIN messages m ON c.id = m.chat_id
GROUP BY c.id, c.title, c.created_at, first_msg.content
ORDER BY last_updated DESC
LEFT JOIN messages m ON c.id = m.chat_id AND m.archived = 0
WHERE c.source = 'app'
GROUP BY c.id, c.title, c.created_at
ORDER BY last_updated DESC, COALESCE(MAX(m.id), 0) DESC, c.created_at DESC, c.id DESC
`
rows, err := db.conn.Query(query)
@@ -664,25 +773,27 @@ func (db *database) getAllChats() ([]Chat, error) {
&firstUserContent,
&lastUpdatedStr,
)
// Parse the last updated time
lastUpdated, _ := time.Parse("2006-01-02 15:04:05", lastUpdatedStr)
if err != nil {
return nil, fmt.Errorf("scan chat: %w", err)
}
lastUpdated, err := parseAgentSQLiteTime(lastUpdatedStr)
if err != nil {
return nil, fmt.Errorf("parse chat updated_at: %w", err)
}
chat.CreatedAt = createdAt
// Add a dummy first user message for the UI to display
// This is just for the excerpt, full messages are loaded when needed
chat.Messages = []Message{}
if firstUserContent != "" {
chat.Messages = append(chat.Messages, Message{
Role: "user",
Content: firstUserContent,
UpdatedAt: lastUpdated,
})
// Add a summary message for the UI to display the excerpt and latest update.
// Full messages are loaded when a chat is opened.
summary := Message{
UpdatedAt: lastUpdated,
}
if firstUserContent != "" {
summary.Role = "user"
summary.Content = firstUserContent
}
chat.Messages = []Message{summary}
chats = append(chats, chat)
}
@@ -826,6 +937,7 @@ func (db *database) updateLastMessage(chatID string, msg Message) error {
var messageID int64
err = tx.QueryRow(`
SELECT MAX(id) FROM messages WHERE chat_id = ?
AND archived = 0
`, chatID).Scan(&messageID)
if err != nil {
return fmt.Errorf("get last message id: %w", err)
@@ -933,7 +1045,7 @@ func (db *database) getMessages(chatID string, loadAttachmentData bool) ([]Messa
query := `
SELECT id, role, content, thinking, stream, model_name, created_at, updated_at, thinking_time_start, thinking_time_end, tool_result
FROM messages
WHERE chat_id = ?
WHERE chat_id = ? AND archived = 0
ORDER BY id ASC
`
@@ -1234,9 +1346,9 @@ func (db *database) getSettings() (Settings, error) {
var s Settings
err := db.conn.QueryRow(`
SELECT expose, survey, browser, models, agent, tools, working_dir, context_length, turbo_enabled, websearch_enabled, selected_model, sidebar_open, last_home_view, onboarding_version, think_enabled, think_level, auto_update_enabled, claude_desktop_used
SELECT expose, survey, browser, models, agent, tools, working_dir, context_length, turbo_enabled, websearch_enabled, selected_model, sidebar_open, last_home_view, think_enabled, think_level, auto_update_enabled
FROM settings
`).Scan(&s.Expose, &s.Survey, &s.Browser, &s.Models, &s.Agent, &s.Tools, &s.WorkingDir, &s.ContextLength, &s.TurboEnabled, &s.WebSearchEnabled, &s.SelectedModel, &s.SidebarOpen, &s.LastHomeView, &s.OnboardingVersion, &s.ThinkEnabled, &s.ThinkLevel, &s.AutoUpdateEnabled, &s.ClaudeDesktopUsed)
`).Scan(&s.Expose, &s.Survey, &s.Browser, &s.Models, &s.Agent, &s.Tools, &s.WorkingDir, &s.ContextLength, &s.TurboEnabled, &s.WebSearchEnabled, &s.SelectedModel, &s.SidebarOpen, &s.LastHomeView, &s.ThinkEnabled, &s.ThinkLevel, &s.AutoUpdateEnabled)
if err != nil {
return Settings{}, fmt.Errorf("get settings: %w", err)
}
@@ -1246,14 +1358,28 @@ func (db *database) getSettings() (Settings, error) {
func (db *database) setSettings(s Settings) error {
lastHomeView := strings.ToLower(strings.TrimSpace(s.LastHomeView))
validLaunchView := map[string]struct{}{
"launch": {},
"openclaw": {},
"claude": {},
"hermes": {},
"codex": {},
"codex-app": {},
"copilot": {},
"opencode": {},
"droid": {},
"pi": {},
}
if lastHomeView != "chat" {
lastHomeView = "chat"
if _, ok := validLaunchView[lastHomeView]; !ok {
lastHomeView = "launch"
}
}
_, err := db.conn.Exec(`
UPDATE settings
SET expose = ?, survey = ?, browser = ?, models = ?, agent = ?, tools = ?, working_dir = ?, context_length = ?, turbo_enabled = ?, websearch_enabled = ?, selected_model = ?, sidebar_open = ?, last_home_view = ?, onboarding_version = ?, think_enabled = ?, think_level = ?, auto_update_enabled = ?, claude_desktop_used = ?
`, s.Expose, s.Survey, s.Browser, s.Models, s.Agent, s.Tools, s.WorkingDir, s.ContextLength, s.TurboEnabled, s.WebSearchEnabled, s.SelectedModel, s.SidebarOpen, lastHomeView, s.OnboardingVersion, s.ThinkEnabled, s.ThinkLevel, s.AutoUpdateEnabled, s.ClaudeDesktopUsed)
SET expose = ?, survey = ?, browser = ?, models = ?, agent = ?, tools = ?, working_dir = ?, context_length = ?, turbo_enabled = ?, websearch_enabled = ?, selected_model = ?, sidebar_open = ?, last_home_view = ?, think_enabled = ?, think_level = ?, auto_update_enabled = ?
`, s.Expose, s.Survey, s.Browser, s.Models, s.Agent, s.Tools, s.WorkingDir, s.ContextLength, s.TurboEnabled, s.WebSearchEnabled, s.SelectedModel, s.SidebarOpen, lastHomeView, s.ThinkEnabled, s.ThinkLevel, s.AutoUpdateEnabled)
if err != nil {
return fmt.Errorf("set settings: %w", err)
}
+86 -65
View File
@@ -135,7 +135,7 @@ func TestMigrationV13ToV14ContextLength(t *testing.T) {
}
}
func TestMigrationV15ToV16LastHomeViewMigratesToChat(t *testing.T) {
func TestMigrationV15ToV16LastHomeViewDefaultsToLaunch(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
@@ -161,8 +161,8 @@ func TestMigrationV15ToV16LastHomeViewMigratesToChat(t *testing.T) {
t.Fatalf("failed to read last_home_view: %v", err)
}
if lastHomeView != "chat" {
t.Fatalf("expected last_home_view to migrate to chat, got %q", lastHomeView)
if lastHomeView != "launch" {
t.Fatalf("expected last_home_view to default to launch after migration, got %q", lastHomeView)
}
version, err := db.getSchemaVersion()
@@ -174,85 +174,97 @@ func TestMigrationV15ToV16LastHomeViewMigratesToChat(t *testing.T) {
}
}
func TestOnboardingVersionDefaultsAndMigration(t *testing.T) {
t.Run("fresh installs need onboarding", func(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "fresh.db")
db, err := newDatabase(dbPath)
if err != nil {
t.Fatalf("failed to create database: %v", err)
}
defer db.Close()
func TestMigrationV16ToV17AddsAgentSchema(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
settings, err := db.getSettings()
if err != nil {
t.Fatalf("failed to read settings: %v", err)
}
if settings.OnboardingVersion != 0 {
t.Fatalf("expected fresh install onboarding version 0, got %d", settings.OnboardingVersion)
}
})
t.Run("existing installs skip onboarding", func(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "existing.db")
db, err := newDatabase(dbPath)
if err != nil {
t.Fatalf("failed to create database: %v", err)
}
defer db.Close()
if _, err := db.conn.Exec(`
ALTER TABLE settings DROP COLUMN onboarding_version;
UPDATE settings SET schema_version = 16;
`); err != nil {
t.Fatalf("failed to seed v16 settings row: %v", err)
}
if err := db.migrate(); err != nil {
t.Fatalf("migration from v16 to v17 failed: %v", err)
}
settings, err := db.getSettings()
if err != nil {
t.Fatalf("failed to read settings: %v", err)
}
if settings.OnboardingVersion != 1 {
t.Fatalf("expected existing install onboarding version 1, got %d", settings.OnboardingVersion)
}
})
}
func TestClaudeDesktopUsedDefaultsAndMigration(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "claude-history.db")
db, err := newDatabase(dbPath)
if err != nil {
t.Fatalf("failed to create database: %v", err)
}
defer db.Close()
settings, err := db.getSettings()
if _, err := db.conn.Exec(`
DROP INDEX IF EXISTS idx_messages_chat_id_id;
DROP INDEX IF EXISTS idx_messages_chat_id_archived;
DROP INDEX IF EXISTS idx_compactions_chat_id;
DROP TABLE IF EXISTS compactions;
ALTER TABLE chats DROP COLUMN model_name;
ALTER TABLE chats DROP COLUMN source;
ALTER TABLE messages DROP COLUMN images;
ALTER TABLE messages DROP COLUMN tool_name;
ALTER TABLE messages DROP COLUMN tool_call_id;
ALTER TABLE messages DROP COLUMN archived;
ALTER TABLE tool_calls DROP COLUMN tool_call_id;
UPDATE settings SET schema_version = 16;
`); err != nil {
t.Fatalf("failed to seed v16 schema: %v", err)
}
if err := db.migrate(); err != nil {
t.Fatalf("migration from v16 to v17 failed: %v", err)
}
version, err := db.getSchemaVersion()
if err != nil {
t.Fatalf("failed to read settings: %v", err)
t.Fatalf("failed to get schema version: %v", err)
}
if settings.ClaudeDesktopUsed {
t.Fatal("expected fresh installs to have no Claude Desktop history")
if version != 17 {
t.Fatalf("expected schema version 17, got %d", version)
}
columns := columnMap(db)
for _, want := range []struct {
table string
column string
}{
{"chats", "model_name TEXT NOT NULL DEFAULT ''"},
{"chats", "source TEXT NOT NULL DEFAULT 'app'"},
{"messages", "images TEXT NOT NULL DEFAULT '[]'"},
{"messages", "archived BOOLEAN NOT NULL DEFAULT 0"},
{"tool_calls", "tool_call_id TEXT NOT NULL DEFAULT ''"},
} {
if !containsString(columns[want.table], want.column) {
t.Fatalf("%s columns missing %q: %#v", want.table, want.column, columns[want.table])
}
}
if _, ok := columns["compactions"]; !ok {
t.Fatalf("compactions table was not created: %#v", columns)
}
}
func TestMigrationRepairsIncompleteCurrentSchema(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
db, err := newDatabase(dbPath)
if err != nil {
t.Fatalf("failed to create database: %v", err)
}
defer db.Close()
if _, err := db.conn.Exec(`
ALTER TABLE settings DROP COLUMN claude_desktop_used;
ALTER TABLE chats DROP COLUMN source;
UPDATE settings SET schema_version = 17;
`); err != nil {
t.Fatalf("failed to seed v17 settings row: %v", err)
}
if err := db.migrate(); err != nil {
t.Fatalf("migration from v17 to v18 failed: %v", err)
t.Fatalf("failed to seed incomplete current schema: %v", err)
}
settings, err = db.getSettings()
if err != nil {
t.Fatalf("failed to read migrated settings: %v", err)
if err := db.migrate(); err != nil {
t.Fatalf("migration repair failed: %v", err)
}
if settings.ClaudeDesktopUsed {
t.Fatal("expected existing installs to start with no inferred Claude Desktop history")
version, err := db.getSchemaVersion()
if err != nil {
t.Fatalf("failed to get schema version: %v", err)
}
if version != currentSchemaVersion {
t.Fatalf("expected schema version %d, got %d", currentSchemaVersion, version)
}
columns := columnMap(db)
if !containsString(columns["chats"], "source TEXT NOT NULL DEFAULT 'app'") {
t.Fatalf("chats.source was not repaired: %#v", columns["chats"])
}
}
@@ -451,6 +463,15 @@ func countRowsWithCondition(t *testing.T, db *database, table, condition string,
return count
}
func containsString(values []string, want string) bool {
for _, value := range values {
if value == want {
return true
}
}
return false
}
// Test helpers for schema migration testing
// schemaMap returns both tables/columns and indexes (ignoring order)
+1 -1
View File
@@ -41,7 +41,7 @@ func ImgBytes(path string) ([]byte, error) {
func (s *Store) ImgDir() string {
dbPath := s.DBPath
if dbPath == "" {
dbPath = defaultDBPath
dbPath = defaultDBPath()
}
storeDir := filepath.Dir(dbPath)
return filepath.Join(storeDir, "cache", "images")
-8
View File
@@ -57,14 +57,6 @@ func TestConfigMigration(t *testing.T) {
t.Error("expected has completed first run to be true after migration")
}
settings, err := s.Settings()
if err != nil {
t.Fatalf("failed to get settings: %v", err)
}
if settings.OnboardingVersion != CurrentOnboardingVersion {
t.Fatalf("expected migrated user to skip onboarding, got version %d", settings.OnboardingVersion)
}
// Verify migration is marked as complete
migrated, err := s.db.isConfigMigrated()
if err != nil {
+5 -23
View File
@@ -107,6 +107,7 @@ type Chat struct {
ID string `json:"id"`
Messages []Message `json:"messages"`
Title string `json:"title"`
Model string `json:"model,omitempty"`
CreatedAt time.Time `json:"created_at"`
BrowserState json.RawMessage `json:"browser_state,omitempty" ts_type:"BrowserStateData"`
}
@@ -167,22 +168,13 @@ type Settings struct {
// SidebarOpen indicates if the chat sidebar is open
SidebarOpen bool
// LastHomeView is retained for settings compatibility and resolves to chat.
// LastHomeView stores the preferred home route target ("chat" or integration name)
LastHomeView string
// OnboardingVersion stores the latest onboarding flow the user has completed.
OnboardingVersion int
// AutoUpdateEnabled indicates if automatic updates should be downloaded
AutoUpdateEnabled bool
// ClaudeDesktopUsed records whether Claude Desktop has ever been connected through Ollama.
ClaudeDesktopUsed bool
}
// Keep in sync with CURRENT_ONBOARDING_VERSION in app/ui/app/src/lib/onboarding.ts.
const CurrentOnboardingVersion = 1
type Store struct {
// DBPath allows overriding the default database path (mainly for testing)
DBPath string
@@ -201,7 +193,7 @@ var defaultDBPath = func() string {
default:
return filepath.Join(os.Getenv("HOME"), ".ollama", "db.sqlite")
}
}()
}
// legacyConfigPath is the path to the old config.json file
var legacyConfigPath = func() string {
@@ -238,7 +230,7 @@ func (s *Store) ensureDB() error {
dbPath := s.DBPath
if dbPath == "" {
dbPath = defaultDBPath
dbPath = defaultDBPath()
}
// Ensure directory exists
@@ -343,16 +335,6 @@ func (s *Store) migrateFromConfig(database *database) error {
if err := database.setHasCompletedFirstRun(hasCompleted); err != nil {
return fmt.Errorf("migrate first time run: %w", err)
}
if hasCompleted {
settings, err := database.getSettings()
if err != nil {
return fmt.Errorf("read settings for onboarding migration: %w", err)
}
settings.OnboardingVersion = CurrentOnboardingVersion
if err := database.setSettings(settings); err != nil {
return fmt.Errorf("migrate onboarding completion: %w", err)
}
}
slog.Info("migrated first run status from config.json", "hasCompleted", hasCompleted)
// Mark as migrated
@@ -412,7 +394,7 @@ func (s *Store) Settings() (Settings, error) {
}
if settings.LastHomeView == "" {
settings.LastHomeView = "chat"
settings.LastHomeView = "launch"
}
return settings, nil
+88 -49
View File
@@ -5,6 +5,7 @@ package store
import (
"path/filepath"
"testing"
"time"
)
func TestStore(t *testing.T) {
@@ -81,18 +82,18 @@ func TestStore(t *testing.T) {
}
})
t.Run("settings default home view is chat", func(t *testing.T) {
t.Run("settings default home view is launch", func(t *testing.T) {
loaded, err := s.Settings()
if err != nil {
t.Fatal(err)
}
if loaded.LastHomeView != "chat" {
t.Fatalf("expected default LastHomeView to be chat, got %q", loaded.LastHomeView)
if loaded.LastHomeView != "launch" {
t.Fatalf("expected default LastHomeView to be launch, got %q", loaded.LastHomeView)
}
})
t.Run("settings empty home view falls back to chat", func(t *testing.T) {
t.Run("settings empty home view falls back to launch", func(t *testing.T) {
if err := s.SetSettings(Settings{LastHomeView: ""}); err != nil {
t.Fatal(err)
}
@@ -102,12 +103,12 @@ func TestStore(t *testing.T) {
t.Fatal(err)
}
if loaded.LastHomeView != "chat" {
t.Fatalf("expected empty LastHomeView to fall back to chat, got %q", loaded.LastHomeView)
if loaded.LastHomeView != "launch" {
t.Fatalf("expected empty LastHomeView to fall back to launch, got %q", loaded.LastHomeView)
}
})
t.Run("settings retired home view falls back to chat", func(t *testing.T) {
t.Run("settings disabled home view falls back to launch", func(t *testing.T) {
if err := s.SetSettings(Settings{LastHomeView: "claude-desktop"}); err != nil {
t.Fatal(err)
}
@@ -117,12 +118,12 @@ func TestStore(t *testing.T) {
t.Fatal(err)
}
if loaded.LastHomeView != "chat" {
t.Fatalf("expected retired LastHomeView to fall back to chat, got %q", loaded.LastHomeView)
if loaded.LastHomeView != "launch" {
t.Fatalf("expected disabled LastHomeView to fall back to launch, got %q", loaded.LastHomeView)
}
})
t.Run("settings integration home view falls back to chat", func(t *testing.T) {
t.Run("settings codex app home view is accepted", func(t *testing.T) {
if err := s.SetSettings(Settings{LastHomeView: "codex-app"}); err != nil {
t.Fatal(err)
}
@@ -132,8 +133,8 @@ func TestStore(t *testing.T) {
t.Fatal(err)
}
if loaded.LastHomeView != "chat" {
t.Fatalf("expected integration LastHomeView to fall back to chat, got %q", loaded.LastHomeView)
if loaded.LastHomeView != "codex-app" {
t.Fatalf("expected codex-app LastHomeView to be preserved, got %q", loaded.LastHomeView)
}
})
@@ -227,55 +228,93 @@ func TestStore(t *testing.T) {
})
}
func TestOnboardingVersionRoundTrip(t *testing.T) {
func TestStoreChatSummariesUseLatestActivity(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
settings, err := s.Settings()
base := time.Date(2026, 6, 23, 15, 30, 45, 0, time.UTC)
oldChat := NewChat("chat-old")
oldChat.Title = "Old Chat"
oldChat.CreatedAt = base
oldChat.Messages = []Message{
{
Role: "user",
Content: "older first prompt",
CreatedAt: base.Add(100 * time.Millisecond),
UpdatedAt: base.Add(100 * time.Millisecond),
},
{
Role: "user",
Content: "older second prompt",
CreatedAt: base.Add(200 * time.Millisecond),
UpdatedAt: base.Add(200 * time.Millisecond),
},
}
if err := s.SetChat(*oldChat); err != nil {
t.Fatalf("failed to save old chat: %v", err)
}
newChat := NewChat("chat-new")
newChat.Title = "New Chat"
newChat.CreatedAt = base
newChat.Messages = []Message{
{
Role: "user",
Content: "newer prompt",
CreatedAt: base.Add(900 * time.Millisecond),
UpdatedAt: base.Add(900 * time.Millisecond),
},
}
if err := s.SetChat(*newChat); err != nil {
t.Fatalf("failed to save new chat: %v", err)
}
activityOnlyChat := NewChat("chat-activity-only")
activityOnlyChat.Title = "Activity Only Chat"
activityOnlyChat.CreatedAt = base
activityOnlyChat.Messages = []Message{
{
Role: "assistant",
Content: "recent assistant activity",
CreatedAt: base.Add(1500 * time.Millisecond),
UpdatedAt: base.Add(1500 * time.Millisecond),
},
}
if err := s.SetChat(*activityOnlyChat); err != nil {
t.Fatalf("failed to save activity-only chat: %v", err)
}
chats, err := s.Chats()
if err != nil {
t.Fatal(err)
t.Fatalf("failed to list chats: %v", err)
}
if settings.OnboardingVersion != 0 {
t.Fatalf("expected onboarding version 0 by default, got %d", settings.OnboardingVersion)
if len(chats) != 3 {
t.Fatalf("expected 3 chats, got %d", len(chats))
}
if chats[0].ID != "chat-activity-only" {
t.Fatalf("expected chat-activity-only first, got %s", chats[0].ID)
}
settings.OnboardingVersion = 1
if err := s.SetSettings(settings); err != nil {
t.Fatal(err)
for _, chat := range chats {
if len(chat.Messages) != 1 {
t.Fatalf("expected summary message for %s, got %d messages", chat.ID, len(chat.Messages))
}
}
loaded, err := s.Settings()
if err != nil {
t.Fatal(err)
if !chats[0].Messages[0].UpdatedAt.Equal(activityOnlyChat.Messages[0].UpdatedAt) {
t.Fatalf("expected latest activity updated_at %s, got %s", activityOnlyChat.Messages[0].UpdatedAt, chats[0].Messages[0].UpdatedAt)
}
if loaded.OnboardingVersion != 1 {
t.Fatalf("expected onboarding version 1, got %d", loaded.OnboardingVersion)
if chats[0].Messages[0].Role != "" || chats[0].Messages[0].Content != "" {
t.Fatalf("expected activity-only chat to have no user excerpt, got role=%q content=%q", chats[0].Messages[0].Role, chats[0].Messages[0].Content)
}
}
func TestClaudeDesktopUsedRoundTrip(t *testing.T) {
s, cleanup := setupTestStore(t)
defer cleanup()
settings, err := s.Settings()
if err != nil {
t.Fatal(err)
if chats[1].ID != "chat-new" {
t.Fatalf("expected chat-new second, got %s", chats[1].ID)
}
if settings.ClaudeDesktopUsed {
t.Fatal("expected Claude Desktop history to be false by default")
if !chats[1].Messages[0].UpdatedAt.Equal(newChat.Messages[0].UpdatedAt) {
t.Fatalf("expected precise updated_at %s, got %s", newChat.Messages[0].UpdatedAt, chats[1].Messages[0].UpdatedAt)
}
settings.ClaudeDesktopUsed = true
if err := s.SetSettings(settings); err != nil {
t.Fatal(err)
}
loaded, err := s.Settings()
if err != nil {
t.Fatal(err)
}
if !loaded.ClaudeDesktopUsed {
t.Fatal("expected Claude Desktop history to persist")
if chats[2].Messages[0].Content != "older first prompt" {
t.Fatalf("expected first user prompt excerpt, got %q", chats[2].Messages[0].Content)
}
}
+31
View File
@@ -0,0 +1,31 @@
//go:build windows || darwin
package store
import (
"fmt"
"strings"
"time"
)
func parseAgentSQLiteTime(value string) (time.Time, error) {
value = strings.TrimSpace(value)
if value == "" {
return time.Time{}, nil
}
for _, layout := range []string{
time.RFC3339Nano,
"2006-01-02 15:04:05.999999999-07:00",
"2006-01-02 15:04:05.999999999Z07:00",
"2006-01-02 15:04:05.999999999",
"2006-01-02 15:04:05-07:00",
"2006-01-02 15:04:05Z07:00",
"2006-01-02 15:04:05",
} {
t, err := time.Parse(layout, value)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unsupported time format %q", value)
}
-4
View File
@@ -415,9 +415,7 @@ export class Settings {
SelectedModel: string;
SidebarOpen: boolean;
LastHomeView: string;
OnboardingVersion: number;
AutoUpdateEnabled: boolean;
ClaudeDesktopUsed: boolean;
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
@@ -436,9 +434,7 @@ export class Settings {
this.SelectedModel = source["SelectedModel"];
this.SidebarOpen = source["SidebarOpen"];
this.LastHomeView = source["LastHomeView"];
this.OnboardingVersion = source["OnboardingVersion"];
this.AutoUpdateEnabled = source["AutoUpdateEnabled"];
this.ClaudeDesktopUsed = source["ClaudeDesktopUsed"];
}
}
export class SettingsResponse {
+1 -2
View File
@@ -2,13 +2,12 @@
<html lang="en" style="overflow: hidden">
<head>
<meta charset="UTF-8" />
<meta name="color-scheme" content="light dark" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/src/index.css" />
<title>Ollama</title>
</head>
<body class="bg-white dark:bg-neutral-900 select-text">
<body class="dark:bg-neutral-900 select-text">
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script>
-33
View File
@@ -43,7 +43,6 @@
"@types/node": "^24.7.2",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@types/react-test-renderer": "^19.1.0",
"@vitejs/plugin-react": "^4.4.1",
"@vitest/browser": "^3.2.4",
"@vitest/coverage-v8": "^3.2.4",
@@ -57,7 +56,6 @@
"playwright": "^1.53.2",
"postcss-preset-env": "^10.2.4",
"react-markdown": "^10.1.0",
"react-test-renderer": "19.1.0",
"remark": "^15.0.1",
"remark-gfm": "^4.0.1",
"remark-stringify": "^11.0.0",
@@ -4595,16 +4593,6 @@
"@types/react": "^19.0.0"
}
},
"node_modules/@types/react-test-renderer": {
"version": "19.1.0",
"resolved": "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-19.1.0.tgz",
"integrity": "sha512-XD0WZrHqjNrxA/MaR9O22w/RNidWR9YZmBdRGI7wcnWGrv/3dA8wKCJ8m63Sn+tLJhcjmuhOi629N66W6kgWzQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@types/react": "*"
}
},
"node_modules/@types/resolve": {
"version": "1.20.6",
"resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.6.tgz",
@@ -11164,27 +11152,6 @@
"node": ">=0.10.0"
}
},
"node_modules/react-test-renderer": {
"version": "19.1.0",
"resolved": "https://registry.npmjs.org/react-test-renderer/-/react-test-renderer-19.1.0.tgz",
"integrity": "sha512-jXkSl3CpvPYEF+p/eGDLB4sPoDX8pKkYvRl9+rR8HxLY0X04vW7hCm1/0zHoUSjPZ3bDa+wXWNTDVIw/R8aDVw==",
"dev": true,
"license": "MIT",
"dependencies": {
"react-is": "^19.1.0",
"scheduler": "^0.26.0"
},
"peerDependencies": {
"react": "^19.1.0"
}
},
"node_modules/react-test-renderer/node_modules/react-is": {
"version": "19.2.8",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-19.2.8.tgz",
"integrity": "sha512-s5un28nYxKJw5gvUHyW5PCC28CvBqLu9r3cWgzHT4Vo/5fqqkFcdRYsGcKf50WMPpjjFZS5d76fn3YCo2njKwQ==",
"dev": true,
"license": "MIT"
},
"node_modules/readdirp": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
-2
View File
@@ -52,7 +52,6 @@
"@types/node": "^24.7.2",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@types/react-test-renderer": "^19.1.0",
"@vitejs/plugin-react": "^4.4.1",
"@vitest/browser": "^3.2.4",
"@vitest/coverage-v8": "^3.2.4",
@@ -66,7 +65,6 @@
"playwright": "^1.53.2",
"postcss-preset-env": "^10.2.4",
"react-markdown": "^10.1.0",
"react-test-renderer": "19.1.0",
"remark": "^15.0.1",
"remark-gfm": "^4.0.1",
"remark-stringify": "^11.0.0",
Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 21 KiB

-8
View File
@@ -1,8 +0,0 @@
<svg width="92" height="96" viewBox="0 0 92 96" xmlns="http://www.w3.org/2000/svg">
<g fill="#24292F">
<path fill-rule="evenodd" d="M65.45 16.8c10.89 0 19.71 8.86 19.71 19.8v6.6l5.74 11.46a4 4 0 0 1-.01 3.6l-5.73 11.34v6.6c0 10.94-8.82 19.8-19.71 19.8H26.02C15.13 96 6.31 87.14 6.31 76.2v-6.6L.45 58.3a4 4 0 0 1-.01-3.67l5.87-11.43v-6.6c0-10.94 8.82-19.8 19.71-19.8h39.43Zm-2.52 5.7H29.19c-9.32 0-16.87 7.56-16.87 16.88V45L7.44 54.46a4 4 0 0 0 .01 3.68L12.32 67.5v5.63c0 9.32 7.55 16.87 16.87 16.87h33.74c9.32 0 16.87-7.55 16.87-16.87V67.5l4.77-9.39a4 4 0 0 0 .01-3.61L79.8 45v-5.62c0-9.32-7.55-16.88-16.87-16.88Z"/>
<circle cx="45.73" cy="11.5" r="11"/>
<rect x="27" y="41" width="13" height="30" rx="6.5"/>
<rect x="51" y="41" width="13" height="30" rx="6.5"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 795 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

@@ -1 +0,0 @@
<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2"><g transform="scale(32)"><clipPath id="codex-color-clip"><path d="M0 0h16v16H0z"/></clipPath><g clip-path="url(#codex-color-clip)"><path d="M13.003 0H2.997A3.012 3.012 0 000 2.997v10.006A3.012 3.012 0 002.997 16h10.006A3.012 3.012 0 0016 13.003V2.997A3.012 3.012 0 0013.003 0z" fill="#fff" fill-rule="nonzero"/><path d="M9.064 3.344a4.578 4.578 0 012.285-.312c1 .115 1.891.54 2.673 1.275.01.01.024.017.037.021a.104.104 0 00.043 0 4.556 4.556 0 013.046.275l.047.022.116.057a4.585 4.585 0 012.188 2.399c.209.51.313 1.041.315 1.595.015.412-.03.824-.134 1.223a.124.124 0 00.03.115c.594.607.988 1.33 1.183 2.17.289 1.425-.007 2.71-.887 3.854l-.136.166a4.548 4.548 0 01-2.201 1.388.12.12 0 00-.081.076c-.191.551-.383 1.023-.74 1.494-.9 1.187-2.222 1.846-3.711 1.838-1.187-.006-2.239-.44-3.157-1.302a.109.109 0 00-.105-.024c-.388.125-.78.143-1.204.138a4.438 4.438 0 01-1.945-.466 4.553 4.553 0 01-1.61-1.335c-.152-.202-.303-.392-.414-.617a5.797 5.797 0 01-.37-.961 4.575 4.575 0 01-.014-2.298.133.133 0 00.006-.056.083.083 0 00-.027-.048 4.467 4.467 0 01-1.034-1.651 3.898 3.898 0 01-.251-1.192 5.193 5.193 0 01.141-1.6c.337-1.112.982-1.985 1.933-2.618.212-.141.413-.251.601-.33a6.29 6.29 0 01.646-.227.1.1 0 00.065-.066 4.512 4.512 0 01.829-1.615 4.54 4.54 0 011.837-1.388zm3.482 10.565a.64.64 0 00-.601.636.64.64 0 00.601.636h3.636l.036.001a.64.64 0 00.637-.637.64.64 0 00-.637-.637l-.036.001h-3.636zM8.462 9.23a.64.64 0 00-.543-.304.64.64 0 00-.563.935l1.272 2.224-1.266 2.136a.638.638 0 001.095.649l1.454-2.455a.637.637 0 00.005-.64L8.462 9.23z" fill="url(#codex-color-gradient)" fill-rule="nonzero" transform="scale(.66667)"/></g></g><defs><linearGradient id="codex-color-gradient" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(0 18 -18 0 12 3)"><stop offset="0" stop-color="#b1a7ff"/><stop offset=".5" stop-color="#7a9dff"/><stop offset="1" stop-color="#3941ff"/></linearGradient></defs></svg>

Before

Width:  |  Height:  |  Size: 2.0 KiB

@@ -1,3 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50" fill="none">
<path d="M48.8354 10.0479C48.3232 9.79199 48.1025 10.2798 47.8032 10.5278C47.7007 10.6079 47.6143 10.7119 47.5273 10.8076C46.7793 11.624 45.9048 12.1597 44.7622 12.0957C43.0923 12 41.666 12.5356 40.4058 13.8398C40.1377 12.2319 39.2476 11.272 37.8926 10.6558C37.1836 10.3359 36.4668 10.0156 35.9702 9.31982C35.6235 8.82373 35.5293 8.27197 35.356 7.72754C35.2456 7.3999 35.1353 7.06396 34.7651 7.00781C34.3633 6.94385 34.2056 7.2876 34.0479 7.57568C33.418 8.75195 33.1733 10.0479 33.1973 11.3599C33.2524 14.312 34.4736 16.6641 36.8999 18.3359C37.1758 18.5278 37.2466 18.7197 37.1597 19C36.9946 19.5757 36.7974 20.1357 36.624 20.7119C36.5137 21.0801 36.3486 21.1597 35.9624 21C34.6309 20.4321 33.481 19.5918 32.4644 18.5757C30.7393 16.8721 29.1792 14.9917 27.2334 13.52C26.7764 13.1758 26.3193 12.856 25.8467 12.5518C23.8618 10.584 26.1069 8.96777 26.627 8.77588C27.1704 8.57568 26.8159 7.8877 25.0591 7.896C23.3022 7.90381 21.6953 8.50391 19.647 9.30371C19.3477 9.42383 19.0322 9.51172 18.7095 9.58398C16.8501 9.22363 14.9199 9.14355 12.9033 9.37598C9.10596 9.80762 6.07275 11.6396 3.84326 14.7681C1.16455 18.5278 0.53418 22.7998 1.30664 27.2559C2.11768 31.9521 4.46582 35.8398 8.07373 38.8799C11.8159 42.0322 16.1255 43.5762 21.041 43.2803C24.0269 43.104 27.3516 42.6963 31.1016 39.4561C32.0469 39.936 33.0396 40.1279 34.686 40.272C35.9546 40.3921 37.1758 40.208 38.1211 40.0078C39.6021 39.688 39.4995 38.2881 38.9639 38.0322C34.623 35.9678 35.5762 36.8081 34.71 36.1279C36.9155 33.4639 40.2402 30.6958 41.54 21.728C41.6426 21.0161 41.5557 20.5679 41.54 19.9917C41.5322 19.6396 41.6108 19.5039 42.0049 19.4639C43.0923 19.3359 44.1479 19.0317 45.1167 18.4878C47.9292 16.9199 49.064 14.3438 49.3315 11.2559C49.3711 10.7837 49.3237 10.2959 48.8354 10.0479ZM24.3262 37.8398C20.1196 34.4639 18.0791 33.3521 17.2358 33.3999C16.4482 33.4482 16.5898 34.3682 16.7632 34.9678C16.9443 35.5601 17.1812 35.9683 17.5117 36.4878C17.7402 36.832 17.8979 37.3442 17.2832 37.728C15.9282 38.584 13.5728 37.4399 13.4624 37.3838C10.7207 35.7358 8.42822 33.5601 6.81348 30.584C5.25342 27.7197 4.34766 24.6479 4.19775 21.3677C4.1582 20.5757 4.38672 20.2959 5.15869 20.1519C6.17529 19.96 7.22314 19.9199 8.23926 20.0718C12.5327 20.7119 16.1885 22.6719 19.2529 25.7759C21.002 27.5439 22.3252 29.6558 23.6885 31.7202C25.1377 33.9121 26.6978 36 28.6831 37.7119C29.3843 38.312 29.9434 38.7681 30.479 39.104C28.8643 39.2881 26.1699 39.3281 24.3262 37.8398ZM26.3433 24.6001C26.3433 24.248 26.6191 23.9678 26.9658 23.9678C27.0444 23.9678 27.1152 23.9839 27.1782 24.0078C27.2651 24.04 27.3438 24.0879 27.4067 24.1602C27.5171 24.272 27.5801 24.4321 27.5801 24.6001C27.5801 24.9521 27.3042 25.2319 26.9575 25.2319C26.6108 25.2319 26.3433 24.9521 26.3433 24.6001ZM32.6064 27.8799C32.2046 28.0479 31.8027 28.1919 31.4165 28.208C30.8179 28.2397 30.1641 27.9922 29.8096 27.688C29.2583 27.2158 28.8643 26.9521 28.6987 26.1279C28.6279 25.7759 28.6675 25.2319 28.7305 24.9199C28.8721 24.248 28.7144 23.8159 28.2495 23.4238C27.8716 23.104 27.3911 23.0161 26.8633 23.0161C26.666 23.0161 26.4849 22.9277 26.3511 22.856C26.1304 22.7441 25.9492 22.4639 26.1226 22.1201C26.1777 22.0078 26.4458 21.7358 26.5088 21.688C27.2256 21.272 28.0527 21.4077 28.8169 21.7197C29.5259 22.0161 30.0615 22.5601 30.834 23.3281C31.6216 24.2559 31.7632 24.5117 32.2124 25.208C32.5669 25.752 32.8901 26.312 33.1104 26.9521C33.2446 27.3521 33.0713 27.6802 32.6064 27.8799Z" fill="#4D6BFE" fill-rule="nonzero"/>
</svg>

Before

Width:  |  Height:  |  Size: 3.5 KiB

@@ -1,11 +0,0 @@
<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="omp-gradient" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#ed4abf"/>
<stop offset=".5" stop-color="#9b4dff"/>
<stop offset="1" stop-color="#5ad8e6"/>
</linearGradient>
</defs>
<rect width="64" height="64" rx="12" fill="#0f0a14"/>
<path fill="url(#omp-gradient)" d="M14 16h36v8H40v32h-8V24h-6v22h-8V24h-4z"/>
</svg>

Before

Width:  |  Height:  |  Size: 451 B

@@ -1,11 +0,0 @@
<svg viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="poolside-gradient" x1="8" y1="5" x2="55" y2="59" gradientUnits="userSpaceOnUse">
<stop stop-color="#6c5cff"/>
<stop offset="1" stop-color="#3c2cff"/>
</linearGradient>
</defs>
<rect width="64" height="64" rx="13" fill="url(#poolside-gradient)"/>
<path d="M13 32c0-10.5 8.5-19 19-19 10.49 0 19 8.5 19 19s-8.51 19-19 19c-10.5 0-19-8.5-19-19Z" fill="none" stroke="#fff" stroke-width="4"/>
<path d="M16 24c8-4.1 17.1-.9 22.6 7.1 4.3-1.2 8.6.5 11 4.1M23.5 47.5 38 17.5" fill="none" stroke="#fff" stroke-linecap="round" stroke-linejoin="round" stroke-width="4"/>
</svg>

Before

Width:  |  Height:  |  Size: 682 B

@@ -1,3 +0,0 @@
<svg viewBox="0 0 141.38 140" xmlns="http://www.w3.org/2000/svg">
<path fill="#6D44E8" d="m140.93 85-16.35-28.33-1.93-3.34 8.66-15a3.32 3.32 0 0 0 0-3.34l-9.62-16.67a3.34 3.34 0 0 0-2.89-1.67H82.23l-8.66-15A3.33 3.33 0 0 0 70.68-.02H51.43a3.33 3.33 0 0 0-2.88 1.67L32.19 29.98l-1.92 3.33H12.96a3.34 3.34 0 0 0-2.88 1.67L.45 51.66a3.32 3.32 0 0 0 0 3.34l18.28 31.67-8.66 15a3.32 3.32 0 0 0 0 3.34l9.62 16.67a3.34 3.34 0 0 0 2.89 1.67h36.56l8.66 15a3.35 3.35 0 0 0 2.89 1.67h19.25a3.34 3.34 0 0 0 2.89-1.67l18.28-31.67h17.32a3.34 3.34 0 0 0 2.89-1.67l9.62-16.67a3.32 3.32 0 0 0-.01-3.34ZM51.44 3.33 61.07 20l-9.63 16.66h76.98l-9.62 16.66H45.67l-11.54-20zM57.21 120H22.58l9.63-16.67h19.25l-38.5-66.67h19.25l9.62 16.67L68.78 100l-11.55 20Zm61.59-33.34-9.62-16.67-38.49 66.67-9.63-16.67 9.63-16.66 26.94-46.67h23.1l17.32 30z"/>
</svg>

Before

Width:  |  Height:  |  Size: 832 B

-168
View File
@@ -1,168 +0,0 @@
import { afterEach, describe, expect, it, vi } from "vitest";
const { listModels } = vi.hoisted(() => ({ listModels: vi.fn() }));
vi.mock("./lib/ollama-client", () => ({
ollamaClient: { list: listModels },
}));
import {
fetchConnectUrl,
getClaudeDesktopAvailableModels,
getIntegrationStatuses,
} from "./api";
describe("fetchConnectUrl", () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it("requests a desktop handoff after account creation", async () => {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
signin_url:
"https://ollama.com/connect?name=MacBook&key=public-key",
}),
{ status: 401 },
),
),
);
await expect(fetchConnectUrl()).resolves.toBe(
"https://ollama.com/connect?name=MacBook&key=public-key&launch=true",
);
});
});
describe("getIntegrationStatuses", () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it("returns desktop and launcher integration metadata", async () => {
const fetch = vi.fn().mockResolvedValue(
new Response(
JSON.stringify([
{
id: "claude-desktop",
name: "Claude",
description: "Use Ollama models in Claude Desktop",
installed: true,
},
{
id: "opencode",
name: "OpenCode",
description: "Open-source coding agent",
command: "ollama launch opencode",
},
]),
{ status: 200 },
),
);
vi.stubGlobal("fetch", fetch);
await expect(getIntegrationStatuses()).resolves.toEqual([
{
id: "claude-desktop",
name: "Claude",
description: "Use Ollama models in Claude Desktop",
installed: true,
},
{
id: "opencode",
name: "OpenCode",
description: "Open-source coding agent",
command: "ollama launch opencode",
},
]);
expect(fetch).toHaveBeenCalledWith(
"http://127.0.0.1:3001/api/v1/integrations",
);
});
});
describe("getClaudeDesktopAvailableModels", () => {
afterEach(() => {
listModels.mockReset();
vi.unstubAllGlobals();
vi.restoreAllMocks();
});
it("returns installed local models while pruning remote entries", async () => {
listModels.mockResolvedValue({
models: [
{ name: "llama3.2:latest", digest: "local" },
{
name: "remote-placeholder",
digest: "remote",
remote_host: "https://ollama.com",
},
],
});
const fetch = vi.fn();
vi.stubGlobal("fetch", fetch);
const models = await getClaudeDesktopAvailableModels();
expect(models.map((model) => model.model)).toEqual(["llama3.2"]);
expect(fetch).not.toHaveBeenCalled();
});
it("does not request cloud models when they are unavailable to the user", async () => {
listModels.mockResolvedValue({
models: [
{ name: "qwen3:8b", digest: "local" },
{ name: "deepseek-v4-flash:cloud", digest: "cached-cloud" },
{ name: "gemma4:31b-cloud", digest: "legacy-cached-cloud" },
],
});
const fetch = vi.fn();
vi.stubGlobal("fetch", fetch);
const models = await getClaudeDesktopAvailableModels();
expect(models.map((model) => model.model)).toEqual(["qwen3:8b"]);
expect(fetch).not.toHaveBeenCalled();
});
it("loads the account cloud list in parallel when Cloud is available", async () => {
listModels.mockResolvedValue({
models: [{ name: "qwen3:8b", digest: "local" }],
});
const fetch = vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
models: [
{ name: "glm-5.2", digest: "cloud" },
{ name: "gemma4:31b-cloud", digest: "legacy-cloud" },
{ name: "qwen3:8b", digest: "cloud-duplicate" },
],
}),
),
);
vi.stubGlobal("fetch", fetch);
const models = await getClaudeDesktopAvailableModels(true);
expect(models.map((model) => model.model)).toEqual([
"qwen3:8b",
"glm-5.2:cloud",
"gemma4:31b-cloud",
]);
expect(fetch).toHaveBeenCalledWith(
"http://127.0.0.1:3001/api/v1/models/cloud",
);
});
it("keeps local models when the account cloud list fails", async () => {
listModels.mockResolvedValue({
models: [{ name: "qwen3:8b", digest: "local" }],
});
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new Error("offline")));
const models = await getClaudeDesktopAvailableModels(true);
expect(models.map((model) => model.model)).toEqual(["qwen3:8b"]);
});
});
+2 -102
View File
@@ -32,24 +32,6 @@ export interface CloudStatusResponse {
disabled: boolean;
source: CloudStatusSource;
}
export interface IntegrationStatus {
id: string;
name: string;
description: string;
installed?: boolean;
command?: string;
}
export type IntegrationStatuses = IntegrationStatus[];
export async function getIntegrationStatuses(): Promise<IntegrationStatuses> {
const response = await fetch(`${API_BASE}/api/v1/integrations`);
if (!response.ok) {
throw new Error(`Failed to fetch integration statuses: ${response.status}`);
}
return response.json();
}
// Helper function to convert Uint8Array to base64
function uint8ArrayToBase64(uint8Array: Uint8Array): string {
const chunkSize = 0x8000; // 32KB chunks to avoid stack overflow
@@ -99,9 +81,7 @@ export async function fetchConnectUrl(): Promise<string> {
if (response.status === 401) {
const data = await response.json();
if (data.signin_url) {
const connectUrl = new URL(data.signin_url);
connectUrl.searchParams.set("launch", "true");
return connectUrl.toString();
return data.signin_url;
}
}
@@ -196,84 +176,6 @@ export async function getModels(query?: string): Promise<Model[]> {
}
}
export async function getClaudeDesktopAvailableModels(
includeCloudModels = false,
): Promise<Model[]> {
try {
const [localResult, cloudResult] = await Promise.all([
ollama.list(),
includeCloudModels
? fetch(`${API_BASE}/api/v1/models/cloud`)
.then(async (response) => {
if (!response.ok) {
throw new Error(`cloud model list returned ${response.status}`);
}
return (await response.json()) as { models?: ModelResponse[] };
})
.catch((error) => {
console.warn("Failed to fetch cloud models:", error);
return { models: [] };
})
: Promise.resolve({ models: [] as ModelResponse[] }),
]);
const localModels = localResult.models.filter((model: ModelResponse) => {
const response = model as ModelResponse & {
remote_model?: string;
remote_host?: string;
};
const name = model.name.replace(/:latest$/, "");
return (
!response.remote_model &&
!response.remote_host &&
!name.endsWith("cloud")
);
});
const cloudModels = (cloudResult.models ?? []).map((model) => {
const name = model.name.replace(/:latest$/, "");
const tag = name.slice(name.lastIndexOf(":") + 1).toLowerCase();
const explicitCloud =
name.endsWith(":cloud") ||
(name.includes(":") && tag.endsWith("-cloud"));
return {
...model,
name: explicitCloud ? name : `${name}:cloud`,
};
});
const seen = new Set<string>();
return [...localModels, ...cloudModels]
.filter((model: ModelResponse) => {
const base = model.name
.replace(/:latest$/, "")
.replace(/:cloud$/, "");
if (!base || seen.has(base)) return false;
const families = model.details?.families;
const supported =
!families ||
families.length === 0 ||
!families.every((family: string) =>
family.toLowerCase().includes("bert"),
);
if (supported) seen.add(base);
return supported;
})
.map(
(model: ModelResponse) =>
new Model({
model: model.name.replace(/:latest$/, ""),
digest: model.digest,
modified_at: model.modified_at
? new Date(model.modified_at)
: undefined,
}),
);
} catch (err) {
throw new Error(`Failed to fetch Ollama models: ${err}`);
}
}
export async function getModelCapabilities(
modelName: string,
): Promise<ModelCapabilitiesResponse> {
@@ -516,9 +418,7 @@ export interface ModelRecommendationsResponse {
recommendations: ModelRecommendation[];
}
export async function getModelRecommendations(): Promise<
ModelRecommendation[]
> {
export async function getModelRecommendations(): Promise<ModelRecommendation[]> {
const response = await fetch(
`${API_BASE}/api/experimental/model-recommendations`,
);
-43
View File
@@ -1,43 +0,0 @@
import { Link } from "@/components/ui/link";
import { ChatIcon } from "@/components/ChatIcon";
import { Cog6ToothIcon, RectangleGroupIcon } from "@heroicons/react/24/outline";
type AppSection = "apps" | "chat" | "settings";
export function AppNavigation({ current }: { current: AppSection }) {
const itemClass = (section: AppSection) =>
`flex w-full items-center gap-3 rounded-lg px-2 py-2 text-left text-sm text-neutral-700 hover:bg-neutral-100 dark:text-neutral-100 dark:hover:bg-neutral-800 ${
current === section ? "bg-neutral-100 dark:bg-neutral-800" : ""
}`;
return (
<div className="flex flex-col gap-0.5">
<Link to="/connect" className={itemClass("apps")} draggable={false}>
<RectangleGroupIcon className="h-5 w-5 stroke-current" />
<span className="truncate">Apps</span>
</Link>
<Link
to="/c/$chatId"
params={{ chatId: "new" }}
mask={{ to: "/" }}
className={itemClass("chat")}
draggable={false}
>
<ChatIcon />
<span className="truncate">Chat</span>
</Link>
<Link to="/settings" className={itemClass("settings")} draggable={false}>
<Cog6ToothIcon className="h-5 w-5 stroke-current" />
<span className="truncate">Settings</span>
</Link>
</div>
);
}
export function AppSidebar({ current }: { current: AppSection }) {
return (
<nav className="flex flex-1 flex-col px-4 pb-4 select-none">
<AppNavigation current={current} />
</nav>
);
}
-14
View File
@@ -1,14 +0,0 @@
export function ChatIcon({ className = "h-5 w-5" }: { className?: string }) {
return (
<svg
aria-hidden="true"
className={`${className} fill-current`}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M17.0859 3.39949L15.2135 5.27196H7.27028C5.78649 5.27196 4.94684 6.11336 4.94684 7.59716V16.664C4.94684 18.1558 5.78649 18.9892 7.27028 18.9892H16.3406C17.8324 18.9892 18.6623 18.1558 18.6623 16.664V8.79514L20.5428 6.9115C20.567 7.11532 20.5773 7.33066 20.5773 7.55419V16.7149C20.5773 19.4069 19.0818 20.9024 16.3898 20.9024H7.22107C4.53708 20.9024 3.03357 19.4069 3.03357 16.7149V7.55419C3.03357 4.8622 4.53708 3.35869 7.22107 3.35869H16.3898C16.6329 3.35869 16.8662 3.37094 17.0859 3.39949Z" />
<path d="M9.92714 14.381L11.914 13.5403L20.8312 4.63114L19.3404 3.1581L10.433 12.0655L9.55234 13.9964C9.45664 14.2169 9.70293 14.4714 9.92714 14.381ZM21.5767 3.89364L22.2588 3.19384C22.6347 2.80184 22.6435 2.2663 22.2711 1.90536L22.0148 1.64287C21.6822 1.31377 21.1334 1.36513 20.7689 1.72158L20.0859 2.39833L21.5767 3.89364Z" />
</svg>
);
}
+146 -88
View File
@@ -6,12 +6,14 @@ import { getChat } from "@/api";
import { Link } from "@/components/ui/link";
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
import { ChatsResponse } from "@/gotypes";
import { AppNavigation } from "@/components/AppSidebar";
import { CogIcon, RocketLaunchIcon } from "@heroicons/react/24/outline";
// there's a hidden debug feature to copy a chat's data to the clipboard by
// holding shift and clicking this many times within this many seconds
const DEBUG_SHIFT_CLICKS_REQUIRED = 5;
const DEBUG_SHIFT_CLICK_WINDOW_MS = 7000; // 7 seconds
const launchSidebarRequestedKey = "ollama.launchSidebarRequested";
interface ChatSidebarProps {
currentChatId?: string;
}
@@ -238,100 +240,156 @@ export function ChatSidebar({ currentChatId }: ChatSidebarProps) {
[startEditing, handleDeleteChat],
);
if (isLoading) {
return (
<nav className="flex min-h-0 flex-col">
<div className="flex flex-1 flex-col p-4">
<div className="p-4">Loading...</div>
</div>
</nav>
);
}
if (error) {
return (
<nav className="flex min-h-0 flex-col">
<div className="flex flex-1 flex-col p-4">
<div className="p-4 text-red-500">Error loading chats</div>
</div>
</nav>
);
}
const isWindows = navigator.platform.toLowerCase().includes("win");
return (
<nav
aria-busy={isLoading || undefined}
className="flex flex-1 flex-col min-h-0 select-none"
>
<nav className="flex flex-1 flex-col min-h-0 select-none">
<header className="flex flex-col gap-0.5 px-4 pb-2">
<AppNavigation current="chat" />
<Link
href="/c/new"
mask={{ to: "/" }}
className={`flex w-full items-center gap-3 rounded-lg px-2 py-2 text-left text-sm text-neutral-700 hover:bg-neutral-100 dark:hover:bg-neutral-800 dark:text-neutral-100 ${currentChatId === "new" ? "bg-neutral-100 dark:bg-neutral-800" : ""
}`}
draggable={false}
>
<svg
className="h-5 w-5 fill-current"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M17.0859 3.39949L15.2135 5.27196H7.27028C5.78649 5.27196 4.94684 6.11336 4.94684 7.59716V16.664C4.94684 18.1558 5.78649 18.9892 7.27028 18.9892H16.3406C17.8324 18.9892 18.6623 18.1558 18.6623 16.664V8.79514L20.5428 6.9115C20.567 7.11532 20.5773 7.33066 20.5773 7.55419V16.7149C20.5773 19.4069 19.0818 20.9024 16.3898 20.9024H7.22107C4.53708 20.9024 3.03357 19.4069 3.03357 16.7149V7.55419C3.03357 4.8622 4.53708 3.35869 7.22107 3.35869H16.3898C16.6329 3.35869 16.8662 3.37094 17.0859 3.39949Z" />
<path d="M9.92714 14.381L11.914 13.5403L20.8312 4.63114L19.3404 3.1581L10.433 12.0655L9.55234 13.9964C9.45664 14.2169 9.70293 14.4714 9.92714 14.381ZM21.5767 3.89364L22.2588 3.19384C22.6347 2.80184 22.6435 2.2663 22.2711 1.90536L22.0148 1.64287C21.6822 1.31377 21.1334 1.36513 20.7689 1.72158L20.0859 2.39833L21.5767 3.89364Z" />
</svg>
<span className="truncate">New Chat</span>
</Link>
<Link
to="/c/$chatId"
params={{ chatId: "launch" }}
onClick={() => {
if (currentChatId !== "launch") {
sessionStorage.setItem(launchSidebarRequestedKey, "1");
}
}}
className={`flex w-full items-center gap-3 rounded-lg px-2 py-2 text-left text-sm text-neutral-700 hover:bg-neutral-100 dark:hover:bg-neutral-800 dark:text-neutral-100 cursor-pointer ${currentChatId === "launch"
? "bg-neutral-100 dark:bg-neutral-800"
: ""
}`}
draggable={false}
>
<RocketLaunchIcon className="h-5 w-5 stroke-current" />
<span className="truncate">Launch</span>
</Link>
{isWindows && (
<Link
href="/settings"
className={`flex w-full items-center gap-3 rounded-lg px-2 py-2 text-left text-sm text-neutral-700 hover:bg-neutral-100 dark:hover:bg-neutral-800 dark:text-neutral-300`}
draggable={false}
>
<CogIcon className="h-5 w-5 stroke-current" />
<span className="truncate">Settings</span>
</Link>
)}
</header>
<div className="flex flex-1 flex-col px-4 py-1 overflow-y-auto overscroll-auto scrollbar-gutter">
{error ? (
<div className="px-2 pt-4 text-sm text-red-500">
Error loading chats
</div>
) : (
<div className="flex flex-col gap-3 pt-4">
{chatGroups.map((group) => (
<div key={group.name} className="flex flex-col gap-0.5">
<h3 className="text-xs font-medium text-neutral-400 dark:text-neutral-500 px-2 py-1 select-none">
{group.name}
</h3>
{group.chats.map((chat) => (
<div
key={chat.id}
className={`allow-context-menu flex items-center relative text-sm text-neutral-800 dark:text-neutral-400 rounded-lg hover:bg-neutral-100 dark:hover:bg-neutral-800 ${
chat.id === currentChatId
? "bg-neutral-100 text-black dark:bg-neutral-800"
: ""
<div className="flex flex-col gap-3 pt-4">
{chatGroups.map((group) => (
<div key={group.name} className="flex flex-col gap-0.5">
<h3 className="text-xs font-medium text-neutral-400 dark:text-neutral-500 px-2 py-1 select-none">
{group.name}
</h3>
{group.chats.map((chat) => (
<div
key={chat.id}
className={`allow-context-menu flex items-center relative text-sm text-neutral-800 dark:text-neutral-400 rounded-lg hover:bg-neutral-100 dark:hover:bg-neutral-800 ${chat.id === currentChatId
? "bg-neutral-100 text-black dark:bg-neutral-800"
: ""
}`}
onMouseEnter={() => handleMouseEnter(chat.id)}
onContextMenu={(e) =>
handleContextMenu(
e,
chat.id,
chat.title ||
onMouseEnter={() => handleMouseEnter(chat.id)}
onContextMenu={(e) =>
handleContextMenu(
e,
chat.id,
chat.title ||
chat.userExcerpt ||
chat.createdAt.toLocaleString(),
)
}
>
{editingChatId === chat.id ? (
<div className="flex-1 flex items-center min-w-0 px-2 py-2 bg-neutral-100 text-black dark:bg-neutral-800 rounded-lg">
<span className="truncate font-sans text-sm w-full">
<input
ref={inputRef}
type="text"
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
saveRename();
} else if (e.key === "Escape") {
setEditingChatId(null);
setEditValue("");
}
}}
className="bg-transparent border-0 focus:outline-none w-full dark:text-white"
style={{
font: "inherit",
lineHeight: "inherit",
padding: 0,
margin: 0,
}}
/>
</span>
</div>
) : (
<Link
to="/c/$chatId"
params={{ chatId: chat.id }}
className="flex-1 flex items-center min-w-0 px-2 py-2 select-none"
onClick={(e) => {
handleShiftClick(e, chat.id);
}}
draggable={false}
>
<span className="truncate font-sans text-sm">
{chat.title ||
chat.userExcerpt ||
chat.createdAt.toLocaleString(),
)
}
>
{editingChatId === chat.id ? (
<div className="flex-1 flex items-center min-w-0 px-2 py-2 bg-neutral-100 text-black dark:bg-neutral-800 rounded-lg">
<span className="truncate font-sans text-sm w-full">
<input
ref={inputRef}
type="text"
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
saveRename();
} else if (e.key === "Escape") {
setEditingChatId(null);
setEditValue("");
}
}}
className="bg-transparent border-0 focus:outline-none w-full dark:text-white"
style={{
font: "inherit",
lineHeight: "inherit",
padding: 0,
margin: 0,
}}
/>
chat.createdAt.toLocaleString()}
</span>
{copiedChatId === chat.id && (
<span className="ml-2 text-xs text-green-600 dark:text-green-400">
Copied!
</span>
</div>
) : (
<Link
to="/c/$chatId"
params={{ chatId: chat.id }}
className="flex-1 flex items-center min-w-0 px-2 py-2 select-none"
onClick={(e) => {
handleShiftClick(e, chat.id);
}}
draggable={false}
>
<span className="truncate font-sans text-sm">
{chat.title ||
chat.userExcerpt ||
chat.createdAt.toLocaleString()}
</span>
{copiedChatId === chat.id && (
<span className="ml-2 text-xs text-green-600 dark:text-green-400">
Copied!
</span>
)}
</Link>
)}
</div>
))}
</div>
))}
</div>
)}
)}
</Link>
)}
</div>
))}
</div>
))}
</div>
</div>
</nav>
);
Loaded 100 of 796 files, more files were not shown because too many files have changed in this diff. Show more