@qvac/harness 1.0.0-rc.21 → 1.0.0-rc.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/README.md +2 -0
  3. package/dist-lib/index.d.ts +4 -1
  4. package/dist-lib/index.js +12 -4
  5. package/dist-lib/lib/capabilities/completion-engine/actors/execution-plan.js +3 -1
  6. package/dist-lib/lib/capabilities/completion-engine/tool-outcome.js +2 -4
  7. package/dist-lib/lib/capabilities/tool-turns/answered.d.ts +15 -0
  8. package/dist-lib/lib/capabilities/tool-turns/answered.js +16 -0
  9. package/dist-lib/lib/capabilities/tool-turns.d.ts +2 -0
  10. package/dist-lib/lib/capabilities/tool-turns.js +1 -0
  11. package/dist-lib/lib/contract/completion.d.ts +2 -1
  12. package/dist-lib/lib/contract/drive.d.ts +24 -0
  13. package/dist-lib/lib/contract/root-search.d.ts +34 -0
  14. package/dist-lib/lib/contract/root-search.js +1 -0
  15. package/dist-lib/lib/contract/run.d.ts +2 -0
  16. package/dist-lib/lib/helpers/root-search.d.ts +3 -0
  17. package/dist-lib/lib/helpers/root-search.js +74 -0
  18. package/dist-lib/lib/machine/completion/actions/tools.js +7 -1
  19. package/dist-lib/lib/machine/completion/metrics.js +3 -1
  20. package/dist-lib/lib/machine/completion/types.d.ts +2 -0
  21. package/dist-lib/lib/models/model-catalog-constants.d.ts +1 -0
  22. package/dist-lib/lib/models/model-catalog-constants.js +5 -1
  23. package/dist-lib/lib/models/model-catalog.d.ts +10 -3
  24. package/dist-lib/lib/models/model-catalog.js +14 -8
  25. package/dist-lib/lib/models/model-delete.js +3 -0
  26. package/dist-lib/lib/models/model-list.d.ts +2 -1
  27. package/dist-lib/lib/models/model-list.js +5 -1
  28. package/dist-lib/lib/models/model-pool.d.ts +5 -0
  29. package/dist-lib/lib/models/model-pool.js +16 -4
  30. package/dist-lib/lib/plugins/registry.d.ts +2 -0
  31. package/dist-lib/lib/residency/index.js +9 -6
  32. package/dist-lib/lib/spawn.d.ts +9 -0
  33. package/dist-lib/lib/tools/files/glob.js +71 -10
  34. package/dist-lib/lib/tools/files/grep.js +65 -22
  35. package/dist-lib/lib/tools/files/read-file.js +120 -76
  36. package/dist-lib/lib/tools/fs/engine.d.ts +9 -0
  37. package/dist-lib/lib/tools/fs/engine.js +28 -0
  38. package/dist-lib/lib/tools/fs/suggest.d.ts +8 -0
  39. package/dist-lib/lib/tools/fs/suggest.js +110 -0
  40. package/dist-lib/lib/tools/fs/tool-path.d.ts +3 -0
  41. package/dist-lib/lib/tools/fs/tool-path.js +13 -0
  42. package/dist-lib/lib/tools/fs/walk.d.ts +3 -0
  43. package/dist-lib/lib/tools/fs/walk.js +20 -20
  44. package/dist-lib/lib/tools/toolsets.d.ts +1 -1
  45. package/dist-lib/lib/tools/toolsets.js +2 -0
  46. package/dist-lib/plugins/charts/chart-tool.d.ts +8 -0
  47. package/dist-lib/plugins/charts/chart-tool.js +175 -0
  48. package/dist-lib/plugins/charts/index.d.ts +16 -0
  49. package/dist-lib/plugins/charts/index.js +21 -0
  50. package/dist-lib/plugins/diagrams/draw.d.ts +10 -0
  51. package/dist-lib/plugins/diagrams/draw.js +90 -0
  52. package/dist-lib/plugins/diagrams/index.d.ts +13 -0
  53. package/dist-lib/plugins/diagrams/index.js +15 -0
  54. package/dist-lib/plugins/diagrams/render.d.ts +16 -0
  55. package/dist-lib/plugins/diagrams/render.js +285 -0
  56. package/dist-lib/plugins/ripgrep/index.d.ts +15 -0
  57. package/dist-lib/plugins/ripgrep/index.js +28 -0
  58. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-arm64/qvac-sandbox.exe +0 -0
  59. package/dist-lib/plugins/sandbox-windows/prebuilds/win32-x64/qvac-sandbox.exe +0 -0
  60. package/package.json +28 -2
  61. package/worker/harness-sidecar.mjs +5 -0
@@ -0,0 +1,175 @@
1
+ import b4a from 'b4a';
2
+ import { isRecord } from '../../lib/guards.js';
3
+ import { retryMemo } from '../../lib/helpers/retry-memo.js';
4
+ const WORDS = ['shape', 'variant', 'highlight', 'scale', 'unit', 'callout'];
5
+ const THEMES = ['dark', 'light'];
6
+ const DATA = {
7
+ type: 'array',
8
+ description: 'One item per label; for several series or moments, one per label and group',
9
+ items: {
10
+ type: 'object',
11
+ properties: {
12
+ label: { type: 'string', description: 'The name: "Rent", "2021", "India"' },
13
+ value: { type: 'number', description: 'Its number, in the scale below' },
14
+ group: {
15
+ type: 'string',
16
+ description: 'The series, the moment or the piece; leave out for one series'
17
+ }
18
+ },
19
+ required: ['label', 'value']
20
+ }
21
+ };
22
+ const ANNOTATIONS = {
23
+ type: 'array',
24
+ description: 'At most two short facts, each about one data label; often none',
25
+ items: {
26
+ type: 'object',
27
+ properties: {
28
+ label: { type: 'string', description: 'The data label it is about' },
29
+ text: { type: 'string', description: 'The fact' }
30
+ },
31
+ required: ['label', 'text']
32
+ }
33
+ };
34
+ const loadMaps = retryMemo(async () => import('qvac-charts/maps'));
35
+ export function createChartTool({ chart, theme, mark }) {
36
+ return {
37
+ name: 'create_chart',
38
+ toolset: 'charts',
39
+ skillGated: true,
40
+ description: 'Draw a chart from numbers as an image the user sees in the chat. Describe the data; ' +
41
+ 'the chart type, the arithmetic and the layout are chosen for you. Use the numbers the ' +
42
+ 'user gave, every one of them; numbers from memory get the source "Approximate, from ' +
43
+ 'model knowledge". Returns the attachment id and a one-line summary of what was drawn.',
44
+ parameters: parameters(chart),
45
+ effect: () => 'read',
46
+ resource: (args) => ({ kind: 'chart', label: String(args.title ?? '').slice(0, 80) }),
47
+ execute: (args, ctx) => draw(args, { ctx, chart, theme, mark })
48
+ };
49
+ }
50
+ async function draw(args, { ctx, chart, theme, mark }) {
51
+ const spec = specFrom(args);
52
+ if (typeof spec === 'string') {
53
+ return { error: spec };
54
+ }
55
+ if (!ctx?.attach) {
56
+ return { error: 'this device cannot save charts' };
57
+ }
58
+ let drawn;
59
+ try {
60
+ const maps = spec.shape === 'places'
61
+ ? await (await loadMaps()).mapsFor(spec.data.map((d) => d.label))
62
+ : undefined;
63
+ drawn = chart.from(spec, { maps });
64
+ }
65
+ catch (err) {
66
+ if (err instanceof RangeError) {
67
+ return { error: err.message };
68
+ }
69
+ throw err;
70
+ }
71
+ const record = await ctx.attach({
72
+ fileName: `chart-${Date.now()}.svg`,
73
+ mimeType: 'image/svg+xml',
74
+ bytes: b4a.from(drawn.toSVG({ theme: THEMES.find((t) => t === args.theme) ?? theme, mark })),
75
+ raw: true
76
+ });
77
+ return {
78
+ attachments: [
79
+ { attachmentId: record.id, fileName: record.fileName, byteLength: record.byteLength }
80
+ ],
81
+ summary: drawn.summary
82
+ };
83
+ }
84
+ function parameters(chart) {
85
+ const { properties, required } = chart.schema;
86
+ return {
87
+ type: 'object',
88
+ properties: {
89
+ shape: {
90
+ type: 'string',
91
+ enum: properties.shape.enum,
92
+ description: 'ranking: items by size. parts: pieces of a whole. over-time: values across periods. ' +
93
+ 'change: the same items at two moments, each item twice. sizes: sizes that differ ' +
94
+ 'hugely. places: a map, one value per country; states or cities are a ranking'
95
+ },
96
+ variant: {
97
+ type: 'string',
98
+ enum: properties.variant.enum,
99
+ description: 'Only when the user names a chart type'
100
+ },
101
+ title: { type: 'string', description: 'Short and declarative: the story, with no number' },
102
+ subtitle: { type: 'string', description: 'What is measured, and the year' },
103
+ data: DATA,
104
+ highlight: {
105
+ type: 'string',
106
+ description: 'The one label or group the story is about, copied from the data'
107
+ },
108
+ scale: {
109
+ type: 'string',
110
+ enum: properties.scale.enum,
111
+ description: 'What the numbers are counted in: "30.77 trillion" is 30.77 in trillions'
112
+ },
113
+ unit: {
114
+ type: 'string',
115
+ enum: properties.unit.enum,
116
+ description: 'A currency, percent, or number for anything else'
117
+ },
118
+ annotations: ANNOTATIONS,
119
+ callout: {
120
+ type: 'string',
121
+ enum: properties.callout.enum,
122
+ description: 'share for parts, growth for one series over time, ratio when the top item dwarfs ' +
123
+ 'the second, else none; the number is computed for you'
124
+ },
125
+ source: {
126
+ type: 'string',
127
+ description: 'Where the numbers come from; the user\'s own are "User data"'
128
+ },
129
+ theme: {
130
+ type: 'string',
131
+ enum: [...THEMES],
132
+ description: 'Only when the user asks for a light or a dark chart or background'
133
+ }
134
+ },
135
+ required
136
+ };
137
+ }
138
+ function specFrom(args) {
139
+ const { title, subtitle, source, data } = args;
140
+ if (typeof title !== 'string' || typeof subtitle !== 'string' || typeof source !== 'string') {
141
+ return 'title, subtitle and source must be strings';
142
+ }
143
+ const items = Array.isArray(data) ? data.flatMap(dataItem) : [];
144
+ if (!Array.isArray(data) || items.length !== data.length) {
145
+ return 'data must be a list of { label, value }, with a string label and a number value';
146
+ }
147
+ const spec = { title, subtitle, source, data: items, annotations: notes(args.annotations) };
148
+ for (const key of WORDS) {
149
+ const word = args[key];
150
+ if (typeof word === 'string') {
151
+ spec[key] = word;
152
+ }
153
+ }
154
+ return spec;
155
+ }
156
+ function dataItem(item) {
157
+ if (!isRecord(item) || typeof item.label !== 'string' || typeof item.value !== 'number') {
158
+ return [];
159
+ }
160
+ return [
161
+ {
162
+ label: item.label,
163
+ value: item.value,
164
+ group: typeof item.group === 'string' ? item.group : undefined
165
+ }
166
+ ];
167
+ }
168
+ function notes(value) {
169
+ if (!Array.isArray(value)) {
170
+ return [];
171
+ }
172
+ return value.flatMap((note) => isRecord(note) && typeof note.label === 'string' && typeof note.text === 'string'
173
+ ? [{ label: note.label, text: note.text }]
174
+ : []);
175
+ }
@@ -0,0 +1,16 @@
1
+ import type { Mark, ThemeName } from 'qvac-charts';
2
+ import type { HarnessPlugin, PluginSetup } from '../../lib/plugins/index.ts';
3
+ export interface ChartsOptions {
4
+ readonly read?: readonly string[];
5
+ readonly theme?: ThemeName;
6
+ readonly mark?: Mark;
7
+ }
8
+ export declare class Charts implements HarnessPlugin {
9
+ readonly name = "charts";
10
+ readonly api = 1;
11
+ readonly read: readonly string[] | undefined;
12
+ private readonly _theme;
13
+ private readonly _mark;
14
+ constructor({ read, theme, mark }?: ChartsOptions);
15
+ setup(): Promise<PluginSetup>;
16
+ }
@@ -0,0 +1,21 @@
1
+ import { PluginRegistry } from '../../lib/plugins/index.js';
2
+ import { createChartTool } from './chart-tool.js';
3
+ PluginRegistry.requirePeers('charts', {
4
+ 'qvac-charts': () => import.meta.resolve('qvac-charts')
5
+ });
6
+ export class Charts {
7
+ name = 'charts';
8
+ api = PluginRegistry.API_VERSION;
9
+ read;
10
+ _theme;
11
+ _mark;
12
+ constructor({ read, theme = 'dark', mark } = {}) {
13
+ this.read = read;
14
+ this._theme = theme;
15
+ this._mark = mark;
16
+ }
17
+ async setup() {
18
+ const { Chart } = await import('qvac-charts');
19
+ return { tools: [createChartTool({ chart: Chart, theme: this._theme, mark: this._mark })] };
20
+ }
21
+ }
@@ -0,0 +1,10 @@
1
+ import type { ToolDefinition } from '../../lib/contract/index.ts';
2
+ type DrawResult = {
3
+ content: string;
4
+ answer: true;
5
+ } | {
6
+ error: string;
7
+ };
8
+ export declare function drawDiagram(args: Record<string, unknown>, userTurns?: readonly string[]): DrawResult;
9
+ export declare const drawDiagramTool: ToolDefinition;
10
+ export {};
@@ -0,0 +1,90 @@
1
+ import { DIAGRAM_TYPES, renderMermaid, sliceValue } from './render.js';
2
+ function isType(value) {
3
+ return DIAGRAM_TYPES.some((type) => type === value);
4
+ }
5
+ function stringOr(value) {
6
+ return typeof value === 'string' ? value : undefined;
7
+ }
8
+ function linksOf(raw) {
9
+ if (!Array.isArray(raw) || raw.length === 0) {
10
+ return 'links must be a non-empty list of {"from", "to", "label"}';
11
+ }
12
+ const links = [];
13
+ for (const item of raw) {
14
+ const from = stringOr(item?.from)?.trim();
15
+ if (!from) {
16
+ return 'every link needs a "from" name';
17
+ }
18
+ links.push({ from, to: stringOr(item.to), label: stringOr(item.label) });
19
+ }
20
+ return links;
21
+ }
22
+ const HORIZONTAL_ASK = /horizontal|orizzontal|left[\s-]*to[\s-]*right|sideways|landscape|\bLR\b/i;
23
+ function askedHorizontal(userTurns) {
24
+ return userTurns === undefined || userTurns.some((turn) => HORIZONTAL_ASK.test(turn));
25
+ }
26
+ export function drawDiagram(args, userTurns) {
27
+ if (!isType(args.type)) {
28
+ return { error: `type must be one of ${DIAGRAM_TYPES.join(', ')}` };
29
+ }
30
+ const links = linksOf(args.links);
31
+ if (typeof links === 'string') {
32
+ return { error: links };
33
+ }
34
+ if (args.type === 'pie' && !links.some((link) => sliceValue(link) !== undefined)) {
35
+ return { error: 'every pie slice needs its number: {"from": "Rent", "to": "40"}' };
36
+ }
37
+ const direction = args.direction === 'horizontal' && askedHorizontal(userTurns) ? 'horizontal' : 'vertical';
38
+ const code = renderMermaid({ type: args.type, direction, links });
39
+ const caption = stringOr(args.caption)?.trim();
40
+ return {
41
+ content: `\`\`\`mermaid\n${code}\n\`\`\`${caption ? `\n\n${caption}` : ''}\n`,
42
+ answer: true
43
+ };
44
+ }
45
+ export const drawDiagramTool = {
46
+ name: 'draw_diagram',
47
+ toolset: 'diagrams',
48
+ group: 'diagrams',
49
+ skillGated: true,
50
+ autoApprove: true,
51
+ description: 'Draw any diagram in the chat. Give its type and its links; the app renders the picture. ' +
52
+ 'Every box, step, date or slice is a name; every arrow or entry is one link.',
53
+ parameters: {
54
+ type: 'object',
55
+ properties: {
56
+ type: {
57
+ type: 'string',
58
+ enum: [...DIAGRAM_TYPES],
59
+ description: 'flowchart: steps or parts and arrows. sequence: who calls whom, in order. ' +
60
+ 'state: modes and the events between them. mindmap: an idea tree from one root. ' +
61
+ 'er: tables, from a table to a table it owns. class: from a parent class to a child. ' +
62
+ 'timeline: from a date to what happened. gantt: from a task to its length, like "5d". ' +
63
+ 'pie: from a slice to its number.'
64
+ },
65
+ direction: {
66
+ type: 'string',
67
+ enum: ['vertical', 'horizontal'],
68
+ description: 'Only when the user asks: "horizontal" for horizontal, left to right, or sideways; ' +
69
+ '"vertical" for top to bottom. Leave it out otherwise.'
70
+ },
71
+ links: {
72
+ type: 'array',
73
+ description: 'One entry per arrow, in order. A name ending in ? is a decision.',
74
+ items: {
75
+ type: 'object',
76
+ properties: {
77
+ from: { type: 'string' },
78
+ to: { type: 'string' },
79
+ label: { type: 'string', description: 'the text on the arrow, any characters' }
80
+ },
81
+ required: ['from', 'to']
82
+ }
83
+ },
84
+ caption: { type: 'string', description: 'One sentence saying what the diagram shows' }
85
+ },
86
+ required: ['type', 'links']
87
+ },
88
+ effect: () => 'read',
89
+ execute: async (args, context) => drawDiagram(args, context?.userTurns)
90
+ };
@@ -0,0 +1,13 @@
1
+ import type { HarnessPlugin, PluginSetup } from '../../lib/plugins/index.ts';
2
+ export { drawDiagram, drawDiagramTool } from './draw.ts';
3
+ export { renderMermaid } from './render.ts';
4
+ export interface DiagramsOptions {
5
+ readonly read?: readonly string[];
6
+ }
7
+ export declare class Diagrams implements HarnessPlugin {
8
+ readonly name = "diagrams";
9
+ readonly api = 1;
10
+ readonly read: readonly string[] | undefined;
11
+ constructor({ read }?: DiagramsOptions);
12
+ setup(): PluginSetup;
13
+ }
@@ -0,0 +1,15 @@
1
+ import { PluginRegistry } from '../../lib/plugins/index.js';
2
+ import { drawDiagramTool } from './draw.js';
3
+ export { drawDiagram, drawDiagramTool } from './draw.js';
4
+ export { renderMermaid } from './render.js';
5
+ export class Diagrams {
6
+ name = 'diagrams';
7
+ api = PluginRegistry.API_VERSION;
8
+ read;
9
+ constructor({ read } = {}) {
10
+ this.read = read;
11
+ }
12
+ setup() {
13
+ return { tools: [drawDiagramTool] };
14
+ }
15
+ }
@@ -0,0 +1,16 @@
1
+ export declare const DIAGRAM_TYPES: readonly ['flowchart', 'sequence', 'state', 'mindmap', 'er', 'class', 'timeline', 'gantt', 'pie'];
2
+ export type DiagramType = (typeof DIAGRAM_TYPES)[number];
3
+ export type Direction = 'vertical' | 'horizontal';
4
+ export interface Link {
5
+ readonly from: string;
6
+ readonly to?: string | undefined;
7
+ readonly label?: string | undefined;
8
+ }
9
+ export interface DiagramSpec {
10
+ readonly type: DiagramType;
11
+ readonly direction: Direction;
12
+ readonly links: readonly Link[];
13
+ readonly today?: Date | undefined;
14
+ }
15
+ export declare function sliceValue(link: Link): number | undefined;
16
+ export declare function renderMermaid({ type, direction, links, today }: DiagramSpec): string;
@@ -0,0 +1,285 @@
1
+ export const DIAGRAM_TYPES = [
2
+ 'flowchart',
3
+ 'sequence',
4
+ 'state',
5
+ 'mindmap',
6
+ 'er',
7
+ 'class',
8
+ 'timeline',
9
+ 'gantt',
10
+ 'pie'
11
+ ];
12
+ const LABEL_LIMIT = 80;
13
+ function text(raw) {
14
+ const flat = (raw ?? '').replace(/\s+/g, ' ').trim();
15
+ const bounded = flat.length > LABEL_LIMIT ? `${flat.slice(0, LABEL_LIMIT - 1)}…` : flat;
16
+ return bounded.replace(/"/g, "'").replace(/#/g, '#');
17
+ }
18
+ function key(name) {
19
+ return name.replace(/\s+/g, ' ').trim().toLowerCase();
20
+ }
21
+ class Ids {
22
+ _ids = new Map();
23
+ _prefix;
24
+ names = [];
25
+ constructor(prefix) {
26
+ this._prefix = prefix;
27
+ }
28
+ has(name) {
29
+ return this._ids.has(key(name));
30
+ }
31
+ of(name) {
32
+ const k = key(name);
33
+ let id = this._ids.get(k);
34
+ if (id === undefined) {
35
+ id = `${this._prefix}${this._ids.size + 1}`;
36
+ this._ids.set(k, id);
37
+ this.names.push(name);
38
+ }
39
+ return id;
40
+ }
41
+ }
42
+ function present(name) {
43
+ return name !== undefined && key(name).length > 0;
44
+ }
45
+ function flowchart(links, direction, { numbered = false } = {}) {
46
+ const ids = new Ids('n');
47
+ const lines = [`flowchart ${direction === 'horizontal' ? 'LR' : 'TD'}`];
48
+ const node = (name) => {
49
+ const seen = ids.has(name);
50
+ const id = ids.of(name);
51
+ if (seen) {
52
+ return id;
53
+ }
54
+ const label = text(name);
55
+ return name.trim().endsWith('?') ? `${id}{"${label}"}` : `${id}["${label}"]`;
56
+ };
57
+ links.forEach((link, at) => {
58
+ const from = node(link.from);
59
+ const target = present(link.to) || !numbered ? link.to : link.from;
60
+ if (!present(target)) {
61
+ lines.push(` ${from}`);
62
+ return;
63
+ }
64
+ const to = node(target);
65
+ const label = numbered ? `${at + 1}. ${link.label ?? ''}`.trim() : (link.label ?? '');
66
+ lines.push(present(label) ? ` ${from} -->|"${text(label)}"| ${to}` : ` ${from} --> ${to}`);
67
+ });
68
+ return lines;
69
+ }
70
+ function sequence(links) {
71
+ const ids = new Ids('p');
72
+ for (const link of links) {
73
+ ids.of(link.from);
74
+ if (present(link.to)) {
75
+ ids.of(link.to);
76
+ }
77
+ }
78
+ const lines = [
79
+ 'sequenceDiagram',
80
+ ...ids.names.map((name) => ` participant ${ids.of(name)} as ${text(name).replace(/;/g, ',')}`)
81
+ ];
82
+ const calls = new Set();
83
+ for (const link of links) {
84
+ const from = ids.of(link.from);
85
+ const to = present(link.to) ? ids.of(link.to) : from;
86
+ const arrow = calls.has(`${to}>${from}`) ? '-->>' : '->>';
87
+ calls.add(`${from}>${to}`);
88
+ lines.push(` ${from}${arrow}${to}: ${text(link.label).replace(/;/g, ',') || ' '}`);
89
+ }
90
+ return lines;
91
+ }
92
+ const TERMINAL = new Set(['[*]', 'start', 'end']);
93
+ function state(links, direction) {
94
+ const ids = new Ids('s');
95
+ const lines = ['stateDiagram-v2'];
96
+ if (direction === 'horizontal') {
97
+ lines.push(' direction LR');
98
+ }
99
+ const ref = (name) => {
100
+ if (TERMINAL.has(key(name))) {
101
+ return '[*]';
102
+ }
103
+ const seen = ids.has(name);
104
+ const id = ids.of(name);
105
+ if (!seen) {
106
+ lines.push(` state "${text(name)}" as ${id}`);
107
+ }
108
+ return id;
109
+ };
110
+ for (const link of links) {
111
+ const from = ref(link.from);
112
+ if (!present(link.to)) {
113
+ continue;
114
+ }
115
+ const to = ref(link.to);
116
+ const label = item(link.label);
117
+ lines.push(label ? ` ${from} --> ${to} : ${label}` : ` ${from} --> ${to}`);
118
+ }
119
+ return lines;
120
+ }
121
+ function mindmap(links) {
122
+ const children = new Map();
123
+ const names = new Map();
124
+ const targets = new Set();
125
+ for (const link of links) {
126
+ names.set(key(link.from), names.get(key(link.from)) ?? link.from);
127
+ if (!present(link.to)) {
128
+ continue;
129
+ }
130
+ names.set(key(link.to), names.get(key(link.to)) ?? link.to);
131
+ targets.add(key(link.to));
132
+ const list = children.get(key(link.from)) ?? [];
133
+ if (!list.includes(key(link.to))) {
134
+ list.push(key(link.to));
135
+ }
136
+ children.set(key(link.from), list);
137
+ }
138
+ const order = [...names.keys()];
139
+ const root = order.find((k) => !targets.has(k)) ?? order[0];
140
+ const lines = ['mindmap'];
141
+ const placed = new Set();
142
+ let count = 0;
143
+ const place = (k, depth) => {
144
+ if (placed.has(k)) {
145
+ return;
146
+ }
147
+ placed.add(k);
148
+ count++;
149
+ lines.push(`${' '.repeat(depth + 1)}m${count}["${text(names.get(k))}"]`);
150
+ for (const child of children.get(k) ?? []) {
151
+ place(child, depth + 1);
152
+ }
153
+ };
154
+ if (root !== undefined) {
155
+ place(root, 0);
156
+ }
157
+ for (const k of order) {
158
+ place(k, 1);
159
+ }
160
+ return lines;
161
+ }
162
+ function er(links) {
163
+ const ids = new Ids('e');
164
+ const declared = [];
165
+ const relations = [];
166
+ const ref = (name) => {
167
+ const seen = ids.has(name);
168
+ const id = ids.of(name);
169
+ if (!seen) {
170
+ declared.push(` ${id}["${text(name).replace(/\\/g, '/').replace(/%/g, '%')}"]`);
171
+ }
172
+ return id;
173
+ };
174
+ for (const link of links) {
175
+ const from = ref(link.from);
176
+ if (present(link.to)) {
177
+ relations.push(` ${from} ||--o{ ${ref(link.to)} : "${text(link.label) || 'has'}"`);
178
+ }
179
+ }
180
+ return ['erDiagram', ...declared, ...relations];
181
+ }
182
+ function classes(links) {
183
+ const ids = new Ids('c');
184
+ const declared = [];
185
+ const relations = [];
186
+ const ref = (name) => {
187
+ const seen = ids.has(name);
188
+ const id = ids.of(name);
189
+ if (!seen) {
190
+ declared.push(` class ${id}["${text(name)}"]`);
191
+ }
192
+ return id;
193
+ };
194
+ for (const link of links) {
195
+ const parent = ref(link.from);
196
+ if (present(link.to)) {
197
+ const label = item(link.label).replace(/\{/g, '(').replace(/\}/g, ')').replace(/\\/g, '/');
198
+ relations.push(` ${parent} <|-- ${ref(link.to)}${label ? ` : ${label}` : ''}`);
199
+ }
200
+ }
201
+ return ['classDiagram', ...declared, ...relations];
202
+ }
203
+ function item(raw) {
204
+ return text(raw).replace(/:/g, '꞉').replace(/;/g, ',');
205
+ }
206
+ const GANTT_KEYWORD = /^(click|section|title|dateformat|axisformat|tickinterval|excludes|includes|todaymarker|weekday|weekend|inclusiveenddates|topaxis|displaymode|acctitle|accdescr)\b/i;
207
+ function task(raw) {
208
+ const name = item(raw);
209
+ return GANTT_KEYWORD.test(name) ? `‣ ${name}` : name;
210
+ }
211
+ function timeline(links) {
212
+ const periods = new Map();
213
+ for (const link of links) {
214
+ const period = periods.get(key(link.from)) ?? { name: link.from, events: [] };
215
+ const event = present(link.to) ? link.to : link.label;
216
+ if (present(event)) {
217
+ period.events.push(item(event));
218
+ }
219
+ periods.set(key(link.from), period);
220
+ }
221
+ return [
222
+ 'timeline',
223
+ ...[...periods.values()].map(({ name, events }) => [` ${item(name)}`, ...events].join(' : '))
224
+ ];
225
+ }
226
+ function days(raw) {
227
+ const match = /(\d+(?:\.\d+)?)\s*(d|days?|w|weeks?)?(?![a-z])/i.exec((raw ?? '').slice(0, LABEL_LIMIT));
228
+ if (!match) {
229
+ return 1;
230
+ }
231
+ const perUnit = match[2]?.toLowerCase().startsWith('w') ? 7 : 1;
232
+ return Math.max(1, Math.round(Number(match[1]) * perUnit));
233
+ }
234
+ function gantt(links, today) {
235
+ const start = today.toISOString().slice(0, 10);
236
+ const lines = ['gantt', ' dateFormat YYYY-MM-DD'];
237
+ let section;
238
+ links.forEach((link, at) => {
239
+ if (present(link.label) && link.label !== section) {
240
+ section = link.label;
241
+ lines.push(` section ${task(section)}`);
242
+ }
243
+ const when = at === 0 ? start : `after t${at}`;
244
+ lines.push(` ${task(link.from)} :t${at + 1}, ${when}, ${days(link.to)}d`);
245
+ });
246
+ return lines;
247
+ }
248
+ export function sliceValue(link) {
249
+ const match = /-?\d+(?:\.\d+)?/.exec(`${link.to ?? ''} ${link.label ?? ''}`);
250
+ const value = match ? Number(match[0]) : Number.NaN;
251
+ return value > 0 ? value : undefined;
252
+ }
253
+ function pie(links) {
254
+ return [
255
+ 'pie',
256
+ ...links.flatMap((link) => {
257
+ const value = sliceValue(link);
258
+ return value === undefined ? [] : [` "${text(link.from)}" : ${value}`];
259
+ })
260
+ ];
261
+ }
262
+ export function renderMermaid({ type, direction, links, today }) {
263
+ switch (type) {
264
+ case 'flowchart':
265
+ return flowchart(links, direction).join('\n');
266
+ case 'sequence':
267
+ return (direction === 'horizontal'
268
+ ? flowchart(links, direction, { numbered: true })
269
+ : sequence(links)).join('\n');
270
+ case 'state':
271
+ return state(links, direction).join('\n');
272
+ case 'mindmap':
273
+ return mindmap(links).join('\n');
274
+ case 'er':
275
+ return er(links).join('\n');
276
+ case 'class':
277
+ return classes(links).join('\n');
278
+ case 'timeline':
279
+ return timeline(links).join('\n');
280
+ case 'gantt':
281
+ return gantt(links, today ?? new Date()).join('\n');
282
+ case 'pie':
283
+ return pie(links).join('\n');
284
+ }
285
+ }
@@ -0,0 +1,15 @@
1
+ import type { HarnessPlugin, PluginSetup } from '../../lib/plugins/index.ts';
2
+ export interface RipgrepOptions {
3
+ maxEntries?: number;
4
+ }
5
+ export declare class Ripgrep implements HarnessPlugin {
6
+ #private;
7
+ readonly name = "ripgrep";
8
+ readonly api = 1;
9
+ readonly provides: string[];
10
+ readonly addons: {
11
+ 'bare-ripgrep': () => Promise<void>;
12
+ };
13
+ constructor({ maxEntries }?: RipgrepOptions);
14
+ setup(): Promise<PluginSetup>;
15
+ }