Files
yawan f7fc992f57
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
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

54 lines
1.7 KiB
Go

package cmdutil
import (
"context"
"fmt"
sdk "github.com/Tencent/WeKnora/client"
)
// ModelLister is the narrow SDK surface ResolveModelRef needs. *sdk.Client
// satisfies it.
type ModelLister interface {
ListModels(ctx context.Context) ([]sdk.Model, error)
}
// ResolveModelRef returns ref unchanged when it is already a model id (UUID);
// otherwise it resolves ref as a model NAME among models of wantType (pass ""
// to match any type) and returns that model's id. Mirrors the id-or-name policy
// of --kb so model-referencing flags accept either.
//
// Errors with resource.not_found when nothing matches, and
// input.invalid_argument when the name is ambiguous (so the caller passes an id
// instead). The empty ref is returned as-is for callers to treat as "unset".
func ResolveModelRef(ctx context.Context, lister ModelLister, ref, wantType string) (string, error) {
if ref == "" || IsKBID(ref) { // IsKBID is a generic UUID check
return ref, nil
}
models, err := lister.ListModels(ctx)
if err != nil {
return "", WrapHTTP(err, "list models to resolve %q", ref)
}
var matches []sdk.Model
for _, m := range models {
if m.Name == ref && (wantType == "" || string(m.Type) == wantType) {
matches = append(matches, m)
}
}
switch len(matches) {
case 1:
return matches[0].ID, nil
case 0:
msg := fmt.Sprintf("no model named %q", ref)
if wantType != "" {
msg = fmt.Sprintf("no %s model named %q", wantType, ref)
}
return "", NewError(CodeResourceNotFound, msg).
WithHint("discover models with `weknora model list`")
default:
return "", NewError(CodeInputInvalidArgument,
fmt.Sprintf("%q matches %d models — pass the model id instead", ref, len(matches))).
WithHint("`weknora model list` shows the ids")
}
}