openclacky 1.5.23 → 1.5.24
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 +46 -0
- data/lib/clacky/agent/cost_tracker.rb +55 -36
- data/lib/clacky/agent/hook_manager.rb +4 -1
- data/lib/clacky/agent/llm_caller.rb +46 -12
- data/lib/clacky/agent/memory_injector.rb +209 -0
- data/lib/clacky/agent/memory_updater.rb +16 -0
- data/lib/clacky/agent/skill_manager.rb +30 -8
- data/lib/clacky/agent.rb +66 -2
- data/lib/clacky/agent_config.rb +4 -2
- data/lib/clacky/anthropic_stream_aggregator.rb +11 -1
- data/lib/clacky/bedrock_stream_aggregator.rb +11 -1
- data/lib/clacky/billing/platform_billing.rb +1 -0
- data/lib/clacky/cli.rb +4 -3
- data/lib/clacky/client.rb +30 -25
- data/lib/clacky/default_extensions/advisor/panels/advisor/view.js +7 -1
- data/lib/clacky/default_extensions/git/panels/git/view.js +35 -4
- data/lib/clacky/default_extensions/secretary/README.md +69 -0
- data/lib/clacky/default_extensions/secretary/README.zh.md +69 -0
- data/lib/clacky/default_extensions/secretary/ext.yml +3 -0
- data/lib/clacky/default_extensions/secretary/icon.svg +17 -0
- data/lib/clacky/default_skills/channel-manager/SKILL.md +24 -5
- data/lib/clacky/default_skills/channel-manager/dingtalk_setup.rb +4 -2
- data/lib/clacky/default_skills/channel-manager/discord_setup.rb +4 -1
- data/lib/clacky/default_skills/channel-manager/weixin_setup.rb +2 -0
- data/lib/clacky/default_skills/media-gen/SKILL.md +14 -6
- data/lib/clacky/default_skills/onboard/SKILL.md +5 -7
- data/lib/clacky/default_skills/persist-memory/SKILL.md +15 -1
- data/lib/clacky/extension/api_extension.rb +13 -0
- data/lib/clacky/json_ui_controller.rb +14 -1
- data/lib/clacky/locales/i18n.rb +25 -3
- data/lib/clacky/message_history.rb +15 -2
- data/lib/clacky/openai_responses_stream_aggregator.rb +11 -1
- data/lib/clacky/openai_stream_aggregator.rb +11 -1
- data/lib/clacky/prompts/base.md +1 -1
- data/lib/clacky/providers.rb +7 -3
- data/lib/clacky/server/backup_manager.rb +98 -2
- data/lib/clacky/server/browser_manager.rb +10 -5
- data/lib/clacky/server/channel/channel_manager.rb +69 -45
- data/lib/clacky/server/http_server.rb +133 -42
- data/lib/clacky/server/preview.rb +26 -5
- data/lib/clacky/server/web_ui_controller.rb +12 -0
- data/lib/clacky/skill.rb +1 -1
- data/lib/clacky/ui_interface.rb +5 -0
- data/lib/clacky/utils/file_processor.rb +4 -2
- data/lib/clacky/utils/model_pricing.rb +19 -0
- data/lib/clacky/version.rb +1 -1
- data/lib/clacky/web/app.css +220 -6
- data/lib/clacky/web/app.js +18 -6
- data/lib/clacky/web/components/custom-select.js +7 -18
- data/lib/clacky/web/components/hint-tip.js +172 -0
- data/lib/clacky/web/components/sidebar.js +70 -0
- data/lib/clacky/web/features/backup/store.js +64 -27
- data/lib/clacky/web/features/backup/view.js +42 -11
- data/lib/clacky/web/features/channels/view.js +7 -7
- data/lib/clacky/web/features/new-session/view.js +10 -2
- data/lib/clacky/web/features/profile/view.js +4 -4
- data/lib/clacky/web/features/tasks/view.js +1 -0
- data/lib/clacky/web/features/trash/view.js +4 -4
- data/lib/clacky/web/i18n.js +61 -5
- data/lib/clacky/web/index.html +140 -43
- data/lib/clacky/web/projects.js +15 -2
- data/lib/clacky/web/sessions.js +234 -45
- data/lib/clacky/web/settings.js +93 -34
- data/lib/clacky/web/utils.js +44 -0
- data/lib/clacky/web/ws-dispatcher.js +17 -0
- data/lib/clacky/web/ws.js +16 -4
- data/lib/clacky/work/service.rb +2 -0
- data/lib/clacky.rb +1 -0
- metadata +6 -1
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
|
+
require "find"
|
|
5
|
+
require "open3"
|
|
4
6
|
require "tmpdir"
|
|
5
7
|
require "yaml"
|
|
6
8
|
require "time"
|
|
9
|
+
require "zlib"
|
|
7
10
|
|
|
8
11
|
module Clacky
|
|
9
12
|
module Server
|
|
@@ -21,6 +24,35 @@ module Clacky
|
|
|
21
24
|
CLACKY_DIR = File.expand_path("~/.clacky")
|
|
22
25
|
CONFIG_FILE = File.join(CLACKY_DIR, "backup.yml")
|
|
23
26
|
|
|
27
|
+
# Minimal gzip writer over the raw deflate stream.
|
|
28
|
+
#
|
|
29
|
+
# Two reasons not to use Zlib::GzipWriter: it raises Zlib::BufError once
|
|
30
|
+
# a stream reaches a few MB, and Z_NO_FLUSH intermittently reports "no
|
|
31
|
+
# progress" (also surfaced as BufError). Flushing every chunk costs ~0.3%
|
|
32
|
+
# in size on real backups and keeps the stream well-behaved.
|
|
33
|
+
module GzipStream
|
|
34
|
+
HEADER = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03".b
|
|
35
|
+
|
|
36
|
+
def self.write(io)
|
|
37
|
+
io.write(HEADER)
|
|
38
|
+
deflate = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -Zlib::MAX_WBITS)
|
|
39
|
+
crc = 0
|
|
40
|
+
size = 0
|
|
41
|
+
|
|
42
|
+
yield lambda { |chunk|
|
|
43
|
+
io.write(deflate.deflate(chunk, Zlib::SYNC_FLUSH))
|
|
44
|
+
crc = Zlib.crc32(chunk, crc)
|
|
45
|
+
size += chunk.bytesize
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
io.write(deflate.finish)
|
|
49
|
+
io.write([crc, size & 0xffffffff].pack("V2"))
|
|
50
|
+
ensure
|
|
51
|
+
deflate.close if deflate && !deflate.closed?
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
private_constant :GzipStream
|
|
55
|
+
|
|
24
56
|
# Always excluded — regenerable or disposable.
|
|
25
57
|
ALWAYS_EXCLUDE = %w[
|
|
26
58
|
ocr_cache parsers parsers-1 logger safety_logs trash .DS_Store backup.yml
|
|
@@ -161,10 +193,74 @@ module Clacky
|
|
|
161
193
|
end
|
|
162
194
|
|
|
163
195
|
private def build_archive(archive, excludes)
|
|
164
|
-
|
|
196
|
+
total = source_bytes(excludes)
|
|
197
|
+
done = 0
|
|
198
|
+
report_progress(done, total)
|
|
199
|
+
last_report = Time.now
|
|
200
|
+
|
|
201
|
+
args = ["tar", "-c", "-C", CLACKY_DIR]
|
|
165
202
|
excludes.each { |e| args << "--exclude=./#{e}" }
|
|
166
203
|
args << "."
|
|
167
|
-
|
|
204
|
+
|
|
205
|
+
ok = false
|
|
206
|
+
File.open(archive, "wb") do |out|
|
|
207
|
+
GzipStream.write(out) do |sink|
|
|
208
|
+
Open3.popen2(*args) do |stdin, stdout, wait|
|
|
209
|
+
stdin.close
|
|
210
|
+
stdout.binmode
|
|
211
|
+
while (chunk = stdout.read(64 * 1024))
|
|
212
|
+
sink.call(chunk)
|
|
213
|
+
done += chunk.bytesize
|
|
214
|
+
# Throttled: one update per 100ms is plenty for a 500ms
|
|
215
|
+
# poll and keeps the mutex out of the copy loop.
|
|
216
|
+
if Time.now - last_report >= 0.1
|
|
217
|
+
report_progress(done, total)
|
|
218
|
+
last_report = Time.now
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
ok = wait.value.success?
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
ok
|
|
226
|
+
ensure
|
|
227
|
+
report_progress(nil, nil)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Source bytes the archive will read, so the UI can show a real
|
|
231
|
+
# percentage instead of a spinner. `tar` itself reports nothing.
|
|
232
|
+
private def source_bytes(excludes)
|
|
233
|
+
total = 0
|
|
234
|
+
Find.find(CLACKY_DIR) do |path|
|
|
235
|
+
next if path == CLACKY_DIR
|
|
236
|
+
|
|
237
|
+
rel = path[(CLACKY_DIR.length + 1)..-1]
|
|
238
|
+
if excludes.include?(rel.to_s.split("/", 2).first)
|
|
239
|
+
Find.prune if File.lstat(path).directory?
|
|
240
|
+
next
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
stat = File.lstat(path)
|
|
244
|
+
total += stat.size if stat.file?
|
|
245
|
+
rescue SystemCallError
|
|
246
|
+
next
|
|
247
|
+
end
|
|
248
|
+
total
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# { done:, total: } while packing, nil otherwise. Polled by the UI.
|
|
252
|
+
def progress
|
|
253
|
+
progress_mutex.synchronize { @progress && @progress.dup }
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
private def report_progress(done, total)
|
|
257
|
+
progress_mutex.synchronize do
|
|
258
|
+
@progress = done.nil? ? nil : { done: done, total: total }
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
private def progress_mutex
|
|
263
|
+
@progress_mutex ||= Mutex.new
|
|
168
264
|
end
|
|
169
265
|
|
|
170
266
|
private def prune(dest, keep)
|
|
@@ -111,12 +111,17 @@ module Clacky
|
|
|
111
111
|
|
|
112
112
|
# Returns a status hash with real daemon liveness.
|
|
113
113
|
# Uses wait_thr.alive? for a lightweight check — no ping, no mutex needed.
|
|
114
|
-
#
|
|
114
|
+
# `configured` tells "browser.yml was never written" apart from "set up but
|
|
115
|
+
# switched off"; `enabled` alone cannot, because it is just false in both.
|
|
116
|
+
# @return [Hash] { configured: bool, enabled: bool, daemon_running: bool,
|
|
117
|
+
# chrome_version: String|nil }
|
|
115
118
|
def status
|
|
116
|
-
cfg
|
|
117
|
-
|
|
119
|
+
cfg = load_config
|
|
120
|
+
configured = File.exist?(BROWSER_CONFIG_PATH)
|
|
121
|
+
enabled = cfg["enabled"] == true
|
|
118
122
|
running = @process && @process[:wait_thr]&.alive?
|
|
119
123
|
{
|
|
124
|
+
configured: configured,
|
|
120
125
|
enabled: enabled,
|
|
121
126
|
daemon_running: !!running,
|
|
122
127
|
chrome_version: cfg["chrome_version"],
|
|
@@ -146,7 +151,7 @@ module Clacky
|
|
|
146
151
|
# @param max_base64_bytes [Integer, nil] screenshot size ceiling in bytes
|
|
147
152
|
# @return [Hash] the effective settings after the update
|
|
148
153
|
def update_screenshot_settings(max_base64_bytes: nil)
|
|
149
|
-
raise "Browser not configured. Run /browser-setup first." unless File.exist?(BROWSER_CONFIG_PATH)
|
|
154
|
+
raise Clacky::BrowserNotConfiguredError, "Browser not configured. Run /browser-setup first." unless File.exist?(BROWSER_CONFIG_PATH)
|
|
150
155
|
|
|
151
156
|
if !max_base64_bytes.nil? && !(max_base64_bytes.is_a?(Integer) && max_base64_bytes.positive?)
|
|
152
157
|
raise ArgumentError, "screenshot_max_base64_bytes must be a positive integer"
|
|
@@ -164,7 +169,7 @@ module Clacky
|
|
|
164
169
|
# Raises if browser.yml doesn't exist (not yet set up).
|
|
165
170
|
# @return [Boolean] new enabled state
|
|
166
171
|
def toggle
|
|
167
|
-
raise "Browser not configured. Run /browser-setup first." unless File.exist?(BROWSER_CONFIG_PATH)
|
|
172
|
+
raise Clacky::BrowserNotConfiguredError, "Browser not configured. Run /browser-setup first." unless File.exist?(BROWSER_CONFIG_PATH)
|
|
168
173
|
|
|
169
174
|
cfg = load_config
|
|
170
175
|
new_enabled = !(cfg["enabled"] == true)
|
|
@@ -48,6 +48,7 @@ module Clacky
|
|
|
48
48
|
@running = false
|
|
49
49
|
@mutex = Mutex.new
|
|
50
50
|
@session_counters = {}
|
|
51
|
+
@binding_mutex = Mutex.new
|
|
51
52
|
@dedup_mutex = Mutex.new
|
|
52
53
|
@last_message = {} # channel_key => [digest, monotonic_time]
|
|
53
54
|
end
|
|
@@ -317,6 +318,22 @@ module Clacky
|
|
|
317
318
|
return
|
|
318
319
|
end
|
|
319
320
|
|
|
321
|
+
# A setup request from a conversation claims one chat, once. Consume
|
|
322
|
+
# the target after persisting the binding; other chats keep normal routing.
|
|
323
|
+
@binding_mutex.synchronize do
|
|
324
|
+
instance = @channel_config.instance(event[:platform], event[:channel_instance_id])
|
|
325
|
+
target_id = instance && instance["bind_session_id"]
|
|
326
|
+
unless target_id.to_s.empty?
|
|
327
|
+
unless @registry.ensure(target_id)
|
|
328
|
+
adapter.send_text(event[:chat_id], "The conversation selected during setup no longer exists. Configure the channel again.")
|
|
329
|
+
return
|
|
330
|
+
end
|
|
331
|
+
bind_session(event, target_id)
|
|
332
|
+
instance.delete("bind_session_id")
|
|
333
|
+
@channel_config.save
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
|
|
320
337
|
# Handle built-in commands
|
|
321
338
|
if text&.match?(KNOWN_COMMAND)
|
|
322
339
|
handle_command(adapter, event, text)
|
|
@@ -418,6 +435,57 @@ module Clacky
|
|
|
418
435
|
end
|
|
419
436
|
end
|
|
420
437
|
|
|
438
|
+
def bind_session(event, session_id)
|
|
439
|
+
raise ArgumentError, "Session not found" unless @registry.ensure(session_id)
|
|
440
|
+
key = channel_key(event)
|
|
441
|
+
# Detach channel_ui from the old session's web_ui, reattach to the new one.
|
|
442
|
+
# Stale channel_info for this key is then cleared from every other session
|
|
443
|
+
# (including ones evicted from memory) so resolve_session and
|
|
444
|
+
# restore_channel_bindings never see two sessions claim the same key.
|
|
445
|
+
old_session_id = resolve_session(event)
|
|
446
|
+
channel_ui = old_session_id ? channel_ui_for_session(old_session_id) : nil
|
|
447
|
+
cleared_agent = nil
|
|
448
|
+
|
|
449
|
+
if channel_ui
|
|
450
|
+
@registry.with_session(old_session_id) do |s|
|
|
451
|
+
s[:ui]&.unsubscribe_channel(channel_ui)
|
|
452
|
+
s.delete(:channel_ui)
|
|
453
|
+
agent = s[:agent]
|
|
454
|
+
if agent&.channel_info && channel_key_from_info(agent.channel_info) == key
|
|
455
|
+
agent.channel_info = nil
|
|
456
|
+
cleared_agent = agent
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
else
|
|
460
|
+
channel_ui = ChannelUIController.new(event, -> { adapter_for(event[:platform], event[:channel_instance_id]) }, -> { @channel_config.status_messages_enabled? }, -> { @channel_config.process_messages_enabled? }, -> { @channel_config.progress_cards_enabled? })
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
@persist_session&.call(cleared_agent) if cleared_agent
|
|
464
|
+
clear_channel_info_for_key(key, keep_session_id: session_id)
|
|
465
|
+
|
|
466
|
+
bind_key_to_session(key, session_id)
|
|
467
|
+
bound_agent = nil
|
|
468
|
+
@registry.with_session(session_id) do |s|
|
|
469
|
+
s[:ui]&.subscribe_channel(channel_ui)
|
|
470
|
+
s[:channel_ui] = channel_ui
|
|
471
|
+
|
|
472
|
+
# Stamp channel_info now instead of waiting for the next inbound message
|
|
473
|
+
# (see route_message): a restart in between would otherwise find no session
|
|
474
|
+
# carrying this key and drop the binding.
|
|
475
|
+
agent = s[:agent]
|
|
476
|
+
if agent
|
|
477
|
+
agent.channel_info = extract_channel_info(event)
|
|
478
|
+
bound_agent = agent
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
# Flush outside with_session: the registry mutex must not be held across file IO.
|
|
483
|
+
@persist_session&.call(bound_agent) if bound_agent
|
|
484
|
+
|
|
485
|
+
Clacky::Logger.info("[ChannelManager] Bound #{key} -> session #{session_id[0, 8]}")
|
|
486
|
+
session_id
|
|
487
|
+
end
|
|
488
|
+
|
|
421
489
|
def handle_command(adapter, event, text)
|
|
422
490
|
chat_id = event[:chat_id]
|
|
423
491
|
key = channel_key(event)
|
|
@@ -451,51 +519,7 @@ module Clacky
|
|
|
451
519
|
return
|
|
452
520
|
end
|
|
453
521
|
|
|
454
|
-
|
|
455
|
-
# Stale channel_info for this key is then cleared from every other session
|
|
456
|
-
# (including ones evicted from memory) so resolve_session and
|
|
457
|
-
# restore_channel_bindings never see two sessions claim the same key.
|
|
458
|
-
old_session_id = resolve_session(event)
|
|
459
|
-
channel_ui = old_session_id ? channel_ui_for_session(old_session_id) : nil
|
|
460
|
-
cleared_agent = nil
|
|
461
|
-
|
|
462
|
-
if channel_ui
|
|
463
|
-
@registry.with_session(old_session_id) do |s|
|
|
464
|
-
s[:ui]&.unsubscribe_channel(channel_ui)
|
|
465
|
-
s.delete(:channel_ui)
|
|
466
|
-
agent = s[:agent]
|
|
467
|
-
if agent&.channel_info && channel_key_from_info(agent.channel_info) == key
|
|
468
|
-
agent.channel_info = nil
|
|
469
|
-
cleared_agent = agent
|
|
470
|
-
end
|
|
471
|
-
end
|
|
472
|
-
else
|
|
473
|
-
channel_ui = ChannelUIController.new(event, -> { adapter_for(event[:platform], event[:channel_instance_id]) }, -> { @channel_config.status_messages_enabled? }, -> { @channel_config.process_messages_enabled? }, -> { @channel_config.progress_cards_enabled? })
|
|
474
|
-
end
|
|
475
|
-
|
|
476
|
-
@persist_session&.call(cleared_agent) if cleared_agent
|
|
477
|
-
clear_channel_info_for_key(key, keep_session_id: session_id)
|
|
478
|
-
|
|
479
|
-
bind_key_to_session(key, session_id)
|
|
480
|
-
bound_agent = nil
|
|
481
|
-
@registry.with_session(session_id) do |s|
|
|
482
|
-
s[:ui]&.subscribe_channel(channel_ui)
|
|
483
|
-
s[:channel_ui] = channel_ui
|
|
484
|
-
|
|
485
|
-
# Stamp channel_info now instead of waiting for the next inbound message
|
|
486
|
-
# (see route_message): a restart in between would otherwise find no session
|
|
487
|
-
# carrying this key and drop the binding.
|
|
488
|
-
agent = s[:agent]
|
|
489
|
-
if agent
|
|
490
|
-
agent.channel_info = extract_channel_info(event)
|
|
491
|
-
bound_agent = agent
|
|
492
|
-
end
|
|
493
|
-
end
|
|
494
|
-
|
|
495
|
-
# Flush outside with_session: the registry mutex must not be held across file IO.
|
|
496
|
-
@persist_session&.call(bound_agent) if bound_agent
|
|
497
|
-
|
|
498
|
-
Clacky::Logger.info("[ChannelManager] Bound #{key} -> session #{session_id[0, 8]}")
|
|
522
|
+
bind_session(event, session_id)
|
|
499
523
|
adapter.send_text(chat_id, "Bound to session `#{session_id[0, 8]}`.")
|
|
500
524
|
|
|
501
525
|
when "/stop"
|
|
@@ -328,6 +328,9 @@ module Clacky
|
|
|
328
328
|
# One-time migration: move legacy trash contents into file-trash/ subdirectory.
|
|
329
329
|
Clacky::TrashDirectory.migrate_legacy_if_needed
|
|
330
330
|
|
|
331
|
+
# Drop the tree a previous backup restore parked next to ~/.clacky.
|
|
332
|
+
sweep_pre_restore_dirs
|
|
333
|
+
|
|
331
334
|
# Enable console logging for the server process so log lines are visible
|
|
332
335
|
# in the terminal. Only echo when stderr is a real terminal — under a
|
|
333
336
|
# LaunchAgent it's redirected to the log file, and echoing would write
|
|
@@ -605,7 +608,7 @@ module Clacky
|
|
|
605
608
|
path = req.path
|
|
606
609
|
method = req.request_method
|
|
607
610
|
|
|
608
|
-
Thread.current[:lang] = req["X-Lang"]
|
|
611
|
+
Thread.current[:lang] = Clacky::I18n.normalize(req["X-Lang"])
|
|
609
612
|
|
|
610
613
|
# CORS preflight — must run BEFORE the access-key guard because a
|
|
611
614
|
# browser preflight carries no credentials (only the
|
|
@@ -664,9 +667,10 @@ module Clacky
|
|
|
664
667
|
30
|
|
665
668
|
elsif path == "/api/media/video/understand"
|
|
666
669
|
60
|
|
667
|
-
elsif path
|
|
668
|
-
#
|
|
669
|
-
|
|
670
|
+
elsif path == "/api/backup/restore" || path == "/api/backup/run" || path.start_with?("/api/backup/download")
|
|
671
|
+
# Restore (upload + extract + swap), run (packing to disk) and
|
|
672
|
+
# download (packing + streaming a large body) can take minutes.
|
|
673
|
+
600
|
|
670
674
|
elsif path == "/api/store/extension/install"
|
|
671
675
|
10 # now async — just spawns a thread and returns job_id immediately
|
|
672
676
|
elsif path == "/api/store/extension/import"
|
|
@@ -742,6 +746,7 @@ module Clacky
|
|
|
742
746
|
when ["GET", "/api/backup/status"] then api_backup_status(res)
|
|
743
747
|
when ["POST", "/api/backup/run"] then api_backup_run(res)
|
|
744
748
|
when ["GET", "/api/backup/download"] then api_backup_download(res)
|
|
749
|
+
when ["GET", "/api/backup/progress"] then api_backup_progress(res)
|
|
745
750
|
when ["POST", "/api/backup/restore"] then api_backup_restore(req, res)
|
|
746
751
|
when ["POST", "/api/backup/open-folder"] then api_backup_open_folder(res)
|
|
747
752
|
when ["PATCH", "/api/backup/config"] then api_backup_config(req, res)
|
|
@@ -1753,6 +1758,8 @@ module Clacky
|
|
|
1753
1758
|
def api_browser_toggle(res)
|
|
1754
1759
|
enabled = @browser_manager.toggle
|
|
1755
1760
|
json_response(res, 200, { ok: true, enabled: enabled })
|
|
1761
|
+
rescue Clacky::BrowserNotConfiguredError => e
|
|
1762
|
+
json_response(res, 409, { ok: false, code: "not_configured", error: e.message })
|
|
1756
1763
|
rescue StandardError => e
|
|
1757
1764
|
json_response(res, 500, { ok: false, error: e.message })
|
|
1758
1765
|
end
|
|
@@ -1804,6 +1811,17 @@ module Clacky
|
|
|
1804
1811
|
ensure
|
|
1805
1812
|
FileUtils.rm_f(result[:path]) if result && result[:path]
|
|
1806
1813
|
end
|
|
1814
|
+
# GET /api/backup/progress — packing progress of the archive the
|
|
1815
|
+
# download endpoint is currently building. Polled by the settings page.
|
|
1816
|
+
def api_backup_progress(res)
|
|
1817
|
+
p = BackupManager.progress
|
|
1818
|
+
if p
|
|
1819
|
+
json_response(res, 200, { packing: true, done: p[:done], total: p[:total] })
|
|
1820
|
+
else
|
|
1821
|
+
json_response(res, 200, { packing: false })
|
|
1822
|
+
end
|
|
1823
|
+
end
|
|
1824
|
+
|
|
1807
1825
|
# Body: { enabled?, cron?, dest_dir?, keep?, include_sessions? }
|
|
1808
1826
|
def api_backup_config(req, res)
|
|
1809
1827
|
body = parse_json_body(req) || {}
|
|
@@ -2563,7 +2581,7 @@ module Clacky
|
|
|
2563
2581
|
# on the next server start when the user chooses to skip the conversation.
|
|
2564
2582
|
def api_onboard_skip_soul(req, res)
|
|
2565
2583
|
body = parse_json_body(req)
|
|
2566
|
-
lang = body["lang"].
|
|
2584
|
+
lang = Clacky::I18n.normalize(body["lang"]) || Clacky::I18n.env_locale
|
|
2567
2585
|
soul_content = lang == "zh" ? DEFAULT_SOUL_MD_ZH : DEFAULT_SOUL_MD
|
|
2568
2586
|
|
|
2569
2587
|
agents_dir = File.expand_path("~/.clacky/agents")
|
|
@@ -3188,7 +3206,7 @@ module Clacky
|
|
|
3188
3206
|
dir = container[:dir].to_s
|
|
3189
3207
|
return nil if dir.empty?
|
|
3190
3208
|
|
|
3191
|
-
names =
|
|
3209
|
+
names = Clacky::I18n.locale == "zh" ? %w[README.zh.md README.md] : %w[README.md]
|
|
3192
3210
|
path = names.map { |n| File.join(dir, n) }.find { |p| File.file?(p) }
|
|
3193
3211
|
path && File.read(path, encoding: "UTF-8")
|
|
3194
3212
|
end
|
|
@@ -4961,6 +4979,16 @@ module Clacky
|
|
|
4961
4979
|
fields = body.transform_keys(&:to_sym).reject { |k, _| %i[platform instance_id name id].include?(k) }
|
|
4962
4980
|
fields = fields.transform_values { |v| v.is_a?(String) ? v.strip : v }
|
|
4963
4981
|
|
|
4982
|
+
# Setup may originate in an existing conversation. Persist its target
|
|
4983
|
+
# before starting the adapter, so the first inbound message reuses it.
|
|
4984
|
+
if fields.key?(:bind_session_id)
|
|
4985
|
+
target_id = fields[:bind_session_id].to_s.strip
|
|
4986
|
+
if target_id.empty? || !@registry.ensure(target_id)
|
|
4987
|
+
return json_response(res, 422, { ok: false, error: "Binding session not found" })
|
|
4988
|
+
end
|
|
4989
|
+
fields[:bind_session_id] = target_id
|
|
4990
|
+
end
|
|
4991
|
+
|
|
4964
4992
|
# Record when the token was last updated so clients can detect re-login
|
|
4965
4993
|
fields[:token_updated_at] = Time.now.to_i if platform == :weixin && fields.key?(:token)
|
|
4966
4994
|
fields[:token_updated_at] = Time.now.to_i if platform == :discord && fields.key?(:bot_token)
|
|
@@ -6479,21 +6507,15 @@ module Clacky
|
|
|
6479
6507
|
|
|
6480
6508
|
# GET /api/memories
|
|
6481
6509
|
# Returns { ok:, dir:, memories: [ { filename, topic, description, updated_at, size, preview } ] }
|
|
6482
|
-
# Sorted by
|
|
6510
|
+
# Sorted by file mtime descending — the same signal the agent sees, refreshed
|
|
6511
|
+
# by every write and by recall-memory's touch.
|
|
6483
6512
|
private def api_memories_list(res)
|
|
6484
6513
|
FileUtils.mkdir_p(MEMORIES_DIR)
|
|
6485
6514
|
memories = Dir.glob(File.join(MEMORIES_DIR, "*.md")).map do |path|
|
|
6486
6515
|
_memory_summary(path)
|
|
6487
6516
|
end.compact
|
|
6488
6517
|
|
|
6489
|
-
|
|
6490
|
-
# `mtime` is always present in the summary (ISO 8601), so we use it as the
|
|
6491
|
-
# ultimate tiebreaker. Negate by reversing after sort for descending order.
|
|
6492
|
-
memories.sort_by! do |m|
|
|
6493
|
-
key = m[:updated_at].to_s
|
|
6494
|
-
key = m[:mtime].to_s if key.empty?
|
|
6495
|
-
key
|
|
6496
|
-
end
|
|
6518
|
+
memories.sort_by! { |m| m[:mtime].to_s }
|
|
6497
6519
|
memories.reverse!
|
|
6498
6520
|
|
|
6499
6521
|
json_response(res, 200, { ok: true, dir: MEMORIES_DIR, memories: memories })
|
|
@@ -6622,12 +6644,11 @@ module Clacky
|
|
|
6622
6644
|
if content.start_with?("---")
|
|
6623
6645
|
if (m = content.match(/\A---\s*\n(.*?)\n---\s*\n/m))
|
|
6624
6646
|
begin
|
|
6625
|
-
#
|
|
6626
|
-
#
|
|
6647
|
+
# Legacy files may still carry `updated_at: 2026-05-01`; permit Date so
|
|
6648
|
+
# they parse instead of raising DisallowedClass. The value itself is ignored.
|
|
6627
6649
|
fm = YAML.safe_load(m[1], permitted_classes: [Date, Time]) || {}
|
|
6628
6650
|
topic = fm["topic"].to_s unless fm["topic"].to_s.strip.empty?
|
|
6629
6651
|
description = fm["description"].to_s
|
|
6630
|
-
updated_at = fm["updated_at"].to_s unless fm["updated_at"].to_s.strip.empty?
|
|
6631
6652
|
rescue StandardError
|
|
6632
6653
|
# Bad frontmatter — fall back to defaults above.
|
|
6633
6654
|
end
|
|
@@ -6798,27 +6819,50 @@ module Clacky
|
|
|
6798
6819
|
})
|
|
6799
6820
|
end
|
|
6800
6821
|
|
|
6801
|
-
# POST /api/backup/restore — accept a tar.gz upload,
|
|
6822
|
+
# POST /api/backup/restore — accept a tar.gz upload, swap it in over
|
|
6823
|
+
# ~/.clacky, then hot-restart.
|
|
6824
|
+
#
|
|
6825
|
+
# The archive can be several hundred MB while ~/.clacky can be several
|
|
6826
|
+
# GB, so nothing here buffers or copies the whole tree: the upload is
|
|
6827
|
+
# streamed to disk, extracted into a sibling staging directory, and
|
|
6828
|
+
# swapped in with two renames. A bad archive therefore never leaves
|
|
6829
|
+
# ~/.clacky half-overwritten.
|
|
6802
6830
|
def api_backup_restore(req, res)
|
|
6803
|
-
|
|
6804
|
-
|
|
6805
|
-
|
|
6806
|
-
clacky_dir = File.expand_path("~/.clacky")
|
|
6807
|
-
stamp = Time.now.strftime("%Y%m%d-%H%M%S")
|
|
6831
|
+
clacky_dir = File.expand_path("~/.clacky")
|
|
6832
|
+
parent = File.dirname(clacky_dir)
|
|
6833
|
+
stamp = Time.now.strftime("%Y%m%d-%H%M%S")
|
|
6808
6834
|
tmp_archive = File.join(Dir.tmpdir, "clacky-restore-#{stamp}.tar.gz")
|
|
6809
|
-
|
|
6810
|
-
|
|
6811
|
-
|
|
6812
|
-
|
|
6813
|
-
|
|
6835
|
+
staging = File.join(parent, ".clacky-restore-#{stamp}")
|
|
6836
|
+
retired = File.join(parent, ".clacky-pre-restore-#{stamp}")
|
|
6837
|
+
|
|
6838
|
+
received = 0
|
|
6839
|
+
File.open(tmp_archive, "wb") do |f|
|
|
6840
|
+
req.body do |chunk|
|
|
6841
|
+
received += chunk.bytesize
|
|
6842
|
+
f.write(chunk)
|
|
6843
|
+
end
|
|
6844
|
+
end
|
|
6845
|
+
return json_response(res, 400, { error: "Empty body" }) if received.zero?
|
|
6846
|
+
unless gzip_archive?(tmp_archive)
|
|
6847
|
+
return json_response(res, 400, { error: "Not a .tar.gz archive" })
|
|
6848
|
+
end
|
|
6814
6849
|
|
|
6815
|
-
|
|
6816
|
-
|
|
6817
|
-
|
|
6818
|
-
FileUtils.
|
|
6850
|
+
FileUtils.rm_rf(staging)
|
|
6851
|
+
FileUtils.mkdir_p(staging)
|
|
6852
|
+
unless system("tar", "-xzf", tmp_archive, "-C", staging)
|
|
6853
|
+
FileUtils.rm_rf(staging)
|
|
6819
6854
|
return json_response(res, 500, { error: "Failed to extract archive" })
|
|
6820
6855
|
end
|
|
6821
6856
|
|
|
6857
|
+
FileUtils.rm_rf(retired)
|
|
6858
|
+
File.rename(clacky_dir, retired)
|
|
6859
|
+
begin
|
|
6860
|
+
File.rename(staging, clacky_dir)
|
|
6861
|
+
rescue => e
|
|
6862
|
+
File.rename(retired, clacky_dir)
|
|
6863
|
+
raise e
|
|
6864
|
+
end
|
|
6865
|
+
|
|
6822
6866
|
json_response(res, 200, { ok: true })
|
|
6823
6867
|
|
|
6824
6868
|
Clacky::ThreadRegistry.spawn(name: "exec-restart") do
|
|
@@ -6834,10 +6878,33 @@ module Clacky
|
|
|
6834
6878
|
end
|
|
6835
6879
|
end
|
|
6836
6880
|
rescue => e
|
|
6881
|
+
FileUtils.rm_rf(staging) if staging && Dir.exist?(staging)
|
|
6837
6882
|
json_response(res, 500, { ok: false, error: e.message })
|
|
6838
6883
|
ensure
|
|
6839
6884
|
FileUtils.rm_f(tmp_archive) if tmp_archive
|
|
6840
|
-
|
|
6885
|
+
end
|
|
6886
|
+
|
|
6887
|
+
# A restore leaves the previous tree behind as .clacky-pre-restore-*, so
|
|
6888
|
+
# the swap stays reversible until the new worker is up. Keeping it any
|
|
6889
|
+
# longer just wastes gigabytes.
|
|
6890
|
+
private def sweep_pre_restore_dirs
|
|
6891
|
+
pattern = File.join(File.dirname(File.expand_path("~/.clacky")), ".clacky-pre-restore-*")
|
|
6892
|
+
dirs = Dir.glob(pattern)
|
|
6893
|
+
return if dirs.empty?
|
|
6894
|
+
|
|
6895
|
+
Clacky::ThreadRegistry.spawn(name: "sweep-pre-restore") do
|
|
6896
|
+
dirs.each do |dir|
|
|
6897
|
+
begin
|
|
6898
|
+
FileUtils.rm_rf(dir)
|
|
6899
|
+
rescue => e
|
|
6900
|
+
Clacky::Logger.warn("[HttpServer] could not remove #{dir}: #{e.class}: #{e.message}")
|
|
6901
|
+
end
|
|
6902
|
+
end
|
|
6903
|
+
end
|
|
6904
|
+
end
|
|
6905
|
+
|
|
6906
|
+
private def gzip_archive?(path)
|
|
6907
|
+
File.open(path, "rb") { |f| f.read(2) } == "\x1F\x8B".b
|
|
6841
6908
|
end
|
|
6842
6909
|
|
|
6843
6910
|
# POST /api/backup/open-folder — open the backup destination in Finder/Explorer
|
|
@@ -6900,6 +6967,7 @@ module Clacky
|
|
|
6900
6967
|
enable_idle_compression: @agent_config.enable_idle_compression,
|
|
6901
6968
|
enable_prompt_caching: @agent_config.enable_prompt_caching,
|
|
6902
6969
|
memory_update_enabled: @agent_config.memory_update_enabled,
|
|
6970
|
+
memory_inject_enabled: @agent_config.memory_inject_enabled,
|
|
6903
6971
|
proxy_url: @agent_config.proxy_url.to_s,
|
|
6904
6972
|
clacky_license_server: effective_clacky_license_server
|
|
6905
6973
|
})
|
|
@@ -6932,6 +7000,9 @@ module Clacky
|
|
|
6932
7000
|
if body.key?("memory_update_enabled")
|
|
6933
7001
|
@agent_config.memory_update_enabled = !!body["memory_update_enabled"]
|
|
6934
7002
|
end
|
|
7003
|
+
if body.key?("memory_inject_enabled")
|
|
7004
|
+
@agent_config.memory_inject_enabled = !!body["memory_inject_enabled"]
|
|
7005
|
+
end
|
|
6935
7006
|
if body.key?("proxy_url")
|
|
6936
7007
|
raw = body["proxy_url"].to_s.strip
|
|
6937
7008
|
if raw.empty?
|
|
@@ -8230,10 +8301,18 @@ module Clacky
|
|
|
8230
8301
|
msg = JSON.parse(raw)
|
|
8231
8302
|
type = msg["type"]
|
|
8232
8303
|
|
|
8304
|
+
# WS frames carry the language because the handshake cannot. Thread#[]
|
|
8305
|
+
# is fiber-local, which suffices because each connection owns a thread;
|
|
8306
|
+
# a frame without a usable `lang` keeps this connection's last value.
|
|
8307
|
+
# HTTP requests, by contrast, reset it per request.
|
|
8308
|
+
lang = Clacky::I18n.normalize(msg["lang"])
|
|
8309
|
+
Thread.current[:lang] = lang if lang
|
|
8310
|
+
|
|
8233
8311
|
case type
|
|
8234
8312
|
when "subscribe"
|
|
8235
8313
|
session_id = msg["session_id"]
|
|
8236
8314
|
if @registry.ensure(session_id)
|
|
8315
|
+
conn.stream_assistant_text = msg["stream"] == true
|
|
8237
8316
|
conn.session_id = session_id
|
|
8238
8317
|
subscribe(session_id, conn)
|
|
8239
8318
|
conn.send_json(type: "subscribed", session_id: session_id)
|
|
@@ -8280,7 +8359,6 @@ module Clacky
|
|
|
8280
8359
|
|
|
8281
8360
|
when "send_pending_input"
|
|
8282
8361
|
session_id = msg["session_id"] || conn.session_id
|
|
8283
|
-
Thread.current[:lang] = msg["lang"].to_s.strip.then { |l| l.empty? ? nil : l }
|
|
8284
8362
|
send_pending_input(session_id, msg["id"])
|
|
8285
8363
|
|
|
8286
8364
|
when "edit_message"
|
|
@@ -8289,7 +8367,6 @@ module Clacky
|
|
|
8289
8367
|
|
|
8290
8368
|
when "message"
|
|
8291
8369
|
session_id = msg["session_id"] || conn.session_id
|
|
8292
|
-
Thread.current[:lang] = msg["lang"].to_s.strip.then { |l| l.empty? ? nil : l }
|
|
8293
8370
|
# Merge legacy images array into files as { data_url:, name:, mime_type: } entries
|
|
8294
8371
|
raw_images = (msg["images"] || []).map do |data_url|
|
|
8295
8372
|
{ "data_url" => data_url, "name" => "image.jpg", "mime_type" => "image/jpeg" }
|
|
@@ -8350,7 +8427,11 @@ module Clacky
|
|
|
8350
8427
|
conn.send_json(type: "pong")
|
|
8351
8428
|
|
|
8352
8429
|
else
|
|
8353
|
-
|
|
8430
|
+
namespace = /\Aext\.([^.]+)\..+\z/.match(type.to_s)
|
|
8431
|
+
extension = namespace && Clacky::ApiExtension.registry[namespace[1]]
|
|
8432
|
+
unless extension && extension.on_ws_event(conn, msg)
|
|
8433
|
+
conn.send_json(type: "error", message: "Unknown message type: #{type}")
|
|
8434
|
+
end
|
|
8354
8435
|
end
|
|
8355
8436
|
rescue JSON::ParserError => e
|
|
8356
8437
|
conn.send_json(type: "error", message: "Invalid JSON: #{e.message}")
|
|
@@ -8362,6 +8443,13 @@ module Clacky
|
|
|
8362
8443
|
def on_ws_close(conn)
|
|
8363
8444
|
@ws_mutex.synchronize { @all_ws_conns.delete(conn) }
|
|
8364
8445
|
unsubscribe(conn)
|
|
8446
|
+
Clacky::ApiExtension.registry.each_value do |extension|
|
|
8447
|
+
begin
|
|
8448
|
+
extension.on_ws_close(conn)
|
|
8449
|
+
rescue StandardError => e
|
|
8450
|
+
Clacky::Logger.error("Extension WebSocket close error", ext: extension.ext_id, error: e)
|
|
8451
|
+
end
|
|
8452
|
+
end
|
|
8365
8453
|
end
|
|
8366
8454
|
|
|
8367
8455
|
# ── Session actions ───────────────────────────────────────────────────────
|
|
@@ -8466,11 +8554,11 @@ module Clacky
|
|
|
8466
8554
|
# confirmed skill name (matched against the authoritative skill registry).
|
|
8467
8555
|
# agent.run re-resolves internally for dispatch; the broadcast only needs
|
|
8468
8556
|
# the flag for the UI to highlight a valid /command. The display name is
|
|
8469
|
-
# resolved against the client's language
|
|
8470
|
-
#
|
|
8557
|
+
# resolved against the client's language, falling back to the process
|
|
8558
|
+
# environment, so a zh client gets the Chinese name.
|
|
8471
8559
|
skill_command = agent.parse_skill_command(content)
|
|
8472
8560
|
skill_command_display = if skill_command[:found] && skill_command[:skill]
|
|
8473
|
-
skill_command[:skill].display_name(
|
|
8561
|
+
skill_command[:skill].display_name(Clacky::I18n.locale)
|
|
8474
8562
|
end
|
|
8475
8563
|
# Pass the uploaded files through to the realtime broadcast so images and
|
|
8476
8564
|
# attachments render in the bubble immediately. agent.run later appends the
|
|
@@ -8731,7 +8819,7 @@ module Clacky
|
|
|
8731
8819
|
|
|
8732
8820
|
broadcast_session_update(session_id)
|
|
8733
8821
|
|
|
8734
|
-
locale =
|
|
8822
|
+
locale = Clacky::I18n.locale
|
|
8735
8823
|
thread = Clacky::ThreadRegistry.spawn(name: "agent-task:#{session_id}") do
|
|
8736
8824
|
Thread.current[:lang] = locale
|
|
8737
8825
|
Thread.current[:task_epoch] = epoch
|
|
@@ -8863,6 +8951,9 @@ module Clacky
|
|
|
8863
8951
|
dead << conn
|
|
8864
8952
|
next
|
|
8865
8953
|
end
|
|
8954
|
+
if ["assistant_message_delta", "assistant_message_stream_end"].include?(event[:type])
|
|
8955
|
+
next unless conn.respond_to?(:stream_assistant_text) && conn.stream_assistant_text
|
|
8956
|
+
end
|
|
8866
8957
|
dead << conn unless conn.send_json(event)
|
|
8867
8958
|
end
|
|
8868
8959
|
return if dead.empty?
|
|
@@ -9131,7 +9222,7 @@ module Clacky
|
|
|
9131
9222
|
# with an explicit deadline using IO.select + write_nonblock and declare
|
|
9132
9223
|
# the connection dead on timeout.
|
|
9133
9224
|
class WebSocketConnection
|
|
9134
|
-
attr_accessor :session_id
|
|
9225
|
+
attr_accessor :session_id, :stream_assistant_text
|
|
9135
9226
|
|
|
9136
9227
|
# Maximum time a single send_raw call is allowed to spend writing.
|
|
9137
9228
|
# 5 seconds is generous for healthy LAN/Internet clients and short
|