@prismshadow/mmsp 0.5.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 +121 -0
- package/dist/ant_messages/client.d.ts +59 -0
- package/dist/ant_messages/client.d.ts.map +1 -0
- package/dist/ant_messages/client.js +419 -0
- package/dist/ant_messages/index.d.ts +2 -0
- package/dist/ant_messages/index.d.ts.map +1 -0
- package/dist/ant_messages/index.js +18 -0
- package/dist/anthropic_official/client.d.ts +63 -0
- package/dist/anthropic_official/client.d.ts.map +1 -0
- package/dist/anthropic_official/client.js +540 -0
- package/dist/anthropic_official/index.d.ts +2 -0
- package/dist/anthropic_official/index.d.ts.map +1 -0
- package/dist/anthropic_official/index.js +18 -0
- package/dist/autoClient.d.ts +96 -0
- package/dist/autoClient.d.ts.map +1 -0
- package/dist/autoClient.js +258 -0
- package/dist/baseClient.d.ts +111 -0
- package/dist/baseClient.d.ts.map +1 -0
- package/dist/baseClient.js +284 -0
- package/dist/deepseek_official/client.d.ts +60 -0
- package/dist/deepseek_official/client.d.ts.map +1 -0
- package/dist/deepseek_official/client.js +407 -0
- package/dist/deepseek_official/index.d.ts +2 -0
- package/dist/deepseek_official/index.d.ts.map +1 -0
- package/dist/deepseek_official/index.js +18 -0
- package/dist/errors.d.ts +84 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +140 -0
- package/dist/gemini_generate_content/client.d.ts +86 -0
- package/dist/gemini_generate_content/client.d.ts.map +1 -0
- package/dist/gemini_generate_content/client.js +809 -0
- package/dist/gemini_generate_content/index.d.ts +2 -0
- package/dist/gemini_generate_content/index.d.ts.map +1 -0
- package/dist/gemini_generate_content/index.js +18 -0
- package/dist/gemini_official/client.d.ts +87 -0
- package/dist/gemini_official/client.d.ts.map +1 -0
- package/dist/gemini_official/client.js +780 -0
- package/dist/gemini_official/index.d.ts +2 -0
- package/dist/gemini_official/index.d.ts.map +1 -0
- package/dist/gemini_official/index.js +18 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/integration/index.d.ts +1 -0
- package/dist/integration/index.d.ts.map +1 -0
- package/dist/integration/index.js +14 -0
- package/dist/integration/playground.d.ts +17 -0
- package/dist/integration/playground.d.ts.map +1 -0
- package/dist/integration/playground.js +3246 -0
- package/dist/integration/tracer.d.ts +188 -0
- package/dist/integration/tracer.d.ts.map +1 -0
- package/dist/integration/tracer.js +1928 -0
- package/dist/legacy.d.ts +13 -0
- package/dist/legacy.d.ts.map +1 -0
- package/dist/legacy.js +59 -0
- package/dist/minimax_official/client.d.ts +43 -0
- package/dist/minimax_official/client.d.ts.map +1 -0
- package/dist/minimax_official/client.js +346 -0
- package/dist/minimax_official/index.d.ts +2 -0
- package/dist/minimax_official/index.d.ts.map +1 -0
- package/dist/minimax_official/index.js +18 -0
- package/dist/moonshot_official/client.d.ts +73 -0
- package/dist/moonshot_official/client.d.ts.map +1 -0
- package/dist/moonshot_official/client.js +477 -0
- package/dist/moonshot_official/index.d.ts +2 -0
- package/dist/moonshot_official/index.d.ts.map +1 -0
- package/dist/moonshot_official/index.js +18 -0
- package/dist/openai_chat/client.d.ts +67 -0
- package/dist/openai_chat/client.d.ts.map +1 -0
- package/dist/openai_chat/client.js +419 -0
- package/dist/openai_chat/index.d.ts +2 -0
- package/dist/openai_chat/index.d.ts.map +1 -0
- package/dist/openai_chat/index.js +18 -0
- package/dist/openai_chat_vllm_adapter/client.d.ts +15 -0
- package/dist/openai_chat_vllm_adapter/client.d.ts.map +1 -0
- package/dist/openai_chat_vllm_adapter/client.js +118 -0
- package/dist/openai_chat_vllm_adapter/index.d.ts +2 -0
- package/dist/openai_chat_vllm_adapter/index.d.ts.map +1 -0
- package/dist/openai_chat_vllm_adapter/index.js +5 -0
- package/dist/openai_embedding/client.d.ts +46 -0
- package/dist/openai_embedding/client.d.ts.map +1 -0
- package/dist/openai_embedding/client.js +124 -0
- package/dist/openai_embedding/index.d.ts +2 -0
- package/dist/openai_embedding/index.d.ts.map +1 -0
- package/dist/openai_embedding/index.js +18 -0
- package/dist/openai_official/client.d.ts +61 -0
- package/dist/openai_official/client.d.ts.map +1 -0
- package/dist/openai_official/client.js +464 -0
- package/dist/openai_official/index.d.ts +2 -0
- package/dist/openai_official/index.d.ts.map +1 -0
- package/dist/openai_official/index.js +18 -0
- package/dist/openai_responses/client.d.ts +61 -0
- package/dist/openai_responses/client.d.ts.map +1 -0
- package/dist/openai_responses/client.js +449 -0
- package/dist/openai_responses/index.d.ts +2 -0
- package/dist/openai_responses/index.d.ts.map +1 -0
- package/dist/openai_responses/index.js +18 -0
- package/dist/registry.d.ts +49 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +798 -0
- package/dist/streamItems.d.ts +36 -0
- package/dist/streamItems.d.ts.map +1 -0
- package/dist/streamItems.js +183 -0
- package/dist/types.d.ts +190 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +37 -0
- package/dist/utils.d.ts +99 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +312 -0
- package/dist/zai_official/client.d.ts +78 -0
- package/dist/zai_official/client.d.ts.map +1 -0
- package/dist/zai_official/client.js +420 -0
- package/dist/zai_official/index.d.ts +2 -0
- package/dist/zai_official/index.d.ts.map +1 -0
- package/dist/zai_official/index.js +18 -0
- package/package.json +68 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { LLMClient } from "./baseClient";
|
|
2
|
+
import { UniConfig, UniEvent, UniMessage } from "./types";
|
|
3
|
+
export declare const OFFICIAL_CLIENT_TYPES: readonly ["openai-official", "anthropic-official", "gemini-official", "zai-official", "moonshot-official", "deepseek-official", "minimax-official"];
|
|
4
|
+
export declare const COMPATIBLE_CLIENT_TYPES: readonly ["openai-responses", "openai-chat", "openai-chat-vllm-adapter", "openai-embedding", "ant-messages", "gemini-generate-content"];
|
|
5
|
+
export declare const MODEL_FAMILIES: [string, string][];
|
|
6
|
+
/**
|
|
7
|
+
* The official client a model id routes to on its own, or null when its family is unknown.
|
|
8
|
+
*
|
|
9
|
+
* @param model - The model id, in any casing
|
|
10
|
+
* @returns One of OFFICIAL_CLIENT_TYPES, or null
|
|
11
|
+
*/
|
|
12
|
+
export declare function clientTypeForModel(model: string): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* The one client to call: it creates the client a client type names and forwards to it.
|
|
15
|
+
*
|
|
16
|
+
* A client type is one of OFFICIAL_CLIENT_TYPES or COMPATIBLE_CLIENT_TYPES, given as
|
|
17
|
+
* `clientType` or as the CLIENT_TYPE environment variable. Without one, the family the model
|
|
18
|
+
* id begins with names its official client: `gpt-` routes to `openai-official`, `claude-` to
|
|
19
|
+
* `anthropic-official`, and so on. A model id of no known family throws, and asks for a
|
|
20
|
+
* client type.
|
|
21
|
+
*
|
|
22
|
+
* This client is stateful - it knows the model name at initialization and maintains
|
|
23
|
+
* conversation history for that specific model.
|
|
24
|
+
*/
|
|
25
|
+
export declare class AutoLLMClient extends LLMClient {
|
|
26
|
+
private _client;
|
|
27
|
+
private _clientType;
|
|
28
|
+
private _named;
|
|
29
|
+
/**
|
|
30
|
+
* Initialize AutoLLMClient with a model and, unless the model id names it, a client type.
|
|
31
|
+
*
|
|
32
|
+
* @param options - Configuration object with model, apiKey, baseUrl, clientType and defaultHeaders
|
|
33
|
+
*/
|
|
34
|
+
constructor(options: {
|
|
35
|
+
model: string;
|
|
36
|
+
apiKey?: string;
|
|
37
|
+
baseUrl?: string | null;
|
|
38
|
+
clientType?: string | null;
|
|
39
|
+
defaultHeaders?: Record<string, string>;
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* Delegate to underlying client's transformUniConfigToModelConfig.
|
|
43
|
+
*/
|
|
44
|
+
transformUniConfigToModelConfig(config: UniConfig): any;
|
|
45
|
+
/**
|
|
46
|
+
* Delegate to underlying client's transformUniMessageToModelInput.
|
|
47
|
+
*/
|
|
48
|
+
transformUniMessageToModelInput(messages: UniMessage[], signal?: AbortSignal): any;
|
|
49
|
+
/**
|
|
50
|
+
* Delegate to underlying client's transformModelOutputToUniEvent.
|
|
51
|
+
*/
|
|
52
|
+
transformModelOutputToUniEvent(modelOutput: any): UniEvent;
|
|
53
|
+
/**
|
|
54
|
+
* Not implemented - use streamingResponse instead.
|
|
55
|
+
*/
|
|
56
|
+
_streamingResponseInternal(_options: any): AsyncGenerator<UniEvent>;
|
|
57
|
+
/**
|
|
58
|
+
* Route to underlying client's streamingResponse.
|
|
59
|
+
*/
|
|
60
|
+
streamingResponse(options: {
|
|
61
|
+
messages: UniMessage[];
|
|
62
|
+
config: UniConfig;
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
}): AsyncGenerator<UniEvent>;
|
|
65
|
+
/**
|
|
66
|
+
* Route to underlying client's streamingResponseStateful.
|
|
67
|
+
*/
|
|
68
|
+
streamingResponseStateful(options: {
|
|
69
|
+
message: UniMessage;
|
|
70
|
+
config: UniConfig;
|
|
71
|
+
signal?: AbortSignal;
|
|
72
|
+
}): AsyncGenerator<UniEvent>;
|
|
73
|
+
/**
|
|
74
|
+
* Clear history in the underlying client.
|
|
75
|
+
*/
|
|
76
|
+
clearHistory(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Get history from the underlying client.
|
|
79
|
+
*/
|
|
80
|
+
getHistory(): UniMessage[];
|
|
81
|
+
/**
|
|
82
|
+
* Set history in the underlying client.
|
|
83
|
+
*/
|
|
84
|
+
setHistory(history: UniMessage[]): void;
|
|
85
|
+
/**
|
|
86
|
+
* List the model ids the endpoint serves that this client can be used for.
|
|
87
|
+
*
|
|
88
|
+
* A client named by its type speaks for whatever the endpoint serves, so its listing is
|
|
89
|
+
* returned whole. A client deduced from a model id serves the ids that deduce to it as well,
|
|
90
|
+
* so a gateway fronting many vendors is filtered down to that client's own models.
|
|
91
|
+
*
|
|
92
|
+
* @returns The model ids, in the order the endpoint returned them.
|
|
93
|
+
*/
|
|
94
|
+
listModels(): Promise<string[]>;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=autoClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autoClient.d.ts","sourceRoot":"","sources":["../src/autoClient.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAczC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAW1D,eAAO,MAAM,qBAAqB,qJAQxB,CAAC;AAGX,eAAO,MAAM,uBAAuB,yIAO1B,CAAC;AAGX,eAAO,MAAM,cAAc,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAS5C,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAQ/D;AA6DD;;;;;;;;;;;GAWG;AACH,qBAAa,aAAc,SAAQ,SAAS;IAC1C,OAAO,CAAC,OAAO,CAAY;IAC3B,OAAO,CAAC,WAAW,CAAS;IAE5B,OAAO,CAAC,MAAM,CAAU;IAExB;;;;OAIG;gBACS,OAAO,EAAE;QACnB,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACxB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC3B,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KACzC;IA4BD;;OAEG;IAEH,+BAA+B,CAAC,MAAM,EAAE,SAAS,GAAG,GAAG;IAIvD;;OAEG;IAEH,+BAA+B,CAC7B,QAAQ,EAAE,UAAU,EAAE,EACtB,MAAM,CAAC,EAAE,WAAW,GACnB,GAAG;IAKN;;OAEG;IAEH,8BAA8B,CAAC,WAAW,EAAE,GAAG,GAAG,QAAQ;IAI1D;;OAEG;IAEI,0BAA0B,CAAC,QAAQ,EAAE,GAAG,GAAG,cAAc,CAAC,QAAQ,CAAC;IAI1E;;OAEG;IACI,iBAAiB,CAAC,OAAO,EAAE;QAChC,QAAQ,EAAE,UAAU,EAAE,CAAC;QACvB,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,cAAc,CAAC,QAAQ,CAAC;IAU5B;;OAEG;IACI,yBAAyB,CAAC,OAAO,EAAE;QACxC,OAAO,EAAE,UAAU,CAAC;QACpB,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,cAAc,CAAC,QAAQ,CAAC;IAU5B;;OAEG;IACH,YAAY,IAAI,IAAI;IAIpB;;OAEG;IACH,UAAU,IAAI,UAAU,EAAE;IAI1B;;OAEG;IACH,UAAU,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,IAAI;IAIvC;;;;;;;;OAQG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAStC"}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2025 Prism Shadow. and/or its affiliates
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.AutoLLMClient = exports.MODEL_FAMILIES = exports.COMPATIBLE_CLIENT_TYPES = exports.OFFICIAL_CLIENT_TYPES = void 0;
|
|
17
|
+
exports.clientTypeForModel = clientTypeForModel;
|
|
18
|
+
const baseClient_1 = require("./baseClient");
|
|
19
|
+
const gemini_official_1 = require("./gemini_official");
|
|
20
|
+
const gemini_generate_content_1 = require("./gemini_generate_content");
|
|
21
|
+
const anthropic_official_1 = require("./anthropic_official");
|
|
22
|
+
const openai_official_1 = require("./openai_official");
|
|
23
|
+
const zai_official_1 = require("./zai_official");
|
|
24
|
+
const moonshot_official_1 = require("./moonshot_official");
|
|
25
|
+
const openai_chat_1 = require("./openai_chat");
|
|
26
|
+
const openai_responses_1 = require("./openai_responses");
|
|
27
|
+
const ant_messages_1 = require("./ant_messages");
|
|
28
|
+
const openai_embedding_1 = require("./openai_embedding");
|
|
29
|
+
const deepseek_official_1 = require("./deepseek_official");
|
|
30
|
+
const minimax_official_1 = require("./minimax_official");
|
|
31
|
+
const openai_chat_vllm_adapter_1 = require("./openai_chat_vllm_adapter");
|
|
32
|
+
// An official client speaks its vendor's own API, knows the vendor's models, and reads the
|
|
33
|
+
// vendor's key from the environment.
|
|
34
|
+
exports.OFFICIAL_CLIENT_TYPES = [
|
|
35
|
+
"openai-official",
|
|
36
|
+
"anthropic-official",
|
|
37
|
+
"gemini-official",
|
|
38
|
+
"zai-official",
|
|
39
|
+
"moonshot-official",
|
|
40
|
+
"deepseek-official",
|
|
41
|
+
"minimax-official",
|
|
42
|
+
];
|
|
43
|
+
// A compatible client speaks one wire protocol for whatever endpoint serves it.
|
|
44
|
+
exports.COMPATIBLE_CLIENT_TYPES = [
|
|
45
|
+
"openai-responses",
|
|
46
|
+
"openai-chat",
|
|
47
|
+
"openai-chat-vllm-adapter",
|
|
48
|
+
"openai-embedding",
|
|
49
|
+
"ant-messages",
|
|
50
|
+
"gemini-generate-content",
|
|
51
|
+
];
|
|
52
|
+
// Without a client type, the family a model id begins with names its official client.
|
|
53
|
+
exports.MODEL_FAMILIES = [
|
|
54
|
+
["gpt-", "openai-official"],
|
|
55
|
+
["text-embedding-", "openai-official"],
|
|
56
|
+
["claude-", "anthropic-official"],
|
|
57
|
+
["gemini-", "gemini-official"],
|
|
58
|
+
["glm-", "zai-official"],
|
|
59
|
+
["kimi-", "moonshot-official"],
|
|
60
|
+
["deepseek-", "deepseek-official"],
|
|
61
|
+
["minimax-", "minimax-official"],
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
* The official client a model id routes to on its own, or null when its family is unknown.
|
|
65
|
+
*
|
|
66
|
+
* @param model - The model id, in any casing
|
|
67
|
+
* @returns One of OFFICIAL_CLIENT_TYPES, or null
|
|
68
|
+
*/
|
|
69
|
+
function clientTypeForModel(model) {
|
|
70
|
+
const lowered = model.toLowerCase();
|
|
71
|
+
for (const [prefix, clientType] of exports.MODEL_FAMILIES) {
|
|
72
|
+
if (lowered.startsWith(prefix)) {
|
|
73
|
+
return clientType;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
function clientTypes() {
|
|
79
|
+
return (`official clients: ${exports.OFFICIAL_CLIENT_TYPES.join(", ")}; ` +
|
|
80
|
+
`compatible clients: ${exports.COMPATIBLE_CLIENT_TYPES.join(", ")}`);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The client class a client type names, or null when no client is named so.
|
|
84
|
+
*
|
|
85
|
+
* @param clientType - A lowercased client type
|
|
86
|
+
* @param model - The model id, which tells an official client's embedding models apart
|
|
87
|
+
* @param apiKey - The key, which tells a Vertex AI service account apart
|
|
88
|
+
*/
|
|
89
|
+
function clientClass(clientType, model, apiKey) {
|
|
90
|
+
switch (clientType) {
|
|
91
|
+
case "openai-official":
|
|
92
|
+
// OpenAI serves embedding models through its Embeddings API
|
|
93
|
+
return model.toLowerCase().startsWith("text-embedding-")
|
|
94
|
+
? openai_embedding_1.OpenaiEmbeddingClient
|
|
95
|
+
: openai_official_1.OpenAIOfficialClient;
|
|
96
|
+
case "anthropic-official":
|
|
97
|
+
return anthropic_official_1.AnthropicOfficialClient;
|
|
98
|
+
case "gemini-official":
|
|
99
|
+
// Vertex AI serves none of the Gemini models through the Interactions API, so a
|
|
100
|
+
// service-account JSON key, which only Vertex AI takes, speaks generateContent
|
|
101
|
+
return (apiKey || process.env.GEMINI_API_KEY || "").startsWith("{")
|
|
102
|
+
? gemini_generate_content_1.GeminiGenerateContentClient
|
|
103
|
+
: gemini_official_1.GeminiOfficialClient;
|
|
104
|
+
case "zai-official":
|
|
105
|
+
return zai_official_1.ZAIOfficialClient;
|
|
106
|
+
case "moonshot-official":
|
|
107
|
+
return moonshot_official_1.MoonshotOfficialClient;
|
|
108
|
+
case "deepseek-official":
|
|
109
|
+
return deepseek_official_1.DeepSeekOfficialClient;
|
|
110
|
+
case "minimax-official":
|
|
111
|
+
return minimax_official_1.MiniMaxOfficialClient;
|
|
112
|
+
case "openai-responses":
|
|
113
|
+
return openai_responses_1.OpenaiResponsesClient;
|
|
114
|
+
case "openai-chat":
|
|
115
|
+
case "openai":
|
|
116
|
+
return openai_chat_1.OpenaiChatClient;
|
|
117
|
+
case "openai-chat-vllm-adapter":
|
|
118
|
+
return openai_chat_vllm_adapter_1.OpenaiChatVllmAdapterClient;
|
|
119
|
+
case "openai-embedding":
|
|
120
|
+
return openai_embedding_1.OpenaiEmbeddingClient;
|
|
121
|
+
case "ant-messages":
|
|
122
|
+
return ant_messages_1.AntMessagesClient;
|
|
123
|
+
case "gemini-generate-content":
|
|
124
|
+
return gemini_generate_content_1.GeminiGenerateContentClient;
|
|
125
|
+
default:
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The one client to call: it creates the client a client type names and forwards to it.
|
|
131
|
+
*
|
|
132
|
+
* A client type is one of OFFICIAL_CLIENT_TYPES or COMPATIBLE_CLIENT_TYPES, given as
|
|
133
|
+
* `clientType` or as the CLIENT_TYPE environment variable. Without one, the family the model
|
|
134
|
+
* id begins with names its official client: `gpt-` routes to `openai-official`, `claude-` to
|
|
135
|
+
* `anthropic-official`, and so on. A model id of no known family throws, and asks for a
|
|
136
|
+
* client type.
|
|
137
|
+
*
|
|
138
|
+
* This client is stateful - it knows the model name at initialization and maintains
|
|
139
|
+
* conversation history for that specific model.
|
|
140
|
+
*/
|
|
141
|
+
class AutoLLMClient extends baseClient_1.LLMClient {
|
|
142
|
+
/**
|
|
143
|
+
* Initialize AutoLLMClient with a model and, unless the model id names it, a client type.
|
|
144
|
+
*
|
|
145
|
+
* @param options - Configuration object with model, apiKey, baseUrl, clientType and defaultHeaders
|
|
146
|
+
*/
|
|
147
|
+
constructor(options) {
|
|
148
|
+
super();
|
|
149
|
+
const named = options.clientType || process.env.CLIENT_TYPE;
|
|
150
|
+
const clientType = named
|
|
151
|
+
? named.toLowerCase()
|
|
152
|
+
: clientTypeForModel(options.model);
|
|
153
|
+
if (clientType === null) {
|
|
154
|
+
throw new Error(`No client for model ${JSON.stringify(options.model)}: its family is not known. ` +
|
|
155
|
+
`Pass clientType, one of the ${clientTypes()}.`);
|
|
156
|
+
}
|
|
157
|
+
const ClientClass = clientClass(clientType, options.model, options.apiKey);
|
|
158
|
+
if (ClientClass === null) {
|
|
159
|
+
throw new Error(`Unknown client type ${JSON.stringify(named)}. Pass one of the ${clientTypes()}.`);
|
|
160
|
+
}
|
|
161
|
+
this._clientType = clientType;
|
|
162
|
+
this._named = Boolean(named);
|
|
163
|
+
this._client = new ClientClass({
|
|
164
|
+
model: options.model,
|
|
165
|
+
apiKey: options.apiKey,
|
|
166
|
+
baseUrl: options.baseUrl,
|
|
167
|
+
defaultHeaders: options.defaultHeaders,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Delegate to underlying client's transformUniConfigToModelConfig.
|
|
172
|
+
*/
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
174
|
+
transformUniConfigToModelConfig(config) {
|
|
175
|
+
return this._client.transformUniConfigToModelConfig(config);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Delegate to underlying client's transformUniMessageToModelInput.
|
|
179
|
+
*/
|
|
180
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
181
|
+
transformUniMessageToModelInput(messages, signal) {
|
|
182
|
+
return this._client.transformUniMessageToModelInput(messages, signal);
|
|
183
|
+
}
|
|
184
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
185
|
+
/**
|
|
186
|
+
* Delegate to underlying client's transformModelOutputToUniEvent.
|
|
187
|
+
*/
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
189
|
+
transformModelOutputToUniEvent(modelOutput) {
|
|
190
|
+
return this._client.transformModelOutputToUniEvent(modelOutput);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Not implemented - use streamingResponse instead.
|
|
194
|
+
*/
|
|
195
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, require-yield
|
|
196
|
+
async *_streamingResponseInternal(_options) {
|
|
197
|
+
throw new Error("Please use streamingResponse instead.");
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Route to underlying client's streamingResponse.
|
|
201
|
+
*/
|
|
202
|
+
async *streamingResponse(options) {
|
|
203
|
+
for await (const event of this._client.streamingResponse({
|
|
204
|
+
messages: options.messages,
|
|
205
|
+
config: options.config,
|
|
206
|
+
signal: options.signal,
|
|
207
|
+
})) {
|
|
208
|
+
yield event;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Route to underlying client's streamingResponseStateful.
|
|
213
|
+
*/
|
|
214
|
+
async *streamingResponseStateful(options) {
|
|
215
|
+
for await (const event of this._client.streamingResponseStateful({
|
|
216
|
+
message: options.message,
|
|
217
|
+
config: options.config,
|
|
218
|
+
signal: options.signal,
|
|
219
|
+
})) {
|
|
220
|
+
yield event;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Clear history in the underlying client.
|
|
225
|
+
*/
|
|
226
|
+
clearHistory() {
|
|
227
|
+
this._client.clearHistory();
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Get history from the underlying client.
|
|
231
|
+
*/
|
|
232
|
+
getHistory() {
|
|
233
|
+
return this._client.getHistory();
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Set history in the underlying client.
|
|
237
|
+
*/
|
|
238
|
+
setHistory(history) {
|
|
239
|
+
this._client.setHistory(history);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* List the model ids the endpoint serves that this client can be used for.
|
|
243
|
+
*
|
|
244
|
+
* A client named by its type speaks for whatever the endpoint serves, so its listing is
|
|
245
|
+
* returned whole. A client deduced from a model id serves the ids that deduce to it as well,
|
|
246
|
+
* so a gateway fronting many vendors is filtered down to that client's own models.
|
|
247
|
+
*
|
|
248
|
+
* @returns The model ids, in the order the endpoint returned them.
|
|
249
|
+
*/
|
|
250
|
+
async listModels() {
|
|
251
|
+
const modelIds = await this._client.listModels();
|
|
252
|
+
if (this._named) {
|
|
253
|
+
return modelIds;
|
|
254
|
+
}
|
|
255
|
+
return modelIds.filter((modelId) => clientTypeForModel(modelId) === this._clientType);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
exports.AutoLLMClient = AutoLLMClient;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { UniConfig, UniEvent, UniMessage } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for LLM clients.
|
|
4
|
+
*
|
|
5
|
+
* All model-specific clients must inherit from this class and implement
|
|
6
|
+
* the required abstract methods for complete SDK abstraction.
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class LLMClient {
|
|
9
|
+
protected _model: string;
|
|
10
|
+
private _history;
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Transform universal configuration to model-specific configuration.
|
|
14
|
+
*
|
|
15
|
+
* @param config - Universal configuration object
|
|
16
|
+
* @returns Model-specific configuration object
|
|
17
|
+
*/
|
|
18
|
+
abstract transformUniConfigToModelConfig(config: UniConfig): any;
|
|
19
|
+
/**
|
|
20
|
+
* Transform universal message format to model-specific input format.
|
|
21
|
+
*
|
|
22
|
+
* @param messages - List of universal message objects
|
|
23
|
+
* @returns Model-specific input format (e.g., Gemini's Content list, OpenAI's messages array)
|
|
24
|
+
*/
|
|
25
|
+
abstract transformUniMessageToModelInput(messages: UniMessage[], signal?: AbortSignal): any;
|
|
26
|
+
/**
|
|
27
|
+
* Transform one event of the provider's stream into a universal event, which the base class
|
|
28
|
+
* turns into the public stream.
|
|
29
|
+
*
|
|
30
|
+
* content_items holds the deltas the wire event carries, in wire order, and never a done item:
|
|
31
|
+
* the base class closes an item when the next one begins or the stream ends. The deltas of one
|
|
32
|
+
* item are contiguous and carry the same `fidelity.item_id`, the provider's id for the item
|
|
33
|
+
* where it has one, which never reaches the public stream. Its event_type is "stop" on the wire
|
|
34
|
+
* events that report usage_metadata and/or finish_reason, in pieces the base class merges field
|
|
35
|
+
* by field, and "delta" otherwise; a "delta" event carries neither.
|
|
36
|
+
*
|
|
37
|
+
* @param modelOutput - Model-specific output object (streaming chunk)
|
|
38
|
+
* @returns Universal event object, an empty delta event when the wire event carries nothing
|
|
39
|
+
* universal
|
|
40
|
+
*/
|
|
41
|
+
abstract transformModelOutputToUniEvent(modelOutput: any): UniEvent;
|
|
42
|
+
/**
|
|
43
|
+
* Concatenate a stream of universal events into a single universal message.
|
|
44
|
+
*
|
|
45
|
+
* @param events - List of universal events from streaming response
|
|
46
|
+
* @returns Complete universal message object: every done item in stream order, with the
|
|
47
|
+
* usage, finish reason and timestamp of the stop event
|
|
48
|
+
*/
|
|
49
|
+
concatUniEventsToUniMessage(events: UniEvent[]): UniMessage;
|
|
50
|
+
/**
|
|
51
|
+
* Internal method to handle streaming response.
|
|
52
|
+
*
|
|
53
|
+
* Each model client implements it to send the request and yield one universal event per event
|
|
54
|
+
* of the provider's stream; streamingResponse turns them into the public stream.
|
|
55
|
+
*
|
|
56
|
+
* @param options - Object containing messages and config
|
|
57
|
+
* @yields Universal events of the streaming response
|
|
58
|
+
*/
|
|
59
|
+
abstract _streamingResponseInternal(options: {
|
|
60
|
+
messages: UniMessage[];
|
|
61
|
+
config: UniConfig;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}): AsyncGenerator<UniEvent>;
|
|
64
|
+
/**
|
|
65
|
+
* List the model ids the configured endpoint serves.
|
|
66
|
+
*
|
|
67
|
+
* @returns The model ids, in the order the endpoint returned them.
|
|
68
|
+
*/
|
|
69
|
+
abstract listModels(): Promise<string[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Generate content in streaming mode (stateless).
|
|
72
|
+
*
|
|
73
|
+
* @param options - Object containing messages and config
|
|
74
|
+
* @yields Delta events, each carrying one delta or done item, then exactly one stop event
|
|
75
|
+
* carrying the usage and the finish reason
|
|
76
|
+
*/
|
|
77
|
+
streamingResponse(options: {
|
|
78
|
+
messages: UniMessage[];
|
|
79
|
+
config: UniConfig;
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
}): AsyncGenerator<UniEvent>;
|
|
82
|
+
/**
|
|
83
|
+
* Generate content in streaming mode (stateful).
|
|
84
|
+
*
|
|
85
|
+
* @param message - Latest universal message object to add to conversation
|
|
86
|
+
* @param config - Universal configuration object
|
|
87
|
+
* @yields Universal events from the streaming response
|
|
88
|
+
*/
|
|
89
|
+
streamingResponseStateful(options: {
|
|
90
|
+
message: UniMessage;
|
|
91
|
+
config: UniConfig;
|
|
92
|
+
signal?: AbortSignal;
|
|
93
|
+
}): AsyncGenerator<UniEvent>;
|
|
94
|
+
/**
|
|
95
|
+
* Clear the message history.
|
|
96
|
+
*/
|
|
97
|
+
clearHistory(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Get the current message history.
|
|
100
|
+
*
|
|
101
|
+
* @returns Copy of the current message history
|
|
102
|
+
*/
|
|
103
|
+
getHistory(): UniMessage[];
|
|
104
|
+
/**
|
|
105
|
+
* Replace the message history with a copy of the provided history.
|
|
106
|
+
*
|
|
107
|
+
* @param history - List of universal message objects to set as the new history
|
|
108
|
+
*/
|
|
109
|
+
setHistory(history: UniMessage[]): void;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=baseClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"baseClient.d.ts","sourceRoot":"","sources":["../src/baseClient.ts"],"names":[],"mappings":"AAiBA,OAAO,EAKL,SAAS,EACT,QAAQ,EACR,UAAU,EAEX,MAAM,SAAS,CAAC;AAuHjB;;;;;GAKG;AACH,8BAAsB,SAAS;IAC7B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,QAAQ,CAAe;;IAO/B;;;;;OAKG;IAEH,QAAQ,CAAC,+BAA+B,CAAC,MAAM,EAAE,SAAS,GAAG,GAAG;IAEhE;;;;;OAKG;IACH,QAAQ,CAAC,+BAA+B,CACtC,QAAQ,EAAE,UAAU,EAAE,EACtB,MAAM,CAAC,EAAE,WAAW,GACnB,GAAG;IAEN;;;;;;;;;;;;;;OAcG;IAEH,QAAQ,CAAC,8BAA8B,CAAC,WAAW,EAAE,GAAG,GAAG,QAAQ;IAEnE;;;;;;OAMG;IACH,2BAA2B,CAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU;IAwB3D;;;;;;;;OAQG;IACH,QAAQ,CAAC,0BAA0B,CAAC,OAAO,EAAE;QAC3C,QAAQ,EAAE,UAAU,EAAE,CAAC;QACvB,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,cAAc,CAAC,QAAQ,CAAC;IAE5B;;;;OAIG;IACH,QAAQ,CAAC,UAAU,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAExC;;;;;;OAMG;IACI,iBAAiB,CAAC,OAAO,EAAE;QAChC,QAAQ,EAAE,UAAU,EAAE,CAAC;QACvB,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,cAAc,CAAC,QAAQ,CAAC;IA4C5B;;;;;;OAMG;IACI,yBAAyB,CAAC,OAAO,EAAE;QACxC,OAAO,EAAE,UAAU,CAAC;QACpB,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,cAAc,CAAC,QAAQ,CAAC;IAoB5B;;OAEG;IACH,YAAY,IAAI,IAAI;IAIpB;;;;OAIG;IACH,UAAU,IAAI,UAAU,EAAE;IAI1B;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,IAAI;CAGxC"}
|