@serii84/vertex-partner-provider 1.0.6 ā 1.0.8
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/index.js +79 -2
- package/package.json +1 -1
- package/test.js +0 -78
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Vertex Partner Provider for OpenCode
|
|
3
|
-
*
|
|
3
|
+
* Transforms streaming responses to handle reasoning_content
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const { createOpenAICompatible } = require('@ai-sdk/openai-compatible');
|
|
@@ -20,6 +20,74 @@ async function getAuthToken(googleAuthOptions) {
|
|
|
20
20
|
return token.token;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Transform SSE stream to handle reasoning_content
|
|
25
|
+
* Converts reasoning_content to content so SDK can parse it
|
|
26
|
+
*/
|
|
27
|
+
function transformStream(response) {
|
|
28
|
+
const reader = response.body.getReader();
|
|
29
|
+
const decoder = new TextDecoder();
|
|
30
|
+
const encoder = new TextEncoder();
|
|
31
|
+
|
|
32
|
+
let buffer = '';
|
|
33
|
+
|
|
34
|
+
const transformedStream = new ReadableStream({
|
|
35
|
+
async pull(controller) {
|
|
36
|
+
const { done, value } = await reader.read();
|
|
37
|
+
|
|
38
|
+
if (done) {
|
|
39
|
+
controller.close();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
buffer += decoder.decode(value, { stream: true });
|
|
44
|
+
const lines = buffer.split('\n');
|
|
45
|
+
buffer = lines.pop() || ''; // Keep incomplete line in buffer
|
|
46
|
+
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
if (line.startsWith('data: ')) {
|
|
49
|
+
const data = line.slice(6);
|
|
50
|
+
if (data === '[DONE]') {
|
|
51
|
+
controller.enqueue(encoder.encode(line + '\n'));
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const parsed = JSON.parse(data);
|
|
57
|
+
|
|
58
|
+
// Transform: move reasoning_content to content if content is null
|
|
59
|
+
if (parsed.choices) {
|
|
60
|
+
for (const choice of parsed.choices) {
|
|
61
|
+
if (choice.delta) {
|
|
62
|
+
// If content is null but reasoning_content exists, use reasoning_content
|
|
63
|
+
if (choice.delta.content === null && choice.delta.reasoning_content) {
|
|
64
|
+
choice.delta.content = choice.delta.reasoning_content;
|
|
65
|
+
}
|
|
66
|
+
// Remove reasoning_content to avoid confusion
|
|
67
|
+
delete choice.delta.reasoning_content;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
controller.enqueue(encoder.encode('data: ' + JSON.stringify(parsed) + '\n'));
|
|
73
|
+
} catch (e) {
|
|
74
|
+
// If JSON parse fails, pass through unchanged
|
|
75
|
+
controller.enqueue(encoder.encode(line + '\n'));
|
|
76
|
+
}
|
|
77
|
+
} else if (line.trim()) {
|
|
78
|
+
controller.enqueue(encoder.encode(line + '\n'));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return new Response(transformedStream, {
|
|
85
|
+
headers: response.headers,
|
|
86
|
+
status: response.status,
|
|
87
|
+
statusText: response.statusText,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
23
91
|
function createVertexPartner(options = {}) {
|
|
24
92
|
const {
|
|
25
93
|
project = process.env.GOOGLE_VERTEX_PROJECT,
|
|
@@ -41,7 +109,16 @@ function createVertexPartner(options = {}) {
|
|
|
41
109
|
const token = await getAuthToken(googleAuthOptions);
|
|
42
110
|
const headers = new Headers(init?.headers);
|
|
43
111
|
headers.set('Authorization', `Bearer ${token}`);
|
|
44
|
-
|
|
112
|
+
|
|
113
|
+
const response = await fetch(url, { ...init, headers });
|
|
114
|
+
|
|
115
|
+
// Check if this is a streaming request
|
|
116
|
+
const contentType = response.headers.get('content-type') || '';
|
|
117
|
+
if (contentType.includes('text/event-stream')) {
|
|
118
|
+
return transformStream(response);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return response;
|
|
45
122
|
};
|
|
46
123
|
|
|
47
124
|
const provider = createOpenAICompatible({
|
package/package.json
CHANGED
package/test.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Test script for Vertex Partner Provider
|
|
3
|
-
*
|
|
4
|
-
* Run with: node test.js
|
|
5
|
-
*
|
|
6
|
-
* Requires: gcloud auth application-default login
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const { createGLM, createDeepSeek, createKimi } = require('./index.js');
|
|
10
|
-
const { generateText, tool } = require('ai');
|
|
11
|
-
const { z } = require('zod');
|
|
12
|
-
|
|
13
|
-
const PROJECT = process.env.GOOGLE_VERTEX_PROJECT || 'noter-1c2a1';
|
|
14
|
-
|
|
15
|
-
async function testBasicChat() {
|
|
16
|
-
console.log('\nš Test 1: Basic Chat');
|
|
17
|
-
console.log('-'.repeat(40));
|
|
18
|
-
|
|
19
|
-
const glm = createGLM({ project: PROJECT, location: 'global' });
|
|
20
|
-
const model = glm('glm-4.7-maas');
|
|
21
|
-
|
|
22
|
-
console.log('Model ID:', model.modelId);
|
|
23
|
-
|
|
24
|
-
const result = await generateText({
|
|
25
|
-
model,
|
|
26
|
-
prompt: 'Say "Hello from GLM!" and nothing else.',
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
console.log('ā
Response:', result.text);
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async function testToolCalling() {
|
|
34
|
-
console.log('\nš§ Test 2: Tool Calling');
|
|
35
|
-
console.log('-'.repeat(40));
|
|
36
|
-
|
|
37
|
-
const glm = createGLM({ project: PROJECT, location: 'global' });
|
|
38
|
-
|
|
39
|
-
const result = await generateText({
|
|
40
|
-
model: glm('glm-4.7-maas'),
|
|
41
|
-
prompt: 'What is the weather in San Francisco? Use the weather tool.',
|
|
42
|
-
tools: {
|
|
43
|
-
weather: tool({
|
|
44
|
-
description: 'Get the weather for a location',
|
|
45
|
-
parameters: z.object({
|
|
46
|
-
location: z.string().describe('The city name'),
|
|
47
|
-
}),
|
|
48
|
-
execute: async ({ location }) => {
|
|
49
|
-
return { location, temperature: 72, condition: 'sunny' };
|
|
50
|
-
},
|
|
51
|
-
}),
|
|
52
|
-
},
|
|
53
|
-
maxSteps: 3,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
console.log('ā
Response:', result.text);
|
|
57
|
-
console.log('Tool calls:', result.toolCalls?.length || 0);
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function main() {
|
|
62
|
-
console.log('='.repeat(50));
|
|
63
|
-
console.log('Vertex Partner Provider Test Suite');
|
|
64
|
-
console.log('='.repeat(50));
|
|
65
|
-
console.log(`Project: ${PROJECT}`);
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
await testBasicChat();
|
|
69
|
-
await testToolCalling();
|
|
70
|
-
console.log('\nā
All tests passed!');
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error('\nā Test failed:', error.message);
|
|
73
|
-
if (error.cause) console.error('Cause:', JSON.stringify(error.cause, null, 2));
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
main();
|