Skip to content

Commit 36c4a3e

Browse files
authored
feat!: Add computer use support in anthropic_sdk_dart (#586)
1 parent a41270a commit 36c4a3e

20 files changed

+2696
-255
lines changed

packages/anthropic_sdk_dart/README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ await for (final res in stream) {
106106
},
107107
contentBlockStop: (ContentBlockStopEvent e) {},
108108
ping: (PingEvent e) {},
109+
error: (ErrorEvent v) {},
109110
);
110111
}
111112
// Hello! It's nice to meet you. How are you doing today?
@@ -136,7 +137,7 @@ Map<String, dynamic> _getCurrentWeather(
136137

137138
To do that, we need to provide the definition of the tool:
138139
```dart
139-
const tool = Tool(
140+
const tool = Tool.custom(
140141
name: 'get_current_weather',
141142
description: 'Get the current weather in a given location',
142143
inputSchema: {
@@ -239,11 +240,50 @@ await for (final res in stream) {
239240
},
240241
contentBlockStop: (ContentBlockStopEvent v) {},
241242
ping: (PingEvent v) {},
243+
error: (ErrorEvent v) {},
242244
);
243245
}
244246
// {"location": "Boston, MA", "unit": "fahrenheit"}
245247
```
246248

249+
### Computer use
250+
251+
Claude 3.5 Sonnet model is capable of interacting with tools that can manipulate a computer desktop environment.
252+
253+
Refer to the [official documentation](https://docs.anthropic.com/en/docs/build-with-claude/computer-use) for more information.
254+
255+
Anthropic-defined tools:
256+
- `computer`: a tool that uses a mouse and keyboard to interact with a computer, and take screenshots.
257+
- `text_editor`: a tool for viewing, creating and editing files.
258+
- `bash`: a tool for running commands in a bash shell.
259+
260+
Example:
261+
```dart
262+
const request = CreateMessageRequest(
263+
model: Model.model(Models.claude35Sonnet20241022),
264+
messages: [
265+
Message(
266+
role: MessageRole.user,
267+
content: MessageContent.text(
268+
'Save a picture of a cat to my desktop. '
269+
'After each step, take a screenshot and carefully evaluate if you '
270+
'have achieved the right outcome. Explicitly show your thinking: '
271+
'"I have evaluated step X..." If not correct, try again. '
272+
'Only when you confirm a step was executed correctly should '
273+
'you move on to the next one.',
274+
),
275+
),
276+
],
277+
tools: [
278+
Tool.computerUse(displayWidthPx: 1024, displayHeightPx: 768),
279+
Tool.textEditor(),
280+
Tool.bash(),
281+
],
282+
maxTokens: 1024,
283+
);
284+
final res = await client.createMessage(request: request);
285+
```
286+
247287
### Message Batches
248288

249289
The Message Batches API is a powerful, cost-effective way to asynchronously process large volumes of Messages requests. This approach is well-suited to tasks that do not require immediate responses, reducing costs by 50% while increasing throughput.

packages/anthropic_sdk_dart/example/anthropic_sdk_dart_example.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Future<void> _createMessageStream(final AnthropicClient client) async {
5959
},
6060
contentBlockStop: (ContentBlockStopEvent e) {},
6161
ping: (PingEvent e) {},
62+
error: (ErrorEvent e) {},
6263
);
6364
}
6465
// Hello! It's nice to meet you. How are you doing today?
@@ -161,6 +162,7 @@ Future<void> _toolUseStreaming(final AnthropicClient client) async {
161162
},
162163
contentBlockStop: (ContentBlockStopEvent v) {},
163164
ping: (PingEvent v) {},
165+
error: (ErrorEvent v) {},
164166
);
165167
}
166168
// {"location": "Boston, MA", "unit": "fahrenheit"}
@@ -179,7 +181,7 @@ Map<String, dynamic> _getCurrentWeather(
179181
};
180182
}
181183

182-
const tool = Tool(
184+
const tool = Tool.custom(
183185
name: 'get_current_weather',
184186
description: 'Get the current weather in a given location',
185187
inputSchema: {

packages/anthropic_sdk_dart/lib/src/client.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class AnthropicClient extends g.AnthropicClient {
3838
baseUrl: baseUrl,
3939
headers: {
4040
'anthropic-version': '2023-06-01',
41-
'anthropic-beta': 'message-batches-2024-09-24',
41+
'anthropic-beta': 'message-batches-2024-09-24,'
42+
'prompt-caching-2024-07-31,'
43+
'computer-use-2024-10-22',
4244
...?headers,
4345
},
4446
queryParams: queryParams ?? const {},

packages/anthropic_sdk_dart/lib/src/generated/schema/cache_control_ephemeral.dart

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/error.dart

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/message.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/message_delta.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/message_stream_event.dart

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/message_stream_event_type.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/anthropic_sdk_dart/lib/src/generated/schema/schema.dart

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)