vue-agent-input 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 originRing
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,181 @@
1
- # Temporary Holding Version
1
+ # Vue Agent Input
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 面向智能体和聊天产品的 Vue 3 输入框。支持 `@` 智能体选择、`/` 正文标签、自定义候选面板,以及发送和停止事件。正文可以混排文本与可点击标签,使用 `v-model` 保存和回填草稿。
4
+
5
+ ![demo](https://github.com/OriginRing/agent-input/blob/master/demo/assets/input.png)
6
+
7
+ ![demo](https://github.com/OriginRing/agent-input/blob/master/demo/assets/input2.png)
8
+
9
+ ## 安装
10
+
11
+ 需要 Vue 3.5 或更新的 Vue 3 版本。包以 ESM 发布,附带 TypeScript 声明和编译好的 CSS。
12
+
13
+ ```sh
14
+ npm install vue-agent-input
15
+ ```
16
+
17
+ ## 快速开始
18
+
19
+ 从 `vue-agent-input` 导入组件,并在应用中导入一次 `vue-agent-input/style.css`。
20
+
21
+ ```vue
22
+ <script setup lang="ts">
23
+ import { ref } from 'vue'
24
+ import { AgentInput } from 'vue-agent-input'
25
+ import type { AgentInputContent, AgentInputSendPayload } from 'vue-agent-input'
26
+ import 'vue-agent-input/style.css'
27
+
28
+ const draft = ref<AgentInputContent>([])
29
+
30
+ function handleSend({ text }: AgentInputSendPayload) {
31
+ console.log(text)
32
+ draft.value = []
33
+ }
34
+ </script>
35
+
36
+ <template>
37
+ <AgentInput v-model="draft" placeholder="输入消息…" @send="handleSend" />
38
+ </template>
39
+ ```
40
+
41
+ 在正文中按 Enter 发送,按 Shift+Enter 换行。空草稿也会触发 `send`,此时 `text` 为 `''`,`content` 为 `[]`。草稿由调用方清空,智能体通过 `v-model:agent` 独立管理。
42
+
43
+ ## 自定义 `@` 和 `/` 候选
44
+
45
+ 提供 `#suggestion` 插槽后,输入 `@` 或 `/` 会在组件内展开候选。插槽提供触发符和查询词,候选数据与筛选逻辑由调用方提供。下面的示例用 `@` 选择智能体,用 `/` 在光标处插入正文标签。
46
+
47
+ ```vue
48
+ <script setup lang="ts">
49
+ import { ref } from 'vue'
50
+ import { AgentInput } from 'vue-agent-input'
51
+ import type { AgentInputContent, AgentInputItem, AgentInputTag } from 'vue-agent-input'
52
+ import 'vue-agent-input/style.css'
53
+
54
+ const draft = ref<AgentInputContent>([])
55
+ const agent = ref<AgentInputItem | null>(null)
56
+ const agents: AgentInputItem[] = [{ label: '研究助手', value: 'research' }]
57
+ const tags: AgentInputTag[] = [{ label: '季度报告', value: 'report.pdf' }]
58
+
59
+ function filterCandidates(items: AgentInputItem[], query: string) {
60
+ const keyword = query.toLowerCase()
61
+ return items.filter(item =>
62
+ item.label.toLowerCase().includes(keyword) || item.value.toLowerCase().includes(keyword),
63
+ )
64
+ }
65
+
66
+ function handleTagClick(item: AgentInputTag) {
67
+ console.log(item.value)
68
+ }
69
+ </script>
70
+
71
+ <template>
72
+ <AgentInput v-model="draft" v-model:agent="agent" @tag-click="handleTagClick">
73
+ <template #suggestion="{ trigger, query, setAgent, insertTag }">
74
+ <button
75
+ v-for="item in filterCandidates(trigger === '@' ? agents : tags, query)"
76
+ :key="item.value"
77
+ type="button"
78
+ @click="trigger === '@' ? setAgent(item) : insertTag(item)"
79
+ >
80
+ {{ item.label }}
81
+ </button>
82
+ </template>
83
+ </AgentInput>
84
+ </template>
85
+ ```
86
+
87
+ `setAgent(item)` 和 `insertTag(item)` 都会移除当前触发符及查询词,并恢复正文焦点。`close()` 收起面板并保留已输入的文字。`close({ clearTrigger: true })` 适用于连接器等自定义候选,会移除触发符及查询词并恢复焦点。
88
+
89
+ 按 Escape 关闭候选面板。需要自定义方向键或 Enter 选择时,监听 `suggestion-keydown`,并对接管的按键调用 `event.preventDefault()`。面板打开时,未接管的 Enter 会收起面板。
90
+
91
+ ## 正文标签与发送数据
92
+
93
+ `AgentInputContent` 由字符串和 `AgentInputTag` 按顺序组成。标签的 `label` 用于显示,`img` 和 `color` 分别设置可选图片和文字颜色。使用 Backspace 或 Delete 可以整块删除相邻标签。
94
+
95
+ `send` 的参数为 `{ text, content, agent }`。`text` 是可提交的纯文本,`content` 保留标签对象和顺序,`agent` 是当前智能体或 `null`。序列化时,标签展开为 `value`,和相邻内容之间缺少空白时会补一个空格。下面的草稿会生成 `请看 report.pdf 谢谢`。
96
+
97
+ ```ts
98
+ import type { AgentInputContent } from 'vue-agent-input'
99
+
100
+ const draft: AgentInputContent = [
101
+ '请看',
102
+ { label: '季度报告', value: 'report.pdf' },
103
+ '谢谢',
104
+ ]
105
+ ```
106
+
107
+ ## API
108
+
109
+ ### Props
110
+
111
+ | Prop | 类型 | 默认值 | 用途 |
112
+ | --- | --- | --- | --- |
113
+ | `modelValue` | `AgentInputContent` | `[]` | 通过 `v-model` 绑定草稿。 |
114
+ | `agent` | `AgentInputItem \| null` | `null` | 通过 `v-model:agent` 绑定正文首行的智能体。`AgentInputItem` 包含 `label`、`value` 和可选的 `img`。 |
115
+ | `theme` | `'light' \| 'dark'` | `'light'` | 选择浅色或深色主题。 |
116
+ | `minRows` | `number` | `2` | 正文最少显示的行数。 |
117
+ | `maxRows` | `number` | `6` | 正文最多显示的行数,超出时在正文区域内滚动。 |
118
+ | `placeholder` | `string` | `'输入消息'` | 正文为空时显示的提示。 |
119
+ | `ariaLabel` | `string` | `'消息正文'` | 编辑区的无障碍名称。 |
120
+ | `loading` | `boolean` | `false` | 将发送按钮切换为停止按钮。 |
121
+
122
+ ### 事件
123
+
124
+ | 事件 | 回调参数 | 触发时机 |
125
+ | --- | --- | --- |
126
+ | `send` | `AgentInputSendPayload` | 点击发送按钮,或在正文中按 Enter 发送。 |
127
+ | `stop` | — | `loading` 为 `true` 时点击停止按钮。 |
128
+ | `tag-click` | `AgentInputTag` | 点击正文标签,或在标签聚焦时按 Enter 或空格。 |
129
+ | `suggestion-keydown` | `KeyboardEvent` | 候选面板打开时收到的正文按键。 |
130
+ | `update:modelValue` | `AgentInputContent` | 草稿更新,对应 `v-model`。 |
131
+ | `update:agent` | `AgentInputItem \| null` | 选择或移除智能体,对应 `v-model:agent`。 |
132
+
133
+ 接入流式请求时,调用方设置 `loading`,在 `stop` 回调中取消请求,并在请求结束后将 `loading` 恢复为 `false`。
134
+
135
+ ### 插槽
136
+
137
+ | 插槽 | 位置和用途 |
138
+ | --- | --- |
139
+ | `header` | 位于正文上方,可放工具栏或已选连接器。 |
140
+ | `footer` | 填满发送按钮左侧的可用宽度,两者相隔 `0.5rem`。 |
141
+ | `suggestion` | 位于组件内部的 `header` 上方。接收 `{ trigger, query, setAgent, insertTag, close }`,类型为 `AgentInputSuggestion`。最大高度为 `min(16rem, 50dvh)`,超出时在面板内滚动。 |
142
+
143
+ ### 实例方法
144
+
145
+ 通过 `ref<AgentInputExpose | null>(null)` 获取组件实例。
146
+
147
+ | 方法 | 用途 |
148
+ | --- | --- |
149
+ | `focus()` | 将焦点移回正文。 |
150
+ | `setAgent(item)` | 设置首行智能体,传 `null` 清除。 |
151
+ | `insertTag(item)` | 用标签替换当前或最近的正文选区,未建立选区时追加到末尾。 |
152
+
153
+ ## 主题与样式
154
+
155
+ 使用 `theme="dark"` 切换到深色主题。正文标签默认文字色为 `#06f`,背景透明,在悬停或键盘聚焦时显示背景。可在组件根元素上覆盖 CSS 变量,也可通过单个标签的 `color` 设置文字颜色。
156
+
157
+ ```vue
158
+ <AgentInput
159
+ theme="dark"
160
+ style="--agent-input-tag-color: #8ab4ff; --agent-input-tag-background: #303846"
161
+ />
162
+ ```
163
+
164
+ 常用变量还有 `--agent-input-surface`、`--agent-input-text`、`--agent-input-border` 和 `--agent-input-focus`。
165
+
166
+ ## 本地开发
167
+
168
+ ```sh
169
+ npm install
170
+ npm run dev
171
+ npm run typecheck
172
+ npm run build:demo
173
+ npx playwright install chromium webkit
174
+ npm test
175
+ ```
176
+
177
+ 运行 `npm pack` 会先构建组件库,再生成包含 JavaScript、CSS、类型声明和 `LICENSE` 的本地安装包。
178
+
179
+ ## 许可证
180
+
181
+ MIT © 2026 originRing
@@ -0,0 +1,36 @@
1
+ import type { AgentInputContent, AgentInputExpose, AgentInputItem, AgentInputProps, AgentInputSendPayload, AgentInputSuggestion, AgentInputTag } from './types.js';
2
+ type __VLS_Slots = {
3
+ header?(): unknown;
4
+ footer?(): unknown;
5
+ suggestion?(context: AgentInputSuggestion): unknown;
6
+ };
7
+ declare const __VLS_base: import("vue").DefineComponent<AgentInputProps, AgentInputExpose, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
8
+ stop: () => any;
9
+ "update:modelValue": (content: AgentInputContent) => any;
10
+ "update:agent": (agent: AgentInputItem | null) => any;
11
+ "suggestion-keydown": (event: KeyboardEvent) => any;
12
+ "tag-click": (item: AgentInputTag) => any;
13
+ send: (payload: AgentInputSendPayload) => any;
14
+ }, string, import("vue").PublicProps, Readonly<AgentInputProps> & Readonly<{
15
+ onStop?: (() => any) | undefined;
16
+ "onUpdate:modelValue"?: ((content: AgentInputContent) => any) | undefined;
17
+ "onUpdate:agent"?: ((agent: AgentInputItem | null) => any) | undefined;
18
+ "onSuggestion-keydown"?: ((event: KeyboardEvent) => any) | undefined;
19
+ "onTag-click"?: ((item: AgentInputTag) => any) | undefined;
20
+ onSend?: ((payload: AgentInputSendPayload) => any) | undefined;
21
+ }>, {
22
+ theme: import("./types.js").AgentInputTheme;
23
+ minRows: number;
24
+ maxRows: number;
25
+ placeholder: string;
26
+ ariaLabel: string;
27
+ loading: boolean;
28
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false>;
29
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
30
+ declare const _default: typeof __VLS_export;
31
+ export default _default;
32
+ type __VLS_WithSlots<T, S> = T & {
33
+ new (): {
34
+ $slots: S;
35
+ };
36
+ };
@@ -0,0 +1,10 @@
1
+ import type { AgentInputContent } from './types.js';
2
+ export interface DraftSelection {
3
+ start: number;
4
+ end: number;
5
+ }
6
+ export declare function draftLength(content: AgentInputContent): number;
7
+ export declare function normalizeDraft(content: AgentInputContent): AgentInputContent;
8
+ export declare function sameDraft(first: AgentInputContent, second: AgentInputContent): boolean;
9
+ export declare function sliceDraft(content: AgentInputContent, start: number, end?: number): AgentInputContent;
10
+ export declare function serializeDraft(content: AgentInputContent): string;
@@ -0,0 +1,18 @@
1
+ import type { DraftSelection } from './draft.js';
2
+ import type { AgentInputContent, AgentInputTag } from './types.js';
3
+ interface DomPoint {
4
+ node: Node;
5
+ offset: number;
6
+ }
7
+ export interface EditorSnapshot {
8
+ content: AgentInputContent;
9
+ length: number;
10
+ offsetAt(node: Node, offset: number): number | undefined;
11
+ pointAt(offset: number): DomPoint;
12
+ }
13
+ export declare function tagFromTarget(editor: HTMLElement, target: EventTarget | null): AgentInputTag | undefined;
14
+ export declare function renderEditor(editor: HTMLElement, content: AgentInputContent): EditorSnapshot;
15
+ export declare function readEditor(editor: HTMLElement): EditorSnapshot;
16
+ export declare function readSelection(editor: HTMLElement, snapshot: EditorSnapshot): DraftSelection | null;
17
+ export declare function restoreSelection(scrollArea: HTMLElement, snapshot: EditorSnapshot, selection: DraftSelection): void;
18
+ export {};
@@ -0,0 +1,2 @@
1
+ export { default as AgentInput } from './AgentInput.vue.js';
2
+ export type { AgentInputContent, AgentInputExpose, AgentInputItem, AgentInputProps, AgentInputSendPayload, AgentInputSuggestion, AgentInputTag, AgentInputTheme, AgentInputTrigger, } from './types.js';
package/dist/index.js ADDED
@@ -0,0 +1,496 @@
1
+ import { defineComponent as Ot, useSlots as Pt, ref as F, useId as $t, shallowRef as j, computed as H, watch as Vt, onMounted as Kt, onBeforeUnmount as Ft, openBlock as T, createElementBlock as S, withKeys as qt, normalizeStyle as Wt, renderSlot as Q, createCommentVNode as B, createElementVNode as _, toDisplayString as lt, withModifiers as zt } from "vue";
2
+ const gt = (r) => typeof r == "string" ? r.length : 1;
3
+ function ut(r) {
4
+ return r.reduce((n, a) => n + gt(a), 0);
5
+ }
6
+ function N(r) {
7
+ const n = [];
8
+ for (const a of r) {
9
+ if (typeof a != "string") {
10
+ n.push({ ...a });
11
+ continue;
12
+ }
13
+ if (a === "") continue;
14
+ const l = n[n.length - 1], o = a.replace(/\r\n?/g, `
15
+ `);
16
+ typeof l == "string" ? n[n.length - 1] = l + o : n.push(o);
17
+ }
18
+ return n;
19
+ }
20
+ function Y(r, n) {
21
+ return r.length === n.length && r.every((a, l) => {
22
+ const o = n[l];
23
+ return typeof a == "string" || typeof o == "string" ? a === o : a.label === o.label && a.value === o.value && a.img === o.img && a.color === o.color;
24
+ });
25
+ }
26
+ function R(r, n, a = 1 / 0) {
27
+ const l = [];
28
+ let o = 0;
29
+ for (const d of r) {
30
+ const g = o + gt(d);
31
+ if (o >= a) break;
32
+ g > n && n < a && l.push(typeof d == "string" ? d.slice(Math.max(0, n - o), a - o) : d), o = g;
33
+ }
34
+ return l;
35
+ }
36
+ function ct(r) {
37
+ let n = "", a = !1;
38
+ for (const l of r) {
39
+ const o = typeof l != "string", d = o ? l.value : l;
40
+ d !== "" && ((o || a) && n && !/\s$/.test(n) && !/^\s/.test(d) && (n += " "), n += d, a = o);
41
+ }
42
+ return n;
43
+ }
44
+ const pt = "\uFEFF", Z = "data-agent-input-tag", ht = "data-agent-input-guard";
45
+ function mt(r) {
46
+ const n = r.getAttribute(Z);
47
+ return n === null ? void 0 : JSON.parse(n);
48
+ }
49
+ function ft(r, n) {
50
+ if (!(n instanceof Element)) return;
51
+ const a = n.closest(`[${Z}]`);
52
+ if (a && r.contains(a)) return mt(a);
53
+ }
54
+ function Gt(r, n) {
55
+ const a = r.ownerDocument, l = a.createDocumentFragment();
56
+ function o() {
57
+ const g = a.createElement("span");
58
+ g.setAttribute(ht, ""), g.textContent = pt, l.append(g);
59
+ }
60
+ for (const [g, s] of n.entries()) {
61
+ if (typeof s == "string") {
62
+ l.append(a.createTextNode(s));
63
+ continue;
64
+ }
65
+ typeof n[g - 1] != "string" && o();
66
+ const i = a.createElement("button");
67
+ if (i.type = "button", i.className = "agent-input__tag", i.contentEditable = "false", i.setAttribute(Z, JSON.stringify(s)), s.img) {
68
+ const u = a.createElement("img");
69
+ u.src = s.img, u.alt = "", u.draggable = !1, i.append(u);
70
+ }
71
+ const h = a.createElement("span");
72
+ h.className = "agent-input__tag-label", h.textContent = s.label, s.color && (h.style.color = s.color), i.append(h), l.append(i);
73
+ }
74
+ const d = n[n.length - 1];
75
+ return (typeof d != "string" || d.endsWith(`
76
+ `)) && o(), r.replaceChildren(l), vt(r);
77
+ }
78
+ function vt(r) {
79
+ const n = [], a = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Map();
80
+ let o = 0;
81
+ function d(s) {
82
+ if (!s) return;
83
+ const i = n[n.length - 1];
84
+ typeof i == "string" ? n[n.length - 1] = i + s : n.push(s), o += s.length;
85
+ }
86
+ function g(s, i) {
87
+ if (s.nodeType === Node.TEXT_NODE) {
88
+ const y = s, c = y.data, x = i?.pending ? c.indexOf(pt) : -1;
89
+ x !== -1 && i && (i.pending = !1);
90
+ const M = o;
91
+ d(x === -1 ? c : c.slice(0, x) + c.slice(x + 1)), l.set(s, { node: y, start: M, end: o, guardIndex: x });
92
+ return;
93
+ }
94
+ if (s.nodeType !== Node.ELEMENT_NODE) return;
95
+ const h = s, u = mt(h);
96
+ if (u) {
97
+ a.set(s, [o, o + 1]), n.push(u), o++;
98
+ return;
99
+ }
100
+ if (h.tagName === "BR") {
101
+ a.set(s, [o]), h.nextSibling && d(`
102
+ `);
103
+ return;
104
+ }
105
+ const b = [o], w = h.hasAttribute(ht) ? { pending: !0 } : i;
106
+ let v = !1;
107
+ for (const [y, c] of [...s.childNodes].entries()) {
108
+ const x = /^(DIV|P)$/.test(c.nodeName);
109
+ y > 0 && (x || v) && d(`
110
+ `), b[y] = o, g(c, w), b.push(o), v = x;
111
+ }
112
+ a.set(s, b);
113
+ }
114
+ return g(r), {
115
+ content: n,
116
+ length: o,
117
+ offsetAt(s, i) {
118
+ const h = l.get(s);
119
+ if (h) return h.start + i - +(h.guardIndex !== -1 && i > h.guardIndex);
120
+ const u = a.get(s);
121
+ if (u) return u[Math.min(i, u.length - 1)];
122
+ },
123
+ pointAt(s) {
124
+ const i = Math.max(0, Math.min(s, o));
125
+ for (const b of l.values()) {
126
+ if (i < b.start || i > b.end) continue;
127
+ const w = i - b.start;
128
+ return { node: b.node, offset: w + +(b.guardIndex !== -1 && w >= b.guardIndex) };
129
+ }
130
+ const u = (a.get(r) ?? [0]).findIndex((b) => b >= i);
131
+ return { node: r, offset: u === -1 ? r.childNodes.length : u };
132
+ }
133
+ };
134
+ }
135
+ function Jt(r, n) {
136
+ const a = r.ownerDocument.getSelection();
137
+ if (!a?.rangeCount) return null;
138
+ const l = a.getRangeAt(0), o = n.offsetAt(l.startContainer, l.startOffset), d = n.offsetAt(l.endContainer, l.endOffset);
139
+ return o === void 0 || d === void 0 ? null : { start: o, end: d };
140
+ }
141
+ function dt(r, n, a) {
142
+ const l = r.ownerDocument, o = n.pointAt(a.start), d = n.pointAt(a.end), g = l.createRange();
143
+ g.setStart(o.node, o.offset), g.setEnd(d.node, d.offset), l.getSelection()?.removeAllRanges(), l.getSelection()?.addRange(g);
144
+ const s = g.getBoundingClientRect(), i = r.getBoundingClientRect();
145
+ s.height && s.bottom > i.bottom && (r.scrollTop += s.bottom - i.bottom), s.height && s.top < i.top && (r.scrollTop -= i.top - s.top);
146
+ }
147
+ const Ut = ["data-theme"], Xt = {
148
+ key: 1,
149
+ class: "agent-input__header"
150
+ }, jt = {
151
+ key: 0,
152
+ class: "agent-input__agent"
153
+ }, Ht = ["src"], Qt = { class: "agent-input__agent-label" }, Yt = ["aria-label"], Zt = {
154
+ key: 1,
155
+ class: "agent-input__placeholder",
156
+ "aria-hidden": "true"
157
+ }, te = ["aria-label", "aria-placeholder", "aria-controls"], ee = { class: "agent-input__footer" }, ne = {
158
+ key: 0,
159
+ class: "agent-input__footer-content"
160
+ }, oe = ["aria-label"], ae = {
161
+ key: 0,
162
+ viewBox: "0 0 24 24",
163
+ "aria-hidden": "true"
164
+ }, ie = {
165
+ key: 1,
166
+ viewBox: "0 0 24 24",
167
+ fill: "none",
168
+ "aria-hidden": "true"
169
+ }, se = /* @__PURE__ */ Ot({
170
+ __name: "AgentInput",
171
+ props: {
172
+ modelValue: {},
173
+ agent: {},
174
+ theme: { default: "light" },
175
+ minRows: { default: 2 },
176
+ maxRows: { default: 6 },
177
+ placeholder: { default: "输入消息" },
178
+ ariaLabel: { default: "消息正文" },
179
+ loading: { type: Boolean, default: !1 }
180
+ },
181
+ emits: ["update:modelValue", "update:agent", "suggestion-keydown", "tag-click", "send", "stop"],
182
+ setup(r, { expose: n, emit: a }) {
183
+ const l = r, o = a, d = Pt(), g = F(null), s = F(null), i = F(null), h = `agent-input-suggestion-${$t()}`, u = j(N(l.modelValue ?? [])), b = j(null), w = H(() => l.agent === void 0 ? b.value : l.agent), v = j(null), y = F(!1);
184
+ let c = null, x = null, M = null, E, O = !1;
185
+ const tt = H(() => et(l.minRows, 2)), yt = H(() => Math.max(tt.value, et(l.maxRows, 6)));
186
+ function et(t, e) {
187
+ return Number.isFinite(t) ? Math.max(1, Math.floor(t)) : e;
188
+ }
189
+ function I() {
190
+ return !i.value || !c || E?.activeElement !== i.value ? null : Jt(i.value, c);
191
+ }
192
+ function C() {
193
+ const t = c?.length ?? ut(u.value);
194
+ return I() ?? x ?? { start: t, end: t };
195
+ }
196
+ function q() {
197
+ const t = i.value, e = s.value;
198
+ if (!t || !e || !c || E?.activeElement === t) return;
199
+ const f = C();
200
+ t.focus({ preventScroll: !0 }), dt(e, c, f);
201
+ }
202
+ function D() {
203
+ v.value = null;
204
+ }
205
+ function W(t, e = "") {
206
+ if (!d.suggestion || !t || t.start !== t.end) {
207
+ D();
208
+ return;
209
+ }
210
+ const f = t.end, p = Math.max(e.lastIndexOf("@"), e.lastIndexOf("/")), m = p === -1 ? v.value?.start : f - e.length + p;
211
+ if (m === void 0) return;
212
+ const [k, ...$] = R(u.value, m, f);
213
+ if (typeof k != "string" || $.length || !/^[@/][^\s@/]*$/u.test(k)) {
214
+ D();
215
+ return;
216
+ }
217
+ const V = k[0], st = k.slice(1), K = v.value;
218
+ (K?.start !== m || K.end !== f || K.trigger !== V || K.query !== st) && (v.value = { start: m, end: f, trigger: V, query: st });
219
+ }
220
+ function nt() {
221
+ if (y.value) return;
222
+ const t = I();
223
+ t && (x = t, W(t));
224
+ }
225
+ function L(t, e, f = !1) {
226
+ const p = i.value;
227
+ if (!p || (c = Gt(p, t), !e)) return;
228
+ const m = {
229
+ start: Math.min(e.start, c.length),
230
+ end: Math.min(e.end, c.length)
231
+ };
232
+ x = m, f && p.focus({ preventScroll: !0 }), s.value && E?.activeElement === p && dt(s.value, c, m);
233
+ }
234
+ function ot(t) {
235
+ return Y(u.value, t) ? !1 : (u.value = t, !0);
236
+ }
237
+ function A(t, e, f = {}) {
238
+ const p = N(e), m = N([
239
+ ...R(u.value, 0, t.start),
240
+ ...p,
241
+ ...R(u.value, t.end)
242
+ ]), k = t.start + ut(p), $ = { start: k, end: k }, V = ot(m);
243
+ L(m, $, f.focus), W($, f.typed), V && o("update:modelValue", N(m));
244
+ }
245
+ function at(t = "") {
246
+ const e = i.value;
247
+ if (!e) return;
248
+ c = vt(e);
249
+ const f = I();
250
+ f && (x = f);
251
+ const p = ot(c.content);
252
+ !c.length && c.pointAt(0).node === e && L([], { start: 0, end: 0 }), W(f, t), p && o("update:modelValue", N(u.value));
253
+ }
254
+ function it(t) {
255
+ if (Y(u.value, t)) return;
256
+ const e = I() ?? x;
257
+ u.value = t, D(), L(t, e);
258
+ }
259
+ Vt(() => l.modelValue, (t) => {
260
+ if (t === void 0) return;
261
+ const e = N(t);
262
+ y.value ? M = e : it(e);
263
+ }, { deep: !0 });
264
+ function bt(t) {
265
+ y.value || t.isComposing || at(t.inputType === "insertText" ? t.data ?? "" : "");
266
+ }
267
+ function xt() {
268
+ y.value = !0;
269
+ }
270
+ function Et(t) {
271
+ if (y.value = !1, M !== null) {
272
+ const e = M;
273
+ M = null, Y(u.value, e) ? L(e, x) : it(e);
274
+ return;
275
+ }
276
+ at(t.data);
277
+ }
278
+ function z(t) {
279
+ b.value = t, o("update:agent", t);
280
+ }
281
+ function wt() {
282
+ z(null), q();
283
+ }
284
+ function Dt(t) {
285
+ const e = C();
286
+ D(), A(e, [t], { focus: !0 });
287
+ }
288
+ function G(t) {
289
+ const e = v.value;
290
+ return e ? (D(), A(e, t, { focus: !0 }), !0) : !1;
291
+ }
292
+ function kt(t) {
293
+ G([t]);
294
+ }
295
+ function _t(t) {
296
+ G([]) && z(t);
297
+ }
298
+ function Ct(t) {
299
+ t?.clearTrigger ? G([]) : D();
300
+ }
301
+ function J() {
302
+ if (l.loading || y.value) return;
303
+ const t = N(u.value);
304
+ o("send", { text: ct(t), content: t, agent: w.value && { ...w.value } });
305
+ }
306
+ function Tt() {
307
+ l.loading ? o("stop") : J();
308
+ }
309
+ function U(t) {
310
+ return R(u.value, t.start, t.end).some((e) => typeof e != "string");
311
+ }
312
+ function X(t, e, f) {
313
+ const { start: p, end: m } = f, k = p !== m ? f : {
314
+ start: e === -1 ? Math.max(0, p - 1) : p,
315
+ end: e === 1 ? p + 1 : p
316
+ };
317
+ U(k) && (t.preventDefault(), A(k, []));
318
+ }
319
+ function St(t) {
320
+ t.button === 0 && i.value && ft(i.value, t.target) && t.preventDefault();
321
+ }
322
+ function At(t) {
323
+ if (!i.value) return;
324
+ const e = ft(i.value, t.target);
325
+ e && o("tag-click", e);
326
+ }
327
+ function Nt(t) {
328
+ if (!(t.target !== i.value || y.value || t.isComposing || t.keyCode === 229)) {
329
+ if (v.value && t.key !== "Escape") {
330
+ if (o("suggestion-keydown", t), t.defaultPrevented) return;
331
+ if (t.key === "Enter" && !t.shiftKey) {
332
+ t.preventDefault(), D();
333
+ return;
334
+ }
335
+ }
336
+ t.key === "Enter" ? (t.preventDefault(), t.shiftKey ? A(C(), [`
337
+ `]) : J()) : !t.metaKey && !t.ctrlKey && !t.altKey && (t.key === "Backspace" && X(t, -1, C()), t.key === "Delete" && X(t, 1, C()));
338
+ }
339
+ }
340
+ function Mt(t) {
341
+ if (!(y.value || t.isComposing || !t.cancelable)) {
342
+ if (t.inputType === "insertParagraph" || t.inputType === "insertLineBreak") {
343
+ t.preventDefault(), t.inputType === "insertLineBreak" ? A(C(), [`
344
+ `]) : v.value ? D() : J();
345
+ return;
346
+ }
347
+ if (t.inputType.startsWith("delete") && t.inputType !== "deleteByCut") {
348
+ let e = C();
349
+ const f = t.getTargetRanges?.()[0];
350
+ if (f && c) {
351
+ const p = c.offsetAt(f.startContainer, f.startOffset), m = c.offsetAt(f.endContainer, f.endOffset);
352
+ p !== void 0 && m !== void 0 && p !== m && (e = { start: p, end: m });
353
+ }
354
+ X(t, t.inputType.endsWith("Backward") ? -1 : 1, e);
355
+ return;
356
+ }
357
+ if (t.inputType === "insertText" && t.data !== null) {
358
+ const e = C();
359
+ U(e) && (t.preventDefault(), A(e, [t.data], { typed: t.data }));
360
+ }
361
+ }
362
+ }
363
+ function Bt(t) {
364
+ t.preventDefault();
365
+ const e = t.clipboardData?.getData("text/plain");
366
+ e && A(C(), [e]);
367
+ }
368
+ function It(t) {
369
+ const e = I();
370
+ !e || !U(e) || !t.clipboardData || (t.preventDefault(), t.clipboardData.setData("text/plain", ct(R(u.value, e.start, e.end))), A(e, []));
371
+ }
372
+ function rt(t) {
373
+ O = g.value !== null && t.composedPath().includes(g.value), O || D();
374
+ }
375
+ function P() {
376
+ O = !1;
377
+ }
378
+ function Lt(t) {
379
+ v.value && (t.relatedTarget instanceof Node && g.value?.contains(t.relatedTarget) || !t.relatedTarget && O || queueMicrotask(() => {
380
+ g.value?.contains(E?.activeElement ?? null) || D();
381
+ }));
382
+ }
383
+ function Rt(t) {
384
+ !v.value || y.value || t.isComposing || t.keyCode === 229 || (t.preventDefault(), D(), q());
385
+ }
386
+ return Kt(() => {
387
+ E = i.value?.ownerDocument, L(u.value, null), E?.addEventListener("selectionchange", nt), E?.addEventListener("pointerdown", rt, !0), E?.addEventListener("pointerup", P, !0), E?.addEventListener("pointercancel", P, !0);
388
+ }), Ft(() => {
389
+ E?.removeEventListener("selectionchange", nt), E?.removeEventListener("pointerdown", rt, !0), E?.removeEventListener("pointerup", P, !0), E?.removeEventListener("pointercancel", P, !0);
390
+ }), n({ focus: q, setAgent: z, insertTag: Dt }), (t, e) => (T(), S("div", {
391
+ ref_key: "root",
392
+ ref: g,
393
+ class: "agent-input",
394
+ "data-theme": r.theme,
395
+ style: Wt({ "--agent-input-min-rows": tt.value, "--agent-input-max-rows": yt.value }),
396
+ onFocusout: Lt,
397
+ onKeydown: qt(Rt, ["esc"])
398
+ }, [
399
+ v.value && t.$slots.suggestion ? (T(), S("div", {
400
+ key: 0,
401
+ id: h,
402
+ class: "agent-input__suggestion"
403
+ }, [
404
+ Q(t.$slots, "suggestion", {
405
+ trigger: v.value.trigger,
406
+ query: v.value.query,
407
+ setAgent: _t,
408
+ insertTag: kt,
409
+ close: Ct
410
+ })
411
+ ])) : B("", !0),
412
+ t.$slots.header ? (T(), S("div", Xt, [
413
+ Q(t.$slots, "header")
414
+ ])) : B("", !0),
415
+ _("div", {
416
+ ref_key: "scrollArea",
417
+ ref: s,
418
+ class: "agent-input__body"
419
+ }, [
420
+ w.value ? (T(), S("div", jt, [
421
+ w.value.img ? (T(), S("img", {
422
+ key: 0,
423
+ src: w.value.img,
424
+ alt: "",
425
+ draggable: "false"
426
+ }, null, 8, Ht)) : B("", !0),
427
+ _("span", Qt, "@" + lt(w.value.label), 1),
428
+ _("button", {
429
+ type: "button",
430
+ class: "agent-input__remove",
431
+ "aria-label": `移除智能体 ${w.value.label}`,
432
+ onClick: wt
433
+ }, [...e[1] || (e[1] = [
434
+ _("svg", {
435
+ viewBox: "0 0 24 24",
436
+ fill: "none",
437
+ "aria-hidden": "true"
438
+ }, [
439
+ _("path", { d: "m7 7 10 10M17 7 7 17" })
440
+ ], -1)
441
+ ])], 8, Yt)
442
+ ])) : B("", !0),
443
+ r.placeholder && !u.value.length && !y.value ? (T(), S("div", Zt, lt(r.placeholder), 1)) : B("", !0),
444
+ _("div", {
445
+ ref_key: "editor",
446
+ ref: i,
447
+ class: "agent-input__editor",
448
+ contenteditable: "true",
449
+ role: "textbox",
450
+ "aria-multiline": "true",
451
+ "aria-label": r.ariaLabel,
452
+ "aria-placeholder": r.placeholder,
453
+ "aria-controls": v.value ? h : void 0,
454
+ onMousedown: St,
455
+ onClick: At,
456
+ onKeydown: Nt,
457
+ onBeforeinput: Mt,
458
+ onInput: bt,
459
+ onCompositionstart: xt,
460
+ onCompositionend: Et,
461
+ onPaste: Bt,
462
+ onCut: It,
463
+ onDrop: e[0] || (e[0] = zt(() => {
464
+ }, ["prevent"]))
465
+ }, null, 40, te)
466
+ ], 512),
467
+ _("div", ee, [
468
+ t.$slots.footer ? (T(), S("div", ne, [
469
+ Q(t.$slots, "footer")
470
+ ])) : B("", !0),
471
+ _("button", {
472
+ type: "button",
473
+ class: "agent-input__action",
474
+ "aria-label": r.loading ? "停止生成" : "发送消息",
475
+ onClick: Tt
476
+ }, [
477
+ r.loading ? (T(), S("svg", ae, [...e[2] || (e[2] = [
478
+ _("rect", {
479
+ x: "7",
480
+ y: "7",
481
+ width: "10",
482
+ height: "10",
483
+ rx: "2",
484
+ fill: "currentColor"
485
+ }, null, -1)
486
+ ])])) : (T(), S("svg", ie, [...e[3] || (e[3] = [
487
+ _("path", { d: "M12 19V5m-5 5 5-5 5 5" }, null, -1)
488
+ ])]))
489
+ ], 8, oe)
490
+ ])
491
+ ], 44, Ut));
492
+ }
493
+ });
494
+ export {
495
+ se as AgentInput
496
+ };
package/dist/style.css ADDED
@@ -0,0 +1 @@
1
+ .agent-input{--agent-input-surface: #fff;--agent-input-text: #171717;--agent-input-placeholder: #626262;--agent-input-border: #d4d4d4;--agent-input-focus: #595959;--agent-input-tag-background: #efefef;--agent-input-tag-color: #06f;--agent-input-agent-color: #292929;--agent-input-action-background: #171717;--agent-input-action-hover: #3b3b3b;--agent-input-action-color: #fff;--agent-input-line-height: 1.5rem;position:relative;box-sizing:border-box;display:grid;gap:.375rem;min-inline-size:0;padding:.75rem;border:1px solid var(--agent-input-border);border-radius:1rem;background:var(--agent-input-surface);color:var(--agent-input-text);color-scheme:light}.agent-input[data-theme=dark]{--agent-input-surface: #1c1c1c;--agent-input-text: #f5f5f5;--agent-input-placeholder: #ababab;--agent-input-border: #4a4a4a;--agent-input-focus: #bdbdbd;--agent-input-tag-background: #363636;--agent-input-agent-color: #f2f2f2;--agent-input-action-background: #f5f5f5;--agent-input-action-hover: #d7d7d7;--agent-input-action-color: #171717;color-scheme:dark}.agent-input:focus-within{border-color:var(--agent-input-focus)}.agent-input__header{min-inline-size:0}.agent-input__body{min-inline-size:0;max-block-size:calc(var(--agent-input-line-height) * var(--agent-input-max-rows));padding-inline:.25rem;overflow:auto}.agent-input__editor{min-block-size:calc(var(--agent-input-line-height) * var(--agent-input-min-rows));overflow-wrap:anywhere;white-space:pre-wrap;white-space:break-spaces;outline:none;font-size:1rem;line-height:var(--agent-input-line-height)}.agent-input__placeholder{block-size:var(--agent-input-line-height);margin-block-end:calc(-1 * var(--agent-input-line-height));overflow:hidden;color:var(--agent-input-placeholder);font-size:1rem;line-height:var(--agent-input-line-height);text-overflow:ellipsis;white-space:nowrap;pointer-events:none}.agent-input__tag,.agent-input__agent{display:inline-flex;box-sizing:border-box;align-items:center;gap:.3rem;max-inline-size:100%;padding-inline:.4rem;border-radius:.375rem;vertical-align:middle}.agent-input__tag img,.agent-input__agent img{flex:none;inline-size:1rem;block-size:1rem;border-radius:.2rem;object-fit:cover}.agent-input__tag{height:1.25rem;margin-inline:.125rem;max-inline-size:calc(100% - .25rem);padding-block:0;border:0;background:transparent;color:var(--agent-input-tag-color);font:inherit;font-size:.875rem;white-space:nowrap;cursor:pointer}.agent-input__tag:hover,.agent-input__tag:focus-visible{background:var(--agent-input-tag-background)}.agent-input__tag:focus-visible{outline:2px solid var(--agent-input-focus);outline-offset:-2px}.agent-input__agent{float:inline-start;max-inline-size:55%;block-size:var(--agent-input-line-height);margin-inline-end:.375rem;background:var(--agent-input-tag-background);color:var(--agent-input-agent-color);font-size:.875rem}.agent-input__tag-label,.agent-input__agent-label{min-inline-size:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.agent-input__footer{display:flex;justify-content:flex-end;align-items:center;gap:.5rem}.agent-input__footer-content{flex:1 1 0;min-inline-size:0}.agent-input__remove,.agent-input__action{display:grid;flex:none;place-items:center;padding:0;border:0;cursor:pointer}.agent-input__remove svg,.agent-input__action svg{inline-size:1.125rem;block-size:1.125rem}.agent-input__remove path,.agent-input__action path{stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}.agent-input__remove{inline-size:1.75rem;block-size:1.5rem;appearance:none;background:transparent;box-shadow:none;outline:none;color:inherit;-webkit-tap-highlight-color:transparent}.agent-input__remove:focus-visible path{stroke-width:2.6}.agent-input__action{inline-size:2.75rem;block-size:2.75rem;border-radius:.75rem;background:var(--agent-input-action-background);color:var(--agent-input-action-color)}.agent-input__action:hover{background:var(--agent-input-action-hover)}.agent-input__action:focus-visible{outline:2px solid var(--agent-input-focus);outline-offset:2px}.agent-input__suggestion{box-sizing:border-box;min-inline-size:0;max-block-size:min(16rem,50dvh);overflow:auto;overscroll-behavior:contain;border-block-end:1px solid var(--agent-input-border)}
@@ -0,0 +1,40 @@
1
+ export interface AgentInputItem {
2
+ label: string;
3
+ value: string;
4
+ img?: string;
5
+ }
6
+ export interface AgentInputTag extends AgentInputItem {
7
+ color?: string;
8
+ }
9
+ export type AgentInputContent = Array<string | AgentInputTag>;
10
+ export type AgentInputTheme = 'light' | 'dark';
11
+ export type AgentInputTrigger = '@' | '/';
12
+ export interface AgentInputProps {
13
+ modelValue?: AgentInputContent;
14
+ agent?: AgentInputItem | null;
15
+ theme?: AgentInputTheme;
16
+ minRows?: number;
17
+ maxRows?: number;
18
+ placeholder?: string;
19
+ ariaLabel?: string;
20
+ loading?: boolean;
21
+ }
22
+ export interface AgentInputSendPayload {
23
+ text: string;
24
+ content: AgentInputContent;
25
+ agent: AgentInputItem | null;
26
+ }
27
+ export interface AgentInputExpose {
28
+ focus(): void;
29
+ setAgent(item: AgentInputItem | null): void;
30
+ insertTag(item: AgentInputTag): void;
31
+ }
32
+ export interface AgentInputSuggestion {
33
+ trigger: AgentInputTrigger;
34
+ query: string;
35
+ setAgent(item: AgentInputItem): void;
36
+ insertTag(item: AgentInputTag): void;
37
+ close(options?: {
38
+ clearTrigger?: boolean;
39
+ }): void;
40
+ }
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "vue-agent-input",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A Vue composer with inline tags and customizable suggestion panels.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "vue-agent-input",
8
+ "agent-input",
9
+ "ai-input",
10
+ "input",
11
+ "input-x"
12
+ ],
13
+ "type": "module",
14
+ "files": ["dist", "LICENSE"],
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./style.css": "./dist/style.css"
23
+ },
24
+ "publishConfig": {
25
+ "registry": "https://registry.npmjs.org",
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/OriginRing/agent-input"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/OriginRing/agent-input/issues"
34
+ },
35
+ "homepage": "https://github.com/OriginRing/agent-input",
36
+ "sideEffects": ["**/*.css"],
37
+ "scripts": {
38
+ "dev": "vite",
39
+ "typecheck": "vue-tsc --noEmit",
40
+ "build": "vite build --mode library && vue-tsc -p tsconfig.build.json && node scripts/finalize-types.mjs",
41
+ "build:demo": "vite build",
42
+ "test": "playwright test",
43
+ "prepack": "npm run build"
44
+ },
45
+ "peerDependencies": {
46
+ "vue": "^3.5.0"
47
+ },
48
+ "devDependencies": {
49
+ "@playwright/test": "^1.61.0",
50
+ "@vitejs/plugin-vue": "^6.0.0",
51
+ "less": "^4.2.0",
52
+ "typescript": "~5.9.0",
53
+ "vite": "^7.0.0",
54
+ "vue": "^3.5.0",
55
+ "vue-tsc": "^3.0.0"
56
+ }
57
+ }