modelfusion 0.42.0 → 0.44.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.
Files changed (51) hide show
  1. package/README.md +42 -2
  2. package/core/getRun.cjs +60 -0
  3. package/core/getRun.d.ts +9 -0
  4. package/core/getRun.js +32 -0
  5. package/core/index.cjs +1 -0
  6. package/core/index.d.ts +1 -0
  7. package/core/index.js +1 -0
  8. package/guard/fixStructure.cjs +21 -0
  9. package/guard/fixStructure.d.ts +10 -0
  10. package/guard/fixStructure.js +17 -0
  11. package/guard/guard.cjs +72 -0
  12. package/guard/guard.d.ts +28 -0
  13. package/guard/guard.js +68 -0
  14. package/guard/index.cjs +18 -0
  15. package/guard/index.d.ts +2 -0
  16. package/guard/index.js +2 -0
  17. package/index.cjs +1 -0
  18. package/index.d.ts +1 -0
  19. package/index.js +1 -0
  20. package/model-function/executeCall.cjs +8 -2
  21. package/model-function/executeCall.js +8 -2
  22. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +15 -4
  23. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +2 -1
  24. package/model-function/generate-structure/StructureFromTextGenerationModel.js +15 -4
  25. package/model-function/generate-structure/StructureGenerationModel.d.ts +2 -1
  26. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +2 -0
  27. package/model-function/generate-structure/StructureParseError.cjs +34 -0
  28. package/model-function/generate-structure/StructureParseError.d.ts +10 -0
  29. package/model-function/generate-structure/StructureParseError.js +30 -0
  30. package/model-function/generate-structure/StructureValidationError.cjs +10 -3
  31. package/model-function/generate-structure/StructureValidationError.d.ts +3 -1
  32. package/model-function/generate-structure/StructureValidationError.js +10 -3
  33. package/model-function/generate-structure/generateStructure.cjs +2 -1
  34. package/model-function/generate-structure/generateStructure.js +2 -1
  35. package/model-function/generate-structure/generateStructureOrText.cjs +1 -0
  36. package/model-function/generate-structure/generateStructureOrText.js +1 -0
  37. package/model-function/generate-structure/streamStructure.cjs +8 -2
  38. package/model-function/generate-structure/streamStructure.js +8 -2
  39. package/model-function/generate-text/streamText.cjs +8 -2
  40. package/model-function/generate-text/streamText.js +8 -2
  41. package/model-function/index.cjs +1 -0
  42. package/model-function/index.d.ts +1 -0
  43. package/model-function/index.js +1 -0
  44. package/model-provider/openai/chat/OpenAIChatModel.cjs +47 -20
  45. package/model-provider/openai/chat/OpenAIChatModel.d.ts +36 -2
  46. package/model-provider/openai/chat/OpenAIChatModel.js +47 -20
  47. package/package.json +1 -1
  48. package/retriever/retrieve.cjs +8 -2
  49. package/retriever/retrieve.js +8 -2
  50. package/tool/executeTool.cjs +8 -2
  51. package/tool/executeTool.js +8 -2
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructureParseError = void 0;
4
+ const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
+ class StructureParseError extends Error {
6
+ constructor({ structureName, valueText, cause, }) {
7
+ super(`Structure parsing failed for '${structureName}'. ` +
8
+ `Value: ${valueText}.\n` +
9
+ `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
+ Object.defineProperty(this, "structureName", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "cause", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "valueText", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.name = "StructureParseError";
29
+ this.structureName = structureName;
30
+ this.cause = cause;
31
+ this.valueText = valueText;
32
+ }
33
+ }
34
+ exports.StructureParseError = StructureParseError;
@@ -0,0 +1,10 @@
1
+ export declare class StructureParseError extends Error {
2
+ readonly structureName: string;
3
+ readonly cause: unknown;
4
+ readonly valueText: string;
5
+ constructor({ structureName, valueText, cause, }: {
6
+ structureName: string;
7
+ valueText: string;
8
+ cause: unknown;
9
+ });
10
+ }
@@ -0,0 +1,30 @@
1
+ import { getErrorMessage } from "../../util/getErrorMessage.js";
2
+ export class StructureParseError extends Error {
3
+ constructor({ structureName, valueText, cause, }) {
4
+ super(`Structure parsing failed for '${structureName}'. ` +
5
+ `Value: ${valueText}.\n` +
6
+ `Error message: ${getErrorMessage(cause)}`);
7
+ Object.defineProperty(this, "structureName", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "cause", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "valueText", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.name = "StructureParseError";
26
+ this.structureName = structureName;
27
+ this.cause = cause;
28
+ this.valueText = valueText;
29
+ }
30
+ }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StructureValidationError = void 0;
4
4
  const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
5
  class StructureValidationError extends Error {
6
- constructor({ structureName, value, cause, }) {
7
- super(`Structure validation error for '${structureName}'. ` +
8
- `Value: ${JSON.stringify(value)}.\n` +
6
+ constructor({ structureName, value, valueText, cause, }) {
7
+ super(`Structure validation failed for '${structureName}'. ` +
8
+ `Value: ${valueText}.\n` +
9
9
  `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
10
  Object.defineProperty(this, "structureName", {
11
11
  enumerable: true,
@@ -19,6 +19,12 @@ class StructureValidationError extends Error {
19
19
  writable: true,
20
20
  value: void 0
21
21
  });
22
+ Object.defineProperty(this, "valueText", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
22
28
  Object.defineProperty(this, "value", {
23
29
  enumerable: true,
24
30
  configurable: true,
@@ -29,6 +35,7 @@ class StructureValidationError extends Error {
29
35
  this.structureName = structureName;
30
36
  this.cause = cause;
31
37
  this.value = value;
38
+ this.valueText = valueText;
32
39
  }
33
40
  }
34
41
  exports.StructureValidationError = StructureValidationError;
@@ -1,10 +1,12 @@
1
1
  export declare class StructureValidationError extends Error {
2
2
  readonly structureName: string;
3
3
  readonly cause: unknown;
4
+ readonly valueText: string;
4
5
  readonly value: unknown;
5
- constructor({ structureName, value, cause, }: {
6
+ constructor({ structureName, value, valueText, cause, }: {
6
7
  structureName: string;
7
8
  value: unknown;
9
+ valueText: string;
8
10
  cause: unknown;
9
11
  });
10
12
  }
@@ -1,8 +1,8 @@
1
1
  import { getErrorMessage } from "../../util/getErrorMessage.js";
2
2
  export class StructureValidationError extends Error {
3
- constructor({ structureName, value, cause, }) {
4
- super(`Structure validation error for '${structureName}'. ` +
5
- `Value: ${JSON.stringify(value)}.\n` +
3
+ constructor({ structureName, value, valueText, cause, }) {
4
+ super(`Structure validation failed for '${structureName}'. ` +
5
+ `Value: ${valueText}.\n` +
6
6
  `Error message: ${getErrorMessage(cause)}`);
7
7
  Object.defineProperty(this, "structureName", {
8
8
  enumerable: true,
@@ -16,6 +16,12 @@ export class StructureValidationError extends Error {
16
16
  writable: true,
17
17
  value: void 0
18
18
  });
19
+ Object.defineProperty(this, "valueText", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
19
25
  Object.defineProperty(this, "value", {
20
26
  enumerable: true,
21
27
  configurable: true,
@@ -26,5 +32,6 @@ export class StructureValidationError extends Error {
26
32
  this.structureName = structureName;
27
33
  this.cause = cause;
28
34
  this.value = value;
35
+ this.valueText = valueText;
29
36
  }
30
37
  }
@@ -15,11 +15,12 @@ function generateStructure(model, structureDefinition, prompt, options) {
15
15
  options,
16
16
  generateResponse: async (options) => {
17
17
  const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
18
- const structure = result.structure;
18
+ const structure = result.value;
19
19
  const parseResult = structureDefinition.schema.validate(structure);
20
20
  if (!parseResult.success) {
21
21
  throw new StructureValidationError_js_1.StructureValidationError({
22
22
  structureName: structureDefinition.name,
23
+ valueText: result.valueText,
23
24
  value: structure,
24
25
  cause: parseResult.error,
25
26
  });
@@ -12,11 +12,12 @@ export function generateStructure(model, structureDefinition, prompt, options) {
12
12
  options,
13
13
  generateResponse: async (options) => {
14
14
  const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
15
- const structure = result.structure;
15
+ const structure = result.value;
16
16
  const parseResult = structureDefinition.schema.validate(structure);
17
17
  if (!parseResult.success) {
18
18
  throw new StructureValidationError({
19
19
  structureName: structureDefinition.name,
20
+ valueText: result.valueText,
20
21
  value: structure,
21
22
  cause: parseResult.error,
22
23
  });
@@ -34,6 +34,7 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
34
34
  throw new StructureValidationError_js_1.StructureValidationError({
35
35
  structureName: structure,
36
36
  value,
37
+ valueText: result.structureAndText.valueText,
37
38
  cause: parseResult.error,
38
39
  });
39
40
  }
@@ -31,6 +31,7 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
31
31
  throw new StructureValidationError({
32
32
  structureName: structure,
33
33
  value,
34
+ valueText: result.structureAndText.valueText,
34
35
  cause: parseResult.error,
35
36
  });
36
37
  }
@@ -11,6 +11,7 @@ const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs
11
11
  const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
12
12
  const AbortError_js_1 = require("../../core/api/AbortError.cjs");
13
13
  const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
14
+ const getRun_js_1 = require("../../core/getRun.cjs");
14
15
  const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
15
16
  const runSafe_js_1 = require("../../util/runSafe.cjs");
16
17
  const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
@@ -19,7 +20,7 @@ function streamStructure(model, structureDefinition, prompt, options) {
19
20
  }
20
21
  exports.streamStructure = streamStructure;
21
22
  async function doStreamStructure(model, structureDefinition, prompt, options) {
22
- const run = options?.run;
23
+ const run = await (0, getRun_js_1.getRun)(options?.run);
23
24
  const settings = model.settings;
24
25
  const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
25
26
  observers: [
@@ -50,7 +51,12 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
50
51
  ...startMetadata,
51
52
  });
52
53
  const result = await (0, runSafe_js_1.runSafe)(async () => {
53
- const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, options);
54
+ const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, {
55
+ functionId: options?.functionId,
56
+ logging: options?.logging,
57
+ observers: options?.observers,
58
+ run,
59
+ });
54
60
  return (async function* () {
55
61
  function reportError(error) {
56
62
  const finishMetadata = {
@@ -5,6 +5,7 @@ import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
5
5
  import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
6
6
  import { AbortError } from "../../core/api/AbortError.js";
7
7
  import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
8
+ import { getRun } from "../../core/getRun.js";
8
9
  import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
9
10
  import { runSafe } from "../../util/runSafe.js";
10
11
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
@@ -12,7 +13,7 @@ export function streamStructure(model, structureDefinition, prompt, options) {
12
13
  return new AsyncIterableResultPromise(doStreamStructure(model, structureDefinition, prompt, options));
13
14
  }
14
15
  async function doStreamStructure(model, structureDefinition, prompt, options) {
15
- const run = options?.run;
16
+ const run = await getRun(options?.run);
16
17
  const settings = model.settings;
17
18
  const eventSource = new FunctionEventSource({
18
19
  observers: [
@@ -43,7 +44,12 @@ async function doStreamStructure(model, structureDefinition, prompt, options) {
43
44
  ...startMetadata,
44
45
  });
45
46
  const result = await runSafe(async () => {
46
- const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, options);
47
+ const deltaIterable = await model.doStreamStructure(structureDefinition, prompt, {
48
+ functionId: options?.functionId,
49
+ logging: options?.logging,
50
+ observers: options?.observers,
51
+ run,
52
+ });
47
53
  return (async function* () {
48
54
  function reportError(error) {
49
55
  const finishMetadata = {
@@ -7,6 +7,7 @@ const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs
7
7
  const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
8
8
  const AbortError_js_1 = require("../../core/api/AbortError.cjs");
9
9
  const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
10
+ const getRun_js_1 = require("../../core/getRun.cjs");
10
11
  const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
11
12
  const runSafe_js_1 = require("../../util/runSafe.cjs");
12
13
  const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
@@ -15,7 +16,7 @@ function streamText(model, prompt, options) {
15
16
  }
16
17
  exports.streamText = streamText;
17
18
  async function doStreamText(model, prompt, options) {
18
- const run = options?.run;
19
+ const run = await (0, getRun_js_1.getRun)(options?.run);
19
20
  const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
20
21
  observers: [
21
22
  ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
@@ -45,7 +46,12 @@ async function doStreamText(model, prompt, options) {
45
46
  ...startMetadata,
46
47
  });
47
48
  const result = await (0, runSafe_js_1.runSafe)(async () => {
48
- const deltaIterable = await model.doStreamText(prompt, options);
49
+ const deltaIterable = await model.doStreamText(prompt, {
50
+ functionId: options?.functionId,
51
+ logging: options?.logging,
52
+ observers: options?.observers,
53
+ run,
54
+ });
49
55
  return (async function* () {
50
56
  let accumulatedText = "";
51
57
  let lastFullDelta;
@@ -4,6 +4,7 @@ import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
4
4
  import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
5
5
  import { AbortError } from "../../core/api/AbortError.js";
6
6
  import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
7
+ import { getRun } from "../../core/getRun.js";
7
8
  import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
8
9
  import { runSafe } from "../../util/runSafe.js";
9
10
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
@@ -11,7 +12,7 @@ export function streamText(model, prompt, options) {
11
12
  return new AsyncIterableResultPromise(doStreamText(model, prompt, options));
12
13
  }
13
14
  async function doStreamText(model, prompt, options) {
14
- const run = options?.run;
15
+ const run = await getRun(options?.run);
15
16
  const eventSource = new FunctionEventSource({
16
17
  observers: [
17
18
  ...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
@@ -41,7 +42,12 @@ async function doStreamText(model, prompt, options) {
41
42
  ...startMetadata,
42
43
  });
43
44
  const result = await runSafe(async () => {
44
- const deltaIterable = await model.doStreamText(prompt, options);
45
+ const deltaIterable = await model.doStreamText(prompt, {
46
+ functionId: options?.functionId,
47
+ logging: options?.logging,
48
+ observers: options?.observers,
49
+ run,
50
+ });
45
51
  return (async function* () {
46
52
  let accumulatedText = "";
47
53
  let lastFullDelta;
@@ -34,6 +34,7 @@ __exportStar(require("./generate-structure/StructureFromTextGenerationModel.cjs"
34
34
  __exportStar(require("./generate-structure/StructureGenerationEvent.cjs"), exports);
35
35
  __exportStar(require("./generate-structure/StructureGenerationModel.cjs"), exports);
36
36
  __exportStar(require("./generate-structure/StructureOrTextGenerationModel.cjs"), exports);
37
+ __exportStar(require("./generate-structure/StructureParseError.cjs"), exports);
37
38
  __exportStar(require("./generate-structure/StructureStreamingEvent.cjs"), exports);
38
39
  __exportStar(require("./generate-structure/StructureValidationError.cjs"), exports);
39
40
  __exportStar(require("./generate-structure/generateStructure.cjs"), exports);
@@ -18,6 +18,7 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
18
18
  export * from "./generate-structure/StructureGenerationEvent.js";
19
19
  export * from "./generate-structure/StructureGenerationModel.js";
20
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureParseError.js";
21
22
  export * from "./generate-structure/StructureStreamingEvent.js";
22
23
  export * from "./generate-structure/StructureValidationError.js";
23
24
  export * from "./generate-structure/generateStructure.js";
@@ -18,6 +18,7 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
18
18
  export * from "./generate-structure/StructureGenerationEvent.js";
19
19
  export * from "./generate-structure/StructureGenerationModel.js";
20
20
  export * from "./generate-structure/StructureOrTextGenerationModel.js";
21
+ export * from "./generate-structure/StructureParseError.js";
21
22
  export * from "./generate-structure/StructureStreamingEvent.js";
22
23
  export * from "./generate-structure/StructureValidationError.js";
23
24
  export * from "./generate-structure/generateStructure.js";
@@ -9,6 +9,7 @@ const zod_1 = __importDefault(require("zod"));
9
9
  const callWithRetryAndThrottle_js_1 = require("../../../core/api/callWithRetryAndThrottle.cjs");
10
10
  const postToApi_js_1 = require("../../../core/api/postToApi.cjs");
11
11
  const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
12
+ const StructureParseError_js_1 = require("../../../model-function/generate-structure/StructureParseError.cjs");
12
13
  const parsePartialJson_js_1 = require("../../../model-function/generate-structure/parsePartialJson.cjs");
13
14
  const PromptFormatTextStreamingModel_js_1 = require("../../../prompt/PromptFormatTextStreamingModel.cjs");
14
15
  const OpenAIApiConfiguration_js_1 = require("../OpenAIApiConfiguration.cjs");
@@ -256,11 +257,22 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
256
257
  },
257
258
  ],
258
259
  });
259
- return {
260
- response,
261
- structure: secure_json_parse_1.default.parse(response.choices[0].message.function_call.arguments),
262
- usage: this.extractUsage(response),
263
- };
260
+ const valueText = response.choices[0].message.function_call.arguments;
261
+ try {
262
+ return {
263
+ response,
264
+ valueText,
265
+ value: secure_json_parse_1.default.parse(valueText),
266
+ usage: this.extractUsage(response),
267
+ };
268
+ }
269
+ catch (error) {
270
+ throw new StructureParseError_js_1.StructureParseError({
271
+ structureName: structureDefinition.name,
272
+ valueText: valueText,
273
+ cause: error,
274
+ });
275
+ }
264
276
  }
265
277
  async doStreamStructure(structureDefinition, prompt, options) {
266
278
  return this.callAPI(prompt, {
@@ -290,22 +302,37 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
290
302
  const message = response.choices[0].message;
291
303
  const content = message.content;
292
304
  const functionCall = message.function_call;
293
- const structureAndText = functionCall == null
294
- ? {
295
- structure: null,
296
- value: null,
297
- text: content ?? "",
298
- }
299
- : {
300
- structure: functionCall.name,
301
- value: secure_json_parse_1.default.parse(functionCall.arguments),
302
- text: content,
305
+ if (functionCall == null) {
306
+ return {
307
+ response,
308
+ structureAndText: {
309
+ structure: null,
310
+ value: null,
311
+ valueText: null,
312
+ text: content ?? "",
313
+ },
314
+ usage: this.extractUsage(response),
303
315
  };
304
- return {
305
- response,
306
- structureAndText,
307
- usage: this.extractUsage(response),
308
- };
316
+ }
317
+ try {
318
+ return {
319
+ response,
320
+ structureAndText: {
321
+ structure: functionCall.name,
322
+ value: secure_json_parse_1.default.parse(functionCall.arguments),
323
+ valueText: functionCall.arguments,
324
+ text: content,
325
+ },
326
+ usage: this.extractUsage(response),
327
+ };
328
+ }
329
+ catch (error) {
330
+ throw new StructureParseError_js_1.StructureParseError({
331
+ structureName: functionCall.name,
332
+ valueText: functionCall.arguments,
333
+ cause: error,
334
+ });
335
+ }
309
336
  }
310
337
  extractUsage(response) {
311
338
  return {
@@ -220,7 +220,8 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
220
220
  logprobs?: any;
221
221
  }[];
222
222
  };
223
- structure: any;
223
+ valueText: string;
224
+ value: any;
224
225
  usage: {
225
226
  promptTokens: number;
226
227
  completionTokens: number;
@@ -256,10 +257,43 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
256
257
  structureAndText: {
257
258
  structure: null;
258
259
  value: null;
260
+ valueText: null;
259
261
  text: string;
260
- } | {
262
+ };
263
+ usage: {
264
+ promptTokens: number;
265
+ completionTokens: number;
266
+ totalTokens: number;
267
+ };
268
+ } | {
269
+ response: {
270
+ object: "chat.completion";
271
+ model: string;
272
+ usage: {
273
+ prompt_tokens: number;
274
+ completion_tokens: number;
275
+ total_tokens: number;
276
+ };
277
+ id: string;
278
+ created: number;
279
+ choices: {
280
+ message: {
281
+ content: string | null;
282
+ role: "assistant";
283
+ function_call?: {
284
+ name: string;
285
+ arguments: string;
286
+ } | undefined;
287
+ };
288
+ finish_reason: string;
289
+ index: number;
290
+ logprobs?: any;
291
+ }[];
292
+ };
293
+ structureAndText: {
261
294
  structure: string;
262
295
  value: any;
296
+ valueText: string;
263
297
  text: string | null;
264
298
  };
265
299
  usage: {
@@ -3,6 +3,7 @@ import z from "zod";
3
3
  import { callWithRetryAndThrottle } from "../../../core/api/callWithRetryAndThrottle.js";
4
4
  import { createJsonResponseHandler, postJsonToApi, } from "../../../core/api/postToApi.js";
5
5
  import { AbstractModel } from "../../../model-function/AbstractModel.js";
6
+ import { StructureParseError } from "../../../model-function/generate-structure/StructureParseError.js";
6
7
  import { parsePartialJson } from "../../../model-function/generate-structure/parsePartialJson.js";
7
8
  import { PromptFormatTextStreamingModel } from "../../../prompt/PromptFormatTextStreamingModel.js";
8
9
  import { OpenAIApiConfiguration } from "../OpenAIApiConfiguration.js";
@@ -247,11 +248,22 @@ export class OpenAIChatModel extends AbstractModel {
247
248
  },
248
249
  ],
249
250
  });
250
- return {
251
- response,
252
- structure: SecureJSON.parse(response.choices[0].message.function_call.arguments),
253
- usage: this.extractUsage(response),
254
- };
251
+ const valueText = response.choices[0].message.function_call.arguments;
252
+ try {
253
+ return {
254
+ response,
255
+ valueText,
256
+ value: SecureJSON.parse(valueText),
257
+ usage: this.extractUsage(response),
258
+ };
259
+ }
260
+ catch (error) {
261
+ throw new StructureParseError({
262
+ structureName: structureDefinition.name,
263
+ valueText: valueText,
264
+ cause: error,
265
+ });
266
+ }
255
267
  }
256
268
  async doStreamStructure(structureDefinition, prompt, options) {
257
269
  return this.callAPI(prompt, {
@@ -281,22 +293,37 @@ export class OpenAIChatModel extends AbstractModel {
281
293
  const message = response.choices[0].message;
282
294
  const content = message.content;
283
295
  const functionCall = message.function_call;
284
- const structureAndText = functionCall == null
285
- ? {
286
- structure: null,
287
- value: null,
288
- text: content ?? "",
289
- }
290
- : {
291
- structure: functionCall.name,
292
- value: SecureJSON.parse(functionCall.arguments),
293
- text: content,
296
+ if (functionCall == null) {
297
+ return {
298
+ response,
299
+ structureAndText: {
300
+ structure: null,
301
+ value: null,
302
+ valueText: null,
303
+ text: content ?? "",
304
+ },
305
+ usage: this.extractUsage(response),
294
306
  };
295
- return {
296
- response,
297
- structureAndText,
298
- usage: this.extractUsage(response),
299
- };
307
+ }
308
+ try {
309
+ return {
310
+ response,
311
+ structureAndText: {
312
+ structure: functionCall.name,
313
+ value: SecureJSON.parse(functionCall.arguments),
314
+ valueText: functionCall.arguments,
315
+ text: content,
316
+ },
317
+ usage: this.extractUsage(response),
318
+ };
319
+ }
320
+ catch (error) {
321
+ throw new StructureParseError({
322
+ structureName: functionCall.name,
323
+ valueText: functionCall.arguments,
324
+ cause: error,
325
+ });
326
+ }
300
327
  }
301
328
  extractUsage(response) {
302
329
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.42.0",
4
+ "version": "0.44.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -7,10 +7,11 @@ const GlobalFunctionLogging_js_1 = require("../core/GlobalFunctionLogging.cjs");
7
7
  const GlobalFunctionObservers_js_1 = require("../core/GlobalFunctionObservers.cjs");
8
8
  const AbortError_js_1 = require("../core/api/AbortError.cjs");
9
9
  const getFunctionCallLogger_js_1 = require("../core/getFunctionCallLogger.cjs");
10
+ const getRun_js_1 = require("../core/getRun.cjs");
10
11
  const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
11
12
  const runSafe_js_1 = require("../util/runSafe.cjs");
12
13
  async function retrieve(retriever, query, options) {
13
- const run = options?.run;
14
+ const run = await (0, getRun_js_1.getRun)(options?.run);
14
15
  const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
15
16
  observers: [
16
17
  ...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
@@ -36,7 +37,12 @@ async function retrieve(retriever, query, options) {
36
37
  eventType: "started",
37
38
  ...startMetadata,
38
39
  });
39
- const result = await (0, runSafe_js_1.runSafe)(() => retriever.retrieve(query, options));
40
+ const result = await (0, runSafe_js_1.runSafe)(() => retriever.retrieve(query, {
41
+ functionId: options?.functionId,
42
+ logging: options?.logging,
43
+ observers: options?.observers,
44
+ run,
45
+ }));
40
46
  const finishMetadata = {
41
47
  eventType: "finished",
42
48
  ...startMetadata,