friend-assistant 0.1.110 → 0.1.111
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.
- package/dist/BUILD.json +2 -2
- package/dist/friend.mjs +14 -7
- package/dist/pc-agent/agent.js +100 -21
- package/dist/pc-agent/agent.js.sha256 +1 -1
- package/package.json +1 -1
package/dist/BUILD.json
CHANGED
package/dist/friend.mjs
CHANGED
|
@@ -22090,10 +22090,10 @@ if ($got -ne $agentSha) {
|
|
|
22090
22090
|
}
|
|
22091
22091
|
Move-Item -Force $part $agent
|
|
22092
22092
|
|
|
22093
|
-
# 3. start at every logon, and
|
|
22093
|
+
# 3. start at every logon, and every five minutes if it is not running (after a crash or an update; while it runs the task ignores the trigger)
|
|
22094
22094
|
$who = $env:USERDOMAIN + '\\' + $env:USERNAME
|
|
22095
22095
|
$action = New-ScheduledTaskAction -Execute $nodeExe -Argument ($q + $agent + $q) -WorkingDirectory $dir
|
|
22096
|
-
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $who
|
|
22096
|
+
$trigger = @((New-ScheduledTaskTrigger -AtLogOn -User $who), (New-ScheduledTaskTrigger -Once -At ((Get-Date).AddMinutes(2)) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration (New-TimeSpan -Days 3650)))
|
|
22097
22097
|
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -ExecutionTimeLimit ([TimeSpan]::Zero) -MultipleInstances IgnoreNew
|
|
22098
22098
|
$principal = New-ScheduledTaskPrincipal -UserId $who -LogonType Interactive -RunLevel Limited
|
|
22099
22099
|
Register-ScheduledTask -TaskName $task -Action $action -Trigger $trigger -Settings $settings -Principal $principal -Force | Out-Null
|
|
@@ -27330,6 +27330,7 @@ var DeckStore = class {
|
|
|
27330
27330
|
import { createHash as createHash14, randomBytes as randomBytes17, timingSafeEqual as timingSafeEqual5 } from "node:crypto";
|
|
27331
27331
|
var CODE_TTL_MS = 10 * 6e4;
|
|
27332
27332
|
var ACCESS_TTL_MS = 60 * 6e4;
|
|
27333
|
+
var REFRESH_GRACE_MS = 15 * 6e4;
|
|
27333
27334
|
var ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
27334
27335
|
var sha = (s) => createHash14("sha256").update(s).digest("hex");
|
|
27335
27336
|
var token = () => randomBytes17(32).toString("base64url");
|
|
@@ -27399,8 +27400,14 @@ var PcEnroll = class {
|
|
|
27399
27400
|
const h = sha(refresh2);
|
|
27400
27401
|
const cur = this.db.prepare("select * from pc_tokens where refresh_hash = ?").get(h);
|
|
27401
27402
|
if (!cur) {
|
|
27402
|
-
const old = this.db.prepare("select pc_id from pc_tokens where previous_refresh_hash = ?").get(h);
|
|
27403
|
-
if (old)
|
|
27403
|
+
const old = this.db.prepare("select id, pc_id, created_at from pc_tokens where previous_refresh_hash = ?").get(h);
|
|
27404
|
+
if (old) {
|
|
27405
|
+
const pc3 = this.store.get(old.pc_id);
|
|
27406
|
+
if (pc3 && !pc3.revoked && this.clock() - old.created_at < REFRESH_GRACE_MS) {
|
|
27407
|
+
return this.issue(pc3.id, h);
|
|
27408
|
+
}
|
|
27409
|
+
this.store.revoke(old.pc_id);
|
|
27410
|
+
}
|
|
27404
27411
|
return { error: "That sign-in is no longer valid. Add the PC again." };
|
|
27405
27412
|
}
|
|
27406
27413
|
const pc2 = this.store.get(cur.pc_id);
|
|
@@ -29511,7 +29518,7 @@ function currentSelf() {
|
|
|
29511
29518
|
// ../server/src/system/npm.ts
|
|
29512
29519
|
import { spawn as spawn11 } from "node:child_process";
|
|
29513
29520
|
var PACKAGE = "friend-assistant";
|
|
29514
|
-
var runningVersion = () => true ? "0.1.
|
|
29521
|
+
var runningVersion = () => true ? "0.1.111" : "development";
|
|
29515
29522
|
async function npmLatest(fetchFn = fetch, timeoutMs = 1e4) {
|
|
29516
29523
|
try {
|
|
29517
29524
|
const res = await fetchFn(`https://registry.npmjs.org/${PACKAGE}/latest`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(timeoutMs) });
|
|
@@ -35503,7 +35510,7 @@ import { appendFileSync } from "node:fs";
|
|
|
35503
35510
|
import { join as join44 } from "node:path";
|
|
35504
35511
|
var PACKAGE2 = "friend-assistant";
|
|
35505
35512
|
var real2 = {
|
|
35506
|
-
current: () => true ? "0.1.
|
|
35513
|
+
current: () => true ? "0.1.111" : "development",
|
|
35507
35514
|
latest: () => new Promise((resolve13) => {
|
|
35508
35515
|
const child = crossSpawn5("npm", ["view", PACKAGE2, "version", "--prefer-online"], { stdio: ["ignore", "pipe", "ignore"] });
|
|
35509
35516
|
let out = "";
|
|
@@ -35821,7 +35828,7 @@ async function run2(argv, io2) {
|
|
|
35821
35828
|
case "version":
|
|
35822
35829
|
case "--version":
|
|
35823
35830
|
case "-v":
|
|
35824
|
-
io2.out(true ? "0.1.
|
|
35831
|
+
io2.out(true ? "0.1.111" : "development");
|
|
35825
35832
|
return 0;
|
|
35826
35833
|
case "lan":
|
|
35827
35834
|
return await lanCommand(rest, io2);
|
package/dist/pc-agent/agent.js
CHANGED
|
@@ -7108,11 +7108,11 @@ var require_xterm_headless = __commonJS({
|
|
|
7108
7108
|
});
|
|
7109
7109
|
|
|
7110
7110
|
// ../pc-agent/src/main.ts
|
|
7111
|
-
var
|
|
7111
|
+
var import_node_child_process4 = require("node:child_process");
|
|
7112
7112
|
var import_node_crypto2 = require("node:crypto");
|
|
7113
|
-
var
|
|
7113
|
+
var import_node_fs5 = require("node:fs");
|
|
7114
7114
|
var import_node_os3 = require("node:os");
|
|
7115
|
-
var
|
|
7115
|
+
var import_node_path5 = require("node:path");
|
|
7116
7116
|
|
|
7117
7117
|
// ../../node_modules/.pnpm/ws@8.22.0/node_modules/ws/wrapper.mjs
|
|
7118
7118
|
var import_stream = __toESM(require_stream(), 1);
|
|
@@ -28889,7 +28889,7 @@ var import_node_crypto = require("node:crypto");
|
|
|
28889
28889
|
var json2 = { "content-type": "application/json" };
|
|
28890
28890
|
async function signIn(d) {
|
|
28891
28891
|
const f = d.fetch ?? fetch;
|
|
28892
|
-
const
|
|
28892
|
+
const sleep2 = d.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
28893
28893
|
const verifier = (0, import_node_crypto.randomBytes)(32).toString("base64url");
|
|
28894
28894
|
const challenge = (0, import_node_crypto.createHash)("sha256").update(verifier).digest("base64url");
|
|
28895
28895
|
const base = d.server.replace(/\/+$/, "");
|
|
@@ -28899,7 +28899,7 @@ async function signIn(d) {
|
|
|
28899
28899
|
d.show(`${base}${s.verifyPath ?? `/pc/approve?code=${s.userCode}`}`, s.userCode);
|
|
28900
28900
|
const deadline = Date.now() + (d.maxWaitMs ?? (s.expiresIn ?? 600) * 1e3);
|
|
28901
28901
|
for (; ; ) {
|
|
28902
|
-
await
|
|
28902
|
+
await sleep2(Math.max(1, s.interval ?? 3) * 1e3);
|
|
28903
28903
|
if (Date.now() > deadline) throw new Error("The code was not approved in time. Run the install command again.");
|
|
28904
28904
|
const r = await f(`${base}/api/pc/enroll/token`, { method: "POST", headers: json2, body: JSON.stringify({ deviceCode: s.deviceCode, verifier }) });
|
|
28905
28905
|
const body = await r.json().catch(() => ({}));
|
|
@@ -30930,9 +30930,85 @@ function toTask(r) {
|
|
|
30930
30930
|
return { id: Number(r.id), title: String(r.title), state: r.state, note: String(r.note ?? ""), createdAt: Number(r.created_at), startedAt: r.started_at === null ? null : Number(r.started_at), finishedAt: r.finished_at === null ? null : Number(r.finished_at) };
|
|
30931
30931
|
}
|
|
30932
30932
|
|
|
30933
|
-
// ../pc-agent/src/
|
|
30934
|
-
var
|
|
30933
|
+
// ../pc-agent/src/watchdog.ts
|
|
30934
|
+
var import_node_child_process3 = require("node:child_process");
|
|
30935
|
+
var import_node_fs4 = require("node:fs");
|
|
30936
|
+
var import_node_path4 = require("node:path");
|
|
30935
30937
|
var TASK = "Friend PC Agent";
|
|
30938
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
30939
|
+
function runningAgent(pid) {
|
|
30940
|
+
try {
|
|
30941
|
+
process.kill(pid, 0);
|
|
30942
|
+
} catch (e) {
|
|
30943
|
+
if (e.code !== "EPERM") return false;
|
|
30944
|
+
}
|
|
30945
|
+
if (process.platform !== "win32") return true;
|
|
30946
|
+
try {
|
|
30947
|
+
return /node\.exe/i.test((0, import_node_child_process3.execFileSync)("tasklist.exe", ["/FI", `PID eq ${pid}`, "/FO", "CSV", "/NH"], { windowsHide: true, encoding: "utf8", timeout: 8e3 }));
|
|
30948
|
+
} catch {
|
|
30949
|
+
return true;
|
|
30950
|
+
}
|
|
30951
|
+
}
|
|
30952
|
+
async function takeLock(dir, log, waitMs = 8e3) {
|
|
30953
|
+
const file2 = (0, import_node_path4.join)(dir, "agent.pid");
|
|
30954
|
+
const deadline = Date.now() + waitMs;
|
|
30955
|
+
for (; ; ) {
|
|
30956
|
+
let other = 0;
|
|
30957
|
+
try {
|
|
30958
|
+
other = Number((0, import_node_fs4.readFileSync)(file2, "utf8").trim()) || 0;
|
|
30959
|
+
} catch {
|
|
30960
|
+
}
|
|
30961
|
+
if (!other || other === process.pid || !runningAgent(other)) break;
|
|
30962
|
+
if (Date.now() > deadline) {
|
|
30963
|
+
log(`another agent is running (process ${other}): this one stops`);
|
|
30964
|
+
return false;
|
|
30965
|
+
}
|
|
30966
|
+
await sleep(500);
|
|
30967
|
+
}
|
|
30968
|
+
try {
|
|
30969
|
+
(0, import_node_fs4.writeFileSync)(file2, String(process.pid));
|
|
30970
|
+
} catch {
|
|
30971
|
+
}
|
|
30972
|
+
process.on("exit", () => {
|
|
30973
|
+
try {
|
|
30974
|
+
if ((0, import_node_fs4.readFileSync)(file2, "utf8").trim() === String(process.pid)) (0, import_node_fs4.unlinkSync)(file2);
|
|
30975
|
+
} catch {
|
|
30976
|
+
}
|
|
30977
|
+
});
|
|
30978
|
+
return true;
|
|
30979
|
+
}
|
|
30980
|
+
var POWERSHELL = ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command"];
|
|
30981
|
+
function restartSoon(dir, log) {
|
|
30982
|
+
if (process.platform !== "win32" || process.env.FRIEND_PC_NO_TASK) return;
|
|
30983
|
+
try {
|
|
30984
|
+
(0, import_node_child_process3.spawn)("powershell.exe", [...POWERSHELL, `Start-Sleep -Seconds 4; Start-ScheduledTask -TaskName '${TASK}'`], { cwd: dir, detached: true, stdio: "ignore", windowsHide: true }).unref();
|
|
30985
|
+
} catch (e) {
|
|
30986
|
+
log(`could not ask for a restart: ${e.message}`);
|
|
30987
|
+
}
|
|
30988
|
+
}
|
|
30989
|
+
async function ensureWatchdog(log) {
|
|
30990
|
+
if (process.platform !== "win32" || process.env.FRIEND_PC_NO_TASK) return;
|
|
30991
|
+
const script = `$ErrorActionPreference = 'Stop'
|
|
30992
|
+
$task = '${TASK}'
|
|
30993
|
+
$t = Get-ScheduledTask -TaskName $task -ErrorAction SilentlyContinue
|
|
30994
|
+
if (-not $t) { Write-Output 'no task'; exit 0 }
|
|
30995
|
+
foreach ($x in $t.Triggers) { if ($x.Repetition -and $x.Repetition.Interval) { Write-Output 'already'; exit 0 } }
|
|
30996
|
+
$who = $env:USERDOMAIN + '\\' + $env:USERNAME
|
|
30997
|
+
$triggers = @((New-ScheduledTaskTrigger -AtLogOn -User $who), (New-ScheduledTaskTrigger -Once -At ((Get-Date).AddMinutes(2)) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration (New-TimeSpan -Days 3650)))
|
|
30998
|
+
Set-ScheduledTask -TaskName $task -Trigger $triggers | Out-Null
|
|
30999
|
+
Write-Output 'added'`;
|
|
31000
|
+
await new Promise((resolve) => {
|
|
31001
|
+
(0, import_node_child_process3.execFile)("powershell.exe", [...POWERSHELL, script], { windowsHide: true, timeout: 3e4 }, (e, out) => {
|
|
31002
|
+
if (e) log(`could not check the restart task: ${e.message.split("\n")[0]}`);
|
|
31003
|
+
else if (/added/.test(out)) log("the logon task now also starts the agent again every five minutes if it is not running");
|
|
31004
|
+
resolve();
|
|
31005
|
+
});
|
|
31006
|
+
});
|
|
31007
|
+
}
|
|
31008
|
+
|
|
31009
|
+
// ../pc-agent/src/main.ts
|
|
31010
|
+
var VERSION = "0.1.111";
|
|
31011
|
+
var TASK2 = "Friend PC Agent";
|
|
30936
31012
|
function parse3(argv) {
|
|
30937
31013
|
const out = {};
|
|
30938
31014
|
for (let i = 0; i < argv.length; i++) {
|
|
@@ -30948,30 +31024,31 @@ function parse3(argv) {
|
|
|
30948
31024
|
}
|
|
30949
31025
|
var windowsName = () => `Windows ${/^10\.0\.(2[2-9]\d{3})/.test((0, import_node_os3.release)()) ? "11" : "10"}`;
|
|
30950
31026
|
function uninstall(dir) {
|
|
30951
|
-
(0,
|
|
31027
|
+
(0, import_node_child_process4.execFile)("schtasks.exe", ["/delete", "/tn", TASK2, "/f"], { windowsHide: true }, () => {
|
|
30952
31028
|
});
|
|
30953
31029
|
const ps = `Start-Sleep -Seconds 3; Remove-Item -LiteralPath $env:FR_DIR -Recurse -Force -ErrorAction SilentlyContinue`;
|
|
30954
|
-
(0,
|
|
31030
|
+
(0, import_node_child_process4.spawn)("powershell.exe", ["-NoProfile", "-WindowStyle", "Hidden", "-Command", ps], { detached: true, stdio: "ignore", windowsHide: true, env: { ...process.env, FR_DIR: dir } }).unref();
|
|
30955
31031
|
console.log("Friend PC Agent is being removed from this PC.");
|
|
30956
31032
|
process.exit(0);
|
|
30957
31033
|
}
|
|
30958
31034
|
async function main() {
|
|
30959
31035
|
const args = parse3(process.argv.slice(2));
|
|
30960
|
-
const dir = process.env.FRIEND_PC_DIR ?? (0,
|
|
30961
|
-
(0,
|
|
31036
|
+
const dir = process.env.FRIEND_PC_DIR ?? (0, import_node_path5.join)(process.env.LOCALAPPDATA ?? (0, import_node_os3.homedir)(), "Friend", "pc-agent");
|
|
31037
|
+
(0, import_node_fs5.mkdirSync)(dir, { recursive: true });
|
|
30962
31038
|
if (args.uninstall) return uninstall(dir);
|
|
30963
|
-
const logFile = (0,
|
|
31039
|
+
const logFile = (0, import_node_path5.join)(dir, "agent.log");
|
|
30964
31040
|
const log = (line) => {
|
|
30965
31041
|
const text = `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
30966
31042
|
`;
|
|
30967
31043
|
try {
|
|
30968
|
-
if ((0,
|
|
30969
|
-
(0,
|
|
31044
|
+
if ((0, import_node_fs5.existsSync)(logFile) && (0, import_node_fs5.statSync)(logFile).size > 1e6) (0, import_node_fs5.truncateSync)(logFile, 0);
|
|
31045
|
+
(0, import_node_fs5.appendFileSync)(logFile, text);
|
|
30970
31046
|
} catch {
|
|
30971
31047
|
}
|
|
30972
31048
|
if (process.stdout.isTTY) process.stdout.write(text);
|
|
30973
31049
|
};
|
|
30974
|
-
|
|
31050
|
+
if (!await takeLock(dir, log)) process.exit(0);
|
|
31051
|
+
const store = new AgentStore((0, import_node_path5.join)(dir, "agent.db"));
|
|
30975
31052
|
if (typeof args.server === "string") store.set("server", args.server.replace(/\/+$/, ""));
|
|
30976
31053
|
if (typeof args.name === "string") store.set("name", args.name);
|
|
30977
31054
|
const server = store.get("server");
|
|
@@ -30980,9 +31057,9 @@ async function main() {
|
|
|
30980
31057
|
process.exit(2);
|
|
30981
31058
|
}
|
|
30982
31059
|
const self = process.argv[1] ?? "";
|
|
30983
|
-
const agentSha256 = self && (0,
|
|
31060
|
+
const agentSha256 = self && (0, import_node_fs5.existsSync)(self) ? (0, import_node_crypto2.createHash)("sha256").update((0, import_node_fs5.readFileSync)(self)).digest("hex") : void 0;
|
|
30984
31061
|
const holder = {};
|
|
30985
|
-
const helper = new Helper({ spawn: powershellSpawner((0,
|
|
31062
|
+
const helper = new Helper({ spawn: powershellSpawner((0, import_node_path5.join)(dir, "helper")), onEvent: (e, d) => holder.agent?.onHelperEvent(e, d) });
|
|
30986
31063
|
const selfUpdate = async (url2, sha256) => {
|
|
30987
31064
|
if (!url2.startsWith("/api/pc/agent.js") || !self || sha256 === agentSha256) return;
|
|
30988
31065
|
try {
|
|
@@ -30991,9 +31068,10 @@ async function main() {
|
|
|
30991
31068
|
const res = await fetch(`${server}${url2}`, { headers: { authorization: `Bearer ${token}` } });
|
|
30992
31069
|
const body = Buffer.from(await res.arrayBuffer());
|
|
30993
31070
|
if (!res.ok || (0, import_node_crypto2.createHash)("sha256").update(body).digest("hex") !== sha256) throw new Error("the new agent did not match its hash");
|
|
30994
|
-
(0,
|
|
30995
|
-
(0,
|
|
31071
|
+
(0, import_node_fs5.writeFileSync)(`${self}.new`, body);
|
|
31072
|
+
(0, import_node_fs5.renameSync)(`${self}.new`, self);
|
|
30996
31073
|
log(`updated to ${sha256.slice(0, 8)}, restarting`);
|
|
31074
|
+
restartSoon(dir, log);
|
|
30997
31075
|
process.exit(75);
|
|
30998
31076
|
} catch (e) {
|
|
30999
31077
|
log(`update failed: ${e.message}`);
|
|
@@ -31016,17 +31094,18 @@ Friend PC Agent
|
|
|
31016
31094
|
Approve this PC: ${url2}
|
|
31017
31095
|
Code: ${code}
|
|
31018
31096
|
`);
|
|
31019
|
-
if (!process.env.FRIEND_PC_NO_BROWSER) (0,
|
|
31097
|
+
if (!process.env.FRIEND_PC_NO_BROWSER) (0, import_node_child_process4.execFile)("cmd.exe", ["/c", "start", "", url2], { windowsHide: true }, () => {
|
|
31020
31098
|
});
|
|
31021
31099
|
},
|
|
31022
31100
|
log,
|
|
31023
31101
|
onUpdate: (url2, sha) => void selfUpdate(url2, sha),
|
|
31024
31102
|
onRevoked: () => {
|
|
31025
31103
|
log("This PC was removed by the owner. Stopping.");
|
|
31026
|
-
(0,
|
|
31104
|
+
(0, import_node_child_process4.execFile)("schtasks.exe", ["/delete", "/tn", TASK2, "/f"], { windowsHide: true }, () => setTimeout(() => process.exit(0), 500));
|
|
31027
31105
|
}
|
|
31028
31106
|
});
|
|
31029
31107
|
holder.agent = agent;
|
|
31108
|
+
void ensureWatchdog(log);
|
|
31030
31109
|
const stop = () => void agent.stop().finally(() => process.exit(0));
|
|
31031
31110
|
process.on("SIGINT", stop);
|
|
31032
31111
|
process.on("SIGTERM", stop);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
2d1596e6e39f65504e60bc1199c0b2562b7e6077ae06f6fc31e7219683fbc699
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "friend-assistant",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.111",
|
|
4
4
|
"description": "A self-hosted personal AI assistant for your own PC: a full-screen stage, voice, tools with your approval, and your Claude subscription or a local model.",
|
|
5
5
|
"keywords": ["ai", "assistant", "jarvis", "voice", "claude", "ollama", "mcp", "self-hosted"],
|
|
6
6
|
"license": "UNLICENSED",
|