@dxos/functions 0.8.4-main.72ec0f3 → 0.8.4-main.7996785055
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -6
- package/dist/lib/neutral/chunk-GEM2K7BA.mjs +56 -0
- package/dist/lib/neutral/chunk-GEM2K7BA.mjs.map +7 -0
- package/dist/lib/neutral/fib-XBUKWUUS.mjs +22 -0
- package/dist/lib/neutral/fib-XBUKWUUS.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +960 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/reply-XBQTXDLZ.mjs +18 -0
- package/dist/lib/neutral/reply-XBQTXDLZ.mjs.map +7 -0
- package/dist/lib/neutral/sleep-TLQKHAK2.mjs +14 -0
- package/dist/lib/neutral/sleep-TLQKHAK2.mjs.map +7 -0
- package/dist/types/src/errors.d.ts +24 -32
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/example/definitions.d.ts +11 -0
- package/dist/types/src/example/definitions.d.ts.map +1 -0
- package/dist/types/src/example/fib.d.ts +3 -2
- package/dist/types/src/example/fib.d.ts.map +1 -1
- package/dist/types/src/example/index.d.ts +3 -11
- package/dist/types/src/example/index.d.ts.map +1 -1
- package/dist/types/src/example/reply.d.ts +2 -1
- package/dist/types/src/example/reply.d.ts.map +1 -1
- package/dist/types/src/example/sleep.d.ts +3 -2
- package/dist/types/src/example/sleep.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +2 -2
- package/dist/types/src/protocol/protocol.d.ts.map +1 -1
- package/dist/types/src/sdk.d.ts +2 -79
- package/dist/types/src/sdk.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +6 -4
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +25 -31
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +9 -3
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +0 -1
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +4 -4
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +38 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Script.d.ts +9 -16
- package/dist/types/src/types/Script.d.ts.map +1 -1
- package/dist/types/src/types/Trigger.d.ts +57 -76
- package/dist/types/src/types/Trigger.d.ts.map +1 -1
- package/dist/types/src/types/TriggerEvent.d.ts +44 -13
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +0 -1
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +6 -5
- package/dist/types/src/types/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +23 -17
- package/src/errors.ts +4 -4
- package/src/example/definitions.ts +48 -0
- package/src/example/fib.ts +14 -24
- package/src/example/forex-effect.ts +1 -1
- package/src/example/index.ts +7 -8
- package/src/example/reply.ts +10 -13
- package/src/example/sleep.ts +8 -17
- package/src/protocol/functions-ai-http-client.ts +67 -0
- package/src/protocol/protocol.ts +196 -84
- package/src/sdk.ts +4 -194
- package/src/services/credentials.ts +31 -15
- package/src/services/event-logger.ts +2 -2
- package/src/services/function-invocation-service.ts +18 -3
- package/src/services/index.ts +0 -2
- package/src/services/queues.ts +5 -7
- package/src/services/tracing.ts +64 -4
- package/src/types/Script.ts +13 -8
- package/src/types/Trigger.ts +31 -17
- package/src/types/TriggerEvent.ts +29 -29
- package/src/types/index.ts +0 -1
- package/src/types/url.ts +6 -5
- package/dist/lib/browser/index.mjs +0 -927
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -928
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/types/Function.d.ts +0 -58
- package/dist/types/src/types/Function.d.ts.map +0 -1
- package/src/types/Function.ts +0 -62
|
@@ -0,0 +1,960 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Fibonacci,
|
|
3
|
+
Reply,
|
|
4
|
+
Sleep,
|
|
5
|
+
__export
|
|
6
|
+
} from "./chunk-GEM2K7BA.mjs";
|
|
7
|
+
|
|
8
|
+
// src/errors.ts
|
|
9
|
+
import { BaseError } from "@dxos/errors";
|
|
10
|
+
var ServiceNotAvailableError = class extends BaseError.extend("ServiceNotAvailable", "Service not available") {
|
|
11
|
+
constructor(service, options) {
|
|
12
|
+
super({
|
|
13
|
+
context: {
|
|
14
|
+
service
|
|
15
|
+
},
|
|
16
|
+
...options
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var FunctionNotFoundError = class extends BaseError.extend("FunctionNotFound", "Function not found") {
|
|
21
|
+
constructor(functionKey, options) {
|
|
22
|
+
super({
|
|
23
|
+
context: {
|
|
24
|
+
function: functionKey
|
|
25
|
+
},
|
|
26
|
+
...options
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var FunctionError = class extends BaseError.extend("FunctionError", "Function invocation error") {
|
|
31
|
+
};
|
|
32
|
+
var TriggerStateNotFoundError = class extends BaseError.extend("TriggerStateNotFound", "Trigger state not found") {
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/example/index.ts
|
|
36
|
+
import { OperationHandlerSet } from "@dxos/operation";
|
|
37
|
+
var ExampleHandlers = OperationHandlerSet.lazy(() => import("./fib-XBUKWUUS.mjs"), () => import("./reply-XBQTXDLZ.mjs"), () => import("./sleep-TLQKHAK2.mjs"));
|
|
38
|
+
|
|
39
|
+
// src/services/credentials.ts
|
|
40
|
+
import * as HttpClient from "@effect/platform/HttpClient";
|
|
41
|
+
import * as HttpClientRequest from "@effect/platform/HttpClientRequest";
|
|
42
|
+
import * as Context from "effect/Context";
|
|
43
|
+
import * as Effect from "effect/Effect";
|
|
44
|
+
import * as Layer from "effect/Layer";
|
|
45
|
+
import * as Redacted from "effect/Redacted";
|
|
46
|
+
import { Query } from "@dxos/echo";
|
|
47
|
+
import { Database } from "@dxos/echo";
|
|
48
|
+
import { AccessToken } from "@dxos/types";
|
|
49
|
+
var CredentialsService = class _CredentialsService extends Context.Tag("@dxos/functions/CredentialsService")() {
|
|
50
|
+
static getCredential = (query) => Effect.gen(function* () {
|
|
51
|
+
const credentials = yield* _CredentialsService;
|
|
52
|
+
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
53
|
+
});
|
|
54
|
+
static getApiKey = (query) => Effect.gen(function* () {
|
|
55
|
+
const credential = yield* _CredentialsService.getCredential(query);
|
|
56
|
+
if (!credential.apiKey) {
|
|
57
|
+
throw new Error(`API key not found for service: ${query.service}`);
|
|
58
|
+
}
|
|
59
|
+
return Redacted.make(credential.apiKey);
|
|
60
|
+
});
|
|
61
|
+
static configuredLayer = (credentials) => Layer.succeed(_CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
62
|
+
static layerConfig = (credentials) => Layer.effect(_CredentialsService, Effect.gen(function* () {
|
|
63
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) => Effect.gen(function* () {
|
|
64
|
+
return {
|
|
65
|
+
service,
|
|
66
|
+
apiKey: Redacted.value(yield* apiKey)
|
|
67
|
+
};
|
|
68
|
+
}));
|
|
69
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
70
|
+
}));
|
|
71
|
+
static layerFromDatabase = ({ caching = false } = {}) => Layer.effect(_CredentialsService, Effect.gen(function* () {
|
|
72
|
+
const dbService = yield* Database.Service;
|
|
73
|
+
const cache = /* @__PURE__ */ new Map();
|
|
74
|
+
const queryCredentials = async (query) => {
|
|
75
|
+
const cacheKey = JSON.stringify(query);
|
|
76
|
+
if (caching && cache.has(cacheKey)) {
|
|
77
|
+
return cache.get(cacheKey);
|
|
78
|
+
}
|
|
79
|
+
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
80
|
+
const credentials = accessTokens.filter((accessToken) => accessToken.source === query.service).map((accessToken) => ({
|
|
81
|
+
service: accessToken.source,
|
|
82
|
+
apiKey: accessToken.token
|
|
83
|
+
}));
|
|
84
|
+
if (caching) {
|
|
85
|
+
cache.set(cacheKey, credentials);
|
|
86
|
+
}
|
|
87
|
+
return credentials;
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
getCredential: async (query) => {
|
|
91
|
+
const credentials = await queryCredentials(query);
|
|
92
|
+
if (credentials.length === 0) {
|
|
93
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
94
|
+
}
|
|
95
|
+
return credentials[0];
|
|
96
|
+
},
|
|
97
|
+
queryCredentials: async (query) => {
|
|
98
|
+
return queryCredentials(query);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}));
|
|
102
|
+
};
|
|
103
|
+
var ConfiguredCredentialsService = class {
|
|
104
|
+
credentials;
|
|
105
|
+
constructor(credentials = []) {
|
|
106
|
+
this.credentials = credentials;
|
|
107
|
+
}
|
|
108
|
+
addCredentials(credentials) {
|
|
109
|
+
this.credentials.push(...credentials);
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
async queryCredentials(query) {
|
|
113
|
+
return this.credentials.filter((credential) => credential.service === query.service);
|
|
114
|
+
}
|
|
115
|
+
async getCredential(query) {
|
|
116
|
+
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
117
|
+
if (!credential) {
|
|
118
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
119
|
+
}
|
|
120
|
+
return credential;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var withAuthorization = (token, kind) => HttpClient.mapRequest((request) => {
|
|
124
|
+
const authorization = kind ? `${kind} ${token}` : token;
|
|
125
|
+
return HttpClientRequest.setHeader(request, "Authorization", authorization);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// src/services/event-logger.ts
|
|
129
|
+
import * as Context3 from "effect/Context";
|
|
130
|
+
import * as Effect3 from "effect/Effect";
|
|
131
|
+
import * as Layer3 from "effect/Layer";
|
|
132
|
+
import * as Schema from "effect/Schema";
|
|
133
|
+
import { Obj as Obj2, Type } from "@dxos/echo";
|
|
134
|
+
import { invariant } from "@dxos/invariant";
|
|
135
|
+
import { LogLevel, log } from "@dxos/log";
|
|
136
|
+
|
|
137
|
+
// src/services/tracing.ts
|
|
138
|
+
import * as Context2 from "effect/Context";
|
|
139
|
+
import * as Effect2 from "effect/Effect";
|
|
140
|
+
import * as Layer2 from "effect/Layer";
|
|
141
|
+
import { AgentStatus } from "@dxos/ai";
|
|
142
|
+
import { Obj } from "@dxos/echo";
|
|
143
|
+
import { ObjectId } from "@dxos/keys";
|
|
144
|
+
import { Message } from "@dxos/types";
|
|
145
|
+
var TracingService = class _TracingService extends Context2.Tag("@dxos/functions/TracingService")() {
|
|
146
|
+
static noop = {
|
|
147
|
+
getTraceContext: () => ({}),
|
|
148
|
+
write: () => {
|
|
149
|
+
},
|
|
150
|
+
traceInvocationStart: () => Effect2.sync(() => ({
|
|
151
|
+
invocationId: ObjectId.random(),
|
|
152
|
+
invocationTraceQueue: void 0
|
|
153
|
+
})),
|
|
154
|
+
traceInvocationEnd: () => Effect2.sync(() => {
|
|
155
|
+
})
|
|
156
|
+
};
|
|
157
|
+
static layerNoop = Layer2.succeed(_TracingService, _TracingService.noop);
|
|
158
|
+
/**
|
|
159
|
+
* Creates a TracingService layer that emits events to the parent tracing service.
|
|
160
|
+
*/
|
|
161
|
+
static layerSubframe = (mapContext) => Layer2.effect(_TracingService, Effect2.gen(function* () {
|
|
162
|
+
const tracing = yield* _TracingService;
|
|
163
|
+
const context = mapContext(tracing.getTraceContext());
|
|
164
|
+
return {
|
|
165
|
+
write: (event, context2) => tracing.write(event, context2),
|
|
166
|
+
getTraceContext: () => context,
|
|
167
|
+
traceInvocationStart: () => Effect2.die("Tracing invocation inside another invocation is not supported."),
|
|
168
|
+
traceInvocationEnd: () => Effect2.die("Tracing invocation inside another invocation is not supported.")
|
|
169
|
+
};
|
|
170
|
+
}));
|
|
171
|
+
/**
|
|
172
|
+
* Create sublayer to trace an invocation.
|
|
173
|
+
* @param data
|
|
174
|
+
* @returns
|
|
175
|
+
*/
|
|
176
|
+
static layerInvocation = (data) => _TracingService.layerSubframe((context) => ({
|
|
177
|
+
...context,
|
|
178
|
+
currentInvocation: data
|
|
179
|
+
}));
|
|
180
|
+
/**
|
|
181
|
+
* Emit the current human-readable execution status.
|
|
182
|
+
*/
|
|
183
|
+
static emitStatus = Effect2.fnUntraced(function* (data) {
|
|
184
|
+
const tracing = yield* _TracingService;
|
|
185
|
+
tracing.write(Obj.make(AgentStatus, {
|
|
186
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
187
|
+
toolCallId: tracing.getTraceContext().toolCallId,
|
|
188
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
189
|
+
...data
|
|
190
|
+
}), tracing.getTraceContext());
|
|
191
|
+
});
|
|
192
|
+
static emitConverationMessage = Effect2.fnUntraced(function* (data) {
|
|
193
|
+
const tracing = yield* _TracingService;
|
|
194
|
+
tracing.write(Obj.make(Message.Message, {
|
|
195
|
+
parentMessage: tracing.getTraceContext().parentMessage,
|
|
196
|
+
...data,
|
|
197
|
+
properties: {
|
|
198
|
+
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
199
|
+
...data.properties
|
|
200
|
+
}
|
|
201
|
+
}), tracing.getTraceContext());
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
var MESSAGE_PROPERTY_TOOL_CALL_ID = "toolCallId";
|
|
205
|
+
|
|
206
|
+
// src/services/event-logger.ts
|
|
207
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
208
|
+
var ComputeEventPayload = Schema.Union(Schema.Struct({
|
|
209
|
+
type: Schema.Literal("begin-compute"),
|
|
210
|
+
nodeId: Schema.String,
|
|
211
|
+
/**
|
|
212
|
+
* Names of the inputs begin computed.
|
|
213
|
+
*/
|
|
214
|
+
inputs: Schema.Array(Schema.String)
|
|
215
|
+
}), Schema.Struct({
|
|
216
|
+
type: Schema.Literal("end-compute"),
|
|
217
|
+
nodeId: Schema.String,
|
|
218
|
+
/**
|
|
219
|
+
* Names of the outputs computed.
|
|
220
|
+
*/
|
|
221
|
+
outputs: Schema.Array(Schema.String)
|
|
222
|
+
}), Schema.Struct({
|
|
223
|
+
type: Schema.Literal("compute-input"),
|
|
224
|
+
nodeId: Schema.String,
|
|
225
|
+
property: Schema.String,
|
|
226
|
+
value: Schema.Any
|
|
227
|
+
}), Schema.Struct({
|
|
228
|
+
type: Schema.Literal("compute-output"),
|
|
229
|
+
nodeId: Schema.String,
|
|
230
|
+
property: Schema.String,
|
|
231
|
+
value: Schema.Any
|
|
232
|
+
}), Schema.Struct({
|
|
233
|
+
type: Schema.Literal("custom"),
|
|
234
|
+
nodeId: Schema.String,
|
|
235
|
+
event: Schema.Any
|
|
236
|
+
}));
|
|
237
|
+
var ComputeEvent = Schema.Struct({
|
|
238
|
+
payload: ComputeEventPayload
|
|
239
|
+
}).pipe(Type.object({
|
|
240
|
+
typename: "org.dxos.type.compute-event",
|
|
241
|
+
version: "0.1.0"
|
|
242
|
+
}));
|
|
243
|
+
var ComputeEventLogger = class _ComputeEventLogger extends Context3.Tag("@dxos/functions/ComputeEventLogger")() {
|
|
244
|
+
static noop = {
|
|
245
|
+
log: () => {
|
|
246
|
+
},
|
|
247
|
+
nodeId: void 0
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Implements ComputeEventLogger using TracingService.
|
|
251
|
+
*/
|
|
252
|
+
static layerFromTracing = Layer3.effect(_ComputeEventLogger, Effect3.gen(function* () {
|
|
253
|
+
const tracing = yield* TracingService;
|
|
254
|
+
return {
|
|
255
|
+
log: (event) => {
|
|
256
|
+
tracing.write(Obj2.make(ComputeEvent, {
|
|
257
|
+
payload: event
|
|
258
|
+
}), tracing.getTraceContext());
|
|
259
|
+
},
|
|
260
|
+
nodeId: void 0
|
|
261
|
+
};
|
|
262
|
+
}));
|
|
263
|
+
};
|
|
264
|
+
var logCustomEvent = (data) => Effect3.gen(function* () {
|
|
265
|
+
const logger = yield* ComputeEventLogger;
|
|
266
|
+
if (!logger.nodeId) {
|
|
267
|
+
throw new Error("logCustomEvent must be called within a node compute function");
|
|
268
|
+
}
|
|
269
|
+
logger.log({
|
|
270
|
+
type: "custom",
|
|
271
|
+
nodeId: logger.nodeId,
|
|
272
|
+
event: data
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
var createDefectLogger = () => Effect3.catchAll((error) => Effect3.gen(function* () {
|
|
276
|
+
log.error("unhandled effect error", {
|
|
277
|
+
error
|
|
278
|
+
}, {
|
|
279
|
+
F: __dxlog_file,
|
|
280
|
+
L: 102,
|
|
281
|
+
S: this,
|
|
282
|
+
C: (f, a) => f(...a)
|
|
283
|
+
});
|
|
284
|
+
throw error;
|
|
285
|
+
}));
|
|
286
|
+
var createEventLogger = (level, message = "event") => {
|
|
287
|
+
const logFunction = {
|
|
288
|
+
[LogLevel.WARN]: log.warn,
|
|
289
|
+
[LogLevel.VERBOSE]: log.verbose,
|
|
290
|
+
[LogLevel.DEBUG]: log.debug,
|
|
291
|
+
[LogLevel.INFO]: log.info,
|
|
292
|
+
[LogLevel.ERROR]: log.error
|
|
293
|
+
}[level];
|
|
294
|
+
invariant(logFunction, void 0, {
|
|
295
|
+
F: __dxlog_file,
|
|
296
|
+
L: 120,
|
|
297
|
+
S: void 0,
|
|
298
|
+
A: [
|
|
299
|
+
"logFunction",
|
|
300
|
+
""
|
|
301
|
+
]
|
|
302
|
+
});
|
|
303
|
+
return {
|
|
304
|
+
log: (event) => {
|
|
305
|
+
logFunction(message, event);
|
|
306
|
+
},
|
|
307
|
+
nodeId: void 0
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// src/services/function-invocation-service.ts
|
|
312
|
+
import * as Context4 from "effect/Context";
|
|
313
|
+
import * as Effect4 from "effect/Effect";
|
|
314
|
+
import * as Layer4 from "effect/Layer";
|
|
315
|
+
var FunctionInvocationService = class _FunctionInvocationService extends Context4.Tag("@dxos/functions/FunctionInvocationService")() {
|
|
316
|
+
static layerNotAvailable = Layer4.succeed(_FunctionInvocationService, {
|
|
317
|
+
invokeFunction: () => Effect4.die("FunctionInvocationService is not avaialble."),
|
|
318
|
+
resolveFunction: () => Effect4.die("FunctionInvocationService is not available.")
|
|
319
|
+
});
|
|
320
|
+
static invokeFunction = (functionDef, input) => Effect4.serviceFunctionEffect(_FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
321
|
+
static resolveFunction = (key) => Effect4.serviceFunctionEffect(_FunctionInvocationService, (service) => service.resolveFunction)(key);
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// src/services/queues.ts
|
|
325
|
+
import * as Context5 from "effect/Context";
|
|
326
|
+
import * as Effect5 from "effect/Effect";
|
|
327
|
+
import * as Layer5 from "effect/Layer";
|
|
328
|
+
var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/QueueService")() {
|
|
329
|
+
static notAvailable = Layer5.succeed(_QueueService, {
|
|
330
|
+
queues: {
|
|
331
|
+
get(_dxn) {
|
|
332
|
+
throw new Error("Queues not available");
|
|
333
|
+
},
|
|
334
|
+
create() {
|
|
335
|
+
throw new Error("Queues not available");
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
queue: void 0
|
|
339
|
+
});
|
|
340
|
+
static make = (queues, queue) => {
|
|
341
|
+
return {
|
|
342
|
+
queues,
|
|
343
|
+
queue
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
static layer = (queues, queue) => Layer5.succeed(_QueueService, _QueueService.make(queues, queue));
|
|
347
|
+
/**
|
|
348
|
+
* Gets a queue by its DXN.
|
|
349
|
+
*/
|
|
350
|
+
static getQueue = (dxn) => _QueueService.pipe(Effect5.map(({ queues }) => queues.get(dxn)));
|
|
351
|
+
/**
|
|
352
|
+
* Creates a new queue.
|
|
353
|
+
*/
|
|
354
|
+
static createQueue = (options) => _QueueService.pipe(Effect5.map(({ queues }) => queues.create(options)));
|
|
355
|
+
static append = (queue, objects) => Effect5.promise(() => queue.append(objects));
|
|
356
|
+
};
|
|
357
|
+
var ContextQueueService = class _ContextQueueService extends Context5.Tag("@dxos/functions/ContextQueueService")() {
|
|
358
|
+
static layer = (queue) => Layer5.succeed(_ContextQueueService, {
|
|
359
|
+
queue
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
// src/types/Script.ts
|
|
364
|
+
var Script_exports = {};
|
|
365
|
+
__export(Script_exports, {
|
|
366
|
+
Script: () => Script,
|
|
367
|
+
make: () => make2
|
|
368
|
+
});
|
|
369
|
+
import * as Schema2 from "effect/Schema";
|
|
370
|
+
import { Annotation, Obj as Obj3, Ref, Type as Type2 } from "@dxos/echo";
|
|
371
|
+
import { FormInputAnnotation } from "@dxos/echo/internal";
|
|
372
|
+
import { Text } from "@dxos/schema";
|
|
373
|
+
var Script = Schema2.Struct({
|
|
374
|
+
name: Schema2.String.pipe(Schema2.optional),
|
|
375
|
+
description: Schema2.String.pipe(Schema2.optional),
|
|
376
|
+
// TODO(burdon): Change to hash of deployed content.
|
|
377
|
+
// Whether source has changed since last deploy.
|
|
378
|
+
changed: Schema2.Boolean.pipe(FormInputAnnotation.set(false), Schema2.optional),
|
|
379
|
+
source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false))
|
|
380
|
+
}).pipe(Type2.object({
|
|
381
|
+
typename: "org.dxos.type.script",
|
|
382
|
+
version: "0.1.0"
|
|
383
|
+
}), Annotation.LabelAnnotation.set([
|
|
384
|
+
"name"
|
|
385
|
+
]), Annotation.IconAnnotation.set({
|
|
386
|
+
icon: "ph--code--regular",
|
|
387
|
+
hue: "sky"
|
|
388
|
+
}));
|
|
389
|
+
var make2 = ({ source = "", ...props } = {}) => Obj3.make(Script, {
|
|
390
|
+
...props,
|
|
391
|
+
source: Ref.make(Text.make(source))
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// src/types/Trigger.ts
|
|
395
|
+
var Trigger_exports = {};
|
|
396
|
+
__export(Trigger_exports, {
|
|
397
|
+
EmailSpec: () => EmailSpec,
|
|
398
|
+
Kinds: () => Kinds,
|
|
399
|
+
QueueSpec: () => QueueSpec,
|
|
400
|
+
Spec: () => Spec,
|
|
401
|
+
SubscriptionSpec: () => SubscriptionSpec,
|
|
402
|
+
TimerSpec: () => TimerSpec,
|
|
403
|
+
Trigger: () => Trigger,
|
|
404
|
+
WebhookSpec: () => WebhookSpec,
|
|
405
|
+
make: () => make3
|
|
406
|
+
});
|
|
407
|
+
import * as Schema3 from "effect/Schema";
|
|
408
|
+
import * as SchemaAST from "effect/SchemaAST";
|
|
409
|
+
import { Annotation as Annotation2, Obj as Obj4, QueryAST, Ref as Ref2, Type as Type3 } from "@dxos/echo";
|
|
410
|
+
import { OptionsAnnotationId, SystemTypeAnnotation } from "@dxos/echo/internal";
|
|
411
|
+
import { DXN } from "@dxos/keys";
|
|
412
|
+
var Kinds = [
|
|
413
|
+
"email",
|
|
414
|
+
"queue",
|
|
415
|
+
"subscription",
|
|
416
|
+
"timer",
|
|
417
|
+
"webhook"
|
|
418
|
+
];
|
|
419
|
+
var kindLiteralAnnotations = {
|
|
420
|
+
title: "Kind"
|
|
421
|
+
};
|
|
422
|
+
var EmailSpec = Schema3.Struct({
|
|
423
|
+
kind: Schema3.Literal("email").annotations(kindLiteralAnnotations)
|
|
424
|
+
});
|
|
425
|
+
var QueueSpec = Schema3.Struct({
|
|
426
|
+
kind: Schema3.Literal("queue").annotations(kindLiteralAnnotations),
|
|
427
|
+
// TODO(dmaretskyi): Change to a reference.
|
|
428
|
+
queue: DXN.Schema
|
|
429
|
+
});
|
|
430
|
+
var SubscriptionSpec = Schema3.Struct({
|
|
431
|
+
kind: Schema3.Literal("subscription").annotations(kindLiteralAnnotations),
|
|
432
|
+
query: Schema3.Struct({
|
|
433
|
+
raw: Schema3.optional(Schema3.String.annotations({
|
|
434
|
+
title: "Query"
|
|
435
|
+
})),
|
|
436
|
+
ast: QueryAST.Query
|
|
437
|
+
}),
|
|
438
|
+
options: Schema3.optional(Schema3.Struct({
|
|
439
|
+
// Watch changes to object (not just creation).
|
|
440
|
+
deep: Schema3.optional(Schema3.Boolean.annotations({
|
|
441
|
+
title: "Nested"
|
|
442
|
+
})),
|
|
443
|
+
// Debounce changes (delay in ms).
|
|
444
|
+
delay: Schema3.optional(Schema3.Number.annotations({
|
|
445
|
+
title: "Delay"
|
|
446
|
+
}))
|
|
447
|
+
}).annotations({
|
|
448
|
+
title: "Options"
|
|
449
|
+
}))
|
|
450
|
+
});
|
|
451
|
+
var TimerSpec = Schema3.Struct({
|
|
452
|
+
kind: Schema3.Literal("timer").annotations(kindLiteralAnnotations),
|
|
453
|
+
cron: Schema3.String.annotations({
|
|
454
|
+
title: "Cron",
|
|
455
|
+
[SchemaAST.ExamplesAnnotationId]: [
|
|
456
|
+
"0 0 * * *"
|
|
457
|
+
]
|
|
458
|
+
})
|
|
459
|
+
});
|
|
460
|
+
var WebhookSpec = Schema3.Struct({
|
|
461
|
+
kind: Schema3.Literal("webhook").annotations(kindLiteralAnnotations),
|
|
462
|
+
method: Schema3.optional(Schema3.String.annotations({
|
|
463
|
+
title: "Method",
|
|
464
|
+
[OptionsAnnotationId]: [
|
|
465
|
+
"GET",
|
|
466
|
+
"POST"
|
|
467
|
+
]
|
|
468
|
+
})),
|
|
469
|
+
port: Schema3.optional(Schema3.Number.annotations({
|
|
470
|
+
title: "Port"
|
|
471
|
+
}))
|
|
472
|
+
});
|
|
473
|
+
var Spec = Schema3.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
|
|
474
|
+
title: "Trigger"
|
|
475
|
+
});
|
|
476
|
+
var TriggerSchema = Schema3.Struct({
|
|
477
|
+
/**
|
|
478
|
+
* Function or workflow to invoke.
|
|
479
|
+
*/
|
|
480
|
+
// TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
|
|
481
|
+
function: Schema3.optional(Ref2.Ref(Obj4.Unknown).annotations({
|
|
482
|
+
title: "Function"
|
|
483
|
+
})),
|
|
484
|
+
/**
|
|
485
|
+
* Only used for workflowSchema.
|
|
486
|
+
* Specifies the input node in the circuit.
|
|
487
|
+
* @deprecated Remove and enforce a single input node in all compute graphSchema.
|
|
488
|
+
*/
|
|
489
|
+
inputNodeId: Schema3.optional(Schema3.String.annotations({
|
|
490
|
+
title: "Input Node ID"
|
|
491
|
+
})),
|
|
492
|
+
// TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
|
|
493
|
+
// Need lint rule; or agent rule to require PR review for "boolean" key word.
|
|
494
|
+
enabled: Schema3.optional(Schema3.Boolean.annotations({
|
|
495
|
+
title: "Enabled"
|
|
496
|
+
})),
|
|
497
|
+
spec: Schema3.optional(Spec),
|
|
498
|
+
concurrency: Schema3.optional(Schema3.Number.annotations({
|
|
499
|
+
title: "Concurrency",
|
|
500
|
+
default: 1,
|
|
501
|
+
description: "Maximum number of concurrent invocations of the trigger. For queue triggers, this will process queue items in parallel."
|
|
502
|
+
})),
|
|
503
|
+
/**
|
|
504
|
+
* Passed as the input data to the function.
|
|
505
|
+
* Must match the function's input schema.
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
508
|
+
* {
|
|
509
|
+
* item: '{{event.item}}',
|
|
510
|
+
* instructions: 'Summarize and perform entity-extraction'
|
|
511
|
+
* mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
|
|
512
|
+
* }
|
|
513
|
+
*/
|
|
514
|
+
input: Schema3.optional(Schema3.Record({
|
|
515
|
+
key: Schema3.String,
|
|
516
|
+
value: Schema3.Any
|
|
517
|
+
}))
|
|
518
|
+
}).pipe(Type3.object({
|
|
519
|
+
typename: "org.dxos.type.trigger",
|
|
520
|
+
version: "0.1.0"
|
|
521
|
+
}), Annotation2.IconAnnotation.set({
|
|
522
|
+
icon: "ph--lightning--regular",
|
|
523
|
+
hue: "yellow"
|
|
524
|
+
}), SystemTypeAnnotation.set(true));
|
|
525
|
+
var Trigger = TriggerSchema;
|
|
526
|
+
var make3 = (props) => Obj4.make(Trigger, props);
|
|
527
|
+
|
|
528
|
+
// src/types/TriggerEvent.ts
|
|
529
|
+
var TriggerEvent_exports = {};
|
|
530
|
+
__export(TriggerEvent_exports, {
|
|
531
|
+
EmailEvent: () => EmailEvent,
|
|
532
|
+
QueueEvent: () => QueueEvent,
|
|
533
|
+
SubscriptionEvent: () => SubscriptionEvent,
|
|
534
|
+
TimerEvent: () => TimerEvent,
|
|
535
|
+
TriggerEvent: () => TriggerEvent,
|
|
536
|
+
WebhookEvent: () => WebhookEvent
|
|
537
|
+
});
|
|
538
|
+
import * as Schema4 from "effect/Schema";
|
|
539
|
+
import { DXN as DXN2, Obj as Obj5, Ref as Ref3 } from "@dxos/echo";
|
|
540
|
+
var EmailEvent = Schema4.Struct({
|
|
541
|
+
from: Schema4.String,
|
|
542
|
+
to: Schema4.String,
|
|
543
|
+
subject: Schema4.String,
|
|
544
|
+
created: Schema4.String,
|
|
545
|
+
body: Schema4.String
|
|
546
|
+
});
|
|
547
|
+
var QueueEvent = Schema4.Struct({
|
|
548
|
+
queue: DXN2.Schema,
|
|
549
|
+
item: Schema4.Any,
|
|
550
|
+
cursor: Schema4.String
|
|
551
|
+
});
|
|
552
|
+
var SubscriptionEvent = Schema4.Struct({
|
|
553
|
+
/**
|
|
554
|
+
* Type of the mutation.
|
|
555
|
+
*/
|
|
556
|
+
// TODO(dmaretskyi): Specify enum.
|
|
557
|
+
type: Schema4.String,
|
|
558
|
+
/**
|
|
559
|
+
* Reference to the object that was changed or created.
|
|
560
|
+
*/
|
|
561
|
+
subject: Ref3.Ref(Obj5.Unknown),
|
|
562
|
+
/**
|
|
563
|
+
* @deprecated
|
|
564
|
+
*/
|
|
565
|
+
changedObjectId: Schema4.optional(Schema4.String)
|
|
566
|
+
});
|
|
567
|
+
var TimerEvent = Schema4.Struct({
|
|
568
|
+
tick: Schema4.Number
|
|
569
|
+
});
|
|
570
|
+
var WebhookEvent = Schema4.Struct({
|
|
571
|
+
url: Schema4.String,
|
|
572
|
+
method: Schema4.Literal("GET", "POST"),
|
|
573
|
+
headers: Schema4.Record({
|
|
574
|
+
key: Schema4.String,
|
|
575
|
+
value: Schema4.String
|
|
576
|
+
}),
|
|
577
|
+
bodyText: Schema4.String
|
|
578
|
+
});
|
|
579
|
+
var TriggerEvent = Schema4.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
|
|
580
|
+
|
|
581
|
+
// src/types/url.ts
|
|
582
|
+
var FUNCTIONS_META_KEY = "org.dxos.service.function";
|
|
583
|
+
var FUNCTIONS_PRESET_META_KEY = "org.dxos.service.function-preset";
|
|
584
|
+
var getUserFunctionIdInMetadata = (meta) => {
|
|
585
|
+
return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
|
|
586
|
+
};
|
|
587
|
+
var setUserFunctionIdInMetadata = (meta, functionId) => {
|
|
588
|
+
const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
|
|
589
|
+
if (key) {
|
|
590
|
+
if (key.id !== functionId) {
|
|
591
|
+
throw new Error("Metadata mismatch");
|
|
592
|
+
}
|
|
593
|
+
} else {
|
|
594
|
+
meta.keys.push({
|
|
595
|
+
source: FUNCTIONS_META_KEY,
|
|
596
|
+
id: functionId
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// src/protocol/protocol.ts
|
|
602
|
+
import * as AnthropicClient from "@effect/ai-anthropic/AnthropicClient";
|
|
603
|
+
import * as Effect7 from "effect/Effect";
|
|
604
|
+
import * as Layer7 from "effect/Layer";
|
|
605
|
+
import * as Schema5 from "effect/Schema";
|
|
606
|
+
import * as SchemaAST2 from "effect/SchemaAST";
|
|
607
|
+
import { AiModelResolver, AiService } from "@dxos/ai";
|
|
608
|
+
import { AnthropicResolver } from "@dxos/ai/resolvers";
|
|
609
|
+
import { LifecycleState, Resource } from "@dxos/context";
|
|
610
|
+
import { Database as Database2, Feed, JsonSchema, Ref as Ref4 } from "@dxos/echo";
|
|
611
|
+
import { refFromEncodedReference } from "@dxos/echo/internal";
|
|
612
|
+
import { EchoClient, createFeedServiceLayer } from "@dxos/echo-db";
|
|
613
|
+
import { runAndForwardErrors } from "@dxos/effect";
|
|
614
|
+
import { assertState, failedInvariant, invariant as invariant2 } from "@dxos/invariant";
|
|
615
|
+
import { PublicKey } from "@dxos/keys";
|
|
616
|
+
import { Operation } from "@dxos/operation";
|
|
617
|
+
|
|
618
|
+
// src/protocol/functions-ai-http-client.ts
|
|
619
|
+
import * as Headers from "@effect/platform/Headers";
|
|
620
|
+
import * as HttpClient2 from "@effect/platform/HttpClient";
|
|
621
|
+
import * as HttpClientError from "@effect/platform/HttpClientError";
|
|
622
|
+
import * as HttpClientResponse from "@effect/platform/HttpClientResponse";
|
|
623
|
+
import * as Effect6 from "effect/Effect";
|
|
624
|
+
import * as FiberRef from "effect/FiberRef";
|
|
625
|
+
import * as Layer6 from "effect/Layer";
|
|
626
|
+
import * as Stream from "effect/Stream";
|
|
627
|
+
import { log as log2 } from "@dxos/log";
|
|
628
|
+
import { ErrorCodec } from "@dxos/protocols";
|
|
629
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/protocol/functions-ai-http-client.ts";
|
|
630
|
+
var requestInitTagKey = "@effect/platform/FetchHttpClient/FetchOptions";
|
|
631
|
+
var FunctionsAiHttpClient = class _FunctionsAiHttpClient {
|
|
632
|
+
static make = (service) => HttpClient2.make((request, url, signal, fiber) => {
|
|
633
|
+
const context = fiber.getFiberRef(FiberRef.currentContext);
|
|
634
|
+
const options = context.unsafeMap.get(requestInitTagKey) ?? {};
|
|
635
|
+
const headers = options.headers ? Headers.merge(Headers.fromInput(options.headers), request.headers) : request.headers;
|
|
636
|
+
const send = (body) => Effect6.tryPromise({
|
|
637
|
+
try: () => service.fetch(new Request(url, {
|
|
638
|
+
...options,
|
|
639
|
+
method: request.method,
|
|
640
|
+
headers,
|
|
641
|
+
body
|
|
642
|
+
})),
|
|
643
|
+
catch: (cause) => {
|
|
644
|
+
log2.error("Failed to fetch", {
|
|
645
|
+
errorSerialized: ErrorCodec.encode(cause)
|
|
646
|
+
}, {
|
|
647
|
+
F: __dxlog_file2,
|
|
648
|
+
L: 43,
|
|
649
|
+
S: this,
|
|
650
|
+
C: (f, a) => f(...a)
|
|
651
|
+
});
|
|
652
|
+
return new HttpClientError.RequestError({
|
|
653
|
+
request,
|
|
654
|
+
reason: "Transport",
|
|
655
|
+
cause
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
}).pipe(Effect6.map((response) => HttpClientResponse.fromWeb(request, response)));
|
|
659
|
+
switch (request.body._tag) {
|
|
660
|
+
case "Raw":
|
|
661
|
+
case "Uint8Array":
|
|
662
|
+
return send(request.body.body);
|
|
663
|
+
case "FormData":
|
|
664
|
+
return send(request.body.formData);
|
|
665
|
+
case "Stream":
|
|
666
|
+
return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect6.flatMap(send));
|
|
667
|
+
}
|
|
668
|
+
return send(void 0);
|
|
669
|
+
});
|
|
670
|
+
static layer = (service) => Layer6.succeed(HttpClient2.HttpClient, _FunctionsAiHttpClient.make(service));
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
// src/protocol/protocol.ts
|
|
674
|
+
function _ts_add_disposable_resource(env, value2, async) {
|
|
675
|
+
if (value2 !== null && value2 !== void 0) {
|
|
676
|
+
if (typeof value2 !== "object" && typeof value2 !== "function") throw new TypeError("Object expected.");
|
|
677
|
+
var dispose, inner;
|
|
678
|
+
if (async) {
|
|
679
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
680
|
+
dispose = value2[Symbol.asyncDispose];
|
|
681
|
+
}
|
|
682
|
+
if (dispose === void 0) {
|
|
683
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
684
|
+
dispose = value2[Symbol.dispose];
|
|
685
|
+
if (async) inner = dispose;
|
|
686
|
+
}
|
|
687
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
688
|
+
if (inner) dispose = function() {
|
|
689
|
+
try {
|
|
690
|
+
inner.call(this);
|
|
691
|
+
} catch (e) {
|
|
692
|
+
return Promise.reject(e);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
env.stack.push({
|
|
696
|
+
value: value2,
|
|
697
|
+
dispose,
|
|
698
|
+
async
|
|
699
|
+
});
|
|
700
|
+
} else if (async) {
|
|
701
|
+
env.stack.push({
|
|
702
|
+
async: true
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
return value2;
|
|
706
|
+
}
|
|
707
|
+
function _ts_dispose_resources(env) {
|
|
708
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
709
|
+
var e = new Error(message);
|
|
710
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
711
|
+
};
|
|
712
|
+
return (_ts_dispose_resources = function _ts_dispose_resources2(env2) {
|
|
713
|
+
function fail(e) {
|
|
714
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
715
|
+
env2.hasError = true;
|
|
716
|
+
}
|
|
717
|
+
var r, s = 0;
|
|
718
|
+
function next() {
|
|
719
|
+
while (r = env2.stack.pop()) {
|
|
720
|
+
try {
|
|
721
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
722
|
+
if (r.dispose) {
|
|
723
|
+
var result = r.dispose.call(r.value);
|
|
724
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
725
|
+
fail(e);
|
|
726
|
+
return next();
|
|
727
|
+
});
|
|
728
|
+
} else s |= 1;
|
|
729
|
+
} catch (e) {
|
|
730
|
+
fail(e);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
734
|
+
if (env2.hasError) throw env2.error;
|
|
735
|
+
}
|
|
736
|
+
return next();
|
|
737
|
+
})(env);
|
|
738
|
+
}
|
|
739
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/protocol/protocol.ts";
|
|
740
|
+
var wrapFunctionHandler = (func) => {
|
|
741
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
742
|
+
throw new TypeError("Expected operation with handler");
|
|
743
|
+
}
|
|
744
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
745
|
+
return {
|
|
746
|
+
meta: {
|
|
747
|
+
key: func.meta.key,
|
|
748
|
+
name: func.meta.name,
|
|
749
|
+
description: func.meta.description,
|
|
750
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
751
|
+
outputSchema: func.output === void 0 ? void 0 : JsonSchema.toJsonSchema(func.output),
|
|
752
|
+
services: func.services.map((service) => service.key)
|
|
753
|
+
},
|
|
754
|
+
handler: async ({ data, context }) => {
|
|
755
|
+
if ((serviceTags.includes(Database2.Service.key) || serviceTags.includes(QueueService.key) || serviceTags.includes(Feed.Service.key)) && (!context.services.dataService || !context.services.queryService)) {
|
|
756
|
+
throw new FunctionError({
|
|
757
|
+
message: "Services not provided: dataService, queryService"
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
try {
|
|
761
|
+
const env = {
|
|
762
|
+
stack: [],
|
|
763
|
+
error: void 0,
|
|
764
|
+
hasError: false
|
|
765
|
+
};
|
|
766
|
+
try {
|
|
767
|
+
if (!SchemaAST2.isAnyKeyword(func.input.ast)) {
|
|
768
|
+
try {
|
|
769
|
+
Schema5.validateSync(func.input)(data);
|
|
770
|
+
} catch (error) {
|
|
771
|
+
throw new FunctionError({
|
|
772
|
+
message: "Invalid input schema",
|
|
773
|
+
cause: error
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
const funcContext = _ts_add_disposable_resource(env, await new FunctionContext(context).open(), true);
|
|
778
|
+
if (func.types.length > 0) {
|
|
779
|
+
invariant2(funcContext.db, "Database is required for functions with types", {
|
|
780
|
+
F: __dxlog_file3,
|
|
781
|
+
L: 73,
|
|
782
|
+
S: void 0,
|
|
783
|
+
A: [
|
|
784
|
+
"funcContext.db",
|
|
785
|
+
"'Database is required for functions with types'"
|
|
786
|
+
]
|
|
787
|
+
});
|
|
788
|
+
await funcContext.db.graph.schemaRegistry.register(func.types);
|
|
789
|
+
}
|
|
790
|
+
const dataWithDecodedRefs = funcContext.db && !SchemaAST2.isAnyKeyword(func.input.ast) ? decodeRefsFromSchema(func.input.ast, data, funcContext.db) : data;
|
|
791
|
+
let result = await func.handler(dataWithDecodedRefs);
|
|
792
|
+
if (Effect7.isEffect(result)) {
|
|
793
|
+
result = await runAndForwardErrors(result.pipe(Effect7.orDie, Effect7.provide(funcContext.createLayer())));
|
|
794
|
+
}
|
|
795
|
+
if (func.output && !SchemaAST2.isAnyKeyword(func.output.ast)) {
|
|
796
|
+
Schema5.validateSync(func.output)(result);
|
|
797
|
+
}
|
|
798
|
+
return result;
|
|
799
|
+
} catch (e) {
|
|
800
|
+
env.error = e;
|
|
801
|
+
env.hasError = true;
|
|
802
|
+
} finally {
|
|
803
|
+
const result = _ts_dispose_resources(env);
|
|
804
|
+
if (result) await result;
|
|
805
|
+
}
|
|
806
|
+
} catch (error) {
|
|
807
|
+
throw error;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
};
|
|
812
|
+
var FunctionContext = class extends Resource {
|
|
813
|
+
context;
|
|
814
|
+
client;
|
|
815
|
+
db;
|
|
816
|
+
queues;
|
|
817
|
+
constructor(context) {
|
|
818
|
+
super();
|
|
819
|
+
this.context = context;
|
|
820
|
+
if (context.services.dataService && context.services.queryService) {
|
|
821
|
+
this.client = new EchoClient().connectToService({
|
|
822
|
+
dataService: context.services.dataService,
|
|
823
|
+
queryService: context.services.queryService,
|
|
824
|
+
queueService: context.services.queueService
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
async _open() {
|
|
829
|
+
await this.client?.open();
|
|
830
|
+
this.db = this.client && this.context.spaceId ? this.client.constructDatabase({
|
|
831
|
+
spaceId: this.context.spaceId ?? failedInvariant(),
|
|
832
|
+
spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant("spaceKey missing in context")),
|
|
833
|
+
reactiveSchemaQuery: false,
|
|
834
|
+
preloadSchemaOnOpen: false
|
|
835
|
+
}) : void 0;
|
|
836
|
+
await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant("spaceRootUrl missing in context"));
|
|
837
|
+
await this.db?.open();
|
|
838
|
+
this.queues = this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : void 0;
|
|
839
|
+
}
|
|
840
|
+
async _close() {
|
|
841
|
+
await this.db?.close();
|
|
842
|
+
await this.client?.close();
|
|
843
|
+
}
|
|
844
|
+
createLayer() {
|
|
845
|
+
assertState(this._lifecycleState === LifecycleState.OPEN, "FunctionContext is not open");
|
|
846
|
+
const dbLayer = this.db ? Database2.layer(this.db) : Database2.notAvailable;
|
|
847
|
+
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
848
|
+
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
849
|
+
const credentials = dbLayer ? CredentialsService.layerFromDatabase({
|
|
850
|
+
caching: true
|
|
851
|
+
}).pipe(Layer7.provide(dbLayer)) : CredentialsService.configuredLayer([]);
|
|
852
|
+
const functionInvocationService = MockedFunctionInvocationService;
|
|
853
|
+
const tracing = TracingService.layerNoop;
|
|
854
|
+
const aiLayer = this.context.services.functionsAiService ? AiModelResolver.AiModelResolver.buildAiService.pipe(Layer7.provide(AnthropicResolver.make().pipe(Layer7.provide(AnthropicClient.layer({
|
|
855
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
856
|
+
apiUrl: "http://internal/provider/anthropic"
|
|
857
|
+
}).pipe(Layer7.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))))))) : AiService.notAvailable;
|
|
858
|
+
return Layer7.mergeAll(dbLayer, queuesLayer, feedLayer, credentials, functionInvocationService, aiLayer, tracing);
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
var MockedFunctionInvocationService = Layer7.succeed(FunctionInvocationService, {
|
|
862
|
+
invokeFunction: () => Effect7.die("Calling functions from functions is not implemented yet."),
|
|
863
|
+
resolveFunction: () => Effect7.die("Not implemented.")
|
|
864
|
+
});
|
|
865
|
+
var decodeRefsFromSchema = (ast, value2, db) => {
|
|
866
|
+
if (value2 == null) {
|
|
867
|
+
return value2;
|
|
868
|
+
}
|
|
869
|
+
const encoded = SchemaAST2.encodedBoundAST(ast);
|
|
870
|
+
if (Ref4.isRefType(encoded)) {
|
|
871
|
+
if (Ref4.isRef(value2)) {
|
|
872
|
+
return value2;
|
|
873
|
+
}
|
|
874
|
+
if (typeof value2 === "object" && value2 !== null && typeof value2["/"] === "string") {
|
|
875
|
+
const resolver = db.graph.createRefResolver({
|
|
876
|
+
context: {
|
|
877
|
+
space: db.spaceId
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
return refFromEncodedReference(value2, resolver);
|
|
881
|
+
}
|
|
882
|
+
return value2;
|
|
883
|
+
}
|
|
884
|
+
switch (encoded._tag) {
|
|
885
|
+
case "TypeLiteral": {
|
|
886
|
+
if (typeof value2 !== "object" || value2 === null || Array.isArray(value2)) {
|
|
887
|
+
return value2;
|
|
888
|
+
}
|
|
889
|
+
const result = {
|
|
890
|
+
...value2
|
|
891
|
+
};
|
|
892
|
+
for (const prop of SchemaAST2.getPropertySignatures(encoded)) {
|
|
893
|
+
const key = prop.name.toString();
|
|
894
|
+
if (key in result) {
|
|
895
|
+
result[key] = decodeRefsFromSchema(prop.type, result[key], db);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
return result;
|
|
899
|
+
}
|
|
900
|
+
case "TupleType": {
|
|
901
|
+
if (!Array.isArray(value2)) {
|
|
902
|
+
return value2;
|
|
903
|
+
}
|
|
904
|
+
if (encoded.elements.length === 0 && encoded.rest.length === 1) {
|
|
905
|
+
const elementType = encoded.rest[0].type;
|
|
906
|
+
return value2.map((item) => decodeRefsFromSchema(elementType, item, db));
|
|
907
|
+
}
|
|
908
|
+
return value2;
|
|
909
|
+
}
|
|
910
|
+
case "Union": {
|
|
911
|
+
const nonUndefined = encoded.types.filter((t) => !SchemaAST2.isUndefinedKeyword(t));
|
|
912
|
+
if (nonUndefined.length === 1) {
|
|
913
|
+
return decodeRefsFromSchema(nonUndefined[0], value2, db);
|
|
914
|
+
}
|
|
915
|
+
return value2;
|
|
916
|
+
}
|
|
917
|
+
case "Suspend": {
|
|
918
|
+
return decodeRefsFromSchema(encoded.f(), value2, db);
|
|
919
|
+
}
|
|
920
|
+
case "Refinement": {
|
|
921
|
+
return decodeRefsFromSchema(encoded.from, value2, db);
|
|
922
|
+
}
|
|
923
|
+
default: {
|
|
924
|
+
return value2;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
export {
|
|
929
|
+
ComputeEvent,
|
|
930
|
+
ComputeEventLogger,
|
|
931
|
+
ComputeEventPayload,
|
|
932
|
+
ConfiguredCredentialsService,
|
|
933
|
+
ContextQueueService,
|
|
934
|
+
CredentialsService,
|
|
935
|
+
ExampleHandlers,
|
|
936
|
+
FUNCTIONS_META_KEY,
|
|
937
|
+
FUNCTIONS_PRESET_META_KEY,
|
|
938
|
+
Fibonacci,
|
|
939
|
+
FunctionError,
|
|
940
|
+
FunctionInvocationService,
|
|
941
|
+
FunctionNotFoundError,
|
|
942
|
+
MESSAGE_PROPERTY_TOOL_CALL_ID,
|
|
943
|
+
QueueService,
|
|
944
|
+
Reply,
|
|
945
|
+
Script_exports as Script,
|
|
946
|
+
ServiceNotAvailableError,
|
|
947
|
+
Sleep,
|
|
948
|
+
TracingService,
|
|
949
|
+
Trigger_exports as Trigger,
|
|
950
|
+
TriggerEvent_exports as TriggerEvent,
|
|
951
|
+
TriggerStateNotFoundError,
|
|
952
|
+
createDefectLogger,
|
|
953
|
+
createEventLogger,
|
|
954
|
+
getUserFunctionIdInMetadata,
|
|
955
|
+
logCustomEvent,
|
|
956
|
+
setUserFunctionIdInMetadata,
|
|
957
|
+
withAuthorization,
|
|
958
|
+
wrapFunctionHandler
|
|
959
|
+
};
|
|
960
|
+
//# sourceMappingURL=index.mjs.map
|