prompt-area 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 +21 -0
- package/README.md +120 -0
- package/dist/action-bar/index.d.ts +60 -0
- package/dist/action-bar/index.js +5 -0
- package/dist/action-bar/index.js.map +1 -0
- package/dist/chat-prompt-layout/index.d.ts +53 -0
- package/dist/chat-prompt-layout/index.js +5 -0
- package/dist/chat-prompt-layout/index.js.map +1 -0
- package/dist/chunk-ANZZEZP2.js +38 -0
- package/dist/chunk-ANZZEZP2.js.map +1 -0
- package/dist/chunk-BPJO4DGM.js +198 -0
- package/dist/chunk-BPJO4DGM.js.map +1 -0
- package/dist/chunk-BWVBDP7C.js +38 -0
- package/dist/chunk-BWVBDP7C.js.map +1 -0
- package/dist/chunk-E7HUXORB.js +2692 -0
- package/dist/chunk-E7HUXORB.js.map +1 -0
- package/dist/chunk-NF2LHZIE.js +12 -0
- package/dist/chunk-NF2LHZIE.js.map +1 -0
- package/dist/chunk-UBBCAMJA.js +116 -0
- package/dist/chunk-UBBCAMJA.js.map +1 -0
- package/dist/chunk-XDKRP7UE.js +125 -0
- package/dist/chunk-XDKRP7UE.js.map +1 -0
- package/dist/compact-prompt-area/index.d.ts +86 -0
- package/dist/compact-prompt-area/index.js +6 -0
- package/dist/compact-prompt-area/index.js.map +1 -0
- package/dist/helpers/index.d.ts +374 -0
- package/dist/helpers/index.js +291 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/prompt-area/index.d.ts +327 -0
- package/dist/prompt-area/index.js +6 -0
- package/dist/prompt-area/index.js.map +1 -0
- package/dist/status-bar/index.d.ts +50 -0
- package/dist/status-bar/index.js +5 -0
- package/dist/status-bar/index.js.map +1 -0
- package/dist/styles.css +2 -0
- package/dist/tailwind.css +181 -0
- package/dist/types-C4BgDEpe.d.ts +271 -0
- package/package.json +102 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PromptArea component types
|
|
3
|
+
*
|
|
4
|
+
* A lightweight contentEditable-based text input that supports:
|
|
5
|
+
* - Trigger characters (/, @, #) that activate handlers
|
|
6
|
+
* - Immutable chips for resolved mentions/commands
|
|
7
|
+
* - Configurable trigger behavior (dropdown vs callback)
|
|
8
|
+
* - Simple inline markdown rendering
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* A segment of content within the editable text.
|
|
12
|
+
* The document model is an ordered array of these segments.
|
|
13
|
+
*/
|
|
14
|
+
type TextSegment = {
|
|
15
|
+
type: 'text';
|
|
16
|
+
text: string;
|
|
17
|
+
};
|
|
18
|
+
type ChipSegment = {
|
|
19
|
+
type: 'chip';
|
|
20
|
+
/** The trigger character that created this chip (e.g., '@', '#') */
|
|
21
|
+
trigger: string;
|
|
22
|
+
/** The resolved value/ID (e.g., user ID, file ID) */
|
|
23
|
+
value: string;
|
|
24
|
+
/** The display text shown in the chip */
|
|
25
|
+
displayText: string;
|
|
26
|
+
/** Optional data payload attached to the chip */
|
|
27
|
+
data?: unknown;
|
|
28
|
+
/**
|
|
29
|
+
* True when this chip was auto-created by pressing space (resolveOnSpace).
|
|
30
|
+
* Backspace on an auto-resolved chip reverts it to plain text instead of deleting.
|
|
31
|
+
*/
|
|
32
|
+
autoResolved?: boolean;
|
|
33
|
+
};
|
|
34
|
+
type Segment = TextSegment | ChipSegment;
|
|
35
|
+
/**
|
|
36
|
+
* Determines where a trigger character is valid.
|
|
37
|
+
* - 'start': Only valid at the very start of input or after a newline (e.g., slash commands)
|
|
38
|
+
* - 'any': Valid after any whitespace boundary (e.g., @mentions)
|
|
39
|
+
*/
|
|
40
|
+
type TriggerPosition = 'start' | 'any';
|
|
41
|
+
/**
|
|
42
|
+
* Defines how a trigger behaves when activated.
|
|
43
|
+
* - 'dropdown': Shows a popover with suggestions from `onSearch`
|
|
44
|
+
* - 'callback': Fires `onActivate` immediately without a dropdown
|
|
45
|
+
*/
|
|
46
|
+
type TriggerMode = 'dropdown' | 'callback';
|
|
47
|
+
/**
|
|
48
|
+
* Visual style for rendered chips.
|
|
49
|
+
* - 'pill': Button-like pill with background color, padding, border-radius (default)
|
|
50
|
+
* - 'inline': Bold inline text that flows naturally with surrounding content
|
|
51
|
+
*/
|
|
52
|
+
type ChipStyle = 'pill' | 'inline';
|
|
53
|
+
/**
|
|
54
|
+
* A suggestion item shown in the trigger dropdown.
|
|
55
|
+
*/
|
|
56
|
+
type TriggerSuggestion = {
|
|
57
|
+
/** Unique value/ID for this suggestion */
|
|
58
|
+
value: string;
|
|
59
|
+
/** Display label shown in the dropdown */
|
|
60
|
+
label: string;
|
|
61
|
+
/** Optional description shown below the label */
|
|
62
|
+
description?: string;
|
|
63
|
+
/** Optional icon element rendered before the label */
|
|
64
|
+
icon?: React.ReactNode;
|
|
65
|
+
/** Optional arbitrary data passed through on selection */
|
|
66
|
+
data?: unknown;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Configuration for a trigger character.
|
|
70
|
+
*/
|
|
71
|
+
type TriggerConfig = {
|
|
72
|
+
/** The trigger character (e.g., '/', '@', '#') */
|
|
73
|
+
char: string;
|
|
74
|
+
/** Where this trigger is valid */
|
|
75
|
+
position: TriggerPosition;
|
|
76
|
+
/** How this trigger behaves */
|
|
77
|
+
mode: TriggerMode;
|
|
78
|
+
/**
|
|
79
|
+
* For 'dropdown' mode: called with the current query to fetch suggestions.
|
|
80
|
+
* Should return a list of suggestions to display.
|
|
81
|
+
*
|
|
82
|
+
* Receives an options object with an `AbortSignal` that is aborted when a
|
|
83
|
+
* newer search supersedes this one. Pass it to `fetch()` or other async
|
|
84
|
+
* APIs to cancel in-flight work automatically.
|
|
85
|
+
*/
|
|
86
|
+
onSearch?: (query: string, options: {
|
|
87
|
+
signal: AbortSignal;
|
|
88
|
+
}) => TriggerSuggestion[] | Promise<TriggerSuggestion[]>;
|
|
89
|
+
/**
|
|
90
|
+
* For 'dropdown' mode: called when a suggestion is selected.
|
|
91
|
+
* Return the display text for the chip, or void to use `suggestion.label`.
|
|
92
|
+
*/
|
|
93
|
+
onSelect?: (suggestion: TriggerSuggestion) => string | void;
|
|
94
|
+
/**
|
|
95
|
+
* For 'callback' mode: called when the trigger is activated.
|
|
96
|
+
* Receives the full input text and cursor position.
|
|
97
|
+
*/
|
|
98
|
+
onActivate?: (context: TriggerActivateContext) => void;
|
|
99
|
+
/**
|
|
100
|
+
* When true, pressing space while this trigger is active (with a non-empty query)
|
|
101
|
+
* auto-resolves the typed text into a chip without selecting from the dropdown.
|
|
102
|
+
* The auto-resolved chip can be reverted to plain text with backspace.
|
|
103
|
+
* Useful for free-form tags (e.g., #hashtag).
|
|
104
|
+
*/
|
|
105
|
+
resolveOnSpace?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Visual style for chips created by this trigger.
|
|
108
|
+
* - 'pill' (default): Button-like pill with background, padding, border-radius
|
|
109
|
+
* - 'inline': Bold inline text without pill styling
|
|
110
|
+
*/
|
|
111
|
+
chipStyle?: ChipStyle;
|
|
112
|
+
/** CSS class name(s) applied to chips created by this trigger */
|
|
113
|
+
chipClassName?: string;
|
|
114
|
+
/** Label used for accessibility (e.g., "mention", "command") */
|
|
115
|
+
accessibilityLabel?: string;
|
|
116
|
+
/**
|
|
117
|
+
* Debounce delay in milliseconds before calling `onSearch`.
|
|
118
|
+
* Defaults to 0 (immediate). The initial empty-query search always fires
|
|
119
|
+
* immediately regardless of this setting so the dropdown appears instantly.
|
|
120
|
+
*/
|
|
121
|
+
searchDebounceMs?: number;
|
|
122
|
+
/**
|
|
123
|
+
* Called when `onSearch` rejects or throws (non-abort errors only).
|
|
124
|
+
* Use this to log errors or show toast notifications.
|
|
125
|
+
*/
|
|
126
|
+
onSearchError?: (error: unknown) => void;
|
|
127
|
+
/**
|
|
128
|
+
* Message shown in the dropdown when `onSearch` returns an empty array.
|
|
129
|
+
* If omitted, the popover hides when there are no results (current behavior).
|
|
130
|
+
*/
|
|
131
|
+
emptyMessage?: string;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Context passed to callback-mode trigger handlers.
|
|
135
|
+
*/
|
|
136
|
+
type TriggerActivateContext = {
|
|
137
|
+
/** The full plain text content at the time of activation */
|
|
138
|
+
text: string;
|
|
139
|
+
/** The cursor offset position */
|
|
140
|
+
cursorPosition: number;
|
|
141
|
+
/** Function to insert a chip at the current cursor position */
|
|
142
|
+
insertChip: (chip: Omit<ChipSegment, 'type'>) => void;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Represents an active trigger being typed by the user.
|
|
146
|
+
*/
|
|
147
|
+
type ActiveTrigger = {
|
|
148
|
+
/** The trigger config that was activated */
|
|
149
|
+
config: TriggerConfig;
|
|
150
|
+
/** Position (character offset) where the trigger character was typed */
|
|
151
|
+
startOffset: number;
|
|
152
|
+
/** The text typed after the trigger character so far */
|
|
153
|
+
query: string;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* An image attachment displayed in the prompt area.
|
|
157
|
+
* State is managed externally by the parent component.
|
|
158
|
+
*/
|
|
159
|
+
type PromptAreaImage = {
|
|
160
|
+
/** Unique identifier for this image */
|
|
161
|
+
id: string;
|
|
162
|
+
/** URL to display (CDN URL or temporary blob URL for preview) */
|
|
163
|
+
url: string;
|
|
164
|
+
/** Optional alt text for accessibility */
|
|
165
|
+
alt?: string;
|
|
166
|
+
/** When true, shows a loading indicator over the thumbnail */
|
|
167
|
+
loading?: boolean;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* A file attachment displayed in the prompt area.
|
|
171
|
+
* State is managed externally by the parent component.
|
|
172
|
+
*/
|
|
173
|
+
type PromptAreaFile = {
|
|
174
|
+
/** Unique identifier for this file */
|
|
175
|
+
id: string;
|
|
176
|
+
/** Display filename (e.g., "report.pdf") */
|
|
177
|
+
name: string;
|
|
178
|
+
/** File size in bytes */
|
|
179
|
+
size?: number;
|
|
180
|
+
/** MIME type (used for icon selection, e.g., "application/pdf") */
|
|
181
|
+
type?: string;
|
|
182
|
+
/** When true, shows a loading indicator over the file card */
|
|
183
|
+
loading?: boolean;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Pure logic engine for the PromptArea component.
|
|
188
|
+
* No DOM dependencies - fully testable in Node.
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Converts an array of segments to a plain text string.
|
|
193
|
+
* Chips are represented as `{trigger}{displayText}` (e.g., "@Alice").
|
|
194
|
+
*/
|
|
195
|
+
declare function segmentsToPlainText(segments: Segment[]): string;
|
|
196
|
+
/**
|
|
197
|
+
* Converts plain text into a single text segment.
|
|
198
|
+
* Used for initial value conversion from plain strings.
|
|
199
|
+
*/
|
|
200
|
+
declare function plainTextToSegments(text: string): Segment[];
|
|
201
|
+
/**
|
|
202
|
+
* Checks whether a trigger character at the given position in text
|
|
203
|
+
* is valid according to the position rule.
|
|
204
|
+
*
|
|
205
|
+
* @param text - The full text content
|
|
206
|
+
* @param charIndex - The index of the trigger character in the text
|
|
207
|
+
* @param position - The position rule to validate against
|
|
208
|
+
*/
|
|
209
|
+
declare function isValidTriggerPosition(text: string, charIndex: number, position: TriggerPosition): boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Scans backwards from the cursor position to detect if the user is
|
|
212
|
+
* currently typing a trigger word.
|
|
213
|
+
*
|
|
214
|
+
* Returns the active trigger info, or null if no trigger is active.
|
|
215
|
+
*
|
|
216
|
+
* @param text - The full plain text content
|
|
217
|
+
* @param cursorPos - The cursor position (character offset from start)
|
|
218
|
+
* @param triggers - Available trigger configurations
|
|
219
|
+
*/
|
|
220
|
+
declare function detectActiveTrigger(text: string, cursorPos: number, triggers: TriggerConfig[]): ActiveTrigger | null;
|
|
221
|
+
/**
|
|
222
|
+
* Resolves an active trigger into a chip within the segments array.
|
|
223
|
+
* Replaces the trigger text (trigger char + query) with a chip segment.
|
|
224
|
+
*
|
|
225
|
+
* @param segments - Current document segments
|
|
226
|
+
* @param activeTrigger - The active trigger to resolve
|
|
227
|
+
* @param chip - The chip data (value, displayText, optional data)
|
|
228
|
+
* @returns New segments array with the chip inserted, and the new cursor position
|
|
229
|
+
*/
|
|
230
|
+
declare function resolveChip(segments: Segment[], activeTrigger: ActiveTrigger, chip: {
|
|
231
|
+
value: string;
|
|
232
|
+
displayText: string;
|
|
233
|
+
data?: unknown;
|
|
234
|
+
autoResolved?: boolean;
|
|
235
|
+
}): {
|
|
236
|
+
segments: Segment[];
|
|
237
|
+
cursorOffset: number;
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Scans text segments for trigger patterns and auto-resolves them into chips.
|
|
241
|
+
* Only resolves triggers that have `resolveOnSpace: true`.
|
|
242
|
+
*
|
|
243
|
+
* Trigger patterns must appear at word boundaries: start of text, after
|
|
244
|
+
* whitespace, or after a newline. This avoids false positives like email
|
|
245
|
+
* addresses (user@example.com).
|
|
246
|
+
*/
|
|
247
|
+
declare function resolveTriggersInSegments(segments: Segment[], triggers: TriggerConfig[]): Segment[];
|
|
248
|
+
type MarkdownToken = {
|
|
249
|
+
type: 'plain';
|
|
250
|
+
text: string;
|
|
251
|
+
} | {
|
|
252
|
+
type: 'bold';
|
|
253
|
+
text: string;
|
|
254
|
+
} | {
|
|
255
|
+
type: 'italic';
|
|
256
|
+
text: string;
|
|
257
|
+
} | {
|
|
258
|
+
type: 'bold-italic';
|
|
259
|
+
text: string;
|
|
260
|
+
} | {
|
|
261
|
+
type: 'url';
|
|
262
|
+
text: string;
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* Parses text for simple inline markdown: bold, italic, bold-italic, and URLs.
|
|
266
|
+
* Does NOT handle block-level markdown (lists, headings, etc.).
|
|
267
|
+
*/
|
|
268
|
+
declare function parseInlineMarkdown(text: string): MarkdownToken[];
|
|
269
|
+
/**
|
|
270
|
+
* Shallow equality check for two segment arrays.
|
|
271
|
+
* Compares type, text, trigger, value, displayText, and autoResolved fields.
|
|
272
|
+
* Avoids JSON.stringify overhead for the common case.
|
|
273
|
+
*/
|
|
274
|
+
declare function segmentsEqual(a: Segment[], b: Segment[]): boolean;
|
|
275
|
+
/**
|
|
276
|
+
* Merges adjacent text segments into single text segments.
|
|
277
|
+
* Also removes empty text segments.
|
|
278
|
+
*/
|
|
279
|
+
declare function mergeAdjacentTextSegments(segments: Segment[]): Segment[];
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Convenience helpers for creating and inspecting Segments.
|
|
283
|
+
*
|
|
284
|
+
* These reduce boilerplate when building AI chat UIs that work with the
|
|
285
|
+
* PromptArea document model.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* import { text, chip, isSegmentsEmpty, segmentsToPlainText } from './segment-helpers'
|
|
290
|
+
*
|
|
291
|
+
* const greeting = [text('Hello '), chip({ trigger: '@', value: 'u1', displayText: 'Alice' })]
|
|
292
|
+
* isSegmentsEmpty(greeting) // false
|
|
293
|
+
* segmentsToPlainText(greeting) // "Hello @Alice"
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
|
|
297
|
+
/** Create a text segment. */
|
|
298
|
+
declare function text(value: string): TextSegment;
|
|
299
|
+
/** Create a chip segment. */
|
|
300
|
+
declare function chip(opts: Omit<ChipSegment, 'type'>): ChipSegment;
|
|
301
|
+
/** Returns `true` when the segment array is empty or contains only whitespace text. */
|
|
302
|
+
declare function isSegmentsEmpty(segments: Segment[]): boolean;
|
|
303
|
+
/** Returns `true` when the segment array contains at least one chip. */
|
|
304
|
+
declare function hasChips(segments: Segment[]): boolean;
|
|
305
|
+
/** Extracts all chip segments from a segment array. */
|
|
306
|
+
declare function getChips(segments: Segment[]): ChipSegment[];
|
|
307
|
+
/** Extracts chips matching a specific trigger character. */
|
|
308
|
+
declare function getChipsByTrigger(segments: Segment[], trigger: string): ChipSegment[];
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Pre-built trigger configuration factories for common AI chat patterns.
|
|
312
|
+
*
|
|
313
|
+
* Each factory returns a full `TriggerConfig` with sensible defaults.
|
|
314
|
+
* Pass only what you need to override.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```tsx
|
|
318
|
+
* <PromptArea
|
|
319
|
+
* triggers={[
|
|
320
|
+
* mentionTrigger({ onSearch: searchUsers }),
|
|
321
|
+
* commandTrigger({ onSearch: searchCommands }),
|
|
322
|
+
* hashtagTrigger(),
|
|
323
|
+
* ]}
|
|
324
|
+
* />
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
type TriggerPresetOptions = Omit<Partial<TriggerConfig>, 'char' | 'position' | 'mode'>;
|
|
329
|
+
type MentionTriggerOptions = TriggerPresetOptions & {
|
|
330
|
+
/** Override the trigger character. Defaults to `'@'`. */
|
|
331
|
+
char?: string;
|
|
332
|
+
};
|
|
333
|
+
/**
|
|
334
|
+
* Creates a **mention** trigger (`@`).
|
|
335
|
+
*
|
|
336
|
+
* Defaults: `position: 'any'`, `mode: 'dropdown'`, `chipStyle: 'pill'`,
|
|
337
|
+
* accessible label `"mention"`.
|
|
338
|
+
*/
|
|
339
|
+
declare function mentionTrigger(opts?: MentionTriggerOptions): TriggerConfig;
|
|
340
|
+
type CommandTriggerOptions = TriggerPresetOptions & {
|
|
341
|
+
/** Override the trigger character. Defaults to `'/'`. */
|
|
342
|
+
char?: string;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Creates a **command** trigger (`/`).
|
|
346
|
+
*
|
|
347
|
+
* Defaults: `position: 'start'`, `mode: 'dropdown'`, `chipStyle: 'inline'`,
|
|
348
|
+
* accessible label `"command"`.
|
|
349
|
+
*/
|
|
350
|
+
declare function commandTrigger(opts?: CommandTriggerOptions): TriggerConfig;
|
|
351
|
+
type HashtagTriggerOptions = TriggerPresetOptions & {
|
|
352
|
+
/** Override the trigger character. Defaults to `'#'`. */
|
|
353
|
+
char?: string;
|
|
354
|
+
};
|
|
355
|
+
/**
|
|
356
|
+
* Creates a **hashtag / tag** trigger (`#`).
|
|
357
|
+
*
|
|
358
|
+
* Defaults: `position: 'any'`, `mode: 'dropdown'`, `chipStyle: 'pill'`,
|
|
359
|
+
* `resolveOnSpace: true`, accessible label `"tag"`.
|
|
360
|
+
*/
|
|
361
|
+
declare function hashtagTrigger(opts?: HashtagTriggerOptions): TriggerConfig;
|
|
362
|
+
type CallbackTriggerOptions = Omit<Partial<TriggerConfig>, 'mode'> & {
|
|
363
|
+
/** The trigger character. Required. */
|
|
364
|
+
char: string;
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* Creates a **callback** trigger that fires `onActivate` instead of showing
|
|
368
|
+
* a dropdown. Useful for opening file pickers, model selectors, etc.
|
|
369
|
+
*
|
|
370
|
+
* Defaults: `position: 'start'`, `mode: 'callback'`.
|
|
371
|
+
*/
|
|
372
|
+
declare function callbackTrigger(opts: CallbackTriggerOptions): TriggerConfig;
|
|
373
|
+
|
|
374
|
+
export { type ActiveTrigger, type CallbackTriggerOptions, type ChipSegment, type ChipStyle, type CommandTriggerOptions, type HashtagTriggerOptions, type MarkdownToken, type MentionTriggerOptions, type PromptAreaFile, type PromptAreaImage, type Segment, type TextSegment, type TriggerActivateContext, type TriggerConfig, type TriggerMode, type TriggerPosition, type TriggerSuggestion, callbackTrigger, chip, commandTrigger, detectActiveTrigger, getChips, getChipsByTrigger, hasChips, hashtagTrigger, isSegmentsEmpty, isValidTriggerPosition, mentionTrigger, mergeAdjacentTextSegments, parseInlineMarkdown, plainTextToSegments, resolveChip, resolveTriggersInSegments, segmentsEqual, segmentsToPlainText, text };
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// src/prompt-area/prompt-area-engine.ts
|
|
2
|
+
function segmentsToPlainText(segments) {
|
|
3
|
+
return segments.map((seg) => {
|
|
4
|
+
if (seg.type === "text") return seg.text;
|
|
5
|
+
return `${seg.trigger}${seg.displayText}`;
|
|
6
|
+
}).join("");
|
|
7
|
+
}
|
|
8
|
+
function plainTextToSegments(text2) {
|
|
9
|
+
if (!text2) return [];
|
|
10
|
+
return [{ type: "text", text: text2 }];
|
|
11
|
+
}
|
|
12
|
+
function isValidTriggerPosition(text2, charIndex, position) {
|
|
13
|
+
if (charIndex === 0) return true;
|
|
14
|
+
const prevChar = text2[charIndex - 1];
|
|
15
|
+
if (position === "start") {
|
|
16
|
+
return prevChar === "\n";
|
|
17
|
+
}
|
|
18
|
+
return prevChar === " " || prevChar === "\n" || prevChar === " ";
|
|
19
|
+
}
|
|
20
|
+
function detectActiveTrigger(text2, cursorPos, triggers) {
|
|
21
|
+
if (!text2 || cursorPos === 0 || triggers.length === 0) return null;
|
|
22
|
+
for (let i = cursorPos - 1; i >= 0; i--) {
|
|
23
|
+
const char = text2[i];
|
|
24
|
+
if (char === " " || char === "\n" || char === " ") {
|
|
25
|
+
if (i + 1 < cursorPos) {
|
|
26
|
+
const nextChar = text2[i + 1];
|
|
27
|
+
const matchingTrigger2 = triggers.find((t) => t.char === nextChar);
|
|
28
|
+
if (matchingTrigger2 && isValidTriggerPosition(text2, i + 1, matchingTrigger2.position)) {
|
|
29
|
+
return {
|
|
30
|
+
config: matchingTrigger2,
|
|
31
|
+
startOffset: i + 1,
|
|
32
|
+
query: text2.slice(i + 2, cursorPos)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const matchingTrigger = triggers.find((t) => t.char === char);
|
|
39
|
+
if (matchingTrigger && isValidTriggerPosition(text2, i, matchingTrigger.position)) {
|
|
40
|
+
return {
|
|
41
|
+
config: matchingTrigger,
|
|
42
|
+
startOffset: i,
|
|
43
|
+
query: text2.slice(i + 1, cursorPos)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function resolveChip(segments, activeTrigger, chip2) {
|
|
50
|
+
const triggerStart = activeTrigger.startOffset;
|
|
51
|
+
const triggerEnd = triggerStart + 1 + activeTrigger.query.length;
|
|
52
|
+
const newSegments = [];
|
|
53
|
+
let offset = 0;
|
|
54
|
+
for (const seg of segments) {
|
|
55
|
+
if (seg.type === "chip") {
|
|
56
|
+
const chipText = `${seg.trigger}${seg.displayText}`;
|
|
57
|
+
const chipStart = offset;
|
|
58
|
+
const chipEnd = offset + chipText.length;
|
|
59
|
+
if (chipEnd <= triggerStart || chipStart >= triggerEnd) {
|
|
60
|
+
newSegments.push(seg);
|
|
61
|
+
}
|
|
62
|
+
offset = chipEnd;
|
|
63
|
+
} else {
|
|
64
|
+
const textStart = offset;
|
|
65
|
+
const textEnd = offset + seg.text.length;
|
|
66
|
+
if (textEnd <= triggerStart) {
|
|
67
|
+
newSegments.push(seg);
|
|
68
|
+
} else if (textStart >= triggerEnd) {
|
|
69
|
+
newSegments.push(seg);
|
|
70
|
+
} else {
|
|
71
|
+
const beforeText = seg.text.slice(0, Math.max(0, triggerStart - textStart));
|
|
72
|
+
const afterText = seg.text.slice(Math.min(seg.text.length, triggerEnd - textStart));
|
|
73
|
+
if (beforeText) {
|
|
74
|
+
newSegments.push({ type: "text", text: beforeText });
|
|
75
|
+
}
|
|
76
|
+
const newChip = {
|
|
77
|
+
type: "chip",
|
|
78
|
+
trigger: activeTrigger.config.char,
|
|
79
|
+
value: chip2.value,
|
|
80
|
+
displayText: chip2.displayText,
|
|
81
|
+
...chip2.data !== void 0 ? { data: chip2.data } : {},
|
|
82
|
+
...chip2.autoResolved ? { autoResolved: true } : {}
|
|
83
|
+
};
|
|
84
|
+
newSegments.push(newChip);
|
|
85
|
+
if (afterText) {
|
|
86
|
+
newSegments.push({ type: "text", text: " " + afterText.replace(/^\s/, "") });
|
|
87
|
+
} else {
|
|
88
|
+
newSegments.push({ type: "text", text: " " });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
offset = textEnd;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const merged = mergeAdjacentTextSegments(newSegments);
|
|
95
|
+
let lastChipEndOffset = -1;
|
|
96
|
+
let runningOffset = 0;
|
|
97
|
+
for (const seg of merged) {
|
|
98
|
+
if (seg.type === "text") {
|
|
99
|
+
runningOffset += seg.text.length;
|
|
100
|
+
} else {
|
|
101
|
+
runningOffset += seg.trigger.length + seg.displayText.length;
|
|
102
|
+
if (seg.value === chip2.value && seg.displayText === chip2.displayText && seg.trigger === activeTrigger.config.char) {
|
|
103
|
+
lastChipEndOffset = runningOffset;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const cursorOffset = lastChipEndOffset === -1 ? runningOffset : lastChipEndOffset + 1;
|
|
108
|
+
return { segments: merged, cursorOffset };
|
|
109
|
+
}
|
|
110
|
+
function resolveTriggersInSegments(segments, triggers) {
|
|
111
|
+
const autoResolveTriggers = triggers.filter((t) => t.resolveOnSpace);
|
|
112
|
+
if (autoResolveTriggers.length === 0) return segments;
|
|
113
|
+
const triggerChars = new Set(autoResolveTriggers.map((t) => t.char));
|
|
114
|
+
const result = [];
|
|
115
|
+
for (const seg of segments) {
|
|
116
|
+
if (seg.type === "chip") {
|
|
117
|
+
result.push(seg);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const parts = splitTextByTriggerPatterns(seg.text, autoResolveTriggers, triggerChars);
|
|
121
|
+
result.push(...parts);
|
|
122
|
+
}
|
|
123
|
+
return mergeAdjacentTextSegments(result);
|
|
124
|
+
}
|
|
125
|
+
function splitTextByTriggerPatterns(text2, triggers, triggerChars) {
|
|
126
|
+
if (!text2) return [];
|
|
127
|
+
const segments = [];
|
|
128
|
+
let i = 0;
|
|
129
|
+
while (i < text2.length) {
|
|
130
|
+
const char = text2[i];
|
|
131
|
+
if (triggerChars.has(char)) {
|
|
132
|
+
const isAtBoundary = i === 0 || text2[i - 1] === " " || text2[i - 1] === "\n" || text2[i - 1] === " ";
|
|
133
|
+
if (isAtBoundary) {
|
|
134
|
+
const trigger = triggers.find((t) => t.char === char);
|
|
135
|
+
if (trigger && isValidTriggerPosition(text2, i, trigger.position)) {
|
|
136
|
+
let end = i + 1;
|
|
137
|
+
while (end < text2.length && text2[end] !== " " && text2[end] !== "\n" && text2[end] !== " ") {
|
|
138
|
+
end++;
|
|
139
|
+
}
|
|
140
|
+
const query = text2.slice(i + 1, end);
|
|
141
|
+
if (query.length > 0) {
|
|
142
|
+
const displayText = trigger.onSelect?.({ value: query, label: query }) || query;
|
|
143
|
+
segments.push({
|
|
144
|
+
type: "chip",
|
|
145
|
+
trigger: char,
|
|
146
|
+
value: query,
|
|
147
|
+
displayText,
|
|
148
|
+
autoResolved: true
|
|
149
|
+
});
|
|
150
|
+
i = end;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const start = i;
|
|
157
|
+
i++;
|
|
158
|
+
while (i < text2.length && !(triggerChars.has(text2[i]) && (text2[i - 1] === " " || text2[i - 1] === "\n" || text2[i - 1] === " "))) {
|
|
159
|
+
i++;
|
|
160
|
+
}
|
|
161
|
+
segments.push({ type: "text", text: text2.slice(start, i) });
|
|
162
|
+
}
|
|
163
|
+
return segments;
|
|
164
|
+
}
|
|
165
|
+
function parseInlineMarkdown(text2) {
|
|
166
|
+
if (!text2) return [];
|
|
167
|
+
const tokens = [];
|
|
168
|
+
const pattern = /(\*{3}(.+?)\*{3})|(\*{2}(.+?)\*{2})|(\*(.+?)\*)|(https?:\/\/[^\s),]+)/g;
|
|
169
|
+
let lastIndex = 0;
|
|
170
|
+
let match;
|
|
171
|
+
while ((match = pattern.exec(text2)) !== null) {
|
|
172
|
+
if (match.index > lastIndex) {
|
|
173
|
+
tokens.push({ type: "plain", text: text2.slice(lastIndex, match.index) });
|
|
174
|
+
}
|
|
175
|
+
if (match[1] && match[2]) {
|
|
176
|
+
tokens.push({ type: "bold-italic", text: match[2] });
|
|
177
|
+
} else if (match[3] && match[4]) {
|
|
178
|
+
tokens.push({ type: "bold", text: match[4] });
|
|
179
|
+
} else if (match[5] && match[6]) {
|
|
180
|
+
tokens.push({ type: "italic", text: match[6] });
|
|
181
|
+
} else if (match[7]) {
|
|
182
|
+
tokens.push({ type: "url", text: match[7] });
|
|
183
|
+
}
|
|
184
|
+
lastIndex = match.index + match[0].length;
|
|
185
|
+
}
|
|
186
|
+
if (lastIndex < text2.length) {
|
|
187
|
+
tokens.push({ type: "plain", text: text2.slice(lastIndex) });
|
|
188
|
+
}
|
|
189
|
+
return tokens;
|
|
190
|
+
}
|
|
191
|
+
function segmentsEqual(a, b) {
|
|
192
|
+
if (a === b) return true;
|
|
193
|
+
if (a.length !== b.length) return false;
|
|
194
|
+
for (let i = 0; i < a.length; i++) {
|
|
195
|
+
const sa = a[i];
|
|
196
|
+
const sb = b[i];
|
|
197
|
+
if (sa.type !== sb.type) return false;
|
|
198
|
+
if (sa.type === "text") {
|
|
199
|
+
if (sb.type !== "text" || sa.text !== sb.text) return false;
|
|
200
|
+
} else {
|
|
201
|
+
if (sb.type !== "chip" || sa.trigger !== sb.trigger || sa.value !== sb.value || sa.displayText !== sb.displayText || sa.autoResolved !== sb.autoResolved)
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
function mergeAdjacentTextSegments(segments) {
|
|
208
|
+
const result = [];
|
|
209
|
+
for (const seg of segments) {
|
|
210
|
+
if (seg.type === "text" && seg.text === "") continue;
|
|
211
|
+
const last = result[result.length - 1];
|
|
212
|
+
if (seg.type === "text" && last?.type === "text") {
|
|
213
|
+
result[result.length - 1] = { type: "text", text: last.text + seg.text };
|
|
214
|
+
} else {
|
|
215
|
+
result.push(seg);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/prompt-area/segment-helpers.ts
|
|
222
|
+
function text(value) {
|
|
223
|
+
return { type: "text", text: value };
|
|
224
|
+
}
|
|
225
|
+
function chip(opts) {
|
|
226
|
+
return { type: "chip", ...opts };
|
|
227
|
+
}
|
|
228
|
+
function isSegmentsEmpty(segments) {
|
|
229
|
+
if (segments.length === 0) return true;
|
|
230
|
+
return segments.every((seg) => seg.type === "text" && seg.text.trim() === "");
|
|
231
|
+
}
|
|
232
|
+
function hasChips(segments) {
|
|
233
|
+
return segments.some((seg) => seg.type === "chip");
|
|
234
|
+
}
|
|
235
|
+
function getChips(segments) {
|
|
236
|
+
return segments.filter((seg) => seg.type === "chip");
|
|
237
|
+
}
|
|
238
|
+
function getChipsByTrigger(segments, trigger) {
|
|
239
|
+
return segments.filter(
|
|
240
|
+
(seg) => seg.type === "chip" && seg.trigger === trigger
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/prompt-area/trigger-presets.ts
|
|
245
|
+
function mentionTrigger(opts = {}) {
|
|
246
|
+
const { char = "@", ...rest } = opts;
|
|
247
|
+
return {
|
|
248
|
+
char,
|
|
249
|
+
position: "any",
|
|
250
|
+
mode: "dropdown",
|
|
251
|
+
chipStyle: "pill",
|
|
252
|
+
accessibilityLabel: "mention",
|
|
253
|
+
...rest
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function commandTrigger(opts = {}) {
|
|
257
|
+
const { char = "/", ...rest } = opts;
|
|
258
|
+
return {
|
|
259
|
+
char,
|
|
260
|
+
position: "start",
|
|
261
|
+
mode: "dropdown",
|
|
262
|
+
chipStyle: "inline",
|
|
263
|
+
accessibilityLabel: "command",
|
|
264
|
+
...rest
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
function hashtagTrigger(opts = {}) {
|
|
268
|
+
const { char = "#", ...rest } = opts;
|
|
269
|
+
return {
|
|
270
|
+
char,
|
|
271
|
+
position: "any",
|
|
272
|
+
mode: "dropdown",
|
|
273
|
+
chipStyle: "pill",
|
|
274
|
+
resolveOnSpace: true,
|
|
275
|
+
accessibilityLabel: "tag",
|
|
276
|
+
...rest
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function callbackTrigger(opts) {
|
|
280
|
+
const { char, ...rest } = opts;
|
|
281
|
+
return {
|
|
282
|
+
char,
|
|
283
|
+
position: "start",
|
|
284
|
+
mode: "callback",
|
|
285
|
+
...rest
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export { callbackTrigger, chip, commandTrigger, detectActiveTrigger, getChips, getChipsByTrigger, hasChips, hashtagTrigger, isSegmentsEmpty, isValidTriggerPosition, mentionTrigger, mergeAdjacentTextSegments, parseInlineMarkdown, plainTextToSegments, resolveChip, resolveTriggersInSegments, segmentsEqual, segmentsToPlainText, text };
|
|
290
|
+
//# sourceMappingURL=index.js.map
|
|
291
|
+
//# sourceMappingURL=index.js.map
|