Show startup shell before background avatar admission and update reviewed capabilities
This commit is contained in:
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"gateway.py": {
|
||||
"source": "workspace:gateway/gateway.py",
|
||||
"sha256": "de94825b2005c5e4eedd1beb10360b477dc40d7e1bd6dc5c7d95b50bbc46cbef",
|
||||
"bytes": 45724,
|
||||
"sha256": "0d57875ec480b2fe3990754fbef75d72e7a92cbfc1ec291b27461a983227bdbb",
|
||||
"bytes": 46738,
|
||||
"upstream_sha256": "d088bedcf5c0accd4712e06202f6ced8ab155aad57a5b05683724efa346326ce"
|
||||
},
|
||||
"guest_sessions.py": {
|
||||
@@ -326,8 +326,9 @@
|
||||
},
|
||||
"action_protocol.py": {
|
||||
"source": "workspace:gateway/action_protocol.py",
|
||||
"sha256": "ca98055f9ddba361d36607791522cee15ce254b3d40395cd45c508e96c42a2f8",
|
||||
"bytes": 4166
|
||||
"sha256": "04c65ce27140da7757e0164983efe33800dddcff0139d16e8fe4c8a99a3b50ee",
|
||||
"bytes": 4697,
|
||||
"upstream_sha256": "ca98055f9ddba361d36607791522cee15ce254b3d40395cd45c508e96c42a2f8"
|
||||
},
|
||||
"action_reply.py": {
|
||||
"source": "workspace:gateway/action_reply.py",
|
||||
@@ -359,6 +360,11 @@
|
||||
"source": "workspace:gateway/reviewed_action_packs.json",
|
||||
"sha256": "830334cacf04bc488e7f07809a6defdcec481410b6bedb7b78cfa78f8ecf334e",
|
||||
"bytes": 125
|
||||
},
|
||||
"tests/test_capabilities_ready.py": {
|
||||
"source": "workspace:gateway/tests/test_capabilities_ready.py",
|
||||
"sha256": "49b9cf64e6d30e389588014a3a1de04d1a7b98f20fbea036ace4dcf8debfe857",
|
||||
"bytes": 3494
|
||||
}
|
||||
},
|
||||
"archive_baseline_commit": "19a7adccd9322986eb0646b165b9ce1cf2650cf2",
|
||||
@@ -392,13 +398,13 @@
|
||||
},
|
||||
"release_baseline_commit": "d43c7d4e809a5a6b18ca9f28c8163a2f9ad34c7f",
|
||||
"deployment_baseline": {
|
||||
"release": "gateway-0.8.0 / reviewed full action/emotion candidate",
|
||||
"source": "startup capabilities update; live gateway-only hash-fenced deployment",
|
||||
"files": {
|
||||
"action_protocol.py": "ca98055f9ddba361d36607791522cee15ce254b3d40395cd45c508e96c42a2f8",
|
||||
"action_protocol.py": "04c65ce27140da7757e0164983efe33800dddcff0139d16e8fe4c8a99a3b50ee",
|
||||
"action_reply.py": "4d66164a6668a571dc861282a5c80e408b51dfcfd67f45141e8c6701f6b81364",
|
||||
"app.py": "d6520894dfbe035f64b1984aae15976db4df0e7e720195c81d3bf483b240056f",
|
||||
"face_context.py": "d032ff8033a49219595d6edff2950856937e024782143621d37d02b56b024315",
|
||||
"gateway.py": "de94825b2005c5e4eedd1beb10360b477dc40d7e1bd6dc5c7d95b50bbc46cbef",
|
||||
"gateway.py": "0d57875ec480b2fe3990754fbef75d72e7a92cbfc1ec291b27461a983227bdbb",
|
||||
"guest_sessions.py": "cc082fdb6c2ff7f5a41361ffe9ea2a53ebb944ee37354f65119405ad7a521c82",
|
||||
"history.py": "0015b3143268f3e22a8a8a53b65ef4e3522f1f881dfca213c93142899c02631e",
|
||||
"llm_provider.py": "b3cf6ab5e4c953ef13dc00802f4594055164c89b10b875dda44f071ad1ede082",
|
||||
@@ -412,6 +418,7 @@
|
||||
"server_clock.py": "dc46d79fd43c8b94f52acfdea0564058c47f3a3b8b43aa53fe6868a1821f3a0e",
|
||||
"tests/test_action_protocol.py": "f1340c6c68674fc5d077003ca6be51746b31462ccc26acc0c092fbad01f629dc",
|
||||
"tests/test_action_reply.py": "0c40a937be4c6545e04ed2268df1714c34dce982058f6b19daabf06b0fa30932",
|
||||
"tests/test_capabilities_ready.py": "49b9cf64e6d30e389588014a3a1de04d1a7b98f20fbea036ace4dcf8debfe857",
|
||||
"tests/test_continuity.py": "d8390238f390463a98f12b9909bb5846be65f5b6dc1b21b94338b4cd75aaafae",
|
||||
"tests/test_debug.py": "7d1d7876c4106f3df0b84cf8f7e479afb4a30ca51264fb94fa288fdd8155883e",
|
||||
"tests/test_face_context.py": "c297866117fa7406b5fda95250b67ca7ebd7476486ddd5455935c140d31c9f6f",
|
||||
|
||||
@@ -36,6 +36,17 @@ def negotiate(hello, configured):
|
||||
return sha, dict(pack)
|
||||
|
||||
|
||||
def pending_pin(hello, configured):
|
||||
"""Authorize only a future SAME reviewed pin on this authenticated connection.
|
||||
No action available until the client's complete immutable admission and handoff.
|
||||
"""
|
||||
if hello.get('action_protocol') != 1 or hello.get('action_kinds') != [] or 'action_pack_sha256' in hello:
|
||||
return None
|
||||
sha=hello.get('action_pending_pack_sha256')
|
||||
offer={'action_protocol':1,'action_kinds':['dance'],'action_pack_sha256':sha}
|
||||
return sha if negotiate(offer, configured) else None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ActionTicket:
|
||||
id: str
|
||||
|
||||
+21
-5
@@ -21,7 +21,7 @@ from history import History
|
||||
from face_context import FaceContext
|
||||
from reply_emotion import SYSTEM_FRAGMENT, parse_reply
|
||||
from action_reply import ACTION_FRAGMENT, ExpressionPrefix, parse_action_reply
|
||||
from action_protocol import ActionPlans, negotiate, configured_packs
|
||||
from action_protocol import ActionPlans, negotiate, configured_packs, pending_pin
|
||||
from guest_sessions import GuestSessions, COOKIE
|
||||
from tts_debug import DebugStore
|
||||
from persona_resolver import resolve_persona
|
||||
@@ -236,6 +236,7 @@ class Dialogue:
|
||||
self.owner = secrets.token_hex(16)
|
||||
self.action_plans = ActionPlans(self.owner, self.connection_generation)
|
||||
self.action_capability = None
|
||||
self.pending_action_pin = None
|
||||
self.action_expiry_task = None
|
||||
self.action_holding = False
|
||||
self.followup_pending = None
|
||||
@@ -504,10 +505,11 @@ class Dialogue:
|
||||
|
||||
async def generate(self, turn, text, queue, metrics, began):
|
||||
request_start = time.perf_counter()
|
||||
capability = self.action_capability # Request snapshot: readiness never changes an in-flight answer.
|
||||
metrics.update(voice_mode=self.voice_mode, voice_id=self.voice_id, tts_request_count=0)
|
||||
# Exactly one initial system: Qwen's deployed template rejects a second system.
|
||||
system = '以下角色设定只约束reply字段的可朗读正文。\n' + self.persona_rules + ''.join(
|
||||
'\n\n' + item['content'] for item in self.face_messages.get(turn, [])) + '\n\n' + SYSTEM_FRAGMENT + '\n' + ACTION_FRAGMENT + '\n网关实际可用动作:' + ('dance' if self.action_capability else '无')
|
||||
'\n\n' + item['content'] for item in self.face_messages.get(turn, [])) + '\n\n' + SYSTEM_FRAGMENT + '\n' + ACTION_FRAGMENT + '\n网关实际可用动作:' + ('dance' if capability else '无')
|
||||
messages = [{'role': 'system', 'content': system}] + self.contexts.get(turn, []) + [{'role': 'user', 'content': text}]
|
||||
url, headers, body = self.llm_provider.request(messages, max_tokens=160, action_plan=True)
|
||||
metrics['llm_request_count'] = 1
|
||||
@@ -548,14 +550,14 @@ class Dialogue:
|
||||
if turn != self.turn: return
|
||||
if not raw_body.strip(): raise ValueError('empty LLM body')
|
||||
parse_start = time.perf_counter()
|
||||
plan = parse_action_reply(raw_body, finish_reason, bool(self.action_capability))
|
||||
plan = parse_action_reply(raw_body, finish_reason, bool(capability))
|
||||
result = plan.speech
|
||||
metrics['action_reason'] = plan.action_reason
|
||||
metrics['expression_prepare_mode'] = 'schema_prefix_preload' if prepare_enabled else 'full_reply'
|
||||
if prepare_enabled and prefix.candidate and not prefix.consistent(plan, self.turn):
|
||||
await self.event('reply.emotion.prepare.cancel', turn, reason='final_invalid')
|
||||
if plan.action == 'dance':
|
||||
ticket = self.action_plans.create(turn, plan.after, self.action_capability)
|
||||
ticket = self.action_plans.create(turn, plan.after, capability)
|
||||
self.action_expiry_task = asyncio.create_task(self.expire_action(ticket.id, turn))
|
||||
metrics.update(reply_emotion_reason=result.reason, reply_parse_branch=result.parse_branch,
|
||||
reply_emotion=result.emotion, reply_intensity=result.intensity,
|
||||
@@ -592,6 +594,17 @@ class Dialogue:
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
async def capabilities_ready(self, data):
|
||||
# This Dialogue belongs to the authenticated socket/owner generation. The
|
||||
# HELLO authorization cannot be manufactured or changed by a later event.
|
||||
pin=self.pending_action_pin
|
||||
offered=negotiate(data, configured_packs())
|
||||
accepted=bool(pin and offered and offered[0]==pin and
|
||||
(self.action_capability is None or self.action_capability==offered))
|
||||
if accepted: self.action_capability=offered
|
||||
await self.event('client.capabilities', action_available=accepted,
|
||||
action_pack_sha256=pin or '')
|
||||
|
||||
async def action_status(self, data):
|
||||
status, after = self.action_plans.status(data, self.turn, self.owner, self.connection_generation)
|
||||
if status in ('cancelled', 'failed'):
|
||||
@@ -755,6 +768,7 @@ async def dialogue(ws: WebSocket):
|
||||
session.owner = owner or 'admin'
|
||||
session.action_plans = ActionPlans(session.owner, session.connection_generation)
|
||||
session.action_capability = negotiate(hello, configured_packs())
|
||||
session.pending_action_pin = pending_pin(hello, configured_packs())
|
||||
if owner: session.auth_check = lambda: bool(app.state.guests.get(owner))
|
||||
voice_mode = hello.get('voice_mode', 'continuous_voice')
|
||||
if voice_mode not in ('continuous_voice', 'fragment'): raise ValueError('voice mode')
|
||||
@@ -769,7 +783,7 @@ async def dialogue(ws: WebSocket):
|
||||
session.voice_id = voice_id
|
||||
session.debug_store = app.state.debug
|
||||
session.debug_enabled = hello.get('tts_debug', True) is True
|
||||
await session.event('hello', action_protocol=1, action_available=bool(session.action_capability), context_face_cue_version=1, reply_emotion_version=1, llm_structured_output=session.llm_provider.structured_output, version=VERSION, codec='pcm16', sample_rate=16000, output_sample_rate=48000, channels=1, frame_duration=20, header_version=1, conversation_id=cid, duplex_default=True, voice_mode=voice_mode, voice_id=voice_id, voices=app.state.voices)
|
||||
await session.event('hello', action_protocol=1, action_available=bool(session.action_capability), action_pending=bool(session.pending_action_pin), context_face_cue_version=1, reply_emotion_version=1, llm_structured_output=session.llm_provider.structured_output, version=VERSION, codec='pcm16', sample_rate=16000, output_sample_rate=48000, channels=1, frame_duration=20, header_version=1, conversation_id=cid, duplex_default=True, voice_mode=voice_mode, voice_id=voice_id, voices=app.state.voices)
|
||||
await session.send_history()
|
||||
started = time.monotonic()
|
||||
while time.monotonic() - started < 600:
|
||||
@@ -817,6 +831,8 @@ async def dialogue(ws: WebSocket):
|
||||
else: raise ValueError('history action')
|
||||
elif data.get('type') == 'playback.drained':
|
||||
await session.request_drained(data.get('turn_id'))
|
||||
elif data.get('type') == 'client.capabilities.ready':
|
||||
await session.capabilities_ready(data)
|
||||
elif data.get('type') == 'action.status':
|
||||
await session.action_status(data)
|
||||
elif data.get('type') == 'ping': await session.event('pong')
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import httpx
|
||||
from action_protocol import pending_pin
|
||||
from gateway import Dialogue
|
||||
from test_gateway import Socket
|
||||
from tests_fake import FakeModel
|
||||
|
||||
PIN='a'*64
|
||||
PACK={PIN:{'asset_01':1000,'asset_02':2000}}
|
||||
READY={'type':'client.capabilities.ready','action_protocol':1,'action_kinds':['dance'],'action_pack_sha256':PIN}
|
||||
|
||||
class CapabilitiesTests(unittest.IsolatedAsyncioTestCase):
|
||||
def test_pending_pin_exact_reviewed_empty_only(self):
|
||||
valid={'action_protocol':1,'action_kinds':[],'action_pending_pack_sha256':PIN}
|
||||
self.assertEqual(pending_pin(valid,PACK),PIN)
|
||||
for hello in ({}, {**valid,'action_protocol':2},{**valid,'action_kinds':['dance']},
|
||||
{**valid,'action_pack_sha256':PIN},{**valid,'action_pending_pack_sha256':'b'*64}):
|
||||
self.assertIsNone(pending_pin(hello,PACK))
|
||||
|
||||
async def test_update_same_socket_pin_once_no_turn_ticket_or_generator_change(self):
|
||||
d=Dialogue(Socket(),FakeModel(),None)
|
||||
with patch('gateway.configured_packs',return_value=PACK):
|
||||
await d.capabilities_ready(READY);self.assertIsNone(d.action_capability)
|
||||
d.pending_action_pin=PIN
|
||||
for bad in ({**READY,'action_pack_sha256':'b'*64},{**READY,'action_kinds':[]},{**READY,'action_protocol':2}):
|
||||
await d.capabilities_ready(bad);self.assertIsNone(d.action_capability)
|
||||
await d.capabilities_ready(READY);cap=d.action_capability
|
||||
ticket=d.action_plans.create(0,'after',cap)
|
||||
await d.capabilities_ready(READY)
|
||||
self.assertEqual(d.action_capability,cap);self.assertIs(d.action_plans.ticket,ticket)
|
||||
self.assertEqual(d.turn,0);self.assertIsNone(d.task)
|
||||
await d.capabilities_ready({**READY,'action_pack_sha256':'b'*64});self.assertEqual(d.action_capability,cap)
|
||||
other=Dialogue(Socket(),FakeModel(),None)
|
||||
with patch('gateway.configured_packs',return_value=PACK):await other.capabilities_ready(READY)
|
||||
self.assertIsNone(other.action_capability)
|
||||
await other.close();await d.close()
|
||||
|
||||
async def test_inflight_reply_freezes_capability_and_next_request_can_dance(self):
|
||||
entered=asyncio.Event();release=asyncio.Event();calls=[]
|
||||
raw=json.dumps(dict(emotion='happy',intensity=1,reply='公开合成正文',action='dance',after_action=dict(reply='公开后续',emotion='sad',intensity=1)))
|
||||
async def backend(request):
|
||||
calls.append(request)
|
||||
if len(calls)==1:entered.set();await release.wait()
|
||||
return httpx.Response(200,text='data: '+json.dumps({'choices':[{'delta':{'content':raw},'finish_reason':'stop'}]})+'\n\ndata: [DONE]\n\n')
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(backend)) as client:
|
||||
d=Dialogue(Socket(),FakeModel(),client);d.pending_action_pin=PIN
|
||||
with patch('gateway.configured_packs',return_value=PACK):
|
||||
task=asyncio.create_task(d.generate(0,'公开合成请求',asyncio.Queue(),{},time.perf_counter()))
|
||||
await asyncio.wait_for(entered.wait(),2);await d.capabilities_ready(READY);release.set();await task
|
||||
self.assertIsNone(d.action_plans.ticket)
|
||||
await d.generate(0,'公开合成请求',asyncio.Queue(),{},time.perf_counter())
|
||||
self.assertIsNotNone(d.action_plans.ticket);self.assertEqual(len(calls),2)
|
||||
await d.close()
|
||||
Reference in New Issue
Block a user