File size: 1,694 Bytes
abcf568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { appendStudioReferenceImages } from '../../composer/attachments'
import { resolveStudioCommand } from '../../commands/resolve-studio-command'
import type { StudioSession } from '../../protocol/studio-agent-types'

interface SubmitStudioCommandComposerOptions<TAttachment> {
  input: string
  disabled: boolean
  session: StudioSession | null
  attachments: TAttachment[]
  onRun: (inputText: string) => Promise<void> | void
  clearInput: () => void
  restoreInput: (value: string) => void
  clearAttachments: () => void
  retainAttachments: (attachments: TAttachment[]) => void
  focusInput: () => void
  openImageInputMode: () => void
}

export async function submitStudioCommandComposer<TAttachment>({
  input,
  disabled,
  session,
  attachments,
  onRun,
  clearInput,
  restoreInput,
  clearAttachments,
  retainAttachments,
  focusInput,
  openImageInputMode,
}: SubmitStudioCommandComposerOptions<TAttachment>) {
  const next = input.trim()
  if (!next || disabled) {
    return
  }

  const localCommand = resolveStudioCommand(next)
  if (localCommand?.definition.scope === 'local') {
    clearInput()
    await localCommand.definition.execute(localCommand.command as never, {
      session,
      openHistory: () => {},
      createSession: async () => {},
      setPendingMode: () => {},
      openImageInputMode,
      runCommandInput: async (inputText: string) => {
        await onRun(inputText)
      },
    })
    focusInput()
    return
  }

  clearInput()
  clearAttachments()
  const runInput = appendStudioReferenceImages(next, attachments)
  try {
    await onRun(runInput)
  } catch {
    restoreInput(next)
    retainAttachments(attachments)
  }
  focusInput()
}