Open History to see the last commit
1/**
2 * Facts OpenCode publishes for every Go request, plus training on request
3 * data where the Go privacy table names that id (issue #463). A probe
4 * record still states it for an id the table does not mention.
5 */
6import { markdownSection, markdownTableRows } from './model-facts.ts'
7import { requiredHeaderParameters } from './connect.ts'
8import { probesFor } from './probes/index.ts'
9import type { FactSource, ModelFactSources, RequiredHeader } from './types.ts'
10
11/** https://opencode.ai/docs/console/go/ (`/docs/go/` 301s here). */
12export const OPENCODE_GO_DOCS_URL = 'https://opencode.ai/docs/console/go/'
13
14export const OPENCODE_GO_REQUIRED_HEADERS: Array<RequiredHeader> = [
15 {
16 name: 'x-opencode-session',
17 description:
18 'A stable session id the client chooses for each conversation.',
19 },
20 {
21 name: 'User-Agent',
22 description:
23 'A client-specific User-Agent, such as my-coding-agent/1.0, not a generic SDK or HTTP-library name.',
24 },
25]
26
27/** OpenAPI header parameters for every Go operation. Values are not constants. */
28export function openCodeGoHeaderParameters(): Array<Record<string, unknown>> {
29 return requiredHeaderParameters(OPENCODE_GO_REQUIRED_HEADERS).map(
30 (parameter) => ({
31 ...parameter,
32 description: `${String(parameter.description)} ${OPENCODE_GO_DOCS_URL}`,
33 }),
34 )
35}
36
37const HEADER_SOURCE: FactSource = {
38 derivation: 'docs-derived',
39 sourceUrl: OPENCODE_GO_DOCS_URL,
40 path: 'Where can I use it?',
41}
42
43function cellId(cell: string | undefined): string {
44 return (cell ?? '').replace(/^`|`$/g, '').trim()
45}
46
47/**
48 * Privacy table joined to the Endpoints table on the model name.
49 * `true` is "Yes". `false` is "Not used". Throws when a section, a row,
50 * or a training cell is not the shape the page publishes.
51 */
52export function parseGoTraining(text: string): Map<string, boolean> {
53 const endpoints = markdownSection(text, 'Endpoints')
54 const privacy = markdownSection(text, 'Privacy')
55 if (endpoints === '' || privacy === '') {
56 throw new Error('opencode-go: go docs have no Endpoints or Privacy section')
57 }
58 const ids = new Map<string, string>()
59 for (const cells of markdownTableRows(endpoints)) {
60 if (cells[0] === 'Model') continue
61 const name = cells[0]
62 const id = cellId(cells[1])
63 if (!name || id === '') continue
64 ids.set(name, id)
65 }
66 const training = new Map<string, boolean>()
67 for (const cells of markdownTableRows(privacy)) {
68 if (cells[0] === 'Model') continue
69 const name = cells[0]
70 const stated = cells[1]
71 if (!name) continue
72 if (stated !== 'Yes' && stated !== 'Not used') {
73 throw new Error(
74 `opencode-go: privacy training cell ${JSON.stringify(stated)} for ${name}`,
75 )
76 }
77 const id = ids.get(name)
78 if (!id) {
79 throw new Error(`opencode-go: privacy model ${name} has no endpoint id`)
80 }
81 training.set(id, stated === 'Yes')
82 }
83 if (training.size === 0) {
84 throw new Error('opencode-go: privacy table names no models')
85 }
86 return training
87}
88
89export function openCodeGoFacts(
90 providerId: string,
91 rawId: string,
92): {
93 requiredRequestHeaders: Array<RequiredHeader> | null
94 trainsOnRequestData: boolean | null
95 factSources: Pick<
96 ModelFactSources,
97 'requiredRequestHeaders' | 'trainsOnRequestData'
98 >
99} {
100 if (providerId !== 'opencode-go') {
101 return {
102 requiredRequestHeaders: null,
103 trainsOnRequestData: null,
104 factSources: {},
105 }
106 }
107 const training = probesFor(providerId).find(
108 (record) => record.model === rawId && record.trainsOnRequestData === true,
109 )
110 return {
111 requiredRequestHeaders: OPENCODE_GO_REQUIRED_HEADERS,
112 trainsOnRequestData: training ? true : null,
113 factSources: {
114 requiredRequestHeaders: HEADER_SOURCE,
115 ...(training
116 ? {
117 trainsOnRequestData: {
118 derivation: 'probe-verified' as const,
119 verifiedAt: training.verifiedAt,
120 sourceUrl: `src/server/providers/probes/opencode-go.ts#${training.id}`,
121 },
122 }
123 : {}),
124 },
125 }
126}