Skip to content
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

Fix bug causing generateEmbeddings to run when it shouldn't #53

Merged
merged 6 commits into from
Jul 31, 2024

Conversation

justyns
Copy link
Owner

@justyns justyns commented Jul 31, 2024

This fixes two issues mentioned in silverbulletmd/silverbullet#1002 and #52

1: Error dispatching event page:index to silverbullet-ai.indexEmbeddings: Cannot read properties of undefined (reading 'generateEmbeddings')
2: indexEmbeddings was being ignored

Summary by CodeRabbit

  • New Features

    • Introduced mock provider classes for enhanced testing capabilities.
    • Added new functions to streamline the indexing of embeddings and summaries.
    • Expanded provider setup to include mock providers for easier development.
  • Bug Fixes

    • Improved error handling in embedding generation to ensure necessary conditions are met.
  • Documentation

    • Enhanced internal testing suite to ensure reliable functionality related to AI settings.
  • Tests

    • Added a comprehensive suite of unit tests to validate embedding and summary indexing logic.
  • Chores

    • Updated code structure and organization to improve maintainability and modularity.

Copy link
Contributor

coderabbitai bot commented Jul 31, 2024

Walkthrough

Walkthrough

This update extensively enhances the indexing functionality for AI settings within the application. Key changes include the introduction of mock providers for testing and the refactoring of existing functions to improve modularity and maintainability. New enums allow for the inclusion of mock options, facilitating easier testing scenarios. Overall, these modifications significantly strengthen the application’s architecture while providing robust testing capabilities.

Changes

Files Change Summary
src/embeddings.test.ts Added unit tests for AI settings, focusing on indexing functions with mock syscalls to simulate environments.
src/embeddings.ts Refactored indexing logic, introduced new functions for conditional indexing, improved error handling, and made several functions exportable.
src/init.ts Added mock provider cases in setup functions to enhance testing flexibility with mock implementations.
src/mocks/mockproviders.ts Created mock provider classes for simulation of AI interactions, image generation, and embedding generation.
src/mocks/syscalls.ts Enhanced syscall method for state management with a client store and added new function invocation cases.
src/types.ts Introduced Mock enumeration values for providers to support testing and simulation options.

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 Configuration 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: 0

Outside diff range, codebase verification and nitpick comments (1)
src/embeddings.ts (1)

Line range hint 72-103: LGTM!

The indexEmbeddings function is well-structured and the logic is clear.

Reminder: Address the TODO comment.

The TODO comment suggests filtering out images or sending them to a vision model for summary.

Do you want me to generate the code for this TODO or open a GitHub issue to track this task?

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d8a845a and e0d6ec0.

Files selected for processing (6)
  • src/embeddings.test.ts (1 hunks)
  • src/embeddings.ts (5 hunks)
  • src/init.ts (4 hunks)
  • src/mocks/mockproviders.ts (1 hunks)
  • src/mocks/syscalls.ts (2 hunks)
  • src/types.ts (1 hunks)
Additional context used
Path-based instructions (6)
src/mocks/mockproviders.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

src/mocks/syscalls.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

src/types.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

src/embeddings.test.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

src/init.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

src/embeddings.ts (1)

Pattern **/*.ts: Review these typescript files for best practices, readability, and DRY.

Additional comments not posted (27)
src/mocks/mockproviders.ts (3)

10-27: LGTM! Consider adding type annotations for better clarity.

The class is well-structured and simulates AI chat responses effectively. Adding type annotations to the constructor parameters and the chatWithAI method would improve code readability and maintainability.

-  constructor(
-    apiKey: string,
-    modelName: string,
-    baseUrl: string = "http://localhost",
-  ) {
+  constructor(
+    apiKey: string,
+    modelName: string,
+    baseUrl: string = "http://localhost",
+  ) {
+    super(apiKey, baseUrl, "mock", modelName);
+  }

-  async chatWithAI(options: StreamChatOptions): Promise<any> {
+  async chatWithAI(options: StreamChatOptions): Promise<string> {

31-46: LGTM! Consider adding type annotations for better clarity.

The class is well-structured and simulates image generation effectively. Adding type annotations to the constructor parameters and the generateImage method would improve code readability and maintainability.

-  constructor(
-    apiKey: string,
-    modelName: string,
-    baseUrl: string = "http://localhost",
-  ) {
+  constructor(
+    apiKey: string,
+    modelName: string,
+    baseUrl: string = "http://localhost",
+  ) {
+    super(apiKey, baseUrl, "mock", modelName);
+  }

-  generateImage(options: ImageGenerationOptions): Promise<string> {
+  generateImage(options: ImageGenerationOptions): Promise<string> {

49-67: LGTM! Consider adding type annotations for better clarity.

The class is well-structured and simulates embedding generation effectively. Adding type annotations to the constructor parameters and the _generateEmbeddings method would improve code readability and maintainability.

-  constructor(
-    apiKey: string,
-    modelName: string,
-    baseUrl: string = "http://localhost",
-  ) {
+  constructor(
+    apiKey: string,
+    modelName: string,
+    baseUrl: string = "http://localhost",
+  ) {
+    super(apiKey, baseUrl, "mock", modelName);
+  }

-  _generateEmbeddings(
-    options: EmbeddingGenerationOptions,
-  ): Promise<Array<number>> {
+  _generateEmbeddings(
+    options: EmbeddingGenerationOptions,
+  ): Promise<Array<number>> {
src/mocks/syscalls.ts (5)

55-57: LGTM!

The implementation of the clientStore.set case is straightforward and correct.


58-59: LGTM!

The implementation of the clientStore.get case is straightforward and correct.


50-51: LGTM!

The implementation of the system.invokeFunctionOnServer case is straightforward and correct.


52-53: LGTM!

The implementation of the system.invokeFunction case is straightforward and correct.


66-73: LGTM! Consider adding more commands as needed.

The implementation of the invokeFunctionMock function is straightforward and correct. Adding more commands as needed would improve its utility.

src/types.ts (3)

65-66: LGTM!

The addition of the Mock value to the Provider enum is straightforward and correct.


71-72: LGTM!

The addition of the Mock value to the ImageProvider enum is straightforward and correct.


79-80: LGTM!

The addition of the Mock value to the EmbeddingProvider enum is straightforward and correct.

src/embeddings.test.ts (8)

54-63: LGTM!

The test case for canIndexPage is well-structured and verifies multiple conditions.


65-73: LGTM!

The test case for shouldIndexEmbeddings is well-structured and verifies the expected behavior when conditions are met.


75-83: LGTM!

The test case for shouldIndexEmbeddings is well-structured and verifies the expected behavior when not on the server.


85-97: LGTM!

The test case for shouldIndexEmbeddings is well-structured and verifies the expected behavior when indexEmbeddings is disabled.


99-111: LGTM!

The test case for shouldIndexSummaries is well-structured and verifies the expected behavior when conditions are met.


113-121: LGTM!

The test case for shouldIndexSummaries is well-structured and verifies the expected behavior when indexSummary is disabled.


123-131: LGTM!

The test case for shouldIndexEmbeddings is well-structured and verifies the expected behavior when no embedding models are configured.


133-141: LGTM!

The test case for shouldIndexSummaries is well-structured and verifies the expected behavior when no embedding models are configured.

src/init.ts (3)

152-157: LGTM!

The new case for MockImageProvider in setupImageProvider is well-structured.


179-185: LGTM!

The new case for MockProvider in setupAIProvider is well-structured.


219-225: LGTM!

The new case for MockEmbeddingProvider in setupEmbeddingProvider is well-structured.

src/embeddings.ts (5)

Line range hint 25-41: LGTM!

The canIndexPage function is well-structured and the logic is clear.


48-55: LGTM!

The shouldIndexEmbeddings function is well-structured and the logic is clear.


57-65: LGTM!

The shouldIndexSummaries function is well-structured and the logic is clear.


Line range hint 146-195: LGTM!

The indexSummary function is well-structured and the logic is clear.


238-240: LGTM!

The generateEmbeddings function is well-structured and the logic is clear.

@justyns justyns merged commit d9d6027 into main Jul 31, 2024
3 checks passed
@justyns justyns deleted the fix-undefined-error branch July 31, 2024 08:07
@coderabbitai coderabbitai bot mentioned this pull request Sep 8, 2024
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