Module Dsml.CodecSource

Codec

A bidirectional combinator codec for the DSML tool-call grammar, in the style of Json.Codec: one description both decodes a tool-call block to OCaml values and encodes them back.

type edit = { path : string; line : int }

let edit_codec =
  let open Dsml.Codec in
  Invoke.map "edit" (fun path line -> { path; line })
  |> Invoke.param ~enc:(fun e -> e.path) "path" string
  |> Invoke.param ~enc:(fun e -> e.line) "line" int
  |> Invoke.seal

let block =
  Dsml.Codec.encode edit_codec [ { path = "/tmp/x.c"; line = 42 } ]

let calls = Dsml.Codec.decode edit_codec block
Sourcetype 'a value

A codec for one parameter value.

Sourceval string : string value

string is a raw-text parameter (string="true").

Sourceval bool : bool value

bool is a JSON boolean parameter.

Sourceval int : int value

int is a JSON integer parameter.

Sourceval float : float value

float is a JSON number parameter.

Sourceval json : Json.t value

json is any JSON parameter (or a JSON string).

Sourceval map_value : dec:('a -> 'b) -> enc:('b -> 'a) -> 'a value -> 'b value

map_value ~dec ~enc v adapts the value codec v to another type.

Sourcetype 'a t

A codec for one tool invocation, producing a value of type 'a.

Sourcemodule Invoke : sig ... end

Build an invoke codec member-by-member, like Json.Codec.Object.

Sourceval dynamic : tool_call t

dynamic decodes/encodes any tool, mapping parameters to JSON arguments.

Sourceval map : dec:('a -> 'b) -> enc:('b -> 'a) -> 'a t -> 'b t

map ~dec ~enc c adapts an invoke codec to another type.

Sourceval name : 'a t -> string

name c is the tool name c decodes and encodes ("" for dynamic and choice).

Sourceval schema : 'a t -> Json.t

schema c is the JSON Schema for c's parameters object, with one property per Invoke.param and a required list. Pass it to Tool.v to advertise the tool to the model.

Sourceval decode_arguments : 'a t -> string -> ('a, string) result

decode_arguments c arguments decodes one tool call's JSON-object arguments string (as carried by tool_call) through c, returning the typed value or an error message.

Sourcetype 'b case

A case in a choice: one tool name handled by a typed codec.

Sourceval case : inject:('a -> 'b) -> project:('b -> 'a option) -> 'a t -> 'b case

case ~inject ~project c makes invoke codec c a case of a sum type 'b: inject lifts a decoded value in, project selects it for encoding.

Sourceval choice : ?default:'b t -> 'b case list -> 'b t

choice ?default cases dispatches an invocation on its tool name to the matching case; default handles unmatched names (e.g. dynamic).

Sourceval decode : 'a t -> string -> ('a list, string) result

decode c s decodes the tool-call block found in s with c.

Sourceval encode : 'a t -> 'a list -> string

encode c calls renders calls as a <|DSML|tool_calls> block.