feat: Add workspace selection synchronization

- Create `codewalker` module with file system utilities
- Refactor `Workspace` file operations to use `codewalker`
- Add `include_tree` flag to `HeropromptChild` struct
- Implement new `/selection` API endpoint for workspace
- Sync frontend selection state to backend via new API
This commit is contained in:
Mahmoud-Emad
2025-08-24 12:58:09 +03:00
parent 6098f166bb
commit cc93081b15
5 changed files with 306 additions and 163 deletions

View File

@@ -70,7 +70,7 @@ pub fn (app &App) api_heroprompt_directory(mut ctx Context) veb.Result {
mut wsp := hp.get(name: wsname, create: false) or {
return ctx.text('{"error":"workspace not found"}')
}
items_w := wsp.list() or { return ctx.text('{"error":"cannot list directory"}') }
items_w := wsp.list_dir(path_q) or { return ctx.text('{"error":"cannot list directory"}') }
ctx.set_content_type('application/json')
mut items := []DirItem{}
for it in items_w {
@@ -148,3 +148,33 @@ pub fn (app &App) api_heroprompt_generate_prompt(mut ctx Context, name string) v
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('{"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('{"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('{"ok":true}')
}

View File

@@ -473,6 +473,10 @@ async function generatePrompt() {
outputEl.innerHTML = '<div class="loading">Generating prompt...</div>';
try {
// sync selection to backend before generating
const paths = Array.from(selected);
await post(`/api/heroprompt/workspaces/${currentWs}/selection`, { paths: JSON.stringify(paths) });
const r = await fetch(`/api/heroprompt/workspaces/${currentWs}/prompt`, {
method: 'POST',
body: new URLSearchParams({ text: promptText })