@warlock.js/ai-openai 4.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.
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # @warlock.js/ai-openai
2
+
3
+ OpenAI adapter for [`@warlock.js/ai`](../ai). Works with any endpoint that speaks the OpenAI Chat Completions protocol — OpenAI proper, Azure OpenAI, OpenRouter, and OpenAI-compatible local gateways.
4
+
5
+ ```bash
6
+ npm install @warlock.js/ai @warlock.js/ai-openai @warlock.js/seal openai
7
+ ```
8
+
9
+ > `@warlock.js/seal` is the recommended Standard Schema library for tool inputs and structured output. Any Standard Schema V1 library works (Zod, Valibot, …).
10
+
11
+ ## Quick start
12
+
13
+ ```ts
14
+ import { OpenAISDK } from "@warlock.js/ai-openai";
15
+ import { ai } from "@warlock.js/ai";
16
+
17
+ const openai = new OpenAISDK({ apiKey: process.env.OPENAI_API_KEY! });
18
+
19
+ const myAgent = ai.agent({
20
+ model: openai.model({ name: "gpt-4o-mini" }),
21
+ });
22
+
23
+ const result = await myAgent.execute("Hello!");
24
+ console.log(result.text);
25
+ ```
26
+
27
+ ## API surface
28
+
29
+ ```ts
30
+ new OpenAISDK(config: OpenAISDKConfig) // wraps `openai` SDK ClientOptions
31
+ .model(config: OpenAIModelConfig) // → ModelContract
32
+ .embedder(config: OpenAIEmbedderConfig) // → EmbedderContract
33
+ .count(text, model?) // approximate token count
34
+
35
+ OpenAIModelConfig {
36
+ name: string; // e.g. "gpt-4o-mini", "o3"
37
+ temperature?: number;
38
+ maxTokens?: number;
39
+ vision?: boolean; // override auto-inference
40
+ // ...any OpenAI-specific extras passed through to ModelCallOptions
41
+ }
42
+
43
+ OpenAIEmbedderConfig {
44
+ name: string; // e.g. "text-embedding-3-small"
45
+ dimensions?: number; // optional truncation for supported models
46
+ }
47
+ ```
48
+
49
+ ## Embeddings
50
+
51
+ ```ts
52
+ const embedder = openai.embedder({ name: "text-embedding-3-small" });
53
+
54
+ // Single string — returns EmbeddingResult
55
+ const { vector, dimensions, usage } = await embedder.embed("Hello world");
56
+
57
+ // Batch — returns EmbeddingBatchResult
58
+ const { vectors, dimensions, usage } = await embedder.embed(["doc 1", "doc 2", "doc 3"]);
59
+ ```
60
+
61
+ `dimensions` on the embedder object starts at `0` and is resolved from the first response's vector length, then cached. Pass `dimensions` in config to request output truncation and set the value immediately:
62
+
63
+ ```ts
64
+ openai.embedder({ name: "text-embedding-3-large", dimensions: 256 });
65
+ ```
66
+
67
+ ## Capabilities
68
+
69
+ `OpenAIModel` declares:
70
+
71
+ | Capability | Default |
72
+ | ------------------ | ---------------------------------------------------------------------- |
73
+ | `structuredOutput` | `true` — schemas forwarded as `response_format: json_schema, strict` |
74
+ | `vision` | Auto-inferred from model name; pass `vision: true \| false` to override |
75
+
76
+ Vision auto-inference matches model name prefixes: `gpt-4o`, `gpt-4-turbo`, `gpt-4.1`, `o1`, `o3`, `chatgpt-4o`. Unknown models default to `vision: false` so unsupported requests fail with a clear capability error rather than an opaque OpenAI 400.
77
+
78
+ ## OpenAI-compatible endpoints
79
+
80
+ Pass a `baseURL` for Azure OpenAI / OpenRouter / local servers:
81
+
82
+ ```ts
83
+ new OpenAISDK({
84
+ apiKey: process.env.OPENROUTER_API_KEY!,
85
+ baseURL: "https://openrouter.ai/api/v1",
86
+ });
87
+ ```
88
+
89
+ ## Tests
90
+
91
+ ```bash
92
+ npm test
93
+ ```
94
+
95
+ Covers vision capability inference and message conversion (including multipart user content with image attachments).
96
+
97
+ ## License
98
+
99
+ MIT