@swttch/extend-kit 0.3.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/README.ko.md +445 -0
- package/README.md +445 -0
- package/dist/battery/api/client.d.ts +12 -0
- package/dist/battery/api/client.js +54 -0
- package/dist/battery/api/client.js.map +1 -0
- package/dist/battery/api/index.d.ts +3 -0
- package/dist/battery/api/index.js +3 -0
- package/dist/battery/api/index.js.map +1 -0
- package/dist/battery/api/oauth.d.ts +8 -0
- package/dist/battery/api/oauth.js +22 -0
- package/dist/battery/api/oauth.js.map +1 -0
- package/dist/battery/api/types.d.ts +49 -0
- package/dist/battery/api/types.js +2 -0
- package/dist/battery/api/types.js.map +1 -0
- package/dist/battery/auth/file-store.d.ts +2 -0
- package/dist/battery/auth/file-store.js +21 -0
- package/dist/battery/auth/file-store.js.map +1 -0
- package/dist/battery/auth/index.d.ts +6 -0
- package/dist/battery/auth/index.js +23 -0
- package/dist/battery/auth/index.js.map +1 -0
- package/dist/battery/auth/keychain.d.ts +2 -0
- package/dist/battery/auth/keychain.js +21 -0
- package/dist/battery/auth/keychain.js.map +1 -0
- package/dist/battery/auth/types.d.ts +16 -0
- package/dist/battery/auth/types.js +2 -0
- package/dist/battery/auth/types.js.map +1 -0
- package/dist/battery/errors.d.ts +12 -0
- package/dist/battery/errors.js +20 -0
- package/dist/battery/errors.js.map +1 -0
- package/dist/battery/index.d.ts +14 -0
- package/dist/battery/index.js +13 -0
- package/dist/battery/index.js.map +1 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +79 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/oauth.d.ts +2 -0
- package/dist/cli/oauth.js +63 -0
- package/dist/cli/oauth.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.ko.md
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
# @swttch/extend-kit
|
|
2
|
+
|
|
3
|
+
[English](README.md)
|
|
4
|
+
|
|
5
|
+
[Swttch](https://github.com/Swttch/swttch)가 필요로 하지만 플러그인 번들 안에 넣을 수 없어, 사용자 머신에 따로 설치되는 도구들의 모음입니다. 각 도구는 터미널에서든 라이브러리로든 단독으로 쓸 수 있으며, 플러그인은 그 사용자 중 하나일 뿐입니다.
|
|
6
|
+
|
|
7
|
+
| 도구 | 번들에 못 들어가는 이유 |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| **battery** | *사용자 본인의* 머신에 저장된 Claude Code 로그인 정보(keychain 또는 자격증명 파일)를 읽어야 합니다 |
|
|
10
|
+
| **stt** | 예정. 플러그인이 실어 나를 수 없는 네이티브 바이너리가 필요합니다 |
|
|
11
|
+
|
|
12
|
+
## battery
|
|
13
|
+
|
|
14
|
+
Claude Code의 내부 API를 래핑한 TypeScript SDK 및 CLI입니다. Claude Code 사용자의 API 사용량 및 계정 정보를 프로그래매틱하게 접근할 수 있습니다.
|
|
15
|
+
|
|
16
|
+
> **`claude-code-battery`에서 옮겨오시나요?** import만 `@swttch/extend-kit`으로 바꾸면 나머지는 그대로입니다 — export 목록과 `ccb` 명령은 변경되지 않았습니다. 기존 패키지도 계속 동작하며, 이제 이 패키지를 재노출합니다.
|
|
17
|
+
|
|
18
|
+
## 주의사항
|
|
19
|
+
|
|
20
|
+
- **비공식 API**: 이 패키지는 Claude Code 클라이언트의 비공식 내부 API를 사용합니다. Anthropic에서 공식으로 지원하지 않으므로 향후 변경될 수 있습니다.
|
|
21
|
+
- **Claude Code 로그인 필수**: 이 SDK를 사용하려면 로컬 환경에 Claude Code가 설치되어 있고 로그인되어 있어야 합니다.
|
|
22
|
+
- **API Key 지원**: Anthropic API Key를 사용한 인증도 가능합니다. 다만 OAuth 전용 기능(`getUsage()`, `getProfile()`)은 API Key로는 사용할 수 없습니다.
|
|
23
|
+
|
|
24
|
+
## 설치
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @swttch/extend-kit
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Node.js 버전 20 이상이 필요합니다.
|
|
31
|
+
|
|
32
|
+
## 빠른 시작
|
|
33
|
+
|
|
34
|
+
### SDK 사용
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { ClaudeCodeClient } from '@swttch/extend-kit';
|
|
38
|
+
|
|
39
|
+
// 토큰 불필요 — 첫 API 호출 시 자동으로 크레덴셜을 읽어옵니다
|
|
40
|
+
const client = new ClaudeCodeClient();
|
|
41
|
+
|
|
42
|
+
const usage = await client.oauth.getUsage();
|
|
43
|
+
console.log(usage);
|
|
44
|
+
|
|
45
|
+
const profile = await client.oauth.getProfile();
|
|
46
|
+
console.log(profile);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
토큰을 직접 전달할 수도 있습니다:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
// OAuth 접근 토큰 명시
|
|
53
|
+
const client = new ClaudeCodeClient(myOAuthAccessToken);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Anthropic API Key를 사용하려면:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// Anthropic API Key 사용
|
|
60
|
+
const client = new ClaudeCodeClient({ apiKey: process.env.ANTHROPIC_API_KEY });
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### CLI 사용
|
|
64
|
+
|
|
65
|
+
빌드 후 `ccb` 명령으로 사용할 수 있습니다.
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# 빌드
|
|
69
|
+
npm run build
|
|
70
|
+
|
|
71
|
+
# 설치 (전역으로 사용하려면)
|
|
72
|
+
npm install -g .
|
|
73
|
+
|
|
74
|
+
# 사용량 조회
|
|
75
|
+
ccb oauth usage
|
|
76
|
+
|
|
77
|
+
# 프로필 조회
|
|
78
|
+
ccb oauth profile
|
|
79
|
+
|
|
80
|
+
# JSON 형식으로 출력
|
|
81
|
+
ccb oauth usage --json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## API 문서
|
|
85
|
+
|
|
86
|
+
### 인증 함수
|
|
87
|
+
|
|
88
|
+
#### `getCredentials(): Promise<ClaudeCredentials>`
|
|
89
|
+
|
|
90
|
+
Claude Code 저장소에서 크레덴셜을 읽습니다.
|
|
91
|
+
|
|
92
|
+
- **macOS**: Keychain에서 읽음
|
|
93
|
+
- **Windows/Linux**: `~/.claude/.credentials.json` 파일에서 읽음
|
|
94
|
+
- **커스터마이징**: `CLAUDE_CONFIG_DIR` 환경변수로 경로 지정 가능
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const credentials = await getCredentials();
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### `getAccessToken(credentials: ClaudeCredentials): string`
|
|
101
|
+
|
|
102
|
+
크레덴셜에서 접근 토큰을 추출합니다.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const token = getAccessToken(credentials);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### `isTokenExpired(credentials: ClaudeCredentials): boolean`
|
|
109
|
+
|
|
110
|
+
토큰이 만료되었는지 확인합니다.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
if (isTokenExpired(credentials)) {
|
|
114
|
+
console.log('Token has expired');
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 인증 방식
|
|
119
|
+
|
|
120
|
+
| 방식 | 헤더 | 사용 사례 |
|
|
121
|
+
|------|------|----------|
|
|
122
|
+
| OAuth (자동) | `Authorization: Bearer {token}` | Claude Code 로그인 사용자 |
|
|
123
|
+
| OAuth (명시) | `Authorization: Bearer {token}` | 수동 OAuth 토큰 |
|
|
124
|
+
| API Key | `x-api-key: {key}` | Anthropic API 사용자 |
|
|
125
|
+
|
|
126
|
+
**참고**: OAuth 전용 기능(`getUsage()`, `getProfile()`)은 API Key 인증으로는 사용할 수 없습니다.
|
|
127
|
+
|
|
128
|
+
### ClaudeCodeClient
|
|
129
|
+
|
|
130
|
+
API 호출을 위한 클라이언트 클래스입니다. 서브모듈을 통해 API 엔드포인트에 접근합니다.
|
|
131
|
+
|
|
132
|
+
#### `constructor(auth?: string | { apiKey: string })`
|
|
133
|
+
|
|
134
|
+
OAuth 접근 토큰이나 API Key로 클라이언트를 초기화합니다. 인수를 전달하지 않으면 자동으로 크레덴셜을 읽어옵니다.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// 자동으로 크레덴셜 읽어오기
|
|
138
|
+
const client = new ClaudeCodeClient();
|
|
139
|
+
|
|
140
|
+
// OAuth 토큰 명시
|
|
141
|
+
const client = new ClaudeCodeClient(myOAuthAccessToken);
|
|
142
|
+
|
|
143
|
+
// API Key
|
|
144
|
+
const client = new ClaudeCodeClient({ apiKey: process.env.ANTHROPIC_API_KEY });
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### `oauth: OAuthApi`
|
|
148
|
+
|
|
149
|
+
OAuth 관련 API를 제공하는 서브모듈입니다.
|
|
150
|
+
|
|
151
|
+
##### `oauth.getUsage(): Promise<UsageResponse>`
|
|
152
|
+
|
|
153
|
+
사용량 정보를 조회합니다. 5시간, 7일 버킷별 이용률 정보를 반환합니다.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const usage = await client.oauth.getUsage();
|
|
157
|
+
// {
|
|
158
|
+
// five_hour: { utilization: 45, resets_at: '2024-01-01T12:00:00Z' },
|
|
159
|
+
// seven_day: { utilization: 62, resets_at: '2024-01-08T00:00:00Z' },
|
|
160
|
+
// seven_day_opus: { utilization: 30, resets_at: '2024-01-08T00:00:00Z' },
|
|
161
|
+
// seven_day_sonnet: { utilization: 55, resets_at: '2024-01-08T00:00:00Z' },
|
|
162
|
+
// extra_usage: { is_enabled: true, monthly_limit: 1000, ... }
|
|
163
|
+
// }
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
##### `oauth.getProfile(): Promise<ProfileResponse>`
|
|
167
|
+
|
|
168
|
+
계정 및 조직 프로필 정보를 조회합니다.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const profile = await client.oauth.getProfile();
|
|
172
|
+
// {
|
|
173
|
+
// account: {
|
|
174
|
+
// uuid: '...',
|
|
175
|
+
// display_name: 'John Doe',
|
|
176
|
+
// email: 'john@example.com',
|
|
177
|
+
// has_claude_pro: true,
|
|
178
|
+
// ...
|
|
179
|
+
// },
|
|
180
|
+
// organization: {
|
|
181
|
+
// name: 'My Org',
|
|
182
|
+
// organization_type: 'free',
|
|
183
|
+
// subscription_status: 'active',
|
|
184
|
+
// ...
|
|
185
|
+
// },
|
|
186
|
+
// application: { ... }
|
|
187
|
+
// }
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 에러 핸들링
|
|
191
|
+
|
|
192
|
+
SDK에서 발생하는 모든 에러는 `CcbError` 인스턴스이며, 기계가 읽을 수 있는 `code`와 선택적인 `hint`를 포함합니다.
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import { ClaudeCodeClient, CcbError } from '@swttch/extend-kit';
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
const usage = await client.oauth.getUsage();
|
|
199
|
+
} catch (err) {
|
|
200
|
+
if (err instanceof CcbError) {
|
|
201
|
+
console.log(err.code); // 'unsupported_auth'
|
|
202
|
+
console.log(err.hint); // 'Check usage at console.anthropic.com, ...'
|
|
203
|
+
console.log(err.toJSON()); // { error: { code, message, hint } }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### 에러 코드
|
|
209
|
+
|
|
210
|
+
| 코드 | 설명 | 힌트 |
|
|
211
|
+
|------|------|------|
|
|
212
|
+
| `unsupported_auth` | API Key는 이 작업을 지원하지 않음 | console.anthropic.com에서 사용량 확인 또는 OAuth 로그인 사용 |
|
|
213
|
+
| `credentials_not_found` | 크레덴셜 파일을 찾을 수 없음 | `claude login` 명령 실행으로 인증 |
|
|
214
|
+
| `token_expired` | OAuth 토큰이 만료됨 | `claude login` 명령 실행으로 토큰 갱신 |
|
|
215
|
+
| `api_error` | API 요청 실패 (HTTP 상태 포함) | — |
|
|
216
|
+
| `unsupported_platform` | 지원하지 않는 플랫폼 | — |
|
|
217
|
+
|
|
218
|
+
## 타입 정의
|
|
219
|
+
|
|
220
|
+
### 인증 타입
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
interface ApiKeyAuth {
|
|
224
|
+
apiKey: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
type ClientAuth = string | ApiKeyAuth;
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### UsageResponse
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
interface UsageResponse {
|
|
234
|
+
five_hour: UsageBucket | null;
|
|
235
|
+
seven_day: UsageBucket | null;
|
|
236
|
+
seven_day_oauth_apps: UsageBucket | null;
|
|
237
|
+
seven_day_opus: UsageBucket | null;
|
|
238
|
+
seven_day_sonnet: UsageBucket | null;
|
|
239
|
+
seven_day_cowork: UsageBucket | null;
|
|
240
|
+
extra_usage: ExtraUsage | null;
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### UsageBucket
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
interface UsageBucket {
|
|
248
|
+
utilization: number; // 0-100 %
|
|
249
|
+
resets_at: string | null; // ISO 8601 형식의 리셋 시간
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### ExtraUsage
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
interface ExtraUsage {
|
|
257
|
+
is_enabled: boolean;
|
|
258
|
+
monthly_limit: number | null;
|
|
259
|
+
used_credits: number | null;
|
|
260
|
+
utilization: number | null;
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### ProfileResponse
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
interface ProfileResponse {
|
|
268
|
+
account: AccountInfo;
|
|
269
|
+
organization: OrganizationInfo;
|
|
270
|
+
application: ApplicationInfo;
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### AccountInfo
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
interface AccountInfo {
|
|
278
|
+
uuid: string;
|
|
279
|
+
full_name: string;
|
|
280
|
+
display_name: string;
|
|
281
|
+
email: string;
|
|
282
|
+
has_claude_max: boolean;
|
|
283
|
+
has_claude_pro: boolean;
|
|
284
|
+
created_at: string;
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### OrganizationInfo
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
interface OrganizationInfo {
|
|
292
|
+
uuid: string;
|
|
293
|
+
name: string;
|
|
294
|
+
organization_type: string;
|
|
295
|
+
billing_type: string;
|
|
296
|
+
rate_limit_tier: string;
|
|
297
|
+
has_extra_usage_enabled: boolean;
|
|
298
|
+
subscription_status: string;
|
|
299
|
+
subscription_created_at: string;
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### ApplicationInfo
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
interface ApplicationInfo {
|
|
307
|
+
uuid: string;
|
|
308
|
+
name: string;
|
|
309
|
+
slug: string;
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### ClaudeCredentials
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
interface ClaudeCredentials {
|
|
317
|
+
claudeAiOauth: ClaudeOAuthCredentials;
|
|
318
|
+
organizationUuid: string;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface ClaudeOAuthCredentials {
|
|
322
|
+
accessToken: string;
|
|
323
|
+
refreshToken: string;
|
|
324
|
+
expiresAt: number;
|
|
325
|
+
scopes: string[];
|
|
326
|
+
subscriptionType: string;
|
|
327
|
+
rateLimitTier: string;
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## CLI 명령어
|
|
332
|
+
|
|
333
|
+
### usage
|
|
334
|
+
|
|
335
|
+
사용량 제한 정보를 조회합니다.
|
|
336
|
+
|
|
337
|
+
```bash
|
|
338
|
+
ccb oauth usage
|
|
339
|
+
|
|
340
|
+
# 출력 예:
|
|
341
|
+
# Usage:
|
|
342
|
+
# five hour: 45%
|
|
343
|
+
# seven day: 62% (resets 2024-01-08T00:00:00)
|
|
344
|
+
# seven day opus: 30% (resets 2024-01-08T00:00:00)
|
|
345
|
+
# seven day sonnet: 55% (resets 2024-01-08T00:00:00)
|
|
346
|
+
# extra usage: enabled
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### profile
|
|
350
|
+
|
|
351
|
+
계정 및 조직 프로필을 조회합니다.
|
|
352
|
+
|
|
353
|
+
```bash
|
|
354
|
+
ccb oauth profile
|
|
355
|
+
|
|
356
|
+
# 출력 예:
|
|
357
|
+
# Profile:
|
|
358
|
+
# name: John Doe
|
|
359
|
+
# email: john@example.com
|
|
360
|
+
# plan: free
|
|
361
|
+
# org: My Organization
|
|
362
|
+
# status: active
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### 옵션
|
|
366
|
+
|
|
367
|
+
- `--json`: JSON 형식으로 출력
|
|
368
|
+
- `-h, --help`: 도움말 표시
|
|
369
|
+
- `-v, --version`: 버전 정보 표시
|
|
370
|
+
|
|
371
|
+
```bash
|
|
372
|
+
ccb oauth usage --json
|
|
373
|
+
ccb oauth profile --json
|
|
374
|
+
ccb --version
|
|
375
|
+
ccb --help
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### 에러 출력
|
|
379
|
+
|
|
380
|
+
에러 출력도 `--json` 플래그를 따릅니다:
|
|
381
|
+
|
|
382
|
+
```bash
|
|
383
|
+
$ ccb oauth usage
|
|
384
|
+
Error: API Key authentication does not support usage/profile queries.
|
|
385
|
+
Hint: Check usage at console.anthropic.com, or use OAuth login (claude login)
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
```bash
|
|
389
|
+
$ ccb oauth usage --json
|
|
390
|
+
{
|
|
391
|
+
"error": {
|
|
392
|
+
"code": "unsupported_auth",
|
|
393
|
+
"message": "API Key authentication does not support usage/profile queries.",
|
|
394
|
+
"hint": "Check usage at console.anthropic.com, or use OAuth login (claude login)"
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
## 개발
|
|
400
|
+
|
|
401
|
+
### 설치
|
|
402
|
+
|
|
403
|
+
```bash
|
|
404
|
+
npm install
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### 빌드
|
|
408
|
+
|
|
409
|
+
```bash
|
|
410
|
+
npm run build
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
TypeScript를 JavaScript로 컴파일합니다. 결과물은 `dist/` 디렉토리에 저장됩니다.
|
|
414
|
+
|
|
415
|
+
### Watch 모드
|
|
416
|
+
|
|
417
|
+
```bash
|
|
418
|
+
npm run dev
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
파일 변경을 감지하여 자동으로 컴파일합니다.
|
|
422
|
+
|
|
423
|
+
### 타입 검사
|
|
424
|
+
|
|
425
|
+
```bash
|
|
426
|
+
npm run lint
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
TypeScript 타입 검사를 실행합니다.
|
|
430
|
+
|
|
431
|
+
### 테스트
|
|
432
|
+
|
|
433
|
+
```bash
|
|
434
|
+
npm test
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
`dist/**/*.test.js` 파일을 실행합니다.
|
|
438
|
+
|
|
439
|
+
## 라이선스
|
|
440
|
+
|
|
441
|
+
MIT
|
|
442
|
+
|
|
443
|
+
## 작성자
|
|
444
|
+
|
|
445
|
+
yhk1038
|