-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extract page content and send to AI assistant (#126)
* Feature: Extract page content and send to AI assistant * Update UI * fix: Optimize prompt words * fix: update iframe src in SidePanelChat component * feat: Implement standardized AI integration for remaining three panels
- Loading branch information
1 parent
660ba3a
commit 7d1181c
Showing
5 changed files
with
184 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,45 @@ | ||
import { WidgetType, EditorView, Decoration } from '@codemirror/view'; | ||
import { RangeSetBuilder } from '@codemirror/state'; | ||
|
||
type OpenDrawerCallback = (message: string) => void; | ||
|
||
class ButtonWidget extends WidgetType { | ||
openDrawerCallback: OpenDrawerCallback; | ||
extractContentCallback: (boxType: string) => string; | ||
boxType: string; | ||
|
||
constructor(openDrawerCallback: OpenDrawerCallback, extractContentCallback: (boxType: string) => string, boxType: string) { | ||
super(); | ||
this.openDrawerCallback = openDrawerCallback; | ||
this.extractContentCallback = extractContentCallback; | ||
this.boxType = boxType; | ||
} | ||
|
||
toDOM() { | ||
const button = document.createElement('button'); | ||
button.className = `flex items-center rounded text-[#453d7d] px-1 border border-[#453d7d] | ||
bg-[#efefef] hover:bg-[#453d7d] hover:text-white transition-colors duration-500 | ||
font-medium whitespace-nowrap overflow-hidden`; | ||
button.innerHTML = '<img src="/openai.svg" alt="" class="w-4 h-4 mr-1" /> Ask AI'; | ||
button.style.position = 'absolute'; | ||
button.style.right = '1px'; | ||
button.style.top = '1px'; | ||
|
||
button.addEventListener('click', () => { | ||
if (this.openDrawerCallback) { | ||
const extractedContent = this.extractContentCallback(this.boxType); | ||
this.openDrawerCallback(extractedContent); | ||
} | ||
}); | ||
|
||
return button; | ||
} | ||
} | ||
|
||
export function buttonPlugin(openDrawerCallback: OpenDrawerCallback, extractContentCallback: (boxType: string) => string, boxType: string) { | ||
return EditorView.decorations.compute([], (state) => { | ||
const builder = new RangeSetBuilder<Decoration>(); | ||
builder.add(0, 0, Decoration.widget({ widget: new ButtonWidget(openDrawerCallback, extractContentCallback, boxType) })); | ||
return builder.finish(); | ||
}); | ||
} |
This file contains 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
This file contains 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,56 @@ | ||
const cleanContent = (content: string) => { | ||
return content.replace(/^\d+\s+/gm, '').trim(); | ||
}; | ||
|
||
export const extractPageContent = (boxType: string) => { | ||
const mainContent = document.querySelector('main')?.innerText || 'No main content found'; | ||
|
||
const customConfigMatch = mainContent.match(/Custom config\s+([\s\S]*?)\s+Model/); | ||
const modelMatch = mainContent.match(/Model\s+([\s\S]*?)\s+Policy/); | ||
const policyMatch = mainContent.match(/Policy\s+([\s\S]*?)\s+Request/); | ||
const requestMatch = mainContent.match(/Request\s+([\s\S]*?)\s+Enforcement Result/); | ||
const enforcementResultMatch = mainContent.match(/Enforcement Result\s+([\s\S]*?)\s+SYNTAX VALIDATE/); | ||
|
||
const customConfig = customConfigMatch ? cleanContent(customConfigMatch[1]) : 'No custom config found'; | ||
const model = modelMatch ? cleanContent(modelMatch[1].replace(/Select your model[\s\S]*?RESET/, '')) : 'No model found'; | ||
const policy = policyMatch ? cleanContent(policyMatch[1].replace(/Node-Casbin v[\d.]+/, '')) : 'No policy found'; | ||
const request = requestMatch ? cleanContent(requestMatch[1]) : 'No request found'; | ||
const enforcementResult = enforcementResultMatch | ||
? cleanContent(enforcementResultMatch[1].replace(/Why this result\?[\s\S]*?AI Assistant/, '')) | ||
: 'No enforcement result found'; | ||
|
||
const extractedContent = ` | ||
Custom Config: ${customConfig} | ||
Model: ${model} | ||
Policy: ${policy} | ||
Request: ${request} | ||
Enforcement Result: ${enforcementResult} | ||
`; | ||
|
||
let message = ''; | ||
switch (boxType) { | ||
case 'model': | ||
message = `Briefly explain the Model content. | ||
no need to repeat the content of the question.\n${extractedContent}`; | ||
break; | ||
case 'policy': | ||
message = `Briefly explain the Policy content. | ||
no need to repeat the content of the question.\n${extractedContent}`; | ||
break; | ||
case 'request': | ||
message = `Briefly explain the Request content. | ||
no need to repeat the content of the question.\n${extractedContent}`; | ||
break; | ||
case 'enforcementResult': | ||
message = `Why this result? please provide a brief summary. | ||
no need to repeat the content of the question.\n${extractedContent}`; | ||
break; | ||
default: | ||
message = extractedContent; | ||
} | ||
|
||
return { | ||
extractedContent, | ||
message, | ||
}; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.