@strav/brain 0.4.31 → 1.0.0-alpha.9
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 +42 -76
- package/src/agent_result.ts +32 -0
- package/src/agent_runner.ts +61 -0
- package/src/brain_config.ts +72 -0
- package/src/brain_error.ts +29 -0
- package/src/brain_manager.ts +170 -123
- package/src/brain_provider.ts +90 -6
- package/src/define_tool.ts +42 -0
- package/src/index.ts +40 -42
- package/src/provider.ts +74 -0
- package/src/providers/anthropic_provider.ts +347 -231
- package/src/thread.ts +99 -0
- package/src/tool.ts +28 -44
- package/src/tool_execution_error.ts +26 -0
- package/src/types.ts +129 -241
- 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/README.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# @strav/brain
|
|
2
|
-
|
|
3
|
-
AI module for the [Strav](https://www.npmjs.com/package/@strav/core) framework. Provides a unified interface for AI providers with support for agents, threads, tool use, and multi-step workflows.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
bun add @strav/brain
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Requires `@strav/core` as a peer dependency.
|
|
12
|
-
|
|
13
|
-
## Providers
|
|
14
|
-
|
|
15
|
-
- **Anthropic** (Claude)
|
|
16
|
-
- **OpenAI** (GPT, also works with DeepSeek via custom `baseUrl`)
|
|
17
|
-
- **Google** (Gemini)
|
|
18
|
-
|
|
19
|
-
## Usage
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { brain } from '@strav/brain'
|
|
23
|
-
|
|
24
|
-
// One-shot chat
|
|
25
|
-
const response = await brain.chat('Explain quantum computing')
|
|
26
|
-
|
|
27
|
-
// Streaming
|
|
28
|
-
for await (const chunk of brain.stream('Write a poem')) {
|
|
29
|
-
process.stdout.write(chunk.text)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Structured output with Zod
|
|
33
|
-
import { z } from 'zod'
|
|
34
|
-
const result = await brain.generate('List 3 colors', {
|
|
35
|
-
schema: z.object({ colors: z.array(z.string()) }),
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
// Embeddings
|
|
39
|
-
const vectors = await brain.embed('Hello world')
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Tools
|
|
43
|
-
|
|
44
|
-
Define tools that AI agents can use:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
import { defineTool } from '@strav/brain'
|
|
48
|
-
import { z } from 'zod'
|
|
49
|
-
|
|
50
|
-
const searchTool = defineTool({
|
|
51
|
-
name: 'search',
|
|
52
|
-
description: 'Search the database',
|
|
53
|
-
parameters: z.object({ query: z.string() }),
|
|
54
|
-
execute: async ({ query }, context) => {
|
|
55
|
-
const userId = context?.userId
|
|
56
|
-
return await db.search(query, { userId })
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
The `execute` function receives two parameters:
|
|
62
|
-
- `args` - The parsed and validated tool arguments
|
|
63
|
-
- `context` - Optional context object passed from the agent runner
|
|
64
|
-
|
|
65
|
-
## Agents
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
import { Agent, defineTool } from '@strav/brain'
|
|
69
|
-
|
|
70
|
-
class ResearchAgent extends Agent {
|
|
71
|
-
provider = 'anthropic'
|
|
72
|
-
model = 'claude-sonnet-4-20250514'
|
|
73
|
-
instructions = 'You are a research assistant.'
|
|
74
|
-
tools = [searchTool, summarizeTool]
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Google Gemini agent
|
|
78
|
-
class GeminiResearchAgent extends Agent {
|
|
79
|
-
provider = 'google'
|
|
80
|
-
model = 'gemini-2.0-flash'
|
|
81
|
-
instructions = 'You are a research assistant powered by Gemini.'
|
|
82
|
-
tools = [searchTool, summarizeTool]
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Run agent with context
|
|
86
|
-
const runner = brain.agent(ResearchAgent)
|
|
87
|
-
runner.context({ userId: '123' }) // Pass context to tools
|
|
88
|
-
const result = await runner.input('Find info on Bun').run()
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
## Threads
|
|
92
|
-
|
|
93
|
-
Multi-turn conversations with serialization support:
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
const thread = brain.thread({ provider: 'anthropic', model: 'claude-sonnet-4-20250514' })
|
|
97
|
-
await thread.send('Hello')
|
|
98
|
-
await thread.send('Tell me more')
|
|
99
|
-
const saved = thread.serialize() // persist and restore later
|
|
100
|
-
|
|
101
|
-
// Google Gemini example
|
|
102
|
-
const geminiThread = brain.thread({ provider: 'google', model: 'gemini-2.0-flash' })
|
|
103
|
-
await geminiThread.send('Explain quantum computing')
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Workflows
|
|
107
|
-
|
|
108
|
-
Orchestrate multi-agent pipelines:
|
|
109
|
-
|
|
110
|
-
```ts
|
|
111
|
-
const workflow = brain.workflow()
|
|
112
|
-
.step('research', ResearchAgent)
|
|
113
|
-
.step('summarize', SummaryAgent)
|
|
114
|
-
.parallel('review', [FactCheckAgent, StyleAgent])
|
|
115
|
-
|
|
116
|
-
const result = await workflow.run('Analyze this topic')
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## License
|
|
120
|
-
|
|
121
|
-
MIT
|