@oscharko-dev/keiko 0.2.9 → 0.2.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.
Files changed (206) hide show
  1. package/dist/ui/csp-hashes.json +15 -15
  2. package/dist/ui/static/404.html +1 -1
  3. package/dist/ui/static/__next.__PAGE__.txt +2 -2
  4. package/dist/ui/static/__next._full.txt +3 -3
  5. package/dist/ui/static/__next._head.txt +1 -1
  6. package/dist/ui/static/__next._index.txt +2 -2
  7. package/dist/ui/static/__next._tree.txt +2 -2
  8. package/dist/ui/static/_next/static/chunks/1587.617caa17651dac38.js +1 -0
  9. package/dist/ui/static/_next/static/chunks/2063.22631ea0405e4bb8.js +1 -0
  10. package/dist/ui/static/_next/static/chunks/3883.6a682796a62b576a.js +1 -0
  11. package/dist/ui/static/_next/static/chunks/3968.133ba5ea9ceed689.js +1 -0
  12. package/dist/ui/static/_next/static/chunks/4444.2b28731160b8c136.js +1 -0
  13. package/dist/ui/static/_next/static/chunks/4792.093159997fe3fbef.js +1 -0
  14. package/dist/ui/static/_next/static/chunks/{9211-06ff5a834d85f608.js → 6565-478649df3cb99b80.js} +2 -2
  15. package/dist/ui/static/_next/static/chunks/7301.0750920b7a295c19.js +1 -0
  16. package/dist/ui/static/_next/static/chunks/8395.500469ba6d01580a.js +1 -0
  17. package/dist/ui/static/_next/static/chunks/{8556.4f751fdef390f050.js → 8556.0ec3541a6b932c72.js} +1 -1
  18. package/dist/ui/static/_next/static/chunks/8824-59a261e25fd0a5d0.js +102 -0
  19. package/dist/ui/static/_next/static/chunks/8835-0b991bfcb0308e77.js +1 -0
  20. package/dist/ui/static/_next/static/chunks/9682.565c030c9bd50e04.js +1 -0
  21. package/dist/ui/static/_next/static/chunks/9832.92edd670fc6dec55.js +1 -0
  22. package/dist/ui/static/_next/static/chunks/aad4249a.1f9d09171ef39096.js +45 -0
  23. package/dist/ui/static/_next/static/chunks/app/launch/page-7e1c4ec16697ece8.js +1 -0
  24. package/dist/ui/static/_next/static/chunks/app/local-knowledge/page-7aebdcecb102697e.js +1 -0
  25. package/dist/ui/static/_next/static/chunks/app/page-e7eafa7e282e0347.js +1 -0
  26. package/dist/ui/static/_next/static/chunks/webpack-c61050a60249b41b.js +1 -0
  27. package/dist/ui/static/_next/static/css/{5881ca0ca97f8d4f.css → a44f4ffb37299b42.css} +2 -2
  28. package/dist/ui/static/_next/static/media/pdf.worker.145f9e3d.mjs +6 -0
  29. package/dist/ui/static/_not-found/__next._full.txt +2 -2
  30. package/dist/ui/static/_not-found/__next._head.txt +1 -1
  31. package/dist/ui/static/_not-found/__next._index.txt +2 -2
  32. package/dist/ui/static/_not-found/__next._not-found.__PAGE__.txt +1 -1
  33. package/dist/ui/static/_not-found/__next._not-found.txt +1 -1
  34. package/dist/ui/static/_not-found/__next._tree.txt +2 -2
  35. package/dist/ui/static/_not-found.html +1 -1
  36. package/dist/ui/static/_not-found.txt +2 -2
  37. package/dist/ui/static/index.html +1 -1
  38. package/dist/ui/static/index.txt +3 -3
  39. package/dist/ui/static/launch/__next._full.txt +3 -3
  40. package/dist/ui/static/launch/__next._head.txt +1 -1
  41. package/dist/ui/static/launch/__next._index.txt +2 -2
  42. package/dist/ui/static/launch/__next._tree.txt +2 -2
  43. package/dist/ui/static/launch/__next.launch.__PAGE__.txt +2 -2
  44. package/dist/ui/static/launch/__next.launch.txt +1 -1
  45. package/dist/ui/static/launch.html +1 -1
  46. package/dist/ui/static/launch.txt +3 -3
  47. package/dist/ui/static/local-knowledge/__next._full.txt +3 -3
  48. package/dist/ui/static/local-knowledge/__next._head.txt +1 -1
  49. package/dist/ui/static/local-knowledge/__next._index.txt +2 -2
  50. package/dist/ui/static/local-knowledge/__next._tree.txt +2 -2
  51. package/dist/ui/static/local-knowledge/__next.local-knowledge.__PAGE__.txt +2 -2
  52. package/dist/ui/static/local-knowledge/__next.local-knowledge.txt +1 -1
  53. package/dist/ui/static/local-knowledge/capsule/__next._full.txt +3 -3
  54. package/dist/ui/static/local-knowledge/capsule/__next._head.txt +1 -1
  55. package/dist/ui/static/local-knowledge/capsule/__next._index.txt +2 -2
  56. package/dist/ui/static/local-knowledge/capsule/__next._tree.txt +2 -2
  57. package/dist/ui/static/local-knowledge/capsule/__next.local-knowledge.capsule.__PAGE__.txt +2 -2
  58. package/dist/ui/static/local-knowledge/capsule/__next.local-knowledge.capsule.txt +1 -1
  59. package/dist/ui/static/local-knowledge/capsule/__next.local-knowledge.txt +1 -1
  60. package/dist/ui/static/local-knowledge/capsule.html +1 -1
  61. package/dist/ui/static/local-knowledge/capsule.txt +3 -3
  62. package/dist/ui/static/local-knowledge.html +1 -1
  63. package/dist/ui/static/local-knowledge.txt +3 -3
  64. package/dist/ui/static/memoriaviva/__next._full.txt +2 -2
  65. package/dist/ui/static/memoriaviva/__next._head.txt +1 -1
  66. package/dist/ui/static/memoriaviva/__next._index.txt +2 -2
  67. package/dist/ui/static/memoriaviva/__next._tree.txt +2 -2
  68. package/dist/ui/static/memoriaviva/__next.memoriaviva.__PAGE__.txt +1 -1
  69. package/dist/ui/static/memoriaviva/__next.memoriaviva.txt +1 -1
  70. package/dist/ui/static/memoriaviva/consolidation/__next._full.txt +2 -2
  71. package/dist/ui/static/memoriaviva/consolidation/__next._head.txt +1 -1
  72. package/dist/ui/static/memoriaviva/consolidation/__next._index.txt +2 -2
  73. package/dist/ui/static/memoriaviva/consolidation/__next._tree.txt +2 -2
  74. package/dist/ui/static/memoriaviva/consolidation/__next.memoriaviva.consolidation.__PAGE__.txt +1 -1
  75. package/dist/ui/static/memoriaviva/consolidation/__next.memoriaviva.consolidation.txt +1 -1
  76. package/dist/ui/static/memoriaviva/consolidation/__next.memoriaviva.txt +1 -1
  77. package/dist/ui/static/memoriaviva/consolidation.html +1 -1
  78. package/dist/ui/static/memoriaviva/consolidation.txt +2 -2
  79. package/dist/ui/static/memoriaviva/detail/__next._full.txt +2 -2
  80. package/dist/ui/static/memoriaviva/detail/__next._head.txt +1 -1
  81. package/dist/ui/static/memoriaviva/detail/__next._index.txt +2 -2
  82. package/dist/ui/static/memoriaviva/detail/__next._tree.txt +2 -2
  83. package/dist/ui/static/memoriaviva/detail/__next.memoriaviva.detail.__PAGE__.txt +1 -1
  84. package/dist/ui/static/memoriaviva/detail/__next.memoriaviva.detail.txt +1 -1
  85. package/dist/ui/static/memoriaviva/detail/__next.memoriaviva.txt +1 -1
  86. package/dist/ui/static/memoriaviva/detail.html +1 -1
  87. package/dist/ui/static/memoriaviva/detail.txt +2 -2
  88. package/dist/ui/static/memoriaviva/review-queue/__next._full.txt +2 -2
  89. package/dist/ui/static/memoriaviva/review-queue/__next._head.txt +1 -1
  90. package/dist/ui/static/memoriaviva/review-queue/__next._index.txt +2 -2
  91. package/dist/ui/static/memoriaviva/review-queue/__next._tree.txt +2 -2
  92. package/dist/ui/static/memoriaviva/review-queue/__next.memoriaviva.review-queue.__PAGE__.txt +1 -1
  93. package/dist/ui/static/memoriaviva/review-queue/__next.memoriaviva.review-queue.txt +1 -1
  94. package/dist/ui/static/memoriaviva/review-queue/__next.memoriaviva.txt +1 -1
  95. package/dist/ui/static/memoriaviva/review-queue.html +1 -1
  96. package/dist/ui/static/memoriaviva/review-queue.txt +2 -2
  97. package/dist/ui/static/memoriaviva.html +1 -1
  98. package/dist/ui/static/memoriaviva.txt +2 -2
  99. package/node_modules/@oscharko-dev/keiko-cli/dist/.tsbuildinfo +1 -1
  100. package/node_modules/@oscharko-dev/keiko-cli/package.json +14 -14
  101. package/node_modules/@oscharko-dev/keiko-contracts/dist/.tsbuildinfo +1 -1
  102. package/node_modules/@oscharko-dev/keiko-contracts/dist/index.d.ts +3 -1
  103. package/node_modules/@oscharko-dev/keiko-contracts/dist/index.d.ts.map +1 -1
  104. package/node_modules/@oscharko-dev/keiko-contracts/dist/index.js +2 -1
  105. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-preview.d.ts +101 -0
  106. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-preview.d.ts.map +1 -0
  107. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-preview.js +62 -0
  108. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-schema.d.ts +1 -1
  109. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-schema.d.ts.map +1 -1
  110. package/node_modules/@oscharko-dev/keiko-contracts/dist/local-knowledge-schema.js +59 -2
  111. package/node_modules/@oscharko-dev/keiko-contracts/package.json +5 -1
  112. package/node_modules/@oscharko-dev/keiko-evaluations/dist/.tsbuildinfo +1 -1
  113. package/node_modules/@oscharko-dev/keiko-evaluations/package.json +9 -9
  114. package/node_modules/@oscharko-dev/keiko-evidence/dist/.tsbuildinfo +1 -1
  115. package/node_modules/@oscharko-dev/keiko-evidence/package.json +4 -4
  116. package/node_modules/@oscharko-dev/keiko-harness/dist/.tsbuildinfo +1 -1
  117. package/node_modules/@oscharko-dev/keiko-harness/package.json +6 -6
  118. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/.tsbuildinfo +1 -1
  119. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/citation-preview-snapshot.d.ts +15 -0
  120. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/citation-preview-snapshot.d.ts.map +1 -0
  121. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/citation-preview-snapshot.js +49 -0
  122. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/index.d.ts +1 -0
  123. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/index.d.ts.map +1 -1
  124. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/index.js +1 -0
  125. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/privacy/audit-emitter.d.ts.map +1 -1
  126. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/privacy/audit-emitter.js +45 -19
  127. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/privacy/types.d.ts +21 -1
  128. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/privacy/types.d.ts.map +1 -1
  129. package/node_modules/@oscharko-dev/keiko-local-knowledge/package.json +5 -5
  130. package/node_modules/@oscharko-dev/keiko-memory-capture/package.json +3 -3
  131. package/node_modules/@oscharko-dev/keiko-memory-consolidation/package.json +3 -3
  132. package/node_modules/@oscharko-dev/keiko-memory-governance/package.json +3 -3
  133. package/node_modules/@oscharko-dev/keiko-memory-retrieval/package.json +3 -3
  134. package/node_modules/@oscharko-dev/keiko-memory-vault/dist/.tsbuildinfo +1 -1
  135. package/node_modules/@oscharko-dev/keiko-memory-vault/package.json +3 -3
  136. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/.tsbuildinfo +1 -1
  137. package/node_modules/@oscharko-dev/keiko-model-gateway/package.json +3 -3
  138. package/node_modules/@oscharko-dev/keiko-quality-intelligence/dist/.tsbuildinfo +1 -1
  139. package/node_modules/@oscharko-dev/keiko-quality-intelligence/package.json +3 -3
  140. package/node_modules/@oscharko-dev/keiko-sandbox/dist/.tsbuildinfo +1 -1
  141. package/node_modules/@oscharko-dev/keiko-sandbox/package.json +2 -2
  142. package/node_modules/@oscharko-dev/keiko-sdk/dist/.tsbuildinfo +1 -1
  143. package/node_modules/@oscharko-dev/keiko-sdk/package.json +9 -9
  144. package/node_modules/@oscharko-dev/keiko-security/dist/.tsbuildinfo +1 -1
  145. package/node_modules/@oscharko-dev/keiko-security/package.json +2 -2
  146. package/node_modules/@oscharko-dev/keiko-server/dist/.tsbuildinfo +1 -1
  147. package/node_modules/@oscharko-dev/keiko-server/dist/deps.d.ts +2 -0
  148. package/node_modules/@oscharko-dev/keiko-server/dist/deps.d.ts.map +1 -1
  149. package/node_modules/@oscharko-dev/keiko-server/dist/grounded-qa-hybrid.d.ts.map +1 -1
  150. package/node_modules/@oscharko-dev/keiko-server/dist/grounded-qa-hybrid.js +19 -2
  151. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-grounded-qa.d.ts.map +1 -1
  152. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-grounded-qa.js +26 -4
  153. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-authority.d.ts +11 -0
  154. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-authority.d.ts.map +1 -0
  155. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-authority.js +85 -0
  156. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-delivery.d.ts +16 -0
  157. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-delivery.d.ts.map +1 -0
  158. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-delivery.js +131 -0
  159. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-handlers.d.ts +8 -0
  160. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-handlers.d.ts.map +1 -0
  161. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-handlers.js +349 -0
  162. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-service.d.ts +29 -0
  163. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-service.d.ts.map +1 -0
  164. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-service.js +260 -0
  165. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-session-manager.d.ts +41 -0
  166. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-session-manager.d.ts.map +1 -0
  167. package/node_modules/@oscharko-dev/keiko-server/dist/local-knowledge-preview-session-manager.js +145 -0
  168. package/node_modules/@oscharko-dev/keiko-server/dist/routes.d.ts.map +1 -1
  169. package/node_modules/@oscharko-dev/keiko-server/dist/routes.js +26 -0
  170. package/node_modules/@oscharko-dev/keiko-server/dist/store/db.d.ts.map +1 -1
  171. package/node_modules/@oscharko-dev/keiko-server/dist/store/db.js +3 -2
  172. package/node_modules/@oscharko-dev/keiko-server/dist/store/messages.d.ts +3 -2
  173. package/node_modules/@oscharko-dev/keiko-server/dist/store/messages.d.ts.map +1 -1
  174. package/node_modules/@oscharko-dev/keiko-server/dist/store/messages.js +37 -7
  175. package/node_modules/@oscharko-dev/keiko-server/dist/store/schema.d.ts +1 -1
  176. package/node_modules/@oscharko-dev/keiko-server/dist/store/schema.d.ts.map +1 -1
  177. package/node_modules/@oscharko-dev/keiko-server/dist/store/schema.js +9 -1
  178. package/node_modules/@oscharko-dev/keiko-server/dist/store/types.d.ts +4 -2
  179. package/node_modules/@oscharko-dev/keiko-server/dist/store/types.d.ts.map +1 -1
  180. package/node_modules/@oscharko-dev/keiko-server/package.json +19 -19
  181. package/node_modules/@oscharko-dev/keiko-tools/dist/.tsbuildinfo +1 -1
  182. package/node_modules/@oscharko-dev/keiko-tools/package.json +5 -5
  183. package/node_modules/@oscharko-dev/keiko-verification/dist/.tsbuildinfo +1 -1
  184. package/node_modules/@oscharko-dev/keiko-verification/package.json +5 -5
  185. package/node_modules/@oscharko-dev/keiko-workflows/dist/.tsbuildinfo +1 -1
  186. package/node_modules/@oscharko-dev/keiko-workflows/package.json +10 -10
  187. package/node_modules/@oscharko-dev/keiko-workspace/dist/.tsbuildinfo +1 -1
  188. package/node_modules/@oscharko-dev/keiko-workspace/package.json +3 -3
  189. package/package.json +22 -22
  190. package/dist/ui/static/_next/static/chunks/1587.33fc772cb66c38d7.js +0 -1
  191. package/dist/ui/static/_next/static/chunks/2063.a7cd2441b60aa093.js +0 -1
  192. package/dist/ui/static/_next/static/chunks/2107.c1ff3a39fd003d88.js +0 -1
  193. package/dist/ui/static/_next/static/chunks/3883.f8e3dc3fea7fdbe1.js +0 -1
  194. package/dist/ui/static/_next/static/chunks/3968.55fa02cd74fb96b3.js +0 -1
  195. package/dist/ui/static/_next/static/chunks/4444.46b9cfb85257a381.js +0 -1
  196. package/dist/ui/static/_next/static/chunks/7301.501daea6a309d3f5.js +0 -1
  197. package/dist/ui/static/_next/static/chunks/8395.e30ea9a63eb35cb3.js +0 -1
  198. package/dist/ui/static/_next/static/chunks/8824-7bd1fafeeeda68c2.js +0 -102
  199. package/dist/ui/static/_next/static/chunks/8835-b68c3abd8ddcb452.js +0 -1
  200. package/dist/ui/static/_next/static/chunks/9682.d5b8d9ce8cb80519.js +0 -1
  201. package/dist/ui/static/_next/static/chunks/app/launch/page-82ed92234aabb847.js +0 -1
  202. package/dist/ui/static/_next/static/chunks/app/local-knowledge/page-9124d233dd58858c.js +0 -1
  203. package/dist/ui/static/_next/static/chunks/app/page-dcdb404becd4ddb6.js +0 -1
  204. package/dist/ui/static/_next/static/chunks/webpack-4e37711fe87dbc9e.js +0 -1
  205. /package/dist/ui/static/_next/static/{tzWFDxPPJ3-NtjB1T1JFu → WGuvz70_f_uiVSiv-B1wx}/_buildManifest.js +0 -0
  206. /package/dist/ui/static/_next/static/{tzWFDxPPJ3-NtjB1T1JFu → WGuvz70_f_uiVSiv-B1wx}/_ssgManifest.js +0 -0
@@ -0,0 +1,349 @@
1
+ import { pdfCitationPreviewFailureState } from "@oscharko-dev/keiko-contracts";
2
+ import { STREAMING, errorBody } from "./routes.js";
3
+ import { authorizePdfCitationPreview, getPdfCitationPreviewStatus, projectPdfCitationPreviewAuthorizationResponse, } from "./local-knowledge-preview-service.js";
4
+ import { normalizePreviewMarkerIndex } from "./local-knowledge-preview-authority.js";
5
+ import { loadVerifiedPdfPreviewSource, MAX_PDF_PREVIEW_RANGE_BYTES } from "./local-knowledge-preview-delivery.js";
6
+ import { previewSessionManagerFor } from "./local-knowledge-preview-session-manager.js";
7
+ const MAX_BODY_BYTES = 16_000;
8
+ class InvalidRequest extends Error {
9
+ }
10
+ async function readBody(req) {
11
+ return new Promise((resolve, reject) => {
12
+ const chunks = [];
13
+ let total = 0;
14
+ req.on("data", (chunk) => {
15
+ total += chunk.length;
16
+ if (total > MAX_BODY_BYTES) {
17
+ reject(new InvalidRequest("Request body is too large."));
18
+ req.resume();
19
+ return;
20
+ }
21
+ chunks.push(chunk);
22
+ });
23
+ req.on("end", () => {
24
+ resolve(Buffer.concat(chunks).toString("utf8"));
25
+ });
26
+ req.on("error", (error) => {
27
+ reject(error);
28
+ });
29
+ });
30
+ }
31
+ async function readJsonObject(req) {
32
+ const raw = await readBody(req);
33
+ if (raw.length === 0)
34
+ return {};
35
+ let parsed;
36
+ try {
37
+ parsed = JSON.parse(raw);
38
+ }
39
+ catch {
40
+ throw new InvalidRequest("Request body must be valid JSON.");
41
+ }
42
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
43
+ throw new InvalidRequest("Request body must be a JSON object.");
44
+ }
45
+ return parsed;
46
+ }
47
+ function parseBaseBody(body) {
48
+ if (typeof body.chatId !== "string" || body.chatId.trim().length === 0) {
49
+ throw new InvalidRequest("Field \"chatId\" must be a non-empty string.");
50
+ }
51
+ if (typeof body.assistantMessageId !== "string" ||
52
+ body.assistantMessageId.trim().length === 0) {
53
+ throw new InvalidRequest("Field \"assistantMessageId\" must be a non-empty string.");
54
+ }
55
+ if (body.stableId !== undefined && typeof body.stableId !== "string") {
56
+ throw new InvalidRequest("Field \"stableId\" must be a string when present.");
57
+ }
58
+ return {
59
+ chatId: body.chatId,
60
+ assistantMessageId: body.assistantMessageId,
61
+ ...(typeof body.stableId === "string" ? { stableId: body.stableId } : {}),
62
+ };
63
+ }
64
+ function parseMarker(body, required) {
65
+ if (!("marker" in body)) {
66
+ if (required) {
67
+ throw new InvalidRequest("Field \"marker\" is required.");
68
+ }
69
+ return undefined;
70
+ }
71
+ const marker = body.marker;
72
+ if (typeof marker !== "string" && typeof marker !== "number") {
73
+ throw new InvalidRequest("Field \"marker\" must be a string or number.");
74
+ }
75
+ if (normalizePreviewMarkerIndex(marker) === undefined) {
76
+ throw new InvalidRequest("Field \"marker\" must resolve to a positive citation index.");
77
+ }
78
+ return marker;
79
+ }
80
+ function parseOrigin(body) {
81
+ const origin = body.origin;
82
+ return origin === "inline-marker" || origin === "citation-chip" ? origin : undefined;
83
+ }
84
+ function invalidRequestResult(message) {
85
+ return { status: 400, body: errorBody("BAD_REQUEST", message) };
86
+ }
87
+ function previewSourceError(reason) {
88
+ switch (reason) {
89
+ case "document-content-mismatch":
90
+ return {
91
+ status: 409,
92
+ body: errorBody("PREVIEW_SOURCE_CHANGED", "The verified PDF bytes no longer match the citation."),
93
+ };
94
+ case "document-not-pdf":
95
+ return {
96
+ status: 409,
97
+ body: errorBody("PREVIEW_SOURCE_NOT_PDF", "The verified citation source is no longer a PDF."),
98
+ };
99
+ case "document-not-ready":
100
+ return {
101
+ status: 409,
102
+ body: errorBody("PREVIEW_SOURCE_NOT_READY", "The verified citation source is not ready for preview."),
103
+ };
104
+ case "preview-source-oversized":
105
+ return {
106
+ status: 413,
107
+ body: errorBody("PREVIEW_SOURCE_TOO_LARGE", "The verified PDF exceeds the preview size limit."),
108
+ };
109
+ case "preview-source-unreadable":
110
+ return {
111
+ status: 503,
112
+ body: errorBody("PREVIEW_SOURCE_UNREADABLE", "The verified PDF could not be read safely."),
113
+ };
114
+ default:
115
+ return {
116
+ status: 410,
117
+ body: errorBody("PREVIEW_SOURCE_MISSING", "The verified PDF source is no longer available."),
118
+ };
119
+ }
120
+ }
121
+ function selectionInput(body) {
122
+ const base = parseBaseBody(body);
123
+ const marker = parseMarker(body, true);
124
+ const origin = parseOrigin(body);
125
+ return {
126
+ ...base,
127
+ marker: marker ?? "[1]",
128
+ ...(origin === undefined ? {} : { origin }),
129
+ };
130
+ }
131
+ function invalidRangeResult(message) {
132
+ return {
133
+ status: 416,
134
+ body: errorBody("PREVIEW_RANGE_NOT_SATISFIABLE", message),
135
+ };
136
+ }
137
+ function parseExplicitRange(startRaw, endRaw) {
138
+ if (startRaw.length === 0 || endRaw.length === 0)
139
+ return undefined;
140
+ return {
141
+ start: Number.parseInt(startRaw, 10),
142
+ end: Number.parseInt(endRaw, 10),
143
+ };
144
+ }
145
+ function parseOpenEndedRange(startRaw, totalBytes) {
146
+ if (startRaw.length === 0)
147
+ return undefined;
148
+ return {
149
+ start: Number.parseInt(startRaw, 10),
150
+ end: totalBytes - 1,
151
+ };
152
+ }
153
+ function parseSuffixRange(endRaw, totalBytes) {
154
+ if (endRaw.length === 0)
155
+ return undefined;
156
+ const suffixLength = Number.parseInt(endRaw, 10);
157
+ return {
158
+ start: Math.max(0, totalBytes - suffixLength),
159
+ end: totalBytes - 1,
160
+ };
161
+ }
162
+ function parsedRangeValues(startRaw, endRaw, totalBytes) {
163
+ return (parseExplicitRange(startRaw, endRaw) ??
164
+ parseOpenEndedRange(startRaw, totalBytes) ??
165
+ parseSuffixRange(endRaw, totalBytes));
166
+ }
167
+ function validatedRange(start, end, totalBytes) {
168
+ if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end < start || start >= totalBytes) {
169
+ return invalidRangeResult("The requested byte range is invalid.");
170
+ }
171
+ const boundedEnd = Math.min(end, totalBytes - 1);
172
+ if (boundedEnd - start + 1 > MAX_PDF_PREVIEW_RANGE_BYTES) {
173
+ return invalidRangeResult("The requested byte range exceeds the preview limit.");
174
+ }
175
+ return { start, end: boundedEnd };
176
+ }
177
+ function parseRangeHeader(header, totalBytes) {
178
+ if (header === undefined) {
179
+ return { start: 0, end: totalBytes - 1 };
180
+ }
181
+ const match = /^bytes=(\d*)-(\d*)$/u.exec(header.trim());
182
+ if (match === null || header.includes(",")) {
183
+ return invalidRangeResult("Only a single bounded byte range is supported.");
184
+ }
185
+ const startRaw = match[1] ?? "";
186
+ const endRaw = match[2] ?? "";
187
+ const parsed = parsedRangeValues(startRaw, endRaw, totalBytes);
188
+ return parsed === undefined
189
+ ? invalidRangeResult("The requested byte range is invalid.")
190
+ : validatedRange(parsed.start, parsed.end, totalBytes);
191
+ }
192
+ export async function handleGetPdfCitationPreviewStatus(ctx, deps) {
193
+ try {
194
+ const body = await readJsonObject(ctx.req);
195
+ const base = parseBaseBody(body);
196
+ const marker = parseMarker(body, false);
197
+ const input = {
198
+ ...base,
199
+ ...(marker !== undefined ? { marker } : {}),
200
+ };
201
+ return { status: 200, body: getPdfCitationPreviewStatus(deps, input) };
202
+ }
203
+ catch (error) {
204
+ if (error instanceof InvalidRequest) {
205
+ return invalidRequestResult(error.message);
206
+ }
207
+ throw error;
208
+ }
209
+ }
210
+ export async function handleAuthorizePdfCitationPreview(ctx, deps) {
211
+ try {
212
+ const body = await readJsonObject(ctx.req);
213
+ const input = selectionInput(body);
214
+ return {
215
+ status: 200,
216
+ body: projectPdfCitationPreviewAuthorizationResponse(authorizePdfCitationPreview(deps, input)),
217
+ };
218
+ }
219
+ catch (error) {
220
+ if (error instanceof InvalidRequest) {
221
+ return invalidRequestResult(error.message);
222
+ }
223
+ throw error;
224
+ }
225
+ }
226
+ export async function handleOpenPdfCitationPreviewSession(ctx, deps) {
227
+ try {
228
+ const body = await readJsonObject(ctx.req);
229
+ const authorized = authorizePdfCitationPreview(deps, selectionInput(body));
230
+ if ("kind" in authorized) {
231
+ return rejectedOpenResult(authorized.reason, authorized.display);
232
+ }
233
+ const verified = await loadVerifiedPdfPreviewSource(deps, authorized.authority);
234
+ if (verified.kind !== "ok") {
235
+ return rejectedOpenResult(verified.reason, authorized.display);
236
+ }
237
+ const opened = previewSessionManagerFor(deps).openSession(authorized.authority);
238
+ return openSessionResult(authorized.display, opened, verified.bytes.byteLength);
239
+ }
240
+ catch (error) {
241
+ if (error instanceof InvalidRequest) {
242
+ return invalidRequestResult(error.message);
243
+ }
244
+ throw error;
245
+ }
246
+ }
247
+ function rejectedOpenResult(reason, display) {
248
+ return {
249
+ status: 200,
250
+ body: {
251
+ outcome: "rejected",
252
+ state: pdfCitationPreviewFailureState(reason),
253
+ reason,
254
+ ...(display === undefined ? {} : { display }),
255
+ },
256
+ };
257
+ }
258
+ function openSessionResult(display, opened, byteLength) {
259
+ return {
260
+ status: 200,
261
+ body: {
262
+ outcome: "authorized",
263
+ display,
264
+ session: {
265
+ handle: opened.session.handle,
266
+ expiresAt: opened.session.expiresAt,
267
+ reused: opened.reused,
268
+ byteLength,
269
+ contentType: "application/pdf",
270
+ },
271
+ },
272
+ };
273
+ }
274
+ function sessionHandleFromContext(ctx) {
275
+ const sessionHandle = ctx.params.sessionHandle ?? "";
276
+ if (sessionHandle.length === 0) {
277
+ return { status: 400, body: errorBody("BAD_REQUEST", "sessionHandle is required.") };
278
+ }
279
+ return sessionHandle;
280
+ }
281
+ function sessionLookupResult(deps, sessionHandle) {
282
+ const lookup = previewSessionManagerFor(deps).lookupSession(sessionHandle);
283
+ if (lookup.kind === "missing") {
284
+ return {
285
+ status: 404,
286
+ body: errorBody("PREVIEW_SESSION_NOT_FOUND", "Preview session not found."),
287
+ };
288
+ }
289
+ if (lookup.kind === "closed") {
290
+ return {
291
+ status: 410,
292
+ body: errorBody("PREVIEW_SESSION_CLOSED", "Preview session is closed."),
293
+ };
294
+ }
295
+ if (lookup.kind === "expired") {
296
+ return {
297
+ status: 410,
298
+ body: errorBody("PREVIEW_SESSION_EXPIRED", "Preview session expired."),
299
+ };
300
+ }
301
+ return lookup;
302
+ }
303
+ function isRouteResult(value) {
304
+ return typeof value === "object" && value !== null && "status" in value;
305
+ }
306
+ function streamPdfPreview(ctx, bytes, range) {
307
+ const isPartial = range.start !== 0 || range.end !== bytes.byteLength - 1;
308
+ const chunk = bytes.subarray(range.start, range.end + 1);
309
+ ctx.res.writeHead(isPartial ? 206 : 200, {
310
+ "Accept-Ranges": "bytes",
311
+ "Cache-Control": "no-store",
312
+ "Content-Length": String(chunk.byteLength),
313
+ "Content-Type": "application/pdf",
314
+ ...(isPartial
315
+ ? { "Content-Range": `bytes ${String(range.start)}-${String(range.end)}/${String(bytes.byteLength)}` }
316
+ : {}),
317
+ "X-Content-Type-Options": "nosniff",
318
+ });
319
+ ctx.res.end(Buffer.from(chunk));
320
+ return STREAMING;
321
+ }
322
+ export async function handleGetPdfCitationPreviewDocument(ctx, deps) {
323
+ const sessionHandle = sessionHandleFromContext(ctx);
324
+ if (isRouteResult(sessionHandle)) {
325
+ return sessionHandle;
326
+ }
327
+ const lookup = sessionLookupResult(deps, sessionHandle);
328
+ if (isRouteResult(lookup)) {
329
+ return lookup;
330
+ }
331
+ const verified = await loadVerifiedPdfPreviewSource(deps, lookup.session.authority);
332
+ if (verified.kind !== "ok") {
333
+ return previewSourceError(verified.reason);
334
+ }
335
+ const rangeHeader = ctx.req.headers.range;
336
+ const parsedRange = parseRangeHeader(typeof rangeHeader === "string" ? rangeHeader : rangeHeader?.[0], verified.bytes.byteLength);
337
+ if ("status" in parsedRange) {
338
+ return parsedRange;
339
+ }
340
+ return streamPdfPreview(ctx, verified.bytes, parsedRange);
341
+ }
342
+ export function handleClosePdfCitationPreviewSession(ctx, deps) {
343
+ const sessionHandle = sessionHandleFromContext(ctx);
344
+ if (isRouteResult(sessionHandle)) {
345
+ return sessionHandle;
346
+ }
347
+ previewSessionManagerFor(deps).closeSession(sessionHandle);
348
+ return { status: 200, body: { ok: true } };
349
+ }
@@ -0,0 +1,29 @@
1
+ import type { CurrentPdfCitationPreviewSnapshot, PdfCitationPreviewAuthorizationResponse, PdfCitationPreviewDisplay, PdfCitationPreviewReasonCode, PdfCitationPreviewSelection, PdfCitationPreviewStatusRequest, PdfCitationPreviewStatusResponse, StoredPdfCitationPreviewCitation } from "@oscharko-dev/keiko-contracts";
2
+ import type { UiHandlerDeps } from "./deps.js";
3
+ type PreviewOutcome = {
4
+ readonly kind: "available";
5
+ readonly display: PdfCitationPreviewDisplay;
6
+ readonly authority: PdfCitationPreviewAuthority;
7
+ } | {
8
+ readonly kind: "rejected";
9
+ readonly reason: PdfCitationPreviewReasonCode;
10
+ readonly display?: PdfCitationPreviewDisplay;
11
+ readonly citations?: readonly StoredPdfCitationPreviewCitation[];
12
+ };
13
+ type RejectedPreviewOutcome = Extract<PreviewOutcome, {
14
+ readonly kind: "rejected";
15
+ }>;
16
+ export interface PdfCitationPreviewAuthority {
17
+ readonly citation: StoredPdfCitationPreviewCitation;
18
+ readonly current?: CurrentPdfCitationPreviewSnapshot;
19
+ }
20
+ export type PdfCitationPreviewAuthorizationResult = {
21
+ readonly outcome: "authorized";
22
+ readonly display: PdfCitationPreviewDisplay;
23
+ readonly authority: PdfCitationPreviewAuthority;
24
+ } | RejectedPreviewOutcome;
25
+ export declare function getPdfCitationPreviewStatus(deps: UiHandlerDeps, input: PdfCitationPreviewStatusRequest): PdfCitationPreviewStatusResponse;
26
+ export declare function authorizePdfCitationPreview(deps: UiHandlerDeps, input: PdfCitationPreviewSelection): PdfCitationPreviewAuthorizationResult;
27
+ export declare function projectPdfCitationPreviewAuthorizationResponse(result: PdfCitationPreviewAuthorizationResult): PdfCitationPreviewAuthorizationResponse;
28
+ export {};
29
+ //# sourceMappingURL=local-knowledge-preview-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-knowledge-preview-service.d.ts","sourceRoot":"","sources":["../src/local-knowledge-preview-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iCAAiC,EACjC,uCAAuC,EAEvC,yBAAyB,EACzB,4BAA4B,EAC5B,2BAA2B,EAC3B,+BAA+B,EAC/B,gCAAgC,EAChC,gCAAgC,EAEjC,MAAM,+BAA+B,CAAC;AAQvC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAI/C,KAAK,cAAc,GACf;IACE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,2BAA2B,CAAC;CACjD,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,4BAA4B,CAAC;IAC9C,QAAQ,CAAC,OAAO,CAAC,EAAE,yBAAyB,CAAC;IAC7C,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,gCAAgC,EAAE,CAAC;CAClE,CAAC;AAEN,KAAK,sBAAsB,GAAG,OAAO,CAAC,cAAc,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAAC;AAKrF,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,QAAQ,EAAE,gCAAgC,CAAC;IACpD,QAAQ,CAAC,OAAO,CAAC,EAAE,iCAAiC,CAAC;CACtD;AAED,MAAM,MAAM,qCAAqC,GAC7C;IACE,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,2BAA2B,CAAC;CACjD,GACD,sBAAsB,CAAC;AAsP3B,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,+BAA+B,GACrC,gCAAgC,CAyBlC;AA2BD,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,2BAA2B,GACjC,qCAAqC,CAuCvC;AAED,wBAAgB,8CAA8C,CAC5D,MAAM,EAAE,qCAAqC,GAC5C,uCAAuC,CAKzC"}
@@ -0,0 +1,260 @@
1
+ import { pdfCitationPreviewFailureState } from "@oscharko-dev/keiko-contracts";
2
+ import { createSqliteAuditSink, lookupCitationPreviewSnapshot, } from "@oscharko-dev/keiko-local-knowledge";
3
+ import { openStoreForDeps } from "./local-knowledge-grounded-qa.js";
4
+ import { normalizePreviewMarkerIndex, previewDisplay } from "./local-knowledge-preview-authority.js";
5
+ function reject(reason, display, citations) {
6
+ return {
7
+ kind: "rejected",
8
+ reason,
9
+ ...(display !== undefined ? { display } : {}),
10
+ ...(citations !== undefined ? { citations } : {}),
11
+ };
12
+ }
13
+ function citationDisplay(stored, current) {
14
+ return previewDisplay(resolveCitationDisplayInput(stored, current));
15
+ }
16
+ // The projection is intentionally flat so the preview display uses one normalized source of truth.
17
+ // eslint-disable-next-line complexity
18
+ function resolveCitationDisplayInput(stored, current) {
19
+ const input = {
20
+ documentLabel: current?.documentLabel ?? stored.documentLabel,
21
+ };
22
+ assignOptionalCitationField(input, "sourceLabel", stored.sourceLabel);
23
+ assignOptionalCitationField(input, "pageNumber", current?.pageNumber ?? stored.pageNumber);
24
+ assignOptionalCitationField(input, "pageLabel", current?.pageLabel ?? stored.pageLabel);
25
+ assignOptionalCitationField(input, "characterStart", current?.characterStart ?? stored.characterStart);
26
+ assignOptionalCitationField(input, "characterEnd", current?.characterEnd ?? stored.characterEnd);
27
+ return input;
28
+ }
29
+ function assignOptionalCitationField(target, key, value) {
30
+ if (value !== undefined) {
31
+ target[key] = value;
32
+ }
33
+ }
34
+ function isLocalKnowledgeGroundingKind(kind) {
35
+ return kind === "local-knowledge" || kind === "hybrid";
36
+ }
37
+ function selectedCitations(citations, marker, stableId) {
38
+ let selected = citations;
39
+ if (marker !== undefined) {
40
+ const markerIndex = normalizePreviewMarkerIndex(marker);
41
+ if (markerIndex === undefined) {
42
+ return reject("citation-not-found");
43
+ }
44
+ selected = selected.filter((citation) => citation.markerIndex === markerIndex);
45
+ if (selected.length === 0) {
46
+ return reject("citation-not-found");
47
+ }
48
+ }
49
+ if (stableId !== undefined && selected.length === 1) {
50
+ const stableMatches = selected.filter((citation) => citation.stableId === stableId);
51
+ if (stableMatches.length === 0) {
52
+ return reject("stable-id-mismatch", selected[0] === undefined ? undefined : citationDisplay(selected[0]), selected);
53
+ }
54
+ selected = stableMatches;
55
+ }
56
+ return { kind: "selected", citations: selected };
57
+ }
58
+ function currentDisplayForLookup(stored, lookup) {
59
+ return lookup.kind === "ok" ? citationDisplay(stored, lookup.snapshot) : citationDisplay(stored);
60
+ }
61
+ function storedCitationPrecheck(stored, display) {
62
+ if (display.anchorQuality === "unavailable") {
63
+ return reject("page-provenance-missing", display);
64
+ }
65
+ if (stored.documentMediaType !== "application/pdf") {
66
+ return reject("document-not-pdf", display);
67
+ }
68
+ return undefined;
69
+ }
70
+ function currentSnapshotPrecheck(stored, current, display) {
71
+ if (current.lineage.sourceId !== stored.lineage.sourceId ||
72
+ current.lineage.documentId !== stored.lineage.documentId ||
73
+ current.lineage.chunkId !== stored.lineage.chunkId) {
74
+ return reject("lineage-mismatch", display);
75
+ }
76
+ if (current.documentMediaType !== "application/pdf") {
77
+ return reject("document-not-pdf", display);
78
+ }
79
+ if (current.documentStatus !== "extracted") {
80
+ return reject("document-not-ready", display);
81
+ }
82
+ if (current.documentContentHash !== stored.documentContentHash) {
83
+ return reject("document-content-mismatch", display);
84
+ }
85
+ if (current.pageNumber === undefined) {
86
+ return reject("page-provenance-missing", display);
87
+ }
88
+ if (stored.pageNumber !== undefined && current.pageNumber !== stored.pageNumber) {
89
+ return reject("lineage-mismatch", display);
90
+ }
91
+ return undefined;
92
+ }
93
+ function evaluateStoredCitation(store, stored) {
94
+ const storedDisplay = citationDisplay(stored);
95
+ const initialFailure = storedCitationPrecheck(stored, storedDisplay);
96
+ if (initialFailure !== undefined)
97
+ return initialFailure;
98
+ const lookup = lookupCitationPreviewSnapshot(store, stored.lineage.capsuleId, stored.lineage.chunkId);
99
+ if (lookup.kind !== "ok") {
100
+ return reject("lineage-missing", currentDisplayForLookup(stored, lookup));
101
+ }
102
+ const current = lookup.snapshot;
103
+ const display = citationDisplay(stored, current);
104
+ const currentFailure = currentSnapshotPrecheck(stored, current, display);
105
+ if (currentFailure !== undefined)
106
+ return currentFailure;
107
+ return { kind: "available", display, authority: { citation: stored, current } };
108
+ }
109
+ function emitPreviewAudit(store, citations, outcome) {
110
+ if (citations.length === 0)
111
+ return;
112
+ const sink = createSqliteAuditSink(store);
113
+ const firstCitation = citations[0];
114
+ if (firstCitation === undefined)
115
+ return;
116
+ const base = {
117
+ capsuleId: firstCitation.lineage.capsuleId,
118
+ sourceIds: citations.map((citation) => citation.lineage.sourceId),
119
+ chunkIds: citations.map((citation) => String(citation.lineage.chunkId)),
120
+ targetQuality: citationDisplay(firstCitation).anchorQuality,
121
+ occurredAt: Date.now(),
122
+ };
123
+ if (outcome.kind === "available") {
124
+ sink.emit({ kind: "citation-preview-authorized", ...base });
125
+ return;
126
+ }
127
+ const state = pdfCitationPreviewFailureState(outcome.reason);
128
+ if (state === "recoverable") {
129
+ sink.emit({ kind: "citation-preview-recoverable", reasonCode: outcome.reason, ...base });
130
+ return;
131
+ }
132
+ if (state === "blocked") {
133
+ sink.emit({ kind: "citation-preview-blocked", reasonCode: outcome.reason, ...base });
134
+ }
135
+ }
136
+ function loadMessageContext(deps, chatId, assistantMessageId) {
137
+ const message = deps.store.findMessageById(assistantMessageId);
138
+ if (message?.role !== "assistant") {
139
+ return reject("assistant-message-not-found");
140
+ }
141
+ if (message.chatId !== chatId) {
142
+ return reject("assistant-message-chat-mismatch");
143
+ }
144
+ const grounded = message.groundedAnswer;
145
+ if (grounded === undefined) {
146
+ return reject("grounded-answer-missing");
147
+ }
148
+ if (!isLocalKnowledgeGroundingKind(grounded.groundingKind)) {
149
+ return reject("not-local-knowledge-citation");
150
+ }
151
+ return { citations: deps.store.findGroundedPreviewCitations(assistantMessageId) };
152
+ }
153
+ function passiveStatusState(result) {
154
+ if (result.kind === "available") {
155
+ return "available";
156
+ }
157
+ if (result.reason === "document-not-pdf") {
158
+ return "not-applicable";
159
+ }
160
+ return pdfCitationPreviewFailureState(result.reason);
161
+ }
162
+ function passiveCitationStatus(citation, result) {
163
+ const state = passiveStatusState(result);
164
+ return {
165
+ stableId: citation.stableId,
166
+ marker: citation.marker,
167
+ markerIndex: citation.markerIndex,
168
+ state,
169
+ ...(result.kind === "available" ? { display: result.display } : {}),
170
+ ...(result.kind === "rejected" && state !== "not-applicable" ? { reason: result.reason } : {}),
171
+ ...(result.kind === "rejected" && result.display !== undefined ? { display: result.display } : {}),
172
+ };
173
+ }
174
+ export function getPdfCitationPreviewStatus(deps, input) {
175
+ const loaded = loadMessageContext(deps, input.chatId, input.assistantMessageId);
176
+ if ("kind" in loaded) {
177
+ return { citations: [] };
178
+ }
179
+ if (loaded.citations === undefined) {
180
+ return { citations: [] };
181
+ }
182
+ if (loaded.citations.length === 0) {
183
+ return { citations: [] };
184
+ }
185
+ const selected = selectedCitations(loaded.citations, input.marker, input.stableId);
186
+ if (selected.kind !== "selected") {
187
+ return { citations: [] };
188
+ }
189
+ const session = openStoreForDeps(deps);
190
+ try {
191
+ return {
192
+ citations: selected.citations.map((citation) => passiveCitationStatus(citation, evaluateStoredCitation(session.store, citation))),
193
+ };
194
+ }
195
+ finally {
196
+ session.close();
197
+ }
198
+ }
199
+ function rejectedAuthorizationResponse(failure) {
200
+ return {
201
+ outcome: "rejected",
202
+ state: pdfCitationPreviewFailureState(failure.reason),
203
+ reason: failure.reason,
204
+ ...(failure.display !== undefined ? { display: failure.display } : {}),
205
+ };
206
+ }
207
+ function emitAuthorizationAuditIfPossible(deps, citations, outcome) {
208
+ if (citations.length === 0)
209
+ return;
210
+ const session = openStoreForDeps(deps);
211
+ try {
212
+ emitPreviewAudit(session.store, citations, outcome);
213
+ }
214
+ finally {
215
+ session.close();
216
+ }
217
+ }
218
+ export function authorizePdfCitationPreview(deps, input) {
219
+ const loaded = loadMessageContext(deps, input.chatId, input.assistantMessageId);
220
+ if ("kind" in loaded) {
221
+ return loaded;
222
+ }
223
+ if (loaded.citations === undefined) {
224
+ return reject("preview-metadata-missing");
225
+ }
226
+ const selected = selectedCitations(loaded.citations, input.marker, input.stableId);
227
+ if (selected.kind !== "selected") {
228
+ if (selected.citations !== undefined && selected.citations.length > 0) {
229
+ emitAuthorizationAuditIfPossible(deps, selected.citations, selected);
230
+ }
231
+ return selected;
232
+ }
233
+ if (selected.citations.length !== 1) {
234
+ const failure = reject("citation-not-found", selected.citations[0] === undefined ? undefined : citationDisplay(selected.citations[0]), selected.citations);
235
+ emitAuthorizationAuditIfPossible(deps, selected.citations, failure);
236
+ return failure;
237
+ }
238
+ const citation = selected.citations[0];
239
+ if (citation === undefined) {
240
+ const failure = reject("citation-not-found");
241
+ return failure;
242
+ }
243
+ const session = openStoreForDeps(deps);
244
+ try {
245
+ const outcome = evaluateStoredCitation(session.store, citation);
246
+ emitPreviewAudit(session.store, [citation], outcome);
247
+ return outcome.kind === "available"
248
+ ? { outcome: "authorized", display: outcome.display, authority: outcome.authority }
249
+ : outcome;
250
+ }
251
+ finally {
252
+ session.close();
253
+ }
254
+ }
255
+ export function projectPdfCitationPreviewAuthorizationResponse(result) {
256
+ if ("kind" in result) {
257
+ return rejectedAuthorizationResponse(result);
258
+ }
259
+ return { outcome: "authorized", display: result.display };
260
+ }
@@ -0,0 +1,41 @@
1
+ import type { UiHandlerDeps } from "./deps.js";
2
+ import type { PdfCitationPreviewAuthority } from "./local-knowledge-preview-service.js";
3
+ export interface PdfCitationPreviewSessionManagerOptions {
4
+ readonly autoSweep?: boolean;
5
+ readonly now?: () => number;
6
+ readonly sweepIntervalMs?: number;
7
+ readonly ttlMs?: number;
8
+ }
9
+ export interface PdfCitationPreviewSession {
10
+ readonly authority: PdfCitationPreviewAuthority;
11
+ readonly documentKey: string;
12
+ readonly expiresAt: string;
13
+ readonly handle: string;
14
+ readonly state: "closed" | "expired" | "open";
15
+ }
16
+ export interface OpenPdfCitationPreviewSessionResult {
17
+ readonly session: PdfCitationPreviewSession;
18
+ readonly reused: boolean;
19
+ }
20
+ export type PdfCitationPreviewSessionLookup = {
21
+ readonly kind: "missing";
22
+ } | {
23
+ readonly kind: "closed";
24
+ readonly session: PdfCitationPreviewSession;
25
+ } | {
26
+ readonly kind: "expired";
27
+ readonly session: PdfCitationPreviewSession;
28
+ } | {
29
+ readonly kind: "open";
30
+ readonly session: PdfCitationPreviewSession;
31
+ };
32
+ export interface PdfCitationPreviewSessionManager {
33
+ closeSession(handle: string): boolean;
34
+ dispose(): void;
35
+ lookupSession(handle: string): PdfCitationPreviewSessionLookup;
36
+ openSession(authority: PdfCitationPreviewAuthority): OpenPdfCitationPreviewSessionResult;
37
+ sweep(): void;
38
+ }
39
+ export declare function createPdfCitationPreviewSessionManager(options?: PdfCitationPreviewSessionManagerOptions): PdfCitationPreviewSessionManager;
40
+ export declare function previewSessionManagerFor(deps: UiHandlerDeps): PdfCitationPreviewSessionManager;
41
+ //# sourceMappingURL=local-knowledge-preview-session-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-knowledge-preview-session-manager.d.ts","sourceRoot":"","sources":["../src/local-knowledge-preview-session-manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,sCAAsC,CAAC;AAMxF,MAAM,WAAW,uCAAuC;IACtD,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IAC5B,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC;IAClC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,SAAS,EAAE,2BAA2B,CAAC;IAChD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;CAC/C;AAED,MAAM,WAAW,mCAAmC;IAClD,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAC;IAC5C,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,MAAM,+BAA+B,GACvC;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;CAAE,GAC5B;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAA;CAAE,GACxE;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAA;CAAE,GACzE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAA;CAAE,CAAC;AAE3E,MAAM,WAAW,gCAAgC;IAC/C,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACtC,OAAO,IAAI,IAAI,CAAC;IAChB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,+BAA+B,CAAC;IAC/D,WAAW,CAAC,SAAS,EAAE,2BAA2B,GAAG,mCAAmC,CAAC;IACzF,KAAK,IAAI,IAAI,CAAC;CACf;AAuJD,wBAAgB,sCAAsC,CACpD,OAAO,GAAE,uCAA4C,GACpD,gCAAgC,CAkBlC;AAID,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,aAAa,GAAG,gCAAgC,CAQ9F"}