Open History to see the last commit
1import { cachedDocs } from './model-facts.ts'
2import { fetchText, sha256Text } from './types.ts'
3import type { ProviderConfig } from './types.ts'
4import {
5 applyCnCardFacts,
6 applyCnCatalog,
7 applyCnTextFacts,
8 cnPriceModels,
9 DASHSCOPE_CN_MODELS,
10 DASHSCOPE_CN_PRICES,
11} from './dashscope-cn-docs.ts'
12import type { CnDoc } from './dashscope-cn-docs.ts'
13
14export const DASHSCOPE_CN_TEXT =
15 'https://help.aliyun.com/zh/model-studio/text-generation-model.md'
16export const DASHSCOPE_CN_FLASH =
17 'https://help.aliyun.com/zh/model-studio/qwen-flash.md'
18const EXPECTED_TITLES = [
19 '模型调用价格',
20 '选择模型',
21 '文本生成',
22 'qwen-flash',
23] as const
24const URLS = [
25 DASHSCOPE_CN_PRICES,
26 DASHSCOPE_CN_MODELS,
27 DASHSCOPE_CN_TEXT,
28 DASHSCOPE_CN_FLASH,
29] as const
30async function load(kv?: KVNamespace) {
31 return cachedDocs(kv, DASHSCOPE_CN_PRICES, async () => {
32 const docs = await Promise.all(
33 URLS.map(async (url, index): Promise<CnDoc> => {
34 const text = await fetchText(url, {
35 signal: AbortSignal.timeout(30_000),
36 })
37 if (
38 /<!doctype|<html\b/i.test(text) ||
39 /^#\s+([^\r\n]+)/m.exec(text)?.[1]?.trim() !== EXPECTED_TITLES[index]
40 )
41 throw new Error(`dashscope-cn: malformed native source ${url}`)
42 return { text, url, hash: await sha256Text(text) }
43 }),
44 )
45 const [prices, catalog, textModels, flash] = docs
46 if (!prices || !catalog || !textModels || !flash)
47 throw new Error('dashscope-cn: missing native source')
48 const models = applyCnCardFacts(
49 applyCnTextFacts(
50 applyCnCatalog(cnPriceModels(prices), catalog),
51 textModels,
52 ),
53 flash,
54 )
55 return { docs, models }
56 })
57}
58export const provider: ProviderConfig = {
59 id: 'dashscope-cn',
60 displayName: 'Alibaba Cloud Model Studio (China)',
61 defaultDerivation: 'docs-derived',
62 modelsEndpoint: DASHSCOPE_CN_MODELS,
63 specSourceUrl: DASHSCOPE_CN_MODELS,
64 bindSyncedRoutesOnly: true,
65 async listModels(_env, kv) {
66 return { models: (await load(kv)).models }
67 },
68 async fetchSpec() {
69 const { docs } = await load()
70 return {
71 specs: [],
72 sources: docs.map(({ url, hash }) => ({ url, hash })),
73 outputStrategy: 'post-200',
74 skipped:
75 'dashscope-cn: native Beijing docs publish public model facts and RMB prices, but no sourced complete API body schema has been extracted — skipped',
76 }
77 },
78 classify: () => null,
79}