@compose-market/sdk 0.1.0 → 0.2.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 +43 -0
- package/LICENSE +21 -0
- package/README.md +186 -0
- 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/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 +104 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/resources/inference.d.ts +161 -0
- package/dist/resources/inference.d.ts.map +1 -0
- package/dist/resources/inference.js +710 -0
- package/dist/resources/inference.js.map +1 -0
- package/dist/resources/keys.d.ts +50 -0
- package/dist/resources/keys.d.ts.map +1 -0
- package/dist/resources/keys.js +154 -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/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/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 +497 -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/package.json +30 -89
- package/index.d.ts +0 -244
- package/index.js +0 -397
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Receipt helpers.
|
|
3
|
+
*
|
|
4
|
+
* `X-Compose-Receipt` is a url-safe base64 blob carrying an authoritative
|
|
5
|
+
* settlement breakdown. The same shape is also emitted in streams as an SSE
|
|
6
|
+
* `event: compose.receipt` frame and echoed as `compose_receipt` in JSON
|
|
7
|
+
* response bodies.
|
|
8
|
+
*/
|
|
9
|
+
export const RECEIPT_HEADER_NAMES = ["x-compose-receipt", "X-Compose-Receipt"];
|
|
10
|
+
function base64UrlDecode(value) {
|
|
11
|
+
const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
12
|
+
const padding = normalized.length % 4 === 0 ? "" : "=".repeat(4 - (normalized.length % 4));
|
|
13
|
+
if (typeof globalThis.atob === "function") {
|
|
14
|
+
const binary = globalThis.atob(normalized + padding);
|
|
15
|
+
const bytes = new Uint8Array(binary.length);
|
|
16
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
17
|
+
bytes[i] = binary.charCodeAt(i);
|
|
18
|
+
}
|
|
19
|
+
return new TextDecoder("utf-8").decode(bytes);
|
|
20
|
+
}
|
|
21
|
+
// Node fallback (tests + older runtimes).
|
|
22
|
+
return Buffer.from(normalized + padding, "base64").toString("utf-8");
|
|
23
|
+
}
|
|
24
|
+
export function decodeReceiptHeader(value) {
|
|
25
|
+
return JSON.parse(base64UrlDecode(value));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Extract a receipt from a Fetch `Response`. Preference order:
|
|
29
|
+
* 1. `X-Compose-Receipt` header (binary truth)
|
|
30
|
+
* 2. `compose_receipt` field on the JSON body (fallback mirror)
|
|
31
|
+
*/
|
|
32
|
+
export function extractReceiptFromResponse(response, body) {
|
|
33
|
+
const headerValue = response.headers.get("x-compose-receipt") ?? response.headers.get("X-Compose-Receipt");
|
|
34
|
+
if (typeof headerValue === "string" && headerValue.length > 0) {
|
|
35
|
+
try {
|
|
36
|
+
return decodeReceiptHeader(headerValue);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// fall through to body inspection
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (body && typeof body === "object" && body.compose_receipt && typeof body.compose_receipt === "object") {
|
|
43
|
+
return normalizeBodyReceipt(body.compose_receipt);
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
function normalizeBodyReceipt(raw) {
|
|
48
|
+
return {
|
|
49
|
+
subject: typeof raw.subject === "string" ? raw.subject : undefined,
|
|
50
|
+
lineItems: Array.isArray(raw.line_items)
|
|
51
|
+
? raw.line_items.map((item) => ({
|
|
52
|
+
key: String(item.key ?? ""),
|
|
53
|
+
unit: String(item.unit ?? ""),
|
|
54
|
+
quantity: Number(item.quantity ?? 0),
|
|
55
|
+
unitPriceUsd: Number(item.unit_price_usd ?? 0),
|
|
56
|
+
amountWei: String(item.amount_wei ?? "0"),
|
|
57
|
+
}))
|
|
58
|
+
: undefined,
|
|
59
|
+
providerAmountWei: raw.provider_amount_wei ? String(raw.provider_amount_wei) : undefined,
|
|
60
|
+
platformFeeWei: raw.platform_fee_wei ? String(raw.platform_fee_wei) : undefined,
|
|
61
|
+
finalAmountWei: String(raw.final_amount_wei ?? "0"),
|
|
62
|
+
txHash: raw.tx_hash ? String(raw.tx_hash) : undefined,
|
|
63
|
+
network: raw.network ?? "eip155:0",
|
|
64
|
+
settledAt: Number(raw.settled_at ?? 0),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse the payload of an SSE `event: compose.receipt` frame. Throws on
|
|
69
|
+
* malformed JSON so callers can abort the stream consumer.
|
|
70
|
+
*/
|
|
71
|
+
export function parseReceiptEvent(data) {
|
|
72
|
+
const raw = JSON.parse(data);
|
|
73
|
+
return {
|
|
74
|
+
subject: typeof raw.meterSubject === "string" ? raw.meterSubject : (typeof raw.subject === "string" ? raw.subject : undefined),
|
|
75
|
+
lineItems: Array.isArray(raw.lineItems)
|
|
76
|
+
? raw.lineItems.map((item) => ({
|
|
77
|
+
key: String(item.key ?? ""),
|
|
78
|
+
unit: String(item.unit ?? ""),
|
|
79
|
+
quantity: Number(item.quantity ?? 0),
|
|
80
|
+
unitPriceUsd: Number(item.unitPriceUsd ?? 0),
|
|
81
|
+
amountWei: String(item.amountWei ?? "0"),
|
|
82
|
+
}))
|
|
83
|
+
: undefined,
|
|
84
|
+
providerAmountWei: raw.providerAmountWei ? String(raw.providerAmountWei) : undefined,
|
|
85
|
+
platformFeeWei: raw.platformFeeWei ? String(raw.platformFeeWei) : undefined,
|
|
86
|
+
finalAmountWei: String(raw.finalAmountWei ?? "0"),
|
|
87
|
+
txHash: raw.txHash ? String(raw.txHash) : undefined,
|
|
88
|
+
network: raw.network ?? "eip155:0",
|
|
89
|
+
settledAt: Number(raw.settledAt ?? 0),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=receipt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"receipt.js","sourceRoot":"","sources":["../../src/streaming/receipt.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,mBAAmB,EAAE,mBAAmB,CAAU,CAAC;AAExF,SAAS,eAAe,CAAC,KAAa;IAClC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC/D,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;IAE3F,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AAc,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,SAAS;QAC3E,cAAc,EAAE,MAAM,CAAC,GAAG,CAAC,cAAc,IAAI,GAAG,CAAC;QACjD,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS;QACnD,OAAO,EAAG,GAAG,CAAC,OAA8B,IAAI,UAAU;QAC1D,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,SAAS,IAAI,CAAC,CAAC;KACxC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free SSE parser.
|
|
3
|
+
*
|
|
4
|
+
* Works in Node 20+, Bun, Deno, Cloudflare Workers, and modern browsers.
|
|
5
|
+
* Given a `ReadableStream<Uint8Array>` it yields parsed SSE frames with:
|
|
6
|
+
* - `event`: the `event:` field (or "message" if omitted)
|
|
7
|
+
* - `data`: the joined `data:` lines
|
|
8
|
+
* - `id`: the `id:` field if present
|
|
9
|
+
*
|
|
10
|
+
* Compose's named SSE events include `message` (default OpenAI data frames),
|
|
11
|
+
* `compose.receipt`, `compose.error`, `compose.video.status`.
|
|
12
|
+
*
|
|
13
|
+
* The special OpenAI terminator frame `data: [DONE]` short-circuits iteration:
|
|
14
|
+
* it is yielded once with `data === "[DONE]"` and then the stream ends.
|
|
15
|
+
*/
|
|
16
|
+
export interface SSEFrame {
|
|
17
|
+
event: string;
|
|
18
|
+
data: string;
|
|
19
|
+
id?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a browser-style ReadableStream of SSE bytes and yield one frame at a
|
|
23
|
+
* time. `signal` can abort the iterator; when aborted, the underlying stream
|
|
24
|
+
* reader is cancelled.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseSSEStream(stream: ReadableStream<Uint8Array>, options?: {
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}): AsyncGenerator<SSEFrame, void, void>;
|
|
29
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/streaming/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,QAAQ;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,MAAM,CAAC;CACf;AAWD;;;;GAIG;AACH,wBAAuB,cAAc,CACjC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAO,GACvC,cAAc,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC,CA2FtC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free SSE parser.
|
|
3
|
+
*
|
|
4
|
+
* Works in Node 20+, Bun, Deno, Cloudflare Workers, and modern browsers.
|
|
5
|
+
* Given a `ReadableStream<Uint8Array>` it yields parsed SSE frames with:
|
|
6
|
+
* - `event`: the `event:` field (or "message" if omitted)
|
|
7
|
+
* - `data`: the joined `data:` lines
|
|
8
|
+
* - `id`: the `id:` field if present
|
|
9
|
+
*
|
|
10
|
+
* Compose's named SSE events include `message` (default OpenAI data frames),
|
|
11
|
+
* `compose.receipt`, `compose.error`, `compose.video.status`.
|
|
12
|
+
*
|
|
13
|
+
* The special OpenAI terminator frame `data: [DONE]` short-circuits iteration:
|
|
14
|
+
* it is yielded once with `data === "[DONE]"` and then the stream ends.
|
|
15
|
+
*/
|
|
16
|
+
const ENCODER_EOL = /\r\n|\r|\n/;
|
|
17
|
+
function splitLines(buffer) {
|
|
18
|
+
const parts = buffer.split(ENCODER_EOL);
|
|
19
|
+
// The last element might be a partial line; keep it.
|
|
20
|
+
const rest = parts.pop() ?? "";
|
|
21
|
+
return { lines: parts, rest };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse a browser-style ReadableStream of SSE bytes and yield one frame at a
|
|
25
|
+
* time. `signal` can abort the iterator; when aborted, the underlying stream
|
|
26
|
+
* reader is cancelled.
|
|
27
|
+
*/
|
|
28
|
+
export async function* parseSSEStream(stream, options = {}) {
|
|
29
|
+
const reader = stream.getReader();
|
|
30
|
+
const decoder = new TextDecoder("utf-8");
|
|
31
|
+
let buffer = "";
|
|
32
|
+
let currentEvent = "message";
|
|
33
|
+
let currentData = [];
|
|
34
|
+
let currentId;
|
|
35
|
+
const onAbort = () => {
|
|
36
|
+
try {
|
|
37
|
+
reader.cancel();
|
|
38
|
+
}
|
|
39
|
+
catch { /* best-effort */ }
|
|
40
|
+
};
|
|
41
|
+
options.signal?.addEventListener("abort", onAbort);
|
|
42
|
+
try {
|
|
43
|
+
while (true) {
|
|
44
|
+
const { value, done } = await reader.read();
|
|
45
|
+
if (done)
|
|
46
|
+
break;
|
|
47
|
+
buffer += decoder.decode(value, { stream: true });
|
|
48
|
+
const { lines, rest } = splitLines(buffer);
|
|
49
|
+
buffer = rest;
|
|
50
|
+
for (const line of lines) {
|
|
51
|
+
if (line === "") {
|
|
52
|
+
// End of frame. Emit if we have data.
|
|
53
|
+
if (currentData.length > 0) {
|
|
54
|
+
yield {
|
|
55
|
+
event: currentEvent,
|
|
56
|
+
data: currentData.join("\n"),
|
|
57
|
+
id: currentId,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
currentEvent = "message";
|
|
61
|
+
currentData = [];
|
|
62
|
+
currentId = undefined;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (line.startsWith(":")) {
|
|
66
|
+
// Comment / keep-alive — ignore.
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const colonIndex = line.indexOf(":");
|
|
70
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
71
|
+
let value = colonIndex === -1 ? "" : line.slice(colonIndex + 1);
|
|
72
|
+
if (value.startsWith(" ")) {
|
|
73
|
+
value = value.slice(1);
|
|
74
|
+
}
|
|
75
|
+
switch (field) {
|
|
76
|
+
case "event":
|
|
77
|
+
currentEvent = value || "message";
|
|
78
|
+
break;
|
|
79
|
+
case "data":
|
|
80
|
+
currentData.push(value);
|
|
81
|
+
break;
|
|
82
|
+
case "id":
|
|
83
|
+
currentId = value;
|
|
84
|
+
break;
|
|
85
|
+
case "retry":
|
|
86
|
+
// Ignored: the SDK controls retry policy, not the server.
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Flush any final partial frame without a terminating blank line.
|
|
92
|
+
buffer += decoder.decode();
|
|
93
|
+
if (buffer.length > 0) {
|
|
94
|
+
const { lines } = splitLines(buffer + "\n");
|
|
95
|
+
for (const line of lines) {
|
|
96
|
+
if (line === "")
|
|
97
|
+
break;
|
|
98
|
+
if (line.startsWith(":"))
|
|
99
|
+
continue;
|
|
100
|
+
const colonIndex = line.indexOf(":");
|
|
101
|
+
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
|
|
102
|
+
let value = colonIndex === -1 ? "" : line.slice(colonIndex + 1);
|
|
103
|
+
if (value.startsWith(" "))
|
|
104
|
+
value = value.slice(1);
|
|
105
|
+
if (field === "event")
|
|
106
|
+
currentEvent = value || "message";
|
|
107
|
+
else if (field === "data")
|
|
108
|
+
currentData.push(value);
|
|
109
|
+
else if (field === "id")
|
|
110
|
+
currentId = value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (currentData.length > 0) {
|
|
114
|
+
yield { event: currentEvent, data: currentData.join("\n"), id: currentId };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
119
|
+
try {
|
|
120
|
+
reader.releaseLock();
|
|
121
|
+
}
|
|
122
|
+
catch { /* best-effort */ }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/streaming/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAQH,MAAM,WAAW,GAAG,YAAY,CAAC;AAEjC,SAAS,UAAU,CAAC,MAAc;IAC9B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IACxC,qDAAqD;IACrD,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;IAC/B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAClC,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,cAAc,CACjC,MAAkC,EAClC,UAAoC,EAAE;IAEtC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;IAClC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;IACzC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,YAAY,GAAG,SAAS,CAAC;IAC7B,IAAI,WAAW,GAAa,EAAE,CAAC;IAC/B,IAAI,SAA6B,CAAC;IAElC,MAAM,OAAO,GAAG,GAAG,EAAE;QACjB,IAAI,CAAC;YAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACxD,CAAC,CAAC;IACF,OAAO,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAEnD,IAAI,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;YACV,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,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