@dxos/functions 0.8.3 → 0.8.4-main.1068cf700f

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 (172) hide show
  1. package/dist/lib/neutral/index.mjs +1229 -0
  2. package/dist/lib/neutral/index.mjs.map +7 -0
  3. package/dist/lib/neutral/meta.json +1 -0
  4. package/dist/types/src/errors.d.ts +121 -0
  5. package/dist/types/src/errors.d.ts.map +1 -0
  6. package/dist/types/src/example/fib.d.ts +7 -0
  7. package/dist/types/src/example/fib.d.ts.map +1 -0
  8. package/dist/types/src/example/forex-effect.d.ts +3 -0
  9. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  10. package/dist/types/src/example/index.d.ts +12 -0
  11. package/dist/types/src/example/index.d.ts.map +1 -0
  12. package/dist/types/src/example/reply.d.ts +3 -0
  13. package/dist/types/src/example/reply.d.ts.map +1 -0
  14. package/dist/types/src/example/sleep.d.ts +5 -0
  15. package/dist/types/src/example/sleep.d.ts.map +1 -0
  16. package/dist/types/src/index.d.ts +5 -6
  17. package/dist/types/src/index.d.ts.map +1 -1
  18. package/dist/types/src/operation-compatibility.test.d.ts +2 -0
  19. package/dist/types/src/operation-compatibility.test.d.ts.map +1 -0
  20. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  21. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  22. package/dist/types/src/protocol/index.d.ts +2 -0
  23. package/dist/types/src/protocol/index.d.ts.map +1 -0
  24. package/dist/types/src/protocol/protocol.d.ts +7 -0
  25. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  26. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  27. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  28. package/dist/types/src/sdk.d.ts +114 -0
  29. package/dist/types/src/sdk.d.ts.map +1 -0
  30. package/dist/types/src/services/credentials.d.ts +24 -4
  31. package/dist/types/src/services/credentials.d.ts.map +1 -1
  32. package/dist/types/src/services/event-logger.d.ts +74 -30
  33. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  34. package/dist/types/src/services/function-invocation-service.d.ts +16 -0
  35. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  36. package/dist/types/src/services/index.d.ts +5 -6
  37. package/dist/types/src/services/index.d.ts.map +1 -1
  38. package/dist/types/src/services/queues.d.ts +35 -6
  39. package/dist/types/src/services/queues.d.ts.map +1 -1
  40. package/dist/types/src/services/tracing.d.ts +78 -5
  41. package/dist/types/src/services/tracing.d.ts.map +1 -1
  42. package/dist/types/src/types/Function.d.ts +52 -0
  43. package/dist/types/src/types/Function.d.ts.map +1 -0
  44. package/dist/types/src/types/Script.d.ts +21 -0
  45. package/dist/types/src/types/Script.d.ts.map +1 -0
  46. package/dist/types/src/types/Trigger.d.ts +121 -0
  47. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  48. package/dist/types/src/types/TriggerEvent.d.ts +74 -0
  49. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  50. package/dist/types/src/types/index.d.ts +6 -0
  51. package/dist/types/src/types/index.d.ts.map +1 -0
  52. package/dist/types/src/types/url.d.ts +13 -0
  53. package/dist/types/src/types/url.d.ts.map +1 -0
  54. package/dist/types/tsconfig.tsbuildinfo +1 -1
  55. package/package.json +27 -65
  56. package/src/errors.ts +21 -0
  57. package/src/example/fib.ts +32 -0
  58. package/src/example/forex-effect.ts +40 -0
  59. package/src/example/index.ts +13 -0
  60. package/src/example/reply.ts +21 -0
  61. package/src/example/sleep.ts +24 -0
  62. package/src/index.ts +5 -8
  63. package/src/operation-compatibility.test.ts +185 -0
  64. package/src/protocol/functions-ai-http-client.ts +67 -0
  65. package/src/{executor → protocol}/index.ts +1 -1
  66. package/src/protocol/protocol.test.ts +59 -0
  67. package/src/protocol/protocol.ts +262 -0
  68. package/src/sdk.ts +289 -0
  69. package/src/services/credentials.ts +109 -5
  70. package/src/services/event-logger.ts +77 -37
  71. package/src/services/function-invocation-service.ts +37 -0
  72. package/src/services/index.ts +5 -6
  73. package/src/services/queues.ts +56 -11
  74. package/src/services/tracing.ts +151 -11
  75. package/src/types/Function.ts +82 -0
  76. package/src/types/Script.ts +34 -0
  77. package/src/types/Trigger.ts +143 -0
  78. package/src/types/TriggerEvent.ts +62 -0
  79. package/src/types/index.ts +9 -0
  80. package/src/types/url.ts +32 -0
  81. package/dist/lib/browser/bundler/index.mjs +0 -236
  82. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  83. package/dist/lib/browser/chunk-WEFZUEL2.mjs +0 -300
  84. package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
  85. package/dist/lib/browser/edge/index.mjs +0 -69
  86. package/dist/lib/browser/edge/index.mjs.map +0 -7
  87. package/dist/lib/browser/index.mjs +0 -486
  88. package/dist/lib/browser/index.mjs.map +0 -7
  89. package/dist/lib/browser/meta.json +0 -1
  90. package/dist/lib/browser/testing/index.mjs +0 -28
  91. package/dist/lib/browser/testing/index.mjs.map +0 -7
  92. package/dist/lib/node/bundler/index.cjs +0 -260
  93. package/dist/lib/node/bundler/index.cjs.map +0 -7
  94. package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
  95. package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
  96. package/dist/lib/node/edge/index.cjs +0 -94
  97. package/dist/lib/node/edge/index.cjs.map +0 -7
  98. package/dist/lib/node/index.cjs +0 -522
  99. package/dist/lib/node/index.cjs.map +0 -7
  100. package/dist/lib/node/meta.json +0 -1
  101. package/dist/lib/node/testing/index.cjs +0 -43
  102. package/dist/lib/node/testing/index.cjs.map +0 -7
  103. package/dist/lib/node-esm/bundler/index.mjs +0 -238
  104. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  105. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
  106. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
  107. package/dist/lib/node-esm/edge/index.mjs +0 -71
  108. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  109. package/dist/lib/node-esm/index.mjs +0 -487
  110. package/dist/lib/node-esm/index.mjs.map +0 -7
  111. package/dist/lib/node-esm/meta.json +0 -1
  112. package/dist/lib/node-esm/testing/index.mjs +0 -29
  113. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  114. package/dist/types/src/bundler/bundler.d.ts +0 -51
  115. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  116. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  117. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  118. package/dist/types/src/bundler/index.d.ts +0 -2
  119. package/dist/types/src/bundler/index.d.ts.map +0 -1
  120. package/dist/types/src/edge/functions.d.ts +0 -16
  121. package/dist/types/src/edge/functions.d.ts.map +0 -1
  122. package/dist/types/src/edge/index.d.ts +0 -2
  123. package/dist/types/src/edge/index.d.ts.map +0 -1
  124. package/dist/types/src/executor/executor.d.ts +0 -8
  125. package/dist/types/src/executor/executor.d.ts.map +0 -1
  126. package/dist/types/src/executor/index.d.ts +0 -2
  127. package/dist/types/src/executor/index.d.ts.map +0 -1
  128. package/dist/types/src/handler.d.ts +0 -64
  129. package/dist/types/src/handler.d.ts.map +0 -1
  130. package/dist/types/src/schema.d.ts +0 -38
  131. package/dist/types/src/schema.d.ts.map +0 -1
  132. package/dist/types/src/services/ai.d.ts +0 -12
  133. package/dist/types/src/services/ai.d.ts.map +0 -1
  134. package/dist/types/src/services/database.d.ts +0 -11
  135. package/dist/types/src/services/database.d.ts.map +0 -1
  136. package/dist/types/src/services/function-call-service.d.ts +0 -16
  137. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  138. package/dist/types/src/services/service-container.d.ts +0 -44
  139. package/dist/types/src/services/service-container.d.ts.map +0 -1
  140. package/dist/types/src/testing/index.d.ts +0 -2
  141. package/dist/types/src/testing/index.d.ts.map +0 -1
  142. package/dist/types/src/testing/logger.d.ts +0 -5
  143. package/dist/types/src/testing/logger.d.ts.map +0 -1
  144. package/dist/types/src/testing/services.d.ts +0 -13
  145. package/dist/types/src/testing/services.d.ts.map +0 -1
  146. package/dist/types/src/trace.d.ts +0 -124
  147. package/dist/types/src/trace.d.ts.map +0 -1
  148. package/dist/types/src/translations.d.ts +0 -12
  149. package/dist/types/src/translations.d.ts.map +0 -1
  150. package/dist/types/src/types.d.ts +0 -411
  151. package/dist/types/src/types.d.ts.map +0 -1
  152. package/dist/types/src/url.d.ts +0 -17
  153. package/dist/types/src/url.d.ts.map +0 -1
  154. package/src/bundler/bundler.test.ts +0 -59
  155. package/src/bundler/bundler.ts +0 -270
  156. package/src/bundler/index.ts +0 -5
  157. package/src/edge/functions.ts +0 -64
  158. package/src/edge/index.ts +0 -9
  159. package/src/executor/executor.ts +0 -47
  160. package/src/handler.ts +0 -113
  161. package/src/schema.ts +0 -57
  162. package/src/services/ai.ts +0 -32
  163. package/src/services/database.ts +0 -28
  164. package/src/services/function-call-service.ts +0 -64
  165. package/src/services/service-container.ts +0 -109
  166. package/src/testing/index.ts +0 -5
  167. package/src/testing/logger.ts +0 -16
  168. package/src/testing/services.ts +0 -32
  169. package/src/trace.ts +0 -180
  170. package/src/translations.ts +0 -20
  171. package/src/types.ts +0 -211
  172. package/src/url.ts +0 -52
@@ -1,109 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Layer, type Context } from 'effect';
6
-
7
- import { AiService } from './ai';
8
- import { ConfiguredCredentialsService, CredentialsService } from './credentials';
9
- import { DatabaseService } from './database';
10
- import { EventLogger } from './event-logger';
11
- import { FunctionCallService } from './function-call-service';
12
- import { QueueService } from './queues';
13
- import { TracingService } from './tracing';
14
-
15
- /**
16
- * List of all service tags and their names.
17
- */
18
- export interface ServiceTagRecord {
19
- ai: AiService;
20
- credentials: CredentialsService;
21
- database: DatabaseService;
22
- eventLogger: EventLogger;
23
- functionCallService: FunctionCallService;
24
- tracing: TracingService;
25
- queues: QueueService;
26
- }
27
-
28
- /**
29
- * List of all services and their runtime types.
30
- */
31
- export type ServiceRecord = {
32
- [K in keyof ServiceTagRecord]: Context.Tag.Service<ServiceTagRecord[K]>;
33
- };
34
-
35
- /**
36
- * Union of all services.
37
- */
38
- export type Services = ServiceTagRecord[keyof ServiceTagRecord];
39
-
40
- const SERVICE_MAPPING: Record<string, keyof ServiceRecord> = {
41
- [AiService.key]: 'ai',
42
- [CredentialsService.key]: 'credentials',
43
- [DatabaseService.key]: 'database',
44
- [EventLogger.key]: 'eventLogger',
45
- [FunctionCallService.key]: 'functionCallService',
46
- [QueueService.key]: 'queues',
47
- [TracingService.key]: 'tracing',
48
- };
49
-
50
- export const SERVICE_TAGS: Context.Tag<any, any>[] = [
51
- AiService,
52
- CredentialsService,
53
- DatabaseService,
54
- EventLogger,
55
- FunctionCallService,
56
- TracingService,
57
- QueueService,
58
- ];
59
-
60
- const DEFAULT_SERVICES: Partial<ServiceRecord> = {
61
- tracing: TracingService.noop,
62
- };
63
-
64
- export class ServiceContainer {
65
- private _services: Partial<ServiceRecord> = { ...DEFAULT_SERVICES };
66
-
67
- /**
68
- * Set services.
69
- * @param services - Services to set.
70
- * @returns The container instance.
71
- */
72
- setServices(services: Partial<ServiceRecord>): this {
73
- this._services = { ...this._services, ...services };
74
- return this;
75
- }
76
-
77
- getService<T extends Context.Tag<any, any>>(tag: T): Context.Tag.Service<T> {
78
- const serviceKey = SERVICE_MAPPING[tag.key];
79
- const service = serviceKey != null ? this._services[serviceKey] : undefined;
80
- if (!service) {
81
- throw new Error(`Service not available: ${tag.key}`);
82
- }
83
- return service as Context.Tag.Service<T>;
84
- }
85
-
86
- clone(): ServiceContainer {
87
- return new ServiceContainer().setServices({ ...this._services });
88
- }
89
-
90
- // TODO(dmaretskyi): `getService` is designed to error at runtime if the service is not available, but layer forces us to provide all services and makes stubs for the ones that are not available.
91
- createLayer(): Layer.Layer<Services> {
92
- const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;
93
- const credentials = Layer.succeed(CredentialsService, new ConfiguredCredentialsService());
94
- const database =
95
- this._services.database != null
96
- ? Layer.succeed(DatabaseService, this._services.database)
97
- : DatabaseService.notAvailable;
98
- const queues =
99
- this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
100
- const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
101
- const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
102
- const functionCallService = Layer.succeed(
103
- FunctionCallService,
104
- this._services.functionCallService ?? FunctionCallService.mock(),
105
- );
106
-
107
- return Layer.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
108
- }
109
- }
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './services';
@@ -1,16 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Effect, type Context } from 'effect';
6
-
7
- import { LogLevel } from '@dxos/log';
8
-
9
- import { type EventLogger, createEventLogger } from '../services';
10
-
11
- export const noopLogger: Context.Tag.Service<EventLogger> = {
12
- log: () => Effect.succeed(undefined),
13
- nodeId: undefined,
14
- };
15
-
16
- export const consoleLogger: Context.Tag.Service<EventLogger> = createEventLogger(LogLevel.INFO);
@@ -1,32 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type Context } from 'effect';
6
-
7
- import type { AiServiceClient } from '@dxos/ai';
8
- import type { QueueFactory } from '@dxos/echo-db';
9
-
10
- import { consoleLogger, noopLogger } from './logger';
11
- import { AiService, QueueService, ServiceContainer } from '../services';
12
- import type { EventLogger } from '../services/event-logger';
13
-
14
- export type TestServiceOptions = {
15
- ai?: AiServiceClient;
16
- queueFactory?: QueueFactory;
17
- enableLogging?: boolean;
18
- logger?: Context.Tag.Service<EventLogger>;
19
- };
20
-
21
- export const createTestServices = ({
22
- ai,
23
- queueFactory,
24
- enableLogging = false,
25
- logger = enableLogging ? consoleLogger : noopLogger,
26
- }: TestServiceOptions = {}): ServiceContainer => {
27
- return new ServiceContainer().setServices({
28
- ai: ai != null ? AiService.make(ai) : undefined,
29
- eventLogger: logger,
30
- queues: queueFactory != null ? QueueService.make(queueFactory, undefined) : undefined,
31
- });
32
- };
package/src/trace.ts DELETED
@@ -1,180 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
-
7
- import { Type, type Ref } from '@dxos/echo';
8
- import { Queue } from '@dxos/echo-db';
9
- import { ObjectId } from '@dxos/echo-schema';
10
- import { log } from '@dxos/log';
11
-
12
- import { FunctionTrigger, type FunctionTriggerType } from './types';
13
-
14
- export enum InvocationOutcome {
15
- SUCCESS = 'success',
16
- FAILURE = 'failure',
17
- PENDING = 'pending',
18
- }
19
-
20
- // TODO(burdon): Convert to extensible discriminated union of EDGE events.
21
- export enum InvocationTraceEventType {
22
- START = 'start',
23
- END = 'end',
24
- }
25
-
26
- export const TraceEventException = Schema.Struct({
27
- timestampMs: Schema.Number,
28
- message: Schema.String,
29
- name: Schema.String,
30
- stack: Schema.optional(Schema.String),
31
- });
32
- export type TraceEventException = Schema.Schema.Type<typeof TraceEventException>;
33
-
34
- export const InvocationTraceStartEvent = Schema.Struct({
35
- /**
36
- * Queue message id.
37
- */
38
- id: ObjectId,
39
- type: Schema.Literal(InvocationTraceEventType.START),
40
- /**
41
- * Invocation id, the same for invocation start and end events.
42
- */
43
- invocationId: ObjectId,
44
- /**
45
- * Event generation time.
46
- */
47
- timestampMs: Schema.Number,
48
- /**
49
- * Data passed to function / workflow as an argument.
50
- */
51
- // TODO(burdon): Input schema?
52
- input: Schema.Object,
53
- /**
54
- * Queue for function/workflow invocation events.
55
- */
56
- invocationTraceQueue: Type.Ref(Queue),
57
- /**
58
- * DXN of the invoked function/workflow.
59
- */
60
- invocationTarget: Type.Ref(Type.Expando),
61
- /**
62
- * Present for automatic invocations.
63
- */
64
- trigger: Schema.optional(Type.Ref(FunctionTrigger)),
65
- }).pipe(Type.Obj({ typename: 'dxos.org/type/InvocationTraceStart', version: '0.1.0' }));
66
-
67
- export type InvocationTraceStartEvent = Schema.Schema.Type<typeof InvocationTraceStartEvent>;
68
-
69
- export const InvocationTraceEndEvent = Schema.Struct({
70
- /**
71
- * Trace event id.
72
- */
73
- id: ObjectId,
74
- type: Schema.Literal(InvocationTraceEventType.END),
75
- /**
76
- * Invocation id, will be the same for invocation start and end.
77
- */
78
- invocationId: ObjectId,
79
- /**
80
- * Event generation time.
81
- */
82
- // TODO(burdon): Remove ms suffix.
83
- timestampMs: Schema.Number,
84
- outcome: Schema.Enums(InvocationOutcome),
85
- exception: Schema.optional(TraceEventException),
86
- }).pipe(Type.Obj({ typename: 'dxos.org/type/InvocationTraceEnd', version: '0.1.0' }));
87
-
88
- export type InvocationTraceEndEvent = Schema.Schema.Type<typeof InvocationTraceEndEvent>;
89
-
90
- export type InvocationTraceEvent = InvocationTraceStartEvent | InvocationTraceEndEvent;
91
-
92
- export const TraceEventLog = Schema.Struct({
93
- timestampMs: Schema.Number,
94
- level: Schema.String,
95
- message: Schema.String,
96
- context: Schema.optional(Schema.Object),
97
- });
98
-
99
- export const TraceEvent = Schema.Struct({
100
- id: ObjectId,
101
- // TODO(burdon): Need enum/numeric result (not string).
102
- outcome: Schema.String,
103
- truncated: Schema.Boolean,
104
- /**
105
- * Time when the event was persisted.
106
- */
107
- ingestionTimestampMs: Schema.Number,
108
- logs: Schema.Array(TraceEventLog),
109
- exceptions: Schema.Array(TraceEventException),
110
- }).pipe(Type.Obj({ typename: 'dxos.org/type/TraceEvent', version: '0.1.0' }));
111
-
112
- export type TraceEvent = Schema.Schema.Type<typeof TraceEvent>;
113
-
114
- /**
115
- * InvocationTrace event format.
116
- * This is the combined format of InvocationTraceStartEvent and InvocationTraceEndEvents for UI consumption.
117
- */
118
- export type InvocationSpan = {
119
- id: string;
120
- timestampMs: number;
121
- outcome: InvocationOutcome;
122
- input: object;
123
- durationMs: number;
124
- invocationTraceQueue: Ref.Ref<Queue>;
125
- invocationTarget: Ref.Ref<Type.Expando>;
126
- trigger?: Ref.Ref<FunctionTriggerType>;
127
- exception?: TraceEventException;
128
- };
129
-
130
- export const createInvocationSpans = (items?: InvocationTraceEvent[]): InvocationSpan[] => {
131
- if (!items) {
132
- return [];
133
- }
134
-
135
- const eventsByInvocationId = new Map<string, { start?: InvocationTraceStartEvent; end?: InvocationTraceEndEvent }>();
136
- for (const event of items) {
137
- if (!('invocationId' in event)) {
138
- // Skip legacy format entries.
139
- continue;
140
- }
141
-
142
- const invocationId = event.invocationId;
143
- const entry = eventsByInvocationId.get(invocationId) || { start: undefined, end: undefined };
144
- if (event.type === InvocationTraceEventType.START) {
145
- entry.start = event as InvocationTraceStartEvent;
146
- } else if (event.type === InvocationTraceEventType.END) {
147
- entry.end = event as InvocationTraceEndEvent;
148
- }
149
-
150
- eventsByInvocationId.set(invocationId, entry);
151
- }
152
-
153
- const now = Date.now();
154
- const result: InvocationSpan[] = [];
155
-
156
- // Create spans for each invocation
157
- for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
158
- if (!start) {
159
- // No start event, can't create a meaningful span
160
- log.warn('found end event without matching start', { invocationId });
161
- continue;
162
- }
163
-
164
- const isInProgress = end === undefined;
165
-
166
- result.push({
167
- id: invocationId,
168
- timestampMs: start.timestampMs,
169
- durationMs: isInProgress ? now - start.timestampMs : end!.timestampMs - start.timestampMs,
170
- outcome: end?.outcome ?? InvocationOutcome.PENDING,
171
- exception: end?.exception,
172
- input: start.input,
173
- invocationTraceQueue: start.invocationTraceQueue,
174
- invocationTarget: start.invocationTarget,
175
- trigger: start.trigger,
176
- });
177
- }
178
-
179
- return result;
180
- };
@@ -1,20 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { Type } from '@dxos/echo';
6
-
7
- import { ScriptType } from './schema';
8
-
9
- export default [
10
- {
11
- 'en-US': {
12
- [Type.getTypename(ScriptType)]: {
13
- 'typename label': 'Script',
14
- 'typename label_zero': 'Scripts',
15
- 'typename label_one': 'Script',
16
- 'typename label_other': 'Scripts',
17
- },
18
- },
19
- },
20
- ];
package/src/types.ts DELETED
@@ -1,211 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { Schema, SchemaAST } from 'effect';
6
-
7
- import { Expando, OptionsAnnotationId, TypedObject, Ref, RawObject } from '@dxos/echo-schema';
8
- import { DXN } from '@dxos/keys';
9
-
10
- import { FunctionType } from './schema';
11
-
12
- /**
13
- * Type discriminator for TriggerType.
14
- * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
15
- * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
16
- */
17
- export enum TriggerKind {
18
- Timer = 'timer',
19
- Webhook = 'webhook',
20
- Subscription = 'subscription',
21
- Email = 'email',
22
- Queue = 'queue',
23
- }
24
-
25
- const kindLiteralAnnotations = { title: 'Kind' };
26
-
27
- /**
28
- * Cron timer.
29
- */
30
- const TimerTriggerSchema = Schema.Struct({
31
- kind: Schema.Literal(TriggerKind.Timer).annotations(kindLiteralAnnotations),
32
- cron: Schema.String.annotations({
33
- title: 'Cron',
34
- [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
35
- }),
36
- }).pipe(Schema.mutable);
37
- export type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;
38
-
39
- const EmailTriggerSchema = Schema.Struct({
40
- kind: Schema.Literal(TriggerKind.Email).annotations(kindLiteralAnnotations),
41
- }).pipe(Schema.mutable);
42
- export type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;
43
-
44
- const QueueTriggerSchema = Schema.Struct({
45
- kind: Schema.Literal(TriggerKind.Queue).annotations(kindLiteralAnnotations),
46
- queue: DXN.Schema,
47
- }).pipe(Schema.mutable);
48
- export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
49
-
50
- /**
51
- * Webhook.
52
- */
53
- const WebhookTriggerSchema = Schema.Struct({
54
- kind: Schema.Literal(TriggerKind.Webhook).annotations(kindLiteralAnnotations),
55
- method: Schema.optional(
56
- Schema.String.annotations({
57
- title: 'Method',
58
- [OptionsAnnotationId]: ['GET', 'POST'],
59
- }),
60
- ),
61
- port: Schema.optional(
62
- Schema.Number.annotations({
63
- title: 'Port',
64
- }),
65
- ),
66
- }).pipe(Schema.mutable);
67
- export type WebhookTrigger = Schema.Schema.Type<typeof WebhookTriggerSchema>;
68
-
69
- // TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).
70
- const QuerySchema = Schema.Struct({
71
- type: Schema.optional(Schema.String.annotations({ title: 'Type' })),
72
- props: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
73
- }).annotations({ title: 'Query' });
74
-
75
- /**
76
- * Subscription.
77
- */
78
- const SubscriptionTriggerSchema = Schema.Struct({
79
- kind: Schema.Literal(TriggerKind.Subscription).annotations(kindLiteralAnnotations),
80
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
81
- filter: QuerySchema,
82
- options: Schema.optional(
83
- Schema.Struct({
84
- // Watch changes to object (not just creation).
85
- deep: Schema.optional(Schema.Boolean.annotations({ title: 'Nested' })),
86
- // Debounce changes (delay in ms).
87
- delay: Schema.optional(Schema.Number.annotations({ title: 'Delay' })),
88
- }).annotations({ title: 'Options' }),
89
- ),
90
- }).pipe(Schema.mutable);
91
- export type SubscriptionTrigger = Schema.Schema.Type<typeof SubscriptionTriggerSchema>;
92
-
93
- /**
94
- * Trigger schema (discriminated union).
95
- */
96
- export const TriggerSchema = Schema.Union(
97
- TimerTriggerSchema,
98
- WebhookTriggerSchema,
99
- SubscriptionTriggerSchema,
100
- EmailTriggerSchema,
101
- QueueTriggerSchema,
102
- ).annotations({
103
- title: 'Trigger',
104
- });
105
- export type TriggerType = Schema.Schema.Type<typeof TriggerSchema>;
106
-
107
- export type EventType =
108
- | EmailTriggerOutput
109
- | WebhookTriggerOutput
110
- | QueueTriggerOutput
111
- | SubscriptionTriggerOutput
112
- | TimerTriggerOutput;
113
-
114
- // TODO(burdon): Reuse trigger schema from @dxos/functions (TriggerType).
115
- export const EmailTriggerOutput = Schema.mutable(
116
- Schema.Struct({
117
- from: Schema.String,
118
- to: Schema.String,
119
- subject: Schema.String,
120
- created: Schema.String,
121
- body: Schema.String,
122
- }),
123
- );
124
- export type EmailTriggerOutput = Schema.Schema.Type<typeof EmailTriggerOutput>;
125
-
126
- export const WebhookTriggerOutput = Schema.mutable(
127
- Schema.Struct({
128
- url: Schema.String,
129
- method: Schema.Literal('GET', 'POST'),
130
- headers: Schema.Record({ key: Schema.String, value: Schema.String }),
131
- bodyText: Schema.String,
132
- }),
133
- );
134
- export type WebhookTriggerOutput = Schema.Schema.Type<typeof WebhookTriggerOutput>;
135
-
136
- export const QueueTriggerOutput = Schema.mutable(
137
- Schema.Struct({
138
- queue: DXN.Schema,
139
- item: Schema.Any,
140
- cursor: Schema.String,
141
- }),
142
- );
143
- export type QueueTriggerOutput = Schema.Schema.Type<typeof QueueTriggerOutput>;
144
-
145
- export const SubscriptionTriggerOutput = Schema.mutable(
146
- Schema.Struct({ type: Schema.String, changedObjectId: Schema.String }),
147
- );
148
- export type SubscriptionTriggerOutput = Schema.Schema.Type<typeof SubscriptionTriggerOutput>;
149
-
150
- export const TimerTriggerOutput = Schema.mutable(Schema.Struct({ tick: Schema.Number }));
151
- export type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;
152
-
153
- /**
154
- * Function trigger.
155
- * Function is invoked with the `payload` passed as input data.
156
- * The event that triggers the function is available in the function context.
157
- */
158
- export const FunctionTriggerSchema = Schema.Struct({
159
- /**
160
- * Function or workflow to invoke.
161
- */
162
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
163
- function: Schema.optional(Ref(Expando).annotations({ title: 'Function' })),
164
-
165
- /**
166
- * Only used for workflowSchema.
167
- * Specifies the input node in the circuit.
168
- * @deprecated Remove and enforce a single input node in all compute graphSchema.
169
- */
170
- inputNodeId: Schema.optional(Schema.String.annotations({ title: 'Input Node ID' })),
171
-
172
- enabled: Schema.optional(Schema.Boolean.annotations({ title: 'Enabled' })),
173
-
174
- spec: Schema.optional(TriggerSchema),
175
-
176
- /**
177
- * Passed as the input data to the function.
178
- * Must match the function's input schema.
179
- *
180
- * @example
181
- * {
182
- * item: '{{$.trigger.event}}',
183
- * instructions: 'Summarize and perform entity-extraction'
184
- * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
185
- * }
186
- */
187
- input: Schema.optional(Schema.mutable(Schema.Record({ key: Schema.String, value: Schema.Any }))),
188
- });
189
-
190
- export type FunctionTriggerType = Schema.Schema.Type<typeof FunctionTriggerSchema>;
191
-
192
- /**
193
- * Function trigger.
194
- */
195
- export class FunctionTrigger extends TypedObject({
196
- typename: 'dxos.org/type/FunctionTrigger',
197
- version: '0.2.0',
198
- })(FunctionTriggerSchema.fields) {}
199
-
200
- // TODO(wittjosiah): Remove?
201
-
202
- /**
203
- * Function manifest file.
204
- */
205
- export const FunctionManifestSchema = Schema.Struct({
206
- functions: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionType)))),
207
- triggers: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionTrigger)))),
208
- });
209
- export type FunctionManifest = Schema.Schema.Type<typeof FunctionManifestSchema>;
210
-
211
- export const FUNCTION_TYPES = [FunctionType, FunctionTrigger];
package/src/url.ts DELETED
@@ -1,52 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type ObjectMeta } from '@dxos/echo-schema';
6
- import { type SpaceId } from '@dxos/keys';
7
-
8
- // TODO: use URL scheme for source?
9
- const FUNCTIONS_META_KEY = 'dxos.org/service/function';
10
-
11
- export const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
12
-
13
- const isSecure = (protocol: string) => {
14
- return protocol === 'https:' || protocol === 'wss:';
15
- };
16
-
17
- export const getUserFunctionUrlInMetadata = (meta: ObjectMeta) => {
18
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
19
- };
20
-
21
- export const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: string) => {
22
- const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
23
- if (key) {
24
- if (key.id !== functionUrl) {
25
- throw new Error('Metadata mismatch');
26
- }
27
- } else {
28
- meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionUrl });
29
- }
30
- };
31
-
32
- /**
33
- * NOTE: functionId is backend ID, not ECHO object id.
34
- */
35
- export const makeFunctionUrl = (fn: { functionId: string }) => `/${fn.functionId}`;
36
-
37
- export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
38
- const baseUrl = new URL('functions/', edgeUrl);
39
-
40
- // Leading slashes cause the URL to be treated as an absolute path.
41
- const relativeUrl = functionUrl.replace(/^\//, '');
42
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
43
- options.spaceId && url.searchParams.set('spaceId', options.spaceId);
44
- options.subjectId && url.searchParams.set('subjectId', options.subjectId);
45
- url.protocol = isSecure(url.protocol) ? 'https' : 'http';
46
- return url.toString();
47
- };
48
-
49
- export type InvocationOptions = {
50
- spaceId?: SpaceId;
51
- subjectId?: string;
52
- };