ai-functions 0.2.19 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-test.log +105 -0
- package/README.md +232 -37
- package/TODO.md +138 -0
- package/dist/ai-promise.d.ts +219 -0
- package/dist/ai-promise.d.ts.map +1 -0
- package/dist/ai-promise.js +610 -0
- package/dist/ai-promise.js.map +1 -0
- package/dist/ai.d.ts +285 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +842 -0
- package/dist/ai.js.map +1 -0
- package/dist/batch/anthropic.d.ts +23 -0
- package/dist/batch/anthropic.d.ts.map +1 -0
- package/dist/batch/anthropic.js +257 -0
- package/dist/batch/anthropic.js.map +1 -0
- package/dist/batch/bedrock.d.ts +64 -0
- package/dist/batch/bedrock.d.ts.map +1 -0
- package/dist/batch/bedrock.js +586 -0
- package/dist/batch/bedrock.js.map +1 -0
- package/dist/batch/cloudflare.d.ts +37 -0
- package/dist/batch/cloudflare.d.ts.map +1 -0
- package/dist/batch/cloudflare.js +289 -0
- package/dist/batch/cloudflare.js.map +1 -0
- package/dist/batch/google.d.ts +41 -0
- package/dist/batch/google.d.ts.map +1 -0
- package/dist/batch/google.js +360 -0
- package/dist/batch/google.js.map +1 -0
- package/dist/batch/index.d.ts +31 -0
- package/dist/batch/index.d.ts.map +1 -0
- package/dist/batch/index.js +31 -0
- package/dist/batch/index.js.map +1 -0
- package/dist/batch/memory.d.ts +44 -0
- package/dist/batch/memory.d.ts.map +1 -0
- package/dist/batch/memory.js +188 -0
- package/dist/batch/memory.js.map +1 -0
- package/dist/batch/openai.d.ts +37 -0
- package/dist/batch/openai.d.ts.map +1 -0
- package/dist/batch/openai.js +403 -0
- package/dist/batch/openai.js.map +1 -0
- package/dist/batch-map.d.ts +125 -0
- package/dist/batch-map.d.ts.map +1 -0
- package/dist/batch-map.js +406 -0
- package/dist/batch-map.js.map +1 -0
- package/dist/batch-queue.d.ts +273 -0
- package/dist/batch-queue.d.ts.map +1 -0
- package/dist/batch-queue.js +271 -0
- package/dist/batch-queue.js.map +1 -0
- package/dist/context.d.ts +133 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +267 -0
- package/dist/context.js.map +1 -0
- package/dist/embeddings.d.ts +123 -0
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +170 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/eval/index.d.ts +8 -0
- package/dist/eval/index.d.ts.map +1 -0
- package/dist/eval/index.js +8 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/models.d.ts +66 -0
- package/dist/eval/models.d.ts.map +1 -0
- package/dist/eval/models.js +120 -0
- package/dist/eval/models.js.map +1 -0
- package/dist/eval/runner.d.ts +64 -0
- package/dist/eval/runner.d.ts.map +1 -0
- package/dist/eval/runner.js +148 -0
- package/dist/eval/runner.js.map +1 -0
- package/dist/generate.d.ts +168 -0
- package/dist/generate.d.ts.map +1 -0
- package/dist/generate.js +174 -0
- package/dist/generate.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +292 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +471 -0
- package/dist/primitives.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +9 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +9 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +9 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/schema.d.ts +54 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +109 -0
- package/dist/schema.js.map +1 -0
- package/dist/template.d.ts +73 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +129 -0
- package/dist/template.js.map +1 -0
- package/dist/types.d.ts +481 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/evalite.config.ts +19 -0
- package/evals/README.md +212 -0
- package/evals/classification.eval.ts +108 -0
- package/evals/marketing.eval.ts +370 -0
- package/evals/math.eval.ts +94 -0
- package/evals/run-evals.ts +166 -0
- package/evals/structured-output.eval.ts +143 -0
- package/evals/writing.eval.ts +117 -0
- package/examples/batch-blog-posts.ts +160 -0
- package/package.json +59 -43
- package/src/ai-promise.ts +784 -0
- package/src/ai.ts +1183 -0
- package/src/batch/anthropic.ts +375 -0
- package/src/batch/bedrock.ts +801 -0
- package/src/batch/cloudflare.ts +421 -0
- package/src/batch/google.ts +491 -0
- package/src/batch/index.ts +31 -0
- package/src/batch/memory.ts +253 -0
- package/src/batch/openai.ts +557 -0
- package/src/batch-map.ts +534 -0
- package/src/batch-queue.ts +493 -0
- package/src/context.ts +332 -0
- package/src/embeddings.ts +244 -0
- package/src/eval/index.ts +8 -0
- package/src/eval/models.ts +158 -0
- package/src/eval/runner.ts +217 -0
- package/src/generate.ts +245 -0
- package/src/index.ts +154 -0
- package/src/primitives.ts +612 -0
- package/src/providers/cloudflare.ts +15 -0
- package/src/providers/index.ts +14 -0
- package/src/schema.ts +147 -0
- package/src/template.ts +209 -0
- package/src/types.ts +540 -0
- package/test/README.md +105 -0
- package/test/ai-proxy.test.ts +192 -0
- package/test/async-iterators.test.ts +327 -0
- package/test/batch-background.test.ts +482 -0
- package/test/batch-blog-posts.test.ts +387 -0
- package/test/blog-generation.test.ts +510 -0
- package/test/browse-read.test.ts +611 -0
- package/test/core-functions.test.ts +694 -0
- package/test/decide.test.ts +393 -0
- package/test/define.test.ts +274 -0
- package/test/e2e-bedrock-manual.ts +163 -0
- package/test/e2e-bedrock.test.ts +191 -0
- package/test/e2e-flex-gateway.ts +157 -0
- package/test/e2e-flex-manual.ts +183 -0
- package/test/e2e-flex.test.ts +209 -0
- package/test/e2e-google-manual.ts +178 -0
- package/test/e2e-google.test.ts +216 -0
- package/test/embeddings.test.ts +284 -0
- package/test/evals/define-function.eval.test.ts +379 -0
- package/test/evals/primitives.eval.test.ts +384 -0
- package/test/function-types.test.ts +492 -0
- package/test/generate-core.test.ts +319 -0
- package/test/generate.test.ts +163 -0
- package/test/implicit-batch.test.ts +422 -0
- package/test/schema.test.ts +109 -0
- package/test/tagged-templates.test.ts +302 -0
- package/tsconfig.json +8 -6
- package/vitest.config.ts +42 -0
- package/LICENSE +0 -21
- package/db/cache.ts +0 -6
- package/db/mongo.ts +0 -75
- package/dist/mjs/db/cache.d.ts +0 -1
- package/dist/mjs/db/cache.js +0 -5
- package/dist/mjs/db/mongo.d.ts +0 -31
- package/dist/mjs/db/mongo.js +0 -48
- package/dist/mjs/examples/data.d.ts +0 -1105
- package/dist/mjs/examples/data.js +0 -1105
- package/dist/mjs/functions/ai.d.ts +0 -20
- package/dist/mjs/functions/ai.js +0 -83
- package/dist/mjs/functions/ai.test.d.ts +0 -1
- package/dist/mjs/functions/ai.test.js +0 -29
- package/dist/mjs/functions/gpt.d.ts +0 -4
- package/dist/mjs/functions/gpt.js +0 -10
- package/dist/mjs/functions/list.d.ts +0 -7
- package/dist/mjs/functions/list.js +0 -72
- package/dist/mjs/index.d.ts +0 -3
- package/dist/mjs/index.js +0 -3
- package/dist/mjs/queue/kafka.d.ts +0 -0
- package/dist/mjs/queue/kafka.js +0 -1
- package/dist/mjs/queue/memory.d.ts +0 -0
- package/dist/mjs/queue/memory.js +0 -1
- package/dist/mjs/queue/mongo.d.ts +0 -30
- package/dist/mjs/queue/mongo.js +0 -42
- package/dist/mjs/streams/kafka.d.ts +0 -0
- package/dist/mjs/streams/kafka.js +0 -1
- package/dist/mjs/streams/memory.d.ts +0 -0
- package/dist/mjs/streams/memory.js +0 -1
- package/dist/mjs/streams/mongo.d.ts +0 -0
- package/dist/mjs/streams/mongo.js +0 -1
- package/dist/mjs/streams/types.d.ts +0 -0
- package/dist/mjs/streams/types.js +0 -1
- package/dist/mjs/types.d.ts +0 -11
- package/dist/mjs/types.js +0 -1
- package/dist/mjs/utils/completion.d.ts +0 -9
- package/dist/mjs/utils/completion.js +0 -20
- package/dist/mjs/utils/schema.d.ts +0 -10
- package/dist/mjs/utils/schema.js +0 -72
- package/dist/mjs/utils/schema.test.d.ts +0 -1
- package/dist/mjs/utils/schema.test.js +0 -60
- package/dist/mjs/utils/state.d.ts +0 -1
- package/dist/mjs/utils/state.js +0 -19
- package/examples/data.ts +0 -1105
- package/fixup +0 -11
- package/functions/ai.test.ts +0 -41
- package/functions/ai.ts +0 -115
- package/functions/gpt.ts +0 -12
- package/functions/list.ts +0 -84
- package/index.ts +0 -3
- package/queue/kafka.ts +0 -0
- package/queue/memory.ts +0 -0
- package/queue/mongo.ts +0 -88
- package/streams/kafka.ts +0 -0
- package/streams/memory.ts +0 -0
- package/streams/mongo.ts +0 -0
- package/streams/types.ts +0 -0
- package/tsconfig-backup.json +0 -105
- package/tsconfig-base.json +0 -26
- package/tsconfig-cjs.json +0 -8
- package/types.ts +0 -12
- package/utils/completion.ts +0 -28
- package/utils/schema.test.ts +0 -69
- package/utils/schema.ts +0 -74
- package/utils/state.ts +0 -23
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for tagged template literal support
|
|
3
|
+
*
|
|
4
|
+
* Every function should support:
|
|
5
|
+
* 1. Tagged template syntax: fn`prompt ${variable}`
|
|
6
|
+
* 2. Objects/arrays auto-convert to YAML in templates
|
|
7
|
+
* 3. Options chaining: fn`prompt`({ model: '...' })
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from 'vitest'
|
|
11
|
+
import { stringify } from 'yaml'
|
|
12
|
+
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Template Parsing Tests (Unit Tests - No AI calls)
|
|
15
|
+
// ============================================================================
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a tagged template literal into a prompt string
|
|
19
|
+
* Objects and arrays are converted to YAML for readability
|
|
20
|
+
*/
|
|
21
|
+
function parseTemplate(strings: TemplateStringsArray, ...values: unknown[]): string {
|
|
22
|
+
return strings.reduce((result, str, i) => {
|
|
23
|
+
const value = values[i]
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
return result + str
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Convert objects/arrays to YAML
|
|
29
|
+
if (typeof value === 'object' && value !== null) {
|
|
30
|
+
const yaml = stringify(value).trim()
|
|
31
|
+
return result + str + '\n' + yaml
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Primitives use toString()
|
|
35
|
+
return result + str + String(value)
|
|
36
|
+
}, '')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a template function that supports both call styles
|
|
41
|
+
*/
|
|
42
|
+
function createTemplateFunction<T>(
|
|
43
|
+
handler: (prompt: string, options?: Record<string, unknown>) => Promise<T>
|
|
44
|
+
): {
|
|
45
|
+
(strings: TemplateStringsArray, ...values: unknown[]): Promise<T> & { (options?: Record<string, unknown>): Promise<T> }
|
|
46
|
+
(prompt: string, options?: Record<string, unknown>): Promise<T>
|
|
47
|
+
} {
|
|
48
|
+
function templateFn(promptOrStrings: string | TemplateStringsArray, ...args: unknown[]) {
|
|
49
|
+
// Tagged template literal
|
|
50
|
+
if (Array.isArray(promptOrStrings) && 'raw' in promptOrStrings) {
|
|
51
|
+
const prompt = parseTemplate(promptOrStrings as TemplateStringsArray, ...args)
|
|
52
|
+
|
|
53
|
+
// Return a thenable that also accepts options
|
|
54
|
+
const promise = handler(prompt)
|
|
55
|
+
|
|
56
|
+
// Allow chaining with options: fn`prompt`({ model: '...' })
|
|
57
|
+
// Create a function that also implements then/catch for Promise interface
|
|
58
|
+
const chainable = (options?: Record<string, unknown>) => handler(prompt, options)
|
|
59
|
+
|
|
60
|
+
// Make it thenable by adding then/catch from the original promise
|
|
61
|
+
;(chainable as unknown as Promise<T>).then = promise.then.bind(promise)
|
|
62
|
+
;(chainable as unknown as Promise<T>).catch = promise.catch.bind(promise)
|
|
63
|
+
|
|
64
|
+
return chainable
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Regular function call
|
|
68
|
+
return handler(promptOrStrings as string, args[0] as Record<string, unknown> | undefined)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return templateFn as ReturnType<typeof createTemplateFunction<T>>
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('template parsing', () => {
|
|
75
|
+
it('parses simple string interpolation', () => {
|
|
76
|
+
const topic = 'TypeScript'
|
|
77
|
+
const result = parseTemplate`Write about ${topic}`
|
|
78
|
+
expect(result).toBe('Write about TypeScript')
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('parses multiple interpolations', () => {
|
|
82
|
+
const topic = 'TypeScript'
|
|
83
|
+
const audience = 'beginners'
|
|
84
|
+
const result = parseTemplate`Write about ${topic} for ${audience}`
|
|
85
|
+
expect(result).toBe('Write about TypeScript for beginners')
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('converts objects to YAML', () => {
|
|
89
|
+
const context = { topic: 'TypeScript', level: 'beginner' }
|
|
90
|
+
const result = parseTemplate`Write a post about ${{ context }}`
|
|
91
|
+
|
|
92
|
+
expect(result).toContain('context:')
|
|
93
|
+
expect(result).toContain('topic: TypeScript')
|
|
94
|
+
expect(result).toContain('level: beginner')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('converts arrays to YAML lists', () => {
|
|
98
|
+
const topics = ['React', 'Vue', 'Angular']
|
|
99
|
+
const result = parseTemplate`Compare ${topics}`
|
|
100
|
+
|
|
101
|
+
expect(result).toContain('- React')
|
|
102
|
+
expect(result).toContain('- Vue')
|
|
103
|
+
expect(result).toContain('- Angular')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('handles nested objects', () => {
|
|
107
|
+
const brand = {
|
|
108
|
+
hero: 'developers',
|
|
109
|
+
problem: {
|
|
110
|
+
internal: 'complexity',
|
|
111
|
+
external: 'time constraints',
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
const result = parseTemplate`Create a story for ${{ brand }}`
|
|
115
|
+
|
|
116
|
+
expect(result).toContain('brand:')
|
|
117
|
+
expect(result).toContain('hero: developers')
|
|
118
|
+
expect(result).toContain('problem:')
|
|
119
|
+
expect(result).toContain('internal: complexity')
|
|
120
|
+
expect(result).toContain('external: time constraints')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('handles numbers and booleans', () => {
|
|
124
|
+
const count = 10
|
|
125
|
+
const active = true
|
|
126
|
+
const result = parseTemplate`Generate ${count} items, active: ${active}`
|
|
127
|
+
|
|
128
|
+
expect(result).toBe('Generate 10 items, active: true')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('handles null and undefined gracefully', () => {
|
|
132
|
+
const value = null
|
|
133
|
+
const result = parseTemplate`Value is ${value}`
|
|
134
|
+
expect(result).toContain('Value is')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('preserves template structure with objects inline', () => {
|
|
138
|
+
const requirements = {
|
|
139
|
+
pages: ['home', 'about', 'contact'],
|
|
140
|
+
features: ['dark mode', 'responsive'],
|
|
141
|
+
}
|
|
142
|
+
const result = parseTemplate`marketing site${{ requirements }}`
|
|
143
|
+
|
|
144
|
+
expect(result).toContain('marketing site')
|
|
145
|
+
expect(result).toContain('requirements:')
|
|
146
|
+
expect(result).toContain('pages:')
|
|
147
|
+
expect(result).toContain('- home')
|
|
148
|
+
expect(result).toContain('features:')
|
|
149
|
+
expect(result).toContain('- dark mode')
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('template function creation', () => {
|
|
154
|
+
it('creates a function that handles tagged templates', async () => {
|
|
155
|
+
const mockHandler = async (prompt: string) => `Processed: ${prompt}`
|
|
156
|
+
const fn = createTemplateFunction(mockHandler)
|
|
157
|
+
|
|
158
|
+
const result = await fn`Hello ${'world'}`
|
|
159
|
+
expect(result).toBe('Processed: Hello world')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('creates a function that handles regular calls', async () => {
|
|
163
|
+
const mockHandler = async (prompt: string) => `Processed: ${prompt}`
|
|
164
|
+
const fn = createTemplateFunction(mockHandler)
|
|
165
|
+
|
|
166
|
+
const result = await fn('Hello world')
|
|
167
|
+
expect(result).toBe('Processed: Hello world')
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('supports options chaining on tagged templates', async () => {
|
|
171
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
172
|
+
const mockHandler = async (prompt: string, options?: Record<string, unknown>) => {
|
|
173
|
+
capturedOptions = options
|
|
174
|
+
return `Processed: ${prompt}`
|
|
175
|
+
}
|
|
176
|
+
const fn = createTemplateFunction(mockHandler)
|
|
177
|
+
|
|
178
|
+
const result = await fn`Hello world`({ model: 'claude-opus-4-5' })
|
|
179
|
+
|
|
180
|
+
expect(result).toBe('Processed: Hello world')
|
|
181
|
+
expect(capturedOptions).toEqual({ model: 'claude-opus-4-5' })
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('passes options in regular calls', async () => {
|
|
185
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
186
|
+
const mockHandler = async (prompt: string, options?: Record<string, unknown>) => {
|
|
187
|
+
capturedOptions = options
|
|
188
|
+
return `Processed: ${prompt}`
|
|
189
|
+
}
|
|
190
|
+
const fn = createTemplateFunction(mockHandler)
|
|
191
|
+
|
|
192
|
+
await fn('Hello world', { model: 'gpt-5-1', temperature: 0.7 })
|
|
193
|
+
|
|
194
|
+
expect(capturedOptions).toEqual({ model: 'gpt-5-1', temperature: 0.7 })
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('converts complex objects to YAML in templates', async () => {
|
|
198
|
+
let capturedPrompt = ''
|
|
199
|
+
const mockHandler = async (prompt: string) => {
|
|
200
|
+
capturedPrompt = prompt
|
|
201
|
+
return 'ok'
|
|
202
|
+
}
|
|
203
|
+
const fn = createTemplateFunction(mockHandler)
|
|
204
|
+
|
|
205
|
+
const brand = { hero: 'developers', guide: 'ai-functions' }
|
|
206
|
+
await fn`Create story for ${{ brand }}`
|
|
207
|
+
|
|
208
|
+
expect(capturedPrompt).toContain('brand:')
|
|
209
|
+
expect(capturedPrompt).toContain('hero: developers')
|
|
210
|
+
expect(capturedPrompt).toContain('guide: ai-functions')
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
describe('options parameter', () => {
|
|
215
|
+
it('supports model option', async () => {
|
|
216
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
217
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
218
|
+
capturedOptions = options
|
|
219
|
+
return 'ok'
|
|
220
|
+
}
|
|
221
|
+
const fn = createTemplateFunction(mockHandler)
|
|
222
|
+
|
|
223
|
+
await fn`test`({ model: 'claude-opus-4-5' })
|
|
224
|
+
|
|
225
|
+
expect(capturedOptions?.model).toBe('claude-opus-4-5')
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('supports thinking option', async () => {
|
|
229
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
230
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
231
|
+
capturedOptions = options
|
|
232
|
+
return 'ok'
|
|
233
|
+
}
|
|
234
|
+
const fn = createTemplateFunction(mockHandler)
|
|
235
|
+
|
|
236
|
+
await fn`complex analysis`({ thinking: 'high' })
|
|
237
|
+
|
|
238
|
+
expect(capturedOptions?.thinking).toBe('high')
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('supports thinking as token budget number', async () => {
|
|
242
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
243
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
244
|
+
capturedOptions = options
|
|
245
|
+
return 'ok'
|
|
246
|
+
}
|
|
247
|
+
const fn = createTemplateFunction(mockHandler)
|
|
248
|
+
|
|
249
|
+
await fn`complex analysis`({ thinking: 10000 })
|
|
250
|
+
|
|
251
|
+
expect(capturedOptions?.thinking).toBe(10000)
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
it('supports temperature option', async () => {
|
|
255
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
256
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
257
|
+
capturedOptions = options
|
|
258
|
+
return 'ok'
|
|
259
|
+
}
|
|
260
|
+
const fn = createTemplateFunction(mockHandler)
|
|
261
|
+
|
|
262
|
+
await fn`creative writing`({ temperature: 0.9 })
|
|
263
|
+
|
|
264
|
+
expect(capturedOptions?.temperature).toBe(0.9)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('supports maxTokens option', async () => {
|
|
268
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
269
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
270
|
+
capturedOptions = options
|
|
271
|
+
return 'ok'
|
|
272
|
+
}
|
|
273
|
+
const fn = createTemplateFunction(mockHandler)
|
|
274
|
+
|
|
275
|
+
await fn`long article`({ maxTokens: 4000 })
|
|
276
|
+
|
|
277
|
+
expect(capturedOptions?.maxTokens).toBe(4000)
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
it('supports multiple options together', async () => {
|
|
281
|
+
let capturedOptions: Record<string, unknown> | undefined
|
|
282
|
+
const mockHandler = async (_prompt: string, options?: Record<string, unknown>) => {
|
|
283
|
+
capturedOptions = options
|
|
284
|
+
return 'ok'
|
|
285
|
+
}
|
|
286
|
+
const fn = createTemplateFunction(mockHandler)
|
|
287
|
+
|
|
288
|
+
await fn`complex task`({
|
|
289
|
+
model: 'claude-opus-4-5',
|
|
290
|
+
thinking: 'high',
|
|
291
|
+
temperature: 0.7,
|
|
292
|
+
maxTokens: 8000,
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
expect(capturedOptions).toEqual({
|
|
296
|
+
model: 'claude-opus-4-5',
|
|
297
|
+
thinking: 'high',
|
|
298
|
+
temperature: 0.7,
|
|
299
|
+
maxTokens: 8000,
|
|
300
|
+
})
|
|
301
|
+
})
|
|
302
|
+
})
|
package/tsconfig.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"extends": "
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
4
|
+
"rootDir": "src",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"]
|
|
7
|
+
},
|
|
8
|
+
"include": ["src/**/*"],
|
|
9
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
|
|
10
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config'
|
|
2
|
+
import { config } from 'dotenv'
|
|
3
|
+
import { resolve } from 'path'
|
|
4
|
+
import { existsSync } from 'fs'
|
|
5
|
+
|
|
6
|
+
// Load .env from current directory, parent directories, and root
|
|
7
|
+
// This supports primitives being used as a submodule
|
|
8
|
+
const envPaths = [
|
|
9
|
+
resolve(process.cwd(), '.env'),
|
|
10
|
+
resolve(process.cwd(), '..', '.env'),
|
|
11
|
+
resolve(process.cwd(), '..', '..', '.env'),
|
|
12
|
+
resolve(process.cwd(), '..', '..', '..', '.env'),
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
for (const envPath of envPaths) {
|
|
16
|
+
if (existsSync(envPath)) {
|
|
17
|
+
config({ path: envPath })
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
test: {
|
|
23
|
+
globals: false,
|
|
24
|
+
environment: 'node',
|
|
25
|
+
include: ['test/**/*.test.ts'],
|
|
26
|
+
// Exclude runtime-specific tests (use separate configs)
|
|
27
|
+
exclude: [
|
|
28
|
+
'test/rpc/workers/**',
|
|
29
|
+
'test/rpc/bun/**',
|
|
30
|
+
'node_modules/**',
|
|
31
|
+
],
|
|
32
|
+
testTimeout: 60000, // AI calls can take time
|
|
33
|
+
hookTimeout: 30000,
|
|
34
|
+
// Run tests sequentially to avoid rate limiting
|
|
35
|
+
pool: 'forks',
|
|
36
|
+
poolOptions: {
|
|
37
|
+
forks: {
|
|
38
|
+
singleFork: true,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
})
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 Nathan Clevenger
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/db/cache.ts
DELETED
package/db/mongo.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { MongoClient, Db, Collection, InsertOneResult, BSON } from 'mongodb'
|
|
2
|
-
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources'
|
|
3
|
-
import { QueueInput } from '../queue/mongo'
|
|
4
|
-
|
|
5
|
-
export let client: MongoClient
|
|
6
|
-
export let db: Db
|
|
7
|
-
export let cache: Collection
|
|
8
|
-
export let cacheTTL: number
|
|
9
|
-
export let events: Collection
|
|
10
|
-
export let queue: Collection
|
|
11
|
-
export let actors: Collection
|
|
12
|
-
|
|
13
|
-
export type AIDBConfig = {
|
|
14
|
-
client: MongoClient
|
|
15
|
-
db?: Db | string
|
|
16
|
-
cache?: Collection | string
|
|
17
|
-
cacheTTL?: number
|
|
18
|
-
actors?: Collection | string
|
|
19
|
-
events?: Collection | string
|
|
20
|
-
queue?: Collection | string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export type AIDB = {
|
|
24
|
-
client: MongoClient
|
|
25
|
-
db: Db
|
|
26
|
-
cache: Collection
|
|
27
|
-
cacheTTL: number
|
|
28
|
-
actors: Collection
|
|
29
|
-
events: Collection
|
|
30
|
-
queue: Collection
|
|
31
|
-
log: (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => Promise<InsertOneResult<any>>
|
|
32
|
-
send: (input: QueueInput | QueueInput[]) => Promise<InsertOneResult<any>>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const AIDB = (args: AIDBConfig | MongoClient) => {
|
|
36
|
-
const config = (args as AIDBConfig).client ? (args as AIDBConfig) : { client: args as MongoClient }
|
|
37
|
-
client = config.client
|
|
38
|
-
db = typeof config.db === 'string' ? client.db(config.db) : config.db ?? client.db()
|
|
39
|
-
cache = typeof config.cache === 'string' ? db.collection(config.cache) : config.cache ?? db.collection('ai-cache')
|
|
40
|
-
cacheTTL = config.cacheTTL ?? 1000 * 60 * 60 * 24 * 30
|
|
41
|
-
events =
|
|
42
|
-
typeof config.events === 'string' ? db.collection(config.events) : config.events ?? db.collection('ai-events')
|
|
43
|
-
// queue = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-queue')
|
|
44
|
-
// actors = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-actors')
|
|
45
|
-
return { client, db, cache, actors, events, queue, log } as AIDB // , send } as AIDB
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// const send = (input: QueueInput | QueueInput[]) =>
|
|
49
|
-
// Array.isArray(input) ? queue.insertMany(input) : queue.insertOne(input)
|
|
50
|
-
|
|
51
|
-
const log = (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => {
|
|
52
|
-
const system = prompt.messages.find((message: any) => message.role === 'system')?.content
|
|
53
|
-
const userMessages = prompt.messages.filter((message: any) => message.role === 'user').map((message) => message.content)
|
|
54
|
-
const user = userMessages.length === 1 ? userMessages[0] : userMessages
|
|
55
|
-
const content = completion.choices?.[0].message.content
|
|
56
|
-
const tool = completion.choices?.[0].message.tool_calls?.[0].function
|
|
57
|
-
let functionData: object | string | undefined
|
|
58
|
-
try {
|
|
59
|
-
if (tool) functionData = JSON.parse(tool.arguments)
|
|
60
|
-
} catch (err) {
|
|
61
|
-
functionData = tool?.arguments
|
|
62
|
-
}
|
|
63
|
-
const timestamp = new Date()
|
|
64
|
-
const event = {
|
|
65
|
-
timestamp,
|
|
66
|
-
user,
|
|
67
|
-
system,
|
|
68
|
-
content,
|
|
69
|
-
functionName: tool?.name,
|
|
70
|
-
functionData,
|
|
71
|
-
prompt,
|
|
72
|
-
completion,
|
|
73
|
-
}
|
|
74
|
-
return events.insertOne(event, { ignoreUndefined: true })
|
|
75
|
-
}
|
package/dist/mjs/db/cache.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const md5: (data: string | object) => string;
|
package/dist/mjs/db/cache.js
DELETED
package/dist/mjs/db/mongo.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { MongoClient, Db, Collection, InsertOneResult } from 'mongodb';
|
|
2
|
-
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources';
|
|
3
|
-
import { QueueInput } from '../queue/mongo';
|
|
4
|
-
export declare let client: MongoClient;
|
|
5
|
-
export declare let db: Db;
|
|
6
|
-
export declare let cache: Collection;
|
|
7
|
-
export declare let cacheTTL: number;
|
|
8
|
-
export declare let events: Collection;
|
|
9
|
-
export declare let queue: Collection;
|
|
10
|
-
export declare let actors: Collection;
|
|
11
|
-
export type AIDBConfig = {
|
|
12
|
-
client: MongoClient;
|
|
13
|
-
db?: Db | string;
|
|
14
|
-
cache?: Collection | string;
|
|
15
|
-
cacheTTL?: number;
|
|
16
|
-
actors?: Collection | string;
|
|
17
|
-
events?: Collection | string;
|
|
18
|
-
queue?: Collection | string;
|
|
19
|
-
};
|
|
20
|
-
export type AIDB = {
|
|
21
|
-
client: MongoClient;
|
|
22
|
-
db: Db;
|
|
23
|
-
cache: Collection;
|
|
24
|
-
cacheTTL: number;
|
|
25
|
-
actors: Collection;
|
|
26
|
-
events: Collection;
|
|
27
|
-
queue: Collection;
|
|
28
|
-
log: (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => Promise<InsertOneResult<any>>;
|
|
29
|
-
send: (input: QueueInput | QueueInput[]) => Promise<InsertOneResult<any>>;
|
|
30
|
-
};
|
|
31
|
-
export declare const AIDB: (args: AIDBConfig | MongoClient) => AIDB;
|
package/dist/mjs/db/mongo.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export let client;
|
|
2
|
-
export let db;
|
|
3
|
-
export let cache;
|
|
4
|
-
export let cacheTTL;
|
|
5
|
-
export let events;
|
|
6
|
-
export let queue;
|
|
7
|
-
export let actors;
|
|
8
|
-
export const AIDB = (args) => {
|
|
9
|
-
const config = args.client ? args : { client: args };
|
|
10
|
-
client = config.client;
|
|
11
|
-
db = typeof config.db === 'string' ? client.db(config.db) : config.db ?? client.db();
|
|
12
|
-
cache = typeof config.cache === 'string' ? db.collection(config.cache) : config.cache ?? db.collection('ai-cache');
|
|
13
|
-
cacheTTL = config.cacheTTL ?? 1000 * 60 * 60 * 24 * 30;
|
|
14
|
-
events =
|
|
15
|
-
typeof config.events === 'string' ? db.collection(config.events) : config.events ?? db.collection('ai-events');
|
|
16
|
-
// queue = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-queue')
|
|
17
|
-
// actors = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-actors')
|
|
18
|
-
return { client, db, cache, actors, events, queue, log }; // , send } as AIDB
|
|
19
|
-
};
|
|
20
|
-
// const send = (input: QueueInput | QueueInput[]) =>
|
|
21
|
-
// Array.isArray(input) ? queue.insertMany(input) : queue.insertOne(input)
|
|
22
|
-
const log = (prompt, completion) => {
|
|
23
|
-
const system = prompt.messages.find((message) => message.role === 'system')?.content;
|
|
24
|
-
const userMessages = prompt.messages.filter((message) => message.role === 'user').map((message) => message.content);
|
|
25
|
-
const user = userMessages.length === 1 ? userMessages[0] : userMessages;
|
|
26
|
-
const content = completion.choices?.[0].message.content;
|
|
27
|
-
const tool = completion.choices?.[0].message.tool_calls?.[0].function;
|
|
28
|
-
let functionData;
|
|
29
|
-
try {
|
|
30
|
-
if (tool)
|
|
31
|
-
functionData = JSON.parse(tool.arguments);
|
|
32
|
-
}
|
|
33
|
-
catch (err) {
|
|
34
|
-
functionData = tool?.arguments;
|
|
35
|
-
}
|
|
36
|
-
const timestamp = new Date();
|
|
37
|
-
const event = {
|
|
38
|
-
timestamp,
|
|
39
|
-
user,
|
|
40
|
-
system,
|
|
41
|
-
content,
|
|
42
|
-
functionName: tool?.name,
|
|
43
|
-
functionData,
|
|
44
|
-
prompt,
|
|
45
|
-
completion,
|
|
46
|
-
};
|
|
47
|
-
return events.insertOne(event, { ignoreUndefined: true });
|
|
48
|
-
};
|