Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
phase 5
Julius Marmingef20bbdf26767
diff --git a/.plans/16-frontend-contract-schema-validation-alignment.md b/.plans/16-frontend-contract-schema-validation-alignment.mdindex 5ead8949c..221c2f46f 100644--- a/.plans/16-frontend-contract-schema-validation-alignment.md+++ b/.plans/16-frontend-contract-schema-validation-alignment.md@@ -97,6 +97,9 @@ Align frontend input/output validation with `@t3tools/contracts` schemas so the## Phase 5: Script/Keybinding Validation Consistency+### Status+Complete+### Issue`ProjectScriptsControl` and `ChatView` rely mostly on local non-empty checks while sending keybinding data into a contract-validated endpoint (`server.upsertKeybinding`).diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 42c5ad5e5..1c528379f 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -92,6 +92,7 @@ import { CursorIcon, Icon } from "./Icons";import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";import { Badge } from "./ui/badge";import { Command, CommandInput, CommandItem, CommandList } from "./ui/command";+import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";import ProjectScriptsControl, { type NewProjectScriptInput } from "./ProjectScriptsControl";import {commandForProjectScript,@@ -814,21 +815,14 @@ export default function ChatView({ threadId }: ChatViewProps) {scripts: input.nextScripts,});- if (isElectron && input.keybinding) {- const keybindingUpdate = await api.server.upsertKeybinding({- key: input.keybinding,- command: input.keybindingCommand,- });- queryClient.setQueryData(- serverQueryKeys.config(),- (current: { cwd: string; keybindings: ResolvedKeybindingsConfig } | undefined) =>- current- ? { ...current, keybindings: keybindingUpdate.keybindings }- : {- cwd: input.projectCwd,- keybindings: keybindingUpdate.keybindings,- },- );+ const keybindingRule = decodeProjectScriptKeybindingRule({+ keybinding: input.keybinding,+ command: input.keybindingCommand,+ });++ if (isElectron && keybindingRule) {+ await api.server.upsertKeybinding(keybindingRule);+ await queryClient.invalidateQueries({ queryKey: serverQueryKeys.all });}},[queryClient],diff --git a/apps/web/src/components/ProjectScriptsControl.tsx b/apps/web/src/components/ProjectScriptsControl.tsxindex 2217a9098..13966a5f5 100644--- a/apps/web/src/components/ProjectScriptsControl.tsx+++ b/apps/web/src/components/ProjectScriptsControl.tsx@@ -16,7 +16,8 @@ import {} from "lucide-react";import React, { type FormEvent, type KeyboardEvent, useMemo, useState } from "react";-import { commandForProjectScript, primaryProjectScript } from "~/projectScripts";+import { keybindingValueForCommand, decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";+import { commandForProjectScript, nextProjectScriptId, primaryProjectScript } from "~/projectScripts";import { shortcutLabelForCommand } from "~/keybindings";import { isMacPlatform } from "~/lib/utils";import { Button } from "./ui/button";@@ -129,32 +130,6 @@ function keybindingFromEvent(event: KeyboardEvent<HTMLInputElement>): string | nreturn parts.join("+");}-function keybindingValueForCommand(- keybindings: ResolvedKeybindingsConfig,- command: string,-): string | null {- for (let index = keybindings.length - 1; index >= 0; index -= 1) {- const binding = keybindings[index];- if (!binding || binding.command !== command) continue;-- const parts: string[] = [];- if (binding.shortcut.modKey) parts.push("mod");- if (binding.shortcut.ctrlKey) parts.push("ctrl");- if (binding.shortcut.metaKey) parts.push("meta");- if (binding.shortcut.altKey) parts.push("alt");- if (binding.shortcut.shiftKey) parts.push("shift");- const keyToken =- binding.shortcut.key === " "- ? "space"- : binding.shortcut.key === "escape"- ? "esc"- : binding.shortcut.key;- parts.push(keyToken);- return parts.join("+");- }- return null;-}-export default function ProjectScriptsControl({scripts,keybindings,@@ -212,12 +187,18 @@ export default function ProjectScriptsControl({setValidationError(null);try {+ const scriptIdForValidation =+ editingScriptId ?? nextProjectScriptId(trimmedName, scripts.map((script) => script.id));+ const keybindingRule = decodeProjectScriptKeybindingRule({+ keybinding,+ command: commandForProjectScript(scriptIdForValidation),+ });const payload = {name: trimmedName,command: trimmedCommand,icon,runOnWorktreeCreate,- keybinding: keybinding.trim() || null,+ keybinding: keybindingRule?.key ?? null,} satisfies NewProjectScriptInput;if (editingScriptId) {await onUpdateScript(editingScriptId, payload);diff --git a/apps/web/src/lib/projectScriptKeybindings.test.ts b/apps/web/src/lib/projectScriptKeybindings.test.tsnew file mode 100644index 000000000..9db0fd529--- /dev/null+++ b/apps/web/src/lib/projectScriptKeybindings.test.ts@@ -0,0 +1,83 @@+import { MAX_KEYBINDING_VALUE_LENGTH, type KeybindingCommand } from "@t3tools/contracts";+import { describe, expect, it } from "vitest";++import { commandForProjectScript } from "../projectScripts";+import {+ decodeProjectScriptKeybindingRule,+ keybindingValueForCommand,+ PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE,+} from "./projectScriptKeybindings";++describe("projectScriptKeybindings", () => {+ it("decodes and trims valid keybinding rules", () => {+ const rule = decodeProjectScriptKeybindingRule({+ keybinding: " mod+k ",+ command: commandForProjectScript("lint"),+ });++ expect(rule).toEqual({+ key: "mod+k",+ command: "script.lint.run",+ });+ });++ it("returns null when keybinding is empty", () => {+ expect(+ decodeProjectScriptKeybindingRule({+ keybinding: " ",+ command: commandForProjectScript("lint"),+ }),+ ).toBeNull();+ });++ it("rejects invalid keybinding values", () => {+ expect(() =>+ decodeProjectScriptKeybindingRule({+ keybinding: "k".repeat(MAX_KEYBINDING_VALUE_LENGTH + 1),+ command: commandForProjectScript("lint"),+ }),+ ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);+ });++ it("rejects invalid commands", () => {+ expect(() =>+ decodeProjectScriptKeybindingRule({+ keybinding: "mod+k",+ command: "script.BAD.run" as KeybindingCommand,+ }),+ ).toThrowError(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);+ });++ it("reads latest matching keybinding value for a command", () => {+ const command = commandForProjectScript("test");+ const value = keybindingValueForCommand(+ [+ {+ command,+ shortcut: {+ key: "escape",+ metaKey: false,+ ctrlKey: false,+ shiftKey: false,+ altKey: false,+ modKey: true,+ },+ },+ {+ command,+ shortcut: {+ key: "k",+ metaKey: false,+ ctrlKey: false,+ shiftKey: true,+ altKey: false,+ modKey: true,+ },+ },+ ],+ command,+ );++ expect(value).toBe("mod+shift+k");+ });+});diff --git a/apps/web/src/lib/projectScriptKeybindings.ts b/apps/web/src/lib/projectScriptKeybindings.tsnew file mode 100644index 000000000..4ea80cf82--- /dev/null+++ b/apps/web/src/lib/projectScriptKeybindings.ts@@ -0,0 +1,59 @@+import {+ KeybindingRule as KeybindingRuleSchema,+ type KeybindingCommand,+ type KeybindingRule,+ type ResolvedKeybindingsConfig,+} from "@t3tools/contracts";+import { Schema } from "effect";++export const PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE = "Invalid keybinding.";++function normalizeProjectScriptKeybindingInput(+ keybinding: string | null | undefined,+): string | null {+ const trimmed = keybinding?.trim() ?? "";+ return trimmed.length > 0 ? trimmed : null;+}++export function decodeProjectScriptKeybindingRule(input: {+ keybinding: string | null | undefined;+ command: KeybindingCommand;+}): KeybindingRule | null {+ const normalizedKey = normalizeProjectScriptKeybindingInput(input.keybinding);+ if (!normalizedKey) return null;++ const decoded = Schema.decodeUnknownOption(KeybindingRuleSchema)({+ key: normalizedKey,+ command: input.command,+ });+ if (decoded._tag === "None") {+ throw new Error(PROJECT_SCRIPT_KEYBINDING_INVALID_MESSAGE);+ }+ return decoded.value;+}++export function keybindingValueForCommand(+ keybindings: ResolvedKeybindingsConfig,+ command: KeybindingCommand,+): string | null {+ for (let index = keybindings.length - 1; index >= 0; index -= 1) {+ const binding = keybindings[index];+ if (!binding || binding.command !== command) continue;++ const parts: string[] = [];+ if (binding.shortcut.modKey) parts.push("mod");+ if (binding.shortcut.ctrlKey) parts.push("ctrl");+ if (binding.shortcut.metaKey) parts.push("meta");+ if (binding.shortcut.altKey) parts.push("alt");+ if (binding.shortcut.shiftKey) parts.push("shift");+ const keyToken =+ binding.shortcut.key === " "+ ? "space"+ : binding.shortcut.key === "escape"+ ? "esc"+ : binding.shortcut.key;+ parts.push(keyToken);+ return parts.join("+");+ }+ return null;+}