Skip to content

Commit 1dbea1c

Browse files
Kabuki94claude
andcommitted
feat(cli): mios-ollama wrapper + OLLAMA_HOST env -- Ptyxis-to-Ollama flow
Operator workflow this enables: Ptyxis flatpak -> default tab spawns into the host shell via flatpak-spawn --host -> ollama list / mios-ollama chat "..." / mios "..." land directly on the local Ollama Quadlet (:11434) and LocalAI Quadlet (:8080). etc/profile.d/mios-env.sh: export OLLAMA_HOST=http://localhost:11434 so the upstream `ollama` CLI auto-binds to the local Quadlet without having to remember the port. Architectural Law 5's canonical surface (MIOS_AI_ENDPOINT) still defaults to LocalAI on :8080 -- this var is for the upstream ollama CLI, which uses its own env-var convention. usr/bin/mios-ollama: thin wrapper that delegates to /usr/bin/ollama for `list / pull / run / rm / show / ps / cp` (upstream UX) but adds a `chat <prompt>` verb that re-runs /usr/bin/mios with MIOS_AI_ENDPOINT pinned at Ollama's OpenAI-compat endpoint (http://localhost:11434/v1). Falls back to raw curl streaming when the openai SDK isn't present (minimal images during early build), parsing SSE chunks via jq. Honors MIOS_AI_MODEL. 36-tools.sh: mios-ollama added to the TOOLS array so chmod is set during build. .gitignore: documented inline (mios-ollama matches the existing !/usr/bin/mios-* whitelist). Note: mios-ollama is for operators who want to talk to Ollama specifically. The default `mios <prompt>` still routes through MIOS_AI_ENDPOINT (LocalAI), per the canonical AI surface. Set MIOS_AI_ENDPOINT=http://localhost:11434/v1 in env to flip the default for the entire session. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 52c89ee commit 1dbea1c

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ usr/bin/*
173173
# iommu-groups doesn't carry the mios- prefix (it's the canonical Linux
174174
# verb name) but ships alongside mios-vfio-* as part of the MiOS CLI.
175175
!/usr/bin/iommu-groups
176+
# (mios-ollama is an OpenAI-compat wrapper around upstream `ollama`;
177+
# matches the !/usr/bin/mios-* pattern above but listed for clarity.)
176178

177179
# usr/share -- mios/ build files + doc/mios/ KB docs
178180
usr/share/*

automation/36-tools.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TOOLS=(
2222
mios-status
2323
mios-vfio-toggle
2424
mios-vfio-check
25+
mios-ollama
2526
iommu-groups
2627
)
2728
# Note: aichat / aichat-ng are installed by 37-aichat.sh (which fetches

etc/profile.d/mios-env.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ export MIOS_AI_MODEL="${MIOS_AI_MODEL:-qwen2.5-coder:7b}"
7373
export MIOS_AI_EMBED_MODEL="${MIOS_AI_EMBED_MODEL:-nomic-embed-text}"
7474
export MIOS_AI_KEY="${MIOS_AI_KEY:-}"
7575

76+
# Ollama-specific bind. /usr/bin/ollama (upstream CLI) reads OLLAMA_HOST
77+
# to find the API server. The MiOS Ollama Quadlet
78+
# (usr/share/containers/systemd/ollama.container) publishes 0.0.0.0:11434
79+
# on the host, so any host-side shell -- including Ptyxis flatpak's
80+
# default `flatpak-spawn --host bash` session -- talks to it via this
81+
# env. Set explicitly here so `ollama list` / `ollama run <model>` work
82+
# without arguments out of the box. Architectural Law 5 still has the
83+
# canonical OpenAI surface at MIOS_AI_ENDPOINT (LocalAI on :8080);
84+
# Ollama's OpenAI-compatible endpoint at localhost:11434/v1 is reached
85+
# via the mios-ollama wrapper or by overriding MIOS_AI_ENDPOINT.
86+
export OLLAMA_HOST="${OLLAMA_HOST:-http://localhost:11434}"
87+
7688
# Identity surface (consumed by 'mios' CLI, ai-bootstrap, postcheck).
7789
export MIOS_USER="${MIOS_USER:-${MIOS_DEFAULT_USER:-mios}}"
7890
export MIOS_HOSTNAME="${MIOS_HOSTNAME:-${MIOS_DEFAULT_HOST:-mios}}"

usr/bin/mios-ollama

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
# mios-ollama -- thin wrapper around the upstream `ollama` CLI that
3+
# pins OLLAMA_HOST to the locally-running Ollama Quadlet
4+
# (usr/share/containers/systemd/ollama.container, port 11434), so
5+
# `ollama list / pull / run / rm` Just Work from any host shell --
6+
# including Ptyxis flatpak's `flatpak-spawn --host bash` session,
7+
# the MiOS-DEV WSLg-routed terminal, or a plain TTY on a deployed
8+
# MiOS host.
9+
#
10+
# mios-ollama list installed models
11+
# mios-ollama run <model> interactive chat (upstream UX)
12+
# mios-ollama pull <model> download a model
13+
# mios-ollama rm <model> delete a model
14+
# mios-ollama serve (don't -- the Quadlet does this)
15+
# mios-ollama chat "<prompt>" one-shot prompt via OpenAI-compat
16+
# /v1/chat/completions; routes through
17+
# /usr/bin/mios with MIOS_AI_ENDPOINT
18+
# pointed at Ollama (:11434/v1) and
19+
# falls back to streaming raw curl
20+
# when the openai SDK is unavailable
21+
#
22+
# Env overrides:
23+
# OLLAMA_HOST default http://localhost:11434
24+
# OLLAMA_API_BASE default http://localhost:11434/v1 (OpenAI-compat)
25+
# MIOS_AI_MODEL default qwen2.5-coder:7b
26+
#
27+
# Note: per Architectural Law 5, the canonical agent surface is
28+
# MIOS_AI_ENDPOINT (LocalAI on :8080). This wrapper is for operators
29+
# who want to drive Ollama directly without changing global env.
30+
set -euo pipefail
31+
32+
OLLAMA_HOST="${OLLAMA_HOST:-http://localhost:11434}"
33+
OLLAMA_API_BASE="${OLLAMA_API_BASE:-${OLLAMA_HOST%/}/v1}"
34+
MIOS_AI_MODEL="${MIOS_AI_MODEL:-qwen2.5-coder:7b}"
35+
export OLLAMA_HOST
36+
37+
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
38+
sed -n '2,28p' "$0" | sed 's/^# \?//'
39+
exit 0
40+
fi
41+
42+
# `mios-ollama chat "<prompt>"` routes through the canonical mios CLI
43+
# but pins MIOS_AI_ENDPOINT/KEY at Ollama. Streams + tool-calls work
44+
# the same way; the local model just happens to be served by Ollama.
45+
if [[ "${1:-}" == "chat" ]]; then
46+
shift
47+
if [[ -x /usr/bin/mios ]]; then
48+
exec env \
49+
MIOS_AI_ENDPOINT="$OLLAMA_API_BASE" \
50+
MIOS_AI_MODEL="$MIOS_AI_MODEL" \
51+
MIOS_AI_KEY="${MIOS_AI_KEY:-sk-noop}" \
52+
/usr/bin/mios "$@"
53+
fi
54+
# Fallback: openai SDK / mios CLI not available -- raw curl streaming.
55+
# Useful in minimal images that haven't pulled python3-openai yet.
56+
if [[ $# -eq 0 ]]; then echo "mios-ollama chat <prompt>" >&2; exit 2; fi
57+
body=$(jq -n \
58+
--arg model "$MIOS_AI_MODEL" \
59+
--arg content "$*" \
60+
'{model:$model, stream:true, messages:[{role:"user",content:$content}]}')
61+
curl -sNH "Content-Type: application/json" -d "$body" \
62+
"$OLLAMA_API_BASE/chat/completions" \
63+
| sed -nE 's/^data: //p' \
64+
| jq -j 'select(. != "[DONE]") | .choices[0].delta.content // empty'
65+
echo
66+
exit 0
67+
fi
68+
69+
# Anything else: delegate to the upstream `ollama` CLI verbatim.
70+
if ! command -v ollama >/dev/null 2>&1; then
71+
echo "mios-ollama: /usr/bin/ollama not found -- check 37-ollama-prep.sh ran" >&2
72+
exit 127
73+
fi
74+
exec /usr/bin/ollama "$@"

0 commit comments

Comments
 (0)