linshenkx/prompt-optimizer · error · TestError

At least one message is required

Error message

At least one message is required

What it means

testCustomConversationStream requires at least one entry in request.messages; an empty or missing array throws TestError 'At least one message is required'. The custom conversation test replays a message list, so an empty list has nothing to send.

Solutions

  1. Ensure at least one message (typically the user's draft) is appended before invoking the test
  2. Guard in the UI: disable Test when the conversation list is empty
  3. Construct the request only after pushing the current draft message

Example fix

// before
await service.testCustomConversationStream({ modelKey, messages: [] }, handlers);

// after
const messages = [...conversation, { role: 'user', content: draft }];
if (messages.length === 0) return;
await service.testCustomConversationStream({ modelKey, messages }, handlers);
Defensive patterns

Strategy: validation

Validate before calling

const messages = [...conversation];
if (messages.length === 0) throw new Error('Add at least one message before testing');

Type guard

const hasMessages = (
  r: { messages?: unknown[] }
): r is { messages: unknown[] } =>
  Array.isArray(r.messages) && r.messages.length > 0;

Try / catch

try {
  await svc.testCustomConversationStream(req, handlers);
} catch (e) {
  if (e instanceof TestError && e.message === 'At least one message is required') {
    // notify user to write a message first
  } else throw e;
}

Prevention

When it happens

Trigger: Calling testCustomConversationStream with messages: [], messages: undefined, or a messages array that was filtered down to zero entries.

Common situations: Chat editor state cleared before Test clicked; message draft not yet appended to the conversation array; serialization dropping empty-ish message collections.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/4e14601dfca90868. Report an issue: GitHub.

Appendix: source

Thrown at packages/core/src/services/prompt/service.ts:1113

  // 3. 版本管理逻辑在UI层更容易处理
  //
  // 相比之下,优化操作会创建新的链,所以可以在核心层处理
  // 这种混合架构是经过权衡的设计决策

  /**
   * 自定义会话测试(流式)- 高级模式功能
   */
  async testCustomConversationStream(
    request: CustomConversationRequest,
    callbacks: StreamHandlers,
  ): Promise<void> {
    try {
      // 验证请求
      if (!request.modelKey?.trim()) {
        throw new TestError("", "", "Model key is required");
      }
      if (!request.messages || request.messages.length === 0) {
        throw new TestError("", "", "At least one message is required");
      }

      // 验证模型存在
      const modelConfig = await this.modelManager.getModel(request.modelKey);
      if (!modelConfig) {
        throw new TestError("", "", "Model not found");
      }

      // 处理会话消息:替换变量
      const processedMessages = TemplateProcessor.processConversationMessages(
        request.messages,
        request.variables,
      );

      if (processedMessages.length === 0) {
        throw new TestError("", "", "No valid messages after processing");
      }

View on GitHub (pinned to 3e677b1d9f)