This commit is contained in:
Timothy Jaeryang Baek
2026-06-12 18:59:28 +01:00
Unverified
parent e46dafc204
commit 493a3a1353
5 changed files with 102 additions and 61 deletions
@@ -189,6 +189,14 @@
<span class="text-gray-400 dark:text-gray-600">API Key:</span>
<span class="text-gray-700 dark:text-gray-300">sk-cptr-...</span>
</div>
<div>
<span class="text-gray-400 dark:text-gray-600">Header:</span>
<span class="text-gray-700 dark:text-gray-300">X-OpenWebUI-Chat-Id: {'{{CHAT_ID}}'}</span>
</div>
<div>
<span class="text-gray-400 dark:text-gray-600">Also accepts:</span>
<span class="text-gray-700 dark:text-gray-300">X-Chat-Id</span>
</div>
</div>
</div>
{/if}
+34 -10
View File
@@ -235,6 +235,10 @@
<polyline points="7 3 7 8 15 8" />
{:else if name === 'shield'}
<path d="M12 22C12 22 20 18 20 12V5L12 2L4 5V12C4 18 12 22 12 22Z" />
{:else if name === 'gateway'}
<path d="M3 19H12M21 19H12M12 19V13M12 13H18V5H6V13H12Z" />
<path d="M9 9.01L9.01 8.99889" />
<path d="M12 9.01L12.01 8.99889" />
{:else if name === 'quote'}
<path d="M10 12H5C5 7 7.5 4.5 10 3.5" />
<path d="M21 12H16C16 7 18.5 4.5 21 3.5" />
@@ -306,7 +310,9 @@
<path d="M10 6V2" />
<path d="M14 6V2" />
{:else if name === 'play'}
<path d="M6.90588 4.53682C6.50592 4.2998 6 4.58808 6 5.05299V18.947C6 19.4119 6.50592 19.7002 6.90588 19.4632L18.629 12.5162C19.0211 12.2838 19.0211 11.7162 18.629 11.4838L6.90588 4.53682Z" />
<path
d="M6.90588 4.53682C6.50592 4.2998 6 4.58808 6 5.05299V18.947C6 19.4119 6.50592 19.7002 6.90588 19.4632L18.629 12.5162C19.0211 12.2838 19.0211 11.7162 18.629 11.4838L6.90588 4.53682Z"
/>
{:else if name === 'clock'}
<circle cx="12" cy="12" r="10" />
<path d="M12 6V12H16.5" />
@@ -318,28 +324,46 @@
<path d="M21 5L2 12.5L9 13.5M21 5L18.5 20L9 13.5M21 5L9 13.5M9 13.5V19L12.2488 15.7229" />
{:else if name === 'discord'}
<path d="M5.5 16C10.5 18.5 13.5 18.5 18.5 16" />
<path d="M15.5 17.5L16.5 19.5C16.5 19.5 20.6713 18.1717 22 16C22 15 22.5301 7.85339 19 5.5C17.5 4.5 15 4 15 4L14 6H12" />
<path d="M8.52832 17.5L7.52832 19.5C7.52832 19.5 3.35699 18.1717 2.02832 16C2.02832 15 1.49823 7.85339 5.02832 5.5C6.52832 4.5 9.02832 4 9.02832 4L10.0283 6H12.0283" />
<path d="M8.5 14C7.67157 14 7 13.1046 7 12C7 10.8954 7.67157 10 8.5 10C9.32843 10 10 10.8954 10 12C10 13.1046 9.32843 14 8.5 14Z" />
<path d="M15.5 14C14.6716 14 14 13.1046 14 12C14 10.8954 14.6716 10 15.5 10C16.3284 10 17 10.8954 17 12C17 13.1046 16.3284 14 15.5 14Z" />
<path
d="M15.5 17.5L16.5 19.5C16.5 19.5 20.6713 18.1717 22 16C22 15 22.5301 7.85339 19 5.5C17.5 4.5 15 4 15 4L14 6H12"
/>
<path
d="M8.52832 17.5L7.52832 19.5C7.52832 19.5 3.35699 18.1717 2.02832 16C2.02832 15 1.49823 7.85339 5.02832 5.5C6.52832 4.5 9.02832 4 9.02832 4L10.0283 6H12.0283"
/>
<path
d="M8.5 14C7.67157 14 7 13.1046 7 12C7 10.8954 7.67157 10 8.5 10C9.32843 10 10 10.8954 10 12C10 13.1046 9.32843 14 8.5 14Z"
/>
<path
d="M15.5 14C14.6716 14 14 13.1046 14 12C14 10.8954 14.6716 10 15.5 10C16.3284 10 17 10.8954 17 12C17 13.1046 16.3284 14 15.5 14Z"
/>
{:else if name === 'slack'}
<path d="M10 3L6 21" />
<path d="M20.5 16H2.5" />
<path d="M22 7H4" />
<path d="M18 3L14 21" />
{:else if name === 'whatsapp'}
<path d="M22 12C22 17.5228 17.5228 22 12 22C10.1786 22 8.47087 21.513 7 20.6622L2 21.5L2.83209 16C2.29689 14.7751 2 13.4222 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12Z" />
<path d="M12.9604 13.8683L15.0399 13.4624L17 14.2149V16.0385C17 16.6449 16.4783 17.1073 15.8901 16.9783C14.3671 16.6444 11.5997 15.8043 9.67826 13.8683C7.84859 12.0248 7.22267 9.45734 7.01039 8.04128C6.92535 7.47406 7.3737 7 7.94306 7H9.83707L10.572 8.96888L10.1832 11.0701" />
<path
d="M22 12C22 17.5228 17.5228 22 12 22C10.1786 22 8.47087 21.513 7 20.6622L2 21.5L2.83209 16C2.29689 14.7751 2 13.4222 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12Z"
/>
<path
d="M12.9604 13.8683L15.0399 13.4624L17 14.2149V16.0385C17 16.6449 16.4783 17.1073 15.8901 16.9783C14.3671 16.6444 11.5997 15.8043 9.67826 13.8683C7.84859 12.0248 7.22267 9.45734 7.01039 8.04128C6.92535 7.47406 7.3737 7 7.94306 7H9.83707L10.572 8.96888L10.1832 11.0701"
/>
{:else if name === 'signal'}
<path d="M8.5 11.5L11.5 14.5L16.5 9.5" />
<path d="M5 18L3.13036 4.91253C3.05646 4.39524 3.39389 3.91247 3.90398 3.79912L11.5661 2.09641C11.8519 2.03291 12.1481 2.03291 12.4339 2.09641L20.096 3.79912C20.6061 3.91247 20.9435 4.39524 20.8696 4.91252L19 18C18.9293 18.495 18.5 21.5 12 21.5C5.5 21.5 5.07071 18.495 5 18Z" />
<path
d="M5 18L3.13036 4.91253C3.05646 4.39524 3.39389 3.91247 3.90398 3.79912L11.5661 2.09641C11.8519 2.03291 12.1481 2.03291 12.4339 2.09641L20.096 3.79912C20.6061 3.91247 20.9435 4.39524 20.8696 4.91252L19 18C18.9293 18.495 18.5 21.5 12 21.5C5.5 21.5 5.07071 18.495 5 18Z"
/>
{:else if name === 'browser'}
<path d="M22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12Z" />
<path
d="M22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12Z"
/>
<path d="M13 2.04932C13 2.04932 16 6 16 12C16 18 13 21.9507 13 21.9507" />
<path d="M11 21.9507C11 21.9507 8 18 8 12C8 6 11 2.04932 11 2.04932" />
<path d="M2 12H22" />
{:else if name === 'microphone'}
<path d="M12 1C10.3431 1 9 2.34315 9 4V12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12V4C15 2.34315 13.6569 1 12 1Z" />
<path
d="M12 1C10.3431 1 9 2.34315 9 4V12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12V4C15 2.34315 13.6569 1 12 1Z"
/>
<path d="M5 10V12C5 15.866 8.13401 19 12 19C15.866 19 19 15.866 19 12V10" />
<line x1="12" y1="19" x2="12" y2="23" />
<line x1="8" y1="23" x2="16" y2="23" />
@@ -53,7 +53,7 @@
{ id: 'connections', label: $t('admin.connections'), icon: 'plug' },
{ id: 'models', label: $t('admin.models'), icon: 'cube' },
{ id: 'messaging', label: $t('admin.messaging'), icon: 'chat-bubble' },
{ id: 'gateway', label: $t('admin.gateway.tab'), icon: 'shield' },
{ id: 'gateway', label: $t('admin.gateway.tab'), icon: 'gateway' },
{ id: 'audio', label: 'Audio', icon: 'microphone' },
{ id: 'browser', label: 'Browser', icon: 'browser' },
{ id: 'admin_settings', label: $t('settings.configuration'), icon: 'shield' }
@@ -92,7 +92,9 @@
<!-- Admin section -->
{#if isAdmin}
<span class="hidden md:block text-[10px] text-gray-400 dark:text-gray-600 px-2 mt-2 mb-0.5">{$t('sidebar.admin')}</span>
<span class="hidden md:block text-[10px] text-gray-400 dark:text-gray-600 px-2 mt-2 mb-0.5"
>{$t('sidebar.admin')}</span
>
{#each adminTabs as tab}
<button
+2 -2
View File
@@ -412,7 +412,7 @@
"admin.gateway.tab": "Gateway",
"admin.gateway.title": "API Gateway",
"admin.gateway.description": "Generate API keys to connect Open-WebUI or any OpenAI-compatible client to your workspaces.",
"admin.gateway.description": "Generate API keys to connect Open WebUI or any OpenAI-compatible client to your workspaces.",
"admin.gateway.loadError": "Failed to load API keys",
"admin.gateway.createKey": "Create key",
"admin.gateway.keyNamePlaceholder": "Key name (e.g. open-webui)",
@@ -424,7 +424,7 @@
"admin.gateway.noKeys": "No API keys yet",
"admin.gateway.newKey": "New API key created. Copy it now",
"admin.gateway.keyWarning": "This key will only be shown once. Store it securely.",
"admin.gateway.howToConnect": "Connect from Open-WebUI",
"admin.gateway.howToConnect": "Connect from Open WebUI",
"admin.messaging": "Messaging",
+54 -47
View File
@@ -1,15 +1,15 @@
"""Gateway: expose cptr workspaces as OpenAI-compatible models.
GET /v1/models — list workspaces in OpenAI model-list format
POST /v1/chat/completions — run the agentic loop on a workspace, stream SSE
GET /v1/models - list workspaces in OpenAI model-list format
POST /v1/chat/completions - run the agentic loop on a workspace, stream SSE
Any OpenAI-compatible client (Open-WebUI, curl, Python SDK) can connect
Any OpenAI-compatible client (Open WebUI, curl, Python SDK) can connect
to cptr and use each workspace as a "model" that can read files, edit code,
run commands, and use skills.
Session mapping: if the caller sends X-OpenWebUI-Chat-Id (OWUI does this
when ENABLE_FORWARD_USER_INFO_HEADERS=true), the same cptr chat is reused
across turns. Otherwise each request creates a fresh chat.
Session mapping: if the caller sends X-Chat-Id or X-OpenWebUI-Chat-Id,
the same cptr chat is reused across turns. Otherwise each request creates
a fresh chat.
Auth: Bearer token validated against hashed keys in the Config store.
"""
@@ -37,7 +37,8 @@ logger = logging.getLogger(__name__)
router = APIRouter(prefix="/v1", tags=["gateway"])
# Header that Open-WebUI sends with the chat ID (configurable on OWUI side).
# Headers that clients can send with the chat ID.
CHAT_ID_HEADER = "X-Chat-Id"
OWUI_CHAT_ID_HEADER = "X-OpenWebUI-Chat-Id"
@@ -58,8 +59,6 @@ def _hash_key(raw: str) -> str:
return hashlib.sha256(raw.encode()).hexdigest()
async def _validate_bearer(request: Request) -> str:
"""Validate Bearer token from Authorization header. Returns user_id."""
auth = request.headers.get("Authorization", "")
@@ -107,15 +106,17 @@ async def list_models(request: Request):
else:
model_id = f"cptr/{basename}"
models.append({
"id": model_id,
"object": "model",
"created": ws.created_at or int(time.time()),
"owned_by": "cptr",
"name": f"{ws.name} — {ws.path}",
# Extra metadata for cptr
"cptr_workspace": ws.path,
})
models.append(
{
"id": model_id,
"object": "model",
"created": ws.created_at or int(time.time()),
"owned_by": "cptr",
"name": f"{ws.name} - {ws.path}",
# Extra metadata for cptr
"cptr_workspace": ws.path,
}
)
return {"object": "list", "data": models}
@@ -150,9 +151,9 @@ async def create_chat_completion(request: Request, body: ChatCompletionRequest):
connection, bare_model, model_id = await _resolve_model_connection(workspace)
# 3. Session mapping: find or create a cptr chat
owui_chat_id = request.headers.get(OWUI_CHAT_ID_HEADER)
client_chat_id = request.headers.get(CHAT_ID_HEADER) or request.headers.get(OWUI_CHAT_ID_HEADER)
chat_id = await _find_or_create_chat(
user_id, workspace, owui_chat_id, body.messages, model_id
user_id, workspace, client_chat_id, body.messages, model_id
)
# 4. Create user + assistant messages
@@ -189,12 +190,14 @@ async def create_chat_completion(request: Request, body: ChatCompletionRequest):
# Export JSON so cptr sidebar sees it immediately
from cptr.utils.chat_export import export_chat_to_file
await export_chat_to_file(chat_id)
# 5. Create output queue and start the agentic loop
output_queue: asyncio.Queue = asyncio.Queue()
from cptr.utils.chat_task import start_task
start_task(
message_id=assistant_msg.id,
chat_id=chat_id,
@@ -227,11 +230,13 @@ async def create_chat_completion(request: Request, body: ChatCompletionRequest):
"object": "chat.completion",
"created": created,
"model": body.model,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": full_text},
"finish_reason": "stop",
}],
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": full_text},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
@@ -257,11 +262,13 @@ async def _sse_generator(
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": delta,
"finish_reason": finish_reason,
}],
"choices": [
{
"index": 0,
"delta": delta,
"finish_reason": finish_reason,
}
],
}
return f"data: {json.dumps(data)}\n\n"
@@ -272,7 +279,7 @@ async def _sse_generator(
try:
event = await asyncio.wait_for(queue.get(), timeout=300)
except asyncio.TimeoutError:
# Safety timeout — end the stream
# Safety timeout, end the stream
yield _chunk({}, "stop")
yield "data: [DONE]\n\n"
return
@@ -304,7 +311,7 @@ async def _sse_generator(
yield "data: [DONE]\n\n"
return
# Other event types (tool calls, etc.) are silently consumed —
# Other event types (tool calls, etc.) are silently consumed,
# they're persisted in cptr's DB and visible in its sidebar.
@@ -402,9 +409,7 @@ async def _resolve_model_connection(workspace: str) -> tuple[dict, str, str]:
full = f"{prefix}/{bare}" if prefix else bare
return conn, bare, full
raise HTTPException(
503, "No model connections configured. Add a connection in cptr settings."
)
raise HTTPException(503, "No model connections configured. Add a connection in cptr settings.")
# ── Session mapping ──────────────────────────────────────────
@@ -413,28 +418,29 @@ async def _resolve_model_connection(workspace: str) -> tuple[dict, str, str]:
async def _find_or_create_chat(
user_id: str,
workspace: str,
owui_chat_id: str | None,
client_chat_id: str | None,
messages: list[dict],
model_id: str,
) -> str:
"""Find an existing cptr chat for this OWUI conversation, or create one."""
"""Find an existing cptr chat for this client conversation, or create one."""
if owui_chat_id:
# Search for a chat with this OWUI chat ID in metadata
if client_chat_id:
# Search for a chat with this client chat ID in metadata
from cptr.utils.db import get_db
from sqlalchemy import select
async with await get_db() as db:
result = await db.execute(
select(Chat).where(Chat.user_id == user_id)
)
result = await db.execute(select(Chat).where(Chat.user_id == user_id))
for chat in result.scalars():
meta = chat.meta or {}
if meta.get("owui_chat_id") == owui_chat_id:
if (
meta.get("client_chat_id") == client_chat_id
or meta.get("owui_chat_id") == client_chat_id
):
return chat.id
# Create a new chat
title = "Open-WebUI Chat"
title = "Open WebUI Chat"
if messages:
first_user = next(
(m.get("content", "")[:50] for m in messages if m.get("role") == "user"),
@@ -447,8 +453,9 @@ async def _find_or_create_chat(
"workspace": workspace,
"params": {"tool_approval_mode": "full"},
}
if owui_chat_id:
meta["owui_chat_id"] = owui_chat_id
if client_chat_id:
meta["client_chat_id"] = client_chat_id
meta["owui_chat_id"] = client_chat_id
chat = await Chat.create(
user_id=user_id,
@@ -474,7 +481,7 @@ class CreateApiKeyRequest(BaseModel):
@router.post("/keys")
async def create_api_key(request: Request, body: CreateApiKeyRequest):
"""Create a new API key (requires cookie auth — admin only)."""
"""Create a new API key (requires cookie auth, admin only)."""
from cptr.utils.config import check_access
client_host = request.client.host if request.client else "127.0.0.1"