Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
117 lines
3.8 KiB
Go
117 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
// userEnvReader is the slice of the repository this resolver needs. Narrowing it
|
|
// keeps the resolver testable without a database and makes the queries it
|
|
// issues per execution visible in the type.
|
|
type userEnvReader interface {
|
|
ListUserEnvVars(
|
|
ctx context.Context, tenantID uint64, p types.Principal, configID, skillID string,
|
|
) ([]*types.TenantUserEnvVar, error)
|
|
}
|
|
|
|
// userEnvResolver answers skills.SkillEnvResolver from the skill rows this turn
|
|
// already loaded plus the caller's own values.
|
|
type userEnvResolver struct {
|
|
// byName indexes the rows by the name the model addresses skills with.
|
|
byName map[string]*types.TenantSkillEntity
|
|
userEnvs userEnvReader
|
|
// tenantID and configID are the scope the rows came from, not whatever the
|
|
// context happens to hold, so a lookup cannot resolve into another
|
|
// workspace or another config.
|
|
tenantID uint64
|
|
configID string
|
|
}
|
|
|
|
// NewUserEnvResolver builds the resolver for one agent run. rows are this run's
|
|
// installed skills, already carrying their declarations and admin values; they
|
|
// may be empty, in which case only config-wide variables are resolved.
|
|
func NewUserEnvResolver(
|
|
rows []*types.TenantSkillEntity, userEnvs userEnvReader, tenantID uint64, configID string,
|
|
) *userEnvResolver {
|
|
byName := make(map[string]*types.TenantSkillEntity, len(rows))
|
|
for _, row := range rows {
|
|
if row != nil {
|
|
byName[row.Name] = row
|
|
}
|
|
}
|
|
return &userEnvResolver{
|
|
byName: byName, userEnvs: userEnvs, tenantID: tenantID, configID: configID,
|
|
}
|
|
}
|
|
|
|
// ResolveEnv layers the sources in a fixed order, each overwriting the last:
|
|
// the admin's workspace-wide skill values, then this caller's config-wide
|
|
// variables, then this caller's values for that skill. Mine beat the
|
|
// workspace's; among mine, the skill-specific one beats the config-wide one.
|
|
//
|
|
// An empty skillName resolves only the config-wide variables, which is what
|
|
// shell_exec gets when the model names no skill. An unknown name behaves the
|
|
// same way: preloaded skills reach here too and carry no declaration.
|
|
func (r *userEnvResolver) ResolveEnv(
|
|
ctx context.Context, skillName string,
|
|
) (map[string]string, []string, error) {
|
|
// The identity is the Principal from the context and nothing else. A user
|
|
// id would give every IM caller in a workspace the same synthetic account
|
|
// and therefore the same values.
|
|
principal, hasPrincipal := types.PrincipalFromContext(ctx)
|
|
canReadMine := hasPrincipal && r.userEnvs != nil
|
|
|
|
env := map[string]string{}
|
|
row := r.byName[skillName]
|
|
if row != nil {
|
|
for _, declared := range row.Envs {
|
|
// An empty admin value means unset; injecting it would make an
|
|
// unconfigured variable indistinguishable from one set to "".
|
|
if declared.Value != "" {
|
|
env[declared.Name] = declared.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
if canReadMine {
|
|
if err := r.overlayMine(ctx, principal, "", env); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if row != nil {
|
|
if err := r.overlayMine(ctx, principal, row.ID, env); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
var missing []string
|
|
if row != nil {
|
|
for _, declared := range row.Envs {
|
|
if declared.Required && env[declared.Name] == "" {
|
|
missing = append(missing, declared.Name)
|
|
}
|
|
}
|
|
}
|
|
return env, missing, nil
|
|
}
|
|
|
|
// overlayMine writes the caller's own values for one scope over env. A failure
|
|
// is never degraded to the admin value: running a skill with somebody else's
|
|
// key is worse than not running it.
|
|
func (r *userEnvResolver) overlayMine(
|
|
ctx context.Context, principal types.Principal, skillID string, env map[string]string,
|
|
) error {
|
|
owned, err := r.userEnvs.ListUserEnvVars(ctx, r.tenantID, principal, r.configID, skillID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, value := range owned {
|
|
if value == nil || value.Value == "" {
|
|
continue
|
|
}
|
|
env[value.Name] = value.Value
|
|
}
|
|
return nil
|
|
}
|