@cockpitify/node 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +65 -65
- package/dist/proxy.js +3 -0
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Cockpitify
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cockpitify
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
# @cockpitify/node
|
|
2
|
-
|
|
3
|
-
Records each user's AI token usage to `ops.usage` in your own Supabase project. Wrap your AI client once per request; the wrapped client behaves exactly like the original.
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
import OpenAI from 'npm:openai';
|
|
7
|
-
import { Ops } from 'npm:@cockpitify/node';
|
|
8
|
-
|
|
9
|
-
const ai = Ops.wrap(new OpenAI({ baseURL: 'https://api.deepseek.com', apiKey }), {
|
|
10
|
-
userId: user.id,
|
|
11
|
-
supabase, // service-role client; writes to ops.usage
|
|
12
|
-
});
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Supported clients
|
|
16
|
-
|
|
17
|
-
- **OpenAI SDK and compatibles** (DeepSeek, Groq, OpenRouter via `baseURL`)
|
|
18
|
-
- `chat.completions.create` (streaming or not)
|
|
19
|
-
- `chat.completions.stream()`
|
|
20
|
-
- `responses.create`
|
|
21
|
-
- **Anthropic SDK**
|
|
22
|
-
- `messages.create` (streaming or not)
|
|
23
|
-
- `messages.stream()`
|
|
24
|
-
- the same two under `beta.messages`
|
|
25
|
-
- **Raw HTTP:** `Ops.wrapFetch(fetch, opts)`. Handles OpenAI-compatible chat, Responses and Anthropic, both JSON and SSE.
|
|
26
|
-
- **Vercel AI SDK:** `wrapLanguageModel({ model, middleware: Ops.middleware(opts) })`.
|
|
27
|
-
|
|
28
|
-
## Calls not measured in tokens
|
|
29
|
-
|
|
30
|
-
Speech-to-text, text-to-speech or image calls are recorded by hand (ops 0.17.0+):
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
Ops.record({ userId, supabase, provider: 'groq', model: 'whisper-large-v3', units: 742, unit: 'seconds' });
|
|
34
|
-
Ops.record({ userId, supabase, provider: 'elevenlabs', model: 'eleven_multilingual_v2', units: 48000, unit: 'characters', costUsd: 0.36 });
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Without `costUsd` the row stays unpriced: ops shows the amount, not a guessed cost.
|
|
38
|
-
|
|
39
|
-
## Streaming
|
|
40
|
-
|
|
41
|
-
Streaming is fully supported, and chunks reach your code unchanged and without delay.
|
|
42
|
-
|
|
43
|
-
- **OpenAI-compatible streams** only report usage when `stream_options.include_usage` is set, so it is added for you.
|
|
44
|
-
- The extra usage-only chunk it produces is hidden from your code, unless you asked for it yourself.
|
|
45
|
-
- Groq is the exception: it reports usage on its own, so its requests are left untouched.
|
|
46
|
-
- **If a stream is cut** (user left, `break`, abort, network error), there is no usage report from the provider. Tokens are then estimated from the prompt and the text seen so far, and the row is stored with `estimated = true`.
|
|
47
|
-
|
|
48
|
-
## Guarantees
|
|
49
|
-
|
|
50
|
-
- **Fail-open.** A recording failure never throws into your code or slows the AI call. It goes to `onError`, which by default logs one warning.
|
|
51
|
-
- **Writes outlive the response.** On Supabase Edge Functions, writes run through `EdgeRuntime.waitUntil`. Elsewhere, call `await Ops.flush()` before the process exits.
|
|
52
|
-
- **Setup mistakes fail loudly.** A missing `userId`, or neither `supabase` nor `sink` given, throws at wrap time.
|
|
53
|
-
|
|
54
|
-
## Options
|
|
55
|
-
|
|
56
|
-
| Option | |
|
|
57
|
-
|---|---|
|
|
58
|
-
| `userId` | Required. The end user to attribute usage to. |
|
|
59
|
-
| `supabase` / `sink` | Where rows go: `ops.usage` through a supabase-js client, or your own `(row) => Promise`. |
|
|
60
|
-
| `feature` | Optional tag, e.g. `'chat'`, `'transcribe'`. |
|
|
61
|
-
| `prices` | USD per 1M tokens per model (prefix match). Without it `cost_usd` is null and the database prices the row. |
|
|
62
|
-
| `provider` | Overrides the provider inferred from `baseURL` / URL. |
|
|
63
|
-
| `injectStreamUsage` | Set `false` to never touch `stream_options`. |
|
|
64
|
-
| `onError` | Receives recording errors. |
|
|
65
|
-
|
|
1
|
+
# @cockpitify/node
|
|
2
|
+
|
|
3
|
+
Records each user's AI token usage to `ops.usage` in your own Supabase project. Wrap your AI client once per request; the wrapped client behaves exactly like the original.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import OpenAI from 'npm:openai';
|
|
7
|
+
import { Ops } from 'npm:@cockpitify/node';
|
|
8
|
+
|
|
9
|
+
const ai = Ops.wrap(new OpenAI({ baseURL: 'https://api.deepseek.com', apiKey }), {
|
|
10
|
+
userId: user.id,
|
|
11
|
+
supabase, // service-role client; writes to ops.usage
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Supported clients
|
|
16
|
+
|
|
17
|
+
- **OpenAI SDK and compatibles** (DeepSeek, Groq, OpenRouter via `baseURL`)
|
|
18
|
+
- `chat.completions.create` (streaming or not)
|
|
19
|
+
- `chat.completions.stream()`
|
|
20
|
+
- `responses.create`
|
|
21
|
+
- **Anthropic SDK**
|
|
22
|
+
- `messages.create` (streaming or not)
|
|
23
|
+
- `messages.stream()`
|
|
24
|
+
- the same two under `beta.messages`
|
|
25
|
+
- **Raw HTTP:** `Ops.wrapFetch(fetch, opts)`. Handles OpenAI-compatible chat, Responses and Anthropic, both JSON and SSE.
|
|
26
|
+
- **Vercel AI SDK:** `wrapLanguageModel({ model, middleware: Ops.middleware(opts) })`.
|
|
27
|
+
|
|
28
|
+
## Calls not measured in tokens
|
|
29
|
+
|
|
30
|
+
Speech-to-text, text-to-speech or image calls are recorded by hand (ops 0.17.0+):
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
Ops.record({ userId, supabase, provider: 'groq', model: 'whisper-large-v3', units: 742, unit: 'seconds' });
|
|
34
|
+
Ops.record({ userId, supabase, provider: 'elevenlabs', model: 'eleven_multilingual_v2', units: 48000, unit: 'characters', costUsd: 0.36 });
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Without `costUsd` the row stays unpriced: ops shows the amount, not a guessed cost.
|
|
38
|
+
|
|
39
|
+
## Streaming
|
|
40
|
+
|
|
41
|
+
Streaming is fully supported, and chunks reach your code unchanged and without delay.
|
|
42
|
+
|
|
43
|
+
- **OpenAI-compatible streams** only report usage when `stream_options.include_usage` is set, so it is added for you.
|
|
44
|
+
- The extra usage-only chunk it produces is hidden from your code, unless you asked for it yourself.
|
|
45
|
+
- Groq is the exception: it reports usage on its own, so its requests are left untouched.
|
|
46
|
+
- **If a stream is cut** (user left, `break`, abort, network error), there is no usage report from the provider. Tokens are then estimated from the prompt and the text seen so far, and the row is stored with `estimated = true`.
|
|
47
|
+
|
|
48
|
+
## Guarantees
|
|
49
|
+
|
|
50
|
+
- **Fail-open.** A recording failure never throws into your code or slows the AI call. It goes to `onError`, which by default logs one warning.
|
|
51
|
+
- **Writes outlive the response.** On Supabase Edge Functions, writes run through `EdgeRuntime.waitUntil`. Elsewhere, call `await Ops.flush()` before the process exits.
|
|
52
|
+
- **Setup mistakes fail loudly.** A missing `userId`, or neither `supabase` nor `sink` given, throws at wrap time.
|
|
53
|
+
|
|
54
|
+
## Options
|
|
55
|
+
|
|
56
|
+
| Option | |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `userId` | Required. The end user to attribute usage to. |
|
|
59
|
+
| `supabase` / `sink` | Where rows go: `ops.usage` through a supabase-js client, or your own `(row) => Promise`. |
|
|
60
|
+
| `feature` | Optional tag, e.g. `'chat'`, `'transcribe'`. |
|
|
61
|
+
| `prices` | USD per 1M tokens per model (prefix match). Without it `cost_usd` is null and the database prices the row. |
|
|
62
|
+
| `provider` | Overrides the provider inferred from `baseURL` / URL. |
|
|
63
|
+
| `injectStreamUsage` | Set `false` to never touch `stream_options`. |
|
|
64
|
+
| `onError` | Receives recording errors. |
|
|
65
|
+
|
package/dist/proxy.js
CHANGED
|
@@ -38,6 +38,9 @@ export function inferProvider(url, fallback) {
|
|
|
38
38
|
return 'deepseek';
|
|
39
39
|
if (s.includes('groq'))
|
|
40
40
|
return 'groq';
|
|
41
|
+
// Gemini's OpenAI-compatible endpoint (generativelanguage.googleapis.com/v1beta/openai).
|
|
42
|
+
if (s.includes('generativelanguage.googleapis.com'))
|
|
43
|
+
return 'gemini';
|
|
41
44
|
if (s.includes('openrouter'))
|
|
42
45
|
return 'openrouter';
|
|
43
46
|
if (s.includes('anthropic'))
|