Skip to content

Commit a8a6ffc

Browse files
refactor(adk): address pr review comments
- Propagate ProcessState error instead of ignoring it - Remove unused parameter from applyDecisionForRetry - Use named return to eliminate repeated var zero M in generateLegacy - Use isNilMessage helper consistently in runctx and utils - Reorder StreamErr/concatenatedMessage checks for consistency - Rename TypedRootInput to AgenticRootInput for clarity Change-Id: Id179317d22d98ecd292f5caac80b303ecc4ce58f
1 parent 76689ba commit a8a6ffc

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

adk/chatmodel.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ func extractTextContent[M messageType](msg M) string {
713713
texts = append(texts, block.AssistantGenText.Text)
714714
}
715715
}
716-
return strings.Join(texts, "")
716+
return strings.Join(texts, "\n")
717717
default:
718718
return ""
719719
}
@@ -950,10 +950,12 @@ func (a *TypedChatModelAgent[M]) buildNoToolsRunFunc(_ context.Context) (typedRu
950950
if err != nil {
951951
return nil, err
952952
}
953-
_ = compose.ProcessState(ctx, func(_ context.Context, st *typedState[M]) error {
953+
if err := compose.ProcessState(ctx, func(_ context.Context, st *typedState[M]) error {
954954
st.Messages = append(st.Messages, messages...)
955955
return nil
956-
})
956+
}); err != nil {
957+
return nil, err
958+
}
957959
return messages, nil
958960
}))
959961

adk/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func (b *TypedBaseChatModelAgentMiddleware[M]) AfterToolCallsRewriteState(ctx co
281281

282282
func processTypedState(ctx context.Context, fn func(extra map[string]any) map[string]any) error {
283283
runCtx := getRunCtx(ctx)
284-
if runCtx != nil && runCtx.TypedRootInput != nil {
284+
if runCtx != nil && runCtx.AgenticRootInput != nil {
285285
return compose.ProcessState(ctx, func(_ context.Context, st *typedState[*schema.AgenticMessage]) error {
286286
st.Extra = fn(st.Extra)
287287
return nil

adk/retry_chatmodel.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (r *typedRetryModelWrapper[M]) Generate(ctx context.Context, input []M, opt
312312
return r.generateLegacy(ctx, input, opts...)
313313
}
314314

315-
func (r *typedRetryModelWrapper[M]) generateLegacy(ctx context.Context, input []M, opts ...model.Option) (M, error) {
315+
func (r *typedRetryModelWrapper[M]) generateLegacy(ctx context.Context, input []M, opts ...model.Option) (zero M, _ error) {
316316
isRetryAble := r.config.IsRetryAble
317317
if isRetryAble == nil {
318318
isRetryAble = defaultIsRetryAble
@@ -330,30 +330,25 @@ func (r *typedRetryModelWrapper[M]) generateLegacy(ctx context.Context, input []
330330
}
331331

332332
if _, ok := compose.ExtractInterruptInfo(err); ok {
333-
var zero M
334333
return zero, err
335334
}
336335

337336
if errors.Is(err, ErrStreamCanceled) {
338-
var zero M
339337
return zero, err
340338
}
341339

342340
if !isRetryAble(ctx, err) {
343-
var zero M
344341
return zero, err
345342
}
346343

347344
lastErr = err
348345
if attempt < r.config.MaxRetries {
349346
if err := r.contextAwareSleep(ctx, backoffFunc(ctx, attempt+1)); err != nil {
350-
var zero M
351347
return zero, err
352348
}
353349
}
354350
}
355351

356-
var zero M
357352
return zero, &RetryExhaustedError{LastErr: lastErr, TotalRetries: r.config.MaxRetries}
358353
}
359354

@@ -439,7 +434,7 @@ func generateWithShouldRetry(r *typedRetryModelWrapper[*schema.Message], ctx con
439434
break
440435
}
441436

442-
applyDecisionForRetry(r, &currentInput, &currentOpts, ctx, decision)
437+
applyDecisionForRetry(&currentInput, &currentOpts, ctx, decision)
443438

444439
delay := decision.Backoff
445440
if delay == 0 {
@@ -576,7 +571,7 @@ func streamWithShouldRetry(r *typedRetryModelWrapper[*schema.Message], ctx conte
576571

577572
lastErr = err
578573
if attempt < r.config.MaxRetries {
579-
applyDecisionForRetry(r, &currentInput, &currentOpts, ctx, decision)
574+
applyDecisionForRetry(&currentInput, &currentOpts, ctx, decision)
580575
delay := decision.Backoff
581576
if delay == 0 {
582577
delay = backoffFunc(ctx, attempt+1)
@@ -646,7 +641,7 @@ func streamWithShouldRetry(r *typedRetryModelWrapper[*schema.Message], ctx conte
646641
lastErr = verdictErr
647642

648643
if attempt < r.config.MaxRetries {
649-
applyDecisionForRetry(r, &currentInput, &currentOpts, ctx, decision)
644+
applyDecisionForRetry(&currentInput, &currentOpts, ctx, decision)
650645
delay := decision.Backoff
651646
if delay == 0 {
652647
delay = backoffFunc(ctx, attempt+1)
@@ -660,7 +655,7 @@ func streamWithShouldRetry(r *typedRetryModelWrapper[*schema.Message], ctx conte
660655
return nil, &RetryExhaustedError{LastErr: lastErr, TotalRetries: r.config.MaxRetries}
661656
}
662657

663-
func applyDecisionForRetry(r *typedRetryModelWrapper[*schema.Message], currentInput *[]*schema.Message, currentOpts *[]model.Option, ctx context.Context, decision *RetryDecision) {
658+
func applyDecisionForRetry(currentInput *[]*schema.Message, currentOpts *[]model.Option, ctx context.Context, decision *RetryDecision) {
664659
if decision.ModifiedInputMessages != nil {
665660
*currentInput = decision.ModifiedInputMessages
666661
if decision.PersistModifiedInputMessages {

adk/runctx.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ type typedAgentEventWrapperForGob[M messageType] struct {
8787
func (e *typedAgentEventWrapper[M]) GobEncode() ([]byte, error) {
8888
if e.event != nil && e.event.Output != nil && e.event.Output.MessageOutput != nil && e.event.Output.MessageOutput.IsStreaming {
8989
// Materialize the stream before encoding.
90-
var zero M
91-
if any(e.concatenatedMessage) == any(zero) && e.StreamErr == nil {
90+
if isNilMessage(e.concatenatedMessage) && e.StreamErr == nil {
9291
e.consumeStream()
9392
}
9493
}
@@ -127,8 +126,7 @@ func (e *typedAgentEventWrapper[M]) consumeStream() {
127126
e.mu.Lock()
128127
defer e.mu.Unlock()
129128

130-
var zero M
131-
if any(e.concatenatedMessage) != any(zero) {
129+
if !isNilMessage(e.concatenatedMessage) {
132130
return
133131
}
134132

@@ -396,7 +394,7 @@ type runContext struct {
396394
RootInput *AgentInput
397395
RunPath []RunStep
398396

399-
TypedRootInput any
397+
AgenticRootInput any
400398

401399
Session *runSession
402400
}
@@ -407,10 +405,10 @@ func (rc *runContext) isRoot() bool {
407405

408406
func (rc *runContext) deepCopy() *runContext {
409407
copied := &runContext{
410-
RootInput: rc.RootInput,
411-
TypedRootInput: rc.TypedRootInput,
412-
RunPath: make([]RunStep, len(rc.RunPath)),
413-
Session: rc.Session,
408+
RootInput: rc.RootInput,
409+
AgenticRootInput: rc.AgenticRootInput,
410+
RunPath: make([]RunStep, len(rc.RunPath)),
411+
Session: rc.Session,
414412
}
415413

416414
copy(copied.RunPath, rc.RunPath)
@@ -462,7 +460,7 @@ func initTypedRunCtx[M messageType](ctx context.Context, agentName string, input
462460
if _, ok := any(zero).(*schema.Message); ok {
463461
runCtx.RootInput = any(input).(*AgentInput)
464462
} else {
465-
runCtx.TypedRootInput = input
463+
runCtx.AgenticRootInput = input
466464
}
467465
}
468466

@@ -601,7 +599,7 @@ func ctxWithNewTypedRunCtx[M messageType](ctx context.Context, input *TypedAgent
601599
if _, ok := any(zero).(*schema.Message); ok {
602600
rc.RootInput = any(input).(*AgentInput)
603601
} else {
604-
rc.TypedRootInput = input
602+
rc.AgenticRootInput = input
605603
}
606604
return setRunCtx(ctx, rc)
607605
}

adk/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ func getMessageFromTypedWrappedEvent[M messageType](e *typedAgentEventWrapper[M]
129129
return e.event.Output.MessageOutput.Message, nil
130130
}
131131

132-
if any(e.concatenatedMessage) != any(zero) {
133-
return e.concatenatedMessage, nil
134-
}
135-
136132
if e.StreamErr != nil {
137133
return zero, e.StreamErr
138134
}
139135

136+
if !isNilMessage(e.concatenatedMessage) {
137+
return e.concatenatedMessage, nil
138+
}
139+
140140
e.consumeStream()
141141

142142
if e.StreamErr != nil {

0 commit comments

Comments
 (0)