@lambforge/agent-chat 0.0.7 → 0.0.8

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
@@ -2440,6 +2440,7 @@ __export(index_exports, {
2440
2440
  agentEvent: () => agentEvent,
2441
2441
  agentsLoaded: () => agentsLoaded,
2442
2442
  chatReducer: () => chatReducer,
2443
+ closeComment: () => closeComment,
2443
2444
  connecting: () => connecting,
2444
2445
  errorCode: () => errorCode,
2445
2446
  failed: () => failed,
@@ -2449,6 +2450,8 @@ __export(index_exports, {
2449
2450
  newConversation: () => newConversation,
2450
2451
  pickAgent: () => pickAgent,
2451
2452
  pickAgents: () => pickAgents,
2453
+ rate: () => rate,
2454
+ rateFailed: () => rateFailed,
2452
2455
  removeFile: () => removeFile,
2453
2456
  renderMarkdown: () => renderMarkdown,
2454
2457
  resume: () => resume,
@@ -2675,6 +2678,12 @@ var resume = action(
2675
2678
  var agentEvent = action("[chat] event", payload());
2676
2679
  var runEnded = action("[chat] run ended");
2677
2680
  var stop = action("[chat] stop", payload());
2681
+ var rate = action("[chat] rate", payload());
2682
+ var rateFailed = action(
2683
+ "[chat] rate failed",
2684
+ payload()
2685
+ );
2686
+ var closeComment = action("[chat] close comment");
2678
2687
 
2679
2688
  // ../../node_modules/@rxdi/redux-store/dist/esm/interfaces.js
2680
2689
  var FlattenOperator;
@@ -3013,6 +3022,20 @@ var ChatEffects = class {
3013
3022
  })
3014
3023
  )
3015
3024
  );
3025
+ /** Send a rating; a refusal puts the earlier one back. */
3026
+ this.rate$ = createEffect()(
3027
+ (action$) => action$.pipe(
3028
+ ofType(rate),
3029
+ (0, import_operators7.mergeMap)(({ payload: payload2 }) => {
3030
+ const { agentId, conversationId, messageId, rating, comment, previous } = payload2;
3031
+ if (!this.ctx.client) return import_rxjs6.EMPTY;
3032
+ return this.conversation(agentId, conversationId).rate(messageId, rating, comment).pipe(
3033
+ (0, import_operators7.ignoreElements)(),
3034
+ (0, import_operators7.catchError)(() => (0, import_rxjs6.of)(rateFailed({ messageId, previous })))
3035
+ );
3036
+ })
3037
+ )
3038
+ );
3016
3039
  /** Failures also reach the page as an `error` event. */
3017
3040
  this.failures$ = createEffect()(
3018
3041
  (action$) => action$.pipe(
@@ -3055,7 +3078,8 @@ var initialChatState = {
3055
3078
  answering: false,
3056
3079
  live: null,
3057
3080
  notice: null,
3058
- files: []
3081
+ files: [],
3082
+ commentFor: null
3059
3083
  };
3060
3084
  function toMessage(m4) {
3061
3085
  const tools = (m4.toolCalls ?? []).map((c9) => c9?.name).filter((n11) => !!n11);
@@ -3064,7 +3088,8 @@ function toMessage(m4) {
3064
3088
  role: m4.role,
3065
3089
  text: m4.text ?? "",
3066
3090
  tools: Array.from(new Set(tools)),
3067
- attachments: (m4.attachments ?? []).map((a5) => a5.name)
3091
+ attachments: (m4.attachments ?? []).map((a5) => a5.name),
3092
+ feedback: m4.feedback?.rating ?? null
3068
3093
  };
3069
3094
  }
3070
3095
 
@@ -3076,7 +3101,8 @@ var empty = {
3076
3101
  answering: false,
3077
3102
  live: null,
3078
3103
  notice: null,
3079
- files: []
3104
+ files: [],
3105
+ commentFor: null
3080
3106
  };
3081
3107
  var chatReducer = reducer(
3082
3108
  initialChatState,
@@ -3121,7 +3147,8 @@ var chatReducer = reducer(
3121
3147
  role: "user",
3122
3148
  text: payload2.text,
3123
3149
  tools: [],
3124
- attachments: (payload2.files ?? []).map((f4) => f4.name)
3150
+ attachments: (payload2.files ?? []).map((f4) => f4.name),
3151
+ feedback: null
3125
3152
  }
3126
3153
  ],
3127
3154
  files: [],
@@ -3148,6 +3175,22 @@ var chatReducer = reducer(
3148
3175
  live: null,
3149
3176
  notice: s9.notice?.kind === "busy" ? null : s9.notice
3150
3177
  })),
3178
+ on(rate, (s9, { payload: payload2 }) => ({
3179
+ ...s9,
3180
+ messages: s9.messages.map(
3181
+ (m4) => m4.id === payload2.messageId ? { ...m4, feedback: payload2.rating } : m4
3182
+ ),
3183
+ // a fresh thumbs down asks what was wrong; a comment or anything else closes it
3184
+ commentFor: payload2.rating === "down" && payload2.comment === void 0 && payload2.previous !== "down" ? payload2.messageId : null
3185
+ })),
3186
+ on(rateFailed, (s9, { payload: payload2 }) => ({
3187
+ ...s9,
3188
+ messages: s9.messages.map(
3189
+ (m4) => m4.id === payload2.messageId ? { ...m4, feedback: payload2.previous } : m4
3190
+ ),
3191
+ commentFor: s9.commentFor === payload2.messageId ? null : s9.commentFor
3192
+ })),
3193
+ on(closeComment, (s9) => ({ ...s9, commentFor: null })),
3151
3194
  on(stop, (s9) => ({ ...s9, answering: false, live: null, notice: { kind: "info", what: "stopped" } }))
3152
3195
  );
3153
3196
  function withEvent(s9, event) {
@@ -3199,6 +3242,7 @@ var ChatStore = class {
3199
3242
  effects.select$,
3200
3243
  effects.run$,
3201
3244
  effects.abort$,
3245
+ effects.rate$,
3202
3246
  effects.failures$,
3203
3247
  effects.connect$
3204
3248
  ),
@@ -3233,6 +3277,12 @@ var en = {
3233
3277
  attached: "Attached files",
3234
3278
  removeFile: "Remove",
3235
3279
  filesOnly: "Please look at the attached files.",
3280
+ helpful: "Helpful",
3281
+ notHelpful: "Not helpful",
3282
+ whatWentWrong: "What was wrong? (optional)",
3283
+ sendFeedback: "Send",
3284
+ skipFeedback: "Skip",
3285
+ thanks: "Thanks for the feedback",
3236
3286
  errors: {
3237
3287
  "limit-reached": "This conversation is full - start a new one.",
3238
3288
  "cap-reached": "The assistant is not available right now - try again later.",
@@ -3271,6 +3321,12 @@ var bg = {
3271
3321
  attached: "\u041F\u0440\u0438\u043A\u0430\u0447\u0435\u043D\u0438 \u0444\u0430\u0439\u043B\u043E\u0432\u0435",
3272
3322
  removeFile: "\u041F\u0440\u0435\u043C\u0430\u0445\u043D\u0438",
3273
3323
  filesOnly: "\u041C\u043E\u043B\u044F, \u0440\u0430\u0437\u0433\u043B\u0435\u0434\u0430\u0439\u0442\u0435 \u043F\u0440\u0438\u043A\u0430\u0447\u0435\u043D\u0438\u0442\u0435 \u0444\u0430\u0439\u043B\u043E\u0432\u0435.",
3324
+ helpful: "\u041F\u043E\u043B\u0435\u0437\u043D\u043E",
3325
+ notHelpful: "\u041D\u0435 \u0435 \u043F\u043E\u043B\u0435\u0437\u043D\u043E",
3326
+ whatWentWrong: "\u041A\u0430\u043A\u0432\u043E \u043D\u0435 \u0431\u0435\u0448\u0435 \u043D\u0430\u0440\u0435\u0434? (\u043F\u043E \u0436\u0435\u043B\u0430\u043D\u0438\u0435)",
3327
+ sendFeedback: "\u0418\u0437\u043F\u0440\u0430\u0442\u0438",
3328
+ skipFeedback: "\u041F\u0440\u043E\u043F\u0443\u0441\u043D\u0438",
3329
+ thanks: "\u0411\u043B\u0430\u0433\u043E\u0434\u0430\u0440\u0438\u043C \u0437\u0430 \u043E\u0442\u0437\u0438\u0432\u0430",
3274
3330
  errors: {
3275
3331
  "limit-reached": "\u0420\u0430\u0437\u0433\u043E\u0432\u043E\u0440\u044A\u0442 \u0435 \u043F\u044A\u043B\u0435\u043D - \u0437\u0430\u043F\u043E\u0447\u043D\u0435\u0442\u0435 \u043D\u043E\u0432.",
3276
3332
  "cap-reached": "\u0410\u0441\u0438\u0441\u0442\u0435\u043D\u0442\u044A\u0442 \u043D\u0435 \u0435 \u0434\u043E\u0441\u0442\u044A\u043F\u0435\u043D \u0432 \u043C\u043E\u043C\u0435\u043D\u0442\u0430 - \u043E\u043F\u0438\u0442\u0430\u0439\u0442\u0435 \u043F\u043E-\u043A\u044A\u0441\u043D\u043E.",
@@ -3434,6 +3490,26 @@ header select {
3434
3490
  @keyframes lf-caret { 50% { opacity: 0; } }
3435
3491
  @media (prefers-reduced-motion: reduce) { .msg.live > :last-child::after { animation: none; } }
3436
3492
  .tools { font-size: 12px; color: var(--_muted); margin-top: 4px; }
3493
+ .rate { display: flex; align-items: center; gap: 2px; margin-top: -6px; }
3494
+ .thumb {
3495
+ border: 0; background: transparent; color: var(--_muted);
3496
+ width: 26px; height: 26px; border-radius: 6px; display: grid; place-items: center; cursor: pointer;
3497
+ }
3498
+ .thumb svg { width: 14px; height: 14px; }
3499
+ .thumb:hover, .thumb:focus-visible { background: var(--_bot-bg); color: var(--_fg); }
3500
+ .thumb.on { color: var(--_accent); }
3501
+ .thanks { font-size: 12px; color: var(--_muted); margin-left: 4px; }
3502
+ .comment { display: flex; gap: 6px; align-items: center; max-width: 85%; }
3503
+ .comment input {
3504
+ flex: 1; min-width: 0; font: inherit; font-size: 13px; color: var(--_fg); background: var(--_bg);
3505
+ border: 1px solid var(--_border); border-radius: 8px; padding: 6px 8px;
3506
+ }
3507
+ .comment button {
3508
+ border: 1px solid var(--_border); background: transparent; color: var(--_fg);
3509
+ border-radius: 8px; padding: 5px 9px; font-size: 13px; cursor: pointer;
3510
+ }
3511
+ .comment button[type='submit'] { border-color: var(--_accent); color: var(--_accent); }
3512
+ .comment .skip { color: var(--_muted); }
3437
3513
 
3438
3514
  .welcome { color: var(--_muted); }
3439
3515
  .suggestions { display: flex; flex-wrap: wrap; gap: 6px; }
@@ -3585,6 +3661,8 @@ var TAG = "lambforge-agent-chat";
3585
3661
  var ICON_CHAT = import_lit_html19.svg`<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>`;
3586
3662
  var ICON_CLOSE = import_lit_html19.svg`<path d="M18 6 6 18M6 6l12 12"/>`;
3587
3663
  var ICON_NEW = import_lit_html19.svg`<path d="M12 5v14M5 12h14"/>`;
3664
+ var ICON_UP = import_lit_html19.svg`<path d="M7 10v12M15 5.88 14 10h5.83a2 2 0 0 1 1.92 2.56l-2.33 8A2 2 0 0 1 17.5 22H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.76a2 2 0 0 0 1.79-1.11L12 2a3.13 3.13 0 0 1 3 3.88Z"/>`;
3665
+ var ICON_DOWN = import_lit_html19.svg`<path d="M17 14V2M9 18.12 10 14H4.17a2 2 0 0 1-1.92-2.56l2.33-8A2 2 0 0 1 6.5 2H20a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-2.76a2 2 0 0 0-1.79 1.11L12 22a3.13 3.13 0 0 1-3-3.88Z"/>`;
3588
3666
  var ICON_ATTACH = import_lit_html19.svg`<path d="m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l8.57-8.57A4 4 0 1 1 18 8.84l-8.59 8.57a2 2 0 0 1-2.83-2.83l8.49-8.48"/>`;
3589
3667
  var ACCEPT = [...import_agents2.ACCEPTED_ATTACHMENT_TYPES, ".md", ".csv", ".yaml", ".yml", ".json", ".txt"].join(",");
3590
3668
  var icon = (paths) => import_lit_html19.html`<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${paths}</svg>`;
@@ -3873,7 +3951,7 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3873
3951
  aria-relevant="additions"
3874
3952
  aria-busy=${String(!!live)}
3875
3953
  >
3876
- ${state.messages.length ? state.messages.map((m4) => this.bubble(m4)) : this.intro(state)}
3954
+ ${state.messages.length ? state.messages.map((m4) => this.bubble(m4, state)) : this.intro(state)}
3877
3955
  ${live ? import_lit_html19.html`<div class="msg assistant live" part="message assistant live">${renderMarkdown(live.text)}</div>` : ""}
3878
3956
  </div>
3879
3957
  <div class="turnstile" ?hidden=${notice?.kind !== "busy" || notice.what !== "verifying"}></div>
@@ -3933,14 +4011,67 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3933
4011
  </section>
3934
4012
  `;
3935
4013
  }
3936
- bubble(m4) {
4014
+ /**
4015
+ * Rate an answer: the same thumb again clears it. Only stored answers of
4016
+ * an agent that takes ratings (plan 51 §15 #7).
4017
+ */
4018
+ rateAnswer(m4, rating, comment) {
4019
+ return ([state]) => {
4020
+ if (!state.agentId || !state.conversationId) return;
4021
+ const next = comment === void 0 && m4.feedback === rating ? null : rating;
4022
+ this.dispatch(
4023
+ rate({
4024
+ agentId: state.agentId,
4025
+ conversationId: state.conversationId,
4026
+ messageId: m4.id,
4027
+ rating: next,
4028
+ ...comment !== void 0 ? { comment } : {},
4029
+ previous: m4.feedback
4030
+ })
4031
+ );
4032
+ };
4033
+ }
4034
+ sendComment(ev, m4, state) {
4035
+ ev.preventDefault();
4036
+ const input = ev.target.querySelector("input");
4037
+ const text = input?.value.trim() ?? "";
4038
+ if (!text) {
4039
+ this.dispatch(closeComment());
4040
+ return;
4041
+ }
4042
+ this.rateAnswer(m4, "down", text)([state]);
4043
+ }
4044
+ feedback(m4, state) {
4045
+ const agent = state.agents.find((a5) => a5.id === state.agentId);
4046
+ if (agent?.allowFeedback === false || m4.id.startsWith("local:") || !state.conversationId) return "";
4047
+ const t9 = this.t;
4048
+ return import_lit_html19.html`
4049
+ <div class="rate" part="rate">
4050
+ <button type="button" class="thumb ${m4.feedback === "up" ? "on" : ""}" part="thumb-up"
4051
+ title=${t9.helpful} aria-label=${t9.helpful} aria-pressed=${m4.feedback === "up" ? "true" : "false"}
4052
+ @click=${() => this.rateAnswer(m4, "up")([state])}>${icon(ICON_UP)}</button>
4053
+ <button type="button" class="thumb ${m4.feedback === "down" ? "on" : ""}" part="thumb-down"
4054
+ title=${t9.notHelpful} aria-label=${t9.notHelpful} aria-pressed=${m4.feedback === "down" ? "true" : "false"}
4055
+ @click=${() => this.rateAnswer(m4, "down")([state])}>${icon(ICON_DOWN)}</button>
4056
+ ${m4.feedback && state.commentFor !== m4.id ? import_lit_html19.html`<span class="thanks" aria-live="polite">${t9.thanks}</span>` : ""}
4057
+ </div>
4058
+ ${state.commentFor === m4.id ? import_lit_html19.html`<form class="comment" part="feedback-comment" @submit=${(ev) => this.sendComment(ev, m4, state)}>
4059
+ <input type="text" maxlength="1000" placeholder=${t9.whatWentWrong} aria-label=${t9.whatWentWrong}
4060
+ @keydown=${(ev) => ev.key === "Escape" && this.dispatch(closeComment())} />
4061
+ <button type="submit">${t9.sendFeedback}</button>
4062
+ <button type="button" class="skip" @click=${() => this.dispatch(closeComment())}>${t9.skipFeedback}</button>
4063
+ </form>` : ""}
4064
+ `;
4065
+ }
4066
+ bubble(m4, state) {
3937
4067
  if (m4.role === "user") {
3938
4068
  return import_lit_html19.html`<div class="msg user" part="message user">${m4.text}${m4.attachments?.length ? import_lit_html19.html`<div class="msg-files">${m4.attachments.map((name) => import_lit_html19.html`<span class="msg-file">${name}</span>`)}</div>` : ""}</div>`;
3939
4069
  }
3940
4070
  return import_lit_html19.html`<div class="msg assistant" part="message assistant">
3941
4071
  ${renderMarkdown(m4.text)}
3942
4072
  ${m4.tools.length ? import_lit_html19.html`<div class="tools">${this.t.usedTools}: ${m4.tools.join(", ")}</div>` : ""}
3943
- </div>`;
4073
+ </div>
4074
+ ${this.feedback(m4, state)}`;
3944
4075
  }
3945
4076
  intro(state) {
3946
4077
  const agent = state.agents.find((a5) => a5.id === state.agentId);