AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil } from '@activepieces/core-utils'
import { FastifyBaseLogger } from 'fastify'
import { AddConversationCreditsParams, AiUsageHooks, UnpinConversationsFromKeyParams } from '../../ai/ai-usage-hooks'
import { agentChatModel } from './agent-chat-model'
import { AgentConversationEntity } from './agent-conversation-entity'
import { agentHelpers } from './agent-helpers'
export const agentConversationCreditsHooks = (log: FastifyBaseLogger): AiUsageHooks => ({
async addConversationCredits({ conversationId, credits }: AddConversationCreditsParams): Promise<void> {
const wholeCredits = Math.round(credits)
if (wholeCredits === 0) {
return
}
await agentHelpers.conversationRepo()
.createQueryBuilder()
.update(AgentConversationEntity)
.set({ aiCredits: () => 'COALESCE("aiCredits", 0) + :credits' })
.setParameter('credits', wholeCredits)
.where('id = :conversationId', { conversationId })
.execute()
},
async unpinConversationsFromKey({ manager, platformId, configId, key }: UnpinConversationsFromKeyParams): Promise<void> {
const repo = manager.getRepository(AgentConversationEntity)
const pinned = await repo.find({
where: { platformId, providerConfigId: configId },
select: { id: true, projectId: true, modelName: true },
})
const stale = isNil(key) ? pinned : pinned.filter((conversation) => !agentChatModel(log).keyServes({ key, conversation }))
if (stale.length === 0) {
return
}
await manager.query(
`
UPDATE "agent_conversation" AS conversation
SET "providerConfigId" = NULL
FROM unnest($3::varchar[], $4::varchar[]) AS stale(id, "modelName")
WHERE conversation."platformId" = $1
AND conversation."providerConfigId" = $2
AND conversation.id = stale.id
AND conversation."modelName" IS NOT DISTINCT FROM stale."modelName"
`,
[platformId, configId, stale.map((conversation) => conversation.id), stale.map((conversation) => conversation.modelName ?? null)],
)
},
})