@stravigor/saina 0.4.7
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/CHANGELOG.md +7 -0
- package/README.md +82 -0
- package/package.json +25 -0
- package/src/agent.ts +73 -0
- package/src/helpers.ts +756 -0
- package/src/index.ts +38 -0
- package/src/providers/anthropic_provider.ts +278 -0
- package/src/providers/openai_provider.ts +351 -0
- package/src/saina_manager.ts +116 -0
- package/src/saina_provider.ts +16 -0
- package/src/tool.ts +50 -0
- package/src/types.ts +179 -0
- package/src/utils/schema.ts +27 -0
- package/src/utils/sse_parser.ts +62 -0
- package/src/workflow.ts +180 -0
- package/tsconfig.json +4 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @stravigor/saina
|
|
2
|
+
|
|
3
|
+
AI module for the [Strav](https://www.npmjs.com/package/@stravigor/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 @stravigor/saina
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires `@stravigor/core` as a peer dependency.
|
|
12
|
+
|
|
13
|
+
## Providers
|
|
14
|
+
|
|
15
|
+
- **Anthropic** (Claude)
|
|
16
|
+
- **OpenAI** (GPT, also works with DeepSeek via custom `baseUrl`)
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { saina } from '@stravigor/saina'
|
|
22
|
+
|
|
23
|
+
// One-shot chat
|
|
24
|
+
const response = await saina.chat('Explain quantum computing')
|
|
25
|
+
|
|
26
|
+
// Streaming
|
|
27
|
+
for await (const chunk of saina.stream('Write a poem')) {
|
|
28
|
+
process.stdout.write(chunk.text)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Structured output with Zod
|
|
32
|
+
import { z } from 'zod'
|
|
33
|
+
const result = await saina.generate('List 3 colors', {
|
|
34
|
+
schema: z.object({ colors: z.array(z.string()) }),
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
// Embeddings
|
|
38
|
+
const vectors = await saina.embed('Hello world')
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Agents
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { Agent, defineTool } from '@stravigor/saina'
|
|
45
|
+
|
|
46
|
+
class ResearchAgent extends Agent {
|
|
47
|
+
provider = 'anthropic'
|
|
48
|
+
model = 'claude-sonnet-4-20250514'
|
|
49
|
+
instructions = 'You are a research assistant.'
|
|
50
|
+
tools = [searchTool, summarizeTool]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const result = await saina.agent(ResearchAgent).input('Find info on Bun').run()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Threads
|
|
57
|
+
|
|
58
|
+
Multi-turn conversations with serialization support:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const thread = saina.thread({ provider: 'anthropic', model: 'claude-sonnet-4-20250514' })
|
|
62
|
+
await thread.send('Hello')
|
|
63
|
+
await thread.send('Tell me more')
|
|
64
|
+
const saved = thread.serialize() // persist and restore later
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Workflows
|
|
68
|
+
|
|
69
|
+
Orchestrate multi-agent pipelines:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const workflow = saina.workflow()
|
|
73
|
+
.step('research', ResearchAgent)
|
|
74
|
+
.step('summarize', SummaryAgent)
|
|
75
|
+
.parallel('review', [FactCheckAgent, StyleAgent])
|
|
76
|
+
|
|
77
|
+
const result = await workflow.run('Analyze this topic')
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stravigor/saina",
|
|
3
|
+
"version": "0.4.7",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "AI module for the Strav framework",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts",
|
|
9
|
+
"./*": "./src/*.ts"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src/",
|
|
13
|
+
"package.json",
|
|
14
|
+
"tsconfig.json",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@stravigor/core": "0.4.5",
|
|
19
|
+
"@stravigor/workflow": "0.4.5"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "bun test tests/",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/agent.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ToolDefinition,
|
|
3
|
+
ToolCall,
|
|
4
|
+
ToolCallRecord,
|
|
5
|
+
AgentResult,
|
|
6
|
+
OutputSchema,
|
|
7
|
+
} from './types.ts'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base class for AI agents.
|
|
11
|
+
*
|
|
12
|
+
* Extend this class to define an agent with custom instructions,
|
|
13
|
+
* tools, structured output, and lifecycle hooks.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* class SupportAgent extends Agent {
|
|
17
|
+
* provider = 'anthropic'
|
|
18
|
+
* model = 'claude-sonnet-4-5-20250929'
|
|
19
|
+
* instructions = 'You are a customer support agent.'
|
|
20
|
+
* tools = [searchTool, lookupOrderTool]
|
|
21
|
+
*
|
|
22
|
+
* output = z.object({
|
|
23
|
+
* reply: z.string(),
|
|
24
|
+
* category: z.enum(['billing', 'shipping', 'product', 'other']),
|
|
25
|
+
* })
|
|
26
|
+
*
|
|
27
|
+
* onToolCall(call: ToolCall) {
|
|
28
|
+
* console.log(`Calling tool: ${call.name}`)
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
export abstract class Agent {
|
|
33
|
+
/** Provider name (e.g., 'anthropic', 'openai'). Falls back to config default. */
|
|
34
|
+
provider?: string
|
|
35
|
+
|
|
36
|
+
/** Model identifier. Falls back to the provider's configured default model. */
|
|
37
|
+
model?: string
|
|
38
|
+
|
|
39
|
+
/** System prompt / instructions for this agent. Supports `{{key}}` context interpolation. */
|
|
40
|
+
instructions: string = ''
|
|
41
|
+
|
|
42
|
+
/** Tools available to this agent during execution. */
|
|
43
|
+
tools?: ToolDefinition[]
|
|
44
|
+
|
|
45
|
+
/** Structured output schema (Zod or JSON Schema). When set, the final response is parsed and validated. */
|
|
46
|
+
output?: OutputSchema
|
|
47
|
+
|
|
48
|
+
/** Maximum tool-use loop iterations before forcing a stop. Falls back to config default (10). */
|
|
49
|
+
maxIterations?: number
|
|
50
|
+
|
|
51
|
+
/** Maximum tokens per completion request. Falls back to config default (4096). */
|
|
52
|
+
maxTokens?: number
|
|
53
|
+
|
|
54
|
+
/** Temperature for completion requests. Falls back to config default (0.7). */
|
|
55
|
+
temperature?: number
|
|
56
|
+
|
|
57
|
+
// ── Lifecycle hooks (optional) ───────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
/** Called before the first completion request. */
|
|
60
|
+
onStart?(input: string, context: Record<string, unknown>): void | Promise<void>
|
|
61
|
+
|
|
62
|
+
/** Called when the model requests a tool call, before execution. */
|
|
63
|
+
onToolCall?(call: ToolCall): void | Promise<void>
|
|
64
|
+
|
|
65
|
+
/** Called after a tool finishes execution. */
|
|
66
|
+
onToolResult?(call: ToolCallRecord): void | Promise<void>
|
|
67
|
+
|
|
68
|
+
/** Called when the agent run completes successfully. */
|
|
69
|
+
onComplete?(result: AgentResult): void | Promise<void>
|
|
70
|
+
|
|
71
|
+
/** Called when the agent run encounters an error. */
|
|
72
|
+
onError?(error: Error): void | Promise<void>
|
|
73
|
+
}
|