-
Notifications
You must be signed in to change notification settings - Fork 188
feat: add CrofAI provider #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
msadiks
wants to merge
4
commits into
robinebers:main
Choose a base branch
from
msadiks:crofai-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # CrofAI | ||
|
|
||
| > Uses the CrofAI Usage API with a user-provided API key. | ||
|
|
||
| ## Overview | ||
|
|
||
| - **Protocol:** HTTPS (JSON) | ||
| - **Endpoint:** `GET https://crof.ai/usage_api/` | ||
| - **Auth:** `Authorization: Bearer <api_key>` | ||
| - **Data:** credit balance, daily requests remaining | ||
|
|
||
| ## Authentication | ||
|
|
||
| Set the `CROFAI_API_KEY` environment variable: | ||
|
|
||
| ```bash | ||
| export CROFAI_API_KEY="your-api-key-here" | ||
| ``` | ||
|
|
||
| The key is read from the environment at probe time. Restart OpenUsage after setting it. | ||
|
|
||
| ## Data Source | ||
|
|
||
| Request: | ||
|
|
||
| ```http | ||
| GET /usage_api/ HTTP/1.1 | ||
| Host: crof.ai | ||
| Authorization: Bearer <api_key> | ||
| Accept: application/json | ||
| ``` | ||
|
|
||
| Response: | ||
|
|
||
| ```json | ||
| { | ||
| "credits": 12.3456, | ||
| "usable_requests": 450 | ||
| } | ||
| ``` | ||
|
|
||
| | Field | Type | Description | | ||
| |---|---|---| | ||
| | `credits` | number | Available credit balance (USD) | | ||
| | `usable_requests` | number \| null | Requests remaining today (`null` if not on a subscription plan) | | ||
|
|
||
| ## Output | ||
|
|
||
| - **Requests** (overview text line): requests remaining today (e.g., `450 remaining`); hidden when `usable_requests` is null | ||
| - **Credits** (overview text line): formatted dollar balance (e.g., `$12.35`) | ||
|
|
||
| ## Errors | ||
|
|
||
| | Condition | Message | | ||
| |---|---|---| | ||
| | Missing `CROFAI_API_KEY` env var | `No CROFAI_API_KEY found. Set up environment variable first.` | | ||
| | HTTP 401/403 | `API key invalid. Check your CrofAI API key.` | | ||
| | Non-2xx | `Usage request failed (HTTP {status}). Try again later.` | | ||
| | Network failure | `Usage request failed. Check your connection.` | | ||
| | Unparseable or invalid response shape/type | `Usage response invalid. Try again later.` | | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| (function () { | ||
| var API_URL = "https://crof.ai/usage_api/" | ||
|
|
||
| function probe(ctx) { | ||
| var apiKey = ctx.host.env.get("CROFAI_API_KEY") | ||
| if (!apiKey || !String(apiKey).trim()) { | ||
| throw "No CROFAI_API_KEY found. Set up environment variable first." | ||
| } | ||
|
|
||
| var resp | ||
| try { | ||
| resp = ctx.util.request({ | ||
| method: "GET", | ||
| url: API_URL, | ||
| headers: { | ||
| Authorization: "Bearer " + String(apiKey).trim(), | ||
| Accept: "application/json", | ||
| }, | ||
| timeoutMs: 10000, | ||
| }) | ||
| } catch (e) { | ||
| throw "Usage request failed. Check your connection." | ||
| } | ||
|
|
||
| if (ctx.util.isAuthStatus(resp.status)) { | ||
| throw "API key invalid. Check your CrofAI API key." | ||
| } | ||
|
|
||
| if (resp.status < 200 || resp.status >= 300) { | ||
| throw "Usage request failed (HTTP " + String(resp.status) + "). Try again later." | ||
| } | ||
|
|
||
| var data = ctx.util.tryParseJson(resp.bodyText) | ||
| if (!data || typeof data !== "object" || Array.isArray(data)) { | ||
| throw "Usage response invalid. Try again later." | ||
| } | ||
|
|
||
| var hasCredits = typeof data.credits === "number" && Number.isFinite(data.credits) | ||
| if ("credits" in data && !hasCredits) { | ||
| throw "Usage response invalid. Try again later." | ||
| } | ||
| var credits = hasCredits ? data.credits : 0 | ||
|
|
||
| var lines = [] | ||
|
|
||
| var usableRequests = data.usable_requests | ||
| if (usableRequests !== null && usableRequests !== undefined) { | ||
| if (typeof usableRequests !== "number" || !Number.isFinite(usableRequests)) { | ||
| throw "Usage response invalid. Try again later." | ||
| } | ||
| lines.push( | ||
| ctx.line.text({ | ||
| label: "Requests", | ||
| value: String(usableRequests) + " remaining", | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| if (hasCredits && credits >= 0) { | ||
| lines.push( | ||
| ctx.line.text({ | ||
| label: "Credits", | ||
| value: "$" + credits.toFixed(2), | ||
| }) | ||
| ) | ||
| } | ||
|
|
||
| return { lines: lines } | ||
| } | ||
|
|
||
| globalThis.__openusage_plugin = { id: "crofai", probe } | ||
| })() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "schemaVersion": 1, | ||
| "id": "crofai", | ||
| "name": "CrofAI", | ||
| "version": "0.0.1", | ||
| "entry": "plugin.js", | ||
| "icon": "icon.svg", | ||
| "brandColor": "#000000", | ||
| "links": [ | ||
| { "label": "Status", "url": "https://status.nahcrof.com" } | ||
| ], | ||
| "lines": [ | ||
| { "type": "text", "label": "Requests", "scope": "overview" }, | ||
| { "type": "text", "label": "Credits", "scope": "overview" } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.