@yeaft/webchat-agent 1.0.508 → 1.0.510

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.
@@ -24,6 +24,7 @@ import { buildDreamOutputSnapshot } from './dream/output-snapshot.js';
24
24
  import { Engine } from './engine.js';
25
25
  import { loadSession } from './session.js';
26
26
  import { loadAgentMCPConfig, loadConfig, loadMCPConfig } from './config.js';
27
+ import { resolveMaxOutputTokens } from './models.js';
27
28
  import {
28
29
  createManagedProjectSkill,
29
30
  createManagedSkill,
@@ -160,7 +161,8 @@ const SKILL_RELOAD_INTERVAL_MS = 2_000;
160
161
  * loads the Session.
161
162
  * @type {Map<string, {
162
163
  * resolve:Function,
163
- * reject?:Function,
164
+ * reject:Function,
165
+ * conversationId:string|null,
164
166
  * sessionId:string,
165
167
  * vpId:string,
166
168
  * threadId:string,
@@ -176,6 +178,9 @@ const SKILL_RELOAD_INTERVAL_MS = 2_000;
176
178
  * onAbort?:Function,
177
179
  * }>} */
178
180
  const pendingUserPrompts = new Map();
181
+ // Instance-local retry receipts, never persisted with Session runtime data.
182
+ const terminalUserPrompts = new Map();
183
+ const MAX_TERMINAL_USER_PROMPTS = 256;
179
184
 
180
185
  /** @type {import('./session.js').Session | null} */
181
186
  let session = null;
@@ -2238,7 +2243,9 @@ async function routeEnvelopeToVpThread(sessionId, vpId, envelope) {
2238
2243
  });
2239
2244
  }
2240
2245
 
2241
- if (related) {
2246
+ // An append shares the running query's request config. A quick send owns
2247
+ // a separate query, so retain its envelope in this thread's normal inbox.
2248
+ if (related && !envelope?._turnConfig) {
2242
2249
  const content = promptParts || prompt;
2243
2250
  const injectedBy = envelope?.msg?.meta?.injectedBy;
2244
2251
  const isInternalAppend = injectedBy === 'route_forward' || injectedBy === 'task_result';
@@ -3036,10 +3043,10 @@ function mergedStatusForProjectRuntime(runtime, ownerSession = session) {
3036
3043
  }
3037
3044
 
3038
3045
  /** Send a Yeaft Session metadata event over the legacy-compatible envelope. */
3039
- function sendSessionEvent(event, { sessionId, chatId, vpId, turnId, threadId, requestId, requestClientId, perfTraceId } = {}) {
3046
+ function sendSessionEvent(event, { sessionId, chatId, vpId, turnId, threadId, requestId, requestClientId, perfTraceId, conversationId = yeaftConversationId } = {}) {
3040
3047
  sendToServer({
3041
3048
  type: 'yeaft_output',
3042
- conversationId: yeaftConversationId,
3049
+ conversationId,
3043
3050
  ...(perfTraceId ? { perfTraceId } : {}),
3044
3051
  ...(requestId ? { requestId } : {}),
3045
3052
  ...(requestClientId ? { _requestClientId: requestClientId } : {}),
@@ -3085,26 +3092,52 @@ function replayPendingUserPrompts(sessionId) {
3085
3092
  }
3086
3093
  }
3087
3094
 
3088
- function settlePendingUserPrompt(requestId, pending, { answers = null, timedOut = false } = {}) {
3095
+ function registerPendingUserPrompt(requestId, pending) {
3096
+ pending.onAbort = () => settlePendingUserPrompt(requestId, pending, { aborted: true });
3097
+ pending.timer = setTimeout(() => {
3098
+ settlePendingUserPrompt(requestId, pending, { timedOut: true });
3099
+ }, Math.max(0, pending.expiresAt - Date.now()));
3100
+ pending.timer.unref?.();
3101
+ pendingUserPrompts.set(requestId, pending);
3102
+ if (pending.signal?.aborted) pending.onAbort();
3103
+ else pending.signal?.addEventListener('abort', pending.onAbort, { once: true });
3104
+ }
3105
+
3106
+ function settlePendingUserPrompt(requestId, pending, { answers = null, timedOut = false, aborted = false } = {}) {
3089
3107
  if (pendingUserPrompts.get(requestId) !== pending) return false;
3090
3108
  pendingUserPrompts.delete(requestId);
3091
3109
  if (pending.timer) clearTimeout(pending.timer);
3092
3110
  if (pending.signal && pending.onAbort) {
3093
3111
  try { pending.signal.removeEventListener('abort', pending.onAbort); } catch { /* ignore */ }
3094
3112
  }
3095
- try { pending.resumeQueryTimer?.(); } catch { /* best-effort */ }
3096
- sendSessionEvent({
3097
- type: timedOut ? 'ask_user_expired' : 'ask_user_answered',
3113
+ if (!aborted) {
3114
+ try { pending.resumeQueryTimer?.(); } catch { /* best-effort */ }
3115
+ }
3116
+ const expired = timedOut || aborted;
3117
+ const event = {
3118
+ type: expired ? 'ask_user_expired' : 'ask_user_answered',
3098
3119
  requestId,
3099
3120
  toolCallId: pending.toolCallId || null,
3100
- ...(timedOut ? { expiredAt: Date.now() } : { answers: answers || {} }),
3101
- }, {
3121
+ ...(expired ? { expiredAt: Date.now() } : { answers: answers || {} }),
3122
+ };
3123
+ const identity = {
3124
+ requestId,
3125
+ toolCallId: pending.toolCallId,
3126
+ conversationId: pending.conversationId,
3102
3127
  sessionId: pending.sessionId,
3103
3128
  vpId: pending.vpId,
3104
3129
  threadId: pending.threadId,
3105
3130
  turnId: pending.turnId,
3106
- });
3107
- pending.resolve(timedOut ? { __yeaftTimedOut: true } : (answers || {}));
3131
+ };
3132
+ terminalUserPrompts.set(requestId, { identity, event });
3133
+ while (terminalUserPrompts.size > MAX_TERMINAL_USER_PROMPTS) {
3134
+ terminalUserPrompts.delete(terminalUserPrompts.keys().next().value);
3135
+ }
3136
+ // Like prompt replay, live settlement belongs to the current projection,
3137
+ // not the conversation generation captured when the question was created.
3138
+ sendSessionEvent(event, { ...identity, conversationId: yeaftConversationId || pending.conversationId });
3139
+ if (aborted) pending.reject(new Error('aborted'));
3140
+ else pending.resolve(timedOut ? { __yeaftTimedOut: true } : (answers || {}));
3108
3141
  return true;
3109
3142
  }
3110
3143
 
@@ -4744,6 +4777,27 @@ function handleEngineEvent(event, hctx) {
4744
4777
  * - No legacy "no-session" fallback paths — they were the source of the
4745
4778
  * router_unavailable bug fixed in v0.1.671.
4746
4779
  */
4780
+ /** Validate a one-message override against the loaded Agent catalog, never Session config. */
4781
+ export function validateQuickSend(value, config) {
4782
+ if (value === undefined) return null;
4783
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
4784
+ throw new Error('quickSend must be an object');
4785
+ }
4786
+ const { model, effort, maxOutputTokens } = value;
4787
+ const entry = typeof model === 'string' && model.includes('/')
4788
+ ? config?.availableModels?.find(candidate => candidate.ref === model)
4789
+ : null;
4790
+ if (!entry) throw new Error('quickSend.model must name an available provider/model');
4791
+ if (effort !== null && (typeof effort !== 'string' || !entry.effortOptions?.includes(effort))) {
4792
+ throw new Error('quickSend.effort is not supported by the selected model');
4793
+ }
4794
+ if (maxOutputTokens !== null && (!Number.isSafeInteger(maxOutputTokens) || maxOutputTokens < 1
4795
+ || maxOutputTokens > resolveMaxOutputTokens(entry.id, { modelInfo: entry }))) {
4796
+ throw new Error('quickSend.maxOutputTokens must be null or a positive integer within the model output limit');
4797
+ }
4798
+ return { model, effort, maxOutputTokens };
4799
+ }
4800
+
4747
4801
  async function runYeaftSessionSend(msg) {
4748
4802
  if (!msg || typeof msg !== 'object') return;
4749
4803
  const { text } = msg;
@@ -4800,6 +4854,20 @@ async function runYeaftSessionSend(msg) {
4800
4854
  return;
4801
4855
  }
4802
4856
 
4857
+ // Reject malformed overrides before boot, roster changes, attachment writes,
4858
+ // or coordinator persistence. The error includes the client id for retry.
4859
+ let turnConfig = null;
4860
+ try {
4861
+ if (msg.quickSend !== undefined) {
4862
+ turnConfig = validateQuickSend(msg.quickSend, loadConfig({ dir: yeaftDir }));
4863
+ }
4864
+ } catch (err) {
4865
+ sendSessionOutputFrame({ type: 'error', code: 'invalid_quick_send',
4866
+ message: err.message, clientMessageId: msg.id || null }, { sessionId });
4867
+ sendSessionOutputFrame({ type: 'result', result_text: '', is_error: true }, { sessionId });
4868
+ return;
4869
+ }
4870
+
4803
4871
  // Open the user-level session metadata first so project runtime loading can
4804
4872
  // use the stored workDir without blocking the user-message hot path. If
4805
4873
  // metadata is missing we still boot the agent runtime for a useful error
@@ -4981,6 +5049,7 @@ async function runYeaftSessionSend(msg) {
4981
5049
  // Live form — adapters need the base64 image blocks; runVpTurn
4982
5050
  // reads `_promptParts` off the envelope rather than going
4983
5051
  // back to disk on every fan-out target. NOT persisted.
5052
+ _turnConfig: turnConfig,
4984
5053
  _promptParts: attachmentBundle.promptParts,
4985
5054
  _promptSuffix: attachmentBundle.promptSuffix,
4986
5055
  _perfTraceId: perfTraceId,
@@ -5166,6 +5235,12 @@ export function buildVpQueryOpts({ vpId, sessionCoordinator, sessionId, envelope
5166
5235
  // envelope inside router.forward.
5167
5236
  if (envelope && typeof envelope === 'object') {
5168
5237
  out.inboundEnvelope = envelope;
5238
+ // Only direct delivery of this user message inherits quick-send settings.
5239
+ // RouteForward and background reentry keep the recipient's own defaults.
5240
+ if (envelope._turnConfig && !envelope.msg?.meta?.injectedBy
5241
+ && (envelope.msg?.role === 'user' || envelope.msg?.from === 'user')) {
5242
+ out.turnConfig = { ...envelope._turnConfig };
5243
+ }
5169
5244
  }
5170
5245
  // TodoWrite per-thread isolation. Bind closures that read/write a slot
5171
5246
  // keyed by `${sessionId}::${vpId}::${threadId}` so concurrent threads for
@@ -5720,6 +5795,7 @@ async function runVpTurn({ prompt, promptParts = null, sessionId, vpId, threadId
5720
5795
  const pending = {
5721
5796
  resolve,
5722
5797
  reject,
5798
+ conversationId: yeaftConversationId,
5723
5799
  sessionId,
5724
5800
  vpId,
5725
5801
  threadId,
@@ -5734,20 +5810,8 @@ async function runVpTurn({ prompt, promptParts = null, sessionId, vpId, threadId
5734
5810
  signal,
5735
5811
  onAbort: null,
5736
5812
  };
5737
- const onAbort = () => {
5738
- if (pendingUserPrompts.get(requestId) !== pending) return;
5739
- pendingUserPrompts.delete(requestId);
5740
- if (pending.timer) clearTimeout(pending.timer);
5741
- reject(new Error('aborted'));
5742
- };
5743
- pending.onAbort = onAbort;
5744
- pending.timer = setTimeout(() => {
5745
- settlePendingUserPrompt(requestId, pending, { timedOut: true });
5746
- }, ASK_USER_TIMEOUT_MS);
5747
- if (typeof pending.timer.unref === 'function') pending.timer.unref();
5748
- pendingUserPrompts.set(requestId, pending);
5749
- signal.addEventListener('abort', onAbort, { once: true });
5750
- sendPendingUserPrompt(requestId, pending);
5813
+ registerPendingUserPrompt(requestId, pending);
5814
+ if (pendingUserPrompts.has(requestId)) sendPendingUserPrompt(requestId, pending);
5751
5815
  }),
5752
5816
  threadId,
5753
5817
  vpTurnId: turnId,
@@ -6829,16 +6893,42 @@ export async function handleYeaftFetchDebugHistory(msg = {}) {
6829
6893
  }
6830
6894
 
6831
6895
  /** Resolve a pending Yeaft AskUser prompt from the web UI. */
6832
- export function handleYeaftAskUserAnswer(msg) {
6896
+ export function handleYeaftAskUserAnswer(msg = {}) {
6833
6897
  const requestId = typeof msg?.requestId === 'string' ? msg.requestId : '';
6898
+ const rejectAnswer = reason => {
6899
+ // Echo only submitted identity: never expose the actual pending scope.
6900
+ sendSessionEvent({
6901
+ type: 'ask_user_answer_rejected', requestId,
6902
+ toolCallId: msg?.toolCallId || null, reason,
6903
+ }, {
6904
+ requestId, requestClientId: msg?._requestClientId,
6905
+ conversationId: msg?.conversationId ?? null,
6906
+ sessionId: msg?.sessionId, vpId: msg?.vpId,
6907
+ turnId: msg?.turnId, threadId: msg?.threadId,
6908
+ });
6909
+ return false;
6910
+ };
6834
6911
  const pending = pendingUserPrompts.get(requestId);
6835
- if (!pending) return false;
6836
- if (msg.sessionId && msg.sessionId !== pending.sessionId) return false;
6837
- if (msg.vpId && msg.vpId !== pending.vpId) return false;
6838
- if (msg.turnId && msg.turnId !== pending.turnId) return false;
6839
- if (msg.threadId && msg.threadId !== pending.threadId) return false;
6840
- if (msg.toolCallId && msg.toolCallId !== pending.toolCallId) return false;
6841
-
6912
+ const terminal = terminalUserPrompts.get(requestId);
6913
+ const identity = pending || terminal?.identity;
6914
+ if (!identity) return rejectAnswer('unavailable');
6915
+ // Retain legacy optional identity matching. conversationId is a projection
6916
+ // generation, not Session ownership; it can change after a reconnect.
6917
+ for (const field of ['sessionId', 'vpId', 'turnId', 'threadId', 'toolCallId']) {
6918
+ if (msg[field] && msg[field] !== identity[field]) return rejectAnswer('identity_mismatch');
6919
+ }
6920
+ if (!pending) {
6921
+ sendSessionEvent(terminal.event, {
6922
+ ...terminal.identity,
6923
+ conversationId: msg.conversationId ?? terminal.identity.conversationId,
6924
+ requestClientId: msg._requestClientId,
6925
+ });
6926
+ return terminal.event.type === 'ask_user_answered' ? true : rejectAnswer('unavailable');
6927
+ }
6928
+ if (pending.expiresAt <= Date.now()) {
6929
+ settlePendingUserPrompt(requestId, pending, { timedOut: true });
6930
+ return rejectAnswer('unavailable');
6931
+ }
6842
6932
  return settlePendingUserPrompt(requestId, pending, { answers: msg.answers || {} });
6843
6933
  }
6844
6934
 
@@ -8211,11 +8301,10 @@ export const __testHooks = {
8211
8301
  vpAborts.clear();
8212
8302
  vpInboxes.clear();
8213
8303
  },
8214
- seedPendingUserPrompt({ requestId = 'ask-test', sessionId = 'session-test', vpId = 'vp-test', threadId = 'main', turnId = 'turn-test', toolCallId = 'call-test', question = 'Continue?', options = [], createdAt = Date.now(), expiresAt = Date.now() + ASK_USER_TIMEOUT_MS } = {}) {
8215
- let resolved;
8216
- const promise = new Promise(resolve => { resolved = resolve; });
8217
- pendingUserPrompts.set(requestId, { resolve: resolved, sessionId, vpId, threadId, turnId, toolCallId, question, options, createdAt, expiresAt, timer: null });
8218
- return promise;
8304
+ seedPendingUserPrompt({ requestId = 'ask-test', sessionId = 'session-test', vpId = 'vp-test', threadId = 'main', turnId = 'turn-test', toolCallId = 'call-test', conversationId = yeaftConversationId, question = 'Continue?', options = [], createdAt = Date.now(), expiresAt = Date.now() + ASK_USER_TIMEOUT_MS, signal = null } = {}) {
8305
+ return new Promise((resolve, reject) => {
8306
+ registerPendingUserPrompt(requestId, { resolve, reject, conversationId, sessionId, vpId, threadId, turnId, toolCallId, question, options, createdAt, expiresAt, signal });
8307
+ });
8219
8308
  },
8220
8309
  replayPendingUserPrompts,
8221
8310
  settlePendingUserPromptForTest(requestId, opts = {}) {
@@ -8225,8 +8314,10 @@ export const __testHooks = {
8225
8314
  resetPendingUserPrompts() {
8226
8315
  for (const pending of pendingUserPrompts.values()) {
8227
8316
  if (pending.timer) clearTimeout(pending.timer);
8317
+ if (pending.onAbort) pending.signal?.removeEventListener('abort', pending.onAbort);
8228
8318
  }
8229
8319
  pendingUserPrompts.clear();
8320
+ terminalUserPrompts.clear();
8230
8321
  },
8231
8322
  resetVpStatusBroker() {
8232
8323
  if (vpStatusBroker) vpStatusBroker.reset();