@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fcc0d83b33

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.
Files changed (188) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/Trace.mjs +42 -0
  3. package/dist/lib/neutral/Trace.mjs.map +7 -0
  4. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  5. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  6. package/dist/lib/neutral/chunk-LI2RBI7V.mjs +49 -0
  7. package/dist/lib/neutral/chunk-LI2RBI7V.mjs.map +7 -0
  8. package/dist/lib/neutral/chunk-VOFFKJP4.mjs +178 -0
  9. package/dist/lib/neutral/chunk-VOFFKJP4.mjs.map +7 -0
  10. package/dist/lib/neutral/fib-WEAWVJIJ.mjs +23 -0
  11. package/dist/lib/neutral/fib-WEAWVJIJ.mjs.map +7 -0
  12. package/dist/lib/neutral/index.mjs +1082 -0
  13. package/dist/lib/neutral/index.mjs.map +7 -0
  14. package/dist/lib/neutral/meta.json +1 -0
  15. package/dist/lib/neutral/reply-AIAJ3PTW.mjs +19 -0
  16. package/dist/lib/neutral/reply-AIAJ3PTW.mjs.map +7 -0
  17. package/dist/lib/neutral/sleep-M4YMO6FP.mjs +15 -0
  18. package/dist/lib/neutral/sleep-M4YMO6FP.mjs.map +7 -0
  19. package/dist/types/src/Trace.d.ts +174 -0
  20. package/dist/types/src/Trace.d.ts.map +1 -0
  21. package/dist/types/src/errors.d.ts +179 -0
  22. package/dist/types/src/errors.d.ts.map +1 -0
  23. package/dist/types/src/example/definitions.d.ts +11 -0
  24. package/dist/types/src/example/definitions.d.ts.map +1 -0
  25. package/dist/types/src/example/fib.d.ts +8 -0
  26. package/dist/types/src/example/fib.d.ts.map +1 -0
  27. package/dist/types/src/example/forex-effect.d.ts +3 -0
  28. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  29. package/dist/types/src/example/index.d.ts +4 -0
  30. package/dist/types/src/example/index.d.ts.map +1 -0
  31. package/dist/types/src/example/reply.d.ts +4 -0
  32. package/dist/types/src/example/reply.d.ts.map +1 -0
  33. package/dist/types/src/example/sleep.d.ts +6 -0
  34. package/dist/types/src/example/sleep.d.ts.map +1 -0
  35. package/dist/types/src/index.d.ts +9 -6
  36. package/dist/types/src/index.d.ts.map +1 -1
  37. package/dist/types/src/process/Process.d.ts +246 -0
  38. package/dist/types/src/process/Process.d.ts.map +1 -0
  39. package/dist/types/src/process/ServiceResolver.d.ts +74 -0
  40. package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
  41. package/dist/types/src/process/StorageService.d.ts +58 -0
  42. package/dist/types/src/process/StorageService.d.ts.map +1 -0
  43. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  44. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  45. package/dist/types/src/protocol/index.d.ts +2 -0
  46. package/dist/types/src/protocol/index.d.ts.map +1 -0
  47. package/dist/types/src/protocol/protocol.d.ts +7 -0
  48. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  49. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  50. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  51. package/dist/types/src/sdk.d.ts +11 -0
  52. package/dist/types/src/sdk.d.ts.map +1 -0
  53. package/dist/types/src/services/credentials.d.ts +23 -3
  54. package/dist/types/src/services/credentials.d.ts.map +1 -1
  55. package/dist/types/src/services/function-invocation-service.d.ts +16 -0
  56. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  57. package/dist/types/src/services/index.d.ts +4 -8
  58. package/dist/types/src/services/index.d.ts.map +1 -1
  59. package/dist/types/src/services/queues.d.ts +1 -17
  60. package/dist/types/src/services/queues.d.ts.map +1 -1
  61. package/dist/types/src/services/tracing.d.ts +4 -14
  62. package/dist/types/src/services/tracing.d.ts.map +1 -1
  63. package/dist/types/src/types/Script.d.ts +22 -0
  64. package/dist/types/src/types/Script.d.ts.map +1 -0
  65. package/dist/types/src/types/Trigger.d.ts +150 -0
  66. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  67. package/dist/types/src/types/TriggerEvent.d.ts +75 -0
  68. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  69. package/dist/types/src/types/index.d.ts +5 -0
  70. package/dist/types/src/types/index.d.ts.map +1 -0
  71. package/dist/types/src/types/url.d.ts +13 -0
  72. package/dist/types/src/types/url.d.ts.map +1 -0
  73. package/dist/types/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +32 -67
  75. package/src/Trace.ts +231 -0
  76. package/src/errors.ts +31 -0
  77. package/src/example/definitions.ts +49 -0
  78. package/src/example/fib.ts +23 -0
  79. package/src/example/forex-effect.ts +40 -0
  80. package/src/example/index.ts +12 -0
  81. package/src/example/reply.ts +19 -0
  82. package/src/example/sleep.ts +17 -0
  83. package/src/index.ts +9 -8
  84. package/src/process/Process.ts +489 -0
  85. package/src/process/ServiceResolver.ts +174 -0
  86. package/src/process/StorageService.ts +99 -0
  87. package/src/protocol/functions-ai-http-client.ts +67 -0
  88. package/src/{executor → protocol}/index.ts +1 -1
  89. package/src/protocol/protocol.test.ts +58 -0
  90. package/src/protocol/protocol.ts +283 -0
  91. package/src/sdk.ts +30 -0
  92. package/src/services/credentials.ts +108 -4
  93. package/src/services/function-invocation-service.ts +35 -0
  94. package/src/services/index.ts +4 -8
  95. package/src/services/queues.ts +1 -38
  96. package/src/services/tracing.ts +4 -23
  97. package/src/types/Script.ts +38 -0
  98. package/src/types/Trigger.ts +207 -0
  99. package/src/types/TriggerEvent.ts +62 -0
  100. package/src/types/index.ts +8 -0
  101. package/src/types/url.ts +32 -0
  102. package/dist/lib/browser/bundler/index.mjs +0 -247
  103. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  104. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  105. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  106. package/dist/lib/browser/edge/index.mjs +0 -69
  107. package/dist/lib/browser/edge/index.mjs.map +0 -7
  108. package/dist/lib/browser/index.mjs +0 -492
  109. package/dist/lib/browser/index.mjs.map +0 -7
  110. package/dist/lib/browser/meta.json +0 -1
  111. package/dist/lib/browser/testing/index.mjs +0 -79
  112. package/dist/lib/browser/testing/index.mjs.map +0 -7
  113. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  114. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  115. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  116. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  117. package/dist/lib/node-esm/edge/index.mjs +0 -71
  118. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  119. package/dist/lib/node-esm/index.mjs +0 -493
  120. package/dist/lib/node-esm/index.mjs.map +0 -7
  121. package/dist/lib/node-esm/meta.json +0 -1
  122. package/dist/lib/node-esm/testing/index.mjs +0 -80
  123. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  124. package/dist/types/src/bundler/bundler.d.ts +0 -50
  125. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  126. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  127. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  128. package/dist/types/src/bundler/index.d.ts +0 -2
  129. package/dist/types/src/bundler/index.d.ts.map +0 -1
  130. package/dist/types/src/edge/functions.d.ts +0 -16
  131. package/dist/types/src/edge/functions.d.ts.map +0 -1
  132. package/dist/types/src/edge/index.d.ts +0 -2
  133. package/dist/types/src/edge/index.d.ts.map +0 -1
  134. package/dist/types/src/executor/executor.d.ts +0 -8
  135. package/dist/types/src/executor/executor.d.ts.map +0 -1
  136. package/dist/types/src/executor/index.d.ts +0 -2
  137. package/dist/types/src/executor/index.d.ts.map +0 -1
  138. package/dist/types/src/handler.d.ts +0 -61
  139. package/dist/types/src/handler.d.ts.map +0 -1
  140. package/dist/types/src/schema.d.ts +0 -38
  141. package/dist/types/src/schema.d.ts.map +0 -1
  142. package/dist/types/src/services/ai.d.ts +0 -12
  143. package/dist/types/src/services/ai.d.ts.map +0 -1
  144. package/dist/types/src/services/database.d.ts +0 -15
  145. package/dist/types/src/services/database.d.ts.map +0 -1
  146. package/dist/types/src/services/event-logger.d.ts +0 -37
  147. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  148. package/dist/types/src/services/function-call-service.d.ts +0 -16
  149. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  150. package/dist/types/src/services/service-container.d.ts +0 -46
  151. package/dist/types/src/services/service-container.d.ts.map +0 -1
  152. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  153. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  154. package/dist/types/src/testing/index.d.ts +0 -2
  155. package/dist/types/src/testing/index.d.ts.map +0 -1
  156. package/dist/types/src/testing/logger.d.ts +0 -5
  157. package/dist/types/src/testing/logger.d.ts.map +0 -1
  158. package/dist/types/src/testing/services.d.ts +0 -71
  159. package/dist/types/src/testing/services.d.ts.map +0 -1
  160. package/dist/types/src/trace.d.ts +0 -124
  161. package/dist/types/src/trace.d.ts.map +0 -1
  162. package/dist/types/src/translations.d.ts +0 -12
  163. package/dist/types/src/translations.d.ts.map +0 -1
  164. package/dist/types/src/types.d.ts +0 -411
  165. package/dist/types/src/types.d.ts.map +0 -1
  166. package/dist/types/src/url.d.ts +0 -17
  167. package/dist/types/src/url.d.ts.map +0 -1
  168. package/src/bundler/bundler.test.ts +0 -59
  169. package/src/bundler/bundler.ts +0 -292
  170. package/src/bundler/index.ts +0 -5
  171. package/src/edge/functions.ts +0 -64
  172. package/src/edge/index.ts +0 -9
  173. package/src/executor/executor.ts +0 -54
  174. package/src/handler.ts +0 -113
  175. package/src/schema.ts +0 -57
  176. package/src/services/ai.ts +0 -32
  177. package/src/services/database.ts +0 -50
  178. package/src/services/event-logger.ts +0 -87
  179. package/src/services/function-call-service.ts +0 -64
  180. package/src/services/service-container.ts +0 -127
  181. package/src/services/tool-resolver.ts +0 -31
  182. package/src/testing/index.ts +0 -5
  183. package/src/testing/logger.ts +0 -16
  184. package/src/testing/services.ts +0 -174
  185. package/src/trace.ts +0 -180
  186. package/src/translations.ts +0 -20
  187. package/src/types.ts +0 -211
  188. package/src/url.ts +0 -52
@@ -0,0 +1,35 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+ import * as Context from 'effect/Context';
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
7
+
8
+ import { Operation } from '@dxos/operation';
9
+
10
+ import type { FunctionNotFoundError } from '../errors';
11
+
12
+ export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
13
+ FunctionInvocationService,
14
+ {
15
+ invokeFunction<I, O>(functionDef: Operation.Definition<I, O, any>, input: I): Effect.Effect<O>;
16
+
17
+ resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
18
+ }
19
+ >() {
20
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
21
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
22
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
23
+ });
24
+
25
+ static invokeFunction = <I, O>(
26
+ functionDef: Operation.Definition<I, O, any>,
27
+ input: I,
28
+ ): Effect.Effect<O, never, FunctionInvocationService> =>
29
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
30
+
31
+ static resolveFunction = (
32
+ key: string,
33
+ ): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
34
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
35
+ }
@@ -2,12 +2,8 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './ai';
6
- export * from './database';
7
- export * from './queues';
8
- export * from './service-container';
9
5
  export * from './credentials';
10
- export * from './tracing';
11
- export * from './event-logger';
12
- export * from './function-call-service';
13
- export * from './tool-resolver';
6
+ export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
7
+ export * from './function-invocation-service';
8
+ export * from './queues';
9
+ export { MESSAGE_PROPERTY_TOOL_CALL_ID } from './tracing';
@@ -2,41 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Layer } from 'effect';
6
-
7
- import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
8
-
9
- export class QueueService extends Context.Tag('QueueService')<
10
- QueueService,
11
- {
12
- /**
13
- * API to access the queues.
14
- */
15
- readonly queues: QueueAPI;
16
-
17
- /**
18
- * The queue that is used to store the context of the current research.
19
- */
20
- // TODO(dmaretskyi): Is this really part of the queue service?
21
- readonly contextQueue: Queue | undefined;
22
- }
23
- >() {
24
- static notAvailable = Layer.succeed(QueueService, {
25
- queues: {
26
- get(dxn) {
27
- throw new Error('Queues not available');
28
- },
29
- create() {
30
- throw new Error('Queues not available');
31
- },
32
- },
33
- contextQueue: undefined,
34
- });
35
-
36
- static make = (queues: QueueFactory, contextQueue: Queue | undefined): Context.Tag.Service<QueueService> => {
37
- return {
38
- queues,
39
- contextQueue,
40
- };
41
- };
42
- }
5
+ export { ContextQueueService, QueueService, feedServiceFromQueueServiceLayer } from '@dxos/echo-db';
@@ -2,26 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context } from 'effect';
6
-
7
- import type { AnyEchoObject } from '@dxos/echo-schema';
8
-
9
- export class TracingService extends Context.Tag('TracingService')<
10
- TracingService,
11
- {
12
- /**
13
- * Write an event to the tracing queue.
14
- * @param event - The event to write. Must be an a typed object.
15
- */
16
- write(event: AnyEchoObject): void;
17
- }
18
- >() {
19
- static noop: Context.Tag.Service<TracingService> = { write: () => {} };
20
-
21
- static console: Context.Tag.Service<TracingService> = {
22
- write: (event) => {
23
- // eslint-disable-next-line no-console
24
- console.log(event);
25
- },
26
- };
27
- }
5
+ /**
6
+ * Goes into {@link Message['properties']}
7
+ */
8
+ export const MESSAGE_PROPERTY_TOOL_CALL_ID = 'toolCallId' as const;
@@ -0,0 +1,38 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { Annotation, Obj, Ref, Type } from '@dxos/echo';
8
+ import { FormInputAnnotation } from '@dxos/echo/internal';
9
+ import { Text } from '@dxos/schema';
10
+
11
+ /**
12
+ * Source script.
13
+ */
14
+ export const Script = Schema.Struct({
15
+ name: Schema.String.pipe(Schema.optional),
16
+ description: Schema.String.pipe(Schema.optional),
17
+ // TODO(burdon): Change to hash of deployed content.
18
+ // Whether source has changed since last deploy.
19
+ changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
20
+ source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
21
+ }).pipe(
22
+ Type.object({
23
+ typename: 'org.dxos.type.script',
24
+ version: '0.1.0',
25
+ }),
26
+ Annotation.LabelAnnotation.set(['name']),
27
+ Annotation.IconAnnotation.set({
28
+ icon: 'ph--code--regular',
29
+ hue: 'sky',
30
+ }),
31
+ );
32
+
33
+ export interface Script extends Schema.Schema.Type<typeof Script> {}
34
+
35
+ type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
36
+
37
+ export const make = ({ source = '', ...props }: Props = {}): Script =>
38
+ Obj.make(Script, { ...props, source: Ref.make(Text.make({ content: source })) });
@@ -0,0 +1,207 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+ import * as SchemaAST from 'effect/SchemaAST';
7
+
8
+ import { Annotation, Feed, Obj, QueryAST, Ref, Type, type Query } from '@dxos/echo';
9
+ import { OptionsAnnotationId, SystemTypeAnnotation } from '@dxos/echo/internal';
10
+ import { failedInvariant } from '@dxos/invariant';
11
+ import { DXN } from '@dxos/keys';
12
+
13
+ /**
14
+ * Type discriminator for TriggerType.
15
+ * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
16
+ * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
17
+ */
18
+ export const Kinds = ['email', 'queue', 'subscription', 'timer', 'webhook'] as const;
19
+ export type Kind = (typeof Kinds)[number];
20
+
21
+ const kindLiteralAnnotations = { title: 'Kind' };
22
+
23
+ export const EmailSpec = Schema.Struct({
24
+ kind: Schema.Literal('email').annotations(kindLiteralAnnotations),
25
+ });
26
+ export type EmailSpec = Schema.Schema.Type<typeof EmailSpec>;
27
+
28
+ /**
29
+ * Construct an Email trigger spec.
30
+ */
31
+ export const specEmail = (): EmailSpec => ({ kind: 'email' });
32
+
33
+ // TODO(burdon): Change to Feed.
34
+ // TODO(wittjosiah): Remove. Migrate to Subscription triggers once EDGE supports them for feed queries.
35
+ export const QueueSpec = Schema.Struct({
36
+ kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
37
+
38
+ // TODO(dmaretskyi): Rename to `feed` and change to a reference.
39
+ queue: DXN.Schema,
40
+ });
41
+ export type QueueSpec = Schema.Schema.Type<typeof QueueSpec>;
42
+
43
+ /**
44
+ * Construct a Queue trigger spec from a queue DXN string.
45
+ */
46
+ export const specQueue = (queueDxn: string): QueueSpec => ({
47
+ kind: 'queue',
48
+ queue: queueDxn,
49
+ });
50
+
51
+ /**
52
+ * Construct a Queue trigger spec from a Feed object.
53
+ */
54
+ export const specFeed = (feed: Feed.Feed): QueueSpec =>
55
+ specQueue(Feed.getQueueDxn(feed)?.toString() ?? failedInvariant(new Error('Could not extract DXN from feed')));
56
+
57
+ /**
58
+ * Subscription.
59
+ */
60
+ export const SubscriptionSpec = Schema.Struct({
61
+ kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
62
+ query: Schema.Struct({
63
+ raw: Schema.optional(Schema.String.annotations({ title: 'Query' })),
64
+ ast: QueryAST.Query,
65
+ }),
66
+ options: Schema.optional(
67
+ Schema.Struct({
68
+ // Watch changes to object (not just creation).
69
+ deep: Schema.optional(Schema.Boolean.annotations({ title: 'Nested' })),
70
+ // Debounce changes (delay in ms).
71
+ delay: Schema.optional(Schema.Number.annotations({ title: 'Delay' })),
72
+ }).annotations({ title: 'Options' }),
73
+ ),
74
+ });
75
+ export type SubscriptionSpec = Schema.Schema.Type<typeof SubscriptionSpec>;
76
+
77
+ /**
78
+ * Construct a Subscription trigger spec from a Query object.
79
+ */
80
+ export const specSubscription = (
81
+ query: Query.Query<any>,
82
+ options?: { deep?: boolean; delay?: number },
83
+ ): SubscriptionSpec => ({
84
+ kind: 'subscription',
85
+ query: {
86
+ ast: query.ast,
87
+ },
88
+ options: options
89
+ ? {
90
+ deep: options.deep,
91
+ delay: options.delay,
92
+ }
93
+ : undefined,
94
+ });
95
+
96
+ /**
97
+ * Cron timer.
98
+ */
99
+ export const TimerSpec = Schema.Struct({
100
+ kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
101
+ cron: Schema.String.annotations({
102
+ title: 'Cron',
103
+ [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
104
+ }),
105
+ });
106
+ export type TimerSpec = Schema.Schema.Type<typeof TimerSpec>;
107
+
108
+ /**
109
+ * Construct a Timer trigger spec from a cron string.
110
+ */
111
+ export const specTimer = (cron: string): TimerSpec => ({ kind: 'timer', cron });
112
+
113
+ /**
114
+ * Webhook.
115
+ */
116
+ export const WebhookSpec = Schema.Struct({
117
+ kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
118
+ method: Schema.optional(
119
+ Schema.String.annotations({
120
+ title: 'Method',
121
+ [OptionsAnnotationId]: ['GET', 'POST'],
122
+ }),
123
+ ),
124
+ port: Schema.optional(
125
+ Schema.Number.annotations({
126
+ title: 'Port',
127
+ }),
128
+ ),
129
+ });
130
+ export type WebhookSpec = Schema.Schema.Type<typeof WebhookSpec>;
131
+
132
+ /**
133
+ * Construct a Webhook trigger spec from a method and port.
134
+ */
135
+ export const specWebhook = (opts?: { method?: string; port?: number }): WebhookSpec => ({
136
+ kind: 'webhook',
137
+ method: opts?.method,
138
+ port: opts?.port,
139
+ });
140
+
141
+ /**
142
+ * Trigger schema.
143
+ */
144
+ export const Spec = Schema.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
145
+ title: 'Trigger',
146
+ });
147
+ export type Spec = Schema.Schema.Type<typeof Spec>;
148
+
149
+ /**
150
+ * Function trigger.
151
+ * Function is invoked with the `payload` passed as input data.
152
+ * The event that triggers the function is available in the function context.
153
+ */
154
+ const TriggerSchema = Schema.Struct({
155
+ /**
156
+ * Function or workflow to invoke.
157
+ */
158
+ // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
159
+ function: Schema.optional(Ref.Ref(Obj.Unknown).annotations({ title: 'Function' })),
160
+
161
+ /**
162
+ * Only used for workflowSchema.
163
+ * Specifies the input node in the circuit.
164
+ * @deprecated Remove and enforce a single input node in all compute graphSchema.
165
+ */
166
+ inputNodeId: Schema.optional(Schema.String.annotations({ title: 'Input Node ID' })),
167
+
168
+ // TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
169
+ // Need lint rule; or agent rule to require PR review for "boolean" key word.
170
+ enabled: Schema.optional(Schema.Boolean.annotations({ title: 'Enabled' })),
171
+
172
+ spec: Schema.optional(Spec),
173
+
174
+ concurrency: Schema.optional(
175
+ Schema.Number.annotations({
176
+ title: 'Concurrency',
177
+ default: 1,
178
+ description:
179
+ 'Maximum number of concurrent invocations of the trigger. For Feed triggers, this will process Feed items in parallel.',
180
+ }),
181
+ ),
182
+
183
+ /**
184
+ * Passed as the input data to the function.
185
+ * Must match the function's input schema.
186
+ *
187
+ * @example
188
+ * {
189
+ * item: '{{event.item}}',
190
+ * instructions: 'Summarize and perform entity-extraction'
191
+ * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
192
+ * }
193
+ */
194
+ input: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
195
+ }).pipe(
196
+ Type.object({
197
+ typename: 'org.dxos.type.trigger',
198
+ version: '0.1.0',
199
+ }),
200
+ Annotation.IconAnnotation.set({ icon: 'ph--lightning--regular', hue: 'yellow' }),
201
+ SystemTypeAnnotation.set(true),
202
+ );
203
+
204
+ export interface Trigger extends Schema.Schema.Type<typeof TriggerSchema> {}
205
+ export const Trigger: Type.Obj<Trigger> = TriggerSchema as any;
206
+
207
+ export const make = (props: Obj.MakeProps<typeof Trigger>) => Obj.make(Trigger, props);
@@ -0,0 +1,62 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+
7
+ import { DXN, Obj, Ref } from '@dxos/echo';
8
+
9
+ // TODO(wittjosiah): Review this type.
10
+ // - Should be discriminated union.
11
+ // - Should be more consistent (e.g. subject vs item).
12
+ // - Should re-use schemas if possible.
13
+
14
+ // TODO(burdon): Reuse trigger schema from @dxos/functions (TriggerType).
15
+ export const EmailEvent = Schema.Struct({
16
+ from: Schema.String,
17
+ to: Schema.String,
18
+ subject: Schema.String,
19
+ created: Schema.String,
20
+ body: Schema.String,
21
+ });
22
+ export type EmailEvent = Schema.Schema.Type<typeof EmailEvent>;
23
+
24
+ export const QueueEvent = Schema.Struct({
25
+ queue: DXN.Schema,
26
+ item: Schema.Any,
27
+ cursor: Schema.String,
28
+ });
29
+ export type QueueEvent = Schema.Schema.Type<typeof QueueEvent>;
30
+
31
+ export const SubscriptionEvent = Schema.Struct({
32
+ /**
33
+ * Type of the mutation.
34
+ */
35
+ // TODO(dmaretskyi): Specify enum.
36
+ type: Schema.String,
37
+
38
+ /**
39
+ * Reference to the object that was changed or created.
40
+ */
41
+ subject: Ref.Ref(Obj.Unknown),
42
+
43
+ /**
44
+ * @deprecated
45
+ */
46
+ changedObjectId: Schema.optional(Schema.String),
47
+ });
48
+ export type SubscriptionEvent = Schema.Schema.Type<typeof SubscriptionEvent>;
49
+
50
+ export const TimerEvent = Schema.Struct({ tick: Schema.Number });
51
+ export type TimerEvent = Schema.Schema.Type<typeof TimerEvent>;
52
+
53
+ export const WebhookEvent = Schema.Struct({
54
+ url: Schema.String,
55
+ method: Schema.Literal('GET', 'POST'),
56
+ headers: Schema.Record({ key: Schema.String, value: Schema.String }),
57
+ bodyText: Schema.String,
58
+ });
59
+ export type WebhookEvent = Schema.Schema.Type<typeof WebhookEvent>;
60
+
61
+ export const TriggerEvent = Schema.Union(EmailEvent, QueueEvent, SubscriptionEvent, TimerEvent, WebhookEvent);
62
+ export type TriggerEvent = Schema.Schema.Type<typeof TriggerEvent>;
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * as Script from './Script';
6
+ export * as Trigger from './Trigger';
7
+ export * as TriggerEvent from './TriggerEvent';
8
+ export * from './url';
@@ -0,0 +1,32 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Obj } from '@dxos/echo';
6
+
7
+ // TODO: use URL scheme for source?
8
+ export const FUNCTIONS_META_KEY = 'org.dxos.service.function';
9
+
10
+ export const FUNCTIONS_PRESET_META_KEY = 'org.dxos.service.function-preset';
11
+
12
+ /**
13
+ * NOTE: functionId is backend ID, not ECHO object id.
14
+ */
15
+ export const getUserFunctionIdInMetadata = (meta: Obj.ReadonlyMeta) => {
16
+ return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
17
+ };
18
+
19
+ /**
20
+ * NOTE: functionId is backend ID, not ECHO object id.
21
+ * Must be called inside Obj.update() since it mutates the meta.
22
+ */
23
+ export const setUserFunctionIdInMetadata = (meta: Obj.Meta, functionId: string) => {
24
+ const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
25
+ if (key) {
26
+ if (key.id !== functionId) {
27
+ throw new Error('Metadata mismatch');
28
+ }
29
+ } else {
30
+ meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionId });
31
+ }
32
+ };