@magicx-eng/ai-autocomplete-vanilla 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/index.d.mts ADDED
@@ -0,0 +1,168 @@
1
+ type TaskKind = "automation" | "email" | "insight";
2
+ interface CompletedParam {
3
+ placeholder: string;
4
+ type: string;
5
+ text?: string;
6
+ kind: TaskKind | null;
7
+ }
8
+ interface SuggestionOption {
9
+ text: string;
10
+ icon?: string;
11
+ tag?: string;
12
+ is_tappable: boolean;
13
+ kind: TaskKind | null;
14
+ metadata?: Record<string, unknown>;
15
+ }
16
+ interface Suggestion {
17
+ type: string;
18
+ text: string;
19
+ required: boolean;
20
+ options?: SuggestionOption[];
21
+ }
22
+ interface CompletedParamState extends CompletedParam {
23
+ id: string;
24
+ text: string;
25
+ suggestionType: string;
26
+ suggestionPlaceholder: string;
27
+ options: SuggestionOption[];
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+ type Segment = {
31
+ type: "text";
32
+ value: string;
33
+ } | {
34
+ type: "completed";
35
+ value: string;
36
+ param: CompletedParamState;
37
+ };
38
+ type AppearanceMode = "light" | "dark" | "auto";
39
+ interface APIConfigBase {
40
+ endpoint?: string;
41
+ appIdentifier?: string;
42
+ headers?: Record<string, string>;
43
+ }
44
+ interface APIKeyConfig extends APIConfigBase {
45
+ type?: "apiKey";
46
+ apiKey: string;
47
+ authScheme?: "Bearer" | "Basic";
48
+ }
49
+ interface AccessTokenConfig extends APIConfigBase {
50
+ type: "accessToken";
51
+ accessToken?: string;
52
+ getAccessToken: () => Promise<AccessTokenResult>;
53
+ }
54
+ interface AccessTokenResult {
55
+ accessToken: string;
56
+ /** UNIX ms. If provided, SDK refreshes proactively 30s before expiry. */
57
+ expiresAt?: number;
58
+ }
59
+ type APIConfig = APIKeyConfig | AccessTokenConfig;
60
+ type OptionOverrides = Record<string, (query: string) => SuggestionOption[]>;
61
+ interface AutocompleteResult {
62
+ query: string;
63
+ raw_query: string;
64
+ completed_params: CompletedParam[];
65
+ }
66
+
67
+ type Listener<S> = (next: S, prev: S) => void;
68
+ interface Store<S> {
69
+ get: () => S;
70
+ set: (patch: Partial<S> | ((s: S) => Partial<S>)) => void;
71
+ subscribe: (listener: Listener<S>) => () => void;
72
+ }
73
+ declare function createStore<S>(initial: S): Store<S>;
74
+
75
+ /** Internal state managed by the store. */
76
+ interface CoreState {
77
+ text: string;
78
+ completedParams: CompletedParamState[];
79
+ suggestions: Suggestion[];
80
+ activeDropdownIndex: number;
81
+ newParamId: string | null;
82
+ isLoading: boolean;
83
+ isReady: boolean;
84
+ error: Error | null;
85
+ segments: Segment[];
86
+ actionableSuggestions: Suggestion[];
87
+ filteredOptions: SuggestionOption[];
88
+ placeholderText: string;
89
+ isDropdownOpen: boolean;
90
+ filterBase: number;
91
+ filterInProgress: boolean;
92
+ pillTapped: boolean;
93
+ skipNextFetch: boolean;
94
+ lastRawQuery: string;
95
+ }
96
+ /** Options passed to the AIAutocomplete constructor. */
97
+ interface CoreOptions {
98
+ apiConfig?: APIConfig;
99
+ optionOverrides?: OptionOverrides;
100
+ maskCompletedText?: boolean;
101
+ placeholder?: string;
102
+ columns?: number;
103
+ typewriterEffect?: boolean;
104
+ pillPlacement?: "inline" | "dropdown";
105
+ mode?: AppearanceMode;
106
+ optionsPosition?: "above" | "below";
107
+ animations?: boolean;
108
+ onSubmit?: (result: AutocompleteResult) => void;
109
+ onError?: (error: Error) => void;
110
+ onChange?: (text: string) => void;
111
+ onParamsChange?: (params: CompletedParamState[]) => void;
112
+ onStateChange?: (state: CoreState) => void;
113
+ value?: string;
114
+ completedParams?: CompletedParamState[];
115
+ }
116
+
117
+ interface KeyboardContext {
118
+ columns: number;
119
+ listboxId: string;
120
+ onSubmit?: (result: AutocompleteResult) => void;
121
+ selectOption: (option: SuggestionOption) => void;
122
+ }
123
+ declare class KeyboardController {
124
+ private store;
125
+ private ctx;
126
+ constructor(store: Store<CoreState>, ctx: KeyboardContext);
127
+ handleKeyDown(e: KeyboardEvent): void;
128
+ private getTappableIndices;
129
+ private clickOrSelect;
130
+ /** Delegate to pills controller logic inline (avoids circular dep) */
131
+ private pillsSetActivePill;
132
+ }
133
+
134
+ declare class AIAutocomplete {
135
+ private store;
136
+ private listboxId;
137
+ private opts;
138
+ private fetchController;
139
+ keyboardController: KeyboardController;
140
+ private pillsController;
141
+ private modeController;
142
+ private container;
143
+ private unsubscribers;
144
+ private derivedInProgress;
145
+ private domRefs;
146
+ private newParamTimer;
147
+ constructor(container: HTMLElement, opts?: CoreOptions);
148
+ focus(): void;
149
+ reset(): void;
150
+ destroy(): void;
151
+ setMode(mode: AppearanceMode): void;
152
+ setValue(text: string): void;
153
+ setCompletedParams(params: CompletedParamState[]): void;
154
+ setActivePill(index: number): void;
155
+ removeLastParam(): void;
156
+ clearNewParamId(): void;
157
+ getState(): CoreState;
158
+ get isReady(): boolean;
159
+ on(event: string, callback: (...args: unknown[]) => void): () => void;
160
+ update(opts: Partial<CoreOptions>): void;
161
+ private selectOption;
162
+ private recomputeDerived;
163
+ private setupContainer;
164
+ private buildAndRender;
165
+ private handleChange;
166
+ }
167
+
168
+ export { AIAutocomplete, type APIConfig, type APIKeyConfig, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, type Segment, type Store, type Suggestion, type SuggestionOption, createStore };
package/index.d.ts ADDED
@@ -0,0 +1,168 @@
1
+ type TaskKind = "automation" | "email" | "insight";
2
+ interface CompletedParam {
3
+ placeholder: string;
4
+ type: string;
5
+ text?: string;
6
+ kind: TaskKind | null;
7
+ }
8
+ interface SuggestionOption {
9
+ text: string;
10
+ icon?: string;
11
+ tag?: string;
12
+ is_tappable: boolean;
13
+ kind: TaskKind | null;
14
+ metadata?: Record<string, unknown>;
15
+ }
16
+ interface Suggestion {
17
+ type: string;
18
+ text: string;
19
+ required: boolean;
20
+ options?: SuggestionOption[];
21
+ }
22
+ interface CompletedParamState extends CompletedParam {
23
+ id: string;
24
+ text: string;
25
+ suggestionType: string;
26
+ suggestionPlaceholder: string;
27
+ options: SuggestionOption[];
28
+ metadata?: Record<string, unknown>;
29
+ }
30
+ type Segment = {
31
+ type: "text";
32
+ value: string;
33
+ } | {
34
+ type: "completed";
35
+ value: string;
36
+ param: CompletedParamState;
37
+ };
38
+ type AppearanceMode = "light" | "dark" | "auto";
39
+ interface APIConfigBase {
40
+ endpoint?: string;
41
+ appIdentifier?: string;
42
+ headers?: Record<string, string>;
43
+ }
44
+ interface APIKeyConfig extends APIConfigBase {
45
+ type?: "apiKey";
46
+ apiKey: string;
47
+ authScheme?: "Bearer" | "Basic";
48
+ }
49
+ interface AccessTokenConfig extends APIConfigBase {
50
+ type: "accessToken";
51
+ accessToken?: string;
52
+ getAccessToken: () => Promise<AccessTokenResult>;
53
+ }
54
+ interface AccessTokenResult {
55
+ accessToken: string;
56
+ /** UNIX ms. If provided, SDK refreshes proactively 30s before expiry. */
57
+ expiresAt?: number;
58
+ }
59
+ type APIConfig = APIKeyConfig | AccessTokenConfig;
60
+ type OptionOverrides = Record<string, (query: string) => SuggestionOption[]>;
61
+ interface AutocompleteResult {
62
+ query: string;
63
+ raw_query: string;
64
+ completed_params: CompletedParam[];
65
+ }
66
+
67
+ type Listener<S> = (next: S, prev: S) => void;
68
+ interface Store<S> {
69
+ get: () => S;
70
+ set: (patch: Partial<S> | ((s: S) => Partial<S>)) => void;
71
+ subscribe: (listener: Listener<S>) => () => void;
72
+ }
73
+ declare function createStore<S>(initial: S): Store<S>;
74
+
75
+ /** Internal state managed by the store. */
76
+ interface CoreState {
77
+ text: string;
78
+ completedParams: CompletedParamState[];
79
+ suggestions: Suggestion[];
80
+ activeDropdownIndex: number;
81
+ newParamId: string | null;
82
+ isLoading: boolean;
83
+ isReady: boolean;
84
+ error: Error | null;
85
+ segments: Segment[];
86
+ actionableSuggestions: Suggestion[];
87
+ filteredOptions: SuggestionOption[];
88
+ placeholderText: string;
89
+ isDropdownOpen: boolean;
90
+ filterBase: number;
91
+ filterInProgress: boolean;
92
+ pillTapped: boolean;
93
+ skipNextFetch: boolean;
94
+ lastRawQuery: string;
95
+ }
96
+ /** Options passed to the AIAutocomplete constructor. */
97
+ interface CoreOptions {
98
+ apiConfig?: APIConfig;
99
+ optionOverrides?: OptionOverrides;
100
+ maskCompletedText?: boolean;
101
+ placeholder?: string;
102
+ columns?: number;
103
+ typewriterEffect?: boolean;
104
+ pillPlacement?: "inline" | "dropdown";
105
+ mode?: AppearanceMode;
106
+ optionsPosition?: "above" | "below";
107
+ animations?: boolean;
108
+ onSubmit?: (result: AutocompleteResult) => void;
109
+ onError?: (error: Error) => void;
110
+ onChange?: (text: string) => void;
111
+ onParamsChange?: (params: CompletedParamState[]) => void;
112
+ onStateChange?: (state: CoreState) => void;
113
+ value?: string;
114
+ completedParams?: CompletedParamState[];
115
+ }
116
+
117
+ interface KeyboardContext {
118
+ columns: number;
119
+ listboxId: string;
120
+ onSubmit?: (result: AutocompleteResult) => void;
121
+ selectOption: (option: SuggestionOption) => void;
122
+ }
123
+ declare class KeyboardController {
124
+ private store;
125
+ private ctx;
126
+ constructor(store: Store<CoreState>, ctx: KeyboardContext);
127
+ handleKeyDown(e: KeyboardEvent): void;
128
+ private getTappableIndices;
129
+ private clickOrSelect;
130
+ /** Delegate to pills controller logic inline (avoids circular dep) */
131
+ private pillsSetActivePill;
132
+ }
133
+
134
+ declare class AIAutocomplete {
135
+ private store;
136
+ private listboxId;
137
+ private opts;
138
+ private fetchController;
139
+ keyboardController: KeyboardController;
140
+ private pillsController;
141
+ private modeController;
142
+ private container;
143
+ private unsubscribers;
144
+ private derivedInProgress;
145
+ private domRefs;
146
+ private newParamTimer;
147
+ constructor(container: HTMLElement, opts?: CoreOptions);
148
+ focus(): void;
149
+ reset(): void;
150
+ destroy(): void;
151
+ setMode(mode: AppearanceMode): void;
152
+ setValue(text: string): void;
153
+ setCompletedParams(params: CompletedParamState[]): void;
154
+ setActivePill(index: number): void;
155
+ removeLastParam(): void;
156
+ clearNewParamId(): void;
157
+ getState(): CoreState;
158
+ get isReady(): boolean;
159
+ on(event: string, callback: (...args: unknown[]) => void): () => void;
160
+ update(opts: Partial<CoreOptions>): void;
161
+ private selectOption;
162
+ private recomputeDerived;
163
+ private setupContainer;
164
+ private buildAndRender;
165
+ private handleChange;
166
+ }
167
+
168
+ export { AIAutocomplete, type APIConfig, type APIKeyConfig, type AccessTokenConfig, type AccessTokenResult, type AppearanceMode, type AutocompleteResult, type CompletedParam, type CompletedParamState, type CoreOptions, type CoreState, type Segment, type Store, type Suggestion, type SuggestionOption, createStore };