@bitcobblers/wod-wiki-engine 0.59.2238 → 0.59.2243

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.
package/dist/agent.cjs CHANGED
@@ -1059,68 +1059,22 @@ function notebookSlugForChunkId(chunkId) {
1059
1059
  return value && value !== "_root" ? value : void 0;
1060
1060
  }
1061
1061
 
1062
- // src/agent/helpers.ts
1063
- function mintAgentUuid() {
1064
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
1065
- return crypto.randomUUID();
1066
- }
1067
- const bytes = new Uint8Array(16);
1068
- crypto.getRandomValues(bytes);
1069
- bytes[6] = bytes[6] & 15 | 64;
1070
- bytes[8] = bytes[8] & 63 | 128;
1071
- const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
1072
- return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
1073
- }
1074
- function extractHeadingTitleFromMarkdown(rawContent) {
1075
- const withoutFm = rawContent.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, "");
1076
- for (const line of withoutFm.split(/\r?\n/)) {
1077
- const m = line.match(/^#\s+(.+)$/);
1078
- if (m?.[1]?.trim()) return m[1].trim();
1079
- }
1080
- return void 0;
1081
- }
1082
- function normalizeMarkdownOrBareScript(source, sport, title) {
1083
- const trimmed = source.trim();
1084
- if (!trimmed) return title ? `# ${title}
1085
- ` : "";
1086
- const rewrittenFences = source.replace(/^(\s*`{3,})(?:wod|whiteboard)(?=(?::[\w-]+)?\s*$)/gim, "$1time");
1087
- const sections = wodWikiCore.parseDocumentSections(rewrittenFences, { registeredWidgets: wodWikiCore.KNOWN_SECTION_WIDGETS });
1088
- if (sections.some((s) => s.scriptBlock) || /^---\r?\n/.test(rewrittenFences) || /^#\s+/m.test(rewrittenFences)) {
1089
- return rewrittenFences.endsWith("\n") ? rewrittenFences : `${rewrittenFences}
1090
- `;
1091
- }
1092
- let fenceLen = 3;
1093
- for (const match of rewrittenFences.matchAll(/`+/g)) {
1094
- fenceLen = Math.max(fenceLen, match[0].length + 1);
1095
- }
1096
- const fence = "`".repeat(fenceLen);
1097
- const heading = title ? `# ${title}
1098
-
1099
- ` : "";
1100
- return `${heading}${fence}time${sport ? `:${sport}` : ""}
1101
- ${trimmed}
1102
- ${fence}
1103
- `;
1104
- }
1105
- function civilDateKey(input, nowMs = Date.now()) {
1106
- if (!input || input.trim().toLowerCase() === "today") {
1107
- const d = new Date(nowMs);
1108
- const y = d.getUTCFullYear();
1109
- const m = String(d.getUTCMonth() + 1).padStart(2, "0");
1110
- const day = String(d.getUTCDate()).padStart(2, "0");
1111
- return `${y}-${m}-${day}`;
1112
- }
1113
- const trimmed = input.trim();
1114
- if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
1115
- throw new Error(`Invalid date '${input}'; expected YYYY-MM-DD or 'today'.`);
1116
- }
1117
- return trimmed;
1118
- }
1119
- function buildQueryServiceStoresFromStorage(storage) {
1062
+ // src/storage/queryServiceStores.ts
1063
+ function buildQueryServiceStoresFromStorage(storage, options = {}) {
1120
1064
  const asRange = (lower, upper) => typeof IDBKeyRange !== "undefined" ? IDBKeyRange.bound(lower, upper) : { lower, upper };
1121
1065
  const resultRows = (key) => asRange([key, ""], [`${key}\uFFFF`, "\uFFFF"]);
1122
- const eventStore = {
1066
+ const rawEventStore = {
1123
1067
  getEventsByTimeRange: async (start, end) => storage.readonly("events").getAllFromIndex("by-timestamp", asRange(start, end)),
1068
+ getEventsByMetricDates: async (dates) => {
1069
+ const store = storage.readonly("events");
1070
+ const byId = /* @__PURE__ */ new Map();
1071
+ for (const date of dates) {
1072
+ for (const row of await store.getAllFromIndex("by-metric-date", date)) {
1073
+ byId.set(row.id, row);
1074
+ }
1075
+ }
1076
+ return Array.from(byId.values());
1077
+ },
1124
1078
  getEventsByResult: async (resultId) => storage.readonly("events").getAllFromIndex("by-result-grain", resultRows(resultId)),
1125
1079
  getEventsForNote: async (noteId) => (await storage.readonly("events").getAll()).filter((row) => row.noteId === noteId),
1126
1080
  getEventsByContent: async (cid) => storage.readonly("events").getAllFromIndex("by-content-grain", resultRows(cid)),
@@ -1141,6 +1095,7 @@ function buildQueryServiceStoresFromStorage(storage) {
1141
1095
  for (const id of ids) await storage.readwrite("events").delete(id);
1142
1096
  }
1143
1097
  };
1098
+ const eventStore = options.coalesce !== false ? wodWikiWql.coalescingEventStore(rawEventStore) : rawEventStore;
1144
1099
  const noteStore = {
1145
1100
  getAllNotes: () => storage.readonly("notes").getAll(),
1146
1101
  getNotesByIds: async (ids) => {
@@ -1175,7 +1130,7 @@ function buildQueryServiceStoresFromStorage(storage) {
1175
1130
  return result && result.plan === "blocks" ? result : void 0;
1176
1131
  }
1177
1132
  };
1178
- const effortStore = {
1133
+ const effortStore = options.effortStore ?? {
1179
1134
  getAllEfforts: async () => {
1180
1135
  const stored = await storage.readonly("efforts").getAll();
1181
1136
  const bySlug = /* @__PURE__ */ new Map();
@@ -1184,7 +1139,65 @@ function buildQueryServiceStoresFromStorage(storage) {
1184
1139
  return [...bySlug.values()];
1185
1140
  }
1186
1141
  };
1187
- return {
1142
+ const feedCatalogStore = options.feedCatalogStore ?? {
1143
+ getAllFeeds: async () => {
1144
+ const rows = [];
1145
+ for (const row of await storage.readonly("meta").getAll()) {
1146
+ const key = row.key;
1147
+ if (typeof key !== "string" || !key.startsWith("seed:") || key === seedMetaKey("user")) continue;
1148
+ const id = key.slice("seed:".length);
1149
+ rows.push({
1150
+ id,
1151
+ label: id,
1152
+ url: `https://${id}.example/feed/`,
1153
+ tier: "imported",
1154
+ subscribed: true,
1155
+ installed: true,
1156
+ notebookCount: 0,
1157
+ itemCount: 0
1158
+ });
1159
+ }
1160
+ return rows;
1161
+ },
1162
+ getAllNotebooks: async () => {
1163
+ const links = await storage.readonly("notebook_links").getAll().catch(() => []);
1164
+ const notes = await storage.readonly("notes").getAll();
1165
+ const memberCount = /* @__PURE__ */ new Map();
1166
+ for (const link of links) {
1167
+ if (link.targetKind === "note") {
1168
+ memberCount.set(link.notebookId, (memberCount.get(link.notebookId) ?? 0) + 1);
1169
+ }
1170
+ }
1171
+ const rows = [];
1172
+ for (const [notebookId, count] of memberCount) {
1173
+ const slug = notebookId.startsWith("notebook/") ? notebookId.slice("notebook/".length) : notebookId;
1174
+ const landing = notes.find((n) => n.id === notebookId);
1175
+ const feedSourceId = landing?.feedSourceId;
1176
+ const chunkId = `notebook.${slug}`;
1177
+ const feedId = feedSourceId ?? "local";
1178
+ let subscribed = feedSourceId !== void 0;
1179
+ if (options.isNotebookSubscribed) {
1180
+ const res = options.isNotebookSubscribed(feedId, chunkId);
1181
+ subscribed = typeof res === "boolean" ? res : Boolean(await res);
1182
+ }
1183
+ rows.push({
1184
+ id: notebookId,
1185
+ feedId,
1186
+ feedLabel: feedSourceId ?? "Yours (Local)",
1187
+ chunkId,
1188
+ slug,
1189
+ label: landing?.title ?? slug,
1190
+ count,
1191
+ subscribed,
1192
+ installed: true,
1193
+ origin: feedSourceId ? "feed" : "user",
1194
+ ...landing?.public !== void 0 ? { public: landing.public } : {}
1195
+ });
1196
+ }
1197
+ return rows;
1198
+ }
1199
+ };
1200
+ const stores = {
1188
1201
  eventStore,
1189
1202
  noteStore,
1190
1203
  blockStore,
@@ -1225,58 +1238,76 @@ function buildQueryServiceStoresFromStorage(storage) {
1225
1238
  return bundles;
1226
1239
  }
1227
1240
  },
1228
- feedCatalogStore: {
1229
- getAllFeeds: async () => {
1230
- const rows = [];
1231
- for (const row of await storage.readonly("meta").getAll()) {
1232
- const key = row.key;
1233
- if (typeof key !== "string" || !key.startsWith("seed:") || key === seedMetaKey("user")) continue;
1234
- const id = key.slice("seed:".length);
1235
- rows.push({
1236
- id,
1237
- label: id,
1238
- url: `https://${id}.example/feed/`,
1239
- tier: "imported",
1240
- subscribed: true,
1241
- installed: true,
1242
- notebookCount: 0,
1243
- itemCount: 0
1244
- });
1245
- }
1246
- return rows;
1247
- },
1248
- getAllNotebooks: async () => {
1249
- const links = await storage.readonly("notebook_links").getAll().catch(() => []);
1250
- const notes = await storage.readonly("notes").getAll();
1251
- const memberCount = /* @__PURE__ */ new Map();
1252
- for (const link of links) {
1253
- if (link.targetKind === "note") {
1254
- memberCount.set(link.notebookId, (memberCount.get(link.notebookId) ?? 0) + 1);
1255
- }
1256
- }
1257
- const rows = [];
1258
- for (const [notebookId, count] of memberCount) {
1259
- const slug = notebookId.startsWith("notebook/") ? notebookId.slice("notebook/".length) : notebookId;
1260
- const landing = notes.find((n) => n.id === notebookId);
1261
- const feedSourceId = landing?.feedSourceId;
1262
- rows.push({
1263
- id: notebookId,
1264
- feedId: feedSourceId ?? "local",
1265
- feedLabel: feedSourceId ?? "Yours (Local)",
1266
- chunkId: `notebook.${slug}`,
1267
- slug,
1268
- label: landing?.title ?? slug,
1269
- count,
1270
- subscribed: feedSourceId !== void 0,
1271
- installed: true,
1272
- origin: feedSourceId ? "feed" : "user",
1273
- ...landing?.public !== void 0 ? { public: landing.public } : {}
1274
- });
1275
- }
1276
- return rows;
1277
- }
1278
- }
1241
+ feedCatalogStore
1279
1242
  };
1243
+ if (options.datasetStore) {
1244
+ stores.datasetStore = options.datasetStore;
1245
+ }
1246
+ if (options.isNotebookSubscribed) {
1247
+ stores.isNotebookSubscribed = (feedId, chunkId) => {
1248
+ const res = options.isNotebookSubscribed(feedId, chunkId);
1249
+ return typeof res === "boolean" ? res : true;
1250
+ };
1251
+ }
1252
+ return stores;
1253
+ }
1254
+
1255
+ // src/agent/helpers.ts
1256
+ function mintAgentUuid() {
1257
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
1258
+ return crypto.randomUUID();
1259
+ }
1260
+ const bytes = new Uint8Array(16);
1261
+ crypto.getRandomValues(bytes);
1262
+ bytes[6] = bytes[6] & 15 | 64;
1263
+ bytes[8] = bytes[8] & 63 | 128;
1264
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
1265
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
1266
+ }
1267
+ function extractHeadingTitleFromMarkdown(rawContent) {
1268
+ const withoutFm = rawContent.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, "");
1269
+ for (const line of withoutFm.split(/\r?\n/)) {
1270
+ const m = line.match(/^#\s+(.+)$/);
1271
+ if (m?.[1]?.trim()) return m[1].trim();
1272
+ }
1273
+ return void 0;
1274
+ }
1275
+ function normalizeMarkdownOrBareScript(source, sport, title) {
1276
+ const trimmed = source.trim();
1277
+ if (!trimmed) return title ? `# ${title}
1278
+ ` : "";
1279
+ const rewrittenFences = source.replace(/^(\s*`{3,})(?:wod|whiteboard)(?=(?::[\w-]+)?\s*$)/gim, "$1time");
1280
+ const sections = wodWikiCore.parseDocumentSections(rewrittenFences, { registeredWidgets: wodWikiCore.KNOWN_SECTION_WIDGETS });
1281
+ if (sections.some((s) => s.scriptBlock) || /^---\r?\n/.test(rewrittenFences) || /^#\s+/m.test(rewrittenFences)) {
1282
+ return rewrittenFences.endsWith("\n") ? rewrittenFences : `${rewrittenFences}
1283
+ `;
1284
+ }
1285
+ let fenceLen = 3;
1286
+ for (const match of rewrittenFences.matchAll(/`+/g)) {
1287
+ fenceLen = Math.max(fenceLen, match[0].length + 1);
1288
+ }
1289
+ const fence = "`".repeat(fenceLen);
1290
+ const heading = title ? `# ${title}
1291
+
1292
+ ` : "";
1293
+ return `${heading}${fence}time${sport ? `:${sport}` : ""}
1294
+ ${trimmed}
1295
+ ${fence}
1296
+ `;
1297
+ }
1298
+ function civilDateKey(input, nowMs = Date.now()) {
1299
+ if (!input || input.trim().toLowerCase() === "today") {
1300
+ const d = new Date(nowMs);
1301
+ const y = d.getUTCFullYear();
1302
+ const m = String(d.getUTCMonth() + 1).padStart(2, "0");
1303
+ const day = String(d.getUTCDate()).padStart(2, "0");
1304
+ return `${y}-${m}-${day}`;
1305
+ }
1306
+ const trimmed = input.trim();
1307
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) {
1308
+ throw new Error(`Invalid date '${input}'; expected YYYY-MM-DD or 'today'.`);
1309
+ }
1310
+ return trimmed;
1280
1311
  }
1281
1312
  function createQueryServiceForStorage(storage) {
1282
1313
  return new wodWikiWql.QueryService(buildQueryServiceStoresFromStorage(storage));
@@ -3063,7 +3094,7 @@ async function ingestPortableFilesIntoStorage(storage, files, options = {}) {
3063
3094
  }
3064
3095
 
3065
3096
  // src/version.ts
3066
- var VERSION = "0.59.2238";
3097
+ var VERSION = "0.59.2243";
3067
3098
 
3068
3099
  // src/feed/qr.ts
3069
3100
  var APP_ORIGIN = "https://wod.wiki";