Repository navigation
Expand file tree
/
Copy pathOpenCodeGoService.h
More file actions
79 lines (65 loc) · 2.97 KB
/
Copy pathOpenCodeGoService.h
File metadata and controls
79 lines (65 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <string>
#include <vector>
// A small JSON value parser used only for the fixed response shapes returned by
// the OpenCode Go and OpenRouter endpoints. A tiny recursive parser is
// sufficient and avoids pulling in a heavyweight JSON dependency.
class CJsonNode
{
public:
enum class Type { Null, Object, Array, Number, String, Bool };
Type type = Type::Null;
double number = 0;
bool boolean = false;
std::wstring str; // string value (or key for an object)
// Children for object/array nodes.
std::vector<std::pair<std::wstring, CJsonNode>> children;
const CJsonNode* Find(const wchar_t* key) const;
};
// Parses a JSON document (UTF-8 bytes) into a node tree. Returns false on malformed input.
bool JsonParse(const std::string& text, CJsonNode& root);
// ---------------------------------------------
// OpenCode Go usage
// ---------------------------------------------
struct OpenCodeWindow
{
int percent = -1; // -1 => window not reported by the API
std::wstring resets_at; // ISO-8601 text ("" when absent)
bool ok = false; // "status":"ok"
};
struct OpenCodeGoUsage
{
OpenCodeWindow rolling;
OpenCodeWindow weekly;
OpenCodeWindow monthly;
bool has_usage = false; // true when at least one window was parsed
std::string error; // non-empty when the whole request/parse failed
};
// Fetches OpenCode Go usage from https://opencode.ai/zen/go/v1/usage using the
// supplied bearer token. This is a blocking call intended for a background thread.
// On success OpenCodeGoUsage::error is empty and has_usage reflects parsed windows;
// on transport/HTTP/parse failure error carries a short ASCII description.
OpenCodeGoUsage FetchOpenCodeGoUsage(const std::wstring& api_key);
// Formats a percent (0..100) as a wide string, e.g. L"18%" or L"--".
std::wstring FormatPercentText(int percent);
// Formats a window's value string, e.g. L"18% (reset 2026-08-17)" or L"--".
std::wstring FormatWindowValue(int percent, const std::wstring& resets_at, bool include_reset);
// Formats just the reset countdown of one window, e.g. L"1h", L"3d", or L"-"
// when there is no reset time / data.
std::wstring FormatResetCountdown(const std::wstring& resets_at);
// ---------------------------------------------
// OpenRouter credits
// ---------------------------------------------
struct OpenRouterCredits
{
double total = 0; // total_credits (dollars)
double used = 0; // total_usage (dollars)
bool has_data = false;
std::string error;
};
// Fetches OpenRouter credits from https://openrouter.ai/api/v1/credits using the
// supplied bearer token. Returns total_credits and total_usage from the response
// {"data":{"total_credits":...,"total_usage":...}}.
OpenRouterCredits FetchOpenRouterCredits(const std::wstring& api_key);
// Formats a dollar amount as L"$12.34" (two decimals, no thousands separator).
std::wstring FormatMoney(double dollars);