@rcrsr/rill-ext-openai 0.8.5 → 0.9.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @rcrsr/rill-ext-openai
2
2
 
3
- [rill](https://rill.run) extension for [OpenAI](https://platform.openai.com/docs) API integration. Provides `message`, `messages`, `embed`, `embed_batch`, and `tool_loop` host functions. Compatible with any OpenAI-compatible server (LM Studio, Ollama, vLLM).
3
+ [rill](https://rill.run) extension for [OpenAI](https://platform.openai.com/docs) API integration. Provides `message`, `messages`, `embed`, `embed_batch`, `tool_loop`, and `generate` host functions. Compatible with any OpenAI-compatible server (LM Studio, Ollama, vLLM).
4
4
 
5
5
  > **Experimental.** Breaking changes will occur before stabilization.
6
6
 
@@ -20,7 +20,7 @@ import { createOpenAIExtension } from '@rcrsr/rill-ext-openai';
20
20
 
21
21
  const ext = createOpenAIExtension({
22
22
  api_key: process.env.OPENAI_API_KEY!,
23
- model: 'gpt-4-turbo',
23
+ model: 'gpt-4o',
24
24
  });
25
25
  const prefixed = prefixFunctions('openai', ext);
26
26
  const { dispose, ...functions } = prefixed;
@@ -36,134 +36,15 @@ const result = await execute(parse(script), ctx);
36
36
  dispose?.();
37
37
  ```
38
38
 
39
- ## Host Functions
40
-
41
- All functions return a dict with `content`, `model`, `usage`, `stop_reason`, `id`, and `messages`.
42
-
43
- ### openai::message(text, options?)
44
-
45
- Send a single message to OpenAI.
46
-
47
- ```rill
48
- openai::message("Analyze this code for security issues") => $response
49
- $response.content -> log
50
- $response.usage.output -> log
51
- ```
52
-
53
- ### openai::messages(messages, options?)
54
-
55
- Send a multi-turn conversation.
56
-
57
- ```rill
58
- openai::messages([
59
- [role: "user", content: "What is rill?"],
60
- [role: "assistant", content: "A scripting language for AI agents."],
61
- [role: "user", content: "Show me an example."]
62
- ]) => $response
63
- $response.content -> log
64
- ```
65
-
66
- ### openai::embed(text)
67
-
68
- Generate an embedding vector for text. Requires `embed_model` in config.
69
-
70
- ```rill
71
- openai::embed("Hello world") => $vector
72
- ```
73
-
74
- ### openai::embed_batch(texts)
75
-
76
- Generate embedding vectors for multiple texts in a single API call.
77
-
78
- ```rill
79
- openai::embed_batch(["Hello", "World"]) => $vectors
80
- ```
81
-
82
- ### openai::tool_loop(prompt, options)
83
-
84
- Execute a tool-use loop where the model calls rill functions iteratively.
85
-
86
- ```rill
87
- openai::tool_loop("Find the weather", [tools: $my_tools]) => $result
88
- $result.content -> log
89
- $result.turns -> log
90
- ```
91
-
92
- ## Configuration
93
-
94
- ```typescript
95
- const ext = createOpenAIExtension({
96
- api_key: process.env.OPENAI_API_KEY!,
97
- model: 'gpt-4-turbo',
98
- temperature: 0.7,
99
- base_url: 'http://localhost:1234/v1', // OpenAI-compatible server
100
- max_tokens: 4096,
101
- max_retries: 3,
102
- timeout: 30000,
103
- system: 'You are a helpful assistant.',
104
- embed_model: 'text-embedding-3-small',
105
- });
106
- ```
107
-
108
- | Option | Type | Default | Description |
109
- |--------|------|---------|-------------|
110
- | `api_key` | string | required | OpenAI API key |
111
- | `model` | string | required | Model identifier |
112
- | `temperature` | number | undefined | Temperature (0.0-2.0) |
113
- | `base_url` | string | undefined | Custom API endpoint URL |
114
- | `max_tokens` | number | `4096` | Max tokens in response |
115
- | `max_retries` | number | undefined | Max retry attempts |
116
- | `timeout` | number | undefined | Request timeout in ms |
117
- | `system` | string | undefined | Default system prompt |
118
- | `embed_model` | string | undefined | Embedding model identifier |
119
-
120
- ## Result Shape
121
-
122
- ```typescript
123
- interface OpenAIResult {
124
- content: string; // response text
125
- model: string; // model used
126
- usage: {
127
- input: number; // prompt tokens
128
- output: number; // completion tokens
129
- };
130
- stop_reason: string; // finish reason
131
- id: string; // request ID
132
- messages: Array<{ // full conversation history
133
- role: string;
134
- content: string;
135
- }>;
136
- }
137
- ```
138
-
139
- ## Lifecycle
140
-
141
- Call `dispose()` on the extension to cancel pending requests:
142
-
143
- ```typescript
144
- const ext = createOpenAIExtension({ ... });
145
- // ... use extension ...
146
- await ext.dispose?.();
147
- ```
148
-
149
- ## Test Host
150
-
151
- A runnable example at `examples/test-host.ts` wires up the extension with the rill runtime:
152
-
153
- ```bash
154
- pnpm exec tsx examples/test-host.ts
155
- pnpm exec tsx examples/test-host.ts -e 'openai::message("Tell me a joke") -> log'
156
- pnpm exec tsx examples/test-host.ts script.rill
157
- ```
39
+ ## Documentation
158
40
 
159
- Requires `OPENAI_API_KEY` environment variable (or `--base-url` for local servers).
41
+ See [full documentation](docs/extension-llm-openai.md) for configuration, functions, error handling, events, and examples.
160
42
 
161
- ## Documentation
43
+ ## Related
162
44
 
163
- | Document | Description |
164
- |----------|-------------|
165
- | [Extensions Guide](https://github.com/rcrsr/rill/blob/main/docs/integration-extensions.md) | Extension contract and patterns |
166
- | [Host API Reference](https://github.com/rcrsr/rill/blob/main/docs/ref-host-api.md) | Runtime context and host functions |
45
+ - [rill](https://github.com/rcrsr/rill) Core language runtime
46
+ - [Extensions Guide](https://github.com/rcrsr/rill/blob/main/docs/integration-extensions.md) — Extension contract and patterns
47
+ - [Host API Reference](https://github.com/rcrsr/rill/blob/main/docs/ref-host-api.md) Runtime context and host functions
167
48
 
168
49
  ## License
169
50
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
- import { ExtensionResult } from '@rcrsr/rill';
3
+ import { ExtensionConfigSchema, ExtensionResult } from '@rcrsr/rill';
4
4
 
5
5
  /**
6
6
  * Base configuration for LLM extensions
@@ -73,12 +73,8 @@ export type OpenAIExtensionConfig = LLMProviderConfig;
73
73
  * ```
74
74
  */
75
75
  export declare function createOpenAIExtension(config: OpenAIExtensionConfig): ExtensionResult;
76
- /**
77
- * @rcrsr/rill-ext-openai
78
- *
79
- * Extension for OpenAI API integration with rill scripts.
80
- */
81
76
  export declare const VERSION = "0.0.1";
77
+ export declare const configSchema: ExtensionConfigSchema;
82
78
 
83
79
  export {
84
80
  LLMProviderConfig as LLMExtensionConfig,
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // src/factory.ts
2
2
  import OpenAI from "openai";
3
3
  import {
4
- RuntimeError as RuntimeError4,
4
+ RuntimeError as RuntimeError5,
5
5
  emitExtensionEvent,
6
6
  createVector,
7
- isCallable as isCallable2,
7
+ isDict as isDict2,
8
8
  isVector
9
9
  } from "@rcrsr/rill";
10
10
 
@@ -76,29 +76,108 @@ function mapProviderError(providerName, error, detect) {
76
76
  }
77
77
 
78
78
  // ../../shared/ext-llm/dist/tool-loop.js
79
- import { isCallable, isDict, RuntimeError as RuntimeError3 } from "@rcrsr/rill";
79
+ import { invokeCallable, isCallable, isDict, isRuntimeCallable, RuntimeError as RuntimeError4 } from "@rcrsr/rill";
80
+
81
+ // ../../shared/ext-llm/dist/schema.js
82
+ import { RuntimeError as RuntimeError3 } from "@rcrsr/rill";
83
+ var RILL_TYPE_MAP = {
84
+ string: "string",
85
+ number: "number",
86
+ bool: "boolean",
87
+ list: "array",
88
+ dict: "object",
89
+ vector: "object",
90
+ shape: "object"
91
+ };
92
+ function mapRillType(rillType) {
93
+ const jsonType = RILL_TYPE_MAP[rillType];
94
+ if (jsonType === void 0) {
95
+ throw new RuntimeError3("RILL-R004", `unsupported type: ${rillType}`);
96
+ }
97
+ return jsonType;
98
+ }
99
+ function buildJsonSchema(rillSchema) {
100
+ const properties = {};
101
+ const required = [];
102
+ for (const [key, value] of Object.entries(rillSchema)) {
103
+ if (typeof value === "string") {
104
+ properties[key] = buildProperty(value);
105
+ } else if (typeof value === "object" && value !== null) {
106
+ properties[key] = buildProperty(value);
107
+ } else {
108
+ throw new RuntimeError3("RILL-R004", `unsupported type: ${String(value)}`);
109
+ }
110
+ required.push(key);
111
+ }
112
+ return { type: "object", properties, required, additionalProperties: false };
113
+ }
114
+ function buildProperty(descriptor) {
115
+ if (typeof descriptor === "string") {
116
+ const jsonType2 = mapRillType(descriptor);
117
+ return { type: jsonType2 };
118
+ }
119
+ const rillType = descriptor["type"];
120
+ if (typeof rillType !== "string") {
121
+ throw new RuntimeError3("RILL-R004", `unsupported type: ${String(rillType)}`);
122
+ }
123
+ const jsonType = mapRillType(rillType);
124
+ const property = { type: jsonType };
125
+ const description = descriptor["description"];
126
+ if (typeof description === "string") {
127
+ property.description = description;
128
+ }
129
+ if ("enum" in descriptor) {
130
+ if (rillType !== "string") {
131
+ throw new RuntimeError3("RILL-R004", "enum is only valid for string type");
132
+ }
133
+ const enumValues = descriptor["enum"];
134
+ if (Array.isArray(enumValues)) {
135
+ property.enum = enumValues;
136
+ }
137
+ }
138
+ if (rillType === "list" && "items" in descriptor) {
139
+ const items = descriptor["items"];
140
+ if (typeof items === "string") {
141
+ property.items = buildProperty(items);
142
+ } else if (typeof items === "object" && items !== null) {
143
+ property.items = buildProperty(items);
144
+ }
145
+ }
146
+ if (rillType === "dict" && "properties" in descriptor) {
147
+ const nestedProps = descriptor["properties"];
148
+ if (typeof nestedProps === "object" && nestedProps !== null) {
149
+ const subSchema = buildJsonSchema(nestedProps);
150
+ property.properties = subSchema.properties;
151
+ property.required = subSchema.required;
152
+ property.additionalProperties = false;
153
+ }
154
+ }
155
+ return property;
156
+ }
157
+
158
+ // ../../shared/ext-llm/dist/tool-loop.js
80
159
  async function executeToolCall(toolName, toolInput, tools, context) {
81
160
  if (!isDict(tools)) {
82
- throw new RuntimeError3("RILL-R004", "tools must be a dict mapping tool names to functions");
161
+ throw new RuntimeError4("RILL-R004", "tool_loop: tools must be a dict of name \u2192 callable");
83
162
  }
84
163
  const toolsDict = tools;
85
164
  const toolFn = toolsDict[toolName];
86
165
  if (toolFn === void 0 || toolFn === null) {
87
- throw new RuntimeError3("RILL-R004", `Unknown tool: ${toolName}`);
166
+ throw new RuntimeError4("RILL-R004", `Unknown tool: ${toolName}`);
88
167
  }
89
168
  if (!isCallable(toolFn)) {
90
- throw new RuntimeError3("RILL-R004", `Invalid tool input for ${toolName}: tool must be callable`);
169
+ throw new RuntimeError4("RILL-R004", `Invalid tool input for ${toolName}: tool must be callable`);
91
170
  }
92
171
  if (typeof toolInput !== "object" || toolInput === null) {
93
- throw new RuntimeError3("RILL-R004", `Invalid tool input for ${toolName}: input must be an object`);
172
+ throw new RuntimeError4("RILL-R004", `Invalid tool input for ${toolName}: input must be an object`);
94
173
  }
95
174
  const callable = toolFn;
96
- if (callable.kind !== "runtime" && callable.kind !== "application") {
97
- throw new RuntimeError3("RILL-R004", `Invalid tool input for ${toolName}: tool must be application or runtime callable`);
175
+ if (callable.kind !== "runtime" && callable.kind !== "application" && callable.kind !== "script") {
176
+ throw new RuntimeError4("RILL-R004", `Invalid tool input for ${toolName}: tool must be application, runtime, or script callable`);
98
177
  }
99
178
  try {
100
179
  let args;
101
- if (callable.kind === "application" && callable.params) {
180
+ if ((callable.kind === "application" || callable.kind === "script") && callable.params && callable.params.length > 0) {
102
181
  const params = callable.params;
103
182
  const inputDict = toolInput;
104
183
  args = params.map((param) => {
@@ -108,6 +187,12 @@ async function executeToolCall(toolName, toolInput, tools, context) {
108
187
  } else {
109
188
  args = [toolInput];
110
189
  }
190
+ if (callable.kind === "script") {
191
+ if (!context) {
192
+ throw new RuntimeError4("RILL-R004", `Invalid tool input for ${toolName}: script callable requires a runtime context`);
193
+ }
194
+ return await invokeCallable(callable, args, context);
195
+ }
111
196
  const ctx = context ?? {
112
197
  parent: void 0,
113
198
  variables: /* @__PURE__ */ new Map(),
@@ -116,74 +201,146 @@ async function executeToolCall(toolName, toolInput, tools, context) {
116
201
  const result = callable.fn(args, ctx);
117
202
  return result instanceof Promise ? await result : result;
118
203
  } catch (error) {
119
- if (error instanceof RuntimeError3) {
204
+ if (error instanceof RuntimeError4) {
120
205
  throw error;
121
206
  }
122
207
  const message = error instanceof Error ? error.message : "Unknown error";
123
- throw new RuntimeError3("RILL-R004", `Invalid tool input for ${toolName}: ${message}`);
208
+ throw new RuntimeError4("RILL-R004", `Invalid tool input for ${toolName}: ${message}`);
209
+ }
210
+ }
211
+ function sanitizeToolName(name) {
212
+ const match = name.match(/^[a-zA-Z0-9_-]*/);
213
+ const sanitized = match ? match[0] : "";
214
+ return sanitized.length > 0 ? sanitized : name;
215
+ }
216
+ function patchResponseToolCallNames(response, nameMap) {
217
+ if (!nameMap.size || !response || typeof response !== "object")
218
+ return;
219
+ const resp = response;
220
+ if (Array.isArray(resp["choices"])) {
221
+ for (const choice of resp["choices"]) {
222
+ const msg = choice?.["message"];
223
+ const tcs = msg?.["tool_calls"];
224
+ if (Array.isArray(tcs)) {
225
+ for (const tc of tcs) {
226
+ const fn = tc?.["function"];
227
+ if (fn && typeof fn["name"] === "string") {
228
+ const orig = fn["name"];
229
+ const san = nameMap.get(orig);
230
+ if (san !== void 0)
231
+ fn["name"] = san;
232
+ }
233
+ }
234
+ }
235
+ }
236
+ }
237
+ if (Array.isArray(resp["content"])) {
238
+ for (const block of resp["content"]) {
239
+ const b = block;
240
+ if (b?.["type"] === "tool_use" && typeof b?.["name"] === "string") {
241
+ const orig = b["name"];
242
+ const san = nameMap.get(orig);
243
+ if (san !== void 0)
244
+ b["name"] = san;
245
+ }
246
+ }
247
+ }
248
+ if (Array.isArray(resp["functionCalls"])) {
249
+ for (const fc of resp["functionCalls"]) {
250
+ const f = fc;
251
+ if (typeof f?.["name"] === "string") {
252
+ const orig = f["name"];
253
+ const san = nameMap.get(orig);
254
+ if (san !== void 0)
255
+ f["name"] = san;
256
+ }
257
+ }
258
+ }
259
+ if (Array.isArray(resp["candidates"])) {
260
+ for (const cand of resp["candidates"]) {
261
+ const content = cand?.["content"];
262
+ const parts = content?.["parts"];
263
+ if (Array.isArray(parts)) {
264
+ for (const part of parts) {
265
+ const fc = part?.["functionCall"];
266
+ if (fc && typeof fc["name"] === "string") {
267
+ const orig = fc["name"];
268
+ const san = nameMap.get(orig);
269
+ if (san !== void 0)
270
+ fc["name"] = san;
271
+ }
272
+ }
273
+ }
274
+ }
124
275
  }
125
276
  }
126
277
  async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent, maxTurns = 10, context) {
127
278
  if (tools === void 0) {
128
- throw new RuntimeError3("RILL-R004", "tools parameter is required");
279
+ throw new RuntimeError4("RILL-R004", "tools parameter is required");
129
280
  }
130
281
  if (!isDict(tools)) {
131
- throw new RuntimeError3("RILL-R004", "tools must be a dict mapping tool names to functions");
282
+ throw new RuntimeError4("RILL-R004", "tool_loop: tools must be a dict of name \u2192 callable");
132
283
  }
133
284
  const toolsDict = tools;
134
285
  const toolDescriptors = Object.entries(toolsDict).map(([name, fn]) => {
135
286
  const fnValue = fn;
287
+ if (isRuntimeCallable(fnValue)) {
288
+ throw new RuntimeError4("RILL-R004", `tool_loop: builtin "${name}" cannot be used as a tool \u2014 wrap in a closure`);
289
+ }
136
290
  if (!isCallable(fnValue)) {
137
- throw new RuntimeError3("RILL-R004", `tool '${name}' must be callable function`);
291
+ throw new RuntimeError4("RILL-R004", `tool_loop: tool "${name}" is not a callable`);
138
292
  }
139
293
  const callable = fnValue;
140
- const description = callable.kind === "application" && callable.description ? callable.description : "";
141
- const properties = {};
142
- const required = [];
143
- if (callable.kind === "application" && callable.params) {
144
- for (const param of callable.params) {
145
- let jsonSchemaType;
146
- switch (param.typeName) {
147
- case "string":
148
- jsonSchemaType = "string";
149
- break;
150
- case "number":
151
- jsonSchemaType = "number";
152
- break;
153
- case "bool":
154
- jsonSchemaType = "boolean";
155
- break;
156
- case "list":
157
- jsonSchemaType = "array";
158
- break;
159
- case "dict":
160
- case "vector":
161
- jsonSchemaType = "object";
162
- break;
163
- case null:
164
- jsonSchemaType = "string";
165
- break;
294
+ let description;
295
+ if (callable.kind === "script") {
296
+ description = callable.annotations["description"] ?? "";
297
+ } else {
298
+ description = callable.description ?? "";
299
+ }
300
+ let inputSchema;
301
+ const params = callable.kind === "application" ? callable.params ?? [] : callable.kind === "script" ? callable.params : [];
302
+ if (params.length > 0) {
303
+ const properties = {};
304
+ const required = [];
305
+ for (const param of params) {
306
+ const property = {};
307
+ if (param.typeName !== null) {
308
+ const descriptor = {
309
+ [param.name]: { type: param.typeName }
310
+ };
311
+ const schema = buildJsonSchema(descriptor);
312
+ const built = schema.properties[param.name];
313
+ if (built !== void 0) {
314
+ Object.assign(property, built);
315
+ }
316
+ }
317
+ let paramDesc;
318
+ if (callable.kind === "script") {
319
+ const annot = callable.paramAnnotations[param.name];
320
+ paramDesc = annot?.["description"] ?? "";
321
+ } else {
322
+ paramDesc = param.description ?? "";
166
323
  }
167
- const property = {
168
- type: jsonSchemaType
169
- };
170
- if (param.description) {
171
- property["description"] = param.description;
324
+ if (paramDesc) {
325
+ property["description"] = paramDesc;
172
326
  }
173
327
  properties[param.name] = property;
174
328
  if (param.defaultValue === null) {
175
329
  required.push(param.name);
176
330
  }
177
331
  }
332
+ inputSchema = {
333
+ type: "object",
334
+ properties,
335
+ required
336
+ };
337
+ } else {
338
+ inputSchema = { type: "object", properties: {}, required: [] };
178
339
  }
179
340
  return {
180
341
  name,
181
342
  description,
182
- input_schema: {
183
- type: "object",
184
- properties,
185
- required
186
- }
343
+ input_schema: inputSchema
187
344
  };
188
345
  });
189
346
  const providerTools = callbacks.buildTools(toolDescriptors);
@@ -200,7 +357,7 @@ async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent,
200
357
  response = await callbacks.callAPI(currentMessages, providerTools);
201
358
  } catch (error) {
202
359
  const message = error instanceof Error ? error.message : "Unknown error";
203
- throw new RuntimeError3("RILL-R004", `Provider API error: ${message}`, void 0, { cause: error });
360
+ throw new RuntimeError4("RILL-R004", `Provider API error: ${message}`, void 0, { cause: error });
204
361
  }
205
362
  if (typeof response === "object" && response !== null && "usage" in response) {
206
363
  const usage = response["usage"];
@@ -212,7 +369,17 @@ async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent,
212
369
  totalOutputTokens += outputTokens;
213
370
  }
214
371
  }
215
- const toolCalls = callbacks.extractToolCalls(response);
372
+ const rawToolCalls = callbacks.extractToolCalls(response);
373
+ const nameMap = /* @__PURE__ */ new Map();
374
+ const toolCalls = rawToolCalls?.map((tc) => {
375
+ const sanitized = sanitizeToolName(tc.name);
376
+ if (sanitized !== tc.name)
377
+ nameMap.set(tc.name, sanitized);
378
+ return sanitized !== tc.name ? { ...tc, name: sanitized } : tc;
379
+ }) ?? null;
380
+ if (nameMap.size > 0) {
381
+ patchResponseToolCallNames(response, nameMap);
382
+ }
216
383
  if (toolCalls === null || toolCalls.length === 0) {
217
384
  return {
218
385
  response,
@@ -237,7 +404,7 @@ async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent,
237
404
  const duration = Date.now() - toolStartTime;
238
405
  consecutiveErrors++;
239
406
  let originalError;
240
- if (error instanceof RuntimeError3) {
407
+ if (error instanceof RuntimeError4) {
241
408
  const prefix = `Invalid tool input for ${name}: `;
242
409
  if (error.message.startsWith(prefix)) {
243
410
  originalError = error.message.slice(prefix.length);
@@ -262,12 +429,20 @@ async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent,
262
429
  duration
263
430
  });
264
431
  if (consecutiveErrors >= maxErrors) {
265
- throw new RuntimeError3("RILL-R004", `Tool execution failed: ${maxErrors} consecutive errors`);
432
+ throw new RuntimeError4("RILL-R004", `Tool execution failed: ${maxErrors} consecutive errors (last: ${name}: ${originalError})`);
266
433
  }
267
434
  }
268
435
  }
436
+ const assistantMessage = callbacks.formatAssistantMessage(response);
437
+ if (assistantMessage != null) {
438
+ currentMessages.push(assistantMessage);
439
+ }
269
440
  const toolResultMessage = callbacks.formatToolResult(toolResults);
270
- currentMessages.push(toolResultMessage);
441
+ if (Array.isArray(toolResultMessage)) {
442
+ currentMessages.push(...toolResultMessage);
443
+ } else {
444
+ currentMessages.push(toolResultMessage);
445
+ }
271
446
  }
272
447
  return {
273
448
  response: null,
@@ -278,7 +453,7 @@ async function executeToolLoop(messages, tools, maxErrors, callbacks, emitEvent,
278
453
  }
279
454
 
280
455
  // src/factory.ts
281
- var DEFAULT_MAX_TOKENS = 4096;
456
+ var DEFAULT_MAX_COMPLETION_TOKENS = 4096;
282
457
  var detectOpenAIError = (error) => {
283
458
  if (error instanceof OpenAI.APIError) {
284
459
  return {
@@ -300,7 +475,7 @@ function createOpenAIExtension(config) {
300
475
  });
301
476
  const factoryModel = config.model;
302
477
  const factoryTemperature = config.temperature;
303
- const factoryMaxTokens = config.max_tokens ?? DEFAULT_MAX_TOKENS;
478
+ const factoryMaxTokens = config.max_tokens ?? DEFAULT_MAX_COMPLETION_TOKENS;
304
479
  const factorySystem = config.system;
305
480
  const factoryEmbedModel = config.embed_model;
306
481
  void factoryEmbedModel;
@@ -334,7 +509,7 @@ function createOpenAIExtension(config) {
334
509
  const text = args[0];
335
510
  const options = args[1] ?? {};
336
511
  if (text.trim().length === 0) {
337
- throw new RuntimeError4("RILL-R004", "prompt text cannot be empty");
512
+ throw new RuntimeError5("RILL-R004", "prompt text cannot be empty");
338
513
  }
339
514
  const system = typeof options["system"] === "string" ? options["system"] : factorySystem;
340
515
  const maxTokens = typeof options["max_tokens"] === "number" ? options["max_tokens"] : factoryMaxTokens;
@@ -351,7 +526,7 @@ function createOpenAIExtension(config) {
351
526
  });
352
527
  const apiParams = {
353
528
  model: factoryModel,
354
- max_tokens: maxTokens,
529
+ max_completion_tokens: maxTokens,
355
530
  messages: apiMessages
356
531
  };
357
532
  if (factoryTemperature !== void 0) {
@@ -380,7 +555,9 @@ function createOpenAIExtension(config) {
380
555
  subsystem: "extension:openai",
381
556
  duration,
382
557
  model: response.model,
383
- usage: result2.usage
558
+ usage: result2.usage,
559
+ request: apiMessages,
560
+ content
384
561
  });
385
562
  return result2;
386
563
  } catch (error) {
@@ -414,7 +591,7 @@ function createOpenAIExtension(config) {
414
591
  const messages = args[0];
415
592
  const options = args[1] ?? {};
416
593
  if (messages.length === 0) {
417
- throw new RuntimeError4(
594
+ throw new RuntimeError5(
418
595
  "RILL-R004",
419
596
  "messages list cannot be empty"
420
597
  );
@@ -431,18 +608,18 @@ function createOpenAIExtension(config) {
431
608
  for (let i = 0; i < messages.length; i++) {
432
609
  const msg = messages[i];
433
610
  if (!msg || typeof msg !== "object" || !("role" in msg)) {
434
- throw new RuntimeError4(
611
+ throw new RuntimeError5(
435
612
  "RILL-R004",
436
613
  "message missing required 'role' field"
437
614
  );
438
615
  }
439
616
  const role = msg["role"];
440
617
  if (role !== "user" && role !== "assistant" && role !== "tool") {
441
- throw new RuntimeError4("RILL-R004", `invalid role '${role}'`);
618
+ throw new RuntimeError5("RILL-R004", `invalid role '${role}'`);
442
619
  }
443
620
  if (role === "user" || role === "tool") {
444
621
  if (!("content" in msg) || typeof msg["content"] !== "string") {
445
- throw new RuntimeError4(
622
+ throw new RuntimeError5(
446
623
  "RILL-R004",
447
624
  `${role} message requires 'content'`
448
625
  );
@@ -455,7 +632,7 @@ function createOpenAIExtension(config) {
455
632
  const hasContent = "content" in msg && msg["content"];
456
633
  const hasToolCalls = "tool_calls" in msg && msg["tool_calls"];
457
634
  if (!hasContent && !hasToolCalls) {
458
- throw new RuntimeError4(
635
+ throw new RuntimeError5(
459
636
  "RILL-R004",
460
637
  "assistant message requires 'content' or 'tool_calls'"
461
638
  );
@@ -470,7 +647,7 @@ function createOpenAIExtension(config) {
470
647
  }
471
648
  const apiParams = {
472
649
  model: factoryModel,
473
- max_tokens: maxTokens,
650
+ max_completion_tokens: maxTokens,
474
651
  messages: apiMessages
475
652
  };
476
653
  if (factoryTemperature !== void 0) {
@@ -504,7 +681,9 @@ function createOpenAIExtension(config) {
504
681
  subsystem: "extension:openai",
505
682
  duration,
506
683
  model: response.model,
507
- usage: result2.usage
684
+ usage: result2.usage,
685
+ request: apiMessages,
686
+ content
508
687
  });
509
688
  return result2;
510
689
  } catch (error) {
@@ -542,7 +721,7 @@ function createOpenAIExtension(config) {
542
721
  });
543
722
  const embeddingData = response.data[0]?.embedding;
544
723
  if (!embeddingData || embeddingData.length === 0) {
545
- throw new RuntimeError4(
724
+ throw new RuntimeError5(
546
725
  "RILL-R004",
547
726
  "OpenAI: empty embedding returned"
548
727
  );
@@ -598,7 +777,7 @@ function createOpenAIExtension(config) {
598
777
  for (const embeddingItem of response.data) {
599
778
  const embeddingData = embeddingItem.embedding;
600
779
  if (!embeddingData || embeddingData.length === 0) {
601
- throw new RuntimeError4(
780
+ throw new RuntimeError5(
602
781
  "RILL-R004",
603
782
  "OpenAI: empty embedding returned"
604
783
  );
@@ -650,79 +829,14 @@ function createOpenAIExtension(config) {
650
829
  const prompt = args[0];
651
830
  const options = args[1] ?? {};
652
831
  if (prompt.trim().length === 0) {
653
- throw new RuntimeError4("RILL-R004", "prompt text cannot be empty");
832
+ throw new RuntimeError5("RILL-R004", "prompt text cannot be empty");
654
833
  }
655
- if (!("tools" in options) || !Array.isArray(options["tools"])) {
656
- throw new RuntimeError4(
834
+ if (!("tools" in options) || !isDict2(options["tools"])) {
835
+ throw new RuntimeError5(
657
836
  "RILL-R004",
658
837
  "tool_loop requires 'tools' option"
659
838
  );
660
839
  }
661
- const toolDescriptors = options["tools"];
662
- const toolsDict = {};
663
- for (const descriptor of toolDescriptors) {
664
- const name = typeof descriptor["name"] === "string" ? descriptor["name"] : null;
665
- if (!name) {
666
- throw new RuntimeError4(
667
- "RILL-R004",
668
- "tool descriptor missing name"
669
- );
670
- }
671
- const toolFnValue = descriptor["fn"];
672
- if (!toolFnValue) {
673
- throw new RuntimeError4(
674
- "RILL-R004",
675
- `tool '${name}' missing fn property`
676
- );
677
- }
678
- if (!isCallable2(toolFnValue)) {
679
- throw new RuntimeError4(
680
- "RILL-R004",
681
- `tool '${name}' fn must be callable`
682
- );
683
- }
684
- const paramsObj = descriptor["params"];
685
- const description = typeof descriptor["description"] === "string" ? descriptor["description"] : "";
686
- let enhancedCallable = toolFnValue;
687
- if (paramsObj && typeof paramsObj === "object" && !Array.isArray(paramsObj)) {
688
- const params = Object.entries(
689
- paramsObj
690
- ).map(([paramName, paramMeta]) => {
691
- const meta = paramMeta;
692
- const typeStr = typeof meta["type"] === "string" ? meta["type"] : null;
693
- let typeName = null;
694
- if (typeStr === "string") typeName = "string";
695
- else if (typeStr === "number") typeName = "number";
696
- else if (typeStr === "bool" || typeStr === "boolean")
697
- typeName = "bool";
698
- else if (typeStr === "list" || typeStr === "array")
699
- typeName = "list";
700
- else if (typeStr === "dict" || typeStr === "object")
701
- typeName = "dict";
702
- else if (typeStr === "vector") typeName = "vector";
703
- const param = {
704
- name: paramName,
705
- typeName,
706
- defaultValue: null,
707
- annotations: {}
708
- };
709
- if (typeof meta["description"] === "string") {
710
- param.description = meta["description"];
711
- }
712
- return param;
713
- });
714
- const baseCallable = toolFnValue;
715
- enhancedCallable = {
716
- __type: "callable",
717
- kind: "application",
718
- params,
719
- fn: baseCallable.fn,
720
- description,
721
- isProperty: baseCallable.isProperty ?? false
722
- };
723
- }
724
- toolsDict[name] = enhancedCallable;
725
- }
726
840
  const system = typeof options["system"] === "string" ? options["system"] : factorySystem;
727
841
  const maxTokens = typeof options["max_tokens"] === "number" ? options["max_tokens"] : factoryMaxTokens;
728
842
  const maxErrors = typeof options["max_errors"] === "number" ? options["max_errors"] : 3;
@@ -738,17 +852,17 @@ function createOpenAIExtension(config) {
738
852
  const prependedMessages = options["messages"];
739
853
  for (const msg of prependedMessages) {
740
854
  if (!msg || typeof msg !== "object" || !("role" in msg)) {
741
- throw new RuntimeError4(
855
+ throw new RuntimeError5(
742
856
  "RILL-R004",
743
857
  "message missing required 'role' field"
744
858
  );
745
859
  }
746
860
  const role = msg["role"];
747
861
  if (role !== "user" && role !== "assistant") {
748
- throw new RuntimeError4("RILL-R004", `invalid role '${role}'`);
862
+ throw new RuntimeError5("RILL-R004", `invalid role '${role}'`);
749
863
  }
750
864
  if (!("content" in msg) || typeof msg["content"] !== "string") {
751
- throw new RuntimeError4(
865
+ throw new RuntimeError5(
752
866
  "RILL-R004",
753
867
  `${role} message requires 'content'`
754
868
  );
@@ -779,7 +893,7 @@ function createOpenAIExtension(config) {
779
893
  callAPI: async (msgs, tools) => {
780
894
  const apiParams = {
781
895
  model: factoryModel,
782
- max_tokens: maxTokens,
896
+ max_completion_tokens: maxTokens,
783
897
  messages: msgs,
784
898
  tools,
785
899
  tool_choice: "auto"
@@ -836,6 +950,21 @@ function createOpenAIExtension(config) {
836
950
  };
837
951
  });
838
952
  },
953
+ // Extract assistant message (with tool_calls) from OpenAI response
954
+ formatAssistantMessage: (response2) => {
955
+ if (!response2 || typeof response2 !== "object" || !("choices" in response2)) {
956
+ return null;
957
+ }
958
+ const choices = response2.choices;
959
+ if (!Array.isArray(choices) || choices.length === 0) {
960
+ return null;
961
+ }
962
+ const choice = choices[0];
963
+ if (!choice || typeof choice !== "object" || !("message" in choice)) {
964
+ return null;
965
+ }
966
+ return choice.message;
967
+ },
839
968
  // Format tool results into OpenAI message format
840
969
  formatToolResult: (toolResults) => {
841
970
  return toolResults.map((tr) => ({
@@ -847,7 +976,7 @@ function createOpenAIExtension(config) {
847
976
  };
848
977
  const loopResult = await executeToolLoop(
849
978
  messages,
850
- toolsDict,
979
+ options["tools"],
851
980
  maxErrors,
852
981
  callbacks,
853
982
  (event, data) => {
@@ -905,7 +1034,9 @@ function createOpenAIExtension(config) {
905
1034
  subsystem: "extension:openai",
906
1035
  turns: loopResult.turns,
907
1036
  total_duration: duration,
908
- usage: result2.usage
1037
+ usage: result2.usage,
1038
+ request: messages,
1039
+ content
909
1040
  });
910
1041
  return result2;
911
1042
  } catch (error) {
@@ -926,6 +1057,125 @@ function createOpenAIExtension(config) {
926
1057
  },
927
1058
  description: "Execute tool-use loop with OpenAI API",
928
1059
  returnType: "dict"
1060
+ },
1061
+ // IR-3: openai::generate
1062
+ generate: {
1063
+ params: [
1064
+ { name: "prompt", type: "string" },
1065
+ { name: "options", type: "dict" }
1066
+ ],
1067
+ fn: async (args, ctx) => {
1068
+ const startTime = Date.now();
1069
+ try {
1070
+ const prompt = args[0];
1071
+ const options = args[1] ?? {};
1072
+ if (!("schema" in options) || options["schema"] === null || options["schema"] === void 0) {
1073
+ throw new RuntimeError5(
1074
+ "RILL-R004",
1075
+ "generate requires 'schema' option"
1076
+ );
1077
+ }
1078
+ const rillSchema = options["schema"];
1079
+ const jsonSchema = buildJsonSchema(rillSchema);
1080
+ const system = typeof options["system"] === "string" ? options["system"] : factorySystem;
1081
+ const maxTokens = typeof options["max_tokens"] === "number" ? options["max_tokens"] : factoryMaxTokens;
1082
+ const apiMessages = [];
1083
+ if (system !== void 0) {
1084
+ apiMessages.push({
1085
+ role: "system",
1086
+ content: system
1087
+ });
1088
+ }
1089
+ if ("messages" in options && Array.isArray(options["messages"])) {
1090
+ const prependedMessages = options["messages"];
1091
+ for (const msg of prependedMessages) {
1092
+ if (!msg || typeof msg !== "object" || !("role" in msg)) {
1093
+ throw new RuntimeError5(
1094
+ "RILL-R004",
1095
+ "message missing required 'role' field"
1096
+ );
1097
+ }
1098
+ const role = msg["role"];
1099
+ if (role !== "user" && role !== "assistant") {
1100
+ throw new RuntimeError5("RILL-R004", `invalid role '${role}'`);
1101
+ }
1102
+ if (!("content" in msg) || typeof msg["content"] !== "string") {
1103
+ throw new RuntimeError5(
1104
+ "RILL-R004",
1105
+ `${role} message requires 'content'`
1106
+ );
1107
+ }
1108
+ apiMessages.push({
1109
+ role,
1110
+ content: msg["content"]
1111
+ });
1112
+ }
1113
+ }
1114
+ apiMessages.push({ role: "user", content: prompt });
1115
+ const apiParams = {
1116
+ model: factoryModel,
1117
+ max_completion_tokens: maxTokens,
1118
+ messages: apiMessages,
1119
+ response_format: {
1120
+ type: "json_schema",
1121
+ json_schema: {
1122
+ name: "output",
1123
+ schema: jsonSchema,
1124
+ strict: true
1125
+ }
1126
+ }
1127
+ };
1128
+ if (factoryTemperature !== void 0) {
1129
+ apiParams.temperature = factoryTemperature;
1130
+ }
1131
+ const response = await client.chat.completions.create(apiParams);
1132
+ const raw = response.choices[0]?.message?.content ?? "";
1133
+ let data;
1134
+ try {
1135
+ data = JSON.parse(raw);
1136
+ } catch (parseError) {
1137
+ const detail = parseError instanceof Error ? parseError.message : String(parseError);
1138
+ throw new RuntimeError5(
1139
+ "RILL-R004",
1140
+ `generate: failed to parse response JSON: ${detail}`
1141
+ );
1142
+ }
1143
+ const result2 = {
1144
+ data,
1145
+ raw,
1146
+ model: response.model,
1147
+ usage: {
1148
+ input: response.usage?.prompt_tokens ?? 0,
1149
+ output: response.usage?.completion_tokens ?? 0
1150
+ },
1151
+ stop_reason: response.choices[0]?.finish_reason ?? "unknown",
1152
+ id: response.id
1153
+ };
1154
+ const duration = Date.now() - startTime;
1155
+ emitExtensionEvent(ctx, {
1156
+ event: "openai:generate",
1157
+ subsystem: "extension:openai",
1158
+ duration,
1159
+ model: response.model,
1160
+ usage: result2.usage,
1161
+ request: apiMessages,
1162
+ content: raw
1163
+ });
1164
+ return result2;
1165
+ } catch (error) {
1166
+ const duration = Date.now() - startTime;
1167
+ const rillError = error instanceof RuntimeError5 ? error : mapProviderError("OpenAI", error, detectOpenAIError);
1168
+ emitExtensionEvent(ctx, {
1169
+ event: "openai:error",
1170
+ subsystem: "extension:openai",
1171
+ error: rillError.message,
1172
+ duration
1173
+ });
1174
+ throw rillError;
1175
+ }
1176
+ },
1177
+ description: "Generate structured output from OpenAI API",
1178
+ returnType: "dict"
929
1179
  }
930
1180
  };
931
1181
  result.dispose = dispose;
@@ -934,7 +1184,19 @@ function createOpenAIExtension(config) {
934
1184
 
935
1185
  // src/index.ts
936
1186
  var VERSION = "0.0.1";
1187
+ var configSchema = {
1188
+ api_key: { type: "string", required: true, secret: true },
1189
+ model: { type: "string", required: true },
1190
+ base_url: { type: "string" },
1191
+ temperature: { type: "number" },
1192
+ max_tokens: { type: "number" },
1193
+ timeout: { type: "number" },
1194
+ max_retries: { type: "number" },
1195
+ system: { type: "string" },
1196
+ embed_model: { type: "string" }
1197
+ };
937
1198
  export {
938
1199
  VERSION,
1200
+ configSchema,
939
1201
  createOpenAIExtension
940
1202
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rcrsr/rill-ext-openai",
3
- "version": "0.8.5",
3
+ "version": "0.9.0",
4
4
  "description": "rill extension for OpenAI API integration",
5
5
  "license": "MIT",
6
6
  "author": "Andre Bremer",
@@ -17,14 +17,14 @@
17
17
  "scripting"
18
18
  ],
19
19
  "peerDependencies": {
20
- "@rcrsr/rill": "^0.8.5"
20
+ "@rcrsr/rill": "^0.9.0"
21
21
  },
22
22
  "devDependencies": {
23
- "@types/node": "^25.2.3",
23
+ "@rcrsr/rill": "^0.9.0",
24
+ "@types/node": "^25.3.0",
24
25
  "dts-bundle-generator": "^9.5.1",
25
- "tsup": "^8.5.0",
26
- "undici-types": "^7.21.0",
27
- "@rcrsr/rill": "^0.8.5",
26
+ "tsup": "^8.5.1",
27
+ "undici-types": "^7.22.0",
28
28
  "@rcrsr/rill-ext-llm-shared": "^0.0.1"
29
29
  },
30
30
  "files": [
@@ -32,18 +32,18 @@
32
32
  ],
33
33
  "repository": {
34
34
  "type": "git",
35
- "url": "git+https://github.com/rcrsr/rill.git",
36
- "directory": "packages/ext/openai"
35
+ "url": "git+https://github.com/rcrsr/rill-ext.git",
36
+ "directory": "packages/ext/llm-openai"
37
37
  },
38
- "homepage": "https://rill.run/docs/extensions/openai/",
38
+ "homepage": "https://github.com/rcrsr/rill-ext/tree/main/packages/ext/llm-openai#readme",
39
39
  "bugs": {
40
- "url": "https://github.com/rcrsr/rill/issues"
40
+ "url": "https://github.com/rcrsr/rill-ext/issues"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public"
44
44
  },
45
45
  "dependencies": {
46
- "openai": "^6.18.0"
46
+ "openai": "^6.25.0"
47
47
  },
48
48
  "scripts": {
49
49
  "build": "tsup && dts-bundle-generator --config dts-bundle-generator.config.cjs",