generated from nhcarrigan/template
feat: add slash commands
This commit is contained in:
@@ -0,0 +1,139 @@
|
|||||||
|
import { get } from "svelte/store";
|
||||||
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
import { claudeStore } from "$lib/stores/claude";
|
||||||
|
import { characterState } from "$lib/stores/character";
|
||||||
|
import { setSkipNextGreeting } from "$lib/tauri";
|
||||||
|
|
||||||
|
export interface SlashCommand {
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
usage: string;
|
||||||
|
execute: (args: string) => Promise<void> | void;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startNewConversation(): Promise<void> {
|
||||||
|
const conversationId = get(claudeStore.activeConversationId);
|
||||||
|
if (!conversationId) {
|
||||||
|
claudeStore.addLine("error", "No active conversation");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const workingDir = await invoke<string>("get_working_directory", {
|
||||||
|
conversationId,
|
||||||
|
});
|
||||||
|
|
||||||
|
claudeStore.addLine("system", "Starting new conversation...");
|
||||||
|
characterState.setState("thinking");
|
||||||
|
|
||||||
|
await invoke("interrupt_claude", { conversationId });
|
||||||
|
|
||||||
|
claudeStore.clearTerminal();
|
||||||
|
|
||||||
|
setSkipNextGreeting(true);
|
||||||
|
|
||||||
|
await invoke("start_claude", {
|
||||||
|
conversationId,
|
||||||
|
options: {
|
||||||
|
working_dir: workingDir,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
claudeStore.addLine("system", "New conversation started!");
|
||||||
|
characterState.setState("idle");
|
||||||
|
} catch (error) {
|
||||||
|
claudeStore.addLine("error", `Failed to start new conversation: ${error}`);
|
||||||
|
characterState.setTemporaryState("error", 3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const slashCommands: SlashCommand[] = [
|
||||||
|
{
|
||||||
|
name: "clear",
|
||||||
|
description: "Clear the terminal display (keeps conversation context)",
|
||||||
|
usage: "/clear",
|
||||||
|
execute: () => {
|
||||||
|
claudeStore.clearTerminal();
|
||||||
|
claudeStore.addLine("system", "Terminal cleared");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "new",
|
||||||
|
description: "Start a fresh conversation (resets context)",
|
||||||
|
usage: "/new",
|
||||||
|
execute: startNewConversation,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "help",
|
||||||
|
description: "Show available slash commands",
|
||||||
|
usage: "/help",
|
||||||
|
execute: () => {
|
||||||
|
const helpText = slashCommands
|
||||||
|
.map((cmd) => ` ${cmd.usage.padEnd(12)} - ${cmd.description}`)
|
||||||
|
.join("\n");
|
||||||
|
claudeStore.addLine("system", `Available commands:\n${helpText}`);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "summarise",
|
||||||
|
description: "Get a summary of the entire conversation",
|
||||||
|
usage: "/summarise",
|
||||||
|
execute: async () => {
|
||||||
|
const conversationId = get(claudeStore.activeConversationId);
|
||||||
|
if (!conversationId) {
|
||||||
|
claudeStore.addLine("error", "No active conversation");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
claudeStore.addLine("system", "Requesting conversation summary...");
|
||||||
|
await invoke("send_prompt", {
|
||||||
|
conversationId,
|
||||||
|
message:
|
||||||
|
"Please provide a comprehensive summary of our entire conversation so far, including the key topics we've discussed, decisions made, and any important context.",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
claudeStore.addLine("error", `Failed to request summary: ${error}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function parseSlashCommand(input: string): {
|
||||||
|
command: SlashCommand | null;
|
||||||
|
args: string;
|
||||||
|
} {
|
||||||
|
const trimmed = input.trim();
|
||||||
|
|
||||||
|
if (!trimmed.startsWith("/")) {
|
||||||
|
return { command: null, args: "" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = trimmed.slice(1).split(/\s+/);
|
||||||
|
const commandName = parts[0]?.toLowerCase();
|
||||||
|
const args = parts.slice(1).join(" ");
|
||||||
|
|
||||||
|
const command = slashCommands.find((cmd) => cmd.name.toLowerCase() === commandName);
|
||||||
|
|
||||||
|
return { command: command || null, args };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getMatchingCommands(input: string): SlashCommand[] {
|
||||||
|
const trimmed = input.trim();
|
||||||
|
|
||||||
|
if (!trimmed.startsWith("/")) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const partial = trimmed.slice(1).toLowerCase();
|
||||||
|
|
||||||
|
if (partial === "") {
|
||||||
|
return slashCommands;
|
||||||
|
}
|
||||||
|
|
||||||
|
return slashCommands.filter((cmd) => cmd.name.toLowerCase().startsWith(partial));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isSlashCommand(input: string): boolean {
|
||||||
|
return input.trim().startsWith("/");
|
||||||
|
}
|
||||||
@@ -13,13 +13,23 @@
|
|||||||
clearHistoryRestore,
|
clearHistoryRestore,
|
||||||
} from "$lib/stores/historyRestore";
|
} from "$lib/stores/historyRestore";
|
||||||
import MessageModeSelector from "$lib/components/MessageModeSelector.svelte";
|
import MessageModeSelector from "$lib/components/MessageModeSelector.svelte";
|
||||||
|
import SlashCommandMenu from "$lib/components/SlashCommandMenu.svelte";
|
||||||
import { getCurrentMode } from "$lib/stores/messageMode";
|
import { getCurrentMode } from "$lib/stores/messageMode";
|
||||||
import { formatMessageWithMode } from "$lib/types/messageMode";
|
import { formatMessageWithMode } from "$lib/types/messageMode";
|
||||||
|
import {
|
||||||
|
parseSlashCommand,
|
||||||
|
getMatchingCommands,
|
||||||
|
isSlashCommand,
|
||||||
|
type SlashCommand,
|
||||||
|
} from "$lib/commands/slashCommands";
|
||||||
|
|
||||||
let inputValue = $state("");
|
let inputValue = $state("");
|
||||||
let isSubmitting = $state(false);
|
let isSubmitting = $state(false);
|
||||||
let isConnected = $state(false);
|
let isConnected = $state(false);
|
||||||
let isProcessing = $state(false);
|
let isProcessing = $state(false);
|
||||||
|
let showCommandMenu = $state(false);
|
||||||
|
let matchingCommands = $state<SlashCommand[]>([]);
|
||||||
|
let selectedCommandIndex = $state(0);
|
||||||
|
|
||||||
claudeStore.connectionStatus.subscribe((status) => {
|
claudeStore.connectionStatus.subscribe((status) => {
|
||||||
isConnected = status === "connected";
|
isConnected = status === "connected";
|
||||||
@@ -29,11 +39,56 @@
|
|||||||
isProcessing = processing;
|
isProcessing = processing;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function handleInputChange() {
|
||||||
|
if (isSlashCommand(inputValue)) {
|
||||||
|
matchingCommands = getMatchingCommands(inputValue);
|
||||||
|
showCommandMenu = matchingCommands.length > 0;
|
||||||
|
selectedCommandIndex = 0;
|
||||||
|
} else {
|
||||||
|
showCommandMenu = false;
|
||||||
|
matchingCommands = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectCommand(command: SlashCommand) {
|
||||||
|
inputValue = `/${command.name} `;
|
||||||
|
showCommandMenu = false;
|
||||||
|
matchingCommands = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function executeSlashCommand(): Promise<boolean> {
|
||||||
|
const { command, args } = parseSlashCommand(inputValue);
|
||||||
|
if (command) {
|
||||||
|
inputValue = "";
|
||||||
|
showCommandMenu = false;
|
||||||
|
matchingCommands = [];
|
||||||
|
await command.execute(args);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async function handleSubmit(event: Event) {
|
async function handleSubmit(event: Event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
const message = inputValue.trim();
|
const message = inputValue.trim();
|
||||||
if (!message || isSubmitting || !isConnected) return;
|
if (!message || isSubmitting) return;
|
||||||
|
|
||||||
|
// Check for slash commands first (these work even when disconnected)
|
||||||
|
if (isSlashCommand(message)) {
|
||||||
|
const wasCommand = await executeSlashCommand();
|
||||||
|
if (wasCommand) return;
|
||||||
|
// If it started with / but wasn't a valid command, show error
|
||||||
|
claudeStore.addLine(
|
||||||
|
"error",
|
||||||
|
`Unknown command: ${message.split(" ")[0]}. Type /help for available commands.`
|
||||||
|
);
|
||||||
|
inputValue = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regular messages require connection
|
||||||
|
if (!isConnected) return;
|
||||||
|
|
||||||
isSubmitting = true;
|
isSubmitting = true;
|
||||||
inputValue = "";
|
inputValue = "";
|
||||||
@@ -139,6 +194,34 @@ User: ${formattedMessage}`;
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleKeyDown(event: KeyboardEvent) {
|
function handleKeyDown(event: KeyboardEvent) {
|
||||||
|
// Handle command menu navigation
|
||||||
|
if (showCommandMenu && matchingCommands.length > 0) {
|
||||||
|
if (event.key === "ArrowDown") {
|
||||||
|
event.preventDefault();
|
||||||
|
selectedCommandIndex = (selectedCommandIndex + 1) % matchingCommands.length;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key === "ArrowUp") {
|
||||||
|
event.preventDefault();
|
||||||
|
selectedCommandIndex =
|
||||||
|
(selectedCommandIndex - 1 + matchingCommands.length) % matchingCommands.length;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key === "Tab") {
|
||||||
|
event.preventDefault();
|
||||||
|
const selected = matchingCommands[selectedCommandIndex];
|
||||||
|
if (selected) {
|
||||||
|
selectCommand(selected);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key === "Escape") {
|
||||||
|
event.preventDefault();
|
||||||
|
showCommandMenu = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (event.key === "Enter" && !event.shiftKey) {
|
if (event.key === "Enter" && !event.shiftKey) {
|
||||||
handleSubmit(event);
|
handleSubmit(event);
|
||||||
}
|
}
|
||||||
@@ -152,11 +235,19 @@ User: ${formattedMessage}`;
|
|||||||
|
|
||||||
<div class="input-row flex gap-3 items-end">
|
<div class="input-row flex gap-3 items-end">
|
||||||
<div class="flex-1 relative">
|
<div class="flex-1 relative">
|
||||||
|
<SlashCommandMenu
|
||||||
|
commands={matchingCommands}
|
||||||
|
selectedIndex={selectedCommandIndex}
|
||||||
|
onSelect={selectCommand}
|
||||||
|
/>
|
||||||
<textarea
|
<textarea
|
||||||
bind:value={inputValue}
|
bind:value={inputValue}
|
||||||
onkeydown={handleKeyDown}
|
onkeydown={handleKeyDown}
|
||||||
placeholder={isConnected ? "Ask Hikari anything..." : "Connect to Claude first..."}
|
oninput={handleInputChange}
|
||||||
disabled={!isConnected || isSubmitting}
|
placeholder={isConnected
|
||||||
|
? "Ask Hikari anything... (type / for commands)"
|
||||||
|
: "Connect to Claude first..."}
|
||||||
|
disabled={isSubmitting}
|
||||||
rows={1}
|
rows={1}
|
||||||
class="w-full px-4 py-3 bg-[var(--bg-secondary)] border border-[var(--border-color)]
|
class="w-full px-4 py-3 bg-[var(--bg-secondary)] border border-[var(--border-color)]
|
||||||
rounded-lg text-[var(--text-primary)] placeholder-gray-500 resize-none
|
rounded-lg text-[var(--text-primary)] placeholder-gray-500 resize-none
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import type { SlashCommand } from "$lib/commands/slashCommands";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
commands: SlashCommand[];
|
||||||
|
selectedIndex: number;
|
||||||
|
onSelect: (command: SlashCommand) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
let { commands, selectedIndex, onSelect }: Props = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if commands.length > 0}
|
||||||
|
<div class="slash-command-menu">
|
||||||
|
<div class="menu-header">Commands</div>
|
||||||
|
{#each commands as command, index (command.name)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="menu-item"
|
||||||
|
class:selected={index === selectedIndex}
|
||||||
|
onclick={() => onSelect(command)}
|
||||||
|
onmouseenter={() => (selectedIndex = index)}
|
||||||
|
>
|
||||||
|
<span class="command-name">/{command.name}</span>
|
||||||
|
<span class="command-description">{command.description}</span>
|
||||||
|
</button>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.slash-command-menu {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 100%;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: 8px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.2);
|
||||||
|
z-index: 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-header {
|
||||||
|
padding: 8px 12px;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
|
color: var(--text-tertiary);
|
||||||
|
background: var(--bg-tertiary);
|
||||||
|
border-bottom: 1px solid var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
width: 100%;
|
||||||
|
padding: 10px 12px;
|
||||||
|
text-align: left;
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.15s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-item:hover,
|
||||||
|
.menu-item.selected {
|
||||||
|
background: var(--bg-tertiary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.command-name {
|
||||||
|
font-family: monospace;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--accent-primary);
|
||||||
|
min-width: 80px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.command-description {
|
||||||
|
color: var(--text-secondary);
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Reference in New Issue
Block a user