@tagma/sdk 0.4.13 → 0.4.15
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/LICENSE +21 -21
- package/README.md +569 -572
- package/dist/dag.d.ts.map +1 -1
- package/dist/dag.js +22 -56
- package/dist/dag.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +63 -37
- package/dist/engine.js.map +1 -1
- package/dist/middlewares/static-context.d.ts.map +1 -1
- package/dist/middlewares/static-context.js +7 -3
- package/dist/middlewares/static-context.js.map +1 -1
- package/dist/prompt-doc.d.ts +36 -0
- package/dist/prompt-doc.d.ts.map +1 -0
- package/dist/prompt-doc.js +44 -0
- package/dist/prompt-doc.js.map +1 -0
- package/dist/sdk.d.ts +3 -0
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +4 -0
- package/dist/sdk.js.map +1 -1
- package/dist/task-ref.d.ts +55 -0
- package/dist/task-ref.d.ts.map +1 -0
- package/dist/task-ref.js +101 -0
- package/dist/task-ref.js.map +1 -0
- package/dist/templates.d.ts +20 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +93 -0
- package/dist/templates.js.map +1 -0
- package/dist/validate-raw.d.ts.map +1 -1
- package/dist/validate-raw.js +27 -53
- package/dist/validate-raw.js.map +1 -1
- package/package.json +2 -2
- package/scripts/preinstall.js +31 -31
- package/src/adapters/stdin-approval.ts +106 -106
- package/src/adapters/websocket-approval.ts +224 -224
- package/src/approval.ts +131 -131
- package/src/bootstrap.ts +37 -37
- package/src/completions/exit-code.ts +34 -34
- package/src/completions/file-exists.ts +66 -66
- package/src/completions/output-check.ts +86 -86
- package/src/config-ops.ts +307 -307
- package/src/dag.ts +24 -54
- package/src/drivers/claude-code.ts +250 -250
- package/src/engine.ts +1137 -1098
- package/src/hooks.ts +187 -187
- package/src/logger.ts +182 -182
- package/src/middlewares/static-context.ts +49 -45
- package/src/pipeline-runner.ts +156 -156
- package/src/prompt-doc.ts +49 -0
- package/src/registry.ts +242 -242
- package/src/runner.ts +395 -395
- package/src/schema.test.ts +101 -101
- package/src/schema.ts +338 -338
- package/src/sdk.ts +111 -92
- package/src/task-ref.ts +120 -0
- package/src/triggers/file.ts +164 -164
- package/src/triggers/manual.ts +86 -86
- package/src/types.ts +18 -18
- package/src/utils.ts +203 -203
- package/src/validate-raw.ts +412 -442
package/src/logger.ts
CHANGED
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
import { resolve, dirname } from 'node:path';
|
|
2
|
-
import { mkdirSync, writeFileSync, openSync, writeSync, closeSync } from 'node:fs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Structured record emitted for every log line. Consumers (e.g. the editor
|
|
6
|
-
* server) use this to stream process-level detail into UIs alongside the
|
|
7
|
-
* on-disk pipeline.log. `taskId` is extracted from a `[task:<id>]` prefix
|
|
8
|
-
* when the call site passes one, or overridden explicitly via the optional
|
|
9
|
-
* `taskId` argument on `section`/`quiet` (which carry no prefix).
|
|
10
|
-
*/
|
|
11
|
-
export type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'section' | 'quiet';
|
|
12
|
-
|
|
13
|
-
export interface LogRecord {
|
|
14
|
-
readonly level: LogLevel;
|
|
15
|
-
readonly taskId: string | null;
|
|
16
|
-
readonly timestamp: string;
|
|
17
|
-
readonly text: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type LogListener = (record: LogRecord) => void;
|
|
21
|
-
|
|
22
|
-
const TASK_PREFIX_RE = /\[task:([^\]]+)\]/;
|
|
23
|
-
|
|
24
|
-
function taskIdFromPrefix(prefix: string): string | null {
|
|
25
|
-
const m = TASK_PREFIX_RE.exec(prefix);
|
|
26
|
-
return m ? m[1] : null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Dual-channel logger.
|
|
31
|
-
*
|
|
32
|
-
* - `info/warn/error` → console AND file (brief, user-visible events)
|
|
33
|
-
* - `debug` → file ONLY (verbose diagnostics)
|
|
34
|
-
* - `section` → file ONLY (visual separators)
|
|
35
|
-
* - `quiet` → file ONLY (bulk payload like full stdout dumps)
|
|
36
|
-
*
|
|
37
|
-
* Log file path: <workDir>/.tagma/logs/<runId>/pipeline.log (one file per pipeline run,
|
|
38
|
-
* truncated on construction). Every line is also forwarded to the optional
|
|
39
|
-
* `onLine` callback as a structured `LogRecord`, so callers that want to
|
|
40
|
-
* stream the run process over IPC/SSE don't need to tail the file.
|
|
41
|
-
*/
|
|
42
|
-
export class Logger {
|
|
43
|
-
private readonly filePath: string;
|
|
44
|
-
private readonly runDir: string;
|
|
45
|
-
private readonly onLine: LogListener | null;
|
|
46
|
-
/** Persistent file descriptor for append writes (avoids open/close per line). */
|
|
47
|
-
private fd: number | null;
|
|
48
|
-
|
|
49
|
-
constructor(workDir: string, runId: string, onLine?: LogListener) {
|
|
50
|
-
this.runDir = resolve(workDir, '.tagma', 'logs', runId);
|
|
51
|
-
this.filePath = resolve(this.runDir, 'pipeline.log');
|
|
52
|
-
this.onLine = onLine ?? null;
|
|
53
|
-
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
54
|
-
const header =
|
|
55
|
-
`# Pipeline run ${runId} @ ${new Date().toISOString()}\n` +
|
|
56
|
-
`# Host: ${process.platform} ${process.arch} Bun: ${process.versions.bun ?? 'n/a'}\n` +
|
|
57
|
-
`# Work dir: ${workDir}\n\n`;
|
|
58
|
-
writeFileSync(this.filePath, header);
|
|
59
|
-
// Open once for all subsequent appends (O_APPEND is implied by 'a' flag)
|
|
60
|
-
this.fd = openSync(this.filePath, 'a');
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
info(prefix: string, message: string): void {
|
|
64
|
-
const ts = timestamp();
|
|
65
|
-
const line = `${ts} ${prefix} ${message}`;
|
|
66
|
-
// eslint-disable-next-line no-console
|
|
67
|
-
console.log(line);
|
|
68
|
-
this.emit('info', ts, line, taskIdFromPrefix(prefix));
|
|
69
|
-
this.append(line);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
warn(prefix: string, message: string): void {
|
|
73
|
-
const ts = timestamp();
|
|
74
|
-
const line = `${ts} ${prefix} WARN: ${message}`;
|
|
75
|
-
console.warn(line);
|
|
76
|
-
this.emit('warn', ts, line, taskIdFromPrefix(prefix));
|
|
77
|
-
this.append(line);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
error(prefix: string, message: string): void {
|
|
81
|
-
const ts = timestamp();
|
|
82
|
-
const line = `${ts} ${prefix} ERROR: ${message}`;
|
|
83
|
-
console.error(line);
|
|
84
|
-
this.emit('error', ts, line, taskIdFromPrefix(prefix));
|
|
85
|
-
this.append(line);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** File-only diagnostic log line. */
|
|
89
|
-
debug(prefix: string, message: string): void {
|
|
90
|
-
const ts = timestamp();
|
|
91
|
-
const line = `${ts} ${prefix} DEBUG: ${message}`;
|
|
92
|
-
this.emit('debug', ts, line, taskIdFromPrefix(prefix));
|
|
93
|
-
this.append(line);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** File-only visual separator with title. */
|
|
97
|
-
section(title: string, taskId?: string | null): void {
|
|
98
|
-
const ts = timestamp();
|
|
99
|
-
const text = `\n━━━ ${title} ━━━`;
|
|
100
|
-
this.emit('section', ts, text, taskId ?? null);
|
|
101
|
-
this.append(text);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** File-only bulk payload (e.g. full stdout / stderr dumps). */
|
|
105
|
-
quiet(message: string, taskId?: string | null): void {
|
|
106
|
-
const ts = timestamp();
|
|
107
|
-
this.emit('quiet', ts, message, taskId ?? null);
|
|
108
|
-
this.append(message);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private append(line: string): void {
|
|
112
|
-
if (this.fd === null) return;
|
|
113
|
-
try {
|
|
114
|
-
const data = line.endsWith('\n') ? line : line + '\n';
|
|
115
|
-
writeSync(this.fd, data);
|
|
116
|
-
} catch {
|
|
117
|
-
// Swallow log write failures; engine correctness shouldn't depend on logging.
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Close the persistent file handle. Called by the engine at run completion. */
|
|
122
|
-
close(): void {
|
|
123
|
-
if (this.fd !== null) {
|
|
124
|
-
try {
|
|
125
|
-
closeSync(this.fd);
|
|
126
|
-
} catch {
|
|
127
|
-
/* already closed */
|
|
128
|
-
}
|
|
129
|
-
this.fd = null;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private emit(level: LogLevel, ts: string, text: string, taskId: string | null): void {
|
|
134
|
-
if (!this.onLine) return;
|
|
135
|
-
try {
|
|
136
|
-
this.onLine({ level, taskId, timestamp: ts, text });
|
|
137
|
-
} catch {
|
|
138
|
-
// Never let a listener error derail the pipeline.
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
get path(): string {
|
|
143
|
-
return this.filePath;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Directory that holds all artifacts for this run (pipeline.log, *.stderr, etc.). */
|
|
147
|
-
get dir(): string {
|
|
148
|
-
return this.runDir;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function timestamp(): string {
|
|
153
|
-
const d = new Date();
|
|
154
|
-
const hh = String(d.getHours()).padStart(2, '0');
|
|
155
|
-
const mm = String(d.getMinutes()).padStart(2, '0');
|
|
156
|
-
const ss = String(d.getSeconds()).padStart(2, '0');
|
|
157
|
-
const ms = String(d.getMilliseconds()).padStart(3, '0');
|
|
158
|
-
return `${hh}:${mm}:${ss}.${ms}`;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Return the last `n` non-empty lines of `text`, joined with newlines. */
|
|
162
|
-
export function tailLines(text: string, n: number): string {
|
|
163
|
-
if (!text) return '';
|
|
164
|
-
const lines = text.split(/\r?\n/).filter((l) => l.length > 0);
|
|
165
|
-
return lines.slice(-n).join('\n');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Truncate a blob to at most `maxBytes` UTF-8 bytes for log embedding,
|
|
170
|
-
* appending a marker when truncation occurred.
|
|
171
|
-
* Uses TextEncoder so CJK and emoji (multi-byte) characters are counted correctly.
|
|
172
|
-
*/
|
|
173
|
-
export function clip(text: string, maxBytes = 16 * 1024): string {
|
|
174
|
-
if (!text) return '';
|
|
175
|
-
const encoder = new TextEncoder();
|
|
176
|
-
const bytes = encoder.encode(text);
|
|
177
|
-
if (bytes.length <= maxBytes) return text;
|
|
178
|
-
const omittedBytes = bytes.length - maxBytes;
|
|
179
|
-
// TextDecoder handles partial code-point boundaries safely (replacement char insertion)
|
|
180
|
-
const truncated = new TextDecoder().decode(bytes.slice(0, maxBytes));
|
|
181
|
-
return truncated + `\n…[truncated ${omittedBytes} bytes]`;
|
|
182
|
-
}
|
|
1
|
+
import { resolve, dirname } from 'node:path';
|
|
2
|
+
import { mkdirSync, writeFileSync, openSync, writeSync, closeSync } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Structured record emitted for every log line. Consumers (e.g. the editor
|
|
6
|
+
* server) use this to stream process-level detail into UIs alongside the
|
|
7
|
+
* on-disk pipeline.log. `taskId` is extracted from a `[task:<id>]` prefix
|
|
8
|
+
* when the call site passes one, or overridden explicitly via the optional
|
|
9
|
+
* `taskId` argument on `section`/`quiet` (which carry no prefix).
|
|
10
|
+
*/
|
|
11
|
+
export type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'section' | 'quiet';
|
|
12
|
+
|
|
13
|
+
export interface LogRecord {
|
|
14
|
+
readonly level: LogLevel;
|
|
15
|
+
readonly taskId: string | null;
|
|
16
|
+
readonly timestamp: string;
|
|
17
|
+
readonly text: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type LogListener = (record: LogRecord) => void;
|
|
21
|
+
|
|
22
|
+
const TASK_PREFIX_RE = /\[task:([^\]]+)\]/;
|
|
23
|
+
|
|
24
|
+
function taskIdFromPrefix(prefix: string): string | null {
|
|
25
|
+
const m = TASK_PREFIX_RE.exec(prefix);
|
|
26
|
+
return m ? m[1] : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Dual-channel logger.
|
|
31
|
+
*
|
|
32
|
+
* - `info/warn/error` → console AND file (brief, user-visible events)
|
|
33
|
+
* - `debug` → file ONLY (verbose diagnostics)
|
|
34
|
+
* - `section` → file ONLY (visual separators)
|
|
35
|
+
* - `quiet` → file ONLY (bulk payload like full stdout dumps)
|
|
36
|
+
*
|
|
37
|
+
* Log file path: <workDir>/.tagma/logs/<runId>/pipeline.log (one file per pipeline run,
|
|
38
|
+
* truncated on construction). Every line is also forwarded to the optional
|
|
39
|
+
* `onLine` callback as a structured `LogRecord`, so callers that want to
|
|
40
|
+
* stream the run process over IPC/SSE don't need to tail the file.
|
|
41
|
+
*/
|
|
42
|
+
export class Logger {
|
|
43
|
+
private readonly filePath: string;
|
|
44
|
+
private readonly runDir: string;
|
|
45
|
+
private readonly onLine: LogListener | null;
|
|
46
|
+
/** Persistent file descriptor for append writes (avoids open/close per line). */
|
|
47
|
+
private fd: number | null;
|
|
48
|
+
|
|
49
|
+
constructor(workDir: string, runId: string, onLine?: LogListener) {
|
|
50
|
+
this.runDir = resolve(workDir, '.tagma', 'logs', runId);
|
|
51
|
+
this.filePath = resolve(this.runDir, 'pipeline.log');
|
|
52
|
+
this.onLine = onLine ?? null;
|
|
53
|
+
mkdirSync(dirname(this.filePath), { recursive: true });
|
|
54
|
+
const header =
|
|
55
|
+
`# Pipeline run ${runId} @ ${new Date().toISOString()}\n` +
|
|
56
|
+
`# Host: ${process.platform} ${process.arch} Bun: ${process.versions.bun ?? 'n/a'}\n` +
|
|
57
|
+
`# Work dir: ${workDir}\n\n`;
|
|
58
|
+
writeFileSync(this.filePath, header);
|
|
59
|
+
// Open once for all subsequent appends (O_APPEND is implied by 'a' flag)
|
|
60
|
+
this.fd = openSync(this.filePath, 'a');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
info(prefix: string, message: string): void {
|
|
64
|
+
const ts = timestamp();
|
|
65
|
+
const line = `${ts} ${prefix} ${message}`;
|
|
66
|
+
// eslint-disable-next-line no-console
|
|
67
|
+
console.log(line);
|
|
68
|
+
this.emit('info', ts, line, taskIdFromPrefix(prefix));
|
|
69
|
+
this.append(line);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
warn(prefix: string, message: string): void {
|
|
73
|
+
const ts = timestamp();
|
|
74
|
+
const line = `${ts} ${prefix} WARN: ${message}`;
|
|
75
|
+
console.warn(line);
|
|
76
|
+
this.emit('warn', ts, line, taskIdFromPrefix(prefix));
|
|
77
|
+
this.append(line);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
error(prefix: string, message: string): void {
|
|
81
|
+
const ts = timestamp();
|
|
82
|
+
const line = `${ts} ${prefix} ERROR: ${message}`;
|
|
83
|
+
console.error(line);
|
|
84
|
+
this.emit('error', ts, line, taskIdFromPrefix(prefix));
|
|
85
|
+
this.append(line);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** File-only diagnostic log line. */
|
|
89
|
+
debug(prefix: string, message: string): void {
|
|
90
|
+
const ts = timestamp();
|
|
91
|
+
const line = `${ts} ${prefix} DEBUG: ${message}`;
|
|
92
|
+
this.emit('debug', ts, line, taskIdFromPrefix(prefix));
|
|
93
|
+
this.append(line);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** File-only visual separator with title. */
|
|
97
|
+
section(title: string, taskId?: string | null): void {
|
|
98
|
+
const ts = timestamp();
|
|
99
|
+
const text = `\n━━━ ${title} ━━━`;
|
|
100
|
+
this.emit('section', ts, text, taskId ?? null);
|
|
101
|
+
this.append(text);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** File-only bulk payload (e.g. full stdout / stderr dumps). */
|
|
105
|
+
quiet(message: string, taskId?: string | null): void {
|
|
106
|
+
const ts = timestamp();
|
|
107
|
+
this.emit('quiet', ts, message, taskId ?? null);
|
|
108
|
+
this.append(message);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private append(line: string): void {
|
|
112
|
+
if (this.fd === null) return;
|
|
113
|
+
try {
|
|
114
|
+
const data = line.endsWith('\n') ? line : line + '\n';
|
|
115
|
+
writeSync(this.fd, data);
|
|
116
|
+
} catch {
|
|
117
|
+
// Swallow log write failures; engine correctness shouldn't depend on logging.
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Close the persistent file handle. Called by the engine at run completion. */
|
|
122
|
+
close(): void {
|
|
123
|
+
if (this.fd !== null) {
|
|
124
|
+
try {
|
|
125
|
+
closeSync(this.fd);
|
|
126
|
+
} catch {
|
|
127
|
+
/* already closed */
|
|
128
|
+
}
|
|
129
|
+
this.fd = null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private emit(level: LogLevel, ts: string, text: string, taskId: string | null): void {
|
|
134
|
+
if (!this.onLine) return;
|
|
135
|
+
try {
|
|
136
|
+
this.onLine({ level, taskId, timestamp: ts, text });
|
|
137
|
+
} catch {
|
|
138
|
+
// Never let a listener error derail the pipeline.
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
get path(): string {
|
|
143
|
+
return this.filePath;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Directory that holds all artifacts for this run (pipeline.log, *.stderr, etc.). */
|
|
147
|
+
get dir(): string {
|
|
148
|
+
return this.runDir;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function timestamp(): string {
|
|
153
|
+
const d = new Date();
|
|
154
|
+
const hh = String(d.getHours()).padStart(2, '0');
|
|
155
|
+
const mm = String(d.getMinutes()).padStart(2, '0');
|
|
156
|
+
const ss = String(d.getSeconds()).padStart(2, '0');
|
|
157
|
+
const ms = String(d.getMilliseconds()).padStart(3, '0');
|
|
158
|
+
return `${hh}:${mm}:${ss}.${ms}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Return the last `n` non-empty lines of `text`, joined with newlines. */
|
|
162
|
+
export function tailLines(text: string, n: number): string {
|
|
163
|
+
if (!text) return '';
|
|
164
|
+
const lines = text.split(/\r?\n/).filter((l) => l.length > 0);
|
|
165
|
+
return lines.slice(-n).join('\n');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Truncate a blob to at most `maxBytes` UTF-8 bytes for log embedding,
|
|
170
|
+
* appending a marker when truncation occurred.
|
|
171
|
+
* Uses TextEncoder so CJK and emoji (multi-byte) characters are counted correctly.
|
|
172
|
+
*/
|
|
173
|
+
export function clip(text: string, maxBytes = 16 * 1024): string {
|
|
174
|
+
if (!text) return '';
|
|
175
|
+
const encoder = new TextEncoder();
|
|
176
|
+
const bytes = encoder.encode(text);
|
|
177
|
+
if (bytes.length <= maxBytes) return text;
|
|
178
|
+
const omittedBytes = bytes.length - maxBytes;
|
|
179
|
+
// TextDecoder handles partial code-point boundaries safely (replacement char insertion)
|
|
180
|
+
const truncated = new TextDecoder().decode(bytes.slice(0, maxBytes));
|
|
181
|
+
return truncated + `\n…[truncated ${omittedBytes} bytes]`;
|
|
182
|
+
}
|
|
@@ -1,45 +1,49 @@
|
|
|
1
|
-
import { basename } from 'path';
|
|
2
|
-
import type { MiddlewarePlugin, MiddlewareContext } from '../types';
|
|
3
|
-
import { validatePath } from '../utils';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
import { basename } from 'path';
|
|
2
|
+
import type { MiddlewarePlugin, MiddlewareContext, PromptDocument } from '../types';
|
|
3
|
+
import { validatePath } from '../utils';
|
|
4
|
+
import { appendContext } from '../prompt-doc';
|
|
5
|
+
|
|
6
|
+
export const StaticContextMiddleware: MiddlewarePlugin = {
|
|
7
|
+
name: 'static_context',
|
|
8
|
+
schema: {
|
|
9
|
+
description: 'Prepend a reference file to the prompt as static context.',
|
|
10
|
+
fields: {
|
|
11
|
+
file: {
|
|
12
|
+
type: 'path',
|
|
13
|
+
required: true,
|
|
14
|
+
description: 'Path to the reference file (relative to workDir or absolute).',
|
|
15
|
+
placeholder: 'docs/spec.md',
|
|
16
|
+
},
|
|
17
|
+
label: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
description: 'Header shown before the content. Defaults to "Reference: <basename>".',
|
|
20
|
+
placeholder: 'Reference: spec.md',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
async enhanceDoc(
|
|
26
|
+
doc: PromptDocument,
|
|
27
|
+
config: Record<string, unknown>,
|
|
28
|
+
ctx: MiddlewareContext,
|
|
29
|
+
): Promise<PromptDocument> {
|
|
30
|
+
const filePath = config.file as string;
|
|
31
|
+
if (!filePath) throw new Error('static_context middleware: "file" is required');
|
|
32
|
+
|
|
33
|
+
const safePath = validatePath(filePath, ctx.workDir);
|
|
34
|
+
const file = Bun.file(safePath);
|
|
35
|
+
|
|
36
|
+
if (!(await file.exists())) {
|
|
37
|
+
console.warn(`static_context: file ${filePath} not found, skipping`);
|
|
38
|
+
return doc;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const content = await file.text();
|
|
42
|
+
const label = (config.label as string) ?? `Reference: ${basename(filePath)}`;
|
|
43
|
+
|
|
44
|
+
// Append a labeled context block; the engine's serializer joins blocks
|
|
45
|
+
// with blank lines and places the task last. No [Task] header here —
|
|
46
|
+
// that framing is the driver's concern (e.g. opencode's agent_profile).
|
|
47
|
+
return appendContext(doc, { label, content });
|
|
48
|
+
},
|
|
49
|
+
};
|