Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

byteplus-tools.ts

113 lines3.7 KB

1/**
2 * BytePlus server-side tools (#409). The ModelArk model list has a "Tool
3 * use" table, one row per model, with an MCP column; the cloud-deployed /
4 * remote MCP guide's "Supported models" points at it. A `Supported` or
5 * `Yes` cell (the wording served on 2026-10-23) is the `mcp` tool type.
6 */
7import { fetchByteplusDoc } from './byteplus-docs.ts'
8import {
9 BYTEPLUS_MODEL_LIST_URL,
10 pricingContent,
11 tablesWith,
12} from './byteplus-pricing.ts'
13import type { ByteplusDoc } from './byteplus-pricing.ts'
14import { serverToolsSilent } from './fact-sources.ts'
15import { tryDocs, unavailable } from './model-facts.ts'
16import type { DocsRun } from './model-facts.ts'
17import { sha256Text } from './types.ts'
18import type { ModelInfo } from './types.ts'
19​
20/**
21 * Model id → whether the model list's Tool use table marks MCP supported.
22 * No such table, or a cell other than Supported or Yes, Not Supported or
23 * No, throws. An id the table lists twice with different answers is dropped.
24 */
25export function parseByteplusMcpSupport(
26 doc: ByteplusDoc,
27): Map<string, boolean> {
28 const tables = tablesWith(doc, ['model id']).filter((table) =>
29 table.keys.some((key) => key.startsWith('mcp')),
30 )
31 if (tables.length === 0) {
32 throw new Error('byteplus model list: no Tool use table with an MCP column')
33 }
34 const out = new Map<string, boolean>()
35 const conflicts = new Set<string>()
36 for (const { keys, rows } of tables) {
37 const idAt = keys.indexOf('model id')
38 const mcpAt = keys.findIndex((key) => key.startsWith('mcp'))
39 for (const row of rows) {
40 const id = /^[a-z0-9][a-z0-9.-]*/.exec(row[idAt] ?? '')?.[0]
41 const cell = (row[mcpAt] ?? '').trim()
42 const supported = /^(supported|yes)$/i.test(cell)
43 ? true
44 : /^(not supported|no)$/i.test(cell)
45 ? false
46 : null
47 if (!id || supported === null) {
48 throw new Error(
49 `byteplus model list: unread Tool use row ${JSON.stringify(row)}`,
50 )
51 }
52 const prior = out.get(id)
53 if (prior !== undefined && prior !== supported) conflicts.add(id)
54 out.set(id, supported)
55 }
56 }
57 for (const id of conflicts) out.delete(id)
58 return out
60​
61type HostedFacts = Pick<ModelInfo, 'serverTools' | 'factSources' | 'absent'>
62​
63/**
64 * `mcp` for each model the Tool use table marks supported; one it marks not
65 * supported is source-silent there, and one it does not list gets nothing
66 * (#707). When the page
67 * fails to load, every row keeps its stored tools (`unavailable`), and the
68 * failure is in `docsFailures`.
69 */
70export async function byteplusServerTools(
71 run: DocsRun,
72 kv?: KVNamespace,
73): Promise<(rawId: string) => HostedFacts> {
74 const doc = await tryDocs(run, BYTEPLUS_MODEL_LIST_URL, (cached) =>
75 cached(kv, BYTEPLUS_MODEL_LIST_URL, async () => {
76 const content = pricingContent(
77 await fetchByteplusDoc(BYTEPLUS_MODEL_LIST_URL),
78 'byteplus model list',
79 )
80 return {
81 mcp: Object.fromEntries(
82 parseByteplusMcpSupport(JSON.parse(content) as ByteplusDoc),
83 ),
84 hash: await sha256Text(content),
85 }
86 }),
87 )
88 return (rawId) => {
89 if (!doc) return unavailable('serverTools')
90 // A model the table does not list states nothing (#707).
91 if (doc.mcp[rawId] === undefined) return {}
92 if (doc.mcp[rawId] === false) {
93 return {
94 factSources: {
95 serverTools: serverToolsSilent(BYTEPLUS_MODEL_LIST_URL),
96 },
97 }
98 }
99 return {
100 serverTools: ['mcp'],
101 factSources: {
102 serverTools: {
103 mcp: {
104 derivation: 'docs-derived',
105 sourceUrl: BYTEPLUS_MODEL_LIST_URL,
106 sourceHash: doc.hash,
107 path: 'Tool use table: MCP',
108 },
109 },
110 },
111 }
112 }