Skip to content

Commit 8c6394b

Browse files
committed
fix: add revive lint and fix related issues
Signed-off-by: Donnie Adams <[email protected]>
1 parent e28e7b7 commit 8c6394b

File tree

12 files changed

+47
-52
lines changed

12 files changed

+47
-52
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ linters:
1818
- unused
1919
- goimports
2020
- whitespace
21+
- revive
2122
fast: false
2223
max-same-issues: 50

pkg/assemble/assemble.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package assemble
22

33
import (
4-
"context"
54
"encoding/json"
65
"io"
76

@@ -10,7 +9,7 @@ import (
109

1110
var Header = []byte("GPTSCRIPT!")
1211

13-
func Assemble(ctx context.Context, prg types.Program, output io.Writer) error {
12+
func Assemble(prg types.Program, output io.Writer) error {
1413
if _, err := output.Write(Header); err != nil {
1514
return err
1615
}

pkg/cache/cache.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cache
22

33
import (
4-
"context"
54
"errors"
65
"io/fs"
76
"os"
@@ -47,14 +46,14 @@ func New(opts ...Options) (*Client, error) {
4746
}, nil
4847
}
4948

50-
func (c *Client) Store(ctx context.Context, key string, content []byte) error {
49+
func (c *Client) Store(key string, content []byte) error {
5150
if c == nil || c.noop {
5251
return nil
5352
}
5453
return os.WriteFile(filepath.Join(c.dir, key), content, 0644)
5554
}
5655

57-
func (c *Client) Get(ctx context.Context, key string) ([]byte, bool, error) {
56+
func (c *Client) Get(key string) ([]byte, bool, error) {
5857
if c == nil || c.noop {
5958
return nil, false, nil
6059
}

pkg/cli/gptscript.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (r *GPTScript) Customize(cmd *cobra.Command) {
5252
cmd.Flags().SetInterspersed(false)
5353
}
5454

55-
func (r *GPTScript) listTools(ctx context.Context) error {
55+
func (r *GPTScript) listTools() error {
5656
var lines []string
5757
for _, tool := range builtin.ListTools() {
5858
lines = append(lines, tool.String())
@@ -79,7 +79,7 @@ func (r *GPTScript) listModels(ctx context.Context) error {
7979
return nil
8080
}
8181

82-
func (r *GPTScript) Pre(cmd *cobra.Command, args []string) error {
82+
func (r *GPTScript) Pre(*cobra.Command, []string) error {
8383
if r.Quiet == nil {
8484
if term.IsTerminal(int(os.Stdout.Fd())) {
8585
r.Quiet = new(bool)
@@ -107,7 +107,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
107107
}
108108

109109
if r.ListTools {
110-
return r.listTools(cmd.Context())
110+
return r.listTools()
111111
}
112112

113113
if r.Server {
@@ -158,7 +158,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
158158
out = f
159159
}
160160

161-
return assemble.Assemble(cmd.Context(), prg, out)
161+
return assemble.Assemble(prg, out)
162162
}
163163

164164
runner, err := runner.New(r.Options, runner.Options{

pkg/engine/cmd.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func (e *Engine) runCommand(ctx context.Context, tool types.Tool, input string)
4040
return tool.BuiltinFunc(ctx, e.Env, input)
4141
}
4242

43-
cmd, close, err := e.newCommand(ctx, nil, tool.Instructions, input)
43+
cmd, stop, err := e.newCommand(ctx, nil, tool.Instructions, input)
4444
if err != nil {
4545
return "", err
4646
}
47-
defer close()
47+
defer stop()
4848

4949
e.Progress <- openai.Status{
5050
CompletionID: id,
@@ -130,28 +130,28 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, instructions
130130

131131
var (
132132
cmdArgs = args[1:]
133-
close func()
133+
stop func()
134134
)
135135

136136
if strings.TrimSpace(rest) != "" {
137137
f, err := os.CreateTemp("", version.ProgramName)
138138
if err != nil {
139139
return nil, nil, err
140140
}
141-
close = func() {
142-
os.Remove(f.Name())
141+
stop = func() {
142+
_ = os.Remove(f.Name())
143143
}
144144

145145
_, err = f.Write([]byte(rest))
146146
_ = f.Close()
147147
if err != nil {
148-
close()
148+
stop()
149149
return nil, nil, err
150150
}
151151
cmdArgs = append(cmdArgs, f.Name())
152152
}
153153

154154
cmd := exec.CommandContext(ctx, args[0], cmdArgs...)
155155
cmd.Env = env
156-
return cmd, close, nil
156+
return cmd, stop, nil
157157
}

pkg/engine/daemon.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
8989
port = e.getNextPort()
9090
url = fmt.Sprintf("http://127.0.0.1:%d%s", port, path)
9191

92-
cmd, close, err := e.newCommand(ctx, []string{
92+
cmd, stop, err := e.newCommand(ctx, []string{
9393
fmt.Sprintf("PORT=%d", port),
9494
},
9595
types.CommandPrefix+instructions,
@@ -103,7 +103,7 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
103103
cmd.Stdout = os.Stdout
104104
log.Infof("launched [%s][%s] port [%d] %v", tool.Name, tool.ID, port, cmd.Args)
105105
if err := cmd.Start(); err != nil {
106-
close()
106+
stop()
107107
return url, err
108108
}
109109

@@ -122,7 +122,7 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
122122
}
123123

124124
cancel(err)
125-
close()
125+
stop()
126126
daemonLock.Lock()
127127
defer daemonLock.Unlock()
128128

@@ -140,11 +140,9 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
140140
for i := 0; i < 20; i++ {
141141
resp, err := http.Get(url)
142142
if err == nil && resp.StatusCode == http.StatusOK {
143-
defer func() {
144-
_ = resp.Body.Close()
145-
}()
146143
go func() {
147144
_, _ = io.ReadAll(resp.Body)
145+
_ = resp.Body.Close()
148146
}()
149147
return url, nil
150148
}

pkg/engine/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (c *Context) ParentID() string {
9595
return c.Parent.ID
9696
}
9797

98-
func (c *Context) UnmarshalJSON(data []byte) error {
98+
func (c *Context) UnmarshalJSON([]byte) error {
9999
panic("this data struct is circular by design and can not be read from json")
100100
}
101101

pkg/loader/loader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package loader
22

33
import "testing"
44

5-
func TestLoader(t *testing.T) {
5+
func TestLoader(*testing.T) {
66
}

pkg/monitor/display.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
prettyIDCounter int64
4040
)
4141

42-
func (c *Console) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
42+
func (c *Console) Start(_ context.Context, prg *types.Program, _ []string, input string) (runner.Monitor, error) {
4343
id := atomic.AddInt64(&runID, 1)
4444
mon := newDisplay(c.dumpState, c.displayProgress)
4545
mon.dump.ID = fmt.Sprint(id)
@@ -79,7 +79,7 @@ func (l *livePrinter) end() {
7979
}
8080
}
8181

82-
func (l *livePrinter) progressStart(event runner.Event, c call) {
82+
func (l *livePrinter) progressStart(c call) {
8383
if l == nil {
8484
return
8585
}
@@ -91,7 +91,7 @@ func (l *livePrinter) progressStart(event runner.Event, c call) {
9191
})
9292
}
9393

94-
func (l *livePrinter) progressEnd(event runner.Event, c call) {
94+
func (l *livePrinter) progressEnd(c call) {
9595
if l == nil {
9696
return
9797
}
@@ -217,17 +217,17 @@ func (d *display) Event(event runner.Event) {
217217

218218
switch event.Type {
219219
case runner.EventTypeCallStart:
220-
d.livePrinter.progressStart(event, currentCall)
220+
d.livePrinter.progressStart(currentCall)
221221
d.livePrinter.end()
222222
currentCall.Start = event.Time
223223
currentCall.Input = event.Content
224224
log.Fields("input", event.Content).Infof("started [%s]", callName)
225225
case runner.EventTypeCallSubCalls:
226-
d.livePrinter.progressEnd(event, currentCall)
226+
d.livePrinter.progressEnd(currentCall)
227227
case runner.EventTypeCallProgress:
228228
d.livePrinter.print(event, currentCall)
229229
case runner.EventTypeCallContinue:
230-
d.livePrinter.progressStart(event, currentCall)
230+
d.livePrinter.progressStart(currentCall)
231231
d.livePrinter.end()
232232
log.Fields("toolResults", event.ToolResults).Infof("continue [%s]", callName)
233233
case runner.EventTypeChat:
@@ -253,7 +253,7 @@ func (d *display) Event(event runner.Event) {
253253
Cached: event.ChatResponseCached,
254254
})
255255
case runner.EventTypeCallFinish:
256-
d.livePrinter.progressEnd(event, currentCall)
256+
d.livePrinter.progressEnd(currentCall)
257257
d.livePrinter.end()
258258
currentCall.End = event.Time
259259
currentCall.Output = event.Content

pkg/openai/client.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ func (c *Client) seed(request openai.ChatCompletionRequest) int {
136136
return hash.Seed(newRequest)
137137
}
138138

139-
func (c *Client) fromCache(ctx context.Context, messageRequest types.CompletionRequest, request openai.ChatCompletionRequest) (result []openai.ChatCompletionStreamResponse, _ bool, _ error) {
139+
func (c *Client) fromCache(messageRequest types.CompletionRequest, request openai.ChatCompletionRequest) (result []openai.ChatCompletionStreamResponse, _ bool, _ error) {
140140
if messageRequest.Cache != nil && !*messageRequest.Cache {
141141
return nil, false, nil
142142
}
143143

144-
cache, found, err := c.cache.Get(ctx, c.cacheKey(request))
144+
cache, found, err := c.cache.Get(c.cacheKey(request))
145145
if err != nil {
146146
return nil, false, err
147147
} else if !found {
@@ -167,10 +167,10 @@ func toToolCall(call types.CompletionToolCall) openai.ToolCall {
167167
}
168168
}
169169

170-
func toMessages(ctx context.Context, cache *cache.Client, request types.CompletionRequest) (result []openai.ChatCompletionMessage, err error) {
170+
func toMessages(cache *cache.Client, request types.CompletionRequest) (result []openai.ChatCompletionMessage, err error) {
171171
for _, message := range request.Messages {
172172
if request.Vision {
173-
message, err = vision.ToVisionMessage(ctx, cache, message)
173+
message, err = vision.ToVisionMessage(cache, message)
174174
if err != nil {
175175
return nil, err
176176
}
@@ -189,7 +189,7 @@ func toMessages(ctx context.Context, cache *cache.Client, request types.Completi
189189
chatMessage.ToolCalls = append(chatMessage.ToolCalls, toToolCall(*content.ToolCall))
190190
}
191191
if content.Image != nil {
192-
url, err := vision.ImageToURL(ctx, cache, request.Vision, *content.Image)
192+
url, err := vision.ImageToURL(cache, request.Vision, *content.Image)
193193
if err != nil {
194194
return nil, err
195195
}
@@ -247,7 +247,7 @@ type Status struct {
247247
}
248248

249249
func (c *Client) Call(ctx context.Context, messageRequest types.CompletionRequest, status chan<- Status) (*types.CompletionMessage, error) {
250-
msgs, err := toMessages(ctx, c.cache, messageRequest)
250+
msgs, err := toMessages(c.cache, messageRequest)
251251
if err != nil {
252252
return nil, err
253253
}
@@ -298,7 +298,7 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
298298

299299
var cacheResponse bool
300300
request.Seed = ptr(c.seed(request))
301-
response, ok, err := c.fromCache(ctx, messageRequest, request)
301+
response, ok, err := c.fromCache(messageRequest, request)
302302
if err != nil {
303303
return nil, err
304304
} else if !ok {
@@ -390,7 +390,7 @@ func override(left, right string) string {
390390
return left
391391
}
392392

393-
func (c *Client) store(ctx context.Context, key string, responses []openai.ChatCompletionStreamResponse) error {
393+
func (c *Client) store(key string, responses []openai.ChatCompletionStreamResponse) error {
394394
buf := &bytes.Buffer{}
395395
gz := gzip.NewWriter(buf)
396396
err := json.NewEncoder(gz).Encode(responses)
@@ -400,7 +400,7 @@ func (c *Client) store(ctx context.Context, key string, responses []openai.ChatC
400400
if err := gz.Close(); err != nil {
401401
return err
402402
}
403-
return c.cache.Store(ctx, key, buf.Bytes())
403+
return c.cache.Store(key, buf.Bytes())
404404
}
405405

406406
func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest, transactionID string, partial chan<- Status) (responses []openai.ChatCompletionStreamResponse, _ error) {
@@ -426,7 +426,7 @@ func (c *Client) call(ctx context.Context, request openai.ChatCompletionRequest,
426426
for {
427427
response, err := stream.Recv()
428428
if err == io.EOF {
429-
return responses, c.store(ctx, cacheKey, responses)
429+
return responses, c.store(cacheKey, responses)
430430
} else if err != nil {
431431
return nil, err
432432
}

pkg/runner/monitor.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import (
99
type noopFactory struct {
1010
}
1111

12-
func (n noopFactory) Start(ctx context.Context, prg *types.Program, env []string, input string) (Monitor, error) {
12+
func (n noopFactory) Start(context.Context, *types.Program, []string, string) (Monitor, error) {
1313
return noopMonitor{}, nil
1414
}
1515

1616
type noopMonitor struct {
1717
}
1818

19-
func (n noopMonitor) Event(event Event) {
19+
func (n noopMonitor) Event(Event) {
2020
}
2121

22-
func (n noopMonitor) Stop(output string, err error) {
23-
}
22+
func (n noopMonitor) Stop(string, error) {}

0 commit comments

Comments
 (0)