Skip to content

Commit 84f32b3

Browse files
author
Tony Worm
committed
(human) small fixes and extra checks
1 parent 4efa407 commit 84f32b3

File tree

5 files changed

+131
-35
lines changed

5 files changed

+131
-35
lines changed

hack/genai.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
"google.golang.org/genai"
10+
)
11+
12+
func handleErr(err error) {
13+
if err != nil {
14+
fmt.Println("ERROR:", err)
15+
os.Exit(1)
16+
}
17+
}
18+
19+
func main() {
20+
ctx := context.Background()
21+
project := os.Getenv("GOOGLE_CLOUD_PROJECT")
22+
location := os.Getenv("GOOGLE_CLOUD_LOCATION")
23+
24+
client, err := genai.NewClient(ctx, &genai.ClientConfig{
25+
Project: project,
26+
Location: location,
27+
Backend: genai.BackendVertexAI,
28+
})
29+
handleErr(err)
30+
31+
for result, err := range client.Models.GenerateContentStream(
32+
ctx,
33+
"gemini-3-flash-preview",
34+
genai.Text("Why is the sky blue?"),
35+
&genai.GenerateContentConfig{
36+
MaxOutputTokens: 512,
37+
},
38+
) {
39+
if err != nil {
40+
handleErr(err)
41+
}
42+
fmt.Print(result.Candidates[0].Content.Parts[0].Text)
43+
}
44+
45+
// // Call the GenerateContent method.
46+
// result, err := client.Models.GenerateContent(ctx,
47+
// "gemini-2.5-flash",
48+
// genai.Text("Tell me about New York?"),
49+
// &genai.GenerateContentConfig{
50+
// Temperature: genai.Ptr[float32](0.5),
51+
// TopP: genai.Ptr[float32](0.5),
52+
// TopK: genai.Ptr[float32](2.0),
53+
// ResponseMIMEType: "application/json",
54+
// StopSequences: []string{"\n"},
55+
// CandidateCount: 2,
56+
// Seed: genai.Ptr[int32](42),
57+
// MaxOutputTokens: 128,
58+
// PresencePenalty: genai.Ptr[float32](0.5),
59+
// FrequencyPenalty: genai.Ptr[float32](0.5),
60+
// },
61+
// )
62+
// handleErr(err)
63+
// debugPrint(result)
64+
65+
// ms := client.Models.All(ctx)
66+
// for m := range ms {
67+
// fmt.Println(m.Name, m.Version)
68+
// }
69+
70+
}
71+
72+
func debugPrint[T any](r *T) {
73+
response, err := json.MarshalIndent(*r, "", " ")
74+
handleErr(err)
75+
fmt.Println(string(response))
76+
}

lib/agent/agents/cue.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ func buildTools(cfg *config.Config, agt config.Agent, models map[string]model.LL
272272
}
273273

274274
func addCallbacks(cfg *config.Config, agt config.Agent, environMDs map[string]string, c *llmagent.Config) {
275+
c.OnModelErrorCallbacks = []llmagent.OnModelErrorCallback{
276+
func(ctx agent.CallbackContext, llmRequest *model.LLMRequest, llmResponseError error) (*model.LLMResponse, error) {
277+
// if llmResponseError != nil {
278+
// fmt.Printf("ModelError.%s: %s\n", ctx.AgentName(), llmResponseError)
279+
// }
280+
return nil, nil
281+
},
282+
}
283+
275284
c.BeforeAgentCallbacks = []agent.BeforeAgentCallback{
276285
func(ctx agent.CallbackContext) (*genai.Content, error) {
277286
// fmt.Printf("\nBAC.%s\n", ctx.AgentName())

lib/agent/cmd/tui/chat.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,15 @@ func (m *chatModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
130130
}
131131
case key.Matches(msg, m.keymap.send):
132132
if m.textarea.Focused() {
133-
// TODO, actually send the message
134133
content := m.textarea.Value()
135134
lines := strings.Split(content, "\n")
136135
m.root.msg = lines[0]
137-
m.root.sendMessage(content)
136+
err := m.root.sendMessage(content)
137+
if err != nil {
138+
// fmt.Println("root.sendMessage.error", err)
139+
m.root.err = err
140+
return m, nil
141+
}
138142

139143
if m.root.asession != nil {
140144
m.textarea.Reset()
@@ -144,20 +148,31 @@ func (m *chatModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
144148
sess := m.root.asession
145149
// every time we get an event...
146150
for _ = range sess.EventChan {
151+
// fmt.Println("event:", e.Author, e.ID)
147152
// load the full lasest (being lazy, but also don't have to deal with state deltas and updates)
148153
// we should send the latest state back or make a func/api for returning session details w/o event list (between list & get today)
149-
m.root.loadSession(m.root.currSid)
154+
err := m.root.loadSession(m.root.currSid)
155+
if err != nil {
156+
// fmt.Println("sess.ChatLoop.error:", err)
157+
m.root.err = err
158+
continue
159+
}
160+
150161
m.updateMessagesFromEvents()
151162
m.updateChatStatus()
152163

153164
// look for any errors
154165
select {
155166
case err := <-sess.ErrorChan:
167+
// fmt.Println("sess.ErrorChat:", err)
156168
m.root.err = err
157169
default:
158170
}
171+
// fmt.Println("loop around")
159172
}
160173
}()
174+
} else {
175+
m.root.err = fmt.Errorf("m.root.asession is nil")
161176
}
162177

163178
} else {

lib/agent/cmd/tui/list.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,25 @@ func (m *listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
112112
// if the input is not focused*
113113
switch {
114114
case key.Matches(msg, m.keymap.info):
115-
m.mode = "info"
116-
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
117-
m.root.currSid = sid
118-
m.root.loadSession(sid)
119-
m.root.updateCurrName("info")
120-
m.root.updateRootTitle()
115+
if len(m.table.Rows()) > 0 {
116+
m.mode = "info"
117+
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
118+
m.root.currSid = sid
119+
m.root.loadSession(sid)
120+
m.root.updateCurrName("info")
121+
m.root.updateRootTitle()
122+
}
121123

122124
case key.Matches(msg, m.keymap.del):
123-
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
124-
m.root.delSession(sid)
125-
err := m.updateSessions()
126-
if err != nil {
127-
m.root.err = err
125+
if len(m.table.Rows()) > 0 {
126+
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
127+
m.root.delSession(sid)
128+
err := m.updateSessions()
129+
if err != nil {
130+
m.root.err = err
131+
}
132+
m.updateRows()
128133
}
129-
m.updateRows()
130134

131135
case key.Matches(msg, m.keymap.sort):
132136
m.mode = "sort"
@@ -166,12 +170,14 @@ func (m *listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
166170
m.updateRows()
167171

168172
default:
169-
m.root.clearSession()
170-
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
171-
m.root.currSid = sid
172-
m.root.updateCurrName("chat")
173-
m.root.chat.refresh()
174-
cmd = m.root.runTick()
173+
if len(m.table.Rows()) > 0 {
174+
m.root.clearSession()
175+
sid := m.table.SelectedRow()[SESSION_LIST_ID_POS]
176+
m.root.currSid = sid
177+
m.root.updateCurrName("chat")
178+
m.root.chat.refresh()
179+
cmd = m.root.runTick()
180+
}
175181
}
176182
}
177183
}

lib/agent/models/gemini.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,15 @@ func Gemini(ctx context.Context, model string) (model.LLM, error) {
1414
use := os.Getenv("GOOGLE_GENAI_USE_VERTEXAI")
1515
if use != "" { // todo, be more truthy
1616
// creds file
17-
creds := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
18-
if creds != "" {
19-
return gemini.NewModel(ctx, model, &genai.ClientConfig{
20-
Backend: genai.BackendVertexAI,
21-
})
22-
}
17+
// creds := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
2318
proj := os.Getenv("GOOGLE_CLOUD_PROJECT")
2419
loc := os.Getenv("GOOGLE_CLOUD_LOCATION")
25-
if proj != "" && loc != "" {
26-
return gemini.NewModel(ctx, model, &genai.ClientConfig{
27-
Project: proj,
28-
Location: loc,
29-
Backend: genai.BackendVertexAI,
30-
})
31-
}
3220

3321
// default inference (same as Go SDK) (typically a service account)
3422
return gemini.NewModel(ctx, model, &genai.ClientConfig{
35-
Backend: genai.BackendVertexAI,
23+
Project: proj,
24+
Location: loc,
25+
Backend: genai.BackendVertexAI,
3626
})
3727
}
3828

0 commit comments

Comments
 (0)