diff --git a/server/plugins/webhook.ts b/server/plugins/webhook.ts deleted file mode 100644 index f900609..0000000 --- a/server/plugins/webhook.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { isEmpty } from "radash"; -import { URLSearchParams } from "url"; - -export default defineNitroPlugin(() => { - onHubReady(async () => { - const config = useRuntimeConfig(); - - const webhooks = await $fetch( - "https://www.strava.com/api/v3/push_subscriptions", - { - params: { - client_id: config.oauth.strava.clientId, - client_secret: config.oauth.strava.clientSecret, - }, - }, - ); - - if (!isEmpty(webhooks)) { - return; - } - - await $fetch("https://www.strava.com/api/v3/push_subscriptions", { - method: "post", - body: new URLSearchParams({ - client_id: config.oauth.strava.clientId, - client_secret: config.oauth.strava.clientSecret, - callback_url: config.webhooksUrl, - verify_token: config.stravaVerifyToken, - }), - }); - - console.log("Webhook registered successfully!"); - }); -}); diff --git a/server/routes/auth/strava.ts b/server/routes/auth/strava.ts index 865c590..fd57f3e 100644 --- a/server/routes/auth/strava.ts +++ b/server/routes/auth/strava.ts @@ -2,7 +2,7 @@ import { omit } from "radash"; export default defineOAuthStravaEventHandler({ config: { - scope: ["read,activity:read,activity:write"], + scope: ["read,activity:read_all,activity:write"], }, onSuccess: async (event, auth) => { const userPayload = { diff --git a/server/routes/webhooks/strava/activity-create.post.ts b/server/routes/webhooks/strava/activity-create.post.ts index 5885743..72a25da 100644 --- a/server/routes/webhooks/strava/activity-create.post.ts +++ b/server/routes/webhooks/strava/activity-create.post.ts @@ -1,4 +1,4 @@ -import { get, omit } from "radash"; +import { get, omit, tryit } from "radash"; export default defineEventHandler(async (event) => { const body = await readBody(event); @@ -21,19 +21,34 @@ export default defineEventHandler(async (event) => { const activity = (await strava!(`/activities/${body.object_id}`)) as any; - const aiResponse = await ai.run("@cf/meta/llama-3.1-8b-instruct", { - response_format: { - type: "json_schema", - json_schema: { - type: "object", - properties: { - title: "string", - description: "string", + const promptActivity = ` + type: ${get(activity, "type")} + distance: ${get(activity, "distance")}m + moving time: ${get(activity, "moving_time")}sec + elapsed time: ${get(activity, "elapsed_time")}sec + total elevation gain: ${get(activity, "total_elevation_gain")}m + start (local): ${get(activity, "start_date_local")} + trainer: ${get(activity, "trainer")} + commute: ${get(activity, "commute")} + suffer score: ${get(activity, "suffer_score")}/100 + calories: ${get(activity, "calories")} + `; + + const [aiError, aiResponse] = await tryit(ai.run)( + "@cf/meta/llama-3.1-8b-instruct", + { + response_format: { + type: "json_schema", + json_schema: { + type: "object", + properties: { + title: "string", + description: "string", + }, + required: ["title", "description"], }, - required: ["title", "description"], }, - }, - prompt: ` + prompt: ` Generate a title and a short description for my strava activity. Use my preferred language. Use ${user?.preferences.data.tone} tone to generate content. Add emojis unless tone is set to minimalist. @@ -43,10 +58,23 @@ export default defineEventHandler(async (event) => { Weight: ${user?.weight} Language: ${user?.preferences.data.language} - The activity data in json format: - ${JSON.stringify(omit(activity, ["map", "laps", "stats_visibility", "embed_token", "private_note"]))} + The activity data: + ${promptActivity} `, - }); + }, + ); + + console.log( + omit(activity, [ + "map", + "laps", + "stats_visibility", + "embed_token", + "private_note", + ]), + ); + + console.error(aiError?.message); // console.log(activity); // console.log(aiResponse.response.title); diff --git a/server/routes/webhooks/strava/index.post.ts b/server/routes/webhooks/strava/index.post.ts index 57987af..2ff9ea1 100644 --- a/server/routes/webhooks/strava/index.post.ts +++ b/server/routes/webhooks/strava/index.post.ts @@ -2,7 +2,6 @@ import { get } from "radash"; export default defineEventHandler(async (event) => { const body = await readBody(event); - const db = useDrizzle(); const aspectType = get(body, "aspect_type"); const objectType = get(body, "object_type");