@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fffef41
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/dist/lib/browser/index.mjs +795 -360
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +795 -360
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +129 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/example/fib.d.ts +7 -0
- package/dist/types/src/example/fib.d.ts.map +1 -0
- package/dist/types/src/example/forex-effect.d.ts +3 -0
- package/dist/types/src/example/forex-effect.d.ts.map +1 -0
- package/dist/types/src/example/index.d.ts +12 -0
- package/dist/types/src/example/index.d.ts.map +1 -0
- package/dist/types/src/example/reply.d.ts +3 -0
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/example/sleep.d.ts +5 -0
- package/dist/types/src/example/sleep.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -6
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/index.d.ts +2 -0
- package/dist/types/src/protocol/index.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +7 -0
- package/dist/types/src/protocol/protocol.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.test.d.ts +2 -0
- package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
- package/dist/types/src/sdk.d.ts +89 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +21 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +80 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +11 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +6 -7
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +33 -4
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +44 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +58 -0
- package/dist/types/src/types/Function.d.ts.map +1 -0
- package/dist/types/src/types/Script.d.ts +28 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/types/Trigger.d.ts +139 -0
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +44 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +6 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/url.d.ts +12 -0
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -66
- package/src/errors.ts +21 -0
- package/src/example/fib.ts +32 -0
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +13 -0
- package/src/example/reply.ts +21 -0
- package/src/example/sleep.ts +24 -0
- package/src/index.ts +5 -8
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +59 -0
- package/src/protocol/protocol.ts +145 -0
- package/src/sdk.ts +226 -0
- package/src/services/credentials.ts +92 -4
- package/src/services/event-logger.ts +77 -37
- package/src/services/function-invocation-service.ts +23 -0
- package/src/services/index.ts +7 -7
- package/src/services/queues.ts +50 -8
- package/src/services/tracing.ts +92 -11
- package/src/types/Function.ts +62 -0
- package/src/types/Script.ts +33 -0
- package/src/types/Trigger.ts +139 -0
- package/src/types/TriggerEvent.ts +62 -0
- package/src/types/index.ts +9 -0
- package/src/types/url.ts +31 -0
- package/dist/lib/browser/bundler/index.mjs +0 -247
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
- package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -69
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -79
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -249
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -71
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -80
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -50
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -16
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -8
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/handler.d.ts +0 -61
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -15
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -46
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/tool-resolver.d.ts +0 -14
- package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -71
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -124
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -17
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -59
- package/src/bundler/bundler.ts +0 -292
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -64
- package/src/edge/index.ts +0 -9
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -113
- package/src/schema.ts +0 -57
- package/src/services/ai.ts +0 -32
- package/src/services/database.ts +0 -50
- package/src/services/function-call-service.ts +0 -64
- package/src/services/service-container.ts +0 -127
- package/src/services/tool-resolver.ts +0 -31
- package/src/testing/index.ts +0 -5
- package/src/testing/logger.ts +0 -16
- package/src/testing/services.ts +0 -174
- package/src/trace.ts +0 -180
- package/src/translations.ts +0 -20
- package/src/types.ts +0 -211
- package/src/url.ts +0 -52
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
// src/bundler/bundler.ts
|
|
2
|
-
import { FetchHttpClient, HttpClient } from "@effect/platform";
|
|
3
|
-
import { Duration, Effect, pipe, Schedule } from "effect";
|
|
4
|
-
import { build, initialize } from "esbuild-wasm";
|
|
5
|
-
import { subtleCrypto } from "@dxos/crypto";
|
|
6
|
-
import { runAndForwardErrors } from "@dxos/effect";
|
|
7
|
-
import { invariant } from "@dxos/invariant";
|
|
8
|
-
import { log } from "@dxos/log";
|
|
9
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/bundler/bundler.ts";
|
|
10
|
-
var initialized;
|
|
11
|
-
var initializeBundler = async (options) => {
|
|
12
|
-
await (initialized ??= initialize({
|
|
13
|
-
wasmURL: options.wasmUrl
|
|
14
|
-
}));
|
|
15
|
-
};
|
|
16
|
-
var Bundler = class {
|
|
17
|
-
constructor(_options) {
|
|
18
|
-
this._options = _options;
|
|
19
|
-
}
|
|
20
|
-
async bundle({ path, source }) {
|
|
21
|
-
const { sandboxedModules: providedModules, ...options } = this._options;
|
|
22
|
-
const createResult = async (result) => {
|
|
23
|
-
return {
|
|
24
|
-
timestamp: Date.now(),
|
|
25
|
-
sourceHash: source ? Buffer.from(await subtleCrypto.digest("SHA-256", Buffer.from(source))) : void 0,
|
|
26
|
-
...result
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
if (this._options.platform === "browser") {
|
|
30
|
-
invariant(initialized, "Compiler not initialized.", {
|
|
31
|
-
F: __dxlog_file,
|
|
32
|
-
L: 73,
|
|
33
|
-
S: this,
|
|
34
|
-
A: [
|
|
35
|
-
"initialized",
|
|
36
|
-
"'Compiler not initialized.'"
|
|
37
|
-
]
|
|
38
|
-
});
|
|
39
|
-
await initialized;
|
|
40
|
-
}
|
|
41
|
-
const imports = source ? analyzeSourceFileImports(source) : [];
|
|
42
|
-
try {
|
|
43
|
-
const result = await build({
|
|
44
|
-
platform: options.platform,
|
|
45
|
-
conditions: [
|
|
46
|
-
"workerd",
|
|
47
|
-
"browser"
|
|
48
|
-
],
|
|
49
|
-
metafile: true,
|
|
50
|
-
write: false,
|
|
51
|
-
entryPoints: [
|
|
52
|
-
path ?? "memory:main.tsx"
|
|
53
|
-
],
|
|
54
|
-
bundle: true,
|
|
55
|
-
format: "esm",
|
|
56
|
-
plugins: [
|
|
57
|
-
{
|
|
58
|
-
name: "memory",
|
|
59
|
-
setup: (build2) => {
|
|
60
|
-
build2.onResolve({
|
|
61
|
-
filter: /^\.\/runtime\.js$/
|
|
62
|
-
}, ({ path: path2 }) => {
|
|
63
|
-
return {
|
|
64
|
-
path: path2,
|
|
65
|
-
external: true
|
|
66
|
-
};
|
|
67
|
-
});
|
|
68
|
-
build2.onResolve({
|
|
69
|
-
filter: /^dxos:functions$/
|
|
70
|
-
}, ({ path: path2 }) => {
|
|
71
|
-
return {
|
|
72
|
-
path: "./runtime.js",
|
|
73
|
-
external: true
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
build2.onResolve({
|
|
77
|
-
filter: /^memory:/
|
|
78
|
-
}, ({ path: path2 }) => {
|
|
79
|
-
return {
|
|
80
|
-
path: path2.split(":")[1],
|
|
81
|
-
namespace: "memory"
|
|
82
|
-
};
|
|
83
|
-
});
|
|
84
|
-
build2.onLoad({
|
|
85
|
-
filter: /.*/,
|
|
86
|
-
namespace: "memory"
|
|
87
|
-
}, ({ path: path2 }) => {
|
|
88
|
-
if (path2 === "main.tsx") {
|
|
89
|
-
return {
|
|
90
|
-
contents: source,
|
|
91
|
-
loader: "tsx"
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
for (const module of providedModules) {
|
|
96
|
-
build2.onResolve({
|
|
97
|
-
filter: new RegExp(`^${module}$`)
|
|
98
|
-
}, ({ path: path2 }) => {
|
|
99
|
-
return {
|
|
100
|
-
path: path2,
|
|
101
|
-
namespace: "injected-module"
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
build2.onLoad({
|
|
106
|
-
filter: /.*/,
|
|
107
|
-
namespace: "injected-module"
|
|
108
|
-
}, ({ path: path2 }) => {
|
|
109
|
-
const namedImports = imports.find((entry) => entry.moduleIdentifier === path2)?.namedImports ?? [];
|
|
110
|
-
return {
|
|
111
|
-
contents: `
|
|
112
|
-
const { ${namedImports.join(",")} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path2)}];
|
|
113
|
-
export { ${namedImports.join(",")} };
|
|
114
|
-
export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path2)}].default;
|
|
115
|
-
`,
|
|
116
|
-
loader: "tsx"
|
|
117
|
-
};
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
httpPlugin
|
|
122
|
-
]
|
|
123
|
-
});
|
|
124
|
-
log("compile complete", result.metafile, {
|
|
125
|
-
F: __dxlog_file,
|
|
126
|
-
L: 137,
|
|
127
|
-
S: this,
|
|
128
|
-
C: (f, a) => f(...a)
|
|
129
|
-
});
|
|
130
|
-
return await createResult({
|
|
131
|
-
imports: this.analyzeImports(result),
|
|
132
|
-
bundle: result.outputFiles[0].text
|
|
133
|
-
});
|
|
134
|
-
} catch (err) {
|
|
135
|
-
return await createResult({
|
|
136
|
-
error: err
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
|
|
141
|
-
analyzeImports(result) {
|
|
142
|
-
invariant(result.outputFiles, void 0, {
|
|
143
|
-
F: __dxlog_file,
|
|
144
|
-
L: 150,
|
|
145
|
-
S: this,
|
|
146
|
-
A: [
|
|
147
|
-
"result.outputFiles",
|
|
148
|
-
""
|
|
149
|
-
]
|
|
150
|
-
});
|
|
151
|
-
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
152
|
-
return Object.values(result.metafile.outputs)[0].imports.map((entry) => {
|
|
153
|
-
const namedImports = [];
|
|
154
|
-
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
155
|
-
if (parsedImport?.[2]) {
|
|
156
|
-
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
157
|
-
const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
|
|
158
|
-
if (namedImportsMatch) {
|
|
159
|
-
namedImportsMatch[1].split(",").forEach((importName) => {
|
|
160
|
-
namedImports.push(importName.trim());
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
moduleUrl: entry.path,
|
|
166
|
-
defaultImport: !!parsedImport?.[1],
|
|
167
|
-
namedImports
|
|
168
|
-
};
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
analyzeSourceFileImports(code) {
|
|
172
|
-
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
173
|
-
return parsedImports.map((capture) => {
|
|
174
|
-
return {
|
|
175
|
-
defaultImportName: capture[1],
|
|
176
|
-
namedImports: capture[2]?.split(",").map((importName) => importName.trim()),
|
|
177
|
-
wildcardImportName: capture[3],
|
|
178
|
-
moduleIdentifier: capture[4],
|
|
179
|
-
quotes: capture[5]
|
|
180
|
-
};
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
var IMPORT_REGEX = /import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/gm;
|
|
185
|
-
var NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
186
|
-
var allMatches = (regex, str) => {
|
|
187
|
-
regex.lastIndex = 0;
|
|
188
|
-
let match;
|
|
189
|
-
const matches = [];
|
|
190
|
-
while (match = regex.exec(str)) {
|
|
191
|
-
matches.push(match);
|
|
192
|
-
}
|
|
193
|
-
return matches;
|
|
194
|
-
};
|
|
195
|
-
var analyzeSourceFileImports = (code) => {
|
|
196
|
-
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
197
|
-
return parsedImports.map((capture) => {
|
|
198
|
-
return {
|
|
199
|
-
defaultImportName: capture[1],
|
|
200
|
-
namedImports: capture[2]?.trim().slice(1, -1).split(",").map((importName) => importName.trim()),
|
|
201
|
-
wildcardImportName: capture[3],
|
|
202
|
-
moduleIdentifier: capture[4],
|
|
203
|
-
quotes: capture[5]
|
|
204
|
-
};
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
var MAX_RETRIES = 5;
|
|
208
|
-
var INITIAL_DELAY = 1e3;
|
|
209
|
-
var httpPlugin = {
|
|
210
|
-
name: "http",
|
|
211
|
-
setup: (build2) => {
|
|
212
|
-
build2.onResolve({
|
|
213
|
-
filter: /^https?:\/\//
|
|
214
|
-
}, (args) => ({
|
|
215
|
-
path: args.path,
|
|
216
|
-
namespace: "http-url"
|
|
217
|
-
}));
|
|
218
|
-
build2.onResolve({
|
|
219
|
-
filter: /.*/,
|
|
220
|
-
namespace: "http-url"
|
|
221
|
-
}, (args) => ({
|
|
222
|
-
path: new URL(args.path, args.importer).toString(),
|
|
223
|
-
namespace: "http-url"
|
|
224
|
-
}));
|
|
225
|
-
build2.onLoad({
|
|
226
|
-
filter: /.*/,
|
|
227
|
-
namespace: "http-url"
|
|
228
|
-
}, async (args) => {
|
|
229
|
-
return Effect.gen(function* () {
|
|
230
|
-
const response = yield* HttpClient.get(args.path);
|
|
231
|
-
if (response.status !== 200) {
|
|
232
|
-
throw new Error(`failed to fetch: ${response.status}`);
|
|
233
|
-
}
|
|
234
|
-
const text = yield* response.text;
|
|
235
|
-
return {
|
|
236
|
-
contents: text,
|
|
237
|
-
loader: "jsx"
|
|
238
|
-
};
|
|
239
|
-
}).pipe(Effect.retry(pipe(Schedule.exponential(Duration.millis(INITIAL_DELAY)), Schedule.jittered, Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)))), Effect.provide(FetchHttpClient.layer), runAndForwardErrors);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
export {
|
|
244
|
-
Bundler,
|
|
245
|
-
initializeBundler
|
|
246
|
-
};
|
|
247
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/bundler/bundler.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { FetchHttpClient, HttpClient } from '@effect/platform';\nimport { Duration, Effect, pipe, Schedule } from 'effect';\nimport { type BuildOptions, type Loader, build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { runAndForwardErrors } from '@dxos/effect';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nexport type Import = {\n moduleUrl: string;\n defaultImport: boolean;\n namedImports: string[];\n};\n\nexport type BundleOptions = {\n /**\n * Path to the source file on the local file system.\n * If provided, the path will be used instead of the `source` code.\n */\n path?: string;\n\n /**\n * Source code to bundle.\n * Required if `path` is not provided.\n */\n source?: string;\n};\n\nexport type BundleResult = {\n timestamp: number;\n sourceHash?: Buffer;\n imports?: Import[];\n bundle?: string;\n error?: any;\n};\n\nexport type BundlerOptions = {\n platform: BuildOptions['platform'];\n sandboxedModules: string[];\n remoteModules: Record<string, string>;\n};\n\nlet initialized: Promise<void>;\nexport const initializeBundler = async (options: { wasmUrl: string }) => {\n await (initialized ??= initialize({\n wasmURL: options.wasmUrl,\n }));\n};\n\n/**\n * ESBuild bundler.\n */\nexport class Bundler {\n constructor(private readonly _options: BundlerOptions) {}\n\n async bundle({ path, source }: BundleOptions): Promise<BundleResult> {\n const { sandboxedModules: providedModules, ...options } = this._options;\n\n const createResult = async (result?: Partial<BundleResult>) => {\n return {\n timestamp: Date.now(),\n sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,\n ...result,\n };\n };\n\n if (this._options.platform === 'browser') {\n invariant(initialized, 'Compiler not initialized.');\n await initialized;\n }\n\n const imports = source ? analyzeSourceFileImports(source) : [];\n\n // https://esbuild.github.io/api/#build\n try {\n const result = await build({\n platform: options.platform,\n conditions: ['workerd', 'browser'],\n metafile: true,\n write: false,\n entryPoints: [path ?? 'memory:main.tsx'],\n bundle: true,\n format: 'esm',\n plugins: [\n {\n name: 'memory',\n setup: (build) => {\n build.onResolve({ filter: /^\\.\\/runtime\\.js$/ }, ({ path }) => {\n return { path, external: true };\n });\n\n build.onResolve({ filter: /^dxos:functions$/ }, ({ path }) => {\n return { path: './runtime.js', external: true };\n });\n\n build.onResolve({ filter: /^memory:/ }, ({ path }) => {\n return { path: path.split(':')[1], namespace: 'memory' };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {\n if (path === 'main.tsx') {\n return {\n contents: source,\n loader: 'tsx',\n };\n }\n });\n\n for (const module of providedModules) {\n build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {\n return { path, namespace: 'injected-module' };\n });\n }\n\n build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {\n const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];\n return {\n contents: `\n const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];\n export { ${namedImports.join(',')} };\n export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;\n `,\n loader: 'tsx',\n };\n });\n },\n },\n httpPlugin,\n ],\n });\n\n log('compile complete', result.metafile);\n\n return await createResult({\n imports: this.analyzeImports(result),\n bundle: result.outputFiles![0].text,\n });\n } catch (err) {\n return await createResult({ error: err });\n }\n }\n\n // TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.\n analyzeImports(result: BuildResult): Import[] {\n invariant(result.outputFiles);\n\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);\n return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {\n const namedImports: string[] = [];\n\n const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);\n if (parsedImport?.[2]) {\n NAMED_IMPORTS_REGEX.lastIndex = 0;\n const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);\n if (namedImportsMatch) {\n namedImportsMatch[1].split(',').forEach((importName) => {\n namedImports.push(importName.trim());\n });\n }\n }\n\n return {\n moduleUrl: entry.path,\n defaultImport: !!parsedImport?.[1],\n namedImports,\n };\n });\n }\n\n analyzeSourceFileImports(code: string): {\n defaultImportName: string;\n namedImports: string[];\n wildcardImportName: string;\n moduleIdentifier: string;\n quotes: string;\n }[] {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]?.split(',').map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n }\n}\n\n// https://regex101.com/r/FEN5ks/1\n// https://stackoverflow.com/a/73265022\n// $1 = default import name (can be non-existent)\n// $2 = destructured exports (can be non-existent)\n// $3 = wildcard import name (can be non-existent)\n// $4 = module identifier\n// $5 = quotes used (either ' or \")\nconst IMPORT_REGEX =\n /import(?:(?:(?:[ \\n\\t]+([^ *\\n\\t{},]+)[ \\n\\t]*(?:,|[ \\n\\t]+))?([ \\n\\t]*{(?:[ \\n\\t]*[^ \\n\\t\"'{}]+[ \\n\\t]*,?)+})?[ \\n\\t]*)|[ \\n\\t]*\\*[ \\n\\t]*as[ \\n\\t]+([^ \\n\\t{}]+)[ \\n\\t]+)from[ \\n\\t]*(?:['\"])([^'\"\\n]+)(['\"])/gm;\n\nconst NAMED_IMPORTS_REGEX = /[ \\n\\t]*{((?:[ \\n\\t]*[^ \\n\\t\"'{}]+[ \\n\\t]*,?)+)}[ \\n\\t]*/gm;\n\nconst allMatches = (regex: RegExp, str: string) => {\n regex.lastIndex = 0;\n\n let match;\n const matches = [];\n while ((match = regex.exec(str))) {\n matches.push(match);\n }\n\n return matches;\n};\n\ntype ParsedImport = {\n defaultImportName?: string;\n namedImports: string[];\n wildcardImportName?: string;\n moduleIdentifier: string;\n quotes: string;\n};\n\nconst analyzeSourceFileImports = (code: string): ParsedImport[] => {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]\n ?.trim()\n .slice(1, -1)\n .split(',')\n .map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n};\n\nconst MAX_RETRIES = 5;\nconst INITIAL_DELAY = 1_000;\n\nconst httpPlugin: Plugin = {\n name: 'http',\n setup: (build) => {\n // Intercept import paths starting with \"http:\" and \"https:\" so esbuild doesn't attempt to map them to a file system location.\n // Tag them with the \"http-url\" namespace to associate them with this plugin.\n build.onResolve({ filter: /^https?:\\/\\// }, (args) => ({\n path: args.path,\n namespace: 'http-url',\n }));\n\n // We also want to intercept all import paths inside downloaded files and resolve them against the original URL.\n // All of these files will be in the \"http-url\" namespace.\n // Make sure to keep the newly resolved URL in the \"http-url\" namespace so imports inside it will also be resolved as URLs recursively.\n build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({\n path: new URL(args.path, args.importer).toString(),\n namespace: 'http-url',\n }));\n\n // When a URL is loaded, we want to actually download the content from the internet.\n // This has just enough logic to be able to handle the example import from unpkg.com but in reality this would probably need to be more complex.\n build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {\n return Effect.gen(function* () {\n const response = yield* HttpClient.get(args.path);\n if (response.status !== 200) {\n throw new Error(`failed to fetch: ${response.status}`);\n }\n\n const text = yield* response.text;\n return { contents: text, loader: 'jsx' as Loader };\n }).pipe(\n Effect.retry(\n pipe(\n Schedule.exponential(Duration.millis(INITIAL_DELAY)),\n Schedule.jittered,\n Schedule.intersect(Schedule.recurs(MAX_RETRIES - 1)),\n ),\n ),\n Effect.provide(FetchHttpClient.layer),\n runAndForwardErrors,\n );\n });\n },\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,iBAAiBC,kBAAkB;AAC5C,SAASC,UAAUC,QAAQC,MAAMC,gBAAgB;AACjD,SAAyCC,OAAOC,kBAAiD;AAEjG,SAASC,oBAAoB;AAC7B,SAASC,2BAA2B;AACpC,SAASC,iBAAiB;AAC1B,SAASC,WAAW;;AAoCpB,IAAIC;AACG,IAAMC,oBAAoB,OAAOC,YAAAA;AACtC,SAAOF,gBAAgBL,WAAW;IAChCQ,SAASD,QAAQE;EACnB,CAAA;AACF;AAKO,IAAMC,UAAN,MAAMA;EACX,YAA6BC,UAA0B;SAA1BA,WAAAA;EAA2B;EAExD,MAAMC,OAAO,EAAEC,MAAMC,OAAM,GAA0C;AACnE,UAAM,EAAEC,kBAAkBC,iBAAiB,GAAGT,QAAAA,IAAY,KAAKI;AAE/D,UAAMM,eAAe,OAAOC,WAAAA;AAC1B,aAAO;QACLC,WAAWC,KAAKC,IAAG;QACnBC,YAAYR,SAASS,OAAOC,KAAK,MAAMvB,aAAawB,OAAO,WAAWF,OAAOC,KAAKV,MAAAA,CAAAA,CAAAA,IAAYY;QAC9F,GAAGR;MACL;IACF;AAEA,QAAI,KAAKP,SAASgB,aAAa,WAAW;AACxCxB,gBAAUE,aAAa,6BAAA;;;;;;;;;AACvB,YAAMA;IACR;AAEA,UAAMuB,UAAUd,SAASe,yBAAyBf,MAAAA,IAAU,CAAA;AAG5D,QAAI;AACF,YAAMI,SAAS,MAAMnB,MAAM;QACzB4B,UAAUpB,QAAQoB;QAClBG,YAAY;UAAC;UAAW;;QACxBC,UAAU;QACVC,OAAO;QACPC,aAAa;UAACpB,QAAQ;;QACtBD,QAAQ;QACRsB,QAAQ;QACRC,SAAS;UACP;YACEC,MAAM;YACNC,OAAO,CAACtC,WAAAA;AACNA,cAAAA,OAAMuC,UAA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+E,IAAID,KAAK9E,MAAM8E,KAAKE,QAAQ,EAAEC,SAAQ;MAChDpD,WAAW;IACb,EAAA;AAIA3C,IAAAA,OAAM4C,OAAO;MAAEJ,QAAQ;MAAMG,WAAW;IAAW,GAAG,OAAOiD,SAAAA;AAC3D,aAAO/F,OAAOmG,IAAI,aAAA;AAChB,cAAMC,WAAW,OAAOtG,WAAWuG,IAAIN,KAAK9E,IAAI;AAChD,YAAImF,SAASE,WAAW,KAAK;AAC3B,gBAAM,IAAIC,MAAM,oBAAoBH,SAASE,MAAM,EAAE;QACvD;AAEA,cAAMxC,OAAO,OAAOsC,SAAStC;AAC7B,eAAO;UAAEd,UAAUc;UAAMb,QAAQ;QAAgB;MACnD,CAAA,EAAGhD,KACDD,OAAOwG,MACLvG,KACEC,SAASuG,YAAY1G,SAAS2G,OAAOZ,aAAAA,CAAAA,GACrC5F,SAASyG,UACTzG,SAAS0G,UAAU1G,SAAS2G,OAAOhB,cAAc,CAAA,CAAA,CAAA,CAAA,GAGrD7F,OAAO8G,QAAQjH,gBAAgBkH,KAAK,GACpCzG,mBAAAA;IAEJ,CAAA;EACF;AACF;",
|
|
6
|
-
"names": ["FetchHttpClient", "HttpClient", "Duration", "Effect", "pipe", "Schedule", "build", "initialize", "subtleCrypto", "runAndForwardErrors", "invariant", "log", "initialized", "initializeBundler", "options", "wasmURL", "wasmUrl", "Bundler", "_options", "bundle", "path", "source", "sandboxedModules", "providedModules", "createResult", "result", "timestamp", "Date", "now", "sourceHash", "Buffer", "from", "digest", "undefined", "platform", "imports", "analyzeSourceFileImports", "conditions", "metafile", "write", "entryPoints", "format", "plugins", "name", "setup", "onResolve", "filter", "external", "split", "namespace", "onLoad", "contents", "loader", "module", "RegExp", "namedImports", "find", "entry", "moduleIdentifier", "join", "JSON", "stringify", "httpPlugin", "analyzeImports", "outputFiles", "text", "err", "error", "parsedImports", "allMatches", "IMPORT_REGEX", "Object", "values", "outputs", "map", "parsedImport", "capture", "NAMED_IMPORTS_REGEX", "lastIndex", "namedImportsMatch", "exec", "forEach", "importName", "push", "trim", "moduleUrl", "defaultImport", "code", "defaultImportName", "wildcardImportName", "quotes", "regex", "str", "match", "matches", "slice", "MAX_RETRIES", "INITIAL_DELAY", "args", "URL", "importer", "toString", "gen", "response", "get", "status", "Error", "retry", "exponential", "millis", "jittered", "intersect", "recurs", "provide", "layer"]
|
|
7
|
-
}
|
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
// src/services/ai.ts
|
|
2
|
-
import { Context, Layer } from "effect";
|
|
3
|
-
var AiService = class _AiService extends Context.Tag("AiService")() {
|
|
4
|
-
static {
|
|
5
|
-
this.make = (client) => {
|
|
6
|
-
return {
|
|
7
|
-
get client() {
|
|
8
|
-
return client;
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
static {
|
|
14
|
-
this.makeLayer = (client) => Layer.succeed(_AiService, _AiService.make(client));
|
|
15
|
-
}
|
|
16
|
-
static {
|
|
17
|
-
this.notAvailable = Layer.succeed(_AiService, {
|
|
18
|
-
get client() {
|
|
19
|
-
throw new Error("AiService not available");
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// src/services/database.ts
|
|
26
|
-
import { Context as Context2, Effect, Layer as Layer2 } from "effect";
|
|
27
|
-
var DatabaseService = class _DatabaseService extends Context2.Tag("DatabaseService")() {
|
|
28
|
-
static {
|
|
29
|
-
this.notAvailable = Layer2.succeed(_DatabaseService, {
|
|
30
|
-
get db() {
|
|
31
|
-
throw new Error("Database not available");
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
static {
|
|
36
|
-
this.make = (db) => {
|
|
37
|
-
return {
|
|
38
|
-
get db() {
|
|
39
|
-
return db;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
static {
|
|
45
|
-
this.resolve = Effect.fn(function* (dxn) {
|
|
46
|
-
const { db } = yield* _DatabaseService;
|
|
47
|
-
return yield* Effect.tryPromise({
|
|
48
|
-
try: () => db.graph.createRefResolver({
|
|
49
|
-
context: {
|
|
50
|
-
space: db.spaceId
|
|
51
|
-
}
|
|
52
|
-
}).resolve(dxn),
|
|
53
|
-
catch: (error) => error
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
static {
|
|
58
|
-
this.loadRef = Effect.fn(function* (ref) {
|
|
59
|
-
return yield* Effect.tryPromise({
|
|
60
|
-
try: () => ref.load(),
|
|
61
|
-
catch: (error) => error
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// src/services/queues.ts
|
|
68
|
-
import { Context as Context3, Layer as Layer3 } from "effect";
|
|
69
|
-
var QueueService = class _QueueService extends Context3.Tag("QueueService")() {
|
|
70
|
-
static {
|
|
71
|
-
this.notAvailable = Layer3.succeed(_QueueService, {
|
|
72
|
-
queues: {
|
|
73
|
-
get(dxn) {
|
|
74
|
-
throw new Error("Queues not available");
|
|
75
|
-
},
|
|
76
|
-
create() {
|
|
77
|
-
throw new Error("Queues not available");
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
contextQueue: void 0
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
static {
|
|
84
|
-
this.make = (queues, contextQueue) => {
|
|
85
|
-
return {
|
|
86
|
-
queues,
|
|
87
|
-
contextQueue
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// src/services/credentials.ts
|
|
94
|
-
import { Context as Context4 } from "effect";
|
|
95
|
-
var CredentialsService = class extends Context4.Tag("CredentialsService")() {
|
|
96
|
-
};
|
|
97
|
-
var ConfiguredCredentialsService = class {
|
|
98
|
-
constructor(credentials = []) {
|
|
99
|
-
this.credentials = credentials;
|
|
100
|
-
}
|
|
101
|
-
addCredentials(credentials) {
|
|
102
|
-
this.credentials.push(...credentials);
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
async queryCredentials(query) {
|
|
106
|
-
return this.credentials.filter((credential) => credential.service === query.service);
|
|
107
|
-
}
|
|
108
|
-
async getCredential(query) {
|
|
109
|
-
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
110
|
-
if (!credential) {
|
|
111
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
112
|
-
}
|
|
113
|
-
return credential;
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// src/services/event-logger.ts
|
|
118
|
-
import { Effect as Effect2, Context as Context5 } from "effect";
|
|
119
|
-
import { invariant } from "@dxos/invariant";
|
|
120
|
-
import { log, LogLevel } from "@dxos/log";
|
|
121
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
122
|
-
var EventLogger = class extends Context5.Tag("EventLogger")() {
|
|
123
|
-
static {
|
|
124
|
-
this.noop = {
|
|
125
|
-
log: () => {
|
|
126
|
-
},
|
|
127
|
-
nodeId: void 0
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var logCustomEvent = (data) => Effect2.gen(function* () {
|
|
132
|
-
const logger = yield* EventLogger;
|
|
133
|
-
if (!logger.nodeId) {
|
|
134
|
-
throw new Error("logCustomEvent must be called within a node compute function");
|
|
135
|
-
}
|
|
136
|
-
logger.log({
|
|
137
|
-
type: "custom",
|
|
138
|
-
nodeId: logger.nodeId,
|
|
139
|
-
event: data
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
var createDefectLogger = () => Effect2.catchAll((error) => Effect2.gen(function* () {
|
|
143
|
-
log.error("unhandled effect error", {
|
|
144
|
-
error
|
|
145
|
-
}, {
|
|
146
|
-
F: __dxlog_file,
|
|
147
|
-
L: 65,
|
|
148
|
-
S: this,
|
|
149
|
-
C: (f, a) => f(...a)
|
|
150
|
-
});
|
|
151
|
-
throw error;
|
|
152
|
-
}));
|
|
153
|
-
var createEventLogger = (level, message = "event") => {
|
|
154
|
-
const logFunction = {
|
|
155
|
-
[LogLevel.WARN]: log.warn,
|
|
156
|
-
[LogLevel.VERBOSE]: log.verbose,
|
|
157
|
-
[LogLevel.DEBUG]: log.debug,
|
|
158
|
-
[LogLevel.INFO]: log.info,
|
|
159
|
-
[LogLevel.ERROR]: log.error
|
|
160
|
-
}[level];
|
|
161
|
-
invariant(logFunction, void 0, {
|
|
162
|
-
F: __dxlog_file,
|
|
163
|
-
L: 80,
|
|
164
|
-
S: void 0,
|
|
165
|
-
A: [
|
|
166
|
-
"logFunction",
|
|
167
|
-
""
|
|
168
|
-
]
|
|
169
|
-
});
|
|
170
|
-
return {
|
|
171
|
-
log: (event) => {
|
|
172
|
-
logFunction(message, event);
|
|
173
|
-
},
|
|
174
|
-
nodeId: void 0
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// src/services/function-call-service.ts
|
|
179
|
-
import { Context as Context6 } from "effect";
|
|
180
|
-
var FunctionCallService = class extends Context6.Tag("FunctionCallService")() {
|
|
181
|
-
static fromClient(baseUrl, spaceId) {
|
|
182
|
-
return {
|
|
183
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
184
|
-
const url = getInvocationUrl(deployedFunctionId, baseUrl, {
|
|
185
|
-
spaceId
|
|
186
|
-
});
|
|
187
|
-
const result = await fetch(url, {
|
|
188
|
-
method: "POST",
|
|
189
|
-
headers: {
|
|
190
|
-
"Content-Type": "application/json"
|
|
191
|
-
},
|
|
192
|
-
body: JSON.stringify(input)
|
|
193
|
-
});
|
|
194
|
-
if (result.status >= 300 || result.status < 200) {
|
|
195
|
-
throw new Error("Failed to invoke function", {
|
|
196
|
-
cause: new Error(`HTTP error: ${await result.text()}`)
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
return await result.json();
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
static {
|
|
204
|
-
this.mock = () => {
|
|
205
|
-
return {
|
|
206
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
207
|
-
return input;
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
214
|
-
const baseUrl = new URL("functions/", edgeUrl);
|
|
215
|
-
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
216
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
217
|
-
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
218
|
-
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
219
|
-
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
220
|
-
return url.toString();
|
|
221
|
-
};
|
|
222
|
-
var isSecure = (protocol) => {
|
|
223
|
-
return protocol === "https:" || protocol === "wss:";
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
// src/services/tool-resolver.ts
|
|
227
|
-
import { Context as Context7 } from "effect";
|
|
228
|
-
var ToolResolverService = class extends Context7.Tag("ToolResolverService")() {
|
|
229
|
-
static {
|
|
230
|
-
this.notAvailable = {
|
|
231
|
-
toolResolver: {
|
|
232
|
-
resolve: async (id) => {
|
|
233
|
-
throw new Error("Tool resolver not available");
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
static {
|
|
239
|
-
this.make = (toolResolver) => {
|
|
240
|
-
return {
|
|
241
|
-
toolResolver
|
|
242
|
-
};
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
// src/services/tracing.ts
|
|
248
|
-
import { Context as Context8 } from "effect";
|
|
249
|
-
var TracingService = class extends Context8.Tag("TracingService")() {
|
|
250
|
-
static {
|
|
251
|
-
this.noop = {
|
|
252
|
-
write: () => {
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
static {
|
|
257
|
-
this.console = {
|
|
258
|
-
write: (event) => {
|
|
259
|
-
console.log(event);
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
// src/services/service-container.ts
|
|
266
|
-
import { Layer as Layer4 } from "effect";
|
|
267
|
-
var SERVICE_MAPPING = {
|
|
268
|
-
[AiService.key]: "ai",
|
|
269
|
-
[CredentialsService.key]: "credentials",
|
|
270
|
-
[DatabaseService.key]: "database",
|
|
271
|
-
[EventLogger.key]: "eventLogger",
|
|
272
|
-
[FunctionCallService.key]: "functionCallService",
|
|
273
|
-
[QueueService.key]: "queues",
|
|
274
|
-
[TracingService.key]: "tracing",
|
|
275
|
-
[ToolResolverService.key]: "toolResolver"
|
|
276
|
-
};
|
|
277
|
-
var SERVICE_TAGS = [
|
|
278
|
-
AiService,
|
|
279
|
-
CredentialsService,
|
|
280
|
-
DatabaseService,
|
|
281
|
-
EventLogger,
|
|
282
|
-
FunctionCallService,
|
|
283
|
-
TracingService,
|
|
284
|
-
QueueService
|
|
285
|
-
];
|
|
286
|
-
var DEFAULT_SERVICES = {
|
|
287
|
-
tracing: TracingService.noop
|
|
288
|
-
};
|
|
289
|
-
var ServiceContainer = class _ServiceContainer {
|
|
290
|
-
constructor() {
|
|
291
|
-
this._services = {
|
|
292
|
-
...DEFAULT_SERVICES
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Set services.
|
|
297
|
-
* @param services - Services to set.
|
|
298
|
-
* @returns The container instance.
|
|
299
|
-
*/
|
|
300
|
-
setServices(services) {
|
|
301
|
-
this._services = {
|
|
302
|
-
...this._services,
|
|
303
|
-
...services
|
|
304
|
-
};
|
|
305
|
-
return this;
|
|
306
|
-
}
|
|
307
|
-
getService(tag) {
|
|
308
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
309
|
-
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
310
|
-
if (!service) {
|
|
311
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
312
|
-
}
|
|
313
|
-
return service;
|
|
314
|
-
}
|
|
315
|
-
clone() {
|
|
316
|
-
return new _ServiceContainer().setServices({
|
|
317
|
-
...this._services
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
// TODO(dmaretskyi): `getService` is designed to error at runtime if the service is not available, but layer forces us to provide all services and makes stubs for the ones that are not available.
|
|
321
|
-
createLayer() {
|
|
322
|
-
const ai = this._services.ai != null ? Layer4.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
323
|
-
const credentials = Layer4.succeed(CredentialsService, new ConfiguredCredentialsService());
|
|
324
|
-
const database = this._services.database != null ? Layer4.succeed(DatabaseService, this._services.database) : DatabaseService.notAvailable;
|
|
325
|
-
const queues = this._services.queues != null ? Layer4.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
326
|
-
const tracing = Layer4.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
327
|
-
const eventLogger = Layer4.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
|
|
328
|
-
const functionCallService = Layer4.succeed(FunctionCallService, this._services.functionCallService ?? FunctionCallService.mock());
|
|
329
|
-
const toolResolver = Layer4.succeed(ToolResolverService, this._services.toolResolver ?? ToolResolverService.notAvailable);
|
|
330
|
-
return Layer4.mergeAll(
|
|
331
|
-
//
|
|
332
|
-
ai,
|
|
333
|
-
credentials,
|
|
334
|
-
database,
|
|
335
|
-
queues,
|
|
336
|
-
tracing,
|
|
337
|
-
eventLogger,
|
|
338
|
-
functionCallService,
|
|
339
|
-
toolResolver
|
|
340
|
-
);
|
|
341
|
-
}
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
export {
|
|
345
|
-
AiService,
|
|
346
|
-
DatabaseService,
|
|
347
|
-
QueueService,
|
|
348
|
-
CredentialsService,
|
|
349
|
-
ConfiguredCredentialsService,
|
|
350
|
-
EventLogger,
|
|
351
|
-
logCustomEvent,
|
|
352
|
-
createDefectLogger,
|
|
353
|
-
createEventLogger,
|
|
354
|
-
FunctionCallService,
|
|
355
|
-
ToolResolverService,
|
|
356
|
-
TracingService,
|
|
357
|
-
SERVICE_TAGS,
|
|
358
|
-
ServiceContainer
|
|
359
|
-
};
|
|
360
|
-
//# sourceMappingURL=chunk-54U464M4.mjs.map
|