@trazum/core 1.8.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 (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +163 -0
  3. package/dist/advisories.d.ts +23 -0
  4. package/dist/advisories.d.ts.map +1 -0
  5. package/dist/advisories.js +376 -0
  6. package/dist/advisories.js.map +1 -0
  7. package/dist/aws-sigv4.d.ts +88 -0
  8. package/dist/aws-sigv4.d.ts.map +1 -0
  9. package/dist/aws-sigv4.js +117 -0
  10. package/dist/aws-sigv4.js.map +1 -0
  11. package/dist/baseline.d.ts +171 -0
  12. package/dist/baseline.d.ts.map +1 -0
  13. package/dist/baseline.js +273 -0
  14. package/dist/baseline.js.map +1 -0
  15. package/dist/cache.d.ts +26 -0
  16. package/dist/cache.d.ts.map +1 -0
  17. package/dist/cache.js +28 -0
  18. package/dist/cache.js.map +1 -0
  19. package/dist/changes.d.ts +29 -0
  20. package/dist/changes.d.ts.map +1 -0
  21. package/dist/changes.js +142 -0
  22. package/dist/changes.js.map +1 -0
  23. package/dist/compare.d.ts +65 -0
  24. package/dist/compare.d.ts.map +1 -0
  25. package/dist/compare.js +58 -0
  26. package/dist/compare.js.map +1 -0
  27. package/dist/config-schema.d.ts +118 -0
  28. package/dist/config-schema.d.ts.map +1 -0
  29. package/dist/config-schema.js +315 -0
  30. package/dist/config-schema.js.map +1 -0
  31. package/dist/config.d.ts +47 -0
  32. package/dist/config.d.ts.map +1 -0
  33. package/dist/config.js +112 -0
  34. package/dist/config.js.map +1 -0
  35. package/dist/detect.d.ts +70 -0
  36. package/dist/detect.d.ts.map +1 -0
  37. package/dist/detect.js +228 -0
  38. package/dist/detect.js.map +1 -0
  39. package/dist/evaluate.d.ts +98 -0
  40. package/dist/evaluate.d.ts.map +1 -0
  41. package/dist/evaluate.js +110 -0
  42. package/dist/evaluate.js.map +1 -0
  43. package/dist/extract.d.ts +81 -0
  44. package/dist/extract.d.ts.map +1 -0
  45. package/dist/extract.js +280 -0
  46. package/dist/extract.js.map +1 -0
  47. package/dist/gcp-auth.d.ts +58 -0
  48. package/dist/gcp-auth.d.ts.map +1 -0
  49. package/dist/gcp-auth.js +113 -0
  50. package/dist/gcp-auth.js.map +1 -0
  51. package/dist/glob.d.ts +49 -0
  52. package/dist/glob.d.ts.map +1 -0
  53. package/dist/glob.js +154 -0
  54. package/dist/glob.js.map +1 -0
  55. package/dist/host.d.ts +30 -0
  56. package/dist/host.d.ts.map +1 -0
  57. package/dist/host.js +69 -0
  58. package/dist/host.js.map +1 -0
  59. package/dist/i18n/en.d.ts +4 -0
  60. package/dist/i18n/en.d.ts.map +1 -0
  61. package/dist/i18n/en.js +168 -0
  62. package/dist/i18n/en.js.map +1 -0
  63. package/dist/i18n/es.d.ts +4 -0
  64. package/dist/i18n/es.d.ts.map +1 -0
  65. package/dist/i18n/es.js +168 -0
  66. package/dist/i18n/es.js.map +1 -0
  67. package/dist/i18n/index.d.ts +36 -0
  68. package/dist/i18n/index.d.ts.map +1 -0
  69. package/dist/i18n/index.js +50 -0
  70. package/dist/i18n/index.js.map +1 -0
  71. package/dist/i18n/types.d.ts +180 -0
  72. package/dist/i18n/types.d.ts.map +1 -0
  73. package/dist/i18n/types.js +11 -0
  74. package/dist/i18n/types.js.map +1 -0
  75. package/dist/index.d.ts +66 -0
  76. package/dist/index.d.ts.map +1 -0
  77. package/dist/index.js +69 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/llm.d.ts +226 -0
  80. package/dist/llm.d.ts.map +1 -0
  81. package/dist/llm.js +485 -0
  82. package/dist/llm.js.map +1 -0
  83. package/dist/nearest.d.ts +20 -0
  84. package/dist/nearest.d.ts.map +1 -0
  85. package/dist/nearest.js +54 -0
  86. package/dist/nearest.js.map +1 -0
  87. package/dist/net.d.ts +90 -0
  88. package/dist/net.d.ts.map +1 -0
  89. package/dist/net.js +203 -0
  90. package/dist/net.js.map +1 -0
  91. package/dist/node.d.ts +32 -0
  92. package/dist/node.d.ts.map +1 -0
  93. package/dist/node.js +33 -0
  94. package/dist/node.js.map +1 -0
  95. package/dist/openrouter.d.ts +25 -0
  96. package/dist/openrouter.d.ts.map +1 -0
  97. package/dist/openrouter.js +72 -0
  98. package/dist/openrouter.js.map +1 -0
  99. package/dist/optimize.d.ts +38 -0
  100. package/dist/optimize.d.ts.map +1 -0
  101. package/dist/optimize.js +183 -0
  102. package/dist/optimize.js.map +1 -0
  103. package/dist/otlp.d.ts +91 -0
  104. package/dist/otlp.d.ts.map +1 -0
  105. package/dist/otlp.js +102 -0
  106. package/dist/otlp.js.map +1 -0
  107. package/dist/phrases.d.ts +169 -0
  108. package/dist/phrases.d.ts.map +1 -0
  109. package/dist/phrases.js +939 -0
  110. package/dist/phrases.js.map +1 -0
  111. package/dist/pricing-overlay.d.ts +55 -0
  112. package/dist/pricing-overlay.d.ts.map +1 -0
  113. package/dist/pricing-overlay.js +241 -0
  114. package/dist/pricing-overlay.js.map +1 -0
  115. package/dist/pricing.d.ts +115 -0
  116. package/dist/pricing.d.ts.map +1 -0
  117. package/dist/pricing.js +400 -0
  118. package/dist/pricing.js.map +1 -0
  119. package/dist/profile.d.ts +71 -0
  120. package/dist/profile.d.ts.map +1 -0
  121. package/dist/profile.js +55 -0
  122. package/dist/profile.js.map +1 -0
  123. package/dist/promptfoo.d.ts +58 -0
  124. package/dist/promptfoo.d.ts.map +1 -0
  125. package/dist/promptfoo.js +149 -0
  126. package/dist/promptfoo.js.map +1 -0
  127. package/dist/prune.d.ts +91 -0
  128. package/dist/prune.d.ts.map +1 -0
  129. package/dist/prune.js +110 -0
  130. package/dist/prune.js.map +1 -0
  131. package/dist/reorder.d.ts +82 -0
  132. package/dist/reorder.d.ts.map +1 -0
  133. package/dist/reorder.js +215 -0
  134. package/dist/reorder.js.map +1 -0
  135. package/dist/review.d.ts +54 -0
  136. package/dist/review.d.ts.map +1 -0
  137. package/dist/review.js +131 -0
  138. package/dist/review.js.map +1 -0
  139. package/dist/rules.d.ts +5 -0
  140. package/dist/rules.d.ts.map +1 -0
  141. package/dist/rules.js +279 -0
  142. package/dist/rules.js.map +1 -0
  143. package/dist/savings.d.ts +36 -0
  144. package/dist/savings.d.ts.map +1 -0
  145. package/dist/savings.js +83 -0
  146. package/dist/savings.js.map +1 -0
  147. package/dist/segment.d.ts +8 -0
  148. package/dist/segment.d.ts.map +1 -0
  149. package/dist/segment.js +74 -0
  150. package/dist/segment.js.map +1 -0
  151. package/dist/shared-prefix.d.ts +63 -0
  152. package/dist/shared-prefix.d.ts.map +1 -0
  153. package/dist/shared-prefix.js +151 -0
  154. package/dist/shared-prefix.js.map +1 -0
  155. package/dist/similarity.d.ts +13 -0
  156. package/dist/similarity.d.ts.map +1 -0
  157. package/dist/similarity.js +30 -0
  158. package/dist/similarity.js.map +1 -0
  159. package/dist/structure.d.ts +144 -0
  160. package/dist/structure.d.ts.map +1 -0
  161. package/dist/structure.js +455 -0
  162. package/dist/structure.js.map +1 -0
  163. package/dist/suggest.d.ts +100 -0
  164. package/dist/suggest.d.ts.map +1 -0
  165. package/dist/suggest.js +151 -0
  166. package/dist/suggest.js.map +1 -0
  167. package/dist/tokenizer.d.ts +57 -0
  168. package/dist/tokenizer.d.ts.map +1 -0
  169. package/dist/tokenizer.js +157 -0
  170. package/dist/tokenizer.js.map +1 -0
  171. package/dist/types.d.ts +296 -0
  172. package/dist/types.d.ts.map +1 -0
  173. package/dist/types.js +3 -0
  174. package/dist/types.js.map +1 -0
  175. package/dist/walk.d.ts +40 -0
  176. package/dist/walk.d.ts.map +1 -0
  177. package/dist/walk.js +95 -0
  178. package/dist/walk.js.map +1 -0
  179. package/package.json +56 -0
  180. package/src/advisories.ts +431 -0
  181. package/src/aws-sigv4.ts +174 -0
  182. package/src/baseline.ts +390 -0
  183. package/src/cache.ts +54 -0
  184. package/src/changes.ts +158 -0
  185. package/src/compare.ts +131 -0
  186. package/src/config-schema.ts +451 -0
  187. package/src/config.ts +161 -0
  188. package/src/detect.ts +312 -0
  189. package/src/evaluate.ts +188 -0
  190. package/src/extract.ts +336 -0
  191. package/src/gcp-auth.ts +166 -0
  192. package/src/glob.ts +160 -0
  193. package/src/host.ts +90 -0
  194. package/src/i18n/en.ts +236 -0
  195. package/src/i18n/es.ts +236 -0
  196. package/src/i18n/index.ts +68 -0
  197. package/src/i18n/types.ts +230 -0
  198. package/src/index.ts +228 -0
  199. package/src/llm.ts +708 -0
  200. package/src/nearest.ts +61 -0
  201. package/src/net.ts +233 -0
  202. package/src/node.ts +63 -0
  203. package/src/openrouter.ts +125 -0
  204. package/src/optimize.ts +228 -0
  205. package/src/otlp.ts +179 -0
  206. package/src/phrases.ts +1047 -0
  207. package/src/pricing-overlay.ts +319 -0
  208. package/src/pricing.ts +468 -0
  209. package/src/profile.ts +124 -0
  210. package/src/promptfoo.ts +213 -0
  211. package/src/prune.ts +211 -0
  212. package/src/reorder.ts +307 -0
  213. package/src/review.ts +180 -0
  214. package/src/rules.ts +324 -0
  215. package/src/savings.ts +121 -0
  216. package/src/segment.ts +106 -0
  217. package/src/shared-prefix.ts +198 -0
  218. package/src/similarity.ts +28 -0
  219. package/src/structure.ts +652 -0
  220. package/src/suggest.ts +254 -0
  221. package/src/tokenizer.ts +190 -0
  222. package/src/types.ts +323 -0
  223. package/src/walk.ts +117 -0
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ export * from './types.js';
2
+ export { estimateTokens, countTokensAnthropic } from './tokenizer.js';
3
+ export { countSentences, profilePrompt } from './profile.js';
4
+ export { PHRASE_LANGUAGES } from './phrases.js';
5
+ export { toPromptfoo } from './promptfoo.js';
6
+ export { toOtlpMetrics } from './otlp.js';
7
+ export { SUGGEST_SYSTEM_PROMPT, applyRewrites, rejectionText, suggestRewrites } from './suggest.js';
8
+ export { MODELS, DEFAULT_MODEL, COST_MULTIPLIERS, PRICING_LAST_REVIEWED, reviewAgeDays, BUNDLED_CATALOGUE, getModel, listModels, effectivePricing, cheapestOfTier, modelFrom, cheapestOfTierIn, multipliersFor, } from './pricing.js';
9
+ // Local price corrections, so a price change needs no library upgrade.
10
+ export { MAX_PRICING_BYTES, PRICING_MODEL_KEYS, PRICING_OVERLAY_KEYS, PricingOverlayError, applyPricingOverlay, catalogueFromOverlay, parsePricingOverlay, } from './pricing-overlay.js';
11
+ export { openrouterOverlay } from './openrouter.js';
12
+ // SigV4 and the Google service-account flow, both hand-rolled on WebCrypto to
13
+ // keep the zero-dependency invariant. Exported so they can be tested directly:
14
+ // a signer that is only reachable through a network call is a signer nothing
15
+ // checks.
16
+ export { amzDates, canonicalRequest, signRequest, signingKey } from './aws-sigv4.js';
17
+ export { accessToken, pkcs8FromPem, signedJwt } from './gcp-auth.js';
18
+ export { BASELINE_FILENAME, BASELINE_VERSION, BaselineError, MAX_BASELINE_BYTES, breaches, compareToBaseline, formatBaseline, moneyIsComparable, parseBaseline, } from './baseline.js';
19
+ export { RULES, getRule } from './rules.js';
20
+ export { segment, join, protectedTexts } from './segment.js';
21
+ export { computeSavings, costOfCall, formatUsd, formatSignedUsd } from './savings.js';
22
+ export { buildAdvisories, recommendTier } from './advisories.js';
23
+ export { analyzeCachePrefix } from './cache.js';
24
+ // Rearranging a prompt so its stable instructions sit in the cacheable prefix.
25
+ // The largest saving Trazum knows about, and the most dangerous transformation
26
+ // here — see reorder.ts for what it refuses to move and why.
27
+ export { reorderForCache } from './reorder.js';
28
+ // The caching loss that only exists between prompts: a preamble shared by forty
29
+ // files and byte-identical in none of them. Reports no dollar figure on purpose
30
+ // — see shared-prefix.ts for why the cost model cannot price it.
31
+ export { cacheableMinimum, sharedPrefixes } from './shared-prefix.js';
32
+ // Prompts embedded in source files, found by an explicit marker rather than
33
+ // guessed at — see extract.ts for why a gate cannot afford a heuristic here.
34
+ export { extractPrompts, promptId, hasMarker, SOURCE_EXTENSIONS } from './extract.js';
35
+ // Which provider a prompt is actually sent to, read from the code rather than
36
+ // assumed — see detect.ts for why it declines when a file points two ways.
37
+ export { detectFromSource } from './detect.js';
38
+ export { optimize, withExactTokenCounts, DEFAULT_USAGE } from './optimize.js';
39
+ export { refineWithLlm, openAiCompatible, anthropicProvider, bedrockProvider, geminiProvider, vertexProvider, customProvider, providerFromEnv, REFINER_SYSTEM_PROMPT, } from './llm.js';
40
+ // Internationalisation
41
+ export { DEFAULT_LOCALE, LOCALES, getMessages, isLocale, matchLocale, resolveLocale, en, es, } from './i18n/index.js';
42
+ // Structural analysis
43
+ export { analyzeExamples, findContradictions, findExamples, findMovableSchema, findRestatedFormat, } from './structure.js';
44
+ export { jaccard, normalizeForCompare } from './similarity.js';
45
+ export { OUTPUT_CUES, OUTPUT_CUES_BY_LANGUAGE } from './phrases.js';
46
+ // Endpoint validation for the pluggable LLM layer
47
+ export { SAFE_FETCH_INIT, allowedEndpoints, checkedEndpoint, isPrivateHost, resolveEndpoint, validateLlmEndpoint, } from './net.js';
48
+ // Per-rule change extraction
49
+ export { extractChanges, DEFAULT_CHANGE_LIMIT } from './changes.js';
50
+ // Semantic review of few-shot examples (optional, costs an LLM call)
51
+ export { reviewExamples, EXAMPLE_REVIEW_SYSTEM_PROMPT } from './review.js';
52
+ // Which few-shot examples earn their tokens, measured by leave-one-out. Optional,
53
+ // and the most expensive thing here: see prune.ts for the call arithmetic and for
54
+ // what the measurement cannot tell you.
55
+ export { NothingToPrune, plannedCalls, pruneExamples, withoutExample } from './prune.js';
56
+ // Golden-set evaluation (optional, costs three LLM calls per case)
57
+ export { evaluate, fillPrompt, verdictFor } from './evaluate.js';
58
+ // Comparing two prompt versions
59
+ export { comparePrompts } from './compare.js';
60
+ // Glob matching for config budget patterns. Pure, so it belongs here; the
61
+ // config loader and directory walk that use it read the filesystem and live in
62
+ // "@trazum/core/node" instead — see node.ts for why that split is structural.
63
+ export { matchGlob, mostSpecificMatch, specificity } from './glob.js';
64
+ export { editDistance, nearestName } from './nearest.js';
65
+ // The config *schema*, without the loader: validation is a pure function of a
66
+ // string, so it belongs on the browser-safe entry point. `loadConfig`, which
67
+ // actually opens the file, is only in "@trazum/core/node".
68
+ export { CONFIG_FILENAME, CONFIG_KEYS, CONFIG_USAGE_KEYS, ConfigError, DEFAULT_EXTENSIONS, MAX_CONFIG_BYTES, MAX_CONFIG_SEARCH_DEPTH, budgetFor, parseConfig, } from './config-schema.js';
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAI1C,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAQpG,OAAO,EACL,MAAM,EACN,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EACrB,aAAa,EACb,iBAAiB,EACjB,QAAQ,EACR,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,cAAc,GACf,MAAM,cAAc,CAAC;AAItB,uEAAuE;AACvE,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,UAAU;AACV,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAErF,OAAO,EAAE,WAAW,EA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,WAAW,GACZ,MAAM,oBAAoB,CAAC"}
package/dist/llm.d.ts ADDED
@@ -0,0 +1,226 @@
1
+ import type { ServiceAccount } from './gcp-auth.js';
2
+ import type { Locale } from './i18n/types.js';
3
+ import type { LlmProvider, OptimizationResult, TokenCounter } from './types.js';
4
+ /**
5
+ * Optional LLM layer.
6
+ *
7
+ * The deterministic core already does the work at zero cost. This pass adds
8
+ * the semantic compression rules cannot do — rewriting a whole sentence,
9
+ * merging two instructions that say the same thing in different words — and
10
+ * that is why it costs one call.
11
+ *
12
+ * The provider is pluggable on purpose: your own hosted model, an
13
+ * OpenAI-compatible endpoint, the Claude API or anything else behind
14
+ * `customProvider`.
15
+ */
16
+ export declare const REFINER_SYSTEM_PROMPT = "You rewrite prompts so they cost fewer tokens without changing what they ask for.\n\nRules:\n- Preserve exactly the same task, constraints, output format and success criteria.\n- Copy verbatim, without changing a single character: code blocks, URLs, template placeholders ({{x}}, ${x}, {x}) and XML/HTML tags.\n- Do not summarise and do not drop requirements. When in doubt about whether something is a requirement, keep it.\n- Remove redundancy, padding and repetition. Merge duplicated instructions.\n- Keep the original language of the prompt.\n- Return ONLY the rewritten prompt. No explanations, no commentary, no code fences wrapping the answer.";
17
+ export interface RefineOptions {
18
+ /**
19
+ * Minimum fraction of tokens the result must keep (0-1). Below this
20
+ * threshold the model is assumed to have summarised rather than compressed,
21
+ * and the candidate is rejected. Defaults to 0.25.
22
+ */
23
+ minRetainRatio?: number;
24
+ tokenCounter?: TokenCounter;
25
+ /** Language of the rejection reason. Defaults to the result's locale. */
26
+ locale?: Locale;
27
+ }
28
+ /**
29
+ * Runs the already-optimised prompt through the LLM and accepts the result
30
+ * only if it passes the safety checks.
31
+ *
32
+ * It never returns a prompt worse than the deterministic one: if the candidate
33
+ * loses protected content, grows in tokens or shrinks suspiciously, it is
34
+ * discarded and the previous result stands.
35
+ */
36
+ export declare function refineWithLlm(result: OptimizationResult, provider: LlmProvider, options?: RefineOptions): Promise<OptimizationResult>;
37
+ export interface OpenAiCompatibleOptions {
38
+ /** Base URL, without `/chat/completions`. E.g. `https://llm.example.com/v1` */
39
+ baseUrl: string;
40
+ apiKey?: string;
41
+ model: string;
42
+ /** Extra headers, in case your gateway requires its own. */
43
+ headers?: Record<string, string>;
44
+ /** Name shown in the report. */
45
+ name?: string;
46
+ maxTokens?: number;
47
+ fetchImpl?: typeof fetch;
48
+ /**
49
+ * Allow http and private hosts — localhost, the RFC1918 ranges, the cloud
50
+ * metadata address.
51
+ *
52
+ * **Only when the operator chose the URL.** That is the whole distinction. An
53
+ * endpoint from `TRAZUM_LLM_BASE_URL` is somebody configuring their own
54
+ * machine, and pointing it at `http://localhost:11434` for Ollama is the
55
+ * documented normal case. An endpoint arriving in an HTTP request body is a
56
+ * stranger naming a host for this server to fetch, which is server-side
57
+ * request forgery whatever else it is called.
58
+ */
59
+ allowInsecure?: boolean;
60
+ }
61
+ /**
62
+ * The endpoint check lives in `net.ts`, beside the validator and beside the
63
+ * `fetch` options every server-side call here carries.
64
+ *
65
+ * At construction rather than at call time, so a provider that can never work
66
+ * does not exist to be handed around.
67
+ *
68
+ * The web route already validates a body-supplied URL before it gets here, and
69
+ * that stays — it turns the reason code into a sentence in the reader's
70
+ * language. This is the second lock, at the boundary. `openAiCompatible` is an
71
+ * exported library function, so "the caller checks" is a promise about every
72
+ * future caller, including ones outside this repository.
73
+ */
74
+ /**
75
+ * Any endpoint speaking OpenAI's `/chat/completions` format. Covers vLLM,
76
+ * Ollama, OpenRouter, LM Studio, Together and most internal gateways.
77
+ */
78
+ export declare function openAiCompatible(options: OpenAiCompatibleOptions): LlmProvider;
79
+ export interface AnthropicProviderOptions {
80
+ apiKey: string;
81
+ model?: string;
82
+ baseUrl?: string;
83
+ maxTokens?: number;
84
+ fetchImpl?: typeof fetch;
85
+ /** See `OpenAiCompatibleOptions.allowInsecure`: only when you chose the URL. */
86
+ allowInsecure?: boolean;
87
+ }
88
+ /** The Claude API directly, via `/v1/messages`. */
89
+ export declare function anthropicProvider(options: AnthropicProviderOptions): LlmProvider;
90
+ export interface GeminiProviderOptions {
91
+ apiKey: string;
92
+ /** Default: `gemini-2.5-pro`. */
93
+ model?: string;
94
+ /** Default: Google's public endpoint. */
95
+ baseUrl?: string;
96
+ maxTokens?: number;
97
+ fetchImpl?: typeof fetch;
98
+ /** See `OpenAiCompatibleOptions.allowInsecure`: only when you chose the URL. */
99
+ allowInsecure?: boolean;
100
+ }
101
+ /**
102
+ * Gemini directly, via `generateContent`.
103
+ *
104
+ * The one provider on the list that needs its own function rather than the
105
+ * OpenAI-compatible path. Everything else — Groq, Together, Fireworks,
106
+ * DeepInfra, Cerebras, SiliconFlow, OpenRouter, LiteLLM — speaks the OpenAI
107
+ * shape, so `openAiCompatibleProvider` with a base URL is the whole
108
+ * integration. Google's is a different document: the system prompt is
109
+ * `systemInstruction` rather than a message, turns are `contents` with `parts`,
110
+ * and the answer is the first candidate's parts joined.
111
+ *
112
+ * Three failure modes that are not HTTP errors, and each has bitten somebody:
113
+ *
114
+ * - **A safety block returns 200.** `promptFeedback.blockReason` arrives with no
115
+ * candidates at all, so reading `candidates[0]` gives `undefined` and the
116
+ * caller sees "no text" for what is actually a refusal.
117
+ * - **`finishReason: MAX_TOKENS` also returns 200**, with a truncated answer.
118
+ * For a rewrite pass that is worse than an error: the text looks like a
119
+ * result and is half a result.
120
+ * - **Parts can be empty.** A candidate with no text part is a valid document
121
+ * and not a valid answer.
122
+ *
123
+ * The key goes in a header, not the query string. Google's own examples put it
124
+ * in `?key=`, which puts a credential in every proxy log and referrer between
125
+ * here and there.
126
+ */
127
+ export declare function geminiProvider(options: GeminiProviderOptions): LlmProvider;
128
+ export interface BedrockProviderOptions {
129
+ /** e.g. `anthropic.claude-sonnet-4-5-20250929-v1:0`. */
130
+ model: string;
131
+ region: string;
132
+ accessKeyId: string;
133
+ secretAccessKey: string;
134
+ /** For temporary credentials from STS or an instance role. */
135
+ sessionToken?: string;
136
+ maxTokens?: number;
137
+ /** Override the host. Defaults to the regional Bedrock runtime endpoint. */
138
+ baseUrl?: string;
139
+ fetchImpl?: typeof fetch;
140
+ allowInsecure?: boolean;
141
+ /** Injectable for tests; the signature is a function of the clock. */
142
+ now?: () => Date;
143
+ }
144
+ /**
145
+ * Amazon Bedrock, through **Converse** rather than `InvokeModel`.
146
+ *
147
+ * That choice is the whole reason this is one provider instead of six.
148
+ * `InvokeModel` takes a body in each model family's own shape — Anthropic's
149
+ * `messages` with `anthropic_version`, Meta's `prompt`, Amazon's
150
+ * `inputText` — so supporting "Bedrock" through it means supporting each vendor
151
+ * separately and getting a 400 for every model nobody thought about. `Converse`
152
+ * is Bedrock's unified surface: one request shape, one response shape, every
153
+ * model that supports it.
154
+ *
155
+ * Signed with SigV4 by hand — see `aws-sigv4.ts` for why there is no SDK here
156
+ * and what the tests do and do not prove.
157
+ *
158
+ * `stopReason: 'max_tokens'` throws, for the same reason it does on Gemini: a
159
+ * truncated rewrite reads exactly like a finished one, and that is the failure
160
+ * this package exists to refuse.
161
+ */
162
+ export declare function bedrockProvider(options: BedrockProviderOptions): LlmProvider;
163
+ export interface VertexProviderOptions {
164
+ /** The parsed contents of a service-account JSON key. */
165
+ serviceAccount: ServiceAccount;
166
+ project: string;
167
+ /** e.g. `us-central1`. `global` is also valid for some models. */
168
+ location: string;
169
+ /** Default: `gemini-2.5-pro`. */
170
+ model?: string;
171
+ /** Default: `google`. `anthropic` for Claude on Vertex. */
172
+ publisher?: string;
173
+ maxTokens?: number;
174
+ baseUrl?: string;
175
+ fetchImpl?: typeof fetch;
176
+ allowInsecure?: boolean;
177
+ now?: () => Date;
178
+ }
179
+ /**
180
+ * Gemini through Vertex AI, with a service account instead of an API key.
181
+ *
182
+ * Vertex will not take an API key, which is the whole difference from
183
+ * `geminiProvider`: the credential is a signed assertion traded for an access
184
+ * token that lasts an hour. `gcp-auth.ts` does that, caches the token, and
185
+ * explains why there is no SDK.
186
+ *
187
+ * The response shape is Gemini's, so the same three HTTP-200 failures apply and
188
+ * are refused the same way — a blocked prompt, a truncated answer, an empty
189
+ * candidate. The parsing is shared with `geminiProvider` rather than copied,
190
+ * because two copies of "is this answer complete" is one copy too many.
191
+ */
192
+ export declare function vertexProvider(options: VertexProviderOptions): LlmProvider;
193
+ export interface CustomProviderOptions {
194
+ name: string;
195
+ model: string;
196
+ /** Builds the HTTP request from the system and user prompts. */
197
+ request(input: {
198
+ system: string;
199
+ user: string;
200
+ }): {
201
+ url: string;
202
+ init: RequestInit;
203
+ };
204
+ /** Extracts the text from the already-parsed response body. */
205
+ extract(body: unknown): string;
206
+ fetchImpl?: typeof fetch;
207
+ }
208
+ /**
209
+ * Escape hatch: if your endpoint speaks none of the formats above, you define
210
+ * how the request is built and how the response is read, and everything else
211
+ * keeps working the same.
212
+ */
213
+ export declare function customProvider(options: CustomProviderOptions): LlmProvider;
214
+ /**
215
+ * Builds a provider from environment variables.
216
+ *
217
+ * TRAZUM_LLM_PROVIDER openai | anthropic (default: openai)
218
+ * TRAZUM_LLM_BASE_URL base URL of the endpoint
219
+ * TRAZUM_LLM_API_KEY key, when one is needed
220
+ * TRAZUM_LLM_MODEL model identifier
221
+ *
222
+ * Returns `null` when the configuration is incomplete, so the tool keeps
223
+ * working in deterministic mode instead of failing.
224
+ */
225
+ export declare function providerFromEnv(env?: Record<string, string | undefined>): LlmProvider | null;
226
+ //# sourceMappingURL=llm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAe,cAAc,EAAE,MAAM,eAAe,CAAC;AAEjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAI9C,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEhF;;;;;;;;;;;GAWG;AAEH,eAAO,MAAM,qBAAqB,gpBAQsE,CAAC;AAEzG,MAAM,WAAW,aAAa;IAC5B;;;;OAIG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