@argosvix/sdk 0.1.0
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 -0
- package/README.md +162 -0
- package/dist/client.d.ts +24 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +887 -0
- package/dist/client.js.map +1 -0
- package/dist/flush.d.ts +29 -0
- package/dist/flush.d.ts.map +1 -0
- package/dist/flush.js +41 -0
- package/dist/flush.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/pricing.d.ts +32 -0
- package/dist/pricing.d.ts.map +1 -0
- package/dist/pricing.js +137 -0
- package/dist/pricing.js.map +1 -0
- package/dist/recorder.d.ts +31 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +100 -0
- package/dist/recorder.js.map +1 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 jissocyu
|
|
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
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# @argosvix/sdk
|
|
2
|
+
|
|
3
|
+
Transparent observability wrapper for AI provider SDKs. Wrap a single line of code and get cost / latency / token / error records for every LLM call across OpenAI, Anthropic, Gemini, and Mistral.
|
|
4
|
+
|
|
5
|
+
> 🚧 **Pre-MVP** — backend ingest + dashboard は近日公開。 npm publish は `--tag alpha` で予定。 早期アクセスは [argosvix.com](https://argosvix.com) で waitlist 受付中。
|
|
6
|
+
>
|
|
7
|
+
> 設計方針:
|
|
8
|
+
> - **顧客のエンドユーザー PII は送信しない**(prompt / completion 本文は記録対象外、 token 数 + cost + latency + error code のみ)
|
|
9
|
+
> - **wrap idempotent**(二重 wrap しても安全、 WeakMap / WeakSet で 同一 instance を再 wrap しない)
|
|
10
|
+
> - **runtime monkey-patch なし**(global を汚さず、 client 引数のみ mutate)
|
|
11
|
+
|
|
12
|
+
## Supported providers
|
|
13
|
+
|
|
14
|
+
| provider | hook | streaming | notes |
|
|
15
|
+
|---|---|---|---|
|
|
16
|
+
| **OpenAI** chat.completions | ✅ | ✅ | `chat.completions.create` |
|
|
17
|
+
| **OpenAI** responses | ✅ | warn + skip(Phase B 以降)| `responses.create` |
|
|
18
|
+
| **Anthropic** | ✅ | ✅ | `messages.create`(`message_start` + `message_delta` 累積)|
|
|
19
|
+
| **Mistral** | ✅ | ✅ | `chat.complete` + `chat.stream` |
|
|
20
|
+
| **Gemini legacy**(`@google/generative-ai`)| ✅ | ✅ | `getGenerativeModel({...}).generateContent` / `generateContentStream` |
|
|
21
|
+
| **Gemini current**(`@google/genai`)| ✅ | ✅ | `client.models.generateContent` / `generateContentStream` |
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# alpha 段階 (= 公開後)
|
|
27
|
+
npm install @argosvix/sdk@alpha openai
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
API key は [argosvix.com](https://argosvix.com) で waitlist 登録後、 launch 時に発行されます。 設定:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
export ARGOSVIX_API_KEY=argosvix_live_...
|
|
34
|
+
# default ingest endpoint = https://ingest.argosvix.com
|
|
35
|
+
# 自前 deploy する場合 = ARGOSVIX_API_BASE で override 可能
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import OpenAI from "openai";
|
|
42
|
+
import { wrap, getRecorder } from "@argosvix/sdk";
|
|
43
|
+
|
|
44
|
+
const client = wrap(new OpenAI(), {
|
|
45
|
+
apiKey: process.env.ARGOSVIX_API_KEY,
|
|
46
|
+
tags: { service: "support-bot", env: "production" },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Use the wrapped client exactly like the original
|
|
50
|
+
const response = await client.chat.completions.create({
|
|
51
|
+
model: "gpt-5.5",
|
|
52
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// optional: graceful flush before process exit
|
|
56
|
+
const recorder = getRecorder(client);
|
|
57
|
+
if (recorder) await recorder.flush();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
各呼び出しで自動的に記録される項目:
|
|
61
|
+
|
|
62
|
+
## Short-lived runtimes(Cloudflare Workers / AWS Lambda / Vercel Edge)
|
|
63
|
+
|
|
64
|
+
これらの runtime は handler が return した瞬間に outstanding fire-and-forget fetch を kill する設計です。 SDK の buffer は `bufferMaxSize` (default 100) に到達するまで auto-flush しないため、 1 リクエスト = 数回の LLM call では context exit で record が失われます。
|
|
65
|
+
|
|
66
|
+
対策 = handler の `finally` で `flushClient` を await:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
70
|
+
import { wrap, flushClient } from "@argosvix/sdk";
|
|
71
|
+
|
|
72
|
+
export default {
|
|
73
|
+
async scheduled(_event, env) {
|
|
74
|
+
const client = wrap(new Anthropic({ apiKey: env.ANTHROPIC_API_KEY }), {
|
|
75
|
+
apiKey: env.ARGOSVIX_API_KEY,
|
|
76
|
+
});
|
|
77
|
+
try {
|
|
78
|
+
const res = await client.messages.create({
|
|
79
|
+
model: "claude-opus-4-1",
|
|
80
|
+
max_tokens: 200,
|
|
81
|
+
messages: [{ role: "user", content: "..." }],
|
|
82
|
+
});
|
|
83
|
+
// ...
|
|
84
|
+
} finally {
|
|
85
|
+
await flushClient(client); // 必須: backend 到達を保証
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
長時間 dev server(Express / Next.js dev)や stateful Node プロセスでは不要(buffer auto-flush + process exit までは十分な時間がある)。
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
- Prompt + completion token 数(camelCase の `promptTokens` / `completionTokens` で保持)
|
|
95
|
+
- USD コスト(provider 別 pricing テーブル + resource-prefixed model 名 `models/gemini-2.0-flash` 対応)
|
|
96
|
+
- 応答時間(ミリ秒)
|
|
97
|
+
- ストリーミング = 最終 chunk の usage を取得した時点で record(consumer 完了と同期)
|
|
98
|
+
- 任意タグ(`tags: { service: "X", env: "prod" }`)
|
|
99
|
+
- エラー詳細(`statusCode` / `code` / `type` / `retryAfter`)
|
|
100
|
+
|
|
101
|
+
**記録しない項目**: prompt 本文、 completion 本文、 system message、 tool call の引数本文。 集計に必要な metadata のみ送信し、 PII / 機密文字列を保管しない設計。
|
|
102
|
+
|
|
103
|
+
## Composite client / explicit provider
|
|
104
|
+
|
|
105
|
+
Auto-detect が ambiguous な場合 = `config.provider` で明示指定:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const client = wrap(myCompositeClient, { provider: "openai" });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Tags + 集計
|
|
112
|
+
|
|
113
|
+
`tags` は record ごとに保存され、 backend dashboard で「`service=support-bot` の cost trend」 等の cross-dimension 集計が可能(Phase C dashboard 以降)。
|
|
114
|
+
|
|
115
|
+
## Streaming
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const stream = await client.chat.completions.create({
|
|
119
|
+
model: "gpt-5.5",
|
|
120
|
+
messages: [...],
|
|
121
|
+
stream: true,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
for await (const chunk of stream) {
|
|
125
|
+
// 通常の AsyncIterable として消費
|
|
126
|
+
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
|
|
127
|
+
}
|
|
128
|
+
// stream 完了時点で record が backend に POST される
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Examples
|
|
132
|
+
|
|
133
|
+
[`examples/`](../../examples/) に 3 stack の minimum integration:
|
|
134
|
+
- [`examples/nextjs-openai/`](../../examples/nextjs-openai/)= Next.js 14 App Router
|
|
135
|
+
- [`examples/express-anthropic/`](../../examples/express-anthropic/)= Express + streaming + SIGTERM flush
|
|
136
|
+
- [`examples/lambda-gemini/`](../../examples/lambda-gemini/)= AWS Lambda + `@google/genai` + container reuse 注意
|
|
137
|
+
|
|
138
|
+
## API
|
|
139
|
+
|
|
140
|
+
### `wrap<T>(client: T, config?: ArgosvixConfig): T`
|
|
141
|
+
Transparently wrap an AI SDK client. Returns the same client(mutated)so the call site shape is unchanged.
|
|
142
|
+
|
|
143
|
+
### `getRecorder(client: object): Recorder | null`
|
|
144
|
+
Retrieve the Recorder instance associated with a wrapped client.
|
|
145
|
+
|
|
146
|
+
### `flushClient(client: object): Promise<void>`
|
|
147
|
+
Cloudflare Workers / Lambda 等 short-lived runtime 向け helper。 handler の finally で await すると、 SDK buffer を明示 flush + backend 到達待ちまで carry する。 失敗は console.error のみで throw しない(production の inline error 防止)。
|
|
148
|
+
|
|
149
|
+
### `class Recorder`
|
|
150
|
+
- `record(record: LlmCallRecord): void`
|
|
151
|
+
- `flush(): Promise<LlmCallRecord[]>` — POST buffer to backend、 retry on 5xx/network、 4xx no-retry
|
|
152
|
+
- `getBufferSize(): number`
|
|
153
|
+
|
|
154
|
+
### `calculateCost(provider, model, promptTokens, completionTokens): number`
|
|
155
|
+
Internal pricing calculator(USD)。 公開して unit-test や custom pricing で活用可能。 resource-prefixed model 名(`models/gemini-2.0-flash` 等)= terminal model 部分で lookup。
|
|
156
|
+
|
|
157
|
+
### types
|
|
158
|
+
`Provider` / `ArgosvixConfig` / `LlmCallRecord` / `PricingEntry`
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ArgosvixConfig } from "./types.js";
|
|
2
|
+
import { Recorder } from "./recorder.js";
|
|
3
|
+
/**
|
|
4
|
+
* Wrap an AI provider SDK client to transparently record every call.
|
|
5
|
+
*
|
|
6
|
+
* Supported: OpenAI (chat.completions + responses), Anthropic (messages),
|
|
7
|
+
* Mistral (chat.complete + chat.stream), Gemini (legacy `@google/generative-ai`
|
|
8
|
+
* + current `@google/genai`).
|
|
9
|
+
*
|
|
10
|
+
* Provider detection: constructor-name fingerprint with shape-based fallback.
|
|
11
|
+
* For composite or adapter clients, pass `config.provider` to override detection.
|
|
12
|
+
*
|
|
13
|
+
* Idempotency: wrapping the same client twice is a no-op.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import OpenAI from "openai";
|
|
17
|
+
* import { wrap, getRecorder } from "@argosvix/sdk";
|
|
18
|
+
*
|
|
19
|
+
* const client = wrap(new OpenAI(), { apiKey: "...", tags: { service: "bot" } });
|
|
20
|
+
* const recorder = getRecorder(client);
|
|
21
|
+
*/
|
|
22
|
+
export declare function wrap<T extends object>(client: T, config?: ArgosvixConfig): T;
|
|
23
|
+
export declare function getRecorder(client: object): Recorder | null;
|
|
24
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAA2B,MAAM,YAAY,CAAC;AAE1E,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAKzC;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAE,cAAmB,GAAG,CAAC,CAiChF;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAE3D"}
|