@morlay/dsh-llm-openai-compatible 0.0.1 → 0.0.3
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 +21 -0
- package/README.md +0 -9
- package/cordis.patch.yml +3 -3
- package/dist/adapter-CYd_pegB.d.mts +62 -0
- package/dist/index.d.mts +49 -0
- package/{lib → dist}/index.mjs +267 -65
- package/dist/invariant.d.mts +7 -0
- package/dist/invariant.mjs +8 -0
- package/{lib/serialize.mjs → dist/translate-BzOJ1xx-.mjs} +192 -67
- package/dist/wire.d.mts +37 -0
- package/dist/wire.mjs +2 -0
- package/package.json +23 -19
- package/src/adapter.ts +426 -0
- package/src/index.ts +497 -0
- package/src/invariant.ts +13 -0
- package/src/serialize.ts +336 -0
- package/src/translate.ts +195 -0
- package/src/wire.ts +4 -0
- package/lib/adapter.d.mts +0 -137
- package/lib/adapter.d.mts.map +0 -1
- package/lib/adapter.mjs +0 -295
- package/lib/adapter.mjs.map +0 -1
- package/lib/index.d.mts +0 -89
- package/lib/index.d.mts.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/lib/serialize.d.mts +0 -68
- package/lib/serialize.d.mts.map +0 -1
- package/lib/serialize.mjs.map +0 -1
- package/lib/translate.d.mts +0 -19
- package/lib/translate.d.mts.map +0 -1
- package/lib/translate.mjs +0 -209
- package/lib/translate.mjs.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 morlay
|
|
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,15 +12,6 @@ DeepSeek Harness 的 **OpenAI 兼容 LLM 适配器**插件。与内置 `llm-pi-a
|
|
|
12
12
|
harness 消息 → AI SDK prompt 转换、采样默认合并、stream part → `StreamChunk`
|
|
13
13
|
翻译、错误归一化与凭据策略。
|
|
14
14
|
|
|
15
|
-
## 安装
|
|
16
|
-
|
|
17
|
-
```sh
|
|
18
|
-
dsh plugin --profile web add "@morlay/dsh-llm-openai-compatible"
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
插件默认休眠(空 `providers`):不声明任何 provider 路由,直到你在
|
|
22
|
-
`settings.yaml` 中配置。
|
|
23
|
-
|
|
24
15
|
## 配置
|
|
25
16
|
|
|
26
17
|
`providers` 是 dict:**key 就是 provider 路由键**(选择器与
|
package/cordis.patch.yml
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
ollama:
|
|
7
7
|
apiKeyEnv: OLLAMA_API_KEY
|
|
8
8
|
baseURL: https://ollama.com/v1
|
|
9
|
-
displayName: Ollama
|
|
9
|
+
displayName: Ollama Cloud
|
|
10
10
|
temperature: 1 # 0..2
|
|
11
11
|
topP: 0.95
|
|
12
12
|
reasoning: high
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
defaultMaxTokens: 65535
|
|
15
15
|
models:
|
|
16
16
|
- id: deepseek-v4-flash:0731
|
|
17
|
-
name: "DeepSeek V4 Flash
|
|
17
|
+
name: "DeepSeek V4 Flash @ Ollama Cloud"
|
|
18
18
|
contextWindow: 1000000
|
|
19
19
|
maxTokens: 65535
|
|
20
20
|
inputModalities: [text]
|
|
21
21
|
reasoningEfforts:
|
|
22
|
-
off:
|
|
22
|
+
off:
|
|
23
23
|
high: high
|
|
24
24
|
max: max
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { GenerateOptions, LlmAdapter, LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo, ModelModality, ResolvedRetryPolicy, StreamChunk } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { CredentialRef } from "@deepseek-ai/dsh-credentials";
|
|
3
|
+
import { LanguageModelV4Usage } from "@ai-sdk/provider";
|
|
4
|
+
import { AttachmentStore } from "@deepseek-ai/dsh-attachment";
|
|
5
|
+
//#region src/adapter.d.ts
|
|
6
|
+
declare const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 300000;
|
|
7
|
+
declare const DEFAULT_CONTEXT_WINDOW = 262144;
|
|
8
|
+
declare const DEFAULT_MAX_TOKENS = 32768;
|
|
9
|
+
declare const DEFAULT_MAX_REQUEST_IMAGE_BYTES: number;
|
|
10
|
+
type ReasoningEffort = "off" | "low" | "high" | "max";
|
|
11
|
+
interface ResolvedModelProfile {
|
|
12
|
+
id: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
contextWindow?: number;
|
|
16
|
+
maxTokens?: number;
|
|
17
|
+
inputModalities: readonly ModelModality[];
|
|
18
|
+
reasoningEfforts?: false | Partial<Record<ReasoningEffort, string | null>>;
|
|
19
|
+
}
|
|
20
|
+
interface ResolvedProviderProfile {
|
|
21
|
+
provider: string;
|
|
22
|
+
displayName: string;
|
|
23
|
+
apiKeyEnv?: CredentialRef;
|
|
24
|
+
baseURL: string;
|
|
25
|
+
headers?: Readonly<Record<string, string>>;
|
|
26
|
+
temperature?: number;
|
|
27
|
+
topP?: number;
|
|
28
|
+
topK?: number;
|
|
29
|
+
presencePenalty?: number;
|
|
30
|
+
frequencyPenalty?: number;
|
|
31
|
+
seed?: number;
|
|
32
|
+
reasoning?: ReasoningEffort;
|
|
33
|
+
models: readonly ResolvedModelProfile[];
|
|
34
|
+
defaultContextWindow: number;
|
|
35
|
+
defaultMaxTokens: number;
|
|
36
|
+
maxRequestImageBytes: number;
|
|
37
|
+
streamIdleTimeoutMs: number;
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
retryPolicy: ResolvedRetryPolicy;
|
|
40
|
+
}
|
|
41
|
+
interface OpenAICompatibleAdapterOptions {
|
|
42
|
+
profiles: () => ReadonlyMap<string, ResolvedProviderProfile>;
|
|
43
|
+
resolveApiKey: (provider: string, profile: ResolvedProviderProfile) => Promise<string | undefined>;
|
|
44
|
+
resolveUserId: () => string;
|
|
45
|
+
resolveAttachments?: () => AttachmentStore | undefined;
|
|
46
|
+
}
|
|
47
|
+
declare class OpenAICompatibleAdapter extends LlmAdapter {
|
|
48
|
+
private readonly config;
|
|
49
|
+
private readonly sdkProviders;
|
|
50
|
+
constructor(config: OpenAICompatibleAdapterOptions);
|
|
51
|
+
private profileOf;
|
|
52
|
+
private modelOf;
|
|
53
|
+
private sdkModel;
|
|
54
|
+
providerInfo(provider: string): LlmProviderInfo;
|
|
55
|
+
providerRetryPolicy(provider: string): ResolvedRetryPolicy | undefined;
|
|
56
|
+
listModels(provider: string): Promise<readonly LlmModelInfo[]>;
|
|
57
|
+
resolveModel(provider: string, model: string, _signal?: AbortSignal): Promise<LlmResolvedModelInfo>;
|
|
58
|
+
stream(options: GenerateOptions): AsyncGenerator<StreamChunk>;
|
|
59
|
+
private normalizeTransportError;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { OpenAICompatibleAdapter as a, ResolvedModelProfile as c, DEFAULT_STREAM_IDLE_TIMEOUT_MS as i, ResolvedProviderProfile as l, DEFAULT_MAX_REQUEST_IMAGE_BYTES as n, OpenAICompatibleAdapterOptions as o, DEFAULT_MAX_TOKENS as r, ReasoningEffort as s, DEFAULT_CONTEXT_WINDOW as t };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { a as OpenAICompatibleAdapter, c as ResolvedModelProfile, i as DEFAULT_STREAM_IDLE_TIMEOUT_MS, l as ResolvedProviderProfile, n as DEFAULT_MAX_REQUEST_IMAGE_BYTES, o as OpenAICompatibleAdapterOptions, r as DEFAULT_MAX_TOKENS, s as ReasoningEffort, t as DEFAULT_CONTEXT_WINDOW } from "./adapter-CYd_pegB.mjs";
|
|
2
|
+
import z from "@deepseek-ai/schemastery";
|
|
3
|
+
import { ModelModality, RetryPolicyConfig } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
declare const name = "llm-openai-compatible";
|
|
7
|
+
declare const inject: string[];
|
|
8
|
+
declare const NS = "llm-openai-compatible";
|
|
9
|
+
declare const REASONING_LEVELS: readonly ["off", "low", "high", "max"];
|
|
10
|
+
declare const MODEL_MODALITIES: readonly ["text", "image"];
|
|
11
|
+
interface ModelProfileSource {
|
|
12
|
+
id: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
contextWindow?: number;
|
|
16
|
+
maxTokens?: number;
|
|
17
|
+
inputModalities?: ModelModality[];
|
|
18
|
+
reasoningEfforts?: false | Partial<Record<ReasoningEffort, string | null>>;
|
|
19
|
+
}
|
|
20
|
+
interface ProviderProfileSource {
|
|
21
|
+
apiKeyEnv?: string;
|
|
22
|
+
displayName?: string;
|
|
23
|
+
baseURL: string;
|
|
24
|
+
headers?: Record<string, string>;
|
|
25
|
+
temperature?: number;
|
|
26
|
+
topP?: number;
|
|
27
|
+
topK?: number;
|
|
28
|
+
presencePenalty?: number;
|
|
29
|
+
frequencyPenalty?: number;
|
|
30
|
+
seed?: number;
|
|
31
|
+
reasoning?: ReasoningEffort;
|
|
32
|
+
models?: ModelProfileSource[];
|
|
33
|
+
defaultContextWindow?: number;
|
|
34
|
+
defaultMaxTokens?: number;
|
|
35
|
+
maxRequestImageBytes?: number;
|
|
36
|
+
streamIdleTimeoutMs?: number;
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
retryPolicy?: RetryPolicyConfig;
|
|
39
|
+
}
|
|
40
|
+
interface Config {
|
|
41
|
+
providers?: Record<string, ProviderProfileSource>;
|
|
42
|
+
}
|
|
43
|
+
declare const Config: z<Config>;
|
|
44
|
+
declare function resolveAdapterOptions(provider: string, source: ProviderProfileSource): ResolvedProviderProfile;
|
|
45
|
+
declare function resolveProfiles(providers: Readonly<Record<string, ProviderProfileSource>> | undefined): Map<string, ResolvedProviderProfile>;
|
|
46
|
+
declare function assertServiceable(config: Config): void;
|
|
47
|
+
declare function apply(ctx: Context, config: Config): void;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { Config, DEFAULT_CONTEXT_WINDOW, DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_MAX_TOKENS, DEFAULT_STREAM_IDLE_TIMEOUT_MS, MODEL_MODALITIES, ModelProfileSource, NS, OpenAICompatibleAdapter, type OpenAICompatibleAdapterOptions, ProviderProfileSource, REASONING_LEVELS, type ReasoningEffort, type ResolvedModelProfile, type ResolvedProviderProfile, apply, assertServiceable, inject, name, resolveAdapterOptions, resolveProfiles };
|
package/{lib → dist}/index.mjs
RENAMED
|
@@ -1,23 +1,263 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as serializeCallOptions, o as serializeCallOptionsWithImages, r as translate } from "./translate-BzOJ1xx-.mjs";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
|
-
import { LlmError, RetryPolicySchema, assertUsableApiKey, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
3
|
+
import { CONTEXT_WINDOW_EXCEEDED_CODE, LlmAdapter, LlmError, ProviderRequestId, QUOTA_EXCEEDED_CODE, ReasoningEffortId, RetryPolicySchema, assertUsableApiKey, attributionHeaders, contentHasImage, isContextWindowExceededError, isQuotaExceededError, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
4
4
|
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
5
5
|
import { launchEnvironmentOf } from "@deepseek-ai/dsh-launch-environment";
|
|
6
|
-
import { deepEqualJson
|
|
7
|
-
import { MAX_TIMER_DELAY_MS } from "@deepseek-ai/dsh-timeout";
|
|
6
|
+
import { deepEqualJson } from "@deepseek-ai/dsh-util-values";
|
|
7
|
+
import { MAX_TIMER_DELAY_MS, deadline, idleWatchdog, timeoutOf } from "@deepseek-ai/dsh-timeout";
|
|
8
8
|
import { getOrCreateAnonymousUserId } from "@deepseek-ai/dsh-anonymous-user-id";
|
|
9
|
+
import { APICallError } from "@ai-sdk/provider";
|
|
10
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
11
|
+
//#region src/adapter.ts
|
|
12
|
+
const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 3e5;
|
|
13
|
+
const DEFAULT_CONTEXT_WINDOW = 262144;
|
|
14
|
+
const DEFAULT_MAX_TOKENS = 32768;
|
|
15
|
+
const DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20971520;
|
|
16
|
+
const STREAM_IDLE_TIMEOUT_CODE = "LLM_STREAM_IDLE_TIMEOUT";
|
|
17
|
+
const REQUEST_TIMEOUT_CODE = "LLM_REQUEST_TIMEOUT";
|
|
18
|
+
const PROVIDER_OPTIONS_KEY = "openai-compatible";
|
|
19
|
+
function convertUsage(usage) {
|
|
20
|
+
if (usage == null) return {
|
|
21
|
+
inputTokens: {
|
|
22
|
+
total: 0,
|
|
23
|
+
noCache: 0,
|
|
24
|
+
cacheRead: void 0,
|
|
25
|
+
cacheWrite: void 0
|
|
26
|
+
},
|
|
27
|
+
outputTokens: {
|
|
28
|
+
total: 0,
|
|
29
|
+
text: void 0,
|
|
30
|
+
reasoning: void 0
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const promptTokens = usage.prompt_tokens ?? 0;
|
|
34
|
+
const completionTokens = usage.completion_tokens ?? 0;
|
|
35
|
+
const cacheRead = usage.prompt_tokens_details?.cached_tokens ?? usage.prompt_cache_hit_tokens ?? 0;
|
|
36
|
+
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
37
|
+
return {
|
|
38
|
+
inputTokens: {
|
|
39
|
+
total: promptTokens,
|
|
40
|
+
noCache: Math.max(0, promptTokens - cacheRead),
|
|
41
|
+
cacheRead,
|
|
42
|
+
cacheWrite: void 0
|
|
43
|
+
},
|
|
44
|
+
outputTokens: {
|
|
45
|
+
total: completionTokens,
|
|
46
|
+
text: Math.max(0, completionTokens - reasoningTokens),
|
|
47
|
+
reasoning: reasoningTokens
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function modelInfo(profile, model) {
|
|
52
|
+
return {
|
|
53
|
+
provider: profile.provider,
|
|
54
|
+
id: model.id,
|
|
55
|
+
name: model.name ?? model.id,
|
|
56
|
+
...model.description === void 0 ? {} : { description: model.description },
|
|
57
|
+
inputModalities: [...model.inputModalities]
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function reasoningInfo(model, defaultEffort) {
|
|
61
|
+
const declaration = model?.reasoningEfforts;
|
|
62
|
+
if (declaration === void 0 || declaration === false) return {};
|
|
63
|
+
return { reasoning: {
|
|
64
|
+
efforts: Object.entries(declaration).map(([id]) => ({
|
|
65
|
+
id: ReasoningEffortId(id),
|
|
66
|
+
name: `${id.charAt(0).toUpperCase()}${id.slice(1)}`
|
|
67
|
+
})),
|
|
68
|
+
...defaultEffort !== void 0 && declaration[defaultEffort] !== void 0 ? { defaultEffort: ReasoningEffortId(defaultEffort) } : {}
|
|
69
|
+
} };
|
|
70
|
+
}
|
|
71
|
+
function httpErrorCode(status, error) {
|
|
72
|
+
if (status === 401 || status === 403) return "AUTH";
|
|
73
|
+
if (status === 413) return "INVALID_REQUEST";
|
|
74
|
+
const detail = [
|
|
75
|
+
error?.code,
|
|
76
|
+
error?.type,
|
|
77
|
+
error?.message
|
|
78
|
+
].filter(Boolean).join(" ");
|
|
79
|
+
if (isQuotaExceededError(detail)) return QUOTA_EXCEEDED_CODE;
|
|
80
|
+
if (status === 429) return "RATE_LIMIT";
|
|
81
|
+
if (status === 400) {
|
|
82
|
+
if (isContextWindowExceededError(detail)) return CONTEXT_WINDOW_EXCEEDED_CODE;
|
|
83
|
+
return "INVALID_REQUEST";
|
|
84
|
+
}
|
|
85
|
+
if (status >= 500) return "SERVER";
|
|
86
|
+
return `HTTP_${status}`;
|
|
87
|
+
}
|
|
88
|
+
function providerErrorBody(error) {
|
|
89
|
+
if (error.responseBody === void 0) return void 0;
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(error.responseBody).error;
|
|
92
|
+
} catch {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function requestId(headers) {
|
|
97
|
+
if (headers === void 0) return void 0;
|
|
98
|
+
const value = headers["x-request-id"] ?? headers["x-openai-compatible-request-id"];
|
|
99
|
+
return value === void 0 || value.length === 0 ? void 0 : ProviderRequestId(value);
|
|
100
|
+
}
|
|
101
|
+
var OpenAICompatibleAdapter = class extends LlmAdapter {
|
|
102
|
+
config;
|
|
103
|
+
sdkProviders = /* @__PURE__ */ new Map();
|
|
104
|
+
constructor(config) {
|
|
105
|
+
super();
|
|
106
|
+
this.config = config;
|
|
107
|
+
}
|
|
108
|
+
profileOf(provider) {
|
|
109
|
+
const profile = this.config.profiles().get(provider);
|
|
110
|
+
if (profile === void 0) throw new LlmError(`OpenAI-compatible adapter does not own provider "${provider}"`, "NO_ADAPTER");
|
|
111
|
+
return profile;
|
|
112
|
+
}
|
|
113
|
+
modelOf(profile, model) {
|
|
114
|
+
return profile.models.find((entry) => entry.id === model);
|
|
115
|
+
}
|
|
116
|
+
sdkModel(profile, modelId) {
|
|
117
|
+
let byModel = this.sdkProviders.get(profile);
|
|
118
|
+
if (byModel === void 0) {
|
|
119
|
+
byModel = /* @__PURE__ */ new Map();
|
|
120
|
+
this.sdkProviders.set(profile, byModel);
|
|
121
|
+
}
|
|
122
|
+
let model = byModel.get(modelId);
|
|
123
|
+
if (model === void 0) {
|
|
124
|
+
model = createOpenAICompatible({
|
|
125
|
+
name: PROVIDER_OPTIONS_KEY,
|
|
126
|
+
baseURL: profile.baseURL,
|
|
127
|
+
headers: {
|
|
128
|
+
...profile.headers,
|
|
129
|
+
...attributionHeaders()
|
|
130
|
+
},
|
|
131
|
+
includeUsage: true,
|
|
132
|
+
convertUsage
|
|
133
|
+
}).chatModel(modelId);
|
|
134
|
+
byModel.set(modelId, model);
|
|
135
|
+
}
|
|
136
|
+
return model;
|
|
137
|
+
}
|
|
138
|
+
providerInfo(provider) {
|
|
139
|
+
return {
|
|
140
|
+
id: provider,
|
|
141
|
+
name: this.config.profiles().get(provider)?.displayName ?? provider
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
providerRetryPolicy(provider) {
|
|
145
|
+
return this.config.profiles().get(provider)?.retryPolicy;
|
|
146
|
+
}
|
|
147
|
+
listModels(provider) {
|
|
148
|
+
const profile = this.profileOf(provider);
|
|
149
|
+
return Promise.resolve(profile.models.map((model) => modelInfo(profile, model)));
|
|
150
|
+
}
|
|
151
|
+
resolveModel(provider, model, _signal) {
|
|
152
|
+
const profile = this.profileOf(provider);
|
|
153
|
+
const configured = this.modelOf(profile, model);
|
|
154
|
+
const contextWindow = configured?.contextWindow ?? profile.defaultContextWindow;
|
|
155
|
+
const maxTokens = configured?.maxTokens ?? profile.defaultMaxTokens;
|
|
156
|
+
return Promise.resolve({
|
|
157
|
+
...configured === void 0 ? {
|
|
158
|
+
provider,
|
|
159
|
+
id: model,
|
|
160
|
+
name: model,
|
|
161
|
+
inputModalities: ["text"]
|
|
162
|
+
} : modelInfo(profile, configured),
|
|
163
|
+
context: { contextWindow },
|
|
164
|
+
...maxTokens !== void 0 ? { defaultMaxTokens: maxTokens } : {},
|
|
165
|
+
...reasoningInfo(configured, profile.reasoning)
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
async *stream(options) {
|
|
169
|
+
const profile = this.profileOf(options.provider);
|
|
170
|
+
const model = this.modelOf(profile, options.model);
|
|
171
|
+
const hasImages = options.messages.some((message) => contentHasImage(message.content));
|
|
172
|
+
let attachments;
|
|
173
|
+
if (hasImages) {
|
|
174
|
+
if (model?.inputModalities.includes("image") !== true) throw new LlmError(`OpenAI-compatible model "${options.model}" does not accept image input.`, "UNSUPPORTED_CONTENT");
|
|
175
|
+
attachments = this.config.resolveAttachments?.();
|
|
176
|
+
if (attachments === void 0) throw new LlmError("OpenAI-compatible image conversion requires the durable attachment service.", "UNSUPPORTED_CONTENT");
|
|
177
|
+
}
|
|
178
|
+
const apiKey = await this.config.resolveApiKey(options.provider, profile);
|
|
179
|
+
const userId = this.config.resolveUserId();
|
|
180
|
+
const consumer = new AbortController();
|
|
181
|
+
const upstream = options.signal === void 0 ? consumer.signal : AbortSignal.any([options.signal, consumer.signal]);
|
|
182
|
+
const overall = profile.timeoutMs === void 0 ? void 0 : deadline(upstream, profile.timeoutMs, REQUEST_TIMEOUT_CODE);
|
|
183
|
+
const watchdog = idleWatchdog(overall?.signal ?? upstream, profile.streamIdleTimeoutMs, STREAM_IDLE_TIMEOUT_CODE);
|
|
184
|
+
try {
|
|
185
|
+
const callOptions = attachments === void 0 ? await serializeCallOptions(options, profile, model) : await serializeCallOptionsWithImages(options, profile, model, {
|
|
186
|
+
attachments,
|
|
187
|
+
maxRequestImageBytes: profile.maxRequestImageBytes,
|
|
188
|
+
signal: watchdog.signal
|
|
189
|
+
});
|
|
190
|
+
const sdkModel = this.sdkModel(profile, options.model);
|
|
191
|
+
let result;
|
|
192
|
+
try {
|
|
193
|
+
result = await sdkModel.doStream({
|
|
194
|
+
...callOptions,
|
|
195
|
+
abortSignal: watchdog.signal,
|
|
196
|
+
headers: {
|
|
197
|
+
...apiKey === void 0 ? {} : { authorization: `Bearer ${apiKey}` },
|
|
198
|
+
"x-openai-compatible-harness-user-id": String(userId),
|
|
199
|
+
...options.sessionId !== void 0 ? { "x-openai-compatible-harness-session-id": String(options.sessionId) } : {},
|
|
200
|
+
...options.purpose === "compaction" ? { "x-openai-compatible-harness-compact": "1" } : {}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
} catch (error) {
|
|
204
|
+
throw this.normalizeTransportError(error, profile);
|
|
205
|
+
}
|
|
206
|
+
const iterator = translate(result.stream)[Symbol.asyncIterator]();
|
|
207
|
+
let exhausted = false;
|
|
208
|
+
try {
|
|
209
|
+
while (true) {
|
|
210
|
+
const next = await watchdog.next(iterator);
|
|
211
|
+
if (next.done) {
|
|
212
|
+
exhausted = true;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
yield next.value;
|
|
216
|
+
}
|
|
217
|
+
} catch (error) {
|
|
218
|
+
if (timeoutOf(watchdog.signal, "LLM_STREAM_IDLE_TIMEOUT") !== void 0) throw new LlmError(`OpenAI-compatible stream idle timeout after ${profile.streamIdleTimeoutMs}ms`, "TIMEOUT", { cause: error });
|
|
219
|
+
if (profile.timeoutMs !== void 0 && timeoutOf(watchdog.signal, "LLM_REQUEST_TIMEOUT") !== void 0) throw new LlmError(`OpenAI-compatible request timeout after ${profile.timeoutMs}ms`, "TIMEOUT", { cause: error });
|
|
220
|
+
if (options.signal?.aborted) throw new LlmError("OpenAI-compatible request aborted by caller", "ABORTED", { cause: error });
|
|
221
|
+
if (error instanceof LlmError) throw error;
|
|
222
|
+
throw this.normalizeTransportError(error, profile);
|
|
223
|
+
} finally {
|
|
224
|
+
consumer.abort("OpenAI-compatible stream consumer stopped");
|
|
225
|
+
if (!exhausted) try {
|
|
226
|
+
await iterator.return(void 0);
|
|
227
|
+
} catch {}
|
|
228
|
+
}
|
|
229
|
+
} finally {
|
|
230
|
+
watchdog[Symbol.dispose]();
|
|
231
|
+
overall?.[Symbol.dispose]();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
normalizeTransportError(error, profile) {
|
|
235
|
+
if (error instanceof LlmError) return error;
|
|
236
|
+
if (APICallError.isInstance(error)) {
|
|
237
|
+
const providerError = providerErrorBody(error);
|
|
238
|
+
const message = typeof providerError?.message === "string" ? providerError.message : error.message;
|
|
239
|
+
const id = requestId(error.responseHeaders);
|
|
240
|
+
return new LlmError(message, httpErrorCode(error.statusCode ?? 0, providerError), {
|
|
241
|
+
...error.statusCode === void 0 ? {} : { status: error.statusCode },
|
|
242
|
+
...id === void 0 ? {} : { requestId: id },
|
|
243
|
+
cause: error
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
if (error instanceof Error) return new LlmError(`OpenAI-compatible API request to ${profile.baseURL} failed`, "TRANSPORT", { cause: error });
|
|
247
|
+
return new LlmError(`OpenAI-compatible API request to ${profile.baseURL} failed`, "TRANSPORT");
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
//#endregion
|
|
9
251
|
//#region src/index.ts
|
|
10
252
|
const name = "llm-openai-compatible";
|
|
11
253
|
const inject = ["llm"];
|
|
12
|
-
const NS =
|
|
13
|
-
/** Selectable reasoning levels a profile or model may declare. */
|
|
254
|
+
const NS = "llm-openai-compatible";
|
|
14
255
|
const REASONING_LEVELS = [
|
|
15
256
|
"off",
|
|
16
257
|
"low",
|
|
17
258
|
"high",
|
|
18
259
|
"max"
|
|
19
260
|
];
|
|
20
|
-
/** Accepted model input modalities. */
|
|
21
261
|
const MODEL_MODALITIES = ["text", "image"];
|
|
22
262
|
const modelSchema = z.object({
|
|
23
263
|
id: z.string().required(),
|
|
@@ -48,12 +288,10 @@ const providerSchema = z.object({
|
|
|
48
288
|
timeoutMs: z.number().min(Number.MIN_VALUE).max(MAX_TIMER_DELAY_MS),
|
|
49
289
|
retryPolicy: RetryPolicySchema
|
|
50
290
|
});
|
|
51
|
-
/** Runtime schema for {@link Config}. */
|
|
52
291
|
const Config = z.object({ providers: z.dict(providerSchema).default({}) });
|
|
53
292
|
function isReasoningEffort(value) {
|
|
54
293
|
return REASONING_LEVELS.includes(value);
|
|
55
294
|
}
|
|
56
|
-
/** Validate one model's declared reasoning efforts into detached form. */
|
|
57
295
|
function resolveReasoningEfforts(provider, modelId, value) {
|
|
58
296
|
if (value === void 0) return {};
|
|
59
297
|
if (value === false) return { reasoningEfforts: false };
|
|
@@ -70,7 +308,6 @@ function resolveReasoningEfforts(provider, modelId, value) {
|
|
|
70
308
|
}
|
|
71
309
|
return { reasoningEfforts: declaration };
|
|
72
310
|
}
|
|
73
|
-
/** Validate and detach one provider route's model catalog. */
|
|
74
311
|
function resolveModels(provider, models) {
|
|
75
312
|
if (models === void 0) return [];
|
|
76
313
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -96,21 +333,11 @@ function resolveModels(provider, models) {
|
|
|
96
333
|
};
|
|
97
334
|
});
|
|
98
335
|
}
|
|
99
|
-
/** A bounded finite number within `[lo, hi]`, or undefined. */
|
|
100
336
|
function bounded(value, lo, hi) {
|
|
101
337
|
if (value === void 0) return void 0;
|
|
102
338
|
if (!Number.isFinite(value) || value < lo || value > hi) return void 0;
|
|
103
339
|
return value;
|
|
104
340
|
}
|
|
105
|
-
/**
|
|
106
|
-
* The one explicit resolve step from a raw profile to validated connection
|
|
107
|
-
* facts. Programmatic construction may bypass Schemastery normalization, so
|
|
108
|
-
* every default and bound is re-judged here — for the composition entry at
|
|
109
|
-
* load (fail loud) and for each settings snapshot at its first use.
|
|
110
|
-
* @param provider - the route key owning this profile.
|
|
111
|
-
* @param source - raw profile from config or a resolved settings snapshot.
|
|
112
|
-
* @returns validated connection facts plus the credential reference.
|
|
113
|
-
*/
|
|
114
341
|
function resolveAdapterOptions(provider, source) {
|
|
115
342
|
if (provider.length === 0) throw new Error("llm-openai-compatible: provider names must be non-empty");
|
|
116
343
|
if (source.baseURL === void 0 || source.baseURL.length === 0) throw new Error(`llm-openai-compatible: provider "${provider}" requires a non-empty baseURL`);
|
|
@@ -154,31 +381,15 @@ function resolveAdapterOptions(provider, source) {
|
|
|
154
381
|
retryPolicy: resolveRetryPolicy(source.retryPolicy, `llm-openai-compatible: provider "${provider}" retryPolicy`)
|
|
155
382
|
};
|
|
156
383
|
}
|
|
157
|
-
/**
|
|
158
|
-
* Validate profiles and return a detached route-keyed map suitable for
|
|
159
|
-
* per-request reads. This is the one explicit resolve step, so an omitted dict
|
|
160
|
-
* resolves to the empty (dormant) route set here rather than through a hidden
|
|
161
|
-
* fallback.
|
|
162
|
-
* @param providers - configured provider profiles keyed by route.
|
|
163
|
-
* @returns validated profiles in configuration order.
|
|
164
|
-
*/
|
|
165
384
|
function resolveProfiles(providers) {
|
|
166
385
|
if (Array.isArray(providers)) throw new Error("llm-openai-compatible: providers is now a dict keyed by provider route, not an array of profiles");
|
|
167
386
|
const resolved = /* @__PURE__ */ new Map();
|
|
168
387
|
for (const [provider, source] of Object.entries(providers ?? {})) resolved.set(provider, resolveAdapterOptions(provider, source));
|
|
169
388
|
return resolved;
|
|
170
389
|
}
|
|
171
|
-
/**
|
|
172
|
-
* Reject a section this adapter could not serve. Registered as the settings
|
|
173
|
-
* namespace's validator, so an unserviceable profile is refused where it is
|
|
174
|
-
* written instead of being stored and then quietly disabling every route in
|
|
175
|
-
* the namespace.
|
|
176
|
-
* @param config - the resolved section to check.
|
|
177
|
-
*/
|
|
178
390
|
function assertServiceable(config) {
|
|
179
391
|
resolveProfiles(config.providers);
|
|
180
392
|
}
|
|
181
|
-
/** The registry captures these per route; a change here must re-register. */
|
|
182
393
|
function registrationFacts(profiles) {
|
|
183
394
|
return [...profiles.entries()].map(([provider, profile]) => ({
|
|
184
395
|
provider,
|
|
@@ -186,13 +397,6 @@ function registrationFacts(profiles) {
|
|
|
186
397
|
retryPolicy: profile.retryPolicy
|
|
187
398
|
})).sort((left, right) => left.provider.localeCompare(right.provider));
|
|
188
399
|
}
|
|
189
|
-
/**
|
|
190
|
-
* The configurable-provider directory: every route the current profiles
|
|
191
|
-
* declare. A hand-declared route has no catalog entry, so without this it
|
|
192
|
-
* would have no settings address and configuration surfaces could neither
|
|
193
|
-
* show nor edit it. The profile half is unconditional, which keeps a route
|
|
194
|
-
* already stored against a withheld provider editable and deletable.
|
|
195
|
-
*/
|
|
196
400
|
function directoryEntries(profiles) {
|
|
197
401
|
const entries = /* @__PURE__ */ new Map();
|
|
198
402
|
for (const [provider, profile] of profiles) entries.set(provider, {
|
|
@@ -204,12 +408,10 @@ function directoryEntries(profiles) {
|
|
|
204
408
|
});
|
|
205
409
|
return [...entries.values()];
|
|
206
410
|
}
|
|
207
|
-
/** Register one generic OpenAI-compatible adapter for all configured provider routes. */
|
|
208
411
|
function apply(ctx, config) {
|
|
209
412
|
let current = () => config;
|
|
210
413
|
let lastRaw;
|
|
211
414
|
let memoized;
|
|
212
|
-
/** The resolved profiles for the current configuration, memoized by raw identity. */
|
|
213
415
|
const profiles = () => {
|
|
214
416
|
const raw = current();
|
|
215
417
|
if (raw === lastRaw && memoized !== void 0) return memoized;
|
|
@@ -266,28 +468,28 @@ function apply(ctx, config) {
|
|
|
266
468
|
registeredFacts = facts;
|
|
267
469
|
};
|
|
268
470
|
ensureRegistrationFacts();
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
471
|
+
ctx.inject(["settings"], (settingsCtx) => {
|
|
472
|
+
settingsCtx.settings.installSection(ctx, NS, Config, config, {
|
|
473
|
+
validate: assertServiceable,
|
|
474
|
+
setSource: (source) => {
|
|
475
|
+
current = source;
|
|
476
|
+
},
|
|
477
|
+
onChange: () => {
|
|
478
|
+
try {
|
|
479
|
+
ensureRegistrationFacts();
|
|
480
|
+
} catch (error) {
|
|
481
|
+
ctx.logger.error("llm-openai-compatible: keeping the previously registered routes after a refused update");
|
|
482
|
+
ctx.logger.error(error);
|
|
483
|
+
}
|
|
484
|
+
try {
|
|
485
|
+
ensureDirectory();
|
|
486
|
+
} catch (error) {
|
|
487
|
+
ctx.logger.error("llm-openai-compatible: keeping the previous configurable-provider directory after a refused update");
|
|
488
|
+
ctx.logger.error(error);
|
|
489
|
+
}
|
|
286
490
|
}
|
|
287
|
-
}
|
|
491
|
+
});
|
|
288
492
|
});
|
|
289
493
|
}
|
|
290
494
|
//#endregion
|
|
291
495
|
export { Config, DEFAULT_CONTEXT_WINDOW, DEFAULT_MAX_REQUEST_IMAGE_BYTES, DEFAULT_MAX_TOKENS, DEFAULT_STREAM_IDLE_TIMEOUT_MS, MODEL_MODALITIES, NS, OpenAICompatibleAdapter, REASONING_LEVELS, apply, assertServiceable, inject, name, resolveAdapterOptions, resolveProfiles };
|
|
292
|
-
|
|
293
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
2
|
+
//#region src/invariant.d.ts
|
|
3
|
+
declare const name = "llm-openai-compatible-invariant";
|
|
4
|
+
declare const inject: string[];
|
|
5
|
+
declare const apply: (ctx: Context) => Promise<() => void>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { apply, inject, name };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/invariant.ts
|
|
2
|
+
const PACKAGE_NAME = "@morlay/dsh-llm-openai-compatible";
|
|
3
|
+
const name = "llm-openai-compatible-invariant";
|
|
4
|
+
const inject = ["invariants"];
|
|
5
|
+
const install = () => {};
|
|
6
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
7
|
+
//#endregion
|
|
8
|
+
export { apply, inject, name };
|