codex-claude-proxy 1.0.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.
@@ -0,0 +1,48 @@
1
+ import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
2
+ import { join } from 'path';
3
+ import { CONFIG_DIR } from './account-manager.js';
4
+
5
+ const SETTINGS_FILE = join(CONFIG_DIR, 'settings.json');
6
+
7
+ const DEFAULT_SETTINGS = {
8
+ haikuKiloModel: 'glm-5'
9
+ };
10
+
11
+ function ensureConfigDir() {
12
+ if (!existsSync(CONFIG_DIR)) {
13
+ mkdirSync(CONFIG_DIR, { recursive: true });
14
+ }
15
+ }
16
+
17
+ export function getServerSettings() {
18
+ ensureConfigDir();
19
+
20
+ if (!existsSync(SETTINGS_FILE)) {
21
+ return { ...DEFAULT_SETTINGS };
22
+ }
23
+
24
+ try {
25
+ const data = JSON.parse(readFileSync(SETTINGS_FILE, 'utf8'));
26
+ return { ...DEFAULT_SETTINGS, ...data };
27
+ } catch (error) {
28
+ console.error('[ServerSettings] Failed to read settings:', error.message);
29
+ return { ...DEFAULT_SETTINGS };
30
+ }
31
+ }
32
+
33
+ export function setServerSettings(patch = {}) {
34
+ const current = getServerSettings();
35
+ const next = { ...current, ...patch };
36
+
37
+ ensureConfigDir();
38
+ writeFileSync(SETTINGS_FILE, JSON.stringify(next, null, 2));
39
+ return next;
40
+ }
41
+
42
+ export { SETTINGS_FILE };
43
+
44
+ export default {
45
+ getServerSettings,
46
+ setServerSettings,
47
+ SETTINGS_FILE
48
+ };
package/src/server.js ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Server bootstrap
3
+ * Creates the Express app, middleware, and registers API routes.
4
+ */
5
+
6
+ import express from 'express';
7
+ import cors from 'cors';
8
+
9
+ import { ensureAccountsPersist, startAutoRefresh } from './account-manager.js';
10
+ import { registerApiRoutes } from './routes/api-routes.js';
11
+
12
+ export function createServer({ port }) {
13
+ ensureAccountsPersist();
14
+ startAutoRefresh();
15
+
16
+ const app = express();
17
+ app.use(cors());
18
+ app.use(express.json({ limit: '10mb' }));
19
+
20
+ registerApiRoutes(app, { port });
21
+
22
+ return app;
23
+ }
24
+
25
+ export function startServer({ port }) {
26
+ const app = createServer({ port });
27
+ return app.listen(port);
28
+ }
29
+
30
+ export default { createServer, startServer };
@@ -0,0 +1,156 @@
1
+ import EventEmitter from 'events';
2
+
3
+ const COLORS = {
4
+ reset: '\x1b[0m',
5
+ blue: '\x1b[34m',
6
+ green: '\x1b[32m',
7
+ yellow: '\x1b[33m',
8
+ red: '\x1b[31m',
9
+ magenta: '\x1b[35m',
10
+ cyan: '\x1b[36m',
11
+ gray: '\x1b[90m',
12
+ dim: '\x1b[2m'
13
+ };
14
+
15
+ const LEVEL_ICONS = {
16
+ INFO: 'ℹ',
17
+ SUCCESS: '✓',
18
+ WARN: '⚠',
19
+ ERROR: '✗',
20
+ DEBUG: '•'
21
+ };
22
+
23
+ class Logger extends EventEmitter {
24
+ constructor() {
25
+ super();
26
+ this.debugEnabled = false;
27
+ this.history = [];
28
+ this.maxHistory = 1000;
29
+ }
30
+
31
+ setDebug(enabled) {
32
+ this.debugEnabled = enabled;
33
+ }
34
+
35
+ get isDebugEnabled() {
36
+ return this.debugEnabled;
37
+ }
38
+
39
+ getHistory() {
40
+ return [...this.history];
41
+ }
42
+
43
+ clear() {
44
+ this.history = [];
45
+ }
46
+
47
+ _formatTimestamp() {
48
+ return new Date().toISOString().replace('T', ' ').slice(0, 19);
49
+ }
50
+
51
+ _formatMessage(args) {
52
+ return args.map(arg => {
53
+ if (arg === null) return 'null';
54
+ if (arg === undefined) return 'undefined';
55
+ if (typeof arg === 'object') {
56
+ try {
57
+ return JSON.stringify(arg, null, 2);
58
+ } catch (e) {
59
+ return String(arg);
60
+ }
61
+ }
62
+ return String(arg);
63
+ }).join(' ');
64
+ }
65
+
66
+ _log(level, ...args) {
67
+ const timestamp = this._formatTimestamp();
68
+ const icon = LEVEL_ICONS[level] || '•';
69
+ const color = {
70
+ INFO: COLORS.blue,
71
+ SUCCESS: COLORS.green,
72
+ WARN: COLORS.yellow,
73
+ ERROR: COLORS.red,
74
+ DEBUG: COLORS.magenta
75
+ }[level] || COLORS.reset;
76
+
77
+ const message = this._formatMessage(args);
78
+
79
+ if (level === 'DEBUG' && !this.debugEnabled) {
80
+ return;
81
+ }
82
+
83
+ const logEntry = {
84
+ timestamp,
85
+ level,
86
+ message,
87
+ icon
88
+ };
89
+
90
+ this.history.push(logEntry);
91
+ if (this.history.length > this.maxHistory) {
92
+ this.history.shift();
93
+ }
94
+
95
+ this.emit('log', logEntry);
96
+
97
+ const consoleMessage = `${COLORS.gray}${timestamp}${COLORS.reset} ${color}${icon}${COLORS.reset} ${message}`;
98
+
99
+ switch (level) {
100
+ case 'ERROR':
101
+ console.error(consoleMessage);
102
+ break;
103
+ case 'WARN':
104
+ console.warn(consoleMessage);
105
+ break;
106
+ default:
107
+ console.log(consoleMessage);
108
+ }
109
+ }
110
+
111
+ info(...args) {
112
+ this._log('INFO', ...args);
113
+ }
114
+
115
+ success(...args) {
116
+ this._log('SUCCESS', ...args);
117
+ }
118
+
119
+ warn(...args) {
120
+ this._log('WARN', ...args);
121
+ }
122
+
123
+ error(...args) {
124
+ this._log('ERROR', ...args);
125
+ }
126
+
127
+ debug(...args) {
128
+ this._log('DEBUG', ...args);
129
+ }
130
+
131
+ request(method, path, details = {}) {
132
+ const parts = [`${method}`, path];
133
+ if (details.model) parts.push(`model=${details.model}`);
134
+ if (details.account) parts.push(`account=${details.account}`);
135
+ if (details.stream !== undefined) parts.push(`stream=${details.stream}`);
136
+ if (details.messages) parts.push(`messages=${details.messages}`);
137
+ if (details.tools) parts.push(`tools=${details.tools}`);
138
+ this.info(`[Request] ${parts.join(' | ')}`);
139
+ }
140
+
141
+ response(status, details = {}) {
142
+ const parts = [`status=${status}`];
143
+ if (details.model) parts.push(`model=${details.model}`);
144
+ if (details.tokens) parts.push(`tokens=${details.tokens}`);
145
+ if (details.duration) parts.push(`${details.duration}ms`);
146
+ if (details.error) parts.push(`error=${details.error}`);
147
+
148
+ if (status >= 400) {
149
+ this.error(`[Response] ${parts.join(' | ')}`);
150
+ } else {
151
+ this.success(`[Response] ${parts.join(' | ')}`);
152
+ }
153
+ }
154
+ }
155
+
156
+ export const logger = new Logger();