@dxos/functions 0.8.4-main.fffef41 → 0.9.0

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 (83) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/neutral/index.mjs +584 -0
  4. package/dist/lib/neutral/index.mjs.map +7 -0
  5. package/dist/lib/neutral/meta.json +1 -0
  6. package/dist/types/src/index.d.ts +0 -2
  7. package/dist/types/src/index.d.ts.map +1 -1
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  9. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  10. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  11. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  12. package/dist/types/src/protocol/protocol.d.ts +14 -2
  13. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  14. package/dist/types/src/sdk.d.ts +5 -84
  15. package/dist/types/src/sdk.d.ts.map +1 -1
  16. package/dist/types/src/services/credentials.d.ts +17 -38
  17. package/dist/types/src/services/credentials.d.ts.map +1 -1
  18. package/dist/types/src/services/function-invocation-service.d.ts +10 -3
  19. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  20. package/dist/types/src/services/index.d.ts +1 -6
  21. package/dist/types/src/services/index.d.ts.map +1 -1
  22. package/dist/types/src/services/tracing.d.ts +1 -50
  23. package/dist/types/src/services/tracing.d.ts.map +1 -1
  24. package/dist/types/src/types/index.d.ts +0 -4
  25. package/dist/types/src/types/index.d.ts.map +1 -1
  26. package/dist/types/src/types/url.d.ts +6 -5
  27. package/dist/types/src/types/url.d.ts.map +1 -1
  28. package/dist/types/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +25 -18
  30. package/src/index.ts +0 -2
  31. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  32. package/src/protocol/functions-ai-http-client.ts +141 -0
  33. package/src/protocol/protocol.test.ts +10 -10
  34. package/src/protocol/protocol.ts +355 -86
  35. package/src/sdk.ts +12 -209
  36. package/src/services/credentials.ts +80 -108
  37. package/src/services/function-invocation-service.ts +20 -7
  38. package/src/services/index.ts +1 -7
  39. package/src/services/tracing.ts +0 -100
  40. package/src/types/index.ts +0 -4
  41. package/src/types/url.ts +6 -5
  42. package/dist/lib/browser/index.mjs +0 -927
  43. package/dist/lib/browser/index.mjs.map +0 -7
  44. package/dist/lib/browser/meta.json +0 -1
  45. package/dist/lib/node-esm/index.mjs +0 -928
  46. package/dist/lib/node-esm/index.mjs.map +0 -7
  47. package/dist/lib/node-esm/meta.json +0 -1
  48. package/dist/types/src/errors.d.ts +0 -129
  49. package/dist/types/src/errors.d.ts.map +0 -1
  50. package/dist/types/src/example/fib.d.ts +0 -7
  51. package/dist/types/src/example/fib.d.ts.map +0 -1
  52. package/dist/types/src/example/forex-effect.d.ts +0 -3
  53. package/dist/types/src/example/forex-effect.d.ts.map +0 -1
  54. package/dist/types/src/example/index.d.ts +0 -12
  55. package/dist/types/src/example/index.d.ts.map +0 -1
  56. package/dist/types/src/example/reply.d.ts +0 -3
  57. package/dist/types/src/example/reply.d.ts.map +0 -1
  58. package/dist/types/src/example/sleep.d.ts +0 -5
  59. package/dist/types/src/example/sleep.d.ts.map +0 -1
  60. package/dist/types/src/services/event-logger.d.ts +0 -87
  61. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  62. package/dist/types/src/services/queues.d.ts +0 -47
  63. package/dist/types/src/services/queues.d.ts.map +0 -1
  64. package/dist/types/src/types/Function.d.ts +0 -58
  65. package/dist/types/src/types/Function.d.ts.map +0 -1
  66. package/dist/types/src/types/Script.d.ts +0 -28
  67. package/dist/types/src/types/Script.d.ts.map +0 -1
  68. package/dist/types/src/types/Trigger.d.ts +0 -139
  69. package/dist/types/src/types/Trigger.d.ts.map +0 -1
  70. package/dist/types/src/types/TriggerEvent.d.ts +0 -44
  71. package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
  72. package/src/errors.ts +0 -21
  73. package/src/example/fib.ts +0 -32
  74. package/src/example/forex-effect.ts +0 -40
  75. package/src/example/index.ts +0 -13
  76. package/src/example/reply.ts +0 -21
  77. package/src/example/sleep.ts +0 -24
  78. package/src/services/event-logger.ts +0 -127
  79. package/src/services/queues.ts +0 -84
  80. package/src/types/Function.ts +0 -62
  81. package/src/types/Script.ts +0 -33
  82. package/src/types/Trigger.ts +0 -139
  83. package/src/types/TriggerEvent.ts +0 -62
package/src/sdk.ts CHANGED
@@ -2,225 +2,28 @@
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 Credential, type Operation, type Trace } from '@dxos/compute';
7
+ import { type Database, type Feed } from '@dxos/echo';
13
8
 
14
- import {
15
- type CredentialsService,
16
- type FunctionInvocationService,
17
- type QueueService,
18
- type TracingService,
19
- } from './services';
20
- import { Function } from './types';
21
- import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
9
+ import { type FunctionInvocationService } from './services';
22
10
 
23
11
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
24
12
  // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
25
13
  // https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
26
14
  // https://www.npmjs.com/package/aws-lambda
27
15
 
28
- /**
29
- * Services that are provided at the function call site by the caller.
30
- */
31
- export type InvocationServices = TracingService;
32
-
33
16
  /**
34
17
  * Services that are available to invoked functions.
18
+ * @deprecated
35
19
  */
36
20
  export type FunctionServices =
37
- | InvocationServices
38
21
  | AiService.AiService
39
- | CredentialsService
40
- | DatabaseService
41
- | QueueService
42
- | 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
- };
22
+ | Credential.CredentialsService
23
+ | Database.Service
24
+ | Feed.FeedService
25
+ | Trace.TraceService
26
+ // TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
27
+ // it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
28
+ | FunctionInvocationService
29
+ | Operation.Service;
@@ -10,117 +10,23 @@ import * as Effect from 'effect/Effect';
10
10
  import * as Layer from 'effect/Layer';
11
11
  import * as Redacted from 'effect/Redacted';
12
12
 
13
- import { Query } from '@dxos/echo';
14
- import { DatabaseService } from '@dxos/echo-db';
13
+ import { Credential } from '@dxos/compute';
14
+ import { Database, Query } from '@dxos/echo';
15
15
  import { AccessToken } from '@dxos/types';
16
16
 
17
- export type CredentialQuery = {
18
- service?: string;
19
- };
20
-
21
- // TODO(dmaretskyi): Unify with other apis.
22
- // packages/sdk/schema/src/common/access-token.ts
23
- export type ServiceCredential = {
24
- service: string;
25
-
26
- // TODO(dmaretskyi): Build out.
27
- apiKey?: string;
28
- };
29
-
30
- export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
31
- CredentialsService,
32
- {
33
- /**
34
- * Query all.
35
- */
36
- queryCredentials: (query: CredentialQuery) => Promise<ServiceCredential[]>;
37
-
38
- /**
39
- * Get a single credential.
40
- * @throws {Error} If no credential is found.
41
- */
42
- getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
43
- }
44
- >() {
45
- static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
46
- Effect.gen(function* () {
47
- const credentials = yield* CredentialsService;
48
- return yield* Effect.promise(() => credentials.getCredential(query));
49
- });
50
-
51
- static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
52
- Effect.gen(function* () {
53
- const credential = yield* CredentialsService.getCredential(query);
54
- if (!credential.apiKey) {
55
- throw new Error(`API key not found for service: ${query.service}`);
56
- }
57
- return Redacted.make(credential.apiKey);
58
- });
59
-
60
- static configuredLayer = (credentials: ServiceCredential[]) =>
61
- Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
62
-
63
- static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
64
- Layer.effect(
65
- CredentialsService,
66
- Effect.gen(function* () {
67
- const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
68
- Effect.gen(function* () {
69
- return {
70
- service,
71
- apiKey: Redacted.value(yield* apiKey),
72
- };
73
- }),
74
- );
75
-
76
- return new ConfiguredCredentialsService(serviceCredentials);
77
- }),
78
- );
79
-
80
- static layerFromDatabase = () =>
81
- Layer.effect(
82
- CredentialsService,
83
- Effect.gen(function* () {
84
- const dbService = yield* DatabaseService;
85
- const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
86
- const { objects: accessTokens } = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
87
- return accessTokens
88
- .filter((accessToken) => accessToken.source === query.service)
89
- .map((accessToken) => ({
90
- service: accessToken.source,
91
- apiKey: accessToken.token,
92
- }));
93
- };
94
- return {
95
- getCredential: async (query) => {
96
- const credentials = await queryCredentials(query);
97
- if (credentials.length === 0) {
98
- throw new Error(`Credential not found for service: ${query.service}`);
99
- }
100
-
101
- return credentials[0];
102
- },
103
- queryCredentials: async (query) => {
104
- return queryCredentials(query);
105
- },
106
- };
107
- }),
108
- );
109
- }
110
-
111
- export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
112
- constructor(private readonly credentials: ServiceCredential[] = []) {}
17
+ export class ConfiguredCredentialsService implements Context.Tag.Service<Credential.CredentialsService> {
18
+ constructor(private readonly credentials: Credential.ServiceCredential[] = []) {}
113
19
 
114
- addCredentials(credentials: ServiceCredential[]): ConfiguredCredentialsService {
20
+ addCredentials(credentials: Credential.ServiceCredential[]): ConfiguredCredentialsService {
115
21
  this.credentials.push(...credentials);
116
22
  return this;
117
23
  }
118
24
 
119
- async queryCredentials(query: CredentialQuery): Promise<ServiceCredential[]> {
25
+ async queryCredentials(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> {
120
26
  return this.credentials.filter((credential) => credential.service === query.service);
121
27
  }
122
28
 
123
- async getCredential(query: CredentialQuery): Promise<ServiceCredential> {
29
+ async getCredential(query: Credential.CredentialQuery): Promise<Credential.ServiceCredential> {
124
30
  const credential = this.credentials.find((credential) => credential.service === query.service);
125
31
  if (!credential) {
126
32
  throw new Error(`Credential not found for service: ${query.service}`);
@@ -131,13 +37,79 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
131
37
  }
132
38
 
133
39
  /**
134
- * Maps the request to include the API key from the credential.
40
+ * Maps the request to include the given token in the Authorization header.
135
41
  */
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);
42
+ export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
43
+ HttpClient.mapRequest((request) => {
44
+ const authorization = kind ? `${kind} ${token}` : token;
45
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
46
+ });
47
+
48
+ export const configuredCredentialsLayer = (credentials: Credential.ServiceCredential[]) =>
49
+ Layer.succeed(Credential.CredentialsService, new ConfiguredCredentialsService(credentials));
50
+
51
+ export const credentialsLayerConfig = (
52
+ credentials: {
53
+ service: string;
54
+ apiKey: Config.Config<Redacted.Redacted<string>>;
55
+ }[],
56
+ ) =>
57
+ Layer.effect(
58
+ Credential.CredentialsService,
59
+ Effect.gen(function* () {
60
+ const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
61
+ Effect.gen(function* () {
62
+ return {
63
+ service,
64
+ apiKey: Redacted.value(yield* apiKey),
65
+ };
66
+ }),
67
+ );
68
+
69
+ return new ConfiguredCredentialsService(serviceCredentials);
70
+ }),
71
+ );
72
+
73
+ export const credentialsLayerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
74
+ Layer.effect(
75
+ Credential.CredentialsService,
76
+ Effect.gen(function* () {
77
+ const dbService = yield* Database.Service;
78
+ const cache = new Map<string, Credential.ServiceCredential[]>();
79
+
80
+ const queryCredentials = async (query: Credential.CredentialQuery): Promise<Credential.ServiceCredential[]> => {
81
+ const cacheKey = JSON.stringify(query);
82
+ if (caching && cache.has(cacheKey)) {
83
+ return cache.get(cacheKey)!;
84
+ }
85
+
86
+ const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
87
+ const credentials = accessTokens
88
+ .filter((accessToken) => accessToken.source === query.service)
89
+ .map((accessToken) => ({
90
+ service: accessToken.source,
91
+ apiKey: accessToken.token,
92
+ }));
93
+
94
+ if (caching) {
95
+ cache.set(cacheKey, credentials);
96
+ }
97
+
98
+ return credentials;
99
+ };
100
+
101
+ return {
102
+ getCredential: async (query) => {
103
+ const credentials = await queryCredentials(query);
104
+ if (credentials.length === 0) {
105
+ throw new Error(`Credential not found for service: ${query.service}`);
106
+ }
107
+
108
+ return credentials[0];
109
+ },
110
+ queryCredentials: async (query) => {
111
+ return queryCredentials(query);
112
+ },
113
+ };
142
114
  }),
143
115
  );
@@ -3,21 +3,34 @@
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 { type FunctionNotFoundError, Operation } from '@dxos/compute';
8
9
 
10
+ /**
11
+ * @deprecated
12
+ */
9
13
  export class FunctionInvocationService extends Context.Tag('@dxos/functions/FunctionInvocationService')<
10
14
  FunctionInvocationService,
11
15
  {
12
- invokeFunction<I, O>(
13
- functionDef: FunctionDefinition<I, O, any>,
14
- input: I,
15
- ): Effect.Effect<O, never, InvocationServices>;
16
+ invokeFunction<I, O>(functionDef: Operation.Definition<I, O, any>, input: I): Effect.Effect<O>;
17
+
18
+ resolveFunction(key: string): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError>;
16
19
  }
17
20
  >() {
21
+ static layerNotAvailable = Layer.succeed(FunctionInvocationService, {
22
+ invokeFunction: () => Effect.die('FunctionInvocationService is not avaialble.'),
23
+ resolveFunction: () => Effect.die('FunctionInvocationService is not available.'),
24
+ });
25
+
18
26
  static invokeFunction = <I, O>(
19
- functionDef: FunctionDefinition<I, O, any>,
27
+ functionDef: Operation.Definition<I, O, any>,
20
28
  input: I,
21
- ): Effect.Effect<O, never, FunctionInvocationService | InvocationServices> =>
29
+ ): Effect.Effect<O, never, FunctionInvocationService> =>
22
30
  Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.invokeFunction)(functionDef, input);
31
+
32
+ static resolveFunction = (
33
+ key: string,
34
+ ): Effect.Effect<Operation.Definition.Any, FunctionNotFoundError, FunctionInvocationService> =>
35
+ Effect.serviceFunctionEffect(FunctionInvocationService, (service) => service.resolveFunction)(key);
23
36
  }
@@ -2,12 +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
- export { ConfiguredCredentialsService, type ServiceCredential } from './credentials';
9
- export * from './event-logger';
10
- export { createEventLogger, createDefectLogger } from './event-logger';
11
6
  export * from './function-invocation-service';
12
- export * from './queues';
13
- export * from './tracing';
7
+ export { MESSAGE_PROPERTY_TOOL_CALL_ID } from './tracing';
@@ -2,106 +2,6 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import * as Context from 'effect/Context';
6
- import * as Effect from 'effect/Effect';
7
- import * as Layer from 'effect/Layer';
8
-
9
- import { AgentStatus } from '@dxos/ai';
10
- import { Obj } from '@dxos/echo';
11
- import type { ObjectId } from '@dxos/echo/internal';
12
- import { Message } from '@dxos/types';
13
-
14
- /**
15
- * Provides a way for compute primitives (functions, workflows, tools)
16
- * to emit an execution trace as a series of structured ECHO objects.
17
- */
18
- export class TracingService extends Context.Tag('@dxos/functions/TracingService')<
19
- TracingService,
20
- {
21
- /**
22
- * Gets the parent message ID.
23
- */
24
- getTraceContext: () => TracingService.TraceContext;
25
-
26
- /**
27
- * Write an event to the tracing queue.
28
- * @param event - The event to write. Must be an a typed object.
29
- */
30
- write: (event: Obj.Any) => void;
31
- }
32
- >() {
33
- static noop: Context.Tag.Service<TracingService> = {
34
- getTraceContext: () => ({}),
35
- write: () => {},
36
- };
37
-
38
- static layerNoop: Layer.Layer<TracingService> = Layer.succeed(TracingService, TracingService.noop);
39
- /**
40
- * Creates a TracingService layer that emits events to the parent tracing service.
41
- */
42
- static layerSubframe = (mapContext: (currentContext: TracingService.TraceContext) => TracingService.TraceContext) =>
43
- Layer.effect(
44
- TracingService,
45
- Effect.gen(function* () {
46
- const tracing = yield* TracingService;
47
- const context = mapContext(tracing.getTraceContext());
48
- return {
49
- write: (event) => tracing.write(event),
50
- getTraceContext: () => context,
51
- };
52
- }),
53
- );
54
-
55
- /**
56
- * Emit the current human-readable execution status.
57
- */
58
- static emitStatus: (
59
- data: Omit<Obj.MakeProps<typeof AgentStatus>, 'created'>,
60
- ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
61
- const tracing = yield* TracingService;
62
- tracing.write(
63
- Obj.make(AgentStatus, {
64
- parentMessage: tracing.getTraceContext().parentMessage,
65
- toolCallId: tracing.getTraceContext().toolCallId,
66
- created: new Date().toISOString(),
67
- ...data,
68
- }),
69
- );
70
- });
71
-
72
- static emitConverationMessage: (
73
- data: Obj.MakeProps<typeof Message.Message>,
74
- ) => Effect.Effect<void, never, TracingService> = Effect.fnUntraced(function* (data) {
75
- const tracing = yield* TracingService;
76
- tracing.write(
77
- Obj.make(Message.Message, {
78
- parentMessage: tracing.getTraceContext().parentMessage,
79
- ...data,
80
- properties: {
81
- [MESSAGE_PROPERTY_TOOL_CALL_ID]: tracing.getTraceContext().toolCallId,
82
- ...data.properties,
83
- },
84
- }),
85
- );
86
- });
87
- }
88
-
89
- export namespace TracingService {
90
- export interface TraceContext {
91
- /**
92
- * If this thread sprung from a tool call, this is the ID of the message containing the tool call.
93
- */
94
- parentMessage?: ObjectId;
95
-
96
- /**
97
- * If the current thread is a byproduct of a tool call, this is the ID of the tool call.
98
- */
99
- toolCallId?: string;
100
-
101
- debugInfo?: unknown;
102
- }
103
- }
104
-
105
5
  /**
106
6
  * Goes into {@link Message['properties']}
107
7
  */
@@ -2,8 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * as Function from './Function';
6
- export * as Script from './Script';
7
- export * as Trigger from './Trigger';
8
- export * as TriggerEvent from './TriggerEvent';
9
5
  export * from './url';
package/src/types/url.ts CHANGED
@@ -2,24 +2,25 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { type ObjectMeta } from '@dxos/echo/internal';
5
+ import { type Obj } from '@dxos/echo';
6
6
 
7
7
  // TODO: use URL scheme for source?
8
- export const FUNCTIONS_META_KEY = 'dxos.org/service/function';
8
+ export const FUNCTIONS_META_KEY = 'org.dxos.service.function';
9
9
 
10
- export const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
10
+ export const FUNCTIONS_PRESET_META_KEY = 'org.dxos.service.function-preset';
11
11
 
12
12
  /**
13
13
  * NOTE: functionId is backend ID, not ECHO object id.
14
14
  */
15
- export const getUserFunctionIdInMetadata = (meta: ObjectMeta) => {
15
+ export const getUserFunctionIdInMetadata = (meta: Obj.ReadonlyMeta) => {
16
16
  return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
17
17
  };
18
18
 
19
19
  /**
20
20
  * NOTE: functionId is backend ID, not ECHO object id.
21
+ * Must be called inside Obj.update() since it mutates the meta.
21
22
  */
22
- export const setUserFunctionIdInMetadata = (meta: ObjectMeta, functionId: string) => {
23
+ export const setUserFunctionIdInMetadata = (meta: Obj.Meta, functionId: string) => {
23
24
  const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
24
25
  if (key) {
25
26
  if (key.id !== functionId) {