Open History to see the last commit
1/**
2 * Facts OpenCode publishes for every Go request, plus training on request
3 * data. The live models list states it as `go.training` (issue #675). A
4 * probe record still states it for an id the list does not mention.
5 */
6import { requiredHeaderParameters } from './connect.ts'
7import { probesFor } from './probes/index.ts'
8import type { FactSource, ModelFactSources, RequiredHeader } from './types.ts'
9
10/** https://opencode.ai/docs/console/go/ (`/docs/go/` 301s here). */
11export const OPENCODE_GO_DOCS_URL = 'https://opencode.ai/docs/console/go/'
12
13export const OPENCODE_GO_REQUIRED_HEADERS: Array<RequiredHeader> = [
14 {
15 name: 'x-opencode-session',
16 description:
17 'A stable session id the client chooses for each conversation.',
18 },
19 {
20 name: 'User-Agent',
21 description:
22 'A client-specific User-Agent, such as my-coding-agent/1.0, not a generic SDK or HTTP-library name.',
23 },
24]
25
26/** OpenAPI header parameters for every Go operation. Values are not constants. */
27export function openCodeGoHeaderParameters(): Array<Record<string, unknown>> {
28 return requiredHeaderParameters(OPENCODE_GO_REQUIRED_HEADERS).map(
29 (parameter) => ({
30 ...parameter,
31 description: `${String(parameter.description)} ${OPENCODE_GO_DOCS_URL}`,
32 }),
33 )
34}
35
36const HEADER_SOURCE: FactSource = {
37 derivation: 'docs-derived',
38 sourceUrl: OPENCODE_GO_DOCS_URL,
39 path: 'Where can I use it?',
40}
41
42function isRecord(value: unknown): value is Record<string, unknown> {
43 return typeof value === 'object' && value !== null && !Array.isArray(value)
44}
45
46/**
47 * `go.training` on each id of `GET /v1/models` for Go. That boolean is what
48 * https://opencode.ai/console/go#privacy shows. `true` trains. `false` is a
49 * stated no. Throws when a listed id has no boolean: a missing field is an
50 * unknown shape, not an unstated fact.
51 */
52export function goTrainsOnRequestData(payload: unknown): Map<string, boolean> {
53 if (!isRecord(payload) || !Array.isArray(payload.data)) {
54 throw new Error('opencode-go: models payload has no data array')
55 }
56 const training = new Map<string, boolean>()
57 for (const row of payload.data) {
58 if (!isRecord(row) || typeof row.id !== 'string' || row.id.length === 0) {
59 continue
60 }
61 const go = row.go
62 if (!isRecord(go) || typeof go.training !== 'boolean') {
63 throw new Error(`opencode-go: ${row.id} has no go.training boolean`)
64 }
65 training.set(row.id, go.training)
66 }
67 if (training.size === 0) {
68 throw new Error('opencode-go: models payload listed no ids')
69 }
70 return training
71}
72
73export function openCodeGoFacts(
74 providerId: string,
75 rawId: string,
76): {
77 requiredRequestHeaders: Array<RequiredHeader> | null
78 trainsOnRequestData: boolean | null
79 factSources: Pick<
80 ModelFactSources,
81 'requiredRequestHeaders' | 'trainsOnRequestData'
82 >
83} {
84 if (providerId !== 'opencode-go') {
85 return {
86 requiredRequestHeaders: null,
87 trainsOnRequestData: null,
88 factSources: {},
89 }
90 }
91 const training = probesFor(providerId).find(
92 (record) => record.model === rawId && record.trainsOnRequestData === true,
93 )
94 return {
95 requiredRequestHeaders: OPENCODE_GO_REQUIRED_HEADERS,
96 trainsOnRequestData: training ? true : null,
97 factSources: {
98 requiredRequestHeaders: HEADER_SOURCE,
99 ...(training
100 ? {
101 trainsOnRequestData: {
102 derivation: 'probe-verified' as const,
103 verifiedAt: training.verifiedAt,
104 sourceUrl: `src/server/providers/probes/opencode-go.ts#${training.id}`,
105 },
106 }
107 : {}),
108 },
109 }
110}