make-mp-data 2.1.11 → 3.0.1

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 (76) hide show
  1. package/README.md +31 -0
  2. package/dungeons/adspend.js +2 -2
  3. package/dungeons/ai-chat-analytics-ed.js +3 -2
  4. package/dungeons/anon.js +2 -2
  5. package/dungeons/array-of-object-loopup.js +181 -0
  6. package/dungeons/benchmark-heavy.js +241 -0
  7. package/dungeons/benchmark-light.js +141 -0
  8. package/dungeons/big.js +9 -8
  9. package/dungeons/business.js +2 -1
  10. package/dungeons/clinch-agi.js +632 -0
  11. package/dungeons/complex.js +3 -2
  12. package/dungeons/copilot.js +383 -0
  13. package/dungeons/ecommerce-store.js +0 -0
  14. package/dungeons/experiments.js +5 -4
  15. package/dungeons/foobar.js +1 -1
  16. package/dungeons/funnels.js +2 -2
  17. package/dungeons/gaming.js +3 -2
  18. package/dungeons/harness/harness-education.js +988 -0
  19. package/dungeons/harness/harness-fintech.js +976 -0
  20. package/dungeons/harness/harness-food.js +985 -0
  21. package/dungeons/harness/harness-gaming.js +1178 -0
  22. package/dungeons/harness/harness-media.js +961 -0
  23. package/dungeons/harness/harness-sass.js +923 -0
  24. package/dungeons/harness/harness-social.js +928 -0
  25. package/dungeons/kurby.js +211 -0
  26. package/dungeons/media.js +5 -4
  27. package/dungeons/mil.js +4 -3
  28. package/dungeons/mirror.js +2 -2
  29. package/dungeons/money2020-ed.js +8 -7
  30. package/dungeons/sanity.js +3 -2
  31. package/dungeons/scd.js +3 -2
  32. package/dungeons/simple.js +30 -15
  33. package/dungeons/strict-event-test.js +30 -0
  34. package/dungeons/student-teacher.js +3 -2
  35. package/dungeons/text-generation.js +84 -85
  36. package/dungeons/too-big-events.js +166 -0
  37. package/dungeons/uday-schema.json +220 -0
  38. package/dungeons/userAgent.js +4 -3
  39. package/index.js +41 -54
  40. package/lib/core/config-validator.js +122 -7
  41. package/lib/core/context.js +7 -14
  42. package/lib/core/storage.js +57 -25
  43. package/lib/generators/adspend.js +12 -12
  44. package/lib/generators/events.js +6 -5
  45. package/lib/generators/funnels.js +32 -10
  46. package/lib/generators/product-lookup.js +262 -0
  47. package/lib/generators/product-names.js +195 -0
  48. package/lib/generators/profiles.js +3 -3
  49. package/lib/generators/scd.js +13 -3
  50. package/lib/generators/text.js +17 -4
  51. package/lib/orchestrators/mixpanel-sender.js +244 -204
  52. package/lib/orchestrators/user-loop.js +54 -16
  53. package/lib/templates/funnels-instructions.txt +272 -0
  54. package/lib/templates/hook-examples.json +187 -0
  55. package/lib/templates/hooks-instructions.txt +295 -8
  56. package/lib/templates/phrases.js +473 -16
  57. package/lib/templates/refine-instructions.txt +485 -0
  58. package/lib/templates/schema-instructions.txt +239 -109
  59. package/lib/templates/schema.d.ts +173 -0
  60. package/lib/templates/verbose-schema.js +140 -206
  61. package/lib/utils/ai.js +853 -77
  62. package/lib/utils/chart.js +210 -0
  63. package/lib/utils/function-registry.js +285 -0
  64. package/lib/utils/json-evaluator.js +172 -0
  65. package/lib/utils/logger.js +38 -0
  66. package/lib/utils/mixpanel.js +101 -0
  67. package/lib/utils/project.js +3 -2
  68. package/lib/utils/utils.js +41 -4
  69. package/package.json +15 -21
  70. package/types.d.ts +15 -5
  71. package/lib/generators/text-bak-old.js +0 -1121
  72. package/lib/orchestrators/worker-manager.js +0 -203
  73. package/lib/templates/phrases-bak.js +0 -925
  74. package/lib/templates/prompt (old).txt +0 -98
  75. package/lib/templates/scratch-dungeon-template.js +0 -116
  76. package/lib/templates/textQuickTest.js +0 -172
@@ -1,203 +0,0 @@
1
- /**
2
- * Cloud Worker Manager module
3
- * Handles distributed processing across multiple cloud function workers
4
- */
5
-
6
- /** @typedef {import('../../types.js').Context} Context */
7
- /** @typedef {import('../../types.js').Dungeon} Dungeon */
8
-
9
- import pLimit from 'p-limit';
10
- import { GoogleAuth } from 'google-auth-library';
11
- import { timer, uid, sLog } from 'ak-tools';
12
-
13
- const CONCURRENCY = 1_000;
14
- let RUNTIME_URL = "https://dm4-lmozz6xkha-uc.a.run.app";
15
-
16
- /**
17
- * Spawn multiple cloud function workers to process data generation in parallel
18
- * @param {number} numberWorkers - Number of worker instances to spawn
19
- * @param {string} payload - Script payload to execute on each worker
20
- * @param {Object} params - Parameters for the job execution
21
- * @returns {Promise<Object>} Results summary with success/failure counts
22
- */
23
- export async function spawnFileWorkers(numberWorkers, payload, params) {
24
- const auth = new GoogleAuth();
25
- let client;
26
-
27
- if (RUNTIME_URL.includes('localhost')) {
28
- client = await auth.getClient();
29
- } else {
30
- client = await auth.getIdTokenClient(RUNTIME_URL);
31
- }
32
-
33
- const limit = pLimit(CONCURRENCY);
34
- const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
35
-
36
- const requestPromises = Array.from({ length: numberWorkers }, async (_, index) => {
37
- index = index + 1;
38
- await delay(index * 108); // Stagger requests to avoid thundering herd
39
- return limit(() => buildRequest(client, payload, index, params, numberWorkers));
40
- });
41
-
42
- const complete = await Promise.allSettled(requestPromises);
43
- const results = {
44
- jobs_success: complete.filter((p) => p.status === "fulfilled").length,
45
- jobs_fail: complete.filter((p) => p.status === "rejected").length
46
- };
47
-
48
- return results;
49
- }
50
-
51
- /**
52
- * Build and execute a single worker request
53
- * @param {Object} client - Authenticated Google Cloud client
54
- * @param {string} payload - Script payload to send
55
- * @param {number} index - Worker index number
56
- * @param {Object} params - Job parameters
57
- * @param {number} total - Total number of workers
58
- * @returns {Promise<Object>} Worker response data
59
- */
60
- async function buildRequest(client, payload, index, params, total) {
61
- let retryAttempt = 0;
62
- sLog(`DM4: summoning worker #${index} of ${total}`, params);
63
-
64
- try {
65
- const req = await client.request({
66
- url: RUNTIME_URL + `?replicate=1&is_replica=true&runId=${params.runId || "no run id"}`,
67
- method: "POST",
68
- data: payload,
69
- headers: {
70
- "Content-Type": "text/plain",
71
- },
72
- timeout: 3600 * 1000 * 10, // 10 hours timeout
73
- retryConfig: {
74
- retry: 3,
75
- onRetryAttempt: (error) => {
76
- const statusCode = error?.response?.status?.toString() || "";
77
- retryAttempt++;
78
- sLog(`DM4: summon worker ${index} retry #${retryAttempt}`, {
79
- statusCode,
80
- message: error.message,
81
- stack: error.stack,
82
- ...params
83
- }, "DEBUG");
84
- },
85
- retryDelay: 1000,
86
- shouldRetry: (error) => {
87
- if (error.code === 'ECONNRESET') return true;
88
- const statusCode = error?.response?.status;
89
- if (statusCode >= 500) return true;
90
- if (statusCode === 429) return true;
91
- return false;
92
- }
93
- },
94
- });
95
-
96
- sLog(`DM4: worker #${index} responded`, params);
97
- const { data } = req;
98
- return data;
99
- } catch (error) {
100
- sLog(`DM4: worker #${index} failed to respond`, {
101
- message: error.message,
102
- stack: error.stack,
103
- code: error.code,
104
- retries: retryAttempt,
105
- ...params
106
- }, "ERROR");
107
- return {};
108
- }
109
- }
110
-
111
- /**
112
- * Handle cloud function HTTP entry point
113
- * @param {Object} req - HTTP request object
114
- * @param {Object} res - HTTP response object
115
- * @param {Function} mainFunction - Main data generation function to execute
116
- * @returns {Promise<void>}
117
- */
118
- export async function handleCloudFunctionEntry(req, res, mainFunction) {
119
- const reqTimer = timer('request');
120
- reqTimer.start();
121
- let response = {};
122
- let script = req.body || "";
123
- const params = {
124
- replicate: 1,
125
- is_replica: "false",
126
- runId: "",
127
- seed: "",
128
- ...req.query
129
- };
130
-
131
- const replicate = Number(params.replicate);
132
-
133
- // Parse boolean parameters
134
- if (params?.is_replica === "true") params.is_replica = true;
135
- else params.is_replica = false;
136
-
137
- const isReplica = params.is_replica;
138
-
139
- if (!params.runId) params.runId = uid(42);
140
-
141
- try {
142
- if (!script) throw new Error("no script");
143
-
144
- /** @type {Dungeon} */
145
- const config = eval(script);
146
-
147
- if (isReplica) {
148
- // Generate unique seed for replica workers
149
- const newSeed = (Math.random() / Math.random() / Math.random() / Math.random() / Math.random() / Math.random()).toString();
150
- config.seed = newSeed;
151
- params.seed = newSeed;
152
- }
153
-
154
- /** @type {Dungeon} */
155
- const optionsYouCantChange = {
156
- verbose: false
157
- };
158
-
159
- if (replicate <= 1 || isReplica) {
160
- if (isReplica) sLog("DM4: worker start", params);
161
-
162
- const { files = [], operations = 0, eventCount = 0, userCount = 0 } = await mainFunction({
163
- ...config,
164
- ...optionsYouCantChange,
165
- });
166
-
167
- reqTimer.stop(false);
168
- response = { files, operations, eventCount, userCount };
169
- } else {
170
- sLog(`DM4: job start (${replicate} workers)`, params);
171
- const results = await spawnFileWorkers(replicate, script, params);
172
- response = results;
173
- }
174
- } catch (e) {
175
- sLog("DM4: error", { error: e.message, stack: e.stack }, "ERROR");
176
- response = { error: e.message };
177
- res.status(500);
178
- } finally {
179
- reqTimer.stop(false);
180
- const { start, end, delta, human } = reqTimer.report(false);
181
-
182
- if (!isReplica) {
183
- sLog(`DM4: job end (${human})`, { human, delta, ...params, ...response });
184
- }
185
-
186
- if (isReplica) {
187
- const eps = Math.floor(((response?.eventCount || 0) / delta) * 1000);
188
- sLog(`DM4: worker end (${human})`, { human, delta, eps, ...params, ...response });
189
- }
190
-
191
- response = { ...response, start, end, delta, human, ...params };
192
- res.send(response);
193
- return;
194
- }
195
- }
196
-
197
- /**
198
- * Set the runtime URL for the cloud function service
199
- * @param {string} url - The runtime URL
200
- */
201
- export function setRuntimeUrl(url) {
202
- RUNTIME_URL = url;
203
- }