Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 87 additions & 30 deletions cmd/testdata/stores.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,92 @@
stores:
- name: git
kind:
name: atest-store-git
enabled: true
url: xxx
readonly: false
disabled: false
- name: ai
kind:
name: atest-ext-ai
enabled: true
url: ""
readonly: false
disabled: false
properties:
- name: git
kind:
name: atest-store-git
dependencies: []
url: "unix:///tmp/atest-store-git.sock"
params: []
link: ""
enabled: true
categories: []
description: ""
url: xxx
username: ""
password: ""
readonly: false
disabled: false
properties: {}
- name: ai
kind:
name: atest-ext-ai
dependencies: [] # 无依赖
url: "unix:///tmp/atest-ext-ai.sock"
params:
- key: "provider"
description: "AI provider (local, openai, claude)"
defaultValue: "local"
- key: "model"
description: "AI model name"
defaultValue: "codellama"
description: "AI provider (ollama, openai, deepseek)"
defaultValue: "ollama"
- key: "endpoint"
description: "AI service endpoint"
description: "AI service endpoint URL"
defaultValue: "http://localhost:11434"
plugins:
- name: atest-store-git
url: unix:///tmp/atest-store-git.sock
enabled: true
- name: atest-ext-ai
url: unix:///tmp/atest-ext-ai.sock
- key: "api_key"
description: "API key for OpenAI/Deepseek providers"
defaultValue: ""
- key: "model"
description: "AI model name (auto-discovered for ollama)"
defaultValue: ""
- key: "max_tokens"
description: "Maximum tokens for AI generation"
defaultValue: "4096"
- key: "temperature"
description: "Generation temperature (0.0-2.0)"
defaultValue: "0.7"
- key: "timeout"
description: "Request timeout duration"
defaultValue: "30s"
link: "https://github.com/LinuxSuRen/atest-ext-ai"
enabled: true
description: "AI Extension Plugin for intelligent SQL generation and execution"
version: "latest"
registry: "ghcr.io/linuxsuren/atest-ext-ai"
categories: ["ai", "sql-generation"]
description: "AI Extension Plugin for natural language to SQL conversion"
url: "unix:///tmp/atest-ext-ai.sock"
username: ""
password: ""
readonly: false
disabled: false
properties:
provider: "ollama"
endpoint: "http://localhost:11434"
api_key: ""
model: ""
max_tokens: "4096"
temperature: "0.7"
timeout: "30s"

plugins:
- name: atest-store-git
dependencies: []
url: "unix:///tmp/atest-store-git.sock"
params: []
link: ""
enabled: true
categories: []
- name: atest-ext-ai
dependencies: []
url: "unix:///tmp/atest-ext-ai.sock"
params:
- key: "provider"
description: "AI provider (ollama, openai, deepseek)"
defaultValue: "ollama"
- key: "endpoint"
description: "AI service endpoint"
defaultValue: "http://localhost:11434"
- key: "api_key"
description: "API key for external AI services"
defaultValue: ""
- key: "model"
description: "AI model name (auto-discovered for ollama)"
defaultValue: ""
link: "https://github.com/LinuxSuRen/atest-ext-ai"
enabled: true
categories: ["ai", "sql-generation"]
description: "AI Extension Plugin for natural language to SQL conversion"
version: "v0.1.0"
registry: "ghcr.io/linuxsuren/atest-ext-ai"
14 changes: 8 additions & 6 deletions console/atest-ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const appVersion = ref('')
const appVersionLink = ref('https://github.com/LinuxSuRen/api-testing')
API.GetVersion((d) => {
appVersion.value = d.version
const version = d.version.match('^v\\d*.\\d*.\\d*')
const dirtyVersion = d.version.match('^v\\d*.\\d*.\\d*-\\d*-g')
const version = d.version.match(String.raw`^v\d*.\d*.\d*`)
const dirtyVersion = d.version.match(String.raw`^v\d*.\d*.\d*-\d*-g`)

if (!version && !dirtyVersion) {
return
Expand All @@ -55,16 +55,18 @@ API.GetVersion((d) => {
}
})

const hasLocalStorage = typeof globalThis !== 'undefined' && 'localStorage' in globalThis
const storage = hasLocalStorage ? globalThis.localStorage : undefined
const isCollapse = ref(true)
watch(isCollapse, (v: boolean) => {
window.localStorage.setItem('button.style', v ? 'simple' : '')
storage?.setItem('button.style', v ? 'simple' : '')
})
const lastActiveMenu = window.localStorage.getItem('activeMenu')
const lastActiveMenu = storage?.getItem('activeMenu') ?? 'welcome'
const activeMenu = ref(lastActiveMenu === '' ? 'welcome' : lastActiveMenu)
const panelName = ref(activeMenu)
const handleSelect = (key: string) => {
panelName.value = key
window.localStorage.setItem('activeMenu', key)
storage?.setItem('activeMenu', key)
}

const locale = ref(Cache.GetPreference().language)
Expand Down Expand Up @@ -178,7 +180,7 @@ API.GetMenus((menus) => {
<WelcomePage v-else-if="panelName === 'welcome' || panelName === ''" />

<span v-for="menu in extensionMenus" :key="menu.index" :index="menu.index">
<Extension v-if="panelName === menu.index" :name="menu.name" />
<Extension v-if="panelName === menu.index" :name="menu.index" />
</span>
</el-main>

Expand Down
50 changes: 33 additions & 17 deletions console/atest-ui/src/views/Extension.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,56 @@ const props = defineProps<Props>()
const loading = ref(true)
const loadPlugin = async (): Promise<void> => {
try {
// First load CSS
API.GetPageOfCSS(props.name, (d) => {
const style = document.createElement('style');
style.textContent = d.message;
document.head.appendChild(style);
});

// Then load JS and mount plugin
API.GetPageOfJS(props.name, (d) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = d.message;
document.head.appendChild(script);

const plugin = window.ATestPlugin;

if (plugin && plugin.mount) {
console.log('extension load success');
const container = document.getElementById("plugin-container");
if (container) {
container.innerHTML = ''; // Clear previous content
plugin.mount(container);
// Implement retry mechanism with exponential backoff
const checkPluginLoad = (retries = 0, maxRetries = 10) => {
const globalScope = globalThis as { ATestPlugin?: { mount?: (el: Element) => void } };
const plugin = globalScope.ATestPlugin;

if (plugin && plugin.mount) {
const container = document.getElementById("plugin-container");
if (container) {
container.innerHTML = ''; // Clear previous content
plugin.mount(container);
loading.value = false;
} else {
loading.value = false;
}
} else if (retries < maxRetries) {
// Incremental retry mechanism: 50ms, 100ms, 150ms...
const delay = 50 + retries * 50;
setTimeout(() => checkPluginLoad(retries + 1, maxRetries), delay);
} else {
loading.value = false;
}
}
};

// Start the retry mechanism
checkPluginLoad();
});
} catch (error) {
console.log(`extension load error: ${(error as Error).message}`)
} finally {
console.log('extension load finally');
loading.value = false; // Set loading to false on error
console.error('Failed to load extension assets', error);
}
};
try {
loadPlugin();
} catch (error) {
console.error('extension load error:', error);
}

loadPlugin().catch((error) => {
loading.value = false;
console.error('Failed to initialize extension plugin', error);
});
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion console/atest-ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
vue({
template: {
compilerOptions: {
nodeTransforms: true ? [removeDataTestAttrs] : [],
nodeTransforms: process.env.NODE_ENV === 'production' ? [removeDataTestAttrs] : [],
},
},
}),
Expand Down
74 changes: 74 additions & 0 deletions pkg/testing/remote/grpc_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package remote

import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/linuxsuren/api-testing/pkg/logging"
Expand Down Expand Up @@ -316,6 +318,12 @@ func (g *gRPCLoader) PProf(name string) []byte {
}

func (g *gRPCLoader) Query(query map[string]string) (result testing.DataResult, err error) {
// Detect AI method calls
if method := query["method"]; strings.HasPrefix(method, "ai.") {
return g.handleAIQuery(query)
}

// Original standard query logic
var dataResult *server.DataQueryResult
offset, _ := strconv.ParseInt(query["offset"], 10, 64)
limit, _ := strconv.ParseInt(query["limit"], 10, 64)
Expand Down Expand Up @@ -444,3 +452,69 @@ func (g *gRPCLoader) Close() {
g.conn.Close()
}
}

// handleAIQuery handles AI-specific queries
func (g *gRPCLoader) handleAIQuery(query map[string]string) (testing.DataResult, error) {
method := query["method"]

var dataQuery *server.DataQuery
switch method {
case "ai.generate":
dataQuery = &server.DataQuery{
Type: "ai",
Key: "generate",
Sql: g.encodeAIGenerateParams(query),
}
case "ai.capabilities":
dataQuery = &server.DataQuery{
Type: "ai",
Key: "capabilities",
Sql: "", // No additional parameters needed
}
default:
return testing.DataResult{}, fmt.Errorf("unsupported AI method: %s", method)
}

// Call existing gRPC Query
dataResult, err := g.client.Query(g.ctx, dataQuery)
if err != nil {
return testing.DataResult{}, err
}

// Convert response to testing.DataResult format
return g.convertAIResponse(dataResult), nil
}

// encodeAIGenerateParams filters and encodes AI generation parameters into JSON string.
// This function intentionally creates a new map to exclude the "method" field from the query,
// as "method" is used for routing decisions and should not be forwarded to AI plugins.
// Only the actual AI parameters (model, prompt, config) are encoded and sent via the SQL field.
func (g *gRPCLoader) encodeAIGenerateParams(query map[string]string) string {
// Extract only AI-specific parameters, excluding the routing field "method"
params := map[string]string{
"model": query["model"],
"prompt": query["prompt"],
"config": query["config"],
}
Comment on lines +494 to +498
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's necessary? It looks like you just convert the map data into a JSON string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Added comments in 6171a02 to clarify the intent.
The function intentionally filters out the "method" field (used for routing)
before encoding - it's not just a simple map-to-JSON conversion.

(Or we can use Go structs instead of maps to provide better type safety?)

data, _ := json.Marshal(params)
return string(data)
}

// convertAIResponse converts AI response to standard format
func (g *gRPCLoader) convertAIResponse(dataResult *server.DataQueryResult) testing.DataResult {
result := testing.DataResult{
Pairs: pairToMap(dataResult.Data),
}

// Map AI-specific response fields
if content := result.Pairs["generated_sql"]; content != "" {
result.Pairs["content"] = content // Follow AI interface standard
}
if result.Pairs["error"] != "" {
result.Pairs["success"] = "false"
} else {
result.Pairs["success"] = "true"
}

return result
}
Loading