@hivemagnus/ardent-studio 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +133 -2
- package/dist/agents/config-edit.d.ts +20 -0
- package/dist/agents/config-edit.js +75 -0
- package/dist/agents/directory.d.ts +27 -0
- package/dist/agents/directory.js +97 -0
- package/dist/agents/operations.d.ts +47 -0
- package/dist/agents/operations.js +93 -0
- package/dist/auth/links.d.ts +27 -0
- package/dist/auth/links.js +46 -0
- package/dist/auth/oathweave.d.ts +46 -0
- package/dist/auth/oathweave.js +123 -0
- package/dist/blobs/store.d.ts +45 -0
- package/dist/blobs/store.js +159 -0
- package/dist/clients/chattermost.d.ts +16 -0
- package/dist/clients/chattermost.js +36 -0
- package/dist/clients/hivemem.d.ts +85 -0
- package/dist/clients/hivemem.js +89 -0
- package/dist/clients/magnus.d.ts +851 -0
- package/dist/clients/magnus.js +541 -0
- package/dist/clients/service-token.d.ts +24 -0
- package/dist/clients/service-token.js +87 -0
- package/dist/clients/sse.d.ts +8 -0
- package/dist/clients/sse.js +42 -0
- package/dist/conductor/activity.d.ts +38 -0
- package/dist/conductor/activity.js +101 -0
- package/dist/conductor/addressing.d.ts +24 -0
- package/dist/conductor/addressing.js +42 -0
- package/dist/conductor/conductor.d.ts +225 -0
- package/dist/conductor/conductor.js +939 -0
- package/dist/conductor/follower.d.ts +48 -0
- package/dist/conductor/follower.js +154 -0
- package/dist/conductor/live.d.ts +23 -0
- package/dist/conductor/live.js +32 -0
- package/dist/conductor/prompting.d.ts +74 -0
- package/dist/conductor/prompting.js +177 -0
- package/dist/config.d.ts +52 -0
- package/dist/config.js +163 -0
- package/dist/db/database.d.ts +12 -0
- package/dist/db/database.js +62 -0
- package/dist/db/json.d.ts +8 -0
- package/dist/db/json.js +13 -0
- package/dist/db/migrations.d.ts +6 -0
- package/dist/db/migrations.js +571 -0
- package/dist/db/repos/agents.d.ts +86 -0
- package/dist/db/repos/agents.js +92 -0
- package/dist/db/repos/audit.d.ts +37 -0
- package/dist/db/repos/audit.js +20 -0
- package/dist/db/repos/events.d.ts +29 -0
- package/dist/db/repos/events.js +55 -0
- package/dist/db/repos/files.d.ts +41 -0
- package/dist/db/repos/files.js +72 -0
- package/dist/db/repos/idempotency.d.ts +27 -0
- package/dist/db/repos/idempotency.js +46 -0
- package/dist/db/repos/links.d.ts +47 -0
- package/dist/db/repos/links.js +99 -0
- package/dist/db/repos/projects.d.ts +44 -0
- package/dist/db/repos/projects.js +107 -0
- package/dist/db/repos/proposals.d.ts +46 -0
- package/dist/db/repos/proposals.js +65 -0
- package/dist/db/repos/review.d.ts +191 -0
- package/dist/db/repos/review.js +195 -0
- package/dist/db/repos/room.d.ts +163 -0
- package/dist/db/repos/room.js +151 -0
- package/dist/db/repos/sessions.d.ts +36 -0
- package/dist/db/repos/sessions.js +30 -0
- package/dist/db/repos/summaries.d.ts +22 -0
- package/dist/db/repos/summaries.js +105 -0
- package/dist/db/repos/tasks.d.ts +103 -0
- package/dist/db/repos/tasks.js +112 -0
- package/dist/db/repos/team.d.ts +49 -0
- package/dist/db/repos/team.js +95 -0
- package/dist/db/repos/templates.d.ts +55 -0
- package/dist/db/repos/templates.js +120 -0
- package/dist/db/store.d.ts +57 -0
- package/dist/db/store.js +108 -0
- package/dist/events/bus.d.ts +21 -0
- package/dist/events/bus.js +31 -0
- package/dist/events/types.d.ts +3 -0
- package/dist/events/types.js +1 -0
- package/dist/ids.d.ts +28 -0
- package/dist/ids.js +81 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/knowledge/extract.d.ts +17 -0
- package/dist/knowledge/extract.js +82 -0
- package/dist/knowledge/sync.d.ts +40 -0
- package/dist/knowledge/sync.js +172 -0
- package/dist/logger.d.ts +5 -0
- package/dist/logger.js +11 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +42 -0
- package/dist/review/anchors.d.ts +16 -0
- package/dist/review/anchors.js +67 -0
- package/dist/review/context.d.ts +4 -0
- package/dist/review/context.js +29 -0
- package/dist/review/docx.d.ts +29 -0
- package/dist/review/docx.js +21 -0
- package/dist/review/export.d.ts +16 -0
- package/dist/review/export.js +84 -0
- package/dist/review/importer.d.ts +68 -0
- package/dist/review/importer.js +215 -0
- package/dist/review/index.d.ts +13 -0
- package/dist/review/index.js +7 -0
- package/dist/server/app.d.ts +5 -0
- package/dist/server/app.js +44 -0
- package/dist/server/deps.d.ts +43 -0
- package/dist/server/deps.js +17 -0
- package/dist/server/env.d.ts +6 -0
- package/dist/server/env.js +1 -0
- package/dist/server/errors.d.ts +12 -0
- package/dist/server/errors.js +25 -0
- package/dist/server/routes/agent-studio.d.ts +5 -0
- package/dist/server/routes/agent-studio.js +441 -0
- package/dist/server/routes/agents.d.ts +3 -0
- package/dist/server/routes/agents.js +4 -0
- package/dist/server/routes/files.d.ts +3 -0
- package/dist/server/routes/files.js +88 -0
- package/dist/server/routes/health.d.ts +3 -0
- package/dist/server/routes/health.js +31 -0
- package/dist/server/routes/links.d.ts +3 -0
- package/dist/server/routes/links.js +79 -0
- package/dist/server/routes/projects.d.ts +3 -0
- package/dist/server/routes/projects.js +119 -0
- package/dist/server/routes/review.d.ts +3 -0
- package/dist/server/routes/review.js +198 -0
- package/dist/server/routes/room.d.ts +9 -0
- package/dist/server/routes/room.js +224 -0
- package/dist/server/routes/stream.d.ts +4 -0
- package/dist/server/routes/stream.js +104 -0
- package/dist/server/routes/team.d.ts +3 -0
- package/dist/server/routes/team.js +183 -0
- package/dist/server/routes/templates.d.ts +11 -0
- package/dist/server/routes/templates.js +103 -0
- package/dist/server/routes/work.d.ts +3 -0
- package/dist/server/routes/work.js +139 -0
- package/dist/server/routing.d.ts +31 -0
- package/dist/server/routing.js +132 -0
- package/dist/service.d.ts +34 -0
- package/dist/service.js +93 -0
- package/dist/shared/browser.d.ts +5824 -0
- package/dist/shared/browser.js +1318 -0
- package/dist/team/proposals.d.ts +129 -0
- package/dist/team/proposals.js +349 -0
- package/dist/team/service.d.ts +12 -0
- package/dist/team/service.js +13 -0
- package/dist/team/validation.d.ts +37 -0
- package/dist/team/validation.js +157 -0
- package/dist/templates/secrets.d.ts +2 -0
- package/dist/templates/secrets.js +23 -0
- package/dist/templates/seeds.d.ts +34 -0
- package/dist/templates/seeds.js +277 -0
- package/dist/url.d.ts +2 -0
- package/dist/url.js +7 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +11 -0
- package/docs/openapi-browser.json +12658 -0
- package/package.json +91 -4
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** One server-sent event. `id` is absent for events that are not resumable. */
|
|
2
|
+
export interface SseMessage {
|
|
3
|
+
event: string;
|
|
4
|
+
data: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Parse a text/event-stream body. Comments are skipped; a final unterminated event is dropped, as the spec says. */
|
|
8
|
+
export declare function parseSse(body: ReadableStream<Uint8Array>, maxEventBytes?: number): AsyncGenerator<SseMessage>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Parse a text/event-stream body. Comments are skipped; a final unterminated event is dropped, as the spec says. */
|
|
2
|
+
export async function* parseSse(body, maxEventBytes = 4 * 1024 * 1024) {
|
|
3
|
+
const decoder = new TextDecoder();
|
|
4
|
+
let buffer = '';
|
|
5
|
+
let event = '';
|
|
6
|
+
let data = [];
|
|
7
|
+
let id;
|
|
8
|
+
let size = 0;
|
|
9
|
+
for await (const chunk of body) {
|
|
10
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
11
|
+
let newline;
|
|
12
|
+
while ((newline = buffer.search(/\r\n|\r|\n/)) >= 0) {
|
|
13
|
+
const line = buffer.slice(0, newline);
|
|
14
|
+
buffer = buffer.slice(newline + (buffer.startsWith('\r\n', newline) ? 2 : 1));
|
|
15
|
+
if (line === '') {
|
|
16
|
+
if (data.length)
|
|
17
|
+
yield { event: event || 'message', data: data.join('\n'), ...(id !== undefined ? { id } : {}) };
|
|
18
|
+
event = '';
|
|
19
|
+
data = [];
|
|
20
|
+
id = undefined;
|
|
21
|
+
size = 0;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (line.startsWith(':'))
|
|
25
|
+
continue;
|
|
26
|
+
const colon = line.indexOf(':');
|
|
27
|
+
const field = colon < 0 ? line : line.slice(0, colon);
|
|
28
|
+
const value = colon < 0 ? '' : line.slice(colon + 1).replace(/^ /, '');
|
|
29
|
+
size += value.length;
|
|
30
|
+
if (size > maxEventBytes)
|
|
31
|
+
throw new Error('Server-sent event exceeds the size limit');
|
|
32
|
+
if (field === 'event')
|
|
33
|
+
event = value;
|
|
34
|
+
else if (field === 'data')
|
|
35
|
+
data.push(value);
|
|
36
|
+
else if (field === 'id' && !value.includes('\0'))
|
|
37
|
+
id = value;
|
|
38
|
+
}
|
|
39
|
+
if (buffer.length > maxEventBytes)
|
|
40
|
+
throw new Error('Server-sent event exceeds the size limit');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { DeliverableRevision } from '../db/repos/review.js';
|
|
3
|
+
import type { Message } from '../db/repos/room.js';
|
|
4
|
+
import { type StudioTask } from '../db/repos/tasks.js';
|
|
5
|
+
import type { activityStateSchema } from '../shared/browser.js';
|
|
6
|
+
export type ActivityState = z.infer<typeof activityStateSchema>;
|
|
7
|
+
/** Highest first: when an agent has several tasks, the first state in this list wins. */
|
|
8
|
+
export declare const ACTIVITY_PRECEDENCE: readonly ActivityState[];
|
|
9
|
+
export interface ActivityCandidate {
|
|
10
|
+
state: ActivityState;
|
|
11
|
+
label: string;
|
|
12
|
+
source: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
/** The last status probe of an agent: running and reachable, not, or Magnus could not be asked. Absent until probed. */
|
|
15
|
+
export type ProbeResult = 'available' | 'offline' | 'unreachable';
|
|
16
|
+
export interface ActivityInput {
|
|
17
|
+
probe: ProbeResult | undefined;
|
|
18
|
+
/** Every task of this binding in the project, oldest first. */
|
|
19
|
+
tasks: readonly StudioTask[];
|
|
20
|
+
/** The first unfinished task ahead of a queued task in its ordering group. */
|
|
21
|
+
blockerOf: (task: StudioTask) => StudioTask | undefined;
|
|
22
|
+
/** Active tasks started by a hand-off from one of this binding's tasks. */
|
|
23
|
+
handedOff: readonly StudioTask[];
|
|
24
|
+
message: (id: string) => Pick<Message, 'body_md' | 'links'> | undefined;
|
|
25
|
+
roleName: (task: StudioTask) => string;
|
|
26
|
+
/** The newest revision this binding delivered in the project. */
|
|
27
|
+
delivered: (DeliverableRevision & {
|
|
28
|
+
deliverable_name: string;
|
|
29
|
+
}) | undefined;
|
|
30
|
+
now: Date;
|
|
31
|
+
deliveringMs: number;
|
|
32
|
+
}
|
|
33
|
+
export declare const excerpt: (text: string, max?: number) => string;
|
|
34
|
+
/** Every state the records show for one agent binding in one project. */
|
|
35
|
+
export declare function activityCandidates(input: ActivityInput): ActivityCandidate[];
|
|
36
|
+
/** The candidate with the highest precedence; among equals, the oldest task's. */
|
|
37
|
+
export declare function pickActivity(candidates: readonly ActivityCandidate[]): ActivityCandidate;
|
|
38
|
+
export declare function deriveActivity(input: ActivityInput): ActivityCandidate;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { TERMINAL_TASK_STATES } from '../db/repos/tasks.js';
|
|
2
|
+
/** Highest first: when an agent has several tasks, the first state in this list wins. */
|
|
3
|
+
export const ACTIVITY_PRECEDENCE = [
|
|
4
|
+
'offline', 'paused', 'blocked', 'asking', 'reviewing', 'working', 'queued', 'waiting', 'delivering', 'available', 'unknown',
|
|
5
|
+
];
|
|
6
|
+
export const excerpt = (text, max = 80) => { const line = text.replace(/\s+/g, ' ').trim(); return line.length > max ? `${line.slice(0, max - 1)}…` : line; };
|
|
7
|
+
const outcomeOf = (task) => (task.outcome && typeof task.outcome === 'object' ? task.outcome : {});
|
|
8
|
+
/** The clarification a task posted that nobody has answered yet. */
|
|
9
|
+
function openQuestion(task, input) {
|
|
10
|
+
const id = outcomeOf(task).clarification_message_id;
|
|
11
|
+
const message = typeof id === 'string' ? input.message(id) : undefined;
|
|
12
|
+
if (!message || message.links.answer_message_id)
|
|
13
|
+
return undefined;
|
|
14
|
+
return message.body_md;
|
|
15
|
+
}
|
|
16
|
+
/** Why the binding's newest task needs a person: it failed, Ardent lost track of it, or the agent reported itself blocked. */
|
|
17
|
+
function blockedBy(task, input) {
|
|
18
|
+
if (!TERMINAL_TASK_STATES.includes(task.state))
|
|
19
|
+
return undefined;
|
|
20
|
+
const outcome = outcomeOf(task);
|
|
21
|
+
const error = typeof outcome.error === 'string' && outcome.error ? outcome.error : undefined;
|
|
22
|
+
if (task.state === 'failed')
|
|
23
|
+
return error ?? `${task.title} failed`;
|
|
24
|
+
if (task.state === 'unknown')
|
|
25
|
+
return error ?? `Ardent does not know how ${task.title} ended`;
|
|
26
|
+
const result = outcome.team_result;
|
|
27
|
+
if (result?.outcome === 'blocked') {
|
|
28
|
+
const reply = typeof outcome.reply_message_id === 'string' ? input.message(outcome.reply_message_id)?.body_md : undefined;
|
|
29
|
+
return reply?.trim() ? reply : task.title;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
function taskCandidate(task, input) {
|
|
34
|
+
const source = { task_id: task.id, kind: 'task' };
|
|
35
|
+
switch (task.state) {
|
|
36
|
+
case 'pausing': return { state: 'paused', label: `Pause requested: ${task.title}`, source };
|
|
37
|
+
case 'paused': return { state: 'paused', label: `Paused: ${task.title}`, source };
|
|
38
|
+
case 'input_required': return { state: 'asking', label: `Asking: ${excerpt(openQuestion(task, input) ?? task.title)}`, source: { ...source, kind: 'clarification' } };
|
|
39
|
+
case 'running':
|
|
40
|
+
case 'queued': {
|
|
41
|
+
const question = openQuestion(task, input);
|
|
42
|
+
if (question)
|
|
43
|
+
return { state: 'asking', label: `Asking: ${excerpt(question)}`, source: { ...source, kind: 'clarification' } };
|
|
44
|
+
const accepted = Boolean(task.ardent.task_id);
|
|
45
|
+
if (task.mode === 'review' && (task.state === 'running' || task.progress.model_started))
|
|
46
|
+
return { state: 'reviewing', label: `Reviewing: ${task.title}`, source };
|
|
47
|
+
// Working only once the journal shows the model started or a tool was tried.
|
|
48
|
+
if (accepted && task.progress.model_started) {
|
|
49
|
+
return { state: 'working', label: `Working: ${task.title}${task.progress.tool ? ` · ${task.progress.tool}` : ''}`, source: { ...source, kind: 'journal' } };
|
|
50
|
+
}
|
|
51
|
+
if (task.state === 'running')
|
|
52
|
+
return { state: 'queued', label: `Starting: ${task.title}`, source };
|
|
53
|
+
const blocker = accepted ? undefined : input.blockerOf(task);
|
|
54
|
+
if (blocker)
|
|
55
|
+
return { state: 'waiting', label: `Waiting for ${input.roleName(blocker)}`, source: { ...source, kind: 'group', waiting_for: blocker.id } };
|
|
56
|
+
return { state: 'queued', label: `Queued: ${task.title}`, source };
|
|
57
|
+
}
|
|
58
|
+
default: return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Every state the records show for one agent binding in one project. */
|
|
62
|
+
export function activityCandidates(input) {
|
|
63
|
+
const candidates = [];
|
|
64
|
+
if (input.probe === 'offline')
|
|
65
|
+
candidates.push({ state: 'offline', label: 'Offline', source: { kind: 'presence' } });
|
|
66
|
+
if (input.probe === 'unreachable')
|
|
67
|
+
candidates.push({ state: 'offline', label: 'Offline: Magnus is unreachable', source: { kind: 'presence' } });
|
|
68
|
+
for (const task of input.tasks) {
|
|
69
|
+
const candidate = taskCandidate(task, input);
|
|
70
|
+
if (candidate)
|
|
71
|
+
candidates.push(candidate);
|
|
72
|
+
}
|
|
73
|
+
const newest = input.tasks.at(-1);
|
|
74
|
+
const reason = newest ? blockedBy(newest, input) : undefined;
|
|
75
|
+
if (newest && reason)
|
|
76
|
+
candidates.push({ state: 'blocked', label: `Blocked: ${excerpt(reason, 120)}`, source: { task_id: newest.id, kind: 'outcome' } });
|
|
77
|
+
for (const task of input.handedOff) {
|
|
78
|
+
candidates.push({ state: 'waiting', label: `Waiting for ${input.roleName(task)}`, source: { task_id: task.id, kind: 'handoff', from_task: task.source.from_task } });
|
|
79
|
+
}
|
|
80
|
+
const delivered = input.delivered;
|
|
81
|
+
if (delivered && input.now.getTime() - Date.parse(delivered.received_at) < input.deliveringMs) {
|
|
82
|
+
candidates.push({ state: 'delivering', label: `Delivered ${delivered.deliverable_name} r${delivered.revision}`,
|
|
83
|
+
source: { kind: 'artifact_registered', revision_id: delivered.id, task_id: delivered.source_task_id } });
|
|
84
|
+
}
|
|
85
|
+
if (input.probe === 'available')
|
|
86
|
+
candidates.push({ state: 'available', label: 'Available', source: { kind: 'presence' } });
|
|
87
|
+
candidates.push({ state: 'unknown', label: 'Status not checked yet', source: { kind: 'presence' } });
|
|
88
|
+
return candidates;
|
|
89
|
+
}
|
|
90
|
+
/** The candidate with the highest precedence; among equals, the oldest task's. */
|
|
91
|
+
export function pickActivity(candidates) {
|
|
92
|
+
let best;
|
|
93
|
+
for (const candidate of candidates) {
|
|
94
|
+
if (!best || ACTIVITY_PRECEDENCE.indexOf(candidate.state) < ACTIVITY_PRECEDENCE.indexOf(best.state))
|
|
95
|
+
best = candidate;
|
|
96
|
+
}
|
|
97
|
+
return best;
|
|
98
|
+
}
|
|
99
|
+
export function deriveActivity(input) {
|
|
100
|
+
return pickActivity(activityCandidates(input));
|
|
101
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Conversation } from '../db/repos/room.js';
|
|
2
|
+
import type { TeamRole } from './prompting.js';
|
|
3
|
+
export type Recipient = {
|
|
4
|
+
kind: 'team';
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'coordinator';
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'roles';
|
|
9
|
+
roles: string[];
|
|
10
|
+
};
|
|
11
|
+
/** Role keys @mentioned in a message, in order of first mention. Unknown keys are ignored. */
|
|
12
|
+
export declare function mentionedRoles(body: string, roles: readonly Pick<TeamRole, 'key'>[]): string[];
|
|
13
|
+
export type Addressing = {
|
|
14
|
+
roles: TeamRole[];
|
|
15
|
+
} | {
|
|
16
|
+
error: string;
|
|
17
|
+
details: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
export declare function addressees(input: {
|
|
20
|
+
conversation: Pick<Conversation, 'kind' | 'role_key'>;
|
|
21
|
+
to: Recipient;
|
|
22
|
+
body: string;
|
|
23
|
+
roles: readonly TeamRole[];
|
|
24
|
+
}): Addressing;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const MENTION = /(^|[\s(,;])@([a-z][a-z0-9_-]{0,47})(?![a-z0-9_-])/g;
|
|
2
|
+
/** Role keys @mentioned in a message, in order of first mention. Unknown keys are ignored. */
|
|
3
|
+
export function mentionedRoles(body, roles) {
|
|
4
|
+
const known = new Set(roles.map(role => role.key));
|
|
5
|
+
const found = [];
|
|
6
|
+
for (const match of body.matchAll(MENTION)) {
|
|
7
|
+
const key = match[2];
|
|
8
|
+
if (known.has(key) && !found.includes(key))
|
|
9
|
+
found.push(key);
|
|
10
|
+
}
|
|
11
|
+
return found;
|
|
12
|
+
}
|
|
13
|
+
export function addressees(input) {
|
|
14
|
+
const { conversation, roles } = input;
|
|
15
|
+
if (conversation.kind === 'discussion')
|
|
16
|
+
return { roles: [] };
|
|
17
|
+
let keys;
|
|
18
|
+
if (conversation.kind === 'direct') {
|
|
19
|
+
keys = conversation.role_key ? [conversation.role_key] : [];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
const mentions = mentionedRoles(input.body, roles);
|
|
23
|
+
if (input.to.kind === 'roles')
|
|
24
|
+
keys = [...new Set([...input.to.roles, ...mentions])];
|
|
25
|
+
else if (mentions.length)
|
|
26
|
+
keys = mentions;
|
|
27
|
+
else {
|
|
28
|
+
const coordinator = roles.find(role => role.coordinator);
|
|
29
|
+
if (!coordinator)
|
|
30
|
+
return { error: 'The team has no coordinator', details: {} };
|
|
31
|
+
keys = [coordinator.key];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const unknown = keys.filter(key => !roles.some(role => role.key === key));
|
|
35
|
+
if (unknown.length)
|
|
36
|
+
return { error: 'The message names roles that are not in the published team', details: { roles: unknown } };
|
|
37
|
+
const chosen = keys.map(key => roles.find(role => role.key === key));
|
|
38
|
+
const unbound = chosen.filter(role => !role.agent_ref).map(role => role.key);
|
|
39
|
+
if (unbound.length)
|
|
40
|
+
return { error: 'These roles have no bound agent', details: { roles: unbound } };
|
|
41
|
+
return { roles: chosen };
|
|
42
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { type AgentDirectory } from '../agents/directory.js';
|
|
2
|
+
import { type ArdentContextItem, type ArdentTaskEvent, type ArdentTeamMission, type MagnusClient } from '../clients/magnus.js';
|
|
3
|
+
import type { BlobStore } from '../blobs/store.js';
|
|
4
|
+
import { type Project } from '../db/repos/projects.js';
|
|
5
|
+
import type { Conversation, Message } from '../db/repos/room.js';
|
|
6
|
+
import { type StudioTask, type TaskMode, type TaskState } from '../db/repos/tasks.js';
|
|
7
|
+
import type { TeamRevision } from '../db/repos/team.js';
|
|
8
|
+
import type { Store } from '../db/store.js';
|
|
9
|
+
import type { Logger } from '../logger.js';
|
|
10
|
+
import { TaskFollower, type DoneRecord, type FollowerOptions } from './follower.js';
|
|
11
|
+
import type { LiveText } from './live.js';
|
|
12
|
+
import { type TeamRole } from './prompting.js';
|
|
13
|
+
export type ConductorMagnus = Pick<MagnusClient, 'getInstallation' | 'openSession' | 'submitTask' | 'submitMission' | 'streamTask' | 'taskEvents' | 'task' | 'cancelTask' | 'pauseTask' | 'resumeTask'>;
|
|
14
|
+
export interface ConductorOptions {
|
|
15
|
+
store: Store;
|
|
16
|
+
blobs: BlobStore;
|
|
17
|
+
magnus: ConductorMagnus;
|
|
18
|
+
agents: Pick<AgentDirectory, 'profiles'>;
|
|
19
|
+
live: LiveText;
|
|
20
|
+
logger: Logger;
|
|
21
|
+
/** Presence probe interval. Default 30 s. */
|
|
22
|
+
presenceMs?: number;
|
|
23
|
+
/** How long an agent shows Delivering after a verified deliverable revision. Default 20 s. */
|
|
24
|
+
deliveringMs?: number;
|
|
25
|
+
/** First retry delay for a submission Magnus could not take. Doubles per attempt. Default 1 s. */
|
|
26
|
+
submitRetryMs?: number;
|
|
27
|
+
maxSubmitAttempts?: number;
|
|
28
|
+
capabilityCacheMs?: number;
|
|
29
|
+
follower?: FollowerOptions;
|
|
30
|
+
}
|
|
31
|
+
/** Hooks later features attach to journal events (the artifact importer). Called inside the event's transaction. */
|
|
32
|
+
export type JournalHook = (task: StudioTask, event: ArdentTaskEvent) => void;
|
|
33
|
+
/** Extra context for a task's turn (review comments for a revision task). */
|
|
34
|
+
export type ContextProvider = (task: StudioTask) => Array<{
|
|
35
|
+
title: string;
|
|
36
|
+
text: string;
|
|
37
|
+
mime?: string;
|
|
38
|
+
}>;
|
|
39
|
+
/** What a mode handler sends: a mission objective and context items, or one prompt for task-submit. */
|
|
40
|
+
export interface ModeRequest {
|
|
41
|
+
objective: string;
|
|
42
|
+
contextItems: ArdentContextItem[];
|
|
43
|
+
prompt: string;
|
|
44
|
+
mission: ArdentTeamMission;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A task mode that does not speak in the room (a team proposal). The handler names the agent and
|
|
48
|
+
* composes the request; the conductor submits and follows it like any task, then hands the final
|
|
49
|
+
* reply to the handler instead of writing a room message.
|
|
50
|
+
*/
|
|
51
|
+
export interface TaskModeHandler {
|
|
52
|
+
/** The agent to ask, or why the task cannot start. */
|
|
53
|
+
agent(task: StudioTask): {
|
|
54
|
+
agentRef: string;
|
|
55
|
+
} | {
|
|
56
|
+
error: string;
|
|
57
|
+
};
|
|
58
|
+
compose(task: StudioTask, input: {
|
|
59
|
+
project: Project;
|
|
60
|
+
dispatch: 'mission' | 'turn';
|
|
61
|
+
now: Date;
|
|
62
|
+
}): ModeRequest;
|
|
63
|
+
/** The task's final state and reply. Called inside the transaction that records it. */
|
|
64
|
+
finished(task: StudioTask, outcome: {
|
|
65
|
+
state: TaskState;
|
|
66
|
+
reply: string | null;
|
|
67
|
+
error: string | null;
|
|
68
|
+
}): void;
|
|
69
|
+
}
|
|
70
|
+
export declare class ConductorError extends Error {
|
|
71
|
+
readonly status: 404 | 409 | 502 | 503;
|
|
72
|
+
readonly code: string;
|
|
73
|
+
name: string;
|
|
74
|
+
constructor(message: string, status: 404 | 409 | 502 | 503, code: string);
|
|
75
|
+
}
|
|
76
|
+
/** A stable UUID for a Studio task, so a retried submission is the same Ardent task. */
|
|
77
|
+
export declare function ardentIdempotencyKey(taskId: string): string;
|
|
78
|
+
export declare class Conductor {
|
|
79
|
+
private readonly options;
|
|
80
|
+
private readonly followers;
|
|
81
|
+
private readonly submitting;
|
|
82
|
+
private readonly retryAt;
|
|
83
|
+
private readonly capabilities;
|
|
84
|
+
/** Last presence probe per agent. Absent until probed. */
|
|
85
|
+
private readonly presence;
|
|
86
|
+
/** Re-derive an agent's activity when its Delivering window ends, per project and binding. */
|
|
87
|
+
private readonly deliveringTimers;
|
|
88
|
+
private readonly hooks;
|
|
89
|
+
private readonly contextProviders;
|
|
90
|
+
private readonly modes;
|
|
91
|
+
private pumping;
|
|
92
|
+
private pumpAgain;
|
|
93
|
+
private retryTimer;
|
|
94
|
+
private presenceTimer;
|
|
95
|
+
private stopped;
|
|
96
|
+
private readonly store;
|
|
97
|
+
private readonly magnus;
|
|
98
|
+
constructor(options: ConductorOptions);
|
|
99
|
+
/** Register a journal hook (for example the artifact importer). */
|
|
100
|
+
onJournalEvent(hook: JournalHook): void;
|
|
101
|
+
/** Register extra turn context (for example the review comments of a revision task). */
|
|
102
|
+
addContextProvider(provider: ContextProvider): void;
|
|
103
|
+
/** Handle tasks of this mode with the handler instead of the room (for example team proposals). */
|
|
104
|
+
registerMode(mode: TaskMode, handler: TaskModeHandler): void;
|
|
105
|
+
/** Resume following every accepted task from its stored cursor, finish discussions a restart interrupted, and start dispatching. */
|
|
106
|
+
start(): void;
|
|
107
|
+
stop(): Promise<void>;
|
|
108
|
+
/** Tasks the conductor currently follows (for tests and diagnostics). */
|
|
109
|
+
followerOf(taskId: string): TaskFollower | undefined;
|
|
110
|
+
/** Submit every task that is ready. Safe to call often: runs are serialized. */
|
|
111
|
+
kick(): void;
|
|
112
|
+
/** Resolves when no dispatch run is in progress. */
|
|
113
|
+
settled(): Promise<void>;
|
|
114
|
+
private pump;
|
|
115
|
+
private resolve;
|
|
116
|
+
/**
|
|
117
|
+
* Studio level and whether the agent runs an Ardent with missions, both from the node's one
|
|
118
|
+
* installation record (every profile on a node runs its one Ardent package). Cached briefly.
|
|
119
|
+
* A failed or stale agent list never decides this, so a mission is not silently sent as a
|
|
120
|
+
* plain task without its tools.
|
|
121
|
+
*/
|
|
122
|
+
private capability;
|
|
123
|
+
/** A room turn: the role's agent under the task's team revision, with an Ardent session when the node supports one. */
|
|
124
|
+
private planTurn;
|
|
125
|
+
/** A handled mode: one request without a session, composed by its handler. */
|
|
126
|
+
private planHandled;
|
|
127
|
+
private submit;
|
|
128
|
+
private scheduleRetry;
|
|
129
|
+
private compose;
|
|
130
|
+
/** Text attachments are read synchronously only when small; others are named, not inlined. */
|
|
131
|
+
private attachmentText;
|
|
132
|
+
/** A task that could not start: record why, in the task and in the room. */
|
|
133
|
+
private failTask;
|
|
134
|
+
private cancelLocally;
|
|
135
|
+
private follow;
|
|
136
|
+
/** Live reply text: create the streaming message on the first visible text, then relay it. */
|
|
137
|
+
private relay;
|
|
138
|
+
/** Handle one journal event and advance the cursor in the same transaction. True when it ends the turn. */
|
|
139
|
+
private journal;
|
|
140
|
+
/** An agent asked for a hand-off. Studio starts it only along a delegates_to or hands_off_to edge of the task's team revision. */
|
|
141
|
+
private handoff;
|
|
142
|
+
private clarification;
|
|
143
|
+
private proposal;
|
|
144
|
+
/** The task finished in Ardent: write the final message and the task state. Idempotent on the Ardent task id. */
|
|
145
|
+
private finish;
|
|
146
|
+
/** Exposed for routes that receive a final task record directly (cancel). */
|
|
147
|
+
complete(taskId: string, record: DoneRecord): void;
|
|
148
|
+
/** A handled mode has no clarifications: Ardent input_required ends it as partial, and the handler reads the reply. */
|
|
149
|
+
private completeHandled;
|
|
150
|
+
private afterFinish;
|
|
151
|
+
/** The project's team conversation, created on first use. Call inside a transaction. */
|
|
152
|
+
ensureTeamConversation(projectId: string, createdBy: string): Conversation;
|
|
153
|
+
/**
|
|
154
|
+
* Queue one turn per addressee in an ordering group, so each addressee sees the replies before
|
|
155
|
+
* theirs. Call inside the transaction that stored the message.
|
|
156
|
+
*/
|
|
157
|
+
queueTurns(input: {
|
|
158
|
+
project: Project;
|
|
159
|
+
revision: TeamRevision;
|
|
160
|
+
conversation: Conversation;
|
|
161
|
+
message: Message;
|
|
162
|
+
roles: readonly TeamRole[];
|
|
163
|
+
createdBy: string;
|
|
164
|
+
mode?: 'turn';
|
|
165
|
+
source?: Record<string, unknown>;
|
|
166
|
+
}): StudioTask[];
|
|
167
|
+
/** Queue one task (an assignment, follow-up or answer). Call inside a transaction. */
|
|
168
|
+
queueTask(input: Parameters<Store['tasks']['create']>[0]): StudioTask;
|
|
169
|
+
/** Queue round `round` of a discussion: one turn per participant, in order. Call inside a transaction. */
|
|
170
|
+
queueDiscussionRound(conversation: Conversation, revision: TeamRevision, round: number, createdBy: string): StudioTask[];
|
|
171
|
+
/** Move a discussion on once the current round (or its summary) has finished. */
|
|
172
|
+
advanceDiscussion(conversationId: string): void;
|
|
173
|
+
private runningDiscussions;
|
|
174
|
+
/** Stop a conversation: unstarted turns are cancelled here, started ones are cancelled through Magnus. */
|
|
175
|
+
stopConversation(conversationId: string): Promise<void>;
|
|
176
|
+
/** Cancel a task. An unstarted one is cancelled here; a started one through Magnus, and its final record follows. */
|
|
177
|
+
cancel(task: StudioTask): Promise<StudioTask>;
|
|
178
|
+
/** Pause a task at its next step boundary (studio >= 2), or hold an unstarted one. */
|
|
179
|
+
pause(task: StudioTask): Promise<StudioTask>;
|
|
180
|
+
resume(task: StudioTask): Promise<StudioTask>;
|
|
181
|
+
private magnusCall;
|
|
182
|
+
/** Probe agent status every 30 s. The probe only records reachability; activity is then derived as for any change. */
|
|
183
|
+
probe(): Promise<void>;
|
|
184
|
+
/**
|
|
185
|
+
* Derive a binding's activity from the last probe and its real records (docs/CONDUCTOR.md#activity),
|
|
186
|
+
* and announce it when it changed. Call inside a transaction, after the records changed.
|
|
187
|
+
*/
|
|
188
|
+
refreshActivity(projectId: string, bindingId: string | null): void;
|
|
189
|
+
private writeActivity;
|
|
190
|
+
private agentAuthor;
|
|
191
|
+
private roleName;
|
|
192
|
+
/** A Studio note in the room. Call inside a transaction. */
|
|
193
|
+
systemMessage(conversationId: string, text: string, links: Record<string, unknown>, replyTo?: string | null): Message;
|
|
194
|
+
messageEvent(message: Message): Record<string, unknown>;
|
|
195
|
+
taskEvent(task: StudioTask): Record<string, unknown>;
|
|
196
|
+
}
|
|
197
|
+
/** A task as the browser sees it (no mission JSON). */
|
|
198
|
+
export declare function publicTask(task: StudioTask): {
|
|
199
|
+
id: string;
|
|
200
|
+
project_id: string;
|
|
201
|
+
title: string;
|
|
202
|
+
mode: "review" | "revision" | "turn" | "assignment" | "discussion_turn" | "team_proposal";
|
|
203
|
+
role_key: string | null;
|
|
204
|
+
binding_id: string | null;
|
|
205
|
+
assignment_revision: number | null;
|
|
206
|
+
conversation_id: string | null;
|
|
207
|
+
trigger_message_id: string | null;
|
|
208
|
+
instructions_md: string;
|
|
209
|
+
source: Record<string, unknown>;
|
|
210
|
+
state: "unknown" | "failed" | "partial" | "queued" | "running" | "cancelled" | "input_required" | "pausing" | "paused" | "completed";
|
|
211
|
+
dispatch: import("../db/repos/tasks.js").TaskDispatch | null;
|
|
212
|
+
ardent: {
|
|
213
|
+
agent_ref: string | null;
|
|
214
|
+
task_id: string | null;
|
|
215
|
+
session_id: string | null;
|
|
216
|
+
};
|
|
217
|
+
outcome: {} | null;
|
|
218
|
+
reassigned_from: string | null;
|
|
219
|
+
group_id: string | null;
|
|
220
|
+
group_position: number;
|
|
221
|
+
created_by: string;
|
|
222
|
+
created_at: string;
|
|
223
|
+
updated_at: string;
|
|
224
|
+
finished_at: string | null;
|
|
225
|
+
};
|