File size: 4,409 Bytes
9853396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node

const https = require('https');
const fs = require('fs');
const path = require('path');

const SOURCE_URL = 'https://raw.githubusercontent.com/ThinkInAIXYZ/PublicProviderConf/refs/heads/dev/dist/all.json';
const CONSTANTS_PATH = path.join(__dirname, '../../frontend/src/features/models/data/constants.ts');
const OUTPUT_PATH = path.join(__dirname, '../../frontend/src/features/models/data/providers.json');
const MODELS_JSON_PATH = path.join(__dirname, './models.json');

function fetchJSON(url) {
  return new Promise((resolve, reject) => {
    https.get(url, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        try {
          resolve(JSON.parse(data));
        } catch (e) {
          reject(new Error(`Failed to parse JSON: ${e.message}`));
        }
      });
    }).on('error', (err) => {
      reject(err);
    });
  });
}

function extractDeveloperIds(constantsPath) {
  const content = fs.readFileSync(constantsPath, 'utf8');
  const match = content.match(/export const DEVELOPER_IDS = \[([\s\S]*?)\]/);
  
  if (!match) {
    throw new Error('Could not find DEVELOPER_IDS in constants.ts');
  }
  
  const idsString = match[1];
  const ids = idsString
    .split(',')
    .map(line => line.trim())
    .filter(line => line.startsWith("'") || line.startsWith('"'))
    .map(line => line.replace(/^['"]|['"]$/g, ''));
  
  return ids;
}

function filterProviders(data, allowedIds) {
  if (!data.providers) {
    throw new Error('Invalid data structure: missing providers field');
  }
  
  const filtered = {};
  
  for (const [key, value] of Object.entries(data.providers)) {
    if (allowedIds.includes(value.id)) {
      filtered[key] = value;
    }
  }
  
  return { providers: filtered };
}

function sortModelsByDate(data) {
  for (const provider of Object.values(data.providers)) {
    if (provider.models && Array.isArray(provider.models)) {
      provider.models.sort((a, b) => {
        const dateA = a.release_date ? new Date(a.release_date) : new Date(0);
        const dateB = b.release_date ? new Date(b.release_date) : new Date(0);
        return dateB - dateA;
      });
    }
  }
  return data;
}

function mergeWithModelsJson(data, modelsJsonPath) {
  if (!fs.existsSync(modelsJsonPath)) {
    console.log('models.json does not exist, skipping merge');
    return data;
  }

  console.log('Merging with models.json...');
  const modelsJson = JSON.parse(fs.readFileSync(modelsJsonPath, 'utf8'));

  for (const [providerKey, models] of Object.entries(modelsJson)) {
    if (data.providers[providerKey]) {
      const existingProvider = data.providers[providerKey];
      if (!existingProvider.models) {
        existingProvider.models = [];
      }
      
      const existingIds = new Set(existingProvider.models.map(m => m.id));
      
      for (const model of models) {
        if (!existingIds.has(model.id)) {
          existingProvider.models.push(model);
          existingIds.add(model.id);
        }
      }
    } else {
      data.providers[providerKey] = {
        id: providerKey,
        models: models
      };
    }
  }

  return data;
}

async function main() {
  try {
    console.log('Fetching model developers data from:', SOURCE_URL);
    const data = await fetchJSON(SOURCE_URL);
    
    console.log('Extracting allowed developer IDs from:', CONSTANTS_PATH);
    const allowedIds = extractDeveloperIds(CONSTANTS_PATH);
    console.log('Allowed developer IDs:', allowedIds);
    
    console.log('Filtering providers...');
    const filtered = filterProviders(data, allowedIds);
    
    const providerCount = Object.keys(filtered.providers).length;
    console.log(`Filtered to ${providerCount} providers`);
    
    console.log('Merging with models.json...');
    mergeWithModelsJson(filtered, MODELS_JSON_PATH);
    
    console.log('Sorting models by release date...');
    sortModelsByDate(filtered);
    
    console.log('Writing to:', OUTPUT_PATH);
    fs.writeFileSync(OUTPUT_PATH, JSON.stringify(filtered, null, 2) + '\n');
    
    console.log('Sync completed successfully!');
  } catch (error) {
    console.error('Error during sync:', error.message);
    process.exit(1);
  }
}

main();