codeassembly-agent-logic 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/.meta/changelog.json +77 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +7 -0
- package/README.md +85 -2
- package/bin/agent-logic.js +26 -0
- package/dist/esm/cli/check-coverage-command.d.ts +3 -0
- package/dist/esm/cli/check-coverage-command.js +99 -0
- package/dist/esm/cli/convert-command.d.ts +3 -0
- package/dist/esm/cli/convert-command.js +116 -0
- package/dist/esm/cli/find-package-root.d.ts +7 -0
- package/dist/esm/cli/find-package-root.js +31 -0
- package/dist/esm/cli/index.d.ts +1 -0
- package/dist/esm/cli/index.js +11 -0
- package/dist/esm/cli/run.d.ts +8 -0
- package/dist/esm/cli/run.js +62 -0
- package/dist/esm/contract/built-in-harnesses.d.ts +22 -0
- package/dist/esm/contract/built-in-harnesses.js +30 -0
- package/dist/esm/contract/conversion-job.d.ts +36 -0
- package/dist/esm/contract/conversion-job.js +17 -0
- package/dist/esm/contract/index.d.ts +4 -0
- package/dist/esm/contract/index.js +3 -0
- package/dist/esm/contract/viewer-contract.d.ts +804 -0
- package/dist/esm/contract/viewer-contract.js +38 -0
- package/dist/esm/convert/convert-prompt.d.ts +21 -0
- package/dist/esm/convert/convert-prompt.js +60 -0
- package/dist/esm/convert/draft-schema.d.ts +308 -0
- package/dist/esm/convert/draft-schema.js +57 -0
- package/dist/esm/convert/find-gate-spans.d.ts +6 -0
- package/dist/esm/convert/find-gate-spans.js +18 -0
- package/dist/esm/convert/format-state-name.d.ts +6 -0
- package/dist/esm/convert/format-state-name.js +6 -0
- package/dist/esm/convert/normalize-config.d.ts +11 -0
- package/dist/esm/convert/normalize-config.js +164 -0
- package/dist/esm/convert/resolve-quotes.d.ts +18 -0
- package/dist/esm/convert/resolve-quotes.js +169 -0
- package/dist/esm/convert/sanitize-name.d.ts +5 -0
- package/dist/esm/convert/sanitize-name.js +8 -0
- package/dist/esm/coverage-map/check-coverage.d.ts +20 -0
- package/dist/esm/coverage-map/check-coverage.js +21 -0
- package/dist/esm/coverage-map/coverage-map-schema.d.ts +11 -0
- package/dist/esm/coverage-map/coverage-map-schema.js +2 -0
- package/dist/esm/coverage-map/find-prompt-blocks.d.ts +15 -0
- package/dist/esm/coverage-map/find-prompt-blocks.js +140 -0
- package/dist/esm/examples/change-approval.d.ts +2 -0
- package/dist/esm/examples/change-approval.js +438 -0
- package/dist/esm/examples/examples.d.ts +9 -0
- package/dist/esm/examples/examples.js +8 -0
- package/dist/esm/examples/password-reset.d.ts +2 -0
- package/dist/esm/examples/password-reset.js +235 -0
- package/dist/esm/examples/refund-request.d.ts +2 -0
- package/dist/esm/examples/refund-request.js +233 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/layout/build-elk-graph.d.ts +18 -0
- package/dist/esm/layout/build-elk-graph.js +96 -0
- package/dist/esm/layout/card-limits.d.ts +8 -0
- package/dist/esm/layout/card-limits.js +4 -0
- package/dist/esm/layout/describe-config.d.ts +118 -0
- package/dist/esm/layout/describe-config.js +199 -0
- package/dist/esm/layout/layout-config.d.ts +5 -0
- package/dist/esm/layout/layout-config.js +73 -0
- package/dist/esm/layout/layout-metrics.d.ts +6 -0
- package/dist/esm/layout/layout-metrics.js +6 -0
- package/dist/esm/layout/pack-preamble.d.ts +20 -0
- package/dist/esm/layout/pack-preamble.js +66 -0
- package/dist/esm/layout/to-flow-elements.d.ts +46 -0
- package/dist/esm/layout/to-flow-elements.js +83 -0
- package/dist/esm/react/canvas/AmbientSelectionContext.d.ts +7 -0
- package/dist/esm/react/canvas/AmbientSelectionContext.js +5 -0
- package/dist/esm/react/canvas/DetailLevelContext.d.ts +3 -0
- package/dist/esm/react/canvas/DetailLevelContext.js +2 -0
- package/dist/esm/react/canvas/FocusNavigation.d.ts +8 -0
- package/dist/esm/react/canvas/FocusNavigation.js +92 -0
- package/dist/esm/react/canvas/StateNode.d.ts +5 -0
- package/dist/esm/react/canvas/StateNode.js +91 -0
- package/dist/esm/react/canvas/StatechartCanvas.d.ts +16 -0
- package/dist/esm/react/canvas/StatechartCanvas.js +184 -0
- package/dist/esm/react/canvas/TransitionEdge.d.ts +5 -0
- package/dist/esm/react/canvas/TransitionEdge.js +22 -0
- package/dist/esm/react/canvas/canvas-key-entries.d.ts +14 -0
- package/dist/esm/react/canvas/canvas-key-entries.js +51 -0
- package/dist/esm/react/canvas/find-element-box.d.ts +8 -0
- package/dist/esm/react/canvas/find-element-box.js +25 -0
- package/dist/esm/react/canvas/find-view-centre.d.ts +15 -0
- package/dist/esm/react/canvas/find-view-centre.js +16 -0
- package/dist/esm/react/canvas/list-chart-marks.d.ts +5 -0
- package/dist/esm/react/canvas/list-chart-marks.js +37 -0
- package/dist/esm/react/canvas/marks.d.ts +19 -0
- package/dist/esm/react/canvas/marks.js +24 -0
- package/dist/esm/react/canvas/useNewLayout.d.ts +17 -0
- package/dist/esm/react/canvas/useNewLayout.js +44 -0
- package/dist/esm/react/canvas/zoom-levels.d.ts +28 -0
- package/dist/esm/react/canvas/zoom-levels.js +54 -0
- package/dist/esm/react/hooks/conversion-client.d.ts +77 -0
- package/dist/esm/react/hooks/conversion-client.js +132 -0
- package/dist/esm/react/hooks/describe-agent-selection.d.ts +6 -0
- package/dist/esm/react/hooks/describe-agent-selection.js +18 -0
- package/dist/esm/react/hooks/describe-load-outcome.d.ts +13 -0
- package/dist/esm/react/hooks/describe-load-outcome.js +30 -0
- package/dist/esm/react/hooks/download-config.d.ts +6 -0
- package/dist/esm/react/hooks/download-config.js +11 -0
- package/dist/esm/react/hooks/example-location.d.ts +7 -0
- package/dist/esm/react/hooks/example-location.js +14 -0
- package/dist/esm/react/hooks/label-source.d.ts +21 -0
- package/dist/esm/react/hooks/label-source.js +19 -0
- package/dist/esm/react/hooks/parse-config-text.d.ts +16 -0
- package/dist/esm/react/hooks/parse-config-text.js +38 -0
- package/dist/esm/react/hooks/useAgentBridge.d.ts +28 -0
- package/dist/esm/react/hooks/useAgentBridge.js +52 -0
- package/dist/esm/react/hooks/useAgentSync.d.ts +29 -0
- package/dist/esm/react/hooks/useAgentSync.js +75 -0
- package/dist/esm/react/hooks/useChartLayout.d.ts +25 -0
- package/dist/esm/react/hooks/useChartLayout.js +29 -0
- package/dist/esm/react/hooks/useConversion.d.ts +31 -0
- package/dist/esm/react/hooks/useConversion.js +177 -0
- package/dist/esm/react/hooks/useConvertDraft.d.ts +18 -0
- package/dist/esm/react/hooks/useConvertDraft.js +11 -0
- package/dist/esm/react/hooks/useDocumentTitle.d.ts +2 -0
- package/dist/esm/react/hooks/useDocumentTitle.js +7 -0
- package/dist/esm/react/hooks/useEscapeToClear.d.ts +2 -0
- package/dist/esm/react/hooks/useEscapeToClear.js +16 -0
- package/dist/esm/react/hooks/useHarnessList.d.ts +26 -0
- package/dist/esm/react/hooks/useHarnessList.js +24 -0
- package/dist/esm/react/hooks/usePromptDirectory.d.ts +27 -0
- package/dist/esm/react/hooks/usePromptDirectory.js +34 -0
- package/dist/esm/react/hooks/useSelection.d.ts +17 -0
- package/dist/esm/react/hooks/useSelection.js +21 -0
- package/dist/esm/react/hooks/useShowAmbient.d.ts +7 -0
- package/dist/esm/react/hooks/useShowAmbient.js +22 -0
- package/dist/esm/react/hooks/useUnknownExample.d.ts +7 -0
- package/dist/esm/react/hooks/useUnknownExample.js +26 -0
- package/dist/esm/react/index.d.ts +24 -0
- package/dist/esm/react/index.js +23 -0
- package/dist/esm/react/styles.css +482 -0
- package/dist/esm/schema/agent-logic-schema.d.ts +439 -0
- package/dist/esm/schema/agent-logic-schema.js +100 -0
- package/dist/esm/schema/config-traversal.d.ts +19 -0
- package/dist/esm/schema/config-traversal.js +32 -0
- package/dist/esm/schema/validate-config.d.ts +17 -0
- package/dist/esm/schema/validate-config.js +205 -0
- package/dist/esm/schema/write-json-schema.d.ts +4 -0
- package/dist/esm/schema/write-json-schema.js +12 -0
- package/dist/esm/server/conversion-job-implementation.d.ts +11 -0
- package/dist/esm/server/conversion-job-implementation.js +84 -0
- package/dist/esm/server/create-prompts-app.d.ts +26 -0
- package/dist/esm/server/create-prompts-app.js +41 -0
- package/dist/esm/server/create-viewer-server-plugins.d.ts +15 -0
- package/dist/esm/server/create-viewer-server-plugins.js +76 -0
- package/dist/esm/server/index.d.ts +7 -0
- package/dist/esm/server/index.js +6 -0
- package/dist/esm/server/prompt-directory-schemas.d.ts +30 -0
- package/dist/esm/server/prompt-directory-schemas.js +11 -0
- package/dist/esm/server/prompt-directory.d.ts +40 -0
- package/dist/esm/server/prompt-directory.js +86 -0
- package/dist/esm/server/viewer-contract-implementation.d.ts +12 -0
- package/dist/esm/server/viewer-contract-implementation.js +85 -0
- package/dist/esm/source/fence.d.ts +16 -0
- package/dist/esm/source/fence.js +15 -0
- package/dist/esm/source/find-comment-spans.d.ts +6 -0
- package/dist/esm/source/find-comment-spans.js +60 -0
- package/dist/esm/source/find-occurrences.d.ts +2 -0
- package/dist/esm/source/find-occurrences.js +7 -0
- package/dist/esm/source/prompt-segments.d.ts +21 -0
- package/dist/esm/source/prompt-segments.js +65 -0
- package/docs/conversion-agreement.md +37 -0
- package/docs/extraction-rules.md +123 -0
- package/package.json +105 -3
- package/schema/agent-logic.schema.json +475 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const ID_WORD_COUNT = 4;
|
|
2
|
+
export function normalizeConfig(config) {
|
|
3
|
+
const { prompt } = config.meta;
|
|
4
|
+
const newIdsByOldId = assignIds(config, prompt);
|
|
5
|
+
function renameState(id) {
|
|
6
|
+
return newIdsByOldId.get(id) ?? id;
|
|
7
|
+
}
|
|
8
|
+
function normalizeTransition({ target, guard, actions, meta }) {
|
|
9
|
+
return omitUndefined({
|
|
10
|
+
target: target === undefined ? undefined : `#${renameState(target.slice(1))}`,
|
|
11
|
+
guard: guard === undefined ? undefined : normalizeEntry(guard),
|
|
12
|
+
actions: actions?.map(normalizeAction),
|
|
13
|
+
meta: normalizeMeta(meta),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function normalizeTransitions(transitions) {
|
|
17
|
+
if (transitions === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
if (!Array.isArray(transitions))
|
|
20
|
+
return normalizeTransition(transitions);
|
|
21
|
+
const [only, ...rest] = transitions;
|
|
22
|
+
if (only !== undefined && rest.length === 0)
|
|
23
|
+
return normalizeTransition(only);
|
|
24
|
+
return transitions.map(normalizeTransition);
|
|
25
|
+
}
|
|
26
|
+
function normalizeEvents(on) {
|
|
27
|
+
if (on === undefined)
|
|
28
|
+
return undefined;
|
|
29
|
+
const normalized = {};
|
|
30
|
+
for (const event of Object.keys(on).toSorted(compareStrings)) {
|
|
31
|
+
const transitions = normalizeTransitions(on[event]);
|
|
32
|
+
if (transitions !== undefined)
|
|
33
|
+
normalized[event] = transitions;
|
|
34
|
+
}
|
|
35
|
+
return normalized;
|
|
36
|
+
}
|
|
37
|
+
function normalizeChildren(states) {
|
|
38
|
+
if (states === undefined)
|
|
39
|
+
return undefined;
|
|
40
|
+
const children = sortStates(Object.entries(states), prompt).map(([, state]) => normalizeState(state));
|
|
41
|
+
return Object.fromEntries(children.map((child) => [child.id, child]));
|
|
42
|
+
}
|
|
43
|
+
function renameInitial(states, initial) {
|
|
44
|
+
if (initial === undefined)
|
|
45
|
+
return undefined;
|
|
46
|
+
const child = states?.[initial];
|
|
47
|
+
return child === undefined ? initial : renameState(child.id);
|
|
48
|
+
}
|
|
49
|
+
function normalizeState(state) {
|
|
50
|
+
return omitUndefined({
|
|
51
|
+
id: renameState(state.id),
|
|
52
|
+
type: state.type,
|
|
53
|
+
initial: renameInitial(state.states, state.initial),
|
|
54
|
+
entry: state.entry?.map(normalizeAction),
|
|
55
|
+
exit: state.exit?.map(normalizeAction),
|
|
56
|
+
on: normalizeEvents(state.on),
|
|
57
|
+
always: normalizeTransitions(state.always),
|
|
58
|
+
states: normalizeChildren(state.states),
|
|
59
|
+
meta: normalizeStateMeta(state.meta),
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return omitUndefined({
|
|
63
|
+
id: config.id,
|
|
64
|
+
initial: renameInitial(config.states, config.initial) ?? config.initial,
|
|
65
|
+
entry: config.entry?.map(normalizeAction),
|
|
66
|
+
exit: config.exit?.map(normalizeAction),
|
|
67
|
+
on: normalizeEvents(config.on),
|
|
68
|
+
always: normalizeTransitions(config.always),
|
|
69
|
+
states: normalizeChildren(config.states) ?? {},
|
|
70
|
+
meta: omitUndefined({
|
|
71
|
+
name: config.meta.name,
|
|
72
|
+
prompt,
|
|
73
|
+
source: { start: config.meta.source.start, end: config.meta.source.end },
|
|
74
|
+
unconverted: config.meta.unconverted.toSorted(compareSpans).map(normalizeUnconverted),
|
|
75
|
+
ambient: normalizeAmbient(config.meta.ambient),
|
|
76
|
+
findings: normalizeFindings(config.meta.findings),
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export function slugifyStateText(text) {
|
|
81
|
+
const words = text.toLowerCase().match(/[a-z0-9]+/g) ?? [];
|
|
82
|
+
const slug = words.slice(0, ID_WORD_COUNT).join('_');
|
|
83
|
+
if (slug === '')
|
|
84
|
+
return 'state';
|
|
85
|
+
return /^[0-9]/.test(slug) ? `s_${slug}` : slug;
|
|
86
|
+
}
|
|
87
|
+
function assignIds(config, prompt) {
|
|
88
|
+
const newIdsByOldId = new Map();
|
|
89
|
+
const used = new Set([config.id]);
|
|
90
|
+
visit(config.states);
|
|
91
|
+
return newIdsByOldId;
|
|
92
|
+
function visit(states) {
|
|
93
|
+
const siblings = sortStates(Object.entries(states ?? {}), prompt);
|
|
94
|
+
for (const [, state] of siblings) {
|
|
95
|
+
const base = slugifyStateText(sliceSource(state, prompt));
|
|
96
|
+
let id = base;
|
|
97
|
+
for (let suffix = 2; used.has(id); suffix += 1)
|
|
98
|
+
id = `${base}_${suffix}`;
|
|
99
|
+
used.add(id);
|
|
100
|
+
newIdsByOldId.set(state.id, id);
|
|
101
|
+
visit(state.states);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function compareSpans(a, b) {
|
|
106
|
+
return a.start - b.start || a.end - b.end;
|
|
107
|
+
}
|
|
108
|
+
function compareStrings(a, b) {
|
|
109
|
+
if (a === b)
|
|
110
|
+
return 0;
|
|
111
|
+
return a < b ? -1 : 1;
|
|
112
|
+
}
|
|
113
|
+
function normalizeAction({ params, meta }) {
|
|
114
|
+
return omitUndefined({
|
|
115
|
+
...normalizeEntry({ type: 'nl', params }),
|
|
116
|
+
meta: meta === undefined ? undefined : { source: { start: meta.source.start, end: meta.source.end }, gate: meta.gate },
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function normalizeAmbient(ambient) {
|
|
120
|
+
return ambient?.toSorted(compareSpans).map(({ start, end, gate }) => omitUndefined({ start, end, gate }));
|
|
121
|
+
}
|
|
122
|
+
function normalizeEntry({ params }) {
|
|
123
|
+
return { type: 'nl', params: { text: params.text } };
|
|
124
|
+
}
|
|
125
|
+
function normalizeFindings(findings) {
|
|
126
|
+
return findings
|
|
127
|
+
?.map(({ kind, sources, reason }) => ({
|
|
128
|
+
kind,
|
|
129
|
+
sources: sources.toSorted(compareSpans).map(({ start, end }) => ({ start, end })),
|
|
130
|
+
reason,
|
|
131
|
+
}))
|
|
132
|
+
.toSorted((a, b) => compareSpans(a.sources[0] ?? { start: 0, end: 0 }, b.sources[0] ?? { start: 0, end: 0 }) ||
|
|
133
|
+
compareStrings(a.kind, b.kind));
|
|
134
|
+
}
|
|
135
|
+
function normalizeMeta({ source, inferred, gate, ambient, findings }) {
|
|
136
|
+
return omitUndefined({
|
|
137
|
+
source: { start: source.start, end: source.end },
|
|
138
|
+
inferred,
|
|
139
|
+
gate,
|
|
140
|
+
ambient: normalizeAmbient(ambient),
|
|
141
|
+
findings: normalizeFindings(findings),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function normalizeStateMeta({ name, ...meta }) {
|
|
145
|
+
return omitUndefined({ name, ...normalizeMeta(meta) });
|
|
146
|
+
}
|
|
147
|
+
function normalizeUnconverted({ start, end, reason }) {
|
|
148
|
+
return omitUndefined({ start, end, reason });
|
|
149
|
+
}
|
|
150
|
+
function omitUndefined(value) {
|
|
151
|
+
for (const [key, member] of Object.entries(value)) {
|
|
152
|
+
if (member === undefined)
|
|
153
|
+
Reflect.deleteProperty(value, key);
|
|
154
|
+
}
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
function sliceSource(state, prompt) {
|
|
158
|
+
return prompt.slice(state.meta.source.start, state.meta.source.end);
|
|
159
|
+
}
|
|
160
|
+
function sortStates(entries, prompt) {
|
|
161
|
+
return entries.toSorted(([keyA, a], [keyB, b]) => compareSpans(a.meta.source, b.meta.source) ||
|
|
162
|
+
compareStrings(slugifyStateText(sliceSource(a, prompt)), slugifyStateText(sliceSource(b, prompt))) ||
|
|
163
|
+
compareStrings(keyA, keyB));
|
|
164
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AgentLogicConfig } from '../schema/agent-logic-schema.js';
|
|
2
|
+
import { type Problem } from '../schema/validate-config.js';
|
|
3
|
+
import type { Draft } from './draft-schema.js';
|
|
4
|
+
export type ResolveResult = {
|
|
5
|
+
ok: true;
|
|
6
|
+
config: AgentLogicConfig;
|
|
7
|
+
} | {
|
|
8
|
+
ok: false;
|
|
9
|
+
problems: Problem[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Turns a draft into a config by replacing each quote with its span in the prompt, giving each state its key as its
|
|
13
|
+
* id, and naming each state, the root included, by its key or id written as words. Reports every quote that does not
|
|
14
|
+
* occur in the prompt exactly once, every unconverted, ambient, or finding quote that lies inside an HTML comment, which
|
|
15
|
+
* is markup rather than logic, every `<HARD-GATE>` body that no element, ambient, or unconverted quote overlaps, and
|
|
16
|
+
* every gate flag on a quote that overlaps no gate body.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveQuotes(draft: Draft, prompt: string, rootId: string): ResolveResult;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { formatPath } from "../schema/validate-config.js";
|
|
2
|
+
import { findCommentSpans } from "../source/find-comment-spans.js";
|
|
3
|
+
import { findOccurrences } from "../source/find-occurrences.js";
|
|
4
|
+
import { findGateSpans } from "./find-gate-spans.js";
|
|
5
|
+
import { formatStateName } from "./format-state-name.js";
|
|
6
|
+
const GATE_NAME_LENGTH = 60;
|
|
7
|
+
export function resolveQuotes(draft, prompt, rootId) {
|
|
8
|
+
const problems = [];
|
|
9
|
+
const comments = findCommentSpans(prompt);
|
|
10
|
+
const quoted = [];
|
|
11
|
+
const flagged = [];
|
|
12
|
+
function resolveSpan(quote, path, list) {
|
|
13
|
+
const starts = findOccurrences(prompt, quote);
|
|
14
|
+
const [start] = starts;
|
|
15
|
+
if (start === undefined) {
|
|
16
|
+
problems.push({
|
|
17
|
+
path: formatPath(path),
|
|
18
|
+
message: `The quote "${quote}" does not occur in the prompt. Copy it verbatim.`,
|
|
19
|
+
});
|
|
20
|
+
return { start: 0, end: 0 };
|
|
21
|
+
}
|
|
22
|
+
if (starts.length > 1) {
|
|
23
|
+
problems.push({
|
|
24
|
+
path: formatPath(path),
|
|
25
|
+
message: `The quote "${quote}" occurs ${starts.length} times in the prompt. Lengthen it until it occurs once.`,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const span = { start, end: start + quote.length };
|
|
29
|
+
if (starts.length === 1 && list !== 'meta.findings')
|
|
30
|
+
quoted.push(span);
|
|
31
|
+
if (list !== undefined && starts.length === 1 && comments.some((comment) => contains(comment, span))) {
|
|
32
|
+
problems.push({
|
|
33
|
+
path: formatPath(path),
|
|
34
|
+
message: `The quote "${quote}" lies inside an HTML comment, which is not logic. Leave it out of ${list}.`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return span;
|
|
38
|
+
}
|
|
39
|
+
function resolveAmbient(ambient, path) {
|
|
40
|
+
return ambient?.map(({ quote, gate }, index) => {
|
|
41
|
+
const span = resolveSpan(quote, [...path, 'meta', 'ambient', index, 'quote'], 'meta.ambient');
|
|
42
|
+
if (gate !== true)
|
|
43
|
+
return span;
|
|
44
|
+
flagged.push({ span, path: [...path, 'meta', 'ambient', index, 'gate'] });
|
|
45
|
+
return { ...span, gate };
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function resolveFindings(findings, path) {
|
|
49
|
+
return findings?.map(({ kind, quotes, reason }, index) => ({
|
|
50
|
+
kind,
|
|
51
|
+
sources: quotes.map((quote, quoteIndex) => resolveSpan(quote, [...path, 'meta', 'findings', index, 'quotes', quoteIndex], 'meta.findings')),
|
|
52
|
+
reason,
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
function resolveActions(actions, path) {
|
|
56
|
+
return actions?.map(({ meta, ...action }, index) => {
|
|
57
|
+
if (meta === undefined)
|
|
58
|
+
return action;
|
|
59
|
+
const source = resolveSpan(meta.quote, [...path, index, 'meta', 'quote']);
|
|
60
|
+
flagged.push({ span: source, path: [...path, index, 'meta', 'gate'] });
|
|
61
|
+
return { ...action, meta: { source, gate: meta.gate } };
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function resolveMeta(meta, path) {
|
|
65
|
+
const source = resolveSpan(meta.quote, [...path, 'meta', 'quote']);
|
|
66
|
+
const ambient = resolveAmbient(meta.ambient, path);
|
|
67
|
+
const findings = resolveFindings(meta.findings, path);
|
|
68
|
+
if (meta.gate === true)
|
|
69
|
+
flagged.push({ span: source, path: [...path, 'meta', 'gate'] });
|
|
70
|
+
return {
|
|
71
|
+
source,
|
|
72
|
+
...(meta.inferred === true && { inferred: true }),
|
|
73
|
+
...(meta.gate === true && { gate: true }),
|
|
74
|
+
...(ambient !== undefined && { ambient }),
|
|
75
|
+
...(findings !== undefined && { findings }),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function resolveTransition({ actions, ...transition }, path) {
|
|
79
|
+
const resolvedActions = resolveActions(actions, [...path, 'actions']);
|
|
80
|
+
return {
|
|
81
|
+
...transition,
|
|
82
|
+
...(resolvedActions !== undefined && { actions: resolvedActions }),
|
|
83
|
+
meta: resolveMeta(transition.meta, path),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function resolveTransitions(transitions, path) {
|
|
87
|
+
if (!Array.isArray(transitions))
|
|
88
|
+
return resolveTransition(transitions, path);
|
|
89
|
+
return transitions.map((transition, index) => resolveTransition(transition, [...path, index]));
|
|
90
|
+
}
|
|
91
|
+
function resolveChildren(states, path) {
|
|
92
|
+
return Object.fromEntries(Object.entries(states).map(([key, child]) => [key, resolveState(key, child, [...path, 'states', key])]));
|
|
93
|
+
}
|
|
94
|
+
function resolveEvents(on, path) {
|
|
95
|
+
return Object.fromEntries(Object.entries(on).map(([event, transitions]) => [
|
|
96
|
+
event,
|
|
97
|
+
resolveTransitions(transitions, [...path, 'on', event]),
|
|
98
|
+
]));
|
|
99
|
+
}
|
|
100
|
+
function resolveState(key, state, path) {
|
|
101
|
+
const { on, always, states, entry, exit, meta, ...rest } = state;
|
|
102
|
+
const name = formatStateName(key);
|
|
103
|
+
return {
|
|
104
|
+
...rest,
|
|
105
|
+
id: key,
|
|
106
|
+
entry: resolveActions(entry, [...path, 'entry']),
|
|
107
|
+
exit: resolveActions(exit, [...path, 'exit']),
|
|
108
|
+
on: on === undefined ? undefined : resolveEvents(on, path),
|
|
109
|
+
always: always === undefined ? undefined : resolveTransitions(always, [...path, 'always']),
|
|
110
|
+
states: states === undefined ? undefined : resolveChildren(states, path),
|
|
111
|
+
meta: { ...(name !== undefined && { name }), ...resolveMeta(meta, path) },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const rootName = formatStateName(rootId);
|
|
115
|
+
const config = {
|
|
116
|
+
id: rootId,
|
|
117
|
+
initial: draft.initial,
|
|
118
|
+
entry: resolveActions(draft.entry, ['entry']),
|
|
119
|
+
exit: resolveActions(draft.exit, ['exit']),
|
|
120
|
+
on: draft.on === undefined ? undefined : resolveEvents(draft.on, []),
|
|
121
|
+
always: draft.always === undefined ? undefined : resolveTransitions(draft.always, ['always']),
|
|
122
|
+
states: resolveChildren(draft.states, []),
|
|
123
|
+
meta: {
|
|
124
|
+
...(rootName !== undefined && { name: rootName }),
|
|
125
|
+
prompt,
|
|
126
|
+
source: { start: 0, end: prompt.length },
|
|
127
|
+
unconverted: draft.meta.unconverted.map(({ quote, reason }, index) => ({
|
|
128
|
+
...resolveSpan(quote, ['meta', 'unconverted', index, 'quote'], 'meta.unconverted'),
|
|
129
|
+
reason,
|
|
130
|
+
})),
|
|
131
|
+
...(draft.meta.ambient !== undefined && { ambient: resolveAmbient(draft.meta.ambient, []) }),
|
|
132
|
+
...(draft.meta.findings !== undefined && { findings: resolveFindings(draft.meta.findings, []) }),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
checkGates(prompt, quoted, flagged, problems);
|
|
136
|
+
return problems.length === 0 ? { ok: true, config } : { ok: false, problems };
|
|
137
|
+
}
|
|
138
|
+
function checkGates(prompt, quoted, flagged, problems) {
|
|
139
|
+
const gates = findGateSpans(prompt);
|
|
140
|
+
for (const gate of gates) {
|
|
141
|
+
if (quoted.some((span) => overlaps(span, gate)))
|
|
142
|
+
continue;
|
|
143
|
+
problems.push({
|
|
144
|
+
path: '',
|
|
145
|
+
message: `The gate "${describeGate(prompt.slice(gate.start, gate.end))}" is not quoted by any element, ambient instruction, or unconverted passage. Represent it as the extraction rules' Gates section states.`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
for (const { span, path } of flagged) {
|
|
149
|
+
if (span.end === 0 || gates.some((gate) => overlaps(span, gate)))
|
|
150
|
+
continue;
|
|
151
|
+
problems.push({
|
|
152
|
+
path: formatPath(path),
|
|
153
|
+
message: `The quote "${prompt.slice(span.start, span.end)}" lies outside every <HARD-GATE> body. Remove "gate", or quote the gate.`,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function contains(outer, inner) {
|
|
158
|
+
return outer.start <= inner.start && inner.end <= outer.end;
|
|
159
|
+
}
|
|
160
|
+
function describeGate(body) {
|
|
161
|
+
const [firstLine = ''] = body.split('\n', 1);
|
|
162
|
+
if (firstLine.length <= GATE_NAME_LENGTH)
|
|
163
|
+
return firstLine;
|
|
164
|
+
const cut = firstLine.lastIndexOf(' ', GATE_NAME_LENGTH);
|
|
165
|
+
return `${firstLine.slice(0, cut === -1 ? GATE_NAME_LENGTH : cut)}…`;
|
|
166
|
+
}
|
|
167
|
+
function overlaps(first, second) {
|
|
168
|
+
return first.start < second.end && second.start < first.end;
|
|
169
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CoverageMap } from './coverage-map-schema.js';
|
|
2
|
+
import { type PromptBlock } from './find-prompt-blocks.js';
|
|
3
|
+
/** A map entry whose quote does not occur in the prompt exactly once. */
|
|
4
|
+
export interface QuoteProblem {
|
|
5
|
+
index: number;
|
|
6
|
+
quote: string;
|
|
7
|
+
occurrences: number;
|
|
8
|
+
}
|
|
9
|
+
export interface CoverageResult {
|
|
10
|
+
/** The blocks that no entry's quote overlaps, in prompt order. */
|
|
11
|
+
unmapped: PromptBlock[];
|
|
12
|
+
quoteProblems: QuoteProblem[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Checks a coverage map against its prompt. A block is mapped when the quote of some entry, whatever element the entry
|
|
16
|
+
* names, occurs in the prompt exactly once and overlaps the block.
|
|
17
|
+
*/
|
|
18
|
+
export declare function checkCoverage(prompt: string, map: CoverageMap): CoverageResult;
|
|
19
|
+
/** Returns the block's first words, by which the checker names it. */
|
|
20
|
+
export declare function describeBlock(prompt: string, block: PromptBlock, wordCount?: number): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { findOccurrences } from "../source/find-occurrences.js";
|
|
2
|
+
import { findPromptBlocks } from "./find-prompt-blocks.js";
|
|
3
|
+
export function checkCoverage(prompt, map) {
|
|
4
|
+
const quoteProblems = [];
|
|
5
|
+
const spans = [];
|
|
6
|
+
for (const [index, { quote }] of map.entries()) {
|
|
7
|
+
const starts = findOccurrences(prompt, quote);
|
|
8
|
+
const [start] = starts;
|
|
9
|
+
if (starts.length === 1 && start !== undefined)
|
|
10
|
+
spans.push({ start, end: start + quote.length });
|
|
11
|
+
else
|
|
12
|
+
quoteProblems.push({ index, quote, occurrences: starts.length });
|
|
13
|
+
}
|
|
14
|
+
const unmapped = findPromptBlocks(prompt).filter((block) => spans.every((span) => span.end <= block.start || block.end <= span.start));
|
|
15
|
+
return { unmapped, quoteProblems };
|
|
16
|
+
}
|
|
17
|
+
export function describeBlock(prompt, block, wordCount = 8) {
|
|
18
|
+
const words = prompt.slice(block.start, block.end).trim().split(/\s+/);
|
|
19
|
+
const opening = words.slice(0, wordCount).join(' ');
|
|
20
|
+
return words.length > wordCount ? `${opening}…` : opening;
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* The coverage map that the `convert-agent-prompt` skill writes before converting: one entry per instruction of the
|
|
4
|
+
* prompt, whose quote is copied verbatim from the prompt and must occur in it exactly once, and whose element names
|
|
5
|
+
* the draft's state, transition, or action that holds the instruction, or `ambient`, `unconverted`, or `none`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const coverageMapSchema: z.ZodArray<z.ZodObject<{
|
|
8
|
+
quote: z.ZodString;
|
|
9
|
+
element: z.ZodString;
|
|
10
|
+
}, z.core.$strict>>;
|
|
11
|
+
export type CoverageMap = z.infer<typeof coverageMapSchema>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A paragraph, list item, table body row, or fenced block of a prompt, which a coverage map must quote. */
|
|
2
|
+
export interface PromptBlock {
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
/** The 1-based number of the block's first line. */
|
|
6
|
+
line: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Cuts the prompt into the blocks that hold its instructions. A list item runs to the next item at any depth and
|
|
10
|
+
* keeps an indented paragraph that follows it; a line without a marker continues the block before it. Headings,
|
|
11
|
+
* thematic breaks, table header and separator rows, lines that hold only an HTML tag, a leading YAML front matter,
|
|
12
|
+
* and HTML comments hold no instruction, so they are not blocks. A one-line paragraph that ends with a colon, such as
|
|
13
|
+
* "Example:", introduces the fenced block after it and belongs to that block, since a prompt may repeat it verbatim.
|
|
14
|
+
*/
|
|
15
|
+
export declare function findPromptBlocks(prompt: string): PromptBlock[];
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { isFenceCloser, matchFenceOpener } from "../source/fence.js";
|
|
2
|
+
import { findCommentSpans } from "../source/find-comment-spans.js";
|
|
3
|
+
const BLOCKQUOTE = /^\s*>/;
|
|
4
|
+
const HEADING = /^ {0,3}#{1,6}(?:\s|$)/;
|
|
5
|
+
const LIST_ITEM = /^\s*(?:[-*+]|\d+[.)])\s+/;
|
|
6
|
+
const LONE_TAG = /^\s*<\/?[A-Za-z][\w-]*>\s*$/;
|
|
7
|
+
const TABLE_ROW = /^\s*\|/;
|
|
8
|
+
const TABLE_SEPARATOR = /^\s*\|?(?:\s*:?-+:?\s*\|)+\s*:?-*:?\s*$/;
|
|
9
|
+
const THEMATIC_BREAK = /^ {0,3}(?:-{3,}|\*{3,}|_{3,})\s*$/;
|
|
10
|
+
export function findPromptBlocks(prompt) {
|
|
11
|
+
const blocks = [];
|
|
12
|
+
const source = blankComments(prompt);
|
|
13
|
+
const lines = splitLines(source);
|
|
14
|
+
let current;
|
|
15
|
+
let afterBlank = false;
|
|
16
|
+
function flush() {
|
|
17
|
+
if (current !== undefined)
|
|
18
|
+
blocks.push({ start: current.start, end: current.end, line: current.line });
|
|
19
|
+
current = undefined;
|
|
20
|
+
afterBlank = false;
|
|
21
|
+
}
|
|
22
|
+
function open(kind, line) {
|
|
23
|
+
flush();
|
|
24
|
+
current = { kind, start: line.start + indentOf(line.text), end: line.end, line: line.number };
|
|
25
|
+
}
|
|
26
|
+
function continueOrOpen(kind, line, continues) {
|
|
27
|
+
if (current !== undefined && continues) {
|
|
28
|
+
current.end = line.end;
|
|
29
|
+
afterBlank = false;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
open(kind, line);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function takeFence(line, index) {
|
|
36
|
+
if (current?.kind === 'paragraph' && isLeadIn(source.slice(current.start, current.end)))
|
|
37
|
+
current.kind = 'fence';
|
|
38
|
+
else
|
|
39
|
+
open('fence', line);
|
|
40
|
+
const closing = findFenceClose(lines, index);
|
|
41
|
+
if (current !== undefined)
|
|
42
|
+
current.end = (lines[closing] ?? line).end;
|
|
43
|
+
flush();
|
|
44
|
+
return closing;
|
|
45
|
+
}
|
|
46
|
+
function takeText(line) {
|
|
47
|
+
const continues = !afterBlank || (current?.kind === 'item' && indentOf(line.text) >= 2);
|
|
48
|
+
continueOrOpen('paragraph', line, continues);
|
|
49
|
+
}
|
|
50
|
+
const handlers = {
|
|
51
|
+
blank: () => {
|
|
52
|
+
afterBlank = current !== undefined;
|
|
53
|
+
},
|
|
54
|
+
fence: takeFence,
|
|
55
|
+
item: (line) => {
|
|
56
|
+
open('item', line);
|
|
57
|
+
},
|
|
58
|
+
quote: (line) => {
|
|
59
|
+
continueOrOpen('quote', line, current?.kind === 'quote' && !afterBlank);
|
|
60
|
+
},
|
|
61
|
+
row: (line) => {
|
|
62
|
+
open('paragraph', line);
|
|
63
|
+
flush();
|
|
64
|
+
},
|
|
65
|
+
skip: () => {
|
|
66
|
+
flush();
|
|
67
|
+
},
|
|
68
|
+
text: (line) => {
|
|
69
|
+
takeText(line);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
for (let index = skipFrontMatter(lines); index < lines.length; index++) {
|
|
73
|
+
const line = lines[index];
|
|
74
|
+
if (line === undefined)
|
|
75
|
+
break;
|
|
76
|
+
index = handlers[classifyLine(lines, index)](line, index) ?? index;
|
|
77
|
+
}
|
|
78
|
+
flush();
|
|
79
|
+
return blocks;
|
|
80
|
+
}
|
|
81
|
+
function blankComments(prompt) {
|
|
82
|
+
let result = '';
|
|
83
|
+
let from = 0;
|
|
84
|
+
for (const { start, end } of findCommentSpans(prompt)) {
|
|
85
|
+
result += prompt.slice(from, start) + prompt.slice(start, end).replaceAll(/[^\n]/g, ' ');
|
|
86
|
+
from = end;
|
|
87
|
+
}
|
|
88
|
+
return result + prompt.slice(from);
|
|
89
|
+
}
|
|
90
|
+
function classifyLine(lines, index) {
|
|
91
|
+
const text = lines[index]?.text ?? '';
|
|
92
|
+
if (matchFenceOpener(text) !== undefined)
|
|
93
|
+
return 'fence';
|
|
94
|
+
if (text.trim() === '')
|
|
95
|
+
return 'blank';
|
|
96
|
+
if (HEADING.test(text) || THEMATIC_BREAK.test(text) || LONE_TAG.test(text))
|
|
97
|
+
return 'skip';
|
|
98
|
+
if (TABLE_ROW.test(text)) {
|
|
99
|
+
const next = lines[index + 1];
|
|
100
|
+
const isHeader = next !== undefined && TABLE_SEPARATOR.test(next.text);
|
|
101
|
+
return TABLE_SEPARATOR.test(text) || isHeader ? 'skip' : 'row';
|
|
102
|
+
}
|
|
103
|
+
if (LIST_ITEM.test(text))
|
|
104
|
+
return 'item';
|
|
105
|
+
if (BLOCKQUOTE.test(text))
|
|
106
|
+
return 'quote';
|
|
107
|
+
return 'text';
|
|
108
|
+
}
|
|
109
|
+
function findFenceClose(lines, opening) {
|
|
110
|
+
const fence = matchFenceOpener(lines[opening]?.text ?? '');
|
|
111
|
+
if (fence === undefined)
|
|
112
|
+
return opening;
|
|
113
|
+
for (let index = opening + 1; index < lines.length; index++) {
|
|
114
|
+
if (isFenceCloser(lines[index]?.text ?? '', fence))
|
|
115
|
+
return index;
|
|
116
|
+
}
|
|
117
|
+
return lines.length - 1;
|
|
118
|
+
}
|
|
119
|
+
function indentOf(text) {
|
|
120
|
+
return text.length - text.trimStart().length;
|
|
121
|
+
}
|
|
122
|
+
function isLeadIn(text) {
|
|
123
|
+
return !text.includes('\n') && text.trimEnd().endsWith(':');
|
|
124
|
+
}
|
|
125
|
+
function skipFrontMatter(lines) {
|
|
126
|
+
if (lines[0]?.text !== '---')
|
|
127
|
+
return 0;
|
|
128
|
+
const close = lines.findIndex((line, index) => index > 0 && line.text === '---');
|
|
129
|
+
return close === -1 ? 0 : close + 1;
|
|
130
|
+
}
|
|
131
|
+
function splitLines(text) {
|
|
132
|
+
const lines = [];
|
|
133
|
+
let start = 0;
|
|
134
|
+
for (const [index, rawLine] of text.split('\n').entries()) {
|
|
135
|
+
const lineText = rawLine.endsWith('\r') ? rawLine.slice(0, -1) : rawLine;
|
|
136
|
+
lines.push({ start, end: start + lineText.length, number: index + 1, text: lineText });
|
|
137
|
+
start += rawLine.length + 1;
|
|
138
|
+
}
|
|
139
|
+
return lines;
|
|
140
|
+
}
|