genai-lite 0.7.0 → 0.7.2

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.
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Format options for prepending system content when model doesn't support system messages.
3
+ * - 'xml': Wrap in XML tags (default) - `<system>content</system>\n\n{user message}`
4
+ * - 'separator': Use a custom separator string - `{content}{separator}{user message}`
5
+ * Default separator is `\n\n---\n\n`. Specify full separator including whitespace.
6
+ * - 'plain': Just prepend with double newline - `{content}\n\n{user message}`
7
+ */
8
+ export type SystemMessageFallbackFormat = 'xml' | 'separator' | 'plain';
9
+ /**
10
+ * Options for formatting system content when prepending to user messages
11
+ */
12
+ export interface SystemMessageFormatOptions {
13
+ /**
14
+ * Format to use when prepending system content to user message.
15
+ * @default 'xml'
16
+ */
17
+ format?: SystemMessageFallbackFormat;
18
+ /**
19
+ * Tag name to use when format is 'xml'.
20
+ * @default 'system'
21
+ * @example tagName: 'instructions' produces `<instructions>content</instructions>`
22
+ */
23
+ tagName?: string;
24
+ /**
25
+ * Separator string to use when format is 'separator'.
26
+ * Include any whitespace/newlines in the separator itself.
27
+ * @default '\n\n---\n\n'
28
+ * @example separator: '\n\n===\n\n' produces `content\n\n===\n\nuser message`
29
+ */
30
+ separator?: string;
31
+ }
32
+ /**
33
+ * Result of collecting system content for fallback handling
34
+ */
35
+ export interface SystemContentResult {
36
+ /** Combined system content from all sources */
37
+ combinedSystemContent: string | undefined;
38
+ /** Whether to use native system message support */
39
+ useNativeSystemMessage: boolean;
40
+ }
41
+ /**
42
+ * Generic message interface for system message handling
43
+ */
44
+ export interface GenericMessage {
45
+ role: string;
46
+ content: string;
47
+ }
48
+ /**
49
+ * Default format options for system message fallback
50
+ */
51
+ export declare const DEFAULT_SYSTEM_MESSAGE_FORMAT_OPTIONS: Required<SystemMessageFormatOptions>;
52
+ /**
53
+ * Formats system content according to the specified format options.
54
+ *
55
+ * @param systemContent - The system content to format
56
+ * @param userContent - The original user message content
57
+ * @param options - Format options (defaults to XML with 'system' tag)
58
+ * @returns The formatted combined content
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // XML format (default)
63
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'xml' });
64
+ * // Returns: '<system>\nBe helpful\n</system>\n\nHello'
65
+ *
66
+ * // Separator format (default separator is '\n\n---\n\n')
67
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'separator' });
68
+ * // Returns: 'Be helpful\n\n---\n\nHello'
69
+ *
70
+ * // Custom separator (specify full separator including whitespace)
71
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'separator', separator: '\n\n===\n\n' });
72
+ * // Returns: 'Be helpful\n\n===\n\nHello'
73
+ *
74
+ * // Plain format
75
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'plain' });
76
+ * // Returns: 'Be helpful\n\nHello'
77
+ * ```
78
+ */
79
+ export declare function formatSystemContentForPrepend(systemContent: string, userContent: string, options?: SystemMessageFormatOptions): string;
80
+ /**
81
+ * Collects and combines system content from multiple sources.
82
+ *
83
+ * Combines the request-level systemMessage with any inline system messages
84
+ * from the messages array into a single string.
85
+ *
86
+ * @param requestSystemMessage - The request.systemMessage field (if any)
87
+ * @param inlineSystemMessages - Array of system message contents from the messages array
88
+ * @param supportsSystemMessage - Whether the model supports native system messages
89
+ * @returns Object with combined content and whether to use native support
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const { combinedSystemContent, useNativeSystemMessage } = collectSystemContent(
94
+ * 'You are helpful',
95
+ * ['Be concise'],
96
+ * false // Model doesn't support system messages
97
+ * );
98
+ * // combinedSystemContent = 'You are helpful\n\nBe concise'
99
+ * // useNativeSystemMessage = false
100
+ * ```
101
+ */
102
+ export declare function collectSystemContent(requestSystemMessage: string | undefined, inlineSystemMessages: string[], supportsSystemMessage: boolean): SystemContentResult;
103
+ /**
104
+ * Prepends system content to the first user message in an array.
105
+ *
106
+ * This is used as a fallback for models that don't support native system
107
+ * instructions. The system content is formatted according to the options
108
+ * and prepended to the first user message.
109
+ *
110
+ * @param messages - Array of message objects with role and content
111
+ * @param systemContent - System content to prepend
112
+ * @param options - Format options (defaults to XML with 'system' tag)
113
+ * @returns The index of the modified message, or -1 if no user message found
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * const messages = [
118
+ * { role: 'user', content: 'Hello' },
119
+ * { role: 'assistant', content: 'Hi!' }
120
+ * ];
121
+ *
122
+ * // Default (XML format)
123
+ * prependSystemToFirstUserMessage(messages, 'Be helpful');
124
+ * // messages[0].content = '<system>\nBe helpful\n</system>\n\nHello'
125
+ *
126
+ * // Plain format
127
+ * prependSystemToFirstUserMessage(messages, 'Be helpful', { format: 'plain' });
128
+ * // messages[0].content = 'Be helpful\n\nHello'
129
+ *
130
+ * // Separator format (default separator is '\n\n---\n\n')
131
+ * prependSystemToFirstUserMessage(messages, 'Be helpful', { format: 'separator' });
132
+ * // messages[0].content = 'Be helpful\n\n---\n\nHello'
133
+ * ```
134
+ */
135
+ export declare function prependSystemToFirstUserMessage<T extends GenericMessage>(messages: T[], systemContent: string, options?: SystemMessageFormatOptions): number;
136
+ /**
137
+ * Filters system messages from an array and returns non-system messages
138
+ * along with collected system content.
139
+ *
140
+ * This is a convenience function that combines message filtering with
141
+ * system content collection in a single pass.
142
+ *
143
+ * @param messages - Array of messages to process
144
+ * @param requestSystemMessage - Optional request-level system message
145
+ * @param supportsSystemMessage - Whether the model supports native system messages
146
+ * @returns Object with filtered messages and system content handling info
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * const messages = [
151
+ * { role: 'system', content: 'Be helpful' },
152
+ * { role: 'user', content: 'Hello' }
153
+ * ];
154
+ * const result = processMessagesForSystemSupport(messages, undefined, false);
155
+ * // result.nonSystemMessages = [{ role: 'user', content: 'Hello' }]
156
+ * // result.systemContent.combinedSystemContent = 'Be helpful'
157
+ * ```
158
+ */
159
+ export declare function processMessagesForSystemSupport<T extends GenericMessage>(messages: T[], requestSystemMessage: string | undefined, supportsSystemMessage: boolean): {
160
+ nonSystemMessages: T[];
161
+ systemContent: SystemContentResult;
162
+ };
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ // AI Summary: Shared utilities for handling system messages across LLM adapters.
3
+ // Provides functions to collect, combine, and transform system content for models
4
+ // that don't support native system instructions.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_SYSTEM_MESSAGE_FORMAT_OPTIONS = void 0;
7
+ exports.formatSystemContentForPrepend = formatSystemContentForPrepend;
8
+ exports.collectSystemContent = collectSystemContent;
9
+ exports.prependSystemToFirstUserMessage = prependSystemToFirstUserMessage;
10
+ exports.processMessagesForSystemSupport = processMessagesForSystemSupport;
11
+ /**
12
+ * Default format options for system message fallback
13
+ */
14
+ exports.DEFAULT_SYSTEM_MESSAGE_FORMAT_OPTIONS = {
15
+ format: 'xml',
16
+ tagName: 'system',
17
+ separator: '\n\n---\n\n',
18
+ };
19
+ /**
20
+ * Formats system content according to the specified format options.
21
+ *
22
+ * @param systemContent - The system content to format
23
+ * @param userContent - The original user message content
24
+ * @param options - Format options (defaults to XML with 'system' tag)
25
+ * @returns The formatted combined content
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * // XML format (default)
30
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'xml' });
31
+ * // Returns: '<system>\nBe helpful\n</system>\n\nHello'
32
+ *
33
+ * // Separator format (default separator is '\n\n---\n\n')
34
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'separator' });
35
+ * // Returns: 'Be helpful\n\n---\n\nHello'
36
+ *
37
+ * // Custom separator (specify full separator including whitespace)
38
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'separator', separator: '\n\n===\n\n' });
39
+ * // Returns: 'Be helpful\n\n===\n\nHello'
40
+ *
41
+ * // Plain format
42
+ * formatSystemContentForPrepend('Be helpful', 'Hello', { format: 'plain' });
43
+ * // Returns: 'Be helpful\n\nHello'
44
+ * ```
45
+ */
46
+ function formatSystemContentForPrepend(systemContent, userContent, options) {
47
+ const opts = { ...exports.DEFAULT_SYSTEM_MESSAGE_FORMAT_OPTIONS, ...options };
48
+ switch (opts.format) {
49
+ case 'xml':
50
+ return `<${opts.tagName}>\n${systemContent}\n</${opts.tagName}>\n\n${userContent}`;
51
+ case 'separator':
52
+ return `${systemContent}${opts.separator}${userContent}`;
53
+ case 'plain':
54
+ default:
55
+ return `${systemContent}\n\n${userContent}`;
56
+ }
57
+ }
58
+ /**
59
+ * Collects and combines system content from multiple sources.
60
+ *
61
+ * Combines the request-level systemMessage with any inline system messages
62
+ * from the messages array into a single string.
63
+ *
64
+ * @param requestSystemMessage - The request.systemMessage field (if any)
65
+ * @param inlineSystemMessages - Array of system message contents from the messages array
66
+ * @param supportsSystemMessage - Whether the model supports native system messages
67
+ * @returns Object with combined content and whether to use native support
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const { combinedSystemContent, useNativeSystemMessage } = collectSystemContent(
72
+ * 'You are helpful',
73
+ * ['Be concise'],
74
+ * false // Model doesn't support system messages
75
+ * );
76
+ * // combinedSystemContent = 'You are helpful\n\nBe concise'
77
+ * // useNativeSystemMessage = false
78
+ * ```
79
+ */
80
+ function collectSystemContent(requestSystemMessage, inlineSystemMessages, supportsSystemMessage) {
81
+ // Combine all system content
82
+ const allSystemContent = [];
83
+ if (requestSystemMessage) {
84
+ allSystemContent.push(requestSystemMessage);
85
+ }
86
+ allSystemContent.push(...inlineSystemMessages);
87
+ const combinedSystemContent = allSystemContent.length > 0 ? allSystemContent.join("\n\n") : undefined;
88
+ return {
89
+ combinedSystemContent,
90
+ useNativeSystemMessage: supportsSystemMessage,
91
+ };
92
+ }
93
+ /**
94
+ * Prepends system content to the first user message in an array.
95
+ *
96
+ * This is used as a fallback for models that don't support native system
97
+ * instructions. The system content is formatted according to the options
98
+ * and prepended to the first user message.
99
+ *
100
+ * @param messages - Array of message objects with role and content
101
+ * @param systemContent - System content to prepend
102
+ * @param options - Format options (defaults to XML with 'system' tag)
103
+ * @returns The index of the modified message, or -1 if no user message found
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const messages = [
108
+ * { role: 'user', content: 'Hello' },
109
+ * { role: 'assistant', content: 'Hi!' }
110
+ * ];
111
+ *
112
+ * // Default (XML format)
113
+ * prependSystemToFirstUserMessage(messages, 'Be helpful');
114
+ * // messages[0].content = '<system>\nBe helpful\n</system>\n\nHello'
115
+ *
116
+ * // Plain format
117
+ * prependSystemToFirstUserMessage(messages, 'Be helpful', { format: 'plain' });
118
+ * // messages[0].content = 'Be helpful\n\nHello'
119
+ *
120
+ * // Separator format (default separator is '\n\n---\n\n')
121
+ * prependSystemToFirstUserMessage(messages, 'Be helpful', { format: 'separator' });
122
+ * // messages[0].content = 'Be helpful\n\n---\n\nHello'
123
+ * ```
124
+ */
125
+ function prependSystemToFirstUserMessage(messages, systemContent, options) {
126
+ const firstUserIndex = messages.findIndex((m) => m.role === "user");
127
+ if (firstUserIndex !== -1) {
128
+ messages[firstUserIndex].content = formatSystemContentForPrepend(systemContent, messages[firstUserIndex].content, options);
129
+ return firstUserIndex;
130
+ }
131
+ return -1;
132
+ }
133
+ /**
134
+ * Filters system messages from an array and returns non-system messages
135
+ * along with collected system content.
136
+ *
137
+ * This is a convenience function that combines message filtering with
138
+ * system content collection in a single pass.
139
+ *
140
+ * @param messages - Array of messages to process
141
+ * @param requestSystemMessage - Optional request-level system message
142
+ * @param supportsSystemMessage - Whether the model supports native system messages
143
+ * @returns Object with filtered messages and system content handling info
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * const messages = [
148
+ * { role: 'system', content: 'Be helpful' },
149
+ * { role: 'user', content: 'Hello' }
150
+ * ];
151
+ * const result = processMessagesForSystemSupport(messages, undefined, false);
152
+ * // result.nonSystemMessages = [{ role: 'user', content: 'Hello' }]
153
+ * // result.systemContent.combinedSystemContent = 'Be helpful'
154
+ * ```
155
+ */
156
+ function processMessagesForSystemSupport(messages, requestSystemMessage, supportsSystemMessage) {
157
+ const nonSystemMessages = [];
158
+ const inlineSystemMessages = [];
159
+ for (const message of messages) {
160
+ if (message.role === "system") {
161
+ inlineSystemMessages.push(message.content);
162
+ }
163
+ else {
164
+ nonSystemMessages.push(message);
165
+ }
166
+ }
167
+ const systemContent = collectSystemContent(requestSystemMessage, inlineSystemMessages, supportsSystemMessage);
168
+ return {
169
+ nonSystemMessages,
170
+ systemContent,
171
+ };
172
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genai-lite",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "A lightweight, portable toolkit for interacting with various Generative AI APIs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -45,6 +45,7 @@
45
45
  "dependencies": {
46
46
  "@anthropic-ai/sdk": "^0.56.0",
47
47
  "@google/genai": "^1.0.1",
48
+ "@mistralai/mistralai": "^1.11.0",
48
49
  "js-tiktoken": "^1.0.20",
49
50
  "openai": "^5.8.2"
50
51
  },
@@ -555,5 +555,45 @@
555
555
  "settings": {
556
556
  "temperature": 0.7
557
557
  }
558
+ },
559
+ {
560
+ "id": "openrouter-gemma-3-27b-free",
561
+ "displayName": "OpenRouter - Gemma 3 27B (Free)",
562
+ "description": "Google's Gemma 3 27B via OpenRouter free tier.",
563
+ "providerId": "openrouter",
564
+ "modelId": "google/gemma-3-27b-it:free",
565
+ "settings": {
566
+ "temperature": 0.7
567
+ }
568
+ },
569
+ {
570
+ "id": "openrouter-mistral-small-3.1-free",
571
+ "displayName": "OpenRouter - Mistral Small 3.1 (Free)",
572
+ "description": "Mistral Small 3.1 24B via OpenRouter free tier.",
573
+ "providerId": "openrouter",
574
+ "modelId": "mistralai/mistral-small-3.1-24b-instruct:free",
575
+ "settings": {
576
+ "temperature": 0.7
577
+ }
578
+ },
579
+ {
580
+ "id": "mistral-small-default",
581
+ "displayName": "Mistral - Small",
582
+ "description": "Cost-effective Mistral Small model for general tasks.",
583
+ "providerId": "mistral",
584
+ "modelId": "mistral-small-latest",
585
+ "settings": {
586
+ "temperature": 0.7
587
+ }
588
+ },
589
+ {
590
+ "id": "mistral-codestral-default",
591
+ "displayName": "Mistral - Codestral",
592
+ "description": "Specialized model for code generation with lower temperature.",
593
+ "providerId": "mistral",
594
+ "modelId": "codestral-2501",
595
+ "settings": {
596
+ "temperature": 0.3
597
+ }
558
598
  }
559
599
  ]