@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fffef41

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 (155) hide show
  1. package/dist/lib/browser/index.mjs +795 -360
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +795 -360
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/errors.d.ts +129 -0
  8. package/dist/types/src/errors.d.ts.map +1 -0
  9. package/dist/types/src/example/fib.d.ts +7 -0
  10. package/dist/types/src/example/fib.d.ts.map +1 -0
  11. package/dist/types/src/example/forex-effect.d.ts +3 -0
  12. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  13. package/dist/types/src/example/index.d.ts +12 -0
  14. package/dist/types/src/example/index.d.ts.map +1 -0
  15. package/dist/types/src/example/reply.d.ts +3 -0
  16. package/dist/types/src/example/reply.d.ts.map +1 -0
  17. package/dist/types/src/example/sleep.d.ts +5 -0
  18. package/dist/types/src/example/sleep.d.ts.map +1 -0
  19. package/dist/types/src/index.d.ts +5 -6
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/protocol/index.d.ts +2 -0
  22. package/dist/types/src/protocol/index.d.ts.map +1 -0
  23. package/dist/types/src/protocol/protocol.d.ts +7 -0
  24. package/dist/types/src/protocol/protocol.d.ts.map +1 -0
  25. package/dist/types/src/protocol/protocol.test.d.ts +2 -0
  26. package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
  27. package/dist/types/src/sdk.d.ts +89 -0
  28. package/dist/types/src/sdk.d.ts.map +1 -0
  29. package/dist/types/src/services/credentials.d.ts +21 -3
  30. package/dist/types/src/services/credentials.d.ts.map +1 -1
  31. package/dist/types/src/services/event-logger.d.ts +80 -30
  32. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  33. package/dist/types/src/services/function-invocation-service.d.ts +11 -0
  34. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  35. package/dist/types/src/services/index.d.ts +6 -7
  36. package/dist/types/src/services/index.d.ts.map +1 -1
  37. package/dist/types/src/services/queues.d.ts +33 -4
  38. package/dist/types/src/services/queues.d.ts.map +1 -1
  39. package/dist/types/src/services/tracing.d.ts +44 -5
  40. package/dist/types/src/services/tracing.d.ts.map +1 -1
  41. package/dist/types/src/types/Function.d.ts +58 -0
  42. package/dist/types/src/types/Function.d.ts.map +1 -0
  43. package/dist/types/src/types/Script.d.ts +28 -0
  44. package/dist/types/src/types/Script.d.ts.map +1 -0
  45. package/dist/types/src/types/Trigger.d.ts +139 -0
  46. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  47. package/dist/types/src/types/TriggerEvent.d.ts +44 -0
  48. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  49. package/dist/types/src/types/index.d.ts +6 -0
  50. package/dist/types/src/types/index.d.ts.map +1 -0
  51. package/dist/types/src/types/url.d.ts +12 -0
  52. package/dist/types/src/types/url.d.ts.map +1 -0
  53. package/dist/types/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +19 -66
  55. package/src/errors.ts +21 -0
  56. package/src/example/fib.ts +32 -0
  57. package/src/example/forex-effect.ts +40 -0
  58. package/src/example/index.ts +13 -0
  59. package/src/example/reply.ts +21 -0
  60. package/src/example/sleep.ts +24 -0
  61. package/src/index.ts +5 -8
  62. package/src/{executor → protocol}/index.ts +1 -1
  63. package/src/protocol/protocol.test.ts +59 -0
  64. package/src/protocol/protocol.ts +145 -0
  65. package/src/sdk.ts +226 -0
  66. package/src/services/credentials.ts +92 -4
  67. package/src/services/event-logger.ts +77 -37
  68. package/src/services/function-invocation-service.ts +23 -0
  69. package/src/services/index.ts +7 -7
  70. package/src/services/queues.ts +50 -8
  71. package/src/services/tracing.ts +92 -11
  72. package/src/types/Function.ts +62 -0
  73. package/src/types/Script.ts +33 -0
  74. package/src/types/Trigger.ts +139 -0
  75. package/src/types/TriggerEvent.ts +62 -0
  76. package/src/types/index.ts +9 -0
  77. package/src/types/url.ts +31 -0
  78. package/dist/lib/browser/bundler/index.mjs +0 -247
  79. package/dist/lib/browser/bundler/index.mjs.map +0 -7
  80. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  81. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  82. package/dist/lib/browser/edge/index.mjs +0 -69
  83. package/dist/lib/browser/edge/index.mjs.map +0 -7
  84. package/dist/lib/browser/testing/index.mjs +0 -79
  85. package/dist/lib/browser/testing/index.mjs.map +0 -7
  86. package/dist/lib/node-esm/bundler/index.mjs +0 -249
  87. package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
  88. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  89. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  90. package/dist/lib/node-esm/edge/index.mjs +0 -71
  91. package/dist/lib/node-esm/edge/index.mjs.map +0 -7
  92. package/dist/lib/node-esm/testing/index.mjs +0 -80
  93. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  94. package/dist/types/src/bundler/bundler.d.ts +0 -50
  95. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  96. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  97. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  98. package/dist/types/src/bundler/index.d.ts +0 -2
  99. package/dist/types/src/bundler/index.d.ts.map +0 -1
  100. package/dist/types/src/edge/functions.d.ts +0 -16
  101. package/dist/types/src/edge/functions.d.ts.map +0 -1
  102. package/dist/types/src/edge/index.d.ts +0 -2
  103. package/dist/types/src/edge/index.d.ts.map +0 -1
  104. package/dist/types/src/executor/executor.d.ts +0 -8
  105. package/dist/types/src/executor/executor.d.ts.map +0 -1
  106. package/dist/types/src/executor/index.d.ts +0 -2
  107. package/dist/types/src/executor/index.d.ts.map +0 -1
  108. package/dist/types/src/handler.d.ts +0 -61
  109. package/dist/types/src/handler.d.ts.map +0 -1
  110. package/dist/types/src/schema.d.ts +0 -38
  111. package/dist/types/src/schema.d.ts.map +0 -1
  112. package/dist/types/src/services/ai.d.ts +0 -12
  113. package/dist/types/src/services/ai.d.ts.map +0 -1
  114. package/dist/types/src/services/database.d.ts +0 -15
  115. package/dist/types/src/services/database.d.ts.map +0 -1
  116. package/dist/types/src/services/function-call-service.d.ts +0 -16
  117. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  118. package/dist/types/src/services/service-container.d.ts +0 -46
  119. package/dist/types/src/services/service-container.d.ts.map +0 -1
  120. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  121. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  122. package/dist/types/src/testing/index.d.ts +0 -2
  123. package/dist/types/src/testing/index.d.ts.map +0 -1
  124. package/dist/types/src/testing/logger.d.ts +0 -5
  125. package/dist/types/src/testing/logger.d.ts.map +0 -1
  126. package/dist/types/src/testing/services.d.ts +0 -71
  127. package/dist/types/src/testing/services.d.ts.map +0 -1
  128. package/dist/types/src/trace.d.ts +0 -124
  129. package/dist/types/src/trace.d.ts.map +0 -1
  130. package/dist/types/src/translations.d.ts +0 -12
  131. package/dist/types/src/translations.d.ts.map +0 -1
  132. package/dist/types/src/types.d.ts +0 -411
  133. package/dist/types/src/types.d.ts.map +0 -1
  134. package/dist/types/src/url.d.ts +0 -17
  135. package/dist/types/src/url.d.ts.map +0 -1
  136. package/src/bundler/bundler.test.ts +0 -59
  137. package/src/bundler/bundler.ts +0 -292
  138. package/src/bundler/index.ts +0 -5
  139. package/src/edge/functions.ts +0 -64
  140. package/src/edge/index.ts +0 -9
  141. package/src/executor/executor.ts +0 -54
  142. package/src/handler.ts +0 -113
  143. package/src/schema.ts +0 -57
  144. package/src/services/ai.ts +0 -32
  145. package/src/services/database.ts +0 -50
  146. package/src/services/function-call-service.ts +0 -64
  147. package/src/services/service-container.ts +0 -127
  148. package/src/services/tool-resolver.ts +0 -31
  149. package/src/testing/index.ts +0 -5
  150. package/src/testing/logger.ts +0 -16
  151. package/src/testing/services.ts +0 -174
  152. package/src/trace.ts +0 -180
  153. package/src/translations.ts +0 -20
  154. package/src/types.ts +0 -211
  155. package/src/url.ts +0 -52
@@ -1,64 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Context } from 'effect';
6
-
7
- import type { SpaceId } from '@dxos/keys';
8
-
9
- /**
10
- * Allows calling into other functions.
11
- */
12
- export class FunctionCallService extends Context.Tag('FunctionCallService')<
13
- FunctionCallService,
14
- {
15
- callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;
16
- }
17
- >() {
18
- static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<FunctionCallService> {
19
- return {
20
- callFunction: async (deployedFunctionId: string, input: any) => {
21
- const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });
22
- const result = await fetch(url, {
23
- method: 'POST',
24
- headers: { 'Content-Type': 'application/json' },
25
- body: JSON.stringify(input),
26
- });
27
- if (result.status >= 300 || result.status < 200) {
28
- throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });
29
- }
30
- return await result.json();
31
- },
32
- };
33
- }
34
-
35
- static mock = () => {
36
- return {
37
- callFunction: async (deployedFunctionId: string, input: any) => {
38
- return input;
39
- },
40
- };
41
- };
42
- }
43
-
44
- // TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.
45
- const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
46
- const baseUrl = new URL('functions/', edgeUrl);
47
-
48
- // Leading slashes cause the URL to be treated as an absolute path.
49
- const relativeUrl = functionUrl.replace(/^\//, '');
50
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
51
- options.spaceId && url.searchParams.set('spaceId', options.spaceId);
52
- options.subjectId && url.searchParams.set('subjectId', options.subjectId);
53
- url.protocol = isSecure(url.protocol) ? 'https' : 'http';
54
- return url.toString();
55
- };
56
-
57
- const isSecure = (protocol: string) => {
58
- return protocol === 'https:' || protocol === 'wss:';
59
- };
60
-
61
- type InvocationOptions = {
62
- spaceId?: SpaceId;
63
- subjectId?: string;
64
- };
@@ -1,127 +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 { ToolResolverService } from './tool-resolver';
14
- import { TracingService } from './tracing';
15
-
16
- /**
17
- * List of all service tags and their names.
18
- */
19
- export interface ServiceTagRecord {
20
- ai: AiService;
21
- credentials: CredentialsService;
22
- database: DatabaseService;
23
- eventLogger: EventLogger;
24
- functionCallService: FunctionCallService;
25
- tracing: TracingService;
26
- queues: QueueService;
27
- toolResolver: ToolResolverService;
28
- }
29
-
30
- /**
31
- * List of all services and their runtime types.
32
- */
33
- export type ServiceRecord = {
34
- [K in keyof ServiceTagRecord]: Context.Tag.Service<ServiceTagRecord[K]>;
35
- };
36
-
37
- /**
38
- * Union of all services.
39
- */
40
- export type Services = ServiceTagRecord[keyof ServiceTagRecord];
41
-
42
- const SERVICE_MAPPING: Record<string, keyof ServiceRecord> = {
43
- [AiService.key]: 'ai',
44
- [CredentialsService.key]: 'credentials',
45
- [DatabaseService.key]: 'database',
46
- [EventLogger.key]: 'eventLogger',
47
- [FunctionCallService.key]: 'functionCallService',
48
- [QueueService.key]: 'queues',
49
- [TracingService.key]: 'tracing',
50
- [ToolResolverService.key]: 'toolResolver',
51
- };
52
-
53
- export const SERVICE_TAGS: Context.Tag<any, any>[] = [
54
- AiService,
55
- CredentialsService,
56
- DatabaseService,
57
- EventLogger,
58
- FunctionCallService,
59
- TracingService,
60
- QueueService,
61
- ];
62
-
63
- const DEFAULT_SERVICES: Partial<ServiceRecord> = {
64
- tracing: TracingService.noop,
65
- };
66
-
67
- export class ServiceContainer {
68
- private _services: Partial<ServiceRecord> = { ...DEFAULT_SERVICES };
69
-
70
- /**
71
- * Set services.
72
- * @param services - Services to set.
73
- * @returns The container instance.
74
- */
75
- setServices(services: Partial<ServiceRecord>): this {
76
- this._services = { ...this._services, ...services };
77
- return this;
78
- }
79
-
80
- getService<Id, T>(tag: Context.Tag<Id, T>): T {
81
- const serviceKey = SERVICE_MAPPING[tag.key];
82
- const service = serviceKey != null ? this._services[serviceKey] : undefined;
83
- if (!service) {
84
- throw new Error(`Service not available: ${tag.key}`);
85
- }
86
-
87
- return service as T;
88
- }
89
-
90
- clone(): ServiceContainer {
91
- return new ServiceContainer().setServices({ ...this._services });
92
- }
93
-
94
- // 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.
95
- createLayer(): Layer.Layer<Services> {
96
- const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;
97
- const credentials = Layer.succeed(CredentialsService, new ConfiguredCredentialsService());
98
- const database =
99
- this._services.database != null
100
- ? Layer.succeed(DatabaseService, this._services.database)
101
- : DatabaseService.notAvailable;
102
- const queues =
103
- this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
104
- const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);
105
- const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
106
- const functionCallService = Layer.succeed(
107
- FunctionCallService,
108
- this._services.functionCallService ?? FunctionCallService.mock(),
109
- );
110
- const toolResolver = Layer.succeed(
111
- ToolResolverService,
112
- this._services.toolResolver ?? ToolResolverService.notAvailable,
113
- );
114
-
115
- return Layer.mergeAll(
116
- //
117
- ai,
118
- credentials,
119
- database,
120
- queues,
121
- tracing,
122
- eventLogger,
123
- functionCallService,
124
- toolResolver,
125
- );
126
- }
127
- }
@@ -1,31 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Context } from 'effect';
6
-
7
- import { type ToolResolver } from '@dxos/ai';
8
-
9
- export class ToolResolverService extends Context.Tag('ToolResolverService')<
10
- ToolResolverService,
11
- {
12
- /**
13
- * Tool resolver.
14
- */
15
- readonly toolResolver: ToolResolver;
16
- }
17
- >() {
18
- static notAvailable: Context.Tag.Service<ToolResolverService> = {
19
- toolResolver: {
20
- resolve: async (id: string) => {
21
- throw new Error('Tool resolver not available');
22
- },
23
- },
24
- };
25
-
26
- static make = (toolResolver: ToolResolver): Context.Tag.Service<ToolResolverService> => {
27
- return {
28
- toolResolver,
29
- };
30
- };
31
- }
@@ -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,174 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type Context } from 'effect';
6
-
7
- import { type AiServiceEdgeClientOptions, EdgeAiServiceClient, type AiServiceClient, ToolRegistry } from '@dxos/ai';
8
- import { AI_SERVICE_ENDPOINT, createTestAiServiceClient } from '@dxos/ai/testing';
9
- import type { Space } from '@dxos/client/echo';
10
- import type { EchoDatabase, QueueFactory } from '@dxos/echo-db';
11
- import { assertArgument } from '@dxos/invariant';
12
-
13
- import { consoleLogger, noopLogger } from './logger';
14
- import {
15
- AiService,
16
- ConfiguredCredentialsService,
17
- type CredentialsService,
18
- DatabaseService,
19
- type EventLogger,
20
- QueueService,
21
- ServiceContainer,
22
- type ServiceCredential,
23
- ToolResolverService,
24
- type TracingService,
25
- } from '../services';
26
-
27
- // TODO(burdon): Factor out.
28
- export type OneOf<T> = {
29
- [K in keyof T]: { [P in K]: T[P] } & { [P in Exclude<keyof T, K>]?: never };
30
- }[keyof T];
31
-
32
- export type AiServiceProvider = 'dev' | 'edge' | 'ollama' | 'lmstudio';
33
-
34
- export type TestServiceOptions = {
35
- /**
36
- * AI service configuration.
37
- */
38
- ai?: OneOf<{
39
- /**
40
- * Custom AI service client.
41
- */
42
- client?: AiServiceClient;
43
-
44
- /**
45
- * Edge AI service at specified endpoint.
46
- */
47
- endpoint?: AiServiceEdgeClientOptions['endpoint'];
48
-
49
- /**
50
- * Predefined AI service configuration.
51
- */
52
- // TODO(burdon): 'dev' and 'edge' are redundant with providing an endpoint.
53
- provider?: AiServiceProvider;
54
- }>;
55
-
56
- /**
57
- * Credentials service configuration.
58
- */
59
- credentials?: OneOf<{
60
- /**
61
- * Predefined credentials list.
62
- */
63
- services?: ServiceCredential[];
64
-
65
- /**
66
- * Custom credentials service.
67
- */
68
- service?: Context.Tag.Service<CredentialsService>;
69
- }>;
70
-
71
- /**
72
- * Database configuration.
73
- */
74
- db?: EchoDatabase;
75
-
76
- /**
77
- * Gets database and queue services from the space.
78
- * Exclusive with: `db`, `queues`
79
- */
80
- space?: Space;
81
-
82
- /**
83
- * Logging configuration.
84
- */
85
- logging?: {
86
- enabled?: boolean;
87
- logger?: Context.Tag.Service<EventLogger>;
88
- };
89
-
90
- /**
91
- * Queue service configuration.
92
- */
93
- queues?: QueueFactory;
94
-
95
- tracing?: {
96
- service?: Context.Tag.Service<TracingService>;
97
- };
98
-
99
- toolResolver?: Context.Tag.Service<ToolResolverService>;
100
- };
101
-
102
- export const createTestServices = ({
103
- ai,
104
- credentials,
105
- db,
106
- logging,
107
- queues,
108
- space,
109
- tracing,
110
- toolResolver,
111
- }: TestServiceOptions = {}): ServiceContainer => {
112
- assertArgument(!(!!space && (!!db || !!queues)), 'space can be provided only if db and queues are not');
113
-
114
- return new ServiceContainer().setServices({
115
- ai: createAiService(ai),
116
- credentials: createCredentialsService(credentials),
117
- database: space || db ? DatabaseService.make(space?.db || db!) : undefined,
118
- eventLogger: logging?.logger ?? logging?.enabled ? consoleLogger : noopLogger,
119
- queues: space || queues ? QueueService.make(space?.queues || queues!, undefined) : undefined,
120
- tracing: tracing?.service,
121
- toolResolver: toolResolver ?? ToolResolverService.make(new ToolRegistry([])),
122
- });
123
- };
124
-
125
- // TODO(burdon): Enable model configuration.
126
- const createAiService = (ai: TestServiceOptions['ai']): Context.Tag.Service<AiService> | undefined => {
127
- if (ai?.client != null) {
128
- return AiService.make(ai.client);
129
- }
130
-
131
- if (ai?.endpoint != null) {
132
- return AiService.make(new EdgeAiServiceClient({ endpoint: ai.endpoint }));
133
- }
134
-
135
- switch (ai?.provider) {
136
- case 'dev':
137
- return AiService.make(
138
- new EdgeAiServiceClient({
139
- endpoint: AI_SERVICE_ENDPOINT.LOCAL,
140
- defaultGenerationOptions: {
141
- model: '@anthropic/claude-3-5-sonnet-20241022',
142
- },
143
- }),
144
- );
145
-
146
- case 'edge':
147
- return AiService.make(
148
- new EdgeAiServiceClient({
149
- endpoint: AI_SERVICE_ENDPOINT.REMOTE,
150
- defaultGenerationOptions: {
151
- model: '@anthropic/claude-3-5-sonnet-20241022',
152
- },
153
- }),
154
- );
155
-
156
- case 'ollama':
157
- return AiService.make(createTestAiServiceClient());
158
-
159
- case 'lmstudio':
160
- throw new Error('LMStudio is not supported');
161
- }
162
- };
163
-
164
- const createCredentialsService = (
165
- credentials: TestServiceOptions['credentials'] | undefined,
166
- ): Context.Tag.Service<CredentialsService> | undefined => {
167
- if (credentials?.services) {
168
- return new ConfiguredCredentialsService(credentials.services);
169
- }
170
-
171
- if (credentials?.service) {
172
- return credentials.service;
173
- }
174
- };
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 Resource } from 'i18next';
6
-
7
- import { ScriptType } from './schema';
8
-
9
- export const translations = [
10
- {
11
- 'en-US': {
12
- [ScriptType.typename]: {
13
- 'typename label': 'Script',
14
- 'typename label_zero': 'Scripts',
15
- 'typename label_one': 'Script',
16
- 'typename label_other': 'Scripts',
17
- },
18
- },
19
- },
20
- ] as const satisfies Resource[];