askui 0.24.1 → 0.25.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/dist/cjs/core/models/anthropic/askui-agent.d.ts +13 -0
- package/dist/cjs/core/models/anthropic/askui-agent.js +195 -0
- package/dist/cjs/core/models/anthropic/claude-agent.d.ts +40 -0
- package/dist/cjs/core/models/anthropic/claude-agent.js +200 -0
- package/dist/cjs/core/models/anthropic/index.d.ts +4 -0
- package/dist/cjs/core/models/anthropic/index.js +9 -0
- package/dist/cjs/core/models/anthropic/tools/agent-errors.d.ts +2 -0
- package/dist/cjs/core/models/anthropic/tools/agent-errors.js +6 -0
- package/dist/cjs/core/models/anthropic/tools/base.d.ts +24 -0
- package/dist/cjs/core/models/anthropic/tools/base.js +66 -0
- package/dist/cjs/core/models/anthropic/tools/os-agent-tools.d.ts +113 -0
- package/dist/cjs/core/models/anthropic/tools/os-agent-tools.js +476 -0
- package/dist/cjs/core/ui-control-commands/index.d.ts +2 -0
- package/dist/cjs/core/ui-control-commands/index.js +5 -1
- package/dist/cjs/execution/dsl.d.ts +7 -4
- package/dist/cjs/execution/dsl.js +4 -1
- package/dist/cjs/execution/execution-runtime.d.ts +11 -1
- package/dist/cjs/execution/execution-runtime.js +5 -0
- package/dist/cjs/execution/inference-client.d.ts +9 -0
- package/dist/cjs/execution/inference-client.js +13 -5
- package/dist/cjs/execution/ui-control-client-dependency-builder.js +1 -1
- package/dist/cjs/execution/ui-control-client.d.ts +50 -0
- package/dist/cjs/execution/ui-control-client.js +61 -3
- package/dist/cjs/lib/interactive_cli/create-example-project.d.ts +1 -0
- package/dist/cjs/lib/interactive_cli/create-example-project.js +20 -3
- package/dist/cjs/main.d.ts +1 -0
- package/dist/cjs/main.js +5 -1
- package/dist/cjs/utils/base_64_image/base-64-image.d.ts +2 -1
- package/dist/cjs/utils/base_64_image/base-64-image.js +17 -2
- package/dist/esm/core/models/anthropic/askui-agent.d.ts +13 -0
- package/dist/esm/core/models/anthropic/askui-agent.js +191 -0
- package/dist/esm/core/models/anthropic/claude-agent.d.ts +40 -0
- package/dist/esm/core/models/anthropic/claude-agent.js +196 -0
- package/dist/esm/core/models/anthropic/index.d.ts +4 -0
- package/dist/esm/core/models/anthropic/index.js +2 -0
- package/dist/esm/core/models/anthropic/tools/agent-errors.d.ts +2 -0
- package/dist/esm/core/models/anthropic/tools/agent-errors.js +2 -0
- package/dist/esm/core/models/anthropic/tools/base.d.ts +24 -0
- package/dist/esm/core/models/anthropic/tools/base.js +59 -0
- package/dist/esm/core/models/anthropic/tools/os-agent-tools.d.ts +113 -0
- package/dist/esm/core/models/anthropic/tools/os-agent-tools.js +461 -0
- package/dist/esm/core/ui-control-commands/index.d.ts +2 -0
- package/dist/esm/core/ui-control-commands/index.js +2 -0
- package/dist/esm/execution/dsl.d.ts +7 -4
- package/dist/esm/execution/dsl.js +3 -0
- package/dist/esm/execution/execution-runtime.d.ts +11 -1
- package/dist/esm/execution/execution-runtime.js +5 -0
- package/dist/esm/execution/inference-client.d.ts +9 -0
- package/dist/esm/execution/inference-client.js +13 -5
- package/dist/esm/execution/ui-control-client-dependency-builder.js +1 -1
- package/dist/esm/execution/ui-control-client.d.ts +50 -0
- package/dist/esm/execution/ui-control-client.js +61 -3
- package/dist/esm/lib/interactive_cli/create-example-project.d.ts +1 -0
- package/dist/esm/lib/interactive_cli/create-example-project.js +20 -3
- package/dist/esm/main.d.ts +1 -0
- package/dist/esm/main.js +1 -0
- package/dist/esm/utils/base_64_image/base-64-image.d.ts +2 -1
- package/dist/esm/utils/base_64_image/base-64-image.js +17 -2
- package/dist/example_projects_templates/configs/vscode-settings.json +41 -0
- package/package.json +2 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ClaudeAgent } from './claude-agent';
|
|
2
|
+
import { ExecutionRuntime } from '../../../execution/execution-runtime';
|
|
3
|
+
export declare class AskUIAgent extends ClaudeAgent {
|
|
4
|
+
private osAgentHandler;
|
|
5
|
+
private executionRuntime;
|
|
6
|
+
constructor(executionRuntime: ExecutionRuntime);
|
|
7
|
+
isConnected(): boolean;
|
|
8
|
+
initializeOsAgentHandler(): Promise<void>;
|
|
9
|
+
configureAsDesktopAgent(): Promise<void>;
|
|
10
|
+
configureAsAndroidAgent(): Promise<void>;
|
|
11
|
+
private static DesktopSystemPrompt;
|
|
12
|
+
private static AndroidSystemPrompt;
|
|
13
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AskUIAgent = void 0;
|
|
13
|
+
const os_agent_tools_1 = require("./tools/os-agent-tools");
|
|
14
|
+
const claude_agent_1 = require("./claude-agent");
|
|
15
|
+
class AskUIAgent extends claude_agent_1.ClaudeAgent {
|
|
16
|
+
constructor(executionRuntime) {
|
|
17
|
+
super((params) => executionRuntime.predictActResponse(params));
|
|
18
|
+
this.osAgentHandler = undefined;
|
|
19
|
+
this.executionRuntime = executionRuntime;
|
|
20
|
+
}
|
|
21
|
+
isConnected() {
|
|
22
|
+
return this.osAgentHandler !== undefined;
|
|
23
|
+
}
|
|
24
|
+
initializeOsAgentHandler() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
this.osAgentHandler = yield os_agent_tools_1.OsAgentHandler.createInstance(this.executionRuntime);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
configureAsDesktopAgent() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
if (!this.osAgentHandler) {
|
|
32
|
+
throw new Error('Agent OS client is not connected');
|
|
33
|
+
}
|
|
34
|
+
const tools = [
|
|
35
|
+
new os_agent_tools_1.AgentErrorTool(),
|
|
36
|
+
new os_agent_tools_1.ScreenShotTool(this.osAgentHandler),
|
|
37
|
+
new os_agent_tools_1.MouseMoveTool(this.osAgentHandler),
|
|
38
|
+
new os_agent_tools_1.MouseClickTool(this.osAgentHandler),
|
|
39
|
+
new os_agent_tools_1.MouseScrollTool(this.osAgentHandler),
|
|
40
|
+
new os_agent_tools_1.DesktopKeyPressSequenceTool(this.osAgentHandler),
|
|
41
|
+
new os_agent_tools_1.DesktopSingleKeyPressTool(this.osAgentHandler),
|
|
42
|
+
new os_agent_tools_1.TypeTool(this.osAgentHandler),
|
|
43
|
+
];
|
|
44
|
+
this.setTools(tools);
|
|
45
|
+
this.setSystemPrompt(AskUIAgent.DesktopSystemPrompt);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
configureAsAndroidAgent() {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
if (!this.osAgentHandler) {
|
|
51
|
+
throw new Error('Agent OS client is not connected');
|
|
52
|
+
}
|
|
53
|
+
const tools = [
|
|
54
|
+
new os_agent_tools_1.AgentErrorTool(),
|
|
55
|
+
new os_agent_tools_1.ScreenShotTool(this.osAgentHandler),
|
|
56
|
+
new os_agent_tools_1.MouseMoveTool(this.osAgentHandler),
|
|
57
|
+
new os_agent_tools_1.MouseClickTool(this.osAgentHandler),
|
|
58
|
+
new os_agent_tools_1.MouseScrollTool(this.osAgentHandler),
|
|
59
|
+
new os_agent_tools_1.AndroidSingleKeyPressTool(this.osAgentHandler),
|
|
60
|
+
new os_agent_tools_1.AndroidSequenceKeyPressTool(this.osAgentHandler),
|
|
61
|
+
new os_agent_tools_1.TypeTool(this.osAgentHandler),
|
|
62
|
+
new os_agent_tools_1.ExecuteShellCommandTool(this.osAgentHandler),
|
|
63
|
+
];
|
|
64
|
+
this.setTools(tools);
|
|
65
|
+
this.setSystemPrompt(AskUIAgent.AndroidSystemPrompt);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.AskUIAgent = AskUIAgent;
|
|
70
|
+
AskUIAgent.DesktopSystemPrompt = `
|
|
71
|
+
<SYSTEM_CAPABILITY>
|
|
72
|
+
You are an autonomous AI assistant operating on a ${process.platform} machine with ${process.arch} architecture. You have full access to the system and internet connectivity.
|
|
73
|
+
Your main goal is to mimic a human user interacting with a desktop computer. So you should try to use the tools in a way that a human would use a mouse and keyboard to interact with a computer.
|
|
74
|
+
|
|
75
|
+
Key Capabilities:
|
|
76
|
+
* Full system control through mouse and keyboard interactions
|
|
77
|
+
* Screen capture and analysis
|
|
78
|
+
* Web browser automation and navigation
|
|
79
|
+
* File system access and manipulation
|
|
80
|
+
* PDF document handling and text extraction
|
|
81
|
+
* Error handling and recovery mechanisms
|
|
82
|
+
|
|
83
|
+
Available Tools:
|
|
84
|
+
* Mouse control (move, click, scroll)
|
|
85
|
+
* Keyboard input (single keys, key combinations, typing)
|
|
86
|
+
* Screen capture and analysis
|
|
87
|
+
* Error reporting and recovery
|
|
88
|
+
|
|
89
|
+
Current Date: ${new Date().toUTCString()} UTC
|
|
90
|
+
</SYSTEM_CAPABILITY>
|
|
91
|
+
|
|
92
|
+
<OPERATIONAL_GUIDELINES>
|
|
93
|
+
1. Autonomous Operation:
|
|
94
|
+
* Work independently to achieve user goals
|
|
95
|
+
* Make informed decisions based on available information
|
|
96
|
+
* Chain multiple actions efficiently when possible
|
|
97
|
+
* Verify results after each significant action
|
|
98
|
+
|
|
99
|
+
2. Web Interaction:
|
|
100
|
+
* Launch appropriate browser if not already open
|
|
101
|
+
* Ensure full page visibility through zoom or scrolling
|
|
102
|
+
* Handle browser-specific behaviors (e.g., Firefox startup wizard)
|
|
103
|
+
* Extract and process PDF content when encountered
|
|
104
|
+
|
|
105
|
+
3. Error Handling:
|
|
106
|
+
* Detect and analyze failure points
|
|
107
|
+
* Implement appropriate recovery strategies
|
|
108
|
+
* Report issues with clear diagnostic information
|
|
109
|
+
* Use the error tool when stuck or unable to proceed
|
|
110
|
+
|
|
111
|
+
4. Performance Optimization:
|
|
112
|
+
* Batch related actions when possible
|
|
113
|
+
* Minimize unnecessary screen captures
|
|
114
|
+
* Use efficient navigation patterns
|
|
115
|
+
* Maintain context between actions
|
|
116
|
+
|
|
117
|
+
5. Safety and Validation:
|
|
118
|
+
* Verify coordinates are within screen bounds
|
|
119
|
+
* Validate input parameters before execution
|
|
120
|
+
* Ensure proper cleanup after operations
|
|
121
|
+
* Maintain system stability
|
|
122
|
+
</OPERATIONAL_GUIDELINES>
|
|
123
|
+
|
|
124
|
+
<IMPORTANT_NOTES>
|
|
125
|
+
* When you are stuck or unable to proceed, use the error tool to raise an error.
|
|
126
|
+
* Always verify tool availability before use
|
|
127
|
+
* Use screenshots strategically for state analysis
|
|
128
|
+
* Report issues promptly with clear diagnostic information
|
|
129
|
+
* Maintain awareness of screen boundaries and coordinate validity
|
|
130
|
+
* Adapt to unexpected situations with appropriate fallback strategies
|
|
131
|
+
</IMPORTANT_NOTES>
|
|
132
|
+
`;
|
|
133
|
+
AskUIAgent.AndroidSystemPrompt = `
|
|
134
|
+
<SYSTEM_CAPABILITY>
|
|
135
|
+
You are an autonomous AI assistant operating on an Android device via ADB. The host machine is ${process.platform} with ${process.arch} architecture and internet connectivity.
|
|
136
|
+
Your main goal is to mimic a human user interacting with an Android device. So you should try to use the tools in a way that a human would use a touch screen to interact with an Android device.
|
|
137
|
+
|
|
138
|
+
Key Capabilities:
|
|
139
|
+
* Full Android device control through ADB
|
|
140
|
+
* Screen capture and analysis
|
|
141
|
+
* Touch input simulation
|
|
142
|
+
* Android-specific key events
|
|
143
|
+
* Error handling and recovery mechanisms
|
|
144
|
+
|
|
145
|
+
Available Tools:
|
|
146
|
+
* Touch control (click, swipe, scroll)
|
|
147
|
+
* Android key events (single and sequence)
|
|
148
|
+
* Screen capture and analysis
|
|
149
|
+
* Error reporting and recovery
|
|
150
|
+
|
|
151
|
+
Current Date: ${new Date().toUTCString()} UTC
|
|
152
|
+
</SYSTEM_CAPABILITY>
|
|
153
|
+
|
|
154
|
+
<OPERATIONAL_GUIDELINES>
|
|
155
|
+
1. Autonomous Operation:
|
|
156
|
+
* Work independently to achieve user goals
|
|
157
|
+
* Make informed decisions based on available information
|
|
158
|
+
* Chain multiple actions efficiently when possible
|
|
159
|
+
* Verify results after each significant action
|
|
160
|
+
|
|
161
|
+
2. Screen Interaction:
|
|
162
|
+
* Analyze screen state before interactions
|
|
163
|
+
* Use appropriate input methods (touch, keys)
|
|
164
|
+
* Handle dynamic UI elements effectively
|
|
165
|
+
* Implement efficient navigation patterns
|
|
166
|
+
|
|
167
|
+
3. Error Handling:
|
|
168
|
+
* Detect and analyze failure points
|
|
169
|
+
* Implement appropriate recovery strategies
|
|
170
|
+
* Report issues with clear diagnostic information
|
|
171
|
+
* Use the error tool when stuck or unable to proceed
|
|
172
|
+
|
|
173
|
+
4. Performance Optimization:
|
|
174
|
+
* Batch related actions when possible
|
|
175
|
+
* Minimize unnecessary screen captures
|
|
176
|
+
* Use efficient navigation patterns
|
|
177
|
+
* Maintain context between actions
|
|
178
|
+
|
|
179
|
+
5. Safety and Validation:
|
|
180
|
+
* Verify coordinates are within screen bounds
|
|
181
|
+
* Validate input parameters before execution
|
|
182
|
+
* Ensure proper cleanup after operations
|
|
183
|
+
* Maintain device stability
|
|
184
|
+
</OPERATIONAL_GUIDELINES>
|
|
185
|
+
|
|
186
|
+
<IMPORTANT_NOTES>
|
|
187
|
+
* When you are stuck or unable to proceed, use the error tool to raise an error.
|
|
188
|
+
* Always verify tool availability before use
|
|
189
|
+
* Use screenshots strategically for state analysis
|
|
190
|
+
* Ensure all coordinates are integers and within screen bounds
|
|
191
|
+
* Handle permission issues and device state appropriately
|
|
192
|
+
* Report issues promptly with clear diagnostic information
|
|
193
|
+
* Adapt to unexpected situations with appropriate fallback strategies
|
|
194
|
+
</IMPORTANT_NOTES>
|
|
195
|
+
`;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Beta } from '@anthropic-ai/sdk/resources';
|
|
2
|
+
import { BaseAgentTool } from './tools/base';
|
|
3
|
+
type PredictActResponseFunction = (params: {
|
|
4
|
+
max_tokens: number;
|
|
5
|
+
messages: Beta.BetaMessageParam[];
|
|
6
|
+
model: string;
|
|
7
|
+
system?: string;
|
|
8
|
+
tools?: any[];
|
|
9
|
+
betas?: string[];
|
|
10
|
+
}) => Promise<Beta.BetaMessage>;
|
|
11
|
+
export declare class ClaudeAgent {
|
|
12
|
+
private predictActResponseFunction;
|
|
13
|
+
private maxTokens;
|
|
14
|
+
private onlyNMostRecentImages;
|
|
15
|
+
private imageTruncationThreshold;
|
|
16
|
+
private systemPrompt;
|
|
17
|
+
private model;
|
|
18
|
+
private betas;
|
|
19
|
+
private _toolCollection;
|
|
20
|
+
private tools;
|
|
21
|
+
private history;
|
|
22
|
+
constructor(predictActResponseFunction: PredictActResponseFunction);
|
|
23
|
+
setTools(tools: BaseAgentTool[]): void;
|
|
24
|
+
addTool(tool: BaseAgentTool): void;
|
|
25
|
+
listToolNames(): string[];
|
|
26
|
+
removeToolByName(toolName: string): void;
|
|
27
|
+
setSystemPrompt(systemPrompt: string): void;
|
|
28
|
+
private IsConfigured;
|
|
29
|
+
private get toolCollection();
|
|
30
|
+
private setHistory;
|
|
31
|
+
private getHistory;
|
|
32
|
+
act(goal: string, options?: {
|
|
33
|
+
chatId?: string;
|
|
34
|
+
agentHistory?: Beta.BetaMessageParam[];
|
|
35
|
+
}): Promise<Beta.BetaMessageParam[]>;
|
|
36
|
+
private makeApiToolResult;
|
|
37
|
+
private maybePrependSystemToolResult;
|
|
38
|
+
private static filterNMostRecentImages;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ClaudeAgent = void 0;
|
|
13
|
+
const base_1 = require("./tools/base");
|
|
14
|
+
const logger_1 = require("../../../lib/logger");
|
|
15
|
+
class ClaudeAgent {
|
|
16
|
+
constructor(predictActResponseFunction) {
|
|
17
|
+
this.predictActResponseFunction = predictActResponseFunction;
|
|
18
|
+
this.maxTokens = 4096;
|
|
19
|
+
this.onlyNMostRecentImages = 3;
|
|
20
|
+
this.imageTruncationThreshold = 10;
|
|
21
|
+
this.systemPrompt = '';
|
|
22
|
+
this.model = 'claude-3-5-sonnet-20241022';
|
|
23
|
+
this.betas = ['computer-use-2024-10-22'];
|
|
24
|
+
this._toolCollection = undefined;
|
|
25
|
+
this.tools = [];
|
|
26
|
+
this.history = {};
|
|
27
|
+
}
|
|
28
|
+
setTools(tools) {
|
|
29
|
+
this._toolCollection = undefined;
|
|
30
|
+
this.tools = tools;
|
|
31
|
+
}
|
|
32
|
+
addTool(tool) {
|
|
33
|
+
this._toolCollection = undefined;
|
|
34
|
+
this.tools.push(tool);
|
|
35
|
+
}
|
|
36
|
+
listToolNames() {
|
|
37
|
+
return this.tools.map((tool) => tool.ToolName);
|
|
38
|
+
}
|
|
39
|
+
removeToolByName(toolName) {
|
|
40
|
+
this.tools = this.tools.filter((tool) => tool.ToolName !== toolName);
|
|
41
|
+
}
|
|
42
|
+
setSystemPrompt(systemPrompt) {
|
|
43
|
+
this.systemPrompt = systemPrompt;
|
|
44
|
+
}
|
|
45
|
+
IsConfigured() {
|
|
46
|
+
return this.tools.length > 0 && this.systemPrompt !== '';
|
|
47
|
+
}
|
|
48
|
+
get toolCollection() {
|
|
49
|
+
if (!this._toolCollection) {
|
|
50
|
+
this._toolCollection = new base_1.ToolCollection(this.tools);
|
|
51
|
+
}
|
|
52
|
+
return this._toolCollection;
|
|
53
|
+
}
|
|
54
|
+
setHistory(key, messages) {
|
|
55
|
+
this.history[key] = messages;
|
|
56
|
+
}
|
|
57
|
+
getHistory(key) {
|
|
58
|
+
return this.history[key] || [];
|
|
59
|
+
}
|
|
60
|
+
act(goal, options) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
if (!goal.trim()) {
|
|
63
|
+
throw new Error('Goal cannot be empty');
|
|
64
|
+
}
|
|
65
|
+
if (!this.IsConfigured()) {
|
|
66
|
+
throw new Error('Claude agent is not configured. Please configure the agent first.');
|
|
67
|
+
}
|
|
68
|
+
const messages = [];
|
|
69
|
+
if (options === null || options === void 0 ? void 0 : options.agentHistory) {
|
|
70
|
+
messages.push(...options.agentHistory);
|
|
71
|
+
}
|
|
72
|
+
if (options === null || options === void 0 ? void 0 : options.chatId) {
|
|
73
|
+
messages.push(...this.getHistory(options.chatId));
|
|
74
|
+
}
|
|
75
|
+
// Add the new goal as a user message
|
|
76
|
+
messages.push({
|
|
77
|
+
content: goal,
|
|
78
|
+
role: 'user',
|
|
79
|
+
});
|
|
80
|
+
if (this.onlyNMostRecentImages) {
|
|
81
|
+
ClaudeAgent.filterNMostRecentImages(messages, this.onlyNMostRecentImages, this.imageTruncationThreshold);
|
|
82
|
+
}
|
|
83
|
+
while (true) {
|
|
84
|
+
const response = yield this.predictActResponseFunction({
|
|
85
|
+
max_tokens: this.maxTokens,
|
|
86
|
+
messages,
|
|
87
|
+
model: this.model,
|
|
88
|
+
system: this.systemPrompt,
|
|
89
|
+
tools: (new base_1.ToolCollection(this.tools).toParams()),
|
|
90
|
+
betas: this.betas,
|
|
91
|
+
});
|
|
92
|
+
messages.push({
|
|
93
|
+
content: response.content,
|
|
94
|
+
role: 'assistant',
|
|
95
|
+
});
|
|
96
|
+
const toolResultContent = [];
|
|
97
|
+
for (const block of response.content) {
|
|
98
|
+
if (block.type === 'tool_use') {
|
|
99
|
+
logger_1.logger.debug(`Agent will execute tool '${block.name}' with input '${JSON.stringify(block.input)}'`);
|
|
100
|
+
const toolUseBlock = block;
|
|
101
|
+
const result = yield this.toolCollection.run(toolUseBlock.name, toolUseBlock.input);
|
|
102
|
+
const toolResult = this.makeApiToolResult(result, toolUseBlock.id);
|
|
103
|
+
toolResultContent.push(toolResult);
|
|
104
|
+
}
|
|
105
|
+
if (block.type === 'thinking') {
|
|
106
|
+
logger_1.logger.debug(`Agent is thinking: ${block.thinking}`);
|
|
107
|
+
}
|
|
108
|
+
if (block.type === 'text') {
|
|
109
|
+
logger_1.logger.info(`Agent: ${block.text}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (toolResultContent.length === 0) {
|
|
113
|
+
if (options === null || options === void 0 ? void 0 : options.chatId) {
|
|
114
|
+
this.setHistory(options.chatId, messages);
|
|
115
|
+
}
|
|
116
|
+
return messages;
|
|
117
|
+
}
|
|
118
|
+
messages.push({
|
|
119
|
+
role: 'user',
|
|
120
|
+
content: toolResultContent,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
makeApiToolResult(result, toolUseId) {
|
|
126
|
+
const content = [];
|
|
127
|
+
let isError = false;
|
|
128
|
+
if (result.error) {
|
|
129
|
+
isError = true;
|
|
130
|
+
content.push({
|
|
131
|
+
type: 'text',
|
|
132
|
+
text: this.maybePrependSystemToolResult(result, result.error),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (result.output) {
|
|
137
|
+
content.push({
|
|
138
|
+
type: 'text',
|
|
139
|
+
text: this.maybePrependSystemToolResult(result, result.output),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (result.base64Images) {
|
|
143
|
+
for (const base64Image of result.base64Images) {
|
|
144
|
+
content.push({
|
|
145
|
+
type: 'image',
|
|
146
|
+
source: {
|
|
147
|
+
type: 'base64',
|
|
148
|
+
media_type: 'image/png',
|
|
149
|
+
data: base64Image,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
type: 'tool_result',
|
|
157
|
+
tool_use_id: toolUseId,
|
|
158
|
+
content,
|
|
159
|
+
is_error: isError,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
maybePrependSystemToolResult(result, text) {
|
|
163
|
+
if (result.system) {
|
|
164
|
+
return `<system>${result.system}</system>\n${text}`;
|
|
165
|
+
}
|
|
166
|
+
return text;
|
|
167
|
+
}
|
|
168
|
+
static filterNMostRecentImages(messages, imagesToKeep, minRemovalThreshold = 10) {
|
|
169
|
+
const toolResultBlocks = messages
|
|
170
|
+
.flatMap((message) => (Array.isArray(message.content) ? message.content : []))
|
|
171
|
+
.filter((item) => typeof item === 'object'
|
|
172
|
+
&& 'type' in item
|
|
173
|
+
&& item.type === 'tool_result');
|
|
174
|
+
const totalImages = toolResultBlocks.reduce((count, result) => {
|
|
175
|
+
const content = Array.isArray(result.content) ? result.content : [];
|
|
176
|
+
return count + content.filter((item) => typeof item === 'object'
|
|
177
|
+
&& 'type' in item
|
|
178
|
+
&& item.type === 'image').length;
|
|
179
|
+
}, 0);
|
|
180
|
+
let imagesToRemove = totalImages - imagesToKeep;
|
|
181
|
+
imagesToRemove -= imagesToRemove % minRemovalThreshold;
|
|
182
|
+
if (imagesToRemove <= 0)
|
|
183
|
+
return;
|
|
184
|
+
for (const result of toolResultBlocks) {
|
|
185
|
+
if (Array.isArray(result.content)) {
|
|
186
|
+
const newContent = result.content.filter((item) => {
|
|
187
|
+
if ('type' in item && item.type === 'image') {
|
|
188
|
+
if (imagesToRemove > 0) {
|
|
189
|
+
imagesToRemove -= 1;
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
});
|
|
195
|
+
result.content = newContent;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.ClaudeAgent = ClaudeAgent;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseAgentTool = exports.ToolError = exports.ToolFailure = exports.AskUIAgent = void 0;
|
|
4
|
+
var askui_agent_1 = require("./askui-agent");
|
|
5
|
+
Object.defineProperty(exports, "AskUIAgent", { enumerable: true, get: function () { return askui_agent_1.AskUIAgent; } });
|
|
6
|
+
var base_1 = require("./tools/base");
|
|
7
|
+
Object.defineProperty(exports, "ToolFailure", { enumerable: true, get: function () { return base_1.ToolFailure; } });
|
|
8
|
+
Object.defineProperty(exports, "ToolError", { enumerable: true, get: function () { return base_1.ToolError; } });
|
|
9
|
+
Object.defineProperty(exports, "BaseAgentTool", { enumerable: true, get: function () { return base_1.BaseAgentTool; } });
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ToolResult {
|
|
2
|
+
output?: string;
|
|
3
|
+
error?: string;
|
|
4
|
+
base64Images?: string[];
|
|
5
|
+
system?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class ToolFailure implements ToolResult {
|
|
8
|
+
error: string;
|
|
9
|
+
constructor(error: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class ToolError extends Error {
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
export declare abstract class BaseAgentTool {
|
|
15
|
+
abstract execute(params: Record<string, any>): Promise<ToolResult>;
|
|
16
|
+
abstract toParams(): any;
|
|
17
|
+
get ToolName(): string;
|
|
18
|
+
}
|
|
19
|
+
export declare class ToolCollection {
|
|
20
|
+
private toolMap;
|
|
21
|
+
constructor(tools: BaseAgentTool[]);
|
|
22
|
+
toParams(): any[];
|
|
23
|
+
run(name: string, toolInput: Record<string, any>): Promise<ToolResult>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ToolCollection = exports.BaseAgentTool = exports.ToolError = exports.ToolFailure = void 0;
|
|
13
|
+
/* eslint-disable */
|
|
14
|
+
const agent_errors_1 = require("./agent-errors");
|
|
15
|
+
class ToolFailure {
|
|
16
|
+
constructor(error) {
|
|
17
|
+
this.error = error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ToolFailure = ToolFailure;
|
|
21
|
+
class ToolError extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'ToolError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.ToolError = ToolError;
|
|
28
|
+
class BaseAgentTool {
|
|
29
|
+
get ToolName() {
|
|
30
|
+
return this.constructor.name;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.BaseAgentTool = BaseAgentTool;
|
|
34
|
+
class ToolCollection {
|
|
35
|
+
constructor(tools) {
|
|
36
|
+
this.toolMap = new Map();
|
|
37
|
+
tools.forEach((tool) => {
|
|
38
|
+
const params = tool.toParams();
|
|
39
|
+
this.toolMap.set(params.name, tool);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
toParams() {
|
|
43
|
+
return Array.from(this.toolMap.values()).map((tool) => tool.toParams());
|
|
44
|
+
}
|
|
45
|
+
run(name, toolInput) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const tool = this.toolMap.get(name);
|
|
48
|
+
if (!tool) {
|
|
49
|
+
return new ToolFailure(`Tool ${name} is invalid`);
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return yield tool.execute(toolInput);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
if (err instanceof agent_errors_1.AgentError) {
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
if (err instanceof ToolError) {
|
|
59
|
+
return new ToolFailure(err.message);
|
|
60
|
+
}
|
|
61
|
+
return new ToolFailure(`Unknown error: ${err}`);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.ToolCollection = ToolCollection;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { BetaTool } from '@anthropic-ai/sdk/resources/beta/messages/messages';
|
|
2
|
+
import { MODIFIER_KEY, PC_KEY, ANDROID_KEY, PC_AND_MODIFIER_KEY } from '../../../../execution/dsl';
|
|
3
|
+
import { BaseAgentTool, ToolResult } from './base';
|
|
4
|
+
import { ExecutionRuntime } from '../../../../execution/execution-runtime';
|
|
5
|
+
import { ControlCommand } from '../../../ui-control-commands';
|
|
6
|
+
export declare class OsAgentHandler {
|
|
7
|
+
private AgentOsClient;
|
|
8
|
+
private TargetResolution;
|
|
9
|
+
private screenDimensions;
|
|
10
|
+
constructor(AgentOsClient: ExecutionRuntime, screenDimensions: {
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
});
|
|
14
|
+
static createInstance(AgentOsClient: ExecutionRuntime): Promise<OsAgentHandler>;
|
|
15
|
+
getTargetResolution(): {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
};
|
|
19
|
+
setTargetResolution(width: number, height: number): void;
|
|
20
|
+
takeScreenshot(): Promise<string>;
|
|
21
|
+
private scaleCoordinates;
|
|
22
|
+
requestControl(controlCommand: ControlCommand): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export declare class ScreenShotTool extends BaseAgentTool {
|
|
25
|
+
private osAgentHandler;
|
|
26
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
27
|
+
execute(): Promise<ToolResult>;
|
|
28
|
+
toParams(): BetaTool;
|
|
29
|
+
}
|
|
30
|
+
export declare class MouseMoveTool extends BaseAgentTool {
|
|
31
|
+
private osAgentHandler;
|
|
32
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
33
|
+
execute(command: {
|
|
34
|
+
x: number;
|
|
35
|
+
y: number;
|
|
36
|
+
}): Promise<ToolResult>;
|
|
37
|
+
toParams(): BetaTool;
|
|
38
|
+
}
|
|
39
|
+
export declare class MouseClickTool extends BaseAgentTool {
|
|
40
|
+
private osAgentHandler;
|
|
41
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
42
|
+
execute(command: {
|
|
43
|
+
button: 'left' | 'right' | 'middle';
|
|
44
|
+
doubleClick: boolean;
|
|
45
|
+
}): Promise<ToolResult>;
|
|
46
|
+
toParams(): BetaTool;
|
|
47
|
+
}
|
|
48
|
+
export declare class MouseScrollTool extends BaseAgentTool {
|
|
49
|
+
private osAgentHandler;
|
|
50
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
51
|
+
execute(command: {
|
|
52
|
+
dx: number;
|
|
53
|
+
dy: number;
|
|
54
|
+
}): Promise<ToolResult>;
|
|
55
|
+
toParams(): BetaTool;
|
|
56
|
+
}
|
|
57
|
+
export declare class DesktopKeyPressSequenceTool extends BaseAgentTool {
|
|
58
|
+
private osAgentHandler;
|
|
59
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
60
|
+
execute(command: {
|
|
61
|
+
key: PC_KEY;
|
|
62
|
+
firstModifier?: MODIFIER_KEY;
|
|
63
|
+
secondModifier?: MODIFIER_KEY;
|
|
64
|
+
}): Promise<ToolResult>;
|
|
65
|
+
toParams(): BetaTool;
|
|
66
|
+
}
|
|
67
|
+
export declare class DesktopSingleKeyPressTool extends BaseAgentTool {
|
|
68
|
+
private osAgentHandler;
|
|
69
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
70
|
+
execute(command: {
|
|
71
|
+
key: PC_AND_MODIFIER_KEY;
|
|
72
|
+
}): Promise<ToolResult>;
|
|
73
|
+
toParams(): BetaTool;
|
|
74
|
+
}
|
|
75
|
+
export declare class TypeTool extends BaseAgentTool {
|
|
76
|
+
private osAgentHandler;
|
|
77
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
78
|
+
execute(command: {
|
|
79
|
+
text: string;
|
|
80
|
+
}): Promise<ToolResult>;
|
|
81
|
+
toParams(): BetaTool;
|
|
82
|
+
}
|
|
83
|
+
export declare class AndroidSingleKeyPressTool extends BaseAgentTool {
|
|
84
|
+
private osAgentHandler;
|
|
85
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
86
|
+
execute(command: {
|
|
87
|
+
key: ANDROID_KEY;
|
|
88
|
+
}): Promise<ToolResult>;
|
|
89
|
+
toParams(): BetaTool;
|
|
90
|
+
}
|
|
91
|
+
export declare class AndroidSequenceKeyPressTool extends BaseAgentTool {
|
|
92
|
+
private osAgentHandler;
|
|
93
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
94
|
+
execute(command: {
|
|
95
|
+
keys: ANDROID_KEY[];
|
|
96
|
+
}): Promise<ToolResult>;
|
|
97
|
+
toParams(): BetaTool;
|
|
98
|
+
}
|
|
99
|
+
export declare class AgentErrorTool extends BaseAgentTool {
|
|
100
|
+
constructor();
|
|
101
|
+
execute(command: {
|
|
102
|
+
error: string;
|
|
103
|
+
}): Promise<ToolResult>;
|
|
104
|
+
toParams(): BetaTool;
|
|
105
|
+
}
|
|
106
|
+
export declare class ExecuteShellCommandTool extends BaseAgentTool {
|
|
107
|
+
private osAgentHandler;
|
|
108
|
+
constructor(osAgentHandler: OsAgentHandler);
|
|
109
|
+
execute(command: {
|
|
110
|
+
command: string;
|
|
111
|
+
}): Promise<ToolResult>;
|
|
112
|
+
toParams(): BetaTool;
|
|
113
|
+
}
|