mirror of
https://github.com/musistudio/claude-code-router.git
synced 2026-06-16 21:03:54 +08:00
Merge branch 'main' into feature/add-model-selector-command
This commit is contained in:
+2
-2
@@ -158,7 +158,7 @@ async function run(options: RunOptions = {}) {
|
||||
});
|
||||
});
|
||||
server.addHook("preHandler", async (req, reply) => {
|
||||
if (req.url.startsWith("/v1/messages")) {
|
||||
if (req.url.startsWith("/v1/messages") && !req.url.startsWith("/v1/messages/count_tokens")) {
|
||||
const useAgents = []
|
||||
|
||||
for (const agent of agentsManager.getAllAgents()) {
|
||||
@@ -198,7 +198,7 @@ async function run(options: RunOptions = {}) {
|
||||
event.emit('onError', request, reply, error);
|
||||
})
|
||||
server.addHook("onSend", (req, reply, payload, done) => {
|
||||
if (req.sessionId && req.url.startsWith("/v1/messages")) {
|
||||
if (req.sessionId && req.url.startsWith("/v1/messages") && !req.url.startsWith("/v1/messages/count_tokens")) {
|
||||
if (payload instanceof ReadableStream) {
|
||||
if (req.agents) {
|
||||
const abortController = new AbortController();
|
||||
|
||||
@@ -5,10 +5,17 @@ import { join } from "path";
|
||||
import fastifyStatic from "@fastify/static";
|
||||
import { readdirSync, statSync, readFileSync, writeFileSync, existsSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import {calculateTokenCount} from "./utils/router";
|
||||
|
||||
export const createServer = (config: any): Server => {
|
||||
const server = new Server(config);
|
||||
|
||||
server.app.post("/v1/messages/count_tokens", async (req, reply) => {
|
||||
const {messages, tools, system} = req.body;
|
||||
const tokenCount = calculateTokenCount(messages, system, tools);
|
||||
return { "input_tokens": tokenCount }
|
||||
});
|
||||
|
||||
// Add endpoint to read config.json with access control
|
||||
server.app.get("/api/config", async (req, reply) => {
|
||||
return await readConfigFile();
|
||||
|
||||
@@ -63,7 +63,13 @@ export async function executeCodeCommand(args: string[] = []) {
|
||||
const argsArr = []
|
||||
for (const [argsObjKey, argsObjValue] of Object.entries(argsObj)) {
|
||||
if (argsObjKey !== '_' && argsObj[argsObjKey]) {
|
||||
argsArr.push(`${argsObjKey.length === 1 ? '-' : '--'}${argsObjKey} ${JSON.stringify(argsObjValue)}`);
|
||||
const prefix = argsObjKey.length === 1 ? '-' : '--';
|
||||
// For boolean flags, don't append the value
|
||||
if (argsObjValue === true) {
|
||||
argsArr.push(`${prefix}${argsObjKey}`);
|
||||
} else {
|
||||
argsArr.push(`${prefix}${argsObjKey} ${JSON.stringify(argsObjValue)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
const claudeProcess = spawn(
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import { readFile } from 'fs/promises'
|
||||
|
||||
const enc = get_encoding("cl100k_base");
|
||||
|
||||
const calculateTokenCount = (
|
||||
export const calculateTokenCount = (
|
||||
messages: MessageParam[],
|
||||
system: any,
|
||||
tools: Tool[]
|
||||
|
||||
Reference in New Issue
Block a user