centaurus-cli 2.4.0 → 2.5.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/README.md +151 -1
- package/dist/cli-adapter.d.ts +41 -2
- package/dist/cli-adapter.d.ts.map +1 -1
- package/dist/cli-adapter.js +407 -79
- package/dist/cli-adapter.js.map +1 -1
- package/dist/config/types.d.ts +23 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/config/types.js +20 -0
- package/dist/config/types.js.map +1 -1
- package/dist/context/__tests__/command-detector.test.d.ts +14 -0
- package/dist/context/__tests__/command-detector.test.d.ts.map +1 -0
- package/dist/context/__tests__/command-detector.test.js +318 -0
- package/dist/context/__tests__/command-detector.test.js.map +1 -0
- package/dist/context/__tests__/context-manager.test.d.ts +16 -0
- package/dist/context/__tests__/context-manager.test.d.ts.map +1 -0
- package/dist/context/__tests__/context-manager.test.js +375 -0
- package/dist/context/__tests__/context-manager.test.js.map +1 -0
- package/dist/context/__tests__/error-handling.test.d.ts +15 -0
- package/dist/context/__tests__/error-handling.test.d.ts.map +1 -0
- package/dist/context/__tests__/error-handling.test.js +447 -0
- package/dist/context/__tests__/error-handling.test.js.map +1 -0
- package/dist/context/command-detector.d.ts +50 -0
- package/dist/context/command-detector.d.ts.map +1 -0
- package/dist/context/command-detector.js +72 -0
- package/dist/context/command-detector.js.map +1 -0
- package/dist/context/context-manager.d.ts +144 -0
- package/dist/context/context-manager.d.ts.map +1 -0
- package/dist/context/context-manager.js +487 -0
- package/dist/context/context-manager.js.map +1 -0
- package/dist/context/handlers/__tests__/docker-handler.test.d.ts +13 -0
- package/dist/context/handlers/__tests__/docker-handler.test.d.ts.map +1 -0
- package/dist/context/handlers/__tests__/docker-handler.test.js +285 -0
- package/dist/context/handlers/__tests__/docker-handler.test.js.map +1 -0
- package/dist/context/handlers/__tests__/ssh-handler.test.d.ts +13 -0
- package/dist/context/handlers/__tests__/ssh-handler.test.d.ts.map +1 -0
- package/dist/context/handlers/__tests__/ssh-handler.test.js +251 -0
- package/dist/context/handlers/__tests__/ssh-handler.test.js.map +1 -0
- package/dist/context/handlers/__tests__/wsl-handler.test.d.ts +7 -0
- package/dist/context/handlers/__tests__/wsl-handler.test.d.ts.map +1 -0
- package/dist/context/handlers/__tests__/wsl-handler.test.js +331 -0
- package/dist/context/handlers/__tests__/wsl-handler.test.js.map +1 -0
- package/dist/context/handlers/docker-handler.d.ts +111 -0
- package/dist/context/handlers/docker-handler.d.ts.map +1 -0
- package/dist/context/handlers/docker-handler.js +439 -0
- package/dist/context/handlers/docker-handler.js.map +1 -0
- package/dist/context/handlers/ssh-handler.d.ts +120 -0
- package/dist/context/handlers/ssh-handler.d.ts.map +1 -0
- package/dist/context/handlers/ssh-handler.js +523 -0
- package/dist/context/handlers/ssh-handler.js.map +1 -0
- package/dist/context/handlers/wsl-handler.d.ts +128 -0
- package/dist/context/handlers/wsl-handler.d.ts.map +1 -0
- package/dist/context/handlers/wsl-handler.js +590 -0
- package/dist/context/handlers/wsl-handler.js.map +1 -0
- package/dist/context/index.d.ts +8 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +7 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/subshell-handler.d.ts +130 -0
- package/dist/context/subshell-handler.d.ts.map +1 -0
- package/dist/context/subshell-handler.js +5 -0
- package/dist/context/subshell-handler.js.map +1 -0
- package/dist/context/types.d.ts +70 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/context/types.js +34 -0
- package/dist/context/types.js.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/services/__tests__/ai-context-injector.test.d.ts +15 -0
- package/dist/services/__tests__/ai-context-injector.test.d.ts.map +1 -0
- package/dist/services/__tests__/ai-context-injector.test.js +326 -0
- package/dist/services/__tests__/ai-context-injector.test.js.map +1 -0
- package/dist/services/ai-context-injector.d.ts +41 -0
- package/dist/services/ai-context-injector.d.ts.map +1 -0
- package/dist/services/ai-context-injector.js +97 -0
- package/dist/services/ai-context-injector.js.map +1 -0
- package/dist/services/ai-service-client.d.ts +4 -1
- package/dist/services/ai-service-client.d.ts.map +1 -1
- package/dist/services/ai-service-client.js +5 -1
- package/dist/services/ai-service-client.js.map +1 -1
- package/dist/src/context/types.js +27 -0
- package/dist/src/services/ai-context-injector.js +96 -0
- package/dist/src/services/ai-service-client.js +270 -0
- package/dist/src/services/api-client.js +349 -0
- package/dist/src/tools/types.js +1 -0
- package/dist/src/types/index.js +1 -0
- package/dist/test/context/types.js +27 -0
- package/dist/test/services/__tests__/ai-context-injector.test.js +325 -0
- package/dist/test/services/ai-context-injector.js +96 -0
- package/dist/test/services/ai-service-client.js +270 -0
- package/dist/test/services/api-client.js +349 -0
- package/dist/test/tools/types.js +1 -0
- package/dist/test/types/index.js +1 -0
- package/dist/test-ai-context-injector.js +97 -0
- package/dist/test-ssh-handler.d.ts +8 -0
- package/dist/test-ssh-handler.d.ts.map +1 -0
- package/dist/test-ssh-handler.js +198 -0
- package/dist/test-ssh-handler.js.map +1 -0
- package/dist/tools/command.d.ts.map +1 -1
- package/dist/tools/command.js +123 -46
- package/dist/tools/command.js.map +1 -1
- package/dist/tools/file-ops.d.ts.map +1 -1
- package/dist/tools/file-ops.js +115 -48
- package/dist/tools/file-ops.js.map +1 -1
- package/dist/tools/types.d.ts +1 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/types/index.d.ts +41 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/ui/components/App.d.ts +3 -0
- package/dist/ui/components/App.d.ts.map +1 -1
- package/dist/ui/components/App.js +213 -46
- package/dist/ui/components/App.js.map +1 -1
- package/dist/ui/components/Breadcrumbs.d.ts +12 -0
- package/dist/ui/components/Breadcrumbs.d.ts.map +1 -0
- package/dist/ui/components/Breadcrumbs.js +62 -0
- package/dist/ui/components/Breadcrumbs.js.map +1 -0
- package/dist/ui/components/CodeBlock.js +1 -1
- package/dist/ui/components/CodeBlock.js.map +1 -1
- package/dist/ui/components/DiffViewer.js +1 -1
- package/dist/ui/components/DiffViewer.js.map +1 -1
- package/dist/ui/components/FileViewerScreen.d.ts +14 -0
- package/dist/ui/components/FileViewerScreen.d.ts.map +1 -0
- package/dist/ui/components/FileViewerScreen.js +74 -0
- package/dist/ui/components/FileViewerScreen.js.map +1 -0
- package/dist/ui/components/InputBox.d.ts +2 -0
- package/dist/ui/components/InputBox.d.ts.map +1 -1
- package/dist/ui/components/InputBox.js +85 -41
- package/dist/ui/components/InputBox.js.map +1 -1
- package/dist/ui/components/MessageDisplay.d.ts.map +1 -1
- package/dist/ui/components/MessageDisplay.js +3 -28
- package/dist/ui/components/MessageDisplay.js.map +1 -1
- package/dist/ui/components/PasswordPrompt.d.ts +9 -0
- package/dist/ui/components/PasswordPrompt.d.ts.map +1 -0
- package/dist/ui/components/PasswordPrompt.js +20 -0
- package/dist/ui/components/PasswordPrompt.js.map +1 -0
- package/dist/ui/components/StatusBar.d.ts +2 -0
- package/dist/ui/components/StatusBar.d.ts.map +1 -1
- package/dist/ui/components/StatusBar.js +36 -1
- package/dist/ui/components/StatusBar.js.map +1 -1
- package/dist/ui/components/ToolExecutionMessage.d.ts.map +1 -1
- package/dist/ui/components/ToolExecutionMessage.js +13 -24
- package/dist/ui/components/ToolExecutionMessage.js.map +1 -1
- package/dist/ui/components/VersionUpdatePrompt.d.ts +10 -0
- package/dist/ui/components/VersionUpdatePrompt.d.ts.map +1 -0
- package/dist/ui/components/VersionUpdatePrompt.js +41 -0
- package/dist/ui/components/VersionUpdatePrompt.js.map +1 -0
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +38 -10
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/version-checker.d.ts +14 -0
- package/dist/utils/version-checker.d.ts.map +1 -0
- package/dist/utils/version-checker.js +63 -0
- package/dist/utils/version-checker.js.map +1 -0
- package/package.json +71 -69
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Context Injector
|
|
3
|
+
*
|
|
4
|
+
* Injects subshell context into AI message history to make the AI aware
|
|
5
|
+
* of the current execution environment (SSH, WSL, Docker, etc.)
|
|
6
|
+
*/
|
|
7
|
+
import type { Message } from './ai-service-client.js';
|
|
8
|
+
import type { SubshellContext } from '../context/types.js';
|
|
9
|
+
/**
|
|
10
|
+
* AI Context Injector class
|
|
11
|
+
* Handles injection of subshell context into message history before AI calls
|
|
12
|
+
*/
|
|
13
|
+
export declare class AIContextInjector {
|
|
14
|
+
/**
|
|
15
|
+
* Inject subshell context into message history
|
|
16
|
+
*
|
|
17
|
+
* If the current context is a subshell (not local), this method inserts
|
|
18
|
+
* a system message describing the environment before the last user message.
|
|
19
|
+
* This ensures the AI is aware of where commands will execute.
|
|
20
|
+
*
|
|
21
|
+
* @param messages - The conversation history
|
|
22
|
+
* @param context - The current subshell context
|
|
23
|
+
* @returns Modified message array with context injected (if applicable)
|
|
24
|
+
*/
|
|
25
|
+
injectSubshellContext(messages: Message[], context: SubshellContext): Message[];
|
|
26
|
+
/**
|
|
27
|
+
* Build context message describing the current subshell environment
|
|
28
|
+
*
|
|
29
|
+
* Creates a formatted message that informs the AI about:
|
|
30
|
+
* - Environment type (SSH, WSL, Docker)
|
|
31
|
+
* - Working directory
|
|
32
|
+
* - Shell type
|
|
33
|
+
* - Operating system
|
|
34
|
+
* - Connection details (hostname, username, etc.)
|
|
35
|
+
*
|
|
36
|
+
* @param context - The current subshell context
|
|
37
|
+
* @returns Formatted context message string
|
|
38
|
+
*/
|
|
39
|
+
private buildContextMessage;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=ai-context-injector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-context-injector.d.ts","sourceRoot":"","sources":["../../src/services/ai-context-injector.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B;;;;;;;;;;OAUG;IACH,qBAAqB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,EAAE;IAoC/E;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,mBAAmB;CAiC5B"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Context Injector
|
|
3
|
+
*
|
|
4
|
+
* Injects subshell context into AI message history to make the AI aware
|
|
5
|
+
* of the current execution environment (SSH, WSL, Docker, etc.)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* AI Context Injector class
|
|
9
|
+
* Handles injection of subshell context into message history before AI calls
|
|
10
|
+
*/
|
|
11
|
+
export class AIContextInjector {
|
|
12
|
+
/**
|
|
13
|
+
* Inject subshell context into message history
|
|
14
|
+
*
|
|
15
|
+
* If the current context is a subshell (not local), this method inserts
|
|
16
|
+
* a system message describing the environment before the last user message.
|
|
17
|
+
* This ensures the AI is aware of where commands will execute.
|
|
18
|
+
*
|
|
19
|
+
* @param messages - The conversation history
|
|
20
|
+
* @param context - The current subshell context
|
|
21
|
+
* @returns Modified message array with context injected (if applicable)
|
|
22
|
+
*/
|
|
23
|
+
injectSubshellContext(messages, context) {
|
|
24
|
+
// Don't inject context for local environment
|
|
25
|
+
if (context.type === 'local') {
|
|
26
|
+
return messages;
|
|
27
|
+
}
|
|
28
|
+
// Build context message
|
|
29
|
+
const contextMessage = {
|
|
30
|
+
role: 'system',
|
|
31
|
+
content: this.buildContextMessage(context),
|
|
32
|
+
};
|
|
33
|
+
// Insert context message before the last user message
|
|
34
|
+
// This ensures the AI sees the context right before processing the request
|
|
35
|
+
const result = [...messages];
|
|
36
|
+
// Find the last user message index
|
|
37
|
+
let lastUserMessageIndex = -1;
|
|
38
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
39
|
+
if (result[i].role === 'user') {
|
|
40
|
+
lastUserMessageIndex = i;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// If we found a user message, insert context before it
|
|
45
|
+
// Otherwise, append to the end
|
|
46
|
+
if (lastUserMessageIndex >= 0) {
|
|
47
|
+
result.splice(lastUserMessageIndex, 0, contextMessage);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
result.push(contextMessage);
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build context message describing the current subshell environment
|
|
56
|
+
*
|
|
57
|
+
* Creates a formatted message that informs the AI about:
|
|
58
|
+
* - Environment type (SSH, WSL, Docker)
|
|
59
|
+
* - Working directory
|
|
60
|
+
* - Shell type
|
|
61
|
+
* - Operating system
|
|
62
|
+
* - Connection details (hostname, username, etc.)
|
|
63
|
+
*
|
|
64
|
+
* @param context - The current subshell context
|
|
65
|
+
* @returns Formatted context message string
|
|
66
|
+
*/
|
|
67
|
+
buildContextMessage(context) {
|
|
68
|
+
const { type, metadata } = context;
|
|
69
|
+
let message = `\n\n## CURRENT EXECUTION ENVIRONMENT\n\n`;
|
|
70
|
+
message += `You are currently operating in a ${type.toUpperCase()} environment.\n\n`;
|
|
71
|
+
message += `**Environment Details:**\n`;
|
|
72
|
+
message += `- Type: ${type}\n`;
|
|
73
|
+
message += `- Working Directory: ${metadata.workingDirectory}\n`;
|
|
74
|
+
message += `- Shell: ${metadata.shell}\n`;
|
|
75
|
+
message += `- OS: ${metadata.os}\n`;
|
|
76
|
+
// Add type-specific details
|
|
77
|
+
if (metadata.hostname) {
|
|
78
|
+
message += `- Hostname: ${metadata.hostname}\n`;
|
|
79
|
+
}
|
|
80
|
+
if (metadata.username) {
|
|
81
|
+
message += `- Username: ${metadata.username}\n`;
|
|
82
|
+
}
|
|
83
|
+
if (metadata.distroName) {
|
|
84
|
+
message += `- Distribution: ${metadata.distroName}\n`;
|
|
85
|
+
}
|
|
86
|
+
if (metadata.containerId) {
|
|
87
|
+
message += `- Container: ${metadata.containerId}\n`;
|
|
88
|
+
}
|
|
89
|
+
if (metadata.port) {
|
|
90
|
+
message += `- Port: ${metadata.port}\n`;
|
|
91
|
+
}
|
|
92
|
+
message += `\n**IMPORTANT:** All commands and file operations you execute will run in this ${type} environment, not on the local machine.\n`;
|
|
93
|
+
message += `When reading files, writing files, executing commands, or performing any operations, they will all happen in the ${type} environment.\n`;
|
|
94
|
+
return message;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=ai-context-injector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-context-injector.js","sourceRoot":"","sources":["../../src/services/ai-context-injector.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAC5B;;;;;;;;;;OAUG;IACH,qBAAqB,CAAC,QAAmB,EAAE,OAAwB;QACjE,6CAA6C;QAC7C,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAC7B,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,wBAAwB;QACxB,MAAM,cAAc,GAAY;YAC9B,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;SAC3C,CAAC;QAEF,sDAAsD;QACtD,2EAA2E;QAC3E,MAAM,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;QAE7B,mCAAmC;QACnC,IAAI,oBAAoB,GAAG,CAAC,CAAC,CAAC;QAC9B,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5C,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC9B,oBAAoB,GAAG,CAAC,CAAC;gBACzB,MAAM;YACR,CAAC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C,IAAI,EAAE,CAAC;YAClB,OAAO,IAAI,WAAW,QAAQ,CAAC,IAAI,IAAI,CAAC;QAC1C,CAAC;QAED,OAAO,IAAI,kFAAkF,IAAI,2CAA2C,CAAC;QAC7I,OAAO,IAAI,oHAAoH,IAAI,iBAAiB,CAAC;QAErJ,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* AI chat requests. Replaces direct Gemini SDK usage in the CLI.
|
|
6
6
|
*/
|
|
7
7
|
import type { ToolSchema } from '../tools/types.js';
|
|
8
|
+
import type { EnvironmentContext } from '../types/index.js';
|
|
8
9
|
/**
|
|
9
10
|
* Message format for AI chat requests
|
|
10
11
|
*/
|
|
@@ -59,9 +60,11 @@ export declare class AIServiceClient {
|
|
|
59
60
|
* @param model - The AI model to use (e.g., 'gemini-2.5-flash')
|
|
60
61
|
* @param messages - Conversation history including system, user, assistant, and tool messages
|
|
61
62
|
* @param tools - Available tool schemas for the AI to use
|
|
63
|
+
* @param environmentContext - Optional environment context (OS, shell, cwd, etc.)
|
|
64
|
+
* @param mode - Optional mode (default, plan, command)
|
|
62
65
|
* @yields Stream chunks (text, tool calls, done, or error events)
|
|
63
66
|
*/
|
|
64
|
-
streamChat(model: string, messages: Message[], tools: ToolSchema[]): AsyncGenerator<StreamChunk, void, unknown>;
|
|
67
|
+
streamChat(model: string, messages: Message[], tools: ToolSchema[], environmentContext?: EnvironmentContext, mode?: string): AsyncGenerator<StreamChunk, void, unknown>;
|
|
65
68
|
/**
|
|
66
69
|
* Check if an error code is retryable
|
|
67
70
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-service-client.d.ts","sourceRoot":"","sources":["../../src/services/ai-service-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"ai-service-client.d.ts","sourceRoot":"","sources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ACvC,IAAI,CAAC,EAAE,MAAM,GACZ,cAAc,CAAC,WAAW,EAAE,IAAI,EAAE,OAAO,CAAC;IAwJ7C;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAKxB;;OAEG;IACH,OAAO,CAAC,KAAK;IAIb;;;OAGG;IACH,OAAO,CAAC,eAAe;IAiBvB;;;;;OAKG;YACY,cAAc;CAuD9B;AAGD,eAAO,MAAM,eAAe,iBAAwB,CAAC"}
|
|
@@ -39,15 +39,19 @@ export class AIServiceClient {
|
|
|
39
39
|
* @param model - The AI model to use (e.g., 'gemini-2.5-flash')
|
|
40
40
|
* @param messages - Conversation history including system, user, assistant, and tool messages
|
|
41
41
|
* @param tools - Available tool schemas for the AI to use
|
|
42
|
+
* @param environmentContext - Optional environment context (OS, shell, cwd, etc.)
|
|
43
|
+
* @param mode - Optional mode (default, plan, command)
|
|
42
44
|
* @yields Stream chunks (text, tool calls, done, or error events)
|
|
43
45
|
*/
|
|
44
|
-
async *streamChat(model, messages, tools) {
|
|
46
|
+
async *streamChat(model, messages, tools, environmentContext, mode) {
|
|
45
47
|
// Build request payload
|
|
46
48
|
const payload = {
|
|
47
49
|
model,
|
|
48
50
|
messages,
|
|
49
51
|
tools,
|
|
50
52
|
stream: true,
|
|
53
|
+
environmentContext,
|
|
54
|
+
mode,
|
|
51
55
|
};
|
|
52
56
|
// Get authentication token from api client
|
|
53
57
|
if (!apiClient.isAuthenticated()) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-service-client.js","sourceRoot":"","sources":["../../src/services/ai-service-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"ai-service-client.js","sourceRoot":"","sources":["../../src/services/ai-service-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,Y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AAC,GAAG,EAAE,IAAI,EAAE,CAAC;gBAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;oBACzB,6BAA6B;oBAC7B,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;wBAC9B,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,yBAAyB;wBAExD,wBAAwB;wBACxB,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;4BACpB,SAAS;wBACX,CAAC;wBAED,IAAI,CAAC;4BACH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAgB,CAAC;4BACjD,MAAM,KAAK,CAAC;4BAEZ,2CAA2C;4BAC3C,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;gCACpD,OAAO;4BACT,CAAC;wBACH,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,sBAAsB;4BACtB,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,OAAO,CAAC,CAAC;wBACtD,CAAC;oBACH,CAAC;oBACD,sCAAsC;oBACtC,6EAA6E;gBAC/E,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;CACF;AAED,4BAA4B;AAC5B,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for the subshell context management system
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a subshell connection fails
|
|
6
|
+
*/
|
|
7
|
+
export class SubshellConnectionError extends Error {
|
|
8
|
+
constructor(type, reason, recoverable) {
|
|
9
|
+
super(`${type} connection failed: ${reason}`);
|
|
10
|
+
this.type = type;
|
|
11
|
+
this.reason = reason;
|
|
12
|
+
this.recoverable = recoverable;
|
|
13
|
+
this.name = 'SubshellConnectionError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown when a subshell command execution fails
|
|
18
|
+
*/
|
|
19
|
+
export class SubshellExecutionError extends Error {
|
|
20
|
+
constructor(command, reason, exitCode) {
|
|
21
|
+
super(`Command execution failed: ${reason}`);
|
|
22
|
+
this.command = command;
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
this.exitCode = exitCode;
|
|
25
|
+
this.name = 'SubshellExecutionError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Context Injector
|
|
3
|
+
*
|
|
4
|
+
* Injects subshell context into AI message history to make the AI aware
|
|
5
|
+
* of the current execution environment (SSH, WSL, Docker, etc.)
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* AI Context Injector class
|
|
9
|
+
* Handles injection of subshell context into message history before AI calls
|
|
10
|
+
*/
|
|
11
|
+
export class AIContextInjector {
|
|
12
|
+
/**
|
|
13
|
+
* Inject subshell context into message history
|
|
14
|
+
*
|
|
15
|
+
* If the current context is a subshell (not local), this method inserts
|
|
16
|
+
* a system message describing the environment before the last user message.
|
|
17
|
+
* This ensures the AI is aware of where commands will execute.
|
|
18
|
+
*
|
|
19
|
+
* @param messages - The conversation history
|
|
20
|
+
* @param context - The current subshell context
|
|
21
|
+
* @returns Modified message array with context injected (if applicable)
|
|
22
|
+
*/
|
|
23
|
+
injectSubshellContext(messages, context) {
|
|
24
|
+
// Don't inject context for local environment
|
|
25
|
+
if (context.type === 'local') {
|
|
26
|
+
return messages;
|
|
27
|
+
}
|
|
28
|
+
// Build context message
|
|
29
|
+
const contextMessage = {
|
|
30
|
+
role: 'system',
|
|
31
|
+
content: this.buildContextMessage(context),
|
|
32
|
+
};
|
|
33
|
+
// Insert context message before the last user message
|
|
34
|
+
// This ensures the AI sees the context right before processing the request
|
|
35
|
+
const result = [...messages];
|
|
36
|
+
// Find the last user message index
|
|
37
|
+
let lastUserMessageIndex = -1;
|
|
38
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
39
|
+
if (result[i].role === 'user') {
|
|
40
|
+
lastUserMessageIndex = i;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// If we found a user message, insert context before it
|
|
45
|
+
// Otherwise, append to the end
|
|
46
|
+
if (lastUserMessageIndex >= 0) {
|
|
47
|
+
result.splice(lastUserMessageIndex, 0, contextMessage);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
result.push(contextMessage);
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build context message describing the current subshell environment
|
|
56
|
+
*
|
|
57
|
+
* Creates a formatted message that informs the AI about:
|
|
58
|
+
* - Environment type (SSH, WSL, Docker)
|
|
59
|
+
* - Working directory
|
|
60
|
+
* - Shell type
|
|
61
|
+
* - Operating system
|
|
62
|
+
* - Connection details (hostname, username, etc.)
|
|
63
|
+
*
|
|
64
|
+
* @param context - The current subshell context
|
|
65
|
+
* @returns Formatted context message string
|
|
66
|
+
*/
|
|
67
|
+
buildContextMessage(context) {
|
|
68
|
+
const { type, metadata } = context;
|
|
69
|
+
let message = `\n\n## CURRENT EXECUTION ENVIRONMENT\n\n`;
|
|
70
|
+
message += `You are currently operating in a ${type.toUpperCase()} environment.\n\n`;
|
|
71
|
+
message += `**Environment Details:**\n`;
|
|
72
|
+
message += `- Type: ${type}\n`;
|
|
73
|
+
message += `- Working Directory: ${metadata.workingDirectory}\n`;
|
|
74
|
+
message += `- Shell: ${metadata.shell}\n`;
|
|
75
|
+
message += `- OS: ${metadata.os}\n`;
|
|
76
|
+
// Add type-specific details
|
|
77
|
+
if (metadata.hostname) {
|
|
78
|
+
message += `- Hostname: ${metadata.hostname}\n`;
|
|
79
|
+
}
|
|
80
|
+
if (metadata.username) {
|
|
81
|
+
message += `- Username: ${metadata.username}\n`;
|
|
82
|
+
}
|
|
83
|
+
if (metadata.distroName) {
|
|
84
|
+
message += `- Distribution: ${metadata.distroName}\n`;
|
|
85
|
+
}
|
|
86
|
+
if (metadata.containerId) {
|
|
87
|
+
message += `- Container: ${metadata.containerId}\n`;
|
|
88
|
+
}
|
|
89
|
+
if (metadata.port) {
|
|
90
|
+
message += `- Port: ${metadata.port}\n`;
|
|
91
|
+
}
|
|
92
|
+
message += `\n**IMPORTANT:** All commands and file operations you execute will run in this ${type} environment, not on the local machine.\n`;
|
|
93
|
+
message += `When reading files, writing files, executing commands, or performing any operations, they will all happen in the ${type} environment.\n`;
|
|
94
|
+
return message;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Service Client
|
|
3
|
+
*
|
|
4
|
+
* Handles communication with the backend AI proxy service for streaming
|
|
5
|
+
* AI chat requests. Replaces direct Gemini SDK usage in the CLI.
|
|
6
|
+
*/
|
|
7
|
+
import { apiClient } from './api-client.js';
|
|
8
|
+
import { readFileSync, existsSync } from 'fs';
|
|
9
|
+
import { join } from 'path';
|
|
10
|
+
import { homedir } from 'os';
|
|
11
|
+
/**
|
|
12
|
+
* AI Service Client for streaming chat requests to backend
|
|
13
|
+
*/
|
|
14
|
+
export class AIServiceClient {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.maxRetries = 3;
|
|
17
|
+
this.retryDelay = 1000; // Start with 1 second
|
|
18
|
+
// Don't set baseURL yet - lazy load it when first used
|
|
19
|
+
// This allows environment variables to be loaded first
|
|
20
|
+
this.baseURL = '';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the base URL for API requests
|
|
24
|
+
* Lazy-loaded to ensure environment variables are loaded first
|
|
25
|
+
*/
|
|
26
|
+
getBaseURL() {
|
|
27
|
+
if (!this.baseURL) {
|
|
28
|
+
// Use production URL by default, only use localhost in development mode
|
|
29
|
+
this.baseURL = process.env.DEV_MODE === 'true'
|
|
30
|
+
? 'http://localhost:3002/api'
|
|
31
|
+
: (process.env.BACKEND_URL || 'https://centaurus-backend-354715948975.asia-south1.run.app/api');
|
|
32
|
+
}
|
|
33
|
+
return this.baseURL;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Stream chat request to backend AI proxy
|
|
37
|
+
*
|
|
38
|
+
* @param model - The AI model to use (e.g., 'gemini-2.5-flash')
|
|
39
|
+
* @param messages - Conversation history including system, user, assistant, and tool messages
|
|
40
|
+
* @param tools - Available tool schemas for the AI to use
|
|
41
|
+
* @param environmentContext - Optional environment context (OS, shell, cwd, etc.)
|
|
42
|
+
* @param mode - Optional mode (default, plan, command)
|
|
43
|
+
* @yields Stream chunks (text, tool calls, done, or error events)
|
|
44
|
+
*/
|
|
45
|
+
async *streamChat(model, messages, tools, environmentContext, mode) {
|
|
46
|
+
// Build request payload
|
|
47
|
+
const payload = {
|
|
48
|
+
model,
|
|
49
|
+
messages,
|
|
50
|
+
tools,
|
|
51
|
+
stream: true,
|
|
52
|
+
environmentContext,
|
|
53
|
+
mode,
|
|
54
|
+
};
|
|
55
|
+
// Get authentication token from api client
|
|
56
|
+
if (!apiClient.isAuthenticated()) {
|
|
57
|
+
yield {
|
|
58
|
+
type: 'error',
|
|
59
|
+
message: 'Authentication required. Please sign in.',
|
|
60
|
+
code: 'AUTH_REQUIRED',
|
|
61
|
+
};
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
// Retry logic for transient errors
|
|
65
|
+
let lastError = null;
|
|
66
|
+
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
|
|
67
|
+
try {
|
|
68
|
+
// Make fetch request to backend AI endpoint
|
|
69
|
+
const response = await fetch(`${this.getBaseURL()}/ai/chat`, {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/json',
|
|
73
|
+
'Authorization': `Bearer ${this.getSessionToken()}`,
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify(payload),
|
|
76
|
+
signal: AbortSignal.timeout(60000), // 60 second timeout
|
|
77
|
+
});
|
|
78
|
+
// Check for HTTP errors
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
const errorText = await response.text();
|
|
81
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
82
|
+
let errorCode = 'HTTP_ERROR';
|
|
83
|
+
try {
|
|
84
|
+
const errorData = JSON.parse(errorText);
|
|
85
|
+
if (errorData.error) {
|
|
86
|
+
errorMessage = errorData.error.message || errorMessage;
|
|
87
|
+
errorCode = errorData.error.code || errorCode;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// If response is not JSON, use the text as message
|
|
92
|
+
if (errorText) {
|
|
93
|
+
errorMessage = errorText;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Handle specific error codes
|
|
97
|
+
if (response.status === 401) {
|
|
98
|
+
yield {
|
|
99
|
+
type: 'error',
|
|
100
|
+
message: 'Session expired. Please sign in again.',
|
|
101
|
+
code: 'AUTH_REQUIRED',
|
|
102
|
+
};
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (response.status === 429) {
|
|
106
|
+
// Rate limit - don't retry immediately
|
|
107
|
+
yield {
|
|
108
|
+
type: 'error',
|
|
109
|
+
message: 'Service temporarily unavailable due to high demand. Please try again in a moment.',
|
|
110
|
+
code: 'RATE_LIMIT',
|
|
111
|
+
};
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (response.status === 504) {
|
|
115
|
+
// Timeout error - retryable
|
|
116
|
+
lastError = {
|
|
117
|
+
type: 'error',
|
|
118
|
+
message: 'Request timed out. Retrying...',
|
|
119
|
+
code: 'TIMEOUT',
|
|
120
|
+
};
|
|
121
|
+
if (attempt < this.maxRetries - 1) {
|
|
122
|
+
await this.sleep(this.retryDelay * Math.pow(2, attempt));
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// For other errors, yield and return
|
|
127
|
+
yield {
|
|
128
|
+
type: 'error',
|
|
129
|
+
message: errorMessage,
|
|
130
|
+
code: errorCode,
|
|
131
|
+
};
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// Check if response body exists
|
|
135
|
+
if (!response.body) {
|
|
136
|
+
yield {
|
|
137
|
+
type: 'error',
|
|
138
|
+
message: 'No response body from backend',
|
|
139
|
+
code: 'NO_RESPONSE_BODY',
|
|
140
|
+
};
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Parse SSE stream - if successful, we're done
|
|
144
|
+
yield* this.parseSSEStream(response.body);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
// Handle network errors
|
|
149
|
+
if (error.name === 'TypeError' && error.message.includes('fetch')) {
|
|
150
|
+
lastError = {
|
|
151
|
+
type: 'error',
|
|
152
|
+
message: 'Backend service is unreachable. Please check your connection.',
|
|
153
|
+
code: 'NETWORK_ERROR',
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
else if (error.name === 'AbortError' || error.name === 'TimeoutError') {
|
|
157
|
+
lastError = {
|
|
158
|
+
type: 'error',
|
|
159
|
+
message: 'Request timed out. Please try again.',
|
|
160
|
+
code: 'TIMEOUT',
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
lastError = {
|
|
165
|
+
type: 'error',
|
|
166
|
+
message: error.message || 'Unknown error occurred',
|
|
167
|
+
code: error.code || 'UNKNOWN_ERROR',
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
// Retry for transient errors
|
|
171
|
+
if (this.isRetryableError(lastError.code) && attempt < this.maxRetries - 1) {
|
|
172
|
+
await this.sleep(this.retryDelay * Math.pow(2, attempt));
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
// If not retryable or max retries reached, yield error and return
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// If we get here, we've exhausted retries
|
|
180
|
+
if (lastError) {
|
|
181
|
+
yield lastError;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Check if an error code is retryable
|
|
186
|
+
*/
|
|
187
|
+
isRetryableError(code) {
|
|
188
|
+
const retryableCodes = ['NETWORK_ERROR', 'TIMEOUT', 'UNKNOWN_ERROR'];
|
|
189
|
+
return retryableCodes.includes(code);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Sleep for specified milliseconds
|
|
193
|
+
*/
|
|
194
|
+
sleep(ms) {
|
|
195
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get session token from apiClient
|
|
199
|
+
* This is a workaround since sessionToken is private
|
|
200
|
+
*/
|
|
201
|
+
getSessionToken() {
|
|
202
|
+
// Read session token from the same location apiClient uses
|
|
203
|
+
const configPath = join(homedir(), '.centaurus', 'session.json');
|
|
204
|
+
try {
|
|
205
|
+
if (existsSync(configPath)) {
|
|
206
|
+
const data = readFileSync(configPath, 'utf-8');
|
|
207
|
+
const session = JSON.parse(data);
|
|
208
|
+
return session.sessionToken || '';
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
// Return empty string if unable to read
|
|
213
|
+
}
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Parse Server-Sent Events stream from response body
|
|
218
|
+
*
|
|
219
|
+
* @param body - ReadableStream from fetch response
|
|
220
|
+
* @yields Parsed stream chunks
|
|
221
|
+
*/
|
|
222
|
+
async *parseSSEStream(body) {
|
|
223
|
+
const reader = body.getReader();
|
|
224
|
+
const decoder = new TextDecoder();
|
|
225
|
+
let buffer = '';
|
|
226
|
+
try {
|
|
227
|
+
while (true) {
|
|
228
|
+
const { done, value } = await reader.read();
|
|
229
|
+
if (done) {
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
// Decode chunk and add to buffer
|
|
233
|
+
buffer += decoder.decode(value, { stream: true });
|
|
234
|
+
// Process complete lines in buffer
|
|
235
|
+
const lines = buffer.split('\n');
|
|
236
|
+
// Keep the last incomplete line in buffer
|
|
237
|
+
buffer = lines.pop() || '';
|
|
238
|
+
for (const line of lines) {
|
|
239
|
+
// SSE format: "data: {json}"
|
|
240
|
+
if (line.startsWith('data: ')) {
|
|
241
|
+
const dataStr = line.slice(6); // Remove "data: " prefix
|
|
242
|
+
// Skip empty data lines
|
|
243
|
+
if (!dataStr.trim()) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
try {
|
|
247
|
+
const chunk = JSON.parse(dataStr);
|
|
248
|
+
yield chunk;
|
|
249
|
+
// Stop if we receive a done or error event
|
|
250
|
+
if (chunk.type === 'done' || chunk.type === 'error') {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
// Skip malformed JSON
|
|
256
|
+
console.error('Failed to parse SSE data:', dataStr);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// SSE event type line: "event: chunk"
|
|
260
|
+
// We don't need to process these separately since the data contains the type
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
reader.releaseLock();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Export singleton instance
|
|
270
|
+
export const aiServiceClient = new AIServiceClient();
|