Skip to content

Bug: Zhipu AI token count showing NaNM / NaNM #24

@aditya11201

Description

@aditya11201

Bug Description

When querying Zhipu AI quota with /mystatus, the token count displays as `NaNM / NaNM` instead of showing actual token usage values.

Steps to Reproduce

  1. Configure Zhipu AI credentials in `~/.local/share/opencode/auth.json`:
    ```json
    "zhipuai-coding-plan": {
    "type": "api",
    "key": "YOUR_API_KEY"
    }
    ```
  2. Call `/mystatus`
  3. Observe Zhipu AI section shows:
    ```
    5-hour token limit
    ██████████░░░░░░░░░░░░░░░░░ 44% remaining
    Used: NaNM / NaNM
    ```

Root Cause Analysis

The issue occurs in `plugin/lib/zhipu.ts` when calling `formatTokens(tokensLimit.currentValue)` and `formatTokens(tokensLimit.usage)`. These values appear to be `undefined` or `null`, causing the division in `formatTokens()` to return `NaN`.

FormatTokens function:
```typescript
export function formatTokens(tokens: number): string {
return (tokens / 1000000).toFixed(1) + "M";
}
```

Where it"s called:
```typescript
lines.push(
`${t.used}: ${formatTokens(tokensLimit.currentValue)} / ${formatTokens(tokensLimit.usage)}`,
);
```

Potential Causes

  1. API Response Format: Zhipu AI API may be returning data in a different format than expected
  2. Missing Fields: The `currentValue` or `usage` fields may be missing from API response
  3. Data Type Mismatch: API may be returning strings instead of numbers
  4. API Authentication: The API key may not have sufficient permissions to access token count data

Suggested Fix

Add validation and error handling in `formatTokens()` and `formatZhipuUsage()`:

```typescript
export function formatTokens(tokens: number | undefined | null): string {
if (tokens === undefined || tokens === null || isNaN(tokens)) {
return "N/A";
}
return (tokens / 1000000).toFixed(1) + "M";
}
```

And in `formatZhipuUsage()`:
```typescript
if (tokensLimit) {
// ... existing code ...

// Add validation before formatTokens calls
const currentTokens = tokensLimit.currentValue;
const totalTokens = tokensLimit.usage;

if (currentTokens !== undefined && currentTokens !== null && totalTokens !== undefined && totalTokens !== null) {
lines.push(
`${t.used}: ${formatTokens(currentTokens)} / ${formatTokens(totalTokens)}`,
);
} else {
lines.push(`${t.used}: N/A`);
}
}
```

Environment

  • Plan: Zhipu AI (type not specified by user)
  • opencode-mystatus: latest (npm)
  • OS: macOS
  • OpenCode: current version

Additional Context

Note: Issue #23 describes a related problem where multiple TOKENS_LIMIT entries exist but only the first is shown. This issue (#XX) focuses on the NaN display problem which may be separate or related to the API response handling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions