@milabench/bujang 0.1.2 → 0.1.4
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/README.md +3 -2
- package/dist/api.d.ts +65 -1
- package/dist/context.d.ts +5 -10
- package/dist/index.d.ts +8 -3
- package/dist/index.js +4750 -2910
- package/dist/index.js.map +1 -1
- package/dist/paths.d.ts +8 -1
- package/dist/routes.d.ts +1 -1
- package/dist/types.d.ts +332 -0
- package/dist/views/AgentActivityView.d.ts +2 -0
- package/dist/views/JobArtifacts.d.ts +9 -0
- package/dist/views/JobDataOverview.d.ts +9 -0
- package/dist/views/JobMarks.d.ts +17 -0
- package/dist/views/PipelineEditor.d.ts +24 -0
- package/dist/views/PipelineGraph.d.ts +49 -0
- package/dist/views/PipelinesView.d.ts +3 -0
- package/dist/views/SettingsView.d.ts +3 -0
- package/package.json +7 -1
- package/src/api.ts +206 -0
- package/src/context.tsx +7 -13
- package/src/index.ts +8 -3
- package/src/paths.ts +22 -6
- package/src/routes.tsx +5 -2
- package/src/types.ts +307 -0
- package/src/views/AgentActivityView.tsx +115 -5
- package/src/views/JobArtifacts.tsx +89 -0
- package/src/views/JobDataOverview.tsx +594 -0
- package/src/views/JobDetails.tsx +3 -0
- package/src/views/JobLogs.tsx +47 -162
- package/src/views/JobMarks.tsx +233 -0
- package/src/views/JobSubmit.tsx +4 -4
- package/src/views/JobsView.tsx +245 -54
- package/src/views/PipelineEditor.tsx +779 -0
- package/src/views/PipelineGraph.tsx +197 -0
- package/src/views/PipelinesView.tsx +363 -1087
- package/src/views/SettingsView.tsx +910 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import React, { useEffect, useId, useMemo, useState } from 'react';
|
|
2
|
+
import { Box, Text } from '@chakra-ui/react';
|
|
3
|
+
import type { PipelineNode } from '../types';
|
|
4
|
+
import { useColorMode } from '../hooks/useColorMode';
|
|
5
|
+
|
|
6
|
+
/** A job of the graph: its step label (the same as the server's, `0.prepare`, `1.runs/0.a`) and what it waits on */
|
|
7
|
+
export interface GraphStep {
|
|
8
|
+
id: string;
|
|
9
|
+
step: string;
|
|
10
|
+
script: string;
|
|
11
|
+
profile: string;
|
|
12
|
+
/** what the step sets (its "env"): tells apart the steps of one script */
|
|
13
|
+
env?: Record<string, string>;
|
|
14
|
+
after: string[];
|
|
15
|
+
/** the sweep variable of its parallel group (nearest one) and its value: what tells it apart from its siblings */
|
|
16
|
+
sweepKey?: string;
|
|
17
|
+
sweep?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The sweep variable of a parallel group: its `sweep`, else the env variable every job of the group sets,
|
|
22
|
+
* each to another value (the first one in the order of the first job; the server guesses the same way)
|
|
23
|
+
*/
|
|
24
|
+
export const groupSweepKey = (group: PipelineNode): string | undefined => {
|
|
25
|
+
if (group.type !== 'parallel') return undefined;
|
|
26
|
+
if (group.sweep) return group.sweep;
|
|
27
|
+
const jobs = group.jobs.filter((child) => child.type === 'job');
|
|
28
|
+
if (jobs.length < 2 || jobs.length !== group.jobs.length) return undefined;
|
|
29
|
+
const envs = jobs.map((job) => (job.type === 'job' && job.env) || {});
|
|
30
|
+
return Object.keys(envs[0]).find((key) => {
|
|
31
|
+
const values = envs.map((env) => env[key]);
|
|
32
|
+
return values.every(Boolean) && new Set(values).size === values.length;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** The value of the sweep variable `key` for a job, undefined when the job does not set it */
|
|
37
|
+
export const sweepValue = (env: Record<string, string> | undefined, key: string | undefined): string | undefined =>
|
|
38
|
+
key && env && env[key] !== undefined && env[key] !== '' ? env[key] : undefined;
|
|
39
|
+
|
|
40
|
+
// Pipeline.step_links on the server: "<index>.<script stem or group name>", unsafe characters as _
|
|
41
|
+
const label = (node: PipelineNode, index: number): string => {
|
|
42
|
+
const name = node.type === 'job'
|
|
43
|
+
? (node.script.split('/').pop() || node.script).replace(/\.[^.]*$/, '')
|
|
44
|
+
: node.name;
|
|
45
|
+
return `${index}.${(name || 'step').replace(/[^A-Za-z0-9._-]/g, '_')}`;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
interface Group {
|
|
49
|
+
id: string;
|
|
50
|
+
kind: 'sequential' | 'parallel';
|
|
51
|
+
name: string;
|
|
52
|
+
steps: string[];
|
|
53
|
+
groups: Group[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The steps of a definition with their dependencies (sequential: each child waits on the one before;
|
|
58
|
+
* parallel: the children share the dependencies, the next node waits on all of them).
|
|
59
|
+
*/
|
|
60
|
+
export function pipelineSteps(root: PipelineNode): { steps: GraphStep[]; root: Group | null } {
|
|
61
|
+
const steps: GraphStep[] = [];
|
|
62
|
+
let counter = 0;
|
|
63
|
+
|
|
64
|
+
const walk = (node: PipelineNode, path: string[], deps: string[], group: Group | null, sweepKey?: string): string[] => {
|
|
65
|
+
if (node.type === 'job') {
|
|
66
|
+
const id = `s${counter++}`;
|
|
67
|
+
const sweep = sweepValue(node.env, sweepKey);
|
|
68
|
+
steps.push({
|
|
69
|
+
id, step: path.join('/'), script: node.script, profile: node.profile, env: node.env, after: deps,
|
|
70
|
+
...(sweep ? { sweepKey, sweep } : {}),
|
|
71
|
+
});
|
|
72
|
+
group?.steps.push(id);
|
|
73
|
+
return [id];
|
|
74
|
+
}
|
|
75
|
+
const sub: Group = { id: `g${counter++}`, kind: node.type, name: node.name, steps: [], groups: [] };
|
|
76
|
+
group?.groups.push(sub);
|
|
77
|
+
const children = node.jobs || [];
|
|
78
|
+
if (node.type === 'sequential') {
|
|
79
|
+
let current = deps;
|
|
80
|
+
children.forEach((child, i) => { current = walk(child, [...path, label(child, i)], current, sub, sweepKey); });
|
|
81
|
+
return current;
|
|
82
|
+
}
|
|
83
|
+
const key = groupSweepKey(node) || sweepKey;
|
|
84
|
+
if (children.length === 0) return deps;
|
|
85
|
+
return children.flatMap((child, i) => walk(child, [...path, label(child, i)], deps, sub, key));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
if (root.type === 'job') {
|
|
89
|
+
walk(root, [label(root, 0)], [], null);
|
|
90
|
+
return { steps, root: null };
|
|
91
|
+
}
|
|
92
|
+
const top: Group = { id: 'root', kind: root.type, name: root.name, steps: [], groups: [] };
|
|
93
|
+
// the root group is the pipeline itself: no box around it
|
|
94
|
+
const children = root.jobs || [];
|
|
95
|
+
if (root.type === 'sequential') {
|
|
96
|
+
let current: string[] = [];
|
|
97
|
+
children.forEach((child, i) => { current = walk(child, [label(child, i)], current, top); });
|
|
98
|
+
} else {
|
|
99
|
+
children.forEach((child, i) => walk(child, [label(child, i)], [], top, groupSweepKey(root)));
|
|
100
|
+
}
|
|
101
|
+
return { steps, root: top };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Above this many steps the mermaid graph is unreadable: it is not drawn */
|
|
105
|
+
export const MAX_GRAPH_STEPS = 10;
|
|
106
|
+
|
|
107
|
+
/** Whether the graph of `root` is small enough to draw (at most MAX_GRAPH_STEPS steps) */
|
|
108
|
+
export const graphIsDrawable = (root: PipelineNode): boolean => pipelineSteps(root).steps.length <= MAX_GRAPH_STEPS;
|
|
109
|
+
|
|
110
|
+
const STATE_CLASS:Record<string, string> = {
|
|
111
|
+
COMPLETED: 'done',
|
|
112
|
+
RUNNING: 'running', COMPLETING: 'running', CONFIGURING: 'running',
|
|
113
|
+
PENDING: 'waiting', HELD: 'waiting', STARTING: 'waiting', SUBMITTED: 'waiting', REQUEUED: 'waiting',
|
|
114
|
+
FAILED: 'failed', DEPENDENCY_FAILED: 'failed', TIMEOUT: 'failed', OUT_OF_MEMORY: 'failed',
|
|
115
|
+
NODE_FAIL: 'failed', BOOT_FAIL: 'failed', DEADLINE: 'failed', PREEMPTED: 'failed',
|
|
116
|
+
CANCELLED: 'cancelled',
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const escape = (text: string) => text.replace(/["<>]/g, (c) => ({ '"': '#quot;', '<': '#lt;', '>': '#gt;' })[c] as string);
|
|
120
|
+
|
|
121
|
+
/** Mermaid flowchart of a pipeline; `states`: step label -> state, colours the steps of a run */
|
|
122
|
+
export function pipelineMermaid(root: PipelineNode, states: Record<string, string> = {}, dark = false): string {
|
|
123
|
+
const { steps, root: top } = pipelineSteps(root);
|
|
124
|
+
const byId = new Map(steps.map((s) => [s.id, s]));
|
|
125
|
+
const lines = ['flowchart LR'];
|
|
126
|
+
|
|
127
|
+
const node = (id: string, indent: string) => {
|
|
128
|
+
const s = byId.get(id)!;
|
|
129
|
+
const state = states[s.step];
|
|
130
|
+
// under a sweep: the swept value names the step, the rest of its env stays below
|
|
131
|
+
const env = Object.entries(s.env ?? {}).filter(([k]) => k !== s.sweepKey).map(([k, v]) => `${k}=${v}`).join(' ');
|
|
132
|
+
const parts = [s.sweep ?? s.script, s.profile, env.length > 60 ? `${env.slice(0, 57)}...` : env, state].filter(Boolean) as string[];
|
|
133
|
+
lines.push(`${indent}${id}["${parts.map(escape).join('<br/>')}"]`);
|
|
134
|
+
};
|
|
135
|
+
const group = (g: Group, indent: string, boxed: boolean) => {
|
|
136
|
+
if (boxed) lines.push(`${indent}subgraph ${g.id}["${escape(g.name)} · ${g.kind}"]`);
|
|
137
|
+
const inner = boxed ? `${indent} ` : indent;
|
|
138
|
+
g.steps.forEach((id) => node(id, inner));
|
|
139
|
+
g.groups.forEach((sub) => group(sub, inner, true));
|
|
140
|
+
if (boxed) lines.push(`${indent}end`);
|
|
141
|
+
};
|
|
142
|
+
if (top) group(top, ' ', false);
|
|
143
|
+
else steps.forEach((s) => node(s.id, ' '));
|
|
144
|
+
|
|
145
|
+
for (const s of steps) for (const dep of s.after) lines.push(` ${dep} --> ${s.id}`);
|
|
146
|
+
|
|
147
|
+
const palette = dark
|
|
148
|
+
? { done: '#22543d,#68d391', running: '#2a4365,#63b3ed', waiting: '#5f370e,#f6ad55', failed: '#63171b,#fc8181', cancelled: '#2d3748,#a0aec0' }
|
|
149
|
+
: { done: '#c6f6d5,#38a169', running: '#bee3f8,#3182ce', waiting: '#feebc8,#dd6b20', failed: '#fed7d7,#e53e3e', cancelled: '#edf2f7,#718096' };
|
|
150
|
+
for (const [name, colors] of Object.entries(palette)) {
|
|
151
|
+
const [fill, stroke] = colors.split(',');
|
|
152
|
+
lines.push(` classDef ${name} fill:${fill},stroke:${stroke},stroke-width:2px`);
|
|
153
|
+
}
|
|
154
|
+
for (const s of steps) {
|
|
155
|
+
const cls = states[s.step] ? STATE_CLASS[states[s.step]] : undefined;
|
|
156
|
+
if (cls) lines.push(` class ${s.id} ${cls}`);
|
|
157
|
+
}
|
|
158
|
+
return lines.join('\n');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let mermaidCounter = 0;
|
|
162
|
+
|
|
163
|
+
/** The pipeline graph, rendered by mermaid (loaded on first use) */
|
|
164
|
+
export const PipelineGraph: React.FC<{ definition: PipelineNode; states?: Record<string, string> }> = ({ definition, states }) => {
|
|
165
|
+
const { colorMode } = useColorMode();
|
|
166
|
+
const dark = colorMode === 'dark';
|
|
167
|
+
const source = useMemo(() => pipelineMermaid(definition, states, dark), [definition, states, dark]);
|
|
168
|
+
const baseId = useId().replace(/[^A-Za-z0-9]/g, '');
|
|
169
|
+
const [svg, setSvg] = useState<string | null>(null);
|
|
170
|
+
const [error, setError] = useState<string | null>(null);
|
|
171
|
+
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
let cancelled = false;
|
|
174
|
+
(async () => {
|
|
175
|
+
try {
|
|
176
|
+
const mermaid = (await import('mermaid')).default;
|
|
177
|
+
mermaid.initialize({ startOnLoad: false, theme: dark ? 'dark' : 'default', securityLevel: 'strict', flowchart: { htmlLabels: true } });
|
|
178
|
+
const { svg: out } = await mermaid.render(`bujang-pipeline-${baseId}-${mermaidCounter++}`, source);
|
|
179
|
+
if (!cancelled) { setSvg(out); setError(null); }
|
|
180
|
+
} catch (e) {
|
|
181
|
+
if (!cancelled) setError((e as Error).message || String(e));
|
|
182
|
+
}
|
|
183
|
+
})();
|
|
184
|
+
return () => { cancelled = true; };
|
|
185
|
+
}, [source, dark, baseId]);
|
|
186
|
+
|
|
187
|
+
if (error) {
|
|
188
|
+
return (
|
|
189
|
+
<Box>
|
|
190
|
+
<Text color="red.500" fontSize="sm">Cannot draw the pipeline: {error}</Text>
|
|
191
|
+
<Box as="pre" fontSize="xs" mt={2} color="var(--color-text-muted)">{source}</Box>
|
|
192
|
+
</Box>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (!svg) return <Text color="var(--color-text-muted)" fontSize="sm">Drawing the pipeline...</Text>;
|
|
196
|
+
return <Box overflowX="auto" css={{ '& svg': { maxWidth: '100%', height: 'auto' } }} dangerouslySetInnerHTML={{ __html: svg }} />;
|
|
197
|
+
};
|