refactor: cleanup auto-rename code
This commit is contained in:
@@ -66,7 +66,10 @@ const cancelAutoRename = async (topicId: string) => {
|
||||
const renameId = activeAutoRenames.get(topicId);
|
||||
if (!renameId) return;
|
||||
|
||||
await $fetch(`/api/topic/auto-rename/cancel/${renameId}`, {
|
||||
await $fetch(`/api/auto-rename/cancel`, {
|
||||
body: {
|
||||
renameId,
|
||||
},
|
||||
method: 'POST',
|
||||
});
|
||||
|
||||
|
||||
@@ -412,10 +412,6 @@ export const useChat = (agentId: string) => {
|
||||
return Err(AutoRenameError.NoModelSelected);
|
||||
}
|
||||
|
||||
await triplit.update('topics', topicId, {
|
||||
renaming: true
|
||||
});
|
||||
|
||||
const modelResult = await attempt(triplit.fetchOne(triplit.query('models').Where('id', '=', settings.value.systemAssistants.rename.modelId).Include('provider')));
|
||||
|
||||
if (modelResult.ok === false) {
|
||||
@@ -453,19 +449,30 @@ export const useChat = (agentId: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
await triplit.update('topics', topicId, {
|
||||
renaming: true
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await $fetch(`/api/topic/auto-rename`, {
|
||||
const res = await $fetch(`/api/auto-rename/${topicId}`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
modelId: model.id,
|
||||
topicId,
|
||||
prompt,
|
||||
providerApiKey,
|
||||
}),
|
||||
});
|
||||
}) as { ok: true, renameId: string } | { ok: false, code: string };
|
||||
if (!res.ok) {
|
||||
console.error('Failed to auto-rename:', res.code);
|
||||
return Err(AutoRenameError.FailedToGenerate);
|
||||
}
|
||||
|
||||
return Ok(res.renameId);
|
||||
} catch (error) {
|
||||
triplit.update('topics', topicId, {
|
||||
renaming: false,
|
||||
});
|
||||
|
||||
console.error('Failed to auto-rename:', error);
|
||||
return Err(AutoRenameError.FailedToGenerate);
|
||||
}
|
||||
|
||||
@@ -49,16 +49,7 @@ const handleSubmit = async (message: string, model: ModelWithProvider | null) =>
|
||||
const topic = await createTopic();
|
||||
if (!topic) throw new Error('Failed to create topic');
|
||||
|
||||
autoRename(topic.id, message).then(async res => {
|
||||
if (res.ok === false) {
|
||||
console.error('Failed to auto-rename:', res.error);
|
||||
await triplit.update('topics', topic.id, {
|
||||
renaming: false,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
});
|
||||
autoRename(topic.id, message);
|
||||
|
||||
await navigateTo(`/agent/${route.params.id}/topic/${topic.id}`);
|
||||
|
||||
|
||||
+1
-10
@@ -110,16 +110,7 @@ const handleChatSubmit = async (message: string, model: ModelWithProvider | null
|
||||
|
||||
await navigateTo(`/agent/${agent.id}/topic/${topic.id}`);
|
||||
|
||||
autoRename(topic.id, message).then(async res => {
|
||||
if (res.ok === false) {
|
||||
console.error('Failed to auto-rename:', res.error);
|
||||
await triplit.update('topics', topic.id, {
|
||||
renaming: false,
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
});
|
||||
autoRename(topic.id, message);
|
||||
|
||||
return sendMessage(message, topic, [], agent, model.provider, model).then(async res => {
|
||||
if (res.ok === false) {
|
||||
|
||||
+34
-9
@@ -12,12 +12,12 @@ export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
|
||||
const userId = event.context.user!.id;
|
||||
const topicId = getRouterParam(event, 'topicId')!;
|
||||
|
||||
const result = await readValidatedBody(event, (body) =>
|
||||
z
|
||||
.object({
|
||||
modelId: z.string(),
|
||||
topicId: z.string(),
|
||||
prompt: z.string(),
|
||||
providerApiKey: z.string().optional(),
|
||||
})
|
||||
@@ -27,47 +27,72 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: result.error.issues[0]!.message,
|
||||
data: {
|
||||
code: 'INVALID_BODY',
|
||||
ok: false,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const { modelId, topicId, prompt, providerApiKey } = result.data;
|
||||
const { modelId, prompt, providerApiKey } = result.data;
|
||||
|
||||
const model = await httpClient.fetchOne(httpClient.query('models').Where('id', '=', modelId).Include('provider'));
|
||||
if (model === null || model.providerId !== model.providerId || model.userId !== userId) {
|
||||
if (model === null || model.providerId !== model.providerId || model.userId !== userId || model.provider === null) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid model',
|
||||
statusMessage: 'Invalid model',
|
||||
data: {
|
||||
code: 'INVALID_MODEL',
|
||||
ok: false,
|
||||
}
|
||||
});
|
||||
}
|
||||
assert(model.provider !== null, 'Invalid model provider');
|
||||
|
||||
const providerDetails = await getProviderDetails(model.provider, providerApiKey, model);
|
||||
if (!providerDetails.ok) {
|
||||
switch (providerDetails.error) {
|
||||
case GatewayFetchError.NoProviderApiKey: {
|
||||
setResponseStatus(event, 400, "No provider API key");
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: `${model.provider.type} provider requires an API key`,
|
||||
statusMessage: `${model.provider.type} provider requires an API key`,
|
||||
data: {
|
||||
code: 'NO_PROVIDER_API_KEY',
|
||||
ok: false,
|
||||
}
|
||||
});
|
||||
}
|
||||
case GatewayFetchError.NoProviderBaseUrl: {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid provider URL',
|
||||
statusMessage: 'Invalid provider URL',
|
||||
data: {
|
||||
code: 'BAD_PROVIDER_URL',
|
||||
ok: false,
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { gateway } = providerDetails.data;
|
||||
assert(gateway !== null, 'Invalid gateway');
|
||||
if (gateway === null) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Invalid gateway',
|
||||
data: {
|
||||
code: 'INVALID_GATEWAY',
|
||||
ok: false,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const [renameId, pendingRename] = addPendingRename(topicId);
|
||||
event.waitUntil(autoRename(topicId, renameId, pendingRename.abortController, { gateway: gateway.gateway, model }, gateway.textTransformer, prompt));
|
||||
|
||||
return {
|
||||
success: true,
|
||||
renameId,
|
||||
ok: true,
|
||||
};
|
||||
});
|
||||
|
||||
+17
-3
@@ -1,3 +1,4 @@
|
||||
import * as z from 'zod';
|
||||
import { httpClient } from '~~/server/lib/triplit';
|
||||
import { cancelPendingRename } from '~~/server/utils/renames';
|
||||
import { assert } from '~~/utils/assert';
|
||||
@@ -5,7 +6,20 @@ import { assert } from '~~/utils/assert';
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
|
||||
const { renameId } = event.context.params!;
|
||||
const body = await readValidatedBody(event, (body) =>
|
||||
z
|
||||
.object({
|
||||
renameId: z.string(),
|
||||
})
|
||||
.safeParse(body),
|
||||
);
|
||||
if (!body.success) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
message: 'Invalid rename ID',
|
||||
});
|
||||
}
|
||||
const { renameId } = body.data;
|
||||
assert(renameId);
|
||||
|
||||
const [success, pendingRename] = cancelPendingRename(renameId);
|
||||
@@ -18,11 +32,11 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
ok: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
ok: false,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user