@strav/brain 0.4.31 → 1.0.0-alpha.10
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 +17 -20
- package/src/agent.ts +50 -75
- package/src/agent_result.ts +32 -0
- package/src/agent_runner.ts +63 -0
- package/src/brain_config.ts +80 -0
- package/src/brain_error.ts +29 -0
- package/src/brain_manager.ts +186 -123
- package/src/brain_provider.ts +91 -6
- package/src/define_tool.ts +42 -0
- package/src/index.ts +43 -42
- package/src/mcp_server.ts +47 -0
- package/src/provider.ts +83 -0
- package/src/providers/anthropic_provider.ts +435 -232
- package/src/thread.ts +99 -0
- package/src/tool.ts +28 -44
- package/src/tool_execution_error.ts +26 -0
- package/src/types.ts +164 -237
- package/CHANGELOG.md +0 -44
- package/README.md +0 -121
- package/src/helpers.ts +0 -1082
- package/src/mcp_toolbox.ts +0 -62
- package/src/memory/context_budget.ts +0 -120
- package/src/memory/index.ts +0 -17
- package/src/memory/memory_manager.ts +0 -168
- package/src/memory/semantic_memory.ts +0 -89
- package/src/memory/strategies/sliding_window.ts +0 -20
- package/src/memory/strategies/summarize.ts +0 -157
- package/src/memory/thread_store.ts +0 -56
- package/src/memory/token_counter.ts +0 -101
- package/src/memory/types.ts +0 -68
- package/src/providers/google_provider.ts +0 -496
- package/src/providers/openai_provider.ts +0 -569
- package/src/providers/openai_responses_provider.ts +0 -321
- package/src/utils/error_scrub.ts +0 -5
- package/src/utils/prompt.ts +0 -65
- package/src/utils/retry.ts +0 -104
- package/src/utils/schema.ts +0 -27
- package/src/utils/sse_parser.ts +0 -62
- package/src/workflow.ts +0 -199
- package/tsconfig.json +0 -5
package/src/provider.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Provider` — the contract every brain backend implements.
|
|
3
|
+
*
|
|
4
|
+
* Each concrete provider (Anthropic, OpenAI later, Gemini later,
|
|
5
|
+
* DeepSeek later) wraps the vendor's SDK and translates the framework
|
|
6
|
+
* shapes (`Message`, `ChatOptions`) into the vendor's native request,
|
|
7
|
+
* then translates the response back into `ChatResult` / `StreamEvent`.
|
|
8
|
+
*
|
|
9
|
+
* Providers are values, not classes — apps use them via the
|
|
10
|
+
* `BrainManager` facade. The interface is exported so apps that need
|
|
11
|
+
* to plug in a custom provider (e.g. a local Ollama) can do so without
|
|
12
|
+
* subclassing.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { AgentResult } from './agent_result.ts'
|
|
16
|
+
import type { MCPServer } from './mcp_server.ts'
|
|
17
|
+
import type { Tool } from './tool.ts'
|
|
18
|
+
import type {
|
|
19
|
+
ChatOptions,
|
|
20
|
+
ChatResult,
|
|
21
|
+
Message,
|
|
22
|
+
StreamEvent,
|
|
23
|
+
} from './types.ts'
|
|
24
|
+
|
|
25
|
+
export interface RunWithToolsOptions extends ChatOptions {
|
|
26
|
+
/** Safety ceiling on tool-use round-trips. Default `10`. */
|
|
27
|
+
maxIterations?: number
|
|
28
|
+
/** Free-form context bag passed to every tool's `execute(input, ctx)`. */
|
|
29
|
+
context?: Record<string, unknown>
|
|
30
|
+
/**
|
|
31
|
+
* MCP servers Anthropic should connect to on this call. Merges
|
|
32
|
+
* with `config.brain.mcpServers` (per-call wins). Empty array or
|
|
33
|
+
* undefined → no MCP servers. Anthropic's backend handles tool
|
|
34
|
+
* discovery + invocation; the framework only surfaces the
|
|
35
|
+
* resulting `mcp_tool_use` / `mcp_tool_result` blocks.
|
|
36
|
+
*/
|
|
37
|
+
mcpServers?: readonly MCPServer[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Provider {
|
|
41
|
+
/** Identifier — matches the `config.brain.providers` key. */
|
|
42
|
+
readonly name: string
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Generate a single reply. Awaits the full response; for
|
|
46
|
+
* token-by-token rendering use `stream()`.
|
|
47
|
+
*/
|
|
48
|
+
chat(messages: readonly Message[], options?: ChatOptions): Promise<ChatResult>
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Stream the reply as it's generated. The async iterable yields
|
|
52
|
+
* `text` events for each delta and a final `stop` event with usage
|
|
53
|
+
* + stop-reason. Apps that want the full collected message at the
|
|
54
|
+
* end pass the same `messages` to `chat()` instead; this surface is
|
|
55
|
+
* for UI streaming, not for "make one call and get the message".
|
|
56
|
+
*/
|
|
57
|
+
stream(messages: readonly Message[], options?: ChatOptions): AsyncIterable<StreamEvent>
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Count input tokens for a given message set + options. Used by
|
|
61
|
+
* apps that need to budget context before sending. Optional — not
|
|
62
|
+
* every provider exposes a cheap token-count endpoint, so the
|
|
63
|
+
* implementation may approximate.
|
|
64
|
+
*/
|
|
65
|
+
countTokens?(messages: readonly Message[], options?: ChatOptions): Promise<number>
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Agentic loop. Sends the `messages` + `tools` to the model;
|
|
69
|
+
* detects tool-use blocks in the response; runs the matching
|
|
70
|
+
* tool's `execute`; appends the result and re-asks. Loops until
|
|
71
|
+
* the model returns `stop_reason: 'end_turn'` (or its
|
|
72
|
+
* provider-specific equivalent) or `maxIterations` is hit.
|
|
73
|
+
*
|
|
74
|
+
* Optional on the interface so providers that don't (yet) support
|
|
75
|
+
* tool use can omit it; `BrainManager.runTools` throws a
|
|
76
|
+
* `BrainError` when the configured provider lacks the method.
|
|
77
|
+
*/
|
|
78
|
+
runWithTools?(
|
|
79
|
+
messages: readonly Message[],
|
|
80
|
+
tools: readonly Tool[],
|
|
81
|
+
options?: RunWithToolsOptions,
|
|
82
|
+
): Promise<AgentResult>
|
|
83
|
+
}
|