Add random length and some more house-keeping

This commit is contained in:
2025-05-12 17:36:51 +03:00
parent 7096302999
commit ce5d77d32f
7 changed files with 52 additions and 34 deletions

View File

@@ -9,7 +9,6 @@ export default defineEventHandler(async (event) => {
data: { data: {
enabled: body.enabled, enabled: body.enabled,
language: body.language, language: body.language,
tone: body.tone,
units: body.units, units: body.units,
}, },
}) })

25
server/auth.d.ts vendored Normal file
View File

@@ -0,0 +1,25 @@
declare module "#auth-utils" {
interface User {
id: number;
name: string;
country: string;
sex: string;
weight: number;
avatar: string;
}
interface UserSession {
user: {
id: number;
name: string;
country: string;
sex: string;
weight: number;
avatar: string;
};
}
interface SecureSessionData {
// Add your own fields
}
}

View File

@@ -1,4 +1,5 @@
import { get, omit } from "radash"; import { get, omit } from "radash";
import type { H3Event } from "h3";
const requiredScope = ["read", "activity:write", "activity:read_all"]; const requiredScope = ["read", "activity:write", "activity:read_all"];
const hasEnoughScope = (scope: string) => { const hasEnoughScope = (scope: string) => {
@@ -11,7 +12,7 @@ export default defineOAuthStravaEventHandler({
config: { config: {
scope: [requiredScope.join(",")], scope: [requiredScope.join(",")],
}, },
onSuccess: async (event, auth) => { onSuccess: async (event: H3Event, auth: any) => {
const query = getQuery(event); const query = getQuery(event);
const scope = get(query, "scope", ""); const scope = get(query, "scope", "");

View File

@@ -1,10 +1,9 @@
import { get, omit, draw } from "radash"; import { get } from "radash";
import { createActivityContent } from "~~/server/utils/create-content"; import { createActivityContent } from "~~/server/utils/create-content";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const body = await readBody(event); const body = await readBody(event);
const db = useDrizzle(); const db = useDrizzle();
const openai = useOpenAI();
const user = await db.query.users.findFirst({ const user = await db.query.users.findFirst({
where: (f, o) => o.eq(f.id, body.owner_id), where: (f, o) => o.eq(f.id, body.owner_id),
@@ -17,18 +16,9 @@ export default defineEventHandler(async (event) => {
return; return;
} }
const tone = draw([
"casual",
"funny",
"epic",
"poetic",
"reflective",
"snarky",
]);
const strava = await useStrava(body.owner_id); const strava = await useStrava(body.owner_id);
const [, activity] = await strava!<any>(`/activities/${body.object_id}`); const activity = await strava!<any>(`/activities/${body.object_id}`);
const [aiError, aiResponse] = await createActivityContent(activity, user); const [aiError, aiResponse] = await createActivityContent(activity, user);
if (aiError) { if (aiError) {
@@ -52,15 +42,13 @@ export default defineEventHandler(async (event) => {
description: [responseObject.description, promo].join("\n"), description: [responseObject.description, promo].join("\n"),
}; };
const [stravaError] = await strava!(`activities/${body.object_id}`, { await strava!(`activities/${body.object_id}`, {
method: "PUT", method: "PUT",
body: stravaRequestBody, body: stravaRequestBody,
}); }).catch((error) => {
if (stravaError) {
throw createError({ throw createError({
statusCode: 500, statusCode: 500,
message: `Strava API: ${stravaError.message}`, message: `Strava API: ${error.message}`,
});
}); });
}
}); });

View File

@@ -1,4 +1,4 @@
import { get, isEmpty } from "radash"; import { get } from "radash";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {

View File

@@ -101,7 +101,10 @@ const stringifyActivity = chain<[Activity], Activity, string>(
(activity) => JSON.stringify(activity), (activity) => JSON.stringify(activity),
); );
export const createActivityContent = async (activity: Activity, user: User) => { export const createActivityContent = async (
activity: Activity,
user: User & { preferences: any },
) => {
const openai = useOpenAI(); const openai = useOpenAI();
const tone = draw([ const tone = draw([
@@ -113,12 +116,14 @@ export const createActivityContent = async (activity: Activity, user: User) => {
"snarky", "snarky",
]); ]);
console.log(tone, activity.type); const length = draw(["short", "short", "short", "medium", "long"]);
console.log(tone, length);
const prompt = ` const prompt = `
Generate a short title and description for my strava activity. Use my preferred language and unit system. Generate a short title and a ${length}-lengthed description for my strava activity. Use my preferred language and unit system.
Don't exaggerate. Try keeping it calm as I am using Strava often and I don't want to have boring feed. Keep things short. Don't exaggerate. Try keeping it calm as I am using Strava often and I don't want to have boring feed. Keep things short.
Use a little bit of ${tone} to make things less boring. Highlight any PR if available. Use a little bit of ${tone} to make things less boring. Highlight any PR only if available, do not mention them if no PRs.
Language: ${user?.preferences.data.language} Language: ${user?.preferences.data.language}
Unit system: ${user?.preferences.data.units} Unit system: ${user?.preferences.data.units}
@@ -134,7 +139,7 @@ export const createActivityContent = async (activity: Activity, user: User) => {
const [aiError, aiResponse] = await openai("/responses", { const [aiError, aiResponse] = await openai("/responses", {
body: { body: {
model: "gpt-4o", model: "gpt-4.1",
input: [ input: [
{ {
role: "user", role: "user",

View File

@@ -57,12 +57,12 @@ export const useStrava = async (userId: number) => {
.where(eq(tables.tokens.userId, userId)); .where(eq(tables.tokens.userId, userId));
} }
return tryit( const client = $fetch.create({
$fetch.create({
baseURL: "https://www.strava.com/api/v3/", baseURL: "https://www.strava.com/api/v3/",
onRequest({ options }) { onRequest({ options }) {
options.headers.set("Authorization", `Bearer ${tokens?.accessToken}`); options.headers.set("Authorization", `Bearer ${tokens?.accessToken}`);
}, },
}), });
);
return client;
}; };