@tagma/sdk 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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
+ }
package/src/prompt-doc.ts CHANGED
@@ -1,49 +1,49 @@
1
- import type { PromptDocument, PromptContextBlock } from './types';
2
-
3
- /**
4
- * Build a fresh `PromptDocument` from a raw task string.
5
- * Middlewares receive this from the engine and push context blocks onto
6
- * `contexts`. `task` is the user's original prompt and should not be
7
- * rewritten by middlewares (translation middlewares are the rare exception).
8
- */
9
- export function promptDocumentFromString(task: string): PromptDocument {
10
- return { contexts: [], task };
11
- }
12
-
13
- /**
14
- * Serialize a `PromptDocument` to the default string form consumed by
15
- * drivers that read `task.prompt` instead of `ctx.promptDoc`.
16
- *
17
- * Format:
18
- *
19
- * [<label1>]
20
- * <content1>
21
- *
22
- * [<label2>]
23
- * <content2>
24
- *
25
- * <task>
26
- *
27
- * Each context block is separated from the next (and from `task`) by a
28
- * single blank line. No implicit `[Task]` header is emitted — that framing
29
- * is the driver's responsibility (e.g. opencode's `agent_profile` wrapping).
30
- * Emitting one here would compose incorrectly with any driver that also
31
- * adds a `[Task]` header, producing a double header that some models
32
- * (observed with `opencode/big-pickle`) misread as a cut-off message.
33
- */
34
- export function serializePromptDocument(doc: PromptDocument): string {
35
- if (doc.contexts.length === 0) return doc.task;
36
- const blocks = doc.contexts.map((c) => `[${c.label}]\n${c.content}`);
37
- return `${blocks.join('\n\n')}\n\n${doc.task}`;
38
- }
39
-
40
- /**
41
- * Helper for middlewares: return a new document with the given block
42
- * appended to `contexts`, preserving immutability of `doc`.
43
- */
44
- export function appendContext(
45
- doc: PromptDocument,
46
- block: PromptContextBlock,
47
- ): PromptDocument {
48
- return { contexts: [...doc.contexts, block], task: doc.task };
49
- }
1
+ import type { PromptDocument, PromptContextBlock } from './types';
2
+
3
+ /**
4
+ * Build a fresh `PromptDocument` from a raw task string.
5
+ * Middlewares receive this from the engine and push context blocks onto
6
+ * `contexts`. `task` is the user's original prompt and should not be
7
+ * rewritten by middlewares (translation middlewares are the rare exception).
8
+ */
9
+ export function promptDocumentFromString(task: string): PromptDocument {
10
+ return { contexts: [], task };
11
+ }
12
+
13
+ /**
14
+ * Serialize a `PromptDocument` to the default string form consumed by
15
+ * drivers that read `task.prompt` instead of `ctx.promptDoc`.
16
+ *
17
+ * Format:
18
+ *
19
+ * [<label1>]
20
+ * <content1>
21
+ *
22
+ * [<label2>]
23
+ * <content2>
24
+ *
25
+ * <task>
26
+ *
27
+ * Each context block is separated from the next (and from `task`) by a
28
+ * single blank line. No implicit `[Task]` header is emitted — that framing
29
+ * is the driver's responsibility (e.g. opencode's `agent_profile` wrapping).
30
+ * Emitting one here would compose incorrectly with any driver that also
31
+ * adds a `[Task]` header, producing a double header that some models
32
+ * (observed with `opencode/big-pickle`) misread as a cut-off message.
33
+ */
34
+ export function serializePromptDocument(doc: PromptDocument): string {
35
+ if (doc.contexts.length === 0) return doc.task;
36
+ const blocks = doc.contexts.map((c) => `[${c.label}]\n${c.content}`);
37
+ return `${blocks.join('\n\n')}\n\n${doc.task}`;
38
+ }
39
+
40
+ /**
41
+ * Helper for middlewares: return a new document with the given block
42
+ * appended to `contexts`, preserving immutability of `doc`.
43
+ */
44
+ export function appendContext(
45
+ doc: PromptDocument,
46
+ block: PromptContextBlock,
47
+ ): PromptDocument {
48
+ return { contexts: [...doc.contexts, block], task: doc.task };
49
+ }