mirror of
https://github.com/open-webui/computer.git
synced 2026-12-06 11:37:58 -05:00
refac
This commit is contained in:
1 parent
2df67001f2
commit
3403257e89
5 files changed
+147
-9
No files matched your search
@@ -18,6 +18,7 @@
|
||||
import DictateButton from './DictateButton.svelte';
|
||||
import QueuedMessageItem from './QueuedMessageItem.svelte';
|
||||
import Icon from '../Icon.svelte';
|
||||
import { planMode } from '$lib/stores';
|
||||
|
||||
interface Props {
|
||||
inputText: string;
|
||||
@@ -446,12 +447,47 @@
|
||||
class="flex items-center justify-between mt-0.5 mb-2.5 mx-0.5"
|
||||
onmousedown={(e) => e.stopPropagation()}
|
||||
>
|
||||
<div class="ml-0.5 self-end flex items-center">
|
||||
<div class="ml-0.5 self-end flex items-center gap-1">
|
||||
<PlusMenu
|
||||
onfiles={(files) => {
|
||||
if (files) processFiles(Array.from(files));
|
||||
}}
|
||||
/>
|
||||
{#if $planMode}
|
||||
<button
|
||||
type="button"
|
||||
class="group p-1.5 flex gap-1 items-center text-xs rounded-full transition-colors duration-150
|
||||
text-gray-600 dark:text-gray-300 bg-gray-100 hover:bg-gray-200 dark:bg-white/8 dark:hover:bg-white/12 border border-gray-200/60 dark:border-white/8"
|
||||
onclick={() => planMode.set(false)}
|
||||
>
|
||||
<svg
|
||||
class="size-3 shrink-0 group-hover:hidden"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="1.75"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
||||
<polyline points="14 2 14 8 20 8" />
|
||||
<line x1="9" y1="13" x2="15" y2="13" />
|
||||
<line x1="9" y1="17" x2="15" y2="17" />
|
||||
</svg>
|
||||
<svg
|
||||
class="size-3 shrink-0 hidden group-hover:block"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<line x1="18" y1="6" x2="6" y2="18" />
|
||||
<line x1="6" y1="6" x2="18" y2="18" />
|
||||
</svg>
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="self-end mr-1 flex items-center gap-2">
|
||||
<ModelSelector bind:selectedModel />
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
import {
|
||||
currentWorkspace,
|
||||
toolApprovalMode,
|
||||
planMode,
|
||||
streamingBehavior,
|
||||
selectedModelId
|
||||
} from '$lib/stores';
|
||||
@@ -519,7 +520,7 @@
|
||||
workspace,
|
||||
chatId,
|
||||
parentId,
|
||||
{ tool_approval_mode: mode },
|
||||
{ tool_approval_mode: mode, plan_mode: get(planMode) },
|
||||
undefined,
|
||||
files
|
||||
);
|
||||
@@ -583,7 +584,7 @@
|
||||
workspace,
|
||||
chatId ?? undefined,
|
||||
parentId,
|
||||
{ tool_approval_mode: mode },
|
||||
{ tool_approval_mode: mode, plan_mode: get(planMode) },
|
||||
undefined,
|
||||
files
|
||||
);
|
||||
@@ -733,7 +734,8 @@
|
||||
try {
|
||||
const mode = get(toolApprovalMode);
|
||||
const result = await apiSendMessage('', selectedModel, workspace, chatId, msg.parent_id, {
|
||||
tool_approval_mode: mode
|
||||
tool_approval_mode: mode,
|
||||
plan_mode: get(planMode)
|
||||
});
|
||||
await loadChat(result.chat_id);
|
||||
} catch (e) {
|
||||
@@ -773,7 +775,7 @@
|
||||
workspace,
|
||||
chatId,
|
||||
msg.parent_id,
|
||||
{ tool_approval_mode: get(toolApprovalMode) }
|
||||
{ tool_approval_mode: get(toolApprovalMode), plan_mode: get(planMode) }
|
||||
);
|
||||
await loadChat(result.chat_id);
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { toolApprovalMode, type ToolApprovalMode } from '$lib/stores';
|
||||
import { toolApprovalMode, planMode, type ToolApprovalMode } from '$lib/stores';
|
||||
import { tooltip } from '$lib/tooltip';
|
||||
import Icon from '../Icon.svelte';
|
||||
|
||||
@@ -147,6 +147,40 @@
|
||||
|
||||
<div class="h-px bg-gray-100/50 dark:bg-white/3 mx-1 my-0.5"></div>
|
||||
|
||||
<!-- Plan mode toggle -->
|
||||
<button
|
||||
class="flex items-center gap-2 w-full h-7 px-2 rounded-xl text-xs text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white transition-colors duration-75"
|
||||
onclick={() => planMode.update((v) => !v)}
|
||||
>
|
||||
<svg
|
||||
class="size-3.5 shrink-0"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="1.5"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
||||
<polyline points="14 2 14 8 20 8" />
|
||||
<line x1="9" y1="13" x2="15" y2="13" />
|
||||
<line x1="9" y1="17" x2="15" y2="17" />
|
||||
</svg>
|
||||
<span class="flex-1 text-left truncate">Plan mode</span>
|
||||
<!-- Toggle switch -->
|
||||
<div
|
||||
class="relative w-7 h-4 rounded-full transition-colors duration-150
|
||||
{$planMode ? 'bg-blue-500' : 'bg-gray-300 dark:bg-white/10'}"
|
||||
>
|
||||
<div
|
||||
class="absolute top-0.5 left-0.5 w-3 h-3 bg-white rounded-full shadow-sm transition-transform duration-150
|
||||
{$planMode ? 'translate-x-3' : 'translate-x-0'}"
|
||||
></div>
|
||||
</div>
|
||||
</button>
|
||||
|
||||
<div class="h-px bg-gray-100/50 dark:bg-white/3 mx-1 my-0.5"></div>
|
||||
|
||||
<button
|
||||
class="flex items-center gap-2 w-full h-7 px-2 rounded-xl text-xs text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-white/5 hover:text-gray-900 dark:hover:text-white transition-colors duration-75"
|
||||
onclick={() => (tab = 'tools')}
|
||||
|
||||
@@ -70,6 +70,7 @@ export interface UserPreferences {
|
||||
sidebarOpen: boolean;
|
||||
sidebarWidth: number;
|
||||
toolApprovalMode: ToolApprovalMode;
|
||||
planMode: boolean;
|
||||
locale: string;
|
||||
workspaceOrder?: string[]; // ordered paths for sidebar drag-reorder
|
||||
keybindings?: Record<string, string>; // user-customised keyboard shortcuts
|
||||
@@ -133,6 +134,7 @@ if (typeof window !== 'undefined') {
|
||||
export const sidebarWidth = writable(220);
|
||||
export const theme = writable<Theme>('dark');
|
||||
export const toolApprovalMode = writable<ToolApprovalMode>('auto');
|
||||
export const planMode = writable(false);
|
||||
export const appVersion = writable('');
|
||||
export const lastSeenVersion = writable('');
|
||||
export const showChangelog = writable(false);
|
||||
@@ -277,6 +279,7 @@ function persistPreferences(): void {
|
||||
sidebarOpen: get(sidebarOpen),
|
||||
sidebarWidth: get(sidebarWidth),
|
||||
toolApprovalMode: get(toolApprovalMode),
|
||||
planMode: get(planMode),
|
||||
locale: i18next.language,
|
||||
workspaceOrder: get(workspaceOrder),
|
||||
keybindings: get(keybindings),
|
||||
@@ -306,6 +309,9 @@ function subscribeForPersistence() {
|
||||
toolApprovalMode.subscribe(() => {
|
||||
if (get(stateLoaded)) persistPreferences();
|
||||
});
|
||||
planMode.subscribe(() => {
|
||||
if (get(stateLoaded)) persistPreferences();
|
||||
});
|
||||
workspaceOrder.subscribe(() => {
|
||||
if (get(stateLoaded)) persistPreferences();
|
||||
});
|
||||
@@ -338,6 +344,7 @@ export async function loadPreferences(): Promise<void> {
|
||||
// Legacy boolean migration
|
||||
toolApprovalMode.set((prefs.autoApproveTools as unknown as boolean) ? 'full' : 'ask');
|
||||
}
|
||||
if (prefs.planMode !== undefined) planMode.set(prefs.planMode as boolean);
|
||||
if (prefs.locale) changeLocale(prefs.locale as string);
|
||||
if (Array.isArray(prefs.workspaceOrder)) workspaceOrder.set(prefs.workspaceOrder as string[]);
|
||||
if (prefs.keybindings) loadKeybindings(prefs.keybindings as Record<string, string>);
|
||||
|
||||
+62
-3
@@ -29,6 +29,11 @@ from cptr.utils.json_parser import extract_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PLAN_MODE_PROMPT = (
|
||||
"[Plan Mode] Research the codebase with read-only tools, then present your plan "
|
||||
"using create_file with artifact_type='implementation_plan'. Wait for approval before coding."
|
||||
)
|
||||
|
||||
# ── Task registry ───────────────────────────────────────────
|
||||
|
||||
_tasks: dict[str, asyncio.Task] = {} # message_id → asyncio.Task
|
||||
@@ -262,8 +267,39 @@ def _get_file_tree(workspace: str, max_entries: int = 200) -> str:
|
||||
return "\n".join(entries)
|
||||
|
||||
|
||||
INSTRUCTION_FILENAMES = ["MEMORY.md", "AGENTS.md", "AGENT.md", "CLAUDE.md"]
|
||||
|
||||
|
||||
def _load_instruction_files(workspace: str, max_bytes: int = 32_000) -> str:
|
||||
"""Load well-known AI instruction files from workspace root.
|
||||
|
||||
Scans for MEMORY.md, AGENTS.md, AGENT.md, CLAUDE.md.
|
||||
All found files are concatenated (not first-found-wins).
|
||||
"""
|
||||
ws = Path(workspace)
|
||||
if not ws.is_dir():
|
||||
return ""
|
||||
parts: list[str] = []
|
||||
total = 0
|
||||
for name in INSTRUCTION_FILENAMES:
|
||||
path = ws / name
|
||||
if path.is_file():
|
||||
remaining = max_bytes - total
|
||||
if remaining <= 0:
|
||||
break
|
||||
try:
|
||||
content = path.read_text(errors="replace")[:remaining].strip()
|
||||
except OSError:
|
||||
continue
|
||||
if content:
|
||||
parts.append(f"# {name}\n{content}")
|
||||
total += len(content)
|
||||
logger.debug("[instructions] Loaded %s (%d bytes)", name, len(content))
|
||||
return "\n\n".join(parts)
|
||||
|
||||
|
||||
def _load_system_prompt(workspace: str) -> str:
|
||||
"""Load system prompt: .cptr/system.md > default. Appends file tree."""
|
||||
"""Load system prompt: .cptr/system.md > default. Appends instruction files + file tree."""
|
||||
ws_prompt = Path(workspace) / ".cptr" / "system.md"
|
||||
if ws_prompt.is_file():
|
||||
base = ws_prompt.read_text(errors="replace").strip()
|
||||
@@ -271,7 +307,25 @@ def _load_system_prompt(workspace: str) -> str:
|
||||
base = (
|
||||
"You are a helpful coding assistant. "
|
||||
"You have access to tools to read, search, and modify files in the workspace. "
|
||||
"Use them to help the user with their coding tasks."
|
||||
"Use them to help the user with their coding tasks.\n\n"
|
||||
"## Planning for Complex Tasks\n"
|
||||
"For non-trivial tasks (multi-file changes, architectural decisions, ambiguous requirements), "
|
||||
"FIRST investigate the codebase, then create an implementation plan artifact by calling "
|
||||
"create_file with artifact_type='implementation_plan'. Write the plan as structured markdown "
|
||||
"with: Background, Proposed Changes (grouped by component/file), and Verification Steps. "
|
||||
"Wait for the user to review before making code changes. "
|
||||
"For simple/clear tasks (typo fixes, small edits, direct questions), skip the plan and act directly."
|
||||
)
|
||||
|
||||
# Load MEMORY.md / AGENTS.md / CLAUDE.md from workspace root
|
||||
instructions = _load_instruction_files(workspace)
|
||||
if instructions:
|
||||
base += f"\n\n<instructions>\n{instructions}\n</instructions>"
|
||||
base += (
|
||||
"\n\nThe above <instructions> were loaded from files in the workspace root "
|
||||
"(MEMORY.md, AGENTS.md, CLAUDE.md). These files persist across sessions. "
|
||||
"You can update them with your file tools to save learnings, decisions, or "
|
||||
"project conventions for future sessions."
|
||||
)
|
||||
|
||||
tree = _get_file_tree(workspace)
|
||||
@@ -569,6 +623,12 @@ async def run_chat_task(
|
||||
chat_obj = await Chat.get_by_id(chat_id)
|
||||
chat_params = (chat_obj.meta or {}).get("params", {}) if chat_obj else {}
|
||||
|
||||
# Plan mode: inject prompt
|
||||
plan_mode = chat_params.get("plan_mode", False)
|
||||
if plan_mode:
|
||||
messages.append({"role": "user", "content": PLAN_MODE_PROMPT})
|
||||
logger.info("[task %s] plan mode active", message_id[:8])
|
||||
|
||||
# Tool approval mode: 'ask' | 'auto' | 'full'
|
||||
# ask = require approval for ALL tools (including reads)
|
||||
# auto = auto-approve tools marked auto=True, ask for others
|
||||
@@ -616,7 +676,6 @@ async def run_chat_task(
|
||||
"arguments": event["arguments"],
|
||||
}
|
||||
|
||||
# Decide whether to auto-approve
|
||||
should_auto = approval_mode == "full" or (
|
||||
approval_mode == "auto" and tool and tool["auto"]
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user