add ai response to current file

This commit is contained in:
Dian 2025-01-31 23:03:54 +08:00
parent 92c4195a25
commit 82900fac51
3 changed files with 97 additions and 34 deletions

View file

@ -10,6 +10,8 @@ export const VIEW_TYPE_EXAMPLE = "example-view";
export class ExampleView extends ItemView {
root: Root | null = null;
content: string = '';
contents: string[] = [];
constructor(leaf: WorkspaceLeaf) {
super(leaf);
@ -30,11 +32,27 @@ export class ExampleView extends ItemView {
this.root = createRoot(this.containerEl.children[1]);
// this.root.render(<div>asdf</div>);
// this.root.render(<ExampleReactView />);
this.root.render(React.createElement(ExampleReactView));
}
this.root.render(
React.createElement(ExampleReactView, {
contents: this.contents
})
); }
async onClose() {
// Nothing to clean up.
this.root?.unmount();
}
updateContent(content: string) {
this.content = content;
this.contents = this.content.split("<section-done>")
// console.log(this.content)
// console.log(this.contents)
if (!this.root) return null;
this.root.render(
React.createElement(ExampleReactView, {
contents: this.contents
})
);
}
}

View file

@ -1,9 +1,18 @@
import * as React from "react";
export const ExampleReactView = () => {
type ExampleReactViewProps = {
contents: string[]
}
export const ExampleReactView = ({ contents }: ExampleReactViewProps) => {
return (
<div>
<h1>Hello Nemesis</h1>
<p>This is a React view</p>
<h1>Hello My Nemesis</h1>
{contents.map((content, index) => {
return (<><div key={index}>{content}</div><br /></>)
})}
</div>
);
)
};

92
main.ts
View file

@ -29,34 +29,72 @@ const DEFAULT_SETTINGS: MyPluginSettings = {
export default class NemesisPlugin extends Plugin {
settings: MyPluginSettings;
settings: MyPluginSettings;
async testFunction() {
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.settings.openAiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Hello!" }]
})
});
async testFunction() {
const activeFile = this.app.workspace.getActiveFile();
console.log('activeFile')
console.log(activeFile)
if (!activeFile) return null;
const data = await response.json();
if (!response.ok) {
throw new Error(data.error?.message || 'API request failed');
}
const fileContent = await this.app.vault.read(activeFile);
console.log(fileContent)
const nemesisPrompt = `You are a friendly but challenging intellectual nemesis.
Review the following content and respond with:
1. Point out potential logical flaws or assumptions
2. Suggest alternative perspectives
3. Ask thought-provoking questions that challenge the main ideas
4. Provide constructive criticism while maintaining a supportive tone
new Notice("OpenAI Response: " + data.choices[0].message.content);
} catch (error) {
new Notice("OpenAI Error: " + error.message);
console.error("OpenAI Error:", error);
}
}
End each section with a <section-done>.
Here's the content to analyze:
${fileContent}`;
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.settings.openAiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: nemesisPrompt }]
})
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error?.message || 'API request failed');
}
const aiResponse = data.choices[0].message.content
// const aiResponse = `1. The statement "Billionaires could've stopped sooner, but they didn't, because there's nothing else they wish to do" assumes that billionaires continue to build their companies solely out of interest and not for other reasons such as maintaining power or increasing wealth. It could be argued that there are various motivations for billionaires to continue building their projects beyond just genuine interest.
// <section-done>
// 2. An alternative perspective could be that while YC does emphasize making something people want and understanding user needs, the process of becoming a billionaire involves more factors than just user satisfaction. Factors like market demand, competition, innovation, timing, and luck also play a significant role in achieving billionaire status.
// <section-done>
// 3. How do we define what users want? Is it always a straightforward process to understand and meet user needs, especially in a fast-changing market? Are there instances where companies have succeeded without prioritizing user desires above all else?
// <section-done>
// 4. The article discusses the importance of understanding user needs and building products based on that understanding. It would be beneficial to also explore the potential drawbacks of solely focusing on user feedback and the risks of ignoring broader market trends or innovations.
// <section-done>
// `
// update the leaf with new response
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE);
// if the view exists, update the content
if (leaves.length > 0) {
const view = leaves[0].view as ExampleView;
view.updateContent(aiResponse);
}
} catch (error) {
new Notice("OpenAI Error: " + error.message);
console.error("OpenAI Error:", error);
}
}
async activateView() {
const { workspace } = this.app;
@ -103,8 +141,6 @@ export default class NemesisPlugin extends Plugin {
// console.log(`My openai key: ${this.settings.openAiKey}`);
}
);
// todo create and open new view to display the open ai nemesis result
// it currently does not pop open the view
this.registerView(VIEW_TYPE_EXAMPLE, (leaf) => new ExampleView(leaf));
// use this class to perform additional things with the ribbon
@ -168,7 +204,7 @@ export default class NemesisPlugin extends Plugin {
);
}
onunload() {}
onunload() { }
async loadSettings() {
this.settings = Object.assign(