@rcrsr/rill-ext-openai 0.18.1 → 0.18.3

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.
Files changed (2) hide show
  1. package/dist/index.js +68 -77
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -117,94 +117,67 @@ function buildPropertyFromStructuralType(rillType) {
117
117
  return property;
118
118
  }
119
119
  if (rillType.kind === "dict") {
120
+ const dictType = rillType;
121
+ if (dictType.fields && Object.keys(dictType.fields).length > 0) {
122
+ const nested = buildDictSchema(dictType);
123
+ return {
124
+ type: "object",
125
+ properties: nested.properties,
126
+ required: nested.required,
127
+ additionalProperties: false
128
+ };
129
+ }
120
130
  return { type: "object" };
121
131
  }
122
132
  return { type: mapRillType(rillType.kind) };
123
133
  }
124
- function buildJsonSchemaFromStructuralType(type, params) {
134
+ function buildDictSchema(dictType) {
125
135
  const properties = {};
126
136
  const required = [];
127
- if (type.kind === "closure") {
128
- const closureParams = type.params ?? [];
129
- for (let i = 0; i < closureParams.length; i++) {
130
- const fieldDef = closureParams[i];
131
- const paramName = fieldDef.name ?? `param${i}`;
132
- const paramType = fieldDef.type;
133
- const rillParam = params?.[i];
134
- const property = buildPropertyFromStructuralType(paramType);
135
- const description = rillParam?.annotations["description"];
136
- if (typeof description === "string") {
137
- property.description = description;
138
- }
139
- const enumAnnotation = rillParam?.annotations["enum"];
140
- if (Array.isArray(enumAnnotation)) {
141
- property.enum = enumAnnotation;
142
- }
143
- properties[paramName] = property;
144
- if (rillParam === void 0 || rillParam.defaultValue === void 0) {
145
- required.push(paramName);
146
- }
137
+ const fields = dictType.fields ?? {};
138
+ for (const [name, fieldDef] of Object.entries(fields)) {
139
+ const property = buildPropertyFromStructuralType(fieldDef.type);
140
+ const description = fieldDef.annotations?.["description"];
141
+ if (typeof description === "string") {
142
+ property.description = description;
147
143
  }
148
- }
149
- return { type: "object", properties, required, additionalProperties: false };
150
- }
151
- function buildJsonSchema(rillSchema) {
152
- const properties = {};
153
- const required = [];
154
- for (const [key, value] of Object.entries(rillSchema)) {
155
- if (typeof value === "string") {
156
- properties[key] = buildProperty(value);
157
- } else if (typeof value === "object" && value !== null) {
158
- properties[key] = buildProperty(value);
159
- } else {
160
- throw new RuntimeError3("RILL-R004", `unsupported type: ${String(value)}`);
144
+ properties[name] = property;
145
+ if (fieldDef.defaultValue === void 0) {
146
+ required.push(name);
161
147
  }
162
- required.push(key);
163
148
  }
164
149
  return { type: "object", properties, required, additionalProperties: false };
165
150
  }
166
- function buildProperty(descriptor) {
167
- if (typeof descriptor === "string") {
168
- const jsonType2 = mapRillType(descriptor);
169
- return { type: jsonType2 };
170
- }
171
- const rillType = descriptor["type"];
172
- if (typeof rillType !== "string") {
173
- throw new RuntimeError3("RILL-R004", `unsupported type: ${String(rillType)}`);
151
+ function buildJsonSchemaFromStructuralType(type, params) {
152
+ if (type.kind === "dict") {
153
+ return buildDictSchema(type);
174
154
  }
175
- const jsonType = mapRillType(rillType);
176
- const property = { type: jsonType };
177
- const description = descriptor["description"];
178
- if (typeof description === "string") {
179
- property.description = description;
155
+ if (type.kind !== "closure") {
156
+ throw new RuntimeError3("RILL-R004", `unsupported schema kind: ${type.kind} (expected dict or closure)`);
180
157
  }
181
- if ("enum" in descriptor) {
182
- if (rillType !== "string") {
183
- throw new RuntimeError3("RILL-R004", "enum is only valid for string type");
184
- }
185
- const enumValues = descriptor["enum"];
186
- if (Array.isArray(enumValues)) {
187
- property.enum = enumValues;
158
+ const properties = {};
159
+ const required = [];
160
+ const closureParams = type.params ?? [];
161
+ for (let i = 0; i < closureParams.length; i++) {
162
+ const fieldDef = closureParams[i];
163
+ const paramName = fieldDef.name ?? `param${i}`;
164
+ const paramType = fieldDef.type;
165
+ const rillParam = params?.[i];
166
+ const property = buildPropertyFromStructuralType(paramType);
167
+ const description = rillParam?.annotations["description"];
168
+ if (typeof description === "string") {
169
+ property.description = description;
188
170
  }
189
- }
190
- if (rillType === "list" && "items" in descriptor) {
191
- const items = descriptor["items"];
192
- if (typeof items === "string") {
193
- property.items = buildProperty(items);
194
- } else if (typeof items === "object" && items !== null) {
195
- property.items = buildProperty(items);
171
+ const enumAnnotation = rillParam?.annotations["enum"];
172
+ if (Array.isArray(enumAnnotation)) {
173
+ property.enum = enumAnnotation;
196
174
  }
197
- }
198
- if (rillType === "dict" && "properties" in descriptor) {
199
- const nestedProps = descriptor["properties"];
200
- if (typeof nestedProps === "object" && nestedProps !== null) {
201
- const subSchema = buildJsonSchema(nestedProps);
202
- property.properties = subSchema.properties;
203
- property.required = subSchema.required;
204
- property.additionalProperties = false;
175
+ properties[paramName] = property;
176
+ if (rillParam === void 0 || rillParam.defaultValue === void 0) {
177
+ required.push(paramName);
205
178
  }
206
179
  }
207
- return property;
180
+ return { type: "object", properties, required, additionalProperties: false };
208
181
  }
209
182
 
210
183
  // ../../shared/ext-llm/dist/tool-loop.js
@@ -1249,7 +1222,19 @@ function createOpenAIExtension(config) {
1249
1222
  if (!choice || typeof choice !== "object" || !("message" in choice)) {
1250
1223
  return null;
1251
1224
  }
1252
- return choice.message;
1225
+ const msg = choice.message;
1226
+ if (!msg || typeof msg !== "object") {
1227
+ return null;
1228
+ }
1229
+ const m = msg;
1230
+ const clean = {
1231
+ role: m["role"],
1232
+ content: m["content"]
1233
+ };
1234
+ if (m["tool_calls"]) {
1235
+ clean["tool_calls"] = m["tool_calls"];
1236
+ }
1237
+ return clean;
1253
1238
  },
1254
1239
  // Format tool results into OpenAI message format
1255
1240
  formatToolResult: (toolResults) => {
@@ -1387,8 +1372,8 @@ function createOpenAIExtension(config) {
1387
1372
  generate: {
1388
1373
  params: [
1389
1374
  p.str("prompt"),
1375
+ { name: "schema", type: { kind: "type" }, defaultValue: void 0, annotations: { description: "Type expression for structured output schema" } },
1390
1376
  p.dict("options", void 0, {}, {
1391
- schema: { type: { kind: "dict" } },
1392
1377
  system: { type: { kind: "string" }, defaultValue: "" },
1393
1378
  max_tokens: { type: { kind: "number" }, defaultValue: 0 },
1394
1379
  messages: { type: { kind: "list", element: { kind: "dict", fields: { role: { type: { kind: "string" } }, content: { type: { kind: "string" } } } } }, defaultValue: [] }
@@ -1398,15 +1383,21 @@ function createOpenAIExtension(config) {
1398
1383
  const startTime = Date.now();
1399
1384
  try {
1400
1385
  const prompt = args["prompt"];
1386
+ const schemaArg = args["schema"];
1401
1387
  const options = args["options"] ?? {};
1402
- if (!("schema" in options) || options["schema"] === null || options["schema"] === void 0) {
1388
+ if (!schemaArg || !schemaArg.__rill_type || !schemaArg.structure) {
1389
+ throw new RuntimeError6(
1390
+ "RILL-R004",
1391
+ "generate requires a type expression as schema"
1392
+ );
1393
+ }
1394
+ if (schemaArg.structure.kind !== "dict") {
1403
1395
  throw new RuntimeError6(
1404
1396
  "RILL-R004",
1405
- "generate requires 'schema' option"
1397
+ `generate requires a dict type as schema, got ${schemaArg.structure.kind}`
1406
1398
  );
1407
1399
  }
1408
- const rillSchema = options["schema"];
1409
- const jsonSchema = buildJsonSchema(rillSchema);
1400
+ const jsonSchema = buildJsonSchemaFromStructuralType(schemaArg.structure);
1410
1401
  const system = typeof options["system"] === "string" ? options["system"] : factorySystem;
1411
1402
  const maxTokens = typeof options["max_tokens"] === "number" && options["max_tokens"] > 0 ? options["max_tokens"] : factoryMaxTokens;
1412
1403
  const apiMessages = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rcrsr/rill-ext-openai",
3
- "version": "0.18.1",
3
+ "version": "0.18.3",
4
4
  "description": "rill extension for OpenAI API integration",
5
5
  "license": "MIT",
6
6
  "author": "Andre Bremer",
@@ -25,8 +25,8 @@
25
25
  "dts-bundle-generator": "^9.5.1",
26
26
  "tsup": "^8.5.1",
27
27
  "undici-types": "^7.22.0",
28
- "@rcrsr/rill-ext-llm-shared": "^0.18.1",
29
- "@rcrsr/rill-ext-param-shared": "0.18.1"
28
+ "@rcrsr/rill-ext-param-shared": "0.18.1",
29
+ "@rcrsr/rill-ext-llm-shared": "^0.18.3"
30
30
  },
31
31
  "files": [
32
32
  "dist"