• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kshard / chatter / 16538940978

26 Jul 2025 10:35AM UTC coverage: 67.584% (+44.9%) from 22.674%
16538940978

Pull #53

github

fogfish
update license
Pull Request #53: Enable multi-content I/O within prompts & responses

596 of 837 new or added lines in 27 files covered. (71.21%)

2 existing lines in 2 files now uncovered.

884 of 1308 relevant lines covered (67.58%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/provider/openai/service.go
1
//
2
// Copyright (C) 2024 - 2025 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/kshard/chatter
7
//
8

9
package openai
10

11
import (
12
        "context"
13
        "fmt"
14
        "os/user"
15
        "path/filepath"
16

17
        "github.com/fogfish/gurl/v2/http"
18
        ƒ "github.com/fogfish/gurl/v2/http/recv"
19
        ø "github.com/fogfish/gurl/v2/http/send"
20
        "github.com/fogfish/opts"
21
        "github.com/jdxcode/netrc"
22
)
23

24
//
25
// Configuration options for OpenAI
26
//
27

28
type Option = opts.Option[Client]
29

30
var (
31
        // Config HTTP stack
32
        WithHTTP = opts.Use[Client](http.NewStack)
33

34
        // Config the host, api.openai.com is default
35
        WithHost = opts.ForType[Client, ø.Authority]()
36

37
        // Config API secret key
38
        WithSecret = opts.ForName[Client, string]("secret")
39

40
        // Set api secret from ~/.netrc file
41
        WithNetRC = opts.FMap(withNetRC)
42
)
43

NEW
44
func withNetRC(h *Client, host string) error {
×
NEW
45
        if h.secret != "" {
×
NEW
46
                return nil
×
NEW
47
        }
×
48

NEW
49
        usr, err := user.Current()
×
NEW
50
        if err != nil {
×
NEW
51
                return err
×
NEW
52
        }
×
53

NEW
54
        n, err := netrc.Parse(filepath.Join(usr.HomeDir, ".netrc"))
×
NEW
55
        if err != nil {
×
NEW
56
                return err
×
NEW
57
        }
×
58

NEW
59
        machine := n.Machine(host)
×
NEW
60
        if machine == nil {
×
NEW
61
                return fmt.Errorf("undefined secret for host <%s> at ~/.netrc", host)
×
NEW
62
        }
×
63

NEW
64
        h.secret = machine.Get("password")
×
NEW
65
        return nil
×
66
}
67

68
type Client struct {
69
        http.Stack
70
        host   ø.Authority
71
        path   ø.Path
72
        secret string
73
}
74

75
type Service[A, B any] struct {
76
        client Client
77
}
78

NEW
79
func New[A, B any](path ø.Path, opt ...Option) (*Service[A, B], error) {
×
NEW
80
        c := Client{
×
NEW
81
                host: ø.Authority("https://api.openai.com"),
×
NEW
82
                path: path,
×
NEW
83
        }
×
NEW
84
        if err := opts.Apply(&c, opt); err != nil {
×
NEW
85
                return nil, err
×
NEW
86
        }
×
87

NEW
88
        if c.Stack == nil {
×
NEW
89
                c.Stack = http.New()
×
NEW
90
        }
×
91

NEW
92
        return &Service[A, B]{client: c}, nil
×
93
}
94

NEW
95
func (s *Service[A, B]) Invoke(ctx context.Context, input A) (B, error) {
×
NEW
96
        bag, err := http.IO[B](s.client.WithContext(ctx),
×
NEW
97
                http.POST(
×
NEW
98
                        ø.URI("%s%s", s.client.host, s.client.path),
×
NEW
99
                        ø.Accept.JSON,
×
NEW
100
                        ø.Authorization.Set("Bearer "+s.client.secret),
×
NEW
101
                        ø.ContentType.JSON,
×
NEW
102
                        ø.Send(input),
×
NEW
103

×
NEW
104
                        ƒ.Status.OK,
×
NEW
105
                        ƒ.ContentType.JSON,
×
NEW
106
                ),
×
NEW
107
        )
×
NEW
108
        if err != nil {
×
NEW
109
                return *new(B), err
×
NEW
110
        }
×
111

NEW
112
        return *bag, nil
×
113
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc