modelfusion 0.7.0 → 0.8.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 (73) hide show
  1. package/README.md +14 -4
  2. package/composed-function/index.cjs +0 -3
  3. package/composed-function/index.d.ts +0 -3
  4. package/composed-function/index.js +0 -3
  5. package/index.cjs +1 -0
  6. package/index.d.ts +1 -0
  7. package/index.js +1 -0
  8. package/model-function/Model.d.ts +2 -2
  9. package/model-function/ModelCallEvent.d.ts +4 -6
  10. package/model-function/SuccessfulModelCall.cjs +6 -3
  11. package/model-function/SuccessfulModelCall.d.ts +3 -3
  12. package/model-function/SuccessfulModelCall.js +6 -3
  13. package/model-function/executeCall.cjs +6 -6
  14. package/model-function/executeCall.js +6 -6
  15. package/model-function/generate-json/JsonGenerationEvent.d.ts +2 -2
  16. package/model-function/generate-json/generateJsonOrText.cjs +4 -4
  17. package/model-function/generate-json/generateJsonOrText.js +4 -4
  18. package/model-function/generate-text/streamText.cjs +7 -7
  19. package/model-function/generate-text/streamText.js +7 -7
  20. package/model-function/index.cjs +1 -1
  21. package/model-function/index.d.ts +1 -1
  22. package/model-function/index.js +1 -1
  23. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
  24. package/model-provider/openai/chat/OpenAIChatPrompt.d.ts +1 -1
  25. package/package.json +1 -1
  26. package/run/ConsoleLogger.cjs +2 -2
  27. package/run/ConsoleLogger.d.ts +5 -5
  28. package/run/ConsoleLogger.js +2 -2
  29. package/run/DefaultRun.cjs +7 -7
  30. package/run/DefaultRun.d.ts +6 -6
  31. package/run/DefaultRun.js +7 -7
  32. package/run/Run.d.ts +2 -2
  33. package/run/RunFunction.d.ts +0 -4
  34. package/run/RunFunctionEvent.d.ts +12 -0
  35. package/{model-function/ModelCallEventSource.cjs → run/RunFunctionEventSource.cjs} +7 -7
  36. package/run/RunFunctionEventSource.d.ts +13 -0
  37. package/{model-function/ModelCallEventSource.js → run/RunFunctionEventSource.js} +5 -5
  38. package/run/RunFunctionObserver.cjs +2 -0
  39. package/run/RunFunctionObserver.d.ts +5 -0
  40. package/run/RunFunctionObserver.js +1 -0
  41. package/run/index.cjs +3 -0
  42. package/run/index.d.ts +3 -0
  43. package/run/index.js +3 -0
  44. package/tool/ExecuteToolEvent.cjs +2 -0
  45. package/tool/ExecuteToolEvent.d.ts +22 -0
  46. package/tool/ExecuteToolEvent.js +1 -0
  47. package/{composed-function/use-tool → tool}/Tool.cjs +7 -0
  48. package/{composed-function/use-tool → tool}/Tool.d.ts +5 -2
  49. package/{composed-function/use-tool → tool}/Tool.js +7 -0
  50. package/tool/ToolExecutionError.cjs +31 -0
  51. package/tool/ToolExecutionError.d.ts +11 -0
  52. package/tool/ToolExecutionError.js +27 -0
  53. package/tool/executeTool.cjs +79 -0
  54. package/tool/executeTool.d.ts +20 -0
  55. package/tool/executeTool.js +75 -0
  56. package/tool/index.cjs +22 -0
  57. package/tool/index.d.ts +6 -0
  58. package/tool/index.js +6 -0
  59. package/tool/useTool.cjs +33 -0
  60. package/tool/useTool.d.ts +15 -0
  61. package/tool/useTool.js +29 -0
  62. package/tool/useToolOrGenerateText.cjs +38 -0
  63. package/{composed-function/use-tool/useTool.d.ts → tool/useToolOrGenerateText.d.ts} +2 -15
  64. package/tool/useToolOrGenerateText.js +34 -0
  65. package/composed-function/use-tool/useTool.cjs +0 -62
  66. package/composed-function/use-tool/useTool.js +0 -57
  67. package/model-function/ModelCallEventSource.d.ts +0 -13
  68. package/model-function/ModelCallObserver.d.ts +0 -5
  69. /package/{model-function/ModelCallObserver.cjs → run/RunFunctionEvent.cjs} +0 -0
  70. /package/{model-function/ModelCallObserver.js → run/RunFunctionEvent.js} +0 -0
  71. /package/{composed-function/use-tool → tool}/NoSuchToolError.cjs +0 -0
  72. /package/{composed-function/use-tool → tool}/NoSuchToolError.d.ts +0 -0
  73. /package/{composed-function/use-tool → tool}/NoSuchToolError.js +0 -0
package/tool/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./NoSuchToolError.js";
2
+ export * from "./Tool.js";
3
+ export * from "./ToolExecutionError.js";
4
+ export * from "./executeTool.js";
5
+ export * from "./useTool.js";
6
+ export * from "./useToolOrGenerateText.js";
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useTool = void 0;
4
+ const generateJson_js_1 = require("../model-function/generate-json/generateJson.cjs");
5
+ const executeTool_js_1 = require("./executeTool.cjs");
6
+ // In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
7
+ // retrieved through lookups such as TOOL["name"], such that any does not affect any client.
8
+ /* eslint-disable @typescript-eslint/no-explicit-any */
9
+ /**
10
+ * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
11
+ *
12
+ * @returns The result contains the name of the tool (`tool` property),
13
+ * the parameters (`parameters` property, typed),
14
+ * and the result of the tool execution (`result` property, typed).
15
+ */
16
+ async function useTool(model, tool, prompt, options) {
17
+ const { value } = await (0, generateJson_js_1.generateJson)(model, {
18
+ name: tool.name,
19
+ description: tool.description,
20
+ schema: tool.inputSchema,
21
+ }, () => prompt(tool), {
22
+ ...(options ?? {}),
23
+ fullResponse: true,
24
+ });
25
+ return {
26
+ tool: tool.name,
27
+ parameters: value,
28
+ result: await (0, executeTool_js_1.executeTool)(tool, value, {
29
+ run: options?.run,
30
+ }),
31
+ };
32
+ }
33
+ exports.useTool = useTool;
@@ -0,0 +1,15 @@
1
+ import { FunctionOptions } from "../model-function/FunctionOptions.js";
2
+ import { GenerateJsonModel, GenerateJsonModelSettings, GenerateJsonPrompt } from "../model-function/generate-json/GenerateJsonModel.js";
3
+ import { Tool } from "./Tool.js";
4
+ /**
5
+ * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
6
+ *
7
+ * @returns The result contains the name of the tool (`tool` property),
8
+ * the parameters (`parameters` property, typed),
9
+ * and the result of the tool execution (`result` property, typed).
10
+ */
11
+ export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
12
+ tool: TOOL["name"];
13
+ parameters: TOOL["inputSchema"];
14
+ result: Awaited<ReturnType<TOOL["execute"]>>;
15
+ }>;
@@ -0,0 +1,29 @@
1
+ import { generateJson } from "../model-function/generate-json/generateJson.js";
2
+ import { executeTool } from "./executeTool.js";
3
+ // In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
4
+ // retrieved through lookups such as TOOL["name"], such that any does not affect any client.
5
+ /* eslint-disable @typescript-eslint/no-explicit-any */
6
+ /**
7
+ * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
8
+ *
9
+ * @returns The result contains the name of the tool (`tool` property),
10
+ * the parameters (`parameters` property, typed),
11
+ * and the result of the tool execution (`result` property, typed).
12
+ */
13
+ export async function useTool(model, tool, prompt, options) {
14
+ const { value } = await generateJson(model, {
15
+ name: tool.name,
16
+ description: tool.description,
17
+ schema: tool.inputSchema,
18
+ }, () => prompt(tool), {
19
+ ...(options ?? {}),
20
+ fullResponse: true,
21
+ });
22
+ return {
23
+ tool: tool.name,
24
+ parameters: value,
25
+ result: await executeTool(tool, value, {
26
+ run: options?.run,
27
+ }),
28
+ };
29
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useToolOrGenerateText = void 0;
4
+ const generateJsonOrText_js_1 = require("../model-function/generate-json/generateJsonOrText.cjs");
5
+ const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
6
+ const executeTool_js_1 = require("./executeTool.cjs");
7
+ async function useToolOrGenerateText(model, tools, prompt, options) {
8
+ const expandedPrompt = prompt(tools);
9
+ const modelResponse = await (0, generateJsonOrText_js_1.generateJsonOrText)(model, tools.map((tool) => ({
10
+ name: tool.name,
11
+ description: tool.description,
12
+ schema: tool.inputSchema,
13
+ })), () => expandedPrompt, options);
14
+ const { schema, text } = modelResponse;
15
+ if (schema == null) {
16
+ return {
17
+ tool: null,
18
+ parameters: null,
19
+ result: null,
20
+ text,
21
+ };
22
+ }
23
+ const tool = tools.find((tool) => tool.name === schema);
24
+ if (tool == null) {
25
+ throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
26
+ }
27
+ const toolParameters = modelResponse.value;
28
+ const result = await (0, executeTool_js_1.executeTool)(tool, toolParameters, {
29
+ run: options?.run,
30
+ });
31
+ return {
32
+ tool: schema,
33
+ result,
34
+ parameters: toolParameters,
35
+ text: text, // string | null is the expected value here
36
+ };
37
+ }
38
+ exports.useToolOrGenerateText = useToolOrGenerateText;
@@ -1,19 +1,6 @@
1
- import { FunctionOptions } from "../../model-function/FunctionOptions.js";
2
- import { GenerateJsonModel, GenerateJsonModelSettings, GenerateJsonPrompt } from "../../model-function/generate-json/GenerateJsonModel.js";
3
- import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "../../model-function/generate-json/GenerateJsonOrTextModel.js";
1
+ import { FunctionOptions } from "../model-function/FunctionOptions.js";
2
+ import { GenerateJsonOrTextModel, GenerateJsonOrTextModelSettings, GenerateJsonOrTextPrompt } from "../model-function/generate-json/GenerateJsonOrTextModel.js";
4
3
  import { Tool } from "./Tool.js";
5
- /**
6
- * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
7
- *
8
- * @returns The result contains the name of the tool (`tool` property),
9
- * the parameters (`parameters` property, typed),
10
- * and the result of the tool execution (`result` property, typed).
11
- */
12
- export declare function useTool<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings, TOOL extends Tool<any, any, any>>(model: GenerateJsonModel<PROMPT, RESPONSE, SETTINGS>, tool: TOOL, prompt: (tool: TOOL) => PROMPT & GenerateJsonPrompt<RESPONSE>, options?: FunctionOptions<SETTINGS>): Promise<{
13
- tool: TOOL["name"];
14
- parameters: TOOL["inputSchema"];
15
- result: Awaited<ReturnType<TOOL["execute"]>>;
16
- }>;
17
4
  type ToolArray<T extends Tool<any, any, any>[]> = T;
18
5
  type ToToolMap<T extends ToolArray<Tool<any, any, any>[]>> = {
19
6
  [K in T[number]["name"]]: Extract<T[number], Tool<K, any, any>>;
@@ -0,0 +1,34 @@
1
+ import { generateJsonOrText } from "../model-function/generate-json/generateJsonOrText.js";
2
+ import { NoSuchToolError } from "./NoSuchToolError.js";
3
+ import { executeTool } from "./executeTool.js";
4
+ export async function useToolOrGenerateText(model, tools, prompt, options) {
5
+ const expandedPrompt = prompt(tools);
6
+ const modelResponse = await generateJsonOrText(model, tools.map((tool) => ({
7
+ name: tool.name,
8
+ description: tool.description,
9
+ schema: tool.inputSchema,
10
+ })), () => expandedPrompt, options);
11
+ const { schema, text } = modelResponse;
12
+ if (schema == null) {
13
+ return {
14
+ tool: null,
15
+ parameters: null,
16
+ result: null,
17
+ text,
18
+ };
19
+ }
20
+ const tool = tools.find((tool) => tool.name === schema);
21
+ if (tool == null) {
22
+ throw new NoSuchToolError(schema.toString());
23
+ }
24
+ const toolParameters = modelResponse.value;
25
+ const result = await executeTool(tool, toolParameters, {
26
+ run: options?.run,
27
+ });
28
+ return {
29
+ tool: schema,
30
+ result,
31
+ parameters: toolParameters,
32
+ text: text, // string | null is the expected value here
33
+ };
34
+ }
@@ -1,62 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useToolOrGenerateText = exports.useTool = void 0;
4
- const generateJson_js_1 = require("../../model-function/generate-json/generateJson.cjs");
5
- const generateJsonOrText_js_1 = require("../../model-function/generate-json/generateJsonOrText.cjs");
6
- const NoSuchToolError_js_1 = require("./NoSuchToolError.cjs");
7
- // In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
8
- // retrieved through lookups such as TOOL["name"], such that any does not affect any client.
9
- /* eslint-disable @typescript-eslint/no-explicit-any */
10
- /**
11
- * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
12
- *
13
- * @returns The result contains the name of the tool (`tool` property),
14
- * the parameters (`parameters` property, typed),
15
- * and the result of the tool execution (`result` property, typed).
16
- */
17
- async function useTool(model, tool, prompt, options) {
18
- const { value } = await (0, generateJson_js_1.generateJson)(model, {
19
- name: tool.name,
20
- description: tool.description,
21
- schema: tool.inputSchema,
22
- }, () => prompt(tool), {
23
- ...(options ?? {}),
24
- fullResponse: true,
25
- });
26
- return {
27
- tool: tool.name,
28
- parameters: value,
29
- result: await tool.execute(value),
30
- };
31
- }
32
- exports.useTool = useTool;
33
- async function useToolOrGenerateText(model, tools, prompt, options) {
34
- const expandedPrompt = prompt(tools);
35
- const modelResponse = await (0, generateJsonOrText_js_1.generateJsonOrText)(model, tools.map((tool) => ({
36
- name: tool.name,
37
- description: tool.description,
38
- schema: tool.inputSchema,
39
- })), () => expandedPrompt, options);
40
- const { schema, text } = modelResponse;
41
- if (schema == null) {
42
- return {
43
- tool: null,
44
- parameters: null,
45
- result: null,
46
- text,
47
- };
48
- }
49
- const tool = tools.find((tool) => tool.name === schema);
50
- if (tool == null) {
51
- throw new NoSuchToolError_js_1.NoSuchToolError(schema.toString());
52
- }
53
- const toolParameters = modelResponse.value;
54
- const result = await tool.execute(toolParameters);
55
- return {
56
- tool: schema,
57
- result,
58
- parameters: toolParameters,
59
- text: text, // string | null is the expected value here
60
- };
61
- }
62
- exports.useToolOrGenerateText = useToolOrGenerateText;
@@ -1,57 +0,0 @@
1
- import { generateJson } from "../../model-function/generate-json/generateJson.js";
2
- import { generateJsonOrText } from "../../model-function/generate-json/generateJsonOrText.js";
3
- import { NoSuchToolError } from "./NoSuchToolError.js";
4
- // In this file, using 'any' is required to allow for flexibility in the inputs. The actual types are
5
- // retrieved through lookups such as TOOL["name"], such that any does not affect any client.
6
- /* eslint-disable @typescript-eslint/no-explicit-any */
7
- /**
8
- * `useTool` uses `generateJson` to generate parameters for a tool and then executes the tool with the parameters.
9
- *
10
- * @returns The result contains the name of the tool (`tool` property),
11
- * the parameters (`parameters` property, typed),
12
- * and the result of the tool execution (`result` property, typed).
13
- */
14
- export async function useTool(model, tool, prompt, options) {
15
- const { value } = await generateJson(model, {
16
- name: tool.name,
17
- description: tool.description,
18
- schema: tool.inputSchema,
19
- }, () => prompt(tool), {
20
- ...(options ?? {}),
21
- fullResponse: true,
22
- });
23
- return {
24
- tool: tool.name,
25
- parameters: value,
26
- result: await tool.execute(value),
27
- };
28
- }
29
- export async function useToolOrGenerateText(model, tools, prompt, options) {
30
- const expandedPrompt = prompt(tools);
31
- const modelResponse = await generateJsonOrText(model, tools.map((tool) => ({
32
- name: tool.name,
33
- description: tool.description,
34
- schema: tool.inputSchema,
35
- })), () => expandedPrompt, options);
36
- const { schema, text } = modelResponse;
37
- if (schema == null) {
38
- return {
39
- tool: null,
40
- parameters: null,
41
- result: null,
42
- text,
43
- };
44
- }
45
- const tool = tools.find((tool) => tool.name === schema);
46
- if (tool == null) {
47
- throw new NoSuchToolError(schema.toString());
48
- }
49
- const toolParameters = modelResponse.value;
50
- const result = await tool.execute(toolParameters);
51
- return {
52
- tool: schema,
53
- result,
54
- parameters: toolParameters,
55
- text: text, // string | null is the expected value here
56
- };
57
- }
@@ -1,13 +0,0 @@
1
- import { ErrorHandler } from "../util/ErrorHandler.js";
2
- import { ModelCallFinishedEvent, ModelCallStartedEvent } from "./ModelCallEvent.js";
3
- import { ModelCallObserver } from "./ModelCallObserver.js";
4
- export declare class ModelCallEventSource {
5
- readonly observers: ModelCallObserver[];
6
- readonly errorHandler: ErrorHandler;
7
- constructor({ observers, errorHandler, }: {
8
- observers: ModelCallObserver[];
9
- errorHandler?: ErrorHandler;
10
- });
11
- notifyModelCallStarted(event: ModelCallStartedEvent): void;
12
- notifyModelCallFinished(event: ModelCallFinishedEvent): void;
13
- }
@@ -1,5 +0,0 @@
1
- import { ModelCallFinishedEvent, ModelCallStartedEvent } from "./ModelCallEvent.js";
2
- export type ModelCallObserver = {
3
- onModelCallStarted?: (event: ModelCallStartedEvent) => void;
4
- onModelCallFinished?: (event: ModelCallFinishedEvent) => void;
5
- };