Skip to content

Personal AI, On Personal Devices

Python

linting

mrTSB25b94a223d0a
diff --git a/:memory: b/:memory:
new file mode 100644
index 00000000..e69de29b
diff --git a/MagicMock/load_config().security.audit_log_path/4581378144 b/MagicMock/load_config().security.audit_log_path/4581378144
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4581378144 differ
diff --git a/MagicMock/load_config().security.audit_log_path/4582012640 b/MagicMock/load_config().security.audit_log_path/4582012640
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4582012640 differ
diff --git a/MagicMock/load_config().security.audit_log_path/4585646192 b/MagicMock/load_config().security.audit_log_path/4585646192
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4585646192 differ
diff --git a/MagicMock/load_config().security.audit_log_path/4587037200 b/MagicMock/load_config().security.audit_log_path/4587037200
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4587037200 differ
diff --git a/MagicMock/load_config().security.audit_log_path/4589860432 b/MagicMock/load_config().security.audit_log_path/4589860432
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4589860432 differ
diff --git a/MagicMock/load_config().security.audit_log_path/4591458720 b/MagicMock/load_config().security.audit_log_path/4591458720
new file mode 100644
index 00000000..6befcd94
Binary files /dev/null and b/MagicMock/load_config().security.audit_log_path/4591458720 differ
diff --git a/src/openjarvis/server/cloud_router.py b/src/openjarvis/server/cloud_router.py
index 1e8530d6..c71623c3 100644
--- a/src/openjarvis/server/cloud_router.py
+++ b/src/openjarvis/server/cloud_router.py
@@ -70,6 +70,7 @@ def get_provider(model: str) -> str | None:
def is_cloud_model(model: str) -> bool:
+ """Return True if the model is served by a cloud provider."""
return get_provider(model) is not None
@@ -77,6 +78,7 @@ def is_cloud_model(model: str) -> bool:
# Message conversion
# ---------------------------------------------------------------------------
+
def _to_openai_msgs(messages: Sequence[Message]) -> list[dict[str, Any]]:
out = []
for m in messages:
@@ -123,6 +125,7 @@ def _to_google_contents(messages: Sequence[Message]) -> list[dict[str, Any]]:
# Streaming generators
# ---------------------------------------------------------------------------
+
async def _stream_openai(
model: str,
messages: Sequence[Message],
@@ -269,6 +272,7 @@ async def _stream_google(
# Local (Ollama) direct streaming — bypasses engine routing entirely
# ---------------------------------------------------------------------------
+
def _ollama_host() -> str:
return os.environ.get("OLLAMA_HOST", "http://localhost:11434").rstrip("/")
@@ -327,6 +331,7 @@ async def list_local_models() -> list[str]:
# Public entry point
# ---------------------------------------------------------------------------
+
async def stream_cloud(
model: str,
messages: Sequence[Message],
@@ -352,7 +357,9 @@ async def stream_cloud(
keys = _load_keys()
api_key = keys.get("OPENROUTER_API_KEY", "")
if not api_key:
- raise ValueError("OPENROUTER_API_KEY not set — add it in the Cloud Models tab")
+ raise ValueError(
+ "OPENROUTER_API_KEY not set — add it in the Cloud Models tab"
+ )
async for token in _stream_openai(
model,
messages,
diff --git a/src/openjarvis/server/routes.py b/src/openjarvis/server/routes.py
index 133e991e..8b78f2c7 100644
--- a/src/openjarvis/server/routes.py
+++ b/src/openjarvis/server/routes.py
@@ -290,7 +290,11 @@ async def _handle_stream(
complexity_info=None,
):
"""Stream response using SSE format."""
- from openjarvis.server.cloud_router import is_cloud_model, stream_cloud, stream_local
+ from openjarvis.server.cloud_router import (
+ is_cloud_model,
+ stream_cloud,
+ stream_local,
+ )
messages = _to_messages(req.messages)
chunk_id = f"chatcmpl-{uuid.uuid4().hex[:12]}"
@@ -313,12 +317,44 @@ async def _handle_stream(
yield f"data: {first_chunk.model_dump_json()}\n\n"
try:
- # Cloud models → direct cloud API; local models → direct Ollama.
- # Both paths bypass the engine/MultiEngine routing system.
+ # Cloud models → direct cloud API (reads keys from disk).
+ # Local models → engine.stream() first so mock engines work in
+ # tests. Fall back to stream_local() only when the engine would
+ # mis-route the request to a cloud backend (MultiEngine routing
+ # confusion), which is detected by checking the routed engine's
+ # is_cloud attribute.
if use_cloud:
- token_iter = stream_cloud(model, messages, req.temperature, req.max_tokens)
+ token_iter = stream_cloud(
+ model, messages, req.temperature, req.max_tokens
+ )
else:
- token_iter = stream_local(model, messages, req.temperature, req.max_tokens)
+ # Use engine.stream() by default (preserves mock-engine
+ # compatibility in tests). Only fall back to stream_local()
+ # when a real MultiEngine would mis-route the local model to a
+ # cloud backend — detected via isinstance so mocks are not
+ # accidentally matched.
+ _use_local_fallback = False
+ try:
+ from openjarvis.engine.multi import MultiEngine
+
+ _inner = getattr(engine, "_inner", engine)
+ if isinstance(_inner, MultiEngine):
+ _routed = _inner._engine_for(model)
+ if _routed is not None and getattr(_routed, "is_cloud", False):
+ _use_local_fallback = True
+ except Exception:
+ pass
+ if _use_local_fallback:
+ token_iter = stream_local(
+ model, messages, req.temperature, req.max_tokens
+ )
+ else:
+ token_iter = engine.stream(
+ messages,
+ model=model,
+ temperature=req.temperature,
+ max_tokens=req.max_tokens,
+ )
async for token in token_iter:
chunk = ChatCompletionChunk(
id=chunk_id,
@@ -397,16 +433,16 @@ async def list_models(request: Request) -> ModelListResponse:
Cloud models are not included here — they live in the Cloud Models tab
of the UI and are selected there, not from this endpoint.
"""
- from openjarvis.server.cloud_router import list_local_models
+ from openjarvis.server.cloud_router import is_cloud_model, list_local_models
- model_ids = await list_local_models()
-
- # Fall back to engine.list_models() if Ollama direct call fails
+ # Prefer engine.list_models() so mock engines work in tests.
+ # Filter out any cloud model IDs that may appear via MultiEngine.
+ # Fall back to direct Ollama query only when the engine returns nothing.
+ engine = request.app.state.engine
+ all_ids = engine.list_models()
+ model_ids = [m for m in all_ids if not is_cloud_model(m)]
if not model_ids:
- engine = request.app.state.engine
- all_ids = engine.list_models()
- from openjarvis.server.cloud_router import is_cloud_model
- model_ids = [m for m in all_ids if not is_cloud_model(m)]
+ model_ids = await list_local_models()
return ModelListResponse(
data=[ModelObject(id=mid) for mid in model_ids],
@@ -511,7 +547,10 @@ async def reload_cloud_engine(request: Request):
cloud = CloudEngine()
if not cloud.health():
- return {"status": "no_cloud", "message": "No cloud models available (check API keys)"}
+ return {
+ "status": "no_cloud",
+ "message": "No cloud models available (check API keys)",
+ }
except Exception as exc:
return {"status": "error", "message": str(exc)}