Files
poimen-workflows/internal/memory/example_activity.go
T

135 lines
3.8 KiB
Go
Raw Normal View History

package memory
import (
"context"
"fmt"
)
// ExampleActivity demonstrates memory service integration in workflows
// This can be used as a template for workflow activities
// LearnTaskActivity learns from task execution
func LearnTaskActivity(ctx context.Context, service *Service, task string, result string) error {
// Create knowledge from task result
knowledgeID, err := service.CreateKnowledge(ctx, &KnowledgeRecord{
Level: "L1",
Title: fmt.Sprintf("Task: %s", task),
Content: result,
Source: fmt.Sprintf("workflow://task/%s", task),
Metadata: map[string]interface{}{
"task": task,
"type": "execution_result",
},
})
if err != nil {
return fmt.Errorf("learn task: %w", err)
}
fmt.Printf("Learned: %s\n", knowledgeID)
return nil
}
// DiagnosticActivity retrieves context for problem diagnosis
func DiagnosticActivity(ctx context.Context, service *Service, tool string, issue string) ([]string, error) {
// Retrieve context for tool/issue
svcCtx, err := service.RetrieveContext(ctx, tool, issue, 8192)
if err != nil {
return nil, fmt.Errorf("diagnose: %w", err)
}
// Extract lessons
diagnostics := make([]string, 0)
for _, lesson := range svcCtx.Lessons {
if lesson.Tier == 1 {
diagnostics = append(diagnostics, lesson.Text)
}
}
// Extract skills
for _, skill := range svcCtx.Skills {
diagnostics = append(diagnostics, fmt.Sprintf("Skill: %s (%s)", skill.Name, skill.Why))
}
return diagnostics, nil
}
// DocumentationActivity searches vault for relevant docs
func DocumentationActivity(ctx context.Context, service *Service, topic string) ([]string, error) {
// Retrieve vault files
files, err := service.GetVault(ctx)
if err != nil {
return nil, fmt.Errorf("get vault: %w", err)
}
// Filter by topic
results := make([]string, 0)
for _, file := range files {
if file.Level == "R" { // Reference docs
results = append(results, fmt.Sprintf("%s: %s", file.Title, file.Path))
}
}
return results, nil
}
// SearchKnowledgeActivity searches knowledge base
func SearchKnowledgeActivity(ctx context.Context, service *Service, query string) ([]string, error) {
records, err := service.RetrieveKnowledge(ctx, query, &RetrievalOptions{
LevelFilter: []string{"L1", "L2"},
Limit: 5,
Floor: 0.7,
})
if err != nil {
return nil, fmt.Errorf("search: %w", err)
}
results := make([]string, 0)
for _, record := range records {
results = append(results, fmt.Sprintf("[%s] %s", record.Level, record.Content))
}
return results, nil
}
// UpdateLessonActivity updates learned facts
func UpdateLessonActivity(ctx context.Context, service *Service, id string, newContent string) error {
_, err := service.UpdateKnowledge(ctx, &KnowledgeRecord{
ID: id,
Level: "L2",
Content: newContent,
})
return err
}
// HealthCheckActivity checks memory service health
func HealthCheckActivity(ctx context.Context, service *Service) (bool, error) {
return service.IsHealthy(ctx), nil
}
// Example workflow structure using memory service
type WorkflowWithMemory struct {
MemoryService *Service
}
// ExecuteWithLearning executes task and learns from it
func (w *WorkflowWithMemory) ExecuteWithLearning(ctx context.Context, task string, executor func() (string, error)) error {
// Execute task
result, err := executor()
if err != nil {
return err
}
// Learn from result
return LearnTaskActivity(ctx, w.MemoryService, task, result)
}
// DiagnoseWithContext diagnoses issue using memory context
func (w *WorkflowWithMemory) DiagnoseWithContext(ctx context.Context, tool string, issue string) ([]string, error) {
return DiagnosticActivity(ctx, w.MemoryService, tool, issue)
}
// SearchKnowledge searches knowledge
func (w *WorkflowWithMemory) SearchKnowledge(ctx context.Context, query string) ([]string, error) {
return SearchKnowledgeActivity(ctx, w.MemoryService, query)
}