feat(server): extract check params (#12187)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Improved input validation and error reporting for chat messages, attachments, and embeddings, with clearer error messages for invalid inputs.
  - Enhanced support for multimodal messages, including attachments such as images or audio.

- **Refactor**
  - Unified and streamlined parameter validation across AI providers, resulting in more consistent behavior and error handling.
  - Centralized parameter checks into a common provider layer, removing duplicate validation code from individual AI providers.

- **Tests**
  - Simplified and consolidated audio transcription test stubs for better maintainability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
darkskygit
2025-05-22 13:43:59 +00:00
parent 5035ab218d
commit 477250f1b8
8 changed files with 114 additions and 206 deletions
@@ -5,17 +5,12 @@ import {
import { generateText, streamText } from 'ai';
import { z } from 'zod';
import {
CopilotPromptInvalid,
CopilotProviderSideError,
metrics,
} from '../../../base';
import { CopilotProviderSideError, metrics } from '../../../base';
import { CopilotProvider } from './provider';
import {
CopilotChatOptions,
CopilotProviderType,
ModelConditions,
ModelFullConditions,
ModelInputType,
ModelOutputType,
PromptMessage,
@@ -115,7 +110,7 @@ export class PerplexityProvider extends CopilotProvider<PerplexityConfig> {
options: CopilotChatOptions = {}
): Promise<string> {
const fullCond = { ...cond, outputType: ModelOutputType.Text };
await this.checkParams({ cond: fullCond, messages });
await this.checkParams({ cond: fullCond, messages, options });
const model = this.selectModel(fullCond);
try {
@@ -155,7 +150,7 @@ export class PerplexityProvider extends CopilotProvider<PerplexityConfig> {
options: CopilotChatOptions = {}
): AsyncIterable<string> {
const fullCond = { ...cond, outputType: ModelOutputType.Text };
await this.checkParams({ cond: fullCond, messages });
await this.checkParams({ cond: fullCond, messages, options });
const model = this.selectModel(fullCond);
try {
@@ -215,19 +210,6 @@ export class PerplexityProvider extends CopilotProvider<PerplexityConfig> {
}
}
protected async checkParams({
cond,
}: {
cond: ModelFullConditions;
messages?: PromptMessage[];
embeddings?: string[];
options?: CopilotChatOptions;
}) {
if (!(await this.match(cond))) {
throw new CopilotPromptInvalid(`Invalid model: ${cond.modelId}`);
}
}
private convertError(e: PerplexityError) {
function getErrMessage(e: PerplexityError) {
let err = 'Unexpected perplexity response';