@jambonz/llm 0.6.5 → 0.6.7

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.
@@ -135,12 +135,20 @@ async function* streamFromOpenAI(client, req, options) {
135
135
  throw new Error(`OpenAI-compatible stream returned an error: ${message}`);
136
136
  }
137
137
  if (chunk.usage) {
138
- const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
138
+ const details = chunk.usage.prompt_tokens_details;
139
+ const cachedTokens = details?.cached_tokens;
140
+ const cacheWriteTokens = details?.cache_write_tokens;
139
141
  usage = {
140
- ...chunk.usage.prompt_tokens !== void 0 ? { inputTokens: chunk.usage.prompt_tokens - (cachedTokens ?? 0) } : {},
142
+ ...chunk.usage.prompt_tokens !== void 0 ? {
143
+ inputTokens: Math.max(
144
+ 0,
145
+ chunk.usage.prompt_tokens - (cachedTokens ?? 0) - (cacheWriteTokens ?? 0)
146
+ )
147
+ } : {},
141
148
  ...chunk.usage.completion_tokens !== void 0 ? { outputTokens: chunk.usage.completion_tokens } : {},
142
149
  ...chunk.usage.total_tokens !== void 0 ? { totalTokens: chunk.usage.total_tokens } : {},
143
- ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {}
150
+ ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {},
151
+ ...cacheWriteTokens !== void 0 ? { cacheWriteTokens } : {}
144
152
  };
145
153
  }
146
154
  const choice = chunk.choices?.[0];
@@ -3537,6 +3545,179 @@ var huggingfaceFactory = {
3537
3545
  create: () => new HuggingfaceAdapter()
3538
3546
  };
3539
3547
 
3548
+ // src/adapters/openrouter/manifest.ts
3549
+ var OPENROUTER_KNOWN_MODELS = [
3550
+ {
3551
+ id: "anthropic/claude-haiku-5.5",
3552
+ displayName: "Claude Haiku 5.5 (OpenRouter)",
3553
+ capabilities: {
3554
+ streaming: true,
3555
+ tools: true,
3556
+ vision: true,
3557
+ systemPrompt: true,
3558
+ maxContextTokens: 1e6
3559
+ }
3560
+ },
3561
+ {
3562
+ id: "anthropic/claude-sonnet-5.5",
3563
+ displayName: "Claude Sonnet 5.5 (OpenRouter)",
3564
+ capabilities: {
3565
+ streaming: true,
3566
+ tools: true,
3567
+ vision: true,
3568
+ systemPrompt: true,
3569
+ maxContextTokens: 1e6
3570
+ }
3571
+ },
3572
+ {
3573
+ id: "openai/gpt-5.4-mini",
3574
+ displayName: "GPT-5.4 Mini (OpenRouter)",
3575
+ capabilities: {
3576
+ streaming: true,
3577
+ tools: true,
3578
+ vision: true,
3579
+ systemPrompt: true,
3580
+ maxContextTokens: 4e5
3581
+ }
3582
+ },
3583
+ {
3584
+ id: "openai/gpt-4.1-mini",
3585
+ displayName: "GPT-4.1 Mini (OpenRouter)",
3586
+ capabilities: {
3587
+ streaming: true,
3588
+ tools: true,
3589
+ vision: true,
3590
+ systemPrompt: true,
3591
+ maxContextTokens: 1047576
3592
+ }
3593
+ },
3594
+ {
3595
+ id: "google/gemini-3.5-flash",
3596
+ displayName: "Gemini 3.5 Flash (OpenRouter)",
3597
+ capabilities: {
3598
+ streaming: true,
3599
+ tools: true,
3600
+ vision: true,
3601
+ systemPrompt: true,
3602
+ maxContextTokens: 1048576
3603
+ }
3604
+ },
3605
+ {
3606
+ id: "meta-llama/llama-3.3-70b-instruct",
3607
+ displayName: "Llama 3.3 70B Instruct (OpenRouter)",
3608
+ capabilities: {
3609
+ streaming: true,
3610
+ tools: true,
3611
+ vision: false,
3612
+ systemPrompt: true,
3613
+ maxContextTokens: 131072
3614
+ }
3615
+ }
3616
+ ];
3617
+ var openRouterManifest = {
3618
+ vendor: "openrouter",
3619
+ displayName: "OpenRouter",
3620
+ authKinds: [
3621
+ {
3622
+ kind: "apiKey",
3623
+ displayName: "API Key",
3624
+ fields: [
3625
+ {
3626
+ name: "apiKey",
3627
+ label: "API Key",
3628
+ type: "password",
3629
+ required: true,
3630
+ help: "Get an API key from https://openrouter.ai/settings/keys"
3631
+ },
3632
+ {
3633
+ name: "baseURL",
3634
+ label: "Base URL",
3635
+ type: "url",
3636
+ required: false,
3637
+ default: "https://openrouter.ai/api/v1",
3638
+ help: "Override only for proxies. Default points at OpenRouter's production endpoint."
3639
+ }
3640
+ ]
3641
+ }
3642
+ ],
3643
+ knownModels: OPENROUTER_KNOWN_MODELS,
3644
+ // /models is public; the adapter enriches it with OpenRouter's own capability metadata.
3645
+ supportsModelListing: true,
3646
+ docsUrl: "https://openrouter.ai/docs/api_reference/overview"
3647
+ };
3648
+
3649
+ // src/adapters/openrouter/adapter.ts
3650
+ var OPENROUTER_METADATA_EXTRACTOR = makeMetadataExtractor([
3651
+ { header: "x-generation-id", key: "request_id" }
3652
+ ]);
3653
+ var OPENROUTER_ATTRIBUTION_HEADERS = {
3654
+ "HTTP-Referer": "https://jambonz.org",
3655
+ "X-OpenRouter-Title": "jambonz"
3656
+ };
3657
+ var OpenRouterAdapter = class extends OpenAIAdapter {
3658
+ vendor = openRouterManifest.vendor;
3659
+ init(auth, client) {
3660
+ super.init(
3661
+ {
3662
+ ...auth,
3663
+ baseURL: auth.baseURL ?? "https://openrouter.ai/api/v1"
3664
+ },
3665
+ client
3666
+ );
3667
+ this.client = this.ensureClient().withOptions({
3668
+ defaultHeaders: OPENROUTER_ATTRIBUTION_HEADERS
3669
+ });
3670
+ }
3671
+ stream(req) {
3672
+ return streamFromOpenAI(this.ensureClient(), req, {
3673
+ knownModels: this.knownModels(),
3674
+ vendorMetadataExtractor: OPENROUTER_METADATA_EXTRACTOR,
3675
+ // Verified accepted by OpenRouter (forwarded to OpenAI-family upstreams).
3676
+ includeCacheKey: true,
3677
+ openRouterAutoCache: this.isOpenRouter
3678
+ });
3679
+ }
3680
+ async listAvailableModels() {
3681
+ const result = await this.ensureClient().models.list();
3682
+ const out = [];
3683
+ for (const m of result.data) {
3684
+ if (m.id.endsWith(":batch")) continue;
3685
+ const outputs = m.architecture?.output_modalities;
3686
+ if (outputs && !outputs.includes("text")) continue;
3687
+ const known = this.knownModels().find((k) => k.id === m.id);
3688
+ if (known) {
3689
+ out.push(known);
3690
+ continue;
3691
+ }
3692
+ out.push({
3693
+ id: m.id,
3694
+ ...m.name ? { displayName: m.name } : {},
3695
+ capabilities: {
3696
+ streaming: true,
3697
+ tools: m.supported_parameters?.includes("tools") ?? true,
3698
+ vision: m.architecture?.input_modalities?.includes("image") ?? false,
3699
+ systemPrompt: true,
3700
+ ...m.context_length ? { maxContextTokens: m.context_length } : {}
3701
+ }
3702
+ });
3703
+ }
3704
+ return out;
3705
+ }
3706
+ async testCredential() {
3707
+ await this.ensureClient().get("/key");
3708
+ }
3709
+ knownModels() {
3710
+ return openRouterManifest.knownModels;
3711
+ }
3712
+ };
3713
+
3714
+ // src/adapters/openrouter/factory.ts
3715
+ var openRouterFactory = {
3716
+ vendor: openRouterManifest.vendor,
3717
+ manifest: openRouterManifest,
3718
+ create: () => new OpenRouterAdapter()
3719
+ };
3720
+
3540
3721
  // src/adapters/index.ts
3541
3722
  registerAdapter(openAIFactory);
3542
3723
  registerAdapter(deepseekFactory);
@@ -3553,6 +3734,7 @@ registerAdapter(bedrockFactory);
3553
3734
  registerAdapter(azureOpenAIFactory);
3554
3735
  registerAdapter(groqFactory);
3555
3736
  registerAdapter(huggingfaceFactory);
3737
+ registerAdapter(openRouterFactory);
3556
3738
 
3557
3739
  // src/index.ts
3558
3740
  async function createLlm(args) {