feat: manage task process lifetimes and preserve turn history

This commit is contained in:
Ed1s0nZ
2026-09-16 17:52:58 +08:00
parent fd1c13a43d
commit f7882be546
54 changed files with 3650 additions and 330 deletions
+10 -5
View File
@@ -49,6 +49,7 @@ func NewEinoTurnLoopRuntime(cfg EinoTurnLoopRuntimeConfig) *EinoTurnLoopRuntime
}
enableStreaming := cfg.EnableStreaming
prepareAgent := cfg.PrepareAgent
history := &einoTurnHistory{}
if prepareAgent == nil {
prepareAgent = func(context.Context, *adk.TurnLoop[EinoTurnLoopItem, *schema.Message], []EinoTurnLoopItem) (adk.Agent, error) {
return cfg.Agent, nil
@@ -58,9 +59,11 @@ func NewEinoTurnLoopRuntime(cfg EinoTurnLoopRuntimeConfig) *EinoTurnLoopRuntime
Store: cfg.Store,
CheckpointID: cfg.CheckpointID,
GenInput: func(ctx context.Context, _ *adk.TurnLoop[EinoTurnLoopItem, *schema.Message], items []EinoTurnLoopItem) (*adk.GenInputResult[EinoTurnLoopItem, *schema.Message], error) {
msgs := mergeEinoTurnLoopMessages(items)
msgs := append(history.nextInput(), mergeEinoTurnLoopMessages(items)...)
history = &einoTurnHistory{}
history.begin(msgs)
return &adk.GenInputResult[EinoTurnLoopItem, *schema.Message]{
RunCtx: ctx,
RunCtx: context.WithValue(ctx, einoTurnHistoryKey{}, history),
Input: &adk.AgentInput{
Messages: msgs,
EnableStreaming: enableStreaming,
@@ -74,13 +77,15 @@ func NewEinoTurnLoopRuntime(cfg EinoTurnLoopRuntimeConfig) *EinoTurnLoopRuntime
consumed = append(consumed, newItems...)
remaining := append([]EinoTurnLoopItem(nil), unhandledItems...)
return &adk.GenResumeResult[EinoTurnLoopItem, *schema.Message]{
RunCtx: ctx,
RunCtx: context.WithValue(ctx, einoTurnHistoryKey{}, history),
Consumed: consumed,
Remaining: remaining,
}, nil
},
PrepareAgent: prepareAgent,
OnAgentEvents: cfg.OnAgentEvents,
PrepareAgent: prepareAgent,
OnAgentEvents: func(ctx context.Context, tc *adk.TurnContext[EinoTurnLoopItem, *schema.Message], events *adk.AsyncIterator[*adk.AgentEvent]) error {
return history.wrapEvents(cfg.OnAgentEvents)(ctx, tc, events)
},
})
if len(cfg.InitialMessages) > 0 {
loop.Push(EinoTurnLoopItem{Kind: "initial", Messages: cloneSchemaMessages(cfg.InitialMessages)})