genai-lite 0.14.1 → 0.15.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/README.md +22 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +9 -1
- package/dist/llm/LLMService.d.ts +35 -3
- package/dist/llm/LLMService.js +673 -184
- package/dist/llm/clients/AnthropicClientAdapter.d.ts +11 -3
- package/dist/llm/clients/AnthropicClientAdapter.js +280 -58
- package/dist/llm/clients/GeminiClientAdapter.d.ts +9 -3
- package/dist/llm/clients/GeminiClientAdapter.js +215 -41
- package/dist/llm/clients/LlamaCppClientAdapter.d.ts +30 -7
- package/dist/llm/clients/LlamaCppClientAdapter.js +398 -90
- package/dist/llm/clients/LlamaCppServerClient.d.ts +31 -5
- package/dist/llm/clients/LlamaCppServerClient.js +50 -4
- package/dist/llm/clients/MistralClientAdapter.d.ts +10 -3
- package/dist/llm/clients/MistralClientAdapter.js +250 -47
- package/dist/llm/clients/MockClientAdapter.d.ts +6 -3
- package/dist/llm/clients/MockClientAdapter.js +135 -11
- package/dist/llm/clients/OpenAIClientAdapter.d.ts +9 -3
- package/dist/llm/clients/OpenAIClientAdapter.js +189 -38
- package/dist/llm/clients/OpenRouterClientAdapter.d.ts +9 -3
- package/dist/llm/clients/OpenRouterClientAdapter.js +190 -38
- package/dist/llm/clients/llamaCppState.d.ts +17 -0
- package/dist/llm/clients/llamaCppState.js +83 -0
- package/dist/llm/clients/preparedAdapterUtils.d.ts +31 -0
- package/dist/llm/clients/preparedAdapterUtils.js +195 -0
- package/dist/llm/clients/types.d.ts +79 -2
- package/dist/llm/clients/types.js +8 -0
- package/dist/llm/config.js +7 -1
- package/dist/llm/services/ModelResolver.d.ts +4 -2
- package/dist/llm/services/ModelResolver.js +29 -12
- package/dist/llm/services/RequestValidator.js +24 -0
- package/dist/llm/services/SettingsManager.js +8 -0
- package/dist/llm/tokenization/bounds.d.ts +11 -0
- package/dist/llm/tokenization/bounds.js +132 -0
- package/dist/llm/tokenization/index.d.ts +4 -0
- package/dist/llm/tokenization/index.js +13 -0
- package/dist/llm/tokenization/profiles.d.ts +8 -0
- package/dist/llm/tokenization/profiles.js +174 -0
- package/dist/llm/tokenization/types.d.ts +46 -0
- package/dist/llm/tokenization/types.js +4 -0
- package/dist/llm/types.d.ts +221 -1
- package/dist/prompting/content.js +11 -0
- package/dist/prompting/index.d.ts +6 -4
- package/dist/prompting/index.js +9 -1
- package/dist/shared/adapters/usageUtils.d.ts +21 -0
- package/dist/shared/adapters/usageUtils.js +103 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -19,6 +19,9 @@ A lightweight, portable Node.js/TypeScript library providing a unified interface
|
|
|
19
19
|
- 🎭 **Template Engine** - Sophisticated templating with conditionals and variable substitution
|
|
20
20
|
- 📊 **Configurable Logging** - Debug mode, custom loggers (pino, winston), and silent mode for tests
|
|
21
21
|
|
|
22
|
+
Prepared calls can inspect and budget the immutable semantic provider request,
|
|
23
|
+
then dispatch that same representation with truthful token/termination evidence.
|
|
24
|
+
|
|
22
25
|
## Installation
|
|
23
26
|
|
|
24
27
|
```bash
|
|
@@ -105,6 +108,24 @@ for await (const event of llmService.streamMessage({
|
|
|
105
108
|
|
|
106
109
|
Streaming is implemented for all text providers: `openai`, `anthropic`, `gemini`, `mistral`, `openrouter`, and `llamacpp`. The final `complete.response` event contains the same normalized response shape returned by `sendMessage()`.
|
|
107
110
|
|
|
111
|
+
### Prepared Calls
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
const prepared = await llmService.prepareMessage(request, { mode: 'complete' });
|
|
115
|
+
if ('object' in prepared) throw new Error(prepared.error.message);
|
|
116
|
+
|
|
117
|
+
const inspection = await llmService.inspectPrepared(prepared);
|
|
118
|
+
if ('object' in inspection) throw new Error(inspection.error.message);
|
|
119
|
+
|
|
120
|
+
console.log(inspection.promptAccounting, inspection.outputTokenLimit);
|
|
121
|
+
const response = await llmService.sendPrepared(prepared);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Preparation is credential-free and mode-bound. See
|
|
125
|
+
**[Prepared Calls & Token Accounting](./genai-lite-docs/prepared-calls-and-accounting.md)**
|
|
126
|
+
for certified structural bounds, the single-margin capacity formula, response
|
|
127
|
+
evidence, and exact active-template llama.cpp counting.
|
|
128
|
+
|
|
108
129
|
### Image Generation
|
|
109
130
|
|
|
110
131
|
```typescript
|
|
@@ -138,6 +159,7 @@ Comprehensive documentation is available in the **[`genai-lite-docs`](./genai-li
|
|
|
138
159
|
|
|
139
160
|
### API Reference
|
|
140
161
|
- **[LLM Service](./genai-lite-docs/llm-service.md)** - Text generation and chat
|
|
162
|
+
- **[Prepared Calls & Token Accounting](./genai-lite-docs/prepared-calls-and-accounting.md)** - Inspect, budget, and dispatch one canonical request
|
|
141
163
|
- **[Image Service](./genai-lite-docs/image-service.md)** - Image generation (cloud and local)
|
|
142
164
|
- **[llama.cpp Integration](./genai-lite-docs/llamacpp-integration.md)** - Local LLM inference
|
|
143
165
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { ApiKeyProvider, PresetMode } from "./types";
|
|
2
2
|
export { LLMService } from "./llm/LLMService";
|
|
3
|
-
export type { LLMServiceOptions, SendMessageOptions, StreamMessageOptions, CreateMessagesResult } from "./llm/LLMService";
|
|
3
|
+
export type { LLMServiceOptions, SendMessageOptions, StreamMessageOptions, PrepareMessageOptions, CreateMessagesResult, } from "./llm/LLMService";
|
|
4
4
|
export { withRetry, DEFAULT_RETRY_POLICY } from "./shared/services/withRetry";
|
|
5
5
|
export type { RetryPolicy, RetryVerdict, WithRetryOptions } from "./shared/services/withRetry";
|
|
6
6
|
export type { ModelPreset } from "./types/presets";
|
|
@@ -10,7 +10,7 @@ export { fromEnvironment } from "./providers/fromEnvironment";
|
|
|
10
10
|
export { LlamaCppClientAdapter } from "./llm/clients/LlamaCppClientAdapter";
|
|
11
11
|
export { LlamaCppServerClient } from "./llm/clients/LlamaCppServerClient";
|
|
12
12
|
export type { LlamaCppClientConfig, } from "./llm/clients/LlamaCppClientAdapter";
|
|
13
|
-
export type { LlamaCppHealthResponse, LlamaCppTokenizeResponse, LlamaCppDetokenizeResponse, LlamaCppEmbeddingResponse, LlamaCppInfillResponse, LlamaCppPropsResponse, LlamaCppMetricsResponse, LlamaCppSlot, LlamaCppSlotsResponse, LlamaCppModel, LlamaCppModelsResponse, } from "./llm/clients/LlamaCppServerClient";
|
|
13
|
+
export type { LlamaCppHealthResponse, LlamaCppTokenizeResponse, LlamaCppDetokenizeResponse, LlamaCppEmbeddingResponse, LlamaCppInfillResponse, LlamaCppPropsResponse, LlamaCppMetricsResponse, LlamaCppSlot, LlamaCppSlotsResponse, LlamaCppModel, LlamaCppModelsResponse, LlamaCppChatInputTokensResponse, LlamaCppUtilityRequestOptions, } from "./llm/clients/LlamaCppServerClient";
|
|
14
14
|
export { OpenRouterClientAdapter } from "./llm/clients/OpenRouterClientAdapter";
|
|
15
15
|
export type { OpenRouterClientConfig } from "./llm/clients/OpenRouterClientAdapter";
|
|
16
16
|
export { MistralClientAdapter } from "./llm/clients/MistralClientAdapter";
|
|
@@ -19,6 +19,8 @@ export { ImageService } from "./image/ImageService";
|
|
|
19
19
|
export type { ImageProviderId, ImageMimeType, ImageResponseFormat, ImageQuality, ImageStyle, DiffusionSampler, ImageProgressStage, ImageProgressCallback, DiffusionSettings, OpenAISpecificSettings, ImageGenerationSettings, ResolvedImageGenerationSettings, ImageUsage, GeneratedImage, ImageGenerationRequestBase, ImageGenerationRequest, ImageGenerationRequestWithPreset, ImageGenerationResponse, ImageFailureResponse, ImageProviderCapabilities, ImageModelInfo, ImageProviderInfo, ImagePreset, ImageProviderAdapterConfig, ImageProviderAdapter, ImageServiceOptions, GenerateImageOptions, CreatePromptResult, } from "./types/image";
|
|
20
20
|
export { renderTemplate } from "./prompting/template";
|
|
21
21
|
export { countTokens, getSmartPreview, extractRandomVariables } from "./prompting/content";
|
|
22
|
+
export { TOKEN_PROFILE_MAPPING_REVISION, codePointBoundToTokenUpperBound, countTextTokens, estimateTextTokens, getTokenProfileById, resolveTokenProfile, retokenizationUpperBound, } from "./llm/tokenization";
|
|
23
|
+
export type { TokenBoundResult, TokenCountResult, TokenProfile, TokenProfileId, TokenProfileResolution, } from "./llm/tokenization";
|
|
22
24
|
export { parseStructuredContent, parseRoleTags, extractInitialTaggedContent, extractMarkerDelimitedContent, parseTemplateWithMetadata } from "./prompting/parser";
|
|
23
25
|
export type { TemplateMetadata } from "./prompting/parser";
|
|
24
26
|
export { createFallbackModelInfo, detectGgufCapabilities, KNOWN_GGUF_MODELS } from "./llm/config";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.silentLogger = exports.DEFAULT_LOG_LEVEL = exports.createDefaultLogger = exports.KNOWN_GGUF_MODELS = exports.detectGgufCapabilities = exports.createFallbackModelInfo = exports.parseTemplateWithMetadata = exports.extractMarkerDelimitedContent = exports.extractInitialTaggedContent = exports.parseRoleTags = exports.parseStructuredContent = exports.extractRandomVariables = exports.getSmartPreview = exports.countTokens = exports.renderTemplate = exports.ImageService = exports.MistralClientAdapter = exports.OpenRouterClientAdapter = exports.LlamaCppServerClient = exports.LlamaCppClientAdapter = exports.fromEnvironment = exports.DEFAULT_RETRY_POLICY = exports.withRetry = exports.LLMService = void 0;
|
|
17
|
+
exports.silentLogger = exports.DEFAULT_LOG_LEVEL = exports.createDefaultLogger = exports.KNOWN_GGUF_MODELS = exports.detectGgufCapabilities = exports.createFallbackModelInfo = exports.parseTemplateWithMetadata = exports.extractMarkerDelimitedContent = exports.extractInitialTaggedContent = exports.parseRoleTags = exports.parseStructuredContent = exports.retokenizationUpperBound = exports.resolveTokenProfile = exports.getTokenProfileById = exports.estimateTextTokens = exports.countTextTokens = exports.codePointBoundToTokenUpperBound = exports.TOKEN_PROFILE_MAPPING_REVISION = exports.extractRandomVariables = exports.getSmartPreview = exports.countTokens = exports.renderTemplate = exports.ImageService = exports.MistralClientAdapter = exports.OpenRouterClientAdapter = exports.LlamaCppServerClient = exports.LlamaCppClientAdapter = exports.fromEnvironment = exports.DEFAULT_RETRY_POLICY = exports.withRetry = exports.LLMService = void 0;
|
|
18
18
|
// --- LLM Service ---
|
|
19
19
|
var LLMService_1 = require("./llm/LLMService");
|
|
20
20
|
Object.defineProperty(exports, "LLMService", { enumerable: true, get: function () { return LLMService_1.LLMService; } });
|
|
@@ -51,6 +51,14 @@ var content_1 = require("./prompting/content");
|
|
|
51
51
|
Object.defineProperty(exports, "countTokens", { enumerable: true, get: function () { return content_1.countTokens; } });
|
|
52
52
|
Object.defineProperty(exports, "getSmartPreview", { enumerable: true, get: function () { return content_1.getSmartPreview; } });
|
|
53
53
|
Object.defineProperty(exports, "extractRandomVariables", { enumerable: true, get: function () { return content_1.extractRandomVariables; } });
|
|
54
|
+
var tokenization_1 = require("./llm/tokenization");
|
|
55
|
+
Object.defineProperty(exports, "TOKEN_PROFILE_MAPPING_REVISION", { enumerable: true, get: function () { return tokenization_1.TOKEN_PROFILE_MAPPING_REVISION; } });
|
|
56
|
+
Object.defineProperty(exports, "codePointBoundToTokenUpperBound", { enumerable: true, get: function () { return tokenization_1.codePointBoundToTokenUpperBound; } });
|
|
57
|
+
Object.defineProperty(exports, "countTextTokens", { enumerable: true, get: function () { return tokenization_1.countTextTokens; } });
|
|
58
|
+
Object.defineProperty(exports, "estimateTextTokens", { enumerable: true, get: function () { return tokenization_1.estimateTextTokens; } });
|
|
59
|
+
Object.defineProperty(exports, "getTokenProfileById", { enumerable: true, get: function () { return tokenization_1.getTokenProfileById; } });
|
|
60
|
+
Object.defineProperty(exports, "resolveTokenProfile", { enumerable: true, get: function () { return tokenization_1.resolveTokenProfile; } });
|
|
61
|
+
Object.defineProperty(exports, "retokenizationUpperBound", { enumerable: true, get: function () { return tokenization_1.retokenizationUpperBound; } });
|
|
54
62
|
var parser_1 = require("./prompting/parser");
|
|
55
63
|
Object.defineProperty(exports, "parseStructuredContent", { enumerable: true, get: function () { return parser_1.parseStructuredContent; } });
|
|
56
64
|
Object.defineProperty(exports, "parseRoleTags", { enumerable: true, get: function () { return parser_1.parseRoleTags; } });
|
package/dist/llm/LLMService.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ApiKeyProvider, PresetMode } from '../types';
|
|
2
2
|
import type { Logger, LogLevel } from '../logging/types';
|
|
3
|
-
import type { LLMChatRequest, LLMChatRequestWithPreset, LLMResponse, LLMFailureResponse, ProviderInfo, ModelInfo, ApiProviderId, LLMSettings, ModelContext, LLMMessage,
|
|
3
|
+
import type { LLMChatRequest, LLMChatRequestWithPreset, LLMResponse, LLMFailureResponse, ProviderInfo, ModelInfo, ApiProviderId, LLMSettings, ModelContext, LLMMessage, LLMServiceStreamEvent, LLMRequestCapabilityPreflight, LLMRequestCapabilityValidationResult, ModelCapabilitiesResult, StructuredOutputSupport, PreparedCallMode, PreparedCall, PreparedCompleteCall, PreparedStreamCall, PrepareMessageResult, PreparedRequestInspection } from "./types";
|
|
4
4
|
import type { ModelPreset } from "../types/presets";
|
|
5
5
|
import { type RetryPolicy } from "../shared/services/withRetry";
|
|
6
6
|
export type { PresetMode };
|
|
@@ -55,6 +55,10 @@ export interface StreamMessageOptions {
|
|
|
55
55
|
/** Per-request timeout in ms (overrides the service-level timeoutMs) */
|
|
56
56
|
timeoutMs?: number;
|
|
57
57
|
}
|
|
58
|
+
/** Selects the immutable dispatch mode fixed during preparation. */
|
|
59
|
+
export interface PrepareMessageOptions<TMode extends PreparedCallMode> {
|
|
60
|
+
mode: TMode;
|
|
61
|
+
}
|
|
58
62
|
/**
|
|
59
63
|
* Result from createMessages method
|
|
60
64
|
*/
|
|
@@ -87,6 +91,7 @@ export declare class LLMService {
|
|
|
87
91
|
private modelResolver;
|
|
88
92
|
private retryOptions;
|
|
89
93
|
private defaultTimeoutMs?;
|
|
94
|
+
private preparedCalls;
|
|
90
95
|
constructor(getApiKey: ApiKeyProvider, options?: LLMServiceOptions);
|
|
91
96
|
/**
|
|
92
97
|
* Gets list of supported LLM providers
|
|
@@ -133,6 +138,15 @@ export declare class LLMService {
|
|
|
133
138
|
* @returns Promise resolving to either success or failure response
|
|
134
139
|
*/
|
|
135
140
|
sendMessage(request: LLMChatRequest | LLMChatRequestWithPreset, callOptions?: SendMessageOptions): Promise<LLMResponse | LLMFailureResponse>;
|
|
141
|
+
/**
|
|
142
|
+
* Resolves and freezes the final semantic provider request without retrieving
|
|
143
|
+
* credentials or creating transport state.
|
|
144
|
+
*/
|
|
145
|
+
prepareMessage<TMode extends PreparedCallMode>(request: LLMChatRequest | LLMChatRequestWithPreset, options: PrepareMessageOptions<TMode>): Promise<PrepareMessageResult<TMode>>;
|
|
146
|
+
/** Returns the immutable inspection view for a service-owned prepared call. */
|
|
147
|
+
inspectPrepared(handle: PreparedCall<PreparedCallMode>): Promise<PreparedRequestInspection | LLMFailureResponse>;
|
|
148
|
+
/** Dispatches a reusable complete-mode prepared call. */
|
|
149
|
+
sendPrepared(handle: PreparedCompleteCall, callOptions?: SendMessageOptions): Promise<LLMResponse | LLMFailureResponse>;
|
|
136
150
|
/**
|
|
137
151
|
* Streams a chat message from an LLM provider.
|
|
138
152
|
*
|
|
@@ -140,12 +154,30 @@ export declare class LLMService {
|
|
|
140
154
|
* sendMessage(). Validation/API key failures and unsupported streaming adapters
|
|
141
155
|
* are yielded as a single `error` event.
|
|
142
156
|
*/
|
|
143
|
-
streamMessage(request: LLMChatRequest | LLMChatRequestWithPreset, callOptions?: StreamMessageOptions): AsyncGenerator<
|
|
157
|
+
streamMessage(request: LLMChatRequest | LLMChatRequestWithPreset, callOptions?: StreamMessageOptions): AsyncGenerator<LLMServiceStreamEvent>;
|
|
158
|
+
/** Dispatches a reusable stream-mode prepared call with no automatic retries. */
|
|
159
|
+
streamPrepared(handle: PreparedStreamCall, callOptions?: StreamMessageOptions): AsyncGenerator<LLMServiceStreamEvent>;
|
|
160
|
+
private streamPreparedWithAttempt;
|
|
144
161
|
private resolveAndValidateCapabilities;
|
|
145
162
|
private buildModelCapabilities;
|
|
146
163
|
private getStructuredOutputSupport;
|
|
147
164
|
private getCapabilitySource;
|
|
148
|
-
private
|
|
165
|
+
private isFailureResponse;
|
|
166
|
+
private createPreparedFailure;
|
|
167
|
+
private createPreparedHandleError;
|
|
168
|
+
private resolvePreparedHandle;
|
|
169
|
+
private resolveDispatchApiKey;
|
|
170
|
+
private createAdapterOptions;
|
|
171
|
+
private revalidatePrepared;
|
|
172
|
+
private nextStreamEvent;
|
|
173
|
+
private observeStreamPartial;
|
|
174
|
+
private getStreamPartialChoice;
|
|
175
|
+
private finalizeStreamFailure;
|
|
176
|
+
private attachStreamPartial;
|
|
177
|
+
private isValidPreparedAccounting;
|
|
178
|
+
private createEffectiveOutputTokenLimit;
|
|
179
|
+
private createUnexpectedDispatchFailure;
|
|
180
|
+
private prepareCanonicalRequest;
|
|
149
181
|
private postProcessResponse;
|
|
150
182
|
/**
|
|
151
183
|
* Gets all configured model presets
|