Skip to content

Commit 8f2e3d4

Browse files
authored
fix: add start args, avoid port conflict (#27)
* fix: add start args, avoid port confict
1 parent afb7c85 commit 8f2e3d4

File tree

7 files changed

+92
-111
lines changed

7 files changed

+92
-111
lines changed

CONTRIBUTING.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Thank you for your interest in contributing to MineContext! We welcome contribut
2929
uv sync
3030

3131
# Run commands in the uv environment
32-
uv run python -m opencontext.cli start
32+
uv run opencontext start
3333
```
3434

3535
**Option 2: Using traditional venv**
@@ -44,10 +44,13 @@ Thank you for your interest in contributing to MineContext! We welcome contribut
4444
**If using uv:**
4545
```bash
4646
# Start with default configuration
47-
uv run python -m opencontext.cli start
47+
uv run opencontext start
4848

4949
# Start with custom config
50-
uv run python -m opencontext.cli start --config /path/to/config.yaml
50+
uv run opencontext start --config /path/to/config.yaml
51+
52+
# Start with custom port (useful for avoiding conflicts)
53+
uv run opencontext start --port 8000
5154
```
5255

5356
**If using traditional venv:**
@@ -56,12 +59,20 @@ Thank you for your interest in contributing to MineContext! We welcome contribut
5659
source venv/bin/activate # On Windows: venv\Scripts\activate
5760

5861
# Start with default configuration
59-
python -m opencontext.cli start
62+
opencontext start
6063

6164
# Start with custom config
62-
python -m opencontext.cli start --config /path/to/config.yaml
65+
opencontext start --config /path/to/config.yaml
66+
67+
# Start with custom port
68+
opencontext start --port 8000
6369
```
6470

71+
**Available startup options:**
72+
- `--config`: Path to configuration file
73+
- `--host`: Host address (overrides config file)
74+
- `--port`: Port number (overrides config file)
75+
6576
## How to Contribute
6677

6778
### Reporting Issues

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,20 @@ server:
285285
debug: false
286286

287287
embedding_model:
288-
provider: doubao # 选项: openai, doubao
288+
provider: doubao # options: openai, doubao
289289
api_key: your-api-key
290290
model: doubao-embedding-large-text-240915
291291

292292
vlm_model:
293-
provider: doubao # 选项: openai, doubao
293+
provider: doubao # options: openai, doubao
294294
api_key: your-api-key
295295
model: doubao-seed-1-6-flash-250828
296296

297297
capture:
298298
enabled: true
299299
screenshot:
300-
enabled: true # 开启截图捕获
301-
capture_interval: 5 # 截图间隔(秒)
300+
enabled: true # enable screenshot capture
301+
capture_interval: 5 # capture interval in seconds
302302
```
303303
304304
2. **Prompt Templates** (`config/prompts_*.yaml`):
@@ -309,18 +309,28 @@ capture:
309309

310310
```bash
311311
# Start with default configuration
312-
uv run python -m opencontext.cli start
312+
uv run opencontext start
313313
314314
# Start with custom config
315-
uv run python -m opencontext.cli start --config /path/to/config.yaml
315+
uv run opencontext start --config /path/to/config.yaml
316+
317+
# Start with custom port (useful for avoiding conflicts)
318+
uv run opencontext start --port 8000
316319
```
317320

321+
**Available Options:**
322+
- `--config`: Path to configuration file
323+
- `--host`: Host address (default: from config or `localhost`)
324+
- `--port`: Port number (default: from config or `8000`)
325+
326+
**Priority**: Command-line arguments > Config file > Default values
327+
318328
Alternatively, you can activate the virtual environment manually:
319329

320330
```bash
321331
source .venv/bin/activate # On Windows: .venv\Scripts\activate
322332
pip install -e .
323-
python -m opencontext.cli start
333+
opencontext start --port 8000
324334
```
325335

326336
## 👥 Community

README_zh.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,18 +309,28 @@ capture:
309309

310310
```bash
311311
# 使用默认配置启动
312-
uv run python -m opencontext.cli start
312+
uv run opencontext start
313313
314314
# 使用自定义配置启动
315-
uv run python -m opencontext.cli start --config /path/to/config.yaml
315+
uv run opencontext start --config /path/to/config.yaml
316+
317+
# 使用自定义端口启动
318+
uv run opencontext start --port 8000
316319
```
317320

321+
**可用选项:**
322+
- `--config`:配置文件路径
323+
- `--host`:主机地址(默认:配置文件中的值或 `localhost`)
324+
- `--port`:端口号(默认:配置文件中的值或 `8000`)
325+
326+
**优先级**:命令行参数 > 配置文件 > 默认值
327+
318328
或者,你也可以手动激活虚拟环境:
319329

320330
```bash
321331
source .venv/bin/activate # Windows系统: .venv\Scripts\activate
322332
pip install -e .
323-
python -m opencontext.cli start
333+
opencontext start --port 8000
324334
```
325335

326336
## 👥 社区

build.sh

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ if ! command -v python3 &> /dev/null; then
1414
exit 1
1515
fi
1616

17-
# 2. Install dependencies from requirements.txt
18-
echo "--> Installing dependencies from requirements.txt..."
19-
python3 -m pip install -r requirements.txt
17+
# 2. Check for uv and install dependencies
18+
if command -v uv &> /dev/null; then
19+
echo "--> Using uv to install dependencies..."
20+
uv sync
21+
else
22+
echo "--> uv not found, using pip to install from pyproject.toml..."
23+
python3 -m pip install -e .
24+
fi
2025

2126
# 3. Install PyInstaller if not present
2227
if ! python3 -c "import PyInstaller" 2>/dev/null; then
@@ -58,48 +63,14 @@ if [ -f "dist/$EXECUTABLE_NAME" ] || [ -f "dist/$EXECUTABLE_NAME.exe" ]; then
5863
echo "⚠️ Warning: 'config' directory not found."
5964
fi
6065

61-
# Create a start script for the packaged application
62-
echo "--> Creating start script in 'dist/'..."
63-
cat > dist/start.sh << 'EOF'
64-
#!/bin/bash
65-
# OpenContext Start Script
66-
67-
# Get the directory where the script is located
68-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
69-
70-
# Change to the script's directory to ensure paths are correct
71-
cd "$SCRIPT_DIR"
72-
73-
# --- Configuration ---
74-
# Please replace 'your_api_key' with your actual API key before running.
75-
export CONTEXT_API_KEY='your_api_key'
76-
77-
# --- Start Application ---
78-
EXECUTABLE="./main"
79-
if [ -f "$EXECUTABLE" ]; then
80-
echo "Starting OpenContext..."
81-
"$EXECUTABLE" start "$@"
82-
elif [ -f "$EXECUTABLE.exe" ]; then
83-
echo "Starting OpenContext..."
84-
"$EXECUTABLE.exe" start "$@"
85-
else
86-
echo "❌ Error: Main executable not found in the current directory."
87-
exit 1
88-
fi
89-
EOF
90-
91-
chmod +x dist/start.sh
92-
9366
echo
94-
echo "🚀 To run the application:"
95-
echo " cd dist/"
96-
echo " # IMPORTANT: Edit start.sh to set your CONTEXT_API_KEY"
97-
echo " ./start.sh"
67+
echo "✅ Build complete!"
68+
echo
69+
echo "To run:"
70+
echo " cd dist && ./main start"
71+
echo
72+
echo "Options: --port 9000 | --host 0.0.0.0 | --config config/config.yaml"
9873
echo
99-
echo "Alternatively, run the executable directly:"
100-
echo " # (Make sure to set the CONTEXT_API_KEY environment variable first)"
101-
echo " ./dist/main start"
102-
10374
else
10475
echo "❌ Build failed. Check the PyInstaller logs above for errors."
10576
exit 1

opencontext/cli.py

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -121,60 +121,40 @@ def start_web_server(context_lab_instance: OpenContext, host: str, port: int, wo
121121

122122
def parse_args() -> argparse.Namespace:
123123
"""Parse command line arguments.
124-
124+
125125
Returns:
126126
Parsed command line arguments
127127
"""
128128
parser = argparse.ArgumentParser(
129129
description="OpenContext - Context capture, processing, storage and consumption system"
130130
)
131-
131+
132132
subparsers = parser.add_subparsers(dest="command", help="Available commands")
133-
133+
134134
# Start command
135-
start_parser = subparsers.add_parser("start", help="Start OpenContext")
135+
start_parser = subparsers.add_parser("start", help="Start OpenContext server")
136136
start_parser.add_argument(
137-
"-c", "--config",
138-
type=str,
137+
"--config",
138+
type=str,
139139
help="Configuration file path"
140140
)
141141
start_parser.add_argument(
142-
"--workers",
143-
type=int,
144-
default=1,
145-
help="Number of worker processes (default: 1)"
142+
"--host",
143+
type=str,
144+
help="Host address (overrides config file)"
146145
)
147-
148-
# Initialize command
149-
init_parser = subparsers.add_parser("init", help="Initialize configuration")
150-
init_parser.add_argument(
151-
"-o", "--output",
152-
type=str,
153-
default="./config/config.yaml",
154-
help="Output configuration file path"
155-
)
156-
157-
# Web command
158-
web_parser = subparsers.add_parser("web", help="Start web interface")
159-
web_parser.add_argument(
160-
"--host",
161-
type=str,
162-
default="localhost",
163-
help="Host address"
164-
)
165-
web_parser.add_argument(
166-
"--port",
167-
type=int,
168-
default=8080,
169-
help="Port number"
146+
start_parser.add_argument(
147+
"--port",
148+
type=int,
149+
help="Port number (overrides config file)"
170150
)
171-
web_parser.add_argument(
151+
start_parser.add_argument(
172152
"--workers",
173153
type=int,
174154
default=1,
175155
help="Number of worker processes (default: 1)"
176156
)
177-
157+
178158
return parser.parse_args()
179159

180160

@@ -214,10 +194,10 @@ def _run_headless_mode(lab_instance: OpenContext) -> None:
214194

215195
def handle_start(args: argparse.Namespace) -> int:
216196
"""Handle the start command.
217-
197+
218198
Args:
219199
args: Parsed command line arguments
220-
200+
221201
Returns:
222202
Exit code (0 for success, 1 for failure)
223203
"""
@@ -231,9 +211,10 @@ def handle_start(args: argparse.Namespace) -> int:
231211

232212
web_config = lab_instance.config.get("web", {})
233213
if web_config.get("enabled", True):
234-
host = web_config.get("host", "localhost")
235-
port = web_config.get("port", 8080)
236-
214+
# Command line arguments override config file
215+
host = args.host if args.host else web_config.get("host", "localhost")
216+
port = args.port if args.port else web_config.get("port", 8000)
217+
237218
try:
238219
logger.info(f"Starting web server on {host}:{port}")
239220
workers = getattr(args, 'workers', 1)
@@ -243,7 +224,7 @@ def handle_start(args: argparse.Namespace) -> int:
243224
lab_instance.shutdown()
244225
else:
245226
_run_headless_mode(lab_instance)
246-
227+
247228
return 0
248229
def _setup_logging(config_path: Optional[str]) -> None:
249230
"""Setup logging configuration.
@@ -261,21 +242,21 @@ def _setup_logging(config_path: Optional[str]) -> None:
261242

262243
def main() -> int:
263244
"""Main entry point.
264-
245+
265246
Returns:
266247
Exit code (0 for success, non-zero for failure)
267248
"""
268249
args = parse_args()
269-
250+
270251
# Setup logging first
271-
_setup_logging(args.config)
272-
252+
_setup_logging(getattr(args, 'config', None))
253+
273254
logger.debug(f"Command line arguments: {args}")
274-
255+
275256
if not args.command:
276-
logger.error("No command specified. Use --help for usage information.")
257+
logger.error("No command specified. Use 'opencontext start' or 'opencontext --help' for usage.")
277258
return 1
278-
259+
279260
if args.command == "start":
280261
return handle_start(args)
281262
else:

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ dependencies = [
4141
"imagehash",
4242
]
4343

44+
[project.scripts]
45+
opencontext = "opencontext.cli:main"
46+
4447
[project.urls]
4548
Homepage = "https://github.com/volcengine/MineContext"
4649
Repository = "https://github.com/volcengine/MineContext"

start.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)