Skip to content

Commit 436cdff

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 d2054f3 commit 436cdff

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
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: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ func New(opts ...Options) (*Server, error) {
4949

5050
runnerOpts, opt := complete(opts)
5151
r, err := runner.New(append(runnerOpts, runner.Options{
52-
MonitorFactory: &SessionFactory{
53-
events: events,
54-
},
52+
MonitorFactory: NewSessionFactory(events),
5553
})...)
5654
if err != nil {
5755
return nil, err
@@ -61,9 +59,7 @@ func New(opts ...Options) (*Server, error) {
6159
CacheOptions: runner.CacheOptions{
6260
Cache: new(bool),
6361
},
64-
MonitorFactory: &SessionFactory{
65-
events: events,
66-
},
62+
MonitorFactory: NewSessionFactory(events),
6763
})...)
6864
if err != nil {
6965
return nil, err
@@ -102,6 +98,14 @@ var (
10298

10399
type execKey struct{}
104100

101+
func ContextWithNewID(ctx context.Context) context.Context {
102+
return context.WithValue(ctx, execKey{}, fmt.Sprint(atomic.AddInt64(&execID, 1)))
103+
}
104+
105+
func IDFromContext(ctx context.Context) string {
106+
return ctx.Value(execKey{}).(string)
107+
}
108+
105109
func (s *Server) list(rw http.ResponseWriter, req *http.Request) {
106110
rw.Header().Set("Content-Type", "application/json")
107111
enc := json.NewEncoder(rw)
@@ -283,6 +287,12 @@ type SessionFactory struct {
283287
events *broadcaster.Broadcaster[Event]
284288
}
285289

290+
func NewSessionFactory(events *broadcaster.Broadcaster[Event]) *SessionFactory {
291+
return &SessionFactory{
292+
events: events,
293+
}
294+
}
295+
286296
func (s SessionFactory) Start(ctx context.Context, prg *types.Program, env []string, input string) (runner.Monitor, error) {
287297
id, _ := ctx.Value(execKey{}).(string)
288298

0 commit comments

Comments
 (0)