@namzu/sdk 50.1.0 → 51.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 (161) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/bridge/sse/mapper.d.ts.map +1 -1
  3. package/dist/bridge/sse/mapper.js +1 -0
  4. package/dist/bridge/sse/mapper.js.map +1 -1
  5. package/dist/execution/code-runtime/types.d.ts +35 -0
  6. package/dist/execution/code-runtime/types.d.ts.map +1 -1
  7. package/dist/execution/code-runtime/types.js.map +1 -1
  8. package/dist/execution/code-runtime/worker-program.d.ts +1 -1
  9. package/dist/execution/code-runtime/worker-program.d.ts.map +1 -1
  10. package/dist/execution/code-runtime/worker-program.js +72 -13
  11. package/dist/execution/code-runtime/worker-program.js.map +1 -1
  12. package/dist/execution/code-runtime/worker.d.ts.map +1 -1
  13. package/dist/execution/code-runtime/worker.js +35 -1
  14. package/dist/execution/code-runtime/worker.js.map +1 -1
  15. package/dist/execution/workflow/errors.d.ts +5 -0
  16. package/dist/execution/workflow/errors.d.ts.map +1 -0
  17. package/dist/execution/workflow/errors.js +8 -0
  18. package/dist/execution/workflow/errors.js.map +1 -0
  19. package/dist/execution/workflow/index.d.ts +9 -0
  20. package/dist/execution/workflow/index.d.ts.map +1 -0
  21. package/dist/execution/workflow/index.js +6 -0
  22. package/dist/execution/workflow/index.js.map +1 -0
  23. package/dist/execution/workflow/journal.d.ts +18 -0
  24. package/dist/execution/workflow/journal.d.ts.map +1 -0
  25. package/dist/execution/workflow/journal.js +43 -0
  26. package/dist/execution/workflow/journal.js.map +1 -0
  27. package/dist/execution/workflow/meta.d.ts +14 -0
  28. package/dist/execution/workflow/meta.d.ts.map +1 -0
  29. package/dist/execution/workflow/meta.js +314 -0
  30. package/dist/execution/workflow/meta.js.map +1 -0
  31. package/dist/execution/workflow/phases.d.ts +32 -0
  32. package/dist/execution/workflow/phases.d.ts.map +1 -0
  33. package/dist/execution/workflow/phases.js +57 -0
  34. package/dist/execution/workflow/phases.js.map +1 -0
  35. package/dist/execution/workflow/prelude.d.ts +15 -0
  36. package/dist/execution/workflow/prelude.d.ts.map +1 -0
  37. package/dist/execution/workflow/prelude.js +128 -0
  38. package/dist/execution/workflow/prelude.js.map +1 -0
  39. package/dist/execution/workflow/runtime.d.ts +31 -0
  40. package/dist/execution/workflow/runtime.d.ts.map +1 -0
  41. package/dist/execution/workflow/runtime.js +682 -0
  42. package/dist/execution/workflow/runtime.js.map +1 -0
  43. package/dist/execution/workflow/schema.d.ts +10 -0
  44. package/dist/execution/workflow/schema.d.ts.map +1 -0
  45. package/dist/execution/workflow/schema.js +145 -0
  46. package/dist/execution/workflow/schema.js.map +1 -0
  47. package/dist/execution/workflow/types.d.ts +267 -0
  48. package/dist/execution/workflow/types.d.ts.map +1 -0
  49. package/dist/execution/workflow/types.js +7 -0
  50. package/dist/execution/workflow/types.js.map +1 -0
  51. package/dist/index.d.ts +1 -0
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +1 -0
  54. package/dist/index.js.map +1 -1
  55. package/dist/plugin/shell-hook.d.ts.map +1 -1
  56. package/dist/plugin/shell-hook.js +2 -1
  57. package/dist/plugin/shell-hook.js.map +1 -1
  58. package/dist/process/kill-tree.d.ts +7 -0
  59. package/dist/process/kill-tree.d.ts.map +1 -1
  60. package/dist/process/kill-tree.js +41 -0
  61. package/dist/process/kill-tree.js.map +1 -1
  62. package/dist/provider/errors.d.ts +10 -0
  63. package/dist/provider/errors.d.ts.map +1 -1
  64. package/dist/provider/errors.js +16 -0
  65. package/dist/provider/errors.js.map +1 -1
  66. package/dist/provider/retry.d.ts +27 -0
  67. package/dist/provider/retry.d.ts.map +1 -1
  68. package/dist/provider/retry.js +19 -3
  69. package/dist/provider/retry.js.map +1 -1
  70. package/dist/provider/token-budget.d.ts.map +1 -1
  71. package/dist/provider/token-budget.js +31 -2
  72. package/dist/provider/token-budget.js.map +1 -1
  73. package/dist/public-runtime.d.ts +1 -0
  74. package/dist/public-runtime.d.ts.map +1 -1
  75. package/dist/public-runtime.js +3 -0
  76. package/dist/public-runtime.js.map +1 -1
  77. package/dist/public-tools.d.ts +1 -1
  78. package/dist/public-tools.d.ts.map +1 -1
  79. package/dist/public-tools.js +1 -1
  80. package/dist/public-tools.js.map +1 -1
  81. package/dist/public-types.d.ts +3 -0
  82. package/dist/public-types.d.ts.map +1 -1
  83. package/dist/runtime/jobs/job-id.d.ts +21 -0
  84. package/dist/runtime/jobs/job-id.d.ts.map +1 -0
  85. package/dist/runtime/jobs/job-id.js +66 -0
  86. package/dist/runtime/jobs/job-id.js.map +1 -0
  87. package/dist/runtime/jobs/registry.d.ts +0 -1
  88. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  89. package/dist/runtime/jobs/registry.js +2 -3
  90. package/dist/runtime/jobs/registry.js.map +1 -1
  91. package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
  92. package/dist/runtime/query/iteration/stream-turn.js +24 -1
  93. package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
  94. package/dist/runtime/query/prompt.d.ts.map +1 -1
  95. package/dist/runtime/query/prompt.js +5 -1
  96. package/dist/runtime/query/prompt.js.map +1 -1
  97. package/dist/tools/builtins/bash.d.ts +8 -1
  98. package/dist/tools/builtins/bash.d.ts.map +1 -1
  99. package/dist/tools/builtins/bash.js +20 -3
  100. package/dist/tools/builtins/bash.js.map +1 -1
  101. package/dist/tools/builtins/job.d.ts.map +1 -1
  102. package/dist/tools/builtins/job.js +4 -0
  103. package/dist/tools/builtins/job.js.map +1 -1
  104. package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
  105. package/dist/tools/builtins/wait-for-job.js +4 -0
  106. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  107. package/dist/tools/command-shell.d.ts +55 -0
  108. package/dist/tools/command-shell.d.ts.map +1 -1
  109. package/dist/tools/command-shell.js +174 -13
  110. package/dist/tools/command-shell.js.map +1 -1
  111. package/dist/types/provider/error.d.ts +6 -0
  112. package/dist/types/provider/error.d.ts.map +1 -1
  113. package/dist/types/provider/errors.d.ts.map +1 -1
  114. package/dist/types/provider/errors.js +15 -1
  115. package/dist/types/provider/errors.js.map +1 -1
  116. package/dist/types/provider/stream.d.ts +6 -0
  117. package/dist/types/provider/stream.d.ts.map +1 -1
  118. package/dist/types/session/events.d.ts +5 -0
  119. package/dist/types/session/events.d.ts.map +1 -1
  120. package/dist/types/session/events.js.map +1 -1
  121. package/dist/utils/backoff.d.ts +12 -0
  122. package/dist/utils/backoff.d.ts.map +1 -1
  123. package/dist/utils/backoff.js +15 -0
  124. package/dist/utils/backoff.js.map +1 -1
  125. package/package.json +1 -1
  126. package/src/agents/AGENTS.md +134 -46
  127. package/src/bridge/sse/mapper.ts +1 -0
  128. package/src/execution/code-runtime/types.ts +37 -1
  129. package/src/execution/code-runtime/worker-program.ts +72 -13
  130. package/src/execution/code-runtime/worker.ts +40 -1
  131. package/src/execution/workflow/errors.ts +7 -0
  132. package/src/execution/workflow/index.ts +8 -0
  133. package/src/execution/workflow/journal.ts +46 -0
  134. package/src/execution/workflow/meta.ts +316 -0
  135. package/src/execution/workflow/phases.ts +91 -0
  136. package/src/execution/workflow/prelude.ts +135 -0
  137. package/src/execution/workflow/runtime.ts +786 -0
  138. package/src/execution/workflow/schema.ts +137 -0
  139. package/src/execution/workflow/types.ts +300 -0
  140. package/src/index.ts +4 -0
  141. package/src/plugin/shell-hook.ts +2 -1
  142. package/src/process/kill-tree.ts +43 -0
  143. package/src/provider/errors.ts +18 -0
  144. package/src/provider/retry.ts +53 -3
  145. package/src/provider/token-budget.ts +31 -1
  146. package/src/public-runtime.ts +11 -0
  147. package/src/public-tools.ts +6 -0
  148. package/src/public-types.ts +3 -0
  149. package/src/runtime/jobs/job-id.ts +66 -0
  150. package/src/runtime/jobs/registry.ts +2 -3
  151. package/src/runtime/query/iteration/stream-turn.ts +24 -1
  152. package/src/runtime/query/prompt.ts +5 -1
  153. package/src/tools/builtins/bash.ts +33 -3
  154. package/src/tools/builtins/job.ts +4 -0
  155. package/src/tools/builtins/wait-for-job.ts +4 -0
  156. package/src/tools/command-shell.ts +194 -11
  157. package/src/types/provider/error.ts +6 -0
  158. package/src/types/provider/errors.ts +16 -1
  159. package/src/types/provider/stream.ts +6 -0
  160. package/src/types/session/events.ts +5 -0
  161. package/src/utils/backoff.ts +21 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"journal.d.ts","sourceRoot":"","sources":["../../../src/execution/workflow/journal.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAExE,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE3C;AAED,oFAAoF;AACpF,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAM,GAAG,MAAM,CAMvD;AAED;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,eAAe;IAG9C,OAAO,CAAC,QAAQ,CAAC,GAAG;IAFhC,OAAO,CAAC,KAAK,CAA2B;gBAEX,GAAG,EAAE,MAAM;IAExC,OAAO,CAAC,MAAM;IAKR,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK1C,MAAM,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;CAM1D"}
@@ -0,0 +1,43 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { appendFile, mkdir, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ export function sha256(text) {
5
+ return createHash('sha256').update(text).digest('hex');
6
+ }
7
+ /** The first `max` UTF-16 units of `text`, never ending inside a surrogate pair. */
8
+ export function preview(text, max = 500) {
9
+ if (text.length <= max)
10
+ return text;
11
+ let end = max;
12
+ const last = text.charCodeAt(end - 1);
13
+ if (last >= 0xd800 && last <= 0xdbff)
14
+ end--;
15
+ return `${text.slice(0, end)}…`;
16
+ }
17
+ /**
18
+ * A journal in a directory: `script.js` (the exact script text, mode 0600)
19
+ * and `journal.jsonl` (one JSON record per line, append-only). The caller
20
+ * chooses the directory, normally `<session>/runs/<runId>/workflow/`.
21
+ */
22
+ export class FileWorkflowJournal {
23
+ dir;
24
+ ready;
25
+ constructor(dir) {
26
+ this.dir = dir;
27
+ }
28
+ ensure() {
29
+ this.ready ??= mkdir(this.dir, { recursive: true, mode: 0o700 }).then(() => undefined);
30
+ return this.ready;
31
+ }
32
+ async writeScript(source) {
33
+ await this.ensure();
34
+ await writeFile(join(this.dir, 'script.js'), source, { mode: 0o600 });
35
+ }
36
+ async append(record) {
37
+ await this.ensure();
38
+ await appendFile(join(this.dir, 'journal.jsonl'), `${JSON.stringify(record)}\n`, {
39
+ mode: 0o600,
40
+ });
41
+ }
42
+ }
43
+ //# sourceMappingURL=journal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"journal.js","sourceRoot":"","sources":["../../../src/execution/workflow/journal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAIhC,MAAM,UAAU,MAAM,CAAC,IAAY;IAClC,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AACvD,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,GAAG,GAAG,GAAG;IAC9C,IAAI,IAAI,CAAC,MAAM,IAAI,GAAG;QAAE,OAAO,IAAI,CAAA;IACnC,IAAI,GAAG,GAAG,GAAG,CAAA;IACb,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,CAAA;IACrC,IAAI,IAAI,IAAI,MAAM,IAAI,IAAI,IAAI,MAAM;QAAE,GAAG,EAAE,CAAA;IAC3C,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,CAAA;AAChC,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,mBAAmB;IAGF;IAFrB,KAAK,CAA2B;IAExC,YAA6B,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;IAAG,CAAC;IAEpC,MAAM;QACb,IAAI,CAAC,KAAK,KAAK,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QACtF,OAAO,IAAI,CAAC,KAAK,CAAA;IAClB,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,MAAc;QAC/B,MAAM,IAAI,CAAC,MAAM,EAAE,CAAA;QACnB,MAAM,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;IACtE,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,MAA6B;QACzC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAA;QACnB,MAAM,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,eAAe,CAAC,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE;YAChF,IAAI,EAAE,KAAK;SACX,CAAC,CAAA;IACH,CAAC;CACD"}
@@ -0,0 +1,14 @@
1
+ import type { WorkflowMeta } from './types.js';
2
+ export declare function normalizePhaseTitle(value: string): string;
3
+ /**
4
+ * Split a script into its `meta` and its body. The body keeps the script's
5
+ * line breaks (the meta statement is blanked, not removed) so an error line
6
+ * still points at the author's text.
7
+ */
8
+ export declare function splitWorkflowScript(source: string): {
9
+ meta: WorkflowMeta;
10
+ body: string;
11
+ };
12
+ /** Parse `meta` from a script without executing it. Throws {@link WorkflowScriptError}. */
13
+ export declare function parseWorkflowMeta(source: string): WorkflowMeta;
14
+ //# sourceMappingURL=meta.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../../src/execution/workflow/meta.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAqB,MAAM,YAAY,CAAA;AA0OjE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEzD;AA6CD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAuBxF;AAED,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAE9D"}
@@ -0,0 +1,314 @@
1
+ import { WorkflowScriptError } from './errors.js';
2
+ const MAX_META_BYTES = 16 * 1024;
3
+ const MAX_DEPTH = 8;
4
+ const MAX_PHASES = 50;
5
+ const KEYWORD = /export\s+const\s+meta\s*=/y;
6
+ /**
7
+ * Reads the `export const meta = {...}` literal WITHOUT running the script.
8
+ *
9
+ * The statement must be the first one in the file (comments and blank lines
10
+ * before it are fine) and its value must be a pure literal: strings, numbers,
11
+ * booleans, null, arrays and objects. Variables, calls, spreads, template
12
+ * substitutions and computed keys are refused with a plain message, because
13
+ * the operator reviews this literal BEFORE the script is allowed to run.
14
+ */
15
+ class LiteralParser {
16
+ source;
17
+ pos = 0;
18
+ constructor(source) {
19
+ this.source = source;
20
+ }
21
+ fail(what) {
22
+ const before = this.source.slice(0, this.pos);
23
+ const line = before.split('\n').length;
24
+ const column = this.pos - before.lastIndexOf('\n');
25
+ throw new WorkflowScriptError(`${what} (line ${line}, column ${column}).`);
26
+ }
27
+ skipTrivia() {
28
+ const s = this.source;
29
+ for (;;) {
30
+ const c = s[this.pos];
31
+ if (c === ' ' || c === '\t' || c === '\n' || c === '\r' || c === '')
32
+ this.pos++;
33
+ else if (c === '/' && s[this.pos + 1] === '/') {
34
+ while (this.pos < s.length && s[this.pos] !== '\n')
35
+ this.pos++;
36
+ }
37
+ else if (c === '/' && s[this.pos + 1] === '*') {
38
+ const end = s.indexOf('*/', this.pos + 2);
39
+ if (end === -1)
40
+ this.fail('Unterminated comment');
41
+ this.pos = end + 2;
42
+ }
43
+ else
44
+ return;
45
+ }
46
+ }
47
+ describe() {
48
+ const snippet = this.source.slice(this.pos, this.pos + 24).split('\n')[0];
49
+ return snippet ? `"${snippet}"` : 'the end of the script';
50
+ }
51
+ notLiteral() {
52
+ return this.fail(`\`meta\` must be a pure literal (no variables, calls, spreads or expressions); found ${this.describe()}`);
53
+ }
54
+ value(depth) {
55
+ if (depth > MAX_DEPTH)
56
+ this.fail('`meta` is nested too deeply');
57
+ this.skipTrivia();
58
+ const c = this.source[this.pos];
59
+ if (c === '{')
60
+ return this.object(depth);
61
+ if (c === '[')
62
+ return this.array(depth);
63
+ if (c === '"' || c === "'")
64
+ return this.string(c);
65
+ if (c === '`')
66
+ return this.string('`');
67
+ if (c === '-' || c === '.' || (c !== undefined && c >= '0' && c <= '9'))
68
+ return this.number();
69
+ for (const [word, value] of [
70
+ ['true', true],
71
+ ['false', false],
72
+ ['null', null],
73
+ ]) {
74
+ if (this.source.startsWith(word, this.pos) &&
75
+ !/[\w$]/.test(this.source[this.pos + word.length] ?? '')) {
76
+ this.pos += word.length;
77
+ return value;
78
+ }
79
+ }
80
+ return this.notLiteral();
81
+ }
82
+ number() {
83
+ const match = /-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/y;
84
+ match.lastIndex = this.pos;
85
+ const found = match.exec(this.source);
86
+ if (!found)
87
+ return this.notLiteral();
88
+ this.pos += found[0].length;
89
+ if (/[\w$]/.test(this.source[this.pos] ?? ''))
90
+ return this.notLiteral();
91
+ return Number(found[0]);
92
+ }
93
+ string(quote) {
94
+ const s = this.source;
95
+ this.pos++;
96
+ let out = '';
97
+ for (;;) {
98
+ const c = s[this.pos];
99
+ if (c === undefined)
100
+ return this.fail('Unterminated string in `meta`');
101
+ if (c === quote) {
102
+ this.pos++;
103
+ return out;
104
+ }
105
+ if (c === '\n' && quote !== '`')
106
+ return this.fail('Unterminated string in `meta`');
107
+ if (quote === '`' && c === '$' && s[this.pos + 1] === '{') {
108
+ return this.fail('`meta` strings cannot use `${...}` substitutions');
109
+ }
110
+ if (c !== '\\') {
111
+ out += c;
112
+ this.pos++;
113
+ continue;
114
+ }
115
+ const e = s[this.pos + 1];
116
+ this.pos += 2;
117
+ const simple = {
118
+ n: '\n',
119
+ t: '\t',
120
+ r: '\r',
121
+ b: '\b',
122
+ f: '\f',
123
+ v: '\v',
124
+ '0': '\0',
125
+ '\\': '\\',
126
+ "'": "'",
127
+ '"': '"',
128
+ '`': '`',
129
+ $: '$',
130
+ };
131
+ if (e !== undefined && e in simple)
132
+ out += simple[e];
133
+ else if (e === '\n')
134
+ continue;
135
+ else if (e === 'u' && s[this.pos] === '{') {
136
+ const end = s.indexOf('}', this.pos);
137
+ const hex = s.slice(this.pos + 1, end);
138
+ if (end === -1 || !/^[0-9a-fA-F]{1,6}$/.test(hex) || Number.parseInt(hex, 16) > 0x10ffff) {
139
+ return this.fail('Bad escape in a `meta` string');
140
+ }
141
+ out += String.fromCodePoint(Number.parseInt(hex, 16));
142
+ this.pos = end + 1;
143
+ }
144
+ else if (e === 'u' || e === 'x') {
145
+ const len = e === 'u' ? 4 : 2;
146
+ const hex = s.slice(this.pos, this.pos + len);
147
+ if (!new RegExp(`^[0-9a-fA-F]{${len}}$`).test(hex))
148
+ return this.fail('Bad escape in a `meta` string');
149
+ out += String.fromCharCode(Number.parseInt(hex, 16));
150
+ this.pos += len;
151
+ }
152
+ else
153
+ return this.fail('Bad escape in a `meta` string');
154
+ }
155
+ }
156
+ array(depth) {
157
+ this.pos++;
158
+ const out = [];
159
+ for (;;) {
160
+ this.skipTrivia();
161
+ if (this.source[this.pos] === ']') {
162
+ this.pos++;
163
+ return out;
164
+ }
165
+ out.push(this.value(depth + 1));
166
+ this.skipTrivia();
167
+ if (this.source[this.pos] === ',')
168
+ this.pos++;
169
+ else if (this.source[this.pos] !== ']')
170
+ return this.notLiteral();
171
+ }
172
+ }
173
+ object(depth) {
174
+ this.pos++;
175
+ const out = Object.create(null);
176
+ for (;;) {
177
+ this.skipTrivia();
178
+ const c = this.source[this.pos];
179
+ if (c === '}') {
180
+ this.pos++;
181
+ return out;
182
+ }
183
+ let key;
184
+ if (c === '"' || c === "'")
185
+ key = this.string(c);
186
+ else {
187
+ const ident = /[A-Za-z_$][\w$]*/y;
188
+ ident.lastIndex = this.pos;
189
+ const found = ident.exec(this.source);
190
+ if (!found)
191
+ return this.notLiteral();
192
+ key = found[0];
193
+ this.pos += key.length;
194
+ }
195
+ if (key === '__proto__')
196
+ this.fail('`meta` cannot define `__proto__`');
197
+ if (key in out)
198
+ this.fail(`\`meta\` repeats the key "${key}"`);
199
+ this.skipTrivia();
200
+ if (this.source[this.pos] !== ':')
201
+ return this.notLiteral();
202
+ this.pos++;
203
+ out[key] = this.value(depth + 1);
204
+ this.skipTrivia();
205
+ if (this.source[this.pos] === ',')
206
+ this.pos++;
207
+ else if (this.source[this.pos] !== '}')
208
+ return this.notLiteral();
209
+ }
210
+ }
211
+ }
212
+ function text(value, field, max, required) {
213
+ if (value === undefined) {
214
+ if (required)
215
+ throw new WorkflowScriptError(`\`meta.${field}\` is required.`);
216
+ return undefined;
217
+ }
218
+ if (typeof value !== 'string' || value.trim() === '') {
219
+ throw new WorkflowScriptError(`\`meta.${field}\` must be a non-empty string.`);
220
+ }
221
+ if (value.length > max) {
222
+ throw new WorkflowScriptError(`\`meta.${field}\` is longer than ${max} characters.`);
223
+ }
224
+ return value;
225
+ }
226
+ function onlyKeys(object, allowed, where) {
227
+ for (const key of Object.keys(object)) {
228
+ if (!allowed.includes(key)) {
229
+ throw new WorkflowScriptError(`\`${where}\` has an unknown key "${key}". Allowed: ${allowed.join(', ')}.`);
230
+ }
231
+ }
232
+ }
233
+ function isRecord(value) {
234
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
235
+ }
236
+ export function normalizePhaseTitle(value) {
237
+ return value.replace(/\s+/g, ' ').trim().slice(0, 120);
238
+ }
239
+ function toMeta(literal) {
240
+ if (!isRecord(literal))
241
+ throw new WorkflowScriptError('`meta` must be an object literal.');
242
+ onlyKeys(literal, ['name', 'description', 'whenToUse', 'phases'], 'meta');
243
+ const name = text(literal.name, 'name', 80, true);
244
+ const description = text(literal.description, 'description', 500, true);
245
+ const whenToUse = text(literal.whenToUse, 'whenToUse', 500, false);
246
+ const phases = [];
247
+ if (literal.phases !== undefined) {
248
+ if (!Array.isArray(literal.phases)) {
249
+ throw new WorkflowScriptError('`meta.phases` must be an array of { title, detail?, model? }.');
250
+ }
251
+ if (literal.phases.length > MAX_PHASES) {
252
+ throw new WorkflowScriptError(`\`meta.phases\` lists more than ${MAX_PHASES} phases.`);
253
+ }
254
+ const seen = new Set();
255
+ for (const [i, entry] of literal.phases.entries()) {
256
+ if (!isRecord(entry)) {
257
+ throw new WorkflowScriptError(`\`meta.phases[${i}]\` must be an object with a title.`);
258
+ }
259
+ onlyKeys(entry, ['title', 'detail', 'model'], `meta.phases[${i}]`);
260
+ const raw = text(entry.title, `phases[${i}].title`, 120, true);
261
+ const title = normalizePhaseTitle(raw);
262
+ if (seen.has(title)) {
263
+ throw new WorkflowScriptError(`\`meta.phases\` repeats the title "${title}".`);
264
+ }
265
+ seen.add(title);
266
+ const detail = text(entry.detail, `phases[${i}].detail`, 300, false);
267
+ const model = text(entry.model, `phases[${i}].model`, 120, false);
268
+ phases.push({
269
+ title,
270
+ ...(detail !== undefined ? { detail } : {}),
271
+ ...(model !== undefined ? { model } : {}),
272
+ });
273
+ }
274
+ }
275
+ return {
276
+ name,
277
+ description,
278
+ ...(whenToUse !== undefined ? { whenToUse } : {}),
279
+ phases,
280
+ };
281
+ }
282
+ /**
283
+ * Split a script into its `meta` and its body. The body keeps the script's
284
+ * line breaks (the meta statement is blanked, not removed) so an error line
285
+ * still points at the author's text.
286
+ */
287
+ export function splitWorkflowScript(source) {
288
+ const parser = new LiteralParser(source);
289
+ parser.skipTrivia();
290
+ KEYWORD.lastIndex = parser.pos;
291
+ const found = KEYWORD.exec(source);
292
+ if (!found) {
293
+ throw new WorkflowScriptError('A workflow script must start with `export const meta = { name, description, phases? }`.');
294
+ }
295
+ const start = parser.pos;
296
+ parser.pos = start + found[0].length;
297
+ const literal = parser.value(0);
298
+ parser.skipTrivia();
299
+ if (source[parser.pos] === ';')
300
+ parser.pos++;
301
+ if (Buffer.byteLength(source.slice(start, parser.pos)) > MAX_META_BYTES) {
302
+ throw new WorkflowScriptError(`\`meta\` is larger than ${MAX_META_BYTES} bytes.`);
303
+ }
304
+ const meta = toMeta(literal);
305
+ return {
306
+ meta,
307
+ body: source.slice(0, parser.pos).replace(/[^\n]/g, ' ') + source.slice(parser.pos),
308
+ };
309
+ }
310
+ /** Parse `meta` from a script without executing it. Throws {@link WorkflowScriptError}. */
311
+ export function parseWorkflowMeta(source) {
312
+ return splitWorkflowScript(source).meta;
313
+ }
314
+ //# sourceMappingURL=meta.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"meta.js","sourceRoot":"","sources":["../../../src/execution/workflow/meta.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAGjD,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,CAAA;AAChC,MAAM,SAAS,GAAG,CAAC,CAAA;AACnB,MAAM,UAAU,GAAG,EAAE,CAAA;AACrB,MAAM,OAAO,GAAG,4BAA4B,CAAA;AAI5C;;;;;;;;GAQG;AACH,MAAM,aAAa;IAGW;IAF7B,GAAG,GAAG,CAAC,CAAA;IAEP,YAA6B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,C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KAAK,CAAC,MAAM,CAAC,GAAG,CAAC;KACnF,CAAA;AACF,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,iBAAiB,CAAC,MAAc;IAC/C,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC,IAAI,CAAA;AACxC,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { WorkflowAgentState, WorkflowPhaseMeta, WorkflowPhaseSummary } from './types.js';
2
+ export interface PhaseRecord {
3
+ readonly index: number;
4
+ readonly title: string;
5
+ readonly declared: boolean;
6
+ }
7
+ /**
8
+ * Phase titles of one run. A title is the only grouping key: the same title
9
+ * is the same group. Indexes are 1-based first-seen order, declared titles
10
+ * first. `register` reports whether the title was announced just now.
11
+ */
12
+ export declare class PhaseRegistry {
13
+ private readonly byTitle;
14
+ register(rawTitle: string, declared?: boolean): {
15
+ phase: PhaseRecord;
16
+ isNew: boolean;
17
+ };
18
+ all(): PhaseRecord[];
19
+ }
20
+ export interface PhaseAgentView {
21
+ readonly phaseIndex?: number;
22
+ readonly state: WorkflowAgentState;
23
+ readonly tokens: number;
24
+ }
25
+ /**
26
+ * Per-phase status from the agents seen so far, in phase order. A phase with
27
+ * no agent yet is `not-started`. When no agent belongs to any phase (and
28
+ * nothing is declared) the run shows one derived group, "Agents"; agents
29
+ * without a phase next to phased ones also land in "Agents", listed last.
30
+ */
31
+ export declare function summarizeWorkflowPhases(declared: readonly WorkflowPhaseMeta[], phases: readonly PhaseRecord[], agents: readonly PhaseAgentView[]): WorkflowPhaseSummary[];
32
+ //# sourceMappingURL=phases.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"phases.d.ts","sourceRoot":"","sources":["../../../src/execution/workflow/phases.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,kBAAkB,EAClB,iBAAiB,EAEjB,oBAAoB,EACpB,MAAM,YAAY,CAAA;AAEnB,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;CAC1B;AAED;;;;GAIG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiC;IAEzD,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,UAAQ,GAAG;QAAE,KAAK,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE;IASpF,GAAG,IAAI,WAAW,EAAE;CAGpB;AAED,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,KAAK,EAAE,kBAAkB,CAAA;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACvB;AAQD;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACtC,QAAQ,EAAE,SAAS,iBAAiB,EAAE,EACtC,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,MAAM,EAAE,SAAS,cAAc,EAAE,GAC/B,oBAAoB,EAAE,CAgCxB"}
@@ -0,0 +1,57 @@
1
+ import { normalizePhaseTitle } from './meta.js';
2
+ /**
3
+ * Phase titles of one run. A title is the only grouping key: the same title
4
+ * is the same group. Indexes are 1-based first-seen order, declared titles
5
+ * first. `register` reports whether the title was announced just now.
6
+ */
7
+ export class PhaseRegistry {
8
+ byTitle = new Map();
9
+ register(rawTitle, declared = false) {
10
+ const title = normalizePhaseTitle(rawTitle);
11
+ const existing = this.byTitle.get(title);
12
+ if (existing)
13
+ return { phase: existing, isNew: false };
14
+ const phase = { index: this.byTitle.size + 1, title, declared };
15
+ this.byTitle.set(title, phase);
16
+ return { phase, isNew: true };
17
+ }
18
+ all() {
19
+ return [...this.byTitle.values()];
20
+ }
21
+ }
22
+ function statusOf(total, done, failed) {
23
+ if (total === 0)
24
+ return 'not-started';
25
+ if (done + failed === total)
26
+ return failed > 0 ? 'failed' : 'done';
27
+ return 'running';
28
+ }
29
+ /**
30
+ * Per-phase status from the agents seen so far, in phase order. A phase with
31
+ * no agent yet is `not-started`. When no agent belongs to any phase (and
32
+ * nothing is declared) the run shows one derived group, "Agents"; agents
33
+ * without a phase next to phased ones also land in "Agents", listed last.
34
+ */
35
+ export function summarizeWorkflowPhases(declared, phases, agents) {
36
+ const detail = new Map(declared.map((p) => [normalizePhaseTitle(p.title), p.detail]));
37
+ const summarize = (index, title, members, extra) => {
38
+ const done = members.filter((a) => a.state === 'done').length;
39
+ const failed = members.filter((a) => a.state === 'error').length;
40
+ return {
41
+ index,
42
+ title,
43
+ ...(extra !== undefined ? { detail: extra } : {}),
44
+ status: statusOf(members.length, done, failed),
45
+ agents: members.length,
46
+ done,
47
+ failed,
48
+ tokens: members.reduce((sum, a) => sum + a.tokens, 0),
49
+ };
50
+ };
51
+ const out = phases.map((p) => summarize(p.index, p.title, agents.filter((a) => a.phaseIndex === p.index), detail.get(p.title)));
52
+ const ungrouped = agents.filter((a) => a.phaseIndex === undefined);
53
+ if (ungrouped.length > 0)
54
+ out.push(summarize(0, 'Agents', ungrouped));
55
+ return out;
56
+ }
57
+ //# sourceMappingURL=phases.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"phases.js","sourceRoot":"","sources":["../../../src/execution/workflow/phases.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAA;AAc/C;;;;GAIG;AACH,MAAM,OAAO,aAAa;IACR,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAA;IAEzD,QAAQ,CAAC,QAAgB,EAAE,QAAQ,GAAG,KAAK;QAC1C,MAAM,KAAK,GAAG,mBAAmB,CAAC,QAAQ,CAAC,CAAA;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACxC,IAAI,QAAQ;YAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,C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KAAK,OAAO,CAAC,CAAC,MAAM,CAAA;QAChE,OAAO;YACN,KAAK;YACL,KAAK;YACL,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACjD,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC;YAC9C,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,IAAI;YACJ,MAAM;YACN,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;SACrD,CAAA;IACF,CAAC,CAAA;IACD,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAC5B,SAAS,CACR,CAAC,CAAC,KAAK,EACP,CAAC,CAAC,KAAK,EACP,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,CAAC,CAAC,KAAK,CAAC,EAC9C,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CACnB,CACD,CAAA;IACD,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,SAAS,CAAC,CAAA;IAClE,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC;QAAE,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAA;IACrE,OAAO,GAAG,CAAA;AACX,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Lines the prelude occupies in the interpreter source, plus the newline that
3
+ * joins it to the script body: subtract this from an interpreter line number
4
+ * to get the line in the script the author wrote.
5
+ */
6
+ export declare function preludeLineCount(config: {
7
+ args: unknown;
8
+ tokenLimit: number | null;
9
+ }): number;
10
+ /** Source that installs the workflow globals, ready to prepend to a script body. */
11
+ export declare function buildPrelude(config: {
12
+ args: unknown;
13
+ tokenLimit: number | null;
14
+ }): string;
15
+ //# sourceMappingURL=prelude.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prelude.d.ts","sourceRoot":"","sources":["../../../src/execution/workflow/prelude.ts"],"names":[],"mappings":"AA+GA;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACxC,IAAI,EAAE,OAAO,CAAA;IACb,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB,GAAG,MAAM,CAET;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACpC,IAAI,EAAE,OAAO,CAAA;IACb,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB,GAAG,MAAM,CAOT"}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * The interpreter-side half of the workflow language: `agent`, `parallel`,
3
+ * `pipeline`, `phase`, `log`, `args` and `budget`.
4
+ *
5
+ * This text runs INSIDE the QuickJS interpreter, ahead of the script body,
6
+ * and is the only place those names are defined. It captures the two bridge
7
+ * natives (`call`, `print`) in its own closure and deletes them from the
8
+ * global object, so the script reaches the host only through `agent()`, and
9
+ * `phase()`/`log()` can only emit well-formed records. None of this is a
10
+ * security boundary: the host re-validates every request and treats every
11
+ * printed line as untrusted text. The boundary is the interpreter itself.
12
+ *
13
+ * Written with String.raw and no backticks or substitutions so no escaping
14
+ * layer can change what the interpreter parses.
15
+ */
16
+ const PRELUDE_SOURCE = String.raw `(function (config) {
17
+ 'use strict';
18
+ var rawCall = globalThis.call;
19
+ var rawPrint = globalThis.print;
20
+ delete globalThis.call;
21
+ delete globalThis.print;
22
+ var stringify = JSON.stringify;
23
+ var spent = { tokensUsed: 0 };
24
+ var cursor = null;
25
+ function emit(record) { rawPrint(stringify(record)); }
26
+ function clean(value, limit) {
27
+ return String(value).replace(/\s+/g, ' ').trim().slice(0, limit);
28
+ }
29
+ function phase(title) {
30
+ var t = clean(title, 120);
31
+ if (!t) throw new TypeError('phase() needs a non-empty title.');
32
+ cursor = t;
33
+ emit({ k: 'phase', title: t });
34
+ }
35
+ function log(message) {
36
+ emit({ k: 'log', message: String(message).slice(0, 2000) });
37
+ }
38
+ function kind(value) {
39
+ if (value === null) return 'null';
40
+ if (value === '') return 'an empty string';
41
+ if (Array.isArray(value)) return 'an array';
42
+ var t = typeof value;
43
+ if (t === 'undefined') return 'undefined';
44
+ if (t === 'object') return typeof value.then === 'function' ? 'a promise' : 'an object';
45
+ return 'a ' + t;
46
+ }
47
+ async function agent(prompt, opts) {
48
+ if (typeof prompt !== 'string' || prompt.length === 0) {
49
+ throw new TypeError('agent(prompt, options?): the prompt must be a non-empty string, got ' + kind(prompt));
50
+ }
51
+ var o = opts == null ? {} : opts;
52
+ if (typeof o !== 'object' || Array.isArray(o)) {
53
+ throw new TypeError('agent(prompt, options?): options must be an object, got ' + kind(o));
54
+ }
55
+ var request = { prompt: prompt, cursor: cursor };
56
+ if (o.label != null) request.label = clean(o.label, 120);
57
+ if (o.phase != null) request.phase = clean(o.phase, 120);
58
+ if (o.schema != null) request.schema = o.schema;
59
+ if (o.agentType != null) request.agentType = clean(o.agentType, 120);
60
+ if (o.model != null) request.model = clean(o.model, 120);
61
+ if (o.effort != null) request.effort = clean(o.effort, 32);
62
+ var reply = await rawCall('agent', request);
63
+ spent.tokensUsed = reply.t;
64
+ return reply.v;
65
+ }
66
+ async function parallel(thunks) {
67
+ if (!Array.isArray(thunks)) {
68
+ throw new TypeError('parallel(thunks): the argument must be an array of functions, got ' + kind(thunks));
69
+ }
70
+ for (var p = 0; p < thunks.length; p++) {
71
+ if (typeof thunks[p] !== 'function') {
72
+ throw new TypeError('parallel(thunks): item ' + (p + 1) + ' must be a function (wrap the work as () => agent(...)), got ' + kind(thunks[p]));
73
+ }
74
+ }
75
+ return Promise.all(thunks.map(function (t) { return t(); }));
76
+ }
77
+ async function pipeline(items) {
78
+ if (!Array.isArray(items)) {
79
+ throw new TypeError('pipeline(items, ...stages): the first argument must be an array, got ' + kind(items));
80
+ }
81
+ var stages = Array.prototype.slice.call(arguments, 1);
82
+ for (var s = 0; s < stages.length; s++) {
83
+ if (typeof stages[s] !== 'function') {
84
+ throw new TypeError('pipeline(items, ...stages): stage ' + (s + 1) + ' must be a function, got ' + kind(stages[s]));
85
+ }
86
+ }
87
+ return Promise.all(items.map(async function (item, index) {
88
+ var value = item;
89
+ for (var i = 0; i < stages.length; i++) {
90
+ value = await stages[i](value, index, item);
91
+ if (value === null || value === undefined) return null;
92
+ }
93
+ return value;
94
+ }));
95
+ }
96
+ var budget = Object.freeze({
97
+ get tokensUsed() { return spent.tokensUsed; },
98
+ get tokenLimit() { return config.tokenLimit === null ? undefined : config.tokenLimit; },
99
+ });
100
+ function install(name, value) {
101
+ Object.defineProperty(globalThis, name, { value: value, writable: false, configurable: false, enumerable: true });
102
+ }
103
+ install('agent', agent);
104
+ install('parallel', parallel);
105
+ install('pipeline', pipeline);
106
+ install('phase', phase);
107
+ install('log', log);
108
+ install('budget', budget);
109
+ install('args', config.args);
110
+ })`;
111
+ /**
112
+ * Lines the prelude occupies in the interpreter source, plus the newline that
113
+ * joins it to the script body: subtract this from an interpreter line number
114
+ * to get the line in the script the author wrote.
115
+ */
116
+ export function preludeLineCount(config) {
117
+ return buildPrelude(config).split('\n').length;
118
+ }
119
+ /** Source that installs the workflow globals, ready to prepend to a script body. */
120
+ export function buildPrelude(config) {
121
+ const literal = JSON.stringify(config)
122
+ .split(String.fromCharCode(0x2028))
123
+ .join('\\u2028')
124
+ .split(String.fromCharCode(0x2029))
125
+ .join('\\u2029');
126
+ return `${PRELUDE_SOURCE}(${literal});`;
127
+ }
128
+ //# sourceMappingURL=prelude.js.map