Repository navigation
Expand file tree
/
Copy pathcontext_commit.go
More file actions
58 lines (53 loc) · 1.94 KB
/
Copy pathcontext_commit.go
File metadata and controls
58 lines (53 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package agentgo
import "context"
// Context preparation failures are not provider failures, even if a host wraps
// a retryable error. Retrying cannot repair a rejected durable state transition.
type contextPreparationError struct{ cause error }
func (e *contextPreparationError) Error() string { return "prepare context: " + e.cause.Error() }
func (e *contextPreparationError) Unwrap() error { return e.cause }
func stageContext(ctx context.Context, source *memoryArtifactManager) (context.Context, *memoryArtifactManager) {
staged := newArtifactManager()
if source != nil {
for _, artifact := range source.ListArtifacts() {
staged.artifacts[artifact.ID()] = artifact
}
}
// Summary model middleware must use the same staged collection as compaction.
runtime, ok := ctx.Value(modelExecutionRuntimeKey{}).(modelExecutionRuntime)
if ok {
runtime.artifacts = staged
ctx = context.WithValue(ctx, modelExecutionRuntimeKey{}, runtime)
}
return ctx, staged
}
func acceptContext(agentCtx *AgentContext, config LoopConfig, result ContextCommitResult, staged *memoryArtifactManager, execution Execution, sink eventSink) error {
if !result.Changed && !staged.changed {
return nil
}
if !result.Changed {
result.Messages = copyMessages(agentCtx.Messages)
}
result.Changed = true
result.Artifacts = staged.ListArtifacts()
if result.Compaction != nil {
info := *result.Compaction
info.Committed = false
result.Compaction = &info
}
if config.CommitContext != nil {
if err := config.CommitContext(result); err != nil {
return &contextPreparationError{cause: err}
}
}
agentCtx.Messages = copyMessages(result.Messages)
if config.artifacts != nil {
config.artifacts.replace(staged.artifacts)
}
config.ContextManager.Sync(agentCtx.Messages)
if result.Compaction != nil {
info := *result.Compaction
info.Committed = true
sink.emit(Event{Type: EventContextCompacted, Execution: executionRef(execution), Compaction: &info})
}
return nil
}