-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkspace-parser.ts
More file actions
69 lines (58 loc) · 1.91 KB
/
Copy pathworkspace-parser.ts
File metadata and controls
69 lines (58 loc) · 1.91 KB
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { readFile } from 'node:fs/promises';
import { load } from 'js-yaml';
import {
WorkspaceConfigSchema,
type WorkspaceConfig,
} from '../models/workspace-config.js';
import { CONFIG_DIR, WORKSPACE_CONFIG_FILE } from '../constants.js';
const configName = `${CONFIG_DIR}/${WORKSPACE_CONFIG_FILE}`;
/**
* Parse and validate .allagents/workspace.yaml file
* @param path - Path to .allagents/workspace.yaml file
* @returns Validated WorkspaceConfig
* @throws Error if file doesn't exist, is invalid YAML, or fails validation
*/
export async function parseWorkspaceConfig(
path: string,
): Promise<WorkspaceConfig> {
try {
// Read the YAML file
const content = await readFile(path, 'utf-8');
// Parse YAML
const parsed = load(content);
if (!parsed) {
throw new Error(`${configName} is empty`);
}
// Validate with Zod schema
const result = WorkspaceConfigSchema.safeParse(parsed);
if (!result.success) {
const errors = result.error.errors.map(
(err) => ` - ${err.path.join('.')}: ${err.message}`,
);
throw new Error(
`${configName} validation failed:\n${errors.join('\n')}`,
);
}
return result.data;
} catch (error) {
if (error instanceof Error) {
// Re-throw validation errors as-is
if (error.message.includes('validation failed')) {
throw error;
}
// Handle file not found
if ('code' in error && error.code === 'ENOENT') {
throw new Error(
`${configName} not found at ${path}\n Run 'allagents workspace init <path>' to create a new workspace`,
);
}
// Handle YAML parsing errors
if (error.message.includes('YAMLException')) {
throw new Error(`Invalid YAML in ${configName}: ${error.message}`);
}
// Re-throw other errors
throw error;
}
throw new Error(`Unknown error parsing ${configName}: ${String(error)}`);
}
}