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.
Files changed (168) hide show
  1. package/.meta/changelog.json +77 -0
  2. package/CHANGELOG.md +28 -0
  3. package/LICENSE +7 -0
  4. package/README.md +85 -2
  5. package/bin/agent-logic.js +26 -0
  6. package/dist/esm/cli/check-coverage-command.d.ts +3 -0
  7. package/dist/esm/cli/check-coverage-command.js +99 -0
  8. package/dist/esm/cli/convert-command.d.ts +3 -0
  9. package/dist/esm/cli/convert-command.js +116 -0
  10. package/dist/esm/cli/find-package-root.d.ts +7 -0
  11. package/dist/esm/cli/find-package-root.js +31 -0
  12. package/dist/esm/cli/index.d.ts +1 -0
  13. package/dist/esm/cli/index.js +11 -0
  14. package/dist/esm/cli/run.d.ts +8 -0
  15. package/dist/esm/cli/run.js +62 -0
  16. package/dist/esm/contract/built-in-harnesses.d.ts +22 -0
  17. package/dist/esm/contract/built-in-harnesses.js +30 -0
  18. package/dist/esm/contract/conversion-job.d.ts +36 -0
  19. package/dist/esm/contract/conversion-job.js +17 -0
  20. package/dist/esm/contract/index.d.ts +4 -0
  21. package/dist/esm/contract/index.js +3 -0
  22. package/dist/esm/contract/viewer-contract.d.ts +804 -0
  23. package/dist/esm/contract/viewer-contract.js +38 -0
  24. package/dist/esm/convert/convert-prompt.d.ts +21 -0
  25. package/dist/esm/convert/convert-prompt.js +60 -0
  26. package/dist/esm/convert/draft-schema.d.ts +308 -0
  27. package/dist/esm/convert/draft-schema.js +57 -0
  28. package/dist/esm/convert/find-gate-spans.d.ts +6 -0
  29. package/dist/esm/convert/find-gate-spans.js +18 -0
  30. package/dist/esm/convert/format-state-name.d.ts +6 -0
  31. package/dist/esm/convert/format-state-name.js +6 -0
  32. package/dist/esm/convert/normalize-config.d.ts +11 -0
  33. package/dist/esm/convert/normalize-config.js +164 -0
  34. package/dist/esm/convert/resolve-quotes.d.ts +18 -0
  35. package/dist/esm/convert/resolve-quotes.js +169 -0
  36. package/dist/esm/convert/sanitize-name.d.ts +5 -0
  37. package/dist/esm/convert/sanitize-name.js +8 -0
  38. package/dist/esm/coverage-map/check-coverage.d.ts +20 -0
  39. package/dist/esm/coverage-map/check-coverage.js +21 -0
  40. package/dist/esm/coverage-map/coverage-map-schema.d.ts +11 -0
  41. package/dist/esm/coverage-map/coverage-map-schema.js +2 -0
  42. package/dist/esm/coverage-map/find-prompt-blocks.d.ts +15 -0
  43. package/dist/esm/coverage-map/find-prompt-blocks.js +140 -0
  44. package/dist/esm/examples/change-approval.d.ts +2 -0
  45. package/dist/esm/examples/change-approval.js +438 -0
  46. package/dist/esm/examples/examples.d.ts +9 -0
  47. package/dist/esm/examples/examples.js +8 -0
  48. package/dist/esm/examples/password-reset.d.ts +2 -0
  49. package/dist/esm/examples/password-reset.js +235 -0
  50. package/dist/esm/examples/refund-request.d.ts +2 -0
  51. package/dist/esm/examples/refund-request.js +233 -0
  52. package/dist/esm/index.d.ts +13 -0
  53. package/dist/esm/index.js +12 -0
  54. package/dist/esm/layout/build-elk-graph.d.ts +18 -0
  55. package/dist/esm/layout/build-elk-graph.js +96 -0
  56. package/dist/esm/layout/card-limits.d.ts +8 -0
  57. package/dist/esm/layout/card-limits.js +4 -0
  58. package/dist/esm/layout/describe-config.d.ts +118 -0
  59. package/dist/esm/layout/describe-config.js +199 -0
  60. package/dist/esm/layout/layout-config.d.ts +5 -0
  61. package/dist/esm/layout/layout-config.js +73 -0
  62. package/dist/esm/layout/layout-metrics.d.ts +6 -0
  63. package/dist/esm/layout/layout-metrics.js +6 -0
  64. package/dist/esm/layout/pack-preamble.d.ts +20 -0
  65. package/dist/esm/layout/pack-preamble.js +66 -0
  66. package/dist/esm/layout/to-flow-elements.d.ts +46 -0
  67. package/dist/esm/layout/to-flow-elements.js +83 -0
  68. package/dist/esm/react/canvas/AmbientSelectionContext.d.ts +7 -0
  69. package/dist/esm/react/canvas/AmbientSelectionContext.js +5 -0
  70. package/dist/esm/react/canvas/DetailLevelContext.d.ts +3 -0
  71. package/dist/esm/react/canvas/DetailLevelContext.js +2 -0
  72. package/dist/esm/react/canvas/FocusNavigation.d.ts +8 -0
  73. package/dist/esm/react/canvas/FocusNavigation.js +92 -0
  74. package/dist/esm/react/canvas/StateNode.d.ts +5 -0
  75. package/dist/esm/react/canvas/StateNode.js +91 -0
  76. package/dist/esm/react/canvas/StatechartCanvas.d.ts +16 -0
  77. package/dist/esm/react/canvas/StatechartCanvas.js +184 -0
  78. package/dist/esm/react/canvas/TransitionEdge.d.ts +5 -0
  79. package/dist/esm/react/canvas/TransitionEdge.js +22 -0
  80. package/dist/esm/react/canvas/canvas-key-entries.d.ts +14 -0
  81. package/dist/esm/react/canvas/canvas-key-entries.js +51 -0
  82. package/dist/esm/react/canvas/find-element-box.d.ts +8 -0
  83. package/dist/esm/react/canvas/find-element-box.js +25 -0
  84. package/dist/esm/react/canvas/find-view-centre.d.ts +15 -0
  85. package/dist/esm/react/canvas/find-view-centre.js +16 -0
  86. package/dist/esm/react/canvas/list-chart-marks.d.ts +5 -0
  87. package/dist/esm/react/canvas/list-chart-marks.js +37 -0
  88. package/dist/esm/react/canvas/marks.d.ts +19 -0
  89. package/dist/esm/react/canvas/marks.js +24 -0
  90. package/dist/esm/react/canvas/useNewLayout.d.ts +17 -0
  91. package/dist/esm/react/canvas/useNewLayout.js +44 -0
  92. package/dist/esm/react/canvas/zoom-levels.d.ts +28 -0
  93. package/dist/esm/react/canvas/zoom-levels.js +54 -0
  94. package/dist/esm/react/hooks/conversion-client.d.ts +77 -0
  95. package/dist/esm/react/hooks/conversion-client.js +132 -0
  96. package/dist/esm/react/hooks/describe-agent-selection.d.ts +6 -0
  97. package/dist/esm/react/hooks/describe-agent-selection.js +18 -0
  98. package/dist/esm/react/hooks/describe-load-outcome.d.ts +13 -0
  99. package/dist/esm/react/hooks/describe-load-outcome.js +30 -0
  100. package/dist/esm/react/hooks/download-config.d.ts +6 -0
  101. package/dist/esm/react/hooks/download-config.js +11 -0
  102. package/dist/esm/react/hooks/example-location.d.ts +7 -0
  103. package/dist/esm/react/hooks/example-location.js +14 -0
  104. package/dist/esm/react/hooks/label-source.d.ts +21 -0
  105. package/dist/esm/react/hooks/label-source.js +19 -0
  106. package/dist/esm/react/hooks/parse-config-text.d.ts +16 -0
  107. package/dist/esm/react/hooks/parse-config-text.js +38 -0
  108. package/dist/esm/react/hooks/useAgentBridge.d.ts +28 -0
  109. package/dist/esm/react/hooks/useAgentBridge.js +52 -0
  110. package/dist/esm/react/hooks/useAgentSync.d.ts +29 -0
  111. package/dist/esm/react/hooks/useAgentSync.js +75 -0
  112. package/dist/esm/react/hooks/useChartLayout.d.ts +25 -0
  113. package/dist/esm/react/hooks/useChartLayout.js +29 -0
  114. package/dist/esm/react/hooks/useConversion.d.ts +31 -0
  115. package/dist/esm/react/hooks/useConversion.js +177 -0
  116. package/dist/esm/react/hooks/useConvertDraft.d.ts +18 -0
  117. package/dist/esm/react/hooks/useConvertDraft.js +11 -0
  118. package/dist/esm/react/hooks/useDocumentTitle.d.ts +2 -0
  119. package/dist/esm/react/hooks/useDocumentTitle.js +7 -0
  120. package/dist/esm/react/hooks/useEscapeToClear.d.ts +2 -0
  121. package/dist/esm/react/hooks/useEscapeToClear.js +16 -0
  122. package/dist/esm/react/hooks/useHarnessList.d.ts +26 -0
  123. package/dist/esm/react/hooks/useHarnessList.js +24 -0
  124. package/dist/esm/react/hooks/usePromptDirectory.d.ts +27 -0
  125. package/dist/esm/react/hooks/usePromptDirectory.js +34 -0
  126. package/dist/esm/react/hooks/useSelection.d.ts +17 -0
  127. package/dist/esm/react/hooks/useSelection.js +21 -0
  128. package/dist/esm/react/hooks/useShowAmbient.d.ts +7 -0
  129. package/dist/esm/react/hooks/useShowAmbient.js +22 -0
  130. package/dist/esm/react/hooks/useUnknownExample.d.ts +7 -0
  131. package/dist/esm/react/hooks/useUnknownExample.js +26 -0
  132. package/dist/esm/react/index.d.ts +24 -0
  133. package/dist/esm/react/index.js +23 -0
  134. package/dist/esm/react/styles.css +482 -0
  135. package/dist/esm/schema/agent-logic-schema.d.ts +439 -0
  136. package/dist/esm/schema/agent-logic-schema.js +100 -0
  137. package/dist/esm/schema/config-traversal.d.ts +19 -0
  138. package/dist/esm/schema/config-traversal.js +32 -0
  139. package/dist/esm/schema/validate-config.d.ts +17 -0
  140. package/dist/esm/schema/validate-config.js +205 -0
  141. package/dist/esm/schema/write-json-schema.d.ts +4 -0
  142. package/dist/esm/schema/write-json-schema.js +12 -0
  143. package/dist/esm/server/conversion-job-implementation.d.ts +11 -0
  144. package/dist/esm/server/conversion-job-implementation.js +84 -0
  145. package/dist/esm/server/create-prompts-app.d.ts +26 -0
  146. package/dist/esm/server/create-prompts-app.js +41 -0
  147. package/dist/esm/server/create-viewer-server-plugins.d.ts +15 -0
  148. package/dist/esm/server/create-viewer-server-plugins.js +76 -0
  149. package/dist/esm/server/index.d.ts +7 -0
  150. package/dist/esm/server/index.js +6 -0
  151. package/dist/esm/server/prompt-directory-schemas.d.ts +30 -0
  152. package/dist/esm/server/prompt-directory-schemas.js +11 -0
  153. package/dist/esm/server/prompt-directory.d.ts +40 -0
  154. package/dist/esm/server/prompt-directory.js +86 -0
  155. package/dist/esm/server/viewer-contract-implementation.d.ts +12 -0
  156. package/dist/esm/server/viewer-contract-implementation.js +85 -0
  157. package/dist/esm/source/fence.d.ts +16 -0
  158. package/dist/esm/source/fence.js +15 -0
  159. package/dist/esm/source/find-comment-spans.d.ts +6 -0
  160. package/dist/esm/source/find-comment-spans.js +60 -0
  161. package/dist/esm/source/find-occurrences.d.ts +2 -0
  162. package/dist/esm/source/find-occurrences.js +7 -0
  163. package/dist/esm/source/prompt-segments.d.ts +21 -0
  164. package/dist/esm/source/prompt-segments.js +65 -0
  165. package/docs/conversion-agreement.md +37 -0
  166. package/docs/extraction-rules.md +123 -0
  167. package/package.json +105 -3
  168. package/schema/agent-logic.schema.json +475 -0
@@ -0,0 +1,38 @@
1
+ import { defineContract } from '@williamthorsen/telefactor/contract';
2
+ import { z } from 'zod';
3
+ import { agentLogicConfigSchema } from "../schema/agent-logic-schema.js";
4
+ export const contractChartSchema = z.object({
5
+ source: z.string(),
6
+ downloadName: z.string(),
7
+ config: agentLogicConfigSchema,
8
+ });
9
+ export const viewerContract = defineContract({
10
+ state: z.object({
11
+ chart: contractChartSchema.optional(),
12
+ selectedPath: z.string().optional(),
13
+ savedPrompt: z.object({ fileName: z.string(), path: z.string() }).optional(),
14
+ }),
15
+ actions: {
16
+ save_prompt: {
17
+ description: "Saves a prompt to the viewer's prompts directory as \"<name>.md\", the name reduced to lowercase letters, digits, and hyphens; refuses a name that a saved prompt or agent-logic file already uses. The state's `savedPrompt.path` is the saved file. To chart it, run the convert-agent-prompt skill on that path with its default output, which it writes beside the prompt, then call `load_chart` with the output's file name.",
18
+ input: z.object({
19
+ name: z.string().describe('The prompt\'s name, such as "refund triage".'),
20
+ text: z.string().describe('The prompt text.'),
21
+ }),
22
+ },
23
+ load_chart: {
24
+ description: 'Loads an agent-logic file from the prompts directory into the page, such as the "<name>.agent-logic.json" that convert-agent-prompt wrote beside a saved prompt, and clears the selection. Refuses a file that the directory does not list or that does not validate, naming its problems.',
25
+ input: z.object({
26
+ fileName: z.string().describe('A file name in the prompts directory, not a path.'),
27
+ }),
28
+ },
29
+ show_chart: {
30
+ description: 'Shows a chart given as its config, and clears the selection. The page calls it to publish a chart that it loaded; prefer `load_chart` for a saved file.',
31
+ input: contractChartSchema,
32
+ },
33
+ select_element: {
34
+ description: 'Selects an element of the loaded chart by its path into the config, such as `states.greeting` or `states.greeting.on["user.replied"]`, or clears the selection when `path` is omitted. Refuses a path that the chart does not contain.',
35
+ input: z.object({ path: z.string().optional() }),
36
+ },
37
+ },
38
+ });
@@ -0,0 +1,21 @@
1
+ import type { AgentLogicConfig } from '../schema/agent-logic-schema.js';
2
+ import { type Problem } from '../schema/validate-config.js';
3
+ import { type OutOfScope } from './draft-schema.js';
4
+ export type ConvertResult = {
5
+ ok: true;
6
+ config: AgentLogicConfig;
7
+ text: string;
8
+ } | {
9
+ ok: false;
10
+ problems: Problem[];
11
+ } | {
12
+ ok: false;
13
+ outOfScope: OutOfScope;
14
+ };
15
+ /**
16
+ * Turns the text of a draft into a normalized, valid config whose root id derives from the prompt file's name, returns
17
+ * the draft's out-of-scope verdict, or reports every problem that prevents either.
18
+ */
19
+ export declare function convertPrompt(prompt: string, draftText: string, promptFileName: string): ConvertResult;
20
+ /** Derives the root id from a prompt file's base name: its words in lowercase ASCII, joined by `_`. */
21
+ export declare function deriveRootId(promptFileName: string): string;
@@ -0,0 +1,60 @@
1
+ import path from 'node:path';
2
+ import { describeError } from '@williamthorsen/toolbelt.errors';
3
+ import { formatPath, validateConfig } from "../schema/validate-config.js";
4
+ import { draftSchema, outOfScopeSchema } from "./draft-schema.js";
5
+ import { normalizeConfig } from "./normalize-config.js";
6
+ import { resolveQuotes } from "./resolve-quotes.js";
7
+ export function convertPrompt(prompt, draftText, promptFileName) {
8
+ let value;
9
+ try {
10
+ value = JSON.parse(draftText);
11
+ }
12
+ catch (error) {
13
+ const reason = describeError(error);
14
+ return { ok: false, problems: [{ path: '', message: `The draft is not valid JSON: ${reason}. Correct it.` }] };
15
+ }
16
+ if (typeof value === 'object' && value !== null && 'outOfScope' in value) {
17
+ const verdict = outOfScopeSchema.safeParse(value);
18
+ return verdict.success
19
+ ? { ok: false, outOfScope: verdict.data.outOfScope }
20
+ : { ok: false, problems: verdict.error.issues.flatMap((issue) => describeDraftIssue(issue, [])) };
21
+ }
22
+ const parsed = draftSchema.safeParse(value);
23
+ if (!parsed.success) {
24
+ return {
25
+ ok: false,
26
+ problems: parsed.error.issues.flatMap((issue) => describeDraftIssue(issue, [])),
27
+ };
28
+ }
29
+ const resolved = resolveQuotes(parsed.data, prompt, deriveRootId(promptFileName));
30
+ if (!resolved.ok)
31
+ return resolved;
32
+ const checked = validateConfig(resolved.config);
33
+ if (!checked.ok)
34
+ return checked;
35
+ const validated = validateConfig(normalizeConfig(checked.config));
36
+ if (!validated.ok)
37
+ return validated;
38
+ return { ok: true, config: validated.config, text: `${JSON.stringify(validated.config, undefined, 2)}\n` };
39
+ }
40
+ export function deriveRootId(promptFileName) {
41
+ const baseName = path.basename(promptFileName, path.extname(promptFileName));
42
+ const words = baseName.toLowerCase().match(/[a-z0-9]+/g) ?? [];
43
+ const id = words.join('_');
44
+ if (id === '')
45
+ return 'agent';
46
+ return /^[0-9]/.test(id) ? `a_${id}` : id;
47
+ }
48
+ function describeDraftIssue(issue, parentPath) {
49
+ const path = [...parentPath, ...issue.path];
50
+ if (issue.code === 'invalid_union') {
51
+ const deepest = issue.errors.toSorted((a, b) => maxPathLength(b) - maxPathLength(a))[0];
52
+ if (deepest !== undefined && maxPathLength(deepest) > 0) {
53
+ return deepest.flatMap((nested) => describeDraftIssue(nested, path));
54
+ }
55
+ }
56
+ return [{ path: formatPath(path), message: `${issue.message}.` }];
57
+ }
58
+ function maxPathLength(issues) {
59
+ return Math.max(0, ...issues.map((issue) => issue.path.length));
60
+ }
@@ -0,0 +1,308 @@
1
+ import { z } from 'zod';
2
+ /** The converter's findings about an element, or about the prompt on the root, each quoting the passages involved. */
3
+ declare const draftFindingsSchema: z.ZodOptional<z.ZodArray<z.ZodObject<{
4
+ kind: z.ZodEnum<{
5
+ contradiction: "contradiction";
6
+ "unused-input": "unused-input";
7
+ "unhandled-case": "unhandled-case";
8
+ "inferred-resolution": "inferred-resolution";
9
+ }>;
10
+ quotes: z.ZodArray<z.ZodString>;
11
+ reason: z.ZodString;
12
+ }, z.core.$strict>>>;
13
+ /** Where an element comes from: a passage copied verbatim from the prompt, which must occur in it exactly once. */
14
+ declare const draftMetaSchema: z.ZodObject<{
15
+ quote: z.ZodString;
16
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
17
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
18
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
19
+ quote: z.ZodString;
20
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
21
+ }, z.core.$strict>>>;
22
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
23
+ kind: z.ZodEnum<{
24
+ contradiction: "contradiction";
25
+ "unused-input": "unused-input";
26
+ "unhandled-case": "unhandled-case";
27
+ "inferred-resolution": "inferred-resolution";
28
+ }>;
29
+ quotes: z.ZodArray<z.ZodString>;
30
+ reason: z.ZodString;
31
+ }, z.core.$strict>>>;
32
+ }, z.core.$strict>;
33
+ /** An action; a gated action quotes the gate that it represents. */
34
+ declare const draftActionSchema: z.ZodObject<{
35
+ type: z.ZodLiteral<"nl">;
36
+ params: z.ZodObject<{
37
+ text: z.ZodString;
38
+ }, z.core.$strict>;
39
+ meta: z.ZodOptional<z.ZodObject<{
40
+ quote: z.ZodString;
41
+ gate: z.ZodLiteral<true>;
42
+ }, z.core.$strict>>;
43
+ }, z.core.$strict>;
44
+ /** A transition whose `target` is `#` followed by the key of a state in the draft. */
45
+ declare const draftTransitionSchema: z.ZodObject<{
46
+ target: z.ZodOptional<z.ZodString>;
47
+ guard: z.ZodOptional<z.ZodObject<{
48
+ type: z.ZodLiteral<"nl">;
49
+ params: z.ZodObject<{
50
+ text: z.ZodString;
51
+ }, z.core.$strict>;
52
+ }, z.core.$strict>>;
53
+ actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
54
+ type: z.ZodLiteral<"nl">;
55
+ params: z.ZodObject<{
56
+ text: z.ZodString;
57
+ }, z.core.$strict>;
58
+ meta: z.ZodOptional<z.ZodObject<{
59
+ quote: z.ZodString;
60
+ gate: z.ZodLiteral<true>;
61
+ }, z.core.$strict>>;
62
+ }, z.core.$strict>>>;
63
+ meta: z.ZodObject<{
64
+ quote: z.ZodString;
65
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
66
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
67
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
68
+ quote: z.ZodString;
69
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
70
+ }, z.core.$strict>>>;
71
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
72
+ kind: z.ZodEnum<{
73
+ contradiction: "contradiction";
74
+ "unused-input": "unused-input";
75
+ "unhandled-case": "unhandled-case";
76
+ "inferred-resolution": "inferred-resolution";
77
+ }>;
78
+ quotes: z.ZodArray<z.ZodString>;
79
+ reason: z.ZodString;
80
+ }, z.core.$strict>>>;
81
+ }, z.core.$strict>;
82
+ }, z.core.$strict>;
83
+ /** A draft state below the root. Declared by hand because zod cannot infer a recursive type. */
84
+ export interface DraftState {
85
+ type?: 'final' | undefined;
86
+ initial?: string | undefined;
87
+ states?: Record<string, DraftState> | undefined;
88
+ on?: Record<string, DraftTransition | DraftTransition[]> | undefined;
89
+ always?: DraftTransition | DraftTransition[] | undefined;
90
+ entry?: DraftAction[] | undefined;
91
+ exit?: DraftAction[] | undefined;
92
+ meta: DraftMeta;
93
+ }
94
+ export declare const draftStateSchema: z.ZodType<DraftState>;
95
+ /**
96
+ * What the model writes: the agent-logic config without offsets or ids. Each element quotes the prompt instead of
97
+ * giving a span, `initial` names a child's key, and a target names a state's key, so keys are unique across the draft.
98
+ */
99
+ export declare const draftSchema: z.ZodObject<{
100
+ initial: z.ZodString;
101
+ states: z.ZodRecord<z.ZodString, z.ZodType<DraftState, unknown, z.core.$ZodTypeInternals<DraftState, unknown>>>;
102
+ meta: z.ZodObject<{
103
+ unconverted: z.ZodArray<z.ZodObject<{
104
+ quote: z.ZodString;
105
+ reason: z.ZodString;
106
+ }, z.core.$strict>>;
107
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
108
+ quote: z.ZodString;
109
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
110
+ }, z.core.$strict>>>;
111
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
112
+ kind: z.ZodEnum<{
113
+ contradiction: "contradiction";
114
+ "unused-input": "unused-input";
115
+ "unhandled-case": "unhandled-case";
116
+ "inferred-resolution": "inferred-resolution";
117
+ }>;
118
+ quotes: z.ZodArray<z.ZodString>;
119
+ reason: z.ZodString;
120
+ }, z.core.$strict>>>;
121
+ }, z.core.$strict>;
122
+ on: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodObject<{
123
+ target: z.ZodOptional<z.ZodString>;
124
+ guard: z.ZodOptional<z.ZodObject<{
125
+ type: z.ZodLiteral<"nl">;
126
+ params: z.ZodObject<{
127
+ text: z.ZodString;
128
+ }, z.core.$strict>;
129
+ }, z.core.$strict>>;
130
+ actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
131
+ type: z.ZodLiteral<"nl">;
132
+ params: z.ZodObject<{
133
+ text: z.ZodString;
134
+ }, z.core.$strict>;
135
+ meta: z.ZodOptional<z.ZodObject<{
136
+ quote: z.ZodString;
137
+ gate: z.ZodLiteral<true>;
138
+ }, z.core.$strict>>;
139
+ }, z.core.$strict>>>;
140
+ meta: z.ZodObject<{
141
+ quote: z.ZodString;
142
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
143
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
144
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
145
+ quote: z.ZodString;
146
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
147
+ }, z.core.$strict>>>;
148
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
149
+ kind: z.ZodEnum<{
150
+ contradiction: "contradiction";
151
+ "unused-input": "unused-input";
152
+ "unhandled-case": "unhandled-case";
153
+ "inferred-resolution": "inferred-resolution";
154
+ }>;
155
+ quotes: z.ZodArray<z.ZodString>;
156
+ reason: z.ZodString;
157
+ }, z.core.$strict>>>;
158
+ }, z.core.$strict>;
159
+ }, z.core.$strict>, z.ZodArray<z.ZodObject<{
160
+ target: z.ZodOptional<z.ZodString>;
161
+ guard: z.ZodOptional<z.ZodObject<{
162
+ type: z.ZodLiteral<"nl">;
163
+ params: z.ZodObject<{
164
+ text: z.ZodString;
165
+ }, z.core.$strict>;
166
+ }, z.core.$strict>>;
167
+ actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
168
+ type: z.ZodLiteral<"nl">;
169
+ params: z.ZodObject<{
170
+ text: z.ZodString;
171
+ }, z.core.$strict>;
172
+ meta: z.ZodOptional<z.ZodObject<{
173
+ quote: z.ZodString;
174
+ gate: z.ZodLiteral<true>;
175
+ }, z.core.$strict>>;
176
+ }, z.core.$strict>>>;
177
+ meta: z.ZodObject<{
178
+ quote: z.ZodString;
179
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
180
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
181
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
182
+ quote: z.ZodString;
183
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
184
+ }, z.core.$strict>>>;
185
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
186
+ kind: z.ZodEnum<{
187
+ contradiction: "contradiction";
188
+ "unused-input": "unused-input";
189
+ "unhandled-case": "unhandled-case";
190
+ "inferred-resolution": "inferred-resolution";
191
+ }>;
192
+ quotes: z.ZodArray<z.ZodString>;
193
+ reason: z.ZodString;
194
+ }, z.core.$strict>>>;
195
+ }, z.core.$strict>;
196
+ }, z.core.$strict>>]>>>;
197
+ always: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
198
+ target: z.ZodOptional<z.ZodString>;
199
+ guard: z.ZodOptional<z.ZodObject<{
200
+ type: z.ZodLiteral<"nl">;
201
+ params: z.ZodObject<{
202
+ text: z.ZodString;
203
+ }, z.core.$strict>;
204
+ }, z.core.$strict>>;
205
+ actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
206
+ type: z.ZodLiteral<"nl">;
207
+ params: z.ZodObject<{
208
+ text: z.ZodString;
209
+ }, z.core.$strict>;
210
+ meta: z.ZodOptional<z.ZodObject<{
211
+ quote: z.ZodString;
212
+ gate: z.ZodLiteral<true>;
213
+ }, z.core.$strict>>;
214
+ }, z.core.$strict>>>;
215
+ meta: z.ZodObject<{
216
+ quote: z.ZodString;
217
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
218
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
219
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
220
+ quote: z.ZodString;
221
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
222
+ }, z.core.$strict>>>;
223
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
224
+ kind: z.ZodEnum<{
225
+ contradiction: "contradiction";
226
+ "unused-input": "unused-input";
227
+ "unhandled-case": "unhandled-case";
228
+ "inferred-resolution": "inferred-resolution";
229
+ }>;
230
+ quotes: z.ZodArray<z.ZodString>;
231
+ reason: z.ZodString;
232
+ }, z.core.$strict>>>;
233
+ }, z.core.$strict>;
234
+ }, z.core.$strict>, z.ZodArray<z.ZodObject<{
235
+ target: z.ZodOptional<z.ZodString>;
236
+ guard: z.ZodOptional<z.ZodObject<{
237
+ type: z.ZodLiteral<"nl">;
238
+ params: z.ZodObject<{
239
+ text: z.ZodString;
240
+ }, z.core.$strict>;
241
+ }, z.core.$strict>>;
242
+ actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
243
+ type: z.ZodLiteral<"nl">;
244
+ params: z.ZodObject<{
245
+ text: z.ZodString;
246
+ }, z.core.$strict>;
247
+ meta: z.ZodOptional<z.ZodObject<{
248
+ quote: z.ZodString;
249
+ gate: z.ZodLiteral<true>;
250
+ }, z.core.$strict>>;
251
+ }, z.core.$strict>>>;
252
+ meta: z.ZodObject<{
253
+ quote: z.ZodString;
254
+ inferred: z.ZodOptional<z.ZodLiteral<true>>;
255
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
256
+ ambient: z.ZodOptional<z.ZodArray<z.ZodObject<{
257
+ quote: z.ZodString;
258
+ gate: z.ZodOptional<z.ZodLiteral<true>>;
259
+ }, z.core.$strict>>>;
260
+ findings: z.ZodOptional<z.ZodArray<z.ZodObject<{
261
+ kind: z.ZodEnum<{
262
+ contradiction: "contradiction";
263
+ "unused-input": "unused-input";
264
+ "unhandled-case": "unhandled-case";
265
+ "inferred-resolution": "inferred-resolution";
266
+ }>;
267
+ quotes: z.ZodArray<z.ZodString>;
268
+ reason: z.ZodString;
269
+ }, z.core.$strict>>>;
270
+ }, z.core.$strict>;
271
+ }, z.core.$strict>>]>>;
272
+ entry: z.ZodOptional<z.ZodArray<z.ZodObject<{
273
+ type: z.ZodLiteral<"nl">;
274
+ params: z.ZodObject<{
275
+ text: z.ZodString;
276
+ }, z.core.$strict>;
277
+ meta: z.ZodOptional<z.ZodObject<{
278
+ quote: z.ZodString;
279
+ gate: z.ZodLiteral<true>;
280
+ }, z.core.$strict>>;
281
+ }, z.core.$strict>>>;
282
+ exit: z.ZodOptional<z.ZodArray<z.ZodObject<{
283
+ type: z.ZodLiteral<"nl">;
284
+ params: z.ZodObject<{
285
+ text: z.ZodString;
286
+ }, z.core.$strict>;
287
+ meta: z.ZodOptional<z.ZodObject<{
288
+ quote: z.ZodString;
289
+ gate: z.ZodLiteral<true>;
290
+ }, z.core.$strict>>;
291
+ }, z.core.$strict>>>;
292
+ }, z.core.$strict>;
293
+ /**
294
+ * What the model writes for a prompt that the representation cannot usefully hold, in place of a draft. The `convert`
295
+ * script writes the same object to the problems file.
296
+ */
297
+ export declare const outOfScopeSchema: z.ZodObject<{
298
+ outOfScope: z.ZodObject<{
299
+ reason: z.ZodString;
300
+ }, z.core.$strict>;
301
+ }, z.core.$strict>;
302
+ export type Draft = z.output<typeof draftSchema>;
303
+ export type DraftAction = z.output<typeof draftActionSchema>;
304
+ export type DraftFinding = NonNullable<z.output<typeof draftFindingsSchema>>[number];
305
+ export type DraftMeta = z.output<typeof draftMetaSchema>;
306
+ export type DraftTransition = z.output<typeof draftTransitionSchema>;
307
+ export type OutOfScope = z.output<typeof outOfScopeSchema>['outOfScope'];
308
+ export {};
@@ -0,0 +1,57 @@
1
+ import { z } from 'zod';
2
+ import { eventNameSchema, findingKindSchema, nlEntrySchema, transitionSchema } from "../schema/agent-logic-schema.js";
3
+ const draftAmbientSchema = z
4
+ .array(z.strictObject({ quote: z.string().min(1), gate: z.literal(true).optional() }))
5
+ .optional();
6
+ const draftFindingsSchema = z
7
+ .array(z.strictObject({
8
+ kind: findingKindSchema,
9
+ quotes: z.array(z.string().min(1)).min(1),
10
+ reason: z.string().min(1),
11
+ }))
12
+ .optional();
13
+ const draftMetaSchema = z.strictObject({
14
+ quote: z.string().min(1),
15
+ inferred: z.literal(true).optional(),
16
+ gate: z.literal(true).optional(),
17
+ ambient: draftAmbientSchema,
18
+ findings: draftFindingsSchema,
19
+ });
20
+ const draftActionSchema = nlEntrySchema.extend({
21
+ meta: z.strictObject({ quote: z.string().min(1), gate: z.literal(true) }).optional(),
22
+ });
23
+ const draftTransitionSchema = transitionSchema.extend({
24
+ actions: z.array(draftActionSchema).optional(),
25
+ meta: draftMetaSchema,
26
+ });
27
+ const draftTransitionsSchema = z.union([draftTransitionSchema, z.array(draftTransitionSchema).min(1)]);
28
+ const draftStateMembers = {
29
+ on: z.record(eventNameSchema, draftTransitionsSchema).optional(),
30
+ always: draftTransitionsSchema.optional(),
31
+ entry: z.array(draftActionSchema).optional(),
32
+ exit: z.array(draftActionSchema).optional(),
33
+ };
34
+ export const draftStateSchema = z.strictObject({
35
+ ...draftStateMembers,
36
+ type: z.literal('final').optional(),
37
+ initial: z.string().optional(),
38
+ get states() {
39
+ return z.record(z.string(), draftStateSchema).optional();
40
+ },
41
+ meta: draftMetaSchema,
42
+ });
43
+ export const draftSchema = z.strictObject({
44
+ ...draftStateMembers,
45
+ initial: z.string(),
46
+ states: z.record(z.string(), draftStateSchema),
47
+ meta: z.strictObject({
48
+ unconverted: z.array(z.strictObject({ quote: z.string().min(1), reason: z.string().min(1) })),
49
+ ambient: draftAmbientSchema,
50
+ findings: draftFindingsSchema,
51
+ }),
52
+ });
53
+ export const outOfScopeSchema = z.strictObject({
54
+ outOfScope: z.strictObject({
55
+ reason: z.string().refine((value) => value.trim() !== '', 'State why the prompt is out of scope'),
56
+ }),
57
+ });
@@ -0,0 +1,6 @@
1
+ import type { Span } from '../schema/agent-logic-schema.js';
2
+ /**
3
+ * Returns the span of each `<HARD-GATE>` body in the prompt, trimmed of surrounding whitespace, in order. An opening
4
+ * tag without a closing tag has no body, and a tag inside an HTML comment is markup rather than a gate.
5
+ */
6
+ export declare function findGateSpans(prompt: string): Span[];
@@ -0,0 +1,18 @@
1
+ import { findCommentSpans } from "../source/find-comment-spans.js";
2
+ const GATE_PATTERN = /<HARD-GATE>([\s\S]*?)<\/HARD-GATE>/g;
3
+ export function findGateSpans(prompt) {
4
+ const comments = findCommentSpans(prompt);
5
+ const spans = [];
6
+ for (const match of prompt.matchAll(GATE_PATTERN)) {
7
+ const [tag, body = ''] = match;
8
+ if (comments.some(({ start, end }) => start <= match.index && match.index < end))
9
+ continue;
10
+ const bodyStart = match.index + tag.indexOf(body);
11
+ const leading = body.length - body.trimStart().length;
12
+ const trimmed = body.trim();
13
+ if (trimmed === '')
14
+ continue;
15
+ spans.push({ start: bodyStart + leading, end: bodyStart + leading + trimmed.length });
16
+ }
17
+ return spans;
18
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Turns a draft key into a state's name: each run of `_`, `-`, or whitespace becomes one space, and the first character
3
+ * is uppercased, so that `open_the_PR` becomes `Open the PR`. Returns undefined for a key that contains no other
4
+ * character.
5
+ */
6
+ export declare function formatStateName(key: string): string | undefined;
@@ -0,0 +1,6 @@
1
+ export function formatStateName(key) {
2
+ const words = key.replaceAll(/[\s_-]+/g, ' ').trim();
3
+ if (words === '')
4
+ return undefined;
5
+ return `${words.charAt(0).toUpperCase()}${words.slice(1)}`;
6
+ }
@@ -0,0 +1,11 @@
1
+ import type { AgentLogicConfig } from '../schema/agent-logic-schema.js';
2
+ /**
3
+ * Rewrites a config into its canonical form, so that two configs describing the same logic serialize identically.
4
+ * Each state's key and id derive from the first words of its span text, `initial` and every target follow the new
5
+ * ids, and states, events, unconverted passages, ambient passages, findings, and object members take a fixed order.
6
+ * The order of transitions within one event or `always` list, and of actions, is kept, because XState evaluates them
7
+ * in order. The root keeps its id.
8
+ */
9
+ export declare function normalizeConfig(config: AgentLogicConfig): AgentLogicConfig;
10
+ /** Derives a state id from its span text: the first words, lowercase ASCII letters and digits joined by `_`. */
11
+ export declare function slugifyStateText(text: string): string;