erosolar-cli 1.0.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 (70) hide show
  1. package/ARCHITECTURE.json +157 -0
  2. package/Agents.md +207 -0
  3. package/LICENSE +21 -0
  4. package/SOURCE_OF_TRUTH.json +103 -0
  5. package/dist/adapters/browser/index.js +10 -0
  6. package/dist/adapters/node/index.js +34 -0
  7. package/dist/adapters/remote/index.js +19 -0
  8. package/dist/adapters/types.js +1 -0
  9. package/dist/bin/erosolar.js +6 -0
  10. package/dist/capabilities/bashCapability.js +23 -0
  11. package/dist/capabilities/filesystemCapability.js +23 -0
  12. package/dist/capabilities/index.js +3 -0
  13. package/dist/capabilities/searchCapability.js +23 -0
  14. package/dist/capabilities/tavilyCapability.js +26 -0
  15. package/dist/capabilities/toolRegistry.js +98 -0
  16. package/dist/config.js +60 -0
  17. package/dist/contracts/v1/agent.js +7 -0
  18. package/dist/contracts/v1/provider.js +6 -0
  19. package/dist/contracts/v1/tool.js +6 -0
  20. package/dist/core/agent.js +135 -0
  21. package/dist/core/agentProfiles.js +34 -0
  22. package/dist/core/contextWindow.js +29 -0
  23. package/dist/core/errors/apiKeyErrors.js +114 -0
  24. package/dist/core/preferences.js +157 -0
  25. package/dist/core/secretStore.js +143 -0
  26. package/dist/core/toolRuntime.js +180 -0
  27. package/dist/core/types.js +1 -0
  28. package/dist/plugins/providers/anthropic/index.js +24 -0
  29. package/dist/plugins/providers/deepseek/index.js +24 -0
  30. package/dist/plugins/providers/google/index.js +25 -0
  31. package/dist/plugins/providers/index.js +17 -0
  32. package/dist/plugins/providers/openai/index.js +25 -0
  33. package/dist/plugins/providers/xai/index.js +24 -0
  34. package/dist/plugins/tools/bash/localBashPlugin.js +13 -0
  35. package/dist/plugins/tools/filesystem/localFilesystemPlugin.js +13 -0
  36. package/dist/plugins/tools/index.js +2 -0
  37. package/dist/plugins/tools/nodeDefaults.js +16 -0
  38. package/dist/plugins/tools/registry.js +57 -0
  39. package/dist/plugins/tools/search/localSearchPlugin.js +13 -0
  40. package/dist/plugins/tools/tavily/tavilyPlugin.js +16 -0
  41. package/dist/providers/anthropicProvider.js +218 -0
  42. package/dist/providers/googleProvider.js +193 -0
  43. package/dist/providers/openaiChatCompletionsProvider.js +148 -0
  44. package/dist/providers/openaiResponsesProvider.js +182 -0
  45. package/dist/providers/providerFactory.js +21 -0
  46. package/dist/runtime/agentHost.js +152 -0
  47. package/dist/runtime/agentSession.js +65 -0
  48. package/dist/runtime/browser.js +9 -0
  49. package/dist/runtime/cloud.js +9 -0
  50. package/dist/runtime/node.js +10 -0
  51. package/dist/runtime/universal.js +28 -0
  52. package/dist/shell/__tests__/bracketedPasteManager.test.js +35 -0
  53. package/dist/shell/bracketedPasteManager.js +75 -0
  54. package/dist/shell/interactiveShell.js +1426 -0
  55. package/dist/shell/shellApp.js +392 -0
  56. package/dist/tools/bashTools.js +117 -0
  57. package/dist/tools/diffUtils.js +137 -0
  58. package/dist/tools/fileTools.js +232 -0
  59. package/dist/tools/searchTools.js +175 -0
  60. package/dist/tools/tavilyTools.js +176 -0
  61. package/dist/ui/__tests__/richText.test.js +36 -0
  62. package/dist/ui/codeHighlighter.js +843 -0
  63. package/dist/ui/designSystem.js +98 -0
  64. package/dist/ui/display.js +731 -0
  65. package/dist/ui/layout.js +108 -0
  66. package/dist/ui/richText.js +318 -0
  67. package/dist/ui/theme.js +91 -0
  68. package/dist/workspace.js +44 -0
  69. package/package.json +62 -0
  70. package/scripts/preinstall-clean-bins.mjs +66 -0
@@ -0,0 +1,218 @@
1
+ import Anthropic from '@anthropic-ai/sdk';
2
+ import { APIError, RateLimitError } from '@anthropic-ai/sdk/error.js';
3
+ const DEFAULT_RATE_LIMIT_RETRIES = 4;
4
+ const DEFAULT_RATE_LIMIT_INITIAL_DELAY_MS = 1500;
5
+ const MIN_RATE_LIMIT_DELAY_MS = 750;
6
+ const MAX_RATE_LIMIT_DELAY_MS = 40_000;
7
+ export class AnthropicMessagesProvider {
8
+ id = 'anthropic';
9
+ model;
10
+ client;
11
+ maxTokens;
12
+ temperature;
13
+ rateLimitMaxRetries;
14
+ rateLimitInitialDelayMs;
15
+ constructor(options) {
16
+ this.client = new Anthropic({ apiKey: options.apiKey });
17
+ this.model = options.model;
18
+ this.maxTokens = options.maxTokens ?? 2048;
19
+ this.temperature = options.temperature ?? 0;
20
+ this.rateLimitMaxRetries = Math.max(0, options.rateLimitMaxRetries ?? DEFAULT_RATE_LIMIT_RETRIES);
21
+ this.rateLimitInitialDelayMs = Math.max(MIN_RATE_LIMIT_DELAY_MS, options.rateLimitInitialDelayMs ?? DEFAULT_RATE_LIMIT_INITIAL_DELAY_MS);
22
+ }
23
+ async generate(messages, tools) {
24
+ const { system, chat } = convertConversation(messages);
25
+ const response = await this.executeWithRateLimitRetries(() => this.client.messages.create({
26
+ model: this.model,
27
+ max_tokens: this.maxTokens,
28
+ temperature: this.temperature,
29
+ system: system ?? undefined,
30
+ messages: chat,
31
+ tools: tools.length ? tools.map(mapTool) : undefined,
32
+ }));
33
+ const usage = mapUsage(response.usage);
34
+ const toolCalls = response.content
35
+ .filter((block) => block.type === 'tool_use')
36
+ .map((block) => ({
37
+ id: block.id,
38
+ name: block.name,
39
+ arguments: toRecord(block.input),
40
+ }));
41
+ const textContent = response.content
42
+ .filter((block) => block.type === 'text')
43
+ .map((block) => ('text' in block ? block.text : ''))
44
+ .join('\n')
45
+ .trim();
46
+ if (toolCalls.length > 0) {
47
+ return {
48
+ type: 'tool_calls',
49
+ toolCalls,
50
+ content: textContent,
51
+ usage,
52
+ };
53
+ }
54
+ return {
55
+ type: 'message',
56
+ content: textContent,
57
+ usage,
58
+ };
59
+ }
60
+ async executeWithRateLimitRetries(operation) {
61
+ let retries = 0;
62
+ let delayMs = this.rateLimitInitialDelayMs;
63
+ while (true) {
64
+ try {
65
+ return await operation();
66
+ }
67
+ catch (error) {
68
+ if (!isRateLimitError(error)) {
69
+ throw error;
70
+ }
71
+ if (retries >= this.rateLimitMaxRetries) {
72
+ throw buildRateLimitFailureError(error, retries);
73
+ }
74
+ const waitMs = determineRetryDelay(error.headers, delayMs);
75
+ await sleep(waitMs);
76
+ retries += 1;
77
+ delayMs = Math.min(delayMs * 2, MAX_RATE_LIMIT_DELAY_MS);
78
+ }
79
+ }
80
+ }
81
+ }
82
+ function convertConversation(messages) {
83
+ const systemPrompts = [];
84
+ const chat = [];
85
+ for (const message of messages) {
86
+ switch (message.role) {
87
+ case 'system': {
88
+ systemPrompts.push(message.content);
89
+ break;
90
+ }
91
+ case 'user': {
92
+ chat.push({
93
+ role: 'user',
94
+ content: [{ type: 'text', text: message.content }],
95
+ });
96
+ break;
97
+ }
98
+ case 'assistant': {
99
+ const contentBlocks = [];
100
+ if (message.content.trim().length > 0) {
101
+ contentBlocks.push({ type: 'text', text: message.content });
102
+ }
103
+ for (const call of message.toolCalls ?? []) {
104
+ contentBlocks.push({
105
+ type: 'tool_use',
106
+ id: call.id,
107
+ name: call.name,
108
+ input: call.arguments,
109
+ });
110
+ }
111
+ chat.push({
112
+ role: 'assistant',
113
+ content: contentBlocks.length ? contentBlocks : [{ type: 'text', text: '' }],
114
+ });
115
+ break;
116
+ }
117
+ case 'tool': {
118
+ const block = {
119
+ type: 'tool_result',
120
+ tool_use_id: message.toolCallId,
121
+ content: [{ type: 'text', text: message.content }],
122
+ };
123
+ chat.push({
124
+ role: 'user',
125
+ content: [block],
126
+ });
127
+ break;
128
+ }
129
+ default:
130
+ break;
131
+ }
132
+ }
133
+ return {
134
+ system: systemPrompts.length ? systemPrompts.join('\n\n') : null,
135
+ chat,
136
+ };
137
+ }
138
+ function mapTool(definition) {
139
+ return {
140
+ name: definition.name,
141
+ description: definition.description,
142
+ input_schema: definition.parameters ?? {
143
+ type: 'object',
144
+ properties: {},
145
+ },
146
+ };
147
+ }
148
+ function toRecord(value) {
149
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
150
+ return value;
151
+ }
152
+ return {};
153
+ }
154
+ function mapUsage(usage) {
155
+ if (!usage) {
156
+ return null;
157
+ }
158
+ const total = typeof usage.input_tokens === 'number' && typeof usage.output_tokens === 'number'
159
+ ? usage.input_tokens + usage.output_tokens
160
+ : undefined;
161
+ return {
162
+ inputTokens: usage.input_tokens,
163
+ outputTokens: usage.output_tokens,
164
+ totalTokens: total,
165
+ };
166
+ }
167
+ function isRateLimitError(error) {
168
+ if (error instanceof RateLimitError) {
169
+ return true;
170
+ }
171
+ if (error instanceof APIError && error.status === 429) {
172
+ return true;
173
+ }
174
+ return typeof error === 'object' && error !== null && 'status' in error && error.status === 429;
175
+ }
176
+ function determineRetryDelay(headers, fallbackMs) {
177
+ const retryAfter = parseRetryAfterHeader(headers);
178
+ if (retryAfter !== null) {
179
+ return clamp(retryAfter, MIN_RATE_LIMIT_DELAY_MS, MAX_RATE_LIMIT_DELAY_MS);
180
+ }
181
+ const jitter = fallbackMs * 0.25;
182
+ const randomized = fallbackMs + (Math.random() * (2 * jitter) - jitter);
183
+ return clamp(Math.round(randomized), MIN_RATE_LIMIT_DELAY_MS, MAX_RATE_LIMIT_DELAY_MS);
184
+ }
185
+ function parseRetryAfterHeader(headers) {
186
+ if (!headers || typeof headers.get !== 'function') {
187
+ return null;
188
+ }
189
+ const retryAfter = headers.get('retry-after');
190
+ if (!retryAfter) {
191
+ return null;
192
+ }
193
+ const numeric = Number(retryAfter);
194
+ if (Number.isFinite(numeric) && numeric >= 0) {
195
+ return numeric * 1000;
196
+ }
197
+ const parsedDate = Date.parse(retryAfter);
198
+ if (Number.isFinite(parsedDate)) {
199
+ return Math.max(0, parsedDate - Date.now());
200
+ }
201
+ return null;
202
+ }
203
+ function clamp(value, min, max) {
204
+ return Math.max(min, Math.min(max, value));
205
+ }
206
+ async function sleep(durationMs) {
207
+ await new Promise((resolve) => setTimeout(resolve, durationMs));
208
+ }
209
+ function buildRateLimitFailureError(error, retries) {
210
+ const baseMessage = 'Anthropic rejected the request because the per-minute token rate limit was exceeded.';
211
+ const retryDetails = retries > 0 ? ` Waited and retried ${retries} time${retries === 1 ? '' : 's'} without success.` : '';
212
+ const advisory = ' Reduce the prompt size or wait for usage to reset before trying again. ' +
213
+ 'See https://docs.claude.com/en/api/rate-limits for quota guidance.';
214
+ const original = error.message ? `\nOriginal message: ${error.message}` : '';
215
+ return new Error(`${baseMessage}${retryDetails}${advisory}${original}`, {
216
+ cause: error,
217
+ });
218
+ }
@@ -0,0 +1,193 @@
1
+ import { GoogleGenAI, } from '@google/genai';
2
+ export class GoogleGenAIProvider {
3
+ id;
4
+ model;
5
+ client;
6
+ temperature;
7
+ maxOutputTokens;
8
+ constructor(options) {
9
+ this.client = new GoogleGenAI({
10
+ apiKey: options.apiKey,
11
+ });
12
+ this.id = options.providerId ?? 'google';
13
+ this.model = options.model;
14
+ this.temperature = options.temperature;
15
+ this.maxOutputTokens = options.maxOutputTokens;
16
+ }
17
+ async generate(messages, tools) {
18
+ const { contents, systemInstruction } = mapConversation(messages);
19
+ const config = {};
20
+ if (systemInstruction) {
21
+ config.systemInstruction = systemInstruction;
22
+ }
23
+ if (typeof this.temperature === 'number') {
24
+ config.temperature = this.temperature;
25
+ }
26
+ if (typeof this.maxOutputTokens === 'number') {
27
+ config.maxOutputTokens = this.maxOutputTokens;
28
+ }
29
+ const mappedTools = mapTools(tools);
30
+ if (mappedTools.length > 0) {
31
+ config.tools = mappedTools;
32
+ }
33
+ const response = await this.client.models.generateContent({
34
+ model: this.model,
35
+ contents: contents.length ? contents : createEmptyUserContent(),
36
+ config: Object.keys(config).length ? config : undefined,
37
+ });
38
+ const usage = mapUsage(response.usageMetadata);
39
+ const toolCalls = mapFunctionCalls(response.functionCalls ?? []);
40
+ const content = response.text?.trim() ?? '';
41
+ if (toolCalls.length > 0) {
42
+ return {
43
+ type: 'tool_calls',
44
+ toolCalls,
45
+ content,
46
+ usage,
47
+ };
48
+ }
49
+ return {
50
+ type: 'message',
51
+ content,
52
+ usage,
53
+ };
54
+ }
55
+ }
56
+ function mapConversation(messages) {
57
+ const contents = [];
58
+ const systemPrompts = [];
59
+ for (const message of messages) {
60
+ switch (message.role) {
61
+ case 'system': {
62
+ if (message.content.trim()) {
63
+ systemPrompts.push(message.content.trim());
64
+ }
65
+ break;
66
+ }
67
+ case 'user': {
68
+ contents.push({
69
+ role: 'user',
70
+ parts: [{ text: message.content }],
71
+ });
72
+ break;
73
+ }
74
+ case 'assistant': {
75
+ contents.push(mapAssistantMessage(message));
76
+ break;
77
+ }
78
+ case 'tool': {
79
+ const content = mapToolMessage(message);
80
+ if (content) {
81
+ contents.push(content);
82
+ }
83
+ break;
84
+ }
85
+ default:
86
+ break;
87
+ }
88
+ }
89
+ return {
90
+ contents,
91
+ systemInstruction: systemPrompts.length ? systemPrompts.join('\n\n') : undefined,
92
+ };
93
+ }
94
+ function mapAssistantMessage(message) {
95
+ const parts = [];
96
+ const text = message.content.trim();
97
+ if (text) {
98
+ parts.push({ text });
99
+ }
100
+ for (const call of message.toolCalls ?? []) {
101
+ parts.push({
102
+ functionCall: {
103
+ id: call.id || undefined,
104
+ name: call.name,
105
+ args: toRecord(call.arguments),
106
+ },
107
+ });
108
+ }
109
+ return {
110
+ role: 'model',
111
+ parts: parts.length ? parts : [{ text: '' }],
112
+ };
113
+ }
114
+ function mapToolMessage(message) {
115
+ if (!message.toolCallId) {
116
+ return null;
117
+ }
118
+ return {
119
+ role: 'user',
120
+ parts: [
121
+ {
122
+ functionResponse: {
123
+ id: message.toolCallId,
124
+ name: message.name,
125
+ response: parseToolResponse(message.content),
126
+ },
127
+ },
128
+ ],
129
+ };
130
+ }
131
+ function parseToolResponse(content) {
132
+ const trimmed = content.trim();
133
+ if (!trimmed) {
134
+ return { output: '' };
135
+ }
136
+ try {
137
+ const parsed = JSON.parse(trimmed);
138
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
139
+ return parsed;
140
+ }
141
+ }
142
+ catch {
143
+ // Fallback to wrapping raw tool text below.
144
+ }
145
+ return { output: content };
146
+ }
147
+ function mapFunctionCalls(calls) {
148
+ return calls
149
+ .filter((call) => Boolean(call.name))
150
+ .map((call) => ({
151
+ id: call.id || call.name || 'function_call',
152
+ name: call.name ?? 'function_call',
153
+ arguments: toRecord(call.args),
154
+ }));
155
+ }
156
+ function createEmptyUserContent() {
157
+ return [
158
+ {
159
+ role: 'user',
160
+ parts: [{ text: '' }],
161
+ },
162
+ ];
163
+ }
164
+ function mapTools(tools) {
165
+ if (!tools.length) {
166
+ return [];
167
+ }
168
+ return [
169
+ {
170
+ functionDeclarations: tools.map((tool) => ({
171
+ name: tool.name,
172
+ description: tool.description,
173
+ parametersJsonSchema: tool.parameters ?? { type: 'object', properties: {} },
174
+ })),
175
+ },
176
+ ];
177
+ }
178
+ function mapUsage(metadata) {
179
+ if (!metadata) {
180
+ return null;
181
+ }
182
+ return {
183
+ inputTokens: metadata.promptTokenCount ?? undefined,
184
+ outputTokens: metadata.candidatesTokenCount ?? undefined,
185
+ totalTokens: metadata.totalTokenCount ?? undefined,
186
+ };
187
+ }
188
+ function toRecord(value) {
189
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
190
+ return value;
191
+ }
192
+ return {};
193
+ }
@@ -0,0 +1,148 @@
1
+ import OpenAI from 'openai';
2
+ export class OpenAIChatCompletionsProvider {
3
+ id;
4
+ model;
5
+ client;
6
+ constructor(options) {
7
+ const clientConfig = {
8
+ apiKey: options.apiKey,
9
+ };
10
+ if (options.baseURL) {
11
+ clientConfig.baseURL = options.baseURL;
12
+ }
13
+ this.client = new OpenAI(clientConfig);
14
+ this.id = options.providerId ?? 'openai';
15
+ this.model = options.model;
16
+ }
17
+ async generate(messages, tools) {
18
+ const request = {
19
+ model: this.model,
20
+ messages: mapMessages(messages),
21
+ tools: tools.length ? tools.map(mapTool) : undefined,
22
+ stream: false,
23
+ };
24
+ const completion = await this.client.chat.completions.create(request);
25
+ assertHasChoices(completion);
26
+ const choice = completion.choices[0];
27
+ const usage = mapUsage(completion.usage);
28
+ if (!choice) {
29
+ return {
30
+ type: 'message',
31
+ content: '',
32
+ usage,
33
+ };
34
+ }
35
+ const toolCalls = (choice.message.tool_calls ?? []).map(mapToolCall);
36
+ const content = extractMessageContent(choice);
37
+ if (toolCalls.length > 0) {
38
+ return {
39
+ type: 'tool_calls',
40
+ toolCalls,
41
+ content,
42
+ usage,
43
+ };
44
+ }
45
+ return {
46
+ type: 'message',
47
+ content,
48
+ usage,
49
+ };
50
+ }
51
+ }
52
+ function mapMessages(messages) {
53
+ const params = [];
54
+ for (const message of messages) {
55
+ switch (message.role) {
56
+ case 'system':
57
+ case 'user': {
58
+ params.push({
59
+ role: message.role,
60
+ content: message.content,
61
+ });
62
+ break;
63
+ }
64
+ case 'assistant': {
65
+ params.push({
66
+ role: 'assistant',
67
+ content: message.content,
68
+ tool_calls: message.toolCalls?.map((call, index) => ({
69
+ id: call.id || `call_${index}`,
70
+ type: 'function',
71
+ function: {
72
+ name: call.name,
73
+ arguments: JSON.stringify(call.arguments ?? {}),
74
+ },
75
+ })),
76
+ });
77
+ break;
78
+ }
79
+ case 'tool': {
80
+ params.push({
81
+ role: 'tool',
82
+ content: message.content,
83
+ tool_call_id: message.toolCallId,
84
+ });
85
+ break;
86
+ }
87
+ default:
88
+ break;
89
+ }
90
+ }
91
+ return params;
92
+ }
93
+ function mapTool(definition) {
94
+ const parameters = definition.parameters ??
95
+ {
96
+ type: 'object',
97
+ properties: {},
98
+ };
99
+ return {
100
+ type: 'function',
101
+ function: {
102
+ name: definition.name,
103
+ description: definition.description,
104
+ parameters,
105
+ },
106
+ };
107
+ }
108
+ function extractMessageContent(choice) {
109
+ const message = choice.message;
110
+ const content = message?.content;
111
+ if (typeof content === 'string' && content.trim()) {
112
+ return content.trim();
113
+ }
114
+ const refusal = message?.refusal;
115
+ if (typeof refusal === 'string' && refusal.trim()) {
116
+ return refusal.trim();
117
+ }
118
+ return '';
119
+ }
120
+ function mapToolCall(call) {
121
+ let parsed = {};
122
+ try {
123
+ parsed = JSON.parse(call.function.arguments ?? '{}');
124
+ }
125
+ catch {
126
+ parsed = {};
127
+ }
128
+ return {
129
+ id: call.id ?? call.function.name,
130
+ name: call.function.name,
131
+ arguments: parsed,
132
+ };
133
+ }
134
+ function mapUsage(usage) {
135
+ if (!usage) {
136
+ return null;
137
+ }
138
+ return {
139
+ inputTokens: usage.prompt_tokens,
140
+ outputTokens: usage.completion_tokens,
141
+ totalTokens: usage.total_tokens,
142
+ };
143
+ }
144
+ function assertHasChoices(result) {
145
+ if (!('choices' in result)) {
146
+ throw new Error('Streaming responses are not supported in this runtime.');
147
+ }
148
+ }