-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassess.go
More file actions
255 lines (220 loc) · 7.57 KB
/
assess.go
File metadata and controls
255 lines (220 loc) · 7.57 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package cogito
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/zoobzio/capitan"
"github.com/zoobzio/pipz"
"github.com/zoobzio/zyn"
)
// Assess is a sentiment assessment primitive that implements pipz.Chainable[*Thought].
// It assesses the emotional tone of input and stores the full response for typed retrieval.
type Assess struct {
identity pipz.Identity
key string
summaryKey string
useIntrospection bool
reasoningTemperature float32
introspectionTemperature float32
provider Provider
temperature float32
}
// NewAssess creates a new sentiment assessment primitive with introspection enabled by default.
//
// The primitive uses two zyn synapses:
// 1. Sentiment synapse: Assesses emotional tone (positive/negative/neutral/mixed)
// 2. Transform synapse: Synthesizes a semantic summary for context accumulation
//
// Output Notes:
// - {key}: JSON-serialized zyn.SentimentResponse
// - {key}_summary: Semantic summary for next steps (if introspection enabled)
//
// Example:
//
// step := cogito.NewAssess("user_mood")
// result, _ := step.Process(ctx, thought)
// resp, _ := step.Scan(result)
// fmt.Println(resp.Overall, resp.Confidence, resp.Scores)
func NewAssess(key string) *Assess {
return &Assess{
identity: pipz.NewIdentity(key, "Sentiment assessment primitive"),
key: key,
useIntrospection: DefaultIntrospection,
temperature: DefaultReasoningTemperature,
}
}
// Process implements pipz.Chainable[*Thought].
func (s *Assess) Process(ctx context.Context, t *Thought) (*Thought, error) {
start := time.Now()
// Resolve provider
provider, err := ResolveProvider(ctx, s.provider)
if err != nil {
return t, fmt.Errorf("assess: %w", err)
}
// Create zyn sentiment synapse
sentimentSynapse, err := zyn.NewSentiment("overall emotional tone", provider)
if err != nil {
return t, fmt.Errorf("assess: failed to create sentiment synapse: %w", err)
}
// Get unpublished notes
unpublished := t.GetUnpublishedNotes()
noteContext := RenderNotesToContext(unpublished)
// Emit step started
capitan.Emit(ctx, StepStarted,
FieldTraceID.Field(t.TraceID),
FieldStepName.Field(s.key),
FieldStepType.Field("assess"),
FieldUnpublishedCount.Field(len(unpublished)),
FieldTemperature.Field(s.temperature),
)
// Determine reasoning temperature
reasoningTemp := s.temperature
if s.reasoningTemperature != 0 {
reasoningTemp = s.reasoningTemperature
}
// PHASE 1: REASONING - Sentiment analysis
sentResponse, err := sentimentSynapse.FireWithInput(ctx, t.Session, zyn.SentimentInput{
Text: noteContext,
Temperature: reasoningTemp,
})
if err != nil {
s.emitFailed(ctx, t, start, err)
return t, fmt.Errorf("assess: sentiment synapse execution failed: %w", err)
}
// Store full response as JSON
respJSON, err := json.Marshal(sentResponse)
if err != nil {
s.emitFailed(ctx, t, start, err)
return t, fmt.Errorf("assess: failed to marshal response: %w", err)
}
if err := t.SetContent(ctx, s.key, string(respJSON), "assess"); err != nil {
s.emitFailed(ctx, t, start, err)
return t, fmt.Errorf("assess: failed to persist note: %w", err)
}
// PHASE 2: INTROSPECTION - Semantic summary (optional)
if s.useIntrospection {
if err := s.runIntrospection(ctx, t, sentResponse, unpublished, provider); err != nil {
s.emitFailed(ctx, t, start, err)
return t, err
}
}
// Mark notes as published
t.MarkNotesPublished(ctx)
// Emit step completed
duration := time.Since(start)
capitan.Emit(ctx, StepCompleted,
FieldTraceID.Field(t.TraceID),
FieldStepName.Field(s.key),
FieldStepType.Field("assess"),
FieldStepDuration.Field(duration),
FieldNoteCount.Field(len(t.AllNotes())),
)
return t, nil
}
// runIntrospection executes the transform synapse for semantic summary.
func (s *Assess) runIntrospection(ctx context.Context, t *Thought, resp zyn.SentimentResponse, originalNotes []Note, provider Provider) error {
return runIntrospection(ctx, t, provider, s.buildIntrospectionInput(resp, originalNotes), introspectionConfig{
stepType: "assess",
key: s.key,
summaryKey: s.summaryKey,
introspectionTemperature: s.introspectionTemperature,
synapsePrompt: "Synthesize sentiment analysis into context for next reasoning step",
})
}
// buildIntrospectionInput formats sentiment for the transform synapse.
func (s *Assess) buildIntrospectionInput(resp zyn.SentimentResponse, originalNotes []Note) zyn.TransformInput {
sentText := fmt.Sprintf(
"Sentiment: %s (confidence: %.2f)\n",
resp.Overall,
resp.Confidence,
)
sentText += fmt.Sprintf("Scores: positive=%.2f, negative=%.2f, neutral=%.2f\n",
resp.Scores.Positive,
resp.Scores.Negative,
resp.Scores.Neutral,
)
if len(resp.Emotions) > 0 {
sentText += fmt.Sprintf("Emotions: %v\n", resp.Emotions)
}
if len(resp.Aspects) > 0 {
sentText += "Aspect sentiments:\n"
for aspect, sentiment := range resp.Aspects {
sentText += fmt.Sprintf(" %s: %s\n", aspect, sentiment)
}
}
sentText += "Reasoning:\n"
for i, reason := range resp.Reasoning {
sentText += fmt.Sprintf(" %d. %s\n", i+1, reason)
}
return zyn.TransformInput{
Text: sentText,
Context: RenderNotesToContext(originalNotes),
Style: "Synthesize this sentiment analysis into rich semantic context for the next reasoning step. Focus on emotional tone implications, what it suggests about user state or satisfaction, and actionable insights. Be concise but comprehensive.",
}
}
// emitFailed emits a step failed event.
func (s *Assess) emitFailed(ctx context.Context, t *Thought, start time.Time, err error) {
capitan.Error(ctx, StepFailed,
FieldTraceID.Field(t.TraceID),
FieldStepName.Field(s.key),
FieldStepType.Field("assess"),
FieldStepDuration.Field(time.Since(start)),
FieldError.Field(err),
)
}
// Identity implements pipz.Chainable[*Thought].
func (s *Assess) Identity() pipz.Identity {
return s.identity
}
// Schema implements pipz.Chainable[*Thought].
func (s *Assess) Schema() pipz.Node {
return pipz.Node{Identity: s.identity, Type: "assess"}
}
// Close implements pipz.Chainable[*Thought].
func (s *Assess) Close() error {
return nil
}
// Scan retrieves the typed sentiment response from a thought.
func (s *Assess) Scan(t *Thought) (*zyn.SentimentResponse, error) {
content, err := t.GetContent(s.key)
if err != nil {
return nil, fmt.Errorf("assess scan: %w", err)
}
var resp zyn.SentimentResponse
if err := json.Unmarshal([]byte(content), &resp); err != nil {
return nil, fmt.Errorf("assess scan: failed to unmarshal response: %w", err)
}
return &resp, nil
}
// Builder methods
// WithProvider sets the provider for this step.
func (s *Assess) WithProvider(p Provider) *Assess {
s.provider = p
return s
}
// WithTemperature sets the default temperature for this step.
func (s *Assess) WithTemperature(temp float32) *Assess {
s.temperature = temp
return s
}
// WithIntrospection enables the introspection phase.
func (s *Assess) WithIntrospection() *Assess {
s.useIntrospection = true
return s
}
// WithSummaryKey sets a custom key for the introspection summary note.
func (s *Assess) WithSummaryKey(key string) *Assess {
s.summaryKey = key
return s
}
// WithReasoningTemperature sets the temperature for the reasoning phase.
func (s *Assess) WithReasoningTemperature(temp float32) *Assess {
s.reasoningTemperature = temp
return s
}
// WithIntrospectionTemperature sets the temperature for the introspection phase.
func (s *Assess) WithIntrospectionTemperature(temp float32) *Assess {
s.introspectionTemperature = temp
return s
}