@wraith-ai/core 0.1.0 → 0.1.1

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 (153) hide show
  1. package/README.md +1 -1
  2. package/dist/{attempt-BeB7EltG.d.mts → attempt-HgararU3.d.mts} +2 -1
  3. package/dist/attempt-HgararU3.d.mts.map +1 -0
  4. package/dist/{capabilities-BPb5HSH6.d.mts → capabilities-CNsPrkB7.d.mts} +2 -1
  5. package/dist/capabilities-CNsPrkB7.d.mts.map +1 -0
  6. package/dist/index.d.mts +6 -5
  7. package/dist/index.d.mts.map +1 -0
  8. package/dist/index.mjs +11 -9
  9. package/dist/index.mjs.map +1 -0
  10. package/dist/{methods-CZp6XGn8.mjs → methods-7Ryz6zbd.mjs} +4 -2
  11. package/dist/methods-7Ryz6zbd.mjs.map +1 -0
  12. package/dist/{model-B6tJ9kX_.mjs → model-C16NUwXW.mjs} +2 -0
  13. package/dist/model-C16NUwXW.mjs.map +1 -0
  14. package/dist/{model-D6LQSveS.d.mts → model-_wqePNTL.d.mts} +4 -3
  15. package/dist/model-_wqePNTL.d.mts.map +1 -0
  16. package/dist/{models-dev-Y0ZikMBQ.d.mts → models-dev-DNgymZy3.d.mts} +3 -2
  17. package/dist/models-dev-DNgymZy3.d.mts.map +1 -0
  18. package/dist/{openai-codex-BhtZvNpm.mjs → openai-codex-Bz1EFPEi.mjs} +3 -1
  19. package/dist/openai-codex-Bz1EFPEi.mjs.map +1 -0
  20. package/dist/{openai-codex-Bk-iG7MZ.mjs → openai-codex-uvjhVoVy.mjs} +6 -4
  21. package/dist/openai-codex-uvjhVoVy.mjs.map +1 -0
  22. package/dist/{openai-compatible-hkXv-P74.mjs → openai-compatible-DBppnQ_N.mjs} +4 -2
  23. package/dist/openai-compatible-DBppnQ_N.mjs.map +1 -0
  24. package/dist/{openrouter-BflQBug2.mjs → openrouter-D8nTOAQo.mjs} +5 -3
  25. package/dist/openrouter-D8nTOAQo.mjs.map +1 -0
  26. package/dist/providers/openai-codex.d.mts +4 -3
  27. package/dist/providers/openai-codex.d.mts.map +1 -0
  28. package/dist/providers/openai-codex.mjs +1 -1
  29. package/dist/providers/openai-compatible.d.mts +3 -2
  30. package/dist/providers/openai-compatible.d.mts.map +1 -0
  31. package/dist/providers/openai-compatible.mjs +1 -1
  32. package/dist/providers/openrouter.d.mts +3 -2
  33. package/dist/providers/openrouter.d.mts.map +1 -0
  34. package/dist/providers/openrouter.mjs +1 -1
  35. package/dist/{shared-8d7RsyPH.mjs → shared-BIJaPhMm.mjs} +3 -1
  36. package/dist/shared-BIJaPhMm.mjs.map +1 -0
  37. package/dist/snapshot.d.mts +3 -2
  38. package/dist/snapshot.d.mts.map +1 -0
  39. package/dist/snapshot.mjs +4999 -4977
  40. package/dist/snapshot.mjs.map +1 -0
  41. package/dist/{turn-effort-DquDDwdE.mjs → turn-effort-C61L4JIt.mjs} +2 -0
  42. package/dist/turn-effort-C61L4JIt.mjs.map +1 -0
  43. package/package.json +3 -2
  44. package/src/agent/agent.ts +90 -0
  45. package/src/agent/attachments.ts +195 -0
  46. package/src/agent/classifier.ts +525 -0
  47. package/src/agent/compact.ts +384 -0
  48. package/src/agent/conversation.ts +300 -0
  49. package/src/agent/cost.ts +164 -0
  50. package/src/agent/inbox.ts +80 -0
  51. package/src/agent/instructions.ts +62 -0
  52. package/src/agent/loop.ts +675 -0
  53. package/src/agent/messages.ts +252 -0
  54. package/src/agent/on-demand.ts +58 -0
  55. package/src/agent/prompts.ts +35 -0
  56. package/src/agent/request-images.ts +94 -0
  57. package/src/agent/stop.ts +37 -0
  58. package/src/agent/title.ts +122 -0
  59. package/src/agent/tool-groups.ts +186 -0
  60. package/src/agent/tools.ts +106 -0
  61. package/src/app.ts +67 -0
  62. package/src/config/credential-file-store.ts +66 -0
  63. package/src/config/environment.ts +105 -0
  64. package/src/config/instructions.ts +157 -0
  65. package/src/config/load.ts +667 -0
  66. package/src/config/locked-file.ts +85 -0
  67. package/src/config/paths.ts +368 -0
  68. package/src/config/schema.ts +363 -0
  69. package/src/config/subagents.ts +156 -0
  70. package/src/config/trust.ts +53 -0
  71. package/src/config/write.ts +241 -0
  72. package/src/errors.ts +85 -0
  73. package/src/events/types.ts +258 -0
  74. package/src/glob.ts +161 -0
  75. package/src/index.ts +681 -0
  76. package/src/logger.ts +47 -0
  77. package/src/paths.ts +143 -0
  78. package/src/permissions/chain.ts +42 -0
  79. package/src/permissions/command.ts +233 -0
  80. package/src/permissions/layered.ts +77 -0
  81. package/src/permissions/match.ts +80 -0
  82. package/src/permissions/modes.ts +48 -0
  83. package/src/permissions/policy.ts +160 -0
  84. package/src/permissions/types.ts +159 -0
  85. package/src/providers/cache.ts +107 -0
  86. package/src/providers/capabilities.ts +77 -0
  87. package/src/providers/catalog-view.ts +91 -0
  88. package/src/providers/catalog.ts +65 -0
  89. package/src/providers/errors.ts +108 -0
  90. package/src/providers/factories/index.ts +15 -0
  91. package/src/providers/factories/openai-codex.ts +382 -0
  92. package/src/providers/factories/openai-compatible.ts +81 -0
  93. package/src/providers/factories/openrouter.ts +165 -0
  94. package/src/providers/factories/shared.ts +22 -0
  95. package/src/providers/model.ts +197 -0
  96. package/src/providers/models-dev.ts +355 -0
  97. package/src/providers/oauth/methods.ts +10 -0
  98. package/src/providers/oauth/openai-codex.ts +228 -0
  99. package/src/providers/oauth.ts +177 -0
  100. package/src/providers/openai-codex.ts +7 -0
  101. package/src/providers/openai-compatible.ts +2 -0
  102. package/src/providers/openrouter.ts +2 -0
  103. package/src/providers/registry.ts +464 -0
  104. package/src/providers/turn-effort.ts +156 -0
  105. package/src/providers/types.ts +83 -0
  106. package/src/session/backend.ts +44 -0
  107. package/src/session/file-store.ts +278 -0
  108. package/src/session/format.ts +1037 -0
  109. package/src/session/memory-store.ts +73 -0
  110. package/src/session/session.ts +153 -0
  111. package/src/session/store.ts +219 -0
  112. package/src/sign-in/attempt.ts +166 -0
  113. package/src/sign-in/loopback.ts +74 -0
  114. package/src/snapshot/data.json +8936 -0
  115. package/src/snapshot/index.ts +37 -0
  116. package/src/tools/bin.ts +364 -0
  117. package/src/tools/builtin.ts +48 -0
  118. package/src/tools/change.ts +88 -0
  119. package/src/tools/define.ts +267 -0
  120. package/src/tools/describe.ts +96 -0
  121. package/src/tools/fs/edit-file.ts +129 -0
  122. package/src/tools/fs/glob.ts +75 -0
  123. package/src/tools/fs/grep.ts +212 -0
  124. package/src/tools/fs/line-endings.ts +17 -0
  125. package/src/tools/fs/list-dir.ts +85 -0
  126. package/src/tools/fs/read-file.ts +230 -0
  127. package/src/tools/fs/ripgrep.ts +211 -0
  128. package/src/tools/fs/write-file.ts +97 -0
  129. package/src/tools/job.ts +252 -0
  130. package/src/tools/mcp/auth-store.ts +127 -0
  131. package/src/tools/mcp/command.ts +151 -0
  132. package/src/tools/mcp/config.ts +168 -0
  133. package/src/tools/mcp/servers.ts +463 -0
  134. package/src/tools/mcp/sign-in.ts +226 -0
  135. package/src/tools/mcp/stdio.ts +204 -0
  136. package/src/tools/mcp/tools.ts +173 -0
  137. package/src/tools/on-demand.ts +184 -0
  138. package/src/tools/registry.ts +935 -0
  139. package/src/tools/shell/bash.ts +217 -0
  140. package/src/tools/shell/find-bash.ts +162 -0
  141. package/src/tools/source.ts +35 -0
  142. package/src/tools/spill.ts +127 -0
  143. package/src/tools/subagents.ts +597 -0
  144. package/src/tools/tasks.ts +603 -0
  145. package/src/tools/to-model.ts +33 -0
  146. package/src/tools/web/address.ts +95 -0
  147. package/src/tools/web/exa.ts +163 -0
  148. package/src/tools/web/fetch.ts +373 -0
  149. package/src/tools/web/html.ts +173 -0
  150. package/src/tools/web/search.ts +111 -0
  151. package/src/tools/web/source.ts +51 -0
  152. package/src/units.ts +18 -0
  153. package/src/version.ts +23 -0
@@ -0,0 +1,151 @@
1
+ import { statSync } from "node:fs";
2
+ import { win32 } from "node:path";
3
+ import { windowsCommandLine } from "../job";
4
+
5
+ /**
6
+ * How an MCP server's command is started, and with which variables.
7
+ *
8
+ * On Windows `npx` is `npx.cmd`, which a process cannot start without
9
+ * `cmd.exe`, so the name is looked up the way the shell would and a batch
10
+ * file goes through `cmd.exe /d /s /c`. The escaping is cross-spawn's, which
11
+ * is what every other MCP client starts its servers with.
12
+ */
13
+
14
+ export interface SpawnPlan {
15
+ file: string;
16
+ args: string[];
17
+ /** For `cmd.exe`: the arguments are already quoted for it. */
18
+ verbatim: boolean;
19
+ /** The same as one Windows command line, for the job-object helper. */
20
+ commandLine: string;
21
+ }
22
+
23
+ export interface PlanDeps {
24
+ platform: NodeJS.Platform;
25
+ isFile(path: string): boolean;
26
+ }
27
+
28
+ const defaultDeps: PlanDeps = {
29
+ platform: process.platform,
30
+ isFile: (path) => {
31
+ try {
32
+ return statSync(path).isFile();
33
+ } catch {
34
+ return false;
35
+ }
36
+ },
37
+ };
38
+
39
+ /** The SDK's own lists, kept here so a test can ask for another system's. */
40
+ const INHERITED = {
41
+ win32: [
42
+ "APPDATA",
43
+ "COMSPEC",
44
+ "HOMEDRIVE",
45
+ "HOMEPATH",
46
+ "LOCALAPPDATA",
47
+ "PATH",
48
+ "PATHEXT",
49
+ "PROCESSOR_ARCHITECTURE",
50
+ "PROGRAMDATA",
51
+ "PROGRAMFILES",
52
+ "PROGRAMFILES(X86)",
53
+ "PROGRAMW6432",
54
+ "SYSTEMDRIVE",
55
+ "SYSTEMROOT",
56
+ "TEMP",
57
+ "USERNAME",
58
+ "USERPROFILE",
59
+ "WINDIR",
60
+ ],
61
+ other: ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"],
62
+ } as const;
63
+
64
+ /** Only these are inherited: the API keys in this process are not a server's business. */
65
+ export function serverEnvironment(
66
+ hostEnv: Readonly<Record<string, string | undefined>>,
67
+ own: Readonly<Record<string, string>>,
68
+ platform: NodeJS.Platform = process.platform,
69
+ ): Record<string, string> {
70
+ const env: Record<string, string> = {};
71
+ for (const key of platform === "win32" ? INHERITED.win32 : INHERITED.other) {
72
+ const value = envValue(hostEnv, key, platform);
73
+ // A function exported by bash: a server has no use for it, and it can carry code.
74
+ if (value !== undefined && !value.startsWith("()")) env[key] = value;
75
+ }
76
+ return { ...env, ...own };
77
+ }
78
+
79
+ /** Windows names its variables case-insensitively, and a copied env keeps `Path`. */
80
+ export function envValue(
81
+ env: Readonly<Record<string, string | undefined>>,
82
+ key: string,
83
+ platform: NodeJS.Platform = process.platform,
84
+ ): string | undefined {
85
+ if (platform !== "win32") return env[key];
86
+ const found = Object.keys(env).find((name) => name.toUpperCase() === key.toUpperCase());
87
+ return found === undefined ? undefined : env[found];
88
+ }
89
+
90
+ export function planSpawn(
91
+ command: string,
92
+ args: readonly string[],
93
+ env: Readonly<Record<string, string>>,
94
+ deps: PlanDeps = defaultDeps,
95
+ ): SpawnPlan {
96
+ if (deps.platform !== "win32") {
97
+ return { file: command, args: [...args], verbatim: false, commandLine: [command, ...args].join(" ") };
98
+ }
99
+
100
+ const resolved = resolveOnPath(command, env, deps) ?? command;
101
+ if (!/\.(cmd|bat)$/i.test(resolved)) {
102
+ return { file: resolved, args: [...args], verbatim: false, commandLine: windowsCommandLine(resolved, args) };
103
+ }
104
+
105
+ // npm's own shims re-parse their arguments once more.
106
+ const shim = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i.test(resolved);
107
+ const line = [escapeCommand(win32.normalize(resolved)), ...args.map((arg) => escapeArgument(arg, shim))].join(" ");
108
+ const shell = envValue(env, "COMSPEC", "win32") ?? "cmd.exe";
109
+ const shellArgs = ["/d", "/s", "/c", `"${line}"`];
110
+ return {
111
+ file: shell,
112
+ args: shellArgs,
113
+ verbatim: true,
114
+ commandLine: [windowsCommandLine(shell, []), ...shellArgs].join(" "),
115
+ };
116
+ }
117
+
118
+ /** Where the shell would find the command, or nothing. */
119
+ function resolveOnPath(command: string, env: Readonly<Record<string, string>>, deps: PlanDeps): string | undefined {
120
+ const extensions = (envValue(env, "PATHEXT", "win32") ?? ".COM;.EXE;.BAT;.CMD")
121
+ .split(";")
122
+ .filter((extension) => extension !== "");
123
+ // `node.exe` is itself; `my.server` may still be `my.server.cmd`.
124
+ const candidates = (base: string): string[] => [
125
+ ...(win32.extname(base) === "" ? [] : [base]),
126
+ ...extensions.map((extension) => `${base}${extension}`),
127
+ ];
128
+
129
+ if (win32.isAbsolute(command) || /[\\/]/.test(command)) {
130
+ return candidates(win32.normalize(command)).find((path) => deps.isFile(path));
131
+ }
132
+ const dirs = (envValue(env, "PATH", "win32") ?? "").split(";").filter((dir) => dir !== "");
133
+ for (const dir of dirs) {
134
+ const found = candidates(win32.join(dir, command)).find((path) => deps.isFile(path));
135
+ if (found !== undefined) return found;
136
+ }
137
+ return undefined;
138
+ }
139
+
140
+ const META = /([()\][%!^"`<>&|;, *?])/g;
141
+
142
+ function escapeCommand(command: string): string {
143
+ return command.replace(META, "^$1");
144
+ }
145
+
146
+ /** https://qntm.org/cmd, as cross-spawn applies it. */
147
+ function escapeArgument(arg: string, twice: boolean): string {
148
+ let quoted = arg.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, "$1$1");
149
+ quoted = `"${quoted}"`.replace(META, "^$1");
150
+ return twice ? quoted.replace(META, "^$1") : quoted;
151
+ }
@@ -0,0 +1,168 @@
1
+ import { z } from "zod";
2
+
3
+ /**
4
+ * One MCP server as a config file or a host names it: the shape Claude Code,
5
+ * Cursor and pi use, so an entry from a server's README pastes as it is.
6
+ *
7
+ * The schema checks types only. Whether the fields make one server — a
8
+ * command or a URL, never both — is checked when the server starts, so a bad
9
+ * entry stops that server with a reason and not the whole program.
10
+ */
11
+
12
+ export const mcpServerSchema = z.object({
13
+ /** "stdio" or "http"; absent, `command` or `url` decides. */
14
+ type: z.string().optional(),
15
+ command: z.string().min(1).optional(),
16
+ args: z.array(z.string()).optional(),
17
+ env: z.record(z.string(), z.string()).optional(),
18
+ cwd: z.string().min(1).optional(),
19
+ url: z.string().min(1).optional(),
20
+ headers: z.record(z.string(), z.string()).optional(),
21
+ /** For a server that signs in: a client registered by hand, or a fixed callback port. */
22
+ oauth: z
23
+ .object({
24
+ clientId: z.string().min(1).optional(),
25
+ clientSecret: z.string().min(1).optional(),
26
+ callbackPort: z.number().int().min(1).max(65_535).optional(),
27
+ })
28
+ .optional(),
29
+ enabled: z.boolean().optional(),
30
+ /** One tool call's limit. */
31
+ timeoutMs: z.number().int().positive().optional(),
32
+ });
33
+
34
+ export type McpServerConfig = z.infer<typeof mcpServerSchema>;
35
+
36
+ export const MCP_SERVER_NAME = /^[A-Za-z0-9_-]+$/;
37
+
38
+ export const DEFAULT_MCP_CALL_TIMEOUT_MS = 10 * 60 * 1000;
39
+
40
+ export type ResolvedMcpServer =
41
+ | {
42
+ transport: "stdio";
43
+ command: string;
44
+ args: string[];
45
+ /** Only what the entry sets; the transport adds what a server inherits. */
46
+ env: Record<string, string>;
47
+ /** Absolute, or relative to the host's `cwd`. */
48
+ cwd: string | undefined;
49
+ timeoutMs: number;
50
+ }
51
+ | {
52
+ transport: "http";
53
+ url: URL;
54
+ headers: Record<string, string>;
55
+ oauth: McpOAuthSettings;
56
+ timeoutMs: number;
57
+ };
58
+
59
+ export interface McpOAuthSettings {
60
+ clientId?: string;
61
+ clientSecret?: string;
62
+ callbackPort?: number;
63
+ }
64
+
65
+ export type McpServerCheck =
66
+ | { kind: "disabled" }
67
+ | { kind: "invalid"; reason: string }
68
+ | { kind: "ok"; server: ResolvedMcpServer };
69
+
70
+ const STDIO_ONLY = ["args", "env", "cwd"] as const;
71
+ const HTTP_ONLY = ["headers", "oauth"] as const;
72
+
73
+ /** Whether an entry makes a server, with `${VAR}` filled in from `env`. */
74
+ export function checkMcpServer(
75
+ name: string,
76
+ entry: McpServerConfig,
77
+ env: Readonly<Record<string, string | undefined>>,
78
+ ): McpServerCheck {
79
+ if (entry.enabled === false) return { kind: "disabled" };
80
+ if (!MCP_SERVER_NAME.test(name)) {
81
+ return { kind: "invalid", reason: `"${name}" is not a server name: use letters, digits, "_" and "-".` };
82
+ }
83
+
84
+ const type = entry.type ?? (entry.url !== undefined ? "http" : "stdio");
85
+ if (type === "sse") {
86
+ return {
87
+ kind: "invalid",
88
+ reason: 'The SSE transport is not supported. Most servers also serve streamable HTTP, often at "/mcp".',
89
+ };
90
+ }
91
+ if (type !== "stdio" && type !== "http") {
92
+ return { kind: "invalid", reason: `Unknown transport "${type}": use "stdio" or "http".` };
93
+ }
94
+ if (entry.command !== undefined && entry.url !== undefined) {
95
+ return { kind: "invalid", reason: 'Give either "command" or "url", not both.' };
96
+ }
97
+
98
+ const misplaced = (type === "stdio" ? HTTP_ONLY : STDIO_ONLY).filter((key) => entry[key] !== undefined);
99
+ if (misplaced.length > 0) {
100
+ const where = type === "stdio" ? "a command" : "a URL";
101
+ return { kind: "invalid", reason: `"${misplaced.join('", "')}" means nothing for ${where}.` };
102
+ }
103
+
104
+ try {
105
+ const fill = (value: string): string => expandVariables(value, env);
106
+ const timeoutMs = entry.timeoutMs ?? DEFAULT_MCP_CALL_TIMEOUT_MS;
107
+
108
+ if (type === "stdio") {
109
+ if (entry.command === undefined) return { kind: "invalid", reason: 'A server needs "command" or "url".' };
110
+ return {
111
+ kind: "ok",
112
+ server: {
113
+ transport: "stdio",
114
+ command: fill(entry.command),
115
+ args: (entry.args ?? []).map(fill),
116
+ env: Object.fromEntries(Object.entries(entry.env ?? {}).map(([key, value]) => [key, fill(value)])),
117
+ cwd: entry.cwd === undefined ? undefined : fill(entry.cwd),
118
+ timeoutMs,
119
+ },
120
+ };
121
+ }
122
+
123
+ if (entry.url === undefined) return { kind: "invalid", reason: 'An HTTP server needs "url".' };
124
+ const url = fill(entry.url);
125
+ if (!URL.canParse(url)) return { kind: "invalid", reason: `"${url}" is not a URL.` };
126
+ const parsed = new URL(url);
127
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
128
+ return { kind: "invalid", reason: `"${url}" is not an http or https URL.` };
129
+ }
130
+ const oauth = entry.oauth ?? {};
131
+ if (oauth.clientSecret !== undefined && oauth.clientId === undefined) {
132
+ return { kind: "invalid", reason: '"oauth.clientSecret" needs the "oauth.clientId" it belongs to.' };
133
+ }
134
+ return {
135
+ kind: "ok",
136
+ server: {
137
+ transport: "http",
138
+ url: parsed,
139
+ headers: Object.fromEntries(Object.entries(entry.headers ?? {}).map(([key, value]) => [key, fill(value)])),
140
+ oauth: {
141
+ ...(oauth.clientId === undefined ? {} : { clientId: fill(oauth.clientId) }),
142
+ ...(oauth.clientSecret === undefined ? {} : { clientSecret: fill(oauth.clientSecret) }),
143
+ ...(oauth.callbackPort === undefined ? {} : { callbackPort: oauth.callbackPort }),
144
+ },
145
+ timeoutMs,
146
+ },
147
+ };
148
+ } catch (error) {
149
+ return { kind: "invalid", reason: error instanceof Error ? error.message : String(error) };
150
+ }
151
+ }
152
+
153
+ /**
154
+ * `${NAME}` and `${NAME:-fallback}`. A name that is not set and has no fallback
155
+ * is an error: a header carrying the literal `${TOKEN}` fails later, and less
156
+ * clearly.
157
+ */
158
+ export function expandVariables(
159
+ value: string,
160
+ env: Readonly<Record<string, string | undefined>>,
161
+ ): string {
162
+ return value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g, (_, name: string, fallback?: string) => {
163
+ const found = env[name];
164
+ if (found !== undefined && found !== "") return found;
165
+ if (fallback !== undefined) return fallback;
166
+ throw new Error(`The environment variable ${name} is not set.`);
167
+ });
168
+ }