Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(web-app): keep the Cowork draft when a send is refused

thinhlpg4360f0fd9c6f
diff --git a/web-app/src/containers/ChatInput.tsx b/web-app/src/containers/ChatInput.tsx
index dbfa1558c..7f5e559e3 100644
--- a/web-app/src/containers/ChatInput.tsx
+++ b/web-app/src/containers/ChatInput.tsx
@@ -125,6 +125,7 @@ type ChatInputProps = {
projectAssistantId?: string
/** Whether this surface applies assistant instructions and selection. */
supportsAssistants?: boolean
+ /** Returning `'refused'` keeps the draft and attachments: nothing was sent. */
onSubmit?: (
text: string,
files?: Array<{ type: 'file'; mediaType: string; url: string }>,
@@ -134,7 +135,7 @@ type ChatInputProps = {
/** What the user typed, before @path references were expanded into
* `text`, so a cancelled send can hand it back unchanged. */
draft?: string
- ) => void
+ ) => void | 'refused'
onStop?: () => void
chatStatus?: ChatStatus
/**
@@ -714,12 +715,13 @@ const ChatInput = memo(function ChatInput({
const { files, documents } = collectMedia()
- onSubmit(
+ const outcome = onSubmit(
effectivePrompt,
files,
documents,
prompt
)
+ if (outcome === 'refused') return
setPrompt('')
clearAttachmentsForThread(attachmentsKey)
} else {
diff --git a/web-app/src/containers/CoworkPage.tsx b/web-app/src/containers/CoworkPage.tsx
index a1fabc8d5..19224a4d0 100644
--- a/web-app/src/containers/CoworkPage.tsx
+++ b/web-app/src/containers/CoworkPage.tsx
@@ -1265,7 +1265,7 @@ export function CoworkPage() {
text: string,
files?: CoworkMediaPart[],
documents?: Attachment[]
- ) => {
+ ): 'refused' | undefined => {
const skillCommand = parseSkillCommand(text)
const isBuiltInCommand = skillCommand
? SLASH_COMMANDS.some((command) => command.name === `/${skillCommand.name}`)
@@ -1278,7 +1278,7 @@ export function CoworkPage() {
toast.error(
t('common:coworkSlash.skillUnavailable', { name: skillCommand.name })
)
- return
+ return 'refused'
}
// Non-skill slash commands are client-side actions.
@@ -1293,17 +1293,28 @@ export function CoworkPage() {
})
return
}
- void runRequest(text, {
- media: files,
- files: documents
- ?.filter((d): d is Attachment & { path: string } => !!d.path)
- .map((d) => ({
- name: d.name,
- path: d.path,
- fileType: d.fileType,
- size: d.size,
- })),
- })
+ // Every refusal in runRequest happens before its first await, so whether
+ // the run was claimed is known by the time the call returns.
+ let accepted = false
+ void runRequest(
+ text,
+ {
+ media: files,
+ files: documents
+ ?.filter((d): d is Attachment & { path: string } => !!d.path)
+ .map((d) => ({
+ name: d.name,
+ path: d.path,
+ fileType: d.fileType,
+ size: d.size,
+ })),
+ },
+ undefined,
+ () => {
+ accepted = true
+ }
+ )
+ return accepted ? undefined : 'refused'
}
diff --git a/web-app/src/containers/__tests__/ChatInput.test.tsx b/web-app/src/containers/__tests__/ChatInput.test.tsx
index 738fd5855..82158573b 100644
--- a/web-app/src/containers/__tests__/ChatInput.test.tsx
+++ b/web-app/src/containers/__tests__/ChatInput.test.tsx
@@ -477,6 +477,15 @@ describe('ChatInput', () => {
expect(setPromptMock).toHaveBeenCalledWith('')
})
+ it('keeps the draft when the surface refuses the send', async () => {
+ promptState = 'keep me'
+ const onSubmit = vi.fn(() => 'refused' as const)
+ renderInput({ onSubmit })
+ fireEvent.keyDown(getTextarea(), { key: 'Enter' })
+ await waitFor(() => expect(onSubmit).toHaveBeenCalled())
+ expect(setPromptMock).not.toHaveBeenCalledWith('')
+ })
+
it('does NOT submit on Shift+Enter (newline behavior)', () => {
promptState = 'hello'
const onSubmit = vi.fn()