@mnaimfaizy/agent-kit 1.0.0-beta.1 → 1.0.0-beta.10

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.
@@ -1,7 +1,7 @@
1
1
  // Reads a reusable workflow's `on.workflow_call.secrets` block: which secrets
2
2
  // it declares and which of them are required.
3
3
 
4
- import { parseYamlSubset } from "./yaml-subset.mjs";
4
+ import { isMapping, parseYamlSubset } from "./yaml-subset.mjs";
5
5
 
6
6
  /** @returns {Map<string, { required: boolean }> | null} null when the file is not a reusable workflow. */
7
7
  export function parseWorkflowCallSecrets(text) {
@@ -9,8 +9,7 @@ export function parseWorkflowCallSecrets(text) {
9
9
  const on = doc.on ?? doc["'on'"] ?? doc['"on"'];
10
10
  if (!on || typeof on !== "object" || !("workflow_call" in on)) return null;
11
11
  const call = on.workflow_call && typeof on.workflow_call === "object" ? on.workflow_call : {};
12
- const secrets =
13
- call.secrets && typeof call.secrets === "object" && !call.secrets.$seq ? call.secrets : {};
12
+ const secrets = isMapping(call.secrets) ? call.secrets : {};
14
13
  const out = new Map();
15
14
  for (const [name, spec] of Object.entries(secrets)) {
16
15
  const required =
@@ -30,8 +29,7 @@ export function parseWorkflowCallInputs(text) {
30
29
  const on = doc.on ?? doc["'on'"] ?? doc['"on"'];
31
30
  if (!on || typeof on !== "object" || !("workflow_call" in on)) return null;
32
31
  const call = on.workflow_call && typeof on.workflow_call === "object" ? on.workflow_call : {};
33
- const inputs =
34
- call.inputs && typeof call.inputs === "object" && !call.inputs.$seq ? call.inputs : {};
32
+ const inputs = isMapping(call.inputs) ? call.inputs : {};
35
33
  const out = new Map();
36
34
  for (const [name, spec] of Object.entries(inputs)) {
37
35
  const obj = spec && typeof spec === "object" ? spec : {};
@@ -1,9 +1,9 @@
1
- // A small, dependency-free reader for the YAML that workflow files use: block
2
- // mappings, plain and quoted scalars, block scalars (| and >), and sequences.
3
- // It returns nested plain objects. Sequences come back as { $seq: rawLines }
4
- // and block scalars as strings; flow collections ([a, b], {a: b}) stay as
5
- // their raw text. That is enough to read a Caller's jobs and a reusable
6
- // workflow's `on.workflow_call.secrets`, which is all the CLI needs.
1
+ // A small, dependency-free reader for the YAML that workflow files and Scan
2
+ // recipes use: block mappings, block sequences, plain and quoted scalars, and
3
+ // block scalars (| and >). Mappings come back as plain objects, sequences as
4
+ // arrays (of scalars, mappings or sequences), and block scalars as strings;
5
+ // flow collections ([a, b], {a: b}) stay as their raw text. Anchors, aliases,
6
+ // merge keys and tags are not resolved.
7
7
 
8
8
  const BOM = String.fromCharCode(0xfeff);
9
9
 
@@ -13,6 +13,11 @@ export function stripBom(text) {
13
13
  return s.startsWith(BOM) ? s.slice(1) : s;
14
14
  }
15
15
 
16
+ /** True for a block mapping: a plain object, not a list, a string or null. */
17
+ export function isMapping(value) {
18
+ return value !== null && typeof value === "object" && !Array.isArray(value);
19
+ }
20
+
16
21
  /** Remove a trailing `# comment` that sits outside quotes. */
17
22
  export function stripComment(line) {
18
23
  let quote = null;
@@ -41,10 +46,22 @@ export function parseScalar(raw) {
41
46
 
42
47
  const KEY = /^("[^"]*"|'[^']*'|[^\s"'#:-][^:]*?|-[^\s:][^:]*?):(?:\s+(.*))?$/;
43
48
 
44
- export function parseYamlSubset(text) {
49
+ /**
50
+ * Parse `text`. `problems`, when given, collects a line the reader would
51
+ * otherwise drop without a word: one in a list that is not a `- ` item,
52
+ * which ends the list early.
53
+ * @param {string} text
54
+ * @param {{ problems?: string[] }} [opts]
55
+ */
56
+ export function parseYamlSubset(text, { problems } = {}) {
45
57
  const lines = stripBom(text).replace(/\r\n?/g, "\n").split("\n");
46
58
  const indentOf = (l) => l.length - l.trimStart().length;
47
59
  const isBlank = (l) => stripComment(l).trim() === "";
60
+ // A list item's marker: `-` alone or followed by a space, never `-foo:`.
61
+ const isItem = (l) => {
62
+ const c = stripComment(l).trim();
63
+ return c === "-" || c.startsWith("- ");
64
+ };
48
65
 
49
66
  function nextContent(i) {
50
67
  while (i < lines.length && isBlank(lines[i])) i++;
@@ -76,23 +93,10 @@ export function parseYamlSubset(text) {
76
93
  const value = (m[2] ?? "").trim();
77
94
  if (value === "") {
78
95
  const j = nextContent(i);
79
- if (
80
- j < lines.length &&
81
- stripComment(lines[j]).trimStart().startsWith("-") &&
82
- indentOf(lines[j]) >= childIndent
83
- ) {
84
- const raw = [];
85
- while (i < lines.length) {
86
- const l = lines[i];
87
- if (!isBlank(l)) {
88
- const li = indentOf(l);
89
- const starts = l.trimStart().startsWith("-");
90
- if (li < childIndent || (li === childIndent && !starts)) break;
91
- }
92
- raw.push(l);
93
- i++;
94
- }
95
- obj[key] = { $seq: raw };
96
+ if (j < lines.length && isItem(lines[j]) && indentOf(lines[j]) >= childIndent) {
97
+ const [seq, next] = parseSequence(j, indentOf(lines[j]), childIndent);
98
+ obj[key] = seq;
99
+ i = next;
96
100
  } else if (j < lines.length && indentOf(lines[j]) > childIndent) {
97
101
  const [sub, next] = parseMapping(j, childIndent);
98
102
  obj[key] = sub;
@@ -114,5 +118,111 @@ export function parseYamlSubset(text) {
114
118
  return [obj, i];
115
119
  }
116
120
 
121
+ // A block sequence whose `-` markers sit at `seqIndent`, inside a block
122
+ // whose own lines sit at `ownerIndent`.
123
+ function parseSequence(start, seqIndent, ownerIndent) {
124
+ const list = [];
125
+ let i = start;
126
+ while (i < lines.length) {
127
+ if (isBlank(lines[i])) {
128
+ i++;
129
+ continue;
130
+ }
131
+ const ind = indentOf(lines[i]);
132
+ if (ind < seqIndent) {
133
+ // Shallower than the list's items but deeper than the block that
134
+ // holds it: no reader takes it, so it and the rest are dropped.
135
+ if (ind > ownerIndent) {
136
+ problems?.push(
137
+ `line ${i + 1}: is indented less than the list above it, but more than its key; the reader would drop it and the rest of the list.`,
138
+ );
139
+ }
140
+ break;
141
+ }
142
+ const content = stripComment(lines[i].slice(ind)).trimEnd();
143
+ if (ind > seqIndent) {
144
+ // Deeper than an item, but nothing above it took it (a scalar item
145
+ // stops at a blank line): the reader would drop it.
146
+ const afterBlank = i > 0 && isBlank(lines[i - 1]);
147
+ problems?.push(
148
+ `line ${i + 1}: sits deeper than the list item above it${afterBlank ? ", after a blank line" : ""}, and no item takes it; the reader would drop it.`,
149
+ );
150
+ i++;
151
+ continue;
152
+ }
153
+ if (!isItem(content)) {
154
+ // Deeper than the block that holds the list, so nothing else can
155
+ // read it: the reader would drop it, and the rest of the list.
156
+ if (seqIndent > ownerIndent) {
157
+ problems?.push(
158
+ `line ${i + 1}: sits in a list but is not a \`- \` item; the reader would drop it and the rest of the list.`,
159
+ );
160
+ }
161
+ break;
162
+ }
163
+ const rest = content.slice(1).trimStart();
164
+ if (rest === "") {
165
+ // A bare `-`: the item is the deeper block that follows, if any.
166
+ const j = nextContent(i + 1);
167
+ if (j < lines.length && indentOf(lines[j]) > seqIndent) {
168
+ const nested = stripComment(lines[j]).trim();
169
+ if (isItem(nested)) {
170
+ const [item, next] = parseSequence(j, indentOf(lines[j]), seqIndent);
171
+ list.push(item);
172
+ i = next;
173
+ } else if (KEY.test(nested) && !/^[[{]/.test(nested)) {
174
+ const [item, next] = parseMapping(j, seqIndent);
175
+ list.push(item);
176
+ i = next;
177
+ } else {
178
+ const [item, next] = foldScalar(nested, j + 1, seqIndent);
179
+ list.push(item);
180
+ i = next;
181
+ }
182
+ } else {
183
+ list.push(null);
184
+ i++;
185
+ }
186
+ continue;
187
+ }
188
+ if (!/^[[{]/.test(rest) && KEY.test(rest)) {
189
+ // `- key: value`: the item is a mapping whose first key sits where
190
+ // `rest` starts. Re-indent that line so the mapping reader sees the
191
+ // key column it shares with the item's later keys.
192
+ lines[i] = " ".repeat(ind + content.length - rest.length) + rest;
193
+ const [item, next] = parseMapping(i, seqIndent);
194
+ list.push(item);
195
+ i = next;
196
+ continue;
197
+ }
198
+ i++;
199
+ if (/^[|>][-+0-9]*$/.test(rest)) {
200
+ const raw = [];
201
+ while (i < lines.length && (lines[i].trim() === "" || indentOf(lines[i]) > seqIndent)) {
202
+ raw.push(lines[i]);
203
+ i++;
204
+ }
205
+ list.push(raw.join("\n"));
206
+ } else {
207
+ const [item, next] = foldScalar(rest, i, seqIndent);
208
+ list.push(item);
209
+ i = next;
210
+ }
211
+ }
212
+ return [list, i];
213
+ }
214
+
215
+ // A scalar item whose text starts with `first` and runs on over the
216
+ // non-blank lines from `i` that sit deeper than the item's `-`. The lines
217
+ // fold into one, joined by spaces.
218
+ function foldScalar(first, i, seqIndent) {
219
+ const parts = [first];
220
+ while (i < lines.length && !isBlank(lines[i]) && indentOf(lines[i]) > seqIndent) {
221
+ parts.push(stripComment(lines[i]).trim());
222
+ i++;
223
+ }
224
+ return [parseScalar(parts.join(" ")), i];
225
+ }
226
+
117
227
  return parseMapping(0, -1)[0];
118
228
  }