@dxos/functions 0.8.4-main.ef1bc66f44 → 0.8.4-main.effb148878

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 (79) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/neutral/index.mjs +291 -925
  4. package/dist/lib/neutral/index.mjs.map +4 -4
  5. package/dist/lib/neutral/meta.json +1 -1
  6. package/dist/types/src/index.d.ts +0 -2
  7. package/dist/types/src/index.d.ts.map +1 -1
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -1
  9. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  10. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  11. package/dist/types/src/protocol/protocol.d.ts +19 -2
  12. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  13. package/dist/types/src/sdk.d.ts +5 -109
  14. package/dist/types/src/sdk.d.ts.map +1 -1
  15. package/dist/types/src/services/credentials.d.ts +15 -38
  16. package/dist/types/src/services/credentials.d.ts.map +1 -1
  17. package/dist/types/src/services/function-invocation-service.d.ts +8 -6
  18. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  19. package/dist/types/src/services/index.d.ts +1 -5
  20. package/dist/types/src/services/index.d.ts.map +1 -1
  21. package/dist/types/src/services/tracing.d.ts +1 -84
  22. package/dist/types/src/services/tracing.d.ts.map +1 -1
  23. package/dist/types/src/types/index.d.ts +0 -4
  24. package/dist/types/src/types/index.d.ts.map +1 -1
  25. package/dist/types/src/types/url.d.ts +3 -3
  26. package/dist/types/src/types/url.d.ts.map +1 -1
  27. package/dist/types/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +18 -17
  29. package/src/index.ts +0 -2
  30. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  31. package/src/protocol/functions-ai-http-client.ts +75 -1
  32. package/src/protocol/protocol.test.ts +9 -10
  33. package/src/protocol/protocol.ts +244 -60
  34. package/src/sdk.ts +11 -271
  35. package/src/services/credentials.ts +76 -120
  36. package/src/services/function-invocation-service.ts +9 -10
  37. package/src/services/index.ts +1 -5
  38. package/src/services/tracing.ts +0 -159
  39. package/src/types/index.ts +0 -4
  40. package/src/types/url.ts +3 -3
  41. package/dist/types/src/errors.d.ts +0 -121
  42. package/dist/types/src/errors.d.ts.map +0 -1
  43. package/dist/types/src/example/fib.d.ts +0 -7
  44. package/dist/types/src/example/fib.d.ts.map +0 -1
  45. package/dist/types/src/example/forex-effect.d.ts +0 -3
  46. package/dist/types/src/example/forex-effect.d.ts.map +0 -1
  47. package/dist/types/src/example/index.d.ts +0 -12
  48. package/dist/types/src/example/index.d.ts.map +0 -1
  49. package/dist/types/src/example/reply.d.ts +0 -3
  50. package/dist/types/src/example/reply.d.ts.map +0 -1
  51. package/dist/types/src/example/sleep.d.ts +0 -5
  52. package/dist/types/src/example/sleep.d.ts.map +0 -1
  53. package/dist/types/src/operation-compatibility.test.d.ts +0 -2
  54. package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
  55. package/dist/types/src/services/event-logger.d.ts +0 -81
  56. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  57. package/dist/types/src/services/queues.d.ts +0 -47
  58. package/dist/types/src/services/queues.d.ts.map +0 -1
  59. package/dist/types/src/types/Function.d.ts +0 -52
  60. package/dist/types/src/types/Function.d.ts.map +0 -1
  61. package/dist/types/src/types/Script.d.ts +0 -21
  62. package/dist/types/src/types/Script.d.ts.map +0 -1
  63. package/dist/types/src/types/Trigger.d.ts +0 -121
  64. package/dist/types/src/types/Trigger.d.ts.map +0 -1
  65. package/dist/types/src/types/TriggerEvent.d.ts +0 -74
  66. package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
  67. package/src/errors.ts +0 -21
  68. package/src/example/fib.ts +0 -32
  69. package/src/example/forex-effect.ts +0 -40
  70. package/src/example/index.ts +0 -13
  71. package/src/example/reply.ts +0 -21
  72. package/src/example/sleep.ts +0 -24
  73. package/src/operation-compatibility.test.ts +0 -185
  74. package/src/services/event-logger.ts +0 -127
  75. package/src/services/queues.ts +0 -82
  76. package/src/types/Function.ts +0 -82
  77. package/src/types/Script.ts +0 -34
  78. package/src/types/Trigger.ts +0 -143
  79. package/src/types/TriggerEvent.ts +0 -62
@@ -1,185 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import * as Effect from 'effect/Effect';
6
- import * as Schema from 'effect/Schema';
7
- import { describe, expect, test } from 'vitest';
8
-
9
- import { FunctionDefinition, defineFunction } from './sdk';
10
-
11
- describe('Function/Operation Compatibility', () => {
12
- test('can convert FunctionDefinition to OperationDefinition', () => {
13
- const func = defineFunction({
14
- key: 'test.function',
15
- name: 'Test Function',
16
- description: 'A test function',
17
- inputSchema: Schema.Struct({ value: Schema.Number }),
18
- outputSchema: Schema.Struct({ result: Schema.String }),
19
- handler: async ({ data }) => {
20
- return { result: data.value.toString() };
21
- },
22
- });
23
-
24
- const op = FunctionDefinition.toOperation(func);
25
-
26
- expect(op.meta.key).toBe('test.function');
27
- expect(op.meta.name).toBe('Test Function');
28
- expect(op.meta.description).toBe('A test function');
29
- expect(Schema.isSchema(op.schema.input)).toBe(true);
30
- expect(Schema.isSchema(op.schema.output)).toBe(true);
31
- });
32
-
33
- test('converted operation has matching schemas', () => {
34
- const inputSchema = Schema.Struct({
35
- name: Schema.String,
36
- age: Schema.Number,
37
- });
38
- const outputSchema = Schema.Struct({
39
- greeting: Schema.String,
40
- });
41
-
42
- const func = defineFunction({
43
- key: 'test.greet',
44
- name: 'Greet',
45
- inputSchema,
46
- outputSchema,
47
- handler: async ({ data }) => {
48
- return { greeting: `Hello, ${data.name}!` };
49
- },
50
- });
51
-
52
- const op = FunctionDefinition.toOperation(func);
53
-
54
- // Verify schemas match
55
- const testInput = { name: 'Alice', age: 30 };
56
- const validatedInput = Schema.decodeSync(op.schema.input)(testInput);
57
- expect(validatedInput).toEqual(testInput);
58
-
59
- const testOutput = { greeting: 'Hello, Alice!' };
60
- const validatedOutput = Schema.decodeSync(op.schema.output)(testOutput);
61
- expect(validatedOutput).toEqual(testOutput);
62
- });
63
-
64
- test('converted operation preserves metadata', () => {
65
- const func = defineFunction({
66
- key: 'test.meta',
67
- name: 'Meta Test',
68
- description: 'Tests metadata preservation',
69
- inputSchema: Schema.Void,
70
- outputSchema: Schema.Void,
71
- handler: async () => {},
72
- });
73
-
74
- const op = FunctionDefinition.toOperation(func);
75
-
76
- expect(op.meta.key).toBe('test.meta');
77
- expect(op.meta.name).toBe('Meta Test');
78
- expect(op.meta.description).toBe('Tests metadata preservation');
79
- });
80
-
81
- test('converted operation is pipeable', () => {
82
- const func = defineFunction({
83
- key: 'test.pipe',
84
- name: 'Pipe Test',
85
- inputSchema: Schema.Void,
86
- outputSchema: Schema.Void,
87
- handler: async () => {},
88
- });
89
-
90
- const op = FunctionDefinition.toOperation(func);
91
-
92
- expect(typeof op.pipe).toBe('function');
93
- });
94
-
95
- test('handles functions without output schema', () => {
96
- const func = defineFunction({
97
- key: 'test.no-output',
98
- name: 'No Output',
99
- inputSchema: Schema.Struct({ value: Schema.Number }),
100
- handler: async ({ data }) => {
101
- return data.value * 2;
102
- },
103
- });
104
-
105
- const op = FunctionDefinition.toOperation(func);
106
-
107
- expect(op.meta.key).toBe('test.no-output');
108
- expect(Schema.isSchema(op.schema.output)).toBe(true);
109
- });
110
-
111
- test('converted operation includes handler', () => {
112
- const func = defineFunction({
113
- key: 'test.handler',
114
- name: 'Handler Test',
115
- inputSchema: Schema.Struct({ value: Schema.Number }),
116
- outputSchema: Schema.Struct({ doubled: Schema.Number }),
117
- handler: async ({ data }) => {
118
- return { doubled: data.value * 2 };
119
- },
120
- });
121
-
122
- const op = FunctionDefinition.toOperation(func);
123
-
124
- expect(op.handler).toBeDefined();
125
- expect(typeof op.handler).toBe('function');
126
- });
127
-
128
- test('converted operation has handler function', () => {
129
- const func = defineFunction({
130
- key: 'test.execute',
131
- name: 'Execute Test',
132
- inputSchema: Schema.Struct({ value: Schema.Number }),
133
- outputSchema: Schema.Struct({ result: Schema.Number }),
134
- handler: async ({ data }) => {
135
- return { result: data.value * 3 };
136
- },
137
- });
138
-
139
- const op = FunctionDefinition.toOperation(func);
140
-
141
- // Handler is present and is a function that returns an Effect
142
- expect(op.handler).toBeDefined();
143
- const effect = op.handler({ value: 5 });
144
- // Verify it returns an Effect-like object
145
- expect(effect).toHaveProperty('pipe');
146
- });
147
-
148
- test('converted operation works with Effect-based handlers', () => {
149
- const func = defineFunction({
150
- key: 'test.effect',
151
- name: 'Effect Test',
152
- inputSchema: Schema.Struct({ value: Schema.Number }),
153
- outputSchema: Schema.Struct({ result: Schema.Number }),
154
- handler: Effect.fn(function* ({ data }) {
155
- return { result: data.value * 4 };
156
- }),
157
- });
158
-
159
- const op = FunctionDefinition.toOperation(func);
160
-
161
- // Handler is present and produces an Effect
162
- expect(op.handler).toBeDefined();
163
- const effect = op.handler({ value: 3 });
164
- expect(effect).toHaveProperty('pipe');
165
- });
166
-
167
- test('converted operation works with synchronous handlers', () => {
168
- const func = defineFunction({
169
- key: 'test.sync',
170
- name: 'Sync Test',
171
- inputSchema: Schema.Struct({ value: Schema.Number }),
172
- outputSchema: Schema.Struct({ result: Schema.Number }),
173
- handler: ({ data }) => {
174
- return { result: data.value * 5 };
175
- },
176
- });
177
-
178
- const op = FunctionDefinition.toOperation(func);
179
-
180
- // Handler is present and produces an Effect
181
- expect(op.handler).toBeDefined();
182
- const effect = op.handler({ value: 2 });
183
- expect(effect).toHaveProperty('pipe');
184
- });
185
- });
@@ -1,127 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import * as Context from 'effect/Context';
6
- import * as Effect from 'effect/Effect';
7
- import * as Layer from 'effect/Layer';
8
- import * as Schema from 'effect/Schema';
9
-
10
- import { Obj, Type } from '@dxos/echo';
11
- import { invariant } from '@dxos/invariant';
12
- import { LogLevel, log } from '@dxos/log';
13
-
14
- import { TracingService } from './tracing';
15
-
16
- export const ComputeEventPayload = Schema.Union(
17
- Schema.Struct({
18
- type: Schema.Literal('begin-compute'),
19
- nodeId: Schema.String,
20
- /**
21
- * Names of the inputs begin computed.
22
- */
23
- inputs: Schema.Array(Schema.String),
24
- }),
25
- Schema.Struct({
26
- type: Schema.Literal('end-compute'),
27
- nodeId: Schema.String,
28
- /**
29
- * Names of the outputs computed.
30
- */
31
- outputs: Schema.Array(Schema.String),
32
- }),
33
- Schema.Struct({
34
- type: Schema.Literal('compute-input'),
35
- nodeId: Schema.String,
36
- property: Schema.String,
37
- value: Schema.Any,
38
- }),
39
- Schema.Struct({
40
- type: Schema.Literal('compute-output'),
41
- nodeId: Schema.String,
42
- property: Schema.String,
43
- value: Schema.Any,
44
- }),
45
- Schema.Struct({
46
- type: Schema.Literal('custom'),
47
- nodeId: Schema.String,
48
- event: Schema.Any,
49
- }),
50
- );
51
- export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>;
52
-
53
- export const ComputeEvent = Schema.Struct({
54
- payload: ComputeEventPayload,
55
- }).pipe(Type.object({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
56
-
57
- /**
58
- * Logs event for the compute workflows.
59
- */
60
- export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEventLogger')<
61
- ComputeEventLogger,
62
- { readonly log: (event: ComputeEventPayload) => void; readonly nodeId: string | undefined }
63
- >() {
64
- static noop: Context.Tag.Service<ComputeEventLogger> = {
65
- log: () => {},
66
- nodeId: undefined,
67
- };
68
-
69
- /**
70
- * Implements ComputeEventLogger using TracingService.
71
- */
72
- static layerFromTracing = Layer.effect(
73
- ComputeEventLogger,
74
- Effect.gen(function* () {
75
- const tracing = yield* TracingService;
76
- return {
77
- log: (event: ComputeEventPayload) => {
78
- tracing.write(Obj.make(ComputeEvent, { payload: event }), tracing.getTraceContext());
79
- },
80
- nodeId: undefined,
81
- };
82
- }),
83
- );
84
- }
85
-
86
- export const logCustomEvent = (data: any) =>
87
- Effect.gen(function* () {
88
- const logger = yield* ComputeEventLogger;
89
- if (!logger.nodeId) {
90
- throw new Error('logCustomEvent must be called within a node compute function');
91
- }
92
- logger.log({
93
- type: 'custom',
94
- nodeId: logger.nodeId,
95
- event: data,
96
- });
97
- });
98
-
99
- export const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>) =>
100
- Effect.catchAll((error) =>
101
- Effect.gen(function* () {
102
- log.error('unhandled effect error', { error });
103
- throw error;
104
- }),
105
- );
106
-
107
- export const createEventLogger = (
108
- level: LogLevel,
109
- message: string = 'event',
110
- ): Context.Tag.Service<ComputeEventLogger> => {
111
- const logFunction = (
112
- {
113
- [LogLevel.WARN]: log.warn,
114
- [LogLevel.VERBOSE]: log.verbose,
115
- [LogLevel.DEBUG]: log.debug,
116
- [LogLevel.INFO]: log.info,
117
- [LogLevel.ERROR]: log.error,
118
- } as any
119
- )[level];
120
- invariant(logFunction);
121
- return {
122
- log: (event: ComputeEventPayload) => {
123
- logFunction(message, event);
124
- },
125
- nodeId: undefined,
126
- };
127
- };
@@ -1,82 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import * as Context from 'effect/Context';
6
- import * as Effect from 'effect/Effect';
7
- import * as Layer from 'effect/Layer';
8
-
9
- import type { Entity } from '@dxos/echo';
10
- import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
11
- import type { DXN, QueueSubspaceTag } from '@dxos/keys';
12
-
13
- /**
14
- * Gives access to all queues.
15
- */
16
- export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
17
- QueueService,
18
- {
19
- /**
20
- * API to access the queues.
21
- */
22
- readonly queues: QueueAPI;
23
-
24
- /**
25
- * The queue that is used to store the context of the current research.
26
- * @deprecated Use `ContextQueueService` instead.
27
- */
28
- readonly queue: Queue | undefined;
29
- }
30
- >() {
31
- static notAvailable = Layer.succeed(QueueService, {
32
- queues: {
33
- get(_dxn) {
34
- throw new Error('Queues not available');
35
- },
36
- create() {
37
- throw new Error('Queues not available');
38
- },
39
- },
40
- queue: undefined,
41
- });
42
-
43
- static make = (queues: QueueFactory, queue?: Queue): Context.Tag.Service<QueueService> => {
44
- return {
45
- queues,
46
- queue,
47
- };
48
- };
49
-
50
- static layer = (queues: QueueFactory, queue?: Queue): Layer.Layer<QueueService> =>
51
- Layer.succeed(QueueService, QueueService.make(queues, queue));
52
-
53
- /**
54
- * Gets a queue by its DXN.
55
- */
56
- static getQueue = <T extends Entity.Unknown = Entity.Unknown>(
57
- dxn: DXN,
58
- ): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
59
-
60
- /**
61
- * Creates a new queue.
62
- */
63
- static createQueue = <T extends Entity.Unknown = Entity.Unknown>(options?: {
64
- subspaceTag?: QueueSubspaceTag;
65
- }): Effect.Effect<Queue<T>, never, QueueService> =>
66
- QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
67
-
68
- static append = <T extends Entity.Unknown = Entity.Unknown>(queue: Queue<T>, objects: T[]): Effect.Effect<void> =>
69
- Effect.promise(() => queue.append(objects));
70
- }
71
-
72
- /**
73
- * Gives access to a specific queue passed as a context.
74
- */
75
- export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
76
- ContextQueueService,
77
- {
78
- readonly queue: Queue;
79
- }
80
- >() {
81
- static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
82
- }
@@ -1,82 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import * as Schema from 'effect/Schema';
6
-
7
- import { Annotation, JsonSchema, Obj, Type } from '@dxos/echo';
8
- import { SystemTypeAnnotation } from '@dxos/echo/internal';
9
-
10
- import { Script } from './Script';
11
-
12
- /**
13
- * Function deployment.
14
- */
15
- export const Function = Schema.Struct({
16
- /**
17
- * Global registry ID.
18
- * NOTE: The `key` property refers to the original registry entry.
19
- */
20
- // TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
21
- // TODO(dmaretskyi): Consider making it part of ECHO meta.
22
- // TODO(dmaretskyi): Make required.
23
- key: Schema.optional(Schema.String).annotations({
24
- description: 'Unique registration key for the blueprint',
25
- }),
26
-
27
- name: Schema.NonEmptyString,
28
- version: Schema.String,
29
-
30
- description: Schema.optional(Schema.String),
31
-
32
- /**
33
- * ISO date string of the last deployment.
34
- */
35
- updated: Schema.optional(Schema.String),
36
-
37
- // Reference to a source script if it exists within ECHO.
38
- // TODO(burdon): Don't ref ScriptType directly (core).
39
- source: Schema.optional(Type.Ref(Script)),
40
-
41
- inputSchema: Schema.optional(JsonSchema.JsonSchema),
42
- outputSchema: Schema.optional(JsonSchema.JsonSchema),
43
-
44
- /**
45
- * List of required services.
46
- * Match the Context.Tag keys of the FunctionServices variants.
47
- */
48
- services: Schema.optional(Schema.Array(Schema.String)),
49
-
50
- // Local binding to a function name.
51
- binding: Schema.optional(Schema.String),
52
- }).pipe(
53
- Type.object({
54
- typename: 'dxos.org/type/Function',
55
- version: '0.1.0',
56
- }),
57
- Annotation.LabelAnnotation.set(['name']),
58
- SystemTypeAnnotation.set(true),
59
- );
60
-
61
- export interface Function extends Schema.Schema.Type<typeof Function> {}
62
-
63
- export const make = (props: Obj.MakeProps<typeof Function>) => Obj.make(Function, props);
64
-
65
- /**
66
- * Copies properties from source to target.
67
- * @param target - Target object to copy properties to.
68
- * @param source - Source object to copy properties from.
69
- */
70
- export const setFrom = (target: Function, source: Function) => {
71
- Obj.change(target, (t) => {
72
- t.key = source.key ?? target.key;
73
- t.name = source.name ?? target.name;
74
- t.version = source.version;
75
- t.description = source.description;
76
- t.updated = source.updated;
77
- // TODO(dmaretskyi): A workaround for an ECHO bug.
78
- t.inputSchema = source.inputSchema ? JSON.parse(JSON.stringify(source.inputSchema)) : undefined;
79
- t.outputSchema = source.outputSchema ? JSON.parse(JSON.stringify(source.outputSchema)) : undefined;
80
- Obj.getMeta(t).keys = JSON.parse(JSON.stringify(Obj.getMeta(source).keys));
81
- });
82
- };
@@ -1,34 +0,0 @@
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: Type.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
21
- }).pipe(
22
- Type.object({
23
- typename: 'dxos.org/type/Script',
24
- version: '0.1.0',
25
- }),
26
- Annotation.LabelAnnotation.set(['name']),
27
- );
28
-
29
- export interface Script extends Schema.Schema.Type<typeof Script> {}
30
-
31
- type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
32
-
33
- export const make = ({ source = '', ...props }: Props = {}): Script =>
34
- Obj.make(Script, { ...props, source: Ref.make(Text.make(source)) });
@@ -1,143 +0,0 @@
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 { Obj, QueryAST, Type } from '@dxos/echo';
9
- import { OptionsAnnotationId, SystemTypeAnnotation } from '@dxos/echo/internal';
10
- import { DXN } from '@dxos/keys';
11
- import { Expando } from '@dxos/schema';
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
- export const QueueSpec = Schema.Struct({
29
- kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
30
-
31
- // TODO(dmaretskyi): Change to a reference.
32
- queue: DXN.Schema,
33
- });
34
- export type QueueSpec = Schema.Schema.Type<typeof QueueSpec>;
35
-
36
- /**
37
- * Subscription.
38
- */
39
- export const SubscriptionSpec = Schema.Struct({
40
- kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
41
- query: Schema.Struct({
42
- raw: Schema.optional(Schema.String.annotations({ title: 'Query' })),
43
- ast: QueryAST.Query,
44
- }),
45
- options: Schema.optional(
46
- Schema.Struct({
47
- // Watch changes to object (not just creation).
48
- deep: Schema.optional(Schema.Boolean.annotations({ title: 'Nested' })),
49
- // Debounce changes (delay in ms).
50
- delay: Schema.optional(Schema.Number.annotations({ title: 'Delay' })),
51
- }).annotations({ title: 'Options' }),
52
- ),
53
- });
54
- export type SubscriptionSpec = Schema.Schema.Type<typeof SubscriptionSpec>;
55
-
56
- /**
57
- * Cron timer.
58
- */
59
- export const TimerSpec = Schema.Struct({
60
- kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
61
- cron: Schema.String.annotations({
62
- title: 'Cron',
63
- [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
64
- }),
65
- });
66
- export type TimerSpec = Schema.Schema.Type<typeof TimerSpec>;
67
-
68
- /**
69
- * Webhook.
70
- */
71
- export const WebhookSpec = Schema.Struct({
72
- kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
73
- method: Schema.optional(
74
- Schema.String.annotations({
75
- title: 'Method',
76
- [OptionsAnnotationId]: ['GET', 'POST'],
77
- }),
78
- ),
79
- port: Schema.optional(
80
- Schema.Number.annotations({
81
- title: 'Port',
82
- }),
83
- ),
84
- });
85
- export type WebhookSpec = Schema.Schema.Type<typeof WebhookSpec>;
86
-
87
- /**
88
- * Trigger schema.
89
- */
90
- export const Spec = Schema.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
91
- title: 'Trigger',
92
- });
93
- export type Spec = Schema.Schema.Type<typeof Spec>;
94
-
95
- /**
96
- * Function trigger.
97
- * Function is invoked with the `payload` passed as input data.
98
- * The event that triggers the function is available in the function context.
99
- */
100
- const TriggerSchema = Schema.Struct({
101
- /**
102
- * Function or workflow to invoke.
103
- */
104
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
105
- function: Schema.optional(Type.Ref(Expando.Expando).annotations({ title: 'Function' })),
106
-
107
- /**
108
- * Only used for workflowSchema.
109
- * Specifies the input node in the circuit.
110
- * @deprecated Remove and enforce a single input node in all compute graphSchema.
111
- */
112
- inputNodeId: Schema.optional(Schema.String.annotations({ title: 'Input Node ID' })),
113
-
114
- // TODO(burdon): NO BOOLEAN PROPERTIES (enabld/disabled/paused, etc.)
115
- // Need lint rule; or agent rule to require PR review for "boolean" key word.
116
- enabled: Schema.optional(Schema.Boolean.annotations({ title: 'Enabled' })),
117
-
118
- spec: Schema.optional(Spec),
119
-
120
- /**
121
- * Passed as the input data to the function.
122
- * Must match the function's input schema.
123
- *
124
- * @example
125
- * {
126
- * item: '{{$.trigger.event}}',
127
- * instructions: 'Summarize and perform entity-extraction'
128
- * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
129
- * }
130
- */
131
- input: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
132
- }).pipe(
133
- Type.object({
134
- typename: 'dxos.org/type/Trigger',
135
- version: '0.1.0',
136
- }),
137
- SystemTypeAnnotation.set(true),
138
- );
139
-
140
- export interface Trigger extends Schema.Schema.Type<typeof TriggerSchema> {}
141
- export const Trigger: Type.Obj<Trigger> = TriggerSchema as any;
142
-
143
- export const make = (props: Obj.MakeProps<typeof Trigger>) => Obj.make(Trigger, props);