@lambforge/agent-chat 0.0.4 → 0.0.6

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/dist/index.mjs CHANGED
@@ -3016,6 +3016,7 @@ var initialChatState = {
3016
3016
  conversationId: null,
3017
3017
  messages: [],
3018
3018
  answering: false,
3019
+ live: null,
3019
3020
  notice: null
3020
3021
  };
3021
3022
  function toMessage(m4) {
@@ -3025,7 +3026,7 @@ function toMessage(m4) {
3025
3026
 
3026
3027
  // src/store/chat-reducer.ts
3027
3028
  var LOCAL_ID = /^local:/;
3028
- var empty = { conversationId: null, messages: [], answering: false, notice: null };
3029
+ var empty = { conversationId: null, messages: [], answering: false, live: null, notice: null };
3029
3030
  var chatReducer = reducer(
3030
3031
  initialChatState,
3031
3032
  on(shown, (s9, { payload: payload2 }) => s9.shown || !payload2 ? s9 : { ...s9, shown: true }),
@@ -3033,6 +3034,7 @@ var chatReducer = reducer(
3033
3034
  on(failed, (s9, { payload: payload2 }) => ({
3034
3035
  ...s9,
3035
3036
  answering: false,
3037
+ live: null,
3036
3038
  notice: { kind: "error", code: payload2 }
3037
3039
  })),
3038
3040
  on(verifying, (s9, { payload: payload2 }) => ({
@@ -3053,6 +3055,7 @@ var chatReducer = reducer(
3053
3055
  conversationId: payload2.detail.id,
3054
3056
  messages: (payload2.detail.messages ?? []).map(toMessage),
3055
3057
  answering: !!payload2.detail.answering,
3058
+ live: null,
3056
3059
  notice: payload2.detail.answering ? { kind: "busy", what: "thinking" } : null
3057
3060
  }
3058
3061
  ),
@@ -3071,16 +3074,23 @@ var chatReducer = reducer(
3071
3074
  on(runEnded, (s9) => ({
3072
3075
  ...s9,
3073
3076
  answering: false,
3077
+ live: null,
3074
3078
  notice: s9.notice?.kind === "busy" ? null : s9.notice
3075
3079
  })),
3076
- on(stop, (s9) => ({ ...s9, answering: false, notice: { kind: "info", what: "stopped" } }))
3080
+ on(stop, (s9) => ({ ...s9, answering: false, live: null, notice: { kind: "info", what: "stopped" } }))
3077
3081
  );
3078
3082
  function withEvent(s9, event) {
3079
3083
  const conversationId = s9.conversationId ?? event.conversationId;
3080
3084
  if (event.conversationId !== conversationId) return s9;
3081
3085
  if (event.type === "error") {
3082
- return { ...s9, conversationId, notice: { kind: "error", code: event.data?.code ?? "internal" } };
3086
+ return {
3087
+ ...s9,
3088
+ conversationId,
3089
+ live: null,
3090
+ notice: { kind: "error", code: event.data?.code ?? "internal" }
3091
+ };
3083
3092
  }
3093
+ if (event.type === "delta") return withDelta({ ...s9, conversationId }, event);
3084
3094
  if (event.type !== "message" || !event.data?.id) return { ...s9, conversationId };
3085
3095
  const message = toMessage(event.data);
3086
3096
  if (s9.messages.some((m4) => m4.id === message.id)) return { ...s9, conversationId };
@@ -3088,7 +3098,17 @@ function withEvent(s9, event) {
3088
3098
  const local = message.role === "user" ? messages.findIndex((m4) => LOCAL_ID.test(m4.id) && m4.text === message.text) : -1;
3089
3099
  if (local >= 0) messages[local] = message;
3090
3100
  else messages.push(message);
3091
- return { ...s9, conversationId, messages };
3101
+ const live = message.role === "assistant" ? null : s9.live;
3102
+ return { ...s9, conversationId, messages, live };
3103
+ }
3104
+ function withDelta(s9, event) {
3105
+ if (!s9.answering || !event.runId) return s9;
3106
+ const segment = Number(event.data?.segment) || 0;
3107
+ const text = typeof event.data?.text === "string" ? event.data.text : "";
3108
+ const current = s9.live?.runId === event.runId ? s9.live : null;
3109
+ if (current && segment < current.segment) return s9;
3110
+ if (current && segment === current.segment && text.length < current.text.length) return s9;
3111
+ return { ...s9, live: { runId: event.runId, segment, text } };
3092
3112
  }
3093
3113
 
3094
3114
  // src/store/chat-store.ts
@@ -3317,6 +3337,9 @@ header select {
3317
3337
  .msg pre { margin: 6px 0; padding: 8px; overflow-x: auto; border-radius: 8px; background: rgba(0,0,0,0.06); font-size: 13px; }
3318
3338
  .msg code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
3319
3339
  .msg a { color: inherit; }
3340
+ .msg.live > :last-child::after { content: '\\258D'; margin-left: 1px; opacity: 0.6; animation: lf-caret 1s steps(1) infinite; }
3341
+ @keyframes lf-caret { 50% { opacity: 0; } }
3342
+ @media (prefers-reduced-motion: reduce) { .msg.live > :last-child::after { animation: none; } }
3320
3343
  .tools { font-size: 12px; color: var(--_muted); margin-top: 4px; }
3321
3344
 
3322
3345
  .welcome { color: var(--_muted); }
@@ -3467,7 +3490,8 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3467
3490
  this.turnstileSitekey = "";
3468
3491
  this.turnstileUrl = "";
3469
3492
  this.strings = {};
3470
- this.lastCount = 0;
3493
+ /** messages + live text length: scroll when either grows */
3494
+ this.lastSize = "";
3471
3495
  }
3472
3496
  get shown() {
3473
3497
  return this.mode !== "drawer" || this.open;
@@ -3654,10 +3678,14 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3654
3678
  const drawer = this.mode === "drawer";
3655
3679
  const agent = state.agents.find((a5) => a5.id === state.agentId) ?? null;
3656
3680
  const title = this.heading || agent?.name || t9.title;
3657
- const notice = state.notice;
3658
- if (state.messages.length !== this.lastCount) {
3659
- this.lastCount = state.messages.length;
3660
- this.updateComplete.then(() => requestAnimationFrame(() => this.scrollDown()));
3681
+ const live = state.live?.text ? state.live : null;
3682
+ const notice = live && state.notice?.kind === "busy" ? null : state.notice;
3683
+ const size = `${state.messages.length}:${live?.text.length ?? 0}`;
3684
+ if (size !== this.lastSize) {
3685
+ const newMessage = size.split(":")[0] !== this.lastSize.split(":")[0];
3686
+ this.lastSize = size;
3687
+ const follow = newMessage || this.atBottom();
3688
+ if (follow) this.updateComplete.then(() => requestAnimationFrame(() => this.scrollDown()));
3661
3689
  }
3662
3690
  return import_lit_html19.html`
3663
3691
  <button
@@ -3701,8 +3729,16 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3701
3729
  ${icon(ICON_CLOSE)}
3702
3730
  </button>
3703
3731
  </header>
3704
- <div class="messages" part="messages" role="log" aria-live="polite" aria-relevant="additions">
3732
+ <div
3733
+ class="messages"
3734
+ part="messages"
3735
+ role="log"
3736
+ aria-live="polite"
3737
+ aria-relevant="additions"
3738
+ aria-busy=${String(!!live)}
3739
+ >
3705
3740
  ${state.messages.length ? state.messages.map((m4) => this.bubble(m4)) : this.intro(state)}
3741
+ ${live ? import_lit_html19.html`<div class="msg assistant live" part="message assistant live">${renderMarkdown(live.text)}</div>` : ""}
3706
3742
  </div>
3707
3743
  <div class="turnstile" ?hidden=${notice?.kind !== "busy" || notice.what !== "verifying"}></div>
3708
3744
  <div
@@ -3748,6 +3784,10 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3748
3784
  </div>` : ""}
3749
3785
  `;
3750
3786
  }
3787
+ atBottom() {
3788
+ const m4 = this.shadowRoot?.querySelector(".messages");
3789
+ return !m4 || m4.scrollHeight - m4.scrollTop - m4.clientHeight < 80;
3790
+ }
3751
3791
  scrollDown() {
3752
3792
  const m4 = this.shadowRoot?.querySelector(".messages");
3753
3793
  if (m4) m4.scrollTop = m4.scrollHeight;