@dxos/functions 0.8.4-main.d05673bc65 → 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 (77) hide show
  1. package/dist/lib/neutral/index.mjs +282 -929
  2. package/dist/lib/neutral/index.mjs.map +4 -4
  3. package/dist/lib/neutral/meta.json +1 -1
  4. package/dist/types/src/index.d.ts +0 -2
  5. package/dist/types/src/index.d.ts.map +1 -1
  6. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -1
  7. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  8. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  9. package/dist/types/src/protocol/protocol.d.ts +14 -2
  10. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  11. package/dist/types/src/sdk.d.ts +5 -112
  12. package/dist/types/src/sdk.d.ts.map +1 -1
  13. package/dist/types/src/services/credentials.d.ts +15 -38
  14. package/dist/types/src/services/credentials.d.ts.map +1 -1
  15. package/dist/types/src/services/function-invocation-service.d.ts +5 -6
  16. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  17. package/dist/types/src/services/index.d.ts +1 -4
  18. package/dist/types/src/services/index.d.ts.map +1 -1
  19. package/dist/types/src/services/queues.d.ts +1 -46
  20. package/dist/types/src/services/queues.d.ts.map +1 -1
  21. package/dist/types/src/services/tracing.d.ts +1 -84
  22. package/dist/types/src/services/tracing.d.ts.map +1 -1
  23. package/dist/types/src/types/index.d.ts +0 -4
  24. package/dist/types/src/types/index.d.ts.map +1 -1
  25. package/dist/types/src/types/url.d.ts +1 -1
  26. package/dist/types/src/types/url.d.ts.map +1 -1
  27. package/dist/types/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +17 -16
  29. package/src/index.ts +0 -2
  30. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  31. package/src/protocol/functions-ai-http-client.ts +75 -1
  32. package/src/protocol/protocol.test.ts +9 -10
  33. package/src/protocol/protocol.ts +227 -57
  34. package/src/sdk.ts +12 -278
  35. package/src/services/credentials.ts +75 -118
  36. package/src/services/function-invocation-service.ts +6 -10
  37. package/src/services/index.ts +1 -4
  38. package/src/services/queues.ts +1 -78
  39. package/src/services/tracing.ts +0 -159
  40. package/src/types/index.ts +0 -4
  41. package/src/types/url.ts +1 -1
  42. package/dist/types/src/errors.d.ts +0 -121
  43. package/dist/types/src/errors.d.ts.map +0 -1
  44. package/dist/types/src/example/fib.d.ts +0 -7
  45. package/dist/types/src/example/fib.d.ts.map +0 -1
  46. package/dist/types/src/example/forex-effect.d.ts +0 -3
  47. package/dist/types/src/example/forex-effect.d.ts.map +0 -1
  48. package/dist/types/src/example/index.d.ts +0 -12
  49. package/dist/types/src/example/index.d.ts.map +0 -1
  50. package/dist/types/src/example/reply.d.ts +0 -3
  51. package/dist/types/src/example/reply.d.ts.map +0 -1
  52. package/dist/types/src/example/sleep.d.ts +0 -5
  53. package/dist/types/src/example/sleep.d.ts.map +0 -1
  54. package/dist/types/src/operation-compatibility.test.d.ts +0 -2
  55. package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
  56. package/dist/types/src/services/event-logger.d.ts +0 -81
  57. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  58. package/dist/types/src/types/Function.d.ts +0 -52
  59. package/dist/types/src/types/Function.d.ts.map +0 -1
  60. package/dist/types/src/types/Script.d.ts +0 -21
  61. package/dist/types/src/types/Script.d.ts.map +0 -1
  62. package/dist/types/src/types/Trigger.d.ts +0 -122
  63. package/dist/types/src/types/Trigger.d.ts.map +0 -1
  64. package/dist/types/src/types/TriggerEvent.d.ts +0 -75
  65. package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
  66. package/src/errors.ts +0 -21
  67. package/src/example/fib.ts +0 -32
  68. package/src/example/forex-effect.ts +0 -40
  69. package/src/example/index.ts +0 -13
  70. package/src/example/reply.ts +0 -21
  71. package/src/example/sleep.ts +0 -24
  72. package/src/operation-compatibility.test.ts +0 -185
  73. package/src/services/event-logger.ts +0 -127
  74. package/src/types/Function.ts +0 -83
  75. package/src/types/Script.ts +0 -34
  76. package/src/types/Trigger.ts +0 -154
  77. package/src/types/TriggerEvent.ts +0 -62
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.d05673bc65",
3
+ "version": "0.8.4-main.dfabb4ec29",
4
4
  "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -26,23 +26,24 @@
26
26
  "src"
27
27
  ],
28
28
  "dependencies": {
29
+ "@effect-atom/atom": "^0.5.1",
29
30
  "@effect/ai-anthropic": "0.23.0",
30
31
  "@effect/platform": "0.94.4",
31
- "effect": "3.19.16",
32
- "@dxos/context": "0.8.4-main.d05673bc65",
33
- "@dxos/ai": "0.8.4-main.d05673bc65",
34
- "@dxos/echo": "0.8.4-main.d05673bc65",
35
- "@dxos/echo-db": "0.8.4-main.d05673bc65",
36
- "@dxos/effect": "0.8.4-main.d05673bc65",
37
- "@dxos/errors": "0.8.4-main.d05673bc65",
38
- "@dxos/invariant": "0.8.4-main.d05673bc65",
39
- "@dxos/log": "0.8.4-main.d05673bc65",
40
- "@dxos/node-std": "0.8.4-main.d05673bc65",
41
- "@dxos/protocols": "0.8.4-main.d05673bc65",
42
- "@dxos/types": "0.8.4-main.d05673bc65",
43
- "@dxos/operation": "0.8.4-main.d05673bc65",
44
- "@dxos/schema": "0.8.4-main.d05673bc65",
45
- "@dxos/keys": "0.8.4-main.d05673bc65"
32
+ "effect": "3.20.0",
33
+ "@dxos/ai": "0.8.4-main.dfabb4ec29",
34
+ "@dxos/compute": "0.8.4-main.dfabb4ec29",
35
+ "@dxos/echo-db": "0.8.4-main.dfabb4ec29",
36
+ "@dxos/effect": "0.8.4-main.dfabb4ec29",
37
+ "@dxos/echo": "0.8.4-main.dfabb4ec29",
38
+ "@dxos/invariant": "0.8.4-main.dfabb4ec29",
39
+ "@dxos/errors": "0.8.4-main.dfabb4ec29",
40
+ "@dxos/log": "0.8.4-main.dfabb4ec29",
41
+ "@dxos/keys": "0.8.4-main.dfabb4ec29",
42
+ "@dxos/node-std": "0.8.4-main.dfabb4ec29",
43
+ "@dxos/types": "0.8.4-main.dfabb4ec29",
44
+ "@dxos/context": "0.8.4-main.dfabb4ec29",
45
+ "@dxos/schema": "0.8.4-main.dfabb4ec29",
46
+ "@dxos/protocols": "0.8.4-main.dfabb4ec29"
46
47
  },
47
48
  "publishConfig": {
48
49
  "access": "public"
package/src/index.ts CHANGED
@@ -2,8 +2,6 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './errors';
6
- export * from './example';
7
5
  export * from './sdk';
8
6
  export * from './services';
9
7
  export * from './types';
@@ -0,0 +1,105 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as HttpClient from '@effect/platform/HttpClient';
6
+ import * as HttpClientRequest from '@effect/platform/HttpClientRequest';
7
+ import * as Cause from 'effect/Cause';
8
+ import * as Chunk from 'effect/Chunk';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Exit from 'effect/Exit';
11
+ import { describe, test } from 'vitest';
12
+
13
+ import { FunctionsAiMemoizationMissError, FunctionsAiUpstreamError } from '@dxos/compute';
14
+ import { runAndForwardErrors } from '@dxos/effect';
15
+ import { type EdgeFunctionEnv } from '@dxos/protocols';
16
+
17
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
18
+
19
+ const makeStubService = (response: Response): EdgeFunctionEnv.FunctionsAiService => ({
20
+ fetch: async () => response as any,
21
+ });
22
+
23
+ const runRequest = (service: EdgeFunctionEnv.FunctionsAiService) =>
24
+ HttpClient.HttpClient.pipe(
25
+ Effect.flatMap((client) =>
26
+ client.execute(HttpClientRequest.post('http://internal/provider/anthropic/v1/messages')),
27
+ ),
28
+ Effect.provide(FunctionsAiHttpClient.layer(service)),
29
+ );
30
+
31
+ const extractDefect = (exit: Exit.Exit<unknown, unknown>): Error | null => {
32
+ if (Exit.isSuccess(exit)) {
33
+ return null;
34
+ }
35
+ return (Chunk.toReadonlyArray(Cause.defects(exit.cause))[0] as Error | undefined) ?? null;
36
+ };
37
+
38
+ describe('FunctionsAiHttpClient', () => {
39
+ test('emits FunctionsAiMemoizationMissError when upstream returns the memoization_miss envelope', async ({
40
+ expect,
41
+ }) => {
42
+ const service = makeStubService(
43
+ new Response(
44
+ JSON.stringify({
45
+ type: 'error',
46
+ error: {
47
+ type: 'memoization_miss',
48
+ message: 'No memoized Anthropic conversation found for POST /v1/messages (key=abc).',
49
+ cacheKey: 'abc',
50
+ },
51
+ }),
52
+ { status: 500, headers: { 'content-type': 'application/json' } },
53
+ ),
54
+ );
55
+
56
+ const exit = await Effect.runPromiseExit(runRequest(service));
57
+ const error = extractDefect(exit);
58
+ expect(error).toBeInstanceOf(FunctionsAiMemoizationMissError);
59
+ expect(error?.name).toBe('FunctionsAiMemoizationMissError');
60
+ expect((error as any)?.context?.cacheKey).toBe('abc');
61
+ expect((error as any)?.context?.status).toBe(500);
62
+ });
63
+
64
+ test('emits FunctionsAiUpstreamError for generic JSON error envelopes', async ({ expect }) => {
65
+ const service = makeStubService(
66
+ new Response(
67
+ JSON.stringify({
68
+ type: 'error',
69
+ error: { type: 'overloaded_error', message: 'Server overloaded.' },
70
+ }),
71
+ { status: 529, headers: { 'content-type': 'application/json' } },
72
+ ),
73
+ );
74
+
75
+ const exit = await Effect.runPromiseExit(runRequest(service));
76
+ const error = extractDefect(exit);
77
+ expect(error).toBeInstanceOf(FunctionsAiUpstreamError);
78
+ expect((error as any)?.context?.type).toBe('overloaded_error');
79
+ expect((error as any)?.context?.status).toBe(529);
80
+ });
81
+
82
+ test('passes non-error responses through unchanged', async ({ expect }) => {
83
+ const service = makeStubService(
84
+ new Response(JSON.stringify({ ok: true }), {
85
+ status: 200,
86
+ headers: { 'content-type': 'application/json' },
87
+ }),
88
+ );
89
+
90
+ const result = await runAndForwardErrors(runRequest(service));
91
+ expect(result.status).toBe(200);
92
+ });
93
+
94
+ test('passes non-JSON 5xx responses through to the @effect/ai layer unchanged', async ({ expect }) => {
95
+ const service = makeStubService(
96
+ new Response('Internal Server Error', {
97
+ status: 500,
98
+ headers: { 'content-type': 'text/plain' },
99
+ }),
100
+ );
101
+
102
+ const result = await runAndForwardErrors(runRequest(service));
103
+ expect(result.status).toBe(500);
104
+ });
105
+ });
@@ -11,13 +11,40 @@ import * as FiberRef from 'effect/FiberRef';
11
11
  import * as Layer from 'effect/Layer';
12
12
  import * as Stream from 'effect/Stream';
13
13
 
14
+ import { FunctionsAiMemoizationMissError, FunctionsAiUpstreamError } from '@dxos/compute';
14
15
  import { log } from '@dxos/log';
15
16
  import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
17
+
16
18
  /**
17
19
  * Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
18
20
  */
19
21
  export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
20
22
 
23
+ /**
24
+ * Shape of the JSON error envelope emitted by the upstream AI gateway (and by the memoization
25
+ * layer that fronts it in test environments).
26
+ *
27
+ * @example
28
+ * ```json
29
+ * {
30
+ * "type": "error",
31
+ * "error": {
32
+ * "type": "memoization_miss",
33
+ * "message": "No memoized Anthropic conversation found for ...",
34
+ * "cacheKey": "114dae3db8fe60..."
35
+ * }
36
+ * }
37
+ * ```
38
+ */
39
+ type UpstreamErrorEnvelope = {
40
+ type?: string;
41
+ error?: {
42
+ type?: string;
43
+ message?: string;
44
+ cacheKey?: string;
45
+ };
46
+ };
47
+
21
48
  export class FunctionsAiHttpClient {
22
49
  static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
23
50
  HttpClient.make((request, url, signal, fiber) => {
@@ -47,7 +74,18 @@ export class FunctionsAiHttpClient {
47
74
  cause,
48
75
  });
49
76
  },
50
- }).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response)));
77
+ }).pipe(
78
+ Effect.flatMap((response) =>
79
+ // Inspect the body before handing the response to `@effect/ai` so that structured
80
+ // upstream errors surface as typed defects (`FunctionsAiUpstreamError` and friends)
81
+ // rather than as the generic `HttpResponseError` from `@effect/ai/AiError`.
82
+ Effect.flatMap(
83
+ Effect.promise(() => parseUpstreamError(response)),
84
+ (typedError) =>
85
+ typedError ? Effect.die(typedError) : Effect.succeed(HttpClientResponse.fromWeb(request, response)),
86
+ ),
87
+ ),
88
+ );
51
89
 
52
90
  switch (request.body._tag) {
53
91
  case 'Raw':
@@ -65,3 +103,39 @@ export class FunctionsAiHttpClient {
65
103
  static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
66
104
  Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
67
105
  }
106
+
107
+ /**
108
+ * Returns a typed error if the response is a non-2xx JSON payload matching
109
+ * {@link UpstreamErrorEnvelope}; otherwise returns `undefined` and the response is forwarded
110
+ * unchanged.
111
+ */
112
+ const parseUpstreamError = async (response: Response): Promise<Error | undefined> => {
113
+ if (response.ok) {
114
+ return undefined;
115
+ }
116
+ const contentType = response.headers.get('content-type') ?? '';
117
+ if (!contentType.toLowerCase().includes('application/json')) {
118
+ return undefined;
119
+ }
120
+ let body: UpstreamErrorEnvelope;
121
+ try {
122
+ body = (await response.clone().json()) as UpstreamErrorEnvelope;
123
+ } catch {
124
+ return undefined;
125
+ }
126
+ if (!body || body.type !== 'error' || typeof body.error !== 'object' || body.error === null) {
127
+ return undefined;
128
+ }
129
+ const inner = body.error;
130
+ const message = inner.message ?? `Upstream AI service responded with HTTP ${response.status}`;
131
+ if (inner.type === 'memoization_miss' && typeof inner.cacheKey === 'string') {
132
+ return new FunctionsAiMemoizationMissError({
133
+ message,
134
+ context: { cacheKey: inner.cacheKey, status: response.status },
135
+ });
136
+ }
137
+ return new FunctionsAiUpstreamError({
138
+ message,
139
+ context: { type: inner.type, status: response.status, ...(inner.cacheKey ? { cacheKey: inner.cacheKey } : {}) },
140
+ });
141
+ };
@@ -4,17 +4,16 @@
4
4
 
5
5
  import { describe, test } from 'vitest';
6
6
 
7
- import { FunctionError } from '../errors';
8
- import fibFunc from '../example/fib';
9
- import replyFunc from '../example/reply';
7
+ import { InvalidOperationInputError } from '@dxos/compute';
8
+ import { FibonacciHandler, ReplyHandler } from '@dxos/compute/testing';
10
9
 
11
10
  import { wrapFunctionHandler } from './protocol';
12
11
 
13
12
  describe('wrapFunctionHandler', () => {
14
13
  test('wraps reply function and executes handler', async ({ expect }) => {
15
- const wrapped = wrapFunctionHandler(replyFunc);
14
+ const wrapped = wrapFunctionHandler(ReplyHandler);
16
15
 
17
- expect(wrapped.meta.key).toBe('example.org/function/reply');
16
+ expect(wrapped.meta.key).toBe('org.example.function.reply');
18
17
  expect(wrapped.meta.name).toBe('Reply');
19
18
 
20
19
  const testData = { message: 'hello' };
@@ -29,9 +28,9 @@ describe('wrapFunctionHandler', () => {
29
28
  });
30
29
 
31
30
  test('wraps fibonacci function with valid input', async ({ expect }) => {
32
- const wrapped = wrapFunctionHandler(fibFunc);
31
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
33
32
 
34
- expect(wrapped.meta.key).toBe('example.org/function/fib');
33
+ expect(wrapped.meta.key).toBe('org.example.function.fib');
35
34
  expect(wrapped.meta.name).toBe('Fibonacci');
36
35
 
37
36
  const result = await wrapped.handler({
@@ -44,8 +43,8 @@ describe('wrapFunctionHandler', () => {
44
43
  expect(result).toEqual({ result: '55' });
45
44
  });
46
45
 
47
- test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
48
- const wrapped = wrapFunctionHandler(fibFunc);
46
+ test('throws InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
47
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
49
48
 
50
49
  await expect(
51
50
  wrapped.handler({
@@ -54,6 +53,6 @@ describe('wrapFunctionHandler', () => {
54
53
  services: {},
55
54
  },
56
55
  }),
57
- ).rejects.toThrow(FunctionError);
56
+ ).rejects.toThrow(InvalidOperationInputError);
58
57
  });
59
58
  });
@@ -5,48 +5,78 @@
5
5
  import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
6
6
  import * as Effect from 'effect/Effect';
7
7
  import * as Layer from 'effect/Layer';
8
+ import * as Option from 'effect/Option';
8
9
  import * as Schema from 'effect/Schema';
9
10
  import * as SchemaAST from 'effect/SchemaAST';
10
11
 
11
- import { AiModelResolver, AiService } from '@dxos/ai';
12
+ import { AiModelResolver, AiService, OpaqueToolkit } from '@dxos/ai';
12
13
  import { AnthropicResolver } from '@dxos/ai/resolvers';
14
+ import {
15
+ FunctionError,
16
+ InvalidOperationInputError,
17
+ InvalidOperationOutputError,
18
+ Operation,
19
+ OperationRegistry,
20
+ Trace,
21
+ } from '@dxos/compute';
13
22
  import { LifecycleState, Resource } from '@dxos/context';
14
23
  import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
24
+ import { createFeedServiceLayer, EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
15
25
  import { refFromEncodedReference } from '@dxos/echo/internal';
16
- import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
17
26
  import { runAndForwardErrors } from '@dxos/effect';
18
27
  import { assertState, failedInvariant, invariant } from '@dxos/invariant';
19
- import { PublicKey } from '@dxos/keys';
20
- import { type FunctionProtocol } from '@dxos/protocols';
28
+ import { PublicKey, type SpaceId } from '@dxos/keys';
29
+ import { log } from '@dxos/log';
30
+ import { EdgeFunctionEnv, ErrorCodec, type FunctionProtocol, type TraceProtocol } from '@dxos/protocols';
31
+
32
+ import { type FunctionServices } from '../sdk';
33
+ import {
34
+ configuredCredentialsLayer,
35
+ credentialsLayerFromDatabase,
36
+ FunctionInvocationService,
37
+ QueueService,
38
+ } from '../services';
39
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
21
40
 
22
- import { FunctionError } from '../errors';
23
- import { FunctionDefinition, type FunctionServices } from '../sdk';
24
- import { CredentialsService, FunctionInvocationService, QueueService, TracingService } from '../services';
41
+ export interface FunctionWrappingOptions {
42
+ /**
43
+ * Additional types to register with the database.
44
+ */
45
+ types?: Type.AnyEntity[];
25
46
 
26
- import { FunctionsAiHttpClient } from './functions-ai-http-client';
47
+ /**
48
+ * Toolkits to make available via the `OpaqueToolkitProvider`.
49
+ */
50
+ toolkits?: OpaqueToolkit.OpaqueToolkit[];
51
+ }
27
52
 
28
53
  /**
29
54
  * Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
30
55
  */
31
- export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.Func => {
32
- if (!FunctionDefinition.isFunction(func)) {
33
- throw new TypeError('Invalid function definition');
56
+ export const wrapFunctionHandler = (
57
+ func: Operation.WithHandler<Operation.Definition.Any>,
58
+ opts: FunctionWrappingOptions = {},
59
+ ): FunctionProtocol.Func => {
60
+ if (!Operation.isOperationWithHandler(func)) {
61
+ throw new TypeError('Expected operation with handler');
34
62
  }
35
63
 
64
+ const serviceTags = func.services.map((service) => service.key);
65
+
36
66
  return {
37
67
  meta: {
38
- key: func.key,
39
- name: func.name,
40
- description: func.description,
41
- inputSchema: JsonSchema.toJsonSchema(func.inputSchema),
42
- outputSchema: func.outputSchema === undefined ? undefined : JsonSchema.toJsonSchema(func.outputSchema),
43
- services: func.services,
68
+ key: func.meta.key,
69
+ name: func.meta.name,
70
+ description: func.meta.description,
71
+ inputSchema: JsonSchema.toJsonSchema(func.input),
72
+ outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
73
+ services: func.services.map((service) => service.key),
44
74
  },
45
75
  handler: async ({ data, context }) => {
46
76
  if (
47
- (func.services.includes(Database.Service.key) ||
48
- func.services.includes(QueueService.key) ||
49
- func.services.includes(Feed.Service.key)) &&
77
+ (serviceTags.includes(Database.Service.key) ||
78
+ serviceTags.includes(QueueService.key) ||
79
+ serviceTags.includes(Feed.FeedService.key)) &&
50
80
  (!context.services.dataService || !context.services.queryService)
51
81
  ) {
52
82
  throw new FunctionError({
@@ -56,31 +86,31 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
56
86
 
57
87
  // eslint-disable-next-line no-useless-catch
58
88
  try {
59
- if (!SchemaAST.isAnyKeyword(func.inputSchema.ast)) {
89
+ if (!SchemaAST.isAnyKeyword(func.input.ast)) {
60
90
  try {
61
- Schema.validateSync(func.inputSchema)(data);
62
- } catch (error) {
63
- throw new FunctionError({ message: 'Invalid input schema', cause: error });
91
+ Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
92
+ } catch (error: any) {
93
+ throw new InvalidOperationInputError({
94
+ message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
95
+ cause: error,
96
+ });
64
97
  }
65
98
  }
66
99
 
67
- await using funcContext = await new FunctionContext(context).open();
100
+ await using funcContext = await new FunctionContext(context, opts).open();
68
101
 
69
- if (func.types.length > 0) {
102
+ const types = [...(opts.types ?? []), ...(func.types ?? [])];
103
+ if (types.length > 0) {
70
104
  invariant(funcContext.db, 'Database is required for functions with types');
71
- await funcContext.db.graph.schemaRegistry.register(func.types as Type.AnyEntity[]);
105
+ await funcContext.db.graph.schemaRegistry.register(types as Type.AnyEntity[]);
72
106
  }
73
107
 
74
108
  const dataWithDecodedRefs =
75
- funcContext.db && !SchemaAST.isAnyKeyword(func.inputSchema.ast)
76
- ? decodeRefsFromSchema(func.inputSchema.ast, data, funcContext.db)
109
+ funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
110
+ ? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
77
111
  : data;
78
112
 
79
- let result = await func.handler({
80
- // TODO(dmaretskyi): Fix the types.
81
- context: context as any,
82
- data: dataWithDecodedRefs,
83
- });
113
+ let result: any = await func.handler(dataWithDecodedRefs);
84
114
 
85
115
  if (Effect.isEffect(result)) {
86
116
  result = await runAndForwardErrors(
@@ -91,8 +121,15 @@ export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.
91
121
  );
92
122
  }
93
123
 
94
- if (func.outputSchema && !SchemaAST.isAnyKeyword(func.outputSchema.ast)) {
95
- Schema.validateSync(func.outputSchema)(result);
124
+ if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
125
+ try {
126
+ Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
127
+ } catch (error: any) {
128
+ throw new InvalidOperationOutputError({
129
+ message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
130
+ cause: error,
131
+ });
132
+ }
96
133
  }
97
134
 
98
135
  return result;
@@ -112,10 +149,12 @@ class FunctionContext extends Resource {
112
149
  readonly client: EchoClient | undefined;
113
150
  db: EchoDatabaseImpl | undefined;
114
151
  queues: QueueFactory | undefined;
152
+ readonly opts: FunctionWrappingOptions;
115
153
 
116
- constructor(context: FunctionProtocol.Context) {
154
+ constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
117
155
  super();
118
156
  this.context = context;
157
+ this.opts = opts;
119
158
  if (context.services.dataService && context.services.queryService) {
120
159
  this.client = new EchoClient().connectToService({
121
160
  dataService: context.services.dataService,
@@ -155,33 +194,164 @@ class FunctionContext extends Resource {
155
194
  const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
156
195
  const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
157
196
  const credentials = dbLayer
158
- ? CredentialsService.layerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
159
- : CredentialsService.configuredLayer([]);
160
- const functionInvocationService = MockedFunctionInvocationService;
161
- const tracing = TracingService.layerNoop;
197
+ ? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
198
+ : configuredCredentialsLayer([]);
162
199
 
163
200
  const aiLayer = this.context.services.functionsAiService
164
- ? AiModelResolver.AiModelResolver.buildAiService.pipe(
165
- Layer.provide(
166
- AnthropicResolver.make().pipe(
167
- Layer.provide(
168
- AnthropicClient.layer({
169
- // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
170
- apiUrl: 'http://internal/provider/anthropic',
171
- }).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
172
- ),
173
- ),
174
- ),
175
- )
201
+ ? InternalAiServiceLayer(this.context.services.functionsAiService)
176
202
  : AiService.notAvailable;
177
203
 
178
- return Layer.mergeAll(dbLayer, queuesLayer, feedLayer, credentials, functionInvocationService, aiLayer, tracing);
204
+ const operationServiceLayer = this.context.services.functionsService
205
+ ? makeOperationServiceLayer(this.context.services.functionsService)
206
+ : unavailableOperationServiceLayer;
207
+
208
+ const operationRegistryLayer = this.context.services.functionsService
209
+ ? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
210
+ : emptyOperationRegistryLayer;
211
+
212
+ const traceWriterLayer = this.context.services.traceService
213
+ ? makeTraceWriterLayer(this.context.services.traceService)
214
+ : Trace.writerLayerNoop;
215
+
216
+ log('Creating function context layer', {
217
+ traceService: !!this.context.services.traceService,
218
+ functionsService: !!this.context.services.functionsService,
219
+ functionsAiService: !!this.context.services.functionsAiService,
220
+ spaceId: this.context.spaceId,
221
+ spaceRootUrl: this.context.spaceRootUrl,
222
+ toolkits: this.opts.toolkits?.length ?? 0,
223
+ types: this.opts.types?.length ?? 0,
224
+ });
225
+
226
+ return Layer.mergeAll(
227
+ dbLayer,
228
+ queuesLayer,
229
+ feedLayer,
230
+ credentials,
231
+ operationServiceLayer,
232
+ operationRegistryLayer,
233
+ aiLayer,
234
+ OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
235
+ traceWriterLayer,
236
+
237
+ // `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
238
+ // The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
239
+ // type union — handlers that yield it will die at invocation time.
240
+ FunctionInvocationService.layerNotAvailable,
241
+ );
179
242
  }
180
243
  }
181
244
 
182
- const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
183
- invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
184
- resolveFunction: () => Effect.die('Not implemented.'),
245
+ /**
246
+ * Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
247
+ * handlers can write trace events that are forwarded to the runtime's trace sink.
248
+ */
249
+ const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
250
+ Layer.succeed(Trace.TraceService, {
251
+ write: (eventType, payload) => {
252
+ log('Writing trace event', {
253
+ eventType: eventType.key,
254
+ });
255
+ traceService.write([
256
+ {
257
+ key: eventType.key,
258
+ isEphemeral: eventType.isEphemeral,
259
+ data: payload,
260
+ },
261
+ ]);
262
+ },
263
+ });
264
+
265
+ /**
266
+ * AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
267
+ */
268
+ const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
269
+ AiModelResolver.AiModelResolver.buildAiService.pipe(
270
+ Layer.provide(
271
+ AnthropicResolver.make().pipe(
272
+ Layer.provide(
273
+ AnthropicClient.layer({
274
+ // Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
275
+ apiUrl: 'http://internal/provider/anthropic',
276
+ }).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
277
+ ),
278
+ ),
279
+ ),
280
+ );
281
+
282
+ /**
283
+ * Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
284
+ * handlers can invoke other deployed operations remotely. The `deployedId` on the operation
285
+ * definition is used as the routing key.
286
+ */
287
+ const makeOperationServiceLayer = (
288
+ functionsService: EdgeFunctionEnv.FunctionsService,
289
+ ): Layer.Layer<Operation.Service> => {
290
+ const invokeRemote = async (
291
+ op: Operation.Definition.Any,
292
+ input: unknown,
293
+ options?: Operation.InvokeOptions,
294
+ ): Promise<{ data?: unknown; error?: Error }> => {
295
+ invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
296
+ const result = await functionsService.invoke(op.meta.deployedId, input, {
297
+ spaceId: options?.spaceId,
298
+ });
299
+ if (result._kind === 'success') {
300
+ return { data: result.data };
301
+ }
302
+ return { error: ErrorCodec.decode(result.error) };
303
+ };
304
+
305
+ return Layer.succeed(Operation.Service, {
306
+ invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
307
+ Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
308
+ Effect.orDie,
309
+ Effect.flatMap((outcome) =>
310
+ outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
311
+ ),
312
+ )) as Operation.OperationService['invoke'],
313
+ schedule: ((op: Operation.Definition.Any, input: unknown) =>
314
+ Effect.sync(() => {
315
+ invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
316
+ // Fire and forget — schedule is intentionally non-awaiting.
317
+ void functionsService.invoke(op.meta.deployedId, input).catch(() => {
318
+ // Swallow errors — schedule is observability-only.
319
+ });
320
+ })) as Operation.OperationService['schedule'],
321
+ invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
322
+ invokeRemote(op, input, options).catch((error: unknown) => ({
323
+ error: error instanceof Error ? error : new Error(String(error)),
324
+ }))) as Operation.OperationService['invokePromise'],
325
+ } satisfies Operation.OperationService);
326
+ };
327
+
328
+ const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
329
+ invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
330
+ schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
331
+ invokePromise: async () => ({
332
+ error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
333
+ }),
334
+ } as Operation.OperationService);
335
+
336
+ /**
337
+ * Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
338
+ * the first persistent operation matching the requested key, or `Option.none()` when not found.
339
+ */
340
+ const makeOperationRegistryLayer = (
341
+ functionsService: EdgeFunctionEnv.FunctionsService,
342
+ spaceId: SpaceId | undefined,
343
+ ): Layer.Layer<OperationRegistry.Service> =>
344
+ Layer.succeed(OperationRegistry.Service, {
345
+ resolve: (key: string) =>
346
+ Effect.gen(function* () {
347
+ const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
348
+ const match = (records as Operation.PersistentOperation[]).find((record) => record.key === key);
349
+ return match ? Option.some(Operation.deserialize(match)) : Option.none();
350
+ }),
351
+ });
352
+
353
+ const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
354
+ resolve: () => Effect.succeed(Option.none()),
185
355
  });
186
356
 
187
357
  const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {