-
Notifications
You must be signed in to change notification settings - Fork 15
fix: add faq mapper for edge deploy #1083
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
+3,591
−219
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5cf4027
fix: add faq mapper for edge deploy
dipratap 4dc8f9f
fix: merge main
dipratap 4f36bfa
Merge branch 'main' of github.com:adobe/spacecat-shared into faq-edge
dipratap 9c2d6be
fix: faq new schema handling
dipratap e3b30b1
fix: faq new schema handling
dipratap 1d725ca
fix: faq new schema handling
dipratap d02417f
fix: refactoring
dipratap 86c63f6
fix: refactoring
dipratap 4fd713f
fix: faq oppty new schema
dipratap 816895a
fix: faq oppty new schema
dipratap 5fc1254
fix: keep suggestionsToPatches only
dipratap 442d396
fix: keep suggestionsToPatches only
dipratap d50c438
fix: heading-faq patch lastUpdated
dipratap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/spacecat-shared-tokowaka-client/src/mappers/faq-mapper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { toHast } from 'mdast-util-to-hast'; | ||
| import { fromMarkdown } from 'mdast-util-from-markdown'; | ||
| import { hasText } from '@adobe/spacecat-shared-utils'; | ||
| import { TARGET_USER_AGENTS_CATEGORIES } from '../constants.js'; | ||
| import BaseOpportunityMapper from './base-mapper.js'; | ||
|
|
||
| /** | ||
| * Mapper for FAQ opportunity | ||
| * Handles conversion of FAQ suggestions to Tokowaka patches | ||
| */ | ||
| export default class FaqMapper extends BaseOpportunityMapper { | ||
| constructor(log) { | ||
| super(log); | ||
| this.opportunityType = 'faq'; | ||
| this.prerenderRequired = true; | ||
| this.validActions = ['insertAfter', 'insertBefore', 'appendChild']; | ||
| } | ||
|
|
||
| getOpportunityType() { | ||
| return this.opportunityType; | ||
| } | ||
|
|
||
| requiresPrerender() { | ||
| return this.prerenderRequired; | ||
| } | ||
|
|
||
| /** | ||
| * Converts markdown text to HAST (Hypertext Abstract Syntax Tree) format | ||
| * @param {string} markdown - Markdown text | ||
| * @returns {Object} - HAST object | ||
| */ | ||
| // eslint-disable-next-line class-methods-use-this | ||
| markdownToHast(markdown) { | ||
| const mdast = fromMarkdown(markdown); | ||
| return toHast(mdast); | ||
| } | ||
|
|
||
| suggestionToPatch(suggestion, opportunityId) { | ||
| const eligibility = this.canDeploy(suggestion); | ||
| if (!eligibility.eligible) { | ||
| this.log.warn(`FAQ suggestion ${suggestion.getId()} cannot be deployed: ${eligibility.reason}`); | ||
| return null; | ||
| } | ||
|
|
||
| const data = suggestion.getData(); | ||
| const { text, transformRules } = data; | ||
|
|
||
| // Convert markdown to HAST | ||
| let hastValue; | ||
| try { | ||
| hastValue = this.markdownToHast(text); | ||
| } catch (error) { | ||
| this.log.error(`Failed to convert markdown to HAST for suggestion ${suggestion.getId()}: ${error.message}`); | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| ...this.createBasePatch(suggestion, opportunityId), | ||
| op: transformRules.action, | ||
| selector: transformRules.selector, | ||
| value: hastValue, | ||
| valueFormat: 'hast', | ||
| target: TARGET_USER_AGENTS_CATEGORIES.AI_BOTS, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a FAQ suggestion can be deployed | ||
| * @param {Object} suggestion - Suggestion object | ||
| * @returns {Object} { eligible: boolean, reason?: string } | ||
| */ | ||
| canDeploy(suggestion) { | ||
| const data = suggestion.getData(); | ||
|
|
||
| // Validate required fields | ||
| if (!data?.text) { | ||
| return { eligible: false, reason: 'text is required' }; | ||
| } | ||
|
|
||
| if (!data.transformRules) { | ||
| return { eligible: false, reason: 'transformRules is required' }; | ||
| } | ||
|
|
||
| if (!hasText(data.transformRules.selector)) { | ||
| return { eligible: false, reason: 'transformRules.selector is required' }; | ||
| } | ||
|
|
||
| if (!this.validActions.includes(data.transformRules.action)) { | ||
| return { eligible: false, reason: 'transformRules.action must be insertAfter, insertBefore, or appendChild' }; | ||
| } | ||
|
|
||
| return { eligible: true }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer markdowntohast as a util method instead of part of this interface. Its an internal inpl detail of suggestion to patch conversion that shouldnt be exposed in the mapper interface.