Skip to content

AI prompt for each generated argument#191

Merged
elie222 merged 18 commits intomainfrom
arg-prompt
Jun 25, 2024
Merged

AI prompt for each generated argument#191
elie222 merged 18 commits intomainfrom
arg-prompt

Conversation

@elie222
Copy link
Owner

@elie222 elie222 commented Jun 24, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced AI categorization for email content, improving the accuracy and relevance of AI-generated labels and prompts.
    • Introduced additional prompts for AI-generated fields in rules, such as labelPrompt, subjectPrompt, and contentPrompt.
  • Bug Fixes

    • Improved handling of actions within rules, including conditions for AI field values and toggling AI generation.
  • Refactor

    • Streamlined AI chat completion handling, enhancing performance and reducing complexity.
    • Consolidated validation schemas for action fields, simplifying code maintenance.
  • Dependencies

    • Updated AI SDKs for improved compatibility and performance.
  • Database

    • Added new columns for AI prompts in the Action table, enabling more detailed and customized AI field generation.

@vercel
Copy link

vercel bot commented Jun 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
inbox-zero ✅ Ready (Inspect) Visit Preview Jun 25, 2024 3:11pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 24, 2024

Important

Review skipped

More than 25% of the files skipped due to max files limit. The review is being skipped to prevent a low-quality review.

158 files out of 216 files are above the max files limit of 50. Please upgrade to Pro plan to get higher limits.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The updates involve significant modifications to the handling of AI-generated fields, validation schemas, and AI interaction patterns across various modules. New prompts for AI fields were introduced in the database schema, and changes were made to the rule management and action creation functions. Additionally, dependencies were updated, and some functionalities were rewritten to streamline AI response handling and enhance the overall system.

Changes

File(s) Change Summary
apps/web/app/(app)/automation/RuleForm.tsx Adjusted logic for AI-generated fields, refined UI, and modified imports and toggle behavior.
apps/web/app/(app)/automation/create/examples.tsx Updated action definitions in rule examples to use nested objects and modified imported constants.
apps/web/prisma/schema.prisma Added optional prompt fields for AI-generated fields in the Action model.
apps/web/utils/actions/rule.ts Refactored functions handling rule creation and updates to improve logic and error handling.
apps/web/utils/actions/validation.ts Replaced individual string schemas with a common zodField object schema for validation.
apps/web/utils/ai/choose-rule/ai-choose-args.ts Refactored AI argument handling and updated AI response functions.
apps/web/app/(app)/automation/rule/[ruleId]/page.tsx Updated action structure mapping to enhance rule representation with AI flags.
apps/web/app/api/ai/categorise/controller.ts Refined AI categorization logic and response handling mechanisms.
apps/web/app/api/ai/cold-email/controller.ts Simplified AI interaction with streamlined function calls and response handling.
apps/web/app/api/ai/compose-autocomplete/route.ts Refactored AI chat completion and response generation for compose-autocomplete functionality.
apps/web/app/api/ai/models/route.ts Replaced custom OpenAI initialization with direct instantiation.
apps/web/app/api/ai/summarise/controller.ts Simplified summarization function by updating parameter structure and including logic for saving summaries.
apps/web/app/api/ai/summarise/route.ts Streamlined the call to summarization function and updated response generation.
apps/web/app/api/google/webhook/group-rule.ts, apps/web/app/api/google/webhook/static-rule.ts Replaced getFunctionsFromRules with getActionsWithParameters to simplify AI argument determination logic.
apps/web/app/api/user/settings/validation.ts Updated model validation logic and removed unnecessary imports related to AI model checking.
apps/web/package.json Updated dependencies related to AI SDKs and removed deprecated packages.
apps/web/prisma/migrations/20240624075134_argument_prompt/migration.sql Added columns for AI-generated prompts in Action table of the database.
apps/web/utils/actions/ai-rule.ts Refined logic for handling user email in various functions and enhanced error handling.
apps/web/utils/ai/choose-rule/ai-choose-rule.ts, apps/web/utils/ai/choose-rule/choose.ts Updated AI response handling by renaming functions and adjusting parameters for better compatibility.
apps/web/utils/ai/group/create-group.ts, apps/web/utils/ai/rule/create-rule.ts Renamed key functions and streamlined AI interaction logic for creating groups and rules.
apps/web/utils/llms/index.ts Refactor to support improved AI provider and model handling, integrating new SDKs and updating AI completion functions.
apps/web/utils/redis/usage.ts Renamed fields in usage tracking to camelCase format for consistency with other parts of the codebase.
apps/web/utils/actions/categorize.ts Introduced a new function categorizeAction for categorizing email content using AI models.

Poem

In the code a rabbit hopped with cheer,
AI fields refined, their purpose clear.
Prompts were set, and schemas new,
Transforming rules with a vibrant hue.
Dependencies trimmed, old code replaced,
A streamlined automation, perfectly paced.
Through changes vast, the system grew,
A brighter path for tasks to pursue!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range and nitpick comments (2)
apps/web/utils/actions/rule.ts (1)

[!TIP]
Codebase Verification

Ensure robust error handling and transaction management in updateRuleAction.

The updateRuleAction function uses a transaction to manage rule updates, deletions, and creations. However, the current implementation does not show explicit error handling within the transaction or mechanisms for rolling back in case of failure. Additionally, there is no logging to trace transaction steps for debugging.

  • Add error handling within the transaction to catch and manage potential failures.
  • Implement transaction rollback mechanisms to ensure data integrity.
  • Include logging to trace the steps of the transaction for easier debugging.

Code location:

  • apps/web/utils/actions/rule.ts
Analysis chain

Line range hint 90-180: Ensure robust error handling and transaction management in updateRuleAction.

The function updateRuleAction uses a transaction to handle updates, deletions, and creations of actions. Ensure that:

  1. All potential errors are handled appropriately within the transaction.
  2. The transaction is rolled back correctly in case of any failures.
  3. Adequate logging is in place to trace transaction steps for debugging purposes.

Additionally, validate that the refactoring of the action update logic does not introduce any regressions or change the expected behavior.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the transaction management logic in `updateRuleAction`.

# Test: Search for error handling patterns and transaction rollback mechanisms.
rg --type python --files-with-matches $'prisma.$transaction' | xargs -L1 gawk '/prisma.$transaction/,/catch/'

Length of output: 141



Script:

#!/bin/bash
# Description: Verify the transaction management logic in `updateRuleAction`.

# Test: Search for error handling patterns and transaction rollback mechanisms.
rg --type js --files-with-matches $'prisma.$transaction' | xargs -L1 gawk '/prisma.$transaction/,/catch/'

Length of output: 106



Script:

#!/bin/bash
# Description: Verify the transaction management logic in `updateRuleAction`.

# Test: Search for the `updateRuleAction` function definition and its transaction management logic.
ast-grep --lang typescript --pattern $'async function updateRuleAction($_) {
  $$$
  const [rule] = await prisma.$transaction([
    $$$
  ]);
  $$$
}' --json

Length of output: 161



Script:

#!/bin/bash
# Description: Verify the transaction management logic in `updateRuleAction`.

# Test: Search for the `updateRuleAction` function definition and its transaction management logic.
rg 'async function updateRuleAction' -A 50

Length of output: 3351



Script:

#!/bin/bash
# Description: Verify the transaction management logic in `updateRuleAction`.

# Test: Search for the complete implementation of the `updateRuleAction` function to confirm error handling and transaction rollback mechanisms.
rg 'export async function updateRuleAction' -A 100

Length of output: 7088

apps/web/app/(app)/automation/RuleForm.tsx (1)

Line range hint 244-301: Refine the handling of AI-generated fields in the UI.

The toggle switch for enabling AI-generated fields is a critical component. Ensure that:

  1. The state management of the toggle is consistent and error-free.
  2. User inputs are validated, especially when toggling between AI-generated and static values.
  3. The UI provides clear feedback on the state of the AI generation toggle to prevent user confusion.

Consider adding visual indicators or animations to make the toggle action more noticeable and intuitive.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3175a25 and eb7b27b.

Files selected for processing (7)
  • apps/web/app/(app)/automation/RuleForm.tsx (4 hunks)
  • apps/web/app/(app)/automation/create/examples.tsx (7 hunks)
  • apps/web/prisma/schema.prisma (1 hunks)
  • apps/web/utils/actions/rule.ts (3 hunks)
  • apps/web/utils/actions/validation.ts (1 hunks)
  • apps/web/utils/ai/choose-rule/ai-choose-args.ts (2 hunks)
  • apps/web/utils/ai/choose-rule/functions-from-rules.ts (2 hunks)
Additional context used
Biome
apps/web/utils/ai/choose-rule/ai-choose-args.ts

[error] 4-4: Do not shadow the global "Function" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

apps/web/utils/ai/choose-rule/functions-from-rules.ts

[error] 1-1: Do not shadow the global "Function" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

Additional comments not posted (5)
apps/web/utils/actions/validation.ts (1)

30-44: Refactored validation schema to use a common zodField object schema.

The refactoring to use a common zodField schema for multiple fields (label, subject, content, to, cc, bcc) promotes consistency and maintainability. This approach reduces code duplication and simplifies future modifications to the validation logic.

apps/web/utils/ai/choose-rule/ai-choose-args.ts (1)

102-113: Refactored handling of AI-generated arguments based on prompts.

The refactoring to check for prompt fields (labelPrompt, subjectPrompt, etc.) before using AI-generated or predefined values enhances the flexibility and functionality of action item generation. This allows for dynamic content generation based on AI or predefined inputs, which can significantly improve user interaction with AI features.

apps/web/app/(app)/automation/create/examples.tsx (1)

28-30: Updated action definitions to use nested objects for properties.

The restructuring of action definitions to use nested objects ({ value: "..." }) for properties like to, label, and content enhances the flexibility and maintainability of rule definitions. This change also supports the integration of AI-generated content by allowing the inclusion of an ai flag.

Also applies to: 40-43, 58-58, 72-72, 87-91, 108-108

apps/web/utils/ai/choose-rule/functions-from-rules.ts (1)

18-54: Enhanced logic for extracting AI-required properties from actions.

The changes introduce a more structured approach to determining which properties need AI generation based on the presence of prompts. This structured approach not only makes the code more maintainable but also ensures that AI capabilities are utilized effectively where needed.

apps/web/prisma/schema.prisma (1)

193-207: Validate the optional prompt fields added to the Action model.

The addition of optional prompt fields (labelPrompt, subjectPrompt, etc.) to the Action model is crucial for handling AI-generated fields. Ensure that:

  1. The fields are appropriately indexed if necessary, considering their usage in queries.
  2. The schema changes are reflected in any ORM or database interaction code that might be affected.
  3. Adequate migration scripts are prepared to handle these schema changes in production databases.
Verification successful

Validate the optional prompt fields added to the Action model.

The addition of optional prompt fields (labelPrompt, subjectPrompt, etc.) to the Action model has been properly reflected in the codebase. These fields are used in various files, ensuring that the schema changes are integrated into the ORM interactions.

  • apps/web/utils/actions/rule.ts: Multiple instances of the new fields being used.
  • apps/web/utils/ai/choose-rule/functions-from-rules.ts: Several occurrences of the new fields.
  • apps/web/utils/ai/choose-rule/ai-choose-args.ts: Usage of the new fields.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the reflection of new schema changes in ORM interactions.

# Test: Search for ORM interactions involving the new fields.
rg --type prisma $'labelPrompt|subjectPrompt' --files-with-matches | xargs -L1 gawk '/model Action/,/}/'

Length of output: 136



Script:

#!/bin/bash
# Description: Verify the reflection of new schema changes in ORM interactions.

# Test: Search for ORM interactions involving the new fields.
rg --glob '*.prisma' 'labelPrompt|subjectPrompt|contentPrompt|toPrompt|ccPrompt|bccPrompt' --files-with-matches | xargs -L1 gawk '/model Action/,/}/'

Length of output: 782



Script:

#!/bin/bash
# Description: Verify ORM interactions involving the new fields.

# Test: Search for occurrences of the new fields in the codebase.
rg 'labelPrompt|subjectPrompt|contentPrompt|toPrompt|ccPrompt|bccPrompt'

Length of output: 6078

@@ -4,7 +4,6 @@ import { Action, ActionType, User } from "@prisma/client";
import { Function } from "ai";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name shadowing issue.

The use of Function as a type name shadows the global JavaScript Function object, which can lead to confusion and potential bugs.

- import { Function } from "ai";
+ import { AI_Function } from "ai";
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { Function } from "ai";
import { AI_Function } from "ai";
Tools
Biome

[error] 4-4: Do not shadow the global "Function" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

@@ -1,8 +1,6 @@
import { Function } from "ai";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name shadowing issue.

The use of Function as a type name shadows the global JavaScript Function object, which can lead to confusion and potential bugs.

- import { Function } from "ai";
+ import { AI_Function } from "ai";
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { Function } from "ai";
import { AI_Function } from "ai";
Tools
Biome

[error] 1-1: Do not shadow the global "Function" property. (lint/suspicious/noShadowRestrictedNames)

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

Comment on lines +31 to +55
data: body.actions.map(
({ type, label, subject, content, to, cc, bcc }) => {
return {
type,
...(label?.ai
? { label: null, labelPrompt: label?.value }
: { label: label?.value, labelPrompt: null }),
...(subject?.ai
? { subject: null, subjectPrompt: subject?.value }
: { subject: subject?.value, subjectPrompt: null }),
...(content?.ai
? { content: null, contentPrompt: content?.value }
: { content: content?.value, contentPrompt: null }),
...(to?.ai
? { to: null, toPrompt: to?.value }
: { to: to?.value, toPrompt: null }),
...(cc?.ai
? { cc: null, ccPrompt: cc?.value }
: { cc: cc?.value, ccPrompt: null }),
...(bcc?.ai
? { bcc: null, bccPrompt: bcc?.value }
: { bcc: bcc?.value, bccPrompt: null }),
};
},
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor the transformation logic for AI-generated fields.

The transformation logic for handling AI-generated fields is repeated across multiple functions (createRuleAction, updateRuleAction). Consider abstracting this logic into a utility function to avoid code duplication and improve maintainability.

- // Current repeated code block
+ // Proposed utility function usage
+ data: body.actions.map(transformActionFields),

Committable suggestion was skipped due to low confidence.

Comment on lines +84 to +85
if (!hasLabel && action.label?.value) {
await createLabelAction({ name: action.label.value });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust error handling when creating labels.

The logic to create labels if they do not exist needs robust error handling. Consider adding retries or fallback mechanisms if the label creation fails, and ensure that the user is adequately informed of any issues.

+ try {
+   await createLabelAction({ name: action.label.value });
+ } catch (error) {
+   toastError({ description: 'Failed to create label: ' + error.message });
+ }

Committable suggestion was skipped due to low confidence.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Outside diff range and nitpick comments (2)
apps/web/utils/ai/group/create-group.ts (1)

Line range hint 16-56: Enhance error handling in AI response processing.

The aiCreateGroup function handles AI interactions effectively. Consider adding error handling for AI response failures to improve robustness.

+ if (!aiResponse || aiResponse.error) {
+   throw new Error('Failed to process AI response');
+ }
apps/web/utils/actions/ai-rule.ts (1)

Line range hint 243-247: Redundant else clause detected.

The else clause after checking for the newsletter group's rule can be omitted as all branches in the if-else structure return early.

-  } else if (result.group === "Receipts") {
+  } if (result.group === "Receipts") {
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eb7b27b and f81e5a6.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (26)
  • apps/web/app/(app)/automation/RuleForm.tsx (5 hunks)
  • apps/web/app/(app)/automation/rule/[ruleId]/page.tsx (1 hunks)
  • apps/web/app/api/ai/categorise/controller.ts (5 hunks)
  • apps/web/app/api/ai/cold-email/controller.ts (4 hunks)
  • apps/web/app/api/ai/compose-autocomplete/route.ts (2 hunks)
  • apps/web/app/api/ai/models/route.ts (1 hunks)
  • apps/web/app/api/ai/summarise/controller.ts (1 hunks)
  • apps/web/app/api/ai/summarise/route.ts (1 hunks)
  • apps/web/app/api/google/webhook/group-rule.ts (2 hunks)
  • apps/web/app/api/google/webhook/static-rule.ts (2 hunks)
  • apps/web/app/api/user/settings/validation.ts (2 hunks)
  • apps/web/package.json (2 hunks)
  • apps/web/prisma/migrations/20240624075134_argument_prompt/migration.sql (1 hunks)
  • apps/web/prisma/schema.prisma (1 hunks)
  • apps/web/utils/actions/ai-rule.ts (5 hunks)
  • apps/web/utils/actions/rule.ts (4 hunks)
  • apps/web/utils/actions/validation.ts (2 hunks)
  • apps/web/utils/ai/choose-rule/ai-choose-args.ts (4 hunks)
  • apps/web/utils/ai/choose-rule/ai-choose-rule.ts (3 hunks)
  • apps/web/utils/ai/choose-rule/choose.ts (3 hunks)
  • apps/web/utils/ai/group/create-group.ts (4 hunks)
  • apps/web/utils/ai/rule/create-rule.ts (3 hunks)
  • apps/web/utils/llms/index.ts (2 hunks)
  • apps/web/utils/redis/usage.ts (2 hunks)
  • apps/web/utils/types.ts (2 hunks)
  • apps/web/utils/usage.ts (7 hunks)
Files skipped from review due to trivial changes (2)
  • apps/web/prisma/migrations/20240624075134_argument_prompt/migration.sql
  • apps/web/utils/redis/usage.ts
Files skipped from review as they are similar to previous changes (4)
  • apps/web/app/(app)/automation/RuleForm.tsx
  • apps/web/prisma/schema.prisma
  • apps/web/utils/actions/rule.ts
  • apps/web/utils/actions/validation.ts
Additional context used
Biome
apps/web/utils/actions/ai-rule.ts

[error] 219-223: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 221-223: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)


[error] 243-247: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 245-247: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Additional comments not posted (24)
apps/web/app/api/user/settings/validation.ts (1)

22-24: Ensure validation logic is clear.

The validation logic within superRefine ensures that a valid AI model is selected if no OpenAI API key is provided. Consider adding a comment to clarify this specific validation scenario for future maintainability.

apps/web/app/api/ai/models/route.ts (1)

10-10: LGTM!

Initialization of the OpenAI SDK and model filtering is correctly implemented.

apps/web/app/api/ai/summarise/route.ts (1)

34-36: Effective use of caching and streaming responses.

The implementation of caching and streaming responses in the POST method is well-executed and should enhance performance and user experience.

apps/web/app/api/ai/compose-autocomplete/route.ts (3)

30-32: Clarify the purpose of the system string.

The newly added system string seems to set parameters for the AI's behavior. However, it's important to ensure this aligns with the intended functionality and user expectations. If this text guides the AI's behavior, consider externalizing it to configuration files or environment variables for easier management and updates.


44-44: Approve the return statement modification.

The updated return statement, using toTextStreamResponse, aligns with the expected response handling for a streaming text response. This change appears to be correctly implemented.


34-39: Check the integration of the chatCompletionStream function.

Ensure that the chatCompletionStream function correctly handles the new parameters (system and prompt). It's crucial that these parameters are supported and utilized correctly by the underlying AI service. Additionally, verify that the label parameter is being used as intended in the system's context.

Verification successful

The chatCompletionStream function correctly handles the new parameters system and prompt.

The implementation of the chatCompletionStream function in apps/web/utils/llms/index.ts supports the new parameters system and prompt and utilizes them appropriately. The label parameter is also used as intended within the onFinish callback.

  • File: apps/web/utils/llms/index.ts
  • Lines: 88-125

Category:

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct implementation of chatCompletionStream function.

# Test: Search for the function definition and ensure it supports the new parameters.
rg --type python $'chatCompletionStream'

Length of output: 70



Script:

#!/bin/bash
# Search for the definition of the `chatCompletionStream` function in the entire repository.
rg 'function chatCompletionStream' --glob '!*node_modules/*'

Length of output: 136



Script:

#!/bin/bash
# Extract the implementation of the `chatCompletionStream` function from `apps/web/utils/llms/index.ts`.
ast-grep --lang typescript --pattern $'export async function chatCompletionStream({
  $$$
}) {
  $$$
}' apps/web/utils/llms/index.ts

Length of output: 2048

apps/web/utils/ai/choose-rule/choose.ts (2)

30-30: Review the logic flow in chooseRule function.

The changes in the chooseRule function seem to integrate well with the overall logic of rule selection based on AI responses. However, ensure that the handling of undefined or unexpected responses (like ruleNumber being undefined) is robust and that error messages or fallbacks are clear to end-users.

Also applies to: 40-40, 42-42, 44-45, 47-51, 57-57, 61-61


5-5: Confirm integration of the getActionsWithParameters function.

Ensure that the getActionsWithParameters function is correctly integrated and used. This function should effectively identify which actions require parameters, impacting the AI's decision-making process.

Verification successful

Confirm integration of the getActionsWithParameters function.

The getActionsWithParameters function is correctly integrated and used across multiple files in the project. The function is defined in apps/web/utils/ai/choose-rule/ai-choose-args.ts and is utilized in several places, including choose.ts, group-rule.ts, and static-rule.ts.

  • Definition: apps/web/utils/ai/choose-rule/ai-choose-args.ts
  • Usage:
    • apps/web/utils/ai/choose-rule/choose.ts
    • apps/web/app/api/google/webhook/group-rule.ts
    • apps/web/app/api/google/webhook/static-rule.ts
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct implementation and usage of getActionsWithParameters.

# Test: Search for the function definition and usage across the project.
rg --type python $'getActionsWithParameters'

Length of output: 74



Script:

#!/bin/bash
# Description: Verify the correct implementation and usage of getActionsWithParameters.

# Test: Search for the function definition and usage across the project.
rg --type typescript 'getActionsWithParameters'

Length of output: 82



Script:

#!/bin/bash
# Description: Verify the correct implementation and usage of getActionsWithParameters.

# Test: Search for the function definition and usage across the project.
rg 'getActionsWithParameters'

Length of output: 787

apps/web/app/(app)/automation/rule/[ruleId]/page.tsx (1)

57-60: Approve the new description component.

The addition of a description component that informs the user about AI-generated rules is a good UX practice. It sets proper expectations for the users about the nature of the rules they are interacting with.

apps/web/utils/types.ts (1)

12-15: Confirm the updated type definition for RuleWithActions.

The update to the RuleWithActions type definition to include actions explicitly is crucial for ensuring type safety and clarity throughout the application. Verify that this new type is used consistently wherever rules with actions are handled.

Verification successful

Confirm the updated type definition for RuleWithActions.

The updated RuleWithActions type definition is consistently used across the project. This ensures type safety and clarity when handling rules with actions.

  • apps/web/utils/ai/choose-rule/run-rules.ts
  • apps/web/utils/ai/choose-rule/ai-choose-args.ts
  • apps/web/utils/ai/choose-rule/choose.ts
  • apps/web/app/api/google/webhook/static-rule.ts
  • apps/web/app/api/google/webhook/route.ts
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the consistent use of the updated RuleWithActions type across the project.

# Test: Search for the usage of RuleWithActions in the project.
rg --type python $'RuleWithActions'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify the consistent use of the updated RuleWithActions type across the project.

# Test: Search for the usage of RuleWithActions in the project.
rg --type typescript 'RuleWithActions'

Length of output: 73



Script:

#!/bin/bash
# Description: Verify the consistent use of the updated RuleWithActions type across the project.

# Test: Search for the usage of RuleWithActions in the project.
rg --glob '*.ts' 'RuleWithActions'

Length of output: 1418

apps/web/utils/usage.ts (1)

18-20: Good structuring, ensure consistent naming conventions.

The restructuring of the usage object and its use in various functions is well done, enhancing clarity and maintainability. Ensure that the naming conventions are consistent across the entire codebase to avoid confusion.

Also applies to: 30-32, 85-87, 126-127, 134-134

apps/web/utils/ai/rule/create-rule.ts (1)

81-96: Well-implemented AI interaction, consider adding documentation.

The setup of system and prompt strings for AI interactions is well-implemented. Consider adding documentation or comments explaining the purpose and expected outcomes of these AI interactions to help future developers.

apps/web/utils/llms/index.ts (1)

1-6: Robust AI interaction setup, add detailed comments.

The updates to AI interaction functions and the setup for fetching AI models are robust and align with best practices. Consider adding more detailed comments explaining each function's role and the expected types of interactions, which will enhance maintainability and readability.

Also applies to: 32-36, 41-125

apps/web/app/api/ai/categorise/controller.ts (5)

38-40: Clarification of system constant usage.

The system constant defines the role of the AI in a straightforward manner. This is good as it sets clear expectations for the AI's functionality.


40-40: Detailed and clear AI prompt.

The prompt provided to the AI model is well-detailed, explaining the categories clearly and setting expectations for the AI's response. This should help in achieving accurate categorizations.

Also applies to: 52-52


86-93: Efficient handling of AI response.

The handling of the AI response after calling chatCompletionObject is done efficiently, with checks for response.usage and saving this usage data. This is crucial for tracking and auditing AI usage.


120-120: Handling of insufficient information scenario.

The method handles cases where the AI requires more information to make a decision by expanding the email content. This is a good practice as it ensures that decisions are made with sufficient data.


125-130: Proper handling of missing category.

The function correctly handles cases where no category could be determined by the AI, by simply returning without saving any category. This prevents incorrect data entries.

apps/web/package.json (1)

15-16: Updated AI SDK dependencies.

The updates to @ai-sdk/anthropic and @ai-sdk/openai are crucial to keep the project in sync with the latest AI capabilities and improvements. It's important to verify that these updates do not introduce breaking changes.

apps/web/app/api/ai/cold-email/controller.ts (2)

49-52: Clear definition of AI's role and detailed prompt.

The constants system and prompt are well-defined, providing clear instructions to the AI. This should aid in accurate determination of whether an email is considered a cold email.


74-81: Efficient handling of AI interactions and usage tracking.

The interaction with the AI through chatCompletionObject and the subsequent handling of the AI usage data are done efficiently. This ensures that the AI's decisions are well-documented and traceable.

apps/web/utils/ai/choose-rule/ai-choose-args.ts (2)

19-41: Efficient handling of action parameters.

The function getToolParametersForRule effectively handles the extraction and organization of parameters needed for AI tools. The handling of potential duplicate keys is a good preventive measure.


103-105: Comprehensive AI prompt construction.

The construction of the AI prompt in getArgsAiResponse is thorough, providing all necessary details about the user and the email. This should help in generating accurate AI responses.

Also applies to: 108-111

apps/web/utils/actions/ai-rule.ts (1)

190-190: Check for user email before proceeding.

The function createAutomationAction correctly checks for the presence of the user's email before proceeding. This is crucial as the email is used in subsequent operations.

Comment on lines +64 to +71
const aiArgsResponse =
getActionsWithParameters(match.rule.actions).length > 0
? await getArgsAiResponse({
email,
selectedRule: match.rule,
user,
})
: undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust error handling in AI argument fetching.

The addition of AI argument fetching based on the presence of parameters is a good integration. However, consider adding error handling around the getArgsAiResponse call to manage potential failures gracefully.

Comment on lines +56 to +63
const aiArgsResponse =
getActionsWithParameters(staticRule.actions).length > 0
? await getArgsAiResponse({
email,
selectedRule: staticRule,
user,
})
: undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure robust error handling in AI argument fetching.

The conditional AI argument fetching is well-integrated. Similar to the group-rule.ts file, consider adding error handling around the getArgsAiResponse call to manage potential failures gracefully.

Comment on lines 5 to 28
export async function summarise(text: string, userEmail: string) {
const model = "gpt-3.5-turbo-0125" as const;
const messages = [
{
role: "system" as const,
content: `You are an email assistant. You summarise emails.

const system = `You are an email assistant. You summarise emails.
Summarise each email in a short ~5 word sentence.
If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words.`,
},
{
role: "user" as const,
content: `Summarise this:
${text}`,
},
];
If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words.`;

const response = await chatCompletionStream("openai", model, null, messages);
const prompt = `Summarise this:\n${text}`;

const stream = await saveAiUsageStream({
response,
const response = await chatCompletionStream({
provider: "openai",
model,
apiKey: null,
system,
prompt,
userEmail,
messages,
label: "Summarise",
onFinal,
onFinish: async (completion) => {
await saveSummary(prompt, completion);
await expire(prompt, 60 * 60 * 24);
},
});

return stream;
return response;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion for clarity and maintainability.

The summarise function is well-structured. However, consider refactoring the prompt construction into a separate function to improve clarity and maintainability.

+ function constructPrompt(text: string): string {
+   return `Summarise this:\n${text}`;
+ }
- const prompt = `Summarise this:\n${text}`;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export async function summarise(text: string, userEmail: string) {
const model = "gpt-3.5-turbo-0125" as const;
const messages = [
{
role: "system" as const,
content: `You are an email assistant. You summarise emails.
const system = `You are an email assistant. You summarise emails.
Summarise each email in a short ~5 word sentence.
If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words.`,
},
{
role: "user" as const,
content: `Summarise this:
${text}`,
},
];
If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words.`;
const response = await chatCompletionStream("openai", model, null, messages);
const prompt = `Summarise this:\n${text}`;
const stream = await saveAiUsageStream({
response,
const response = await chatCompletionStream({
provider: "openai",
model,
apiKey: null,
system,
prompt,
userEmail,
messages,
label: "Summarise",
onFinal,
onFinish: async (completion) => {
await saveSummary(prompt, completion);
await expire(prompt, 60 * 60 * 24);
},
});
return stream;
return response;
export async function summarise(text: string, userEmail: string) {
const model = "gpt-3.5-turbo-0125" as const;
const system = `You are an email assistant. You summarise emails.
Summarise each email in a short ~5 word sentence.
If you need to summarise a longer email, you can use bullet points. Each bullet should be ~5 words.`;
function constructPrompt(text: string): string {
return `Summarise this:\n${text}`;
}
const prompt = constructPrompt(text);
const response = await chatCompletionStream({
provider: "openai",
model,
apiKey: null,
system,
prompt,
userEmail,
label: "Summarise",
onFinish: async (completion) => {
await saveSummary(prompt, completion);
await expire(prompt, 60 * 60 * 24);
},
});
return response;

Comment on lines +26 to +49
const ruleWithActions = {
...rule,
actions: rule.actions.map((action) => ({
...action,
label: action.labelPrompt
? { value: action.labelPrompt, ai: true }
: { value: action.label, ai: false },
subject: action.subjectPrompt
? { value: action.subjectPrompt, ai: true }
: { value: action.subject, ai: false },
content: action.contentPrompt
? { value: action.contentPrompt, ai: true }
: { value: action.content, ai: false },
to: action.toPrompt
? { value: action.toPrompt, ai: true }
: { value: action.to, ai: false },
cc: action.ccPrompt
? { value: action.ccPrompt, ai: true }
: { value: action.cc, ai: false },
bcc: action.bccPrompt
? { value: action.bccPrompt, ai: true }
: { value: action.bcc, ai: false },
})),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assess the restructuring of rule actions.

The restructuring of rule actions to include AI prompts is a significant change. It's crucial to ensure that this new structure is supported throughout the application, especially in areas where rule actions are processed or displayed. Consider adding tests to verify that both AI and non-AI fields behave as expected in different scenarios.

import { parseJSON } from "@/utils/json";
import { saveAiUsage } from "@/utils/usage";
import { chatCompletion, getAiProviderAndModel } from "@/utils/llms";
import { chatCompletionObject, getAiProviderAndModel } from "@/utils/llms";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review updates to AI response generation logic.

The changes in the getAiResponse function, including the addition of a default rule and updates to the AI prompt, are significant. It's important to ensure that these changes enhance the AI's ability to make informed decisions and that the responses are handled correctly in the application. Additionally, verify that the zod schema used for validating AI responses is appropriate and effective.

Also applies to: 17-17, 21-21, 23-29, 31-44, 46-54, 60-70, 82-82

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
apps/web/app/api/ai/categorise/controller.ts (2)

Line range hint 38-111: Refactor the AI categorization prompt for clarity and maintainability.

The AI categorization prompt within aiCategorise is verbose and mixes logic with presentation. Consider refactoring this to separate the prompt template from the function logic, possibly using a template literal or an external template function. This will improve readability and maintainability.

- const prompt = `Categorize this email. ... `;
+ const prompt = generatePrompt(body, expanded);

Line range hint 111-132: Handle potential undefined category and improve logging.

The function categorise does not handle the case where category.object.category might be undefined before saving to the database. Also, consider enhancing the logging for the case where more information is required to categorize the email.

-  if (!category.object.category) return;
+  if (!category.object.category) {
+    console.error("Failed to categorize email due to undefined category.");
+    return;
+  }
apps/web/components/email-list/EmailList.tsx (1)

Line range hint 220-239: Optimize AI categorization error handling and feedback.

The error handling in onAiCategorize can be optimized. The else clause after the error check is unnecessary and can be removed for cleaner code.

-          } else {
-            // setCategory(res);
-            refetch();
-          }
+          refetch();

The categorization logic is well-structured and uses promises effectively.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f81e5a6 and 5ab7205.

Files selected for processing (3)
  • apps/web/app/api/ai/categorise/controller.ts (5 hunks)
  • apps/web/components/email-list/EmailList.tsx (5 hunks)
  • apps/web/utils/actions/categorize.ts (1 hunks)
Additional context used
Biome
apps/web/components/email-list/EmailList.tsx

[error] 233-236: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.

Additional comments not posted (1)
apps/web/utils/actions/categorize.ts (1)

17-65: Ensure error handling and data validation are robust.

The function categorizeAction performs several operations including database access, email content parsing, and AI categorization. Ensure that:

  • The error handling for each asynchronous operation is robust.
  • Input validation is thorough, especially since unsafeBody is being parsed directly.

Consider adding more specific error messages and handling potential exceptions from database calls and external API requests.

@elie222 elie222 merged commit 0ca1aa3 into main Jun 25, 2024
@elie222 elie222 deleted the arg-prompt branch June 25, 2024 15:25
This was referenced Oct 20, 2024
This was referenced Oct 30, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 12, 2024
This was referenced Nov 22, 2024
@coderabbitai coderabbitai bot mentioned this pull request Apr 7, 2025
@coderabbitai coderabbitai bot mentioned this pull request Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments