saarthiclaw 0.2.2 → 0.3.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.
- package/CHANGELOG.md +19 -0
- package/README.md +10 -0
- package/dist/chunks/{chunk-EG5A2Q6X.js → chunk-33MUQ6E5.js} +9 -1
- package/dist/chunks/{chunk-OS5GVLKA.js → chunk-CGJ4VEUD.js} +1 -1
- package/dist/chunks/{chunk-E4DRW4AW.js → chunk-H2FAKNUI.js} +10 -2
- package/dist/chunks/{chunk-OICPXSO2.js → chunk-HFMRQTXE.js} +1 -1
- package/dist/chunks/{chunk-GX2I7K4S.js → chunk-KAWTUWO2.js} +15 -5
- package/dist/chunks/{chunk-VDYO3QC7.js → chunk-N33J57DT.js} +6 -6
- package/dist/chunks/{chunk-DSQDVSZS.js → chunk-OF5PQQQ4.js} +6 -6
- package/dist/chunks/chunk-R4PMMMD5.js +407 -0
- package/dist/chunks/{chunk-SKYTL7AH.js → chunk-S6YZ2A73.js} +17 -8
- package/dist/chunks/{chunk-TTT3KK46.js → chunk-SYH37PL6.js} +5 -5
- package/dist/chunks/{chunk-ZXVP7JFV.js → chunk-XATEICDJ.js} +11 -13
- package/dist/chunks/{chunk-3UFD5346.js → chunk-XT5WAZRV.js} +5 -5
- package/dist/chunks/{client-5WRI2PBK.js → client-TI5W2YDP.js} +3 -5
- package/dist/chunks/{context-AP3U34DQ.js → context-RKFGCGP7.js} +5 -7
- package/dist/chunks/{doctor-O3R2YTRO.js → doctor-ZQICU2F4.js} +11 -6
- package/dist/chunks/{errors-ZXTAHCZI.js → errors-OJZTUG74.js} +2 -4
- package/dist/chunks/orchestrator-ATA2LHPJ.js +22 -0
- package/dist/chunks/{orchestrator-NIDJK4C3.js → orchestrator-NJ7US5XC.js} +9 -11
- package/dist/chunks/{orchestrator-KNCW4K57.js → orchestrator-XLVMKC4V.js} +10 -12
- package/dist/chunks/{session-CRFC3IEU.js → session-ZC5D2O77.js} +6 -3
- package/dist/chunks/{store-KW257WIV.js → store-JG5OKLEO.js} +5 -5
- package/dist/chunks/{sync-S2A65FIE.js → sync-ESB3YWI2.js} +7 -9
- package/dist/chunks/{team-RBSOK3HT.js → team-PFETXIEC.js} +123 -23
- package/dist/chunks/{telegram-JSGRYPIX.js → telegram-IMADPCKR.js} +14 -16
- package/dist/chunks/{wakeup-4IP4JXVF.js → wakeup-UX6AM7M6.js} +16 -18
- package/dist/chunks/workspace-XERWEYJB.js +281 -0
- package/dist/index.js +27 -20
- package/package.json +1 -1
- package/dist/chunks/chunk-BTFIQWAE.js +0 -185
- package/dist/chunks/chunk-G6GCAEKE.js +0 -64
- package/dist/chunks/chunk-XTVJWMUH.js +0 -143
- package/dist/chunks/orchestrator-JFJU4FGP.js +0 -24
- package/dist/chunks/{init-GCJNYE3Y.js → init-5ENYFSX2.js} +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project are documented here. This project follows [Semantic Versioning](https://semver.org/).
|
|
4
4
|
|
|
5
|
+
## 0.3.0 — 2026-11-29
|
|
6
|
+
|
|
7
|
+
- **`saarthiclaw team watch`** keeps a project folder and the team website (dashboard → Code) in step.
|
|
8
|
+
- Website → laptop for everyone: web edits, approved requests, AI changes and reverts are written into the folder within a couple of seconds.
|
|
9
|
+
- Laptop → website for the Admin and Trusted members: edits, new files and deletions in the folder become a version on the website (revertable from History).
|
|
10
|
+
- Safe by design: a file you edited locally is never overwritten — if both sides changed it, it is reported and left alone until you choose `--prefer website` or `--prefer laptop`. Pushes carry the hash of the version they are based on, so a newer website change is never lost. Only text files sync; `node_modules`, build and dot-folders, images and secrets (`.env`, keys) never leave the laptop. A folder that suddenly looks empty does not wipe the website.
|
|
11
|
+
- First sync: an empty website gets the folder; an empty folder gets the website; when both differ you choose which version wins.
|
|
12
|
+
- Options: `--once`, `--prefer website|laptop`, `--interval <seconds>`.
|
|
13
|
+
- `team connect` now points to `team watch`.
|
|
14
|
+
- Needs the dashboard v0.4 (web workspace).
|
|
15
|
+
|
|
16
|
+
## 0.2.3 — 2026-11-19
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
- **Team AI.** The Admin adds one OpenRouter key on the dashboard (Settings → Team AI). Every team member's AI requests then go through the dashboard, which adds that key and uses the Admin's chosen model, so nobody else needs an AI key. Only the team access key is sent from the terminal; the AI key never leaves the dashboard.
|
|
20
|
+
- `team connect` and `team status` say which AI is used, and `doctor` accepts Team AI in place of a local key.
|
|
21
|
+
- The model is picked without a network call, using `teamAi` stored in `.saarthiclaw/team.json`; it is refreshed on every dashboard check.
|
|
22
|
+
- Set `SAARTHICLAW_OWN_AI_KEY=1` to use your own key instead.
|
|
23
|
+
|
|
5
24
|
## 0.2.2 — 2026-11-19
|
|
6
25
|
|
|
7
26
|
### Fixed
|
package/README.md
CHANGED
|
@@ -175,6 +175,16 @@ npx saarthiclaw agent "validate todo text"
|
|
|
175
175
|
```
|
|
176
176
|
Closed the terminal before the Admin decided? `npx saarthiclaw team sync` applies whatever was approved since.
|
|
177
177
|
|
|
178
|
+
### Keep the folder in step with the team website
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
npx saarthiclaw team watch
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Leave it running. Changes made on the website (web editor, approved requests, AI changes, reverts) appear in this folder within seconds. For the Admin and Trusted members it also works the other way: edit files in your folder and they become a new version on the website. If a file was changed on both sides it is left alone and reported; run with `--prefer website` or `--prefer laptop` to settle it. Only text files are synced — `node_modules`, images and secrets such as `.env` never leave your computer.
|
|
185
|
+
|
|
186
|
+
**AI for the whole team (one key):** the Admin adds an OpenRouter key on the dashboard under **Settings → Team AI**. From then on every teammate's `ask`, `plan` and `agent` use it through the dashboard, so teammates need **no AI key of their own**. The key never leaves the dashboard, and everyone uses the model the Admin chose. (`SAARTHICLAW_OWN_AI_KEY=1` makes a terminal use its own key instead.)
|
|
187
|
+
|
|
178
188
|
How it stays safe:
|
|
179
189
|
- **The dashboard never touches files.** It stores the proposal and the decision; your own terminal applies it.
|
|
180
190
|
- **No overwriting newer work.** Each proposal carries a hash of the file it was based on. If the file changed since, `team sync` does not apply it and reports it as failed; ask the agent again.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UserFacingError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-R4PMMMD5.js";
|
|
4
4
|
|
|
5
5
|
// team/client.ts
|
|
6
6
|
var LEVEL_LABEL = {
|
|
@@ -108,6 +108,14 @@ var TeamClient = class {
|
|
|
108
108
|
inboxDone(id) {
|
|
109
109
|
return this.call("POST", `/inbox/${id}/done`);
|
|
110
110
|
}
|
|
111
|
+
/** Website copy of the project: everything after `since` (or a full snapshot when since=0). */
|
|
112
|
+
workspace(since) {
|
|
113
|
+
return this.call("GET", `/workspace?since=${Math.max(0, Math.floor(since))}`);
|
|
114
|
+
}
|
|
115
|
+
/** Admin / Trusted: send laptop edits to the website copy as one version. */
|
|
116
|
+
pushWorkspace(changes, message) {
|
|
117
|
+
return this.call("POST", "/workspace/push", { changes, message });
|
|
118
|
+
}
|
|
111
119
|
invite(name, permissionLevel, regenerate = false) {
|
|
112
120
|
return this.call("POST", "/members", { name, permissionLevel, ...regenerate ? { regenerate: true } : {} });
|
|
113
121
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UserFacingError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-R4PMMMD5.js";
|
|
4
4
|
import {
|
|
5
5
|
userConfigDir
|
|
6
6
|
} from "./chunk-2GDAWRT7.js";
|
|
@@ -106,6 +106,13 @@ function readSyncState(root) {
|
|
|
106
106
|
function writeSyncState(root, state) {
|
|
107
107
|
writeJson(syncStateFile(root), { seen: state.seen.slice(-2e3), lastSyncAt: state.lastSyncAt });
|
|
108
108
|
}
|
|
109
|
+
function teamAiFor(root, env = process.env) {
|
|
110
|
+
if (env.SAARTHICLAW_OWN_AI_KEY === "1") return void 0;
|
|
111
|
+
const link = readTeamLink(root, env);
|
|
112
|
+
if (!link?.teamAi) return void 0;
|
|
113
|
+
const key = readKey(link.server, root, env);
|
|
114
|
+
return key ? { server: link.server, key } : void 0;
|
|
115
|
+
}
|
|
109
116
|
|
|
110
117
|
export {
|
|
111
118
|
KEY_PATTERN,
|
|
@@ -122,5 +129,6 @@ export {
|
|
|
122
129
|
saveKey,
|
|
123
130
|
removeKey,
|
|
124
131
|
readSyncState,
|
|
125
|
-
writeSyncState
|
|
132
|
+
writeSyncState,
|
|
133
|
+
teamAiFor
|
|
126
134
|
};
|
|
@@ -2,12 +2,14 @@ import {
|
|
|
2
2
|
attachAuditLog
|
|
3
3
|
} from "./chunk-5WH3FNFK.js";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
} from "./chunk-
|
|
5
|
+
teamAiFor
|
|
6
|
+
} from "./chunk-H2FAKNUI.js";
|
|
7
7
|
import {
|
|
8
8
|
getAgentModel,
|
|
9
|
+
getTeamModel,
|
|
10
|
+
isSecretPath,
|
|
9
11
|
mapAiError
|
|
10
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-R4PMMMD5.js";
|
|
11
13
|
|
|
12
14
|
// modes/session.ts
|
|
13
15
|
import chalk from "chalk";
|
|
@@ -352,7 +354,11 @@ function createSession(opts) {
|
|
|
352
354
|
config,
|
|
353
355
|
prompter: opts.prompter,
|
|
354
356
|
debug: Boolean(opts.debug),
|
|
355
|
-
model: opts.model ?? (() =>
|
|
357
|
+
model: opts.model ?? (() => {
|
|
358
|
+
const team = teamAiFor(config.root, opts.env ?? process.env);
|
|
359
|
+
if (team) return getTeamModel(team.server, team.key, config.model);
|
|
360
|
+
return getAgentModel({ apiKey: config.openrouterApiKey, model: config.model });
|
|
361
|
+
}),
|
|
356
362
|
teamFetch: opts.teamFetch,
|
|
357
363
|
env: opts.env,
|
|
358
364
|
teamPollMs: opts.teamPollMs
|
|
@@ -385,6 +391,9 @@ ${chalk.red("✖")} ${chalk.bold(f.title)}`);
|
|
|
385
391
|
if (debug && error instanceof Error && error.stack) console.error(chalk.dim(error.stack));
|
|
386
392
|
console.error("");
|
|
387
393
|
}
|
|
394
|
+
function hasAiAccess(session) {
|
|
395
|
+
return Boolean(session.config.openrouterApiKey) || Boolean(teamAiFor(session.root, session.env ?? process.env));
|
|
396
|
+
}
|
|
388
397
|
|
|
389
398
|
export {
|
|
390
399
|
agentConfigFrom,
|
|
@@ -393,5 +402,6 @@ export {
|
|
|
393
402
|
hasWebTools,
|
|
394
403
|
instructionsFor,
|
|
395
404
|
createTracker,
|
|
396
|
-
reportAiError
|
|
405
|
+
reportAiError,
|
|
406
|
+
hasAiAccess
|
|
397
407
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createWebTools
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-HFMRQTXE.js";
|
|
4
4
|
import {
|
|
5
5
|
REPEAT_NOTE,
|
|
6
6
|
createAgentTools,
|
|
@@ -10,18 +10,18 @@ import {
|
|
|
10
10
|
finishOnLastStep,
|
|
11
11
|
generateWithProgress,
|
|
12
12
|
renderTerminalMarkdown
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-OF5PQQQ4.js";
|
|
14
14
|
import {
|
|
15
15
|
ToolExecutor,
|
|
16
16
|
clipOutput
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-CGJ4VEUD.js";
|
|
18
18
|
import {
|
|
19
19
|
Activity
|
|
20
20
|
} from "./chunk-QRAFFI5V.js";
|
|
21
21
|
import {
|
|
22
22
|
describeTeamMode,
|
|
23
23
|
getTeamContext
|
|
24
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-S6YZ2A73.js";
|
|
25
25
|
import {
|
|
26
26
|
RunCancelledError,
|
|
27
27
|
runCancellable
|
|
@@ -31,10 +31,10 @@ import {
|
|
|
31
31
|
createTracker,
|
|
32
32
|
instructionsFor,
|
|
33
33
|
readOnlyAgentConfig
|
|
34
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-KAWTUWO2.js";
|
|
35
35
|
import {
|
|
36
36
|
AI_MAX_RETRIES
|
|
37
|
-
} from "./chunk-
|
|
37
|
+
} from "./chunk-R4PMMMD5.js";
|
|
38
38
|
|
|
39
39
|
// modes/plan/orchestrator.ts
|
|
40
40
|
import fs from "node:fs";
|
|
@@ -8,17 +8,17 @@ import {
|
|
|
8
8
|
} from "./chunk-QRAFFI5V.js";
|
|
9
9
|
import {
|
|
10
10
|
getTeamContext
|
|
11
|
-
} from "./chunk-
|
|
12
|
-
import {
|
|
13
|
-
sha256Hex
|
|
14
|
-
} from "./chunk-E4DRW4AW.js";
|
|
11
|
+
} from "./chunk-S6YZ2A73.js";
|
|
15
12
|
import {
|
|
16
13
|
RunCancelledError,
|
|
17
14
|
runCancellable
|
|
18
15
|
} from "./chunk-HGBZ55ND.js";
|
|
16
|
+
import {
|
|
17
|
+
sha256Hex
|
|
18
|
+
} from "./chunk-H2FAKNUI.js";
|
|
19
19
|
import {
|
|
20
20
|
resolveInsideRoot
|
|
21
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-R4PMMMD5.js";
|
|
22
22
|
|
|
23
23
|
// modes/agent/agent-tools.ts
|
|
24
24
|
import { tool } from "ai";
|
|
@@ -361,7 +361,7 @@ async function sendForApproval(session, tracker, info, team) {
|
|
|
361
361
|
📨 Sent ${res.ids.length} change${res.ids.length === 1 ? "" : "s"} to the Admin for approval.`) + chalk2.dim(" Nothing was written yet.\n"));
|
|
362
362
|
const wait = session.prompter.interactive ? await session.prompter.confirm({ message: "Wait here for the Admin's decision? (approved changes are applied here right after)", initialValue: true }) : false;
|
|
363
363
|
if (wait) {
|
|
364
|
-
const { runTeamSync } = await import("./sync-
|
|
364
|
+
const { runTeamSync } = await import("./sync-ESB3YWI2.js");
|
|
365
365
|
const summary = await runTeamSync(session, { wait: true });
|
|
366
366
|
return { approved: summary.applied > 0, sent: res.ids.length };
|
|
367
367
|
}
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfigError,
|
|
3
|
+
DEFAULT_MODEL,
|
|
4
|
+
loadConfig
|
|
5
|
+
} from "./chunk-2GDAWRT7.js";
|
|
6
|
+
import {
|
|
7
|
+
findWorkspaceRoot
|
|
8
|
+
} from "./chunk-MMIJKDOU.js";
|
|
9
|
+
import {
|
|
10
|
+
NonInteractiveError
|
|
11
|
+
} from "./chunk-IT46XDPQ.js";
|
|
12
|
+
|
|
13
|
+
// cli/errors.ts
|
|
14
|
+
import chalk from "chalk";
|
|
15
|
+
|
|
16
|
+
// ai/ai.config.ts
|
|
17
|
+
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
|
|
18
|
+
|
|
19
|
+
// ai/errors.ts
|
|
20
|
+
var MissingApiKeyError = class extends Error {
|
|
21
|
+
constructor() {
|
|
22
|
+
super("OPENROUTER_API_KEY is not set");
|
|
23
|
+
this.name = "MissingApiKeyError";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var FREE_TIER_NOTE = "Free OpenRouter models allow roughly 20 requests/minute and about 200 requests/day. Wait a minute, switch model with `--model <id>`, or add credits on openrouter.ai.";
|
|
27
|
+
function asErrorLike(e) {
|
|
28
|
+
return e && typeof e === "object" ? e : { message: String(e) };
|
|
29
|
+
}
|
|
30
|
+
function unwrap(e) {
|
|
31
|
+
let cur = asErrorLike(e);
|
|
32
|
+
for (let i = 0; i < 6; i++) {
|
|
33
|
+
if (cur.name === "AI_RetryError" && cur.lastError) {
|
|
34
|
+
cur = asErrorLike(cur.lastError);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (cur.statusCode === void 0 && cur.status === void 0 && cur.cause) {
|
|
38
|
+
const c = asErrorLike(cur.cause);
|
|
39
|
+
if (c.statusCode !== void 0 || c.status !== void 0 || c.name === "AI_RetryError") {
|
|
40
|
+
cur = c;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
return cur;
|
|
47
|
+
}
|
|
48
|
+
var NETWORK_CODES = /* @__PURE__ */ new Set([
|
|
49
|
+
"ECONNREFUSED",
|
|
50
|
+
"ECONNRESET",
|
|
51
|
+
"ENOTFOUND",
|
|
52
|
+
"EAI_AGAIN",
|
|
53
|
+
"ETIMEDOUT",
|
|
54
|
+
"ENETUNREACH",
|
|
55
|
+
"EHOSTUNREACH",
|
|
56
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
57
|
+
"UND_ERR_SOCKET"
|
|
58
|
+
]);
|
|
59
|
+
function findCode(e) {
|
|
60
|
+
let cur = e;
|
|
61
|
+
for (let i = 0; i < 6 && cur && typeof cur === "object"; i++) {
|
|
62
|
+
const c = cur.code;
|
|
63
|
+
if (typeof c === "string") return c;
|
|
64
|
+
cur = cur.cause;
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
function mapAiError(error) {
|
|
69
|
+
const outer = asErrorLike(error);
|
|
70
|
+
if (error instanceof MissingApiKeyError || outer.name === "MissingApiKeyError" || outer.name === "AI_LoadAPIKeyError") {
|
|
71
|
+
return {
|
|
72
|
+
kind: "missing_key",
|
|
73
|
+
title: "No OpenRouter API key configured.",
|
|
74
|
+
hint: "Run `saarthiclaw init` (or set OPENROUTER_API_KEY in your project .env). Get a key at https://openrouter.ai/keys"
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (outer.name === "AI_NoOutputGeneratedError") {
|
|
78
|
+
return {
|
|
79
|
+
kind: "no_output",
|
|
80
|
+
title: "The model finished without giving an answer.",
|
|
81
|
+
hint: "This happens when a model keeps calling tools until it runs out of steps. Try again, ask a more specific question, or use a stronger model with `--model <id>`."
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (outer.name === "AI_NoObjectGeneratedError") {
|
|
85
|
+
return {
|
|
86
|
+
kind: "no_output",
|
|
87
|
+
title: "The model did not return a valid plan.",
|
|
88
|
+
hint: "Try again, or use a model that is better at structured output (e.g. `--model openai/gpt-4o-mini`)."
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (outer.name === "AbortError" || outer.name === "RunCancelledError") {
|
|
92
|
+
return { kind: "cancelled", title: "Cancelled.", hint: "Nothing was written." };
|
|
93
|
+
}
|
|
94
|
+
const e = unwrap(error);
|
|
95
|
+
const status = e.statusCode ?? e.status;
|
|
96
|
+
const body = `${e.message ?? ""} ${e.responseBody ?? ""}`.toLowerCase();
|
|
97
|
+
if (/team ai key/.test(body)) {
|
|
98
|
+
return {
|
|
99
|
+
kind: "missing_key",
|
|
100
|
+
title: "Your team has no AI key yet.",
|
|
101
|
+
hint: "Ask the Admin to add it on the dashboard: Settings → Team AI. (Or run `saarthiclaw init` to use your own key.)",
|
|
102
|
+
status
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (status === 401 && /access key is not valid|revoked/.test(body)) {
|
|
106
|
+
return {
|
|
107
|
+
kind: "invalid_key",
|
|
108
|
+
title: "The team dashboard did not accept your access key.",
|
|
109
|
+
hint: "Ask the Admin for a new key, then run `saarthiclaw team connect` again.",
|
|
110
|
+
status
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (e.name === "TimeoutError" || /timed? ?out/.test(body) && status === void 0) {
|
|
114
|
+
return {
|
|
115
|
+
kind: "timeout",
|
|
116
|
+
title: "The AI request timed out.",
|
|
117
|
+
hint: "The model may be overloaded. Try again, or pick a faster model with `--model <id>`."
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
if (status === 401) {
|
|
121
|
+
return {
|
|
122
|
+
kind: "invalid_key",
|
|
123
|
+
title: "OpenRouter rejected the API key (401).",
|
|
124
|
+
hint: "Check OPENROUTER_API_KEY or run `saarthiclaw init` to replace it. Keys: https://openrouter.ai/keys",
|
|
125
|
+
status
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (status === 402) {
|
|
129
|
+
return {
|
|
130
|
+
kind: "payment",
|
|
131
|
+
title: "OpenRouter says this request needs credits (402).",
|
|
132
|
+
hint: "Use a `:free` model (e.g. qwen/qwen3-coder:free) or add credits at https://openrouter.ai/credits",
|
|
133
|
+
status
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (status === 403) {
|
|
137
|
+
return {
|
|
138
|
+
kind: "forbidden",
|
|
139
|
+
title: "OpenRouter refused the request (403).",
|
|
140
|
+
hint: "Your key may lack permission for this model, or the input was flagged by moderation. Try another model.",
|
|
141
|
+
status
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (status === 429) {
|
|
145
|
+
return {
|
|
146
|
+
kind: "rate_limit",
|
|
147
|
+
title: "Rate limited by OpenRouter (429), even after retrying with backoff.",
|
|
148
|
+
hint: FREE_TIER_NOTE,
|
|
149
|
+
status
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (status === 404 || status === 400 && /(not a valid model|no endpoints found|model .*not (found|exist)|unknown model)/.test(body)) {
|
|
153
|
+
return {
|
|
154
|
+
kind: "model_not_found",
|
|
155
|
+
title: "That model is not available on OpenRouter.",
|
|
156
|
+
hint: "Free model lineups rotate. Pick another with `saarthiclaw init` or `--model <id>` (see https://openrouter.ai/models?q=free).",
|
|
157
|
+
status
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const code = findCode(error);
|
|
161
|
+
if (code && NETWORK_CODES.has(code) || /fetch failed|network|socket hang up|getaddrinfo/.test(body)) {
|
|
162
|
+
return {
|
|
163
|
+
kind: "network",
|
|
164
|
+
title: "Could not reach OpenRouter (network error).",
|
|
165
|
+
hint: "Check your internet connection, proxy or firewall, then try again."
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (status !== void 0 && status >= 500) {
|
|
169
|
+
return {
|
|
170
|
+
kind: "unknown",
|
|
171
|
+
title: `OpenRouter had a server error (${status}).`,
|
|
172
|
+
hint: "This is usually temporary — try again in a moment or switch model.",
|
|
173
|
+
status
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
kind: "unknown",
|
|
178
|
+
title: e.message?.split("\n")[0] || "Unexpected AI error.",
|
|
179
|
+
hint: "Run with --debug for details, or `saarthiclaw doctor` to check your setup.",
|
|
180
|
+
status
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
var AI_MAX_RETRIES = 3;
|
|
184
|
+
|
|
185
|
+
// ai/ai.config.ts
|
|
186
|
+
function getTeamModel(server, accessKey, modelId) {
|
|
187
|
+
const provider = createOpenRouter({
|
|
188
|
+
apiKey: accessKey,
|
|
189
|
+
baseURL: `${server}/api/cli/ai`,
|
|
190
|
+
headers: { "X-Title": "SaarthiClaw" }
|
|
191
|
+
});
|
|
192
|
+
return provider(modelId || DEFAULT_MODEL);
|
|
193
|
+
}
|
|
194
|
+
function getAgentModel(options = {}) {
|
|
195
|
+
let apiKey = options.apiKey?.trim();
|
|
196
|
+
let modelId = options.model?.trim();
|
|
197
|
+
if (!apiKey || !modelId) {
|
|
198
|
+
const cfg = loadConfig({ root: findWorkspaceRoot() });
|
|
199
|
+
apiKey ||= cfg.openrouterApiKey;
|
|
200
|
+
modelId ||= cfg.model;
|
|
201
|
+
}
|
|
202
|
+
if (!apiKey) throw new MissingApiKeyError();
|
|
203
|
+
const provider = createOpenRouter({
|
|
204
|
+
apiKey,
|
|
205
|
+
headers: {
|
|
206
|
+
"HTTP-Referer": "https://github.com/hritik0447/SaarthiClaw",
|
|
207
|
+
"X-Title": "SaarthiClaw"
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return provider(modelId || DEFAULT_MODEL);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// modes/agent/path-safety.ts
|
|
214
|
+
import fs from "node:fs";
|
|
215
|
+
import path from "node:path";
|
|
216
|
+
var PathSafetyError = class extends Error {
|
|
217
|
+
constructor(message) {
|
|
218
|
+
super(message);
|
|
219
|
+
this.name = "PathSafetyError";
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var SECRET_PATTERNS = [
|
|
223
|
+
".env",
|
|
224
|
+
".env.*",
|
|
225
|
+
"*.pem",
|
|
226
|
+
"*.key",
|
|
227
|
+
"*.p12",
|
|
228
|
+
"*.pfx",
|
|
229
|
+
"id_rsa*",
|
|
230
|
+
"id_dsa*",
|
|
231
|
+
"id_ecdsa*",
|
|
232
|
+
"id_ed25519*",
|
|
233
|
+
".npmrc",
|
|
234
|
+
".yarnrc.yml",
|
|
235
|
+
".pypirc",
|
|
236
|
+
".netrc",
|
|
237
|
+
".pgpass",
|
|
238
|
+
".ssh",
|
|
239
|
+
".aws",
|
|
240
|
+
".saarthiclaw"
|
|
241
|
+
];
|
|
242
|
+
function toPosix(p) {
|
|
243
|
+
return p.replace(/\\/g, "/");
|
|
244
|
+
}
|
|
245
|
+
var regexCache = /* @__PURE__ */ new Map();
|
|
246
|
+
function globToRegExp(glob) {
|
|
247
|
+
const cached = regexCache.get(glob);
|
|
248
|
+
if (cached) return cached;
|
|
249
|
+
let re = "";
|
|
250
|
+
for (let i = 0; i < glob.length; i++) {
|
|
251
|
+
const c = glob[i];
|
|
252
|
+
if (c === "*") {
|
|
253
|
+
if (glob[i + 1] === "*") {
|
|
254
|
+
re += ".*";
|
|
255
|
+
i++;
|
|
256
|
+
if (glob[i + 1] === "/") i++;
|
|
257
|
+
} else {
|
|
258
|
+
re += "[^/]*";
|
|
259
|
+
}
|
|
260
|
+
} else if (c === "?") {
|
|
261
|
+
re += "[^/]";
|
|
262
|
+
} else {
|
|
263
|
+
re += c.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const out = new RegExp(`^${re}$`, "i");
|
|
267
|
+
regexCache.set(glob, out);
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
function matchesPattern(relPosix, pattern) {
|
|
271
|
+
const p = toPosix(pattern).replace(/^\.\//, "").replace(/\/$/, "");
|
|
272
|
+
if (!p) return false;
|
|
273
|
+
const norm = relPosix.replace(/^\.\//, "");
|
|
274
|
+
const segments = norm.split("/").filter((s) => s && s !== ".");
|
|
275
|
+
if (!p.includes("/")) {
|
|
276
|
+
const re2 = globToRegExp(p);
|
|
277
|
+
return segments.some((s) => re2.test(s));
|
|
278
|
+
}
|
|
279
|
+
const re = globToRegExp(p);
|
|
280
|
+
for (let i = 1; i <= segments.length; i++) {
|
|
281
|
+
if (re.test(segments.slice(0, i).join("/"))) return true;
|
|
282
|
+
}
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
function isSecretPath(relPosix) {
|
|
286
|
+
return SECRET_PATTERNS.some((p) => matchesPattern(relPosix, p));
|
|
287
|
+
}
|
|
288
|
+
function isExcludedPath(relPosix, patterns) {
|
|
289
|
+
return patterns.some((p) => matchesPattern(relPosix, p));
|
|
290
|
+
}
|
|
291
|
+
function lexists(p) {
|
|
292
|
+
try {
|
|
293
|
+
fs.lstatSync(p);
|
|
294
|
+
return true;
|
|
295
|
+
} catch {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function isInside(root, target) {
|
|
300
|
+
const rel = path.relative(root, target);
|
|
301
|
+
return rel === "" || !rel.startsWith(`..${path.sep}`) && rel !== ".." && !path.isAbsolute(rel);
|
|
302
|
+
}
|
|
303
|
+
var realRootCache;
|
|
304
|
+
function realRootOf(rootAbs) {
|
|
305
|
+
if (realRootCache?.root === rootAbs) return realRootCache.real;
|
|
306
|
+
const real = fs.realpathSync(rootAbs);
|
|
307
|
+
realRootCache = { root: rootAbs, real };
|
|
308
|
+
return real;
|
|
309
|
+
}
|
|
310
|
+
function resolveInsideRoot(root, input) {
|
|
311
|
+
if (typeof input !== "string") throw new PathSafetyError("Path must be a string");
|
|
312
|
+
if (input.includes("\0")) throw new PathSafetyError("Path contains a NUL byte");
|
|
313
|
+
let p = toPosix(input.trim());
|
|
314
|
+
if (!p) p = ".";
|
|
315
|
+
const rootAbs = path.resolve(root);
|
|
316
|
+
if (process.platform !== "win32" && (/^[a-zA-Z]:\//.test(p) || p.startsWith("//"))) {
|
|
317
|
+
throw new PathSafetyError(`Path escapes workspace: ${input}`);
|
|
318
|
+
}
|
|
319
|
+
const abs = path.resolve(rootAbs, p);
|
|
320
|
+
if (!isInside(rootAbs, abs)) {
|
|
321
|
+
throw new PathSafetyError(`Path escapes workspace: ${input}`);
|
|
322
|
+
}
|
|
323
|
+
const realRoot = realRootOf(rootAbs);
|
|
324
|
+
let probe = abs;
|
|
325
|
+
while (!lexists(probe)) {
|
|
326
|
+
const parent = path.dirname(probe);
|
|
327
|
+
if (parent === probe) break;
|
|
328
|
+
probe = parent;
|
|
329
|
+
}
|
|
330
|
+
let realProbe;
|
|
331
|
+
try {
|
|
332
|
+
realProbe = fs.realpathSync(probe);
|
|
333
|
+
} catch {
|
|
334
|
+
throw new PathSafetyError(`Path is a dangling symlink: ${input}`);
|
|
335
|
+
}
|
|
336
|
+
if (!isInside(realRoot, realProbe)) {
|
|
337
|
+
throw new PathSafetyError(`Path resolves outside workspace via symlink: ${input}`);
|
|
338
|
+
}
|
|
339
|
+
return abs;
|
|
340
|
+
}
|
|
341
|
+
function relativePosix(root, abs) {
|
|
342
|
+
const rel = path.relative(path.resolve(root), abs);
|
|
343
|
+
return rel ? toPosix(rel) : ".";
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// cli/errors.ts
|
|
347
|
+
var UserFacingError = class extends Error {
|
|
348
|
+
constructor(message, hint) {
|
|
349
|
+
super(message);
|
|
350
|
+
this.hint = hint;
|
|
351
|
+
this.name = "UserFacingError";
|
|
352
|
+
}
|
|
353
|
+
hint;
|
|
354
|
+
};
|
|
355
|
+
function looksLikeAiError(e) {
|
|
356
|
+
if (!e || typeof e !== "object") return false;
|
|
357
|
+
const name = e.name ?? "";
|
|
358
|
+
return name.startsWith("AI_") || name === "MissingApiKeyError" || "statusCode" in e;
|
|
359
|
+
}
|
|
360
|
+
function reportError(error, debug) {
|
|
361
|
+
let title;
|
|
362
|
+
let hint;
|
|
363
|
+
if (error instanceof UserFacingError) {
|
|
364
|
+
title = error.message;
|
|
365
|
+
hint = error.hint;
|
|
366
|
+
} else if (error instanceof ConfigError) {
|
|
367
|
+
title = "Configuration problem";
|
|
368
|
+
hint = `${error.message}
|
|
369
|
+
Fix the file above or re-run \`saarthiclaw init\`.`;
|
|
370
|
+
} else if (error instanceof NonInteractiveError || error instanceof PathSafetyError) {
|
|
371
|
+
title = error.message;
|
|
372
|
+
} else if (looksLikeAiError(error)) {
|
|
373
|
+
const f = mapAiError(error);
|
|
374
|
+
title = f.title;
|
|
375
|
+
hint = f.hint;
|
|
376
|
+
} else {
|
|
377
|
+
title = error instanceof Error ? error.message : String(error);
|
|
378
|
+
hint = debug ? void 0 : "Run again with --debug for details, or `saarthiclaw doctor` to check your setup.";
|
|
379
|
+
}
|
|
380
|
+
console.error(`
|
|
381
|
+
${chalk.red("✖")} ${chalk.bold(title)}`);
|
|
382
|
+
if (hint) for (const line of hint.split("\n")) console.error(chalk.dim(` ${line}`));
|
|
383
|
+
if (debug && error instanceof Error && error.stack) {
|
|
384
|
+
console.error(chalk.dim(`
|
|
385
|
+
${error.stack}`));
|
|
386
|
+
const cause = error.cause;
|
|
387
|
+
if (cause) console.error(chalk.dim(`Caused by: ${cause instanceof Error ? cause.stack : String(cause)}`));
|
|
388
|
+
}
|
|
389
|
+
console.error("");
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export {
|
|
393
|
+
MissingApiKeyError,
|
|
394
|
+
mapAiError,
|
|
395
|
+
AI_MAX_RETRIES,
|
|
396
|
+
getTeamModel,
|
|
397
|
+
getAgentModel,
|
|
398
|
+
PathSafetyError,
|
|
399
|
+
toPosix,
|
|
400
|
+
globToRegExp,
|
|
401
|
+
isSecretPath,
|
|
402
|
+
isExcludedPath,
|
|
403
|
+
resolveInsideRoot,
|
|
404
|
+
relativePosix,
|
|
405
|
+
UserFacingError,
|
|
406
|
+
reportError
|
|
407
|
+
};
|