@crewhaus/runtime-core 0.1.3 → 0.1.5

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,190 +0,0 @@
1
- /**
2
- * Streaming markdown → ANSI renderer for CLI target stdout output.
3
- *
4
- * Activated when `CREWHAUS_CLI_MARKDOWN=1` is set. Wraps the model's
5
- * text-delta stream so the user sees rendered markdown (bold, italic,
6
- * headers, lists, code fences, inline code) instead of raw asterisks.
7
- *
8
- * Design notes:
9
- * - **Line-buffered**: chunks accumulate until a `\n` arrives, then the
10
- * full line renders and flushes. Trades per-character streaming feel
11
- * for stable rendering — the model can split a `**` marker across
12
- * chunks, so per-character pass-through would emit half-tokens.
13
- * - **Stateful**: tracks code-fence state across lines so multi-line
14
- * ```code blocks``` render as a unit (dim color, optional language tag).
15
- * - **Zero deps**: pure ANSI escape codes; works in any terminal that
16
- * supports VT100 (essentially every terminal in active use).
17
- *
18
- * Limitations (acceptable for v1; document in demo READMEs):
19
- * - Per-line buffering means responses appear in line chunks, not
20
- * character-by-character. For most chat responses (< 20 lines) this
21
- * is imperceptible. Long generations may feel slightly chunked.
22
- * - Nested markup (e.g. `**bold *italic*?**`) renders the outermost
23
- * correctly; nested handling is best-effort.
24
- * - No table rendering, no image alt rendering, no link highlighting
25
- * (links pass through as plain text — terminals already
26
- * auto-linkify URLs).
27
- */
28
-
29
- const RESET = "\x1b[0m";
30
- const BOLD = "\x1b[1m";
31
- const DIM = "\x1b[2m";
32
- const ITALIC = "\x1b[3m";
33
- const CYAN = "\x1b[36m";
34
- const MAGENTA = "\x1b[35m";
35
- const YELLOW = "\x1b[33m";
36
- const GREEN = "\x1b[32m";
37
-
38
- /** Heading levels 1..6 → color + bold. Level 0 is "no heading". */
39
- const HEADING_COLORS: readonly string[] = [
40
- "",
41
- `${BOLD}${CYAN}`, // h1
42
- `${BOLD}${MAGENTA}`, // h2
43
- `${BOLD}${YELLOW}`, // h3
44
- BOLD, // h4
45
- BOLD, // h5
46
- BOLD, // h6
47
- ];
48
-
49
- export interface StreamRenderer {
50
- /** Push an incoming text chunk. Renders + flushes completed lines. */
51
- push(chunk: string): void;
52
- /** Flush any remaining buffered content (no newline appended). */
53
- end(): void;
54
- }
55
-
56
- export interface RendererOptions {
57
- /** Override the output sink. Defaults to `process.stdout.write`. */
58
- readonly write?: (s: string) => void;
59
- }
60
-
61
- /**
62
- * Construct a streaming markdown renderer. Returns a `StreamRenderer` with
63
- * `push(chunk)` and `end()` methods.
64
- *
65
- * Usage:
66
- * ```ts
67
- * const r = createCliMarkdownRenderer();
68
- * r.push("hello **world**\n");
69
- * r.push("`code` ");
70
- * r.push("done\n");
71
- * r.end();
72
- * ```
73
- */
74
- export function createCliMarkdownRenderer(opts: RendererOptions = {}): StreamRenderer {
75
- const write = opts.write ?? ((s: string) => process.stdout.write(s));
76
- let buf = "";
77
- let inCodeFence = false;
78
- let codeFenceLang = "";
79
-
80
- function flushLine(line: string): void {
81
- write(`${renderLine(line)}\n`);
82
- }
83
-
84
- function renderLine(line: string): string {
85
- // Code-fence delimiter — toggles state, dims the fence itself.
86
- const fence = /^(\s*)```\s*(\S*)\s*$/.exec(line);
87
- if (fence) {
88
- if (!inCodeFence) {
89
- inCodeFence = true;
90
- codeFenceLang = fence[2] ?? "";
91
- return `${fence[1] ?? ""}${DIM}${GREEN}\`\`\`${codeFenceLang}${RESET}`;
92
- }
93
- inCodeFence = false;
94
- const langSuffix = codeFenceLang ? ` (${codeFenceLang})` : "";
95
- codeFenceLang = "";
96
- return `${fence[1] ?? ""}${DIM}${GREEN}\`\`\`${langSuffix}${RESET}`;
97
- }
98
- if (inCodeFence) {
99
- // Inside a fenced block — dim everything; preserve indentation.
100
- return `${DIM}${line}${RESET}`;
101
- }
102
- // Headings
103
- const heading = /^(\s*)(#{1,6})\s+(.*)$/.exec(line);
104
- if (heading) {
105
- const indent = heading[1] ?? "";
106
- const level = (heading[2] ?? "").length;
107
- const text = heading[3] ?? "";
108
- const color = HEADING_COLORS[level] ?? BOLD;
109
- return `${indent}${color}${renderInline(text)}${RESET}`;
110
- }
111
- // Unordered list bullets — replace marker with a pretty bullet.
112
- const bullet = /^(\s*)[-*+]\s+(.*)$/.exec(line);
113
- if (bullet) {
114
- return `${bullet[1] ?? ""}${CYAN}•${RESET} ${renderInline(bullet[2] ?? "")}`;
115
- }
116
- // Ordered list — keep the number, color it.
117
- const numbered = /^(\s*)(\d+)\.\s+(.*)$/.exec(line);
118
- if (numbered) {
119
- return `${numbered[1] ?? ""}${CYAN}${numbered[2]}.${RESET} ${renderInline(numbered[3] ?? "")}`;
120
- }
121
- // Blockquote
122
- const quote = /^(\s*)>\s?(.*)$/.exec(line);
123
- if (quote) {
124
- return `${quote[1] ?? ""}${DIM}│${RESET} ${ITALIC}${renderInline(quote[2] ?? "")}${RESET}`;
125
- }
126
- // Horizontal rule
127
- if (/^\s*(-{3,}|_{3,}|\*{3,})\s*$/.test(line)) {
128
- return `${DIM}${"─".repeat(40)}${RESET}`;
129
- }
130
- return renderInline(line);
131
- }
132
-
133
- function renderInline(text: string): string {
134
- // Backtick-quoted inline code first (so its content isn't bold/italic-mangled).
135
- // We do a placeholder-swap pass: extract code spans, then re-insert after styling.
136
- const codeSpans: string[] = [];
137
- let working = text.replace(/`([^`]+?)`/g, (_match, content) => {
138
- codeSpans.push(content as string);
139
- return ` CODE${codeSpans.length - 1} `;
140
- });
141
- // Bold: **text** or __text__ — non-greedy, doesn't span newlines.
142
- working = working.replace(/\*\*([^*\n]+?)\*\*/g, (_m, t) => `${BOLD}${t}${RESET}`);
143
- working = working.replace(/__([^_\n]+?)__/g, (_m, t) => `${BOLD}${t}${RESET}`);
144
- // Italic: *text* or _text_ — guarded by word-boundary lookarounds so
145
- // list bullets ("- foo") and snake_case identifiers don't false-match.
146
- working = working.replace(
147
- /(^|[^*\w])\*([^*\n]+?)\*(?!\w)/g,
148
- (_m, p, t) => `${p}${ITALIC}${t}${RESET}`,
149
- );
150
- working = working.replace(
151
- /(^|[^_\w])_([^_\n]+?)_(?!\w)/g,
152
- (_m, p, t) => `${p}${ITALIC}${t}${RESET}`,
153
- );
154
- // Restore inline code spans with styling.
155
- working = working.replace(/ CODE(\d+) /g, (_m, idx) => {
156
- const content = codeSpans[Number(idx)] ?? "";
157
- return `${DIM}${CYAN}\`${content}\`${RESET}`;
158
- });
159
- return working;
160
- }
161
-
162
- return {
163
- push(chunk: string): void {
164
- buf += chunk;
165
- let nl = buf.indexOf("\n");
166
- while (nl !== -1) {
167
- const line = buf.slice(0, nl);
168
- buf = buf.slice(nl + 1);
169
- flushLine(line);
170
- nl = buf.indexOf("\n");
171
- }
172
- },
173
- end(): void {
174
- if (buf.length > 0) {
175
- // Tail content with no terminating newline — render in place.
176
- write(renderLine(buf));
177
- buf = "";
178
- }
179
- },
180
- };
181
- }
182
-
183
- /**
184
- * Decide whether to activate the CLI markdown renderer based on env.
185
- * Centralized so callers don't repeat the parse.
186
- */
187
- export function isCliMarkdownEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
188
- const v = env["CREWHAUS_CLI_MARKDOWN"];
189
- return v === "1" || v === "true";
190
- }
@@ -1,58 +0,0 @@
1
- /**
2
- * Unit tests for `formatCostInlineLine` — the CLI-target cost ribbon
3
- * emitted when `CREWHAUS_COST_INLINE=1` and `CREWHAUS_COST_TRACKING=1`
4
- * are both set. The integration path is exercised by the T3 trace-event
5
- * test in observability.test.ts.
6
- */
7
- import { describe, expect, test } from "bun:test";
8
- import type { CostAccrualEvent } from "@crewhaus/trace-event-bus";
9
- import { formatCostInlineLine } from "./observability";
10
-
11
- function makeEvent(overrides: Partial<CostAccrualEvent> = {}): CostAccrualEvent {
12
- return {
13
- kind: "cost_accrual",
14
- traceId: "trace_a",
15
- runId: "run_a",
16
- sessionId: "sess_0000000000000001",
17
- timestamp: 0,
18
- provider: "anthropic",
19
- modelId: "claude-sonnet-4-6",
20
- inputTokens: 12_345,
21
- outputTokens: 678,
22
- cachedReadTokens: 0,
23
- costUsdMicros: 4_200, // $0.0042
24
- ...overrides,
25
- } as CostAccrualEvent;
26
- }
27
-
28
- describe("formatCostInlineLine", () => {
29
- test("renders the dollar amount with 4-digit precision", () => {
30
- expect(formatCostInlineLine(makeEvent({ costUsdMicros: 4_200 }))).toContain("$0.0042");
31
- expect(formatCostInlineLine(makeEvent({ costUsdMicros: 1_500_000 }))).toContain("$1.5000");
32
- expect(formatCostInlineLine(makeEvent({ costUsdMicros: 0 }))).toContain("$0.0000");
33
- });
34
-
35
- test("formats token counts above 1k with one decimal place", () => {
36
- const line = formatCostInlineLine(makeEvent({ inputTokens: 12_345, outputTokens: 678 }));
37
- expect(line).toContain("12.3k in");
38
- expect(line).toContain("678 out"); // below 1k stays raw
39
- });
40
-
41
- test("includes the model id for visibility on model swaps", () => {
42
- const claudeLine = formatCostInlineLine(makeEvent({ modelId: "claude-opus-4-7" }));
43
- expect(claudeLine).toContain("model=claude-opus-4-7");
44
-
45
- const gptLine = formatCostInlineLine(makeEvent({ modelId: "gpt-4o-2024-11-20" }));
46
- expect(gptLine).toContain("model=gpt-4o-2024-11-20");
47
- });
48
-
49
- test("output is a single line (no embedded newlines)", () => {
50
- const line = formatCostInlineLine(makeEvent());
51
- expect(line).not.toContain("\n");
52
- });
53
-
54
- test("starts with the dollar emoji marker so users can grep for it", () => {
55
- const line = formatCostInlineLine(makeEvent());
56
- expect(line.startsWith("[💸 ")).toBe(true);
57
- });
58
- });
@@ -1,138 +0,0 @@
1
- /**
2
- * Regression — Section 27 cost tracking over the live `runChatLoop` path
3
- * with provider-prefixed spec model strings.
4
- *
5
- * `model_response` used to publish the raw spec string
6
- * (`"bedrock/us.anthropic.claude-…"`, `"openai/gpt-4o"`); cost-tracker
7
- * pricing keys are bare wire model ids, so every prefixed run was a
8
- * silent pricing miss charged $0 — only unprefixed `claude-*` strings
9
- * priced. These tests run the real loop (stub adapter) against a
10
- * cost-tracker on the run bus and assert non-zero cost, zero misses, and
11
- * the wire-id/spec-string split on the published events.
12
- */
13
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
14
- import { mkdtempSync, rmSync } from "node:fs";
15
- import { tmpdir } from "node:os";
16
- import { join } from "node:path";
17
- import type { ProviderAdapter } from "@crewhaus/adapter-anthropic";
18
- import { createCostTracker } from "@crewhaus/cost-tracker";
19
- import { createRunContext } from "@crewhaus/run-context";
20
- import type {
21
- CostAccrualEvent,
22
- ModelResponseEvent,
23
- ProviderId,
24
- TraceEvent,
25
- } from "@crewhaus/trace-event-bus";
26
- import { runChatLoop } from "./index";
27
-
28
- const SHARED_SESSION_ROOT = mkdtempSync(join(tmpdir(), "crewhaus-cost-wire-model-tests-"));
29
- beforeAll(() => {
30
- process.env["CREWHAUS_SESSION_DIR"] = SHARED_SESSION_ROOT;
31
- });
32
- afterAll(() => {
33
- process.env["CREWHAUS_SESSION_DIR"] = undefined;
34
- rmSync(SHARED_SESSION_ROOT, { recursive: true, force: true });
35
- });
36
-
37
- /** Single text-only turn: 100 input tokens, 10 output tokens. */
38
- function makeTextAdapter(providerId: ProviderId): ProviderAdapter {
39
- return {
40
- providerId,
41
- features: {
42
- caching: "explicit",
43
- tool_use: true,
44
- vision: true,
45
- thinking: false,
46
- web_search: false,
47
- },
48
- estimateTokens: () => 0,
49
- stream: () =>
50
- (async function* () {
51
- yield { kind: "message_start", usage: { input: 100, output: 0 } } as const;
52
- yield {
53
- kind: "content_block_start",
54
- index: 0,
55
- block: { type: "text", text: "" },
56
- } as const;
57
- yield {
58
- kind: "content_block_delta",
59
- index: 0,
60
- delta: { type: "text_delta", text: "ok" },
61
- } as const;
62
- yield { kind: "content_block_stop", index: 0 } as const;
63
- yield {
64
- kind: "message_delta",
65
- stopReason: "end_turn",
66
- usage: { input: 100, output: 10 },
67
- } as const;
68
- yield { kind: "message_stop" } as const;
69
- })(),
70
- };
71
- }
72
-
73
- async function runOnce(model: string, providerId: ProviderId) {
74
- const runContext = createRunContext();
75
- const seen: TraceEvent[] = [];
76
- runContext.eventBus.subscribe((e) => {
77
- seen.push(e);
78
- });
79
- const tracker = createCostTracker(runContext.eventBus);
80
- await runChatLoop({
81
- model,
82
- instructions: "test",
83
- _adapter: makeTextAdapter(providerId),
84
- runContext,
85
- singleTurn: true,
86
- seedMessages: [{ role: "user", content: "hello" }],
87
- });
88
- tracker.unsubscribe();
89
- const responses = seen.filter((e): e is ModelResponseEvent => e.kind === "model_response");
90
- const accruals = seen.filter((e): e is CostAccrualEvent => e.kind === "cost_accrual");
91
- return { tracker, responses, accruals, runId: runContext.eventBus.runId };
92
- }
93
-
94
- describe("cost tracking with provider-prefixed spec model strings", () => {
95
- test("bedrock inference-profile spec string prices — no $0 pricing miss", async () => {
96
- const spec = "bedrock/us.anthropic.claude-sonnet-4-5-20250929-v1:0";
97
- const wire = "us.anthropic.claude-sonnet-4-5-20250929-v1:0";
98
- const { tracker, responses, accruals, runId } = await runOnce(spec, "bedrock");
99
-
100
- expect(responses.length).toBe(1);
101
- expect(responses[0]?.model).toBe(wire);
102
- expect(responses[0]?.specModel).toBe(spec);
103
-
104
- expect(tracker.pricingMisses()).toBe(0);
105
- expect(tracker.observed()).toBe(1);
106
- // anthropic.claude-sonnet-4 row: 100 in × $3/M + 10 out × $15/M = 450 micros
107
- expect(tracker.getRunCost(runId).totalUsdMicros).toBe(450);
108
- expect(tracker.getRunCost(runId).byProvider.bedrock).toBe(450);
109
-
110
- expect(accruals.length).toBe(1);
111
- expect(accruals[0]?.modelId).toBe(wire);
112
- expect(accruals[0]?.specModel).toBe(spec);
113
- expect(accruals[0]?.costUsdMicros).toBe(450);
114
- });
115
-
116
- test("openai/* spec string prices — no $0 pricing miss", async () => {
117
- const { tracker, responses, accruals, runId } = await runOnce("openai/gpt-4o", "openai");
118
-
119
- expect(responses[0]?.model).toBe("gpt-4o");
120
- expect(responses[0]?.specModel).toBe("openai/gpt-4o");
121
-
122
- expect(tracker.pricingMisses()).toBe(0);
123
- // gpt-4o row: 100 in × $2.5/M + 10 out × $10/M = 350 micros
124
- expect(tracker.getRunCost(runId).byProvider.openai).toBe(350);
125
- expect(accruals[0]?.modelId).toBe("gpt-4o");
126
- });
127
-
128
- test("unprefixed claude-* spec string still prices; no specModel published", async () => {
129
- const { tracker, responses, accruals, runId } = await runOnce("claude-sonnet-4-6", "anthropic");
130
-
131
- expect(responses[0]?.model).toBe("claude-sonnet-4-6");
132
- expect(responses[0]?.specModel).toBeUndefined();
133
-
134
- expect(tracker.pricingMisses()).toBe(0);
135
- expect(tracker.getRunCost(runId).byProvider.anthropic).toBe(450);
136
- expect(accruals[0]?.specModel).toBeUndefined();
137
- });
138
- });