forked from longtao.wu/droboticflow
285 lines
11 KiB
Go
285 lines
11 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestGenerateOperatorRetriesTimeoutThenSucceeds(t *testing.T) {
|
|
var attempts atomic.Int32
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
count := attempts.Add(1)
|
|
if count == 1 {
|
|
time.Sleep(80 * time.Millisecond)
|
|
return
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(writer, `{
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"content": "{\"name\":\"Timeout Retry Operator\",\"category\":\"AI\",\"description\":\"retry ok\",\"inputs\":[{\"name\":\"input\",\"type\":\"json\",\"transmission\":\"bus\"}],\"outputs\":[{\"name\":\"result\",\"type\":\"json\",\"transmission\":\"bus\"}],\"hyperparams\":[],\"skillDefinition\":\"retry ok\"}"
|
|
}
|
|
}
|
|
]
|
|
}`)
|
|
}))
|
|
defer server.Close()
|
|
|
|
service := NewService(server.URL, "test-key", "model-x").(*client)
|
|
service.requestTimeout = 40 * time.Millisecond
|
|
service.maxAttempts = 2
|
|
|
|
item, err := service.GenerateOperator(context.Background(), "generate operator")
|
|
if err != nil {
|
|
t.Fatalf("expected retry to succeed, got error: %v", err)
|
|
}
|
|
if item.Name != "Timeout Retry Operator" {
|
|
t.Fatalf("unexpected operator name: %s", item.Name)
|
|
}
|
|
if attempts.Load() != 2 {
|
|
t.Fatalf("expected 2 attempts, got %d", attempts.Load())
|
|
}
|
|
}
|
|
|
|
func TestGenerateOperatorTimeoutReturnsFriendlyError(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
time.Sleep(80 * time.Millisecond)
|
|
}))
|
|
defer server.Close()
|
|
|
|
service := NewService(server.URL, "test-key", "model-x").(*client)
|
|
service.requestTimeout = 30 * time.Millisecond
|
|
service.maxAttempts = 1
|
|
|
|
_, err := service.GenerateOperator(context.Background(), "generate operator")
|
|
if err == nil {
|
|
t.Fatalf("expected timeout error")
|
|
}
|
|
if err.Error() != "AI 生成超时,请稍后重试,或缩短描述范围后再试" {
|
|
t.Fatalf("unexpected timeout message: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGenerateOperatorRequestsBoundedJSONMode(t *testing.T) {
|
|
requestSeen := false
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
requestSeen = true
|
|
body, err := io.ReadAll(request.Body)
|
|
if err != nil {
|
|
t.Fatalf("read request body: %v", err)
|
|
}
|
|
var payload chatCompletionRequest
|
|
if err := json.Unmarshal(body, &payload); err != nil {
|
|
t.Fatalf("decode request body: %v", err)
|
|
}
|
|
if payload.MaxTokens != operatorMaxTokens {
|
|
t.Fatalf("max_tokens = %d", payload.MaxTokens)
|
|
}
|
|
if payload.ResponseFormat == nil || payload.ResponseFormat.Type != "json_object" {
|
|
t.Fatalf("response_format = %#v", payload.ResponseFormat)
|
|
}
|
|
if len(payload.Messages) != 2 {
|
|
t.Fatalf("messages length = %d", len(payload.Messages))
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(writer, `{
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"content": "{\"name\":\"Bounded JSON Operator\",\"category\":\"AI\",\"description\":\"bounded ok\",\"inputs\":[{\"name\":\"input\",\"type\":\"json\",\"transmission\":\"bus\"}],\"outputs\":[{\"name\":\"result\",\"type\":\"json\",\"transmission\":\"bus\"}],\"hyperparams\":[],\"skillDefinition\":\"bounded ok\"}"
|
|
}
|
|
}
|
|
]
|
|
}`)
|
|
}))
|
|
defer server.Close()
|
|
|
|
item, err := NewService(server.URL, "test-key", "model-x").GenerateOperator(context.Background(), "generate operator")
|
|
if err != nil {
|
|
t.Fatalf("expected generation to succeed, got error: %v", err)
|
|
}
|
|
if !requestSeen {
|
|
t.Fatalf("test server did not receive request")
|
|
}
|
|
if item.Name != "Bounded JSON Operator" {
|
|
t.Fatalf("unexpected operator name: %s", item.Name)
|
|
}
|
|
}
|
|
|
|
func TestGenerateOperatorEnrichesRunnableExternalScaffold(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(writer, `{
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"content": "{\"name\":\"视频目标检测裁剪\",\"category\":\"CV\",\"description\":\"检测视频目标并裁剪片段\",\"inputs\":[{\"name\":\"videos\",\"type\":\"video\"}],\"outputs\":[{\"name\":\"clips\",\"type\":\"video\"}],\"hyperparams\":[{\"key\":\"confidence threshold\",\"value\":0.35,\"description\":\"检测阈值\"}],\"skillDefinition\":\"接收视频并输出裁剪片段\"}"
|
|
}
|
|
}
|
|
]
|
|
}`)
|
|
}))
|
|
defer server.Close()
|
|
|
|
item, err := NewService(server.URL, "test-key", "model-x").GenerateOperator(context.Background(), "生成一个 YOLO 视频目标检测裁剪算子,需要 GPU")
|
|
if err != nil {
|
|
t.Fatalf("expected generation to succeed, got error: %v", err)
|
|
}
|
|
if item.Version != "v1.0.0" {
|
|
t.Fatalf("expected default version, got %q", item.Version)
|
|
}
|
|
if len(item.ContainerCommand) == 0 || strings.Join(item.ContainerCommand, " ") != "python /app/src/operator.py" {
|
|
t.Fatalf("expected runnable container command, got %#v", item.ContainerCommand)
|
|
}
|
|
if item.Resources == nil || item.Resources.GPUType != "NVIDIA A800 80GB" || item.Resources.GPULimit != "1" {
|
|
t.Fatalf("expected inferred GPU resources, got %#v", item.Resources)
|
|
}
|
|
if item.Inputs[0].Transmission != "oss" || item.Outputs[0].Transmission != "oss" {
|
|
t.Fatalf("expected video ports to default to oss transmission, inputs=%#v outputs=%#v", item.Inputs, item.Outputs)
|
|
}
|
|
if len(item.Hyperparams) != 1 || item.Hyperparams[0].Key != "CONFIDENCE_THRESHOLD" || item.Hyperparams[0].Type != "number" {
|
|
t.Fatalf("expected normalized hyperparam, got %#v", item.Hyperparams)
|
|
}
|
|
if item.Hyperparams[0].Value != "0.35" {
|
|
t.Fatalf("expected numeric hyperparam value to be stringified, got %#v", item.Hyperparams[0])
|
|
}
|
|
if item.RuntimeContract == nil || item.RuntimeContract["protocol"] != "droboticflow.external-operator/v1" {
|
|
t.Fatalf("expected runtime contract, got %#v", item.RuntimeContract)
|
|
}
|
|
if item.RuntimeContract["generatedRepository"] == nil {
|
|
t.Fatalf("expected generated repository to be embedded in runtime contract")
|
|
}
|
|
if item.Implementation == nil || item.Implementation.RepositoryPath == "" || len(item.Implementation.Files) < 5 {
|
|
t.Fatalf("expected implementation files, got %#v", item.Implementation)
|
|
}
|
|
paths := map[string]bool{}
|
|
for _, file := range item.Implementation.Files {
|
|
paths[file.Path] = strings.TrimSpace(file.Content) != ""
|
|
}
|
|
for _, required := range []string{"Dockerfile", "src/operator.py", "runtime_contract.json", "operator.json", "tests/acceptance/fixture_contract.json", "README.md"} {
|
|
if !paths[required] {
|
|
t.Fatalf("expected generated file %s in %#v", required, paths)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerateWorkflowCompactsOperatorCatalog(t *testing.T) {
|
|
operators := []domain.OperatorRecord{
|
|
{
|
|
ID: "operator-builtin-csv-to-json",
|
|
Name: "CSV 转 JSON",
|
|
Category: "IO",
|
|
Description: "将 CSV 文件转换为 JSON 记录",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeFile}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "records", Type: domain.PortTypeJSON}},
|
|
},
|
|
{
|
|
ID: "operator-builtin-upload",
|
|
Name: "结果上传",
|
|
Category: "IO",
|
|
Description: "上传结果文件",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
},
|
|
}
|
|
for index := 0; index < maxWorkflowCatalogOps+12; index++ {
|
|
operators = append(operators, domain.OperatorRecord{
|
|
ID: fmt.Sprintf("operator-extra-%02d", index),
|
|
Name: fmt.Sprintf("Extra %02d", index),
|
|
Category: "MATH",
|
|
Description: "unrelated operator",
|
|
SkillDefinition: "long unused skill definition should not be sent to model",
|
|
Inputs: []domain.PortDefinition{{ID: "in-1", Name: "input", Type: domain.PortTypeJSON}},
|
|
Outputs: []domain.PortDefinition{{ID: "out-1", Name: "result", Type: domain.PortTypeJSON}},
|
|
})
|
|
}
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
body, err := io.ReadAll(request.Body)
|
|
if err != nil {
|
|
t.Fatalf("read request body: %v", err)
|
|
}
|
|
var payload chatCompletionRequest
|
|
if err := json.Unmarshal(body, &payload); err != nil {
|
|
t.Fatalf("decode request body: %v", err)
|
|
}
|
|
if payload.MaxTokens != workflowMaxTokens {
|
|
t.Fatalf("max_tokens = %d", payload.MaxTokens)
|
|
}
|
|
if payload.ResponseFormat == nil || payload.ResponseFormat.Type != "json_object" {
|
|
t.Fatalf("response_format = %#v", payload.ResponseFormat)
|
|
}
|
|
if len(payload.Messages) != 2 {
|
|
t.Fatalf("messages length = %d", len(payload.Messages))
|
|
}
|
|
userPrompt := payload.Messages[1].Content
|
|
if !strings.Contains(userPrompt, "operator-builtin-csv-to-json") {
|
|
t.Fatalf("expected matching CSV operator in prompt: %s", userPrompt)
|
|
}
|
|
if strings.Contains(userPrompt, "operator-extra-14") {
|
|
t.Fatalf("expected catalog to be compacted, got prompt: %s", userPrompt)
|
|
}
|
|
if strings.Contains(userPrompt, "long unused skill definition") {
|
|
t.Fatalf("expected skill definitions to be omitted from workflow catalog")
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(writer, `{
|
|
"choices": [
|
|
{
|
|
"message": {
|
|
"content": "{\"name\":\"CSV JSON 工作流\",\"description\":\"ok\",\"nodes\":[{\"id\":\"node-csv\",\"operatorId\":\"operator-builtin-csv-to-json\",\"position\":{\"x\":120,\"y\":80}},{\"id\":\"node-upload\",\"operatorId\":\"operator-builtin-upload\",\"position\":{\"x\":360,\"y\":80}}],\"edges\":[{\"id\":\"edge-1\",\"source\":\"node-csv\",\"target\":\"node-upload\",\"sourcePort\":\"records\",\"targetPort\":\"input\"}]}"
|
|
}
|
|
}
|
|
]
|
|
}`)
|
|
}))
|
|
defer server.Close()
|
|
|
|
item, err := NewService(server.URL, "test-key", "model-x").GenerateWorkflow(context.Background(), "CSV 数据转 JSON 后上传结果", operators)
|
|
if err != nil {
|
|
t.Fatalf("expected workflow generation to succeed, got error: %v", err)
|
|
}
|
|
if len(item.Nodes) != 2 || len(item.Edges) != 1 {
|
|
t.Fatalf("unexpected workflow shape: nodes=%d edges=%d", len(item.Nodes), len(item.Edges))
|
|
}
|
|
}
|
|
|
|
func TestNewServiceDoesNotInjectDefaultProviderRuntime(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
base string
|
|
key string
|
|
model string
|
|
reason string
|
|
}{
|
|
{name: "missing key", base: "", key: "", model: "", reason: "服务端未配置 AI_API_KEY"},
|
|
{name: "missing base url with key", base: "", key: "test-key", model: "model-x", reason: "服务端未配置 AI_API_BASE_URL"},
|
|
{name: "missing model with key", base: "https://ai.example/v1", key: "test-key", model: "", reason: "服务端未配置 AI_MODEL"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
service := NewService(tc.base, tc.key, tc.model)
|
|
status := service.Status()
|
|
if status.Enabled {
|
|
t.Fatalf("expected service disabled")
|
|
}
|
|
if status.Provider != "openai-compatible" {
|
|
t.Fatalf("expected generic provider, got %q", status.Provider)
|
|
}
|
|
if status.Reason != tc.reason {
|
|
t.Fatalf("expected reason %q, got %q", tc.reason, status.Reason)
|
|
}
|
|
})
|
|
}
|
|
}
|