Files
RoboMovel/simulacao/robo-movel-godot/addons/claude_godot/agent/background_tasks.gd
T
mguinhosandClaude Opus 5.5 393019705c Projeto: app demo, servidor/firmware em software/, simulacao Godot, planta do bloco D, chapas MDF da base, renders e slides
- app/: app demo (Kivy/buildozer) da API do robo
- software/: servidor RoboMovel (SLAM, profundidade, navegacao) e firmware da base movel (movido de firmware/)
- simulacao/: projeto Godot do robo e script de lancamento servidor + simulacao
- modelos/: planta do 5o andar do bloco D, cena Blender do corredor e modelo real do robo
- partes/mdf/: chapas identificadas e nesting da base do robo (DXF/SVG/FCStd)
- projeto/render, relatorio/imagens, slides: renders finais e apresentacoes de progresso
- ferramentas/scripts: encaixe de chapas, planta do bloco D e render do corredor no Blender
- .gitignore: ignora backups do Blender/FreeCAD e .godot/

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 23:38:16 -03:00

117 lines
3.9 KiB
GDScript

## The background tasks running right now, and the ones that recently ended.
##
## Static because a task has to outlive the turn that started it. The tool
## that starts one returns within the same turn; the run itself is still going
## several turns later, and the agent checking on it is a fresh tool call with
## no memory of the first. Somewhere has to hold the list, and it cannot be
## the turn loop.
##
## Everything here lives for as long as the editor session does. Tasks are not
## written to disk and do not survive a restart — a run holds an open HTTP
## request and a provider's in-memory history, neither of which resumes.
class_name ClaudeGodotBackgroundTasks
extends RefCounted
## Concurrency is cheap in the editor (all these runs interleave on one
## thread) but not free at the provider: every one of them is a separate
## billed conversation, and rate limits are per key. Four at once is enough to
## be useful and small enough to notice on a bill.
const MAX_CONCURRENT := 4
## Finished runs stay readable so a result isn't lost between turns, but not
## forever — each one holds its whole provider history.
const KEEP_FINISHED := 10
static var _host: Node = null
static var _on_change: Callable = Callable()
static var _runs: Array = []
static var _next_id := 1
## Wires the registry to the node that will parent the runs — they need a
## place in the tree for their HTTP requests to work — and to whoever wants
## redrawing when one makes progress.
static func bind(host: Node, on_change: Callable) -> void:
_host = host
_on_change = on_change
## Returns the new run, or {"error": String}.
static func start(brief: String, label: String, context: Dictionary, step_cap: int) -> Dictionary:
if _host == null or not is_instance_valid(_host):
return {"error": "Background tasks aren't available: the agent dock isn't open."}
if running().size() >= MAX_CONCURRENT:
return {"error": "Already running %d background tasks, which is the limit. Wait for one to finish, or stop one." % MAX_CONCURRENT}
var run := ClaudeGodotTaskRun.new()
run.name = "task_%d" % _next_id
run.setup("t%d" % _next_id, brief, label, context, step_cap)
_next_id += 1
if _on_change.is_valid():
run.changed.connect(func(r): _on_change.call(r))
_host.add_child(run)
_runs.append(run)
_forget_old()
run.start()
# The run's own first `changed` is a whole model call away. Announcing the
# start here is what puts the row in the dock while the user is still
# reading the message that asked for it.
if _on_change.is_valid():
_on_change.call(run)
return {"run": run}
## The node the runs hang off, which is the agent itself — the tool needs it
## to borrow the chat's provider and model.
static func host() -> Node:
return _host if is_instance_valid(_host) else null
static func all() -> Array:
_prune_freed()
return _runs.duplicate()
static func running() -> Array:
var out: Array = []
for run in all():
if run.is_running():
out.append(run)
return out
static func find(id: String) -> ClaudeGodotTaskRun:
for run in all():
if run.id == id:
return run
return null
static func stop_all() -> void:
for run in running():
run.stop()
## One line per task, or "" when there are none — the shape the system prompt
## and the tool's list action both want.
static func summary_lines() -> String:
var runs := all()
if runs.is_empty():
return ""
var lines: Array = []
for run in runs:
lines.append(run.headline())
return "\n".join(lines)
static func _prune_freed() -> void:
var live: Array = []
for run in _runs:
if is_instance_valid(run):
live.append(run)
_runs = live
## Drops the oldest finished runs once there are too many. Running ones are
## never touched, however many there are.
static func _forget_old() -> void:
var finished: Array = []
for run in all():
if not run.is_running():
finished.append(run)
while finished.size() > KEEP_FINISHED:
var oldest: ClaudeGodotTaskRun = finished.pop_front()
_runs.erase(oldest)
oldest.queue_free()