diff options
| author | Dhravya Shah <[email protected]> | 2024-07-24 09:00:26 -0500 |
|---|---|---|
| committer | Dhravya Shah <[email protected]> | 2024-07-24 09:00:26 -0500 |
| commit | 33f8cdac2396035fb8b99815f02c2e4ad4d83f66 (patch) | |
| tree | 33abc56d5ca5326e354839808974015301b0a7ae /apps | |
| parent | fix weird wrangler problems in prod for recommendations] (diff) | |
| download | supermemory-33f8cdac2396035fb8b99815f02c2e4ad4d83f66.tar.xz supermemory-33f8cdac2396035fb8b99815f02c2e4ad4d83f66.zip | |
better error handling
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/web/app/actions/doers.ts | 159 |
1 files changed, 85 insertions, 74 deletions
diff --git a/apps/web/app/actions/doers.ts b/apps/web/app/actions/doers.ts index 383efda7..12d2c29e 100644 --- a/apps/web/app/actions/doers.ts +++ b/apps/web/app/actions/doers.ts @@ -747,95 +747,106 @@ export async function getQuerySuggestions() { const { env } = getRequestContext(); - const recommendations = await env.RECOMMENDATIONS.get(data.user.id); + try { + const recommendations = await env.RECOMMENDATIONS.get(data.user.id); - if (recommendations) { - return { - success: true, - data: JSON.parse(recommendations), - }; - } + if (recommendations) { + return { + success: true, + data: JSON.parse(recommendations), + }; + } - // Randomly choose some storedContent of the user. - const content = await db - .select() - .from(storedContent) - .where(eq(storedContent.userId, data.user.id)) - .orderBy(sql`random()`) - .limit(5) - .all(); + // Randomly choose some storedContent of the user. + const content = await db + .select() + .from(storedContent) + .where(eq(storedContent.userId, data.user.id)) + .orderBy(sql`random()`) + .limit(5) + .all(); - if (content.length === 0) { - return { - success: true, - data: [], - }; - } + if (content.length === 0) { + return { + success: true, + data: [], + }; + } - const fullQuery = content.map((c) => `${c.title} \n\n${c.content}`).join(" "); + const fullQuery = content + .map((c) => `${c.title} \n\n${c.content}`) + .join(" "); - const sentences = getRandomSentences(fullQuery); + const sentences = getRandomSentences(fullQuery); - const suggestionsCall = (await env.AI.run( - // @ts-ignore - "@cf/meta/llama-3.1-8b-instruct", - { - messages: [ - { - role: "system", - content: `You are a model that suggests questions based on the user's content.`, - }, - { - role: "user", - content: `Run the function based on this input: ${sentences}`, - }, - ], - tools: [ - { - type: "function", - function: { - name: "querySuggestions", - description: - "Take the user's content to suggest some good questions that they could ask.", - parameters: { - type: "object", - properties: { - querySuggestions: { - type: "array", - description: - "Short questions that the user can ask. Give atleast 3 suggestions. No more than 5.", - items: { - type: "string", + const suggestionsCall = (await env.AI.run( + // @ts-ignore + "@cf/meta/llama-3.1-8b-instruct", + { + messages: [ + { + role: "system", + content: `You are a model that suggests questions based on the user's content.`, + }, + { + role: "user", + content: `Run the function based on this input: ${sentences}`, + }, + ], + tools: [ + { + type: "function", + function: { + name: "querySuggestions", + description: + "Take the user's content to suggest some good questions that they could ask.", + parameters: { + type: "object", + properties: { + querySuggestions: { + type: "array", + description: + "Short questions that the user can ask. Give atleast 3 suggestions. No more than 5.", + items: { + type: "string", + }, }, }, + required: ["querySuggestions"], }, - required: ["querySuggestions"], }, }, - }, - ], - }, - )) as { - response: string; - tool_calls: { name: string; arguments: { querySuggestions: string[] } }[]; - }; + ], + }, + )) as { + response: string; + tool_calls: { name: string; arguments: { querySuggestions: string[] } }[]; + }; + + const suggestions = + suggestionsCall.tool_calls?.[0]?.arguments?.querySuggestions; + + if (!suggestions || suggestions.length === 0) { + return { + success: false, + error: "Failed to get query suggestions", + }; + } - const suggestions = - suggestionsCall.tool_calls?.[0]?.arguments?.querySuggestions; + await env.RECOMMENDATIONS.put(data.user.id, JSON.stringify(suggestions), { + expirationTtl: 60 * 2, + }); - if (!suggestions || suggestions.length === 0) { + return { + success: true, + data: suggestions, + }; + } catch (exception) { + const error = exception as Error; return { success: false, - error: "Failed to get query suggestions", + error: error.message, + data: [], }; } - - await env.RECOMMENDATIONS.put(data.user.id, JSON.stringify(suggestions), { - expirationTtl: 60 * 5, - }); - - return { - success: true, - data: suggestions, - }; } |