Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(review): fail a review whose reply was truncated or blocked (#21591)
h3n4l1b251a510eb5
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yamlindex a8053288e8..5b73c78429 100644--- a/backend/api/mcp/gen/openapi.yaml+++ b/backend/api/mcp/gen/openapi.yaml@@ -9079,9 +9079,25 @@ components:- type: "null"title: usagedescription: Token usage for this provider call, when available.+ stopReason:+ allOf:+ - $ref: '#/components/schemas/bytebase.v1.AIChatResponse.StopReason'+ title: stop_reason+ description: Why the model stopped. A truncated or blocked response can still carry content.title: AIChatResponseadditionalProperties: falsedescription: Response message for AIService.Chat.+ bytebase.v1.AIChatResponse.StopReason:+ type: string+ title: StopReason+ enum:+ - STOP_REASON_UNSPECIFIED+ - TURN_COMPLETED+ - TOKEN_LIMIT+ - BLOCKED+ - MALFORMED_TOOL_CALL+ - OTHER+ description: Why the model stopped generating a response.bytebase.v1.AIChatToolCall:type: objectproperties:diff --git a/backend/generated-go/v1/ai_service.pb.go b/backend/generated-go/v1/ai_service.pb.goindex 50492bd87a..32ff09df5f 100644--- a/backend/generated-go/v1/ai_service.pb.go+++ b/backend/generated-go/v1/ai_service.pb.go@@ -83,6 +83,71 @@ func (AIChatMessageRole) EnumDescriptor() ([]byte, []int) {return file_v1_ai_service_proto_rawDescGZIP(), []int{0}}+// Why the model stopped generating a response.+type AIChatResponse_StopReason int32++const (+ // The provider did not report why the model stopped.+ AIChatResponse_STOP_REASON_UNSPECIFIED AIChatResponse_StopReason = 0+ // The model completed its turn with an answer or with tool calls.+ AIChatResponse_TURN_COMPLETED AIChatResponse_StopReason = 1+ // The response was truncated at a token limit and is incomplete.+ AIChatResponse_TOKEN_LIMIT AIChatResponse_StopReason = 2+ // The provider blocked the request or the response, or the model refused to answer.+ AIChatResponse_BLOCKED AIChatResponse_StopReason = 3+ // The model generated a tool call that the provider could not parse.+ AIChatResponse_MALFORMED_TOOL_CALL AIChatResponse_StopReason = 4+ // The model stopped for a reason that no other value names.+ AIChatResponse_OTHER AIChatResponse_StopReason = 5+)++// Enum value maps for AIChatResponse_StopReason.+var (+ AIChatResponse_StopReason_name = map[int32]string{+ 0: "STOP_REASON_UNSPECIFIED",+ 1: "TURN_COMPLETED",+ 2: "TOKEN_LIMIT",+ 3: "BLOCKED",+ 4: "MALFORMED_TOOL_CALL",+ 5: "OTHER",+ }+ AIChatResponse_StopReason_value = map[string]int32{+ "STOP_REASON_UNSPECIFIED": 0,+ "TURN_COMPLETED": 1,+ "TOKEN_LIMIT": 2,+ "BLOCKED": 3,+ "MALFORMED_TOOL_CALL": 4,+ "OTHER": 5,+ }+)++func (x AIChatResponse_StopReason) Enum() *AIChatResponse_StopReason {+ p := new(AIChatResponse_StopReason)+ *p = x+ return p+}++func (x AIChatResponse_StopReason) String() string {+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))+}++func (AIChatResponse_StopReason) Descriptor() protoreflect.EnumDescriptor {+ return file_v1_ai_service_proto_enumTypes[1].Descriptor()+}++func (AIChatResponse_StopReason) Type() protoreflect.EnumType {+ return &file_v1_ai_service_proto_enumTypes[1]+}++func (x AIChatResponse_StopReason) Number() protoreflect.EnumNumber {+ return protoreflect.EnumNumber(x)+}++// Deprecated: Use AIChatResponse_StopReason.Descriptor instead.+func (AIChatResponse_StopReason) EnumDescriptor() ([]byte, []int) {+ return file_v1_ai_service_proto_rawDescGZIP(), []int{5, 0}+}+// A single message in the conversation.type AIChatMessage struct {state protoimpl.MessageState `protogen:"open.v1"`@@ -403,7 +468,9 @@ type AIChatResponse struct {// Tool calls the AI wants to make.ToolCalls []*AIChatToolCall `protobuf:"bytes,2,rep,name=tool_calls,json=toolCalls,proto3" json:"tool_calls,omitempty"`// Token usage for this provider call, when available.- Usage *AIChatUsage `protobuf:"bytes,3,opt,name=usage,proto3,oneof" json:"usage,omitempty"`+ Usage *AIChatUsage `protobuf:"bytes,3,opt,name=usage,proto3,oneof" json:"usage,omitempty"`+ // Why the model stopped. A truncated or blocked response can still carry content.+ StopReason AIChatResponse_StopReason `protobuf:"varint,4,opt,name=stop_reason,json=stopReason,proto3,enum=bytebase.v1.AIChatResponse_StopReason" json:"stop_reason,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}@@ -459,6 +526,13 @@ func (x *AIChatResponse) GetUsage() *AIChatUsage {return nil}+func (x *AIChatResponse) GetStopReason() AIChatResponse_StopReason {+ if x != nil {+ return x.StopReason+ }+ return AIChatResponse_STOP_REASON_UNSPECIFIED+}+var File_v1_ai_service_proto protoreflect.FileDescriptorconst file_v1_ai_service_proto_rawDesc = "" +@@ -488,12 +562,22 @@ const file_v1_ai_service_proto_rawDesc = "" +"\bmessages\x18\x01 \x03(\v2\x1a.bytebase.v1.AIChatMessageB\x04\xd0\xea0\x02R\bmessages\x12R\n" +"\x10tool_definitions\x18\x02 \x03(\v2!.bytebase.v1.AIChatToolDefinitionB\x04\xd0\xea0\x02R\x0ftoolDefinitions\"0\n" +"\vAIChatUsage\x12!\n" +- "\ftotal_tokens\x18\x01 \x01(\x05R\vtotalTokens\"\xb6\x01\n" ++ "\ftotal_tokens\x18\x01 \x01(\x05R\vtotalTokens\"\x80\x03\n" +"\x0eAIChatResponse\x12\x1d\n" +"\acontent\x18\x01 \x01(\tH\x00R\acontent\x88\x01\x01\x12:\n" +"\n" +"tool_calls\x18\x02 \x03(\v2\x1b.bytebase.v1.AIChatToolCallR\ttoolCalls\x123\n" +- "\x05usage\x18\x03 \x01(\v2\x18.bytebase.v1.AIChatUsageH\x01R\x05usage\x88\x01\x01B\n" ++ "\x05usage\x18\x03 \x01(\v2\x18.bytebase.v1.AIChatUsageH\x01R\x05usage\x88\x01\x01\x12G\n" ++ "\vstop_reason\x18\x04 \x01(\x0e2&.bytebase.v1.AIChatResponse.StopReasonR\n" ++ "stopReason\"\x7f\n" ++ "\n" ++ "StopReason\x12\x1b\n" ++ "\x17STOP_REASON_UNSPECIFIED\x10\x00\x12\x12\n" ++ "\x0eTURN_COMPLETED\x10\x01\x12\x0f\n" ++ "\vTOKEN_LIMIT\x10\x02\x12\v\n" ++ "\aBLOCKED\x10\x03\x12\x17\n" ++ "\x13MALFORMED_TOOL_CALL\x10\x04\x12\t\n" ++ "\x05OTHER\x10\x05B\n" +"\n" +"\b_contentB\b\n" +"\x06_usage*\xbc\x01\n" +@@ -519,31 +603,33 @@ func file_v1_ai_service_proto_rawDescGZIP() []byte {return file_v1_ai_service_proto_rawDescData}-var file_v1_ai_service_proto_enumTypes = make([]protoimpl.EnumInfo, 1)+var file_v1_ai_service_proto_enumTypes = make([]protoimpl.EnumInfo, 2)var file_v1_ai_service_proto_msgTypes = make([]protoimpl.MessageInfo, 6)var file_v1_ai_service_proto_goTypes = []any{- (AIChatMessageRole)(0), // 0: bytebase.v1.AIChatMessageRole- (*AIChatMessage)(nil), // 1: bytebase.v1.AIChatMessage- (*AIChatToolDefinition)(nil), // 2: bytebase.v1.AIChatToolDefinition- (*AIChatToolCall)(nil), // 3: bytebase.v1.AIChatToolCall- (*AIChatRequest)(nil), // 4: bytebase.v1.AIChatRequest- (*AIChatUsage)(nil), // 5: bytebase.v1.AIChatUsage- (*AIChatResponse)(nil), // 6: bytebase.v1.AIChatResponse+ (AIChatMessageRole)(0), // 0: bytebase.v1.AIChatMessageRole+ (AIChatResponse_StopReason)(0), // 1: bytebase.v1.AIChatResponse.StopReason+ (*AIChatMessage)(nil), // 2: bytebase.v1.AIChatMessage+ (*AIChatToolDefinition)(nil), // 3: bytebase.v1.AIChatToolDefinition+ (*AIChatToolCall)(nil), // 4: bytebase.v1.AIChatToolCall+ (*AIChatRequest)(nil), // 5: bytebase.v1.AIChatRequest+ (*AIChatUsage)(nil), // 6: bytebase.v1.AIChatUsage+ (*AIChatResponse)(nil), // 7: bytebase.v1.AIChatResponse}var file_v1_ai_service_proto_depIdxs = []int32{0, // 0: bytebase.v1.AIChatMessage.role:type_name -> bytebase.v1.AIChatMessageRole- 3, // 1: bytebase.v1.AIChatMessage.tool_calls:type_name -> bytebase.v1.AIChatToolCall- 1, // 2: bytebase.v1.AIChatRequest.messages:type_name -> bytebase.v1.AIChatMessage- 2, // 3: bytebase.v1.AIChatRequest.tool_definitions:type_name -> bytebase.v1.AIChatToolDefinition- 3, // 4: bytebase.v1.AIChatResponse.tool_calls:type_name -> bytebase.v1.AIChatToolCall- 5, // 5: bytebase.v1.AIChatResponse.usage:type_name -> bytebase.v1.AIChatUsage- 4, // 6: bytebase.v1.AIService.Chat:input_type -> bytebase.v1.AIChatRequest- 6, // 7: bytebase.v1.AIService.Chat:output_type -> bytebase.v1.AIChatResponse- 7, // [7:8] is the sub-list for method output_type- 6, // [6:7] is the sub-list for method input_type- 6, // [6:6] is the sub-list for extension type_name- 6, // [6:6] is the sub-list for extension extendee- 0, // [0:6] is the sub-list for field type_name+ 4, // 1: bytebase.v1.AIChatMessage.tool_calls:type_name -> bytebase.v1.AIChatToolCall+ 2, // 2: bytebase.v1.AIChatRequest.messages:type_name -> bytebase.v1.AIChatMessage+ 3, // 3: bytebase.v1.AIChatRequest.tool_definitions:type_name -> bytebase.v1.AIChatToolDefinition+ 4, // 4: bytebase.v1.AIChatResponse.tool_calls:type_name -> bytebase.v1.AIChatToolCall+ 6, // 5: bytebase.v1.AIChatResponse.usage:type_name -> bytebase.v1.AIChatUsage+ 1, // 6: bytebase.v1.AIChatResponse.stop_reason:type_name -> bytebase.v1.AIChatResponse.StopReason+ 5, // 7: bytebase.v1.AIService.Chat:input_type -> bytebase.v1.AIChatRequest+ 7, // 8: bytebase.v1.AIService.Chat:output_type -> bytebase.v1.AIChatResponse+ 8, // [8:9] is the sub-list for method output_type+ 7, // [7:8] is the sub-list for method input_type+ 7, // [7:7] is the sub-list for extension type_name+ 7, // [7:7] is the sub-list for extension extendee+ 0, // [0:7] is the sub-list for field type_name}func init() { file_v1_ai_service_proto_init() }@@ -560,7 +646,7 @@ func file_v1_ai_service_proto_init() {File: protoimpl.DescBuilder{GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: unsafe.Slice(unsafe.StringData(file_v1_ai_service_proto_rawDesc), len(file_v1_ai_service_proto_rawDesc)),- NumEnums: 1,+ NumEnums: 2,NumMessages: 6,NumExtensions: 0,NumServices: 1,diff --git a/backend/generated-go/v1/ai_service_equal.pb.go b/backend/generated-go/v1/ai_service_equal.pb.goindex d341601f49..0f2e458b2a 100644--- a/backend/generated-go/v1/ai_service_equal.pb.go+++ b/backend/generated-go/v1/ai_service_equal.pb.go@@ -131,5 +131,8 @@ func (x *AIChatResponse) Equal(y *AIChatResponse) bool {if !x.Usage.Equal(y.Usage) {return false}+ if x.StopReason != y.StopReason {+ return false+ }return true}diff --git a/backend/plugin/ai/claude.go b/backend/plugin/ai/claude.goindex 79583d5e4b..2ac748d5fe 100644--- a/backend/plugin/ai/claude.go+++ b/backend/plugin/ai/claude.go@@ -51,8 +51,9 @@ type chatClaudeUsage struct {}type chatClaudeResponse struct {- Content []json.RawMessage `json:"content"`- Usage *chatClaudeUsage `json:"usage,omitempty"`+ Content []json.RawMessage `json:"content"`+ StopReason string `json:"stop_reason"`+ Usage *chatClaudeUsage `json:"usage,omitempty"`}func claudeContentFromToolCalls(toolCalls []*v1pb.AIChatToolCall) []json.RawMessage {@@ -244,7 +245,7 @@ func chatClaude(ctx context.Context, aiSetting *storepb.AISetting, request *v1pbreturn nil, errors.Errorf("failed to unmarshal Claude response: %s", err)}- result := &v1pb.AIChatResponse{}+ result := &v1pb.AIChatResponse{StopReason: claudeStopReason(resp.StopReason)}if resp.Usage != nil {result.Usage = newAIChatUsage(resp.Usage.InputTokens + resp.Usage.OutputTokens)}diff --git a/backend/plugin/ai/gemini.go b/backend/plugin/ai/gemini.goindex 2a1098401e..b96f611211 100644--- a/backend/plugin/ai/gemini.go+++ b/backend/plugin/ai/gemini.go@@ -70,7 +70,11 @@ type chatGeminiResponse struct {Content struct {Parts []chatGeminiResponsePart `json:"parts"`} `json:"content"`+ FinishReason string `json:"finishReason"`} `json:"candidates"`+ PromptFeedback struct {+ BlockReason string `json:"blockReason"`+ } `json:"promptFeedback"`UsageMetadata *chatGeminiUsage `json:"usageMetadata,omitempty"`}@@ -128,6 +132,17 @@ func readGeminiFailure(httpErr *HTTPError) {}}+// appendGeminiContent adds parts as a turn of role. Gemini expects the roles+// to alternate, so parts with the role of the last turn join that turn, as the+// results of parallel function calls do in Google's examples.+func appendGeminiContent(contents []chatGeminiContent, role string, parts ...chatGeminiPart) []chatGeminiContent {+ if last := len(contents) - 1; last >= 0 && contents[last].Role == role {+ contents[last].Parts = append(contents[last].Parts, parts...)+ return contents+ }+ return append(contents, chatGeminiContent{Role: role, Parts: parts})+}+func chatGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb.AIChatRequest) (*v1pb.AIChatResponse, error) {payload := chatGeminiRequest{}@@ -139,10 +154,7 @@ func chatGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pbsystemParts = append(systemParts, chatGeminiPart{Text: m.GetContent()})}case v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER:- payload.Contents = append(payload.Contents, chatGeminiContent{- Role: "user",- Parts: []chatGeminiPart{{Text: m.GetContent()}},- })+ payload.Contents = appendGeminiContent(payload.Contents, "user", chatGeminiPart{Text: m.GetContent()})case v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_ASSISTANT:var parts []chatGeminiPartif m.Content != nil && *m.Content != "" {@@ -164,24 +176,16 @@ func chatGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pb}parts = append(parts, part)}- payload.Contents = append(payload.Contents, chatGeminiContent{- Role: "model",- Parts: parts,- })+ payload.Contents = appendGeminiContent(payload.Contents, "model", parts...)case v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_TOOL:// Gemini uses functionResponse parts in a "user" role message.// We need the tool name; get it from the tool call ID by searching prior messages.toolName := getToolNameFromMessages(request.Messages, m.GetToolCallId())resultData := geminiFunctionResponse(m.GetContent())- payload.Contents = append(payload.Contents, chatGeminiContent{- Role: "user",- Parts: []chatGeminiPart{- {- FunctionResponse: &chatGeminiFunctionResult{- Name: toolName,- Response: resultData,- },- },+ payload.Contents = appendGeminiContent(payload.Contents, "user", chatGeminiPart{+ FunctionResponse: &chatGeminiFunctionResult{+ Name: toolName,+ Response: resultData,},})default:@@ -243,6 +247,7 @@ func chatGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pbresult.Usage = newAIChatUsage(resp.UsageMetadata.TotalTokenCount)}if len(resp.Candidates) > 0 {+ result.StopReason = geminiStopReason(resp.Candidates[0].FinishReason)var textContent stringfor _, part := range resp.Candidates[0].Content.Parts {if part.Text != "" {@@ -271,6 +276,8 @@ func chatGemini(ctx context.Context, aiSetting *storepb.AISetting, request *v1pbif textContent != "" {result.Content = &textContent}+ } else {+ result.StopReason = geminiPromptBlockStopReason(resp.PromptFeedback.BlockReason)}return result, nil}diff --git a/backend/plugin/ai/gemini_contents_test.go b/backend/plugin/ai/gemini_contents_test.gonew file mode 100644index 0000000000..843a9928e5--- /dev/null+++ b/backend/plugin/ai/gemini_contents_test.go@@ -0,0 +1,99 @@+package ai++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++func sentGeminiRequest(t *testing.T, messages []*v1pb.AIChatMessage) chatGeminiRequest {+ t.Helper()++ var payload chatGeminiRequest+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))+ w.Header().Set("Content-Type", "application/json")+ _, err := w.Write([]byte(`{"candidates": [{"content": {"parts": [{"text": "done"}]}, "finishReason": "STOP"}]}`))+ require.NoError(t, err)+ }))+ defer server.Close()++ _, err := Chat(+ context.Background(),+ &storepb.AISetting{Provider: storepb.AISetting_GEMINI, Endpoint: server.URL, Model: "gemini-3.5-flash", ApiKey: "test-key"},+ &v1pb.AIChatRequest{Messages: messages},+ )+ require.NoError(t, err)+ return payload+}++func geminiRoles(contents []chatGeminiContent) []string {+ roles := make([]string, 0, len(contents))+ for _, content := range contents {+ roles = append(roles, content.Role)+ }+ return roles+}++func TestChatGeminiJoinsToolResultsAndTheTextAfterThemInOneUserTurn(t *testing.T) {+ t.Parallel()++ system, prompt, correction := "You review SQL.", "Review this change.", "Call the tool again."+ readID, searchID := "call-read", "call-search"+ readResult, searchResult := `{"rows": 7}`, `{"total": 2}`+ payload := sentGeminiRequest(t, []*v1pb.AIChatMessage{+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_SYSTEM, Content: &system},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, Content: &prompt},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_ASSISTANT, ToolCalls: []*v1pb.AIChatToolCall{+ {Id: readID, Name: "read", Arguments: `{"objects": [{"name": "orders"}]}`},+ {Id: searchID, Name: "search", Arguments: `{"text": "orders"}`},+ }},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_TOOL, Content: &readResult, ToolCallId: &readID},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_TOOL, Content: &searchResult, ToolCallId: &searchID},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, Content: &correction},+ })++ require.NotNil(t, payload.SystemInstruction)+ require.Len(t, payload.SystemInstruction.Parts, 1)+ require.Equal(t, system, payload.SystemInstruction.Parts[0].Text)++ require.Equal(t, []string{"user", "model", "user"}, geminiRoles(payload.Contents))+ require.Len(t, payload.Contents[0].Parts, 1)+ require.Equal(t, prompt, payload.Contents[0].Parts[0].Text)+ calls := payload.Contents[1].Parts+ require.Len(t, calls, 2)+ require.Equal(t, "read", calls[0].FunctionCall.Name)+ require.Equal(t, "search", calls[1].FunctionCall.Name)++ results := payload.Contents[2].Parts+ require.Len(t, results, 3)+ require.Equal(t, "read", results[0].FunctionResponse.Name)+ require.Equal(t, map[string]any{"rows": float64(7)}, results[0].FunctionResponse.Response)+ require.Equal(t, "search", results[1].FunctionResponse.Name)+ require.Equal(t, map[string]any{"total": float64(2)}, results[1].FunctionResponse.Response)+ require.Nil(t, results[2].FunctionResponse)+ require.Equal(t, correction, results[2].Text)+}++func TestChatGeminiJoinsATextAfterThePromptInOneUserTurn(t *testing.T) {+ t.Parallel()++ prompt, correction := "Review this change.", "Call the tool again."+ payload := sentGeminiRequest(t, []*v1pb.AIChatMessage{+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, Content: &prompt},+ {Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, Content: &correction},+ })++ require.Nil(t, payload.SystemInstruction)+ require.Equal(t, []string{"user"}, geminiRoles(payload.Contents))+ require.Len(t, payload.Contents[0].Parts, 2)+ require.Equal(t, prompt, payload.Contents[0].Parts[0].Text)+ require.Equal(t, correction, payload.Contents[0].Parts[1].Text)+}diff --git a/backend/plugin/ai/openai.go b/backend/plugin/ai/openai.goindex e08c45c419..2402c782d6 100644--- a/backend/plugin/ai/openai.go+++ b/backend/plugin/ai/openai.go@@ -62,8 +62,10 @@ type chatOpenAIResponse struct {Message struct {Role string `json:"role"`Content *string `json:"content"`+ Refusal string `json:"refusal"`ToolCalls []json.RawMessage `json:"tool_calls"`} `json:"message"`+ FinishReason string `json:"finish_reason"`} `json:"choices"`Usage *chatOpenAIUsage `json:"usage,omitempty"`}@@ -318,6 +320,7 @@ func chatOpenAIChatCompletions(ctx context.Context, aiSetting *storepb.AISetting}if len(resp.Choices) > 0 {msg := resp.Choices[0].Message+ result.StopReason = openAIChatCompletionsStopReason(resp.Choices[0].FinishReason, msg.Refusal)result.Content = msg.Contentfor _, rawTC := range msg.ToolCalls {var parsed chatOpenAIToolCallParsed@@ -442,7 +445,7 @@ func chatOpenAIResponses(ctx context.Context, aiSetting *storepb.AISetting, requreturn nil, errors.Errorf("OpenAI Responses API returned an incomplete response: %s", reason)}- result := &v1pb.AIChatResponse{}+ result := &v1pb.AIChatResponse{StopReason: openAIResponsesStopReason(response.Status)}if response.Usage != nil {result.Usage = newAIChatUsage(response.Usage.TotalTokens)}@@ -460,6 +463,7 @@ func chatOpenAIResponses(ctx context.Context, aiSetting *storepb.AISetting, requ_, _ = content.WriteString(part.Text)case "refusal":_, _ = content.WriteString(part.Refusal)+ result.StopReason = v1pb.AIChatResponse_BLOCKEDdefault:continue}diff --git a/backend/plugin/ai/stop_reason.go b/backend/plugin/ai/stop_reason.gonew file mode 100644index 0000000000..06ad9a8c4f--- /dev/null+++ b/backend/plugin/ai/stop_reason.go@@ -0,0 +1,96 @@+package ai++import (+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++// Each mapping sends a value its provider's API reference does not list to+// OTHER, never to TURN_COMPLETED: a caller trusts a completed turn, and nobody+// has read what an unlisted value means.++// openAIChatCompletionsStopReason maps choices[].finish_reason. A refusal+// arrives as message.refusal beside a finish_reason of stop.+func openAIChatCompletionsStopReason(finishReason string, refusal string) v1pb.AIChatResponse_StopReason {+ if refusal != "" {+ return v1pb.AIChatResponse_BLOCKED+ }+ switch finishReason {+ case "":+ return v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ case "stop", "tool_calls", "function_call":+ return v1pb.AIChatResponse_TURN_COMPLETED+ case "length":+ return v1pb.AIChatResponse_TOKEN_LIMIT+ case "content_filter":+ return v1pb.AIChatResponse_BLOCKED+ default:+ return v1pb.AIChatResponse_OTHER+ }+}++// openAIResponsesStopReason maps status. The caller returns an error for an+// incomplete response and checks for a refusal, which arrives as a content part.+func openAIResponsesStopReason(status string) v1pb.AIChatResponse_StopReason {+ switch status {+ case "":+ return v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ case "completed":+ return v1pb.AIChatResponse_TURN_COMPLETED+ default:+ // failed, cancelled, queued, and in_progress land here.+ return v1pb.AIChatResponse_OTHER+ }+}++// claudeStopReason maps stop_reason.+func claudeStopReason(stopReason string) v1pb.AIChatResponse_StopReason {+ switch stopReason {+ case "":+ return v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ case "end_turn", "stop_sequence", "tool_use":+ return v1pb.AIChatResponse_TURN_COMPLETED+ case "max_tokens", "model_context_window_exceeded":+ return v1pb.AIChatResponse_TOKEN_LIMIT+ case "refusal":+ return v1pb.AIChatResponse_BLOCKED+ default:+ // pause_turn lands here.+ return v1pb.AIChatResponse_OTHER+ }+}++// geminiStopReason maps candidates[].finishReason.+func geminiStopReason(finishReason string) v1pb.AIChatResponse_StopReason {+ switch finishReason {+ case "", "FINISH_REASON_UNSPECIFIED":+ return v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ case "STOP":+ return v1pb.AIChatResponse_TURN_COMPLETED+ case "MAX_TOKENS", "CONTINUATION":+ return v1pb.AIChatResponse_TOKEN_LIMIT+ case "SAFETY", "RECITATION", "LANGUAGE", "BLOCKLIST", "PROHIBITED_CONTENT", "SPII",+ "IMAGE_SAFETY", "IMAGE_PROHIBITED_CONTENT", "IMAGE_RECITATION",+ "ESCALATION", "PUP_LIMITED_DISABLED":+ return v1pb.AIChatResponse_BLOCKED+ case "MALFORMED_FUNCTION_CALL":+ return v1pb.AIChatResponse_MALFORMED_TOOL_CALL+ default:+ // OTHER, IMAGE_OTHER, NO_IMAGE, UNEXPECTED_TOOL_CALL, TOO_MANY_TOOL_CALLS,+ // MISSING_THOUGHT_SIGNATURE, and MALFORMED_RESPONSE land here.+ return v1pb.AIChatResponse_OTHER+ }+}++// geminiPromptBlockStopReason maps promptFeedback.blockReason, which Gemini+// sets when it blocked the prompt and returned no candidate. Its OTHER is a+// block for a reason Gemini does not name.+func geminiPromptBlockStopReason(blockReason string) v1pb.AIChatResponse_StopReason {+ switch blockReason {+ case "", "BLOCK_REASON_UNSPECIFIED":+ return v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ case "SAFETY", "OTHER", "BLOCKLIST", "PROHIBITED_CONTENT", "IMAGE_SAFETY":+ return v1pb.AIChatResponse_BLOCKED+ default:+ return v1pb.AIChatResponse_OTHER+ }+}diff --git a/backend/plugin/ai/stop_reason_test.go b/backend/plugin/ai/stop_reason_test.gonew file mode 100644index 0000000000..a0dfedcc48--- /dev/null+++ b/backend/plugin/ai/stop_reason_test.go@@ -0,0 +1,493 @@+package ai++import (+ "context"+ "fmt"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++const (+ stopUnspecified = v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED+ stopTurnCompleted = v1pb.AIChatResponse_TURN_COMPLETED+ stopTokenLimit = v1pb.AIChatResponse_TOKEN_LIMIT+ stopBlocked = v1pb.AIChatResponse_BLOCKED+ stopMalformedToolCall = v1pb.AIChatResponse_MALFORMED_TOOL_CALL+ stopOther = v1pb.AIChatResponse_OTHER+)++// chatWithScriptedReply sends one user message through Chat to a server that+// answers every request with reply. endpointPath selects the OpenAI wire format.+func chatWithScriptedReply(t *testing.T, provider storepb.AISetting_Provider, endpointPath string, reply string) (*v1pb.AIChatResponse, error) {+ t.Helper()++ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _, err := w.Write([]byte(reply))+ require.NoError(t, err)+ }))+ t.Cleanup(server.Close)++ content := "Review this change."+ return Chat(+ context.Background(),+ &storepb.AISetting{Provider: provider, Endpoint: server.URL + endpointPath, Model: "test-model", ApiKey: "test-key"},+ &v1pb.AIChatRequest{Messages: []*v1pb.AIChatMessage{{Role: v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, Content: &content}}},+ )+}++func TestOpenAIChatCompletionsStopReason(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ provider storepb.AISetting_Provider+ reply string+ want v1pb.AIChatResponse_StopReason+ wantContent string+ }{+ {+ name: "stop",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done"}, "finish_reason": "stop"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "tool_calls",+ reply: `{"choices": [{"message": {"role": "assistant", "content": null, "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "read", "arguments": "{}"}}]}, "finish_reason": "tool_calls"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "function_call",+ reply: `{"choices": [{"message": {"role": "assistant", "content": null}, "finish_reason": "function_call"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "length keeps the truncated text",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "{\"findings\": ["}, "finish_reason": "length"}]}`,+ want: stopTokenLimit,+ wantContent: `{"findings": [`,+ },+ {+ name: "length with tool calls",+ reply: `{"choices": [{"message": {"role": "assistant", "content": null, "tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "read", "arguments": "{\"objects\": ["}}]}, "finish_reason": "length"}]}`,+ want: stopTokenLimit,+ },+ {+ name: "content_filter",+ reply: `{"choices": [{"message": {"role": "assistant", "content": null}, "finish_reason": "content_filter"}]}`,+ want: stopBlocked,+ },+ {+ name: "content_filter from Azure OpenAI",+ provider: storepb.AISetting_AZURE_OPENAI,+ reply: `{"choices": [{"message": {"role": "assistant", "content": ""}, "finish_reason": "content_filter"}]}`,+ want: stopBlocked,+ },+ {+ name: "refusal beside a stop",+ reply: `{"choices": [{"message": {"role": "assistant", "content": null, "refusal": "I can't help with that request."}, "finish_reason": "stop"}]}`,+ want: stopBlocked,+ },+ {+ name: "null refusal is not a refusal",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done", "refusal": null}, "finish_reason": "stop"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "empty refusal is not a refusal",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done", "refusal": ""}, "finish_reason": "stop"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "null finish_reason",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done"}, "finish_reason": null}]}`,+ want: stopUnspecified,+ },+ {+ name: "no finish_reason",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done"}}]}`,+ want: stopUnspecified,+ },+ {+ name: "no choices",+ reply: `{"choices": []}`,+ want: stopUnspecified,+ },+ {+ name: "unknown finish_reason",+ reply: `{"choices": [{"message": {"role": "assistant", "content": "done"}, "finish_reason": "insufficient_system_resource"}]}`,+ want: stopOther,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ provider := tc.provider+ if provider == storepb.AISetting_PROVIDER_UNSPECIFIED {+ provider = storepb.AISetting_OPEN_AI+ }+ response, err := chatWithScriptedReply(t, provider, "/chat/completions", tc.reply)+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ if tc.wantContent != "" {+ require.Equal(t, tc.wantContent, response.GetContent())+ }+ })+ }+}++func TestOpenAIResponsesStopReason(t *testing.T) {+ t.Parallel()++ const textOutput = `"output": [{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "{\"findings\": ["}]}]`+ const refusalOutput = `"output": [{"type": "message", "role": "assistant", "content": [{"type": "refusal", "refusal": "I can't help with that request."}]}]`+ tests := []struct {+ name string+ reply string+ want v1pb.AIChatResponse_StopReason+ wantContent string+ wantErrText string+ }{+ {+ name: "completed",+ reply: `{"status": "completed", ` + textOutput + `}`,+ want: stopTurnCompleted,+ },+ {+ name: "completed with a function call",+ reply: `{"status": "completed", "output": [{"type": "function_call", "call_id": "call_1", "name": "read", "arguments": "{}"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "no status",+ reply: `{` + textOutput + `}`,+ want: stopUnspecified,+ },+ {+ name: "refusal part of a completed response keeps the refusal text",+ reply: `{"status": "completed", ` + refusalOutput + `}`,+ want: stopBlocked,+ wantContent: "I can't help with that request.",+ },+ {+ name: "refusal part without a status",+ reply: `{` + refusalOutput + `}`,+ want: stopBlocked,+ },+ {+ name: "failed without an error object",+ reply: `{"status": "failed", ` + textOutput + `}`,+ want: stopOther,+ },+ {+ name: "cancelled",+ reply: `{"status": "cancelled", ` + textOutput + `}`,+ want: stopOther,+ },+ {+ name: "queued",+ reply: `{"status": "queued", "output": []}`,+ want: stopOther,+ },+ {+ name: "in_progress",+ reply: `{"status": "in_progress", "output": []}`,+ want: stopOther,+ },+ {+ name: "unknown status",+ reply: `{"status": "expired", ` + textOutput + `}`,+ want: stopOther,+ },+ {+ name: "incomplete at max_output_tokens is an error",+ reply: `{"status": "incomplete", "incomplete_details": {"reason": "max_output_tokens"}, ` + textOutput + `}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: max_output_tokens",+ },+ {+ name: "incomplete for content_filter is an error",+ reply: `{"status": "incomplete", "incomplete_details": {"reason": "content_filter"}, "output": []}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: content_filter",+ },+ {+ name: "incomplete with a refusal part is an error",+ reply: `{"status": "incomplete", "incomplete_details": {"reason": "content_filter"}, ` + refusalOutput + `}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: content_filter",+ },+ {+ name: "incomplete at max_messages is an error",+ reply: `{"status": "incomplete", "incomplete_details": {"reason": "max_messages"}, ` + textOutput + `}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: max_messages",+ },+ {+ name: "incomplete when steered is an error",+ reply: `{"status": "incomplete", "incomplete_details": {"reason": "steered"}, ` + textOutput + `}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: steered",+ },+ {+ name: "incomplete without details is an error",+ reply: `{"status": "incomplete", ` + textOutput + `}`,+ wantErrText: "OpenAI Responses API returned an incomplete response: ",+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ response, err := chatWithScriptedReply(t, storepb.AISetting_OPEN_AI, "/responses", tc.reply)+ if tc.wantErrText != "" {+ require.ErrorContains(t, err, tc.wantErrText)+ require.Nil(t, response)+ return+ }+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ if tc.wantContent != "" {+ require.Equal(t, tc.wantContent, response.GetContent())+ }+ })+ }+}++func TestClaudeStopReason(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ reply string+ want v1pb.AIChatResponse_StopReason+ wantContent string+ }{+ {+ name: "end_turn",+ reply: `{"content": [{"type": "text", "text": "done"}], "stop_reason": "end_turn"}`,+ want: stopTurnCompleted,+ },+ {+ name: "stop_sequence",+ reply: `{"content": [{"type": "text", "text": "done"}], "stop_reason": "stop_sequence"}`,+ want: stopTurnCompleted,+ },+ {+ name: "tool_use",+ reply: `{"content": [{"type": "tool_use", "id": "toolu_1", "name": "read", "input": {}}], "stop_reason": "tool_use"}`,+ want: stopTurnCompleted,+ },+ {+ name: "max_tokens keeps the truncated text",+ reply: `{"content": [{"type": "text", "text": "{\"findings\": ["}], "stop_reason": "max_tokens"}`,+ want: stopTokenLimit,+ wantContent: `{"findings": [`,+ },+ {+ name: "max_tokens with a tool call",+ reply: `{"content": [{"type": "tool_use", "id": "toolu_1", "name": "read", "input": {}}], "stop_reason": "max_tokens"}`,+ want: stopTokenLimit,+ },+ {+ name: "model_context_window_exceeded",+ reply: `{"content": [{"type": "text", "text": "{\"findings\": ["}], "stop_reason": "model_context_window_exceeded"}`,+ want: stopTokenLimit,+ },+ {+ name: "refusal keeps the text before it",+ reply: `{"content": [{"type": "text", "text": "I can't help with that request."}], "stop_reason": "refusal", "stop_details": {"type": "refusal", "category": "cyber", "explanation": null}}`,+ want: stopBlocked,+ wantContent: "I can't help with that request.",+ },+ {+ name: "refusal without content",+ reply: `{"content": [], "stop_reason": "refusal"}`,+ want: stopBlocked,+ },+ {+ name: "pause_turn",+ reply: `{"content": [{"type": "text", "text": "done"}], "stop_reason": "pause_turn"}`,+ want: stopOther,+ },+ {+ name: "null stop_reason",+ reply: `{"content": [{"type": "text", "text": "done"}], "stop_reason": null}`,+ want: stopUnspecified,+ },+ {+ name: "no stop_reason",+ reply: `{"content": [{"type": "text", "text": "done"}]}`,+ want: stopUnspecified,+ },+ {+ name: "unknown stop_reason",+ reply: `{"content": [{"type": "text", "text": "done"}], "stop_reason": "compaction"}`,+ want: stopOther,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ response, err := chatWithScriptedReply(t, storepb.AISetting_CLAUDE, "", tc.reply)+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ if tc.wantContent != "" {+ require.Equal(t, tc.wantContent, response.GetContent())+ }+ })+ }+}++func TestGeminiStopReasonForEveryFinishReason(t *testing.T) {+ t.Parallel()++ tests := []struct {+ finishReason string+ want v1pb.AIChatResponse_StopReason+ }{+ {finishReason: "FINISH_REASON_UNSPECIFIED", want: stopUnspecified},+ {finishReason: "STOP", want: stopTurnCompleted},+ {finishReason: "MAX_TOKENS", want: stopTokenLimit},+ {finishReason: "CONTINUATION", want: stopTokenLimit},+ {finishReason: "SAFETY", want: stopBlocked},+ {finishReason: "RECITATION", want: stopBlocked},+ {finishReason: "LANGUAGE", want: stopBlocked},+ {finishReason: "BLOCKLIST", want: stopBlocked},+ {finishReason: "PROHIBITED_CONTENT", want: stopBlocked},+ {finishReason: "SPII", want: stopBlocked},+ {finishReason: "IMAGE_SAFETY", want: stopBlocked},+ {finishReason: "IMAGE_PROHIBITED_CONTENT", want: stopBlocked},+ {finishReason: "IMAGE_RECITATION", want: stopBlocked},+ {finishReason: "ESCALATION", want: stopBlocked},+ {finishReason: "PUP_LIMITED_DISABLED", want: stopBlocked},+ {finishReason: "MALFORMED_FUNCTION_CALL", want: stopMalformedToolCall},+ {finishReason: "OTHER", want: stopOther},+ {finishReason: "IMAGE_OTHER", want: stopOther},+ {finishReason: "NO_IMAGE", want: stopOther},+ {finishReason: "UNEXPECTED_TOOL_CALL", want: stopOther},+ {finishReason: "TOO_MANY_TOOL_CALLS", want: stopOther},+ {finishReason: "MISSING_THOUGHT_SIGNATURE", want: stopOther},+ {finishReason: "MALFORMED_RESPONSE", want: stopOther},+ {finishReason: "A_REASON_ADDED_LATER", want: stopOther},+ }+ for _, tc := range tests {+ t.Run(tc.finishReason, func(t *testing.T) {+ t.Parallel()++ reply := fmt.Sprintf(`{"candidates": [{"content": {"parts": [{"text": "{\"findings\": ["}]}, "finishReason": %q}]}`, tc.finishReason)+ response, err := chatWithScriptedReply(t, storepb.AISetting_GEMINI, "", reply)+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ require.Equal(t, `{"findings": [`, response.GetContent())+ })+ }+}++func TestGeminiStopReasonForCandidateShapes(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ reply string+ want v1pb.AIChatResponse_StopReason+ }{+ {+ name: "no finishReason",+ reply: `{"candidates": [{"content": {"parts": [{"text": "done"}]}}]}`,+ want: stopUnspecified,+ },+ {+ name: "function call that stops with STOP",+ reply: `{"candidates": [{"content": {"parts": [{"functionCall": {"name": "read", "args": {}}}]}, "finishReason": "STOP"}]}`,+ want: stopTurnCompleted,+ },+ {+ name: "safety block without content",+ reply: `{"candidates": [{"finishReason": "SAFETY", "safetyRatings": [{"category": "HARM_CATEGORY_HATE_SPEECH", "probability": "HIGH"}]}]}`,+ want: stopBlocked,+ },+ {+ name: "malformed function call without content",+ reply: `{"candidates": [{"content": {}, "finishReason": "MALFORMED_FUNCTION_CALL", "finishMessage": "Malformed function call: read(objects="}]}`,+ want: stopMalformedToolCall,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ response, err := chatWithScriptedReply(t, storepb.AISetting_GEMINI, "", tc.reply)+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ })+ }+}++func TestGeminiStopReasonWithoutACandidate(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ reply string+ want v1pb.AIChatResponse_StopReason+ }{+ {+ name: "no prompt feedback",+ reply: `{"usageMetadata": {"totalTokenCount": 12}}`,+ want: stopUnspecified,+ },+ {+ name: "prompt feedback without a block reason",+ reply: `{"promptFeedback": {"safetyRatings": []}}`,+ want: stopUnspecified,+ },+ {+ name: "prompt blocked with BLOCK_REASON_UNSPECIFIED",+ reply: `{"promptFeedback": {"blockReason": "BLOCK_REASON_UNSPECIFIED"}}`,+ want: stopUnspecified,+ },+ {+ name: "prompt blocked for SAFETY",+ reply: `{"promptFeedback": {"blockReason": "SAFETY"}}`,+ want: stopBlocked,+ },+ {+ name: "prompt blocked for OTHER",+ reply: `{"promptFeedback": {"blockReason": "OTHER"}}`,+ want: stopBlocked,+ },+ {+ name: "prompt blocked for BLOCKLIST",+ reply: `{"promptFeedback": {"blockReason": "BLOCKLIST"}}`,+ want: stopBlocked,+ },+ {+ name: "prompt blocked for PROHIBITED_CONTENT",+ reply: `{"promptFeedback": {"blockReason": "PROHIBITED_CONTENT"}}`,+ want: stopBlocked,+ },+ {+ name: "prompt blocked for IMAGE_SAFETY",+ reply: `{"promptFeedback": {"blockReason": "IMAGE_SAFETY"}}`,+ want: stopBlocked,+ },+ {+ name: "unknown block reason",+ reply: `{"promptFeedback": {"blockReason": "A_REASON_ADDED_LATER"}}`,+ want: stopOther,+ },+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ response, err := chatWithScriptedReply(t, storepb.AISetting_GEMINI, "", tc.reply)+ require.NoError(t, err)+ require.Equal(t, tc.want, response.GetStopReason())+ })+ }+}diff --git a/backend/plugin/aireview/model_ai_gemini_test.go b/backend/plugin/aireview/model_ai_gemini_test.gonew file mode 100644index 0000000000..fa67d7ca09--- /dev/null+++ b/backend/plugin/aireview/model_ai_gemini_test.go@@ -0,0 +1,50 @@+package aireview++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++func TestSettingModelKeepsGeminiRolesAlternatingAfterAnUnparsedToolCall(t *testing.T) {+ t.Parallel()++ replies := []string{+ `{"candidates": [{"content": {"parts": [{"functionCall": {"name": "search", "args": {"text": "orders"}}}]}, "finishReason": "STOP"}]}`,+ `{"candidates": [{"content": {}, "finishReason": "MALFORMED_FUNCTION_CALL", "finishMessage": "Malformed function call: read(objects="}]}`,+ `{"candidates": [{"content": {"parts": [{"text": "{\"findings\": []}"}]}, "finishReason": "STOP"}]}`,+ }+ var requests []geminiRequest+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ var request geminiRequest+ require.NoError(t, json.NewDecoder(r.Body).Decode(&request))+ requests = append(requests, request)+ w.Header().Set("Content-Type", "application/json")+ _, err := w.Write([]byte(replies[min(len(requests), len(replies))-1]))+ require.NoError(t, err)+ }))+ defer server.Close()++ model := NewModel(&storepb.AISetting{Provider: storepb.AISetting_GEMINI, Endpoint: server.URL, Model: "gemini-3.5-flash", ApiKey: "test-key"})+ result, err := NewReviewer(model).Review(context.Background(), &Request{Statement: "DROP TABLE orders;"}, ordersCatalog())+ require.NoError(t, err)+ require.Empty(t, result.Findings)+ require.Len(t, requests, 3)++ contents := requests[2].Contents+ roles := make([]string, 0, len(contents))+ for _, content := range contents {+ roles = append(roles, content.Role)+ }+ require.Equal(t, []string{"user", "model", "user"}, roles)+ last := contents[2].Parts+ require.Len(t, last, 2)+ require.Equal(t, "search", last[0].FunctionResponse.Name)+ require.Equal(t, malformedToolCallMessage, last[1].Text)+}diff --git a/backend/plugin/aireview/model_ai_test.go b/backend/plugin/aireview/model_ai_test.goindex 3f3014db33..51ed5785c3 100644--- a/backend/plugin/aireview/model_ai_test.go+++ b/backend/plugin/aireview/model_ai_test.go@@ -106,7 +106,7 @@ func TestSettingModelFailsOnASafetyBlockedGeminiReply(t *testing.T) {model := NewModel(&storepb.AISetting{Provider: storepb.AISetting_GEMINI, Endpoint: server.URL, Model: "gemini-3.5-flash", ApiKey: "test-key"})result, err := NewReviewer(model).Review(context.Background(), &Request{Statement: "DELETE FROM banned_phrases;"}, ordersCatalog())- require.ErrorIs(t, err, ErrEmptyReply)+ require.ErrorIs(t, err, ErrBlockedReply)require.Nil(t, result)require.Equal(t, 1, requests, "a blocked turn gets no correction round")}diff --git a/backend/plugin/aireview/reviewer.go b/backend/plugin/aireview/reviewer.goindex a182d7d5bc..735e3effd4 100644--- a/backend/plugin/aireview/reviewer.go+++ b/backend/plugin/aireview/reviewer.go@@ -19,9 +19,14 @@ import (const (defaultMaxModelCalls = 20defaultTimeout = 10 * time.Minute- // maxInvalidReplies is how many times the model is asked to correct a- // final reply before the review fails.+ // maxInvalidReplies is how many times the model is asked to try again, after+ // an invalid final reply or a tool call the vendor could not parse, before+ // the review fails.maxInvalidReplies = 1+ // malformedToolCallMessage answers a turn that held a tool call the vendor+ // could not parse. That turn is not in the history, so the message has to+ // say what happened.+ malformedToolCallMessage = "Your last turn held a tool call that could not be parsed, so no tool ran. Call the tool again, or reply with only the JSON object from the answer format.")var (@@ -29,10 +34,16 @@ var (ErrCallLimit = errors.New("the review reached the model call limit")// ErrDeadline means the review ran longer than its time limit.ErrDeadline = errors.New("the review ran out of time")- // ErrInvalidReply means the final reply was still invalid after the model was asked to correct it.+ // ErrInvalidReply means an invalid final reply, or a tool call the vendor could not parse, came when no correction round or call was left.ErrInvalidReply = errors.New("the model did not return a valid review result")// ErrEmptyReply means the vendor returned neither text nor tool calls.ErrEmptyReply = errors.New("the model returned an empty reply")+ // ErrTruncatedReply means the reply was truncated at a token limit, so findings may be missing.+ ErrTruncatedReply = errors.New("the model's reply was truncated at a token limit")+ // ErrBlockedReply means the vendor blocked the request or the reply, or the model refused to review.+ ErrBlockedReply = errors.New("the vendor blocked the review or the model refused it")+ // ErrUnfinishedReply means the stop reason was OTHER, such as Gemini's OTHER, or a value the loop does not know.+ ErrUnfinishedReply = errors.New("the vendor ended the turn for a reason the review does not accept"))// Request is one review: one sheet against one target database.@@ -124,9 +135,34 @@ func (r *Reviewer) Review(parent context.Context, request *Request, tools Tools)content := response.GetContent()toolCalls := response.GetToolCalls()- // An empty reply is a safety block, a refusal, or a garbled tool call.- // ai.Chat carries no stop reason yet, so the loop cannot tell them apart,- // and asking again could let a blocked review pass on the second try.+ // A truncated reply can parse as a whole answer with findings or tool+ // arguments missing, a blocked change was never judged, and OTHER can be+ // a block the vendor does not name. None of the three gets a correction+ // round, which could pass a change the model did not finish judging.+ switch response.GetStopReason() {+ case v1pb.AIChatResponse_TURN_COMPLETED, v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED:+ // A vendor that sends no reason, such as a compatible server without finish_reason, is judged by its reply.+ case v1pb.AIChatResponse_TOKEN_LIMIT:+ return nil, ErrTruncatedReply+ case v1pb.AIChatResponse_BLOCKED:+ return nil, ErrBlockedReply+ case v1pb.AIChatResponse_MALFORMED_TOOL_CALL:+ // The model meant to call a tool, so the turn is not final whatever+ // text or tool calls came with it. None of it runs or enters the+ // history. The turn takes a correction round from the budget of an+ // invalid reply, and fails the review when no round or call is left.+ if invalidReplies >= maxInvalidReplies || result.Calls >= r.maxModelCalls {+ return nil, errors.Wrap(ErrInvalidReply, "the vendor could not parse the model's tool call")+ }+ invalidReplies+++ messages = append(messages, textMessage(v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_USER, malformedToolCallMessage))+ continue+ default:+ // OTHER lands here, and so does any value added later.+ return nil, ErrUnfinishedReply+ }+ // An empty reply that no stop reason explains can be a block the vendor+ // did not name, and asking again could let a blocked review pass.if strings.TrimSpace(content) == "" && len(toolCalls) == 0 {return nil, ErrEmptyReply}diff --git a/backend/plugin/aireview/reviewer_test.go b/backend/plugin/aireview/reviewer_test.goindex 05b3097dbb..102957fbec 100644--- a/backend/plugin/aireview/reviewer_test.go+++ b/backend/plugin/aireview/reviewer_test.go@@ -20,6 +20,14 @@ const (roleTool = v1pb.AIChatMessageRole_AI_CHAT_MESSAGE_ROLE_TOOL)+const (+ stopTurnCompleted = v1pb.AIChatResponse_TURN_COMPLETED+ stopTokenLimit = v1pb.AIChatResponse_TOKEN_LIMIT+ stopBlocked = v1pb.AIChatResponse_BLOCKED+ stopMalformedToolCall = v1pb.AIChatResponse_MALFORMED_TOOL_CALL+ stopOther = v1pb.AIChatResponse_OTHER+)+const threeLineStatement = "ALTER TABLE orders ADD COLUMN note text;\n\nDELETE FROM order_events;\n"const validReply = `{"findings": [{"title": "Add a WHERE clause", "severity": "P0", "line": 3, "rule": "an UPDATE or DELETE that touches more rows than the author means", "evidence": "order_events has 9000000 rows", "fix": "Delete in batches by id range"}]}`@@ -79,6 +87,11 @@ func toolCallReply(calls ...*v1pb.AIChatToolCall) modelStep {return modelStep{response: &v1pb.AIChatResponse{ToolCalls: calls, Usage: &v1pb.AIChatUsage{TotalTokens: 100}}}}+func (s modelStep) stoppedFor(reason v1pb.AIChatResponse_StopReason) modelStep {+ s.response.StopReason = reason+ return s+}+func TestReviewReturnsFindings(t *testing.T) {t.Parallel()@@ -184,13 +197,170 @@ func TestReviewExitPaths(t *testing.T) {wantToolCalls: []string{"read"},},{- // A safety block, a refusal, and a garbled tool call all arrive as an- // empty reply. Asking again could let a blocked review pass.name: "empty reply fails without a correction round",steps: []modelStep{finalReply(" \n"), finalReply(validReply)},wantErr: ErrEmptyReply,wantRequests: 1,},+ {+ name: "empty reply of a completed turn fails without a correction round",+ steps: []modelStep{finalReply("").stoppedFor(stopTurnCompleted), finalReply(validReply)},+ wantErr: ErrEmptyReply,+ wantRequests: 1,+ },+ {+ name: "empty reply with another stop reason fails without a correction round",+ steps: []modelStep{finalReply("").stoppedFor(stopOther), finalReply(validReply)},+ wantErr: ErrUnfinishedReply,+ wantRequests: 1,+ },+ {+ name: "reply of a completed turn is parsed",+ steps: []modelStep{finalReply(validReply).stoppedFor(stopTurnCompleted)},+ wantFindings: 1,+ wantCalls: 1,+ },+ {+ name: "reply without a stop reason is parsed",+ steps: []modelStep{finalReply(validReply).stoppedFor(v1pb.AIChatResponse_STOP_REASON_UNSPECIFIED)},+ wantFindings: 1,+ wantCalls: 1,+ },+ {+ name: "reply with another stop reason fails even when it passes",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(stopOther), finalReply(validReply)},+ wantErr: ErrUnfinishedReply,+ wantRequests: 1,+ },+ {+ name: "turn with another stop reason runs no tool",+ steps: []modelStep{toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"}).stoppedFor(stopOther), finalReply(validReply)},+ wantErr: ErrUnfinishedReply,+ wantRequests: 1,+ },+ {+ name: "reply with a stop reason added later fails",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(v1pb.AIChatResponse_StopReason(99)), finalReply(validReply)},+ wantErr: ErrUnfinishedReply,+ wantRequests: 1,+ },+ {+ name: "truncated reply fails even when it parses",+ steps: []modelStep{finalReply(validReply).stoppedFor(stopTokenLimit), finalReply(validReply)},+ wantErr: ErrTruncatedReply,+ wantRequests: 1,+ },+ {+ name: "truncated reply that does not parse gets no correction round",+ steps: []modelStep{finalReply(`{"findings": [{"title": "Add a WHERE`).stoppedFor(stopTokenLimit), finalReply(validReply)},+ wantErr: ErrTruncatedReply,+ wantRequests: 1,+ },+ {+ name: "truncated turn with tool calls runs no tool",+ steps: []modelStep{toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"}).stoppedFor(stopTokenLimit), finalReply(validReply)},+ wantErr: ErrTruncatedReply,+ wantRequests: 1,+ },+ {+ name: "blocked reply with text gets no correction round",+ steps: []modelStep{finalReply("I can't help with that request.").stoppedFor(stopBlocked), finalReply(validReply)},+ wantErr: ErrBlockedReply,+ wantRequests: 1,+ },+ {+ name: "blocked reply that parses still fails",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(stopBlocked)},+ wantErr: ErrBlockedReply,+ wantRequests: 1,+ },+ {+ name: "blocked reply without text gets no correction round",+ steps: []modelStep{finalReply("").stoppedFor(stopBlocked), finalReply(validReply)},+ wantErr: ErrBlockedReply,+ wantRequests: 1,+ },+ {+ name: "blocked turn with tool calls runs no tool",+ steps: []modelStep{toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"}).stoppedFor(stopBlocked), finalReply(validReply)},+ wantErr: ErrBlockedReply,+ wantRequests: 1,+ },+ {+ name: "tool call the vendor could not parse gets one correction round",+ steps: []modelStep{finalReply("").stoppedFor(stopMalformedToolCall), finalReply(validReply)},+ wantFindings: 1,+ wantCalls: 2,+ wantLastUserText: "Call the tool again, or reply with only the JSON object",+ },+ {+ name: "model calls the tool again after the vendor could not parse the call",+ steps: []modelStep{+ finalReply("").stoppedFor(stopMalformedToolCall),+ toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"}),+ finalReply(validReply),+ },+ wantFindings: 1,+ wantCalls: 3,+ wantToolCalls: []string{"read"},+ },+ {+ name: "passing text beside an unparsed tool call gets a correction round, not a pass",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(stopMalformedToolCall), finalReply(validReply)},+ wantFindings: 1,+ wantCalls: 2,+ wantLastUserText: "Call the tool again, or reply with only the JSON object",+ },+ {+ name: "tool calls beside an unparsed tool call do not run",+ steps: []modelStep{toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"}).stoppedFor(stopMalformedToolCall), finalReply(validReply)},+ wantFindings: 1,+ wantCalls: 2,+ wantToolCalls: nil,+ },+ {+ name: "second tool call the vendor could not parse fails",+ steps: []modelStep{finalReply("").stoppedFor(stopMalformedToolCall)},+ wantErr: ErrInvalidReply,+ wantErrText: "the vendor could not parse the model's tool call",+ wantRequests: 2,+ },+ {+ name: "second unparsed tool call with passing text fails",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(stopMalformedToolCall)},+ wantErr: ErrInvalidReply,+ wantErrText: "the vendor could not parse the model's tool call",+ wantRequests: 2,+ },+ {+ name: "unparsed tool call after an invalid reply has no correction round left",+ steps: []modelStep{finalReply("not json"), finalReply("").stoppedFor(stopMalformedToolCall), finalReply(validReply)},+ wantErr: ErrInvalidReply,+ wantErrText: "the vendor could not parse the model's tool call",+ wantRequests: 2,+ },+ {+ name: "unparsed tool call with passing text after an invalid reply has no correction round left",+ steps: []modelStep{finalReply("not json"), finalReply(`{"findings": []}`).stoppedFor(stopMalformedToolCall), finalReply(validReply)},+ wantErr: ErrInvalidReply,+ wantErrText: "the vendor could not parse the model's tool call",+ wantRequests: 2,+ },+ {+ name: "invalid reply after an unparsed tool call has no correction round left",+ steps: []modelStep{finalReply("").stoppedFor(stopMalformedToolCall), finalReply("not json"), finalReply(validReply)},+ wantErr: ErrInvalidReply,+ wantErrText: "the reply is not a JSON object",+ wantRequests: 2,+ },+ {+ name: "unparsed tool call on the last permitted call fails",+ steps: []modelStep{finalReply(`{"findings": []}`).stoppedFor(stopMalformedToolCall)},+ maxModelCalls: 1,+ wantErr: ErrInvalidReply,+ wantErrText: "the vendor could not parse the model's tool call",+ wantRequests: 1,+ },{name: "model error fails",steps: []modelStep{{err: errors.New("Gemini API returned status 401")}},@@ -265,6 +435,55 @@ func TestReviewExitPaths(t *testing.T) {}}+func TestReviewStopReasonFailureOfAnEmptyReplyIsNotErrEmptyReply(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ reason v1pb.AIChatResponse_StopReason+ wantErr error+ }{+ {name: "truncated", reason: stopTokenLimit, wantErr: ErrTruncatedReply},+ {name: "blocked", reason: stopBlocked, wantErr: ErrBlockedReply},+ {name: "another stop reason", reason: stopOther, wantErr: ErrUnfinishedReply},+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ model := &scriptedModel{steps: []modelStep{finalReply("").stoppedFor(tc.reason)}}+ _, err := NewReviewer(model).Review(context.Background(), &Request{Statement: threeLineStatement}, &fakeTools{})+ require.ErrorIs(t, err, tc.wantErr)+ require.NotErrorIs(t, err, ErrEmptyReply)+ })+ }+}++func TestReviewKeepsAnUnparsedToolCallTurnOutOfTheHistory(t *testing.T) {+ t.Parallel()++ tests := []struct {+ name string+ step modelStep+ }{+ {name: "empty", step: finalReply("")},+ {name: "with text", step: finalReply(`{"findings": []}`)},+ {name: "with tool calls", step: toolCallReply(&v1pb.AIChatToolCall{Id: "call-1", Name: "read"})},+ }+ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()++ model := &scriptedModel{steps: []modelStep{tc.step.stoppedFor(stopMalformedToolCall), finalReply(validReply)}}+ _, err := NewReviewer(model).Review(context.Background(), &Request{Statement: threeLineStatement}, &fakeTools{})+ require.NoError(t, err)+ history := model.requests[1].Messages+ require.Equal(t, []v1pb.AIChatMessageRole{roleSystem, roleUser, roleUser}, roles(history))+ require.Equal(t, malformedToolCallMessage, history[2].GetContent())+ })+ }+}+func TestReviewUnknownToolNamesTheRealTools(t *testing.T) {t.Parallel()diff --git a/frontend/src/types/proto-es/v1/ai_service_pb.d.ts b/frontend/src/types/proto-es/v1/ai_service_pb.d.tsindex 4ad9218bb5..1d83a88eda 100644--- a/frontend/src/types/proto-es/v1/ai_service_pb.d.ts+++ b/frontend/src/types/proto-es/v1/ai_service_pb.d.ts@@ -200,6 +200,13 @@ export declare type AIChatResponse = Message<"bytebase.v1.AIChatResponse"> & {* @generated from field: optional bytebase.v1.AIChatUsage usage = 3;*/usage?: AIChatUsage | undefined;++ /**+ * Why the model stopped. A truncated or blocked response can still carry content.+ *+ * @generated from field: bytebase.v1.AIChatResponse.StopReason stop_reason = 4;+ */+ stopReason: AIChatResponse_StopReason;};/**@@ -208,6 +215,60 @@ export declare type AIChatResponse = Message<"bytebase.v1.AIChatResponse"> & {*/export declare const AIChatResponseSchema: GenMessage<AIChatResponse>;+/**+ * Why the model stopped generating a response.+ *+ * @generated from enum bytebase.v1.AIChatResponse.StopReason+ */+export enum AIChatResponse_StopReason {+ /**+ * The provider did not report why the model stopped.+ *+ * @generated from enum value: STOP_REASON_UNSPECIFIED = 0;+ */+ STOP_REASON_UNSPECIFIED = 0,++ /**+ * The model completed its turn with an answer or with tool calls.+ *+ * @generated from enum value: TURN_COMPLETED = 1;+ */+ TURN_COMPLETED = 1,++ /**+ * The response was truncated at a token limit and is incomplete.+ *+ * @generated from enum value: TOKEN_LIMIT = 2;+ */+ TOKEN_LIMIT = 2,++ /**+ * The provider blocked the request or the response, or the model refused to answer.+ *+ * @generated from enum value: BLOCKED = 3;+ */+ BLOCKED = 3,++ /**+ * The model generated a tool call that the provider could not parse.+ *+ * @generated from enum value: MALFORMED_TOOL_CALL = 4;+ */+ MALFORMED_TOOL_CALL = 4,++ /**+ * The model stopped for a reason that no other value names.+ *+ * @generated from enum value: OTHER = 5;+ */+ OTHER = 5,+}++/**+ * Describes the enum bytebase.v1.AIChatResponse.StopReason.+ */+export declare const AIChatResponse_StopReasonSchema: GenEnum<AIChatResponse_StopReason>;+/*** Role of a chat message.*diff --git a/frontend/src/types/proto-es/v1/ai_service_pb.js b/frontend/src/types/proto-es/v1/ai_service_pb.jsindex be173db01f..a9f5b83a5e 100644--- a/frontend/src/types/proto-es/v1/ai_service_pb.js+++ b/frontend/src/types/proto-es/v1/ai_service_pb.js@@ -10,7 +10,7 @@ import { file_v1_annotation } from "./annotation_pb";* Describes the file v1/ai_service.proto.*/export const file_v1_ai_service = /*@__PURE__*/- fileDesc("ChN2MS9haV9zZXJ2aWNlLnByb3RvEgtieXRlYmFzZS52MSK8AQoNQUlDaGF0TWVzc2FnZRIsCgRyb2xlGAEgASgOMh4uYnl0ZWJhc2UudjEuQUlDaGF0TWVzc2FnZVJvbGUSFAoHY29udGVudBgCIAEoCUgAiAEBEi8KCnRvb2xfY2FsbHMYAyADKAsyGy5ieXRlYmFzZS52MS5BSUNoYXRUb29sQ2FsbBIZCgx0b29sX2NhbGxfaWQYBCABKAlIAYgBAUIKCghfY29udGVudEIPCg1fdG9vbF9jYWxsX2lkIlQKFEFJQ2hhdFRvb2xEZWZpbml0aW9uEgwKBG5hbWUYASABKAkSEwoLZGVzY3JpcHRpb24YAiABKAkSGQoRcGFyYW1ldGVyc19zY2hlbWEYAyABKAkiYQoOQUlDaGF0VG9vbENhbGwSCgoCaWQYASABKAkSDAoEbmFtZRgCIAEoCRIRCglhcmd1bWVudHMYAyABKAkSFQoIbWV0YWRhdGEYBCABKAlIAIgBAUILCglfbWV0YWRhdGEihgEKDUFJQ2hhdFJlcXVlc3QSMgoIbWVzc2FnZXMYASADKAsyGi5ieXRlYmFzZS52MS5BSUNoYXRNZXNzYWdlQgTQ6jACEkEKEHRvb2xfZGVmaW5pdGlvbnMYAiADKAsyIS5ieXRlYmFzZS52MS5BSUNoYXRUb29sRGVmaW5pdGlvbkIE0OowAiIjCgtBSUNoYXRVc2FnZRIUCgx0b3RhbF90b2tlbnMYASABKAUimwEKDkFJQ2hhdFJlc3BvbnNlEhQKB2NvbnRlbnQYASABKAlIAIgBARIvCgp0b29sX2NhbGxzGAIgAygLMhsuYnl0ZWJhc2UudjEuQUlDaGF0VG9vbENhbGwSLAoFdXNhZ2UYAyABKAsyGC5ieXRlYmFzZS52MS5BSUNoYXRVc2FnZUgBiAEBQgoKCF9jb250ZW50QggKBl91c2FnZSq8AQoRQUlDaGF0TWVzc2FnZVJvbGUSJAogQUlfQ0hBVF9NRVNTQUdFX1JPTEVfVU5TUEVDSUZJRUQQABIfChtBSV9DSEFUX01FU1NBR0VfUk9MRV9TWVNURU0QARIdChlBSV9DSEFUX01FU1NBR0VfUk9MRV9VU0VSEAISIgoeQUlfQ0hBVF9NRVNTQUdFX1JPTEVfQVNTSVNUQU5UEAMSHQoZQUlfQ0hBVF9NRVNTQUdFX1JPTEVfVE9PTBAEMnAKCUFJU2VydmljZRJjCgRDaGF0EhouYnl0ZWJhc2UudjEuQUlDaGF0UmVxdWVzdBobLmJ5dGViYXNlLnYxLkFJQ2hhdFJlc3BvbnNlIiKQ6jACoOowBKjqMAyC0+STAhA6ASoiCy92MS9haS9jaGF0QqQBCg9jb20uYnl0ZWJhc2UudjFCDkFpU2VydmljZVByb3RvUAFaNGdpdGh1Yi5jb20vYnl0ZWJhc2UvYnl0ZWJhc2UvYmFja2VuZC9nZW5lcmF0ZWQtZ28vdjGiAgNCWFiqAgtCeXRlYmFzZS5WMcoCC0J5dGViYXNlXFYx4gIXQnl0ZWJhc2VcVjFcR1BCTWV0YWRhdGHqAgxCeXRlYmFzZTo6VjFiBnByb3RvMw", [file_google_api_annotations, file_v1_annotation]);+ fileDesc("ChN2MS9haV9zZXJ2aWNlLnByb3RvEgtieXRlYmFzZS52MSK8AQoNQUlDaGF0TWVzc2FnZRIsCgRyb2xlGAEgASgOMh4uYnl0ZWJhc2UudjEuQUlDaGF0TWVzc2FnZVJvbGUSFAoHY29udGVudBgCIAEoCUgAiAEBEi8KCnRvb2xfY2FsbHMYAyADKAsyGy5ieXRlYmFzZS52MS5BSUNoYXRUb29sQ2FsbBIZCgx0b29sX2NhbGxfaWQYBCABKAlIAYgBAUIKCghfY29udGVudEIPCg1fdG9vbF9jYWxsX2lkIlQKFEFJQ2hhdFRvb2xEZWZpbml0aW9uEgwKBG5hbWUYASABKAkSEwoLZGVzY3JpcHRpb24YAiABKAkSGQoRcGFyYW1ldGVyc19zY2hlbWEYAyABKAkiYQoOQUlDaGF0VG9vbENhbGwSCgoCaWQYASABKAkSDAoEbmFtZRgCIAEoCRIRCglhcmd1bWVudHMYAyABKAkSFQoIbWV0YWRhdGEYBCABKAlIAIgBAUILCglfbWV0YWRhdGEihgEKDUFJQ2hhdFJlcXVlc3QSMgoIbWVzc2FnZXMYASADKAsyGi5ieXRlYmFzZS52MS5BSUNoYXRNZXNzYWdlQgTQ6jACEkEKEHRvb2xfZGVmaW5pdGlvbnMYAiADKAsyIS5ieXRlYmFzZS52MS5BSUNoYXRUb29sRGVmaW5pdGlvbkIE0OowAiIjCgtBSUNoYXRVc2FnZRIUCgx0b3RhbF90b2tlbnMYASABKAUi2QIKDkFJQ2hhdFJlc3BvbnNlEhQKB2NvbnRlbnQYASABKAlIAIgBARIvCgp0b29sX2NhbGxzGAIgAygLMhsuYnl0ZWJhc2UudjEuQUlDaGF0VG9vbENhbGwSLAoFdXNhZ2UYAyABKAsyGC5ieXRlYmFzZS52MS5BSUNoYXRVc2FnZUgBiAEBEjsKC3N0b3BfcmVhc29uGAQgASgOMiYuYnl0ZWJhc2UudjEuQUlDaGF0UmVzcG9uc2UuU3RvcFJlYXNvbiJ/CgpTdG9wUmVhc29uEhsKF1NUT1BfUkVBU09OX1VOU1BFQ0lGSUVEEAASEgoOVFVSTl9DT01QTEVURUQQARIPCgtUT0tFTl9MSU1JVBACEgsKB0JMT0NLRUQQAxIXChNNQUxGT1JNRURfVE9PTF9DQUxMEAQSCQoFT1RIRVIQBUIKCghfY29udGVudEIICgZfdXNhZ2UqvAEKEUFJQ2hhdE1lc3NhZ2VSb2xlEiQKIEFJX0NIQVRfTUVTU0FHRV9ST0xFX1VOU1BFQ0lGSUVEEAASHwobQUlfQ0hBVF9NRVNTQUdFX1JPTEVfU1lTVEVNEAESHQoZQUlfQ0hBVF9NRVNTQUdFX1JPTEVfVVNFUhACEiIKHkFJX0NIQVRfTUVTU0FHRV9ST0xFX0FTU0lTVEFOVBADEh0KGUFJX0NIQVRfTUVTU0FHRV9ST0xFX1RPT0wQBDJwCglBSVNlcnZpY2USYwoEQ2hhdBIaLmJ5dGViYXNlLnYxLkFJQ2hhdFJlcXVlc3QaGy5ieXRlYmFzZS52MS5BSUNoYXRSZXNwb25zZSIikOowAqDqMASo6jAMgtPkkwIQOgEqIgsvdjEvYWkvY2hhdEKkAQoPY29tLmJ5dGViYXNlLnYxQg5BaVNlcnZpY2VQcm90b1ABWjRnaXRodWIuY29tL2J5dGViYXNlL2J5dGViYXNlL2JhY2tlbmQvZ2VuZXJhdGVkLWdvL3YxogIDQlhYqgILQnl0ZWJhc2UuVjHKAgtCeXRlYmFzZVxWMeICF0J5dGViYXNlXFYxXEdQQk1ldGFkYXRh6gIMQnl0ZWJhc2U6OlYxYgZwcm90bzM", [file_google_api_annotations, file_v1_annotation]);/*** Describes the message bytebase.v1.AIChatMessage.@@ -54,6 +54,20 @@ export const AIChatUsageSchema = /*@__PURE__*/export const AIChatResponseSchema = /*@__PURE__*/messageDesc(file_v1_ai_service, 5);+/**+ * Describes the enum bytebase.v1.AIChatResponse.StopReason.+ */+export const AIChatResponse_StopReasonSchema = /*@__PURE__*/+ enumDesc(file_v1_ai_service, 5, 0);++/**+ * Why the model stopped generating a response.+ *+ * @generated from enum bytebase.v1.AIChatResponse.StopReason+ */+export const AIChatResponse_StopReason = /*@__PURE__*/+ tsEnum(AIChatResponse_StopReasonSchema);+/*** Describes the enum bytebase.v1.AIChatMessageRole.*/diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex 3fee8a3d0f..d61500bd6b 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -16583,9 +16583,25 @@ components:- type: "null"title: usagedescription: Token usage for this provider call, when available.+ stopReason:+ allOf:+ - $ref: '#/components/schemas/bytebase.v1.AIChatResponse.StopReason'+ title: stop_reason+ description: Why the model stopped. A truncated or blocked response can still carry content.title: AIChatResponseadditionalProperties: falsedescription: Response message for AIService.Chat.+ bytebase.v1.AIChatResponse.StopReason:+ type: string+ title: StopReason+ enum:+ - STOP_REASON_UNSPECIFIED+ - TURN_COMPLETED+ - TOKEN_LIMIT+ - BLOCKED+ - MALFORMED_TOOL_CALL+ - OTHER+ description: Why the model stopped generating a response.bytebase.v1.AIChatToolCall:type: objectproperties:diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex 2271ffd15c..2a9f118091 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -292,6 +292,7 @@- [AIChatUsage](#bytebase-v1-AIChatUsage)- [AIChatMessageRole](#bytebase-v1-AIChatMessageRole)+ - [AIChatResponse.StopReason](#bytebase-v1-AIChatResponse-StopReason)- [AIService](#bytebase-v1-AIService)@@ -5188,6 +5189,7 @@ Response message for AIService.Chat.| content | [string](#string) | optional | The text content of the AI response. Optional when the response only contains tool calls. || tool_calls | [AIChatToolCall](#bytebase-v1-AIChatToolCall) | repeated | Tool calls the AI wants to make. || usage | [AIChatUsage](#bytebase-v1-AIChatUsage) | optional | Token usage for this provider call, when available. |+| stop_reason | [AIChatResponse.StopReason](#bytebase-v1-AIChatResponse-StopReason) | | Why the model stopped. A truncated or blocked response can still carry content. |@@ -5260,6 +5262,22 @@ Role of a chat message.| AI_CHAT_MESSAGE_ROLE_TOOL | 4 | Tool result message. |++<a name="bytebase-v1-AIChatResponse-StopReason"></a>++### AIChatResponse.StopReason+Why the model stopped generating a response.++| Name | Number | Description |+| ---- | ------ | ----------- |+| STOP_REASON_UNSPECIFIED | 0 | The provider did not report why the model stopped. |+| TURN_COMPLETED | 1 | The model completed its turn with an answer or with tool calls. |+| TOKEN_LIMIT | 2 | The response was truncated at a token limit and is incomplete. |+| BLOCKED | 3 | The provider blocked the request or the response, or the model refused to answer. |+| MALFORMED_TOOL_CALL | 4 | The model generated a tool call that the provider could not parse. |+| OTHER | 5 | The model stopped for a reason that no other value names. |++diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.htmlindex 486ca1e81f..20312737f5 100644--- a/proto/gen/grpc-doc/v1/index.html+++ b/proto/gen/grpc-doc/v1/index.html@@ -1292,6 +1292,10 @@<a href="#bytebase.v1.AIChatMessageRole"><span class="badge">E</span>AIChatMessageRole</a></li>+ <li>+ <a href="#bytebase.v1.AIChatResponse.StopReason"><span class="badge">E</span>AIChatResponse.StopReason</a>+ </li>+<li>@@ -15097,6 +15101,13 @@ Permissions required: None (authentication required)</p></td><td><p>Token usage for this provider call, when available. </p></td></tr>+ <tr>+ <td>stop_reason</td>+ <td><a href="#bytebase.v1.AIChatResponse.StopReason">AIChatResponse.StopReason</a></td>+ <td></td>+ <td><p>Why the model stopped. A truncated or blocked response can still carry content. </p></td>+ </tr>+</tbody></table>@@ -15255,6 +15266,53 @@ Frontend must echo this back unchanged when sending tool results. </p></td></tbody></table>+ <h3 id="bytebase.v1.AIChatResponse.StopReason">AIChatResponse.StopReason</h3>+ <p>Why the model stopped generating a response.</p>+ <table class="enum-table">+ <thead>+ <tr><td>Name</td><td>Number</td><td>Description</td></tr>+ </thead>+ <tbody>++ <tr>+ <td>STOP_REASON_UNSPECIFIED</td>+ <td>0</td>+ <td><p>The provider did not report why the model stopped.</p></td>+ </tr>++ <tr>+ <td>TURN_COMPLETED</td>+ <td>1</td>+ <td><p>The model completed its turn with an answer or with tool calls.</p></td>+ </tr>++ <tr>+ <td>TOKEN_LIMIT</td>+ <td>2</td>+ <td><p>The response was truncated at a token limit and is incomplete.</p></td>+ </tr>++ <tr>+ <td>BLOCKED</td>+ <td>3</td>+ <td><p>The provider blocked the request or the response, or the model refused to answer.</p></td>+ </tr>++ <tr>+ <td>MALFORMED_TOOL_CALL</td>+ <td>4</td>+ <td><p>The model generated a tool call that the provider could not parse.</p></td>+ </tr>++ <tr>+ <td>OTHER</td>+ <td>5</td>+ <td><p>The model stopped for a reason that no other value names.</p></td>+ </tr>++ </tbody>+ </table>+diff --git a/proto/v1/v1/ai_service.proto b/proto/v1/v1/ai_service.protoindex 6ed21e0216..8c4ac2cb61 100644--- a/proto/v1/v1/ai_service.proto+++ b/proto/v1/v1/ai_service.proto@@ -96,6 +96,22 @@ message AIChatUsage {// Response message for AIService.Chat.message AIChatResponse {+ // Why the model stopped generating a response.+ enum StopReason {+ // The provider did not report why the model stopped.+ STOP_REASON_UNSPECIFIED = 0;+ // The model completed its turn with an answer or with tool calls.+ TURN_COMPLETED = 1;+ // The response was truncated at a token limit and is incomplete.+ TOKEN_LIMIT = 2;+ // The provider blocked the request or the response, or the model refused to answer.+ BLOCKED = 3;+ // The model generated a tool call that the provider could not parse.+ MALFORMED_TOOL_CALL = 4;+ // The model stopped for a reason that no other value names.+ OTHER = 5;+ }+// The text content of the AI response. Optional when the response only contains tool calls.optional string content = 1;@@ -104,4 +120,7 @@ message AIChatResponse {// Token usage for this provider call, when available.optional AIChatUsage usage = 3;++ // Why the model stopped. A truncated or blocked response can still carry content.+ StopReason stop_reason = 4;}