modelfusion 0.8.0 → 0.10.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
@@ -199,7 +199,7 @@ A tool is a function with a name, a description, and a schema for the input para
199
199
 
200
200
  ```ts
201
201
  const calculator = new Tool({
202
- name: "calculator" as const, // mark 'as const' for type inference
202
+ name: "calculator",
203
203
  description: "Execute a calculation",
204
204
 
205
205
  inputSchema: z.object({
@@ -17,10 +17,7 @@ async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, m
17
17
  tokenizer: model.tokenizer,
18
18
  maxTokensPerChunk: tokenLimit - emptyPromptTokens,
19
19
  }),
20
- summarize: async (input) => {
21
- const text = await (0, generateText_js_1.generateText)(model, await prompt(input), options);
22
- return text;
23
- },
20
+ summarize: async (input) => (0, generateText_js_1.generateText)(model, await prompt(input), options),
24
21
  join,
25
22
  text,
26
23
  }, options);
@@ -14,10 +14,7 @@ export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({
14
14
  tokenizer: model.tokenizer,
15
15
  maxTokensPerChunk: tokenLimit - emptyPromptTokens,
16
16
  }),
17
- summarize: async (input) => {
18
- const text = await generateText(model, await prompt(input), options);
19
- return text;
20
- },
17
+ summarize: async (input) => generateText(model, await prompt(input), options),
21
18
  join,
22
19
  text,
23
20
  }, options);
@@ -21,4 +21,27 @@ exports.OpenAIChatMessage = {
21
21
  functionResult(name, content) {
22
22
  return { role: "function", name, content };
23
23
  },
24
+ /**
25
+ * Creates a function call chat message for tool calls.
26
+ */
27
+ toolCall({ text, tool, parameters, }) {
28
+ return {
29
+ role: "assistant",
30
+ content: text,
31
+ function_call: {
32
+ name: tool,
33
+ arguments: JSON.stringify(parameters),
34
+ },
35
+ };
36
+ },
37
+ /**
38
+ * Creates a function result chat message for tool call results.
39
+ */
40
+ toolResult({ tool, result }) {
41
+ return {
42
+ role: "function",
43
+ name: tool,
44
+ content: JSON.stringify(result),
45
+ };
46
+ },
24
47
  };
@@ -23,4 +23,30 @@ export declare const OpenAIChatMessage: {
23
23
  arguments: string;
24
24
  }): OpenAIChatMessage;
25
25
  functionResult(name: string, content: string): OpenAIChatMessage;
26
+ /**
27
+ * Creates a function call chat message for tool calls.
28
+ */
29
+ toolCall({ text, tool, parameters, }: {
30
+ text: string | null;
31
+ tool: string;
32
+ parameters: unknown;
33
+ }): {
34
+ role: "assistant";
35
+ content: string | null;
36
+ function_call: {
37
+ name: string;
38
+ arguments: string;
39
+ };
40
+ };
41
+ /**
42
+ * Creates a function result chat message for tool call results.
43
+ */
44
+ toolResult({ tool, result }: {
45
+ tool: string;
46
+ result: unknown;
47
+ }): {
48
+ role: "function";
49
+ name: string;
50
+ content: string;
51
+ };
26
52
  };
@@ -18,4 +18,27 @@ export const OpenAIChatMessage = {
18
18
  functionResult(name, content) {
19
19
  return { role: "function", name, content };
20
20
  },
21
+ /**
22
+ * Creates a function call chat message for tool calls.
23
+ */
24
+ toolCall({ text, tool, parameters, }) {
25
+ return {
26
+ role: "assistant",
27
+ content: text,
28
+ function_call: {
29
+ name: tool,
30
+ arguments: JSON.stringify(parameters),
31
+ },
32
+ };
33
+ },
34
+ /**
35
+ * Creates a function result chat message for tool call results.
36
+ */
37
+ toolResult({ tool, result }) {
38
+ return {
39
+ role: "function",
40
+ name: tool,
41
+ content: JSON.stringify(result),
42
+ };
43
+ },
21
44
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.8.0",
4
+ "version": "0.10.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidToolNameError = void 0;
4
+ class InvalidToolNameError extends Error {
5
+ constructor({ toolName, namePattern, }) {
6
+ super(`Invalid tool name '${toolName}'. The tool name must match the regular expression pattern ${namePattern}.`);
7
+ Object.defineProperty(this, "toolName", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ this.name = "InvalidToolNameError";
14
+ this.toolName = toolName;
15
+ }
16
+ }
17
+ exports.InvalidToolNameError = InvalidToolNameError;
@@ -0,0 +1,7 @@
1
+ export declare class InvalidToolNameError extends Error {
2
+ readonly toolName: string;
3
+ constructor({ toolName, namePattern, }: {
4
+ toolName: string;
5
+ namePattern: RegExp;
6
+ });
7
+ }
@@ -0,0 +1,13 @@
1
+ export class InvalidToolNameError extends Error {
2
+ constructor({ toolName, namePattern, }) {
3
+ super(`Invalid tool name '${toolName}'. The tool name must match the regular expression pattern ${namePattern}.`);
4
+ Object.defineProperty(this, "toolName", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ this.name = "InvalidToolNameError";
11
+ this.toolName = toolName;
12
+ }
13
+ }
package/tool/Tool.cjs CHANGED
@@ -1,44 +1,79 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tool = void 0;
4
+ const InvalidToolNameError_js_1 = require("./InvalidToolNameError.cjs");
5
+ const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
6
+ /**
7
+ * A tool is a function with a name, description and defined inputs that can be used
8
+ * by agents and chatbots.
9
+ */
4
10
  class Tool {
5
- constructor(options) {
11
+ constructor({ name, description, inputSchema, outputSchema, execute, }) {
12
+ /**
13
+ * The name of the tool.
14
+ * It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
15
+ * Should be understandable for language models and unique among the tools that they know.
16
+ */
6
17
  Object.defineProperty(this, "name", {
7
18
  enumerable: true,
8
19
  configurable: true,
9
20
  writable: true,
10
21
  value: void 0
11
22
  });
23
+ /**
24
+ * A description of what the tool does. Will be used by the language model to decide whether to use the tool.
25
+ */
12
26
  Object.defineProperty(this, "description", {
13
27
  enumerable: true,
14
28
  configurable: true,
15
29
  writable: true,
16
30
  value: void 0
17
31
  });
32
+ /**
33
+ * The schema of the input that the tool expects. The language model will use this to generate the input.
34
+ * Use descriptions to make the input understandable for the language model.
35
+ */
18
36
  Object.defineProperty(this, "inputSchema", {
19
37
  enumerable: true,
20
38
  configurable: true,
21
39
  writable: true,
22
40
  value: void 0
23
41
  });
42
+ /**
43
+ * An optional schema of the output that the tool produces. This will be used to validate the output.
44
+ */
24
45
  Object.defineProperty(this, "outputSchema", {
25
46
  enumerable: true,
26
47
  configurable: true,
27
48
  writable: true,
28
49
  value: void 0
29
50
  });
51
+ /**
52
+ * The actual execution function of the tool.
53
+ */
30
54
  Object.defineProperty(this, "execute", {
31
55
  enumerable: true,
32
56
  configurable: true,
33
57
  writable: true,
34
58
  value: void 0
35
59
  });
36
- this.name = options.name;
37
- this.description = options.description;
38
- this.inputSchema = options.inputSchema;
39
- this.outputSchema = options.outputSchema;
40
- this.execute = options.execute;
60
+ // check that the name is a valid function name:
61
+ if (!namePattern.test(name)) {
62
+ throw new InvalidToolNameError_js_1.InvalidToolNameError({
63
+ toolName: name,
64
+ namePattern,
65
+ });
66
+ }
67
+ this.name = name;
68
+ this.description = description;
69
+ this.inputSchema = inputSchema;
70
+ this.outputSchema = outputSchema;
71
+ this.execute = execute;
41
72
  }
73
+ /**
74
+ * Provdes a schema definition with the name, description and schema of the input.
75
+ * This is used by `useTool`.
76
+ */
42
77
  get inputSchemaDefinition() {
43
78
  return {
44
79
  name: this.name,
package/tool/Tool.d.ts CHANGED
@@ -1,18 +1,44 @@
1
1
  import { z } from "zod";
2
2
  import { SchemaDefinition } from "../model-function/generate-json/SchemaDefinition.js";
3
3
  import { RunFunction } from "../run/RunFunction.js";
4
+ /**
5
+ * A tool is a function with a name, description and defined inputs that can be used
6
+ * by agents and chatbots.
7
+ */
4
8
  export declare class Tool<NAME extends string, INPUT, OUTPUT> {
9
+ /**
10
+ * The name of the tool.
11
+ * It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
12
+ * Should be understandable for language models and unique among the tools that they know.
13
+ */
5
14
  readonly name: NAME;
15
+ /**
16
+ * A description of what the tool does. Will be used by the language model to decide whether to use the tool.
17
+ */
6
18
  readonly description: string;
19
+ /**
20
+ * The schema of the input that the tool expects. The language model will use this to generate the input.
21
+ * Use descriptions to make the input understandable for the language model.
22
+ */
7
23
  readonly inputSchema: z.ZodSchema<INPUT>;
24
+ /**
25
+ * An optional schema of the output that the tool produces. This will be used to validate the output.
26
+ */
8
27
  readonly outputSchema?: z.ZodSchema<OUTPUT>;
28
+ /**
29
+ * The actual execution function of the tool.
30
+ */
9
31
  readonly execute: RunFunction<INPUT, OUTPUT>;
10
- constructor(options: {
32
+ constructor({ name, description, inputSchema, outputSchema, execute, }: {
11
33
  name: NAME;
12
34
  description: string;
13
35
  inputSchema: z.ZodSchema<INPUT>;
14
36
  outputSchema?: z.ZodSchema<OUTPUT>;
15
37
  execute(input: INPUT): Promise<OUTPUT>;
16
38
  });
39
+ /**
40
+ * Provdes a schema definition with the name, description and schema of the input.
41
+ * This is used by `useTool`.
42
+ */
17
43
  get inputSchemaDefinition(): SchemaDefinition<NAME, INPUT>;
18
44
  }
package/tool/Tool.js CHANGED
@@ -1,41 +1,76 @@
1
+ import { InvalidToolNameError } from "./InvalidToolNameError.js";
2
+ const namePattern = /^[a-zA-Z0-9_-]{1,64}$/;
3
+ /**
4
+ * A tool is a function with a name, description and defined inputs that can be used
5
+ * by agents and chatbots.
6
+ */
1
7
  export class Tool {
2
- constructor(options) {
8
+ constructor({ name, description, inputSchema, outputSchema, execute, }) {
9
+ /**
10
+ * The name of the tool.
11
+ * It has to be a function name that matches the regular expression pattern '^[a-zA-Z0-9_-]{1,64}$'.
12
+ * Should be understandable for language models and unique among the tools that they know.
13
+ */
3
14
  Object.defineProperty(this, "name", {
4
15
  enumerable: true,
5
16
  configurable: true,
6
17
  writable: true,
7
18
  value: void 0
8
19
  });
20
+ /**
21
+ * A description of what the tool does. Will be used by the language model to decide whether to use the tool.
22
+ */
9
23
  Object.defineProperty(this, "description", {
10
24
  enumerable: true,
11
25
  configurable: true,
12
26
  writable: true,
13
27
  value: void 0
14
28
  });
29
+ /**
30
+ * The schema of the input that the tool expects. The language model will use this to generate the input.
31
+ * Use descriptions to make the input understandable for the language model.
32
+ */
15
33
  Object.defineProperty(this, "inputSchema", {
16
34
  enumerable: true,
17
35
  configurable: true,
18
36
  writable: true,
19
37
  value: void 0
20
38
  });
39
+ /**
40
+ * An optional schema of the output that the tool produces. This will be used to validate the output.
41
+ */
21
42
  Object.defineProperty(this, "outputSchema", {
22
43
  enumerable: true,
23
44
  configurable: true,
24
45
  writable: true,
25
46
  value: void 0
26
47
  });
48
+ /**
49
+ * The actual execution function of the tool.
50
+ */
27
51
  Object.defineProperty(this, "execute", {
28
52
  enumerable: true,
29
53
  configurable: true,
30
54
  writable: true,
31
55
  value: void 0
32
56
  });
33
- this.name = options.name;
34
- this.description = options.description;
35
- this.inputSchema = options.inputSchema;
36
- this.outputSchema = options.outputSchema;
37
- this.execute = options.execute;
57
+ // check that the name is a valid function name:
58
+ if (!namePattern.test(name)) {
59
+ throw new InvalidToolNameError({
60
+ toolName: name,
61
+ namePattern,
62
+ });
63
+ }
64
+ this.name = name;
65
+ this.description = description;
66
+ this.inputSchema = inputSchema;
67
+ this.outputSchema = outputSchema;
68
+ this.execute = execute;
38
69
  }
70
+ /**
71
+ * Provdes a schema definition with the name, description and schema of the input.
72
+ * This is used by `useTool`.
73
+ */
39
74
  get inputSchemaDefinition() {
40
75
  return {
41
76
  name: this.name,
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebSearchTool = void 0;
4
+ const zod_1 = require("zod");
5
+ const Tool_js_1 = require("./Tool.cjs");
6
+ const INPUT_SCHEMA = zod_1.z.object({
7
+ query: zod_1.z.string(),
8
+ });
9
+ const OUTPUT_SCHEMA = zod_1.z.object({
10
+ results: zod_1.z.array(zod_1.z.object({
11
+ title: zod_1.z.string(),
12
+ link: zod_1.z.string().url(),
13
+ snippet: zod_1.z.string(),
14
+ })),
15
+ });
16
+ /**
17
+ * @see https://serpapi.com/search-api
18
+ */
19
+ class WebSearchTool extends Tool_js_1.Tool {
20
+ constructor(options) {
21
+ super({
22
+ name: options.name,
23
+ description: options.description,
24
+ inputSchema: INPUT_SCHEMA,
25
+ outputSchema: OUTPUT_SCHEMA,
26
+ execute: options.execute,
27
+ });
28
+ }
29
+ }
30
+ exports.WebSearchTool = WebSearchTool;
31
+ // expose the schemas to library consumers:
32
+ Object.defineProperty(WebSearchTool, "INPUT_SCHEMA", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: INPUT_SCHEMA
37
+ });
38
+ Object.defineProperty(WebSearchTool, "OUTPUT_SCHEMA", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: OUTPUT_SCHEMA
43
+ });
@@ -0,0 +1,81 @@
1
+ import { z } from "zod";
2
+ import { Tool } from "./Tool.js";
3
+ declare const INPUT_SCHEMA: z.ZodObject<{
4
+ query: z.ZodString;
5
+ }, "strip", z.ZodTypeAny, {
6
+ query: string;
7
+ }, {
8
+ query: string;
9
+ }>;
10
+ declare const OUTPUT_SCHEMA: z.ZodObject<{
11
+ results: z.ZodArray<z.ZodObject<{
12
+ title: z.ZodString;
13
+ link: z.ZodString;
14
+ snippet: z.ZodString;
15
+ }, "strip", z.ZodTypeAny, {
16
+ link: string;
17
+ title: string;
18
+ snippet: string;
19
+ }, {
20
+ link: string;
21
+ title: string;
22
+ snippet: string;
23
+ }>, "many">;
24
+ }, "strip", z.ZodTypeAny, {
25
+ results: {
26
+ link: string;
27
+ title: string;
28
+ snippet: string;
29
+ }[];
30
+ }, {
31
+ results: {
32
+ link: string;
33
+ title: string;
34
+ snippet: string;
35
+ }[];
36
+ }>;
37
+ /**
38
+ * @see https://serpapi.com/search-api
39
+ */
40
+ export declare class WebSearchTool<NAME extends string> extends Tool<NAME, z.infer<typeof INPUT_SCHEMA>, z.infer<typeof OUTPUT_SCHEMA>> {
41
+ static readonly INPUT_SCHEMA: z.ZodObject<{
42
+ query: z.ZodString;
43
+ }, "strip", z.ZodTypeAny, {
44
+ query: string;
45
+ }, {
46
+ query: string;
47
+ }>;
48
+ static readonly OUTPUT_SCHEMA: z.ZodObject<{
49
+ results: z.ZodArray<z.ZodObject<{
50
+ title: z.ZodString;
51
+ link: z.ZodString;
52
+ snippet: z.ZodString;
53
+ }, "strip", z.ZodTypeAny, {
54
+ link: string;
55
+ title: string;
56
+ snippet: string;
57
+ }, {
58
+ link: string;
59
+ title: string;
60
+ snippet: string;
61
+ }>, "many">;
62
+ }, "strip", z.ZodTypeAny, {
63
+ results: {
64
+ link: string;
65
+ title: string;
66
+ snippet: string;
67
+ }[];
68
+ }, {
69
+ results: {
70
+ link: string;
71
+ title: string;
72
+ snippet: string;
73
+ }[];
74
+ }>;
75
+ constructor(options: {
76
+ name: NAME;
77
+ description: string;
78
+ execute(input: z.infer<typeof INPUT_SCHEMA>): Promise<z.infer<typeof OUTPUT_SCHEMA>>;
79
+ });
80
+ }
81
+ export {};
@@ -0,0 +1,39 @@
1
+ import { z } from "zod";
2
+ import { Tool } from "./Tool.js";
3
+ const INPUT_SCHEMA = z.object({
4
+ query: z.string(),
5
+ });
6
+ const OUTPUT_SCHEMA = z.object({
7
+ results: z.array(z.object({
8
+ title: z.string(),
9
+ link: z.string().url(),
10
+ snippet: z.string(),
11
+ })),
12
+ });
13
+ /**
14
+ * @see https://serpapi.com/search-api
15
+ */
16
+ export class WebSearchTool extends Tool {
17
+ constructor(options) {
18
+ super({
19
+ name: options.name,
20
+ description: options.description,
21
+ inputSchema: INPUT_SCHEMA,
22
+ outputSchema: OUTPUT_SCHEMA,
23
+ execute: options.execute,
24
+ });
25
+ }
26
+ }
27
+ // expose the schemas to library consumers:
28
+ Object.defineProperty(WebSearchTool, "INPUT_SCHEMA", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: INPUT_SCHEMA
33
+ });
34
+ Object.defineProperty(WebSearchTool, "OUTPUT_SCHEMA", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: OUTPUT_SCHEMA
39
+ });
@@ -9,6 +9,9 @@ export type ExecuteToolMetadata = {
9
9
  startEpochSeconds: number;
10
10
  durationInMs: number;
11
11
  };
12
+ /**
13
+ * `executeTool` directly executes a tool with the given parameters.
14
+ */
12
15
  export declare function executeTool<INPUT, OUTPUT>(tool: Tool<string, INPUT, OUTPUT>, input: INPUT, options: FunctionOptions<undefined> & {
13
16
  fullResponse: true;
14
17
  }): Promise<{
package/tool/index.cjs CHANGED
@@ -14,9 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./InvalidToolNameError.cjs"), exports);
17
18
  __exportStar(require("./NoSuchToolError.cjs"), exports);
18
19
  __exportStar(require("./Tool.cjs"), exports);
19
20
  __exportStar(require("./ToolExecutionError.cjs"), exports);
21
+ __exportStar(require("./WebSearchTool.cjs"), exports);
20
22
  __exportStar(require("./executeTool.cjs"), exports);
21
23
  __exportStar(require("./useTool.cjs"), exports);
22
24
  __exportStar(require("./useToolOrGenerateText.cjs"), exports);
package/tool/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
+ export * from "./InvalidToolNameError.js";
1
2
  export * from "./NoSuchToolError.js";
2
3
  export * from "./Tool.js";
3
4
  export * from "./ToolExecutionError.js";
5
+ export * from "./WebSearchTool.js";
4
6
  export * from "./executeTool.js";
5
7
  export * from "./useTool.js";
6
8
  export * from "./useToolOrGenerateText.js";
package/tool/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ export * from "./InvalidToolNameError.js";
1
2
  export * from "./NoSuchToolError.js";
2
3
  export * from "./Tool.js";
3
4
  export * from "./ToolExecutionError.js";
5
+ export * from "./WebSearchTool.js";
4
6
  export * from "./executeTool.js";
5
7
  export * from "./useTool.js";
6
8
  export * from "./useToolOrGenerateText.js";