simple-module-chat 0.0.20__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple_module_chat-0.0.20.dist-info/METADATA +118 -0
- simple_module_chat-0.0.20.dist-info/RECORD +46 -0
- simple_module_chat-0.0.20.dist-info/WHEEL +4 -0
- simple_module_chat-0.0.20.dist-info/entry_points.txt +2 -0
- sm_chat/__init__.py +7 -0
- sm_chat/agent/__init__.py +3 -0
- sm_chat/agent/definition.py +72 -0
- sm_chat/agent/instructions.py +101 -0
- sm_chat/agent/toolset.py +102 -0
- sm_chat/contracts/__init__.py +21 -0
- sm_chat/contracts/registry.py +61 -0
- sm_chat/contracts/types.py +51 -0
- sm_chat/core/__init__.py +3 -0
- sm_chat/core/constants.py +55 -0
- sm_chat/core/settings.py +104 -0
- sm_chat/core/tenancy/__init__.py +125 -0
- sm_chat/core/tenancy/_mode.py +207 -0
- sm_chat/index.ts +26 -0
- sm_chat/locales/de.json +99 -0
- sm_chat/locales/en.json +99 -0
- sm_chat/module.py +85 -0
- sm_chat/package.json +20 -0
- sm_chat/pages/Index.tsx +108 -0
- sm_chat/ui/components/ApprovalCard.tsx +129 -0
- sm_chat/ui/components/ChatDrawerHost.tsx +248 -0
- sm_chat/ui/components/ChatExportActions.tsx +87 -0
- sm_chat/ui/components/ChatPanel.tsx +225 -0
- sm_chat/ui/components/MessageList.tsx +153 -0
- sm_chat/ui/components/SuggestedPrompts.tsx +65 -0
- sm_chat/ui/components/ThreadSidebar.tsx +222 -0
- sm_chat/ui/components/ToolCallPill.tsx +105 -0
- sm_chat/ui/hooks/threadMapping.ts +248 -0
- sm_chat/ui/hooks/useChatContext.ts +46 -0
- sm_chat/ui/hooks/useChatThread.ts +289 -0
- sm_chat/ui/hooks/useThreadStream.ts +87 -0
- sm_chat/ui/utils/api.ts +133 -0
- sm_chat/ui/utils/exportMarkdown.ts +66 -0
- sm_chat/ui/utils/i18n.ts +59 -0
- sm_chat/ui/utils/markdown.tsx +238 -0
- sm_chat/ui/utils/sse.ts +171 -0
- sm_chat/ui/utils/types.ts +111 -0
- sm_chat/web/__init__.py +3 -0
- sm_chat/web/endpoints/__init__.py +3 -0
- sm_chat/web/endpoints/api.py +117 -0
- sm_chat/web/endpoints/views.py +22 -0
- sm_chat/web/shared_props.py +33 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/** Reusable chat workspace pane shared by `/chat/` and `<ChatDrawerHost />`. */
|
|
2
|
+
|
|
3
|
+
import { router } from '@inertiajs/react';
|
|
4
|
+
import { AiQuotaError, type ParsedAiError } from '@simple-module-py/ai';
|
|
5
|
+
import { Alert, AlertDescription, AlertTitle } from '@simple-module-py/ui/components/ui/alert';
|
|
6
|
+
import { Button } from '@simple-module-py/ui/components/ui/button';
|
|
7
|
+
import { Textarea } from '@simple-module-py/ui/components/ui/textarea';
|
|
8
|
+
import { AlertTriangle, Bot, Send, Square } from 'lucide-react';
|
|
9
|
+
import type React from 'react';
|
|
10
|
+
import { useState } from 'react';
|
|
11
|
+
|
|
12
|
+
import { keys, useT } from '../utils/i18n';
|
|
13
|
+
import type {
|
|
14
|
+
ChatContextData,
|
|
15
|
+
ChatMessage,
|
|
16
|
+
ChatRequestMetadata,
|
|
17
|
+
PendingApprovalItem,
|
|
18
|
+
} from '../utils/types';
|
|
19
|
+
import { ChatExportActions } from './ChatExportActions';
|
|
20
|
+
import { MessageList } from './MessageList';
|
|
21
|
+
import { SuggestedPrompts } from './SuggestedPrompts';
|
|
22
|
+
|
|
23
|
+
export type ChatPanelProps = {
|
|
24
|
+
context: ChatContextData | null;
|
|
25
|
+
contextError?: string | null;
|
|
26
|
+
messages: ChatMessage[];
|
|
27
|
+
pendingApprovals: PendingApprovalItem[];
|
|
28
|
+
streaming: boolean;
|
|
29
|
+
error: ParsedAiError | null;
|
|
30
|
+
compact?: boolean;
|
|
31
|
+
threadTitle?: string;
|
|
32
|
+
requestMetadata?: ChatRequestMetadata;
|
|
33
|
+
onSendMessage: (text: string, requestMetadata?: ChatRequestMetadata) => void | Promise<void>;
|
|
34
|
+
onDecideApproval: (
|
|
35
|
+
approval: PendingApprovalItem,
|
|
36
|
+
approved: boolean,
|
|
37
|
+
denialReason?: string,
|
|
38
|
+
requestMetadata?: ChatRequestMetadata,
|
|
39
|
+
) => void | Promise<void>;
|
|
40
|
+
onCancel: () => void | Promise<void>;
|
|
41
|
+
onClearError: () => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function ChatPanel({
|
|
45
|
+
context,
|
|
46
|
+
contextError,
|
|
47
|
+
messages,
|
|
48
|
+
pendingApprovals,
|
|
49
|
+
streaming,
|
|
50
|
+
error,
|
|
51
|
+
compact = false,
|
|
52
|
+
threadTitle,
|
|
53
|
+
requestMetadata,
|
|
54
|
+
onSendMessage,
|
|
55
|
+
onDecideApproval,
|
|
56
|
+
onCancel,
|
|
57
|
+
onClearError,
|
|
58
|
+
}: ChatPanelProps) {
|
|
59
|
+
const { t } = useT();
|
|
60
|
+
const [draft, setDraft] = useState('');
|
|
61
|
+
|
|
62
|
+
const aiUnconfigured = context !== null && !context.ai_configured;
|
|
63
|
+
const hasConversation = messages.length > 0 || pendingApprovals.length > 0;
|
|
64
|
+
const canSend = draft.trim().length > 0 && !streaming && !aiUnconfigured;
|
|
65
|
+
|
|
66
|
+
const submitDraft = () => {
|
|
67
|
+
const text = draft.trim();
|
|
68
|
+
if (!text || streaming || aiUnconfigured) return;
|
|
69
|
+
setDraft('');
|
|
70
|
+
void onSendMessage(text, requestMetadata);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const handleSubmit = (event: React.FormEvent) => {
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
submitDraft();
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
79
|
+
if (event.key === 'Enter' && !event.shiftKey) {
|
|
80
|
+
event.preventDefault();
|
|
81
|
+
submitDraft();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const handleOpenAiSettings = (event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
86
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
87
|
+
event.preventDefault();
|
|
88
|
+
router.visit('/ai/');
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<div
|
|
93
|
+
className="flex min-h-0 flex-1 flex-col bg-background"
|
|
94
|
+
data-testid="chat-panel"
|
|
95
|
+
data-compact={compact ? 'true' : 'false'}
|
|
96
|
+
>
|
|
97
|
+
{aiUnconfigured && (
|
|
98
|
+
<div className="border-b px-4 py-3">
|
|
99
|
+
<Alert data-testid="chat-ai-unconfigured">
|
|
100
|
+
<AlertTriangle className="size-4" aria-hidden="true" />
|
|
101
|
+
<AlertTitle>{t(keys.chat.errors.not_configured_title)}</AlertTitle>
|
|
102
|
+
<AlertDescription className="space-y-2">
|
|
103
|
+
<p>{t(keys.chat.errors.not_configured_body)}</p>
|
|
104
|
+
<a
|
|
105
|
+
href="/ai/"
|
|
106
|
+
onClick={handleOpenAiSettings}
|
|
107
|
+
className="inline-flex text-xs font-medium text-primary underline underline-offset-2"
|
|
108
|
+
>
|
|
109
|
+
{t(keys.chat.errors.open_ai_settings)}
|
|
110
|
+
</a>
|
|
111
|
+
</AlertDescription>
|
|
112
|
+
</Alert>
|
|
113
|
+
</div>
|
|
114
|
+
)}
|
|
115
|
+
|
|
116
|
+
{contextError && (
|
|
117
|
+
<div className="border-b px-4 py-2">
|
|
118
|
+
<Alert variant="destructive">
|
|
119
|
+
<AlertDescription>{contextError}</AlertDescription>
|
|
120
|
+
</Alert>
|
|
121
|
+
</div>
|
|
122
|
+
)}
|
|
123
|
+
|
|
124
|
+
{error && (
|
|
125
|
+
<div className="border-b px-4 py-2">
|
|
126
|
+
<AiQuotaError
|
|
127
|
+
error={
|
|
128
|
+
error.isQuota
|
|
129
|
+
? {
|
|
130
|
+
status: 402,
|
|
131
|
+
body: {
|
|
132
|
+
detail: 'quota_exceeded',
|
|
133
|
+
meter: error.meter,
|
|
134
|
+
used: error.used,
|
|
135
|
+
limit: error.limit,
|
|
136
|
+
resets_at: error.resetsAt,
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
: error.code !== 'unknown'
|
|
140
|
+
? error.code
|
|
141
|
+
: error.message
|
|
142
|
+
}
|
|
143
|
+
onDismiss={onClearError}
|
|
144
|
+
data-testid="chat-error-banner"
|
|
145
|
+
/>
|
|
146
|
+
</div>
|
|
147
|
+
)}
|
|
148
|
+
|
|
149
|
+
{hasConversation ? (
|
|
150
|
+
<>
|
|
151
|
+
<ChatExportActions messages={messages} threadTitle={threadTitle} />
|
|
152
|
+
<MessageList
|
|
153
|
+
messages={messages}
|
|
154
|
+
pendingApprovals={pendingApprovals}
|
|
155
|
+
streaming={streaming}
|
|
156
|
+
onDecideApproval={(approval, approved, reason) =>
|
|
157
|
+
void onDecideApproval(approval, approved, reason, requestMetadata)
|
|
158
|
+
}
|
|
159
|
+
/>
|
|
160
|
+
</>
|
|
161
|
+
) : (
|
|
162
|
+
<div
|
|
163
|
+
className={`flex flex-1 flex-col items-center justify-center gap-5 overflow-y-auto px-6 text-center ${
|
|
164
|
+
compact ? 'py-6' : 'py-12'
|
|
165
|
+
}`}
|
|
166
|
+
data-testid="chat-empty-state"
|
|
167
|
+
>
|
|
168
|
+
<div className="flex size-11 items-center justify-center rounded-full border bg-muted/60">
|
|
169
|
+
<Bot className="size-5 text-primary" aria-hidden="true" />
|
|
170
|
+
</div>
|
|
171
|
+
<div className="max-w-md space-y-1.5">
|
|
172
|
+
<h2 className="text-base font-semibold text-foreground">{t(keys.chat.empty.title)}</h2>
|
|
173
|
+
<p className="text-xs text-muted-foreground">{t(keys.chat.empty.description)}</p>
|
|
174
|
+
</div>
|
|
175
|
+
{context && (
|
|
176
|
+
<div className="w-full max-w-lg">
|
|
177
|
+
<SuggestedPrompts
|
|
178
|
+
providers={context.providers}
|
|
179
|
+
prompts={context.suggested_prompts}
|
|
180
|
+
disabled={streaming || aiUnconfigured}
|
|
181
|
+
onSelect={(prompt) => void onSendMessage(prompt, requestMetadata)}
|
|
182
|
+
/>
|
|
183
|
+
</div>
|
|
184
|
+
)}
|
|
185
|
+
</div>
|
|
186
|
+
)}
|
|
187
|
+
|
|
188
|
+
<form onSubmit={handleSubmit} className="border-t bg-background px-4 py-3">
|
|
189
|
+
<div className="flex items-end gap-2">
|
|
190
|
+
<Textarea
|
|
191
|
+
value={draft}
|
|
192
|
+
disabled={aiUnconfigured}
|
|
193
|
+
onChange={(event) => setDraft(event.target.value)}
|
|
194
|
+
onKeyDown={handleKeyDown}
|
|
195
|
+
rows={compact ? 2 : 3}
|
|
196
|
+
placeholder={t(keys.chat.composer.placeholder)}
|
|
197
|
+
aria-label={t(keys.chat.composer.placeholder)}
|
|
198
|
+
className="min-h-[44px] max-h-36 flex-1 resize-none text-sm"
|
|
199
|
+
data-testid="chat-composer-input"
|
|
200
|
+
/>
|
|
201
|
+
{streaming ? (
|
|
202
|
+
<Button
|
|
203
|
+
type="button"
|
|
204
|
+
variant="destructive"
|
|
205
|
+
size="sm"
|
|
206
|
+
onClick={() => void onCancel()}
|
|
207
|
+
data-testid="chat-stop-button"
|
|
208
|
+
>
|
|
209
|
+
<Square className="mr-1.5 size-3.5" aria-hidden="true" />
|
|
210
|
+
{t(keys.chat.composer.stop)}
|
|
211
|
+
</Button>
|
|
212
|
+
) : (
|
|
213
|
+
<Button type="submit" size="sm" disabled={!canSend} data-testid="chat-send-button">
|
|
214
|
+
<Send className="mr-1.5 size-3.5" aria-hidden="true" />
|
|
215
|
+
{t(keys.chat.composer.send)}
|
|
216
|
+
</Button>
|
|
217
|
+
)}
|
|
218
|
+
</div>
|
|
219
|
+
{!compact && (
|
|
220
|
+
<p className="mt-1.5 text-[11px] text-muted-foreground">{t(keys.chat.composer.hint)}</p>
|
|
221
|
+
)}
|
|
222
|
+
</form>
|
|
223
|
+
</div>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** Scrollable transcript of user/assistant messages, tool pills, and HITL cards. */
|
|
2
|
+
|
|
3
|
+
import { Button } from '@simple-module-py/ui/components/ui/button';
|
|
4
|
+
import { Bot, Check, Copy, Loader2, User } from 'lucide-react';
|
|
5
|
+
import { useEffect, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { keys, useT } from '../utils/i18n';
|
|
8
|
+
import { SafeMarkdown } from '../utils/markdown';
|
|
9
|
+
import type { ChatMessage, PendingApprovalItem } from '../utils/types';
|
|
10
|
+
import { ApprovalCard } from './ApprovalCard';
|
|
11
|
+
import { ToolCallPill } from './ToolCallPill';
|
|
12
|
+
|
|
13
|
+
export type MessageListProps = {
|
|
14
|
+
messages: ChatMessage[];
|
|
15
|
+
pendingApprovals: PendingApprovalItem[];
|
|
16
|
+
streaming: boolean;
|
|
17
|
+
onDecideApproval: (
|
|
18
|
+
approval: PendingApprovalItem,
|
|
19
|
+
approved: boolean,
|
|
20
|
+
denialReason?: string,
|
|
21
|
+
) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function MessageList({
|
|
25
|
+
messages,
|
|
26
|
+
pendingApprovals,
|
|
27
|
+
streaming,
|
|
28
|
+
onDecideApproval,
|
|
29
|
+
}: MessageListProps) {
|
|
30
|
+
const { t } = useT();
|
|
31
|
+
const bottomRef = useRef<HTMLDivElement | null>(null);
|
|
32
|
+
const [copiedId, setCopiedId] = useState<string | null>(null);
|
|
33
|
+
|
|
34
|
+
const lastMessage = messages[messages.length - 1];
|
|
35
|
+
const scrollDep = `${messages.length}:${lastMessage?.content.length ?? 0}:${pendingApprovals.length}`;
|
|
36
|
+
|
|
37
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: scrollDep intentionally triggers auto-scroll on stream deltas
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
bottomRef.current?.scrollIntoView?.({ block: 'end' });
|
|
40
|
+
}, [scrollDep]);
|
|
41
|
+
|
|
42
|
+
const handleCopy = async (msg: ChatMessage) => {
|
|
43
|
+
try {
|
|
44
|
+
await navigator.clipboard?.writeText(msg.content);
|
|
45
|
+
setCopiedId(msg.id);
|
|
46
|
+
setTimeout(() => setCopiedId((prev) => (prev === msg.id ? null : prev)), 1500);
|
|
47
|
+
} catch {
|
|
48
|
+
// Clipboard API unavailable in some non-secure contexts.
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<div className="flex-1 space-y-4 overflow-y-auto px-4 py-4" data-testid="chat-message-list">
|
|
54
|
+
{messages.map((msg, index) => {
|
|
55
|
+
const isUser = msg.role === 'user';
|
|
56
|
+
const isLast = index === messages.length - 1;
|
|
57
|
+
const isStreamingThis = streaming && !isUser && isLast;
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<div
|
|
61
|
+
key={msg.id}
|
|
62
|
+
className={`flex gap-3 ${isUser ? 'justify-end' : 'justify-start'}`}
|
|
63
|
+
data-testid={`chat-message-${msg.role}`}
|
|
64
|
+
>
|
|
65
|
+
{!isUser && (
|
|
66
|
+
<div className="mt-0.5 flex size-7 shrink-0 items-center justify-center rounded-full border bg-muted">
|
|
67
|
+
<Bot className="size-4 text-muted-foreground" aria-hidden="true" />
|
|
68
|
+
</div>
|
|
69
|
+
)}
|
|
70
|
+
<div className={`max-w-[85%] space-y-2 ${isUser ? 'items-end' : 'items-start'}`}>
|
|
71
|
+
<div
|
|
72
|
+
className={
|
|
73
|
+
isUser
|
|
74
|
+
? 'rounded-2xl rounded-br-sm bg-primary px-3.5 py-2 text-sm text-primary-foreground whitespace-pre-wrap break-words'
|
|
75
|
+
: 'rounded-2xl rounded-bl-sm border bg-card px-3.5 py-2.5 text-card-foreground shadow-xs'
|
|
76
|
+
}
|
|
77
|
+
>
|
|
78
|
+
{!isUser && msg.toolCalls && msg.toolCalls.length > 0 && (
|
|
79
|
+
<div className="mb-2 space-y-1.5">
|
|
80
|
+
{msg.toolCalls.map((tc) => (
|
|
81
|
+
<ToolCallPill key={tc.toolCallId} toolCall={tc} />
|
|
82
|
+
))}
|
|
83
|
+
</div>
|
|
84
|
+
)}
|
|
85
|
+
|
|
86
|
+
{isUser ? (
|
|
87
|
+
msg.content
|
|
88
|
+
) : msg.content ? (
|
|
89
|
+
<SafeMarkdown content={msg.content} />
|
|
90
|
+
) : isStreamingThis ? (
|
|
91
|
+
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
92
|
+
<Loader2 className="size-3.5 animate-spin" aria-hidden="true" />
|
|
93
|
+
<span>{t(keys.chat.messages.thinking)}</span>
|
|
94
|
+
</div>
|
|
95
|
+
) : null}
|
|
96
|
+
|
|
97
|
+
{msg.cancelled && (
|
|
98
|
+
<p className="mt-1.5 text-xs italic text-muted-foreground">
|
|
99
|
+
{t(keys.chat.messages.cancelled)}
|
|
100
|
+
</p>
|
|
101
|
+
)}
|
|
102
|
+
</div>
|
|
103
|
+
|
|
104
|
+
{!isUser && msg.content && !isStreamingThis && (
|
|
105
|
+
<div className="flex items-center gap-1 px-1">
|
|
106
|
+
<Button
|
|
107
|
+
type="button"
|
|
108
|
+
variant="ghost"
|
|
109
|
+
size="xs"
|
|
110
|
+
className="h-6 px-1.5 text-[11px] text-muted-foreground"
|
|
111
|
+
onClick={() => void handleCopy(msg)}
|
|
112
|
+
>
|
|
113
|
+
{copiedId === msg.id ? (
|
|
114
|
+
<>
|
|
115
|
+
<Check className="mr-1 size-3" aria-hidden="true" />
|
|
116
|
+
{t(keys.chat.messages.copied)}
|
|
117
|
+
</>
|
|
118
|
+
) : (
|
|
119
|
+
<>
|
|
120
|
+
<Copy className="mr-1 size-3" aria-hidden="true" />
|
|
121
|
+
{t(keys.chat.messages.copy)}
|
|
122
|
+
</>
|
|
123
|
+
)}
|
|
124
|
+
</Button>
|
|
125
|
+
</div>
|
|
126
|
+
)}
|
|
127
|
+
</div>
|
|
128
|
+
{isUser && (
|
|
129
|
+
<div className="mt-0.5 flex size-7 shrink-0 items-center justify-center rounded-full bg-primary/10">
|
|
130
|
+
<User className="size-4 text-primary" aria-hidden="true" />
|
|
131
|
+
</div>
|
|
132
|
+
)}
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
})}
|
|
136
|
+
|
|
137
|
+
{pendingApprovals.length > 0 && (
|
|
138
|
+
<div className="space-y-3 pt-1">
|
|
139
|
+
{pendingApprovals.map((approval) => (
|
|
140
|
+
<ApprovalCard
|
|
141
|
+
key={approval.interruptId}
|
|
142
|
+
approval={approval}
|
|
143
|
+
disabled={streaming}
|
|
144
|
+
onDecide={onDecideApproval}
|
|
145
|
+
/>
|
|
146
|
+
))}
|
|
147
|
+
</div>
|
|
148
|
+
)}
|
|
149
|
+
|
|
150
|
+
<div ref={bottomRef} />
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Starter prompt chips and connected module provider badges. */
|
|
2
|
+
|
|
3
|
+
import { Badge } from '@simple-module-py/ui/components/ui/badge';
|
|
4
|
+
import { Sparkles } from 'lucide-react';
|
|
5
|
+
|
|
6
|
+
import { keys, useT } from '../utils/i18n';
|
|
7
|
+
import type { ChatProviderInfo, ChatSuggestedPrompt } from '../utils/types';
|
|
8
|
+
|
|
9
|
+
export type SuggestedPromptsProps = {
|
|
10
|
+
providers: ChatProviderInfo[];
|
|
11
|
+
prompts: ChatSuggestedPrompt[];
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
onSelect: (prompt: string) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function SuggestedPrompts({
|
|
17
|
+
providers,
|
|
18
|
+
prompts,
|
|
19
|
+
disabled = false,
|
|
20
|
+
onSelect,
|
|
21
|
+
}: SuggestedPromptsProps) {
|
|
22
|
+
const { t } = useT();
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<div className="space-y-4" data-testid="chat-suggested-prompts">
|
|
26
|
+
{providers.length > 0 ? (
|
|
27
|
+
<div className="flex flex-wrap justify-center gap-1.5">
|
|
28
|
+
{providers.map((provider) => (
|
|
29
|
+
<Badge key={provider.id} variant="secondary" className="text-xs font-normal">
|
|
30
|
+
{provider.title}
|
|
31
|
+
</Badge>
|
|
32
|
+
))}
|
|
33
|
+
</div>
|
|
34
|
+
) : (
|
|
35
|
+
<p className="text-center text-xs text-muted-foreground">
|
|
36
|
+
{t(keys.chat.empty.no_providers)}
|
|
37
|
+
</p>
|
|
38
|
+
)}
|
|
39
|
+
|
|
40
|
+
{prompts.length > 0 ? (
|
|
41
|
+
<div className="space-y-2">
|
|
42
|
+
<p className="text-center text-xs font-medium text-muted-foreground">
|
|
43
|
+
{t(keys.chat.prompts.title)}
|
|
44
|
+
</p>
|
|
45
|
+
<div className="flex flex-wrap justify-center gap-2">
|
|
46
|
+
{prompts.map((item) => (
|
|
47
|
+
<button
|
|
48
|
+
key={`${item.provider_id}-${item.id}`}
|
|
49
|
+
type="button"
|
|
50
|
+
disabled={disabled}
|
|
51
|
+
onClick={() => onSelect(item.prompt)}
|
|
52
|
+
className="inline-flex items-center gap-1.5 rounded-full border bg-background px-3 py-1.5 text-xs text-foreground shadow-2xs transition-colors hover:bg-accent hover:text-accent-foreground disabled:pointer-events-none disabled:opacity-50"
|
|
53
|
+
>
|
|
54
|
+
<Sparkles className="size-3 shrink-0 text-primary" aria-hidden="true" />
|
|
55
|
+
<span>{item.label}</span>
|
|
56
|
+
</button>
|
|
57
|
+
))}
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
) : (
|
|
61
|
+
<p className="text-center text-xs text-muted-foreground">{t(keys.chat.prompts.empty)}</p>
|
|
62
|
+
)}
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/** Thread history sidebar for the dedicated `/chat/` workspace page. */
|
|
2
|
+
|
|
3
|
+
import { Button } from '@simple-module-py/ui/components/ui/button';
|
|
4
|
+
import { MessageSquare, Pencil, Plus, Search, Trash2, X } from 'lucide-react';
|
|
5
|
+
import { useMemo, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { keys, useT } from '../utils/i18n';
|
|
8
|
+
import type { ThreadSummary } from '../utils/types';
|
|
9
|
+
|
|
10
|
+
export type ThreadSidebarProps = {
|
|
11
|
+
threads: ThreadSummary[];
|
|
12
|
+
loading: boolean;
|
|
13
|
+
activeThreadId: string | null;
|
|
14
|
+
onSelect: (threadId: string) => void;
|
|
15
|
+
onNewChat: () => void;
|
|
16
|
+
onDelete: (threadId: string) => void;
|
|
17
|
+
onRename?: (threadId: string, title: string) => void | Promise<void>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function formatShortDate(iso: string): string {
|
|
21
|
+
const date = new Date(iso);
|
|
22
|
+
if (Number.isNaN(date.getTime())) return '';
|
|
23
|
+
return new Intl.DateTimeFormat(undefined, {
|
|
24
|
+
month: 'short',
|
|
25
|
+
day: 'numeric',
|
|
26
|
+
}).format(date);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function ThreadSidebar({
|
|
30
|
+
threads,
|
|
31
|
+
loading,
|
|
32
|
+
activeThreadId,
|
|
33
|
+
onSelect,
|
|
34
|
+
onNewChat,
|
|
35
|
+
onDelete,
|
|
36
|
+
onRename,
|
|
37
|
+
}: ThreadSidebarProps) {
|
|
38
|
+
const { t } = useT();
|
|
39
|
+
const [query, setQuery] = useState('');
|
|
40
|
+
const [editingId, setEditingId] = useState<string | null>(null);
|
|
41
|
+
const [editingValue, setEditingValue] = useState('');
|
|
42
|
+
const cancelRenameRef = useRef(false);
|
|
43
|
+
|
|
44
|
+
const untitledLabel = t(keys.chat.sidebar.untitled);
|
|
45
|
+
const trimmedQuery = query.trim().toLowerCase();
|
|
46
|
+
const visibleThreads = useMemo(() => {
|
|
47
|
+
if (!trimmedQuery) return threads;
|
|
48
|
+
return threads.filter((thread) => {
|
|
49
|
+
const label = (thread.title.trim() || untitledLabel).toLowerCase();
|
|
50
|
+
return label.includes(trimmedQuery);
|
|
51
|
+
});
|
|
52
|
+
}, [threads, trimmedQuery, untitledLabel]);
|
|
53
|
+
|
|
54
|
+
const startRename = (thread: ThreadSummary) => {
|
|
55
|
+
cancelRenameRef.current = false;
|
|
56
|
+
setEditingId(thread.id);
|
|
57
|
+
setEditingValue(thread.title.trim());
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const commitRename = (thread: ThreadSummary) => {
|
|
61
|
+
if (cancelRenameRef.current) {
|
|
62
|
+
cancelRenameRef.current = false;
|
|
63
|
+
setEditingId(null);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const nextTitle = editingValue.trim();
|
|
67
|
+
setEditingId(null);
|
|
68
|
+
if (nextTitle && nextTitle !== thread.title.trim()) {
|
|
69
|
+
void onRename?.(thread.id, nextTitle);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const cancelRename = () => {
|
|
74
|
+
cancelRenameRef.current = true;
|
|
75
|
+
setEditingId(null);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<aside
|
|
80
|
+
className="flex h-full w-64 shrink-0 flex-col border-r bg-muted/20"
|
|
81
|
+
data-testid="chat-thread-sidebar"
|
|
82
|
+
>
|
|
83
|
+
<div className="flex items-center justify-between gap-2 border-b px-3 py-2.5">
|
|
84
|
+
<h2 className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
|
85
|
+
{t(keys.chat.sidebar.title)}
|
|
86
|
+
</h2>
|
|
87
|
+
<Button type="button" size="xs" variant="outline" onClick={onNewChat}>
|
|
88
|
+
<Plus className="mr-1 size-3.5" aria-hidden="true" />
|
|
89
|
+
{t(keys.chat.sidebar.new_chat)}
|
|
90
|
+
</Button>
|
|
91
|
+
</div>
|
|
92
|
+
|
|
93
|
+
<div className="border-b px-2.5 py-2">
|
|
94
|
+
<div className="relative flex items-center">
|
|
95
|
+
<Search
|
|
96
|
+
className="pointer-events-none absolute left-2 size-3.5 text-muted-foreground"
|
|
97
|
+
aria-hidden="true"
|
|
98
|
+
/>
|
|
99
|
+
<input
|
|
100
|
+
type="text"
|
|
101
|
+
value={query}
|
|
102
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
103
|
+
placeholder={t(keys.chat.sidebar.search_placeholder)}
|
|
104
|
+
aria-label={t(keys.chat.sidebar.search_placeholder)}
|
|
105
|
+
data-testid="chat-thread-filter"
|
|
106
|
+
className="h-7 w-full rounded-md border bg-background pl-7 pr-7 text-xs placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
|
107
|
+
/>
|
|
108
|
+
{query && (
|
|
109
|
+
<button
|
|
110
|
+
type="button"
|
|
111
|
+
onClick={() => setQuery('')}
|
|
112
|
+
aria-label={t(keys.chat.sidebar.clear_search)}
|
|
113
|
+
data-testid="chat-thread-filter-clear"
|
|
114
|
+
className="absolute right-1.5 rounded p-0.5 text-muted-foreground hover:text-foreground"
|
|
115
|
+
>
|
|
116
|
+
<X className="size-3" aria-hidden="true" />
|
|
117
|
+
</button>
|
|
118
|
+
)}
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
|
|
122
|
+
<div className="flex-1 overflow-y-auto p-2">
|
|
123
|
+
{loading && threads.length === 0 ? (
|
|
124
|
+
<p className="px-2 py-4 text-xs text-muted-foreground">{t(keys.chat.sidebar.loading)}</p>
|
|
125
|
+
) : threads.length === 0 ? (
|
|
126
|
+
<p className="px-2 py-4 text-xs text-muted-foreground">{t(keys.chat.sidebar.empty)}</p>
|
|
127
|
+
) : visibleThreads.length === 0 ? (
|
|
128
|
+
<p
|
|
129
|
+
className="px-2 py-4 text-xs text-muted-foreground"
|
|
130
|
+
data-testid="chat-thread-no-matches"
|
|
131
|
+
>
|
|
132
|
+
{t(keys.chat.sidebar.no_matches)}
|
|
133
|
+
</p>
|
|
134
|
+
) : (
|
|
135
|
+
<ul className="space-y-1">
|
|
136
|
+
{visibleThreads.map((thread) => {
|
|
137
|
+
const active = thread.id === activeThreadId;
|
|
138
|
+
const title = thread.title.trim() || untitledLabel;
|
|
139
|
+
const dateLabel = formatShortDate(thread.updated_at || thread.created_at);
|
|
140
|
+
const isEditing = editingId === thread.id;
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<li key={thread.id} className="group flex items-center gap-1">
|
|
144
|
+
{isEditing ? (
|
|
145
|
+
<input
|
|
146
|
+
type="text"
|
|
147
|
+
value={editingValue}
|
|
148
|
+
onChange={(event) => setEditingValue(event.target.value)}
|
|
149
|
+
onKeyDown={(event) => {
|
|
150
|
+
if (event.key === 'Enter') {
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
commitRename(thread);
|
|
153
|
+
} else if (event.key === 'Escape') {
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
cancelRename();
|
|
156
|
+
}
|
|
157
|
+
}}
|
|
158
|
+
onBlur={() => commitRename(thread)}
|
|
159
|
+
placeholder={t(keys.chat.sidebar.rename_placeholder)}
|
|
160
|
+
aria-label={t(keys.chat.sidebar.rename_placeholder)}
|
|
161
|
+
data-testid="chat-thread-rename-input"
|
|
162
|
+
// biome-ignore lint/a11y/noAutofocus: focus inline rename input on edit trigger
|
|
163
|
+
autoFocus
|
|
164
|
+
className="h-7 min-w-0 flex-1 rounded-md border bg-background px-2 text-xs focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
|
165
|
+
/>
|
|
166
|
+
) : (
|
|
167
|
+
<>
|
|
168
|
+
<button
|
|
169
|
+
type="button"
|
|
170
|
+
onClick={() => onSelect(thread.id)}
|
|
171
|
+
aria-current={active ? 'true' : undefined}
|
|
172
|
+
className={`flex min-w-0 flex-1 items-center gap-2 rounded-md px-2.5 py-2 text-left text-xs transition-colors ${
|
|
173
|
+
active
|
|
174
|
+
? 'bg-accent font-medium text-accent-foreground'
|
|
175
|
+
: 'text-foreground hover:bg-muted'
|
|
176
|
+
}`}
|
|
177
|
+
>
|
|
178
|
+
<MessageSquare
|
|
179
|
+
className="size-3.5 shrink-0 text-muted-foreground"
|
|
180
|
+
aria-hidden="true"
|
|
181
|
+
/>
|
|
182
|
+
<span className="min-w-0 flex-1 truncate">{title}</span>
|
|
183
|
+
{dateLabel && (
|
|
184
|
+
<span className="shrink-0 text-[10px] text-muted-foreground">
|
|
185
|
+
{dateLabel}
|
|
186
|
+
</span>
|
|
187
|
+
)}
|
|
188
|
+
</button>
|
|
189
|
+
{onRename && (
|
|
190
|
+
<Button
|
|
191
|
+
type="button"
|
|
192
|
+
variant="ghost"
|
|
193
|
+
size="icon-xs"
|
|
194
|
+
aria-label={t(keys.chat.sidebar.rename)}
|
|
195
|
+
data-testid="chat-thread-rename-btn"
|
|
196
|
+
className="shrink-0 text-muted-foreground opacity-60 hover:text-foreground hover:opacity-100"
|
|
197
|
+
onClick={() => startRename(thread)}
|
|
198
|
+
>
|
|
199
|
+
<Pencil className="size-3.5" aria-hidden="true" />
|
|
200
|
+
</Button>
|
|
201
|
+
)}
|
|
202
|
+
<Button
|
|
203
|
+
type="button"
|
|
204
|
+
variant="ghost"
|
|
205
|
+
size="icon-xs"
|
|
206
|
+
aria-label={t(keys.chat.sidebar.delete)}
|
|
207
|
+
className="shrink-0 text-muted-foreground opacity-60 hover:text-destructive hover:opacity-100"
|
|
208
|
+
onClick={() => onDelete(thread.id)}
|
|
209
|
+
>
|
|
210
|
+
<Trash2 className="size-3.5" aria-hidden="true" />
|
|
211
|
+
</Button>
|
|
212
|
+
</>
|
|
213
|
+
)}
|
|
214
|
+
</li>
|
|
215
|
+
);
|
|
216
|
+
})}
|
|
217
|
+
</ul>
|
|
218
|
+
)}
|
|
219
|
+
</div>
|
|
220
|
+
</aside>
|
|
221
|
+
);
|
|
222
|
+
}
|