@polka-codes/core 0.7.1 → 0.7.2

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 (2) hide show
  1. package/dist/index.js +16 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -13420,6 +13420,7 @@ class OpenRouterService extends AiServiceBase {
13420
13420
  #client;
13421
13421
  #apiKey;
13422
13422
  #options;
13423
+ #modelProviderInfo;
13423
13424
  model;
13424
13425
  constructor(options) {
13425
13426
  super(options.usageMeter);
@@ -13444,6 +13445,9 @@ class OpenRouterService extends AiServiceBase {
13444
13445
  id: options.model,
13445
13446
  info: {}
13446
13447
  };
13448
+ fetch(`https://openrouter.ai/api/v1/models/${this.model.id}/endpoints`).then((res) => res.json()).then((data) => {
13449
+ this.#modelProviderInfo = data.data;
13450
+ });
13447
13451
  }
13448
13452
  async* sendImpl(systemPrompt, messages) {
13449
13453
  const openAiMessages = [
@@ -13564,12 +13568,20 @@ class OpenRouterService extends AiServiceBase {
13564
13568
  signal: controller.signal
13565
13569
  });
13566
13570
  const responseBody = await response.json();
13567
- const generation = responseBody.data;
13571
+ const generation = responseBody.data ?? {};
13572
+ let totalCost = generation.total_cost || 0;
13573
+ if (generation.is_byok && this.#modelProviderInfo) {
13574
+ const price = this.#modelProviderInfo.endpoints.find((e) => e.provider_name === generation.provider_name)?.pricing;
13575
+ if (price) {
13576
+ totalCost += (generation.native_tokens_prompt || 0) * price.request;
13577
+ totalCost += (generation.native_tokens_completion || 0) * price.completion;
13578
+ }
13579
+ }
13568
13580
  yield {
13569
13581
  type: "usage",
13570
- inputTokens: generation?.native_tokens_prompt || 0,
13571
- outputTokens: generation?.native_tokens_completion || 0,
13572
- totalCost: generation?.total_cost || 0
13582
+ inputTokens: generation.native_tokens_prompt || 0,
13583
+ outputTokens: generation.native_tokens_completion || 0,
13584
+ totalCost
13573
13585
  };
13574
13586
  } catch (error) {
13575
13587
  console.error("Error fetching OpenRouter generation details:", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",