research-agent-ui 0.1.84 → 0.1.86
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/dist/api.cjs +15 -15
- package/dist/api.mjs +183 -179
- package/dist/components/ArticleReader/ArticleAIResponse.stories.d.ts +13 -0
- package/dist/components/ArticleReader/ArticleActionButtons.d.ts +58 -0
- package/dist/components/ArticleReader/ArticleActionButtons.stories.d.ts +21 -0
- package/dist/components/ArticleReader/ArticleFollowupQuestions.stories.d.ts +14 -0
- package/dist/components/ArticleReader/ArticlePanelHeader.stories.d.ts +10 -0
- package/dist/components/ArticleReader/ArticlePromptInput.stories.d.ts +13 -0
- package/dist/components/ArticleReader/index.d.ts +1 -0
- package/dist/components/ChatConversation/MessageBubble/ThinkTagProcessor.stories.d.ts +12 -0
- package/dist/components/ChatConversation/MessageReasoningPanel.stories.d.ts +14 -0
- package/dist/components/ChatHistoryDropdown/HistoryChatItem.stories.d.ts +9 -0
- package/dist/components/ConfigError.stories.d.ts +10 -0
- package/dist/components/Footer.stories.d.ts +13 -0
- package/dist/components/MessageActions/CopyMessageButton.stories.d.ts +11 -0
- package/dist/components/SearchResults/WebCitationBadge.stories.d.ts +15 -0
- package/dist/index.cjs +13 -13
- package/dist/index.mjs +1471 -1344
- package/dist/ui/Loader.stories.d.ts +15 -0
- package/dist/ui/badge.stories.d.ts +21 -0
- package/dist/ui/button.stories.d.ts +25 -0
- package/dist/ui/dialog.stories.d.ts +12 -0
- package/dist/ui/dropdown-menu.stories.d.ts +10 -0
- package/dist/ui/glowing-effect.stories.d.ts +13 -0
- package/dist/ui/live-waveform.stories.d.ts +15 -0
- package/dist/ui/popover.stories.d.ts +10 -0
- package/dist/ui/tooltip.stories.d.ts +15 -0
- package/dist/ui/visually-hidden.stories.d.ts +17 -0
- package/package.json +7 -6
package/dist/api.mjs
CHANGED
|
@@ -1,41 +1,43 @@
|
|
|
1
|
-
import e from "
|
|
2
|
-
import
|
|
3
|
-
import l from "
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import p from "
|
|
7
|
-
import m from "
|
|
8
|
-
import h from "
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
1
|
+
import { generateText as e } from "ai";
|
|
2
|
+
import t from "chat-agent-toolkit/models/registry";
|
|
3
|
+
import { and as n, count as r, eq as i, inArray as a, like as o, max as s, or as c, sql as l } from "drizzle-orm";
|
|
4
|
+
import u from "chat-agent-toolkit/tools/search/titleGeneratorAgent";
|
|
5
|
+
import { searchSearxng as d, searchWeb as f } from "search-web-api/search/public-searxng";
|
|
6
|
+
import { searchAutocompleteMulti as p } from "extract-webpage/suggest-next-words/autocomplete-search-engines";
|
|
7
|
+
import m from "fuse.js";
|
|
8
|
+
import h from "domain-rank/data/domain-rank-merged.json";
|
|
9
|
+
import g from "chat-agent-toolkit/tools/search/suggestionGeneratorAgent";
|
|
10
|
+
import { testProviderModels as _ } from "chat-agent-toolkit/config/model-tester";
|
|
11
|
+
import { LANGUAGE_MODELS as v } from "chat-agent-toolkit/config/language-models-database";
|
|
11
12
|
//#region src/api/handlers/article-followups.ts
|
|
12
|
-
function
|
|
13
|
-
return async (
|
|
13
|
+
function y(n) {
|
|
14
|
+
return async (n) => {
|
|
14
15
|
try {
|
|
15
|
-
let { article:
|
|
16
|
-
if (!
|
|
17
|
-
let
|
|
16
|
+
let { article: r, chatHistory: i = [], maxQuestions: a = 5, chatModel: o } = await n.json();
|
|
17
|
+
if (!r) return Response.json({ error: "Article is required" }, { status: 400 });
|
|
18
|
+
let s = new t(), c;
|
|
18
19
|
try {
|
|
19
|
-
|
|
20
|
+
c = await s.loadChatModel(o?.providerId, o?.key);
|
|
20
21
|
} catch (e) {
|
|
21
22
|
return Response.json({ error: e instanceof Error ? e.message : "Failed to load language model" }, { status: 500 });
|
|
22
23
|
}
|
|
23
|
-
let
|
|
24
|
-
Generate ${
|
|
25
|
-
Return ONLY the questions, one per line, without numbering or bullet points.`,
|
|
26
|
-
${
|
|
24
|
+
let l = i.length > 0 ? `\n\nPrevious questions asked:\n${i.filter((e) => e.role === "user").map((e) => `- ${e.content}`).join("\n")}` : "", u = `You are a helpful AI that generates insightful follow-up questions about articles.
|
|
25
|
+
Generate ${a} thought-provoking questions that would help readers understand the article better.
|
|
26
|
+
Return ONLY the questions, one per line, without numbering or bullet points.`, d = `Article content:
|
|
27
|
+
${r.slice(0, 15e3)}
|
|
27
28
|
|
|
28
|
-
${
|
|
29
|
+
${l}
|
|
29
30
|
|
|
30
|
-
Generate ${
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
Generate ${a} follow-up questions that would help readers dive deeper into this article.`, { text: f } = await e({
|
|
32
|
+
model: c,
|
|
33
|
+
messages: [{
|
|
34
|
+
role: "system",
|
|
35
|
+
content: u
|
|
36
|
+
}, {
|
|
37
|
+
role: "user",
|
|
38
|
+
content: d
|
|
39
|
+
}]
|
|
40
|
+
}), p = f.split("\n").map((e) => e.trim()).filter((e) => e.length > 0).map((e) => e.replace(/^[\d\-\*\.\)]+\s*/, "").trim()).filter((e) => e.length > 10).slice(0, a);
|
|
39
41
|
return Response.json({
|
|
40
42
|
extract: p,
|
|
41
43
|
success: !0
|
|
@@ -50,32 +52,34 @@ Generate ${i} follow-up questions that would help readers dive deeper into this
|
|
|
50
52
|
}
|
|
51
53
|
//#endregion
|
|
52
54
|
//#region src/api/handlers/article-qa.ts
|
|
53
|
-
function
|
|
54
|
-
return async (
|
|
55
|
+
function b(n) {
|
|
56
|
+
return async (n) => {
|
|
55
57
|
try {
|
|
56
|
-
let { article:
|
|
57
|
-
if (!
|
|
58
|
-
let
|
|
58
|
+
let { article: r, question: i, chatHistory: a = [], chatModel: o } = await n.json();
|
|
59
|
+
if (!r || !i) return Response.json({ error: "Article and question are required" }, { status: 400 });
|
|
60
|
+
let s = new t(), c;
|
|
59
61
|
try {
|
|
60
|
-
|
|
62
|
+
c = await s.loadChatModel(o?.providerId, o?.key);
|
|
61
63
|
} catch (e) {
|
|
62
64
|
return Response.json({ error: e instanceof Error ? e.message : "Failed to load language model" }, { status: 500 });
|
|
63
65
|
}
|
|
64
|
-
let
|
|
65
|
-
${
|
|
66
|
+
let l = a.length > 0 ? `\n\nPrevious conversation:\n${a.map((e) => `${e.role === "user" ? "User" : "Assistant"}: ${e.content}`).join("\n")}` : "", u = `Article content:
|
|
67
|
+
${r.slice(0, 15e3)}
|
|
66
68
|
|
|
67
|
-
${
|
|
69
|
+
${l}
|
|
68
70
|
|
|
69
|
-
User question: ${
|
|
71
|
+
User question: ${i}
|
|
70
72
|
|
|
71
|
-
Please provide a helpful answer based on the article content above.`,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
Please provide a helpful answer based on the article content above.`, { text: d } = await e({
|
|
74
|
+
model: c,
|
|
75
|
+
messages: [{
|
|
76
|
+
role: "system",
|
|
77
|
+
content: "You are a helpful AI assistant that answers questions about articles.\nProvide clear, concise, and accurate answers based on the article content provided.\nIf the answer is not in the article, say so."
|
|
78
|
+
}, {
|
|
79
|
+
role: "user",
|
|
80
|
+
content: u
|
|
81
|
+
}]
|
|
82
|
+
});
|
|
79
83
|
return Response.json({
|
|
80
84
|
content: d,
|
|
81
85
|
success: !0
|
|
@@ -90,35 +94,35 @@ Please provide a helpful answer based on the article content above.`, u = await
|
|
|
90
94
|
}
|
|
91
95
|
//#endregion
|
|
92
96
|
//#region src/api/handlers/chats.ts
|
|
93
|
-
function
|
|
94
|
-
let { chats:
|
|
97
|
+
function x(e) {
|
|
98
|
+
let { chats: t, messages: o } = e.schema;
|
|
95
99
|
return {
|
|
96
|
-
GET: async (
|
|
100
|
+
GET: async (a) => {
|
|
97
101
|
try {
|
|
98
|
-
let
|
|
99
|
-
id:
|
|
100
|
-
title:
|
|
101
|
-
createdAt:
|
|
102
|
-
focusMode:
|
|
103
|
-
userId:
|
|
104
|
-
files:
|
|
105
|
-
messageCount:
|
|
106
|
-
lastMessageAt: o
|
|
107
|
-
}).from(
|
|
102
|
+
let a = e.getDB(), c = await e.requireUserId(), u = await a.select({
|
|
103
|
+
id: t.id,
|
|
104
|
+
title: t.title,
|
|
105
|
+
createdAt: t.createdAt,
|
|
106
|
+
focusMode: t.focusMode,
|
|
107
|
+
userId: t.userId,
|
|
108
|
+
files: t.files,
|
|
109
|
+
messageCount: r(o.id),
|
|
110
|
+
lastMessageAt: s(o.createdAt)
|
|
111
|
+
}).from(t).leftJoin(o, n(i(o.chatId, t.id), i(o.role, "user"))).where(i(t.userId, c)).groupBy(t.id, t.title, t.createdAt, t.focusMode, t.userId, t.files).orderBy(l`${t.createdAt} DESC`);
|
|
108
112
|
return Response.json({ chats: u }, { status: 200 });
|
|
109
113
|
} catch (e) {
|
|
110
114
|
return e instanceof Error && e.message === "Unauthorized" ? Response.json({ message: "Authentication required" }, { status: 401 }) : (console.error("Error in getting chats: ", e), Response.json({ message: "An error has occurred." }, { status: 500 }));
|
|
111
115
|
}
|
|
112
116
|
},
|
|
113
|
-
DELETE: async (
|
|
117
|
+
DELETE: async (n) => {
|
|
114
118
|
try {
|
|
115
|
-
let
|
|
116
|
-
where:
|
|
119
|
+
let n = e.getDB(), r = await e.requireUserId(), s = await n.query.chats.findMany({
|
|
120
|
+
where: i(t.userId, r),
|
|
117
121
|
columns: { id: !0 }
|
|
118
122
|
});
|
|
119
|
-
if (
|
|
120
|
-
let e =
|
|
121
|
-
await
|
|
123
|
+
if (s.length > 0) {
|
|
124
|
+
let e = s.map((e) => e.id);
|
|
125
|
+
await n.delete(o).where(a(o.chatId, e)), await n.delete(t).where(i(t.userId, r));
|
|
122
126
|
}
|
|
123
127
|
return Response.json({ message: "All chats deleted" }, { status: 200 });
|
|
124
128
|
} catch (e) {
|
|
@@ -127,21 +131,21 @@ function b(e) {
|
|
|
127
131
|
}
|
|
128
132
|
};
|
|
129
133
|
}
|
|
130
|
-
function
|
|
131
|
-
let { chats:
|
|
134
|
+
function S(e) {
|
|
135
|
+
let { chats: t, messages: r } = e.schema;
|
|
132
136
|
return {
|
|
133
|
-
GET: async (
|
|
137
|
+
GET: async (n, { params: a }) => {
|
|
134
138
|
try {
|
|
135
|
-
let
|
|
139
|
+
let n = e.getDB(), { id: o } = await a, s;
|
|
136
140
|
try {
|
|
137
141
|
s = await e.requireUserId();
|
|
138
142
|
} catch {
|
|
139
143
|
s = void 0;
|
|
140
144
|
}
|
|
141
|
-
let c = await
|
|
145
|
+
let c = await n.query.chats.findFirst({ where: i(t.id, o) });
|
|
142
146
|
if (!c) return Response.json({ message: "Chat not found" }, { status: 404 });
|
|
143
147
|
if (!(c.userId === s || c.isPublic)) return Response.json({ message: "Unauthorized - this chat is private" }, { status: 403 });
|
|
144
|
-
let l = await
|
|
148
|
+
let l = await n.query.messages.findMany({ where: i(r.chatId, o) });
|
|
145
149
|
return Response.json({
|
|
146
150
|
chat: c,
|
|
147
151
|
messages: l
|
|
@@ -153,37 +157,37 @@ function x(e) {
|
|
|
153
157
|
DELETE: async (a, { params: o }) => {
|
|
154
158
|
try {
|
|
155
159
|
let a = e.getDB(), { id: s } = await o, c = await e.requireUserId();
|
|
156
|
-
return await a.query.chats.findFirst({ where:
|
|
160
|
+
return await a.query.chats.findFirst({ where: n(i(t.id, s), i(t.userId, c)) }) ? (await a.delete(t).where(i(t.id, s)).execute(), await a.delete(r).where(i(r.chatId, s)).execute(), Response.json({ message: "Chat deleted successfully" }, { status: 200 })) : Response.json({ message: "Chat not found" }, { status: 404 });
|
|
157
161
|
} catch (e) {
|
|
158
162
|
return e instanceof Error && e.message === "Unauthorized" ? Response.json({ message: "Authentication required" }, { status: 401 }) : (console.error("Error in deleting chat by id: ", e), Response.json({ message: "An error has occurred." }, { status: 500 }));
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
};
|
|
162
166
|
}
|
|
163
|
-
function
|
|
164
|
-
let { chats:
|
|
165
|
-
return { GET: async (
|
|
167
|
+
function C(e) {
|
|
168
|
+
let { chats: t, messages: r } = e.schema;
|
|
169
|
+
return { GET: async (a) => {
|
|
166
170
|
try {
|
|
167
|
-
let
|
|
171
|
+
let s = e.getDB(), u = await e.requireUserId(), { searchParams: d } = new URL(a.url), f = d.get("q");
|
|
168
172
|
if (!f || f.trim().length === 0) return Response.json({ chats: [] }, { status: 200 });
|
|
169
|
-
let p = `%${f.trim()}%`, m = await
|
|
170
|
-
id:
|
|
171
|
-
title:
|
|
172
|
-
createdAt:
|
|
173
|
-
focusMode:
|
|
174
|
-
userId:
|
|
175
|
-
files:
|
|
176
|
-
}).from(
|
|
177
|
-
id:
|
|
178
|
-
title:
|
|
179
|
-
createdAt:
|
|
180
|
-
focusMode:
|
|
181
|
-
userId:
|
|
182
|
-
files:
|
|
183
|
-
}).from(
|
|
184
|
-
chatId:
|
|
185
|
-
count:
|
|
186
|
-
}).from(
|
|
173
|
+
let p = `%${f.trim()}%`, m = await s.select({
|
|
174
|
+
id: t.id,
|
|
175
|
+
title: t.title,
|
|
176
|
+
createdAt: t.createdAt,
|
|
177
|
+
focusMode: t.focusMode,
|
|
178
|
+
userId: t.userId,
|
|
179
|
+
files: t.files
|
|
180
|
+
}).from(t).where(n(i(t.userId, u), o(t.title, p))).orderBy(l`${t.createdAt} DESC`), h = await s.selectDistinct({
|
|
181
|
+
id: t.id,
|
|
182
|
+
title: t.title,
|
|
183
|
+
createdAt: t.createdAt,
|
|
184
|
+
focusMode: t.focusMode,
|
|
185
|
+
userId: t.userId,
|
|
186
|
+
files: t.files
|
|
187
|
+
}).from(r).innerJoin(t, i(r.chatId, t.id)).where(n(i(t.userId, u), c(o(r.content, p)))).orderBy(l`${t.createdAt} DESC`), g = [...m, ...h], _ = Array.from(new Map(g.map((e) => [e.id, e])).values()), v = _.map((e) => e.id), y = v.length ? await s.select({
|
|
188
|
+
chatId: r.chatId,
|
|
189
|
+
count: l`count(*)`
|
|
190
|
+
}).from(r).where(n(l`${r.chatId} IN (${l.join(v.map((e) => l`${e}`), l`, `)})`, i(r.role, "user"))).groupBy(r.chatId) : [], b = new Map(y.map((e) => [e.chatId, Number(e.count)])), x = _.map((e) => ({
|
|
187
191
|
...e,
|
|
188
192
|
messageCount: b.get(e.id) || 0
|
|
189
193
|
}));
|
|
@@ -193,16 +197,16 @@ function S(e) {
|
|
|
193
197
|
}
|
|
194
198
|
} };
|
|
195
199
|
}
|
|
196
|
-
function
|
|
200
|
+
function w(e) {
|
|
197
201
|
let { chats: t } = e.schema;
|
|
198
202
|
return { POST: async (n) => {
|
|
199
203
|
try {
|
|
200
|
-
let
|
|
204
|
+
let r = e.getDB(), { chatId: a } = await n.json();
|
|
201
205
|
if (!a) return Response.json({
|
|
202
206
|
success: !1,
|
|
203
207
|
error: "Chat ID is required"
|
|
204
208
|
}, { status: 400 });
|
|
205
|
-
let o = await e.requireUserId(), s = await
|
|
209
|
+
let o = await e.requireUserId(), s = await r.query.chats.findFirst({ where: i(t.id, a) });
|
|
206
210
|
if (!s) return Response.json({
|
|
207
211
|
success: !1,
|
|
208
212
|
error: "Chat not found"
|
|
@@ -211,7 +215,7 @@ function C(e) {
|
|
|
211
215
|
success: !1,
|
|
212
216
|
error: "Unauthorized"
|
|
213
217
|
}, { status: 403 });
|
|
214
|
-
await
|
|
218
|
+
await r.update(t).set({ isPublic: 1 }).where(i(t.id, a)).execute();
|
|
215
219
|
let c = `${new URL(n.url).origin}/c/${a}`;
|
|
216
220
|
return Response.json({
|
|
217
221
|
success: !0,
|
|
@@ -233,21 +237,21 @@ function C(e) {
|
|
|
233
237
|
}
|
|
234
238
|
//#endregion
|
|
235
239
|
//#region src/api/handlers/chat-title.ts
|
|
236
|
-
function
|
|
237
|
-
let { chats: n } =
|
|
238
|
-
return { POST: async (
|
|
240
|
+
function ee(e) {
|
|
241
|
+
let { chats: n } = e.schema;
|
|
242
|
+
return { POST: async (r) => {
|
|
239
243
|
try {
|
|
240
|
-
let a = await
|
|
244
|
+
let a = await r.json(), o = a.chatHistory.filter((e) => e.role === "user" || e.role === "assistant").map((e) => ({
|
|
241
245
|
role: e.role,
|
|
242
246
|
content: String(e.content ?? "")
|
|
243
247
|
}));
|
|
244
248
|
if (o.length === 0) return Response.json({ message: "No conversation content" }, { status: 400 });
|
|
245
|
-
let s = await new
|
|
249
|
+
let s = await new t().loadChatModel(a.chatModel.providerId, a.chatModel.key), c = await u({ chat_history: o }, s);
|
|
246
250
|
if (!c) return Response.json({ message: "Failed to generate title" }, { status: 500 });
|
|
247
|
-
let
|
|
248
|
-
if (
|
|
249
|
-
let
|
|
250
|
-
|
|
251
|
+
let l = e.getUserId ? await e.getUserId() : null;
|
|
252
|
+
if (l && a.chatId) try {
|
|
253
|
+
let t = e.getDB(), r = await t.query.chats.findFirst({ where: i(n.id, a.chatId) });
|
|
254
|
+
r && r.userId === l && await t.update(n).set({ title: c }).where(i(n.id, a.chatId)).execute();
|
|
251
255
|
} catch (e) {
|
|
252
256
|
console.error("Failed to persist chat title:", e);
|
|
253
257
|
}
|
|
@@ -259,22 +263,22 @@ function w(t) {
|
|
|
259
263
|
}
|
|
260
264
|
//#endregion
|
|
261
265
|
//#region src/api/handlers/messages.ts
|
|
262
|
-
var
|
|
266
|
+
var T = [
|
|
263
267
|
"assistant",
|
|
264
268
|
"user",
|
|
265
269
|
"source",
|
|
266
270
|
"suggestion"
|
|
267
271
|
];
|
|
268
|
-
function
|
|
272
|
+
function E(e) {
|
|
269
273
|
let t = [], n = e;
|
|
270
274
|
for (let e = 0; n != null && e < 5; e++) t.push(n instanceof Error ? n.message : String(n)), n = n instanceof Error ? n.cause : void 0;
|
|
271
275
|
return t.join(" <- caused by: ");
|
|
272
276
|
}
|
|
273
|
-
function
|
|
277
|
+
function D(e) {
|
|
274
278
|
return { POST: async (t) => {
|
|
275
279
|
try {
|
|
276
280
|
let n = e.getDB(), r = await e.requireUserId(), { chatId: i, messageId: a, role: o, suggestions: s, content: c, sources: l } = await t.json();
|
|
277
|
-
return !i || !a || !o ? Response.json({ message: "Missing required fields" }, { status: 400 }) :
|
|
281
|
+
return !i || !a || !o ? Response.json({ message: "Missing required fields" }, { status: 400 }) : T.includes(o) ? (await n.insert(e.messagesSchema).values({
|
|
278
282
|
chatId: i,
|
|
279
283
|
userId: r,
|
|
280
284
|
messageId: a,
|
|
@@ -285,18 +289,18 @@ function E(e) {
|
|
|
285
289
|
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
286
290
|
}), Response.json({ message: "Message saved successfully" }, { status: 200 })) : Response.json({ message: `Invalid role: ${o}` }, { status: 400 });
|
|
287
291
|
} catch (e) {
|
|
288
|
-
return console.error("Error saving message:",
|
|
292
|
+
return console.error("Error saving message:", E(e)), Response.json({ message: "Failed to save message" }, { status: 500 });
|
|
289
293
|
}
|
|
290
294
|
} };
|
|
291
295
|
}
|
|
292
296
|
//#endregion
|
|
293
297
|
//#region src/api/handlers/providers.ts
|
|
294
|
-
function
|
|
298
|
+
function O(e) {
|
|
295
299
|
return {
|
|
296
300
|
GET: async (n) => {
|
|
297
301
|
try {
|
|
298
|
-
let r = new
|
|
299
|
-
i === null && (a = !await
|
|
302
|
+
let r = new t(), i = new URL(n.url).searchParams.get("guest"), a = i === "true";
|
|
303
|
+
i === null && (a = !await e.getSession());
|
|
300
304
|
let o = (await r.getActiveProviders(a)).filter((e) => !e.chatModels.some((e) => e.key === "error"));
|
|
301
305
|
return o.length === 0 ? Response.json({
|
|
302
306
|
providers: [],
|
|
@@ -309,11 +313,11 @@ function D(t) {
|
|
|
309
313
|
return console.error("An error occurred while fetching providers", e), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
310
314
|
}
|
|
311
315
|
},
|
|
312
|
-
POST: async (
|
|
316
|
+
POST: async (e) => {
|
|
313
317
|
try {
|
|
314
|
-
let { type: n, config: r } = await
|
|
318
|
+
let { type: n, config: r } = await e.json();
|
|
315
319
|
if (!n || !r) return Response.json({ message: "Missing required fields." }, { status: 400 });
|
|
316
|
-
let i = await new
|
|
320
|
+
let i = await new t().addProvider(n, r);
|
|
317
321
|
return Response.json({ provider: i }, { status: 200 });
|
|
318
322
|
} catch (e) {
|
|
319
323
|
return console.error("An error occurred while creating provider", e), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
@@ -321,21 +325,21 @@ function D(t) {
|
|
|
321
325
|
}
|
|
322
326
|
};
|
|
323
327
|
}
|
|
324
|
-
function
|
|
328
|
+
function k() {
|
|
325
329
|
return {
|
|
326
|
-
DELETE: async (
|
|
330
|
+
DELETE: async (e, { params: n }) => {
|
|
327
331
|
try {
|
|
328
|
-
let { id:
|
|
329
|
-
return
|
|
332
|
+
let { id: e } = await n;
|
|
333
|
+
return e ? (await new t().removeProvider(e), Response.json({ message: "Provider deleted successfully." }, { status: 200 })) : Response.json({ message: "Provider ID is required." }, { status: 400 });
|
|
330
334
|
} catch (e) {
|
|
331
335
|
return console.error("An error occurred while deleting provider", e.message), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
332
336
|
}
|
|
333
337
|
},
|
|
334
|
-
PATCH: async (
|
|
338
|
+
PATCH: async (e, { params: n }) => {
|
|
335
339
|
try {
|
|
336
|
-
let { config: r } = await
|
|
340
|
+
let { config: r } = await e.json(), { id: i } = await n;
|
|
337
341
|
if (!i || !r) return Response.json({ message: "Missing required fields." }, { status: 400 });
|
|
338
|
-
let a = await new
|
|
342
|
+
let a = await new t().updateProvider(i, r);
|
|
339
343
|
return Response.json({ provider: a }, { status: 200 });
|
|
340
344
|
} catch (e) {
|
|
341
345
|
return console.error("An error occurred while updating provider", e.message), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
@@ -343,20 +347,20 @@ function O() {
|
|
|
343
347
|
}
|
|
344
348
|
};
|
|
345
349
|
}
|
|
346
|
-
function
|
|
350
|
+
function A() {
|
|
347
351
|
return {
|
|
348
|
-
POST: async (
|
|
352
|
+
POST: async (e, { params: n }) => {
|
|
349
353
|
try {
|
|
350
|
-
let { id: r } = await n, i = await
|
|
351
|
-
return !i.key || !i.name ? Response.json({ message: "Key and name must be provided" }, { status: 400 }) : (await new
|
|
354
|
+
let { id: r } = await n, i = await e.json();
|
|
355
|
+
return !i.key || !i.name ? Response.json({ message: "Key and name must be provided" }, { status: 400 }) : (await new t().addProviderModel(r, i.type, i), Response.json({ message: "Model added successfully" }, { status: 200 }));
|
|
352
356
|
} catch (e) {
|
|
353
357
|
return console.error("An error occurred while adding provider model", e), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
354
358
|
}
|
|
355
359
|
},
|
|
356
|
-
DELETE: async (
|
|
360
|
+
DELETE: async (e, { params: n }) => {
|
|
357
361
|
try {
|
|
358
|
-
let { id: r } = await n, i = await
|
|
359
|
-
return i.key ? (await new
|
|
362
|
+
let { id: r } = await n, i = await e.json();
|
|
363
|
+
return i.key ? (await new t().removeProviderModel(r, i.type, i.key), Response.json({ message: "Model added successfully" }, { status: 200 })) : Response.json({ message: "Key and name must be provided" }, { status: 400 });
|
|
360
364
|
} catch (e) {
|
|
361
365
|
return console.error("An error occurred while deleting provider model", e), Response.json({ message: "An error has occurred." }, { status: 500 });
|
|
362
366
|
}
|
|
@@ -365,7 +369,7 @@ function k() {
|
|
|
365
369
|
}
|
|
366
370
|
//#endregion
|
|
367
371
|
//#region src/api/handlers/mcpservers.ts
|
|
368
|
-
function
|
|
372
|
+
function j(e) {
|
|
369
373
|
return {
|
|
370
374
|
GET: async (t) => {
|
|
371
375
|
try {
|
|
@@ -387,7 +391,7 @@ function A(e) {
|
|
|
387
391
|
}
|
|
388
392
|
};
|
|
389
393
|
}
|
|
390
|
-
function
|
|
394
|
+
function M(e) {
|
|
391
395
|
return {
|
|
392
396
|
DELETE: async (t, { params: n }) => {
|
|
393
397
|
try {
|
|
@@ -409,7 +413,7 @@ function j(e) {
|
|
|
409
413
|
}
|
|
410
414
|
};
|
|
411
415
|
}
|
|
412
|
-
function
|
|
416
|
+
function N(e) {
|
|
413
417
|
return { POST: async (t, { params: n }) => {
|
|
414
418
|
try {
|
|
415
419
|
let { enabled: r } = await t.json(), { id: i } = await n;
|
|
@@ -423,7 +427,7 @@ function M(e) {
|
|
|
423
427
|
}
|
|
424
428
|
//#endregion
|
|
425
429
|
//#region src/api/handlers/search.ts
|
|
426
|
-
var
|
|
430
|
+
var P = {
|
|
427
431
|
tech: {
|
|
428
432
|
query: [
|
|
429
433
|
"technology news",
|
|
@@ -489,14 +493,14 @@ var N = {
|
|
|
489
493
|
]
|
|
490
494
|
}
|
|
491
495
|
};
|
|
492
|
-
function
|
|
496
|
+
function F(e = {}) {
|
|
493
497
|
let t = e.searxngDomain ?? "https://search.qwksearch.com";
|
|
494
498
|
return { GET: async (e) => {
|
|
495
499
|
let n = new URL(e.url).searchParams, r = n.get("q"), i = n.get("cat") || "general", a = parseInt(n.get("page") || "1", 10), o = n.get("lang") || "en-US", s = n.get("safesearch") === "true", c = n.get("recency") || void 0, l = n.get("publicInstances") === "true";
|
|
496
500
|
if (!r) return Response.json({ error: "Query parameter is required" }, { status: 400 });
|
|
497
501
|
let u = Date.now();
|
|
498
502
|
try {
|
|
499
|
-
let e = await
|
|
503
|
+
let e = await f(r, {
|
|
500
504
|
category: i,
|
|
501
505
|
recency: c,
|
|
502
506
|
safesearch: s,
|
|
@@ -506,7 +510,7 @@ function P(e = {}) {
|
|
|
506
510
|
lang: o,
|
|
507
511
|
page: a
|
|
508
512
|
});
|
|
509
|
-
e && (Array.isArray(e) ? e.length > 0 : e.results && e.results.length > 0) || (e = await
|
|
513
|
+
e && (Array.isArray(e) ? e.length > 0 : e.results && e.results.length > 0) || (e = await f(r, {
|
|
510
514
|
category: i,
|
|
511
515
|
recency: c,
|
|
512
516
|
safesearch: s,
|
|
@@ -536,13 +540,13 @@ function P(e = {}) {
|
|
|
536
540
|
}
|
|
537
541
|
} };
|
|
538
542
|
}
|
|
539
|
-
function
|
|
543
|
+
function I() {
|
|
540
544
|
return { GET: async (e) => {
|
|
541
545
|
try {
|
|
542
|
-
let t = new URL(e.url).searchParams, n = t.get("mode") || "normal", r =
|
|
546
|
+
let t = new URL(e.url).searchParams, n = t.get("mode") || "normal", r = P[t.get("topic") || "tech"], i = [];
|
|
543
547
|
if (n === "normal") {
|
|
544
548
|
let e = /* @__PURE__ */ new Set();
|
|
545
|
-
i = (await Promise.all(r.links.flatMap((e) => r.query.map(async (t) => (await
|
|
549
|
+
i = (await Promise.all(r.links.flatMap((e) => r.query.map(async (t) => (await d(`site:${e} ${t}`, {
|
|
546
550
|
engines: ["bing news"],
|
|
547
551
|
pageno: 1,
|
|
548
552
|
language: "en"
|
|
@@ -550,7 +554,7 @@ function F() {
|
|
|
550
554
|
let n = t.url?.toLowerCase().trim();
|
|
551
555
|
return e.has(n) ? !1 : (e.add(n), !0);
|
|
552
556
|
}).sort(() => Math.random() - .5);
|
|
553
|
-
} else i = (await
|
|
557
|
+
} else i = (await d(`site:${r.links[Math.floor(Math.random() * r.links.length)]} ${r.query[Math.floor(Math.random() * r.query.length)]}`, {
|
|
554
558
|
engines: ["bing news"],
|
|
555
559
|
pageno: 1,
|
|
556
560
|
language: "en"
|
|
@@ -563,17 +567,17 @@ function F() {
|
|
|
563
567
|
}
|
|
564
568
|
//#endregion
|
|
565
569
|
//#region src/api/handlers/autocomplete.ts
|
|
566
|
-
var
|
|
570
|
+
var L = [
|
|
567
571
|
"google",
|
|
568
572
|
"duckduckgo",
|
|
569
573
|
"wikipedia"
|
|
570
|
-
],
|
|
574
|
+
], R = 3, z = 3, B = Object.entries(h).map(([e, t]) => ({
|
|
571
575
|
domain: e,
|
|
572
576
|
name: typeof t?.[0] == "string" ? t[0] : "",
|
|
573
577
|
rank: typeof t?.[1] == "number" ? t[1] : 2 ** 53 - 1
|
|
574
|
-
})),
|
|
575
|
-
function
|
|
576
|
-
return
|
|
578
|
+
})), V = null;
|
|
579
|
+
function H() {
|
|
580
|
+
return V ||= new m(B, {
|
|
577
581
|
keys: [{
|
|
578
582
|
name: "name",
|
|
579
583
|
weight: .6
|
|
@@ -585,22 +589,22 @@ function V() {
|
|
|
585
589
|
ignoreLocation: !0,
|
|
586
590
|
includeScore: !0,
|
|
587
591
|
minMatchCharLength: 3
|
|
588
|
-
}),
|
|
592
|
+
}), V;
|
|
589
593
|
}
|
|
590
|
-
function
|
|
594
|
+
function U(e) {
|
|
591
595
|
let t = e.split(/\s+/).filter(Boolean), n = t[t.length - 1] || "";
|
|
592
|
-
return n.length < 3 ? [] :
|
|
596
|
+
return n.length < 3 ? [] : H().search(n, { limit: 12 }).sort((e, t) => Math.round((e.score ?? 1) * 10) - Math.round((t.score ?? 1) * 10) || e.item.rank - t.item.rank).slice(0, z).map(({ item: e }) => ({
|
|
593
597
|
domain: e.domain,
|
|
594
598
|
name: e.name,
|
|
595
599
|
favicon: `https://www.google.com/s2/favicons?domain=${encodeURIComponent(e.domain)}&sz=64`
|
|
596
600
|
}));
|
|
597
601
|
}
|
|
598
|
-
async function
|
|
599
|
-
let r = await
|
|
602
|
+
async function W(e, t, n) {
|
|
603
|
+
let r = await p(e, t, n);
|
|
600
604
|
if (r.length > 0) return r;
|
|
601
|
-
let i = t.split(/\s+/).filter(Boolean), a = Math.min(
|
|
605
|
+
let i = t.split(/\s+/).filter(Boolean), a = Math.min(R, i.length - 1);
|
|
602
606
|
for (let r = a; r >= 1; r--) {
|
|
603
|
-
let a = i.slice(-r).join(" "), o = i.slice(0, -r).join(" "), s = await
|
|
607
|
+
let a = i.slice(-r).join(" "), o = i.slice(0, -r).join(" "), s = await p(e, a, n);
|
|
604
608
|
if (s.length > 0) {
|
|
605
609
|
let e = /* @__PURE__ */ new Set();
|
|
606
610
|
for (let n of s) {
|
|
@@ -612,16 +616,16 @@ async function U(e, t, n) {
|
|
|
612
616
|
}
|
|
613
617
|
return [];
|
|
614
618
|
}
|
|
615
|
-
function
|
|
619
|
+
function G() {
|
|
616
620
|
return { GET: async (e) => {
|
|
617
621
|
let { searchParams: t } = new URL(e.url), n = t.get("q")?.trim(), r = t.get("locale") || "en-US", i = t.get("backends"), a = parseInt(t.get("limit") || "8", 10);
|
|
618
622
|
if (!n) return Response.json({
|
|
619
623
|
suggestions: [],
|
|
620
624
|
domains: []
|
|
621
625
|
});
|
|
622
|
-
let o = i ? i.split(",").map((e) => e.trim()).filter(Boolean) :
|
|
626
|
+
let o = i ? i.split(",").map((e) => e.trim()).filter(Boolean) : L;
|
|
623
627
|
try {
|
|
624
|
-
let e = await
|
|
628
|
+
let e = await W(o, n, r), t = U(n);
|
|
625
629
|
return Response.json({
|
|
626
630
|
suggestions: e.slice(0, a),
|
|
627
631
|
domains: t
|
|
@@ -636,12 +640,12 @@ function W() {
|
|
|
636
640
|
}
|
|
637
641
|
//#endregion
|
|
638
642
|
//#region src/api/handlers/suggestions.ts
|
|
639
|
-
function
|
|
640
|
-
return { POST: async (
|
|
641
|
-
let n = await
|
|
643
|
+
function K() {
|
|
644
|
+
return { POST: async (e) => {
|
|
645
|
+
let n = await e.json(), r = n.chatHistory.filter((e) => e.role === "user" || e.role === "assistant").map((e) => ({
|
|
642
646
|
role: e.role,
|
|
643
647
|
content: String(e.content ?? "")
|
|
644
|
-
})), i = await new
|
|
648
|
+
})), i = await new t().loadChatModel(n.chatModel.providerId, n.chatModel.key), a = (await g({
|
|
645
649
|
chat_history: r,
|
|
646
650
|
maxQuestions: n.maxQuestions
|
|
647
651
|
}, i)).flatMap((e) => (e.match(/\?/g) || []).length > 1 ? e.split(/\?/).map((e) => e.trim()).filter((e) => e.length > 0).map((e) => e + "?") : [e]);
|
|
@@ -650,7 +654,7 @@ function G() {
|
|
|
650
654
|
}
|
|
651
655
|
//#endregion
|
|
652
656
|
//#region src/api/handlers/agents.ts
|
|
653
|
-
function
|
|
657
|
+
function q(e) {
|
|
654
658
|
return { POST: async (t) => {
|
|
655
659
|
let n;
|
|
656
660
|
try {
|
|
@@ -659,13 +663,13 @@ function K(e) {
|
|
|
659
663
|
return Response.json({ error: "Invalid JSON input" }, { status: 500 });
|
|
660
664
|
}
|
|
661
665
|
n.ip = t.headers?.get?.("x-forwarded-for") || t.headers?.get?.("x-real-ip") || "unknown";
|
|
662
|
-
let
|
|
663
|
-
return a && (o = await
|
|
666
|
+
let r = e.getDB(), a = await e.getUserId(), o = null;
|
|
667
|
+
return a && (o = await r.query.user.findFirst({ where: i(e.userSchema.id, a) })), o && (n.apiKey = o.settings?.providerApiKeys?.find((e) => e.provider == n.provider)?.key), n.apiKey ||= n.provider == "groq" && e.getEnv("GROQ_API_KEY"), n.apiKey ? Response.json({ error: "Language generation endpoint is deprecated. Please use /api/agent/chat instead." }, { status: 501 }) : Response.json({ error: "API key is required" }, { status: 500 });
|
|
664
668
|
} };
|
|
665
669
|
}
|
|
666
670
|
//#endregion
|
|
667
671
|
//#region src/api/handlers/rewrite.ts
|
|
668
|
-
function
|
|
672
|
+
function J(e) {
|
|
669
673
|
return { POST: async (t) => {
|
|
670
674
|
try {
|
|
671
675
|
let { text: n, prompt: r } = await t.json();
|
|
@@ -687,7 +691,7 @@ ${n}`, s = (await e.generateText({
|
|
|
687
691
|
}
|
|
688
692
|
//#endregion
|
|
689
693
|
//#region src/api/handlers/voice.ts
|
|
690
|
-
function
|
|
694
|
+
function Y(e) {
|
|
691
695
|
return { POST: async (t) => {
|
|
692
696
|
let n, r, i;
|
|
693
697
|
try {
|
|
@@ -722,15 +726,15 @@ function J(e) {
|
|
|
722
726
|
}
|
|
723
727
|
//#endregion
|
|
724
728
|
//#region src/api/handlers/transcript.ts
|
|
725
|
-
function
|
|
729
|
+
function X(e) {
|
|
726
730
|
return e === "small" || e === "fast" ? "@cf/openai/whisper-tiny-en" : e === "medium" || e === "turbo" ? "@cf/openai/whisper-large-v3-turbo" : e === "large" ? "@cf/openai/whisper-large-v3" : "@cf/openai/whisper-large-v3-turbo";
|
|
727
731
|
}
|
|
728
|
-
function
|
|
732
|
+
function Z(e) {
|
|
729
733
|
return { POST: async (t) => {
|
|
730
734
|
try {
|
|
731
735
|
let n = await t.formData(), r = n.get("file");
|
|
732
736
|
if (!r || !(r instanceof File)) return Response.json({ error: "file is required" }, { status: 400 });
|
|
733
|
-
let i =
|
|
737
|
+
let i = X(n.get("model")), a;
|
|
734
738
|
try {
|
|
735
739
|
a = e.getCloudflareContext().env?.AI;
|
|
736
740
|
} catch {}
|
|
@@ -747,15 +751,15 @@ function X(e) {
|
|
|
747
751
|
}
|
|
748
752
|
//#endregion
|
|
749
753
|
//#region src/api/handlers/test-models.ts
|
|
750
|
-
function
|
|
754
|
+
function Q() {
|
|
751
755
|
return { POST: async (e) => {
|
|
752
756
|
try {
|
|
753
757
|
let { providerType: t, apiKey: n, onlyFree: r = !0 } = await e.json();
|
|
754
758
|
if (!t) return Response.json({ error: "Provider type is required" }, { status: 400 });
|
|
755
759
|
if (!n) return Response.json({ error: "API key is required" }, { status: 400 });
|
|
756
|
-
let i =
|
|
760
|
+
let i = v.find((e) => e.provider.toLowerCase() === t.toLowerCase());
|
|
757
761
|
if (!i) return Response.json({ error: `Provider ${t} not found` }, { status: 404 });
|
|
758
|
-
let a = await
|
|
762
|
+
let a = await _(t, n, i.models, {
|
|
759
763
|
onlyFree: r,
|
|
760
764
|
concurrency: 3,
|
|
761
765
|
timeout: 15e3
|
|
@@ -768,13 +772,13 @@ function Z() {
|
|
|
768
772
|
}
|
|
769
773
|
//#endregion
|
|
770
774
|
//#region src/api/handlers/validate-openrouter.ts
|
|
771
|
-
var
|
|
772
|
-
function
|
|
775
|
+
var te = 1440 * 60 * 1e3, $ = null;
|
|
776
|
+
function ne(e) {
|
|
773
777
|
return {
|
|
774
778
|
GET: async (t) => {
|
|
775
779
|
try {
|
|
776
780
|
let t = Date.now();
|
|
777
|
-
if ($ && t - $.timestamp <
|
|
781
|
+
if ($ && t - $.timestamp < te) return Response.json({
|
|
778
782
|
...$.result,
|
|
779
783
|
cached: !0,
|
|
780
784
|
cacheAge: t - $.timestamp
|
|
@@ -808,4 +812,4 @@ function te(e) {
|
|
|
808
812
|
};
|
|
809
813
|
}
|
|
810
814
|
//#endregion
|
|
811
|
-
export {
|
|
815
|
+
export { q as createAgentsHandler, y as createArticleFollowupsHandler, b as createArticleQAHandler, G as createAutocompleteHandler, S as createChatByIdHandler, ee as createChatTitleHandler, x as createChatsHandler, C as createChatsSearchHandler, w as createChatsShareHandler, I as createDiscoverHandler, M as createMCPServerByIdHandler, N as createMCPServerToggleHandler, j as createMCPServersHandler, D as createMessagesHandler, k as createProviderByIdHandler, A as createProviderModelsHandler, O as createProvidersHandler, J as createRewriteHandler, F as createSearchHandler, K as createSuggestionsHandler, Q as createTestModelsHandler, Z as createTranscriptHandler, ne as createValidateOpenRouterHandler, Y as createVoiceHandler, E as describeError };
|