This commit is contained in:
2025-08-17 11:07:26 +02:00
parent 3c5e0a053e
commit f3449d6812
15 changed files with 309 additions and 252 deletions

View File

@@ -8,15 +8,15 @@ import freeflowuniverse.herolib.core.pathlib
// force: true
// }
// mut args2 := generator.GeneratorArgs{
// path: '~/code/github/freeflowuniverse/herolib/lib/installers/lang/rust'
// force: true
// }
// generator.scan(args2)!
mut args := generator.GeneratorArgs{
path: '~/code/github/freeflowuniverse/herolib/lib/installers'
mut args2 := generator.GeneratorArgs{
path: '~/code/github/freeflowuniverse/herolib/lib/develop/heroprompt'
force: true
}
}
generator.scan(args2)!
generator.scan(args)!
// mut args := generator.GeneratorArgs{
// path: '~/code/github/freeflowuniverse/herolib/lib/installers'
// force: true
// }
// generator.scan(args)!

View File

@@ -4,56 +4,17 @@ import freeflowuniverse.herolib.core.pathlib
pub struct CodeWalker {
pub mut:
gitignore_patterns []string
ignorematcher IgnoreMatcher
errors []CWError
}
fn (cw CodeWalker) default_gitignore() []string {
return [
'__pycache__/',
'*.py[cod]',
'*\$py.class',
'*.so',
'.Python',
'build/',
'develop-eggs/',
'dist/',
'downloads/',
'eggs/',
'.eggs/',
'lib/',
'lib64/',
'parts/',
'sdist/',
'var/',
'wheels/',
'*.egg-info/',
'.installed.cfg',
'*.egg',
'.env',
'.venv',
'venv/',
'.tox/',
'.nox/',
'.coverage',
'.coveragerc',
'coverage.xml',
'*.cover',
'*.gem',
'*.pyc',
'.cache',
'.pytest_cache/',
'.mypy_cache/',
'.hypothesis/',
]
}
@[params]
pub struct FileMapArgs{
pub mut:
path string
content string
content_read bool = true //if we start from path, and this is on false then we don't read the content
}
pub fn (mut cw CodeWalker) filemap_get(args FileMapArgs) !FileMap {
@@ -66,12 +27,24 @@ pub fn (mut cw CodeWalker) filemap_get(args FileMapArgs) !FileMap {
}
}
//walk recursirve over the dir find all .gitignore and .heroignore
fn (mut cw CodeWalker) ignore_walk(path string) !{
//TODO: pahtlib has the features to walk
self.ignorematcher.add(path, content)!
}
//get the filemap from a path
fn (mut cw CodeWalker) filemap_get_from_path(path string) !FileMap {
mut dir := pathlib.get(path)
if !dir.exists() {
return error('Source directory "${path}" does not exist')
}
//make recursive ourselves, if we find a gitignore then we use it for the level we are on
mut files := dir.list(recursive: true)!
mut fm := FileMap{

View File

@@ -0,0 +1,88 @@
module codewalker
const default_gitignore := '
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
.env
.venv
venv/
.tox/
.nox/
.coverage
.coveragerc
coverage.xml
*.cover
*.gem
*.pyc
.cache
.pytest_cache/
.mypy_cache/
.hypothesis/
'
//responsible to help us to find if a file matches or not
pub struct IgnoreMatcher {
pub mut:
items map[string]Ignore //the key is the path where the gitignore plays
}
pub struct Ignore {
pub mut:
patterns map[string]string
}
pub fn (mut self Ignore) add(content string) ! {
for line in content.split_into_lines() {
line = line.trim_space()
if line.len == 0 {
continue
}
self.patterns[line] = line
}
}
pub fn (mut self Ignore) check(path string) !bool {
return false //TODO
}
pub fn gitignore_matcher_new() !IgnoreMatcher {
mut matcher := IgnoreMatcher{}
gitignore.add(default_gitignore)!
matcher.patterns['.gitignore'] = gitignore
return matcher
}
//add content to path of gitignore
pub fn (mut self IgnoreMatcher) add(path string, content string) ! {
self.items[path] = Ignore{}
self.items[path].add(content)!
}
pub fn (mut self IgnoreMatcher) check(path string) !bool {
return false //TODO here figure out which gitignores apply to the given path and check them all
}

View File

@@ -1,7 +1,7 @@
!!hero_code.generate_client
name:'heroprompt'
classname:'HeropromptWorkspace'
classname:'Workspace'
singleton:0
default:1
hasconfig:1

View File

@@ -0,0 +1,136 @@
module heroprompt
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook { PlayBook }
import freeflowuniverse.herolib.ui.console
import json
__global (
heroprompt_global map[string]&Workspace
heroprompt_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string = 'default'
fromdb bool // will load from filesystem
create bool // default will not create if not exist
}
pub fn new(args ArgsGet) !&Workspace {
mut obj := Workspace{
name: args.name
}
set(obj)!
return get(name: args.name)!
}
pub fn get(args ArgsGet) !&Workspace {
mut context := base.context()!
heroprompt_default = args.name
if args.fromdb || args.name !in heroprompt_global {
mut r := context.redis()!
if r.hexists('context:heroprompt', args.name)! {
data := r.hget('context:heroprompt', args.name)!
if data.len == 0 {
return error('Workspace with name: heroprompt does not exist, prob bug.')
}
mut obj := json.decode(Workspace, data)!
set_in_mem(obj)!
} else {
if args.create {
new(args)!
} else {
return error("Workspace with name 'heroprompt' does not exist")
}
}
return get(name: args.name)! // no longer from db nor create
}
return heroprompt_global[args.name] or {
return error('could not get config for heroprompt with name:heroprompt')
}
}
// register the config for the future
pub fn set(o Workspace) ! {
mut o2 := set_in_mem(o)!
heroprompt_default = o2.name
mut context := base.context()!
mut r := context.redis()!
r.hset('context:heroprompt', o2.name, json.encode(o2))!
}
// does the config exists?
pub fn exists(args ArgsGet) !bool {
mut context := base.context()!
mut r := context.redis()!
return r.hexists('context:heroprompt', args.name)!
}
pub fn delete(args ArgsGet) ! {
mut context := base.context()!
mut r := context.redis()!
r.hdel('context:heroprompt', args.name)!
}
@[params]
pub struct ArgsList {
pub mut:
fromdb bool // will load from filesystem
}
// if fromdb set: load from filesystem, and not from mem, will also reset what is in mem
pub fn list(args ArgsList) ![]&Workspace {
mut res := []&Workspace{}
mut context := base.context()!
if args.fromdb {
// reset what is in mem
heroprompt_global = map[string]&Workspace{}
heroprompt_default = ''
}
if args.fromdb {
mut r := context.redis()!
mut l := r.hkeys('context:heroprompt')!
for name in l {
res << get(name: name, fromdb: true)!
}
return res
} else {
// load from memory
for _, client in heroprompt_global {
res << client
}
}
return res
}
// only sets in mem, does not set as config
fn set_in_mem(o Workspace) !Workspace {
mut o2 := obj_init(o)!
heroprompt_global[o2.name] = &o2
heroprompt_default = o2.name
return o2
}
pub fn play(mut plbook PlayBook) ! {
if !plbook.exists(filter: 'heroprompt.') {
return
}
mut install_actions := plbook.find(filter: 'heroprompt.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
}
// switch instance to be used for heroprompt
pub fn switch(name string) {
heroprompt_default = name
}

View File

@@ -0,0 +1,33 @@
module heroprompt
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '0.0.0'
const singleton = false
const default = true
/
// Workspace represents a workspace containing multiple directories
// and their selected files for AI prompt generation
@[heap]
pub struct Workspace {
pub mut:
name string = 'default' // Workspace name
base_path string // Base path of the workspace
children []HeropromptChild // List of directories and files in this workspace
}
// your checking & initialization code if needed
fn obj_init(mycfg_ Workspace) !Workspace {
return mycfg
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_loads(heroscript string) !Workspace {
mut obj := encoderhero.decode[Workspace](heroscript)!
return obj
}

View File

@@ -5,16 +5,11 @@ import time
import os
import freeflowuniverse.herolib.core.pathlib
@[params]
struct NewWorkspaceParams {
mut:
name string
path string
}
/
/// Create a new workspace
/// If the name is not passed, we will generate a random one
fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace {
fn (wsp Workspace) new(args_ NewWorkspaceParams) !&Workspace {
mut args := args_
if args.name.len == 0 {
args.name = generate_random_workspace_name()
@@ -30,7 +25,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
}
}
mut workspace := &HeropromptWorkspace{
mut workspace := &Workspace{
name: args.name
base_path: os.real_path(args.path)
}
@@ -62,7 +57,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
// }
// // list returns the complete hierarchical structure of the workspace
// pub fn (wsp HeropromptWorkspace) list() WorkspaceList {
// pub fn (wsp Workspace) list() WorkspaceList {
// mut result := WorkspaceList{
// root_path: wsp.base_path
// }
@@ -82,7 +77,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
// }
// // build_workspace_tree recursively builds the workspace tree structure
// fn (wsp HeropromptWorkspace) build_workspace_tree(path string, depth int) []WorkspaceItem {
// fn (wsp Workspace) build_workspace_tree(path string, depth int) []WorkspaceItem {
// mut items := []WorkspaceItem{}
// entries := os.ls(path) or { return items }
@@ -147,7 +142,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
// }
// // calculate_totals counts total files and directories in the workspace
// fn (wsp HeropromptWorkspace) calculate_totals(items []WorkspaceItem, mut result WorkspaceList) {
// fn (wsp Workspace) calculate_totals(items []WorkspaceItem, mut result WorkspaceList) {
// for item in items {
// if item.is_directory {
// result.total_dirs++
@@ -159,7 +154,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
// }
// // mark_selected_items marks which items are currently selected for prompts
// fn (wsp HeropromptWorkspace) mark_selected_items(mut items []WorkspaceItem) {
// fn (wsp Workspace) mark_selected_items(mut items []WorkspaceItem) {
// for mut item in items {
// // Check if this item is selected by comparing paths
// item.is_selected = wsp.is_item_selected(item.path)
@@ -172,7 +167,7 @@ fn (wsp HeropromptWorkspace) new(args_ NewWorkspaceParams) !&HeropromptWorkspace
// }
// // is_item_selected checks if a specific path is selected in the workspace
// fn (wsp HeropromptWorkspace) is_item_selected(path string) bool {
// fn (wsp Workspace) is_item_selected(path string) bool {
// dirs := wsp.children.filter(fn (item &HeropromptChild) bool {
// return item.path.cat == .dir
// })
@@ -212,7 +207,7 @@ pub mut:
}
// add a directory to the selection (no recursion stored; recursion is done on-demand)
pub fn (mut wsp HeropromptWorkspace) add_dir(args AddDirParams) !HeropromptChild {
pub fn (mut wsp Workspace) add_dir(args AddDirParams) !HeropromptChild {
if args.path.len == 0 {
return error('The dir path is required')
}
@@ -234,7 +229,7 @@ pub fn (mut wsp HeropromptWorkspace) add_dir(args AddDirParams) !HeropromptChild
}
// add a file to the selection
pub fn (mut wsp HeropromptWorkspace) add_file(args AddFileParams) !HeropromptChild {
pub fn (mut wsp Workspace) add_file(args AddFileParams) !HeropromptChild {
if args.path.len == 0 {
return error('The file path is required')
}
@@ -273,7 +268,7 @@ fn list_files_recursive(root string) []string {
}
// build_file_content generates formatted content for all selected files (and all files under selected dirs)
fn (wsp HeropromptWorkspace) build_file_content() string {
fn (wsp Workspace) build_file_content() string {
mut content := ''
// files selected directly
for ch in wsp.children {
@@ -382,12 +377,12 @@ pub mut:
file_contents string
}
fn (wsp HeropromptWorkspace) build_user_instructions(text string) string {
fn (wsp Workspace) build_user_instructions(text string) string {
return text
}
// build_file_map creates a complete file map with base path and metadata
fn (wsp HeropromptWorkspace) build_file_map() string {
fn (wsp Workspace) build_file_map() string {
mut file_map := ''
// roots are selected directories
mut roots := []HeropromptChild{}
@@ -452,12 +447,12 @@ fn (wsp HeropromptWorkspace) build_file_map() string {
return file_map
}
pub struct HeropromptWorkspacePrompt {
pub struct WorkspacePrompt {
pub mut:
text string
}
pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
pub fn (wsp Workspace) prompt(args WorkspacePrompt) string {
user_instructions := wsp.build_user_instructions(args.text)
file_map := wsp.build_file_map()
file_contents := wsp.build_file_content()
@@ -471,7 +466,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
}
// // is_path_in_selected_dirs recursively checks subdirectories for selected items
// fn (wsp HeropromptWorkspace) is_path_in_selected_dirs(path string, dirs []&HeropromptChild) bool {
// fn (wsp Workspace) is_path_in_selected_dirs(path string, dirs []&HeropromptChild) bool {
// for dir in dirs {
// if dir.path.cat != .dir {
// continue
@@ -504,7 +499,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// select_all bool
// }
// pub fn (mut wsp HeropromptWorkspace) add_dir(args_ AddDirParams) !&HeropromptChild {
// pub fn (mut wsp Workspace) add_dir(args_ AddDirParams) !&HeropromptChild {
// if args_.path.len == 0 {
// return error('The dir path is required')
// }
@@ -542,13 +537,13 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// selected_files []SelectedFilesMetadata // Files in this directory
// }
// struct HeropromptWorkspaceGetSelected {
// struct WorkspaceGetSelected {
// pub mut:
// dirs []SelectedDirsMetadata // All directories with their selected files
// }
// pub fn (wsp HeropromptWorkspace) get_selected() HeropromptWorkspaceGetSelected {
// mut result := HeropromptWorkspaceGetSelected{}
// pub fn (wsp Workspace) get_selected() WorkspaceGetSelected {
// mut result := WorkspaceGetSelected{}
// for dir in wsp.children.filter(fn (c &HeropromptChild) bool {
// return c.path.cat == .dir
// }) {
@@ -571,18 +566,18 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// return result
// }
// pub struct HeropromptWorkspacePrompt {
// pub struct WorkspacePrompt {
// pub mut:
// text string
// }
// pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// pub fn (wsp Workspace) prompt(args WorkspacePrompt) string {
// prompt := wsp.build_prompt(args.text)
// return prompt
// }
// // Placeholder function for future needs, in case we need to highlight the user_instructions block with some addtional messages
// fn (wsp HeropromptWorkspace) build_user_instructions(text string) string {
// fn (wsp Workspace) build_user_instructions(text string) string {
// return text
// }
@@ -650,7 +645,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// }
// // build_file_content generates formatted content for all selected files
// fn (wsp HeropromptWorkspace) build_file_content() string {
// fn (wsp Workspace) build_file_content() string {
// mut content := ''
// for dir in wsp.children.filter(fn (c &HeropromptChild) bool {
@@ -679,7 +674,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// }
// // build_dir_file_content recursively processes subdirectories
// fn (wsp HeropromptWorkspace) build_dir_file_content(dirs []&HeropromptChild) string {
// fn (wsp Workspace) build_dir_file_content(dirs []&HeropromptChild) string {
// mut content := ''
// for dir in dirs {
// if dir.path.cat != .dir {
@@ -719,7 +714,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// }
// // build_prompt generates the final prompt with metadata and file tree
// fn (wsp HeropromptWorkspace) build_prompt(text string) string {
// fn (wsp Workspace) build_prompt(text string) string {
// user_instructions := wsp.build_user_instructions(text)
// file_map := wsp.build_file_map()
// file_contents := wsp.build_file_content()
@@ -735,7 +730,7 @@ pub fn (wsp HeropromptWorkspace) prompt(args HeropromptWorkspacePrompt) string {
// }
// // build_file_map creates a complete file map with base path and metadata
// fn (wsp HeropromptWorkspace) build_file_map() string {
// fn (wsp Workspace) build_file_map() string {
// mut file_map := ''
// // Consider only top-level directories as roots
// mut roots := wsp.children.filter(fn (c &HeropromptChild) bool {

View File

@@ -1,101 +0,0 @@
module heroprompt
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook { PlayBook }
__global (
heroprompt_global map[string]&HeropromptWorkspace
heroprompt_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&HeropromptWorkspace {
mut context := base.context()!
mut args := args_get(args_)
mut obj := HeropromptWorkspace{
name: args.name
}
if args.name !in heroprompt_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('heropromptworkspace', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return heroprompt_global[args.name] or {
println(heroprompt_global)
// bug if we get here because should be in globals
panic('could not get config for heroprompt with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o HeropromptWorkspace) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('heropromptworkspace', o.name, heroscript)!
}
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('heropromptworkspace', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('heropromptworkspace', args.name)!
if args.name in heroprompt_global {
// del heroprompt_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o HeropromptWorkspace) ! {
mut o2 := obj_init(o)!
heroprompt_global[o.name] = &o2
heroprompt_default = o.name
}
pub fn play(mut plbook PlayBook) ! {
mut install_actions := plbook.find(filter: 'heropromptworkspace.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
}
// switch instance to be used for heroprompt
pub fn switch(name string) {
heroprompt_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -1,57 +0,0 @@
module heroprompt
import freeflowuniverse.herolib.data.encoderhero
pub const version = '0.0.0'
const singleton = false
const default = true
// HeropromptWorkspace represents a workspace containing multiple directories
// and their selected files for AI prompt generation
@[heap]
pub struct HeropromptWorkspace {
pub mut:
name string = 'default' // Workspace name
base_path string // Base path of the workspace
children []HeropromptChild // List of directories and files in this workspace
}
@[params]
pub struct AddWorkspaceParams {
pub mut:
name string
path string
}
// add_workspace creates and adds a new workspace
pub fn new_workspace(args_ AddWorkspaceParams) !&HeropromptWorkspace {
mut wsp := &HeropromptWorkspace{}
wsp = wsp.new(name: args_.name, path: args_.path)!
return wsp
}
// get_workspace gets the saved workspace
pub fn get_workspace(args_ AddWorkspaceParams) !&HeropromptWorkspace {
if args_.name.len == 0 {
return error('Workspace name is required')
}
return get(name: args_.name)!
}
// your checking & initialization code if needed
fn obj_init(mycfg_ HeropromptWorkspace) !HeropromptWorkspace {
mut mycfg := mycfg_
return mycfg
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj HeropromptWorkspace) !string {
return encoderhero.encode[HeropromptWorkspace](obj)!
}
pub fn heroscript_loads(heroscript string) !HeropromptWorkspace {
mut obj := encoderhero.decode[HeropromptWorkspace](heroscript)!
return obj
}

View File

@@ -1,10 +0,0 @@
!!heropromptworkspace.configure name:"default"
// !!heropromptworkspace.workspace_dir name:"default"
// path:"@HOME/code/github/freeflowuniverse/herolib/lib/builder"
// selection:"path1,path2" //paths are relative in the path of workspace
// filter_exclude:","
// filter_include:","