-
Notifications
You must be signed in to change notification settings - Fork 63
fix: add deterministic mock AI provider for local/demo use #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
4541224
454bf7d
0f58ba1
77f42b5
f03518a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ PORT=3000 | |
| NODE_ENV=development | ||
|
|
||
| # AI Service Configuration | ||
| # AI provider selection: "openrouter" (default) or "ollama" | ||
| # AI provider selection: "openrouter" (default), "ollama", or "mock" (local/demo only) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a developer follows this new Useful? React with 👍 / 👎. |
||
| AI_PROVIDER=openrouter | ||
|
|
||
| # OpenRouter Configuration (when AI_PROVIDER=openrouter) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,7 +88,7 @@ Common optional variables: | |
| | Variable | Default | Notes | | ||
| | --- | --- | --- | | ||
| | `PORT` | `3000` | Gateway listen port. | | ||
| | `AI_PROVIDER` | `openrouter` | Supported values: `openrouter`, `ollama`. | | ||
| | `AI_PROVIDER` | `openrouter` | Supported values: `openrouter`, `ollama`, `mock` (local/demo only). | | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When readers follow the root README instead of Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Adding Useful? React with 👍 / 👎. |
||
| | `OPENROUTER_MODEL` | `z-ai/glm-4.5-air:free` in code/docs unless overridden | OpenRouter model. | | ||
| | `OPENROUTER_URL` | `https://openrouter.ai/api/v1/chat/completions` provider default | Used by tests and custom OpenRouter-compatible endpoints. | | ||
| | `OLLAMA_URL` | `http://localhost:11434` | Used when `AI_PROVIDER=ollama`. | | ||
|
|
@@ -129,7 +129,7 @@ cd gateway | |
| RECEIPT_STORE=memory CACHE_ENABLED=false go run . | ||
| ``` | ||
|
|
||
| The verifier must be reachable at `VERIFIER_URL` for signed requests. OpenRouter startup requires `OPENROUTER_API_KEY` unless `AI_PROVIDER=ollama`. | ||
| The verifier must be reachable at `VERIFIER_URL` for signed requests. OpenRouter startup requires `OPENROUTER_API_KEY` unless `AI_PROVIDER` is set to `ollama` or `mock`. | ||
|
|
||
| ## Testing | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ai | ||
|
|
||
| import "context" | ||
|
|
||
| // MockProvider implements the Provider interface for local/demo testing | ||
| type MockProvider struct{} | ||
|
|
||
| // NewMockProvider creates a new MockProvider instance | ||
| func NewMockProvider() *MockProvider { | ||
| return &MockProvider{} | ||
| } | ||
|
|
||
| // Generate returns a deterministic mock summary response | ||
| func (p *MockProvider) Generate(ctx context.Context, text string) (string, error) { | ||
| return "This is a deterministic mock summary of the input text for local/demo testing.", nil | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ func NewProvider() (Provider, error) { | |
| return NewOpenRouterProvider(), nil | ||
| case "ollama": | ||
| return NewOllamaProvider(), nil | ||
| case "mock": | ||
| return NewMockProvider(), nil | ||
|
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because Useful? React with 👍 / 👎.
Comment on lines
+29
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because the mock provider is documented as local/demo-only but this branch accepts Useful? React with 👍 / 👎. |
||
| default: | ||
| return nil, fmt.Errorf("unsupported AI provider: %s", providerType) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,14 @@ package main | |
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "gateway/internal/ai" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" | ||
|
|
@@ -616,3 +618,96 @@ func TestHandleReadyz_RedisUnreachable(t *testing.T) { | |
| checks := response["checks"].(map[string]interface{}) | ||
| require.Equal(t, "unreachable", checks["redis"]) | ||
| } | ||
|
|
||
| func TestHandleReadyz_MockProvider(t *testing.T) { | ||
| gin.SetMode(gin.TestMode) | ||
| t.Setenv("AI_PROVIDER", "mock") | ||
| t.Setenv("RECEIPT_STORE", "memory") | ||
| t.Setenv("CACHE_ENABLED", "false") | ||
|
|
||
| origVerifier := checkVerifierHealth | ||
| defer func() { | ||
| checkVerifierHealth = origVerifier | ||
| }() | ||
|
|
||
| checkVerifierHealth = func() string { return "ok" } | ||
|
|
||
| r := gin.Default() | ||
| r.GET("/readyz", handleReadyz) | ||
|
|
||
| req, _ := http.NewRequest(http.MethodGet, "/readyz", nil) | ||
| w := httptest.NewRecorder() | ||
|
|
||
| r.ServeHTTP(w, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, w.Code) | ||
|
|
||
| var response map[string]interface{} | ||
| err := json.Unmarshal(w.Body.Bytes(), &response) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, true, response["ready"]) | ||
|
|
||
| checks := response["checks"].(map[string]interface{}) | ||
| require.Equal(t, "ok", checks["verifier"]) | ||
| require.Equal(t, "ok", checks["mock"]) | ||
|
|
||
| aiProviderCheck := checks["ai_provider"].(map[string]interface{}) | ||
| require.Equal(t, "mock", aiProviderCheck["provider"]) | ||
| require.Equal(t, "ok", aiProviderCheck["status"]) | ||
| } | ||
|
|
||
| func TestHandleSummarize_MockProvider(t *testing.T) { | ||
| // Set up a verifier that returns valid immediately | ||
| verifier := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(200) | ||
| w.Write([]byte(`{"is_valid":true, "recovered_address":"0xabc","error":""}`)) | ||
| })) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| defer verifier.Close() | ||
|
|
||
| // Environment | ||
| t.Setenv("AI_PROVIDER", "mock") | ||
| t.Setenv("VERIFIER_URL", verifier.URL) | ||
| t.Setenv("SERVER_WALLET_PRIVATE_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Setting Useful? React with 👍 / 👎. |
||
| t.Setenv("RECEIPT_STORE", "memory") | ||
| t.Setenv("CACHE_ENABLED", "false") | ||
|
|
||
| // Initialize AI provider for this test | ||
| var err error | ||
| aiProvider, err = ai.NewProvider() | ||
| require.NoError(t, err) | ||
|
|
||
| // Initialize receipt store for this test | ||
| err = initReceiptStore() | ||
| require.NoError(t, err) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| gin.SetMode(gin.TestMode) | ||
| r := gin.New() | ||
| r.POST("/api/ai/summarize", handleSummarize) | ||
|
|
||
| // Build a valid request with signature/nonce | ||
| reqBody := strings.NewReader(`{"text":"hello world text to summarize"}`) | ||
| req, _ := http.NewRequest("POST", "/api/ai/summarize", reqBody) | ||
| req.Header.Set("X-402-Signature", "sig") | ||
| req.Header.Set("X-402-Nonce", "nonce") | ||
| req.Header.Set("X-402-Timestamp", strconv.FormatInt(time.Now().Unix(), 10)) | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r.ServeHTTP(w, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, w.Code) | ||
|
|
||
| var response map[string]interface{} | ||
| err = json.Unmarshal(w.Body.Bytes(), &response) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify deterministic response | ||
| expected := "This is a deterministic mock summary of the input text for local/demo testing." | ||
| require.Equal(t, expected, response["result"]) | ||
|
|
||
| // Check that X-402-Receipt header is set | ||
| receiptHeader := w.Header().Get("X-402-Receipt") | ||
| require.NotEmpty(t, receiptHeader) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a local/demo user copies this env template, sets
AI_PROVIDER=mock, and starts the stack with Docker Compose, the gateway container still will not see the setting becausedocker-compose.ymlonly lists the variables that get placed in the container environment and omitsAI_PROVIDER; Docker’s Compose docs describe theenvironmentattribute as what sets container environment variables. In that scenario the gateway falls back toopenrouterand fails startup withoutOPENROUTER_API_KEY, so please addAI_PROVIDER(and any provider-specific mock/ollama variables as needed) to the gateway service environment.Useful? React with 👍 / 👎.