59bb7fbc12
This is once again a huge commit, but its mostly performance improvements along with some bug fixes and refactoring. It also includes changes to the theming systems. I'm still not 100% happy with the theming system, but its better than before. Model fetching has been dramatically improved! Nearly all the important computation and pre-processing has been moved to the server. This has also somehow fixed the way model details are loaded, which was causing many models to be missing their details despite models.dev having them. The markdown renderer has once again been changed, but I'm mostly certain that this is the last time major changes will be made to it. The renderer is not spamming components, bloating memory usage, and its not using a bug prone custom written chunking system. There's also a lot more that I haven't mentioned and honestly forgot. I need to get better commit hygiene tbh.
118 lines
3.7 KiB
TypeScript
118 lines
3.7 KiB
TypeScript
import * as z from 'zod';
|
|
import { httpClient } from '~~/server/lib/triplit';
|
|
import { renamePrompt } from '~~/prompts';
|
|
import { schema } from '~~/triplit/schema';
|
|
import { type Entity } from '@triplit/client';
|
|
import { generateText } from 'ai';
|
|
import { GatewayFetchError, getProviderDetails, type ModelGateway } from '~~/server/utils/ai-provider';
|
|
import { addPendingRename } from '~~/server/utils/renames';
|
|
import { assert } from '~~/utils/assert';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
|
|
const userId = event.context.user!.id;
|
|
|
|
const result = await readValidatedBody(event, (body) =>
|
|
z
|
|
.object({
|
|
modelId: z.string(),
|
|
topicId: z.string(),
|
|
prompt: z.string(),
|
|
providerApiKey: z.string().optional(),
|
|
})
|
|
.safeParse(body),
|
|
);
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: result.error.issues[0]!.message,
|
|
});
|
|
}
|
|
|
|
const { modelId, topicId, 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) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Invalid model',
|
|
});
|
|
}
|
|
assert(model.provider !== null, 'Invalid model provider');
|
|
|
|
const providerDetails = await getProviderDetails(model.provider, providerApiKey, model);
|
|
if (!providerDetails.ok) {
|
|
switch (providerDetails.error) {
|
|
case GatewayFetchError.NoProviderApiKey: {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: `${model.provider.type} provider requires an API key`,
|
|
});
|
|
}
|
|
case GatewayFetchError.NoProviderBaseUrl: {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Invalid provider URL',
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
const { gateway } = providerDetails.data;
|
|
assert(gateway !== null, 'Invalid gateway');
|
|
|
|
const [renameId, pendingRename] = addPendingRename(topicId);
|
|
event.waitUntil(autoRename(topicId, renameId, pendingRename.abortController, { gateway: gateway.gateway, model }, gateway.textTransformer, prompt));
|
|
|
|
return {
|
|
success: true,
|
|
renameId,
|
|
};
|
|
});
|
|
|
|
const autoRename = async (
|
|
topicId: string,
|
|
renameId: string,
|
|
abortController: AbortController,
|
|
model: {
|
|
gateway: ModelGateway,
|
|
model: Entity<typeof schema, 'models'>,
|
|
},
|
|
textTransformer: ((text: string) => string) | ((text: string) => string)[] | undefined,
|
|
prompt: string,
|
|
) => {
|
|
try {
|
|
const response = await generateText({
|
|
model: model.gateway(model.model.externalId),
|
|
system: renamePrompt,
|
|
prompt,
|
|
timeout: 90 * 1000,
|
|
abortSignal: abortController.signal,
|
|
})
|
|
|
|
let text = response.text;
|
|
|
|
if (textTransformer !== undefined) {
|
|
if (Array.isArray(textTransformer)) {
|
|
for (const transformer of textTransformer) {
|
|
text = transformer(text);
|
|
}
|
|
} else {
|
|
text = textTransformer(text);
|
|
}
|
|
}
|
|
|
|
await httpClient.update('topics', topicId, {
|
|
name: text,
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to auto-rename:', error);
|
|
} finally {
|
|
completeRename(renameId);
|
|
await httpClient.update('topics', topicId, {
|
|
renaming: false
|
|
});
|
|
}
|
|
}
|