Files
zhaoyupeng.elqbandTRAE CLI 81deca8c8e feat: initialize Eino examples and ADK applications
Include chat, weather, image, travel, streaming ASR and text examples,
shared model initialization, documentation, and existing tests.

Co-authored-by: TRAE CLI <traecli@bytedance.com>
2026-11-19 11:08:03 +08:00

106 lines
4.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
"time"
"example.com/ueino/pkg/model"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestTerminalWeatherAndMemory(t *testing.T) {
previousTransport := http.DefaultTransport
weatherCalls := 0
today := time.Now().In(time.FixedZone("Asia/Shanghai", 8*60*60)).Format(time.DateOnly)
http.DefaultTransport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
var body string
switch r.URL.Host {
case "geocoding-api.open-meteo.com":
if r.URL.Query().Get("name") != "北京" {
t.Errorf("地点查询错误: %s", r.URL)
}
body = `{"results":[{"name":"北京","country":"中国","latitude":39.9,"longitude":116.4}]}`
case "api.open-meteo.com":
if r.URL.Query().Get("start_date") != today || r.URL.Query().Get("end_date") != today {
t.Errorf("默认日期错误: %s", r.URL)
}
body = fmt.Sprintf(`{"daily":{"time":[%q],"weather_code":[0],"temperature_2m_min":[10],"temperature_2m_max":[20],"precipitation_probability_max":[0]}}`, today)
default:
return previousTransport.RoundTrip(r)
}
weatherCalls++
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil
})
t.Cleanup(func() { http.DefaultTransport = previousTransport })
var step atomic.Int32
requests := make(chan model.ChatCompletionRequest, 4)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var request model.ChatCompletionRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
t.Error(err)
}
requests <- request
w.Header().Set("Content-Type", "text/event-stream")
switch step.Add(1) {
case 1:
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"weather-1\",\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"arguments\":\"{\\\"location\\\":\\\"北京\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n\ndata: [DONE]\n\n")
case 3:
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"未完成\"}}]}\n\ndata: invalid-json\n\n")
default:
fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"北京晴天\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
}
}))
defer server.Close()
stdin := os.Stdin
originalURL, originalModel, originalKey, originalMax := baseURL, modelName, apiKeyEnv, maxToken
reader, writer, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer reader.Close()
if _, err := writer.WriteString("北京天气\n中断\n继续\n"); err != nil {
t.Fatal(err)
}
writer.Close()
os.Stdin = reader
baseURL, modelName, apiKeyEnv, maxToken = server.URL, "test-model", "TEST_WEATHER_KEY", 4096
defer func() {
os.Stdin = stdin
baseURL, modelName, apiKeyEnv, maxToken = originalURL, originalModel, originalKey, originalMax
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
run(ctx)
if weatherCalls != 2 || step.Load() != 4 {
t.Fatalf("调用次数不正确: 天气 %d,模型 %d", weatherCalls, step.Load())
}
first, second, third, fourth := <-requests, <-requests, <-requests, <-requests
if len(first.Tools) != 1 || len(second.Messages) != 4 || second.Messages[2].ToolCalls[0].ID != "weather-1" || second.Messages[3].ToolCallID != "weather-1" {
t.Fatalf("工具调用续接不正确: %+v", second)
}
var forecast model.WeatherForecast
if err := json.Unmarshal([]byte(second.Messages[3].Content.(string)), &forecast); err != nil || len(forecast.Days) != 1 || forecast.Days[0].Date != today || forecast.Days[0].Condition != "晴" {
t.Fatalf("天气结果不正确: %+v,%v", forecast, err)
}
for _, request := range []model.ChatCompletionRequest{third, fourth} {
if len(request.Messages) != 4 || request.Messages[1].Content != "北京天气" || request.Messages[2].Content != "北京晴天" {
t.Fatalf("只应保存用户问题和最终回答: %+v", request.Messages)
}
}
if fourth.Messages[3].Content != "继续" {
t.Fatalf("流失败后未继续: %+v", fourth)
}
}