Skip to content

feat(openrouter): support multiple named keys in usage command#16

Open
IgorWarzocha wants to merge 2 commits intomasterfrom
multi-openrouter-keys
Open

feat(openrouter): support multiple named keys in usage command#16
IgorWarzocha wants to merge 2 commits intomasterfrom
multi-openrouter-keys

Conversation

@IgorWarzocha
Copy link
Owner

Summary

  • add support for multiple named OpenRouter API keys in usage-config.jsonc via openrouterKeys
  • update /usage command parsing to support /usage openrouter <key_name> and filter to a single named key
  • include OpenRouter key labels in rendered status output while preserving existing auth-file fallback behavior

Validation

  • tsc --noEmit
  • npm run build

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @IgorWarzocha, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the management and reporting of OpenRouter API usage by introducing support for multiple named API keys. Users can now define distinct OpenRouter keys in their configuration, query usage for specific keys via the /usage command, and see clear labels for each key in the usage output. This enhancement provides greater flexibility and organization for users managing various OpenRouter API credentials.

Highlights

  • Multiple Named OpenRouter API Keys: Introduced support for configuring multiple named OpenRouter API keys within usage-config.jsonc, allowing users to manage different keys for various purposes.
  • Enhanced Usage Command Filtering: The /usage command now supports filtering OpenRouter usage by a specific named key (e.g., /usage openrouter <key_name>), providing more granular control over usage reporting.
  • Improved Usage Output Clarity: The display of OpenRouter usage has been updated to include the associated key label, making it clearer which key's usage is being reported when multiple are configured.
  • Configuration and Type System Updates: Extended data structures (OpenRouterAuth, OpenRouterQuota, UsageConfig) and internal logic to correctly resolve and fetch usage data for individual OpenRouter keys based on the new configuration.
Changelog
  • src/hooks/command.ts
    • Updated the description for the /usage command to explicitly include 'openrouter' as a supported provider.
    • Modified command argument parsing to extract a potential OpenRouter key name from the /usage command arguments.
    • Passed the extracted OpenRouter key name to the fetchUsageSnapshots function for targeted usage retrieval.
  • src/providers/openrouter/index.ts
    • Populated the keyName field within the openrouterQuota object using the auth.keyName or the label from the API response, ensuring key identification in usage data.
  • src/providers/openrouter/types.ts
    • Added an optional keyName property to the OpenRouterAuth interface to support named OpenRouter API keys.
  • src/types.ts
    • Introduced an optional openrouterKeys array to the UsageConfig interface, enabling the configuration of multiple named OpenRouter keys.
    • Added an optional keyName property to the OpenRouterQuota interface to store the name of the associated OpenRouter key.
  • src/ui/formatters/openrouter.ts
    • Modified the formatOpenRouterSnapshot function to display the keyName alongside 'OPENROUTER' in the formatted output, enhancing readability for multiple keys.
  • src/usage/config.ts
    • Added commented-out examples for the new openrouterKeys configuration in the default usage-config.jsonc template, guiding users on how to set up named keys.
  • src/usage/fetch.ts
    • Updated fetchUsageSnapshots to accept an optional openrouterKeyName parameter for filtering.
    • Implemented filtering logic to fetch usage only for the specified OpenRouter key if openrouterKeyName is provided.
    • Changed the key used for snapshotsMap from providerID to entryID to correctly handle multiple entries for the same provider (e.g., multiple OpenRouter keys).
  • src/usage/registry.ts
    • Imported the UsageConfig type for use in resolving provider authentications.
    • Modified the ProviderAuthEntry type to include an entryID for unique identification of each provider entry, especially for multiple keys of the same provider.
    • Introduced resolveProviderAuthsWithConfig to integrate configuration-based OpenRouter keys into the authentication resolution process.
    • Added resolveOpenRouterAuths function to parse and create ProviderAuthEntry objects for OpenRouter keys defined in the UsageConfig or traditional auths object, handling duplicates and assigning default names.
Activity
  • The pull request was created by IgorWarzocha, introducing new functionality for OpenRouter API key management and usage reporting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for multiple named OpenRouter API keys, which is a great enhancement. The implementation is solid, with updates to configuration, command parsing, and usage fetching logic. I've found one potential issue with handling duplicate key names in the configuration, which could lead to silently ignoring some keys. My review includes a suggestion to fix this to make the feature more robust.

Comment on lines 99 to 115
const entries: ProviderAuthEntry[] = []
const seenKeys = new Set<string>()

const configuredKeys = Array.isArray(config?.openrouterKeys) ? config.openrouterKeys : []
for (const configured of configuredKeys) {
const key = configured?.key?.trim()
if (!key || seenKeys.has(key)) continue
const rawName = configured?.name?.trim()
const name = rawName || `key-${entries.length + 1}`
if (configured.enabled === false) continue
seenKeys.add(key)
entries.push({
providerID: "openrouter",
entryID: `openrouter:${name}`,
auth: { key, keyName: name },
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation does not handle cases where multiple OpenRouter keys are configured with the same name in usage-config.jsonc. This can lead to one of the keys being silently overwritten in the usage report because they will have the same entryID. To prevent this, you should track seen key names (case-insensitively) and skip any duplicates.

  const entries: ProviderAuthEntry[] = []
  const seenKeys = new Set<string>()
  const seenNames = new Set<string>()

  const configuredKeys = Array.isArray(config?.openrouterKeys) ? config.openrouterKeys : []
  for (const configured of configuredKeys) {
    const key = configured?.key?.trim()
    if (!key || seenKeys.has(key)) continue

    const rawName = configured?.name?.trim()
    const name = rawName || `key-${entries.length + 1}`

    if (configured.enabled === false) continue
    if (seenNames.has(name.toLowerCase())) continue

    seenKeys.add(key)
    seenNames.add(name.toLowerCase())
    entries.push({
      providerID: "openrouter",
      entryID: `openrouter:${name}`,
      auth: { key, keyName: name },
    })
  }

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7049a98cd9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 38 to 43
.map(async e => {
const snap = await providers[e.providerID]?.fetchUsage?.(e.auth).catch(() => null)
if (snap) {
snapshotsMap.set(e.providerID, snap)
snapshotsMap.set(e.entryID, snap)
fetched.add(e.providerID)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track OpenRouter fetches per key to avoid silent drops

When multiple OpenRouter keys are configured, the fetch loop records success in fetched by provider ID only. If one key succeeds and another key fails (bad key, rate limit, etc.), appendMissingStates will think OpenRouter is fully fetched and won’t emit any missing snapshot for the failed key, so that key silently disappears from /usage output. This regression shows up only when multiple keys are configured and at least one fails; consider tracking fetched by entryID (or emitting per-key missing snapshots) so users see failures for individual keys.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments