-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
140 lines (119 loc) · 4.34 KB
/
Copy pathplugin.py
File metadata and controls
140 lines (119 loc) · 4.34 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
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from pydantic import BaseModel, ConfigDict, Field
from agent.plugin_composition import (
MANAGED_PROCESSES,
MCP_SERVERS,
RUNTIME_STARTED,
RUNTIME_STOPPING,
TIMERS,
UI_SLOTS,
Context,
EndpointEnv,
ManagedProcessDefinition,
McpServerDefinition,
MobileUiDefinition,
MobileUiNavigation,
)
from .src.content_adapter import (
FitbitWakeRuntime,
FitbitMonitorClient,
)
from .src.eventmail import EVENTMAIL_ALERT_SOURCE, EVENTMAIL_CONTEXT_SOURCE
from .src.mobile_reader import mobile_ui_query
from .src.sleep_context import FitbitAdapterStore
class FitbitContentConfig(BaseModel):
model_config = ConfigDict(extra="forbid")
poll_interval_seconds: int = Field(default=300, ge=1)
sleep_ttl_seconds: int = Field(default=600, ge=1)
class FitbitConfig(BaseModel):
model_config = ConfigDict(extra="forbid")
content: FitbitContentConfig = Field(default_factory=FitbitContentConfig)
api_version = 3
name = "fitbit"
version = "3.2.2"
desc = "Fitbit health Alert and sleep Context source"
Config = FitbitConfig
inject = (
MANAGED_PROCESSES,
MCP_SERVERS,
TIMERS,
UI_SLOTS,
)
dashboard_module = "dashboard.py"
web_module = "web_module.js"
web_requires = ("workbench.panels.v2",)
web_provides = ()
web_contract_digests = {
"workbench.panels.v2": "fb6417c9bf532c1fdb344767d06065d5d3293da85deb64eff1e8088889a33bcb",
}
async def apply(ctx: Context, config: FitbitConfig) -> None:
"""装配 monitor、工具、Wake 来源和移动界面。"""
# 1. 登记现有 monitor 与用户显式调用的普通 MCP 工具
await ctx.require(MANAGED_PROCESSES).register(
ctx,
ManagedProcessDefinition(
name="monitor",
command=("python", "monitor/server.py"),
cwd=".",
port_env="FITBIT_MONITOR_PORT",
formal_port=18765,
readiness_path="/api/data",
startup_timeout_seconds=15.0,
),
)
await ctx.require(MCP_SERVERS).register(
ctx,
McpServerDefinition(
name="fitbit",
command=("python", "run_mcp.py"),
required_tools=("fitbit_health_snapshot", "fitbit_sleep_report"),
candidate_read_only_tools=(
"fitbit_health_snapshot",
"fitbit_sleep_report",
),
endpoint_env=(EndpointEnv("FITBIT_MONITOR_PORT", "monitor"),),
candidate_env={"FITBIT_BACKEND": "recording"},
),
)
# 2. EventMail 存在时,独立子 Fiber 才启动健康来源。
async def apply_eventmail(source_ctx: Context) -> None:
store = FitbitAdapterStore(source_ctx.data_root / "adapter.sqlite3")
store.initialize(datetime.now(UTC))
runtime = FitbitWakeRuntime(
store,
source_ctx.require(TIMERS),
source_ctx.require(EVENTMAIL_ALERT_SOURCE).bind("fitbit-health-alerts"),
source_ctx.require(EVENTMAIL_CONTEXT_SOURCE).bind("fitbit-sleep"),
FitbitMonitorClient(),
poll_interval=timedelta(seconds=config.content.poll_interval_seconds),
sleep_ttl=timedelta(seconds=config.content.sleep_ttl_seconds),
)
def setup() -> object:
return runtime.close
_ = await source_ctx.effect(setup, label="fitbit-eventmail-runtime")
poll_health = await source_ctx.health("fitbit-eventmail-poll")
async def start(_event: object) -> None:
await runtime.start(source_ctx, poll_health)
async def stop(_event: object) -> None:
await runtime.close()
_ = await source_ctx.on(RUNTIME_STARTED, start)
_ = await source_ctx.on(RUNTIME_STOPPING, stop)
_ = await ctx.inject(
(TIMERS, EVENTMAIL_ALERT_SOURCE, EVENTMAIL_CONTEXT_SOURCE),
apply_eventmail,
name="fitbit-eventmail-source",
)
# 3. 在同一个 exact Root 上保留现有移动投影
await ctx.require(UI_SLOTS).register_mobile(
ctx,
MobileUiDefinition(
module="mobile_panel.js",
stylesheet="mobile_panel.css",
navigation=MobileUiNavigation(
label="健康状态",
description="查看当前心率、血氧、步数和最近睡眠节律",
),
),
query=mobile_ui_query,
)