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

source-silence-ledger.ts

39 lines1.3 KB

1/** Exact provider/model scopes shared by scoring and read-time evidence. */
2export interface LedgerEntry {
3 scope: string
4 provider: string
5 rawId?: string
6 fact: string
7}
8​
9/** First slash separates the provider; the remaining native id is never normalized. */
10export function modelLedgerScope(provider: string, rawId: string): string {
11 return `${provider}/${rawId}`
13​
14/** Markdown prose is ignored; a recognized entry must have a valid scope. */
15export function parseLedgerEntry(line: string): LedgerEntry | null {
16 const match =
17 /^- (?:`([^`]+)`|([^\s`]+)): (?:`([^`]+)`|([^\s`]+))(?=\s|$)/.exec(line)
18 if (!match) {
19 if (/^- (?:`[\w.-]+(?:\/[^`]*)?`|[\w.-]+(?:\/[^\s]*)?):/.test(line))
20 throw new Error(`source-silent ledger: malformed entry: ${line}`)
21 return null
22 }
23 const scope = match[1] ?? match[2]!
24 const slash = scope.indexOf('/')
25 const provider = slash < 0 ? scope : scope.slice(0, slash)
26 const rawId = slash < 0 ? undefined : scope.slice(slash + 1)
27 if (
28 !/^[\w.-]+$/.test(provider) ||
29 (rawId !== undefined && (!rawId || /\s/.test(rawId)))
30 ) {
31 throw new Error(`source-silent ledger: invalid scope "${scope}"`)
32 }
33 return {
34 scope,
35 provider,
36 ...(rawId !== undefined ? { rawId } : {}),
37 fact: match[3] ?? match[4]!,
38 }