orchid-ai 1.0.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.
Files changed (52) hide show
  1. package/README.md +225 -0
  2. package/dist/components/ChatPanel.d.ts +123 -0
  3. package/dist/components/Conversation.d.ts +75 -0
  4. package/dist/components/ErrorBoundary.d.ts +16 -0
  5. package/dist/components/Icon.d.ts +84 -0
  6. package/dist/components/ModelSwitcher.d.ts +24 -0
  7. package/dist/components/SuggestionsPanel.d.ts +27 -0
  8. package/dist/constants.d.ts +353 -0
  9. package/dist/hooks/useAiMerge.d.ts +20 -0
  10. package/dist/hooks/useModelSwitcher.d.ts +65 -0
  11. package/dist/hooks/useStreamingAI.d.ts +29 -0
  12. package/dist/hooks/useSuggestions.d.ts +48 -0
  13. package/dist/index.d.ts +13 -0
  14. package/dist/index.esm.js +3385 -0
  15. package/dist/index.js +3400 -0
  16. package/dist/server/components/ChatPanel.d.ts +123 -0
  17. package/dist/server/components/Conversation.d.ts +75 -0
  18. package/dist/server/components/ErrorBoundary.d.ts +16 -0
  19. package/dist/server/components/Icon.d.ts +84 -0
  20. package/dist/server/components/ModelSwitcher.d.ts +24 -0
  21. package/dist/server/components/SuggestionsPanel.d.ts +27 -0
  22. package/dist/server/constants.d.ts +353 -0
  23. package/dist/server/contextual-service.d.ts +59 -0
  24. package/dist/server/document-processor.d.ts +60 -0
  25. package/dist/server/hooks/useAiMerge.d.ts +20 -0
  26. package/dist/server/hooks/useModelSwitcher.d.ts +65 -0
  27. package/dist/server/hooks/useStreamingAI.d.ts +29 -0
  28. package/dist/server/hooks/useSuggestions.d.ts +48 -0
  29. package/dist/server/index.d.ts +7 -0
  30. package/dist/server/index.esm.js +14008 -0
  31. package/dist/server/index.js +14019 -0
  32. package/dist/server/server/contextual-service.d.ts +59 -0
  33. package/dist/server/server/document-processor.d.ts +60 -0
  34. package/dist/server/server/index.d.ts +7 -0
  35. package/dist/server/server/server.d.ts +32 -0
  36. package/dist/server/server/service.d.ts +267 -0
  37. package/dist/server/server/training-schema.d.ts +17 -0
  38. package/dist/server/server/training.d.ts +231 -0
  39. package/dist/server/server/utils.d.ts +209 -0
  40. package/dist/server/server.d.ts +32 -0
  41. package/dist/server/service.d.ts +267 -0
  42. package/dist/server/training-schema.d.ts +17 -0
  43. package/dist/server/training.d.ts +231 -0
  44. package/dist/server/types/types.d.ts +481 -0
  45. package/dist/server/utils/fileHandler.d.ts +20 -0
  46. package/dist/server/utils/mergeWithAi.d.ts +19 -0
  47. package/dist/server/utils.d.ts +209 -0
  48. package/dist/types/types.d.ts +481 -0
  49. package/dist/utils/fileHandler.d.ts +20 -0
  50. package/dist/utils/mergeWithAi.d.ts +19 -0
  51. package/dist/utils.d.ts +19 -0
  52. package/package.json +137 -0
@@ -0,0 +1,209 @@
1
+ import { AnalysisContext, CommandSuggestion, Intent, RouteConfig, TrainingData } from '../types/types';
2
+ export declare function sanitizeFormData(formData: Record<string, any> | undefined): Record<string, any>;
3
+ export declare function handleError(error: unknown, prefix?: string, verboseLogging?: boolean): void;
4
+ export declare function extractFirstJsonArray(text: string): any;
5
+ export declare function getModelSupportsImages(modelId: string, provider: string): boolean;
6
+ /**
7
+ * Check if the current model supports image input
8
+ * @returns {boolean} True if current model supports images
9
+ */
10
+ export declare function currentModelSupportsImages(): string;
11
+ export declare function getDefaultModelsForProvider(provider: string): {
12
+ id: string;
13
+ name: string;
14
+ provider: string;
15
+ available: boolean;
16
+ supportsImages: boolean;
17
+ }[] | {
18
+ id: string;
19
+ name: string;
20
+ provider: string;
21
+ available: boolean;
22
+ supportsImages: boolean;
23
+ }[] | {
24
+ id: string;
25
+ name: string;
26
+ provider: string;
27
+ available: boolean;
28
+ supportsImages: boolean;
29
+ }[];
30
+ /**
31
+ * Enhanced context analysis for better AI suggestions
32
+ * Analyzes chat history to extract relevant context for form state management
33
+ */
34
+ export declare function analyzeContext(chatHistory: any[], currentCommand: string): AnalysisContext;
35
+ /**
36
+ * Extract entities (quotes, customers, shipments) from message content
37
+ */
38
+ export declare function extractEntitiesFromMessage(content: string, context: any): void;
39
+ /**
40
+ * Extract form state information from message content
41
+ */
42
+ export declare function extractFormStateFromMessage(content: string, context: any): void;
43
+ /**
44
+ * Extract user preferences and patterns from message content
45
+ */
46
+ export declare function extractUserPreferencesFromMessage(content: string, context: any): void;
47
+ /**
48
+ * Track recent actions for continuity
49
+ */
50
+ export declare function trackRecentActions(content: string, context: any): void;
51
+ /**
52
+ * Extract data from current command
53
+ */
54
+ export declare function extractDataFromCommand(command: string, context: any): void;
55
+ /**
56
+ * Classify user intent based on command and context
57
+ */
58
+ export declare function classifyIntent(command: string, context: any): Intent;
59
+ /**
60
+ * Determine action type based on context and intent
61
+ */
62
+ export declare function determineActionType(context: any, intent: string | null): "create" | "edit" | "view";
63
+ /**
64
+ * Helper: Parse and fix JSON response from any provider
65
+ */
66
+ export declare function parseAndFixJsonResponse(content: string, provider: string): any[];
67
+ /**
68
+ * Helper: Safely parse JSON that might contain JavaScript expressions
69
+ * Replaces common JS expressions (e.g., new Date(), Date.now()) with JSON-safe values
70
+ * Returns parsed object/array on success, or null on failure
71
+ */
72
+ export declare function parseJsonWithExpressions(str: string): any;
73
+ /**
74
+ * Helper: Enhance suggestions with actionType and context
75
+ */
76
+ export declare function enhanceSuggestions(suggestions: any, context: any): CommandSuggestion[];
77
+ /**
78
+ * Helper: Create fallback suggestions
79
+ */
80
+ export declare function createFallbackSuggestions(message?: string): {
81
+ action: string;
82
+ actionType: string;
83
+ path: string;
84
+ formState: {};
85
+ message: string;
86
+ }[];
87
+ /**
88
+ * Dynamic route validation based on training data
89
+ */
90
+ export declare function isValidRoute(path: string, routes: RouteConfig[]): boolean;
91
+ /**
92
+ * Unified JSON extraction function for all models
93
+ * Extracts ALL JSON objects from text and maintains their order
94
+ */
95
+ export declare function extractAllJsonFromText(text: string, provider: string): Array<{
96
+ type: 'array' | 'object';
97
+ content: string;
98
+ startIndex: number;
99
+ endIndex: number;
100
+ }>;
101
+ /**
102
+ * Check if JSON is actually complete (not just balanced brackets)
103
+ */
104
+ export declare function isJsonActuallyComplete(str: string): boolean;
105
+ /**
106
+ * Enhanced JSON depth tracking that handles nested structures properly
107
+ */
108
+ export declare function calculateJsonDepth(str: string): number;
109
+ /**
110
+ * Enhanced suggestion extraction with better error handling
111
+ */
112
+ export declare function extractSuggestions(jsonStr: string): any;
113
+ /**
114
+ * Extract JSON from streamed text, handling both markdown code blocks and raw JSON
115
+ * Returns the cleaned text and extracted JSON with completion status
116
+ */
117
+ export declare function extractJsonFromStream(input: string): {
118
+ text: string;
119
+ json: string;
120
+ isJsonComplete: boolean;
121
+ isMarkdownBlock: boolean;
122
+ };
123
+ /**
124
+ * Extract partial JSON from potentially incomplete string
125
+ */
126
+ export declare function extractPartialJson(str: string): string | null;
127
+ /**
128
+ * Validate JSON structure and attempt to fix common issues
129
+ * Now includes bulletproof JavaScript expression evaluation
130
+ */
131
+ export declare function validateAndFixJson(str: string): string | null;
132
+ /**
133
+ * Bulletproof JavaScript expression evaluator
134
+ * Safely evaluates common JavaScript expressions in JSON strings
135
+ */
136
+ export declare function evaluateJavaScriptExpressions(str: string): string;
137
+ export declare function buildSystemPrompt(trainingData: TrainingData | undefined, chatLevel: 'full' | 'basic' | 'none' | undefined, provider?: string): string;
138
+ /**
139
+ * Get chat level specific instructions
140
+ * CRITICAL: These contain the JSON format requirements and must be preserved
141
+ */
142
+ export declare function getChatLevelInstructions(chatLevel: 'full' | 'basic' | 'none' | undefined): string;
143
+ export declare function prepareMessageWithImages(command: string, images: any[], provider?: string | null): Promise<string | ({
144
+ type: string;
145
+ source: {
146
+ type: string;
147
+ media_type: any;
148
+ data: any;
149
+ };
150
+ image_url?: undefined;
151
+ inlineData?: undefined;
152
+ } | {
153
+ type: string;
154
+ image_url: {
155
+ url: string;
156
+ };
157
+ source?: undefined;
158
+ inlineData?: undefined;
159
+ } | {
160
+ inlineData: {
161
+ mimeType: any;
162
+ data: any;
163
+ };
164
+ type?: undefined;
165
+ source?: undefined;
166
+ image_url?: undefined;
167
+ } | {
168
+ text: string;
169
+ type?: undefined;
170
+ } | {
171
+ type: string;
172
+ text: string;
173
+ })[]>;
174
+ export declare function processImageForAI(image: any, provider?: string | null): Promise<{
175
+ type: string;
176
+ source: {
177
+ type: string;
178
+ media_type: any;
179
+ data: any;
180
+ };
181
+ image_url?: undefined;
182
+ inlineData?: undefined;
183
+ } | {
184
+ type: string;
185
+ image_url: {
186
+ url: string;
187
+ };
188
+ source?: undefined;
189
+ inlineData?: undefined;
190
+ } | {
191
+ inlineData: {
192
+ mimeType: any;
193
+ data: any;
194
+ };
195
+ type?: undefined;
196
+ source?: undefined;
197
+ image_url?: undefined;
198
+ }>;
199
+ export declare function detectImageTypeFromBase64(base64Data: string): "image/jpeg" | "image/png" | "image/gif" | "image/webp" | "image/heic" | "image/heif";
200
+ /**
201
+ * Comprehensive text cleaning utility that removes JSON blocks and related content
202
+ * This consolidates all the scattered cleaning logic into one place
203
+ */
204
+ export declare function cleanStreamedText(text: string): string;
205
+ /**
206
+ * Detect if text contains the start of a JSON code block
207
+ * Used by frontend to show loading suggestions component
208
+ */
209
+ export declare function detectJsonBlockStart(text: string): boolean;
@@ -0,0 +1,32 @@
1
+ import type { CommandConfig, RouteConfig, TrainingCustomData, AIProvider, AIServiceConfig } from '../types/types.js';
2
+ export interface ServerOptions extends AIServiceConfig {
3
+ port?: number;
4
+ baseDir?: string;
5
+ service?: AIProvider;
6
+ apiKey?: string;
7
+ customTrainingData?: TrainingCustomData;
8
+ routes?: (string | RouteConfig)[];
9
+ }
10
+ export interface CommandServer {
11
+ app: any;
12
+ start: () => void;
13
+ stop: () => void;
14
+ getConfig: () => CommandConfig;
15
+ }
16
+ /**
17
+ * Helper function to format error messages for user-friendly display
18
+ */
19
+ export declare function formatErrorForUser(error: Error): {
20
+ message: string;
21
+ type: string;
22
+ retryAfter?: number;
23
+ };
24
+ /**
25
+ * Setup a command server with training data collection and streaming support.
26
+ * Works both as a standalone server and as middleware for existing Express apps.
27
+ */
28
+ export declare function setupCommandServer(options?: ServerOptions): Promise<CommandServer>;
29
+ /**
30
+ * Create a command configuration by merging training data from multiple sources
31
+ */
32
+ export declare function createCommandConfig(options: ServerOptions): Promise<CommandConfig>;
@@ -0,0 +1,267 @@
1
+ import { OpenAI } from 'openai';
2
+ import { GoogleGenerativeAI } from '@google/generative-ai';
3
+ import Anthropic from '@anthropic-ai/sdk';
4
+ import { TrainingDataPaths, AIServiceConfig, ImageContent } from '../types/types.js';
5
+ export declare class CommandService {
6
+ private aiConfig;
7
+ private verboseLogging;
8
+ private model;
9
+ private modelConfig;
10
+ private usage;
11
+ constructor(config: AIServiceConfig, verboseLogging?: boolean);
12
+ private autoConfigureProviders;
13
+ private log;
14
+ private handleError;
15
+ /**
16
+ * Initialize the service with training data
17
+ * This must be called before using the service
18
+ */
19
+ initialize(): Promise<void>;
20
+ initializeTrainingData(): Promise<void>;
21
+ writeConfigToFile(): Promise<void>;
22
+ writeFailedJsonToFile(failedJson: string, errorMessage: string): Promise<void>;
23
+ tryPrettifyJson(jsonStr: string): string;
24
+ /**
25
+ * Build model configuration with current training data
26
+ */
27
+ buildModelConfig(): {
28
+ claude: {
29
+ model: string | null | undefined;
30
+ system: string;
31
+ max_tokens: number;
32
+ temperature: number;
33
+ };
34
+ openai: {
35
+ model: string | null | undefined;
36
+ messages: {
37
+ role: string;
38
+ content: string;
39
+ }[];
40
+ temperature: number;
41
+ max_tokens: number;
42
+ };
43
+ gemini: {
44
+ model: string | null | undefined;
45
+ systemInstruction: string;
46
+ generationConfig: {
47
+ temperature: number;
48
+ maxOutputTokens: number;
49
+ };
50
+ };
51
+ };
52
+ extractPatterns(data: TrainingDataPaths): Map<string, Set<string>>;
53
+ updateSystemPrompt(patterns: Map<string, Set<string>>): string;
54
+ getAvailableModels(): Promise<any[]>;
55
+ createModelInstance(provider: string, apiKey: string): OpenAI | GoogleGenerativeAI | Anthropic;
56
+ getModelDisplayName(modelId: string): string;
57
+ getSingleProviderModels(): Promise<any>;
58
+ /**
59
+ * Get complete model state (models, current, capabilities, usage) in one call
60
+ * This reduces API calls and ensures data consistency
61
+ */
62
+ getModelState(): Promise<{
63
+ models: any[];
64
+ current: {
65
+ provider: string;
66
+ model: string;
67
+ };
68
+ capabilities: {
69
+ supportsImages: string;
70
+ };
71
+ usage: {
72
+ requests: number;
73
+ totalTokens: number;
74
+ totalComputeUnits: number;
75
+ modelSwitches: number;
76
+ uptime: number;
77
+ providers: {
78
+ name: string;
79
+ requests: number;
80
+ models: string[];
81
+ }[];
82
+ };
83
+ timestamp: number;
84
+ }>;
85
+ /**
86
+ * Create a model instance for a specific request without changing global state
87
+ */
88
+ createModelForRequest(modelSelection: {
89
+ provider: string;
90
+ model: string;
91
+ } | undefined): {
92
+ model: OpenAI | GoogleGenerativeAI | Anthropic;
93
+ config: {
94
+ openai: {
95
+ model: string;
96
+ temperature: number;
97
+ max_tokens: number;
98
+ messages: {
99
+ role: string;
100
+ content: string;
101
+ }[];
102
+ };
103
+ claude?: undefined;
104
+ gemini?: undefined;
105
+ } | {
106
+ claude: {
107
+ model: string;
108
+ temperature: number;
109
+ max_tokens: number;
110
+ system: string;
111
+ messages: never[];
112
+ };
113
+ openai?: undefined;
114
+ gemini?: undefined;
115
+ } | {
116
+ gemini: {
117
+ model: string;
118
+ temperature: number;
119
+ maxOutputTokens: number;
120
+ systemInstruction: string;
121
+ generationConfig: {
122
+ temperature: number;
123
+ maxOutputTokens: number;
124
+ candidateCount: number;
125
+ };
126
+ };
127
+ openai?: undefined;
128
+ claude?: undefined;
129
+ };
130
+ provider: string;
131
+ modelId: string;
132
+ };
133
+ /**
134
+ * Get API key for a specific provider
135
+ */
136
+ getApiKeyForProvider(provider: string): string | null | undefined;
137
+ /**
138
+ * Build model configuration for a specific request
139
+ */
140
+ buildModelConfigForRequest(provider: string, model: string): {
141
+ openai: {
142
+ model: string;
143
+ temperature: number;
144
+ max_tokens: number;
145
+ messages: {
146
+ role: string;
147
+ content: string;
148
+ }[];
149
+ };
150
+ claude?: undefined;
151
+ gemini?: undefined;
152
+ } | {
153
+ claude: {
154
+ model: string;
155
+ temperature: number;
156
+ max_tokens: number;
157
+ system: string;
158
+ messages: never[];
159
+ };
160
+ openai?: undefined;
161
+ gemini?: undefined;
162
+ } | {
163
+ gemini: {
164
+ model: string;
165
+ temperature: number;
166
+ maxOutputTokens: number;
167
+ systemInstruction: string;
168
+ generationConfig: {
169
+ temperature: number;
170
+ maxOutputTokens: number;
171
+ candidateCount: number;
172
+ };
173
+ };
174
+ openai?: undefined;
175
+ claude?: undefined;
176
+ };
177
+ /**
178
+ * Get suggestions for a command
179
+ * @param command - The command to get suggestions for
180
+ * @param chatHistory - The chat history
181
+ * @param retryCount - The number of retries
182
+ * @param chatLevel - The chat level
183
+ * @param modelSelection - The model selection
184
+ * @returns The suggestions
185
+ */
186
+ getSuggestions(command: string, chatHistory: any[] | undefined, retryCount: number | undefined, chatLevel: 'full' | 'basic' | 'none' | undefined, modelSelection: {
187
+ provider: string;
188
+ model: string;
189
+ } | undefined, context?: string): Promise<Array<{
190
+ action?: string;
191
+ path?: string;
192
+ formState?: Record<string, any>;
193
+ [key: string]: any;
194
+ }>>;
195
+ /**
196
+ * Check if a response is effectively empty and needs retry
197
+ */
198
+ isEmptyResponse(text: string, suggestions?: any[] | null): boolean;
199
+ /**
200
+ * Create a retry prompt that encourages more helpful responses
201
+ */
202
+ createRetryPrompt(originalCommand: string, retryCount: number): string;
203
+ streamSuggestions(command: string, userId: string, chatHistory: any[], onData: (data: any) => void, onDone: () => void, onError: (error: Error) => void, images?: any[], retryCount?: number, chatLevel?: 'full' | 'basic' | 'none' | undefined, modelSelection?: {
204
+ provider: string;
205
+ model: string;
206
+ capabilities: {
207
+ computeWeight: number;
208
+ imageSupport: boolean;
209
+ };
210
+ }, context?: string): Promise<void>;
211
+ getSuggestionsWithImages(command: string, chatHistory: any[], images: any[], modelSelection?: {
212
+ provider: string;
213
+ model: string;
214
+ } | undefined): Promise<{
215
+ formState: Record<string, any>;
216
+ action?: string;
217
+ actionType: import("../types/types.js").Action;
218
+ path?: string;
219
+ queryParams?: Record<string, string>;
220
+ message?: string;
221
+ user?: import("../types/types.js").User;
222
+ initials?: string;
223
+ color?: string;
224
+ shortcut?: string;
225
+ title?: string;
226
+ description?: string;
227
+ }[]>;
228
+ getUsageStats(): {
229
+ requests: number;
230
+ totalTokens: number;
231
+ totalComputeUnits: number;
232
+ modelSwitches: number;
233
+ uptime: number;
234
+ providers: {
235
+ name: string;
236
+ requests: number;
237
+ models: string[];
238
+ }[];
239
+ };
240
+ resetUsageStats(): void;
241
+ trackRequest(provider: string, tokenCount?: number): void;
242
+ /**
243
+ * Helper: Refine fields that require refinement
244
+ */
245
+ refineFieldsIfNeeded(suggestions: any, context: any, messages?: any[], provider?: string | null): Promise<any>;
246
+ /**
247
+ * Helper: Get raw response content from any provider
248
+ */
249
+ getProviderResponse(command: string | ImageContent[], chatHistory: any[] | undefined, messages?: any[], provider?: string | null): Promise<any>;
250
+ /**
251
+ * Helper: Process suggestions with full pipeline
252
+ */
253
+ processSuggestionsPipeline(suggestions: any[], context: any, messages?: any[], provider?: string | null): Promise<{
254
+ formState: Record<string, any>;
255
+ action?: string;
256
+ actionType: import("../types/types.js").Action;
257
+ path?: string;
258
+ queryParams?: Record<string, string>;
259
+ message?: string;
260
+ user?: import("../types/types.js").User;
261
+ initials?: string;
262
+ color?: string;
263
+ shortcut?: string;
264
+ title?: string;
265
+ description?: string;
266
+ }[]>;
267
+ }
@@ -0,0 +1,17 @@
1
+ import type { TrainingConfig } from '../types/types.js';
2
+ /**
3
+ * Validate training configuration
4
+ */
5
+ export declare function validateTrainingConfig(config: TrainingConfig): {
6
+ isValid: boolean;
7
+ errors: string[];
8
+ };
9
+ /**
10
+ * Create a normalized training configuration
11
+ */
12
+ export declare function createTrainingConfig(options?: Partial<TrainingConfig>): TrainingConfig;
13
+ /**
14
+ * Gets the expected file structure for training data
15
+ * @returns Expected file structure
16
+ */
17
+ export declare function getExpectedFileStructure(): Record<string, string>;