@ssweens/pi-vertex 1.0.0 → 1.0.1
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/package.json +1 -1
- package/streaming/maas.ts +21 -1
package/package.json
CHANGED
package/streaming/maas.ts
CHANGED
|
@@ -17,6 +17,7 @@ export function streamMaaS(
|
|
|
17
17
|
const stream = createAssistantMessageEventStream();
|
|
18
18
|
|
|
19
19
|
(async () => {
|
|
20
|
+
const originalFetch = globalThis.fetch;
|
|
20
21
|
try {
|
|
21
22
|
// Priority: config file > env var > model region > default
|
|
22
23
|
const location = resolveLocation(model.region);
|
|
@@ -27,8 +28,10 @@ export function streamMaaS(
|
|
|
27
28
|
const endpoint = `${baseUrl}/endpoints/openapi`;
|
|
28
29
|
// Create a model object compatible with pi-ai's OpenAI streaming.
|
|
29
30
|
// Note: baseUrl must point to the OpenAPI root; pi-ai appends /chat/completions.
|
|
31
|
+
// Use model.id (registered name like "glm-5") so pi can restore sessions correctly.
|
|
32
|
+
// The actual API model name (apiId like "zai-org/glm-5-maas") is injected via fetch interceptor below.
|
|
30
33
|
const modelForPi: Model<"openai-completions"> = {
|
|
31
|
-
id: model.
|
|
34
|
+
id: model.id,
|
|
32
35
|
name: model.name,
|
|
33
36
|
api: "openai-completions",
|
|
34
37
|
provider: "vertex",
|
|
@@ -48,6 +51,21 @@ export function streamMaaS(
|
|
|
48
51
|
},
|
|
49
52
|
};
|
|
50
53
|
|
|
54
|
+
// Intercept fetch to replace model.id with the actual API model name (apiId)
|
|
55
|
+
// pi-ai's streaming uses model.id in the request body, but Vertex MaaS needs the full publisher-prefixed name
|
|
56
|
+
globalThis.fetch = async (input: any, init?: any) => {
|
|
57
|
+
if (init?.body && typeof init.body === "string") {
|
|
58
|
+
try {
|
|
59
|
+
const body = JSON.parse(init.body);
|
|
60
|
+
if (body.model === model.id) {
|
|
61
|
+
body.model = model.apiId;
|
|
62
|
+
init = { ...init, body: JSON.stringify(body) };
|
|
63
|
+
}
|
|
64
|
+
} catch {}
|
|
65
|
+
}
|
|
66
|
+
return originalFetch(input, init);
|
|
67
|
+
};
|
|
68
|
+
|
|
51
69
|
// Delegate to pi-ai's built-in OpenAI streaming
|
|
52
70
|
const innerStream = streamSimpleOpenAICompletions(
|
|
53
71
|
modelForPi,
|
|
@@ -64,9 +82,11 @@ export function streamMaaS(
|
|
|
64
82
|
for await (const event of innerStream) {
|
|
65
83
|
stream.push(event);
|
|
66
84
|
}
|
|
85
|
+
globalThis.fetch = originalFetch;
|
|
67
86
|
stream.end();
|
|
68
87
|
|
|
69
88
|
} catch (error) {
|
|
89
|
+
globalThis.fetch = originalFetch;
|
|
70
90
|
stream.push({
|
|
71
91
|
type: "error",
|
|
72
92
|
reason: options?.signal?.aborted ? "aborted" : "error",
|