@thorprovider/medusa-extended 1.11.3 → 1.11.5
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/dist/index.js +49 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/dropshipper.ts +58 -8
package/dist/index.js
CHANGED
|
@@ -501,7 +501,10 @@ var DropshipperClient = class {
|
|
|
501
501
|
};
|
|
502
502
|
let response;
|
|
503
503
|
try {
|
|
504
|
-
response = await fetchBackendWithResilience(url, init
|
|
504
|
+
response = await fetchBackendWithResilience(url, init, {
|
|
505
|
+
timeoutMs: 6e4,
|
|
506
|
+
maxRetries: 1
|
|
507
|
+
});
|
|
505
508
|
} catch (networkError) {
|
|
506
509
|
throw new import_adapters3.ProviderAPIError(
|
|
507
510
|
`[dropshipper] Network error on POST /admin/thor/dropshipper/ai/chat: ${networkError.message}`,
|
|
@@ -511,20 +514,36 @@ var DropshipperClient = class {
|
|
|
511
514
|
}
|
|
512
515
|
if (!response.ok) {
|
|
513
516
|
let message = `HTTP ${response.status}`;
|
|
517
|
+
let errorCode;
|
|
518
|
+
let requestId;
|
|
519
|
+
let details;
|
|
514
520
|
try {
|
|
515
521
|
const json = await response.json();
|
|
516
522
|
message = json?.message ?? message;
|
|
523
|
+
errorCode = json?.error_code ?? json?.type;
|
|
524
|
+
requestId = json?.request_id;
|
|
525
|
+
details = json?.details;
|
|
517
526
|
} catch {
|
|
518
527
|
}
|
|
528
|
+
this.logger.error(
|
|
529
|
+
`[dropshipper] POST /admin/thor/dropshipper/ai/chat failed status=${response.status} message=${message}` + (errorCode ? ` error_code=${errorCode}` : "") + (requestId ? ` request_id=${requestId}` : "") + (details ? ` details=${details}` : "")
|
|
530
|
+
);
|
|
519
531
|
throw new import_adapters3.ProviderAPIError(
|
|
520
532
|
`[dropshipper] POST /admin/thor/dropshipper/ai/chat failed: ${message}`,
|
|
521
533
|
`DROPSHIPPER_API_${response.status}`,
|
|
522
|
-
response.status
|
|
534
|
+
response.status,
|
|
535
|
+
{
|
|
536
|
+
message,
|
|
537
|
+
status: response.status,
|
|
538
|
+
errorCode,
|
|
539
|
+
requestId,
|
|
540
|
+
details
|
|
541
|
+
}
|
|
523
542
|
);
|
|
524
543
|
}
|
|
525
544
|
const contentType = response.headers.get("content-type") ?? "";
|
|
526
545
|
if (contentType.includes("text/event-stream") && response.body) {
|
|
527
|
-
return mapUiMessageStreamToEvents(response.body);
|
|
546
|
+
return mapUiMessageStreamToEvents(response.body, this.logger);
|
|
528
547
|
}
|
|
529
548
|
const data = await response.json();
|
|
530
549
|
return new ReadableStream({
|
|
@@ -1698,10 +1717,11 @@ var DropshipperClient = class {
|
|
|
1698
1717
|
);
|
|
1699
1718
|
}
|
|
1700
1719
|
};
|
|
1701
|
-
async function* parseSse(stream) {
|
|
1720
|
+
async function* parseSse(stream, logger) {
|
|
1702
1721
|
const reader = stream.getReader();
|
|
1703
1722
|
const decoder = new TextDecoder();
|
|
1704
1723
|
let buffer = "";
|
|
1724
|
+
let frameIndex = 0;
|
|
1705
1725
|
try {
|
|
1706
1726
|
while (true) {
|
|
1707
1727
|
const { done, value } = await reader.read();
|
|
@@ -1714,9 +1734,13 @@ async function* parseSse(stream) {
|
|
|
1714
1734
|
if (!trimmed.startsWith("data:")) continue;
|
|
1715
1735
|
const payload = trimmed.slice(5).trim();
|
|
1716
1736
|
if (!payload || payload === "[DONE]") continue;
|
|
1737
|
+
frameIndex += 1;
|
|
1717
1738
|
try {
|
|
1718
1739
|
yield JSON.parse(payload);
|
|
1719
|
-
} catch {
|
|
1740
|
+
} catch (err) {
|
|
1741
|
+
logger?.warn(
|
|
1742
|
+
`[dropshipper] sse-malformed-frame frame=${frameIndex} payload=${payload.slice(0, 200)}`
|
|
1743
|
+
);
|
|
1720
1744
|
}
|
|
1721
1745
|
}
|
|
1722
1746
|
}
|
|
@@ -1724,9 +1748,13 @@ async function* parseSse(stream) {
|
|
|
1724
1748
|
if (tail.startsWith("data:")) {
|
|
1725
1749
|
const payload = tail.slice(5).trim();
|
|
1726
1750
|
if (payload && payload !== "[DONE]") {
|
|
1751
|
+
frameIndex += 1;
|
|
1727
1752
|
try {
|
|
1728
1753
|
yield JSON.parse(payload);
|
|
1729
|
-
} catch {
|
|
1754
|
+
} catch (err) {
|
|
1755
|
+
logger?.warn(
|
|
1756
|
+
`[dropshipper] sse-malformed-frame frame=${frameIndex} payload=${payload.slice(0, 200)}`
|
|
1757
|
+
);
|
|
1730
1758
|
}
|
|
1731
1759
|
}
|
|
1732
1760
|
}
|
|
@@ -1734,10 +1762,11 @@ async function* parseSse(stream) {
|
|
|
1734
1762
|
reader.releaseLock();
|
|
1735
1763
|
}
|
|
1736
1764
|
}
|
|
1737
|
-
function mapUiMessageStreamToEvents(stream) {
|
|
1765
|
+
function mapUiMessageStreamToEvents(stream, logger) {
|
|
1738
1766
|
return new ReadableStream({
|
|
1739
1767
|
async start(controller) {
|
|
1740
|
-
|
|
1768
|
+
let eventCount = 0;
|
|
1769
|
+
for await (const raw of parseSse(stream, logger)) {
|
|
1741
1770
|
const chunk = raw;
|
|
1742
1771
|
switch (chunk.type) {
|
|
1743
1772
|
case "start":
|
|
@@ -1745,10 +1774,12 @@ function mapUiMessageStreamToEvents(stream) {
|
|
|
1745
1774
|
type: "start",
|
|
1746
1775
|
intent: chunk.messageMetadata?.intent
|
|
1747
1776
|
});
|
|
1777
|
+
eventCount += 1;
|
|
1748
1778
|
break;
|
|
1749
1779
|
case "text-delta":
|
|
1750
1780
|
if (chunk.delta !== void 0) {
|
|
1751
1781
|
controller.enqueue({ type: "text-delta", delta: chunk.delta });
|
|
1782
|
+
eventCount += 1;
|
|
1752
1783
|
}
|
|
1753
1784
|
break;
|
|
1754
1785
|
case "finish":
|
|
@@ -1757,15 +1788,25 @@ function mapUiMessageStreamToEvents(stream) {
|
|
|
1757
1788
|
intent: chunk.messageMetadata?.intent ?? "other",
|
|
1758
1789
|
sources: chunk.messageMetadata?.sources ?? []
|
|
1759
1790
|
});
|
|
1791
|
+
eventCount += 1;
|
|
1760
1792
|
break;
|
|
1761
1793
|
case "error":
|
|
1794
|
+
logger?.warn(
|
|
1795
|
+
`[dropshipper] chat-stream-error errorText=${chunk.errorText}`
|
|
1796
|
+
);
|
|
1762
1797
|
controller.enqueue({
|
|
1763
1798
|
type: "error",
|
|
1764
1799
|
errorText: chunk.errorText ?? "Error inesperado del asistente."
|
|
1765
1800
|
});
|
|
1801
|
+
eventCount += 1;
|
|
1766
1802
|
break;
|
|
1803
|
+
default:
|
|
1804
|
+
logger?.debug(
|
|
1805
|
+
`[dropshipper] chat-stream-unknown-frame type=${chunk.type}`
|
|
1806
|
+
);
|
|
1767
1807
|
}
|
|
1768
1808
|
}
|
|
1809
|
+
logger?.debug(`[dropshipper] chat-stream-completed events=${eventCount}`);
|
|
1769
1810
|
controller.close();
|
|
1770
1811
|
},
|
|
1771
1812
|
cancel() {
|