@gaganref/confect-server 10.3.1-fork.6 → 10.3.1-fork.60

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/ActionCall.d.ts +30 -0
  2. package/dist/ActionCall.d.ts.map +1 -0
  3. package/dist/ActionCall.js +41 -0
  4. package/dist/ActionCall.js.map +1 -0
  5. package/dist/ActionRunner.d.ts +1 -1
  6. package/dist/ActionRunner.d.ts.map +1 -1
  7. package/dist/ActionRunner.js +3 -3
  8. package/dist/ActionRunner.js.map +1 -1
  9. package/dist/AiGatewayDecisionClient.js +1 -1
  10. package/dist/AiGatewayLanguageClient.js +1 -1
  11. package/dist/Auth.d.ts +13 -1
  12. package/dist/Auth.d.ts.map +1 -1
  13. package/dist/Auth.js +13 -3
  14. package/dist/Auth.js.map +1 -1
  15. package/dist/Callback.d.ts +1 -1
  16. package/dist/Callback.d.ts.map +1 -1
  17. package/dist/Callback.js +6 -6
  18. package/dist/Callback.js.map +1 -1
  19. package/dist/ConvexConfigProvider.js +2 -2
  20. package/dist/ConvexFunctionDefinitions.d.ts.map +1 -1
  21. package/dist/ConvexFunctionDefinitions.js +4 -4
  22. package/dist/ConvexFunctionDefinitions.js.map +1 -1
  23. package/dist/ConvexLogger.js +1 -1
  24. package/dist/CronJobs.d.ts.map +1 -1
  25. package/dist/CronJobs.js +7 -6
  26. package/dist/CronJobs.js.map +1 -1
  27. package/dist/DatabaseSchema.js +2 -2
  28. package/dist/DatabaseWriter.d.ts +36 -2
  29. package/dist/DatabaseWriter.d.ts.map +1 -1
  30. package/dist/DatabaseWriter.js +55 -8
  31. package/dist/DatabaseWriter.js.map +1 -1
  32. package/dist/ExecutionMetadata.js +1 -1
  33. package/dist/FunctionImpl.js +2 -2
  34. package/dist/FunctionRegistryItem.d.ts +1 -0
  35. package/dist/FunctionRegistryItem.d.ts.map +1 -1
  36. package/dist/FunctionRegistryItem.js +2 -1
  37. package/dist/FunctionRegistryItem.js.map +1 -1
  38. package/dist/GroupImpl.d.ts +21 -2
  39. package/dist/GroupImpl.d.ts.map +1 -1
  40. package/dist/GroupImpl.js +21 -22
  41. package/dist/GroupImpl.js.map +1 -1
  42. package/dist/GroupRegistry.d.ts +1 -0
  43. package/dist/GroupRegistry.d.ts.map +1 -1
  44. package/dist/GroupRegistry.js +10 -3
  45. package/dist/GroupRegistry.js.map +1 -1
  46. package/dist/Handler.d.ts +8 -4
  47. package/dist/Handler.d.ts.map +1 -1
  48. package/dist/Handler.js.map +1 -1
  49. package/dist/HttpRouter.d.ts +2 -0
  50. package/dist/HttpRouter.d.ts.map +1 -1
  51. package/dist/HttpRouter.js +17 -8
  52. package/dist/HttpRouter.js.map +1 -1
  53. package/dist/MiddlewareImpl.js +1 -1
  54. package/dist/MutationRunner.d.ts +1 -1
  55. package/dist/MutationRunner.d.ts.map +1 -1
  56. package/dist/MutationRunner.js +3 -3
  57. package/dist/MutationRunner.js.map +1 -1
  58. package/dist/MutationTransactionContext.d.ts.map +1 -1
  59. package/dist/MutationTransactionContext.js +4 -3
  60. package/dist/MutationTransactionContext.js.map +1 -1
  61. package/dist/OfficialComponent.d.ts +60 -0
  62. package/dist/OfficialComponent.d.ts.map +1 -0
  63. package/dist/OfficialComponent.js +72 -0
  64. package/dist/OfficialComponent.js.map +1 -0
  65. package/dist/QueryInitializer.js +2 -2
  66. package/dist/QueryRunner.js +2 -2
  67. package/dist/QueryStream.d.ts +120 -12
  68. package/dist/QueryStream.d.ts.map +1 -1
  69. package/dist/QueryStream.js +143 -41
  70. package/dist/QueryStream.js.map +1 -1
  71. package/dist/QueryStreamCursor.js +1 -1
  72. package/dist/QueryStreamIndexPrefix.js +1 -1
  73. package/dist/QueryStreamIndexRange.js +2 -2
  74. package/dist/QueryStreamKey.js +1 -1
  75. package/dist/QueryStreamKeyBounds.js +2 -2
  76. package/dist/QueryStreamKeyLayout.js +2 -2
  77. package/dist/QueryStreamKeyValues.js +2 -2
  78. package/dist/QueryStreamPagination.js +1 -1
  79. package/dist/QueryStreamReadBudget.d.ts +3 -2
  80. package/dist/QueryStreamReadBudget.d.ts.map +1 -1
  81. package/dist/QueryStreamReadBudget.js +8 -5
  82. package/dist/QueryStreamReadBudget.js.map +1 -1
  83. package/dist/QueryTransactionContext.js +2 -2
  84. package/dist/RegisteredConvexFunction.d.ts +10 -5
  85. package/dist/RegisteredConvexFunction.d.ts.map +1 -1
  86. package/dist/RegisteredConvexFunction.js +45 -29
  87. package/dist/RegisteredConvexFunction.js.map +1 -1
  88. package/dist/RegisteredFunction.d.ts +39 -22
  89. package/dist/RegisteredFunction.d.ts.map +1 -1
  90. package/dist/RegisteredFunction.js +155 -42
  91. package/dist/RegisteredFunction.js.map +1 -1
  92. package/dist/RegisteredNodeFunction.d.ts +1 -1
  93. package/dist/RegisteredNodeFunction.d.ts.map +1 -1
  94. package/dist/RegisteredNodeFunction.js +10 -6
  95. package/dist/RegisteredNodeFunction.js.map +1 -1
  96. package/dist/RegistryItems.d.ts +7 -0
  97. package/dist/RegistryItems.d.ts.map +1 -1
  98. package/dist/RegistryItems.js +8 -3
  99. package/dist/RegistryItems.js.map +1 -1
  100. package/dist/RequestMetadata.js +1 -1
  101. package/dist/Scheduler.d.ts +1 -1
  102. package/dist/Scheduler.d.ts.map +1 -1
  103. package/dist/Scheduler.js +14 -13
  104. package/dist/Scheduler.js.map +1 -1
  105. package/dist/Storage.js +1 -1
  106. package/dist/StorageActionWriter.js +1 -1
  107. package/dist/StorageReader.js +1 -1
  108. package/dist/StorageWriter.js +1 -1
  109. package/dist/TransactionMetadata.js +1 -1
  110. package/dist/VectorSearch.js +1 -1
  111. package/dist/index.d.ts +2 -0
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +20 -18
  114. package/dist/internal/AiGatewayDecisionClient.js +1 -1
  115. package/dist/internal/AiGatewayLanguageClient.js +1 -1
  116. package/dist/internal/AiGatewayServiceToken.js +1 -1
  117. package/dist/internal/DocumentPatch.d.ts +16 -0
  118. package/dist/internal/DocumentPatch.d.ts.map +1 -0
  119. package/dist/internal/DocumentPatch.js +36 -0
  120. package/dist/internal/DocumentPatch.js.map +1 -0
  121. package/dist/internal/observability.d.ts +5 -0
  122. package/dist/internal/observability.d.ts.map +1 -0
  123. package/dist/internal/observability.js +21 -0
  124. package/dist/internal/observability.js.map +1 -0
  125. package/dist/internal/reservedArgument.d.ts +8 -0
  126. package/dist/internal/reservedArgument.d.ts.map +1 -0
  127. package/dist/internal/reservedArgument.js +30 -0
  128. package/dist/internal/reservedArgument.js.map +1 -0
  129. package/package.json +5 -5
  130. package/src/ActionCall.ts +83 -0
  131. package/src/ActionRunner.ts +3 -2
  132. package/src/Auth.ts +23 -1
  133. package/src/Callback.ts +3 -2
  134. package/src/ConvexFunctionDefinitions.ts +2 -1
  135. package/src/CronJobs.ts +2 -1
  136. package/src/DatabaseWriter.ts +183 -25
  137. package/src/FunctionRegistryItem.ts +4 -0
  138. package/src/GroupImpl.ts +61 -8
  139. package/src/GroupRegistry.ts +7 -3
  140. package/src/Handler.ts +21 -5
  141. package/src/HttpRouter.ts +54 -4
  142. package/src/MutationRunner.ts +3 -2
  143. package/src/MutationTransactionContext.ts +2 -1
  144. package/src/OfficialComponent.ts +168 -0
  145. package/src/QueryStream.ts +391 -61
  146. package/src/QueryStreamReadBudget.ts +19 -6
  147. package/src/RegisteredConvexFunction.ts +35 -2
  148. package/src/RegisteredFunction.ts +251 -45
  149. package/src/RegisteredNodeFunction.ts +11 -1
  150. package/src/RegistryItems.ts +31 -0
  151. package/src/Scheduler.ts +17 -19
  152. package/src/index.ts +2 -0
  153. package/src/internal/DocumentPatch.ts +87 -0
  154. package/src/internal/observability.ts +27 -0
  155. package/src/internal/reservedArgument.ts +56 -0
package/src/HttpRouter.ts CHANGED
@@ -24,6 +24,8 @@ import type * as HttpApiGroup from "effect/http-api/HttpApiGroup";
24
24
  import type * as Etag from "effect/http/Etag";
25
25
  import type * as HttpPlatform from "effect/http/HttpPlatform";
26
26
  import * as HttpRouter from "effect/http/HttpRouter";
27
+ import * as HttpMiddleware from "effect/http/HttpMiddleware";
28
+ import type * as HttpServerRequest from "effect/http/HttpServerRequest";
27
29
  import * as HttpServer from "effect/http/HttpServer";
28
30
  import type * as ActionRunner from "./ActionRunner.js";
29
31
  import type * as Auth from "./Auth.js";
@@ -33,6 +35,7 @@ import type * as DatabaseSchema from "./DatabaseSchema.js";
33
35
  import type * as DataModel from "./DataModel.js";
34
36
  import type * as Handler from "./Handler.js";
35
37
  import * as ConvexConfigProvider from "./ConvexConfigProvider.js";
38
+ import * as ObservabilityRuntime from "./internal/observability.js";
36
39
  import * as ConvexLogger from "./ConvexLogger.js";
37
40
  import type * as MutationRunner from "./MutationRunner.js";
38
41
  import type * as QueryRunner from "./QueryRunner.js";
@@ -116,9 +119,17 @@ export type Routes<Services_ = Services> = Layer.Layer<
116
119
  */
117
120
  export const make = <Id extends string, Groups extends HttpApiGroup.Constraint>(
118
121
  routes: Routes,
119
- options?: { readonly api?: HttpApi.HttpApi<Id, Groups> | undefined },
122
+ options?: {
123
+ readonly observability?: Layer.Layer<never> | undefined;
124
+ readonly api?: HttpApi.HttpApi<Id, Groups> | undefined;
125
+ },
120
126
  ): ConvexHttpRouter =>
121
- makeWithLayer(routes, RegisteredFunction.baseActionLayer, options?.api);
127
+ makeWithLayer(
128
+ routes,
129
+ RegisteredFunction.baseActionLayer,
130
+ options?.api,
131
+ options?.observability,
132
+ );
122
133
 
123
134
  /**
124
135
  * Derive HTTP capabilities and ID scopes from the generated database schema.
@@ -131,7 +142,10 @@ export const forSchema =
131
142
  ) =>
132
143
  <Id extends string, Groups extends HttpApiGroup.Constraint>(
133
144
  routes: Routes<Handler.ActionServices<DatabaseSchema_>>,
134
- options?: { readonly api?: HttpApi.HttpApi<Id, Groups> | undefined },
145
+ options?: {
146
+ readonly observability?: Layer.Layer<never> | undefined;
147
+ readonly api?: HttpApi.HttpApi<Id, Groups> | undefined;
148
+ },
135
149
  ): ConvexHttpRouter =>
136
150
  makeWithLayer<
137
151
  Handler.ActionServices<DatabaseSchema_>,
@@ -148,6 +162,8 @@ export const forSchema =
148
162
  ),
149
163
  (ctx) => RegisteredFunction.actionLayer(schema, ctx),
150
164
  options?.api,
165
+ options?.observability,
166
+ schema.target.kind === "component",
151
167
  );
152
168
 
153
169
  // Convex uses a component-relative path to select its HTTP action but passes
@@ -175,15 +191,38 @@ const makeWithLayer = <
175
191
  routes: Routes<Services_>,
176
192
  layer: (ctx: GenericActionCtx<DataModel_>) => Layer.Layer<Services_>,
177
193
  api: HttpApi.HttpApi<Id, Groups> | undefined,
194
+ observability?: Layer.Layer<never>,
195
+ siteRelative = false,
178
196
  ): ConvexHttpRouter => {
179
197
  applyMonkeyPatches();
180
198
 
199
+ const spanName = Layer.effect(
200
+ HttpMiddleware.SpanNameGenerator,
201
+ Effect.gen(function* () {
202
+ const prefix = siteRelative
203
+ ? (yield* Config.schema(
204
+ Schema.URLFromString,
205
+ "CONVEX_SITE_URL",
206
+ )).pathname.replace(/\/$/, "")
207
+ : "";
208
+ return (request: HttpServerRequest.HttpServerRequest) =>
209
+ convexHttpRouter.lookup(
210
+ new URL(request.originalUrl).pathname.slice(prefix.length) || "/",
211
+ request.method as (typeof ROUTABLE_HTTP_METHODS)[number],
212
+ )?.[2] ?? "/*";
213
+ }).pipe(Effect.orDie),
214
+ );
215
+
181
216
  // Provided (not just merged) so that route layers' own construction—e.g.
182
217
  // a `Layer.unwrap` reading `Config`—resolves configuration through the
183
218
  // Convex-aware provider; merged so that request fibers—endpoint handlers
184
219
  // and middleware—do too.
185
220
  const AppLayer = routes.pipe(
186
- Layer.provideMerge(ConvexLogger.layer),
221
+ Layer.provideMerge(
222
+ observability === undefined
223
+ ? ConvexLogger.layer
224
+ : Layer.merge(ObservabilityRuntime.memoized(observability), spanName),
225
+ ),
187
226
  Layer.provideMerge(ConvexConfigProvider.layer),
188
227
  Layer.provide(HttpServer.layerServices),
189
228
  );
@@ -193,6 +232,17 @@ const makeWithLayer = <
193
232
  // exposes as a per-request context argument.
194
233
  const { handler } = HttpRouter.toWebHandler(AppLayer, {
195
234
  disableLogger: true,
235
+ /* oxlint-disable effecttsgo/any-unknown-in-error-context -- Keep Effect's erased HTTP boundary errors intact. */
236
+ ...(observability === undefined
237
+ ? {}
238
+ : {
239
+ middleware: (effect) =>
240
+ Effect.andThen(
241
+ Effect.annotateCurrentSpan("kind", "httpAction"),
242
+ effect,
243
+ ),
244
+ }),
245
+ /* oxlint-enable effecttsgo/any-unknown-in-error-context */
196
246
  });
197
247
 
198
248
  const httpAction = httpActionGeneric((ctx, request): Promise<Response> => {
@@ -1,4 +1,5 @@
1
- import * as Ref from "@confect/core/Ref";
1
+ import * as ReservedArgument from "./internal/reservedArgument.js";
2
+ import type * as Ref from "@confect/core/Ref";
2
3
  import type { GenericActionCtx } from "convex/server";
3
4
  import * as Context from "effect/Context";
4
5
  import * as Effect from "effect/Effect";
@@ -46,7 +47,7 @@ const make = (nativeRunMutation: GenericActionCtx<any>["runMutation"]) => {
46
47
  yield* MutationTransactionContext.MutationTransactionContext;
47
48
  return yield* controls.runMutation(mutation, actualArgs, options);
48
49
  }
49
- return yield* Ref.runWithCodec(
50
+ return yield* ReservedArgument.runWithCodec(
50
51
  mutation,
51
52
  actualArgs,
52
53
  (ref, encodedArgs) => nativeRunMutation(ref, encodedArgs),
@@ -1,3 +1,4 @@
1
+ import * as ReservedArgument from "./internal/reservedArgument.js";
1
2
  import * as Ref from "@confect/core/Ref";
2
3
  import type {
3
4
  AdvancedRunQueryOptions,
@@ -32,7 +33,7 @@ const make = (
32
33
  args: Ref.Args<Mutation>,
33
34
  options: MutationOptions,
34
35
  ) =>
35
- Ref.runWithCodec(mutation, args, (ref, encodedArgs) =>
36
+ ReservedArgument.runWithCodec(mutation, args, (ref, encodedArgs) =>
36
37
  ctx.runMutation(ref, encodedArgs, options),
37
38
  ),
38
39
  ),
@@ -0,0 +1,168 @@
1
+ import * as Ref from "@confect/core/Ref";
2
+ import { getFunctionReference } from "@confect/core/RefInternal";
3
+ import type {
4
+ DefaultFunctionArgs,
5
+ GenericActionCtx,
6
+ GenericDataModel,
7
+ GenericMutationCtx,
8
+ GenericQueryCtx,
9
+ } from "convex/server";
10
+ import * as Effect from "effect/Effect";
11
+ import * as Option from "effect/Option";
12
+ import * as Schema from "effect/Schema";
13
+ import * as ActionCtx from "./ActionCtx.js";
14
+ import type * as ActionRunner from "./ActionRunner.js";
15
+ import * as MutationCtx from "./MutationCtx.js";
16
+ import type * as MutationRunner from "./MutationRunner.js";
17
+ import * as QueryCtx from "./QueryCtx.js";
18
+ import type * as QueryRunner from "./QueryRunner.js";
19
+ import * as ReservedArgument from "./internal/reservedArgument.js";
20
+
21
+ /**
22
+ * An official component's method rejected. `cause` is the rejection as thrown.
23
+ */
24
+ export class ComponentCallFailed extends Schema.TaggedError<ComponentCallFailed>()(
25
+ "ComponentCallFailed",
26
+ { component: Schema.String, method: Schema.String, cause: Schema.Defect() },
27
+ ) {}
28
+
29
+ declare const unsupported: unique symbol;
30
+ type Unsupported = typeof unsupported;
31
+ type Needs<Param> = [GenericActionCtx<GenericDataModel>] extends [Param]
32
+ ? [GenericMutationCtx<GenericDataModel>] extends [Param]
33
+ ? [GenericQueryCtx<GenericDataModel>] extends [Param]
34
+ ? QueryRunner.QueryRunner
35
+ : MutationRunner.MutationRunner
36
+ : [GenericQueryCtx<GenericDataModel>] extends [Param]
37
+ ? Unsupported
38
+ : ActionRunner.ActionRunner
39
+ : Unsupported;
40
+
41
+ type Method<Official> = {
42
+ [K in keyof Official & string]: Official[K] extends (
43
+ ctx: infer Ctx,
44
+ ...args: infer _Args
45
+ ) => Promise<unknown>
46
+ ? Needs<Ctx> extends Unsupported
47
+ ? never
48
+ : K
49
+ : never;
50
+ }[keyof Official & string];
51
+
52
+ type Call<F> = F extends (
53
+ ctx: infer Ctx,
54
+ ...args: infer Args
55
+ ) => Promise<infer A>
56
+ ? (...args: Args) => Effect.Effect<A, never, Needs<Ctx>>
57
+ : never;
58
+
59
+ type Lifted<Official> = <M extends Method<Official>>(
60
+ method: M,
61
+ ...args: Parameters<Call<Official[M]>>
62
+ ) => ReturnType<Call<Official[M]>>;
63
+
64
+ /**
65
+ * Lift official promise methods, deriving each capability from its ctx
66
+ * parameter. A rejection is caller misuse, a limit or a platform fault rather
67
+ * than a domain fact, so it dies.
68
+ */
69
+ export const make = <Official extends object>(
70
+ component: string,
71
+ official: Official,
72
+ ): Lifted<Official> => {
73
+ const context = Effect.gen(function* () {
74
+ const action = yield* Effect.serviceOption(
75
+ ActionCtx.ActionCtx<GenericDataModel>(),
76
+ );
77
+ if (Option.isSome(action)) return action.value;
78
+ const mutation = yield* Effect.serviceOption(
79
+ MutationCtx.MutationCtx<GenericDataModel>(),
80
+ );
81
+ if (Option.isSome(mutation)) return mutation.value;
82
+ const query = yield* Effect.serviceOption(
83
+ QueryCtx.QueryCtx<GenericDataModel>(),
84
+ );
85
+ if (Option.isSome(query)) return query.value;
86
+ return yield* Effect.die(
87
+ new Error(`${component} called outside a Confect handler`),
88
+ );
89
+ });
90
+ const call = Effect.fn(`${component}.call`)(function* (
91
+ method: Method<Official>,
92
+ ...args: unknown[]
93
+ ) {
94
+ const ctx = yield* context;
95
+ // Method selects promise methods whose first argument accepts a native handler context.
96
+ const invoke = official[method] as (
97
+ native: typeof ctx,
98
+ ...args: unknown[]
99
+ ) => Promise<unknown>;
100
+ return yield* Effect.tryPromise({
101
+ try: () => invoke.call(official, ctx, ...args),
102
+ catch: (cause) => ComponentCallFailed.make({ component, method, cause }),
103
+ }).pipe(Effect.orDie);
104
+ });
105
+ // Confect installs the native ctx with the runner capability inferred by Needs.
106
+ return call as unknown as Lifted<Official>;
107
+ };
108
+
109
+ /**
110
+ * A function reference with its encoded arguments, ready for an official method
111
+ * that takes both.
112
+ */
113
+ export interface Fn<R extends Ref.Any> {
114
+ readonly reference: Ref.FunctionReference<R>;
115
+ readonly args: DefaultFunctionArgs;
116
+ }
117
+
118
+ /**
119
+ * Choose whether an official component call forwards the caller's carrier.
120
+ */
121
+ export interface EncodeCallOptions {
122
+ /**
123
+ * Forward the caller's carrier to reserving mutations and actions. Use `none`
124
+ * when the component replays or compares the stored arguments.
125
+ */
126
+ readonly carrier: "forward" | "none";
127
+ }
128
+
129
+ /**
130
+ * Encode a ref's arguments through its codecs to the wire record an official
131
+ * method expects, with an explicit carrier policy.
132
+ */
133
+ export const encodeCall = <R extends Ref.Any>(
134
+ ref: R,
135
+ args: NoInfer<Ref.Args<R>>,
136
+ options: EncodeCallOptions,
137
+ ): Effect.Effect<Fn<R>> =>
138
+ (options.carrier === "forward"
139
+ ? ReservedArgument.encodeArgs(ref, args)
140
+ : Ref.encodeArgs(ref, args)
141
+ ).pipe(
142
+ Effect.flatMap(
143
+ Schema.decodeUnknownEffect(Schema.Record(Schema.String, Schema.Unknown)),
144
+ ),
145
+ Effect.map((encoded) => ({
146
+ reference: getFunctionReference(ref),
147
+ args: encoded,
148
+ })),
149
+ Effect.orDie,
150
+ );
151
+
152
+ /**
153
+ * Encode a callback's real field without fabricating the rest of its arguments.
154
+ */
155
+ export const encodeField = <
156
+ R extends Ref.AnyConfect,
157
+ K extends keyof Ref.Args<R> & keyof Ref.ArgsFields<R>,
158
+ >(
159
+ ref: R,
160
+ key: K,
161
+ value: NoInfer<Ref.Args<R>[K]>,
162
+ ): Effect.Effect<unknown> => {
163
+ const fields = ref.args.fields as Record<
164
+ keyof Ref.ArgsFields<R>,
165
+ Schema.Codec<unknown, unknown>
166
+ >;
167
+ return Schema.encodeUnknownEffect(fields[key])(value).pipe(Effect.orDie);
168
+ };