-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.py
More file actions
313 lines (278 loc) · 10.4 KB
/
Copy pathplugin.py
File metadata and controls
313 lines (278 loc) · 10.4 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from __future__ import annotations
import json
from pathlib import Path
from collections.abc import Mapping
from typing import Any
from agent.lifecycle.composition import CONTEXT_PREPARED_EVENT
from agent.plugin_composition import (
RUNTIME_STARTED,
RUNTIME_STOPPING,
TIMERS,
TOOL_CATALOG,
Context,
MobileUiDefinition,
MobileUiNavigation,
MobileUiRpcInvalidRequest,
PluginToolDefinition,
ServiceKey,
UI_SLOTS,
)
from agent.turn_events.after_turn import AFTER_TURN_COMMITTED
from bus.events_lifecycle import TurnCommitted
from .db import apply_feedback, open_db
from .dashboard import EmotionDashboardReader
from .feedback_history import (
PROACTIVE_FEEDBACK_HISTORY,
FeedbackHistoryConsumer,
)
from .runtime import DriftProposalServices, DriftWakeServices, EmotionRuntime
api_version = 3
name = "emotion"
version = "3.0.4"
desc = "Timer-refreshed Emotion context and ordinary Drift preference projection."
DRIFT_PROPOSALS = ServiceKey[DriftProposalServices]("drift.proposals.v1")
DRIFT_WAKE = ServiceKey[DriftWakeServices]("drift.wake.v1")
inject = (TIMERS, TOOL_CATALOG, UI_SLOTS, DRIFT_PROPOSALS, DRIFT_WAKE)
workspace_roots = ("emotion",)
drift_skill_roots = ("drift/skills",)
dashboard_module = "dashboard.py"
web_module = "web_module.js"
web_requires = ("workbench.panels.v2",)
web_provides = ()
web_contract_digests = {
"workbench.panels.v2": "fb6417c9bf532c1fdb344767d06065d5d3293da85deb64eff1e8088889a33bcb",
}
_v3_emotion_root: Path | None = None
_v3_emotion_runtime: EmotionRuntime | None = None
_FEEDBACK_PREVIEW_MAX_CHARS = 2400
async def apply(ctx: Context, config: object) -> None:
"""Compose Emotion from Timer, lifecycle, Drift, Tool, and UI atoms."""
# 1. 冻结 exact Root;apply/candidate 不打开数据库、不登记真实 Timer。
del config
global _v3_emotion_root, _v3_emotion_runtime
_v3_emotion_root = ctx.workspace_root("emotion")
emotion_root = _v3_emotion_root
runtime = EmotionRuntime(
ctx,
emotion_root,
ctx.require(TIMERS),
ctx.require(DRIFT_PROPOSALS),
ctx.require(DRIFT_WAKE),
)
_v3_emotion_runtime = runtime
# 2. Drift 结果只经普通 Tool 进入 Emotion 自有事务。
await ctx.require(TOOL_CATALOG).register(
ctx,
PluginToolDefinition(
name="emotion_commit_preference_context",
description=(
"提交当前 Emotion Drift proposal 的稳定主动偏好;"
"完整结果进入 Emotion history,current context 原位替换。"
),
parameters=_commit_tool_schema(),
handler_export="emotion_commit_preference_context",
risk="read-write",
search_hint="emotion drift preference feedback",
),
handler=emotion_commit_preference_context,
)
# 3. Timer/lifecycle/Turn observer 都属于同一个 generation Fiber。
def on_turn_committed(event: TurnCommitted) -> None:
_on_turn_committed(event, root=emotion_root, runtime=runtime)
await ctx.on(AFTER_TURN_COMMITTED, on_turn_committed)
await ctx.on(CONTEXT_PREPARED_EVENT, runtime.prepare_context)
await ctx.on(RUNTIME_STARTED, lambda _: runtime.start())
await ctx.on(RUNTIME_STOPPING, lambda _: runtime.close())
# 4. PF history is an optional pull composition with its own Timer/Fiber.
async def apply_feedback_history(child: Context) -> None:
consumer = FeedbackHistoryConsumer(
child,
emotion_root,
child.require(TIMERS),
child.require(PROACTIVE_FEEDBACK_HISTORY),
)
await child.on(RUNTIME_STARTED, lambda _: consumer.start())
await child.on(RUNTIME_STOPPING, lambda _: consumer.close())
_ = await ctx.inject(
(TIMERS, PROACTIVE_FEEDBACK_HISTORY),
apply_feedback_history,
name="proactive-feedback-history",
)
# 5. Mobile 只读查询与静态资源绑定到同一个 generation Root。
def mobile_query(
method: str,
payload: dict[str, object],
*,
session_id: str | None,
turn_id: str | None,
) -> dict[str, object]:
return _mobile_ui_query(
method,
payload,
session_id=session_id,
turn_id=turn_id,
root=emotion_root,
)
await ctx.require(UI_SLOTS).register_mobile(
ctx,
MobileUiDefinition(
module="mobile_panel.js",
stylesheet="mobile_panel.css",
navigation=MobileUiNavigation(
label="主动状态",
description="反馈如何改变 Agent 的语气和主动发送把握",
),
),
query=mobile_query,
)
def _on_turn_committed(
event: TurnCommitted,
*,
root: Path | None = None,
runtime: EmotionRuntime | None = None,
) -> None:
"""Project one typed committed Turn into Emotion's idempotent SQLite state."""
if runtime is not None:
runtime.observe_turn(event)
feedback = _explicit_quote_feedback_from_turn(event)
if feedback is None:
return
root = _require_v3_emotion_root() if root is None else root
conn = open_db(root / "emotion.db")
try:
apply_feedback(
conn,
source_event_id=feedback["source_event_id"],
session_key=event.session_key,
feedback_type=feedback["feedback_type"],
confidence=feedback["confidence"],
payload=feedback["payload"],
)
finally:
conn.close()
def _explicit_quote_feedback_from_turn(
event: TurnCommitted,
) -> dict[str, Any] | None:
"""Project Emotion's direct explicit-quote signal in its own namespace."""
marker = "【你当前新消息】"
source = event.turn_id or event.persisted_user_message_id
if marker not in event.input_message or not isinstance(source, str) or not source:
return None
payload = {
"feedback_event_id": source,
"user_message_id": event.persisted_user_message_id,
"assistant_message_id": event.assistant_message_id,
"proactive_message_id": None,
"feedback_type": "explicit_quote",
"confidence": "gold",
"pua_score": 1.0,
"lag_seconds": None,
"matched_by": "explicit_quote",
"candidate_count": None,
"pa_score": None,
"reason": "explicit_quote",
"user_content_preview": _feedback_preview(
event.persisted_user_message or event.input_message
),
"assistant_content_preview": _feedback_preview(event.assistant_response),
"proactive_content_preview": _feedback_preview(
_quoted_proactive_text(event.input_message)
),
}
return {
"source_event_id": f"emotion_explicit_quote:{source}",
"feedback_type": "explicit_quote",
"confidence": "gold",
"payload": payload,
}
def _feedback_preview(value: object) -> str | None:
if not isinstance(value, str):
return None
clean = " ".join(value.split())
if len(clean) <= _FEEDBACK_PREVIEW_MAX_CHARS:
return clean
return clean[:_FEEDBACK_PREVIEW_MAX_CHARS].rstrip() + "..."
def _quoted_proactive_text(value: str) -> str | None:
marker = "【你当前新消息】"
quoted = value.split(marker, 1)[0].strip()
prefix = "被回复消息:"
if quoted.startswith(prefix):
quoted = quoted[len(prefix) :].strip()
return quoted or None
def _mobile_ui_query(
method: str,
payload: dict[str, object],
*,
session_id: str | None,
turn_id: str | None,
root: Path | None = None,
) -> dict[str, object]:
"""Return a read-only bounded Emotion mobile projection."""
_ = session_id, turn_id
if method != "emotion.bootstrap":
raise MobileUiRpcInvalidRequest(f"未知 emotion 移动方法: {method}")
limit = payload.get("limit", 30)
if not isinstance(limit, int) or isinstance(limit, bool) or not 1 <= limit <= 50:
raise MobileUiRpcInvalidRequest("limit 必须是 1 到 50 的整数")
emotion_root = _require_v3_emotion_root() if root is None else root
return EmotionDashboardReader(emotion_root).get_mobile_bootstrap(
limit=limit
)
async def emotion_commit_preference_context(
context: object,
arguments: Mapping[str, object],
) -> str:
"""Delegate legacy export lookup to the exact Root runtime."""
runtime = _v3_emotion_runtime
if runtime is None:
raise RuntimeError("emotion v3 generation 尚未完成 apply")
result = await runtime.commit_preference_context(context, arguments)
return json.dumps(result, ensure_ascii=False, sort_keys=True)
def _commit_tool_schema() -> dict[str, object]:
return {
"type": "object",
"properties": {
"proposal_id": {"type": "string"},
"revision": {"type": "string"},
"context": {
"type": "string",
"description": "完整且简短的当前主动偏好上下文。",
},
"candidates": {
"type": "array",
"items": {
"type": "object",
"properties": {
"effect": {
"type": "string",
"enum": ["block", "boost", "timing", "tone", "verify"],
},
"confidence": {
"type": "string",
"enum": ["high", "low", "medium"],
},
"topic": {"type": "string"},
"action": {"type": "string"},
"evidence": {
"type": "array",
"items": {"type": "integer", "minimum": 1},
},
},
"required": [
"effect",
"confidence",
"topic",
"action",
"evidence",
],
"additionalProperties": False,
},
},
},
"required": ["proposal_id", "revision", "context", "candidates"],
"additionalProperties": False,
}
def _require_v3_emotion_root() -> Path:
if _v3_emotion_root is None:
raise RuntimeError("emotion v3 generation 尚未绑定 workspace root")
return _v3_emotion_root