Skip to content

Commit

Permalink
Add a command to run in headful version mid-conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
noahshinn committed Dec 7, 2023
1 parent b706fdc commit ea946d1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go run ./cmd/shell/shell.go -url scholar.google.com -headful
Built-in commands:

- `help`: prints some usage instructions
- `headful`: if the current browser is running headless, open the headful representation
- `log`: logs the current browser and trajectory to the specified log path
- `exit`: gracefully exits the shell

Expand Down
72 changes: 54 additions & 18 deletions browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"collaborativebrowser/trajectory"
"collaborativebrowser/translators"
"collaborativebrowser/translators/html2md"
"collaborativebrowser/utils/slicesx"
"context"
"errors"
"fmt"
"log"
"sync"
"time"

Expand All @@ -17,13 +19,14 @@ import (
)

type Browser struct {
mu *sync.Mutex
ctx context.Context
cancel context.CancelFunc
options []BrowserOption
vIDGenerator virtualid.VirtualIDGenerator
translators map[language.Language]translators.Translator
display *BrowserDisplay
mu *sync.Mutex
ctx context.Context
cancel context.CancelFunc
options []BrowserOption
vIDGenerator virtualid.VirtualIDGenerator
translators map[language.Language]translators.Translator
display *BrowserDisplay
isRunningHeadless bool
}

type BrowserOption string
Expand Down Expand Up @@ -216,8 +219,28 @@ func (b *Browser) Cancel() {
b.cancel()
}

func NewBrowser(ctx context.Context, options ...BrowserOption) *Browser {
vIDGenerator := virtualid.NewIncrIntVirtualIDGenerator()
func (b *Browser) IsRunningHeadless() bool {
return b.isRunningHeadless
}

func (b *Browser) RunHeadful(ctx context.Context) error {
if !b.isRunningHeadless {
log.Println("requested to run the browser in headful mode but this browser is already running in headful mode")
return nil
}
log.Println("running the browser in headful mode; warning: you will lose all non-location state from the current browser")
newOps := append(b.options, BrowserOptionHeadful)
newBrowserCtx, newBrowserCancelFunc := newBrowser(ctx, newOps...)
b.ctx = newBrowserCtx
b.cancel = newBrowserCancelFunc
if err := b.Navigate(b.display.Location); err != nil {
return fmt.Errorf("error navigating to current location %s: %w", b.display.Location, err)
}
b.isRunningHeadless = false
return nil
}

func buildOptions(options ...BrowserOption) []func(*chromedp.ExecAllocator) {
ops := chromedp.DefaultExecAllocatorOptions[:]
for _, option := range options {
switch option {
Expand All @@ -229,19 +252,32 @@ func NewBrowser(ctx context.Context, options ...BrowserOption) *Browser {
default:
}
}
return ops
}

func newBrowser(ctx context.Context, options ...BrowserOption) (browserCtx context.Context, cancelFunc context.CancelFunc) {
ops := buildOptions(options...)
parentCtx, _ := chromedp.NewExecAllocator(ctx, ops...)
browserCtx, cancel := chromedp.NewContext(parentCtx)
return browserCtx, cancel
}

func NewBrowser(ctx context.Context, options ...BrowserOption) *Browser {
isRunningHeadless := !slicesx.Contains(options, BrowserOptionHeadful)
vIDGenerator := virtualid.NewIncrIntVirtualIDGenerator()
htmlToMDTranslator := html2md.NewHTML2MDTranslator(nil)
translatorMap := map[language.Language]translators.Translator{
language.LanguageMD: htmlToMDTranslator,
}
parentCtx, _ := chromedp.NewExecAllocator(context.Background(), ops...)
browserCtx, cancel := chromedp.NewContext(parentCtx)
browserCtx, cancel := newBrowser(ctx, options...)
return &Browser{
mu: &sync.Mutex{},
ctx: browserCtx,
cancel: cancel,
options: options,
vIDGenerator: vIDGenerator,
translators: translatorMap,
display: &BrowserDisplay{},
mu: &sync.Mutex{},
ctx: browserCtx,
cancel: cancel,
options: options,
vIDGenerator: vIDGenerator,
translators: translatorMap,
display: &BrowserDisplay{},
isRunningHeadless: isRunningHeadless,
}
}
13 changes: 9 additions & 4 deletions cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ func main() {
runner.Log()
printx.PrintStandardHeader("TRAJECTORY")
fmt.Println()
for _, item := range runner.Trajectory().Items {
fmt.Println(item.GetAbbreviatedText())
}
runner.DisplayTrajectory()

scanner := bufio.NewScanner(os.Stdin)
fmt.Print("user: ")
Expand All @@ -77,6 +75,13 @@ ScannerLoop:
case "":
fmt.Print("user: ")
continue ScannerLoop
case "headful":
printx.PrintInColor(printx.ColorGray, "Running browser in headful mode...")
if err := runner.RunHeadful(); err != nil {
printx.PrintInColor(printx.ColorYellow, fmt.Sprintf("Failed to run browser in headful mode: %s, continuing in headless mode", err.Error()))
}
fmt.Print("user: ")
continue ScannerLoop
case "exit":
fmt.Println("\nexiting...")
runner.Terminate()
Expand All @@ -91,7 +96,7 @@ ScannerLoop:
fmt.Print("user: ")
continue ScannerLoop
default:
runner.Trajectory().AddItem(trajectory.NewMessage(trajectory.MessageAuthorUser, userMessageText))
runner.AddItemToTrajectory(trajectory.NewMessage(trajectory.MessageAuthorUser, userMessageText))
stream, err := runner.RunAndStream()
if err != nil {
panic(fmt.Errorf("failed to run and stream: %w", err))
Expand Down
18 changes: 10 additions & 8 deletions runner/finiterunner/finiterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,14 @@ func (r *FiniteRunner) RunAndStream() (<-chan *trajectory.TrajectoryStreamEvent,
return stream, nil
}

func (r *FiniteRunner) Trajectory() *trajectory.Trajectory {
return r.trajectory
func (r *FiniteRunner) AddItemToTrajectory(item trajectory.TrajectoryItem) {
r.trajectory.AddItem(item)
}

func (r *FiniteRunner) Actor() actorstrategy.ActorStrategy {
return r.actor
}

func (r *FiniteRunner) Browser() *browser.Browser {
return r.browser
func (r *FiniteRunner) DisplayTrajectory() {
for _, item := range r.trajectory.Items {
fmt.Println(item.GetAbbreviatedText())
}
}

func (r *FiniteRunner) Log() error {
Expand All @@ -202,6 +200,10 @@ func (r *FiniteRunner) Log() error {
}
}

func (r *FiniteRunner) RunHeadful() error {
return r.browser.RunHeadful(r.ctx)
}

func (r *FiniteRunner) Terminate() {
r.browser.Cancel()
}
8 changes: 3 additions & 5 deletions runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package runner

import (
"collaborativebrowser/actor/actorstrategy"
"collaborativebrowser/browser"
"collaborativebrowser/trajectory"
)

type Runner interface {
Run() error
RunAndStream() (<-chan *trajectory.TrajectoryStreamEvent, error)
Actor() actorstrategy.ActorStrategy
Trajectory() *trajectory.Trajectory
Browser() *browser.Browser
AddItemToTrajectory(item trajectory.TrajectoryItem)
DisplayTrajectory()
RunHeadful() error
Log() error
Terminate()
}

0 comments on commit ea946d1

Please sign in to comment.