Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
</script>

<div class="my-2 rounded-lg border border-(--action-card-border) bg-card p-3">
<div class="mb-3 flex items-center gap-2 text-body-sm">
<IconComponent class="{ICON_CLASS_DEFAULT} shrink-0 text-muted-foreground" />
<div class="mb-3 flex items-start gap-2 text-body-sm">
<!-- on the first line of the message, however long it runs -->
<IconComponent class="{ICON_CLASS_DEFAULT} mt-0.5 shrink-0 text-muted-foreground" />

<span>
<div class="min-w-0 flex-1">
{@render message()}
</span>
</div>
</div>

{#if detail}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,86 @@
import { Button, buttonVariants } from '@ggml-org/llama-ui/ui/button';
import * as ButtonGroup from '@ggml-org/llama-ui/ui/button-group';
import * as DropdownMenu from '@ggml-org/llama-ui/ui/dropdown-menu';
import { Input } from '@ggml-org/llama-ui/ui/input';
import { cn } from '@ggml-org/llama-ui/utils';
import { ChevronDown, ShieldQuestion } from '@lucide/svelte';
import { ChatMessageActionCard } from '#lib/components/app/index.ts';
import { TOOL_SERVER_LABELS } from '#lib/constants/index.ts';
import { ToolPermissionDecision, ToolSource } from '#lib/enums/index.ts';
import { toolsStore } from '#lib/stores/index.ts';
import type { PermissionAnswer, PermissionRequest } from '#lib/types/index.ts';

interface Props {
toolName: string;
serverLabel: string;
/**
* The shell command the call would run, shown in full: the trace row may
* show the model's own description of it, which is not what gets executed.
*/
command?: string;
onDecision: (decision: ToolPermissionDecision) => void;
request: PermissionRequest;
onDecision: (decision: ToolPermissionDecision, answer?: PermissionAnswer) => void;
}

let { command, onDecision, serverLabel, toolName }: Props = $props();
let { onDecision, request }: Props = $props();

// the proposed patterns, editable before a choice that remembers them
// svelte-ignore state_referenced_locally
let patterns = $state([...request.patterns]);

const isShell = $derived(request.command !== undefined);
const folderName = $derived(
request.cwd
?.replace(/[\\/]+$/, '')
.split(/[\\/]/)
.pop() || request.cwd
);
const providerName = $derived.by(() => {
if (request.serverLabel) return request.serverLabel;

const source = toolsStore.getToolSource(request.toolName);

if (source === ToolSource.SERVER) return TOOL_SERVER_LABELS[ToolSource.SERVER];

if (source === ToolSource.CUSTOM) return TOOL_SERVER_LABELS[ToolSource.CUSTOM];

return 'MCP Tools';
});

function decide(decision: ToolPermissionDecision) {
onDecision(decision, isShell ? { patterns } : undefined);
}
</script>

<ChatMessageActionCard icon={ShieldQuestion}>
{#snippet message()}
Allow use of <span class="font-semibold">{toolName}</span>{#if serverLabel}
&nbsp;from <span class="font-semibold">{serverLabel}</span>{/if}?
Allow use of <span class="font-semibold">{request.toolName}</span>{#if request.serverLabel}
&nbsp;from <span class="font-semibold">{request.serverLabel}</span>{/if}?

{#if isShell && patterns.length > 0}
<span class="mt-2 flex flex-col gap-1.5">
<span class="text-caption text-muted-foreground">
Remembered as {patterns.length > 1 ? 'these patterns' : 'this pattern'} when you allow it for
this chat or always:
</span>

{#each patterns as _, index (index)}
<Input
bind:value={patterns[index]}
aria-label="Command pattern {index + 1}"
class="h-7 font-mono text-caption"
spellcheck={false}
/>
{/each}
</span>
{/if}
{/snippet}

{#snippet detail()}
{#if command}
<!-- the command in full: the trace row may show the model's own description of it -->
{#if request.command !== undefined}
<pre
class="mb-3 max-h-40 overflow-auto rounded-md bg-(--surface-code) px-3 py-2 font-mono text-caption whitespace-pre-wrap break-words">{command}</pre>
class="mb-3 max-h-40 overflow-auto rounded-md bg-(--surface-code) px-3 py-2 font-mono text-caption whitespace-pre-wrap break-words">{request.command}</pre>
{/if}
{/snippet}

{#snippet actions()}
<DropdownMenu.Root>
<ButtonGroup.Root>
<Button onclick={() => onDecision(ToolPermissionDecision.ONCE)} size="sm" variant="outline">
<Button onclick={() => decide(ToolPermissionDecision.ONCE)} size="sm" variant="outline">
Allow once
</Button>

Expand All @@ -57,31 +99,34 @@
</ButtonGroup.Root>

<DropdownMenu.Content align="start" class="min-w-32">
<DropdownMenu.Item onclick={() => onDecision(ToolPermissionDecision.ALWAYS)}>
Always allow <pre>{toolName}</pre>
tool
<DropdownMenu.Item onclick={() => decide(ToolPermissionDecision.SESSION)}>
Allow for this chat
</DropdownMenu.Item>

{#if serverLabel}
<DropdownMenu.Item onclick={() => onDecision(ToolPermissionDecision.ALWAYS_SERVER)}>
Always allow all tools from {serverLabel}
{#if request.cwd}
<DropdownMenu.Item onclick={() => decide(ToolPermissionDecision.ALWAYS_HERE)}>
Always allow in <span class="font-mono" title={request.cwd}>{folderName}</span>
</DropdownMenu.Item>
{:else}
{@const source = toolsStore.getToolSource(toolName)}
{@const providerName =
source === ToolSource.SERVER
? TOOL_SERVER_LABELS[ToolSource.SERVER]
: source === ToolSource.CUSTOM
? TOOL_SERVER_LABELS[ToolSource.CUSTOM]
: 'MCP Tools'}
<DropdownMenu.Item onclick={() => onDecision(ToolPermissionDecision.ALWAYS_SERVER)}>
Approve all tools from {providerName}
{/if}

<DropdownMenu.Item onclick={() => decide(ToolPermissionDecision.ALWAYS)}>
{#if isShell}
Always allow everywhere
{:else}
Always allow <pre>{request.toolName}</pre>
tool
{/if}
</DropdownMenu.Item>

{#if !isShell}
<DropdownMenu.Item onclick={() => decide(ToolPermissionDecision.ALWAYS_SERVER)}>
Always allow all tools from {providerName}
</DropdownMenu.Item>
{/if}
</DropdownMenu.Content>
</DropdownMenu.Root>

<Button onclick={() => onDecision(ToolPermissionDecision.DENY)} size="sm" variant="destructive">
<Button onclick={() => decide(ToolPermissionDecision.DENY)} size="sm" variant="destructive">
Deny
</Button>
{/snippet}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import ChatMessageToolCallBlock from './ChatMessage/ChatMessageToolCall/ChatMessageToolCallBlock.svelte';
import ChatMessageToolCallGroup from './ChatMessage/ChatMessageToolCall/ChatMessageToolCallGroup.svelte';
import { parseExecShellCommandMeta } from './ChatMessage/ChatMessageToolCall/parsers/exec-shell-command';
import { setToolCallElapsedContext } from './ChatMessage/ChatMessageToolCall/tool-call-elapsed';
import ChatMessageReasoningBlock from './ChatMessageReasoningBlock.svelte';
import { groupToolCallRuns, isToolCallStep } from '@ggml-org/llama-harness';
Expand All @@ -23,7 +22,8 @@
AgenticSection,
ChatMessageAgenticTimings,
ChatMessageAgenticTurnStats,
DatabaseMessage
DatabaseMessage,
PermissionAnswer
} from '#lib/types/index.ts';
import {
deriveAgenticSections,
Expand Down Expand Up @@ -86,9 +86,9 @@
pendingPermission && dismissedPermission !== pendingPermission ? pendingPermission : null
);

function handlePermission(decision: ToolPermissionDecision) {
function handlePermission(decision: ToolPermissionDecision, answer?: PermissionAnswer) {
dismissedPermission = pendingPermission;
agenticStore.resolvePermission(message.convId, decision);
agenticStore.resolvePermission(message.convId, decision, answer);
}

const pendingContinue = $derived(
Expand All @@ -115,17 +115,6 @@

const sections = $derived(deriveAgenticSections(message, toolMessages, [], isStreaming));

// The command the pending call would run, for the permission card: the gate
// names the call it is asking about, so an earlier call of the same tool
// whose result has not landed yet cannot be mistaken for it.
const pendingCommand = $derived.by(() => {
if (!visiblePermission) return undefined;

const pending = sections.find((section) => section.toolCallId === visiblePermission.toolCallId);

return pending ? parseExecShellCommandMeta(pending)?.command : undefined;
});

const currentlyExecutingToolCallId = $derived(
isStreaming ? agenticStore.getExecutingToolCallId(message.convId) : null
);
Expand Down Expand Up @@ -354,12 +343,13 @@
{/each}

{#if visiblePermission}
<ChatMessageActionCardPermissionRequest
command={pendingCommand}
onDecision={handlePermission}
serverLabel={visiblePermission.serverLabel}
toolName={visiblePermission.toolName}
/>
<!-- a new request gets a new card, so patterns edited for one never carry over -->
{#key visiblePermission}
<ChatMessageActionCardPermissionRequest
onDecision={handlePermission}
request={visiblePermission}
/>
{/key}
{/if}

{#if showContinue}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<script lang="ts">
import { Badge } from '@ggml-org/llama-ui/ui/badge';
import { Button } from '@ggml-org/llama-ui/ui/button';
import { X } from '@lucide/svelte';
import { PermissionEffect } from '#lib/enums/index.ts';
import { permissionsStore, toolsStore } from '#lib/stores/index.ts';
import type { PermissionRule } from '#lib/types/index.ts';

const EFFECT_LABELS: Record<PermissionEffect, string> = {
[PermissionEffect.ALLOW]: 'Allow',
[PermissionEffect.ASK]: 'Ask',
[PermissionEffect.DENY]: 'Deny'
};

// rules a checkbox above already shows: a tool allowed everywhere, for every call
let rules = $derived(
permissionsStore.rules.filter(
(rule) => rule.pattern || rule.cwd || rule.effect !== PermissionEffect.ALLOW
)
);

function toolName(rule: PermissionRule): string {
return (
toolsStore.allTools.find((entry) => entry.key === rule.tool)?.definition.function.name ??
rule.tool
);
}
</script>

<section class="space-y-2 pt-4">
<div class="space-y-1">
<h3 class="text-body-sm font-medium">Permission rules</h3>

<p class="text-caption text-muted-foreground">
Added when you allow a command or a tool in its prompt for good, everywhere or in one folder.
Deny wins over ask, and ask over allow. Rules spare prompts; what tools may do is still set by
llama-server.
</p>
</div>

{#if rules.length === 0}
<p class="py-3 text-center text-body-sm text-muted-foreground">No rules yet</p>
{:else}
<ul class="divide-y divide-border/50 rounded-lg border border-border/50">
{#each rules as rule (rule.id)}
<li class="flex items-center gap-2 px-3 py-2 text-body-sm">
<Badge
class="shrink-0"
variant={rule.effect === PermissionEffect.DENY ? 'destructive' : 'secondary'}
>
{EFFECT_LABELS[rule.effect]}
</Badge>

<span class="flex min-w-0 flex-1 flex-col">
<span class="truncate font-mono text-caption" title={rule.pattern ?? toolName(rule)}>
{rule.pattern ?? toolName(rule)}
</span>

<span class="truncate text-caption text-muted-foreground" title={rule.cwd}>
{rule.pattern ? `${toolName(rule)} · ` : ''}{rule.cwd ?? 'Everywhere'}
</span>
</span>

<Button
aria-label="Remove rule"
class="shrink-0"
onclick={() => permissionsStore.removeRule(rule.id)}
size="icon-sm"
variant="ghost"
>
<X class="size-icon-sm" />
</Button>
</li>
{/each}
</ul>
{/if}
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as Collapsible from '@ggml-org/llama-ui/ui/collapsible';
import * as Select from '@ggml-org/llama-ui/ui/select';
import { ChevronDown, ChevronRight } from '@lucide/svelte';
import { TruncatedText } from '#lib/components/app/index.ts';
import { SettingsChatPermissionRules, TruncatedText } from '#lib/components/app/index.ts';
import {
ICON_CLASS_DEFAULT,
SETTINGS_KEYS,
Expand Down Expand Up @@ -229,3 +229,5 @@
{/each}
</div>
{/if}

<SettingsChatPermissionRules />
5 changes: 5 additions & 0 deletions ui/apps/chat/src/lib/components/app/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ export { default as SettingsChatFields } from './SettingsChat/SettingsChatFields
* server favicons and permission management controls.
*/
export { default as SettingsChatToolsTab } from './SettingsChat/SettingsChatToolsTab.svelte';

/**
* **SettingsChatPermissionRules** - The kept permission rules, with a way to remove each
*/
export { default as SettingsChatPermissionRules } from './SettingsChat/SettingsChatPermissionRules.svelte';
1 change: 1 addition & 0 deletions ui/apps/chat/src/lib/constants/settings-keys.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const SETTINGS_KEYS = {
// Display
PASTE_LONG_TEXT_TO_FILE_LEN: 'pasteLongTextToFileLen',
PDF_AS_IMAGE: 'pdfAsImage',
PERMISSION_MODE: 'permissionMode',
// Performance
PRE_ENCODE_CONVERSATION: 'preEncodeConversation',
PRESENCE_PENALTY: 'presence_penalty',
Expand Down
19 changes: 18 additions & 1 deletion ui/apps/chat/src/lib/constants/settings.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import { VOICE_MICROPHONE_AUTO } from '@ggml-org/llama-voice';
import AudioLines from '@lucide/svelte/icons/audio-lines';
import Code from '@lucide/svelte/icons/code';
import Database from '@lucide/svelte/icons/database';
import Eye from '@lucide/svelte/icons/eye';
import FilePen from '@lucide/svelte/icons/file-pen';
import Funnel from '@lucide/svelte/icons/funnel';
import ListRestart from '@lucide/svelte/icons/list-restart';
import Mic from '@lucide/svelte/icons/mic';
import Monitor from '@lucide/svelte/icons/monitor';
import Moon from '@lucide/svelte/icons/moon';
import PencilRuler from '@lucide/svelte/icons/pencil-ruler';
import Puzzle from '@lucide/svelte/icons/puzzle';
import ShieldQuestion from '@lucide/svelte/icons/shield-question-mark';
import SlidersVertical from '@lucide/svelte/icons/sliders-vertical';
import Sun from '@lucide/svelte/icons/sun';
import TextCursorInput from '@lucide/svelte/icons/text-cursor-input';
import { SyncableParameterType, VoiceInputMode } from '#lib/enums/index.ts';
import Zap from '@lucide/svelte/icons/zap';
import { PermissionMode, SyncableParameterType, VoiceInputMode } from '#lib/enums/index.ts';
import { SettingsFieldType } from '#lib/enums/settings.enums.ts';
import { ColorMode } from '#lib/enums/ui.enums.ts';
import type {
Expand Down Expand Up @@ -542,6 +546,19 @@ export const SETTINGS_REGISTRY: SettingsSectionEntry[] = [
label: 'Agentic turns',
type: SettingsFieldType.INPUT
},
{
defaultValue: PermissionMode.ASK,
help: 'What happens to a tool call that no permission rule decides. Ask shows a prompt. Accept edits also writes and edits files inside the working folder without asking. Read-only refuses tools that change files or run commands, except read-only shell commands. Auto-approve runs everything no rule denies. Commands that could destroy the system are refused in every mode.',
key: SETTINGS_KEYS.PERMISSION_MODE,
label: 'Permission mode',
options: [
{ icon: ShieldQuestion, label: 'Ask', value: PermissionMode.ASK },
{ icon: FilePen, label: 'Accept edits', value: PermissionMode.ACCEPT_EDITS },
{ icon: Eye, label: 'Read-only', value: PermissionMode.READ_ONLY },
{ icon: Zap, label: 'Auto-approve', value: PermissionMode.AUTO }
],
type: SettingsFieldType.SELECT
},
{
defaultValue: DEFAULT_MCP_CONFIG.requestTimeoutSeconds,
extension: 'mcp',
Expand Down
Loading
Loading