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 { parseGlmCodingModels } from './glm-coding-docs.ts'
5import type { CodingDoc, CodingLocale } from './glm-coding-docs.ts'
6import {
7 applyReplay,
8 parseGlmReplay,
9 ZAI_THINKING_MODE_URL,
10 ZHIPU_THINKING_MODE_URL,
11} from './provider-replay.ts'
12
13export interface CodingSources {
14 overview: string
15 latest: string
16 thinking: string
17 replay: string
18}
19export const ZAI_CODING_SOURCES: CodingSources = {
20 overview: 'https://docs.z.ai/devpack/overview.md',
21 latest: 'https://docs.z.ai/devpack/latest-model.md',
22 thinking: 'https://docs.z.ai/guides/capabilities/thinking.md',
23 replay: ZAI_THINKING_MODE_URL,
24}
25export const ZHIPU_CODING_SOURCES: CodingSources = {
26 overview: 'https://docs.bigmodel.cn/cn/coding-plan/overview.md',
27 latest: 'https://docs.bigmodel.cn/cn/coding-plan/latest-model.md',
28 thinking: 'https://docs.bigmodel.cn/cn/guide/capabilities/thinking.md',
29 replay: ZHIPU_THINKING_MODE_URL,
30}
31export function parseCodingReplay(
32 doc: CodingDoc,
33 locale: CodingLocale,
34): Set<string> {
35 const section = doc.text
36 .split(/^## /m)
37 .find((part) => /^(?:\*\*)?Preserved thinking|^保留式思考/.test(part))
38 const prose =
39 section?.replace(/\\_/g, '_').replace(/[*`]/g, '').replace(/\s+/g, ' ') ??
40 ''
41 const scoped =
42 locale === 'en'
43 ? /enabled by default.{0,80}Coding Plan endpoint/.test(prose) &&
44 /must return the complete.{0,40}unmodified reasoning_content back to the API/.test(
45 prose,
46 )
47 : /Coding Plan 端点默认开启/.test(prose) &&
48 /需要将完整、未修改的 reasoning content 传回 API/.test(prose)
49 if (!scoped)
50 throw new Error('glm coding: no native Coding Plan replay contract')
51 return new Set(parseGlmReplay(doc.text))
52}
53export function glmCodingProvider(
54 id: string,
55 displayName: string,
56 locale: CodingLocale,
57 urls: CodingSources,
58): ProviderConfig {
59 async function load(kv?: KVNamespace) {
60 return cachedDocs(kv, urls.overview, async () => {
61 const doc = async (url: string): Promise<CodingDoc> => {
62 const text = await fetchText(url, {
63 signal: AbortSignal.timeout(30_000),
64 })
65 return { url, text, hash: await sha256Text(text) }
66 }
67 const [overview, latest, thinking, replay] = await Promise.all([
68 doc(urls.overview),
69 doc(urls.latest),
70 doc(urls.thinking),
71 doc(urls.replay),
72 ])
73 return { overview, latest, thinking, replay }
74 })
75 }
76 return {
77 id,
78 displayName,
79 specSourceUrl: urls.latest,
80 modelsEndpoint: urls.overview,
81 defaultDerivation: 'docs-derived',
82 bindSyncedRoutesOnly: true,
83 async listModels(_env, kv) {
84 const docs = await load(kv)
85 const models = parseGlmCodingModels(docs, locale)
86 const replayIds = parseCodingReplay(docs.replay, locale)
87 return {
88 models: models.map((model) =>
89 replayIds.has(model.rawId)
90 ? applyReplay(model, {
91 derivation: 'docs-derived',
92 sourceUrl: docs.replay.url,
93 sourceHash: docs.replay.hash,
94 path: 'Preserved thinking: Coding Plan reasoning_content replay',
95 })
96 : model,
97 ),
98 }
99 },
100 async fetchSpec() {
101 const docs = await load()
102 parseGlmCodingModels(docs, locale)
103 parseCodingReplay(docs.replay, locale)
104 return {
105 specs: [],
106 ...(id === 'zhipuai-coding-plan'
107 ? {
108 withdrawnSchemaSources: [
109 'https://docs.bigmodel.cn/openapi/openapi.json',
110 ],
111 }
112 : {}),
113 sources: Object.values(docs).map((doc) => ({
114 url: doc.url,
115 hash: doc.hash,
116 })),
117 outputStrategy: 'post-200',
118 skipped: `${id}: own Coding docs state model facts and effort controls, but publish no complete Coding request/response schema — skipped`,
119 }
120 },
121 classify: () => null,
122 }
123}