openclacky 1.5.22 → 1.5.23
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 +58 -0
- data/docs/secretary-prototype.md +286 -0
- data/lib/clacky/agent/session_serializer.rb +15 -11
- data/lib/clacky/agent.rb +58 -15
- data/lib/clacky/agent_config.rb +3 -1
- data/lib/clacky/agent_profile.rb +33 -6
- data/lib/clacky/billing/platform_billing.rb +6 -0
- data/lib/clacky/default_extensions/advisor/README.md +25 -0
- data/lib/clacky/default_extensions/advisor/README.zh.md +25 -0
- data/lib/clacky/default_extensions/advisor/ext.yml +1 -1
- data/lib/clacky/default_extensions/advisor/icon.svg +14 -0
- data/lib/clacky/default_extensions/coding/ext.yml +0 -1
- data/lib/clacky/default_extensions/computer-use/README.md +48 -0
- data/lib/clacky/default_extensions/computer-use/README.zh.md +48 -0
- data/lib/clacky/default_extensions/computer-use/ext.yml +1 -1
- data/lib/clacky/default_extensions/computer-use/icon.svg +16 -0
- data/lib/clacky/default_extensions/ext-studio/ext.yml +0 -1
- data/lib/clacky/default_extensions/general/ext.yml +0 -1
- data/lib/clacky/default_extensions/git/README.md +41 -0
- data/lib/clacky/default_extensions/git/README.zh.md +41 -0
- data/lib/clacky/default_extensions/git/ext.yml +1 -1
- data/lib/clacky/default_extensions/git/icon.svg +15 -0
- data/lib/clacky/default_extensions/git/panels/git/view.js +71 -23
- data/lib/clacky/default_extensions/meeting/README.md +46 -0
- data/lib/clacky/default_extensions/meeting/README.zh.md +47 -0
- data/lib/clacky/default_extensions/meeting/ext.yml +1 -1
- data/lib/clacky/default_extensions/meeting/icon.svg +13 -0
- data/lib/clacky/default_extensions/preview/README.md +29 -0
- data/lib/clacky/default_extensions/preview/README.zh.md +29 -0
- data/lib/clacky/default_extensions/preview/ext.yml +1 -1
- data/lib/clacky/default_extensions/preview/icon.svg +13 -0
- data/lib/clacky/default_extensions/secretary/agents/secretary.md +41 -0
- data/lib/clacky/default_extensions/secretary/agents/worker.md +35 -0
- data/lib/clacky/default_extensions/secretary/api/handler.rb +8 -0
- data/lib/clacky/default_extensions/secretary/ext.yml +39 -0
- data/lib/clacky/default_extensions/secretary/panels/work/view.js +344 -0
- data/lib/clacky/default_extensions/secretary/tools/delegate_work.rb +37 -0
- data/lib/clacky/default_extensions/secretary/tools/manage_work.rb +22 -0
- data/lib/clacky/default_extensions/secretary/tools/yield_work.rb +35 -0
- data/lib/clacky/default_extensions/time_machine/README.md +32 -0
- data/lib/clacky/default_extensions/time_machine/README.zh.md +32 -0
- data/lib/clacky/default_extensions/time_machine/ext.yml +1 -1
- data/lib/clacky/default_extensions/time_machine/icon.svg +14 -0
- data/lib/clacky/default_skills/channel-manager/SKILL.md +79 -8
- data/lib/clacky/default_skills/channel-manager/discord_setup.rb +5 -1
- data/lib/clacky/default_skills/channel-manager/weixin_setup.rb +8 -1
- data/lib/clacky/default_skills/cron-task-creator/SKILL.md +183 -206
- data/lib/clacky/default_skills/cron-task-creator/evals/evals.json +45 -0
- data/lib/clacky/default_skills/media-gen/SKILL.md +17 -8
- data/lib/clacky/extension/api_extension.rb +10 -2
- data/lib/clacky/extension/loader.rb +2 -0
- data/lib/clacky/extension/verifier.rb +2 -2
- data/lib/clacky/locales/en.rb +14 -0
- data/lib/clacky/locales/zh.rb +14 -0
- data/lib/clacky/prompts/base.md +1 -1
- data/lib/clacky/providers.rb +25 -14
- data/lib/clacky/server/channel/adapters/base.rb +18 -0
- data/lib/clacky/server/channel/adapters/feishu/message_parser.rb +1 -0
- data/lib/clacky/server/channel/adapters/telegram/adapter.rb +1 -0
- data/lib/clacky/server/channel/channel_config.rb +185 -48
- data/lib/clacky/server/channel/channel_manager.rb +179 -66
- data/lib/clacky/server/git_panel.rb +2 -1
- data/lib/clacky/server/http_server.rb +385 -134
- data/lib/clacky/server/scheduler.rb +166 -85
- data/lib/clacky/session_manager.rb +4 -1
- data/lib/clacky/tools/base.rb +7 -1
- data/lib/clacky/utils/file_processor.rb +20 -10
- data/lib/clacky/version.rb +1 -1
- data/lib/clacky/web/app.css +401 -46
- data/lib/clacky/web/app.js +85 -3
- data/lib/clacky/web/components/code-editor.js +73 -4
- data/lib/clacky/web/components/model-picker.js +1 -1
- data/lib/clacky/web/components/notify.js +37 -10
- data/lib/clacky/web/components/onboard.js +1 -1
- data/lib/clacky/web/features/channels/store.js +19 -4
- data/lib/clacky/web/features/channels/view.js +87 -26
- data/lib/clacky/web/features/extensions/view.js +15 -8
- data/lib/clacky/web/features/new-session/view.js +23 -20
- data/lib/clacky/web/features/tasks/store.js +5 -3
- data/lib/clacky/web/features/tasks/view.js +98 -26
- data/lib/clacky/web/features/workspace/view.js +193 -57
- data/lib/clacky/web/i18n.js +128 -12
- data/lib/clacky/web/index.html +25 -10
- data/lib/clacky/web/sessions.js +82 -41
- data/lib/clacky/web/settings.js +215 -45
- data/lib/clacky/web/utils.js +217 -0
- data/lib/clacky/web/ws-dispatcher.js +7 -3
- data/lib/clacky/work/service.rb +542 -0
- data/lib/clacky/work/store.rb +57 -0
- metadata +30 -1
|
@@ -24,6 +24,7 @@ require_relative "preview"
|
|
|
24
24
|
require_relative "web_ui_controller"
|
|
25
25
|
require_relative "model_prices"
|
|
26
26
|
require_relative "scheduler"
|
|
27
|
+
require_relative "../work/service"
|
|
27
28
|
require_relative "../brand_config"
|
|
28
29
|
require_relative "channel"
|
|
29
30
|
require_relative "../banner"
|
|
@@ -269,14 +270,24 @@ module Clacky
|
|
|
269
270
|
@ws_clients = {} # session_id => [WebSocketConnection, ...]
|
|
270
271
|
@all_ws_conns = [] # every connected WS client, regardless of session subscription
|
|
271
272
|
@ws_mutex = Mutex.new
|
|
273
|
+
@task_admission_mutex = Mutex.new
|
|
272
274
|
# Version cache: { latest: "x.y.z", checked_at: Time }
|
|
273
275
|
@version_cache = nil
|
|
274
276
|
@version_mutex = Mutex.new
|
|
275
277
|
@scheduler = Scheduler.new(
|
|
276
278
|
session_registry: @registry,
|
|
277
279
|
session_builder: method(:build_session),
|
|
278
|
-
task_runner: method(:run_agent_task)
|
|
280
|
+
task_runner: method(:run_agent_task),
|
|
281
|
+
scheduled_dispatcher: ->(plan, prompt, due) { @work_service.dispatch_schedule(plan, prompt, due) }
|
|
279
282
|
)
|
|
283
|
+
@work_service = Clacky::Work::Service.new(
|
|
284
|
+
store: Clacky::Work::Store.new(File.join(sessions_dir || Clacky::SessionManager::SESSIONS_DIR, "work", "state.json")),
|
|
285
|
+
registry: @registry, build_session: method(:build_session), scheduler: @scheduler,
|
|
286
|
+
submit_task: method(:run_session_task), interrupt: method(:interrupt_session),
|
|
287
|
+
persist: ->(agent) { @session_manager.save(agent.to_session_data) },
|
|
288
|
+
publish: ->(sid, type) { broadcast(sid, { type: type, session_id: sid }) }
|
|
289
|
+
)
|
|
290
|
+
@session_manager.protected_session_ids = -> { @work_service.protected_session_ids }
|
|
280
291
|
@channel_manager = Clacky::Channel::ChannelManager.new(
|
|
281
292
|
session_registry: @registry,
|
|
282
293
|
session_builder: method(:build_session),
|
|
@@ -285,6 +296,7 @@ module Clacky
|
|
|
285
296
|
# No updated_at: clearing a stale channel_info must not bump the session's
|
|
286
297
|
# position in the newest-first list.
|
|
287
298
|
persist_session: ->(agent) { @session_manager.save(agent.to_session_data) },
|
|
299
|
+
work_service: @work_service,
|
|
288
300
|
channel_config: Clacky::ChannelConfig.load
|
|
289
301
|
)
|
|
290
302
|
@browser_manager = Clacky::BrowserManager.instance
|
|
@@ -309,6 +321,8 @@ module Clacky
|
|
|
309
321
|
end
|
|
310
322
|
end
|
|
311
323
|
|
|
324
|
+
attr_reader :work_service
|
|
325
|
+
|
|
312
326
|
def start
|
|
313
327
|
@start_time = Time.now
|
|
314
328
|
# One-time migration: move legacy trash contents into file-trash/ subdirectory.
|
|
@@ -362,6 +376,7 @@ module Clacky
|
|
|
362
376
|
next if shutdown_once
|
|
363
377
|
shutdown_once = true
|
|
364
378
|
@draining = true
|
|
379
|
+
@work_service.stop
|
|
365
380
|
# Flip the process-wide cooperative shutdown flag FIRST so agent
|
|
366
381
|
# threads poll it at safe points (stream chunk callbacks, retry
|
|
367
382
|
# loops, main loop) and exit cleanly instead of spinning.
|
|
@@ -556,6 +571,7 @@ module Clacky
|
|
|
556
571
|
|
|
557
572
|
# Start the background scheduler
|
|
558
573
|
@scheduler.start
|
|
574
|
+
@work_service.start
|
|
559
575
|
puts " Scheduler: #{@scheduler.schedules.size} task(s) loaded"
|
|
560
576
|
|
|
561
577
|
# Reclaim orphaned Time Machine snapshots (sessions deleted earlier
|
|
@@ -577,6 +593,7 @@ module Clacky
|
|
|
577
593
|
|
|
578
594
|
server.start unless @draining
|
|
579
595
|
ensure
|
|
596
|
+
@work_service&.stop
|
|
580
597
|
# close only this process's FD, never shutdown the shared kernel socket.
|
|
581
598
|
@inherited_socket.close if @inherited_socket && !@inherited_socket.closed?
|
|
582
599
|
end
|
|
@@ -797,14 +814,19 @@ module Clacky
|
|
|
797
814
|
api_group_history(chat_id, res)
|
|
798
815
|
elsif method == "GET" && path.match?(%r{^/api/channels/[^/]+/users$})
|
|
799
816
|
platform = path.sub("/api/channels/", "").sub("/users", "")
|
|
800
|
-
api_list_channel_users(platform, res)
|
|
817
|
+
api_list_channel_users(platform, req, res)
|
|
801
818
|
elsif method == "POST" && path.match?(%r{^/api/channels/qq/qr/start$})
|
|
802
|
-
api_qq_qr_start(res)
|
|
819
|
+
api_qq_qr_start(req, res)
|
|
803
820
|
elsif method == "POST" && path.match?(%r{^/api/channels/qq/qr/poll$})
|
|
804
821
|
api_qq_qr_poll(req, res)
|
|
805
822
|
elsif method == "POST" && path.match?(%r{^/api/channels/[^/]+/test$})
|
|
806
823
|
platform = path.sub("/api/channels/", "").sub("/test", "")
|
|
807
824
|
api_test_channel(platform, req, res)
|
|
825
|
+
elsif method == "DELETE" && path.match?(%r{^/api/channels/[^/]+/instances/[^/]+$})
|
|
826
|
+
rest = path.sub("/api/channels/", "")
|
|
827
|
+
platform = rest.split("/").first
|
|
828
|
+
instance_id = URI.decode_www_form_component(rest.split("/").last)
|
|
829
|
+
api_delete_channel(platform, res, instance_id)
|
|
808
830
|
elsif method == "PATCH" && path == "/api/channels/status_messages"
|
|
809
831
|
api_channel_status_messages(req, res)
|
|
810
832
|
elsif method == "PATCH" && path == "/api/channels/process_messages"
|
|
@@ -1443,6 +1465,12 @@ module Clacky
|
|
|
1443
1465
|
agent_data = agent_profile_data
|
|
1444
1466
|
agent_data.each do |name, data|
|
|
1445
1467
|
ext_id = data["_ext_id"]
|
|
1468
|
+
# Base agent panels retain their extension-qualified IDs.
|
|
1469
|
+
base_unit = ext_result&.agents&.find { |unit| unit.id == data["base_agent"] }
|
|
1470
|
+
Array(base_unit&.spec&.dig("panels")).each do |panel|
|
|
1471
|
+
pid = panel.to_s
|
|
1472
|
+
add.call(pid.include?("/") ? pid : "#{base_unit.ext_id}/#{pid}", name)
|
|
1473
|
+
end
|
|
1446
1474
|
Array(data["panels"]).each do |panel|
|
|
1447
1475
|
pid = panel.to_s
|
|
1448
1476
|
full = pid.include?("/") ? pid : (ext_id ? "#{ext_id}/#{pid}" : pid)
|
|
@@ -1461,7 +1489,9 @@ module Clacky
|
|
|
1461
1489
|
if attach.include?("*")
|
|
1462
1490
|
all_agent_ids.each { |a| add.call(full_id, a) }
|
|
1463
1491
|
else
|
|
1464
|
-
|
|
1492
|
+
all_agent_ids.each do |name|
|
|
1493
|
+
add.call(full_id, name) if attach.include?(name) || attach.include?(agent_data.dig(name, "base_agent"))
|
|
1494
|
+
end
|
|
1465
1495
|
end
|
|
1466
1496
|
end
|
|
1467
1497
|
result
|
|
@@ -1475,6 +1505,7 @@ module Clacky
|
|
|
1475
1505
|
Array(Clacky::ExtensionLoader.last_result&.agents).each do |unit|
|
|
1476
1506
|
data[unit.id] = {
|
|
1477
1507
|
"name" => unit.id,
|
|
1508
|
+
"base_agent" => unit.spec["base_agent"],
|
|
1478
1509
|
"description" => unit.spec["description"],
|
|
1479
1510
|
"panels" => unit.spec["panels"],
|
|
1480
1511
|
"skills" => unit.spec["skills"],
|
|
@@ -1572,19 +1603,31 @@ module Clacky
|
|
|
1572
1603
|
|
|
1573
1604
|
# Serve a file from a resolved extension container: /ext_ui/<ext_id>/<rel>.
|
|
1574
1605
|
# The container dir is looked up via the loader (it may live in any layer),
|
|
1575
|
-
# then strict path containment prevents escaping it.
|
|
1606
|
+
# then strict path containment prevents escaping it. Source files and the
|
|
1607
|
+
# icon set are allowed; everything else is refused.
|
|
1608
|
+
EXT_UI_TYPES = {
|
|
1609
|
+
".js" => "application/javascript",
|
|
1610
|
+
".css" => "text/css",
|
|
1611
|
+
".png" => "image/png",
|
|
1612
|
+
".jpg" => "image/jpeg",
|
|
1613
|
+
".jpeg" => "image/jpeg",
|
|
1614
|
+
".webp" => "image/webp",
|
|
1615
|
+
".svg" => "image/svg+xml",
|
|
1616
|
+
".gif" => "image/gif",
|
|
1617
|
+
}.freeze
|
|
1618
|
+
|
|
1576
1619
|
private def serve_ext_ui(req, res)
|
|
1577
1620
|
rel = req.path.delete_prefix("/ext_ui/")
|
|
1578
1621
|
ext_id, _, sub = rel.partition("/")
|
|
1579
1622
|
|
|
1580
|
-
|
|
1581
|
-
unless
|
|
1623
|
+
container = Clacky::ExtensionLoader.load_all.containers[ext_id]
|
|
1624
|
+
unless container
|
|
1582
1625
|
res.status = 404
|
|
1583
1626
|
res.body = "not found"
|
|
1584
1627
|
return
|
|
1585
1628
|
end
|
|
1586
1629
|
|
|
1587
|
-
root = File.expand_path(
|
|
1630
|
+
root = File.expand_path(container[:dir].to_s)
|
|
1588
1631
|
abs = File.expand_path(File.join(root, sub))
|
|
1589
1632
|
unless abs.start_with?(root + File::SEPARATOR) && File.file?(abs)
|
|
1590
1633
|
res.status = 404
|
|
@@ -1592,8 +1635,7 @@ module Clacky
|
|
|
1592
1635
|
return
|
|
1593
1636
|
end
|
|
1594
1637
|
|
|
1595
|
-
|
|
1596
|
-
ctype = { ".js" => "application/javascript", ".css" => "text/css" }[ext]
|
|
1638
|
+
ctype = EXT_UI_TYPES[File.extname(abs).downcase]
|
|
1597
1639
|
unless ctype
|
|
1598
1640
|
res.status = 415
|
|
1599
1641
|
res.body = "unsupported media type"
|
|
@@ -1604,7 +1646,7 @@ module Clacky
|
|
|
1604
1646
|
res["Content-Type"] = ctype
|
|
1605
1647
|
res["Cache-Control"] = "no-store"
|
|
1606
1648
|
res["Pragma"] = "no-cache"
|
|
1607
|
-
res.body = File.read(abs)
|
|
1649
|
+
res.body = ctype.start_with?("image/") ? File.binread(abs) : File.read(abs)
|
|
1608
1650
|
end
|
|
1609
1651
|
|
|
1610
1652
|
# Serve agents/<name>/webui/<file> from built-in or user agent dir.
|
|
@@ -2928,6 +2970,7 @@ module Clacky
|
|
|
2928
2970
|
"description" => local_desc,
|
|
2929
2971
|
"description_zh" => local_desc_zh,
|
|
2930
2972
|
"author" => local_author,
|
|
2973
|
+
"icon_url" => extension_icon_url(container),
|
|
2931
2974
|
"units" => local_units,
|
|
2932
2975
|
"layer" => "builtin",
|
|
2933
2976
|
"installed" => true,
|
|
@@ -2984,7 +3027,7 @@ module Clacky
|
|
|
2984
3027
|
"description" => (label && label["description"]) || container.dig(:raw, "description").to_s,
|
|
2985
3028
|
"description_zh" => (label && label["description_zh"]) || container.dig(:raw, "description_zh").to_s,
|
|
2986
3029
|
"author" => (label && label["author"]) || container[:author].to_s,
|
|
2987
|
-
"icon_url" => market&.dig("icon_url"),
|
|
3030
|
+
"icon_url" => builtin ? extension_icon_url(container) : (market&.dig("icon_url") || extension_icon_url(container)),
|
|
2988
3031
|
"units" => (label && label["units"]) || units_from_container(container),
|
|
2989
3032
|
"homepage" => (label && label["homepage"]) || container[:homepage].to_s,
|
|
2990
3033
|
"origin" => (label && (label["origin"] || container[:origin])) || container[:origin],
|
|
@@ -3054,9 +3097,11 @@ module Clacky
|
|
|
3054
3097
|
|
|
3055
3098
|
# GET /api/store/extension?id=<slug-or-id>[&source=brand]
|
|
3056
3099
|
#
|
|
3057
|
-
# Detail for a single extension.
|
|
3058
|
-
#
|
|
3059
|
-
#
|
|
3100
|
+
# Detail for a single extension. A builtin ships with the gem and is not a
|
|
3101
|
+
# marketplace entry — its slug can collide with an unrelated published
|
|
3102
|
+
# extension, so it is answered from the local manifest without asking the
|
|
3103
|
+
# store. Pass source=brand to query brand-private extensions via the
|
|
3104
|
+
# license-gated API; omit (or any other value) for the public catalog.
|
|
3060
3105
|
def api_store_extension_detail(req, res)
|
|
3061
3106
|
id = req.query["id"].to_s
|
|
3062
3107
|
if id.strip.empty?
|
|
@@ -3064,6 +3109,12 @@ module Clacky
|
|
|
3064
3109
|
return
|
|
3065
3110
|
end
|
|
3066
3111
|
|
|
3112
|
+
container = extension_container(id)
|
|
3113
|
+
if container && container[:layer] == :builtin
|
|
3114
|
+
json_response(res, 200, { ok: true, extension: local_extension_payload(container) })
|
|
3115
|
+
return
|
|
3116
|
+
end
|
|
3117
|
+
|
|
3067
3118
|
brand = Clacky::BrandConfig.load
|
|
3068
3119
|
result = if req.query["source"] == "brand"
|
|
3069
3120
|
brand.brand_extension_detail!(id)
|
|
@@ -3081,44 +3132,85 @@ module Clacky
|
|
|
3081
3132
|
"installed_version" => container&.dig(:version),
|
|
3082
3133
|
"removable" => container && container[:layer] == :installed,
|
|
3083
3134
|
"disabled" => container ? container[:disabled] == true : false,
|
|
3135
|
+
"icon_url" => ext["icon_url"] || (container && extension_icon_url(container)),
|
|
3084
3136
|
)
|
|
3085
3137
|
json_response(res, 200, { ok: true, extension: ext })
|
|
3138
|
+
elsif container && container[:layer] == :installed
|
|
3139
|
+
market = fetch_batch_market_data([id])[id]
|
|
3140
|
+
json_response(res, 200, { ok: true, extension: local_extension_payload(container, market: market) })
|
|
3086
3141
|
else
|
|
3087
|
-
|
|
3088
|
-
if container && container[:layer] == :installed
|
|
3089
|
-
market = fetch_batch_market_data([id])[id]
|
|
3090
|
-
# Fall back to local ext.yml for self-authored extensions.
|
|
3091
|
-
local_name = container[:name].to_s.then { |n| n.empty? ? id : n }
|
|
3092
|
-
local_desc = container.dig(:raw, "description").to_s
|
|
3093
|
-
local_author = container[:author].to_s
|
|
3094
|
-
local_units = units_from_container(container)
|
|
3095
|
-
ext = {
|
|
3096
|
-
"id" => id,
|
|
3097
|
-
"name" => market ? (market["name"] || id) : local_name,
|
|
3098
|
-
"name_zh" => market&.dig("name_zh"),
|
|
3099
|
-
"name_en" => market&.dig("name_en"),
|
|
3100
|
-
"slug" => id,
|
|
3101
|
-
"version" => market ? (market["version"] || container[:version]) : container[:version],
|
|
3102
|
-
"installed_version" => container[:version],
|
|
3103
|
-
"description" => market ? market["description"] : local_desc,
|
|
3104
|
-
"author" => market ? market["author"] : local_author,
|
|
3105
|
-
"icon_url" => market&.dig("icon_url"),
|
|
3106
|
-
"units" => market ? market["units"] : local_units,
|
|
3107
|
-
"homepage" => market ? (market["homepage"] || "") : container[:homepage].to_s,
|
|
3108
|
-
"origin" => market ? (market["origin"] || container[:origin]) : container[:origin],
|
|
3109
|
-
"hub_active" => market&.dig("hub_active"),
|
|
3110
|
-
"unlisted" => market.nil? || market["unlisted"] == true,
|
|
3111
|
-
"installed" => true,
|
|
3112
|
-
"removable" => true,
|
|
3113
|
-
"disabled" => container[:disabled] == true,
|
|
3114
|
-
}
|
|
3115
|
-
json_response(res, 200, { ok: true, extension: ext })
|
|
3116
|
-
else
|
|
3117
|
-
json_response(res, 404, { ok: false, error: result[:error] || "Not found" })
|
|
3118
|
-
end
|
|
3142
|
+
json_response(res, 404, { ok: false, error: result[:error] || "Not found" })
|
|
3119
3143
|
end
|
|
3120
3144
|
end
|
|
3121
3145
|
|
|
3146
|
+
# Local manifest view of an installed extension, used when the marketplace
|
|
3147
|
+
# has nothing to say about it (or must not be asked at all).
|
|
3148
|
+
private def local_extension_payload(container, market: nil)
|
|
3149
|
+
builtin = container[:layer] == :builtin
|
|
3150
|
+
id = container[:ext_id].to_s
|
|
3151
|
+
# A builtin's manifest is the source of truth — plenty ship with the gem
|
|
3152
|
+
# and were never published.
|
|
3153
|
+
label = builtin ? nil : market
|
|
3154
|
+
local_name = container[:name].to_s.then { |n| n.empty? ? id : n }
|
|
3155
|
+
local_desc = container.dig(:raw, "description").to_s
|
|
3156
|
+
|
|
3157
|
+
{
|
|
3158
|
+
"id" => id,
|
|
3159
|
+
"name" => label ? (label["name"] || id) : local_name,
|
|
3160
|
+
"name_zh" => (label && label["name_zh"]) || container.dig(:raw, "name_zh"),
|
|
3161
|
+
"name_en" => (label && label["name_en"]) || container.dig(:raw, "name_en"),
|
|
3162
|
+
"display_name" => (label && label["display_name"]) || container.dig(:raw, "display_name") || local_name,
|
|
3163
|
+
"display_name_zh" => (label && label["display_name_zh"]) || container.dig(:raw, "display_name_zh"),
|
|
3164
|
+
"slug" => id,
|
|
3165
|
+
"version" => (label && label["version"]) || container[:version],
|
|
3166
|
+
"installed_version" => container[:version],
|
|
3167
|
+
"description" => (label && label["description"]) || local_desc,
|
|
3168
|
+
"description_zh" => (label && label["description_zh"]) || container.dig(:raw, "description_zh").to_s,
|
|
3169
|
+
"author" => (label && label["author"]) || container[:author].to_s,
|
|
3170
|
+
"icon_url" => builtin ? extension_icon_url(container) : ((label && label["icon_url"]) || extension_icon_url(container)),
|
|
3171
|
+
"units" => (label && label["units"]) || units_from_container(container),
|
|
3172
|
+
"homepage" => (label && label["homepage"]) || container[:homepage].to_s,
|
|
3173
|
+
"origin" => (label && label["origin"]) || container[:origin],
|
|
3174
|
+
"hub_active" => label && label["hub_active"],
|
|
3175
|
+
"download_count" => label ? label["download_count"].to_i : 0,
|
|
3176
|
+
"unlisted" => builtin ? false : (market.nil? || market["unlisted"] == true),
|
|
3177
|
+
"layer" => container[:layer].to_s,
|
|
3178
|
+
"installed" => true,
|
|
3179
|
+
"removable" => container[:layer] == :installed,
|
|
3180
|
+
"disabled" => container[:disabled] == true,
|
|
3181
|
+
"contributes" => container[:contributes] || {},
|
|
3182
|
+
"readme" => (label && label["readme"]).to_s.then { |r| r.strip.empty? ? local_extension_readme(container) : r },
|
|
3183
|
+
}
|
|
3184
|
+
end
|
|
3185
|
+
|
|
3186
|
+
# README.zh.md wins for Chinese clients; README.md is the fallback.
|
|
3187
|
+
private def local_extension_readme(container)
|
|
3188
|
+
dir = container[:dir].to_s
|
|
3189
|
+
return nil if dir.empty?
|
|
3190
|
+
|
|
3191
|
+
names = Thread.current[:lang].to_s.start_with?("zh") ? %w[README.zh.md README.md] : %w[README.md]
|
|
3192
|
+
path = names.map { |n| File.join(dir, n) }.find { |p| File.file?(p) }
|
|
3193
|
+
path && File.read(path, encoding: "UTF-8")
|
|
3194
|
+
end
|
|
3195
|
+
|
|
3196
|
+
# Public URL for an extension's local icon file, or nil when the manifest
|
|
3197
|
+
# names none (or the file is missing / outside the container). Served by
|
|
3198
|
+
# /ext_ui, so it works for every layer and while the extension is off.
|
|
3199
|
+
private def extension_icon_url(container)
|
|
3200
|
+
rel = container.dig(:raw, "icon").to_s.strip
|
|
3201
|
+
return nil if rel.empty?
|
|
3202
|
+
|
|
3203
|
+
ext_id = container[:ext_id].to_s
|
|
3204
|
+
dir = container[:dir].to_s
|
|
3205
|
+
return nil if ext_id.empty? || dir.empty?
|
|
3206
|
+
|
|
3207
|
+
root = File.expand_path(dir)
|
|
3208
|
+
abs = File.expand_path(File.join(root, rel))
|
|
3209
|
+
return nil unless abs.start_with?(root + File::SEPARATOR) && File.file?(abs)
|
|
3210
|
+
|
|
3211
|
+
"/ext_ui/#{ext_id}/#{rel}"
|
|
3212
|
+
end
|
|
3213
|
+
|
|
3122
3214
|
# Locate an installed container by slug/id (nil when not installed).
|
|
3123
3215
|
def extension_container(slug)
|
|
3124
3216
|
return nil if slug.to_s.strip.empty?
|
|
@@ -4213,18 +4305,31 @@ module Clacky
|
|
|
4213
4305
|
end
|
|
4214
4306
|
|
|
4215
4307
|
def api_list_channels(res)
|
|
4216
|
-
config
|
|
4217
|
-
running
|
|
4308
|
+
config = Clacky::ChannelConfig.load
|
|
4309
|
+
running = @channel_manager.running_instances
|
|
4218
4310
|
|
|
4219
4311
|
platforms = Clacky::Channel::Adapters.all.map do |klass|
|
|
4220
|
-
platform
|
|
4221
|
-
|
|
4312
|
+
platform = klass.platform_id
|
|
4313
|
+
instances = config.instances(platform).map do |inst|
|
|
4314
|
+
{
|
|
4315
|
+
id: inst["id"],
|
|
4316
|
+
name: inst["name"].to_s,
|
|
4317
|
+
enabled: !!inst["enabled"],
|
|
4318
|
+
running: running.any? { |r| r[:platform] == platform && r[:instance_id] == inst["id"] },
|
|
4319
|
+
has_config: !config.platform_config(platform, inst["id"]).nil?
|
|
4320
|
+
}.merge(platform_safe_fields(platform, config, inst["id"]))
|
|
4321
|
+
end
|
|
4322
|
+
|
|
4323
|
+
primary = instances.first || { id: Clacky::ChannelConfig::DEFAULT_INSTANCE_ID, name: "", enabled: false, running: false, has_config: false }
|
|
4222
4324
|
{
|
|
4223
|
-
platform:
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4325
|
+
platform: platform,
|
|
4326
|
+
id: primary[:id],
|
|
4327
|
+
name: primary[:name],
|
|
4328
|
+
enabled: instances.any? { |inst| inst[:enabled] },
|
|
4329
|
+
running: running.any? { |r| r[:platform] == platform },
|
|
4330
|
+
has_config: !instances.empty?,
|
|
4331
|
+
instances: instances
|
|
4332
|
+
}.merge(platform_safe_fields(platform, config, primary[:id]))
|
|
4228
4333
|
end
|
|
4229
4334
|
|
|
4230
4335
|
json_response(res, 200, { channels: platforms, status_messages: config.status_messages_enabled?, process_messages: config.process_messages_enabled?, progress_cards: config.progress_cards_enabled? })
|
|
@@ -4519,6 +4624,7 @@ module Clacky
|
|
|
4519
4624
|
platform = platform.to_sym
|
|
4520
4625
|
body = parse_json_body(req)
|
|
4521
4626
|
message = body["message"].to_s.strip
|
|
4627
|
+
instance_id = body["instance_id"].to_s.strip
|
|
4522
4628
|
|
|
4523
4629
|
if message.empty?
|
|
4524
4630
|
json_response(res, 400, { ok: false, error: "message is required" })
|
|
@@ -4529,7 +4635,7 @@ module Clacky
|
|
|
4529
4635
|
user_id = body["user_id"].to_s.strip
|
|
4530
4636
|
if user_id.empty?
|
|
4531
4637
|
# Default to the most-recently active user for this platform
|
|
4532
|
-
known = @channel_manager.known_users(platform)
|
|
4638
|
+
known = @channel_manager.known_users(platform, instance_id)
|
|
4533
4639
|
if known.empty?
|
|
4534
4640
|
json_response(res, 503, {
|
|
4535
4641
|
ok: false,
|
|
@@ -4540,21 +4646,21 @@ module Clacky
|
|
|
4540
4646
|
user_id = known.last
|
|
4541
4647
|
end
|
|
4542
4648
|
|
|
4543
|
-
result = @channel_manager.send_to_user(platform, user_id, message)
|
|
4649
|
+
result = @channel_manager.send_to_user(platform, user_id, message, instance_id)
|
|
4544
4650
|
if result.nil?
|
|
4545
4651
|
json_response(res, 400, {
|
|
4546
4652
|
ok: false,
|
|
4547
4653
|
error: "Failed to send message. The :#{platform} adapter may not be running, or no context_token is available for user #{user_id}."
|
|
4548
4654
|
})
|
|
4549
4655
|
else
|
|
4550
|
-
json_response(res, 200, { ok: true, platform: platform, user_id: user_id })
|
|
4656
|
+
json_response(res, 200, { ok: true, platform: platform, instance_id: instance_id, user_id: user_id })
|
|
4551
4657
|
end
|
|
4552
4658
|
rescue StandardError => e
|
|
4553
4659
|
json_response(res, 500, { ok: false, error: e.message })
|
|
4554
4660
|
end
|
|
4555
4661
|
|
|
4556
|
-
# GET /api/channels/:platform/users
|
|
4557
|
-
# Returns the list of known user IDs for the given platform.
|
|
4662
|
+
# GET /api/channels/:platform/users?instance_id=...
|
|
4663
|
+
# Returns the list of known user IDs for the given platform (or instance).
|
|
4558
4664
|
# These are users who have sent at least one message to the bot in this server session.
|
|
4559
4665
|
#
|
|
4560
4666
|
# For Weixin: returns users with a cached context_token (required for proactive messaging).
|
|
@@ -4562,10 +4668,11 @@ module Clacky
|
|
|
4562
4668
|
#
|
|
4563
4669
|
# Response:
|
|
4564
4670
|
# 200 { users: ["uid1", "uid2", ...] }
|
|
4565
|
-
def api_list_channel_users(platform, res)
|
|
4566
|
-
platform
|
|
4567
|
-
|
|
4568
|
-
|
|
4671
|
+
def api_list_channel_users(platform, req, res)
|
|
4672
|
+
platform = platform.to_sym
|
|
4673
|
+
instance_id = req.query["instance_id"].to_s.strip
|
|
4674
|
+
users = @channel_manager.known_users(platform, instance_id)
|
|
4675
|
+
json_response(res, 200, { platform: platform, instance_id: instance_id, users: users })
|
|
4569
4676
|
rescue StandardError => e
|
|
4570
4677
|
json_response(res, 500, { ok: false, error: e.message })
|
|
4571
4678
|
end
|
|
@@ -4834,13 +4941,24 @@ module Clacky
|
|
|
4834
4941
|
|
|
4835
4942
|
# POST /api/channels/:platform
|
|
4836
4943
|
# Body: { fields... } (platform-specific credential fields)
|
|
4944
|
+
# Optional: { instance_id, name } to target an additional instance of the
|
|
4945
|
+
# same platform. Without instance_id the default (first) instance is
|
|
4946
|
+
# updated, which keeps older clients and skill curls working unchanged.
|
|
4837
4947
|
# Saves credentials and optionally (re)starts the adapter.
|
|
4838
4948
|
def api_save_channel(platform, req, res)
|
|
4839
4949
|
platform = platform.to_sym
|
|
4840
4950
|
body = parse_json_body(req)
|
|
4841
4951
|
config = Clacky::ChannelConfig.load
|
|
4842
4952
|
|
|
4843
|
-
|
|
4953
|
+
instance_id = body["instance_id"].to_s.strip
|
|
4954
|
+
name = body["name"].is_a?(String) ? body["name"].strip : nil
|
|
4955
|
+
if !instance_id.empty? && !valid_instance_id?(instance_id)
|
|
4956
|
+
json_response(res, 422, { ok: false, error: "instance_id may only contain letters, digits, '-' and '_'" })
|
|
4957
|
+
return
|
|
4958
|
+
end
|
|
4959
|
+
return json_response(res, 422, { ok: false, error: "name is too long" }) if name && name.length > 60
|
|
4960
|
+
|
|
4961
|
+
fields = body.transform_keys(&:to_sym).reject { |k, _| %i[platform instance_id name id].include?(k) }
|
|
4844
4962
|
fields = fields.transform_values { |v| v.is_a?(String) ? v.strip : v }
|
|
4845
4963
|
|
|
4846
4964
|
# Record when the token was last updated so clients can detect re-login
|
|
@@ -4848,10 +4966,12 @@ module Clacky
|
|
|
4848
4966
|
fields[:token_updated_at] = Time.now.to_i if platform == :discord && fields.key?(:bot_token)
|
|
4849
4967
|
|
|
4850
4968
|
# Validate credentials against live API before persisting.
|
|
4851
|
-
# Merge with existing config so partial updates
|
|
4969
|
+
# Merge with the target instance's existing config so partial updates
|
|
4970
|
+
# (e.g. allowed_users only) still validate correctly. A new instance
|
|
4971
|
+
# does not inherit the default instance's credentials.
|
|
4852
4972
|
klass = Clacky::Channel::Adapters.find(platform)
|
|
4853
4973
|
if klass && klass.respond_to?(:test_connection)
|
|
4854
|
-
existing = config.platform_config(platform) || {}
|
|
4974
|
+
existing = config.platform_config(platform, instance_id) || {}
|
|
4855
4975
|
merged = existing.merge(fields)
|
|
4856
4976
|
result = klass.test_connection(merged)
|
|
4857
4977
|
unless result[:ok]
|
|
@@ -4860,13 +4980,19 @@ module Clacky
|
|
|
4860
4980
|
end
|
|
4861
4981
|
end
|
|
4862
4982
|
|
|
4863
|
-
config.set_platform(
|
|
4983
|
+
resolved_id = config.set_platform(
|
|
4984
|
+
platform,
|
|
4985
|
+
instance_id: instance_id,
|
|
4986
|
+
name: name,
|
|
4987
|
+
enabled: body.key?("enabled") ? body["enabled"] == true : nil,
|
|
4988
|
+
**fields
|
|
4989
|
+
)
|
|
4864
4990
|
config.save
|
|
4865
4991
|
|
|
4866
|
-
# Hot-reload
|
|
4867
|
-
@channel_manager.reload_platform(platform, config)
|
|
4992
|
+
# Hot-reload only the affected instance.
|
|
4993
|
+
@channel_manager.reload_platform(platform, config, resolved_id)
|
|
4868
4994
|
|
|
4869
|
-
json_response(res, 200, { ok: true })
|
|
4995
|
+
json_response(res, 200, { ok: true, instance_id: resolved_id })
|
|
4870
4996
|
rescue StandardError => e
|
|
4871
4997
|
json_response(res, 422, { ok: false, error: e.message })
|
|
4872
4998
|
end
|
|
@@ -4875,13 +5001,14 @@ module Clacky
|
|
|
4875
5001
|
# Creates a QQ "lite bind" task and returns the official connect URL
|
|
4876
5002
|
# (Tencent renders the QR). The AES key is held server-side, keyed by
|
|
4877
5003
|
# task_id, so the poll step can decrypt the returned AppSecret.
|
|
4878
|
-
def api_qq_qr_start(res)
|
|
5004
|
+
def api_qq_qr_start(req, res)
|
|
5005
|
+
instance_id = parse_json_body(req)["instance_id"].to_s.strip
|
|
4879
5006
|
binder = Clacky::Channel::Adapters::Qq::QrBinder.new
|
|
4880
5007
|
task = binder.create_task
|
|
4881
|
-
qr_bind_store[task[:task_id]] = { "key" => task[:key], "created_at" => Time.now.to_i }
|
|
5008
|
+
qr_bind_store[task[:task_id]] = { "key" => task[:key], "instance_id" => instance_id, "created_at" => Time.now.to_i }
|
|
4882
5009
|
prune_qr_bind_store
|
|
4883
5010
|
|
|
4884
|
-
json_response(res, 200, { ok: true, task_id: task[:task_id], connect_url: task[:connect_url] })
|
|
5011
|
+
json_response(res, 200, { ok: true, task_id: task[:task_id], connect_url: task[:connect_url], instance_id: instance_id })
|
|
4885
5012
|
rescue StandardError => e
|
|
4886
5013
|
json_response(res, 422, { ok: false, error: e.message })
|
|
4887
5014
|
end
|
|
@@ -4910,9 +5037,9 @@ module Clacky
|
|
|
4910
5037
|
user_openid: result[:user_openid],
|
|
4911
5038
|
key: stored["key"]
|
|
4912
5039
|
)
|
|
4913
|
-
persist_qq_qr_credentials(creds)
|
|
5040
|
+
persist_qq_qr_credentials(creds, stored["instance_id"])
|
|
4914
5041
|
qr_bind_store.delete(task_id)
|
|
4915
|
-
json_response(res, 200, { ok: true, status: "completed", app_id: creds[:app_id] })
|
|
5042
|
+
json_response(res, 200, { ok: true, status: "completed", app_id: creds[:app_id], instance_id: stored["instance_id"].to_s })
|
|
4916
5043
|
else
|
|
4917
5044
|
json_response(res, 200, { ok: true, status: "pending" })
|
|
4918
5045
|
end
|
|
@@ -4921,21 +5048,22 @@ module Clacky
|
|
|
4921
5048
|
end
|
|
4922
5049
|
|
|
4923
5050
|
# Persist scanned QQ credentials and ensure the scanning user can talk
|
|
4924
|
-
# to the bot (merged with
|
|
4925
|
-
def persist_qq_qr_credentials(creds)
|
|
5051
|
+
# to the bot (merged with that instance's existing whitelist).
|
|
5052
|
+
def persist_qq_qr_credentials(creds, instance_id = nil)
|
|
4926
5053
|
config = Clacky::ChannelConfig.load
|
|
4927
|
-
existing = config.platform_config(:qq) || {}
|
|
5054
|
+
existing = config.platform_config(:qq, instance_id) || {}
|
|
4928
5055
|
whitelist = Array(existing[:allowed_users]).map(&:to_s)
|
|
4929
5056
|
whitelist << creds[:user_openid] unless creds[:user_openid].to_s.empty?
|
|
4930
5057
|
|
|
4931
|
-
config.set_platform(
|
|
5058
|
+
resolved_id = config.set_platform(
|
|
4932
5059
|
:qq,
|
|
5060
|
+
instance_id: instance_id,
|
|
4933
5061
|
app_id: creds[:app_id],
|
|
4934
5062
|
app_secret: creds[:app_secret],
|
|
4935
5063
|
allowed_users: whitelist.uniq
|
|
4936
5064
|
)
|
|
4937
5065
|
config.save
|
|
4938
|
-
@channel_manager.reload_platform(:qq, config)
|
|
5066
|
+
@channel_manager.reload_platform(:qq, config, resolved_id)
|
|
4939
5067
|
end
|
|
4940
5068
|
|
|
4941
5069
|
def qr_bind_store
|
|
@@ -4951,16 +5079,20 @@ module Clacky
|
|
|
4951
5079
|
end
|
|
4952
5080
|
|
|
4953
5081
|
# DELETE /api/channels/:platform
|
|
4954
|
-
#
|
|
4955
|
-
|
|
5082
|
+
# DELETE /api/channels/:platform/instances/:id
|
|
5083
|
+
# Removes the instance from channels.yml and stops its adapter.
|
|
5084
|
+
# Without an instance id the whole platform entry is removed.
|
|
5085
|
+
def api_delete_channel(platform, res, instance_id = nil)
|
|
4956
5086
|
platform = platform.to_sym
|
|
4957
5087
|
config = Clacky::ChannelConfig.load
|
|
4958
|
-
config.
|
|
5088
|
+
removed = config.remove_platform(platform, instance_id)
|
|
5089
|
+
return json_response(res, 404, { ok: false, error: "instance not found" }) unless removed
|
|
5090
|
+
|
|
4959
5091
|
config.save
|
|
4960
5092
|
|
|
4961
|
-
@channel_manager.reload_platform(platform, config)
|
|
5093
|
+
@channel_manager.reload_platform(platform, config, instance_id)
|
|
4962
5094
|
|
|
4963
|
-
json_response(res, 200, { ok: true })
|
|
5095
|
+
json_response(res, 200, { ok: true, instance_id: instance_id })
|
|
4964
5096
|
rescue StandardError => e
|
|
4965
5097
|
json_response(res, 422, { ok: false, error: e.message })
|
|
4966
5098
|
end
|
|
@@ -5019,29 +5151,34 @@ module Clacky
|
|
|
5019
5151
|
end
|
|
5020
5152
|
|
|
5021
5153
|
# PATCH /api/channels/:platform/enabled
|
|
5022
|
-
# Body: { enabled: true|false }
|
|
5023
|
-
# Toggles
|
|
5024
|
-
#
|
|
5154
|
+
# Body: { enabled: true|false, instance_id?: "..." }
|
|
5155
|
+
# Toggles an instance on/off without touching credentials. Without an
|
|
5156
|
+
# instance_id the default (first) instance is toggled.
|
|
5157
|
+
# Enabling requires the instance to already be configured.
|
|
5025
5158
|
def api_toggle_channel(platform, req, res)
|
|
5026
5159
|
platform = platform.to_sym
|
|
5027
|
-
|
|
5160
|
+
body = parse_json_body(req)
|
|
5161
|
+
enabled = body["enabled"] == true
|
|
5028
5162
|
|
|
5029
5163
|
config = Clacky::ChannelConfig.load
|
|
5164
|
+
instance_id = body["instance_id"].to_s.strip
|
|
5165
|
+
instance_id = config.instances(platform).first&.fetch("id", nil) if instance_id.empty?
|
|
5166
|
+
|
|
5167
|
+
unless instance_id && config.instance(platform, instance_id)
|
|
5168
|
+
json_response(res, 422, { ok: false, error: "Platform is not configured yet" })
|
|
5169
|
+
return
|
|
5170
|
+
end
|
|
5030
5171
|
|
|
5031
5172
|
if enabled
|
|
5032
|
-
|
|
5033
|
-
json_response(res, 422, { ok: false, error: "Platform is not configured yet" })
|
|
5034
|
-
return
|
|
5035
|
-
end
|
|
5036
|
-
config.enable_platform(platform)
|
|
5173
|
+
config.enable_platform(platform, instance_id)
|
|
5037
5174
|
else
|
|
5038
|
-
config.disable_platform(platform)
|
|
5175
|
+
config.disable_platform(platform, instance_id)
|
|
5039
5176
|
end
|
|
5040
5177
|
|
|
5041
5178
|
config.save
|
|
5042
|
-
@channel_manager.reload_platform(platform, config)
|
|
5179
|
+
@channel_manager.reload_platform(platform, config, instance_id)
|
|
5043
5180
|
|
|
5044
|
-
json_response(res, 200, { ok: true, enabled: config.enabled?(platform) })
|
|
5181
|
+
json_response(res, 200, { ok: true, instance_id: instance_id, enabled: config.enabled?(platform, instance_id) })
|
|
5045
5182
|
rescue StandardError => e
|
|
5046
5183
|
json_response(res, 422, { ok: false, error: e.message })
|
|
5047
5184
|
end
|
|
@@ -5066,9 +5203,15 @@ module Clacky
|
|
|
5066
5203
|
json_response(res, 200, { ok: false, error: e.message })
|
|
5067
5204
|
end
|
|
5068
5205
|
|
|
5069
|
-
#
|
|
5070
|
-
|
|
5071
|
-
|
|
5206
|
+
# Instance ids are used in channel binding keys and thread names, so keep
|
|
5207
|
+
# them to a small, predictable character set.
|
|
5208
|
+
private def valid_instance_id?(id)
|
|
5209
|
+
id.to_s.match?(/\A[A-Za-z0-9][A-Za-z0-9_-]{0,39}\z/)
|
|
5210
|
+
end
|
|
5211
|
+
|
|
5212
|
+
# Returns non-secret fields for a platform instance (masked secrets).
|
|
5213
|
+
private def platform_safe_fields(platform, config, instance_id = nil)
|
|
5214
|
+
raw = config.instance(platform, instance_id) || {}
|
|
5072
5215
|
case platform.to_sym
|
|
5073
5216
|
when :feishu
|
|
5074
5217
|
{
|
|
@@ -5197,10 +5340,11 @@ module Clacky
|
|
|
5197
5340
|
end
|
|
5198
5341
|
|
|
5199
5342
|
# POST /api/cron-tasks — create task file + schedule in one step
|
|
5200
|
-
# Body: { name, content, cron, enabled? }
|
|
5343
|
+
# Body: { name, title?, content, cron, enabled? }
|
|
5201
5344
|
def api_create_cron_task(req, res)
|
|
5202
5345
|
body = parse_json_body(req)
|
|
5203
5346
|
name = body["name"].to_s.strip
|
|
5347
|
+
title = body["title"].to_s.strip
|
|
5204
5348
|
content = body["content"].to_s
|
|
5205
5349
|
cron = body["cron"].to_s.strip
|
|
5206
5350
|
enabled = body.key?("enabled") ? body["enabled"] : true
|
|
@@ -5214,24 +5358,36 @@ module Clacky
|
|
|
5214
5358
|
return json_response(res, 422, { error: "cron must have 5 fields (min hour dom month dow)" })
|
|
5215
5359
|
end
|
|
5216
5360
|
|
|
5217
|
-
|
|
5361
|
+
target = nil
|
|
5362
|
+
if body.key?("owner_session_id")
|
|
5363
|
+
owner_id = body["owner_session_id"].to_s.strip
|
|
5364
|
+
return json_response(res, 422, { error: "owner_session_id is required for secretary plans" }) if owner_id.empty?
|
|
5365
|
+
target = @work_service.schedule_target(owner_id, title: title.empty? ? name : title)
|
|
5366
|
+
end
|
|
5367
|
+
@scheduler.create_cron_task(name: name, content: content, cron: cron, enabled: enabled,
|
|
5368
|
+
target: target, title: title.empty? ? nil : title)
|
|
5218
5369
|
json_response(res, 201, { ok: true, name: name })
|
|
5370
|
+
rescue ArgumentError => e
|
|
5371
|
+
json_response(res, 422, { error: e.message })
|
|
5219
5372
|
end
|
|
5220
5373
|
|
|
5221
|
-
# PATCH /api/cron-tasks/:name — update content and/or cron/enabled
|
|
5222
|
-
# Body: { content?, cron?, enabled? }
|
|
5374
|
+
# PATCH /api/cron-tasks/:name — update content and/or cron/enabled/title
|
|
5375
|
+
# Body: { content?, cron?, enabled?, title? }
|
|
5223
5376
|
def api_update_cron_task(name, req, res)
|
|
5224
5377
|
body = parse_json_body(req)
|
|
5225
5378
|
content = body["content"]
|
|
5226
5379
|
cron = body["cron"]&.to_s&.strip
|
|
5227
5380
|
enabled = body["enabled"]
|
|
5381
|
+
title = body.key?("title") ? body["title"].to_s.strip : nil
|
|
5228
5382
|
|
|
5229
5383
|
if cron && cron.split(/\s+/).size != 5
|
|
5230
5384
|
return json_response(res, 422, { error: "cron must have 5 fields (min hour dom month dow)" })
|
|
5231
5385
|
end
|
|
5232
5386
|
|
|
5233
|
-
@scheduler.update_cron_task(name, content: content, cron: cron, enabled: enabled)
|
|
5387
|
+
@scheduler.update_cron_task(name, content: content, cron: cron, enabled: enabled, title: title)
|
|
5234
5388
|
json_response(res, 200, { ok: true, name: name })
|
|
5389
|
+
rescue ArgumentError => e
|
|
5390
|
+
json_response(res, 422, { error: e.message })
|
|
5235
5391
|
rescue => e
|
|
5236
5392
|
json_response(res, 404, { error: e.message })
|
|
5237
5393
|
end
|
|
@@ -5266,8 +5422,16 @@ module Clacky
|
|
|
5266
5422
|
return json_response(res, 404, { error: "Cron task not found: #{name}" })
|
|
5267
5423
|
end
|
|
5268
5424
|
|
|
5425
|
+
schedule = @scheduler.schedules.find { |plan| plan["task"] == name }
|
|
5426
|
+
if schedule && schedule["target"]
|
|
5427
|
+
card = @work_service.dispatch_schedule(schedule, @scheduler.read_task(name), Time.now)
|
|
5428
|
+
return json_response(res, 409, { error: "Previous work is unfinished or its owner is unavailable" }) unless card
|
|
5429
|
+
return json_response(res, 202, { ok: true, session: @registry.session_summary(card["session_id"]) })
|
|
5430
|
+
end
|
|
5431
|
+
|
|
5269
5432
|
prompt = @scheduler.read_task(name)
|
|
5270
|
-
|
|
5433
|
+
label = schedule && !schedule["title"].to_s.empty? ? schedule["title"] : name
|
|
5434
|
+
session_name = "▶ #{label} #{Time.now.strftime("%H:%M")}"
|
|
5271
5435
|
working_dir = File.expand_path("~/clacky_workspace")
|
|
5272
5436
|
FileUtils.mkdir_p(working_dir)
|
|
5273
5437
|
|
|
@@ -7193,6 +7357,14 @@ module Clacky
|
|
|
7193
7357
|
client.test_connection(model: model)
|
|
7194
7358
|
end
|
|
7195
7359
|
|
|
7360
|
+
private def preset_model_capabilities(id, preset)
|
|
7361
|
+
names = (preset["models"] || []) | (preset["model_capabilities"] || {}).keys
|
|
7362
|
+
names.each_with_object({}) do |name, out|
|
|
7363
|
+
caps = Clacky::Providers.capabilities(id, model_name: name)
|
|
7364
|
+
out[name] = caps unless caps.empty?
|
|
7365
|
+
end
|
|
7366
|
+
end
|
|
7367
|
+
|
|
7196
7368
|
# GET /api/providers — return built-in provider presets for quick setup
|
|
7197
7369
|
def api_list_providers(res)
|
|
7198
7370
|
providers = Clacky::Providers::PRESETS.map do |id, preset|
|
|
@@ -7212,7 +7384,9 @@ module Clacky
|
|
|
7212
7384
|
# billing-plan variants) when present. Absent for single-endpoint
|
|
7213
7385
|
# providers — UI renders a plain text input in that case.
|
|
7214
7386
|
endpoint_variants: preset["endpoint_variants"],
|
|
7215
|
-
website_url: preset["website_url"]
|
|
7387
|
+
website_url: preset["website_url"],
|
|
7388
|
+
# Lets the custom-provider form infer capabilities from a typed model name.
|
|
7389
|
+
model_capabilities: preset_model_capabilities(id, preset)
|
|
7216
7390
|
}
|
|
7217
7391
|
end
|
|
7218
7392
|
json_response(res, 200, { providers: providers })
|
|
@@ -7908,6 +8082,8 @@ module Clacky
|
|
|
7908
8082
|
zos.write(File.binread(chunk_path))
|
|
7909
8083
|
end
|
|
7910
8084
|
|
|
8085
|
+
write_work_debug_bundle(zos, bundle[:session][:session_id])
|
|
8086
|
+
|
|
7911
8087
|
log_path = Clacky::Logger.current_log_file
|
|
7912
8088
|
if log_path && File.exist?(log_path)
|
|
7913
8089
|
zos.put_next_entry("logs/#{File.basename(log_path)}")
|
|
@@ -7930,6 +8106,46 @@ module Clacky
|
|
|
7930
8106
|
json_response(res, 500, { error: "Export failed: #{e.message}" })
|
|
7931
8107
|
end
|
|
7932
8108
|
|
|
8109
|
+
private def write_work_debug_bundle(zos, session_id)
|
|
8110
|
+
snapshot = @work_service&.debug_snapshot(session_id)
|
|
8111
|
+
return if !snapshot || snapshot["cards"].empty?
|
|
8112
|
+
|
|
8113
|
+
sessions = snapshot["cards"].map { |card| card["session_id"] }.uniq.map do |worker_id|
|
|
8114
|
+
entry = { "session_id" => worker_id }
|
|
8115
|
+
if worker_id == session_id
|
|
8116
|
+
entry.merge("path" => "session.json", "status" => "included")
|
|
8117
|
+
else
|
|
8118
|
+
bundle = @session_manager.files_for(worker_id)
|
|
8119
|
+
# files_for accepts prefixes; debug associations must be exact.
|
|
8120
|
+
if bundle && bundle[:session][:session_id] == worker_id
|
|
8121
|
+
prefix = "work/sessions/#{URI.encode_www_form_component(worker_id)}/"
|
|
8122
|
+
begin
|
|
8123
|
+
files = [["session.json", File.binread(bundle[:json_path])]]
|
|
8124
|
+
bundle[:chunks].each { |path| files << [File.basename(path), File.binread(path)] }
|
|
8125
|
+
files.each do |name, content|
|
|
8126
|
+
zos.put_next_entry(prefix + name)
|
|
8127
|
+
zos.write(content)
|
|
8128
|
+
end
|
|
8129
|
+
entry.merge("path" => prefix + "session.json", "status" => "included")
|
|
8130
|
+
rescue Errno::ENOENT
|
|
8131
|
+
entry.merge("status" => "missing")
|
|
8132
|
+
end
|
|
8133
|
+
else
|
|
8134
|
+
entry.merge("status" => "missing")
|
|
8135
|
+
end
|
|
8136
|
+
end
|
|
8137
|
+
end
|
|
8138
|
+
manifest = {
|
|
8139
|
+
"session_id" => session_id,
|
|
8140
|
+
"work" => snapshot["cards"].map { |card| card.slice("id", "session_id", "owner_session_id", "revision") },
|
|
8141
|
+
"sessions" => sessions
|
|
8142
|
+
}
|
|
8143
|
+
{ "manifest" => manifest, "cards" => snapshot["cards"], "events" => snapshot["events"] }.each do |name, data|
|
|
8144
|
+
zos.put_next_entry("work/#{name}.json")
|
|
8145
|
+
zos.write(JSON.pretty_generate(data))
|
|
8146
|
+
end
|
|
8147
|
+
end
|
|
8148
|
+
|
|
7933
8149
|
# ── WebSocket ─────────────────────────────────────────────────────────────
|
|
7934
8150
|
|
|
7935
8151
|
def websocket_upgrade?(req)
|
|
@@ -8199,8 +8415,7 @@ module Clacky
|
|
|
8199
8415
|
@registry.with_session(session_id) do |s|
|
|
8200
8416
|
if s[:status] == :running && %w[queue steer].include?(mode)
|
|
8201
8417
|
queued_created_at = Time.now.to_f
|
|
8202
|
-
s[:agent].enqueue_input(content, delivery: mode.to_sym, files: files,
|
|
8203
|
-
reference_contexts: build_reference_contexts(references), created_at: queued_created_at)
|
|
8418
|
+
s[:agent].enqueue_input(content, delivery: mode.to_sym, files: files, reference_contexts: build_reference_contexts(references), created_at: queued_created_at)
|
|
8204
8419
|
queued = true
|
|
8205
8420
|
end
|
|
8206
8421
|
end
|
|
@@ -8222,8 +8437,7 @@ module Clacky
|
|
|
8222
8437
|
@registry.with_session(session_id) { |s| old_thread = s[:thread] }
|
|
8223
8438
|
if old_thread && !old_thread.join(2)
|
|
8224
8439
|
retained_created_at = Time.now.to_f
|
|
8225
|
-
session[:agent].enqueue_input(content, files: files,
|
|
8226
|
-
reference_contexts: build_reference_contexts(references), created_at: retained_created_at)
|
|
8440
|
+
session[:agent].enqueue_input(content, files: files, reference_contexts: build_reference_contexts(references), created_at: retained_created_at)
|
|
8227
8441
|
broadcast(session_id, { type: "input_enqueued", session_id: session_id, created_at: retained_created_at })
|
|
8228
8442
|
broadcast(session_id, { type: "input_queue_notice", session_id: session_id, key: "chat.input.stoppingDelayed" })
|
|
8229
8443
|
return
|
|
@@ -8274,14 +8488,13 @@ module Clacky
|
|
|
8274
8488
|
|
|
8275
8489
|
# File references are now handled inside agent.run — injected as a system_injected
|
|
8276
8490
|
# message after the user message, so replay_history skips them automatically.
|
|
8277
|
-
reference_contexts = build_reference_contexts(references)
|
|
8278
8491
|
run_agent_task(session_id, agent) do
|
|
8279
8492
|
# Start the progress card inside the task: run_agent_task returns early when
|
|
8280
8493
|
# the concurrency cap is hit, and posting beforehand would strand the card at
|
|
8281
8494
|
# "Thinking..." forever. A web-originated turn bound to an IM channel gets a
|
|
8282
8495
|
# card too, so tools/narration render like a native IM turn (#603).
|
|
8283
8496
|
@channel_manager.channel_ui_for_session(session_id)&.start_task(reply_to: nil)
|
|
8284
|
-
agent.run(content, files: files,
|
|
8497
|
+
agent.run(content, files: files, created_at: msg_created_at, reference_contexts: build_reference_contexts(references))
|
|
8285
8498
|
end
|
|
8286
8499
|
end
|
|
8287
8500
|
|
|
@@ -8290,21 +8503,24 @@ module Clacky
|
|
|
8290
8503
|
# (e.g. branch diff, browser snapshot) only add a `when` branch here.
|
|
8291
8504
|
private def build_reference_contexts(references)
|
|
8292
8505
|
Array(references).filter_map do |ref|
|
|
8506
|
+
next ref if ref.is_a?(String)
|
|
8293
8507
|
next unless ref.is_a?(Hash)
|
|
8294
|
-
case ref["type"].to_s
|
|
8295
|
-
|
|
8296
|
-
|
|
8297
|
-
|
|
8508
|
+
context = case (ref[:type] || ref["type"]).to_s
|
|
8509
|
+
when "session" then build_session_reference_context(ref)
|
|
8510
|
+
when "quote" then build_quote_reference_context(ref)
|
|
8511
|
+
end
|
|
8512
|
+
metadata = ref.reject { |key, _| key.to_s == "context" }
|
|
8513
|
+
context ? metadata.merge(context: context) : metadata
|
|
8298
8514
|
end
|
|
8299
8515
|
end
|
|
8300
8516
|
|
|
8301
8517
|
# Resolve a past-chat reference into a lightweight pointer for the LLM:
|
|
8302
8518
|
# the session name + ID + on-disk file path, no transcript inlined.
|
|
8303
8519
|
private def build_session_reference_context(ref)
|
|
8304
|
-
session_id = ref["session_id"].to_s
|
|
8520
|
+
session_id = (ref[:session_id] || ref["session_id"]).to_s
|
|
8305
8521
|
return nil if session_id.empty?
|
|
8306
8522
|
|
|
8307
|
-
name = ref["name"].to_s
|
|
8523
|
+
name = (ref[:name] || ref["name"]).to_s
|
|
8308
8524
|
name = session_id if name.empty?
|
|
8309
8525
|
|
|
8310
8526
|
lines = ["[Referenced conversation: #{name}]", "Session ID: #{session_id}"]
|
|
@@ -8318,10 +8534,10 @@ module Clacky
|
|
|
8318
8534
|
# message. The excerpt is the whole point of the reference, so inline it
|
|
8319
8535
|
# verbatim — no file pointer, nothing for the model to go fetch.
|
|
8320
8536
|
private def build_quote_reference_context(ref)
|
|
8321
|
-
text = ref["text"].to_s.strip
|
|
8537
|
+
text = (ref[:text] || ref["text"]).to_s.strip
|
|
8322
8538
|
return nil if text.empty?
|
|
8323
8539
|
|
|
8324
|
-
label = ref["label"].to_s.strip
|
|
8540
|
+
label = (ref[:label] || ref["label"]).to_s.strip
|
|
8325
8541
|
header = label.empty? ? "[Quoted excerpt from this conversation]" :
|
|
8326
8542
|
"[Quoted excerpt from this conversation: #{label}]"
|
|
8327
8543
|
[header, text].join("\n")
|
|
@@ -8356,13 +8572,14 @@ module Clacky
|
|
|
8356
8572
|
Clacky::Logger.warn("[interrupt] raise failed: #{e.message}")
|
|
8357
8573
|
end
|
|
8358
8574
|
end
|
|
8359
|
-
end
|
|
8360
|
-
|
|
8361
|
-
#
|
|
8362
|
-
#
|
|
8363
|
-
#
|
|
8364
|
-
#
|
|
8365
|
-
|
|
8575
|
+
end
|
|
8576
|
+
|
|
8577
|
+
# Shared submission path for host services and extensions. Execution and
|
|
8578
|
+
# presentation happen only after admission, on the session's owning thread.
|
|
8579
|
+
# display_message supplies the same friendly input for live display and replay.
|
|
8580
|
+
# around_execution, when supplied by a host service, wraps the admitted
|
|
8581
|
+
# execution on its owning thread (e.g. generation fencing and receipts).
|
|
8582
|
+
def run_session_task(session_id, prompt, display_message: nil, reference_contexts: nil, background: false, &around_execution)
|
|
8366
8583
|
return unless @registry.exist?(session_id)
|
|
8367
8584
|
|
|
8368
8585
|
agent = nil
|
|
@@ -8373,9 +8590,18 @@ module Clacky
|
|
|
8373
8590
|
end
|
|
8374
8591
|
return unless agent
|
|
8375
8592
|
|
|
8376
|
-
|
|
8377
|
-
|
|
8378
|
-
|
|
8593
|
+
run_agent_task(session_id, agent, background: background) do
|
|
8594
|
+
execute = lambda do
|
|
8595
|
+
display_options = { source: :web }
|
|
8596
|
+
visible_references = Array(reference_contexts).select { |ref| ref.is_a?(Hash) }.map { |ref| ref.reject { |key, _| key.to_s == "context" } }
|
|
8597
|
+
display_options[:references] = visible_references unless visible_references.empty?
|
|
8598
|
+
web_ui&.show_user_message(display_message, **display_options) if display_message
|
|
8599
|
+
options = { display_text: display_message }
|
|
8600
|
+
options[:reference_contexts] = build_reference_contexts(reference_contexts) if reference_contexts
|
|
8601
|
+
agent.run(prompt, **options)
|
|
8602
|
+
end
|
|
8603
|
+
around_execution ? around_execution.call(execute, agent) : execute.call
|
|
8604
|
+
end
|
|
8379
8605
|
end
|
|
8380
8606
|
|
|
8381
8607
|
# Start the pending task for a session.
|
|
@@ -8451,7 +8677,23 @@ module Clacky
|
|
|
8451
8677
|
# Run an agent task in a background thread, handling status updates,
|
|
8452
8678
|
# session persistence, and idle compression timer lifecycle.
|
|
8453
8679
|
# Yields to the caller to perform the actual agent.run call.
|
|
8454
|
-
private def run_agent_task(session_id, agent, &task)
|
|
8680
|
+
private def run_agent_task(session_id, agent, background: false, &task)
|
|
8681
|
+
# Admission must be atomic across user messages and coordinator wakeups.
|
|
8682
|
+
(@task_admission_mutex ||= Mutex.new).synchronize do
|
|
8683
|
+
session = @registry.get(session_id)
|
|
8684
|
+
return unless session
|
|
8685
|
+
if session[:status] == :running || (background && session[:thread]&.alive?)
|
|
8686
|
+
unless background
|
|
8687
|
+
broadcast(session_id, { type: "error", session_id: session_id,
|
|
8688
|
+
message: "Session is busy; please send your message again." })
|
|
8689
|
+
end
|
|
8690
|
+
return
|
|
8691
|
+
end
|
|
8692
|
+
launch_agent_task(session_id, agent, &task)
|
|
8693
|
+
end
|
|
8694
|
+
end
|
|
8695
|
+
|
|
8696
|
+
private def launch_agent_task(session_id, agent, &task)
|
|
8455
8697
|
if @registry.running_full?
|
|
8456
8698
|
broadcast(session_id, { type: "error", session_id: session_id,
|
|
8457
8699
|
message: "Too many concurrent tasks (max #{@registry.max_running_agents}), please try again later" })
|
|
@@ -8525,6 +8767,13 @@ module Clacky
|
|
|
8525
8767
|
# Also fires when awaiting feedback — that is precisely when the user
|
|
8526
8768
|
# most needs pulling back; the flag lets the client word it differently.
|
|
8527
8769
|
finished = { type: "task_finished", session_id: session_id, awaiting_feedback: !!awaiting }
|
|
8770
|
+
# Carried inline because the client may not have this session in its
|
|
8771
|
+
# list yet (cron runs land in another project, and the sidebar is
|
|
8772
|
+
# paged), so a client-side lookup by id would come back empty and the
|
|
8773
|
+
# toast would lose the session name.
|
|
8774
|
+
name = @registry.with_session(session_id) { |s| s[:agent]&.name }
|
|
8775
|
+
finished[:session_name] = name unless name.to_s.strip.empty?
|
|
8776
|
+
finished[:notify_user] = false if run_result.is_a?(Hash) && run_result[:notify_user] == false
|
|
8528
8777
|
# Lets clients not viewing this session keep the summary until it is first opened.
|
|
8529
8778
|
complete = web_ui&.last_complete
|
|
8530
8779
|
finished[:complete] = complete if complete&.dig(:task_id) && run_result.is_a?(Hash) && complete[:task_id] == run_result[:task_id]
|
|
@@ -8709,6 +8958,7 @@ module Clacky
|
|
|
8709
8958
|
ui = WebUIController.new(session_id, broadcaster)
|
|
8710
8959
|
agent = Clacky::Agent.new(client, config, working_dir: working_dir, ui: ui, profile: profile,
|
|
8711
8960
|
session_id: session_id, source: source)
|
|
8961
|
+
@work_service&.attach(agent)
|
|
8712
8962
|
agent.rename(name) unless name.nil? || name.empty?
|
|
8713
8963
|
idle_timer = build_idle_timer(session_id, agent)
|
|
8714
8964
|
|
|
@@ -8741,6 +8991,7 @@ module Clacky
|
|
|
8741
8991
|
profile = session_data[:agent_profile].to_s
|
|
8742
8992
|
profile = "general" if profile.empty?
|
|
8743
8993
|
agent = Clacky::Agent.from_session(client, config, session_data, ui: ui, profile: profile)
|
|
8994
|
+
@work_service&.attach(agent)
|
|
8744
8995
|
idle_timer = build_idle_timer(original_id, agent)
|
|
8745
8996
|
|
|
8746
8997
|
# Register session atomically with a fully-built agent so no concurrent
|