fi-glass 1.1.1 → 1.2.0

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.
@@ -167,11 +167,25 @@ interface TurnError {
167
167
  /** Default idle watchdog: a turn with no state change for this long is hung. */
168
168
  declare const DEFAULT_TURN_TIMEOUT_MS = 60000;
169
169
  interface UseAgentConversationOptions {
170
- /** Identity of the active conversation. When it changes, the thread re-hydrates. */
170
+ /**
171
+ * Controlled mode: when provided, the CONSUMER owns the visible thread. The
172
+ * hook returns these verbatim as `conversation.messages` and stops folding —
173
+ * `send` drives `agent.send(text)` but pushes no optimistic message and folds
174
+ * no finished turn (the consumer maps its own transport into this transcript).
175
+ * For workflow adapters whose single send yields many agent-handoff messages
176
+ * (the 1-send -> 8+ messages case the single-turn fold cannot express; the
177
+ * Activist OS canary that surfaced this). Mutually exclusive with
178
+ * `initialMessages`/`conversationId`/`onMessagesChange` (those are bypassed —
179
+ * the consumer owns persistence); if both `externalMessages` and
180
+ * `initialMessages` are passed, `externalMessages` wins. Undefined =
181
+ * uncontrolled (the hook owns the thread, byte-identical to before).
182
+ */
183
+ externalMessages?: ChatMessage[];
184
+ /** Identity of the active conversation. When it changes, the thread re-hydrates. (Ignored in controlled mode.) */
171
185
  conversationId?: string | null;
172
- /** Messages to seed the thread with (the active conversation's stored transcript). */
186
+ /** Messages to seed the thread with (the active conversation's stored transcript). (Ignored in controlled mode.) */
173
187
  initialMessages?: ChatMessage[];
174
- /** Called when the thread changes from real activity (fold/revert) — a persist hook. */
188
+ /** Called when the thread changes from real activity (fold/revert) — a persist hook. (Not called in controlled mode.) */
175
189
  onMessagesChange?: (messages: ChatMessage[]) => void;
176
190
  /**
177
191
  * Idle watchdog in ms: if the live turn's state does not change for this long
@@ -381,15 +381,19 @@ import {
381
381
  var DEFAULT_TURN_TIMEOUT_MS = 6e4;
382
382
  function useAgentConversation(agent, options = {}) {
383
383
  const {
384
+ externalMessages,
384
385
  conversationId,
385
386
  initialMessages,
386
387
  onMessagesChange,
387
388
  turnTimeoutMs = DEFAULT_TURN_TIMEOUT_MS,
388
389
  isAppHandledError
389
390
  } = options;
391
+ const controlled = externalMessages !== void 0;
390
392
  const [messages, setMessages] = useState2(
391
393
  initialMessages ?? []
392
394
  );
395
+ const controlledRef = useRef(controlled);
396
+ controlledRef.current = controlled;
393
397
  const [turnError, setTurnError] = useState2(null);
394
398
  const [timedOut, setTimedOut] = useState2(false);
395
399
  const pending = useRef(false);
@@ -403,6 +407,7 @@ function useAgentConversation(agent, options = {}) {
403
407
  const skipPersist = useRef(true);
404
408
  const mounted = useRef(false);
405
409
  const revertOptimistic = useCallback(() => {
410
+ if (controlledRef.current) return;
406
411
  setMessages((prev) => {
407
412
  const last = prev[prev.length - 1];
408
413
  return last?.role === "user" ? prev.slice(0, -1) : prev;
@@ -415,12 +420,14 @@ function useAgentConversation(agent, options = {}) {
415
420
  lastSent.current = t;
416
421
  setTurnError(null);
417
422
  setTimedOut(false);
418
- skipPersist.current = true;
419
- setMessages((prev) => [...prev, makeUserMessage(t)]);
423
+ if (!controlled) {
424
+ skipPersist.current = true;
425
+ setMessages((prev) => [...prev, makeUserMessage(t)]);
426
+ }
420
427
  pending.current = true;
421
428
  void agent.send(t);
422
429
  },
423
- [agent]
430
+ [agent, controlled]
424
431
  );
425
432
  const retry = useCallback(() => {
426
433
  if (lastSent.current) send(lastSent.current);
@@ -442,6 +449,7 @@ function useAgentConversation(agent, options = {}) {
442
449
  }
443
450
  return;
444
451
  }
452
+ if (controlledRef.current) return;
445
453
  if (agent.turn.text) {
446
454
  skipPersist.current = false;
447
455
  setMessages((prev) => [...prev, foldAssistantTurn(agent.turn)]);
@@ -467,6 +475,7 @@ function useAgentConversation(agent, options = {}) {
467
475
  mounted.current = true;
468
476
  return;
469
477
  }
478
+ if (controlledRef.current) return;
470
479
  skipPersist.current = true;
471
480
  pending.current = false;
472
481
  setTurnError(null);
@@ -475,6 +484,7 @@ function useAgentConversation(agent, options = {}) {
475
484
  agent.reset?.();
476
485
  }, [conversationId]);
477
486
  useEffect2(() => {
487
+ if (controlledRef.current) return;
478
488
  if (skipPersist.current) {
479
489
  skipPersist.current = false;
480
490
  return;
@@ -485,12 +495,12 @@ function useAgentConversation(agent, options = {}) {
485
495
  skipPersist.current = true;
486
496
  setTurnError(null);
487
497
  setTimedOut(false);
488
- setMessages([]);
498
+ if (!controlled) setMessages([]);
489
499
  pending.current = false;
490
500
  agent.reset?.();
491
- }, [agent]);
501
+ }, [agent, controlled]);
492
502
  return {
493
- messages,
503
+ messages: externalMessages ?? messages,
494
504
  turn: agent.turn,
495
505
  isStreaming: agent.isStreaming && !timedOut,
496
506
  turnError,