@diyor28/context 1.0.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 (280) hide show
  1. package/README.md +270 -0
  2. package/dist/__tests__/attachment-selector.test.d.ts +11 -0
  3. package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
  4. package/dist/__tests__/attachment-selector.test.js +449 -0
  5. package/dist/__tests__/attachment-selector.test.js.map +1 -0
  6. package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
  7. package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
  8. package/dist/__tests__/cache-breakpoints.test.js +398 -0
  9. package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
  10. package/dist/__tests__/codecs.test.d.ts +7 -0
  11. package/dist/__tests__/codecs.test.d.ts.map +1 -0
  12. package/dist/__tests__/codecs.test.js +331 -0
  13. package/dist/__tests__/codecs.test.js.map +1 -0
  14. package/dist/__tests__/compactor.test.d.ts +11 -0
  15. package/dist/__tests__/compactor.test.d.ts.map +1 -0
  16. package/dist/__tests__/compactor.test.js +519 -0
  17. package/dist/__tests__/compactor.test.js.map +1 -0
  18. package/dist/__tests__/context-graph.test.d.ts +7 -0
  19. package/dist/__tests__/context-graph.test.d.ts.map +1 -0
  20. package/dist/__tests__/context-graph.test.js +262 -0
  21. package/dist/__tests__/context-graph.test.js.map +1 -0
  22. package/dist/__tests__/hash.test.d.ts +7 -0
  23. package/dist/__tests__/hash.test.d.ts.map +1 -0
  24. package/dist/__tests__/hash.test.js +228 -0
  25. package/dist/__tests__/hash.test.js.map +1 -0
  26. package/dist/__tests__/integration.test.d.ts +15 -0
  27. package/dist/__tests__/integration.test.d.ts.map +1 -0
  28. package/dist/__tests__/integration.test.js +728 -0
  29. package/dist/__tests__/integration.test.js.map +1 -0
  30. package/dist/__tests__/kind-order.test.d.ts +7 -0
  31. package/dist/__tests__/kind-order.test.d.ts.map +1 -0
  32. package/dist/__tests__/kind-order.test.js +243 -0
  33. package/dist/__tests__/kind-order.test.js.map +1 -0
  34. package/dist/__tests__/phase2-integration.test.d.ts +5 -0
  35. package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
  36. package/dist/__tests__/phase2-integration.test.js +222 -0
  37. package/dist/__tests__/phase2-integration.test.js.map +1 -0
  38. package/dist/__tests__/queries.test.d.ts +7 -0
  39. package/dist/__tests__/queries.test.d.ts.map +1 -0
  40. package/dist/__tests__/queries.test.js +254 -0
  41. package/dist/__tests__/queries.test.js.map +1 -0
  42. package/dist/__tests__/token-estimator.test.d.ts +7 -0
  43. package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
  44. package/dist/__tests__/token-estimator.test.js +267 -0
  45. package/dist/__tests__/token-estimator.test.js.map +1 -0
  46. package/dist/adapters/anthropic-estimator.d.ts +38 -0
  47. package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
  48. package/dist/adapters/anthropic-estimator.js +108 -0
  49. package/dist/adapters/anthropic-estimator.js.map +1 -0
  50. package/dist/adapters/attachment-resolver.d.ts +96 -0
  51. package/dist/adapters/attachment-resolver.d.ts.map +1 -0
  52. package/dist/adapters/attachment-resolver.js +176 -0
  53. package/dist/adapters/attachment-resolver.js.map +1 -0
  54. package/dist/adapters/attachment-selector.d.ts +59 -0
  55. package/dist/adapters/attachment-selector.d.ts.map +1 -0
  56. package/dist/adapters/attachment-selector.js +163 -0
  57. package/dist/adapters/attachment-selector.js.map +1 -0
  58. package/dist/adapters/gemini-estimator.d.ts +27 -0
  59. package/dist/adapters/gemini-estimator.d.ts.map +1 -0
  60. package/dist/adapters/gemini-estimator.js +80 -0
  61. package/dist/adapters/gemini-estimator.js.map +1 -0
  62. package/dist/adapters/index.d.ts +12 -0
  63. package/dist/adapters/index.d.ts.map +1 -0
  64. package/dist/adapters/index.js +28 -0
  65. package/dist/adapters/index.js.map +1 -0
  66. package/dist/adapters/memory-store.d.ts +139 -0
  67. package/dist/adapters/memory-store.d.ts.map +1 -0
  68. package/dist/adapters/memory-store.js +187 -0
  69. package/dist/adapters/memory-store.js.map +1 -0
  70. package/dist/adapters/openai-estimator.d.ts +35 -0
  71. package/dist/adapters/openai-estimator.d.ts.map +1 -0
  72. package/dist/adapters/openai-estimator.js +89 -0
  73. package/dist/adapters/openai-estimator.js.map +1 -0
  74. package/dist/adapters/summarizer.d.ts +121 -0
  75. package/dist/adapters/summarizer.d.ts.map +1 -0
  76. package/dist/adapters/summarizer.js +121 -0
  77. package/dist/adapters/summarizer.js.map +1 -0
  78. package/dist/adapters/token-estimator.d.ts +63 -0
  79. package/dist/adapters/token-estimator.d.ts.map +1 -0
  80. package/dist/adapters/token-estimator.js +37 -0
  81. package/dist/adapters/token-estimator.js.map +1 -0
  82. package/dist/builder/context-builder.d.ts +186 -0
  83. package/dist/builder/context-builder.d.ts.map +1 -0
  84. package/dist/builder/context-builder.js +305 -0
  85. package/dist/builder/context-builder.js.map +1 -0
  86. package/dist/builder/context-fork.d.ts +166 -0
  87. package/dist/builder/context-fork.d.ts.map +1 -0
  88. package/dist/builder/context-fork.js +282 -0
  89. package/dist/builder/context-fork.js.map +1 -0
  90. package/dist/builder/index.d.ts +6 -0
  91. package/dist/builder/index.d.ts.map +1 -0
  92. package/dist/builder/index.js +22 -0
  93. package/dist/builder/index.js.map +1 -0
  94. package/dist/codecs/base.d.ts +18 -0
  95. package/dist/codecs/base.d.ts.map +1 -0
  96. package/dist/codecs/base.js +39 -0
  97. package/dist/codecs/base.js.map +1 -0
  98. package/dist/codecs/conversation-history.codec.d.ts +81 -0
  99. package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
  100. package/dist/codecs/conversation-history.codec.js +89 -0
  101. package/dist/codecs/conversation-history.codec.js.map +1 -0
  102. package/dist/codecs/index.d.ts +31 -0
  103. package/dist/codecs/index.d.ts.map +1 -0
  104. package/dist/codecs/index.js +71 -0
  105. package/dist/codecs/index.js.map +1 -0
  106. package/dist/codecs/redacted-stub.codec.d.ts +32 -0
  107. package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
  108. package/dist/codecs/redacted-stub.codec.js +64 -0
  109. package/dist/codecs/redacted-stub.codec.js.map +1 -0
  110. package/dist/codecs/structured-reference.codec.d.ts +40 -0
  111. package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
  112. package/dist/codecs/structured-reference.codec.js +81 -0
  113. package/dist/codecs/structured-reference.codec.js.map +1 -0
  114. package/dist/codecs/system-rules.codec.d.ts +32 -0
  115. package/dist/codecs/system-rules.codec.d.ts.map +1 -0
  116. package/dist/codecs/system-rules.codec.js +62 -0
  117. package/dist/codecs/system-rules.codec.js.map +1 -0
  118. package/dist/codecs/tool-output.codec.d.ts +66 -0
  119. package/dist/codecs/tool-output.codec.d.ts.map +1 -0
  120. package/dist/codecs/tool-output.codec.js +95 -0
  121. package/dist/codecs/tool-output.codec.js.map +1 -0
  122. package/dist/codecs/tool-schema.codec.d.ts +36 -0
  123. package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
  124. package/dist/codecs/tool-schema.codec.js +74 -0
  125. package/dist/codecs/tool-schema.codec.js.map +1 -0
  126. package/dist/codecs/unsafe-text.codec.d.ts +28 -0
  127. package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
  128. package/dist/codecs/unsafe-text.codec.js +63 -0
  129. package/dist/codecs/unsafe-text.codec.js.map +1 -0
  130. package/dist/graph/context-graph.d.ts +121 -0
  131. package/dist/graph/context-graph.d.ts.map +1 -0
  132. package/dist/graph/context-graph.js +166 -0
  133. package/dist/graph/context-graph.js.map +1 -0
  134. package/dist/graph/index.d.ts +8 -0
  135. package/dist/graph/index.d.ts.map +1 -0
  136. package/dist/graph/index.js +24 -0
  137. package/dist/graph/index.js.map +1 -0
  138. package/dist/graph/kind-order.d.ts +60 -0
  139. package/dist/graph/kind-order.d.ts.map +1 -0
  140. package/dist/graph/kind-order.js +113 -0
  141. package/dist/graph/kind-order.js.map +1 -0
  142. package/dist/graph/queries.d.ts +68 -0
  143. package/dist/graph/queries.d.ts.map +1 -0
  144. package/dist/graph/queries.js +240 -0
  145. package/dist/graph/queries.js.map +1 -0
  146. package/dist/graph/views.d.ts +90 -0
  147. package/dist/graph/views.d.ts.map +1 -0
  148. package/dist/graph/views.js +173 -0
  149. package/dist/graph/views.js.map +1 -0
  150. package/dist/index.d.ts +16 -0
  151. package/dist/index.d.ts.map +1 -0
  152. package/dist/index.js +40 -0
  153. package/dist/index.js.map +1 -0
  154. package/dist/pipeline/compactor.d.ts +128 -0
  155. package/dist/pipeline/compactor.d.ts.map +1 -0
  156. package/dist/pipeline/compactor.js +346 -0
  157. package/dist/pipeline/compactor.js.map +1 -0
  158. package/dist/pipeline/index.d.ts +6 -0
  159. package/dist/pipeline/index.d.ts.map +1 -0
  160. package/dist/pipeline/index.js +22 -0
  161. package/dist/pipeline/index.js.map +1 -0
  162. package/dist/pipeline/summarizer.d.ts +18 -0
  163. package/dist/pipeline/summarizer.d.ts.map +1 -0
  164. package/dist/pipeline/summarizer.js +68 -0
  165. package/dist/pipeline/summarizer.js.map +1 -0
  166. package/dist/policies/default-policy.d.ts +29 -0
  167. package/dist/policies/default-policy.d.ts.map +1 -0
  168. package/dist/policies/default-policy.js +58 -0
  169. package/dist/policies/default-policy.js.map +1 -0
  170. package/dist/policies/index.d.ts +5 -0
  171. package/dist/policies/index.d.ts.map +1 -0
  172. package/dist/policies/index.js +21 -0
  173. package/dist/policies/index.js.map +1 -0
  174. package/dist/providers/anthropic-compiler.d.ts +58 -0
  175. package/dist/providers/anthropic-compiler.d.ts.map +1 -0
  176. package/dist/providers/anthropic-compiler.js +182 -0
  177. package/dist/providers/anthropic-compiler.js.map +1 -0
  178. package/dist/providers/capabilities.d.ts +54 -0
  179. package/dist/providers/capabilities.d.ts.map +1 -0
  180. package/dist/providers/capabilities.js +87 -0
  181. package/dist/providers/capabilities.js.map +1 -0
  182. package/dist/providers/gemini-compiler.d.ts +51 -0
  183. package/dist/providers/gemini-compiler.d.ts.map +1 -0
  184. package/dist/providers/gemini-compiler.js +206 -0
  185. package/dist/providers/gemini-compiler.js.map +1 -0
  186. package/dist/providers/index.d.ts +8 -0
  187. package/dist/providers/index.d.ts.map +1 -0
  188. package/dist/providers/index.js +24 -0
  189. package/dist/providers/index.js.map +1 -0
  190. package/dist/providers/openai-compiler.d.ts +46 -0
  191. package/dist/providers/openai-compiler.d.ts.map +1 -0
  192. package/dist/providers/openai-compiler.js +149 -0
  193. package/dist/providers/openai-compiler.js.map +1 -0
  194. package/dist/types/attachment.d.ts +62 -0
  195. package/dist/types/attachment.d.ts.map +1 -0
  196. package/dist/types/attachment.js +6 -0
  197. package/dist/types/attachment.js.map +1 -0
  198. package/dist/types/block.d.ts +61 -0
  199. package/dist/types/block.d.ts.map +1 -0
  200. package/dist/types/block.js +8 -0
  201. package/dist/types/block.js.map +1 -0
  202. package/dist/types/codec.d.ts +58 -0
  203. package/dist/types/codec.d.ts.map +1 -0
  204. package/dist/types/codec.js +6 -0
  205. package/dist/types/codec.js.map +1 -0
  206. package/dist/types/compiled.d.ts +91 -0
  207. package/dist/types/compiled.d.ts.map +1 -0
  208. package/dist/types/compiled.js +6 -0
  209. package/dist/types/compiled.js.map +1 -0
  210. package/dist/types/hash.d.ts +24 -0
  211. package/dist/types/hash.d.ts.map +1 -0
  212. package/dist/types/hash.js +49 -0
  213. package/dist/types/hash.js.map +1 -0
  214. package/dist/types/index.d.ts +10 -0
  215. package/dist/types/index.d.ts.map +1 -0
  216. package/dist/types/index.js +26 -0
  217. package/dist/types/index.js.map +1 -0
  218. package/dist/types/policy.d.ts +128 -0
  219. package/dist/types/policy.d.ts.map +1 -0
  220. package/dist/types/policy.js +55 -0
  221. package/dist/types/policy.js.map +1 -0
  222. package/package.json +55 -0
  223. package/postcss.config.js +4 -0
  224. package/src/__tests__/attachment-selector.test.ts +559 -0
  225. package/src/__tests__/cache-breakpoints.test.ts +566 -0
  226. package/src/__tests__/codecs.test.ts +417 -0
  227. package/src/__tests__/compactor.test.ts +608 -0
  228. package/src/__tests__/context-graph.test.ts +383 -0
  229. package/src/__tests__/hash.test.ts +274 -0
  230. package/src/__tests__/integration.test.ts +866 -0
  231. package/src/__tests__/kind-order.test.ts +312 -0
  232. package/src/__tests__/phase2-integration.test.ts +253 -0
  233. package/src/__tests__/queries.test.ts +387 -0
  234. package/src/__tests__/token-estimator.test.ts +326 -0
  235. package/src/adapters/anthropic-estimator.ts +125 -0
  236. package/src/adapters/attachment-resolver.ts +295 -0
  237. package/src/adapters/attachment-selector.ts +218 -0
  238. package/src/adapters/gemini-estimator.ts +93 -0
  239. package/src/adapters/index.ts +12 -0
  240. package/src/adapters/memory-store.ts +299 -0
  241. package/src/adapters/openai-estimator.ts +105 -0
  242. package/src/adapters/summarizer.ts +250 -0
  243. package/src/adapters/token-estimator.ts +74 -0
  244. package/src/builder/context-builder.ts +467 -0
  245. package/src/builder/context-fork.ts +471 -0
  246. package/src/builder/index.ts +6 -0
  247. package/src/codecs/base.ts +36 -0
  248. package/src/codecs/conversation-history.codec.ts +108 -0
  249. package/src/codecs/index.ts +57 -0
  250. package/src/codecs/redacted-stub.codec.ts +76 -0
  251. package/src/codecs/structured-reference.codec.ts +96 -0
  252. package/src/codecs/system-rules.codec.ts +74 -0
  253. package/src/codecs/tool-output.codec.ts +109 -0
  254. package/src/codecs/tool-schema.codec.ts +87 -0
  255. package/src/codecs/unsafe-text.codec.ts +74 -0
  256. package/src/graph/context-graph.ts +205 -0
  257. package/src/graph/index.ts +8 -0
  258. package/src/graph/kind-order.ts +125 -0
  259. package/src/graph/queries.ts +306 -0
  260. package/src/graph/views.ts +255 -0
  261. package/src/index.ts +31 -0
  262. package/src/pipeline/compactor.ts +563 -0
  263. package/src/pipeline/index.ts +6 -0
  264. package/src/pipeline/summarizer.ts +76 -0
  265. package/src/policies/default-policy.ts +69 -0
  266. package/src/policies/index.ts +5 -0
  267. package/src/providers/anthropic-compiler.ts +294 -0
  268. package/src/providers/capabilities.ts +144 -0
  269. package/src/providers/gemini-compiler.ts +272 -0
  270. package/src/providers/index.ts +8 -0
  271. package/src/providers/openai-compiler.ts +191 -0
  272. package/src/types/attachment.ts +86 -0
  273. package/src/types/block.ts +84 -0
  274. package/src/types/codec.ts +68 -0
  275. package/src/types/compiled.ts +109 -0
  276. package/src/types/hash.ts +58 -0
  277. package/src/types/index.ts +10 -0
  278. package/src/types/policy.ts +194 -0
  279. package/tsconfig.json +21 -0
  280. package/vitest.config.ts +21 -0
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ /**
3
+ * AnthropicTokenEstimator: Exact token counting via Anthropic API.
4
+ *
5
+ * Uses client.messages.countTokens() for exact counts.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.AnthropicTokenEstimator = void 0;
9
+ const token_estimator_js_1 = require("./token-estimator.js");
10
+ /**
11
+ * AnthropicTokenEstimator using API-based exact counting.
12
+ */
13
+ class AnthropicTokenEstimator {
14
+ client;
15
+ model;
16
+ /**
17
+ * Create an AnthropicTokenEstimator.
18
+ *
19
+ * @param client - Anthropic SDK client
20
+ * @param model - Model name (e.g., 'claude-sonnet-4-5')
21
+ */
22
+ constructor(client, model = 'claude-sonnet-4-5') {
23
+ this.client = client;
24
+ this.model = model;
25
+ }
26
+ /**
27
+ * Estimate tokens for a single block using Anthropic API.
28
+ *
29
+ * @param block - Block to estimate
30
+ * @returns Token estimate (exact confidence)
31
+ */
32
+ async estimateBlock(block) {
33
+ try {
34
+ // Serialize block to text
35
+ const text = (0, token_estimator_js_1.serializeBlockForEstimation)(block);
36
+ // Use Anthropic API for exact count
37
+ const result = await this.client.messages.countTokens({
38
+ model: this.model,
39
+ messages: [
40
+ {
41
+ role: 'user',
42
+ content: text,
43
+ },
44
+ ],
45
+ });
46
+ return {
47
+ tokens: result.input_tokens,
48
+ confidence: 'exact',
49
+ };
50
+ }
51
+ catch (error) {
52
+ // Fallback to heuristic on API error
53
+ console.warn('[AnthropicTokenEstimator] API error, falling back to heuristic:', error);
54
+ const text = (0, token_estimator_js_1.serializeBlockForEstimation)(block);
55
+ return {
56
+ tokens: (0, token_estimator_js_1.heuristicTokenCount)(text),
57
+ confidence: 'low',
58
+ };
59
+ }
60
+ }
61
+ /**
62
+ * Estimate tokens for multiple blocks.
63
+ * Batches API calls for efficiency.
64
+ *
65
+ * @param blocks - Blocks to estimate
66
+ * @returns Token estimate (exact confidence if all succeed)
67
+ */
68
+ async estimate(blocks) {
69
+ if (blocks.length === 0) {
70
+ return { tokens: 0, confidence: 'exact' };
71
+ }
72
+ try {
73
+ // Concatenate all block texts
74
+ const combinedText = blocks
75
+ .map((block) => (0, token_estimator_js_1.serializeBlockForEstimation)(block))
76
+ .join('\n\n');
77
+ // Single API call for all blocks
78
+ const result = await this.client.messages.countTokens({
79
+ model: this.model,
80
+ messages: [
81
+ {
82
+ role: 'user',
83
+ content: combinedText,
84
+ },
85
+ ],
86
+ });
87
+ return {
88
+ tokens: result.input_tokens,
89
+ confidence: 'exact',
90
+ };
91
+ }
92
+ catch (error) {
93
+ // Fallback: sum individual heuristic estimates
94
+ console.warn('[AnthropicTokenEstimator] API error, falling back to heuristic:', error);
95
+ let totalTokens = 0;
96
+ for (const block of blocks) {
97
+ const text = (0, token_estimator_js_1.serializeBlockForEstimation)(block);
98
+ totalTokens += (0, token_estimator_js_1.heuristicTokenCount)(text);
99
+ }
100
+ return {
101
+ tokens: totalTokens,
102
+ confidence: 'low',
103
+ };
104
+ }
105
+ }
106
+ }
107
+ exports.AnthropicTokenEstimator = AnthropicTokenEstimator;
108
+ //# sourceMappingURL=anthropic-estimator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic-estimator.js","sourceRoot":"","sources":["../../src/adapters/anthropic-estimator.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAKH,6DAG8B;AAE9B;;GAEG;AACH,MAAa,uBAAuB;IACjB,MAAM,CAAY;IAClB,KAAK,CAAS;IAE/B;;;;;OAKG;IACH,YAAY,MAAiB,EAAE,QAAgB,mBAAmB;QAChE,IAAI,CAAC,MAAM,GAAG,MAA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ACjC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC;gBACpD,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE,YAAY;qBACtB;iBACF;aACF,CAAC,CAAC;YAEH,OAAO;gBACL,MAAM,EAAE,MAAM,CAAC,YAAY;gBAC3B,UAAU,EAAE,OAAO;aACpB,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,+CAA+C;YAC/C,OAAO,CAAC,IAAI,CACV,iEAAiE,EACjE,KAAK,CACN,CAAC;YAEF,IAAI,WAAW,GAAG,CAAC,CAAC;YACpB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,MAAM,IAAI,GAAG,IAAA,gDAA2B,EAAC,KAAK,CAAC,CAAC;gBAChD,WAAW,IAAI,IAAA,wCAAmB,EAAC,IAAI,CAAC,CAAC;YAC3C,CAAC;YAED,OAAO;gBACL,MAAM,EAAE,WAAW;gBACnB,UAAU,EAAE,KAAK;aAClB,CAAC;QACJ,CAAC;IACH,CAAC;CACF;AA3GD,0DA2GC"}
@@ -0,0 +1,96 @@
1
+ /**
2
+ * AttachmentResolver: Versioned resolution of attachments with provenance.
3
+ *
4
+ * Responsibilities:
5
+ * - Resolve attachment references to actual content
6
+ * - Support different resolution levels (metadata_only, extract, full)
7
+ * - Generate derived blocks with provenance tracking
8
+ * - Compute snapshot hashes for reproducibility
9
+ */
10
+ import type { ContextBlock } from '../types/block.js';
11
+ import type { AttachmentRef, AttachmentMeta, AttachmentMimeType } from '../types/attachment.js';
12
+ /**
13
+ * Resolution level for attachments.
14
+ */
15
+ export type AttachmentResolutionLevel = 'metadata_only' | 'extract' | 'full';
16
+ /**
17
+ * Resolved attachment part (for multi-part content).
18
+ */
19
+ export interface AttachmentPart {
20
+ /** Part type */
21
+ type: 'text' | 'image' | 'json';
22
+ /** Text content (for text parts) */
23
+ text?: string;
24
+ /** Image content (base64-encoded, for image parts) */
25
+ image?: {
26
+ data: string;
27
+ mimeType: AttachmentMimeType;
28
+ };
29
+ /** JSON content (for json parts) */
30
+ json?: unknown;
31
+ /** Optional description */
32
+ description?: string;
33
+ }
34
+ /**
35
+ * Resolved attachment with content and derived blocks.
36
+ * Extended version with provenance tracking.
37
+ */
38
+ export interface ResolvedAttachmentWithProvenance extends AttachmentMeta {
39
+ /** Resolution level used */
40
+ level: AttachmentResolutionLevel;
41
+ /** Resolved content parts */
42
+ parts: AttachmentPart[];
43
+ /** Derived blocks (generated from attachment content) */
44
+ derivedBlocks: ContextBlock[];
45
+ /** Snapshot hash (for reproducibility) */
46
+ snapshotHash: string;
47
+ /** Resolver version (for debugging) */
48
+ resolverVersion: string;
49
+ }
50
+ /**
51
+ * Attachment resolver interface.
52
+ */
53
+ export interface AttachmentResolver {
54
+ /** Resolver identifier */
55
+ resolverId: string;
56
+ /** Resolver version */
57
+ version: string;
58
+ /**
59
+ * Resolve an attachment reference to actual content.
60
+ *
61
+ * @param ref - Attachment reference
62
+ * @param level - Resolution level
63
+ * @returns Resolved attachment with derived blocks
64
+ */
65
+ resolve(ref: AttachmentRef, level: AttachmentResolutionLevel): Promise<ResolvedAttachmentWithProvenance>;
66
+ }
67
+ /**
68
+ * Create a snapshot hash for a resolved attachment.
69
+ * Used for reproducibility and change detection.
70
+ *
71
+ * @param attachmentId - Attachment ID
72
+ * @param level - Resolution level
73
+ * @param parts - Resolved parts
74
+ * @param resolverVersion - Resolver version
75
+ * @returns Snapshot hash
76
+ */
77
+ export declare function createSnapshotHash(attachmentId: string, level: AttachmentResolutionLevel, parts: AttachmentPart[], resolverVersion: string): string;
78
+ /**
79
+ * Create derived blocks from resolved attachment parts.
80
+ *
81
+ * @param attachmentId - Attachment ID
82
+ * @param parts - Resolved parts
83
+ * @param parentHash - Parent block hash (for provenance)
84
+ * @returns Array of derived blocks
85
+ */
86
+ export declare function createDerivedBlocks(attachmentId: string, parts: AttachmentPart[], parentHash: string): ContextBlock[];
87
+ /**
88
+ * Default attachment resolver (stub implementation).
89
+ * In production, this would integrate with actual storage backends (S3, GCS, etc.).
90
+ */
91
+ export declare class DefaultAttachmentResolver implements AttachmentResolver {
92
+ resolverId: string;
93
+ version: string;
94
+ resolve(ref: AttachmentRef, level: AttachmentResolutionLevel): Promise<ResolvedAttachmentWithProvenance>;
95
+ }
96
+ //# sourceMappingURL=attachment-resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-resolver.d.ts","sourceRoot":"","sources":["../../src/adapters/attachment-resolver.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAa,MAAM,mBAAmB,CAAC;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAGhG;;GAEG;AACH,MAAM,MAAM,yBAAyB,GACjC,eAAe,GACf,SAAS,GACT,MAAM,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,gBAAgB;IAChB,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;IAEhC,oCAAoC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,sDAAsD;IACtD,KAAK,CAAC,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,kBAAkB,CAAC;KAC9B,CAAC;IAEF,oCAAoC;IACpC,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,2BAA2B;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;GAGG;AACH,MAAM,WAAW,gCAAiC,SAAQ,cAAc;IACtE,4BAA4B;IAC5B,KAAK,EAAE,yBAAyB,CAAC;IAEjC,6BAA6B;IAC7B,KAAK,EAAE,cAAc,EAAE,CAAC;IAExB,yDAAyD;IACzD,aAAa,EAAE,YAAY,EAAE,CAAC;IAE9B,0CAA0C;IAC1C,YAAY,EAAE,MAAM,CAAC;IAErB,uCAAuC;IACvC,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,0BAA0B;IAC1B,UAAU,EAAE,MAAM,CAAC;IAEnB,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;;;OAMG;IACH,OAAO,CACL,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,yBAAyB,GAC/B,OAAO,CAAC,gCAAgC,CAAC,CAAC;CAC9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,yBAAyB,EAChC,KAAK,EAAE,cAAc,EAAE,EACvB,eAAe,EAAE,MAAM,GACtB,MAAM,CAkBR;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,cAAc,EAAE,EACvB,UAAU,EAAE,MAAM,GACjB,YAAY,EAAE,CAsFhB;AAED;;;GAGG;AACH,qBAAa,yBAA0B,YAAW,kBAAkB;IAClE,UAAU,SAAsB;IAChC,OAAO,SAAW;IAEZ,OAAO,CACX,GAAG,EAAE,aAAa,EAClB,KAAK,EAAE,yBAAyB,GAC/B,OAAO,CAAC,gCAAgC,CAAC;CA2D7C"}
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ /**
3
+ * AttachmentResolver: Versioned resolution of attachments with provenance.
4
+ *
5
+ * Responsibilities:
6
+ * - Resolve attachment references to actual content
7
+ * - Support different resolution levels (metadata_only, extract, full)
8
+ * - Generate derived blocks with provenance tracking
9
+ * - Compute snapshot hashes for reproducibility
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DefaultAttachmentResolver = void 0;
13
+ exports.createSnapshotHash = createSnapshotHash;
14
+ exports.createDerivedBlocks = createDerivedBlocks;
15
+ const hash_js_1 = require("../types/hash.js");
16
+ /**
17
+ * Create a snapshot hash for a resolved attachment.
18
+ * Used for reproducibility and change detection.
19
+ *
20
+ * @param attachmentId - Attachment ID
21
+ * @param level - Resolution level
22
+ * @param parts - Resolved parts
23
+ * @param resolverVersion - Resolver version
24
+ * @returns Snapshot hash
25
+ */
26
+ function createSnapshotHash(attachmentId, level, parts, resolverVersion) {
27
+ const snapshot = {
28
+ attachmentId,
29
+ level,
30
+ parts: parts.map((part) => ({
31
+ type: part.type,
32
+ // Hash content without including full data
33
+ textHash: part.text ? (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'text', codecVersion: '1.0.0' }, { text: part.text }) : undefined,
34
+ imageHash: part.image ? (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'image', codecVersion: '1.0.0' }, { data: part.image.data, mimeType: part.image.mimeType }) : undefined,
35
+ jsonHash: part.json ? (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'json', codecVersion: '1.0.0' }, part.json) : undefined,
36
+ })),
37
+ resolverVersion,
38
+ };
39
+ return (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'attachment-snapshot', codecVersion: '1.0.0' }, snapshot);
40
+ }
41
+ /**
42
+ * Create derived blocks from resolved attachment parts.
43
+ *
44
+ * @param attachmentId - Attachment ID
45
+ * @param parts - Resolved parts
46
+ * @param parentHash - Parent block hash (for provenance)
47
+ * @returns Array of derived blocks
48
+ */
49
+ function createDerivedBlocks(attachmentId, parts, parentHash) {
50
+ const blocks = [];
51
+ for (let i = 0; i < parts.length; i++) {
52
+ const part = parts[i];
53
+ if (part.type === 'text' && part.text) {
54
+ const payload = {
55
+ text: part.text,
56
+ source: attachmentId,
57
+ partIndex: i,
58
+ description: part.description,
59
+ };
60
+ const blockHash = (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'attachment-text', codecVersion: '1.0.0' }, payload);
61
+ blocks.push({
62
+ blockHash,
63
+ meta: {
64
+ kind: 'reference',
65
+ sensitivity: 'public',
66
+ codecId: 'attachment-text',
67
+ codecVersion: '1.0.0',
68
+ createdAt: Math.floor(Date.now() / 1000),
69
+ source: attachmentId,
70
+ },
71
+ payload,
72
+ });
73
+ }
74
+ else if (part.type === 'image' && part.image) {
75
+ const payload = {
76
+ data: part.image.data,
77
+ mimeType: part.image.mimeType,
78
+ source: attachmentId,
79
+ partIndex: i,
80
+ description: part.description,
81
+ };
82
+ const blockHash = (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'attachment-image', codecVersion: '1.0.0' }, payload);
83
+ blocks.push({
84
+ blockHash,
85
+ meta: {
86
+ kind: 'reference',
87
+ sensitivity: 'public',
88
+ codecId: 'attachment-image',
89
+ codecVersion: '1.0.0',
90
+ createdAt: Math.floor(Date.now() / 1000),
91
+ source: attachmentId,
92
+ },
93
+ payload,
94
+ });
95
+ }
96
+ else if (part.type === 'json' && part.json) {
97
+ const payload = {
98
+ data: part.json,
99
+ source: attachmentId,
100
+ partIndex: i,
101
+ description: part.description,
102
+ };
103
+ const blockHash = (0, hash_js_1.computeBlockHash)({ kind: 'reference', sensitivity: 'public', codecId: 'attachment-json', codecVersion: '1.0.0' }, payload);
104
+ blocks.push({
105
+ blockHash,
106
+ meta: {
107
+ kind: 'reference',
108
+ sensitivity: 'public',
109
+ codecId: 'attachment-json',
110
+ codecVersion: '1.0.0',
111
+ createdAt: Math.floor(Date.now() / 1000),
112
+ source: attachmentId,
113
+ },
114
+ payload,
115
+ });
116
+ }
117
+ }
118
+ return blocks;
119
+ }
120
+ /**
121
+ * Default attachment resolver (stub implementation).
122
+ * In production, this would integrate with actual storage backends (S3, GCS, etc.).
123
+ */
124
+ class DefaultAttachmentResolver {
125
+ resolverId = 'default-resolver';
126
+ version = '1.0.0';
127
+ async resolve(ref, level) {
128
+ // Stub implementation - in production, this would:
129
+ // 1. Fetch attachment metadata from storage
130
+ // 2. Fetch content based on resolution level
131
+ // 3. Process content (extract text, OCR images, etc.)
132
+ // 4. Generate derived blocks
133
+ // For now, return a minimal resolved attachment
134
+ const parts = [];
135
+ if (level === 'metadata_only') {
136
+ // No content parts for metadata_only
137
+ }
138
+ else if (level === 'extract') {
139
+ // Extract text/structured data
140
+ parts.push({
141
+ type: 'text',
142
+ text: `Extracted content from ${ref.attachmentId}`,
143
+ description: ref.description,
144
+ });
145
+ }
146
+ else if (level === 'full') {
147
+ // Full content (would be base64-encoded image data in production)
148
+ parts.push({
149
+ type: 'text',
150
+ text: `Full content of ${ref.attachmentId}`,
151
+ description: ref.description,
152
+ });
153
+ }
154
+ const snapshotHash = createSnapshotHash(ref.attachmentId, level, parts, this.version);
155
+ const derivedBlocks = createDerivedBlocks(ref.attachmentId, parts, ref.attachmentId // Parent hash (would be actual block hash in production)
156
+ );
157
+ return {
158
+ // Metadata (would be fetched from storage in production)
159
+ attachmentId: ref.attachmentId,
160
+ mimeType: 'text/plain',
161
+ sizeBytes: 0,
162
+ filename: ref.attachmentId,
163
+ storage: 'local',
164
+ storagePath: '',
165
+ createdAt: Math.floor(Date.now() / 1000),
166
+ // Resolution results
167
+ level,
168
+ parts,
169
+ derivedBlocks,
170
+ snapshotHash,
171
+ resolverVersion: this.version,
172
+ };
173
+ }
174
+ }
175
+ exports.DefaultAttachmentResolver = DefaultAttachmentResolver;
176
+ //# sourceMappingURL=attachment-resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-resolver.js","sourceRoot":"","sources":["../../src/adapters/attachment-resolver.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AA2FH,gDAuBC;AAUD,kDA0FC;AAlND,8CAAoD;AA6EpD;;;;;;;;;GASG;AACH,SAAgB,kBAAkB,CAChC,YAAoB,EACpB,KAAgC,EAChC,KAAuB,EACvB,eAAuB;IAEvB,MAAM,QAAQ,GAAG;QACf,YAAY;QACZ,KAAK;QACL,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,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WAAW,EAAE,EAAE;YACf,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;YAExC,qBAAqB;YACrB,KAAK;YACL,KAAK;YACL,aAAa;YACb,YAAY;YACZ,eAAe,EAAE,IAAI,CAAC,OAAO;SAC9B,CAAC;IACJ,CAAC;CACF;AAlED,8DAkEC"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * AttachmentSelector: Token budget-aware attachment selection.
3
+ *
4
+ * Selects attachments based on policy-driven ranking with deterministic ordering.
5
+ */
6
+ import type { ResolvedAttachment, AttachmentRef } from '../types/attachment.js';
7
+ import type { AttachmentPolicy, AttachmentPurpose } from '../types/policy.js';
8
+ import type { TokenEstimator } from './token-estimator.js';
9
+ /**
10
+ * Attachment with selection metadata.
11
+ */
12
+ export interface RankedAttachment extends ResolvedAttachment {
13
+ /** Attachment purpose (for ranking) */
14
+ purpose: AttachmentPurpose;
15
+ /** User explicitly mentioned this attachment */
16
+ userMention: boolean;
17
+ /** Ranking score (lower = higher priority) */
18
+ rankScore: number;
19
+ }
20
+ /**
21
+ * Selected attachments result.
22
+ */
23
+ export interface SelectedAttachments {
24
+ /** Selected attachments (within budget) */
25
+ selected: ResolvedAttachment[];
26
+ /** Excluded attachments (over budget) */
27
+ excluded: AttachmentRef[];
28
+ /** Total tokens used by selected attachments */
29
+ tokensUsed: number;
30
+ }
31
+ /**
32
+ * AttachmentSelector: Select attachments with policy enforcement.
33
+ */
34
+ export declare class AttachmentSelector {
35
+ private readonly policy;
36
+ private readonly estimator;
37
+ constructor(policy: AttachmentPolicy, estimator: TokenEstimator);
38
+ /**
39
+ * Select attachments within token budget.
40
+ * Deterministic: same inputs → same selection order.
41
+ *
42
+ * @param attachments - Ranked attachments to select from
43
+ * @returns Selected attachments result
44
+ */
45
+ selectAttachments(attachments: RankedAttachment[]): Promise<SelectedAttachments>;
46
+ /**
47
+ * Select attachments from raw list with purpose/mention metadata.
48
+ * Convenience method that wraps selectAttachments.
49
+ *
50
+ * @param attachments - Resolved attachments
51
+ * @param metadata - Per-attachment metadata
52
+ * @returns Selected attachments result
53
+ */
54
+ selectFromList(attachments: ResolvedAttachment[], metadata: Map<string, {
55
+ purpose: AttachmentPurpose;
56
+ userMention: boolean;
57
+ }>): Promise<SelectedAttachments>;
58
+ }
59
+ //# sourceMappingURL=attachment-selector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-selector.d.ts","sourceRoot":"","sources":["../../src/adapters/attachment-selector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAoB,MAAM,oBAAoB,CAAC;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,gBAAiB,SAAQ,kBAAkB;IAC1D,uCAAuC;IACvC,OAAO,EAAE,iBAAiB,CAAC;IAE3B,gDAAgD;IAChD,WAAW,EAAE,OAAO,CAAC;IAErB,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,2CAA2C;IAC3C,QAAQ,EAAE,kBAAkB,EAAE,CAAC;IAE/B,yCAAyC;IACzC,QAAQ,EAAE,aAAa,EAAE,CAAC;IAE1B,gDAAgD;IAChD,UAAU,EAAE,MAAM,CAAC;CACpB;AA4ED;;GAEG;AACH,qBAAa,kBAAkB;IAE3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS;gBADT,MAAM,EAAE,gBAAgB,EACxB,SAAS,EAAE,cAAc;IAG5C;;;;;;OAMG;IACG,iBAAiB,CACrB,WAAW,EAAE,gBAAgB,EAAE,GAC9B,OAAO,CAAC,mBAAmB,CAAC;IA0D/B;;;;;;;OAOG;IACG,cAAc,CAClB,WAAW,EAAE,kBAAkB,EAAE,EACjC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,iBAAiB,CAAC;QAAC,WAAW,EAAE,OAAO,CAAA;KAAE,CAAC,GAC1E,OAAO,CAAC,mBAAmB,CAAC;CAkBhC"}
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ /**
3
+ * AttachmentSelector: Token budget-aware attachment selection.
4
+ *
5
+ * Selects attachments based on policy-driven ranking with deterministic ordering.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.AttachmentSelector = void 0;
9
+ /**
10
+ * Compute rank score based on selection strategy.
11
+ *
12
+ * @param attachment - Ranked attachment
13
+ * @param rankBy - Ranking criteria in priority order
14
+ * @param purposePriority - Purpose priority mapping
15
+ * @returns Rank score (lower = higher priority)
16
+ */
17
+ function computeRankScore(attachment, rankBy, purposePriority) {
18
+ let score = 0;
19
+ let multiplier = 1000; // High multiplier for primary criterion
20
+ for (const criterion of rankBy) {
21
+ switch (criterion) {
22
+ case 'purpose':
23
+ score += purposePriority[attachment.purpose] * multiplier;
24
+ break;
25
+ case 'user_mention':
26
+ // User mention: 0 if mentioned, 1 if not
27
+ score += (attachment.userMention ? 0 : 1) * multiplier;
28
+ break;
29
+ case 'recency':
30
+ // More recent = lower score (inverted createdAt)
31
+ score += (Date.now() / 1000 - attachment.createdAt) * multiplier * 0.01;
32
+ break;
33
+ }
34
+ multiplier /= 100; // Reduce multiplier for next criterion
35
+ }
36
+ return score;
37
+ }
38
+ /**
39
+ * Estimate tokens for an attachment.
40
+ *
41
+ * @param attachment - Resolved attachment
42
+ * @returns Estimated tokens
43
+ */
44
+ function estimateAttachmentTokens(attachment) {
45
+ // For text attachments, estimate from text content
46
+ if (attachment.text) {
47
+ // Rough estimate: 1 token per 4 characters
48
+ return Math.ceil(attachment.text.length / 4);
49
+ }
50
+ // For images, use a heuristic based on size
51
+ // (actual token count depends on image dimensions and provider)
52
+ if (attachment.mimeType.startsWith('image/')) {
53
+ // Rough estimate: ~85 tokens per 512x512 tile
54
+ // Assume average image is ~4 tiles
55
+ return 340;
56
+ }
57
+ // For PDFs and JSON, use a conservative estimate
58
+ if (attachment.mimeType === 'application/pdf' || attachment.mimeType === 'application/json') {
59
+ // Estimate based on content length if available
60
+ if (attachment.content) {
61
+ // Base64 content: approximate text length
62
+ const textLength = Math.floor(attachment.content.length * 0.75);
63
+ return Math.ceil(textLength / 4); // Rough token estimate
64
+ }
65
+ // Fallback: 500 tokens per attachment
66
+ return 500;
67
+ }
68
+ // Default fallback
69
+ return 100;
70
+ }
71
+ /**
72
+ * AttachmentSelector: Select attachments with policy enforcement.
73
+ */
74
+ class AttachmentSelector {
75
+ policy;
76
+ estimator;
77
+ constructor(policy, estimator) {
78
+ this.policy = policy;
79
+ this.estimator = estimator;
80
+ }
81
+ /**
82
+ * Select attachments within token budget.
83
+ * Deterministic: same inputs → same selection order.
84
+ *
85
+ * @param attachments - Ranked attachments to select from
86
+ * @returns Selected attachments result
87
+ */
88
+ async selectAttachments(attachments) {
89
+ const { maxTokensTotal, selectionStrategy } = this.policy;
90
+ const { rankBy, purposePriority } = selectionStrategy;
91
+ // Use default purpose priority if not provided
92
+ const effectivePurposePriority = purposePriority ?? {
93
+ evidence: 1,
94
+ input: 2,
95
+ context: 3,
96
+ artifact: 4,
97
+ };
98
+ // Compute rank scores for all attachments
99
+ const scoredAttachments = attachments.map((attachment) => ({
100
+ ...attachment,
101
+ rankScore: computeRankScore(attachment, rankBy, effectivePurposePriority),
102
+ }));
103
+ // Sort by rank score (ascending - lower score = higher priority)
104
+ // Use attachmentId as tiebreaker for deterministic ordering
105
+ scoredAttachments.sort((a, b) => {
106
+ if (a.rankScore !== b.rankScore) {
107
+ return a.rankScore - b.rankScore;
108
+ }
109
+ return a.attachmentId.localeCompare(b.attachmentId);
110
+ });
111
+ // Select attachments within budget
112
+ const selected = [];
113
+ const excluded = [];
114
+ let tokensUsed = 0;
115
+ for (const attachment of scoredAttachments) {
116
+ // Estimate tokens for this attachment
117
+ const attachmentTokens = estimateAttachmentTokens(attachment);
118
+ // Check if adding this attachment would exceed budget
119
+ if (tokensUsed + attachmentTokens > maxTokensTotal) {
120
+ // Budget exceeded - exclude this attachment
121
+ excluded.push({
122
+ attachmentId: attachment.attachmentId,
123
+ description: attachment.filename,
124
+ });
125
+ continue;
126
+ }
127
+ // Include attachment
128
+ selected.push(attachment);
129
+ tokensUsed += attachmentTokens;
130
+ }
131
+ return {
132
+ selected,
133
+ excluded,
134
+ tokensUsed,
135
+ };
136
+ }
137
+ /**
138
+ * Select attachments from raw list with purpose/mention metadata.
139
+ * Convenience method that wraps selectAttachments.
140
+ *
141
+ * @param attachments - Resolved attachments
142
+ * @param metadata - Per-attachment metadata
143
+ * @returns Selected attachments result
144
+ */
145
+ async selectFromList(attachments, metadata) {
146
+ // Build ranked attachments
147
+ const ranked = attachments.map((attachment) => {
148
+ const meta = metadata.get(attachment.attachmentId) ?? {
149
+ purpose: 'context',
150
+ userMention: false,
151
+ };
152
+ return {
153
+ ...attachment,
154
+ purpose: meta.purpose,
155
+ userMention: meta.userMention,
156
+ rankScore: 0, // Will be computed in selectAttachments
157
+ };
158
+ });
159
+ return this.selectAttachments(ranked);
160
+ }
161
+ }
162
+ exports.AttachmentSelector = AttachmentSelector;
163
+ //# sourceMappingURL=attachment-selector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attachment-selector.js","sourceRoot":"","sources":["../../src/adapters/attachment-selector.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAkCH;;;;;;;GAOG;AACH,SAAS,gBAAgB,CACvB,UAA4B,EAC5B,MAA0B,EAC1B,eAAkD;IAElD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,UAAU,GAAG,IAAI,CAAC,CAAC,wCAAwC;IAE/D,KAAK,MAAM,SAAS,IAAI,MAAM,EAAE,CAAC;QAC/B,QAAQ,SAAS,EAAE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,CAAC;CACF;AAtGD,gDAsGC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * GeminiTokenEstimator: High-confidence token counting via tiktoken.
3
+ *
4
+ * Uses tiktoken with gpt-4 encoding as a good approximation for Gemini.
5
+ */
6
+ import type { ContextBlock } from '../types/block.js';
7
+ import type { TokenEstimator, TokenEstimate } from './token-estimator.js';
8
+ /**
9
+ * GeminiTokenEstimator using tiktoken (gpt-4 encoding as approximation).
10
+ */
11
+ export declare class GeminiTokenEstimator implements TokenEstimator {
12
+ /**
13
+ * Estimate tokens for a single block using tiktoken.
14
+ *
15
+ * @param block - Block to estimate
16
+ * @returns Token estimate (high confidence)
17
+ */
18
+ estimateBlock(block: ContextBlock<unknown>): Promise<TokenEstimate>;
19
+ /**
20
+ * Estimate tokens for multiple blocks.
21
+ *
22
+ * @param blocks - Blocks to estimate
23
+ * @returns Token estimate (high confidence if all succeed)
24
+ */
25
+ estimate(blocks: ContextBlock<unknown>[]): Promise<TokenEstimate>;
26
+ }
27
+ //# sourceMappingURL=gemini-estimator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gemini-estimator.d.ts","sourceRoot":"","sources":["../../src/adapters/gemini-estimator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAM1E;;GAEG;AACH,qBAAa,oBAAqB,YAAW,cAAc;IACzD;;;;;OAKG;IACG,aAAa,CAAC,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC;IAyBzE;;;;;OAKG;IACG,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,aAAa,CAAC;CAqCxE"}