-
-
Notifications
You must be signed in to change notification settings - Fork 639
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (121 loc) · 4.38 KB
/
Copy pathProgram.cs
File metadata and controls
135 lines (121 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Anthropic.SDK.Constants;
using Anthropic.SDK.Messaging;
using Anthropic.SDK;
using System.Text.Json.Nodes;
using BotSharp.Plugin.OsDriver;
using Anthropic.SDK.Common;
Console.WriteLine("Open Google Chrome in Incognito Mode in a given window. Enter which disaplay ID you are using:");
var displayId = Convert.ToInt32(Console.ReadLine());
var capturer = new ScreenshotWinOS();
var (width, height) = capturer.GetScreenSize(displayId - 1);
Console.WriteLine($"Screen Size: {width}x{height}");
var coordScaler = new CoordinateScaler(true, width, height);
var (scaledX, scaledY) = coordScaler.ScaleCoordinates(ScalingSource.COMPUTER, width, height);
Console.WriteLine($"Scaled Screen Size: {scaledX}x{scaledY}");
var client = new AnthropicClient(new APIAuthentication(""));
var messages = new List<Message>();
messages.Add(new Message()
{
Role = RoleType.User,
Content = new List<ContentBase>()
{
new TextContent()
{
Text = """
Open google.com to search blogs about BotSharp, find the most recent updates on it and output the summary.
"""
}
}
});
var tools = new List<Anthropic.SDK.Common.Tool>()
{
new Function("computer", "computer_20241022",new Dictionary<string, object>()
{
{"display_width_px", scaledX },
{"display_height_px", scaledY },
{"display_number", displayId }
})
};
var parameters = new MessageParameters()
{
Messages = messages,
Model = AnthropicModels.Claude46Sonnet,
Stream = false,
Tools = tools,
System = new List<SystemMessage>()
{
new SystemMessage($""""
A Google Chrome Incognito window is already open and maximized in the appropriate monitor. Use that instance.
"""")
}
};
var isRunning = true;
var res = await client.Messages.GetClaudeMessageAsync(parameters);
messages.Add(res.Message);
while (isRunning)
{
var toolUse = res.Content.OfType<ToolUseContent>().ToList();
if (toolUse.Count == 0)
{
isRunning = false;
break;
}
var cb = new List<ContentBase>();
foreach (var tool in toolUse)
{
var action = tool.Input["action"].ToString();
var text = tool.Input["text"]?.ToString();
var coordinate = tool.Input["coordinate"] as JsonArray;
switch (action)
{
case "screenshot":
messages.Add(new Message()
{
Role = RoleType.User,
Content = new List<ContentBase>()
{
new ToolResultContent()
{
ToolUseId = tool.Id,
Content =new List<ContentBase>() { new ImageContent()
{
Source = new ImageSource() {
Data = WinOSDriver.DownscaleScreenshot(capturer.CaptureScreen(displayId -1), scaledX, scaledY),
MediaType = "image/jpeg"
}
} }
}
}
});
break;
default:
WinOSDriver.TakeAction(action, text,
coordinate == null ? null : new Tuple<int, int>(Convert.ToInt32(coordinate[0].ToString()),
Convert.ToInt32(coordinate[1].ToString())), displayId - 1, coordScaler);
await Task.Delay(1000);
cb.Add(new ToolResultContent()
{
ToolUseId = tool.Id,
Content = new List<ContentBase>()
{
new TextContent()
{
Text = "Action completed"
}
}
});
break;
}
}
messages.Add(new Message()
{
Role = RoleType.User,
Content = cb
});
res = await client.Messages.GetClaudeMessageAsync(parameters);
messages.Add(res.Message);
}
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Final Result:");
Console.WriteLine(messages.Last().Content.OfType<TextContent>().First().Text);
Console.ReadLine();