refactor(tools): back generate_video with ai-sdk's minimax video provider
Delete the hand-rolled MiniMax HTTP/poll client; ai-sdk v0.1.43 already ships provider/minimax implementing video.Provider with the same endpoints, defaults and poll semantics. The tool now takes a video.Provider and the composition root builds it, matching the transcription and embedding capabilities.
This commit is contained in:
3 files changed
+34
-308
No files matched your search
@@ -8,11 +8,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/samcharles93/ai-sdk/provider/minimax"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
"github.com/samcharles93/archie-core/internal/agentexec/modelloop"
|
||||||
"github.com/samcharles93/archie-core/internal/config"
|
"github.com/samcharles93/archie-core/internal/config"
|
||||||
"github.com/samcharles93/archie-core/internal/plugin"
|
"github.com/samcharles93/archie-core/internal/plugin"
|
||||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||||
"github.com/samcharles93/archie-core/internal/tools/minimax"
|
minimaxtool "github.com/samcharles93/archie-core/internal/tools/minimax"
|
||||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||||
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
||||||
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp"
|
||||||
@@ -123,7 +125,13 @@ func (b *server) registerMinimaxTool(cfg config.Config, log *slog.Logger) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := minimax.Tool(minimax.Config{Enabled: true, APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
|
provider, err := minimax.New(minimax.Config{APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("minimax video generation enabled but the provider could not be built; tool not registered", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := minimaxtool.Tool(provider)
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,292 +0,0 @@
|
|||||||
// Package minimax generates video with the MiniMax API. GenerateAndWait
|
|
||||||
// submits a job and polls until it finishes or MaxWait elapses.
|
|
||||||
package minimax
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// defaultBaseURL is MiniMax's production API host. Overridable for
|
|
||||||
// tests and for an operator pointed at a proxy.
|
|
||||||
defaultBaseURL = "https://api.minimax.io"
|
|
||||||
|
|
||||||
// defaultModel is the only model documented for this endpoint.
|
|
||||||
defaultModel = "MiniMax-H3"
|
|
||||||
|
|
||||||
// defaultResolution and defaultDuration match the middle of MiniMax's
|
|
||||||
// documented ranges (768P/2K; 4-15s) rather than the extremes, so an
|
|
||||||
// unconfigured request is neither the cheapest-lowest-quality nor the
|
|
||||||
// most expensive option by default.
|
|
||||||
defaultResolution = "768P"
|
|
||||||
defaultDuration = 6
|
|
||||||
|
|
||||||
// defaultPollInterval and defaultMaxWait bound one generation. MiniMax
|
|
||||||
// documents no expected completion time, so these are a starting
|
|
||||||
// guess: frequent enough to feel responsive, bounded low enough that
|
|
||||||
// a stuck job fails a chat turn in minutes rather than hanging it.
|
|
||||||
defaultPollInterval = 5 * time.Second
|
|
||||||
defaultMaxWait = 5 * time.Minute
|
|
||||||
|
|
||||||
// defaultTimeout bounds one HTTP call (create or a single query), not
|
|
||||||
// the whole generate-and-wait loop -- that is MaxWait.
|
|
||||||
defaultTimeout = 30 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config controls the client. The zero value is disabled.
|
|
||||||
type Config struct {
|
|
||||||
// Enabled advertises the tool. When false, Tool returns nil.
|
|
||||||
Enabled bool
|
|
||||||
|
|
||||||
// APIKey authenticates every request as a Bearer token.
|
|
||||||
APIKey string
|
|
||||||
|
|
||||||
// BaseURL overrides the API host. Empty uses defaultBaseURL.
|
|
||||||
BaseURL string
|
|
||||||
|
|
||||||
// Timeout bounds one HTTP call. Zero uses defaultTimeout.
|
|
||||||
Timeout time.Duration
|
|
||||||
|
|
||||||
// PollInterval is the delay between status queries. Zero uses
|
|
||||||
// defaultPollInterval.
|
|
||||||
PollInterval time.Duration
|
|
||||||
|
|
||||||
// MaxWait bounds the whole submit-and-poll loop. Zero uses
|
|
||||||
// defaultMaxWait.
|
|
||||||
MaxWait time.Duration
|
|
||||||
|
|
||||||
// Sleep delays between polls. Nil uses time.Sleep. Tests inject a
|
|
||||||
// no-op or counting stand-in so a poll loop that spans MaxWait does
|
|
||||||
// not actually take that long to run.
|
|
||||||
Sleep func(time.Duration)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Client generates video through the MiniMax API.
|
|
||||||
type Client struct {
|
|
||||||
cfg Config
|
|
||||||
http *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// New builds a Client, filling unset Config fields with their defaults.
|
|
||||||
func New(cfg Config) *Client {
|
|
||||||
if cfg.BaseURL == "" {
|
|
||||||
cfg.BaseURL = defaultBaseURL
|
|
||||||
}
|
|
||||||
if cfg.Timeout <= 0 {
|
|
||||||
cfg.Timeout = defaultTimeout
|
|
||||||
}
|
|
||||||
if cfg.PollInterval <= 0 {
|
|
||||||
cfg.PollInterval = defaultPollInterval
|
|
||||||
}
|
|
||||||
if cfg.MaxWait <= 0 {
|
|
||||||
cfg.MaxWait = defaultMaxWait
|
|
||||||
}
|
|
||||||
if cfg.Sleep == nil {
|
|
||||||
cfg.Sleep = time.Sleep
|
|
||||||
}
|
|
||||||
return &Client{cfg: cfg, http: &http.Client{Timeout: cfg.Timeout}}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateRequest is one text-to-video job. Image-to-video (image_url
|
|
||||||
// content items) is not implemented: the first consumer is text-only
|
|
||||||
// generation, and adding image references is a schema-and-routing change
|
|
||||||
// this can grow into rather than needing up front.
|
|
||||||
type GenerateRequest struct {
|
|
||||||
// Prompt is the required text description (MiniMax caps this at 7000
|
|
||||||
// characters; that is not separately enforced here, the API rejects
|
|
||||||
// an oversized prompt with a 400 the caller sees).
|
|
||||||
Prompt string
|
|
||||||
|
|
||||||
// Resolution is "768P" or "2K". Empty uses defaultResolution.
|
|
||||||
Resolution string
|
|
||||||
|
|
||||||
// Duration is the clip length in seconds, 4-15. Zero uses
|
|
||||||
// defaultDuration.
|
|
||||||
Duration int
|
|
||||||
|
|
||||||
// Ratio is the aspect ratio (see the package doc's API reference for
|
|
||||||
// the allowed set). Empty uses "adaptive", MiniMax's own default.
|
|
||||||
Ratio string
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateAndWait submits req and blocks until MiniMax reports a terminal
|
|
||||||
// status, returning the hosted video URL on success.
|
|
||||||
func (c *Client) GenerateAndWait(ctx context.Context, req GenerateRequest) (string, error) {
|
|
||||||
taskID, err := c.create(ctx, req)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// elapsed is tracked from the injected Sleep calls rather than a real
|
|
||||||
// time.Now() deadline, so a test that fakes Sleep as an instant
|
|
||||||
// counter also makes this loop terminate instantly instead of
|
|
||||||
// spinning in real wall-clock time until an unfaked clock catches up.
|
|
||||||
var elapsed time.Duration
|
|
||||||
for {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return "", fmt.Errorf("minimax: task %s: %w", taskID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
task, err := c.query(ctx, taskID)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch task.Status {
|
|
||||||
case "succeeded":
|
|
||||||
if task.Content.URL == "" {
|
|
||||||
return "", fmt.Errorf("minimax: task %s succeeded but reported no video url", taskID)
|
|
||||||
}
|
|
||||||
return task.Content.URL, nil
|
|
||||||
case "failed", "cancelled":
|
|
||||||
return "", fmt.Errorf("minimax: task %s %s: %s", taskID, task.Status, task.errorMessage())
|
|
||||||
}
|
|
||||||
|
|
||||||
if elapsed >= c.cfg.MaxWait {
|
|
||||||
return "", fmt.Errorf("minimax: task %s timed out after %v waiting for completion (last status %q)",
|
|
||||||
taskID, c.cfg.MaxWait, task.Status)
|
|
||||||
}
|
|
||||||
c.cfg.Sleep(c.cfg.PollInterval)
|
|
||||||
elapsed += c.cfg.PollInterval
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type createResponse struct {
|
|
||||||
TaskID string `json:"task_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// create submits req and returns the task ID MiniMax assigned.
|
|
||||||
func (c *Client) create(ctx context.Context, req GenerateRequest) (string, error) {
|
|
||||||
resolution := req.Resolution
|
|
||||||
if resolution == "" {
|
|
||||||
resolution = defaultResolution
|
|
||||||
}
|
|
||||||
duration := req.Duration
|
|
||||||
if duration == 0 {
|
|
||||||
duration = defaultDuration
|
|
||||||
}
|
|
||||||
ratio := req.Ratio
|
|
||||||
if ratio == "" {
|
|
||||||
ratio = "adaptive"
|
|
||||||
}
|
|
||||||
|
|
||||||
body := map[string]any{
|
|
||||||
"model": defaultModel,
|
|
||||||
"resolution": resolution,
|
|
||||||
"duration": duration,
|
|
||||||
"ratio": ratio,
|
|
||||||
"content": []map[string]string{
|
|
||||||
{"type": "text", "text": req.Prompt},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var out createResponse
|
|
||||||
if err := c.doJSON(ctx, http.MethodPost, "/v2/video_generation", body, &out); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if out.TaskID == "" {
|
|
||||||
return "", errors.New("minimax: create response carried no task_id")
|
|
||||||
}
|
|
||||||
return out.TaskID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// taskStatus is the polled state of one generation job.
|
|
||||||
type taskStatus struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
Error *struct {
|
|
||||||
Code string `json:"code"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
} `json:"error,omitempty"`
|
|
||||||
Content struct {
|
|
||||||
URL string `json:"url"`
|
|
||||||
} `json:"content"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// errorMessage reports the platform's own failure detail, falling back to
|
|
||||||
// a placeholder when MiniMax reported a terminal failure with no error
|
|
||||||
// object -- which the docs don't rule out.
|
|
||||||
func (t taskStatus) errorMessage() string {
|
|
||||||
if t.Error == nil || t.Error.Message == "" {
|
|
||||||
return "no error detail reported"
|
|
||||||
}
|
|
||||||
return t.Error.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
type queryResponse struct {
|
|
||||||
Task taskStatus `json:"task"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// query fetches the current status of taskID.
|
|
||||||
func (c *Client) query(ctx context.Context, taskID string) (taskStatus, error) {
|
|
||||||
var out queryResponse
|
|
||||||
path := "/v2/query/video_generation/" + taskID
|
|
||||||
if err := c.doJSON(ctx, http.MethodGet, path, nil, &out); err != nil {
|
|
||||||
return taskStatus{}, err
|
|
||||||
}
|
|
||||||
return out.Task, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// apiError is the documented error envelope MiniMax returns on a non-200
|
|
||||||
// response.
|
|
||||||
type apiError struct {
|
|
||||||
Error struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
} `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// doJSON issues one request and decodes a 200 response into out. A non-200
|
|
||||||
// response is parsed as apiError and returned as an error carrying
|
|
||||||
// MiniMax's own message, so a create/query failure is never reported as a
|
|
||||||
// bare HTTP status.
|
|
||||||
func (c *Client) doJSON(ctx context.Context, method, path string, body, out any) error {
|
|
||||||
var reader io.Reader
|
|
||||||
if body != nil {
|
|
||||||
data, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("minimax: encode request: %w", err)
|
|
||||||
}
|
|
||||||
reader = bytes.NewReader(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, strings.TrimRight(c.cfg.BaseURL, "/")+path, reader)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("minimax: build request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
|
||||||
if body != nil {
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.http.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("minimax: request failed: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
data, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("minimax: read response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
var apiErr apiError
|
|
||||||
if err := json.Unmarshal(data, &apiErr); err == nil && apiErr.Error.Message != "" {
|
|
||||||
return fmt.Errorf("minimax: %s (http %d)", apiErr.Error.Message, resp.StatusCode)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("minimax: request failed with http %d: %s", resp.StatusCode, string(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(data, out); err != nil {
|
|
||||||
return fmt.Errorf("minimax: decode response: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,24 +1,29 @@
|
|||||||
|
// Package minimax exposes MiniMax video generation as the generate_video
|
||||||
|
// tool. The HTTP and polling behaviour lives in ai-sdk's
|
||||||
|
// provider/minimax; this package only shapes the tool schema and maps a tool
|
||||||
|
// call onto video.Provider.
|
||||||
package minimax
|
package minimax
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samcharles93/ai-sdk/video"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/tools"
|
"github.com/samcharles93/archie-core/internal/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToolName is the registry name of the video-generation tool.
|
// ToolName is the registry name of the video-generation tool.
|
||||||
const ToolName = "generate_video"
|
const ToolName = "generate_video"
|
||||||
|
|
||||||
// Tool returns the generate_video tool, or nil when disabled.
|
// Tool returns the generate_video tool backed by p, or nil when p is nil.
|
||||||
func Tool(cfg Config) *tools.ToolEntry {
|
func Tool(p video.Provider) *tools.ToolEntry {
|
||||||
if !cfg.Enabled {
|
if p == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
client := New(cfg)
|
|
||||||
|
|
||||||
return &tools.ToolEntry{
|
return &tools.ToolEntry{
|
||||||
Name: ToolName,
|
Name: ToolName,
|
||||||
Toolset: "media",
|
Toolset: "media",
|
||||||
@@ -48,7 +53,7 @@ func Tool(cfg Config) *tools.ToolEntry {
|
|||||||
"ratio": map[string]any{
|
"ratio": map[string]any{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": []any{"adaptive", "21:9", "16:9", "4:3", "1:1", "3:4", "9:16"},
|
"enum": []any{"adaptive", "21:9", "16:9", "4:3", "1:1", "3:4", "9:16"},
|
||||||
"description": "Aspect ratio. Defaults to adaptive.",
|
"description": "Aspect ratio. Defaults to 16:9.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"required": []any{"prompt"},
|
"required": []any{"prompt"},
|
||||||
@@ -60,25 +65,30 @@ func Tool(cfg Config) *tools.ToolEntry {
|
|||||||
}
|
}
|
||||||
resolution, _ := input["resolution"].(string)
|
resolution, _ := input["resolution"].(string)
|
||||||
ratio, _ := input["ratio"].(string)
|
ratio, _ := input["ratio"].(string)
|
||||||
duration := 0
|
|
||||||
if d, ok := input["duration"].(float64); ok {
|
|
||||||
duration = int(d)
|
|
||||||
}
|
|
||||||
|
|
||||||
url, err := client.GenerateAndWait(ctx, GenerateRequest{
|
req := video.GenerateVideoRequest{
|
||||||
Prompt: prompt,
|
Prompt: prompt,
|
||||||
Resolution: resolution,
|
Resolution: resolution,
|
||||||
Duration: duration,
|
|
||||||
Ratio: ratio,
|
Ratio: ratio,
|
||||||
})
|
}
|
||||||
|
// Leave Duration empty when unset so the provider applies its own
|
||||||
|
// default; "0" would be clamped to the 4s floor instead of 6s.
|
||||||
|
if d, ok := input["duration"].(float64); ok && d > 0 {
|
||||||
|
req.Duration = strconv.Itoa(int(d))
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := p.GenerateVideo(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %w", ToolName, err)
|
return nil, fmt.Errorf("%s: %w", ToolName, err)
|
||||||
}
|
}
|
||||||
|
if len(resp.Videos) == 0 || resp.Videos[0].URL == "" {
|
||||||
|
return nil, fmt.Errorf("%s: provider returned no video", ToolName)
|
||||||
|
}
|
||||||
|
|
||||||
return tools.MultimodalResult{
|
return tools.MultimodalResult{
|
||||||
IsMultimodal: true,
|
IsMultimodal: true,
|
||||||
Summary: "Generated a video from the prompt.",
|
Summary: "Generated a video from the prompt.",
|
||||||
URLs: []tools.MediaRef{{Type: "video", URL: url}},
|
URLs: []tools.MediaRef{{Type: "video", URL: resp.Videos[0].URL}},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user