-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fixes #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,9 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { z } from "zod"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { chatCompletionObject } from "@/utils/llms"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { EmailAccountWithAI } from "@/utils/llms/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createScopedLogger } from "@/utils/logger"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getModel } from "@/utils/llms/model"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { generateObject } from "ai"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { saveAiUsage } from "@/utils/usage"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const logger = createScopedLogger("ai/clean/select-labels"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -31,15 +33,37 @@ ${instructions} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.trace("Input", { system, prompt }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const aiResponse = await chatCompletionObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userAi: emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // const aiResponse = await chatCompletionObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // userAi: emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // system, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // userEmail: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // usageLabel: "Clean - Select Labels", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { provider, model, llmModel, providerOptions } = getModel( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const aiResponse = await generateObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model: llmModel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| system, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userEmail: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usageLabel: "Clean - Select Labels", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Implement error handling for AI generation failures. The refactored code lacks fallback mechanisms for AI failures, which is a requirement per the coding guidelines for LLM-related functions. Consider adding error handling: + try {
const aiResponse = await generateObject({
model: llmModel,
system,
prompt,
schema,
providerOptions,
});
+ } catch (error) {
+ logger.error("AI generation failed", { error });
+ // Return empty array as fallback for labels
+ return [];
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (aiResponse.usage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await saveAiUsage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| email: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usage: aiResponse.usage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Clean - Select Labels", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.trace("Result", { response: aiResponse.object }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return aiResponse.object.labels; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { z } from "zod"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { chatCompletionObject } from "@/utils/llms"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { generateObject } from "ai"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { EmailAccountWithAI } from "@/utils/llms/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createScopedLogger } from "@/utils/logger"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { EmailForLLM } from "@/utils/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { stringifyEmailSimple } from "@/utils/stringify-email"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { formatDateForLLM, formatRelativeTimeForLLM } from "@/utils/date"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { preprocessBooleanLike } from "@/utils/zod"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getModel } from "@/utils/llms/model"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { saveAiUsage } from "@/utils/usage"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // import { Braintrust } from "@/utils/braintrust"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const logger = createScopedLogger("ai/clean"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -92,15 +94,37 @@ The current date is ${currentDate}. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.trace("Input", { system, prompt }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const aiResponse = await chatCompletionObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userAi: emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // const aiResponse = await chatCompletionObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // userAi: emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // system, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // userEmail: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // usageLabel: "Clean", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { provider, model, llmModel, providerOptions } = getModel( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| emailAccount.user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const aiResponse = await generateObject({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model: llmModel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| system, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prompt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| schema, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| userEmail: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usageLabel: "Clean", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerOptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+106
to
116
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling and fallback for AI generation failures. The function lacks error handling for AI generation failures, which could cause the entire email processing to fail. Given this function makes critical decisions about archiving emails, a fallback strategy is essential. Add comprehensive error handling: + try {
const aiResponse = await generateObject({
model: llmModel,
system,
prompt,
schema,
providerOptions,
});
+ } catch (error) {
+ logger.error("AI generation failed for email archiving", {
+ error,
+ messageId,
+ emailAccount: emailAccount.email
+ });
+ // Fallback: don't archive when AI fails (safer default)
+ return { archive: false };
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (aiResponse.usage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await saveAiUsage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| email: emailAccount.email, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usage: aiResponse.usage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| model, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label: "Clean", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.trace("Result", { response: aiResponse.object }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // braintrust.insertToDataset({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass
modelTypeparameter togetModelcall.The
modelTypeparameter (defaulted to "default" on line 23) should be passed to thegetModelcall to ensure the correct model type is used.Apply this fix:
const { provider, model, llmModel, providerOptions } = getModel( emailAccount.user, + modelType, );📝 Committable suggestion
🤖 Prompt for AI Agents