PoC running commands

This commit is contained in:
Jorge Manrubia
2025-05-10 11:17:35 +02:00
parent b7ee0b284d
commit 413dfce5aa
4 changed files with 84 additions and 14 deletions
@@ -1,5 +1,6 @@
import { Controller } from "@hotwired/stimulus"
import { HttpStatus } from "helpers/http_helpers"
import { delay } from "helpers/timing_helpers"
export default class extends Controller {
static targets = [ "input", "form", "confirmation" ]
@@ -39,7 +40,15 @@ export default class extends Controller {
handleCommandResponse(event) {
if (event.detail.success) {
this.#reset()
const response = event.detail.fetchResponse;
if (response && response.response.headers.get("Content-Type")?.includes("application/json")) {
response.response.json().then((commands) => {
this.element.querySelector("#chat-responses").textContent = JSON.stringify(commands, null, 2)
this.#executeCommands(commands)
});
} else {
this.#reset()
}
} else {
const response = event.detail.fetchResponse.response
this.#handleErrorResponse(response)
@@ -117,4 +126,18 @@ export default class extends Controller {
this.confirmationTarget.value = "confirmed"
this.formTarget.requestSubmit()
}
async #executeCommands(commands) {
for (const command of commands) {
if (command.command == "/search") {
const params = new URLSearchParams(command)
// Clunky example, for PoC purposes
Turbo.visit(`/cards?${params.toString()}`)
await delay(2000)
} else {
this.inputTarget.value = command.command
this.formTarget.requestSubmit()
}
}
}
}