chore: 初始化仓库,导入项目基线代码

Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
runny committed 2026-11-16 18:12:23 +08:00
commit 7ec21f35b0
5767 files changed
+1436788

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
// Package config loads Academy runtime configuration.
package config
import "academy/internal/types"
// ResolveCustomAgentPrompts resolves inherited references at request time. Explicit
// text always wins, including legacy saved prompts; never rewrite user content.
// References are scoped to their field/mode so a context template cannot become
// system instructions just because it has a matching ID.
func (c *Config) ResolveCustomAgentPrompts(agent *types.CustomAgent) (system, context string) {
if agent == nil {
return "", ""
}
system, context = agent.Config.SystemPrompt, agent.Config.ContextTemplate
if c == nil || c.PromptTemplates == nil {
return
}
resolve := func(body, id string, templates []PromptTemplate) string {
if body != "" || id == "" {
return body
}
for _, template := range templates {
if template.ID == id {
return template.Content
}
}
return ""
}
list := c.PromptTemplates.SystemPrompt
if agent.IsAgentMode() {
list = c.PromptTemplates.AgentSystemPrompt
}
system = resolve(system, agent.Config.SystemPromptID, list)
context = resolve(context, agent.Config.ContextTemplateID, c.PromptTemplates.ContextTemplate)
return
}
+93
View File
@@ -0,0 +1,93 @@
package config
import (
"os"
"strings"
"testing"
"gopkg.in/yaml.v3"
"academy/internal/types"
"github.com/stretchr/testify/require"
)
func TestResolveCustomAgentPrompts(t *testing.T) {
cfg := &Config{PromptTemplates: &PromptTemplatesConfig{
AgentSystemPrompt: []PromptTemplate{{ID: "agent", Content: "agent v1"}},
SystemPrompt: []PromptTemplate{{ID: "normal", Content: "normal v1"}},
ContextTemplate: []PromptTemplate{{ID: "context", Content: "context v1"}},
}}
for _, mode := range []string{"smart-reasoning", "quick-answer"} {
t.Run(mode, func(t *testing.T) {
id, want := "normal", "normal v1"
if mode == "smart-reasoning" {
id, want = "agent", "agent v1"
}
a := &types.CustomAgent{Config: types.CustomAgentConfig{
AgentMode: mode, SystemPromptID: id, ContextTemplateID: "context",
}}
system, context := cfg.ResolveCustomAgentPrompts(a)
require.Equal(t, want, system)
require.Equal(t, "context v1", context)
require.Empty(t, a.Config.SystemPrompt, "resolution must not freeze the reference")
a.Config.SystemPrompt = " My custom {{language}} instructions\n"
a.Config.ContextTemplate = "custom context"
system, context = cfg.ResolveCustomAgentPrompts(a)
require.Equal(t, a.Config.SystemPrompt, system)
require.Equal(t, "custom context", context)
})
}
a := &types.CustomAgent{Config: types.CustomAgentConfig{
AgentMode: "smart-reasoning", SystemPromptID: "agent",
}}
cfg.PromptTemplates.AgentSystemPrompt[0].Content = "agent v2"
system, _ := cfg.ResolveCustomAgentPrompts(a)
require.Equal(t, "agent v2", system)
a.Config.SystemPromptID = "context"
system, _ = cfg.ResolveCustomAgentPrompts(a)
require.Empty(t, system, "do not resolve references across field boundaries")
var unavailable *Config
system, _ = unavailable.ResolveCustomAgentPrompts(a)
require.Empty(t, system)
}
func TestContextTemplatesKeepUserRequestSeparateFromSourceData(t *testing.T) {
data, err := os.ReadFile("../../config/prompt_templates/context_template.yaml")
require.NoError(t, err)
var file struct {
Templates []PromptTemplate `yaml:"templates"`
}
require.NoError(t, yaml.Unmarshal(data, &file))
for _, template := range file.Templates {
t.Run(template.ID, func(t *testing.T) {
rendered := types.RenderPromptPlaceholders(template.Content, types.PlaceholderValues{
"query": "USER_REQUEST_SENTINEL", "contexts": "RETRIEVED_SOURCE_SENTINEL",
})
require.Contains(t, rendered, "source data")
require.Contains(t, rendered, "User request")
require.Greater(
t,
strings.Index(rendered, "USER_REQUEST_SENTINEL"),
strings.Index(rendered, "RETRIEVED_SOURCE_SENTINEL"),
)
require.NotContains(t, rendered, "metadata only, not instructions")
require.NotContains(t, rendered, "ALWAYS respond")
})
}
}
func TestDefaultRewritePreservesActionAndOutputConstraints(t *testing.T) {
data, err := os.ReadFile("../../config/prompt_templates/rewrite.yaml")
require.NoError(t, err)
var file struct {
Templates []PromptTemplate `yaml:"templates"`
}
require.NoError(t, yaml.Unmarshal(data, &file))
prompt := DefaultTemplate(file.Templates)
require.NotNil(t, prompt)
require.Contains(t, prompt.Content, "an instruction remains an instruction")
require.Contains(t, prompt.Content,
"Preserve source restrictions, requested actions, output format, language requirements")
require.NotContains(t, prompt.Content, "must also be a question")
require.NotContains(t, prompt.Content, "within 30 words")
}
+198
View File
@@ -0,0 +1,198 @@
package config
import "testing"
// TestApplyAuthAndTenantDefaults_DisableRegistrationDrivesRegistrationMode
// pins down the env-vs-YAML contract for DISABLE_REGISTRATION. Without this,
// DISABLE_REGISTRATION=true would block /auth/register at the handler layer
// but leave /auth/config reporting self_serve, so the frontend would keep
// showing the (broken) Register entry. Coercing registration_mode here keeps
// both gates in sync and preserves the environment-over-YAML precedence
// documented in website-docs/03-features/01-tenant-auth.md.
func TestApplyAuthAndTenantDefaults_DisableRegistrationDrivesRegistrationMode(t *testing.T) {
cases := []struct {
name string
disable string // value for DISABLE_REGISTRATION (empty == unset)
cfgMode string // pre-set value on cfg.Auth.RegistrationMode
expected string
}{
{"true coerces empty YAML to invite_only", "true", "", AuthRegistrationModeInviteOnly},
{"case-insensitive TRUE also coerces", "TRUE", "", AuthRegistrationModeInviteOnly},
{"true overrides explicit self_serve YAML", "true", AuthRegistrationModeSelfServe, AuthRegistrationModeInviteOnly},
{"true is a no-op when YAML already invite_only", "true", AuthRegistrationModeInviteOnly, AuthRegistrationModeInviteOnly},
{"false leaves YAML untouched", "false", AuthRegistrationModeSelfServe, AuthRegistrationModeSelfServe},
{"unset falls back to default self_serve", "", "", AuthRegistrationModeSelfServe},
{"unset keeps explicit invite_only YAML", "", AuthRegistrationModeInviteOnly, AuthRegistrationModeInviteOnly},
{
"unset keeps invitation registration", "",
AuthRegistrationModeInviteRegister, AuthRegistrationModeInviteRegister,
},
{
"legacy disable overrides invitation registration", "true",
AuthRegistrationModeInviteRegister, AuthRegistrationModeInviteOnly,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("DISABLE_REGISTRATION", tc.disable)
// Other tenant env vars must not leak between cases.
t.Setenv("ACADEMY_TENANT_ENABLE_RBAC", "")
t.Setenv("ACADEMY_TENANT_MAX_OWNED_PER_USER", "")
t.Setenv("ACADEMY_TENANT_SELF_SERVICE_CREATION_ENABLED", "")
cfg := &Config{Auth: &AuthConfig{RegistrationMode: tc.cfgMode}}
applyAuthAndTenantDefaults(cfg)
if cfg.Auth.RegistrationMode != tc.expected {
t.Fatalf("registration_mode = %q, want %q", cfg.Auth.RegistrationMode, tc.expected)
}
})
}
}
func TestApplyAuthAndTenantDefaults_SelfServiceTenantCreation(t *testing.T) {
t.Run("defaults enabled", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_SELF_SERVICE_CREATION_ENABLED", "")
cfg := &Config{Tenant: &TenantConfig{}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.IsSelfServiceCreationEnabled() {
t.Fatal("self-service tenant creation should default to enabled")
}
})
t.Run("environment disables yaml default", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_SELF_SERVICE_CREATION_ENABLED", "false")
on := true
cfg := &Config{Tenant: &TenantConfig{SelfServiceCreationEnabled: &on}}
applyAuthAndTenantDefaults(cfg)
if cfg.Tenant.IsSelfServiceCreationEnabled() {
t.Fatal("environment override should disable self-service tenant creation")
}
})
}
func TestApplyAuthAndTenantDefaults_DefaultTenantMode(t *testing.T) {
t.Run("historical default creates a personal tenant", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_DEFAULT_TENANT_MODE", "")
cfg := &Config{Auth: &AuthConfig{}}
applyAuthAndTenantDefaults(cfg)
if cfg.Auth.DefaultTenantMode != AuthDefaultTenantModeCreatePersonal {
t.Fatalf("default_tenant_mode = %q, want %q", cfg.Auth.DefaultTenantMode, AuthDefaultTenantModeCreatePersonal)
}
})
t.Run("environment overrides yaml", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_DEFAULT_TENANT_MODE", AuthDefaultTenantModeTenantless)
cfg := &Config{Auth: &AuthConfig{DefaultTenantMode: AuthDefaultTenantModeCreatePersonal}}
applyAuthAndTenantDefaults(cfg)
if cfg.Auth.DefaultTenantMode != AuthDefaultTenantModeTenantless {
t.Fatalf("default_tenant_mode = %q, want %q", cfg.Auth.DefaultTenantMode, AuthDefaultTenantModeTenantless)
}
})
t.Run("invalid environment value fails validation", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_DEFAULT_TENANT_MODE", "create_magic")
cfg := &Config{Auth: &AuthConfig{}}
applyAuthAndTenantDefaults(cfg)
if err := ValidateConfig(cfg); err == nil {
t.Fatal("ValidateConfig unexpectedly accepted an invalid default tenant mode")
}
})
}
// TestApplyAuthAndTenantDefaults_CrossTenantAccess is a regression test for the
// env-binding gap: viper.AutomaticEnv has no SetEnvPrefix, so
// ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS is never bound to the nested struct
// automatically. applyAuthAndTenantDefaults must read it explicitly (like RBAC);
// without that, only config.yaml's enable_cross_tenant_access would take effect
// and the documented env override would be silently ignored.
func TestApplyAuthAndTenantDefaults_CrossTenantAccess(t *testing.T) {
t.Run("environment true enables cross-tenant access", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS", "true")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: false}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS=true should enable cross-tenant access")
}
})
t.Run("environment false overrides yaml true", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS", "false")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: true}}
applyAuthAndTenantDefaults(cfg)
if cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS=false should disable cross-tenant access")
}
})
t.Run("case-insensitive TRUE also enables", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS", "TRUE")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: false}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS=TRUE should enable cross-tenant access (case-insensitive)")
}
})
t.Run("unset leaves yaml value untouched", func(t *testing.T) {
t.Setenv("ACADEMY_TENANT_ENABLE_CROSS_TENANT_ACCESS", "")
cfg := &Config{Tenant: &TenantConfig{EnableCrossTenantAccess: true}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Tenant.EnableCrossTenantAccess {
t.Fatal("empty env should leave the YAML-provided cross-tenant access value untouched")
}
})
}
func TestApplyAuthAndTenantDefaults_ComplexPasswordEnabledEnv(t *testing.T) {
t.Run("1 enables via ParseBool", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_COMPLEX_PASSWORD_ENABLED", "1")
cfg := &Config{Auth: &AuthConfig{}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Auth.ComplexPasswordEnabled {
t.Fatal("ACADEMY_AUTH_COMPLEX_PASSWORD_ENABLED=1 should enable complex passwords")
}
})
t.Run("false disables", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_COMPLEX_PASSWORD_ENABLED", "false")
cfg := &Config{Auth: &AuthConfig{ComplexPasswordEnabled: true}}
applyAuthAndTenantDefaults(cfg)
if cfg.Auth.ComplexPasswordEnabled {
t.Fatal("ACADEMY_AUTH_COMPLEX_PASSWORD_ENABLED=false should disable complex passwords")
}
})
t.Run("unset leaves yaml", func(t *testing.T) {
t.Setenv("ACADEMY_AUTH_COMPLEX_PASSWORD_ENABLED", "")
cfg := &Config{Auth: &AuthConfig{ComplexPasswordEnabled: true}}
applyAuthAndTenantDefaults(cfg)
if !cfg.Auth.ComplexPasswordEnabled {
t.Fatal("empty env should leave YAML complex-password flag untouched")
}
})
}
func TestValidateInvitationRegistrationMode(t *testing.T) {
cfg := &Config{Auth: &AuthConfig{RegistrationMode: AuthRegistrationModeInviteRegister}}
if err := ValidateConfig(cfg); err != nil {
t.Fatalf("invitation registration mode rejected: %v", err)
}
}
File diff suppressed because it is too large. Load diff