Skip to content

Commit 449cbe1

Browse files
Small script tweaks
1 parent f82253a commit 449cbe1

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

examples/fib.gpt

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@ What's the myfunction of 3
33

44
---
55
name: myfunction
6+
tools: sub, mul
67
description: A function taking an integer as argument and returns an integer
78
args: number: An integer
89
tools: myfunction
910

10-
Do the following steps in strict sequence:
11-
Step 1. If ${number} is 0, skip the remaining steps and return 1
12-
Step 2. Calculate ${number} minus 1
13-
Step 3. Calculate myfunction of the result of Step 2
14-
Step 4. Multiply result of Step 3 with ${number}
15-
Step 5. Return the result of Step 4
11+
If ${number} is 0 return 1. Otherwise return myfunction(${number}-1) * ${number}
12+
13+
---
14+
name: sub
15+
description: Subtract two numbers
16+
args: left: a number
17+
args: right: a number
18+
19+
#!/bin/bash
20+
21+
echo $((${LEFT} - ${RIGHT}))
22+
23+
---
24+
name: mul
25+
description: Multiply two numbers
26+
args: left: a number
27+
args: right: a number
28+
29+
#!/bin/bash
30+
31+
echo $((${LEFT} * ${RIGHT}))

pkg/monitor/display.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9-
"sort"
9+
"slices"
1010
"strings"
1111
"sync/atomic"
1212
"time"
@@ -87,18 +87,18 @@ func (d *display) Event(event runner.Event) {
8787
case runner.EventTypeCallStart:
8888
currentCall.Start = event.Time
8989
currentCall.Input = event.Content
90-
log.Fields("input", event.Content).Infof("%s started", callName)
90+
log.Fields("input", event.Content).Infof("started [%s]", callName)
9191
case runner.EventTypeCallProgress:
9292
case runner.EventTypeCallContinue:
93-
log.Fields("toolResults", event.ToolResults).Infof("%s continue", callName)
93+
log.Fields("toolResults", event.ToolResults).Infof("continue [%s]", callName)
9494
case runner.EventTypeChat:
9595
if event.ChatRequest == nil {
9696
log = log.Fields(
9797
"response", toJSON(event.ChatResponse),
9898
"cached", event.ChatResponseCached,
9999
)
100100
} else {
101-
log.Infof("%s openai request sent", callName)
101+
log.Infof("openai request sent [%s]", callName)
102102
log = log.Fields(
103103
"request", toJSON(event.ChatRequest),
104104
)
@@ -112,7 +112,7 @@ func (d *display) Event(event runner.Event) {
112112
case runner.EventTypeCallFinish:
113113
currentCall.End = event.Time
114114
currentCall.Output = event.Content
115-
log.Fields("output", event.Content).Infof("%s ended", callName)
115+
log.Fields("output", event.Content).Infof("ended [%s]", callName)
116116
}
117117

118118
d.dump.Calls[currentIndex] = currentCall
@@ -158,6 +158,10 @@ type jsonDump struct {
158158
obj any
159159
}
160160

161+
func (j jsonDump) MarshalJSON() ([]byte, error) {
162+
return json.Marshal(j.obj)
163+
}
164+
161165
func (j jsonDump) String() string {
162166
d, err := json.Marshal(j.obj)
163167
if err != nil {
@@ -184,6 +188,9 @@ func (c callName) String() string {
184188
if name == "" {
185189
name = tool.Source.File
186190
}
191+
if currentCall.ID != "1" {
192+
name += "(" + currentCall.ID + ")"
193+
}
187194
msg = append(msg, name)
188195
found := false
189196
for _, parent := range c.calls {
@@ -198,7 +205,7 @@ func (c callName) String() string {
198205
}
199206
}
200207

201-
sort.Sort(sort.Reverse(sort.StringSlice(msg)))
208+
slices.Reverse(msg)
202209
return strings.Join(msg, "->")
203210
}
204211

@@ -227,3 +234,7 @@ type call struct {
227234
Input string `json:"input,omitempty"`
228235
Output string `json:"output,omitempty"`
229236
}
237+
238+
func (c call) String() string {
239+
return c.ID
240+
}

pkg/mvl/log.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package mvl
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57
"runtime"
68
"strings"
79
"time"
@@ -23,13 +25,37 @@ type formatter struct {
2325
}
2426

2527
func (f formatter) Format(entry *logrus.Entry) ([]byte, error) {
26-
return []byte(fmt.Sprintf("%s %s %s\n",
28+
msg := entry.Message
29+
if i, ok := entry.Data["input"].(string); ok && i != "" {
30+
msg += fmt.Sprintf(" [input=%s]", i)
31+
}
32+
return []byte(fmt.Sprintf("%s %s\n",
2733
entry.Time.Format(time.RFC3339),
28-
entry.Level,
29-
entry.Message)), nil
34+
msg)), nil
35+
}
36+
37+
type verbose struct {
38+
}
39+
40+
func (f verbose) Format(entry *logrus.Entry) ([]byte, error) {
41+
buf, err := json.MarshalIndent(struct {
42+
Time time.Time `json:"time,omitempty"`
43+
Level string `json:"level,omitempty"`
44+
Message string `json:"message,omitempty"`
45+
Data any `json:"data,omitempty"`
46+
}{
47+
Time: entry.Time,
48+
Level: entry.Level.String(),
49+
Message: entry.Message,
50+
Data: entry.Data,
51+
}, "", " ")
52+
return append(buf, []byte("\n")...), err
3053
}
3154

3255
func SetDebug() {
56+
logrus.SetFormatter(&logrus.JSONFormatter{
57+
PrettyPrint: os.Getenv("GPTSCRIPT_JSON_LOG_SINGLE_LINE") != "true",
58+
})
3359
logrus.SetLevel(logrus.DebugLevel)
3460
}
3561

0 commit comments

Comments
 (0)