@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/Scheduler.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import * as ReservedArgument from "./internal/reservedArgument.js";
1
2
  import type * as IdScope from "@confect/core/IdScope";
2
- import { Ref } from "@confect/core";
3
+ import type { Ref } from "@confect/core";
4
+ import { getFunctionReference } from "@confect/core/RefInternal";
3
5
  import type { GenericId } from "@confect/core/GenericId";
4
6
  import type { Scheduler as ConvexScheduler } from "convex/server";
5
7
  import type { GenericId as ConvexGenericId } from "convex/values";
@@ -12,38 +14,34 @@ import * as Layer from "effect/Layer";
12
14
  const make = (scheduler: ConvexScheduler) => ({
13
15
  cancel: (id: ConvexGenericId<"_scheduled_functions">) =>
14
16
  Effect.promise(() => scheduler.cancel(id)),
15
- runAfter: <Ref_ extends Ref.AnyMutation | Ref.AnyAction>(
16
- delay: Duration.Duration,
17
- ref: Ref_,
18
- ...args: Ref.OptionalArgs<Ref_>
19
- ) => {
17
+ runAfter: Effect.fn("Scheduler.runAfter")(function* <
18
+ Ref_ extends Ref.AnyMutation | Ref.AnyAction,
19
+ >(delay: Duration.Duration, ref: Ref_, ...args: Ref.OptionalArgs<Ref_>) {
20
20
  const delayMs = Duration.toMillis(delay);
21
- const functionReference = Ref.getFunctionReference(ref);
22
- const encodedArgs = Ref.encodeArgsSync(
21
+ const functionReference = getFunctionReference(ref);
22
+ const encodedArgs = yield* ReservedArgument.encodeArgs(
23
23
  ref,
24
24
  (args[0] ?? {}) as Ref.Args<Ref_>,
25
25
  );
26
26
 
27
- return Effect.promise(() =>
27
+ return yield* Effect.promise(() =>
28
28
  scheduler.runAfter(delayMs, functionReference, encodedArgs),
29
29
  );
30
- },
31
- runAt: <Ref_ extends Ref.AnyMutation | Ref.AnyAction>(
32
- dateTime: DateTime.DateTime,
33
- ref: Ref_,
34
- ...args: Ref.OptionalArgs<Ref_>
35
- ) => {
30
+ }),
31
+ runAt: Effect.fn("Scheduler.runAt")(function* <
32
+ Ref_ extends Ref.AnyMutation | Ref.AnyAction,
33
+ >(dateTime: DateTime.DateTime, ref: Ref_, ...args: Ref.OptionalArgs<Ref_>) {
36
34
  const timestamp = DateTime.toEpochMillis(dateTime);
37
- const functionReference = Ref.getFunctionReference(ref);
38
- const encodedArgs = Ref.encodeArgsSync(
35
+ const functionReference = getFunctionReference(ref);
36
+ const encodedArgs = yield* ReservedArgument.encodeArgs(
39
37
  ref,
40
38
  (args[0] ?? {}) as Ref.Args<Ref_>,
41
39
  );
42
40
 
43
- return Effect.promise(() =>
41
+ return yield* Effect.promise(() =>
44
42
  scheduler.runAt(timestamp, functionReference, encodedArgs),
45
43
  );
46
- },
44
+ }),
47
45
  });
48
46
 
49
47
  export const Scheduler = Context.Service<ReturnType<typeof make>>(
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * as ActionCall from "./ActionCall.js";
1
2
  export * as AiGatewayDecisionClient from "./AiGatewayDecisionClient.js";
2
3
  export * as AiGatewayDecisionModel from "./AiGatewayDecisionModel.js";
3
4
  export * as AiGatewayLanguageClient from "./AiGatewayLanguageClient.js";
@@ -28,6 +29,7 @@ export * as MiddlewareRegistryItem from "./MiddlewareRegistryItem.js";
28
29
  export * as MutationCtx from "./MutationCtx.js";
29
30
  export * as MutationRunner from "./MutationRunner.js";
30
31
  export * as MutationTransactionContext from "./MutationTransactionContext.js";
32
+ export * as OfficialComponent from "./OfficialComponent.js";
31
33
  export * as OrderedQuery from "./OrderedQuery.js";
32
34
  export * as QueryCtx from "./QueryCtx.js";
33
35
  export * as QueryInitializer from "./QueryInitializer.js";
@@ -0,0 +1,87 @@
1
+ import type { ReadonlyValue } from "@confect/core/SchemaToValidator";
2
+ import { pipe } from "effect/Function";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Record from "effect/Record";
5
+ import * as Schema from "effect/Schema";
6
+ import * as SchemaAST from "effect/SchemaAST";
7
+ import type * as Table from "../Table.js";
8
+
9
+ export type EncodedPatch = Record.ReadonlyRecord<
10
+ string,
11
+ ReadonlyValue | undefined
12
+ >;
13
+
14
+ export type PatchEncoder = (
15
+ patch: Record.ReadonlyRecord<string, unknown>,
16
+ ) => Effect.Effect<EncodedPatch, Schema.SchemaError>;
17
+
18
+ const patchEncoders = new WeakMap<Table.AnyWithProps, PatchEncoder | null>();
19
+
20
+ /**
21
+ * Returns the encoder for patches to the table's documents, or `undefined` when
22
+ * the table's codec does not encode each field independently. The encoder maps
23
+ * every patched field of the table to its encoded value, or to `undefined` when
24
+ * the patch unsets the field or the field's codec encodes the value as an
25
+ * absent key. Keys outside the table's fields are left out.
26
+ */
27
+ export const encoder = (
28
+ table: Table.AnyWithProps,
29
+ ): PatchEncoder | undefined => {
30
+ let cached = patchEncoders.get(table);
31
+ if (cached === undefined) {
32
+ cached = makePatchEncoder(table);
33
+ patchEncoders.set(table, cached);
34
+ }
35
+ return cached ?? undefined;
36
+ };
37
+
38
+ // A struct with no encoding of its own encodes each field through that field's
39
+ // codec alone, so a patch encodes field by field. A union or a transformation
40
+ // over the whole struct can make one field's encoding depend on another field.
41
+ // A struct-level check must see the decoded fields before they are encoded,
42
+ // and keys under an index signature have no field codec of their own.
43
+ const makePatchEncoder = (table: Table.AnyWithProps): PatchEncoder | null => {
44
+ const ast = table.Fields.ast;
45
+ if (
46
+ !SchemaAST.isObjects(ast) ||
47
+ ast.encoding !== undefined ||
48
+ ast.checks !== undefined ||
49
+ ast.indexSignatures.length > 0
50
+ ) {
51
+ return null;
52
+ }
53
+ // A field's codec runs only when the patch gives the field a value, since a
54
+ // codec can handle a missing key. Each comes from the table, whose codecs
55
+ // need no services.
56
+ const fieldEncoders: Record.ReadonlyRecord<
57
+ string,
58
+ (
59
+ value: unknown,
60
+ ) => Effect.Effect<ReadonlyValue | undefined, Schema.SchemaError>
61
+ > = Object.fromEntries(
62
+ ast.propertySignatures.map(({ name, type }) => {
63
+ const key = String(name);
64
+ const encode = Schema.encodeUnknownEffect(
65
+ Schema.Struct({
66
+ [key]: Schema.optionalKey(
67
+ Schema.make<Schema.Codec<unknown, ReadonlyValue>>(type),
68
+ ),
69
+ }),
70
+ );
71
+ return [
72
+ key,
73
+ (value: unknown) =>
74
+ Effect.map(encode({ [key]: value }), (encoded) => encoded[key]),
75
+ ];
76
+ }),
77
+ );
78
+ return (patch) =>
79
+ pipe(
80
+ fieldEncoders,
81
+ Record.filter((_, name) => Object.hasOwn(patch, name)),
82
+ Record.map((encode, name) =>
83
+ patch[name] === undefined ? Effect.undefined : encode(patch[name]),
84
+ ),
85
+ Effect.all,
86
+ );
87
+ };
@@ -0,0 +1,27 @@
1
+ import * as Duration from "effect/Duration";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Exit from "effect/Exit";
4
+ import * as Layer from "effect/Layer";
5
+ import * as Scope from "effect/Scope";
6
+ import * as ConvexConfigProvider from "../ConvexConfigProvider.js";
7
+
8
+ const memoMap = Layer.makeMemoMapUnsafe();
9
+ export const scope = Scope.makeUnsafe();
10
+ const layers = new WeakMap<Layer.Layer<never>, Layer.Layer<never>>();
11
+
12
+ export const memoized = (layer: Layer.Layer<never>): Layer.Layer<never> => {
13
+ const cached = layers.get(layer);
14
+ if (cached !== undefined) return cached;
15
+ const built = Layer.buildWithMemoMap(layer, memoMap, scope).pipe(
16
+ Effect.provide(ConvexConfigProvider.layer),
17
+ // A failed first build is retried on the next invocation instead of failing every later one.
18
+ Effect.cachedWithTTL((exit) =>
19
+ Exit.isSuccess(exit) ? Duration.infinity : Duration.zero,
20
+ ),
21
+ Effect.runSync,
22
+ Effect.uninterruptible,
23
+ Layer.effectContext,
24
+ );
25
+ layers.set(layer, built);
26
+ return built;
27
+ };
@@ -0,0 +1,56 @@
1
+ import { convexToJson, type Value } from "convex/values";
2
+ import * as Ref from "@confect/core/Ref";
3
+ import * as Context from "effect/Context";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Tracer from "effect/Tracer";
6
+
7
+ export const Forward = Context.Reference<
8
+ ((span: Tracer.Span) => unknown) | undefined
9
+ >("@confect/server/RegisteredFunction/Forward", {
10
+ defaultValue: () => undefined,
11
+ });
12
+
13
+ const inject = (
14
+ ref: Ref.Any,
15
+ encoded: unknown,
16
+ context: Context.Context<never>,
17
+ ): unknown => {
18
+ if (ref.reservedArgument === undefined || ref.functionType === "query")
19
+ return encoded;
20
+ const forward = Context.get(context, Forward);
21
+ const span = Context.getOrUndefined(context, Tracer.ParentSpan);
22
+ if (forward === undefined || span?._tag !== "Span") return encoded;
23
+ try {
24
+ const value = forward(span);
25
+ if (value === undefined) return encoded;
26
+ convexToJson(value as Value);
27
+ return {
28
+ ...(encoded as Record<string, unknown>),
29
+ [ref.reservedArgument]: value,
30
+ };
31
+ } catch {
32
+ return encoded;
33
+ }
34
+ };
35
+
36
+ export const runWithCodec = <Ref_ extends Ref.Any>(
37
+ ref: Ref_,
38
+ args: Ref.Args<Ref_>,
39
+ call: (
40
+ native: Ref.FunctionReference<Ref_>,
41
+ encoded: unknown,
42
+ ) => PromiseLike<unknown>,
43
+ ) =>
44
+ Effect.withFiber((fiber) =>
45
+ Ref.runWithCodec(ref, args, (native, encoded) =>
46
+ call(native, inject(ref, encoded, fiber.context)),
47
+ ),
48
+ );
49
+
50
+ export const encodeArgs = <Ref_ extends Ref.Any>(
51
+ ref: Ref_,
52
+ args: Ref.Args<Ref_>,
53
+ ) =>
54
+ Effect.withFiberSucceed((fiber) =>
55
+ inject(ref, Ref.encodeArgsSync(ref, args), fiber.context),
56
+ );