File size: 2,033 Bytes
60a8358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { requestHeaders } from './headers.js';

const BASE_URL = 'https://chat.qwen.ai';
let cachedModels = null;
let cacheTime = 0;
const CACHE_TTL = 30 * 60 * 1000; // 30 min

async function fetchModels(token) {
  const res = await fetch(`${BASE_URL}/api/models`, {
    headers: {
      'authorization': `Bearer ${token}`,
      ...requestHeaders(),
    },
  });
  const json = await res.json();
  return json.data || [];
}

export async function getModels(token) {
  if (cachedModels && Date.now() - cacheTime < CACHE_TTL) return cachedModels;
  cachedModels = await fetchModels(token);
  cacheTime = Date.now();
  return cachedModels;
}

export function clearModelCache() {
  cachedModels = null;
  cacheTime = 0;
}

export function handleOpenAIModels(modelList) {
  const variants = [];

  for (const m of modelList) {
    const meta = m.info?.meta || {};
    const caps = meta.capabilities || {};
    const chatTypes = meta.chat_type || [];

    const has = {
      vision: !!caps.vision,
      thinking: !!caps.thinking,
      search: !!caps.search,
      deep_research: chatTypes.includes('deep_research'),
      image_gen: chatTypes.includes('t2i'),
      video_gen: chatTypes.includes('t2v'),
      web_dev: chatTypes.includes('web_dev'),
      slides: chatTypes.includes('slides'),
    };

    const base = { object: 'model', created: 1700000000, owned_by: 'qwen', capabilities: has };

    // Base model
    variants.push({ id: m.id, ...base });

    // Suffixed variants based on capabilities
    if (has.thinking)       variants.push({ id: m.id + '-thinking', ...base });
    if (has.deep_research)  variants.push({ id: m.id + '-deep-research', ...base });
    if (has.image_gen)      variants.push({ id: m.id + '-image', ...base });
    if (has.video_gen)      variants.push({ id: m.id + '-video', ...base });
    if (has.web_dev)        variants.push({ id: m.id + '-webdev', ...base });
    if (has.slides)         variants.push({ id: m.id + '-slides', ...base });
  }

  return { object: 'list', data: variants };
}