Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ tests/e2e/reports/

ASSETS_LICENSES.md

external/
external/
8 changes: 8 additions & 0 deletions resources/codgrep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Place the prebuilt `cg` daemon binary in this directory.

Expected filenames:

- macOS/Linux: `cg`
- Windows: `cg.exe`

BitFun dev/build scripts load the daemon from this repository-relative path.
Binary file added resources/codgrep/cg
Binary file not shown.
2 changes: 2 additions & 0 deletions scripts/desktop-tauri-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readdirSync } from 'fs';
import { ensureOpenSslWindows } from './ensure-openssl-windows.mjs';
import { ensureCodgrepBinary } from './prepare-codgrep-resource.mjs';

const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = join(__dirname, '..');
Expand All @@ -26,6 +27,7 @@ async function main() {
const forward = tauriBuildArgsFromArgv();

await ensureOpenSslWindows();
process.env.CODGREP_DAEMON_BIN = ensureCodgrepBinary();

const desktopDir = join(ROOT, 'src', 'apps', 'desktop');
// Tauri CLI reads CI and rejects numeric "1" (common in CI providers).
Expand Down
67 changes: 63 additions & 4 deletions scripts/dev.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const { execSync, spawn } = require('child_process');
const { existsSync } = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const {
Expand Down Expand Up @@ -110,12 +111,16 @@ function runCommand(command, cwd = ROOT_DIR) {
/**
* Spawn a command with explicit args array (no shell interpolation, safe for paths with spaces)
*/
function spawnCommand(cmd, args, cwd = ROOT_DIR) {
function spawnCommand(cmd, args, cwd = ROOT_DIR, envOverrides = {}) {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, {
cwd,
stdio: 'inherit',
shell: true,
env: {
...process.env,
...envOverrides,
},
});

child.on('close', (code) => {
Expand All @@ -130,6 +135,34 @@ function spawnCommand(cmd, args, cwd = ROOT_DIR) {
});
}

function codgrepBinaryName() {
return process.platform === 'win32' ? 'cg.exe' : 'cg';
}

function codgrepBinaryPath() {
return path.join(ROOT_DIR, 'resources', 'codgrep', codgrepBinaryName());
}

function ensureCodgrepBinary() {
const binaryPath = codgrepBinaryPath();
if (!existsSync(binaryPath)) {
return {
ok: false,
error: new Error(
`codgrep binary not found: ${binaryPath}. Put the prebuilt daemon binary at resources/codgrep/${codgrepBinaryName()}`
),
};
}

return { ok: true, binaryPath };
}

async function ensureCodgrepBundleResource() {
const helperUrl = pathToFileURL(path.join(__dirname, 'prepare-codgrep-resource.mjs')).href;
const helper = await import(helperUrl);
return helper.ensureCodgrepBinary();
}

/**
* Main entry
*/
Expand All @@ -141,7 +174,7 @@ async function main() {
printHeader(`BitFun ${modeLabel} Development`);
printBlank();

const totalSteps = mode === 'desktop' ? 4 : 3;
const totalSteps = mode === 'desktop' ? 5 : 3;

// Step 1: Copy resources
printStep(1, totalSteps, 'Copy resources');
Expand Down Expand Up @@ -181,7 +214,7 @@ async function main() {

// Step 3: Build mobile-web (desktop only)
if (mode === 'desktop') {
printStep(3, 4, 'Build mobile-web');
printStep(3, totalSteps, 'Build mobile-web');
const mobileWebResult = buildMobileWeb({
install: true,
logInfo: printInfo,
Expand All @@ -191,6 +224,27 @@ async function main() {
if (!mobileWebResult.ok) {
process.exit(1);
}

printStep(4, totalSteps, 'Build workspace search daemon');
const codgrepResult = ensureCodgrepBinary();
if (!codgrepResult.ok) {
printError('Workspace search daemon is missing');
if (codgrepResult.error && codgrepResult.error.message) {
printError(codgrepResult.error.message);
}
if (codgrepResult.error && codgrepResult.error.status !== undefined) {
printError(`Exit code: ${codgrepResult.error.status}`);
}
process.exit(1);
}

try {
await ensureCodgrepBundleResource();
} catch (error) {
printError('Validate workspace search daemon failed');
printError(error instanceof Error ? error.message : String(error));
process.exit(1);
}
}

// Final step: Start dev server
Expand All @@ -217,7 +271,12 @@ async function main() {
const desktopDir = path.join(ROOT_DIR, 'src/apps/desktop');
const tauriConfig = path.join(desktopDir, 'tauri.conf.json');
const tauriBin = path.join(ROOT_DIR, 'node_modules', '.bin', 'tauri');
await spawnCommand(tauriBin, ['dev', '--config', tauriConfig], desktopDir);
await spawnCommand(
tauriBin,
['dev', '--config', tauriConfig],
desktopDir,
{ CODGREP_DAEMON_BIN: codgrepBinaryPath() }
);
} else {
await runCommand('pnpm exec vite', path.join(ROOT_DIR, 'src/web-ui'));
}
Expand Down
29 changes: 29 additions & 0 deletions scripts/prepare-codgrep-resource.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { chmodSync, existsSync, statSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = join(__dirname, '..');
const RESOURCE_DIR = join(ROOT, 'resources', 'codgrep');

export function codgrepBinaryName() {
return process.platform === 'win32' ? 'cg.exe' : 'cg';
}

export function codgrepBinaryPath() {
return join(RESOURCE_DIR, codgrepBinaryName());
}

export function ensureCodgrepBinary() {
const binaryPath = codgrepBinaryPath();
if (!existsSync(binaryPath)) {
throw new Error(
`codgrep binary not found: ${binaryPath}. Put the prebuilt daemon binary at resources/codgrep/${codgrepBinaryName()}`
);
}

if (process.platform !== 'win32') {
chmodSync(binaryPath, statSync(binaryPath).mode | 0o111);
}
return binaryPath;
}
30 changes: 25 additions & 5 deletions src/apps/desktop/src/api/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bitfun_core::service::remote_ssh::{
init_remote_workspace_manager, RemoteFileService, RemoteTerminalManager, SSHConnectionManager,
};
use bitfun_core::service::{
ai_rules, announcement, config, filesystem, mcp, token_usage, workspace,
ai_rules, announcement, config, filesystem, mcp, search, token_usage, workspace,
};
use bitfun_core::util::errors::*;

Expand Down Expand Up @@ -67,6 +67,7 @@ pub struct AppState {
pub workspace_path: Arc<RwLock<Option<std::path::PathBuf>>>,
pub config_service: Arc<config::ConfigService>,
pub filesystem_service: Arc<filesystem::FileSystemService>,
pub workspace_search_service: Arc<search::WorkspaceSearchService>,
pub ai_rules_service: Arc<ai_rules::AIRulesService>,
pub agent_registry: Arc<agents::AgentRegistry>,
pub mcp_service: Option<Arc<mcp::MCPService>>,
Expand Down Expand Up @@ -113,6 +114,8 @@ impl AppState {
);
workspace::set_global_workspace_service(workspace_service.clone());
let filesystem_service = Arc::new(filesystem::FileSystemServiceFactory::create_default());
let workspace_search_service = Arc::new(search::WorkspaceSearchService::new());
search::set_global_workspace_search_service(workspace_search_service.clone());

ai_rules::initialize_global_ai_rules_service()
.await
Expand Down Expand Up @@ -170,10 +173,10 @@ impl AppState {
uptime_seconds: 0,
}));

let initial_workspace_path = workspace_service
.get_current_workspace()
.await
.map(|workspace| workspace.root_path);
let initial_workspace = workspace_service.get_current_workspace().await;
let initial_workspace_path = initial_workspace
.as_ref()
.map(|workspace| workspace.root_path.clone());

if let Some(workspace_path) = initial_workspace_path.clone() {
if let Err(e) =
Expand All @@ -194,6 +197,21 @@ impl AppState {
}
}

if let Some(workspace_info) = initial_workspace {
if workspace_info.workspace_kind != workspace::WorkspaceKind::Remote {
if let Err(e) = workspace_search_service
.open_repo(&workspace_info.root_path)
.await
{
log::warn!(
"Failed to restore workspace search repository session on startup: path={}, error={}",
workspace_info.root_path.display(),
e
);
}
}
}

// Initialize SSH Remote services synchronously so they're ready before app starts
let ssh_data_dir = dirs::data_local_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
Expand Down Expand Up @@ -273,6 +291,7 @@ impl AppState {
workspace_path: Arc::new(RwLock::new(initial_workspace_path)),
config_service,
filesystem_service,
workspace_search_service,
ai_rules_service,
agent_registry,
mcp_service,
Expand Down Expand Up @@ -304,6 +323,7 @@ impl AppState {
services.insert("workspace_service".to_string(), true);
services.insert("config_service".to_string(), true);
services.insert("filesystem_service".to_string(), true);
services.insert("workspace_search_service".to_string(), true);

let all_healthy = services.values().all(|&status| status);

Expand Down
Loading
Loading