chore: cleanup

This commit is contained in:
Adam
2026-02-16 07:58:18 -06:00
parent 45fa5e7199
commit b055f973df
4 changed files with 194 additions and 221 deletions

View File

@@ -23,7 +23,6 @@ import { useSync } from "@/context/sync"
import { useTerminal, type LocalPTY } from "@/context/terminal" import { useTerminal, type LocalPTY } from "@/context/terminal"
import { useLayout } from "@/context/layout" import { useLayout } from "@/context/layout"
import { checksum, base64Encode } from "@opencode-ai/util/encode" import { checksum, base64Encode } from "@opencode-ai/util/encode"
import { findLast } from "@opencode-ai/util/array"
import { useDialog } from "@opencode-ai/ui/context/dialog" import { useDialog } from "@opencode-ai/ui/context/dialog"
import { DialogSelectFile } from "@/components/dialog-select-file" import { DialogSelectFile } from "@/components/dialog-select-file"
import FileTree from "@/components/file-tree" import FileTree from "@/components/file-tree"
@@ -35,7 +34,6 @@ import { useSDK } from "@/context/sdk"
import { usePrompt } from "@/context/prompt" import { usePrompt } from "@/context/prompt"
import { useComments } from "@/context/comments" import { useComments } from "@/context/comments"
import { ConstrainDragYAxis, getDraggableId } from "@/utils/solid-dnd" import { ConstrainDragYAxis, getDraggableId } from "@/utils/solid-dnd"
import { usePermission } from "@/context/permission"
import { showToast } from "@opencode-ai/ui/toast" import { showToast } from "@opencode-ai/ui/toast"
import { SessionHeader, SessionContextTab, SortableTab, FileVisual, NewSessionView } from "@/components/session" import { SessionHeader, SessionContextTab, SortableTab, FileVisual, NewSessionView } from "@/components/session"
import { navMark, navParams } from "@/utils/perf" import { navMark, navParams } from "@/utils/perf"
@@ -101,7 +99,6 @@ export default function Page() {
const sdk = useSDK() const sdk = useSDK()
const prompt = usePrompt() const prompt = usePrompt()
const comments = useComments() const comments = useComments()
const permission = usePermission()
const permRequest = createMemo(() => { const permRequest = createMemo(() => {
const sessionID = params.id const sessionID = params.id
@@ -770,11 +767,6 @@ export default function Page() {
return lines.slice(0, 2).join("\n") return lines.slice(0, 2).join("\n")
} }
const addSelectionToContext = (path: string, selection: FileSelection) => {
const preview = selectionPreview(path, selection)
prompt.context.add({ type: "file", path, selection, preview })
}
const addCommentToContext = (input: { const addCommentToContext = (input: {
file: string file: string
selection: SelectedLineRange selection: SelectedLineRange
@@ -913,31 +905,11 @@ export default function Page() {
const focusInput = () => inputRef?.focus() const focusInput = () => inputRef?.focus()
useSessionCommands({ useSessionCommands({
command,
dialog,
file,
language,
local,
permission,
prompt,
sdk,
sync,
terminal,
layout,
params,
navigate,
tabs,
view,
info,
status,
userMessages,
visibleUserMessages,
activeMessage, activeMessage,
showAllFiles, showAllFiles,
navigateMessageByOffset, navigateMessageByOffset,
setExpanded: (id, fn) => setStore("expanded", id, fn), setExpanded: (id, fn) => setStore("expanded", id, fn),
setActiveMessage, setActiveMessage,
addSelectionToContext,
focusInput, focusInput,
}) })

View File

@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test" import { describe, expect, test } from "bun:test"
import { combineCommandSections, createOpenReviewFile, focusTerminalById, getTabReorderIndex } from "./helpers" import { createOpenReviewFile, focusTerminalById, getTabReorderIndex } from "./helpers"
describe("createOpenReviewFile", () => { describe("createOpenReviewFile", () => {
test("opens and loads selected review file", () => { test("opens and loads selected review file", () => {
@@ -46,20 +46,6 @@ describe("focusTerminalById", () => {
}) })
}) })
describe("combineCommandSections", () => {
test("keeps section order stable", () => {
const result = combineCommandSections([
[{ id: "a", title: "A" }],
[
{ id: "b", title: "B" },
{ id: "c", title: "C" },
],
])
expect(result.map((item) => item.id)).toEqual(["a", "b", "c"])
})
})
describe("getTabReorderIndex", () => { describe("getTabReorderIndex", () => {
test("returns target index for valid drag reorder", () => { test("returns target index for valid drag reorder", () => {
expect(getTabReorderIndex(["a", "b", "c"], "a", "c")).toBe(2) expect(getTabReorderIndex(["a", "b", "c"], "a", "c")).toBe(2)

View File

@@ -1,4 +1,3 @@
import type { CommandOption } from "@/context/command"
import { batch } from "solid-js" import { batch } from "solid-js"
export const focusTerminalById = (id: string) => { export const focusTerminalById = (id: string) => {
@@ -36,10 +35,6 @@ export const createOpenReviewFile = (input: {
} }
} }
export const combineCommandSections = (sections: readonly (readonly CommandOption[])[]) => {
return sections.flatMap((section) => section)
}
export const getTabReorderIndex = (tabs: readonly string[], from: string, to: string) => { export const getTabReorderIndex = (tabs: readonly string[], from: string, to: string) => {
const fromIndex = tabs.indexOf(from) const fromIndex = tabs.indexOf(from)
const toIndex = tabs.indexOf(to) const toIndex = tabs.indexOf(to)

View File

@@ -19,35 +19,14 @@ import { showToast } from "@opencode-ai/ui/toast"
import { findLast } from "@opencode-ai/util/array" import { findLast } from "@opencode-ai/util/array"
import { extractPromptFromParts } from "@/utils/prompt" import { extractPromptFromParts } from "@/utils/prompt"
import { UserMessage } from "@opencode-ai/sdk/v2" import { UserMessage } from "@opencode-ai/sdk/v2"
import { combineCommandSections } from "@/pages/session/helpers"
import { canAddSelectionContext } from "@/pages/session/session-command-helpers" import { canAddSelectionContext } from "@/pages/session/session-command-helpers"
export type SessionCommandContext = { export type SessionCommandContext = {
command: ReturnType<typeof useCommand>
dialog: ReturnType<typeof useDialog>
file: ReturnType<typeof useFile>
language: ReturnType<typeof useLanguage>
local: ReturnType<typeof useLocal>
permission: ReturnType<typeof usePermission>
prompt: ReturnType<typeof usePrompt>
sdk: ReturnType<typeof useSDK>
sync: ReturnType<typeof useSync>
terminal: ReturnType<typeof useTerminal>
layout: ReturnType<typeof useLayout>
params: ReturnType<typeof useParams>
navigate: ReturnType<typeof useNavigate>
tabs: () => ReturnType<ReturnType<typeof useLayout>["tabs"]>
view: () => ReturnType<ReturnType<typeof useLayout>["view"]>
info: () => { revert?: { messageID?: string }; share?: { url?: string } } | undefined
status: () => { type: string }
userMessages: () => UserMessage[]
visibleUserMessages: () => UserMessage[]
activeMessage: () => UserMessage | undefined activeMessage: () => UserMessage | undefined
showAllFiles: () => void showAllFiles: () => void
navigateMessageByOffset: (offset: number) => void navigateMessageByOffset: (offset: number) => void
setExpanded: (id: string, fn: (open: boolean | undefined) => boolean) => void setExpanded: (id: string, fn: (open: boolean | undefined) => boolean) => void
setActiveMessage: (message: UserMessage | undefined) => void setActiveMessage: (message: UserMessage | undefined) => void
addSelectionToContext: (path: string, selection: FileSelection) => void
focusInput: () => void focusInput: () => void
} }
@@ -58,45 +37,88 @@ const withCategory = (category: string) => {
}) })
} }
export const useSessionCommands = (input: SessionCommandContext) => { export const useSessionCommands = (args: SessionCommandContext) => {
const sessionCommand = withCategory(input.language.t("command.category.session")) const command = useCommand()
const fileCommand = withCategory(input.language.t("command.category.file")) const dialog = useDialog()
const contextCommand = withCategory(input.language.t("command.category.context")) const file = useFile()
const viewCommand = withCategory(input.language.t("command.category.view")) const language = useLanguage()
const terminalCommand = withCategory(input.language.t("command.category.terminal")) const local = useLocal()
const modelCommand = withCategory(input.language.t("command.category.model")) const permission = usePermission()
const mcpCommand = withCategory(input.language.t("command.category.mcp")) const prompt = usePrompt()
const agentCommand = withCategory(input.language.t("command.category.agent")) const sdk = useSDK()
const permissionsCommand = withCategory(input.language.t("command.category.permissions")) const sync = useSync()
const terminal = useTerminal()
const layout = useLayout()
const params = useParams()
const navigate = useNavigate()
const sessionKey = createMemo(() => `${params.dir}${params.id ? "/" + params.id : ""}`)
const tabs = createMemo(() => layout.tabs(sessionKey))
const view = createMemo(() => layout.view(sessionKey))
const info = createMemo(() => (params.id ? sync.session.get(params.id) : undefined))
const idle = { type: "idle" as const }
const status = createMemo(() => sync.data.session_status[params.id ?? ""] ?? idle)
const messages = createMemo(() => (params.id ? (sync.data.message[params.id] ?? []) : []))
const userMessages = createMemo(() => messages().filter((m) => m.role === "user") as UserMessage[])
const visibleUserMessages = createMemo(() => {
const revert = info()?.revert?.messageID
if (!revert) return userMessages()
return userMessages().filter((m) => m.id < revert)
})
const selectionPreview = (path: string, selection: FileSelection) => {
const content = file.get(path)?.content?.content
if (!content) return undefined
const start = Math.max(1, Math.min(selection.startLine, selection.endLine))
const end = Math.max(selection.startLine, selection.endLine)
const lines = content.split("\n").slice(start - 1, end)
if (lines.length === 0) return undefined
return lines.slice(0, 2).join("\n")
}
const addSelectionToContext = (path: string, selection: FileSelection) => {
const preview = selectionPreview(path, selection)
prompt.context.add({ type: "file", path, selection, preview })
}
const sessionCommand = withCategory(language.t("command.category.session"))
const fileCommand = withCategory(language.t("command.category.file"))
const contextCommand = withCategory(language.t("command.category.context"))
const viewCommand = withCategory(language.t("command.category.view"))
const terminalCommand = withCategory(language.t("command.category.terminal"))
const modelCommand = withCategory(language.t("command.category.model"))
const mcpCommand = withCategory(language.t("command.category.mcp"))
const agentCommand = withCategory(language.t("command.category.agent"))
const permissionsCommand = withCategory(language.t("command.category.permissions"))
const sessionCommands = createMemo(() => [ const sessionCommands = createMemo(() => [
sessionCommand({ sessionCommand({
id: "session.new", id: "session.new",
title: input.language.t("command.session.new"), title: language.t("command.session.new"),
keybind: "mod+shift+s", keybind: "mod+shift+s",
slash: "new", slash: "new",
onSelect: () => input.navigate(`/${input.params.dir}/session`), onSelect: () => navigate(`/${params.dir}/session`),
}), }),
]) ])
const fileCommands = createMemo(() => [ const fileCommands = createMemo(() => [
fileCommand({ fileCommand({
id: "file.open", id: "file.open",
title: input.language.t("command.file.open"), title: language.t("command.file.open"),
description: input.language.t("palette.search.placeholder"), description: language.t("palette.search.placeholder"),
keybind: "mod+p", keybind: "mod+p",
slash: "open", slash: "open",
onSelect: () => input.dialog.show(() => <DialogSelectFile onOpenFile={input.showAllFiles} />), onSelect: () => dialog.show(() => <DialogSelectFile onOpenFile={args.showAllFiles} />),
}), }),
fileCommand({ fileCommand({
id: "tab.close", id: "tab.close",
title: input.language.t("command.tab.close"), title: language.t("command.tab.close"),
keybind: "mod+w", keybind: "mod+w",
disabled: !input.tabs().active(), disabled: !tabs().active(),
onSelect: () => { onSelect: () => {
const active = input.tabs().active() const active = tabs().active()
if (!active) return if (!active) return
input.tabs().close(active) tabs().close(active)
}, },
}), }),
]) ])
@@ -104,30 +126,30 @@ export const useSessionCommands = (input: SessionCommandContext) => {
const contextCommands = createMemo(() => [ const contextCommands = createMemo(() => [
contextCommand({ contextCommand({
id: "context.addSelection", id: "context.addSelection",
title: input.language.t("command.context.addSelection"), title: language.t("command.context.addSelection"),
description: input.language.t("command.context.addSelection.description"), description: language.t("command.context.addSelection.description"),
keybind: "mod+shift+l", keybind: "mod+shift+l",
disabled: !canAddSelectionContext({ disabled: !canAddSelectionContext({
active: input.tabs().active(), active: tabs().active(),
pathFromTab: input.file.pathFromTab, pathFromTab: file.pathFromTab,
selectedLines: input.file.selectedLines, selectedLines: file.selectedLines,
}), }),
onSelect: () => { onSelect: () => {
const active = input.tabs().active() const active = tabs().active()
if (!active) return if (!active) return
const path = input.file.pathFromTab(active) const path = file.pathFromTab(active)
if (!path) return if (!path) return
const range = input.file.selectedLines(path) as SelectedLineRange | null | undefined const range = file.selectedLines(path) as SelectedLineRange | null | undefined
if (!range) { if (!range) {
showToast({ showToast({
title: input.language.t("toast.context.noLineSelection.title"), title: language.t("toast.context.noLineSelection.title"),
description: input.language.t("toast.context.noLineSelection.description"), description: language.t("toast.context.noLineSelection.description"),
}) })
return return
} }
input.addSelectionToContext(path, selectionFromLines(range)) addSelectionToContext(path, selectionFromLines(range))
}, },
}), }),
]) ])
@@ -135,50 +157,50 @@ export const useSessionCommands = (input: SessionCommandContext) => {
const viewCommands = createMemo(() => [ const viewCommands = createMemo(() => [
viewCommand({ viewCommand({
id: "terminal.toggle", id: "terminal.toggle",
title: input.language.t("command.terminal.toggle"), title: language.t("command.terminal.toggle"),
keybind: "ctrl+`", keybind: "ctrl+`",
slash: "terminal", slash: "terminal",
onSelect: () => input.view().terminal.toggle(), onSelect: () => view().terminal.toggle(),
}), }),
viewCommand({ viewCommand({
id: "review.toggle", id: "review.toggle",
title: input.language.t("command.review.toggle"), title: language.t("command.review.toggle"),
keybind: "mod+shift+r", keybind: "mod+shift+r",
onSelect: () => input.view().reviewPanel.toggle(), onSelect: () => view().reviewPanel.toggle(),
}), }),
viewCommand({ viewCommand({
id: "fileTree.toggle", id: "fileTree.toggle",
title: input.language.t("command.fileTree.toggle"), title: language.t("command.fileTree.toggle"),
keybind: "mod+\\", keybind: "mod+\\",
onSelect: () => input.layout.fileTree.toggle(), onSelect: () => layout.fileTree.toggle(),
}), }),
viewCommand({ viewCommand({
id: "input.focus", id: "input.focus",
title: input.language.t("command.input.focus"), title: language.t("command.input.focus"),
keybind: "ctrl+l", keybind: "ctrl+l",
onSelect: () => input.focusInput(), onSelect: () => args.focusInput(),
}), }),
terminalCommand({ terminalCommand({
id: "terminal.new", id: "terminal.new",
title: input.language.t("command.terminal.new"), title: language.t("command.terminal.new"),
description: input.language.t("command.terminal.new.description"), description: language.t("command.terminal.new.description"),
keybind: "ctrl+alt+t", keybind: "ctrl+alt+t",
onSelect: () => { onSelect: () => {
if (input.terminal.all().length > 0) input.terminal.new() if (terminal.all().length > 0) terminal.new()
input.view().terminal.open() view().terminal.open()
}, },
}), }),
viewCommand({ viewCommand({
id: "steps.toggle", id: "steps.toggle",
title: input.language.t("command.steps.toggle"), title: language.t("command.steps.toggle"),
description: input.language.t("command.steps.toggle.description"), description: language.t("command.steps.toggle.description"),
keybind: "mod+e", keybind: "mod+e",
slash: "steps", slash: "steps",
disabled: !input.params.id, disabled: !params.id,
onSelect: () => { onSelect: () => {
const msg = input.activeMessage() const msg = args.activeMessage()
if (!msg) return if (!msg) return
input.setExpanded(msg.id, (open: boolean | undefined) => !open) args.setExpanded(msg.id, (open: boolean | undefined) => !open)
}, },
}), }),
]) ])
@@ -186,61 +208,61 @@ export const useSessionCommands = (input: SessionCommandContext) => {
const messageCommands = createMemo(() => [ const messageCommands = createMemo(() => [
sessionCommand({ sessionCommand({
id: "message.previous", id: "message.previous",
title: input.language.t("command.message.previous"), title: language.t("command.message.previous"),
description: input.language.t("command.message.previous.description"), description: language.t("command.message.previous.description"),
keybind: "mod+arrowup", keybind: "mod+arrowup",
disabled: !input.params.id, disabled: !params.id,
onSelect: () => input.navigateMessageByOffset(-1), onSelect: () => args.navigateMessageByOffset(-1),
}), }),
sessionCommand({ sessionCommand({
id: "message.next", id: "message.next",
title: input.language.t("command.message.next"), title: language.t("command.message.next"),
description: input.language.t("command.message.next.description"), description: language.t("command.message.next.description"),
keybind: "mod+arrowdown", keybind: "mod+arrowdown",
disabled: !input.params.id, disabled: !params.id,
onSelect: () => input.navigateMessageByOffset(1), onSelect: () => args.navigateMessageByOffset(1),
}), }),
]) ])
const agentCommands = createMemo(() => [ const agentCommands = createMemo(() => [
modelCommand({ modelCommand({
id: "model.choose", id: "model.choose",
title: input.language.t("command.model.choose"), title: language.t("command.model.choose"),
description: input.language.t("command.model.choose.description"), description: language.t("command.model.choose.description"),
keybind: "mod+'", keybind: "mod+'",
slash: "model", slash: "model",
onSelect: () => input.dialog.show(() => <DialogSelectModel />), onSelect: () => dialog.show(() => <DialogSelectModel />),
}), }),
mcpCommand({ mcpCommand({
id: "mcp.toggle", id: "mcp.toggle",
title: input.language.t("command.mcp.toggle"), title: language.t("command.mcp.toggle"),
description: input.language.t("command.mcp.toggle.description"), description: language.t("command.mcp.toggle.description"),
keybind: "mod+;", keybind: "mod+;",
slash: "mcp", slash: "mcp",
onSelect: () => input.dialog.show(() => <DialogSelectMcp />), onSelect: () => dialog.show(() => <DialogSelectMcp />),
}), }),
agentCommand({ agentCommand({
id: "agent.cycle", id: "agent.cycle",
title: input.language.t("command.agent.cycle"), title: language.t("command.agent.cycle"),
description: input.language.t("command.agent.cycle.description"), description: language.t("command.agent.cycle.description"),
keybind: "mod+.", keybind: "mod+.",
slash: "agent", slash: "agent",
onSelect: () => input.local.agent.move(1), onSelect: () => local.agent.move(1),
}), }),
agentCommand({ agentCommand({
id: "agent.cycle.reverse", id: "agent.cycle.reverse",
title: input.language.t("command.agent.cycle.reverse"), title: language.t("command.agent.cycle.reverse"),
description: input.language.t("command.agent.cycle.reverse.description"), description: language.t("command.agent.cycle.reverse.description"),
keybind: "shift+mod+.", keybind: "shift+mod+.",
onSelect: () => input.local.agent.move(-1), onSelect: () => local.agent.move(-1),
}), }),
modelCommand({ modelCommand({
id: "model.variant.cycle", id: "model.variant.cycle",
title: input.language.t("command.model.variant.cycle"), title: language.t("command.model.variant.cycle"),
description: input.language.t("command.model.variant.cycle.description"), description: language.t("command.model.variant.cycle.description"),
keybind: "shift+mod+d", keybind: "shift+mod+d",
onSelect: () => { onSelect: () => {
input.local.model.variant.cycle() local.model.variant.cycle()
}, },
}), }),
]) ])
@@ -249,22 +271,22 @@ export const useSessionCommands = (input: SessionCommandContext) => {
permissionsCommand({ permissionsCommand({
id: "permissions.autoaccept", id: "permissions.autoaccept",
title: title:
input.params.id && input.permission.isAutoAccepting(input.params.id, input.sdk.directory) params.id && permission.isAutoAccepting(params.id, sdk.directory)
? input.language.t("command.permissions.autoaccept.disable") ? language.t("command.permissions.autoaccept.disable")
: input.language.t("command.permissions.autoaccept.enable"), : language.t("command.permissions.autoaccept.enable"),
keybind: "mod+shift+a", keybind: "mod+shift+a",
disabled: !input.params.id || !input.permission.permissionsEnabled(), disabled: !params.id || !permission.permissionsEnabled(),
onSelect: () => { onSelect: () => {
const sessionID = input.params.id const sessionID = params.id
if (!sessionID) return if (!sessionID) return
input.permission.toggleAutoAccept(sessionID, input.sdk.directory) permission.toggleAutoAccept(sessionID, sdk.directory)
showToast({ showToast({
title: input.permission.isAutoAccepting(sessionID, input.sdk.directory) title: permission.isAutoAccepting(sessionID, sdk.directory)
? input.language.t("toast.permissions.autoaccept.on.title") ? language.t("toast.permissions.autoaccept.on.title")
: input.language.t("toast.permissions.autoaccept.off.title"), : language.t("toast.permissions.autoaccept.off.title"),
description: input.permission.isAutoAccepting(sessionID, input.sdk.directory) description: permission.isAutoAccepting(sessionID, sdk.directory)
? input.language.t("toast.permissions.autoaccept.on.description") ? language.t("toast.permissions.autoaccept.on.description")
: input.language.t("toast.permissions.autoaccept.off.description"), : language.t("toast.permissions.autoaccept.off.description"),
}) })
}, },
}), }),
@@ -273,71 +295,71 @@ export const useSessionCommands = (input: SessionCommandContext) => {
const sessionActionCommands = createMemo(() => [ const sessionActionCommands = createMemo(() => [
sessionCommand({ sessionCommand({
id: "session.undo", id: "session.undo",
title: input.language.t("command.session.undo"), title: language.t("command.session.undo"),
description: input.language.t("command.session.undo.description"), description: language.t("command.session.undo.description"),
slash: "undo", slash: "undo",
disabled: !input.params.id || input.visibleUserMessages().length === 0, disabled: !params.id || visibleUserMessages().length === 0,
onSelect: async () => { onSelect: async () => {
const sessionID = input.params.id const sessionID = params.id
if (!sessionID) return if (!sessionID) return
if (input.status()?.type !== "idle") { if (status()?.type !== "idle") {
await input.sdk.client.session.abort({ sessionID }).catch(() => {}) await sdk.client.session.abort({ sessionID }).catch(() => {})
} }
const revert = input.info()?.revert?.messageID const revert = info()?.revert?.messageID
const message = findLast(input.userMessages(), (x) => !revert || x.id < revert) const message = findLast(userMessages(), (x) => !revert || x.id < revert)
if (!message) return if (!message) return
await input.sdk.client.session.revert({ sessionID, messageID: message.id }) await sdk.client.session.revert({ sessionID, messageID: message.id })
const parts = input.sync.data.part[message.id] const parts = sync.data.part[message.id]
if (parts) { if (parts) {
const restored = extractPromptFromParts(parts, { directory: input.sdk.directory }) const restored = extractPromptFromParts(parts, { directory: sdk.directory })
input.prompt.set(restored) prompt.set(restored)
} }
const priorMessage = findLast(input.userMessages(), (x) => x.id < message.id) const priorMessage = findLast(userMessages(), (x) => x.id < message.id)
input.setActiveMessage(priorMessage) args.setActiveMessage(priorMessage)
}, },
}), }),
sessionCommand({ sessionCommand({
id: "session.redo", id: "session.redo",
title: input.language.t("command.session.redo"), title: language.t("command.session.redo"),
description: input.language.t("command.session.redo.description"), description: language.t("command.session.redo.description"),
slash: "redo", slash: "redo",
disabled: !input.params.id || !input.info()?.revert?.messageID, disabled: !params.id || !info()?.revert?.messageID,
onSelect: async () => { onSelect: async () => {
const sessionID = input.params.id const sessionID = params.id
if (!sessionID) return if (!sessionID) return
const revertMessageID = input.info()?.revert?.messageID const revertMessageID = info()?.revert?.messageID
if (!revertMessageID) return if (!revertMessageID) return
const nextMessage = input.userMessages().find((x) => x.id > revertMessageID) const nextMessage = userMessages().find((x) => x.id > revertMessageID)
if (!nextMessage) { if (!nextMessage) {
await input.sdk.client.session.unrevert({ sessionID }) await sdk.client.session.unrevert({ sessionID })
input.prompt.reset() prompt.reset()
const lastMsg = findLast(input.userMessages(), (x) => x.id >= revertMessageID) const lastMsg = findLast(userMessages(), (x) => x.id >= revertMessageID)
input.setActiveMessage(lastMsg) args.setActiveMessage(lastMsg)
return return
} }
await input.sdk.client.session.revert({ sessionID, messageID: nextMessage.id }) await sdk.client.session.revert({ sessionID, messageID: nextMessage.id })
const priorMsg = findLast(input.userMessages(), (x) => x.id < nextMessage.id) const priorMsg = findLast(userMessages(), (x) => x.id < nextMessage.id)
input.setActiveMessage(priorMsg) args.setActiveMessage(priorMsg)
}, },
}), }),
sessionCommand({ sessionCommand({
id: "session.compact", id: "session.compact",
title: input.language.t("command.session.compact"), title: language.t("command.session.compact"),
description: input.language.t("command.session.compact.description"), description: language.t("command.session.compact.description"),
slash: "compact", slash: "compact",
disabled: !input.params.id || input.visibleUserMessages().length === 0, disabled: !params.id || visibleUserMessages().length === 0,
onSelect: async () => { onSelect: async () => {
const sessionID = input.params.id const sessionID = params.id
if (!sessionID) return if (!sessionID) return
const model = input.local.model.current() const model = local.model.current()
if (!model) { if (!model) {
showToast({ showToast({
title: input.language.t("toast.model.none.title"), title: language.t("toast.model.none.title"),
description: input.language.t("toast.model.none.description"), description: language.t("toast.model.none.description"),
}) })
return return
} }
await input.sdk.client.session.summarize({ await sdk.client.session.summarize({
sessionID, sessionID,
modelID: model.id, modelID: model.id,
providerID: model.provider.id, providerID: model.provider.id,
@@ -346,29 +368,27 @@ export const useSessionCommands = (input: SessionCommandContext) => {
}), }),
sessionCommand({ sessionCommand({
id: "session.fork", id: "session.fork",
title: input.language.t("command.session.fork"), title: language.t("command.session.fork"),
description: input.language.t("command.session.fork.description"), description: language.t("command.session.fork.description"),
slash: "fork", slash: "fork",
disabled: !input.params.id || input.visibleUserMessages().length === 0, disabled: !params.id || visibleUserMessages().length === 0,
onSelect: () => input.dialog.show(() => <DialogFork />), onSelect: () => dialog.show(() => <DialogFork />),
}), }),
]) ])
const shareCommands = createMemo(() => { const shareCommands = createMemo(() => {
if (input.sync.data.config.share === "disabled") return [] if (sync.data.config.share === "disabled") return []
return [ return [
sessionCommand({ sessionCommand({
id: "session.share", id: "session.share",
title: input.info()?.share?.url title: info()?.share?.url ? language.t("session.share.copy.copyLink") : language.t("command.session.share"),
? input.language.t("session.share.copy.copyLink") description: info()?.share?.url
: input.language.t("command.session.share"), ? language.t("toast.session.share.success.description")
description: input.info()?.share?.url : language.t("command.session.share.description"),
? input.language.t("toast.session.share.success.description")
: input.language.t("command.session.share.description"),
slash: "share", slash: "share",
disabled: !input.params.id, disabled: !params.id,
onSelect: async () => { onSelect: async () => {
if (!input.params.id) return if (!params.id) return
const write = (value: string) => { const write = (value: string) => {
const body = typeof document === "undefined" ? undefined : document.body const body = typeof document === "undefined" ? undefined : document.body
@@ -398,7 +418,7 @@ export const useSessionCommands = (input: SessionCommandContext) => {
const ok = await write(url) const ok = await write(url)
if (!ok) { if (!ok) {
showToast({ showToast({
title: input.language.t("toast.session.share.copyFailed.title"), title: language.t("toast.session.share.copyFailed.title"),
variant: "error", variant: "error",
}) })
return return
@@ -406,27 +426,27 @@ export const useSessionCommands = (input: SessionCommandContext) => {
showToast({ showToast({
title: existing title: existing
? input.language.t("session.share.copy.copied") ? language.t("session.share.copy.copied")
: input.language.t("toast.session.share.success.title"), : language.t("toast.session.share.success.title"),
description: input.language.t("toast.session.share.success.description"), description: language.t("toast.session.share.success.description"),
variant: "success", variant: "success",
}) })
} }
const existing = input.info()?.share?.url const existing = info()?.share?.url
if (existing) { if (existing) {
await copy(existing, true) await copy(existing, true)
return return
} }
const url = await input.sdk.client.session const url = await sdk.client.session
.share({ sessionID: input.params.id }) .share({ sessionID: params.id })
.then((res) => res.data?.share?.url) .then((res) => res.data?.share?.url)
.catch(() => undefined) .catch(() => undefined)
if (!url) { if (!url) {
showToast({ showToast({
title: input.language.t("toast.session.share.failed.title"), title: language.t("toast.session.share.failed.title"),
description: input.language.t("toast.session.share.failed.description"), description: language.t("toast.session.share.failed.description"),
variant: "error", variant: "error",
}) })
return return
@@ -437,25 +457,25 @@ export const useSessionCommands = (input: SessionCommandContext) => {
}), }),
sessionCommand({ sessionCommand({
id: "session.unshare", id: "session.unshare",
title: input.language.t("command.session.unshare"), title: language.t("command.session.unshare"),
description: input.language.t("command.session.unshare.description"), description: language.t("command.session.unshare.description"),
slash: "unshare", slash: "unshare",
disabled: !input.params.id || !input.info()?.share?.url, disabled: !params.id || !info()?.share?.url,
onSelect: async () => { onSelect: async () => {
if (!input.params.id) return if (!params.id) return
await input.sdk.client.session await sdk.client.session
.unshare({ sessionID: input.params.id }) .unshare({ sessionID: params.id })
.then(() => .then(() =>
showToast({ showToast({
title: input.language.t("toast.session.unshare.success.title"), title: language.t("toast.session.unshare.success.title"),
description: input.language.t("toast.session.unshare.success.description"), description: language.t("toast.session.unshare.success.description"),
variant: "success", variant: "success",
}), }),
) )
.catch(() => .catch(() =>
showToast({ showToast({
title: input.language.t("toast.session.unshare.failed.title"), title: language.t("toast.session.unshare.failed.title"),
description: input.language.t("toast.session.unshare.failed.description"), description: language.t("toast.session.unshare.failed.description"),
variant: "error", variant: "error",
}), }),
) )
@@ -464,8 +484,8 @@ export const useSessionCommands = (input: SessionCommandContext) => {
] ]
}) })
input.command.register("session", () => command.register("session", () =>
combineCommandSections([ [
sessionCommands(), sessionCommands(),
fileCommands(), fileCommands(),
contextCommands(), contextCommands(),
@@ -475,6 +495,6 @@ export const useSessionCommands = (input: SessionCommandContext) => {
permissionCommands(), permissionCommands(),
sessionActionCommands(), sessionActionCommands(),
shareCommands(), shareCommands(),
]), ].flatMap((section) => section),
) )
} }