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.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -0
  3. data/docs/secretary-prototype.md +286 -0
  4. data/lib/clacky/agent/session_serializer.rb +15 -11
  5. data/lib/clacky/agent.rb +58 -15
  6. data/lib/clacky/agent_config.rb +3 -1
  7. data/lib/clacky/agent_profile.rb +33 -6
  8. data/lib/clacky/billing/platform_billing.rb +6 -0
  9. data/lib/clacky/default_extensions/advisor/README.md +25 -0
  10. data/lib/clacky/default_extensions/advisor/README.zh.md +25 -0
  11. data/lib/clacky/default_extensions/advisor/ext.yml +1 -1
  12. data/lib/clacky/default_extensions/advisor/icon.svg +14 -0
  13. data/lib/clacky/default_extensions/coding/ext.yml +0 -1
  14. data/lib/clacky/default_extensions/computer-use/README.md +48 -0
  15. data/lib/clacky/default_extensions/computer-use/README.zh.md +48 -0
  16. data/lib/clacky/default_extensions/computer-use/ext.yml +1 -1
  17. data/lib/clacky/default_extensions/computer-use/icon.svg +16 -0
  18. data/lib/clacky/default_extensions/ext-studio/ext.yml +0 -1
  19. data/lib/clacky/default_extensions/general/ext.yml +0 -1
  20. data/lib/clacky/default_extensions/git/README.md +41 -0
  21. data/lib/clacky/default_extensions/git/README.zh.md +41 -0
  22. data/lib/clacky/default_extensions/git/ext.yml +1 -1
  23. data/lib/clacky/default_extensions/git/icon.svg +15 -0
  24. data/lib/clacky/default_extensions/git/panels/git/view.js +71 -23
  25. data/lib/clacky/default_extensions/meeting/README.md +46 -0
  26. data/lib/clacky/default_extensions/meeting/README.zh.md +47 -0
  27. data/lib/clacky/default_extensions/meeting/ext.yml +1 -1
  28. data/lib/clacky/default_extensions/meeting/icon.svg +13 -0
  29. data/lib/clacky/default_extensions/preview/README.md +29 -0
  30. data/lib/clacky/default_extensions/preview/README.zh.md +29 -0
  31. data/lib/clacky/default_extensions/preview/ext.yml +1 -1
  32. data/lib/clacky/default_extensions/preview/icon.svg +13 -0
  33. data/lib/clacky/default_extensions/secretary/agents/secretary.md +41 -0
  34. data/lib/clacky/default_extensions/secretary/agents/worker.md +35 -0
  35. data/lib/clacky/default_extensions/secretary/api/handler.rb +8 -0
  36. data/lib/clacky/default_extensions/secretary/ext.yml +39 -0
  37. data/lib/clacky/default_extensions/secretary/panels/work/view.js +344 -0
  38. data/lib/clacky/default_extensions/secretary/tools/delegate_work.rb +37 -0
  39. data/lib/clacky/default_extensions/secretary/tools/manage_work.rb +22 -0
  40. data/lib/clacky/default_extensions/secretary/tools/yield_work.rb +35 -0
  41. data/lib/clacky/default_extensions/time_machine/README.md +32 -0
  42. data/lib/clacky/default_extensions/time_machine/README.zh.md +32 -0
  43. data/lib/clacky/default_extensions/time_machine/ext.yml +1 -1
  44. data/lib/clacky/default_extensions/time_machine/icon.svg +14 -0
  45. data/lib/clacky/default_skills/channel-manager/SKILL.md +79 -8
  46. data/lib/clacky/default_skills/channel-manager/discord_setup.rb +5 -1
  47. data/lib/clacky/default_skills/channel-manager/weixin_setup.rb +8 -1
  48. data/lib/clacky/default_skills/cron-task-creator/SKILL.md +183 -206
  49. data/lib/clacky/default_skills/cron-task-creator/evals/evals.json +45 -0
  50. data/lib/clacky/default_skills/media-gen/SKILL.md +17 -8
  51. data/lib/clacky/extension/api_extension.rb +10 -2
  52. data/lib/clacky/extension/loader.rb +2 -0
  53. data/lib/clacky/extension/verifier.rb +2 -2
  54. data/lib/clacky/locales/en.rb +14 -0
  55. data/lib/clacky/locales/zh.rb +14 -0
  56. data/lib/clacky/prompts/base.md +1 -1
  57. data/lib/clacky/providers.rb +25 -14
  58. data/lib/clacky/server/channel/adapters/base.rb +18 -0
  59. data/lib/clacky/server/channel/adapters/feishu/message_parser.rb +1 -0
  60. data/lib/clacky/server/channel/adapters/telegram/adapter.rb +1 -0
  61. data/lib/clacky/server/channel/channel_config.rb +185 -48
  62. data/lib/clacky/server/channel/channel_manager.rb +179 -66
  63. data/lib/clacky/server/git_panel.rb +2 -1
  64. data/lib/clacky/server/http_server.rb +385 -134
  65. data/lib/clacky/server/scheduler.rb +166 -85
  66. data/lib/clacky/session_manager.rb +4 -1
  67. data/lib/clacky/tools/base.rb +7 -1
  68. data/lib/clacky/utils/file_processor.rb +20 -10
  69. data/lib/clacky/version.rb +1 -1
  70. data/lib/clacky/web/app.css +401 -46
  71. data/lib/clacky/web/app.js +85 -3
  72. data/lib/clacky/web/components/code-editor.js +73 -4
  73. data/lib/clacky/web/components/model-picker.js +1 -1
  74. data/lib/clacky/web/components/notify.js +37 -10
  75. data/lib/clacky/web/components/onboard.js +1 -1
  76. data/lib/clacky/web/features/channels/store.js +19 -4
  77. data/lib/clacky/web/features/channels/view.js +87 -26
  78. data/lib/clacky/web/features/extensions/view.js +15 -8
  79. data/lib/clacky/web/features/new-session/view.js +23 -20
  80. data/lib/clacky/web/features/tasks/store.js +5 -3
  81. data/lib/clacky/web/features/tasks/view.js +98 -26
  82. data/lib/clacky/web/features/workspace/view.js +193 -57
  83. data/lib/clacky/web/i18n.js +128 -12
  84. data/lib/clacky/web/index.html +25 -10
  85. data/lib/clacky/web/sessions.js +82 -41
  86. data/lib/clacky/web/settings.js +215 -45
  87. data/lib/clacky/web/utils.js +217 -0
  88. data/lib/clacky/web/ws-dispatcher.js +7 -3
  89. data/lib/clacky/work/service.rb +542 -0
  90. data/lib/clacky/work/store.rb +57 -0
  91. metadata +30 -1
@@ -33,6 +33,51 @@
33
33
  "显示了变更前后的对比",
34
34
  "任务文件内容未被修改"
35
35
  ]
36
+ },
37
+ {
38
+ "id": 4,
39
+ "prompt": "当前是普通会话。帮我每天早上9点生成一份早报,在独立执行会话里查看。",
40
+ "expected_output": "使用原有 cron API 创建普通任务,不因早报名称而添加秘书归属。",
41
+ "assertions": [
42
+ "POST 不包含 owner_session_id",
43
+ "使用 cron 0 9 * * *",
44
+ "说明独立执行会话的结果位置,不承诺回到当前对话"
45
+ ]
46
+ },
47
+ {
48
+ "id": 5,
49
+ "prompt": "当前是秘书会话,CLACKY_SESSION_ID 是有效秘书会话。每天晚上8点汇总我的工作进展,并在这里告诉我。",
50
+ "expected_output": "通过同一个 cron skill/API 创建有秘书归属的计划。",
51
+ "assertions": [
52
+ "从环境读取实际 CLACKY_SESSION_ID 作为 owner_session_id",
53
+ "使用 cron 0 20 * * *",
54
+ "不调用 manage_work 创建计划",
55
+ "说明结果回到归属秘书会话",
56
+ "不要求 timezone 参数"
57
+ ]
58
+ },
59
+ {
60
+ "id": 6,
61
+ "prompt": "当前是秘书会话。把旧任务 daily_file_summary 改到每天18点。GET 显示该计划没有 target。",
62
+ "expected_output": "只 PATCH cron,保留普通任务执行方式。",
63
+ "assertions": [
64
+ "使用 PATCH 而非删除重建",
65
+ "只修改 cron 为 0 18 * * *",
66
+ "不添加 owner_session_id 或改变执行类型",
67
+ "不修改任务内容"
68
+ ]
69
+ },
70
+ {
71
+ "id": 7,
72
+ "prompt": "当前是普通会话。查看 morning_report 的结果并立刻运行一次。GET 显示该计划 target.type 为 secretary_work,归属会话为 secretary_a。",
73
+ "expected_output": "依据保存的秘书目标选择工作历史和运行结果目的地。",
74
+ "assertions": [
75
+ "通过归属会话的 work API 并按 schedule_name 查询工作记录",
76
+ "调用原 cron run API",
77
+ "说明结果仍回到 secretary_a,不回到当前会话",
78
+ "不将缺少 cron last_run 当作没有执行过",
79
+ "409 时不绕过限制创建普通任务"
80
+ ]
36
81
  }
37
82
  ]
38
83
  }
@@ -190,10 +190,19 @@ When the user gives a vague request like "给我配张图", ask one clarifying q
190
190
  - The user wants a **diagram / chart** with specific data — use a charting library (matplotlib, mermaid, etc.) instead; image gen is for illustrations, not data viz
191
191
  - The user asks for **screenshots** of real software — use the browser tool
192
192
 
193
- ## Generating video (Veo)
193
+ ## Generating video (Gemini Omni / Veo)
194
194
 
195
195
  The same `/api/media/` namespace serves video generation. The user must
196
- configure a `type=video` model in settings (recommended: `or-veo-3-1`).
196
+ configure a `type=video` model in settings (recommended: `or-gemini-omni-flash`).
197
+
198
+ | Model | Duration | Notes |
199
+ |------------------------|----------|-------|
200
+ | `or-gemini-omni-flash` | 3–15 s | Default. Newest, ~$0.07/s at 720p. |
201
+ | `or-veo-3-1` | 4–8 s | Best cinematic quality, ~$0.40/s. |
202
+ | `or-veo-3-1-fast` | 4–8 s | ~$0.10/s. |
203
+ | `or-veo-3-1-lite` | 4–8 s | Cheapest, ~$0.05/s. |
204
+
205
+ Out-of-range durations are clamped by the gateway, not rejected.
197
206
 
198
207
  ### Endpoint
199
208
 
@@ -231,7 +240,7 @@ curl -s -X POST http://${CLACKY_SERVER_HOST}:${CLACKY_SERVER_PORT}/api/media/vid
231
240
  |--------------------|----------|---------------------------------|-------|
232
241
  | `prompt` | yes | string | Same prompt-craft tips as images apply. |
233
242
  | `aspect_ratio` | no | `landscape` / `portrait` | Defaults to `landscape` (16:9). |
234
- | `duration_seconds` | no | 4–8 | Defaults to 8. |
243
+ | `duration_seconds` | no | 3–15 (Omni) / 4–8 (Veo) | Defaults to 8. |
235
244
  | `image` | no | `{ "b64_json": "...", "mime_type": "image/png" }` | Optional first frame for image-to-video. |
236
245
  | `output_dir` | no | absolute path | The exact directory the file is written to — nothing is appended. Use the directory the user asked for; otherwise `$(pwd)/assets/generated` as shown in the example. |
237
246
  | `session_id` | yes | string | Current Clacky session ID. Always pass the rendered value shown in the request example. |
@@ -262,10 +271,10 @@ Same shape and `error_type` values as image generation, but with `"video": null`
262
271
 
263
272
  ### Continuous / long video (last-frame chaining)
264
273
 
265
- A single Veo call maxes out at 8 seconds, and separate calls are visually
274
+ A single call maxes out at 15 seconds (Omni) or 8 seconds (Veo), and separate calls are visually
266
275
  **unrelated** (the character, lighting and framing jump between clips). To make
267
276
  several clips flow as one continuous shot, chain them: take the **last frame**
268
- of clip N and feed it as the `image` (first frame) of clip N+1. Veo's
277
+ of clip N and feed it as the `image` (first frame) of clip N+1. The model's
269
278
  image-to-video then continues from exactly where the previous clip ended, so
270
279
  the seam is smooth.
271
280
 
@@ -320,9 +329,9 @@ Rules & caveats:
320
329
  slower re-encode (and may letterbox). Use the same `aspect_ratio` everywhere.
321
330
  - **Cost adds up linearly** — N segments ≈ N × single-clip price. Confirm the
322
331
  number of segments and total length with the user before starting.
323
- - For >30s or a true single-take >8s with no seam at all, this client-side
324
- chaining is the practical option today; Veo's native server-side `extend`
325
- (148s) is not wired into this endpoint yet.
332
+ - For >30s or a true single-take longer than one clip with no seam at all,
333
+ this client-side chaining is the practical option today; the native
334
+ server-side `extend` (Veo / Omni) is not wired into this endpoint yet.
326
335
 
327
336
  ### Seedance — multimodal video
328
337
 
@@ -250,6 +250,10 @@ module Clacky
250
250
  self.class.meta["config"] || {}
251
251
  end
252
252
 
253
+ def work_service
254
+ @http_server&.work_service
255
+ end
256
+
253
257
  def session_manager
254
258
  @http_server&.instance_variable_get(:@session_manager)
255
259
  end
@@ -329,11 +333,13 @@ module Clacky
329
333
  end
330
334
 
331
335
  # Submit a prompt to an existing session for execution.
336
+ # prompt is model input; display_message overrides its visible text; reference_contexts
337
+ # supply both UI badges and model reference context.
332
338
  # Returns the session_id on success.
333
339
  # Raises Halt (409) if the session is already running and `interrupt: false`.
334
340
  # When `interrupt: true`, supersedes the current turn (raises AgentInterrupted
335
341
  # on the running thread, waits up to 2s for it to exit) then runs the new task.
336
- def submit_task(session_id, prompt, display_message: nil, interrupt: false)
342
+ def submit_task(session_id, prompt, display_message: nil, interrupt: false, reference_contexts: nil)
337
343
  reg = registry
338
344
  error!("server not ready", status: 503) unless reg
339
345
 
@@ -351,7 +357,9 @@ module Clacky
351
357
  old_thread&.join(2)
352
358
  end
353
359
 
354
- @http_server.send(:run_session_task, session_id, prompt, display_message: display_message)
360
+ options = { display_message: display_message }
361
+ options[:reference_contexts] = reference_contexts if reference_contexts
362
+ @http_server.send(:run_session_task, session_id, prompt, **options)
355
363
  session_id
356
364
  end
357
365
 
@@ -502,6 +502,8 @@ module Clacky
502
502
  "title_zh" => spec["title_zh"].to_s,
503
503
  "prompt" => spec["prompt"],
504
504
  "prompt_abs" => prompt_abs,
505
+ "base_agent" => spec["base_agent"],
506
+ "work_agent" => spec["work_agent"],
505
507
  "avatar" => avatar_rel,
506
508
  "avatar_abs" => avatar_abs,
507
509
  "description" => spec["description"].to_s,
@@ -16,13 +16,13 @@ module Clacky
16
16
  module ExtensionVerifier
17
17
  Issue = Struct.new(:ext, :unit, :level, :code, :message, :file, :hint, keyword_init: true)
18
18
 
19
- KNOWN_TOP_KEYS = %w[id name name_zh display_name display_name_zh title description description_zh emoji version origin author homepage license public license_required keywords enabled_by_default config contributes].freeze
19
+ KNOWN_TOP_KEYS = %w[id name name_zh display_name display_name_zh title description description_zh emoji icon version origin author homepage license public license_required keywords enabled_by_default config contributes].freeze
20
20
  KNOWN_CONTRIBUTES = %w[panels api skills agents channels patches hooks tools].freeze
21
21
 
22
22
  PANEL_KEYS = %w[id title title_zh description description_zh view order attach entry_points].freeze
23
23
  API_KEYS = %w[id handler].freeze
24
24
  SKILL_KEYS = %w[id dir protected].freeze
25
- AGENT_KEYS = %w[id title title_zh description description_zh order prompt panels skills disabled_skills avatar hidden tools].freeze
25
+ AGENT_KEYS = %w[id base_agent work_agent title title_zh description description_zh order prompt panels skills disabled_skills avatar hidden tools].freeze
26
26
  CHANNEL_KEYS = %w[id platform adapter].freeze
27
27
  PATCH_KEYS = %w[target file fingerprint on_mismatch].freeze
28
28
  HOOK_KEYS = %w[event file].freeze
@@ -3,6 +3,20 @@
3
3
  module Clacky
4
4
  module Locales
5
5
  EN = {
6
+ "work.delegating" => "Delegate: %<title>s",
7
+ "work.delegated" => "Created work item “%<title>s”, waiting to start. Follow its progress in the Work panel.",
8
+ "work.queued" => "Waiting to start",
9
+ "work.cancelled" => "Cancelled by you",
10
+ "work.paused" => "Paused; add instructions to continue",
11
+ "work.interrupted" => "Interrupted by a server restart. Review progress and add instructions to continue.",
12
+ "work.running" => "In progress",
13
+ "work.started" => "Starting: %<title>s",
14
+ "work.missing_result" => "Execution ended without a yield_work result. Review the execution history.",
15
+ "work.completed" => "Work completed: %<title>s",
16
+ "work.needs_user" => "Your decision needed: %<title>s",
17
+ "work.failed" => "Work needs attention: %<title>s",
18
+ "work.reply_received" => "Reply received; waiting to resume",
19
+
6
20
  "phase.skill_evolution" => "Reflecting on this task",
7
21
  "phase.memory_update" => "Updating long-term memory",
8
22
  "llm.error.insufficient_credit" => "Insufficient credit, please top up your account to continue",
@@ -3,6 +3,20 @@
3
3
  module Clacky
4
4
  module Locales
5
5
  ZH = {
6
+ "work.delegating" => "委派:%<title>s",
7
+ "work.delegated" => "已创建工作事项「%<title>s」,等待执行。可在工作事项面板跟进。",
8
+ "work.queued" => "等待执行",
9
+ "work.cancelled" => "用户已取消",
10
+ "work.paused" => "用户已暂停,补充要求后可继续",
11
+ "work.interrupted" => "执行因服务重启中断,请核实进展后补充要求以继续。",
12
+ "work.running" => "执行中",
13
+ "work.started" => "开始处理:%<title>s",
14
+ "work.missing_result" => "执行结束但没有调用 yield_work 交回结果,请检查执行记录。",
15
+ "work.completed" => "工作已完成:%<title>s",
16
+ "work.needs_user" => "需要你确认:%<title>s",
17
+ "work.failed" => "工作需要处理:%<title>s",
18
+ "work.reply_received" => "收到回复,等待继续执行",
19
+
6
20
  "phase.skill_evolution" => "正在复盘本次任务",
7
21
  "phase.memory_update" => "正在更新长期记忆",
8
22
  "llm.error.insufficient_credit" => "API Key 账户余额不足,请前往控制台充值后继续使用",
@@ -3,7 +3,7 @@
3
3
  - Ask clarifying questions if requirements are unclear.
4
4
  - Break down complex tasks into manageable steps.
5
5
  - **USE TOOLS to create/modify files** — don't just return content.
6
- - When the user asks to send/download a file or you generate one for them, append `[filename](file:///absolute/path/to/file)` at the end of your reply. Use the exact absolute path the write tool returned — don't rewrite or abbreviate it with `~` (keep non-ASCII characters and spaces literal); never URL-encode it.
6
+ - When the user asks to send/download a file or you generate one for them, append `[filename](file:///absolute/path/to/file)` at the end of your reply (for images use `![filename](file:///absolute/path/to/image)` so they show inline). Use the exact absolute path the write tool returned — don't rewrite or abbreviate it with `~` (keep non-ASCII characters and spaces literal); never URL-encode it.
7
7
 
8
8
  ## Tool Usage Rules
9
9
 
@@ -69,32 +69,43 @@ module Clacky
69
69
  # only — the platform may dispatch to OpenRouter or Vertex AI
70
70
  # (Gemini Nano Banana family) depending on the model.
71
71
  "image_models" => [
72
+ "or-gemini-nano-banana-2-1",
72
73
  "or-gemini-3-pro-image",
73
74
  "or-gemini-3-1-flash-image",
75
+ "or-gemini-3-1-flash-lite-image",
76
+ "or-gpt-image-2-5",
77
+ "or-gpt-image-2-5-flare",
74
78
  "or-gpt-image-2"
75
79
  ],
76
80
  "image_model_aliases" => {
77
- "or-gemini-3-pro-image" => "Nano Banana Pro",
78
- "or-gemini-3-1-flash-image" => "Nano Banana 2",
79
- "or-gpt-image-2" => "GPT Image 2"
81
+ "or-gemini-nano-banana-2-1" => "Nano Banana 2.1",
82
+ "or-gemini-3-pro-image" => "Nano Banana Pro",
83
+ "or-gemini-3-1-flash-image" => "Nano Banana 2",
84
+ "or-gemini-3-1-flash-lite-image" => "Nano Banana 2 Lite",
85
+ "or-gpt-image-2-5" => "GPT Image 2.5",
86
+ "or-gpt-image-2-5-flare" => "GPT Image 2.5 Flare",
87
+ "or-gpt-image-2" => "GPT Image 2"
80
88
  },
81
- "default_image_model" => "or-gemini-3-1-flash-image",
82
- # Video generation models served by the openclacky gateway, which
83
- # routes them to Vertex AI Veo (async predictLongRunning under the
84
- # hood; the gateway hides the polling and returns the MP4 inline).
89
+ "default_image_model" => "or-gemini-nano-banana-2-1",
90
+ # Video generation models served by the openclacky gateway. Gemini
91
+ # Omni Flash rides Vertex's Interactions API (token-billed, 3–15s);
92
+ # the Veo 3.1 family uses async predictLongRunning (per-second, 4–8s).
93
+ # The gateway hides the polling and returns the MP4 inline for both.
94
+ # Veo 3 / Veo 3 Fast are still routable server-side for old configs
95
+ # but no longer offered: same price as 3.1 with older quality.
85
96
  "video_models" => [
97
+ "or-gemini-omni-flash",
86
98
  "or-veo-3-1",
87
99
  "or-veo-3-1-fast",
88
- "or-veo-3",
89
- "or-veo-3-fast"
100
+ "or-veo-3-1-lite"
90
101
  ],
91
102
  "video_model_aliases" => {
92
- "or-veo-3-1" => "Veo 3.1",
93
- "or-veo-3-1-fast" => "Veo 3.1 Fast",
94
- "or-veo-3" => "Veo 3",
95
- "or-veo-3-fast" => "Veo 3 Fast"
103
+ "or-gemini-omni-flash" => "Gemini Omni Flash",
104
+ "or-veo-3-1" => "Veo 3.1",
105
+ "or-veo-3-1-fast" => "Veo 3.1 Fast",
106
+ "or-veo-3-1-lite" => "Veo 3.1 Lite"
96
107
  },
97
- "default_video_model" => "or-veo-3-1",
108
+ "default_video_model" => "or-gemini-omni-flash",
98
109
  # Text-to-speech models served by the openclacky gateway, which
99
110
  # routes them to Vertex AI Gemini 2.5 (responseModalities=["AUDIO"]).
100
111
  # The gateway returns WAV inline as base64.
@@ -26,6 +26,24 @@ module Clacky
26
26
  # Base adapter interface for IM platforms.
27
27
  # Subclasses must implement every abstract method below.
28
28
  class Base
29
+ DEFAULT_INSTANCE_ID = "default"
30
+
31
+ attr_writer :instance_id, :instance_name
32
+
33
+ # Identifier of the configured instance this adapter serves. The first
34
+ # instance of a platform keeps the historical id "default".
35
+ # @return [String]
36
+ def instance_id
37
+ id = @instance_id.to_s
38
+ id.empty? ? DEFAULT_INSTANCE_ID : id
39
+ end
40
+
41
+ # Human-readable name of this instance (e.g. "Company Feishu").
42
+ # @return [String]
43
+ def instance_name
44
+ @instance_name.to_s
45
+ end
46
+
29
47
  # @return [Symbol] e.g. :feishu, :wecom
30
48
  def self.platform_id
31
49
  raise NotImplementedError, "#{self} must implement .platform_id"
@@ -112,6 +112,7 @@ module Clacky
112
112
  file_attachments: file_attachments,
113
113
  doc_urls: extract_doc_urls(text),
114
114
  message_id: message_id,
115
+ reply_to: message["parent_id"],
115
116
  timestamp: timestamp,
116
117
  chat_type: chat_type,
117
118
  mentioned_open_ids: Array(message["mentions"]).filter_map { |m| m.dig("id", "open_id") },
@@ -276,6 +276,7 @@ module Clacky
276
276
  text: text.strip,
277
277
  files: files,
278
278
  message_id: msg["message_id"].to_s,
279
+ reply_to: msg.dig("reply_to_message", "message_id")&.to_s,
279
280
  timestamp: msg["date"] ? Time.at(msg["date"]) : Time.now,
280
281
  chat_type: is_group ? :group : :direct,
281
282
  raw: msg
@@ -7,20 +7,30 @@ require "json"
7
7
  module Clacky
8
8
  # ChannelConfig manages IM platform credentials (Feishu, WeCom, etc.).
9
9
  #
10
- # Config is stored in ~/.clacky/channels.yml:
10
+ # Config is stored in ~/.clacky/channels.yml. Every platform holds a list of
11
+ # instances, so one platform can be connected more than once (e.g. a personal
12
+ # and a work Feishu app):
11
13
  #
12
14
  # channels:
13
15
  # feishu:
14
- # enabled: true
15
- # app_id: cli_xxx
16
- # app_secret: xxx
17
- # domain: https://open.feishu.cn
18
- # allowed_users:
19
- # - ou_xxx
16
+ # - id: default
17
+ # name: Personal
18
+ # enabled: true
19
+ # app_id: cli_xxx
20
+ # app_secret: xxx
21
+ # - id: work
22
+ # name: Company
23
+ # enabled: true
24
+ # app_id: cli_yyy
25
+ # app_secret: yyy
20
26
  # wecom:
21
- # enabled: false
22
- # bot_id: xxx
23
- # secret: xxx
27
+ # - id: default
28
+ # enabled: false
29
+ # bot_id: xxx
30
+ # secret: xxx
31
+ #
32
+ # The legacy single-hash form (`feishu: { enabled: true, ... }`) is migrated
33
+ # in memory on load: it becomes one instance with id "default".
24
34
  #
25
35
  # This class is only responsible for platform credentials.
26
36
  # working_dir and permission_mode live in AgentConfig.
@@ -28,7 +38,12 @@ module Clacky
28
38
  CONFIG_DIR = File.join(Dir.home, ".clacky")
29
39
  CONFIG_FILE = File.join(CONFIG_DIR, "channels.yml")
30
40
 
31
- # @param channels [Hash<String, Hash>] string-keyed platform configs (raw from YAML)
41
+ DEFAULT_INSTANCE_ID = "default"
42
+
43
+ META_KEYS = %w[id name enabled].freeze
44
+
45
+ # @param channels [Hash<String, Array<Hash>|Hash>] platform configs (raw from
46
+ # YAML; a Hash value is treated as a legacy single-instance config)
32
47
  # @param status_messages [Boolean] global toggle for process-status messages
33
48
  # ("Thinking...", "Done" summary) sent to IM chats
34
49
  # @param process_messages [Boolean] global toggle for tool-process messages
@@ -36,7 +51,7 @@ module Clacky
36
51
  # @param progress_cards [Boolean] global toggle for live-updating progress
37
52
  # cards on platforms that support them
38
53
  def initialize(channels: {}, status_messages: false, process_messages: false, progress_cards: true)
39
- @channels = channels || {}
54
+ @channels = normalize_channels(channels || {})
40
55
  @status_messages = status_messages == true ? true : false
41
56
  @process_messages = process_messages == true ? true : false
42
57
  # Opt-out (unlike the toggles above): missing key keeps cards on for existing users.
@@ -70,34 +85,78 @@ module Clacky
70
85
  YAML.dump({ "status_messages" => @status_messages, "process_messages" => @process_messages, "progress_cards" => @progress_cards, "channels" => @channels })
71
86
  end
72
87
 
73
- # Returns true if at least one channel is enabled.
88
+ # Returns true if at least one instance of any platform is enabled.
74
89
  def any_enabled?
75
- @channels.any? { |_, cfg| cfg["enabled"] }
90
+ @channels.any? { |_, list| list.any? { |inst| inst["enabled"] } }
76
91
  end
77
92
 
78
- # Returns the list of enabled platform symbols.
93
+ # Returns the list of platform symbols that have at least one enabled instance.
79
94
  # @return [Array<Symbol>]
80
95
  def enabled_platforms
81
- @channels
82
- .select { |_, cfg| cfg["enabled"] }
83
- .keys
84
- .map(&:to_sym)
96
+ enabled_instances.map { |platform, _| platform }.uniq
97
+ end
98
+
99
+ # All enabled instances as [platform_symbol, instance_hash] pairs.
100
+ # @return [Array<Array(Symbol, Hash)>]
101
+ def enabled_instances
102
+ result = []
103
+ @channels.each do |platform, list|
104
+ list.each do |inst|
105
+ result << [platform.to_sym, inst] if inst["enabled"]
106
+ end
107
+ end
108
+ result
85
109
  end
86
110
 
87
- # Returns true if the given platform is configured and enabled.
111
+ # Returns true if the platform (or one specific instance) is configured and
112
+ # enabled.
88
113
  # @param platform [Symbol, String]
89
- def enabled?(platform)
90
- cfg = @channels[platform.to_s]
91
- cfg && cfg["enabled"]
114
+ # @param instance_id [String, nil] nil means "any instance of this platform"
115
+ def enabled?(platform, instance_id = nil)
116
+ list = instances(platform)
117
+ return false if list.empty?
118
+
119
+ if instance_id.nil? || instance_id.to_s.empty?
120
+ list.any? { |inst| inst["enabled"] }
121
+ else
122
+ inst = instance(platform, instance_id)
123
+ !!(inst && inst["enabled"])
124
+ end
125
+ end
126
+
127
+ # Configured instances for a platform.
128
+ # @param platform [Symbol, String]
129
+ # @return [Array<Hash>] string-keyed instance hashes
130
+ def instances(platform)
131
+ @channels[platform.to_s] || []
132
+ end
133
+
134
+ # Look up one instance. A nil/blank id resolves to the first instance,
135
+ # which keeps legacy single-instance callers working unchanged.
136
+ # @param platform [Symbol, String]
137
+ # @param instance_id [String, nil]
138
+ # @return [Hash, nil]
139
+ def instance(platform, instance_id = nil)
140
+ list = instances(platform)
141
+ return nil if list.empty?
142
+ return list.first if instance_id.nil? || instance_id.to_s.empty?
143
+
144
+ list.find { |inst| inst["id"] == instance_id.to_s }
145
+ end
146
+
147
+ # @return [Array<String>] instance ids for a platform
148
+ def instance_ids(platform)
149
+ instances(platform).map { |inst| inst["id"] }
92
150
  end
93
151
 
94
152
  # Return the symbol-keyed config hash expected by each adapter's initializer.
95
- # Returns nil if the platform is not configured.
153
+ # Returns nil if the platform/instance is not configured.
96
154
  #
97
155
  # @param platform [Symbol, String]
156
+ # @param instance_id [String, nil] nil resolves to the first instance
98
157
  # @return [Hash, nil]
99
- def platform_config(platform)
100
- raw = @channels[platform.to_s]
158
+ def platform_config(platform, instance_id = nil)
159
+ raw = instance(platform, instance_id)
101
160
  return nil unless raw
102
161
 
103
162
  case platform.to_sym
@@ -147,22 +206,42 @@ module Clacky
147
206
  }.compact
148
207
  else
149
208
  # Unknown platform — pass all non-meta keys as symbol-keyed hash
150
- raw.reject { |k, _| k == "enabled" }
209
+ raw.reject { |k, _| META_KEYS.include?(k) }
151
210
  .transform_keys(&:to_sym)
152
211
  end
153
212
  end
154
213
 
155
- # Set or update a platform's credentials.
156
- # Merges provided fields into the existing entry.
214
+ # Set or update an instance's credentials.
215
+ # Merges provided fields into the existing entry; creates a new instance
216
+ # when the id does not exist yet.
157
217
  # Automatically sets enabled: true unless explicitly provided.
158
218
  #
159
219
  # @param platform [Symbol, String]
220
+ # @param instance_id [String, nil] nil updates the first instance (creating
221
+ # the default one when the platform has none)
222
+ # @param name [String, nil] display name for this instance
223
+ # @param enabled [Boolean, nil] leave untouched when nil
160
224
  # @param fields [Hash] symbol-keyed credential fields
161
- def set_platform(platform, **fields)
162
- key = platform.to_s
163
- @channels[key] ||= {}
164
- fields.each { |k, v| @channels[key][k.to_s] = v }
165
- @channels[key]["enabled"] = true unless @channels[key].key?("enabled")
225
+ # @return [String] the instance id that was written
226
+ def set_platform(platform, instance_id: nil, name: nil, enabled: nil, **fields)
227
+ key = platform.to_s
228
+ inst = instance(key, instance_id)
229
+
230
+ if inst.nil?
231
+ inst = { "id" => instance_id.to_s.empty? ? next_instance_id(key) : instance_id.to_s }
232
+ @channels[key] ||= []
233
+ @channels[key] << inst
234
+ end
235
+
236
+ fields.each do |k, v|
237
+ k = k.to_s
238
+ next if META_KEYS.include?(k)
239
+ inst[k] = v
240
+ end
241
+ inst["name"] = name unless name.nil?
242
+ inst["enabled"] = enabled unless enabled.nil?
243
+ inst["enabled"] = true if enabled.nil? && !inst.key?("enabled")
244
+ inst["id"]
166
245
  end
167
246
 
168
247
  # Global toggle: whether process-status messages ("Thinking...", "Done"
@@ -202,27 +281,47 @@ module Clacky
202
281
  @progress_cards = enabled ? true : false
203
282
  end
204
283
 
205
- # Enable a platform (requires it to already be configured).
284
+ # Enable an instance (requires it to already be configured).
206
285
  # @param platform [Symbol, String]
207
- # @raise [ArgumentError] if the platform has no stored credentials yet.
208
- def enable_platform(platform)
209
- key = platform.to_s
210
- raise ArgumentError, "Platform #{platform} is not configured" unless @channels.key?(key)
211
- @channels[key]["enabled"] = true
286
+ # @param instance_id [String, nil] nil enables the first instance
287
+ # @raise [ArgumentError] if the instance has no stored credentials yet.
288
+ def enable_platform(platform, instance_id = nil)
289
+ inst = instance(platform, instance_id)
290
+ raise ArgumentError, "Platform #{platform} is not configured" unless inst
291
+ inst["enabled"] = true
212
292
  end
213
293
 
214
- # Disable a platform (keeps credentials, just sets enabled: false).
294
+ # Disable an instance (keeps credentials, just sets enabled: false).
295
+ # Without an id, every instance of the platform is disabled.
215
296
  # @param platform [Symbol, String]
216
- def disable_platform(platform)
217
- key = platform.to_s
218
- return unless @channels.key?(key)
219
- @channels[key]["enabled"] = false
297
+ # @param instance_id [String, nil]
298
+ def disable_platform(platform, instance_id = nil)
299
+ targets = if instance_id.nil? || instance_id.to_s.empty?
300
+ instances(platform)
301
+ else
302
+ [instance(platform, instance_id)].compact
303
+ end
304
+ targets.each { |inst| inst["enabled"] = false }
220
305
  end
221
306
 
222
- # Remove a platform entry entirely.
307
+ # Remove an instance entirely. Without an id, the whole platform entry is
308
+ # removed.
223
309
  # @param platform [Symbol, String]
224
- def remove_platform(platform)
225
- @channels.delete(platform.to_s)
310
+ # @param instance_id [String, nil]
311
+ # @return [Boolean] true when something was removed
312
+ def remove_platform(platform, instance_id = nil)
313
+ key = platform.to_s
314
+ return false unless @channels.key?(key)
315
+
316
+ if instance_id.nil? || instance_id.to_s.empty?
317
+ @channels.delete(key)
318
+ return true
319
+ end
320
+
321
+ list = @channels[key]
322
+ removed = list.reject! { |inst| inst["id"] == instance_id.to_s }
323
+ @channels.delete(key) if list.empty?
324
+ !removed.nil?
226
325
  end
227
326
 
228
327
  # Deep copy - prevents callers from mutating shared config state.
@@ -230,5 +329,43 @@ module Clacky
230
329
  def deep_copy
231
330
  self.class.new(channels: JSON.parse(JSON.generate(@channels)), status_messages: @status_messages, process_messages: @process_messages, progress_cards: @progress_cards)
232
331
  end
332
+
333
+ private def normalize_channels(raw)
334
+ result = {}
335
+ raw.each do |platform, value|
336
+ result[platform.to_s] = normalize_instances(platform.to_s, value)
337
+ end
338
+ result
339
+ end
340
+
341
+ private def normalize_instances(platform, value)
342
+ list = case value
343
+ when Array then value
344
+ when Hash then [value]
345
+ else []
346
+ end
347
+
348
+ used_ids = []
349
+ list.each_with_index.map do |item, index|
350
+ inst = (item || {}).each_with_object({}) { |(k, v), out| out[k.to_s] = v }
351
+ id = inst["id"].to_s
352
+ id = index.zero? ? DEFAULT_INSTANCE_ID : "#{platform}-#{index + 1}" if id.empty?
353
+ id = "#{id}-#{index + 1}" while used_ids.include?(id)
354
+ used_ids << id
355
+ inst["id"] = id
356
+ inst["enabled"] = false unless inst.key?("enabled")
357
+ inst
358
+ end
359
+ end
360
+
361
+ private def next_instance_id(platform)
362
+ list = instances(platform)
363
+ return DEFAULT_INSTANCE_ID if list.empty?
364
+
365
+ used = list.map { |inst| inst["id"] }
366
+ index = list.length + 1
367
+ index += 1 while used.include?("#{platform}-#{index}")
368
+ "#{platform}-#{index}"
369
+ end
233
370
  end
234
371
  end