Skip to content

Commit c6d0a73

Browse files
committed
moving to sdk
1 parent 5be276e commit c6d0a73

File tree

3 files changed

+41
-78
lines changed

3 files changed

+41
-78
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ extension/dist
1313
.tmp/
1414

1515
.yarn
16+
17+
.calcit-snippets/

compact.cirru

Lines changed: 38 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{} (:about "|file is generated - never edit directly; learn cr edit/tree workflows before changing") (:package |app)
33
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
4-
:modules $ [] |respo.calcit/ |lilac/ |memof/ |respo-ui.calcit/ |reel.calcit/ |respo-markdown.calcit/ |alerts.calcit/ |respo-feather.calcit/
4+
:modules $ [] |respo.calcit/ |lilac/ |memof/ |respo-ui.calcit/ |reel.calcit/ |respo-markdown.calcit/ |alerts.calcit/ |respo-feather.calcit/ |genai.calcit/
55
:entries $ {}
66
:files $ {}
77
|app.comp.container $ %{} :FileEntry
@@ -113,8 +113,7 @@
113113
:code $ quote
114114
defn call-flash-imagen-msg! (variant cursor state prompt-text d!) (hint-fn async)
115115
if (nil? @*gen-ai-new)
116-
reset! *gen-ai-new $ new GoogleGenAI
117-
js-object $ :apiKey (get-gemini-key!)
116+
reset! *gen-ai-new $ sdk/new-client (get-gemini-key!)
118117
if-let
119118
target $ js/document.querySelector "\".show-image"
120119
.!setAttribute target "\"src" "\""
@@ -126,23 +125,15 @@
126125
-> state (assoc :answer nil) (assoc :loading? true)
127126
let
128127
selected $ js-await (get-selected)
129-
gen-ai $ let
130-
ai @*gen-ai-new
131-
js/console.log ai
132-
, ai
128+
gen-ai @*gen-ai-new
133129
content $ .!replace prompt-text "\"{{selected}}" (or selected "\"<未找到选中内容>")
134130
sdk-result $ js-await
135-
.!generateContent (.-models gen-ai)
136-
js-object (:model "\"gemini-2.5-flash-image") (:contents content)
137-
:config $ js-object
138-
:httpOptions $ js-object (:baseUrl |https://ja.chenyong.life)
139-
:signal $ let
140-
abort $ new js/AbortController
141-
reset! *abort-control abort
142-
.-signal abort
143-
:responseModalities $ js-array (.-TEXT Modality) (.-IMAGE Modality)
131+
sdk/generate-content! gen-ai $ {} (:model "\"gemini-2.5-flash-image") (:contents content)
132+
:abort-signal $ sdk/make-abort-signal *abort-control
133+
:http-options $ sdk/make-http-options |https://ja.chenyong.life
134+
:response-modalities $ js-array "\"TEXT" "\"IMAGE"
144135
*text $ atom "\""
145-
js-await $ -> sdk-result .-candidates .-0 .-content .-parts
136+
js-await $ -> (sdk/extract-content-parts sdk-result)
146137
.!forEach $ fn (? chunk _a _b)
147138
if (some? chunk)
148139
if-let
@@ -170,17 +161,14 @@
170161
:code $ quote
171162
defn call-genai-msg! (variant cursor state prompt-text search? think? d! *text *thinking-text) (hint-fn async)
172163
if (nil? @*gen-ai-new)
173-
reset! *gen-ai-new $ new GoogleGenAI
174-
js-object $ :apiKey (get-gemini-key!)
164+
reset! *gen-ai-new $ sdk/new-client (get-gemini-key!)
175165
if-let
176166
abort $ deref *abort-control
177167
do (js/console.warn "\"Aborting prev") (.!abort abort)
178168
let
179169
selected $ if (.includes? prompt-text "\"{{selected}}")
180170
js-await $ get-selected
181-
gen-ai $ let
182-
ai @*gen-ai-new
183-
, ai
171+
gen-ai @*gen-ai-new
184172
model $ pick-model variant
185173
content $ .!replace prompt-text "\"{{selected}}" (or selected "\"<未找到选中内容>")
186174
json? $ or (.!includes prompt-text "\"{{json}}") (.!includes prompt-text "\"{{JSON}}")
@@ -189,35 +177,17 @@
189177
messages0 $ or (:messages state) ([])
190178
messages1 $ upsert-assistant-message messages0 "\"" nil
191179
sdk-result $ js-await
192-
.!generateContentStream (.-models gen-ai)
193-
js-object (:model model)
194-
:contents $ messages->gemini messages0
195-
:config $ js/Object.assign
196-
js-object
197-
:thinkingConfig $ if think?
198-
js-object
199-
:thinkingBudget $ get-env "\"think-budget" (if pro? 3200 800)
200-
:includeThoughts think?
201-
js-object (:thinkingBudget 0) (:includeThoughts false)
202-
:httpOptions $ js-object (:baseUrl |https://ja.chenyong.life)
203-
:tools $ let
204-
t $ ->
205-
js-array
206-
if search? $ js-object
207-
:googleSearch $ js-object
208-
if has-url? $ js-object
209-
:urlContext $ js-object
210-
.!filter $ fn (x & _a) x
211-
if
212-
= 0 $ .-length t
213-
, js/undefined t
214-
:abortSignal $ let
215-
abort $ new js/AbortController
216-
reset! *abort-control abort
217-
.-signal abort
218-
if json?
219-
js-object $ "\"responseMimeType" "\"application/json"
220-
, js/undefined
180+
sdk/generate-content-stream! gen-ai $ {} (:model model)
181+
:contents $ sdk/messages->contents messages0
182+
:thinking-config $ if think?
183+
sdk/make-thinking-config
184+
get-env "\"think-budget" $ if pro? 3200 800
185+
, true
186+
sdk/make-thinking-config 0 false
187+
:tools $ sdk/make-search-tools search? has-url?
188+
:abort-signal $ sdk/make-abort-signal *abort-control
189+
:http-options $ sdk/make-http-options |https://ja.chenyong.life
190+
:response-mime-type $ if json? "\"application/json" nil
221191
do
222192
js/setTimeout $ fn ()
223193
d! $ :: :states-merge cursor state
@@ -226,18 +196,16 @@
226196
fn (? chunk)
227197
if (some? chunk)
228198
let
229-
part js/chunk.candidates?.[0]?.content?.parts?.[0]
230-
is-thinking? $ if (some? part) (.-thought part) false
231-
t $ if (some? part) (.-text part) (.-text chunk)
232-
let
233-
text $ or t (-> chunk .?-promptFeedback .?-blockReason) |__BLANK__
234-
if is-thinking? (swap! *thinking-text str text) (swap! *text str text)
235-
d! $ :: :states-merge cursor state
236-
{} (:answer @*text) (:thinking @*thinking-text) (:loading? false) (:done? false)
237-
:messages $ upsert-assistant-message messages1 @*text @*thinking-text
238-
d! $ :: :states-merge cursor state
239-
{} (:answer @*text) (:thinking @*thinking-text) (:loading? false) (:done? false)
240-
:messages $ upsert-assistant-message messages1 @*text @*thinking-text
199+
info $ sdk/extract-stream-chunk chunk
200+
is-thinking? $ :thinking? info
201+
text $ or (:text info) |__BLANK__
202+
if is-thinking? (swap! *thinking-text str text) (swap! *text str text)
203+
d! $ :: :states-merge cursor state
204+
{} (:answer @*text) (:thinking @*thinking-text) (:loading? false) (:done? false)
205+
:messages $ upsert-assistant-message messages1 @*text @*thinking-text
206+
d! $ :: :states-merge cursor state
207+
{} (:answer @*text) (:thinking @*thinking-text) (:loading? false) (:done? false)
208+
:messages $ upsert-assistant-message messages1 @*text @*thinking-text
241209
d! $ :: :states-merge cursor state
242210
{} (:answer @*text) (:thinking @*thinking-text) (:loading? false) (:done? true)
243211
:messages $ upsert-assistant-message messages1 @*text @*thinking-text
@@ -246,8 +214,7 @@
246214
:code $ quote
247215
defn call-imagen-4-msg! (variant cursor state prompt-text d!) (hint-fn async)
248216
if (nil? @*gen-ai-new)
249-
reset! *gen-ai-new $ new GoogleGenAI
250-
js-object $ :apiKey (get-gemini-key!)
217+
reset! *gen-ai-new $ sdk/new-client (get-gemini-key!)
251218
if-let
252219
target $ js/document.querySelector "\".show-image"
253220
.!removeAttribute target "\"src"
@@ -259,21 +226,14 @@
259226
-> state (assoc :answer nil) (assoc :loading? true)
260227
let
261228
selected $ js-await (get-selected)
262-
gen-ai $ let
263-
ai @*gen-ai-new
264-
, ai
229+
gen-ai @*gen-ai-new
265230
response $ js-await
266-
.!generateImages (.-models gen-ai)
267-
js-object (:model "\"imagen-4.0-generate-001") (:prompt prompt-text)
268-
:config $ js-object (:numberOfImages 1) (:includeRaiReason true)
269-
:httpOptions $ js-object (:baseUrl |https://ja.chenyong.life)
270-
:signal $ let
271-
abort $ new js/AbortController
272-
reset! *abort-control abort
273-
.-signal abort
231+
sdk/generate-images! gen-ai $ {} (:model "\"imagen-4.0-generate-001") (:prompt prompt-text) (:number-of-images 1) (:include-rai-reason true)
232+
:abort-signal $ sdk/make-abort-signal *abort-control
233+
:http-options $ sdk/make-http-options |https://ja.chenyong.life
274234
*text $ atom "\""
275235
if-let
276-
image-data $ -> response .-generatedImages .-0 .-image .-imageBytes
236+
image-data $ sdk/extract-image-bytes response
277237
let
278238
image-blob $ base64ToBlob image-data
279239
url $ js/URL.createObjectURL image-blob
@@ -1176,11 +1136,11 @@
11761136
respo-ui.comp :refer $ comp-copy comp-close
11771137
"\"../extension/get-selected" :refer $ get-selected
11781138
memof.once :refer $ memof1-call memof1-call-by
1179-
"\"@google/genai" :refer $ GoogleGenAI Modality
11801139
"\"../lib/image" :refer $ base64ToBlob
11811140
"\"openai" :default OpenAI
11821141
feather.core :refer $ comp-i
11831142
respo-alerts.core :refer $ [] use-modal-menu use-prompt use-drawer
1143+
genai.sdk :as sdk
11841144
:examples $ []
11851145
|app.config $ %{} :FileEntry
11861146
:defs $ {}

deps.cirru

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
|Respo/respo.calcit |0.16.28
99
|calcit-lang/lilac |main
1010
|calcit-lang/memof |0.0.22
11+
|Memkits/genai.calcit |0.0.2

0 commit comments

Comments
 (0)