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

byteplus-tools.ts

102 lines3.3 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 { tryDocs, unavailable } from './model-facts.ts'
15import type { DocsRun } from './model-facts.ts'
16import { sha256Text } from './types.ts'
17import type { ModelInfo } from './types.ts'
18​
19/**
20 * Model id → whether the model list's Tool use table marks MCP supported.
21 * No such table, or a cell other than Supported or Yes, Not Supported or
22 * No, throws. An id the table lists twice with different answers is dropped.
23 */
24export function parseByteplusMcpSupport(
25 doc: ByteplusDoc,
26): Map<string, boolean> {
27 const tables = tablesWith(doc, ['model id']).filter((table) =>
28 table.keys.some((key) => key.startsWith('mcp')),
29 )
30 if (tables.length === 0) {
31 throw new Error('byteplus model list: no Tool use table with an MCP column')
32 }
33 const out = new Map<string, boolean>()
34 const conflicts = new Set<string>()
35 for (const { keys, rows } of tables) {
36 const idAt = keys.indexOf('model id')
37 const mcpAt = keys.findIndex((key) => key.startsWith('mcp'))
38 for (const row of rows) {
39 const id = /^[a-z0-9][a-z0-9.-]*/.exec(row[idAt] ?? '')?.[0]
40 const cell = (row[mcpAt] ?? '').trim()
41 const supported = /^(supported|yes)$/i.test(cell)
42 ? true
43 : /^(not supported|no)$/i.test(cell)
44 ? false
45 : null
46 if (!id || supported === null) {
47 throw new Error(
48 `byteplus model list: unread Tool use row ${JSON.stringify(row)}`,
49 )
50 }
51 const prior = out.get(id)
52 if (prior !== undefined && prior !== supported) conflicts.add(id)
53 out.set(id, supported)
54 }
55 }
56 for (const id of conflicts) out.delete(id)
57 return out
59​
60type HostedFacts = Pick<ModelInfo, 'serverTools' | 'factSources' | 'absent'>
61​
62/**
63 * `mcp` for each model the Tool use table marks supported. When the page
64 * fails to load, every row keeps its stored tools (`unavailable`), and the
65 * failure is in `docsFailures`.
66 */
67export async function byteplusServerTools(
68 run: DocsRun,
69 kv?: KVNamespace,
70): Promise<(rawId: string) => HostedFacts> {
71 const doc = await tryDocs(run, BYTEPLUS_MODEL_LIST_URL, (cached) =>
72 cached(kv, BYTEPLUS_MODEL_LIST_URL, async () => {
73 const content = pricingContent(
74 await fetchByteplusDoc(BYTEPLUS_MODEL_LIST_URL),
75 'byteplus model list',
76 )
77 return {
78 mcp: Object.fromEntries(
79 parseByteplusMcpSupport(JSON.parse(content) as ByteplusDoc),
80 ),
81 hash: await sha256Text(content),
82 }
83 }),
84 )
85 return (rawId) => {
86 if (!doc) return unavailable('serverTools')
87 if (doc.mcp[rawId] !== true) return {}
88 return {
89 serverTools: ['mcp'],
90 factSources: {
91 serverTools: {
92 mcp: {
93 derivation: 'docs-derived',
94 sourceUrl: BYTEPLUS_MODEL_LIST_URL,
95 sourceHash: doc.hash,
96 path: 'Tool use table: MCP',
97 },
98 },
99 },
100 }
101 }