modelfusion 0.105.0 → 0.107.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 (199) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +16 -59
  3. package/core/DefaultRun.cjs +0 -4
  4. package/core/DefaultRun.d.ts +0 -2
  5. package/core/DefaultRun.js +0 -4
  6. package/core/ExtensionFunctionEvent.d.ts +11 -0
  7. package/core/FunctionEvent.d.ts +2 -2
  8. package/extension/index.cjs +22 -3
  9. package/extension/index.d.ts +5 -1
  10. package/extension/index.js +4 -1
  11. package/index.cjs +0 -3
  12. package/index.d.ts +0 -3
  13. package/index.js +0 -3
  14. package/model-function/generate-structure/jsonStructurePrompt.cjs +42 -6
  15. package/model-function/generate-structure/jsonStructurePrompt.d.ts +12 -1
  16. package/model-function/generate-structure/jsonStructurePrompt.js +42 -5
  17. package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -1
  18. package/model-function/generate-text/PromptTemplateTextGenerationModel.js +1 -1
  19. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.test.cjs +11 -0
  20. package/model-function/generate-text/prompt-template/ChatMLPromptTemplate.test.js +11 -0
  21. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +10 -8
  22. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +1 -1
  23. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +10 -8
  24. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.test.cjs +11 -0
  25. package/model-function/generate-text/prompt-template/Llama2PromptTemplate.test.js +11 -0
  26. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.cjs +150 -0
  27. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.d.ts +62 -0
  28. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.js +143 -0
  29. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.cjs +60 -0
  30. package/model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.js +58 -0
  31. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.test.cjs +11 -0
  32. package/model-function/generate-text/prompt-template/NeuralChatPromptTemplate.test.js +11 -0
  33. package/model-function/generate-text/prompt-template/TextPromptTemplate.test.cjs +11 -0
  34. package/model-function/generate-text/prompt-template/TextPromptTemplate.test.js +11 -0
  35. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.test.cjs +11 -0
  36. package/model-function/generate-text/prompt-template/VicunaPromptTemplate.test.js +11 -0
  37. package/model-function/generate-text/prompt-template/index.cjs +2 -1
  38. package/model-function/generate-text/prompt-template/index.d.ts +1 -0
  39. package/model-function/generate-text/prompt-template/index.js +1 -0
  40. package/model-function/index.cjs +0 -1
  41. package/model-function/index.d.ts +0 -1
  42. package/model-function/index.js +0 -1
  43. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
  44. package/model-provider/cohere/CohereTextGenerationModel.d.ts +6 -6
  45. package/model-provider/llamacpp/LlamaCppBakLLaVA1PromptTemplate.d.ts +3 -3
  46. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.cjs → LlamaCppCompletionModel.cjs} +8 -8
  47. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.d.ts → LlamaCppCompletionModel.d.ts} +49 -49
  48. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.js → LlamaCppCompletionModel.js} +6 -6
  49. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.test.cjs → LlamaCppCompletionModel.test.cjs} +3 -3
  50. package/model-provider/llamacpp/{LlamaCppTextGenerationModel.test.js → LlamaCppCompletionModel.test.js} +3 -3
  51. package/model-provider/llamacpp/LlamaCppFacade.cjs +2 -2
  52. package/model-provider/llamacpp/LlamaCppFacade.d.ts +2 -2
  53. package/model-provider/llamacpp/LlamaCppFacade.js +2 -2
  54. package/model-provider/llamacpp/index.cjs +1 -1
  55. package/model-provider/llamacpp/index.d.ts +1 -1
  56. package/model-provider/llamacpp/index.js +1 -1
  57. package/model-provider/mistral/MistralChatModel.cjs +4 -4
  58. package/model-provider/mistral/MistralChatModel.d.ts +6 -6
  59. package/model-provider/mistral/MistralChatModel.js +1 -1
  60. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
  61. package/model-provider/mistral/index.cjs +3 -3
  62. package/model-provider/mistral/index.d.ts +2 -2
  63. package/model-provider/mistral/index.js +2 -2
  64. package/model-provider/ollama/OllamaChatModel.d.ts +9 -8
  65. package/model-provider/ollama/OllamaChatModel.js +1 -1
  66. package/model-provider/ollama/OllamaCompletionModel.d.ts +2 -1
  67. package/model-provider/ollama/OllamaCompletionModel.js +1 -1
  68. package/model-provider/ollama/OllamaCompletionModel.test.cjs +1 -7
  69. package/model-provider/ollama/OllamaCompletionModel.test.js +1 -7
  70. package/model-provider/openai/AbstractOpenAIChatModel.d.ts +8 -8
  71. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -1
  72. package/model-provider/openai/OpenAICompletionModel.d.ts +6 -6
  73. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
  74. package/model-provider/openai/OpenAITranscriptionModel.d.ts +11 -11
  75. package/model-provider/openai/index.cjs +0 -1
  76. package/model-provider/openai/index.d.ts +0 -1
  77. package/model-provider/openai/index.js +0 -1
  78. package/model-provider/stability/StabilityImageGenerationModel.d.ts +5 -5
  79. package/package.json +9 -20
  80. package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +1 -1
  81. package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +1 -1
  82. package/tool/generate-tool-call/TextGenerationToolCallModel.js +1 -1
  83. package/tool/generate-tool-call/index.cjs +1 -0
  84. package/tool/generate-tool-call/index.d.ts +1 -0
  85. package/tool/generate-tool-call/index.js +1 -0
  86. package/tool/generate-tool-call/jsonToolCallPrompt.cjs +30 -0
  87. package/tool/generate-tool-call/jsonToolCallPrompt.d.ts +5 -0
  88. package/tool/generate-tool-call/jsonToolCallPrompt.js +27 -0
  89. package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.d.ts +1 -11
  90. package/tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.d.ts +12 -0
  91. package/tool/generate-tool-calls-or-text/index.cjs +1 -0
  92. package/tool/generate-tool-calls-or-text/index.d.ts +1 -0
  93. package/tool/generate-tool-calls-or-text/index.js +1 -0
  94. package/util/index.cjs +0 -1
  95. package/util/index.d.ts +0 -1
  96. package/util/index.js +0 -1
  97. package/browser/MediaSourceAppender.cjs +0 -54
  98. package/browser/MediaSourceAppender.d.ts +0 -11
  99. package/browser/MediaSourceAppender.js +0 -50
  100. package/browser/convertAudioChunksToBase64.cjs +0 -8
  101. package/browser/convertAudioChunksToBase64.d.ts +0 -4
  102. package/browser/convertAudioChunksToBase64.js +0 -4
  103. package/browser/convertBlobToBase64.cjs +0 -23
  104. package/browser/convertBlobToBase64.d.ts +0 -1
  105. package/browser/convertBlobToBase64.js +0 -19
  106. package/browser/index.cjs +0 -22
  107. package/browser/index.d.ts +0 -6
  108. package/browser/index.js +0 -6
  109. package/browser/invokeFlow.cjs +0 -23
  110. package/browser/invokeFlow.d.ts +0 -8
  111. package/browser/invokeFlow.js +0 -19
  112. package/browser/readEventSource.cjs +0 -29
  113. package/browser/readEventSource.d.ts +0 -9
  114. package/browser/readEventSource.js +0 -25
  115. package/browser/readEventSourceStream.cjs +0 -35
  116. package/browser/readEventSourceStream.d.ts +0 -7
  117. package/browser/readEventSourceStream.js +0 -31
  118. package/composed-function/index.cjs +0 -19
  119. package/composed-function/index.d.ts +0 -3
  120. package/composed-function/index.js +0 -3
  121. package/composed-function/summarize/SummarizationFunction.d.ts +0 -4
  122. package/composed-function/summarize/summarizeRecursively.cjs +0 -19
  123. package/composed-function/summarize/summarizeRecursively.d.ts +0 -11
  124. package/composed-function/summarize/summarizeRecursively.js +0 -15
  125. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +0 -25
  126. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +0 -24
  127. package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +0 -21
  128. package/cost/Cost.cjs +0 -38
  129. package/cost/Cost.d.ts +0 -16
  130. package/cost/Cost.js +0 -34
  131. package/cost/CostCalculator.d.ts +0 -8
  132. package/cost/calculateCost.cjs +0 -28
  133. package/cost/calculateCost.d.ts +0 -7
  134. package/cost/calculateCost.js +0 -24
  135. package/cost/index.cjs +0 -19
  136. package/cost/index.d.ts +0 -3
  137. package/cost/index.js +0 -3
  138. package/guard/GuardEvent.cjs +0 -2
  139. package/guard/GuardEvent.d.ts +0 -7
  140. package/guard/fixStructure.cjs +0 -75
  141. package/guard/fixStructure.d.ts +0 -64
  142. package/guard/fixStructure.js +0 -71
  143. package/guard/guard.cjs +0 -79
  144. package/guard/guard.d.ts +0 -29
  145. package/guard/guard.js +0 -75
  146. package/guard/index.cjs +0 -19
  147. package/guard/index.d.ts +0 -3
  148. package/guard/index.js +0 -3
  149. package/model-function/SuccessfulModelCall.cjs +0 -10
  150. package/model-function/SuccessfulModelCall.d.ts +0 -12
  151. package/model-function/SuccessfulModelCall.js +0 -6
  152. package/model-provider/openai/OpenAICostCalculator.cjs +0 -89
  153. package/model-provider/openai/OpenAICostCalculator.d.ts +0 -6
  154. package/model-provider/openai/OpenAICostCalculator.js +0 -85
  155. package/server/fastify/AssetStorage.cjs +0 -2
  156. package/server/fastify/AssetStorage.d.ts +0 -17
  157. package/server/fastify/AssetStorage.js +0 -1
  158. package/server/fastify/DefaultFlow.cjs +0 -22
  159. package/server/fastify/DefaultFlow.d.ts +0 -16
  160. package/server/fastify/DefaultFlow.js +0 -18
  161. package/server/fastify/FileSystemAssetStorage.cjs +0 -60
  162. package/server/fastify/FileSystemAssetStorage.d.ts +0 -19
  163. package/server/fastify/FileSystemAssetStorage.js +0 -56
  164. package/server/fastify/FileSystemLogger.cjs +0 -49
  165. package/server/fastify/FileSystemLogger.d.ts +0 -18
  166. package/server/fastify/FileSystemLogger.js +0 -45
  167. package/server/fastify/Flow.cjs +0 -2
  168. package/server/fastify/Flow.d.ts +0 -9
  169. package/server/fastify/Flow.js +0 -1
  170. package/server/fastify/FlowRun.cjs +0 -71
  171. package/server/fastify/FlowRun.d.ts +0 -28
  172. package/server/fastify/FlowRun.js +0 -67
  173. package/server/fastify/FlowSchema.cjs +0 -2
  174. package/server/fastify/FlowSchema.d.ts +0 -5
  175. package/server/fastify/FlowSchema.js +0 -1
  176. package/server/fastify/Logger.cjs +0 -2
  177. package/server/fastify/Logger.d.ts +0 -13
  178. package/server/fastify/Logger.js +0 -1
  179. package/server/fastify/PathProvider.cjs +0 -34
  180. package/server/fastify/PathProvider.d.ts +0 -12
  181. package/server/fastify/PathProvider.js +0 -30
  182. package/server/fastify/index.cjs +0 -24
  183. package/server/fastify/index.d.ts +0 -8
  184. package/server/fastify/index.js +0 -8
  185. package/server/fastify/modelFusionFlowPlugin.cjs +0 -103
  186. package/server/fastify/modelFusionFlowPlugin.d.ts +0 -12
  187. package/server/fastify/modelFusionFlowPlugin.js +0 -99
  188. package/util/getAudioFileExtension.cjs +0 -29
  189. package/util/getAudioFileExtension.d.ts +0 -1
  190. package/util/getAudioFileExtension.js +0 -25
  191. /package/{composed-function/summarize/SummarizationFunction.cjs → core/ExtensionFunctionEvent.cjs} +0 -0
  192. /package/{composed-function/summarize/SummarizationFunction.js → core/ExtensionFunctionEvent.js} +0 -0
  193. /package/{cost/CostCalculator.js → model-function/generate-text/prompt-template/MistralInstructPromptTemplate.test.d.ts} +0 -0
  194. /package/{guard/GuardEvent.js → model-provider/llamacpp/LlamaCppCompletionModel.test.d.ts} +0 -0
  195. /package/model-provider/mistral/{MistralPromptTemplate.cjs → MistralChatPromptTemplate.cjs} +0 -0
  196. /package/model-provider/mistral/{MistralPromptTemplate.d.ts → MistralChatPromptTemplate.d.ts} +0 -0
  197. /package/model-provider/mistral/{MistralPromptTemplate.js → MistralChatPromptTemplate.js} +0 -0
  198. /package/{cost/CostCalculator.cjs → tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.cjs} +0 -0
  199. /package/{model-provider/llamacpp/LlamaCppTextGenerationModel.test.d.ts → tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.js} +0 -0
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FileSystemLogger = void 0;
4
- const node_fs_1 = require("node:fs");
5
- const node_path_1 = require("node:path");
6
- class FileSystemLogger {
7
- constructor({ path }) {
8
- Object.defineProperty(this, "logPath", {
9
- enumerable: true,
10
- configurable: true,
11
- writable: true,
12
- value: void 0
13
- });
14
- this.logPath = path;
15
- }
16
- async logFunctionEvent({ run, event, }) {
17
- const timestamp = event.startTimestamp.getTime();
18
- try {
19
- const logPath = this.logPath(run);
20
- await node_fs_1.promises.mkdir(logPath, { recursive: true });
21
- await node_fs_1.promises.writeFile((0, node_path_1.join)(logPath, `${timestamp}-${event.callId}-${event.functionId ?? event.functionType}-${event.eventType}.json`), JSON.stringify(event));
22
- }
23
- catch (error) {
24
- this.logError({
25
- run,
26
- message: `Failed to write function event ${event.callId}`,
27
- error,
28
- });
29
- }
30
- }
31
- async logError({ run, error, message, }) {
32
- const timestamp = Date.now();
33
- try {
34
- const logPath = this.logPath(run);
35
- await node_fs_1.promises.mkdir(logPath, { recursive: true });
36
- await node_fs_1.promises.writeFile((0, node_path_1.join)(logPath, `${timestamp}-error.json`), JSON.stringify({
37
- timestamp: new Date(timestamp).toISOString(),
38
- runId: run.runId,
39
- message,
40
- error,
41
- }));
42
- }
43
- catch (error) {
44
- console.error(`Failed to write error log`);
45
- console.error(error);
46
- }
47
- }
48
- }
49
- exports.FileSystemLogger = FileSystemLogger;
@@ -1,18 +0,0 @@
1
- import { FunctionEvent } from "../../core/FunctionEvent.js";
2
- import { FlowRun } from "./FlowRun.js";
3
- import { Logger } from "./Logger.js";
4
- export declare class FileSystemLogger implements Logger {
5
- private readonly logPath;
6
- constructor({ path }: {
7
- path: (run: FlowRun<unknown>) => string;
8
- });
9
- logFunctionEvent({ run, event, }: {
10
- run: FlowRun<unknown>;
11
- event: FunctionEvent;
12
- }): Promise<void>;
13
- logError({ run, error, message, }: {
14
- run: FlowRun<unknown>;
15
- message: string;
16
- error: unknown;
17
- }): Promise<void>;
18
- }
@@ -1,45 +0,0 @@
1
- import { promises as fs } from "node:fs";
2
- import { join } from "node:path";
3
- export class FileSystemLogger {
4
- constructor({ path }) {
5
- Object.defineProperty(this, "logPath", {
6
- enumerable: true,
7
- configurable: true,
8
- writable: true,
9
- value: void 0
10
- });
11
- this.logPath = path;
12
- }
13
- async logFunctionEvent({ run, event, }) {
14
- const timestamp = event.startTimestamp.getTime();
15
- try {
16
- const logPath = this.logPath(run);
17
- await fs.mkdir(logPath, { recursive: true });
18
- await fs.writeFile(join(logPath, `${timestamp}-${event.callId}-${event.functionId ?? event.functionType}-${event.eventType}.json`), JSON.stringify(event));
19
- }
20
- catch (error) {
21
- this.logError({
22
- run,
23
- message: `Failed to write function event ${event.callId}`,
24
- error,
25
- });
26
- }
27
- }
28
- async logError({ run, error, message, }) {
29
- const timestamp = Date.now();
30
- try {
31
- const logPath = this.logPath(run);
32
- await fs.mkdir(logPath, { recursive: true });
33
- await fs.writeFile(join(logPath, `${timestamp}-error.json`), JSON.stringify({
34
- timestamp: new Date(timestamp).toISOString(),
35
- runId: run.runId,
36
- message,
37
- error,
38
- }));
39
- }
40
- catch (error) {
41
- console.error(`Failed to write error log`);
42
- console.error(error);
43
- }
44
- }
45
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,9 +0,0 @@
1
- import { FlowRun } from "./FlowRun.js";
2
- import { FlowSchema } from "./FlowSchema.js";
3
- export interface Flow<INPUT, EVENT> {
4
- readonly schema: FlowSchema<INPUT, EVENT>;
5
- process: (options: {
6
- input: INPUT;
7
- run: FlowRun<EVENT>;
8
- }) => Promise<void>;
9
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FlowRun = void 0;
4
- const DefaultRun_js_1 = require("../../core/DefaultRun.cjs");
5
- const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
6
- class FlowRun extends DefaultRun_js_1.DefaultRun {
7
- constructor({ paths, assetStorage, logger, }) {
8
- super();
9
- Object.defineProperty(this, "eventQueue", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: new AsyncQueue_js_1.AsyncQueue()
14
- });
15
- Object.defineProperty(this, "assetStorage", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: void 0
20
- });
21
- Object.defineProperty(this, "logger", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: void 0
26
- });
27
- Object.defineProperty(this, "paths", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: void 0
32
- });
33
- Object.defineProperty(this, "functionObserver", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: {
38
- onFunctionEvent: async (event) => {
39
- this.logger.logFunctionEvent({
40
- run: this,
41
- event,
42
- });
43
- },
44
- }
45
- });
46
- this.paths = paths;
47
- this.assetStorage = assetStorage;
48
- this.logger = logger;
49
- }
50
- publishEvent(event) {
51
- this.eventQueue.push(event);
52
- }
53
- async storeBinaryAsset(asset) {
54
- await this.assetStorage.storeAsset({
55
- run: this,
56
- asset,
57
- });
58
- return this.paths.getAssetUrl(this.runId, asset.name);
59
- }
60
- async storeTextAsset(asset) {
61
- return this.storeBinaryAsset({
62
- data: Buffer.from(asset.text),
63
- contentType: asset.contentType,
64
- name: asset.name,
65
- });
66
- }
67
- finish() {
68
- this.eventQueue.close();
69
- }
70
- }
71
- exports.FlowRun = FlowRun;
@@ -1,28 +0,0 @@
1
- import { DefaultRun } from "../../core/DefaultRun.js";
2
- import { FunctionEvent } from "../../core/FunctionEvent.js";
3
- import { AsyncQueue } from "../../util/AsyncQueue.js";
4
- import { Asset, AssetStorage } from "./AssetStorage.js";
5
- import { Logger } from "./Logger.js";
6
- import { PathProvider } from "./PathProvider.js";
7
- export declare class FlowRun<EVENT> extends DefaultRun {
8
- readonly eventQueue: AsyncQueue<EVENT>;
9
- private readonly assetStorage;
10
- private readonly logger;
11
- private readonly paths;
12
- constructor({ paths, assetStorage, logger, }: {
13
- paths: PathProvider;
14
- assetStorage: AssetStorage;
15
- logger: Logger;
16
- });
17
- readonly functionObserver: {
18
- onFunctionEvent: (event: FunctionEvent) => Promise<void>;
19
- };
20
- publishEvent(event: EVENT): void;
21
- storeBinaryAsset(asset: Asset): Promise<string>;
22
- storeTextAsset(asset: {
23
- text: string;
24
- contentType: string;
25
- name: string;
26
- }): Promise<string>;
27
- finish(): void;
28
- }
@@ -1,67 +0,0 @@
1
- import { DefaultRun } from "../../core/DefaultRun.js";
2
- import { AsyncQueue } from "../../util/AsyncQueue.js";
3
- export class FlowRun extends DefaultRun {
4
- constructor({ paths, assetStorage, logger, }) {
5
- super();
6
- Object.defineProperty(this, "eventQueue", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: new AsyncQueue()
11
- });
12
- Object.defineProperty(this, "assetStorage", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: void 0
17
- });
18
- Object.defineProperty(this, "logger", {
19
- enumerable: true,
20
- configurable: true,
21
- writable: true,
22
- value: void 0
23
- });
24
- Object.defineProperty(this, "paths", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
- Object.defineProperty(this, "functionObserver", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: {
35
- onFunctionEvent: async (event) => {
36
- this.logger.logFunctionEvent({
37
- run: this,
38
- event,
39
- });
40
- },
41
- }
42
- });
43
- this.paths = paths;
44
- this.assetStorage = assetStorage;
45
- this.logger = logger;
46
- }
47
- publishEvent(event) {
48
- this.eventQueue.push(event);
49
- }
50
- async storeBinaryAsset(asset) {
51
- await this.assetStorage.storeAsset({
52
- run: this,
53
- asset,
54
- });
55
- return this.paths.getAssetUrl(this.runId, asset.name);
56
- }
57
- async storeTextAsset(asset) {
58
- return this.storeBinaryAsset({
59
- data: Buffer.from(asset.text),
60
- contentType: asset.contentType,
61
- name: asset.name,
62
- });
63
- }
64
- finish() {
65
- this.eventQueue.close();
66
- }
67
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +0,0 @@
1
- import { z } from "zod";
2
- export interface FlowSchema<INPUT, EVENT> {
3
- input: z.ZodType<INPUT>;
4
- events: z.ZodType<EVENT>;
5
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,13 +0,0 @@
1
- import { FunctionEvent } from "../../core/FunctionEvent.js";
2
- import { FlowRun } from "./FlowRun.js";
3
- export interface Logger {
4
- logFunctionEvent(options: {
5
- run: FlowRun<unknown>;
6
- event: FunctionEvent;
7
- }): Promise<void>;
8
- logError(options: {
9
- run: FlowRun<unknown>;
10
- message: string;
11
- error: unknown;
12
- }): Promise<void>;
13
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PathProvider = void 0;
4
- class PathProvider {
5
- constructor({ baseUrl, basePath }) {
6
- Object.defineProperty(this, "baseUrl", {
7
- enumerable: true,
8
- configurable: true,
9
- writable: true,
10
- value: void 0
11
- });
12
- Object.defineProperty(this, "basePath", {
13
- enumerable: true,
14
- configurable: true,
15
- writable: true,
16
- value: void 0
17
- });
18
- this.baseUrl = baseUrl;
19
- this.basePath = basePath;
20
- }
21
- getAssetUrl(runId, assetName) {
22
- return `${this.baseUrl}${this.basePath}/${runId}/assets/${assetName}`;
23
- }
24
- getAssetPathTemplate() {
25
- return `${this.basePath}/:runId/assets/:assetName`;
26
- }
27
- getEventsUrl(runId) {
28
- return `${this.baseUrl}${this.basePath}/${runId}/events`;
29
- }
30
- getEventsPathTemplate() {
31
- return `${this.basePath}/:runId/events`;
32
- }
33
- }
34
- exports.PathProvider = PathProvider;
@@ -1,12 +0,0 @@
1
- export declare class PathProvider {
2
- readonly baseUrl: string;
3
- readonly basePath: string;
4
- constructor({ baseUrl, basePath }: {
5
- baseUrl: string;
6
- basePath: string;
7
- });
8
- getAssetUrl(runId: string, assetName: string): string;
9
- getAssetPathTemplate(): string;
10
- getEventsUrl(runId: string): string;
11
- getEventsPathTemplate(): string;
12
- }
@@ -1,30 +0,0 @@
1
- export class PathProvider {
2
- constructor({ baseUrl, basePath }) {
3
- Object.defineProperty(this, "baseUrl", {
4
- enumerable: true,
5
- configurable: true,
6
- writable: true,
7
- value: void 0
8
- });
9
- Object.defineProperty(this, "basePath", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: void 0
14
- });
15
- this.baseUrl = baseUrl;
16
- this.basePath = basePath;
17
- }
18
- getAssetUrl(runId, assetName) {
19
- return `${this.baseUrl}${this.basePath}/${runId}/assets/${assetName}`;
20
- }
21
- getAssetPathTemplate() {
22
- return `${this.basePath}/:runId/assets/:assetName`;
23
- }
24
- getEventsUrl(runId) {
25
- return `${this.baseUrl}${this.basePath}/${runId}/events`;
26
- }
27
- getEventsPathTemplate() {
28
- return `${this.basePath}/:runId/events`;
29
- }
30
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./AssetStorage.cjs"), exports);
18
- __exportStar(require("./DefaultFlow.cjs"), exports);
19
- __exportStar(require("./FileSystemAssetStorage.cjs"), exports);
20
- __exportStar(require("./FileSystemLogger.cjs"), exports);
21
- __exportStar(require("./Flow.cjs"), exports);
22
- __exportStar(require("./FlowRun.cjs"), exports);
23
- __exportStar(require("./Logger.cjs"), exports);
24
- __exportStar(require("./modelFusionFlowPlugin.cjs"), exports);
@@ -1,8 +0,0 @@
1
- export * from "./AssetStorage.js";
2
- export * from "./DefaultFlow.js";
3
- export * from "./FileSystemAssetStorage.js";
4
- export * from "./FileSystemLogger.js";
5
- export * from "./Flow.js";
6
- export * from "./FlowRun.js";
7
- export * from "./Logger.js";
8
- export * from "./modelFusionFlowPlugin.js";
@@ -1,8 +0,0 @@
1
- export * from "./AssetStorage.js";
2
- export * from "./DefaultFlow.js";
3
- export * from "./FileSystemAssetStorage.js";
4
- export * from "./FileSystemLogger.js";
5
- export * from "./Flow.js";
6
- export * from "./FlowRun.js";
7
- export * from "./Logger.js";
8
- export * from "./modelFusionFlowPlugin.js";
@@ -1,103 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.modelFusionFastifyPlugin = void 0;
4
- const getRun_js_1 = require("../../core/getRun.cjs");
5
- const FlowRun_js_1 = require("./FlowRun.cjs");
6
- const PathProvider_js_1 = require("./PathProvider.cjs");
7
- const modelFusionFastifyPlugin = async (fastify, { flow, baseUrl, basePath, assetStorage, logger, }) => {
8
- const paths = new PathProvider_js_1.PathProvider({
9
- baseUrl,
10
- basePath,
11
- });
12
- const runs = {};
13
- fastify.post(paths.basePath, async (request) => {
14
- const run = new FlowRun_js_1.FlowRun({
15
- paths,
16
- assetStorage,
17
- logger,
18
- });
19
- runs[run.runId] = run;
20
- // body the request body is json, parse and validate it:
21
- const input = flow.schema.input.parse(request.body);
22
- // start longer-running process (no await):
23
- (0, getRun_js_1.withRun)(run, async () => {
24
- flow
25
- .process({
26
- input,
27
- run,
28
- })
29
- .catch((error) => {
30
- console.error("Failed to process flow", error);
31
- logger.logError({
32
- run,
33
- message: "Failed to process flow",
34
- error,
35
- });
36
- })
37
- .finally(async () => {
38
- run.finish();
39
- });
40
- });
41
- return {
42
- id: run.runId,
43
- url: paths.getEventsUrl(run.runId),
44
- };
45
- });
46
- fastify.get(paths.getAssetPathTemplate(), async (request, reply) => {
47
- const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
48
- const assetName = request.params.assetName; // eslint-disable-line @typescript-eslint/no-explicit-any
49
- const asset = await assetStorage.readAsset({
50
- run: runs[runId],
51
- assetName,
52
- });
53
- if (asset == null) {
54
- logger.logError({
55
- run: runs[runId],
56
- message: `Asset ${assetName} not found`,
57
- error: new Error(`Asset ${assetName} not found`),
58
- });
59
- reply.status(404);
60
- return { error: `Asset ${assetName} not found` };
61
- }
62
- const headers = {
63
- "Access-Control-Allow-Origin": "*",
64
- "Content-Length": asset.data.length,
65
- "Content-Type": asset.contentType,
66
- "Cache-Control": "no-cache",
67
- };
68
- reply.raw.writeHead(200, headers);
69
- reply.raw.write(asset.data);
70
- reply.raw.end();
71
- return;
72
- });
73
- fastify.get(paths.getEventsPathTemplate(), async (request, reply) => {
74
- const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
75
- const eventQueue = runs[runId]?.eventQueue;
76
- if (!eventQueue) {
77
- return {
78
- error: `No event queue found for run ID ${runId}`,
79
- };
80
- }
81
- const headers = {
82
- "Access-Control-Allow-Origin": "*",
83
- "Content-Type": "text/event-stream",
84
- Connection: "keep-alive",
85
- "Cache-Control": "no-cache",
86
- "Content-Encoding": "none",
87
- };
88
- reply.raw.writeHead(200, headers);
89
- const textEncoder = new TextEncoder();
90
- for await (const event of eventQueue) {
91
- if (reply.raw.destroyed) {
92
- break; // client disconnected
93
- }
94
- reply.raw.write(textEncoder.encode(`data: ${JSON.stringify(event)}\n\n`));
95
- }
96
- if (!reply.raw.destroyed) {
97
- reply.raw.write(textEncoder.encode(`data: [DONE]\n\n`));
98
- }
99
- reply.raw.end();
100
- return;
101
- });
102
- };
103
- exports.modelFusionFastifyPlugin = modelFusionFastifyPlugin;
@@ -1,12 +0,0 @@
1
- import type { FastifyPluginAsync } from "fastify";
2
- import type { AssetStorage } from "./AssetStorage.js";
3
- import { Flow } from "./Flow.js";
4
- import { Logger } from "./Logger.js";
5
- export interface ModelFusionFastifyPluginOptions {
6
- flow: Flow<any, any>;
7
- baseUrl: string;
8
- basePath: string;
9
- assetStorage: AssetStorage;
10
- logger: Logger;
11
- }
12
- export declare const modelFusionFastifyPlugin: FastifyPluginAsync<ModelFusionFastifyPluginOptions>;
@@ -1,99 +0,0 @@
1
- import { withRun } from "../../core/getRun.js";
2
- import { FlowRun } from "./FlowRun.js";
3
- import { PathProvider } from "./PathProvider.js";
4
- export const modelFusionFastifyPlugin = async (fastify, { flow, baseUrl, basePath, assetStorage, logger, }) => {
5
- const paths = new PathProvider({
6
- baseUrl,
7
- basePath,
8
- });
9
- const runs = {};
10
- fastify.post(paths.basePath, async (request) => {
11
- const run = new FlowRun({
12
- paths,
13
- assetStorage,
14
- logger,
15
- });
16
- runs[run.runId] = run;
17
- // body the request body is json, parse and validate it:
18
- const input = flow.schema.input.parse(request.body);
19
- // start longer-running process (no await):
20
- withRun(run, async () => {
21
- flow
22
- .process({
23
- input,
24
- run,
25
- })
26
- .catch((error) => {
27
- console.error("Failed to process flow", error);
28
- logger.logError({
29
- run,
30
- message: "Failed to process flow",
31
- error,
32
- });
33
- })
34
- .finally(async () => {
35
- run.finish();
36
- });
37
- });
38
- return {
39
- id: run.runId,
40
- url: paths.getEventsUrl(run.runId),
41
- };
42
- });
43
- fastify.get(paths.getAssetPathTemplate(), async (request, reply) => {
44
- const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
45
- const assetName = request.params.assetName; // eslint-disable-line @typescript-eslint/no-explicit-any
46
- const asset = await assetStorage.readAsset({
47
- run: runs[runId],
48
- assetName,
49
- });
50
- if (asset == null) {
51
- logger.logError({
52
- run: runs[runId],
53
- message: `Asset ${assetName} not found`,
54
- error: new Error(`Asset ${assetName} not found`),
55
- });
56
- reply.status(404);
57
- return { error: `Asset ${assetName} not found` };
58
- }
59
- const headers = {
60
- "Access-Control-Allow-Origin": "*",
61
- "Content-Length": asset.data.length,
62
- "Content-Type": asset.contentType,
63
- "Cache-Control": "no-cache",
64
- };
65
- reply.raw.writeHead(200, headers);
66
- reply.raw.write(asset.data);
67
- reply.raw.end();
68
- return;
69
- });
70
- fastify.get(paths.getEventsPathTemplate(), async (request, reply) => {
71
- const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
72
- const eventQueue = runs[runId]?.eventQueue;
73
- if (!eventQueue) {
74
- return {
75
- error: `No event queue found for run ID ${runId}`,
76
- };
77
- }
78
- const headers = {
79
- "Access-Control-Allow-Origin": "*",
80
- "Content-Type": "text/event-stream",
81
- Connection: "keep-alive",
82
- "Cache-Control": "no-cache",
83
- "Content-Encoding": "none",
84
- };
85
- reply.raw.writeHead(200, headers);
86
- const textEncoder = new TextEncoder();
87
- for await (const event of eventQueue) {
88
- if (reply.raw.destroyed) {
89
- break; // client disconnected
90
- }
91
- reply.raw.write(textEncoder.encode(`data: ${JSON.stringify(event)}\n\n`));
92
- }
93
- if (!reply.raw.destroyed) {
94
- reply.raw.write(textEncoder.encode(`data: [DONE]\n\n`));
95
- }
96
- reply.raw.end();
97
- return;
98
- });
99
- };