Class: Collavre::Tools::PreviewAttachService
- Inherits:
-
Object
- Object
- Collavre::Tools::PreviewAttachService
- Extended by:
- T::Sig, ToolMeta
- Defined in:
- app/services/collavre/tools/preview_attach_service.rb
Overview
Attach a development-preview chip to a topic. AI Agents call this after spinning up ‘./bin/dev` against a worktree so the preview URL and run state surface as a chip in the typing-indicator row. Idempotent by (topic_id, worktree_id).
Instance Method Summary collapse
Instance Method Details
#call(topic_id:, preview_url:, worktree_id:, label: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/services/collavre/tools/preview_attach_service.rb', line 36 def call(topic_id:, preview_url:, worktree_id:, label: nil) validate_preview_url!(preview_url) topic = Collavre::Topic.find(topic_id) Collavre::Tools::TopicAuthorizer.(topic) refresh_config = ->(c) { # Re-attach against the same worktree may carry a fresh URL/label # (e.g. port reassignment after a restart). Refresh the persisted # config so the chip link goes to the live server, not the dead one # — applies both when reactivating a stopped chip and when the chip # is still active (:noop), which is the common dev-restart case. new_config = c.config.merge( "preview_url" => preview_url, "preview_state" => "running" ) new_config["label"] = label if label c.config = new_config # Also refresh the cached chip fields that record_event! seeded on the # first attach. The chip partial renders latest_link.presence || # default_link, so without this update a :noop reattach with a new port # leaves the chip pointing at the dead URL even though config is fresh. # (On :reactivate the subsequent inject_into_topic! would overwrite # these via record_event! anyway, so updating here is harmless.) c.latest_link = preview_url c.latest_label = c.default_label } channel, status = Collavre::ChannelAttacher.call( channel_class: Collavre::PreviewChannel, lookup: -> { lookup_channel(topic, worktree_id) }, create_attrs: { topic_id: topic.id, config: { "worktree_id" => worktree_id, "preview_url" => preview_url, "preview_state" => "running", "label" => label }.compact }, on_reactivate: refresh_config, on_noop: refresh_config ) # Mirror PR-channel UX: only the first attach (and a true reactivation # from stopped/dismissed) announces in the topic. Subsequent idempotent # attaches stay silent so frequent restarts do not spam the timeline. if status == :created || status == :reactivated channel.inject_into_topic!(channel.) end { ok: true, channel_id: channel.id, worktree_id: worktree_id, preview_url: preview_url, status: status } end |