@effect/ai-anthropic 0.19.2 → 0.20.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect/ai-anthropic",
3
- "version": "0.19.2",
3
+ "version": "0.20.0",
4
4
  "description": "Effect modules for working with AI apis",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,10 +14,10 @@
14
14
  "@anthropic-ai/tokenizer": "^0.0.4"
15
15
  },
16
16
  "peerDependencies": {
17
- "@effect/ai": "^0.29.1",
18
- "@effect/platform": "^0.92.1",
17
+ "@effect/ai": "^0.30.0",
19
18
  "@effect/experimental": "^0.56.0",
20
- "effect": "^3.18.2"
19
+ "@effect/platform": "^0.92.1",
20
+ "effect": "^3.18.4"
21
21
  },
22
22
  "publishConfig": {
23
23
  "provenance": true
@@ -326,7 +326,6 @@ export const make = Effect.fnUntraced(function*(options: {
326
326
  const context = yield* Effect.context<never>()
327
327
  const config = { model: options.model, ...options.config, ...context.unsafeMap.get(Config.key) }
328
328
  const { betas: messageBetas, messages, system } = yield* prepareMessages(providerOptions)
329
- console.dir({ messages }, { depth: null, colors: true })
330
329
  const { betas: toolBetas, toolChoice, tools } = yield* prepareTools(providerOptions, config)
331
330
  const responseFormat = providerOptions.responseFormat
332
331
  const request: typeof Generated.BetaCreateMessageParams.Encoded = {
@@ -573,11 +572,26 @@ const prepareMessages: (options: LanguageModel.ProviderOptions) => Effect.Effect
573
572
 
574
573
  // TODO: advanced tool result content parts
575
574
  case "tool": {
576
- for (const part of message.content) {
575
+ for (let j = 0; j < message.content.length; j++) {
576
+ const part = message.content[j]
577
+ const isLastPart = j === message.content.length - 1
578
+
579
+ // Attempt to get the cache control from the part first. If
580
+ // the part does not have cache control defined and we are
581
+ // evaluating the last part for this message, also check the
582
+ // message for cache control.
583
+ const cacheControl = getCacheControl(part) ?? (
584
+ isLastPart ? getCacheControl(message) : undefined
585
+ )
586
+
587
+ const result = part.result._tag === "Right" ? part.result.right : part.result.left
588
+ const isError = part.result._tag === "Left"
577
589
  content.push({
578
590
  type: "tool_result",
579
591
  tool_use_id: part.id,
580
- content: JSON.stringify(part.result)
592
+ content: JSON.stringify(result),
593
+ is_error: isError,
594
+ cache_control: cacheControl
581
595
  })
582
596
  }
583
597
 
@@ -675,24 +689,23 @@ const prepareMessages: (options: LanguageModel.ProviderOptions) => Effect.Effect
675
689
  }
676
690
 
677
691
  case "tool-result": {
692
+ const result = part.result._tag === "Right"
693
+ ? part.result.right
694
+ : part.result.left
678
695
  if (part.name === "AnthropicCodeExecution") {
679
- if (Predicate.hasProperty(part.result, "right")) {
680
- content.push({
681
- type: "code_execution_tool_result",
682
- tool_use_id: part.id,
683
- content: part.result.right as any,
684
- cache_control: cacheControl
685
- })
686
- }
696
+ content.push({
697
+ type: "code_execution_tool_result",
698
+ tool_use_id: part.id,
699
+ content: result as any,
700
+ cache_control: cacheControl
701
+ })
687
702
  } else if (part.name === "AnthropicWebSearch") {
688
- if (Predicate.hasProperty(part.result, "right")) {
689
- content.push({
690
- type: "web_search_tool_result",
691
- tool_use_id: part.id,
692
- content: part.result.right as any,
693
- cache_control: cacheControl
694
- })
695
- }
703
+ content.push({
704
+ type: "web_search_tool_result",
705
+ tool_use_id: part.id,
706
+ content: result as any,
707
+ cache_control: cacheControl
708
+ })
696
709
  } else {
697
710
  return yield* new AiError.MalformedInput({
698
711
  module: "AnthropicLanguageModel",
@@ -828,31 +841,63 @@ const makeResponse: (
828
841
  break
829
842
  }
830
843
 
831
- case "code_execution_tool_result":
832
- case "bash_code_execution_tool_result":
833
- case "text_editor_code_execution_tool_result": {
844
+ case "bash_code_execution_tool_result": {
845
+ const result = part.content.type === "bash_code_execution_result"
846
+ ? { _tag: "Right", right: part.content } as const
847
+ : { _tag: "Left", left: part.content } as const
848
+ parts.push({
849
+ type: "tool-result",
850
+ id: part.tool_use_id,
851
+ name: "AnthropicCodeExecution",
852
+ result,
853
+ providerName: "code_execution",
854
+ providerExecuted: true
855
+ })
856
+ break
857
+ }
858
+
859
+ case "code_execution_tool_result": {
860
+ const result = part.content.type === "code_execution_result"
861
+ ? { _tag: "Right", right: part.content } as const
862
+ : { _tag: "Left", left: part.content } as const
834
863
  parts.push({
835
864
  type: "tool-result",
836
865
  id: part.tool_use_id,
837
866
  name: "AnthropicCodeExecution",
838
- result: part.content,
867
+ result,
839
868
  providerName: "code_execution",
840
869
  providerExecuted: true
841
870
  })
871
+ break
872
+ }
842
873
 
874
+ case "text_editor_code_execution_tool_result": {
875
+ const result = part.content.type === "text_editor_code_execution_tool_result_error"
876
+ ? { _tag: "Left", left: part.content } as const
877
+ : { _tag: "Right", right: part.content } as const
878
+ parts.push({
879
+ type: "tool-result",
880
+ id: part.tool_use_id,
881
+ name: "AnthropicCodeExecution",
882
+ result,
883
+ providerName: "code_execution",
884
+ providerExecuted: true
885
+ })
843
886
  break
844
887
  }
845
888
 
846
889
  case "web_search_tool_result": {
890
+ const result = Array.isArray(part.content)
891
+ ? { _tag: "Right", right: part.content } as const
892
+ : { _tag: "Left", left: part.content } as const
847
893
  parts.push({
848
894
  type: "tool-result",
849
895
  id: part.tool_use_id,
850
896
  name: "AnthropicWebSearch",
851
- result: part.content,
897
+ result,
852
898
  providerName: "web_search",
853
899
  providerExecuted: true
854
900
  })
855
-
856
901
  break
857
902
  }
858
903
  }
@@ -1088,31 +1133,71 @@ const makeStreamResponse: (
1088
1133
  break
1089
1134
  }
1090
1135
 
1091
- case "code_execution_tool_result":
1092
- case "bash_code_execution_tool_result":
1093
- case "text_editor_code_execution_tool_result": {
1136
+ case "bash_code_execution_tool_result": {
1137
+ const toolUseId = event.content_block.tool_use_id
1138
+ const content = event.content_block.content
1139
+ const result = content.type === "bash_code_execution_result"
1140
+ ? { _tag: "Right", right: content } as const
1141
+ : { _tag: "Left", left: content } as const
1142
+ parts.push({
1143
+ type: "tool-result",
1144
+ id: toolUseId,
1145
+ name: "AnthropicCodeExecution",
1146
+ result,
1147
+ providerName: "code_execution",
1148
+ providerExecuted: true
1149
+ })
1150
+ break
1151
+ }
1152
+
1153
+ case "code_execution_tool_result": {
1154
+ const toolUseId = event.content_block.tool_use_id
1155
+ const content = event.content_block.content
1156
+ const result = content.type === "code_execution_result"
1157
+ ? { _tag: "Right", right: content } as const
1158
+ : { _tag: "Left", left: content } as const
1094
1159
  parts.push({
1095
1160
  type: "tool-result",
1096
- id: event.content_block.tool_use_id,
1161
+ id: toolUseId,
1097
1162
  name: "AnthropicCodeExecution",
1098
- result: event.content_block.content,
1163
+ result,
1099
1164
  providerName: "code_execution",
1100
1165
  providerExecuted: true
1101
1166
  })
1167
+ break
1168
+ }
1102
1169
 
1170
+ case "text_editor_code_execution_tool_result": {
1171
+ const toolUseId = event.content_block.tool_use_id
1172
+ const content = event.content_block.content
1173
+ const result = content.type === "text_editor_code_execution_tool_result_error"
1174
+ ? { _tag: "Left", left: content } as const
1175
+ : { _tag: "Right", right: content } as const
1176
+ parts.push({
1177
+ type: "tool-result",
1178
+ id: toolUseId,
1179
+ name: "AnthropicCodeExecution",
1180
+ result,
1181
+ providerName: "code_execution",
1182
+ providerExecuted: true
1183
+ })
1103
1184
  break
1104
1185
  }
1105
1186
 
1106
1187
  case "web_search_tool_result": {
1188
+ const toolUseId = event.content_block.tool_use_id
1189
+ const content = event.content_block.content
1190
+ const result = Array.isArray(content)
1191
+ ? { _tag: "Right", right: content } as const
1192
+ : { _tag: "Left", left: content } as const
1107
1193
  parts.push({
1108
1194
  type: "tool-result",
1109
- id: event.content_block.tool_use_id,
1195
+ id: toolUseId,
1110
1196
  name: "AnthropicWebSearch",
1111
- result: event.content_block.content,
1197
+ result,
1112
1198
  providerName: "web_search",
1113
1199
  providerExecuted: true
1114
1200
  })
1115
-
1116
1201
  break
1117
1202
  }
1118
1203
  }