@compose-market/sdk 0.1.1 → 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/CHANGELOG.md +64 -0
- package/LICENSE +21 -0
- package/README.md +158 -35
- package/dist/errors.d.ts +145 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +152 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +75 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +68 -0
- package/dist/events.js.map +1 -0
- package/dist/http.d.ts +98 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +350 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +340 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/inference.d.ts +175 -0
- package/dist/resources/inference.d.ts.map +1 -0
- package/dist/resources/inference.js +740 -0
- package/dist/resources/inference.js.map +1 -0
- package/dist/resources/instrumentation.d.ts +28 -0
- package/dist/resources/instrumentation.d.ts.map +1 -0
- package/dist/resources/instrumentation.js +43 -0
- package/dist/resources/instrumentation.js.map +1 -0
- package/dist/resources/keys.d.ts +54 -0
- package/dist/resources/keys.d.ts.map +1 -0
- package/dist/resources/keys.js +164 -0
- package/dist/resources/keys.js.map +1 -0
- package/dist/resources/models.d.ts +26 -0
- package/dist/resources/models.d.ts.map +1 -0
- package/dist/resources/models.js +52 -0
- package/dist/resources/models.js.map +1 -0
- package/dist/resources/session-events.d.ts +46 -0
- package/dist/resources/session-events.d.ts.map +1 -0
- package/dist/resources/session-events.js +199 -0
- package/dist/resources/session-events.js.map +1 -0
- package/dist/resources/webhooks.d.ts +27 -0
- package/dist/resources/webhooks.d.ts.map +1 -0
- package/dist/resources/webhooks.js +78 -0
- package/dist/resources/webhooks.js.map +1 -0
- package/dist/resources/x402.d.ts +37 -0
- package/dist/resources/x402.d.ts.map +1 -0
- package/dist/resources/x402.js +72 -0
- package/dist/resources/x402.js.map +1 -0
- package/dist/storage.d.ts +27 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +46 -0
- package/dist/storage.js.map +1 -0
- package/dist/streaming/budget.d.ts +22 -0
- package/dist/streaming/budget.d.ts.map +1 -0
- package/dist/streaming/budget.js +40 -0
- package/dist/streaming/budget.js.map +1 -0
- package/dist/streaming/receipt.d.ts +25 -0
- package/dist/streaming/receipt.d.ts.map +1 -0
- package/dist/streaming/receipt.js +92 -0
- package/dist/streaming/receipt.js.map +1 -0
- package/dist/streaming/sse.d.ts +29 -0
- package/dist/streaming/sse.d.ts.map +1 -0
- package/dist/streaming/sse.js +125 -0
- package/dist/streaming/sse.js.map +1 -0
- package/dist/types/index.d.ts +540 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +10 -0
- package/dist/types/index.js.map +1 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +9 -0
- package/dist/version.js.map +1 -0
- package/package.json +32 -21
- package/index.d.ts +0 -244
- package/index.js +0 -397
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
Live session state surfaces all the way to the caller. First-party parity milestone.
|
|
6
|
+
|
|
7
|
+
### Highlights
|
|
8
|
+
|
|
9
|
+
- **`sdk.events` typed event bus.** Listeners for `budget`, `sessionInvalid`, `sessionActive`, `sessionExpired`, `receipt`. Dispatched automatically on every billable response and every SSE session-event frame. Returns disposers; `.once(...)` fires exactly once.
|
|
10
|
+
- **Live session budget on every response.** `ComposeCompletion<T>` grows `budget` and `sessionInvalidReason` fields sourced from the `x-session-budget-*` / `x-compose-session-invalid` response headers. No more side-channel fetches to `/api/session` to render a session indicator.
|
|
11
|
+
- **SSE session events resource.** `sdk.session.subscribe({ signal })` streams `session-active` / `session-expired` frames from `/api/session/events`. Auto-reconnects with jittered backoff. Wallet context defaults to `sdk.wallets.current()`.
|
|
12
|
+
- **Persistent token storage.** New `storage` + `tokenScope` constructor options. Browsers auto-use `localStorage`; Node callers pass an explicit adapter. Tokens are keyed by `(address, chainId)` so multi-wallet devices never collide. A fresh `ComposeSDK` instance with the same wallet re-hydrates the token without a network call.
|
|
13
|
+
- **`keys.get` / `keys.revoke` now attach `x-session-user-address` + `x-chain-id`.** Matches the canonical header contract the rest of the SDK uses.
|
|
14
|
+
- **Single source of truth for version.** `sdk.version` reads from `package.json` via a build-time generated `src/version.ts`. No more duplicated constants anywhere.
|
|
15
|
+
- **`userAddress` is the canonical field name** across the SDK (was `walletAddress` in internal types); aligns with the header `x-session-user-address` and the Compose Market backend.
|
|
16
|
+
|
|
17
|
+
### New tests
|
|
18
|
+
|
|
19
|
+
- `sdk.events.test.ts` — budget / receipt / sessionInvalid emission, disposer, once semantics, storage persistence, version agreement.
|
|
20
|
+
- `sdk.session-events.test.ts` — SSE iterator + event-bus bridge for `session-active` / `session-expired`, wallet-context guards.
|
|
21
|
+
|
|
22
|
+
### Breaking changes from 0.2.0
|
|
23
|
+
|
|
24
|
+
- `HeaderBagInput.walletAddress` renamed to `HeaderBagInput.userAddress`.
|
|
25
|
+
- `ComposeCallOptions.walletAddress` renamed to `ComposeCallOptions.userAddress`.
|
|
26
|
+
- `ComposeCompletion<T>` now includes `budget` and `sessionInvalidReason`. Existing consumers that destructured `{ data, receipt, requestId, response }` are unaffected; consumers that did `const completion: ComposeCompletion<T> = ...` and then structural-assigned may need to accept the new fields.
|
|
27
|
+
- `audio.speech(...)` return type grows `budget` + `sessionInvalidReason` alongside the existing `{ response, receipt, requestId }`.
|
|
28
|
+
- Streaming final results (`ChatCompletionFinalResult`, `ResponsesStreamFinalResult`) grow `budget` + `sessionInvalidReason`.
|
|
29
|
+
- The `SDK_VERSION` constant is no longer exported at a stable location; use `sdk.version` on an instance.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 0.2.0
|
|
34
|
+
|
|
35
|
+
First production-shaped release. Full rewrite from the 0.1.x typed-fetch prototype.
|
|
36
|
+
|
|
37
|
+
### Highlights
|
|
38
|
+
|
|
39
|
+
- **Modular architecture** — dedicated resource classes (`KeysResource`, `ModelsResource`, `InferenceResource`, `X402Resource`, `WebhooksResource`) over a shared `HttpClient` with lazy request execution (no double-fire when a caller does both `await` and `.asResponse()`).
|
|
40
|
+
- **Streaming** — `AsyncIterable<ChatCompletionChunk>` and `AsyncIterable<ResponseStreamEvent>` with `.final()` for reassembled chat completions, tool-call deltas, and reasoning deltas.
|
|
41
|
+
- **Typed cost receipts** — every billable call returns `{ data, receipt, requestId, response }`; streams emit a terminal `event: compose.receipt` frame; non-streaming calls carry `X-Compose-Receipt` header + `compose_receipt` JSON field.
|
|
42
|
+
- **Canonical Compose model card** — `sdk.models.list()` returns the flat Compose-native shape `{ modelId, name, provider, type, contextWindow, pricing, ... }` exactly as served by `api.compose.market`. `sdk.models.search()` adds cursor pagination with modality/provider/price/context filters over the full ~45k catalog.
|
|
43
|
+
- **x402 v2 first-class** — facilitator `supported`/`chains`/`verify`/`settle` + typed decoders for `PAYMENT-REQUIRED`, `PAYMENT-RESPONSE`, `X-Compose-Receipt`. New `sdk.x402.facilitator.chains()` enumerates live chains + USDC contract metadata.
|
|
44
|
+
- **Typed error tree** — `ComposeError` → `ComposeAPIError` → `BadRequestError` / `AuthenticationError` / `PermissionDeniedError` / `NotFoundError` / `ConflictError` / `UnprocessableEntityError` / `RateLimitError` / `InternalServerError` / `ComposePaymentRequiredError` / `ComposeBudgetExhaustedError`. Every error carries `code`, `status`, `details`, `requestId`, and (for 402) `paymentRequired`.
|
|
45
|
+
- **Webhook verification** — HMAC-SHA256, Stripe-style `X-Compose-Signature: t=...,v1=...` header, constant-time comparison.
|
|
46
|
+
- **Retries + timeouts + AbortSignal** — jittered exponential backoff; honors `Retry-After`; auto-generates `X-Idempotency-Key` on mutations.
|
|
47
|
+
- **Video job streaming** — `sdk.inference.videos.stream(id)` / `.waitUntilDone(id, { onStatus })` for SSE-driven polling.
|
|
48
|
+
|
|
49
|
+
### Tests
|
|
50
|
+
|
|
51
|
+
- **Hermetic.** Three suites: `sdk.unit.test.ts` (pure), `sdk.streaming.test.ts` (in-test synthetic SSE server), `sdk.contract.test.ts` (in-test mock HTTP server with canned responses mirroring `api.compose.market`).
|
|
52
|
+
- No sibling-repo imports. No Redis. No Thirdweb. No secrets. No environment variables. Runs clean from a fresh `git clone`.
|
|
53
|
+
|
|
54
|
+
### Infrastructure prerequisites
|
|
55
|
+
|
|
56
|
+
This release aligns with Compose api/ Phase 0 changes:
|
|
57
|
+
- `GET /api/session` no longer returns the Compose Key token (metadata only).
|
|
58
|
+
- `DELETE /api/keys/:keyId` requires JWT-possession auth.
|
|
59
|
+
- `GET /api/keys/:keyId` added for single-key inspection.
|
|
60
|
+
- `GET /api/x402/facilitator/chains` added for chain metadata enumeration.
|
|
61
|
+
- `X-Request-Id` is set on every response; `X-Compose-Receipt` on billable responses.
|
|
62
|
+
- SSE streams emit named `compose.receipt`, `compose.error`, `compose.video.status` events.
|
|
63
|
+
- CORS is unified (allow-list + `Vary: Origin` + explicit `Expose-Headers`).
|
|
64
|
+
- `POST /api/payments/prepare` honors `X-Idempotency-Key` — replays return the original intent unchanged.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 compose.market
|
|
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,6 +1,11 @@
|
|
|
1
1
|
# `@compose-market/sdk`
|
|
2
2
|
|
|
3
|
-
Official Compose.Market SDK
|
|
3
|
+
Official Compose.Market SDK: Compose Keys, x402 v2 facilitator, 45k+ model catalog, and OpenAI-shaped streaming inference with typed cost receipts.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@compose-market/sdk)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
Zero runtime dependencies. Works in Node 20+, Bun, Deno, Cloudflare Workers, and modern browsers.
|
|
4
9
|
|
|
5
10
|
## Install
|
|
6
11
|
|
|
@@ -11,53 +16,171 @@ npm install @compose-market/sdk
|
|
|
11
16
|
## Quick start
|
|
12
17
|
|
|
13
18
|
```ts
|
|
14
|
-
import { ComposeSDK
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
import { ComposeSDK } from "@compose-market/sdk";
|
|
20
|
+
|
|
21
|
+
// Whatever identity stack you already use produced a wallet address for
|
|
22
|
+
// your user. Pass it through — Compose trusts it exactly the way our
|
|
23
|
+
// first-party apps do.
|
|
24
|
+
const sdk = new ComposeSDK({
|
|
25
|
+
userAddress: "0x1234567890abcdef1234567890abcdef12345678",
|
|
26
|
+
chainId: 43114, // Avalanche C-Chain
|
|
21
27
|
});
|
|
22
28
|
|
|
29
|
+
// Create a 10 USDC / 24 h Compose Key. The token is returned exactly once;
|
|
30
|
+
// store it client-side. Subsequent calls on this SDK instance carry it
|
|
31
|
+
// automatically as `Authorization: Bearer compose-<jwt>`.
|
|
23
32
|
const session = await sdk.keys.create({
|
|
24
33
|
purpose: "session",
|
|
25
|
-
budgetUsd:
|
|
26
|
-
durationHours:
|
|
27
|
-
|
|
34
|
+
budgetUsd: 10,
|
|
35
|
+
durationHours: 24,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Non-streaming chat completion with a cost receipt.
|
|
39
|
+
const { data, receipt, requestId } = await sdk.inference.chat.completions.create({
|
|
40
|
+
model: "gpt-4.1-mini",
|
|
41
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
42
|
+
});
|
|
43
|
+
console.log(data.choices[0].message.content);
|
|
44
|
+
console.log("Charged", receipt?.finalAmountWei, "wei");
|
|
45
|
+
console.log("Request id:", requestId);
|
|
46
|
+
|
|
47
|
+
// Streaming chat completion — async iterable of ChatCompletionChunk.
|
|
48
|
+
const stream = sdk.inference.chat.completions.stream({
|
|
49
|
+
model: "gpt-4.1-mini",
|
|
50
|
+
messages: [{ role: "user", content: "Write a haiku about Avalanche." }],
|
|
28
51
|
});
|
|
52
|
+
for await (const chunk of stream) {
|
|
53
|
+
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
|
|
54
|
+
}
|
|
55
|
+
const { chatCompletion, receipt: streamReceipt } = await stream.final();
|
|
56
|
+
console.log("\nTotal tokens:", chatCompletion.usage.total_tokens);
|
|
57
|
+
console.log("Settled:", streamReceipt?.finalAmountWei, "wei", streamReceipt?.txHash);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Capabilities
|
|
61
|
+
|
|
62
|
+
### Compose Keys
|
|
63
|
+
|
|
64
|
+
- `sdk.keys.create({ purpose, budgetUsd, durationHours, ... })` — creates a Compose Key JWT. Requires the underlying wallet to have pre-approved USDC to the Compose treasury for the requested budget.
|
|
65
|
+
- `sdk.keys.getActive({ chainId? })` — returns current session metadata (budget, expiry, warnings). **Does not** return the token; the token is returned exactly once by `create()` and must be persisted by the integrator.
|
|
66
|
+
- `sdk.keys.list()` — lists all keys for the attached wallet.
|
|
67
|
+
- `sdk.keys.get(keyId)` — inspects a single key. Requires possession of that key's JWT.
|
|
68
|
+
- `sdk.keys.revoke(keyId)` — revokes a key. Requires possession of that key's JWT.
|
|
69
|
+
- `sdk.keys.use(token)` / `sdk.keys.currentToken()` / `sdk.keys.clearToken()` — in-memory token management.
|
|
70
|
+
|
|
71
|
+
### 45k+ Model Catalog
|
|
72
|
+
|
|
73
|
+
- `sdk.models.list()` — curated ~612-model set, canonical Compose shape (`{ modelId, name, provider, type, contextWindow, pricing, input, output, ... }`).
|
|
74
|
+
- `sdk.models.listAll()` — full ~45k catalog.
|
|
75
|
+
- `sdk.models.search({ q, modality, provider, priceMaxPerMTok, contextWindowMin, streaming, cursor, limit })` — cursor-paginated search.
|
|
76
|
+
- `sdk.models.get(modelId)` — single model details.
|
|
77
|
+
- `sdk.models.getParams(modelId)` — optional per-model parameters for image/video generation.
|
|
78
|
+
|
|
79
|
+
### Inference
|
|
80
|
+
|
|
81
|
+
Every billable call resolves to `{ data, receipt, requestId, response }`. The receipt is also available as an SSE `event: compose.receipt` frame on streaming calls and as a `X-Compose-Receipt` base64-url header on all billable responses.
|
|
82
|
+
|
|
83
|
+
- `sdk.inference.chat.completions.create(params)` / `.stream(params)` — OpenAI Chat Completions with typed tool-call delta aggregation and `reasoning_content` deltas.
|
|
84
|
+
- `sdk.inference.responses.create(params)` / `.stream(params)` / `.get(id)` / `.inputItems(id)` / `.cancel(id)` — OpenAI Responses API.
|
|
85
|
+
- `sdk.inference.embeddings.create(params)`.
|
|
86
|
+
- `sdk.inference.images.generate(params)` / `.edit(params)`.
|
|
87
|
+
- `sdk.inference.audio.speech(params)` — returns the raw audio `Response`.
|
|
88
|
+
- `sdk.inference.audio.transcriptions(params)` — multipart/form-data when `file` is a Blob/File/Uint8Array; JSON+base64 when `file` is a string.
|
|
89
|
+
- `sdk.inference.videos.generate(params)` / `.get(id)` / `.stream(id)` / `.waitUntilDone(id, { onStatus })` — async video job polling via SSE.
|
|
90
|
+
|
|
91
|
+
### x402
|
|
92
|
+
|
|
93
|
+
- `sdk.x402.facilitator.supported()` — enumerate schemes + CAIP-2 networks the facilitator is configured for.
|
|
94
|
+
- `sdk.x402.facilitator.chains()` — full chain metadata (USDC contract, explorer, testnet flag).
|
|
95
|
+
- `sdk.x402.facilitator.verify(body)` / `.settle(body)` — direct facilitator access.
|
|
96
|
+
- `sdk.x402.decodePaymentRequired(headerValue)` / `.decodePaymentResponse(headerValue)` / `.decodeReceipt(headerValue)` — typed base64-url decoders for the three x402 v2 headers.
|
|
29
97
|
|
|
30
|
-
|
|
98
|
+
### Webhooks
|
|
31
99
|
|
|
32
|
-
|
|
100
|
+
- `sdk.webhooks.verify({ body, signature, secret })` — HMAC-SHA256 signature verification (constant-time).
|
|
101
|
+
- `sdk.webhooks.constructEvent({ body, signature, secret })` — verify + parse into a typed event. Throws on invalid signatures.
|
|
102
|
+
|
|
103
|
+
## Error model
|
|
104
|
+
|
|
105
|
+
Typed error hierarchy — every error derives from `ComposeError` and has a stable `code` matching the canonical server enum.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import {
|
|
109
|
+
ComposeError,
|
|
110
|
+
ComposeAPIError,
|
|
111
|
+
ComposePaymentRequiredError,
|
|
112
|
+
ComposeBudgetExhaustedError,
|
|
113
|
+
AuthenticationError,
|
|
114
|
+
PermissionDeniedError,
|
|
115
|
+
NotFoundError,
|
|
116
|
+
RateLimitError,
|
|
117
|
+
ComposeConnectionError,
|
|
118
|
+
ComposeTimeoutError,
|
|
119
|
+
} from "@compose-market/sdk";
|
|
33
120
|
|
|
34
121
|
try {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (error instanceof ComposePaymentRequiredError) {
|
|
44
|
-
console.log(error.paymentRequired);
|
|
122
|
+
await sdk.inference.chat.completions.create({ /* ... */ });
|
|
123
|
+
} catch (err) {
|
|
124
|
+
if (err instanceof ComposePaymentRequiredError) {
|
|
125
|
+
console.log("x402 payment required:", err.paymentRequired);
|
|
126
|
+
} else if (err instanceof RateLimitError) {
|
|
127
|
+
console.log("Retry after", err.retryAfter, "seconds");
|
|
128
|
+
} else {
|
|
129
|
+
throw err;
|
|
45
130
|
}
|
|
46
|
-
throw error;
|
|
47
131
|
}
|
|
48
132
|
```
|
|
49
133
|
|
|
50
|
-
##
|
|
134
|
+
## Streaming events
|
|
135
|
+
|
|
136
|
+
The SDK surfaces every SSE event the Compose gateway emits:
|
|
137
|
+
|
|
138
|
+
- Standard OpenAI chunks (`chat.completion.chunk`, `response.output_text.delta`, `response.completed`).
|
|
139
|
+
- **Reasoning deltas** — `reasoning_content` in chat chunks, `response.reasoning.delta` in responses streams.
|
|
140
|
+
- **Tool-call deltas** — assembled across chunks into a typed `ChatCompletionMessageToolCall[]`.
|
|
141
|
+
- **`compose.receipt`** — terminal settlement receipt with tx hash, line items, platform fee.
|
|
142
|
+
- **`compose.error`** — structured stream errors when the upstream fails mid-flight.
|
|
143
|
+
- **`compose.video.status`** — video job progress updates on `videos.stream(id)`.
|
|
144
|
+
|
|
145
|
+
## Retries, timeouts, AbortSignal
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const sdk = new ComposeSDK({
|
|
149
|
+
timeoutMs: 30_000,
|
|
150
|
+
retry: {
|
|
151
|
+
maxRetries: 3,
|
|
152
|
+
initialDelayMs: 500,
|
|
153
|
+
maxDelayMs: 8_000,
|
|
154
|
+
jitter: true,
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const controller = new AbortController();
|
|
159
|
+
setTimeout(() => controller.abort(), 5_000);
|
|
160
|
+
await sdk.inference.chat.completions.create(
|
|
161
|
+
{ model: "gpt-4.1-mini", messages: [{ role: "user", content: "..." }] },
|
|
162
|
+
{ signal: controller.signal },
|
|
163
|
+
);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The SDK auto-retries 408 / 409 / 425 / 429 / 5xx responses and transient network errors, honors `Retry-After` from 429 responses, and auto-generates an `X-Idempotency-Key` on mutating calls so retries are safe against the Compose facilitator.
|
|
167
|
+
|
|
168
|
+
## Supported chains (v1.0)
|
|
169
|
+
|
|
170
|
+
- **Avalanche C-Chain** (`eip155:43114`)
|
|
171
|
+
- **Avalanche Fuji testnet** (`eip155:43113`)
|
|
172
|
+
|
|
173
|
+
Solana support ships in v1.1. Enumerate live chains with `sdk.x402.facilitator.chains()`.
|
|
174
|
+
|
|
175
|
+
## What this SDK is NOT
|
|
176
|
+
|
|
177
|
+
- Not a wallet. The SDK never prompts for a signature, never creates a smart account, never runs KYC. Identity is whatever you already have.
|
|
178
|
+
- Not a retry-forever loop on payment-required errors. 402 responses are surfaced as typed errors; the caller decides what to do next.
|
|
179
|
+
- Not a Thirdweb/Privy/Clerk/Auth0 adapter. It's transport-agnostic on purpose — integrators bring whatever identity stack they already use.
|
|
51
180
|
|
|
52
|
-
|
|
53
|
-
-
|
|
54
|
-
- Discover the full model catalog, per-model parameter schemas, and pricing data.
|
|
55
|
-
- Call Compose inference endpoints through Responses, Chat Completions, Images, Audio, Embeddings, and Video APIs.
|
|
56
|
-
- Decode and work with x402 `PAYMENT-REQUIRED` negotiation details.
|
|
57
|
-
- Call Compose facilitator `supported`, `verify`, and `settle` endpoints directly.
|
|
181
|
+
Not bundled:
|
|
182
|
+
- No wallet client, no identity provider, no auth SDK. Bring the wallet address your stack produced and keep the rest of your infrastructure unchanged.
|
|
58
183
|
|
|
59
|
-
##
|
|
184
|
+
## License
|
|
60
185
|
|
|
61
|
-
|
|
62
|
-
- The core package does not bundle a wallet SDK. External apps can attach any already-known wallet or smart-account address and chain context.
|
|
63
|
-
- Session key creation requires the tied wallet or smart account to already have enough USDC balance and allowance for the requested budget on the selected chain. If not, the SDK throws a `ComposeApiError` with status `402`.
|
|
186
|
+
MIT
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compose Market SDK — typed error hierarchy.
|
|
3
|
+
*
|
|
4
|
+
* Every error raised by the SDK derives from `ComposeError`. Callers can
|
|
5
|
+
* discriminate via `instanceof` (e.g. `err instanceof ComposePaymentRequiredError`)
|
|
6
|
+
* or via `err.code`, which always matches the canonical server error enum.
|
|
7
|
+
*/
|
|
8
|
+
import type { ComposeErrorCode, ComposeReceipt, PaymentRequired } from "./types/index.js";
|
|
9
|
+
export declare class ComposeError extends Error {
|
|
10
|
+
readonly code: ComposeErrorCode;
|
|
11
|
+
readonly status?: number;
|
|
12
|
+
readonly details?: Record<string, unknown>;
|
|
13
|
+
readonly requestId?: string;
|
|
14
|
+
readonly headers?: Record<string, string>;
|
|
15
|
+
readonly body?: unknown;
|
|
16
|
+
constructor(input: {
|
|
17
|
+
code: ComposeErrorCode;
|
|
18
|
+
message: string;
|
|
19
|
+
status?: number;
|
|
20
|
+
details?: Record<string, unknown>;
|
|
21
|
+
requestId?: string;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
body?: unknown;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export declare class ComposeAPIError extends ComposeError {
|
|
27
|
+
constructor(input: {
|
|
28
|
+
code: ComposeErrorCode;
|
|
29
|
+
message: string;
|
|
30
|
+
status: number;
|
|
31
|
+
details?: Record<string, unknown>;
|
|
32
|
+
requestId?: string;
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
body?: unknown;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export declare class BadRequestError extends ComposeAPIError {
|
|
38
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
39
|
+
code?: ComposeErrorCode;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export declare class AuthenticationError extends ComposeAPIError {
|
|
43
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
44
|
+
code?: ComposeErrorCode;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export declare class PermissionDeniedError extends ComposeAPIError {
|
|
48
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
49
|
+
code?: ComposeErrorCode;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export declare class NotFoundError extends ComposeAPIError {
|
|
53
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
54
|
+
code?: ComposeErrorCode;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
export declare class ConflictError extends ComposeAPIError {
|
|
58
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
59
|
+
code?: ComposeErrorCode;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export declare class UnprocessableEntityError extends ComposeAPIError {
|
|
63
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
64
|
+
code?: ComposeErrorCode;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export declare class RateLimitError extends ComposeAPIError {
|
|
68
|
+
readonly retryAfter?: number;
|
|
69
|
+
constructor(input: Omit<ConstructorParameters<typeof ComposeAPIError>[0], "status" | "code"> & {
|
|
70
|
+
code?: ComposeErrorCode;
|
|
71
|
+
retryAfter?: number;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export declare class InternalServerError extends ComposeAPIError {
|
|
75
|
+
constructor(input: {
|
|
76
|
+
code?: ComposeErrorCode;
|
|
77
|
+
message: string;
|
|
78
|
+
status?: number;
|
|
79
|
+
details?: Record<string, unknown>;
|
|
80
|
+
requestId?: string;
|
|
81
|
+
headers?: Record<string, string>;
|
|
82
|
+
body?: unknown;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export declare class ComposePaymentRequiredError extends ComposeAPIError {
|
|
86
|
+
readonly paymentRequired: PaymentRequired | null;
|
|
87
|
+
readonly paymentRequiredHeader: string | null;
|
|
88
|
+
constructor(input: {
|
|
89
|
+
code?: ComposeErrorCode;
|
|
90
|
+
message: string;
|
|
91
|
+
details?: Record<string, unknown>;
|
|
92
|
+
requestId?: string;
|
|
93
|
+
headers?: Record<string, string>;
|
|
94
|
+
body?: unknown;
|
|
95
|
+
paymentRequired: PaymentRequired | null;
|
|
96
|
+
paymentRequiredHeader: string | null;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
export declare class ComposeBudgetExhaustedError extends ComposeAPIError {
|
|
100
|
+
constructor(input: {
|
|
101
|
+
message: string;
|
|
102
|
+
details?: Record<string, unknown>;
|
|
103
|
+
requestId?: string;
|
|
104
|
+
headers?: Record<string, string>;
|
|
105
|
+
body?: unknown;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
export declare class ComposeConnectionError extends ComposeError {
|
|
109
|
+
readonly cause?: unknown;
|
|
110
|
+
constructor(input: {
|
|
111
|
+
message: string;
|
|
112
|
+
cause?: unknown;
|
|
113
|
+
requestId?: string;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
export declare class ComposeTimeoutError extends ComposeError {
|
|
117
|
+
constructor(input: {
|
|
118
|
+
message: string;
|
|
119
|
+
requestId?: string;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
export interface ComposeCallFailure {
|
|
123
|
+
code: ComposeErrorCode;
|
|
124
|
+
message: string;
|
|
125
|
+
details?: Record<string, unknown>;
|
|
126
|
+
receipt?: ComposeReceipt;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Internal constructor: build the most specific error from a parsed HTTP
|
|
130
|
+
* response. Used by the HTTP client before the response is handed back to
|
|
131
|
+
* callers.
|
|
132
|
+
*/
|
|
133
|
+
export declare function buildApiError(input: {
|
|
134
|
+
status: number;
|
|
135
|
+
code: ComposeErrorCode;
|
|
136
|
+
message: string;
|
|
137
|
+
details?: Record<string, unknown>;
|
|
138
|
+
requestId?: string;
|
|
139
|
+
headers?: Record<string, string>;
|
|
140
|
+
body?: unknown;
|
|
141
|
+
paymentRequired?: PaymentRequired | null;
|
|
142
|
+
paymentRequiredHeader?: string | null;
|
|
143
|
+
retryAfter?: number;
|
|
144
|
+
}): ComposeAPIError;
|
|
145
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACR,gBAAgB,EAChB,cAAc,EACd,eAAe,EAClB,MAAM,kBAAkB,CAAC;AAE1B,qBAAa,YAAa,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC3C,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC1C,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;gBAEZ,KAAK,EAAE;QACf,IAAI,EAAE,gBAAgB,CAAC;QACvB,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CA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O,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,cAAc,CAAC;CAC5B;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,gBAAgB,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,eAAe,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;IACzC,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB,GAAG,eAAe,CA4BlB"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compose Market SDK — typed error hierarchy.
|
|
3
|
+
*
|
|
4
|
+
* Every error raised by the SDK derives from `ComposeError`. Callers can
|
|
5
|
+
* discriminate via `instanceof` (e.g. `err instanceof ComposePaymentRequiredError`)
|
|
6
|
+
* or via `err.code`, which always matches the canonical server error enum.
|
|
7
|
+
*/
|
|
8
|
+
export class ComposeError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
status;
|
|
11
|
+
details;
|
|
12
|
+
requestId;
|
|
13
|
+
headers;
|
|
14
|
+
body;
|
|
15
|
+
constructor(input) {
|
|
16
|
+
super(input.message);
|
|
17
|
+
this.name = "ComposeError";
|
|
18
|
+
this.code = input.code;
|
|
19
|
+
this.status = input.status;
|
|
20
|
+
this.details = input.details;
|
|
21
|
+
this.requestId = input.requestId;
|
|
22
|
+
this.headers = input.headers;
|
|
23
|
+
this.body = input.body;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class ComposeAPIError extends ComposeError {
|
|
27
|
+
constructor(input) {
|
|
28
|
+
super(input);
|
|
29
|
+
this.name = "ComposeAPIError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class BadRequestError extends ComposeAPIError {
|
|
33
|
+
constructor(input) {
|
|
34
|
+
super({ code: input.code ?? "validation_error", ...input, status: 400 });
|
|
35
|
+
this.name = "BadRequestError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class AuthenticationError extends ComposeAPIError {
|
|
39
|
+
constructor(input) {
|
|
40
|
+
super({ code: input.code ?? "authentication_failed", ...input, status: 401 });
|
|
41
|
+
this.name = "AuthenticationError";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class PermissionDeniedError extends ComposeAPIError {
|
|
45
|
+
constructor(input) {
|
|
46
|
+
super({ code: input.code ?? "forbidden", ...input, status: 403 });
|
|
47
|
+
this.name = "PermissionDeniedError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class NotFoundError extends ComposeAPIError {
|
|
51
|
+
constructor(input) {
|
|
52
|
+
super({ code: input.code ?? "not_found", ...input, status: 404 });
|
|
53
|
+
this.name = "NotFoundError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export class ConflictError extends ComposeAPIError {
|
|
57
|
+
constructor(input) {
|
|
58
|
+
super({ code: input.code ?? "conflict", ...input, status: 409 });
|
|
59
|
+
this.name = "ConflictError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class UnprocessableEntityError extends ComposeAPIError {
|
|
63
|
+
constructor(input) {
|
|
64
|
+
super({ code: input.code ?? "validation_error", ...input, status: 422 });
|
|
65
|
+
this.name = "UnprocessableEntityError";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export class RateLimitError extends ComposeAPIError {
|
|
69
|
+
retryAfter;
|
|
70
|
+
constructor(input) {
|
|
71
|
+
super({ code: input.code ?? "rate_limited", ...input, status: 429 });
|
|
72
|
+
this.name = "RateLimitError";
|
|
73
|
+
this.retryAfter = input.retryAfter;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export class InternalServerError extends ComposeAPIError {
|
|
77
|
+
constructor(input) {
|
|
78
|
+
super({ code: input.code ?? "internal_error", status: input.status ?? 500, message: input.message, details: input.details, requestId: input.requestId, headers: input.headers, body: input.body });
|
|
79
|
+
this.name = "InternalServerError";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export class ComposePaymentRequiredError extends ComposeAPIError {
|
|
83
|
+
paymentRequired;
|
|
84
|
+
paymentRequiredHeader;
|
|
85
|
+
constructor(input) {
|
|
86
|
+
super({ code: input.code ?? "payment_required", ...input, status: 402 });
|
|
87
|
+
this.name = "ComposePaymentRequiredError";
|
|
88
|
+
this.paymentRequired = input.paymentRequired;
|
|
89
|
+
this.paymentRequiredHeader = input.paymentRequiredHeader;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export class ComposeBudgetExhaustedError extends ComposeAPIError {
|
|
93
|
+
constructor(input) {
|
|
94
|
+
super({ code: "budget_exhausted", ...input, status: 402 });
|
|
95
|
+
this.name = "ComposeBudgetExhaustedError";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export class ComposeConnectionError extends ComposeError {
|
|
99
|
+
cause;
|
|
100
|
+
constructor(input) {
|
|
101
|
+
super({ code: "network_error", ...input });
|
|
102
|
+
this.name = "ComposeConnectionError";
|
|
103
|
+
this.cause = input.cause;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export class ComposeTimeoutError extends ComposeError {
|
|
107
|
+
constructor(input) {
|
|
108
|
+
super({ code: "timeout", ...input });
|
|
109
|
+
this.name = "ComposeTimeoutError";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Internal constructor: build the most specific error from a parsed HTTP
|
|
114
|
+
* response. Used by the HTTP client before the response is handed back to
|
|
115
|
+
* callers.
|
|
116
|
+
*/
|
|
117
|
+
export function buildApiError(input) {
|
|
118
|
+
const base = {
|
|
119
|
+
code: input.code,
|
|
120
|
+
message: input.message,
|
|
121
|
+
details: input.details,
|
|
122
|
+
requestId: input.requestId,
|
|
123
|
+
headers: input.headers,
|
|
124
|
+
body: input.body,
|
|
125
|
+
};
|
|
126
|
+
if (input.status === 402) {
|
|
127
|
+
if (input.code === "budget_exhausted") {
|
|
128
|
+
return new ComposeBudgetExhaustedError(base);
|
|
129
|
+
}
|
|
130
|
+
return new ComposePaymentRequiredError({
|
|
131
|
+
...base,
|
|
132
|
+
paymentRequired: input.paymentRequired ?? null,
|
|
133
|
+
paymentRequiredHeader: input.paymentRequiredHeader ?? null,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (input.status === 400)
|
|
137
|
+
return new BadRequestError(base);
|
|
138
|
+
if (input.status === 401)
|
|
139
|
+
return new AuthenticationError(base);
|
|
140
|
+
if (input.status === 403)
|
|
141
|
+
return new PermissionDeniedError(base);
|
|
142
|
+
if (input.status === 404)
|
|
143
|
+
return new NotFoundError(base);
|
|
144
|
+
if (input.status === 409)
|
|
145
|
+
return new ConflictError(base);
|
|
146
|
+
if (input.status === 422)
|
|
147
|
+
return new UnprocessableEntityError(base);
|
|
148
|
+
if (input.status === 429)
|
|
149
|
+
return new RateLimitError({ ...base, retryAfter: input.retryAfter });
|
|
150
|
+
return new InternalServerError({ ...base, status: input.status });
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,MAAM,OAAO,YAAa,SAAQ,KAAK;IAC1B,IAAI,CAAmB;IACvB,MAAM,CAAU;IAChB,OAAO,CAA2B;IAClC,SAAS,CAAU;IACnB,OAAO,CAA0B;IACjC,IAAI,CAAW;IAExB,YAAY,KAQX;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