relay/prompts

Prompts a server lists and renders, for prompts/list and prompts/get.

prompt(name, arguments, get) declares a prompt; its handler receives the context and the string arguments and returns a PromptResult. prompt_call mirrors relay/tool.handle_call: the handler receives the relay/tool.Call and may return tool.request_input(..) to ask the client for another input round. A handler’s error is hidden from the client, which sees the JSON-RPC invalid-params error.

Attach prompts with relay/server.with_prompts; a client lists them as Declaration values and renders one with relay/client.get_prompt.

import gleam/dict
import gleam/option.{None}
import gleam/result
import relay/content
import relay/prompts

pub fn review() -> prompts.Prompt(Nil) {
  prompts.prompt("review", [prompts.required_argument("code")], fn(_, args) {
    use code <- result.map(dict.get(args, "code"))
    prompts.PromptResult(None, [prompts.user_message("Review:\n" <> code)], [])
  })
}

Types

A prompt as prompts/list publishes it. Read fields by label.

pub type Declaration {
  Declaration(
    name: String,
    title: option.Option(String),
    description: option.Option(String),
    arguments: List(PromptArgument),
    icons: List(content.Icon),
    meta: List(#(String, value.Value)),
  )
}

Constructors

A prompt bound to its handler.

pub type Prompt(context) =
  @internal Prompt(context)

One argument a prompt accepts.

pub type PromptArgument {
  PromptArgument(
    name: String,
    title: option.Option(String),
    description: option.Option(String),
    required: Bool,
  )
}

Constructors

One message of a rendered prompt.

pub type PromptMessage {
  PromptMessage(
    role: content.Role,
    content: content.ContentBlock,
  )
}

Constructors

A rendered prompt: the GetPromptResult of the pinned revision.

pub type PromptResult {
  PromptResult(
    description: option.Option(String),
    messages: List(PromptMessage),
    meta: List(#(String, value.Value)),
  )
}

Constructors

Values

pub fn argument(name: String) -> PromptArgument

An optional argument with only its name.

pub fn assistant_message(text: String) -> PromptMessage

An assistant message with one text block.

pub fn prompt(
  name: String,
  arguments: List(PromptArgument),
  get: fn(context, dict.Dict(String, String)) -> Result(
    PromptResult,
    error,
  ),
) -> Prompt(context)

A prompt whose handler renders it from the context and its arguments.

pub fn prompt_call(
  name: String,
  arguments: List(PromptArgument),
  get: fn(tool.Call(context), dict.Dict(String, String)) -> Result(
    tool.Reply(PromptResult),
    error,
  ),
) -> Prompt(context)

A prompt whose handler receives the relay/tool.Call and may request another input round with tool.request_input.

pub fn required_argument(name: String) -> PromptArgument

A required argument with only its name.

pub fn user_message(text: String) -> PromptMessage

A user message with one text block.

pub fn with_description(
  prompt: Prompt(context),
  description: String,
) -> Prompt(context)

Sets the description.

pub fn with_icons(
  prompt: Prompt(context),
  icons: List(content.Icon),
) -> Prompt(context)

Replaces the icons.

pub fn with_meta(
  prompt: Prompt(context),
  meta: List(#(String, value.Value)),
) -> Prompt(context)

Replaces the declaration’s _meta members.

pub fn with_title(
  prompt: Prompt(context),
  title: String,
) -> Prompt(context)

Sets the human-readable title.

✨ Search Document