@chatman-media/kb 1.3.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 (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +169 -0
  3. package/dist/ab-router.d.ts +66 -0
  4. package/dist/ab-router.d.ts.map +1 -0
  5. package/dist/answer-types.d.ts +194 -0
  6. package/dist/answer-types.d.ts.map +1 -0
  7. package/dist/answer.d.ts +59 -0
  8. package/dist/answer.d.ts.map +1 -0
  9. package/dist/built-in-tools/calendly.d.ts +19 -0
  10. package/dist/built-in-tools/calendly.d.ts.map +1 -0
  11. package/dist/chunk.d.ts +48 -0
  12. package/dist/chunk.d.ts.map +1 -0
  13. package/dist/conversation-store.d.ts +76 -0
  14. package/dist/conversation-store.d.ts.map +1 -0
  15. package/dist/eval.d.ts +64 -0
  16. package/dist/eval.d.ts.map +1 -0
  17. package/dist/extract-user-facts.d.ts +27 -0
  18. package/dist/extract-user-facts.d.ts.map +1 -0
  19. package/dist/fact-checker.d.ts +46 -0
  20. package/dist/fact-checker.d.ts.map +1 -0
  21. package/dist/grade-skills.d.ts +29 -0
  22. package/dist/grade-skills.d.ts.map +1 -0
  23. package/dist/index.d.ts +76 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +62655 -0
  26. package/dist/ingest.d.ts +49 -0
  27. package/dist/ingest.d.ts.map +1 -0
  28. package/dist/multi-query.d.ts +29 -0
  29. package/dist/multi-query.d.ts.map +1 -0
  30. package/dist/parse-pdf.d.ts +14 -0
  31. package/dist/parse-pdf.d.ts.map +1 -0
  32. package/dist/persona-shortcuts.d.ts +51 -0
  33. package/dist/persona-shortcuts.d.ts.map +1 -0
  34. package/dist/prompt.d.ts +9 -0
  35. package/dist/prompt.d.ts.map +1 -0
  36. package/dist/reflect.d.ts +29 -0
  37. package/dist/reflect.d.ts.map +1 -0
  38. package/dist/reranker.d.ts +71 -0
  39. package/dist/reranker.d.ts.map +1 -0
  40. package/dist/retrieval-utils.d.ts +94 -0
  41. package/dist/retrieval-utils.d.ts.map +1 -0
  42. package/dist/retry.d.ts +53 -0
  43. package/dist/retry.d.ts.map +1 -0
  44. package/dist/rewrite-query.d.ts +30 -0
  45. package/dist/rewrite-query.d.ts.map +1 -0
  46. package/dist/sanitize.d.ts +21 -0
  47. package/dist/sanitize.d.ts.map +1 -0
  48. package/dist/semantic-cache.d.ts +70 -0
  49. package/dist/semantic-cache.d.ts.map +1 -0
  50. package/dist/server.d.ts +77 -0
  51. package/dist/server.d.ts.map +1 -0
  52. package/dist/stores/memory-store.d.ts +72 -0
  53. package/dist/stores/memory-store.d.ts.map +1 -0
  54. package/dist/structured-output.d.ts +21 -0
  55. package/dist/structured-output.d.ts.map +1 -0
  56. package/dist/styles.d.ts +186 -0
  57. package/dist/styles.d.ts.map +1 -0
  58. package/dist/summarize-conversation.d.ts +31 -0
  59. package/dist/summarize-conversation.d.ts.map +1 -0
  60. package/dist/system-prompt.d.ts +11 -0
  61. package/dist/system-prompt.d.ts.map +1 -0
  62. package/dist/text-style-rules.d.ts +133 -0
  63. package/dist/text-style-rules.d.ts.map +1 -0
  64. package/dist/tool-loop.d.ts +44 -0
  65. package/dist/tool-loop.d.ts.map +1 -0
  66. package/dist/tools.d.ts +64 -0
  67. package/dist/tools.d.ts.map +1 -0
  68. package/dist/topic-classifier.d.ts +11 -0
  69. package/dist/topic-classifier.d.ts.map +1 -0
  70. package/dist/types.d.ts +83 -0
  71. package/dist/types.d.ts.map +1 -0
  72. package/dist/utils.d.ts +19 -0
  73. package/dist/utils.d.ts.map +1 -0
  74. package/dist/vision.d.ts +72 -0
  75. package/dist/vision.d.ts.map +1 -0
  76. package/package.json +76 -0
  77. package/src/ab-router.ts +118 -0
  78. package/src/answer-types.ts +191 -0
  79. package/src/answer.ts +696 -0
  80. package/src/built-in-tools/calendly.ts +32 -0
  81. package/src/chunk.ts +198 -0
  82. package/src/conversation-store.ts +138 -0
  83. package/src/eval.ts +127 -0
  84. package/src/extract-user-facts.ts +120 -0
  85. package/src/fact-checker.ts +171 -0
  86. package/src/grade-skills.ts +79 -0
  87. package/src/index.ts +191 -0
  88. package/src/ingest.ts +193 -0
  89. package/src/multi-query.ts +89 -0
  90. package/src/parse-pdf.ts +24 -0
  91. package/src/persona-shortcuts.ts +255 -0
  92. package/src/prompt.ts +190 -0
  93. package/src/reflect.ts +99 -0
  94. package/src/reranker.ts +166 -0
  95. package/src/retrieval-utils.ts +209 -0
  96. package/src/retry.ts +139 -0
  97. package/src/rewrite-query.ts +124 -0
  98. package/src/sanitize.ts +44 -0
  99. package/src/semantic-cache.ts +154 -0
  100. package/src/server.ts +164 -0
  101. package/src/stores/memory-store.ts +249 -0
  102. package/src/structured-output.ts +47 -0
  103. package/src/styles.ts +138 -0
  104. package/src/summarize-conversation.ts +88 -0
  105. package/src/system-prompt.ts +118 -0
  106. package/src/text-style-rules.ts +244 -0
  107. package/src/tool-loop.ts +110 -0
  108. package/src/tools.ts +79 -0
  109. package/src/topic-classifier.ts +112 -0
  110. package/src/types.ts +91 -0
  111. package/src/utils.ts +81 -0
  112. package/src/vision.ts +265 -0
@@ -0,0 +1,76 @@
1
+ import type { ChatMessage } from "@chatman-media/llm-router";
2
+ /**
3
+ * Unified interface for persisting conversation history and summaries.
4
+ *
5
+ * Implement this for your storage backend (PostgreSQL, Redis, SQLite, …).
6
+ * An in-memory reference implementation is exported as `InMemoryConversationStore`.
7
+ */
8
+ export interface IConversationStore {
9
+ /** Append one message to the conversation history. */
10
+ addMessage(conversationId: string, message: ChatMessage): Promise<void>;
11
+ /** Return messages in chronological order. Returns [] for unknown ids. */
12
+ getHistory(conversationId: string): Promise<ChatMessage[]>;
13
+ /**
14
+ * Replace the stored history with a shorter version (e.g. after summarisation).
15
+ * Typically called with the last N messages after older turns are compressed.
16
+ */
17
+ setHistory(conversationId: string, messages: ChatMessage[]): Promise<void>;
18
+ /** Store or update the running summary for the conversation. */
19
+ setSummary(conversationId: string, summary: string): Promise<void>;
20
+ /** Retrieve the current summary, or null if none exists. */
21
+ getSummary(conversationId: string): Promise<string | null>;
22
+ /** Remove all data for a conversation (GDPR deletion, session reset, …). */
23
+ deleteConversation(conversationId: string): Promise<void>;
24
+ }
25
+ /**
26
+ * Zero-dependency in-memory `IConversationStore`.
27
+ *
28
+ * Suitable for tests, prototypes, and single-process deployments.
29
+ * Data is lost on process restart — use a database-backed implementation
30
+ * for production.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import {
35
+ * InMemoryConversationStore,
36
+ * summarizeConversation,
37
+ * answerWithRag,
38
+ * } from "@chatman-media/kb";
39
+ *
40
+ * const store = new InMemoryConversationStore({ maxHistoryLength: 20, ttlMs: 2 * 60 * 60_000 });
41
+ *
42
+ * // Each turn:
43
+ * const history = await store.getHistory(userId);
44
+ * const summary = await store.getSummary(userId);
45
+ * const result = await answerWithRag({ question, kb, chat, embedder, history, conversationSummary: summary ?? undefined });
46
+ * await store.addMessage(userId, { role: "user", content: question });
47
+ * await store.addMessage(userId, { role: "assistant", content: result.text });
48
+ *
49
+ * // Compress when history grows long:
50
+ * if (history.length > 30) {
51
+ * const newSummary = await summarizeConversation({ history, chat });
52
+ * await store.setSummary(userId, newSummary);
53
+ * await store.setHistory(userId, history.slice(-10));
54
+ * }
55
+ * ```
56
+ */
57
+ export declare class InMemoryConversationStore implements IConversationStore {
58
+ private readonly data;
59
+ private readonly maxHistoryLength;
60
+ private readonly ttlMs;
61
+ constructor(opts?: {
62
+ maxHistoryLength?: number;
63
+ ttlMs?: number;
64
+ });
65
+ addMessage(conversationId: string, message: ChatMessage): Promise<void>;
66
+ getHistory(conversationId: string): Promise<ChatMessage[]>;
67
+ setHistory(conversationId: string, messages: ChatMessage[]): Promise<void>;
68
+ setSummary(conversationId: string, summary: string): Promise<void>;
69
+ getSummary(conversationId: string): Promise<string | null>;
70
+ deleteConversation(conversationId: string): Promise<void>;
71
+ /** Number of active (non-expired) conversations. */
72
+ get size(): number;
73
+ private getOrCreate;
74
+ private isExpired;
75
+ }
76
+ //# sourceMappingURL=conversation-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation-store.d.ts","sourceRoot":"","sources":["../src/conversation-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC,sDAAsD;IACtD,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAExE,0EAA0E;IAC1E,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAE3D;;;OAGG;IACH,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3E,gEAAgE;IAChE,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnE,4DAA4D;IAC5D,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAE3D,4EAA4E;IAC5E,kBAAkB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3D;AAQD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,yBAA0B,YAAW,kBAAkB;IAClE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAuC;IAC5D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAS;IAC1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBAEnB,IAAI,GAAE;QAAE,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAO;IAK9D,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IASvE,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAM1D,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAM1E,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMlE,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAM1D,kBAAkB,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/D,oDAAoD;IACpD,IAAI,IAAI,IAAI,MAAM,CAMjB;IAED,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,SAAS;CAGlB"}
package/dist/eval.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ import type { KbSearchHit } from "./types.ts";
2
+ /**
3
+ * One labelled query for retrieval evaluation.
4
+ * `relevantChunkIds` are the ground-truth chunk ids that should be retrieved.
5
+ */
6
+ export interface EvalQuery {
7
+ question: string;
8
+ relevantChunkIds: number[];
9
+ }
10
+ /** Per-query metrics returned by `evalRetrieval`. */
11
+ export interface QueryMetrics {
12
+ question: string;
13
+ /** Fraction of relevant chunks found in the top-k results (0–1). */
14
+ recallAtK: number;
15
+ /** Mean Reciprocal Rank — position of the first relevant hit (0–1). */
16
+ mrr: number;
17
+ /** Normalised Discounted Cumulative Gain (0–1). */
18
+ ndcg: number;
19
+ /** Chunk ids that were retrieved. */
20
+ retrievedIds: number[];
21
+ }
22
+ /** Aggregated metrics over a full eval set. */
23
+ export interface EvalResult {
24
+ /** Arithmetic mean of recall@k across all queries. */
25
+ meanRecallAtK: number;
26
+ /** Arithmetic mean of MRR across all queries. */
27
+ meanMrr: number;
28
+ /** Arithmetic mean of NDCG across all queries. */
29
+ meanNdcg: number;
30
+ /** Per-query breakdown. */
31
+ queries: QueryMetrics[];
32
+ }
33
+ /**
34
+ * Evaluate retrieval quality over a labelled query set.
35
+ *
36
+ * Runs `retrieve(query)` for each `EvalQuery`, compares the returned chunk ids
37
+ * against the ground-truth relevant ids, and reports recall@k, MRR, and NDCG.
38
+ *
39
+ * @param queries Labelled queries with ground-truth chunk ids.
40
+ * @param retrieve Function that takes a question and returns `KbSearchHit[]`.
41
+ * Wrap your `kb.search()` / `kb.hybridSearch()` call here.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { evalRetrieval } from "@chatman-media/kb";
46
+ *
47
+ * const result = await evalRetrieval(
48
+ * [
49
+ * { question: "Какая зарплата в Дубае?", relevantChunkIds: [12, 15] },
50
+ * { question: "Нужна ли виза?", relevantChunkIds: [7] },
51
+ * ],
52
+ * async (q) => {
53
+ * const [vec] = await embedder.embed([q]);
54
+ * return kb.hybridSearch({ embedding: vec!, query: q, k: 5 });
55
+ * },
56
+ * );
57
+ *
58
+ * console.log(`recall@5: ${result.meanRecallAtK.toFixed(3)}`);
59
+ * console.log(`MRR: ${result.meanMrr.toFixed(3)}`);
60
+ * console.log(`NDCG: ${result.meanNdcg.toFixed(3)}`);
61
+ * ```
62
+ */
63
+ export declare function evalRetrieval(queries: EvalQuery[], retrieve: (question: string) => Promise<KbSearchHit[]>): Promise<EvalResult>;
64
+ //# sourceMappingURL=eval.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eval.d.ts","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B;AAED,qDAAqD;AACrD,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,uEAAuE;IACvE,GAAG,EAAE,MAAM,CAAC;IACZ,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,+CAA+C;AAC/C,MAAM,WAAW,UAAU;IACzB,sDAAsD;IACtD,aAAa,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC;IAChB,kDAAkD;IAClD,QAAQ,EAAE,MAAM,CAAC;IACjB,2BAA2B;IAC3B,OAAO,EAAE,YAAY,EAAE,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,SAAS,EAAE,EACpB,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,WAAW,EAAE,CAAC,GACrD,OAAO,CAAC,UAAU,CAAC,CA0BrB"}
@@ -0,0 +1,27 @@
1
+ import type { ChatClient, ChatMessage } from "@chatman-media/llm-router";
2
+ /**
3
+ * Extracts persistent facts ABOUT THE CANDIDATE (not the bot persona) from
4
+ * a slice of conversation. Used by the cross-session memory layer in
5
+ * webhook → after each turn the new messages are passed here and the result
6
+ * merged into `users.profile_json.memory.facts`.
7
+ *
8
+ * Facts schema is flexible — the prompt encourages the most common keys for
9
+ * the recruitment use-case but allows free-form keys when the candidate
10
+ * volunteers something unusual ("instagram", "previous_agency", …). Only
11
+ * volunteered facts are returned — no inference or guessing.
12
+ */
13
+ export interface ExtractFactsInput {
14
+ /** Recent messages, oldest first. Should include both user and bot turns. */
15
+ messages: ChatMessage[];
16
+ chat: ChatClient;
17
+ /** Existing facts so the LLM doesn't re-emit already-known data. */
18
+ existingFacts?: Record<string, string>;
19
+ }
20
+ export declare function extractUserFacts(input: ExtractFactsInput): Promise<Record<string, string>>;
21
+ /**
22
+ * Parses the LLM output — strips think-tags and markdown fences, finds the
23
+ * JSON object, validates that values are short strings, caps total keys.
24
+ * Exported for unit tests.
25
+ */
26
+ export declare function parseFactsFromLlmOutput(raw: string): Record<string, string>;
27
+ //# sourceMappingURL=extract-user-facts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-user-facts.d.ts","sourceRoot":"","sources":["../src/extract-user-facts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGzE;;;;;;;;;;GAUG;AACH,MAAM,WAAW,iBAAiB;IAChC,6EAA6E;IAC7E,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,IAAI,EAAE,UAAU,CAAC;IACjB,oEAAoE;IACpE,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxC;AAmCD,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAwBhG;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiC3E"}
@@ -0,0 +1,46 @@
1
+ import type { ChatClient } from "@chatman-media/llm-router";
2
+ /**
3
+ * Unified fact-checker — replaces the separate reflect.ts + vacancy-guard.ts
4
+ * pipeline with a single LLM call that checks both:
5
+ *
6
+ * 1. KB grounding: every concrete fact in the answer is supported by the
7
+ * retrieved KB context (no hallucinations).
8
+ *
9
+ * 2. Vacancy accuracy: any salary/city/condition data matches the
10
+ * authoritative АКТУАЛЬНЫЕ ВАКАНСИИ block from the DB table.
11
+ * ВАКАНСИИ always win over KB chunks when they conflict — an old
12
+ * chat log with a stale salary is not a valid grounding source.
13
+ *
14
+ * Cost: 1 LLM call per turn (was 2 with separate reflect + vacancy-guard).
15
+ *
16
+ * **Fail-closed by default**: on any LLM or parse error returns
17
+ * `{ grounded: false, vacancyOk: false, reason: "checker_error: …" }` so the
18
+ * caller treats the reply as ungrounded and escalates to silence/operator
19
+ * instead of shipping a potentially-hallucinated answer. The legacy
20
+ * fail-open behaviour can be restored with `RAG_FACT_CHECKER_FAIL_OPEN=1`
21
+ * (kept for test fixtures that drive the checker with mock failures).
22
+ */
23
+ export interface FactCheckInput {
24
+ question: string;
25
+ answer: string;
26
+ /** KB chunks concatenated — same string passed to the generator. */
27
+ context: string;
28
+ chat: ChatClient;
29
+ /** Optional: rendered АКТУАЛЬНЫЕ ВАКАНСИИ block.
30
+ * When provided, vacancy facts are checked against it as the authoritative
31
+ * source. KB chunks that contradict it are treated as outdated. */
32
+ vacanciesBlock?: string;
33
+ }
34
+ export interface FactCheckResult {
35
+ /** All concrete facts in the answer are found in context (or vacanciesBlock). */
36
+ grounded: boolean;
37
+ /** All vacancy-specific facts (salary, city, conditions) match vacanciesBlock.
38
+ * Always true when vacanciesBlock was not provided. */
39
+ vacancyOk: boolean;
40
+ /** Human-readable reason when grounded=false or vacancyOk=false. */
41
+ reason?: string;
42
+ }
43
+ export declare function checkFacts(input: FactCheckInput): Promise<FactCheckResult>;
44
+ /** Exported for unit tests. */
45
+ export declare function parseFactCheckResult(raw: string): FactCheckResult;
46
+ //# sourceMappingURL=fact-checker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fact-checker.d.ts","sourceRoot":"","sources":["../src/fact-checker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAe,MAAM,2BAA2B,CAAC;AAGzE;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,oEAAoE;IACpE,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,UAAU,CAAC;IACjB;;wEAEoE;IACpE,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,eAAe;IAC9B,iFAAiF;IACjF,QAAQ,EAAE,OAAO,CAAC;IAClB;4DACwD;IACxD,SAAS,EAAE,OAAO,CAAC;IACnB,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AA0DD,wBAAsB,UAAU,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAyChF;AAED,+BAA+B;AAC/B,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAuBjE"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * After the bot has generated a reply, optionally ask a small LLM to
3
+ * inspect the (system_prompt → user → assistant) triple and report which
4
+ * of the configured skills (slugs) it actually used. Output goes into
5
+ * `messages.meta_json.telemetry.skills_used` for downstream attribution
6
+ * (Phase 2: ELO over outcomes; Phase 3: self-play).
7
+ *
8
+ * Cost: +1 LLM call per assistant turn. Gated behind RAG_SKILL_GRADING=true
9
+ * because for high-volume single-style production it's overkill — flip on
10
+ * during A/B research windows, off in steady state.
11
+ *
12
+ * Failure-soft: any error → returns empty array, never throws. The whole
13
+ * point is post-hoc analytics, not a hard dependency.
14
+ */
15
+ import type { ChatClient } from "@chatman-media/llm-router";
16
+ export interface GradeSkillsInput {
17
+ question: string;
18
+ reply: string;
19
+ /** Slugs offered to the model in the system prompt (only these are valid). */
20
+ availableSlugs: readonly string[];
21
+ chat: ChatClient;
22
+ /** Lightweight model id — falls back to the chat client's default if undefined. */
23
+ model?: string;
24
+ }
25
+ export declare function gradeSkills(input: GradeSkillsInput): Promise<string[]>;
26
+ /** Tolerant parser — accepts a bare JSON array, code-fenced JSON, or
27
+ * comma-separated text. Filters to allowed slugs. Exported for tests. */
28
+ export declare function parseSlugList(raw: string, allowed: readonly string[]): string[];
29
+ //# sourceMappingURL=grade-skills.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grade-skills.d.ts","sourceRoot":"","sources":["../src/grade-skills.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,8EAA8E;IAC9E,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,IAAI,EAAE,UAAU,CAAC;IACjB,mFAAmF;IACnF,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAKD,wBAAsB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAsB5E;AAED;0EAC0E;AAC1E,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAuB/E"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * @chatman-media/kb — production-grade RAG engine for conversational bots.
3
+ *
4
+ * Quick start:
5
+ *
6
+ * ```ts
7
+ * import { answerWithRag, OpenAIChatClient, OpenAIEmbeddingClient } from "@chatman-media/kb";
8
+ *
9
+ * const chat = new OpenAIChatClient({ apiKey, baseUrl, model: "gpt-4o-mini" });
10
+ * const embedder = new OpenAIEmbeddingClient({ apiKey, baseUrl, model: "text-embedding-3-small", dim: 1536 });
11
+ *
12
+ * const result = await answerWithRag({ question, kb: myKbStore, chat, embedder });
13
+ * console.log(result.text);
14
+ * ```
15
+ *
16
+ * See README.md for full usage and `IKbStore` implementation guide.
17
+ */
18
+ export type { ABRouterOptions, ABVariant } from "./ab-router.ts";
19
+ export { ABRouter } from "./ab-router.ts";
20
+ export { answerWithRag, answerWithRagStream, generateSoftFallback, retrieveHits, type RetrievalResult, } from "./answer.ts";
21
+ export type { AnswerInput, AnswerResult, AnswerTelemetry, Persona } from "./answer-types.ts";
22
+ export { NO_CONTEXT_MARKER } from "./answer-types.ts";
23
+ export type { ChatClient, ChatCompletionOpts, ChatMessage, ChatRole, } from "@chatman-media/llm-router";
24
+ export { ChatApiError, OpenAIChatClient } from "@chatman-media/llm-router";
25
+ export type { Chunk, ChunkOptions, SectionChunk } from "./chunk.ts";
26
+ export { chunkBySections, chunkText, estimateTokens } from "./chunk.ts";
27
+ export type { IConversationStore } from "./conversation-store.ts";
28
+ export { InMemoryConversationStore } from "./conversation-store.ts";
29
+ export type { EmbeddingClient } from "@chatman-media/llm-router";
30
+ export { EmbeddingApiError, NullEmbeddingClient, OpenAIEmbeddingClient, } from "@chatman-media/llm-router";
31
+ export type { EvalQuery, EvalResult, QueryMetrics } from "./eval.ts";
32
+ export { evalRetrieval } from "./eval.ts";
33
+ export type { ExtractFactsInput } from "./extract-user-facts.ts";
34
+ export { extractUserFacts, parseFactsFromLlmOutput } from "./extract-user-facts.ts";
35
+ export type { FactCheckInput, FactCheckResult } from "./fact-checker.ts";
36
+ export { checkFacts, parseFactCheckResult } from "./fact-checker.ts";
37
+ export type { GradeSkillsInput } from "./grade-skills.ts";
38
+ export { gradeSkills } from "./grade-skills.ts";
39
+ export type { IngestDeps, IngestDirectorySummary, IngestFileResult } from "./ingest.ts";
40
+ export { deriveTopicFromPath, ingestDirectory, ingestFile, ingestText, stripNonContent, } from "./ingest.ts";
41
+ export { parsePdf, parsePdfBuffer } from "./parse-pdf.ts";
42
+ export { botPresenceReply, isBotPresenceQuestion, isPersonalFactQuestion, isPersonaSmalltalkQuestion, personaFactReply, personaSmalltalkReply, } from "./persona-shortcuts.ts";
43
+ export { composeSystemPrompt } from "./prompt.ts";
44
+ export type { OllamaChatOptions, OllamaEmbeddingOptions, OpenRouterChatOptions, } from "@chatman-media/llm-router";
45
+ export { OllamaChatClient, OllamaEmbeddingClient, OpenRouterChatClient, } from "@chatman-media/llm-router";
46
+ export type { ReflectInput, ReflectResult } from "./reflect.ts";
47
+ export { parseReflection, verifyAnswer } from "./reflect.ts";
48
+ export type { CohereRerankerOptions, JinaRerankerOptions, Reranker } from "./reranker.ts";
49
+ export { CohereReranker, JinaReranker } from "./reranker.ts";
50
+ export type { RetryOptions } from "./retry.ts";
51
+ export { withRetryChatClient, withRetryEmbeddingClient } from "./retry.ts";
52
+ export type { RewriteQueryInput } from "./rewrite-query.ts";
53
+ export { questionNeedsRewrite, rewriteQuery, sanitizeRewritten } from "./rewrite-query.ts";
54
+ export { sanitizeLlmOutput } from "./sanitize.ts";
55
+ export type { SemanticCacheOptions } from "./semantic-cache.ts";
56
+ export { SemanticCache } from "./semantic-cache.ts";
57
+ export type { RagRequestBody, RagServerOptions } from "./server.ts";
58
+ export { createRagServer } from "./server.ts";
59
+ export { InMemoryKbStore } from "./stores/memory-store.ts";
60
+ export { parseStructuredOutput, zodToJsonSchema } from "./structured-output.ts";
61
+ export type { ComposeOptions, DirectorHookForPrompt, FunnelStage, Hook, HookKind, SalesFramework, SkillForPrompt, StageConfig, Style, StylePersona, } from "./styles.ts";
62
+ export { FUNNEL_STAGES, HookSchema, PersonaSchema, SALES_FRAMEWORKS, StyleSchema, } from "./styles.ts";
63
+ export type { SummarizeInput } from "./summarize-conversation.ts";
64
+ export { cleanSummary, summarizeConversation } from "./summarize-conversation.ts";
65
+ export { buildSystemPrompt, DEFAULT_PERSONA, legacyRagSamplingTemperature, renderSummaryBlock, renderUserFactsBlock, } from "./system-prompt.ts";
66
+ export type { TextStyleRule } from "./text-style-rules.ts";
67
+ export { applyStyleRules, capitalizeFirstLetter, DEFAULT_STYLE_RULES, replaceEllipsis, replaceEmDash, stripAILeadIns, stripMarkdownBold, } from "./text-style-rules.ts";
68
+ export type { AnyRagTool, CompleteWithToolsResult, RagTool } from "./tools.ts";
69
+ export { makeBookingLinkTool } from "./built-in-tools/calendly.ts";
70
+ export { buildToolTelemetry, DEFAULT_MAX_TOOL_CYCLES, runToolLoop, type ToolCallRecord, type ToolLoopResult, } from "./tool-loop.ts";
71
+ export { classifyTopic, classifyTopicAll, KNOWN_TOPICS } from "./topic-classifier.ts";
72
+ export type { IKbStore, IKbSuggestionsStore, KbSearchHit } from "./types.ts";
73
+ export { reciprocalRankFusion, sanitizeFtsQuery } from "./utils.ts";
74
+ export type { ClassifyPhotoOptions, PassportIdentity, PhotoClass, VisionProvider, } from "./vision.ts";
75
+ export { classifyPhoto, extractPassportIdentity, PHOTO_CLASSES, parsePassportJson, parsePhotoClass, } from "./vision.ts";
76
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,KAAK,eAAe,GACrB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,YAAY,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC7F,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAItD,YAAY,EACV,UAAU,EACV,kBAAkB,EAClB,WAAW,EACX,QAAQ,GACT,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC3E,YAAY,EAAE,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAExE,YAAY,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACpE,YAAY,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,2BAA2B,CAAC;AAEnC,YAAY,EAA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