buildit.prg
#include "FiveWin.ch"
REQUEST DbfCdx
// REQUEST HB_CODEPAGE_ESWIN
// REQUEST HB_LANG_ESWIN
// get your OpenAI key from https://platform.openai.com/api-keys
static cKey := "sk-proj-..." 聽
static oChatgpt
static oTree
static cCategory := " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽"
static oWebView
static cContext := " 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 "
static oExplBar, oExplPanel
//----------------------------------------------------------------------------//
function Main()
聽 聽local oWnd, oAppPanel
聽 聽oChatgpt = TChatgpt():New( cKey )
聽 聽DEFINE WINDOW oWnd TITLE "Build it"
聽 聽oExplBar = TExplorerBar():New( 0, 82, 480, 100, oWnd:oLeft, CLR_WHITE, RGB( 31, 31, 31 ) )
聽 聽oWnd:oLeft = oExplBar
聽 聽oExplPanel = oExplBar:AddPanel( "AI assistant" )
聽 聽oExplPanel:AddLink( "what is this app about ?",;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽{ || If( MsgGet( "App category", "Please specify it", @cCategory ), AddOptions( cCategory, oExplBar ),) } )
聽 聽oExplPanel:AddLink( "Add options to selected item", { || AddSubOptions() } )
聽 聽oExplPanel:AddLink( "Generate HTML for selected item", { || MsgRun( "Building the GUI", "Please wait", { || GenerateHTML() } ) } )
聽 聽oExplPanel:AddLink( "Modify HTML for selected item", { || ModifyHTML() } )
聽 聽oExplPanel:AddLink( "Overall context", { || Context() } )
聽 聽oExplPanel:AddLink( "Save App", { || SaveApp() } )
聽 聽oExplPanel:AddLink( "Load App", { || LoadApp() } )
聽 聽oWnd:oClient = TPanel():New( 0, 0, 100, 100, oWnd )
聽 聽oWebView = TWebView2():New( oWnd:oClient )
聽 聽oWebView:Navigate( "https://www.google.com" )
聽 聽ACTIVATE WINDOW oWnd MAXIMIZED ;
聽 聽 聽 ON RESIZE ( oWebView:SetSize( nWidth - oExplBar:nWidth, nHeight ), oExplBar:SetSize( 480, nHeight ) )
return nil 聽 聽
//----------------------------------------------------------------------------//
function AddOptions( cCategory, oExplBar )
聽 聽local cOptions, aOptions, cOption, oAppPanel, oItem
聽 聽oChatgpt:cPrompt := "genera una lista separada por comas con las opciones de una aplicaci贸n de " + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽hb_StrToUtf8( AllTrim( cCategory ) ) + ". No des ninguna explicaci贸n"
聽 聽MsgRun( "Asking ChatGPT", "Please wait", { || oChatgpt:Send() } )
聽 聽cOptions = oChatgpt:GetValue( "choices", "message", "content" )
聽 聽aOptions = hb_ATokens( cOptions, "," )
聽 聽oAppPanel = oExplBar:AddPanel( cCategory,, 700 )
聽 聽cCategory = hb_Utf8ToStr( AllTrim( cCategory ) )
聽 聽oTree = TTreeView():New( 2.5, 2, oAppPanel,,,,, 423, 675 )
聽 聽oTree:bChanged = { || oItem := oTree:GetSelected(), oWebView:SetHtml( If( ! Empty( oItem:Cargo ), oItem:Cargo, "" ) ) }
聽 聽for each cOption in aOptions
聽 聽 聽 oItem = oTree:Add( cOption )
聽 聽 聽 oItem:Cargo = HtmlDefault()
聽 聽next 聽
聽 聽
return nil 聽 聽
//----------------------------------------------------------------------------//
function AddSubOptions()
聽 聽local oItem
聽 聽local cOption := If( ( oItem := oTree:GetSelected() ) != nil, oItem:cPrompt, "" )
聽 聽local cOptions, cSubOption, aOptions, oSubItem
聽 聽if ! Empty( cOption )
聽 聽 聽 oChatgpt:cPrompt := "genera una lista separada por comas con las opciones de " + hb_StrToUtf8( cOption ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 " para una aplicaci贸n de " + hb_StrToUtf8( cCategory ) + ". No des ninguna explicaci贸n"
聽 聽 聽 MsgRun( "Asking ChatGPT", "Please wait", { || oChatgpt:Send() } )
聽 聽 聽 cOptions = oChatgpt:GetValue( "choices", "message", "content" )
聽 聽 聽 aOptions = hb_ATokens( cOptions, "," )
聽 聽 聽 for each cSubOption in aOptions
聽 聽 聽 聽 聽oSubItem = oItem:Add( cSubOption )
聽 聽 聽 聽 聽oSubItem:Cargo = HtmlDefault()
聽 聽 聽 next 聽
聽 聽 聽 oItem:Expand()
聽 聽endif 聽
return nil 聽 聽
//----------------------------------------------------------------------------//
function GenerateHTML()
聽 聽local oItem, cHTML
聽 聽local cOption := If( ( oItem := oTree:GetSelected() ) != nil, oItem:cPrompt, "" )
聽 聽if ! Empty( cOption )
聽 聽 聽 oChatgpt:cPrompt := "genera el c贸digo HTML usando bootstrap para la opci贸n " + hb_StrToUtf8( cOption ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 " para una aplicaci贸n de " + hb_StrToUtf8( AllTrim( cCategory ) ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ". Que se vea muy profesional y elegante. No des ninguna explicaci贸n. " + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 cContext
聽 聽 聽 oChatgpt:Send()
聽 聽 聽 cHTML = oChatgpt:GetValue( "choices", "message", "content" )
聽 聽 聽 oItem:Cargo = SubStr( cHTML, 8, Len( cHTML ) - 10 )
聽 聽 聽 oWebView:SetHTML( oItem:Cargo )
聽 聽endif 聽
return nil 聽
//----------------------------------------------------------------------------//
function ModifyHTML()
聽 聽local oItem, cHTML
聽 聽local cOption := If( ( oItem := oTree:GetSelected() ) != nil, oItem:cPrompt, "" )
聽 聽local cChange := Space( 50 )
聽 聽if fw_memoEdit( @cChange, "What do you want to change ?" )
聽 聽 聽 cChange = AllTrim( cChange )
聽 聽endif 聽 聽 聽
聽 聽if ! Empty( cOption )
聽 聽 聽 oChatgpt:cPrompt := "modifica este c贸digo HTML usando bootstrap para la opci贸n " + hb_StrToUtf8( cOption ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 " para una aplicaci贸n de " + hb_StrToUtf8( AllTrim( cCategory ) ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ". Que se vea muy profesional y elegante. No des ninguna explicaci贸n. " + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 cContext + ". Y aplica estos cambios exactamente: " + hb_StrToUtf8( cChange ) + ;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 oItem:Cargo
聽 聽 聽 MsgRun( "Asking ChatGPT", "Please wait", { || oChatgpt:Send() } )
聽 聽 聽 cHTML = oChatgpt:GetValue( "choices", "message", "content" )
聽 聽 聽 oItem:Cargo = SubStr( cHTML, 8, Len( cHTML ) - 10 )
聽 聽 聽 oWebView:SetHTML( oItem:Cargo )
聽 聽endif 聽
return nil 聽
//----------------------------------------------------------------------------//
function Context()
聽 聽if fw_memoEdit( @cContext, "Overall context for HTML generation" )
聽 聽 聽 cContext = AllTrim( cContext )
聽 聽endif 聽 聽 聽
return nil 聽
//----------------------------------------------------------------------------//
function CreateDBF( cFileName )
聽 聽DbCreate( cFileName + ".dbf", { { "prompt", "C", 80, 0 },;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽{ "level", 聽"N", 聽3, 0 },;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽{ "html", 聽 "M", 10, 0 } }, "DBFCDX" )
聽 聽USE ( cFileName )
return nil
//----------------------------------------------------------------------------//
function SaveApp()
聽 聽if ! File( cCategory + ".dbf" )
聽 聽 聽 CreateDBF( StrTran( AllTrim( cCategory ), " ", "_" ) )
聽 聽else
聽 聽 聽 if MsgYesNo ("App already exists", "Do you want to overwrite it?" )
聽 聽 聽 聽 聽CreateDBF( StrTran( AllTrim( cCategory ), " ", "_" ) )
聽 聽 聽 else
聽 聽 聽 聽 聽return nil
聽 聽 聽 endif
聽 聽endif
聽 聽oTree:Scan( { | oItem, nPos, nLevel | DbAppend(), field->prompt := oItem:cPrompt,;
聽 聽 聽 聽 聽 聽 聽 聽field->level := nLevel, field->html := oItem:Cargo, .F. } )
聽 聽GO TOP
聽 聽XBrowse()
聽 聽USE
return nil 聽
//----------------------------------------------------------------------------//
function LoadApp()
聽 聽local cFileName := cGetFile( "DBF file| *.dbf", "Please select a DBF" )
聽 聽local oAppPanel, oItem, oLastItem, nLevel := 1, aLevels := {}
聽 聽USE ( cFileName )
聽 聽oAppPanel = oExplBar:AddPanel( cFileNoExt( cFileName ),, 700 )
聽 聽oTree = TTreeView():New( 2.5, 2, oAppPanel,,,,, 423, 675 )
聽 聽while ! EoF()
聽 聽 聽 IF field->level > nLevel
聽 聽 聽 聽 聽// Aumenta el nivel, guarda el elemento actual y crea una nueva rama
聽 聽 聽 聽 聽AADD(aLevels, oLastItem)
聽 聽 聽 聽 聽nLevel++
聽 聽 聽 聽 聽oItem := oLastItem:Add(field->prompt)
聽 聽 聽 聽 聽oLastItem := oItem
聽 聽 聽 ELSEIF field->level < nLevel
聽 聽 聽 聽 聽// Disminuye el nivel, recupera el elemento del nivel anterior
聽 聽 聽 聽 聽WHILE nLevel > field->level .AND. !EMPTY(aLevels)
聽 聽 聽 聽 聽 聽 nLevel--
聽 聽 聽 聽 聽 聽 oLastItem := ATAIL(aLevels)
聽 聽 聽 聽 聽 聽 ADEL(aLevels, LEN(aLevels))
聽 聽 聽 聽 聽ENDDO
聽 聽 聽 聽 聽if oLastItem != nil
聽 聽 聽 聽 聽 聽 oItem := oLastItem:Add( field->prompt )
聽 聽 聽 聽 聽endif 聽
聽 聽 聽 ELSE
聽 聽 聽 聽 聽// Mismo nivel, a帽ade un nuevo elemento al nivel actual
聽 聽 聽 聽 聽oItem := oTree:Add( field->prompt )
聽 聽 聽 聽 聽oLastItem := oItem
聽 聽 聽 ENDIF
聽 聽
聽 聽 聽 oItem:Cargo := field->html
聽 聽 聽 SKIP
聽 聽end 聽
聽 聽USE
return nil 聽
//----------------------------------------------------------------------------//
function HtmlDefault()
聽 聽local cHtml
聽 聽TEXT INTO cHtml
聽 聽 聽 <!DOCTYPE html>
聽 聽 聽 <html lang="es">
聽 聽 聽 <head>
聽 聽 聽 聽 聽<style>
聽 聽 聽 聽 聽 聽 body {
聽 聽 聽 聽 聽 聽 聽 聽 聽 background-color: rgb(31, 31, 31);
聽 聽 聽 聽 聽 聽 }
聽 聽 聽 聽 聽</style>
聽 聽 聽 </head>
聽 聽 聽 <body>
聽 聽 聽 </body>
聽 聽 聽 </html>
聽 聽ENDTEXT
return cHtml
//----------------------------------------------------------------------------//