@syengup/fridaynext-hermes-channel 0.1.0-beta.3 → 0.1.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/install-config.js +67 -0
- package/install-runtime.js +17 -5
- package/install.js +6 -4
- package/package.json +2 -1
- package/plugin/README.md +2 -2
- package/plugin/fridaynext_plugin/__init__.py +10 -0
- package/plugin/fridaynext_plugin/adapter.py +221 -12
- package/plugin/fridaynext_plugin/agent_management.py +158 -0
- package/plugin/fridaynext_plugin/canonical_control.py +12 -4
- package/plugin/fridaynext_plugin/compat_v0225.py +1174 -4
- package/plugin/fridaynext_plugin/continuations.py +271 -0
- package/plugin/fridaynext_plugin/device-tools-v1.json +302 -0
- package/plugin/fridaynext_plugin/device_tools.py +144 -0
- package/plugin/fridaynext_plugin/history.py +25 -3
- package/plugin/fridaynext_plugin/host.py +7 -4
- package/plugin/fridaynext_plugin/media_input.py +56 -9
- package/plugin/fridaynext_plugin/multi_agent.py +221 -0
- package/plugin/fridaynext_plugin/reply_context.py +39 -0
- package/plugin/fridaynext_plugin/runtime_port.py +23 -1
- package/plugin/fridaynext_plugin/scheduled_tasks.py +229 -0
- package/plugin/fridaynext_plugin/settings.py +7 -3
- package/plugin/fridaynext_plugin/slash_commands.py +163 -0
- package/plugin/fridaynext_plugin/stream_bridge.py +83 -26
- package/plugin/fridaynext_plugin/stream_projection.py +30 -6
- package/plugin/fridaynext_plugin/subagents.py +228 -0
- package/plugin/plugin.yaml +11 -0
- package/plugin/runtime/dist/commands-routes.js +94 -0
- package/plugin/runtime/dist/device-tools-v1.json +302 -0
- package/plugin/runtime/dist/hosted-backend.js +250 -25
- package/plugin/runtime/dist/hosted-continuations.js +41 -0
- package/plugin/runtime/dist/hosted-device-tools.js +82 -0
- package/plugin/runtime/dist/hosted-media.js +54 -13
- package/plugin/runtime/dist/hosted-stream.js +124 -14
- package/plugin/runtime/dist/hosted-subagents.js +78 -0
- package/plugin/runtime/dist/platform-service.js +4 -0
- package/plugin/runtime/dist/plugin-control.js +10 -6
- package/plugin/runtime/dist/scheduled-routes.js +63 -0
- package/plugin/runtime/dist/service-host.js +27 -3
- package/plugin/runtime/dist/tool-presentation.js +399 -0
- package/plugin/runtime/node_modules/.package-lock.json +6 -6
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/API.md +125 -5
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/README.md +21 -1
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/history.d.ts +1 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/history.js +3 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/operations.d.ts +136 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/operations.js +105 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/settings-schema.d.ts +3 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/settings-schema.js +45 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/tool-presentation.d.ts +10 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/tool-presentation.js +2 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/types.d.ts +27 -2
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/types.js +1 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/work-context.d.ts +56 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/contract/work-context.js +60 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/device-request/pending-store.js +3 -3
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/index.d.ts +5 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/index.js +5 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/canonical-update-batcher.d.ts +26 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/canonical-update-batcher.js +112 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/channel-kernel.d.ts +4 -1
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/channel-kernel.js +30 -5
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/durable-run-store.d.ts +18 -1
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/durable-run-store.js +187 -51
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/runner.d.ts +1 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/runner.js +52 -2
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/work-context.d.ts +13 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/runtime/work-context.js +73 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/service/channel-service.js +1 -1
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/command-lifecycle.d.ts +47 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/command-lifecycle.js +184 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/controls.d.ts +1 -1
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/controls.js +11 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/messages.js +12 -6
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/operations.d.ts +13 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/operations.js +336 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/runtime.d.ts +1 -0
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/runtime.js +52 -10
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/session-settings.js +38 -6
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/dist/transport/sessions.js +8 -8
- package/plugin/runtime/node_modules/@syengup/fridaynext-channel-kit/package.json +6 -7
- package/plugin/runtime/node_modules/tslib/README.md +3 -8
- package/plugin/runtime/node_modules/tslib/package.json +1 -1
- package/plugin/runtime/node_modules/tslib/tslib.d.ts +4 -2
- package/plugin/runtime/node_modules/tslib/tslib.es6.js +10 -1
- package/plugin/runtime/node_modules/tslib/tslib.es6.mjs +10 -1
- package/plugin/runtime/node_modules/tslib/tslib.js +10 -1
- package/plugin/runtime/package-lock.json +7 -7
- package/plugin/runtime/package.json +1 -1
- package/plugin/tools.py +3 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"""公开文本观察→既有后台准入的独立展示;不创建或重跑Agent执行。"""
|
|
2
|
+
import asyncio
|
|
3
|
+
import copy
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
from .state import private_json
|
|
7
|
+
from .stream_projection import StreamProjection, opaque_identity
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ContinuationObserver:
|
|
13
|
+
def __init__(self, adapter):
|
|
14
|
+
self.adapter = adapter
|
|
15
|
+
self.folder = adapter.data / "continuation-observations"
|
|
16
|
+
self.origins_folder = self.folder / "origins"
|
|
17
|
+
self.origins_folder.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
18
|
+
self.records, self.origins = {}, {}
|
|
19
|
+
for path in self.origins_folder.glob("*.json"):
|
|
20
|
+
origin = json.loads(path.read_text())
|
|
21
|
+
if origin.get("scope", {}).get("profile") == str(adapter.profile):
|
|
22
|
+
self.origins[origin["producer_id"]] = origin
|
|
23
|
+
for path in self.folder.glob("*.json"):
|
|
24
|
+
record = json.loads(path.read_text())
|
|
25
|
+
if record.get("version") != 1 or record.get("profile") != str(adapter.profile): continue
|
|
26
|
+
pending = [json.loads(p.read_text()) for p in sorted(self._file(record, ".events").glob("*.json"))]
|
|
27
|
+
record["sequence"] = max([record.get("sequence", 0), *[e["sequence"] for e in pending]])
|
|
28
|
+
for entry in pending:
|
|
29
|
+
if entry["kind"] == "stop": record["phase"] = entry["payload"]["phase"]
|
|
30
|
+
if entry.get("native_cursor"): record["native_cursor"] = entry["native_cursor"]
|
|
31
|
+
self.records[record["run_id"]] = record
|
|
32
|
+
self.tasks, self.watchers, self.collectors = {}, {}, {}
|
|
33
|
+
self.scopes = {}
|
|
34
|
+
self.closed = False
|
|
35
|
+
|
|
36
|
+
def _file(self, record, suffix=".json"):
|
|
37
|
+
return self.folder / (opaque_identity(record["run_id"]) + suffix)
|
|
38
|
+
|
|
39
|
+
def _origin(self, event):
|
|
40
|
+
# 宿主工具的结构化回执给出实际unit ID;不从通知文案或“最新父运行”猜来源。
|
|
41
|
+
if event.get("tool_name") != "delegate_task" or event.get("status") != "ok": return False
|
|
42
|
+
payload = event.get("result")
|
|
43
|
+
if isinstance(payload, str):
|
|
44
|
+
try: payload = json.loads(payload)
|
|
45
|
+
except ValueError: return False
|
|
46
|
+
if not isinstance(payload, dict) or payload.get("status") != "dispatched" or payload.get("mode") != "background": return False
|
|
47
|
+
units = payload.get("units") or [{"delegation_id": payload.get("delegation_id")}]
|
|
48
|
+
if not isinstance(units, list) or not units or any(not isinstance(u, dict) or not isinstance(u.get("delegation_id"), str) or not u["delegation_id"] for u in units): return False
|
|
49
|
+
locator = getattr(self.adapter.runtime, "locator", None)
|
|
50
|
+
if not callable(getattr(locator, "subagent_owner", None)): return False
|
|
51
|
+
scope = locator.subagent_owner(event.get("session_id"))
|
|
52
|
+
if not scope or not locator.subagent_context_matches(scope): return False
|
|
53
|
+
for unit in units:
|
|
54
|
+
producer = unit["delegation_id"]
|
|
55
|
+
origin = {"producer_id": producer, "scope": copy.deepcopy(scope)}
|
|
56
|
+
existing = self.origins.get(producer)
|
|
57
|
+
if existing is not None and existing != origin: continue
|
|
58
|
+
private_json(self.origins_folder / (opaque_identity(producer) + ".json"), origin)
|
|
59
|
+
self.origins[producer] = origin
|
|
60
|
+
self._collect_session(scope)
|
|
61
|
+
return True
|
|
62
|
+
|
|
63
|
+
def _scope(self, frame):
|
|
64
|
+
adapter = self.adapter
|
|
65
|
+
locator = getattr(adapter.runtime, "locator", None)
|
|
66
|
+
if (self.closed or not adapter.plugin_enabled or adapter.host is None
|
|
67
|
+
or not callable(getattr(locator, "continuation_owner", None))): return None
|
|
68
|
+
key = (id(adapter.runtime), id(adapter.bindings), adapter.service_id, *(frame.get(k) for k in (
|
|
69
|
+
"session_id", "admission_id", "authority_epoch", "execution_generation")))
|
|
70
|
+
cached = self.scopes.get(key)
|
|
71
|
+
if cached is not None: return cached
|
|
72
|
+
scope = locator.continuation_owner(frame)
|
|
73
|
+
if not scope or scope.get("profile") != str(adapter.profile) or scope.get("service") != adapter.service_id: return None
|
|
74
|
+
producers = scope["producer_ids"]
|
|
75
|
+
origins = [self.origins.get(producer) for producer in producers]
|
|
76
|
+
if not origins or any(origin is None for origin in origins): return None
|
|
77
|
+
roots = []
|
|
78
|
+
for origin in origins:
|
|
79
|
+
original = origin["scope"]
|
|
80
|
+
if any(original.get(key) != scope.get(key) for key in ("profile", "service", "chat_id", "route")): return None
|
|
81
|
+
matches = [r for r in adapter.bindings.all() if r.get("run_id") == original["root_run_id"]
|
|
82
|
+
and r.get("input_id") == original["input_id"] and r.get("profile") == scope["profile"]
|
|
83
|
+
and r.get("service_id") == scope["service"] and r.get("chat_id") == scope["chat_id"]]
|
|
84
|
+
if len(matches) != 1: return None
|
|
85
|
+
roots.append(matches[0])
|
|
86
|
+
if len({root["session_key"] for root in roots}) != 1: return None
|
|
87
|
+
# 合并通知可包含同一母会话的多个原输入;锚定真实来源之一,不改写任何旧输入。
|
|
88
|
+
root = min(roots, key=lambda r: r["run_id"])
|
|
89
|
+
scope = {**scope, "origin_native_ids": sorted({o["scope"]["native_id"] for o in origins})}
|
|
90
|
+
if not locator.continuation_matches(scope): return None
|
|
91
|
+
result = {"native": scope, "root": root, "runtime": adapter.runtime, "bindings": adapter.bindings}
|
|
92
|
+
self.scopes[key] = result
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
def _record(self, scope):
|
|
96
|
+
native, root = scope["native"], scope["root"]
|
|
97
|
+
identity = opaque_identity(native["profile"], native["service"], native["admission_id"], native["authority_epoch"], native["generation"])
|
|
98
|
+
run_id = "continuation:" + identity
|
|
99
|
+
record = self.records.get(run_id)
|
|
100
|
+
if record is None:
|
|
101
|
+
record = {"version": 1, "profile": native["profile"], "service_id": native["service"], "scope": native,
|
|
102
|
+
"run_id": run_id, "root_run_id": root["run_id"], "session_key": root["session_key"],
|
|
103
|
+
"chat_id": native["chat_id"], "native_id": native["native_id"], "input_id": native["input_id"],
|
|
104
|
+
"client_request_id": "observed:" + run_id, "history_uids": [],
|
|
105
|
+
"phase": "running", "sequence": 0, "ack": 0}
|
|
106
|
+
self.records[run_id] = record
|
|
107
|
+
private_json(self._file(record), record)
|
|
108
|
+
if record["phase"] == "running": self._watch(record, scope["runtime"], scope["bindings"])
|
|
109
|
+
return record
|
|
110
|
+
|
|
111
|
+
def _append(self, record, kind, payload, native_cursor=None):
|
|
112
|
+
sequence = record["sequence"] + 1
|
|
113
|
+
folder = self._file(record, ".events")
|
|
114
|
+
folder.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
115
|
+
private_json(folder / (str(sequence).zfill(16) + ".json"), {"sequence": sequence, "kind": kind, "payload": payload,
|
|
116
|
+
**({"native_cursor": native_cursor} if native_cursor else {})})
|
|
117
|
+
if native_cursor: record["native_cursor"] = native_cursor
|
|
118
|
+
record["sequence"] = sequence
|
|
119
|
+
if kind == "stop": record["phase"] = payload["phase"]
|
|
120
|
+
private_json(self._file(record), record)
|
|
121
|
+
return self._schedule(record["run_id"])
|
|
122
|
+
|
|
123
|
+
def observe(self, kind, event):
|
|
124
|
+
return False if self.closed else kind == "tool.complete" and self._origin(event)
|
|
125
|
+
|
|
126
|
+
def _collect_session(self, original):
|
|
127
|
+
native = original["native_id"]
|
|
128
|
+
current = self.collectors.get(native)
|
|
129
|
+
if current is not None and not current.done(): return
|
|
130
|
+
task = self.collectors[native] = asyncio.create_task(self._read_session(copy.deepcopy(original)))
|
|
131
|
+
task.add_done_callback(lambda done: self.collectors.pop(native, None) if self.collectors.get(native) is done else None)
|
|
132
|
+
|
|
133
|
+
async def _read_session(self, original):
|
|
134
|
+
adapter = self.adapter
|
|
135
|
+
runtime, bindings = adapter.runtime, adapter.bindings
|
|
136
|
+
cursor_file = self.folder / "cursors" / (opaque_identity(original["profile"], original["service"], original["native_id"]) + ".json")
|
|
137
|
+
cursor = json.loads(cursor_file.read_text()) if cursor_file.exists() else None
|
|
138
|
+
while (not self.closed and adapter.runtime is runtime and adapter.bindings is bindings
|
|
139
|
+
and adapter.service_id == original["service"]):
|
|
140
|
+
try:
|
|
141
|
+
replay = await runtime.replay(conversation_id=original["native_id"], admission_id=original["admission_id"],
|
|
142
|
+
input_id=original["input_id"], cursor=cursor)
|
|
143
|
+
if (self.closed or adapter.runtime is not runtime or adapter.bindings is not bindings
|
|
144
|
+
or adapter.service_id != original["service"]): return
|
|
145
|
+
epoch = replay.get("replay_epoch")
|
|
146
|
+
if not isinstance(epoch, str) or not epoch: raise ValueError("CONTINUATION_REPLAY_INVALID")
|
|
147
|
+
if replay.get("snapshot_required") or replay.get("gap_detected"):
|
|
148
|
+
raise ValueError("CONTINUATION_REPLAY_GAP")
|
|
149
|
+
sequence = cursor["sequence"] if cursor and cursor["epoch"] == epoch else 0
|
|
150
|
+
for frame in replay["events"]:
|
|
151
|
+
seq = frame.get("seq")
|
|
152
|
+
if (frame.get("session_id") != original["native_id"] or frame.get("replay_epoch") != epoch
|
|
153
|
+
or type(seq) is not int or seq <= sequence): raise ValueError("CONTINUATION_FRAME_INVALID")
|
|
154
|
+
scope = self._scope(frame)
|
|
155
|
+
if scope is not None:
|
|
156
|
+
record = self._record(scope)
|
|
157
|
+
saved = record.get("native_cursor", {})
|
|
158
|
+
if record["phase"] == "running" and (saved.get("epoch") != epoch or saved.get("sequence", 0) < seq):
|
|
159
|
+
self._append(record, frame.get("type", ""), frame.get("payload") or {}, {"epoch": epoch, "sequence": seq})
|
|
160
|
+
sequence = seq
|
|
161
|
+
cursor = {"epoch": epoch, "sequence": sequence}
|
|
162
|
+
private_json(cursor_file, cursor)
|
|
163
|
+
origins = [o for o in self.origins.values() if o["scope"]["service"] == original["service"] and o["scope"]["native_id"] == original["native_id"]]
|
|
164
|
+
related = [r for r in self.records.values() if r["service_id"] == original["service"] and r["native_id"] == original["native_id"]]
|
|
165
|
+
settled = {producer for r in related if r["phase"] != "running" for producer in r["scope"]["producer_ids"]}
|
|
166
|
+
if origins and all(o["producer_id"] in settled for o in origins): return
|
|
167
|
+
await asyncio.sleep(0.15 if any(r["phase"] == "running" for r in related) else 0.5)
|
|
168
|
+
except asyncio.CancelledError: raise
|
|
169
|
+
except Exception:
|
|
170
|
+
logger.warning("continuation.replay.unconfirmed")
|
|
171
|
+
for record in self.records.values():
|
|
172
|
+
if record["service_id"] == original["service"] and record["native_id"] == original["native_id"] and record["phase"] == "running":
|
|
173
|
+
self._append(record, "stop", {"phase": "reconciling"})
|
|
174
|
+
return
|
|
175
|
+
|
|
176
|
+
def _watch(self, record, runtime, bindings):
|
|
177
|
+
run_id = record["run_id"]
|
|
178
|
+
current = self.watchers.get(run_id)
|
|
179
|
+
if current is not None and not current.done(): return
|
|
180
|
+
task = self.watchers[run_id] = asyncio.create_task(self._read_outcome(record, runtime, bindings))
|
|
181
|
+
task.add_done_callback(lambda done: self.watchers.pop(run_id, None) if self.watchers.get(run_id) is done else None)
|
|
182
|
+
|
|
183
|
+
async def _read_outcome(self, record, runtime, bindings):
|
|
184
|
+
adapter = self.adapter
|
|
185
|
+
while (not self.closed and adapter.runtime is runtime and adapter.bindings is bindings
|
|
186
|
+
and adapter.service_id == record["service_id"] and record["phase"] == "running"):
|
|
187
|
+
try:
|
|
188
|
+
receipt, result = await runtime.continuation_receipt(record["scope"])
|
|
189
|
+
if (self.closed or adapter.runtime is not runtime or adapter.bindings is not bindings
|
|
190
|
+
or adapter.service_id != record["service_id"] or record["phase"] != "running"): return
|
|
191
|
+
status = receipt.get("status")
|
|
192
|
+
if status in ("queued", "started"):
|
|
193
|
+
await asyncio.sleep(0.15)
|
|
194
|
+
continue
|
|
195
|
+
phase = "reconciling"
|
|
196
|
+
if status == "terminal":
|
|
197
|
+
if isinstance(result, dict):
|
|
198
|
+
# 原生最终回执给出稳定历史身份;先持久化,再发终态,避免补读竞态。
|
|
199
|
+
record.update({key: copy.deepcopy(result[key]) for key in ("history_uids", "answer_uid", "history_rows", "final_row_id") if key in result})
|
|
200
|
+
private_json(self._file(record), record)
|
|
201
|
+
self._append(record, "message.complete", {"text": result.get("text"), "usage": {
|
|
202
|
+
"model": result.get("model"), "total": result.get("total_tokens"),
|
|
203
|
+
"context_used": result.get("last_prompt_tokens"), "context_max": result.get("context_length"),
|
|
204
|
+
"context_source": result.get("context_source"), "context_estimated": result.get("context_estimated")}})
|
|
205
|
+
phase = {"completed": "completed", "success": "completed", "failed": "failed", "error": "failed", "cancelled": "cancelled", "interrupted": "cancelled"}.get(receipt.get("outcome"), "reconciling")
|
|
206
|
+
self._append(record, "stop", {"phase": phase})
|
|
207
|
+
return
|
|
208
|
+
except asyncio.CancelledError: raise
|
|
209
|
+
except Exception:
|
|
210
|
+
logger.warning("continuation.outcome.unconfirmed")
|
|
211
|
+
self._append(record, "stop", {"phase": "reconciling"})
|
|
212
|
+
return
|
|
213
|
+
|
|
214
|
+
def _schedule(self, run_id):
|
|
215
|
+
current = self.tasks.get(run_id)
|
|
216
|
+
if current is not None and not current.done(): return current
|
|
217
|
+
task = self.tasks[run_id] = asyncio.create_task(self._pump(run_id))
|
|
218
|
+
task.add_done_callback(lambda done: self.tasks.pop(run_id, None) if self.tasks.get(run_id) is done else None)
|
|
219
|
+
return task
|
|
220
|
+
|
|
221
|
+
async def _pump(self, run_id):
|
|
222
|
+
record = self.records[run_id]
|
|
223
|
+
while not self.closed and record["service_id"] == self.adapter.service_id:
|
|
224
|
+
entries = [json.loads(p.read_text()) for p in sorted(self._file(record, ".events").glob("*.json"))]
|
|
225
|
+
entries = [e for e in entries if e["sequence"] > record["ack"]]
|
|
226
|
+
if not entries: return
|
|
227
|
+
projection = StreamProjection(run_id, record.get("projection_state"))
|
|
228
|
+
phase = record.get("acked_phase", "running")
|
|
229
|
+
try:
|
|
230
|
+
for index, entry in enumerate(entries, record["ack"] + 1):
|
|
231
|
+
if entry["sequence"] != index: raise RuntimeError("CONTINUATION_OBSERVATION_GAP")
|
|
232
|
+
updates = [] if entry["kind"] == "stop" else projection.apply(entry["kind"], entry["payload"])
|
|
233
|
+
if entry["kind"] == "stop": phase = entry["payload"]["phase"]
|
|
234
|
+
host = self.adapter.host
|
|
235
|
+
reply = await host.publish({"serviceInstanceId": record["service_id"], "runId": record["root_run_id"],
|
|
236
|
+
"continuation": {"runId": run_id, "phase": phase},
|
|
237
|
+
"source": {"id": opaque_identity(record["profile"], record["service_id"], record["scope"]["admission_id"]), "sequence": entry["sequence"]}, "updates": updates})
|
|
238
|
+
if self.closed or self.adapter.host is not host or reply.get("durable") is not True:
|
|
239
|
+
raise RuntimeError("CONTINUATION_ACK_NOT_DURABLE")
|
|
240
|
+
record.update(ack=entry["sequence"], projection_state=projection.state, acked_phase=phase)
|
|
241
|
+
private_json(self._file(record), record)
|
|
242
|
+
(self._file(record, ".events") / (str(entry["sequence"]).zfill(16) + ".json")).unlink(missing_ok=True)
|
|
243
|
+
except asyncio.CancelledError: raise
|
|
244
|
+
except Exception:
|
|
245
|
+
logger.warning("continuation.delivery.unconfirmed")
|
|
246
|
+
await asyncio.sleep(0.25)
|
|
247
|
+
|
|
248
|
+
def history_links(self):
|
|
249
|
+
fields = ("profile", "run_id", "native_id", "input_id", "client_request_id", "session_key", "history_uids", "answer_uid", "history_rows", "final_row_id")
|
|
250
|
+
return [{k: copy.deepcopy(r[k]) for k in fields if k in r} for r in self.records.values() if r["service_id"] == self.adapter.service_id]
|
|
251
|
+
|
|
252
|
+
def inspect(self, run_id):
|
|
253
|
+
record = self.records.get(run_id)
|
|
254
|
+
if record is None or record["service_id"] != self.adapter.service_id: return {"state": "unknown", "reason": "CONTINUATION_SCOPE_UNKNOWN"}
|
|
255
|
+
if record["ack"] < record["sequence"]: return {"state": "running"}
|
|
256
|
+
if record["phase"] in ("completed", "failed", "cancelled"): return {"state": record["phase"]}
|
|
257
|
+
return {"state": "running"} if record["phase"] == "running" and run_id in self.watchers else {"state": "unknown", "reason": "CONTINUATION_OWNER_UNKNOWN"}
|
|
258
|
+
|
|
259
|
+
def rebind(self):
|
|
260
|
+
for origin in self.origins.values():
|
|
261
|
+
if origin["scope"]["service"] == self.adapter.service_id: self._collect_session(origin["scope"])
|
|
262
|
+
for record in self.records.values():
|
|
263
|
+
if record["service_id"] != self.adapter.service_id: continue
|
|
264
|
+
self._schedule(record["run_id"])
|
|
265
|
+
if record["phase"] == "running": self._watch(record, self.adapter.runtime, self.adapter.bindings)
|
|
266
|
+
|
|
267
|
+
async def close(self):
|
|
268
|
+
self.closed = True
|
|
269
|
+
tasks = list(self.tasks.values()) + list(self.watchers.values()) + list(self.collectors.values())
|
|
270
|
+
for task in tasks: task.cancel()
|
|
271
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"operations": {
|
|
4
|
+
"location-query": {
|
|
5
|
+
"name": "fridaynext_location_query",
|
|
6
|
+
"description": "单次读取当前App会话绑定iPhone的位置、精度和时间;无位置历史或持续追踪。手机须在线并允许位置共享。",
|
|
7
|
+
"parameters": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"additionalProperties": false,
|
|
10
|
+
"properties": {}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"health-query": {
|
|
14
|
+
"name": "fridaynext_health_query",
|
|
15
|
+
"description": "查询当前App会话绑定iPhone的Apple Health聚合数据,指标按用户开启类别返回;默认过去24小时,最多31天。noData仅表示未返回样本,不能据此判断读取权限或编造数据。记录数据请用fridaynext_health_log。",
|
|
16
|
+
"parameters": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"additionalProperties": false,
|
|
19
|
+
"properties": {
|
|
20
|
+
"metrics": {
|
|
21
|
+
"type": "array",
|
|
22
|
+
"items": {
|
|
23
|
+
"type": "string",
|
|
24
|
+
"enum": [
|
|
25
|
+
"steps",
|
|
26
|
+
"distanceWalkingRunning",
|
|
27
|
+
"distanceCycling",
|
|
28
|
+
"distanceSwimming",
|
|
29
|
+
"activeEnergy",
|
|
30
|
+
"basalEnergy",
|
|
31
|
+
"exerciseMinutes",
|
|
32
|
+
"standHours",
|
|
33
|
+
"flightsClimbed",
|
|
34
|
+
"sleep",
|
|
35
|
+
"heartRate",
|
|
36
|
+
"restingHeartRate",
|
|
37
|
+
"walkingHeartRate",
|
|
38
|
+
"hrv",
|
|
39
|
+
"vo2Max",
|
|
40
|
+
"workouts",
|
|
41
|
+
"bodyMass",
|
|
42
|
+
"leanBodyMass",
|
|
43
|
+
"bodyFat",
|
|
44
|
+
"bmi",
|
|
45
|
+
"height",
|
|
46
|
+
"waistCircumference",
|
|
47
|
+
"mindfulMinutes",
|
|
48
|
+
"dietaryWater",
|
|
49
|
+
"dietaryEnergy",
|
|
50
|
+
"dietaryProtein",
|
|
51
|
+
"dietaryCarbohydrates",
|
|
52
|
+
"dietaryFiber",
|
|
53
|
+
"dietarySugar",
|
|
54
|
+
"dietaryFatTotal",
|
|
55
|
+
"dietaryFatSaturated",
|
|
56
|
+
"dietaryFatMonounsaturated",
|
|
57
|
+
"dietaryFatPolyunsaturated",
|
|
58
|
+
"dietaryCholesterol",
|
|
59
|
+
"dietaryCaffeine",
|
|
60
|
+
"dietarySodium",
|
|
61
|
+
"dietaryPotassium",
|
|
62
|
+
"dietaryCalcium",
|
|
63
|
+
"dietaryMagnesium",
|
|
64
|
+
"dietaryPhosphorus",
|
|
65
|
+
"dietaryChloride",
|
|
66
|
+
"dietaryIron",
|
|
67
|
+
"dietaryZinc",
|
|
68
|
+
"dietaryCopper",
|
|
69
|
+
"dietaryManganese",
|
|
70
|
+
"dietarySelenium",
|
|
71
|
+
"dietaryIodine",
|
|
72
|
+
"dietaryChromium",
|
|
73
|
+
"dietaryMolybdenum",
|
|
74
|
+
"dietaryVitaminA",
|
|
75
|
+
"dietaryVitaminC",
|
|
76
|
+
"dietaryVitaminD",
|
|
77
|
+
"dietaryVitaminE",
|
|
78
|
+
"dietaryVitaminK",
|
|
79
|
+
"dietaryVitaminB6",
|
|
80
|
+
"dietaryVitaminB12",
|
|
81
|
+
"dietaryThiamin",
|
|
82
|
+
"dietaryRiboflavin",
|
|
83
|
+
"dietaryNiacin",
|
|
84
|
+
"dietaryFolate",
|
|
85
|
+
"dietaryBiotin",
|
|
86
|
+
"dietaryPantothenicAcid"
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"start": {
|
|
91
|
+
"type": "string"
|
|
92
|
+
},
|
|
93
|
+
"end": {
|
|
94
|
+
"type": "string"
|
|
95
|
+
},
|
|
96
|
+
"bucket": {
|
|
97
|
+
"type": "string",
|
|
98
|
+
"enum": [
|
|
99
|
+
"none",
|
|
100
|
+
"hour",
|
|
101
|
+
"day"
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"health-log": {
|
|
108
|
+
"name": "fridaynext_health_log",
|
|
109
|
+
"description": "在当前App会话绑定iPhone的Apple Health记录营养、饮水、咖啡因、身体测量或正念数据;最多20条,31天内。需独立写入开关、类别及系统权限;读取开关可关闭。不能写入步数、心率、睡眠或运动。结果未知时不要自动重试。",
|
|
110
|
+
"parameters": {
|
|
111
|
+
"type": "object",
|
|
112
|
+
"additionalProperties": false,
|
|
113
|
+
"properties": {
|
|
114
|
+
"samples": {
|
|
115
|
+
"type": "array",
|
|
116
|
+
"items": {
|
|
117
|
+
"type": "object",
|
|
118
|
+
"additionalProperties": false,
|
|
119
|
+
"properties": {
|
|
120
|
+
"metric": {
|
|
121
|
+
"type": "string",
|
|
122
|
+
"enum": [
|
|
123
|
+
"dietaryWater",
|
|
124
|
+
"dietaryEnergy",
|
|
125
|
+
"dietaryProtein",
|
|
126
|
+
"dietaryCarbohydrates",
|
|
127
|
+
"dietaryFiber",
|
|
128
|
+
"dietarySugar",
|
|
129
|
+
"dietaryFatTotal",
|
|
130
|
+
"dietaryFatSaturated",
|
|
131
|
+
"dietaryFatMonounsaturated",
|
|
132
|
+
"dietaryFatPolyunsaturated",
|
|
133
|
+
"dietaryCholesterol",
|
|
134
|
+
"dietaryCaffeine",
|
|
135
|
+
"dietarySodium",
|
|
136
|
+
"dietaryPotassium",
|
|
137
|
+
"dietaryCalcium",
|
|
138
|
+
"dietaryMagnesium",
|
|
139
|
+
"dietaryPhosphorus",
|
|
140
|
+
"dietaryChloride",
|
|
141
|
+
"dietaryIron",
|
|
142
|
+
"dietaryZinc",
|
|
143
|
+
"dietaryCopper",
|
|
144
|
+
"dietaryManganese",
|
|
145
|
+
"dietarySelenium",
|
|
146
|
+
"dietaryIodine",
|
|
147
|
+
"dietaryChromium",
|
|
148
|
+
"dietaryMolybdenum",
|
|
149
|
+
"dietaryVitaminA",
|
|
150
|
+
"dietaryVitaminC",
|
|
151
|
+
"dietaryVitaminD",
|
|
152
|
+
"dietaryVitaminE",
|
|
153
|
+
"dietaryVitaminK",
|
|
154
|
+
"dietaryVitaminB6",
|
|
155
|
+
"dietaryVitaminB12",
|
|
156
|
+
"dietaryThiamin",
|
|
157
|
+
"dietaryRiboflavin",
|
|
158
|
+
"dietaryNiacin",
|
|
159
|
+
"dietaryFolate",
|
|
160
|
+
"dietaryBiotin",
|
|
161
|
+
"dietaryPantothenicAcid",
|
|
162
|
+
"bodyMass",
|
|
163
|
+
"leanBodyMass",
|
|
164
|
+
"bodyFat",
|
|
165
|
+
"bmi",
|
|
166
|
+
"height",
|
|
167
|
+
"waistCircumference",
|
|
168
|
+
"mindfulMinutes"
|
|
169
|
+
]
|
|
170
|
+
},
|
|
171
|
+
"value": {
|
|
172
|
+
"type": "number",
|
|
173
|
+
"minimum": 0
|
|
174
|
+
},
|
|
175
|
+
"unit": {
|
|
176
|
+
"type": "string"
|
|
177
|
+
},
|
|
178
|
+
"at": {
|
|
179
|
+
"type": "string"
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
"required": [
|
|
183
|
+
"metric",
|
|
184
|
+
"value"
|
|
185
|
+
]
|
|
186
|
+
},
|
|
187
|
+
"minItems": 1,
|
|
188
|
+
"maxItems": 20
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
"required": [
|
|
192
|
+
"samples"
|
|
193
|
+
]
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
"calendar-query": {
|
|
197
|
+
"name": "fridaynext_calendar_query",
|
|
198
|
+
"description": "查询当前App会话绑定iPhone的日历事件或提醒事项。默认未来7天,最多31天,按手机当前时区处理未带时区的日期;kinds可分别选择events/reminders。",
|
|
199
|
+
"parameters": {
|
|
200
|
+
"type": "object",
|
|
201
|
+
"additionalProperties": false,
|
|
202
|
+
"properties": {
|
|
203
|
+
"kinds": {
|
|
204
|
+
"type": "array",
|
|
205
|
+
"items": {
|
|
206
|
+
"type": "string",
|
|
207
|
+
"enum": [
|
|
208
|
+
"events",
|
|
209
|
+
"reminders"
|
|
210
|
+
]
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
"start": {
|
|
214
|
+
"type": "string"
|
|
215
|
+
},
|
|
216
|
+
"end": {
|
|
217
|
+
"type": "string"
|
|
218
|
+
},
|
|
219
|
+
"limit": {
|
|
220
|
+
"type": "integer",
|
|
221
|
+
"minimum": 1,
|
|
222
|
+
"maximum": 50
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
"calendar-log": {
|
|
228
|
+
"name": "fridaynext_calendar_log",
|
|
229
|
+
"description": "在当前App会话绑定iPhone创建日历事件(kind=event)或待办(kind=reminder),最多10条;只用用户选定的默认日历/提醒列表,不修改删除或创建重复规则。事件使用start/end/allDay/location;提醒使用dueDate/priority;alarmMinutesBefore设置闹钟,0表示不设。事件默认1小时,全天默认次日结束。需相应实体共享和系统权限。Agent后台定时工作使用Hermes自身定时任务。结果未知时不要自动重试。",
|
|
230
|
+
"parameters": {
|
|
231
|
+
"type": "object",
|
|
232
|
+
"additionalProperties": false,
|
|
233
|
+
"properties": {
|
|
234
|
+
"items": {
|
|
235
|
+
"type": "array",
|
|
236
|
+
"items": {
|
|
237
|
+
"type": "object",
|
|
238
|
+
"additionalProperties": false,
|
|
239
|
+
"properties": {
|
|
240
|
+
"kind": {
|
|
241
|
+
"type": "string",
|
|
242
|
+
"enum": [
|
|
243
|
+
"event",
|
|
244
|
+
"reminder"
|
|
245
|
+
]
|
|
246
|
+
},
|
|
247
|
+
"title": {
|
|
248
|
+
"type": "string",
|
|
249
|
+
"minLength": 1,
|
|
250
|
+
"maxLength": 200
|
|
251
|
+
},
|
|
252
|
+
"notes": {
|
|
253
|
+
"type": "string",
|
|
254
|
+
"maxLength": 2000
|
|
255
|
+
},
|
|
256
|
+
"start": {
|
|
257
|
+
"type": "string"
|
|
258
|
+
},
|
|
259
|
+
"end": {
|
|
260
|
+
"type": "string"
|
|
261
|
+
},
|
|
262
|
+
"allDay": {
|
|
263
|
+
"type": "boolean"
|
|
264
|
+
},
|
|
265
|
+
"location": {
|
|
266
|
+
"type": "string",
|
|
267
|
+
"maxLength": 200
|
|
268
|
+
},
|
|
269
|
+
"dueDate": {
|
|
270
|
+
"type": "string"
|
|
271
|
+
},
|
|
272
|
+
"priority": {
|
|
273
|
+
"type": "string",
|
|
274
|
+
"enum": [
|
|
275
|
+
"none",
|
|
276
|
+
"low",
|
|
277
|
+
"medium",
|
|
278
|
+
"high"
|
|
279
|
+
]
|
|
280
|
+
},
|
|
281
|
+
"alarmMinutesBefore": {
|
|
282
|
+
"type": "integer",
|
|
283
|
+
"minimum": 0,
|
|
284
|
+
"maximum": 20160
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
"required": [
|
|
288
|
+
"kind",
|
|
289
|
+
"title"
|
|
290
|
+
]
|
|
291
|
+
},
|
|
292
|
+
"minItems": 1,
|
|
293
|
+
"maxItems": 10
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
"required": [
|
|
297
|
+
"items"
|
|
298
|
+
]
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|