@wirecat/files-cli 0.0.0-stage → 0.2.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.
Files changed (87) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +204 -0
  3. package/README.md +68 -2
  4. package/contracts/wirecat.json +793 -0
  5. package/dist/answer.js +60 -0
  6. package/dist/app.js +6 -0
  7. package/dist/author.js +87 -0
  8. package/dist/bin/files.js +40 -0
  9. package/dist/bundle/chunk-3GCXH3WT.js +263 -0
  10. package/dist/bundle/files.js +21 -0
  11. package/dist/bundle/program-OBKXQ4VY.js +12479 -0
  12. package/dist/config.js +74 -0
  13. package/dist/doctor.js +33 -0
  14. package/dist/entities.js +104 -0
  15. package/dist/failure.js +21 -0
  16. package/dist/identity.js +27 -0
  17. package/dist/import/audio.js +96 -0
  18. package/dist/import/command.js +156 -0
  19. package/dist/import/external-id.js +44 -0
  20. package/dist/import/files.js +206 -0
  21. package/dist/import/readers.js +87 -0
  22. package/dist/index.js +3 -0
  23. package/dist/migrate.js +192 -0
  24. package/dist/notes/about.js +55 -0
  25. package/dist/notes/bound.js +41 -0
  26. package/dist/notes/command.js +241 -0
  27. package/dist/notes/dates.js +15 -0
  28. package/dist/notes/dialects/common.js +79 -0
  29. package/dist/notes/dialects/index.js +6 -0
  30. package/dist/notes/dialects/markdown.js +51 -0
  31. package/dist/notes/dialects/obsidian.js +33 -0
  32. package/dist/notes/dialects/types.js +2 -0
  33. package/dist/notes/embed.js +97 -0
  34. package/dist/notes/export.js +76 -0
  35. package/dist/notes/files.js +87 -0
  36. package/dist/notes/folders-command.js +172 -0
  37. package/dist/notes/folders.js +90 -0
  38. package/dist/notes/import.js +289 -0
  39. package/dist/notes/internal.js +51 -0
  40. package/dist/notes/links.js +46 -0
  41. package/dist/notes/list.js +23 -0
  42. package/dist/notes/renames.js +24 -0
  43. package/dist/notes/search.js +16 -0
  44. package/dist/notes/state.js +33 -0
  45. package/dist/options.js +9 -0
  46. package/dist/output.js +4 -0
  47. package/dist/paths.js +5 -0
  48. package/dist/people/command.js +92 -0
  49. package/dist/people/identity.js +19 -0
  50. package/dist/people/notes-map.js +58 -0
  51. package/dist/people/owner.js +14 -0
  52. package/dist/people/subject.js +19 -0
  53. package/dist/program.js +101 -0
  54. package/dist/progress.js +19 -0
  55. package/dist/reasons.js +187 -0
  56. package/dist/records/input.js +114 -0
  57. package/dist/records/receipt.js +25 -0
  58. package/dist/records/source.js +12 -0
  59. package/dist/refs.js +28 -0
  60. package/dist/run/lock.js +21 -0
  61. package/dist/save-help.js +3 -0
  62. package/dist/search.js +63 -0
  63. package/dist/sources/command.js +295 -0
  64. package/dist/sources/copy.js +32 -0
  65. package/dist/sources/mentions.js +78 -0
  66. package/dist/sources/read.js +262 -0
  67. package/dist/sources/readers/ai-model.js +74 -0
  68. package/dist/sources/readers/api.js +48 -0
  69. package/dist/sources/readers/claude-code.js +74 -0
  70. package/dist/sources/readers/codex.js +72 -0
  71. package/dist/sources/readers/index.js +10 -0
  72. package/dist/sources/readers/keys.js +31 -0
  73. package/dist/sources/readers/ollama.js +84 -0
  74. package/dist/sources/readers/process.js +49 -0
  75. package/dist/sources/readers/types.js +2 -0
  76. package/dist/sources/suggest-command.js +253 -0
  77. package/dist/sources/suggest.js +142 -0
  78. package/dist/sources/transcript.js +52 -0
  79. package/dist/store/group.js +8 -0
  80. package/dist/store/scope.js +25 -0
  81. package/dist/store/source-list.js +22 -0
  82. package/dist/sync.js +182 -0
  83. package/dist/terminal.js +16 -0
  84. package/dist/version.js +2 -0
  85. package/dist/wcat.js +4 -0
  86. package/package.json +89 -4
  87. package/skills/files-cli/SKILL.md +41 -0
@@ -0,0 +1,74 @@
1
+ import { CliError, isCliError } from "@wirecat/cli-base";
2
+ import { aiLeavesComputer, aiSettingPath, aiTextReader, readAiSetting } from "@wirecat/cli-core/documents";
3
+ import { APP } from "../../app.js";
4
+ const SET = "wcat models ai set";
5
+ const chosen = (context) => aiTextReader({
6
+ app: APP,
7
+ env: context.env,
8
+ fetch: context.fetch,
9
+ ...(context.keyring ? { keyring: context.keyring } : {}),
10
+ ...(context.signal ? { signal: context.signal } : {}),
11
+ });
12
+ /** A shared setting this files cannot read (a newer tool wrote it) counts as none, so the other readers still work. */
13
+ const setting = (env) => {
14
+ try {
15
+ return readAiSetting(aiSettingPath(env));
16
+ }
17
+ catch {
18
+ return undefined;
19
+ }
20
+ };
21
+ const reader = (context) => {
22
+ try {
23
+ return chosen(context);
24
+ }
25
+ catch {
26
+ return { refused: "ai.not-set" };
27
+ }
28
+ };
29
+ const problemOf = (refused) => refused === "ai.not-set" ? `no AI model is chosen: ${SET}` : `the AI model has no key: ${SET} --secret-stdin`;
30
+ /** The model the owner chose for every WireCat tool (`models ai set`), with its one key. */
31
+ export const aiModel = {
32
+ id: "ai-model",
33
+ title: "AI model",
34
+ provider: "ai-model",
35
+ leavesComputer: (env) => {
36
+ const found = setting(env);
37
+ return found === undefined || aiLeavesComputer(found);
38
+ },
39
+ modelOf: (env) => {
40
+ const found = setting(env);
41
+ return found ? `${found.provider}/${found.model}` : null;
42
+ },
43
+ readsFiles: false,
44
+ needsModel: false,
45
+ probe: async (context) => {
46
+ const found = reader(context);
47
+ const refused = "refused" in found ? found.refused : undefined;
48
+ return {
49
+ available: refused !== "ai.not-set",
50
+ signedIn: refused === undefined,
51
+ version: null,
52
+ models: "refused" in found ? null : [found.by],
53
+ problem: refused === undefined ? null : problemOf(refused),
54
+ };
55
+ },
56
+ read: async (context, { system, prompt }) => {
57
+ const found = reader(context);
58
+ if ("refused" in found)
59
+ throw found.refused === "ai.not-set"
60
+ ? new CliError("configuration_error", problemOf(found.refused), { reason: "reader.ai-not-set" })
61
+ : new CliError("authentication_error", problemOf(found.refused), { reason: "reader.no-key" });
62
+ try {
63
+ return await found.complete({ purpose: "sources", system, prompt, maxTokens: 4096 });
64
+ }
65
+ catch (error) {
66
+ if (isCliError(error) && error.code === "rate_limited")
67
+ throw new CliError("rate_limited", `${found.by} says the limit is reached; try later`, {
68
+ reason: "reader.limit",
69
+ });
70
+ throw new CliError("invalid_response", `${found.by} failed: wcat models ai check names the cause (key, model, credit or address)`, { reason: "reader.failed" });
71
+ }
72
+ },
73
+ };
74
+ //# sourceMappingURL=ai-model.js.map
@@ -0,0 +1,48 @@
1
+ import { CliError, isCliError } from "@wirecat/cli-base";
2
+ import { modelGateway } from "@wirecat/cli-core";
3
+ import { providerKeys } from "./keys.js";
4
+ /** A provider's API with the owner's own key from the keyring, through cli-core's model gateway. */
5
+ const apiReader = (provider, title) => ({
6
+ id: provider,
7
+ title,
8
+ provider,
9
+ leavesComputer: true,
10
+ readsFiles: false,
11
+ needsModel: true,
12
+ probe: async (context) => {
13
+ const kept = providerKeys(context).read(provider) !== undefined;
14
+ return {
15
+ available: true,
16
+ signedIn: kept,
17
+ version: null,
18
+ models: null,
19
+ problem: kept ? null : `no ${title} key: wcat models ai set ${provider} --secret-stdin`,
20
+ };
21
+ },
22
+ read: async (context, { system, prompt, model }) => {
23
+ const key = providerKeys(context).read(provider);
24
+ if (key === undefined)
25
+ throw new CliError("authentication_error", `no ${title} key: wcat models ai set ${provider} --secret-stdin`, {
26
+ reason: "reader.no-key",
27
+ });
28
+ const gateway = modelGateway({
29
+ resolve: () => ({ provider, model: model ?? "" }),
30
+ // The owner chose this reader for sources by name; that choice is the consent.
31
+ consent: () => true,
32
+ key: () => key,
33
+ fetch: context.fetch,
34
+ ...(context.signal ? { signal: context.signal } : {}),
35
+ });
36
+ try {
37
+ return (await gateway.complete({ purpose: "sources", system, prompt, maxTokens: 4096 })).text;
38
+ }
39
+ catch (error) {
40
+ if (isCliError(error) && error.code === "rate_limited")
41
+ throw new CliError("rate_limited", `${title} says the limit is reached; try later`, { reason: "reader.limit" });
42
+ throw new CliError("invalid_response", `${title} failed: check the key (wcat models ai set ${provider} --secret-stdin), the model and the account's credit`, { reason: "reader.failed" });
43
+ }
44
+ },
45
+ });
46
+ export const openaiReader = apiReader("openai", "OpenAI");
47
+ export const anthropicReader = apiReader("anthropic", "Anthropic");
48
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1,74 @@
1
+ import { mkdtempSync, rmSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { CliError } from "@wirecat/cli-base";
5
+ import { firstLine, runProgram } from "./process.js";
6
+ /**
7
+ * The owner's own signed-in Claude Code, with every tool, MCP server, hook, slash command and saved session off,
8
+ * started in an empty folder: it only reads the text on stdin and answers.
9
+ */
10
+ export const claudeCode = {
11
+ id: "claude-code",
12
+ title: "Claude Code",
13
+ provider: "anthropic",
14
+ leavesComputer: true,
15
+ readsFiles: false,
16
+ needsModel: false,
17
+ probe: async ({ env, signal }) => {
18
+ const version = await runProgram("claude", ["--version"], { env, ...(signal ? { signal } : {}) });
19
+ if (version?.code !== 0)
20
+ return {
21
+ available: false,
22
+ signedIn: null,
23
+ version: null,
24
+ models: null,
25
+ problem: "Claude Code is not installed",
26
+ };
27
+ const status = await runProgram("claude", ["auth", "status", "--json"], { env, ...(signal ? { signal } : {}) });
28
+ let signedIn = false;
29
+ try {
30
+ signedIn = JSON.parse(status?.stdout ?? "").loggedIn === true;
31
+ }
32
+ catch { }
33
+ return {
34
+ available: true,
35
+ signedIn,
36
+ version: firstLine(version.stdout),
37
+ models: null,
38
+ problem: signedIn ? null : "Claude Code is not signed in: run claude auth login",
39
+ };
40
+ },
41
+ read: async ({ env, signal }, { system, prompt, model }) => {
42
+ const folder = mkdtempSync(join(tmpdir(), "files-reader-"));
43
+ try {
44
+ const ran = await runProgram("claude", [
45
+ "--print",
46
+ "--output-format",
47
+ "text",
48
+ "--tools",
49
+ "",
50
+ "--disallowedTools",
51
+ "mcp__*",
52
+ "--strict-mcp-config",
53
+ "--settings",
54
+ '{"disableAllHooks":true}',
55
+ "--disable-slash-commands",
56
+ "--no-session-persistence",
57
+ "--system-prompt",
58
+ system,
59
+ ...(model ? ["--model", model] : []),
60
+ ], { env, cwd: folder, stdin: prompt, ...(signal ? { signal } : {}) });
61
+ if (!ran)
62
+ throw new CliError("configuration_error", "Claude Code is not installed", { reason: "reader.unavailable" });
63
+ if (ran.code !== 0)
64
+ throw new CliError("invalid_response", `Claude Code failed: ${firstLine(ran.stderr || ran.stdout)}`, {
65
+ reason: "reader.failed",
66
+ });
67
+ return ran.stdout;
68
+ }
69
+ finally {
70
+ rmSync(folder, { recursive: true, force: true });
71
+ }
72
+ },
73
+ };
74
+ //# sourceMappingURL=claude-code.js.map
@@ -0,0 +1,72 @@
1
+ import { mkdtempSync, readFileSync, rmSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { CliError } from "@wirecat/cli-base";
5
+ import { firstLine, runProgram } from "./process.js";
6
+ /**
7
+ * The owner's own signed-in Codex CLI in a read-only sandbox and an empty folder. It is an agent that can still read
8
+ * files of this computer, which `readers` says.
9
+ */
10
+ export const codex = {
11
+ id: "codex",
12
+ title: "Codex CLI",
13
+ provider: "openai",
14
+ leavesComputer: true,
15
+ readsFiles: true,
16
+ needsModel: false,
17
+ probe: async ({ env, signal }) => {
18
+ const version = await runProgram("codex", ["--version"], { env, ...(signal ? { signal } : {}) });
19
+ if (version?.code !== 0)
20
+ return { available: false, signedIn: null, version: null, models: null, problem: "Codex CLI is not installed" };
21
+ const status = await runProgram("codex", ["login", "status"], { env, ...(signal ? { signal } : {}) });
22
+ const signedIn = status?.code === 0;
23
+ return {
24
+ available: true,
25
+ signedIn,
26
+ version: firstLine(version.stdout),
27
+ models: null,
28
+ problem: signedIn ? null : "Codex CLI is not signed in: run codex login",
29
+ };
30
+ },
31
+ read: async ({ env, signal }, { system, prompt, model }) => {
32
+ const folder = mkdtempSync(join(tmpdir(), "files-reader-"));
33
+ const answer = join(folder, "answer.txt");
34
+ try {
35
+ const ran = await runProgram("codex", [
36
+ "exec",
37
+ "--sandbox",
38
+ "read-only",
39
+ "--skip-git-repo-check",
40
+ "--ephemeral",
41
+ "--ignore-user-config",
42
+ "--ignore-rules",
43
+ "--color",
44
+ "never",
45
+ "--cd",
46
+ folder,
47
+ "--output-last-message",
48
+ answer,
49
+ ...(model ? ["--model", model] : []),
50
+ "-",
51
+ ],
52
+ // codex exec has no system prompt of its own; the instructions lead the input.
53
+ { env, cwd: folder, stdin: `${system}\n\n${prompt}`, ...(signal ? { signal } : {}) });
54
+ if (!ran)
55
+ throw new CliError("configuration_error", "Codex CLI is not installed", { reason: "reader.unavailable" });
56
+ if (ran.code !== 0)
57
+ throw new CliError("invalid_response", `Codex CLI failed: ${firstLine(ran.stderr || ran.stdout)}`, {
58
+ reason: "reader.failed",
59
+ });
60
+ try {
61
+ return readFileSync(answer, "utf8");
62
+ }
63
+ catch {
64
+ return ran.stdout;
65
+ }
66
+ }
67
+ finally {
68
+ rmSync(folder, { recursive: true, force: true });
69
+ }
70
+ },
71
+ };
72
+ //# sourceMappingURL=codex.js.map
@@ -0,0 +1,10 @@
1
+ import { aiModel } from "./ai-model.js";
2
+ import { anthropicReader, openaiReader } from "./api.js";
3
+ import { claudeCode } from "./claude-code.js";
4
+ import { codex } from "./codex.js";
5
+ import { ollama } from "./ollama.js";
6
+ export { leavesComputer } from "./types.js";
7
+ export const READERS = [aiModel, claudeCode, codex, ollama, openaiReader, anthropicReader];
8
+ export const READER_IDS = READERS.map((reader) => reader.id);
9
+ export const readerOf = (id) => READERS.find((reader) => reader.id === id);
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,31 @@
1
+ import { Credentials, isMovedRun, resolvePaths } from "@wirecat/cli-base";
2
+ import { APP } from "../../app.js";
3
+ /** Providers whose API a reader calls with the owner's own key. */
4
+ export const KEY_PROVIDERS = ["openai", "anthropic"];
5
+ const ORIGIN = {
6
+ openai: "https://api.openai.com",
7
+ anthropic: "https://api.anthropic.com",
8
+ };
9
+ /**
10
+ * The owner's key for a provider, in the one entry every WireCat tool shares: service `WireCat`, account
11
+ * `ai:<origin>`, which `wcat models ai set` writes. files keeps no key of its own. cli-core 2.0.0 does not export its
12
+ * `aiKeys`, so the entry is named here the same way. No environment variable is read: a key in the environment may be
13
+ * another tool's.
14
+ */
15
+ export const providerKeys = ({ env, keyring }) => {
16
+ const shared = { appName: "WireCat", prefix: "WIRECAT", env };
17
+ const credentials = new Credentials({
18
+ configDir: resolvePaths(shared).config,
19
+ service: "WireCat",
20
+ isolated: isMovedRun(shared) || isMovedRun({ appName: APP.appName, prefix: APP.envPrefix, env }),
21
+ env,
22
+ ...(keyring ? { keyring } : {}),
23
+ warn: (message) => process.stderr.write(`${message}\n`),
24
+ });
25
+ const account = (provider) => `ai:${ORIGIN[provider]}`;
26
+ return {
27
+ read: (provider) => credentials.read(account(provider))?.secret,
28
+ write: (provider, key) => credentials.write(account(provider), key, { signIn: true }),
29
+ };
30
+ };
31
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1,84 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ import { onThisComputer } from "@wirecat/cli-core";
3
+ import { loadConfig } from "../../config.js";
4
+ export const OLLAMA = "http://127.0.0.1:11434";
5
+ /** Only an Ollama on this computer: a remote one would be a way to send the text off it. */
6
+ export const ollamaAddressAllowed = (value) => {
7
+ if (!onThisComputer(value))
8
+ return false;
9
+ const url = new URL(value);
10
+ return url.protocol === "http:" || url.protocol === "https:";
11
+ };
12
+ /** The owner's `sources.base_url`, else Ollama's own default port. */
13
+ const addressOf = (env) => {
14
+ let given;
15
+ try {
16
+ given = loadConfig(env).sources?.base_url;
17
+ }
18
+ catch { }
19
+ if (given === undefined)
20
+ return OLLAMA;
21
+ if (!ollamaAddressAllowed(given))
22
+ throw new CliError("configuration_error", `sources.base_url ${given} is not on this computer`, {
23
+ reason: "reader.address-refused",
24
+ });
25
+ return given.replace(/\/+$/, "");
26
+ };
27
+ const notRunning = () => new CliError("provider_unavailable", "Ollama is not running on this computer: start it with ollama serve", {
28
+ reason: "reader.not-running",
29
+ });
30
+ export const ollama = {
31
+ id: "ollama",
32
+ title: "Ollama",
33
+ provider: "local",
34
+ leavesComputer: false,
35
+ readsFiles: false,
36
+ needsModel: true,
37
+ probe: async ({ env, fetch, signal }) => {
38
+ try {
39
+ const response = await fetch(`${addressOf(env)}/api/tags`, { ...(signal ? { signal } : {}) });
40
+ const body = (await response.json());
41
+ const models = (body.models ?? []).flatMap((model) => (typeof model.name === "string" ? [model.name] : []));
42
+ return {
43
+ available: true,
44
+ signedIn: null,
45
+ version: null,
46
+ models,
47
+ problem: models.length ? null : "Ollama has no model: ollama pull <model>",
48
+ };
49
+ }
50
+ catch {
51
+ return {
52
+ available: false,
53
+ signedIn: null,
54
+ version: null,
55
+ models: null,
56
+ problem: "Ollama is not running on this computer",
57
+ };
58
+ }
59
+ },
60
+ read: async ({ env, fetch, signal }, { system, prompt, model }) => {
61
+ const address = addressOf(env);
62
+ let response;
63
+ try {
64
+ response = await fetch(`${address}/api/generate`, {
65
+ method: "POST",
66
+ headers: { "content-type": "application/json" },
67
+ body: JSON.stringify({ model, system, prompt, stream: false, format: "json" }),
68
+ ...(signal ? { signal } : {}),
69
+ });
70
+ }
71
+ catch {
72
+ throw notRunning();
73
+ }
74
+ if (response.status === 404)
75
+ throw new CliError("not_found", `Ollama has no model ${model}: ollama pull ${model}`, {
76
+ reason: "reader.model-missing",
77
+ });
78
+ const body = (await response.json().catch(() => ({})));
79
+ if (!response.ok || typeof body.response !== "string")
80
+ throw new CliError("invalid_response", `Ollama answered ${response.status}`, { reason: "reader.failed" });
81
+ return body.response;
82
+ },
83
+ };
84
+ //# sourceMappingURL=ollama.js.map
@@ -0,0 +1,49 @@
1
+ import { spawn } from "node:child_process";
2
+ import { CliError } from "@wirecat/cli-base";
3
+ const TIMEOUT_MS = 600_000;
4
+ const MAX_OUTPUT = 4 * 1024 * 1024;
5
+ /**
6
+ * Variables a reader started by files must not inherit: an API key would bill the owner's key instead of the signed-in
7
+ * plan, and an agent's variables would make the reader's own runs look like that agent's.
8
+ */
9
+ const WITHHELD = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "CLAUDECODE", "AI_AGENT"];
10
+ export const readerEnv = (env) => Object.fromEntries(Object.entries(env).filter(([name]) => !WITHHELD.includes(name)));
11
+ /** Runs a reader's program to its end; `undefined` when it is not installed. */
12
+ export const runProgram = (program, args, { env, cwd, stdin, signal }) => new Promise((resolve, reject) => {
13
+ const child = spawn(program, args, { env: readerEnv(env), cwd, stdio: ["pipe", "pipe", "pipe"], signal });
14
+ const out = [];
15
+ const err = [];
16
+ let bytes = 0;
17
+ const timer = setTimeout(() => child.kill("SIGTERM"), TIMEOUT_MS);
18
+ const collect = (into) => (chunk) => {
19
+ bytes += chunk.byteLength;
20
+ if (bytes > MAX_OUTPUT)
21
+ child.kill("SIGTERM");
22
+ else
23
+ into.push(chunk);
24
+ };
25
+ child.stdout.on("data", collect(out));
26
+ child.stderr.on("data", collect(err));
27
+ child.on("error", (error) => {
28
+ clearTimeout(timer);
29
+ if (error.code === "ENOENT")
30
+ resolve(undefined);
31
+ else if (error.name === "AbortError")
32
+ reject(new CliError("cancelled", `${program} was stopped`));
33
+ else
34
+ reject(error);
35
+ });
36
+ child.on("close", (code, killed) => {
37
+ clearTimeout(timer);
38
+ if (killed === "SIGTERM" && code === null)
39
+ return reject(new CliError("timeout", `${program} gave no answer in time, or too long an answer`, {
40
+ reason: "reader.timeout",
41
+ }));
42
+ resolve({ code, stdout: Buffer.concat(out).toString("utf8"), stderr: Buffer.concat(err).toString("utf8") });
43
+ });
44
+ child.stdin.on("error", () => { });
45
+ child.stdin.end(stdin ?? "");
46
+ });
47
+ /** The first line a program printed, short enough for an answer; never the text it read. */
48
+ export const firstLine = (text) => text.trim().split("\n")[0]?.slice(0, 200) ?? "";
49
+ //# sourceMappingURL=process.js.map
@@ -0,0 +1,2 @@
1
+ export const leavesComputer = (reader, env) => typeof reader.leavesComputer === "function" ? reader.leavesComputer(env) : reader.leavesComputer;
2
+ //# sourceMappingURL=types.js.map