Skip to content
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
68f8f3a
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
231fb9d
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
d55d04a
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
092a55f
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
594ac6e
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
fffdcfe
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
44e7e1a
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
b768904
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
f81965d
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
938fd61
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
38fe27c
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
77bfc6a
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
9be498c
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
c43fae6
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
bd0a6f6
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
6b21f96
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
cdaee21
WIP: T-Anthropic-Wire coproduct slice 2 — Anthropic tool_result_conte…
briansrls May 10, 2026
8c48e6d
test(v3): refresh handwritten parse corpus manifest
briansrls May 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions dsl/extdeps/llm/anthropic.dag
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//
module extdeps.llm.anthropic

import extdeps.llm.llm { TokenUsage, llm_snake_wire_contract, ContentBlock }
import extdeps.llm.llm { TokenUsage, llm_snake_wire_contract, ImageSource }
import std.errors { AnthropicErrorShape }
import std.credentials { CredentialSource, EnvVar }
import std.serialization { CoproductWireContract, VariantEncoding }
Expand All @@ -32,6 +32,29 @@ data anthropic_assistant_content_block_wire_contract: CoproductWireContract = {
encoding: InternallyTaggedObject { tag_field: "type", naming: StripPrefixSuffixAndSnakeCase { prefix: "Assistant", suffix: "Block" } }
}

// Wire: tool_result `content` is either a scalar UTF-8 string or a JSON array of
// typed content blocks. CoproductWireContract + `UntaggedJsonStringOrArray`
// captures that dispatch for the v2 Rust emitter.
data anthropic_tool_result_content_wire_contract: CoproductWireContract = {
coproduct: "AnthropicToolResultContent",
encoding: UntaggedJsonStringOrArray {
string_variant: "ToolResultText",
string_field: "text",
array_variant: "ToolResultBlocks",
array_field: "blocks"
}
}

// Nested blocks inside `ToolResultBlocks.blocks[]`: Anthropic-only kinds plus
// mirrored text/image coordinates (shared `ContentBlock` stays in `llm.dag`).
// 🟡 SCAFFOLD — trigger: Anthropic adds/removes tool-result nested block kinds
// beyond document, search_result, tool_reference; tighten to 🟢 when the
// Messages input block set is closed for modeled models.
data anthropic_tool_result_block_wire_contract: CoproductWireContract = {
coproduct: "AnthropicToolResultBlock",
encoding: InternallyTaggedObject { tag_field: "type", naming: StripPrefixSuffixAndSnakeCase { prefix: "Anthropic", suffix: "Block" } }
}

data anthropic_messages_200_content_block_wire_contract: CoproductWireContract = {
coproduct: "AnthropicMessages200ContentBlock",
encoding: InternallyTaggedObject { tag_field: "type", naming: StripPrefixSuffixAndSnakeCase { prefix: "Messages", suffix: "Block" } }
Expand Down Expand Up @@ -60,18 +83,22 @@ data models: List<AnthropicModelSpec> = [
// -- Anthropic content blocks (provider-specific) ---------------------
// Extends the shared ContentBlock (llm.dag) with tool-use blocks.

// 🟢 TERMINAL provider-domain coproduct for tool-result content. Anthropic
// accepts either a scalar text result or a nested content-block list for image
// capable tool responses. This keeps scalar text as an explicit arm and reuses
// the shared `ContentBlock` coordinates for nested text/image blocks. Document,
// search-result, and tool-reference nested blocks remain gap-carried below.
// 🟢 TERMINAL provider-domain coproduct for nested tool-result blocks. Text/image
// mirror shared wire tags; document, search_result, and tool_reference are
// Anthropic-only arms (see `anthropic_tool_result_block_wire_contract`).
type AnthropicToolResultBlock

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: AnthropicToolResultBlock is marked terminal, but the current Claude API reference documents optional nested-block fields such as cache_control plus document citations/title/context, so extdeps fidelity is not tracked at the new authority.

= AnthropicTextBlock { text: String }
| AnthropicImageBlock { source: ImageSource }
| AnthropicDocumentBlock { source: Json }
| AnthropicSearchResultBlock { content: List<AnthropicTextBlock>, source: String, title: String }
| AnthropicToolReferenceBlock { tool_name: String }

// 🟢 TERMINAL provider-domain coproduct for tool-result content. Scalar text vs
// nested block-array dispatch is ratcheted by `anthropic_tool_result_content_wire_contract`
// (`UntaggedJsonStringOrArray`); nested rows use `AnthropicToolResultBlock`.
type AnthropicToolResultContent
= ToolResultText { text: String }
| ToolResultBlocks { blocks: List<ContentBlock> }

data structural_coverage_gap_anthropic_tool_result_additive_blocks: List<String> = [
"closure:anthropic_tool_result_additive_blocks|AnthropicToolResultContent.ToolResultBlocks|missing:document|missing:search_result|missing:tool_reference"
]
| ToolResultBlocks { blocks: List<AnthropicToolResultBlock> }

// 🟢 TERMINAL provider-domain coproduct for user-message content. `UserTextBlock`
// and `UserToolResultBlock` are role-legal Anthropic Messages content rows; the
Expand Down Expand Up @@ -223,17 +250,12 @@ data structural_coverage_gap_anthropic_messages_200_residual: List<String> = [
// Request wire: Messages API expects string `role` + flat `content[]` objects
// keyed by `type` (https://docs.anthropic.com/en/api/messages). These rows are
// now source-level `CoproductWireContract` authorities above; the v2 Rust
// emitter consumes them directly for serde attributes. Nested tool-result
// `content` scalar-vs-block-array wire shape remains gap-carried separately
// below; these paid rows cover the outer request coproduct discriminators only.
// emitter consumes them directly for serde attributes.
data rest_request_wire_serde_alignment_receipt: List<String> = [
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicChatMessage|paid:role_internally_tagged_object_user_assistant",
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicUserContentBlock|paid:type_internally_tagged_object_text_tool_result",
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicAssistantContentBlock|paid:type_internally_tagged_object_text_tool_use"
]

data structural_coverage_gap_anthropic_tool_result_content_wire_shape: List<String> = [
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicUserContentBlock.UserToolResultBlock.content|residue:tool_result_content_must_serialize_scalar_text_or_content_block_array|trigger:emitted serde for AnthropicToolResultContent serializes ToolResultText as scalar string and ToolResultBlocks as content block array"
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicAssistantContentBlock|paid:type_internally_tagged_object_text_tool_use",
"closure:rest_request_wire_serde_alignment|llm.Anthropic.Messages|AnthropicUserContentBlock.UserToolResultBlock.content|paid:tool_result_content_scalar_or_block_array"
]

// -- Service: REST transport wiring ------------------------------
Expand Down
11 changes: 11 additions & 0 deletions dsl/std/serialization.dag
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ type VariantNaming
// UntaggedVariant: serde-style untagged alternatives. Use only when the wire
// shape is determined by payload JSON shape rather than a discriminator field
// (for example string-or-array provider request fields).
// UntaggedJsonStringOrArray: JSON union where one arm is a bare string and the
// other is a bare JSON array (Anthropic `tool_result.content`). The Rust emitter
// lowers the two named variants to `#[serde(untagged)]` tuple payloads so the
// string arm serializes as a scalar and the array arm as `Vec<...>` without a
// wrapper object (`{"blocks":[...]}` is rejected on the wire).
type VariantEncoding
= StringVariant { naming: VariantNaming }
| InternallyTaggedObject { tag_field: String, naming: VariantNaming }
| UntaggedVariant
| TaggedVariant
| UntaggedJsonStringOrArray {
string_variant: String
string_field: String
array_variant: String
array_field: String
}

// Declaration-level override. Selection order is:
// 1. CoproductWireContract for the exact coproduct declaration.
Expand Down
4 changes: 4 additions & 0 deletions src/v2/04_emit_info.dag
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type EmitGraphInfo {
// Used by pattern emission to decide Variant vs Variant { .. }.
// Keys are "EnumName::VariantName" for variants that carry fields.
fielded_variants: Set<String>
// Variants emitted as single-field tuple forms for `UntaggedJsonStringOrArray`
// wire contracts (same `EnumName::VariantName` keys as fielded_variants).
untagged_json_tuple_variants: Set<String>
// Per-type sharing decision: type names that need shared-pointer
// wrapping (Rc in Rust). Computed from type_summaries ×
// recursive_type_set. The Rust emitter also adds collection types.
Expand Down Expand Up @@ -117,6 +120,7 @@ fn empty_emit_graph_info() -> EmitGraphInfo {
type_summaries: empty_map(),
recursive_type_set: empty_set(),
fielded_variants: empty_set(),
untagged_json_tuple_variants: empty_set(),
shared_types: empty_set(),
ownership_index: empty_map(),
movable: empty_set(),
Expand Down
61 changes: 61 additions & 0 deletions src/v2/04_infer.dag
Original file line number Diff line number Diff line change
Expand Up @@ -5929,6 +5929,65 @@ fn collect_parent_envs(resolved: ResolvedModule, module_index: Map<String, Typed
ParentModulesResult { modules: modules, diagnostics: diagnostics }
}

fn infer_literal_string_value(n: Node) -> String? {
match n.expr_data {
ExprLiteral { value: lv } => match lv {
LitStr { value: s } => Some { value: s }
_ => none
}
_ => none
}
}

fn infer_field_value_by_name(record: Node, field_name: String, source_indices: Map<String, NewlineIndex>) -> Node? {
match record.children |> filter(fi => field_init_node_name_at(n: fi, source_indices: source_indices) == field_name) |> first {
Some { value: fi } => Some { value: field_init_node_value(n: fi) }
None => none
}
}

fn infer_record_string_field(record: Node, field_name: String, source_indices: Map<String, NewlineIndex>) -> String? {
match infer_field_value_by_name(record: record, field_name: field_name, source_indices: source_indices) {
Some { value: vn } => infer_literal_string_value(n: vn)
None => none
}
}

fn build_untagged_json_tuple_variants(modules: List<TypedModule>) -> Set<String> {
modules |> fold(init: empty_set(), f: (acc, m) =>
let si = m.type_env.source_indices
m.items |> fold(init: acc, f: (inner, item) =>
if (item_kind(item: item) == DataItem) {
match item.body {
Some { value: body } =>
match infer_record_string_field(record: body, field_name: "coproduct", source_indices: si) {
Some { value: coproduct_name } =>
match infer_field_value_by_name(record: body, field_name: "encoding", source_indices: si) {
Some { value: enc_node } =>
if authored_name_at(source_indices: si, node: enc_node) == "UntaggedJsonStringOrArray" {
match infer_record_string_field(record: enc_node, field_name: "string_variant", source_indices: si) {
Some { value: sv } =>
match infer_record_string_field(record: enc_node, field_name: "array_variant", source_indices: si) {
Some { value: av } =>
let k1 = concat(coproduct_name, "::", sv)
let k2 = concat(coproduct_name, "::", av)
set_insert(set_insert(inner, k1), k2)
None => inner
}
None => inner
}
} else { inner }
None => inner
}
None => inner
}
None => inner
}
} else { inner }
)
)
}


fn build_fielded_variants(modules: List<TypedModule>, type_summaries: Map<String, TypeSummary>) -> Set<String> {
let result = modules |> fold(init: empty_set(), f: (acc, m) =>
Expand Down Expand Up @@ -5971,11 +6030,13 @@ fn build_emit_graph_info(modules: List<TypedModule>) -> EmitGraphInfo {
)
)
let fielded = build_fielded_variants(modules: modules, type_summaries: built.type_summaries)
let untagged_tuples = build_untagged_json_tuple_variants(modules: modules)
let vtoe = derive_variant_to_enum(type_summaries: built.type_summaries)
EmitGraphInfo {
type_summaries: built.type_summaries,
recursive_type_set: all_recursive,
fielded_variants: fielded,
untagged_json_tuple_variants: untagged_tuples,
shared_types: empty_set(),
ownership_index: empty_map(),
movable: empty_set(),
Expand Down
Loading
Loading