-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hayde has AI now! it supports openAI and ollama for now
- Loading branch information
Showing
24 changed files
with
1,183 additions
and
254 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["general", "AI"] | ||
} |
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,17 @@ | ||
{ | ||
"plugins": [ | ||
{ | ||
"name": "general", | ||
"options": { | ||
"srcFolderLocation": "./src/components" | ||
} | ||
}, | ||
{ | ||
"name": "AI", | ||
"options": { | ||
"aiTool": "ollama", | ||
"modelName": "llama2" | ||
} | ||
} | ||
] | ||
} |
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,17 @@ | ||
{ | ||
"plugins": [ | ||
{ | ||
"name": "general", | ||
"options": { | ||
"srcFolderLocation": "./src/components" | ||
} | ||
}, | ||
{ | ||
"name": "AI", | ||
"options": { | ||
"aiTool": "openAI", | ||
"modelName": "gpt-4" | ||
} | ||
} | ||
] | ||
} |
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
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,44 @@ | ||
import { PromptTemplate } from "langchain/prompts"; | ||
|
||
export function stripMarkdown(text: string) { | ||
const markdownRegex = /(```[a-z]*\n[\S\s]*?\n```)/g; | ||
return text.replaceAll(markdownRegex, (match, p1: string): string => | ||
p1.replaceAll(/```[a-z]*\n|```/g, "").trim() | ||
); | ||
} | ||
|
||
export function extractCodeBlock(response: string) { | ||
const codeBlockRegex = | ||
/```(?:typescript|javascript|react)?\n([\S\s]*?)\n```/g; | ||
const codeBlocks = []; | ||
let match; | ||
|
||
while ((match = codeBlockRegex.exec(response)) !== null) { | ||
codeBlocks.push(match[1].trim()); // Trim each code block | ||
} | ||
|
||
return codeBlocks.length > 0 ? codeBlocks.join("\n\n") : response; | ||
} | ||
|
||
export function createQuestionPrompt() { | ||
const questionTemplate = ` | ||
Create functional react component with these information on {tsOrJs}. | ||
Just return {tsOrJs} code to me, don't return any additional information with code. I will handle it. I also don't need any information about how to import the component. | ||
- component description: '{componentDescription}' | ||
- component name: '{componentName}' | ||
- it's using '{styleLibrary}' library for creating component. | ||
If the library is none, dont use any library. | ||
Please include error handling, loading states, and comments within the code for clarity. Prefer functional components with hooks for state management over class components. Ensure the code is modular and easily testable. | ||
If it's typescript, Include type definitions for all props and state objects, and use async/await for asynchronous operations. If it's javascript, use PropTypes for all props and state objects, and use promises for asynchronous operations. For state management, use React Context if needed. | ||
Before sending response, please verify the code that you created. If it's not correct, please send the correct code. | ||
Component code: | ||
`; | ||
return PromptTemplate.fromTemplate(questionTemplate); | ||
} |
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,73 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
PluginInitParams, | ||
PluginInitReturn, | ||
PluginRunParams, | ||
PluginRunReturn, | ||
} from "../../creatorSettings/creatorSettings.type"; | ||
import { AITools, IPluginOptions, ISettings, OutAnswers } from "./interfaces"; | ||
import inquirer from "inquirer"; | ||
import { questions } from "./questions"; | ||
import { logger, publicLog } from "@/helper"; | ||
import { createFile } from "@/internalFeatures/fsLibrary"; | ||
import ora from "ora"; | ||
import { callAgent as openAICallAgent } from "./models/openAI/openai"; | ||
import { callAgent as ollamaCallAgent } from "./models/ollama/ollama"; | ||
|
||
export { questions } from "./questions"; | ||
export { defaultSettings } from "./interfaces"; | ||
|
||
export const pluginName = "AI"; | ||
const log = logger(pluginName); | ||
|
||
export async function initPlugin({ | ||
options, | ||
}: PluginInitParams<IPluginOptions, OutAnswers>): Promise< | ||
PluginInitReturn<IPluginOptions> | ||
> { | ||
publicLog("Before starting, please make sure that you added required ENV variables to your .env file."); | ||
const answers = (await inquirer.prompt( | ||
questions, | ||
options | ||
)) as Required<ISettings>; | ||
|
||
return { | ||
answers, | ||
}; | ||
} | ||
|
||
export async function runPlugin({ | ||
allAnswers, | ||
}: PluginRunParams<ISettings, OutAnswers>): Promise<PluginRunReturn> { | ||
const aiAnswers = allAnswers.AI?.answers as Required<ISettings>; | ||
const generalAnswers = allAnswers.general?.answers; | ||
|
||
log("Answers:", aiAnswers); | ||
|
||
console.log("\n"); | ||
const spinner = ora({ | ||
text: "Waiting for AI answer", | ||
spinner: "point", | ||
}).start(); | ||
|
||
const reqData = { | ||
componentName: generalAnswers.componentName!, | ||
componentDescription: aiAnswers.compDescription, | ||
styleLibrary: aiAnswers.styleLibrary, | ||
modelName: aiAnswers.modelName, | ||
isTypescript: aiAnswers.isTS, | ||
}; | ||
const aiTool = aiAnswers.aiTool; | ||
let response; | ||
|
||
if (aiTool === AITools.openAI) { | ||
response = await openAICallAgent(reqData); | ||
} else if (aiTool === AITools.ollama) { | ||
response = await ollamaCallAgent(reqData); | ||
} | ||
|
||
log("AI response:", response); | ||
|
||
spinner.succeed("AI answer received"); | ||
createFile(allAnswers, aiAnswers.isTS ? ".tsx" : ".jsx", response as string); | ||
} |
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 { PluginInitReturn } from "@/creatorSettings/creatorSettings.type"; | ||
import { StyleLibrary } from "@/features/reactJS/interfaces"; | ||
import { IPluginOptions as GeneralOptions } from "@/features/general/interfaces"; | ||
|
||
export interface IPluginOptions { | ||
modelName?: string; | ||
compDescription?: string; | ||
styleLibrary?: StyleLibrary; | ||
isTS?: boolean; | ||
aiTool?: AITools; | ||
} | ||
|
||
export interface ISettings extends IPluginOptions { | ||
templateName: string; | ||
templateFolder: string; | ||
} | ||
|
||
export type OutAnswers = { | ||
AI: PluginInitReturn<ISettings>; | ||
general: PluginInitReturn<GeneralOptions>; | ||
}; | ||
|
||
export const defaultSettings: ISettings = { | ||
templateName: "main", | ||
templateFolder: "AI", | ||
}; | ||
|
||
export enum AITools { | ||
openAI = "openAI", | ||
ollama = "ollama", | ||
} | ||
|
||
export interface IModelReturn { | ||
callAgent: callAgentFn; | ||
} | ||
|
||
export type callAgentFn = (options: callAgentFnOptions) => Promise<string>; | ||
|
||
export type callAgentFnOptions = { | ||
componentName: string; | ||
componentDescription: string; | ||
styleLibrary: string; | ||
modelName: string; | ||
isTypescript: boolean; | ||
}; |
Oops, something went wrong.