Skip to content

Commit c8a3ee1

Browse files
author
Yetibot
committed
feat(grokimage): add a separate, uninfected command for Grok image generation
1 parent f5acb56 commit c8a3ee1

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns yetibot.core.commands.grokimage
2+
(:require [taoensso.timbre :refer [info error]]
3+
[yetibot.core.hooks :refer [cmd-hook]]
4+
[yetibot.core.util.image-input :as image-input]
5+
[yetibot.core.util.gemini :as gemini]
6+
[yetibot.core.util.xai :as xai]
7+
[yetibot.core.webapp.routes.images :refer [store-image!]]))
8+
9+
(defn grokimage-cmd
10+
"grokimage <prompt> (or grokimg <prompt>) # generate an image using grok image 2.0"
11+
{:yb/cat #{:img}}
12+
[{:keys [match chat-source]}]
13+
(if (xai/configured?)
14+
(try
15+
(let [{:keys [prompt image-urls]} (image-input/extract-images match chat-source)
16+
_ (info "grokimage: generating image for prompt:" prompt
17+
(when (seq image-urls) (str " with " (count image-urls) " image(s)")))
18+
image (xai/generate-image prompt nil image-urls)
19+
id (store-image! image)
20+
base-url (gemini/yetibot-base-url)
21+
image-url (format "%s/generated-images/%s.png" base-url id)
22+
footer (format "\n\nSent via grok-imagine-image-2.0 | Cost: $%.2f" xai/cost-per-image)]
23+
(info "grokimage: image generated successfully, serving at" image-url)
24+
{:result/value (str image-url footer)
25+
:result/data {:id id :prompt match :url image-url}})
26+
(catch Exception e
27+
(error "grokimage: image generation error:" (.getMessage e))
28+
{:result/error (str "Image generation failed: " (.getMessage e))}))
29+
{:result/error
30+
"xAI API is not configured. Set `xai.key` in config."}))
31+
32+
(cmd-hook #"grokimage|grokimg|grok-image"
33+
#".*" grokimage-cmd)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns yetibot.core.test.commands.grokimage-test
2+
(:require [midje.sweet :refer [facts fact => contains provided]]
3+
[yetibot.core.commands.grokimage :as grokimg]
4+
[yetibot.core.util.xai :as xai]
5+
[yetibot.core.util.gemini :as gemini]))
6+
7+
(facts "about grokimage-cmd"
8+
(fact "it returns an error if xAI is not configured"
9+
(grokimg/grokimage-cmd {:match "grokimage space kitty" :chat-source {}}) => (contains {:result/error string?})
10+
(provided (xai/configured?) => false))
11+
12+
(fact "it generates an image and returns URL with model footer when configured, without any system instruction"
13+
(grokimg/grokimage-cmd {:match "grokimage space kitty" :chat-source {}})
14+
=> (contains {:result/value #"http://localhost:3003/generated-images/grok123.png\n\nSent via grok-imagine-image-2.0 \| Cost: \$0.04"
15+
:result/data {:id "grok123" :prompt "grokimage space kitty" :url "http://localhost:3003/generated-images/grok123.png"}})
16+
(provided
17+
(xai/configured?) => true
18+
(yetibot.core.util.image-input/extract-images "grokimage space kitty" {}) => {:prompt "space kitty" :image-urls []}
19+
(xai/generate-image "space kitty" nil []) => {:data "grokbytes" :mime-type "image/jpeg"}
20+
(yetibot.core.webapp.routes.images/store-image! {:data "grokbytes" :mime-type "image/jpeg"}) => "grok123"
21+
(gemini/yetibot-base-url) => "http://localhost:3003"))
22+
23+
(fact "it generates an edited image when image-urls/attachments are provided, without any system instruction"
24+
(grokimg/grokimage-cmd {:match "sketch this" :chat-source {:raw-event {:attachments [{:url "https://example.com/source.jpg" :content-type "image/jpeg"}]}}})
25+
=> (contains {:result/value #"http://localhost:3003/generated-images/grok123.png\n\nSent via grok-imagine-image-2.0 \| Cost: \$0.04"
26+
:result/data {:id "grok123" :prompt "sketch this" :url "http://localhost:3003/generated-images/grok123.png"}})
27+
(provided
28+
(xai/configured?) => true
29+
(yetibot.core.util.image-input/extract-images "sketch this" (contains {:raw-event map?})) => {:prompt "sketch this" :image-urls ["https://example.com/source.jpg"]}
30+
(xai/generate-image "sketch this" nil ["https://example.com/source.jpg"]) => {:data "grokbytes" :mime-type "image/jpeg"}
31+
(yetibot.core.webapp.routes.images/store-image! {:data "grokbytes" :mime-type "image/jpeg"}) => "grok123"
32+
(gemini/yetibot-base-url) => "http://localhost:3003")))

0 commit comments

Comments
 (0)