-
Notifications
You must be signed in to change notification settings - Fork 57
Support advertised chain options in the web wallet flow 228 #250
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 all commits
8306ef5
924fd70
72bc77a
a203e78
c872f73
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "net/http" | ||
| "net/http/httptest" | ||
| "strconv" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -198,18 +199,33 @@ func TestCacheIntegration_FullFlow(t *testing.T) { | |
| } | ||
|
|
||
| // Verify Body | ||
| var resp1, resp2 map[string]interface{} | ||
| if err := json.Unmarshal(w1.Body.Bytes(), &resp1); err != nil { | ||
| t.Fatalf("Failed to unmarshal response 1: %v", err) | ||
| } | ||
| if err := json.Unmarshal(w2.Body.Bytes(), &resp2); err != nil { | ||
| t.Fatalf("Failed to unmarshal response 2: %v", err) | ||
| parseSSEResultForTest := func(body []byte) string { | ||
| lines := strings.Split(string(body), "\n") | ||
| var result string | ||
| for _, line := range lines { | ||
| if strings.HasPrefix(line, "data: ") { | ||
| data := strings.TrimSpace(strings.TrimPrefix(line, "data: ")) | ||
|
Comment on lines
+203
to
+207
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.
This new SSE parsing helper calls Useful? React with 👍 / 👎. |
||
| if data == "[DONE]" || data == "" { | ||
| continue | ||
| } | ||
| var chunk map[string]interface{} | ||
| if err := json.Unmarshal([]byte(data), &chunk); err == nil { | ||
| if text, ok := chunk["text"].(string); ok { | ||
| result += text | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| if val, ok := resp1["result"].(string); !ok || val != "AI Summary Result" { | ||
| t.Errorf("Unexpected result 1: %v", resp1["result"]) | ||
| result1 := parseSSEResultForTest(w1.Body.Bytes()) | ||
| result2 := parseSSEResultForTest(w2.Body.Bytes()) | ||
|
|
||
| if result1 != "AI Summary Result" { | ||
| t.Errorf("Unexpected result 1: %v", result1) | ||
| } | ||
| if val, ok := resp2["result"].(string); !ok || val != "AI Summary Result" { | ||
| t.Errorf("Unexpected result 2: %v", resp2["result"]) | ||
| if result2 != "AI Summary Result" { | ||
| t.Errorf("Unexpected result 2: %v", result2) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package ai | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
|
|
@@ -100,3 +101,88 @@ func (p *OpenRouterProvider) Generate(ctx context.Context, text string) (string, | |
|
|
||
| return content, nil | ||
| } | ||
|
|
||
| type openRouterStream struct { | ||
| resp *http.Response | ||
| reader *bufio.Reader | ||
| } | ||
|
|
||
| func (s *openRouterStream) Recv() (string, error) { | ||
| for { | ||
| line, err := s.reader.ReadBytes('\n') | ||
| if err != nil { | ||
| return "", err | ||
|
Comment on lines
+112
to
+114
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.
For OpenRouter-compatible streaming, only the Useful? React with 👍 / 👎. |
||
| } | ||
| line = bytes.TrimSpace(line) | ||
| if len(line) == 0 { | ||
| continue | ||
| } | ||
| if bytes.HasPrefix(line, []byte("data: ")) { | ||
| data := bytes.TrimPrefix(line, []byte("data: ")) | ||
| if string(data) == "[DONE]" { | ||
| return "", io.EOF | ||
| } | ||
| var chunk map[string]interface{} | ||
| if err := json.Unmarshal(data, &chunk); err != nil { | ||
| continue // skip invalid json | ||
| } | ||
| choices, ok := chunk["choices"].([]interface{}) | ||
| if !ok || len(choices) == 0 { | ||
| continue | ||
| } | ||
| choice, ok := choices[0].(map[string]interface{}) | ||
| if !ok { | ||
| continue | ||
| } | ||
| delta, ok := choice["delta"].(map[string]interface{}) | ||
| if !ok { | ||
| continue | ||
| } | ||
| if content, ok := delta["content"].(string); ok { | ||
| return content, nil | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (s *openRouterStream) Close() error { | ||
| return s.resp.Body.Close() | ||
| } | ||
|
|
||
| func (p *OpenRouterProvider) GenerateStream(ctx context.Context, text string) (Stream, error) { | ||
| prompt := fmt.Sprintf("Summarize this text in 2 sentences: %s", text) | ||
|
|
||
| reqBody, _ := json.Marshal(map[string]interface{}{ | ||
| "model": p.model, | ||
| "messages": []map[string]string{ | ||
| {"role": "user", "content": prompt}, | ||
| }, | ||
| "stream": true, | ||
| }) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "POST", p.url, bytes.NewBuffer(reqBody)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create OpenRouter request: %w", err) | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+p.apiKey) | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) || ctx.Err() == context.DeadlineExceeded { | ||
| return nil, context.DeadlineExceeded | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| body, _ := io.ReadAll(resp.Body) | ||
| resp.Body.Close() | ||
| return nil, fmt.Errorf("openrouter returned status %d: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| return &openRouterStream{ | ||
| resp: resp, | ||
| reader: bufio.NewReader(resp.Body), | ||
| }, nil | ||
| } | ||
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.
This new cache-write path no longer consumes the
bodyBytesassigned just above, leaving an unused local ingateway/cache.go. Go rejects unused locals at compile time, so the gateway package will not build until the assignment is removed or the buffered body is used again.Useful? React with 👍 / 👎.