ai_co_discuss 0.4.19 → 0.4.21
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/backend/server.py +6 -3
- package/package.json +1 -1
- package/runner/adapters/antigravity_cli.mjs +6 -1
- package/runner/adapters/index.mjs +209 -204
- package/runner/discussion_launch.mjs +2260 -2258
- package/web/app.js +15 -1
- package/web/modern.css +1 -1
- package/web/modern.js +35 -35
package/backend/server.py
CHANGED
|
@@ -729,12 +729,15 @@ def list_rooms(connection, params=None):
|
|
|
729
729
|
where = "WHERE EXISTS (SELECT 1 FROM room_creations WHERE room_id=rooms.id AND submission_id=?)" if identifier else ""
|
|
730
730
|
rows = connection.execute(f"""SELECT rooms.*,
|
|
731
731
|
COALESCE(stats.message_count,0) AS message_count,COALESCE(stats.latest_id,0) AS latest_id,
|
|
732
|
-
(SELECT created_at FROM messages WHERE id=stats.latest_id) AS latest_at
|
|
732
|
+
(SELECT created_at FROM messages WHERE id=stats.latest_id) AS latest_at,
|
|
733
|
+
EXISTS (SELECT 1 FROM member_executions e WHERE e.room_id=rooms.id
|
|
734
|
+
AND e.status IN ('dispatched','started') AND e.lease_until>?) AS running
|
|
733
735
|
FROM rooms LEFT JOIN room_message_stats stats ON stats.room_id=rooms.id {where}
|
|
734
|
-
GROUP BY rooms.id ORDER BY rooms.created_at, rooms.id""", (identifier,) if identifier else ())
|
|
736
|
+
GROUP BY rooms.id ORDER BY rooms.created_at, rooms.id""", (int(time.time()), *((identifier,) if identifier else ())))
|
|
735
737
|
directory = {row["room_id"]: dict(row) for row in connection.execute("SELECT * FROM room_directory")}
|
|
736
738
|
assignments = {row["room_id"]: row["folder_id"] for row in connection.execute("SELECT room_id,folder_id FROM room_folder_assignments")}
|
|
737
|
-
rooms = [{**dict(row), "archived": bool(directory[row["id"]]["archived"]), "
|
|
739
|
+
rooms = [{**dict(row), "archived": bool(directory[row["id"]]["archived"]), "running": bool(row["running"]),
|
|
740
|
+
"folder_id": assignments[row["id"]]} for row in rows]
|
|
738
741
|
return {"rooms": sorted(rooms, key=lambda room: directory[room["id"]]["position"]),
|
|
739
742
|
**folder_directory(connection)}
|
|
740
743
|
|
package/package.json
CHANGED
|
@@ -475,6 +475,11 @@ async function defaults(profile, options = {}) {
|
|
|
475
475
|
catch { return { model: '', effort: '' }; }
|
|
476
476
|
}
|
|
477
477
|
|
|
478
|
+
/** The model chosen in agy's own settings; when it changes, a cached default is stale. Permission rules in the same file are ignored. */
|
|
479
|
+
function defaults_stamp(profile, user_home = homedir()) {
|
|
480
|
+
return String(read_json(join(user_home, '.gemini', 'antigravity-cli', 'settings.json')).model || '');
|
|
481
|
+
}
|
|
482
|
+
|
|
478
483
|
/**
|
|
479
484
|
* Whether another agy process has this conversation open: while it does, agy holds an exclusive lock on
|
|
480
485
|
* presence/<id>.lock, and reading the locked byte fails. Only Windows enforces that lock, so elsewhere this says false.
|
|
@@ -540,6 +545,6 @@ export const antigravity_cli = {
|
|
|
540
545
|
deliver,
|
|
541
546
|
invocation, lifecycle, events,
|
|
542
547
|
session: { path: session_path, exists: session_exists, owns: session_exists, branch, settings: session_settings },
|
|
543
|
-
models: { list: models, effort, defaults },
|
|
548
|
+
models: { list: models, effort, defaults, defaults_stamp },
|
|
544
549
|
skills: agy_skills,
|
|
545
550
|
};
|
|
@@ -1,204 +1,209 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The adapter layer's only entry for the rest of the runner. Each supported software has two parts:
|
|
3
|
-
*
|
|
4
|
-
* - its static facts in `profiles.mjs` (label, platform, config fields, efforts, access levels, capabilities), plain data
|
|
5
|
-
* the web page reads as well;
|
|
6
|
-
* - its behaviour in its own module, a descriptor with the same shape for every software:
|
|
7
|
-
* - `deliver(config, binding, task, spawn_process, user_home)` → `{session_id, status, capabilities, steer?}`, where
|
|
8
|
-
* `capabilities` `{stoppable, progress}` says what this one delivery can do, and `steer(text)` (when the running
|
|
9
|
-
* software can take an interjection without ending its turn) passes one in, resolving false once the turn is over;
|
|
10
|
-
* - `session`: `{path, exists, branch, settings}` for the software's own session store, or null;
|
|
11
|
-
* - `models`: `{list, effort?, defaults}` read from the installed software, or null when it is chosen inside the App;
|
|
12
|
-
* - optional `skills(profile, options)` lists installed skills `{id, label, description, prompt}` without a model turn;
|
|
13
|
-
* - optional `chat(config, binding, task, spawn_process, user_home)` for a private chat turn whose software a usual
|
|
14
|
-
* delivery cannot follow (an App, an open window, the model API): same result as `deliver`, and it reports through
|
|
15
|
-
* the task's callbacks — `on_note(text)` for each remark (the last is the reply), `on_exit()` or `on_failure(reason)`
|
|
16
|
-
* at the end, `on_process(handle)` with `kill()` when it can stop (`handle.stop_on_shutdown = false` when only the owner's stop may
|
|
17
|
-
* run it, not the runner's shutdown), `on_output(path, line)` for its process log;
|
|
18
|
-
* - optional `receipt(config, id)` for deliveries confirmed later (`status: 'sidecar_pending'`),
|
|
19
|
-
* `into_window(profile, binding)` (this delivery goes into the member's own open window), `cleanup(directory)`
|
|
20
|
-
* (remove what a deleted room left in the software) and `sweep(service_home, user_home)` (back up and remove retired
|
|
21
|
-
* artifacts from earlier versions).
|
|
22
|
-
*
|
|
23
|
-
* `adapter_of` merges the two. The room runner, launch settings and the website use these, never a software name
|
|
24
|
-
* (AGENTS.md red line 9).
|
|
25
|
-
*/
|
|
26
|
-
import { spawn } from 'node:child_process';
|
|
27
|
-
import { existsSync } from 'node:fs';
|
|
28
|
-
import { homedir } from 'node:os';
|
|
29
|
-
import { isAbsolute } from 'node:path';
|
|
30
|
-
import { antigravity } from './antigravity_app.mjs';
|
|
31
|
-
import { antigravity_cli } from './antigravity_cli.mjs';
|
|
32
|
-
import { api } from './api.mjs';
|
|
33
|
-
import { claude } from './claude.mjs';
|
|
34
|
-
import { codex } from './codex_app.mjs';
|
|
35
|
-
import { codex_cli } from './codex_cli.mjs';
|
|
36
|
-
import { execution_marker, MODEL_PATTERN } from './common.mjs';
|
|
37
|
-
import { adapter_capabilities, adapter_profile, ADAPTER_IDS } from './profiles.mjs';
|
|
38
|
-
|
|
39
|
-
export { execution_marker };
|
|
40
|
-
export { adapter_capabilities as capabilities_of };
|
|
41
|
-
|
|
42
|
-
export const ADAPTERS = Object.fromEntries([codex_cli, codex, claude, antigravity_cli, antigravity, api]
|
|
43
|
-
.map(behaviour => [behaviour.id, { ...adapter_profile(behaviour.id), ...behaviour }]));
|
|
44
|
-
const SESSION_PATTERN = /^[0-9A-Za-z_-]{8,128}$/;
|
|
45
|
-
|
|
46
|
-
/** The descriptor of a configured adapter, or null for a value this version does not support. */
|
|
47
|
-
export function adapter_of(id) { return ADAPTERS[id] || null; }
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Which adapter holds an existing conversation of the software a manual member registered with (`client`): the one
|
|
51
|
-
* whose store owns it, else the CLI that would resume it. '' when no adapter takes over that software.
|
|
52
|
-
*/
|
|
53
|
-
export function adoption_adapter(client, session_id, options = {}) {
|
|
54
|
-
const candidates = Object.values(ADAPTERS).filter(adapter => adapter.client === client && adapter.adopt);
|
|
55
|
-
return (candidates.find(adapter => adapter.session?.owns?.(session_id, options))
|
|
56
|
-
|| candidates.find(adapter => adapter.kind === 'cli') || candidates[0])?.id || '';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Check one configured software member: a supported adapter, its installed software located, and what it alone requires. */
|
|
60
|
-
export function validate_participant(participant) {
|
|
61
|
-
const adapter = adapter_of(participant.adapter);
|
|
62
|
-
if (!adapter) throw new Error(`适配器只能为 ${ADAPTER_IDS.join('/')}`);
|
|
63
|
-
if (adapter.location === 'executable' && !existsSync(participant.executable ?? '')) throw new Error('请指定实际原生可执行文件');
|
|
64
|
-
adapter.validate?.(participant);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Remove what a deleted room's members left inside their software, e.g. agy's session records and legacy projects. */
|
|
68
|
-
export function cleanup_room(directory) {
|
|
69
|
-
for (const adapter of Object.values(ADAPTERS)) adapter.cleanup?.(directory);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Back up and remove retired adapter artifacts left by earlier versions, without touching user-owned software state. */
|
|
73
|
-
export function sweep_adapters(service_home, user_home) {
|
|
74
|
-
return Object.values(ADAPTERS).reduce((count, adapter) => count + (adapter.sweep?.(service_home, user_home) || 0), 0);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Deliver one task through its member's adapter; only allowlisted adapters run. */
|
|
78
|
-
export async function deliver_task(config, binding, task, spawn_process = spawn, user_home = homedir()) {
|
|
79
|
-
const adapter = adapter_of(config.adapter);
|
|
80
|
-
if (!adapter) throw new Error('不支持的软件适配器');
|
|
81
|
-
// A private chat turn (`task.chat`) goes through the adapter's own `chat` when it has one, else as a usual delivery.
|
|
82
|
-
if (task.chat && adapter.chat) return adapter.chat(config, binding, task, spawn_process, user_home);
|
|
83
|
-
return adapter.deliver(config, binding, task, spawn_process, user_home);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** A delivery's later receipt (`status: 'sidecar_pending'`), or null while none has arrived. */
|
|
87
|
-
export function delivery_receipt(config, id) {
|
|
88
|
-
return adapter_of(config.adapter)?.receipt?.(config, id) ?? null;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Read a persisted delivery, including the delivery names written by the pre-capability runner. */
|
|
92
|
-
export function stored_delivery(value) {
|
|
93
|
-
const legacy = { process: { stoppable: true, progress: true }, api: { stoppable: true, progress: true },
|
|
94
|
-
window: { stoppable: false, progress: false }, app: { stoppable: false, progress: false }, sidecar: { stoppable: false, progress: false } };
|
|
95
|
-
return Object.hasOwn(legacy, value) ? legacy[value] : JSON.parse(value);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// What one execution-detail answer carries: the latest entries, each body cut to a readable length.
|
|
99
|
-
const DETAIL_ENTRY_LIMIT = 300;
|
|
100
|
-
const DETAIL_BODY_LIMIT = 4000;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* What an agent did in one execution, rebuilt from the raw event log its adapter's process wrote: messages, commands
|
|
104
|
-
* with their output, searches, edits and tool calls in order. Each adapter's `events.detail(event)` turns one event into
|
|
105
|
-
* entries `{id?, kind, title?, body?, append?, state?, at?, ended_at?}`; an entry with a known id updates the earlier
|
|
106
|
-
* one in place (a command's result fills in its start), `append` extends its body. `at` / `ended_at` are ISO times, given
|
|
107
|
-
* only by software whose log carries them; without them no step time shows. Returns null when the adapter keeps no such log.
|
|
108
|
-
*/
|
|
109
|
-
export function execution_details(adapter_id, text) {
|
|
110
|
-
const adapter = adapter_of(adapter_id);
|
|
111
|
-
if (!adapter) return null;
|
|
112
|
-
const detail = adapter.events?.detail || (() => []);
|
|
113
|
-
const entries = new Map();
|
|
114
|
-
for (const line of text.split('\n')) {
|
|
115
|
-
let event;
|
|
116
|
-
try { event = JSON.parse(line); } catch { continue; }
|
|
117
|
-
// The runner marks where an interjection went into the turn; it reads the same for every software.
|
|
118
|
-
// An adapter without a CLI stream logs ready entries (`codiscuss.entry`); the same reader shows them.
|
|
119
|
-
const parts = event.type === 'codiscuss.interjection' ? [{ kind: 'interjection', body: String(event.text || '') }]
|
|
120
|
-
: event.type === 'codiscuss.entry' ? [event.entry || {}] : detail(event);
|
|
121
|
-
for (const { append, ...fields } of parts || []) {
|
|
122
|
-
const id = fields.id ?? `entry-${entries.size}`;
|
|
123
|
-
const entry = { ...entries.get(id), ...Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined)), id };
|
|
124
|
-
if (append) entry.body = (entry.body || '') + append;
|
|
125
|
-
entries.set(id, entry);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
const all = [...entries.values()].filter(entry => entry.kind);
|
|
129
|
-
return { total: all.length, entries: all.slice(-DETAIL_ENTRY_LIMIT).map(entry => entry.body?.length > DETAIL_BODY_LIMIT
|
|
130
|
-
? { ...entry, body: entry.body.slice(0, DETAIL_BODY_LIMIT), truncated: true } : entry) };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** Adapters whose sessions this machine can cut or branch; the website lists taken-back executions only for these. */
|
|
134
|
-
export const BRANCHABLE_ADAPTERS = Object.values(ADAPTERS).filter(adapter => adapter.session?.branch).map(adapter => adapter.id);
|
|
135
|
-
|
|
136
|
-
/** Whether a session exists on this machine, e.g. to tell if a pending Claude fork already happened. */
|
|
137
|
-
export function session_exists(adapter_id, session_id, options = {}) {
|
|
138
|
-
return Boolean(adapter_of(adapter_id)?.session?.exists(session_id, options));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Branch a session before the first of some taken-back executions it contains, leaving the original untouched.
|
|
143
|
-
*
|
|
144
|
-
* @param {object} profile The member's local software profile (`adapter`, `executable`).
|
|
145
|
-
* @param {string} session_id The session the member is bound to.
|
|
146
|
-
* @param {number[]} executions Execution IDs whose turns the branch must not contain.
|
|
147
|
-
* @param {object} options `always` branches even when none of them is in the session (a forked room needs its own copy).
|
|
148
|
-
* @returns {Promise<null|{session_id: string, fork?: {from: string, at: string}}>} null when nothing needs cutting;
|
|
149
|
-
* an empty session_id starts a new conversation; `fork` asks Claude's next turn to fork into session_id.
|
|
150
|
-
*/
|
|
151
|
-
export async function branch_session(profile, session_id, executions, { always = false, env = process.env, home = homedir(), spawn_process = spawn } = {}) {
|
|
152
|
-
const branch = adapter_of(profile.adapter)?.session?.branch;
|
|
153
|
-
if (!branch) throw new Error(`${profile.adapter} 的会话不能由本机截断`);
|
|
154
|
-
return branch(profile, session_id, executions.map(execution_marker), { always, env, home, spawn_process });
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* The model and effort a session last ran with, read from the software's own session store; no AI turn is started.
|
|
159
|
-
* These are each software's internal formats, so anything unreadable yields empty strings rather than an error.
|
|
160
|
-
*/
|
|
161
|
-
export function session_settings(adapter_id, session_id, options = {}) {
|
|
162
|
-
const none = { model: '', effort: '' };
|
|
163
|
-
if (typeof session_id !== 'string' || !SESSION_PATTERN.test(session_id)) return none;
|
|
164
|
-
try { return adapter_of(adapter_id)?.session?.settings(session_id, options) || none; }
|
|
165
|
-
catch { return none; }
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** Public model metadata only; failures leave the original-software default available to the UI. */
|
|
169
|
-
export async function software_models(profile, options = {}) {
|
|
170
|
-
const adapter = adapter_of(profile.adapter);
|
|
171
|
-
const result = { adapter: profile.adapter, models: [], source: adapter?.catalog_source || 'original-software' };
|
|
172
|
-
if (!adapter?.models) return { ...result, error: '此入口的模型在原软件内设置' };
|
|
173
|
-
if (typeof profile.executable !== 'string' || !isAbsolute(profile.executable)) return { ...result, error: '请先配置 CLI 的完整路径,再读取模型列表' };
|
|
174
|
-
try {
|
|
175
|
-
const models = (await adapter.models.list(profile, options)).filter(Boolean);
|
|
176
|
-
result.models = [...new Map(models.map(model => [model.id, model])).values()];
|
|
177
|
-
if (!result.models.length) result.error = '软件未返回可选模型,请检查版本、登录与网络;仍可使用软件默认模型';
|
|
178
|
-
} catch (error) { result.error = error.message; }
|
|
179
|
-
return result;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** The effort levels the software offers for the selected model, where it can tell without an agent turn. */
|
|
183
|
-
export async function software_effort(profile, options = {}) {
|
|
184
|
-
const effort = adapter_of(profile.adapter)?.models?.effort;
|
|
185
|
-
if (!effort || !isAbsolute(profile.executable || '') || (profile.model && !MODEL_PATTERN.test(profile.model))) return { error: '无法读取所选模型的思考档位' };
|
|
186
|
-
try { return { levels: await effort(profile, options) }; }
|
|
187
|
-
catch { return { error: '无法读取原生思考档位;保留 CLI 默认值,或检查软件版本后重试' }; }
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** What a CLI runs with when the card leaves model or effort empty. */
|
|
191
|
-
export async function software_defaults(profile, options = {}) {
|
|
192
|
-
return (await adapter_of(profile.adapter)?.models?.defaults(profile, options)) || { model: '', effort: '' };
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
export
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return { available:
|
|
204
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The adapter layer's only entry for the rest of the runner. Each supported software has two parts:
|
|
3
|
+
*
|
|
4
|
+
* - its static facts in `profiles.mjs` (label, platform, config fields, efforts, access levels, capabilities), plain data
|
|
5
|
+
* the web page reads as well;
|
|
6
|
+
* - its behaviour in its own module, a descriptor with the same shape for every software:
|
|
7
|
+
* - `deliver(config, binding, task, spawn_process, user_home)` → `{session_id, status, capabilities, steer?}`, where
|
|
8
|
+
* `capabilities` `{stoppable, progress}` says what this one delivery can do, and `steer(text)` (when the running
|
|
9
|
+
* software can take an interjection without ending its turn) passes one in, resolving false once the turn is over;
|
|
10
|
+
* - `session`: `{path, exists, branch, settings}` for the software's own session store, or null;
|
|
11
|
+
* - `models`: `{list, effort?, defaults}` read from the installed software, or null when it is chosen inside the App;
|
|
12
|
+
* - optional `skills(profile, options)` lists installed skills `{id, label, description, prompt}` without a model turn;
|
|
13
|
+
* - optional `chat(config, binding, task, spawn_process, user_home)` for a private chat turn whose software a usual
|
|
14
|
+
* delivery cannot follow (an App, an open window, the model API): same result as `deliver`, and it reports through
|
|
15
|
+
* the task's callbacks — `on_note(text)` for each remark (the last is the reply), `on_exit()` or `on_failure(reason)`
|
|
16
|
+
* at the end, `on_process(handle)` with `kill()` when it can stop (`handle.stop_on_shutdown = false` when only the owner's stop may
|
|
17
|
+
* run it, not the runner's shutdown), `on_output(path, line)` for its process log;
|
|
18
|
+
* - optional `receipt(config, id)` for deliveries confirmed later (`status: 'sidecar_pending'`),
|
|
19
|
+
* `into_window(profile, binding)` (this delivery goes into the member's own open window), `cleanup(directory)`
|
|
20
|
+
* (remove what a deleted room left in the software) and `sweep(service_home, user_home)` (back up and remove retired
|
|
21
|
+
* artifacts from earlier versions).
|
|
22
|
+
*
|
|
23
|
+
* `adapter_of` merges the two. The room runner, launch settings and the website use these, never a software name
|
|
24
|
+
* (AGENTS.md red line 9).
|
|
25
|
+
*/
|
|
26
|
+
import { spawn } from 'node:child_process';
|
|
27
|
+
import { existsSync } from 'node:fs';
|
|
28
|
+
import { homedir } from 'node:os';
|
|
29
|
+
import { isAbsolute } from 'node:path';
|
|
30
|
+
import { antigravity } from './antigravity_app.mjs';
|
|
31
|
+
import { antigravity_cli } from './antigravity_cli.mjs';
|
|
32
|
+
import { api } from './api.mjs';
|
|
33
|
+
import { claude } from './claude.mjs';
|
|
34
|
+
import { codex } from './codex_app.mjs';
|
|
35
|
+
import { codex_cli } from './codex_cli.mjs';
|
|
36
|
+
import { execution_marker, MODEL_PATTERN } from './common.mjs';
|
|
37
|
+
import { adapter_capabilities, adapter_profile, ADAPTER_IDS } from './profiles.mjs';
|
|
38
|
+
|
|
39
|
+
export { execution_marker };
|
|
40
|
+
export { adapter_capabilities as capabilities_of };
|
|
41
|
+
|
|
42
|
+
export const ADAPTERS = Object.fromEntries([codex_cli, codex, claude, antigravity_cli, antigravity, api]
|
|
43
|
+
.map(behaviour => [behaviour.id, { ...adapter_profile(behaviour.id), ...behaviour }]));
|
|
44
|
+
const SESSION_PATTERN = /^[0-9A-Za-z_-]{8,128}$/;
|
|
45
|
+
|
|
46
|
+
/** The descriptor of a configured adapter, or null for a value this version does not support. */
|
|
47
|
+
export function adapter_of(id) { return ADAPTERS[id] || null; }
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Which adapter holds an existing conversation of the software a manual member registered with (`client`): the one
|
|
51
|
+
* whose store owns it, else the CLI that would resume it. '' when no adapter takes over that software.
|
|
52
|
+
*/
|
|
53
|
+
export function adoption_adapter(client, session_id, options = {}) {
|
|
54
|
+
const candidates = Object.values(ADAPTERS).filter(adapter => adapter.client === client && adapter.adopt);
|
|
55
|
+
return (candidates.find(adapter => adapter.session?.owns?.(session_id, options))
|
|
56
|
+
|| candidates.find(adapter => adapter.kind === 'cli') || candidates[0])?.id || '';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Check one configured software member: a supported adapter, its installed software located, and what it alone requires. */
|
|
60
|
+
export function validate_participant(participant) {
|
|
61
|
+
const adapter = adapter_of(participant.adapter);
|
|
62
|
+
if (!adapter) throw new Error(`适配器只能为 ${ADAPTER_IDS.join('/')}`);
|
|
63
|
+
if (adapter.location === 'executable' && !existsSync(participant.executable ?? '')) throw new Error('请指定实际原生可执行文件');
|
|
64
|
+
adapter.validate?.(participant);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Remove what a deleted room's members left inside their software, e.g. agy's session records and legacy projects. */
|
|
68
|
+
export function cleanup_room(directory) {
|
|
69
|
+
for (const adapter of Object.values(ADAPTERS)) adapter.cleanup?.(directory);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Back up and remove retired adapter artifacts left by earlier versions, without touching user-owned software state. */
|
|
73
|
+
export function sweep_adapters(service_home, user_home) {
|
|
74
|
+
return Object.values(ADAPTERS).reduce((count, adapter) => count + (adapter.sweep?.(service_home, user_home) || 0), 0);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Deliver one task through its member's adapter; only allowlisted adapters run. */
|
|
78
|
+
export async function deliver_task(config, binding, task, spawn_process = spawn, user_home = homedir()) {
|
|
79
|
+
const adapter = adapter_of(config.adapter);
|
|
80
|
+
if (!adapter) throw new Error('不支持的软件适配器');
|
|
81
|
+
// A private chat turn (`task.chat`) goes through the adapter's own `chat` when it has one, else as a usual delivery.
|
|
82
|
+
if (task.chat && adapter.chat) return adapter.chat(config, binding, task, spawn_process, user_home);
|
|
83
|
+
return adapter.deliver(config, binding, task, spawn_process, user_home);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A delivery's later receipt (`status: 'sidecar_pending'`), or null while none has arrived. */
|
|
87
|
+
export function delivery_receipt(config, id) {
|
|
88
|
+
return adapter_of(config.adapter)?.receipt?.(config, id) ?? null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Read a persisted delivery, including the delivery names written by the pre-capability runner. */
|
|
92
|
+
export function stored_delivery(value) {
|
|
93
|
+
const legacy = { process: { stoppable: true, progress: true }, api: { stoppable: true, progress: true },
|
|
94
|
+
window: { stoppable: false, progress: false }, app: { stoppable: false, progress: false }, sidecar: { stoppable: false, progress: false } };
|
|
95
|
+
return Object.hasOwn(legacy, value) ? legacy[value] : JSON.parse(value);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// What one execution-detail answer carries: the latest entries, each body cut to a readable length.
|
|
99
|
+
const DETAIL_ENTRY_LIMIT = 300;
|
|
100
|
+
const DETAIL_BODY_LIMIT = 4000;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* What an agent did in one execution, rebuilt from the raw event log its adapter's process wrote: messages, commands
|
|
104
|
+
* with their output, searches, edits and tool calls in order. Each adapter's `events.detail(event)` turns one event into
|
|
105
|
+
* entries `{id?, kind, title?, body?, append?, state?, at?, ended_at?}`; an entry with a known id updates the earlier
|
|
106
|
+
* one in place (a command's result fills in its start), `append` extends its body. `at` / `ended_at` are ISO times, given
|
|
107
|
+
* only by software whose log carries them; without them no step time shows. Returns null when the adapter keeps no such log.
|
|
108
|
+
*/
|
|
109
|
+
export function execution_details(adapter_id, text) {
|
|
110
|
+
const adapter = adapter_of(adapter_id);
|
|
111
|
+
if (!adapter) return null;
|
|
112
|
+
const detail = adapter.events?.detail || (() => []);
|
|
113
|
+
const entries = new Map();
|
|
114
|
+
for (const line of text.split('\n')) {
|
|
115
|
+
let event;
|
|
116
|
+
try { event = JSON.parse(line); } catch { continue; }
|
|
117
|
+
// The runner marks where an interjection went into the turn; it reads the same for every software.
|
|
118
|
+
// An adapter without a CLI stream logs ready entries (`codiscuss.entry`); the same reader shows them.
|
|
119
|
+
const parts = event.type === 'codiscuss.interjection' ? [{ kind: 'interjection', body: String(event.text || '') }]
|
|
120
|
+
: event.type === 'codiscuss.entry' ? [event.entry || {}] : detail(event);
|
|
121
|
+
for (const { append, ...fields } of parts || []) {
|
|
122
|
+
const id = fields.id ?? `entry-${entries.size}`;
|
|
123
|
+
const entry = { ...entries.get(id), ...Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined)), id };
|
|
124
|
+
if (append) entry.body = (entry.body || '') + append;
|
|
125
|
+
entries.set(id, entry);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const all = [...entries.values()].filter(entry => entry.kind);
|
|
129
|
+
return { total: all.length, entries: all.slice(-DETAIL_ENTRY_LIMIT).map(entry => entry.body?.length > DETAIL_BODY_LIMIT
|
|
130
|
+
? { ...entry, body: entry.body.slice(0, DETAIL_BODY_LIMIT), truncated: true } : entry) };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Adapters whose sessions this machine can cut or branch; the website lists taken-back executions only for these. */
|
|
134
|
+
export const BRANCHABLE_ADAPTERS = Object.values(ADAPTERS).filter(adapter => adapter.session?.branch).map(adapter => adapter.id);
|
|
135
|
+
|
|
136
|
+
/** Whether a session exists on this machine, e.g. to tell if a pending Claude fork already happened. */
|
|
137
|
+
export function session_exists(adapter_id, session_id, options = {}) {
|
|
138
|
+
return Boolean(adapter_of(adapter_id)?.session?.exists(session_id, options));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Branch a session before the first of some taken-back executions it contains, leaving the original untouched.
|
|
143
|
+
*
|
|
144
|
+
* @param {object} profile The member's local software profile (`adapter`, `executable`).
|
|
145
|
+
* @param {string} session_id The session the member is bound to.
|
|
146
|
+
* @param {number[]} executions Execution IDs whose turns the branch must not contain.
|
|
147
|
+
* @param {object} options `always` branches even when none of them is in the session (a forked room needs its own copy).
|
|
148
|
+
* @returns {Promise<null|{session_id: string, fork?: {from: string, at: string}}>} null when nothing needs cutting;
|
|
149
|
+
* an empty session_id starts a new conversation; `fork` asks Claude's next turn to fork into session_id.
|
|
150
|
+
*/
|
|
151
|
+
export async function branch_session(profile, session_id, executions, { always = false, env = process.env, home = homedir(), spawn_process = spawn } = {}) {
|
|
152
|
+
const branch = adapter_of(profile.adapter)?.session?.branch;
|
|
153
|
+
if (!branch) throw new Error(`${profile.adapter} 的会话不能由本机截断`);
|
|
154
|
+
return branch(profile, session_id, executions.map(execution_marker), { always, env, home, spawn_process });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The model and effort a session last ran with, read from the software's own session store; no AI turn is started.
|
|
159
|
+
* These are each software's internal formats, so anything unreadable yields empty strings rather than an error.
|
|
160
|
+
*/
|
|
161
|
+
export function session_settings(adapter_id, session_id, options = {}) {
|
|
162
|
+
const none = { model: '', effort: '' };
|
|
163
|
+
if (typeof session_id !== 'string' || !SESSION_PATTERN.test(session_id)) return none;
|
|
164
|
+
try { return adapter_of(adapter_id)?.session?.settings(session_id, options) || none; }
|
|
165
|
+
catch { return none; }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Public model metadata only; failures leave the original-software default available to the UI. */
|
|
169
|
+
export async function software_models(profile, options = {}) {
|
|
170
|
+
const adapter = adapter_of(profile.adapter);
|
|
171
|
+
const result = { adapter: profile.adapter, models: [], source: adapter?.catalog_source || 'original-software' };
|
|
172
|
+
if (!adapter?.models) return { ...result, error: '此入口的模型在原软件内设置' };
|
|
173
|
+
if (typeof profile.executable !== 'string' || !isAbsolute(profile.executable)) return { ...result, error: '请先配置 CLI 的完整路径,再读取模型列表' };
|
|
174
|
+
try {
|
|
175
|
+
const models = (await adapter.models.list(profile, options)).filter(Boolean);
|
|
176
|
+
result.models = [...new Map(models.map(model => [model.id, model])).values()];
|
|
177
|
+
if (!result.models.length) result.error = '软件未返回可选模型,请检查版本、登录与网络;仍可使用软件默认模型';
|
|
178
|
+
} catch (error) { result.error = error.message; }
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The effort levels the software offers for the selected model, where it can tell without an agent turn. */
|
|
183
|
+
export async function software_effort(profile, options = {}) {
|
|
184
|
+
const effort = adapter_of(profile.adapter)?.models?.effort;
|
|
185
|
+
if (!effort || !isAbsolute(profile.executable || '') || (profile.model && !MODEL_PATTERN.test(profile.model))) return { error: '无法读取所选模型的思考档位' };
|
|
186
|
+
try { return { levels: await effort(profile, options) }; }
|
|
187
|
+
catch { return { error: '无法读取原生思考档位;保留 CLI 默认值,或检查软件版本后重试' }; }
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** What a CLI runs with when the card leaves model or effort empty. */
|
|
191
|
+
export async function software_defaults(profile, options = {}) {
|
|
192
|
+
return (await adapter_of(profile.adapter)?.models?.defaults(profile, options)) || { model: '', effort: '' };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A cheap marker of the software's default-model setting, so a cached default is read again after the user changes it. */
|
|
196
|
+
export function software_defaults_stamp(profile) {
|
|
197
|
+
return adapter_of(profile.adapter)?.models?.defaults_stamp?.(profile) || '';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** List only skills the member's own adapter can discover; an unsupported source is not an empty catalog. */
|
|
201
|
+
export async function software_skills(profile, options = {}) {
|
|
202
|
+
const read = adapter_of(profile.adapter)?.skills;
|
|
203
|
+
if (!read) return { available: false, reason: '此接入方式暂不支持读取本机技能', skills: [] };
|
|
204
|
+
const skills = await read(profile, options);
|
|
205
|
+
// Native catalogs can contain a project and a user copy with the same invocation name; keep the first (higher priority).
|
|
206
|
+
const unique = new Map();
|
|
207
|
+
for (const skill of skills) if (!unique.has(skill.id)) unique.set(skill.id, skill);
|
|
208
|
+
return { available: true, skills: [...unique.values()] };
|
|
209
|
+
}
|