@strav/brain 1.0.0-alpha.15 → 1.0.0-alpha.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strav/brain",
3
- "version": "1.0.0-alpha.15",
3
+ "version": "1.0.0-alpha.17",
4
4
  "description": "Strav AI module — unified Provider interface, BrainManager, threads, prompt caching, tools / agents / MCP. Anthropic + OpenAI providers; Gemini / DeepSeek follow.",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -24,7 +24,7 @@
24
24
  "@anthropic-ai/sdk": "^0.100.0",
25
25
  "@google/genai": "^2.7.0",
26
26
  "@modelcontextprotocol/sdk": "^1.29.0",
27
- "@strav/kernel": "1.0.0-alpha.15",
27
+ "@strav/kernel": "1.0.0-alpha.17",
28
28
  "openai": "^6.0.0"
29
29
  },
30
30
  "peerDependencies": {
package/src/agent.ts CHANGED
@@ -21,17 +21,30 @@
21
21
  * .run()
22
22
  * ```
23
23
  *
24
- * V1 makes the configuration declarative-only apps that need
25
- * runtime knobs (per-request model overrides, dynamic tool sets)
26
- * use `BrainManager.runTools(...)` directly. Adding per-instance
27
- * overrides on the Agent class is a future ergonomic slice.
24
+ * Structured output (typed result without `.output(schema)` at the call site):
25
+ *
26
+ * ```ts
27
+ * class CityAgent extends Agent<CityAnswer> {
28
+ * override readonly instructions = 'You only emit verified city data.'
29
+ * override readonly outputSchema = citySchema // OutputSchema<CityAnswer>
30
+ * }
31
+ *
32
+ * const { value } = await brain.agent(CityAgent).input('Capital of France?').run()
33
+ * // ^? CityAnswer — runner is typed from the class generic
34
+ * ```
35
+ *
36
+ * The generic threads `T` through `BrainManager.agent(Class)` →
37
+ * `AgentRunner<T>` → `AgentGenerateResult<T>`. Subclasses that
38
+ * don't declare an output type stay `Agent<never>` and `run()`
39
+ * returns `AgentResult` exactly as before.
28
40
  */
29
41
 
30
42
  import type { MCPServer } from './mcp_server.ts'
43
+ import type { OutputSchema } from './output_schema.ts'
31
44
  import type { ModelTier } from './types.ts'
32
45
  import type { Tool } from './tool.ts'
33
46
 
34
- export abstract class Agent {
47
+ export abstract class Agent<T = never> {
35
48
  /** System prompt — the persona / instructions Claude sees on every turn. */
36
49
  abstract readonly instructions: string
37
50
 
@@ -65,4 +78,20 @@ export abstract class Agent {
65
78
 
66
79
  /** Hard cap on per-call response tokens. Default `4096`. */
67
80
  readonly maxTokens: number = 4096
81
+
82
+ /**
83
+ * Structured-output schema. Set on subclasses that extend
84
+ * `Agent<SomeType>` to declare the agent always returns that
85
+ * shape; `BrainManager.agent(Class)` then types the runner
86
+ * automatically so `.run()` returns `AgentGenerateResult<T>`
87
+ * without a per-call `.output(schema)`.
88
+ *
89
+ * Leave unset on `Agent<never>` subclasses (no structured
90
+ * output). The runner falls back to the standard tool-loop path
91
+ * and returns `AgentResult`.
92
+ *
93
+ * Apps that want a per-call override still chain
94
+ * `.output(otherSchema)` — that wins over the class-side value.
95
+ */
96
+ readonly outputSchema?: OutputSchema<T>
68
97
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * `AgentGenerateResult<T>` — what an Agent run returns when the
3
+ * runner was switched into structured-output mode via
4
+ * `.output(schema)`.
5
+ *
6
+ * Combines the structured-output payload (`value` + raw `text`) with
7
+ * the agent-loop bookkeeping (`messages`, `iterations`, `stopReason`,
8
+ * `usage`) so apps can still render the trace + report token spend
9
+ * the same way they do for `AgentResult`. `iterations` is always `0`
10
+ * in V1 because the structured-output path doesn't engage the
11
+ * tool-use loop — see the docs for the (deferred) "tools + schema"
12
+ * combined slice.
13
+ */
14
+
15
+ import type { ChatUsage, Message } from './types.ts'
16
+
17
+ export interface AgentGenerateResult<T = unknown> {
18
+ /** Parsed structured value matching the supplied `OutputSchema<T>`. */
19
+ value: T
20
+ /** Raw JSON text the model produced — handy for logging when `parse` rejects. */
21
+ text: string
22
+ /** Full message history of the run (single user → assistant turn in V1). */
23
+ messages: Message[]
24
+ /** Always `0` in V1 — the schema path doesn't engage the tool-use loop. */
25
+ iterations: number
26
+ /** Provider-reported terminal stop reason. */
27
+ stopReason: string
28
+ /** Token usage from the single underlying `generate` call. */
29
+ usage: ChatUsage
30
+ }
@@ -2,28 +2,54 @@
2
2
  * `AgentRunner` — fluent builder returned by `BrainManager.agent(Class)`.
3
3
  *
4
4
  * Carries the agent instance + an input message + an optional
5
- * per-run context bag. `run()` translates the agent's declarative
6
- * configuration into a `runWithTools` call and returns the
7
- * `AgentResult`.
5
+ * per-run context bag + an optional structured-output schema.
6
+ * `run()` translates the agent's declarative configuration into
7
+ * either a `runWithTools` call (default) or a `generate` call (when
8
+ * `.output(schema)` was used) and returns the matching result type.
9
+ *
10
+ * Designed to chain:
11
+ *
12
+ * ```ts
13
+ * brain.agent(R).input(text).context({...}).run()
14
+ * brain.agent(R).input(text).output(schema).run() // → AgentGenerateResult<T>
15
+ * ```
8
16
  *
9
- * Designed to chain: `brain.agent(R).input(text).context({...}).run()`.
10
17
  * Apps that need the full Message-array surface bypass the runner
11
- * and call `BrainManager.runTools(messages, tools, options)` directly.
18
+ * and call `BrainManager.runTools(messages, tools, options)` or
19
+ * `BrainManager.generate(input, schema, options)` directly.
12
20
  */
13
21
 
14
22
  import type { Agent } from './agent.ts'
23
+ import type { AgentGenerateResult } from './agent_generate_result.ts'
15
24
  import type { AgentResult } from './agent_result.ts'
25
+ import type { AgentStreamEvent } from './agent_stream_event.ts'
16
26
  import type { BrainManager } from './brain_manager.ts'
27
+ import { BrainError } from './brain_error.ts'
28
+ import type { OutputSchema } from './output_schema.ts'
29
+ import type { ChatOptions, Message } from './types.ts'
17
30
  import type { RunWithToolsOptions } from './provider.ts'
18
- import type { Message } from './types.ts'
19
31
 
20
- export class AgentRunner {
32
+ /**
33
+ * Conditional return shape for `AgentRunner.run()`. With the default
34
+ * generic (`T = never`), `run()` returns `AgentResult` — the
35
+ * tool-loop shape. When the runner has been switched into
36
+ * structured-output mode via `.output(schema)`, `T` carries the
37
+ * inferred type and `run()` returns `AgentGenerateResult<T>`.
38
+ *
39
+ * The `[T] extends [never]` form is the standard "is this still the
40
+ * default never?" check — `T extends never` would distribute over
41
+ * union types and break.
42
+ */
43
+ export type AgentRunResult<T> = [T] extends [never] ? AgentResult : AgentGenerateResult<T>
44
+
45
+ export class AgentRunner<T = never> {
21
46
  private prompt: string | undefined
22
47
  private contextBag: Record<string, unknown> = {}
48
+ private schema: OutputSchema<T> | undefined
23
49
 
24
50
  constructor(
25
51
  private readonly brain: BrainManager,
26
- private readonly agent: Agent,
52
+ private readonly agent: Agent<unknown>,
27
53
  ) {}
28
54
 
29
55
  /** Set the user input. Required before `run()`. */
@@ -43,21 +69,121 @@ export class AgentRunner {
43
69
  return this
44
70
  }
45
71
 
46
- async run(): Promise<AgentResult> {
72
+ /**
73
+ * Switch the runner into structured-output mode. `run()` then
74
+ * delegates to `BrainManager.generate(...)` and returns an
75
+ * `AgentGenerateResult<U>` shaped to the supplied schema.
76
+ *
77
+ * V1 caveat: structured output and tool use can't be combined yet.
78
+ * Agents that declare `tools` or `mcpServers` AND call `.output()`
79
+ * throw a `BrainError` at `run()` with a clear "this combination is
80
+ * deferred" message. Apps that need both today run them in two
81
+ * steps — `runTools(...)` for the loop, then `generate(...)` for
82
+ * the structured summary.
83
+ */
84
+ output<U>(schema: OutputSchema<U>): AgentRunner<U> {
85
+ // Mutate in place + cast — the runtime state is a single object;
86
+ // the generic narrows only the static return type. This avoids
87
+ // cloning the prompt + contextBag fields.
88
+ this.schema = schema as unknown as OutputSchema<T>
89
+ return this as unknown as AgentRunner<U>
90
+ }
91
+
92
+ /**
93
+ * Streaming variant of `run()`. Returns an
94
+ * `AsyncIterable<AgentStreamEvent<T>>` — yields text deltas,
95
+ * tool-use/result boundaries, and a terminal `stop` event with
96
+ * the full trace.
97
+ *
98
+ * Default (no `.output(schema)` set): the terminal `stop` has the
99
+ * plain shape and `T` defaults to `never`.
100
+ *
101
+ * With `.output(schema)`: the terminal `stop` event carries the
102
+ * parsed `value: T` + raw `text` alongside the loop bookkeeping,
103
+ * and the runner delegates to
104
+ * `BrainManager.streamGenerateWithTools`.
105
+ */
106
+ stream(): AsyncIterable<AgentStreamEvent<T>> {
47
107
  if (this.prompt === undefined) {
48
- throw new Error('AgentRunner.run: input() must be called before run().')
108
+ throw new BrainError('AgentRunner.stream: input() must be called before stream().')
49
109
  }
50
110
  const messages: Message[] = [{ role: 'user', content: this.prompt }]
51
111
  const options: RunWithToolsOptions = {
112
+ ...this.buildChatOptions(),
113
+ maxIterations: this.agent.maxIterations,
114
+ context: this.contextBag,
115
+ }
116
+ if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
117
+ if (this.schema !== undefined) {
118
+ return this.brain.streamGenerateWithTools<T>(
119
+ messages,
120
+ this.schema,
121
+ this.agent.tools,
122
+ options,
123
+ )
124
+ }
125
+ return this.brain.streamTools(messages, this.agent.tools, options) as AsyncIterable<
126
+ AgentStreamEvent<T>
127
+ >
128
+ }
129
+
130
+ async run(): Promise<AgentRunResult<T>> {
131
+ if (this.prompt === undefined) {
132
+ throw new BrainError('AgentRunner.run: input() must be called before run().')
133
+ }
134
+ const messages: Message[] = [{ role: 'user', content: this.prompt }]
135
+
136
+ if (this.schema !== undefined) {
137
+ const hasTools = this.agent.tools.length > 0 || this.agent.mcpServers.length > 0
138
+ if (hasTools) {
139
+ const toolOptions: RunWithToolsOptions = {
140
+ ...this.buildChatOptions(),
141
+ maxIterations: this.agent.maxIterations,
142
+ context: this.contextBag,
143
+ }
144
+ if (this.agent.mcpServers.length > 0) toolOptions.mcpServers = this.agent.mcpServers
145
+ const result = await this.brain.generateWithTools<T>(
146
+ messages,
147
+ this.schema,
148
+ this.agent.tools,
149
+ toolOptions,
150
+ )
151
+ return result as AgentRunResult<T>
152
+ }
153
+ const generateOptions = this.buildChatOptions()
154
+ const result = await this.brain.generate<T>(messages, this.schema, generateOptions)
155
+ const generateResult: AgentGenerateResult<T> = {
156
+ value: result.value,
157
+ text: result.text,
158
+ messages: [
159
+ ...messages,
160
+ { role: 'assistant', content: result.text },
161
+ ],
162
+ iterations: 0,
163
+ stopReason: result.stopReason ?? 'stop',
164
+ usage: result.usage,
165
+ }
166
+ return generateResult as AgentRunResult<T>
167
+ }
168
+
169
+ const options: RunWithToolsOptions = {
170
+ ...this.buildChatOptions(),
171
+ maxIterations: this.agent.maxIterations,
172
+ context: this.contextBag,
173
+ }
174
+ if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
175
+ const result = await this.brain.runTools(messages, this.agent.tools, options)
176
+ return result as AgentRunResult<T>
177
+ }
178
+
179
+ private buildChatOptions(): ChatOptions {
180
+ const options: ChatOptions = {
52
181
  tier: this.agent.tier,
53
182
  maxTokens: this.agent.maxTokens,
54
183
  system: this.agent.instructions,
55
- maxIterations: this.agent.maxIterations,
56
- context: this.contextBag,
57
184
  }
58
185
  if (this.agent.model !== undefined) options.model = this.agent.model
59
186
  if (this.agent.provider !== undefined) options.provider = this.agent.provider
60
- if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
61
- return this.brain.runTools(messages, this.agent.tools, options)
187
+ return options
62
188
  }
63
189
  }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * `AgentStreamEvent<T>` — the union yielded by streaming agentic
3
+ * runs (`Provider.streamWithTools` / `BrainManager.streamTools` /
4
+ * `AgentRunner.stream`).
5
+ *
6
+ * The generic `T` carries the structured-output type when the run
7
+ * was schema-constrained:
8
+ * - With the default `T = never`, the terminal `stop` event is the
9
+ * plain shape: `{ stopReason, iterations, usage, messages }`.
10
+ * - With `T` set (via `BrainManager.streamGenerateWithTools` or
11
+ * `AgentRunner.stream()` after `.output(schema)`), the `stop`
12
+ * event additionally carries `{ value: T; text: string }` — the
13
+ * parsed JSON shaped to the schema and the raw text the model
14
+ * produced.
15
+ *
16
+ * Event vocabulary:
17
+ *
18
+ * - `iteration_start` — fired before each model call. `iteration`
19
+ * starts at `0` and increments per round.
20
+ * - `text` — a text delta from the assistant turn currently in
21
+ * flight. Same shape as `StreamEvent.text` so apps can reuse
22
+ * UI code.
23
+ * - `tool_use_start` — a tool call has begun streaming. Fires
24
+ * as soon as the model emits the call's `id` + `name`,
25
+ * before the arguments finish streaming. Apps that render
26
+ * "(calling X with …)" indicators show the tool name here.
27
+ * Anthropic + OpenAI emit this from streaming chunks; Gemini
28
+ * doesn't stream tool arguments (parts arrive complete) and
29
+ * skips both `tool_use_start` and `tool_use_delta`.
30
+ * - `tool_use_delta` — a chunk of the tool-call's argument JSON.
31
+ * Apps that render the model composing the call (e.g. typing
32
+ * `search(q='current state of bun.sql ...`) accumulate
33
+ * `argsDelta` per `id` and re-render. The argument JSON is
34
+ * partial / possibly malformed mid-stream — only the final
35
+ * `tool_use` event carries the parsed input.
36
+ * - `tool_use` — the assistant turn finished and the framework
37
+ * parsed a tool call. Emitted with the parsed `input` before
38
+ * the framework runs the tool. Source-of-truth for tool calls;
39
+ * cross-provider consumers can rely on this even when the
40
+ * start/delta events aren't fired.
41
+ * - `tool_result` — the framework executed the tool and is about
42
+ * to feed the result back to the model. `isError` reflects
43
+ * whether the tool reported a failure (V1: only false today —
44
+ * thrown executions abort the loop with `ToolExecutionError`;
45
+ * graceful tool-error recovery is a later slice).
46
+ * - `iteration_end` — the assistant turn fully drained, including
47
+ * its `stopReason`. Apps that render per-iteration boundaries
48
+ * use this.
49
+ * - `stop` — terminal event. Carries the full `messages` trace,
50
+ * iteration count, aggregated usage, and the framework
51
+ * stop reason (`'end_turn'`, `'max_iterations'`, etc.). When the
52
+ * run was schema-constrained, also carries `value` (parsed JSON)
53
+ * and `text` (the raw JSON the model emitted).
54
+ *
55
+ * What's NOT in V1:
56
+ * - `error` events. Failures throw out of the iterator (the
57
+ * consumer's `for await` rejects). Apps that want resilient
58
+ * loops catch around the consumer.
59
+ */
60
+
61
+ import type { ChatUsage, Message } from './types.ts'
62
+
63
+ interface BaseStopEvent {
64
+ type: 'stop'
65
+ stopReason: string
66
+ iterations: number
67
+ usage: ChatUsage
68
+ messages: Message[]
69
+ }
70
+
71
+ interface ValueStopEvent<T> extends BaseStopEvent {
72
+ /** Parsed JSON shaped to the supplied `OutputSchema<T>`. */
73
+ value: T
74
+ /** Raw JSON text the model emitted on its terminal turn. */
75
+ text: string
76
+ }
77
+
78
+ /**
79
+ * The `stop` variant narrows when the stream was schema-constrained
80
+ * — the `[T] extends [never]` form is the standard "is this still
81
+ * the default never?" check (a bare `T extends never` would
82
+ * distribute over union types and break).
83
+ */
84
+ type StopEvent<T> = [T] extends [never] ? BaseStopEvent : ValueStopEvent<T>
85
+
86
+ export type AgentStreamEvent<T = never> =
87
+ | { type: 'iteration_start'; iteration: number }
88
+ | { type: 'text'; delta: string }
89
+ | { type: 'tool_use_start'; id: string; name: string }
90
+ | { type: 'tool_use_delta'; id: string; argsDelta: string }
91
+ | { type: 'tool_use'; id: string; name: string; input: unknown }
92
+ | {
93
+ type: 'tool_result'
94
+ id: string
95
+ name: string
96
+ content: string
97
+ isError: boolean
98
+ }
99
+ | { type: 'iteration_end'; iteration: number; stopReason: string | null }
100
+ | StopEvent<T>
@@ -34,6 +34,31 @@ export interface AnthropicProviderConfig {
34
34
  betas?: readonly string[]
35
35
  }
36
36
 
37
+ /**
38
+ * OpenAI Responses API driver config — backed by the `openai`
39
+ * SDK's `client.responses.create` endpoint. Use when you need
40
+ * OpenAI's server-side tools (web search, code interpreter) or
41
+ * the Responses API's reasoning surfaces. The chat completions
42
+ * provider (`driver: 'openai'`) covers everything else.
43
+ */
44
+ export interface OpenAIResponsesProviderConfig {
45
+ driver: 'openai-responses'
46
+ /** API key. Required. Most apps source from `env('OPENAI_API_KEY')`. */
47
+ apiKey: string
48
+ /** Optional override of the SDK's base URL. */
49
+ baseUrl?: string
50
+ /** Optional organization id. */
51
+ organization?: string
52
+ /** Default model. Defaults to `gpt-5`. */
53
+ defaultModel?: string
54
+ /** Default `max_output_tokens`. Defaults to 4096. */
55
+ defaultMaxTokens?: number
56
+ /** Default embedding model (inherited from chat completions endpoint). Defaults to `text-embedding-3-small`. */
57
+ defaultEmbedModel?: string
58
+ /** Default audio-transcription model. Defaults to `whisper-1`. */
59
+ defaultTranscribeModel?: string
60
+ }
61
+
37
62
  /** OpenAI-specific driver config. */
38
63
  export interface OpenAIProviderConfig {
39
64
  driver: 'openai'
@@ -47,6 +72,10 @@ export interface OpenAIProviderConfig {
47
72
  defaultModel?: string
48
73
  /** Default `max_tokens` for `chat()` calls that don't specify one. */
49
74
  defaultMaxTokens?: number
75
+ /** Default embedding model for `brain.embed(...)`. Defaults to `text-embedding-3-small`. */
76
+ defaultEmbedModel?: string
77
+ /** Default audio-transcription model for `brain.transcribe(...)`. Defaults to `whisper-1`. */
78
+ defaultTranscribeModel?: string
50
79
  }
51
80
 
52
81
  /** Google (Gemini) driver config — backed by `@google/genai`. */
@@ -62,12 +91,73 @@ export interface GeminiProviderConfig {
62
91
  defaultMaxTokens?: number
63
92
  /** Optional API version pin (`v1` / `v1beta`). */
64
93
  apiVersion?: string
94
+ /** Default embedding model for `brain.embed(...)`. Defaults to `text-embedding-004`. */
95
+ defaultEmbedModel?: string
96
+ }
97
+
98
+ /** DeepSeek driver config — backed by the `openai` SDK pointed at DeepSeek's OpenAI-compatible endpoint. */
99
+ export interface DeepSeekProviderConfig {
100
+ driver: 'deepseek'
101
+ /** API key. Required. Most apps source from `env('DEEPSEEK_API_KEY')`. */
102
+ apiKey: string
103
+ /** Optional override of the SDK's base URL. Defaults to `https://api.deepseek.com/v1`. */
104
+ baseUrl?: string
105
+ /** Default model when neither `options.model` nor `options.tier` is passed. Defaults to `deepseek-chat`. */
106
+ defaultModel?: string
107
+ /** Default `max_tokens` for `chat()` calls that don't specify one. */
108
+ defaultMaxTokens?: number
109
+ }
110
+
111
+ /**
112
+ * Ollama driver config — backed by the `openai` SDK pointed at a
113
+ * local Ollama server's OpenAI-compatible `/v1` endpoint. The same
114
+ * shape works against any OpenAI-compatible local server (LM Studio,
115
+ * llama.cpp's server, vLLM, …) by overriding `baseUrl`.
116
+ */
117
+ export interface OllamaProviderConfig {
118
+ driver: 'ollama'
119
+ /**
120
+ * Required — model must be already pulled on the Ollama server
121
+ * (`ollama pull <model>`). No universal default exists because
122
+ * apps install whichever models they need. Common picks for
123
+ * tool-calling: `llama3.2`, `llama3.1`, `qwen2.5`, `mistral`.
124
+ */
125
+ defaultModel: string
126
+ /** Optional override of the SDK's base URL. Defaults to `http://localhost:11434/v1`. */
127
+ baseUrl?: string
128
+ /**
129
+ * Optional API key. Ollama doesn't require one — the SDK demands
130
+ * a non-empty string, so a placeholder is fine and the default
131
+ * (`'ollama'`) works. Override only when running behind a proxy
132
+ * that adds its own auth layer.
133
+ */
134
+ apiKey?: string
135
+ /** Default `max_tokens` for `chat()` calls that don't specify one. */
136
+ defaultMaxTokens?: number
137
+ /**
138
+ * Default embedding model for `brain.embed(...)`. No universal
139
+ * default — apps pull an embedding-tuned model (e.g.
140
+ * `nomic-embed-text`, `mxbai-embed-large`) and reference it here.
141
+ * Calls that omit `options.model` without this set throw.
142
+ */
143
+ defaultEmbedModel?: string
144
+ /**
145
+ * Default audio-transcription model for `brain.transcribe(...)`.
146
+ * No universal default — Ollama versions vary on whether they
147
+ * expose a Whisper-style endpoint, and apps pull whatever
148
+ * model their build supports (e.g. `whisper`). Calls that omit
149
+ * `options.model` without this set throw.
150
+ */
151
+ defaultTranscribeModel?: string
65
152
  }
66
153
 
67
154
  export type ProviderConfig =
68
155
  | AnthropicProviderConfig
69
156
  | OpenAIProviderConfig
70
- | GeminiProviderConfig // | DeepSeekProviderConfig (later slice)
157
+ | OpenAIResponsesProviderConfig
158
+ | GeminiProviderConfig
159
+ | DeepSeekProviderConfig
160
+ | OllamaProviderConfig
71
161
 
72
162
  /** Cache-shape defaults applied when `ChatOptions.cache` is omitted. */
73
163
  export interface BrainCacheConfig {