Skip to content

Commit fa3491f

Browse files
author
saurav-shakya
committed
fix(tui): keep mistyped leading slash commands blocked
Only fall through to submit when the unmatched slash follows other text, so a typo like `/zzzz` at the start of the prompt is not sent to the model as a message.
1 parent c89ebd8 commit fa3491f

2 files changed

Lines changed: 55 additions & 48 deletions

File tree

‎packages/tui/src/component/prompt/autocomplete.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,9 @@ export function Autocomplete(props: {
707707
title: "Select autocomplete item",
708708
group: "Autocomplete",
709709
run() {
710-
// A slash with no matching command is just text (e.g. a trailing path), so let the prompt submit it.
711-
if (store.visible === "command" && !options().length) {
710+
// A slash after other text with no matching command is just text (e.g. a trailing path), so let the
711+
// prompt submit it. A mistyped command at the start of the prompt stays blocked.
712+
if (store.visible === "command" && !options().length && store.index > 0) {
712713
hide()
713714
return false
714715
}

‎packages/tui/test/app-lifecycle.test.tsx‎

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,54 +1344,60 @@ test("ctrl+c dismisses autocomplete and shell mode before exiting", async () =>
13441344
expect(setup.renderer.isDestroyed).toBe(false)
13451345
})
13461346

1347-
test("enter submits a prompt ending in a path when no slash command matches", async () => {
1348-
const location = { directory, project: { id: "project", directory, canonical: directory } }
1349-
const submitted = Promise.withResolvers<unknown>()
1350-
let session: unknown
1351-
await using setup = await createAppFixture({
1352-
config: { animations: false, tabs: { mode: "off" } },
1353-
fetch: async (url, request) => {
1354-
if (url.pathname === "/api/location") return json(location)
1355-
if (url.pathname === "/api/agent")
1356-
return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] })
1357-
if (url.pathname === "/api/model")
1358-
return json({ location, data: [{ id: "model", providerID: "provider", name: "Remote Model", variants: [] }] })
1359-
if (url.pathname === "/api/provider") return json({ location, data: [{ id: "provider", name: "Provider" }] })
1360-
if (url.pathname === "/api/session" && request.method === "POST") {
1361-
session = {
1362-
...(await request.json()),
1363-
projectID: "project",
1364-
cost: 0,
1365-
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
1366-
time: { created: 0, updated: 0 },
1347+
test.each([
1348+
["submits a prompt ending in a path", "list files in /tmp", true],
1349+
["keeps a mistyped leading command blocked", "/zzzz", false],
1350+
] as const)(
1351+
"enter %s when no slash command matches",
1352+
async (_, text, submits) => {
1353+
const location = { directory, project: { id: "project", directory, canonical: directory } }
1354+
const submitted = Promise.withResolvers<unknown>()
1355+
let session: unknown
1356+
await using setup = await createAppFixture({
1357+
config: { animations: false, tabs: { mode: "off" } },
1358+
fetch: async (url, request) => {
1359+
if (url.pathname === "/api/location") return json(location)
1360+
if (url.pathname === "/api/agent")
1361+
return json({ location, data: [{ id: "build", mode: "primary", hidden: false, permissions: [] }] })
1362+
if (url.pathname === "/api/model")
1363+
return json({ location, data: [{ id: "model", providerID: "provider", name: "Remote Model", variants: [] }] })
1364+
if (url.pathname === "/api/provider") return json({ location, data: [{ id: "provider", name: "Provider" }] })
1365+
if (url.pathname === "/api/session" && request.method === "POST") {
1366+
session = {
1367+
...(await request.json()),
1368+
projectID: "project",
1369+
cost: 0,
1370+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
1371+
time: { created: 0, updated: 0 },
1372+
}
1373+
return json({ data: session })
13671374
}
1368-
return json({ data: session })
1369-
}
1370-
if (/^\/api\/session\/[^/]+\/prompt$/.test(url.pathname)) {
1371-
submitted.resolve(await request.json())
1372-
return json({ data: {} })
1373-
}
1374-
if (/^\/api\/session\/[^/]+\/(message|inbox|permission)$/.test(url.pathname))
1375-
return json({ data: [], cursor: {} })
1376-
if (session && /^\/api\/session\/[^/]+$/.test(url.pathname)) return json({ data: session })
1377-
return undefined
1378-
},
1379-
})
1375+
if (/^\/api\/session\/[^/]+\/prompt$/.test(url.pathname)) {
1376+
submitted.resolve(await request.json())
1377+
return json({ data: {} })
1378+
}
1379+
if (/^\/api\/session\/[^/]+\/(message|inbox|permission)$/.test(url.pathname))
1380+
return json({ data: [], cursor: {} })
1381+
if (session && /^\/api\/session\/[^/]+$/.test(url.pathname)) return json({ data: session })
1382+
return undefined
1383+
},
1384+
})
13801385

1381-
await setup.ready
1382-
await setup.waitForFrame((frame) => frame.includes("Build · Remote Model Provider"))
1383-
await setup.mockInput.typeText("list files in /tmp")
1384-
await setup.waitForFrame((frame) => frame.includes("No matching commands"))
1385-
setup.mockInput.pressEnter()
1386-
expect(
1387-
await Promise.race([
1388-
submitted.promise,
1389-
Bun.sleep(2_000).then(() => {
1390-
throw new Error("prompt was not submitted")
1391-
}),
1392-
]),
1393-
).toMatchObject({ text: "list files in /tmp" })
1394-
}, 10_000)
1386+
await setup.ready
1387+
await setup.waitForFrame((frame) => frame.includes("Build · Remote Model Provider"))
1388+
await setup.mockInput.typeText(text)
1389+
await setup.waitForFrame((frame) => frame.includes("No matching commands"))
1390+
setup.mockInput.pressEnter()
1391+
const result = await Promise.race([submitted.promise, Bun.sleep(2_000).then(() => undefined)])
1392+
if (!submits) {
1393+
expect(result).toBeUndefined()
1394+
expect(setup.captureCharFrame()).toContain("No matching commands")
1395+
return
1396+
}
1397+
expect(result).toMatchObject({ text })
1398+
},
1399+
10_000,
1400+
)
13951401

13961402
test.skipIf(process.platform === "win32").each(["manual", "select"] as const)(
13971403
"selection copy and pane management respect %s mode in the prompt and terminal pane",

0 commit comments

Comments
 (0)