@namzu/openai 1.0.3 → 1.1.1

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.
@@ -1,127 +0,0 @@
1
- import { DuplicateProviderError, ProviderRegistry } from '@namzu/sdk'
2
- import type { ChatCompletionParams } from '@namzu/sdk'
3
- import { beforeEach, describe, expect, it } from 'vitest'
4
- import { toOpenAIMessages } from '../client.js'
5
- import { OPENAI_CAPABILITIES, OpenAIProvider, registerOpenAI } from '../index.js'
6
-
7
- // Ensure a clean slate between tests. The sdk pre-registers 'mock' on import
8
- // via its sideEffects whitelist; we only need to clear 'openai' to make
9
- // re-registration deterministic across tests.
10
- beforeEach(() => {
11
- if (ProviderRegistry.isSupported('openai')) {
12
- ProviderRegistry.unregister('openai')
13
- }
14
- })
15
-
16
- describe('@namzu/openai', () => {
17
- describe('registerOpenAI()', () => {
18
- it("adds 'openai' to the ProviderRegistry", () => {
19
- expect(ProviderRegistry.isSupported('openai')).toBe(false)
20
- registerOpenAI()
21
- expect(ProviderRegistry.isSupported('openai')).toBe(true)
22
- expect(ProviderRegistry.listTypes()).toContain('openai')
23
- })
24
-
25
- it('throws DuplicateProviderError when called twice without options', () => {
26
- registerOpenAI()
27
- expect(() => registerOpenAI()).toThrowError(DuplicateProviderError)
28
- })
29
-
30
- it('allows re-registration when { replace: true } is passed', () => {
31
- registerOpenAI()
32
- expect(() => registerOpenAI({ replace: true })).not.toThrow()
33
- expect(ProviderRegistry.isSupported('openai')).toBe(true)
34
- })
35
-
36
- it('exposes capabilities through the registry after registration', () => {
37
- registerOpenAI()
38
- const caps = ProviderRegistry.getCapabilities('openai')
39
- expect(caps).toEqual(OPENAI_CAPABILITIES)
40
- })
41
- })
42
-
43
- describe('OPENAI_CAPABILITIES', () => {
44
- it('declares the expected capability flags', () => {
45
- expect(OPENAI_CAPABILITIES).toEqual({
46
- supportsTools: true,
47
- supportsStreaming: true,
48
- supportsFunctionCalling: true,
49
- supportsVision: true,
50
- })
51
- })
52
-
53
- it('is exposed on the provider instance for runtime negotiation', () => {
54
- const provider = new OpenAIProvider({ apiKey: 'test-key' })
55
- expect(provider.capabilities).toEqual(OPENAI_CAPABILITIES)
56
- })
57
- })
58
-
59
- describe('ProviderRegistry.create({ type: "openai", ... })', () => {
60
- it('narrows the config type via module augmentation and instantiates OpenAIProvider', () => {
61
- registerOpenAI()
62
- const { provider, capabilities } = ProviderRegistry.create({
63
- type: 'openai',
64
- apiKey: 'test-key',
65
- model: 'gpt-4o-mini',
66
- })
67
- expect(provider).toBeInstanceOf(OpenAIProvider)
68
- expect(capabilities).toEqual(OPENAI_CAPABILITIES)
69
- })
70
-
71
- it('throws when apiKey is missing', () => {
72
- registerOpenAI()
73
- expect(() =>
74
- ProviderRegistry.create({
75
- type: 'openai',
76
- apiKey: '',
77
- }),
78
- ).toThrowError(/API key is required/)
79
- })
80
- })
81
-
82
- describe('toOpenAIMessages image attachments', () => {
83
- it('maps user-message attachments to image_url content parts with base64 data URIs', () => {
84
- const messages: ChatCompletionParams['messages'] = [
85
- {
86
- role: 'user',
87
- content: 'what is in this image?',
88
- attachments: [
89
- { data: 'aGVsbG8=', mediaType: 'image/png' },
90
- { data: 'd29ybGQ=', mediaType: 'image/jpeg' },
91
- ],
92
- },
93
- ]
94
-
95
- const [mapped] = toOpenAIMessages(messages)
96
- expect(mapped).toEqual({
97
- role: 'user',
98
- content: [
99
- { type: 'text', text: 'what is in this image?' },
100
- { type: 'image_url', image_url: { url: 'data:image/png;base64,aGVsbG8=' } },
101
- { type: 'image_url', image_url: { url: 'data:image/jpeg;base64,d29ybGQ=' } },
102
- ],
103
- })
104
- })
105
-
106
- it('omits the text part when the user message is empty but keeps the images', () => {
107
- const messages: ChatCompletionParams['messages'] = [
108
- {
109
- role: 'user',
110
- content: '',
111
- attachments: [{ data: 'aGVsbG8=', mediaType: 'image/webp' }],
112
- },
113
- ]
114
-
115
- const [mapped] = toOpenAIMessages(messages)
116
- expect(mapped).toEqual({
117
- role: 'user',
118
- content: [{ type: 'image_url', image_url: { url: 'data:image/webp;base64,aGVsbG8=' } }],
119
- })
120
- })
121
-
122
- it('keeps plain text-only user messages in string form (no content parts)', () => {
123
- const messages: ChatCompletionParams['messages'] = [{ role: 'user', content: 'hello' }]
124
- expect(toOpenAIMessages(messages)).toEqual([{ role: 'user', content: 'hello' }])
125
- })
126
- })
127
- })