openclacky 1.5.25 → 1.5.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -0
- data/lib/clacky/agent/chunk_index.rb +2 -1
- data/lib/clacky/agent/message_compressor_helper.rb +20 -2
- data/lib/clacky/agent/session_serializer.rb +28 -1
- data/lib/clacky/agent/skill_manager.rb +2 -8
- data/lib/clacky/agent/time_machine.rb +108 -127
- data/lib/clacky/agent/tool_executor.rb +2 -11
- data/lib/clacky/agent.rb +15 -19
- data/lib/clacky/agent_config.rb +14 -4
- data/lib/clacky/billing/media_usage.rb +69 -0
- data/lib/clacky/billing/platform_billing.rb +5 -0
- data/lib/clacky/brand_config.rb +16 -215
- data/lib/clacky/default_extensions/ext-studio/agents/ext-developer/system_prompt.md +2 -80
- data/lib/clacky/default_extensions/ext-studio/panels/studio/view.js +1 -1
- data/lib/clacky/default_extensions/ext-studio/skills/ext-develop/SKILL.md +10 -96
- data/lib/clacky/default_extensions/git/panels/git/view.js +208 -69
- data/lib/clacky/default_extensions/knowledge/README.md +1 -1
- data/lib/clacky/default_extensions/knowledge/README.zh.md +1 -1
- data/lib/clacky/default_extensions/knowledge/lib/importer.rb +1 -1
- data/lib/clacky/default_extensions/knowledge/panels/kb/view.js +3 -3
- data/lib/clacky/default_extensions/meeting/api/handler.rb +23 -2
- data/lib/clacky/default_extensions/meeting/panels/meeting/view.js +194 -36
- data/lib/clacky/default_extensions/preview/icon.svg +3 -2
- data/lib/clacky/default_extensions/secretary/panels/work/view.js +108 -33
- data/lib/clacky/default_extensions/side-chat/README.md +40 -0
- data/lib/clacky/default_extensions/side-chat/README.zh.md +40 -0
- data/lib/clacky/default_extensions/side-chat/api/handler.rb +90 -0
- data/lib/clacky/default_extensions/side-chat/api/runtime.rb +411 -0
- data/lib/clacky/default_extensions/side-chat/ext.yml +16 -0
- data/lib/clacky/default_extensions/side-chat/icon.svg +14 -0
- data/lib/clacky/default_extensions/side-chat/panels/side-chat/view.js +779 -0
- data/lib/clacky/default_extensions/terminal/README.md +36 -0
- data/lib/clacky/default_extensions/terminal/README.zh.md +36 -0
- data/lib/clacky/default_extensions/terminal/api/handler.rb +121 -0
- data/lib/clacky/default_extensions/terminal/api/runtime.rb +397 -0
- data/lib/clacky/default_extensions/terminal/ext.yml +16 -0
- data/lib/clacky/default_extensions/terminal/icon.svg +13 -0
- data/lib/clacky/default_extensions/terminal/panels/terminal/view.js +351 -0
- data/lib/clacky/default_extensions/terminal/vendor/LICENSE +21 -0
- data/lib/clacky/default_extensions/terminal/vendor/addon-fit.js +2 -0
- data/lib/clacky/default_extensions/terminal/vendor/xterm.css +218 -0
- data/lib/clacky/default_extensions/terminal/vendor/xterm.js +2 -0
- data/lib/clacky/default_skills/media-gen/SKILL.md +30 -9
- data/lib/clacky/extension/api_extension.rb +12 -0
- data/lib/clacky/locales/en.rb +14 -11
- data/lib/clacky/locales/i18n.rb +25 -0
- data/lib/clacky/locales/zh.rb +14 -11
- data/lib/clacky/media/base.rb +11 -1
- data/lib/clacky/media/openai_compat.rb +271 -3
- data/lib/clacky/message_format/open_ai.rb +17 -0
- data/lib/clacky/prompts/base.md +3 -0
- data/lib/clacky/providers.rb +33 -20
- data/lib/clacky/reasoning_levels.rb +75 -0
- data/lib/clacky/server/channel/channel_ui_controller.rb +12 -5
- data/lib/clacky/server/git_panel.rb +40 -18
- data/lib/clacky/server/http_server.rb +62 -71
- data/lib/clacky/server/session_registry.rb +3 -0
- data/lib/clacky/ui2/markdown_renderer.rb +153 -0
- data/lib/clacky/ui2/strings_cjk_patch.rb +134 -124
- data/lib/clacky/utils/file_link_normalizer.rb +30 -3
- data/lib/clacky/utils/frontmatter.rb +63 -0
- data/lib/clacky/utils/model_pricing.rb +67 -9
- data/lib/clacky/version.rb +1 -1
- data/lib/clacky/web/app.css +722 -274
- data/lib/clacky/web/app.js +2 -0
- data/lib/clacky/web/components/chat-composer.js +504 -0
- data/lib/clacky/web/components/chat-navigator.js +1 -1
- data/lib/clacky/web/components/model-picker.js +319 -20
- data/lib/clacky/web/components/quote-select.js +97 -39
- data/lib/clacky/web/core/aside.js +137 -27
- data/lib/clacky/web/core/ext.js +658 -81
- data/lib/clacky/web/features/brand/store.js +0 -4
- data/lib/clacky/web/features/brand/view.js +2 -99
- data/lib/clacky/web/features/extensions/store.js +3 -1
- data/lib/clacky/web/features/extensions/view.js +14 -15
- data/lib/clacky/web/features/new-session/store.js +4 -0
- data/lib/clacky/web/features/new-session/view.js +27 -4
- data/lib/clacky/web/features/skills/store.js +7 -5
- data/lib/clacky/web/features/skills/view.js +65 -16
- data/lib/clacky/web/features/version/view.js +1 -1
- data/lib/clacky/web/features/workspace/store.js +7 -3
- data/lib/clacky/web/features/workspace/view.js +46 -5
- data/lib/clacky/web/i18n.js +104 -58
- data/lib/clacky/web/index.html +21 -22
- data/lib/clacky/web/lib/box-diagram.js +239 -0
- data/lib/clacky/web/sessions.js +117 -151
- data/lib/clacky/web/settings.js +23 -16
- data/lib/clacky/web/ws-dispatcher.js +4 -1
- data/lib/clacky.rb +2 -0
- metadata +24 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
## How to open
|
|
2
|
+
|
|
3
|
+
- Tab bar of the session side panel → **+** (new tab) → **Official › Side chat**.
|
|
4
|
+
- Select a passage in the main conversation → the floating bar → **Side chat**: the panel opens with that passage quoted in the composer.
|
|
5
|
+
- Status: on by default. Turn it off under **Extensions › All › Official**; the tab then disappears and the API stops answering.
|
|
6
|
+
- Uses the session's own model, so a configured provider is all it needs.
|
|
7
|
+
|
|
8
|
+
## How to use
|
|
9
|
+
|
|
10
|
+
### Asking without interrupting the task
|
|
11
|
+
|
|
12
|
+
The panel is a read-only branch of the conversation you are in: it inherits the whole thread, including the tool calls the AI already ran, so it can explain what it just did, why a command failed, or what a diff changed.
|
|
13
|
+
|
|
14
|
+
- Nothing you ask here reaches the main conversation, the project files or the AI's task — ask as much as you like.
|
|
15
|
+
- The main thread keeps running while you read and ask; it is not paused and does not scroll.
|
|
16
|
+
- The branch may read files, search and browse, but it cannot write files, run commands, or trigger anything that would need your confirmation. If something needs changing, it tells you and you ask for it in the main chat.
|
|
17
|
+
|
|
18
|
+
### Quoting a passage
|
|
19
|
+
|
|
20
|
+
1. Select text inside one message in the main conversation.
|
|
21
|
+
2. Click **Side chat** on the floating bar — the passage is added to the panel as a quote chip.
|
|
22
|
+
3. Add your question and send. Entering through the bar rebuilds the branch against the conversation as it stands now, so the quote and everything around it are current.
|
|
23
|
+
|
|
24
|
+
You can also attach a file with the paperclip or by dropping it on the panel, as in the main composer — a screenshot of the bug, a log, a spec.
|
|
25
|
+
|
|
26
|
+
### Everyday actions
|
|
27
|
+
|
|
28
|
+
| Goal | How |
|
|
29
|
+
|---|---|
|
|
30
|
+
| Stop an answer | Click the stop button where send was |
|
|
31
|
+
| Close the panel | × on its tab. The branch stays alive; add the tab again to pick it up |
|
|
32
|
+
| Ask about another session | Switch sessions — each one has its own branch |
|
|
33
|
+
|
|
34
|
+
## Notes
|
|
35
|
+
|
|
36
|
+
- At most 6 branches at once across sessions. A branch nobody has been attached to for an hour is reaped the next time a panel opens — there is no timer, so an idle branch lingers until then.
|
|
37
|
+
- Branches live in the Clacky process: restarting Clacky ends them and the panel says the side chat expired — send again to start a new one.
|
|
38
|
+
- Reloading the page does not replay the panel's history. Asking again reattaches to the same branch, so the context is still there even though the earlier answers are not shown.
|
|
39
|
+
- Model calls made here are billed to the session the panel belongs to, so the cost shows up in that session's usage.
|
|
40
|
+
- The panel is a narrow column by default; drag the divider to widen it. The width is remembered per session.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
## 入口
|
|
2
|
+
|
|
3
|
+
- 会话右侧面板的标签栏 → 点 **+(新标签页)** → **官方 › 侧边聊天**。
|
|
4
|
+
- 在主对话里选中一段文字 → 浮条上的 **侧边聊天**:面板打开,并把这句引用带进输入框。
|
|
5
|
+
- 状态:默认开启。在 **扩展 › 全部 › 官方** 里可以关掉;关掉后标签页消失,接口也不再响应。
|
|
6
|
+
- 用当前会话自己的模型,配好模型即可用,不需要额外设置。
|
|
7
|
+
|
|
8
|
+
## 怎么用
|
|
9
|
+
|
|
10
|
+
### 边看边问,不打断主任务
|
|
11
|
+
|
|
12
|
+
面板是你当前会话的一条只读分支:它继承了整段对话,包括 AI 已经执行过的工具调用,所以能回答「它刚才在干嘛」「这条命令为什么失败」「这个 diff 改了什么」这类问题。
|
|
13
|
+
|
|
14
|
+
- 你在这里问的内容不会进主对话、不会动项目文件,也不会影响 AI 正在做的任务,问多少都不影响。
|
|
15
|
+
- 主对话照常在跑,不会因为你在看和问而暂停或滚动。
|
|
16
|
+
- 分支可以读文件、搜索、上网查资料,但不能改文件、跑命令,也不能触发需要你确认的操作。需要改东西时它会说明怎么改,让你回主对话里提。
|
|
17
|
+
|
|
18
|
+
### 引用主对话里的段落
|
|
19
|
+
|
|
20
|
+
1. 在主对话的某条消息里选中文字。
|
|
21
|
+
2. 点浮条上的 **侧边聊天**,这段文字以引用 chip 的形式出现在面板输入框里。
|
|
22
|
+
3. 补上你的问题再发送。从浮条进来时,发送前会按主对话的最新状态重建分支,引用和它周围的上下文都是当下的。
|
|
23
|
+
|
|
24
|
+
附件和主输入框一样:点回形针,或者把文件拖进面板(截图、日志、规格文档都行)。
|
|
25
|
+
|
|
26
|
+
### 常用操作
|
|
27
|
+
|
|
28
|
+
| 想做什么 | 怎么做 |
|
|
29
|
+
|---|---|
|
|
30
|
+
| 停止这次回答 | 发送键会变成停止键,点它 |
|
|
31
|
+
| 关掉面板 | 标签页上的 ×。分支还活着,再添加标签页就能接回来 |
|
|
32
|
+
| 问别的会话 | 直接切会话,每个会话一条独立分支 |
|
|
33
|
+
|
|
34
|
+
## 注意事项
|
|
35
|
+
|
|
36
|
+
- 跨会话最多同时 6 条分支;超过时会提示开不了新的。空闲超过 1 小时的分支会在下次打开侧聊时被回收(没有定时器,空闲期间它一直留着)。
|
|
37
|
+
- 分支活在 Clacky 进程里:重启 Clacky 后它们都没了,面板会提示这次侧边聊天已失效,再发一次就是新的分支。
|
|
38
|
+
- 刷新页面不会回放面板里的历史;再发消息会接回同一条分支,上下文还在,只是之前那几轮问答不显示了。
|
|
39
|
+
- 这里产生的模型调用计入面板所属会话的用量,账单上看得到。
|
|
40
|
+
- 面板默认是窄栏,拖分隔条可以加宽;宽度按会话记住。
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "runtime"
|
|
4
|
+
|
|
5
|
+
# Side chat: a read-only branch of the session shown in the right-hand panel.
|
|
6
|
+
#
|
|
7
|
+
# HTTP opens the branch (it is the only layer that can resolve a session's live
|
|
8
|
+
# agent through the registry); the main WebSocket carries the data plane, so the
|
|
9
|
+
# branch's own thread can push deltas to the panel that opened it. No second
|
|
10
|
+
# socket is needed — the frames are namespaced with `ext.side_chat.`.
|
|
11
|
+
class SideChatExtension < Clacky::ApiExtension
|
|
12
|
+
WS_PREFIX = "ext.side_chat."
|
|
13
|
+
RUNTIME = Clacky::Extensions::SideChat::Runtime
|
|
14
|
+
|
|
15
|
+
# `fresh: true` drops the existing branch for that session and forks the
|
|
16
|
+
# conversation again — used when the main thread has moved on and the panel's
|
|
17
|
+
# context is stale.
|
|
18
|
+
post "/open" do
|
|
19
|
+
body = json_body
|
|
20
|
+
session_id = body["session_id"].to_s
|
|
21
|
+
error!("session_id is required", status: 400) if session_id.empty?
|
|
22
|
+
|
|
23
|
+
parent = parent_agent_for(session_id)
|
|
24
|
+
error!("session not found", status: 404) unless parent
|
|
25
|
+
|
|
26
|
+
chat = begin
|
|
27
|
+
RUNTIME.open(parent_id: session_id, parent: parent, fresh: body["fresh"] == true)
|
|
28
|
+
rescue RUNTIME::CapacityError => e
|
|
29
|
+
error!(e.message, status: 429)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
json(chat_id: chat.id)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
post "/close" do
|
|
36
|
+
RUNTIME.close(json_body["chat_id"].to_s)
|
|
37
|
+
json(ok: true)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Runs on the WebSocket reader thread: every branch must stay cheap. Forking
|
|
41
|
+
# and running happen in HTTP or in a managed thread, never here.
|
|
42
|
+
def self.on_ws_event(conn, message)
|
|
43
|
+
type = message["type"].to_s
|
|
44
|
+
return false unless type.start_with?(WS_PREFIX)
|
|
45
|
+
|
|
46
|
+
chat_id = message["chat_id"].to_s
|
|
47
|
+
|
|
48
|
+
case type.delete_prefix(WS_PREFIX)
|
|
49
|
+
when "attach"
|
|
50
|
+
chat = RUNTIME.find(chat_id)
|
|
51
|
+
# A restarted server has no branches; drop the client's stale pane instead
|
|
52
|
+
# of leaving it waiting for events that can never arrive.
|
|
53
|
+
if chat
|
|
54
|
+
chat.attach(conn)
|
|
55
|
+
else
|
|
56
|
+
conn.send_json(type: "ext.side_chat.gone", chat_id: chat_id)
|
|
57
|
+
end
|
|
58
|
+
when "send"
|
|
59
|
+
chat = RUNTIME.find(chat_id)
|
|
60
|
+
if chat.nil?
|
|
61
|
+
conn.send_json(type: "ext.side_chat.gone", chat_id: chat_id)
|
|
62
|
+
elsif chat.ask(text: message["text"], files: message["files"], references: message["references"]) == :busy
|
|
63
|
+
conn.send_json(type: "ext.side_chat.busy", chat_id: chat_id)
|
|
64
|
+
end
|
|
65
|
+
when "interrupt"
|
|
66
|
+
RUNTIME.find(chat_id)&.interrupt
|
|
67
|
+
when "detach"
|
|
68
|
+
RUNTIME.find(chat_id)&.detach(conn)
|
|
69
|
+
when "close"
|
|
70
|
+
RUNTIME.close(chat_id)
|
|
71
|
+
else
|
|
72
|
+
return false
|
|
73
|
+
end
|
|
74
|
+
true
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def self.on_ws_close(conn)
|
|
78
|
+
RUNTIME.detach(conn)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private def parent_agent_for(session_id)
|
|
82
|
+
reg = registry
|
|
83
|
+
return nil unless reg
|
|
84
|
+
|
|
85
|
+
# A session that is on screen but was never restored in this process (e.g.
|
|
86
|
+
# the server restarted) has to be loaded before it can be forked.
|
|
87
|
+
reg.ensure(session_id) unless reg.exist?(session_id)
|
|
88
|
+
reg.get(session_id)&.dig(:agent)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
require_relative "../../../server/web_ui_controller"
|
|
6
|
+
|
|
7
|
+
module Clacky
|
|
8
|
+
module Extensions
|
|
9
|
+
module SideChat
|
|
10
|
+
# In-memory pool of read-only conversation branches backing the side chat panel.
|
|
11
|
+
#
|
|
12
|
+
# A side chat is a `fork_subagent` of the session it belongs to: it inherits
|
|
13
|
+
# the whole conversation (including tool traffic), so it can answer questions
|
|
14
|
+
# about what the main window already shows. It never writes into the main
|
|
15
|
+
# conversation, never reaches the session registry and never touches disk —
|
|
16
|
+
# closing the panel (or the process) throws the branch away.
|
|
17
|
+
#
|
|
18
|
+
# This file is loaded once via `require_relative` from handler.rb: the
|
|
19
|
+
# extension loader re-`load`s handler.rb when its mtime changes, and live
|
|
20
|
+
# branches and their threads must survive that reload.
|
|
21
|
+
#
|
|
22
|
+
# Wire format (JSON frames over the main WebSocket):
|
|
23
|
+
# server → client ext.side_chat.attached { chat_id, model, running, cost }
|
|
24
|
+
# ext.side_chat.user { chat_id, text, files, references }
|
|
25
|
+
# ext.side_chat.event { chat_id, event: { type: ..., ... } }
|
|
26
|
+
# ext.side_chat.turn { chat_id, state, cost, iterations, duration }
|
|
27
|
+
# ext.side_chat.busy { chat_id } # a turn is in flight
|
|
28
|
+
# ext.side_chat.gone { chat_id }
|
|
29
|
+
# client → server attach | send | interrupt | detach | close
|
|
30
|
+
class Runtime
|
|
31
|
+
MAX_CHATS = 6
|
|
32
|
+
IDLE_TTL = 60 * 60 # an unattached branch is reaped after this
|
|
33
|
+
MAX_TEXT_CHARS = 16_000
|
|
34
|
+
MAX_REFERENCE_CHARS = 8_000
|
|
35
|
+
MAX_REFERENCES = 12
|
|
36
|
+
|
|
37
|
+
# Writing, running commands and asking the user a question all assume the
|
|
38
|
+
# branch owns the task; a read-only branch must not. `todo_manager` and
|
|
39
|
+
# `invoke_skill` mutate shared state (task list, skill evolution).
|
|
40
|
+
FORBIDDEN_TOOLS = %w[
|
|
41
|
+
write edit trash_manager terminal ask_user todo_manager invoke_skill
|
|
42
|
+
browser manage_work delegate_work yield_work
|
|
43
|
+
].freeze
|
|
44
|
+
|
|
45
|
+
# Forwarded to the panel. Everything else a UI controller emits describes
|
|
46
|
+
# chrome the panel does not have (sessionbar, input_queue, complete, …).
|
|
47
|
+
PANEL_EVENTS = %w[
|
|
48
|
+
assistant_message_delta assistant_message_stream_end assistant_message
|
|
49
|
+
tool_call tool_args tool_result tool_error
|
|
50
|
+
progress
|
|
51
|
+
info warning error
|
|
52
|
+
].freeze
|
|
53
|
+
|
|
54
|
+
BRIEF = <<~TEXT.strip
|
|
55
|
+
[SIDE CHAT — READ-ONLY BRANCH]
|
|
56
|
+
|
|
57
|
+
You are answering in a side panel attached to the conversation above. The user opened it because something in the main thread needs clarifying and they did not want to scroll down and interrupt the task.
|
|
58
|
+
|
|
59
|
+
- Answer their question about the conversation above. Keep it short: the panel is narrow.
|
|
60
|
+
- This branch is read-only. You may read files, search and browse, but you cannot change anything, and nothing you say here reaches the project or the main conversation.
|
|
61
|
+
- Do not continue or advance the main task. If a change is needed, describe it and tell the user to ask for it in the main chat.
|
|
62
|
+
TEXT
|
|
63
|
+
|
|
64
|
+
class CapacityError < StandardError; end
|
|
65
|
+
|
|
66
|
+
# UI controller for one branch: forwards the interesting events to the
|
|
67
|
+
# panel and bills every model call into the parent session.
|
|
68
|
+
class PanelUI < Clacky::Server::WebUIController
|
|
69
|
+
def initialize(chat_id, broadcaster, billing:)
|
|
70
|
+
super(chat_id, broadcaster)
|
|
71
|
+
@billing = billing
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def show_token_usage(token_data)
|
|
75
|
+
@billing.call(token_data)
|
|
76
|
+
super
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class Chat
|
|
81
|
+
attr_reader :id, :parent_id, :model
|
|
82
|
+
|
|
83
|
+
def initialize(parent:, agent:, model:)
|
|
84
|
+
@id = SecureRandom.hex(6)
|
|
85
|
+
@parent = parent
|
|
86
|
+
@agent = agent
|
|
87
|
+
@model = model
|
|
88
|
+
@parent_id = parent.session_id
|
|
89
|
+
@mutex = Mutex.new
|
|
90
|
+
@conn = nil
|
|
91
|
+
@thread = nil
|
|
92
|
+
@running = false
|
|
93
|
+
@closed = false
|
|
94
|
+
@cost = 0.0
|
|
95
|
+
@iterations = 0
|
|
96
|
+
@last_seen = Runtime.now
|
|
97
|
+
|
|
98
|
+
@agent.instance_variable_set(:@ui, PanelUI.new(@id, ->(_sid, event) { deliver(event) }, billing: method(:record_spend)))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def running?
|
|
102
|
+
@mutex.synchronize { @running }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def cost
|
|
106
|
+
@mutex.synchronize { @cost }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def attach(conn)
|
|
110
|
+
@mutex.synchronize do
|
|
111
|
+
@conn = conn
|
|
112
|
+
@last_seen = Runtime.now
|
|
113
|
+
end
|
|
114
|
+
conn.send_json(
|
|
115
|
+
type: "ext.side_chat.attached",
|
|
116
|
+
chat_id: @id,
|
|
117
|
+
model: @model,
|
|
118
|
+
running: running?,
|
|
119
|
+
cost: cost.round(6)
|
|
120
|
+
)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def detach(conn)
|
|
124
|
+
@mutex.synchronize do
|
|
125
|
+
next unless @conn.equal?(conn)
|
|
126
|
+
@conn = nil
|
|
127
|
+
@last_seen = Runtime.now
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def attached?
|
|
132
|
+
@mutex.synchronize { !@conn.nil? }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def idle_for
|
|
136
|
+
Runtime.now - @mutex.synchronize { @last_seen }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# @return [Symbol] :started, :busy or :empty
|
|
140
|
+
def ask(text:, files: [], references: [])
|
|
141
|
+
prompt = text.to_s.strip[0, MAX_TEXT_CHARS].to_s
|
|
142
|
+
files = [] unless files.is_a?(Array)
|
|
143
|
+
return :empty if prompt.empty? && files.empty?
|
|
144
|
+
|
|
145
|
+
status = @mutex.synchronize do
|
|
146
|
+
if @closed
|
|
147
|
+
:closed
|
|
148
|
+
elsif @running
|
|
149
|
+
:busy
|
|
150
|
+
else
|
|
151
|
+
@running = true
|
|
152
|
+
:ok
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
return status unless status == :ok
|
|
156
|
+
|
|
157
|
+
refs = normalize_references(references)
|
|
158
|
+
# The turn start is authoritative: the panel paints "running" from
|
|
159
|
+
# this frame instead of guessing at send time, because an `attached`
|
|
160
|
+
# snapshot answered before the send can land after it.
|
|
161
|
+
broadcast(type: "busy")
|
|
162
|
+
broadcast(type: "user", text: prompt, files: files, references: refs)
|
|
163
|
+
|
|
164
|
+
@thread = Clacky::ThreadRegistry.spawn(name: "side-chat-#{@id}", daemon: true) do
|
|
165
|
+
run_turn(prompt, files, refs)
|
|
166
|
+
end
|
|
167
|
+
:started
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def interrupt
|
|
171
|
+
thread = @mutex.synchronize { @thread }
|
|
172
|
+
return false unless thread && thread.alive?
|
|
173
|
+
|
|
174
|
+
# Same mechanism the server uses for Ctrl+C: `Agent#run` rescues this,
|
|
175
|
+
# marks the run interrupted and re-raises, so our turn thread finishes
|
|
176
|
+
# normally and can report the state to the panel.
|
|
177
|
+
thread.raise(Clacky::AgentInterrupted, "Interrupted by user")
|
|
178
|
+
true
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def shutdown
|
|
182
|
+
@mutex.synchronize { @closed = true }
|
|
183
|
+
interrupt
|
|
184
|
+
@conn = nil
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
private def run_turn(prompt, files, refs)
|
|
188
|
+
started = Runtime.now
|
|
189
|
+
result = nil
|
|
190
|
+
state = "done"
|
|
191
|
+
|
|
192
|
+
begin
|
|
193
|
+
result = @agent.run(prompt, files: files, reference_contexts: refs)
|
|
194
|
+
rescue Clacky::AgentInterrupted
|
|
195
|
+
state = "interrupted"
|
|
196
|
+
rescue StandardError => e
|
|
197
|
+
state = "error"
|
|
198
|
+
broadcast(type: "event", event: { type: "error", session_id: @id, message: e.message })
|
|
199
|
+
ensure
|
|
200
|
+
# A second interrupt (the user hitting stop again) would otherwise
|
|
201
|
+
# land between the rescue and this call, ending the thread with
|
|
202
|
+
# @running still true — a panel stuck on "stop" for the rest of the
|
|
203
|
+
# branch's life. Deferring delivery for these few statements closes
|
|
204
|
+
# that window; anything still pending arrives after the state is
|
|
205
|
+
# published.
|
|
206
|
+
Thread.handle_interrupt(Clacky::AgentInterrupted => :never) do
|
|
207
|
+
finish_turn(state, result, Runtime.now - started)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
private def finish_turn(state, result, duration)
|
|
213
|
+
iterations = result.is_a?(Hash) ? result[:iterations].to_i : 0
|
|
214
|
+
total = @mutex.synchronize do
|
|
215
|
+
@running = false
|
|
216
|
+
@thread = nil
|
|
217
|
+
@iterations += iterations
|
|
218
|
+
@last_seen = Runtime.now
|
|
219
|
+
@cost
|
|
220
|
+
end
|
|
221
|
+
broadcast(
|
|
222
|
+
type: "turn",
|
|
223
|
+
state: state,
|
|
224
|
+
cost: total.round(6),
|
|
225
|
+
iterations: @iterations,
|
|
226
|
+
duration: duration.round(2)
|
|
227
|
+
)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Called from the agent thread for every model call. The fork skips
|
|
231
|
+
# billing on purpose (@is_subagent), so the spend is recorded against
|
|
232
|
+
# the parent session and merged into the parent's running total —
|
|
233
|
+
# otherwise side chat would be free and the sessionbar would lie.
|
|
234
|
+
private def record_spend(token_data)
|
|
235
|
+
cost = token_data[:cost]
|
|
236
|
+
usage = {
|
|
237
|
+
prompt_tokens: token_data[:prompt_tokens].to_i,
|
|
238
|
+
completion_tokens: token_data[:completion_tokens].to_i,
|
|
239
|
+
cache_read_input_tokens: token_data[:cache_read].to_i,
|
|
240
|
+
cache_creation_input_tokens: token_data[:cache_write].to_i
|
|
241
|
+
}
|
|
242
|
+
@mutex.synchronize { @cost += cost.to_f }
|
|
243
|
+
@parent.persist_billing_record(usage, cost, model: @model)
|
|
244
|
+
@parent.absorb_subagent_cost({ total_cost_usd: cost.to_f }, notify_ui: true)
|
|
245
|
+
rescue StandardError => e
|
|
246
|
+
Clacky::Logger.warn("side_chat.billing_error", chat_id: @id, error: e.message)
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
private def deliver(event)
|
|
250
|
+
type = event[:type].to_s
|
|
251
|
+
return unless PANEL_EVENTS.include?(type)
|
|
252
|
+
|
|
253
|
+
broadcast(type: "event", event: event)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def broadcast(payload)
|
|
257
|
+
conn = @mutex.synchronize { @conn }
|
|
258
|
+
return unless conn
|
|
259
|
+
|
|
260
|
+
conn.send_json(payload.merge(type: "ext.side_chat.#{payload[:type]}", chat_id: @id))
|
|
261
|
+
rescue StandardError => e
|
|
262
|
+
Clacky::Logger.warn("side_chat.send_error", chat_id: @id, error: e.message)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
private def normalize_references(references)
|
|
266
|
+
return [] unless references.is_a?(Array)
|
|
267
|
+
|
|
268
|
+
references.first(MAX_REFERENCES).filter_map do |ref|
|
|
269
|
+
next unless ref.is_a?(Hash)
|
|
270
|
+
label = ref[:label] || ref["label"]
|
|
271
|
+
body = (ref[:text] || ref["text"]).to_s.strip[0, MAX_REFERENCE_CHARS]
|
|
272
|
+
next if body.nil? || body.empty?
|
|
273
|
+
|
|
274
|
+
{ type: "quote", label: label.to_s, text: body, context: context_for(label, body) }
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
private def context_for(label, body)
|
|
279
|
+
heading = label.to_s.empty? ? "[Quoted excerpt from this conversation]" :
|
|
280
|
+
"[Quoted excerpt from this conversation: #{label}]"
|
|
281
|
+
"#{heading}\n#{body}\n[End of quoted excerpt]"
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
class << self
|
|
286
|
+
def open(parent_id:, parent:, model: nil, fresh: false)
|
|
287
|
+
existing = find_by_parent(parent_id)
|
|
288
|
+
return existing if existing && !fresh
|
|
289
|
+
|
|
290
|
+
# A fresh open takes over the slot its own parent's branch holds, so
|
|
291
|
+
# the cap only applies when there is nothing of its own to replace.
|
|
292
|
+
check_capacity! unless existing
|
|
293
|
+
register(build(parent: parent, model: model), replace_parent: fresh ? parent_id : nil)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def find(chat_id)
|
|
297
|
+
id = chat_id.to_s
|
|
298
|
+
return nil if id.empty?
|
|
299
|
+
|
|
300
|
+
mutex.synchronize { @chats[id] }
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def find_by_parent(parent_id)
|
|
304
|
+
id = parent_id.to_s
|
|
305
|
+
return nil if id.empty?
|
|
306
|
+
|
|
307
|
+
mutex.synchronize { @chats.values.find { |chat| chat.parent_id == id } }
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Called from the WebSocket reader thread — no locking beyond a swap.
|
|
311
|
+
def close(chat_id)
|
|
312
|
+
chat = mutex.synchronize { @chats.delete(chat_id.to_s) }
|
|
313
|
+
chat&.shutdown
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def detach(conn)
|
|
317
|
+
chats = mutex.synchronize { @chats.values }
|
|
318
|
+
chats.each { |chat| chat.detach(conn) }
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def reset!
|
|
322
|
+
chats = mutex.synchronize do
|
|
323
|
+
list = @chats.values
|
|
324
|
+
@chats = {}
|
|
325
|
+
list
|
|
326
|
+
end
|
|
327
|
+
chats.each(&:shutdown)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def now
|
|
331
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
private def build(parent:, model:)
|
|
335
|
+
# Forking outside the lock: it deep-copies the whole conversation and
|
|
336
|
+
# must not block the WebSocket reader thread from finding a chat.
|
|
337
|
+
agent = parent.fork_subagent(model: model, forbidden_tools: FORBIDDEN_TOOLS)
|
|
338
|
+
# Same shape append_user_input uses for injected context: the branch
|
|
339
|
+
# reads it, replay and the main conversation never do.
|
|
340
|
+
agent.history.append({ role: "user", content: BRIEF, system_injected: true })
|
|
341
|
+
|
|
342
|
+
Chat.new(
|
|
343
|
+
parent: parent,
|
|
344
|
+
agent: agent,
|
|
345
|
+
model: model || parent.config.effective_model_name
|
|
346
|
+
)
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# `build` copies the whole conversation, so forking a chat the cap will
|
|
350
|
+
# refuse wastes that work. `register` re-checks under its own lock,
|
|
351
|
+
# which is what actually enforces the cap — two opens can pass this
|
|
352
|
+
# check together and only one of them will fit.
|
|
353
|
+
private def check_capacity!
|
|
354
|
+
mutex.synchronize do
|
|
355
|
+
sweep_locked
|
|
356
|
+
guard_capacity_locked
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
# Replacing the parent's branch happens under the same lock as the
|
|
361
|
+
# insert: closing first and registering afterwards let two concurrent
|
|
362
|
+
# fresh opens leave two branches behind for one session.
|
|
363
|
+
private def register(chat, replace_parent: nil)
|
|
364
|
+
replaced = []
|
|
365
|
+
mutex.synchronize do
|
|
366
|
+
sweep_locked
|
|
367
|
+
if replace_parent
|
|
368
|
+
key = replace_parent.to_s
|
|
369
|
+
@chats.delete_if do |_id, existing|
|
|
370
|
+
next false unless existing.parent_id == key
|
|
371
|
+
replaced << existing
|
|
372
|
+
true
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
guard_capacity_locked
|
|
376
|
+
|
|
377
|
+
@chats[chat.id] = chat
|
|
378
|
+
end
|
|
379
|
+
replaced.each(&:shutdown)
|
|
380
|
+
chat
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
private def guard_capacity_locked
|
|
384
|
+
return if @chats.size < MAX_CHATS
|
|
385
|
+
|
|
386
|
+
raise CapacityError, "too many side chats open (max #{MAX_CHATS})"
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
private def sweep_locked
|
|
390
|
+
@chats.delete_if do |_id, chat|
|
|
391
|
+
stale = !chat.attached? && chat.idle_for > IDLE_TTL
|
|
392
|
+
chat.shutdown if stale
|
|
393
|
+
stale
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
private def mutex
|
|
398
|
+
@mutex ||= Mutex.new
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def chats
|
|
402
|
+
mutex.synchronize { @chats.values }
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
@chats = {}
|
|
407
|
+
@mutex = Mutex.new
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
id: side-chat
|
|
2
|
+
name: Side chat
|
|
3
|
+
display_name: Side chat
|
|
4
|
+
display_name_zh: 侧边聊天
|
|
5
|
+
description: Ask about this conversation in the right panel, without leaving your place in it
|
|
6
|
+
description_zh: 在当前会话右侧追问,不用滚到底部打断主对话(只读分支)
|
|
7
|
+
version: "0.1.0"
|
|
8
|
+
icon: icon.svg
|
|
9
|
+
author: OpenClacky
|
|
10
|
+
license: MIT
|
|
11
|
+
enabled_by_default: true
|
|
12
|
+
contributes:
|
|
13
|
+
api: api/handler.rb
|
|
14
|
+
panels:
|
|
15
|
+
- id: side-chat
|
|
16
|
+
view: panels/side-chat/view.js
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg width="36" height="36" viewBox="0 0 36 36" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="side-chat-bg" x1="18" y1="0" x2="18" y2="36" gradientUnits="userSpaceOnUse">
|
|
4
|
+
<stop stop-color="#818CF8"/>
|
|
5
|
+
<stop offset="1" stop-color="#4F46E5"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="36" height="36" fill="url(#side-chat-bg)"/>
|
|
9
|
+
<g transform="translate(8 8) scale(0.8333)" stroke="#fff" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
|
10
|
+
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
|
|
11
|
+
<path d="M13 8H7"/>
|
|
12
|
+
<path d="M17 12H7"/>
|
|
13
|
+
</g>
|
|
14
|
+
</svg>
|