@ynhcj/xiaoyi-channel 0.0.56-next → 0.0.57-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 +33 -16
- package/package.json +1 -1
package/dist/src/provider.js
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
// models.providers.xiaoyiprovider.api = "openai-completions"
|
|
9
9
|
// models.providers.xiaoyiprovider.models = [...]
|
|
10
10
|
import { createHash } from "crypto";
|
|
11
|
-
import { createAssistantMessageEventStream } from "@mariozechner/pi-ai";
|
|
12
11
|
import { getCurrentSessionContext } from "./tools/session-manager.js";
|
|
13
12
|
// ── Retry config ──────────────────────────────────────────────
|
|
14
13
|
const RETRY_DELAYS_MS = [10_000, 20_000, 40_000, 60_000];
|
|
@@ -36,6 +35,36 @@ function getRetryDelayMs(attempt) {
|
|
|
36
35
|
function sleep(ms) {
|
|
37
36
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
38
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Build a minimal EventStream-compatible object that replays a single
|
|
40
|
+
* done/error event. This avoids importing @mariozechner/pi-ai at runtime
|
|
41
|
+
* (the package is not available in the extension sandbox).
|
|
42
|
+
*/
|
|
43
|
+
function buildReplayStream(result) {
|
|
44
|
+
let settled = false;
|
|
45
|
+
const queued = [
|
|
46
|
+
result.stopReason === "error"
|
|
47
|
+
? { type: "error", reason: "error", error: result }
|
|
48
|
+
: { type: "done", reason: result.stopReason, message: result },
|
|
49
|
+
];
|
|
50
|
+
return {
|
|
51
|
+
result: () => Promise.resolve(result),
|
|
52
|
+
push: () => { },
|
|
53
|
+
end: () => { },
|
|
54
|
+
[Symbol.asyncIterator]: () => {
|
|
55
|
+
return {
|
|
56
|
+
next: async () => {
|
|
57
|
+
if (settled || queued.length === 0) {
|
|
58
|
+
settled = true;
|
|
59
|
+
return { value: undefined, done: true };
|
|
60
|
+
}
|
|
61
|
+
settled = true;
|
|
62
|
+
return { value: queued.shift(), done: false };
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
39
68
|
/**
|
|
40
69
|
* Dynamic header keys injected via extraParams and forwarded to the HTTP request.
|
|
41
70
|
* Correspond to the three fields written to .xiaoyiruntime:
|
|
@@ -195,17 +224,8 @@ export const xiaoyiProvider = {
|
|
|
195
224
|
console.log(`[xiaoyiprovider] stream completed, usage: input=${result.usage?.input} output=${result.usage?.output}`);
|
|
196
225
|
}
|
|
197
226
|
// The original stream has already been consumed by result().
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
// Re-emit events from the result as a single done/error event
|
|
201
|
-
if (result.stopReason === "error") {
|
|
202
|
-
replayStream.push({ type: "error", reason: "error", error: result });
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
replayStream.push({ type: "done", reason: result.stopReason, message: result });
|
|
206
|
-
}
|
|
207
|
-
replayStream.end();
|
|
208
|
-
return replayStream;
|
|
227
|
+
// Build a replay stream that delivers the final result.
|
|
228
|
+
return buildReplayStream(result);
|
|
209
229
|
}
|
|
210
230
|
// All retries exhausted — return the last attempt's real error via a new stream
|
|
211
231
|
console.log(`[xiaoyiprovider] all ${MAX_RETRY_ATTEMPTS} retries exhausted, surfacing last error`);
|
|
@@ -217,10 +237,7 @@ export const xiaoyiProvider = {
|
|
|
217
237
|
},
|
|
218
238
|
});
|
|
219
239
|
const lastResult = await lastStream.result();
|
|
220
|
-
|
|
221
|
-
exhaustedStream.push({ type: "error", reason: "error", error: lastResult });
|
|
222
|
-
exhaustedStream.end();
|
|
223
|
-
return exhaustedStream;
|
|
240
|
+
return buildReplayStream(lastResult);
|
|
224
241
|
};
|
|
225
242
|
},
|
|
226
243
|
};
|