modelfusion 0.29.1 → 0.30.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.
Files changed (111) hide show
  1. package/README.md +27 -32
  2. package/core/index.cjs +1 -0
  3. package/core/index.d.ts +1 -0
  4. package/core/index.js +1 -0
  5. package/core/structure/Schema.d.ts +24 -0
  6. package/core/structure/StructureDefinition.d.ts +6 -0
  7. package/core/structure/UncheckedJsonSchemaSchema.cjs +26 -0
  8. package/core/structure/UncheckedJsonSchemaSchema.d.ts +14 -0
  9. package/core/structure/UncheckedJsonSchemaSchema.js +22 -0
  10. package/core/structure/UncheckedJsonSchemaStructureDefinition.cjs +30 -0
  11. package/core/structure/UncheckedJsonSchemaStructureDefinition.d.ts +12 -0
  12. package/core/structure/UncheckedJsonSchemaStructureDefinition.js +26 -0
  13. package/core/structure/ZodSchema.cjs +31 -0
  14. package/core/structure/ZodSchema.d.ts +15 -0
  15. package/core/structure/ZodSchema.js +27 -0
  16. package/core/structure/ZodStructureDefinition.cjs +30 -0
  17. package/core/structure/ZodStructureDefinition.d.ts +13 -0
  18. package/core/structure/ZodStructureDefinition.js +26 -0
  19. package/core/structure/index.cjs +22 -0
  20. package/core/structure/index.d.ts +6 -0
  21. package/core/structure/index.js +6 -0
  22. package/model-function/ModelCallEvent.d.ts +3 -3
  23. package/model-function/SuccessfulModelCall.d.ts +1 -1
  24. package/model-function/embed-text/embedText.cjs +4 -2
  25. package/model-function/embed-text/embedText.d.ts +1 -1
  26. package/model-function/embed-text/embedText.js +4 -2
  27. package/model-function/generate-structure/InstructionWithStructurePrompt.cjs +17 -0
  28. package/model-function/generate-structure/InstructionWithStructurePrompt.d.ts +17 -0
  29. package/model-function/generate-structure/InstructionWithStructurePrompt.js +14 -0
  30. package/model-function/generate-structure/NoSuchStructureError.cjs +17 -0
  31. package/model-function/generate-structure/NoSuchStructureError.d.ts +4 -0
  32. package/model-function/generate-structure/NoSuchStructureError.js +13 -0
  33. package/model-function/{generate-json/JsonTextGenerationModel.cjs → generate-structure/StructureFromTextGenerationModel.cjs} +7 -7
  34. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +26 -0
  35. package/model-function/{generate-json/JsonTextGenerationModel.js → generate-structure/StructureFromTextGenerationModel.js} +5 -5
  36. package/model-function/generate-structure/StructureGenerationEvent.d.ts +23 -0
  37. package/model-function/generate-structure/StructureGenerationModel.d.ts +13 -0
  38. package/model-function/generate-structure/StructureOrTextGenerationModel.cjs +2 -0
  39. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +23 -0
  40. package/model-function/generate-structure/StructureOrTextGenerationModel.js +1 -0
  41. package/model-function/generate-structure/StructureValidationError.cjs +34 -0
  42. package/model-function/generate-structure/StructureValidationError.d.ts +10 -0
  43. package/model-function/generate-structure/StructureValidationError.js +30 -0
  44. package/model-function/generate-structure/generateStructure.cjs +29 -0
  45. package/model-function/generate-structure/generateStructure.d.ts +5 -0
  46. package/model-function/generate-structure/generateStructure.js +25 -0
  47. package/model-function/generate-structure/generateStructureOrText.cjs +42 -0
  48. package/model-function/generate-structure/generateStructureOrText.d.ts +22 -0
  49. package/model-function/generate-structure/generateStructureOrText.js +38 -0
  50. package/model-function/index.cjs +9 -10
  51. package/model-function/index.d.ts +9 -10
  52. package/model-function/index.js +9 -10
  53. package/model-provider/openai/OpenAICostCalculator.cjs +1 -1
  54. package/model-provider/openai/OpenAICostCalculator.js +1 -1
  55. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +1 -1
  56. package/model-provider/openai/OpenAITextEmbeddingModel.js +1 -1
  57. package/model-provider/openai/chat/OpenAIChatModel.cjs +2 -2
  58. package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -5
  59. package/model-provider/openai/chat/OpenAIChatModel.js +2 -2
  60. package/model-provider/openai/chat/OpenAIChatPrompt.cjs +22 -26
  61. package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +20 -30
  62. package/model-provider/openai/chat/OpenAIChatPrompt.js +22 -26
  63. package/model-provider/openai/index.d.ts +1 -1
  64. package/model-provider/openai/index.js +1 -1
  65. package/package.json +9 -12
  66. package/tool/Tool.cjs +2 -2
  67. package/tool/Tool.d.ts +8 -8
  68. package/tool/Tool.js +2 -2
  69. package/tool/WebSearchTool.cjs +5 -4
  70. package/tool/WebSearchTool.d.ts +2 -22
  71. package/tool/WebSearchTool.js +5 -4
  72. package/tool/executeTool.cjs +1 -1
  73. package/tool/executeTool.d.ts +1 -2
  74. package/tool/executeTool.js +1 -1
  75. package/tool/useTool.cjs +3 -3
  76. package/tool/useTool.d.ts +3 -3
  77. package/tool/useTool.js +3 -3
  78. package/tool/useToolOrGenerateText.cjs +7 -7
  79. package/tool/useToolOrGenerateText.d.ts +2 -2
  80. package/tool/useToolOrGenerateText.js +7 -7
  81. package/util/getErrorMessage.cjs +13 -0
  82. package/util/getErrorMessage.d.ts +1 -0
  83. package/util/getErrorMessage.js +9 -0
  84. package/model-function/generate-json/InstructionWithSchemaPrompt.cjs +0 -17
  85. package/model-function/generate-json/InstructionWithSchemaPrompt.d.ts +0 -26
  86. package/model-function/generate-json/InstructionWithSchemaPrompt.js +0 -14
  87. package/model-function/generate-json/JsonGenerationEvent.d.ts +0 -23
  88. package/model-function/generate-json/JsonGenerationModel.d.ts +0 -13
  89. package/model-function/generate-json/JsonOrTextGenerationModel.d.ts +0 -23
  90. package/model-function/generate-json/JsonTextGenerationModel.d.ts +0 -26
  91. package/model-function/generate-json/NoSuchSchemaError.cjs +0 -17
  92. package/model-function/generate-json/NoSuchSchemaError.d.ts +0 -4
  93. package/model-function/generate-json/NoSuchSchemaError.js +0 -13
  94. package/model-function/generate-json/SchemaDefinition.d.ts +0 -6
  95. package/model-function/generate-json/SchemaValidationError.cjs +0 -36
  96. package/model-function/generate-json/SchemaValidationError.d.ts +0 -11
  97. package/model-function/generate-json/SchemaValidationError.js +0 -32
  98. package/model-function/generate-json/generateJson.cjs +0 -29
  99. package/model-function/generate-json/generateJson.d.ts +0 -5
  100. package/model-function/generate-json/generateJson.js +0 -25
  101. package/model-function/generate-json/generateJsonOrText.cjs +0 -42
  102. package/model-function/generate-json/generateJsonOrText.d.ts +0 -22
  103. package/model-function/generate-json/generateJsonOrText.js +0 -38
  104. /package/{model-function/generate-json/JsonGenerationEvent.cjs → core/structure/Schema.cjs} +0 -0
  105. /package/{model-function/generate-json/JsonGenerationEvent.js → core/structure/Schema.js} +0 -0
  106. /package/{model-function/generate-json/JsonGenerationModel.cjs → core/structure/StructureDefinition.cjs} +0 -0
  107. /package/{model-function/generate-json/JsonGenerationModel.js → core/structure/StructureDefinition.js} +0 -0
  108. /package/model-function/{generate-json/JsonOrTextGenerationModel.cjs → generate-structure/StructureGenerationEvent.cjs} +0 -0
  109. /package/model-function/{generate-json/JsonOrTextGenerationModel.js → generate-structure/StructureGenerationEvent.js} +0 -0
  110. /package/model-function/{generate-json/SchemaDefinition.cjs → generate-structure/StructureGenerationModel.cjs} +0 -0
  111. /package/model-function/{generate-json/SchemaDefinition.js → generate-structure/StructureGenerationModel.js} +0 -0
package/tool/useTool.js CHANGED
@@ -1,17 +1,17 @@
1
- import { generateJson } from "../model-function/generate-json/generateJson.js";
1
+ import { generateStructure } from "../model-function/generate-structure/generateStructure.js";
2
2
  import { executeTool } from "./executeTool.js";
3
3
  // In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
4
4
  // retrieved through lookups such as TOOL["name"], such that any does not affect any client.
5
5
  /* eslint-disable @typescript-eslint/no-explicit-any */
6
6
  /**
7
- * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
7
+ * `useTool` uses `generateStructure` to generate parameters for a tool and then executes the tool with the parameters.
8
8
  *
9
9
  * @returns The result contains the name of the tool (`tool` property),
10
10
  * the parameters (`parameters` property, typed),
11
11
  * and the result of the tool execution (`result` property, typed).
12
12
  */
13
13
  export async function useTool(model, tool, prompt, options) {
14
- const { output: value } = await generateJson(model, {
14
+ const { output: value } = await generateStructure(model, {
15
15
  name: tool.name,
16
16
  description: tool.description,
17
17
  schema: tool.inputSchema,
@@ -1,18 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useToolOrGenerateText = void 0;
4
- const generateJsonOrText_js_1 = require("../model-function/generate-json/generateJsonOrText.cjs");
4
+ const generateStructureOrText_js_1 = require("../model-function/generate-structure/generateStructureOrText.cjs");
5
5
  const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
6
6
  const executeTool_js_1 = require("./executeTool.cjs");
7
7
  async function useToolOrGenerateText(model, tools, prompt, options) {
8
8
  const expandedPrompt = prompt(tools);
9
- const modelResponse = await (0, generateJsonOrText_js_1.generateJsonOrText)(model, tools.map((tool) => ({
9
+ const modelResponse = await (0, generateStructureOrText_js_1.generateStructureOrText)(model, tools.map((tool) => ({
10
10
  name: tool.name,
11
11
  description: tool.description,
12
12
  schema: tool.inputSchema,
13
13
  })), () => expandedPrompt, options);
14
- const { schema, text } = modelResponse;
15
- if (schema == null) {
14
+ const { structure, text } = modelResponse;
15
+ if (structure == null) {
16
16
  return {
17
17
  tool: null,
18
18
  parameters: null,
@@ -20,14 +20,14 @@ async function useToolOrGenerateText(model, tools, prompt, options) {
20
20
  text,
21
21
  };
22
22
  }
23
- const tool = tools.find((tool) => tool.name === schema);
23
+ const tool = tools.find((tool) => tool.name === structure);
24
24
  if (tool == null) {
25
- throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
25
+ throw new NoSuchToolError_js_1.NoSuchToolError(structure.toString());
26
26
  }
27
27
  const toolParameters = modelResponse.value;
28
28
  const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, options);
29
29
  return {
30
- tool: schema,
30
+ tool: structure,
31
31
  result,
32
32
  parameters: toolParameters,
33
33
  text: text, // string | null is the expected value here
@@ -1,5 +1,5 @@
1
1
  import { ModelFunctionOptions } from "../model-function/ModelFunctionOptions.js";
2
- import { JsonOrTextGenerationModel, JsonOrTextGenerationModelSettings, JsonOrTextGenerationPrompt } from "../model-function/generate-json/JsonOrTextGenerationModel.js";
2
+ import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings, StructureOrTextGenerationPrompt } from "../model-function/generate-structure/StructureOrTextGenerationModel.js";
3
3
  import { Tool } from "./Tool.js";
4
4
  type ToolArray<T extends Tool<any, any, any>[]> = T;
5
5
  type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
@@ -14,7 +14,7 @@ type ToToolUnion<T> = {
14
14
  } : never;
15
15
  }[keyof T];
16
16
  type ToOutputValue<TOOLS extends ToolArray<Tool<any, any, any>[]>> = ToToolUnion<ToToolMap<TOOLS>>;
17
- export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends JsonOrTextGenerationModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: JsonOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: (tools: TOOLS) => PROMPT & JsonOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): Promise<{
17
+ export declare function useToolOrGenerateText<PROMPT, RESPONSE, SETTINGS extends StructureOrTextGenerationModelSettings, TOOLS extends Array<Tool<any, any, any>>>(model: StructureOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, tools: TOOLS, prompt: (tools: TOOLS) => PROMPT & StructureOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): Promise<{
18
18
  tool: null;
19
19
  parameters: null;
20
20
  result: null;
@@ -1,15 +1,15 @@
1
- import { generateJsonOrText } from "../model-function/generate-json/generateJsonOrText.js";
1
+ import { generateStructureOrText } from "../model-function/generate-structure/generateStructureOrText.js";
2
2
  import { NoSuchToolError } from "./NoSuchToolError.js";
3
3
  import { executeTool } from "./executeTool.js";
4
4
  export async function useToolOrGenerateText(model, tools, prompt, options) {
5
5
  const expandedPrompt = prompt(tools);
6
- const modelResponse = await generateJsonOrText(model, tools.map((tool) => ({
6
+ const modelResponse = await generateStructureOrText(model, tools.map((tool) => ({
7
7
  name: tool.name,
8
8
  description: tool.description,
9
9
  schema: tool.inputSchema,
10
10
  })), () => expandedPrompt, options);
11
- const { schema, text } = modelResponse;
12
- if (schema == null) {
11
+ const { structure, text } = modelResponse;
12
+ if (structure == null) {
13
13
  return {
14
14
  tool: null,
15
15
  parameters: null,
@@ -17,14 +17,14 @@ export async function useToolOrGenerateText(model, tools, prompt, options) {
17
17
  text,
18
18
  };
19
19
  }
20
- const tool = tools.find((tool) => tool.name === schema);
20
+ const tool = tools.find((tool) => tool.name === structure);
21
21
  if (tool == null) {
22
- throw new NoSuchToolError(schema.toString());
22
+ throw new NoSuchToolError(structure.toString());
23
23
  }
24
24
  const toolParameters = modelResponse.value;
25
25
  const result = await executeTool(tool, toolParameters, options);
26
26
  return {
27
- tool: schema,
27
+ tool: structure,
28
28
  result,
29
29
  parameters: toolParameters,
30
30
  text: text, // string | null is the expected value here
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getErrorMessage = void 0;
4
+ function getErrorMessage(error) {
5
+ if (typeof error === "string") {
6
+ return error;
7
+ }
8
+ if (error instanceof Error) {
9
+ return error.message;
10
+ }
11
+ return JSON.stringify(error);
12
+ }
13
+ exports.getErrorMessage = getErrorMessage;
@@ -0,0 +1 @@
1
+ export declare function getErrorMessage(error: unknown): string;
@@ -0,0 +1,9 @@
1
+ export function getErrorMessage(error) {
2
+ if (typeof error === "string") {
3
+ return error;
4
+ }
5
+ if (error instanceof Error) {
6
+ return error.message;
7
+ }
8
+ return JSON.stringify(error);
9
+ }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InstructionWithSchemaPrompt = void 0;
4
- exports.InstructionWithSchemaPrompt = {
5
- forSchema({ instruction, schemaDefinition, }) {
6
- return { schemaDefinition, instruction };
7
- },
8
- forTool({ instruction, tool, }) {
9
- return exports.InstructionWithSchemaPrompt.forSchema({
10
- instruction,
11
- schemaDefinition: tool.inputSchemaDefinition,
12
- });
13
- },
14
- forToolCurried(instruction) {
15
- return (tool) => this.forTool({ instruction, tool });
16
- },
17
- };
@@ -1,26 +0,0 @@
1
- import { Tool } from "../../tool/Tool.js";
2
- import { SchemaDefinition } from "./SchemaDefinition.js";
3
- export type InstructionWithSchema<NAME extends string, STRUCTURE> = {
4
- instruction: string;
5
- schemaDefinition: SchemaDefinition<NAME, STRUCTURE>;
6
- };
7
- export declare const InstructionWithSchemaPrompt: {
8
- forSchema<STRUCTURE>({ instruction, schemaDefinition, }: {
9
- instruction: string;
10
- schemaDefinition: SchemaDefinition<string, STRUCTURE>;
11
- }): {
12
- schemaDefinition: SchemaDefinition<string, STRUCTURE>;
13
- instruction: string;
14
- };
15
- forTool<INPUT, OUTPUT>({ instruction, tool, }: {
16
- instruction: string;
17
- tool: Tool<string, INPUT, OUTPUT>;
18
- }): {
19
- schemaDefinition: SchemaDefinition<string, INPUT>;
20
- instruction: string;
21
- };
22
- forToolCurried<INPUT_1, OUTPUT_1>(instruction: string): (tool: Tool<string, INPUT_1, OUTPUT_1>) => {
23
- schemaDefinition: SchemaDefinition<string, INPUT_1>;
24
- instruction: string;
25
- };
26
- };
@@ -1,14 +0,0 @@
1
- export const InstructionWithSchemaPrompt = {
2
- forSchema({ instruction, schemaDefinition, }) {
3
- return { schemaDefinition, instruction };
4
- },
5
- forTool({ instruction, tool, }) {
6
- return InstructionWithSchemaPrompt.forSchema({
7
- instruction,
8
- schemaDefinition: tool.inputSchemaDefinition,
9
- });
10
- },
11
- forToolCurried(instruction) {
12
- return (tool) => this.forTool({ instruction, tool });
13
- },
14
- };
@@ -1,23 +0,0 @@
1
- import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
2
- export interface JsonGenerationStartedEvent extends BaseModelCallStartedEvent {
3
- functionType: "json-generation" | "json-or-text-generation";
4
- }
5
- export type JsonGenerationFinishedEventResult = {
6
- status: "success";
7
- response: unknown;
8
- output: unknown;
9
- usage?: {
10
- promptTokens: number;
11
- completionTokens: number;
12
- totalTokens: number;
13
- };
14
- } | {
15
- status: "error";
16
- error: unknown;
17
- } | {
18
- status: "abort";
19
- };
20
- export type JsonGenerationFinishedEvent = BaseModelCallFinishedEvent & {
21
- functionType: "json-generation" | "json-or-text-generation";
22
- result: JsonGenerationFinishedEventResult;
23
- };
@@ -1,13 +0,0 @@
1
- import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
2
- import { Model, ModelSettings } from "../Model.js";
3
- export interface JsonGenerationModelSettings extends ModelSettings {
4
- }
5
- export interface JsonGenerationModel<PROMPT, RESPONSE, SETTINGS extends JsonGenerationModelSettings> extends Model<SETTINGS> {
6
- generateJsonResponse(prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
7
- extractJson(response: RESPONSE): unknown;
8
- extractUsage?(response: RESPONSE): {
9
- promptTokens: number;
10
- completionTokens: number;
11
- totalTokens: number;
12
- };
13
- }
@@ -1,23 +0,0 @@
1
- import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
2
- import { Model, ModelSettings } from "../Model.js";
3
- export interface JsonOrTextGenerationModelSettings extends ModelSettings {
4
- }
5
- export interface JsonOrTextGenerationPrompt<RESPONSE> {
6
- extractJsonAndText(response: RESPONSE): {
7
- schema: null;
8
- value: null;
9
- text: string;
10
- } | {
11
- schema: string;
12
- value: unknown;
13
- text: string | null;
14
- };
15
- }
16
- export interface JsonOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS extends JsonOrTextGenerationModelSettings> extends Model<SETTINGS> {
17
- generateJsonResponse(prompt: PROMPT & JsonOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
18
- extractUsage?(response: RESPONSE): {
19
- promptTokens: number;
20
- completionTokens: number;
21
- totalTokens: number;
22
- };
23
- }
@@ -1,26 +0,0 @@
1
- import { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
2
- import { SchemaDefinition } from "./SchemaDefinition.js";
3
- import { InstructionWithSchema } from "./InstructionWithSchemaPrompt.js";
4
- import { JsonGenerationModel } from "./JsonGenerationModel.js";
5
- import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
6
- export type JsonTextPromptFormat = {
7
- createPrompt: (prompt: {
8
- instruction: string;
9
- schemaDefinition: SchemaDefinition<string, unknown>;
10
- }) => string;
11
- extractJson: (response: string) => unknown;
12
- };
13
- export declare class JsonTextGenerationModel<MODEL extends TextGenerationModel<string, any, any, TextGenerationModelSettings>> implements JsonGenerationModel<InstructionWithSchema<string, unknown>, string, MODEL["settings"]> {
14
- private readonly model;
15
- private readonly format;
16
- constructor({ model, format, }: {
17
- model: MODEL;
18
- format: JsonTextPromptFormat;
19
- });
20
- get modelInformation(): import("../ModelInformation.js").ModelInformation;
21
- get settings(): TextGenerationModelSettings;
22
- get settingsForEvent(): Partial<MODEL["settings"]>;
23
- generateJsonResponse(prompt: InstructionWithSchema<string, unknown>, options?: ModelFunctionOptions<MODEL["settings"]> | undefined): Promise<string>;
24
- extractJson(response: string): unknown;
25
- withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
26
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NoSuchSchemaError = void 0;
4
- class NoSuchSchemaError extends Error {
5
- constructor(schemaName) {
6
- super(`No such schema: ${schemaName}`);
7
- Object.defineProperty(this, "schemaName", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
- this.name = "NoSuchSchemaError";
14
- this.schemaName = schemaName;
15
- }
16
- }
17
- exports.NoSuchSchemaError = NoSuchSchemaError;
@@ -1,4 +0,0 @@
1
- export declare class NoSuchSchemaError extends Error {
2
- readonly schemaName: string;
3
- constructor(schemaName: string);
4
- }
@@ -1,13 +0,0 @@
1
- export class NoSuchSchemaError extends Error {
2
- constructor(schemaName) {
3
- super(`No such schema: ${schemaName}`);
4
- Object.defineProperty(this, "schemaName", {
5
- enumerable: true,
6
- configurable: true,
7
- writable: true,
8
- value: void 0
9
- });
10
- this.name = "NoSuchSchemaError";
11
- this.schemaName = schemaName;
12
- }
13
- }
@@ -1,6 +0,0 @@
1
- import { z } from "zod";
2
- export type SchemaDefinition<NAME extends string, STRUCTURE> = {
3
- name: NAME;
4
- description?: string;
5
- schema: z.Schema<STRUCTURE>;
6
- };
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaValidationError = void 0;
4
- class SchemaValidationError extends Error {
5
- constructor({ schemaName, value, errors, }) {
6
- super(`Schema validation error for '${schemaName}'. ` +
7
- `Value: ${JSON.stringify(value)}.\n` +
8
- `Error details: ${errors.message}\n` +
9
- `Error field(s): ${errors.errors
10
- .map((err) => err.path.join("."))
11
- .join(", ")}`);
12
- Object.defineProperty(this, "schemaName", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: void 0
17
- });
18
- Object.defineProperty(this, "errors", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: void 0
23
- });
24
- Object.defineProperty(this, "value", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
- this.name = "SchemaValidationError";
31
- this.schemaName = schemaName;
32
- this.errors = errors;
33
- this.value = value;
34
- }
35
- }
36
- exports.SchemaValidationError = SchemaValidationError;
@@ -1,11 +0,0 @@
1
- import { z } from "zod";
2
- export declare class SchemaValidationError extends Error {
3
- readonly schemaName: string;
4
- readonly errors: z.ZodError;
5
- readonly value: unknown;
6
- constructor({ schemaName, value, errors, }: {
7
- schemaName: string;
8
- value: unknown;
9
- errors: z.ZodError;
10
- });
11
- }
@@ -1,32 +0,0 @@
1
- export class SchemaValidationError extends Error {
2
- constructor({ schemaName, value, errors, }) {
3
- super(`Schema validation error for '${schemaName}'. ` +
4
- `Value: ${JSON.stringify(value)}.\n` +
5
- `Error details: ${errors.message}\n` +
6
- `Error field(s): ${errors.errors
7
- .map((err) => err.path.join("."))
8
- .join(", ")}`);
9
- Object.defineProperty(this, "schemaName", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- Object.defineProperty(this, "errors", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "value", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- this.name = "SchemaValidationError";
28
- this.schemaName = schemaName;
29
- this.errors = errors;
30
- this.value = value;
31
- }
32
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateJson = void 0;
4
- const executeCall_js_1 = require("../executeCall.cjs");
5
- const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
6
- function generateJson(model, schemaDefinition, prompt, options) {
7
- const expandedPrompt = prompt(schemaDefinition);
8
- return (0, executeCall_js_1.executeCall)({
9
- functionType: "json-generation",
10
- input: expandedPrompt,
11
- model,
12
- options,
13
- generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
14
- extractOutputValue: (response) => {
15
- const json = model.extractJson(response);
16
- const parseResult = schemaDefinition.schema.safeParse(json);
17
- if (!parseResult.success) {
18
- throw new SchemaValidationError_js_1.SchemaValidationError({
19
- schemaName: schemaDefinition.name,
20
- value: json,
21
- errors: parseResult.error,
22
- });
23
- }
24
- return parseResult.data;
25
- },
26
- extractUsage: (result) => model.extractUsage?.(result),
27
- });
28
- }
29
- exports.generateJson = generateJson;
@@ -1,5 +0,0 @@
1
- import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
2
- import { ModelFunctionPromise } from "../executeCall.js";
3
- import { JsonGenerationModel, JsonGenerationModelSettings } from "./JsonGenerationModel.js";
4
- import { SchemaDefinition } from "./SchemaDefinition.js";
5
- export declare function generateJson<STRUCTURE, PROMPT, RESPONSE, NAME extends string, SETTINGS extends JsonGenerationModelSettings>(model: JsonGenerationModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinition: SchemaDefinition<NAME, STRUCTURE>, prompt: (schemaDefinition: SchemaDefinition<NAME, STRUCTURE>) => PROMPT, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<STRUCTURE, RESPONSE>;
@@ -1,25 +0,0 @@
1
- import { executeCall } from "../executeCall.js";
2
- import { SchemaValidationError } from "./SchemaValidationError.js";
3
- export function generateJson(model, schemaDefinition, prompt, options) {
4
- const expandedPrompt = prompt(schemaDefinition);
5
- return executeCall({
6
- functionType: "json-generation",
7
- input: expandedPrompt,
8
- model,
9
- options,
10
- generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
11
- extractOutputValue: (response) => {
12
- const json = model.extractJson(response);
13
- const parseResult = schemaDefinition.schema.safeParse(json);
14
- if (!parseResult.success) {
15
- throw new SchemaValidationError({
16
- schemaName: schemaDefinition.name,
17
- value: json,
18
- errors: parseResult.error,
19
- });
20
- }
21
- return parseResult.data;
22
- },
23
- extractUsage: (result) => model.extractUsage?.(result),
24
- });
25
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateJsonOrText = void 0;
4
- const executeCall_js_1 = require("../executeCall.cjs");
5
- const NoSuchSchemaError_js_1 = require("./NoSuchSchemaError.cjs");
6
- const SchemaValidationError_js_1 = require("./SchemaValidationError.cjs");
7
- function generateJsonOrText(model, schemaDefinitions, prompt, options) {
8
- const expandedPrompt = prompt(schemaDefinitions);
9
- return (0, executeCall_js_1.executeCall)({
10
- functionType: "json-or-text-generation",
11
- input: expandedPrompt,
12
- model,
13
- options,
14
- generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
15
- extractOutputValue: (response) => {
16
- const { schema, value, text } = expandedPrompt.extractJsonAndText(response);
17
- // text generation:
18
- if (schema == null) {
19
- return { schema, value, text };
20
- }
21
- const definition = schemaDefinitions.find((d) => d.name === schema);
22
- if (definition == undefined) {
23
- throw new NoSuchSchemaError_js_1.NoSuchSchemaError(schema);
24
- }
25
- const parseResult = definition.schema.safeParse(value);
26
- if (!parseResult.success) {
27
- throw new SchemaValidationError_js_1.SchemaValidationError({
28
- schemaName: schema,
29
- value,
30
- errors: parseResult.error,
31
- });
32
- }
33
- return {
34
- schema: schema,
35
- value: parseResult.data,
36
- text: text, // text is string | null, which is part of the response for schema values
37
- };
38
- },
39
- extractUsage: (result) => model.extractUsage?.(result),
40
- });
41
- }
42
- exports.generateJsonOrText = generateJsonOrText;
@@ -1,22 +0,0 @@
1
- import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
2
- import { ModelFunctionPromise } from "../executeCall.js";
3
- import { JsonOrTextGenerationModel, JsonOrTextGenerationModelSettings, JsonOrTextGenerationPrompt } from "./JsonOrTextGenerationModel.js";
4
- import { SchemaDefinition } from "./SchemaDefinition.js";
5
- type SchemaDefinitionArray<T extends SchemaDefinition<any, any>[]> = T;
6
- type ToSchemaDefinitionsMap<T extends SchemaDefinitionArray<SchemaDefinition<any, any>[]>> = {
7
- [K in T[number]["name"]]: Extract<T[number], SchemaDefinition<K, any>>;
8
- };
9
- type ToSchemaUnion<T> = {
10
- [KEY in keyof T]: T[KEY] extends SchemaDefinition<any, infer SCHEMA> ? {
11
- schema: KEY;
12
- value: SCHEMA;
13
- text: string | null;
14
- } : never;
15
- }[keyof T];
16
- type ToOutputValue<SCHEMAS extends SchemaDefinitionArray<SchemaDefinition<any, any>[]>> = ToSchemaUnion<ToSchemaDefinitionsMap<SCHEMAS>>;
17
- export declare function generateJsonOrText<SCHEMAS extends SchemaDefinition<any, any>[], PROMPT, RESPONSE, SETTINGS extends JsonOrTextGenerationModelSettings>(model: JsonOrTextGenerationModel<PROMPT, RESPONSE, SETTINGS>, schemaDefinitions: SCHEMAS, prompt: (schemaDefinitions: SCHEMAS) => PROMPT & JsonOrTextGenerationPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<{
18
- schema: null;
19
- value: null;
20
- text: string;
21
- } | ToOutputValue<SCHEMAS>, RESPONSE>;
22
- export {};
@@ -1,38 +0,0 @@
1
- import { executeCall } from "../executeCall.js";
2
- import { NoSuchSchemaError } from "./NoSuchSchemaError.js";
3
- import { SchemaValidationError } from "./SchemaValidationError.js";
4
- export function generateJsonOrText(model, schemaDefinitions, prompt, options) {
5
- const expandedPrompt = prompt(schemaDefinitions);
6
- return executeCall({
7
- functionType: "json-or-text-generation",
8
- input: expandedPrompt,
9
- model,
10
- options,
11
- generateResponse: (options) => model.generateJsonResponse(expandedPrompt, options),
12
- extractOutputValue: (response) => {
13
- const { schema, value, text } = expandedPrompt.extractJsonAndText(response);
14
- // text generation:
15
- if (schema == null) {
16
- return { schema, value, text };
17
- }
18
- const definition = schemaDefinitions.find((d) => d.name === schema);
19
- if (definition == undefined) {
20
- throw new NoSuchSchemaError(schema);
21
- }
22
- const parseResult = definition.schema.safeParse(value);
23
- if (!parseResult.success) {
24
- throw new SchemaValidationError({
25
- schemaName: schema,
26
- value,
27
- errors: parseResult.error,
28
- });
29
- }
30
- return {
31
- schema: schema,
32
- value: parseResult.data,
33
- text: text, // text is string | null, which is part of the response for schema values
34
- };
35
- },
36
- extractUsage: (result) => model.extractUsage?.(result),
37
- });
38
- }