Skip to content

Commit b57117a

Browse files
Add server
1 parent 7b6d1b9 commit b57117a

File tree

7 files changed

+334
-39
lines changed

7 files changed

+334
-39
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ module github.com/acorn-io/gptscript
33
go 1.21.5
44

55
require (
6+
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d
67
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb
78
github.com/adrg/xdg v0.4.0
89
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
910
github.com/hexops/autogold/v2 v2.1.0
1011
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056
12+
github.com/olahol/melody v1.1.4
1113
github.com/sashabaranov/go-openai v1.18.3
1214
github.com/sirupsen/logrus v1.9.3
1315
github.com/spf13/cobra v1.8.0
@@ -30,6 +32,7 @@ require (
3032
github.com/go-logr/logr v1.4.1 // indirect
3133
github.com/google/go-cmp v0.6.0 // indirect
3234
github.com/google/go-containerregistry v0.16.1 // indirect
35+
github.com/gorilla/websocket v1.5.0 // indirect
3336
github.com/hexops/gotextdiff v1.0.3 // indirect
3437
github.com/hexops/valast v1.4.3 // indirect
3538
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/acorn-io/baaah v0.0.0-20240119160309-2a58ee757bbd h1:Zbau2J6sEPl1H4gqnEx4/TI55eZncQR5cjfPOcG2lxE=
22
github.com/acorn-io/baaah v0.0.0-20240119160309-2a58ee757bbd/go.mod h1:13nTO3svO8zTD3j9E5c86tCtK5YrKsK5sxca4Lwkbc0=
3+
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d h1:hfpNQkJ4I2b8+DbMr8m97gG67ku0uPsMzUfskVu3cHU=
4+
github.com/acorn-io/broadcaster v0.0.0-20240105011354-bfadd4a7b45d/go.mod h1:WF6FYrEqW0+ZtY5OKb21JhSL0aeL5VJoVrm+u0d4gOE=
35
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb h1:UVs3i5r0bOh3KEZV5dTsSQzG4MG/8F0m+dcYIzXOO5I=
46
github.com/acorn-io/cmd v0.0.0-20240203032901-e9e631185ddb/go.mod h1:J0xhtXVfrJk3Fz1HYz3AoJ/ON9gaHu/baTkqOvywcfo=
57
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
@@ -33,6 +35,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJY
3335
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
3436
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
3537
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
38+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
39+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3640
github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
3741
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
3842
github.com/hexops/autogold v1.3.1/go.mod h1:sQO+mQUCVfxOKPht+ipDSkJ2SCJ7BNJVHZexsXqWMx4=
@@ -66,6 +70,8 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd
6670
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
6771
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
6872
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
73+
github.com/olahol/melody v1.1.4 h1:RQHfKZkQmDxI0+SLZRNBCn4LiXdqxLKRGSkT8Dyoe/E=
74+
github.com/olahol/melody v1.1.4/go.mod h1:GgkTl6Y7yWj/HtfD48Q5vLKPVoZOH+Qqgfa7CvJgJM4=
6975
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
7076
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
7177
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=

pkg/cli/gptscript.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/acorn-io/gptscript/pkg/mvl"
1717
"github.com/acorn-io/gptscript/pkg/openai"
1818
"github.com/acorn-io/gptscript/pkg/runner"
19+
"github.com/acorn-io/gptscript/pkg/server"
1920
"github.com/acorn-io/gptscript/pkg/version"
2021
"github.com/spf13/cobra"
2122
"golang.org/x/term"
@@ -28,14 +29,16 @@ type (
2829
type GPTScript struct {
2930
runner.Options
3031
DisplayOptions
31-
Debug bool `usage:"Enable debug logging"`
32-
Quiet bool `usage:"No output logging" short:"q"`
33-
Output string `usage:"Save output to a file" short:"o"`
34-
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
35-
SubTool string `usage:"Use tool of this name, not the first tool in file"`
36-
Assemble bool `usage:"Assemble tool to a single artifact, saved to --output"`
37-
ListModels bool `usage:"List the models available and exit"`
38-
ListTools bool `usage:"List built-in tools and exit"`
32+
Debug bool `usage:"Enable debug logging"`
33+
Quiet bool `usage:"No output logging" short:"q"`
34+
Output string `usage:"Save output to a file" short:"o"`
35+
Input string `usage:"Read input from a file (\"-\" for stdin)" short:"f"`
36+
SubTool string `usage:"Use tool of this name, not the first tool in file"`
37+
Assemble bool `usage:"Assemble tool to a single artifact, saved to --output"`
38+
ListModels bool `usage:"List the models available and exit"`
39+
ListTools bool `usage:"List built-in tools and exit"`
40+
Server bool `usage:"Start server"`
41+
ListenAddress string `usage:"Server listen address" default:"127.0.0.1:9090"`
3942
}
4043

4144
func New() *cobra.Command {
@@ -75,13 +78,21 @@ func (r *GPTScript) listModels(ctx context.Context) error {
7578
}
7679

7780
func (r *GPTScript) Pre(cmd *cobra.Command, args []string) error {
81+
if r.Quiet {
82+
if term.IsTerminal(int(os.Stdout.Fd())) {
83+
r.Quiet = false
84+
} else {
85+
r.Quiet = true
86+
}
87+
}
88+
7889
if r.Debug {
7990
mvl.SetDebug()
8091
} else {
8192
mvl.SetSimpleFormat()
82-
}
83-
if r.Quiet {
84-
mvl.SetError()
93+
if r.Quiet {
94+
mvl.SetError()
95+
}
8596
}
8697
return nil
8798
}
@@ -95,6 +106,18 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
95106
return r.listTools(cmd.Context())
96107
}
97108

109+
if r.Server {
110+
s, err := server.New(server.Options{
111+
CacheOptions: r.CacheOptions,
112+
OpenAIOptions: r.OpenAIOptions,
113+
ListenAddress: r.ListenAddress,
114+
})
115+
if err != nil {
116+
return err
117+
}
118+
return s.Start(cmd.Context())
119+
}
120+
98121
if len(args) == 0 {
99122
return fmt.Errorf("scripts argument required")
100123
}
@@ -118,16 +141,12 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
118141
return assemble.Assemble(cmd.Context(), prg, out)
119142
}
120143

121-
if !r.Quiet {
122-
if !term.IsTerminal(int(os.Stdout.Fd())) {
123-
r.Quiet = true
124-
}
125-
}
126-
127144
runner, err := runner.New(r.Options, runner.Options{
128-
CacheOptions: r.CacheOptions,
129-
OpenAIOptions: r.OpenAIOptions,
130-
MonitorFactory: monitor.NewConsole(monitor.Options(r.DisplayOptions)),
145+
CacheOptions: r.CacheOptions,
146+
OpenAIOptions: r.OpenAIOptions,
147+
MonitorFactory: monitor.NewConsole(monitor.Options(r.DisplayOptions), monitor.Options{
148+
DisplayProgress: !r.Quiet,
149+
}),
131150
})
132151
if err != nil {
133152
return err

pkg/monitor/display.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@ import (
1616
)
1717

1818
type Options struct {
19-
LiveOutput bool `usage:"-"`
20-
DumpState string `usage:"Dump the internal execution state to a file"`
19+
DisplayProgress bool `usage:"-"`
20+
DumpState string `usage:"Dump the internal execution state to a file"`
2121
}
2222

2323
func complete(opts ...Options) (result Options) {
2424
for _, opt := range opts {
2525
result.DumpState = types.FirstSet(opt.DumpState, result.DumpState)
26-
result.LiveOutput = types.FirstSet(opt.LiveOutput, result.LiveOutput)
26+
result.DisplayProgress = types.FirstSet(opt.DisplayProgress, result.DisplayProgress)
2727
}
2828
return
2929
}
3030

3131
type Console struct {
32-
dumpState string
32+
dumpState string
33+
displayProgress bool
3334
}
3435

3536
var runID int64
3637

3738
func (c *Console) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
3839
id := atomic.AddInt64(&runID, 1)
39-
mon := newDisplay(c.dumpState)
40+
mon := newDisplay(c.dumpState, c.displayProgress)
4041
mon.dump.ID = fmt.Sprint(id)
4142
mon.dump.Program = prg
4243
mon.dump.Input = input
@@ -175,17 +176,21 @@ func (d *display) Stop(output string, err error) {
175176
func NewConsole(opts ...Options) *Console {
176177
opt := complete(opts...)
177178
return &Console{
178-
dumpState: opt.DumpState,
179+
dumpState: opt.DumpState,
180+
displayProgress: opt.DisplayProgress,
179181
}
180182
}
181183

182-
func newDisplay(dumpState string) *display {
183-
return &display{
184+
func newDisplay(dumpState string, progress bool) *display {
185+
display := &display{
184186
dumpState: dumpState,
185-
livePrinter: &livePrinter{
187+
}
188+
if progress {
189+
display.livePrinter = &livePrinter{
186190
lastLines: map[string]string{},
187-
},
191+
}
188192
}
193+
return display
189194
}
190195

191196
func (d *display) Dump(out io.Writer) error {

pkg/runner/runner.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ func (r *Runner) Run(ctx context.Context, prg types.Program, env []string, input
8383
}
8484

8585
type Event struct {
86-
Time time.Time
87-
CallContext *engine.Context
88-
ToolResults int
89-
Type EventType
90-
ChatCompletionID string
91-
ChatRequest any
92-
ChatResponse any
93-
ChatResponseCached bool
94-
Content string
86+
Time time.Time `json:"time,omitempty"`
87+
CallContext *engine.Context `json:"callContext,omitempty"`
88+
ToolResults int `json:"toolResults,omitempty"`
89+
Type EventType `json:"type,omitempty"`
90+
ChatCompletionID string `json:"chatCompletionId,omitempty"`
91+
ChatRequest any `json:"chatRequest,omitempty"`
92+
ChatResponse any `json:"chatResponse,omitempty"`
93+
ChatResponseCached bool `json:"chatResponseCached,omitempty"`
94+
Content string `json:"content,omitempty"`
9595
}
9696

9797
type EventType string

pkg/server/log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package server
2+
3+
import "github.com/acorn-io/gptscript/pkg/mvl"
4+
5+
var log = mvl.Package()

0 commit comments

Comments
 (0)