-
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 2 commits
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,11 +3,13 @@ package main | |
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "gateway/internal/ai" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
|
|
@@ -616,3 +618,204 @@ 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) { | ||
| // Expected test values | ||
| expectedRecipient := "0x2cAF48b4BA1C58721a85dFADa5aC01C2DFa62219" | ||
| expectedAmount := "0.001" | ||
| expectedToken := "USDC" | ||
| expectedChainID := 84532 | ||
| expectedSignature := "test-signature" | ||
|
|
||
| var expectedNonce string | ||
| var expectedTimestamp uint64 | ||
|
|
||
| // Set up a verifier that asserts the parsed context parameters match exactly | ||
| verifier := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| var req VerifyRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"failed to decode verify request"}`)) | ||
| return | ||
| } | ||
|
|
||
| if req.Signature != expectedSignature { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"signature mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.Nonce != expectedNonce { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"nonce mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.Timestamp != expectedTimestamp { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"timestamp mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.Recipient != expectedRecipient { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"recipient mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.Amount != expectedAmount { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"amount mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.Token != expectedToken { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"token mismatch"}`)) | ||
| return | ||
| } | ||
| if req.Context.ChainID != expectedChainID { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| w.Write([]byte(`{"is_valid":false,"error":"chain_id mismatch"}`)) | ||
| return | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| 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") | ||
|
|
||
| serverPrivateKeyTestMu.Lock() | ||
| receiptGlobalsTestMu.Lock() | ||
|
|
||
| // Save package-level globals | ||
| origAIProvider := aiProvider | ||
| origReceiptStore := getActiveReceiptStore() | ||
| origKey := serverPrivateKey | ||
| origKeyErr := serverPrivateKeyErr | ||
| origOnce := serverPrivateKeyOnce | ||
|
|
||
| defer func() { | ||
| aiProvider = origAIProvider | ||
| setActiveReceiptStore(origReceiptStore) | ||
| serverPrivateKey = origKey | ||
| serverPrivateKeyErr = origKeyErr | ||
| serverPrivateKeyOnce = origOnce | ||
|
|
||
| receiptGlobalsTestMu.Unlock() | ||
| serverPrivateKeyTestMu.Unlock() | ||
| }() | ||
|
|
||
| // Reset private key once/error states for this test | ||
| serverPrivateKey = nil | ||
| serverPrivateKeyErr = nil | ||
| serverPrivateKeyOnce = sync.Once{} | ||
|
|
||
| // 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) | ||
|
|
||
| // Step 1: Send unsigned request to trigger 402 challenge | ||
| reqBody1 := strings.NewReader(`{"text":"hello world text to summarize"}`) | ||
| req1, _ := http.NewRequest("POST", "/api/ai/summarize", reqBody1) | ||
| req1.Header.Set("Content-Type", "application/json") | ||
| w1 := httptest.NewRecorder() | ||
| r.ServeHTTP(w1, req1) | ||
|
|
||
| require.Equal(t, http.StatusPaymentRequired, w1.Code) | ||
|
|
||
| var challengeResp map[string]interface{} | ||
| err = json.Unmarshal(w1.Body.Bytes(), &challengeResp) | ||
| require.NoError(t, err) | ||
|
|
||
| paymentCtx, ok := challengeResp["paymentContext"].(map[string]interface{}) | ||
| require.True(t, ok) | ||
|
|
||
| expectedNonce, ok = paymentCtx["nonce"].(string) | ||
| require.True(t, ok) | ||
|
|
||
| timestampFloat, ok := paymentCtx["timestamp"].(float64) | ||
| require.True(t, ok) | ||
| expectedTimestamp = uint64(timestampFloat) | ||
|
|
||
| // Verify returned payment context fields match expectations | ||
| require.Equal(t, expectedRecipient, paymentCtx["recipient"]) | ||
| require.Equal(t, expectedAmount, paymentCtx["amount"]) | ||
| require.Equal(t, expectedToken, paymentCtx["token"]) | ||
| require.Equal(t, float64(expectedChainID), paymentCtx["chainId"]) | ||
|
|
||
| // Step 2: Send signed retry using context values | ||
| reqBody2 := strings.NewReader(`{"text":"hello world text to summarize"}`) | ||
| req2, _ := http.NewRequest("POST", "/api/ai/summarize", reqBody2) | ||
| req2.Header.Set("X-402-Signature", expectedSignature) | ||
| req2.Header.Set("X-402-Nonce", expectedNonce) | ||
| req2.Header.Set("X-402-Timestamp", strconv.FormatUint(expectedTimestamp, 10)) | ||
| req2.Header.Set("Content-Type", "application/json") | ||
|
|
||
| w2 := httptest.NewRecorder() | ||
| r.ServeHTTP(w2, req2) | ||
|
|
||
| require.Equal(t, http.StatusOK, w2.Code) | ||
|
|
||
| var response2 map[string]interface{} | ||
| err = json.Unmarshal(w2.Body.Bytes(), &response2) | ||
| 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, response2["result"]) | ||
|
|
||
| // Check that X-402-Receipt header is set | ||
| receiptHeader := w2.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 👍 / 👎.