@simplelayer/graniteer-core 0.3.8 → 0.3.10

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.
@@ -28,9 +28,8 @@ import { checkConductorRateLimit, fetchConductorKey, insertConductorEvent, } fro
28
28
  const MODEL = 'claude-haiku-4-5-20251001';
29
29
  /** Short Q&A answer, not a report — deliberately not ls-quarterly-brief's 8000. */
30
30
  const MAX_TOKENS = 1024;
31
- const KB_LIMIT = 30;
32
- /** Per-article body cap, so one long runbook can't crowd out the other 29. */
33
- const KB_BODY_CHARS = 2500;
31
+ /** Per-article body cap. At most eight ranked articles are loaded, so each can keep more of its text. */
32
+ const KB_BODY_CHARS = 6000;
34
33
  const QUESTION_MAX_CHARS = 2000;
35
34
  /**
36
35
  * Bound on what gets stored in the audit row. MAX_TOKENS = 1024 caps a real
@@ -271,19 +270,43 @@ export async function handle(req, operator) {
271
270
  // future third audience value cannot silently start appearing in members'
272
271
  // prompts before anyone revisits this line.
273
272
  //
274
- // KNOWN v1 LIMITATION, not an oversight: "the 30 most recently updated
275
- // articles" is the whole of the relevance ranking. There is no search index or
276
- // embedding store in this repo to rank against, so an article that would have
277
- // answered the question can fall outside the window. Out of scope for v1.
273
+ // Ranking is public.search_kb_articles(), called on this same caller-JWT
274
+ // client. The function is SECURITY INVOKER, so RLS stays the authorization
275
+ // boundary; `audiences` is the same narrowing filter as the old select
276
+ // (member: ['member'], staff: ['admin','member']) and is applied again inside
277
+ // the function. A full-text miss falls back to newest-first there — still
278
+ // inside that audience list, still capped. This package does not apply that
279
+ // SQL. PGRST202 (function not in the PostgREST schema cache) means the
280
+ // operator has not applied the migration yet: fall back in-process to the
281
+ // previous newest-30 table select on the same caller-JWT client, so an
282
+ // upgrade without the RPC does not silently wipe the knowledge base. Any
283
+ // other RPC error, or a payload that is not an article array, still takes
284
+ // the fail-soft path below rather than telling the model the library is empty.
278
285
  const kbAudiences = actorType === 'staff' ? ['admin', 'member'] : ['member'];
279
- const { data: kbRows, error: kbErr } = await reader
280
- .from('kb_articles')
281
- .select('slug,title,audience,tool,tags,body_markdown')
282
- .in('audience', kbAudiences)
283
- .order('updated_at', { ascending: false })
284
- .limit(KB_LIMIT);
286
+ let { data: kbRows, error: kbErr } = await reader.rpc('search_kb_articles', {
287
+ q: question,
288
+ audiences: kbAudiences,
289
+ max_rows: 8,
290
+ });
291
+ if (kbErr?.code === 'PGRST202') {
292
+ console.warn('[conductor] search_kb_articles missing (PGRST202); falling back to newest-30 table select');
293
+ const fallback = await reader
294
+ .from('kb_articles')
295
+ .select('slug,title,audience,tool,tags,body_markdown')
296
+ .in('audience', kbAudiences)
297
+ .order('updated_at', { ascending: false })
298
+ .limit(30);
299
+ kbRows = fallback.data;
300
+ kbErr = fallback.error;
301
+ }
302
+ // null / non-array is "returned nothing", distinct from a real empty list
303
+ // (no articles in this audience). Only the former is a failed load.
304
+ const kbList = Array.isArray(kbRows) ? kbRows : null;
285
305
  if (kbErr)
286
306
  console.warn('[conductor] kb_articles read failed', kbErr.message);
307
+ else if (kbList === null)
308
+ console.warn('[conductor] kb_articles read failed', 'search_kb_articles returned nothing');
309
+ const kbUnavailable = Boolean(kbErr) || kbList === null;
287
310
  // Own-record context is member-only in v1. Staff Conductor answers
288
311
  // wayfinding from kb_articles only — there is no "which member" input, and
289
312
  // building a staff member-lookup is explicitly out of scope.
@@ -337,10 +360,10 @@ export async function handle(req, operator) {
337
360
  const open = `<<<${fence}>>>`;
338
361
  const close = `<<<END ${fence}>>>`;
339
362
  const fenced = (label, body) => [`## ${label}`, open, body, close].join('\n');
340
- const kbBlock = kbErr
363
+ const kbBlock = kbUnavailable
341
364
  ? '(Help articles could NOT be loaded for this request. This is not a statement that none exist.)'
342
- : (kbRows ?? []).length
343
- ? (kbRows ?? [])
365
+ : kbList.length
366
+ ? kbList
344
367
  .map((a) => {
345
368
  const tags = Array.isArray(a.tags) ? a.tags.join(', ') : '';
346
369
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplelayer/graniteer-core",
3
- "version": "0.3.8",
3
+ "version": "0.3.10",
4
4
  "description": "Shared platform core for Graniteer",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",