@dxos/functions 0.8.4-main.7ace549 → 0.8.4-main.937b3ca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +273 -74
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +273 -74
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +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 +18 -4
- 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/tracing.d.ts +37 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +33 -44
- package/dist/types/src/types/Function.d.ts.map +1 -1
- package/dist/types/src/types/Script.d.ts +8 -15
- package/dist/types/src/types/Script.d.ts.map +1 -1
- package/dist/types/src/types/Trigger.d.ts +37 -57
- package/dist/types/src/types/Trigger.d.ts.map +1 -1
- package/dist/types/src/types/TriggerEvent.d.ts +33 -2
- 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 +22 -16
- 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 +118 -12
- package/src/sdk.ts +55 -6
- 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/tracing.ts +63 -4
- package/src/types/Function.ts +12 -10
- package/src/types/Script.ts +3 -2
- package/src/types/Trigger.ts +9 -6
- package/src/types/TriggerEvent.ts +9 -3
- package/src/types/url.ts +4 -3
|
@@ -7,7 +7,7 @@ var __export = (target, all) => {
|
|
|
7
7
|
|
|
8
8
|
// src/errors.ts
|
|
9
9
|
import { BaseError } from "@dxos/errors";
|
|
10
|
-
var ServiceNotAvailableError = class extends BaseError.extend("
|
|
10
|
+
var ServiceNotAvailableError = class extends BaseError.extend("ServiceNotAvailable", "Service not available") {
|
|
11
11
|
constructor(service, options) {
|
|
12
12
|
super({
|
|
13
13
|
context: {
|
|
@@ -17,7 +17,7 @@ var ServiceNotAvailableError = class extends BaseError.extend("SERVICE_NOT_AVAIL
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
var FunctionNotFoundError = class extends BaseError.extend("
|
|
20
|
+
var FunctionNotFoundError = class extends BaseError.extend("FunctionNotFound", "Function not found") {
|
|
21
21
|
constructor(functionKey, options) {
|
|
22
22
|
super({
|
|
23
23
|
context: {
|
|
@@ -27,9 +27,9 @@ var FunctionNotFoundError = class extends BaseError.extend("FUNCTION_NOT_FOUND",
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
};
|
|
30
|
-
var FunctionError = class extends BaseError.extend("
|
|
30
|
+
var FunctionError = class extends BaseError.extend("FunctionError", "Function invocation error") {
|
|
31
31
|
};
|
|
32
|
-
var TriggerStateNotFoundError = class extends BaseError.extend("
|
|
32
|
+
var TriggerStateNotFoundError = class extends BaseError.extend("TriggerStateNotFound", "Trigger state not found") {
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
// src/example/fib.ts
|
|
@@ -39,8 +39,9 @@ import * as Schema6 from "effect/Schema";
|
|
|
39
39
|
// src/sdk.ts
|
|
40
40
|
import * as Effect from "effect/Effect";
|
|
41
41
|
import * as Schema5 from "effect/Schema";
|
|
42
|
-
import { Obj as
|
|
42
|
+
import { Obj as Obj4, Type as Type5 } from "@dxos/echo";
|
|
43
43
|
import { assertArgument, failedInvariant } from "@dxos/invariant";
|
|
44
|
+
import { Operation } from "@dxos/operation";
|
|
44
45
|
|
|
45
46
|
// src/types/Function.ts
|
|
46
47
|
var Function_exports = {};
|
|
@@ -70,7 +71,7 @@ var Script = Schema.Struct({
|
|
|
70
71
|
// Whether source has changed since last deploy.
|
|
71
72
|
changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
|
|
72
73
|
source: Type.Ref(Text.Text).pipe(FormInputAnnotation.set(false))
|
|
73
|
-
}).pipe(Type.
|
|
74
|
+
}).pipe(Type.object({
|
|
74
75
|
typename: "dxos.org/type/Script",
|
|
75
76
|
version: "0.1.0"
|
|
76
77
|
}), Annotation.LabelAnnotation.set([
|
|
@@ -112,7 +113,7 @@ var Function = Schema2.Struct({
|
|
|
112
113
|
services: Schema2.optional(Schema2.Array(Schema2.String)),
|
|
113
114
|
// Local binding to a function name.
|
|
114
115
|
binding: Schema2.optional(Schema2.String)
|
|
115
|
-
}).pipe(Type2.
|
|
116
|
+
}).pipe(Type2.object({
|
|
116
117
|
typename: "dxos.org/type/Function",
|
|
117
118
|
version: "0.1.0"
|
|
118
119
|
}), Annotation2.LabelAnnotation.set([
|
|
@@ -120,14 +121,16 @@ var Function = Schema2.Struct({
|
|
|
120
121
|
]), SystemTypeAnnotation.set(true));
|
|
121
122
|
var make2 = (props) => Obj2.make(Function, props);
|
|
122
123
|
var setFrom = (target, source) => {
|
|
123
|
-
target
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
124
|
+
Obj2.change(target, (t) => {
|
|
125
|
+
t.key = source.key ?? target.key;
|
|
126
|
+
t.name = source.name ?? target.name;
|
|
127
|
+
t.version = source.version;
|
|
128
|
+
t.description = source.description;
|
|
129
|
+
t.updated = source.updated;
|
|
130
|
+
t.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : void 0;
|
|
131
|
+
t.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : void 0;
|
|
132
|
+
Obj2.getMeta(t).keys = JSON.parse(JSON.stringify(Obj2.getMeta(source).keys));
|
|
133
|
+
});
|
|
131
134
|
};
|
|
132
135
|
|
|
133
136
|
// src/types/Trigger.ts
|
|
@@ -148,6 +151,7 @@ import * as SchemaAST from "effect/SchemaAST";
|
|
|
148
151
|
import { Obj as Obj3, QueryAST, Type as Type3 } from "@dxos/echo";
|
|
149
152
|
import { OptionsAnnotationId, SystemTypeAnnotation as SystemTypeAnnotation2 } from "@dxos/echo/internal";
|
|
150
153
|
import { DXN } from "@dxos/keys";
|
|
154
|
+
import { Expando } from "@dxos/schema";
|
|
151
155
|
var Kinds = [
|
|
152
156
|
"email",
|
|
153
157
|
"queue",
|
|
@@ -212,12 +216,12 @@ var WebhookSpec = Schema3.Struct({
|
|
|
212
216
|
var Spec = Schema3.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
|
|
213
217
|
title: "Trigger"
|
|
214
218
|
});
|
|
215
|
-
var
|
|
219
|
+
var TriggerSchema = Schema3.Struct({
|
|
216
220
|
/**
|
|
217
221
|
* Function or workflow to invoke.
|
|
218
222
|
*/
|
|
219
223
|
// TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
|
|
220
|
-
function: Schema3.optional(Type3.Ref(
|
|
224
|
+
function: Schema3.optional(Type3.Ref(Expando.Expando).annotations({
|
|
221
225
|
title: "Function"
|
|
222
226
|
})),
|
|
223
227
|
/**
|
|
@@ -228,6 +232,8 @@ var Trigger_ = Schema3.Struct({
|
|
|
228
232
|
inputNodeId: Schema3.optional(Schema3.String.annotations({
|
|
229
233
|
title: "Input Node ID"
|
|
230
234
|
})),
|
|
235
|
+
// TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
|
|
236
|
+
// Need lint rule; or agent rule to require PR review for "boolean" key word.
|
|
231
237
|
enabled: Schema3.optional(Schema3.Boolean.annotations({
|
|
232
238
|
title: "Enabled"
|
|
233
239
|
})),
|
|
@@ -247,11 +253,11 @@ var Trigger_ = Schema3.Struct({
|
|
|
247
253
|
key: Schema3.String,
|
|
248
254
|
value: Schema3.Any
|
|
249
255
|
})))
|
|
250
|
-
}).pipe(Type3.
|
|
256
|
+
}).pipe(Type3.object({
|
|
251
257
|
typename: "dxos.org/type/Trigger",
|
|
252
258
|
version: "0.1.0"
|
|
253
259
|
}), SystemTypeAnnotation2.set(true));
|
|
254
|
-
var Trigger =
|
|
260
|
+
var Trigger = TriggerSchema;
|
|
255
261
|
var make3 = (props) => Obj3.make(Trigger, props);
|
|
256
262
|
|
|
257
263
|
// src/types/TriggerEvent.ts
|
|
@@ -261,10 +267,11 @@ __export(TriggerEvent_exports, {
|
|
|
261
267
|
QueueEvent: () => QueueEvent,
|
|
262
268
|
SubscriptionEvent: () => SubscriptionEvent,
|
|
263
269
|
TimerEvent: () => TimerEvent,
|
|
270
|
+
TriggerEvent: () => TriggerEvent,
|
|
264
271
|
WebhookEvent: () => WebhookEvent
|
|
265
272
|
});
|
|
266
273
|
import * as Schema4 from "effect/Schema";
|
|
267
|
-
import { DXN as DXN2,
|
|
274
|
+
import { DXN as DXN2, Type as Type4 } from "@dxos/echo";
|
|
268
275
|
var EmailEvent = Schema4.mutable(Schema4.Struct({
|
|
269
276
|
from: Schema4.String,
|
|
270
277
|
to: Schema4.String,
|
|
@@ -286,7 +293,7 @@ var SubscriptionEvent = Schema4.Struct({
|
|
|
286
293
|
/**
|
|
287
294
|
* Reference to the object that was changed or created.
|
|
288
295
|
*/
|
|
289
|
-
subject: Type4.Ref(
|
|
296
|
+
subject: Type4.Ref(Type4.Obj),
|
|
290
297
|
/**
|
|
291
298
|
* @deprecated
|
|
292
299
|
*/
|
|
@@ -304,6 +311,7 @@ var WebhookEvent = Schema4.mutable(Schema4.Struct({
|
|
|
304
311
|
}),
|
|
305
312
|
bodyText: Schema4.String
|
|
306
313
|
}));
|
|
314
|
+
var TriggerEvent = Schema4.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
|
|
307
315
|
|
|
308
316
|
// src/types/url.ts
|
|
309
317
|
var FUNCTIONS_META_KEY = "dxos.org/service/function";
|
|
@@ -377,10 +385,39 @@ var getServiceKeys = (services) => {
|
|
|
377
385
|
if (typeof tag.key === "string") {
|
|
378
386
|
return tag.key;
|
|
379
387
|
}
|
|
380
|
-
console.log(tag);
|
|
381
388
|
failedInvariant();
|
|
382
389
|
});
|
|
383
390
|
};
|
|
391
|
+
var toOperation = (functionDef) => {
|
|
392
|
+
const op = Operation.make({
|
|
393
|
+
schema: {
|
|
394
|
+
input: functionDef.inputSchema,
|
|
395
|
+
output: functionDef.outputSchema ?? Schema5.Any
|
|
396
|
+
},
|
|
397
|
+
meta: {
|
|
398
|
+
key: functionDef.key,
|
|
399
|
+
name: functionDef.name,
|
|
400
|
+
description: functionDef.description
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
const operationHandler = (input) => {
|
|
404
|
+
const result = functionDef.handler({
|
|
405
|
+
context: {},
|
|
406
|
+
data: input
|
|
407
|
+
});
|
|
408
|
+
if (Effect.isEffect(result)) {
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
if (result instanceof Promise) {
|
|
412
|
+
return Effect.tryPromise(() => result);
|
|
413
|
+
}
|
|
414
|
+
return Effect.succeed(result);
|
|
415
|
+
};
|
|
416
|
+
return {
|
|
417
|
+
...op,
|
|
418
|
+
handler: operationHandler
|
|
419
|
+
};
|
|
420
|
+
};
|
|
384
421
|
var FunctionDefinition = {
|
|
385
422
|
make: defineFunction,
|
|
386
423
|
isFunction: (value2) => {
|
|
@@ -391,9 +428,10 @@ var FunctionDefinition = {
|
|
|
391
428
|
return serializeFunction(functionDef);
|
|
392
429
|
},
|
|
393
430
|
deserialize: (functionObj) => {
|
|
394
|
-
assertArgument(
|
|
431
|
+
assertArgument(Obj4.instanceOf(Function_exports.Function, functionObj), "functionObj");
|
|
395
432
|
return deserializeFunction(functionObj);
|
|
396
|
-
}
|
|
433
|
+
},
|
|
434
|
+
toOperation
|
|
397
435
|
};
|
|
398
436
|
var serializeFunction = (functionDef) => {
|
|
399
437
|
const fn4 = Function_exports.make({
|
|
@@ -406,7 +444,7 @@ var serializeFunction = (functionDef) => {
|
|
|
406
444
|
services: functionDef.services
|
|
407
445
|
});
|
|
408
446
|
if (functionDef.meta?.deployedFunctionId) {
|
|
409
|
-
setUserFunctionIdInMetadata(
|
|
447
|
+
Obj4.change(fn4, (fn5) => setUserFunctionIdInMetadata(Obj4.getMeta(fn5), functionDef.meta.deployedFunctionId));
|
|
410
448
|
}
|
|
411
449
|
return fn4;
|
|
412
450
|
};
|
|
@@ -425,7 +463,7 @@ var deserializeFunction = (functionObj) => {
|
|
|
425
463
|
services: functionObj.services ?? [],
|
|
426
464
|
types: [],
|
|
427
465
|
meta: {
|
|
428
|
-
deployedFunctionId: getUserFunctionIdInMetadata(
|
|
466
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj4.getMeta(functionObj))
|
|
429
467
|
}
|
|
430
468
|
};
|
|
431
469
|
};
|
|
@@ -502,9 +540,6 @@ var sleep_default = defineFunction({
|
|
|
502
540
|
})(Example || (Example = {}));
|
|
503
541
|
var Example;
|
|
504
542
|
|
|
505
|
-
// src/services/index.ts
|
|
506
|
-
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
507
|
-
|
|
508
543
|
// src/services/credentials.ts
|
|
509
544
|
import * as HttpClient from "@effect/platform/HttpClient";
|
|
510
545
|
import * as HttpClientRequest from "@effect/platform/HttpClientRequest";
|
|
@@ -513,7 +548,7 @@ import * as Effect5 from "effect/Effect";
|
|
|
513
548
|
import * as Layer from "effect/Layer";
|
|
514
549
|
import * as Redacted from "effect/Redacted";
|
|
515
550
|
import { Query } from "@dxos/echo";
|
|
516
|
-
import {
|
|
551
|
+
import { Database } from "@dxos/echo";
|
|
517
552
|
import { AccessToken } from "@dxos/types";
|
|
518
553
|
var CredentialsService = class _CredentialsService extends Context.Tag("@dxos/functions/CredentialsService")() {
|
|
519
554
|
static getCredential = (query) => Effect5.gen(function* () {
|
|
@@ -537,14 +572,23 @@ var CredentialsService = class _CredentialsService extends Context.Tag("@dxos/fu
|
|
|
537
572
|
}));
|
|
538
573
|
return new ConfiguredCredentialsService(serviceCredentials);
|
|
539
574
|
}));
|
|
540
|
-
static layerFromDatabase = () => Layer.effect(_CredentialsService, Effect5.gen(function* () {
|
|
541
|
-
const dbService = yield*
|
|
575
|
+
static layerFromDatabase = ({ caching = false } = {}) => Layer.effect(_CredentialsService, Effect5.gen(function* () {
|
|
576
|
+
const dbService = yield* Database.Service;
|
|
577
|
+
const cache = /* @__PURE__ */ new Map();
|
|
542
578
|
const queryCredentials = async (query) => {
|
|
543
|
-
const
|
|
544
|
-
|
|
579
|
+
const cacheKey = JSON.stringify(query);
|
|
580
|
+
if (caching && cache.has(cacheKey)) {
|
|
581
|
+
return cache.get(cacheKey);
|
|
582
|
+
}
|
|
583
|
+
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
584
|
+
const credentials = accessTokens.filter((accessToken) => accessToken.source === query.service).map((accessToken) => ({
|
|
545
585
|
service: accessToken.source,
|
|
546
586
|
apiKey: accessToken.token
|
|
547
587
|
}));
|
|
588
|
+
if (caching) {
|
|
589
|
+
cache.set(cacheKey, credentials);
|
|
590
|
+
}
|
|
591
|
+
return credentials;
|
|
548
592
|
};
|
|
549
593
|
return {
|
|
550
594
|
getCredential: async (query) => {
|
|
@@ -580,18 +624,17 @@ var ConfiguredCredentialsService = class {
|
|
|
580
624
|
return credential;
|
|
581
625
|
}
|
|
582
626
|
};
|
|
583
|
-
var withAuthorization = (
|
|
584
|
-
const
|
|
585
|
-
const authorization = kind ? `${kind} ${key}` : key;
|
|
627
|
+
var withAuthorization = (token, kind) => HttpClient.mapRequest((request) => {
|
|
628
|
+
const authorization = kind ? `${kind} ${token}` : token;
|
|
586
629
|
return HttpClientRequest.setHeader(request, "Authorization", authorization);
|
|
587
|
-
})
|
|
630
|
+
});
|
|
588
631
|
|
|
589
632
|
// src/services/event-logger.ts
|
|
590
633
|
import * as Context3 from "effect/Context";
|
|
591
634
|
import * as Effect7 from "effect/Effect";
|
|
592
635
|
import * as Layer3 from "effect/Layer";
|
|
593
636
|
import * as Schema9 from "effect/Schema";
|
|
594
|
-
import { Obj as
|
|
637
|
+
import { Obj as Obj6, Type as Type6 } from "@dxos/echo";
|
|
595
638
|
import { invariant } from "@dxos/invariant";
|
|
596
639
|
import { LogLevel, log as log2 } from "@dxos/log";
|
|
597
640
|
|
|
@@ -600,13 +643,20 @@ import * as Context2 from "effect/Context";
|
|
|
600
643
|
import * as Effect6 from "effect/Effect";
|
|
601
644
|
import * as Layer2 from "effect/Layer";
|
|
602
645
|
import { AgentStatus } from "@dxos/ai";
|
|
603
|
-
import { Obj as
|
|
646
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
647
|
+
import { ObjectId } from "@dxos/keys";
|
|
604
648
|
import { Message } from "@dxos/types";
|
|
605
649
|
var TracingService = class _TracingService extends Context2.Tag("@dxos/functions/TracingService")() {
|
|
606
650
|
static noop = {
|
|
607
651
|
getTraceContext: () => ({}),
|
|
608
652
|
write: () => {
|
|
609
|
-
}
|
|
653
|
+
},
|
|
654
|
+
traceInvocationStart: () => Effect6.sync(() => ({
|
|
655
|
+
invocationId: ObjectId.random(),
|
|
656
|
+
invocationTraceQueue: void 0
|
|
657
|
+
})),
|
|
658
|
+
traceInvocationEnd: () => Effect6.sync(() => {
|
|
659
|
+
})
|
|
610
660
|
};
|
|
611
661
|
static layerNoop = Layer2.succeed(_TracingService, _TracingService.noop);
|
|
612
662
|
/**
|
|
@@ -616,32 +666,43 @@ var TracingService = class _TracingService extends Context2.Tag("@dxos/functions
|
|
|
616
666
|
const tracing = yield* _TracingService;
|
|
617
667
|
const context = mapContext(tracing.getTraceContext());
|
|
618
668
|
return {
|
|
619
|
-
write: (event) => tracing.write(event),
|
|
620
|
-
getTraceContext: () => context
|
|
669
|
+
write: (event, context2) => tracing.write(event, context2),
|
|
670
|
+
getTraceContext: () => context,
|
|
671
|
+
traceInvocationStart: () => Effect6.die("Tracing invocation inside another invocation is not supported."),
|
|
672
|
+
traceInvocationEnd: () => Effect6.die("Tracing invocation inside another invocation is not supported.")
|
|
621
673
|
};
|
|
622
674
|
}));
|
|
623
675
|
/**
|
|
676
|
+
* Create sublayer to trace an invocation.
|
|
677
|
+
* @param data
|
|
678
|
+
* @returns
|
|
679
|
+
*/
|
|
680
|
+
static layerInvocation = (data) => _TracingService.layerSubframe((context) => ({
|
|
681
|
+
...context,
|
|
682
|
+
currentInvocation: data
|
|
683
|
+
}));
|
|
684
|
+
/**
|
|
624
685
|
* Emit the current human-readable execution status.
|
|
625
686
|
*/
|
|
626
687
|
static emitStatus = Effect6.fnUntraced(function* (data) {
|
|
627
688
|
const tracing = yield* _TracingService;
|
|
628
|
-
tracing.write(
|
|
689
|
+
tracing.write(Obj5.make(AgentStatus, {
|
|
629
690
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
630
691
|
toolCallId: tracing.getTraceContext().toolCallId,
|
|
631
692
|
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
632
693
|
...data
|
|
633
|
-
}));
|
|
694
|
+
}), tracing.getTraceContext());
|
|
634
695
|
});
|
|
635
696
|
static emitConverationMessage = Effect6.fnUntraced(function* (data) {
|
|
636
697
|
const tracing = yield* _TracingService;
|
|
637
|
-
tracing.write(
|
|
698
|
+
tracing.write(Obj5.make(Message.Message, {
|
|
638
699
|
parentMessage: tracing.getTraceContext().parentMessage,
|
|
639
700
|
...data,
|
|
640
701
|
properties: {
|
|
641
702
|
[MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
|
|
642
703
|
...data.properties
|
|
643
704
|
}
|
|
644
|
-
}));
|
|
705
|
+
}), tracing.getTraceContext());
|
|
645
706
|
});
|
|
646
707
|
};
|
|
647
708
|
var MESSAGE_PROPERTY_TOOL_CALL_ID = "toolCallId";
|
|
@@ -679,7 +740,7 @@ var ComputeEventPayload = Schema9.Union(Schema9.Struct({
|
|
|
679
740
|
}));
|
|
680
741
|
var ComputeEvent = Schema9.Struct({
|
|
681
742
|
payload: ComputeEventPayload
|
|
682
|
-
}).pipe(Type6.
|
|
743
|
+
}).pipe(Type6.object({
|
|
683
744
|
typename: "dxos.org/type/ComputeEvent",
|
|
684
745
|
version: "0.1.0"
|
|
685
746
|
}));
|
|
@@ -696,9 +757,9 @@ var ComputeEventLogger = class _ComputeEventLogger extends Context3.Tag("@dxos/f
|
|
|
696
757
|
const tracing = yield* TracingService;
|
|
697
758
|
return {
|
|
698
759
|
log: (event) => {
|
|
699
|
-
tracing.write(
|
|
760
|
+
tracing.write(Obj6.make(ComputeEvent, {
|
|
700
761
|
payload: event
|
|
701
|
-
}));
|
|
762
|
+
}), tracing.getTraceContext());
|
|
702
763
|
},
|
|
703
764
|
nodeId: void 0
|
|
704
765
|
};
|
|
@@ -754,16 +815,22 @@ var createEventLogger = (level, message = "event") => {
|
|
|
754
815
|
// src/services/function-invocation-service.ts
|
|
755
816
|
import * as Context4 from "effect/Context";
|
|
756
817
|
import * as Effect8 from "effect/Effect";
|
|
818
|
+
import * as Layer4 from "effect/Layer";
|
|
757
819
|
var FunctionInvocationService = class _FunctionInvocationService extends Context4.Tag("@dxos/functions/FunctionInvocationService")() {
|
|
820
|
+
static layerNotAvailable = Layer4.succeed(_FunctionInvocationService, {
|
|
821
|
+
invokeFunction: () => Effect8.die("FunctionInvocationService is not avaialble."),
|
|
822
|
+
resolveFunction: () => Effect8.die("FunctionInvocationService is not available.")
|
|
823
|
+
});
|
|
758
824
|
static invokeFunction = (functionDef, input) => Effect8.serviceFunctionEffect(_FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
|
|
825
|
+
static resolveFunction = (key) => Effect8.serviceFunctionEffect(_FunctionInvocationService, (service) => service.resolveFunction)(key);
|
|
759
826
|
};
|
|
760
827
|
|
|
761
828
|
// src/services/queues.ts
|
|
762
829
|
import * as Context5 from "effect/Context";
|
|
763
830
|
import * as Effect9 from "effect/Effect";
|
|
764
|
-
import * as
|
|
831
|
+
import * as Layer5 from "effect/Layer";
|
|
765
832
|
var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/QueueService")() {
|
|
766
|
-
static notAvailable =
|
|
833
|
+
static notAvailable = Layer5.succeed(_QueueService, {
|
|
767
834
|
queues: {
|
|
768
835
|
get(_dxn) {
|
|
769
836
|
throw new Error("Queues not available");
|
|
@@ -780,7 +847,7 @@ var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/Que
|
|
|
780
847
|
queue
|
|
781
848
|
};
|
|
782
849
|
};
|
|
783
|
-
static layer = (queues, queue) =>
|
|
850
|
+
static layer = (queues, queue) => Layer5.succeed(_QueueService, _QueueService.make(queues, queue));
|
|
784
851
|
/**
|
|
785
852
|
* Gets a queue by its DXN.
|
|
786
853
|
*/
|
|
@@ -792,22 +859,83 @@ var QueueService = class _QueueService extends Context5.Tag("@dxos/functions/Que
|
|
|
792
859
|
static append = (queue, objects) => Effect9.promise(() => queue.append(objects));
|
|
793
860
|
};
|
|
794
861
|
var ContextQueueService = class _ContextQueueService extends Context5.Tag("@dxos/functions/ContextQueueService")() {
|
|
795
|
-
static layer = (queue) =>
|
|
862
|
+
static layer = (queue) => Layer5.succeed(_ContextQueueService, {
|
|
796
863
|
queue
|
|
797
864
|
});
|
|
798
865
|
};
|
|
799
866
|
|
|
800
867
|
// src/protocol/protocol.ts
|
|
801
|
-
import * as
|
|
802
|
-
import * as
|
|
868
|
+
import * as AnthropicClient from "@effect/ai-anthropic/AnthropicClient";
|
|
869
|
+
import * as Effect11 from "effect/Effect";
|
|
870
|
+
import * as Layer7 from "effect/Layer";
|
|
803
871
|
import * as Schema10 from "effect/Schema";
|
|
804
872
|
import * as SchemaAST2 from "effect/SchemaAST";
|
|
805
|
-
import { AiService } from "@dxos/ai";
|
|
873
|
+
import { AiModelResolver, AiService } from "@dxos/ai";
|
|
874
|
+
import { AnthropicResolver } from "@dxos/ai/resolvers";
|
|
806
875
|
import { LifecycleState, Resource } from "@dxos/context";
|
|
807
|
-
import { Type as Type7 } from "@dxos/echo";
|
|
876
|
+
import { Database as Database2, Ref as Ref2, Type as Type7 } from "@dxos/echo";
|
|
877
|
+
import { refFromEncodedReference } from "@dxos/echo/internal";
|
|
808
878
|
import { EchoClient } from "@dxos/echo-db";
|
|
879
|
+
import { runAndForwardErrors } from "@dxos/effect";
|
|
809
880
|
import { assertState, failedInvariant as failedInvariant2, invariant as invariant2 } from "@dxos/invariant";
|
|
810
881
|
import { PublicKey } from "@dxos/keys";
|
|
882
|
+
|
|
883
|
+
// src/protocol/functions-ai-http-client.ts
|
|
884
|
+
import * as Headers from "@effect/platform/Headers";
|
|
885
|
+
import * as HttpClient2 from "@effect/platform/HttpClient";
|
|
886
|
+
import * as HttpClientError from "@effect/platform/HttpClientError";
|
|
887
|
+
import * as HttpClientResponse from "@effect/platform/HttpClientResponse";
|
|
888
|
+
import * as Effect10 from "effect/Effect";
|
|
889
|
+
import * as FiberRef from "effect/FiberRef";
|
|
890
|
+
import * as Layer6 from "effect/Layer";
|
|
891
|
+
import * as Stream from "effect/Stream";
|
|
892
|
+
import { log as log3 } from "@dxos/log";
|
|
893
|
+
import { ErrorCodec } from "@dxos/protocols";
|
|
894
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/protocol/functions-ai-http-client.ts";
|
|
895
|
+
var requestInitTagKey = "@effect/platform/FetchHttpClient/FetchOptions";
|
|
896
|
+
var FunctionsAiHttpClient = class _FunctionsAiHttpClient {
|
|
897
|
+
static make = (service) => HttpClient2.make((request, url, signal, fiber) => {
|
|
898
|
+
const context = fiber.getFiberRef(FiberRef.currentContext);
|
|
899
|
+
const options = context.unsafeMap.get(requestInitTagKey) ?? {};
|
|
900
|
+
const headers = options.headers ? Headers.merge(Headers.fromInput(options.headers), request.headers) : request.headers;
|
|
901
|
+
const send = (body) => Effect10.tryPromise({
|
|
902
|
+
try: () => service.fetch(new Request(url, {
|
|
903
|
+
...options,
|
|
904
|
+
method: request.method,
|
|
905
|
+
headers,
|
|
906
|
+
body
|
|
907
|
+
})),
|
|
908
|
+
catch: (cause) => {
|
|
909
|
+
log3.error("Failed to fetch", {
|
|
910
|
+
errorSerialized: ErrorCodec.encode(cause)
|
|
911
|
+
}, {
|
|
912
|
+
F: __dxlog_file2,
|
|
913
|
+
L: 43,
|
|
914
|
+
S: this,
|
|
915
|
+
C: (f, a) => f(...a)
|
|
916
|
+
});
|
|
917
|
+
return new HttpClientError.RequestError({
|
|
918
|
+
request,
|
|
919
|
+
reason: "Transport",
|
|
920
|
+
cause
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
}).pipe(Effect10.map((response) => HttpClientResponse.fromWeb(request, response)));
|
|
924
|
+
switch (request.body._tag) {
|
|
925
|
+
case "Raw":
|
|
926
|
+
case "Uint8Array":
|
|
927
|
+
return send(request.body.body);
|
|
928
|
+
case "FormData":
|
|
929
|
+
return send(request.body.formData);
|
|
930
|
+
case "Stream":
|
|
931
|
+
return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect10.flatMap(send));
|
|
932
|
+
}
|
|
933
|
+
return send(void 0);
|
|
934
|
+
});
|
|
935
|
+
static layer = (service) => Layer6.succeed(HttpClient2.HttpClient, _FunctionsAiHttpClient.make(service));
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
// src/protocol/protocol.ts
|
|
811
939
|
function _ts_add_disposable_resource(env, value2, async) {
|
|
812
940
|
if (value2 !== null && value2 !== void 0) {
|
|
813
941
|
if (typeof value2 !== "object" && typeof value2 !== "function") throw new TypeError("Object expected.");
|
|
@@ -873,7 +1001,7 @@ function _ts_dispose_resources(env) {
|
|
|
873
1001
|
return next();
|
|
874
1002
|
})(env);
|
|
875
1003
|
}
|
|
876
|
-
var
|
|
1004
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/protocol/protocol.ts";
|
|
877
1005
|
var wrapFunctionHandler = (func) => {
|
|
878
1006
|
if (!FunctionDefinition.isFunction(func)) {
|
|
879
1007
|
throw new TypeError("Invalid function definition");
|
|
@@ -888,7 +1016,7 @@ var wrapFunctionHandler = (func) => {
|
|
|
888
1016
|
services: func.services
|
|
889
1017
|
},
|
|
890
1018
|
handler: async ({ data, context }) => {
|
|
891
|
-
if ((func.services.includes(
|
|
1019
|
+
if ((func.services.includes(Database2.Service.key) || func.services.includes(QueueService.key)) && (!context.services.dataService || !context.services.queryService)) {
|
|
892
1020
|
throw new FunctionError({
|
|
893
1021
|
message: "Services not provided: dataService, queryService"
|
|
894
1022
|
});
|
|
@@ -913,23 +1041,24 @@ var wrapFunctionHandler = (func) => {
|
|
|
913
1041
|
const funcContext = _ts_add_disposable_resource(env, await new FunctionContext(context).open(), true);
|
|
914
1042
|
if (func.types.length > 0) {
|
|
915
1043
|
invariant2(funcContext.db, "Database is required for functions with types", {
|
|
916
|
-
F:
|
|
917
|
-
L:
|
|
1044
|
+
F: __dxlog_file3,
|
|
1045
|
+
L: 68,
|
|
918
1046
|
S: void 0,
|
|
919
1047
|
A: [
|
|
920
1048
|
"funcContext.db",
|
|
921
1049
|
"'Database is required for functions with types'"
|
|
922
1050
|
]
|
|
923
1051
|
});
|
|
924
|
-
funcContext.db.graph.schemaRegistry.
|
|
1052
|
+
await funcContext.db.graph.schemaRegistry.register(func.types);
|
|
925
1053
|
}
|
|
1054
|
+
const dataWithDecodedRefs = funcContext.db && !SchemaAST2.isAnyKeyword(func.inputSchema.ast) ? decodeRefsFromSchema(func.inputSchema.ast, data, funcContext.db) : data;
|
|
926
1055
|
let result = await func.handler({
|
|
927
1056
|
// TODO(dmaretskyi): Fix the types.
|
|
928
1057
|
context,
|
|
929
|
-
data
|
|
1058
|
+
data: dataWithDecodedRefs
|
|
930
1059
|
});
|
|
931
|
-
if (
|
|
932
|
-
result = await
|
|
1060
|
+
if (Effect11.isEffect(result)) {
|
|
1061
|
+
result = await runAndForwardErrors(result.pipe(Effect11.orDie, Effect11.provide(funcContext.createLayer())));
|
|
933
1062
|
}
|
|
934
1063
|
if (func.outputSchema && !SchemaAST2.isAnyKeyword(func.outputSchema.ast)) {
|
|
935
1064
|
Schema10.validateSync(func.outputSchema)(result);
|
|
@@ -969,7 +1098,8 @@ var FunctionContext = class extends Resource {
|
|
|
969
1098
|
this.db = this.client && this.context.spaceId ? this.client.constructDatabase({
|
|
970
1099
|
spaceId: this.context.spaceId ?? failedInvariant2(),
|
|
971
1100
|
spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant2("spaceKey missing in context")),
|
|
972
|
-
reactiveSchemaQuery: false
|
|
1101
|
+
reactiveSchemaQuery: false,
|
|
1102
|
+
preloadSchemaOnOpen: false
|
|
973
1103
|
}) : void 0;
|
|
974
1104
|
await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant2("spaceRootUrl missing in context"));
|
|
975
1105
|
await this.db?.open();
|
|
@@ -981,18 +1111,87 @@ var FunctionContext = class extends Resource {
|
|
|
981
1111
|
}
|
|
982
1112
|
createLayer() {
|
|
983
1113
|
assertState(this._lifecycleState === LifecycleState.OPEN, "FunctionContext is not open");
|
|
984
|
-
const dbLayer = this.db ?
|
|
1114
|
+
const dbLayer = this.db ? Database2.Service.layer(this.db) : Database2.Service.notAvailable;
|
|
985
1115
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
986
|
-
const credentials = dbLayer ? CredentialsService.layerFromDatabase(
|
|
1116
|
+
const credentials = dbLayer ? CredentialsService.layerFromDatabase({
|
|
1117
|
+
caching: true
|
|
1118
|
+
}).pipe(Layer7.provide(dbLayer)) : CredentialsService.configuredLayer([]);
|
|
987
1119
|
const functionInvocationService = MockedFunctionInvocationService;
|
|
988
|
-
const aiService = AiService.notAvailable;
|
|
989
1120
|
const tracing = TracingService.layerNoop;
|
|
990
|
-
|
|
1121
|
+
const aiLayer = this.context.services.functionsAiService ? AiModelResolver.AiModelResolver.buildAiService.pipe(Layer7.provide(AnthropicResolver.make().pipe(Layer7.provide(AnthropicClient.layer({
|
|
1122
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
1123
|
+
apiUrl: "http://internal/provider/anthropic"
|
|
1124
|
+
}).pipe(Layer7.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))))))) : AiService.notAvailable;
|
|
1125
|
+
return Layer7.mergeAll(dbLayer, queuesLayer, credentials, functionInvocationService, aiLayer, tracing);
|
|
991
1126
|
}
|
|
992
1127
|
};
|
|
993
|
-
var MockedFunctionInvocationService =
|
|
994
|
-
invokeFunction: () =>
|
|
1128
|
+
var MockedFunctionInvocationService = Layer7.succeed(FunctionInvocationService, {
|
|
1129
|
+
invokeFunction: () => Effect11.die("Calling functions from functions is not implemented yet."),
|
|
1130
|
+
resolveFunction: () => Effect11.die("Not implemented.")
|
|
995
1131
|
});
|
|
1132
|
+
var decodeRefsFromSchema = (ast, value2, db) => {
|
|
1133
|
+
if (value2 == null) {
|
|
1134
|
+
return value2;
|
|
1135
|
+
}
|
|
1136
|
+
const encoded = SchemaAST2.encodedBoundAST(ast);
|
|
1137
|
+
if (Ref2.isRefType(encoded)) {
|
|
1138
|
+
if (Ref2.isRef(value2)) {
|
|
1139
|
+
return value2;
|
|
1140
|
+
}
|
|
1141
|
+
if (typeof value2 === "object" && value2 !== null && typeof value2["/"] === "string") {
|
|
1142
|
+
const resolver = db.graph.createRefResolver({
|
|
1143
|
+
context: {
|
|
1144
|
+
space: db.spaceId
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
return refFromEncodedReference(value2, resolver);
|
|
1148
|
+
}
|
|
1149
|
+
return value2;
|
|
1150
|
+
}
|
|
1151
|
+
switch (encoded._tag) {
|
|
1152
|
+
case "TypeLiteral": {
|
|
1153
|
+
if (typeof value2 !== "object" || value2 === null || Array.isArray(value2)) {
|
|
1154
|
+
return value2;
|
|
1155
|
+
}
|
|
1156
|
+
const result = {
|
|
1157
|
+
...value2
|
|
1158
|
+
};
|
|
1159
|
+
for (const prop of SchemaAST2.getPropertySignatures(encoded)) {
|
|
1160
|
+
const key = prop.name.toString();
|
|
1161
|
+
if (key in result) {
|
|
1162
|
+
result[key] = decodeRefsFromSchema(prop.type, result[key], db);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
return result;
|
|
1166
|
+
}
|
|
1167
|
+
case "TupleType": {
|
|
1168
|
+
if (!Array.isArray(value2)) {
|
|
1169
|
+
return value2;
|
|
1170
|
+
}
|
|
1171
|
+
if (encoded.elements.length === 0 && encoded.rest.length === 1) {
|
|
1172
|
+
const elementType = encoded.rest[0].type;
|
|
1173
|
+
return value2.map((item) => decodeRefsFromSchema(elementType, item, db));
|
|
1174
|
+
}
|
|
1175
|
+
return value2;
|
|
1176
|
+
}
|
|
1177
|
+
case "Union": {
|
|
1178
|
+
const nonUndefined = encoded.types.filter((t) => !SchemaAST2.isUndefinedKeyword(t));
|
|
1179
|
+
if (nonUndefined.length === 1) {
|
|
1180
|
+
return decodeRefsFromSchema(nonUndefined[0], value2, db);
|
|
1181
|
+
}
|
|
1182
|
+
return value2;
|
|
1183
|
+
}
|
|
1184
|
+
case "Suspend": {
|
|
1185
|
+
return decodeRefsFromSchema(encoded.f(), value2, db);
|
|
1186
|
+
}
|
|
1187
|
+
case "Refinement": {
|
|
1188
|
+
return decodeRefsFromSchema(encoded.from, value2, db);
|
|
1189
|
+
}
|
|
1190
|
+
default: {
|
|
1191
|
+
return value2;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
996
1195
|
export {
|
|
997
1196
|
ComputeEvent,
|
|
998
1197
|
ComputeEventLogger,
|
|
@@ -1000,7 +1199,6 @@ export {
|
|
|
1000
1199
|
ConfiguredCredentialsService,
|
|
1001
1200
|
ContextQueueService,
|
|
1002
1201
|
CredentialsService,
|
|
1003
|
-
DatabaseService2 as DatabaseService,
|
|
1004
1202
|
Example,
|
|
1005
1203
|
FUNCTIONS_META_KEY,
|
|
1006
1204
|
FUNCTIONS_PRESET_META_KEY,
|
|
@@ -1025,6 +1223,7 @@ export {
|
|
|
1025
1223
|
logCustomEvent,
|
|
1026
1224
|
serializeFunction,
|
|
1027
1225
|
setUserFunctionIdInMetadata,
|
|
1226
|
+
toOperation,
|
|
1028
1227
|
withAuthorization,
|
|
1029
1228
|
wrapFunctionHandler
|
|
1030
1229
|
};
|