openadminjs 0.1.7 → 0.1.9
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.
- package/README.md +114 -18
- package/dist/create-project.js +27 -4
- package/dist/create-project.test.js +57 -41
- package/dist/index.js +16 -5
- package/package.json +4 -2
- package/template/.env.example +2 -2
- package/template/README.md +24 -5
- package/template/apps/admin/app/api/auth/login/route.ts +51 -0
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +46 -0
- package/template/apps/admin/app/api-tokens/page.tsx +253 -0
- package/template/apps/admin/app/audit-logs/page.tsx +5 -0
- package/template/apps/admin/app/dashboard/page.tsx +392 -0
- package/template/apps/admin/app/files/page.tsx +5 -0
- package/template/apps/admin/app/globals.css +206 -0
- package/template/apps/admin/app/jobs/page.tsx +19 -0
- package/template/apps/admin/app/layout.tsx +57 -0
- package/template/apps/admin/app/login/page.test.tsx +18 -0
- package/template/apps/admin/app/login/page.tsx +161 -0
- package/template/apps/admin/app/login/safe-next-param.ts +4 -0
- package/template/apps/admin/app/notifications/page.tsx +146 -0
- package/template/apps/admin/app/page.tsx +5 -0
- package/template/apps/admin/app/plugins/page.tsx +407 -0
- package/template/apps/admin/app/profile/page.tsx +116 -0
- package/template/apps/admin/app/resources/[resource]/[id]/edit/page.tsx +45 -0
- package/template/apps/admin/app/resources/[resource]/[id]/page.tsx +449 -0
- package/template/apps/admin/app/resources/[resource]/create/page.tsx +34 -0
- package/template/apps/admin/app/resources/[resource]/page.tsx +100 -0
- package/template/apps/admin/app/settings/page.tsx +233 -0
- package/template/apps/admin/components/ai-assistant-widget.tsx +684 -0
- package/template/apps/admin/components/app-shell.tsx +305 -0
- package/template/apps/admin/components/auth-guard.test.tsx +55 -0
- package/template/apps/admin/components/auth-guard.tsx +39 -0
- package/template/apps/admin/components/jobs-panel.tsx +103 -0
- package/template/apps/admin/components/logo.tsx +30 -0
- package/template/apps/admin/components/resource-form.tsx +349 -0
- package/template/apps/admin/components/resource-table.tsx +277 -0
- package/template/apps/admin/lib/ai-events.ts +23 -0
- package/template/apps/admin/lib/api.test.ts +118 -0
- package/template/apps/admin/lib/api.ts +135 -0
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +79 -0
- package/template/apps/admin/lib/brand.ts +14 -0
- package/template/apps/admin/lib/locale.ts +49 -0
- package/template/apps/admin/lib/tokens.ts +93 -0
- package/template/apps/admin/middleware.ts +58 -0
- package/template/apps/admin/next-env.d.ts +6 -0
- package/template/apps/admin/next.config.mjs +7 -0
- package/template/apps/admin/package.json +44 -0
- package/template/apps/admin/postcss.config.mjs +6 -0
- package/template/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/apps/admin/public/brand/favicon.svg +1 -0
- package/template/apps/admin/public/brand/og-image.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/apps/admin/tailwind.config.ts +37 -0
- package/template/apps/admin/tsconfig.json +23 -0
- package/template/apps/admin/vitest.config.ts +7 -0
- package/template/apps/api/package.json +56 -0
- package/template/apps/api/plugins.manifest.example.json +34 -0
- package/template/apps/api/plugins.manifest.json +4 -0
- package/template/apps/api/src/access/catalog.ts +112 -0
- package/template/apps/api/src/access/derive-permissions.test.ts +26 -0
- package/template/apps/api/src/access/derive-permissions.ts +36 -0
- package/template/apps/api/src/access/public-catalog.ts +28 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +125 -0
- package/template/apps/api/src/admin/admin-ai.events.ts +23 -0
- package/template/apps/api/src/admin/admin-ai.service.ts +1271 -0
- package/template/apps/api/src/admin/admin-analytics.service.ts +164 -0
- package/template/apps/api/src/admin/admin-plugins.controller.ts +32 -0
- package/template/apps/api/src/admin/admin-plugins.service.ts +267 -0
- package/template/apps/api/src/admin/admin-resource.service.test.ts +118 -0
- package/template/apps/api/src/admin/admin-resource.service.ts +375 -0
- package/template/apps/api/src/admin/admin.controller.ts +72 -0
- package/template/apps/api/src/admin/admin.module.ts +30 -0
- package/template/apps/api/src/admin/dto/plugins.dto.ts +55 -0
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +44 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +103 -0
- package/template/apps/api/src/app.module.ts +45 -0
- package/template/apps/api/src/auth/auth.controller.ts +70 -0
- package/template/apps/api/src/auth/auth.dto.ts +49 -0
- package/template/apps/api/src/auth/auth.module.ts +30 -0
- package/template/apps/api/src/auth/auth.service.test.ts +75 -0
- package/template/apps/api/src/auth/auth.service.ts +231 -0
- package/template/apps/api/src/common/auth-realm.decorator.ts +6 -0
- package/template/apps/api/src/common/auth.guard.test.ts +61 -0
- package/template/apps/api/src/common/auth.guard.ts +78 -0
- package/template/apps/api/src/common/casl-ability.factory.test.ts +33 -0
- package/template/apps/api/src/common/casl-ability.factory.ts +41 -0
- package/template/apps/api/src/common/current-user.decorator.ts +6 -0
- package/template/apps/api/src/common/permission.decorator.ts +7 -0
- package/template/apps/api/src/common/permissions.guard.ts +31 -0
- package/template/apps/api/src/common/prisma.service.ts +13 -0
- package/template/apps/api/src/health/health.controller.ts +25 -0
- package/template/apps/api/src/mail/mail.module.ts +9 -0
- package/template/apps/api/src/mail/mail.service.ts +115 -0
- package/template/apps/api/src/main.ts +37 -0
- package/template/apps/api/src/plugins/bootstrap.ts +4 -0
- package/template/apps/api/src/plugins/bundled/admin-widget.plugin.ts +25 -0
- package/template/apps/api/src/plugins/bundled/hello.plugin.ts +19 -0
- package/template/apps/api/src/plugins/bundled/image-optimizer.plugin.ts +22 -0
- package/template/apps/api/src/plugins/bundled/registry.ts +13 -0
- package/template/apps/api/src/plugins/bundled/seo.plugin.ts +25 -0
- package/template/apps/api/src/plugins/manifest-path.ts +35 -0
- package/template/apps/api/src/plugins/manifest.schema.test.ts +36 -0
- package/template/apps/api/src/plugins/manifest.schema.ts +58 -0
- package/template/apps/api/src/plugins/plugin-api.interceptor.ts +47 -0
- package/template/apps/api/src/plugins/plugin-loader.ts +134 -0
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +24 -0
- package/template/apps/api/src/plugins/plugin-runtime.ts +85 -0
- package/template/apps/api/src/queue/queue.controller.ts +28 -0
- package/template/apps/api/src/queue/queue.module.ts +23 -0
- package/template/apps/api/src/queue/queue.processor.ts +47 -0
- package/template/apps/api/src/queue/queue.service.ts +50 -0
- package/template/apps/api/src/resources/categories.resource.ts +21 -0
- package/template/apps/api/src/resources/commerce.resource.ts +157 -0
- package/template/apps/api/src/resources/operations.resource.ts +111 -0
- package/template/apps/api/src/resources/posts.resource.ts +88 -0
- package/template/apps/api/src/resources/registry.ts +27 -0
- package/template/apps/api/src/resources/resource-hooks.install.ts +18 -0
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +72 -0
- package/template/apps/api/src/resources/resource-hooks.registry.ts +66 -0
- package/template/apps/api/src/resources/resource-hooks.runner.ts +93 -0
- package/template/apps/api/src/resources/system.resource.ts +51 -0
- package/template/apps/api/src/resources/users.resource.ts +19 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +98 -0
- package/template/apps/api/src/store/payment/payment.module.ts +14 -0
- package/template/apps/api/src/store/payment/payment.provider.ts +38 -0
- package/template/apps/api/src/store/payment/payment.service.ts +235 -0
- package/template/apps/api/src/store/store.controller.ts +26 -0
- package/template/apps/api/src/store/store.module.ts +12 -0
- package/template/apps/api/src/test-setup.ts +1 -0
- package/template/apps/api/tsconfig.json +11 -0
- package/template/apps/api/vitest.config.ts +21 -0
- package/template/apps/web/lib/openadmin-client.ts +11 -0
- package/template/apps/web/lib/seo.test.ts +41 -0
- package/template/apps/web/lib/seo.ts +24 -13
- package/template/apps/web/next-env.d.ts +4 -0
- package/template/apps/web/package.json +2 -1
- package/template/apps/web/test/next-stub.ts +7 -0
- package/template/apps/web/vitest.config.ts +19 -0
- package/template/package.json +18 -11
- package/template/packages/auth/package.json +17 -0
- package/template/packages/auth/src/index.test.ts +10 -0
- package/template/packages/auth/src/index.ts +17 -0
- package/template/packages/auth/tsconfig.json +8 -0
- package/template/packages/brand/apps/admin/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/admin/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/admin/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/apple-touch-icon.png +0 -0
- package/template/packages/brand/apps/web/public/brand/favicon.svg +1 -0
- package/template/packages/brand/apps/web/public/brand/og-image.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-icon-new.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-monochrome.png +0 -0
- package/template/packages/brand/apps/web/public/brand/openadminjs-logo-new.png +0 -0
- package/template/packages/brand/package.json +19 -0
- package/template/packages/brand/site/assets/brand/favicon.svg +1 -0
- package/template/packages/brand/site/assets/brand/og-image.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-icon.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-dark.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-horizontal.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo-monochrome.svg +1 -0
- package/template/packages/brand/site/assets/brand/openadminjs-logo.svg +1 -0
- package/template/packages/brand/src/generate-assets.mjs +182 -0
- package/template/packages/brand/src/index.test.ts +9 -0
- package/template/packages/brand/src/index.ts +7 -0
- package/template/packages/brand/tsconfig.json +7 -0
- package/template/packages/core/package.json +20 -0
- package/template/packages/core/src/index.js +1 -0
- package/template/packages/core/src/index.test.ts +27 -0
- package/template/packages/core/src/index.ts +61 -0
- package/template/packages/core/tsconfig.json +8 -0
- package/template/packages/mail/package.json +18 -0
- package/template/packages/mail/src/index.test.ts +23 -0
- package/template/packages/mail/src/index.ts +246 -0
- package/template/packages/mail/tsconfig.json +5 -0
- package/template/packages/notifications/package.json +16 -0
- package/template/packages/notifications/src/index.test.ts +9 -0
- package/template/packages/notifications/src/index.ts +5 -0
- package/template/packages/notifications/tsconfig.json +5 -0
- package/template/packages/payments/package.json +15 -0
- package/template/packages/payments/src/index.ts +256 -0
- package/template/packages/payments/tsconfig.json +9 -0
- package/template/packages/permissions/package.json +17 -0
- package/template/packages/permissions/src/blueprints.js +57 -0
- package/template/packages/permissions/src/blueprints.test.ts +62 -0
- package/template/packages/permissions/src/blueprints.ts +89 -0
- package/template/packages/permissions/src/index.js +11 -0
- package/template/packages/permissions/src/index.test.ts +34 -0
- package/template/packages/permissions/src/index.ts +22 -0
- package/template/packages/permissions/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/package.json +26 -0
- package/template/packages/plugin-sdk/src/hooks.ts +135 -0
- package/template/packages/plugin-sdk/src/index.ts +42 -0
- package/template/packages/plugin-sdk/src/sdk.test.ts +13 -0
- package/template/packages/plugin-sdk/tsconfig.json +8 -0
- package/template/packages/plugin-sdk/vitest.config.ts +5 -0
- package/template/packages/queue/package.json +16 -0
- package/template/packages/queue/src/index.test.ts +12 -0
- package/template/packages/queue/src/index.ts +6 -0
- package/template/packages/queue/tsconfig.json +5 -0
- package/template/packages/resource/package.json +17 -0
- package/template/packages/resource/src/index.js +177 -0
- package/template/packages/resource/src/index.test.ts +151 -0
- package/template/packages/resource/src/index.ts +344 -0
- package/template/packages/resource/tsconfig.json +8 -0
- package/template/packages/storage/package.json +16 -0
- package/template/packages/storage/src/index.test.ts +10 -0
- package/template/packages/storage/src/index.ts +23 -0
- package/template/packages/storage/tsconfig.json +5 -0
- package/template/prisma/migrations/20260505151751_baseline_init_all_tables/migration.sql +371 -0
- package/template/prisma/migrations/migration_lock.toml +3 -0
- package/template/prisma/schema.prisma +291 -0
- package/template/prisma/seed.ts +217 -0
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { Bot, Check, Loader2, MessageCircle, Paperclip, Pencil, Plus, Search, Send, Sparkles, Trash2, X } from "lucide-react";
|
|
5
|
+
import { usePathname } from "next/navigation";
|
|
6
|
+
import { API_URL, api, token } from "../lib/api";
|
|
7
|
+
import type { AiStreamEvent } from "../lib/ai-events";
|
|
8
|
+
|
|
9
|
+
type ChatMessage = {
|
|
10
|
+
id: string;
|
|
11
|
+
role: "user" | "assistant";
|
|
12
|
+
content: string;
|
|
13
|
+
actionResult?: Record<string, unknown> | null;
|
|
14
|
+
errorDetails?: { code?: string; details?: unknown } | null;
|
|
15
|
+
pendingAction?: Record<string, unknown> | null;
|
|
16
|
+
requiresConfirmation?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type UploadAttachment = {
|
|
20
|
+
name: string;
|
|
21
|
+
mimeType: string;
|
|
22
|
+
contentBase64: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const MAX_ATTACHMENTS = 5;
|
|
26
|
+
const QUICK_PROMPTS = [
|
|
27
|
+
"Analyze current page data and summarize insights.",
|
|
28
|
+
"Export this resource to CSV with key fields.",
|
|
29
|
+
"Generate a banner image and prepare it for record update.",
|
|
30
|
+
"Find anomalies in recent records and explain."
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
function toBase64(file: File): Promise<string> {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const reader = new FileReader();
|
|
36
|
+
reader.onload = () => {
|
|
37
|
+
const value = String(reader.result ?? "");
|
|
38
|
+
const marker = "base64,";
|
|
39
|
+
const index = value.indexOf(marker);
|
|
40
|
+
if (index === -1) {
|
|
41
|
+
reject(new Error("Invalid file encoding"));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
resolve(value.slice(index + marker.length));
|
|
45
|
+
};
|
|
46
|
+
reader.onerror = () => reject(reader.error ?? new Error("File read failed"));
|
|
47
|
+
reader.readAsDataURL(file);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function saveBlob(filename: string, blob: Blob) {
|
|
52
|
+
const url = URL.createObjectURL(blob);
|
|
53
|
+
const anchor = document.createElement("a");
|
|
54
|
+
anchor.href = url;
|
|
55
|
+
anchor.download = filename;
|
|
56
|
+
document.body.appendChild(anchor);
|
|
57
|
+
anchor.click();
|
|
58
|
+
anchor.remove();
|
|
59
|
+
URL.revokeObjectURL(url);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function AiAssistantWidget() {
|
|
63
|
+
const pathname = usePathname();
|
|
64
|
+
const [open, setOpen] = useState(false);
|
|
65
|
+
const [prompt, setPrompt] = useState("");
|
|
66
|
+
const [loading, setLoading] = useState(false);
|
|
67
|
+
const [messages, setMessages] = useState<ChatMessage[]>([
|
|
68
|
+
{
|
|
69
|
+
id: "welcome",
|
|
70
|
+
role: "assistant",
|
|
71
|
+
content: "Hi! I am your AI admin consultant. I can explain features, analyze data, and help with CRUD operations."
|
|
72
|
+
}
|
|
73
|
+
]);
|
|
74
|
+
const [conversationId, setConversationId] = useState<string | null>(null);
|
|
75
|
+
const [conversations, setConversations] = useState<Array<{ id: string; title?: string | null }>>([]);
|
|
76
|
+
const [conversationPage, setConversationPage] = useState(1);
|
|
77
|
+
const [conversationPages, setConversationPages] = useState(1);
|
|
78
|
+
const [editingConversationId, setEditingConversationId] = useState<string | null>(null);
|
|
79
|
+
const [editingTitle, setEditingTitle] = useState("");
|
|
80
|
+
const [search, setSearch] = useState("");
|
|
81
|
+
const [attachments, setAttachments] = useState<UploadAttachment[]>([]);
|
|
82
|
+
const [error, setError] = useState<string | null>(null);
|
|
83
|
+
const [streamingText, setStreamingText] = useState("");
|
|
84
|
+
const [lastFailedPayload, setLastFailedPayload] = useState<{
|
|
85
|
+
message: string;
|
|
86
|
+
attachments: UploadAttachment[];
|
|
87
|
+
pendingAction?: Record<string, unknown>;
|
|
88
|
+
confirmAction?: boolean;
|
|
89
|
+
} | null>(null);
|
|
90
|
+
const [expandedDiagnostics, setExpandedDiagnostics] = useState<Record<string, boolean>>({});
|
|
91
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
92
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
93
|
+
|
|
94
|
+
const canSend = useMemo(() => prompt.trim().length > 0 && !loading, [prompt, loading]);
|
|
95
|
+
|
|
96
|
+
async function refreshConversations(query?: string, page = 1, append = false) {
|
|
97
|
+
const qs = query?.trim() ? `?q=${encodeURIComponent(query.trim())}` : "";
|
|
98
|
+
const paging = `${qs ? "&" : "?"}page=${page}&limit=12`;
|
|
99
|
+
return api<{
|
|
100
|
+
data: Array<{ id: string; title?: string | null }>;
|
|
101
|
+
meta: { page: number; pages: number };
|
|
102
|
+
}>(`/admin/ai/conversations${qs}${paging}`)
|
|
103
|
+
.then((response) => {
|
|
104
|
+
setConversations((current) => (append ? [...current, ...response.data] : response.data));
|
|
105
|
+
setConversationPage(response.meta.page);
|
|
106
|
+
setConversationPages(response.meta.pages);
|
|
107
|
+
})
|
|
108
|
+
.catch(() => {});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
void refreshConversations();
|
|
113
|
+
}, []);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
if (!open) return;
|
|
117
|
+
if (conversationId) return;
|
|
118
|
+
const latest = conversations[0];
|
|
119
|
+
if (!latest?.id) return;
|
|
120
|
+
setConversationId(latest.id);
|
|
121
|
+
void api<{ messages: Array<{ id: string; role: "user" | "assistant"; content: string }> }>(`/admin/ai/conversations/${latest.id}`)
|
|
122
|
+
.then((conversation) => {
|
|
123
|
+
if (!conversation.messages?.length) return;
|
|
124
|
+
setMessages(
|
|
125
|
+
conversation.messages.map((msg) => ({
|
|
126
|
+
id: msg.id,
|
|
127
|
+
role: msg.role,
|
|
128
|
+
content: msg.content
|
|
129
|
+
}))
|
|
130
|
+
);
|
|
131
|
+
})
|
|
132
|
+
.catch(() => {});
|
|
133
|
+
}, [open, conversationId, conversations]);
|
|
134
|
+
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: "smooth" });
|
|
137
|
+
}, [messages, streamingText, open]);
|
|
138
|
+
|
|
139
|
+
async function loadConversation(id: string) {
|
|
140
|
+
const conversation = await api<{ messages: Array<{ id: string; role: "user" | "assistant"; content: string }> }>(`/admin/ai/conversations/${id}`);
|
|
141
|
+
setConversationId(id);
|
|
142
|
+
setMessages(
|
|
143
|
+
conversation.messages.length
|
|
144
|
+
? conversation.messages.map((msg) => ({ id: msg.id, role: msg.role, content: msg.content }))
|
|
145
|
+
: [
|
|
146
|
+
{
|
|
147
|
+
id: "welcome",
|
|
148
|
+
role: "assistant",
|
|
149
|
+
content: "Hi! I am your AI admin consultant. I can explain features, analyze data, and help with CRUD operations."
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function startNewChat() {
|
|
156
|
+
setConversationId(null);
|
|
157
|
+
setMessages([
|
|
158
|
+
{
|
|
159
|
+
id: "welcome",
|
|
160
|
+
role: "assistant",
|
|
161
|
+
content: "New chat started. Ask me anything about the admin panel."
|
|
162
|
+
}
|
|
163
|
+
]);
|
|
164
|
+
setAttachments([]);
|
|
165
|
+
setPrompt("");
|
|
166
|
+
setError(null);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function removeConversation(id: string) {
|
|
170
|
+
const ok = window.confirm("Delete this conversation?");
|
|
171
|
+
if (!ok) return;
|
|
172
|
+
await api(`/admin/ai/conversations/${id}`, { method: "DELETE" });
|
|
173
|
+
if (conversationId === id) {
|
|
174
|
+
startNewChat();
|
|
175
|
+
}
|
|
176
|
+
await refreshConversations(search, 1, false);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function downloadArtifact(message: ChatMessage) {
|
|
180
|
+
const result = message.actionResult;
|
|
181
|
+
const downloadUrl = typeof result?.downloadUrl === "string" ? result.downloadUrl : null;
|
|
182
|
+
const filename = typeof result?.filename === "string" ? result.filename : "artifact.bin";
|
|
183
|
+
if (!downloadUrl) return;
|
|
184
|
+
const response = await fetch(`${API_URL}${downloadUrl}`, {
|
|
185
|
+
headers: {
|
|
186
|
+
...(token() ? { authorization: `Bearer ${token()}` } : {})
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
setError("Failed to download artifact");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const blob = await response.blob();
|
|
194
|
+
saveBlob(filename, blob);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function saveInlineRename(id: string) {
|
|
198
|
+
const title = editingTitle.trim();
|
|
199
|
+
if (!title) return;
|
|
200
|
+
await api(`/admin/ai/conversations/${id}`, {
|
|
201
|
+
method: "PATCH",
|
|
202
|
+
body: JSON.stringify({ title })
|
|
203
|
+
});
|
|
204
|
+
setEditingConversationId(null);
|
|
205
|
+
setEditingTitle("");
|
|
206
|
+
await refreshConversations(search, 1, false);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function onPickFiles(files: FileList | null) {
|
|
210
|
+
if (!files || files.length === 0) return;
|
|
211
|
+
const selected = Array.from(files).slice(0, MAX_ATTACHMENTS);
|
|
212
|
+
try {
|
|
213
|
+
const next = await Promise.all(
|
|
214
|
+
selected.map(async (file) => ({
|
|
215
|
+
name: file.name,
|
|
216
|
+
mimeType: file.type || "application/octet-stream",
|
|
217
|
+
contentBase64: await toBase64(file)
|
|
218
|
+
}))
|
|
219
|
+
);
|
|
220
|
+
setAttachments((current) => [...current, ...next].slice(0, MAX_ATTACHMENTS));
|
|
221
|
+
setError(null);
|
|
222
|
+
} catch (pickError) {
|
|
223
|
+
setError(pickError instanceof Error ? pickError.message : "Failed to read file");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function sendStream(payload: {
|
|
228
|
+
message: string;
|
|
229
|
+
attachments: UploadAttachment[];
|
|
230
|
+
pendingAction?: Record<string, unknown>;
|
|
231
|
+
confirmAction?: boolean;
|
|
232
|
+
}) {
|
|
233
|
+
const response = await fetch(`${API_URL}/admin/ai/chat/stream`, {
|
|
234
|
+
method: "POST",
|
|
235
|
+
headers: {
|
|
236
|
+
"content-type": "application/json",
|
|
237
|
+
...(token() ? { authorization: `Bearer ${token()}` } : {})
|
|
238
|
+
},
|
|
239
|
+
body: JSON.stringify({
|
|
240
|
+
message: payload.message,
|
|
241
|
+
pagePath: pathname,
|
|
242
|
+
conversationId,
|
|
243
|
+
attachments: payload.attachments,
|
|
244
|
+
pendingAction: payload.pendingAction,
|
|
245
|
+
confirmAction: payload.confirmAction
|
|
246
|
+
})
|
|
247
|
+
});
|
|
248
|
+
if (!response.ok || !response.body) {
|
|
249
|
+
throw new Error("Unable to start AI stream");
|
|
250
|
+
}
|
|
251
|
+
const reader = response.body.getReader();
|
|
252
|
+
const decoder = new TextDecoder();
|
|
253
|
+
let buffer = "";
|
|
254
|
+
let text = "";
|
|
255
|
+
while (true) {
|
|
256
|
+
const chunk = await reader.read();
|
|
257
|
+
if (chunk.done) break;
|
|
258
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
259
|
+
const events = buffer.split("\n\n");
|
|
260
|
+
buffer = events.pop() ?? "";
|
|
261
|
+
for (const rawEvent of events) {
|
|
262
|
+
const line = rawEvent.split("\n").find((item) => item.startsWith("data: "));
|
|
263
|
+
if (!line) continue;
|
|
264
|
+
const data = JSON.parse(line.slice(6)) as AiStreamEvent;
|
|
265
|
+
if ("conversationId" in data && data.conversationId) {
|
|
266
|
+
setConversationId(data.conversationId);
|
|
267
|
+
}
|
|
268
|
+
if (data.type === "delta") {
|
|
269
|
+
text += data.delta ?? "";
|
|
270
|
+
setStreamingText(text);
|
|
271
|
+
} else if (data.type === "error") {
|
|
272
|
+
const msg = data.message ?? "AI stream failed";
|
|
273
|
+
setMessages((current) => [
|
|
274
|
+
...current,
|
|
275
|
+
{
|
|
276
|
+
id: crypto.randomUUID(),
|
|
277
|
+
role: "assistant",
|
|
278
|
+
content: msg,
|
|
279
|
+
errorDetails: { code: data.code, details: data.details }
|
|
280
|
+
}
|
|
281
|
+
]);
|
|
282
|
+
setStreamingText("");
|
|
283
|
+
setError(msg);
|
|
284
|
+
setLastFailedPayload(payload);
|
|
285
|
+
} else if (data.type === "done") {
|
|
286
|
+
setMessages((current) => [
|
|
287
|
+
...current,
|
|
288
|
+
{
|
|
289
|
+
id: crypto.randomUUID(),
|
|
290
|
+
role: "assistant",
|
|
291
|
+
content: data.reply?.trim() || text.trim() || "Done.",
|
|
292
|
+
actionResult: data.actionResult ?? null,
|
|
293
|
+
errorDetails: null,
|
|
294
|
+
pendingAction: data.pendingAction ?? null,
|
|
295
|
+
requiresConfirmation: Boolean(data.requiresConfirmation)
|
|
296
|
+
}
|
|
297
|
+
]);
|
|
298
|
+
setStreamingText("");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async function onSend(options?: { pendingAction?: Record<string, unknown>; confirmAction?: boolean }) {
|
|
305
|
+
const text = prompt.trim();
|
|
306
|
+
if ((!text && !options?.pendingAction) || loading) return;
|
|
307
|
+
const userMessage: ChatMessage = {
|
|
308
|
+
id: crypto.randomUUID(),
|
|
309
|
+
role: "user",
|
|
310
|
+
content: options?.confirmAction ? "Confirm delete action." : text
|
|
311
|
+
};
|
|
312
|
+
setMessages((current) => [...current, userMessage]);
|
|
313
|
+
if (!options?.pendingAction) setPrompt("");
|
|
314
|
+
setLoading(true);
|
|
315
|
+
setError(null);
|
|
316
|
+
try {
|
|
317
|
+
const streamPayload = {
|
|
318
|
+
message: options?.pendingAction ? "Proceed with pending action." : text,
|
|
319
|
+
attachments,
|
|
320
|
+
pendingAction: options?.pendingAction,
|
|
321
|
+
confirmAction: options?.confirmAction
|
|
322
|
+
};
|
|
323
|
+
await sendStream(streamPayload);
|
|
324
|
+
setAttachments([]);
|
|
325
|
+
await refreshConversations(search, 1, false);
|
|
326
|
+
setLastFailedPayload(null);
|
|
327
|
+
} catch (sendError) {
|
|
328
|
+
setMessages((current) => [
|
|
329
|
+
...current,
|
|
330
|
+
{
|
|
331
|
+
id: crypto.randomUUID(),
|
|
332
|
+
role: "assistant",
|
|
333
|
+
content: "Could not get AI response. Check provider and token in settings."
|
|
334
|
+
}
|
|
335
|
+
]);
|
|
336
|
+
setError(sendError instanceof Error ? sendError.message : "AI request failed");
|
|
337
|
+
} finally {
|
|
338
|
+
setLoading(false);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async function retryLastFailedWorkflow() {
|
|
343
|
+
if (!lastFailedPayload || loading) return;
|
|
344
|
+
setLoading(true);
|
|
345
|
+
setError(null);
|
|
346
|
+
try {
|
|
347
|
+
await sendStream(lastFailedPayload);
|
|
348
|
+
await refreshConversations(search, 1, false);
|
|
349
|
+
setLastFailedPayload(null);
|
|
350
|
+
} catch (retryError) {
|
|
351
|
+
setError(retryError instanceof Error ? retryError.message : "Retry failed");
|
|
352
|
+
} finally {
|
|
353
|
+
setLoading(false);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return (
|
|
358
|
+
<>
|
|
359
|
+
<button
|
|
360
|
+
type="button"
|
|
361
|
+
className="fixed bottom-6 right-6 z-40 flex h-14 w-14 items-center justify-center rounded-full bg-gradient-to-br from-[#2454ff] to-[#0ea5a4] text-white shadow-[0_16px_36px_rgba(36,84,255,0.42)] transition-all duration-300 hover:scale-105 hover:shadow-[0_22px_46px_rgba(36,84,255,0.5)]"
|
|
362
|
+
onClick={() => setOpen((value) => !value)}
|
|
363
|
+
aria-label={open ? "Close AI chat" : "Open AI chat"}
|
|
364
|
+
>
|
|
365
|
+
{open ? <X className="h-6 w-6" /> : <MessageCircle className="h-6 w-6" />}
|
|
366
|
+
</button>
|
|
367
|
+
|
|
368
|
+
{open && (
|
|
369
|
+
<div className="fixed bottom-0 right-0 z-40 h-[84vh] w-full overflow-hidden rounded-t-3xl border border-slate-200 bg-white shadow-[0_18px_56px_rgba(15,23,42,0.22)] animate-in dark:border-slate-700 dark:bg-slate-900 sm:bottom-24 sm:right-6 sm:h-auto sm:w-[min(440px,calc(100vw-24px))] sm:rounded-2xl">
|
|
370
|
+
<div className="flex items-center gap-3 border-b border-slate-100 bg-gradient-to-r from-[#2454ff] to-[#0ea5a4] px-4 py-3 text-white dark:border-slate-700">
|
|
371
|
+
<span className="flex h-8 w-8 items-center justify-center rounded-full bg-white/20">
|
|
372
|
+
<Bot className="h-4 w-4" />
|
|
373
|
+
</span>
|
|
374
|
+
<div className="min-w-0">
|
|
375
|
+
<p className="text-sm font-semibold">AI-консультант</p>
|
|
376
|
+
<p className="truncate text-xs text-white/85">Понимает текущую страницу и структуру ресурсов</p>
|
|
377
|
+
</div>
|
|
378
|
+
<Sparkles className="ml-auto h-4 w-4 text-white/80" />
|
|
379
|
+
</div>
|
|
380
|
+
|
|
381
|
+
<div className="flex min-h-0">
|
|
382
|
+
<aside className="hidden w-44 shrink-0 border-r border-slate-100 bg-slate-50/80 p-2 dark:border-slate-700 dark:bg-slate-950/40 sm:block">
|
|
383
|
+
<button type="button" className="btn-primary mb-2 h-9 w-full justify-center px-2 text-xs" onClick={startNewChat}>
|
|
384
|
+
<Plus className="h-3.5 w-3.5" />
|
|
385
|
+
New chat
|
|
386
|
+
</button>
|
|
387
|
+
<div className="mb-2 flex items-center gap-1 rounded-lg border border-slate-200 bg-white px-2 py-1.5 dark:border-slate-700 dark:bg-slate-900">
|
|
388
|
+
<Search className="h-3.5 w-3.5 text-slate-400" />
|
|
389
|
+
<input
|
|
390
|
+
value={search}
|
|
391
|
+
onChange={(event) => {
|
|
392
|
+
const value = event.target.value;
|
|
393
|
+
setSearch(value);
|
|
394
|
+
void refreshConversations(value, 1, false);
|
|
395
|
+
}}
|
|
396
|
+
placeholder="Search chats"
|
|
397
|
+
className="w-full bg-transparent text-xs outline-none"
|
|
398
|
+
/>
|
|
399
|
+
</div>
|
|
400
|
+
<div className="max-h-[46vh] space-y-1 overflow-y-auto">
|
|
401
|
+
{conversations.map((conversation) => (
|
|
402
|
+
<div
|
|
403
|
+
key={conversation.id}
|
|
404
|
+
className={`group w-full rounded-lg px-2 py-1.5 text-xs transition ${
|
|
405
|
+
conversationId === conversation.id
|
|
406
|
+
? "bg-[#2454ff] text-white"
|
|
407
|
+
: "text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800"
|
|
408
|
+
}`}
|
|
409
|
+
>
|
|
410
|
+
<button
|
|
411
|
+
type="button"
|
|
412
|
+
onClick={() => void loadConversation(conversation.id)}
|
|
413
|
+
className="w-full text-left"
|
|
414
|
+
>
|
|
415
|
+
{editingConversationId === conversation.id ? (
|
|
416
|
+
<input
|
|
417
|
+
value={editingTitle}
|
|
418
|
+
onChange={(event) => setEditingTitle(event.target.value)}
|
|
419
|
+
onBlur={() => void saveInlineRename(conversation.id)}
|
|
420
|
+
onKeyDown={(event) => {
|
|
421
|
+
if (event.key === "Enter") {
|
|
422
|
+
event.preventDefault();
|
|
423
|
+
void saveInlineRename(conversation.id);
|
|
424
|
+
}
|
|
425
|
+
if (event.key === "Escape") {
|
|
426
|
+
setEditingConversationId(null);
|
|
427
|
+
setEditingTitle("");
|
|
428
|
+
}
|
|
429
|
+
}}
|
|
430
|
+
autoFocus
|
|
431
|
+
className="w-full rounded border border-white/30 bg-white/10 px-1.5 py-1 text-xs outline-none"
|
|
432
|
+
/>
|
|
433
|
+
) : (
|
|
434
|
+
<span className="line-clamp-2">{conversation.title || "New chat"}</span>
|
|
435
|
+
)}
|
|
436
|
+
</button>
|
|
437
|
+
<div className="mt-1 flex gap-1 opacity-0 transition-opacity group-hover:opacity-100">
|
|
438
|
+
<button
|
|
439
|
+
type="button"
|
|
440
|
+
onClick={() => {
|
|
441
|
+
setEditingConversationId(conversation.id);
|
|
442
|
+
setEditingTitle(conversation.title || "New chat");
|
|
443
|
+
}}
|
|
444
|
+
className="rounded p-1 hover:bg-black/10"
|
|
445
|
+
title="Rename"
|
|
446
|
+
>
|
|
447
|
+
<Pencil className="h-3 w-3" />
|
|
448
|
+
</button>
|
|
449
|
+
<button
|
|
450
|
+
type="button"
|
|
451
|
+
onClick={() => void removeConversation(conversation.id)}
|
|
452
|
+
className="rounded p-1 hover:bg-black/10"
|
|
453
|
+
title="Delete"
|
|
454
|
+
>
|
|
455
|
+
<Trash2 className="h-3 w-3" />
|
|
456
|
+
</button>
|
|
457
|
+
</div>
|
|
458
|
+
</div>
|
|
459
|
+
))}
|
|
460
|
+
{conversationPage < conversationPages && (
|
|
461
|
+
<button
|
|
462
|
+
type="button"
|
|
463
|
+
className="btn-ghost mt-2 h-8 w-full px-2 text-xs"
|
|
464
|
+
onClick={() => void refreshConversations(search, conversationPage + 1, true)}
|
|
465
|
+
>
|
|
466
|
+
Load more
|
|
467
|
+
</button>
|
|
468
|
+
)}
|
|
469
|
+
</div>
|
|
470
|
+
</aside>
|
|
471
|
+
|
|
472
|
+
<div className="min-w-0 flex-1">
|
|
473
|
+
<div className="border-b border-slate-100 px-3 py-2 dark:border-slate-700 sm:hidden">
|
|
474
|
+
<button type="button" className="btn-primary h-8 px-3 text-xs" onClick={startNewChat}>
|
|
475
|
+
<Plus className="h-3.5 w-3.5" />
|
|
476
|
+
New chat
|
|
477
|
+
</button>
|
|
478
|
+
</div>
|
|
479
|
+
|
|
480
|
+
<div ref={scrollRef} className="max-h-[58vh] space-y-3 overflow-y-auto p-4 sm:max-h-[52vh]">
|
|
481
|
+
{messages.map((message, index) => (
|
|
482
|
+
<div
|
|
483
|
+
key={`${message.id}-${index}`}
|
|
484
|
+
className={`max-w-[90%] rounded-2xl px-3.5 py-2.5 text-sm leading-relaxed transition-all duration-200 ${
|
|
485
|
+
message.role === "assistant"
|
|
486
|
+
? "bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200"
|
|
487
|
+
: "ml-auto bg-[#2454ff] text-white"
|
|
488
|
+
}`}
|
|
489
|
+
>
|
|
490
|
+
{message.content}
|
|
491
|
+
{message.requiresConfirmation && message.pendingAction && (
|
|
492
|
+
<div className="mt-2 flex gap-2">
|
|
493
|
+
<button className="btn-primary h-8 px-3 text-xs" onClick={() => void onSend({ pendingAction: message.pendingAction ?? undefined, confirmAction: true })}>
|
|
494
|
+
<Check className="h-3.5 w-3.5" />
|
|
495
|
+
Confirm delete
|
|
496
|
+
</button>
|
|
497
|
+
<button className="btn-ghost h-8 px-3 text-xs" onClick={() => setMessages((current) => current.filter((item) => item.id !== message.id))}>
|
|
498
|
+
<Trash2 className="h-3.5 w-3.5" />
|
|
499
|
+
Cancel
|
|
500
|
+
</button>
|
|
501
|
+
</div>
|
|
502
|
+
)}
|
|
503
|
+
{message.actionResult && typeof message.actionResult.downloadUrl === "string" && (
|
|
504
|
+
<div className="mt-2">
|
|
505
|
+
<button
|
|
506
|
+
type="button"
|
|
507
|
+
className="btn-ghost h-8 px-3 text-xs"
|
|
508
|
+
onClick={() => void downloadArtifact(message)}
|
|
509
|
+
>
|
|
510
|
+
Download file
|
|
511
|
+
</button>
|
|
512
|
+
</div>
|
|
513
|
+
)}
|
|
514
|
+
{message.actionResult &&
|
|
515
|
+
Array.isArray(message.actionResult.results) && (
|
|
516
|
+
<div className="mt-2 rounded-lg border border-slate-200 bg-white/60 p-2 text-xs text-slate-600 dark:border-slate-700 dark:bg-slate-900/40 dark:text-slate-300">
|
|
517
|
+
Workflow: {String(message.actionResult.stepsExecuted ?? message.actionResult.results.length)} steps executed
|
|
518
|
+
<div className="mt-1.5 space-y-1">
|
|
519
|
+
{message.actionResult.results.map((entry, idx) => (
|
|
520
|
+
<div key={idx} className="flex items-center justify-between">
|
|
521
|
+
<div className="min-w-0">
|
|
522
|
+
<span className="truncate">
|
|
523
|
+
Step {String((entry as Record<string, unknown>).step ?? idx + 1)} · {String((entry as Record<string, unknown>).type ?? "action")}
|
|
524
|
+
</span>
|
|
525
|
+
{String((entry as Record<string, unknown>).resource ?? "") &&
|
|
526
|
+
String((entry as Record<string, unknown>).targetId ?? "") && (
|
|
527
|
+
<div className="mt-1">
|
|
528
|
+
<a
|
|
529
|
+
href={`/resources/${String((entry as Record<string, unknown>).resource)}/${String((entry as Record<string, unknown>).targetId)}`}
|
|
530
|
+
className="text-[10px] font-semibold text-[#2454ff] hover:underline"
|
|
531
|
+
>
|
|
532
|
+
Open related record
|
|
533
|
+
</a>
|
|
534
|
+
</div>
|
|
535
|
+
)}
|
|
536
|
+
</div>
|
|
537
|
+
<span className="rounded-full bg-emerald-100 px-1.5 py-0.5 text-[10px] font-semibold text-emerald-700">SUCCESS</span>
|
|
538
|
+
</div>
|
|
539
|
+
))}
|
|
540
|
+
</div>
|
|
541
|
+
</div>
|
|
542
|
+
)}
|
|
543
|
+
{(() => {
|
|
544
|
+
const details = message.errorDetails?.details;
|
|
545
|
+
if (!details || typeof details !== "object") return null;
|
|
546
|
+
const map = details as Record<string, unknown>;
|
|
547
|
+
return (
|
|
548
|
+
<div className="mt-2 rounded-lg border border-red-200 bg-red-50 p-2 text-xs text-red-700 dark:border-red-900/40 dark:bg-red-950/30 dark:text-red-300">
|
|
549
|
+
<div className="flex items-center justify-between gap-2">
|
|
550
|
+
<p className="font-semibold">Workflow diagnostics</p>
|
|
551
|
+
<button
|
|
552
|
+
type="button"
|
|
553
|
+
className="rounded border border-red-300 px-1.5 py-0.5 text-[10px] font-semibold"
|
|
554
|
+
onClick={() =>
|
|
555
|
+
setExpandedDiagnostics((current) => ({
|
|
556
|
+
...current,
|
|
557
|
+
[message.id]: !current[message.id]
|
|
558
|
+
}))
|
|
559
|
+
}
|
|
560
|
+
>
|
|
561
|
+
{expandedDiagnostics[message.id] ? "Hide" : "Show"}
|
|
562
|
+
</button>
|
|
563
|
+
</div>
|
|
564
|
+
{expandedDiagnostics[message.id] && (
|
|
565
|
+
<div className="mt-1 space-y-1">
|
|
566
|
+
{"failedStep" in map && (
|
|
567
|
+
<p>
|
|
568
|
+
Failed step: {String(map.failedStep ?? "n/a")} ({String(map.failedStepType ?? "unknown")})
|
|
569
|
+
</p>
|
|
570
|
+
)}
|
|
571
|
+
{"partialResults" in map &&
|
|
572
|
+
Array.isArray(map.partialResults) &&
|
|
573
|
+
map.partialResults.length > 0 && (
|
|
574
|
+
<div>
|
|
575
|
+
<p className="font-semibold">Completed before failure:</p>
|
|
576
|
+
{map.partialResults.map((entry, idx) => {
|
|
577
|
+
const obj = entry as Record<string, unknown>;
|
|
578
|
+
return (
|
|
579
|
+
<p key={idx}>
|
|
580
|
+
#{String(obj.step ?? idx + 1)} {String(obj.type ?? "action")}
|
|
581
|
+
</p>
|
|
582
|
+
);
|
|
583
|
+
})}
|
|
584
|
+
</div>
|
|
585
|
+
)}
|
|
586
|
+
{"rolledBack" in map && <p>Rolled back: {JSON.stringify(map.rolledBack)}</p>}
|
|
587
|
+
{"rollbackFailures" in map && (
|
|
588
|
+
<p>Rollback failures: {JSON.stringify(map.rollbackFailures)}</p>
|
|
589
|
+
)}
|
|
590
|
+
</div>
|
|
591
|
+
)}
|
|
592
|
+
</div>
|
|
593
|
+
);
|
|
594
|
+
})()}
|
|
595
|
+
</div>
|
|
596
|
+
))}
|
|
597
|
+
{streamingText && (
|
|
598
|
+
<div className="max-w-[90%] rounded-2xl bg-slate-100 px-3.5 py-2.5 text-sm leading-relaxed text-slate-700 dark:bg-slate-800 dark:text-slate-200">
|
|
599
|
+
{streamingText}
|
|
600
|
+
</div>
|
|
601
|
+
)}
|
|
602
|
+
{loading && (
|
|
603
|
+
<div className="inline-flex items-center gap-2 rounded-2xl bg-slate-100 px-3.5 py-2 text-sm text-slate-600 dark:bg-slate-800 dark:text-slate-300">
|
|
604
|
+
<Loader2 className="h-4 w-4 animate-spin" />
|
|
605
|
+
Generating...
|
|
606
|
+
</div>
|
|
607
|
+
)}
|
|
608
|
+
</div>
|
|
609
|
+
</div>
|
|
610
|
+
</div>
|
|
611
|
+
|
|
612
|
+
<div className="border-t border-slate-100 p-3 dark:border-slate-700">
|
|
613
|
+
{attachments.length > 0 && (
|
|
614
|
+
<div className="mb-2 flex flex-wrap gap-1.5">
|
|
615
|
+
{attachments.map((attachment, index) => (
|
|
616
|
+
<span key={`${attachment.name}-${index}`} className="badge badge-blue">
|
|
617
|
+
{attachment.name}
|
|
618
|
+
</span>
|
|
619
|
+
))}
|
|
620
|
+
</div>
|
|
621
|
+
)}
|
|
622
|
+
{!loading && (
|
|
623
|
+
<div className="mb-2 flex flex-wrap gap-1.5">
|
|
624
|
+
{QUICK_PROMPTS.map((preset) => (
|
|
625
|
+
<button
|
|
626
|
+
key={preset}
|
|
627
|
+
type="button"
|
|
628
|
+
className="rounded-full border border-slate-200 bg-white px-2.5 py-1 text-[11px] text-slate-500 transition hover:border-slate-300 hover:text-slate-700 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-400 dark:hover:text-slate-200"
|
|
629
|
+
onClick={() => setPrompt(preset)}
|
|
630
|
+
>
|
|
631
|
+
{preset}
|
|
632
|
+
</button>
|
|
633
|
+
))}
|
|
634
|
+
</div>
|
|
635
|
+
)}
|
|
636
|
+
{error && <p className="mb-2 text-xs text-red-500">{error}</p>}
|
|
637
|
+
{lastFailedPayload && (
|
|
638
|
+
<div className="mb-2">
|
|
639
|
+
<button
|
|
640
|
+
type="button"
|
|
641
|
+
className="btn-ghost h-8 px-3 text-xs"
|
|
642
|
+
onClick={() => void retryLastFailedWorkflow()}
|
|
643
|
+
disabled={loading}
|
|
644
|
+
>
|
|
645
|
+
Retry last failed workflow
|
|
646
|
+
</button>
|
|
647
|
+
</div>
|
|
648
|
+
)}
|
|
649
|
+
<div className="flex items-end gap-2">
|
|
650
|
+
<button
|
|
651
|
+
type="button"
|
|
652
|
+
className="btn-ghost flex h-10 w-10 items-center justify-center p-0"
|
|
653
|
+
onClick={() => fileInputRef.current?.click()}
|
|
654
|
+
title="Attach file or image"
|
|
655
|
+
>
|
|
656
|
+
<Paperclip className="h-4 w-4" />
|
|
657
|
+
</button>
|
|
658
|
+
<input
|
|
659
|
+
ref={fileInputRef}
|
|
660
|
+
type="file"
|
|
661
|
+
multiple
|
|
662
|
+
className="hidden"
|
|
663
|
+
onChange={(event) => {
|
|
664
|
+
void onPickFiles(event.target.files);
|
|
665
|
+
event.currentTarget.value = "";
|
|
666
|
+
}}
|
|
667
|
+
/>
|
|
668
|
+
<textarea
|
|
669
|
+
value={prompt}
|
|
670
|
+
onChange={(event) => setPrompt(event.target.value)}
|
|
671
|
+
rows={2}
|
|
672
|
+
placeholder="Ask about current functionality or request an action..."
|
|
673
|
+
className="input-base min-h-10 flex-1 resize-none"
|
|
674
|
+
/>
|
|
675
|
+
<button type="button" className="btn-primary h-10 px-3" onClick={() => void onSend()} disabled={!canSend}>
|
|
676
|
+
<Send className="h-4 w-4" />
|
|
677
|
+
</button>
|
|
678
|
+
</div>
|
|
679
|
+
</div>
|
|
680
|
+
</div>
|
|
681
|
+
)}
|
|
682
|
+
</>
|
|
683
|
+
);
|
|
684
|
+
}
|