ReviewOS

also looking at this

stacks/postline

chore(deps): upgrade vendored Stacks framework to 0.70.163

#29
Merged glennmichael123 wants to merge chore/upgrade-0.70.163 into main
224 files +3516 -874
storage/framework/core/ai/src/client.tsadded+202-0
Changes to storage/framework/core/ai/src/client.ts
@@ -0,0 +1,202 @@
1import type {
2 AIMessage,
3 AIMessageContent,
4 AIProvider,
5 AIResult,
6 ChatCompletionOptions,
7 ConfiguredAIClient,
8 ConfiguredAIOptions,
9 GenerateObjectOptions,
10} from './types'
11import * as anthropic from './drivers/anthropic'
12import * as ollama from './drivers/ollama'
13import * as openai from './drivers/openai'
14
15function textContent(content: string | AIMessageContent[]): string {
16 if (typeof content === 'string')
17 return content
18 return content.filter(block => block.type === 'text').map(block => block.text ?? '').join('\n')
19}
20
21function providerFromConfig(config: ConfiguredAIOptions, override?: AIProvider): AIProvider {
22 if (override)
23 return override
24 const configured = String(config.default || '').toLowerCase()
25 if (configured === 'anthropic' || configured.startsWith('anthropic.'))
26 return 'anthropic'
27 if (configured === 'openai' || configured.startsWith('gpt-') || configured.startsWith('o1') || configured.startsWith('o3'))
28 return 'openai'
29 if (configured === 'ollama')
30 return 'ollama'
31 throw new Error(`Unsupported configured AI driver: ${config.default || '(empty)'}. Expected anthropic, openai, or ollama.`)
32}
33
34function parseObject(content: string): unknown {
35 const trimmed = content.trim()
36 const unfenced = trimmed.replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/, '')
37 try {
38 return JSON.parse(unfenced)
39 }
40 catch {
41 const start = unfenced.indexOf('{')
42 const end = unfenced.lastIndexOf('}')
43 if (start >= 0 && end > start)
44 return JSON.parse(unfenced.slice(start, end + 1))
45 throw new Error('AI response did not contain a valid JSON object')
46 }
47}
48
49function typeMatches(value: unknown, type: string): boolean {
50 if (type === 'array') return Array.isArray(value)
51 if (type === 'integer') return typeof value === 'number' && Number.isInteger(value)
52 if (type === 'object') return typeof value === 'object' && value !== null && !Array.isArray(value)
53 if (type === 'null') return value === null
54 return typeof value === type
55}
56
57function validateSchema(value: unknown, schema: Record<string, any>, path = '$'): string[] {
58 const errors: string[] = []
59 if (schema.type) {
60 const allowed = Array.isArray(schema.type) ? schema.type : [schema.type]
61 if (!allowed.some((type: string) => typeMatches(value, type)))
62 return [`${path} must be ${allowed.join(' or ')}`]
63 }
64 if (schema.enum && !schema.enum.some((candidate: unknown) => Object.is(candidate, value)))
65 errors.push(`${path} must be one of ${schema.enum.join(', ')}`)
66 if (typeof value === 'string') {
67 if (schema.minLength !== undefined && value.length < schema.minLength)
68 errors.push(`${path} must contain at least ${schema.minLength} characters`)
69 if (schema.maxLength !== undefined && value.length > schema.maxLength)
70 errors.push(`${path} must contain at most ${schema.maxLength} characters`)
71 }
72 if (typeof value === 'number') {
73 if (schema.minimum !== undefined && value < schema.minimum)
74 errors.push(`${path} must be at least ${schema.minimum}`)
75 if (schema.maximum !== undefined && value > schema.maximum)
76 errors.push(`${path} must be at most ${schema.maximum}`)
77 }
78 if (Array.isArray(value)) {
79 if (schema.minItems !== undefined && value.length < schema.minItems)
80 errors.push(`${path} must contain at least ${schema.minItems} items`)
81 if (schema.maxItems !== undefined && value.length > schema.maxItems)
82 errors.push(`${path} must contain at most ${schema.maxItems} items`)
83 if (schema.items) {
84 value.forEach((item, index) => errors.push(...validateSchema(item, schema.items, `${path}[${index}]`)))
85 }
86 }
87 if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
88 const record = value as Record<string, unknown>
89 for (const key of schema.required ?? []) {
90 if (!(key in record))
91 errors.push(`${path}.${key} is required`)
92 }
93 for (const [key, child] of Object.entries(schema.properties ?? {})) {
94 if (key in record)
95 errors.push(...validateSchema(record[key], child as Record<string, any>, `${path}.${key}`))
96 }
97 if (schema.additionalProperties === false) {
98 const known = new Set(Object.keys(schema.properties ?? {}))
99 for (const key of Object.keys(record)) {
100 if (!known.has(key))
101 errors.push(`${path}.${key} is not allowed`)
102 }
103 }
104 }
105 return errors
106}
107
108function createGenerate(config: ConfiguredAIOptions, provider: AIProvider) {
109 if (provider === 'anthropic') {
110 const driver = config.drivers?.anthropic ?? {}
111 anthropic.configure({
112 apiKey: driver.apiKey || process.env.ANTHROPIC_API_KEY || '',
113 model: driver.model,
114 maxTokens: driver.maxTokens,
115 anthropicVersion: driver.anthropicVersion,
116 })
117 return async (messages: AIMessage[], options: ChatCompletionOptions & { system?: string } = {}): Promise<AIResult> => {
118 const systemMessages = messages.filter(message => message.role === 'system').map(message => textContent(message.content))
119 const nonSystem = messages.filter(message => message.role !== 'system')
120 return anthropic.chat(nonSystem, {
121 ...options,
122 system: [options.system, ...systemMessages].filter(Boolean).join('\n\n') || undefined,
123 })
124 }
125 }
126 if (provider === 'openai') {
127 const driver = config.drivers?.openai ?? {}
128 openai.configure({
129 apiKey: driver.apiKey || process.env.OPENAI_API_KEY || '',
130 model: driver.model,
131 maxTokens: driver.maxTokens,
132 baseUrl: driver.baseUrl,
133 embeddingModel: driver.embeddingModel,
134 })
135 return (messages: AIMessage[], options: ChatCompletionOptions & { system?: string } = {}): Promise<AIResult> => {
136 const finalMessages = options.system
137 ? [{ role: 'system' as const, content: options.system }, ...messages]
138 : messages
139 return openai.chat(finalMessages, options)
140 }
141 }
142 const driver = config.drivers?.ollama ?? {}
143 ollama.configure({
144 host: driver.host || driver.baseUrl,
145 model: driver.model,
146 embeddingModel: driver.embeddingModel,
147 })
148 return (messages: AIMessage[], options: ChatCompletionOptions & { system?: string } = {}): Promise<AIResult> => {
149 const finalMessages = options.system
150 ? [{ role: 'system' as const, content: options.system }, ...messages]
151 : messages
152 return ollama.chat(finalMessages, options)
153 }
154}
155
156/**
157 * Create a provider-neutral AI client from an application's `config/ai.ts`.
158 * Secrets remain server-side and may be supplied by config or provider env vars.
159 */
160export function createAIClient(config: ConfiguredAIOptions, override?: AIProvider): ConfiguredAIClient {
161 const provider = providerFromConfig(config, override)
162 const generate = createGenerate(config, provider)
163 return {
164 provider,
165 generate,
166 async generateObject<T>(messages: AIMessage[], schema: Record<string, unknown>, options: GenerateObjectOptions = {}) {
167 const { attempts = 2, system, ...completionOptions } = options
168 const history = [...messages]
169 let lastError: Error | undefined
170 for (let attempt = 1; attempt <= Math.max(1, attempts); attempt++) {
171 const result = await generate(history, {
172 ...completionOptions,
173 system,
174 responseFormat: {
175 type: 'json_schema',
176 json_schema: { name: 'structured_output', schema, strict: true },
177 },
178 })
179 try {
180 const data = parseObject(result.content)
181 const errors = validateSchema(data, schema)
182 if (errors.length)
183 throw new Error(`AI response failed schema validation: ${errors.slice(0, 8).join('; ')}`)
184 return { data: data as T, result }
185 }
186 catch (error) {
187 lastError = error instanceof Error ? error : new Error(String(error))
188 if (attempt < attempts) {
189 history.push({ role: 'assistant', content: result.content })
190 history.push({
191 role: 'user',
192 content: `Return only a corrected JSON object. Validation error: ${lastError.message}`,
193 })
194 }
195 }
196 }
197 throw lastError ?? new Error('AI object generation failed')
198 },
199 }
200}
201
202export { parseObject as parseAIObject, validateSchema as validateAISchema }