Skip to content

Conversation

@haoAddsearch
Copy link
Collaborator

@haoAddsearch haoAddsearch commented Oct 20, 2025

…t it.

Summary by CodeRabbit

  • New Features

    • Added AI-answers filtering capability supporting complex nested and/or/not logic.
    • New client method to apply an AI-answers filter object at runtime; AI Answers payload now respects the filter.
  • Documentation

    • README guide with examples for constructing and applying AI-answers filters (category, custom fields, language, document date).
  • Chores

    • Package version bumped to 1.1.4 and TypeScript lib configuration updated.

@coderabbitai
Copy link

coderabbitai bot commented Oct 20, 2025

Walkthrough

Adds support for an AI-answers filtering object: new optional settings field and setter, a public client setter, README documentation for the filter object, and inclusion of the filter in the ai-answers POST payload; also minor type, helper, and config/version updates.

Changes

Cohort / File(s) Summary
Documentation
README.md
Adds "Set AI-answers filtering object" section describing nested and/or/not filtering with properties (e.g., category, custom_fields.*, language, doc_date) and a JavaScript example using client.setAiAnswersFilterObject(...).
Settings & Public API
src/settings.ts, src/index.ts
Adds optional aiAnswersFilterObject?: object to Settings; adds setAiAnswersFilterObject(filter: object) on SettingsManager; exposes AddSearchClient.setAiAnswersFilterObject(filter: object): void.
API request logic & types
src/apifetch.ts
Changes SourceDocuments.hits to SearchResponseDocument[]; adds shared helper settingToQueryParam; normalizes keyword handling with replaceAll; adjusts fuzzy retry logic; uses Number.parseInt for prices; iterates sort entries with for...of; includes settings?.aiAnswersFilterObject in ai-answers POST payload.
Project config & metadata
package.json, tsconfig.json
Bumps version 1.1.31.1.4; adds TS compilerOptions.lib entries ["ES6","ES2021.String","DOM"].

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client as AddSearchClient
  participant Settings as SettingsManager
  participant API as ApiFetch
  participant Server as AI-Answers Endpoint

  Client->>Settings: setAiAnswersFilterObject(filter)
  Settings-->>Client: (aiAnswersFilterObject stored)

  Client->>API: requestAiAnswers(question, ...)
  API->>Settings: read aiAnswersFilterObject
  Note right of API #D3E4CD: Build POST payload\ninclude question + aiAnswersFilterObject
  API->>Server: POST /ai-answers { question, aiAnswersFilterObject, ... }
  Server-->>API: 200 OK / response
  API-->>Client: response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • italo-addsearch
  • kanarupan-addsearch

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "[sc-12720] add 'aiAnswersFilterObject' to settings and a method to se…" directly corresponds to the main changes in the changeset. The PR adds a new optional aiAnswersFilterObject field to the Settings type, introduces a setter method setAiAnswersFilterObject() in both SettingsManager and AddSearchClient, updates the API payload to include the filter, and adds supporting documentation. The title is concise and specific, clearly indicating the primary purpose without vague language, though it appears truncated in display with "se…" at the end.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sc-12720/support-filter-parameter-in-ai-answers-endpoint

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@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)
src/apifetch.ts (1)

344-345: Consider conditionally including the filter field for consistency.

The current implementation always includes the filter field in the POST payload, even when aiAnswersFilterObject is undefined. This differs from the search path (lines 312-326), which conditionally includes the filter only when defined.

Apply this diff to match the pattern used in the search implementation:

-    apiInstance
-      .post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, {
-        question: settings?.keyword,
-        filter: settings?.aiAnswersFilterObject
-      })
+    const payload: any = {
+      question: settings?.keyword
+    };
+    
+    if (settings?.aiAnswersFilterObject) {
+      payload.filter = settings.aiAnswersFilterObject;
+    }
+    
+    apiInstance
+      .post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, payload)
README.md (1)

535-551: Consider adding a complete usage example.

The documentation clearly explains the filter structure, but it would be helpful to show the complete flow of setting the filter and then calling aiAnswers().

Consider adding a complete example like this:

// Set the filter before making the AI answers request
var aiAnswersFilter = {
  and: [
    { 'custom_fields.region': 'en-us' },
    { not: { 'custom_fields.color': 'white' } }
  ]
};

client.setAiAnswersFilterObject(aiAnswersFilter);

// Then fetch AI answers with the filter applied
client.aiAnswers('What products are available?', function(response) {
  console.log(response);
});
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5f04df and 264036a.

📒 Files selected for processing (4)
  • README.md (1 hunks)
  • src/apifetch.ts (1 hunks)
  • src/index.ts (1 hunks)
  • src/settings.ts (2 hunks)
🔇 Additional comments (3)
src/index.ts (1)

387-389: LGTM! Clean delegation to settings manager.

The implementation correctly follows the established pattern used by setFilterObject and other similar methods.

src/settings.ts (2)

57-57: LGTM! Appropriate type definition for the optional filter.

The optional aiAnswersFilterObject property correctly mirrors the existing filterObject pattern and allows the feature to be used only when needed.


193-195: LGTM! Setter implementation is consistent.

The setter follows the same straightforward pattern as setFilterObject, maintaining consistency across the codebase.

Copy link

@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: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/apifetch.ts (1)

342-369: Add empty object check for AI answers filter consistency.

The search filter (line 325) uses isEmptyObject() to exclude empty filter objects from the payload, but the AI answers filter (line 346) does not. Since both endpoints target the same API and the utility already exists, apply the same pattern to AI answers for consistency:

     .post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, {
       question: settings?.keyword,
-      filter: settings?.aiAnswersFilterObject
+      filter: isEmptyObject(settings?.aiAnswersFilterObject) ? undefined : settings?.aiAnswersFilterObject
     })
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 264036a and 98daffc.

📒 Files selected for processing (3)
  • package.json (1 hunks)
  • src/apifetch.ts (6 hunks)
  • tsconfig.json (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • tsconfig.json
  • package.json
🔇 Additional comments (2)
src/apifetch.ts (2)

154-157: Good improvement for explicit boolean handling.

The explicit === true comparison makes the boolean coercion clear and defensive, ensuring fuzzy is only set to true when fuzzyRetry is strictly true.


53-57: No action needed—the type change is internal only.

The SourceDocuments and ConversationsApiResponse interfaces are not exported from the module (both lack the export keyword), meaning they are internal implementation details. The type change from Document[] to SearchResponseDocument[] does not introduce breaking changes for external consumers since only SearchResponseDocument is exported and exposed to the public API. This internal refactoring aligns the type with the actual API response structure without affecting consumers.

Likely an incorrect or invalid review comment.

Copy link
Contributor

@kanarupan-addsearch kanarupan-addsearch left a comment

Choose a reason for hiding this comment

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

comments pending.
also it's better to test a couple of hierarchical complex query samples in staging?

Copy link

@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: 1

🧹 Nitpick comments (1)
src/apifetch.ts (1)

345-346: Consider filtering empty objects for consistency with other filter implementations.

The search endpoint (lines 320-326) and customField filter (line 242) both use isEmptyObject() to avoid sending empty filter objects. For consistency across the codebase, apply the same pattern to aiAnswersFilterObject:

       .post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, {
         question: settings?.keyword,
-        filter: settings?.aiAnswersFilterObject
+        filter: isEmptyObject(settings?.aiAnswersFilterObject) ? undefined : settings?.aiAnswersFilterObject
       })

The isEmptyObject utility is already imported and used consistently elsewhere. However, if the AI Answers API explicitly handles empty objects or requires them for backward compatibility, this change may not be necessary.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98daffc and 1ac09e8.

📒 Files selected for processing (1)
  • src/apifetch.ts (6 hunks)
🔇 Additional comments (3)
src/apifetch.ts (3)

156-156: LGTM - Simplified fuzzy retry logic.

The simplified boolean expression correctly evaluates to true on retry (second call) and false on the initial call, which aligns with the fuzzy retry mechanism.


190-195: Good practice using Number.parseInt with explicit radix.

Using Number.parseInt with an explicit radix of 10 is more explicit and prevents potential octal interpretation issues.


222-224: LGTM - Single sort criterion handled correctly.

The else branch correctly handles the case where sortBy is not an array or contains only one element.

Copy link
Contributor

@kanarupan-addsearch kanarupan-addsearch left a comment

Choose a reason for hiding this comment

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

LGTM

@haoAddsearch haoAddsearch merged commit be0768d into master Oct 22, 2025
@haoAddsearch haoAddsearch deleted the sc-12720/support-filter-parameter-in-ai-answers-endpoint branch October 22, 2025 10:01
@sonarqubecloud
Copy link

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.

2 participants