Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions example/src/agents/static_supervisor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import s from 'dedent'
import { agent, AgentOptions } from 'fabrice-ai/agent'
import { childState } from 'fabrice-ai/state'
import { tool } from 'fabrice-ai/tool'
import { zodResponseFormat } from 'openai/helpers/zod'
import { z } from 'zod'

const defaults: AgentOptions = {
tools: {
printThePlan: tool({
description: 'Tool for printing a message to the user',
parameters: z.object({
message: z.string().describe('The message to print to the user'),
}),
execute: ({ message }) => {
console.log(message)
return Promise.resolve('User got the message')
},
}),
},
run: async (state, context, workflow) => {
const response = await workflow.team[state.agent].provider.completions({
messages: [
{
role: 'system',
content: s`
You are a planner that breaks down complex workflows into smaller, actionable steps.
Your job is to determine the execution plan of the workflow.
Print the plan using the 'printThePlan' tool.
Then return the tasks one by one untill execution plan end.
Take available agents into consideration.
If all required tasks are completed, return null.

Rules:
1. Each task should be self-contained and achievable
2. Tasks should be specific and actionable
3. Return null when the workflow is complete
4. Consider dependencies and order of operations
5. Use context from completed tasks to inform next steps

Here are the available agents:
<agents>
${Object.entries(workflow.team).map(([name, agent]) =>
agent.description ? `<agent name="${name}">${agent.description}</agent>` : ''
)}
</agents>
`,
},
{
role: 'assistant',
content: 'What is the request?',
},
...context,
...state.messages,
],
temperature: 0.2,
response_format: zodResponseFormat(
z.object({
plan: z.array(z.string()).describe('The execution plan of the workflow'),
task: z
.string()
.describe('The next task to be completed or null if the workflow is complete')
.nullable(),
reasoning: z
.string()
.describe('The reasoning for selecting the next task or why the workflow is complete'),
}),
'next_task'
),
})

try {
const content = response.choices[0].message.parsed
console.log(content)
if (!content) {
throw new Error('No content in response')
}

if (!content.task) {
return {
...state,
status: 'finished',
}
}

const agentRequest = {
role: 'user' as const,
content: content.task,
}

return {
...state,
status: 'running',
messages: [...state.messages, agentRequest],
child: childState({
agent: 'resourcePlanner',
messages: [agentRequest],
}),
}
} catch (error) {
throw new Error('Failed to determine next task')
}
},
}

export const staticSupervisor = (options?: AgentOptions) =>
agent({
...defaults,
...options,
})
42 changes: 25 additions & 17 deletions example/src/github_trending_vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { createFireCrawlTool } from '@fabrice-ai/tools/firecrawl'
import { getApiKey } from '@fabrice-ai/tools/utils'
import { createVectorStoreTools } from '@fabrice-ai/tools/vector'
import { agent } from 'fabrice-ai/agent'
import { supervisor } from 'fabrice-ai/agents/supervisor'
import { solution } from 'fabrice-ai/solution'
import { teamwork } from 'fabrice-ai/teamwork'
import { logger } from 'fabrice-ai/telemetry'
import { tool } from 'fabrice-ai/tool'
import { workflow } from 'fabrice-ai/workflow'
import { z } from 'zod'

import { staticSupervisor } from './agents/static_supervisor.js'
import { askUser } from './tools/askUser.js'

const apiKey = await getApiKey('Firecrawl.dev API Key', 'FIRECRAWL_API_KEY')
Expand All @@ -19,46 +19,54 @@ const { firecrawl } = createFireCrawlTool({
apiKey,
})

const githubResearcher = agent({
const webCrawler = agent({
description: `
You are skilled at browsing Github pages.
You are skilled at browsing Web pages.
You are saving the documents to vector store for later usage.
You don't do any other thing just these two tasks.
`,
tools: {
firecrawl,
saveDocumentInVectorStore,
},
})

const wrapupRedactor = agent({
const human = agent({
description: `
You ask users for which topic to focus on if it's defined in the task.
Then - you search relevant information in Vector Store and compile reports based on it.
You're famous of beautiful Markdown formatting.
You can ask user and get their answer to questions that are needed by other agents.
`,
tools: {
askUser,
},
})

const reportCompiler = agent({
description: `
You can search Vector Store to find relevant informations and create reports based on it
Based on the information from Vector Store you can compile a comprehensive report.
You're famous of beautiful Markdown formatting.
`,
tools: {
searchInVectorStore,
},
})

const wrapUpTrending = workflow({
team: { githubResearcher, wrapupRedactor },
team: { webCrawler, human, reportCompiler, supervisor: staticSupervisor() }, // an example of overriding supervisor
description: `
Research the URL "https://github.com/trending/typescript" page using firecrawl tool
Select 3 top projects. Browse for details about these projects on their subpages.
Save it all to the vector store.
Research the URL "https://github.com/trending/typescript" page.
Select 3 top projects. Browse details about these projects on their subpages.
Store each page in Vector Store for further usage.
After you store the information you don't need to browse the page again
because everything is stored in Vector Store.

Ask user about which project he wants to learn more. Ask user only once.

Ask user about which project he wants to learn more.
reate a comprehensive report markdown output:
Create a comprehensive markdown report using information from Vector Store, based on user selection:
- create a one, two sentence summary about every project.
- include detailed summary about the project selected by the user.

Here are some ground rules to follow:
- Browser the pages onle once and store content in Vector Store.
- Use Vector Store if you need information about the project.
- Before making up the record: ask user about which project he wants to learn more.
`,
output: `
Comprehensive markdown report including:
Expand Down