mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
🐛 fix: fixed the sandbox tools call when error should use right callback (#11721)
fix: fixed the sandbox tools call when error should use right callback
This commit is contained in:
@@ -65,6 +65,14 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('listLocalFiles', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: { files: [] },
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const files = result.result?.files || [];
|
||||
const state: ListLocalFilesState = { files };
|
||||
|
||||
@@ -90,6 +98,19 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('readLocalFile', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
content: '',
|
||||
endLine: args.endLine,
|
||||
path: args.path,
|
||||
startLine: args.startLine,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: ReadLocalFileState = {
|
||||
content: result.result?.content || '',
|
||||
endLine: args.endLine,
|
||||
@@ -123,6 +144,17 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('writeLocalFile', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
path: args.path,
|
||||
success: false,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: WriteLocalFileState = {
|
||||
bytesWritten: result.result?.bytesWritten,
|
||||
path: args.path,
|
||||
@@ -148,6 +180,17 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('editLocalFile', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
path: args.path,
|
||||
replacements: 0,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: EditLocalFileState = {
|
||||
diffText: result.result?.diffText,
|
||||
linesAdded: result.result?.linesAdded,
|
||||
@@ -177,6 +220,17 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('searchLocalFiles', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
results: [],
|
||||
totalCount: 0,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const results = result.result?.results || [];
|
||||
const state: SearchLocalFilesState = {
|
||||
results,
|
||||
@@ -201,6 +255,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('moveLocalFiles', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
results: [],
|
||||
successCount: 0,
|
||||
totalCount: args.operations.length,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const results = result.result?.results || [];
|
||||
const state: MoveLocalFilesState = {
|
||||
results,
|
||||
@@ -224,6 +290,19 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('renameLocalFile', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
error: result.error?.message,
|
||||
newPath: '',
|
||||
oldPath: args.oldPath,
|
||||
success: false,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: RenameLocalFileState = {
|
||||
error: result.result?.error,
|
||||
newPath: result.result?.newPath || '',
|
||||
@@ -241,7 +320,7 @@ export class CloudSandboxExecutionRuntime {
|
||||
return {
|
||||
content,
|
||||
state,
|
||||
success: result.success,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
return this.handleError(error);
|
||||
@@ -264,15 +343,24 @@ export class CloudSandboxExecutionRuntime {
|
||||
language,
|
||||
output: result.result?.output,
|
||||
stderr: result.result?.stderr,
|
||||
success: result.success,
|
||||
success: result.success || false,
|
||||
};
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state,
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
content: JSON.stringify(result.result),
|
||||
state,
|
||||
success: result.success,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log('executeCode error', error);
|
||||
return this.handleError(error);
|
||||
}
|
||||
}
|
||||
@@ -283,6 +371,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('runCommand', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
error: result.error?.message,
|
||||
isBackground: args.background || false,
|
||||
success: false,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: RunCommandState = {
|
||||
commandId: result.result?.commandId,
|
||||
error: result.result?.error,
|
||||
@@ -296,7 +396,7 @@ export class CloudSandboxExecutionRuntime {
|
||||
return {
|
||||
content: JSON.stringify(result.result),
|
||||
state,
|
||||
success: result.success,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
return this.handleError(error);
|
||||
@@ -307,6 +407,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('getCommandOutput', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
error: result.error?.message,
|
||||
running: false,
|
||||
success: false,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: GetCommandOutputState = {
|
||||
error: result.result?.error,
|
||||
newOutput: result.result?.newOutput,
|
||||
@@ -317,7 +429,7 @@ export class CloudSandboxExecutionRuntime {
|
||||
return {
|
||||
content: JSON.stringify(result.result),
|
||||
state,
|
||||
success: result.success,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
return this.handleError(error);
|
||||
@@ -328,6 +440,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('killCommand', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
commandId: args.commandId,
|
||||
error: result.error?.message,
|
||||
success: false,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: KillCommandState = {
|
||||
commandId: args.commandId,
|
||||
error: result.result?.error,
|
||||
@@ -340,7 +464,7 @@ export class CloudSandboxExecutionRuntime {
|
||||
success: true,
|
||||
}),
|
||||
state,
|
||||
success: result.success,
|
||||
success: true,
|
||||
};
|
||||
} catch (error) {
|
||||
return this.handleError(error);
|
||||
@@ -353,6 +477,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('grepContent', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
matches: [],
|
||||
pattern: args.pattern,
|
||||
totalMatches: 0,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const state: GrepContentState = {
|
||||
matches: result.result?.matches || [],
|
||||
pattern: args.pattern,
|
||||
@@ -373,6 +509,18 @@ export class CloudSandboxExecutionRuntime {
|
||||
try {
|
||||
const result = await this.callTool('globLocalFiles', args);
|
||||
|
||||
if (!result.success) {
|
||||
return {
|
||||
content: result.error?.message || JSON.stringify(result.error),
|
||||
state: {
|
||||
files: [],
|
||||
pattern: args.pattern,
|
||||
totalCount: 0,
|
||||
},
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
const files = result.result?.files || [];
|
||||
const totalCount = result.result?.totalCount || 0;
|
||||
|
||||
@@ -433,7 +581,7 @@ export class CloudSandboxExecutionRuntime {
|
||||
success: false,
|
||||
}),
|
||||
state,
|
||||
success: false,
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -455,13 +603,14 @@ export class CloudSandboxExecutionRuntime {
|
||||
private async callTool(
|
||||
toolName: string,
|
||||
params: Record<string, any>,
|
||||
): Promise<{ result: any; sessionExpiredAndRecreated?: boolean; success: boolean }> {
|
||||
): Promise<{
|
||||
error?: { message: string; name?: string };
|
||||
result: any;
|
||||
sessionExpiredAndRecreated?: boolean;
|
||||
success: boolean;
|
||||
}> {
|
||||
const result = await this.sandboxService.callTool(toolName, params);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error?.message || `Cloud Sandbox tool ${toolName} failed`);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user