@ynhcj/xiaoyi-channel 0.0.60-next → 0.0.61-next
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/src/provider.js +26 -3
- package/package.json +1 -1
package/dist/src/provider.js
CHANGED
|
@@ -23,8 +23,29 @@ function isRetryableProviderError(message) {
|
|
|
23
23
|
return true;
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
|
+
/** Check if the request is triggered by a cron job by inspecting the first user message. */
|
|
27
|
+
function isCronTriggered(messages) {
|
|
28
|
+
if (!messages)
|
|
29
|
+
return false;
|
|
30
|
+
const firstUser = messages.find(m => m.role === "user");
|
|
31
|
+
if (!firstUser)
|
|
32
|
+
return false;
|
|
33
|
+
let text = "";
|
|
34
|
+
if (typeof firstUser.content === "string") {
|
|
35
|
+
text = firstUser.content;
|
|
36
|
+
}
|
|
37
|
+
else if (Array.isArray(firstUser.content)) {
|
|
38
|
+
const block = firstUser.content.find(b => b.type === "text" && typeof b.text === "string");
|
|
39
|
+
if (block)
|
|
40
|
+
text = block.text;
|
|
41
|
+
}
|
|
42
|
+
return /^\[cron:/i.test(text.trim());
|
|
43
|
+
}
|
|
26
44
|
/** Compute retry delay in ms for the given 1-based attempt, with up to 10s jitter. */
|
|
27
|
-
function getRetryDelayMs(attempt) {
|
|
45
|
+
function getRetryDelayMs(attempt, isCron = false) {
|
|
46
|
+
if (isCron) {
|
|
47
|
+
return 60_000 + Math.floor(Math.random() * 10_000);
|
|
48
|
+
}
|
|
28
49
|
const base = attempt <= RETRY_DELAYS_MS.length
|
|
29
50
|
? RETRY_DELAYS_MS[attempt - 1]
|
|
30
51
|
: RETRY_DELAYS_MS[RETRY_DELAYS_MS.length - 1];
|
|
@@ -196,6 +217,9 @@ export const xiaoyiProvider = {
|
|
|
196
217
|
context.systemPrompt = (context.systemPrompt ?? "") + deviceSection;
|
|
197
218
|
}
|
|
198
219
|
// ── Retry loop ─────────────────────────────────────────
|
|
220
|
+
const cronJob = isCronTriggered(context.messages);
|
|
221
|
+
if (cronJob)
|
|
222
|
+
console.log("[xiaoyiprovider] detected cron-triggered request, using extended retry delays");
|
|
199
223
|
for (let attempt = 0; attempt < MAX_RETRY_ATTEMPTS; attempt++) {
|
|
200
224
|
const stream = await underlying(model, context, {
|
|
201
225
|
...options,
|
|
@@ -207,10 +231,9 @@ export const xiaoyiProvider = {
|
|
|
207
231
|
// Wait for the stream to settle (done or error) to inspect the result.
|
|
208
232
|
// stream.result() resolves to the final AssistantMessage (even on error).
|
|
209
233
|
const result = await stream.result();
|
|
210
|
-
console.log("[provider] stream result:", result);
|
|
211
234
|
// Check if this is a retryable error
|
|
212
235
|
if (result.stopReason === "error" && isRetryableProviderError(result.errorMessage)) {
|
|
213
|
-
const delayMs = getRetryDelayMs(attempt);
|
|
236
|
+
const delayMs = getRetryDelayMs(attempt + 1, cronJob);
|
|
214
237
|
console.log(`[xiaoyiprovider] retryable error (attempt ${attempt + 1}/${MAX_RETRY_ATTEMPTS}): ` +
|
|
215
238
|
`${result.errorMessage} — retrying in ${delayMs}ms`);
|
|
216
239
|
await sleep(delayMs);
|