Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/agents/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ declare global {
interface AgentParameters {
debug?: boolean
input: any
uid?: string
output?: any
agent?: Agent
error?: string
apikeys?: APIKeys
state?: any
logWriter?: (p: { worker: AIWorker, state: any }) => void
}
}

Expand Down
74 changes: 67 additions & 7 deletions src/agents/agentfactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { supabase } from "./db"
import { workerRegistry } from "./registry"
import { buildWorker } from "./worker"
import { ulid } from 'ulid'
import { ApiWorker } from "./workers/api"
import { z } from "zod"


interface AgentConfig {
edges?: object
id?: number
title?: string
description?: string
type?: AgentTypes
workers?: object
team_id?: string
debuguuid?: string
}

declare global {
type AgentTypes = "conversational" | "data"
}


Expand All @@ -25,6 +35,12 @@ export function createAgent(config: AgentConfig) {
set title(v: string) { config.title = v },
edges,
workers,
displayData: false,

type: "data" as AgentTypes,
debuguuid: config.debuguuid || "",
description: "",


currentWorker: null as AIWorker,
update() {
Expand All @@ -43,6 +59,19 @@ export function createAgent(config: AgentConfig) {
}
return null
},
getEndAPIWorkers(p: AgentParameters) {
const apiWorkers: ApiWorker[] = []
for (const key in workers) {
const w = workers[key]
if (w.config.type !== "api") continue
const outputHandles = Object.values(w.handles).filter((h) => h.direction === "output")
if (outputHandles.length === 0) continue
const cw = w.getConnectedWokers(p)
if (cw.length === 0) continue
apiWorkers.push(w as any)
}
return apiWorkers
},

getInputWorker() {
for (const key in workers) {
Expand Down Expand Up @@ -82,16 +111,41 @@ export function createAgent(config: AgentConfig) {
p.input ||= {}
p.output ||= {}
p.apikeys ||= {}
p.state ||= {}
p.logWriter ||= () => { }
p.agent = agent

if (p.debug && agent.debuguuid && !p.uid) {
p.uid = agent.debuguuid
}

const hasUid = p.uid && z.string().uuid().safeParse(p.uid).success

if (hasUid) {
const dbState = await supabase.from("states").select("*").eq("id", p.uid).single()
if (dbState.data) p.state = dbState.data.state || {}
}

console.log(`Executing agent '${agent.title}'`)

const worker = agent.getResponseWorker()
if (!worker) return
const apiWorkers = agent.getEndAPIWorkers(p)

if (!worker && !apiWorkers.length) return

try {
await worker.execute(p)
for (const w of apiWorkers) {
await w.execute(p)
}

if (hasUid) await supabase.from("states").upsert({ id: p.uid, state: p.state || {} })

} catch (error) {
console.error(error)
p.error = error.toString()
}

agent.currentWorker = null
agent.update()
},
Expand All @@ -107,7 +161,9 @@ export function createAgent(config: AgentConfig) {
},

addWorker(w: WorkerConfig): AIWorker {
w.id ||= `NODE_${ulid()}`
const nameType = w.type?.toUpperCase() || "NODE"

w.id ||= `${nameType}_${ulid()}`
w.handles ||= {}
const worker = buildWorker(w)
workers[w.id] = worker
Expand All @@ -127,6 +183,8 @@ export function configureAgent(data: AgentConfig) {

const workers: WorkerConfig[] = (data.workers || []) as any
const agent = createAgent(data)
agent.type = data.type || "data"
agent.description = data.description || ""

for (const w of workers) {
const { handles, ...rest } = w
Expand Down Expand Up @@ -170,11 +228,15 @@ export function configureAgent(data: AgentConfig) {

}

export async function saveAgent(agent: Agent) {
export async function saveAgent(agent: Agent, team_id?: string) {

const agentData: AgentConfig = {
title: agent.title,
edges: agent.edges
description: agent.description,
type: agent.type,
edges: agent.edges,
team_id,
debuguuid: agent.debuguuid || "",
}
const workerlist = []

Expand Down Expand Up @@ -212,9 +274,7 @@ export async function saveAgent(agent: Agent) {
agent.id = data[0].id
}


console.log(workerlist)

return agent

}

Expand Down
13 changes: 10 additions & 3 deletions src/agents/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { schema } from "./workers/schema"
import { search } from "./workers/search"
import { text } from "./workers/text"
import { api } from "./workers/api"
// import { stt } from "./workers/tts"
// import { background } from "./workers/background"
// import { condition } from "./workers/condition"
import { documentSelector } from "./workers/documentselector"
import { state } from "./workers/state"
import { stt } from "./workers/stt"
import { tts } from "./workers/tts"
import { translate } from "./workers/translate"


type WorkerCategories = "io" | "generator" | "debug" | "tool"
Expand Down Expand Up @@ -45,9 +47,14 @@ export const workerRegistry = {

search,
combine,
documentSelector,

mock,
display,
api,
state,
stt,
tts,
translate,

} satisfies { [index: string]: WorkerRegistryItem }
Loading