@dxos/functions 0.8.4-main.72ec0f3 → 0.8.4-main.8342792842
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 → neutral}/index.mjs +450 -148
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/errors.d.ts +24 -32
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/operation-compatibility.test.d.ts +2 -0
- package/dist/types/src/operation-compatibility.test.d.ts.map +1 -0
- 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.map +1 -1
- package/dist/types/src/sdk.d.ts +27 -2
- 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 +5 -0
- 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 +37 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +40 -46
- package/dist/types/src/types/Function.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 +58 -76
- package/dist/types/src/types/Trigger.d.ts.map +1 -1
- package/dist/types/src/types/TriggerEvent.d.ts +43 -13
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +4 -3
- 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/operation-compatibility.test.ts +185 -0
- package/src/protocol/functions-ai-http-client.ts +67 -0
- package/src/protocol/protocol.ts +184 -67
- package/src/sdk.ts +68 -5
- package/src/services/credentials.ts +31 -15
- package/src/services/event-logger.ts +2 -2
- package/src/services/function-invocation-service.ts +14 -0
- package/src/services/index.ts +0 -2
- package/src/services/queues.ts +5 -7
- package/src/services/tracing.ts +63 -4
- package/src/types/Function.ts +28 -8
- package/src/types/Script.ts +8 -7
- package/src/types/Trigger.ts +18 -14
- package/src/types/TriggerEvent.ts +29 -29
- package/src/types/url.ts +4 -3
- 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
|
@@ -6,7 +6,7 @@ var __export = (target, all) => {
|
|
|
6
6
|
|
|
7
7
|
// src/errors.ts
|
|
8
8
|
import { BaseError } from "@dxos/errors";
|
|
9
|
-
var ServiceNotAvailableError = class extends BaseError.extend("
|
|
9
|
+
var ServiceNotAvailableError = class extends BaseError.extend("ServiceNotAvailable", "Service not available") {
|
|
10
10
|
constructor(service, options) {
|
|
11
11
|
super({
|
|
12
12
|
context: {
|
|
@@ -16,7 +16,7 @@ var ServiceNotAvailableError = class extends BaseError.extend("SERVICE_NOT_AVAIL
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
|
-
var FunctionNotFoundError = class extends BaseError.extend("
|
|
19
|
+
var FunctionNotFoundError = class extends BaseError.extend("FunctionNotFound", "Function not found") {
|
|
20
20
|
constructor(functionKey, options) {
|
|
21
21
|
super({
|
|
22
22
|
context: {
|
|
@@ -26,9 +26,9 @@ var FunctionNotFoundError = class extends BaseError.extend("FUNCTION_NOT_FOUND",
|
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
|
-
var FunctionError = class extends BaseError.extend("
|
|
29
|
+
var FunctionError = class extends BaseError.extend("FunctionError", "Function invocation error") {
|
|
30
30
|
};
|
|
31
|
-
var TriggerStateNotFoundError = class extends BaseError.extend("
|
|
31
|
+
var TriggerStateNotFoundError = class extends BaseError.extend("TriggerStateNotFound", "Trigger state not found") {
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
// src/example/fib.ts
|
|
@@ -38,18 +38,20 @@ import * as Schema6 from "effect/Schema";
|
|
|
38
38
|
// src/sdk.ts
|
|
39
39
|
import * as Effect from "effect/Effect";
|
|
40
40
|
import * as Schema5 from "effect/Schema";
|
|
41
|
-
import { Obj as
|
|
41
|
+
import { Obj as Obj4, Type as Type5 } from "@dxos/echo";
|
|
42
42
|
import { assertArgument, failedInvariant } from "@dxos/invariant";
|
|
43
|
+
import { Operation } from "@dxos/operation";
|
|
43
44
|
|
|
44
45
|
// src/types/Function.ts
|
|
45
46
|
var Function_exports = {};
|
|
46
47
|
__export(Function_exports, {
|
|
47
48
|
Function: () => Function,
|
|
48
|
-
make: () => make2
|
|
49
|
+
make: () => make2,
|
|
50
|
+
setFrom: () => setFrom
|
|
49
51
|
});
|
|
50
52
|
import * as Schema2 from "effect/Schema";
|
|
51
|
-
import { Obj as Obj2, Type as Type2 } from "@dxos/echo";
|
|
52
|
-
import {
|
|
53
|
+
import { Annotation as Annotation2, JsonSchema, Obj as Obj2, Type as Type2 } from "@dxos/echo";
|
|
54
|
+
import { SystemTypeAnnotation } from "@dxos/echo/internal";
|
|
53
55
|
|
|
54
56
|
// src/types/Script.ts
|
|
55
57
|
var Script_exports = {};
|
|
@@ -58,20 +60,20 @@ __export(Script_exports, {
|
|
|
58
60
|
make: () => make
|
|
59
61
|
});
|
|
60
62
|
import * as Schema from "effect/Schema";
|
|
61
|
-
import { Obj, Ref, Type } from "@dxos/echo";
|
|
62
|
-
import {
|
|
63
|
+
import { Annotation, Obj, Ref, Type } from "@dxos/echo";
|
|
64
|
+
import { FormInputAnnotation } from "@dxos/echo/internal";
|
|
63
65
|
import { Text } from "@dxos/schema";
|
|
64
66
|
var Script = Schema.Struct({
|
|
65
67
|
name: Schema.String.pipe(Schema.optional),
|
|
66
68
|
description: Schema.String.pipe(Schema.optional),
|
|
67
69
|
// TODO(burdon): Change to hash of deployed content.
|
|
68
70
|
// Whether source has changed since last deploy.
|
|
69
|
-
changed: Schema.Boolean.pipe(
|
|
70
|
-
source: Type.Ref(Text.Text).pipe(
|
|
71
|
-
}).pipe(Type.
|
|
71
|
+
changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
|
|
72
|
+
source: Type.Ref(Text.Text).pipe(FormInputAnnotation.set(false))
|
|
73
|
+
}).pipe(Type.object({
|
|
72
74
|
typename: "dxos.org/type/Script",
|
|
73
75
|
version: "0.1.0"
|
|
74
|
-
}), LabelAnnotation.set([
|
|
76
|
+
}), Annotation.LabelAnnotation.set([
|
|
75
77
|
"name"
|
|
76
78
|
]));
|
|
77
79
|
var make = ({ source = "", ...props } = {}) => Obj.make(Script, {
|
|
@@ -91,7 +93,6 @@ var Function = Schema2.Struct({
|
|
|
91
93
|
key: Schema2.optional(Schema2.String).annotations({
|
|
92
94
|
description: "Unique registration key for the blueprint"
|
|
93
95
|
}),
|
|
94
|
-
// TODO(burdon): Rename to id/uri?
|
|
95
96
|
name: Schema2.NonEmptyString,
|
|
96
97
|
version: Schema2.String,
|
|
97
98
|
description: Schema2.optional(Schema2.String),
|
|
@@ -101,9 +102,9 @@ var Function = Schema2.Struct({
|
|
|
101
102
|
updated: Schema2.optional(Schema2.String),
|
|
102
103
|
// Reference to a source script if it exists within ECHO.
|
|
103
104
|
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
104
|
-
source: Schema2.optional(
|
|
105
|
-
inputSchema: Schema2.optional(
|
|
106
|
-
outputSchema: Schema2.optional(
|
|
105
|
+
source: Schema2.optional(Type2.Ref(Script)),
|
|
106
|
+
inputSchema: Schema2.optional(JsonSchema.JsonSchema),
|
|
107
|
+
outputSchema: Schema2.optional(JsonSchema.JsonSchema),
|
|
107
108
|
/**
|
|
108
109
|
* List of required services.
|
|
109
110
|
* Match the Context.Tag keys of the FunctionServices variants.
|
|
@@ -111,13 +112,25 @@ var Function = Schema2.Struct({
|
|
|
111
112
|
services: Schema2.optional(Schema2.Array(Schema2.String)),
|
|
112
113
|
// Local binding to a function name.
|
|
113
114
|
binding: Schema2.optional(Schema2.String)
|
|
114
|
-
}).pipe(Type2.
|
|
115
|
+
}).pipe(Type2.object({
|
|
115
116
|
typename: "dxos.org/type/Function",
|
|
116
117
|
version: "0.1.0"
|
|
117
|
-
}),
|
|
118
|
+
}), Annotation2.LabelAnnotation.set([
|
|
118
119
|
"name"
|
|
119
|
-
]));
|
|
120
|
+
]), SystemTypeAnnotation.set(true));
|
|
120
121
|
var make2 = (props) => Obj2.make(Function, props);
|
|
122
|
+
var setFrom = (target, source) => {
|
|
123
|
+
Obj2.change(target, (t) => {
|
|
124
|
+
t.key = source.key ?? target.key;
|
|
125
|
+
t.name = source.name ?? target.name;
|
|
126
|
+
t.version = source.version;
|
|
127
|
+
t.description = source.description;
|
|
128
|
+
t.updated = source.updated;
|
|
129
|
+
t.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : void 0;
|
|
130
|
+
t.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : void 0;
|
|
131
|
+
Obj2.getMeta(t).keys = JSON.parse(JSON.stringify(Obj2.getMeta(source).keys));
|
|
132
|
+
});
|
|
133
|
+
};
|
|
121
134
|
|
|
122
135
|
// src/types/Trigger.ts
|
|
123
136
|
var Trigger_exports = {};
|
|
@@ -135,8 +148,9 @@ __export(Trigger_exports, {
|
|
|
135
148
|
import * as Schema3 from "effect/Schema";
|
|
136
149
|
import * as SchemaAST from "effect/SchemaAST";
|
|
137
150
|
import { Obj as Obj3, QueryAST, Type as Type3 } from "@dxos/echo";
|
|
138
|
-
import {
|
|
151
|
+
import { OptionsAnnotationId, SystemTypeAnnotation as SystemTypeAnnotation2 } from "@dxos/echo/internal";
|
|
139
152
|
import { DXN } from "@dxos/keys";
|
|
153
|
+
import { Expando } from "@dxos/schema";
|
|
140
154
|
var Kinds = [
|
|
141
155
|
"email",
|
|
142
156
|
"queue",
|
|
@@ -149,12 +163,12 @@ var kindLiteralAnnotations = {
|
|
|
149
163
|
};
|
|
150
164
|
var EmailSpec = Schema3.Struct({
|
|
151
165
|
kind: Schema3.Literal("email").annotations(kindLiteralAnnotations)
|
|
152
|
-
})
|
|
166
|
+
});
|
|
153
167
|
var QueueSpec = Schema3.Struct({
|
|
154
168
|
kind: Schema3.Literal("queue").annotations(kindLiteralAnnotations),
|
|
155
169
|
// TODO(dmaretskyi): Change to a reference.
|
|
156
170
|
queue: DXN.Schema
|
|
157
|
-
})
|
|
171
|
+
});
|
|
158
172
|
var SubscriptionSpec = Schema3.Struct({
|
|
159
173
|
kind: Schema3.Literal("subscription").annotations(kindLiteralAnnotations),
|
|
160
174
|
query: Schema3.Struct({
|
|
@@ -162,7 +176,7 @@ var SubscriptionSpec = Schema3.Struct({
|
|
|
162
176
|
title: "Query"
|
|
163
177
|
})),
|
|
164
178
|
ast: QueryAST.Query
|
|
165
|
-
})
|
|
179
|
+
}),
|
|
166
180
|
options: Schema3.optional(Schema3.Struct({
|
|
167
181
|
// Watch changes to object (not just creation).
|
|
168
182
|
deep: Schema3.optional(Schema3.Boolean.annotations({
|
|
@@ -175,7 +189,7 @@ var SubscriptionSpec = Schema3.Struct({
|
|
|
175
189
|
}).annotations({
|
|
176
190
|
title: "Options"
|
|
177
191
|
}))
|
|
178
|
-
})
|
|
192
|
+
});
|
|
179
193
|
var TimerSpec = Schema3.Struct({
|
|
180
194
|
kind: Schema3.Literal("timer").annotations(kindLiteralAnnotations),
|
|
181
195
|
cron: Schema3.String.annotations({
|
|
@@ -184,7 +198,7 @@ var TimerSpec = Schema3.Struct({
|
|
|
184
198
|
"0 0 * * *"
|
|
185
199
|
]
|
|
186
200
|
})
|
|
187
|
-
})
|
|
201
|
+
});
|
|
188
202
|
var WebhookSpec = Schema3.Struct({
|
|
189
203
|
kind: Schema3.Literal("webhook").annotations(kindLiteralAnnotations),
|
|
190
204
|
method: Schema3.optional(Schema3.String.annotations({
|
|
@@ -197,16 +211,16 @@ var WebhookSpec = Schema3.Struct({
|
|
|
197
211
|
port: Schema3.optional(Schema3.Number.annotations({
|
|
198
212
|
title: "Port"
|
|
199
213
|
}))
|
|
200
|
-
})
|
|
214
|
+
});
|
|
201
215
|
var Spec = Schema3.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
|
|
202
216
|
title: "Trigger"
|
|
203
217
|
});
|
|
204
|
-
var
|
|
218
|
+
var TriggerSchema = Schema3.Struct({
|
|
205
219
|
/**
|
|
206
220
|
* Function or workflow to invoke.
|
|
207
221
|
*/
|
|
208
222
|
// TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
|
|
209
|
-
function: Schema3.optional(
|
|
223
|
+
function: Schema3.optional(Type3.Ref(Expando.Expando).annotations({
|
|
210
224
|
title: "Function"
|
|
211
225
|
})),
|
|
212
226
|
/**
|
|
@@ -217,6 +231,8 @@ var Trigger_ = Schema3.Struct({
|
|
|
217
231
|
inputNodeId: Schema3.optional(Schema3.String.annotations({
|
|
218
232
|
title: "Input Node ID"
|
|
219
233
|
})),
|
|
234
|
+
// TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
|
|
235
|
+
// Need lint rule; or agent rule to require PR review for "boolean" key word.
|
|
220
236
|
enabled: Schema3.optional(Schema3.Boolean.annotations({
|
|
221
237
|
title: "Enabled"
|
|
222
238
|
})),
|
|
@@ -232,15 +248,15 @@ var Trigger_ = Schema3.Struct({
|
|
|
232
248
|
* mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
|
|
233
249
|
* }
|
|
234
250
|
*/
|
|
235
|
-
input: Schema3.optional(Schema3.
|
|
251
|
+
input: Schema3.optional(Schema3.Record({
|
|
236
252
|
key: Schema3.String,
|
|
237
253
|
value: Schema3.Any
|
|
238
|
-
}))
|
|
239
|
-
}).pipe(Type3.
|
|
254
|
+
}))
|
|
255
|
+
}).pipe(Type3.object({
|
|
240
256
|
typename: "dxos.org/type/Trigger",
|
|
241
257
|
version: "0.1.0"
|
|
242
|
-
}));
|
|
243
|
-
var Trigger =
|
|
258
|
+
}), SystemTypeAnnotation2.set(true));
|
|
259
|
+
var Trigger = TriggerSchema;
|
|
244
260
|
var make3 = (props) => Obj3.make(Trigger, props);
|
|
245
261
|
|
|
246
262
|
// src/types/TriggerEvent.ts
|
|
@@ -250,22 +266,23 @@ __export(TriggerEvent_exports, {
|
|
|
250
266
|
QueueEvent: () => QueueEvent,
|
|
251
267
|
SubscriptionEvent: () => SubscriptionEvent,
|
|
252
268
|
TimerEvent: () => TimerEvent,
|
|
269
|
+
TriggerEvent: () => TriggerEvent,
|
|
253
270
|
WebhookEvent: () => WebhookEvent
|
|
254
271
|
});
|
|
255
272
|
import * as Schema4 from "effect/Schema";
|
|
256
|
-
import { DXN as DXN2,
|
|
257
|
-
var EmailEvent = Schema4.
|
|
273
|
+
import { DXN as DXN2, Type as Type4 } from "@dxos/echo";
|
|
274
|
+
var EmailEvent = Schema4.Struct({
|
|
258
275
|
from: Schema4.String,
|
|
259
276
|
to: Schema4.String,
|
|
260
277
|
subject: Schema4.String,
|
|
261
278
|
created: Schema4.String,
|
|
262
279
|
body: Schema4.String
|
|
263
|
-
})
|
|
264
|
-
var QueueEvent = Schema4.
|
|
280
|
+
});
|
|
281
|
+
var QueueEvent = Schema4.Struct({
|
|
265
282
|
queue: DXN2.Schema,
|
|
266
283
|
item: Schema4.Any,
|
|
267
284
|
cursor: Schema4.String
|
|
268
|
-
})
|
|
285
|
+
});
|
|
269
286
|
var SubscriptionEvent = Schema4.Struct({
|
|
270
287
|
/**
|
|
271
288
|
* Type of the mutation.
|
|
@@ -275,16 +292,16 @@ var SubscriptionEvent = Schema4.Struct({
|
|
|
275
292
|
/**
|
|
276
293
|
* Reference to the object that was changed or created.
|
|
277
294
|
*/
|
|
278
|
-
subject: Type4.Ref(
|
|
295
|
+
subject: Type4.Ref(Type4.Obj),
|
|
279
296
|
/**
|
|
280
297
|
* @deprecated
|
|
281
298
|
*/
|
|
282
299
|
changedObjectId: Schema4.optional(Schema4.String)
|
|
283
|
-
})
|
|
284
|
-
var TimerEvent = Schema4.
|
|
300
|
+
});
|
|
301
|
+
var TimerEvent = Schema4.Struct({
|
|
285
302
|
tick: Schema4.Number
|
|
286
|
-
})
|
|
287
|
-
var WebhookEvent = Schema4.
|
|
303
|
+
});
|
|
304
|
+
var WebhookEvent = Schema4.Struct({
|
|
288
305
|
url: Schema4.String,
|
|
289
306
|
method: Schema4.Literal("GET", "POST"),
|
|
290
307
|
headers: Schema4.Record({
|
|
@@ -292,7 +309,8 @@ var WebhookEvent = Schema4.mutable(Schema4.Struct({
|
|
|
292
309
|
value: Schema4.String
|
|
293
310
|
}),
|
|
294
311
|
bodyText: Schema4.String
|
|
295
|
-
})
|
|
312
|
+
});
|
|
313
|
+
var TriggerEvent = Schema4.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
|
|
296
314
|
|
|
297
315
|
// src/types/url.ts
|
|
298
316
|
var FUNCTIONS_META_KEY = "dxos.org/service/function";
|
|
@@ -316,7 +334,7 @@ var setUserFunctionIdInMetadata = (meta, functionId) => {
|
|
|
316
334
|
|
|
317
335
|
// src/sdk.ts
|
|
318
336
|
var typeId = Symbol.for("@dxos/functions/FunctionDefinition");
|
|
319
|
-
var defineFunction = ({ key, name, description, inputSchema, outputSchema = Schema5.Any, handler, services }) => {
|
|
337
|
+
var defineFunction = ({ key, name, description, inputSchema, outputSchema = Schema5.Any, handler, types, services }) => {
|
|
320
338
|
if (!Schema5.isSchema(inputSchema)) {
|
|
321
339
|
throw new Error("Input schema must be a valid schema");
|
|
322
340
|
}
|
|
@@ -357,6 +375,7 @@ var defineFunction = ({ key, name, description, inputSchema, outputSchema = Sche
|
|
|
357
375
|
inputSchema,
|
|
358
376
|
outputSchema,
|
|
359
377
|
handler: handlerWithSpan,
|
|
378
|
+
types: types ?? [],
|
|
360
379
|
services: !services ? [] : getServiceKeys(services)
|
|
361
380
|
};
|
|
362
381
|
};
|
|
@@ -365,10 +384,39 @@ var getServiceKeys = (services) => {
|
|
|
365
384
|
if (typeof tag.key === "string") {
|
|
366
385
|
return tag.key;
|
|
367
386
|
}
|
|
368
|
-
console.log(tag);
|
|
369
387
|
failedInvariant();
|
|
370
388
|
});
|
|
371
389
|
};
|
|
390
|
+
var toOperation = (functionDef) => {
|
|
391
|
+
const op = Operation.make({
|
|
392
|
+
schema: {
|
|
393
|
+
input: functionDef.inputSchema,
|
|
394
|
+
output: functionDef.outputSchema ?? Schema5.Any
|
|
395
|
+
},
|
|
396
|
+
meta: {
|
|
397
|
+
key: functionDef.key,
|
|
398
|
+
name: functionDef.name,
|
|
399
|
+
description: functionDef.description
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
const operationHandler = (input) => {
|
|
403
|
+
const result = functionDef.handler({
|
|
404
|
+
context: {},
|
|
405
|
+
data: input
|
|
406
|
+
});
|
|
407
|
+
if (Effect.isEffect(result)) {
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
410
|
+
if (result instanceof Promise) {
|
|
411
|
+
return Effect.tryPromise(() => result);
|
|
412
|
+
}
|
|
413
|
+
return Effect.succeed(result);
|
|
414
|
+
};
|
|
415
|
+
return {
|
|
416
|
+
...op,
|
|
417
|
+
handler: operationHandler
|
|
418
|
+
};
|
|
419
|
+
};
|
|
372
420
|
var FunctionDefinition = {
|
|
373
421
|
make: defineFunction,
|
|
374
422
|
isFunction: (value2) => {
|
|
@@ -379,9 +427,10 @@ var FunctionDefinition = {
|
|
|
379
427
|
return serializeFunction(functionDef);
|
|
380
428
|
},
|
|
381
429
|
deserialize: (functionObj) => {
|
|
382
|
-
assertArgument(
|
|
430
|
+
assertArgument(Obj4.instanceOf(Function_exports.Function, functionObj), "functionObj");
|
|
383
431
|
return deserializeFunction(functionObj);
|
|
384
|
-
}
|
|
432
|
+
},
|
|
433
|
+
toOperation
|
|
385
434
|
};
|
|
386
435
|
var serializeFunction = (functionDef) => {
|
|
387
436
|
const fn4 = Function_exports.make({
|
|
@@ -394,7 +443,7 @@ var serializeFunction = (functionDef) => {
|
|
|
394
443
|
services: functionDef.services
|
|
395
444
|
});
|
|
396
445
|
if (functionDef.meta?.deployedFunctionId) {
|
|
397
|
-
setUserFunctionIdInMetadata(
|
|
446
|
+
Obj4.change(fn4, (fn5) => setUserFunctionIdInMetadata(Obj4.getMeta(fn5), functionDef.meta.deployedFunctionId));
|
|
398
447
|
}
|
|
399
448
|
return fn4;
|
|
400
449
|
};
|
|
@@ -411,8 +460,9 @@ var deserializeFunction = (functionObj) => {
|
|
|
411
460
|
handler: () => {
|
|
412
461
|
},
|
|
413
462
|
services: functionObj.services ?? [],
|
|
463
|
+
types: [],
|
|
414
464
|
meta: {
|
|
415
|
-
deployedFunctionId: getUserFunctionIdInMetadata(
|
|
465
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj4.getMeta(functionObj))
|
|
416
466
|
}
|
|
417
467
|
};
|
|
418
468
|
};
|
|
@@ -489,9 +539,6 @@ var sleep_default = defineFunction({
|
|
|
489
539
|
})(Example || (Example = {}));
|
|
490
540
|
var Example;
|
|
491
541
|
|
|
492
|
-
// src/services/index.ts
|
|
493
|
-
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
494
|
-
|
|
495
542
|
// src/services/credentials.ts
|
|
496
543
|
import * as HttpClient from "@effect/platform/HttpClient";
|
|
497
544
|
import * as HttpClientRequest from "@effect/platform/HttpClientRequest";
|
|
@@ -500,7 +547,7 @@ import * as Effect5 from "effect/Effect";
|
|
|
500
547
|
import * as Layer from "effect/Layer";
|
|
501
548
|
import * as Redacted from "effect/Redacted";
|
|
502
549
|
import { Query } from "@dxos/echo";
|
|
503
|
-
import {
|
|
550
|
+
import { Database } from "@dxos/echo";
|
|
504
551
|
import { AccessToken } from "@dxos/types";
|
|
505
552
|
var CredentialsService = class _CredentialsService extends Context.Tag("@dxos/functions/CredentialsService")() {
|
|
506
553
|
static getCredential = (query) => Effect5.gen(function* () {
|
|
@@ -524,14 +571,23 @@ var CredentialsService = class _CredentialsService extends Context.Tag("@dxos/fu
|
|
|
524
571
|
}));
|
|
525
572
|
return new ConfiguredCredentialsService(serviceCredentials);
|
|
526
573
|
}));
|
|
527
|
-
static layerFromDatabase = () => Layer.effect(_CredentialsService, Effect5.gen(function* () {
|
|
528
|
-
const dbService = yield*
|
|
574
|
+
static layerFromDatabase = ({ caching = false } = {}) => Layer.effect(_CredentialsService, Effect5.gen(function* () {
|
|
575
|
+
const dbService = yield* Database.Service;
|
|
576
|
+
const cache = /* @__PURE__ */ new Map();
|
|
529
577
|
const queryCredentials = async (query) => {
|
|
530
|
-
const
|
|
531
|
-
|
|
578
|
+
const cacheKey = JSON.stringify(query);
|
|
579
|
+
if (caching && cache.has(cacheKey)) {
|
|
580
|
+
return cache.get(cacheKey);
|
|
581
|
+
}
|
|
582
|
+
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
583
|
+
const credentials = accessTokens.filter((accessToken) => accessToken.source === query.service).map((accessToken) => ({
|
|
532
584
|
service: accessToken.source,
|
|
533
585
|
apiKey: accessToken.token
|
|
534
586
|
}));
|
|
587
|
+
if (caching) {
|
|
588
|
+
cache.set(cacheKey, credentials);
|
|
589
|
+
}
|
|
590
|
+
return credentials;
|
|
535
591
|
};
|
|
536
592
|
return {
|
|
537
593
|
getCredential: async (query) => {
|
|
@@ -567,18 +623,17 @@ var ConfiguredCredentialsService = class {
|
|
|
567
623
|
return credential;
|
|
568
624
|
}
|
|
569
625
|
};
|
|
570
|
-
var withAuthorization = (
|
|
571
|
-
const
|
|
572
|
-
const authorization = kind ? `${kind} ${key}` : key;
|
|
626
|
+
var withAuthorization = (token, kind) => HttpClient.mapRequest((request) => {
|
|
627
|
+
const authorization = kind ? `${kind} ${token}` : token;
|
|
573
628
|
return HttpClientRequest.setHeader(request, "Authorization", authorization);
|
|
574
|
-
})
|
|
629
|
+
});
|
|
575
630
|
|
|
576
631
|
// src/services/event-logger.ts
|
|
577
632
|
import * as Context3 from "effect/Context";
|
|
578
633
|
import * as Effect7 from "effect/Effect";
|
|
579
634
|
import * as Layer3 from "effect/Layer";
|
|
580
635
|
import * as Schema9 from "effect/Schema";
|
|
581
|
-
import { Obj as
|
|
636
|
+
import { Obj as Obj6, Type as Type6 } from "@dxos/echo";
|
|
582
637
|
import { invariant } from "@dxos/invariant";
|
|
583
638
|
import { LogLevel, log as log2 } from "@dxos/log";
|
|
584
639
|
|
|
@@ -587,13 +642,20 @@ import * as Context2 from "effect/Context";
|
|
|
587
642
|
import * as Effect6 from "effect/Effect";
|
|
588
643
|
import * as Layer2 from "effect/Layer";
|
|
589
644
|
import { AgentStatus } from "@dxos/ai";
|
|
590
|
-
import { Obj as
|
|
645
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
646
|
+
import { ObjectId } from "@dxos/keys";
|
|
591
647
|
import { Message } from "@dxos/types";
|
|
592
648
|
var TracingService = class _TracingService extends Context2.Tag("@dxos/functions/TracingService")() {
|
|
593
649
|
static noop = {
|
|
594
650
|
getTraceContext: () => ({}),
|
|
595
651
|
write: () => {
|
|
596
|
-
}
|
|
652
|
+
},
|
|
653
|
+
traceInvocationStart: () => Effect6.sync(() => ({
|
|
654
|
+
invocationId: ObjectId.random(),
|
|
655
|
+
invocationTraceQueue: void 0
|
|
656
|
+
})),
|
|
657
|
+
traceInvocationEnd: () => Effect6.sync(() => {
|
|
658
|
+
})
|
|
597
659
|
};
|
|
598
660
|
static layerNoop = Layer2.succeed(_TracingService, _TracingService.noop);
|
|
599
661
|
/**
|
|
@@ -603,32 +665,43 @@ var TracingService = class _TracingService extends Context2.Tag("@dxos/functions
|
|
|
603
665
|
const tracing = yield* _TracingService;
|
|
604
666
|
const context = mapContext(tracing.getTraceContext());
|
|
605
667
|
return {
|
|
606
|
-
write: (event) => tracing.write(event),
|
|
607
|
-
getTraceContext: () => context
|
|
668
|
+
write: (event, context2) => tracing.write(event, context2),
|
|
669
|
+
getTraceContext: () => context,
|
|
670
|
+
traceInvocationStart: () => Effect6.die("Tracing invocation inside another invocation is not supported."),
|
|
671
|
+
traceInvocationEnd: () => Effect6.die("Tracing invocation inside another invocation is not supported.")
|
|
608
672
|
};
|
|
609
673
|
}));
|
|
610
674
|
/**
|
|
675
|
+
* Create sublayer to trace an invocation.
|
|
676
|
+
* @param data
|
|
677
|
+
* @returns
|
|
678
|
+
*/
|
|
679
|
+
static layerInvocation = (data) => _TracingService.layerSubframe((context) => ({
|
|
680
|
+
...context,
|
|
681
|
+
currentInvocation: data
|
|
682
|
+
}));
|
|
683
|
+
/**
|
|
611
684
|
* Emit the current human-readable execution status.
|
|
612
685
|
*/
|
|
613
686
|
static emitStatus = Effect6.fnUntraced(function* (data) {
|
|
614
687
|
const tracing = yield* _TracingService;
|
|
615
|
-
tracing.write(
|
|
688
|
+
tracing.write(Obj5.make(AgentStatus, {
|
|
616
689
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
617
690
|
toolCallId: tracing.getTraceContext().toolCallId,
|
|
618
691
|
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
619
692
|
...data
|
|
620
|
-
}));
|
|
693
|
+
}), tracing.getTraceContext());
|
|
621
694
|
});
|
|
622
695
|
static emitConverationMessage = Effect6.fnUntraced(function* (data) {
|
|
623
696
|
const tracing = yield* _TracingService;
|
|
624
|
-
tracing.write(
|
|
697
|
+
tracing.write(Obj5.make(Message.Message, {
|
|
625
698
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
626
699
|
...data,
|
|
627
700
|
properties: {
|
|
628
701
|
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
629
702
|
...data.properties
|
|
630
703
|
}
|
|
631
|
-
}));
|
|
704
|
+
}), tracing.getTraceContext());
|
|
632
705
|
});
|
|
633
706
|
};
|
|
634
707
|
var MESSAGE_PROPERTY_TOOL_CALL_ID = "toolCallId";
|
|
@@ -666,7 +739,7 @@ var ComputeEventPayload = Schema9.Union(Schema9.Struct({
|
|
|
666
739
|
}));
|
|
667
740
|
var ComputeEvent = Schema9.Struct({
|
|
668
741
|
payload: ComputeEventPayload
|
|
669
|
-
}).pipe(Type6.
|
|
742
|
+
}).pipe(Type6.object({
|
|
670
743
|
typename: "dxos.org/type/ComputeEvent",
|
|
671
744
|
version: "0.1.0"
|
|
672
745
|
}));
|
|
@@ -683,9 +756,9 @@ var ComputeEventLogger = class _ComputeEventLogger extends Context3.Tag("@dxos/f
|
|
|
683
756
|
const tracing = yield* TracingService;
|
|
684
757
|
return {
|
|
685
758
|
log: (event) => {
|
|
686
|
-
tracing.write(
|
|
759
|
+
tracing.write(Obj6.make(ComputeEvent, {
|
|
687
760
|
payload: event
|
|
688
|
-
}));
|
|
761
|
+
}), tracing.getTraceContext());
|
|
689
762
|
},
|
|
690
763
|
nodeId: void 0
|
|
691
764
|
};
|
|
@@ -741,16 +814,22 @@ var createEventLogger = (level, message = "event") => {
|
|
|
741
814
|
// src/services/function-invocation-service.ts
|
|
742
815
|
import * as Context4 from "effect/Context";
|
|
743
816
|
import * as Effect8 from "effect/Effect";
|
|
817
|
+
import * as Layer4 from "effect/Layer";
|
|
744
818
|
var FunctionInvocationService = class _FunctionInvocationService extends Context4.Tag("@dxos/functions/FunctionInvocationService")() {
|
|
819
|
+
static layerNotAvailable = Layer4.succeed(_FunctionInvocationService, {
|
|
820
|
+
invokeFunction: () => Effect8.die("FunctionInvocationService is not avaialble."),
|
|
821
|
+
resolveFunction: () => Effect8.die("FunctionInvocationService is not available.")
|
|
822
|
+
});
|
|
745
823
|
static invokeFunction = (functionDef, input) => Effect8.serviceFunctionEffect(_FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
824
|
+
static resolveFunction = (key) => Effect8.serviceFunctionEffect(_FunctionInvocationService, (service) => service.resolveFunction)(key);
|
|
746
825
|
};
|
|
747
826
|
|
|
748
827
|
// src/services/queues.ts
|
|
749
828
|
import * as Context5 from "effect/Context";
|
|
750
829
|
import * as Effect9 from "effect/Effect";
|
|
751
|
-
import * as
|
|
830
|
+
import * as Layer5 from "effect/Layer";
|
|
752
831
|
var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/QueueService")() {
|
|
753
|
-
static notAvailable =
|
|
832
|
+
static notAvailable = Layer5.succeed(_QueueService, {
|
|
754
833
|
queues: {
|
|
755
834
|
get(_dxn) {
|
|
756
835
|
throw new Error("Queues not available");
|
|
@@ -767,7 +846,7 @@ var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/Que
|
|
|
767
846
|
queue
|
|
768
847
|
};
|
|
769
848
|
};
|
|
770
|
-
static layer = (queues, queue) =>
|
|
849
|
+
static layer = (queues, queue) => Layer5.succeed(_QueueService, _QueueService.make(queues, queue));
|
|
771
850
|
/**
|
|
772
851
|
* Gets a queue by its DXN.
|
|
773
852
|
*/
|
|
@@ -779,23 +858,149 @@ var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/Que
|
|
|
779
858
|
static append = (queue, objects) => Effect9.promise(() => queue.append(objects));
|
|
780
859
|
};
|
|
781
860
|
var ContextQueueService = class _ContextQueueService extends Context5.Tag("@dxos/functions/ContextQueueService")() {
|
|
782
|
-
static layer = (queue) =>
|
|
861
|
+
static layer = (queue) => Layer5.succeed(_ContextQueueService, {
|
|
783
862
|
queue
|
|
784
863
|
});
|
|
785
864
|
};
|
|
786
865
|
|
|
787
866
|
// src/protocol/protocol.ts
|
|
788
|
-
import * as
|
|
789
|
-
import * as
|
|
867
|
+
import * as AnthropicClient from "@effect/ai-anthropic/AnthropicClient";
|
|
868
|
+
import * as Effect11 from "effect/Effect";
|
|
869
|
+
import * as Layer7 from "effect/Layer";
|
|
790
870
|
import * as Schema10 from "effect/Schema";
|
|
791
871
|
import * as SchemaAST2 from "effect/SchemaAST";
|
|
792
|
-
import { AiService } from "@dxos/ai";
|
|
793
|
-
import {
|
|
872
|
+
import { AiModelResolver, AiService } from "@dxos/ai";
|
|
873
|
+
import { AnthropicResolver } from "@dxos/ai/resolvers";
|
|
874
|
+
import { LifecycleState, Resource } from "@dxos/context";
|
|
875
|
+
import { Database as Database2, Ref as Ref2, Type as Type7 } from "@dxos/echo";
|
|
876
|
+
import { refFromEncodedReference } from "@dxos/echo/internal";
|
|
794
877
|
import { EchoClient } from "@dxos/echo-db";
|
|
795
|
-
import {
|
|
796
|
-
import { failedInvariant as failedInvariant2, invariant as invariant2 } from "@dxos/invariant";
|
|
878
|
+
import { runAndForwardErrors } from "@dxos/effect";
|
|
879
|
+
import { assertState, failedInvariant as failedInvariant2, invariant as invariant2 } from "@dxos/invariant";
|
|
797
880
|
import { PublicKey } from "@dxos/keys";
|
|
798
|
-
|
|
881
|
+
|
|
882
|
+
// src/protocol/functions-ai-http-client.ts
|
|
883
|
+
import * as Headers from "@effect/platform/Headers";
|
|
884
|
+
import * as HttpClient2 from "@effect/platform/HttpClient";
|
|
885
|
+
import * as HttpClientError from "@effect/platform/HttpClientError";
|
|
886
|
+
import * as HttpClientResponse from "@effect/platform/HttpClientResponse";
|
|
887
|
+
import * as Effect10 from "effect/Effect";
|
|
888
|
+
import * as FiberRef from "effect/FiberRef";
|
|
889
|
+
import * as Layer6 from "effect/Layer";
|
|
890
|
+
import * as Stream from "effect/Stream";
|
|
891
|
+
import { log as log3 } from "@dxos/log";
|
|
892
|
+
import { ErrorCodec } from "@dxos/protocols";
|
|
893
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/protocol/functions-ai-http-client.ts";
|
|
894
|
+
var requestInitTagKey = "@effect/platform/FetchHttpClient/FetchOptions";
|
|
895
|
+
var FunctionsAiHttpClient = class _FunctionsAiHttpClient {
|
|
896
|
+
static make = (service) => HttpClient2.make((request, url, signal, fiber) => {
|
|
897
|
+
const context = fiber.getFiberRef(FiberRef.currentContext);
|
|
898
|
+
const options = context.unsafeMap.get(requestInitTagKey) ?? {};
|
|
899
|
+
const headers = options.headers ? Headers.merge(Headers.fromInput(options.headers), request.headers) : request.headers;
|
|
900
|
+
const send = (body) => Effect10.tryPromise({
|
|
901
|
+
try: () => service.fetch(new Request(url, {
|
|
902
|
+
...options,
|
|
903
|
+
method: request.method,
|
|
904
|
+
headers,
|
|
905
|
+
body
|
|
906
|
+
})),
|
|
907
|
+
catch: (cause) => {
|
|
908
|
+
log3.error("Failed to fetch", {
|
|
909
|
+
errorSerialized: ErrorCodec.encode(cause)
|
|
910
|
+
}, {
|
|
911
|
+
F: __dxlog_file2,
|
|
912
|
+
L: 43,
|
|
913
|
+
S: this,
|
|
914
|
+
C: (f, a) => f(...a)
|
|
915
|
+
});
|
|
916
|
+
return new HttpClientError.RequestError({
|
|
917
|
+
request,
|
|
918
|
+
reason: "Transport",
|
|
919
|
+
cause
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
}).pipe(Effect10.map((response) => HttpClientResponse.fromWeb(request, response)));
|
|
923
|
+
switch (request.body._tag) {
|
|
924
|
+
case "Raw":
|
|
925
|
+
case "Uint8Array":
|
|
926
|
+
return send(request.body.body);
|
|
927
|
+
case "FormData":
|
|
928
|
+
return send(request.body.formData);
|
|
929
|
+
case "Stream":
|
|
930
|
+
return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect10.flatMap(send));
|
|
931
|
+
}
|
|
932
|
+
return send(void 0);
|
|
933
|
+
});
|
|
934
|
+
static layer = (service) => Layer6.succeed(HttpClient2.HttpClient, _FunctionsAiHttpClient.make(service));
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
// src/protocol/protocol.ts
|
|
938
|
+
function _ts_add_disposable_resource(env, value2, async) {
|
|
939
|
+
if (value2 !== null && value2 !== void 0) {
|
|
940
|
+
if (typeof value2 !== "object" && typeof value2 !== "function") throw new TypeError("Object expected.");
|
|
941
|
+
var dispose, inner;
|
|
942
|
+
if (async) {
|
|
943
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
944
|
+
dispose = value2[Symbol.asyncDispose];
|
|
945
|
+
}
|
|
946
|
+
if (dispose === void 0) {
|
|
947
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
948
|
+
dispose = value2[Symbol.dispose];
|
|
949
|
+
if (async) inner = dispose;
|
|
950
|
+
}
|
|
951
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
952
|
+
if (inner) dispose = function() {
|
|
953
|
+
try {
|
|
954
|
+
inner.call(this);
|
|
955
|
+
} catch (e) {
|
|
956
|
+
return Promise.reject(e);
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
env.stack.push({
|
|
960
|
+
value: value2,
|
|
961
|
+
dispose,
|
|
962
|
+
async
|
|
963
|
+
});
|
|
964
|
+
} else if (async) {
|
|
965
|
+
env.stack.push({
|
|
966
|
+
async: true
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
return value2;
|
|
970
|
+
}
|
|
971
|
+
function _ts_dispose_resources(env) {
|
|
972
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
973
|
+
var e = new Error(message);
|
|
974
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
975
|
+
};
|
|
976
|
+
return (_ts_dispose_resources = function _ts_dispose_resources2(env2) {
|
|
977
|
+
function fail(e) {
|
|
978
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
979
|
+
env2.hasError = true;
|
|
980
|
+
}
|
|
981
|
+
var r, s = 0;
|
|
982
|
+
function next() {
|
|
983
|
+
while (r = env2.stack.pop()) {
|
|
984
|
+
try {
|
|
985
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
986
|
+
if (r.dispose) {
|
|
987
|
+
var result = r.dispose.call(r.value);
|
|
988
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
989
|
+
fail(e);
|
|
990
|
+
return next();
|
|
991
|
+
});
|
|
992
|
+
} else s |= 1;
|
|
993
|
+
} catch (e) {
|
|
994
|
+
fail(e);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
998
|
+
if (env2.hasError) throw env2.error;
|
|
999
|
+
}
|
|
1000
|
+
return next();
|
|
1001
|
+
})(env);
|
|
1002
|
+
}
|
|
1003
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/protocol/protocol.ts";
|
|
799
1004
|
var wrapFunctionHandler = (func) => {
|
|
800
1005
|
if (!FunctionDefinition.isFunction(func)) {
|
|
801
1006
|
throw new TypeError("Invalid function definition");
|
|
@@ -810,85 +1015,182 @@ var wrapFunctionHandler = (func) => {
|
|
|
810
1015
|
services: func.services
|
|
811
1016
|
},
|
|
812
1017
|
handler: async ({ data, context }) => {
|
|
813
|
-
if ((func.services.includes(
|
|
1018
|
+
if ((func.services.includes(Database2.Service.key) || func.services.includes(QueueService.key)) && (!context.services.dataService || !context.services.queryService)) {
|
|
814
1019
|
throw new FunctionError({
|
|
815
1020
|
message: "Services not provided: dataService, queryService"
|
|
816
1021
|
});
|
|
817
1022
|
}
|
|
818
1023
|
try {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
return result;
|
|
834
|
-
} catch (error) {
|
|
835
|
-
if (FunctionError.is(error)) {
|
|
836
|
-
throw error;
|
|
837
|
-
} else {
|
|
838
|
-
throw new FunctionError({
|
|
839
|
-
cause: error,
|
|
840
|
-
context: {
|
|
841
|
-
func: func.key
|
|
1024
|
+
const env = {
|
|
1025
|
+
stack: [],
|
|
1026
|
+
error: void 0,
|
|
1027
|
+
hasError: false
|
|
1028
|
+
};
|
|
1029
|
+
try {
|
|
1030
|
+
if (!SchemaAST2.isAnyKeyword(func.inputSchema.ast)) {
|
|
1031
|
+
try {
|
|
1032
|
+
Schema10.validateSync(func.inputSchema)(data);
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
throw new FunctionError({
|
|
1035
|
+
message: "Invalid input schema",
|
|
1036
|
+
cause: error
|
|
1037
|
+
});
|
|
842
1038
|
}
|
|
1039
|
+
}
|
|
1040
|
+
const funcContext = _ts_add_disposable_resource(env, await new FunctionContext(context).open(), true);
|
|
1041
|
+
if (func.types.length > 0) {
|
|
1042
|
+
invariant2(funcContext.db, "Database is required for functions with types", {
|
|
1043
|
+
F: __dxlog_file3,
|
|
1044
|
+
L: 68,
|
|
1045
|
+
S: void 0,
|
|
1046
|
+
A: [
|
|
1047
|
+
"funcContext.db",
|
|
1048
|
+
"'Database is required for functions with types'"
|
|
1049
|
+
]
|
|
1050
|
+
});
|
|
1051
|
+
await funcContext.db.graph.schemaRegistry.register(func.types);
|
|
1052
|
+
}
|
|
1053
|
+
const dataWithDecodedRefs = funcContext.db && !SchemaAST2.isAnyKeyword(func.inputSchema.ast) ? decodeRefsFromSchema(func.inputSchema.ast, data, funcContext.db) : data;
|
|
1054
|
+
let result = await func.handler({
|
|
1055
|
+
// TODO(dmaretskyi): Fix the types.
|
|
1056
|
+
context,
|
|
1057
|
+
data: dataWithDecodedRefs
|
|
843
1058
|
});
|
|
1059
|
+
if (Effect11.isEffect(result)) {
|
|
1060
|
+
result = await runAndForwardErrors(result.pipe(Effect11.orDie, Effect11.provide(funcContext.createLayer())));
|
|
1061
|
+
}
|
|
1062
|
+
if (func.outputSchema && !SchemaAST2.isAnyKeyword(func.outputSchema.ast)) {
|
|
1063
|
+
Schema10.validateSync(func.outputSchema)(result);
|
|
1064
|
+
}
|
|
1065
|
+
return result;
|
|
1066
|
+
} catch (e) {
|
|
1067
|
+
env.error = e;
|
|
1068
|
+
env.hasError = true;
|
|
1069
|
+
} finally {
|
|
1070
|
+
const result = _ts_dispose_resources(env);
|
|
1071
|
+
if (result) await result;
|
|
844
1072
|
}
|
|
1073
|
+
} catch (error) {
|
|
1074
|
+
throw error;
|
|
845
1075
|
}
|
|
846
1076
|
}
|
|
847
1077
|
};
|
|
848
1078
|
};
|
|
849
|
-
var
|
|
850
|
-
|
|
851
|
-
|
|
1079
|
+
var FunctionContext = class extends Resource {
|
|
1080
|
+
context;
|
|
1081
|
+
client;
|
|
1082
|
+
db;
|
|
1083
|
+
queues;
|
|
1084
|
+
constructor(context) {
|
|
1085
|
+
super();
|
|
1086
|
+
this.context = context;
|
|
852
1087
|
if (context.services.dataService && context.services.queryService) {
|
|
853
|
-
client =
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
S: this,
|
|
858
|
-
A: [
|
|
859
|
-
"context.services.dataService && context.services.queryService",
|
|
860
|
-
""
|
|
861
|
-
]
|
|
862
|
-
});
|
|
863
|
-
return new EchoClient().connectToService({
|
|
864
|
-
dataService: context.services.dataService,
|
|
865
|
-
queryService: context.services.queryService,
|
|
866
|
-
queueService: context.services.queueService
|
|
867
|
-
});
|
|
1088
|
+
this.client = new EchoClient().connectToService({
|
|
1089
|
+
dataService: context.services.dataService,
|
|
1090
|
+
queryService: context.services.queryService,
|
|
1091
|
+
queueService: context.services.queueService
|
|
868
1092
|
});
|
|
869
1093
|
}
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
1094
|
+
}
|
|
1095
|
+
async _open() {
|
|
1096
|
+
await this.client?.open();
|
|
1097
|
+
this.db = this.client && this.context.spaceId ? this.client.constructDatabase({
|
|
1098
|
+
spaceId: this.context.spaceId ?? failedInvariant2(),
|
|
1099
|
+
spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant2("spaceKey missing in context")),
|
|
1100
|
+
reactiveSchemaQuery: false,
|
|
1101
|
+
preloadSchemaOnOpen: false
|
|
1102
|
+
}) : void 0;
|
|
1103
|
+
await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant2("spaceRootUrl missing in context"));
|
|
1104
|
+
await this.db?.open();
|
|
1105
|
+
this.queues = this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : void 0;
|
|
1106
|
+
}
|
|
1107
|
+
async _close() {
|
|
1108
|
+
await this.db?.close();
|
|
1109
|
+
await this.client?.close();
|
|
1110
|
+
}
|
|
1111
|
+
createLayer() {
|
|
1112
|
+
assertState(this._lifecycleState === LifecycleState.OPEN, "FunctionContext is not open");
|
|
1113
|
+
const dbLayer = this.db ? Database2.layer(this.db) : Database2.notAvailable;
|
|
1114
|
+
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
1115
|
+
const credentials = dbLayer ? CredentialsService.layerFromDatabase({
|
|
1116
|
+
caching: true
|
|
1117
|
+
}).pipe(Layer7.provide(dbLayer)) : CredentialsService.configuredLayer([]);
|
|
883
1118
|
const functionInvocationService = MockedFunctionInvocationService;
|
|
884
|
-
const aiService = AiService.notAvailable;
|
|
885
1119
|
const tracing = TracingService.layerNoop;
|
|
886
|
-
|
|
887
|
-
|
|
1120
|
+
const aiLayer = this.context.services.functionsAiService ? AiModelResolver.AiModelResolver.buildAiService.pipe(Layer7.provide(AnthropicResolver.make().pipe(Layer7.provide(AnthropicClient.layer({
|
|
1121
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
1122
|
+
apiUrl: "http://internal/provider/anthropic"
|
|
1123
|
+
}).pipe(Layer7.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))))))) : AiService.notAvailable;
|
|
1124
|
+
return Layer7.mergeAll(dbLayer, queuesLayer, credentials, functionInvocationService, aiLayer, tracing);
|
|
1125
|
+
}
|
|
888
1126
|
};
|
|
889
|
-
var MockedFunctionInvocationService =
|
|
890
|
-
invokeFunction: () =>
|
|
1127
|
+
var MockedFunctionInvocationService = Layer7.succeed(FunctionInvocationService, {
|
|
1128
|
+
invokeFunction: () => Effect11.die("Calling functions from functions is not implemented yet."),
|
|
1129
|
+
resolveFunction: () => Effect11.die("Not implemented.")
|
|
891
1130
|
});
|
|
1131
|
+
var decodeRefsFromSchema = (ast, value2, db) => {
|
|
1132
|
+
if (value2 == null) {
|
|
1133
|
+
return value2;
|
|
1134
|
+
}
|
|
1135
|
+
const encoded = SchemaAST2.encodedBoundAST(ast);
|
|
1136
|
+
if (Ref2.isRefType(encoded)) {
|
|
1137
|
+
if (Ref2.isRef(value2)) {
|
|
1138
|
+
return value2;
|
|
1139
|
+
}
|
|
1140
|
+
if (typeof value2 === "object" && value2 !== null && typeof value2["/"] === "string") {
|
|
1141
|
+
const resolver = db.graph.createRefResolver({
|
|
1142
|
+
context: {
|
|
1143
|
+
space: db.spaceId
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
return refFromEncodedReference(value2, resolver);
|
|
1147
|
+
}
|
|
1148
|
+
return value2;
|
|
1149
|
+
}
|
|
1150
|
+
switch (encoded._tag) {
|
|
1151
|
+
case "TypeLiteral": {
|
|
1152
|
+
if (typeof value2 !== "object" || value2 === null || Array.isArray(value2)) {
|
|
1153
|
+
return value2;
|
|
1154
|
+
}
|
|
1155
|
+
const result = {
|
|
1156
|
+
...value2
|
|
1157
|
+
};
|
|
1158
|
+
for (const prop of SchemaAST2.getPropertySignatures(encoded)) {
|
|
1159
|
+
const key = prop.name.toString();
|
|
1160
|
+
if (key in result) {
|
|
1161
|
+
result[key] = decodeRefsFromSchema(prop.type, result[key], db);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
return result;
|
|
1165
|
+
}
|
|
1166
|
+
case "TupleType": {
|
|
1167
|
+
if (!Array.isArray(value2)) {
|
|
1168
|
+
return value2;
|
|
1169
|
+
}
|
|
1170
|
+
if (encoded.elements.length === 0 && encoded.rest.length === 1) {
|
|
1171
|
+
const elementType = encoded.rest[0].type;
|
|
1172
|
+
return value2.map((item) => decodeRefsFromSchema(elementType, item, db));
|
|
1173
|
+
}
|
|
1174
|
+
return value2;
|
|
1175
|
+
}
|
|
1176
|
+
case "Union": {
|
|
1177
|
+
const nonUndefined = encoded.types.filter((t) => !SchemaAST2.isUndefinedKeyword(t));
|
|
1178
|
+
if (nonUndefined.length === 1) {
|
|
1179
|
+
return decodeRefsFromSchema(nonUndefined[0], value2, db);
|
|
1180
|
+
}
|
|
1181
|
+
return value2;
|
|
1182
|
+
}
|
|
1183
|
+
case "Suspend": {
|
|
1184
|
+
return decodeRefsFromSchema(encoded.f(), value2, db);
|
|
1185
|
+
}
|
|
1186
|
+
case "Refinement": {
|
|
1187
|
+
return decodeRefsFromSchema(encoded.from, value2, db);
|
|
1188
|
+
}
|
|
1189
|
+
default: {
|
|
1190
|
+
return value2;
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
};
|
|
892
1194
|
export {
|
|
893
1195
|
ComputeEvent,
|
|
894
1196
|
ComputeEventLogger,
|
|
@@ -896,7 +1198,6 @@ export {
|
|
|
896
1198
|
ConfiguredCredentialsService,
|
|
897
1199
|
ContextQueueService,
|
|
898
1200
|
CredentialsService,
|
|
899
|
-
DatabaseService2 as DatabaseService,
|
|
900
1201
|
Example,
|
|
901
1202
|
FUNCTIONS_META_KEY,
|
|
902
1203
|
FUNCTIONS_PRESET_META_KEY,
|
|
@@ -921,6 +1222,7 @@ export {
|
|
|
921
1222
|
logCustomEvent,
|
|
922
1223
|
serializeFunction,
|
|
923
1224
|
setUserFunctionIdInMetadata,
|
|
1225
|
+
toOperation,
|
|
924
1226
|
withAuthorization,
|
|
925
1227
|
wrapFunctionHandler
|
|
926
1228
|
};
|