.
This commit is contained in:
1 parent
7fa293c9e8
commit
ae44aa03db
67 files changed
+6164
-243
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { buildOpenAiRequest, normalizeProviderResponse } from "./shared.js";
|
||||
import type { ProviderAdapter } from "./types.js";
|
||||
|
||||
export const cliproxyProvider: ProviderAdapter = {
|
||||
name: "cliproxy",
|
||||
capabilities: {
|
||||
jsonMode: true,
|
||||
promptCache: false,
|
||||
toolCalls: "openai",
|
||||
},
|
||||
createHealthUrl(endpoint: string): string {
|
||||
try {
|
||||
const url = new URL(endpoint);
|
||||
url.pathname = "/";
|
||||
url.search = "";
|
||||
url.hash = "";
|
||||
return url.toString();
|
||||
} catch {
|
||||
const normalized = endpoint.replace(/\/v1\/chat\/completions\/?$/u, "");
|
||||
return `${normalized.replace(/\/+$/u, "")}/`;
|
||||
}
|
||||
},
|
||||
prepareRequest(input) {
|
||||
return buildOpenAiRequest(input);
|
||||
},
|
||||
supportsHealthCheck: true,
|
||||
toResult(payload, endpoint) {
|
||||
return normalizeProviderResponse(payload, endpoint, "cliproxy");
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ProviderName } from "../types.js";
|
||||
import { cliproxyProvider } from "./cliproxy.js";
|
||||
import { llamaProvider } from "./llama.js";
|
||||
import { openrouterProvider } from "./openrouter.js";
|
||||
import type { ProviderAdapter } from "./types.js";
|
||||
|
||||
const PROVIDERS: Record<ProviderName, ProviderAdapter> = {
|
||||
cliproxy: cliproxyProvider,
|
||||
llama: llamaProvider,
|
||||
openrouter: openrouterProvider,
|
||||
};
|
||||
|
||||
export function getProviderAdapter(name: ProviderName): ProviderAdapter {
|
||||
return PROVIDERS[name];
|
||||
}
|
||||
|
||||
export { cliproxyProvider, llamaProvider, openrouterProvider };
|
||||
export type { ProviderAdapter, ProviderRequestInput } from "./types.js";
|
||||
@@ -0,0 +1,32 @@
|
||||
import { buildHermesRequest, normalizeProviderResponse } from "./shared.js";
|
||||
import type { ProviderAdapter } from "./types.js";
|
||||
|
||||
export const llamaProvider: ProviderAdapter = {
|
||||
name: "llama",
|
||||
capabilities: {
|
||||
jsonMode: false,
|
||||
promptCache: false,
|
||||
toolCalls: "hermes",
|
||||
},
|
||||
createHealthUrl(endpoint: string): string {
|
||||
try {
|
||||
const url = new URL(endpoint);
|
||||
url.pathname = "/health";
|
||||
url.search = "";
|
||||
url.hash = "";
|
||||
return url.toString();
|
||||
} catch {
|
||||
if (endpoint.includes("/v1/chat/completions")) {
|
||||
return `${endpoint.split("/v1/chat/completions")[0]}/health`;
|
||||
}
|
||||
return `${endpoint.replace(/\/+$/u, "")}/health`;
|
||||
}
|
||||
},
|
||||
prepareRequest(input) {
|
||||
return buildHermesRequest(input);
|
||||
},
|
||||
supportsHealthCheck: true,
|
||||
toResult(payload, endpoint) {
|
||||
return normalizeProviderResponse(payload, endpoint, "llama");
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
import { buildOpenAiRequest, normalizeProviderResponse } from "./shared.js";
|
||||
import type { ProviderAdapter } from "./types.js";
|
||||
|
||||
export const openrouterProvider: ProviderAdapter = {
|
||||
name: "openrouter",
|
||||
capabilities: {
|
||||
jsonMode: true,
|
||||
promptCache: false,
|
||||
toolCalls: "openai",
|
||||
},
|
||||
createHealthUrl(endpoint: string): string {
|
||||
return endpoint;
|
||||
},
|
||||
prepareRequest(input) {
|
||||
return buildOpenAiRequest(input);
|
||||
},
|
||||
supportsHealthCheck: false,
|
||||
toResult(payload, endpoint) {
|
||||
return normalizeProviderResponse(payload, endpoint, "openrouter");
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,235 @@
|
||||
import { serializeToolsForHermes, serializeToolsForOpenAi } from "@tools/serializer";
|
||||
import type { Tool } from "@tools/tool";
|
||||
import type {
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionResponse,
|
||||
ChatCompletionResult,
|
||||
ChatMessage,
|
||||
ChatToolCall,
|
||||
} from "../types.js";
|
||||
import type { ProviderRequestInput } from "./types.js";
|
||||
|
||||
function parseJsonSafely(value: string): unknown {
|
||||
try {
|
||||
return JSON.parse(value) as unknown;
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
function serializeHermesToolCalls(toolCalls: readonly ChatToolCall[]): string {
|
||||
const payload = toolCalls.map((toolCall) => ({
|
||||
id: toolCall.id,
|
||||
name: toolCall.function.name,
|
||||
arguments: parseJsonSafely(toolCall.function.arguments),
|
||||
}));
|
||||
|
||||
return `<tool_call>${JSON.stringify(payload)}</tool_call>`;
|
||||
}
|
||||
|
||||
function serializeHermesToolResult(message: ChatMessage): string {
|
||||
return `<tool_result>${JSON.stringify({
|
||||
name: message.name ?? message.tool_call_id ?? "tool",
|
||||
result: parseJsonSafely(message.content),
|
||||
})}</tool_result>`;
|
||||
}
|
||||
|
||||
function injectHermesToolBlock(
|
||||
messages: ChatMessage[],
|
||||
tools: readonly Tool<unknown, unknown>[] | undefined,
|
||||
): ChatMessage[] {
|
||||
if (!tools || tools.length === 0) {
|
||||
return [...messages];
|
||||
}
|
||||
|
||||
const toolBlock = serializeToolsForHermes(tools);
|
||||
const cloned = messages.map((message) => ({ ...message }));
|
||||
const firstSystemIndex = cloned.findIndex((message) => message.role === "system");
|
||||
|
||||
if (firstSystemIndex === -1) {
|
||||
cloned.unshift({
|
||||
role: "system",
|
||||
content: toolBlock,
|
||||
});
|
||||
return cloned;
|
||||
}
|
||||
|
||||
const current = cloned[firstSystemIndex];
|
||||
if (!current) {
|
||||
return cloned;
|
||||
}
|
||||
|
||||
if (current.content.includes("<tools>")) {
|
||||
return cloned;
|
||||
}
|
||||
|
||||
cloned[firstSystemIndex] = {
|
||||
...current,
|
||||
content: `${current.content}\n\n${toolBlock}`,
|
||||
};
|
||||
return cloned;
|
||||
}
|
||||
|
||||
function injectJsonModeReminder(messages: ChatMessage[]): ChatMessage[] {
|
||||
const cloned = messages.map((message) => ({ ...message }));
|
||||
const firstSystemIndex = cloned.findIndex((message) => message.role === "system");
|
||||
const reminder = "반드시 JSON object 하나만 출력하세요. 설명 문장은 쓰지 마세요.";
|
||||
|
||||
if (firstSystemIndex === -1) {
|
||||
cloned.unshift({
|
||||
role: "system",
|
||||
content: reminder,
|
||||
});
|
||||
return cloned;
|
||||
}
|
||||
|
||||
const current = cloned[firstSystemIndex];
|
||||
if (!current || current.content.includes(reminder)) {
|
||||
return cloned;
|
||||
}
|
||||
|
||||
cloned[firstSystemIndex] = {
|
||||
...current,
|
||||
content: `${current.content}\n\n${reminder}`,
|
||||
};
|
||||
return cloned;
|
||||
}
|
||||
|
||||
export function normalizeOpenAiMessages(messages: ChatMessage[]): ChatMessage[] {
|
||||
return messages.map((message) => {
|
||||
if (message.role === "tool") {
|
||||
return {
|
||||
role: "tool",
|
||||
content: message.content,
|
||||
tool_call_id: message.tool_call_id,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
tool_calls: message.tool_calls,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeHermesMessages(messages: ChatMessage[]): ChatMessage[] {
|
||||
return messages.map((message) => {
|
||||
if (message.role === "tool") {
|
||||
return {
|
||||
role: "user",
|
||||
content: serializeHermesToolResult(message),
|
||||
};
|
||||
}
|
||||
|
||||
if (message.tool_calls && message.tool_calls.length > 0) {
|
||||
const parts = [
|
||||
message.content.trim(),
|
||||
serializeHermesToolCalls(message.tool_calls),
|
||||
].filter(Boolean);
|
||||
return {
|
||||
role: message.role,
|
||||
content: parts.join("\n\n"),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function buildOpenAiRequest(
|
||||
input: ProviderRequestInput,
|
||||
): ChatCompletionRequest {
|
||||
const request: ChatCompletionRequest = {
|
||||
model: input.model,
|
||||
messages: normalizeOpenAiMessages(input.messages),
|
||||
max_tokens: input.maxTokens,
|
||||
temperature: input.temperature,
|
||||
};
|
||||
|
||||
if (input.jsonMode) {
|
||||
request.response_format = { type: "json_object" };
|
||||
}
|
||||
|
||||
if (input.tools && input.tools.length > 0) {
|
||||
request.tools = serializeToolsForOpenAi(input.tools);
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
export function buildHermesRequest(
|
||||
input: ProviderRequestInput,
|
||||
): ChatCompletionRequest {
|
||||
let messages = injectHermesToolBlock(input.messages, input.tools);
|
||||
messages = normalizeHermesMessages(messages);
|
||||
|
||||
if (input.jsonMode) {
|
||||
messages = injectJsonModeReminder(messages);
|
||||
}
|
||||
|
||||
return {
|
||||
model: input.model,
|
||||
messages,
|
||||
max_tokens: input.maxTokens,
|
||||
temperature: input.temperature,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeToolCalls(
|
||||
toolCalls: ChatToolCall[] | null | undefined,
|
||||
): ChatToolCall[] | undefined {
|
||||
if (!toolCalls || toolCalls.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const normalized = toolCalls.flatMap((toolCall) => {
|
||||
const name = toolCall.function?.name?.trim();
|
||||
const args = toolCall.function?.arguments;
|
||||
if (!name || typeof args !== "string") {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
id: toolCall.id,
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name,
|
||||
arguments: args,
|
||||
},
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
return normalized.length > 0 ? normalized : undefined;
|
||||
}
|
||||
|
||||
export function normalizeProviderResponse(
|
||||
payload: ChatCompletionResponse,
|
||||
endpoint: string,
|
||||
provider: ChatCompletionResult["provider"],
|
||||
): ChatCompletionResult | null {
|
||||
const firstMessage = payload.choices?.[0]?.message;
|
||||
const content = firstMessage?.content?.trim() ?? "";
|
||||
const toolCalls = normalizeToolCalls(firstMessage?.tool_calls);
|
||||
|
||||
if (!content && (!toolCalls || toolCalls.length === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
content,
|
||||
endpoint,
|
||||
message: {
|
||||
role: firstMessage?.role ?? "assistant",
|
||||
content,
|
||||
tool_calls: toolCalls,
|
||||
},
|
||||
provider,
|
||||
raw: payload,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { Tool } from "@tools/tool";
|
||||
import type {
|
||||
ChatCompletionRequest,
|
||||
ChatCompletionResponse,
|
||||
ChatCompletionResult,
|
||||
ChatMessage,
|
||||
ProviderName,
|
||||
} from "../types.js";
|
||||
|
||||
export interface ProviderRequestInput {
|
||||
jsonMode?: boolean | undefined;
|
||||
maxTokens: number;
|
||||
messages: ChatMessage[];
|
||||
model: string;
|
||||
temperature: number;
|
||||
tools?: readonly Tool<unknown, unknown>[] | undefined;
|
||||
}
|
||||
|
||||
export interface ProviderAdapter {
|
||||
capabilities: {
|
||||
jsonMode: boolean;
|
||||
promptCache: false;
|
||||
toolCalls: "openai" | "hermes" | "none";
|
||||
};
|
||||
createHealthUrl(endpoint: string): string;
|
||||
name: ProviderName;
|
||||
prepareRequest(input: ProviderRequestInput): ChatCompletionRequest;
|
||||
supportsHealthCheck: boolean;
|
||||
toResult(
|
||||
payload: ChatCompletionResponse,
|
||||
endpoint: string,
|
||||
): ChatCompletionResult | null;
|
||||
}
|
||||
Reference in new issue
Block a user