File size: 6,869 Bytes
bfbf1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d6f04b
34afd2e
bfbf1a7
 
 
 
 
 
 
 
 
 
 
 
34afd2e
 
bfbf1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
34afd2e
 
bfbf1a7
 
 
 
 
 
 
 
 
 
 
96ac62a
 
 
 
 
 
 
 
 
 
 
 
0d6f04b
 
96ac62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34afd2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96ac62a
 
 
 
 
bfbf1a7
96ac62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfbf1a7
 
 
 
 
 
96ac62a
bfbf1a7
96ac62a
bfbf1a7
 
96ac62a
bfbf1a7
96ac62a
 
bfbf1a7
 
 
 
 
 
96ac62a
bfbf1a7
96ac62a
bfbf1a7
 
96ac62a
bfbf1a7
96ac62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfbf1a7
96ac62a
 
bfbf1a7
 
96ac62a
bfbf1a7
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use client';

import * as React from 'react';
import { type UseChatHelpers } from 'ai/react';
import Textarea from 'react-textarea-autosize';

import { Button } from '@/components/ui/Button';
import { MessageBase } from '../../lib/types';
import { useEnterSubmit } from '@/lib/hooks/useEnterSubmit';
import Img from '../ui/Img';
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from '@/components/ui/Tooltip';
import {
  IconArrowDown,
  IconArrowElbow,
  IconRefresh,
  IconStop,
} from '@/components/ui/Icons';
import { cn } from '@/lib/utils';
import { generateInputImageMarkdown } from '@/lib/messageUtils';
import { Switch } from '../ui/Switch';

export interface ComposerProps
  extends Pick<
    UseChatHelpers,
    'append' | 'isLoading' | 'reload' | 'stop' | 'input' | 'setInput'
  > {
  id?: string;
  title?: string;
  messages: MessageBase[];
  url?: string;
  isAtBottom: boolean;
  scrollToBottom: () => void;
  enableSelfReflection: boolean;
  setEnableSelfReflection: (value: boolean) => void;
}

export function Composer({
  id,
  title,
  isLoading,
  stop,
  append,
  reload,
  input,
  setInput,
  messages,
  isAtBottom,
  scrollToBottom,
  enableSelfReflection,
  setEnableSelfReflection,
  url,
}: ComposerProps) {
  const { formRef, onKeyDown } = useEnterSubmit();
  const inputRef = React.useRef<HTMLTextAreaElement>(null);
  React.useEffect(() => {
    if (inputRef.current) {
      inputRef.current.focus();
    }
  }, []);

  return (
    // <div className="fixed inset-x-0 bottom-0 w-full animate-in duration-300 ease-in-out peer-[[data-state=open]]:group-[]:lg:pl-[250px] peer-[[data-state=open]]:group-[]:xl:pl-[300px] h-[178px]">
    <div className="mx-auto sm:max-w-3xl sm:px-4 h-full">
      <div className="px-4 py-2 space-y-4 border-t shadow-lg bg-background sm:rounded-t-xl sm:border md:py-4 h-full">
        <form
          onSubmit={async e => {
            e.preventDefault();
            if (!input?.trim()) {
              return;
            }
            setInput('');
            await append({
              id,
              content:
                input + (url ? '\n\n' + generateInputImageMarkdown(url) : ''),
              role: 'user',
            });
            scrollToBottom();
          }}
          ref={formRef}
          className="h-full"
        >
          <div className="relative flex px-8 pl-2 overflow-hidden size-full bg-background sm:rounded-md sm:border sm:px-12 sm:pl-2 items-start">
            {url && (
              <div className="w-1/5 p-2 h-full flex items-center justify-center relative">
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Img
                      src={url}
                      className="cursor-zoom-in"
                      alt="preview-image"
                    />
                  </TooltipTrigger>
                  <TooltipContent>
                    <Img
                      src={url}
                      className="m-2"
                      quality={100}
                      width={500}
                      alt="zoomed-in-image"
                    />
                  </TooltipContent>
                </Tooltip>
              </div>
            )}
            <div className="flex flex-col gap-2 w-4/5 px-4">
              <Textarea
                ref={inputRef}
                tabIndex={0}
                onKeyDown={onKeyDown}
                rows={1}
                value={input}
                disabled={isLoading}
                onChange={e => setInput(e.target.value)}
                placeholder={
                  isLoading
                    ? 'Vision Agent is thinking...'
                    : 'Ask question about the image.'
                }
                spellCheck={false}
                className="min-h-[60px] resize-none bg-transparent py-[1.3em] focus-within:outline-none sm:text-sm"
              />
              <div className="flex items-center gap-2 mt-4">
                <Switch
                  checked={enableSelfReflection}
                  onCheckedChange={checked => {
                    setEnableSelfReflection(checked);
                  }}
                />
                <p className="text-sm">Self Reflection</p>
              </div>
            </div>
            {/* Scroll to bottom Icon */}
            <div
              className={cn(
                'absolute top-3 right-4 transition-opacity duration-300',
                isAtBottom ? 'opacity-0' : 'opacity-100',
              )}
            >
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    variant="outline"
                    size="icon"
                    className="bg-background"
                    onClick={() => scrollToBottom()}
                  >
                    <IconArrowDown />
                  </Button>
                </TooltipTrigger>
                <TooltipContent>Scroll to bottom</TooltipContent>
              </Tooltip>
            </div>
            {/* Stop / Regenerate Icon */}
            <div className="absolute bottom-14 right-4">
              {isLoading ? (
                <Tooltip>
                  <TooltipTrigger asChild>
                    <Button
                      variant="outline"
                      size="icon"
                      className="bg-background"
                      onClick={() => stop()}
                    >
                      <IconStop />
                    </Button>
                  </TooltipTrigger>
                  <TooltipContent>Stop generating</TooltipContent>
                </Tooltip>
              ) : (
                messages?.length >= 2 && (
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <Button
                        variant="outline"
                        size="icon"
                        className="bg-background"
                        onClick={() => reload()}
                      >
                        <IconRefresh />
                      </Button>
                    </TooltipTrigger>
                    <TooltipContent>Regenerate response</TooltipContent>
                  </Tooltip>
                )
              )}
            </div>
            {/* Submit Icon */}
            <div className="absolute bottom-3 right-4">
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    type="submit"
                    size="icon"
                    disabled={isLoading || input === ''}
                  >
                    <IconArrowElbow />
                  </Button>
                </TooltipTrigger>
                <TooltipContent>Send message</TooltipContent>
              </Tooltip>
            </div>
          </div>
        </form>
      </div>
    </div>
    // </div>
  );
}