@lambforge/agent-chat 0.0.6 → 0.0.7

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
@@ -2436,6 +2436,7 @@ __export(index_exports, {
2436
2436
  LOCALES: () => LOCALES,
2437
2437
  LambforgeAgentChat: () => LambforgeAgentChat,
2438
2438
  TAG: () => TAG,
2439
+ addFiles: () => addFiles,
2439
2440
  agentEvent: () => agentEvent,
2440
2441
  agentsLoaded: () => agentsLoaded,
2441
2442
  chatReducer: () => chatReducer,
@@ -2448,6 +2449,7 @@ __export(index_exports, {
2448
2449
  newConversation: () => newConversation,
2449
2450
  pickAgent: () => pickAgent,
2450
2451
  pickAgents: () => pickAgents,
2452
+ removeFile: () => removeFile,
2451
2453
  renderMarkdown: () => renderMarkdown,
2452
2454
  resume: () => resume,
2453
2455
  runEnded: () => runEnded,
@@ -2462,7 +2464,7 @@ __export(index_exports, {
2462
2464
  module.exports = __toCommonJS(index_exports);
2463
2465
 
2464
2466
  // src/element.ts
2465
- var import_agents = require("@lambforge/agents");
2467
+ var import_agents2 = require("@lambforge/agents");
2466
2468
  var import_component = __toESM(require_dist3());
2467
2469
  var import_lit_html19 = __toESM(require_dist());
2468
2470
  var import_rxjs8 = require("rxjs");
@@ -2660,7 +2662,12 @@ var historyLoaded = action(
2660
2662
  payload()
2661
2663
  );
2662
2664
  var newConversation = action("[chat] new conversation");
2663
- var sendMessage = action("[chat] send", payload());
2665
+ var sendMessage = action(
2666
+ "[chat] send",
2667
+ payload()
2668
+ );
2669
+ var addFiles = action("[chat] add files", payload());
2670
+ var removeFile = action("[chat] remove file", payload());
2664
2671
  var resume = action(
2665
2672
  "[chat] resume",
2666
2673
  payload()
@@ -2965,9 +2972,14 @@ var ChatEffects = class {
2965
2972
  action$.pipe(
2966
2973
  ofType(sendMessage),
2967
2974
  (0, import_operators7.map)(({ payload: payload2 }) => {
2968
- const { agentId, conversationId, text } = payload2;
2975
+ const { agentId, conversationId, text, files } = payload2;
2969
2976
  const c9 = this.conversation(agentId, conversationId);
2970
- return { c: c9, events: c9.send(text) };
2977
+ const attachments = (files ?? []).map((file) => ({
2978
+ name: file.name,
2979
+ mimeType: file.type || void 0,
2980
+ data: file
2981
+ }));
2982
+ return { c: c9, events: attachments.length ? c9.send(text, { attachments }) : c9.send(text) };
2971
2983
  })
2972
2984
  ),
2973
2985
  action$.pipe(
@@ -3030,6 +3042,9 @@ var ChatEffects = class {
3030
3042
  }
3031
3043
  };
3032
3044
 
3045
+ // src/store/chat-reducer.ts
3046
+ var import_agents = require("@lambforge/agents");
3047
+
3033
3048
  // src/store/chat-state.ts
3034
3049
  var initialChatState = {
3035
3050
  shown: false,
@@ -3039,16 +3054,30 @@ var initialChatState = {
3039
3054
  messages: [],
3040
3055
  answering: false,
3041
3056
  live: null,
3042
- notice: null
3057
+ notice: null,
3058
+ files: []
3043
3059
  };
3044
3060
  function toMessage(m4) {
3045
3061
  const tools = (m4.toolCalls ?? []).map((c9) => c9?.name).filter((n11) => !!n11);
3046
- return { id: m4.id, role: m4.role, text: m4.text ?? "", tools: Array.from(new Set(tools)) };
3062
+ return {
3063
+ id: m4.id,
3064
+ role: m4.role,
3065
+ text: m4.text ?? "",
3066
+ tools: Array.from(new Set(tools)),
3067
+ attachments: (m4.attachments ?? []).map((a5) => a5.name)
3068
+ };
3047
3069
  }
3048
3070
 
3049
3071
  // src/store/chat-reducer.ts
3050
3072
  var LOCAL_ID = /^local:/;
3051
- var empty = { conversationId: null, messages: [], answering: false, live: null, notice: null };
3073
+ var empty = {
3074
+ conversationId: null,
3075
+ messages: [],
3076
+ answering: false,
3077
+ live: null,
3078
+ notice: null,
3079
+ files: []
3080
+ };
3052
3081
  var chatReducer = reducer(
3053
3082
  initialChatState,
3054
3083
  on(shown, (s9, { payload: payload2 }) => s9.shown || !payload2 ? s9 : { ...s9, shown: true }),
@@ -3087,11 +3116,31 @@ var chatReducer = reducer(
3087
3116
  // shown at once; replaced by the stored copy when it arrives
3088
3117
  messages: [
3089
3118
  ...s9.messages,
3090
- { id: `local:${s9.messages.length}`, role: "user", text: payload2.text, tools: [] }
3119
+ {
3120
+ id: `local:${s9.messages.length}`,
3121
+ role: "user",
3122
+ text: payload2.text,
3123
+ tools: [],
3124
+ attachments: (payload2.files ?? []).map((f4) => f4.name)
3125
+ }
3091
3126
  ],
3127
+ files: [],
3092
3128
  answering: true,
3093
3129
  notice: { kind: "busy", what: "thinking" }
3094
3130
  })),
3131
+ on(addFiles, (s9, { payload: payload2 }) => {
3132
+ const agent = s9.agents.find((a5) => a5.id === s9.agentId);
3133
+ if (!agent?.allowAttachments) return { ...s9, notice: { kind: "error", code: "attachments-off" } };
3134
+ const files = [...s9.files, ...payload2];
3135
+ const problem = (0, import_agents.attachmentsProblem)(files);
3136
+ if (problem) return { ...s9, notice: { kind: "error", code: problem } };
3137
+ return { ...s9, files, notice: s9.notice?.kind === "error" ? null : s9.notice };
3138
+ }),
3139
+ on(removeFile, (s9, { payload: payload2 }) => ({
3140
+ ...s9,
3141
+ files: s9.files.filter((_2, i12) => i12 !== payload2),
3142
+ notice: s9.notice?.kind === "error" ? null : s9.notice
3143
+ })),
3095
3144
  on(agentEvent, (s9, { payload: payload2 }) => withEvent(s9, payload2)),
3096
3145
  on(runEnded, (s9) => ({
3097
3146
  ...s9,
@@ -3180,6 +3229,10 @@ var en = {
3180
3229
  pickAgent: "Assistant",
3181
3230
  usedTools: "Used",
3182
3231
  verifying: "Checking that you are human",
3232
+ attach: "Attach files",
3233
+ attached: "Attached files",
3234
+ removeFile: "Remove",
3235
+ filesOnly: "Please look at the attached files.",
3183
3236
  errors: {
3184
3237
  "limit-reached": "This conversation is full - start a new one.",
3185
3238
  "cap-reached": "The assistant is not available right now - try again later.",
@@ -3191,6 +3244,13 @@ var en = {
3191
3244
  unauthorized: "This chat is not allowed here.",
3192
3245
  "bad-request": "That did not work - try again.",
3193
3246
  network: "No connection - try again.",
3247
+ "attachments-off": "This assistant does not take files.",
3248
+ "attachments-unsupported": "This assistant cannot read that kind of file.",
3249
+ "attachment-invalid": "Only images, PDF and text files can be attached.",
3250
+ "attachment-too-large": "That file is too large (images 5 MB, PDF 10 MB, text 1 MB).",
3251
+ "attachment-limit": "At most 5 files and 15 MB in one message.",
3252
+ "attachment-missing": "A file was lost - attach it again.",
3253
+ "attachment-not-uploaded": "A file did not finish uploading - attach it again.",
3194
3254
  default: "Something went wrong - try again."
3195
3255
  }
3196
3256
  };
@@ -3207,6 +3267,10 @@ var bg = {
3207
3267
  pickAgent: "\u0410\u0441\u0438\u0441\u0442\u0435\u043D\u0442",
3208
3268
  usedTools: "\u0418\u0437\u043F\u043E\u043B\u0437\u0432\u0430\u043D\u043E",
3209
3269
  verifying: "\u041F\u0440\u043E\u0432\u0435\u0440\u043A\u0430, \u0447\u0435 \u0441\u0442\u0435 \u0447\u043E\u0432\u0435\u043A",
3270
+ attach: "\u041F\u0440\u0438\u043A\u0430\u0447\u0438 \u0444\u0430\u0439\u043B\u043E\u0432\u0435",
3271
+ attached: "\u041F\u0440\u0438\u043A\u0430\u0447\u0435\u043D\u0438 \u0444\u0430\u0439\u043B\u043E\u0432\u0435",
3272
+ removeFile: "\u041F\u0440\u0435\u043C\u0430\u0445\u043D\u0438",
3273
+ 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.",
3210
3274
  errors: {
3211
3275
  "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.",
3212
3276
  "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.",
@@ -3218,6 +3282,13 @@ var bg = {
3218
3282
  unauthorized: "\u0422\u043E\u0437\u0438 \u0447\u0430\u0442 \u043D\u0435 \u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043D \u0442\u0443\u043A.",
3219
3283
  "bad-request": "\u041D\u0435 \u0441\u0435 \u043F\u043E\u043B\u0443\u0447\u0438 - \u043E\u043F\u0438\u0442\u0430\u0439\u0442\u0435 \u043E\u0442\u043D\u043E\u0432\u043E.",
3220
3284
  network: "\u041D\u044F\u043C\u0430 \u0432\u0440\u044A\u0437\u043A\u0430 - \u043E\u043F\u0438\u0442\u0430\u0439\u0442\u0435 \u043E\u0442\u043D\u043E\u0432\u043E.",
3285
+ "attachments-off": "\u0422\u043E\u0437\u0438 \u0430\u0441\u0438\u0441\u0442\u0435\u043D\u0442 \u043D\u0435 \u043F\u0440\u0438\u0435\u043C\u0430 \u0444\u0430\u0439\u043B\u043E\u0432\u0435.",
3286
+ "attachments-unsupported": "\u0422\u043E\u0437\u0438 \u0430\u0441\u0438\u0441\u0442\u0435\u043D\u0442 \u043D\u0435 \u043C\u043E\u0436\u0435 \u0434\u0430 \u0447\u0435\u0442\u0435 \u0442\u0430\u043A\u044A\u0432 \u0444\u0430\u0439\u043B.",
3287
+ "attachment-invalid": "\u041C\u043E\u0436\u0435 \u0434\u0430 \u043F\u0440\u0438\u043A\u0430\u0447\u0438\u0442\u0435 \u0441\u0430\u043C\u043E \u0438\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F, PDF \u0438 \u0442\u0435\u043A\u0441\u0442\u043E\u0432\u0438 \u0444\u0430\u0439\u043B\u043E\u0432\u0435.",
3288
+ "attachment-too-large": "\u0424\u0430\u0439\u043B\u044A\u0442 \u0435 \u0442\u0432\u044A\u0440\u0434\u0435 \u0433\u043E\u043B\u044F\u043C (\u0438\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F 5 MB, PDF 10 MB, \u0442\u0435\u043A\u0441\u0442 1 MB).",
3289
+ "attachment-limit": "\u041D\u0430\u0439-\u043C\u043D\u043E\u0433\u043E 5 \u0444\u0430\u0439\u043B\u0430 \u0438 15 MB \u0432 \u0435\u0434\u043D\u043E \u0441\u044A\u043E\u0431\u0449\u0435\u043D\u0438\u0435.",
3290
+ "attachment-missing": "\u0424\u0430\u0439\u043B \u0441\u0435 \u0438\u0437\u0433\u0443\u0431\u0438 - \u043F\u0440\u0438\u043A\u0430\u0447\u0435\u0442\u0435 \u0433\u043E \u043E\u0442\u043D\u043E\u0432\u043E.",
3291
+ "attachment-not-uploaded": "\u0424\u0430\u0439\u043B \u043D\u0435 \u0441\u0435 \u043A\u0430\u0447\u0438 \u0434\u043E\u043A\u0440\u0430\u0439 - \u043F\u0440\u0438\u043A\u0430\u0447\u0435\u0442\u0435 \u0433\u043E \u043E\u0442\u043D\u043E\u0432\u043E.",
3221
3292
  default: "\u041D\u0435\u0449\u043E \u0441\u0435 \u043E\u0431\u044A\u0440\u043A\u0430 - \u043E\u043F\u0438\u0442\u0430\u0439\u0442\u0435 \u043E\u0442\u043D\u043E\u0432\u043E."
3222
3293
  }
3223
3294
  };
@@ -3406,6 +3477,26 @@ textarea:focus-visible { outline: 2px solid var(--_accent); outline-offset: 0; b
3406
3477
  padding: 0 14px; height: 40px; font-weight: 600;
3407
3478
  }
3408
3479
  .send:disabled { opacity: 0.5; cursor: default; }
3480
+ .attach { flex: none; height: 40px; }
3481
+ .attach:disabled { opacity: 0.5; cursor: default; }
3482
+ .files {
3483
+ list-style: none; margin: 0; padding: 8px 12px 0; display: flex; flex-wrap: wrap; gap: 6px;
3484
+ border-top: 1px solid var(--_border);
3485
+ }
3486
+ .file {
3487
+ display: inline-flex; align-items: center; gap: 2px; max-width: 100%;
3488
+ border: 1px solid var(--_border); border-radius: 999px; padding: 2px 2px 2px 10px; font-size: 13px;
3489
+ }
3490
+ .file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
3491
+ .file-remove {
3492
+ border: 0; background: transparent; color: var(--_muted); width: 22px; height: 22px;
3493
+ border-radius: 999px; display: grid; place-items: center; padding: 0;
3494
+ }
3495
+ .file-remove:hover, .file-remove:focus-visible { background: var(--_bot-bg); color: var(--_fg); }
3496
+ .file-remove svg { width: 14px; height: 14px; }
3497
+ .files + form { border-top: 0; }
3498
+ .msg-files { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
3499
+ .msg-file { font-size: 12px; padding: 1px 8px; border-radius: 999px; background: rgba(255,255,255,0.2); }
3409
3500
  .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
3410
3501
  `;
3411
3502
 
@@ -3494,6 +3585,8 @@ var TAG = "lambforge-agent-chat";
3494
3585
  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"/>`;
3495
3586
  var ICON_CLOSE = import_lit_html19.svg`<path d="M18 6 6 18M6 6l12 12"/>`;
3496
3587
  var ICON_NEW = import_lit_html19.svg`<path d="M12 5v14M5 12h14"/>`;
3588
+ 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
+ var ACCEPT = [...import_agents2.ACCEPTED_ATTACHMENT_TYPES, ".md", ".csv", ".yaml", ".yml", ".json", ".txt"].join(",");
3497
3590
  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>`;
3498
3591
  var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3499
3592
  constructor() {
@@ -3556,10 +3649,10 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3556
3649
  const url = this.url || void 0;
3557
3650
  if (this.getToken) {
3558
3651
  const source = this.getToken;
3559
- this.client = new import_agents.AgentClient({ url, getToken: () => source() });
3652
+ this.client = new import_agents2.AgentClient({ url, getToken: () => source() });
3560
3653
  } else if (this.tokenUrl) {
3561
3654
  const tokenUrl = this.tokenUrl;
3562
- this.client = new import_agents.AgentClient({
3655
+ this.client = new import_agents2.AgentClient({
3563
3656
  url,
3564
3657
  getToken: () => fetch(tokenUrl, {
3565
3658
  credentials: "include",
@@ -3570,7 +3663,7 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3570
3663
  })
3571
3664
  });
3572
3665
  } else if (this.publishableKey) {
3573
- this.client = new import_agents.AgentClient({
3666
+ this.client = new import_agents2.AgentClient({
3574
3667
  url,
3575
3668
  anonymous: {
3576
3669
  publishableKey: this.publishableKey,
@@ -3603,16 +3696,36 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3603
3696
  throw new Error("turnstile: the chat is not rendered");
3604
3697
  }
3605
3698
  // ------------------------------------------------------------ actions
3606
- /** Send a message to the selected agent. */
3699
+ /** Send a message (with the picked files) to the selected agent. */
3607
3700
  send(text) {
3608
3701
  return ([state]) => {
3609
- const clean = String(text ?? "").trim();
3702
+ const files = state.files;
3703
+ const clean = String(text ?? "").trim() || (files.length ? this.t.filesOnly : "");
3610
3704
  if (!clean || state.answering || !state.agentId) return;
3611
3705
  this.dispatch(
3612
- sendMessage({ agentId: state.agentId, conversationId: state.conversationId, text: clean })
3706
+ sendMessage({
3707
+ agentId: state.agentId,
3708
+ conversationId: state.conversationId,
3709
+ text: clean,
3710
+ ...files.length ? { files } : {}
3711
+ })
3613
3712
  );
3614
3713
  };
3615
3714
  }
3715
+ /** Files for the next message (the reducer refuses what the API would). */
3716
+ attach(files) {
3717
+ const list = Array.from(files ?? []);
3718
+ if (list.length) this.dispatch(addFiles(list));
3719
+ }
3720
+ detach(index) {
3721
+ this.dispatch(removeFile(index));
3722
+ }
3723
+ onPaste(ev, allowed) {
3724
+ const files = Array.from(ev.clipboardData?.files ?? []);
3725
+ if (!allowed || !files.length) return;
3726
+ ev.preventDefault();
3727
+ this.attach(files);
3728
+ }
3616
3729
  /** Stop the answer in progress. */
3617
3730
  stopAnswer() {
3618
3731
  return ([state]) => {
@@ -3700,6 +3813,7 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3700
3813
  const drawer = this.mode === "drawer";
3701
3814
  const agent = state.agents.find((a5) => a5.id === state.agentId) ?? null;
3702
3815
  const title = this.heading || agent?.name || t9.title;
3816
+ const canAttach = !!agent?.allowAttachments;
3703
3817
  const live = state.live?.text ? state.live : null;
3704
3818
  const notice = live && state.notice?.kind === "busy" ? null : state.notice;
3705
3819
  const size = `${state.messages.length}:${live?.text.length ?? 0}`;
@@ -3768,7 +3882,40 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3768
3882
  part="status"
3769
3883
  role="status"
3770
3884
  >${this.noticeText(notice)}</div>
3885
+ ${canAttach && state.files.length ? import_lit_html19.html`<ul class="files" part="files" aria-label=${t9.attached}>
3886
+ ${state.files.map(
3887
+ (f4, i12) => import_lit_html19.html`<li class="file" part="file">
3888
+ <span class="file-name" title=${f4.name}>${f4.name}</span>
3889
+ <button type="button" class="file-remove" aria-label=${`${t9.removeFile}: ${f4.name}`} @click=${() => this.detach(i12)}>
3890
+ ${icon(ICON_CLOSE)}
3891
+ </button>
3892
+ </li>`
3893
+ )}
3894
+ </ul>` : ""}
3771
3895
  <form part="composer" @submit=${(ev) => this.submit(ev, state)}>
3896
+ ${canAttach ? import_lit_html19.html`<input
3897
+ class="file-input"
3898
+ type="file"
3899
+ multiple
3900
+ accept=${ACCEPT}
3901
+ hidden
3902
+ @change=${(ev) => {
3903
+ const input = ev.target;
3904
+ this.attach(input.files);
3905
+ input.value = "";
3906
+ }}
3907
+ />
3908
+ <button
3909
+ class="icon attach"
3910
+ part="attach"
3911
+ type="button"
3912
+ title=${t9.attach}
3913
+ aria-label=${t9.attach}
3914
+ ?disabled=${state.answering}
3915
+ @click=${() => this.shadowRoot?.querySelector(".file-input")?.click()}
3916
+ >
3917
+ ${icon(ICON_ATTACH)}
3918
+ </button>` : ""}
3772
3919
  <label class="sr-only" for="lf-input">${t9.placeholder}</label>
3773
3920
  <textarea
3774
3921
  id="lf-input"
@@ -3777,6 +3924,7 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3777
3924
  placeholder=${t9.placeholder}
3778
3925
  @keydown=${(ev) => this.onInputKey(ev, state)}
3779
3926
  @input=${(ev) => this.autosize(ev.target)}
3927
+ @paste=${(ev) => this.onPaste(ev, canAttach && !state.answering)}
3780
3928
  ></textarea>
3781
3929
  <button class="send" part="send" type="submit" aria-label=${state.answering ? t9.stop : t9.send}>
3782
3930
  ${state.answering ? t9.stop : t9.send}
@@ -3787,7 +3935,7 @@ var LambforgeAgentChat = class extends import_lit_html19.LitElement {
3787
3935
  }
3788
3936
  bubble(m4) {
3789
3937
  if (m4.role === "user") {
3790
- return import_lit_html19.html`<div class="msg user" part="message user">${m4.text}</div>`;
3938
+ 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>`;
3791
3939
  }
3792
3940
  return import_lit_html19.html`<div class="msg assistant" part="message assistant">
3793
3941
  ${renderMarkdown(m4.text)}