|
1 | | -import path from 'node:path'; |
2 | | - |
3 | 1 | import type { EvaluationResult } from '@agentv/core'; |
4 | 2 |
|
5 | | -import { HtmlWriter } from './html-writer.js'; |
6 | | -import { JsonWriter } from './json-writer.js'; |
7 | 3 | import { JsonlWriter } from './jsonl-writer.js'; |
8 | | -import { JunitWriter } from './junit-writer.js'; |
9 | | -import { YamlWriter } from './yaml-writer.js'; |
10 | | - |
11 | | -export type OutputFormat = 'jsonl' | 'yaml' | 'html'; |
12 | 4 |
|
13 | 5 | export interface OutputWriter { |
14 | 6 | append(result: EvaluationResult): Promise<void>; |
15 | 7 | close(): Promise<void>; |
16 | 8 | } |
17 | 9 |
|
18 | | -export interface WriterOptions { |
19 | | - readonly threshold?: number; |
20 | | -} |
21 | | - |
22 | 10 | export async function createOutputWriter( |
23 | 11 | filePath: string, |
24 | | - format: OutputFormat, |
25 | 12 | options?: { append?: boolean }, |
26 | 13 | ): Promise<OutputWriter> { |
27 | | - switch (format) { |
28 | | - case 'jsonl': |
29 | | - return JsonlWriter.open(filePath, { append: options?.append }); |
30 | | - case 'yaml': |
31 | | - return YamlWriter.open(filePath); |
32 | | - case 'html': |
33 | | - return HtmlWriter.open(filePath); |
34 | | - default: { |
35 | | - const exhaustiveCheck: never = format; |
36 | | - throw new Error(`Unsupported output format: ${exhaustiveCheck}`); |
37 | | - } |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -const SUPPORTED_EXTENSIONS = new Set(['.jsonl', '.json', '.xml', '.yaml', '.yml', '.html', '.htm']); |
42 | | - |
43 | | -export function createWriterFromPath( |
44 | | - filePath: string, |
45 | | - options?: WriterOptions, |
46 | | -): Promise<OutputWriter> { |
47 | | - const ext = path.extname(filePath).toLowerCase(); |
48 | | - switch (ext) { |
49 | | - case '.jsonl': |
50 | | - return JsonlWriter.open(filePath); |
51 | | - case '.json': |
52 | | - return JsonWriter.open(filePath); |
53 | | - case '.xml': |
54 | | - return JunitWriter.open(filePath, { threshold: options?.threshold }); |
55 | | - case '.yaml': |
56 | | - case '.yml': |
57 | | - return YamlWriter.open(filePath); |
58 | | - case '.html': |
59 | | - case '.htm': |
60 | | - return HtmlWriter.open(filePath); |
61 | | - default: |
62 | | - throw new Error( |
63 | | - `Unsupported output file extension "${ext}". Supported: ${[...SUPPORTED_EXTENSIONS].join(', ')}`, |
64 | | - ); |
65 | | - } |
66 | | -} |
67 | | - |
68 | | -export async function createMultiWriter( |
69 | | - filePaths: readonly string[], |
70 | | - options?: WriterOptions, |
71 | | -): Promise<OutputWriter> { |
72 | | - const writers = await Promise.all(filePaths.map((fp) => createWriterFromPath(fp, options))); |
73 | | - return { |
74 | | - async append(result: EvaluationResult): Promise<void> { |
75 | | - await Promise.all(writers.map((w) => w.append(result))); |
76 | | - }, |
77 | | - async close(): Promise<void> { |
78 | | - await Promise.all(writers.map((w) => w.close())); |
79 | | - }, |
80 | | - }; |
| 14 | + return JsonlWriter.open(filePath, { append: options?.append }); |
81 | 15 | } |
0 commit comments