niadraMiddleware(session) is a language model middleware for wrapLanguageModel({ model, middleware }): the AI SDK’s idiomatic path, which works with every provider without a separate provider package. niadraTools(session) returns the history kit as AI SDK tools. TypeScript only.
Install
@niadra/sdk on npm.
The five primitives
Minimal example
examples/ai-sdk.ts.
Agent memory
niadraMiddleware(convo, { agentMemory: true }) puts the agent’s own notes before the customer’s context, and niadraTools(convo, { agentMemory: { write: true } }) adds the two tools. See Agent memory.
Turn records
With theturns feature on in the space (it ships on), niadraMiddleware records each model call in the turn in progress, recordTools(tools) records each tool call with the provider’s call id, and niadraTurn(convo) keeps a streamText turn open until the stream finishes. A run inside a turn in progress records into it. A turn never waits for Niadra and never repeats the conversation’s text.
Limits
- Nothing here fails the model call: a context that does not arrive is left out, and a failure to record is logged without content.
- The middleware does not wrap the provider: any AI SDK
LanguageModel, from any provider, works. - Tested against
ai7 (with the types of versions 5 and 6) and a fake model, with Niadra on the emulator, on Node, Deno, Bun, workerd and the Edge Runtime.
Next steps
Mastra
the same design, as an agent processor.
TypeScript SDK
the conversation, the
suffix and markInjected().
