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
|
@@ -34,7 +34,8 @@ module Clacky
|
|
|
34
34
|
# :chat (default) — one session per chat (all users in a chat share it).
|
|
35
35
|
# :chat_user — one session per (chat, user) pair.
|
|
36
36
|
# :user — one session per user (merges all chats).
|
|
37
|
-
def initialize(session_registry:, session_builder:, run_agent_task:, interrupt_session:, channel_config:, persist_session: nil, binding_mode: :chat)
|
|
37
|
+
def initialize(session_registry:, session_builder:, run_agent_task:, interrupt_session:, channel_config:, persist_session: nil, binding_mode: :chat, work_service: nil)
|
|
38
|
+
@work_service = work_service
|
|
38
39
|
@registry = session_registry
|
|
39
40
|
@session_builder = session_builder
|
|
40
41
|
@run_agent_task = run_agent_task
|
|
@@ -46,25 +47,26 @@ module Clacky
|
|
|
46
47
|
@adapter_threads = []
|
|
47
48
|
@running = false
|
|
48
49
|
@mutex = Mutex.new
|
|
49
|
-
@session_counters =
|
|
50
|
+
@session_counters = {}
|
|
50
51
|
@dedup_mutex = Mutex.new
|
|
51
52
|
@last_message = {} # channel_key => [digest, monotonic_time]
|
|
52
53
|
end
|
|
53
54
|
|
|
54
|
-
# Start all enabled
|
|
55
|
+
# Start all enabled adapter instances in background threads. Non-blocking.
|
|
55
56
|
def start
|
|
56
|
-
|
|
57
|
-
if
|
|
57
|
+
enabled = @channel_config.enabled_instances
|
|
58
|
+
if enabled.empty?
|
|
58
59
|
Clacky::Logger.info("[ChannelManager] No channels configured — skipping")
|
|
59
60
|
return
|
|
60
61
|
end
|
|
61
62
|
|
|
62
|
-
|
|
63
|
+
labels = enabled.map { |platform, inst| instance_label(platform, inst["id"]) }
|
|
64
|
+
Clacky::Logger.info("[ChannelManager] Starting channels: #{labels.join(", ")}")
|
|
63
65
|
@running = true
|
|
64
66
|
|
|
65
67
|
restore_channel_bindings
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
enabled.each { |platform, inst| start_adapter(platform, inst["id"]) }
|
|
68
70
|
end
|
|
69
71
|
|
|
70
72
|
# Stop all adapters gracefully.
|
|
@@ -84,23 +86,41 @@ module Clacky
|
|
|
84
86
|
|
|
85
87
|
# @return [Array<Symbol>] platforms currently running
|
|
86
88
|
def running_platforms
|
|
87
|
-
@mutex.synchronize { @adapters.map(&:platform_id) }
|
|
89
|
+
@mutex.synchronize { @adapters.map(&:platform_id).uniq }
|
|
88
90
|
end
|
|
89
91
|
|
|
90
|
-
#
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
# @return [Array<Hash>] running instances as { platform:, instance_id: }
|
|
93
|
+
def running_instances
|
|
94
|
+
@mutex.synchronize do
|
|
95
|
+
@adapters.map { |a| { platform: a.platform_id, instance_id: a.instance_id } }
|
|
96
|
+
end
|
|
97
|
+
end
|
|
94
98
|
|
|
95
|
-
# Return the currently-live adapter for a
|
|
96
|
-
# Thread-safe — acquires @mutex to read
|
|
99
|
+
# Return the currently-live adapter for a platform (and optionally one
|
|
100
|
+
# configured instance). Thread-safe — acquires @mutex to read @adapters.
|
|
101
|
+
#
|
|
102
|
+
# A nil instance id resolves to the default instance, falling back to the
|
|
103
|
+
# first running adapter of that platform. An explicit id never falls back
|
|
104
|
+
# to another instance, so replies can't leak between two Feishu apps.
|
|
97
105
|
# @param platform [Symbol, String]
|
|
106
|
+
# @param instance_id [String, nil]
|
|
98
107
|
# @return [Object, nil]
|
|
99
|
-
def adapter_for(platform)
|
|
108
|
+
def adapter_for(platform, instance_id = nil)
|
|
100
109
|
platform = platform.to_sym
|
|
101
|
-
@mutex.synchronize
|
|
110
|
+
@mutex.synchronize do
|
|
111
|
+
if instance_id.nil? || instance_id.to_s.empty?
|
|
112
|
+
@adapters.find { |a| a.platform_id == platform && a.instance_id == ChannelConfig::DEFAULT_INSTANCE_ID } ||
|
|
113
|
+
@adapters.find { |a| a.platform_id == platform }
|
|
114
|
+
else
|
|
115
|
+
@adapters.find { |a| a.platform_id == platform && a.instance_id == instance_id.to_s }
|
|
116
|
+
end
|
|
117
|
+
end
|
|
102
118
|
end
|
|
103
119
|
|
|
120
|
+
# Proactively send a message to a user on the given platform.
|
|
121
|
+
#
|
|
122
|
+
# For Weixin (iLink protocol) a context_token is required for every outbound
|
|
123
|
+
# message. This method looks up the most-recently cached token for user_id.
|
|
104
124
|
# If no token is found the message cannot be delivered and nil is returned.
|
|
105
125
|
#
|
|
106
126
|
# For Feishu and WeCom the chat_id / user_id is sufficient — no token needed.
|
|
@@ -108,33 +128,35 @@ module Clacky
|
|
|
108
128
|
# @param platform [Symbol, String] e.g. :weixin, :feishu, :wecom
|
|
109
129
|
# @param user_id [String] IM user identifier
|
|
110
130
|
# @param message [String] plain-text (or markdown) message to send
|
|
131
|
+
# @param instance_id [String, nil] nil uses the default instance
|
|
111
132
|
# @return [Hash, nil] adapter result hash, or nil on failure
|
|
112
|
-
def send_to_user(platform, user_id, message)
|
|
133
|
+
def send_to_user(platform, user_id, message, instance_id = nil)
|
|
113
134
|
platform = platform.to_sym
|
|
114
|
-
adapter = adapter_for(platform)
|
|
135
|
+
adapter = adapter_for(platform, instance_id)
|
|
115
136
|
|
|
116
137
|
unless adapter
|
|
117
|
-
Clacky::Logger.warn("[ChannelManager] send_to_user: no running adapter for
|
|
138
|
+
Clacky::Logger.warn("[ChannelManager] send_to_user: no running adapter for #{instance_label(platform, instance_id)}")
|
|
118
139
|
return nil
|
|
119
140
|
end
|
|
120
141
|
|
|
121
|
-
Clacky::Logger.info("[ChannelManager] send_to_user
|
|
142
|
+
Clacky::Logger.info("[ChannelManager] send_to_user #{instance_label(platform, instance_id)} → #{user_id}")
|
|
122
143
|
adapter.send_text(user_id, message)
|
|
123
144
|
rescue StandardError => e
|
|
124
145
|
Clacky::Logger.error("[ChannelManager] send_to_user failed: #{e.message}")
|
|
125
146
|
nil
|
|
126
147
|
end
|
|
127
148
|
|
|
128
|
-
# Return a list of known user IDs for the given platform.
|
|
149
|
+
# Return a list of known user IDs for the given platform (or instance).
|
|
129
150
|
# Collected from every message that has been processed since the server started.
|
|
130
151
|
# Weixin stores context_tokens keyed by user_id; feishu/wecom track chat_ids
|
|
131
152
|
# via the session binding table in the registry.
|
|
132
153
|
#
|
|
133
154
|
# @param platform [Symbol, String]
|
|
155
|
+
# @param instance_id [String, nil] nil aggregates every instance
|
|
134
156
|
# @return [Array<String>]
|
|
135
|
-
def known_users(platform)
|
|
157
|
+
def known_users(platform, instance_id = nil)
|
|
136
158
|
platform = platform.to_sym
|
|
137
|
-
adapter = adapter_for(platform)
|
|
159
|
+
adapter = adapter_for(platform, instance_id)
|
|
138
160
|
return [] unless adapter
|
|
139
161
|
|
|
140
162
|
# Weixin adapter exposes @context_tokens whose keys are user_ids
|
|
@@ -147,17 +169,17 @@ module Clacky
|
|
|
147
169
|
# :user → "platform:user:USER_ID"
|
|
148
170
|
# :chat → "platform:chat:CHAT_ID"
|
|
149
171
|
# :chat_user → "platform:chat:CHAT_ID:user:USER_ID"
|
|
172
|
+
# Extra instances prefix the platform with "platform#instance_id".
|
|
150
173
|
#
|
|
151
174
|
# For send_text we need the chat_id (Feishu/WeCom use chat_id as the
|
|
152
175
|
# receive_id for outbound messages), so we extract the chat portion.
|
|
153
|
-
prefix = "#{platform}:"
|
|
154
176
|
ids = []
|
|
155
177
|
@registry.list(archived: nil).each do |summary|
|
|
156
178
|
@registry.with_session(summary[:id]) do |s|
|
|
157
179
|
(s[:channel_keys] || []).each do |key|
|
|
158
|
-
|
|
180
|
+
remainder = channel_key_payload(key, platform, instance_id)
|
|
181
|
+
next unless remainder
|
|
159
182
|
|
|
160
|
-
remainder = key.sub(prefix, "") # e.g. "chat:OC_ID:user:OU_ID" or "user:UID" or "chat:CID"
|
|
161
183
|
ids << extract_chat_id(remainder)
|
|
162
184
|
end
|
|
163
185
|
end
|
|
@@ -165,27 +187,36 @@ module Clacky
|
|
|
165
187
|
ids.compact.uniq
|
|
166
188
|
end
|
|
167
189
|
|
|
168
|
-
# Hot-reload
|
|
169
|
-
#
|
|
190
|
+
# Hot-reload platform adapters with updated config.
|
|
191
|
+
# With an instance id, only that instance is restarted; without one, every
|
|
192
|
+
# enabled instance of the platform is rebuilt.
|
|
170
193
|
# @param platform [Symbol]
|
|
171
194
|
# @param config [Clacky::ChannelConfig]
|
|
172
|
-
|
|
173
|
-
|
|
195
|
+
# @param instance_id [String, nil]
|
|
196
|
+
def reload_platform(platform, config, instance_id = nil)
|
|
197
|
+
@channel_config = config
|
|
198
|
+
|
|
174
199
|
@mutex.synchronize do
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
@adapters.delete(existing)
|
|
200
|
+
targets = @adapters.select do |a|
|
|
201
|
+
a.platform_id == platform &&
|
|
202
|
+
(instance_id.nil? || instance_id.to_s.empty? || a.instance_id == instance_id.to_s)
|
|
179
203
|
end
|
|
204
|
+
targets.each { |a| safe_stop_adapter(a) }
|
|
205
|
+
@adapters.reject! { |a| targets.include?(a) }
|
|
180
206
|
end
|
|
181
207
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
208
|
+
if instance_id.nil? || instance_id.to_s.empty?
|
|
209
|
+
config.instances(platform).each do |inst|
|
|
210
|
+
start_adapter(platform, inst["id"]) if inst["enabled"]
|
|
211
|
+
end
|
|
212
|
+
elsif config.enabled?(platform, instance_id)
|
|
213
|
+
start_adapter(platform, instance_id)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
if @adapters.any? { |a| a.platform_id == platform }
|
|
217
|
+
Clacky::Logger.info("[ChannelManager] #{instance_label(platform, instance_id)} adapter reloaded")
|
|
187
218
|
else
|
|
188
|
-
Clacky::Logger.info("[ChannelManager]
|
|
219
|
+
Clacky::Logger.info("[ChannelManager] #{instance_label(platform, instance_id)} disabled — adapter not started")
|
|
189
220
|
end
|
|
190
221
|
end
|
|
191
222
|
|
|
@@ -196,27 +227,39 @@ module Clacky
|
|
|
196
227
|
@channel_config = config
|
|
197
228
|
end
|
|
198
229
|
|
|
199
|
-
def start_adapter(platform)
|
|
230
|
+
def start_adapter(platform, instance_id = nil)
|
|
200
231
|
klass = Adapters.find(platform)
|
|
201
232
|
unless klass
|
|
202
233
|
Clacky::Logger.warn("[ChannelManager] No adapter registered for :#{platform} — skipping")
|
|
203
234
|
return
|
|
204
235
|
end
|
|
205
236
|
|
|
206
|
-
raw_config = @channel_config.platform_config(platform)
|
|
207
|
-
|
|
237
|
+
raw_config = @channel_config.platform_config(platform, instance_id)
|
|
238
|
+
return if raw_config.nil?
|
|
239
|
+
|
|
240
|
+
inst = @channel_config.instance(platform, instance_id)
|
|
241
|
+
id = inst ? inst["id"] : ChannelConfig::DEFAULT_INSTANCE_ID
|
|
242
|
+
|
|
243
|
+
already_running = @mutex.synchronize do
|
|
244
|
+
@adapters.any? { |a| a.platform_id == platform.to_sym && a.instance_id == id }
|
|
245
|
+
end
|
|
246
|
+
return if already_running
|
|
247
|
+
|
|
248
|
+
Clacky::Logger.info("[ChannelManager] Initializing #{instance_label(platform, id)} adapter")
|
|
208
249
|
adapter = klass.new(raw_config)
|
|
250
|
+
adapter.instance_id = id
|
|
251
|
+
adapter.instance_name = inst["name"].to_s if inst
|
|
209
252
|
|
|
210
253
|
errors = adapter.validate_config(raw_config)
|
|
211
254
|
if errors.any?
|
|
212
|
-
Clacky::Logger.warn("[ChannelManager] Config errors for
|
|
255
|
+
Clacky::Logger.warn("[ChannelManager] Config errors for #{instance_label(platform, id)}: #{errors.join(", ")}")
|
|
213
256
|
return
|
|
214
257
|
end
|
|
215
258
|
|
|
216
259
|
@mutex.synchronize { @adapters << adapter }
|
|
217
|
-
Clacky::Logger.info("[ChannelManager]
|
|
260
|
+
Clacky::Logger.info("[ChannelManager] #{instance_label(platform, id)} adapter ready, starting thread")
|
|
218
261
|
|
|
219
|
-
thread = Clacky::ThreadRegistry.spawn(name:
|
|
262
|
+
thread = Clacky::ThreadRegistry.spawn(name: adapter_thread_name(platform, id)) do
|
|
220
263
|
adapter_loop(adapter)
|
|
221
264
|
end
|
|
222
265
|
|
|
@@ -224,30 +267,33 @@ module Clacky
|
|
|
224
267
|
end
|
|
225
268
|
|
|
226
269
|
def adapter_loop(adapter)
|
|
227
|
-
Clacky::Logger.info("[ChannelManager]
|
|
270
|
+
Clacky::Logger.info("[ChannelManager] #{adapter_label(adapter)} adapter loop started")
|
|
228
271
|
adapter.start do |event|
|
|
272
|
+
event[:channel_instance_id] ||= adapter.instance_id
|
|
229
273
|
summary = event[:text].to_s.lines.first.to_s.strip[0, 80]
|
|
230
274
|
summary = "[image]" if summary.empty? && !event[:files].to_a.empty?
|
|
231
|
-
Clacky::Logger.info("[ChannelManager]
|
|
275
|
+
Clacky::Logger.info("[ChannelManager] #{adapter_label(adapter)} message from #{event[:user_id]} in #{event[:chat_id]}: #{summary}")
|
|
232
276
|
route_message(adapter, event)
|
|
233
277
|
rescue StandardError => e
|
|
234
|
-
Clacky::Logger.warn("[ChannelManager] Error routing
|
|
278
|
+
Clacky::Logger.warn("[ChannelManager] Error routing #{adapter_label(adapter)} message: #{e.message}\n#{e.backtrace.first(3).join("\n")}")
|
|
235
279
|
adapter.send_text(event[:chat_id], "Error: #{e.message}")
|
|
236
280
|
end
|
|
237
281
|
rescue StandardError => e
|
|
238
282
|
if @running && !Clacky::Shutdown.requested?
|
|
239
|
-
Clacky::Logger.warn("[ChannelManager]
|
|
240
|
-
Clacky::Logger.info("[ChannelManager]
|
|
283
|
+
Clacky::Logger.warn("[ChannelManager] #{adapter_label(adapter)} adapter crashed: #{e.message}\n#{e.backtrace.first(3).join("\n")}")
|
|
284
|
+
Clacky::Logger.info("[ChannelManager] #{adapter_label(adapter)} restarting in 5s...")
|
|
241
285
|
Clacky::Shutdown.sleep(5)
|
|
242
286
|
retry
|
|
243
287
|
else
|
|
244
288
|
# Shutting down — the adapter was stopped, not crashed (e.g. a ws
|
|
245
289
|
# socket closed mid-read raises EBADF). Keep the log quiet.
|
|
246
|
-
Clacky::Logger.debug("[ChannelManager]
|
|
290
|
+
Clacky::Logger.debug("[ChannelManager] #{adapter_label(adapter)} adapter stopped: #{e.message}")
|
|
247
291
|
end
|
|
248
292
|
end
|
|
249
293
|
|
|
250
294
|
def route_message(adapter, event)
|
|
295
|
+
event[:channel_instance_id] ||= adapter.instance_id
|
|
296
|
+
|
|
251
297
|
if event[:observe_only]
|
|
252
298
|
return
|
|
253
299
|
end
|
|
@@ -277,6 +323,8 @@ module Clacky
|
|
|
277
323
|
return
|
|
278
324
|
end
|
|
279
325
|
|
|
326
|
+
return if @work_service&.receive_event(adapter.platform_id, event)
|
|
327
|
+
|
|
280
328
|
session_id = resolve_session(event)
|
|
281
329
|
if session_id
|
|
282
330
|
bind_key_to_session(channel_key(event), session_id)
|
|
@@ -422,7 +470,7 @@ module Clacky
|
|
|
422
470
|
end
|
|
423
471
|
end
|
|
424
472
|
else
|
|
425
|
-
channel_ui = ChannelUIController.new(event, -> { adapter_for(event[:platform]) }, -> { @channel_config.status_messages_enabled? }, -> { @channel_config.process_messages_enabled? }, -> { @channel_config.progress_cards_enabled? })
|
|
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? })
|
|
426
474
|
end
|
|
427
475
|
|
|
428
476
|
@persist_session&.call(cleared_agent) if cleared_agent
|
|
@@ -750,15 +798,14 @@ module Clacky
|
|
|
750
798
|
def auto_create_session(adapter, event)
|
|
751
799
|
key = channel_key(event)
|
|
752
800
|
platform = event[:platform].to_s
|
|
753
|
-
|
|
754
|
-
name = "#{platform}-#{count}"
|
|
801
|
+
name = next_session_name(platform)
|
|
755
802
|
session_id = @session_builder.call(name: name, source: :channel)
|
|
756
803
|
bind_key_to_session(key, session_id)
|
|
757
804
|
|
|
758
805
|
# Create a long-lived ChannelUIController for this session and subscribe it
|
|
759
806
|
# to the session's WebUIController. It stays for the session's full lifetime
|
|
760
807
|
# so all events (agent output, errors, status) flow through web_ui → channel_ui.
|
|
761
|
-
channel_ui = ChannelUIController.new(event, -> { adapter_for(event[:platform]) }, -> { @channel_config.status_messages_enabled? }, -> { @channel_config.process_messages_enabled? }, -> { @channel_config.progress_cards_enabled? })
|
|
808
|
+
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? })
|
|
762
809
|
@registry.with_session(session_id) do |s|
|
|
763
810
|
s[:ui]&.subscribe_channel(channel_ui)
|
|
764
811
|
s[:channel_ui] = channel_ui
|
|
@@ -768,6 +815,28 @@ module Clacky
|
|
|
768
815
|
session_id
|
|
769
816
|
end
|
|
770
817
|
|
|
818
|
+
# Channel session names resume from the highest "platform-N" already on
|
|
819
|
+
# disk. Counters are process-local, so without this seed a restart restarts
|
|
820
|
+
# numbering at 1 and produces duplicate names (e.g. two "weixin-1" rows).
|
|
821
|
+
private def next_session_name(platform)
|
|
822
|
+
@mutex.synchronize do
|
|
823
|
+
@session_counters[platform] ||= highest_session_number(platform)
|
|
824
|
+
@session_counters[platform] += 1
|
|
825
|
+
"#{platform}-#{@session_counters[platform]}"
|
|
826
|
+
end
|
|
827
|
+
end
|
|
828
|
+
|
|
829
|
+
private def highest_session_number(platform)
|
|
830
|
+
prefix = "#{platform}-"
|
|
831
|
+
@registry.list(archived: nil).map do |summary|
|
|
832
|
+
name = summary[:name].to_s
|
|
833
|
+
next unless name.start_with?(prefix)
|
|
834
|
+
|
|
835
|
+
tail = name[prefix.length..]
|
|
836
|
+
tail.to_i if tail.match?(/\A\d+\z/)
|
|
837
|
+
end.compact.max || 0
|
|
838
|
+
end
|
|
839
|
+
|
|
771
840
|
# Retrieve the ChannelUIController bound to a session (if any).
|
|
772
841
|
def channel_ui_for_session(session_id)
|
|
773
842
|
result = nil
|
|
@@ -785,7 +854,7 @@ module Clacky
|
|
|
785
854
|
end
|
|
786
855
|
return unless needs_attach
|
|
787
856
|
|
|
788
|
-
channel_ui = ChannelUIController.new(event, -> { adapter_for(event[:platform]) }, -> { @channel_config.status_messages_enabled? }, -> { @channel_config.process_messages_enabled? }, -> { @channel_config.progress_cards_enabled? })
|
|
857
|
+
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? })
|
|
789
858
|
@registry.with_session(session_id) do |s|
|
|
790
859
|
next unless s[:ui] && s[:channel_ui].nil?
|
|
791
860
|
s[:ui].subscribe_channel(channel_ui)
|
|
@@ -877,30 +946,74 @@ module Clacky
|
|
|
877
946
|
end
|
|
878
947
|
|
|
879
948
|
def channel_key(event)
|
|
880
|
-
|
|
949
|
+
prefix = channel_key_prefix(event[:platform], event[:channel_instance_id])
|
|
881
950
|
case @binding_mode
|
|
882
|
-
when :chat then "#{
|
|
883
|
-
when :user then "#{
|
|
951
|
+
when :chat then "#{prefix}:chat:#{event[:chat_id]}"
|
|
952
|
+
when :user then "#{prefix}:user:#{event[:user_id]}"
|
|
884
953
|
else # :chat_user
|
|
885
|
-
"#{
|
|
954
|
+
"#{prefix}:chat:#{event[:chat_id]}:user:#{event[:user_id]}"
|
|
886
955
|
end
|
|
887
956
|
end
|
|
888
957
|
|
|
889
958
|
def channel_key_from_info(channel_info)
|
|
890
|
-
|
|
959
|
+
prefix = channel_key_prefix(channel_info[:platform], channel_info[:channel_instance_id])
|
|
891
960
|
chat_id = channel_info[:chat_id].to_s
|
|
892
961
|
user_id = channel_info[:user_id].to_s
|
|
893
962
|
case @binding_mode
|
|
894
|
-
when :chat then "#{
|
|
895
|
-
when :user then "#{
|
|
963
|
+
when :chat then "#{prefix}:chat:#{chat_id}"
|
|
964
|
+
when :user then "#{prefix}:user:#{user_id}"
|
|
896
965
|
else # :chat_user
|
|
897
|
-
"#{
|
|
966
|
+
"#{prefix}:chat:#{chat_id}:user:#{user_id}"
|
|
967
|
+
end
|
|
968
|
+
end
|
|
969
|
+
|
|
970
|
+
# The first (default) instance keeps the historical key format
|
|
971
|
+
# "feishu:chat:xxx"; extra instances use "feishu#work:chat:xxx" so two
|
|
972
|
+
# apps of the same platform never share a binding.
|
|
973
|
+
private def channel_key_prefix(platform, instance_id)
|
|
974
|
+
platform = platform.to_s
|
|
975
|
+
id = instance_id.to_s
|
|
976
|
+
return platform if id.empty? || id == ChannelConfig::DEFAULT_INSTANCE_ID
|
|
977
|
+
"#{platform}##{id}"
|
|
978
|
+
end
|
|
979
|
+
|
|
980
|
+
# Strip the platform/instance prefix from a channel key, returning the
|
|
981
|
+
# binding payload ("chat:..." / "user:..."). nil when the key belongs to
|
|
982
|
+
# another platform or instance.
|
|
983
|
+
private def channel_key_payload(key, platform, instance_id)
|
|
984
|
+
platform = platform.to_s
|
|
985
|
+
if instance_id.nil? || instance_id.to_s.empty?
|
|
986
|
+
if key.start_with?("#{platform}:")
|
|
987
|
+
key.sub("#{platform}:", "")
|
|
988
|
+
elsif key.start_with?("#{platform}#")
|
|
989
|
+
key.sub("#{platform}#", "").split(":", 2)[1]
|
|
990
|
+
end
|
|
991
|
+
else
|
|
992
|
+
prefix = "#{channel_key_prefix(platform, instance_id)}:"
|
|
993
|
+
key.sub(prefix, "") if key.start_with?(prefix)
|
|
898
994
|
end
|
|
899
995
|
end
|
|
900
996
|
|
|
997
|
+
private def instance_label(platform, instance_id = nil)
|
|
998
|
+
id = instance_id.to_s
|
|
999
|
+
return ":#{platform}" if id.empty? || id == ChannelConfig::DEFAULT_INSTANCE_ID
|
|
1000
|
+
":#{platform}##{id}"
|
|
1001
|
+
end
|
|
1002
|
+
|
|
1003
|
+
private def adapter_label(adapter)
|
|
1004
|
+
instance_label(adapter.platform_id, adapter.instance_id)
|
|
1005
|
+
end
|
|
1006
|
+
|
|
1007
|
+
private def adapter_thread_name(platform, instance_id)
|
|
1008
|
+
id = instance_id.to_s
|
|
1009
|
+
return "channel-#{platform}" if id.empty? || id == ChannelConfig::DEFAULT_INSTANCE_ID
|
|
1010
|
+
"channel-#{platform}-#{id}"
|
|
1011
|
+
end
|
|
1012
|
+
|
|
901
1013
|
private def extract_channel_info(event)
|
|
902
1014
|
{
|
|
903
1015
|
platform: event[:platform],
|
|
1016
|
+
channel_instance_id: event[:channel_instance_id],
|
|
904
1017
|
user_id: event[:user_id],
|
|
905
1018
|
chat_id: event[:chat_id]
|
|
906
1019
|
}
|
|
@@ -953,7 +1066,7 @@ module Clacky
|
|
|
953
1066
|
next unless bound_keys.add?(key)
|
|
954
1067
|
bind_key_to_session(key, summary[:id])
|
|
955
1068
|
|
|
956
|
-
event = { platform: info[:platform], chat_id: info[:chat_id] }
|
|
1069
|
+
event = { platform: info[:platform], channel_instance_id: info[:channel_instance_id], chat_id: info[:chat_id] }
|
|
957
1070
|
ensure_channel_ui_subscribed(summary[:id], event)
|
|
958
1071
|
|
|
959
1072
|
Clacky::Logger.info("[ChannelManager] Restored channel binding #{key} -> session #{summary[:id][0, 8]}")
|
|
@@ -28,8 +28,9 @@ module Clacky
|
|
|
28
28
|
# bytes the caller passed in. Output is then normalised to UTF-8 because
|
|
29
29
|
# git inherits the locale, and under a non-UTF-8 locale the raw bytes come
|
|
30
30
|
# back tagged US-ASCII, where String#split would raise on multibyte paths.
|
|
31
|
+
# LC_ALL=C pins git's messages to English so the UI can recognise them.
|
|
31
32
|
def git(dir, *args)
|
|
32
|
-
out, err, status = Open3.capture3("git", "-C", dir.to_s, "-c", "core.quotePath=false", *args)
|
|
33
|
+
out, err, status = Open3.capture3({ "LC_ALL" => "C" }, "git", "-C", dir.to_s, "-c", "core.quotePath=false", *args)
|
|
33
34
|
[Utils::Encoding.to_utf8(out), Utils::Encoding.to_utf8(err), status.success?]
|
|
34
35
|
rescue StandardError => e
|
|
35
36
|
["", e.message, false]
|