@vllnt/convex-progression 0.1.0-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/LICENSE +21 -0
  3. package/README.md +144 -0
  4. package/dist/client/index.d.ts +61 -0
  5. package/dist/client/index.d.ts.map +1 -0
  6. package/dist/client/index.js +64 -0
  7. package/dist/client/index.js.map +1 -0
  8. package/dist/client/types.d.ts +20 -0
  9. package/dist/client/types.d.ts.map +1 -0
  10. package/dist/client/types.js +3 -0
  11. package/dist/client/types.js.map +1 -0
  12. package/dist/component/_generated/api.d.ts +40 -0
  13. package/dist/component/_generated/api.d.ts.map +1 -0
  14. package/dist/component/_generated/api.js +31 -0
  15. package/dist/component/_generated/api.js.map +1 -0
  16. package/dist/component/_generated/component.d.ts +82 -0
  17. package/dist/component/_generated/component.d.ts.map +1 -0
  18. package/dist/component/_generated/component.js +11 -0
  19. package/dist/component/_generated/component.js.map +1 -0
  20. package/dist/component/_generated/dataModel.d.ts +46 -0
  21. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  22. package/dist/component/_generated/dataModel.js +11 -0
  23. package/dist/component/_generated/dataModel.js.map +1 -0
  24. package/dist/component/_generated/server.d.ts +133 -0
  25. package/dist/component/_generated/server.d.ts.map +1 -0
  26. package/dist/component/_generated/server.js +80 -0
  27. package/dist/component/_generated/server.js.map +1 -0
  28. package/dist/component/convex.config.d.ts +3 -0
  29. package/dist/component/convex.config.d.ts.map +1 -0
  30. package/dist/component/convex.config.js +4 -0
  31. package/dist/component/convex.config.js.map +1 -0
  32. package/dist/component/mutations.d.ts +43 -0
  33. package/dist/component/mutations.d.ts.map +1 -0
  34. package/dist/component/mutations.js +179 -0
  35. package/dist/component/mutations.js.map +1 -0
  36. package/dist/component/queries.d.ts +14 -0
  37. package/dist/component/queries.d.ts.map +1 -0
  38. package/dist/component/queries.js +45 -0
  39. package/dist/component/queries.js.map +1 -0
  40. package/dist/component/schema.d.ts +32 -0
  41. package/dist/component/schema.d.ts.map +1 -0
  42. package/dist/component/schema.js +21 -0
  43. package/dist/component/schema.js.map +1 -0
  44. package/dist/component/validation.d.ts +4 -0
  45. package/dist/component/validation.d.ts.map +1 -0
  46. package/dist/component/validation.js +22 -0
  47. package/dist/component/validation.js.map +1 -0
  48. package/dist/component/validators.d.ts +52 -0
  49. package/dist/component/validators.d.ts.map +1 -0
  50. package/dist/component/validators.js +20 -0
  51. package/dist/component/validators.js.map +1 -0
  52. package/dist/shared.d.ts +15 -0
  53. package/dist/shared.d.ts.map +1 -0
  54. package/dist/shared.js +36 -0
  55. package/dist/shared.js.map +1 -0
  56. package/docs/API.md +69 -0
  57. package/llms-full.txt +930 -0
  58. package/llms.txt +35 -0
  59. package/package.json +92 -0
  60. package/src/client/index.ts +191 -0
  61. package/src/client/types.ts +23 -0
  62. package/src/component/_generated/api.ts +56 -0
  63. package/src/component/_generated/component.ts +107 -0
  64. package/src/component/_generated/dataModel.ts +60 -0
  65. package/src/component/_generated/server.ts +169 -0
  66. package/src/component/convex.config.ts +5 -0
  67. package/src/component/mutations.ts +205 -0
  68. package/src/component/queries.ts +50 -0
  69. package/src/component/schema.ts +21 -0
  70. package/src/component/validation.ts +30 -0
  71. package/src/component/validators.ts +23 -0
  72. package/src/shared.ts +47 -0
  73. package/src/test.ts +15 -0
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Generated utilities for implementing server-side Convex query and mutation functions.
3
+ *
4
+ * THIS CODE IS AUTOMATICALLY GENERATED.
5
+ *
6
+ * To regenerate, run `npx convex dev`.
7
+ * @module
8
+ */
9
+ import type { ActionBuilder, HttpActionBuilder, MutationBuilder, QueryBuilder, GenericActionCtx, GenericMutationCtx, GenericQueryCtx, GenericDatabaseReader, GenericDatabaseWriter } from "convex/server";
10
+ import type { DataModel } from "./dataModel.js";
11
+ /**
12
+ * Typesafe environment variables.
13
+ *
14
+ * This includes platform-provided env vars and any variables declared in
15
+ * `convex.config.ts`.
16
+ */
17
+ type Env = {
18
+ readonly CONVEX_CLOUD_URL: string;
19
+ readonly CONVEX_SITE_URL: string;
20
+ };
21
+ /**
22
+ * Define a query in this Convex app's public API.
23
+ *
24
+ * This function will be allowed to read your Convex database and will be accessible from the client.
25
+ *
26
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
27
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
28
+ */
29
+ export declare const query: QueryBuilder<DataModel, "public">;
30
+ /**
31
+ * Define a query that is only accessible from other Convex functions (but not from the client).
32
+ *
33
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
34
+ *
35
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
36
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
37
+ */
38
+ export declare const internalQuery: QueryBuilder<DataModel, "internal">;
39
+ /**
40
+ * Define a mutation in this Convex app's public API.
41
+ *
42
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
43
+ *
44
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
45
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
46
+ */
47
+ export declare const mutation: MutationBuilder<DataModel, "public">;
48
+ /**
49
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
50
+ *
51
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
52
+ *
53
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
54
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
55
+ */
56
+ export declare const internalMutation: MutationBuilder<DataModel, "internal">;
57
+ /**
58
+ * Define an action in this Convex app's public API.
59
+ *
60
+ * An action is a function which can execute any JavaScript code, including non-deterministic
61
+ * code and code with side-effects, like calling third-party services.
62
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
63
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
64
+ *
65
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
66
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
67
+ */
68
+ export declare const action: ActionBuilder<DataModel, "public">;
69
+ /**
70
+ * Define an action that is only accessible from other Convex functions (but not from the client).
71
+ *
72
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
73
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
74
+ */
75
+ export declare const internalAction: ActionBuilder<DataModel, "internal">;
76
+ /**
77
+ * Define an HTTP action.
78
+ *
79
+ * The wrapped function will be used to respond to HTTP requests received
80
+ * by a Convex deployment if the requests matches the path and method where
81
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
82
+ *
83
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
84
+ * and a Fetch API `Request` object as its second.
85
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
86
+ */
87
+ export declare const httpAction: HttpActionBuilder;
88
+ export declare const env: Env;
89
+ /**
90
+ * A set of services for use within Convex query functions.
91
+ *
92
+ * The query context is passed as the first argument to any Convex query
93
+ * function run on the server.
94
+ *
95
+ * If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
96
+ */
97
+ export type QueryCtx = GenericQueryCtx<DataModel>;
98
+ /**
99
+ * A set of services for use within Convex mutation functions.
100
+ *
101
+ * The mutation context is passed as the first argument to any Convex mutation
102
+ * function run on the server.
103
+ *
104
+ * If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
105
+ */
106
+ export type MutationCtx = GenericMutationCtx<DataModel>;
107
+ /**
108
+ * A set of services for use within Convex action functions.
109
+ *
110
+ * The action context is passed as the first argument to any Convex action
111
+ * function run on the server.
112
+ */
113
+ export type ActionCtx = GenericActionCtx<DataModel>;
114
+ /**
115
+ * An interface to read from the database within Convex query functions.
116
+ *
117
+ * The two entry points are {@link DatabaseReader.get}, which fetches a single
118
+ * document by its {@link Id}, or {@link DatabaseReader.query}, which starts
119
+ * building a query.
120
+ */
121
+ export type DatabaseReader = GenericDatabaseReader<DataModel>;
122
+ /**
123
+ * An interface to read from and write to the database within Convex mutation
124
+ * functions.
125
+ *
126
+ * Convex guarantees that all writes within a single mutation are
127
+ * executed atomically, so you never have to worry about partial writes leaving
128
+ * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
129
+ * for the guarantees Convex provides your functions.
130
+ */
131
+ export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
132
+ export {};
133
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/component/_generated/server.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,eAAe,CAAC;AAUvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;GAKG;AACH,KAAK,GAAG,GAAG;IACT,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,EAAE,YAAY,CAAC,SAAS,EAAE,QAAQ,CAAgB,CAAC;AAErE;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,EAAE,YAAY,CAAC,SAAS,EAAE,UAAU,CACxC,CAAC;AAEvB;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,EAAE,eAAe,CAAC,SAAS,EAAE,QAAQ,CAAmB,CAAC;AAE9E;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,eAAe,CAAC,SAAS,EAAE,UAAU,CAC3C,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM,EAAE,aAAa,CAAC,SAAS,EAAE,QAAQ,CAAiB,CAAC;AAExE;;;;;GAKG;AACH,eAAO,MAAM,cAAc,EAAE,aAAa,CAAC,SAAS,EAAE,UAAU,CACzC,CAAC;AAExB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,iBAAqC,CAAC;AAC/D,eAAO,MAAM,GAAG,EAAE,GACJ,CAAC;AAEf;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAExD;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC"}
@@ -0,0 +1,80 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated utilities for implementing server-side Convex query and mutation functions.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+ import { actionGeneric, httpActionGeneric, queryGeneric, mutationGeneric, internalActionGeneric, internalMutationGeneric, internalQueryGeneric, } from "convex/server";
11
+ /**
12
+ * Define a query in this Convex app's public API.
13
+ *
14
+ * This function will be allowed to read your Convex database and will be accessible from the client.
15
+ *
16
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
17
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
18
+ */
19
+ export const query = queryGeneric;
20
+ /**
21
+ * Define a query that is only accessible from other Convex functions (but not from the client).
22
+ *
23
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
24
+ *
25
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
26
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
27
+ */
28
+ export const internalQuery = internalQueryGeneric;
29
+ /**
30
+ * Define a mutation in this Convex app's public API.
31
+ *
32
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
33
+ *
34
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
35
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
36
+ */
37
+ export const mutation = mutationGeneric;
38
+ /**
39
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
40
+ *
41
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
42
+ *
43
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
44
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
45
+ */
46
+ export const internalMutation = internalMutationGeneric;
47
+ /**
48
+ * Define an action in this Convex app's public API.
49
+ *
50
+ * An action is a function which can execute any JavaScript code, including non-deterministic
51
+ * code and code with side-effects, like calling third-party services.
52
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
53
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
54
+ *
55
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
56
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
57
+ */
58
+ export const action = actionGeneric;
59
+ /**
60
+ * Define an action that is only accessible from other Convex functions (but not from the client).
61
+ *
62
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
63
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
64
+ */
65
+ export const internalAction = internalActionGeneric;
66
+ /**
67
+ * Define an HTTP action.
68
+ *
69
+ * The wrapped function will be used to respond to HTTP requests received
70
+ * by a Convex deployment if the requests matches the path and method where
71
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
72
+ *
73
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
74
+ * and a Fetch API `Request` object as its second.
75
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
76
+ */
77
+ export const httpAction = httpActionGeneric;
78
+ export const env = globalThis
79
+ .process.env;
80
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/component/_generated/server.ts"],"names":[],"mappings":"AAAA,oBAAoB;AACpB;;;;;;;GAOG;AAaH,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AAcvB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,KAAK,GAAsC,YAAY,CAAC;AAErE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GACxB,oBAAoB,CAAC;AAEvB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAyC,eAAe,CAAC;AAE9E;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAC3B,uBAAuB,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,MAAM,GAAuC,aAAa,CAAC;AAExE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,qBAAqB,CAAC;AAExB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,UAAU,GAAsB,iBAAiB,CAAC;AAC/D,MAAM,CAAC,MAAM,GAAG,GAAS,UAAmD;KACzE,OAAO,CAAC,GAAG,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const component: import("convex/server").ComponentDefinition<any, {}>;
2
+ export default component;
3
+ //# sourceMappingURL=convex.config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convex.config.d.ts","sourceRoot":"","sources":["../../src/component/convex.config.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,SAAS,sDAAiC,CAAC;eAElC,SAAS"}
@@ -0,0 +1,4 @@
1
+ import { defineComponent } from "convex/server";
2
+ const component = defineComponent("progression");
3
+ export default component;
4
+ //# sourceMappingURL=convex.config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convex.config.js","sourceRoot":"","sources":["../../src/component/convex.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,SAAS,GAAG,eAAe,CAAC,aAAa,CAAC,CAAC;AAEjD,eAAe,SAAS,CAAC"}
@@ -0,0 +1,43 @@
1
+ export declare const accrue: import("convex/server").RegisteredMutation<"public", {
2
+ delta: number;
3
+ key: string;
4
+ scope: string;
5
+ subjectRef: string;
6
+ thresholds: number[];
7
+ }, Promise<{
8
+ lastPeriodKey: string | undefined;
9
+ level: number;
10
+ maxStreak: number;
11
+ streak: number;
12
+ updatedAt: number;
13
+ xp: number;
14
+ leveledUp: boolean;
15
+ previousLevel: number;
16
+ }>>;
17
+ export declare const recordActivity: import("convex/server").RegisteredMutation<"public", {
18
+ expectedPrevious?: string | undefined;
19
+ key: string;
20
+ periodKey: string;
21
+ scope: string;
22
+ subjectRef: string;
23
+ thresholds: number[];
24
+ }, Promise<{
25
+ lastPeriodKey: string;
26
+ level: number;
27
+ maxStreak: number;
28
+ streak: number;
29
+ updatedAt: number;
30
+ xp: number;
31
+ streakDelta: number;
32
+ }>>;
33
+ export declare const reset: import("convex/server").RegisteredMutation<"public", {
34
+ key: string;
35
+ scope: string;
36
+ subjectRef: string;
37
+ }, Promise<null>>;
38
+ export declare const eraseSubject: import("convex/server").RegisteredMutation<"public", {
39
+ batch?: number | undefined;
40
+ scope: string;
41
+ subjectRef: string;
42
+ }, Promise<number>>;
43
+ //# sourceMappingURL=mutations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;GAmEjB,CAAC;AAEH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;GA+DzB,CAAC;AAEH,eAAO,MAAM,KAAK;;;;iBAyBhB,CAAC;AAEH,eAAO,MAAM,YAAY;;;;mBAkCvB,CAAC"}
@@ -0,0 +1,179 @@
1
+ import { v } from "convex/values";
2
+ import { DEFAULT_ERASE_BATCH, levelForXp, MAX_ERASE_BATCH } from "../shared";
3
+ import { api } from "./_generated/api";
4
+ import { mutation } from "./_generated/server";
5
+ import { fail, parseThresholds, requireRef } from "./validation";
6
+ import { accrueResult, activityResult } from "./validators";
7
+ export const accrue = mutation({
8
+ args: {
9
+ delta: v.number(),
10
+ key: v.string(),
11
+ scope: v.string(),
12
+ subjectRef: v.string(),
13
+ thresholds: v.array(v.number()),
14
+ },
15
+ // Keep each atomic transaction readable as one handler.
16
+ // eslint-disable-next-line max-lines-per-function
17
+ handler: async (ctx, arguments_) => {
18
+ requireRef(arguments_.subjectRef, "subjectRef");
19
+ requireRef(arguments_.key, "key");
20
+ requireRef(arguments_.scope, "scope");
21
+ if (!Number.isFinite(arguments_.delta) ||
22
+ arguments_.delta <= 0 ||
23
+ arguments_.delta > Number.MAX_SAFE_INTEGER) {
24
+ fail("INVALID_DELTA", "delta must be positive and at most MAX_SAFE_INTEGER");
25
+ }
26
+ const thresholds = parseThresholds(arguments_.thresholds);
27
+ const existing = await ctx.db
28
+ .query("progress")
29
+ .withIndex("by_scope_subject_key", (q) => q
30
+ .eq("scope", arguments_.scope)
31
+ .eq("subjectRef", arguments_.subjectRef)
32
+ .eq("key", arguments_.key))
33
+ .unique();
34
+ const now = Date.now();
35
+ const previousLevel = existing?.level ?? 0;
36
+ const xp = (existing?.xp ?? 0) + arguments_.delta;
37
+ if (!Number.isFinite(xp) ||
38
+ xp > Number.MAX_SAFE_INTEGER ||
39
+ xp <= (existing?.xp ?? 0)) {
40
+ fail("XP_OVERFLOW", "XP exceeds the safe bound or delta is lost to floating-point precision");
41
+ }
42
+ const level = levelForXp(xp, thresholds);
43
+ const row = {
44
+ lastPeriodKey: existing?.lastPeriodKey,
45
+ level,
46
+ maxStreak: existing?.maxStreak ?? 0,
47
+ streak: existing?.streak ?? 0,
48
+ updatedAt: now,
49
+ xp,
50
+ };
51
+ await (existing === null
52
+ ? ctx.db.insert("progress", {
53
+ key: arguments_.key,
54
+ scope: arguments_.scope,
55
+ subjectRef: arguments_.subjectRef,
56
+ ...row,
57
+ })
58
+ : ctx.db.patch("progress", existing._id, row));
59
+ return { ...row, leveledUp: level > previousLevel, previousLevel };
60
+ },
61
+ returns: accrueResult,
62
+ });
63
+ export const recordActivity = mutation({
64
+ args: {
65
+ expectedPrevious: v.optional(v.string()),
66
+ key: v.string(),
67
+ periodKey: v.string(),
68
+ scope: v.string(),
69
+ subjectRef: v.string(),
70
+ thresholds: v.array(v.number()),
71
+ },
72
+ // Keep each atomic transaction readable as one handler.
73
+ // eslint-disable-next-line max-lines-per-function
74
+ handler: async (ctx, arguments_) => {
75
+ requireRef(arguments_.subjectRef, "subjectRef");
76
+ requireRef(arguments_.key, "key");
77
+ requireRef(arguments_.scope, "scope");
78
+ requireRef(arguments_.periodKey, "periodKey");
79
+ if (arguments_.expectedPrevious !== undefined) {
80
+ requireRef(arguments_.expectedPrevious, "expectedPrevious");
81
+ }
82
+ const thresholds = parseThresholds(arguments_.thresholds);
83
+ const existing = await ctx.db
84
+ .query("progress")
85
+ .withIndex("by_scope_subject_key", (q) => q
86
+ .eq("scope", arguments_.scope)
87
+ .eq("subjectRef", arguments_.subjectRef)
88
+ .eq("key", arguments_.key))
89
+ .unique();
90
+ const now = Date.now();
91
+ const xp = existing?.xp ?? 0;
92
+ const lastPeriodKey = existing?.lastPeriodKey;
93
+ const samePeriod = lastPeriodKey === arguments_.periodKey;
94
+ const consecutive = lastPeriodKey !== undefined &&
95
+ lastPeriodKey === arguments_.expectedPrevious;
96
+ const continuedStreak = consecutive && existing !== null ? existing.streak + 1 : 1;
97
+ const streak = samePeriod && existing !== null ? existing.streak : continuedStreak;
98
+ if (!Number.isSafeInteger(streak)) {
99
+ fail("STREAK_OVERFLOW", "streak exceeds MAX_SAFE_INTEGER");
100
+ }
101
+ const streakDelta = samePeriod ? 0 : 1;
102
+ const row = {
103
+ lastPeriodKey: arguments_.periodKey,
104
+ level: levelForXp(xp, thresholds),
105
+ maxStreak: Math.max(existing?.maxStreak ?? 0, streak),
106
+ streak,
107
+ updatedAt: now,
108
+ xp,
109
+ };
110
+ await (existing === null
111
+ ? ctx.db.insert("progress", {
112
+ key: arguments_.key,
113
+ scope: arguments_.scope,
114
+ subjectRef: arguments_.subjectRef,
115
+ ...row,
116
+ })
117
+ : ctx.db.patch("progress", existing._id, row));
118
+ return { ...row, streakDelta };
119
+ },
120
+ returns: activityResult,
121
+ });
122
+ export const reset = mutation({
123
+ args: {
124
+ key: v.string(),
125
+ scope: v.string(),
126
+ subjectRef: v.string(),
127
+ },
128
+ handler: async (ctx, arguments_) => {
129
+ requireRef(arguments_.subjectRef, "subjectRef");
130
+ requireRef(arguments_.key, "key");
131
+ requireRef(arguments_.scope, "scope");
132
+ const existing = await ctx.db
133
+ .query("progress")
134
+ .withIndex("by_scope_subject_key", (q) => q
135
+ .eq("scope", arguments_.scope)
136
+ .eq("subjectRef", arguments_.subjectRef)
137
+ .eq("key", arguments_.key))
138
+ .unique();
139
+ if (existing !== null) {
140
+ await ctx.db.delete("progress", existing._id);
141
+ }
142
+ return null;
143
+ },
144
+ returns: v.null(),
145
+ });
146
+ export const eraseSubject = mutation({
147
+ args: {
148
+ batch: v.optional(v.number()),
149
+ scope: v.string(),
150
+ subjectRef: v.string(),
151
+ },
152
+ handler: async (ctx, arguments_) => {
153
+ requireRef(arguments_.subjectRef, "subjectRef");
154
+ requireRef(arguments_.scope, "scope");
155
+ const raw = arguments_.batch ?? DEFAULT_ERASE_BATCH;
156
+ if (!Number.isInteger(raw)) {
157
+ fail("INVALID_BATCH", "batch must be a positive integer");
158
+ }
159
+ if (raw < 1) {
160
+ fail("INVALID_BATCH", "batch must be a positive integer");
161
+ }
162
+ const batch = Math.min(raw, MAX_ERASE_BATCH);
163
+ const rows = await ctx.db
164
+ .query("progress")
165
+ .withIndex("by_scope_subject_key", (q) => q.eq("scope", arguments_.scope).eq("subjectRef", arguments_.subjectRef))
166
+ .take(batch);
167
+ await Promise.all(rows.map((row) => ctx.db.delete("progress", row._id)));
168
+ if (rows.length === batch) {
169
+ await ctx.scheduler.runAfter(0, api.mutations.eraseSubject, {
170
+ batch,
171
+ scope: arguments_.scope,
172
+ subjectRef: arguments_.subjectRef,
173
+ });
174
+ }
175
+ return rows.length;
176
+ },
177
+ returns: v.number(),
178
+ });
179
+ //# sourceMappingURL=mutations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAE7E,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE5D,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC;IAC7B,IAAI,EAAE;QACJ,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,UAAU,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;KAChC;IACD,wDAAwD;IACxD,kDAAkD;IAClD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,YAAY,CAA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,MAAM,EAAE;CACpB,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ export declare const get: import("convex/server").RegisteredQuery<"public", {
2
+ key: string;
3
+ scope: string;
4
+ subjectRef: string;
5
+ thresholds?: number[] | undefined;
6
+ }, Promise<{
7
+ lastPeriodKey: string | undefined;
8
+ level: number;
9
+ maxStreak: number;
10
+ streak: number;
11
+ updatedAt: number;
12
+ xp: number;
13
+ } | null>>;
14
+ //# sourceMappingURL=queries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,GAAG;;;;;;;;;;;;UAyCd,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { v } from "convex/values";
2
+ import { levelForXp } from "../shared";
3
+ import { query } from "./_generated/server";
4
+ import { parseThresholds, requireRef } from "./validation";
5
+ import { progressState } from "./validators";
6
+ export const get = query({
7
+ args: {
8
+ key: v.string(),
9
+ scope: v.string(),
10
+ subjectRef: v.string(),
11
+ thresholds: v.optional(v.array(v.number())),
12
+ },
13
+ // Keep boundary validation and indexed read together.
14
+ // eslint-disable-next-line max-lines-per-function
15
+ handler: async (ctx, arguments_) => {
16
+ requireRef(arguments_.subjectRef, "subjectRef");
17
+ requireRef(arguments_.key, "key");
18
+ requireRef(arguments_.scope, "scope");
19
+ if (arguments_.thresholds !== undefined) {
20
+ parseThresholds(arguments_.thresholds);
21
+ }
22
+ const row = await ctx.db
23
+ .query("progress")
24
+ .withIndex("by_scope_subject_key", (q) => q
25
+ .eq("scope", arguments_.scope)
26
+ .eq("subjectRef", arguments_.subjectRef)
27
+ .eq("key", arguments_.key))
28
+ .unique();
29
+ if (row === null) {
30
+ return null;
31
+ }
32
+ return {
33
+ lastPeriodKey: row.lastPeriodKey,
34
+ level: arguments_.thresholds === undefined
35
+ ? row.level
36
+ : levelForXp(row.xp, arguments_.thresholds),
37
+ maxStreak: row.maxStreak,
38
+ streak: row.streak,
39
+ updatedAt: row.updatedAt,
40
+ xp: row.xp,
41
+ };
42
+ },
43
+ returns: v.union(v.null(), progressState),
44
+ });
45
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,CAAC;IACvB,IAAI,EAAE;QACJ,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;KAC5C;IACD,sDAAsD;IACtD,kDAAkD;IAClD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,EAAE;QACjC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;QAChD,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAClC,UAAU,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACtC,IAAI,UAAU,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACxC,eAAe,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;QACzC,CAAC;QACD,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,EAAE;aACrB,KAAK,CAAC,UAAU,CAAC;aACjB,SAAS,CAAC,sBAAsB,EAAE,CAAC,CAAC,EAAE,EAAE,CACvC,CAAC;aACE,EAAE,CAAC,OAAO,EAAE,UAAU,CAAC,KAAK,CAAC;aAC7B,EAAE,CAAC,YAAY,EAAE,UAAU,CAAC,UAAU,CAAC;aACvC,EAAE,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,CAC7B;aACA,MAAM,EAAE,CAAC;QACZ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO;YACL,aAAa,EAAE,GAAG,CAAC,aAAa;YAChC,KAAK,EACH,UAAU,CAAC,UAAU,KAAK,SAAS;gBACjC,CAAC,CAAC,GAAG,CAAC,KAAK;gBACX,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,UAAU,CAAC,UAAU,CAAC;YAC/C,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,EAAE,EAAE,GAAG,CAAC,EAAE;SACX,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,aAAa,CAAC;CAC1C,CAAC,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Sandboxed tables — XP / level / streak per opaque `(scope, subjectRef, key)`.
3
+ * Thresholds are host-supplied on each write so the component never stores a
4
+ * domain-shaped definition table.
5
+ */
6
+ declare const _default: import("convex/server").SchemaDefinition<{
7
+ progress: import("convex/server").TableDefinition<import("convex/values").VObject<{
8
+ key: string;
9
+ lastPeriodKey?: string | undefined;
10
+ level: number;
11
+ maxStreak: number;
12
+ scope: string;
13
+ streak: number;
14
+ subjectRef: string;
15
+ updatedAt: number;
16
+ xp: number;
17
+ }, {
18
+ key: import("convex/values").VString<string, "required">;
19
+ lastPeriodKey: import("convex/values").VString<string | undefined, "optional">;
20
+ level: import("convex/values").VFloat64<number, "required">;
21
+ maxStreak: import("convex/values").VFloat64<number, "required">;
22
+ scope: import("convex/values").VString<string, "required">;
23
+ streak: import("convex/values").VFloat64<number, "required">;
24
+ subjectRef: import("convex/values").VString<string, "required">;
25
+ updatedAt: import("convex/values").VFloat64<number, "required">;
26
+ xp: import("convex/values").VFloat64<number, "required">;
27
+ }, "required", "key" | "lastPeriodKey" | "level" | "maxStreak" | "scope" | "streak" | "subjectRef" | "updatedAt" | "xp">, {
28
+ by_scope_subject_key: ["scope", "subjectRef", "key", "_creationTime"];
29
+ }, {}, {}>;
30
+ }, true>;
31
+ export default _default;
32
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAGA;;;;GAIG"}
@@ -0,0 +1,21 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ /**
4
+ * Sandboxed tables — XP / level / streak per opaque `(scope, subjectRef, key)`.
5
+ * Thresholds are host-supplied on each write so the component never stores a
6
+ * domain-shaped definition table.
7
+ */
8
+ export default defineSchema({
9
+ progress: defineTable({
10
+ key: v.string(),
11
+ lastPeriodKey: v.optional(v.string()),
12
+ level: v.number(),
13
+ maxStreak: v.number(),
14
+ scope: v.string(),
15
+ streak: v.number(),
16
+ subjectRef: v.string(),
17
+ updatedAt: v.number(),
18
+ xp: v.number(),
19
+ }).index("by_scope_subject_key", ["scope", "subjectRef", "key"]),
20
+ });
21
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;GAIG;AACH,eAAe,YAAY,CAAC;IAC1B,QAAQ,EAAE,WAAW,CAAC;QACpB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,aAAa,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QACrC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;QACrB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE;QAClB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;QACrB,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;KACf,CAAC,CAAC,KAAK,CAAC,sBAAsB,EAAE,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;CACjE,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ export declare function fail(code: string, message: string): never;
2
+ export declare function requireRef(value: string, name: string): void;
3
+ export declare function parseThresholds(thresholds: number[]): number[];
4
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/component/validation.ts"],"names":[],"mappings":"AAIA,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,KAAK,CAEzD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAO5D;AAED,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAY9D"}
@@ -0,0 +1,22 @@
1
+ import { ConvexError } from "convex/values";
2
+ import { assertThresholds, MAX_REF_LENGTH } from "../shared";
3
+ export function fail(code, message) {
4
+ throw new ConvexError({ code, message });
5
+ }
6
+ export function requireRef(value, name) {
7
+ if (value.length === 0 || value.length > MAX_REF_LENGTH) {
8
+ fail("INVALID_REF", `${name} must be 1..${String(MAX_REF_LENGTH)} characters`);
9
+ }
10
+ }
11
+ export function parseThresholds(thresholds) {
12
+ try {
13
+ assertThresholds(thresholds);
14
+ }
15
+ catch (error) {
16
+ fail("INVALID_THRESHOLDS", String(error)
17
+ .replace(/^Error:\s*/, "")
18
+ .replace(/^INVALID_THRESHOLDS:\s*/, ""));
19
+ }
20
+ return thresholds;
21
+ }
22
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/component/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE7D,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,OAAe;IAChD,MAAM,IAAI,WAAW,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAa,EAAE,IAAY;IACpD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,cAAc,EAAE,CAAC;QACxD,IAAI,CACF,aAAa,EACb,GAAG,IAAI,eAAe,MAAM,CAAC,cAAc,CAAC,aAAa,CAC1D,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,UAAoB;IAClD,IAAI,CAAC;QACH,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CACF,oBAAoB,EACpB,MAAM,CAAC,KAAK,CAAC;aACV,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC;aACzB,OAAO,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAC1C,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC"}