Malina Claude Opus 4.8 commited on
Commit
d53485f
·
1 Parent(s): 9510df1

NINA chat: NEED_VIDEO handshake for one-video-per-chat

Browse files

The chat holds one video (uploaded once at chat creation, kept in the browser only).
To avoid re-uploading it every turn, the client sends a turn WITHOUT the video; if
the router decides to re-watch, the Space replies with the NEED_VIDEO sentinel and
the client uploads the held video and retries with force_analyze. Adds has_video to
the payload so the router knows a re-watch is possible.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Files changed (1) hide show
  1. chat.py +25 -18
chat.py CHANGED
@@ -18,6 +18,10 @@ from config import LLM_MAX_TOKENS, LLM_MODEL, LLM_TEMPERATURE
18
  from models import llm_client
19
  from synthesis import _detect_language
20
 
 
 
 
 
21
 
22
  def _last_user(messages: list) -> dict:
23
  return next((m for m in reversed(messages) if m.get("role") == "user"), {})
@@ -107,6 +111,7 @@ def chat_stream(video_path, payload_json: str, token: str = ""):
107
  transcript = payload.get("transcript") or ""
108
  allow_analyze = bool(payload.get("allow_analyze", True))
109
  force = bool(payload.get("force_analyze", False))
 
110
 
111
  last = _last_user(messages)
112
  prompt = (last.get("content") or "").strip()
@@ -116,25 +121,27 @@ def chat_stream(video_path, payload_json: str, token: str = ""):
116
  return
117
 
118
  # Decide: analyze (re-watch) vs answer (from context).
119
- decision = "analyze" if force else _route(messages, video_path is not None)
120
- do_analyze = decision == "analyze" and video_path is not None and allow_analyze
121
-
122
- if do_analyze:
123
- from pipeline import _run_core, transcript_text
124
- ask = (f'Regarding "{quote}": ' if quote else "") + (prompt or "Summarize the video.")
125
- last_out = ""
126
- for out in _run_core(video_path, ask):
127
- last_out = out
128
- yield [out, True, ""]
129
- yield [last_out, True, transcript_text(video_path)]
130
- return
131
-
132
- # Answer from context. If the model wanted to re-watch but there's no video this
133
- # turn (media already gone) or no budget, note it.
 
 
 
 
134
  note = ""
135
- if decision == "analyze" and video_path is None:
136
- note = "\n\n_(To re-watch the video for new detail, re-upload it.)_"
137
- elif decision == "analyze" and not allow_analyze:
138
  note = "\n\n_(You've used your video-analysis budget for now, so I answered from our chat.)_"
139
  acc = ""
140
  for partial in _answer_from_context(messages, transcript):
 
18
  from models import llm_client
19
  from synthesis import _detect_language
20
 
21
+ # Control signal: the model wants to re-watch the video but the client didn't send
22
+ # it this turn. The client detects this, uploads the chat's held video, and retries.
23
+ NEED_VIDEO = "\x01NEED_VIDEO\x01"
24
+
25
 
26
  def _last_user(messages: list) -> dict:
27
  return next((m for m in reversed(messages) if m.get("role") == "user"), {})
 
111
  transcript = payload.get("transcript") or ""
112
  allow_analyze = bool(payload.get("allow_analyze", True))
113
  force = bool(payload.get("force_analyze", False))
114
+ has_video = bool(payload.get("has_video", False)) # client holds the chat's video
115
 
116
  last = _last_user(messages)
117
  prompt = (last.get("content") or "").strip()
 
121
  return
122
 
123
  # Decide: analyze (re-watch) vs answer (from context).
124
+ decision = "analyze" if force else _route(messages, video_path is not None or has_video)
125
+
126
+ if decision == "analyze" and allow_analyze:
127
+ if video_path is not None:
128
+ from pipeline import _run_core, transcript_text
129
+ ask = (f'Regarding "{quote}": ' if quote else "") + (prompt or "Summarize the video.")
130
+ last_out = ""
131
+ for out in _run_core(video_path, ask):
132
+ last_out = out
133
+ yield [out, True, ""]
134
+ yield [last_out, True, transcript_text(video_path)]
135
+ return
136
+ if has_video:
137
+ # We want to re-watch but the client didn't send the video this turn.
138
+ # Signal it to upload the chat's held video and retry (NEED_VIDEO handshake).
139
+ yield [NEED_VIDEO, False, ""]
140
+ return
141
+
142
+ # Answer from context (decision was answer, or no analysis budget, or no video).
143
  note = ""
144
+ if decision == "analyze" and not allow_analyze:
 
 
145
  note = "\n\n_(You've used your video-analysis budget for now, so I answered from our chat.)_"
146
  acc = ""
147
  for partial in _answer_from_context(messages, transcript):