
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
@flatfile/improv
Advanced tools
A powerful TypeScript library for building AI agents with multi-threaded conversations, tool execution, and event handling capabilities
A powerful TypeScript library for building AI agents with multi-threaded conversations, tool execution, and event handling capabilities. Built on top of AWS Bedrock (Claude) with support for advanced features like attachments, tool calling, and event streaming.
import { Agent, Tool, Thread, BedrockThreadDriver } from '@flatfile/improv';
import { z } from 'zod';
// Create a custom tool
const calculatorTool = new Tool({
name: 'calculator',
description: 'Performs basic arithmetic operations',
parameters: z.object({
operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
a: z.number(),
b: z.number(),
}),
executeFn: async (args) => {
const { operation, a, b } = args;
switch (operation) {
case 'add': return a + b;
case 'subtract': return a - b;
case 'multiply': return a * b;
case 'divide': return a / b;
}
}
});
// Initialize the Bedrock driver
const driver = new BedrockThreadDriver({
model: 'anthropic.claude-3-haiku-20240307-v1:0',
temperature: 0.7,
});
// Create an agent
const agent = new Agent({
knowledge: [
{ fact: 'The agent can perform basic arithmetic operations.' }
],
instructions: [
{ instruction: 'Use the calculator tool for arithmetic operations.', priority: 1 }
],
tools: [calculatorTool],
driver,
});
// Create and use a thread
const thread = agent.createThread({
prompt: 'What is 25 multiplied by 4?',
onResponse: async (message) => {
console.log('Agent response:', message.content);
}
});
// Send the thread
await thread.send();
The main agent class that manages knowledge, instructions, tools, and conversation threads.
const agent = new Agent({
knowledge?: AgentKnowledge[],
instructions?: AgentInstruction[],
memory?: AgentMemory[],
systemPrompt?: string,
tools?: Tool[],
driver: ThreadDriver
});
Manages a single conversation thread with message history and tool execution.
const thread = new Thread({
messages?: Message[],
tools?: Tool[],
driver: ThreadDriver,
toolChoice?: 'auto' | 'any',
maxSteps?: number
});
Define custom tools that the agent can use during conversations.
const tool = new Tool({
name: string,
description: string,
parameters: z.ZodTypeAny,
followUpMessage?: string,
executeFn: (args: Record<string, any>) => Promise<any>
});
Represents a single message in a conversation thread.
const message = new Message({
content?: string,
role?: 'system' | 'user' | 'assistant' | 'tool',
toolCalls?: ToolCall[],
toolResults?: ToolResult[],
attachments?: Attachment[],
cache?: boolean
});
The library uses an event-driven architecture. All major components extend EventSource
, allowing you to listen for various events:
// Agent events
agent.on('thread.created', ({ agent, threadId, thread }) => {});
agent.on('knowledge.added', ({ agent, knowledge }) => {});
agent.on('instruction.added', ({ agent, instruction }) => {});
// Thread events
thread.on('message.added', ({ thread, message }) => {});
thread.on('message.removed', ({ thread, message }) => {});
thread.on('thread.cleared', ({ thread, removedMessages }) => {});
thread.on('thread.max_steps_reached', ({ thread, steps }) => {});
// Tool events
tool.on('tool.execution.started', ({ tool, name, args }) => {});
tool.on('tool.execution.completed', ({ tool, name, args, result }) => {});
tool.on('tool.execution.failed', ({ tool, name, args, error }) => {});
Tools can include follow-up messages that guide the AI's evaluation of tool responses. This is particularly useful for:
const tool = new Tool({
name: 'someAction',
description: 'Performs some action',
parameters: z.object({...}),
followUpMessage: `Review the data returned by the tool call.
- If successful: What next steps should be taken?
- If error: What went wrong and how should we fix it?
- Are there any patterns in the data we should investigate?`,
executeFn: async (args) => {...}
});
For complex workflows that involve multiple tool calls and state transitions, the 3-keyed lock pattern provides controlled flow and state management:
class ComplexTask extends Agent {
private isEvaluatingTools: boolean = true;
private nextMessageIsSummary: boolean = false;
private waitingForDone: boolean = false;
async execute() {
return new Promise((resolve) => {
const thread = this.createThread({...});
thread.on('thread.response', (event) => {
if (this.isEvaluatingTools) {
// First state: AI is evaluating and using tools
this.isEvaluatingTools = false;
thread.send(new Message({
content: 'Are there other items to process? If not, say "done"'
}));
} else if (this.nextMessageIsSummary) {
// Third state: Get final summary
resolve(thread.last());
} else {
// Second state: Transition to summary
this.nextMessageIsSummary = true;
thread.send(new Message({
content: 'Please provide a final summary'
}));
}
});
// Reset evaluation mode when tools are used
thread.on('tool.execution.started', () => {
this.isEvaluatingTools = true;
this.waitingForDone = false;
});
});
}
}
This pattern provides:
The library uses AWS Bedrock (Claude) as its LLM provider. Configure your AWS credentials:
// Required environment variables
process.env.AWS_ACCESS_KEY_ID = 'your-access-key';
process.env.AWS_SECRET_ACCESS_KEY = 'your-secret-key';
process.env.AWS_REGION = 'your-region';
const driver = new BedrockThreadDriver({
model?: string, // defaults to 'anthropic.claude-3-haiku-20240307-v1:0'
temperature?: number, // defaults to 0.7
maxTokens?: number, // defaults to 4096
cache?: boolean // defaults to false
});
Tool Design
Thread Management
maxSteps
to prevent infinite loopsfork()
to create thread variationscloseThread()
when doneEvent Handling
Memory Management
MIT
Contributions are welcome! Please read our contributing guidelines for details.
FAQs
A powerful TypeScript library for building AI agents with multi-threaded conversations, tool execution, and event handling capabilities
The npm package @flatfile/improv receives a total of 13,934 weekly downloads. As such, @flatfile/improv popularity was classified as popular.
We found that @flatfile/improv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 14 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.