Skip to content

Commit ab99464

Browse files
committed
Anthropic - new domain and json formats for tools, skills, and files
1 parent 3971257 commit ab99464

25 files changed

+1923
-166
lines changed

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/JsonFormats.scala

Lines changed: 757 additions & 142 deletions
Large diffs are not rendered by default.

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/domain/Content.scala

Lines changed: 345 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
package io.cequence.openaiscala.anthropic.domain
22

3+
import io.cequence.openaiscala.anthropic.domain.Content.ContentBlock
4+
import io.cequence.openaiscala.domain.HasType
5+
import io.cequence.wsclient.domain.EnumValue
6+
import play.api.libs.json.JsObject
7+
38
sealed trait Content
49

510
sealed trait CacheControl
11+
612
object CacheControl {
7-
case object Ephemeral extends CacheControl
13+
case class Ephemeral(ttl: Option[CacheTTL] = None) extends CacheControl
14+
}
15+
16+
sealed trait CacheTTL extends EnumValue
17+
18+
object CacheTTL {
19+
case object `5m` extends CacheTTL
20+
case object `1h` extends CacheTTL
21+
22+
def values: Seq[CacheTTL] = Seq(`5m`, `1h`)
823
}
924

1025
trait Cacheable {
@@ -26,18 +41,95 @@ object Content {
2641
) extends Content
2742
with Cacheable
2843

29-
sealed trait ContentBlock
44+
sealed trait ContentBlock extends HasType
3045

3146
object ContentBlock {
3247
case class TextBlock(
3348
text: String,
3449
citations: Seq[Citation] = Nil
35-
) extends ContentBlock
50+
) extends ContentBlock {
51+
val `type`: String = "text"
52+
}
3653

3754
case class ThinkingBlock(
3855
thinking: String,
3956
signature: String
40-
) extends ContentBlock
57+
) extends ContentBlock {
58+
val `type`: String = "thinking"
59+
}
60+
61+
case class RedactedThinkingBlock(
62+
data: String
63+
) extends ContentBlock {
64+
val `type`: String = "redacted_thinking"
65+
}
66+
67+
case class ToolUseBlock(
68+
id: String,
69+
name: String,
70+
input: JsObject
71+
) extends ContentBlock {
72+
val `type`: String = "tool_use"
73+
}
74+
75+
case class ServerToolUseBlock(
76+
id: String,
77+
name: ServerToolName,
78+
input: JsObject
79+
) extends ContentBlock {
80+
val `type`: String = "server_tool_use"
81+
}
82+
83+
case class WebSearchToolResultBlock(
84+
content: WebSearchToolResultContent,
85+
toolUseId: String
86+
) extends ContentBlock {
87+
val `type`: String = "web_search_tool_result"
88+
}
89+
90+
case class McpToolUseBlock(
91+
id: String,
92+
name: String,
93+
serverName: String,
94+
input: JsObject
95+
) extends ContentBlock {
96+
val `type`: String = "mcp_tool_use"
97+
}
98+
99+
case class McpToolResultBlock(
100+
content: McpToolResultContent,
101+
isError: Boolean,
102+
toolUseId: String
103+
) extends ContentBlock {
104+
val `type`: String = "mcp_tool_result"
105+
}
106+
107+
case class ContainerUploadBlock(
108+
fileId: String
109+
) extends ContentBlock {
110+
val `type`: String = "container_upload"
111+
}
112+
113+
case class CodeExecutionToolResultBlock(
114+
content: CodeExecutionToolResultContent,
115+
toolUseId: String
116+
) extends ContentBlock {
117+
val `type`: String = "code_execution_tool_result"
118+
}
119+
120+
case class BashCodeExecutionToolResultBlock(
121+
content: BashCodeExecutionToolResultContent,
122+
toolUseId: String
123+
) extends ContentBlock {
124+
val `type`: String = "bash_code_execution_tool_result"
125+
}
126+
127+
case class TextEditorCodeExecutionToolResultBlock(
128+
content: TextEditorCodeExecutionToolResultContent,
129+
toolUseId: String
130+
) extends ContentBlock {
131+
val `type`: String = "text_editor_code_execution_tool_result"
132+
}
41133

42134
case class Citation(
43135
`type`: String,
@@ -47,7 +139,10 @@ object Content {
47139
startCharIndex: Option[Int],
48140
endCharIndex: Option[Int],
49141
startBlockIndex: Option[Int],
50-
endBlockIndex: Option[Int]
142+
endBlockIndex: Option[Int],
143+
startPageNumber: Option[Int],
144+
endPageNumber: Option[Int],
145+
fileId: Option[String]
51146
)
52147

53148
case class MediaBlock(
@@ -65,7 +160,19 @@ object Content {
65160
title: Option[String] = None, // Document Title
66161
context: Option[String] = None, // Context about the document that will not be cited from
67162
citations: Option[Boolean] = None
68-
) extends ContentBlock
163+
) extends ContentBlock {
164+
override val `type` = "document"
165+
}
166+
167+
// TODO: revisit this
168+
case class FileDocumentContentBlock(
169+
fileId: String,
170+
title: Option[String] = None, // Document Title
171+
context: Option[String] = None, // Context about the document that will not be cited from
172+
citations: Option[Boolean] = None
173+
) extends ContentBlock {
174+
override val `type` = "document"
175+
}
69176

70177
object MediaBlock {
71178
def pdf(
@@ -144,3 +251,235 @@ object Content {
144251

145252
}
146253
}
254+
255+
sealed trait ServerToolName extends EnumValue
256+
257+
object ServerToolName {
258+
case object web_search extends ServerToolName
259+
case object web_fetch extends ServerToolName
260+
case object code_execution extends ServerToolName
261+
case object bash_code_execution extends ServerToolName
262+
case object text_editor_code_execution extends ServerToolName
263+
264+
def values: Seq[ServerToolName] = Seq(
265+
web_search,
266+
web_fetch,
267+
code_execution,
268+
bash_code_execution,
269+
text_editor_code_execution
270+
)
271+
}
272+
273+
sealed trait WebSearchToolResultContent
274+
275+
object WebSearchToolResultContent {
276+
277+
case class Success(
278+
results: Seq[Item]
279+
) extends WebSearchToolResultContent
280+
281+
case class Error(
282+
errorCode: WebSearchErrorCode
283+
) extends WebSearchToolResultContent
284+
with HasType {
285+
val `type`: String = "web_search_tool_result_error"
286+
}
287+
288+
case class Item(
289+
encryptedContent: String,
290+
pageAge: Option[String],
291+
title: String,
292+
url: String
293+
) extends HasType {
294+
val `type`: String = "web_search_result"
295+
}
296+
297+
sealed trait WebSearchErrorCode extends EnumValue
298+
299+
object WebSearchErrorCode {
300+
case object invalid_tool_input extends WebSearchErrorCode
301+
case object unavailable extends WebSearchErrorCode
302+
case object max_uses_exceeded extends WebSearchErrorCode
303+
case object too_many_requests extends WebSearchErrorCode
304+
case object query_too_long extends WebSearchErrorCode
305+
306+
def values: Seq[WebSearchErrorCode] = Seq(
307+
invalid_tool_input,
308+
unavailable,
309+
max_uses_exceeded,
310+
too_many_requests,
311+
query_too_long
312+
)
313+
}
314+
}
315+
316+
sealed trait McpToolResultContent
317+
318+
case class McpToolResultString(
319+
value: String
320+
) extends McpToolResultContent
321+
322+
case class McpToolResultStructured(
323+
results: Seq[ToolResultContent]
324+
) extends McpToolResultContent
325+
326+
case class ToolResultContent(
327+
text: String,
328+
citations: Seq[ContentBlock.Citation] = Nil
329+
) extends HasType {
330+
val `type`: String = "text"
331+
}
332+
333+
sealed trait CodeExecutionToolResultContent extends HasType
334+
335+
object CodeExecutionToolResultContent {
336+
337+
case class Success(
338+
content: Seq[Item],
339+
returnCode: Int,
340+
stderr: String,
341+
stdout: String
342+
) extends CodeExecutionToolResultContent {
343+
val `type`: String = "code_execution_result"
344+
}
345+
346+
case class Item(
347+
fileId: String
348+
) extends HasType {
349+
val `type`: String = "code_execution_output"
350+
}
351+
352+
case class Error(
353+
errorCode: CodeExecutionErrorCode
354+
) extends CodeExecutionToolResultContent {
355+
val `type`: String = "code_execution_tool_result_error"
356+
}
357+
358+
// Code Execution Tool Result types
359+
sealed trait CodeExecutionErrorCode extends EnumValue
360+
361+
object CodeExecutionErrorCode {
362+
case object invalid_tool_input extends CodeExecutionErrorCode
363+
case object unavailable extends CodeExecutionErrorCode
364+
case object too_many_requests extends CodeExecutionErrorCode
365+
case object execution_time_exceeded extends CodeExecutionErrorCode
366+
367+
def values: Seq[CodeExecutionErrorCode] = Seq(
368+
invalid_tool_input,
369+
unavailable,
370+
too_many_requests,
371+
execution_time_exceeded
372+
)
373+
}
374+
}
375+
376+
sealed trait BashCodeExecutionToolResultContent extends HasType
377+
378+
object BashCodeExecutionToolResultContent {
379+
380+
case class Success(
381+
content: Seq[Item],
382+
returnCode: Int,
383+
stderr: String,
384+
stdout: String
385+
) extends BashCodeExecutionToolResultContent {
386+
val `type`: String = "bash_code_execution_result"
387+
}
388+
389+
case class Item(
390+
fileId: String
391+
) extends HasType {
392+
val `type`: String = "bash_code_execution_output"
393+
}
394+
395+
case class Error(
396+
errorCode: BashCodeExecutionErrorCode
397+
) extends BashCodeExecutionToolResultContent {
398+
val `type`: String = "bash_code_execution_tool_result_error"
399+
}
400+
401+
// Bash Code Execution Tool Result types
402+
sealed trait BashCodeExecutionErrorCode extends EnumValue
403+
404+
object BashCodeExecutionErrorCode {
405+
case object invalid_tool_input extends BashCodeExecutionErrorCode
406+
case object unavailable extends BashCodeExecutionErrorCode
407+
case object too_many_requests extends BashCodeExecutionErrorCode
408+
case object execution_time_exceeded extends BashCodeExecutionErrorCode
409+
case object output_file_too_large extends BashCodeExecutionErrorCode
410+
411+
def values: Seq[BashCodeExecutionErrorCode] = Seq(
412+
invalid_tool_input,
413+
unavailable,
414+
too_many_requests,
415+
execution_time_exceeded,
416+
output_file_too_large
417+
)
418+
}
419+
}
420+
421+
sealed trait TextEditorCodeExecutionToolResultContent extends HasType
422+
423+
object TextEditorCodeExecutionToolResultContent {
424+
425+
case class Error(
426+
errorCode: TextEditorCodeExecutionErrorCode,
427+
errorMessage: Option[String]
428+
) extends TextEditorCodeExecutionToolResultContent {
429+
val `type`: String = "text_editor_code_execution_tool_result_error"
430+
}
431+
432+
case class ViewResult(
433+
content: String,
434+
fileType: FileType,
435+
numLines: Option[Int],
436+
startLine: Option[Int],
437+
totalLines: Option[Int]
438+
) extends TextEditorCodeExecutionToolResultContent {
439+
val `type`: String = "text_editor_code_execution_view_result"
440+
}
441+
442+
case class CreateResult(
443+
isFileUpdate: Boolean
444+
) extends TextEditorCodeExecutionToolResultContent {
445+
val `type`: String = "text_editor_code_execution_create_result"
446+
}
447+
448+
case class ReplaceResult(
449+
lines: Seq[String] = Nil,
450+
newLines: Option[Int] = None,
451+
newStart: Option[Int] = None,
452+
oldLines: Option[Int] = None,
453+
oldStart: Option[Int] = None
454+
) extends TextEditorCodeExecutionToolResultContent {
455+
val `type`: String = "text_editor_code_execution_str_replace_result"
456+
}
457+
458+
sealed trait TextEditorCodeExecutionErrorCode extends EnumValue
459+
460+
object TextEditorCodeExecutionErrorCode {
461+
case object invalid_tool_input extends TextEditorCodeExecutionErrorCode
462+
case object unavailable extends TextEditorCodeExecutionErrorCode
463+
case object too_many_requests extends TextEditorCodeExecutionErrorCode
464+
case object execution_time_exceeded extends TextEditorCodeExecutionErrorCode
465+
case object file_not_found extends TextEditorCodeExecutionErrorCode
466+
467+
def values: Seq[TextEditorCodeExecutionErrorCode] = Seq(
468+
invalid_tool_input,
469+
unavailable,
470+
too_many_requests,
471+
execution_time_exceeded,
472+
file_not_found
473+
)
474+
}
475+
476+
sealed trait FileType extends EnumValue
477+
478+
object FileType {
479+
case object text extends FileType
480+
case object image extends FileType
481+
case object pdf extends FileType
482+
483+
def values: Seq[FileType] = Seq(text, image, pdf)
484+
}
485+
}

0 commit comments

Comments
 (0)