Files
herolib/lib/web/ui/heroprompt_api.v
Mahmoud-Emad 63c0b81fc9 feat: Support multi-root workspaces
- Remove `base_path` from Workspace struct and APIs
- Enable adding multiple root directories to a workspace
- Update file tree UI to display all workspace roots
- Refactor file map generation for multi-root display
- Improve prompt output clipboard copy with status
2025-09-07 14:40:17 +03:00

311 lines
8.7 KiB
V

module ui
import veb
import os
import json
import freeflowuniverse.herolib.develop.heroprompt as hp
// Types
struct DirResp {
path string
items []hp.ListItem
}
// Utility functions
fn expand_home_path(path string) string {
if path.starts_with('~') {
home := os.home_dir()
return os.join_path(home, path.all_after('~'))
}
return path
}
fn json_error(message string) string {
return '{"error":"${message}"}'
}
fn json_success() string {
return '{"ok":true}'
}
// Recursive search function
fn search_directory(dir_path string, base_path string, query_lower string, mut results []map[string]string) {
entries := os.ls(dir_path) or { return }
for entry in entries {
full_path := os.join_path(dir_path, entry)
// Skip hidden files and common ignore patterns
if entry.starts_with('.') || entry == 'node_modules' || entry == 'target'
|| entry == 'build' {
continue
}
// Get relative path from workspace base
mut rel_path := full_path
if full_path.starts_with(base_path) {
rel_path = full_path[base_path.len..]
if rel_path.starts_with('/') {
rel_path = rel_path[1..]
}
}
// Check if filename or path matches search query
if entry.to_lower().contains(query_lower) || rel_path.to_lower().contains(query_lower) {
results << {
'name': entry
'path': rel_path
'full_path': full_path
'type': if os.is_dir(full_path) { 'directory' } else { 'file' }
}
}
// Recursively search subdirectories
if os.is_dir(full_path) {
search_directory(full_path, base_path, query_lower, mut results)
}
}
}
// APIs
@['/api/heroprompt/workspaces'; get]
pub fn (app &App) api_heroprompt_list(mut ctx Context) veb.Result {
mut names := []string{}
ws := hp.list_workspaces_fromdb() or { []&hp.Workspace{} }
for w in ws {
names << w.name
}
ctx.set_content_type('application/json')
return ctx.text(json.encode(names))
}
@['/api/heroprompt/workspaces'; post]
pub fn (app &App) api_heroprompt_create(mut ctx Context) veb.Result {
name_input := ctx.form['name'] or { '' }
// Name is now required
mut name := name_input.trim(' \t\n\r')
if name.len == 0 {
return ctx.text(json_error('workspace name is required'))
}
wsp := hp.get(name: name, create: true) or { return ctx.text(json_error('create failed')) }
ctx.set_content_type('application/json')
return ctx.text(json.encode({
'name': wsp.name
}))
}
@['/api/heroprompt/workspaces/:name'; get]
pub fn (app &App) api_heroprompt_get(mut ctx Context, name string) veb.Result {
wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
ctx.set_content_type('application/json')
return ctx.text(json.encode({
'name': wsp.name
'selected_files': wsp.selected_children().len.str()
}))
}
@['/api/heroprompt/workspaces/:name'; put]
pub fn (app &App) api_heroprompt_update(mut ctx Context, name string) veb.Result {
wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
new_name := ctx.form['name'] or { name }
// Update the workspace using the update_workspace method
updated_wsp := wsp.update_workspace(
name: new_name
) or { return ctx.text(json_error('failed to update workspace')) }
ctx.set_content_type('application/json')
return ctx.text(json.encode({
'name': updated_wsp.name
}))
}
// Delete endpoint using POST (VEB framework compatibility)
@['/api/heroprompt/workspaces/:name/delete'; post]
pub fn (app &App) api_heroprompt_delete(mut ctx Context, name string) veb.Result {
wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
// Delete the workspace
wsp.delete_workspace() or { return ctx.text(json_error('failed to delete workspace')) }
ctx.set_content_type('application/json')
return ctx.text(json_success())
}
@['/api/heroprompt/directory'; get]
pub fn (app &App) api_heroprompt_directory(mut ctx Context) veb.Result {
wsname := ctx.query['name'] or { 'default' }
path_q := ctx.query['path'] or { '' }
base_path := ctx.query['base'] or { '' }
if base_path.len == 0 {
return ctx.text(json_error('base path is required'))
}
mut wsp := hp.get(name: wsname, create: false) or {
return ctx.text(json_error('workspace not found'))
}
items := wsp.list_dir(base_path, path_q) or {
return ctx.text(json_error('cannot list directory'))
}
ctx.set_content_type('application/json')
return ctx.text(json.encode(DirResp{
path: if path_q.len > 0 { path_q } else { base_path }
items: items
}))
}
@['/api/heroprompt/file'; get]
pub fn (app &App) api_heroprompt_file(mut ctx Context) veb.Result {
wsname := ctx.query['name'] or { 'default' }
path_q := ctx.query['path'] or { '' }
if path_q.len == 0 {
return ctx.text(json_error('path required'))
}
// Use the path directly (should be absolute)
file_path := path_q
if !os.is_file(file_path) {
return ctx.text(json_error('not a file'))
}
content := os.read_file(file_path) or { return ctx.text(json_error('failed to read')) }
ctx.set_content_type('application/json')
return ctx.text(json.encode({
'language': detect_lang(file_path)
'content': content
}))
}
@['/api/heroprompt/workspaces/:name/files'; post]
pub fn (app &App) api_heroprompt_add_file(mut ctx Context, name string) veb.Result {
path := ctx.form['path'] or { '' }
if path.len == 0 {
return ctx.text(json_error('path required'))
}
mut wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
wsp.add_file(path: path) or { return ctx.text(json_error(err.msg())) }
return ctx.text(json_success())
}
@['/api/heroprompt/workspaces/:name/dirs'; post]
pub fn (app &App) api_heroprompt_add_dir(mut ctx Context, name string) veb.Result {
path := ctx.form['path'] or { '' }
if path.len == 0 {
return ctx.text(json_error('path required'))
}
mut wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
wsp.add_dir(path: path) or { return ctx.text(json_error(err.msg())) }
return ctx.text(json_success())
}
@['/api/heroprompt/workspaces/:name/prompt'; post]
pub fn (app &App) api_heroprompt_generate_prompt(mut ctx Context, name string) veb.Result {
text := ctx.form['text'] or { '' }
mut wsp := hp.get(name: name, create: false) or {
ctx.set_content_type('application/json')
return ctx.text(json_error('workspace not found'))
}
prompt := wsp.prompt(text: text)
ctx.set_content_type('text/plain')
return ctx.text(prompt)
}
@['/api/heroprompt/workspaces/:name/selection'; post]
pub fn (app &App) api_heroprompt_sync_selection(mut ctx Context, name string) veb.Result {
paths_json := ctx.form['paths'] or { '[]' }
mut wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
// Clear current selection
wsp.children.clear()
// Parse paths and add them to workspace
paths := json.decode([]string, paths_json) or {
return ctx.text(json_error('invalid paths format'))
}
for path in paths {
if os.is_file(path) {
wsp.add_file(path: path) or {
continue // Skip files that can't be added
}
} else if os.is_dir(path) {
wsp.add_dir(path: path) or {
continue // Skip directories that can't be added
}
}
}
return ctx.text(json_success())
}
@['/api/heroprompt/workspaces/:name/search'; get]
pub fn (app &App) api_heroprompt_search(mut ctx Context, name string) veb.Result {
query := ctx.query['q'] or { '' }
base_path := ctx.query['base'] or { '' }
if query.len == 0 {
return ctx.text(json_error('search query required'))
}
if base_path.len == 0 {
return ctx.text(json_error('base path required for search'))
}
wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
// Simple recursive file search implementation
mut results := []map[string]string{}
query_lower := query.to_lower()
// Recursive function to search files
search_directory(base_path, base_path, query_lower, mut results)
ctx.set_content_type('application/json')
// Manually build JSON response to avoid encoding issues
mut json_results := '['
for i, result in results {
if i > 0 {
json_results += ','
}
json_results += '{'
json_results += '"name":"${result['name']}",'
json_results += '"path":"${result['path']}",'
json_results += '"full_path":"${result['full_path']}",'
json_results += '"type":"${result['type']}"'
json_results += '}'
}
json_results += ']'
response := '{"query":"${query}","results":${json_results},"count":"${results.len}"}'
return ctx.text(response)
}
@['/api/heroprompt/workspaces/:name/children'; get]
pub fn (app &App) api_heroprompt_get_children(mut ctx Context, name string) veb.Result {
wsp := hp.get(name: name, create: false) or {
return ctx.text(json_error('workspace not found'))
}
children := wsp.selected_children()
ctx.set_content_type('application/json')
return ctx.text(json.encode(children))
}