@dxos/functions 0.8.4-main.72ec0f3 → 0.8.4-main.74a063c4e0

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 (106) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/Trace.mjs +38 -0
  3. package/dist/lib/neutral/Trace.mjs.map +7 -0
  4. package/dist/lib/neutral/chunk-5T7GOX2V.mjs +149 -0
  5. package/dist/lib/neutral/chunk-5T7GOX2V.mjs.map +7 -0
  6. package/dist/lib/neutral/chunk-IVDUS56O.mjs +49 -0
  7. package/dist/lib/neutral/chunk-IVDUS56O.mjs.map +7 -0
  8. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  9. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  10. package/dist/lib/neutral/fib-N45KAC7C.mjs +23 -0
  11. package/dist/lib/neutral/fib-N45KAC7C.mjs.map +7 -0
  12. package/dist/lib/neutral/index.mjs +1266 -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-EUEPKNJF.mjs +19 -0
  16. package/dist/lib/neutral/reply-EUEPKNJF.mjs.map +7 -0
  17. package/dist/lib/neutral/sleep-PUK3D4FF.mjs +15 -0
  18. package/dist/lib/neutral/sleep-PUK3D4FF.mjs.map +7 -0
  19. package/dist/types/src/Trace.d.ts +155 -0
  20. package/dist/types/src/Trace.d.ts.map +1 -0
  21. package/dist/types/src/errors.d.ts +24 -32
  22. package/dist/types/src/errors.d.ts.map +1 -1
  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 +3 -2
  26. package/dist/types/src/example/fib.d.ts.map +1 -1
  27. package/dist/types/src/example/index.d.ts +3 -11
  28. package/dist/types/src/example/index.d.ts.map +1 -1
  29. package/dist/types/src/example/reply.d.ts +2 -1
  30. package/dist/types/src/example/reply.d.ts.map +1 -1
  31. package/dist/types/src/example/sleep.d.ts +3 -2
  32. package/dist/types/src/example/sleep.d.ts.map +1 -1
  33. package/dist/types/src/index.d.ts +4 -0
  34. package/dist/types/src/index.d.ts.map +1 -1
  35. package/dist/types/src/process/Process.d.ts +247 -0
  36. package/dist/types/src/process/Process.d.ts.map +1 -0
  37. package/dist/types/src/process/ServiceResolver.d.ts +74 -0
  38. package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
  39. package/dist/types/src/process/StorageService.d.ts +58 -0
  40. package/dist/types/src/process/StorageService.d.ts.map +1 -0
  41. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  42. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  43. package/dist/types/src/protocol/protocol.d.ts +2 -2
  44. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  45. package/dist/types/src/sdk.d.ts +4 -79
  46. package/dist/types/src/sdk.d.ts.map +1 -1
  47. package/dist/types/src/services/credentials.d.ts +6 -4
  48. package/dist/types/src/services/credentials.d.ts.map +1 -1
  49. package/dist/types/src/services/event-logger.d.ts +25 -31
  50. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  51. package/dist/types/src/services/function-invocation-service.d.ts +9 -3
  52. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  53. package/dist/types/src/services/index.d.ts +0 -1
  54. package/dist/types/src/services/index.d.ts.map +1 -1
  55. package/dist/types/src/services/queues.d.ts +8 -5
  56. package/dist/types/src/services/queues.d.ts.map +1 -1
  57. package/dist/types/src/services/tracing.d.ts +61 -4
  58. package/dist/types/src/services/tracing.d.ts.map +1 -1
  59. package/dist/types/src/types/Script.d.ts +10 -16
  60. package/dist/types/src/types/Script.d.ts.map +1 -1
  61. package/dist/types/src/types/Trigger.d.ts +57 -76
  62. package/dist/types/src/types/Trigger.d.ts.map +1 -1
  63. package/dist/types/src/types/TriggerEvent.d.ts +44 -13
  64. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
  65. package/dist/types/src/types/index.d.ts +0 -1
  66. package/dist/types/src/types/index.d.ts.map +1 -1
  67. package/dist/types/src/types/url.d.ts +6 -5
  68. package/dist/types/src/types/url.d.ts.map +1 -1
  69. package/dist/types/tsconfig.tsbuildinfo +1 -1
  70. package/package.json +29 -17
  71. package/src/Trace.ts +202 -0
  72. package/src/errors.ts +5 -5
  73. package/src/example/definitions.ts +49 -0
  74. package/src/example/fib.ts +15 -24
  75. package/src/example/forex-effect.ts +1 -1
  76. package/src/example/index.ts +7 -8
  77. package/src/example/reply.ts +11 -13
  78. package/src/example/sleep.ts +9 -16
  79. package/src/index.ts +4 -0
  80. package/src/process/Process.ts +486 -0
  81. package/src/process/ServiceResolver.ts +174 -0
  82. package/src/process/StorageService.ts +99 -0
  83. package/src/protocol/functions-ai-http-client.ts +67 -0
  84. package/src/protocol/protocol.test.ts +0 -1
  85. package/src/protocol/protocol.ts +206 -84
  86. package/src/sdk.ts +7 -194
  87. package/src/services/credentials.ts +31 -15
  88. package/src/services/event-logger.ts +2 -2
  89. package/src/services/function-invocation-service.ts +19 -3
  90. package/src/services/index.ts +0 -2
  91. package/src/services/queues.ts +15 -8
  92. package/src/services/tracing.ts +97 -5
  93. package/src/types/Script.ts +14 -9
  94. package/src/types/Trigger.ts +33 -18
  95. package/src/types/TriggerEvent.ts +29 -29
  96. package/src/types/index.ts +0 -1
  97. package/src/types/url.ts +6 -5
  98. package/dist/lib/browser/index.mjs +0 -927
  99. package/dist/lib/browser/index.mjs.map +0 -7
  100. package/dist/lib/browser/meta.json +0 -1
  101. package/dist/lib/node-esm/index.mjs +0 -928
  102. package/dist/lib/node-esm/index.mjs.map +0 -7
  103. package/dist/lib/node-esm/meta.json +0 -1
  104. package/dist/types/src/types/Function.d.ts +0 -58
  105. package/dist/types/src/types/Function.d.ts.map +0 -1
  106. package/src/types/Function.ts +0 -62
package/src/sdk.ts CHANGED
@@ -2,14 +2,9 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import type * as Context from 'effect/Context';
6
- import * as Effect from 'effect/Effect';
7
- import * as Schema from 'effect/Schema';
8
-
9
5
  import { type AiService } from '@dxos/ai';
10
- import { Obj, Type } from '@dxos/echo';
11
- import { type DatabaseService } from '@dxos/echo-db';
12
- import { assertArgument, failedInvariant } from '@dxos/invariant';
6
+ import { type Database, type Feed } from '@dxos/echo';
7
+ import { type Trace } from '@dxos/functions';
13
8
 
14
9
  import {
15
10
  type CredentialsService,
@@ -17,8 +12,6 @@ import {
17
12
  type QueueService,
18
13
  type TracingService,
19
14
  } from './services';
20
- import { Function } from './types';
21
- import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
22
15
 
23
16
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
24
17
  // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
@@ -32,195 +25,15 @@ export type InvocationServices = TracingService;
32
25
 
33
26
  /**
34
27
  * Services that are available to invoked functions.
28
+ * @deprecated
35
29
  */
36
30
  export type FunctionServices =
37
31
  | InvocationServices
38
32
  | AiService.AiService
39
33
  | CredentialsService
40
- | DatabaseService
34
+ | Database.Service
35
+ // TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
41
36
  | QueueService
37
+ | Feed.FeedService
38
+ | Trace.TraceService
42
39
  | FunctionInvocationService;
43
-
44
- /**
45
- * Function handler.
46
- */
47
- export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
48
- /**
49
- * Context available to the function.
50
- */
51
- context: FunctionContext;
52
-
53
- /**
54
- * Data passed as the input to the function.
55
- * Must match the function's input schema.
56
- * This will be the payload from the trigger or other data passed into the function in a workflow.
57
- */
58
- data: TData;
59
- }) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
60
-
61
- /**
62
- * Function context.
63
- */
64
- export interface FunctionContext {
65
- // TODO(dmaretskyi): Consider what we should put into context.
66
- }
67
-
68
- const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
69
-
70
- export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
71
- [typeId]: true;
72
- key: string;
73
- name: string;
74
- description?: string;
75
- inputSchema: Schema.Schema<T, any>;
76
- outputSchema?: Schema.Schema<O, any>;
77
-
78
- /**
79
- * Keys of the required services.
80
- */
81
- services: readonly string[];
82
-
83
- handler: FunctionHandler<T, O, S>;
84
- meta?: {
85
- /**
86
- * Tools that are projected from functions have this annotation.
87
- *
88
- * deployedFunctionId:
89
- * - Backend deployment ID assigned by the EDGE function service (typically a UUID).
90
- * - Used for remote invocation via `FunctionInvocationService` → `RemoteFunctionExecutionService`.
91
- * - Persisted on the corresponding ECHO `Function.Function` object's metadata under the
92
- * `FUNCTIONS_META_KEY` and retrieved with `getUserFunctionIdInMetadata`.
93
- */
94
- deployedFunctionId?: string;
95
- };
96
- };
97
-
98
- export declare namespace FunctionDefinition {
99
- export type Any = FunctionDefinition<any, any, any>;
100
- export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
101
- export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
102
- export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
103
- }
104
-
105
- export type FunctionProps<T, O> = {
106
- key: string;
107
- name: string;
108
- description?: string;
109
- inputSchema: Schema.Schema<T, any>;
110
- outputSchema?: Schema.Schema<O, any>;
111
- // TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
112
- services?: readonly Context.Tag<any, any>[];
113
-
114
- handler: FunctionHandler<T, O, FunctionServices>;
115
- };
116
-
117
- // TODO(dmaretskyi): Output type doesn't get typechecked.
118
- export const defineFunction: {
119
- <I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
120
- } = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, services }) => {
121
- if (!Schema.isSchema(inputSchema)) {
122
- throw new Error('Input schema must be a valid schema');
123
- }
124
- if (typeof handler !== 'function') {
125
- throw new Error('Handler must be a function');
126
- }
127
-
128
- // Captures the function definition location.
129
- const limit = Error.stackTraceLimit;
130
- Error.stackTraceLimit = 2;
131
- const traceError = new Error();
132
- Error.stackTraceLimit = limit;
133
- let cache: false | string = false;
134
- const captureStackTrace = () => {
135
- if (cache !== false) {
136
- return cache;
137
- }
138
- if (traceError.stack !== undefined) {
139
- const stack = traceError.stack.split('\n');
140
- if (stack[2] !== undefined) {
141
- cache = stack[2].trim();
142
- return cache;
143
- }
144
- }
145
- };
146
-
147
- const handlerWithSpan = (...args: any[]) => {
148
- const result = (handler as any)(...args);
149
- if (Effect.isEffect(result)) {
150
- return Effect.withSpan(result, `${key ?? name}`, {
151
- captureStackTrace,
152
- });
153
- }
154
- return result;
155
- };
156
-
157
- return {
158
- [typeId]: true,
159
- key,
160
- name,
161
- description,
162
- inputSchema,
163
- outputSchema,
164
- handler: handlerWithSpan,
165
- services: !services ? [] : getServiceKeys(services),
166
- } satisfies FunctionDefinition.Any;
167
- };
168
-
169
- const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
170
- return services.map((tag: any) => {
171
- if (typeof tag.key === 'string') {
172
- return tag.key;
173
- }
174
- console.log(tag);
175
- failedInvariant();
176
- });
177
- };
178
-
179
- export const FunctionDefinition = {
180
- make: defineFunction,
181
- isFunction: (value: unknown): value is FunctionDefinition.Any => {
182
- return typeof value === 'object' && value !== null && Symbol.for('@dxos/functions/FunctionDefinition') in value;
183
- },
184
- serialize: (functionDef: FunctionDefinition.Any): Function.Function => {
185
- assertArgument(FunctionDefinition.isFunction(functionDef), 'functionDef');
186
- return serializeFunction(functionDef);
187
- },
188
- deserialize: (functionObj: Function.Function): FunctionDefinition.Any => {
189
- assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
190
- return deserializeFunction(functionObj);
191
- },
192
- };
193
-
194
- export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
195
- const fn = Function.make({
196
- key: functionDef.key,
197
- name: functionDef.name,
198
- version: '0.1.0',
199
- description: functionDef.description,
200
- inputSchema: Type.toJsonSchema(functionDef.inputSchema),
201
- outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
202
- services: functionDef.services,
203
- });
204
- if (functionDef.meta?.deployedFunctionId) {
205
- setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta.deployedFunctionId);
206
- }
207
- return fn;
208
- };
209
-
210
- export const deserializeFunction = (functionObj: Function.Function): FunctionDefinition<unknown, unknown> => {
211
- return {
212
- [typeId]: true,
213
- // TODO(dmaretskyi): Fix key.
214
- key: functionObj.key ?? functionObj.name,
215
- name: functionObj.name,
216
- description: functionObj.description,
217
- inputSchema: !functionObj.inputSchema ? Schema.Unknown : Type.toEffectSchema(functionObj.inputSchema),
218
- outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
219
- // TODO(dmaretskyi): This should throw error.
220
- handler: () => {},
221
- services: functionObj.services ?? [],
222
- meta: {
223
- deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
224
- },
225
- };
226
- };
@@ -11,7 +11,7 @@ import * as Layer from 'effect/Layer';
11
11
  import * as Redacted from 'effect/Redacted';
12
12
 
13
13
  import { Query } from '@dxos/echo';
14
- import { DatabaseService } from '@dxos/echo-db';
14
+ import { Database } from '@dxos/echo';
15
15
  import { AccessToken } from '@dxos/types';
16
16
 
17
17
  export type CredentialQuery = {
@@ -60,7 +60,12 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
60
60
  static configuredLayer = (credentials: ServiceCredential[]) =>
61
61
  Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
62
62
 
63
- static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
63
+ static layerConfig = (
64
+ credentials: {
65
+ service: string;
66
+ apiKey: Config.Config<Redacted.Redacted<string>>;
67
+ }[],
68
+ ) =>
64
69
  Layer.effect(
65
70
  CredentialsService,
66
71
  Effect.gen(function* () {
@@ -77,20 +82,34 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
77
82
  }),
78
83
  );
79
84
 
80
- static layerFromDatabase = () =>
85
+ static layerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
81
86
  Layer.effect(
82
87
  CredentialsService,
83
88
  Effect.gen(function* () {
84
- const dbService = yield* DatabaseService;
89
+ const dbService = yield* Database.Service;
90
+ const cache = new Map<string, ServiceCredential[]>();
91
+
85
92
  const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
86
- const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
87
- return accessTokens
93
+ const cacheKey = JSON.stringify(query);
94
+ if (caching && cache.has(cacheKey)) {
95
+ return cache.get(cacheKey)!;
96
+ }
97
+
98
+ const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
99
+ const credentials = accessTokens
88
100
  .filter((accessToken) => accessToken.source === query.service)
89
101
  .map((accessToken) => ({
90
102
  service: accessToken.source,
91
103
  apiKey: accessToken.token,
92
104
  }));
105
+
106
+ if (caching) {
107
+ cache.set(cacheKey, credentials);
108
+ }
109
+
110
+ return credentials;
93
111
  };
112
+
94
113
  return {
95
114
  getCredential: async (query) => {
96
115
  const credentials = await queryCredentials(query);
@@ -131,13 +150,10 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
131
150
  }
132
151
 
133
152
  /**
134
- * Maps the request to include the API key from the credential.
153
+ * Maps the request to include the given token in the Authorization header.
135
154
  */
136
- export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
137
- HttpClient.mapRequestEffect(
138
- Effect.fnUntraced(function* (request) {
139
- const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
140
- const authorization = kind ? `${kind} ${key}` : key;
141
- return HttpClientRequest.setHeader(request, 'Authorization', authorization);
142
- }),
143
- );
155
+ export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
156
+ HttpClient.mapRequest((request) => {
157
+ const authorization = kind ? `${kind} ${token}` : token;
158
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
159
+ });
@@ -52,7 +52,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
52
52
 
53
53
  export const ComputeEvent = Schema.Struct({
54
54
  payload: ComputeEventPayload,
55
- }).pipe(Type.Obj({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
55
+ }).pipe(Type.object({ typename: 'org.dxos.type.computeEvent', version: '0.1.0' }));
56
56
 
57
57
  /**
58
58
  * Logs event for the compute workflows.
@@ -75,7 +75,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
75
75
  const tracing = yield* TracingService;
76
76
  return {
77
77
  log: (event: ComputeEventPayload) => {
78
- tracing.write(Obj.make(ComputeEvent, { payload: event }));
78
+ tracing.write(Obj.make(ComputeEvent, { payload: event }), tracing.getTraceContext());
79
79
  },
80
80
  nodeId: undefined,
81
81
  };
@@ -3,21 +3,37 @@
3
3
  //
4
4
  import * as Context from 'effect/Context';
5
5
  import * as Effect from 'effect/Effect';
6
+ import * as Layer from 'effect/Layer';
6
7
 
7
- import { type FunctionDefinition, type InvocationServices } from '../sdk';
8
+ import { Operation } from '@dxos/operation';
9
+
10
+ import type { FunctionNotFoundError } from '../errors';
11
+ import { type InvocationServices } from '../sdk';
8
12
 
9
13
  export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
10
14
  FunctionInvocationService,
11
15
  {
12
16
  invokeFunction<I, O>(
13
- functionDef: FunctionDefinition<I, O, any>,
17
+ functionDef: Operation.Definition<I, O, any>,
14
18
  input: I,
15
19
  ): Effect.Effect<O, never, InvocationServices>;
20
+
21
+ resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
16
22
  }
17
23
  >() {
24
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
25
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
26
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
27
+ });
28
+
18
29
  static invokeFunction = <I, O>(
19
- functionDef: FunctionDefinition<I, O, any>,
30
+ functionDef: Operation.Definition<I, O, any>,
20
31
  input: I,
21
32
  ): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
22
33
  Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
34
+
35
+ static resolveFunction = (
36
+ key: string,
37
+ ): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
38
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
23
39
  }
@@ -2,8 +2,6 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export { DatabaseService } from '@dxos/echo-db';
6
-
7
5
  export * from './credentials';
8
6
  export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
9
7
  export * from './event-logger';
@@ -6,12 +6,13 @@ import * as Context from 'effect/Context';
6
6
  import * as Effect from 'effect/Effect';
7
7
  import * as Layer from 'effect/Layer';
8
8
 
9
- import type { Obj, Relation } from '@dxos/echo';
10
- import type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';
9
+ import { type Entity } from '@dxos/echo';
10
+ import { createFeedServiceLayer, type Queue, type QueueAPI, type QueueFactory } from '@dxos/echo-db';
11
11
  import type { DXN, QueueSubspaceTag } from '@dxos/keys';
12
12
 
13
13
  /**
14
14
  * Gives access to all queues.
15
+ * @deprecated Use Feed.FeedService instead.
15
16
  */
16
17
  export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
17
18
  QueueService,
@@ -53,26 +54,25 @@ export class QueueService extends Context.Tag('@dxos/functions/QueueService')<
53
54
  /**
54
55
  * Gets a queue by its DXN.
55
56
  */
56
- static getQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
57
+ static getQueue = <T extends Entity.Unknown = Entity.Unknown>(
57
58
  dxn: DXN,
58
59
  ): Effect.Effect<Queue<T>, never, QueueService> => QueueService.pipe(Effect.map(({ queues }) => queues.get<T>(dxn)));
59
60
 
60
61
  /**
61
62
  * Creates a new queue.
62
63
  */
63
- static createQueue = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(options?: {
64
+ static createQueue = <T extends Entity.Unknown = Entity.Unknown>(options?: {
64
65
  subspaceTag?: QueueSubspaceTag;
65
66
  }): Effect.Effect<Queue<T>, never, QueueService> =>
66
67
  QueueService.pipe(Effect.map(({ queues }) => queues.create<T>(options)));
67
68
 
68
- static append = <T extends Obj.Any | Relation.Any = Obj.Any | Relation.Any>(
69
- queue: Queue<T>,
70
- objects: T[],
71
- ): Effect.Effect<void> => Effect.promise(() => queue.append(objects));
69
+ static append = <T extends Entity.Unknown = Entity.Unknown>(queue: Queue<T>, objects: T[]): Effect.Effect<void> =>
70
+ Effect.promise(() => queue.append(objects));
72
71
  }
73
72
 
74
73
  /**
75
74
  * Gives access to a specific queue passed as a context.
75
+ * @deprecated Use Feed.FeedService instead.
76
76
  */
77
77
  export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQueueService')<
78
78
  ContextQueueService,
@@ -82,3 +82,10 @@ export class ContextQueueService extends Context.Tag('@dxos/functions/ContextQue
82
82
  >() {
83
83
  static layer = (queue: Queue) => Layer.succeed(ContextQueueService, { queue });
84
84
  }
85
+
86
+ export const feedServiceFromQueueServiceLayer = Layer.unwrapEffect(
87
+ Effect.gen(function* () {
88
+ const { queues } = yield* QueueService;
89
+ return createFeedServiceLayer(queues);
90
+ }),
91
+ );
@@ -7,13 +7,16 @@ import * as Effect from 'effect/Effect';
7
7
  import * as Layer from 'effect/Layer';
8
8
 
9
9
  import { AgentStatus } from '@dxos/ai';
10
- import { Obj } from '@dxos/echo';
11
- import type { ObjectId } from '@dxos/echo/internal';
10
+ import { type DXN, Obj, Ref } from '@dxos/echo';
11
+ import { ObjectId } from '@dxos/keys';
12
12
  import { Message } from '@dxos/types';
13
13
 
14
+ import type { Trigger } from '../types';
15
+
14
16
  /**
15
17
  * Provides a way for compute primitives (functions, workflows, tools)
16
18
  * to emit an execution trace as a series of structured ECHO objects.
19
+ * @deprecated Use Trace.TraceService instead.
17
20
  */
18
21
  export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
19
22
  TracingService,
@@ -27,15 +30,41 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
27
30
  * Write an event to the tracing queue.
28
31
  * @param event - The event to write. Must be an a typed object.
29
32
  */
30
- write: (event: Obj.Any) => void;
33
+ write: (event: Obj.Unknown, traceContext: TracingService.TraceContext) => void;
34
+
35
+ /**
36
+ * Write an ephemeral event.
37
+ */
38
+ ephemeral: (event: Obj.Unknown, traceContext: TracingService.TraceContext) => void;
39
+
40
+ traceInvocationStart({
41
+ payload,
42
+ target,
43
+ }: {
44
+ payload: TracingService.FunctionInvocationPayload;
45
+ target?: DXN;
46
+ }): Effect.Effect<TracingService.InvocationTraceData>;
47
+
48
+ traceInvocationEnd({
49
+ trace,
50
+ exception,
51
+ }: {
52
+ trace: TracingService.InvocationTraceData;
53
+ exception?: any;
54
+ }): Effect.Effect<void>;
31
55
  }
32
56
  >() {
33
57
  static noop: Context.Tag.Service<TracingService> = {
34
58
  getTraceContext: () => ({}),
35
59
  write: () => {},
60
+ ephemeral: () => {},
61
+ traceInvocationStart: () =>
62
+ Effect.sync(() => ({ invocationId: ObjectId.random(), invocationTraceQueue: undefined })),
63
+ traceInvocationEnd: () => Effect.sync(() => {}),
36
64
  };
37
65
 
38
66
  static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
67
+
39
68
  /**
40
69
  * Creates a TracingService layer that emits events to the parent tracing service.
41
70
  */
@@ -46,12 +75,26 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
46
75
  const tracing = yield* TracingService;
47
76
  const context = mapContext(tracing.getTraceContext());
48
77
  return {
49
- write: (event) => tracing.write(event),
78
+ write: (event, context) => tracing.write(event, context),
79
+ ephemeral: (event, context) => tracing.ephemeral(event, context),
50
80
  getTraceContext: () => context,
81
+ traceInvocationStart: () => Effect.die('Tracing invocation inside another invocation is not supported.'),
82
+ traceInvocationEnd: () => Effect.die('Tracing invocation inside another invocation is not supported.'),
51
83
  };
52
84
  }),
53
85
  );
54
86
 
87
+ /**
88
+ * Create sublayer to trace an invocation.
89
+ * @param data
90
+ * @returns
91
+ */
92
+ static layerInvocation = (data: TracingService.InvocationTraceData) =>
93
+ TracingService.layerSubframe((context) => ({
94
+ ...context,
95
+ currentInvocation: data,
96
+ }));
97
+
55
98
  /**
56
99
  * Emit the current human-readable execution status.
57
100
  */
@@ -66,11 +109,12 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
66
109
  created: new Date().toISOString(),
67
110
  ...data,
68
111
  }),
112
+ tracing.getTraceContext(),
69
113
  );
70
114
  });
71
115
 
72
116
  static emitConverationMessage: (
73
- data: Obj.MakeProps<typeof Message.Message>,
117
+ data: Obj.MakeProps<typeof Message.Message> | Message.Message,
74
118
  ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
75
119
  const tracing = yield* TracingService;
76
120
  tracing.write(
@@ -82,12 +126,21 @@ export class TracingService extends Context.Tag('@dxos/functions/TracingService'
82
126
  ...data.properties,
83
127
  },
84
128
  }),
129
+ tracing.getTraceContext(),
85
130
  );
86
131
  });
132
+
133
+ static emitEphemeralMessage: (data: Message.Message) => Effect.Effect<void, never, TracingService> =
134
+ Effect.fnUntraced(function* (data) {
135
+ const tracing = yield* TracingService;
136
+ tracing.ephemeral(data, tracing.getTraceContext());
137
+ });
87
138
  }
88
139
 
89
140
  export namespace TracingService {
90
141
  export interface TraceContext {
142
+ currentInvocation?: InvocationTraceData;
143
+
91
144
  /**
92
145
  * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
93
146
  */
@@ -100,6 +153,45 @@ export namespace TracingService {
100
153
 
101
154
  debugInfo?: unknown;
102
155
  }
156
+
157
+ /**
158
+ * Trace data for a function/trigger invocation.
159
+ */
160
+ export interface InvocationTraceData {
161
+ invocationId: ObjectId;
162
+ invocationTraceQueue?: DXN.String;
163
+ }
164
+
165
+ /**
166
+ * Payload for a function/trigger invocation.
167
+ */
168
+ export interface FunctionInvocationPayload {
169
+ data?: any;
170
+ inputNodeId?: string;
171
+ trigger?: {
172
+ id: string;
173
+ kind: Trigger.Kind;
174
+ };
175
+ chat?: Ref.Ref<Obj.Unknown>;
176
+ process?: {
177
+ pid: string;
178
+ parentPid?: string;
179
+ /**
180
+ * Key of the executable.
181
+ */
182
+ key: string;
183
+
184
+ /**
185
+ * Process name.
186
+ */
187
+ name?: string;
188
+
189
+ /**
190
+ * Target object that the process is assigned to.
191
+ */
192
+ target?: string;
193
+ };
194
+ }
103
195
  }
104
196
 
105
197
  /**
@@ -4,8 +4,8 @@
4
4
 
5
5
  import * as Schema from 'effect/Schema';
6
6
 
7
- import { Obj, Ref, Type } from '@dxos/echo';
8
- import { FormAnnotation, LabelAnnotation } from '@dxos/echo/internal';
7
+ import { Annotation, Obj, Ref, Type } from '@dxos/echo';
8
+ import { FormInputAnnotation } from '@dxos/echo/internal';
9
9
  import { Text } from '@dxos/schema';
10
10
 
11
11
  /**
@@ -16,18 +16,23 @@ export const Script = Schema.Struct({
16
16
  description: Schema.String.pipe(Schema.optional),
17
17
  // TODO(burdon): Change to hash of deployed content.
18
18
  // Whether source has changed since last deploy.
19
- changed: Schema.Boolean.pipe(FormAnnotation.set(false), Schema.optional),
20
- source: Type.Ref(Text.Text).pipe(FormAnnotation.set(false)),
19
+ changed: Schema.Boolean.pipe(FormInputAnnotation.set(false), Schema.optional),
20
+ source: Ref.Ref(Text.Text).pipe(FormInputAnnotation.set(false)),
21
21
  }).pipe(
22
- Type.Obj({
23
- typename: 'dxos.org/type/Script',
22
+ Type.object({
23
+ typename: 'org.dxos.type.script',
24
24
  version: '0.1.0',
25
25
  }),
26
- LabelAnnotation.set(['name']),
26
+ Annotation.LabelAnnotation.set(['name']),
27
+ Annotation.IconAnnotation.set({
28
+ icon: 'ph--code--regular',
29
+ hue: 'sky',
30
+ }),
27
31
  );
32
+
28
33
  export interface Script extends Schema.Schema.Type<typeof Script> {}
29
34
 
30
35
  type Props = Omit<Obj.MakeProps<typeof Script>, 'source'> & { source?: string };
31
36
 
32
- export const make = ({ source = '', ...props }: Props = {}) =>
33
- Obj.make(Script, { ...props, source: Ref.make(Text.make(source)) });
37
+ export const make = ({ source = '', ...props }: Props = {}): Script =>
38
+ Obj.make(Script, { ...props, source: Ref.make(Text.make({ content: source })) });