@codemem/server 0.48.1 → 0.49.0-beta.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.
@@ -0,0 +1,31 @@
1
+ export type FeedCursorPosition = {
2
+ created_at: string;
3
+ id: number;
4
+ };
5
+ export type FeedCursorPayload = FeedCursorPosition & {
6
+ v: 1;
7
+ filter: string;
8
+ };
9
+ export type FeedCursorFilterOptions = {
10
+ classification: "observation" | "summary";
11
+ project?: string;
12
+ scope?: "mine" | "theirs";
13
+ q?: string;
14
+ actorId: string;
15
+ dbPath: string;
16
+ };
17
+ export declare const MAX_FEED_CURSOR_LENGTH = 4096;
18
+ export declare function feedCursorFilterKey(options: FeedCursorFilterOptions): string;
19
+ export declare function tryEncodeFeedCursor(options: {
20
+ position: FeedCursorPosition;
21
+ filter: string;
22
+ }): string | null;
23
+ export declare function encodeFeedCursor(options: {
24
+ position: FeedCursorPosition;
25
+ filter: string;
26
+ }): string;
27
+ export declare function decodeFeedCursor(options: {
28
+ cursor: string;
29
+ filter: string;
30
+ }): FeedCursorPosition | null;
31
+ //# sourceMappingURL=feed-cursor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feed-cursor.d.ts","sourceRoot":"","sources":["../src/feed-cursor.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,kBAAkB,GAAG;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,EAAE,EAAE,MAAM,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,kBAAkB,GAAG;IACpD,CAAC,EAAE,CAAC,CAAC;IACL,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACrC,cAAc,EAAE,aAAa,GAAG,SAAS,CAAC;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,QAAQ,CAAC;IAC1B,CAAC,CAAC,EAAE,MAAM,CAAC;I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B,CAAC,CAAC;IACrE,gBAAgB,CAAC,EAAE,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;IACzD,oBAAoB,CAAC,EAAE,MAAM;QAC5B,KAAK,EACF,UAAU,GACV,SAAS,GACT,UAAU,GACV,OAAO,GACP,UAAU,GACV,iBAAiB,GACjB,iBAAiB,GACjB,IAAI,CAAC;QACR,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KACvB,GAAG,IAAI,CAAC;CACT;AAoGD,wBAAgB,SAAS,CAAC,IAAI,CAAC,EAAE,UAAU,8EA2D1C;AAED,4FAA4F;AAC5F,wBAAgB,aAAa,CAAC,IAAI,CAAC,EAAE,UAAU,8EAE9C"}
package/dist/index.js CHANGED
@@ -6,8 +6,10 @@ import { Hono } from "hono";
6
6
  import { createCipheriv, createDecipheriv, createHash, createPrivateKey, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
7
7
  import { createMiddleware } from "hono/factory";
8
8
  import { RateLimiter } from "limiter";
9
+ import { feedFullContextText, projectFeedItem } from "@codemem/core/feed-contract";
9
10
  import { and, count, desc, eq, inArray, max, ne, sql } from "drizzle-orm";
10
11
  import { drizzle } from "drizzle-orm/better-sqlite3";
12
+ import { Buffer as Buffer$1 } from "node:buffer";
11
13
  import { homedir, hostname, networkInterfaces } from "node:os";
12
14
  import * as z from "zod";
13
15
  import { chmod, link, mkdir, open, readFile, readdir, rm, stat } from "node:fs/promises";
@@ -2760,8 +2762,61 @@ function healthRoutes(getStore) {
2760
2762
  });
2761
2763
  return app;
2762
2764
  }
2765
+ function feedCursorFilterKey(options) {
2766
+ return createHash("sha256").update(JSON.stringify([
2767
+ options.classification,
2768
+ options.project ?? null,
2769
+ options.scope ?? null,
2770
+ options.q ?? null,
2771
+ options.actorId,
2772
+ options.dbPath
2773
+ ])).digest("hex");
2774
+ }
2775
+ function isFeedCursorPayload(value) {
2776
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
2777
+ const payload = value;
2778
+ return Object.keys(payload).length === 4 && payload.v === 1 && typeof payload.created_at === "string" && payload.created_at.length <= 256 && typeof payload.id === "number" && Number.isSafeInteger(payload.id) && payload.id > 0 && typeof payload.filter === "string" && /^[a-f0-9]{64}$/.test(payload.filter);
2779
+ }
2780
+ function tryEncodeFeedCursor(options) {
2781
+ const payload = {
2782
+ v: 1,
2783
+ created_at: options.position.created_at,
2784
+ id: options.position.id,
2785
+ filter: options.filter
2786
+ };
2787
+ if (!isFeedCursorPayload(payload)) return null;
2788
+ const token = Buffer$1.from(JSON.stringify(payload), "utf8").toString("base64url");
2789
+ if (token.length > 4096) return null;
2790
+ return token;
2791
+ }
2792
+ function decodeFeedCursor(options) {
2793
+ const { cursor, filter } = options;
2794
+ if (!cursor || cursor.length > 4096 || !/^[A-Za-z0-9_-]+$/.test(cursor)) return null;
2795
+ const bytes = Buffer$1.from(cursor, "base64url");
2796
+ if (bytes.toString("base64url") !== cursor) return null;
2797
+ const text = bytes.toString("utf8");
2798
+ if (!Buffer$1.from(text, "utf8").equals(bytes)) return null;
2799
+ let payload;
2800
+ try {
2801
+ payload = JSON.parse(text);
2802
+ } catch {
2803
+ return null;
2804
+ }
2805
+ if (!isFeedCursorPayload(payload) || payload.filter !== filter) return null;
2806
+ if (tryEncodeFeedCursor({
2807
+ position: payload,
2808
+ filter
2809
+ }) !== cursor) return null;
2810
+ return {
2811
+ created_at: payload.created_at,
2812
+ id: payload.id
2813
+ };
2814
+ }
2763
2815
  //#endregion
2764
2816
  //#region src/routes/memory.ts
2817
+ /**
2818
+ * Memory routes — observations, summaries, sessions, projects, pack, artifacts.
2819
+ */
2765
2820
  var MAX_FEED_QUERY_CODE_UNITS = 256;
2766
2821
  var searchFunctionsRegisteredStores = /* @__PURE__ */ new WeakSet();
2767
2822
  function casefold(value) {
@@ -3021,18 +3076,18 @@ function countVisibleArtifactRows(store, project) {
3021
3076
  const row = store.db.prepare(`SELECT COUNT(*) AS total FROM ${from} WHERE ${clauses.join(" AND ")}`).get(...params);
3022
3077
  return Number(row?.total ?? 0);
3023
3078
  }
3024
- function queryMemoryPage(store, options) {
3079
+ function buildMemoryReadQuery(store, options) {
3025
3080
  ensureMemorySearchFunctions(store);
3026
3081
  const filters = {};
3027
3082
  if (options.project) filters.project = options.project;
3028
3083
  if (options.scope) filters.ownership_scope = options.scope;
3029
3084
  const filterResult = buildViewerMemoryFilters(store, filters);
3030
- const summaryPredicate = summaryLikeSqlPredicate();
3031
- const clauses = [
3032
- "memory_items.active = 1",
3033
- options.classification === "summary" ? summaryPredicate : `NOT ${summaryPredicate}`,
3034
- ...filterResult.clauses
3035
- ];
3085
+ const clauses = ["memory_items.active = 1"];
3086
+ if (options.classification) {
3087
+ const summaryPredicate = summaryLikeSqlPredicate();
3088
+ clauses.push(options.classification === "summary" ? summaryPredicate : `NOT ${summaryPredicate}`);
3089
+ }
3090
+ clauses.push(...filterResult.clauses);
3036
3091
  const params = [...filterResult.params];
3037
3092
  if (options.q) {
3038
3093
  const search = memorySearchSql(options.q);
@@ -3040,14 +3095,163 @@ function queryMemoryPage(store, options) {
3040
3095
  params.push(...search.params);
3041
3096
  }
3042
3097
  const where = clauses.join(" AND ");
3043
- const from = filterResult.joinSessions ? "memory_items JOIN sessions ON sessions.id = memory_items.session_id" : "memory_items";
3098
+ return {
3099
+ from: filterResult.joinSessions ? "memory_items JOIN sessions ON sessions.id = memory_items.session_id" : "memory_items",
3100
+ where,
3101
+ params
3102
+ };
3103
+ }
3104
+ function queryMemoryPage(store, options) {
3105
+ const query = buildMemoryReadQuery(store, options);
3106
+ const { from, params } = query;
3107
+ let where = query.where;
3108
+ let limitClause = "LIMIT ?";
3109
+ if (options.paging.mode === "cursor" && options.paging.position) {
3110
+ where += " AND (memory_items.created_at, memory_items.id) < (?, ?)";
3111
+ params.push(options.paging.position.created_at, options.paging.position.id);
3112
+ }
3113
+ params.push(options.limit + 1);
3114
+ if (options.paging.mode === "offset") {
3115
+ limitClause += " OFFSET ?";
3116
+ params.push(options.paging.offset);
3117
+ }
3044
3118
  const rows = store.db.prepare(`SELECT memory_items.* FROM ${from}
3045
3119
  WHERE ${where}
3046
3120
  ORDER BY memory_items.created_at DESC, memory_items.id DESC
3047
- LIMIT ? OFFSET ?`).all(...params, options.limit + 1, options.offset);
3121
+ ${limitClause}`).all(...params);
3048
3122
  const ownedBySelf = store.buildOwnershipPredicate();
3049
3123
  return rows.map((row) => serializeMemoryRow(ownedBySelf, row));
3050
3124
  }
3125
+ function parseMemoryPaging(c, store, options) {
3126
+ if (c.req.query("paging") !== "cursor") return {
3127
+ mode: "offset",
3128
+ offset: Math.max(0, queryInt(c.req.query("offset"), 0))
3129
+ };
3130
+ const offset = c.req.query("offset");
3131
+ if (offset !== void 0 && offset !== "0") return null;
3132
+ const filter = feedCursorFilterKey({
3133
+ ...options,
3134
+ actorId: store.actorId,
3135
+ dbPath: store.dbPath
3136
+ });
3137
+ const cursor = c.req.query("cursor");
3138
+ if (cursor === void 0) return {
3139
+ mode: "cursor",
3140
+ filter,
3141
+ position: null
3142
+ };
3143
+ const position = decodeFeedCursor({
3144
+ cursor,
3145
+ filter
3146
+ });
3147
+ if (!position) return null;
3148
+ return {
3149
+ mode: "cursor",
3150
+ filter,
3151
+ position
3152
+ };
3153
+ }
3154
+ function memoryPagePagination(options) {
3155
+ const { paging, limit, hasMore, items } = options;
3156
+ if (paging.mode === "offset") return {
3157
+ limit,
3158
+ offset: paging.offset,
3159
+ next_offset: hasMore ? paging.offset + items.length : null,
3160
+ has_more: hasMore
3161
+ };
3162
+ const last = items.at(-1);
3163
+ const nextCursor = hasMore && last ? tryEncodeFeedCursor({
3164
+ position: {
3165
+ created_at: last.created_at,
3166
+ id: last.id
3167
+ },
3168
+ filter: paging.filter
3169
+ }) : null;
3170
+ if (hasMore && nextCursor === null) return null;
3171
+ return {
3172
+ limit,
3173
+ offset: null,
3174
+ next_offset: null,
3175
+ has_more: hasMore,
3176
+ paging: "cursor",
3177
+ next_cursor: nextCursor
3178
+ };
3179
+ }
3180
+ function handleMemoryListRequest(c, getStore, options) {
3181
+ if (c.req.query("paging") === "cursor") c.header("Cache-Control", "no-store");
3182
+ const store = getStore();
3183
+ const limit = Math.max(1, queryInt(c.req.query("limit"), options.defaultLimit));
3184
+ const q = normalizeMemorySearchQuery(c.req.query("q"));
3185
+ const readOptions = {
3186
+ project: c.req.query("project") || void 0,
3187
+ scope: normalizeScope(c.req.query("scope")),
3188
+ q,
3189
+ classification: options.classification
3190
+ };
3191
+ const paging = parseMemoryPaging(c, store, readOptions);
3192
+ if (!paging) return c.json({ error: "invalid_cursor" }, 400);
3193
+ const items = queryMemoryPage(store, {
3194
+ ...readOptions,
3195
+ limit,
3196
+ paging
3197
+ });
3198
+ const hasMore = items.length > limit;
3199
+ const result = hasMore ? items.slice(0, limit) : items;
3200
+ const pagination = memoryPagePagination({
3201
+ paging,
3202
+ limit,
3203
+ hasMore,
3204
+ items: result
3205
+ });
3206
+ if (pagination === null) return c.json({ error: "cursor_unavailable" }, 409);
3207
+ attachSessionFields(store, result);
3208
+ attachResolvedIdentityFields(store, result);
3209
+ return c.json({
3210
+ items: c.req.query("view") === "feed" ? feedResponseItems(result, q) : result,
3211
+ pagination
3212
+ });
3213
+ }
3214
+ function fullContextRevision(context) {
3215
+ return createHash("sha256").update(context).digest("hex");
3216
+ }
3217
+ function feedResponseItems(items, query) {
3218
+ return items.map((item) => {
3219
+ const context = feedFullContextText(item);
3220
+ return projectFeedItem({
3221
+ item,
3222
+ query,
3223
+ contextRevision: context === null ? "" : fullContextRevision(context)
3224
+ });
3225
+ });
3226
+ }
3227
+ function queryMemoryContextItem(store, id, options) {
3228
+ const { from, where, params } = buildMemoryReadQuery(store, options);
3229
+ const row = store.db.prepare(`SELECT memory_items.* FROM ${from} WHERE ${where} AND memory_items.id = ?`).get(...params, id);
3230
+ if (!row) return null;
3231
+ return serializeMemoryRow(store.buildOwnershipPredicate(), row);
3232
+ }
3233
+ function handleMemoryContextRequest(c, getStore) {
3234
+ c.header("Cache-Control", "no-store");
3235
+ const rawId = c.req.param("id") ?? "";
3236
+ const id = Number(rawId);
3237
+ if (!/^[1-9][0-9]*$/.test(rawId) || !Number.isSafeInteger(id)) return c.json({ error: "id must be a positive safe integer" }, 400);
3238
+ const classification = c.req.query("classification");
3239
+ if (classification !== void 0 && classification !== "observation" && classification !== "summary") return c.json({ error: "classification must be observation or summary" }, 400);
3240
+ const item = queryMemoryContextItem(getStore(), id, {
3241
+ project: c.req.query("project") || void 0,
3242
+ scope: normalizeScope(c.req.query("scope")),
3243
+ q: normalizeMemorySearchQuery(c.req.query("q")),
3244
+ classification
3245
+ });
3246
+ const context = item ? feedFullContextText(item) : null;
3247
+ if (context === null) return c.json({ error: "memory not found" }, 404);
3248
+ const response = {
3249
+ id,
3250
+ revision: fullContextRevision(context),
3251
+ context
3252
+ };
3253
+ return c.json(response);
3254
+ }
3051
3255
  async function handleLegacyPackRequest(c, getStore) {
3052
3256
  const store = getStore();
3053
3257
  const context = c.req.query("context") || "";
@@ -3108,64 +3312,14 @@ function memoryRoutes(getStore) {
3108
3312
  const search = new URL(c.req.url).search;
3109
3313
  return c.redirect(`/api/observations${search}`, 301);
3110
3314
  });
3111
- app.get("/api/observations", (c) => {
3112
- const store = getStore();
3113
- {
3114
- const limit = Math.max(1, queryInt(c.req.query("limit"), 20));
3115
- const offset = Math.max(0, queryInt(c.req.query("offset"), 0));
3116
- const items = queryMemoryPage(store, {
3117
- limit,
3118
- offset,
3119
- project: c.req.query("project") || void 0,
3120
- scope: normalizeScope(c.req.query("scope")),
3121
- q: normalizeMemorySearchQuery(c.req.query("q")),
3122
- classification: "observation"
3123
- });
3124
- const hasMore = items.length > limit;
3125
- const result = hasMore ? items.slice(0, limit) : items;
3126
- const asRecords = result;
3127
- attachSessionFields(store, asRecords);
3128
- attachResolvedIdentityFields(store, asRecords);
3129
- return c.json({
3130
- items: asRecords,
3131
- pagination: {
3132
- limit,
3133
- offset,
3134
- next_offset: hasMore ? offset + result.length : null,
3135
- has_more: hasMore
3136
- }
3137
- });
3138
- }
3139
- });
3140
- app.get("/api/summaries", (c) => {
3141
- const store = getStore();
3142
- {
3143
- const limit = Math.max(1, queryInt(c.req.query("limit"), 50));
3144
- const offset = Math.max(0, queryInt(c.req.query("offset"), 0));
3145
- const items = queryMemoryPage(store, {
3146
- limit,
3147
- offset,
3148
- project: c.req.query("project") || void 0,
3149
- scope: normalizeScope(c.req.query("scope")),
3150
- q: normalizeMemorySearchQuery(c.req.query("q")),
3151
- classification: "summary"
3152
- });
3153
- const hasMore = items.length > limit;
3154
- const result = hasMore ? items.slice(0, limit) : items;
3155
- const asRecords = result;
3156
- attachSessionFields(store, asRecords);
3157
- attachResolvedIdentityFields(store, asRecords);
3158
- return c.json({
3159
- items: asRecords,
3160
- pagination: {
3161
- limit,
3162
- offset,
3163
- next_offset: hasMore ? offset + result.length : null,
3164
- has_more: hasMore
3165
- }
3166
- });
3167
- }
3168
- });
3315
+ app.get("/api/observations", (c) => handleMemoryListRequest(c, getStore, {
3316
+ classification: "observation",
3317
+ defaultLimit: 20
3318
+ }));
3319
+ app.get("/api/summaries", (c) => handleMemoryListRequest(c, getStore, {
3320
+ classification: "summary",
3321
+ defaultLimit: 50
3322
+ }));
3169
3323
  app.get("/api/session", (c) => {
3170
3324
  const store = getStore();
3171
3325
  {
@@ -3219,6 +3373,7 @@ function memoryRoutes(getStore) {
3219
3373
  ]);
3220
3374
  return c.json(trace);
3221
3375
  });
3376
+ app.get("/api/memory/:id/context", (c) => handleMemoryContextRequest(c, getStore));
3222
3377
  app.get("/api/memory", (c) => {
3223
3378
  const store = getStore();
3224
3379
  {
@@ -17696,6 +17851,7 @@ function buildClient(deps, config) {
17696
17851
  },
17697
17852
  authorityId: config.authorityId,
17698
17853
  groupId: config.groupId,
17854
+ pairPacing: options.pairPacingByGroup?.get(config.groupId),
17699
17855
  relayAudienceId: config.relayAudienceId,
17700
17856
  externalPathname: config.externalPathname,
17701
17857
  coordinator: createRelayCoordinatorClient({
@@ -18022,6 +18178,17 @@ function registerViewerBoundary(app, opts) {
18022
18178
  registry: options.registry
18023
18179
  }));
18024
18180
  }
18181
+ function registerViewerTargetGuards(options) {
18182
+ const { app, storeFactory } = options;
18183
+ const viewerTargetGuard = viewerTargetQueryGuard(storeFactory);
18184
+ app.use("/api/memories/*", viewerTargetGuard);
18185
+ app.use("/api/memory", viewerTargetGuard);
18186
+ app.use("/api/memory/:id/context", (c, next) => {
18187
+ c.header("Cache-Control", "no-store");
18188
+ return viewerTargetGuard(c, next);
18189
+ });
18190
+ app.use("/api/pi/sessions/search", viewerTargetGuard);
18191
+ }
18025
18192
  function createApp(opts) {
18026
18193
  const storeFactory = opts?.storeFactory ?? getStore;
18027
18194
  const sweeper = opts?.sweeper ?? null;
@@ -18031,10 +18198,10 @@ function createApp(opts) {
18031
18198
  registerViewerBoundary(app, opts);
18032
18199
  app.use("*", preflightHandler());
18033
18200
  app.use("*", originGuard({ unsafeGetPathPrefixes: [TEAM_SETUP_ROUTE_PREFIX] }));
18034
- const viewerTargetGuard = viewerTargetQueryGuard(storeFactory);
18035
- app.use("/api/memories/*", viewerTargetGuard);
18036
- app.use("/api/memory", viewerTargetGuard);
18037
- app.use("/api/pi/sessions/search", viewerTargetGuard);
18201
+ registerViewerTargetGuards({
18202
+ app,
18203
+ storeFactory
18204
+ });
18038
18205
  app.route("/", healthRoutes(storeFactory));
18039
18206
  app.route("/", diagnosticsRoutes(storeFactory));
18040
18207
  app.route("/", statsRoutes(storeFactory));