Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: Remove redundant payload remapping in client-fetch #5267

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/services/_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ export const getProviderAuthPayload = (provider: string) => {
const apiKey = (awsSecretAccessKey || '') + (awsAccessKeyId || '');

return {
accessKeyId,
accessKeySecret: awsSecretAccessKey,
apiKey,
awsAccessKeyId,
awsRegion: region,
awsSecretAccessKey,
awsSessionToken: sessionToken,
region,
sessionToken,
};
}

Expand Down
81 changes: 8 additions & 73 deletions src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,90 +82,25 @@ interface CreateAssistantMessageStream extends FetchSSEOptions {
* **Note**: if you try to fetch directly, use `fetchOnClient` instead.
*/
export function initializeWithClientStore(provider: string, payload: any) {
// add auth payload
/**
* Since PR #5250, we no longer need to map the provider options here.
* Provider options mapping is now be done at `getProviderAuthPayload`.
* @see {@link https://github.com/lobehub/lobe-chat/pull/5250}
* @see {@file src/auth/provider.ts#getProviderAuthPayload}
*/
const providerAuthPayload = getProviderAuthPayload(provider);
const commonOptions = {
// Some provider base openai sdk, so enable it run on browser
// Allow OpenAI SDK to run on browser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不仅仅是 OpenAI SDK,Anthropic 的 SDK 也有这个配置的

dangerouslyAllowBrowser: true,
};
let providerOptions = {};

switch (provider) {
default:
case ModelProvider.OpenAI: {
providerOptions = {
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Azure: {
providerOptions = {
apiKey: providerAuthPayload?.apiKey,
apiVersion: providerAuthPayload?.azureApiVersion,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apiVersion 应该也要透传吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改 b4ab60c

};
break;
}
case ModelProvider.Google: {
providerOptions = {
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Bedrock: {
if (providerAuthPayload?.apiKey) {
providerOptions = {
accessKeyId: providerAuthPayload?.awsAccessKeyId,
accessKeySecret: providerAuthPayload?.awsSecretAccessKey,
region: providerAuthPayload?.awsRegion,
sessionToken: providerAuthPayload?.awsSessionToken,
};
}
break;
}
case ModelProvider.Ollama: {
providerOptions = {
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Perplexity: {
providerOptions = {
apikey: providerAuthPayload?.apiKey,
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Anthropic: {
providerOptions = {
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Groq: {
providerOptions = {
apikey: providerAuthPayload?.apiKey,
baseURL: providerAuthPayload?.baseURL,
};
break;
}
case ModelProvider.Cloudflare: {
providerOptions = {
apikey: providerAuthPayload?.apiKey,
baseURLOrAccountID: providerAuthPayload?.cloudflareBaseURLOrAccountID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有 baseURLOrAccountID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改 b4ab60c

};
break;
}
}

/**
* Configuration override order:
* payload -> providerOptions -> providerAuthPayload -> commonOptions
* payload -> providerAuthPayload -> commonOptions
*/
return AgentRuntime.initializeWithProviderOptions(provider, {
[provider]: {
...commonOptions,
...providerAuthPayload,
...providerOptions,
...payload,
},
});
Expand Down
Loading