@strav/brain 0.2.4 → 0.2.8

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/README.md CHANGED
@@ -38,6 +38,29 @@ const result = await brain.generate('List 3 colors', {
38
38
  const vectors = await brain.embed('Hello world')
39
39
  ```
40
40
 
41
+ ## Tools
42
+
43
+ Define tools that AI agents can use:
44
+
45
+ ```ts
46
+ import { defineTool } from '@strav/brain'
47
+ import { z } from 'zod'
48
+
49
+ const searchTool = defineTool({
50
+ name: 'search',
51
+ description: 'Search the database',
52
+ parameters: z.object({ query: z.string() }),
53
+ execute: async ({ query }, context) => {
54
+ const userId = context?.userId
55
+ return await db.search(query, { userId })
56
+ },
57
+ })
58
+ ```
59
+
60
+ The `execute` function receives two parameters:
61
+ - `args` - The parsed and validated tool arguments
62
+ - `context` - Optional context object passed from the agent runner
63
+
41
64
  ## Agents
42
65
 
43
66
  ```ts
@@ -50,7 +73,10 @@ class ResearchAgent extends Agent {
50
73
  tools = [searchTool, summarizeTool]
51
74
  }
52
75
 
53
- const result = await brain.agent(ResearchAgent).input('Find info on Bun').run()
76
+ // Run agent with context
77
+ const runner = brain.agent(ResearchAgent)
78
+ runner.context({ userId: '123' }) // Pass context to tools
79
+ const result = await runner.input('Find info on Bun').run()
54
80
  ```
55
81
 
56
82
  ## Threads
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strav/brain",
3
- "version": "0.2.4",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "description": "AI module for the Strav framework",
6
6
  "license": "MIT",
@@ -15,8 +15,8 @@
15
15
  "CHANGELOG.md"
16
16
  ],
17
17
  "peerDependencies": {
18
- "@strav/kernel": "0.1.4",
19
- "@strav/workflow": "0.1.4"
18
+ "@strav/kernel": "0.2.6",
19
+ "@strav/workflow": "0.2.6"
20
20
  },
21
21
  "dependencies": {
22
22
  "zod": "^3.25 || ^4.0"
package/src/helpers.ts CHANGED
@@ -27,7 +27,8 @@ import type {
27
27
  /** Execute a single tool call, returning the result and the tool message. */
28
28
  async function executeTool(
29
29
  tools: ToolDefinition[] | undefined,
30
- toolCall: ToolCall
30
+ toolCall: ToolCall,
31
+ context?: Record<string, unknown>
31
32
  ): Promise<{ result: unknown; message: Message }> {
32
33
  const toolDef = tools?.find(t => t.name === toolCall.name)
33
34
  let result: unknown
@@ -36,7 +37,7 @@ async function executeTool(
36
37
  result = `Error: Tool "${toolCall.name}" not found`
37
38
  } else {
38
39
  try {
39
- result = await toolDef.execute(toolCall.arguments)
40
+ result = await toolDef.execute(toolCall.arguments, context)
40
41
  } catch (err) {
41
42
  result = `Error: ${err instanceof Error ? err.message : String(err)}`
42
43
  }
@@ -556,7 +557,7 @@ export class AgentRunner<T extends Agent = Agent> {
556
557
  await agent.onToolCall?.(toolCall)
557
558
 
558
559
  const start = performance.now()
559
- const { result, message } = await executeTool(agent.tools, toolCall)
560
+ const { result, message } = await executeTool(agent.tools, toolCall, this._context)
560
561
  const duration = performance.now() - start
561
562
 
562
563
  const record: ToolCallRecord = {
package/src/tool.ts CHANGED
@@ -12,22 +12,23 @@ import type { ToolDefinition, JsonSchema } from './types.ts'
12
12
  * name: 'search',
13
13
  * description: 'Search the database',
14
14
  * parameters: z.object({ query: z.string() }),
15
- * execute: async ({ query }) => {
16
- * return await db.search(query)
15
+ * execute: async ({ query }, context) => {
16
+ * const userId = context?.userId
17
+ * return await db.search(query, { userId })
17
18
  * },
18
19
  * })
19
20
  */
20
- export function defineTool(config: {
21
+ export function defineTool<TArgs = any, TContext = Record<string, unknown>>(config: {
21
22
  name: string
22
23
  description: string
23
24
  parameters: any
24
- execute: (args: any) => unknown | Promise<unknown>
25
+ execute: (args: TArgs, context?: TContext) => unknown | Promise<unknown>
25
26
  }): ToolDefinition {
26
27
  return {
27
28
  name: config.name,
28
29
  description: config.description,
29
30
  parameters: zodToJsonSchema(config.parameters) as JsonSchema,
30
- execute: config.execute,
31
+ execute: config.execute as (args: Record<string, unknown>, context?: Record<string, unknown>) => unknown | Promise<unknown>,
31
32
  }
32
33
  }
33
34
 
package/src/types.ts CHANGED
@@ -49,7 +49,7 @@ export interface ToolDefinition {
49
49
  name: string
50
50
  description: string
51
51
  parameters: JsonSchema
52
- execute: (args: Record<string, unknown>) => unknown | Promise<unknown>
52
+ execute: (args: Record<string, unknown>, context?: Record<string, unknown>) => unknown | Promise<unknown>
53
53
  }
54
54
 
55
55
  // ── Completion Request / Response ────────────────────────────────────────────