ai_co_discuss 0.5.21 → 0.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.
- package/.agents/skills/codiscuss-draft-cleanup/SKILL.md +16 -0
- package/.agents/skills/codiscuss-draft-cleanup/cleanup.py +70 -0
- package/.agents/skills/codiscuss-draft-cleanup/scan.py +70 -0
- package/.agents/skills/discuss-host/references/control.md +2 -2
- package/.agents/skills/discuss-host/references/workflow.md +4 -2
- package/.agents/skills/discuss-participant/SKILL.md +2 -0
- package/.agents/skills/local-stt-setup/SKILL.md +9 -8
- package/.agents/skills/local-tts-setup/SKILL.md +10 -9
- package/.agents/skills/share-card/SKILL.md +7 -6
- package/.agents/skills/share-card/references/share-card.md +34 -8
- package/.agents/skills/workflow-creator/SKILL.md +22 -14
- package/README.md +2 -0
- package/README.zh-CN.md +2 -0
- package/backend/free_room.py +64 -8
- package/backend/message_queue.py +9 -0
- package/backend/schema.sql +13 -1
- package/backend/server.py +289 -59
- package/docs/en/guide/admin.md +18 -0
- package/docs/en/guide/members.md +7 -1
- package/docs/en/guide/remote.md +1 -1
- package/docs/en/guide/rooms.md +14 -6
- package/docs/en/guide/voice.md +2 -0
- package/docs/en/guide/workflows.md +9 -5
- package/docs/en/guide/working.md +6 -4
- package/docs/guide/admin.md +18 -0
- package/docs/guide/members.md +7 -1
- package/docs/guide/remote.md +1 -1
- package/docs/guide/rooms.md +13 -6
- package/docs/guide/voice.md +2 -0
- package/docs/guide/workflows.md +9 -5
- package/docs/guide/working.md +6 -4
- package/frontend/locales/en/admin.json +58 -1
- package/frontend/locales/en/app.json +18 -1
- package/frontend/locales/en/backend.json +6 -0
- package/frontend/locales/en/core.json +4 -2
- package/frontend/locales/en/discussion.json +4 -1
- package/frontend/locales/en/room.json +85 -1
- package/gateway/gateway.mjs +49 -9
- package/package.json +3 -1
- package/runner/account_space.mjs +10 -0
- package/runner/adapters/antigravity_app.mjs +31 -4
- package/runner/adapters/antigravity_cli.mjs +9 -2
- package/runner/adapters/api.mjs +3 -2
- package/runner/adapters/api_pi.mjs +3 -2
- package/runner/adapters/api_pi_extension.mjs +5 -3
- package/runner/adapters/claude.mjs +32 -9
- package/runner/adapters/cli.mjs +2 -2
- package/runner/adapters/codex_app.mjs +192 -69
- package/runner/adapters/codex_cli.mjs +10 -5
- package/runner/adapters/common.mjs +414 -11
- package/runner/adapters/index.mjs +6 -0
- package/runner/admin_cli.mjs +22 -5
- package/runner/admin_panel.mjs +118 -5
- package/runner/admin_storage.mjs +105 -0
- package/runner/discussion_launch.mjs +328 -128
- package/runner/discussion_runner.mjs +12 -3
- package/runner/discussion_service.mjs +224 -37
- package/runner/discussion_skills.mjs +1 -1
- package/runner/expert_prompts.mjs +2 -2
- package/runner/experts.mjs +227 -0
- package/runner/file_diff.mjs +1 -1
- package/runner/free_runner.mjs +236 -51
- package/runner/i18n_en.mjs +39 -0
- package/runner/os_users.mjs +33 -1
- package/runner/project_files.mjs +7 -1
- package/runner/project_migration.mjs +245 -0
- package/runner/relay_channel.mjs +1 -1
- package/runner/remote_launch.mjs +4 -3
- package/runner/share_card.mjs +17 -1
- package/runner/share_shared.mjs +26 -9
- package/runner/site_deploy.mjs +2 -1
- package/runner/slash_names.mjs +12 -1
- package/runner/speech_directories.mjs +20 -0
- package/runner/storage_guard.mjs +383 -0
- package/runner/tts_providers.mjs +22 -21
- package/runner/workflows.mjs +211 -57
- package/web/admin.css +1 -1
- package/web/admin.js +8 -8
- package/web/app.js +589 -80
- package/web/i18n.js +4 -4
- package/web/markdown.css +1 -1
- package/web/modern.css +1 -1
- package/web/modern.js +68 -66
- package/workflows/decision-council/flow.json +216 -25
- package/workflows/trpg-classic/flow.json +151 -21
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ensure_global_experts_migrated, replace_global_experts, change_project_expert, project_experts } from './experts.mjs';
|
|
1
2
|
import { local_settings } from './discussion_setup.mjs';
|
|
2
3
|
/** Shared launch configuration for an existing AI conversation and the local website. */
|
|
3
4
|
import { spawn } from 'node:child_process';
|
|
@@ -16,7 +17,7 @@ import { atomic_json } from './files.mjs';
|
|
|
16
17
|
import { room_draft_space } from './room_destruction.mjs';
|
|
17
18
|
import { ISOLATED_ACCOUNT_VARIABLE } from './os_users.mjs';
|
|
18
19
|
import { read_store } from './external_client.mjs';
|
|
19
|
-
import { bundle_project_markdown, bundle_project_page, outside_directory, project_media, project_root, project_zip, read_only_enforced, read_project_files, read_project_folders, room_directories, search_project_roots } from './project_files.mjs';
|
|
20
|
+
import { bundle_project_markdown, bundle_project_page, inside_room, outside_directory, project_media, project_root, project_zip, read_only_enforced, read_project_files, read_project_folders, room_directories, room_directory_entries, search_project_roots } from './project_files.mjs';
|
|
20
21
|
import { delete_entry, make_folder, paste_entry, read_text, RecycleArea, rename_entry, save_text, upload_part } from './project_manage.mjs';
|
|
21
22
|
import { guard_remote_request, list_remote_directories, remote_allows, remote_roots } from './remote_policy.mjs';
|
|
22
23
|
import { list_devices, machine_fingerprint, revoke_device, start_pairing } from './device_auth.mjs';
|
|
@@ -34,9 +35,9 @@ import { open_project } from './adapters/api_files.mjs';
|
|
|
34
35
|
import { run_image_tool } from './adapters/api_images.mjs';
|
|
35
36
|
import { access_levels, BUILTIN_EXPERT_PRESETS, MAX_EXPERT_PROMPT, PROJECT_ACCESS } from './expert_prompts.mjs';
|
|
36
37
|
import { acquire_lock, api, check_extra_directories, DiscussionStore, service_drafts, validate_config } from './discussion_runner.mjs';
|
|
37
|
-
import { FreeRoomRunner, free_task_files } from './free_runner.mjs';
|
|
38
|
-
import { assert_workflow_enabled, bind_room_workflow, delete_workflow, get_workflow, list_workflows, load_room_workflow, parse_workflow_draft, preview_workflow, rename_workflow, save_workflow, set_workflow_disabled, workflow_cards, workflow_directory, workflow_member_list, WORKFLOW_ID_PATTERN,
|
|
39
|
-
card_folder, CARD_ID_PATTERN, expert_skills, experts_home, load_room_workflow as load_workflow_for_cards, parse_markdown, serialize_markdown, SKILL_FILE,
|
|
38
|
+
import { FreeRoomRunner, free_task_files, member_drafts, member_extra_directories } from './free_runner.mjs';
|
|
39
|
+
import { transfer_workflow, bound_workflow, room_workflow_store, trust_workflow, assert_workflow_enabled, bind_room_workflow, delete_workflow, get_workflow, list_workflows, load_room_workflow, parse_workflow_draft, preview_workflow, rename_workflow, save_workflow, set_workflow_disabled, workflow_cards, workflow_directory, workflow_member_list, WORKFLOW_ID_PATTERN,
|
|
40
|
+
card_folder, expert_disk_key, CARD_ID_PATTERN, expert_skills, experts_home, load_room_workflow as load_workflow_for_cards, parse_markdown, serialize_markdown, SKILL_FILE,
|
|
40
41
|
SKILL_NAME_PATTERN, SKILLS_DIRECTORY } from './workflows.mjs';
|
|
41
42
|
import { assert_shared_home, CONNECTION_FIELDS, configuration_snapshot, read_registry, registered_identity, register_room, service_home, service_pid, start_service } from './discussion_service.mjs';
|
|
42
43
|
import { site_fetch } from './entry_client.mjs';
|
|
@@ -115,7 +116,8 @@ const HELP = `用法:codiscuss launch settings [--file FILE]
|
|
|
115
116
|
codiscuss launch projects [--file FILE]
|
|
116
117
|
codiscuss launch presets [--file FILE]
|
|
117
118
|
codiscuss launch machine-link --file FILE
|
|
118
|
-
codiscuss launch workflow-adopt (--draft FILE | --id WORKFLOW) [--access-file FILE]
|
|
119
|
+
codiscuss launch workflow-adopt (--draft FILE | --id WORKFLOW) [--scope project|global] [--project-id ID] [--access-file FILE]
|
|
120
|
+
codiscuss launch workflow-promote --id ID [--project-id ID] [--access-file FILE]
|
|
119
121
|
codiscuss launch workflow-join [--card ID[,ID] | --sub SUB | --card ID --label 显示名] [--access-file FILE]
|
|
120
122
|
codiscuss launch workflow-leave (--sub SUB | --member 成员) [--access-file FILE]
|
|
121
123
|
codiscuss launch host-control ACTION [选项] [--access-file FILE]
|
|
@@ -166,6 +168,7 @@ room-member-adapter 把自由房间里空闲的 Codex 成员在 App(codex)
|
|
|
166
168
|
room-profiles 导出房间的本机成员软件配置与实际会话 ID(不含凭据),供备份附带;须提供 site、room_id、token。
|
|
167
169
|
room-restore 把导入房间的本机成员配置登记到本机并核对原会话存在,Claude Code 成员保持手动;须提供 site、room_id、token、local_members。
|
|
168
170
|
room-fork 把从原房间分支出的新房间登记到本机,各本机成员从原会话分叉、不含分支点之后的执行,原会话不变;须提供 site、room_id、token、source_room_id、source_token、executions({成员名: [排除的执行编号]})。
|
|
171
|
+
workflow-promote 将本房间的项目工作流移到全局,房主须允许主持人管理工作流。
|
|
169
172
|
workflow-adopt 主持人在本房间启用工作流:--draft 把自己的草稿(一段,分段头 1/1)保存进工作流库后启用,--id 启用工作流库里已有的工作流(不改动它);缺的卡片成员由本机服务邀请,项目权限最高可写;须房主已为本房间打开“主持人完整权限”。凭据为本次任务的 access_file(或 DISCUSS_ACCESS_FILE)。
|
|
170
173
|
workflow-join 主持人为本房间在用工作流里还没有成员的卡片(启用后才新增的)邀请成员,不填 --card 即全部主线卡片;无需额外授权,离开或移除的成员由用户请回。--sub 改为请进该子工作流的卡片成员(离开的请回原会话)。凭据同上。 --card 一张卡片加 --label 显示名:按这个显示名请一位扮演该卡片的成员(同一张卡可请多位,各有自己的会话;显示名只在本房间、不能和别的成员重复;该卡片已有同名的离开成员就请回它的原会话)。
|
|
171
174
|
workflow-leave 主持人用完子工作流后,把 --sub 子工作流卡片的成员设为离开(会话保留,下次 workflow-join --sub 请回);主线成员不能这样请离。凭据同上。 --member 成员(成员 ID 或显示名)只请离子工作流卡片的这一位成员。
|
|
@@ -181,10 +184,10 @@ host-control 主持人代房主控制房间,须房主已为本房间勾选对
|
|
|
181
184
|
推进:order 读取发言顺序,order --order 甲,乙 --revision REV 调整尚未发言的部分;
|
|
182
185
|
discussion 读取讨论,discussion --do start --goal 目标 --revision REV 开启(上限用房间默认值),--do pause|resume|end --revision REV;
|
|
183
186
|
goal 读取 Goal,goal --do start --goal 目标 --revision REV 开启(预算用房主默认值,默认不限时为 8 次续跑、90 分钟),--do pause|resume|end --revision REV。
|
|
184
|
-
房间:create-room --title 标题 [--goal 目标] [--members 甲,乙] [--host 平台] [--cwd 子目录] [--access read|write] [--text 首条消息] [--submission-id ID]
|
|
187
|
+
${t("房间:create-room --title 标题 [--goal 目标] [--members 甲,乙] [--host 平台] [--host-model 模型] [--host-effort 档位] [--cwd 子目录] [--access read|write] [--text 首条消息 | --body-file FILE] [--submission-id ID]")}
|
|
185
188
|
新建一个房间分担工作:成员用本机已配置的软件,项目权限默认只读、最高可写,目录只能是本房间项目目录或其子目录;不填 --host 时由与你同款的专家主持;
|
|
186
|
-
|
|
187
|
-
room-post --room 房间编号 --text 话 [--submission-id ID]
|
|
189
|
+
新房间复制本房间的智能调度、生图、API 上限、连续派活上限与主持人权限(不含新建房间),本房间可新建的数量有上限;两边各留一张链接卡片;重试沿用同一 --submission-id;
|
|
190
|
+
${t("room-post --room 房间编号 (--text 话 | --body-file FILE) [--interrupt] [--submission-id ID] 在关联房间(你新建的,或新建本房间的)发言,空闲时直达主持人,忙时排队;仅父房间可用 --interrupt 打断子房间;自用户上次发言起两边互传的条数受本机“关联房间互传上限”限制(默认不限);读它用平常的读取命令、房间编号写新房间的(跨房间只能读)。")}
|
|
188
191
|
不开放:批准超出上限的轮数、项目权限高于可写或放行工具、改项目目录、撤回回退用户消息、私聊、对主持人自己的其他操作。
|
|
189
192
|
workflow-check 按导入规则校验工作流文件夹或单段草稿,返回预览,不保存;workflow-save 校验后保存进工作流库,卡片按声明的项目权限保存;库里已有同 ID 的工作流时须表明来意:--revision 写读取时的修订号(库在那之后被改过就拒绝,什么都不写),或 --overwrite 明确整份覆盖,两样都没有就拒绝。写法见 workflow-creator 技能。
|
|
190
193
|
JSON 指定 site、entry(conversation/web)、room(建房内容,含 submission_id)
|
|
@@ -410,6 +413,7 @@ function assert_model_available(home, name, profile) {
|
|
|
410
413
|
* set up, `unknown_model` when that software's list lacks its model. The import goes ahead either way.
|
|
411
414
|
*/
|
|
412
415
|
function expert_readiness(home, preset) {
|
|
416
|
+
home = home.service_home || home;
|
|
413
417
|
if (!card_platform_ready(home, preset.name)) return { needs_platform: true };
|
|
414
418
|
const saved = launch_settings(home).participants[platform_of(preset.name)];
|
|
415
419
|
const model = saved ? unknown_model(home, { ...saved, model: preset.model || '' }) : '';
|
|
@@ -429,7 +433,7 @@ export function launch_settings(home) {
|
|
|
429
433
|
* `actual_model` / `actual_effort` (present only when known) are what each member's session last ran with, read from the software's own session log.
|
|
430
434
|
* With `quick`, agy's default model is not queried and its entry is marked `defaults_pending`.
|
|
431
435
|
*/
|
|
432
|
-
export async function room_models(home, raw, { read_defaults = profile => cached_software_defaults(home, profile), read_session = session_settings } = {}) {
|
|
436
|
+
export async function room_models(home, raw, { read_defaults = profile => cached_software_defaults(home, profile), read_session = session_settings, restricted = false } = {}) {
|
|
433
437
|
const input = launch_input({ ...raw, entry: 'web' });
|
|
434
438
|
if (!input.room_id) throw new Error('读取本场模型须提供 room_id');
|
|
435
439
|
const paths = read_registry(home).filter(path => {
|
|
@@ -467,7 +471,10 @@ export async function room_models(home, raw, { read_defaults = profile => cached
|
|
|
467
471
|
})));
|
|
468
472
|
// Smart scheduling is a local choice; only which model decides is shown, never its key.
|
|
469
473
|
const scheduler = config.scheduler ? { provider: config.scheduler.provider, model: config.scheduler.model } : null;
|
|
470
|
-
|
|
474
|
+
const roots = paths.length ? library_projects(home, { room_id: input.room_id }, restricted) : [];
|
|
475
|
+
const project_workflows = roots.flatMap(root => list_workflows(project_store(home, root)).map(({ directory, ...entry }) => ({ ...entry, ...scope_fields(root) })));
|
|
476
|
+
return { room_id: input.room_id, project_workflows, project_experts: roots.flatMap(root => project_experts(home, root, validate_preset)),
|
|
477
|
+
project_roots: roots.map(({ path, ...fields }) => fields), registered: paths.length > 0, participants, scheduler, project_directory: config.cwd || '',
|
|
471
478
|
extra_directories: config.extra_directories || [],
|
|
472
479
|
workflow: room_workflow_status(config, home), host_workflows: Boolean(config.host_workflows), host_permissions: host_permissions(config),
|
|
473
480
|
api_max_requests: config.api_max_requests ?? DEFAULT_API_MAX_REQUESTS,
|
|
@@ -514,7 +521,7 @@ function room_workflow_status(config, home) {
|
|
|
514
521
|
const loaded = load_room_workflow({ ...config, api_home: config.api_home || home, workflow: { ...config.workflow, enabled: true } });
|
|
515
522
|
// `cards` are the main flow's, which a refresh fills; a sub-workflow's cards join only when it is used.
|
|
516
523
|
const cards = Object.values(loaded.cards || {});
|
|
517
|
-
return { id: config.workflow.id, label: loaded.label || config.workflow.id, enabled: Boolean(config.workflow.enabled),
|
|
524
|
+
return { id: config.workflow.id, scope: config.workflow.scope || 'global', ...(config.workflow.project_id ? { project_id: config.workflow.project_id, ...(loaded.directory ? { project_name: basename(dirname(dirname(dirname(loaded.directory)))) } : {}) } : {}), label: loaded.label || config.workflow.id, enabled: Boolean(config.workflow.enabled),
|
|
518
525
|
available: !loaded.error, members: config.workflow.members || {}, ...(config.workflow.extra_members ? { extra_members: config.workflow.extra_members } : {}),
|
|
519
526
|
cards: cards.filter(card => !card.sub_workflow).map(card => card.card), stages: loaded.stages || [],
|
|
520
527
|
sub_workflows: (loaded.sub_workflows || []).map(sub => ({ id: sub.id, label: sub.label || sub.id, description: sub.description, cards: sub.cards, attach: sub.attach || [] })) };
|
|
@@ -638,7 +645,15 @@ const FILE_OPERATIONS = ['roots', 'mkdir', 'rename', 'delete', 'paste', 'upload'
|
|
|
638
645
|
function file_operation(home, config, input) {
|
|
639
646
|
if (!FILE_OPERATIONS.includes(input.op)) throw new Error(`文件操作只能为 ${FILE_OPERATIONS.join('/')}`);
|
|
640
647
|
const recycle = recycle_area(home);
|
|
641
|
-
const
|
|
648
|
+
const readonly = room_directory_entries(config).filter(item => item.access === 'read');
|
|
649
|
+
const assert_writable = path => {
|
|
650
|
+
if (readonly.some(item => realpathSync(item.path).toLowerCase() === realpathSync(path).toLowerCase())) throw new Error('项目目录只读');
|
|
651
|
+
};
|
|
652
|
+
const root = () => {
|
|
653
|
+
const directory = files_root(home, config, input.root ?? 0);
|
|
654
|
+
if (input.op !== 'read') assert_writable(directory);
|
|
655
|
+
return project_root(directory);
|
|
656
|
+
};
|
|
642
657
|
// A folder some room works in keeps its place: renaming, moving, replacing or deleting it is refused.
|
|
643
658
|
const rooms_in = folder => rooms_using(home, folder);
|
|
644
659
|
if (input.op === 'roots') return list_account_roots(home);
|
|
@@ -646,6 +661,7 @@ function file_operation(home, config, input) {
|
|
|
646
661
|
if (input.op === 'rename') return rename_entry(root(), input.path, input.name, rooms_in);
|
|
647
662
|
if (input.op === 'delete') return delete_entry(root(), input.path, recycle, rooms_in);
|
|
648
663
|
if (input.op === 'paste') {
|
|
664
|
+
if (input.mode === 'move') assert_writable(files_root(home, config, input.source?.root ?? 0));
|
|
649
665
|
return paste_entry({ source_root: project_root(files_root(home, config, input.source?.root ?? 0)), source_path: input.source?.path,
|
|
650
666
|
root: root(), path: input.path, mode: input.mode, conflict: input.conflict, recycle, rooms_in });
|
|
651
667
|
}
|
|
@@ -660,7 +676,7 @@ function file_operation(home, config, input) {
|
|
|
660
676
|
const key = path => { try { return realpathSync(path).toLowerCase(); } catch { return ''; } };
|
|
661
677
|
const reachable = [...account_roots(home).map(item => item.path), ...room_directories(config), account_space(home).workspace,
|
|
662
678
|
account_space(home).projects_root].filter(Boolean).map(key).filter(Boolean);
|
|
663
|
-
return recycle.restore(input.id, origin => reachable.includes(key(origin)));
|
|
679
|
+
return recycle.restore(input.id, origin => reachable.includes(key(origin)) && !readonly.some(item => key(item.path) === key(origin)));
|
|
664
680
|
}
|
|
665
681
|
if (input.op === 'new-project') {
|
|
666
682
|
const { projects_root } = account_space(home);
|
|
@@ -939,42 +955,12 @@ const MAX_PRESET_FIELD = 200;
|
|
|
939
955
|
const PRESET_FIELDS = ['name', 'label', 'description', 'system_prompt', 'project_access', 'model', 'effort'];
|
|
940
956
|
const MAX_PRESET_LABEL = 32;
|
|
941
957
|
|
|
942
|
-
/**
|
|
943
|
-
* Presets saved while IDs could hold Chinese: such an ID becomes `platform-presetN` and its old name the label, so the
|
|
944
|
-
* add row still shows it. The next save keeps the new form.
|
|
945
|
-
*/
|
|
946
|
-
function english_preset_ids(presets) {
|
|
947
|
-
const taken = new Set(presets.map(preset => preset.name));
|
|
948
|
-
return presets.map(preset => {
|
|
949
|
-
const platform = /^(Codex|Claude|Antigravity|DeepSeek|Qwen|Grok|OpenAI|Custom)(?:-|$)/.exec(preset.name || '')?.[1];
|
|
950
|
-
if (!platform || platform_of(preset.name)) return preset;
|
|
951
|
-
let number = 1;
|
|
952
|
-
while (taken.has(`${platform}-preset${number}`)) number++;
|
|
953
|
-
const name = `${platform}-preset${number}`;
|
|
954
|
-
taken.add(name);
|
|
955
|
-
return { ...preset, name, label: preset.label || preset.name };
|
|
956
|
-
});
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
/** The user's expert presets and per-platform default descriptions saved on this computer, plus the built-in presets the user has not deleted. */
|
|
958
|
+
/** Read canonical expert definitions, initializing the global library on its first use. */
|
|
960
959
|
export function expert_presets(home) {
|
|
961
|
-
|
|
962
|
-
const
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
const presets = (saved.presets || []).map(({ image_model, ...preset }) => preset);
|
|
966
|
-
const builtin_presets = BUILTIN_EXPERT_PRESETS.filter(preset => !hidden_builtins.includes(preset.name));
|
|
967
|
-
const listed = english_preset_ids(presets);
|
|
968
|
-
return { presets: listed, description_templates: saved.description_templates || {}, hidden_builtins, builtin_presets,
|
|
969
|
-
skilled_presets: skilled_presets(home, [...listed, ...builtin_presets]) };
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
/** The presets that keep skills of their own in the service home (`experts/<编号>/skills/`): advisors, which the panel opens in 高级配置. */
|
|
973
|
-
function skilled_presets(home, presets) {
|
|
974
|
-
return presets.filter(preset => {
|
|
975
|
-
const key = preset.name.slice(platform_of(preset.name).length + 1);
|
|
976
|
-
return CARD_ID_PATTERN.test(key) && expert_skills(join(experts_home(home), key)).length > 0;
|
|
977
|
-
}).map(preset => preset.name);
|
|
960
|
+
if (!home.root) ensure_global_experts_migrated(home);
|
|
961
|
+
const presets = project_experts(home.service_home || home, home.root, validate_preset);
|
|
962
|
+
return { presets, description_templates: launch_settings(home.service_home || home).description_templates || {},
|
|
963
|
+
hidden_builtins: [], builtin_presets: [], skilled_presets: presets.filter(item => item.skills.length).map(item => item.name) };
|
|
978
964
|
}
|
|
979
965
|
|
|
980
966
|
/** Check one expert preset; a workflow's agent card is a preset and obeys the same rule. */
|
|
@@ -992,7 +978,12 @@ export function validate_preset(preset) {
|
|
|
992
978
|
/** Replace the user's presets, default descriptions or deleted built-ins (whichever the input carries); every field is checked first. */
|
|
993
979
|
export function save_expert_presets(home, input) {
|
|
994
980
|
const current = expert_presets(home);
|
|
995
|
-
const presets = input.presets === undefined ?
|
|
981
|
+
const presets = input.presets === undefined ? [] : Array.isArray(input.presets)
|
|
982
|
+
? input.presets.map(item => {
|
|
983
|
+
if (item?.scope !== 'global') return item;
|
|
984
|
+
const { scope, id, platform, skills, ...preset } = item;
|
|
985
|
+
return preset;
|
|
986
|
+
}) : input.presets;
|
|
996
987
|
const templates = input.description_templates === undefined ? current.description_templates : input.description_templates;
|
|
997
988
|
const hidden_builtins = input.hidden_builtins === undefined ? current.hidden_builtins : input.hidden_builtins;
|
|
998
989
|
const builtin_names = BUILTIN_EXPERT_PRESETS.map(preset => preset.name);
|
|
@@ -1008,8 +999,10 @@ export function save_expert_presets(home, input) {
|
|
|
1008
999
|
platform_of(platform) !== platform || typeof text !== 'string' || !text.trim() || text.length > MAX_PRESET_DESCRIPTION)) {
|
|
1009
1000
|
throw new Error(`description_templates 须为 {平台名: 描述},每段 1–${MAX_PRESET_DESCRIPTION} 字`);
|
|
1010
1001
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1002
|
+
const hidden = new Set(hidden_builtins);
|
|
1003
|
+
const remaining = input.presets === undefined ? current.presets : presets;
|
|
1004
|
+
const updated = remaining.filter(item => !hidden.has(item.name));
|
|
1005
|
+
replace_global_experts(home, input.presets !== undefined || input.hidden_builtins !== undefined ? updated : null, input.description_templates, validate_preset);
|
|
1013
1006
|
return expert_presets(home);
|
|
1014
1007
|
}
|
|
1015
1008
|
|
|
@@ -1018,8 +1011,10 @@ function validate_profile(name, profile, binding = false) {
|
|
|
1018
1011
|
if (!clients_of(name) || !profile || typeof profile !== 'object' || Array.isArray(profile)) throw new Error(`不支持的软件配置:${name}`);
|
|
1019
1012
|
if (profile.adapter && !clients_of(name).includes(profile.adapter)) throw new Error(`${name} 的客户端不匹配`);
|
|
1020
1013
|
// Session bindings and project write access apply to one launch only, never to machine defaults.
|
|
1021
|
-
const fields = binding ? [...PROFILE_FIELDS, 'session_id', 'project_access'] : PROFILE_FIELDS;
|
|
1014
|
+
const fields = binding ? [...PROFILE_FIELDS, 'session_id', 'project_access', 'preset_scope', 'preset_project_id'] : PROFILE_FIELDS;
|
|
1022
1015
|
if (Object.keys(profile).some(key => !fields.includes(key))) throw new Error('软件配置包含不支持的字段;默认配置不能保存房间或会话绑定');
|
|
1016
|
+
if (profile.preset_scope && !['global', 'project'].includes(profile.preset_scope)) throw new Error('专家作用域无效');
|
|
1017
|
+
if (profile.preset_project_id && !/^[0-9a-f]{16}$/.test(profile.preset_project_id)) throw new Error('专家项目编号无效');
|
|
1023
1018
|
for (const [key, value] of Object.entries(profile)) {
|
|
1024
1019
|
if (key === 'allowed_tools') {
|
|
1025
1020
|
if (!Array.isArray(value) || value.some(item => typeof item !== 'string')) throw new Error(`${name}.allowed_tools 必须为工具名称数组`);
|
|
@@ -1121,7 +1116,7 @@ function free_launch_config(home, input, room, directory, options) {
|
|
|
1121
1116
|
const members = launch_members(input, names, options);
|
|
1122
1117
|
// A workflow chosen at creation is written with the members, so the first wake already carries it.
|
|
1123
1118
|
const workflow = input.workflow ? bind_room_workflow({}, { ...input.workflow, enabled: true }).workflow : undefined;
|
|
1124
|
-
if (workflow)
|
|
1119
|
+
if (workflow) bound_workflow(home, { ...input, workflow });
|
|
1125
1120
|
if (workflow && Object.values(workflow.members).some(name => !names.includes(name))) throw new Error('工作流成员须在本次邀请的成员中');
|
|
1126
1121
|
return validate_config({ site: input.site, room: room.id || 'preview', token: options.token, mode: 'free', participants, members, api_home: home,
|
|
1127
1122
|
scheduler: input.scheduler || undefined, cwd: input.cwd, extra_directories: input.extra_directories, project_access: input.project_access || undefined, workflow,
|
|
@@ -1152,7 +1147,7 @@ function launch_members(input, names, options) {
|
|
|
1152
1147
|
export function participant_profile(name, defaults, input, current_host) {
|
|
1153
1148
|
const platform = platform_of(name);
|
|
1154
1149
|
// An extra instance starts from its platform's settings but never from another instance's bound session.
|
|
1155
|
-
const { session_id: ignored_session, ...inherited } = name === platform ? {} : input.participants?.[platform] || {};
|
|
1150
|
+
const { session_id: ignored_session, preset_scope: ignored_scope, preset_project_id: ignored_project_id, ...inherited } = name === platform ? {} : input.participants?.[platform] || {};
|
|
1156
1151
|
const saved = { ...defaults[platform], ...(name === platform ? {} : defaults[name]) };
|
|
1157
1152
|
if (input.without_saved_tools) delete saved.allowed_tools;
|
|
1158
1153
|
// An adopted session keeps its own model and effort, and its adapter may differ from the saved one (App vs CLI),
|
|
@@ -2058,7 +2053,8 @@ async function perform_launch(home, context, options) {
|
|
|
2058
2053
|
const { input, config, minutes, draft, request_signature, directory, previous } = context;
|
|
2059
2054
|
// This is the creation input's start-discussion choice, not the persisted room model (always free).
|
|
2060
2055
|
const discussion = previous?.discussion ?? (input.room ? input.room.mode !== 'free' : Boolean(context.room.session && context.room.session.status !== 'concluded'));
|
|
2061
|
-
const record = previous || { token: config.token, signature: draft, request_signature, config, minutes, entry: input.entry, discussion
|
|
2056
|
+
const record = previous || { token: config.token, signature: draft, request_signature, config, minutes, entry: input.entry, discussion,
|
|
2057
|
+
...(options.inherited_folder ? { inherited_folder: options.inherited_folder } : {}) };
|
|
2062
2058
|
record.discussion = discussion;
|
|
2063
2059
|
const persist = () => atomic_json(join(directory, 'launch.json'), record);
|
|
2064
2060
|
if (!previous) persist();
|
|
@@ -2067,6 +2063,7 @@ async function perform_launch(home, context, options) {
|
|
|
2067
2063
|
let room = context.room;
|
|
2068
2064
|
if (!input.room_id) room = await site_request(input.site, '/api/rooms', config.token, {
|
|
2069
2065
|
...input.room,
|
|
2066
|
+
...(record.inherited_folder ? { folder_id: record.inherited_folder } : {}),
|
|
2070
2067
|
members: card_order(input.room.members, [...Object.keys(config.participants).map(name => ({ name, adapter: config.participants[name].adapter,
|
|
2071
2068
|
token: config.members[name].token, access: 'runner', session_mode: config.participants[name].session_id ? 'borrowed' : 'new' })),
|
|
2072
2069
|
// Manual members keep the credentials the browser generated; nothing local is configured for them.
|
|
@@ -2098,31 +2095,133 @@ async function perform_launch(home, context, options) {
|
|
|
2098
2095
|
} catch (error) { error.launch = progress; throw error; }
|
|
2099
2096
|
}
|
|
2100
2097
|
|
|
2098
|
+
/** Resolve project roots from a registered room, saved project or the local creation form. */
|
|
2099
|
+
function library_projects(home, input, restricted = false) {
|
|
2100
|
+
const config = input.room_id ? room_config_by_id(home, input.room_id) : null;
|
|
2101
|
+
if (restricted && (input.cwd || input.extra_directories?.length)) throw new Error('此请求须使用项目编号或房间编号');
|
|
2102
|
+
const selected = input.project_id || input.target_project_id;
|
|
2103
|
+
const registered = !config && selected && (!input.cwd || restricted) ? account_roots(home).find(item => item.id === selected) : null;
|
|
2104
|
+
const cwd = config ? (room_draft_space(home, config) ? null : config.cwd) : !input.room_id && (registered?.path || input.cwd);
|
|
2105
|
+
const extras = config?.extra_directories || (!input.room_id && input.extra_directories) || [];
|
|
2106
|
+
const grant = restricted ? { roots: remote_roots(granted_projects(home)), home } : null;
|
|
2107
|
+
return [cwd, ...extras].flatMap((item, index) => {
|
|
2108
|
+
const path = typeof item === 'string' ? item : item?.path;
|
|
2109
|
+
if (!path || !isAbsolute(path) || !existsSync(path) || (grant && !remote_allows(path, grant))) return [];
|
|
2110
|
+
const id = createHash('sha256').update(resolve(path).toLowerCase()).digest('hex').slice(0, 16);
|
|
2111
|
+
return [{ id, name: basename(path), readonly: item?.access === 'read', primary: index === 0, path }];
|
|
2112
|
+
});
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
function project_store(home, root) {
|
|
2116
|
+
return { service_home: home, project_dir: root.path, project_id: root.id, root };
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
/** Scope fields shared by the editor and the library list. */
|
|
2120
|
+
function scope_fields(root) {
|
|
2121
|
+
return root ? { scope: 'project', project_id: root.id, project_name: root.name, readonly: root.readonly } : { scope: 'global' };
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
/** List and edit the independent experts available to the room or creation form. */
|
|
2125
|
+
export function presets_command(home, input = {}, restricted = false) {
|
|
2126
|
+
if (input.scope && !['project', 'global'].includes(input.scope)) throw new Error('专家作用域无效');
|
|
2127
|
+
const roots = library_projects(home, input, restricted);
|
|
2128
|
+
let global;
|
|
2129
|
+
if (input.scope === 'project' || input.action === 'trust') {
|
|
2130
|
+
const root = roots.find(item => input.project_id ? item.id === input.project_id : item.primary);
|
|
2131
|
+
if (!root) throw new Error('找不到已授权的项目目录');
|
|
2132
|
+
if (input.action !== 'list') change_project_expert(home, root, input, validate_preset);
|
|
2133
|
+
} else if (input.action === 'save' || input.action === 'delete') {
|
|
2134
|
+
ensure_global_experts_migrated(home);
|
|
2135
|
+
change_project_expert(home, null, input, validate_preset);
|
|
2136
|
+
} else if (!input.action && ['presets', 'description_templates', 'hidden_builtins'].some(key => Object.hasOwn(input, key))) {
|
|
2137
|
+
global = save_expert_presets(home, input);
|
|
2138
|
+
}
|
|
2139
|
+
const project_presets = roots.flatMap(root => project_experts(home, root, validate_preset));
|
|
2140
|
+
const current = global || expert_presets(home);
|
|
2141
|
+
const presets = current.presets;
|
|
2142
|
+
return { ...current, presets, project_presets, project_roots: roots.map(({ path, ...fields }) => fields),
|
|
2143
|
+
skilled_project_presets: project_presets.filter(item => item.skills.length).map(item => `project:${item.project_id}:${item.name}`) };
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2101
2146
|
/** The workflow library: list, read, save, rename, disable or enable, delete, preview an import, or read a room's member cards. */
|
|
2102
|
-
export function workflows_command(home, input = {}) {
|
|
2147
|
+
export function workflows_command(home, input = {}, restricted = false) {
|
|
2103
2148
|
const action = input.action || 'list';
|
|
2104
|
-
if (action === '
|
|
2105
|
-
if (
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
if (action === '
|
|
2109
|
-
|
|
2110
|
-
|
|
2149
|
+
if (action === 'trust') input = { ...input, scope: 'project' };
|
|
2150
|
+
if (restricted && input.folder) throw new Error('其他设备请选择 zip 或文件夹导入工作流');
|
|
2151
|
+
if (input.scope && !['global', 'project'].includes(input.scope)) throw new Error('工作流作用域无效');
|
|
2152
|
+
const roots = ['list', 'copy', 'move', 'save-preset'].includes(action) || input.scope === 'project' ? library_projects(home, input, restricted) : [];
|
|
2153
|
+
if (action === 'list') {
|
|
2154
|
+
const global = workflow_library(home);
|
|
2155
|
+
const project_workflows = roots.flatMap(root => list_workflows(project_store(home, root), name => card_platform_ready(home, name))
|
|
2156
|
+
.map(({ directory, ...entry }) => ({ ...entry, ...scope_fields(root) })));
|
|
2157
|
+
return { ...global, workflows: global.workflows.map(entry => ({ ...entry, scope: 'global' })), project_workflows,
|
|
2158
|
+
project_roots: roots.map(({ path, ...entry }) => entry) };
|
|
2159
|
+
}
|
|
2160
|
+
const root = input.scope === 'project' ? roots.find(item => input.project_id ? item.id === input.project_id : item.primary) : null;
|
|
2161
|
+
if (input.scope === 'project' && !root) throw new Error('找不到已授权的项目目录');
|
|
2162
|
+
const store = root ? project_store(home, root) : home;
|
|
2163
|
+
if (root?.readonly && !['get', 'export', 'preview', 'skills', 'export-expert', 'trust', 'copy'].includes(action)) throw new Error('项目目录只读');
|
|
2164
|
+
const fields = scope_fields(root);
|
|
2165
|
+
const present = workflow => {
|
|
2166
|
+
if (root && restricted) { const { directory, ...rest } = workflow; return { ...rest, ...fields }; }
|
|
2167
|
+
return { ...workflow, ...fields };
|
|
2168
|
+
};
|
|
2169
|
+
if (['move', 'copy'].includes(action)) {
|
|
2170
|
+
if (!['project', 'global'].includes(input.target_scope)) throw new Error('请选择目标作用域');
|
|
2171
|
+
const target = input.target_scope === 'project' ? roots.find(item => input.target_project_id ? item.id === input.target_project_id : item.primary) : null;
|
|
2172
|
+
if (input.target_scope === 'project' && (!target || target.readonly)) throw new Error('目标项目不存在或只读');
|
|
2173
|
+
const config = action === 'move' && input.room_id && room_config_by_id(home, input.room_id);
|
|
2174
|
+
const path = config && registered_room_path(home, config.site, config.room);
|
|
2175
|
+
const release = path && acquire_lock(join(dirname(path), 'configuration-edit'));
|
|
2176
|
+
let room_updated = false;
|
|
2177
|
+
try {
|
|
2178
|
+
const after_publish = () => {
|
|
2179
|
+
if (!path) return;
|
|
2180
|
+
const current = configuration_snapshot(path).config;
|
|
2181
|
+
const binding = current.workflow;
|
|
2182
|
+
if (binding?.id !== input.id || (binding.scope || 'global') !== fields.scope || binding.project_id !== root?.id) return;
|
|
2183
|
+
const { project_id, project_dir, ...rest } = binding;
|
|
2184
|
+
atomic_json(path, { ...current, workflow: { ...rest, scope: input.target_scope, ...(target ? { project_id: target.id, project_dir: target.path } : {}) } });
|
|
2185
|
+
room_updated = true;
|
|
2186
|
+
};
|
|
2187
|
+
const result = transfer_workflow(store, target ? project_store(home, target) : home, input.id,
|
|
2188
|
+
{ move: action === 'move', overwrite: input.overwrite === true, after_publish });
|
|
2189
|
+
return { ...result, ...scope_fields(target), room_updated };
|
|
2190
|
+
} finally { if (release) release(); }
|
|
2191
|
+
}
|
|
2192
|
+
if (action === 'trust' && root) {
|
|
2193
|
+
trust_workflow(store, input.id);
|
|
2194
|
+
return { ...fields, ok: true, id: input.id, trusted: true };
|
|
2195
|
+
}
|
|
2196
|
+
if (action === 'get') return present(get_workflow(store, input.id));
|
|
2197
|
+
if (action === 'save') return present(save_workflow(store, input.workflow, validate_preset, { base_revision: input.base_revision, overwrite: input.overwrite === true }));
|
|
2198
|
+
if (action === 'delete') return { ...delete_workflow(store, input.id), ...fields };
|
|
2199
|
+
if (action === 'rename') return { ...rename_workflow(store, input.id, input.label), ...fields };
|
|
2200
|
+
if (action === 'disable' || action === 'enable') return { ...set_workflow_disabled(store, input.id, action === 'disable'), ...fields };
|
|
2201
|
+
if (action === 'export') {
|
|
2202
|
+
get_workflow(store, input.id);
|
|
2203
|
+
return export_workflow_files(store, input.id);
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2111
2206
|
if (action === 'preview') {
|
|
2112
2207
|
return preview_workflow(input, validate_preset, name => card_platform_ready(home, name));
|
|
2113
2208
|
}
|
|
2114
2209
|
if (action === 'room-cards') return room_cards(home, input);
|
|
2115
|
-
if (action === 'save-preset')
|
|
2210
|
+
if (action === 'save-preset') {
|
|
2211
|
+
const target = input.target_scope === 'project' ? roots.find(item => input.target_project_id ? item.id === input.target_project_id : item.primary) : null;
|
|
2212
|
+
if (input.target_scope === 'project' && (!target || target.readonly)) throw new Error('目标项目不存在或只读');
|
|
2213
|
+
const destination = input.target_scope ? (target ? project_store(home, target) : home) : store;
|
|
2214
|
+
return save_card_preset(store, input, destination);
|
|
2215
|
+
}
|
|
2116
2216
|
// The page's expert export and import (the command line has expert-export / expert-import), by files `{path: text}`.
|
|
2117
|
-
if (action === 'export-expert') return expert_export_files(
|
|
2118
|
-
if (action === '
|
|
2119
|
-
if (action === 'import-expert') return
|
|
2120
|
-
if (action === 'import-expert-workflow') return import_expert_to_workflow(home, { files: input.files, workflow: input.workflow, sub: input.sub || '' });
|
|
2217
|
+
if (action === 'export-expert') return expert_export_files(store, input);
|
|
2218
|
+
if (action === 'import-expert') return import_expert(store, { files: input.files, overwrite_preset: input.overwrite_preset === true, replace_knowledge: input.replace_knowledge === true });
|
|
2219
|
+
if (action === 'import-expert-workflow') return import_expert_to_workflow(store, { files: input.files, workflow: input.workflow, sub: input.sub || '' });
|
|
2121
2220
|
// An expert's skills, file by file, outside the workflow's save (see expert_skill_files).
|
|
2122
|
-
if (action === 'skills') return expert_skill_files(
|
|
2123
|
-
if (action === 'skill-write') return write_skill_file(
|
|
2124
|
-
if (action === 'skill-delete') return delete_skill_file(
|
|
2125
|
-
if (action === 'skill-remove') return remove_skill(
|
|
2221
|
+
if (action === 'skills') return { ...expert_skill_files(store, input), ...fields };
|
|
2222
|
+
if (action === 'skill-write') return { ...write_skill_file(store, input), ...fields };
|
|
2223
|
+
if (action === 'skill-delete') return { ...delete_skill_file(store, input), ...fields };
|
|
2224
|
+
if (action === 'skill-remove') return { ...remove_skill(store, input), ...fields };
|
|
2126
2225
|
throw new Error('action 须为 list、get、save、rename、disable、enable、delete、preview、room-cards、save-preset、export、export-expert、import-expert、import-expert-workflow、skills、skill-write、skill-delete 或 skill-remove');
|
|
2127
2226
|
}
|
|
2128
2227
|
|
|
@@ -2213,7 +2312,7 @@ export function expert_export_files(home, { preset: preset_name, workflow: workf
|
|
|
2213
2312
|
}
|
|
2214
2313
|
const { key, text } = expert_definition(preset);
|
|
2215
2314
|
const id = key || platform_of(preset.name);
|
|
2216
|
-
const source = preset_name
|
|
2315
|
+
const source = preset_name && key ? join(experts_home(home), expert_disk_key(experts_home(home), platform_of(preset_name), key)) : folder;
|
|
2217
2316
|
const files = { [`${id}.md`]: text };
|
|
2218
2317
|
const skills = source ? expert_skills(source) : [];
|
|
2219
2318
|
for (const skill of skills) {
|
|
@@ -2234,14 +2333,21 @@ const SKILL_DIGEST_LENGTH = 16;
|
|
|
2234
2333
|
function skill_digest(bytes) { return createHash('sha256').update(bytes).digest('hex').slice(0, SKILL_DIGEST_LENGTH); }
|
|
2235
2334
|
|
|
2236
2335
|
/** The skills folder a request names: `workflow` and `card` (a saved folder card) or `preset` (its name). */
|
|
2237
|
-
function skills_folder(home, { workflow, card, preset } = {}) {
|
|
2336
|
+
function skills_folder(home, { workflow, card, preset } = {}, writing = false) {
|
|
2238
2337
|
if (preset) {
|
|
2239
2338
|
const { presets, builtin_presets } = expert_presets(home);
|
|
2240
2339
|
const found = [...presets, ...builtin_presets].find(item => item.name === preset);
|
|
2241
2340
|
if (!found) throw new Error(`没有预设 ${preset}`);
|
|
2242
2341
|
const { key } = expert_definition(found);
|
|
2243
2342
|
if (!CARD_ID_PATTERN.test(key)) throw new Error(`预设 ${preset} 的名字没有“平台-编号”的编号,不能带技能`);
|
|
2244
|
-
|
|
2343
|
+
const disk = expert_disk_key(experts_home(home), platform_of(preset), key);
|
|
2344
|
+
const folder = join(experts_home(home), disk);
|
|
2345
|
+
const flat = join(experts_home(home), disk + '.md');
|
|
2346
|
+
if (writing && existsSync(flat)) {
|
|
2347
|
+
mkdirSync(folder, { recursive: true });
|
|
2348
|
+
renameSync(flat, join(folder, disk + '.md'));
|
|
2349
|
+
}
|
|
2350
|
+
return join(folder, SKILLS_DIRECTORY);
|
|
2245
2351
|
}
|
|
2246
2352
|
const folder = card_folder(load_workflow_for_cards({ api_home: home, workflow: { id: workflow, enabled: true, members: {} } }), card);
|
|
2247
2353
|
if (!folder) throw new Error(`工作流 ${workflow} 的卡片 ${card} 没有技能;先给它添加技能并保存工作流`);
|
|
@@ -2294,14 +2400,19 @@ export function expert_skill_files(home, input) {
|
|
|
2294
2400
|
|
|
2295
2401
|
/** Write one skill file (`skill-write`): creates the skill and folders it needs; returns the new digest. */
|
|
2296
2402
|
export function write_skill_file(home, { path, text, digest, ...input } = {}) {
|
|
2297
|
-
const root = skills_folder(home, input);
|
|
2403
|
+
const root = skills_folder(home, input, true);
|
|
2298
2404
|
const target = skill_target(root, path);
|
|
2299
2405
|
if (typeof text !== 'string' || Buffer.byteLength(text, 'utf8') > MAX_EXPERT_FILE_BYTES) throw new Error(`技能文件须为文本,最多 ${MAX_EXPERT_FILE_BYTES / 1000000} MB`);
|
|
2300
2406
|
check_skill_digest(target, digest, path);
|
|
2301
2407
|
mkdirSync(dirname(target), { recursive: true });
|
|
2408
|
+
for (const leftover of readdirSync(dirname(target))) {
|
|
2409
|
+
if (/\.[0-9a-f]{8}\.tmp$/.test(leftover)) rmSync(join(dirname(target), leftover), { force: true });
|
|
2410
|
+
}
|
|
2302
2411
|
const temporary = `${target}.${randomUUID().slice(0, 8)}.tmp`;
|
|
2303
|
-
|
|
2304
|
-
|
|
2412
|
+
try {
|
|
2413
|
+
writeFileSync(temporary, text);
|
|
2414
|
+
renameSync(temporary, target);
|
|
2415
|
+
} finally { rmSync(temporary, { force: true }); }
|
|
2305
2416
|
return { path, digest: skill_digest(Buffer.from(text, 'utf8')) };
|
|
2306
2417
|
}
|
|
2307
2418
|
|
|
@@ -2319,8 +2430,8 @@ export function remove_skill(home, { skill, ...input } = {}) {
|
|
|
2319
2430
|
const target = skill_target(skills_folder(home, input), skill, false);
|
|
2320
2431
|
if (!existsSync(target)) throw new Error(`没有技能 ${skill}`);
|
|
2321
2432
|
const backup = `skill-${input.preset ? platform_of(input.preset) : input.workflow}-${input.card || expert_definition({ name: input.preset }).key}-${skill}-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomUUID().slice(0, 8)}`;
|
|
2322
|
-
mkdirSync(join(home, 'backups'), { recursive: true });
|
|
2323
|
-
cpSync(target, join(home, 'backups', backup), { recursive: true });
|
|
2433
|
+
mkdirSync(join(home.service_home || home, 'backups'), { recursive: true });
|
|
2434
|
+
cpSync(target, join(home.service_home || home, 'backups', backup), { recursive: true });
|
|
2324
2435
|
rmSync(target, { recursive: true, force: true });
|
|
2325
2436
|
return { skill, backup };
|
|
2326
2437
|
}
|
|
@@ -2346,9 +2457,8 @@ export function import_expert(home, { files = {}, ignored: unread = [], overwrit
|
|
|
2346
2457
|
const taken = current.presets.some(item => item.name === preset.name);
|
|
2347
2458
|
if (taken && overwrite_preset !== true) throw expert_failure('expert_preset_exists', '面板上已有预设「{name}」;要用导入的替换它,请明确选择覆盖', { name: preset.name });
|
|
2348
2459
|
// The preset is saved first: a full list or a bad field stops the import before any knowledge is written.
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
const { knowledge, backup } = install_knowledge(home, key, skills, replace_knowledge === true);
|
|
2460
|
+
change_project_expert(home.service_home || home, home.root, { action: 'save', preset, overwrite: overwrite_preset }, validate_preset);
|
|
2461
|
+
const { knowledge, backup } = install_knowledge(home, key && expert_disk_key(experts_home(home), platform_of(preset.name), key), skills, replace_knowledge === true);
|
|
2352
2462
|
return { preset, ...(requested ? { requested_access: requested } : {}), ...expert_readiness(home, preset), knowledge, skills: Object.keys(skills).sort(),
|
|
2353
2463
|
...(backup ? { backup } : {}), ignored: [...new Set(ignored)].sort() };
|
|
2354
2464
|
}
|
|
@@ -2400,7 +2510,7 @@ function install_knowledge(home, key, skills, replace) {
|
|
|
2400
2510
|
if (had && !replace) knowledge = 'kept';
|
|
2401
2511
|
else {
|
|
2402
2512
|
if (had) {
|
|
2403
|
-
backup = join(home, 'backups', `expert-${key}-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomUUID().slice(0, 8)}`);
|
|
2513
|
+
backup = join(home.service_home || home, 'backups', `expert-${key}-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomUUID().slice(0, 8)}`);
|
|
2404
2514
|
mkdirSync(dirname(backup), { recursive: true });
|
|
2405
2515
|
renameSync(live, backup);
|
|
2406
2516
|
}
|
|
@@ -2411,6 +2521,11 @@ function install_knowledge(home, key, skills, replace) {
|
|
|
2411
2521
|
writeFileSync(target, text);
|
|
2412
2522
|
}
|
|
2413
2523
|
}
|
|
2524
|
+
{
|
|
2525
|
+
const flat = join(experts_home(home), key + '.md');
|
|
2526
|
+
if (existsSync(flat)) renameSync(flat, join(live, key + '.md'));
|
|
2527
|
+
else if (backup) cpSync(join(backup, key + '.md'), join(live, key + '.md'));
|
|
2528
|
+
}
|
|
2414
2529
|
knowledge = had ? 'replaced' : 'added';
|
|
2415
2530
|
}
|
|
2416
2531
|
}
|
|
@@ -2449,22 +2564,22 @@ export function import_expert_to_workflow(home, { files = {}, ignored: unread =
|
|
|
2449
2564
|
* holds. A folder card's skills are copied from the workflow into `experts/<card>/skills/` in the service home; one
|
|
2450
2565
|
* already there is kept unless `replace_knowledge`, as an import does (the old one goes to backups/).
|
|
2451
2566
|
*/
|
|
2452
|
-
function save_card_preset(home, { id, card, overwrite, replace_knowledge } = {}) {
|
|
2567
|
+
function save_card_preset(home, { id, card, overwrite, replace_knowledge } = {}, destination = home) {
|
|
2453
2568
|
const workflow = get_workflow(home, id);
|
|
2454
2569
|
const agent = workflow_cards(workflow).find(item => item.card === card);
|
|
2455
2570
|
if (!agent) throw new Error(`工作流 ${id} 没有卡片 ${card}`);
|
|
2456
2571
|
const preset = Object.fromEntries(PRESET_FIELDS.filter(field => agent[field]).map(field => [field, agent[field]]));
|
|
2457
|
-
const current = expert_presets(
|
|
2572
|
+
const current = expert_presets(destination);
|
|
2458
2573
|
const taken = current.presets.some(item => item.name === preset.name);
|
|
2459
2574
|
if (taken && overwrite !== true) {
|
|
2460
2575
|
const template = '面板上已有预设「{name}」;要用这张卡片替换它,请明确选择覆盖';
|
|
2461
2576
|
throw Object.assign(new Error(template.replace('{name}', preset.name)), { code: 'preset_exists', error_text: { template, params: { name: preset.name } } });
|
|
2462
2577
|
}
|
|
2463
|
-
|
|
2464
|
-
const saved =
|
|
2578
|
+
change_project_expert(destination.service_home || destination, destination.root, { action: 'save', preset, overwrite }, validate_preset);
|
|
2579
|
+
const saved = expert_presets(destination);
|
|
2465
2580
|
const folder = card_folder(load_workflow_for_cards({ api_home: home, workflow: { id, enabled: true, members: {} } }), card);
|
|
2466
2581
|
const skills = Object.fromEntries((folder ? expert_skills(folder) : []).map(skill => [skill, read_text_tree(join(folder, SKILLS_DIRECTORY, skill)).files]));
|
|
2467
|
-
const { knowledge, backup } = install_knowledge(
|
|
2582
|
+
const { knowledge, backup } = install_knowledge(destination, expert_disk_key(experts_home(destination), platform_of(preset.name), card), skills, replace_knowledge === true);
|
|
2468
2583
|
return { ...saved, preset, knowledge, ...(backup ? { backup } : {}) };
|
|
2469
2584
|
}
|
|
2470
2585
|
|
|
@@ -2512,9 +2627,11 @@ export async function room_workflow(home, raw) {
|
|
|
2512
2627
|
return { room_id: input.room_id, host_workflows: Boolean(next.host_workflows), host_permissions: host_permissions(next) };
|
|
2513
2628
|
}
|
|
2514
2629
|
if (input.action === 'enable') {
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2630
|
+
const binding = input.workflow || {};
|
|
2631
|
+
const store = room_workflow_store(home, config, binding);
|
|
2632
|
+
if (store.project_id && input.trust === true) trust_workflow(store, binding.id);
|
|
2633
|
+
const next = bind_room_workflow(config, { ...binding, project_id: store.project_id, enabled: true });
|
|
2634
|
+
bound_workflow(home, next);
|
|
2518
2635
|
validate_config({ ...next, api_home: home }, dirname(path));
|
|
2519
2636
|
atomic_json(path, next);
|
|
2520
2637
|
return { room_id: input.room_id, workflow: room_workflow_status(next, home) };
|
|
@@ -2558,7 +2675,7 @@ async function host_room(home, access_file) {
|
|
|
2558
2675
|
if (!host || !roster.some(member => member.name === host && member.role === 'host' && member.status === 'active')) throw new Error('只有本房间的主持人能运行此命令');
|
|
2559
2676
|
// How the room shows each member (the owner's label, else its platform nickname), so the host names them as it sees them.
|
|
2560
2677
|
const labels = { ...Object.fromEntries((room.members || []).map(member => [member.name, member.label])), ...room.labels };
|
|
2561
|
-
return { path, config, roster, labels, host };
|
|
2678
|
+
return { path, config, roster, labels, host, folder_id: room.folder_id, max_rounds: room.free?.max_rounds };
|
|
2562
2679
|
}
|
|
2563
2680
|
|
|
2564
2681
|
const IMAGE_ACTIONS = { generate: 'generate_image', edit: 'edit_image' };
|
|
@@ -2657,7 +2774,7 @@ async function join_workflow_cards(home, config, roster, workflow, members, card
|
|
|
2657
2774
|
*/
|
|
2658
2775
|
async function sync_workflow_cards(home, path, config, roster, cards) {
|
|
2659
2776
|
if (!config.workflow?.id || !config.workflow.enabled) throw new Error('本房间没有在用的工作流');
|
|
2660
|
-
const workflow =
|
|
2777
|
+
const workflow = bound_workflow(home, config);
|
|
2661
2778
|
const known = workflow_cards(workflow).map(agent => agent.card);
|
|
2662
2779
|
const unknown = (cards || []).filter(card => !known.includes(card));
|
|
2663
2780
|
if (unknown.length) throw new Error(`工作流 ${workflow.id} 没有这些卡片:${unknown.join('、')};现有卡片:${known.join('、') || '无'}`);
|
|
@@ -2699,7 +2816,7 @@ export async function join_workflow(home, { access_file, cards, sub, label }) {
|
|
|
2699
2816
|
const mapped = Object.keys(config.workflow?.members || {});
|
|
2700
2817
|
const taken = (cards || []).filter(card => mapped.includes(card));
|
|
2701
2818
|
if (taken.length) throw new Error(`这些卡片已有成员:${taken.join('、')};离开或移除的成员由用户请回`);
|
|
2702
|
-
const known = config.workflow?.id && config.workflow.enabled ?
|
|
2819
|
+
const known = config.workflow?.id && config.workflow.enabled ? bound_workflow(home, config).agents.map(agent => agent.card) : [];
|
|
2703
2820
|
return sync_workflow_cards(home, path, config, roster, cards?.length ? cards : known.filter(card => !mapped.includes(card)));
|
|
2704
2821
|
}
|
|
2705
2822
|
|
|
@@ -2718,7 +2835,7 @@ async function join_labeled_member(home, { path, config, roster, labels }, cards
|
|
|
2718
2835
|
if (cards?.length !== 1) throw new Error('--label 须和 --card 一起用,且只写一张卡片');
|
|
2719
2836
|
if (!label || label.length > MAX_MEMBER_LABEL || /\n/.test(label)) throw new Error(`--label 须为 1—${MAX_MEMBER_LABEL} 字、不换行的显示名`);
|
|
2720
2837
|
const [card] = cards;
|
|
2721
|
-
const workflow =
|
|
2838
|
+
const workflow = bound_workflow(home, config);
|
|
2722
2839
|
const agent = workflow_cards(workflow).find(item => item.card === card);
|
|
2723
2840
|
if (!agent) throw new Error(`工作流 ${workflow.id} 没有卡片 ${card}`);
|
|
2724
2841
|
const shown = member => labels[member.name] || member.name;
|
|
@@ -2757,7 +2874,7 @@ async function join_labeled_member(home, { path, config, roster, labels }, cards
|
|
|
2757
2874
|
/** The cards a sub-workflow of the room's workflow in use brings; refuses one it lacks or one with no cards. */
|
|
2758
2875
|
function sub_workflow_cards(home, config, sub) {
|
|
2759
2876
|
if (!config.workflow?.id || !config.workflow.enabled) throw new Error('本房间没有在用的工作流');
|
|
2760
|
-
const list =
|
|
2877
|
+
const list = bound_workflow(home, config).sub_workflows;
|
|
2761
2878
|
const found = list.find(item => item.id === sub);
|
|
2762
2879
|
if (!found) throw new Error(`工作流 ${config.workflow.id} 没有子工作流 ${sub};现有子工作流:${list.map(item => item.id).join('、') || '无'}`);
|
|
2763
2880
|
if (!found.agents.length) throw new Error(`子工作流 ${sub} 没有卡片,不需要请成员加入`);
|
|
@@ -2775,7 +2892,7 @@ export async function leave_sub_workflow(home, { access_file, sub, member: wante
|
|
|
2775
2892
|
if (!config.workflow?.id || !config.workflow.enabled) throw new Error('本房间没有在用的工作流');
|
|
2776
2893
|
// A sub-workflow's members: every member of its cards, a card's further members (`extra_members`) included.
|
|
2777
2894
|
const sub_cards = sub ? sub_workflow_cards(home, config, sub)
|
|
2778
|
-
: workflow_cards(
|
|
2895
|
+
: workflow_cards(bound_workflow(home, config)).filter(agent => agent.sub_workflow).map(agent => agent.card);
|
|
2779
2896
|
let names = workflow_member_list(config.workflow).filter(item => sub_cards.includes(item.card)).map(item => item.name);
|
|
2780
2897
|
if (wanted) {
|
|
2781
2898
|
const member = roster.find(item => item.status !== 'removed' && (item.name === wanted || labels[item.name] === wanted || String(item.id) === String(wanted)));
|
|
@@ -2968,18 +3085,37 @@ async function host_create_room(ctx, o) {
|
|
|
2968
3085
|
if (o.access && !HOST_CARD_ACCESS.includes(o.access)) throw new Error('新房间成员的项目权限只能是 read 或 write');
|
|
2969
3086
|
const access = o.access || 'read';
|
|
2970
3087
|
const host_name = o.host || platform_of(host);
|
|
2971
|
-
const
|
|
3088
|
+
const listed = (o.members || '').split(',').map(name => name.trim()).filter(Boolean);
|
|
3089
|
+
const names = [host_name];
|
|
3090
|
+
let includes_host = false;
|
|
3091
|
+
for (const name of listed) {
|
|
3092
|
+
if (name === host_name && !includes_host) { includes_host = true; continue; }
|
|
3093
|
+
names.push(names.includes(name) ? free_instance(name, new Set(names)) : name);
|
|
3094
|
+
}
|
|
2972
3095
|
const participants = Object.fromEntries(names.map(name => [name, host_invite_profile(home, name, { access })]));
|
|
2973
3096
|
if (!o.host) {
|
|
2974
3097
|
// The same expert as this host: its software, model and effort, in a new session and at the access asked for.
|
|
2975
3098
|
const { session_id, cwd, project_access, ...own } = config.participants?.[host] || {};
|
|
3099
|
+
const facts = adapter_profile(own.adapter);
|
|
3100
|
+
const effort = own.effort || launch_settings(home).participants?.[host_name]?.effort;
|
|
3101
|
+
if (effort && facts?.kind !== 'api' && !facts?.efforts?.includes(effort)) own.effort = '';
|
|
2976
3102
|
participants[host_name] = { ...own, project_access: access };
|
|
2977
3103
|
}
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
const
|
|
3104
|
+
if (o.host_model) participants[host_name].model = o.host_model;
|
|
3105
|
+
if (o.host_effort) participants[host_name].effort = o.host_effort;
|
|
3106
|
+
const permissions = without_host_item(host_permissions(config), 'create_rooms');
|
|
3107
|
+
const cwd = spawned_directory(config.cwd, o.cwd);
|
|
3108
|
+
let extra_directories;
|
|
3109
|
+
if (config.extra_directories?.length) {
|
|
3110
|
+
try { check_extra_directories(cwd, config.extra_directories); extra_directories = config.extra_directories; } catch {}
|
|
3111
|
+
}
|
|
3112
|
+
const receipt = await launch_discussion(home, { site: config.site, entry: 'web', cwd, participants,
|
|
3113
|
+
...(config.scheduler ? { scheduler: config.scheduler } : {}),
|
|
3114
|
+
...(config.image_generation ? { image_generation: true } : {}),
|
|
3115
|
+
...(config.api_max_requests !== undefined ? { api_max_requests: config.api_max_requests } : {}),
|
|
3116
|
+
...(extra_directories ? { extra_directories } : {}),
|
|
2981
3117
|
host_permissions: permissions, room: { title: o.title, goal: o.goal || '', mode: 'free', host: host_name, submission_id,
|
|
2982
|
-
members: names.map(name => ({ name, access: 'runner' })) } }, ctx.launch_options);
|
|
3118
|
+
members: names.map(name => ({ name, access: 'runner' })) } }, { ...ctx.launch_options, inherited_folder: ctx.folder_id });
|
|
2983
3119
|
const room_id = receipt.room.id;
|
|
2984
3120
|
const parent_path = registered_room_path(home, config.site, config.room);
|
|
2985
3121
|
if (!retry) edit_room_config(parent_path, saved => ({ ...saved, spawned_rooms: [...(saved.spawned_rooms || []), { room: room_id, submission_id }] }));
|
|
@@ -2988,10 +3124,13 @@ async function host_create_room(ctx, o) {
|
|
|
2988
3124
|
const registered = registered_room_config(home, config.site, room_id);
|
|
2989
3125
|
if (registered.config.spawned_from !== config.room) edit_room_config(registered.path, saved => ({ ...saved, spawned_from: config.room }));
|
|
2990
3126
|
const child = registered.config;
|
|
3127
|
+
if (!retry && typeof ctx.max_rounds === 'number') {
|
|
3128
|
+
await site_request(config.site, `/api/rooms/${room_id}/budget`, child.token, { max_rounds: ctx.max_rounds }, caller_only(ctx.headers));
|
|
3129
|
+
}
|
|
2991
3130
|
await site_request(config.site, `/api/rooms/${room_id}/room-link`, child.token, { relation: 'parent', room_id: config.room, host: label }, caller_only(ctx.headers));
|
|
2992
3131
|
const first = o.text ? await host_room_post({ ...ctx, config: configuration_snapshot(parent_path).config }, { room: room_id, text: o.text, submission_id: `${submission_id}-first` }) : null;
|
|
2993
3132
|
return { room_id, title: receipt.room.title, url: receipt.url, host: host_name, members: names, project_access: access,
|
|
2994
|
-
cwd: child.cwd || null, ...(first ? { message_id: first.message_id } : {}) };
|
|
3133
|
+
cwd: child.cwd || null, ...(first ? { message_id: first.message_id, ...(first.record_error ? { record_error: first.record_error } : {}) } : {}) };
|
|
2995
3134
|
}
|
|
2996
3135
|
|
|
2997
3136
|
/**
|
|
@@ -3005,12 +3144,24 @@ async function host_room_post(ctx, o) {
|
|
|
3005
3144
|
if (!o.room || (o.room !== config.spawned_from && !(config.spawned_rooms || []).some(item => item.room === o.room))) {
|
|
3006
3145
|
throw new Error('只能在与本房间关联的房间里发言(本房间新建的房间,或新建本房间的房间);用 --room 指定其房间编号');
|
|
3007
3146
|
}
|
|
3008
|
-
if (!o.
|
|
3147
|
+
if (o.interrupt && !(config.spawned_rooms || []).some(item => item.room === o.room)) throw new Error(t('仅父房间主持人可以打断子房间'));
|
|
3148
|
+
if (!o.text) throw new Error(t('须用 --text 或 --body-file 写明要发的消息'));
|
|
3009
3149
|
const child = registered_room_config(home, config.site, o.room).config;
|
|
3150
|
+
const submission_id = o.submission_id || randomUUID();
|
|
3151
|
+
const label = ctx.labels[host] || host;
|
|
3010
3152
|
const result = await site_request(config.site, `/api/rooms/${o.room}/messages`, child.token, { author: '用户', text: o.text,
|
|
3011
|
-
submission_id: o.
|
|
3153
|
+
submission_id, queue: true, ...(o.interrupt ? { interrupt: true } : {}), relay: { room_id: config.room, member: host, host: label,
|
|
3012
3154
|
limit: launch_settings(home).create_preferences?.relay_rounds ?? 0 } }, caller_only(ctx.headers));
|
|
3013
|
-
|
|
3155
|
+
const receipt = { room_id: o.room, message_id: result.message?.id ?? null, queued: result.queued ?? null,
|
|
3156
|
+
interrupted: Boolean(result.interrupted), ...(result.reason ? { reason: result.reason } : {}) };
|
|
3157
|
+
const key = createHash('sha256').update(JSON.stringify([o.room, submission_id])).digest('hex');
|
|
3158
|
+
try {
|
|
3159
|
+
await ctx.call('/room-relay-sent', { target_room_id: o.room, target_message_id: receipt.message_id,
|
|
3160
|
+
queued_id: receipt.queued, interrupted: receipt.interrupted, text: o.text, host: label, member: host, submission_id: 'sent-' + key });
|
|
3161
|
+
} catch (error) {
|
|
3162
|
+
return { ...receipt, submission_id, record_error: error.message };
|
|
3163
|
+
}
|
|
3164
|
+
return receipt;
|
|
3014
3165
|
}
|
|
3015
3166
|
|
|
3016
3167
|
const HOST_CONTROLS = {
|
|
@@ -3069,15 +3220,25 @@ const HOST_CONTROLS = {
|
|
|
3069
3220
|
*/
|
|
3070
3221
|
export async function host_control(home, action, options, launch_options = {}) {
|
|
3071
3222
|
if (!Object.hasOwn(HOST_CONTROLS, action || '')) throw new Error(`操作须为 ${Object.keys(HOST_CONTROLS).join('、')} 之一`);
|
|
3072
|
-
const { config, roster, labels, host } = await host_room(home, options.access_file);
|
|
3223
|
+
const { config, roster, labels, host, folder_id, max_rounds } = await host_room(home, options.access_file);
|
|
3073
3224
|
const needed = HOST_ACTION_ITEMS[action];
|
|
3074
3225
|
if (needed && !host_allowed(config, needed)) throw new Error(host_refusal(needed));
|
|
3226
|
+
if (options.body_file && ['create-room', 'room-post'].includes(action)) {
|
|
3227
|
+
if (options.text !== undefined) throw new Error(t('--body-file 与 --text 只能选一项'));
|
|
3228
|
+
const profile = config.participants[host];
|
|
3229
|
+
const file = realpathSync(options.body_file);
|
|
3230
|
+
const roots = [member_drafts(config, host), profile.cwd || config.cwd, ...member_extra_directories(config, profile).map(item => item.path)];
|
|
3231
|
+
if (!inside_room({ extra_directories: roots.filter(root => root && existsSync(root)).map(root => realpathSync(root)) }, file)) {
|
|
3232
|
+
throw new Error(t('正文文件须位于主持人的项目或草稿目录'));
|
|
3233
|
+
}
|
|
3234
|
+
options = { ...options, text: readFileSync(file, 'utf8').replace(/^\uFEFF/, '') };
|
|
3235
|
+
}
|
|
3075
3236
|
// The User-Agent names the caller in the event log; the host's own credential is what lets the site label what it
|
|
3076
3237
|
// does as the host's (steer, stop) by its display name, since every write here carries the owner's credential.
|
|
3077
3238
|
const headers = { 'User-Agent': `AI-CoDiscuss-host:${host}`, 'X-Acting-Member-Token': config.members[host].token };
|
|
3078
3239
|
const owner = { site: config.site, room_id: config.room, token: config.token };
|
|
3079
3240
|
const call = (path, body) => site_request(config.site, `/api/rooms/${config.room}${path}`, config.token, body, headers);
|
|
3080
|
-
return HOST_CONTROLS[action]({ home, config, roster, labels, host, headers, owner, call, launch_options }, options);
|
|
3241
|
+
return HOST_CONTROLS[action]({ home, config, roster, labels, host, headers, owner, call, launch_options, folder_id, max_rounds }, options);
|
|
3081
3242
|
}
|
|
3082
3243
|
|
|
3083
3244
|
/** Cards as the host may bring them: any access above writing is lowered to it. */
|
|
@@ -3091,19 +3252,26 @@ const host_cards = agents => agents.map(agent => agent.project_access && !HOST_C
|
|
|
3091
3252
|
* write access, one that left is brought back. Returns which member plays which card and where its SKILL.md is, so the
|
|
3092
3253
|
* host can follow it and name them in the same execution.
|
|
3093
3254
|
*/
|
|
3094
|
-
export async function adopt_workflow(home, { access_file, draft_file, workflow_id }) {
|
|
3255
|
+
export async function adopt_workflow(home, { access_file, draft_file, workflow_id, scope, project_id }) {
|
|
3095
3256
|
if (!draft_file === !workflow_id) throw new Error('须提供 --draft 草稿文件或 --id 工作流库中的工作流 ID 之一;凭据用 --access-file 或 DISCUSS_ACCESS_FILE');
|
|
3096
3257
|
const { config, roster } = await host_room(home, access_file);
|
|
3097
3258
|
if (!host_allowed(config, 'workflows')) throw new Error(host_refusal('workflows'));
|
|
3098
|
-
|
|
3259
|
+
if (scope && !['global', 'project'].includes(scope)) throw new Error('工作流作用域无效');
|
|
3260
|
+
const roots = library_projects(home, { room_id: config.room });
|
|
3261
|
+
const root = project_id ? roots.find(item => item.id === project_id) : roots.find(item => draft_file || existsSync(workflow_directory(project_store(home, item), workflow_id)));
|
|
3262
|
+
if ((scope === 'project' || project_id) && !root) throw new Error('找不到房间项目目录');
|
|
3263
|
+
const store = scope === 'global' || !root ? home : project_store(home, root);
|
|
3264
|
+
const binding = store.project_id ? { scope: 'project', project_id: store.project_id } : { scope: 'global' };
|
|
3265
|
+
if (draft_file && store.project_id && root.readonly) throw new Error('项目目录只读');
|
|
3266
|
+
const bound = (config.workflow?.scope || 'global') === binding.scope && config.workflow?.project_id === binding.project_id ? config.workflow?.id : null;
|
|
3099
3267
|
let workflow;
|
|
3100
|
-
if (workflow_id) workflow =
|
|
3268
|
+
if (workflow_id) workflow = bound_workflow(home, { ...config, workflow: { ...binding, id: workflow_id } });
|
|
3101
3269
|
else {
|
|
3102
3270
|
const draft = parse_workflow_draft([readFileSync(resolve(draft_file), 'utf8')]);
|
|
3103
|
-
if (bound !== draft.id && list_workflows(
|
|
3271
|
+
if (bound !== draft.id && list_workflows(store).some(item => item.id === draft.id)) throw new Error(`工作流库已有 ${draft.id},主持人不能覆盖其他工作流;要启用它请用 --id ${draft.id},新建请换一个 ID`);
|
|
3104
3272
|
workflow = draft;
|
|
3105
3273
|
}
|
|
3106
|
-
assert_workflow_enabled(
|
|
3274
|
+
assert_workflow_enabled(store, workflow.id);
|
|
3107
3275
|
const missing = workflow.agents.filter(agent => !card_platform_ready(home, agent.name)).map(agent => agent.name || agent.card);
|
|
3108
3276
|
if (missing.length) {
|
|
3109
3277
|
const ready = configured_platforms(home);
|
|
@@ -3112,26 +3280,28 @@ export async function adopt_workflow(home, { access_file, draft_file, workflow_i
|
|
|
3112
3280
|
workflow = workflow_id ? { ...workflow, agents: host_cards(workflow.agents) }
|
|
3113
3281
|
// The host's draft replaces the room's own workflow on purpose (another one is refused above); a draft carries no
|
|
3114
3282
|
// sub-workflows or other files, so those are kept as saved.
|
|
3115
|
-
: save_workflow(
|
|
3283
|
+
: save_workflow(store, { ...workflow, agents: host_cards(workflow.agents) }, validate_preset, { overwrite: bound === workflow.id });
|
|
3116
3284
|
const owner = { site: config.site, room_id: config.room, token: config.token };
|
|
3117
3285
|
const members = await join_workflow_cards(home, config, roster, workflow, bound === workflow.id ? config.workflow.members || {} : {});
|
|
3118
|
-
const enabled = await room_workflow(home, { ...owner, action: 'enable', workflow: { id: workflow.id, members } });
|
|
3119
|
-
return { ...enabled, members, skill_path: join(workflow_directory(
|
|
3286
|
+
const enabled = await room_workflow(home, { ...owner, action: 'enable', workflow: { ...binding, id: workflow.id, members } });
|
|
3287
|
+
return { ...enabled, members, skill_path: join(workflow_directory(store, workflow.id), 'SKILL.md'), ...(workflow.warnings ? { warnings: workflow.warnings } : {}) };
|
|
3120
3288
|
}
|
|
3121
3289
|
|
|
3122
3290
|
/**
|
|
3123
3291
|
* Check a workflow folder or one-part draft as an import would, and optionally save it. A local AI uses this outside
|
|
3124
3292
|
* rooms with its user present, so the cards keep the project access they declare.
|
|
3125
3293
|
*/
|
|
3126
|
-
export function check_or_save_workflow(home, { dir, draft, revision, overwrite }, save = false) {
|
|
3294
|
+
export function check_or_save_workflow(home, { dir, draft, revision, overwrite, scope, project }, save = false) {
|
|
3295
|
+
if (scope && !['global', 'project'].includes(scope)) throw new Error('工作流作用域无效');
|
|
3127
3296
|
if (!dir === !draft) throw new Error('须提供 --dir 工作流文件夹或 --draft 草稿文件之一');
|
|
3128
3297
|
const source = dir ? { folder: dir } : { parts: [readFileSync(resolve(draft), 'utf8')] };
|
|
3129
3298
|
const preview = workflows_command(home, { action: 'preview', ...source });
|
|
3299
|
+
if (scope === 'project' && !library_projects(home, { cwd: project }).length) throw new Error('找不到项目目录');
|
|
3130
3300
|
if (!save) return preview;
|
|
3131
3301
|
const declared = list => list.map(({ requested_access, ...agent }) => requested_access ? { ...agent, project_access: requested_access } : agent);
|
|
3132
3302
|
const sub_workflows = preview.sub_workflows?.map(sub => ({ ...sub, agents: declared(sub.agents) }));
|
|
3133
|
-
return
|
|
3134
|
-
|
|
3303
|
+
return workflows_command(home, { action: 'save', scope, cwd: project, workflow: { ...preview, agents: declared(preview.agents), ...(sub_workflows ? { sub_workflows } : {}) },
|
|
3304
|
+
base_revision: revision, overwrite: overwrite === true });
|
|
3135
3305
|
}
|
|
3136
3306
|
|
|
3137
3307
|
const WRITING_ACCESS = new Set(['write', 'auto', 'full', 'own']);
|
|
@@ -3186,24 +3356,32 @@ export async function launch_main(args = process.argv.slice(2), input_text) {
|
|
|
3186
3356
|
file: { type: 'string' }, 'dry-run': { type: 'boolean' }, browser: { type: 'boolean' }, remote: { type: 'boolean' }, device: { type: 'string' },
|
|
3187
3357
|
'configuration-locked': { type: 'boolean' }, help: { type: 'boolean', short: 'h' }, overwrite: { type: 'boolean' }, preset: { type: 'string' }, workflow: { type: 'string' }, from: { type: 'string' },
|
|
3188
3358
|
'overwrite-preset': { type: 'boolean' }, 'replace-knowledge': { type: 'boolean' },
|
|
3189
|
-
draft: { type: 'string' }, 'access-file': { type: 'string' }, dir: { type: 'string' }, card: { type: 'string' }, sub: { type: 'string' }, id: { type: 'string' },
|
|
3359
|
+
'project-id': { type: 'string' }, scope: { type: 'string' }, project: { type: 'string' }, draft: { type: 'string' }, 'access-file': { type: 'string' }, dir: { type: 'string' }, card: { type: 'string' }, sub: { type: 'string' }, id: { type: 'string' },
|
|
3190
3360
|
member: { type: 'string' }, order: { type: 'string' }, revision: { type: 'string' }, name: { type: 'string' }, access: { type: 'string' },
|
|
3191
3361
|
model: { type: 'string' }, effort: { type: 'string' }, do: { type: 'string' }, execution: { type: 'string' }, text: { type: 'string' },
|
|
3362
|
+
'host-model': { type: 'string' }, 'host-effort': { type: 'string' }, interrupt: { type: 'boolean' }, 'body-file': { type: 'string' },
|
|
3192
3363
|
goal: { type: 'string' }, label: { type: 'string' }, description: { type: 'string' }, adapter: { type: 'string' }, 'submission-id': { type: 'string' },
|
|
3193
3364
|
message: { type: 'string' }, minutes: { type: 'string' }, off: { type: 'boolean' }, list: { type: 'boolean' },
|
|
3194
3365
|
title: { type: 'string' }, members: { type: 'string' }, host: { type: 'string' }, cwd: { type: 'string' }, room: { type: 'string' },
|
|
3195
3366
|
} });
|
|
3196
3367
|
if (values.help || !positionals.length) return process.stdout.write(command_help(positionals[0]));
|
|
3368
|
+
if (positionals[0] === 'workflow-promote') {
|
|
3369
|
+
const home = service_home();
|
|
3370
|
+
const { config } = await host_room(home, values['access-file'] || process.env.DISCUSS_ACCESS_FILE);
|
|
3371
|
+
if (!host_allowed(config, 'workflows')) throw new Error(host_refusal('workflows'));
|
|
3372
|
+
const result = workflows_command(home, { action: 'move', room_id: config.room, scope: 'project', project_id: values['project-id'], target_scope: 'global', id: values.id });
|
|
3373
|
+
return process.stdout.write(JSON.stringify(result) + '\n');
|
|
3374
|
+
}
|
|
3197
3375
|
if (positionals[0] === 'workflow-adopt') {
|
|
3198
3376
|
const home = service_home();
|
|
3199
|
-
const result = await adopt_workflow(home, { draft_file: values.draft, workflow_id: values.id, access_file: values['access-file'] || process.env.DISCUSS_ACCESS_FILE });
|
|
3377
|
+
const result = await adopt_workflow(home, { draft_file: values.draft, scope: values.scope, project_id: values['project-id'], workflow_id: values.id, access_file: values['access-file'] || process.env.DISCUSS_ACCESS_FILE });
|
|
3200
3378
|
return process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
3201
3379
|
}
|
|
3202
3380
|
if (positionals[0] === 'host-control') {
|
|
3203
3381
|
const { member, order, revision, name, access, model, effort, execution, text, goal, label, description, adapter } = values;
|
|
3204
3382
|
const result = await host_control(service_home(), positionals[1], { member, order, revision, name, access, model, effort, execution, text, goal,
|
|
3205
3383
|
label, description, adapter, message: values.message, minutes: values.minutes, off: values.off, list: values.list, do: values.do, submission_id: values['submission-id'],
|
|
3206
|
-
title: values.title, members: values.members, host: values.host, cwd: values.cwd, room: values.room, access_file: values['access-file'] || process.env.DISCUSS_ACCESS_FILE });
|
|
3384
|
+
title: values.title, members: values.members, host: values.host, cwd: values.cwd, room: values.room, interrupt: values.interrupt, body_file: values['body-file'], host_model: values['host-model'], host_effort: values['host-effort'], access_file: values['access-file'] || process.env.DISCUSS_ACCESS_FILE });
|
|
3207
3385
|
return process.stdout.write(`${JSON.stringify(result)}
|
|
3208
3386
|
`);
|
|
3209
3387
|
}
|
|
@@ -3225,7 +3403,10 @@ export async function launch_main(args = process.argv.slice(2), input_text) {
|
|
|
3225
3403
|
return process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
3226
3404
|
}
|
|
3227
3405
|
if (['workflow-check', 'workflow-save'].includes(positionals[0])) {
|
|
3228
|
-
|
|
3406
|
+
if (values.scope === 'project' && (values.remote || account_space(service_home()).projects_root)) {
|
|
3407
|
+
values.project = library_projects(service_home(), { project_id: values['project-id'], cwd: values.project }, true)[0]?.path;
|
|
3408
|
+
}
|
|
3409
|
+
const result = check_or_save_workflow(service_home(), { dir: values.dir, draft: values.draft, scope: values.scope, project: values.project, revision: values.revision, overwrite: values.overwrite === true }, positionals[0] === 'workflow-save');
|
|
3229
3410
|
return process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
3230
3411
|
}
|
|
3231
3412
|
const input = input_text === undefined ? (values.file ? JSON.parse(readFileSync(values.file, 'utf8').replace(/^\uFEFF/, '')) : null) : JSON.parse(input_text);
|
|
@@ -3235,7 +3416,13 @@ export async function launch_main(args = process.argv.slice(2), input_text) {
|
|
|
3235
3416
|
// An ordinary account's instance holds every request, from any page, to that account's own folders.
|
|
3236
3417
|
const space = account_space(home), restricted = Boolean(space.projects_root);
|
|
3237
3418
|
if (values.remote || restricted) {
|
|
3238
|
-
|
|
3419
|
+
if (positionals[0] === 'room-workflow' && input?.workflow?.scope === 'project') {
|
|
3420
|
+
const roots = library_projects(home, { room_id: input.room_id }, true);
|
|
3421
|
+
if (!roots.some(root => input.workflow.project_id ? root.id === input.workflow.project_id : root.primary)) throw new Error('找不到已授权的项目目录');
|
|
3422
|
+
}
|
|
3423
|
+
const guard_input = ['start', 'room-settings'].includes(positionals[0]) && Array.isArray(input?.extra_directories)
|
|
3424
|
+
? { ...input, extra_directories: input.extra_directories.map(item => typeof item === 'string' ? item : item?.path) } : input;
|
|
3425
|
+
guard_remote_request(positionals[0], guard_input, { projects: granted_projects(home), participants: launch_settings(home).participants, home });
|
|
3239
3426
|
if (values.remote && positionals[0] === 'room-workflow' && input?.action === 'host') throw new Error('主持人权限只能在电脑上开启');
|
|
3240
3427
|
if (restricted && positionals[0] === 'pick-directory') throw new Error('此账号只能使用自己的项目文件夹');
|
|
3241
3428
|
// Another device repeats the saved program locations it cannot see; keep them rather than dropping them.
|
|
@@ -3251,19 +3438,32 @@ export async function launch_main(args = process.argv.slice(2), input_text) {
|
|
|
3251
3438
|
// The online site an invitation would use, so an external expert's card says before the room exists whether a link can be made.
|
|
3252
3439
|
if (positionals[0] === 'settings') result.invite_site = await (await import('./external_gateway.mjs')).invite_site_state(home);
|
|
3253
3440
|
else if (positionals[0] === 'projects') result = input ? save_launch_projects(home, input) : launch_projects(home);
|
|
3254
|
-
else if (positionals[0] === 'presets') result =
|
|
3441
|
+
else if (positionals[0] === 'presets') result = presets_command(home, input || {}, values.remote || restricted);
|
|
3442
|
+
else if (['expert-export', 'expert-import'].includes(positionals[0]) && values.scope === 'project') {
|
|
3443
|
+
const root = library_projects(home, { cwd: values.project, project_id: values['project-id'] }, restricted || values.remote)[0];
|
|
3444
|
+
if (!root || root.readonly) throw new Error('找不到可写的项目目录');
|
|
3445
|
+
const store = project_store(home, root);
|
|
3446
|
+
if (positionals[0] === 'expert-export') result = export_expert(store, { preset: values.preset, workflow: values.workflow, card: values.card, dir: values.dir });
|
|
3447
|
+
else {
|
|
3448
|
+
const source = read_expert_source(values.from || '');
|
|
3449
|
+
result = values.workflow ? import_expert_to_workflow(store, { ...source, workflow: values.workflow, sub: values.sub }) :
|
|
3450
|
+
import_expert(store, { ...source, overwrite_preset: values['overwrite-preset'], replace_knowledge: values['replace-knowledge'] });
|
|
3451
|
+
}
|
|
3452
|
+
}
|
|
3255
3453
|
else if (positionals[0] === 'expert-export') result = export_expert(home, input || { preset: values.preset, workflow: values.workflow, card: values.card, dir: values.dir });
|
|
3256
3454
|
else if (positionals[0] === 'expert-import' && (values.workflow || input?.workflow)) {
|
|
3257
3455
|
result = import_expert_to_workflow(home, input || { ...read_expert_source(values.from || ''), workflow: values.workflow, sub: values.sub });
|
|
3258
3456
|
} else if (positionals[0] === 'expert-import') result = import_expert(home, input || { ...read_expert_source(values.from || ''),
|
|
3259
3457
|
overwrite_preset: values['overwrite-preset'] === true, replace_knowledge: values['replace-knowledge'] === true });
|
|
3260
|
-
else if (positionals[0] === 'workflows') result = await workflows_command(home, input || {});
|
|
3458
|
+
else if (positionals[0] === 'workflows') result = await workflows_command(home, input || {}, values.remote || restricted);
|
|
3261
3459
|
else if (positionals[0] === 'room-workflow' && input) result = await room_workflow(home, input);
|
|
3262
3460
|
else if (positionals[0] === 'room-settings' && input) result = await room_settings(home, input);
|
|
3263
3461
|
else if (positionals[0] === 'api-key' && input) result = save_api_key(home, { base_url: input.base_url, api_key: input.api_key });
|
|
3264
3462
|
else if (positionals[0] === 'api-check' && input) result = await api_check(home, input);
|
|
3265
3463
|
else if (positionals[0] === 'scheduler-check' && input) result = await scheduler_check(home, input);
|
|
3266
3464
|
else if (positionals[0] === 'image-check') result = await image_check(home);
|
|
3465
|
+
else if (positionals[0] === 'storage') result = await (await import('./admin_storage.mjs')).storage_details(home);
|
|
3466
|
+
else if (positionals[0] === 'storage-clean') result = await (await import('./admin_storage.mjs')).clean_storage(home, input || {});
|
|
3267
3467
|
else if (positionals[0] === 'image-settings') result = input && Object.keys(input).length ? save_image_settings(home, input) : image_settings(home);
|
|
3268
3468
|
else if (positionals[0] === 'tts-settings') result = input && Object.keys(input).length ? save_tts_settings(home, input) : tts_settings(home);
|
|
3269
3469
|
else if (positionals[0] === 'tts' && input) result = await tts_speak(home, input);
|
|
@@ -3271,7 +3471,7 @@ export async function launch_main(args = process.argv.slice(2), input_text) {
|
|
|
3271
3471
|
else if (positionals[0] === 'local-speech') result = input?.stop ? await local_service_stop(home, input.stop) : await local_services(home);
|
|
3272
3472
|
else if (positionals[0] === 'weixin') result = await weixin_command(home, input || {});
|
|
3273
3473
|
else if (positionals[0] === 'stt' && input) result = await stt_part(home, input);
|
|
3274
|
-
else if (positionals[0] === 'room-models' && input) result = await room_models(home, input);
|
|
3474
|
+
else if (positionals[0] === 'room-models' && input) result = await room_models(home, input, { restricted: values.remote || restricted });
|
|
3275
3475
|
else if (positionals[0] === 'room-skills' && input) result = await room_skills(home, input);
|
|
3276
3476
|
else if (positionals[0] === 'share-job' && input) result = await room_share_job(home, input);
|
|
3277
3477
|
else if (positionals[0] === 'room-files' && input) result = await room_files(home, input);
|