Skip to content

feat(wren-ui): API enhancement #1781

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

Merged
merged 16 commits into from
Jun 27, 2025
Merged

feat(wren-ui): API enhancement #1781

merged 16 commits into from
Jun 27, 2025

Conversation

wwwy3y3
Copy link
Member

@wwwy3y3 wwwy3y3 commented Jun 24, 2025

This pull request introduces several enhancements and fixes across multiple files, focusing on API functionality, error handling, and support for server-sent events (SSE). Key changes include adding new API types, improving error handling, and introducing SSE types for real-time streaming support.

API Enhancements:

  • Added new API types such as CREATE_INSTRUCTION, UPDATE_INSTRUCTION, GENERATE_SUMMARY, and others to ApiType in wren-ui/src/apollo/client/graphql/__types__.ts and wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts. These changes expand the system's capabilities for handling various operations. [1] [2] [3]

Error Handling Improvements:

  • Introduced new error codes (SQL_EXECUTION_ERROR) and corresponding messages in wren-ui/src/apollo/server/utils/error.ts to improve error classification and debugging. [1] [2] [3]

Dashboard Scheduling Updates:

  • Updated DashboardSchedule and DetailedDashboard types to make certain fields optional, improving flexibility in handling dashboard schedules. [1] [2] [3]

Server-Sent Events (SSE) Support:

  • Added EventType, StateType, and related interfaces in wren-ui/src/apollo/server/utils/sseTypes.ts to enable real-time streaming APIs with structured event handling.

Utility Enhancements:

  • Added validation utilities for AI results and SQL syntax in wren-ui/src/apollo/server/utils/apiUtils.ts, improving robustness and error handling for AI and SQL-related operations. [1] [2]

Summary by CodeRabbit

  • New Features

    • Introduced new API endpoints for question answering, SQL generation, summary generation, and model retrieval, including both synchronous and streaming variants.
    • Added endpoints for managing instructions and SQL/question pairs, supporting creation, update, deletion, and retrieval.
    • Implemented real-time streaming of SQL generation and answer summaries using Server-Sent Events (SSE).
    • Enhanced error handling with new error codes and detailed validation for SQL and AI-generated results.
  • Bug Fixes

    • Improved status code handling in API history to recognize all 2xx codes as successful.
    • Made certain schedule-related fields optional to better handle dashboards without schedules.
  • Documentation

    • Added and updated type definitions and enums to support new API operations and streaming event types.
  • Chores

    • Centralized and re-exported utility functions for easier use across the application.

Copy link
Contributor

coderabbitai bot commented Jun 24, 2025

Walkthrough

This update introduces a comprehensive set of new API endpoints and supporting utilities for question-answering, SQL generation, summary generation, instruction management, and SQL pair management, including both synchronous and streaming (SSE) variants. It expands type definitions, error handling, and adds new utility modules for server-sent events and validation. Several existing types are made more flexible by allowing optional or nullable fields.

Changes

File(s) Change Summary
.../graphql/types.ts, .../schema.ts, .../apiHistoryRepository.ts Extended ApiType enum with new values for ask, summary, instruction, SQL pair, model, and streaming operations. Adjusted related types to allow optional/nullable fields.
.../dashboard.generated.ts Made schedule and its nested fields in DashboardQuery result optional and nullable.
.../apiHistoryResolver.ts Modified responsePayload resolver to handle array payloads directly, bypassing sanitization.
.../services/instructionService.ts Added getInstruction(id) method to instruction service interface and implementation.
.../utils/apiUtils.ts, .../utils/error.ts Added validation, error codes, and response utilities for AI results and SQL. Introduced new error code for SQL execution.
.../utils/index.ts, .../utils/sseTypes.ts, .../utils/sseUtils.ts Added and re-exported new SSE event types and utility functions for handling server-sent events.
.../history.tsx Updated status code rendering logic to treat all 2xx as success; made payload access safer.
.../api/v1/ask.ts, .../api/v1/generate_summary.ts, .../api/v1/models.ts Added new API endpoints for question-answering, summary generation, and model listing.
.../api/v1/generate_sql.ts Refactored to use shared utilities for result validation and transformation.
.../api/v1/knowledge/instructions/[id].ts, .../instructions/index.ts Added endpoints for updating, deleting, listing, and creating instructions, with validation and error handling.
.../api/v1/knowledge/sql_pairs/[id].ts, .../sql_pairs/index.ts Added endpoints for updating, deleting, listing, and creating SQL pairs with input validation and SQL syntax checking.
.../api/v1/stream/ask.ts, .../stream/generate_sql.ts Added streaming endpoints for question-answering and SQL generation using SSE, with incremental status/content updates and robust error handling.

Sequence Diagram(s)

Streaming Question-Answering Flow (/api/v1/stream/ask)

sequenceDiagram
    participant Client
    participant API
    participant ProjectService
    participant AIAdaptor
    participant QueryService

    Client->>API: POST /api/v1/stream/ask (question, ...)
    API->>ProjectService: getCurrentProject()
    ProjectService-->>API: project
    API->>AIAdaptor: start SQL generation (question, history)
    loop Poll until SQL generation finished or timeout
        API->>AIAdaptor: get SQL generation status
        AIAdaptor-->>API: status/result
        API-->>Client: SSE state update
    end
    alt SQL generated
        API->>QueryService: execute SQL
        QueryService-->>API: data
        API->>AIAdaptor: start summary generation (question, SQL, data)
        loop Poll until summary finished or timeout
            API->>AIAdaptor: get summary status
            AIAdaptor-->>API: status/result
            API-->>Client: SSE content block (summary)
        end
    else General response
        API-->>Client: SSE content block (explanation)
    end
    API-->>Client: SSE message stop & end stream
Loading

Instruction Management API (/api/v1/knowledge/instructions)

sequenceDiagram
    participant Client
    participant API
    participant ProjectService
    participant InstructionService

    Client->>API: GET/POST /knowledge/instructions
    API->>ProjectService: getCurrentProject()
    ProjectService-->>API: project
    alt GET
        API->>InstructionService: getAllInstructions(projectId)
        InstructionService-->>API: instructions
        API-->>Client: instructions list
    else POST
        API->>InstructionService: createInstruction(projectId, data)
        InstructionService-->>API: new instruction
        API-->>Client: new instruction
    end
Loading

Possibly related PRs

Suggested reviewers

  • andreashimin

Poem

🐇
New APIs bloom, instructions take flight,
SQL pairs and summaries, streaming in light.
SSE events hop down the wire,
With questions and answers that never tire.
Optional fields—so flexible, so neat—
This bunny thinks these changes are sweet!

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment
  • Commit Unit Tests in branch feature/api-enhancement

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@wwwy3y3 wwwy3y3 changed the title Feature/api enhancement feat(wren-ui): API enhancement Jun 24, 2025
Copy link
Contributor

@onlyjackfrost onlyjackfrost left a comment

Choose a reason for hiding this comment

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

Personally, I prefer using stream as prefix instead of async for APIs under async folder

@wwwy3y3 wwwy3y3 marked this pull request as ready for review June 26, 2025 18:13
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: 10

♻️ Duplicate comments (1)
wren-ui/src/apollo/server/utils/sseTypes.ts (1)

1-1: Consider relocating this file as suggested in previous review.

A previous reviewer suggested moving this file under /models directory, which would better align with the project's architectural patterns for type definitions.

🧹 Nitpick comments (11)
wren-ui/src/pages/api/v1/models.ts (1)

51-64: Consider adding requestPayload for consistency.

Even though GET requests typically don't have a body, consider explicitly passing requestPayload: undefined for consistent API history tracking.

     await respondWithSimple({
       res,
       statusCode: 200,
       responsePayload: {
         hash: lastDeploy.hash,
         models,
         relationships,
         views,
       },
       projectId: project.id,
       apiType: ApiType.GET_MODELS,
       startTime,
+      requestPayload: undefined,
       headers: req.headers as Record<string, string>,
     });
wren-ui/src/pages/api/v1/ask.ts (1)

144-145: Use optional chaining for safer property access.

Replace the logical AND operators with optional chaining for cleaner and safer code.

-          if (match && match[1]) {
+          if (match?.[1]) {

Also applies to: 268-269

wren-ui/src/pages/api/v1/knowledge/sql_pairs/index.ts (1)

71-77: Extract validation constants for better maintainability.

Magic numbers should be extracted as named constants for easier maintenance and consistency across the codebase.

+const MAX_SQL_LENGTH = 10000;
+const MAX_QUESTION_LENGTH = 1000;
+
 if (sql.length > 10000) {
-  throw new ApiError('SQL is too long (max 10000 characters)', 400);
+  throw new ApiError(`SQL is too long (max ${MAX_SQL_LENGTH} characters)`, 400);
 }

 if (question.length > 1000) {
-  throw new ApiError('Question is too long (max 1000 characters)', 400);
+  throw new ApiError(`Question is too long (max ${MAX_QUESTION_LENGTH} characters)`, 400);
 }
wren-ui/src/pages/api/v1/generate_summary.ts (1)

146-151: Consider a more robust streaming data parser.

The current regex-based parsing of streamed chunks might be fragile if the message format changes. Consider using a more structured approach or at least documenting the expected format.

Would you like me to suggest a more robust streaming parser that handles edge cases like partial chunks or format variations?

wren-ui/src/apollo/server/utils/sseUtils.ts (1)

94-115: Add logging for unexpected AskResultStatus values.

The default case silently returns SQL_GENERATION_UNDERSTANDING, which might hide unexpected status values. Consider logging a warning to help with debugging.

+import { getLogger } from '@server/utils';
+
+const logger = getLogger('SSE_UTILS');
+
 export const getSqlGenerationState = (status: AskResultStatus): StateType => {
   switch (status) {
     // ... existing cases ...
     default:
+      logger.warn(`Unexpected AskResultStatus: ${status}`);
       return StateType.SQL_GENERATION_UNDERSTANDING;
   }
 };
wren-ui/src/pages/api/v1/knowledge/instructions/index.ts (2)

49-52: Extract boolean conversion logic to a helper function.

The same boolean conversion logic is repeated. Extract it to reduce duplication and improve maintainability.

+const normalizeIsGlobal = (isDefault: any): boolean => {
+  return typeof isDefault === 'boolean' ? isDefault : Boolean(isDefault);
+};
+
 // In handleGetInstructions:
-const isGlobalValue =
-  typeof instruction.isDefault === 'boolean'
-    ? instruction.isDefault
-    : Boolean(instruction.isDefault);
+const isGlobalValue = normalizeIsGlobal(instruction.isDefault);

 // In handleCreateInstruction:
-const isGlobalValue =
-  typeof newInstruction.isDefault === 'boolean'
-    ? newInstruction.isDefault
-    : Boolean(newInstruction.isDefault);
+const isGlobalValue = normalizeIsGlobal(newInstruction.isDefault);

Also applies to: 147-150


95-97: Clarify the validation logic for instruction type determination.

The current validation using isNil for both fields might be confusing. Consider making the logic more explicit.

-if (isNil(isGlobal) && isNil(questions)) {
-  throw new ApiError('isGlobal or questions is required', 400);
-}
+// If isGlobal is not specified, we need questions to determine instruction type
+if (isGlobal === undefined && (!questions || questions.length === 0)) {
+  throw new ApiError('Either isGlobal must be specified or questions must be provided', 400);
+}
wren-ui/src/pages/api/v1/async/ask.ts (2)

238-243: Improve null safety with optional chaining.

The code can be simplified and made safer using optional chaining as suggested by static analysis.

-          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
-          if (match && match[1]) {
+          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
+          if (match?.[1]) {
             // Send incremental content updates
-            explanation += match[1];
-            sendContentBlockDelta(res, match[1]);
+            explanation += match[1];
+            sendContentBlockDelta(res, match[1]);
           }

395-400: Improve null safety with optional chaining.

Consistent with the previous suggestion, use optional chaining for cleaner and safer code.

-          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
-          if (match && match[1]) {
+          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
+          if (match?.[1]) {
             summary += match[1];
             // Send incremental content updates
-            sendContentBlockDelta(res, match[1]);
+            sendContentBlockDelta(res, match[1]);
           }
wren-ui/src/apollo/server/utils/sseTypes.ts (1)

12-25: Review state transition logic for consistency.

The StateType enum contains comprehensive SQL generation states, but there's potential inconsistency in naming patterns. Some states have specific terminal statuses (SUCCESS, FAILED, STOPPED) while others use more general terms (START, END). Consider ensuring consistent naming conventions across all state transitions.

Additionally, verify that these states align with the actual SQL generation pipeline flow to prevent state management issues.

wren-ui/src/apollo/server/utils/apiUtils.ts (1)

128-149: Improve parameter typing for better type safety.

The function parameters are typed as any, which reduces type safety and IntelliSense support. Consider using proper interfaces or types for the project, deployService, and queryService parameters.

export const validateSql = async (
  sql: string,
-  project: any,
-  deployService: any,
-  queryService: any,
+  project: { id: number },
+  deployService: DeployService,
+  queryService: QueryService,
) => {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4bf4e1 and 6d3cbf9.

📒 Files selected for processing (22)
  • wren-ui/src/apollo/client/graphql/__types__.ts (4 hunks)
  • wren-ui/src/apollo/client/graphql/dashboard.generated.ts (1 hunks)
  • wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts (2 hunks)
  • wren-ui/src/apollo/server/resolvers/apiHistoryResolver.ts (1 hunks)
  • wren-ui/src/apollo/server/schema.ts (1 hunks)
  • wren-ui/src/apollo/server/services/instructionService.ts (2 hunks)
  • wren-ui/src/apollo/server/utils/apiUtils.ts (2 hunks)
  • wren-ui/src/apollo/server/utils/error.ts (3 hunks)
  • wren-ui/src/apollo/server/utils/index.ts (1 hunks)
  • wren-ui/src/apollo/server/utils/sseTypes.ts (1 hunks)
  • wren-ui/src/apollo/server/utils/sseUtils.ts (1 hunks)
  • wren-ui/src/pages/api-management/history.tsx (2 hunks)
  • wren-ui/src/pages/api/v1/ask.ts (1 hunks)
  • wren-ui/src/pages/api/v1/async/ask.ts (1 hunks)
  • wren-ui/src/pages/api/v1/async/generate_sql.ts (1 hunks)
  • wren-ui/src/pages/api/v1/generate_sql.ts (1 hunks)
  • wren-ui/src/pages/api/v1/generate_summary.ts (1 hunks)
  • wren-ui/src/pages/api/v1/knowledge/instructions/[id].ts (1 hunks)
  • wren-ui/src/pages/api/v1/knowledge/instructions/index.ts (1 hunks)
  • wren-ui/src/pages/api/v1/knowledge/sql_pairs/[id].ts (1 hunks)
  • wren-ui/src/pages/api/v1/knowledge/sql_pairs/index.ts (1 hunks)
  • wren-ui/src/pages/api/v1/models.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (19)
📓 Common learnings
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: cyyeh
PR: Canner/WrenAI#1112
File: wren-ai-service/src/pipelines/generation/sql_question.py:76-77
Timestamp: 2025-01-14T07:41:14.564Z
Learning: In the WrenAI codebase, extensive error handling for JSON parsing and array access operations in pipeline post-processing functions is not required, as indicated by the maintainer's preference to skip such suggestions.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
wren-ui/src/apollo/server/utils/error.ts (3)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
wren-ui/src/apollo/server/utils/index.ts (1)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/apollo/server/services/instructionService.ts (3)
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/services/instructionService.ts:107-146
Timestamp: 2025-03-17T09:16:53.878Z
Learning: In the WrenAI project, the instructionResolver merges the instruction ID (from args.where) and project ID (from the current project context) with instruction data before passing to the instructionService, allowing the GraphQL schema to have separate input types while the service layer works with complete objects.
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/adaptors/wrenAIAdaptor.ts:477-494
Timestamp: 2025-03-12T09:51:33.688Z
Learning: The instruction deletion API in the WrenAI service handles bulk deletion atomically - either all instructions are deleted or none are. There's no need for handling partial deletion scenarios.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/pages/api-management/history.tsx (2)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
wren-ui/src/apollo/server/resolvers/apiHistoryResolver.ts (1)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/pages/api/v1/generate_sql.ts (4)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
wren-ui/src/apollo/server/schema.ts (4)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
Learnt from: andreashimin
PR: Canner/WrenAI#1117
File: wren-ui/src/apollo/server/backgrounds/chart.ts:86-86
Timestamp: 2025-01-15T03:50:47.868Z
Learning: In the WrenAI codebase, ChartType is maintained as a string in the web database, while the ChartType enum is specifically used for AI adaptor input/output interfaces. Therefore, string case conversions like toUpperCase() are intentional for maintaining data consistency in the web DB.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts (4)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1117
File: wren-ui/src/apollo/server/backgrounds/chart.ts:86-86
Timestamp: 2025-01-15T03:50:47.868Z
Learning: In the WrenAI codebase, ChartType is maintained as a string in the web database, while the ChartType enum is specifically used for AI adaptor input/output interfaces. Therefore, string case conversions like toUpperCase() are intentional for maintaining data consistency in the web DB.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
Learnt from: andreashimin
PR: Canner/WrenAI#1117
File: wren-ui/src/apollo/server/repositories/threadResponseRepository.ts:37-37
Timestamp: 2025-01-15T03:51:14.432Z
Learning: In the ThreadResponseRepository, the `chartType` property is intentionally typed as string in the database layer, while the ChartType enum is used specifically for AI adaptor input/output in the application layer.
wren-ui/src/pages/api/v1/ask.ts (2)
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
wren-ui/src/pages/api/v1/knowledge/instructions/index.ts (2)
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/services/instructionService.ts:107-146
Timestamp: 2025-03-17T09:16:53.878Z
Learning: In the WrenAI project, the instructionResolver merges the instruction ID (from args.where) and project ID (from the current project context) with instruction data before passing to the instructionService, allowing the GraphQL schema to have separate input types while the service layer works with complete objects.
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/adaptors/wrenAIAdaptor.ts:477-494
Timestamp: 2025-03-12T09:51:33.688Z
Learning: The instruction deletion API in the WrenAI service handles bulk deletion atomically - either all instructions are deleted or none are. There's no need for handling partial deletion scenarios.
wren-ui/src/pages/api/v1/async/generate_sql.ts (3)
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
wren-ui/src/pages/api/v1/async/ask.ts (2)
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
wren-ui/src/pages/api/v1/knowledge/instructions/[id].ts (3)
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/adaptors/wrenAIAdaptor.ts:477-494
Timestamp: 2025-03-12T09:51:33.688Z
Learning: The instruction deletion API in the WrenAI service handles bulk deletion atomically - either all instructions are deleted or none are. There's no need for handling partial deletion scenarios.
Learnt from: onlyjackfrost
PR: Canner/WrenAI#1392
File: wren-ui/src/apollo/server/services/instructionService.ts:107-146
Timestamp: 2025-03-17T09:16:53.878Z
Learning: In the WrenAI project, the instructionResolver merges the instruction ID (from args.where) and project ID (from the current project context) with instruction data before passing to the instructionService, allowing the GraphQL schema to have separate input types while the service layer works with complete objects.
Learnt from: paopa
PR: Canner/WrenAI#1376
File: wren-ai-service/src/pipelines/indexing/instructions.py:100-107
Timestamp: 2025-03-13T03:48:24.664Z
Learning: The `InstructionsCleaner.run()` method in the instructions indexing pipeline only accepts `instruction_ids` and `project_id` parameters, and doesn't support a `delete_all` parameter for unconditional deletion.
wren-ui/src/apollo/server/utils/sseTypes.ts (1)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/apollo/client/graphql/dashboard.generated.ts (1)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/apollo/client/graphql/__types__.ts (4)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1117
File: wren-ui/src/apollo/server/backgrounds/chart.ts:86-86
Timestamp: 2025-01-15T03:50:47.868Z
Learning: In the WrenAI codebase, ChartType is maintained as a string in the web database, while the ChartType enum is specifically used for AI adaptor input/output interfaces. Therefore, string case conversions like toUpperCase() are intentional for maintaining data consistency in the web DB.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
wren-ui/src/pages/api/v1/knowledge/sql_pairs/[id].ts (2)
Learnt from: cyyeh
PR: Canner/WrenAI#1112
File: wren-ai-service/src/web/v1/routers/sql_question.py:59-66
Timestamp: 2025-01-14T07:45:11.981Z
Learning: In the WrenAI codebase, UUID validation for query IDs in API endpoints is not required, as demonstrated in the SQL question router's GET endpoint.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
wren-ui/src/apollo/server/utils/apiUtils.ts (4)
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
🧬 Code Graph Analysis (6)
wren-ui/src/apollo/server/services/instructionService.ts (2)
wren-ui/src/apollo/client/graphql/__types__.ts (1)
  • Instruction (607-616)
wren-ui/src/apollo/server/repositories/instructionRepository.ts (1)
  • Instruction (11-19)
wren-ui/src/pages/api/v1/models.ts (1)
wren-ui/src/apollo/server/utils/apiUtils.ts (3)
  • ApiError (154-170)
  • respondWithSimple (220-253)
  • handleApiError (258-311)
wren-ui/src/pages/api/v1/async/ask.ts (4)
wren-ui/src/apollo/server/utils/sseTypes.ts (4)
  • ContentBlockStartEvent (64-70)
  • ContentBlockDeltaEvent (72-78)
  • ContentBlockStopEvent (80-82)
  • AsyncAskRequest (33-38)
wren-ui/src/apollo/server/utils/sseUtils.ts (6)
  • sendSSEEvent (16-19)
  • sendMessageStart (24-30)
  • sendStateUpdate (54-68)
  • getSqlGenerationState (94-115)
  • endStream (120-128)
  • sendError (73-89)
wren-ui/src/apollo/server/utils/apiUtils.ts (4)
  • ApiError (154-170)
  • MAX_WAIT_TIME (17-17)
  • isAskResultFinished (19-26)
  • validateSummaryResult (79-93)
wren-ui/src/apollo/server/models/adaptor.ts (3)
  • WrenAIError (11-14)
  • TextBasedAnswerInput (178-185)
  • TextBasedAnswerResult (193-197)
wren-ui/src/apollo/server/utils/sseUtils.ts (1)
wren-ui/src/apollo/server/utils/sseTypes.ts (5)
  • StreamEvent (92-99)
  • MessageStartEvent (44-46)
  • MessageStopEvent (48-54)
  • StateEvent (56-62)
  • ErrorEvent (84-90)
wren-ui/src/apollo/client/graphql/__types__.ts (1)
wren-ui/src/apollo/server/models/dashboard.ts (1)
  • DashboardSchedule (28-35)
wren-ui/src/apollo/server/utils/apiUtils.ts (3)
wren-ui/src/common.ts (1)
  • components (225-225)
wren-ui/src/apollo/server/models/adaptor.ts (2)
  • WrenAIError (11-14)
  • TextBasedAnswerResult (193-197)
wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts (1)
  • ApiHistory (30-42)
🪛 Biome (1.9.4)
wren-ui/src/pages/api/v1/generate_summary.ts

[error] 148-148: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

wren-ui/src/pages/api/v1/ask.ts

[error] 145-145: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 269-269: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

wren-ui/src/pages/api/v1/async/ask.ts

[error] 239-239: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 396-396: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (26)
wren-ui/src/apollo/server/utils/error.ts (1)

61-62: LGTM! Consistent error code implementation.

The new SQL_EXECUTION_ERROR enum value and corresponding error messages follow the established pattern in the codebase. All required message mappings are properly included.

Also applies to: 129-130, 163-163

wren-ui/src/pages/api-management/history.tsx (2)

73-78: Excellent improvement! Proper HTTP status code handling.

The change from checking only status === 200 to status >= 200 && status < 300 correctly treats all 2xx status codes as successful, which aligns with standard HTTP semantics.


106-108: Good defensive programming with optional chaining.

Adding optional chaining (payload?.question and payload?.sql) prevents potential runtime errors when the payload is null or undefined.

wren-ui/src/apollo/server/utils/index.ts (1)

9-10: LGTM! Proper module organization.

The addition of SSE-related exports follows the established pattern and centralizes access to SSE utilities.

wren-ui/src/apollo/server/resolvers/apiHistoryResolver.ts (1)

151-159: LGTM! Logical handling of different payload types.

The addition of array detection properly handles cases where response payloads are arrays, returning them directly while preserving the existing sanitization logic for object payloads. This approach accommodates the expanded API types introduced in this PR.

wren-ui/src/apollo/server/schema.ts (1)

11-23: LGTM! Schema additions align with API enhancements.

The new ApiType enum values properly support the expanded API functionality for summaries, instructions, SQL pairs, models, and asynchronous operations.

wren-ui/src/pages/api/v1/generate_sql.ts (1)

3-16: Good refactoring to centralize common utilities.

Moving the utility functions to a shared module promotes code reuse and maintainability across the API endpoints.

wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts (1)

15-27: LGTM! Enum values match schema definitions.

The new ApiType enum values properly align with the GraphQL schema additions.

wren-ui/src/apollo/client/graphql/dashboard.generated.ts (1)

59-59: LGTM! Generated type correctly reflects schema changes.

The generated type properly makes the schedule field and its nested properties optional and nullable, consistent with the GraphQL schema updates for flexible dashboard scheduling.

wren-ui/src/pages/api/v1/knowledge/sql_pairs/[id].ts (3)

29-40: LGTM! Robust ID validation with proper error handling.

The validation correctly handles edge cases and provides clear error messages for invalid inputs.


57-75: LGTM! Comprehensive input validation with appropriate constraints.

The validation logic correctly:

  • Handles partial updates with undefined checks
  • Enforces reasonable length limits (10KB for SQL, 1KB for questions)
  • Uses async SQL validation for syntax checking
  • Provides clear error messages

78-98: LGTM! Clean update logic with proper response handling.

The implementation correctly merges partial updates and returns the updated entity with appropriate API logging metadata.

wren-ui/src/pages/api/v1/async/ask.ts (2)

167-203: LGTM! Excellent polling implementation with comprehensive state tracking.

The polling logic properly:

  • Handles status changes with detailed SSE updates
  • Includes timeout protection with MAX_WAIT_TIME
  • Tracks poll count and provides rich debugging information
  • Uses appropriate delay between polls

254-259: LGTM! Proper client disconnect handling.

The implementation correctly handles client disconnection by destroying the stream and rejecting the promise, preventing resource leaks.

wren-ui/src/pages/api/v1/knowledge/instructions/[id].ts (3)

16-28: LGTM! Excellent documentation of instruction types and business rules.

The comments clearly explain the two types of instructions and their constraints, making the business logic easy to understand.


81-84: LGTM! Correct enforcement of business rules for global instructions.

The logic properly ensures that global instructions cannot have questions, maintaining data consistency.


96-99: LGTM! Proper type conversion handling.

The boolean conversion correctly handles the database field type while maintaining type safety for the API response.

wren-ui/src/apollo/client/graphql/__types__.ts (3)

79-96: LGTM! Comprehensive API type extensions support new functionality.

The added API types correctly cover all the new endpoints introduced in this PR:

  • Async operations (ASYNC_ASK, ASYNC_GENERATE_SQL)
  • Instruction management (CREATE/UPDATE/DELETE/GET_INSTRUCTIONS)
  • SQL pair management (CREATE/UPDATE/DELETE/GET_SQL_PAIRS)
  • Additional operations (GENERATE_SUMMARY, GET_MODELS, RUN_SQL)

310-312: LGTM! Dashboard schedule flexibility improvements.

Making the frequency, hour, and minute fields optional provides better flexibility for dashboard scheduling configurations, consistent with the generated GraphQL types.


400-400: LGTM! Consistent optional schedule field.

The optional schedule field in DetailedDashboard aligns with the dashboard schedule flexibility changes and generated types.

wren-ui/src/apollo/server/utils/sseTypes.ts (1)

40-99: Well-structured event interfaces with proper TypeScript patterns.

The event interfaces follow a consistent pattern by extending BaseEvent and using discriminated unions through the type property. The StreamEvent union type properly captures all possible events, enabling type-safe event handling in SSE consumers.

wren-ui/src/apollo/server/utils/apiUtils.ts (5)

17-17: Good practice using a named constant for timeout values.

The MAX_WAIT_TIME constant improves maintainability and consistency across API handlers that need timeout functionality.


19-26: Comprehensive terminal state checking.

The isAskResultFinished function correctly identifies all terminal states including the error condition, providing a reliable way to determine task completion.


34-72: Robust validation with detailed error context.

The validateAskResult function provides comprehensive validation with appropriate error codes and additional context (like invalidSql and explanationQueryId). The error handling covers different result types and failure scenarios effectively.

Note that both MISLEADING_QUERY and GENERAL result types throw the same NON_SQL_QUERY error code, which appears intentional for consistent client-side handling.


95-118: Well-designed history transformation with proper filtering.

The transformHistoryInput function correctly filters for relevant API types (including both sync and async variants) and ensures required fields are present before transformation. The function handles edge cases gracefully with the null check.


220-253: Appropriate design for CRUD operations.

The respondWithSimple function correctly omits the responseId from the response payload while still logging it to the API history, making it suitable for simple CRUD operations where clients don't need to track response IDs.

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

🧹 Nitpick comments (2)
wren-ui/src/pages/api/v1/stream/ask.ts (2)

237-243: Use optional chaining for cleaner code

The regex match extraction can be simplified using optional chaining.

-          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
-          if (match && match[1]) {
-            // Send incremental content updates
-            explanation += match[1];
-            sendContentBlockDelta(res, match[1]);
-          }
+          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
+          if (match?.[1]) {
+            // Send incremental content updates
+            explanation += match[1];
+            sendContentBlockDelta(res, match[1]);
+          }

393-401: Use optional chaining and consider extracting common streaming logic

Similar to the explanation streaming, this code can use optional chaining. Additionally, the streaming logic is duplicated between explanation and summary streaming.

-          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
-          if (match && match[1]) {
-            summary += match[1];
-            // Send incremental content updates
-            sendContentBlockDelta(res, match[1]);
-          }
+          const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
+          if (match?.[1]) {
+            summary += match[1];
+            // Send incremental content updates
+            sendContentBlockDelta(res, match[1]);
+          }

Consider extracting the common streaming logic into a reusable function to reduce code duplication:

async function streamContent(
  stream: any,
  req: NextApiRequest,
  res: NextApiResponse,
  onChunk: (content: string) => void,
): Promise<void> {
  return new Promise<void>((resolve, reject) => {
    stream.on('data', (chunk) => {
      const chunkString = chunk.toString('utf-8');
      const match = chunkString.match(/data: {"message":"([\s\S]*?)"}/);
      if (match?.[1]) {
        onChunk(match[1]);
        sendContentBlockDelta(res, match[1]);
      }
    });

    stream.on('end', () => resolve());
    stream.on('error', (error) => reject(error));
    
    req.on('close', () => {
      stream.destroy();
      reject(new Error('Client disconnected'));
    });
  });
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6d3cbf9 and c1d1dc0.

📒 Files selected for processing (6)
  • wren-ui/src/apollo/client/graphql/__types__.ts (4 hunks)
  • wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts (2 hunks)
  • wren-ui/src/apollo/server/schema.ts (1 hunks)
  • wren-ui/src/apollo/server/utils/apiUtils.ts (2 hunks)
  • wren-ui/src/pages/api/v1/stream/ask.ts (1 hunks)
  • wren-ui/src/pages/api/v1/stream/generate_sql.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • wren-ui/src/apollo/server/schema.ts
  • wren-ui/src/apollo/server/repositories/apiHistoryRepository.ts
  • wren-ui/src/apollo/server/utils/apiUtils.ts
  • wren-ui/src/apollo/client/graphql/types.ts
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:10.593Z
Learning: The typo in error code enum names ("IDENTIED_AS_GENERAL" and "IDENTIED_AS_MISLEADING_QUERY" instead of "IDENTIFIED_AS_GENERAL" and "IDENTIFIED_AS_MISLEADING_QUERY") in wren-ui/src/apollo/server/utils/error.ts was recognized but intentionally not fixed as it was considered out of scope for PR #1414.
Learnt from: cyyeh
PR: Canner/WrenAI#1112
File: wren-ai-service/src/pipelines/generation/sql_question.py:76-77
Timestamp: 2025-01-14T07:41:14.564Z
Learning: In the WrenAI codebase, extensive error handling for JSON parsing and array access operations in pipeline post-processing functions is not required, as indicated by the maintainer's preference to skip such suggestions.
Learnt from: andreashimin
PR: Canner/WrenAI#1414
File: wren-ui/src/apollo/server/utils/error.ts:0-0
Timestamp: 2025-03-18T10:28:26.608Z
Learning: The enum names `IDENTIED_AS_GENERAL` and `IDENTIED_AS_MISLEADING_QUERY` in GeneralErrorCodes enum in wren-ui/src/apollo/server/utils/error.ts have typos (missing "FI" in "IDENTIFIED") but fixing them is not in scope for the current PR.
wren-ui/src/pages/api/v1/stream/generate_sql.ts (2)
Learnt from: wwwy3y3
PR: Canner/WrenAI#1585
File: wren-ui/src/pages/api/v1/generate_sql.ts:98-106
Timestamp: 2025-04-24T16:10:43.308Z
Learning: In the generate_sql API, allow users to specify language codes not predefined in the WrenAILanguage enum, passing them through directly rather than strictly validating against the enum.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
wren-ui/src/pages/api/v1/stream/ask.ts (2)
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:599-604
Timestamp: 2025-03-17T19:26:10.835Z
Learning: Error handling for non-existent asking tasks in WrenAI is handled at the resolver level rather than in the service implementation.
Learnt from: wwwy3y3
PR: Canner/WrenAI#1388
File: wren-ui/src/apollo/server/services/askingService.ts:586-604
Timestamp: 2025-03-17T19:32:09.267Z
Learning: In WrenAI's askingService.ts, errors should be allowed to bubble up to the top-level error handler rather than being caught and logged at the method level.
🪛 Biome (1.9.4)
wren-ui/src/pages/api/v1/stream/ask.ts

[error] 239-239: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 396-396: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Analyze (go)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
wren-ui/src/pages/api/v1/stream/generate_sql.ts (1)

35-201: Clean implementation of streaming SQL generation

The handler follows a clear structure with proper validation, SSE setup, polling mechanism, and comprehensive error handling. The language configuration correctly allows flexibility per the retrieved learnings.

@wwwy3y3 wwwy3y3 merged commit 4212057 into main Jun 27, 2025
10 checks passed
@wwwy3y3 wwwy3y3 deleted the feature/api-enhancement branch June 27, 2025 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants