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
608 changes: 608 additions & 0 deletions packages/native-messaging-host/PROTOCOL.md

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions packages/native-messaging-host/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
param(
[string]$ExtensionId = "fomrtutthjerocmw"
)

$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$distPath = Join-Path $scriptDir "dist\native-host.bat"

if (-not (Test-Path $distPath)) {
Write-Error "Build output not found: $distPath"
exit 1
}

$manifestPath = Join-Path $scriptDir "manifest.json"
$manifest = Get-Content $manifestPath -Raw | ConvertFrom-Json
$manifest.path = $distPath
$manifest.allowed_origins = @("chrome-extension://$ExtensionId/")
$manifest | ConvertTo-Json -Depth 5 | Set-Content $manifestPath -Encoding UTF8
Write-Host "[OK] manifest.json updated"

$regs = @(
"HKCU:\Software\Google\Chrome\NativeMessagingHosts\com.scriptcat.native_host",
"HKCU:\Software\Microsoft\Edge\NativeMessagingHosts\com.scriptcat.native_host"
)
foreach ($regPath in $regs) {
New-Item -Path $regPath -Force | Out-Null
Set-ItemProperty -Path $regPath -Name "(Default)" -Value $manifestPath
Write-Host "[OK] Registered: $regPath"
}
Write-Host "Done"
31 changes: 31 additions & 0 deletions packages/native-messaging-host/launch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { spawn } = require('child_process');

Check failure on line 1 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

Replace `'child_process'` with `"child_process"`

Check failure on line 1 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

'require' is not defined

Check failure on line 1 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

A `require()` style import is forbidden
const path = require('path');

Check failure on line 2 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

Replace `'path'` with `"path"`

Check failure on line 2 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

'require' is not defined

Check failure on line 2 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

A `require()` style import is forbidden

const scriptPath = path.join(__dirname, 'dist', 'index.js');

Check failure on line 4 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

Replace `'dist',·'index.js'` with `"dist",·"index.js"`

Check failure on line 4 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

'__dirname' is not defined
const node = spawn('node', [scriptPath], {

Check failure on line 5 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

Replace `'node'` with `"node"`
stdio: ['pipe', 'pipe', 'pipe'],

Check failure on line 6 in packages/native-messaging-host/launch.js

View workflow job for this annotation

GitHub Actions / Run tests

Replace `··stdio:·['pipe',·'pipe',·'pipe'` with `stdio:·["pipe",·"pipe",·"pipe"`
detached: true
});

node.stderr.on('data', (data) => {
console.error('[NativeHost]', data.toString());
});
node.stdout.on('data', (data) => {
console.log('[NativeHost]', data.toString());
});

node.on('close', (code) => {
console.log(`NativeHost exited with code ${code}`);
});

// Keep stdin open by writing a dummy keepalive every 30 seconds
setInterval(() => {
if (node.stdin.writable) {
node.stdin.write('\n');
}
}, 30000);

// Keep this script alive
setInterval(() => {}, 1e9);
console.log(`NativeHost started with PID ${node.pid}`);
console.log('Press Ctrl+C to stop');
32 changes: 32 additions & 0 deletions packages/native-messaging-host/launch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const scriptPath = path.join(__dirname, 'dist', 'index.js');

const node = spawn('node', [scriptPath], {
stdio: ['pipe', 'pipe', 'pipe'],
detached: false
});

node.stderr.on('data', (data) => {
process.stderr.write(`[NH] ${data}`);
});
node.stdout.on('data', (data) => {
process.stdout.write(`[NH] ${data}`);
});
node.on('close', (code) => {
console.log(`NativeHost exited with code ${code}`);
});

// Keep stdin alive with periodic writes
setInterval(() => {
if (node.stdin.writable) {
node.stdin.write('\n');
}
}, 30000);

// Keep this script alive
setInterval(() => {}, 1e9);
console.log(`NativeHost PID: ${node.pid}`);
9 changes: 9 additions & 0 deletions packages/native-messaging-host/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "com.scriptcat.native_host",
"description": "ScriptCat Native Messaging Host",
"path": "C:\\Users\\Administrator\\ScriptCat\\packages\\native-messaging-host\\dist\\native-host.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://nfeceabbbdpobdgbgpnjooobkknchemm/"
]
}
18 changes: 18 additions & 0 deletions packages/native-messaging-host/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@scriptcat/native-messaging-host",
"version": "1.0.0",
"description": "Native Messaging Host for ScriptCat MCP/CLI integration",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"start": "node dist/index.js"
},
"dependencies": {
"@types/node": "^20.0.0"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}
Loading
Loading