Skip to content

Commit

Permalink
ai: improved workflow, prompts can be excluded if not single module
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Nov 22, 2024
1 parent 2582149 commit 6ae961a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.4-git
0.9.4
2 changes: 1 addition & 1 deletion internal/config/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"weight": 5,
"placeholder": "AI",
"name": "ai",
"icon": "accessories-calculator",
"icon": "help-browser",
"switcher_only": true,
"anthropic": {
"model": "claude-3-5-sonnet-20241022",
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ type Anthropic struct {
}

type Prompt struct {
Label string `mapstructure:"label"`
Prompt string `mapstructure:"prompt"`
Label string `mapstructure:"label"`
Prompt string `mapstructure:"prompt"`
SingleModuleOnly bool `mapstructure:"single_module_only"`
}

type Calc struct {
Expand Down
31 changes: 19 additions & 12 deletions internal/modules/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type AI struct {
entries []util.Entry
anthropicKey string
currentPrompt string
canProcess bool
currenctMessages []AnthropicMessage
currentScroll *gtk.ScrolledWindow
}
Expand Down Expand Up @@ -82,11 +83,11 @@ func (ai *AI) SetupData(cfg *config.Config, ctx context.Context) {
Label: v.Label,
Sub: "Claude 3.5",
Exec: "",
ScoreFinal: 100,
RecalculateScore: false,
Matching: util.AlwaysTop,
RecalculateScore: true,
Matching: util.Fuzzy,
SpecialFunc: ai.SpecialFunc,
SpecialFuncArgs: []interface{}{"anthropic", v.Prompt},
SingleModuleOnly: v.SingleModuleOnly,
})
}

Expand Down Expand Up @@ -126,7 +127,7 @@ type AnthropicResponse struct {
} `json:"content,omitempty"`
}

func (ai *AI) anthropic(query string, prompt string, scroll *gtk.ScrolledWindow, setupLabelWidgetStyle func(label *gtk.Label, style *config.LabelWidget), labelWidgetStyle *config.LabelWidget, spinner *gtk.Spinner) {
func (ai *AI) anthropic(query string, scroll *gtk.ScrolledWindow, setupLabelWidgetStyle func(label *gtk.Label, style *config.LabelWidget), labelWidgetStyle *config.LabelWidget) {
box := scroll.Child().(*gtk.Viewport).Child().(*gtk.Box)

queryMsg := AnthropicMessage{
Expand All @@ -141,6 +142,7 @@ func (ai *AI) anthropic(query string, prompt string, scroll *gtk.ScrolledWindow,
}

messages = append(messages, queryMsg)
spinner := gtk.NewSpinner()

glib.IdleAdd(func() {
l := gtk.NewLabel(fmt.Sprintf(">> %s", queryMsg.Content))
Expand All @@ -150,12 +152,16 @@ func (ai *AI) anthropic(query string, prompt string, scroll *gtk.ScrolledWindow,
setupLabelWidgetStyle(l, labelWidgetStyle)

box.Append(l)

spinner.SetSpinning(true)

box.Append(spinner)
})

req := AnthropicRequest{
Model: ai.config.Anthropic.Model,
MaxTokens: ai.config.Anthropic.MaxTokens,
System: prompt,
System: ai.currentPrompt,
Messages: messages,
}

Expand Down Expand Up @@ -194,7 +200,7 @@ func (ai *AI) anthropic(query string, prompt string, scroll *gtk.ScrolledWindow,
ai.currenctMessages = messages

glib.IdleAdd(func() {
spinner.SetVisible(false)
box.Remove(spinner)

for _, v := range responseMessages {
label := gtk.NewLabel(v.Content)
Expand Down Expand Up @@ -227,18 +233,19 @@ func (ai *AI) SpecialFunc(args ...interface{}) {
aiScroll := args[3].(*gtk.ScrolledWindow)
setupLabelWidgetStyle := args[4].(func(label *gtk.Label, style *config.LabelWidget))
labelWidgetStyle := args[5].(*config.LabelWidget)
spinner := args[6].(*gtk.Spinner)

if ai.currentPrompt != "" {
prompt = ai.currentPrompt
}

if ai.currentScroll == nil {
ai.currentScroll = aiScroll
}

if ai.currentPrompt == "" {
ai.currentPrompt = prompt
ai.canProcess = true
return
}

switch provider {
case "anthropic":
ai.anthropic(query, prompt, aiScroll, setupLabelWidgetStyle, labelWidgetStyle, spinner)
ai.anthropic(query, aiScroll, setupLabelWidgetStyle, labelWidgetStyle)
}
}
18 changes: 8 additions & 10 deletions internal/ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ func activateItem(keepOpen, selectNext, alt bool) {
go quit()
}

module := findModule(entry.Module, toUse, explicits)

if entry.SpecialFunc != nil {
args := []interface{}{}
args = append(args, entry.SpecialFuncArgs...)
args = append(args, elements.input.Text())

if singleModule != nil && singleModule.General().Name == cfg.Builtins.AI.Name {
if module.General().Name == cfg.Builtins.AI.Name {
isAi = true

glib.IdleAdd(func() {
Expand All @@ -414,18 +416,12 @@ func activateItem(keepOpen, selectNext, alt bool) {
box = elements.aiScroll.Child().(*gtk.Viewport).Child().(*gtk.Box)
}

spinner := gtk.NewSpinner()
spinner.SetSpinning(true)

box.Append(spinner)

setupBoxWidgetStyle(box, &layout.Window.Box.AiScroll.List.BoxWidget)

args = append(args, elements.aiScroll, setupLabelWidgetStyle, &layout.Window.Box.AiScroll.List.Item, spinner)
args = append(args, elements.aiScroll, setupLabelWidgetStyle, &layout.Window.Box.AiScroll.List.Item)

go entry.SpecialFunc(args...)
})

} else {
entry.SpecialFunc(args...)
closeAfterActivation(keepOpen, selectNext)
Expand Down Expand Up @@ -496,8 +492,6 @@ func activateItem(keepOpen, selectNext, alt bool) {
hstry.Save(identifier, strings.TrimSpace(elements.input.Text()))
}

module := findModule(entry.Module, toUse, explicits)

if module != nil && (module.General().History || module.General().Typeahead) {
history.SaveInputHistory(module.General().Name, elements.input.Text(), identifier)
}
Expand Down Expand Up @@ -796,6 +790,10 @@ func processAsync(ctx context.Context, text string) {
g := w.General()

for k := range e {
if e[k].SingleModuleOnly && singleModule == nil {
continue
}

e[k].Module = g.Name
e[k].Weight = g.Weight

Expand Down
27 changes: 14 additions & 13 deletions internal/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ type Entry struct {
Prefer bool `mapstructure:"prefer,omitempty" json:"prefer,omitempty"`

// internal
DaysSinceUsed int `mapstructure:"-"`
File string `mapstructure:"-"`
History bool `mapstructure:"-"`
LastUsed time.Time `mapstructure:"-"`
Module string `mapstructure:"-"`
OpenWindows uint `mapstructure:"-"`
Piped Piped `mapstructure:"-"`
PipedAlt Piped `mapstructure:"-"`
SpecialFunc func(args ...interface{}) `mapstructure:"-"`
SpecialFuncArgs []interface{} `mapstructure:"-"`
Used int `mapstructure:"-"`
Weight int `mapstructure:"-"`
IsAction bool `mapstructure:"-"`
DaysSinceUsed int `mapstructure:"-"`
File string `mapstructure:"-"`
History bool `mapstructure:"-"`
LastUsed time.Time `mapstructure:"-"`
Module string `mapstructure:"-"`
OpenWindows uint `mapstructure:"-"`
Piped Piped `mapstructure:"-"`
PipedAlt Piped `mapstructure:"-"`
SpecialFunc func(args ...interface{}) `mapstructure:"-"`
SpecialFuncArgs []interface{} `mapstructure:"-"`
Used int `mapstructure:"-"`
Weight int `mapstructure:"-"`
IsAction bool `mapstructure:"-"`
SingleModuleOnly bool `mapstructure:"-"`
}

func (e Entry) Identifier() string {
Expand Down

0 comments on commit 6ae961a

Please sign in to comment.