From 21aac68daf683d9eff1b25b1e472549ff67f9fe3 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Tue, 8 Oct 2024 13:54:32 -0700 Subject: [PATCH] Remove compatibility shims for imports --- .../compatibility/compatibility.go | 273 ------------------ .../compatibility/compatibility_test.go | 78 ----- runtime/plugins/plugins.go | 61 +--- 3 files changed, 13 insertions(+), 399 deletions(-) delete mode 100644 runtime/hostfunctions/compatibility/compatibility.go delete mode 100644 runtime/hostfunctions/compatibility/compatibility_test.go diff --git a/runtime/hostfunctions/compatibility/compatibility.go b/runtime/hostfunctions/compatibility/compatibility.go deleted file mode 100644 index f18e5c34c..000000000 --- a/runtime/hostfunctions/compatibility/compatibility.go +++ /dev/null @@ -1,273 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package compatibility - -import ( - "fmt" - - "github.com/hypermodeinc/modus/runtime/plugins/metadata" -) - -// This is a compatibility shim for host functions that are not referenced in the metadata. -// It is only needed to allow previous AssemblyScript SDK versions to work with the latest runtime. -// This should be removed when all plugins are updated to the latest SDK version that uses the V2 metadata format. - -func GetImportMetadataShim(fnName string) (fn *metadata.Function, err error) { - switch fnName { - case "hypermode.log": - fn = metadata.NewFunction(fnName). - WithParameter("level", "~lib/string/String"). - WithParameter("message", "~lib/string/String") - - /* Model function imports */ - - case "hypermode.lookupModel": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithResult("~lib/@hypermode/models-as/index/ModelInfo") - - case "hypermode.invokeModel": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("input", "~lib/string/String"). - WithResult("~lib/string/String | null") - - /* HTTP, GraphQL, and SQL function imports */ - - case "hypermode.httpFetch": - fn = metadata.NewFunction(fnName). - WithParameter("url", "~lib/@hypermode/functions-as/assembly/http/Request"). - WithResult("~lib/@hypermode/functions-as/assembly/http/Response") - - case "hypermode.executeGQL": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("statement", "~lib/string/String"). - WithParameter("variables", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.databaseQuery": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("dbType", "~lib/string/String"). - WithParameter("statement", "~lib/string/String"). - WithParameter("paramsJson", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/database/HostQueryResponse") - - /* Dgraph function imports */ - - case "hypermode.dgraphAlterSchema": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("schema", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.dgraphDropAll": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.dgraphDropAttr": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("attr", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.executeDQL": - fn = metadata.NewFunction(fnName). - WithParameter("hostName", "~lib/string/String"). - WithParameter("request", "~lib/@hypermode/functions-as/assembly/dgraph/Request"). - WithResult("~lib/@hypermode/functions-as/assembly/dgraph/Response") - - /* Collection function imports */ - - case "hypermode.computeDistance_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.deleteFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - case "hypermode.getNamespacesFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithResult("~lib/array/Array<~lib/string/String>") - - case "hypermode.getTextFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.getTextsFromCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithResult("~lib/map/Map<~lib/string/String,~lib/string/String>") - - case "hypermode.getVector": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/array/Array") - - case "hypermode.getLabels": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/array/Array<~lib/string/String>") - - case "hypermode.nnClassifyCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionClassificationResult") - - case "hypermode.recomputeSearchMethod_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/SearchMethodMutationResult") - - case "hypermode.searchCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespaces", "~lib/array/Array<~lib/string/String>"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.searchCollectionByVector": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespaces", "~lib/array/Array<~lib/string/String>"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("vector", "~lib/array/Array"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.upsertToCollection_v2": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("namespace", "~lib/string/String"). - WithParameter("keys", "~lib/array/Array<~lib/string/String>"). - WithParameter("texts", "~lib/array/Array<~lib/string/String>"). - WithParameter("labels", "~lib/array/Array<~lib/array/Array<~lib/string/String>>"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - /* Older collection function imports */ - - case "hypermode.computeDistance": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.computeSimilarity": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("key1", "~lib/string/String"). - WithParameter("key2", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResultObject") - - case "hypermode.deleteFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - case "hypermode.getTextFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("key", "~lib/string/String"). - WithResult("~lib/string/String") - - case "hypermode.getTextsFromCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithResult("~lib/map/Map<~lib/string/String,~lib/string/String>") - - case "hypermode.nnClassifyCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionClassificationResult") - - case "hypermode.recomputeSearchMethod": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/SearchMethodMutationResult") - - case "hypermode.searchCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("searchMethod", "~lib/string/String"). - WithParameter("text", "~lib/string/String"). - WithParameter("limit", "i32"). - WithParameter("returnText", "bool"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionSearchResult") - - case "hypermode.upsertToCollection": - fn = metadata.NewFunction(fnName). - WithParameter("collection", "~lib/string/String"). - WithParameter("keys", "~lib/array/Array<~lib/string/String>"). - WithParameter("texts", "~lib/array/Array<~lib/string/String>"). - WithResult("~lib/@hypermode/functions-as/assembly/collections/CollectionMutationResult") - - /* Legacy model function imports */ - - case "hypermode.invokeClassifier": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("sentenceMap", "~lib/map/Map<~lib/string/String,~lib/string/String>"). - WithResult("~lib/map/Map<~lib/string/String,~lib/map/Map<~lib/string/String,f32>>") - - case "hypermode.computeEmbedding": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("sentenceMap", "~lib/map/Map<~lib/string/String,~lib/string/String>"). - WithResult("~lib/map/Map<~lib/string/String,~lib/array/Array>") - - case "hypermode.invokeTextGenerator": - fn = metadata.NewFunction(fnName). - WithParameter("modelName", "~lib/string/String"). - WithParameter("instruction", "~lib/string/String"). - WithParameter(("format"), "~lib/string/String"). - WithResult("~lib/string/String") - - default: - return nil, fmt.Errorf("unknown host function: %s", fnName) - } - return fn, nil -} diff --git a/runtime/hostfunctions/compatibility/compatibility_test.go b/runtime/hostfunctions/compatibility/compatibility_test.go deleted file mode 100644 index 3291d0c2a..000000000 --- a/runtime/hostfunctions/compatibility/compatibility_test.go +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2024 Hypermode Inc. - * Licensed under the terms of the Apache License, Version 2.0 - * See the LICENSE file that accompanied this code for further details. - * - * SPDX-FileCopyrightText: 2024 Hypermode Inc. - * SPDX-License-Identifier: Apache-2.0 - */ - -package compatibility_test - -import ( - "context" - "testing" - - "github.com/hypermodeinc/modus/runtime/functions" - "github.com/hypermodeinc/modus/runtime/hostfunctions" - "github.com/hypermodeinc/modus/runtime/hostfunctions/compatibility" - "github.com/hypermodeinc/modus/runtime/plugins" - "github.com/hypermodeinc/modus/runtime/utils" - "github.com/hypermodeinc/modus/runtime/wasmhost" - - "github.com/tetratelabs/wazero" - wasm "github.com/tetratelabs/wazero/api" -) - -func Test_HostFunctionsHaveCompatibilityShim(t *testing.T) { - - registrations := hostfunctions.GetRegistrations() - - host := &mockWasmHost{} - for _, reg := range registrations { - if err := reg(host); err != nil { - t.Errorf("Error registering host function: %v", err) - } - } - - for _, name := range host.hostFunctions { - fn, err := compatibility.GetImportMetadataShim(name) - if err != nil { - t.Errorf("Missing import metadata shim for host function: %v", err) - } else if fn == nil { - t.Errorf("Invalid import metadata shim for host function: %v", name) - } - } - -} - -type mockWasmHost struct { - hostFunctions []string -} - -func (m *mockWasmHost) RegisterHostFunction(modName, funcName string, fn any, opts ...wasmhost.HostFunctionOption) error { - m.hostFunctions = append(m.hostFunctions, modName+"."+funcName) - return nil -} - -func (m *mockWasmHost) CallFunction(ctx context.Context, fnInfo functions.FunctionInfo, parameters map[string]any) (wasmhost.ExecutionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) CallFunctionByName(ctx context.Context, fnName string, paramValues ...any) (wasmhost.ExecutionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) Close(ctx context.Context) { - panic("not implemented") -} -func (m *mockWasmHost) CompileModule(ctx context.Context, bytes []byte) (wazero.CompiledModule, error) { - panic("not implemented") -} -func (m *mockWasmHost) GetFunctionInfo(fnName string) (functions.FunctionInfo, error) { - panic("not implemented") -} -func (m *mockWasmHost) GetFunctionRegistry() functions.FunctionRegistry { - panic("not implemented") -} -func (m *mockWasmHost) GetModuleInstance(ctx context.Context, plugin *plugins.Plugin, buffers utils.OutputBuffers) (wasm.Module, error) { - panic("not implemented") -} diff --git a/runtime/plugins/plugins.go b/runtime/plugins/plugins.go index f3a73cf43..d812dd3ef 100644 --- a/runtime/plugins/plugins.go +++ b/runtime/plugins/plugins.go @@ -12,16 +12,14 @@ package plugins import ( "context" "fmt" - "strings" - "github.com/hypermodeinc/modus/runtime/hostfunctions/compatibility" "github.com/hypermodeinc/modus/runtime/langsupport" "github.com/hypermodeinc/modus/runtime/languages" - "github.com/hypermodeinc/modus/runtime/logger" "github.com/hypermodeinc/modus/runtime/plugins/metadata" "github.com/hypermodeinc/modus/runtime/utils" "github.com/tetratelabs/wazero" + wasm "github.com/tetratelabs/wazero/api" ) type Plugin struct { @@ -62,23 +60,25 @@ func NewPlugin(ctx context.Context, cm wazero.CompiledModule, filename string, m plans[fnName] = plan } - warn := true + importsMap := make(map[string]wasm.FunctionDefinition, len(imports)) for _, fnDef := range imports { - modName, fnName, _ := fnDef.Import() - impName := fmt.Sprintf("%s.%s", modName, fnName) + if modName, fnName, ok := fnDef.Import(); ok { + importName := modName + "." + fnName + importsMap[importName] = fnDef + } + } - fnMeta, err := getImportMetadata(ctx, modName, fnName, md, &warn) - if err != nil { - return nil, err - } else if fnMeta == nil { - continue + for importName, fnMeta := range md.FnImports { + fnDef, ok := importsMap[importName] + if !ok { + return nil, fmt.Errorf("no wasm function definition found for %s", importName) } plan, err := planner.GetPlan(ctx, fnMeta, fnDef) if err != nil { - return nil, fmt.Errorf("failed to get execution plan for %s: %w", impName, err) + return nil, fmt.Errorf("failed to get execution plan for %s: %w", importName, err) } - plans[impName] = plan + plans[importName] = plan } plugin := &Plugin{ @@ -93,41 +93,6 @@ func NewPlugin(ctx context.Context, cm wazero.CompiledModule, filename string, m return plugin, nil } -func getImportMetadata(ctx context.Context, modName, fnName string, md *metadata.Metadata, warn *bool) (*metadata.Function, error) { - impName := fmt.Sprintf("%s.%s", modName, fnName) - if fnMeta, ok := md.FnImports[impName]; ok { - return fnMeta, nil - } - - if modName == "hypermode" { - if *warn { - *warn = false - logger.Warn(ctx). - Str("plugin", md.Name()). - Str("build_id", md.BuildId). - Msg("Function imports are missing from the metadata. Using compatibility shims. Please update your SDK to the latest version.") - } - if fnMeta, err := compatibility.GetImportMetadataShim(impName); err != nil { - return nil, fmt.Errorf("error creating compatibility shim for %s: %w", impName, err) - } else { - return fnMeta, nil - } - } else if shouldIgnoreModule(modName) { - return nil, nil - } - - return nil, fmt.Errorf("no metadata found for import %s", impName) -} - -func shouldIgnoreModule(name string) bool { - switch name { - case "wasi_snapshot_preview1", "wasi", "env", "runtime", "syscall", "test": - return true - } - - return strings.HasPrefix(name, "wasi_") -} - func (p *Plugin) NameAndVersion() (name string, version string) { return p.Metadata.NameAndVersion() }