Skip to content

Commit 4e14257

Browse files
committed
feat: add a few helper functions for using code outside of this package
Signed-off-by: Donnie Adams <[email protected]>
1 parent 6e73e5c commit 4e14257

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pkg/mvl/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func SetError() {
4646

4747
func Package() Logger {
4848
_, p, _, _ := runtime.Caller(1)
49-
_, suffix, _ := strings.Cut(p, "gptscript/")
49+
_, suffix, _ := strings.Cut(p, "gptscript")
5050
i := strings.LastIndex(suffix, "/")
5151
return New(suffix[:i])
5252
}

pkg/server/server.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ func New(model engine.Model, opts ...Options) (*Server, error) {
4545

4646
opt := complete(opts)
4747
r, err := runner.New(model, runner.Options{
48-
MonitorFactory: &SessionFactory{
49-
events: events,
50-
},
48+
MonitorFactory: NewSessionFactory(events),
5149
})
5250
if err != nil {
5351
return nil, err
@@ -84,6 +82,14 @@ var (
8482

8583
type execKey struct{}
8684

85+
func ContextWithNewID(ctx context.Context) context.Context {
86+
return context.WithValue(ctx, execKey{}, fmt.Sprint(atomic.AddInt64(&execID, 1)))
87+
}
88+
89+
func IDFromContext(ctx context.Context) string {
90+
return ctx.Value(execKey{}).(string)
91+
}
92+
8793
func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
8894
rw.Header().Set("Content-Type", "application/json")
8995
enc := json.NewEncoder(rw)
@@ -276,6 +282,12 @@ type SessionFactory struct {
276282
events *broadcaster.Broadcaster[Event]
277283
}
278284

285+
func NewSessionFactory(events *broadcaster.Broadcaster[Event]) *SessionFactory {
286+
return &SessionFactory{
287+
events: events,
288+
}
289+
}
290+
279291
func (s SessionFactory) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
280292
id, _ := ctx.Value(execKey{}).(string)
281293

0 commit comments

Comments
 (0)