@dxos/functions 0.8.4-main.8342792842 → 0.8.4-main.8360d9e660

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 CHANGED
@@ -27,11 +27,9 @@ import { FunctionContext } from '@dxos/functions';
27
27
 
28
28
  export default (event: any, context: FunctionContext) => {
29
29
  const identity = context.client.halo.identity.get();
30
- return context
31
- .status(200)
32
- .succeed({
33
- message: `Hello ${identity?.profile?.displayName}`
34
- });
30
+ return context.status(200).succeed({
31
+ message: `Hello ${identity?.profile?.displayName}`,
32
+ });
35
33
  };
36
34
  ```
37
35
 
@@ -69,7 +67,7 @@ nodemon -w ./src -e ts --exec $(git rev-parse --show-toplevel)/packages/devtools
69
67
  > NOTE: The port (7001) must match the one in config.
70
68
 
71
69
  ```bash
72
- curl http://localhost:7001/dev/hello -X POST -H 'Content-Type: application/json' -w '\n' --data '{ "message": "Hello World!" }'
70
+ curl http://localhost:7001/dev/hello -X POST -H 'Content-Type: application/json' -w '\n' --data '{ "message": "Hello World!" }'
73
71
  ```
74
72
 
75
73
  ## DXOS Resources
@@ -38,7 +38,7 @@ 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 Obj4, Type as Type5 } from "@dxos/echo";
41
+ import { JsonSchema as JsonSchema2, Obj as Obj5 } from "@dxos/echo";
42
42
  import { assertArgument, failedInvariant } from "@dxos/invariant";
43
43
  import { Operation } from "@dxos/operation";
44
44
 
@@ -50,7 +50,7 @@ __export(Function_exports, {
50
50
  setFrom: () => setFrom
51
51
  });
52
52
  import * as Schema2 from "effect/Schema";
53
- import { Annotation as Annotation2, JsonSchema, Obj as Obj2, Type as Type2 } from "@dxos/echo";
53
+ import { Annotation as Annotation2, JsonSchema, Obj as Obj2, Ref as Ref2, Type as Type2 } from "@dxos/echo";
54
54
  import { SystemTypeAnnotation } from "@dxos/echo/internal";
55
55
 
56
56
  // src/types/Script.ts
@@ -69,13 +69,16 @@ var Script = Schema.Struct({
69
69
  // TODO(burdon): Change to hash of deployed content.
70
70
  // Whether source has changed since last deploy.
71
71
  changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
72
- source: Type.Ref(Text.Text).pipe(FormInputAnnotation.set(false))
72
+ source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false))
73
73
  }).pipe(Type.object({
74
- typename: "dxos.org/type/Script",
74
+ typename: "org.dxos.type.script",
75
75
  version: "0.1.0"
76
76
  }), Annotation.LabelAnnotation.set([
77
77
  "name"
78
- ]));
78
+ ]), Annotation.IconAnnotation.set({
79
+ icon: "ph--code--regular",
80
+ hue: "sky"
81
+ }));
79
82
  var make = ({ source = "", ...props } = {}) => Obj.make(Script, {
80
83
  ...props,
81
84
  source: Ref.make(Text.make(source))
@@ -102,7 +105,7 @@ var Function = Schema2.Struct({
102
105
  updated: Schema2.optional(Schema2.String),
103
106
  // Reference to a source script if it exists within ECHO.
104
107
  // TODO(burdon): Don't ref ScriptType directly (core).
105
- source: Schema2.optional(Type2.Ref(Script)),
108
+ source: Schema2.optional(Ref2.Ref(Script)),
106
109
  inputSchema: Schema2.optional(JsonSchema.JsonSchema),
107
110
  outputSchema: Schema2.optional(JsonSchema.JsonSchema),
108
111
  /**
@@ -113,11 +116,14 @@ var Function = Schema2.Struct({
113
116
  // Local binding to a function name.
114
117
  binding: Schema2.optional(Schema2.String)
115
118
  }).pipe(Type2.object({
116
- typename: "dxos.org/type/Function",
119
+ typename: "org.dxos.type.function",
117
120
  version: "0.1.0"
118
121
  }), Annotation2.LabelAnnotation.set([
119
122
  "name"
120
- ]), SystemTypeAnnotation.set(true));
123
+ ]), Annotation2.IconAnnotation.set({
124
+ icon: "ph--function--regular",
125
+ hue: "blue"
126
+ }), SystemTypeAnnotation.set(true));
121
127
  var make2 = (props) => Obj2.make(Function, props);
122
128
  var setFrom = (target, source) => {
123
129
  Obj2.change(target, (t) => {
@@ -147,7 +153,7 @@ __export(Trigger_exports, {
147
153
  });
148
154
  import * as Schema3 from "effect/Schema";
149
155
  import * as SchemaAST from "effect/SchemaAST";
150
- import { Obj as Obj3, QueryAST, Type as Type3 } from "@dxos/echo";
156
+ import { Annotation as Annotation3, Obj as Obj3, QueryAST, Ref as Ref3, Type as Type3 } from "@dxos/echo";
151
157
  import { OptionsAnnotationId, SystemTypeAnnotation as SystemTypeAnnotation2 } from "@dxos/echo/internal";
152
158
  import { DXN } from "@dxos/keys";
153
159
  import { Expando } from "@dxos/schema";
@@ -220,7 +226,7 @@ var TriggerSchema = Schema3.Struct({
220
226
  * Function or workflow to invoke.
221
227
  */
222
228
  // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
223
- function: Schema3.optional(Type3.Ref(Expando.Expando).annotations({
229
+ function: Schema3.optional(Ref3.Ref(Expando.Expando).annotations({
224
230
  title: "Function"
225
231
  })),
226
232
  /**
@@ -237,13 +243,18 @@ var TriggerSchema = Schema3.Struct({
237
243
  title: "Enabled"
238
244
  })),
239
245
  spec: Schema3.optional(Spec),
246
+ concurrency: Schema3.optional(Schema3.Number.annotations({
247
+ title: "Concurrency",
248
+ default: 1,
249
+ description: "Maximum number of concurrent invocations of the trigger. For queue triggers, this will process queue items in parallel."
250
+ })),
240
251
  /**
241
252
  * Passed as the input data to the function.
242
253
  * Must match the function's input schema.
243
254
  *
244
255
  * @example
245
256
  * {
246
- * item: '{{$.trigger.event}}',
257
+ * item: '{{event.item}}',
247
258
  * instructions: 'Summarize and perform entity-extraction'
248
259
  * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
249
260
  * }
@@ -253,8 +264,11 @@ var TriggerSchema = Schema3.Struct({
253
264
  value: Schema3.Any
254
265
  }))
255
266
  }).pipe(Type3.object({
256
- typename: "dxos.org/type/Trigger",
267
+ typename: "org.dxos.type.trigger",
257
268
  version: "0.1.0"
269
+ }), Annotation3.IconAnnotation.set({
270
+ icon: "ph--lightning--regular",
271
+ hue: "yellow"
258
272
  }), SystemTypeAnnotation2.set(true));
259
273
  var Trigger = TriggerSchema;
260
274
  var make3 = (props) => Obj3.make(Trigger, props);
@@ -270,7 +284,7 @@ __export(TriggerEvent_exports, {
270
284
  WebhookEvent: () => WebhookEvent
271
285
  });
272
286
  import * as Schema4 from "effect/Schema";
273
- import { DXN as DXN2, Type as Type4 } from "@dxos/echo";
287
+ import { DXN as DXN2, Obj as Obj4, Ref as Ref4 } from "@dxos/echo";
274
288
  var EmailEvent = Schema4.Struct({
275
289
  from: Schema4.String,
276
290
  to: Schema4.String,
@@ -292,7 +306,7 @@ var SubscriptionEvent = Schema4.Struct({
292
306
  /**
293
307
  * Reference to the object that was changed or created.
294
308
  */
295
- subject: Type4.Ref(Type4.Obj),
309
+ subject: Ref4.Ref(Obj4.Unknown),
296
310
  /**
297
311
  * @deprecated
298
312
  */
@@ -313,8 +327,8 @@ var WebhookEvent = Schema4.Struct({
313
327
  var TriggerEvent = Schema4.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
314
328
 
315
329
  // src/types/url.ts
316
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
317
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
330
+ var FUNCTIONS_META_KEY = "org.dxos.service.function";
331
+ var FUNCTIONS_PRESET_META_KEY = "org.dxos.service.function-preset";
318
332
  var getUserFunctionIdInMetadata = (meta) => {
319
333
  return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
320
334
  };
@@ -333,7 +347,7 @@ var setUserFunctionIdInMetadata = (meta, functionId) => {
333
347
  };
334
348
 
335
349
  // src/sdk.ts
336
- var typeId = Symbol.for("@dxos/functions/FunctionDefinition");
350
+ var typeId = /* @__PURE__ */ Symbol.for("@dxos/functions/FunctionDefinition");
337
351
  var defineFunction = ({ key, name, description, inputSchema, outputSchema = Schema5.Any, handler, types, services }) => {
338
352
  if (!Schema5.isSchema(inputSchema)) {
339
353
  throw new Error("Input schema must be a valid schema");
@@ -420,14 +434,14 @@ var toOperation = (functionDef) => {
420
434
  var FunctionDefinition = {
421
435
  make: defineFunction,
422
436
  isFunction: (value2) => {
423
- return typeof value2 === "object" && value2 !== null && Symbol.for("@dxos/functions/FunctionDefinition") in value2;
437
+ return typeof value2 === "object" && value2 !== null && /* @__PURE__ */ Symbol.for("@dxos/functions/FunctionDefinition") in value2;
424
438
  },
425
439
  serialize: (functionDef) => {
426
440
  assertArgument(FunctionDefinition.isFunction(functionDef), "functionDef");
427
441
  return serializeFunction(functionDef);
428
442
  },
429
443
  deserialize: (functionObj) => {
430
- assertArgument(Obj4.instanceOf(Function_exports.Function, functionObj), "functionObj");
444
+ assertArgument(Obj5.instanceOf(Function_exports.Function, functionObj), "functionObj");
431
445
  return deserializeFunction(functionObj);
432
446
  },
433
447
  toOperation
@@ -438,12 +452,12 @@ var serializeFunction = (functionDef) => {
438
452
  name: functionDef.name,
439
453
  version: "0.1.0",
440
454
  description: functionDef.description,
441
- inputSchema: Type5.toJsonSchema(functionDef.inputSchema),
442
- outputSchema: !functionDef.outputSchema ? void 0 : Type5.toJsonSchema(functionDef.outputSchema),
455
+ inputSchema: JsonSchema2.toJsonSchema(functionDef.inputSchema),
456
+ outputSchema: !functionDef.outputSchema ? void 0 : JsonSchema2.toJsonSchema(functionDef.outputSchema),
443
457
  services: functionDef.services
444
458
  });
445
459
  if (functionDef.meta?.deployedFunctionId) {
446
- Obj4.change(fn4, (fn5) => setUserFunctionIdInMetadata(Obj4.getMeta(fn5), functionDef.meta.deployedFunctionId));
460
+ Obj5.change(fn4, (fn5) => setUserFunctionIdInMetadata(Obj5.getMeta(fn5), functionDef.meta.deployedFunctionId));
447
461
  }
448
462
  return fn4;
449
463
  };
@@ -454,15 +468,15 @@ var deserializeFunction = (functionObj) => {
454
468
  key: functionObj.key ?? functionObj.name,
455
469
  name: functionObj.name,
456
470
  description: functionObj.description,
457
- inputSchema: !functionObj.inputSchema ? Schema5.Unknown : Type5.toEffectSchema(functionObj.inputSchema),
458
- outputSchema: !functionObj.outputSchema ? void 0 : Type5.toEffectSchema(functionObj.outputSchema),
471
+ inputSchema: !functionObj.inputSchema ? Schema5.Unknown : JsonSchema2.toEffectSchema(functionObj.inputSchema),
472
+ outputSchema: !functionObj.outputSchema ? void 0 : JsonSchema2.toEffectSchema(functionObj.outputSchema),
459
473
  // TODO(dmaretskyi): This should throw error.
460
474
  handler: () => {
461
475
  },
462
476
  services: functionObj.services ?? [],
463
477
  types: [],
464
478
  meta: {
465
- deployedFunctionId: getUserFunctionIdInMetadata(Obj4.getMeta(functionObj))
479
+ deployedFunctionId: getUserFunctionIdInMetadata(Obj5.getMeta(functionObj))
466
480
  }
467
481
  };
468
482
  };
@@ -532,12 +546,11 @@ var sleep_default = defineFunction({
532
546
  });
533
547
 
534
548
  // src/example/index.ts
535
- (function(Example2) {
536
- Example2.fib = fib_default;
537
- Example2.reply = reply_default;
538
- Example2.sleep = sleep_default;
539
- })(Example || (Example = {}));
540
- var Example;
549
+ var ExampleFunctions = {
550
+ Fibonacci: fib_default,
551
+ Reply: reply_default,
552
+ Sleep: sleep_default
553
+ };
541
554
 
542
555
  // src/services/credentials.ts
543
556
  import * as HttpClient from "@effect/platform/HttpClient";
@@ -633,7 +646,7 @@ import * as Context3 from "effect/Context";
633
646
  import * as Effect7 from "effect/Effect";
634
647
  import * as Layer3 from "effect/Layer";
635
648
  import * as Schema9 from "effect/Schema";
636
- import { Obj as Obj6, Type as Type6 } from "@dxos/echo";
649
+ import { Obj as Obj7, Type as Type4 } from "@dxos/echo";
637
650
  import { invariant } from "@dxos/invariant";
638
651
  import { LogLevel, log as log2 } from "@dxos/log";
639
652
 
@@ -642,7 +655,7 @@ import * as Context2 from "effect/Context";
642
655
  import * as Effect6 from "effect/Effect";
643
656
  import * as Layer2 from "effect/Layer";
644
657
  import { AgentStatus } from "@dxos/ai";
645
- import { Obj as Obj5 } from "@dxos/echo";
658
+ import { Obj as Obj6 } from "@dxos/echo";
646
659
  import { ObjectId } from "@dxos/keys";
647
660
  import { Message } from "@dxos/types";
648
661
  var TracingService = class _TracingService extends Context2.Tag("@dxos/functions/TracingService")() {
@@ -685,7 +698,7 @@ var TracingService = class _TracingService extends Context2.Tag("@dxos/functions
685
698
  */
686
699
  static emitStatus = Effect6.fnUntraced(function* (data) {
687
700
  const tracing = yield* _TracingService;
688
- tracing.write(Obj5.make(AgentStatus, {
701
+ tracing.write(Obj6.make(AgentStatus, {
689
702
  parentMessage: tracing.getTraceContext().parentMessage,
690
703
  toolCallId: tracing.getTraceContext().toolCallId,
691
704
  created: (/* @__PURE__ */ new Date()).toISOString(),
@@ -694,7 +707,7 @@ var TracingService = class _TracingService extends Context2.Tag("@dxos/functions
694
707
  });
695
708
  static emitConverationMessage = Effect6.fnUntraced(function* (data) {
696
709
  const tracing = yield* _TracingService;
697
- tracing.write(Obj5.make(Message.Message, {
710
+ tracing.write(Obj6.make(Message.Message, {
698
711
  parentMessage: tracing.getTraceContext().parentMessage,
699
712
  ...data,
700
713
  properties: {
@@ -739,8 +752,8 @@ var ComputeEventPayload = Schema9.Union(Schema9.Struct({
739
752
  }));
740
753
  var ComputeEvent = Schema9.Struct({
741
754
  payload: ComputeEventPayload
742
- }).pipe(Type6.object({
743
- typename: "dxos.org/type/ComputeEvent",
755
+ }).pipe(Type4.object({
756
+ typename: "org.dxos.type.compute-event",
744
757
  version: "0.1.0"
745
758
  }));
746
759
  var ComputeEventLogger = class _ComputeEventLogger extends Context3.Tag("@dxos/functions/ComputeEventLogger")() {
@@ -756,7 +769,7 @@ var ComputeEventLogger = class _ComputeEventLogger extends Context3.Tag("@dxos/f
756
769
  const tracing = yield* TracingService;
757
770
  return {
758
771
  log: (event) => {
759
- tracing.write(Obj6.make(ComputeEvent, {
772
+ tracing.write(Obj7.make(ComputeEvent, {
760
773
  payload: event
761
774
  }), tracing.getTraceContext());
762
775
  },
@@ -872,9 +885,9 @@ import * as SchemaAST2 from "effect/SchemaAST";
872
885
  import { AiModelResolver, AiService } from "@dxos/ai";
873
886
  import { AnthropicResolver } from "@dxos/ai/resolvers";
874
887
  import { LifecycleState, Resource } from "@dxos/context";
875
- import { Database as Database2, Ref as Ref2, Type as Type7 } from "@dxos/echo";
888
+ import { Database as Database2, Feed, JsonSchema as JsonSchema3, Ref as Ref5 } from "@dxos/echo";
876
889
  import { refFromEncodedReference } from "@dxos/echo/internal";
877
- import { EchoClient } from "@dxos/echo-db";
890
+ import { EchoClient, createFeedServiceLayer } from "@dxos/echo-db";
878
891
  import { runAndForwardErrors } from "@dxos/effect";
879
892
  import { assertState, failedInvariant as failedInvariant2, invariant as invariant2 } from "@dxos/invariant";
880
893
  import { PublicKey } from "@dxos/keys";
@@ -1010,12 +1023,12 @@ var wrapFunctionHandler = (func) => {
1010
1023
  key: func.key,
1011
1024
  name: func.name,
1012
1025
  description: func.description,
1013
- inputSchema: Type7.toJsonSchema(func.inputSchema),
1014
- outputSchema: func.outputSchema === void 0 ? void 0 : Type7.toJsonSchema(func.outputSchema),
1026
+ inputSchema: JsonSchema3.toJsonSchema(func.inputSchema),
1027
+ outputSchema: func.outputSchema === void 0 ? void 0 : JsonSchema3.toJsonSchema(func.outputSchema),
1015
1028
  services: func.services
1016
1029
  },
1017
1030
  handler: async ({ data, context }) => {
1018
- if ((func.services.includes(Database2.Service.key) || func.services.includes(QueueService.key)) && (!context.services.dataService || !context.services.queryService)) {
1031
+ if ((func.services.includes(Database2.Service.key) || func.services.includes(QueueService.key) || func.services.includes(Feed.Service.key)) && (!context.services.dataService || !context.services.queryService)) {
1019
1032
  throw new FunctionError({
1020
1033
  message: "Services not provided: dataService, queryService"
1021
1034
  });
@@ -1041,7 +1054,7 @@ var wrapFunctionHandler = (func) => {
1041
1054
  if (func.types.length > 0) {
1042
1055
  invariant2(funcContext.db, "Database is required for functions with types", {
1043
1056
  F: __dxlog_file3,
1044
- L: 68,
1057
+ L: 70,
1045
1058
  S: void 0,
1046
1059
  A: [
1047
1060
  "funcContext.db",
@@ -1112,6 +1125,7 @@ var FunctionContext = class extends Resource {
1112
1125
  assertState(this._lifecycleState === LifecycleState.OPEN, "FunctionContext is not open");
1113
1126
  const dbLayer = this.db ? Database2.layer(this.db) : Database2.notAvailable;
1114
1127
  const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
1128
+ const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
1115
1129
  const credentials = dbLayer ? CredentialsService.layerFromDatabase({
1116
1130
  caching: true
1117
1131
  }).pipe(Layer7.provide(dbLayer)) : CredentialsService.configuredLayer([]);
@@ -1121,7 +1135,7 @@ var FunctionContext = class extends Resource {
1121
1135
  // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
1122
1136
  apiUrl: "http://internal/provider/anthropic"
1123
1137
  }).pipe(Layer7.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))))))) : AiService.notAvailable;
1124
- return Layer7.mergeAll(dbLayer, queuesLayer, credentials, functionInvocationService, aiLayer, tracing);
1138
+ return Layer7.mergeAll(dbLayer, queuesLayer, feedLayer, credentials, functionInvocationService, aiLayer, tracing);
1125
1139
  }
1126
1140
  };
1127
1141
  var MockedFunctionInvocationService = Layer7.succeed(FunctionInvocationService, {
@@ -1133,8 +1147,8 @@ var decodeRefsFromSchema = (ast, value2, db) => {
1133
1147
  return value2;
1134
1148
  }
1135
1149
  const encoded = SchemaAST2.encodedBoundAST(ast);
1136
- if (Ref2.isRefType(encoded)) {
1137
- if (Ref2.isRef(value2)) {
1150
+ if (Ref5.isRefType(encoded)) {
1151
+ if (Ref5.isRef(value2)) {
1138
1152
  return value2;
1139
1153
  }
1140
1154
  if (typeof value2 === "object" && value2 !== null && typeof value2["/"] === "string") {
@@ -1198,7 +1212,7 @@ export {
1198
1212
  ConfiguredCredentialsService,
1199
1213
  ContextQueueService,
1200
1214
  CredentialsService,
1201
- Example,
1215
+ ExampleFunctions,
1202
1216
  FUNCTIONS_META_KEY,
1203
1217
  FUNCTIONS_PRESET_META_KEY,
1204
1218
  Function_exports as Function,