@motiadev/core 0.0.6 → 0.0.7-build.20250529215343
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/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/index.d.ts +13 -8
- package/dist/index.js +28 -8
- package/dist/src/call-step-file.d.ts +17 -2
- package/dist/src/call-step-file.js +88 -47
- package/dist/src/cron-handler.d.ts +13 -0
- package/dist/src/cron-handler.js +98 -0
- package/dist/src/event-manager.js +10 -5
- package/dist/src/flows-config-endpoint.d.ts +2 -0
- package/dist/src/flows-config-endpoint.js +51 -0
- package/dist/src/flows-endpoint.d.ts +11 -7
- package/dist/src/flows-endpoint.js +53 -22
- package/dist/src/generate-trace-id.d.ts +2 -0
- package/dist/src/generate-trace-id.js +14 -0
- package/dist/src/get-step-config.d.ts +4 -0
- package/dist/src/get-step-config.js +88 -0
- package/dist/src/guards.d.ts +3 -2
- package/dist/src/guards.js +3 -1
- package/dist/src/locked-data.d.ts +60 -0
- package/dist/src/locked-data.js +278 -0
- package/dist/src/logger-factory.d.ts +15 -0
- package/dist/src/logger-factory.js +14 -0
- package/dist/src/logger.d.ts +21 -15
- package/dist/src/logger.js +49 -29
- package/dist/src/mermaid-generator.d.ts +4 -0
- package/dist/src/mermaid-generator.js +203 -0
- package/dist/src/node/get-config.d.ts +1 -2
- package/dist/src/node/get-config.js +51 -8
- package/dist/src/node/logger.d.ts +10 -6
- package/dist/src/node/logger.js +27 -16
- package/dist/src/node/middleware-compose.d.ts +1 -0
- package/dist/src/node/middleware-compose.js +12 -0
- package/dist/src/node/node-runner.js +28 -9
- package/dist/src/node/rpc-state-manager.d.ts +3 -2
- package/dist/src/node/rpc-state-manager.js +5 -2
- package/dist/src/node/rpc.d.ts +3 -1
- package/dist/src/node/rpc.js +21 -3
- package/dist/src/pretty-print.d.ts +1 -0
- package/dist/src/pretty-print.js +66 -0
- package/dist/src/printer.d.ts +45 -0
- package/dist/src/printer.js +121 -0
- package/dist/src/process-communication/communication-config.d.ts +7 -0
- package/dist/src/process-communication/communication-config.js +12 -0
- package/dist/src/process-communication/process-manager.d.ts +30 -0
- package/dist/src/process-communication/process-manager.js +88 -0
- package/dist/src/process-communication/rpc-processor-interface.d.ts +9 -0
- package/dist/src/process-communication/rpc-processor-interface.js +2 -0
- package/dist/src/python/communication_factory.py +35 -0
- package/dist/src/python/context.py +21 -0
- package/dist/src/python/get-config.py +31 -13
- package/dist/src/python/ipc_communication.py +145 -0
- package/dist/src/python/logger.py +4 -6
- package/dist/src/python/middleware.py +17 -0
- package/dist/src/python/python-runner.py +105 -39
- package/dist/src/python/rpc.py +33 -68
- package/dist/src/python/rpc_communication.py +125 -0
- package/dist/src/python/rpc_state_manager.py +49 -11
- package/dist/src/python/type_definitions.py +34 -0
- package/dist/src/python/validation.py +56 -0
- package/dist/src/ruby/get-config.rb +61 -0
- package/dist/src/ruby/logger.rb +21 -16
- package/dist/src/ruby/rpc.rb +118 -0
- package/dist/src/ruby/rpc_state_manager.rb +41 -0
- package/dist/src/ruby/ruby-runner.rb +94 -0
- package/dist/src/server.d.ts +14 -12
- package/dist/src/server.js +149 -28
- package/dist/src/socket-server.d.ts +39 -0
- package/dist/src/socket-server.js +76 -0
- package/dist/src/state/adapters/default-state-adapter.d.ts +7 -3
- package/dist/src/state/adapters/default-state-adapter.js +37 -14
- package/dist/src/state/adapters/memory-state-adapter.d.ts +14 -0
- package/dist/src/state/adapters/memory-state-adapter.js +58 -0
- package/dist/src/state/create-state-adapter.d.ts +5 -6
- package/dist/src/state/create-state-adapter.js +2 -10
- package/dist/src/state/state-adapter.d.ts +4 -4
- package/dist/src/state-stream.d.ts +19 -0
- package/dist/src/state-stream.js +28 -0
- package/dist/src/step-handler-rpc-processor.d.ts +13 -4
- package/dist/src/step-handler-rpc-processor.js +33 -3
- package/dist/src/step-handler-rpc-stdin-processor.d.ts +22 -0
- package/dist/src/step-handler-rpc-stdin-processor.js +84 -0
- package/dist/src/step-handlers.d.ts +7 -2
- package/dist/src/step-handlers.js +47 -19
- package/dist/src/step-validator.d.ts +14 -0
- package/dist/src/step-validator.js +138 -0
- package/dist/src/steps/emit.step.d.ts +11 -0
- package/dist/src/steps/emit.step.js +27 -0
- package/dist/src/steps/emit.step.ts +34 -0
- package/dist/src/steps/index.d.ts +2 -0
- package/dist/src/steps/index.js +16 -0
- package/dist/src/streams/api-endpoints.d.ts +2 -0
- package/dist/src/streams/api-endpoints.js +71 -0
- package/dist/src/streams/flows-stream.d.ts +14 -0
- package/dist/src/streams/flows-stream.js +29 -0
- package/dist/src/streams/logs-stream.d.ts +16 -0
- package/dist/src/streams/logs-stream.js +22 -0
- package/dist/src/types/generate-type-from-schema.d.ts +2 -0
- package/dist/src/types/generate-type-from-schema.js +28 -0
- package/dist/src/types/generate-types-from-response.d.ts +2 -0
- package/dist/src/types/generate-types-from-response.js +13 -0
- package/dist/src/types/generate-types.d.ts +12 -0
- package/dist/src/types/generate-types.js +108 -0
- package/dist/src/types/merge-schemas.d.ts +3 -0
- package/dist/src/types/merge-schemas.js +59 -0
- package/dist/src/types/schema.types.d.ts +20 -0
- package/dist/src/types/schema.types.js +10 -0
- package/dist/src/types-stream.d.ts +34 -0
- package/dist/src/types-stream.js +2 -0
- package/dist/src/types.d.ts +93 -47
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.js +12 -0
- package/package.json +20 -9
- package/requirements.txt +1 -0
- package/dist/src/node/get-module-export.d.ts +0 -1
- package/dist/src/node/get-module-export.js +0 -63
- package/dist/src/python/get-python-config.d.ts +0 -2
- package/dist/src/python/get-python-config.js +0 -34
- package/dist/src/ruby/get_config.rb +0 -78
- package/dist/src/ruby/ruby_runner.rb +0 -80
- package/dist/src/ruby/state_adapter.rb +0 -62
- package/dist/src/state/adapters/redis-state-adapter.d.ts +0 -19
- package/dist/src/state/adapters/redis-state-adapter.js +0 -44
- package/tsconfig.json +0 -20
package/dist/src/server.js
CHANGED
|
@@ -4,37 +4,130 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createServer = void 0;
|
|
7
|
+
const cron_handler_1 = require("./cron-handler");
|
|
7
8
|
const body_parser_1 = __importDefault(require("body-parser"));
|
|
8
|
-
const crypto_1 = require("crypto");
|
|
9
9
|
const express_1 = __importDefault(require("express"));
|
|
10
10
|
const http_1 = __importDefault(require("http"));
|
|
11
|
-
const
|
|
11
|
+
const cors_1 = __importDefault(require("cors"));
|
|
12
12
|
const flows_endpoint_1 = require("./flows-endpoint");
|
|
13
13
|
const guards_1 = require("./guards");
|
|
14
14
|
const logger_1 = require("./logger");
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
15
|
+
const steps_1 = require("./steps");
|
|
16
|
+
const call_step_file_1 = require("./call-step-file");
|
|
17
|
+
const logger_factory_1 = require("./logger-factory");
|
|
18
|
+
const generate_trace_id_1 = require("./generate-trace-id");
|
|
19
|
+
const flows_config_endpoint_1 = require("./flows-config-endpoint");
|
|
20
|
+
const api_endpoints_1 = require("./streams/api-endpoints");
|
|
21
|
+
const socket_server_1 = require("./socket-server");
|
|
22
|
+
const logs_stream_1 = require("./streams/logs-stream");
|
|
23
|
+
const createServer = async (lockedData, eventManager, state, config) => {
|
|
24
|
+
const printer = lockedData.printer;
|
|
18
25
|
const app = (0, express_1.default)();
|
|
19
26
|
const server = http_1.default.createServer(app);
|
|
20
|
-
const
|
|
21
|
-
|
|
27
|
+
const { pushEvent, socketServer } = (0, socket_server_1.createSocketServer)({
|
|
28
|
+
server,
|
|
29
|
+
onJoin: async (streamName, groupId, id) => {
|
|
30
|
+
const streams = lockedData.getStreams();
|
|
31
|
+
const stream = streams[streamName];
|
|
32
|
+
if (stream) {
|
|
33
|
+
const result = await stream().get(groupId, id);
|
|
34
|
+
delete result.__motia; // deleting because we don't need it in the socket
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
onJoinGroup: async (streamName, groupId) => {
|
|
39
|
+
const streams = lockedData.getStreams();
|
|
40
|
+
const stream = streams[streamName];
|
|
41
|
+
if (stream) {
|
|
42
|
+
const result = stream ? await stream().getGroup(groupId) : [];
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
44
|
+
return result.map(({ __motia, ...rest }) => rest);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
lockedData.applyStreamWrapper(state, (streamName, stream) => {
|
|
49
|
+
return () => {
|
|
50
|
+
const suuper = stream();
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
const wrapObject = (id, object) => ({
|
|
53
|
+
...object,
|
|
54
|
+
__motia: { type: 'state-stream', streamName, id },
|
|
55
|
+
});
|
|
56
|
+
const wrapper = {
|
|
57
|
+
...suuper,
|
|
58
|
+
async send(channel, event) {
|
|
59
|
+
pushEvent({ streamName, ...channel, event: { type: 'event', event } });
|
|
60
|
+
},
|
|
61
|
+
async get(groupId, id) {
|
|
62
|
+
const result = await suuper.get.apply(wrapper, [groupId, id]);
|
|
63
|
+
return wrapObject(id, result);
|
|
64
|
+
},
|
|
65
|
+
async set(groupId, id, data) {
|
|
66
|
+
if (!data) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const exists = await suuper.get(groupId, id);
|
|
70
|
+
const updated = await suuper.set.apply(wrapper, [groupId, id, data]);
|
|
71
|
+
const result = updated ?? data;
|
|
72
|
+
const wrappedResult = wrapObject(id, result);
|
|
73
|
+
const type = exists ? 'update' : 'create';
|
|
74
|
+
pushEvent({ streamName, groupId, id, event: { type, data: result } });
|
|
75
|
+
return wrappedResult;
|
|
76
|
+
},
|
|
77
|
+
async delete(groupId, id) {
|
|
78
|
+
const result = await suuper.delete.apply(wrapper, [groupId, id]);
|
|
79
|
+
pushEvent({ streamName, groupId, id, event: { type: 'delete', data: result } });
|
|
80
|
+
return wrapObject(id, result);
|
|
81
|
+
},
|
|
82
|
+
async getGroup(groupId) {
|
|
83
|
+
const list = await suuper.getGroup.apply(wrapper, [groupId]);
|
|
84
|
+
return list.map((object) => wrapObject(object.id, object));
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return wrapper;
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
const logStream = lockedData.createStream({
|
|
91
|
+
filePath: '__motia.logs',
|
|
92
|
+
hidden: true,
|
|
93
|
+
config: {
|
|
94
|
+
name: '__motia.logs',
|
|
95
|
+
baseConfig: { storageType: 'custom', factory: () => new logs_stream_1.LogsStream() },
|
|
96
|
+
schema: null,
|
|
97
|
+
},
|
|
98
|
+
})();
|
|
99
|
+
const allSteps = [...steps_1.systemSteps, ...lockedData.activeSteps];
|
|
100
|
+
const loggerFactory = new logger_factory_1.LoggerFactory(config.isVerbose, logStream);
|
|
101
|
+
const cronManager = (0, cron_handler_1.setupCronHandlers)(lockedData, eventManager, state, loggerFactory);
|
|
102
|
+
const asyncHandler = (step) => {
|
|
22
103
|
return async (req, res) => {
|
|
23
|
-
const traceId = (0,
|
|
24
|
-
const
|
|
25
|
-
logger.
|
|
26
|
-
|
|
104
|
+
const traceId = (0, generate_trace_id_1.generateTraceId)();
|
|
105
|
+
const { name: stepName, flows } = step.config;
|
|
106
|
+
const logger = loggerFactory.create({ traceId, flows, stepName });
|
|
107
|
+
logger.debug('[API] Received request, processing step', { path: req.path });
|
|
27
108
|
const request = {
|
|
28
109
|
body: req.body,
|
|
29
110
|
headers: req.headers,
|
|
30
111
|
pathParams: req.params,
|
|
31
112
|
queryParams: req.query,
|
|
32
113
|
};
|
|
33
|
-
const emit = async ({ data, type }) => {
|
|
34
|
-
await eventManager.emit({ data, type, traceId, flows, logger }, step.filePath);
|
|
35
|
-
};
|
|
36
114
|
try {
|
|
37
|
-
const
|
|
115
|
+
const data = request;
|
|
116
|
+
const result = await (0, call_step_file_1.callStepFile)({
|
|
117
|
+
contextInFirstArg: false,
|
|
118
|
+
lockedData,
|
|
119
|
+
data,
|
|
120
|
+
step,
|
|
121
|
+
printer,
|
|
122
|
+
logger,
|
|
123
|
+
eventManager,
|
|
124
|
+
state,
|
|
125
|
+
traceId,
|
|
126
|
+
});
|
|
127
|
+
if (!result) {
|
|
128
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
38
131
|
if (result.headers) {
|
|
39
132
|
Object.entries(result.headers).forEach(([key, value]) => res.setHeader(key, value));
|
|
40
133
|
}
|
|
@@ -50,24 +143,52 @@ const createServer = async (options) => {
|
|
|
50
143
|
};
|
|
51
144
|
app.use(body_parser_1.default.json());
|
|
52
145
|
app.use(body_parser_1.default.urlencoded({ extended: true }));
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
const { method,
|
|
146
|
+
const router = express_1.default.Router();
|
|
147
|
+
const addRoute = (step) => {
|
|
148
|
+
const { method, path } = step.config;
|
|
56
149
|
logger_1.globalLogger.debug('[API] Registering route', step.config);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
150
|
+
const handler = asyncHandler(step);
|
|
151
|
+
const methods = {
|
|
152
|
+
GET: () => router.get(path, handler),
|
|
153
|
+
POST: () => router.post(path, handler),
|
|
154
|
+
PUT: () => router.put(path, handler),
|
|
155
|
+
DELETE: () => router.delete(path, handler),
|
|
156
|
+
PATCH: () => router.patch(path, handler),
|
|
157
|
+
OPTIONS: () => router.options(path, handler),
|
|
158
|
+
HEAD: () => router.head(path, handler),
|
|
159
|
+
};
|
|
160
|
+
const methodHandler = methods[method];
|
|
161
|
+
if (!methodHandler) {
|
|
64
162
|
throw new Error(`Unsupported method: ${method}`);
|
|
65
163
|
}
|
|
66
|
-
|
|
67
|
-
|
|
164
|
+
methodHandler();
|
|
165
|
+
};
|
|
166
|
+
const removeRoute = (step) => {
|
|
167
|
+
const { path, method } = step.config;
|
|
168
|
+
const routerStack = router.stack;
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
170
|
+
const filteredStack = routerStack.filter((layer) => {
|
|
171
|
+
if (layer.route) {
|
|
172
|
+
const match = layer.route.path === path && layer.route.methods[method.toLowerCase()];
|
|
173
|
+
return !match;
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
});
|
|
177
|
+
router.stack = filteredStack;
|
|
178
|
+
};
|
|
179
|
+
allSteps.filter(guards_1.isApiStep).forEach(addRoute);
|
|
180
|
+
app.use((0, cors_1.default)());
|
|
181
|
+
app.use(router);
|
|
182
|
+
(0, api_endpoints_1.apiEndpoints)(lockedData);
|
|
183
|
+
(0, flows_endpoint_1.flowsEndpoint)(lockedData, app);
|
|
184
|
+
(0, flows_config_endpoint_1.flowsConfigEndpoint)(app, process.cwd());
|
|
68
185
|
server.on('error', (error) => {
|
|
69
186
|
console.error('Server error:', error);
|
|
70
187
|
});
|
|
71
|
-
|
|
188
|
+
const close = async () => {
|
|
189
|
+
cronManager.close();
|
|
190
|
+
socketServer.close();
|
|
191
|
+
};
|
|
192
|
+
return { app, server, socketServer, close, removeRoute, addRoute, cronManager };
|
|
72
193
|
};
|
|
73
194
|
exports.createServer = createServer;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Server as WsServer } from 'ws';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
type BaseMessage = {
|
|
4
|
+
streamName: string;
|
|
5
|
+
groupId: string;
|
|
6
|
+
id?: string;
|
|
7
|
+
};
|
|
8
|
+
type StreamEvent<TData> = {
|
|
9
|
+
type: 'sync';
|
|
10
|
+
data: TData;
|
|
11
|
+
} | {
|
|
12
|
+
type: 'create';
|
|
13
|
+
data: TData;
|
|
14
|
+
} | {
|
|
15
|
+
type: 'update';
|
|
16
|
+
data: TData;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'delete';
|
|
19
|
+
data: TData;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'event';
|
|
22
|
+
event: {
|
|
23
|
+
type: string;
|
|
24
|
+
data: any;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
type EventMessage<TData> = BaseMessage & {
|
|
28
|
+
event: StreamEvent<TData>;
|
|
29
|
+
};
|
|
30
|
+
type Props = {
|
|
31
|
+
server: http.Server;
|
|
32
|
+
onJoin: <TData>(streamName: string, groupId: string, id: string) => Promise<TData>;
|
|
33
|
+
onJoinGroup: <TData>(streamName: string, groupId: string) => Promise<TData[] | undefined>;
|
|
34
|
+
};
|
|
35
|
+
export declare const createSocketServer: ({ server, onJoin, onJoinGroup }: Props) => {
|
|
36
|
+
pushEvent: <TData>(message: EventMessage<TData>) => void;
|
|
37
|
+
socketServer: WsServer<typeof import("ws"), typeof http.IncomingMessage>;
|
|
38
|
+
};
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSocketServer = void 0;
|
|
4
|
+
const ws_1 = require("ws");
|
|
5
|
+
const createSocketServer = ({ server, onJoin, onJoinGroup }) => {
|
|
6
|
+
const socketServer = new ws_1.Server({ server });
|
|
7
|
+
const rooms = {};
|
|
8
|
+
const subscriptions = new Map();
|
|
9
|
+
const getRoom = (message) => {
|
|
10
|
+
return message.id ? `${message.streamName}:id:${message.id}` : `${message.streamName}:group-id:${message.groupId}`;
|
|
11
|
+
};
|
|
12
|
+
socketServer.on('connection', (socket) => {
|
|
13
|
+
subscriptions.set(socket, new Set());
|
|
14
|
+
socket.on('message', async (payload) => {
|
|
15
|
+
const message = JSON.parse(payload.toString());
|
|
16
|
+
if (message.type === 'join') {
|
|
17
|
+
const room = getRoom(message.data);
|
|
18
|
+
if (!rooms[room]) {
|
|
19
|
+
rooms[room] = new Map();
|
|
20
|
+
}
|
|
21
|
+
if (message.data.id) {
|
|
22
|
+
const item = await onJoin(message.data.streamName, message.data.groupId, message.data.id);
|
|
23
|
+
if (item) {
|
|
24
|
+
const resultMessage = {
|
|
25
|
+
streamName: message.data.streamName,
|
|
26
|
+
groupId: message.data.groupId,
|
|
27
|
+
id: message.data.id,
|
|
28
|
+
event: { type: 'sync', data: item },
|
|
29
|
+
};
|
|
30
|
+
socket.send(JSON.stringify(resultMessage));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const items = await onJoinGroup(message.data.streamName, message.data.groupId);
|
|
35
|
+
if (items) {
|
|
36
|
+
const resultMessage = {
|
|
37
|
+
streamName: message.data.streamName,
|
|
38
|
+
groupId: message.data.groupId,
|
|
39
|
+
event: { type: 'sync', data: items },
|
|
40
|
+
};
|
|
41
|
+
socket.send(JSON.stringify(resultMessage));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
rooms[room].set(message.data.subscriptionId, socket);
|
|
45
|
+
subscriptions.get(socket)?.add([room, message.data.subscriptionId]);
|
|
46
|
+
}
|
|
47
|
+
else if (message.type === 'leave') {
|
|
48
|
+
const room = getRoom(message.data);
|
|
49
|
+
if (rooms[room]) {
|
|
50
|
+
rooms[room].delete(message.data.subscriptionId);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
socket.on('close', () => {
|
|
55
|
+
subscriptions.get(socket)?.forEach(([room, subscriptionId]) => {
|
|
56
|
+
rooms[room]?.delete(subscriptionId);
|
|
57
|
+
});
|
|
58
|
+
subscriptions.delete(socket);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
const pushEvent = (message) => {
|
|
62
|
+
const { groupId, streamName, id } = message;
|
|
63
|
+
const groupRoom = getRoom({ streamName, groupId });
|
|
64
|
+
if (rooms[groupRoom]) {
|
|
65
|
+
rooms[groupRoom].forEach((socket) => socket.send(JSON.stringify(message)));
|
|
66
|
+
}
|
|
67
|
+
if (id) {
|
|
68
|
+
const itemRoom = getRoom({ groupId, streamName, id });
|
|
69
|
+
if (rooms[itemRoom]) {
|
|
70
|
+
rooms[itemRoom].forEach((socket) => socket.send(JSON.stringify(message)));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return { pushEvent, socketServer };
|
|
75
|
+
};
|
|
76
|
+
exports.createSocketServer = createSocketServer;
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { StateAdapter } from '../state-adapter';
|
|
2
2
|
export type FileAdapterConfig = {
|
|
3
|
+
adapter: 'default';
|
|
3
4
|
filePath: string;
|
|
4
5
|
};
|
|
5
6
|
export declare class FileStateAdapter implements StateAdapter {
|
|
6
7
|
private filePath;
|
|
7
8
|
constructor(config: FileAdapterConfig);
|
|
8
9
|
init(): Promise<void>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
getGroup<T>(groupId: string): Promise<T[]>;
|
|
11
|
+
get<T>(traceId: string, key: string): Promise<T | null>;
|
|
12
|
+
set<T>(traceId: string, key: string, value: T): Promise<T>;
|
|
13
|
+
delete<T>(traceId: string, key: string): Promise<T | null>;
|
|
12
14
|
clear(traceId: string): Promise<void>;
|
|
15
|
+
keys(traceId: string): Promise<string[]>;
|
|
16
|
+
traceIds(): Promise<string[]>;
|
|
13
17
|
cleanup(): Promise<void>;
|
|
14
18
|
private _makeKey;
|
|
15
19
|
private _readFile;
|
|
@@ -41,49 +41,72 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
41
41
|
const path = __importStar(require("path"));
|
|
42
42
|
class FileStateAdapter {
|
|
43
43
|
constructor(config) {
|
|
44
|
-
this.filePath = path.join(config.filePath, 'motia
|
|
44
|
+
this.filePath = path.join(config.filePath, 'motia.state.json');
|
|
45
45
|
}
|
|
46
46
|
async init() {
|
|
47
|
-
const dir = this.filePath.replace('motia
|
|
47
|
+
const dir = this.filePath.replace('motia.state.json', '');
|
|
48
48
|
try {
|
|
49
49
|
fs_1.default.realpathSync(dir);
|
|
50
50
|
}
|
|
51
|
-
catch
|
|
51
|
+
catch {
|
|
52
52
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
53
53
|
}
|
|
54
54
|
try {
|
|
55
55
|
fs_1.default.readFileSync(this.filePath, 'utf-8');
|
|
56
56
|
}
|
|
57
|
-
catch
|
|
57
|
+
catch {
|
|
58
58
|
fs_1.default.writeFileSync(this.filePath, JSON.stringify({}), 'utf-8');
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
async getGroup(groupId) {
|
|
62
|
+
const data = this._readFile();
|
|
63
|
+
return Object.entries(data)
|
|
64
|
+
.filter(([key]) => key.startsWith(groupId))
|
|
65
|
+
.map(([, value]) => JSON.parse(value));
|
|
66
|
+
}
|
|
61
67
|
async get(traceId, key) {
|
|
62
|
-
const data =
|
|
68
|
+
const data = this._readFile();
|
|
63
69
|
const fullKey = this._makeKey(traceId, key);
|
|
64
70
|
return data[fullKey] ? JSON.parse(data[fullKey]) : null;
|
|
65
71
|
}
|
|
66
72
|
async set(traceId, key, value) {
|
|
67
|
-
const data =
|
|
73
|
+
const data = this._readFile();
|
|
68
74
|
const fullKey = this._makeKey(traceId, key);
|
|
69
75
|
data[fullKey] = JSON.stringify(value);
|
|
70
|
-
|
|
76
|
+
this._writeFile(data);
|
|
77
|
+
return value;
|
|
71
78
|
}
|
|
72
79
|
async delete(traceId, key) {
|
|
73
|
-
const data =
|
|
80
|
+
const data = this._readFile();
|
|
74
81
|
const fullKey = this._makeKey(traceId, key);
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
const value = await this.get(traceId, key);
|
|
83
|
+
if (value) {
|
|
84
|
+
delete data[fullKey];
|
|
85
|
+
this._writeFile(data);
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
77
88
|
}
|
|
78
89
|
async clear(traceId) {
|
|
79
|
-
const data =
|
|
90
|
+
const data = this._readFile();
|
|
80
91
|
const pattern = this._makeKey(traceId, '');
|
|
81
92
|
for (const key in data) {
|
|
82
93
|
if (key.startsWith(pattern)) {
|
|
83
94
|
delete data[key];
|
|
84
95
|
}
|
|
85
96
|
}
|
|
86
|
-
|
|
97
|
+
this._writeFile(data);
|
|
98
|
+
}
|
|
99
|
+
async keys(traceId) {
|
|
100
|
+
const data = this._readFile();
|
|
101
|
+
return Object.keys(data)
|
|
102
|
+
.filter((key) => key.startsWith(this._makeKey(traceId, '')))
|
|
103
|
+
.map((key) => key.replace(this._makeKey(traceId, ''), ''));
|
|
104
|
+
}
|
|
105
|
+
async traceIds() {
|
|
106
|
+
const data = this._readFile();
|
|
107
|
+
const traceIds = new Set();
|
|
108
|
+
Object.keys(data).forEach((key) => traceIds.add(key.split(':')[0]));
|
|
109
|
+
return Array.from(traceIds);
|
|
87
110
|
}
|
|
88
111
|
async cleanup() {
|
|
89
112
|
// No cleanup needed for file system
|
|
@@ -91,11 +114,11 @@ class FileStateAdapter {
|
|
|
91
114
|
_makeKey(traceId, key) {
|
|
92
115
|
return `${traceId}:${key}`;
|
|
93
116
|
}
|
|
94
|
-
|
|
117
|
+
_readFile() {
|
|
95
118
|
const content = fs_1.default.readFileSync(this.filePath, 'utf-8');
|
|
96
119
|
return JSON.parse(content);
|
|
97
120
|
}
|
|
98
|
-
|
|
121
|
+
_writeFile(data) {
|
|
99
122
|
fs_1.default.writeFileSync(this.filePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
100
123
|
}
|
|
101
124
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StateAdapter } from '../state-adapter';
|
|
2
|
+
export declare class MemoryStateAdapter implements StateAdapter {
|
|
3
|
+
private state;
|
|
4
|
+
init(): Promise<void>;
|
|
5
|
+
getGroup<T>(groupId: string): Promise<T[]>;
|
|
6
|
+
get<T>(traceId: string, key: string): Promise<T | null>;
|
|
7
|
+
set<T>(traceId: string, key: string, value: T): Promise<T>;
|
|
8
|
+
delete<T>(traceId: string, key: string): Promise<T | null>;
|
|
9
|
+
clear(traceId: string): Promise<void>;
|
|
10
|
+
keys(traceId: string): Promise<string[]>;
|
|
11
|
+
traceIds(): Promise<string[]>;
|
|
12
|
+
cleanup(): Promise<void>;
|
|
13
|
+
private _makeKey;
|
|
14
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryStateAdapter = void 0;
|
|
4
|
+
class MemoryStateAdapter {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.state = {};
|
|
7
|
+
}
|
|
8
|
+
async init() {
|
|
9
|
+
this.state = {};
|
|
10
|
+
}
|
|
11
|
+
async getGroup(groupId) {
|
|
12
|
+
return Object.entries(this.state)
|
|
13
|
+
.filter(([key]) => key.startsWith(groupId))
|
|
14
|
+
.map(([, value]) => value);
|
|
15
|
+
}
|
|
16
|
+
async get(traceId, key) {
|
|
17
|
+
const fullKey = this._makeKey(traceId, key);
|
|
18
|
+
return this.state[fullKey] ? this.state[fullKey] : null;
|
|
19
|
+
}
|
|
20
|
+
async set(traceId, key, value) {
|
|
21
|
+
const fullKey = this._makeKey(traceId, key);
|
|
22
|
+
this.state[fullKey] = value;
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
async delete(traceId, key) {
|
|
26
|
+
const fullKey = this._makeKey(traceId, key);
|
|
27
|
+
const value = await this.get(traceId, key);
|
|
28
|
+
if (value) {
|
|
29
|
+
delete this.state[fullKey];
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
async clear(traceId) {
|
|
34
|
+
const pattern = this._makeKey(traceId, '');
|
|
35
|
+
for (const key in this.state) {
|
|
36
|
+
if (key.startsWith(pattern)) {
|
|
37
|
+
delete this.state[key];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async keys(traceId) {
|
|
42
|
+
return Object.keys(this.state)
|
|
43
|
+
.filter((key) => key.startsWith(this._makeKey(traceId, '')))
|
|
44
|
+
.map((key) => key.replace(this._makeKey(traceId, ''), ''));
|
|
45
|
+
}
|
|
46
|
+
async traceIds() {
|
|
47
|
+
const traceIds = new Set();
|
|
48
|
+
Object.keys(this.state).forEach((key) => traceIds.add(key.split(':')[0]));
|
|
49
|
+
return Array.from(traceIds);
|
|
50
|
+
}
|
|
51
|
+
async cleanup() {
|
|
52
|
+
// No cleanup needed for file system
|
|
53
|
+
}
|
|
54
|
+
_makeKey(traceId, key) {
|
|
55
|
+
return `${traceId}:${key}`;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.MemoryStateAdapter = MemoryStateAdapter;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { FileStateAdapter } from './adapters/default-state-adapter';
|
|
2
|
-
import {
|
|
3
|
-
type
|
|
4
|
-
adapter: '
|
|
1
|
+
import { FileAdapterConfig, FileStateAdapter } from './adapters/default-state-adapter';
|
|
2
|
+
import { MemoryStateAdapter } from './adapters/memory-state-adapter';
|
|
3
|
+
type AdapterConfig = FileAdapterConfig | {
|
|
4
|
+
adapter: 'memory';
|
|
5
5
|
};
|
|
6
|
-
export
|
|
7
|
-
export declare function createStateAdapter(config: Record<string, unknown>): FileStateAdapter | RedisStateAdapter;
|
|
6
|
+
export declare function createStateAdapter(config: AdapterConfig): FileStateAdapter | MemoryStateAdapter;
|
|
8
7
|
export {};
|
|
@@ -2,15 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createStateAdapter = createStateAdapter;
|
|
4
4
|
const default_state_adapter_1 = require("./adapters/default-state-adapter");
|
|
5
|
-
const
|
|
5
|
+
const memory_state_adapter_1 = require("./adapters/memory-state-adapter");
|
|
6
6
|
function createStateAdapter(config) {
|
|
7
|
-
|
|
8
|
-
switch (adapter) {
|
|
9
|
-
case 'redis':
|
|
10
|
-
return new redis_state_adapter_1.RedisStateAdapter(adapterConfig);
|
|
11
|
-
case 'default':
|
|
12
|
-
return new default_state_adapter_1.FileStateAdapter(adapterConfig);
|
|
13
|
-
default:
|
|
14
|
-
throw new Error(`Unknown state adapter type: ${adapter}`);
|
|
15
|
-
}
|
|
7
|
+
return config.adapter === 'default' ? new default_state_adapter_1.FileStateAdapter(config) : new memory_state_adapter_1.MemoryStateAdapter();
|
|
16
8
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { InternalStateManager } from '../types';
|
|
1
2
|
/**
|
|
2
3
|
* Interface for state management adapters
|
|
3
4
|
*/
|
|
4
|
-
export interface StateAdapter {
|
|
5
|
-
get<T>(traceId: string, key: string): Promise<T | null>;
|
|
6
|
-
set<T>(traceId: string, key: string, value: T): Promise<void>;
|
|
7
|
-
delete(traceId: string, key: string): Promise<void>;
|
|
5
|
+
export interface StateAdapter extends InternalStateManager {
|
|
8
6
|
clear(traceId: string): Promise<void>;
|
|
9
7
|
cleanup(): Promise<void>;
|
|
8
|
+
keys(traceId: string): Promise<string[]>;
|
|
9
|
+
traceIds(): Promise<string[]>;
|
|
10
10
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { InternalStateManager } from './types';
|
|
2
|
+
import type { BaseStreamItem, IStateStream, StateStreamEvent, StateStreamEventChannel } from './types-stream';
|
|
3
|
+
export type StateStreamFactory<TData> = () => IStateStream<TData>;
|
|
4
|
+
export declare abstract class StateStream<TData> implements IStateStream<TData> {
|
|
5
|
+
abstract get(groupId: string, id: string): Promise<BaseStreamItem<TData> | null>;
|
|
6
|
+
abstract set(groupId: string, id: string, data: TData): Promise<BaseStreamItem<TData> | null>;
|
|
7
|
+
abstract delete(groupId: string, id: string): Promise<BaseStreamItem<TData> | null>;
|
|
8
|
+
abstract getGroup(groupId: string): Promise<BaseStreamItem<TData>[]>;
|
|
9
|
+
send<T>(channel: StateStreamEventChannel, event: StateStreamEvent<T>): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare class InternalStateStream<TData> extends StateStream<TData> {
|
|
12
|
+
private readonly state;
|
|
13
|
+
constructor(state: InternalStateManager);
|
|
14
|
+
get(groupId: string, id: string): Promise<BaseStreamItem<TData> | null>;
|
|
15
|
+
set(groupId: string, id: string, data: TData): Promise<BaseStreamItem<TData> | null>;
|
|
16
|
+
delete(groupId: string, id: string): Promise<BaseStreamItem<TData> | null>;
|
|
17
|
+
getGroup(groupId: string): Promise<BaseStreamItem<TData>[]>;
|
|
18
|
+
send(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InternalStateStream = exports.StateStream = void 0;
|
|
4
|
+
class StateStream {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
+
async send(channel, event) { }
|
|
7
|
+
}
|
|
8
|
+
exports.StateStream = StateStream;
|
|
9
|
+
class InternalStateStream extends StateStream {
|
|
10
|
+
constructor(state) {
|
|
11
|
+
super();
|
|
12
|
+
this.state = state;
|
|
13
|
+
}
|
|
14
|
+
async get(groupId, id) {
|
|
15
|
+
return this.state.get(groupId, id);
|
|
16
|
+
}
|
|
17
|
+
async set(groupId, id, data) {
|
|
18
|
+
return this.state.set(groupId, id, { id, ...data });
|
|
19
|
+
}
|
|
20
|
+
async delete(groupId, id) {
|
|
21
|
+
return this.state.delete(groupId, id);
|
|
22
|
+
}
|
|
23
|
+
async getGroup(groupId) {
|
|
24
|
+
return this.state.getGroup(groupId);
|
|
25
|
+
}
|
|
26
|
+
async send() { }
|
|
27
|
+
}
|
|
28
|
+
exports.InternalStateStream = InternalStateStream;
|
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { ChildProcess } from 'child_process';
|
|
2
|
-
|
|
3
|
-
export
|
|
2
|
+
import { RpcProcessorInterface, RpcHandler, MessageCallback } from './process-communication/rpc-processor-interface';
|
|
3
|
+
export type RpcMessage = {
|
|
4
|
+
type: 'rpc_request';
|
|
5
|
+
id: string | undefined;
|
|
6
|
+
method: string;
|
|
7
|
+
args: unknown;
|
|
8
|
+
};
|
|
9
|
+
export declare class RpcProcessor implements RpcProcessorInterface {
|
|
4
10
|
private child;
|
|
5
11
|
private handlers;
|
|
12
|
+
private messageCallback?;
|
|
13
|
+
private isClosed;
|
|
6
14
|
constructor(child: ChildProcess);
|
|
7
15
|
handler<TInput, TOutput = unknown>(method: string, handler: RpcHandler<TInput, TOutput>): void;
|
|
8
|
-
|
|
16
|
+
onMessage<T = unknown>(callback: MessageCallback<T>): void;
|
|
17
|
+
handle(method: string, input: unknown): Promise<any>;
|
|
9
18
|
private response;
|
|
10
19
|
init(): Promise<void>;
|
|
20
|
+
close(): void;
|
|
11
21
|
}
|
|
12
|
-
export {};
|