@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.cjs CHANGED
@@ -3038,6 +3038,7 @@ var initialChatState = {
3038
3038
  conversationId: null,
3039
3039
  messages: [],
3040
3040
  answering: false,
3041
+ live: null,
3041
3042
  notice: null
3042
3043
  };
3043
3044
  function toMessage(m4) {
@@ -3047,7 +3048,7 @@ function toMessage(m4) {
3047
3048
 
3048
3049
  // src/store/chat-reducer.ts
3049
3050
  var LOCAL_ID = /^local:/;
3050
- var empty = { conversationId: null, messages: [], answering: false, notice: null };
3051
+ var empty = { conversationId: null, messages: [], answering: false, live: null, notice: null };
3051
3052
  var chatReducer = reducer(
3052
3053
  initialChatState,
3053
3054
  on(shown, (s9, { payload: payload2 }) => s9.shown || !payload2 ? s9 : { ...s9, shown: true }),
@@ -3055,6 +3056,7 @@ var chatReducer = reducer(
3055
3056
  on(failed, (s9, { payload: payload2 }) => ({
3056
3057
  ...s9,
3057
3058
  answering: false,
3059
+ live: null,
3058
3060
  notice: { kind: "error", code: payload2 }
3059
3061
  })),
3060
3062
  on(verifying, (s9, { payload: payload2 }) => ({
@@ -3075,6 +3077,7 @@ var chatReducer = reducer(
3075
3077
  conversationId: payload2.detail.id,
3076
3078
  messages: (payload2.detail.messages ?? []).map(toMessage),
3077
3079
  answering: !!payload2.detail.answering,
3080
+ live: null,
3078
3081
  notice: payload2.detail.answering ? { kind: "busy", what: "thinking" } : null
3079
3082
  }
3080
3083
  ),
@@ -3093,16 +3096,23 @@ var chatReducer = reducer(
3093
3096
  on(runEnded, (s9) => ({
3094
3097
  ...s9,
3095
3098
  answering: false,
3099
+ live: null,
3096
3100
  notice: s9.notice?.kind === "busy" ? null : s9.notice
3097
3101
  })),
3098
- on(stop, (s9) => ({ ...s9, answering: false, notice: { kind: "info", what: "stopped" } }))
3102
+ on(stop, (s9) => ({ ...s9, answering: false, live: null, notice: { kind: "info", what: "stopped" } }))
3099
3103
  );
3100
3104
  function withEvent(s9, event) {
3101
3105
  const conversationId = s9.conversationId ?? event.conversationId;
3102
3106
  if (event.conversationId !== conversationId) return s9;
3103
3107
  if (event.type === "error") {
3104
- return { ...s9, conversationId, notice: { kind: "error", code: event.data?.code ?? "internal" } };
3108
+ return {
3109
+ ...s9,
3110
+ conversationId,
3111
+ live: null,
3112
+ notice: { kind: "error", code: event.data?.code ?? "internal" }
3113
+ };
3105
3114
  }
3115
+ if (event.type === "delta") return withDelta({ ...s9, conversationId }, event);
3106
3116
  if (event.type !== "message" || !event.data?.id) return { ...s9, conversationId };
3107
3117
  const message = toMessage(event.data);
3108
3118
  if (s9.messages.some((m4) => m4.id === message.id)) return { ...s9, conversationId };
@@ -3110,7 +3120,17 @@ function withEvent(s9, event) {
3110
3120
  const local = message.role === "user" ? messages.findIndex((m4) => LOCAL_ID.test(m4.id) && m4.text === message.text) : -1;
3111
3121
  if (local >= 0) messages[local] = message;
3112
3122
  else messages.push(message);
3113
- return { ...s9, conversationId, messages };
3123
+ const live = message.role === "assistant" ? null : s9.live;
3124
+ return { ...s9, conversationId, messages, live };
3125
+ }
3126
+ function withDelta(s9, event) {
3127
+ if (!s9.answering || !event.runId) return s9;
3128
+ const segment = Number(event.data?.segment) || 0;
3129
+ const text = typeof event.data?.text === "string" ? event.data.text : "";
3130
+ const current = s9.live?.runId === event.runId ? s9.live : null;
3131
+ if (current && segment < current.segment) return s9;
3132
+ if (current && segment === current.segment && text.length < current.text.length) return s9;
3133
+ return { ...s9, live: { runId: event.runId, segment, text } };
3114
3134
  }
3115
3135
 
3116
3136
  // src/store/chat-store.ts
@@ -3339,6 +3359,9 @@ header select {
3339
3359
  .msg pre { margin: 6px 0; padding: 8px; overflow-x: auto; border-radius: 8px; background: rgba(0,0,0,0.06); font-size: 13px; }
3340
3360
  .msg code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
3341
3361
  .msg a { color: inherit; }
3362
+ .msg.live > :last-child::after { content: '\\258D'; margin-left: 1px; opacity: 0.6; animation: lf-caret 1s steps(1) infinite; }
3363
+ @keyframes lf-caret { 50% { opacity: 0; } }
3364
+ @media (prefers-reduced-motion: reduce) { .msg.live > :last-child::after { animation: none; } }
3342
3365
  .tools { font-size: 12px; color: var(--_muted); margin-top: 4px; }
3343
3366
 
3344
3367
  .welcome { color: var(--_muted); }
@@ -3489,7 +3512,8 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3489
3512
  this.turnstileSitekey = "";
3490
3513
  this.turnstileUrl = "";
3491
3514
  this.strings = {};
3492
- this.lastCount = 0;
3515
+ /** messages + live text length: scroll when either grows */
3516
+ this.lastSize = "";
3493
3517
  }
3494
3518
  get shown() {
3495
3519
  return this.mode !== "drawer" || this.open;
@@ -3676,10 +3700,14 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3676
3700
  const drawer = this.mode === "drawer";
3677
3701
  const agent = state.agents.find((a5) => a5.id === state.agentId) ?? null;
3678
3702
  const title = this.heading || agent?.name || t9.title;
3679
- const notice = state.notice;
3680
- if (state.messages.length !== this.lastCount) {
3681
- this.lastCount = state.messages.length;
3682
- this.updateComplete.then(() => requestAnimationFrame(() => this.scrollDown()));
3703
+ const live = state.live?.text ? state.live : null;
3704
+ const notice = live && state.notice?.kind === "busy" ? null : state.notice;
3705
+ const size = `${state.messages.length}:${live?.text.length ?? 0}`;
3706
+ if (size !== this.lastSize) {
3707
+ const newMessage = size.split(":")[0] !== this.lastSize.split(":")[0];
3708
+ this.lastSize = size;
3709
+ const follow = newMessage || this.atBottom();
3710
+ if (follow) this.updateComplete.then(() => requestAnimationFrame(() => this.scrollDown()));
3683
3711
  }
3684
3712
  return import_lit_html19.html`
3685
3713
  <button
@@ -3723,8 +3751,16 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3723
3751
  ${icon(ICON_CLOSE)}
3724
3752
  </button>
3725
3753
  </header>
3726
- <div class="messages" part="messages" role="log" aria-live="polite" aria-relevant="additions">
3754
+ <div
3755
+ class="messages"
3756
+ part="messages"
3757
+ role="log"
3758
+ aria-live="polite"
3759
+ aria-relevant="additions"
3760
+ aria-busy=${String(!!live)}
3761
+ >
3727
3762
  ${state.messages.length ? state.messages.map((m4) => this.bubble(m4)) : this.intro(state)}
3763
+ ${live ? import_lit_html19.html`<div class="msg assistant live" part="message assistant live">${renderMarkdown(live.text)}</div>` : ""}
3728
3764
  </div>
3729
3765
  <div class="turnstile" ?hidden=${notice?.kind !== "busy" || notice.what !== "verifying"}></div>
3730
3766
  <div
@@ -3770,6 +3806,10 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3770
3806
  </div>` : ""}
3771
3807
  `;
3772
3808
  }
3809
+ atBottom() {
3810
+ const m4 = this.shadowRoot?.querySelector(".messages");
3811
+ return !m4 || m4.scrollHeight - m4.scrollTop - m4.clientHeight < 80;
3812
+ }
3773
3813
  scrollDown() {
3774
3814
  const m4 = this.shadowRoot?.querySelector(".messages");
3775
3815
  if (m4) m4.scrollTop = m4.scrollHeight;