| 124 | } |
| 125 | |
| 126 | function validateFile(value: unknown): WorkspaceMeta[] { |
| 127 | if (typeof value !== 'object' || value === null) { |
| 128 | throw new Error('workspaces.json: root must be an object'); |
| 129 | } |
| 130 | const v = value as Record<string, unknown>; |
| 131 | if (v['version'] !== 1) { |
| 132 | throw new Error(`workspaces.json: unsupported version ${String(v['version'])}`); |
| 133 | } |
| 134 | if (!Array.isArray(v['workspaces'])) { |
| 135 | throw new Error('workspaces.json: workspaces must be an array'); |
| 136 | } |
| 137 | return v['workspaces'].map((entry, i) => { |
| 138 | if (typeof entry !== 'object' || entry === null) { |
| 139 | throw new Error(`workspaces.json: entry ${i} is not an object`); |
| 140 | } |
| 141 | const e = entry as Record<string, unknown>; |
| 142 | if ( |
| 143 | typeof e['id'] !== 'string' || |
| 144 | typeof e['tag'] !== 'string' || |
| 145 | typeof e['dir'] !== 'string' || |
| 146 | typeof e['createdAt'] !== 'string' |
| 147 | ) { |
| 148 | throw new Error(`workspaces.json: entry ${i} has wrong shape`); |
| 149 | } |
| 150 | const agents = Array.isArray(e['agents']) |
| 151 | ? (e['agents'].filter((a): a is string => typeof a === 'string') as string[]) |
| 152 | : ['claude']; // legacy migration |
| 153 | const base: WorkspaceMeta = { |
| 154 | id: e['id'], |
| 155 | tag: e['tag'], |
| 156 | dir: e['dir'], |
| 157 | createdAt: e['createdAt'], |
| 158 | agents: agents.length > 0 ? agents : ['claude'], |
| 159 | }; |
| 160 | let withTemplate: WorkspaceMeta = typeof e['template'] === 'string' |
| 161 | ? { ...base, template: e['template'] } |
| 162 | : base; |
| 163 | if (typeof e['spawnedFromVersion'] === 'string') { |
| 164 | withTemplate = { ...withTemplate, spawnedFromVersion: e['spawnedFromVersion'] }; |
| 165 | } |
| 166 | return withTemplate; |
| 167 | }); |
| 168 | } |