|
| 1 | +import { createHash } from 'node:crypto' |
| 2 | +import type { SkillGenerationOptions, SkillProvider } from './provider' |
| 3 | +import { |
| 4 | + SkillInputSchema, |
| 5 | + SkillOutputSchema, |
| 6 | + type SkillGenerationInput, |
| 7 | + type SkillJsonOutput, |
| 8 | + type SkillTopic, |
| 9 | +} from './schema' |
| 10 | + |
| 11 | +const stableStringify = (value: unknown): string => { |
| 12 | + if (value === null || typeof value !== 'object') { |
| 13 | + return JSON.stringify(value) |
| 14 | + } |
| 15 | + |
| 16 | + if (Array.isArray(value)) { |
| 17 | + return `[${value.map((item) => stableStringify(item)).join(',')}]` |
| 18 | + } |
| 19 | + |
| 20 | + const record = value as Record<string, unknown> |
| 21 | + const keys = Object.keys(record).sort() |
| 22 | + const entries = keys.map( |
| 23 | + (key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`, |
| 24 | + ) |
| 25 | + return `{${entries.join(',')}}` |
| 26 | +} |
| 27 | + |
| 28 | +const hashHex = (value: string): string => |
| 29 | + createHash('sha256').update(value).digest('hex') |
| 30 | + |
| 31 | +const sliceHex = (hash: string, index: number, size = 8): string => |
| 32 | + hash.slice(index * size, index * size + size) |
| 33 | + |
| 34 | +const makeTopics = (hash: string, count: number): SkillTopic[] => { |
| 35 | + const topics: SkillTopic[] = [] |
| 36 | + for (let i = 0; i < count; i += 1) { |
| 37 | + const token = sliceHex(hash, i + 3, 6) |
| 38 | + topics.push({ |
| 39 | + title: `Topic ${i + 1} (${token.slice(0, 4)})`, |
| 40 | + slug: `topic-${token}`, |
| 41 | + }) |
| 42 | + } |
| 43 | + return topics |
| 44 | +} |
| 45 | + |
| 46 | +const makeUsage = (hash: string) => { |
| 47 | + const inputTokens = parseInt(sliceHex(hash, 0, 4), 16) % 1000 |
| 48 | + const outputTokens = parseInt(sliceHex(hash, 1, 4), 16) % 1000 |
| 49 | + return { |
| 50 | + inputTokens, |
| 51 | + outputTokens, |
| 52 | + totalTokens: inputTokens + outputTokens, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const normalizeOptions = (options?: SkillGenerationOptions) => { |
| 57 | + if (!options) return undefined |
| 58 | + const { model, temperature, maxTokens, seed, metadata } = options |
| 59 | + return { model, temperature, maxTokens, seed, metadata } |
| 60 | +} |
| 61 | + |
| 62 | +const buildOutput = ( |
| 63 | + input: SkillGenerationInput, |
| 64 | + hash: string, |
| 65 | +): SkillJsonOutput => { |
| 66 | + const boundaries = |
| 67 | + input.boundaries && input.boundaries.length > 0 |
| 68 | + ? input.boundaries |
| 69 | + : [ |
| 70 | + `Avoid assumptions beyond provided inputs (${sliceHex(hash, 2, 6)}).`, |
| 71 | + `Keep output deterministic (${sliceHex(hash, 3, 6)}).`, |
| 72 | + ] |
| 73 | + |
| 74 | + const topics = |
| 75 | + input.topics && input.topics.length > 0 ? input.topics : makeTopics(hash, 3) |
| 76 | + |
| 77 | + const output: SkillJsonOutput = { |
| 78 | + schema: 1, |
| 79 | + library: input.library, |
| 80 | + version: input.version, |
| 81 | + title: `TanStack ${input.library} skill`, |
| 82 | + summary: `Deterministic summary for ${input.library} (${sliceHex(hash, 0, 6)}).`, |
| 83 | + boundaries, |
| 84 | + routing: `Route requests to ${input.library} topics. (${sliceHex(hash, 1, 6)})`, |
| 85 | + topics, |
| 86 | + } |
| 87 | + |
| 88 | + return SkillOutputSchema.parse(output) |
| 89 | +} |
| 90 | + |
| 91 | +export const createNoopProvider = (): SkillProvider => { |
| 92 | + return { |
| 93 | + async generateSkillJson(input, options) { |
| 94 | + const parsedInput = SkillInputSchema.parse(input) |
| 95 | + const seedData = stableStringify({ |
| 96 | + input: parsedInput, |
| 97 | + options: normalizeOptions(options), |
| 98 | + }) |
| 99 | + const hash = hashHex(seedData) |
| 100 | + const output = buildOutput(parsedInput, hash) |
| 101 | + return { |
| 102 | + output, |
| 103 | + usage: makeUsage(hash), |
| 104 | + rawMeta: { |
| 105 | + provider: 'noop', |
| 106 | + hash, |
| 107 | + }, |
| 108 | + } |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
0 commit comments