Skip to content

[ai] kyo-ai: a typed effect module for LLM generation - #1707

Merged
fwbrasil merged 6 commits into
mainfrom
kyo-ai
Jun 27, 2026
Merged

fwbrasil merged 6 commits into
mainfrom
kyo-ai

Conversation

@fwbrasil

Copy link
Copy Markdown
Collaborator

Problem

kyo had no first-class way to call a language model. Using an LLM meant reaching for a mainstream SDK and writing the orchestration by hand: authoring JSON schemas, running the tool-call loop, threading the message list, parsing streaming deltas, and decoding replies.

Solution

Add kyo-ai, a module where a call to a language model is a typed value you compose, not a request you orchestrate. You declare the result type and the capabilities the model may use; the module derives the schema, runs the loop, and decodes the reply.

  • AI.gen[A] returns a typed A: the result schema is derived from A derives Schema, the model is forced to that shape, and the reply is decoded. AI is a first-class instance value (one conversation slot); LLM is a custom ArrowEffect whose single world-reaching op runs the eval loop.
  • A composable generation surface, each enabled via AI.enable: Tool (a typed function the model may call, with an async body), Prompt (instructions), Thought (typed reasoning woven into the result schema), Mode (generation-interception middleware).
  • Agent: an actor-backed conversational entity on kyo-actor that remembers across asks.
  • Streaming: ai.stream[A] in two forms inferred from A, incremental text for String (token by token) and object by object for any other type (each complete element emitted once).
  • Providers: an OpenAI-compatible backend (OpenAI, DeepSeek, Gemini, Groq, Baseten, OpenRouter) and the Anthropic Messages backend, both on kyo-http, with auto-config from environment API keys. Temperature is opt-in.
  • A typed failure hierarchy (AIException), the Config/Context/Image value types, and cross-run misuse detection.

Supporting kyo-schema change the module builds on: a @doc annotation for JSON Schema field descriptions, recorded on each field during derivation and surfaced in the generated schema.

Notes

  • Cross-platform: JVM, Scala.js, and Scala Native.
  • Tested with an in-process server implementing both wire protocols, plus eight runnable *Demo.scala exemplars validated live against OpenAI and Anthropic.

fwbrasil and others added 2 commits June 23, 2026 22:47
## Problem

kyo had no first-class way to call a language model. Using an LLM meant
reaching for a mainstream SDK and writing the orchestration by hand: authoring
JSON schemas, running the tool-call loop, threading the message list, parsing
streaming deltas, and decoding replies.

## Solution

Add kyo-ai, a module where a call to a language model is a typed value you
compose, not a request you orchestrate. You declare the result type and the
capabilities the model may use; the module derives the schema, runs the loop,
and decodes the reply.

- `AI.gen[A]` returns a typed `A`: the result schema is derived from
  `A derives Schema`, the model is forced to that shape, and the reply is
  decoded. `AI` is a first-class instance value (one conversation slot); `LLM`
  is a custom `ArrowEffect` whose single world-reaching op runs the eval loop.
- A composable generation surface, each enabled via `AI.enable`: `Tool` (a
  typed function the model may call, with an async body), `Prompt`
  (instructions), `Thought` (typed reasoning woven into the result schema),
  `Mode` (generation-interception middleware).
- `Agent`: an actor-backed conversational entity on kyo-actor that remembers
  across `ask`s.
- Streaming: `ai.stream[A]` in two forms inferred from `A`, incremental text
  for `String` (token by token) and object by object for any other type (each
  complete element emitted once).
- Providers: an OpenAI-compatible backend (OpenAI, DeepSeek, Gemini, Groq,
  Baseten, OpenRouter) and the Anthropic Messages backend, both on kyo-http,
  with auto-config from environment API keys. Temperature is opt-in.
- A typed failure hierarchy (`AIException`), the `Config`/`Context`/`Image`
  value types, and cross-run misuse detection.

Supporting kyo-schema change the module builds on: a `@doc` annotation for
JSON Schema field descriptions, recorded on each field during derivation and
surfaced in the generated schema.

## Notes

- Cross-platform: JVM, Scala.js, and Scala Native.
- Tested with an in-process server implementing both wire protocols, plus eight
  runnable `*Demo.scala` exemplars validated live against OpenAI and Anthropic.
// The message `content` is polymorphic on the wire: a bare string for text messages, a parts array for
// vision messages. It is carried as a Structure.Value, whose identity Schema writes shape-aware (a string
// stays a string, a sequence becomes an array), reproducing both shapes from one field with no union.
case class MessageEntry(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can this be @CamelCase or whatever? now that we have the schema control? @DamianReeves

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I believe we'd need #1713 and use an untagged union here.

Comment on lines +84 to +85
/** A provider: its display name, base URL, the key env-var name, and the wire completion backend. */
trait Provider(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

any reason to choose a trait over an abstract class?

fwbrasil and others added 4 commits June 24, 2026 14:09
## Problem

kyo-ai cross-built only to JVM, JS, and Native, even though all of its
dependencies (kyo-core, kyo-schema, kyo-http, kyo-actor) already target
Wasm. That left it the lone app module on the AI/MCP boundary without a
Wasm artifact.

## Solution

Add WasmPlatform and `wasm-settings` to the kyo-ai crossProject and
register `kyo-ai`.wasm in the kyoWasm aggregate, mirroring kyo-jsonrpc-http
(a Wasm crossProject that also depends on kyo-http). Source is shared-only,
so no platform tree was added. The CONTRIBUTING platform notes now list Wasm.

## Notes

Validated with `kyo-aiWasm/test`: 216 tests, all passing on Node, including
the in-process TestCompletionServer binding and the OpenAI/Anthropic
completion, streaming, and agent paths.
Provider carries constructor parameters and is extended only by single-inheritance
case objects, so an abstract class is the idiomatic base over a parameterized trait.
Addresses review feedback on #1707.
@fwbrasil
fwbrasil merged commit 14ede8c into main Jun 27, 2026
5 of 14 checks passed
@fwbrasil
fwbrasil deleted the kyo-ai branch June 27, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants