modelfusion 0.53.1 → 0.54.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 (98) hide show
  1. package/README.md +2 -2
  2. package/browser/convertAudioChunksToBase64.cjs +8 -0
  3. package/browser/convertAudioChunksToBase64.d.ts +4 -0
  4. package/browser/convertAudioChunksToBase64.js +4 -0
  5. package/browser/convertBlobToBase64.cjs +23 -0
  6. package/browser/convertBlobToBase64.d.ts +1 -0
  7. package/browser/convertBlobToBase64.js +19 -0
  8. package/{ui → browser}/index.cjs +5 -0
  9. package/browser/index.d.ts +6 -0
  10. package/browser/index.js +6 -0
  11. package/browser/invokeFlow.cjs +23 -0
  12. package/browser/invokeFlow.d.ts +8 -0
  13. package/browser/invokeFlow.js +19 -0
  14. package/{event-source → browser}/readEventSource.cjs +8 -3
  15. package/{event-source → browser}/readEventSource.d.ts +3 -1
  16. package/{event-source → browser}/readEventSource.js +8 -3
  17. package/{event-source → browser}/readEventSourceStream.cjs +1 -1
  18. package/{event-source → browser}/readEventSourceStream.js +1 -1
  19. package/core/FunctionOptions.d.ts +4 -3
  20. package/event-source/createEventSourceStream.cjs +7 -3
  21. package/event-source/createEventSourceStream.js +7 -3
  22. package/event-source/index.cjs +0 -2
  23. package/event-source/index.d.ts +0 -2
  24. package/event-source/index.js +0 -2
  25. package/index.cjs +0 -1
  26. package/index.d.ts +0 -1
  27. package/index.js +0 -1
  28. package/model-function/embed/embed.cjs +16 -0
  29. package/model-function/embed/embed.d.ts +16 -0
  30. package/model-function/embed/embed.js +16 -0
  31. package/model-function/generate-image/generateImage.cjs +12 -3
  32. package/model-function/generate-image/generateImage.d.ts +12 -3
  33. package/model-function/generate-image/generateImage.js +12 -3
  34. package/model-function/generate-speech/generateSpeech.cjs +16 -1
  35. package/model-function/generate-speech/generateSpeech.d.ts +16 -1
  36. package/model-function/generate-speech/generateSpeech.js +16 -1
  37. package/model-function/generate-speech/streamSpeech.cjs +22 -1
  38. package/model-function/generate-speech/streamSpeech.d.ts +22 -1
  39. package/model-function/generate-speech/streamSpeech.js +22 -1
  40. package/model-function/generate-structure/generateStructure.cjs +41 -0
  41. package/model-function/generate-structure/generateStructure.d.ts +41 -0
  42. package/model-function/generate-structure/generateStructure.js +41 -0
  43. package/model-function/generate-structure/generateStructureOrText.cjs +62 -0
  44. package/model-function/generate-structure/generateStructureOrText.d.ts +62 -0
  45. package/model-function/generate-structure/generateStructureOrText.js +62 -0
  46. package/model-function/generate-structure/streamStructure.cjs +72 -1
  47. package/model-function/generate-structure/streamStructure.d.ts +68 -1
  48. package/model-function/generate-structure/streamStructure.js +72 -1
  49. package/model-function/generate-text/generateText.cjs +14 -6
  50. package/model-function/generate-text/generateText.d.ts +14 -6
  51. package/model-function/generate-text/generateText.js +14 -6
  52. package/model-function/generate-text/streamText.cjs +25 -0
  53. package/model-function/generate-text/streamText.d.ts +25 -0
  54. package/model-function/generate-text/streamText.js +25 -0
  55. package/model-function/generate-transcription/generateTranscription.cjs +10 -5
  56. package/model-function/generate-transcription/generateTranscription.d.ts +10 -5
  57. package/model-function/generate-transcription/generateTranscription.js +10 -5
  58. package/model-function/tokenize-text/Tokenizer.d.ts +27 -3
  59. package/package.json +13 -4
  60. package/server/fastify/AssetStorage.cjs +2 -0
  61. package/server/fastify/AssetStorage.d.ts +17 -0
  62. package/server/fastify/AssetStorage.js +1 -0
  63. package/server/fastify/DefaultFlow.cjs +22 -0
  64. package/server/fastify/DefaultFlow.d.ts +16 -0
  65. package/server/fastify/DefaultFlow.js +18 -0
  66. package/server/fastify/FileSystemAssetStorage.cjs +60 -0
  67. package/server/fastify/FileSystemAssetStorage.d.ts +19 -0
  68. package/server/fastify/FileSystemAssetStorage.js +56 -0
  69. package/server/fastify/FileSystemLogger.cjs +48 -0
  70. package/server/fastify/FileSystemLogger.d.ts +18 -0
  71. package/server/fastify/FileSystemLogger.js +44 -0
  72. package/server/fastify/Flow.cjs +2 -0
  73. package/server/fastify/Flow.d.ts +9 -0
  74. package/server/fastify/Flow.js +1 -0
  75. package/server/fastify/FlowRun.cjs +71 -0
  76. package/server/fastify/FlowRun.d.ts +28 -0
  77. package/server/fastify/FlowRun.js +67 -0
  78. package/server/fastify/FlowSchema.cjs +2 -0
  79. package/server/fastify/FlowSchema.d.ts +5 -0
  80. package/server/fastify/FlowSchema.js +1 -0
  81. package/server/fastify/Logger.cjs +2 -0
  82. package/server/fastify/Logger.d.ts +13 -0
  83. package/server/fastify/Logger.js +1 -0
  84. package/server/fastify/PathProvider.cjs +34 -0
  85. package/server/fastify/PathProvider.d.ts +12 -0
  86. package/server/fastify/PathProvider.js +30 -0
  87. package/server/fastify/index.cjs +24 -0
  88. package/server/fastify/index.d.ts +8 -0
  89. package/server/fastify/index.js +8 -0
  90. package/server/fastify/modelFusionFlowPlugin.cjs +102 -0
  91. package/server/fastify/modelFusionFlowPlugin.d.ts +12 -0
  92. package/server/fastify/modelFusionFlowPlugin.js +98 -0
  93. package/ui/index.d.ts +0 -1
  94. package/ui/index.js +0 -1
  95. /package/{ui → browser}/MediaSourceAppender.cjs +0 -0
  96. /package/{ui → browser}/MediaSourceAppender.d.ts +0 -0
  97. /package/{ui → browser}/MediaSourceAppender.js +0 -0
  98. /package/{event-source → browser}/readEventSourceStream.d.ts +0 -0
@@ -0,0 +1,56 @@
1
+ import { promises as fs } from "node:fs";
2
+ import { join } from "node:path";
3
+ export class FileSystemAssetStorage {
4
+ constructor({ path, logger, }) {
5
+ Object.defineProperty(this, "path", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ Object.defineProperty(this, "logger", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ this.path = path;
18
+ this.logger = logger;
19
+ }
20
+ async storeAsset({ run, asset, }) {
21
+ try {
22
+ const assetPath = this.path(run);
23
+ await fs.mkdir(assetPath, { recursive: true });
24
+ await fs.writeFile(join(assetPath, asset.name), asset.data);
25
+ await fs.writeFile(join(assetPath, `${asset.name}.meta.json`), JSON.stringify({
26
+ name: asset.name,
27
+ contentType: asset.contentType,
28
+ }));
29
+ }
30
+ catch (error) {
31
+ this.logger.logError({
32
+ run,
33
+ message: `Failed to store asset ${asset.name}`,
34
+ error,
35
+ });
36
+ throw error;
37
+ }
38
+ }
39
+ async readAsset(options) {
40
+ try {
41
+ const assetPath = this.path(options.run);
42
+ const data = await fs.readFile(join(assetPath, options.assetName));
43
+ const meta = await fs.readFile(join(assetPath, `${options.assetName}.meta.json`));
44
+ const { name, contentType } = JSON.parse(meta.toString());
45
+ return { data, name, contentType };
46
+ }
47
+ catch (error) {
48
+ this.logger.logError({
49
+ run: options.run,
50
+ message: `Failed to read asset ${options.assetName}`,
51
+ error,
52
+ });
53
+ throw error;
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,48 @@
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(options) {
32
+ const timestamp = Date.now();
33
+ try {
34
+ const logPath = this.logPath(options.run);
35
+ return node_fs_1.promises.writeFile((0, node_path_1.join)(logPath, `${timestamp}-error.json`), JSON.stringify({
36
+ timestamp: new Date(timestamp).toISOString(),
37
+ runId: options.run.runId,
38
+ message: options.message,
39
+ error: options.error,
40
+ }));
41
+ }
42
+ catch (error) {
43
+ console.error(`Failed to write error log`);
44
+ console.error(error);
45
+ }
46
+ }
47
+ }
48
+ exports.FileSystemLogger = FileSystemLogger;
@@ -0,0 +1,18 @@
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(options: {
14
+ run: FlowRun<unknown>;
15
+ message: string;
16
+ error: unknown;
17
+ }): Promise<void>;
18
+ }
@@ -0,0 +1,44 @@
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(options) {
29
+ const timestamp = Date.now();
30
+ try {
31
+ const logPath = this.logPath(options.run);
32
+ return fs.writeFile(join(logPath, `${timestamp}-error.json`), JSON.stringify({
33
+ timestamp: new Date(timestamp).toISOString(),
34
+ runId: options.run.runId,
35
+ message: options.message,
36
+ error: options.error,
37
+ }));
38
+ }
39
+ catch (error) {
40
+ console.error(`Failed to write error log`);
41
+ console.error(error);
42
+ }
43
+ }
44
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
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
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,71 @@
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;
@@ -0,0 +1,28 @@
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
+ }
@@ -0,0 +1,67 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { z } from "zod";
2
+ export interface FlowSchema<INPUT, EVENT> {
3
+ input: z.ZodType<INPUT>;
4
+ events: z.ZodType<EVENT>;
5
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
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
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,34 @@
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;
@@ -0,0 +1,12 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ }
@@ -0,0 +1,24 @@
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);
@@ -0,0 +1,8 @@
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";
@@ -0,0 +1,8 @@
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";
@@ -0,0 +1,102 @@
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
+ logger.logError({
31
+ run,
32
+ message: "Failed to process flow",
33
+ error,
34
+ });
35
+ })
36
+ .finally(async () => {
37
+ run.finish();
38
+ });
39
+ });
40
+ return {
41
+ id: run.runId,
42
+ url: paths.getEventsUrl(run.runId),
43
+ };
44
+ });
45
+ fastify.get(paths.getAssetPathTemplate(), async (request, reply) => {
46
+ const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
47
+ const assetName = request.params.assetName; // eslint-disable-line @typescript-eslint/no-explicit-any
48
+ const asset = await assetStorage.readAsset({
49
+ run: runs[runId],
50
+ assetName,
51
+ });
52
+ if (asset == null) {
53
+ logger.logError({
54
+ run: runs[runId],
55
+ message: `Asset ${assetName} not found`,
56
+ error: new Error(`Asset ${assetName} not found`),
57
+ });
58
+ reply.status(404);
59
+ return { error: `Asset ${assetName} not found` };
60
+ }
61
+ const headers = {
62
+ "Access-Control-Allow-Origin": "*",
63
+ "Content-Length": asset.data.length,
64
+ "Content-Type": asset.contentType,
65
+ "Cache-Control": "no-cache",
66
+ };
67
+ reply.raw.writeHead(200, headers);
68
+ reply.raw.write(asset.data);
69
+ reply.raw.end();
70
+ return;
71
+ });
72
+ fastify.get(paths.getEventsPathTemplate(), async (request, reply) => {
73
+ const runId = request.params.runId; // eslint-disable-line @typescript-eslint/no-explicit-any
74
+ const eventQueue = runs[runId]?.eventQueue;
75
+ if (!eventQueue) {
76
+ return {
77
+ error: `No event queue found for run ID ${runId}`,
78
+ };
79
+ }
80
+ const headers = {
81
+ "Access-Control-Allow-Origin": "*",
82
+ "Content-Type": "text/event-stream",
83
+ Connection: "keep-alive",
84
+ "Cache-Control": "no-cache",
85
+ "Content-Encoding": "none",
86
+ };
87
+ reply.raw.writeHead(200, headers);
88
+ const textEncoder = new TextEncoder();
89
+ for await (const event of eventQueue) {
90
+ if (reply.raw.destroyed) {
91
+ break; // client disconnected
92
+ }
93
+ reply.raw.write(textEncoder.encode(`data: ${JSON.stringify(event)}\n\n`));
94
+ }
95
+ if (!reply.raw.destroyed) {
96
+ reply.raw.write(textEncoder.encode(`data: [DONE]\n\n`));
97
+ }
98
+ reply.raw.end();
99
+ return;
100
+ });
101
+ };
102
+ exports.modelFusionFastifyPlugin = modelFusionFastifyPlugin;
@@ -0,0 +1,12 @@
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>;