feat(archied): wire generate_video end to end, gate it behind config

drainChatStream now detects a tools.MultimodalResult in a tool
result's Output and reports each URL via turn.Media(...), alongside
the existing turn.ToolCall(...) narration -- the missing link between
a tool producing media and a channel delivering it.

MinimaxConfig registers the tool only once an API key actually
resolves (registerMinimaxTool), matching web_fetch's "never advertise
a broken capability" pattern. Off by default, unlike web_fetch: this
spends real API credits per call.
This commit is contained in:
sam committed 2026-08-23 02:48:25 +10:00
1 parent 6834452870
commit e04045907c
5 files changed
+139

No files matched your search

+38
View File
@@ -51,6 +51,7 @@ import (
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/tools"
"github.com/samcharles93/archie-core/internal/tools/minimax"
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
@@ -937,6 +938,43 @@ func (b *boot) registerStandaloneTools() {
} else {
log.Info("web fetch disabled")
}
b.registerMinimaxTool(cfg, log)
}
// registerMinimaxTool registers generate_video. Off by default -- see
// MinimaxConfig's doc comment, it spends real API credits per call -- and,
// unlike web_fetch, needs a resolved credential before it can be
// registered at all: a tool advertised with no working key would fail
// every call, the same "don't advertise a broken capability" reasoning as
// disabled-by-config. Split out of registerStandaloneTools to keep that
// function's branching flat rather than nesting this tool's three failure
// modes (resolve error, empty key, registration error) inside it.
func (b *boot) registerMinimaxTool(cfg config.Config, log *slog.Logger) {
if !cfg.Tools.Minimax.IsEnabled() {
log.Info("minimax video generation disabled")
return
}
apiKey, err := cfg.Tools.Minimax.APIKey.Resolve(b.secrets)
if err != nil {
log.Warn("minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err)
return
}
if apiKey == "" {
log.Warn("minimax video generation enabled but no API key is configured; tool not registered")
return
}
entry := minimax.Tool(minimax.Config{Enabled: true, APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
if entry == nil {
return
}
if err := b.toolReg.Register(*entry); err != nil {
log.Warn("generate_video registration failed", "err", err)
return
}
log.Info("minimax video generation enabled")
}
// buildDaemon constructs the daemon and hands the dashboard the handles
+38
View File
@@ -22,6 +22,10 @@ func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
s.events = append(s.events, "tool:"+event.Name+":"+event.Parameters+":"+event.Summary())
}
func (s *recordingStream) Media(event gateway.MediaEvent) {
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type)
}
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
ch := make(chan core.StreamPart, len(parts))
for _, part := range parts {
@@ -86,6 +90,40 @@ func TestDrainChatStream(t *testing.T) {
},
wantEvents: nil,
},
{
// A tool's output carrying a tools.MultimodalResult envelope
// must report both the ordinary tool-call event (so the turn
// narrates what ran) and a Media event per URL, so the
// channel has something to actually deliver.
name: "a tool result carrying MultimodalResult URLs reports Media",
parts: []core.StreamPart{
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "generate_video"}},
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
ToolCallID: "1",
ToolName: "generate_video",
Output: `{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
}},
},
wantEvents: []string{
`tool:generate_video::{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
"media:generate_video:video",
},
},
{
// An ordinary JSON tool result that happens to be a JSON
// object but isn't a MultimodalResult (no is_multimodal, or
// false) must not be misread as carrying media.
name: "a plain JSON tool result reports no Media event",
parts: []core.StreamPart{
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "read_file"}},
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
ToolCallID: "1",
ToolName: "read_file",
Output: `{"path":"a.txt","content":"hello"}`,
}},
},
wantEvents: []string{`tool:read_file::{"path":"a.txt","content":"hello"}`},
},
}
for _, tc := range tests {
+27
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
@@ -330,11 +331,37 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
Output: part.ToolResult.Output,
Err: part.ToolResult.Error,
})
for _, ref := range multimodalMediaRefs(part.ToolResult.Output) {
turn.Media(gateway.MediaEvent{
ToolName: name,
Attachment: gateway.MediaAttachment{Type: ref.Type, URL: ref.URL},
})
}
}
}
return sb.String()
}
// multimodalMediaRefs extracts the URLs a tool result carries when its
// output is a tools.MultimodalResult, so drainChatStream can report each as
// a gateway.MediaEvent alongside the ordinary ToolCall event.
//
// output is decoded with json.Unmarshal directly rather than checked
// against a schema first: most tool output is plain text or unrelated
// JSON, and unmarshalling into MultimodalResult simply leaves IsMultimodal
// false for anything that doesn't match, which is exactly "not multimodal"
// -- no separate detection step earns its keep here.
func multimodalMediaRefs(output string) []tools.MediaRef {
if output == "" {
return nil
}
var result tools.MultimodalResult
if err := json.Unmarshal([]byte(output), &result); err != nil || !result.IsMultimodal {
return nil
}
return result.URLs
}
// chatTitleGenerator proposes session titles through the chat model. It
// is a pure proposal: it persists nothing, so a failed or slow title
// call can never corrupt conversation history or fail the turn. The
+13
View File
@@ -279,3 +279,16 @@ gate_max_failures = 5
# # dial time rather than on the URL string, so a public name that resolves to
# # a private address is refused too. Turn this on only for a homelab endpoint
# # you intend Archie to reach.
# The generate_video tool: text-to-video via MiniMax's API. Off by default,
# unlike web_fetch -- this spends real API credits per call, so it needs an
# explicit opt-in and a working key before it is registered at all.
# [tools.minimax]
# enabled = true
# api_key = { engine = "env", key = "MINIMAX_API_KEY" }
# base_url = "" # override the API host; leave unset in normal operation
# # Generation is synchronous from the model's point of view: the tool call
# # blocks until MiniMax reports the video ready or failed, which can take
# # several minutes. The result is a hosted URL delivered through whatever
# # channel supports gateway.MediaSender (Telegram today); channels without
# # that support get the URL as a plain link in the reply text instead.
+23
View File
@@ -349,11 +349,34 @@ type WebFetchConfig struct {
// absent setting as enabled.
func (c WebFetchConfig) IsEnabled() bool { return c.Enabled == nil || *c.Enabled }
// MinimaxConfig controls the generate_video tool
// (internal/tools/minimax). Unlike WebFetchConfig, absent is disabled, not
// enabled: web_fetch needs no credential and is safe as a silent default,
// while this tool spends real API credits per call, so it stays off until
// an operator explicitly turns it on and supplies a key.
type MinimaxConfig struct {
// Enabled advertises the tool. Defaults to false (see the type doc).
Enabled bool `toml:"enabled" yaml:"enabled" json:"enabled"`
// APIKey authenticates against the MiniMax API.
APIKey secret.SecretRef `toml:"api_key" yaml:"api_key" json:"-"`
// BaseURL overrides the API host. Empty uses the client's built-in
// default. Exists for a proxy or a self-hosted-compatible endpoint,
// not expected to be set in normal operation.
BaseURL string `toml:"base_url" yaml:"base_url" json:"base_url,omitempty"`
}
// IsEnabled reports whether the video-generation tool should be
// registered.
func (c MinimaxConfig) IsEnabled() bool { return c.Enabled }
// ToolsConfig holds MCP server and tool policy configuration.
type ToolsConfig struct {
MCPServers []MCPServer `toml:"mcp_servers" yaml:"mcp_servers" json:"mcp_servers"`
Policy ToolPolicy `toml:"tool_policy" yaml:"tool_policy" json:"tool_policy"`
WebFetch WebFetchConfig `toml:"web_fetch" yaml:"web_fetch" json:"web_fetch"`
Minimax MinimaxConfig `toml:"minimax" yaml:"minimax" json:"minimax"`
}
// Config is the daemon configuration.