@cuijy/free-api 0.1.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.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +299 -0
  3. package/bin/freeapi +8 -0
  4. package/dist/cli/commands/chat.d.ts +3 -0
  5. package/dist/cli/commands/chat.d.ts.map +1 -0
  6. package/dist/cli/commands/chat.js +39 -0
  7. package/dist/cli/commands/chat.js.map +1 -0
  8. package/dist/cli/commands/config.d.ts +3 -0
  9. package/dist/cli/commands/config.d.ts.map +1 -0
  10. package/dist/cli/commands/config.js +289 -0
  11. package/dist/cli/commands/config.js.map +1 -0
  12. package/dist/cli/commands/init.d.ts +3 -0
  13. package/dist/cli/commands/init.d.ts.map +1 -0
  14. package/dist/cli/commands/init.js +114 -0
  15. package/dist/cli/commands/init.js.map +1 -0
  16. package/dist/cli/commands/list.d.ts +3 -0
  17. package/dist/cli/commands/list.d.ts.map +1 -0
  18. package/dist/cli/commands/list.js +109 -0
  19. package/dist/cli/commands/list.js.map +1 -0
  20. package/dist/cli/commands/logs.d.ts +3 -0
  21. package/dist/cli/commands/logs.d.ts.map +1 -0
  22. package/dist/cli/commands/logs.js +51 -0
  23. package/dist/cli/commands/logs.js.map +1 -0
  24. package/dist/cli/commands/start.d.ts +3 -0
  25. package/dist/cli/commands/start.d.ts.map +1 -0
  26. package/dist/cli/commands/start.js +34 -0
  27. package/dist/cli/commands/start.js.map +1 -0
  28. package/dist/cli/commands/status.d.ts +3 -0
  29. package/dist/cli/commands/status.d.ts.map +1 -0
  30. package/dist/cli/commands/status.js +45 -0
  31. package/dist/cli/commands/status.js.map +1 -0
  32. package/dist/cli/commands/stop.d.ts +3 -0
  33. package/dist/cli/commands/stop.d.ts.map +1 -0
  34. package/dist/cli/commands/stop.js +54 -0
  35. package/dist/cli/commands/stop.js.map +1 -0
  36. package/dist/cli/index.d.ts +3 -0
  37. package/dist/cli/index.d.ts.map +1 -0
  38. package/dist/cli/index.js +79 -0
  39. package/dist/cli/index.js.map +1 -0
  40. package/dist/index.d.ts +7 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +17 -0
  43. package/dist/index.js.map +1 -0
  44. package/dist/services/base/index.d.ts +112 -0
  45. package/dist/services/base/index.d.ts.map +1 -0
  46. package/dist/services/base/index.js +51 -0
  47. package/dist/services/base/index.js.map +1 -0
  48. package/dist/services/base/types.d.ts +114 -0
  49. package/dist/services/base/types.d.ts.map +1 -0
  50. package/dist/services/base/types.js +7 -0
  51. package/dist/services/base/types.js.map +1 -0
  52. package/dist/services/chatgpt/api-client.d.ts +81 -0
  53. package/dist/services/chatgpt/api-client.d.ts.map +1 -0
  54. package/dist/services/chatgpt/api-client.js +325 -0
  55. package/dist/services/chatgpt/api-client.js.map +1 -0
  56. package/dist/services/chatgpt/cli-adapter.d.ts +108 -0
  57. package/dist/services/chatgpt/cli-adapter.d.ts.map +1 -0
  58. package/dist/services/chatgpt/cli-adapter.js +315 -0
  59. package/dist/services/chatgpt/cli-adapter.js.map +1 -0
  60. package/dist/services/chatgpt/config-manager.d.ts +87 -0
  61. package/dist/services/chatgpt/config-manager.d.ts.map +1 -0
  62. package/dist/services/chatgpt/config-manager.js +344 -0
  63. package/dist/services/chatgpt/config-manager.js.map +1 -0
  64. package/dist/services/chatgpt/config-wizard.d.ts +35 -0
  65. package/dist/services/chatgpt/config-wizard.d.ts.map +1 -0
  66. package/dist/services/chatgpt/config-wizard.js +524 -0
  67. package/dist/services/chatgpt/config-wizard.js.map +1 -0
  68. package/dist/services/chatgpt/error-handler.d.ts +94 -0
  69. package/dist/services/chatgpt/error-handler.d.ts.map +1 -0
  70. package/dist/services/chatgpt/error-handler.js +409 -0
  71. package/dist/services/chatgpt/error-handler.js.map +1 -0
  72. package/dist/services/chatgpt/exports.d.ts +71 -0
  73. package/dist/services/chatgpt/exports.d.ts.map +1 -0
  74. package/dist/services/chatgpt/exports.js +81 -0
  75. package/dist/services/chatgpt/exports.js.map +1 -0
  76. package/dist/services/chatgpt/index.d.ts +125 -0
  77. package/dist/services/chatgpt/index.d.ts.map +1 -0
  78. package/dist/services/chatgpt/index.js +394 -0
  79. package/dist/services/chatgpt/index.js.map +1 -0
  80. package/dist/services/chatgpt/service-adapter.d.ts +152 -0
  81. package/dist/services/chatgpt/service-adapter.d.ts.map +1 -0
  82. package/dist/services/chatgpt/service-adapter.js +430 -0
  83. package/dist/services/chatgpt/service-adapter.js.map +1 -0
  84. package/dist/services/chatgpt/session-manager.d.ts +113 -0
  85. package/dist/services/chatgpt/session-manager.d.ts.map +1 -0
  86. package/dist/services/chatgpt/session-manager.js +396 -0
  87. package/dist/services/chatgpt/session-manager.js.map +1 -0
  88. package/dist/services/chatgpt/types.d.ts +130 -0
  89. package/dist/services/chatgpt/types.d.ts.map +1 -0
  90. package/dist/services/chatgpt/types.js +19 -0
  91. package/dist/services/chatgpt/types.js.map +1 -0
  92. package/dist/utils/config.d.ts +61 -0
  93. package/dist/utils/config.d.ts.map +1 -0
  94. package/dist/utils/config.js +215 -0
  95. package/dist/utils/config.js.map +1 -0
  96. package/package.json +91 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 naturecode-official
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 ADDED
@@ -0,0 +1,299 @@
1
+ # FreeAPI 🤖
2
+
3
+ [![npm version](https://img.shields.io/npm/v/freeapi.svg)](https://www.npmjs.com/package/freeapi)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![Node.js Version](https://img.shields.io/badge/node-%3E%3D18.0.0-brightgreen.svg)](https://nodejs.org/)
6
+
7
+ > A powerful CLI tool that runs multiple AI web-based free chat services in the background. Start with ChatGPT as your first AI assistant!
8
+
9
+ ## ✨ Features
10
+
11
+ - **🚀 Multiple AI Services**: Support for ChatGPT, DeepSeek, Wenxin, Qwen and more
12
+ - **🔧 Easy Configuration**: Interactive setup wizard for each service
13
+ - **💬 Interactive Chat**: Command-line chat interface with conversation history
14
+ - **⚡ Real API Integration**: Connect to real AI APIs (OpenAI, etc.)
15
+ - **🔐 Dual Authentication Modes**: Public (no login) and Authenticated (full access)
16
+ - **📊 Usage Statistics**: Track tokens, requests, and rate limits
17
+ - **🛡️ Error Recovery**: Automatic retry and rate limiting
18
+ - **🌐 Global CLI**: Install once, use anywhere
19
+
20
+ ## 🚀 Quick Start
21
+
22
+ ### Installation
23
+
24
+ ```bash
25
+ # Install globally from npm
26
+ npm install -g freeapi
27
+
28
+ # Or from source
29
+ git clone https://github.com/naturecode-official/freeapi.git
30
+ cd freeapi
31
+ npm install
32
+ npm run build
33
+ npm link
34
+ ```
35
+
36
+ ### Get Started in 30 Seconds
37
+
38
+ ```bash
39
+ # 1. Initialize FreeAPI
40
+ freeapi init
41
+
42
+ # 2. Configure ChatGPT (interactive wizard)
43
+ freeapi config chatgpt
44
+
45
+ # 3. Start chatting!
46
+ freeapi chat chatgpt
47
+ ```
48
+
49
+ ## 📖 Documentation
50
+
51
+ ### Basic Commands
52
+
53
+ ```bash
54
+ # Initialize configuration
55
+ freeapi init
56
+
57
+ # List available services
58
+ freeapi list
59
+
60
+ # Configure a specific service
61
+ freeapi config chatgpt
62
+ freeapi config deepseek
63
+
64
+ # Start interactive chat
65
+ freeapi chat chatgpt
66
+
67
+ # Check service status
68
+ freeapi status
69
+
70
+ # View logs
71
+ freeapi logs
72
+
73
+ # Stop services
74
+ freeapi stop
75
+ ```
76
+
77
+ ### ChatGPT Service
78
+
79
+ FreeAPI comes with a fully-featured ChatGPT service:
80
+
81
+ #### Features:
82
+ - **Public Mode**: Use without API key (limited functionality)
83
+ - **Authenticated Mode**: Full access with OpenAI API key
84
+ - **Multiple Models**: GPT-3.5 Turbo, GPT-4, GPT-4 Turbo
85
+ - **Conversation History**: Save and continue conversations
86
+ - **Rate Limiting**: Built-in protection against API limits
87
+
88
+ #### Configuration:
89
+ ```bash
90
+ # Run the interactive configuration wizard
91
+ freeapi config chatgpt
92
+
93
+ # Or configure manually
94
+ node -e "require('freeapi/dist/services/chatgpt/exports').runConfigWizard()"
95
+ ```
96
+
97
+ #### Example Usage:
98
+ ```javascript
99
+ // Programmatic usage
100
+ const { createChatGPTService } = require('freeapi/dist/services/chatgpt/exports');
101
+
102
+ async function chat() {
103
+ const chatGPT = createChatGPTService();
104
+ await chatGPT.initialize();
105
+
106
+ const response = await chatGPT.sendMessage('Hello, how are you?', {
107
+ maxTokens: 100,
108
+ temperature: 0.7,
109
+ systemPrompt: 'You are a helpful assistant.'
110
+ });
111
+
112
+ console.log(response.message.content);
113
+ chatGPT.destroy();
114
+ }
115
+
116
+ chat().catch(console.error);
117
+ ```
118
+
119
+ ## 🛠️ Development
120
+
121
+ ### Project Structure
122
+ ```
123
+ freeapi/
124
+ ├── bin/freeapi # CLI entry point
125
+ ├── src/
126
+ │ ├── index.ts # Main entry point
127
+ │ ├── cli/ # CLI command implementations
128
+ │ │ ├── commands/ # Individual CLI commands
129
+ │ │ └── index.ts # CLI setup
130
+ │ ├── services/ # AI service implementations
131
+ │ │ └── chatgpt/ # ChatGPT service (first implementation)
132
+ │ │ ├── api-client.ts # HTTP client with auth
133
+ │ │ ├── session-manager.ts # User session management
134
+ │ │ ├── config-wizard.ts # Interactive setup
135
+ │ │ └── exports.ts # Public API
136
+ │ └── utils/ # Utility functions
137
+ ├── dist/ # Compiled JavaScript
138
+ ├── tests/ # Test suite
139
+ └── docs/ # Documentation
140
+ ```
141
+
142
+ ### Adding New Services
143
+
144
+ 1. Create a new service directory in `src/services/`
145
+ 2. Implement the `BaseServiceAdapter` interface
146
+ 3. Add configuration wizard
147
+ 4. Register with CLI commands
148
+ 5. Add tests
149
+
150
+ Example service structure:
151
+ ```typescript
152
+ // src/services/myservice/index.ts
153
+ export class MyService extends BaseServiceAdapter {
154
+ async sendMessage(request: ChatRequest): Promise<ChatResponse> {
155
+ // Implementation
156
+ }
157
+
158
+ async initialize(): Promise<void> {
159
+ // Initialization
160
+ }
161
+
162
+ // ... other required methods
163
+ }
164
+ ```
165
+
166
+ ### Building from Source
167
+
168
+ ```bash
169
+ # Clone the repository
170
+ git clone https://github.com/naturecode-official/freeapi.git
171
+ cd freeapi
172
+
173
+ # Install dependencies
174
+ npm install
175
+
176
+ # Build TypeScript
177
+ npm run build
178
+
179
+ # Run tests
180
+ npm test
181
+
182
+ # Link globally for development
183
+ npm link
184
+ ```
185
+
186
+ ## 📦 Installation Methods
187
+
188
+ ### Method 1: npm (Recommended)
189
+ ```bash
190
+ npm install -g freeapi
191
+ ```
192
+
193
+ ### Method 2: From Source
194
+ ```bash
195
+ git clone https://github.com/naturecode-official/freeapi.git
196
+ cd freeapi
197
+ ./install-global.sh
198
+ ```
199
+
200
+ ### Method 3: Docker (Coming Soon)
201
+ ```bash
202
+ docker pull naturecode/freeapi
203
+ docker run -it naturecode/freeapi freeapi --help
204
+ ```
205
+
206
+ ## 🔧 Configuration
207
+
208
+ ### Configuration Directory
209
+ FreeAPI stores configuration in `~/.freeapi/`:
210
+ ```
211
+ ~/.freeapi/
212
+ ├── config.json # Global configuration
213
+ ├── services/ # Service configurations
214
+ │ ├── chatgpt.json # ChatGPT configuration
215
+ │ └── deepseek.json # DeepSeek configuration
216
+ └── logs/ # Application logs
217
+ ```
218
+
219
+ ### Environment Variables
220
+ ```bash
221
+ # Set OpenAI API key
222
+ export OPENAI_API_KEY=sk-your-key-here
223
+
224
+ # Enable debug logging
225
+ export DEBUG=freeapi:*
226
+
227
+ # Custom configuration directory
228
+ export FREEAPI_CONFIG_DIR=/path/to/config
229
+ ```
230
+
231
+ ## 🤝 Contributing
232
+
233
+ We welcome contributions! Here's how to get started:
234
+
235
+ 1. **Fork** the repository
236
+ 2. **Create a feature branch**: `git checkout -b feature/amazing-feature`
237
+ 3. **Make your changes**
238
+ 4. **Run tests**: `npm test`
239
+ 5. **Commit your changes**: `git commit -m 'Add amazing feature'`
240
+ 6. **Push to the branch**: `git push origin feature/amazing-feature`
241
+ 7. **Open a Pull Request**
242
+
243
+ ### Development Setup
244
+ ```bash
245
+ # 1. Fork and clone
246
+ git clone https://github.com/YOUR_USERNAME/freeapi.git
247
+ cd freeapi
248
+
249
+ # 2. Install dependencies
250
+ npm install
251
+
252
+ # 3. Create development branch
253
+ git checkout -b dev/feature-name
254
+
255
+ # 4. Make changes and test
256
+ npm run build
257
+ npm test
258
+
259
+ # 5. Link for testing
260
+ npm link
261
+ freeapi --help # Test your changes
262
+ ```
263
+
264
+ ## 📄 License
265
+
266
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
267
+
268
+ ## 🙏 Acknowledgments
269
+
270
+ - OpenAI for the ChatGPT API
271
+ - All contributors and testers
272
+ - The open-source community
273
+
274
+ ## 📞 Support
275
+
276
+ - **GitHub Issues**: [Report bugs or request features](https://github.com/naturecode-official/freeapi/issues)
277
+ - **Documentation**: See `docs/` directory for detailed guides
278
+ - **Quick Help**: Run `freeapi --help` for command reference
279
+
280
+ ## 🚀 Roadmap
281
+
282
+ - [x] ChatGPT service implementation
283
+ - [ ] DeepSeek service integration
284
+ - [ ] Wenxin AI support
285
+ - [ ] Qwen AI integration
286
+ - [ ] Docker containerization
287
+ - [ ] Web dashboard
288
+ - [ ] Plugin system for custom services
289
+ - [ ] Mobile app (React Native)
290
+
291
+ ---
292
+
293
+ <div align="center">
294
+
295
+ **Made with ❤️ by NatureCode**
296
+
297
+ [GitHub](https://github.com/naturecode-official) • [npm](https://www.npmjs.com/package/freeapi) • [Report Bug](https://github.com/naturecode-official/freeapi/issues)
298
+
299
+ </div>
package/bin/freeapi ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { main } = require('../dist/cli/index.js');
4
+
5
+ main().catch((error) => {
6
+ console.error('Error:', error.message);
7
+ process.exit(1);
8
+ });
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ export declare function chatCommand(): Command;
3
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC,wBAAgB,WAAW,IAAI,OAAO,CAiCrC"}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.chatCommand = chatCommand;
7
+ const commander_1 = require("commander");
8
+ const chalk_1 = __importDefault(require("chalk"));
9
+ const ora_1 = __importDefault(require("ora"));
10
+ function chatCommand() {
11
+ const command = new commander_1.Command('chat')
12
+ .description('Interactive chat with a service')
13
+ .argument('<service>', 'Service name to chat with')
14
+ .action(async (serviceName) => {
15
+ const spinner = (0, ora_1.default)(`Connecting to ${serviceName}...`).start();
16
+ try {
17
+ // TODO: Implement interactive chat logic
18
+ await new Promise(resolve => setTimeout(resolve, 1000));
19
+ spinner.stop();
20
+ console.log(chalk_1.default.blue.bold(`Chat with ${serviceName}`));
21
+ console.log(chalk_1.default.gray('Type "exit" or "quit" to end the chat.'));
22
+ console.log(chalk_1.default.gray('Type "clear" to clear the conversation.'));
23
+ console.log();
24
+ // TODO: Implement actual chat interface
25
+ console.log(chalk_1.default.yellow('Chat interface will be implemented in Phase 2.'));
26
+ console.log();
27
+ console.log(chalk_1.default.gray('For now, you can configure services using:'));
28
+ console.log(` ${chalk_1.default.cyan('freeapi config chatgpt_web')}`);
29
+ console.log(` ${chalk_1.default.cyan('freeapi config deepseek')}`);
30
+ }
31
+ catch (error) {
32
+ spinner.fail(chalk_1.default.red(`Failed to connect to ${serviceName}:`));
33
+ console.error(chalk_1.default.red(error instanceof Error ? error.message : String(error)));
34
+ process.exit(1);
35
+ }
36
+ });
37
+ return command;
38
+ }
39
+ //# sourceMappingURL=chat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.js","sourceRoot":"","sources":["../../../src/cli/commands/chat.ts"],"names":[],"mappings":";;;;;AAIA,kCAiCC;AArCD,yCAAoC;AACpC,kDAA0B;AAC1B,8CAAsB;AAEtB,SAAgB,WAAW;IACzB,MAAM,OAAO,GAAG,IAAI,mBAAO,CAAC,MAAM,CAAC;SAChC,WAAW,CAAC,iCAAiC,CAAC;SAC9C,QAAQ,CAAC,WAAW,EAAE,2BAA2B,CAAC;SAClD,MAAM,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC5B,MAAM,OAAO,GAAG,IAAA,aAAG,EAAC,iBAAiB,WAAW,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;QAE/D,IAAI,CAAC;YACH,yCAAyC;YACzC,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAExD,OAAO,CAAC,IAAI,EAAE,CAAC;YAEf,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,WAAW,EAAE,CAAC,CAAC,CAAC;YACzD,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,IAAI,CAAC,wCAAwC,CAAC,CAAC,CAAC;YAClE,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,IAAI,CAAC,yCAAyC,CAAC,CAAC,CAAC;YACnE,OAAO,CAAC,GAAG,EAAE,CAAC;YAEd,wCAAwC;YACxC,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,MAAM,CAAC,gDAAgD,CAAC,CAAC,CAAC;YAC5E,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAC,CAAC;YACtE,OAAO,CAAC,GAAG,CAAC,KAAK,eAAK,CAAC,IAAI,CAAC,4BAA4B,CAAC,EAAE,CAAC,CAAC;YAC7D,OAAO,CAAC,GAAG,CAAC,KAAK,eAAK,CAAC,IAAI,CAAC,yBAAyB,CAAC,EAAE,CAAC,CAAC;QAE5D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC,eAAK,CAAC,GAAG,CAAC,wBAAwB,WAAW,GAAG,CAAC,CAAC,CAAC;YAChE,OAAO,CAAC,KAAK,CAAC,eAAK,CAAC,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACjF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;IACH,CAAC,CAAC,CAAC;IAEL,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { Command } from 'commander';
2
+ export declare function configCommand(): Command;
3
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAQpC,wBAAgB,aAAa,IAAI,OAAO,CA6PvC"}
@@ -0,0 +1,289 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.configCommand = configCommand;
40
+ const commander_1 = require("commander");
41
+ const chalk_1 = __importDefault(require("chalk"));
42
+ const ora_1 = __importDefault(require("ora"));
43
+ const inquirer_1 = __importDefault(require("inquirer"));
44
+ const fs_extra_1 = __importDefault(require("fs-extra"));
45
+ const path_1 = __importDefault(require("path"));
46
+ const os_1 = __importDefault(require("os"));
47
+ function configCommand() {
48
+ const command = new commander_1.Command('config')
49
+ .description('Configure a specific service')
50
+ .argument('<service>', 'Service name to configure')
51
+ .option('-r, --reset', 'Reset to default configuration', false)
52
+ .action(async (serviceName, options) => {
53
+ const spinner = (0, ora_1.default)(`Configuring ${serviceName}...`).start();
54
+ try {
55
+ const configDir = path_1.default.join(os_1.default.homedir(), '.freeapi');
56
+ const servicesDir = path_1.default.join(configDir, 'services');
57
+ const serviceFile = path_1.default.join(servicesDir, `${serviceName}.json`);
58
+ // Check if initialized
59
+ if (!(await fs_extra_1.default.pathExists(configDir))) {
60
+ spinner.fail('FreeAPI is not initialized.');
61
+ console.log(chalk_1.default.yellow('Run "freeapi init" to initialize.'));
62
+ return;
63
+ }
64
+ // Check if service file exists
65
+ let serviceConfig = {};
66
+ if (await fs_extra_1.default.pathExists(serviceFile) && !options.reset) {
67
+ try {
68
+ serviceConfig = await fs_extra_1.default.readJson(serviceFile);
69
+ }
70
+ catch (error) {
71
+ spinner.warn('Invalid service configuration, creating new one...');
72
+ }
73
+ }
74
+ // Determine service type and configuration questions
75
+ let questions = [];
76
+ if (serviceName === 'chatgpt' || serviceName === 'chatgpt_web') {
77
+ // Use ChatGPT configuration wizard
78
+ spinner.stop();
79
+ console.log(chalk_1.default.blue.bold(`Configuring ${serviceName} Service`));
80
+ console.log(chalk_1.default.gray('Launching ChatGPT configuration wizard...'));
81
+ console.log();
82
+ try {
83
+ // Import and run ChatGPT config wizard
84
+ const { runConfigWizard } = await Promise.resolve().then(() => __importStar(require('../../services/chatgpt/exports')));
85
+ await runConfigWizard();
86
+ console.log();
87
+ console.log(chalk_1.default.green(`${serviceName} configured successfully!`));
88
+ console.log();
89
+ console.log(chalk_1.default.yellow('Next steps:'));
90
+ console.log(` ${chalk_1.default.cyan(`freeapi start ${serviceName}`)} - Start the service`);
91
+ console.log(` ${chalk_1.default.cyan(`freeapi chat ${serviceName}`)} - Start interactive chat`);
92
+ console.log(` ${chalk_1.default.cyan(`freeapi status`)} - Check service status`);
93
+ return;
94
+ }
95
+ catch (error) {
96
+ spinner.fail(chalk_1.default.red('ChatGPT configuration failed:'));
97
+ console.error(chalk_1.default.red(error instanceof Error ? error.message : String(error)));
98
+ // Fall back to manual configuration
99
+ console.log(chalk_1.default.yellow('\nFalling back to manual configuration...'));
100
+ spinner.start();
101
+ questions = [
102
+ {
103
+ type: 'confirm',
104
+ name: 'enabled',
105
+ message: 'Enable ChatGPT service?',
106
+ default: serviceConfig.enabled || false,
107
+ },
108
+ {
109
+ type: 'list',
110
+ name: 'mode',
111
+ message: 'Select authentication mode:',
112
+ choices: [
113
+ { name: 'Public Mode (No API key, limited)', value: 'public' },
114
+ { name: 'Authenticated Mode (API key required)', value: 'authenticated' }
115
+ ],
116
+ default: serviceConfig.mode || 'public',
117
+ when: (answers) => answers.enabled,
118
+ },
119
+ {
120
+ type: 'password',
121
+ name: 'api_key',
122
+ message: 'OpenAI API Key (sk-...):',
123
+ mask: '*',
124
+ when: (answers) => answers.enabled && answers.mode === 'authenticated',
125
+ validate: (input) => {
126
+ if (!input.trim()) {
127
+ return 'API Key is required for authenticated mode';
128
+ }
129
+ if (!input.startsWith('sk-')) {
130
+ return 'API Key should start with "sk-"';
131
+ }
132
+ return true;
133
+ },
134
+ },
135
+ {
136
+ type: 'input',
137
+ name: 'base_url',
138
+ message: 'API Base URL:',
139
+ default: serviceConfig.base_url || 'https://api.openai.com/v1/',
140
+ when: (answers) => answers.enabled,
141
+ validate: (input) => {
142
+ try {
143
+ new URL(input);
144
+ return true;
145
+ }
146
+ catch {
147
+ return 'Please enter a valid URL';
148
+ }
149
+ },
150
+ },
151
+ {
152
+ type: 'list',
153
+ name: 'model',
154
+ message: 'Default model:',
155
+ choices: [
156
+ { name: 'GPT-3.5 Turbo (Fast, cost-effective)', value: 'gpt-3.5-turbo' },
157
+ { name: 'GPT-3.5 Turbo 16K (Longer context)', value: 'gpt-3.5-turbo-16k' },
158
+ { name: 'GPT-4 (Most capable)', value: 'gpt-4' },
159
+ { name: 'GPT-4 Turbo (Latest GPT-4)', value: 'gpt-4-turbo-preview' }
160
+ ],
161
+ default: serviceConfig.model || 'gpt-3.5-turbo',
162
+ when: (answers) => answers.enabled,
163
+ },
164
+ ];
165
+ }
166
+ }
167
+ else if (serviceName === 'deepseek') {
168
+ questions = [
169
+ {
170
+ type: 'confirm',
171
+ name: 'enabled',
172
+ message: 'Enable DeepSeek service?',
173
+ default: serviceConfig.enabled || false,
174
+ },
175
+ {
176
+ type: 'password',
177
+ name: 'api_key',
178
+ message: 'DeepSeek API Key:',
179
+ mask: '*',
180
+ when: (answers) => answers.enabled,
181
+ validate: (input) => {
182
+ if (!input.trim()) {
183
+ return 'API Key is required';
184
+ }
185
+ return true;
186
+ },
187
+ },
188
+ {
189
+ type: 'input',
190
+ name: 'model',
191
+ message: 'Model name:',
192
+ default: serviceConfig.model || 'deepseek-chat',
193
+ when: (answers) => answers.enabled,
194
+ },
195
+ {
196
+ type: 'number',
197
+ name: 'max_tokens',
198
+ message: 'Max tokens:',
199
+ default: serviceConfig.max_tokens || 4096,
200
+ when: (answers) => answers.enabled,
201
+ validate: (input) => {
202
+ if (input < 1 || input > 16384) {
203
+ return 'Max tokens must be between 1 and 16384';
204
+ }
205
+ return true;
206
+ },
207
+ },
208
+ {
209
+ type: 'number',
210
+ name: 'temperature',
211
+ message: 'Temperature (0.0-2.0):',
212
+ default: serviceConfig.temperature || 0.7,
213
+ when: (answers) => answers.enabled,
214
+ validate: (input) => {
215
+ if (input < 0 || input > 2) {
216
+ return 'Temperature must be between 0.0 and 2.0';
217
+ }
218
+ return true;
219
+ },
220
+ },
221
+ ];
222
+ }
223
+ else {
224
+ // Generic API service configuration
225
+ questions = [
226
+ {
227
+ type: 'confirm',
228
+ name: 'enabled',
229
+ message: `Enable ${serviceName} service?`,
230
+ default: serviceConfig.enabled || false,
231
+ },
232
+ {
233
+ type: 'password',
234
+ name: 'api_key',
235
+ message: `${serviceName} API Key:`,
236
+ mask: '*',
237
+ when: (answers) => answers.enabled,
238
+ validate: (input) => {
239
+ if (!input.trim()) {
240
+ return 'API Key is required';
241
+ }
242
+ return true;
243
+ },
244
+ },
245
+ {
246
+ type: 'input',
247
+ name: 'endpoint',
248
+ message: 'API Endpoint:',
249
+ default: serviceConfig.endpoint || '',
250
+ when: (answers) => answers.enabled,
251
+ },
252
+ ];
253
+ }
254
+ spinner.stop();
255
+ // Ask configuration questions
256
+ const answers = await inquirer_1.default.prompt(questions);
257
+ // Merge with existing config
258
+ const updatedConfig = {
259
+ name: serviceName,
260
+ type: serviceConfig.type || (serviceName === 'chatgpt_web' ? 'browser' : 'api'),
261
+ ...answers,
262
+ };
263
+ // Save configuration
264
+ spinner.start('Saving configuration...');
265
+ await fs_extra_1.default.ensureDir(servicesDir);
266
+ await fs_extra_1.default.writeJson(serviceFile, updatedConfig, { spaces: 2 });
267
+ spinner.succeed(chalk_1.default.green(`${serviceName} configured successfully!`));
268
+ console.log();
269
+ console.log(chalk_1.default.yellow('Configuration saved to:'));
270
+ console.log(chalk_1.default.cyan(serviceFile));
271
+ console.log();
272
+ if (updatedConfig.enabled) {
273
+ console.log(chalk_1.default.green('Service is enabled. You can now:'));
274
+ console.log(` ${chalk_1.default.cyan(`freeapi start ${serviceName}`)} - Start the service`);
275
+ console.log(` ${chalk_1.default.cyan(`freeapi chat ${serviceName}`)} - Start interactive chat`);
276
+ }
277
+ else {
278
+ console.log(chalk_1.default.yellow('Service is disabled. Enable it to use.'));
279
+ }
280
+ }
281
+ catch (error) {
282
+ spinner.fail(chalk_1.default.red('Configuration failed:'));
283
+ console.error(chalk_1.default.red(error instanceof Error ? error.message : String(error)));
284
+ process.exit(1);
285
+ }
286
+ });
287
+ return command;
288
+ }
289
+ //# sourceMappingURL=config.js.map