(
provider: SyncProvider<SourceModel>,
options: SyncOptions = {},
)
| 126 | } |
| 127 | |
| 128 | export async function syncProvider<SourceModel>( |
| 129 | provider: SyncProvider<SourceModel>, |
| 130 | options: SyncOptions = {}, |
| 131 | ): Promise<SyncResult> { |
| 132 | console.log(`\nSyncing ${provider.name}...`); |
| 133 | |
| 134 | const existingState = await readExisting(provider.modelsDir); |
| 135 | const { models: existing, brokenSymlinks } = existingState; |
| 136 | let { modelMetadata } = existingState; |
| 137 | const sourceModels = provider.parseModels(await provider.fetchModels()); |
| 138 | const desired = new Map<string, { model: z.infer<typeof SyncedAuthoredModel>; content: string }>(); |
| 139 | const desiredMetadata = new Map<string, { model: z.infer<typeof ModelMetadata>; content: string }>(); |
| 140 | const skippedRemote: string[] = []; |
| 141 | |
| 142 | for (const sourceModel of sourceModels) { |
| 143 | const translated = provider.translateModel(sourceModel, { |
| 144 | existing(id) { |
| 145 | return existing.get(`${id}.toml`)?.toml; |
| 146 | }, |
| 147 | authored(id) { |
| 148 | return existing.get(`${id}.toml`)?.authored; |
| 149 | }, |
| 150 | }); |
| 151 | if (translated === undefined) { |
| 152 | if (provider.sourceID !== undefined) skippedRemote.push(provider.sourceID(sourceModel)); |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | const relativePath = `${translated.id}.toml`; |
| 157 | if (provider.skipCreates && !existing.has(relativePath)) { |
| 158 | skippedRemote.push(translated.id); |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | if (desired.has(relativePath)) { |
| 163 | throw new Error(`Duplicate synced model path: ${provider.id}/${relativePath}`); |
| 164 | } |
| 165 | |
| 166 | if (translated.metadata !== undefined) { |
| 167 | const parsedMetadata = ModelMetadata.safeParse({ |
| 168 | id: translated.metadata.id, |
| 169 | ...stripUndefined(translated.metadata.model), |
| 170 | }); |
| 171 | if (!parsedMetadata.success) { |
| 172 | parsedMetadata.error.cause = { provider: provider.id, metadata: translated.metadata.id }; |
| 173 | throw parsedMetadata.error; |
| 174 | } |
| 175 | const metadataPath = `${translated.metadata.id}.toml`; |
| 176 | if (desiredMetadata.has(metadataPath)) throw new Error(`Duplicate synced metadata path: ${metadataPath}`); |
| 177 | desiredMetadata.set(metadataPath, { |
| 178 | model: parsedMetadata.data, |
| 179 | content: formatMetadataToml(parsedMetadata.data), |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | const translatedModel = provider.preserveBaseModels === false |
| 184 | ? translated.model |
| 185 | : preserveBaseModel(translated.model, existing.get(relativePath)?.authored); |
no test coverage detected