@nebulaos/llm-gateway 0.1.0 → 0.1.1
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.txt +21 -0
- package/README.md +54 -4
- package/dist/index.d.mts +38 -2
- package/dist/index.d.ts +38 -2
- package/dist/index.js +349 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +348 -54
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present, iamkun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -12,21 +12,27 @@ npm install @nebulaos/llm-gateway
|
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { LLMGateway } from "@nebulaos/llm-gateway";
|
|
15
|
-
import { Agent } from "@nebulaos/core";
|
|
15
|
+
import { Agent, BufferMemory } from "@nebulaos/core";
|
|
16
16
|
|
|
17
17
|
const agent = new Agent({
|
|
18
|
+
id: "assistente",
|
|
18
19
|
name: "assistente",
|
|
19
20
|
model: new LLMGateway({
|
|
20
|
-
apiKey:
|
|
21
|
+
apiKey: process.env.NEBULAOS_API_KEY,
|
|
21
22
|
baseUrl: "https://your-nebula-instance.com",
|
|
22
23
|
model: "assistente", // Route alias configured in NebulaOS
|
|
23
|
-
|
|
24
|
+
options: {
|
|
25
|
+
reasoning_effort: "high", // Enable deep reasoning (Gemini)
|
|
26
|
+
temperature: 0.7,
|
|
27
|
+
},
|
|
24
28
|
}),
|
|
29
|
+
memory: new BufferMemory(),
|
|
25
30
|
instructions: "You are a helpful assistant.",
|
|
26
31
|
});
|
|
27
32
|
|
|
28
33
|
// Use the agent
|
|
29
|
-
const
|
|
34
|
+
const result = await agent.execute("Analyze this complex problem step by step");
|
|
35
|
+
console.log(result.content);
|
|
30
36
|
```
|
|
31
37
|
|
|
32
38
|
## Configuration
|
|
@@ -37,6 +43,50 @@ const response = await agent.generate("Hello, how can I help you?");
|
|
|
37
43
|
| `baseUrl` | `string` | No | NebulaOS instance URL (defaults to localhost:4100) |
|
|
38
44
|
| `model` | `string` | Yes | Route alias configured in NebulaOS |
|
|
39
45
|
| `logLevel` | `string` | No | Logger verbosity ("debug", "info", "warn", "error", "none") |
|
|
46
|
+
| `options` | `object` | No | Default model options for every call (see below) |
|
|
47
|
+
|
|
48
|
+
## Model Options
|
|
49
|
+
|
|
50
|
+
Use `options` to configure model-specific parameters that apply to every call:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const model = new LLMGateway({
|
|
54
|
+
apiKey: "your-api-key",
|
|
55
|
+
model: "minha-rota",
|
|
56
|
+
options: {
|
|
57
|
+
temperature: 0.7,
|
|
58
|
+
reasoning_effort: "high", // Gemini 2.5+/3+ via LiteLLM
|
|
59
|
+
topK: 40,
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Common Options
|
|
65
|
+
|
|
66
|
+
| Option | Type | Description |
|
|
67
|
+
|--------|------|-------------|
|
|
68
|
+
| `temperature` | `number` | Sampling temperature (0-2) |
|
|
69
|
+
| `topP` | `number` | Nucleus sampling |
|
|
70
|
+
| `topK` | `number` | Top-K sampling (Gemini) |
|
|
71
|
+
| `maxTokens` | `number` | Max output tokens |
|
|
72
|
+
|
|
73
|
+
### Provider-Specific Options
|
|
74
|
+
|
|
75
|
+
These are passed directly to the provider via LiteLLM:
|
|
76
|
+
|
|
77
|
+
**Gemini (via LiteLLM)**:
|
|
78
|
+
| Option | Values | Description |
|
|
79
|
+
|--------|--------|-------------|
|
|
80
|
+
| `reasoning_effort` | `"low"`, `"medium"`, `"high"` | Controls thinking depth (Gemini 2.5+/3+) |
|
|
81
|
+
| `thinking` | `{ type: "enabled", budget_tokens: N }` | Fine-grained thinking control |
|
|
82
|
+
|
|
83
|
+
**OpenAI**:
|
|
84
|
+
| Option | Type | Description |
|
|
85
|
+
|--------|------|-------------|
|
|
86
|
+
| `frequency_penalty` | `number` | Reduce repetition (-2 to 2) |
|
|
87
|
+
| `presence_penalty` | `number` | Encourage new topics (-2 to 2) |
|
|
88
|
+
|
|
89
|
+
Any extra options are passed through to the underlying provider.
|
|
40
90
|
|
|
41
91
|
## Routes
|
|
42
92
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import OpenAIClient, { ClientOptions } from 'openai';
|
|
2
|
-
import { LogLevel, IModel, Message, ToolDefinitionForLLM,
|
|
2
|
+
import { LogLevel, GenerateOptions, IModel, Message, ToolDefinitionForLLM, ProviderResponse, StreamChunk } from '@nebulaos/core';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Custom error class for LLM Gateway errors.
|
|
6
|
+
* Provides clear, actionable error messages for developers.
|
|
7
|
+
*/
|
|
8
|
+
declare class LLMGatewayError extends Error {
|
|
9
|
+
readonly code: string;
|
|
10
|
+
readonly status?: number | undefined;
|
|
11
|
+
readonly cause?: unknown | undefined;
|
|
12
|
+
constructor(message: string, code: string, status?: number | undefined, cause?: unknown | undefined);
|
|
13
|
+
}
|
|
4
14
|
/**
|
|
5
15
|
* LLM Gateway Provider Configuration
|
|
6
16
|
*/
|
|
@@ -15,6 +25,12 @@ interface LLMGatewayConfig {
|
|
|
15
25
|
logLevel?: LogLevel;
|
|
16
26
|
/** Optional OpenAI client options */
|
|
17
27
|
clientOptions?: ClientOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Default model options passed to every generate() call.
|
|
30
|
+
* Supports provider-specific params like reasoning_effort, temperature, topK, etc.
|
|
31
|
+
* These can be overridden by options passed directly to generate().
|
|
32
|
+
*/
|
|
33
|
+
options?: Omit<GenerateOptions, "responseFormat">;
|
|
18
34
|
}
|
|
19
35
|
/**
|
|
20
36
|
* NebulaOS LLM Gateway Provider
|
|
@@ -29,6 +45,7 @@ declare class LLMGateway implements IModel {
|
|
|
29
45
|
private client;
|
|
30
46
|
private baseUrl;
|
|
31
47
|
private logger;
|
|
48
|
+
private options?;
|
|
32
49
|
capabilities: {
|
|
33
50
|
readonly inputFiles: {
|
|
34
51
|
readonly mimeTypes: readonly ["image/*"];
|
|
@@ -38,6 +55,25 @@ declare class LLMGateway implements IModel {
|
|
|
38
55
|
constructor(config: LLMGatewayConfig);
|
|
39
56
|
generate(messages: Message[], tools?: ToolDefinitionForLLM[], options?: GenerateOptions): Promise<ProviderResponse>;
|
|
40
57
|
generateStream(messages: Message[], tools?: ToolDefinitionForLLM[], options?: GenerateOptions): AsyncGenerator<StreamChunk>;
|
|
58
|
+
/**
|
|
59
|
+
* Transforms raw errors into actionable LLMGatewayError with clear messages.
|
|
60
|
+
* This ensures developers get specific guidance on how to resolve issues.
|
|
61
|
+
*
|
|
62
|
+
* Differentiates between:
|
|
63
|
+
* - Gateway errors: LLM Gateway API key issues (check NEBULAOS_API_KEY env var)
|
|
64
|
+
* - Provider errors: LLM provider API key issues (check route config in dashboard)
|
|
65
|
+
*/
|
|
66
|
+
private handleError;
|
|
67
|
+
/**
|
|
68
|
+
* Extracts the error source from an APIError.
|
|
69
|
+
* The backend sets X-Error-Source header or includes source in the error body
|
|
70
|
+
* to differentiate between gateway errors (LLM Gateway API key) and provider errors.
|
|
71
|
+
*
|
|
72
|
+
* @returns "gateway" if the error is from LLM Gateway authentication,
|
|
73
|
+
* "provider" if the error is from the upstream LLM provider,
|
|
74
|
+
* undefined if the source cannot be determined.
|
|
75
|
+
*/
|
|
76
|
+
private extractErrorSource;
|
|
41
77
|
private extractExtraOptions;
|
|
42
78
|
private buildGatewayHeaders;
|
|
43
79
|
protected convertMessages(messages: Message[]): OpenAIClient.Chat.ChatCompletionMessageParam[];
|
|
@@ -47,4 +83,4 @@ declare class LLMGateway implements IModel {
|
|
|
47
83
|
private convertContentPart;
|
|
48
84
|
}
|
|
49
85
|
|
|
50
|
-
export { LLMGateway, type LLMGatewayConfig };
|
|
86
|
+
export { LLMGateway, type LLMGatewayConfig, LLMGatewayError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import OpenAIClient, { ClientOptions } from 'openai';
|
|
2
|
-
import { LogLevel, IModel, Message, ToolDefinitionForLLM,
|
|
2
|
+
import { LogLevel, GenerateOptions, IModel, Message, ToolDefinitionForLLM, ProviderResponse, StreamChunk } from '@nebulaos/core';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Custom error class for LLM Gateway errors.
|
|
6
|
+
* Provides clear, actionable error messages for developers.
|
|
7
|
+
*/
|
|
8
|
+
declare class LLMGatewayError extends Error {
|
|
9
|
+
readonly code: string;
|
|
10
|
+
readonly status?: number | undefined;
|
|
11
|
+
readonly cause?: unknown | undefined;
|
|
12
|
+
constructor(message: string, code: string, status?: number | undefined, cause?: unknown | undefined);
|
|
13
|
+
}
|
|
4
14
|
/**
|
|
5
15
|
* LLM Gateway Provider Configuration
|
|
6
16
|
*/
|
|
@@ -15,6 +25,12 @@ interface LLMGatewayConfig {
|
|
|
15
25
|
logLevel?: LogLevel;
|
|
16
26
|
/** Optional OpenAI client options */
|
|
17
27
|
clientOptions?: ClientOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Default model options passed to every generate() call.
|
|
30
|
+
* Supports provider-specific params like reasoning_effort, temperature, topK, etc.
|
|
31
|
+
* These can be overridden by options passed directly to generate().
|
|
32
|
+
*/
|
|
33
|
+
options?: Omit<GenerateOptions, "responseFormat">;
|
|
18
34
|
}
|
|
19
35
|
/**
|
|
20
36
|
* NebulaOS LLM Gateway Provider
|
|
@@ -29,6 +45,7 @@ declare class LLMGateway implements IModel {
|
|
|
29
45
|
private client;
|
|
30
46
|
private baseUrl;
|
|
31
47
|
private logger;
|
|
48
|
+
private options?;
|
|
32
49
|
capabilities: {
|
|
33
50
|
readonly inputFiles: {
|
|
34
51
|
readonly mimeTypes: readonly ["image/*"];
|
|
@@ -38,6 +55,25 @@ declare class LLMGateway implements IModel {
|
|
|
38
55
|
constructor(config: LLMGatewayConfig);
|
|
39
56
|
generate(messages: Message[], tools?: ToolDefinitionForLLM[], options?: GenerateOptions): Promise<ProviderResponse>;
|
|
40
57
|
generateStream(messages: Message[], tools?: ToolDefinitionForLLM[], options?: GenerateOptions): AsyncGenerator<StreamChunk>;
|
|
58
|
+
/**
|
|
59
|
+
* Transforms raw errors into actionable LLMGatewayError with clear messages.
|
|
60
|
+
* This ensures developers get specific guidance on how to resolve issues.
|
|
61
|
+
*
|
|
62
|
+
* Differentiates between:
|
|
63
|
+
* - Gateway errors: LLM Gateway API key issues (check NEBULAOS_API_KEY env var)
|
|
64
|
+
* - Provider errors: LLM provider API key issues (check route config in dashboard)
|
|
65
|
+
*/
|
|
66
|
+
private handleError;
|
|
67
|
+
/**
|
|
68
|
+
* Extracts the error source from an APIError.
|
|
69
|
+
* The backend sets X-Error-Source header or includes source in the error body
|
|
70
|
+
* to differentiate between gateway errors (LLM Gateway API key) and provider errors.
|
|
71
|
+
*
|
|
72
|
+
* @returns "gateway" if the error is from LLM Gateway authentication,
|
|
73
|
+
* "provider" if the error is from the upstream LLM provider,
|
|
74
|
+
* undefined if the source cannot be determined.
|
|
75
|
+
*/
|
|
76
|
+
private extractErrorSource;
|
|
41
77
|
private extractExtraOptions;
|
|
42
78
|
private buildGatewayHeaders;
|
|
43
79
|
protected convertMessages(messages: Message[]): OpenAIClient.Chat.ChatCompletionMessageParam[];
|
|
@@ -47,4 +83,4 @@ declare class LLMGateway implements IModel {
|
|
|
47
83
|
private convertContentPart;
|
|
48
84
|
}
|
|
49
85
|
|
|
50
|
-
export { LLMGateway, type LLMGatewayConfig };
|
|
86
|
+
export { LLMGateway, type LLMGatewayConfig, LLMGatewayError };
|