Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3faa288b8 | ||
|
|
442bab63e8 | ||
|
|
191883e599 | ||
|
|
dca0b9fb33 | ||
|
|
4437965135 | ||
|
|
5f94bfd5fe | ||
|
|
718a682782 |
No files matched your search
@@ -91,6 +91,14 @@ describe("server keybindings", () => {
|
||||
when: "terminalFocus && (",
|
||||
}),
|
||||
);
|
||||
|
||||
assert.isNull(
|
||||
compileResolvedKeybindingRule({
|
||||
key: "mod+d",
|
||||
command: "terminal.split",
|
||||
when: `${"!".repeat(300)}terminalFocus`,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("upserts custom keybindings to ~/.t3/keybindings.json", () => {
|
||||
@@ -163,6 +171,31 @@ describe("server keybindings", () => {
|
||||
expect(fs.readFileSync(configPath, "utf8")).toBe("{ not-json");
|
||||
});
|
||||
|
||||
it("reports non-array config parse errors without duplicate prefix", () => {
|
||||
const fakeHome = makeTempDir("t3code-keybindings-non-array-");
|
||||
const configDir = path.join(fakeHome, ".t3");
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
const configPath = path.join(configDir, "keybindings.json");
|
||||
fs.writeFileSync(configPath, JSON.stringify({ key: "mod+j", command: "terminal.toggle" }), "utf8");
|
||||
vi.spyOn(os, "homedir").mockReturnValue(fakeHome);
|
||||
|
||||
expect(() =>
|
||||
upsertKeybindingRule(logger, {
|
||||
key: "mod+shift+r",
|
||||
command: "script.run-tests.run",
|
||||
}),
|
||||
).toThrow(new RegExp(`Unable to parse keybindings config at ${configPath}: expected JSON array`));
|
||||
|
||||
expect(() =>
|
||||
upsertKeybindingRule(logger, {
|
||||
key: "mod+shift+r",
|
||||
command: "script.run-tests.run",
|
||||
}),
|
||||
).not.toThrow(
|
||||
new RegExp(`Unable to parse keybindings config at ${configPath}: Unable to parse keybindings config at`),
|
||||
);
|
||||
});
|
||||
|
||||
it("cleans up temp files when atomic keybinding write fails", () => {
|
||||
const fakeHome = makeTempDir("t3code-keybindings-atomic-");
|
||||
const configDir = path.join(fakeHome, ".t3");
|
||||
|
||||
@@ -24,6 +24,8 @@ type WhenToken =
|
||||
| { type: "lparen" }
|
||||
| { type: "rparen" };
|
||||
|
||||
const MAX_WHEN_EXPRESSION_DEPTH = 256;
|
||||
|
||||
export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
|
||||
{ key: "mod+j", command: "terminal.toggle" },
|
||||
{ key: "mod+d", command: "terminal.split", when: "terminalFocus" },
|
||||
@@ -159,7 +161,10 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
if (!tokens || tokens.length === 0) return null;
|
||||
let index = 0;
|
||||
|
||||
const parsePrimary = (): KeybindingWhenNode | null => {
|
||||
const parsePrimary = (depth: number): KeybindingWhenNode | null => {
|
||||
if (depth > MAX_WHEN_EXPRESSION_DEPTH) {
|
||||
return null;
|
||||
}
|
||||
const token = tokens[index];
|
||||
if (!token) return null;
|
||||
|
||||
@@ -170,7 +175,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
|
||||
if (token.type === "lparen") {
|
||||
index += 1;
|
||||
const expressionNode = parseOr();
|
||||
const expressionNode = parseOr(depth + 1);
|
||||
const closeToken = tokens[index];
|
||||
if (!expressionNode || !closeToken || closeToken.type !== "rparen") {
|
||||
return null;
|
||||
@@ -182,24 +187,34 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return null;
|
||||
};
|
||||
|
||||
const parseUnary = (): KeybindingWhenNode | null => {
|
||||
const token = tokens[index];
|
||||
if (token?.type === "not") {
|
||||
const parseUnary = (depth: number): KeybindingWhenNode | null => {
|
||||
let notCount = 0;
|
||||
while (tokens[index]?.type === "not") {
|
||||
index += 1;
|
||||
const node = parseUnary();
|
||||
if (!node) return null;
|
||||
return { type: "not", node };
|
||||
notCount += 1;
|
||||
if (notCount > MAX_WHEN_EXPRESSION_DEPTH) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return parsePrimary();
|
||||
|
||||
let node = parsePrimary(depth);
|
||||
if (!node) return null;
|
||||
|
||||
while (notCount > 0) {
|
||||
node = { type: "not", node };
|
||||
notCount -= 1;
|
||||
}
|
||||
|
||||
return node;
|
||||
};
|
||||
|
||||
const parseAnd = (): KeybindingWhenNode | null => {
|
||||
let left = parseUnary();
|
||||
const parseAnd = (depth: number): KeybindingWhenNode | null => {
|
||||
let left = parseUnary(depth);
|
||||
if (!left) return null;
|
||||
|
||||
while (tokens[index]?.type === "and") {
|
||||
index += 1;
|
||||
const right = parseUnary();
|
||||
const right = parseUnary(depth);
|
||||
if (!right) return null;
|
||||
left = { type: "and", left, right };
|
||||
}
|
||||
@@ -207,13 +222,13 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return left;
|
||||
};
|
||||
|
||||
const parseOr = (): KeybindingWhenNode | null => {
|
||||
let left = parseAnd();
|
||||
const parseOr = (depth: number): KeybindingWhenNode | null => {
|
||||
let left = parseAnd(depth);
|
||||
if (!left) return null;
|
||||
|
||||
while (tokens[index]?.type === "or") {
|
||||
index += 1;
|
||||
const right = parseAnd();
|
||||
const right = parseAnd(depth);
|
||||
if (!right) return null;
|
||||
left = { type: "or", left, right };
|
||||
}
|
||||
@@ -221,7 +236,7 @@ export function parseKeybindingWhenExpression(expression: string): KeybindingWhe
|
||||
return left;
|
||||
};
|
||||
|
||||
const ast = parseOr();
|
||||
const ast = parseOr(0);
|
||||
if (!ast || index !== tokens.length) return null;
|
||||
return ast;
|
||||
}
|
||||
@@ -277,6 +292,12 @@ const DEFAULT_RESOLVED_KEYBINDINGS = compileDefaultKeybindings();
|
||||
const KEYBINDINGS_CONFIG_PATH = path.join(".t3", "keybindings.json");
|
||||
const MAX_KEYBINDINGS = 256;
|
||||
|
||||
class KeybindingsConfigParseError extends Error {
|
||||
constructor(configPath: string, detail: string, options?: { cause?: unknown }) {
|
||||
super(`Unable to parse keybindings config at ${configPath}: ${detail}`, options);
|
||||
}
|
||||
}
|
||||
|
||||
function resolveKeybindingsConfigPath(): string {
|
||||
return path.join(os.homedir(), KEYBINDINGS_CONFIG_PATH);
|
||||
}
|
||||
@@ -293,7 +314,7 @@ function loadCustomKeybindingsConfig(
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
if (!Array.isArray(parsed)) {
|
||||
if (options?.throwOnUnreadableConfig) {
|
||||
throw new Error(`Unable to parse keybindings config at ${configPath}: expected JSON array`);
|
||||
throw new KeybindingsConfigParseError(configPath, "expected JSON array");
|
||||
}
|
||||
logger.warn("ignoring keybindings config with unsupported format; expected array", {
|
||||
path: configPath,
|
||||
@@ -328,10 +349,12 @@ function loadCustomKeybindingsConfig(
|
||||
return [];
|
||||
}
|
||||
if (options?.throwOnUnreadableConfig) {
|
||||
throw new Error(
|
||||
`Unable to parse keybindings config at ${configPath}: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
if (error instanceof KeybindingsConfigParseError) {
|
||||
throw error;
|
||||
}
|
||||
throw new KeybindingsConfigParseError(
|
||||
configPath,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import {
|
||||
projectRemoveInputSchema,
|
||||
projectScriptsSchema,
|
||||
projectUpdateScriptsInputSchema,
|
||||
normalizeProjectScripts,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
function cloneScripts(scripts: readonly ProjectScript[]): ProjectScript[] {
|
||||
@@ -131,7 +130,7 @@ export class ProjectRegistry {
|
||||
throw new Error(`Project not found: ${input.id}`);
|
||||
}
|
||||
|
||||
const nextScripts = normalizeProjectScripts(projectScriptsSchema.parse(input.scripts));
|
||||
const nextScripts = dedupeScriptsById(projectScriptsSchema.parse(input.scripts));
|
||||
const nextUpdatedAt = new Date().toISOString();
|
||||
const nextProject: ProjectRecord = {
|
||||
...this.projects[index]!,
|
||||
@@ -168,7 +167,7 @@ export class ProjectRegistry {
|
||||
cwd: normalizedCwd,
|
||||
name:
|
||||
project.name.trim().length > 0 ? project.name.trim() : inferProjectName(normalizedCwd),
|
||||
scripts: normalizeProjectScripts(project.scripts),
|
||||
scripts: dedupeScriptsById(project.scripts),
|
||||
};
|
||||
deduped.set(normalizedCwd, normalizedProject);
|
||||
normalizedProjects.push(normalizedProject);
|
||||
@@ -233,3 +232,35 @@ export class ProjectRegistry {
|
||||
fs.renameSync(tempFile, this.filePath);
|
||||
}
|
||||
}
|
||||
|
||||
function dedupeScriptsById(scripts: readonly ProjectScript[]): ProjectScript[] {
|
||||
const seenIds = new Set<string>();
|
||||
const deduped: ProjectScript[] = [];
|
||||
for (const script of scripts) {
|
||||
if (seenIds.has(script.id)) continue;
|
||||
seenIds.add(script.id);
|
||||
deduped.push({
|
||||
id: script.id,
|
||||
name: script.name,
|
||||
command: script.command,
|
||||
icon: script.icon,
|
||||
runOnWorktreeCreate: script.runOnWorktreeCreate,
|
||||
});
|
||||
}
|
||||
|
||||
let setupAssigned = false;
|
||||
const normalized: ProjectScript[] = [];
|
||||
for (const script of deduped) {
|
||||
if (!script.runOnWorktreeCreate) {
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
if (!setupAssigned) {
|
||||
setupAssigned = true;
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
normalized.push({ ...script, runOnWorktreeCreate: false });
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
@@ -301,8 +301,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
|
||||
exitCode: null,
|
||||
exitSignal: null,
|
||||
updatedAt: new Date().toISOString(),
|
||||
cols: input.cols,
|
||||
rows: input.rows,
|
||||
cols: input.cols ?? 80,
|
||||
rows: input.rows ?? 24,
|
||||
process: null,
|
||||
unsubscribeData: null,
|
||||
unsubscribeExit: null,
|
||||
@@ -328,10 +328,12 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
|
||||
return this.snapshot(existing);
|
||||
}
|
||||
|
||||
if (existing.cols !== input.cols || existing.rows !== input.rows) {
|
||||
existing.cols = input.cols;
|
||||
existing.rows = input.rows;
|
||||
existing.process.resize(input.cols, input.rows);
|
||||
const effectiveCols = input.cols ?? existing.cols;
|
||||
const effectiveRows = input.rows ?? existing.rows;
|
||||
if (existing.cols !== effectiveCols || existing.rows !== effectiveRows) {
|
||||
existing.cols = effectiveCols;
|
||||
existing.rows = effectiveRows;
|
||||
existing.process.resize(effectiveCols, effectiveRows);
|
||||
existing.updatedAt = new Date().toISOString();
|
||||
}
|
||||
|
||||
@@ -398,8 +400,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
|
||||
exitCode: null,
|
||||
exitSignal: null,
|
||||
updatedAt: new Date().toISOString(),
|
||||
cols: input.cols,
|
||||
rows: input.rows,
|
||||
cols: input.cols ?? 80,
|
||||
rows: input.rows ?? 24,
|
||||
process: null,
|
||||
unsubscribeData: null,
|
||||
unsubscribeExit: null,
|
||||
@@ -469,8 +471,8 @@ export class TerminalManager extends EventEmitter<TerminalManagerEvents> {
|
||||
|
||||
session.status = "starting";
|
||||
session.cwd = input.cwd;
|
||||
session.cols = input.cols;
|
||||
session.rows = input.rows;
|
||||
session.cols = input.cols ?? session.cols;
|
||||
session.rows = input.rows ?? session.rows;
|
||||
session.exitCode = null;
|
||||
session.exitSignal = null;
|
||||
session.hasRunningSubprocess = false;
|
||||
|
||||
@@ -300,9 +300,7 @@ const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
|
||||
<Command
|
||||
mode="none"
|
||||
onItemHighlighted={(highlightedValue) => {
|
||||
props.onHighlightedItemChange(
|
||||
typeof highlightedValue === "string" ? highlightedValue : null,
|
||||
);
|
||||
props.onHighlightedItemChange(typeof highlightedValue === "string" ? highlightedValue : null);
|
||||
}}
|
||||
>
|
||||
<div className="relative overflow-hidden rounded-xl border border-border/80 bg-popover/96 shadow-lg/8 backdrop-blur-xs">
|
||||
@@ -672,7 +670,9 @@ export default function ChatView() {
|
||||
});
|
||||
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.cwd;
|
||||
const baseTerminalId =
|
||||
activeThread.activeTerminalId || activeThread.terminalIds[0] || DEFAULT_THREAD_TERMINAL_ID;
|
||||
activeThread.activeTerminalId ||
|
||||
activeThread.terminalIds[0] ||
|
||||
DEFAULT_THREAD_TERMINAL_ID;
|
||||
const isBaseTerminalBusy = activeThread.runningTerminalIds.includes(baseTerminalId);
|
||||
const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;
|
||||
const shouldCreateNewTerminal =
|
||||
@@ -721,8 +721,9 @@ export default function ChatView() {
|
||||
threadId: activeThreadId,
|
||||
terminalId: targetTerminalId,
|
||||
cwd: targetCwd,
|
||||
cols: SCRIPT_TERMINAL_COLS,
|
||||
rows: SCRIPT_TERMINAL_ROWS,
|
||||
...(shouldCreateNewTerminal
|
||||
? { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS }
|
||||
: {}),
|
||||
});
|
||||
await api.terminal.write({
|
||||
threadId: activeThreadId,
|
||||
@@ -740,25 +741,34 @@ export default function ChatView() {
|
||||
[activeProject, activeThread, activeThreadId, api, dispatch, gitCwd],
|
||||
);
|
||||
const persistProjectScripts = useCallback(
|
||||
async (
|
||||
project: { id: string; cwd: string; scripts: ProjectScript[] },
|
||||
nextScripts: ProjectScript[],
|
||||
keybinding: string | null,
|
||||
keybindingCommand: string,
|
||||
) => {
|
||||
async (input: {
|
||||
projectId: string;
|
||||
projectCwd: string;
|
||||
previousScripts: ProjectScript[];
|
||||
nextScripts: ProjectScript[];
|
||||
keybinding?: string | null;
|
||||
keybindingCommand: string;
|
||||
}) => {
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: input.projectId,
|
||||
scripts: input.nextScripts,
|
||||
});
|
||||
|
||||
if (!isElectron || !api) return;
|
||||
|
||||
let scriptsPersisted = false;
|
||||
try {
|
||||
const updated = await api.projects.updateScripts({
|
||||
id: project.id,
|
||||
scripts: nextScripts,
|
||||
id: input.projectId,
|
||||
scripts: input.nextScripts,
|
||||
});
|
||||
scriptsPersisted = true;
|
||||
|
||||
if (keybinding) {
|
||||
if (input.keybinding) {
|
||||
const keybindingUpdate = await api.server.upsertKeybinding({
|
||||
key: keybinding,
|
||||
command: keybindingCommand,
|
||||
key: input.keybinding,
|
||||
command: input.keybindingCommand,
|
||||
});
|
||||
queryClient.setQueryData(
|
||||
serverQueryKeys.config(),
|
||||
@@ -766,7 +776,7 @@ export default function ChatView() {
|
||||
current
|
||||
? { ...current, keybindings: keybindingUpdate.keybindings }
|
||||
: {
|
||||
cwd: project.cwd,
|
||||
cwd: input.projectCwd,
|
||||
keybindings: keybindingUpdate.keybindings,
|
||||
},
|
||||
);
|
||||
@@ -781,15 +791,15 @@ export default function ChatView() {
|
||||
if (scriptsPersisted) {
|
||||
await api.projects
|
||||
.updateScripts({
|
||||
id: project.id,
|
||||
scripts: project.scripts,
|
||||
id: input.projectId,
|
||||
scripts: input.previousScripts,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
projectId: project.id,
|
||||
scripts: project.scripts,
|
||||
projectId: input.projectId,
|
||||
scripts: input.previousScripts,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@@ -819,20 +829,16 @@ export default function ChatView() {
|
||||
]
|
||||
: [...activeProject.scripts, nextScript];
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
await persistProjectScripts({
|
||||
projectId: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
});
|
||||
|
||||
await persistProjectScripts(
|
||||
activeProject,
|
||||
projectCwd: activeProject.cwd,
|
||||
previousScripts: activeProject.scripts,
|
||||
nextScripts,
|
||||
input.keybinding,
|
||||
commandForProjectScript(nextId),
|
||||
);
|
||||
keybinding: input.keybinding,
|
||||
keybindingCommand: commandForProjectScript(nextId),
|
||||
});
|
||||
},
|
||||
[activeProject, dispatch, persistProjectScripts],
|
||||
[activeProject, persistProjectScripts],
|
||||
);
|
||||
const updateProjectScript = useCallback(
|
||||
async (scriptId: string, input: NewProjectScriptInput) => {
|
||||
@@ -857,20 +863,16 @@ export default function ChatView() {
|
||||
: script,
|
||||
);
|
||||
|
||||
dispatch({
|
||||
type: "SET_PROJECT_SCRIPTS",
|
||||
await persistProjectScripts({
|
||||
projectId: activeProject.id,
|
||||
scripts: nextScripts,
|
||||
});
|
||||
|
||||
await persistProjectScripts(
|
||||
activeProject,
|
||||
projectCwd: activeProject.cwd,
|
||||
previousScripts: activeProject.scripts,
|
||||
nextScripts,
|
||||
input.keybinding,
|
||||
commandForProjectScript(scriptId),
|
||||
);
|
||||
keybinding: input.keybinding,
|
||||
keybindingCommand: commandForProjectScript(scriptId),
|
||||
});
|
||||
},
|
||||
[activeProject, dispatch, persistProjectScripts],
|
||||
[activeProject, persistProjectScripts],
|
||||
);
|
||||
|
||||
const handleRuntimeModeChange = async (mode: "approval-required" | "full-access") => {
|
||||
|
||||
@@ -122,6 +122,9 @@ function keybindingFromEvent(event: KeyboardEvent<HTMLInputElement>): string | n
|
||||
}
|
||||
if (event.altKey) parts.push("alt");
|
||||
if (event.shiftKey) parts.push("shift");
|
||||
if (parts.length === 0) {
|
||||
return null;
|
||||
}
|
||||
parts.push(keyToken);
|
||||
return parts.join("+");
|
||||
}
|
||||
@@ -292,7 +295,7 @@ export default function ProjectScriptsControl({
|
||||
>
|
||||
<ScriptIcon icon={script.icon} className="size-4" />
|
||||
<span className="truncate">
|
||||
{script.runOnWorktreeCreate ? `${script.name}` : script.name}
|
||||
{script.runOnWorktreeCreate ? `${script.name} (setup)` : script.name}
|
||||
</span>
|
||||
<span className="relative ms-auto flex h-6 min-w-6 items-center justify-end">
|
||||
{shortcutLabel && (
|
||||
|
||||
@@ -25,20 +25,12 @@ describe("projectScripts helpers", () => {
|
||||
});
|
||||
|
||||
it("injects environment variables for posix shells", () => {
|
||||
const command = injectEnvIntoShellCommand(
|
||||
"bun install",
|
||||
{ T3CODE_PROJECT_ROOT: "/tmp/project" },
|
||||
"MacIntel",
|
||||
);
|
||||
const command = injectEnvIntoShellCommand("bun install", { T3CODE_PROJECT_ROOT: "/tmp/project" }, "MacIntel");
|
||||
expect(command).toBe("env T3CODE_PROJECT_ROOT='/tmp/project' bun install");
|
||||
});
|
||||
|
||||
it("injects environment variables for windows shells", () => {
|
||||
const command = injectEnvIntoShellCommand(
|
||||
"bun install",
|
||||
{ T3CODE_PROJECT_ROOT: "C:\\\\repo path" },
|
||||
"Win32",
|
||||
);
|
||||
const command = injectEnvIntoShellCommand("bun install", { T3CODE_PROJECT_ROOT: "C:\\\\repo path" }, "Win32");
|
||||
expect(command).toBe('set "T3CODE_PROJECT_ROOT=C:\\\\repo path" && bun install');
|
||||
});
|
||||
|
||||
@@ -48,7 +40,7 @@ describe("projectScripts helpers", () => {
|
||||
{ T3CODE_PROJECT_ROOT: '%USERPROFILE%^"repo"' },
|
||||
"Win32",
|
||||
);
|
||||
expect(command).toBe('set "T3CODE_PROJECT_ROOT=%USERPROFILE%^^""repo""" && bun install');
|
||||
expect(command).toBe('set "T3CODE_PROJECT_ROOT=^%USERPROFILE^%^^""repo""" && bun install');
|
||||
});
|
||||
|
||||
it("resolves primary and setup scripts", () => {
|
||||
|
||||
@@ -48,6 +48,7 @@ export function nextProjectScriptId(name: string, existingIds: Iterable<string>)
|
||||
suffix += 1;
|
||||
}
|
||||
|
||||
// This last-resort fallback only triggers after exhausting thousands of suffixes.
|
||||
return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH);
|
||||
}
|
||||
|
||||
@@ -56,11 +57,7 @@ function shellEscapePosix(value: string): string {
|
||||
}
|
||||
|
||||
function shellEscapeWindowsSetValue(value: string): string {
|
||||
// Only escape ^ and " for interactive cmd.exe `set "K=V"` commands.
|
||||
// Do NOT escape % → %% here: that substitution only works in batch files.
|
||||
// In an interactive cmd.exe session (which is what terminal.write targets),
|
||||
// %% stays as literal %% and corrupts the value.
|
||||
return value.replace(/\^/g, "^^").replace(/"/g, '""');
|
||||
return value.replace(/\^/g, "^^").replace(/%/g, "^%").replace(/"/g, '""');
|
||||
}
|
||||
|
||||
export function injectEnvIntoShellCommand(
|
||||
@@ -98,9 +95,7 @@ interface ProjectScriptRuntimeEnvInput {
|
||||
extraEnv?: Record<string, string>;
|
||||
}
|
||||
|
||||
export function projectScriptRuntimeEnv(
|
||||
input: ProjectScriptRuntimeEnvInput,
|
||||
): Record<string, string> {
|
||||
export function projectScriptRuntimeEnv(input: ProjectScriptRuntimeEnvInput): Record<string, string> {
|
||||
const env: Record<string, string> = {
|
||||
T3CODE_PROJECT_ROOT: input.project.cwd,
|
||||
T3CODE_PROJECT_ID: input.project.id,
|
||||
|
||||
+41
-8
@@ -8,12 +8,7 @@ import {
|
||||
useReducer,
|
||||
} from "react";
|
||||
|
||||
import {
|
||||
type ProviderEvent,
|
||||
type ProviderSession,
|
||||
type TerminalEvent,
|
||||
normalizeProjectScripts,
|
||||
} from "@t3tools/contracts";
|
||||
import type { ProviderEvent, ProviderSession, TerminalEvent } from "@t3tools/contracts";
|
||||
import { resolveModelSlug } from "./model-logic";
|
||||
import { hydratePersistedState, toPersistedState } from "./persistenceSchema";
|
||||
import { applyEventToMessages, asObject, asString, evolveSession } from "./session-logic";
|
||||
@@ -170,6 +165,38 @@ function normalizeRunningTerminalIds(
|
||||
.filter((id) => id.length > 0 && validTerminalIdSet.has(id));
|
||||
}
|
||||
|
||||
function normalizeProjectScripts(scripts: ProjectScript[]): ProjectScript[] {
|
||||
const deduped: ProjectScript[] = [];
|
||||
const seenIds = new Set<string>();
|
||||
for (const script of scripts) {
|
||||
const id = script.id.trim();
|
||||
if (id.length === 0 || seenIds.has(id)) continue;
|
||||
seenIds.add(id);
|
||||
deduped.push({
|
||||
...script,
|
||||
id,
|
||||
name: script.name.trim(),
|
||||
command: script.command.trim(),
|
||||
});
|
||||
}
|
||||
|
||||
let setupAssigned = false;
|
||||
const normalized: ProjectScript[] = [];
|
||||
for (const script of deduped) {
|
||||
if (!script.runOnWorktreeCreate) {
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
if (!setupAssigned) {
|
||||
setupAssigned = true;
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
normalized.push({ ...script, runOnWorktreeCreate: false });
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeTerminalGroupIds(terminalIds: string[]): string[] {
|
||||
return [...new Set(terminalIds.map((id) => id.trim()).filter((id) => id.length > 0))];
|
||||
}
|
||||
@@ -577,7 +604,10 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
threads: updateThread(state.threads, action.threadId, (thread) => {
|
||||
const normalizedThread = normalizeThreadTerminals(thread);
|
||||
const isNewTerminal = !normalizedThread.terminalIds.includes(action.terminalId);
|
||||
if (isNewTerminal && normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
|
||||
if (
|
||||
isNewTerminal &&
|
||||
normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT
|
||||
) {
|
||||
return normalizedThread;
|
||||
}
|
||||
const terminalIds = normalizedThread.terminalIds.includes(action.terminalId)
|
||||
@@ -647,7 +677,10 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
threads: updateThread(state.threads, action.threadId, (thread) => {
|
||||
const normalizedThread = normalizeThreadTerminals(thread);
|
||||
const isNewTerminal = !normalizedThread.terminalIds.includes(action.terminalId);
|
||||
if (isNewTerminal && normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
|
||||
if (
|
||||
isNewTerminal &&
|
||||
normalizedThread.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT
|
||||
) {
|
||||
return normalizedThread;
|
||||
}
|
||||
const terminalIds = normalizedThread.terminalIds.includes(action.terminalId)
|
||||
|
||||
@@ -95,40 +95,3 @@ export type ProjectEntry = z.infer<typeof projectEntrySchema>;
|
||||
export type ProjectSearchEntriesResult = z.infer<typeof projectSearchEntriesResultSchema>;
|
||||
export type ProjectUpdateScriptsInput = z.input<typeof projectUpdateScriptsInputSchema>;
|
||||
export type ProjectUpdateScriptsResult = z.infer<typeof projectUpdateScriptsResultSchema>;
|
||||
|
||||
/**
|
||||
* Deduplicate scripts by ID (first occurrence wins), trim string fields,
|
||||
* and enforce at most one script with `runOnWorktreeCreate: true`.
|
||||
*/
|
||||
export function normalizeProjectScripts(scripts: readonly ProjectScript[]): ProjectScript[] {
|
||||
const seenIds = new Set<string>();
|
||||
const deduped: ProjectScript[] = [];
|
||||
for (const script of scripts) {
|
||||
const id = script.id.trim();
|
||||
if (id.length === 0 || seenIds.has(id)) continue;
|
||||
seenIds.add(id);
|
||||
deduped.push({
|
||||
id,
|
||||
name: script.name.trim(),
|
||||
command: script.command.trim(),
|
||||
icon: script.icon,
|
||||
runOnWorktreeCreate: script.runOnWorktreeCreate,
|
||||
});
|
||||
}
|
||||
|
||||
let setupAssigned = false;
|
||||
const normalized: ProjectScript[] = [];
|
||||
for (const script of deduped) {
|
||||
if (!script.runOnWorktreeCreate) {
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
if (!setupAssigned) {
|
||||
setupAssigned = true;
|
||||
normalized.push(script);
|
||||
continue;
|
||||
}
|
||||
normalized.push({ ...script, runOnWorktreeCreate: false });
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
@@ -16,8 +16,8 @@ export const terminalSessionInputSchema = terminalThreadInputSchema.extend({
|
||||
|
||||
export const terminalOpenInputSchema = terminalSessionInputSchema.extend({
|
||||
cwd: z.string().trim().min(1),
|
||||
cols: terminalColsSchema,
|
||||
rows: terminalRowsSchema,
|
||||
cols: terminalColsSchema.optional(),
|
||||
rows: terminalRowsSchema.optional(),
|
||||
});
|
||||
|
||||
export const terminalWriteInputSchema = terminalSessionInputSchema.extend({
|
||||
|
||||
Reference in new issue
Block a user