This commit is contained in:
Timothy Jaeryang Baek
2026-07-31 17:46:13 -04:00
Unverified
parent 7c0d69fdb9
commit 10507c0cef
17 changed files with 323 additions and 123 deletions
@@ -21,9 +21,13 @@
nextTab: $t('keyboard.nextTab'),
prevTab: $t('keyboard.prevTab'),
quickOpen: $t('keyboard.quickOpen'),
searchAll: $t('keyboard.searchAll'),
openSettings: $t('keyboard.openSettings'),
toggleSplit: $t('keyboard.toggleSplit'),
toggleSidebar: $t('keyboard.toggleSidebar')
toggleSidebar: $t('keyboard.toggleSidebar'),
approveToolCall: $t('keyboard.approveToolCall'),
rejectToolCall: $t('keyboard.rejectToolCall'),
voiceMemo: $t('keyboard.voiceMemo')
});
/** Translated action labels for display. */
@@ -40,6 +44,8 @@
openSettings: $t('keyboard.action.openSettings'),
toggleSplit: $t('keyboard.action.toggleSplit'),
toggleSidebar: $t('keyboard.action.toggleSidebar'),
approveToolCall: $t('keyboard.action.approveToolCall'),
rejectToolCall: $t('keyboard.action.rejectToolCall'),
voiceMemo: $t('keyboard.action.voiceMemo')
});
@@ -30,7 +30,8 @@
streamingChatTabs,
registerStreamingChat,
unregisterStreamingChat,
updateChatStatuses
updateChatStatuses,
registerToolApprovalShortcutHandler
} from '$lib/stores/chat';
import { socketStore } from '$lib/stores/socket.svelte';
import { onMount, onDestroy, tick } from 'svelte';
@@ -763,7 +764,7 @@
}
refreshCommandSessions();
commandSessionsTimer = setInterval(refreshCommandSessions, 5000);
window.addEventListener('cptr:inspect-command-session', handleInspectCommandSession);
window.addEventListener('computer:inspectCommandSession', handleInspectCommandSession);
const offChat = socketStore.on('events:chat', handleSocketEvent);
const offConnect = socketStore.on('connect', handleReconnect);
@@ -779,7 +780,7 @@
unbindSocketListeners = null;
if (commandSessionsTimer) clearInterval(commandSessionsTimer);
commandSessionsTimer = null;
window.removeEventListener('cptr:inspect-command-session', handleInspectCommandSession);
window.removeEventListener('computer:inspectCommandSession', handleInspectCommandSession);
if (landingRefreshTimer) clearTimeout(landingRefreshTimer);
if (taskClearTimer) clearTimeout(taskClearTimer);
// Don't clear streamingChatTabs here -- the global listener in
@@ -821,6 +822,11 @@
return () => unregisterStreamingChat(chatId!, tabId!);
});
$effect(() => {
if (!active) return;
return registerToolApprovalShortcutHandler(handleApproveToolCallShortcut);
});
// ── Persist read state separately from visibility tracking ──
$effect(() => {
@@ -1265,7 +1271,8 @@
item.type === 'function_call' && item.call_id === callId && item.status === 'pending'
);
if (call) {
call.status = approved ? 'running' : 'rejected';
call.status = approved ? 'queued' : 'rejected';
if (approved) call.approved = true;
allMessages = [...allMessages]; // trigger reactivity
}
}
@@ -1277,6 +1284,22 @@
});
}
function handleApproveToolCallShortcut(approved: boolean) {
for (const { msg } of [...activePath].reverse()) {
const item = msg.output?.find(
(output: any) =>
output.type === 'function_call' &&
output.name !== 'ask_user' &&
output.status === 'pending'
);
if (!item) continue;
handleApprove(msg.id, item.call_id, approved);
return true;
}
return false;
}
function handleAskUserAnswer(
messageId: string,
callId: string,
@@ -513,9 +513,13 @@
"keyboard.nextTab": "Zum nächsten Tab wechseln",
"keyboard.prevTab": "Zum vorherigen Tab wechseln",
"keyboard.quickOpen": "Dateien schnell suchen und öffnen",
"keyboard.searchAll": "Chats und Dateien durchsuchen",
"keyboard.openSettings": "Einstellungen öffnen",
"keyboard.toggleSplit": "Geteilte Ansicht umschalten",
"keyboard.toggleSidebar": "Seitenleiste ein-/ausblenden",
"keyboard.approveToolCall": "Ausstehenden Tool-Aufruf genehmigen",
"keyboard.rejectToolCall": "Ausstehenden Tool-Aufruf ablehnen",
"keyboard.voiceMemo": "Sprachnotiz-Rekorder öffnen",
"changelog.whatsNew": "Neuigkeiten",
"changelog.releaseNotes": "Versionshinweise",
"changelog.loadError": "Versionshinweise konnten nicht geladen werden.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "Einstellungen öffnen",
"keyboard.action.toggleSplit": "Teilung umschalten",
"keyboard.action.toggleSidebar": "Seitenleiste umschalten",
"keyboard.action.approveToolCall": "Tool-Aufruf genehmigen",
"keyboard.action.rejectToolCall": "Tool-Aufruf ablehnen",
"keyboard.action.voiceMemo": "Sprachnotiz",
"keyboard.conflict": "Auch belegt von {{action}}",
"chat.history.justNow": "Gerade eben",
@@ -540,9 +540,13 @@
"keyboard.nextTab": "Switch to the next tab",
"keyboard.prevTab": "Switch to the previous tab",
"keyboard.quickOpen": "Search and open files quickly",
"keyboard.searchAll": "Search across chats and files",
"keyboard.openSettings": "Open the settings panel",
"keyboard.toggleSplit": "Toggle split editor view",
"keyboard.toggleSidebar": "Show or hide the sidebar",
"keyboard.approveToolCall": "Approve the pending tool call",
"keyboard.rejectToolCall": "Reject the pending tool call",
"keyboard.voiceMemo": "Open voice memo recorder",
"changelog.whatsNew": "What's New",
"changelog.releaseNotes": "Release Notes",
"changelog.loadError": "Couldn't load release notes.",
@@ -863,6 +867,8 @@
"keyboard.action.openSettings": "Open Settings",
"keyboard.action.toggleSplit": "Toggle Split",
"keyboard.action.toggleSidebar": "Toggle Sidebar",
"keyboard.action.approveToolCall": "Approve Tool Call",
"keyboard.action.rejectToolCall": "Reject Tool Call",
"keyboard.action.voiceMemo": "Voice Memo",
"keyboard.conflict": "Also bound to {{action}}",
"chat.history.justNow": "Just now",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "Cambiar a la siguiente pestaña",
"keyboard.prevTab": "Cambiar a la pestaña anterior",
"keyboard.quickOpen": "Buscar y abrir archivos rápidamente",
"keyboard.searchAll": "Buscar en chats y archivos",
"keyboard.openSettings": "Abrir el panel de ajustes",
"keyboard.toggleSplit": "Alternar vista dividida del editor",
"keyboard.toggleSidebar": "Mostrar u ocultar la barra lateral",
"keyboard.approveToolCall": "Aprobar la llamada de herramienta pendiente",
"keyboard.rejectToolCall": "Rechazar la llamada de herramienta pendiente",
"keyboard.voiceMemo": "Abrir grabadora de nota de voz",
"changelog.whatsNew": "Novedades",
"changelog.releaseNotes": "Notas de la versión",
"changelog.loadError": "No se pudieron cargar las notas de la versión.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "Abrir ajustes",
"keyboard.action.toggleSplit": "Alternar división",
"keyboard.action.toggleSidebar": "Alternar barra lateral",
"keyboard.action.approveToolCall": "Aprobar herramienta",
"keyboard.action.rejectToolCall": "Rechazar herramienta",
"keyboard.action.voiceMemo": "Nota de voz",
"keyboard.conflict": "También asignado a {{action}}",
"chat.history.justNow": "Ahora mismo",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "Passer à l'onglet suivant",
"keyboard.prevTab": "Passer à l'onglet précédent",
"keyboard.quickOpen": "Rechercher et ouvrir des fichiers rapidement",
"keyboard.searchAll": "Rechercher dans les chats et fichiers",
"keyboard.openSettings": "Ouvrir le panneau des paramètres",
"keyboard.toggleSplit": "Basculer la vue divisée de l'éditeur",
"keyboard.toggleSidebar": "Afficher ou masquer la barre latérale",
"keyboard.approveToolCall": "Approuver l'appel d'outil en attente",
"keyboard.rejectToolCall": "Rejeter l'appel d'outil en attente",
"keyboard.voiceMemo": "Ouvrir l'enregistreur de note vocale",
"changelog.whatsNew": "Nouveautés",
"changelog.releaseNotes": "Notes de version",
"changelog.loadError": "Impossible de charger les notes de version.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "Ouvrir les paramètres",
"keyboard.action.toggleSplit": "Basculer la division",
"keyboard.action.toggleSidebar": "Basculer la barre latérale",
"keyboard.action.approveToolCall": "Approuver l'outil",
"keyboard.action.rejectToolCall": "Rejeter l'outil",
"keyboard.action.voiceMemo": "Note vocale",
"keyboard.conflict": "Également lié à {{action}}",
"chat.history.justNow": "À l'instant",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "次のタブに切り替える",
"keyboard.prevTab": "前のタブに切り替える",
"keyboard.quickOpen": "ファイルをすばやく検索して開く",
"keyboard.searchAll": "チャットとファイルを検索",
"keyboard.openSettings": "設定パネルを開く",
"keyboard.toggleSplit": "エディタの分割表示を切替",
"keyboard.toggleSidebar": "サイドバーの表示/非表示",
"keyboard.approveToolCall": "保留中のツール呼び出しを承認",
"keyboard.rejectToolCall": "保留中のツール呼び出しを拒否",
"keyboard.voiceMemo": "音声メモ録音を開く",
"changelog.whatsNew": "新機能",
"changelog.releaseNotes": "リリースノート",
"changelog.loadError": "リリースノートを読み込めませんでした。",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "設定を開く",
"keyboard.action.toggleSplit": "分割を切替",
"keyboard.action.toggleSidebar": "サイドバーを切替",
"keyboard.action.approveToolCall": "ツールを承認",
"keyboard.action.rejectToolCall": "ツールを拒否",
"keyboard.action.voiceMemo": "音声メモ",
"keyboard.conflict": "{{action}}にも割り当て済み",
"chat.history.justNow": "たった今",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "다음 탭으로 전환",
"keyboard.prevTab": "이전 탭으로 전환",
"keyboard.quickOpen": "파일을 빠르게 검색하고 열기",
"keyboard.searchAll": "채팅과 파일 검색",
"keyboard.openSettings": "설정 패널 열기",
"keyboard.toggleSplit": "편집기 분할 보기 전환",
"keyboard.toggleSidebar": "사이드바 표시/숨기기",
"keyboard.approveToolCall": "대기 중인 도구 호출 승인",
"keyboard.rejectToolCall": "대기 중인 도구 호출 거부",
"keyboard.voiceMemo": "음성 메모 녹음기 열기",
"changelog.whatsNew": "새로운 기능",
"changelog.releaseNotes": "릴리스 노트",
"changelog.loadError": "릴리스 노트를 불러올 수 없습니다.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "설정 열기",
"keyboard.action.toggleSplit": "분할 전환",
"keyboard.action.toggleSidebar": "사이드바 전환",
"keyboard.action.approveToolCall": "도구 승인",
"keyboard.action.rejectToolCall": "도구 거부",
"keyboard.action.voiceMemo": "음성 메모",
"keyboard.conflict": "{{action}}에도 할당됨",
"chat.history.justNow": "방금",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "Mudar para a próxima aba",
"keyboard.prevTab": "Mudar para a aba anterior",
"keyboard.quickOpen": "Pesquisar e abrir arquivos rapidamente",
"keyboard.searchAll": "Pesquisar em chats e arquivos",
"keyboard.openSettings": "Abrir o painel de configurações",
"keyboard.toggleSplit": "Alternar visualização dividida do editor",
"keyboard.toggleSidebar": "Mostrar ou ocultar a barra lateral",
"keyboard.approveToolCall": "Aprovar a chamada de ferramenta pendente",
"keyboard.rejectToolCall": "Rejeitar a chamada de ferramenta pendente",
"keyboard.voiceMemo": "Abrir gravador de nota de voz",
"changelog.whatsNew": "Novidades",
"changelog.releaseNotes": "Notas da versão",
"changelog.loadError": "Não foi possível carregar as notas da versão.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "Abrir configurações",
"keyboard.action.toggleSplit": "Alternar divisão",
"keyboard.action.toggleSidebar": "Alternar barra lateral",
"keyboard.action.approveToolCall": "Aprovar ferramenta",
"keyboard.action.rejectToolCall": "Rejeitar ferramenta",
"keyboard.action.voiceMemo": "Nota de voz",
"keyboard.conflict": "Também atribuído a {{action}}",
"chat.history.justNow": "Agora",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "Перейти к следующей вкладке",
"keyboard.prevTab": "Перейти к предыдущей вкладке",
"keyboard.quickOpen": "Быстрый поиск и открытие файлов",
"keyboard.searchAll": "Поиск в чатах и файлах",
"keyboard.openSettings": "Открыть панель настроек",
"keyboard.toggleSplit": "Переключить разделённый вид редактора",
"keyboard.toggleSidebar": "Показать или скрыть боковую панель",
"keyboard.approveToolCall": "Одобрить ожидающий вызов инструмента",
"keyboard.rejectToolCall": "Отклонить ожидающий вызов инструмента",
"keyboard.voiceMemo": "Открыть запись голосовой заметки",
"changelog.whatsNew": "Что нового",
"changelog.releaseNotes": "Примечания к выпуску",
"changelog.loadError": "Не удалось загрузить примечания к выпуску.",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "Открыть настройки",
"keyboard.action.toggleSplit": "Переключить разделение",
"keyboard.action.toggleSidebar": "Переключить боковую панель",
"keyboard.action.approveToolCall": "Одобрить инструмент",
"keyboard.action.rejectToolCall": "Отклонить инструмент",
"keyboard.action.voiceMemo": "Голосовая заметка",
"keyboard.conflict": "Также назначено на {{action}}",
"chat.history.justNow": "Только что",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "切换到下一个标签页",
"keyboard.prevTab": "切换到上一个标签页",
"keyboard.quickOpen": "快速搜索并打开文件",
"keyboard.searchAll": "搜索聊天和文件",
"keyboard.openSettings": "打开设置面板",
"keyboard.toggleSplit": "切换编辑器分割视图",
"keyboard.toggleSidebar": "显示或隐藏侧边栏",
"keyboard.approveToolCall": "批准待处理的工具调用",
"keyboard.rejectToolCall": "拒绝待处理的工具调用",
"keyboard.voiceMemo": "打开语音备忘录录制器",
"changelog.whatsNew": "新功能",
"changelog.releaseNotes": "发行说明",
"changelog.loadError": "无法加载发行说明。",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "打开设置",
"keyboard.action.toggleSplit": "切换分割",
"keyboard.action.toggleSidebar": "切换侧边栏",
"keyboard.action.approveToolCall": "批准工具调用",
"keyboard.action.rejectToolCall": "拒绝工具调用",
"keyboard.action.voiceMemo": "语音备忘录",
"keyboard.conflict": "同时绑定到 {{action}}",
"chat.history.justNow": "刚刚",
@@ -513,9 +513,13 @@
"keyboard.nextTab": "切換到下一個分頁",
"keyboard.prevTab": "切換到上一個分頁",
"keyboard.quickOpen": "快速搜尋並開啟檔案",
"keyboard.searchAll": "搜尋聊天和檔案",
"keyboard.openSettings": "開啟設定面板",
"keyboard.toggleSplit": "切換編輯器分割檢視",
"keyboard.toggleSidebar": "顯示或隱藏側邊欄",
"keyboard.approveToolCall": "核准待處理的工具呼叫",
"keyboard.rejectToolCall": "拒絕待處理的工具呼叫",
"keyboard.voiceMemo": "開啟語音備忘錄錄製器",
"changelog.whatsNew": "最新消息",
"changelog.releaseNotes": "發行說明",
"changelog.loadError": "無法載入發行說明。",
@@ -826,6 +830,8 @@
"keyboard.action.openSettings": "開啟設定",
"keyboard.action.toggleSplit": "切換分割",
"keyboard.action.toggleSidebar": "切換側邊欄",
"keyboard.action.approveToolCall": "核准工具呼叫",
"keyboard.action.rejectToolCall": "拒絕工具呼叫",
"keyboard.action.voiceMemo": "語音備忘錄",
"keyboard.conflict": "同時繫結到 {{action}}",
"chat.history.justNow": "剛才",
+21
View File
@@ -106,6 +106,27 @@ export function unregisterStreamingChat(chatId: string, tabId: string) {
if (tabIds.size === 0) chatToTabs.delete(chatId);
}
type ToolApprovalShortcutHandler = (approved: boolean) => boolean;
let toolApprovalShortcutHandler: ToolApprovalShortcutHandler | null = null;
export function registerToolApprovalShortcutHandler(
handler: ToolApprovalShortcutHandler
): () => void {
toolApprovalShortcutHandler = handler;
return () => {
if (toolApprovalShortcutHandler === handler) toolApprovalShortcutHandler = null;
};
}
export function approveActiveToolCallShortcut(): boolean {
return toolApprovalShortcutHandler?.(true) ?? false;
}
export function rejectActiveToolCallShortcut(): boolean {
return toolApprovalShortcutHandler?.(false) ?? false;
}
/**
* Bind a global socket listener that clears streamingChatTabs when a
* chat's "done" event arrives -- even if the ChatPanel is not mounted.
@@ -41,6 +41,8 @@ export const ACTION_IDS = [
'openSettings',
'toggleSplit',
'toggleSidebar',
'approveToolCall',
'rejectToolCall',
'voiceMemo'
] as const;
@@ -61,6 +63,8 @@ export const ACTION_LABELS: Record<ActionId, string> = {
openSettings: 'Open Settings',
toggleSplit: 'Toggle Split',
toggleSidebar: 'Toggle Sidebar',
approveToolCall: 'Approve Tool Call',
rejectToolCall: 'Reject Tool Call',
voiceMemo: 'Voice Memo'
};
@@ -79,6 +83,8 @@ export const DEFAULT_KEYBINDINGS: Record<ActionId, string> = {
openSettings: 'Cmd+.',
toggleSplit: 'Cmd+\\',
toggleSidebar: 'Cmd+Shift+S',
approveToolCall: 'Cmd+Shift+Enter',
rejectToolCall: 'Cmd+Shift+Backspace',
voiceMemo: 'Cmd+Shift+M'
};
@@ -243,6 +249,8 @@ export function executeAction(
toggleSettings?: () => void;
toggleSearch?: () => void;
toggleVoiceMemo?: () => void;
approveToolCall?: () => boolean;
rejectToolCall?: () => boolean;
}
): boolean {
const dispatchHomeAction = (
@@ -341,6 +349,14 @@ export function executeAction(
sidebarOpen.update((v) => !v);
return true;
case 'approveToolCall': {
return callbacks?.approveToolCall?.() ?? false;
}
case 'rejectToolCall': {
return callbacks?.rejectToolCall?.() ?? false;
}
case 'voiceMemo':
callbacks?.toggleVoiceMemo?.();
return true;
+11 -4
View File
@@ -42,7 +42,12 @@
import { fetchJSON } from '$lib/apis';
import { gitStatusStore } from '$lib/stores/gitStatus.svelte';
import { t } from '$lib/i18n';
import { refreshChatState, bindGlobalChatListener } from '$lib/stores/chat';
import {
refreshChatState,
bindGlobalChatListener,
approveActiveToolCallShortcut,
rejectActiveToolCallShortcut
} from '$lib/stores/chat';
import { refreshAudioState } from '$lib/stores/audio';
import SetupWizard from '$lib/components/SetupWizard.svelte';
@@ -331,8 +336,7 @@
function handleKeydown(e: KeyboardEvent) {
const action = matchKeybinding(e);
if (!action) return;
e.preventDefault();
executeAction(action, {
const handled = executeAction(action, {
toggleQuickOpen: () => {
showSearch.update((v) => !v);
},
@@ -348,8 +352,11 @@
if (get(voiceMemosEnabled)) showVoiceMemo.update((v) => !v);
});
});
}
},
approveToolCall: approveActiveToolCallShortcut,
rejectToolCall: rejectActiveToolCallShortcut
});
if (handled) e.preventDefault();
}
// Chat events belong to the authenticated user, not a workspace.
+34 -75
View File
@@ -491,9 +491,7 @@ async def _get_chat_context_usage(chat, model_id: str | None = None) -> dict | N
tokens += estimate_messages_tokens(
[{"role": m.role, "content": m.content or ""} for m in trailing_messages]
)
return build_context_usage(
tokens, threshold=compact_token_threshold
)
return build_context_usage(tokens, threshold=compact_token_threshold)
return estimate_context_usage(messages, system, threshold=compact_token_threshold)
@@ -1069,84 +1067,45 @@ async def approve_tool(chat_id: str, message_id: str, body: ApproveRequest, requ
if not call:
raise HTTPException(400, "no pending tool call with that call_id")
model_id = msg.model or ""
workspace = chat.meta.get("workspace", "") if chat.meta else ""
from cptr.socket.main import emit_to_user
if body.approved:
# Execute the tool
from cptr.utils.tools import execute_tool
model_id = msg.model or ""
result = await execute_tool(
call["name"],
call.get("arguments", {}),
{
"workspace": chat.meta.get("workspace", ""),
"user_id": user_id,
"model_id": model_id,
"chat_id": chat_id,
"message_id": message_id,
"call_id": body.call_id,
},
)
call["status"] = "completed"
output.append(
{
"type": "function_call_output",
"call_id": body.call_id,
"output": result,
}
)
# Emit artifact card if the tool produced an artifact
from cptr.utils.chat_task import build_artifact_item
artifact_item = build_artifact_item(call["name"], call.get("arguments", {}), result)
if artifact_item:
output.append(artifact_item)
from cptr.socket.main import emit_to_user
await emit_to_user(
user_id,
{"chat_id": chat_id, "message_id": message_id, "output": artifact_item},
)
if call["name"] == "display_file":
try:
file_item = json.loads(result)
except (json.JSONDecodeError, TypeError):
file_item = None
if isinstance(file_item, dict) and file_item.get("type") == "file":
output.append(file_item)
from cptr.socket.main import emit_to_user
await emit_to_user(
user_id,
{"chat_id": chat_id, "message_id": message_id, "output": file_item},
)
call["approved"] = True
call["status"] = "queued"
await ChatMessage.update(message_id, output=output, done=False)
# Resolve model target and continue
from cptr.utils.model_targets import resolve_model_target
target = await resolve_model_target(model_id, request.app.state)
workspace = chat.meta.get("workspace", "") if chat.meta else ""
from cptr.utils.chat_task import start_task
start_task(
message_id=message_id,
chat_id=chat_id,
user_id=user_id,
workspace=workspace,
target=target,
)
await emit_to_user(user_id, {"chat_id": chat_id, "message_id": message_id, "output": call})
else:
call["status"] = "rejected"
await ChatMessage.update(message_id, output=output, done=True)
# Process pending inputs since this chat is now idle.
from cptr.utils.chat_task import process_pending_chat_inputs
result_item = {
"type": "function_call_output",
"call_id": body.call_id,
"output": "Error: tool call rejected by user.",
}
output.append(result_item)
await ChatMessage.update(message_id, output=output, done=False)
await emit_to_user(user_id, {"chat_id": chat_id, "message_id": message_id, "output": call})
await emit_to_user(
user_id,
{"chat_id": chat_id, "message_id": message_id, "output": result_item},
)
workspace = chat.meta.get("workspace", "") if chat.meta else ""
await process_pending_chat_inputs(chat_id, user_id, workspace)
# Resolve model target and continue the saved tool-call queue.
from cptr.utils.model_targets import resolve_model_target
target = await resolve_model_target(model_id, request.app.state)
from cptr.utils.chat_task import start_task
start_task(
message_id=message_id,
chat_id=chat_id,
user_id=user_id,
workspace=workspace,
target=target,
)
return {"ok": True}
+147 -39
View File
@@ -785,7 +785,9 @@ async def generate_chat_title(
# ── Message history ─────────────────────────────────────────
def _output_items_to_messages(output_items: list[dict], message_id: str | None = None) -> list[dict]:
def _output_items_to_messages(
output_items: list[dict], message_id: str | None = None
) -> list[dict]:
"""Convert ordered persisted output items into model-visible messages."""
native_agent_call_ids = {
item["call_id"]
@@ -847,7 +849,7 @@ def _output_items_to_messages(output_items: list[dict], message_id: str | None =
):
continue
pending_reasoning_items.append(item)
elif itype == "function_call" and item.get("status") == "completed":
elif itype == "function_call" and item.get("status") in {"completed", "rejected"}:
call_id = item.get("call_id")
if not call_id or call_id in native_agent_call_ids:
continue
@@ -1140,6 +1142,7 @@ def _scrub_incomplete_items(output_items: list[dict]) -> None:
if item.get("type") == "function_call" and item.get("status") in (
"in_progress",
"pending",
"queued",
):
item["status"] = "failed"
@@ -1897,6 +1900,122 @@ async def run_chat_task(
if "tool_approval_mode" not in chat_params and "auto_approve_tools" in chat_params:
approval_mode = "full" if chat_params["auto_approve_tools"] else "auto"
async def run_queued_tool_calls(tool_ctx: dict) -> str:
"""Run queued tool calls until approval is needed or the queue is empty.
Returns: "idle", "completed", or "approval_required".
"""
processed_any = False
for item in output_items:
if item.get("type") != "function_call":
continue
if item.get("name") == ASK_USER_NAME or _is_native_agent_tool_item(item):
continue
if item.get("status") in {"completed", "rejected", "failed"}:
continue
call_id = item.get("call_id")
if call_id and any(
result.get("type") == "function_call_output"
and result.get("call_id") == call_id
for result in output_items
):
item["status"] = "completed"
continue
name = item.get("name", "")
tool = ALL_TOOLS.get(name)
needs_approval = not (
approval_mode == "full" or (approval_mode == "auto" and tool and tool["auto"])
)
if needs_approval and not item.get("approved"):
item["status"] = "pending"
await _save_message(
"pending approval",
content=content,
output=output_items,
done=False,
)
await emit(output=item)
_task_state.pop(message_id, None)
await emit(
done=True,
status="approval_required",
title=chat_obj.title if chat_obj else None,
tool_name=name,
workspace=workspace,
workspace_name=workspace.rstrip("/").rsplit("/", 1)[-1]
if workspace
else "",
)
return "approval_required"
item["status"] = "in_progress"
await emit(output=item)
_sync_state()
await _save_message("tool call in progress", content=content, output=output_items)
arguments = item.get("arguments") or {}
if name == "create_artifact":
args = dict(arguments)
args.pop("workspace", None)
result = await create_artifact(**args, workspace=workspace)
else:
result = await execute_tool(
name,
arguments,
{**tool_ctx, "call_id": item["call_id"]},
)
result_item = {
"type": "function_call_output",
"call_id": item["call_id"],
"output": result,
}
output_items.append(result_item)
item["status"] = "completed"
await emit(output=item)
await emit(output=result_item)
_sync_state()
artifact_item = build_artifact_item(name, arguments, result)
if artifact_item:
output_items.append(artifact_item)
await emit(output=artifact_item)
_sync_state()
if name == "display_file":
try:
file_item = json.loads(result)
except (json.JSONDecodeError, TypeError):
file_item = None
if isinstance(file_item, dict) and file_item.get("type") == "file":
output_items.append(file_item)
await emit(output=file_item)
_sync_state()
await _save_message("tool call complete", content=content, output=output_items)
processed_any = True
return "completed" if processed_any else "idle"
tool_ctx = {
"workspace": workspace,
"user_id": user_id,
"model_id": model,
"full_model_id": ((chat_obj.meta or {}).get("last_model") if chat_obj else None)
or model,
"chat_id": chat_id,
"message_id": message_id,
"connection": connection,
"builtin_tools": builtin_tools,
}
resumed_calls = await run_queued_tool_calls(tool_ctx)
if resumed_calls == "approval_required":
return
if resumed_calls == "completed":
messages, loaded_summary = await _load_message_history(chat_id, message_id)
last_usage: dict | None = None # real usage from last API call
new_messages_since: int = 0 # messages appended since last API call
@@ -2186,18 +2305,6 @@ async def run_chat_task(
)
flushed_item = _flush_text()
tool_ctx = {
"workspace": workspace,
"user_id": user_id,
"model_id": model,
"full_model_id": ((chat_obj.meta or {}).get("last_model") if chat_obj else None)
or model,
"chat_id": chat_id,
"message_id": message_id,
"connection": connection,
"builtin_tools": builtin_tools,
}
ask_user_calls = [tc for tc in pending_calls if tc["name"] == ASK_USER_NAME]
if ask_user_calls:
tc = ask_user_calls[0]
@@ -2319,39 +2426,40 @@ async def run_chat_task(
break
if needs_approval:
# First non-auto tool stops the loop for approval
tc = needs_approval
item = {
"type": "function_call",
"id": str(uuid.uuid4()),
"call_id": tc["call_id"],
"fc_id": tc.get("id", ""),
"name": tc["name"],
"arguments": tc["arguments"],
"status": "pending",
}
output_items.append(item)
call_items = []
for tc in pending_calls:
item = {
"type": "function_call",
"id": str(uuid.uuid4()),
"call_id": tc["call_id"],
"fc_id": tc.get("id", ""),
"name": tc["name"],
"arguments": tc["arguments"],
"status": "queued",
}
output_items.append(item)
call_items.append(item)
await _save_message(
"pending approval",
"queued tool calls",
content=content,
output=output_items,
done=False,
)
if flushed_item:
await emit(output=flushed_item)
for item in call_items:
await emit(output=item)
_task_state.pop(message_id, None)
await emit(
done=True,
status="approval_required",
title=chat_obj.title if chat_obj else None,
tool_name=tc["name"],
workspace=workspace,
workspace_name=workspace.rstrip("/").rsplit("/", 1)[-1]
if workspace
else "",
)
return
queue_result = await run_queued_tool_calls(tool_ctx)
if queue_result == "approval_required":
return
if queue_result == "completed":
messages, loaded_summary = await _load_message_history(chat_id, message_id)
new_messages_since = 0
restart = True
continue
raise RuntimeError("tool-call queue did not make progress")
# All calls are auto-approved — build UI items
call_items: list[tuple[dict, dict]] = [] # (event, ui_item)