Files
open-gsio/packages/ai/providers/google.ts
geoffsee c6e09644e2 **Refactor:** Restructure server package to streamline imports and improve file organization
- Moved `providers`, `services`, `models`, `lib`, and related files to `src` directory within `server` package.
- Adjusted imports across the codebase to reflect the new paths.
- Renamed several `.ts` files for consistency.
- Introduced an `index.ts` in the `ai/providers` package to export all providers.

This improves maintainability and aligns with the project's updated directory structure.
2025-06-24 20:46:15 -04:00

73 lines
2.0 KiB
TypeScript

import { type StreamParams } from '@open-gsio/server/src/services/ChatService';
import { OpenAI } from 'openai';
import { ProviderRepository } from './_ProviderRepository.ts';
import { BaseChatProvider, type CommonProviderParams } from './chat-stream-provider.ts';
export class GoogleChatProvider extends BaseChatProvider {
getOpenAIClient(param: CommonProviderParams): OpenAI {
return new OpenAI({
baseURL: ProviderRepository.OPENAI_COMPAT_ENDPOINTS.google,
apiKey: param.env.GEMINI_API_KEY,
});
}
getStreamParams(param: CommonProviderParams, safeMessages: any[]): any {
return {
model: param.model,
messages: safeMessages,
stream: true,
};
}
async processChunk(chunk: any, dataCallback: (data: any) => void): Promise<boolean> {
if (chunk.choices?.[0]?.finish_reason === 'stop') {
dataCallback({
type: 'chat',
data: {
choices: [
{
delta: { content: chunk.choices[0].delta.content || '' },
finish_reason: 'stop',
index: chunk.choices[0].index,
},
],
},
});
return true;
} else {
dataCallback({
type: 'chat',
data: {
choices: [
{
delta: { content: chunk.choices?.[0]?.delta?.content || '' },
finish_reason: null,
index: chunk.choices?.[0]?.index || 0,
},
],
},
});
return false;
}
}
}
export class GoogleChatSdk {
private static provider = new GoogleChatProvider();
static async handleGoogleStream(param: StreamParams, dataCallback: (data: any) => void) {
return this.provider.handleStream(
{
systemPrompt: param.systemPrompt,
preprocessedContext: param.preprocessedContext,
maxTokens: param.maxTokens,
messages: param.messages,
model: param.model,
env: param.env,
},
dataCallback,
);
}
}