@compose-market/sdk 0.8.1 → 0.8.2

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.
Files changed (128) hide show
  1. package/.speakeasy/a2a.arazzo.yaml +1 -1
  2. package/.speakeasy/memory.arazzo.yaml +1 -1
  3. package/.speakeasy/tests.arazzo.yaml +1 -1
  4. package/generated/inference/esm/lib/config.d.ts +3 -3
  5. package/generated/inference/esm/lib/config.js +3 -3
  6. package/generated/inference/package.json +1 -1
  7. package/generated/inference/src/lib/config.ts +3 -3
  8. package/generated/manowar/esm/lib/config.d.ts +3 -3
  9. package/generated/manowar/esm/lib/config.js +3 -3
  10. package/generated/manowar/package.json +1 -1
  11. package/generated/manowar/src/lib/config.ts +3 -3
  12. package/generated/memory/esm/lib/config.d.ts +3 -3
  13. package/generated/memory/esm/lib/config.js +3 -3
  14. package/generated/memory/package.json +1 -1
  15. package/generated/memory/src/lib/config.ts +3 -3
  16. package/generated/x402/esm/lib/config.d.ts +3 -3
  17. package/generated/x402/esm/lib/config.js +3 -3
  18. package/generated/x402/package.json +1 -1
  19. package/generated/x402/src/lib/config.ts +3 -3
  20. package/package.json +3 -3
  21. package/specs/inference.openapi.yaml +1 -1
  22. package/specs/manowar.openapi.yaml +1 -1
  23. package/specs/memory.openapi.yaml +1 -1
  24. package/specs/x402.openapi.yaml +1 -1
  25. package/dist/errors.d.ts +0 -2
  26. package/dist/errors.d.ts.map +0 -1
  27. package/dist/errors.js +0 -2
  28. package/dist/errors.js.map +0 -1
  29. package/dist/events.d.ts +0 -202
  30. package/dist/events.d.ts.map +0 -1
  31. package/dist/events.js +0 -70
  32. package/dist/events.js.map +0 -1
  33. package/dist/index.d.ts +0 -277
  34. package/dist/index.d.ts.map +0 -1
  35. package/dist/index.js +0 -551
  36. package/dist/index.js.map +0 -1
  37. package/dist/resources/accounts.d.ts +0 -32
  38. package/dist/resources/accounts.d.ts.map +0 -1
  39. package/dist/resources/accounts.js +0 -128
  40. package/dist/resources/accounts.js.map +0 -1
  41. package/dist/resources/agent.d.ts +0 -58
  42. package/dist/resources/agent.d.ts.map +0 -1
  43. package/dist/resources/agent.js +0 -773
  44. package/dist/resources/agent.js.map +0 -1
  45. package/dist/resources/channels.d.ts +0 -24
  46. package/dist/resources/channels.d.ts.map +0 -1
  47. package/dist/resources/channels.js +0 -74
  48. package/dist/resources/channels.js.map +0 -1
  49. package/dist/resources/directory.d.ts +0 -43
  50. package/dist/resources/directory.d.ts.map +0 -1
  51. package/dist/resources/directory.js +0 -97
  52. package/dist/resources/directory.js.map +0 -1
  53. package/dist/resources/dispenser.d.ts +0 -25
  54. package/dist/resources/dispenser.d.ts.map +0 -1
  55. package/dist/resources/dispenser.js +0 -56
  56. package/dist/resources/dispenser.js.map +0 -1
  57. package/dist/resources/feedback.d.ts +0 -35
  58. package/dist/resources/feedback.d.ts.map +0 -1
  59. package/dist/resources/feedback.js +0 -127
  60. package/dist/resources/feedback.js.map +0 -1
  61. package/dist/resources/inference.d.ts +0 -206
  62. package/dist/resources/inference.d.ts.map +0 -1
  63. package/dist/resources/inference.js +0 -1049
  64. package/dist/resources/inference.js.map +0 -1
  65. package/dist/resources/instrumentation.d.ts +0 -29
  66. package/dist/resources/instrumentation.d.ts.map +0 -1
  67. package/dist/resources/instrumentation.js +0 -43
  68. package/dist/resources/instrumentation.js.map +0 -1
  69. package/dist/resources/keys.d.ts +0 -56
  70. package/dist/resources/keys.d.ts.map +0 -1
  71. package/dist/resources/keys.js +0 -186
  72. package/dist/resources/keys.js.map +0 -1
  73. package/dist/resources/local.d.ts +0 -56
  74. package/dist/resources/local.d.ts.map +0 -1
  75. package/dist/resources/local.js +0 -163
  76. package/dist/resources/local.js.map +0 -1
  77. package/dist/resources/memory.d.ts +0 -249
  78. package/dist/resources/memory.d.ts.map +0 -1
  79. package/dist/resources/memory.js +0 -217
  80. package/dist/resources/memory.js.map +0 -1
  81. package/dist/resources/models.d.ts +0 -45
  82. package/dist/resources/models.d.ts.map +0 -1
  83. package/dist/resources/models.js +0 -101
  84. package/dist/resources/models.js.map +0 -1
  85. package/dist/resources/permissions.d.ts +0 -23
  86. package/dist/resources/permissions.d.ts.map +0 -1
  87. package/dist/resources/permissions.js +0 -69
  88. package/dist/resources/permissions.js.map +0 -1
  89. package/dist/resources/receipts.d.ts +0 -23
  90. package/dist/resources/receipts.d.ts.map +0 -1
  91. package/dist/resources/receipts.js +0 -48
  92. package/dist/resources/receipts.js.map +0 -1
  93. package/dist/resources/session-events.d.ts +0 -51
  94. package/dist/resources/session-events.d.ts.map +0 -1
  95. package/dist/resources/session-events.js +0 -404
  96. package/dist/resources/session-events.js.map +0 -1
  97. package/dist/resources/settlement.d.ts +0 -21
  98. package/dist/resources/settlement.d.ts.map +0 -1
  99. package/dist/resources/settlement.js +0 -30
  100. package/dist/resources/settlement.js.map +0 -1
  101. package/dist/resources/system.d.ts +0 -9
  102. package/dist/resources/system.d.ts.map +0 -1
  103. package/dist/resources/system.js +0 -19
  104. package/dist/resources/system.js.map +0 -1
  105. package/dist/resources/webhooks.d.ts +0 -27
  106. package/dist/resources/webhooks.d.ts.map +0 -1
  107. package/dist/resources/webhooks.js +0 -78
  108. package/dist/resources/webhooks.js.map +0 -1
  109. package/dist/resources/workflow.d.ts +0 -49
  110. package/dist/resources/workflow.d.ts.map +0 -1
  111. package/dist/resources/workflow.js +0 -364
  112. package/dist/resources/workflow.js.map +0 -1
  113. package/dist/resources/x402.d.ts +0 -113
  114. package/dist/resources/x402.d.ts.map +0 -1
  115. package/dist/resources/x402.js +0 -231
  116. package/dist/resources/x402.js.map +0 -1
  117. package/dist/tools/index.d.ts +0 -55
  118. package/dist/tools/index.d.ts.map +0 -1
  119. package/dist/tools/index.js +0 -51
  120. package/dist/tools/index.js.map +0 -1
  121. package/dist/types/index.d.ts +0 -1857
  122. package/dist/types/index.d.ts.map +0 -1
  123. package/dist/types/index.js +0 -14
  124. package/dist/types/index.js.map +0 -1
  125. package/dist/version.d.ts +0 -9
  126. package/dist/version.d.ts.map +0 -1
  127. package/dist/version.js +0 -9
  128. package/dist/version.js.map +0 -1
@@ -1,1049 +0,0 @@
1
- import { AuthenticationError, BadRequestError, Error, PaymentRequiredError } from "../errors.js";
2
- import { parseSSEStream } from "@compose-market/core/transport";
3
- import { parseReceiptEvent } from "@compose-market/core/sse/receipt";
4
- import { decode as decodeModelEvent } from "@compose-market/core/model";
5
- import { encodePaymentSignature } from "./x402.js";
6
- import { instrumentBillableResponse, } from "./instrumentation.js";
7
- export function buildCallHeaders(options, ctxWallet, ctxToken) {
8
- // When the caller explicitly passes `key: null`, force the raw x402
9
- // path by suppressing the instance-level token. Any other value (string or
10
- // undefined) falls back to the instance token.
11
- const tokenResolved = options?.paymentMode === "x402"
12
- ? null
13
- : options && "key" in options
14
- ? options.key
15
- : ctxToken;
16
- return {
17
- userAddress: options?.userAddress ?? ctxWallet.address ?? undefined,
18
- chainId: options?.chainId ?? ctxWallet.chainId ?? undefined,
19
- key: tokenResolved ?? undefined,
20
- paymentSignature: options?.paymentSignature ? encodePaymentSignature(options.paymentSignature) : undefined,
21
- x402MaxAmountWei: options?.x402MaxAmountWei,
22
- idempotencyKey: options?.idempotencyKey,
23
- runId: options?.runId,
24
- };
25
- }
26
- function resolvePaymentMode(options) {
27
- return options?.paymentMode ?? "auto";
28
- }
29
- function resolveX402Signer(ctx, options) {
30
- return options?.x402Signer ?? ctx.getX402SignerMaybe?.() ?? null;
31
- }
32
- async function buildX402RetryConfig(error, config, ctx, options) {
33
- if (!(error instanceof PaymentRequiredError) || !error.paymentRequired) {
34
- return null;
35
- }
36
- if (resolvePaymentMode(options) === "key") {
37
- return null;
38
- }
39
- const signer = resolveX402Signer(ctx, options);
40
- if (!signer) {
41
- return null;
42
- }
43
- const wallet = ctx.getWalletMaybe();
44
- const signed = await signer({
45
- paymentRequired: error.paymentRequired,
46
- paymentRequiredHeader: error.paymentRequiredHeader,
47
- method: config.method,
48
- path: config.path,
49
- url: error.paymentRequired.resource.url,
50
- body: config.body,
51
- userAddress: options?.userAddress ?? wallet.address,
52
- chainId: options?.chainId ?? wallet.chainId,
53
- maxAmountWei: options?.x402MaxAmountWei,
54
- });
55
- return {
56
- ...config,
57
- headers: {
58
- ...(config.headers ?? {}),
59
- key: null,
60
- paymentSignature: encodePaymentSignature(signed),
61
- },
62
- };
63
- }
64
- function shouldRetryInvalidKeyAsX402(error, ctx, options) {
65
- return error instanceof AuthenticationError
66
- && resolvePaymentMode(options) === "auto"
67
- && Boolean(ctx.getTokenMaybe())
68
- && Boolean(resolveX402Signer(ctx, options));
69
- }
70
- function suppressKey(config) {
71
- return {
72
- ...config,
73
- headers: {
74
- ...(config.headers ?? {}),
75
- key: null,
76
- paymentSignature: undefined,
77
- },
78
- };
79
- }
80
- async function requestJsonWithPayment(client, ctx, config, options) {
81
- try {
82
- return await client.request(config).withResponse();
83
- }
84
- catch (error) {
85
- const retryConfig = await buildX402RetryConfig(error, config, ctx, options);
86
- if (retryConfig)
87
- return client.request(retryConfig).withResponse();
88
- if (shouldRetryInvalidKeyAsX402(error, ctx, options)) {
89
- const challengeConfig = suppressKey(config);
90
- try {
91
- return await client.request(challengeConfig).withResponse();
92
- }
93
- catch (challengeError) {
94
- const signedRetryConfig = await buildX402RetryConfig(challengeError, challengeConfig, ctx, options);
95
- if (signedRetryConfig)
96
- return client.request(signedRetryConfig).withResponse();
97
- throw challengeError;
98
- }
99
- }
100
- throw error;
101
- }
102
- }
103
- export async function requestResponseWithPayment(client, ctx, config, options) {
104
- try {
105
- return await client.request(config).asResponse();
106
- }
107
- catch (error) {
108
- const retryConfig = await buildX402RetryConfig(error, config, ctx, options);
109
- if (retryConfig)
110
- return client.request(retryConfig).asResponse();
111
- if (shouldRetryInvalidKeyAsX402(error, ctx, options)) {
112
- const challengeConfig = suppressKey(config);
113
- try {
114
- return await client.request(challengeConfig).asResponse();
115
- }
116
- catch (challengeError) {
117
- const signedRetryConfig = await buildX402RetryConfig(challengeError, challengeConfig, ctx, options);
118
- if (signedRetryConfig)
119
- return client.request(signedRetryConfig).asResponse();
120
- throw challengeError;
121
- }
122
- }
123
- throw error;
124
- }
125
- }
126
- function isReceiptEvent(event) {
127
- return event === "receipt" || event === "compose.receipt";
128
- }
129
- /**
130
- * Read the response header / body pair produced by a billable call, extract
131
- * receipt + budget + invalid-reason, emit them on the SDK event bus, and
132
- * return the public `Completion<T>` shape.
133
- */
134
- function toCompletion(ctx, response, data) {
135
- const result = instrumentBillableResponse(ctx, response, data);
136
- return {
137
- data: result.data,
138
- receipt: result.receipt,
139
- requestId: result.requestId,
140
- response: result.response,
141
- budget: result.budget,
142
- sessionInvalidReason: result.sessionInvalidReason,
143
- };
144
- }
145
- /**
146
- * Emit budget / receipt / invalid events for a streaming response. The receipt
147
- * is attached optionally because streaming calls produce it via the in-band
148
- * `receipt` SSE frame, not necessarily via the header.
149
- */
150
- function emitStreamingEvents(ctx, response, receipt, requestId) {
151
- const walletCtx = ctx.getWalletMaybe();
152
- // Budget / session-invalid headers are set on the initial SSE response
153
- // (before the stream body), so extracting them from `response` is fine.
154
- const { extractSessionBudgetFromResponse } = budgetExtractor;
155
- const { budget, sessionInvalidReason } = extractSessionBudgetFromResponse(response);
156
- if (receipt) {
157
- ctx.events.emit("receipt", {
158
- userAddress: walletCtx.address,
159
- chainId: walletCtx.chainId,
160
- receipt,
161
- requestId,
162
- source: "stream",
163
- });
164
- }
165
- if (budget) {
166
- ctx.events.emit("budget", {
167
- userAddress: walletCtx.address,
168
- chainId: walletCtx.chainId,
169
- snapshot: budget,
170
- requestId,
171
- });
172
- }
173
- if (sessionInvalidReason) {
174
- ctx.events.emit("sessionInvalid", {
175
- userAddress: walletCtx.address,
176
- chainId: walletCtx.chainId,
177
- reason: sessionInvalidReason,
178
- requestId,
179
- });
180
- }
181
- return { budget, sessionInvalidReason };
182
- }
183
- // Lazy-loaded to avoid a circular import through instrumentation.ts -> budget.ts
184
- // (budget.ts is a pure leaf, so we just import it directly).
185
- import * as budgetExtractor from "@compose-market/core/sse/budget";
186
- function bytesToBase64(bytes) {
187
- if (typeof Buffer !== "undefined") {
188
- return Buffer.from(bytes).toString("base64");
189
- }
190
- let binary = "";
191
- const chunkSize = 0x8000;
192
- for (let offset = 0; offset < bytes.length; offset += chunkSize) {
193
- const chunk = bytes.subarray(offset, offset + chunkSize);
194
- binary += String.fromCharCode(...chunk);
195
- }
196
- return globalThis.btoa(binary);
197
- }
198
- function isByteArray(value) {
199
- return value instanceof Uint8Array
200
- || Boolean(value
201
- && typeof value === "object"
202
- && "byteLength" in value
203
- && "buffer" in value
204
- && typeof value.byteLength === "number");
205
- }
206
- async function encodeAudioFile(value) {
207
- if (typeof value === "string") {
208
- return value;
209
- }
210
- if (typeof Blob !== "undefined" && value instanceof Blob) {
211
- return bytesToBase64(new Uint8Array(await value.arrayBuffer()));
212
- }
213
- if (isByteArray(value)) {
214
- return bytesToBase64(value);
215
- }
216
- throw new BadRequestError({ message: "audio transcriptions: unsupported file type" });
217
- }
218
- /**
219
- * Async iterable returned by `inference.chat.completions.stream(...)` and
220
- * `inference.responses.stream(...)`. Exposes `.final()` for consumers that
221
- * want the reassembled final object + receipt without iterating manually.
222
- *
223
- * `for await (const chunk of stream) { ... }` consumes chunks; the generator's
224
- * return value is captured internally so a subsequent `await stream.final()`
225
- * resolves to the typed final object even when the caller drove iteration.
226
- */
227
- export class StreamIterator {
228
- iterator;
229
- finalResult = null;
230
- finalSettled = false;
231
- finalPromise = null;
232
- constructor(iterator) {
233
- this.iterator = iterator;
234
- }
235
- [Symbol.asyncIterator]() {
236
- const self = this;
237
- return {
238
- next: async () => {
239
- const r = await self.iterator.next();
240
- if (r.done) {
241
- self.finalResult = r.value;
242
- self.finalSettled = true;
243
- return { done: true, value: undefined };
244
- }
245
- return { done: false, value: r.value };
246
- },
247
- return: async () => {
248
- const r = await self.iterator.return(undefined);
249
- if (r.done) {
250
- self.finalResult = r.value;
251
- self.finalSettled = true;
252
- }
253
- return { done: true, value: undefined };
254
- },
255
- };
256
- }
257
- async final() {
258
- if (this.finalSettled)
259
- return this.finalResult;
260
- if (this.finalPromise)
261
- return this.finalPromise;
262
- this.finalPromise = (async () => {
263
- let last;
264
- do {
265
- last = await this.iterator.next();
266
- } while (!last.done);
267
- this.finalResult = last.value;
268
- this.finalSettled = true;
269
- return last.value;
270
- })();
271
- return this.finalPromise;
272
- }
273
- }
274
- // ---------------------------------------------------------------------------
275
- // Chat completions
276
- // ---------------------------------------------------------------------------
277
- function cleanse(params) {
278
- const { provider: _provider, ...body } = params;
279
- return body;
280
- }
281
- class ChatCompletionsNamespace {
282
- client;
283
- ctx;
284
- constructor(client, ctx) {
285
- this.client = client;
286
- this.ctx = ctx;
287
- }
288
- async create(params, options) {
289
- if (params.stream === true) {
290
- throw new BadRequestError({
291
- message: "Pass stream: true only to chat.completions.stream(). Use create() for non-streaming calls.",
292
- });
293
- }
294
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
295
- method: "POST",
296
- path: "/v1/chat/completions",
297
- body: { ...cleanse(params), stream: false },
298
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
299
- signal: options?.signal,
300
- timeoutMs: options?.timeoutMs,
301
- }, options);
302
- return toCompletion(this.ctx, response, data);
303
- }
304
- stream(params, options) {
305
- const iterator = streamChatCompletions(this.client, this.ctx, params, options);
306
- return new StreamIterator(iterator);
307
- }
308
- }
309
- async function* streamChatCompletions(client, ctx, params, options) {
310
- const response = await requestResponseWithPayment(client, ctx, {
311
- method: "POST",
312
- path: "/v1/chat/completions",
313
- body: { ...cleanse(params), stream: true },
314
- headers: buildCallHeaders(options, ctx.getWalletMaybe(), ctx.getTokenMaybe()),
315
- signal: options?.signal,
316
- timeoutMs: options?.timeoutMs,
317
- expectStream: true,
318
- }, options);
319
- if (!response.body) {
320
- throw new Error({
321
- code: "upstream_error",
322
- message: "Streaming response had no body",
323
- });
324
- }
325
- let receipt = null;
326
- const aggregator = buildChatCompletionAggregator(params.model);
327
- const emittedToolStarts = new Set();
328
- let streamError = null;
329
- let sawDone = false;
330
- try {
331
- for await (const frame of parseSSEStream(response.body, { signal: options?.signal })) {
332
- if (isReceiptEvent(frame.event)) {
333
- try {
334
- receipt = parseReceiptEvent(frame.data);
335
- }
336
- catch { /* ignore */ }
337
- continue;
338
- }
339
- if (frame.event === "compose.error") {
340
- try {
341
- const parsed = JSON.parse(frame.data);
342
- streamError = new Error({
343
- code: parsed.code ?? "upstream_error",
344
- message: parsed.message ?? "Stream error",
345
- details: parsed.details,
346
- });
347
- }
348
- catch { /* ignore */ }
349
- const decoded = decodeModelEvent(frame, { runId: options?.runId });
350
- if (decoded)
351
- yield decoded;
352
- continue;
353
- }
354
- if (frame.data === "[DONE]") {
355
- sawDone = true;
356
- continue;
357
- }
358
- if (sawDone)
359
- continue;
360
- if (frame.event !== "message") {
361
- // Unknown Compose-specific frames: surface nothing to the chunk stream.
362
- continue;
363
- }
364
- let parsed;
365
- try {
366
- parsed = JSON.parse(frame.data);
367
- }
368
- catch {
369
- continue;
370
- }
371
- aggregator.absorb(parsed);
372
- // Unified tool-call lifecycle events. The chat stream reports
373
- // tool_call deltas; each delta with an `id` signals a new call
374
- // (or continues an existing one). We emit toolCallStart on first
375
- // sight and toolCallEnd when the chunk's finish_reason is set.
376
- const walletCtx = ctx.getWalletMaybe();
377
- const requestId = response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id");
378
- const deltaToolCalls = parsed.choices?.[0]?.delta?.tool_calls;
379
- if (Array.isArray(deltaToolCalls)) {
380
- for (const toolCall of deltaToolCalls) {
381
- if (toolCall.id && !emittedToolStarts.has(toolCall.id)) {
382
- emittedToolStarts.add(toolCall.id);
383
- ctx.events.emit("toolCallStart", {
384
- userAddress: walletCtx.address,
385
- chainId: walletCtx.chainId,
386
- requestId,
387
- source: "chat",
388
- toolCallId: toolCall.id,
389
- toolName: toolCall.function?.name ?? "",
390
- arguments: toolCall.function?.arguments,
391
- });
392
- }
393
- }
394
- }
395
- const finishReason = parsed.choices?.[0]?.finish_reason;
396
- if (finishReason === "tool_calls") {
397
- const finalTools = aggregator.snapshotToolCalls();
398
- for (const tc of finalTools) {
399
- ctx.events.emit("toolCallEnd", {
400
- userAddress: walletCtx.address,
401
- chainId: walletCtx.chainId,
402
- requestId,
403
- source: "chat",
404
- toolCallId: tc.id,
405
- toolName: tc.function.name,
406
- arguments: tc.function.arguments,
407
- failed: false,
408
- });
409
- }
410
- }
411
- const decoded = decodeModelEvent(parsed, { runId: options?.runId, responseId: parsed.id, model: parsed.model });
412
- if (decoded)
413
- yield decoded;
414
- }
415
- if (streamError)
416
- throw streamError;
417
- const requestId = response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id");
418
- const { budget, sessionInvalidReason } = emitStreamingEvents(ctx, response, receipt, requestId);
419
- return {
420
- chatCompletion: aggregator.finalize(),
421
- receipt,
422
- requestId,
423
- budget,
424
- sessionInvalidReason,
425
- };
426
- }
427
- finally {
428
- try {
429
- response.body?.cancel();
430
- }
431
- catch { /* best-effort */ }
432
- }
433
- }
434
- function buildChatCompletionAggregator(modelHint) {
435
- let id = "";
436
- let created = 0;
437
- let model = modelHint;
438
- let role = undefined;
439
- let content = "";
440
- let reasoningContent = "";
441
- let finishReason = "stop";
442
- const toolCalls = new Map();
443
- let usage;
444
- return {
445
- absorb(chunk) {
446
- id = id || chunk.id;
447
- created = created || chunk.created;
448
- model = model || chunk.model;
449
- if (chunk.usage)
450
- usage = chunk.usage;
451
- const choice = chunk.choices?.[0];
452
- if (!choice)
453
- return;
454
- if (choice.finish_reason)
455
- finishReason = choice.finish_reason;
456
- const delta = choice.delta;
457
- if (delta?.role)
458
- role = delta.role;
459
- if (typeof delta?.content === "string")
460
- content += delta.content;
461
- if (typeof delta?.reasoning_content === "string")
462
- reasoningContent += delta.reasoning_content;
463
- if (Array.isArray(delta?.tool_calls)) {
464
- for (const toolCall of delta.tool_calls) {
465
- const idx = toolCall.index ?? 0;
466
- const existing = toolCalls.get(idx) ?? {
467
- id: toolCall.id ?? `call_${idx}`,
468
- type: "function",
469
- function: { name: "", arguments: "" },
470
- };
471
- if (toolCall.id)
472
- existing.id = toolCall.id;
473
- if (toolCall.function?.name)
474
- existing.function.name += toolCall.function.name;
475
- if (toolCall.function?.arguments)
476
- existing.function.arguments += toolCall.function.arguments;
477
- toolCalls.set(idx, existing);
478
- }
479
- }
480
- },
481
- snapshotToolCalls() {
482
- return Array.from(toolCalls.values());
483
- },
484
- finalize() {
485
- const message = {
486
- role: role ?? "assistant",
487
- content,
488
- };
489
- if (toolCalls.size > 0) {
490
- message.tool_calls = Array.from(toolCalls.values());
491
- }
492
- if (reasoningContent.length > 0) {
493
- message.reasoning_content = reasoningContent;
494
- }
495
- return {
496
- id,
497
- object: "chat.completion",
498
- created,
499
- model,
500
- choices: [
501
- {
502
- index: 0,
503
- message,
504
- finish_reason: finishReason,
505
- },
506
- ],
507
- usage: usage ?? { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
508
- };
509
- },
510
- };
511
- }
512
- // ---------------------------------------------------------------------------
513
- // Responses API
514
- // ---------------------------------------------------------------------------
515
- class ResponsesNamespace {
516
- client;
517
- ctx;
518
- constructor(client, ctx) {
519
- this.client = client;
520
- this.ctx = ctx;
521
- }
522
- async create(params, options) {
523
- if (params.stream === true) {
524
- throw new BadRequestError({
525
- message: "Pass stream: true only to responses.stream(). Use create() for non-streaming calls.",
526
- });
527
- }
528
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
529
- method: "POST",
530
- path: "/v1/responses",
531
- body: { ...cleanse(params), stream: false },
532
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
533
- signal: options?.signal,
534
- timeoutMs: options?.timeoutMs,
535
- }, options);
536
- return toCompletion(this.ctx, response, data);
537
- }
538
- stream(params, options) {
539
- const iterator = streamResponses(this.client, this.ctx, params, options);
540
- return new StreamIterator(iterator);
541
- }
542
- async get(responseId, options) {
543
- return this.client.request({
544
- method: "GET",
545
- path: `/v1/responses/${encodeURIComponent(responseId)}`,
546
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
547
- signal: options?.signal,
548
- timeoutMs: options?.timeoutMs,
549
- });
550
- }
551
- async inputItems(responseId, options) {
552
- return this.client.request({
553
- method: "GET",
554
- path: `/v1/responses/${encodeURIComponent(responseId)}/input_items`,
555
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
556
- signal: options?.signal,
557
- timeoutMs: options?.timeoutMs,
558
- });
559
- }
560
- async append(responseId, params, options) {
561
- const body = params && typeof params === "object" && !Array.isArray(params)
562
- ? params
563
- : { input: params };
564
- return this.client.request({
565
- method: "POST",
566
- path: `/v1/responses/${encodeURIComponent(responseId)}/input_items`,
567
- body,
568
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
569
- signal: options?.signal,
570
- timeoutMs: options?.timeoutMs,
571
- });
572
- }
573
- async cancel(responseId, options) {
574
- return this.client.request({
575
- method: "POST",
576
- path: `/v1/responses/${encodeURIComponent(responseId)}/cancel`,
577
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
578
- signal: options?.signal,
579
- timeoutMs: options?.timeoutMs,
580
- });
581
- }
582
- }
583
- async function* streamResponses(client, ctx, params, options) {
584
- const response = await requestResponseWithPayment(client, ctx, {
585
- method: "POST",
586
- path: "/v1/responses",
587
- body: { ...cleanse(params), stream: true },
588
- headers: buildCallHeaders(options, ctx.getWalletMaybe(), ctx.getTokenMaybe()),
589
- signal: options?.signal,
590
- timeoutMs: options?.timeoutMs,
591
- expectStream: true,
592
- }, options);
593
- if (!response.body) {
594
- throw new Error({ code: "upstream_error", message: "Streaming response had no body" });
595
- }
596
- let receipt = null;
597
- let lastCompleted = null;
598
- let streamError = null;
599
- let sawDone = false;
600
- let created = null;
601
- let textOutput = "";
602
- const outputItems = [];
603
- const toolCallAggregator = new Map();
604
- try {
605
- for await (const frame of parseSSEStream(response.body, { signal: options?.signal })) {
606
- if (isReceiptEvent(frame.event)) {
607
- try {
608
- receipt = parseReceiptEvent(frame.data);
609
- }
610
- catch { /* ignore */ }
611
- continue;
612
- }
613
- if (frame.event === "compose.error") {
614
- try {
615
- const parsed = JSON.parse(frame.data);
616
- streamError = new Error({
617
- code: parsed.code ?? "upstream_error",
618
- message: parsed.message ?? "Stream error",
619
- });
620
- }
621
- catch { /* ignore */ }
622
- const decoded = decodeModelEvent(frame, { runId: options?.runId });
623
- if (decoded)
624
- yield decoded;
625
- continue;
626
- }
627
- if (frame.data === "[DONE]") {
628
- sawDone = true;
629
- continue;
630
- }
631
- if (sawDone)
632
- continue;
633
- if (frame.event !== "message")
634
- continue;
635
- let parsed;
636
- try {
637
- parsed = JSON.parse(frame.data);
638
- }
639
- catch {
640
- continue;
641
- }
642
- if (parsed.type === "response.created") {
643
- created = parsed.response;
644
- }
645
- if (parsed.type === "response.output_text.delta") {
646
- textOutput += parsed.delta;
647
- }
648
- if (parsed.type === "response.image_generation_call.completed") {
649
- const url = `data:${parsed.mime_type || "image/png"};base64,${parsed.image_b64}`;
650
- outputItems.push({
651
- type: "output_image",
652
- role: "assistant",
653
- image_url: url,
654
- mime_type: parsed.mime_type || "image/png",
655
- ...(parsed.revised_prompt ? { text: parsed.revised_prompt } : {}),
656
- });
657
- }
658
- if (parsed.type === "response.output_item.completed") {
659
- outputItems[parsed.output_index] = parsed.item;
660
- }
661
- if (parsed.type === "response.output_video.status" && parsed.status === "completed" && parsed.url) {
662
- const existingIndex = outputItems.findIndex((item) => item.type === "output_video" && item.job_id === parsed.job_id);
663
- const item = {
664
- type: "output_video",
665
- role: "assistant",
666
- job_id: parsed.job_id,
667
- status: parsed.status,
668
- video_url: parsed.url,
669
- ...(typeof parsed.progress === "number" ? { progress: parsed.progress } : {}),
670
- };
671
- if (existingIndex >= 0)
672
- outputItems[existingIndex] = { ...outputItems[existingIndex], ...item };
673
- else
674
- outputItems.push(item);
675
- }
676
- // Assemble tool_call + tool_call.delta frames so the final result
677
- // carries ready-to-consume `{id,name,arguments}` entries.
678
- if (parsed.type === "response.tool_call") {
679
- const tc = parsed.tool_call;
680
- if (tc && tc.id) {
681
- const existing = toolCallAggregator.get(tc.id) ?? {
682
- id: tc.id,
683
- name: tc.name ?? "",
684
- arguments: tc.arguments ?? "",
685
- index: toolCallAggregator.size,
686
- };
687
- if (tc.name)
688
- existing.name = tc.name;
689
- if (tc.arguments)
690
- existing.arguments = tc.arguments;
691
- toolCallAggregator.set(tc.id, existing);
692
- const walletCtx = ctx.getWalletMaybe();
693
- ctx.events.emit("toolCallStart", {
694
- userAddress: walletCtx.address,
695
- chainId: walletCtx.chainId,
696
- requestId: response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id"),
697
- source: "responses",
698
- toolCallId: existing.id,
699
- toolName: existing.name,
700
- summary: existing.arguments,
701
- arguments: existing.arguments,
702
- });
703
- }
704
- }
705
- if (parsed.type === "response.tool_call.delta") {
706
- const delta = parsed.delta;
707
- if (delta) {
708
- const idx = parsed.index;
709
- const key = delta.id ?? String(idx);
710
- const existing = toolCallAggregator.get(key) ?? {
711
- id: delta.id ?? `call_${idx}`,
712
- name: "",
713
- arguments: "",
714
- index: idx,
715
- };
716
- if (delta.id)
717
- existing.id = delta.id;
718
- if (delta.name)
719
- existing.name += delta.name;
720
- if (delta.arguments)
721
- existing.arguments += delta.arguments;
722
- toolCallAggregator.set(key, existing);
723
- }
724
- }
725
- if (parsed.type === "response.completed") {
726
- lastCompleted = parsed;
727
- // Emit toolCallEnd for every aggregated tool call — responses
728
- // API doesn't have a per-tool end event, so the completion
729
- // frame is the natural terminal point.
730
- const walletCtx = ctx.getWalletMaybe();
731
- const requestId = response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id");
732
- for (const aggregated of toolCallAggregator.values()) {
733
- ctx.events.emit("toolCallEnd", {
734
- userAddress: walletCtx.address,
735
- chainId: walletCtx.chainId,
736
- requestId,
737
- source: "responses",
738
- toolCallId: aggregated.id,
739
- toolName: aggregated.name,
740
- summary: aggregated.arguments,
741
- arguments: aggregated.arguments,
742
- failed: false,
743
- });
744
- }
745
- }
746
- const responseId = "response_id" in parsed ? parsed.response_id : "response" in parsed ? parsed.response.id : undefined;
747
- const model = "model" in parsed ? parsed.model : "response" in parsed ? parsed.response.model : undefined;
748
- const decoded = decodeModelEvent(parsed, { runId: options?.runId, responseId, model });
749
- if (decoded)
750
- yield decoded;
751
- }
752
- if (streamError)
753
- throw streamError;
754
- const compactOutput = outputItems.filter((item) => Boolean(item));
755
- if (textOutput && !compactOutput.some((item) => item.type === "output_text")) {
756
- compactOutput.unshift({ type: "output_text", role: "assistant", text: textOutput });
757
- }
758
- const finalResponse = lastCompleted
759
- ? {
760
- id: lastCompleted.response_id,
761
- object: "response",
762
- created_at: created?.created_at ?? Math.floor(Date.now() / 1000),
763
- status: "completed",
764
- model: lastCompleted.model,
765
- output: compactOutput,
766
- ...(lastCompleted.usage
767
- ? {
768
- usage: {
769
- input_tokens: lastCompleted.usage.input_tokens,
770
- output_tokens: lastCompleted.usage.output_tokens,
771
- total_tokens: lastCompleted.usage.total_tokens,
772
- },
773
- }
774
- : {}),
775
- }
776
- : created
777
- ? { ...created, output: compactOutput }
778
- : null;
779
- const requestId = response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id");
780
- const { budget, sessionInvalidReason } = emitStreamingEvents(ctx, response, receipt, requestId);
781
- const toolCalls = Array.from(toolCallAggregator.values())
782
- .sort((a, b) => a.index - b.index)
783
- .map(({ id, name, arguments: args }) => ({ id, name, arguments: args }));
784
- return {
785
- response: finalResponse,
786
- toolCalls,
787
- receipt,
788
- requestId,
789
- budget,
790
- sessionInvalidReason,
791
- };
792
- }
793
- finally {
794
- try {
795
- response.body?.cancel();
796
- }
797
- catch { /* best-effort */ }
798
- }
799
- }
800
- // ---------------------------------------------------------------------------
801
- // Embeddings
802
- // ---------------------------------------------------------------------------
803
- class EmbeddingsNamespace {
804
- client;
805
- ctx;
806
- constructor(client, ctx) {
807
- this.client = client;
808
- this.ctx = ctx;
809
- }
810
- async create(params, options) {
811
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
812
- method: "POST",
813
- path: "/v1/embeddings",
814
- body: cleanse(params),
815
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
816
- signal: options?.signal,
817
- timeoutMs: options?.timeoutMs,
818
- }, options);
819
- return toCompletion(this.ctx, response, data);
820
- }
821
- }
822
- // ---------------------------------------------------------------------------
823
- // Images
824
- // ---------------------------------------------------------------------------
825
- class ImagesNamespace {
826
- client;
827
- ctx;
828
- constructor(client, ctx) {
829
- this.client = client;
830
- this.ctx = ctx;
831
- }
832
- async generate(params, options) {
833
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
834
- method: "POST",
835
- path: "/v1/images/generations",
836
- body: cleanse(params),
837
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
838
- signal: options?.signal,
839
- timeoutMs: options?.timeoutMs,
840
- }, options);
841
- return toCompletion(this.ctx, response, data);
842
- }
843
- async edit(params, options) {
844
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
845
- method: "POST",
846
- path: "/v1/images/edits",
847
- body: cleanse(params),
848
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
849
- signal: options?.signal,
850
- timeoutMs: options?.timeoutMs,
851
- }, options);
852
- return toCompletion(this.ctx, response, data);
853
- }
854
- }
855
- // ---------------------------------------------------------------------------
856
- // Audio
857
- // ---------------------------------------------------------------------------
858
- class AudioNamespace {
859
- client;
860
- ctx;
861
- constructor(client, ctx) {
862
- this.client = client;
863
- this.ctx = ctx;
864
- }
865
- /**
866
- * Text-to-speech. Returns the raw audio `Response` (stream the body via
867
- * `response.body` or `await response.arrayBuffer()`), plus any receipt
868
- * extracted from response headers, plus the live budget snapshot.
869
- */
870
- async speech(params, options) {
871
- const response = await requestResponseWithPayment(this.client, this.ctx, {
872
- method: "POST",
873
- path: "/v1/audio/speech",
874
- body: cleanse(params),
875
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
876
- signal: options?.signal,
877
- timeoutMs: options?.timeoutMs,
878
- expectStream: true,
879
- }, options);
880
- // No JSON body to mirror; receipt comes from the header only.
881
- const result = instrumentBillableResponse(this.ctx, response, undefined);
882
- return {
883
- response,
884
- receipt: result.receipt,
885
- requestId: result.requestId,
886
- budget: result.budget,
887
- sessionInvalidReason: result.sessionInvalidReason,
888
- };
889
- }
890
- /**
891
- * Speech-to-text. The native API accepts JSON with `file` as base64 audio.
892
- * Blob, File, and Uint8Array inputs are converted locally before sending.
893
- */
894
- async transcriptions(params, options) {
895
- const { file, provider: _provider, ...rest } = params;
896
- const body = {
897
- ...rest,
898
- file: await encodeAudioFile(file),
899
- };
900
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
901
- method: "POST",
902
- path: "/v1/audio/transcriptions",
903
- body,
904
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
905
- signal: options?.signal,
906
- timeoutMs: options?.timeoutMs,
907
- }, options);
908
- return toCompletion(this.ctx, response, data);
909
- }
910
- }
911
- // ---------------------------------------------------------------------------
912
- // Videos
913
- // ---------------------------------------------------------------------------
914
- class VideosNamespace {
915
- client;
916
- ctx;
917
- constructor(client, ctx) {
918
- this.client = client;
919
- this.ctx = ctx;
920
- }
921
- async generate(params, options) {
922
- const { data, response } = await requestJsonWithPayment(this.client, this.ctx, {
923
- method: "POST",
924
- path: "/v1/videos/generations",
925
- body: cleanse(params),
926
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
927
- signal: options?.signal,
928
- timeoutMs: options?.timeoutMs,
929
- }, options);
930
- return toCompletion(this.ctx, response, data);
931
- }
932
- get(videoId, options) {
933
- return Promise.resolve(this.client.request({
934
- method: "GET",
935
- path: `/v1/videos/${encodeURIComponent(videoId)}`,
936
- headers: buildCallHeaders(options, this.ctx.getWalletMaybe(), this.ctx.getTokenMaybe()),
937
- signal: options?.signal,
938
- timeoutMs: options?.timeoutMs,
939
- }));
940
- }
941
- /**
942
- * Server-driven polling via SSE. Subscribes to `/v1/videos/:id/stream`
943
- * and yields each status update as it arrives, terminating on the
944
- * `completed` or `failed` state.
945
- */
946
- stream(videoId, opts = {}) {
947
- const iterator = streamVideoStatus(this.client, this.ctx, videoId, opts);
948
- return new StreamIterator(iterator);
949
- }
950
- /**
951
- * Convenience helper: resolves once the video job hits a terminal state,
952
- * driven by the server-side SSE poller. Aborts after `timeoutMs` elapses.
953
- */
954
- async waitUntilDone(videoId, opts = {}) {
955
- const stream = this.stream(videoId, opts);
956
- for await (const event of stream) {
957
- opts.onStatus?.(event);
958
- }
959
- const { final } = await stream.final();
960
- return final;
961
- }
962
- }
963
- async function* streamVideoStatus(client, ctx, videoId, opts) {
964
- const query = {};
965
- if (opts.pollIntervalMs !== undefined)
966
- query.pollIntervalMs = String(opts.pollIntervalMs);
967
- if (opts.timeoutMs !== undefined)
968
- query.timeoutMs = String(opts.timeoutMs);
969
- const response = await client.request({
970
- method: "GET",
971
- path: `/v1/videos/${encodeURIComponent(videoId)}/stream`,
972
- query,
973
- headers: buildCallHeaders(opts, ctx.getWalletMaybe(), ctx.getTokenMaybe()),
974
- signal: opts.signal,
975
- timeoutMs: opts.timeoutMs,
976
- expectStream: true,
977
- }).asResponse();
978
- if (!response.body) {
979
- throw new Error({ code: "upstream_error", message: "Streaming response had no body" });
980
- }
981
- let lastStatus = null;
982
- let emittedDone = false;
983
- try {
984
- for await (const frame of parseSSEStream(response.body, { signal: opts.signal })) {
985
- if (frame.data === "[DONE]") {
986
- if (!emittedDone) {
987
- emittedDone = true;
988
- }
989
- continue;
990
- }
991
- if (emittedDone && frame.event !== "compose.error")
992
- continue;
993
- if (frame.event === "compose.video.status") {
994
- try {
995
- const parsed = JSON.parse(frame.data);
996
- lastStatus = {
997
- id: parsed.jobId,
998
- object: "video.generation",
999
- status: parsed.status,
1000
- url: parsed.url,
1001
- error: parsed.error,
1002
- progress: parsed.progress,
1003
- };
1004
- const decoded = decodeModelEvent({ type: "compose.video.status", ...parsed }, { runId: opts.runId, responseId: videoId });
1005
- if (decoded)
1006
- yield decoded;
1007
- }
1008
- catch { /* skip malformed frame */ }
1009
- continue;
1010
- }
1011
- if (frame.event === "compose.error") {
1012
- const decoded = decodeModelEvent(frame, { runId: opts.runId, responseId: videoId });
1013
- if (decoded)
1014
- yield decoded;
1015
- continue;
1016
- }
1017
- }
1018
- return {
1019
- final: lastStatus,
1020
- requestId: response.headers.get("x-request-id") ?? response.headers.get("X-Request-Id"),
1021
- };
1022
- }
1023
- finally {
1024
- try {
1025
- response.body?.cancel();
1026
- }
1027
- catch { /* best-effort */ }
1028
- }
1029
- }
1030
- // ---------------------------------------------------------------------------
1031
- // Top-level inference resource
1032
- // ---------------------------------------------------------------------------
1033
- export class InferenceResource {
1034
- chat;
1035
- responses;
1036
- embeddings;
1037
- images;
1038
- audio;
1039
- videos;
1040
- constructor(client, ctx) {
1041
- this.chat = { completions: new ChatCompletionsNamespace(client, ctx) };
1042
- this.responses = new ResponsesNamespace(client, ctx);
1043
- this.embeddings = new EmbeddingsNamespace(client, ctx);
1044
- this.images = new ImagesNamespace(client, ctx);
1045
- this.audio = new AudioNamespace(client, ctx);
1046
- this.videos = new VideosNamespace(client, ctx);
1047
- }
1048
- }
1049
- //# sourceMappingURL=inference.js.map