@strav/brain 1.0.0-alpha.16 → 1.0.0-alpha.18
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 +4 -2
- package/src/agent.ts +34 -5
- package/src/agent_generate_result.ts +2 -0
- package/src/agent_result.ts +7 -0
- package/src/agent_runner.ts +134 -15
- package/src/agent_stream_event.ts +100 -0
- package/src/brain_config.ts +91 -1
- package/src/brain_manager.ts +287 -6
- package/src/brain_provider.ts +25 -1
- package/src/index.ts +37 -2
- package/src/mcp/client.ts +99 -13
- package/src/mcp/index.ts +7 -0
- package/src/mcp/oauth.ts +227 -0
- package/src/mcp/pool.ts +106 -0
- package/src/mcp/resolve_mcp_tools.ts +31 -9
- package/src/mcp_server.ts +16 -0
- package/src/persistence/brain_message.ts +34 -0
- package/src/persistence/brain_message_repository.ts +106 -0
- package/src/persistence/brain_store.ts +166 -0
- package/src/persistence/brain_suspended_run.ts +30 -0
- package/src/persistence/brain_suspended_run_repository.ts +68 -0
- package/src/persistence/brain_thread.ts +30 -0
- package/src/persistence/brain_thread_repository.ts +65 -0
- package/src/persistence/database_brain_store.ts +190 -0
- package/src/persistence/index.ts +48 -0
- package/src/persistence/schema/brain_message_schema.ts +61 -0
- package/src/persistence/schema/brain_suspended_run_schema.ts +58 -0
- package/src/persistence/schema/brain_thread_schema.ts +50 -0
- package/src/persistence/schema/index.ts +3 -0
- package/src/provider.ts +145 -1
- package/src/providers/anthropic_provider.ts +723 -38
- package/src/providers/deepseek_provider.ts +117 -0
- package/src/providers/gemini_provider.ts +625 -33
- package/src/providers/ollama_provider.ts +86 -0
- package/src/providers/openai_compat_provider.ts +616 -0
- package/src/providers/openai_provider.ts +801 -43
- package/src/providers/openai_responses_provider.ts +1015 -0
- package/src/suspended_run.ts +153 -0
- package/src/thread.ts +40 -1
- package/src/tool.ts +7 -0
- package/src/tool_runner.ts +81 -0
- package/src/types.ts +343 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strav/brain",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.18",
|
|
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",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": "./src/index.ts",
|
|
10
10
|
"./mcp": "./src/mcp/index.ts",
|
|
11
|
+
"./persistence": "./src/persistence/index.ts",
|
|
11
12
|
"./zod": "./src/zod/index.ts"
|
|
12
13
|
},
|
|
13
14
|
"files": [
|
|
@@ -24,7 +25,8 @@
|
|
|
24
25
|
"@anthropic-ai/sdk": "^0.100.0",
|
|
25
26
|
"@google/genai": "^2.7.0",
|
|
26
27
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
27
|
-
"@strav/
|
|
28
|
+
"@strav/database": "1.0.0-alpha.18",
|
|
29
|
+
"@strav/kernel": "1.0.0-alpha.18",
|
|
28
30
|
"openai": "^6.0.0"
|
|
29
31
|
},
|
|
30
32
|
"peerDependencies": {
|
package/src/agent.ts
CHANGED
|
@@ -21,17 +21,30 @@
|
|
|
21
21
|
* .run()
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
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
|
}
|
package/src/agent_result.ts
CHANGED
|
@@ -29,4 +29,11 @@ export interface AgentResult {
|
|
|
29
29
|
stopReason: string
|
|
30
30
|
/** Token usage summed across every model call in the loop. */
|
|
31
31
|
usage: ChatUsage
|
|
32
|
+
/**
|
|
33
|
+
* Final provider response id when the provider exposes stateful
|
|
34
|
+
* conversations (OpenAI Responses API). Captured from the last
|
|
35
|
+
* model turn so apps that persist the conversation can resume
|
|
36
|
+
* via `ChatOptions.previousResponseId`. Undefined elsewhere.
|
|
37
|
+
*/
|
|
38
|
+
responseId?: string
|
|
32
39
|
}
|
package/src/agent_runner.ts
CHANGED
|
@@ -22,11 +22,21 @@
|
|
|
22
22
|
import type { Agent } from './agent.ts'
|
|
23
23
|
import type { AgentGenerateResult } from './agent_generate_result.ts'
|
|
24
24
|
import type { AgentResult } from './agent_result.ts'
|
|
25
|
+
import type { AgentStreamEvent } from './agent_stream_event.ts'
|
|
25
26
|
import type { BrainManager } from './brain_manager.ts'
|
|
26
27
|
import { BrainError } from './brain_error.ts'
|
|
27
28
|
import type { OutputSchema } from './output_schema.ts'
|
|
28
|
-
import type {
|
|
29
|
+
import type {
|
|
30
|
+
ChatOptions,
|
|
31
|
+
Message,
|
|
32
|
+
ToolUseBlock,
|
|
33
|
+
} from './types.ts'
|
|
29
34
|
import type { RunWithToolsOptions } from './provider.ts'
|
|
35
|
+
import type {
|
|
36
|
+
SuspendedRun,
|
|
37
|
+
SuspendedState,
|
|
38
|
+
ToolResultInput,
|
|
39
|
+
} from './suspended_run.ts'
|
|
30
40
|
|
|
31
41
|
/**
|
|
32
42
|
* Conditional return shape for `AgentRunner.run()`. With the default
|
|
@@ -41,16 +51,47 @@ import type { RunWithToolsOptions } from './provider.ts'
|
|
|
41
51
|
*/
|
|
42
52
|
export type AgentRunResult<T> = [T] extends [never] ? AgentResult : AgentGenerateResult<T>
|
|
43
53
|
|
|
44
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Conditional return shape that flips when the runner has opted in
|
|
56
|
+
* to suspension via `.suspend(gate)`. The phantom `S` generic on
|
|
57
|
+
* `AgentRunner<T, S>` carries the bit; `S extends true` widens the
|
|
58
|
+
* union so callers must narrow with `isSuspended(...)` before
|
|
59
|
+
* touching `result.value` / `result.text`.
|
|
60
|
+
*/
|
|
61
|
+
export type AgentRunMaybeSuspended<T, S extends boolean> = [S] extends [true]
|
|
62
|
+
? AgentRunResult<T> | SuspendedRun
|
|
63
|
+
: AgentRunResult<T>
|
|
64
|
+
|
|
65
|
+
export class AgentRunner<T = never, S extends boolean = false> {
|
|
45
66
|
private prompt: string | undefined
|
|
46
67
|
private contextBag: Record<string, unknown> = {}
|
|
47
68
|
private schema: OutputSchema<T> | undefined
|
|
69
|
+
private suspendGate:
|
|
70
|
+
| ((call: ToolUseBlock, context?: Record<string, unknown>) => boolean | Promise<boolean>)
|
|
71
|
+
| undefined
|
|
48
72
|
|
|
49
73
|
constructor(
|
|
50
74
|
private readonly brain: BrainManager,
|
|
51
|
-
private readonly agent: Agent
|
|
75
|
+
private readonly agent: Agent<unknown>,
|
|
52
76
|
) {}
|
|
53
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Install a human-in-the-loop gate. Called before each tool
|
|
80
|
+
* execution inside the agent loop; when it returns `true`, the
|
|
81
|
+
* run pauses and `.run()` resolves with a `SuspendedRun` instead
|
|
82
|
+
* of `AgentResult`. Apps obtain results out-of-band and call
|
|
83
|
+
* `.resume(state, results)` to continue.
|
|
84
|
+
*
|
|
85
|
+
* Throws `BrainError` if the runner is also in structured-output
|
|
86
|
+
* mode (`.output(schema)`) — schema + suspend is a deferred slice.
|
|
87
|
+
*/
|
|
88
|
+
suspend(
|
|
89
|
+
gate: (call: ToolUseBlock, context?: Record<string, unknown>) => boolean | Promise<boolean>,
|
|
90
|
+
): AgentRunner<T, true> {
|
|
91
|
+
this.suspendGate = gate
|
|
92
|
+
return this as unknown as AgentRunner<T, true>
|
|
93
|
+
}
|
|
94
|
+
|
|
54
95
|
/** Set the user input. Required before `run()`. */
|
|
55
96
|
input(text: string): this {
|
|
56
97
|
this.prompt = text
|
|
@@ -88,24 +129,71 @@ export class AgentRunner<T = never> {
|
|
|
88
129
|
return this as unknown as AgentRunner<U>
|
|
89
130
|
}
|
|
90
131
|
|
|
91
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Streaming variant of `run()`. Returns an
|
|
134
|
+
* `AsyncIterable<AgentStreamEvent<T>>` — yields text deltas,
|
|
135
|
+
* tool-use/result boundaries, and a terminal `stop` event with
|
|
136
|
+
* the full trace.
|
|
137
|
+
*
|
|
138
|
+
* Default (no `.output(schema)` set): the terminal `stop` has the
|
|
139
|
+
* plain shape and `T` defaults to `never`.
|
|
140
|
+
*
|
|
141
|
+
* With `.output(schema)`: the terminal `stop` event carries the
|
|
142
|
+
* parsed `value: T` + raw `text` alongside the loop bookkeeping,
|
|
143
|
+
* and the runner delegates to
|
|
144
|
+
* `BrainManager.streamGenerateWithTools`.
|
|
145
|
+
*/
|
|
146
|
+
stream(): AsyncIterable<AgentStreamEvent<T>> {
|
|
147
|
+
if (this.prompt === undefined) {
|
|
148
|
+
throw new BrainError('AgentRunner.stream: input() must be called before stream().')
|
|
149
|
+
}
|
|
150
|
+
const messages: Message[] = [{ role: 'user', content: this.prompt }]
|
|
151
|
+
const options: RunWithToolsOptions = {
|
|
152
|
+
...this.buildChatOptions(),
|
|
153
|
+
maxIterations: this.agent.maxIterations,
|
|
154
|
+
context: this.contextBag,
|
|
155
|
+
}
|
|
156
|
+
if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
|
|
157
|
+
if (this.schema !== undefined) {
|
|
158
|
+
return this.brain.streamGenerateWithTools<T>(
|
|
159
|
+
messages,
|
|
160
|
+
this.schema,
|
|
161
|
+
this.agent.tools,
|
|
162
|
+
options,
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
return this.brain.streamTools(messages, this.agent.tools, options) as AsyncIterable<
|
|
166
|
+
AgentStreamEvent<T>
|
|
167
|
+
>
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async run(): Promise<AgentRunMaybeSuspended<T, S>> {
|
|
92
171
|
if (this.prompt === undefined) {
|
|
93
172
|
throw new BrainError('AgentRunner.run: input() must be called before run().')
|
|
94
173
|
}
|
|
174
|
+
if (this.suspendGate !== undefined && this.schema !== undefined) {
|
|
175
|
+
throw new BrainError(
|
|
176
|
+
'AgentRunner.run: `.suspend(...)` and `.output(schema)` cannot be combined in V1 — the schema variants don\'t yet model pause/resume. Run tools first with suspension, then call brain.generate(...) on the result for the structured summary.',
|
|
177
|
+
)
|
|
178
|
+
}
|
|
95
179
|
const messages: Message[] = [{ role: 'user', content: this.prompt }]
|
|
96
180
|
|
|
97
181
|
if (this.schema !== undefined) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
182
|
+
const hasTools = this.agent.tools.length > 0 || this.agent.mcpServers.length > 0
|
|
183
|
+
if (hasTools) {
|
|
184
|
+
const toolOptions: RunWithToolsOptions = {
|
|
185
|
+
...this.buildChatOptions(),
|
|
186
|
+
maxIterations: this.agent.maxIterations,
|
|
187
|
+
context: this.contextBag,
|
|
188
|
+
}
|
|
189
|
+
if (this.agent.mcpServers.length > 0) toolOptions.mcpServers = this.agent.mcpServers
|
|
190
|
+
const result = await this.brain.generateWithTools<T>(
|
|
191
|
+
messages,
|
|
192
|
+
this.schema,
|
|
193
|
+
this.agent.tools,
|
|
194
|
+
toolOptions,
|
|
108
195
|
)
|
|
196
|
+
return result as AgentRunResult<T>
|
|
109
197
|
}
|
|
110
198
|
const generateOptions = this.buildChatOptions()
|
|
111
199
|
const result = await this.brain.generate<T>(messages, this.schema, generateOptions)
|
|
@@ -129,8 +217,39 @@ export class AgentRunner<T = never> {
|
|
|
129
217
|
context: this.contextBag,
|
|
130
218
|
}
|
|
131
219
|
if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
|
|
220
|
+
if (this.suspendGate !== undefined) options.shouldSuspend = this.suspendGate
|
|
132
221
|
const result = await this.brain.runTools(messages, this.agent.tools, options)
|
|
133
|
-
return result as
|
|
222
|
+
return result as AgentRunMaybeSuspended<T, S>
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Resume a previously-suspended run. Takes the `SuspendedRun.state`
|
|
227
|
+
* snapshot and the results gathered for each `pendingToolCalls`
|
|
228
|
+
* entry; the loop continues from where it paused.
|
|
229
|
+
*
|
|
230
|
+
* The runner's `suspend()` gate carries over so the same
|
|
231
|
+
* approval logic applies to any further tool calls — pass a
|
|
232
|
+
* fresh gate via `suspend()` before `resume()` to change the
|
|
233
|
+
* policy.
|
|
234
|
+
*/
|
|
235
|
+
async resume(
|
|
236
|
+
state: SuspendedState,
|
|
237
|
+
results: readonly ToolResultInput[],
|
|
238
|
+
): Promise<AgentRunMaybeSuspended<T, true>> {
|
|
239
|
+
if (this.schema !== undefined) {
|
|
240
|
+
throw new BrainError(
|
|
241
|
+
'AgentRunner.resume: structured-output runners cannot be resumed in V1 — `.output(schema)` is incompatible with pause/resume.',
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
const options: RunWithToolsOptions = {
|
|
245
|
+
...this.buildChatOptions(),
|
|
246
|
+
maxIterations: this.agent.maxIterations,
|
|
247
|
+
context: this.contextBag,
|
|
248
|
+
}
|
|
249
|
+
if (this.agent.mcpServers.length > 0) options.mcpServers = this.agent.mcpServers
|
|
250
|
+
if (this.suspendGate !== undefined) options.shouldSuspend = this.suspendGate
|
|
251
|
+
const result = await this.brain.resumeTools(state, results, this.agent.tools, options)
|
|
252
|
+
return result as AgentRunMaybeSuspended<T, true>
|
|
134
253
|
}
|
|
135
254
|
|
|
136
255
|
private buildChatOptions(): ChatOptions {
|
|
@@ -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>
|
package/src/brain_config.ts
CHANGED
|
@@ -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
|
-
|
|
|
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 {
|