@dxos/functions 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29

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 (181) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/index.mjs +593 -0
  3. package/dist/lib/neutral/index.mjs.map +7 -0
  4. package/dist/lib/neutral/meta.json +1 -0
  5. package/dist/types/src/index.d.ts +3 -9
  6. package/dist/types/src/index.d.ts.map +1 -1
  7. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  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/index.d.ts +2 -0
  12. package/dist/types/src/protocol/index.d.ts.map +1 -0
  13. package/dist/types/src/protocol/protocol.d.ts +19 -0
  14. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  15. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  16. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  17. package/dist/types/src/sdk.d.ts +10 -0
  18. package/dist/types/src/sdk.d.ts.map +1 -0
  19. package/dist/types/src/services/credentials.d.ts +22 -39
  20. package/dist/types/src/services/credentials.d.ts.map +1 -1
  21. package/dist/types/src/services/function-invocation-service.d.ts +15 -0
  22. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  23. package/dist/types/src/services/index.d.ts +3 -7
  24. package/dist/types/src/services/index.d.ts.map +1 -1
  25. package/dist/types/src/services/queues.d.ts +1 -44
  26. package/dist/types/src/services/queues.d.ts.map +1 -1
  27. package/dist/types/src/services/tracing.d.ts +2 -54
  28. package/dist/types/src/services/tracing.d.ts.map +1 -1
  29. package/dist/types/src/types/index.d.ts +2 -0
  30. package/dist/types/src/types/index.d.ts.map +1 -0
  31. package/dist/types/src/types/url.d.ts +13 -0
  32. package/dist/types/src/types/url.d.ts.map +1 -0
  33. package/dist/types/tsconfig.tsbuildinfo +1 -1
  34. package/package.json +25 -72
  35. package/src/index.ts +3 -9
  36. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  37. package/src/protocol/functions-ai-http-client.ts +141 -0
  38. package/src/{executor → protocol}/index.ts +1 -1
  39. package/src/protocol/protocol.test.ts +58 -0
  40. package/src/protocol/protocol.ts +428 -0
  41. package/src/sdk.ts +31 -0
  42. package/src/services/credentials.ts +90 -111
  43. package/src/services/function-invocation-service.ts +33 -0
  44. package/src/services/index.ts +3 -7
  45. package/src/services/queues.ts +1 -78
  46. package/src/services/tracing.ts +1 -133
  47. package/src/types/index.ts +5 -0
  48. package/src/types/url.ts +32 -0
  49. package/dist/lib/browser/bundler/index.mjs +0 -265
  50. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  51. package/dist/lib/browser/chunk-ANP3DFCO.mjs +0 -623
  52. package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +0 -7
  53. package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
  54. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
  55. package/dist/lib/browser/edge/index.mjs +0 -83
  56. package/dist/lib/browser/edge/index.mjs.map +0 -7
  57. package/dist/lib/browser/index.mjs +0 -1265
  58. package/dist/lib/browser/index.mjs.map +0 -7
  59. package/dist/lib/browser/meta.json +0 -1
  60. package/dist/lib/browser/testing/index.mjs +0 -129
  61. package/dist/lib/browser/testing/index.mjs.map +0 -7
  62. package/dist/lib/node-esm/bundler/index.mjs +0 -266
  63. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  64. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  65. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  66. package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs +0 -625
  67. package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +0 -7
  68. package/dist/lib/node-esm/edge/index.mjs +0 -84
  69. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  70. package/dist/lib/node-esm/index.mjs +0 -1266
  71. package/dist/lib/node-esm/index.mjs.map +0 -7
  72. package/dist/lib/node-esm/meta.json +0 -1
  73. package/dist/lib/node-esm/testing/index.mjs +0 -130
  74. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  75. package/dist/types/src/bundler/bundler.d.ts +0 -49
  76. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  77. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  78. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  79. package/dist/types/src/bundler/index.d.ts +0 -2
  80. package/dist/types/src/bundler/index.d.ts.map +0 -1
  81. package/dist/types/src/edge/functions.d.ts +0 -17
  82. package/dist/types/src/edge/functions.d.ts.map +0 -1
  83. package/dist/types/src/edge/index.d.ts +0 -2
  84. package/dist/types/src/edge/index.d.ts.map +0 -1
  85. package/dist/types/src/errors.d.ts +0 -137
  86. package/dist/types/src/errors.d.ts.map +0 -1
  87. package/dist/types/src/examples/fib.d.ts +0 -7
  88. package/dist/types/src/examples/fib.d.ts.map +0 -1
  89. package/dist/types/src/examples/index.d.ts +0 -4
  90. package/dist/types/src/examples/index.d.ts.map +0 -1
  91. package/dist/types/src/examples/reply.d.ts +0 -3
  92. package/dist/types/src/examples/reply.d.ts.map +0 -1
  93. package/dist/types/src/examples/sleep.d.ts +0 -5
  94. package/dist/types/src/examples/sleep.d.ts.map +0 -1
  95. package/dist/types/src/executor/executor.d.ts +0 -11
  96. package/dist/types/src/executor/executor.d.ts.map +0 -1
  97. package/dist/types/src/executor/index.d.ts +0 -2
  98. package/dist/types/src/executor/index.d.ts.map +0 -1
  99. package/dist/types/src/handler.d.ts +0 -94
  100. package/dist/types/src/handler.d.ts.map +0 -1
  101. package/dist/types/src/schema.d.ts +0 -43
  102. package/dist/types/src/schema.d.ts.map +0 -1
  103. package/dist/types/src/services/database.d.ts +0 -98
  104. package/dist/types/src/services/database.d.ts.map +0 -1
  105. package/dist/types/src/services/event-logger.d.ts +0 -72
  106. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  107. package/dist/types/src/services/local-function-execution.d.ts +0 -25
  108. package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
  109. package/dist/types/src/services/remote-function-execution-service.d.ts +0 -15
  110. package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
  111. package/dist/types/src/services/service-container.d.ts +0 -56
  112. package/dist/types/src/services/service-container.d.ts.map +0 -1
  113. package/dist/types/src/services/service-registry.d.ts +0 -29
  114. package/dist/types/src/services/service-registry.d.ts.map +0 -1
  115. package/dist/types/src/services/service-registry.test.d.ts +0 -2
  116. package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
  117. package/dist/types/src/testing/index.d.ts +0 -3
  118. package/dist/types/src/testing/index.d.ts.map +0 -1
  119. package/dist/types/src/testing/layer.d.ts +0 -15
  120. package/dist/types/src/testing/layer.d.ts.map +0 -1
  121. package/dist/types/src/testing/logger.d.ts +0 -5
  122. package/dist/types/src/testing/logger.d.ts.map +0 -1
  123. package/dist/types/src/testing/persist-database.test.d.ts +0 -2
  124. package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
  125. package/dist/types/src/testing/services.d.ts +0 -59
  126. package/dist/types/src/testing/services.d.ts.map +0 -1
  127. package/dist/types/src/trace.d.ts +0 -122
  128. package/dist/types/src/trace.d.ts.map +0 -1
  129. package/dist/types/src/translations.d.ts +0 -12
  130. package/dist/types/src/translations.d.ts.map +0 -1
  131. package/dist/types/src/triggers/index.d.ts +0 -4
  132. package/dist/types/src/triggers/index.d.ts.map +0 -1
  133. package/dist/types/src/triggers/input-builder.d.ts +0 -3
  134. package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
  135. package/dist/types/src/triggers/invocation-tracer.d.ts +0 -35
  136. package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
  137. package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -75
  138. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
  139. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
  140. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
  141. package/dist/types/src/triggers/trigger-state-store.d.ts +0 -27
  142. package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
  143. package/dist/types/src/types.d.ts +0 -211
  144. package/dist/types/src/types.d.ts.map +0 -1
  145. package/dist/types/src/url.d.ts +0 -21
  146. package/dist/types/src/url.d.ts.map +0 -1
  147. package/src/bundler/bundler.test.ts +0 -58
  148. package/src/bundler/bundler.ts +0 -291
  149. package/src/bundler/index.ts +0 -5
  150. package/src/edge/functions.ts +0 -67
  151. package/src/edge/index.ts +0 -9
  152. package/src/errors.ts +0 -21
  153. package/src/examples/fib.ts +0 -30
  154. package/src/examples/index.ts +0 -7
  155. package/src/examples/reply.ts +0 -18
  156. package/src/examples/sleep.ts +0 -22
  157. package/src/executor/executor.ts +0 -54
  158. package/src/handler.ts +0 -201
  159. package/src/schema.ts +0 -68
  160. package/src/services/database.ts +0 -170
  161. package/src/services/event-logger.ts +0 -118
  162. package/src/services/local-function-execution.ts +0 -114
  163. package/src/services/remote-function-execution-service.ts +0 -46
  164. package/src/services/service-container.ts +0 -114
  165. package/src/services/service-registry.test.ts +0 -42
  166. package/src/services/service-registry.ts +0 -59
  167. package/src/testing/index.ts +0 -6
  168. package/src/testing/layer.ts +0 -111
  169. package/src/testing/logger.ts +0 -16
  170. package/src/testing/persist-database.test.ts +0 -87
  171. package/src/testing/services.ts +0 -115
  172. package/src/trace.ts +0 -178
  173. package/src/translations.ts +0 -20
  174. package/src/triggers/index.ts +0 -7
  175. package/src/triggers/input-builder.ts +0 -35
  176. package/src/triggers/invocation-tracer.ts +0 -99
  177. package/src/triggers/trigger-dispatcher.test.ts +0 -652
  178. package/src/triggers/trigger-dispatcher.ts +0 -512
  179. package/src/triggers/trigger-state-store.ts +0 -60
  180. package/src/types.ts +0 -200
  181. package/src/url.ts +0 -55
package/src/errors.ts DELETED
@@ -1,21 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { BaseError } from '@dxos/errors';
6
-
7
- export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAILABLE') {
8
- constructor(serviceName: string) {
9
- super(`Service not available: ${serviceName}`);
10
- }
11
- }
12
-
13
- export class FunctionNotFoundError extends BaseError.extend('FUNCTION_NOT_FOUND') {
14
- constructor(functionKey: string) {
15
- super(`Function not found: ${functionKey}`);
16
- }
17
- }
18
-
19
- export class FunctionError extends BaseError.extend('FUNCTION_ERROR') {}
20
-
21
- export class TriggerStateNotFoundError extends BaseError.extend('TRIGGER_STATE_NOT_FOUND') {}
@@ -1,30 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Effect, Schema } from 'effect';
6
-
7
- import { defineFunction } from '../handler';
8
-
9
- export default defineFunction({
10
- name: 'example.org/function/fib',
11
- description: 'Function that calculates a Fibonacci number',
12
- inputSchema: Schema.Struct({
13
- iterations: Schema.optional(Schema.Number).annotations({
14
- description: 'Number of iterations',
15
- default: 100_000,
16
- }),
17
- }),
18
- outputSchema: Schema.Struct({
19
- result: Schema.String,
20
- }),
21
- handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
22
- let a = 0n;
23
- let b = 1n;
24
- for (let i = 0; i < iterations; i++) {
25
- a += b;
26
- b = a - b;
27
- }
28
- return { result: a.toString() };
29
- }),
30
- });
@@ -1,7 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export { default as fib } from './fib';
6
- export { default as reply } from './reply';
7
- export { default as sleep } from './sleep';
@@ -1,18 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Console, Effect, Schema } from 'effect';
6
-
7
- import { defineFunction } from '../handler';
8
-
9
- export default defineFunction({
10
- name: 'example.org/function/reply',
11
- description: 'Function that echoes the input',
12
- inputSchema: Schema.Any,
13
- outputSchema: Schema.Any,
14
- handler: Effect.fn(function* ({ data }) {
15
- yield* Console.log('reply', { data });
16
- return data;
17
- }),
18
- });
@@ -1,22 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Effect, Schema } from 'effect';
6
-
7
- import { defineFunction } from '../handler';
8
-
9
- export default defineFunction({
10
- name: 'example.org/function/sleep',
11
- description: 'Function that sleeps for a given amount of time',
12
- inputSchema: Schema.Struct({
13
- duration: Schema.optional(Schema.Number).annotations({
14
- description: 'Milliseconds to sleep',
15
- default: 100_000,
16
- }),
17
- }),
18
- outputSchema: Schema.Void,
19
- handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
20
- yield* Effect.sleep(duration);
21
- }),
22
- });
@@ -1,54 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Effect, Schema } from 'effect';
6
-
7
- import { type SpaceId } from '@dxos/client/echo';
8
- import { runAndForwardErrors } from '@dxos/effect';
9
-
10
- import type { FunctionContext, FunctionDefinition } from '../handler';
11
- import type { ServiceContainer, Services } from '../services';
12
-
13
- export class FunctionExecutor {
14
- constructor(private readonly _services: ServiceContainer) {}
15
-
16
- /**
17
- * Invoke function.
18
- */
19
- // TODO(dmaretskyi): Invocation context: queue, space, etc...
20
- async invoke<F extends FunctionDefinition<any, any>>(
21
- functionDef: F,
22
- input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
23
- ): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
24
- // Assert input matches schema
25
- const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
26
- (assertInput as any)(input);
27
-
28
- const context: FunctionContext = {
29
- space: undefined,
30
- getService: this._services.getService.bind(this._services),
31
- getSpace: async (_spaceId: SpaceId) => {
32
- throw new Error('Not available. Use the database service instead.');
33
- },
34
- };
35
-
36
- const result = functionDef.handler({ context, data: input });
37
-
38
- let data: unknown;
39
- if (Effect.isEffect(result)) {
40
- data = await (result as Effect.Effect<unknown, unknown, Services>).pipe(
41
- Effect.provide(this._services.createLayer()),
42
- runAndForwardErrors,
43
- );
44
- } else {
45
- data = await result;
46
- }
47
-
48
- // Assert output matches schema
49
- const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
50
- (assertOutput as any)(data);
51
-
52
- return data as any;
53
- }
54
- }
package/src/handler.ts DELETED
@@ -1,201 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { type Context, Effect, Schema, type Types } from 'effect';
6
-
7
- import { Obj, Type } from '@dxos/echo';
8
- import { type EchoDatabase } from '@dxos/echo-db';
9
- import { type HasId } from '@dxos/echo-schema';
10
- import { assertArgument } from '@dxos/invariant';
11
- import { type DXN, type SpaceId } from '@dxos/keys';
12
- import { type QueryResult } from '@dxos/protocols';
13
-
14
- import { FunctionType } from './schema';
15
- import { type Services } from './services';
16
-
17
- // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
18
- // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
19
- // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
20
- // https://www.npmjs.com/package/aws-lambda
21
-
22
- /**
23
- * Function handler.
24
- */
25
- export type FunctionHandler<TData = {}, TOutput = any> = (params: {
26
- /**
27
- * Services and context available to the function.
28
- */
29
- context: FunctionContext;
30
-
31
- /**
32
- * Data passed as the input to the function.
33
- * Must match the function's input schema.
34
- * This will be the payload from the trigger or other data passed into the function in a workflow.
35
- */
36
- data: TData;
37
- }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, Services>;
38
-
39
- /**
40
- * Function context.
41
- */
42
- export interface FunctionContext {
43
- /**
44
- * Space from which the function was invoked.
45
- */
46
- space: SpaceAPI | undefined;
47
-
48
- /**
49
- * Resolves a service available to the function.
50
- * @throws if the service is not available.
51
- */
52
- getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
53
-
54
- getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
55
- }
56
-
57
- export interface FunctionContextAi {
58
- // TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.
59
- run(model: string, inputs: any, options?: any): Promise<any>;
60
- }
61
-
62
- //
63
- // API.
64
- //
65
-
66
- // TODO(dmaretskyi): Temporary API to get the queues working.
67
- // TODO(dmaretskyi): To be replaced with integrating queues into echo.
68
- export interface QueuesAPI {
69
- queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
70
- insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;
71
- }
72
-
73
- /**
74
- * Space interface available to functions.
75
- */
76
- export interface SpaceAPI {
77
- get id(): SpaceId;
78
- get db(): EchoDatabase;
79
-
80
- // TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
81
- get queues(): QueuesAPI;
82
- }
83
-
84
- // TODO(wittjosiah): Queues are incompatible.
85
- const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
86
- // const _: SpaceAPI = {} as Space;
87
- };
88
-
89
- const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
90
-
91
- export type FunctionDefinition<T = any, O = any> = {
92
- [typeId]: true;
93
- // TODO(dmaretskyi): Use `key` for FQN and `name` for human-readable-name.
94
- key: string;
95
- name: string;
96
- description?: string;
97
- inputSchema: Schema.Schema<T, any>;
98
- outputSchema?: Schema.Schema<O, any>;
99
- handler: FunctionHandler<T, O>;
100
- };
101
-
102
- export const defineFunction: {
103
- <I, O>(params: {
104
- // TODO(dmaretskyi): Make `key` required.
105
- key?: string;
106
- name: string;
107
- description?: string;
108
- inputSchema: Schema.Schema<I, any>;
109
- outputSchema?: Schema.Schema<O, any>;
110
- handler: Types.NoInfer<FunctionHandler<I, O>>;
111
- }): FunctionDefinition<I, O>;
112
- } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
113
- if (!Schema.isSchema(inputSchema)) {
114
- throw new Error('Input schema must be a valid schema');
115
- }
116
- if (typeof handler !== 'function') {
117
- throw new Error('Handler must be a function');
118
- }
119
-
120
- // Captures the function definition location.
121
- const limit = Error.stackTraceLimit;
122
- Error.stackTraceLimit = 2;
123
- const traceError = new Error();
124
- Error.stackTraceLimit = limit;
125
- let cache: false | string = false;
126
- const captureStackTrace = () => {
127
- if (cache !== false) {
128
- return cache;
129
- }
130
- if (traceError.stack !== undefined) {
131
- const stack = traceError.stack.split('\n');
132
- if (stack[2] !== undefined) {
133
- cache = stack[2].trim();
134
- return cache;
135
- }
136
- }
137
- };
138
-
139
- const handlerWithSpan = (...args: any[]) => {
140
- const result = (handler as any)(...args);
141
- if (Effect.isEffect(result)) {
142
- return Effect.withSpan(result, `${key ?? name}`, {
143
- captureStackTrace,
144
- });
145
- }
146
- return result;
147
- };
148
-
149
- return {
150
- [typeId]: true,
151
- key: key ?? name,
152
- name,
153
- description,
154
- inputSchema,
155
- outputSchema,
156
- handler: handlerWithSpan,
157
- };
158
- };
159
-
160
- export const FunctionDefinition = {
161
- make: defineFunction,
162
- isFunction: (value: unknown): value is FunctionDefinition.Any => {
163
- return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
164
- },
165
- serialize: (functionDef: FunctionDefinition.Any): FunctionType => {
166
- assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
167
- return serializeFunction(functionDef);
168
- },
169
- deserialize: (functionObj: FunctionType): FunctionDefinition.Any => {
170
- assertArgument(Obj.instanceOf(FunctionType, functionObj), 'functionObj');
171
- return deserializeFunction(functionObj);
172
- },
173
- };
174
- export declare namespace FunctionDefinition {
175
- export type Any = FunctionDefinition<any, any>;
176
- export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
177
- export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
178
- }
179
-
180
- export const serializeFunction = (functionDef: FunctionDefinition<any, any>): FunctionType =>
181
- Obj.make(FunctionType, {
182
- key: functionDef.name,
183
- name: functionDef.name,
184
- version: '0.1.0',
185
- description: functionDef.description,
186
- inputSchema: Type.toJsonSchema(functionDef.inputSchema),
187
- outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
188
- });
189
-
190
- export const deserializeFunction = (functionObj: FunctionType): FunctionDefinition<unknown, unknown> => {
191
- return {
192
- [typeId]: true,
193
- // TODO(dmaretskyi): Fix key.
194
- key: functionObj.key ?? functionObj.name,
195
- name: functionObj.name,
196
- description: functionObj.description,
197
- inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
198
- outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
199
- handler: () => {},
200
- };
201
- };
package/src/schema.ts DELETED
@@ -1,68 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
-
7
- import { Type } from '@dxos/echo';
8
- import { JsonSchemaType, LabelAnnotation, Ref } from '@dxos/echo-schema';
9
- import { DataType } from '@dxos/schema';
10
-
11
- /**
12
- * Source script.
13
- */
14
- export const ScriptType = Schema.Struct({
15
- name: Schema.optional(Schema.String),
16
- description: Schema.optional(Schema.String),
17
- // TODO(burdon): Change to hash of deployed content.
18
- // Whether source has changed since last deploy.
19
- changed: Schema.optional(Schema.Boolean),
20
- source: Ref(DataType.Text),
21
- }).pipe(
22
- Type.Obj({
23
- typename: 'dxos.org/type/Script',
24
- version: '0.1.0',
25
- }),
26
- LabelAnnotation.set(['name']),
27
- );
28
-
29
- export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
30
-
31
- /**
32
- * Function deployment.
33
- */
34
- export const FunctionType = Schema.Struct({
35
- /**
36
- * Global registry ID.
37
- * NOTE: The `key` property refers to the original registry entry.
38
- */
39
- // TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
40
- // TODO(dmaretskyi): Consider making it part of ECHO meta.
41
- // TODO(dmaretskyi): Make required.
42
- key: Schema.optional(Schema.String).annotations({
43
- description: 'Unique registration key for the blueprint',
44
- }),
45
-
46
- // TODO(burdon): Rename to id/uri?
47
- name: Schema.NonEmptyString,
48
- version: Schema.String,
49
-
50
- description: Schema.optional(Schema.String),
51
-
52
- // Reference to a source script if it exists within ECHO.
53
- // TODO(burdon): Don't ref ScriptType directly (core).
54
- source: Schema.optional(Ref(ScriptType)),
55
-
56
- inputSchema: Schema.optional(JsonSchemaType),
57
- outputSchema: Schema.optional(JsonSchemaType),
58
-
59
- // Local binding to a function name.
60
- binding: Schema.optional(Schema.String),
61
- }).pipe(
62
- Type.Obj({
63
- typename: 'dxos.org/type/Function',
64
- version: '0.1.0',
65
- }),
66
- LabelAnnotation.set(['name']),
67
- );
68
- export interface FunctionType extends Schema.Schema.Type<typeof FunctionType> {}
@@ -1,170 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Context, Effect, Layer, Option, type Schema } from 'effect';
6
-
7
- import { type Filter, type Live, Obj, type Query, type Ref, type Relation, type Type } from '@dxos/echo';
8
- import type { EchoDatabase, FlushOptions, OneShotQueryResult, QueryResult, SchemaRegistryQuery } from '@dxos/echo-db';
9
- import type { SchemaRegistryPreparedQuery } from '@dxos/echo-db';
10
- import type { EchoSchema } from '@dxos/echo-schema';
11
- import { promiseWithCauseCapture } from '@dxos/effect';
12
- import { BaseError } from '@dxos/errors';
13
- import { invariant } from '@dxos/invariant';
14
- import type { DXN } from '@dxos/keys';
15
-
16
- export class DatabaseService extends Context.Tag('@dxos/functions/DatabaseService')<
17
- DatabaseService,
18
- {
19
- readonly db: EchoDatabase;
20
- }
21
- >() {
22
- static notAvailable = Layer.succeed(DatabaseService, {
23
- get db(): EchoDatabase {
24
- throw new Error('Database not available');
25
- },
26
- });
27
-
28
- static make = (db: EchoDatabase): Context.Tag.Service<DatabaseService> => {
29
- return {
30
- get db() {
31
- return db;
32
- },
33
- };
34
- };
35
-
36
- static layer = (db: EchoDatabase): Layer.Layer<DatabaseService> => {
37
- return Layer.succeed(DatabaseService, DatabaseService.make(db));
38
- };
39
-
40
- /**
41
- * Resolves an object by its DXN.
42
- */
43
- static resolve: {
44
- // No type check.
45
- (dxn: DXN): Effect.Effect<Obj.Any | Relation.Any, never, DatabaseService>;
46
- // Check matches schema.
47
- <S extends Type.Obj.Any | Type.Relation.Any>(
48
- dxn: DXN,
49
- schema: S,
50
- ): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService>;
51
- } = (<S extends Type.Obj.Any | Type.Relation.Any>(
52
- dxn: DXN,
53
- schema?: S,
54
- ): Effect.Effect<Schema.Schema.Type<S>, ObjectNotFoundError, DatabaseService> =>
55
- Effect.gen(function* () {
56
- const { db } = yield* DatabaseService;
57
- const object = yield* promiseWithCauseCapture(() =>
58
- db.graph
59
- .createRefResolver({
60
- context: {
61
- space: db.spaceId,
62
- },
63
- })
64
- .resolve(dxn),
65
- );
66
-
67
- if (!object) {
68
- return yield* Effect.fail(new ObjectNotFoundError({ dxn }));
69
- }
70
- invariant(!schema || Obj.instanceOf(schema, object), 'Object type mismatch.');
71
- return object as any;
72
- })) as any;
73
-
74
- /**
75
- * Loads an object reference.
76
- */
77
- static load: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, ObjectNotFoundError, never> = Effect.fn(function* (ref) {
78
- const object = yield* promiseWithCauseCapture(() => ref.tryLoad());
79
- if (!object) {
80
- return yield* Effect.fail(new ObjectNotFoundError({ dxn: ref.dxn }));
81
- }
82
- return object;
83
- });
84
-
85
- /**
86
- * Loads an object reference option.
87
- */
88
- // TODO(burdon): Option?
89
- static loadOption: <T>(ref: Ref.Ref<T>) => Effect.Effect<Option.Option<T>, never, never> = Effect.fn(function* (ref) {
90
- const object = yield* DatabaseService.load(ref).pipe(
91
- Effect.catchTag('OBJECT_NOT_FOUND', () => Effect.succeed(undefined)),
92
- );
93
- return Option.fromNullable(object);
94
- });
95
-
96
- // TODO(burdon): Can we create a proxy for the following methods on EchoDatabase? Use @inheritDoc?
97
- // TODO(burdon): Figure out how to chain query().run();
98
-
99
- /**
100
- * @link EchoDatabase.add
101
- */
102
- static add = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<T, never, DatabaseService> =>
103
- DatabaseService.pipe(Effect.map(({ db }) => db.add(obj)));
104
-
105
- /**
106
- * @link EchoDatabase.remove
107
- */
108
- static remove = <T extends Obj.Any | Relation.Any>(obj: T): Effect.Effect<void, never, DatabaseService> =>
109
- DatabaseService.pipe(Effect.map(({ db }) => db.remove(obj)));
110
-
111
- /**
112
- * @link EchoDatabase.flush
113
- */
114
- static flush = (opts?: FlushOptions) =>
115
- DatabaseService.pipe(Effect.flatMap(({ db }) => promiseWithCauseCapture(() => db.flush(opts))));
116
-
117
- /**
118
- * @link EchoDatabase.getObjectById
119
- */
120
- static getObjectById = <T extends Obj.Any | Relation.Any>(
121
- id: string,
122
- ): Effect.Effect<Live<T> | undefined, never, DatabaseService> => {
123
- return DatabaseService.pipe(Effect.map(({ db }) => db.getObjectById(id)));
124
- };
125
-
126
- /**
127
- * Creates a `QueryResult` object that can be subscribed to.
128
- */
129
- static query: {
130
- <Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
131
- <F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
132
- } = (queryOrFilter: Query.Any | Filter.Any) =>
133
- DatabaseService.pipe(
134
- Effect.map(({ db }) => db.query(queryOrFilter as any)),
135
- Effect.withSpan('DatabaseService.query'),
136
- );
137
-
138
- /**
139
- * Executes the query once and returns the results.
140
- */
141
- static runQuery: {
142
- <Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;
143
- <F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;
144
- } = (queryOrFilter: Query.Any | Filter.Any) =>
145
- DatabaseService.query(queryOrFilter as any).pipe(
146
- Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
147
- );
148
-
149
- static schemaQuery = <Q extends SchemaRegistryQuery>(
150
- query: Q,
151
- ): Effect.Effect<SchemaRegistryPreparedQuery<EchoSchema>, never, DatabaseService> =>
152
- DatabaseService.pipe(
153
- Effect.map(({ db }) => db.schemaRegistry.query(query)),
154
- Effect.withSpan('DatabaseService.schemaQuery'),
155
- );
156
-
157
- static runSchemaQuery = <Q extends SchemaRegistryQuery>(
158
- query: Q,
159
- ): Effect.Effect<EchoSchema[], never, DatabaseService> =>
160
- DatabaseService.schemaQuery(query).pipe(
161
- Effect.flatMap((queryResult) => promiseWithCauseCapture(() => queryResult.run())),
162
- );
163
- }
164
-
165
- // TODO(burdon): Move to echo/errors.
166
- class ObjectNotFoundError extends BaseError.extend('OBJECT_NOT_FOUND') {
167
- constructor(context?: Record<string, unknown>) {
168
- super('Object not found', { context });
169
- }
170
- }