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
- Configure Zhipu AI credentials in `~/.local/share/opencode/auth.json`:
```json
"zhipuai-coding-plan": {
"type": "api",
"key": "YOUR_API_KEY"
}
```
- Call `/mystatus`
- 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
- API Response Format: Zhipu AI API may be returning data in a different format than expected
- Missing Fields: The `currentValue` or `usage` fields may be missing from API response
- Data Type Mismatch: API may be returning strings instead of numbers
- 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.
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
```json
"zhipuai-coding-plan": {
"type": "api",
"key": "YOUR_API_KEY"
}
```
```
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
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
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.