diff --git a/apps/docs/content/docs/integrations/file.mdx b/apps/docs/content/docs/integrations/file.mdx index 0fc151aad4e..b9532e273cb 100644 --- a/apps/docs/content/docs/integrations/file.mdx +++ b/apps/docs/content/docs/integrations/file.mdx @@ -198,7 +198,7 @@ Apply one precise edit to an existing text file without rewriting it. Use search | `anchor` | string | No | For insert_after, the complete line after which content is inserted. | | `startAnchor` | string | No | For delete_between, the complete first line to delete. The start anchor is removed. | | `endAnchor` | string | No | For delete_between, the complete ending boundary line. The end anchor remains in the file. | -| `occurrence` | number | No | For anchored edits, which matching anchor occurrence to use, starting at 1. Defaults to 1. | +| `occurrence` | number | No | For anchored edits, which start or insert anchor match to use, starting at 1. Defaults to 1. The end anchor is the first match after it. | | `expectedRevision` | string | No | Refuse the edit unless the file still holds the content this revision names, as returned by Get File or an earlier write. Use it so an edit computed from what you read cannot overwrite someone else’s change. | #### Output diff --git a/apps/docs/content/docs/knowledgebase/connectors.mdx b/apps/docs/content/docs/knowledgebase/connectors.mdx index 905b61ac805..d6f64895280 100644 --- a/apps/docs/content/docs/knowledgebase/connectors.mdx +++ b/apps/docs/content/docs/knowledgebase/connectors.mdx @@ -200,7 +200,7 @@ Images smaller than **2 KiB**, SVG, HEIC, and AVIF files are not indexed through New knowledge-base Confluence connections enable image reading by default. Connections created before image support keep it off until you choose **Read with AI**; their next sync then indexes images. -The default admission limit is **500 new or changed images per sync**. Remaining images wait for another sync. Self-hosted operators can configure `KB_IMAGE_EXTRACTIONS_PER_SYNC`. Unchanged images reuse their extraction, including during a full resync; transient or malformed responses can cause additional model requests. +The default admission limit is **500 new or changed images per sync**. Remaining images wait for another sync. With **Member accounts**, each run that continues a sync can read up to 500. Self-hosted operators can configure `KB_IMAGE_EXTRACTIONS_PER_SYNC`. Unchanged images reuse their extraction, including during a full resync; transient or malformed responses can cause additional model requests. Image extraction follows [model billing](/platform/costs): BYOK usage is billed by the provider, while platform-key usage follows the deployment's Sim billing policy. Choose **Don't index** to stop image indexing; the next sync removes indexed images. Switching back processes them again and incurs the applicable model usage. diff --git a/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx b/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx index 8c1b71b8143..5c954c708d1 100644 --- a/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx +++ b/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx @@ -124,7 +124,7 @@ For Ollama on the Docker host, use `OLLAMA_URL=http://host.docker.internal:11434 | Variable | Description | |----------|-------------| | `KB_IMAGE_EXTRACTION_MODEL` | Gemini model that reads Confluence image attachments into searchable text. Defaults to `gemini-3.8-flash`; a value that is not a Gemini 3 or later model in the catalog falls back to the default. Needs `GEMINI_API_KEY` (or a workspace Gemini key); without one, image attachments are left out of syncs | -| `KB_IMAGE_EXTRACTIONS_PER_SYNC` | New or changed images one connector sync sends for reading, at least `1`. Defaults to `500`; the rest wait for the next sync, which reports as partial | +| `KB_IMAGE_EXTRACTIONS_PER_SYNC` | New or changed images one connector sync run sends for reading, at least `1`. Defaults to `500`; the rest wait for the next sync, which reports as partial. With member accounts, each continuation run can read up to this many | | `KB_CONFIG_VISION_REQUESTS_PER_MINUTE` | Image reads per minute per Gemini key, shared by every sync on the deployment. Defaults to `300`; lower it to match a key with a smaller Gemini quota | | `KB_EMBEDDING_MODEL` | Embedding model for new knowledge bases. Defaults to `text-embedding-3-small`; use `ollama/` for a model on your own Ollama. An unrecognised hosted model id falls back to the default, but an `ollama/` id is taken at face value — if that model is not on the server, knowledge-base creation fails rather than falling back | | `EMBEDDING_OUTPUT_DIMS` | Stored vector width: `384`, `768`, `1024`, `1536`, or `3072`. Hosted models default to `1536` when supported, otherwise their largest storable width. Invalid hosted widths fall back with a warning; Ollama can infer its actual width when unset | diff --git a/apps/docs/openapi-v2-files-audit.json b/apps/docs/openapi-v2-files-audit.json index 2f9cac3b6d8..e3e14dbb77a 100644 --- a/apps/docs/openapi-v2-files-audit.json +++ b/apps/docs/openapi-v2-files-audit.json @@ -6920,7 +6920,7 @@ "description": "Replacement text. An empty string clears the interior." }, "occurrence": { - "description": "Matching anchor occurrence, starting at 1. Defaults to 1.", + "description": "Which beforeAnchor match to use, starting at 1. Defaults to 1. afterAnchor is the first match after it.", "type": "integer", "minimum": 1, "maximum": 9007199254740991 @@ -6948,7 +6948,7 @@ "description": "Text to insert." }, "occurrence": { - "description": "Matching anchor occurrence, starting at 1. Defaults to 1.", + "description": "Which anchor match to insert after, starting at 1. Defaults to 1.", "type": "integer", "minimum": 1, "maximum": 9007199254740991 @@ -6976,7 +6976,7 @@ "description": "Ending boundary line. This end anchor remains." }, "occurrence": { - "description": "Matching anchor occurrence, starting at 1. Defaults to 1.", + "description": "Which startAnchor match to use, starting at 1. Defaults to 1. endAnchor is the first match after it.", "type": "integer", "minimum": 1, "maximum": 9007199254740991 diff --git a/apps/realtime/src/handlers/file-doc-presence.integration.ts b/apps/realtime/src/handlers/file-doc-presence.integration.ts new file mode 100644 index 00000000000..536983fabfc --- /dev/null +++ b/apps/realtime/src/handlers/file-doc-presence.integration.ts @@ -0,0 +1,198 @@ +import { mkdir, writeFile } from 'node:fs/promises' +import { createServer, type Server as HttpServer } from 'node:http' +import { dirname, resolve } from 'node:path' +import { + FILE_DOC_EVENTS, + FILE_DOC_SCHEMA_VERSION, + FILE_DOC_SEED, + type FileDocPresence, + type JoinFileDocSuccess, +} from '@sim/realtime-protocol/file-doc' +import { getErrorMessage } from '@sim/utils/errors' +import { generateId } from '@sim/utils/id' +import { createAdapter } from '@socket.io/redis-adapter' +import { createClient, type RedisClientType } from 'redis' +import { Server } from 'socket.io' +import { io as connect, type Socket } from 'socket.io-client' +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' +import * as Y from 'yjs' +import type { AuthenticatedSocket } from '@/middleware/auth' + +const redisUrl = process.env.REDIS_URL +if (!redisUrl) throw new Error('Integration setup must provide disposable Redis') +const internalSecret = process.env.INTERNAL_API_SECRET + +const projectId = generateId() +const fileId = generateId() +const roomName = `project-file-doc:${projectId}/${fileId}` +const docId = generateId() +const seedDoc = new Y.Doc() +seedDoc.getMap(FILE_DOC_SEED.configMap).set(FILE_DOC_SEED.flag, true) +seedDoc.getMap(FILE_DOC_SEED.configMap).set(FILE_DOC_SEED.docIdKey, docId) +const seed = Buffer.from(Y.encodeStateAsUpdate(seedDoc)).toString('base64') +seedDoc.destroy() + +interface Replica { + io: Server + url: string + pub: RedisClientType + sub: RedisClientType + shutdownStore: () => Promise +} + +const checks: { name: string; status: string; durationMs: number; error?: string }[] = [] +const clients: Socket[] = [] +const replicas: Replica[] = [] +let app: HttpServer + +async function listen(server: HttpServer) { + await new Promise((done) => server.listen(0, '127.0.0.1', done)) + const address = server.address() + if (!address || typeof address === 'string') throw new Error('Missing fixture port') + return `http://127.0.0.1:${address.port}` +} + +/** + * One realtime replica: a fresh module graph (its own rooms, owners and document store, as in a + * separate process) behind its own Socket.IO server on the shared Redis adapter. + */ +async function startReplica(): Promise { + vi.resetModules() + const fileDoc = await import('@/handlers/file-doc') + const fileDocStore = await import('@/handlers/file-doc-store') + const { MemoryRoomManager } = await import('@/rooms') + await fileDocStore.initFileDocStore(redisUrl) + const pub: RedisClientType = createClient({ url: redisUrl }) + const sub: RedisClientType = createClient({ url: redisUrl }) + await Promise.all([pub.connect(), sub.connect()]) + const http = createServer() + const io = new Server(http) + io.adapter(createAdapter(pub, sub)) + fileDoc.registerFileDocPresenceRelay(io) + const manager = new MemoryRoomManager(io) + io.on('connection', (socket) => { + const authed = socket as AuthenticatedSocket + authed.userId = socket.handshake.auth.actor + authed.userName = `Fixture ${socket.handshake.auth.actor}` + authed.userImage = 'https://fixture.invalid/avatar.png' + fileDoc.setupWorkspaceFileDocHandlers(authed, manager) + socket.on('disconnect', () => fileDoc.cleanupFileDocForSocket(socket.id, io, true)) + }) + const replica = { + io, + url: await listen(http), + pub, + sub, + shutdownStore: () => fileDocStore.getFileDocStore().shutdown(), + } + replicas.push(replica) + return replica +} + +beforeAll(async () => { + app = createServer((request, response) => { + response.setHeader('Content-Type', 'application/json') + const prefix = `/api/internal/project-file-doc/${projectId}/${fileId}/` + if (request.headers['x-api-key'] !== internalSecret || !request.url?.startsWith(prefix)) { + response.writeHead(401).end('{}') + return + } + const action = request.url.slice(prefix.length) + if (action === 'access') { + response.end(JSON.stringify({ projectId, fileId, canRead: true, canWrite: true, docId })) + } else if (action === 'seed') { + response.end(JSON.stringify({ update: seed, version: 1 })) + } else if (action === 'persist') { + response.end(JSON.stringify({ status: 'persisted', version: 2 })) + } else { + response.writeHead(404).end('{}') + } + }) + process.env.NEXT_PUBLIC_APP_URL = await listen(app) +}) + +/** Joins the shared file through `replica`, recording every roster the client receives. */ +async function joinThrough(replica: Replica, actor: string) { + const socket = connect(replica.url, { transports: ['websocket'], auth: { actor } }) + clients.push(socket) + const rosters: string[][] = [] + socket.on(FILE_DOC_EVENTS.PRESENCE, (payload: FileDocPresence) => { + if (payload.fileId === fileId) rosters.push(payload.users.map((user) => user.socketId).sort()) + }) + await new Promise((done) => socket.once('connect', done)) + const joined = new Promise((done, fail) => { + socket.once(FILE_DOC_EVENTS.JOIN_SUCCESS, done) + socket.once(FILE_DOC_EVENTS.JOIN_ERROR, (error) => fail(new Error(error.code))) + }) + const clientDoc = new Y.Doc() + const clientId = clientDoc.clientID + clientDoc.destroy() + socket.emit(FILE_DOC_EVENTS.JOIN, { + fileId, + projectId, + clientId, + schemaVersion: FILE_DOC_SCHEMA_VERSION, + }) + await joined + const id = socket.id + if (!id) throw new Error('Connected socket has no id') + return { socket, id, latestRoster: () => rosters.at(-1) } +} + +function check(name: string, run: () => Promise) { + it(name, async () => { + const start = performance.now() + try { + await run() + checks.push({ name, status: 'passed', durationMs: performance.now() - start }) + } catch (error) { + checks.push({ + name, + status: 'failed', + durationMs: performance.now() - start, + error: getErrorMessage(error), + }) + throw error + } + }) +} + +describe('File editor presence across replicas sharing the Redis adapter', () => { + check( + 'each replica lists collaborators joined through the other, and drops a departed one', + async () => { + const first = await startReplica() + const second = await startReplica() + const alice = await joinThrough(first, 'fixture-alice') + const bob = await joinThrough(second, 'fixture-bob') + const both = [alice.id, bob.id].sort() + + await expect.poll(alice.latestRoster, { timeout: 5_000 }).toEqual(both) + await expect.poll(bob.latestRoster, { timeout: 5_000 }).toEqual(both) + + bob.socket.disconnect() + await expect.poll(alice.latestRoster, { timeout: 5_000 }).toEqual([alice.id]) + } + ) +}) + +afterAll(async () => { + for (const client of clients) client.disconnect() + for (const replica of replicas) { + await replica.io.close() + await replica.shutdownStore() + } + const cleanup = replicas[0]?.pub + if (cleanup?.isOpen) { + const keys = await cleanup.keys(`filedoc:*:${roomName}`) + if (keys.length) await cleanup.del(keys) + } + for (const replica of replicas) { + for (const client of [replica.pub, replica.sub]) if (client.isOpen) await client.quit() + } + await new Promise((done) => app.close(() => done())) + const reportPath = + process.env.FILE_DOC_PRESENCE_REPORT_PATH ?? resolve('test-results/file-doc-presence.json') + await mkdir(dirname(reportPath), { recursive: true }) + await writeFile(reportPath, JSON.stringify({ checks }, null, 2)) +}) diff --git a/apps/realtime/src/handlers/file-doc.redis-store.test.ts b/apps/realtime/src/handlers/file-doc.redis-store.test.ts index 7565f5e2069..8ebd3e66eb6 100644 --- a/apps/realtime/src/handlers/file-doc.redis-store.test.ts +++ b/apps/realtime/src/handlers/file-doc.redis-store.test.ts @@ -160,7 +160,6 @@ function createIo(sockets: FakeSocket[]) { in: vi.fn(() => ({ socketsLeave: () => {} })), local: { to }, sockets: { sockets: { get: (id: string) => sockets.find((socket) => socket.id === id) } }, - on: vi.fn(), serverSideEmit: vi.fn(), } as unknown as IRoomManager['io'] } diff --git a/apps/realtime/src/handlers/file-doc.test.ts b/apps/realtime/src/handlers/file-doc.test.ts index bfa2979403f..e20140897c0 100644 --- a/apps/realtime/src/handlers/file-doc.test.ts +++ b/apps/realtime/src/handlers/file-doc.test.ts @@ -61,7 +61,10 @@ interface SentMessage { } /** An `io` mock that records every server-originated emit with its target/except. */ -function createIo(deliver?: (message: SentMessage) => void) { +function createIo( + deliver?: (message: SentMessage) => void, + fetchSockets = async () => [...testSockets.values()] +) { const sent: SentMessage[] = [] const emit = (message: SentMessage) => { sent.push(message) @@ -72,7 +75,7 @@ function createIo(deliver?: (message: SentMessage) => void) { const channel = (target: string, excluded: string[] = []) => ({ except: (exclude: string) => channel(target, [...excluded, exclude]), emit: (event: string, payload: unknown) => emit({ target, except: excluded, event, payload }), - fetchSockets: async () => [...testSockets.values()], + fetchSockets, }) const to = vi.fn((target: string) => channel(target)) const inFn = vi.fn((socketId: string) => ({ @@ -87,7 +90,6 @@ function createIo(deliver?: (message: SentMessage) => void) { in: inFn, local: { to }, sockets: { sockets: testSockets }, - on: vi.fn(), serverSideEmit: vi.fn(), } as unknown as IRoomManager['io'], sent, @@ -1107,6 +1109,71 @@ describe('setupWorkspaceFileDocHandlers', () => { expect(sent.some((m) => m.event === FILE_DOC_EVENTS.MESSAGE)).toBe(false) }) + it('delivers a roster change that arrived while the last presence read was failing', async () => { + vi.useFakeTimers() + try { + let reads = 0 + const { io, sent } = createIo(undefined, async () => { + reads += 1 + if (reads === 3) await b.handlers[FILE_DOC_EVENTS.JOIN]({ fileId: 'file-1', clientId: 2 }) + if (reads <= 3) throw new Error('Redis publish failed') + return [...testSockets.values()] + }) + const a = setup('socket-a', io, { userId: 'user-a' }) + const b = setup('socket-b', io, { userId: 'user-b' }) + + await a.handlers[FILE_DOC_EVENTS.JOIN]({ fileId: 'file-1', clientId: 1 }) + await vi.advanceTimersByTimeAsync(1_000) + + const roster = sent.filter((m) => m.event === FILE_DOC_EVENTS.PRESENCE).at(-1)?.payload as + | { users: Array<{ socketId: string }> } + | undefined + expect(roster?.users.map((user) => user.socketId).sort()).toEqual(['socket-a', 'socket-b']) + } finally { + vi.useRealTimers() + } + }) + + it('re-reads the roster when membership changes during a successful presence read', async () => { + let reads = 0 + const { io, sent } = createIo(undefined, async () => { + reads += 1 + const snapshot = [...testSockets.values()] + if (reads === 1) await b.handlers[FILE_DOC_EVENTS.JOIN]({ fileId: 'file-1', clientId: 2 }) + return snapshot + }) + const a = setup('socket-a', io, { userId: 'user-a' }) + const b = setup('socket-b', io, { userId: 'user-b' }) + + await a.handlers[FILE_DOC_EVENTS.JOIN]({ fileId: 'file-1', clientId: 1 }) + await vi.waitFor(() => expect(reads).toBe(2)) + + const roster = sent.filter((m) => m.event === FILE_DOC_EVENTS.PRESENCE).at(-1)?.payload as + | { users: Array<{ socketId: string }> } + | undefined + expect(roster?.users.map((user) => user.socketId).sort()).toEqual(['socket-a', 'socket-b']) + }) + + it('does not retry a presence read the adapter timed out', async () => { + vi.useFakeTimers() + try { + let reads = 0 + const { io, sent } = createIo(undefined, async () => { + reads += 1 + throw new Error('timeout reached while waiting for fetchSockets response') + }) + const a = setup('socket-a', io, { userId: 'user-a' }) + + await a.handlers[FILE_DOC_EVENTS.JOIN]({ fileId: 'file-1', clientId: 1 }) + await vi.advanceTimersByTimeAsync(1_000) + + expect(reads).toBe(1) + expect(sent.some((m) => m.event === FILE_DOC_EVENTS.PRESENCE)).toBe(false) + } finally { + vi.useRealTimers() + } + }) + it('broadcasts a server-authenticated presence roster on join, one entry per session', async () => { const { io, sent } = createIo() const a = setup('socket-a', io, { userId: 'user-a', userName: 'Ada', userImage: 'ada.png' }) diff --git a/apps/realtime/src/handlers/file-doc.ts b/apps/realtime/src/handlers/file-doc.ts index 53b1c0a748f..a86c8387757 100644 --- a/apps/realtime/src/handlers/file-doc.ts +++ b/apps/realtime/src/handlers/file-doc.ts @@ -11,9 +11,11 @@ * each applied update is published to the file's stream and every task's tailer applies it to its own * doc and fans it out to its own clients, so two tasks can never split-brain. Consequently doc-sync * messages are broadcast LOCALLY ({@link io.local}) and cross-task delivery rides the stream — NOT the - * Socket.IO adapter (that would double-deliver). Awareness/presence stay on the adapter: they are - * ephemeral and need neither convergence nor replay. When `REDIS_URL` is unset the store is disabled - * and this falls back to the original single-replica behavior (local doc, local seed, local fan-out). + * Socket.IO adapter (that would double-deliver). Awareness stays on the adapter: it is ephemeral and + * needs neither convergence nor replay. The presence roster is read across replicas with `fetchSockets`, + * and each replica (nudged by `serverSideEmit`) emits it to its own clients locally. When `REDIS_URL` + * is unset the store is disabled and this falls back to the original single-replica behavior (local + * doc, local seed, local fan-out). * * Durability: the live doc is projected back to the file's markdown server-side — debounced while it is * edited and flushed when the last collaborator leaves — via the app's `/persist` endpoint (the app @@ -137,10 +139,6 @@ interface FileDocOwner { /** The owning user — used to tell a reconnect (same user reusing its Yjs client * id) from a spoof (a different user binding a peer's id). */ userId: string - /** Server-authenticated display identity for the presence roster (from the socket's - * session, never the client-set awareness — so a peer cannot spoof it). */ - userName: string - avatarUrl: string | null } interface FileDocRoom { @@ -536,17 +534,27 @@ const FILE_DOC_PRESENCE_RETRIES = 2 const FILE_DOC_PRESENCE_RETRY_MS = 100 const presenceRefreshes = new WeakMap>() -function setupFileDocPresence(io: Server) { - if (presenceRefreshes.has(io)) return - presenceRefreshes.set(io, new Map()) +/** + * Refreshes this replica's file-editor rosters when another replica's membership changes. Register + * once per server, at startup. + */ +export function registerFileDocPresenceRelay(io: Server) { io.on(FILE_DOC_PRESENCE_CHANGED, (name: string, fileId: string) => { if (fileDocRooms.has(name)) void refreshFileDocPresence(io, name, fileId) }) } +/** + * The adapter rejects a `fetchSockets` that misses a replica's reply after its fixed `requestsTimeout` + * (5 s); a per-call timeout is ignored, so retrying one only stalls the roster further. + */ +function isAdapterTimeout(error: unknown): boolean { + return getErrorMessage(error).startsWith('timeout reached') +} + async function refreshFileDocPresence(io: Server, name: string, fileId: string) { - const refreshes = presenceRefreshes.get(io) - if (!refreshes) return + const refreshes = presenceRefreshes.get(io) ?? new Map() + presenceRefreshes.set(io, refreshes) const pending = refreshes.get(name) if (pending) { pending.revision += 1 @@ -562,12 +570,18 @@ async function refreshFileDocPresence(io: Server, name: string, fileId: string) try { sockets = await io.to(name).except(`${name}:pending`).fetchSockets() } catch (error) { - if (failures >= FILE_DOC_PRESENCE_RETRIES) throw error - failures += 1 - await sleep(FILE_DOC_PRESENCE_RETRY_MS) + const retryable = !isAdapterTimeout(error) && failures < FILE_DOC_PRESENCE_RETRIES + if (retryable) { + failures += 1 + await sleep(FILE_DOC_PRESENCE_RETRY_MS) + continue + } + // A membership change that arrived during the failed read still needs its own snapshot. + if (refresh.revision === revision) throw error + failures = 0 continue } - if (revision !== refresh.revision) continue + failures = 0 const users: FileDocPresenceUser[] = [] for (const socket of sockets) { const { fileDocPresence } = socket.data as AuthenticatedSocket['data'] @@ -575,7 +589,8 @@ async function refreshFileDocPresence(io: Server, name: string, fileId: string) } // Each replica refreshes its own clients, so a delayed remote snapshot cannot overwrite theirs. io.local.to(name).except(`${name}:pending`).emit(FILE_DOC_EVENTS.PRESENCE, { fileId, users }) - return + // Emitting before re-reading keeps clients updated under sustained churn. + if (refresh.revision === revision) return } } catch (error) { logger.warn('Skipping file document presence refresh', { error: getErrorMessage(error) }) @@ -590,14 +605,20 @@ async function refreshFileDocPresence(io: Server, name: string, fileId: string) * for display, so a second tab of the same account still registers as present (mirroring the * canvas presence model). Deduping here instead would drop the current user's other sessions * asymmetrically (only one socket survives), so each client could never reliably self-exclude. - * Identity comes from each owner's server-authenticated session — never the client-set awareness - * — so a peer cannot spoof or suppress an entry. + * Identity comes from each admitted socket's server-authenticated session (`socket.data`) — never + * the client-set awareness — so a peer cannot spoof or suppress an entry. */ function broadcastFileDocPresence(io: Server, name: string, room: FileDocRoom) { if (getFileDocStore().enabled) io.serverSideEmit(FILE_DOC_PRESENCE_CHANGED, name, room.fileId) void refreshFileDocPresence(io, name, room.fileId) } +/** Drops a local socket's roster identity for `name`, so later `fetchSockets` reads omit it. */ +function clearFileDocPresence(io: Server, socketId: string, name: string) { + const socket = io.sockets.sockets.get(socketId) as AuthenticatedSocket | undefined + if (socket?.data.fileDocPresence?.roomName === name) socket.data.fileDocPresence = undefined +} + /** Whether the client has recorded that it seeded the document's initial content. */ function isDocSeeded(doc: Y.Doc): boolean { return doc.getMap(FILE_DOC_SEED.configMap).get(FILE_DOC_SEED.flag) === true @@ -1526,8 +1547,7 @@ export function cleanupFileDocForSocket(socketId: string, io: Server, endOfLife // caret via its awareness removal. const clientMap = room.owners.get(socketId) room.owners.delete(socketId) - const socket = io.sockets.sockets.get(socketId) as AuthenticatedSocket | undefined - if (socket?.data.fileDocPresence?.roomName === name) socket.data.fileDocPresence = undefined + clearFileDocPresence(io, socketId, name) if (clientMap !== undefined && clientMap.size > 0) { // Fires the awareness `update` handler with a non-socket origin → the removals // are broadcast to every remaining client, so the departed carets vanish. @@ -1553,7 +1573,6 @@ export function setupWorkspaceFileDocHandlers( roomManager: IRoomManager ) { const io = roomManager.io - setupFileDocPresence(io) // Owner-qualified intent fences delayed admissions and leaves across document switches. let currentFileRoom: string | null = null /** Co-mounted providers share invalidation membership until their last admission settles. */ @@ -1854,10 +1873,7 @@ export function setupWorkspaceFileDocHandlers( if (clientMap.size === 0) { entry.owners.delete(otherSid) socketToRoomName.delete(otherSid) - const previousSocket = io.sockets.sockets.get(otherSid) as - | AuthenticatedSocket - | undefined - if (previousSocket) previousSocket.data.fileDocPresence = undefined + clearFileDocPresence(io, otherSid, name) io.in(otherSid).socketsLeave(name) broadcastFileDocPresence(io, name, entry) } @@ -1884,7 +1900,7 @@ export function setupWorkspaceFileDocHandlers( clientMap = new Map() entry.owners.set(socket.id, clientMap) } - clientMap.set(clientId, { clientId, userId, userName, avatarUrl }) + clientMap.set(clientId, { clientId, userId }) socketToRoomName.set(socket.id, name) registered = true await changeMembership(() => socket.leave(`${name}:pending`)) diff --git a/apps/realtime/src/index.ts b/apps/realtime/src/index.ts index 29240c4e656..c7b568152b7 100644 --- a/apps/realtime/src/index.ts +++ b/apps/realtime/src/index.ts @@ -7,7 +7,7 @@ import { assertSchemaCompatibility } from '@/database/preflight' import { env } from '@/env' import { setupAllHandlers } from '@/handlers' import { waitForConnectionCleanup } from '@/handlers/connection' -import { flushAllFileDocRooms } from '@/handlers/file-doc' +import { flushAllFileDocRooms, registerFileDocPresenceRelay } from '@/handlers/file-doc' import { getFileDocStore, initFileDocStore } from '@/handlers/file-doc-store' import { type AuthenticatedSocket, authenticateSocket } from '@/middleware/auth' import { type IRoomManager, MemoryRoomManager, RedisRoomManager } from '@/rooms' @@ -61,6 +61,7 @@ async function main() { // Initialize the shared Yjs backend for collaborative file docs (Redis Streams). Enabled only when // REDIS_URL is set; otherwise the relay runs its original single-replica in-memory doc path. await initFileDocStore(env.REDIS_URL) + registerFileDocPresenceRelay(io) // Set up authentication middleware io.use(authenticateSocket) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx index 5638255d2e0..c1e9e519f12 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx @@ -142,10 +142,11 @@ interface FileViewerProps { */ collaborative?: boolean /** - * Called (debounced) with the markdown document's leading-heading text while the file is still - * untitled, so the caller can name the file after it. Only wired for the editable markdown editor. + * Called with the file id and the markdown document's leading-heading text once the user finishes + * that heading while the file is still untitled, so the caller can name the file after it. Only wired + * for the editable markdown editor. */ - onDeriveTitleFromHeading?: (headingText: string) => void + onDeriveTitleFromHeading?: (fileId: string, headingText: string) => void /** * Let an open markdown file claim Cmd/Ctrl+F for find-in-document. Set wherever the file is the * whole pane the user is reading — the Files page, the mothership file view, the public share diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/editor-lifecycle.test.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/editor-lifecycle.test.tsx index 95d66c457af..674f9f97158 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/editor-lifecycle.test.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/editor-lifecycle.test.tsx @@ -150,6 +150,8 @@ class FakeFileDocProvider { } interface RenderOptions { + file?: WorkspaceFileRecord + onDeriveTitleFromHeading?: (fileId: string, headingText: string) => void collaborative?: boolean downloadSourceRef?: { current: FileDownloadSource | null } isStreaming?: boolean @@ -170,7 +172,7 @@ async function render( root.render( @@ -471,4 +474,42 @@ describe('loaded rich editor lifecycle', () => { expect(abandonedOnChange).not.toHaveBeenCalled() expect(abandonedSave).not.toHaveBeenCalled() }) + + it('names an untitled file after its heading only once the cursor leaves the heading', async () => { + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }) + try { + const onDeriveTitleFromHeading = vi.fn() + await render('# My Pro', '# My Pro', true, { + file: { ...FILE, name: 'untitled.md' }, + onDeriveTitleFromHeading, + }) + const editor = getEditor() + await act(async () => { + editor.commands.setTextSelection(editor.state.doc.content.size - 1) + editor.commands.insertContent('ject') + }) + await act(async () => vi.advanceTimersByTime(5_000)) + expect(onDeriveTitleFromHeading).not.toHaveBeenCalled() + + await act(async () => editor.commands.splitBlock()) + await act(async () => editor.commands.insertContent('Body')) + expect(onDeriveTitleFromHeading).toHaveBeenCalledOnce() + expect(onDeriveTitleFromHeading).toHaveBeenCalledWith(FILE.id, 'My Project') + } finally { + vi.useRealTimers() + } + }) + + it('never renames an untitled file that was opened and left without edits', async () => { + const onDeriveTitleFromHeading = vi.fn() + await render('# Existing Heading\n\nBody', '# Existing Heading\n\nBody', true, { + file: { ...FILE, name: 'untitled.md' }, + onDeriveTitleFromHeading, + }) + const editor = getEditor() + await act(async () => editor.commands.blur()) + await act(async () => root.render(null)) + + expect(onDeriveTitleFromHeading).not.toHaveBeenCalled() + }) }) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx index cfed0501286..9022a383a46 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/rich-markdown-editor.tsx @@ -75,7 +75,10 @@ import { LinkHoverCard } from '@/app/workspace/[workspaceId]/files/components/fi import { TableBubbleMenu } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/table-menu' import { normalizeMarkdownContent } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/normalize-content' import { isRoundTripSafe } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/round-trip-safety' -import { firstHeadingTitle } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading' +import { + firstHeadingTitle, + isSelectionInLeadingHeading, +} from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading' import { TextEditor } from '@/app/workspace/[workspaceId]/files/components/file-viewer/text-editor' import { useEditableFileContent } from '@/app/workspace/[workspaceId]/files/components/file-viewer/use-editable-file-content' import { useSelectionCopyBridge } from '@/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge' @@ -100,9 +103,6 @@ const EXTENSIONS = createMarkdownEditorExtensions({ const STREAM_REPARSE_THROTTLE_THRESHOLD = 40_000 const STREAM_REPARSE_THROTTLE_MS = 120 -/** Debounce before naming a still-untitled file after its leading heading, so it fires once typing settles. */ -const DERIVE_TITLE_DEBOUNCE_MS = 600 - function warnRichMarkdownPasteLimit(reason?: 'paste' | 'formatting') { if (reason === 'formatting') { toast.warning('Pasted text kept without automatic formatting', { @@ -245,11 +245,12 @@ interface RichMarkdownEditorProps { */ collaborative?: boolean /** - * Called (debounced) with the document's leading-heading text while the file is still untitled, so the - * caller can name the file after it. Omitted on read-only/non-editable surfaces. See - * {@link isUntitledName}. + * Called at most once per mount with the file id and the document's leading-heading text, when the + * local user finishes that heading (the cursor leaves it, the editor blurs, or it unmounts) while the + * file is still untitled, so the caller can name the file after it. Omitted on read-only/non-editable + * surfaces. See {@link isUntitledName}. */ - onDeriveTitleFromHeading?: (headingText: string) => void + onDeriveTitleFromHeading?: (fileId: string, headingText: string) => void /** * Claim Cmd/Ctrl+F for find-in-document. Off by default, because this editor also renders as a * preview pane beside something that owns the shortcut itself. Every find surface binds its own @@ -424,7 +425,7 @@ interface LoadedRichMarkdownEditorProps { /** Reports client autosave eligibility; collaborative documents are persisted by the relay. */ onClientAutosaveChange: (canAutosave: boolean) => void /** See {@link RichMarkdownEditorProps.onDeriveTitleFromHeading}. */ - onDeriveTitleFromHeading?: (headingText: string) => void + onDeriveTitleFromHeading?: (fileId: string, headingText: string) => void /** See {@link RichMarkdownEditorProps.enableFind}. */ enableFind: boolean onEditSource?: () => void @@ -586,13 +587,25 @@ export function LoadedRichMarkdownEditor({ const saveFrontmatterResolverRef = useRef(resolveSaveFrontmatter) /** - * While the file is still unnamed, name it after its leading heading: `onDeriveTitleFromHeading` is - * called (debounced) so the caller can rename the file, and `fileNameRef` lets the onUpdate handler - * read the current name without re-subscribing. See {@link isUntitledName}. + * While the file is still unnamed, name it after its leading heading once the local user finishes + * typing it — never on an idle pause, which would name the file after half a heading. A local edit + * marks the title pending; leaving the heading, blurring, or unmounting commits it, at most once per + * mount. `fileNameRef` lets the editor callbacks read the current name without re-subscribing. See + * {@link isUntitledName}. */ const onDeriveTitleFromHeadingRef = useRef(onDeriveTitleFromHeading) const fileNameRef = useRef(file.name) - const deriveTitleTimerRef = useRef | null>(null) + const headingTitlePendingRef = useRef(false) + const headingTitleCommittedRef = useRef(false) + const commitHeadingTitle = (editor: Editor) => { + if (!headingTitlePendingRef.current || headingTitleCommittedRef.current) return + headingTitlePendingRef.current = false + if (!editor.isEditable || !isUntitledName(fileNameRef.current)) return + const title = firstHeadingTitle(editor.state.doc) + if (!title) return + headingTitleCommittedRef.current = true + onDeriveTitleFromHeadingRef.current?.(file.id, title) + } /** * Read in the RAF tick so an already-scheduled tick still sees the latest edit kind (it can change * between sessions within one turn, e.g. an append followed by a rewrite). @@ -809,36 +822,28 @@ export function LoadedRichMarkdownEditor({ lastSyncedFrontmatterRef.current = saveFrontmatterResolverRef.current() onChangeRef.current(applyFrontmatter(lastSyncedFrontmatterRef.current, md)) } - // While the file is still untitled, name it after its leading heading once typing settles — but - // only for the LOCAL user's own edits. `isChangeOrigin` is true for a remote Yjs change (a peer - // typing); bail BEFORE touching the timer so a remote edit never cancels or reschedules the local - // user's pending rename (and every client doesn't schedule the same rename from a peer's - // not-yet-synced heading). It is false for local edits and non-collaborative surfaces. - if (isChangeOrigin(transaction)) return - // Local edit: restart the debounce. Clearing first cancels a stale rename if the heading was - // removed/changed before it fired; the timer re-derives the title from the live doc rather than a - // value captured now, so it can never name the file after a heading the user has since changed. + // Only the LOCAL user's edits mark the heading title pending: `isChangeOrigin` is true for a + // remote Yjs change, so every peer doesn't rename from someone else's half-synced heading. // `editor.isEditable` is the autosave gate (canEdit + settled + collab-ready), so a view-only - // viewer or the not-yet-editable mount seed never schedules a rename. - if (deriveTitleTimerRef.current) clearTimeout(deriveTitleTimerRef.current) - if ( - !editor.isEditable || - !isUntitledName(fileNameRef.current) || - firstHeadingTitle(editor.state.doc) === null - ) - return - deriveTitleTimerRef.current = setTimeout(() => { - const liveEditor = editorInstanceRef.current - if (!liveEditor || !liveEditor.isEditable || !isUntitledName(fileNameRef.current)) return - const title = firstHeadingTitle(liveEditor.state.doc) - if (title) onDeriveTitleFromHeadingRef.current?.(title) - }, DERIVE_TITLE_DEBOUNCE_MS) + // viewer or the not-yet-editable mount seed never marks it, and `docChanged` skips the empty + // update `setEditable` emits on open. The commit re-checks the name and heading. + if (isChangeOrigin(transaction)) return + if (editor.isEditable && transaction.docChanged) headingTitlePendingRef.current = true + if (!isSelectionInLeadingHeading(editor.state)) commitHeadingTitle(editor) + }, + onSelectionUpdate: ({ editor }) => { + if (!isSelectionInLeadingHeading(editor.state)) commitHeadingTitle(editor) + }, + onBlur: ({ editor }) => { + // Switching windows blurs the editor mid-heading; only a blur within the page finishes it. + if (document.hasFocus()) commitHeadingTitle(editor) }, }) useEffect( () => () => { - if (deriveTitleTimerRef.current) clearTimeout(deriveTitleTimerRef.current) + const liveEditor = editorInstanceRef.current + if (liveEditor && !liveEditor.isDestroyed) commitHeadingTitle(liveEditor) }, [] ) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading.ts b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading.ts index 3877519ca97..b36dfc94a36 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading.ts +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/title-heading.ts @@ -1,4 +1,5 @@ import type { Node as ProseMirrorNode } from '@tiptap/pm/model' +import type { EditorState } from '@tiptap/pm/state' /** The text of the document's leading heading (any level), or null when the first block isn't a heading. */ export function firstHeadingTitle(doc: ProseMirrorNode): string | null { @@ -7,3 +8,9 @@ export function firstHeadingTitle(doc: ProseMirrorNode): string | null { const text = first.textContent.trim() return text.length > 0 ? text : null } + +/** True while the selection head sits inside the document's leading heading. */ +export function isSelectionInLeadingHeading(state: EditorState): boolean { + const { $head } = state.selection + return $head.depth > 0 && $head.index(0) === 0 && state.doc.firstChild?.type.name === 'heading' +} diff --git a/apps/sim/app/workspace/[workspaceId]/files/files.tsx b/apps/sim/app/workspace/[workspaceId]/files/files.tsx index e786ee23518..b2e8d34cd92 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/files.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/files.tsx @@ -550,12 +550,14 @@ function FilesContent() { /** * While a file is still untitled, name it after the leading heading the user types in its editor. The - * editor reports the heading text (debounced); here we re-check the file is still untitled, derive a - * unique `.md` name among its folder siblings, and rename. A no-op once the file has a real name. + * editor reports the heading once the user finishes it — possibly on unmount, after the route has + * already moved to another file, so the file is looked up by the id it reports rather than read from + * the current selection. Re-checks the file is still untitled, derives a unique `.md` name among its + * folder siblings, and renames. A no-op once the file has a real name. */ const handleDeriveTitleFromHeading = useCallback( - (headingText: string) => { - const currentFile = selectedFileRef.current + (fileId: string, headingText: string) => { + const currentFile = filesRef.current.find((f) => f.id === fileId) if (!currentFile || !isUntitledName(currentFile.name)) return const derived = deriveMarkdownFileName(headingText) if (!derived) return diff --git a/apps/sim/app/workspace/[workspaceId]/home/home.tsx b/apps/sim/app/workspace/[workspaceId]/home/home.tsx index 59e7c41599a..9cbebd085ea 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/home.tsx +++ b/apps/sim/app/workspace/[workspaceId]/home/home.tsx @@ -294,10 +294,7 @@ function HomeContent({ chatId, userName, userId }: HomeProps) { const contexts = handoff.contexts ?? [] for (const context of contexts) handleContextAdd(context) addMothershipContexts(contexts) - // `handleContextAdd` is a body function, so it is a new value every render; - // listing it would re-run this drain on every render. Omitted deliberately to - // keep it one-shot — and harmless either way, since `consume` clears the entry - // atomically and any re-run would find nothing. + // `handleContextAdd` is omitted: it is a new function every render. }, [chatId, workspaceId, prepareResourceViewForAgentTurn, sendMessage]) function resolveResourceFromContext( diff --git a/apps/sim/app/workspace/[workspaceId]/logs/components/log-details/log-details.tsx b/apps/sim/app/workspace/[workspaceId]/logs/components/log-details/log-details.tsx index 725d6111468..f8933e4b096 100644 --- a/apps/sim/app/workspace/[workspaceId]/logs/components/log-details/log-details.tsx +++ b/apps/sim/app/workspace/[workspaceId]/logs/components/log-details/log-details.tsx @@ -58,7 +58,6 @@ import { MothershipHandoffStorage } from '@/lib/core/utils/browser-storage' import { filterHiddenOutputKeys } from '@/lib/logs/execution/trace-spans/trace-spans' import type { TraceSpan } from '@/lib/logs/types' import { sendMothershipMessage } from '@/lib/mothership/events' -import { DELETED_WORKFLOW_LABEL } from '@/lib/workflows/workflow-labels' /** * Deep imports on purpose: importing these back through the parent `logs/components` * barrel forms a parent->child cycle that would keep the barrel edge to the snapshot @@ -78,6 +77,7 @@ import { import { formatDate, getDisplayStatus, + getLogWorkflowLabel, resolveLogWorkflowId, StatusBadge, TriggerBadge, @@ -352,18 +352,10 @@ export function LogDetailsContent({ log, onActiveTabChange }: LogDetailsContentP const isWorkflowExecutionLog = (log.trigger === 'manual' && !!log.duration) || !!log.executionData?.traceSpans - /** - * The workflow this run belongs to, when it is still reachable. Null for Sim - * agent jobs and deleted workflows, which render their label as static text. - */ + /** Null when the label renders as static text rather than a link. */ const openableWorkflowId = resolveLogWorkflowId(log) - const workflowLabel = - log.trigger === 'mothership' - ? log.jobTitle || 'Untitled Job' - : openableWorkflowId - ? log.workflow?.name || 'Unknown' - : DELETED_WORKFLOW_LABEL + const workflowLabel = getLogWorkflowLabel(log) const hasCostInfo = !!(isWorkflowExecutionLog && log.cost) const showWorkflowState = diff --git a/apps/sim/app/workspace/[workspaceId]/logs/logs.tsx b/apps/sim/app/workspace/[workspaceId]/logs/logs.tsx index d47cdbb9878..f5ce2b93d20 100644 --- a/apps/sim/app/workspace/[workspaceId]/logs/logs.tsx +++ b/apps/sim/app/workspace/[workspaceId]/logs/logs.tsx @@ -52,7 +52,6 @@ import { type WorkflowData, } from '@/lib/logs/search-suggestions' import { SEARCH_DEBOUNCE_MS } from '@/lib/url-state' -import { DELETED_WORKFLOW_LABEL } from '@/lib/workflows/workflow-labels' import { LogsEmptyState } from '@/app/workspace/[workspaceId]/components/resource/components/resource-empty-state' import type { ResourceAction } from '@/app/workspace/[workspaceId]/components/resource/components/resource-header' import type { @@ -109,6 +108,7 @@ import { Dashboard, LogDetails, LogRowContextMenu } from './components' import { formatDate, getDisplayStatus, + getLogWorkflowLabel, type LogStatus, parseDuration, resolveLogWorkflowId, @@ -790,13 +790,7 @@ export default function Logs() { logs.map((log) => { const formattedDate = formatDate(log.createdAt) const displayStatus = getDisplayStatus(log.status) - const isMothershipJob = log.trigger === 'mothership' - const isDeletedWorkflow = !isMothershipJob && !log.workflow?.id && !log.workflowId - const workflowName = isMothershipJob - ? log.jobTitle || 'Untitled Job' - : isDeletedWorkflow - ? DELETED_WORKFLOW_LABEL - : log.workflow?.name || 'Unknown' + const workflowName = getLogWorkflowLabel(log) const durationMs = parseDuration({ duration: log.duration ?? undefined }) const durationText = diff --git a/apps/sim/app/workspace/[workspaceId]/logs/utils.test.ts b/apps/sim/app/workspace/[workspaceId]/logs/utils.test.ts index 000074c7c51..448c92c4f9d 100644 --- a/apps/sim/app/workspace/[workspaceId]/logs/utils.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/logs/utils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { resolveLogWorkflowId } from './utils' +import { getLogWorkflowLabel, resolveLogWorkflowId } from './utils' describe('resolveLogWorkflowId', () => { it('returns null for Sim agent jobs even when a workflow id exists', () => { @@ -11,4 +11,32 @@ describe('resolveLogWorkflowId', () => { }) ).toBeNull() }) + + it('returns null for an archived workflow that still carries its id', () => { + expect( + resolveLogWorkflowId({ + trigger: 'manual', + workflowId: 'wf-1', + workflow: { id: 'wf-1', archivedAt: '2026-01-01T00:00:00.000Z' }, + }) + ).toBeNull() + }) +}) + +describe('getLogWorkflowLabel', () => { + it('keeps an archived workflow named', () => { + expect( + getLogWorkflowLabel({ + trigger: 'manual', + workflowId: 'wf-1', + workflow: { id: 'wf-1', name: 'Nightly sync', archivedAt: '2026-01-01T00:00:00.000Z' }, + }) + ).toBe('Nightly sync') + }) + + it('labels a hard-deleted workflow, which leaves no workflow id, as deleted', () => { + expect(getLogWorkflowLabel({ trigger: 'manual', workflowId: null, workflow: null })).toBe( + 'Deleted Workflow' + ) + }) }) diff --git a/apps/sim/app/workspace/[workspaceId]/logs/utils.ts b/apps/sim/app/workspace/[workspaceId]/logs/utils.ts index c795883b965..a2dd27e1d6f 100644 --- a/apps/sim/app/workspace/[workspaceId]/logs/utils.ts +++ b/apps/sim/app/workspace/[workspaceId]/logs/utils.ts @@ -3,6 +3,7 @@ import { Badge, formatChartLatency } from '@sim/emcn' import { formatRelativeTime } from '@sim/utils/formatting' import { format } from 'date-fns' import { getIntegrationMetadata } from '@/lib/logs/get-trigger-options' +import { DELETED_WORKFLOW_LABEL } from '@/lib/workflows/workflow-labels' import { getBlock } from '@/blocks/registry' import { CORE_TRIGGER_TYPES } from '@/stores/logs/filters/types' @@ -17,8 +18,9 @@ export const LOG_COLUMNS = { /** * Resolves the workflow a log row points at, or null when there is nowhere to - * navigate. Sim agent jobs have no workflow of their own, and a deleted - * workflow leaves both id fields empty. + * navigate. Sim agent jobs have no workflow of their own, a deleted workflow + * leaves both id fields empty, and an archived workflow keeps its id but can no + * longer be opened. * * Single source of truth for "is this log's workflow reachable" — the list row, * its context menu, and the details panel must agree, or a row can render as @@ -27,12 +29,29 @@ export const LOG_COLUMNS = { export function resolveLogWorkflowId(log: { trigger?: string | null workflowId?: string | null - workflow?: { id?: string } | null + workflow?: { id?: string; archivedAt?: string | null } | null }): string | null { - if (log.trigger === 'mothership') return null + if (log.trigger === 'mothership' || log.workflow?.archivedAt) return null return log.workflow?.id || log.workflowId || null } +/** + * The workflow label a log row and its details panel show. An archived workflow keeps its name + * (it just cannot be opened, see {@link resolveLogWorkflowId}); only a hard-deleted workflow, which + * leaves no workflow id, reads as deleted. + */ +export function getLogWorkflowLabel(log: { + trigger?: string | null + workflowId?: string | null + jobTitle?: string | null + workflow?: { id?: string; name?: string | null; archivedAt?: string | null } | null +}): string { + if (log.trigger === 'mothership') return log.jobTitle || 'Untitled Job' + return log.workflow?.id || log.workflowId + ? log.workflow?.name || 'Unknown' + : DELETED_WORKFLOW_LABEL +} + /** Path to a workflow in the editor. */ export function workflowEditorPath(workspaceId: string, workflowId: string): string { return `/workspace/${workspaceId}/w/${workflowId}` diff --git a/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/table-grid.tsx b/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/table-grid.tsx index 051f9cb9660..88681d3a975 100644 --- a/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/table-grid.tsx +++ b/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/table-grid.tsx @@ -4251,20 +4251,18 @@ export function TableGrid({ * count label and the multi-row "Run workflows" action. */ const contextMenuRowIds = useMemo(() => { - if (!contextMenu.isOpen || !contextMenu.row) return [] - if ( - !rowSelectionIsEmpty(rowSelection) && - rowSelectionIncludes(rowSelection, contextMenu.row.id) - ) { + const contextRow = contextMenu.row + if (!contextMenu.isOpen || !contextRow) return [] + if (!rowSelectionIsEmpty(rowSelection) && rowSelectionIncludes(rowSelection, contextRow.id)) { const ids: string[] = [] for (const row of rows) { if (rowSelectionIncludes(rowSelection, row.id)) ids.push(row.id) } - return ids.length > 0 ? ids : [contextMenu.row.id] + return ids.length > 0 ? ids : [contextRow.id] } const sel = normalizedSelection if (sel) { - const contextRowArrayIndex = rows.findIndex((r) => r.id === contextMenu.row!.id) + const contextRowArrayIndex = rows.findIndex((r) => r.id === contextRow.id) const isInSelection = contextRowArrayIndex >= sel.startRow && contextRowArrayIndex <= sel.endRow if (isInSelection) { @@ -4275,10 +4273,10 @@ export function TableGrid({ const row = rows[r] if (row) ids.push(row.id) } - return ids.length > 0 ? ids : [contextMenu.row.id] + return ids.length > 0 ? ids : [contextRow.id] } } - return [contextMenu.row.id] + return [contextRow.id] }, [contextMenu.isOpen, contextMenu.row, rowSelection, normalizedSelection, rows]) /** @@ -4328,16 +4326,14 @@ export function TableGrid({ * send every column (undefined). */ const contextMenuColumnIds = useMemo(() => { - if (!contextMenu.isOpen || !contextMenu.row) return undefined - if ( - !rowSelectionIsEmpty(rowSelection) && - rowSelectionIncludes(rowSelection, contextMenu.row.id) - ) { + const contextRow = contextMenu.row + if (!contextMenu.isOpen || !contextRow) return undefined + if (!rowSelectionIsEmpty(rowSelection) && rowSelectionIncludes(rowSelection, contextRow.id)) { return undefined } const sel = normalizedSelection if (!sel) return undefined - const contextRowArrayIndex = rows.findIndex((r) => r.id === contextMenu.row!.id) + const contextRowArrayIndex = rows.findIndex((r) => r.id === contextRow.id) if (contextRowArrayIndex < sel.startRow || contextRowArrayIndex > sel.endRow) return undefined // Collapsed here too (not only in buildTableSelectionContext) because this // also decides whether the menu item reads "cell range" or "rows". diff --git a/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.test.ts b/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.test.ts index 45050a5f0ff..c539f3b1501 100644 --- a/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/tables/[tableId]/components/table-grid/utils.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { MAX_TABLE_SELECTION_ROWS } from '@/lib/mothership/chat/selection-context' -import { buildTableSelectionContext, drainTargetForChip } from './utils' +import { buildTableSelectionContext } from './utils' const rowIds = (count: number) => Array.from({ length: count }, (_, i) => `r${i}`) @@ -33,14 +33,3 @@ describe('buildTableSelectionContext', () => { expect(context.columnIds).toEqual(['c0', 'c1', 'c2']) }) }) - -describe('drainTargetForChip', () => { - it('still yields a full cap when every exclusion lands in the loaded prefix', () => { - // The worst case for a gutter select-all: exclusions are filtered out AFTER - // loading, so loading only the cap would leave the chip short of the count - // the menu already advertised. - const excluded = 30 - - expect(drainTargetForChip(excluded) - excluded).toBe(MAX_TABLE_SELECTION_ROWS) - }) -}) diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx index e21624dc885..63effdd697d 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx @@ -63,6 +63,7 @@ import { clearDependentToolParams } from '@/app/workspace/[workspaceId]/w/[workf import type { StoredTool } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/types' import { isAgentToolBlock, + isCoreAgentToolBlock, isCustomToolAlreadySelected, isMcpToolAlreadySelected, isWorkflowAlreadySelected, @@ -78,7 +79,6 @@ import { isCustomBlockType } from '@/blocks/custom/build-config' import { useCustomBlockOverlayVersion } from '@/blocks/custom/client-overlay' import { getTileIconColorClass } from '@/blocks/icon-color' import type { BlockConfig, SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types' -import { BUILT_IN_TOOL_TYPES } from '@/blocks/utils' import { useMcpOauthPopup } from '@/hooks/mcp/use-mcp-oauth-popup' import { useMcpTools } from '@/hooks/mcp/use-mcp-tools' import { useWorkspaceCredential } from '@/hooks/queries/credentials' @@ -1370,8 +1370,8 @@ export const ToolInput = memo(function ToolInput({ }) } - const builtInTools = toolBlocks.filter((block) => BUILT_IN_TOOL_TYPES.has(block.type)) - const integrations = toolBlocks.filter((block) => !BUILT_IN_TOOL_TYPES.has(block.type)) + const builtInTools = toolBlocks.filter(isCoreAgentToolBlock) + const integrations = toolBlocks.filter((block) => !isCoreAgentToolBlock(block)) if (builtInTools.length > 0) { groups.push({ diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils.ts index 9805ba82472..49a0bb1428b 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils.ts +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils.ts @@ -5,9 +5,7 @@ import type { BlockConfig } from '@/blocks/types' /** * Core blocks the agent tool picker offers even though they are not * `category: 'tools'`. Stored as BASE types and matched after stripping the - * `_vN` suffix, so a superseded version never has to be re-listed here at - * cutover time. Naming a specific version instead is what kept `table_v2` out - * of the picker while the toolbar had already moved to it. + * `_vN` suffix, so a new version never has to be re-listed here. */ const CORE_AGENT_TOOL_TYPES = new Set([ 'api', @@ -32,6 +30,14 @@ export function isAgentToolBlock( ): boolean { if (block.hideFromToolbar) return false if (block.category === 'tools') return true + return isCoreAgentToolBlock(block) +} + +/** + * Checks whether a block is one of the platform's core agent tools, which the + * picker lists under "Built-in Tools" rather than "Integrations". + */ +export function isCoreAgentToolBlock(block: Pick): boolean { return CORE_AGENT_TOOL_TYPES.has(stripVersionSuffix(block.type)) } diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-resource-folders.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-resource-folders.ts index a9d0838562f..87e58aaf39e 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-resource-folders.ts +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-resource-folders.ts @@ -26,7 +26,7 @@ export interface ResourceFolder { export interface UseResourceFoldersResult { folders: ResourceFolder[] - /** Canonical path keyed by folder id, for resolving a stored value back to a row. */ + /** Folders keyed by canonical path, for resolving a stored value back to a row. */ byPath: Map isLoading: boolean isPlaceholderData: boolean @@ -102,7 +102,7 @@ export function useResourceFolders( return generic.data ?? [] }, [isFileResource, files.data, generic.data]) - return useMemo(() => { + const { folders, byPath } = useMemo(() => { const byId = new Map(rows.map((row) => [row.id, row])) const folders: ResourceFolder[] = [] for (const row of rows) { @@ -126,17 +126,27 @@ export function useResourceFolders( }) } folders.sort((a, b) => a.path.localeCompare(b.path)) + return { folders, byPath: new Map(folders.map((folder) => [folder.path, folder])) } + }, [rows]) - const query = isFileResource ? files : generic - return { + const { + isLoading, + isPlaceholderData, + error: queryError, + refetch: refetchQuery, + } = isFileResource ? files : generic + + return useMemo( + () => ({ folders, - byPath: new Map(folders.map((folder) => [folder.path, folder])), - isLoading: query.isLoading, - isPlaceholderData: query.isPlaceholderData, - error: query.error ? getErrorMessage(query.error, 'Failed to load folders') : null, + byPath, + isLoading, + isPlaceholderData, + error: queryError ? getErrorMessage(queryError, 'Failed to load folders') : null, refetch: () => { - void query.refetch() + void refetchQuery() }, - } - }, [rows, isFileResource, files, generic]) + }), + [folders, byPath, isLoading, isPlaceholderData, queryError, refetchQuery] + ) } diff --git a/apps/sim/background/cleanup-soft-deletes.test.ts b/apps/sim/background/cleanup-soft-deletes.test.ts index 22169f396ea..727d24d7a3d 100644 --- a/apps/sim/background/cleanup-soft-deletes.test.ts +++ b/apps/sim/background/cleanup-soft-deletes.test.ts @@ -5,7 +5,7 @@ import { resetDbChainMock, schemaMock, } from '@sim/testing' -import { billingStorageMock, billingStorageMockFns } from '@sim/testing/mocks/billing-storage.mock' +import { billingStorageMock } from '@sim/testing/mocks/billing-storage.mock' import { knowledgeDocumentsServiceMock, knowledgeDocumentsServiceMockFns, @@ -82,10 +82,7 @@ vi.mock('@/lib/folders/naming', () => ({ deduplicateFolderName: mockDeduplicateF import { runCleanupSoftDeletes } from '@/background/cleanup-soft-deletes' -const { mockDecrementStorageUsageForBillingContextInTx, mockResolveStorageBillingContext } = - billingStorageMockFns const { mockHardDeleteDocuments } = knowledgeDocumentsServiceMockFns -mockDecrementStorageUsageForBillingContextInTx.mockResolvedValue(undefined) mockHardDeleteDocuments.mockImplementation(async (ids: string[]) => ids.length) const { mockDeleteFiles } = storageServiceMockFns @@ -113,13 +110,6 @@ describe('cleanup soft deletes', () => { mockSelectRowsByIdChunks.mockReset().mockResolvedValue([]) mockDeleteFiles.mockReset().mockResolvedValue({ deleted: 0, failed: [] }) mockChunkedBatchDelete.mockReset().mockResolvedValue({ deleted: 0, failed: 0 }) - mockResolveStorageBillingContext.mockResolvedValue({ - workspaceId: 'ws-1', - billedAccountUserId: 'user-1', - billingEntity: { type: 'user', id: 'user-1' }, - plan: 'free', - customStorageLimitGB: null, - }) }) it('keeps unbilled metadata rows whose object deletion failed', async () => { @@ -169,8 +159,7 @@ describe('cleanup soft deletes', () => { expect(mockDeleteFiles).toHaveBeenCalledWith(['mothership/chat-file'], 'mothership') expect(dbChainMockFns.delete).toHaveBeenCalled() - expect(mockResolveStorageBillingContext).not.toHaveBeenCalled() - expect(mockDecrementStorageUsageForBillingContextInTx).not.toHaveBeenCalled() + expect(dbChainMockFns.transaction).not.toHaveBeenCalled() }) it('fails before deleting storage when canonical size metadata is missing', async () => { @@ -460,8 +449,7 @@ describe('organization-owned Search retention cleanup', () => { await runCleanupSoftDeletes(organizationPayload) expect(mockDeleteFiles).toHaveBeenCalledWith(['knowledge-base/org-key'], 'knowledge-base') expect(mockPrepareChatCleanup).toHaveBeenCalledWith(['org-chat'], organizationPayload.label) - expect(mockResolveStorageBillingContext).not.toHaveBeenCalled() - expect(mockDecrementStorageUsageForBillingContextInTx).not.toHaveBeenCalled() + expect(dbChainMockFns.transaction).not.toHaveBeenCalled() expect(mockBatchDeleteByWorkspaceAndTimestamp).not.toHaveBeenCalled() expect(mockChunkedBatchDelete).not.toHaveBeenCalled() expect(mockScopedChunkedBatchDelete).toHaveBeenCalledWith( @@ -537,7 +525,7 @@ describe('organization-owned Search retention cleanup', () => { await runCleanupSoftDeletes(organizationPayload) expect(mockDeleteFiles).toHaveBeenCalledWith(['knowledge-base/orphan'], 'knowledge-base') expect(mockDeleteFileMetadata).toHaveBeenCalledWith('knowledge-base/orphan') - expect(mockResolveStorageBillingContext).not.toHaveBeenCalled() + expect(dbChainMockFns.transaction).not.toHaveBeenCalled() }) it('rejects an ambiguous batch before reading or deleting resources', async () => { diff --git a/apps/sim/blocks/blocks/agent.test.ts b/apps/sim/blocks/blocks/agent.test.ts index c07d6f85079..c701852780e 100644 --- a/apps/sim/blocks/blocks/agent.test.ts +++ b/apps/sim/blocks/blocks/agent.test.ts @@ -26,27 +26,6 @@ describe('AgentBlock', () => { } describe('tools.config.params function', () => { - it('normalizes fallback models and drops the key when none survive', () => { - const withRows = paramsFunction({ - model: 'gpt-4o', - fallbackModels: [ - { id: 'a', model: ' claude-sonnet-5 ' }, - { id: 'b', model: 'sim-auto' }, - { id: 'c', model: 'claude-sonnet-5' }, - { id: 'd', model: 'openrouter/x', apiKey: '{{OPENROUTER_API_KEY}}' }, - { id: 'e', model: 'openrouter/y', apiKey: '' }, - ], - }) - expect(withRows.fallbackModels).toEqual([ - { model: 'claude-sonnet-5' }, - { model: 'openrouter/x', apiKey: '{{OPENROUTER_API_KEY}}' }, - { model: 'openrouter/y' }, - ]) - - const empty = paramsFunction({ model: 'gpt-4o', fallbackModels: [{ id: 'a', model: '' }] }) - expect(empty).not.toHaveProperty('fallbackModels') - }) - it('should filter out tools with usageControl set to "none"', () => { const params = { model: 'gpt-4o', diff --git a/apps/sim/blocks/blocks/agent.ts b/apps/sim/blocks/blocks/agent.ts index b81fc0a4c74..fb03f56714a 100644 --- a/apps/sim/blocks/blocks/agent.ts +++ b/apps/sim/blocks/blocks/agent.ts @@ -1,7 +1,5 @@ import { createLogger } from '@sim/logger' -import { omit } from '@sim/utils/object' import { AgentIcon } from '@/components/icons' -import { normalizeFallbackModels } from '@/lib/workflows/blocks/fallback-models' import { getModelFallbackSubBlock, MODEL_FALLBACK_INPUTS } from '@/blocks/model-fallbacks' import type { BlockConfig } from '@/blocks/types' import { AuthMode, IntegrationType } from '@/blocks/types' @@ -470,12 +468,7 @@ Return ONLY the JSON array.`, }, params: (params: Record) => { const normalizedFiles = normalizeFileInput(params.files) - const withFiles = normalizedFiles ? { ...params, files: normalizedFiles } : params - const fallbackModels = normalizeFallbackModels(params.fallbackModels) - const baseParams = - fallbackModels.length > 0 - ? { ...withFiles, fallbackModels } - : omit(withFiles, ['fallbackModels']) + const baseParams = normalizedFiles ? { ...params, files: normalizedFiles } : params // If tools array is provided, handle tool usage control if (params.tools && Array.isArray(params.tools)) { diff --git a/apps/sim/blocks/blocks/file.ts b/apps/sim/blocks/blocks/file.ts index 49f40d20e7c..3f43fb0039f 100644 --- a/apps/sim/blocks/blocks/file.ts +++ b/apps/sim/blocks/blocks/file.ts @@ -1636,7 +1636,8 @@ export const FileV5Block: BlockConfig = { type: 'short-input' as SubBlockType, mode: 'advanced', placeholder: '1', - description: 'Which matching anchor occurrence to use, starting at 1.', + description: + 'Which start or insert anchor match to use, starting at 1. The end anchor is the first match after it.', condition: { field: 'operation', value: 'file_edit', @@ -2245,7 +2246,11 @@ export const FileV5Block: BlockConfig = { anchor: { type: 'string', description: 'Line after which content is inserted' }, startAnchor: { type: 'string', description: 'First line deleted by an anchored deletion' }, endAnchor: { type: 'string', description: 'Ending line preserved by an anchored deletion' }, - editOccurrence: { type: 'number', description: 'Matching anchor occurrence, starting at 1' }, + editOccurrence: { + type: 'number', + description: + 'Start or insert anchor match to use, starting at 1; the end anchor is the first match after it', + }, archiveName: { type: 'string', description: 'Name for the compressed .zip archive' }, decompressInput: { type: 'json', diff --git a/apps/sim/blocks/utils.test.ts b/apps/sim/blocks/utils.test.ts index 62405c0965b..b4fe17c07b8 100644 --- a/apps/sim/blocks/utils.test.ts +++ b/apps/sim/blocks/utils.test.ts @@ -59,7 +59,6 @@ vi.mock('@/stores/providers/store', () => ({ vi.mock('@/lib/oauth/utils', () => oauthUtilsMock) import { - BUILT_IN_TOOL_TYPES, getApiKeyCondition, getSerializedModelProviderId, parseOptionalBooleanInput, @@ -69,13 +68,6 @@ import { } from '@/blocks/utils' import { getProviderFromModel } from '@/providers/utils' -describe('BUILT_IN_TOOL_TYPES', () => { - it('classifies the current File block instead of the legacy File block', () => { - expect(BUILT_IN_TOOL_TYPES.has('file_v5')).toBe(true) - expect(BUILT_IN_TOOL_TYPES.has('file')).toBe(false) - }) -}) - const BASE_CLOUD_MODELS: Record = { 'gpt-4o': 'openai', 'claude-sonnet-4-5': 'anthropic', diff --git a/apps/sim/blocks/utils.ts b/apps/sim/blocks/utils.ts index 34a1183d338..37110375bab 100644 --- a/apps/sim/blocks/utils.ts +++ b/apps/sim/blocks/utils.ts @@ -670,32 +670,6 @@ export function normalizeFileInput( return files } -/** - * Block types that are built-in to the platform (as opposed to third-party integrations). - * Used to categorize tools in the tool selection dropdown. - */ -export const BUILT_IN_TOOL_TYPES = new Set([ - 'api', - 'file_v5', - 'function', - 'knowledge', - 'search', - 'thinking', - 'image_generator', - 'image_generator_v2', - 'video_generator', - 'video_generator_v2', - 'video_generator_v3', - 'vision', - 'translate', - 'tts', - 'stt', - 'memory', - 'table_v2', - 'webhook_request', - 'workflow', -]) - /** * Shared wand configuration for the Response Format code subblock. * Used by Agent and Mothership blocks. diff --git a/apps/sim/ee/workspace-forking/application/sync-default.ts b/apps/sim/ee/workspace-forking/application/sync-default.ts index 8c5592aa3ca..d81ddd9fdd5 100644 --- a/apps/sim/ee/workspace-forking/application/sync-default.ts +++ b/apps/sim/ee/workspace-forking/application/sync-default.ts @@ -67,8 +67,10 @@ export const setForkSyncDefault = defineForkUseCase< } // Rank 6: lock the live members whose value differs, in id order, since a multi-row // UPDATE locks in plan order and could deadlock against other multi-workspace writers. - // NO KEY UPDATE keeps FK inserts into these workspaces (KEY SHARE) from queueing behind - // the write. Nothing else writes this column under the lineage lock, so the filter holds. + // NO KEY UPDATE keeps plain FK inserts into these workspaces (KEY SHARE) from queueing + // behind the write; policy readers (`lockActiveWorkspace`, FOR SHARE) do wait, so they + // observe the flipped value. Nothing else writes this column under the lineage lock, so + // the filter holds. const toChange = await tx .select({ id: workspace.id }) .from(workspace) diff --git a/apps/sim/executor/handlers/agent/agent-handler.test.ts b/apps/sim/executor/handlers/agent/agent-handler.test.ts index bc063dcd541..c23d3eab9f2 100644 --- a/apps/sim/executor/handlers/agent/agent-handler.test.ts +++ b/apps/sim/executor/handlers/agent/agent-handler.test.ts @@ -366,14 +366,17 @@ describe('AgentBlockHandler', () => { } ) - it('refuses an evaluation model as a conversational fallback before executing the primary', async () => { + it('refuses an evaluation model as a conversational fallback, without retry, before executing the primary', async () => { await expect( handler.execute(mockContext, mockBlock, { model: 'gpt-4o', messages: [{ role: 'user', content: 'Hello' }], fallbackModels: [{ model: 'jev-latest' }], }) - ).rejects.toThrow('Evaluation models cannot serve as chat fallbacks') + ).rejects.toMatchObject({ + message: 'Evaluation models cannot serve as chat fallbacks', + retryable: false, + }) expect(mockExecuteProviderRequest).not.toHaveBeenCalled() }) }) @@ -692,6 +695,7 @@ describe('AgentBlockHandler', () => { const providerFor = (model: string) => { if (model.startsWith('gpt')) return 'openai' if (model.startsWith('claude')) return 'anthropic' + if (model.startsWith('vertex/')) return 'vertex' if (model === 'blacklisted-model') throw new Error('provider blacklisted') return 'openai' } @@ -1079,6 +1083,26 @@ describe('AgentBlockHandler', () => { ) }) + it('skips a fallback on another provider that needs its own family credentials', async () => { + mockExecuteProviderRequest + .mockRejectedValueOnce(new Error('down')) + .mockResolvedValueOnce(providerResponse('gpt-4o-mini')) + + await handler.execute(mockContext, mockBlock, { + ...baseInputs, + fallbackModels: [{ model: 'vertex/gemini-3.1-pro' }, { model: 'gpt-4o-mini' }], + }) + + expect(mockExecuteProviderRequest.mock.calls.map(([, request]) => request.model)).toEqual([ + 'gpt-4o', + 'gpt-4o-mini', + ]) + expect(mockAgentLogger.warn).toHaveBeenCalledWith( + 'Fallback model unusable; skipping', + expect.objectContaining({ model: 'vertex/gemini-3.1-pro' }) + ) + }) + it('skips a fallback the workspace does not permit', async () => { mockValidateModelProvider.mockImplementation(async (_user, _workspace, model: string) => { if (model === 'claude-sonnet-5') throw new Error('Model not permitted') diff --git a/apps/sim/executor/handlers/agent/agent-handler.ts b/apps/sim/executor/handlers/agent/agent-handler.ts index c35b521d580..63732de0db1 100644 --- a/apps/sim/executor/handlers/agent/agent-handler.ts +++ b/apps/sim/executor/handlers/agent/agent-handler.ts @@ -9,6 +9,7 @@ import { projectResolvedModelInput, selectModelSchemaInputPaths, } from '@/lib/execution/model-input-provenance' +import { NonRetryableExecutionError } from '@/lib/execution/non-retryable-error' import { readAvailableCustomToolByIdOrTitleAsExecutor } from '@/lib/internal/custom-tools/read-available-by-id-or-title' import { resolveExecutorFileMaterializationContext } from '@/lib/internal/file/materialization-context' import { discoverMcpServerToolsAsExecutor } from '@/lib/internal/mcp/discover-tools' @@ -91,6 +92,7 @@ import type { import { collectBlockData } from '@/executor/utils/block-data' import { stringifyJSON } from '@/executor/utils/json' import { + assertFallbackFamilyCredentials, getModelFallbacks, PROVIDER_FAMILY_CREDENTIAL_FIELDS, recordModelFallbacks, @@ -591,7 +593,7 @@ export class AgentBlockHandler implements BlockHandler { logger ) if (configuredFallbacks.some((candidate) => isEvaluationModel(candidate.model))) { - throw new Error('Evaluation models cannot serve as chat fallbacks') + throw new NonRetryableExecutionError('Evaluation models cannot serve as chat fallbacks') } const retry = nodeMetadata?.retry const fallbacksHeld = retry !== undefined && !retry.isFinalTry @@ -2639,6 +2641,7 @@ export class AgentBlockHandler implements BlockHandler { try { candidateProviderId = getProviderFromModel(candidate.model) await validateModelProvider(ctx.userId, ctx.workspaceId, candidate.model, ctx) + assertFallbackFamilyCredentials(candidateProviderId, config.primaryProviderId) } catch (error) { this.warnFallbackSkipped(ctx, block, candidate.model, 'unusable', error) continue diff --git a/apps/sim/executor/utils/model-fallback-request.ts b/apps/sim/executor/utils/model-fallback-request.ts index 17b26763a67..20f9183c9d7 100644 --- a/apps/sim/executor/utils/model-fallback-request.ts +++ b/apps/sim/executor/utils/model-fallback-request.ts @@ -1,11 +1,11 @@ import { createLogger } from '@sim/logger' import { omit } from '@sim/utils/object' import { resolveFallbackTuning } from '@/lib/workflows/blocks/fallback-models' -import { providerRequiresFamilyCredentials } from '@/blocks/utils' import { validateModelProvider } from '@/ee/access-control/utils/permission-check' import { isRetryableBlockError } from '@/executor/execution/block-retry' import type { BlockRetryAttempt, ExecutionContext } from '@/executor/types' import { + assertFallbackFamilyCredentials, getModelFallbacks, PROVIDER_FAMILY_CREDENTIAL_FIELDS, recordModelFallbacks, @@ -71,12 +71,7 @@ export async function executeModelRequestWithFallbacks({ try { candidateProviderId = getProviderFromModel(candidate.model) await validateModelProvider(ctx.userId, ctx.workspaceId, candidate.model, ctx) - if ( - candidateProviderId !== providerId && - providerRequiresFamilyCredentials(candidateProviderId) - ) { - throw new Error('Fallback requires credentials from a different provider family') - } + assertFallbackFamilyCredentials(candidateProviderId, providerId) } catch (error) { logger.warn( 'Fallback model unusable; skipping', diff --git a/apps/sim/executor/utils/model-fallbacks.ts b/apps/sim/executor/utils/model-fallbacks.ts index 84e29e366b6..6bdcc3e3936 100644 --- a/apps/sim/executor/utils/model-fallbacks.ts +++ b/apps/sim/executor/utils/model-fallbacks.ts @@ -6,6 +6,7 @@ import { isWholeEnvVarReference, normalizeFallbackModels, } from '@/lib/workflows/blocks/fallback-models' +import { providerRequiresFamilyCredentials } from '@/blocks/utils' import type { ExecutionContext } from '@/executor/types' import { projectResolvedSecretDiagnosticContent } from '@/executor/utils/resolved-secret-content-projection' import type { SerializedBlock } from '@/serializer/types' @@ -23,6 +24,22 @@ export const PROVIDER_FAMILY_CREDENTIAL_FIELDS = [ 'bedrockRegion', ] as const +/** + * Refuses a fallback on another provider that needs a Vertex credential, Bedrock keys, or an + * Azure endpoint: those fields exist only for the primary's provider family. + */ +export function assertFallbackFamilyCredentials( + candidateProviderId: string, + primaryProviderId: string +): void { + if ( + candidateProviderId !== primaryProviderId && + providerRequiresFamilyCredentials(candidateProviderId) + ) { + throw new Error('Fallback requires credentials from a different provider family') + } +} + /** Validates stored key references before admitting their resolved values to a provider. */ export function getModelFallbacks( ctx: ExecutionContext, diff --git a/apps/sim/lib/api/contracts/logs.ts b/apps/sim/lib/api/contracts/logs.ts index f923b13a3db..a9a8d37a04f 100644 --- a/apps/sim/lib/api/contracts/logs.ts +++ b/apps/sim/lib/api/contracts/logs.ts @@ -101,6 +101,7 @@ const workflowSummarySchema = z workspaceId: z.string().nullable(), createdAt: z.string().nullable(), updatedAt: z.string().nullable(), + archivedAt: z.string().nullable(), }) .partial() diff --git a/apps/sim/lib/api/contracts/v1/admin/dashboard-workspaces.ts b/apps/sim/lib/api/contracts/v1/admin/dashboard-workspaces.ts index 3f557bc58ab..f59fc4af528 100644 --- a/apps/sim/lib/api/contracts/v1/admin/dashboard-workspaces.ts +++ b/apps/sim/lib/api/contracts/v1/admin/dashboard-workspaces.ts @@ -90,6 +90,17 @@ const adminDashboardWorkspaceSourceImpactSchema = z.object({ }) ) .max(500), + /** Source-org blocks published elsewhere but used here; these placements break after the move. */ + inboundCustomBlocks: z + .array( + z.object({ + id: z.string(), + type: z.string(), + name: z.string(), + movingWorkspaceUsage: adminDashboardCustomBlockUsageSchema, + }) + ) + .max(500), /** Non-empty means the move is blocked until the fork is disconnected. */ blockingForkEdges: z .array( @@ -125,6 +136,7 @@ const adminDashboardWorkspaceSourceImpactSchema = z.object({ truncated: z .object({ customBlocks: z.number().int().min(0), + inboundCustomBlocks: z.number().int().min(0), permissionGroups: z.number().int().min(0), collaboratorCaps: z.number().int().min(0), forkEdges: z.number().int().min(0), diff --git a/apps/sim/lib/api/contracts/v2/files.ts b/apps/sim/lib/api/contracts/v2/files.ts index f62f33c840d..baf8975fc76 100644 --- a/apps/sim/lib/api/contracts/v2/files.ts +++ b/apps/sim/lib/api/contracts/v2/files.ts @@ -1214,7 +1214,9 @@ export const v2EditFileContentBodySchema = z .int('occurrence must be a whole number') .min(1, 'occurrence must be at least 1') .optional() - .describe('Matching anchor occurrence, starting at 1. Defaults to 1.'), + .describe( + 'Which beforeAnchor match to use, starting at 1. Defaults to 1. afterAnchor is the first match after it.' + ), }) .strict(), z @@ -1233,7 +1235,7 @@ export const v2EditFileContentBodySchema = z .int('occurrence must be a whole number') .min(1, 'occurrence must be at least 1') .optional() - .describe('Matching anchor occurrence, starting at 1. Defaults to 1.'), + .describe('Which anchor match to insert after, starting at 1. Defaults to 1.'), }) .strict(), z @@ -1256,7 +1258,9 @@ export const v2EditFileContentBodySchema = z .int('occurrence must be a whole number') .min(1, 'occurrence must be at least 1') .optional() - .describe('Matching anchor occurrence, starting at 1. Defaults to 1.'), + .describe( + 'Which startAnchor match to use, starting at 1. Defaults to 1. endAnchor is the first match after it.' + ), }) .strict(), ]) diff --git a/apps/sim/lib/auth/connectors/providers.ts b/apps/sim/lib/auth/connectors/providers.ts index 794c77ce038..4a1fce8f745 100644 --- a/apps/sim/lib/auth/connectors/providers.ts +++ b/apps/sim/lib/auth/connectors/providers.ts @@ -1284,7 +1284,7 @@ export function buildConnectorProviders(): GenericOAuthConfig[] { const zuid = profile.ZUID?.toString() if (!zuid) { - logger.error('Invalid Zoho Desk profile response:', profile) + logger.error('Invalid Zoho Desk profile response', { keys: Object.keys(profile) }) return null } @@ -1418,7 +1418,9 @@ export function buildConnectorProviders(): GenericOAuthConfig[] { const zuid = profile.ZUID?.toString() if (!zuid) { - logger.error('Invalid ManageEngine ServiceDesk Plus profile response:', profile) + logger.error('Invalid ManageEngine ServiceDesk Plus profile response', { + keys: Object.keys(profile), + }) return null } @@ -1475,7 +1477,7 @@ export function buildConnectorProviders(): GenericOAuthConfig[] { const profile = await response.json() if (!profile.data) { - logger.error('Invalid X profile response:', profile) + logger.error('Invalid X profile response', { keys: Object.keys(profile) }) return null } @@ -1578,7 +1580,7 @@ export function buildConnectorProviders(): GenericOAuthConfig[] { const user = profile.data?.user if (!user?.open_id) { - logger.error('Invalid TikTok profile response:', profile) + logger.error('Invalid TikTok profile response', { keys: Object.keys(profile) }) return null } diff --git a/apps/sim/lib/billing/organizations/membership-external-removal.test.ts b/apps/sim/lib/billing/organizations/membership-external-removal.test.ts index 3951dbc3860..2634d05a351 100644 --- a/apps/sim/lib/billing/organizations/membership-external-removal.test.ts +++ b/apps/sim/lib/billing/organizations/membership-external-removal.test.ts @@ -1,5 +1,5 @@ -import { credential, knowledgeBase, member, user, workspaceFiles } from '@sim/db/schema' -import { dbChainMockFns, hasMockCondition, queueTableRows, resetDbChainMock } from '@sim/testing' +import { member } from '@sim/db/schema' +import { dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing' import { organizationMemberLimitsMock, organizationMemberLimitsMockFns, @@ -36,56 +36,6 @@ describe('external organization access removal', () => { ) }) - it('preserves organization indexes and cached documents when a creator leaves with no workspaces', async () => { - queueTableRows(member, [{ id: 'membership', userId: 'departing', role: 'admin' }]) - queueTableRows(member, [{ userId: 'surviving-owner' }]) - queueTableRows(user, [{ email: 'departing@example.test' }]) - queueTableRows(user, [{ email: 'departing@example.test' }]) - queueTableRows(user, [{ id: 'surviving-owner' }]) - queueTableRows(workspaceFiles, [{ id: 'cached-file', context: 'knowledge-base' }]) - dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'membership' }]) - - const result = await removeUserFromOrganization({ - userId: 'departing', - organizationId: 'organization-a', - memberId: 'membership', - skipBillingLogic: true, - onError: 'throw', - }) - - expect(result.success).toBe(true) - expect(dbChainMockFns.update).toHaveBeenCalledWith(knowledgeBase) - expect(dbChainMockFns.update).toHaveBeenCalledWith(workspaceFiles) - expect(dbChainMockFns.update).not.toHaveBeenCalledWith(credential) - expect(dbChainMockFns.set).toHaveBeenCalledWith({ - userId: 'surviving-owner', - updatedAt: expect.any(Date), - }) - for (const table of [knowledgeBase, workspaceFiles]) { - expect( - dbChainMockFns.where.mock.calls.some( - ([condition]) => - hasMockCondition( - condition, - (node) => - node.type === 'eq' && - node.left === table.organizationId && - node.right === 'organization-a' - ) && - hasMockCondition( - condition, - (node) => - node.type === 'eq' && node.left === table.userId && node.right === 'departing' - ) && - hasMockCondition( - condition, - (node) => node.type === 'isNull' && node.column === table.workspaceId - ) - ) - ).toBe(true) - } - }) - it('rejects an actor demoted before the locked removal', async () => { queueTableRows(member, [{ id: 'membership', userId: 'target', role: 'member' }]) queueTableRows(member, [{ id: 'actor-membership', role: 'member' }]) diff --git a/apps/sim/lib/billing/storage/accounting.ts b/apps/sim/lib/billing/storage/accounting.ts index 5e9b4846f90..c050c647ef7 100644 --- a/apps/sim/lib/billing/storage/accounting.ts +++ b/apps/sim/lib/billing/storage/accounting.ts @@ -11,9 +11,12 @@ import type { DbTransaction } from '@/lib/db/types' import { lockProject, lockWorkspaceProject } from '@/lib/projects/membership' import type { EditableFileOwner } from '@/lib/workspace-files/ownership' -interface PreparedFileAccounting { +interface PreparedFileAccounting< + Billing = StorageBillingContext | ProjectStorageBillingContext, + Usage = number | undefined, +> { billing: Billing - mutation: { applyDelta(deltaBytes: number): Promise } + mutation: { applyDelta(deltaBytes: number): Promise } } export function prepareFileAccountingInTx( @@ -23,16 +26,18 @@ export function prepareFileAccountingInTx( export function prepareFileAccountingInTx( tx: DbTransaction, owner: { entityType: 'project'; entityId: string } -): Promise> +): Promise> export function prepareFileAccountingInTx( tx: DbTransaction, owner: EditableFileOwner ): Promise /** * Prepares retained-head accounting in the caller's transaction, after authorization and before - * directory, file, history or cleanup locks. Locks lifecycle, owner row, then payer; multi-owner - * callers must acquire all lifecycle locks first. Resolve from the canonical owner, never actor - * or creator. Apply one signed delta at finalization and notify only after the caller commits. + * directory, file, history or cleanup locks. Locks lifecycle then the owner row; a Project also + * locks its payer here, a workspace only when its non-zero delta is applied. A Project's lifecycle + * lock is exclusive, so its caller must not already hold that Project shared. Multi-owner callers + * must acquire all lifecycle locks first. Resolve from the canonical owner, never actor or + * creator. Apply one signed delta at finalization and notify only after the caller commits. */ export async function prepareFileAccountingInTx( tx: DbTransaction, diff --git a/apps/sim/lib/billing/storage/project-accounting.integration.ts b/apps/sim/lib/billing/storage/project-accounting.integration.ts index 175c79bc419..d495a3aa598 100644 --- a/apps/sim/lib/billing/storage/project-accounting.integration.ts +++ b/apps/sim/lib/billing/storage/project-accounting.integration.ts @@ -429,7 +429,7 @@ describe('Project storage admission, transfer, and reconciliation in PostgreSQL' expect(await sql`SELECT id FROM workspace_files`).toEqual([]) await sql`UPDATE organization SET storage_used_bytes = 100 WHERE id = 'organization-a'` for (const [delta, expected] of [ - [0, 100], + [0, entityType === 'workspace' ? undefined : 100], [-20, 80], ] as const) { await database.transaction(async (tx) => { diff --git a/apps/sim/lib/billing/storage/tracking.ts b/apps/sim/lib/billing/storage/tracking.ts index a206de18107..01ce54f9807 100644 --- a/apps/sim/lib/billing/storage/tracking.ts +++ b/apps/sim/lib/billing/storage/tracking.ts @@ -219,54 +219,74 @@ function assertWorkspaceStorageContext( } /** - * Locks the asserted owner and payer before directory/file/version mutation. The caller holds - * lifecycle locks and owns the transaction. The one-shot signed delta admits finalization, not - * a reservation. Workspace files update both ledgers; Projects update only their payer ledger. + * Locks the asserted owner before directory/file/version mutation. The caller holds lifecycle + * locks and owns the transaction. The one-shot signed delta admits finalization, not a + * reservation. Workspace files update both ledgers and lock the shared payer only when a non-zero + * delta is applied (`undefined` for zero); Projects lock their payer here and update only it. */ export async function prepareFileStorageMutationInTx( tx: DbTransaction, context: ProjectStorageBillingContext | StorageBillingContext -): Promise<{ applyDelta(deltaBytes: number): Promise }> { +): Promise<{ applyDelta(deltaBytes: number): Promise }> { + const limit = isStorageEnforcementEnabled() + ? getStorageLimitForBillingContext(context) + : undefined + let applied = false + const admitDelta = (deltaBytes: number) => { + if (applied) throw new Error('File storage delta was already applied') + if (!Number.isSafeInteger(deltaBytes)) throw new Error('Invalid file storage delta') + applied = true + } if ('workspaceId' in context) { - await mutateWorkspaceStorageUsage(tx, context.workspaceId, 0, 'decrement', undefined, context) - } else { - await lockProject(tx, context.projectId) - const [owner] = await tx - .select({ ownerId: project.ownerId, organizationId: project.organizationId }) - .from(project) - .where(eq(project.id, context.projectId)) - .for('update') - .limit(1) - if (!owner) throw new Error(`Project ${context.projectId} not found for storage accounting`) - const billingEntity: BillingEntity = owner.organizationId - ? { type: 'organization', id: owner.organizationId } - : { type: 'user', id: owner.ownerId } - if ( - owner.ownerId !== context.ownerId || - owner.organizationId !== context.organizationId || - billingEntity.type !== context.billingEntity.type || - billingEntity.id !== context.billingEntity.id || - (!owner.organizationId && context.billedAccountUserId !== owner.ownerId) - ) { - throw new Error( - `Storage payer changed for Project ${context.projectId}; resolve a fresh billing context` - ) + assertWorkspaceStorageContext( + await lockWorkspaceStorageForMutationInTx(tx, context.workspaceId), + context + ) + return { + async applyDelta(deltaBytes) { + admitDelta(deltaBytes) + if (deltaBytes === 0) return undefined + const result = await mutateWorkspaceStorageUsage( + tx, + context.workspaceId, + Math.abs(deltaBytes), + deltaBytes > 0 ? 'increment' : 'decrement', + limit, + context + ) + return result.updatedUsage + }, } } - const billingEntity = context.billingEntity + await lockProject(tx, context.projectId) + const [owner] = await tx + .select({ ownerId: project.ownerId, organizationId: project.organizationId }) + .from(project) + .where(eq(project.id, context.projectId)) + .for('update') + .limit(1) + if (!owner) throw new Error(`Project ${context.projectId} not found for storage accounting`) + const billingEntity: BillingEntity = owner.organizationId + ? { type: 'organization', id: owner.organizationId } + : { type: 'user', id: owner.ownerId } + if ( + owner.ownerId !== context.ownerId || + owner.organizationId !== context.organizationId || + billingEntity.type !== context.billingEntity.type || + billingEntity.id !== context.billingEntity.id || + (!owner.organizationId && context.billedAccountUserId !== owner.ownerId) + ) { + throw new Error( + `Storage payer changed for Project ${context.projectId}; resolve a fresh billing context` + ) + } const currentUsage = await lockStorageUsageForMutation(tx, billingEntity) if (!Number.isSafeInteger(currentUsage) || currentUsage < 0) { throw new Error(`Invalid storage usage for payer ${getPayerKey(billingEntity)}`) } - const limit = isStorageEnforcementEnabled() - ? getStorageLimitForBillingContext(context) - : undefined - let applied = false return { async applyDelta(deltaBytes) { - if (applied) throw new Error('File storage delta was already applied') - if (!Number.isSafeInteger(deltaBytes)) throw new Error('Invalid file storage delta') - applied = true + admitDelta(deltaBytes) const latestUsage = await lockStorageUsageForMutation(tx, billingEntity) const nextUsage = Math.max(0, latestUsage + deltaBytes) if (!Number.isSafeInteger(nextUsage)) throw new Error('Invalid file storage total') @@ -276,17 +296,6 @@ export async function prepareFileStorageMutationInTx( ) } if (deltaBytes === 0) return latestUsage - if ('workspaceId' in context) { - const result = await mutateWorkspaceStorageUsage( - tx, - context.workspaceId, - Math.abs(deltaBytes), - deltaBytes > 0 ? 'increment' : 'decrement', - limit, - context - ) - return result.updatedUsage - } if (deltaBytes < 0 && latestUsage < -deltaBytes) { logger.error('Clamping Project storage payer ledger underflow', { projectId: context.projectId, @@ -547,10 +556,13 @@ export async function applyStorageUsageDeltasInTx( /** * Locks the canonical workspace row before a file mutation that changes its storage ledger. * - * Locks the workspace ledger before file writes and their search-dispatch trigger. Shared file - * accounting acquires lifecycle, workspace, then payer locks before directory/file/history locks. - * Callers preparing accounting must do so before their first file mutation; this low-level helper - * only locks the workspace row. Re-locking it during the final signed delta never waits. + * Every workspace file writer takes its locks in one order: the Project lock (workspace writers + * only; Project-owner directory operations take none), this workspace row, + * the directory lock (only for operations that take one), the file rows and their writes (whose + * search-index trigger upserts the one-per-workspace dispatch queue row), then the payer row when + * a non-zero delta is applied. Call this before the directory lock and the first file write: a + * writer that holds either and only then waits on the workspace row deadlocks with one that holds + * the workspace row. Re-locking it later in the same transaction never waits. */ export async function lockWorkspaceStorageForMutationInTx( tx: DbOrTx, diff --git a/apps/sim/lib/core/config/env.ts b/apps/sim/lib/core/config/env.ts index 2e1eb59e70f..8edac360a93 100644 --- a/apps/sim/lib/core/config/env.ts +++ b/apps/sim/lib/core/config/env.ts @@ -299,7 +299,7 @@ export const env = createEnv({ KB_OPENAI_MODEL_NAME: z.string().optional(), // Azure deployment name serving the configured KB embedding model (used only when AZURE_OPENAI_* credentials are set). KB_EMBEDDING_MODEL: z.string().optional(), // Embedding model used for all new knowledge bases. Must be one of the supported model ids, or `ollama/` for a model on OLLAMA_URL; defaults to text-embedding-3-small. KB_IMAGE_EXTRACTION_MODEL: z.string().optional(), // Vision model that extracts text from knowledge-base images on sources using semantic image extraction (Confluence). Must be a Gemini 3 model in the catalog; anything else falls back to gemini-3.8-flash. - KB_IMAGE_EXTRACTIONS_PER_SYNC: z.number().optional(), // New or changed images a connector sync may send for vision extraction; the rest wait for the next sync. Defaults to 500. + KB_IMAGE_EXTRACTIONS_PER_SYNC: z.number().optional(), // New or changed images one connector sync run may send for vision extraction (each member-accounts continuation run can read up to this many); the rest wait for the next sync. Defaults to 500. EMBEDDING_OUTPUT_DIMS: z.coerce.number().int().positive().optional(), // Vector width new knowledge bases are stored at. One of 384, 768, 1024, 1536, 3072, and the configured KB_EMBEDDING_MODEL must emit it; anything else falls back to 1536. WAND_OPENAI_MODEL_NAME: z.string().optional(), // Wand generation OpenAI model name (works with both regular OpenAI and Azure OpenAI) OCR_AZURE_ENDPOINT: z.string().url().optional(), // Azure Mistral OCR service endpoint diff --git a/apps/sim/lib/execution/non-retryable-error.ts b/apps/sim/lib/execution/non-retryable-error.ts index 3b45ea8c1a9..21d9344ed88 100644 --- a/apps/sim/lib/execution/non-retryable-error.ts +++ b/apps/sim/lib/execution/non-retryable-error.ts @@ -1,6 +1,10 @@ import { findCause, getErrorMessage } from '@sim/utils/errors' -/** A failure whose operation may already have produced externally visible side effects. */ +/** + * A failure that block retries must not repeat: either the operation may already have produced + * externally visible side effects, or it failed on deterministic configuration (such as an invalid + * model fallback) that a retry would only fail again. + */ export class NonRetryableExecutionError extends Error { readonly retryable = false as const diff --git a/apps/sim/lib/file-retention/failure.ts b/apps/sim/lib/file-retention/failure.ts new file mode 100644 index 00000000000..2f949b66d9d --- /dev/null +++ b/apps/sim/lib/file-retention/failure.ts @@ -0,0 +1,18 @@ +import { createLogger } from '@sim/logger' + +const logger = createLogger('FileRetention') + +/** A budgeted run fails whole; otherwise one owner's failure is logged so it cannot starve the rest. */ +export async function isolateOwnerFailure( + budgeted: boolean, + owner: Record, + cleanup: () => Promise +): Promise { + try { + return await cleanup() + } catch (error) { + if (budgeted) throw error + logger.error('File retention failed for one owner', { ...owner, error }) + return 0 + } +} diff --git a/apps/sim/lib/file-retention/folders.ts b/apps/sim/lib/file-retention/folders.ts index ea6b9c6576f..2ff36b9e76d 100644 --- a/apps/sim/lib/file-retention/folders.ts +++ b/apps/sim/lib/file-retention/folders.ts @@ -5,9 +5,11 @@ import { consumeRowBudget, DEFAULT_MAX_BATCHES_PER_TABLE, type RowBudget, + selectRowsByIdChunks, } from '@/lib/cleanup/batch-delete' import { generateRestoreName } from '@/lib/core/utils/restore-name' import type { DbTransaction } from '@/lib/db/types' +import { isolateOwnerFailure } from '@/lib/file-retention/failure' import { FILES_PER_QUERY } from '@/lib/file-retention/versions' import { deduplicateFolderNameInScope } from '@/lib/folders/naming' import { lockWorkspaceProject } from '@/lib/projects/membership' @@ -131,27 +133,54 @@ export async function cleanupExpiredFileFolderInTx( return 1 } -/** Workspace file folders share the directory transaction used by Project folders. */ +/** + * Workspace file folders share the directory transaction used by Project folders. Only + * workspaces with an expired file folder are locked; without a budget, one workspace's failure + * is logged and the rest of the job continues. + */ export async function cleanupArchivedWorkspaceFileFolders( workspaceIds: string[], cutoff: Date, budget?: RowBudget ): Promise { - let total = 0 - for (const workspaceId of workspaceIds) { - for (let batch = 0; batch < DEFAULT_MAX_BATCHES_PER_TABLE && budget?.remaining !== 0; batch++) { - const removed = await cleanupDb.transaction(async (tx) => { - await lockWorkspaceProject(tx, workspaceId) - return cleanupExpiredFileFolderInTx( - tx, - { entityType: 'workspace', entityId: workspaceId }, - cutoff, - budget + const expired = await selectRowsByIdChunks(workspaceIds, (chunkIds, chunkLimit) => + cleanupDb + .selectDistinct({ workspaceId: folder.workspaceId }) + .from(folder) + .where( + and( + eq(folder.resourceType, 'file'), + inArray(folder.workspaceId, chunkIds), + isNotNull(folder.deletedAt), + lt(folder.deletedAt, cutoff) ) - }) - total += removed - if (!removed) break - } + ) + .limit(chunkLimit) + ) + let total = 0 + for (const { workspaceId } of expired) { + if (!workspaceId) continue + total += await isolateOwnerFailure(Boolean(budget), { workspaceId }, async () => { + let removed = 0 + for ( + let batch = 0; + batch < DEFAULT_MAX_BATCHES_PER_TABLE && budget?.remaining !== 0; + batch++ + ) { + const batchRemoved = await cleanupDb.transaction(async (tx) => { + await lockWorkspaceProject(tx, workspaceId) + return cleanupExpiredFileFolderInTx( + tx, + { entityType: 'workspace', entityId: workspaceId }, + cutoff, + budget + ) + }) + removed += batchRemoved + if (!batchRemoved) break + } + return removed + }) } return total } diff --git a/apps/sim/lib/file-retention/service.ts b/apps/sim/lib/file-retention/service.ts index 51225fb6d72..203f6575040 100644 --- a/apps/sim/lib/file-retention/service.ts +++ b/apps/sim/lib/file-retention/service.ts @@ -2,7 +2,9 @@ import type { CleanupJobPayload } from '@/lib/billing/cleanup-dispatcher' import { DEFAULT_DELETE_CHUNK_SIZE, DEFAULT_MAX_BATCHES_PER_TABLE, + type RowBudget, } from '@/lib/cleanup/batch-delete' +import { isolateOwnerFailure } from '@/lib/file-retention/failure' import { prepareLegacyFileArchiveCleanup } from '@/lib/file-retention/legacy-archive' import type { FileArchiveCleanup, @@ -41,15 +43,18 @@ const OWNER_ADAPTERS: FileOwnerAdapters = { prepareLegacyFileArchiveCleanup({ kind: 'organization', ids }, options), }, project: { - async versions(ids, _options, limit) { - let deleted = 0 + async versions(ids, { budgets, label }, limit) { + const budget: RowBudget = { remaining: limit } for (const id of ids) { - if (deleted >= limit) break - deleted += await cleanupProjectFileVersions(id, limit - deleted) + if (budget.remaining <= 0) break + await isolateOwnerFailure(Boolean(budgets), { label, projectId: id }, () => + cleanupProjectFileVersions(id, budget) + ) } + const deleted = limit - budget.remaining return { deleted, attempted: deleted } }, - async archive(ids, { budgets }) { + async archive(ids, { budgets, label }) { const files = budgets?.files ?? { remaining: DEFAULT_DELETE_CHUNK_SIZE * DEFAULT_MAX_BATCHES_PER_TABLE, } @@ -59,8 +64,13 @@ const OWNER_ADAPTERS: FileOwnerAdapters = { let deleted = 0 for (const id of ids) { if (files.remaining <= 0 && folders.remaining <= 0) break - deleted += await cleanupArchivedProjectFiles(id, files) - deleted += await cleanupArchivedProjectFileFolders(id, folders) + deleted += await isolateOwnerFailure( + Boolean(budgets), + { label, projectId: id }, + async () => + (await cleanupArchivedProjectFiles(id, files)) + + (await cleanupArchivedProjectFileFolders(id, folders)) + ) } return { cleanupStorage: async () => ({ filesDeleted: 0, deleteRows: async () => deleted }), diff --git a/apps/sim/lib/file-retention/versions.ts b/apps/sim/lib/file-retention/versions.ts index b9c72ace8a7..d2c0e65a42f 100644 --- a/apps/sim/lib/file-retention/versions.ts +++ b/apps/sim/lib/file-retention/versions.ts @@ -6,6 +6,11 @@ import { enqueueWorkspaceFileStorageCleanups } from '@/lib/uploads/contexts/work /** Always retain the current head and its newest nine predecessors. */ export const KEEP_SUPERSEDED = 9 +/** + * Superseded versions a free file keeps (its newest 100 with the current one); versions beyond it + * are pruned whatever their age. Paid plans are bounded only by the inline write-time ceiling. + */ +export const FREE_MAX_SUPERSEDED_VERSIONS = 99 export const FILES_PER_QUERY = 500 /** Rank each file independently; expiry never removes its protected recent history. */ diff --git a/apps/sim/lib/file-retention/workspace-versions.ts b/apps/sim/lib/file-retention/workspace-versions.ts index fa5ada96540..29a42f72c58 100644 --- a/apps/sim/lib/file-retention/workspace-versions.ts +++ b/apps/sim/lib/file-retention/workspace-versions.ts @@ -10,6 +10,7 @@ import { import type { FileRetentionOptions, FileVersionCleanupResult } from '@/lib/file-retention/types' import { FILES_PER_QUERY, + FREE_MAX_SUPERSEDED_VERSIONS, KEEP_SUPERSEDED, releaseExpiredFileVersions, selectExpiredFileVersions, @@ -17,7 +18,6 @@ import { import { MAX_SUPERSEDED_FILE_VERSIONS } from '@/lib/uploads/contexts/workspace/workspace-file-versions' const cleanupDb = dbFor('cleanup') -const FREE_MAX_SUPERSEDED_VERSIONS = 99 async function selectCandidateFileIds( workspaceIds: string[], diff --git a/apps/sim/lib/internal/file/operations.test.ts b/apps/sim/lib/internal/file/operations.test.ts index c622caf40c2..13d41897ff5 100644 --- a/apps/sim/lib/internal/file/operations.test.ts +++ b/apps/sim/lib/internal/file/operations.test.ts @@ -407,23 +407,34 @@ describe('file manage folder wiring', () => { { ...FOLDER_ROW, id: 'user-b', parentId: 'memory', name: 'user-b', path: 'memory/user-b' }, ] + const MEMORY_FILES = [ + { ...workspaceFile('a-self'), name: 'self.md', folderId: 'user-a' }, + { ...workspaceFile('a-people-self'), name: 'self.md', folderId: 'user-a-people' }, + { ...workspaceFile('b-self'), name: 'self.md', folderId: 'user-b' }, + ] + beforeEach(() => { mockListWorkspaceFileFolders.mockResolvedValue({ folders: MEMORY_FOLDERS }) - mockListWorkspaceFilesInFolderScope.mockImplementation( - async ({ input }: { input: { includeSubfolders?: boolean } }) => ({ - files: [ - { ...workspaceFile('a-self'), name: 'self.md', folderId: 'user-a' }, - ...(input.includeSubfolders === false - ? [] - : [ - { - ...workspaceFile('a-people-self'), - name: 'self.md', - folderId: 'user-a-people', - }, - ]), - ], - truncated: false, + mockQueryWorkspaceFilePage.mockImplementation( + async ({ + input, + }: { + input: { + folderScope?: { folderIds: Set; includeRootItems: boolean } + fileId?: string + name?: string + } + }) => ({ + files: MEMORY_FILES.filter( + (file) => + (!input.folderScope || + (file.folderId + ? input.folderScope.folderIds.has(file.folderId) + : input.folderScope.includeRootItems)) && + (input.fileId === undefined || file.id === input.fileId) && + (input.name === undefined || file.name === input.name) + ), + nextKeys: null, }) ) }) @@ -559,11 +570,11 @@ describe('file manage folder wiring', () => { * the only thing telling them apart. */ it('appends to the file in the chosen folder, not a same-named file elsewhere', async () => { - mockListWorkspaceFilesInFolderScope.mockResolvedValue({ + mockQueryWorkspaceFilePage.mockResolvedValue({ files: [ { ...workspaceFile('notes-in-reports'), name: 'notes.md', folderId: 'folder-reports' }, ], - truncated: false, + nextKeys: null, }) await POST( createMockRequest('POST', { @@ -582,6 +593,33 @@ describe('file manage folder wiring', () => { ) }) + it('resolves a named file in a folder too large to list in one selection', async () => { + mockListWorkspaceFilesInFolderScope.mockResolvedValue({ files: [], truncated: true }) + mockQueryWorkspaceFilePage.mockResolvedValue({ + files: [ + { ...workspaceFile('notes-in-reports'), name: 'notes.md', folderId: 'folder-reports' }, + ], + nextKeys: null, + }) + + const response = await POST( + createMockRequest('POST', { + operation: 'append', + workspaceId: 'workspace-1', + fileName: 'notes.md', + folderPath: '/Reports', + content: 'more', + }) + ) + + expect(response.status).not.toBe(413) + expect(mockResolveWorkspaceFileReference).toHaveBeenCalledWith( + 'workspace-1', + 'notes-in-reports', + { includeOwnedFiles: true } + ) + }) + /* * A folder genuinely named `Q3/Q4` is one level, but the joined reference the * overwrite lookup used re-read it as two — so the existing file was never diff --git a/apps/sim/lib/internal/file/operations.ts b/apps/sim/lib/internal/file/operations.ts index eebc28d1c07..e45dbd26d19 100644 --- a/apps/sim/lib/internal/file/operations.ts +++ b/apps/sim/lib/internal/file/operations.ts @@ -109,6 +109,7 @@ import { MAX_ZIP_DOWNLOAD_FILES, } from '@/lib/workspace-files/limits' import { MAX_WORKSPACE_FILE_CONTENT_BYTES } from '@/lib/workspace-files/orchestration' +import { resolveWorkspaceFolderScope } from '@/lib/workspace-files/resolve-folder-scope' import { parseWorkspaceFileText } from '@/lib/workspace-files/text-extraction' import { type FileTextLineRange, sliceFileTextLines } from '@/lib/workspace-files/text-lines' import { isWorkspaceAccessDeniedError } from '@/lib/workspaces/permissions/utils' @@ -918,6 +919,9 @@ async function expandFolderPathsToFileIds(args: { return (await expandFolderPathsToFiles(args)).map((file) => file.id) } +/** How many same-named candidates an ambiguous scoped reference names. */ +const MAX_LISTED_NAME_MATCHES = 10 + /** * Resolves a typed file name inside a chosen folder to a canonical id. * @@ -926,11 +930,10 @@ async function expandFolderPathsToFileIds(args: { * lookup takes the oldest match anywhere. When a folder was chosen it is the * only thing disambiguating the target, so the name is resolved inside it, by * id, so the slash-in-a-folder-name hazard of a path-shaped reference never - * arises. + * arises. Shared by every operation that writes to a named file. * - * Shared by every operation that writes to a named file, because each of them - * has the same way to go wrong and this logic has already been rewritten twice - * under review. + * The lookups filter by exact id or name in SQL, so the size of the folder + * never bounds whether one file in it can be named. */ async function resolveScopedFileReference(args: { principal: Principal @@ -940,64 +943,76 @@ async function resolveScopedFileReference(args: { folderPaths: string[] | undefined includeSubfolders: boolean | undefined }): Promise { - const { fileName } = args + const { fileName, principal, workspaceId } = args const folderPaths = args.folderPaths ?? (args.folderPath ? [args.folderPath] : []) if (folderPaths.length === 0) return fileName const scopeLabel = folderPaths.join(', ') - const scoped = await expandFolderPathsToFiles({ - principal: args.principal, - workspaceId: args.workspaceId, - folderPaths, - includeSubfolders: args.includeSubfolders, - }) - /* - * An exact id inside the scope wins outright. Matching id and name together - * let a file NAMED like an id outproduce the file that actually carries it: - * `wf_` is a legal filename prefix, so a caller passing a real id could be - * answered with a different file that merely happens to be called that. - */ - const byId = scoped.find((file) => file.id === fileName) - if (byId) return byId.id - /* - * A reference that IS a real file id, but for a file outside this folder, is - * out of scope — never a name match. Falling through would answer a caller - * who named one file exactly with a different file that happens to be called - * like that id, which is the same lookalike hazard pointed the other way. - * - * Decided by looking the id up rather than by its shape, because `wf_` is a - * legal filename prefix and inferring from it is what this resolution has - * twice been wrong about. + * A reference that is a real file id resolves to that file or to nothing, + * never to a name match: `wf_` is a legal filename prefix, so a file merely + * named like an id must not answer a caller who named a file exactly. Decided + * by looking the id up rather than by its shape. */ - let exactIdExists = false - try { - await readWorkspaceFileMetadata.execute({ - principal: args.principal, - input: { fileId: fileName, assertedWorkspaceId: args.workspaceId }, - }) - exactIdExists = true - } catch (error) { - if (!(error instanceof OrchestrationError) || error.code !== 'not_found') throw error + const fileIdExists = async () => { + try { + await readWorkspaceFileMetadata.execute({ + principal, + input: { fileId: fileName, assertedWorkspaceId: workspaceId }, + }) + return true + } catch (error) { + if (error instanceof OrchestrationError && error.code === 'not_found') return false + throw error + } } + const [folderScope, exactIdExists] = await Promise.all([ + resolveWorkspaceFolderScope({ + principal, + workspaceId, + folderPaths, + includeSubfolders: args.includeSubfolders, + }), + fileIdExists(), + ]) + const findInScope = (match: { fileId: string } | { name: string }, limit: number) => + queryWorkspaceFilePage.execute({ + principal, + input: { + workspaceId, + folderScope, + ...match, + sortBy: 'uploadedAt', + sortOrder: 'asc', + limit, + }, + }) + if (exactIdExists) { - throw new OrchestrationError('not_found', `File ${fileName} is not in ${scopeLabel}`) + const { files } = await findInScope({ fileId: fileName }, 1) + if (files.length === 0) { + throw new OrchestrationError('not_found', `File ${fileName} is not in ${scopeLabel}`) + } + return files[0].id } - const matches = scoped.filter((file) => file.name === fileName) + const { files: matches, nextKeys } = await findInScope( + { name: fileName }, + MAX_LISTED_NAME_MATCHES + ) if (matches.length === 0) { throw new OrchestrationError('not_found', `No file named ${fileName} in ${scopeLabel}`) } /* * A recursive scope can hold the same name at several depths, and writing to - * whichever the walk happened to reach first is a silent write to an - * arbitrary file. Refusing names the candidates so the caller can pick one, - * which is the whole reason the scope exists. + * whichever comes first is a silent write to an arbitrary file. Refusing + * names the candidates so the caller can pick one. */ if (matches.length > 1) { + const count = nextKeys ? `More than ${matches.length}` : `${matches.length}` throw new OrchestrationError( 'validation', - `${matches.length} files named ${fileName} under ${scopeLabel}: ${matches + `${count} files named ${fileName} under ${scopeLabel}: ${matches .map((file) => file.id) .join(', ')}. Give the file ID, name a deeper folder, or set includeSubfolders to false.` ) @@ -2186,7 +2201,7 @@ export async function executeFileManageOperation( if (selection.missingPath !== undefined) { throw new OrchestrationError('not_found', `Folder not found: ${selection.missingPath}`) } - rootId = [...selection.folderIds][0] ?? null + rootId = selection.scope?.folderIds.values().next().value ?? null } const maxDepth = body.recursive ? (body.depth ?? Number.POSITIVE_INFINITY) : 1 diff --git a/apps/sim/lib/internal/sailpoint/client.test.ts b/apps/sim/lib/internal/sailpoint/client.test.ts index 8bd44043afb..a7b3adac435 100644 --- a/apps/sim/lib/internal/sailpoint/client.test.ts +++ b/apps/sim/lib/internal/sailpoint/client.test.ts @@ -1,8 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { - clearSailPointTokenStateForTests, getSailPointAccessToken, - getSailPointTokenStateForTests, resolveSailPointHosts, sailpointFetch, } from '@/lib/internal/sailpoint/client' @@ -13,9 +11,13 @@ function tokenResponse(token: string, expiresIn = 3600): Response { return Response.json({ access_token: token, expires_in: expiresIn }) } +/** Tokens are cached per credential for the module's lifetime, so each test owns its secret. */ +function credentialsFor(clientSecret: string) { + return { tenant: 'acme', clientId: 'client', clientSecret } +} + describe('SailPoint client', () => { beforeEach(() => { - clearSailPointTokenStateForTests() mockFetch.mockReset() vi.stubGlobal('fetch', mockFetch) }) @@ -39,9 +41,9 @@ describe('SailPoint client', () => { .mockResolvedValueOnce(tokenResponse('first')) .mockResolvedValueOnce(tokenResponse('second')) - const common = { tenant: 'acme', clientId: 'client' } - expect(await getSailPointAccessToken({ ...common, clientSecret: 'one' })).toBe('first') - expect(await getSailPointAccessToken({ ...common, clientSecret: 'two' })).toBe('second') + expect(await getSailPointAccessToken(credentialsFor('isolate-one'))).toBe('first') + expect(await getSailPointAccessToken(credentialsFor('isolate-two'))).toBe('second') + expect(await getSailPointAccessToken(credentialsFor('isolate-one'))).toBe('first') expect(mockFetch).toHaveBeenCalledTimes(2) }) @@ -53,7 +55,7 @@ describe('SailPoint client', () => { release = resolve }) ) - const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } + const credentials = credentialsFor('single-flight') const first = getSailPointAccessToken(credentials) const second = getSailPointAccessToken(credentials) expect(mockFetch).toHaveBeenCalledTimes(1) @@ -69,7 +71,7 @@ describe('SailPoint client', () => { release = resolve }) ) - const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } + const credentials = credentialsFor('abort-one-waiter') const controller = new AbortController() const first = getSailPointAccessToken(credentials, controller.signal) const second = getSailPointAccessToken(credentials) @@ -79,7 +81,41 @@ describe('SailPoint client', () => { release?.(tokenResponse('shared')) await expect(second).resolves.toBe('shared') expect(mockFetch).toHaveBeenCalledTimes(1) - expect(getSailPointTokenStateForTests().exchangeSize).toBe(0) + }) + + it('caps in-flight exchanges across distinct credentials and frees slots as they settle', async () => { + const releases: Array<(response: Response) => void> = [] + mockFetch.mockImplementation( + () => + new Promise((resolve) => { + releases.push(resolve) + }) + ) + const pending = Array.from({ length: 100 }, (_, index) => + getSailPointAccessToken(credentialsFor(`in-flight-${index}`)) + ) + + await expect(getSailPointAccessToken(credentialsFor('in-flight-overflow'))).rejects.toThrow( + 'Too many concurrent SailPoint token exchanges' + ) + expect(mockFetch).toHaveBeenCalledTimes(100) + + for (const release of releases) release(tokenResponse('token')) + await Promise.all(pending) + mockFetch.mockResolvedValueOnce(tokenResponse('after-drain')) + expect(await getSailPointAccessToken(credentialsFor('in-flight-after-drain'))).toBe( + 'after-drain' + ) + }) + + it('does not cache a failed exchange', async () => { + mockFetch + .mockResolvedValueOnce(Response.json({ error: 'invalid_client' }, { status: 401 })) + .mockResolvedValueOnce(tokenResponse('recovered')) + const credentials = credentialsFor('failed-exchange') + + await expect(getSailPointAccessToken(credentials)).rejects.toThrow('invalid_client') + expect(await getSailPointAccessToken(credentials)).toBe('recovered') }) it('expires cached tokens before their provider expiry', async () => { @@ -88,33 +124,22 @@ describe('SailPoint client', () => { mockFetch .mockResolvedValueOnce(tokenResponse('old', 100)) .mockResolvedValueOnce(tokenResponse('new', 100)) - const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } + const credentials = credentialsFor('expiry') + expect(await getSailPointAccessToken(credentials)).toBe('old') + vi.setSystemTime(new Date('2026-01-01T00:01:29.000Z')) expect(await getSailPointAccessToken(credentials)).toBe('old') vi.setSystemTime(new Date('2026-01-01T00:01:31.000Z')) expect(await getSailPointAccessToken(credentials)).toBe('new') }) - it('evicts the oldest token when the bounded cache is full', async () => { - mockFetch.mockImplementation(async () => tokenResponse('token')) - for (let index = 0; index < 101; index += 1) { - await getSailPointAccessToken({ - tenant: 'acme', - clientId: `client-${index}`, - clientSecret: 'secret', - }) - } - expect(getSailPointTokenStateForTests()).toEqual({ cacheSize: 100, exchangeSize: 0 }) - }) - it('aborts during rate-limit backoff without another provider call', async () => { mockFetch .mockResolvedValueOnce(tokenResponse('token')) .mockResolvedValueOnce(new Response(null, { status: 429, headers: { 'retry-after': '30' } })) const controller = new AbortController() - const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } const pending = sailpointFetch( - credentials, + credentialsFor('rate-limit'), (hosts) => ({ url: `${hosts.apiBaseUrl}/identities/v1`, init: { method: 'GET' }, @@ -128,13 +153,30 @@ describe('SailPoint client', () => { expect(mockFetch).toHaveBeenCalledTimes(2) }) + it('refreshes the token once when the provider rejects it', async () => { + mockFetch + .mockResolvedValueOnce(tokenResponse('revoked')) + .mockResolvedValueOnce(new Response(null, { status: 401 })) + .mockResolvedValueOnce(tokenResponse('fresh')) + .mockResolvedValueOnce(Response.json({ id: 'identity' })) + + const result = await sailpointFetch(credentialsFor('refresh-on-401'), (hosts) => ({ + url: `${hosts.apiBaseUrl}/identities/v1/id`, + init: { method: 'GET' }, + })) + + expect(result.data).toEqual({ id: 'identity' }) + expect(new Headers(mockFetch.mock.calls[3][1]?.headers).get('Authorization')).toBe( + 'Bearer fresh' + ) + }) + it('rejects redirects for token and authenticated provider requests', async () => { mockFetch .mockResolvedValueOnce(tokenResponse('token')) .mockResolvedValueOnce(Response.json({ id: 'identity' })) - const credentials = { tenant: 'acme', clientId: 'client', clientSecret: 'secret' } - await sailpointFetch(credentials, (hosts) => ({ + await sailpointFetch(credentialsFor('redirects'), (hosts) => ({ url: `${hosts.apiBaseUrl}/identities/v1/id`, init: { method: 'GET' }, })) diff --git a/apps/sim/lib/internal/sailpoint/client.ts b/apps/sim/lib/internal/sailpoint/client.ts index 395492cea38..95160617558 100644 --- a/apps/sim/lib/internal/sailpoint/client.ts +++ b/apps/sim/lib/internal/sailpoint/client.ts @@ -2,6 +2,7 @@ import { createHash } from 'node:crypto' import { interruptibleSleep } from '@sim/utils/helpers' import { isRecordLike } from '@sim/utils/object' import { backoffWithJitter, parseRetryAfter } from '@sim/utils/retry' +import { LRUCache } from 'lru-cache' import { MAX_JSON_API_RESPONSE_BYTES } from '@/lib/core/security/input-validation.server' import { consumeOrCancelBody, @@ -28,19 +29,45 @@ export interface SailPointFetchResult { status: number } -interface CachedToken { - expiresAt: number - token: string -} - const MAX_FETCH_RETRIES = 4 -const MAX_TOKEN_CACHE_ENTRIES = 100 const MAX_TOKEN_EXCHANGES = 100 const MAX_TOKEN_RESPONSE_BYTES = 1024 * 1024 const TOKEN_EXCHANGE_TIMEOUT_MS = 30_000 const TOKEN_EXPIRY_BUFFER_MS = 60_000 -const tokenCache = new Map() -const tokenExchanges = new Map>() + +/** + * Access tokens keyed by tenant, client, and secret hash. `fetchMethod` + * coalesces concurrent exchanges per key and sets each entry's TTL from the + * provider's `expires_in`. The exchange owns its timeout and ignores + * cache-driven aborts, so an eviction never fails callers already waiting on + * it; a caller's own signal only detaches that caller. Because an evicted exchange keeps + * running, `max` does not bound in-flight exchanges, so `activeTokenExchanges` caps them + * separately. TTLs run on the wall clock at exact resolution, the same clock as the + * provider's `expires_in`. + */ +let activeTokenExchanges = 0 +const tokenCache = new LRUCache({ + max: 100, + ttlResolution: 0, + ignoreFetchAbort: true, + perf: { now: () => Date.now() }, + fetchMethod: async (_key, _stale, { context, options }) => { + if (activeTokenExchanges >= MAX_TOKEN_EXCHANGES) { + throw new Error('Too many concurrent SailPoint token exchanges') + } + activeTokenExchanges += 1 + try { + const { token, ttlMs } = await exchangeAccessToken( + context, + AbortSignal.timeout(TOKEN_EXCHANGE_TIMEOUT_MS) + ) + options.ttl = ttlMs + return token + } finally { + activeTokenExchanges -= 1 + } + }, +}) async function waitForPromiseWithSignal(promise: Promise, signal?: AbortSignal): Promise { if (!signal) return promise @@ -111,23 +138,6 @@ function credentialsCacheKey(credentials: SailPointCredentials): string { return `${host}:${credentials.clientId}:${secretHash}` } -function pruneTokenCache(now: number): void { - for (const [key, value] of tokenCache) { - if (value.expiresAt <= now) tokenCache.delete(key) - } - while (tokenCache.size >= MAX_TOKEN_CACHE_ENTRIES) { - const oldest = tokenCache.keys().next().value - if (typeof oldest !== 'string') break - tokenCache.delete(oldest) - } -} - -function cacheToken(key: string, token: CachedToken): void { - pruneTokenCache(Date.now()) - tokenCache.delete(key) - tokenCache.set(key, token) -} - async function readBoundedBody( response: Response, maxBytes: number, @@ -149,8 +159,8 @@ async function readBoundedBody( async function exchangeAccessToken( credentials: SailPointCredentials, - signal?: AbortSignal -): Promise { + signal: AbortSignal +): Promise<{ token: string; ttlMs: number }> { const { tokenUrl } = resolveSailPointHosts(credentials.tenant) let attempt = 0 @@ -196,12 +206,11 @@ async function exchangeAccessToken( const parsedExpiry = Number(data.expires_in) const expiresInSeconds = Number.isFinite(parsedExpiry) && parsedExpiry > 0 ? parsedExpiry : 3600 const bufferMs = Math.min(TOKEN_EXPIRY_BUFFER_MS, expiresInSeconds * 100) - const key = credentialsCacheKey(credentials) - cacheToken(key, { + // lru-cache reads a TTL of 0 as "never expires", so keep at least 1ms. + return { token: data.access_token, - expiresAt: Date.now() + Math.max(expiresInSeconds * 1000 - bufferMs, 0), - }) - return data.access_token + ttlMs: Math.max(Math.floor(expiresInSeconds * 1000 - bufferMs), 1), + } } } @@ -214,30 +223,13 @@ export async function getSailPointAccessToken( signal?: AbortSignal ): Promise { signal?.throwIfAborted() - const key = credentialsCacheKey(credentials) - const now = Date.now() - const cached = tokenCache.get(key) - if (cached && cached.expiresAt > now) { - tokenCache.delete(key) - tokenCache.set(key, cached) - return cached.token - } - if (cached) tokenCache.delete(key) - - const existing = tokenExchanges.get(key) - if (existing) return waitForPromiseWithSignal(existing, signal) - if (tokenExchanges.size >= MAX_TOKEN_EXCHANGES) { - throw new Error('Too many concurrent SailPoint token exchanges') - } - - const exchange = exchangeAccessToken( - credentials, - AbortSignal.timeout(TOKEN_EXCHANGE_TIMEOUT_MS) - ).finally(() => { - tokenExchanges.delete(key) - }) - tokenExchanges.set(key, exchange) - return waitForPromiseWithSignal(exchange, signal) + const token = await waitForPromiseWithSignal( + tokenCache.fetch(credentialsCacheKey(credentials), { context: credentials }), + signal + ) + if (token === undefined) + throw new Error('SailPoint authentication did not return an access token') + return token } export async function sailpointFetch( @@ -301,14 +293,3 @@ export function readTotalCount(headers: Headers): number | null { const parsed = Number(raw) return Number.isInteger(parsed) && parsed >= 0 ? parsed : null } - -/** Clears process-local authentication state for deterministic tests. */ -export function clearSailPointTokenStateForTests(): void { - tokenCache.clear() - tokenExchanges.clear() -} - -/** Returns cache sizes for deterministic boundary tests. */ -export function getSailPointTokenStateForTests(): { cacheSize: number; exchangeSize: number } { - return { cacheSize: tokenCache.size, exchangeSize: tokenExchanges.size } -} diff --git a/apps/sim/lib/internal/sailpoint/execute-tool.test.ts b/apps/sim/lib/internal/sailpoint/execute-tool.test.ts index 2aa3a2093e1..0f8e31c2568 100644 --- a/apps/sim/lib/internal/sailpoint/execute-tool.test.ts +++ b/apps/sim/lib/internal/sailpoint/execute-tool.test.ts @@ -14,7 +14,6 @@ vi.mock('@/app/api/files/authorization', () => filesAuthorizationMock) vi.mock('@/lib/uploads/utils/file-utils', () => fileUtilsMock) vi.mock('@/lib/uploads/utils/file-utils.server', () => fileUtilsServerMock) -import { clearSailPointTokenStateForTests } from '@/lib/internal/sailpoint/client' import { executeSailPointTool } from '@/lib/internal/sailpoint/execute-tool' import { MAX_SAILPOINT_CSV_BYTES } from '@/lib/internal/sailpoint/operations' @@ -41,7 +40,6 @@ function request(operation: string, input: Record, userId?: str describe('SailPoint internal tool handler', () => { beforeEach(() => { - clearSailPointTokenStateForTests() mockFetch.mockReset() vi.stubGlobal('fetch', mockFetch) mockAssertToolFileAccess.mockReset().mockResolvedValue(null) @@ -114,7 +112,6 @@ describe('SailPoint internal tool handler', () => { const providerResponse = await request('sailpoint_get_identity', { id: 'identity' }) expect(providerResponse.status).toBe(200) - clearSailPointTokenStateForTests() mockFetch.mockReset() const fileResponse = await request('sailpoint_load_accounts', { sourceId: 'source', diff --git a/apps/sim/lib/internal/sailpoint/operations.ts b/apps/sim/lib/internal/sailpoint/operations.ts index 37b79d0dd53..b52e567338b 100644 --- a/apps/sim/lib/internal/sailpoint/operations.ts +++ b/apps/sim/lib/internal/sailpoint/operations.ts @@ -16,6 +16,7 @@ import { processFilesToUserFiles } from '@/lib/uploads/utils/file-utils' import { downloadServableFileFromStorage } from '@/lib/uploads/utils/file-utils.server' import { docNotReadyResponse } from '@/lib/uploads/utils/servable-file-response' import { assertToolFileAccess } from '@/app/api/files/authorization' +import { safeUrlPathSegment } from '@/tools/url-path' const logger = createLogger('SailPointOperations') @@ -77,8 +78,8 @@ function queryString(params: Record): string { return serialized ? `?${serialized}` : '' } -function encodeId(value: unknown): string { - return encodeURIComponent(String(value)) +function encodeId(value: unknown, paramName: string): string { + return safeUrlPathSegment(String(value), paramName) } function toStringList(value: unknown): string[] | undefined { @@ -301,8 +302,8 @@ async function executeLoad( const isAccountLoad = input.operation === 'sailpoint_load_accounts' const path = isAccountLoad - ? `/sources/v1/${encodeId(input.sourceId)}/load-accounts` - : `/sources/v1/${encodeId(input.sourceId)}/load-entitlements` + ? `/sources/v1/${encodeId(input.sourceId, 'sourceId')}/load-accounts` + : `/sources/v1/${encodeId(input.sourceId, 'sourceId')}/load-entitlements` const result = await sailpointFetch( credentials, @@ -393,7 +394,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/identities/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/identities/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'identity' @@ -403,7 +404,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/entitlements/v1/identities/${encodeId(input.id)}/entitlements${queryString({ limit: input.limit, offset: input.offset, count: input.count })}`, + url: `${hosts.apiBaseUrl}/entitlements/v1/identities/${encodeId(input.id, 'id')}/entitlements${queryString({ limit: input.limit, offset: input.offset, count: input.count })}`, init: { method: 'GET' }, }), 'list' @@ -423,7 +424,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/accounts/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/accounts/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'account' @@ -433,7 +434,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/accounts/v1/${encodeId(input.id)}/entitlements${queryString({ limit: input.limit, offset: input.offset, count: input.count })}`, + url: `${hosts.apiBaseUrl}/accounts/v1/${encodeId(input.id, 'id')}/entitlements${queryString({ limit: input.limit, offset: input.offset, count: input.count })}`, init: { method: 'GET' }, }), 'list' @@ -453,7 +454,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/entitlements/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/entitlements/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'entitlement' @@ -463,7 +464,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/entitlements/v1/${encodeId(input.id)}/entitlement-request-config`, + url: `${hosts.apiBaseUrl}/entitlements/v1/${encodeId(input.id, 'id')}/entitlement-request-config`, init: { method: 'GET' }, }), 'entitlementRequestConfig' @@ -483,7 +484,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/roles/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/roles/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'role' @@ -493,7 +494,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/roles/v1/${encodeId(input.id)}/entitlements${queryString({ filters: input.filters, sorters: input.sorters, limit: input.limit, offset: input.offset, count: input.count })}`, + url: `${hosts.apiBaseUrl}/roles/v1/${encodeId(input.id, 'id')}/entitlements${queryString({ filters: input.filters, sorters: input.sorters, limit: input.limit, offset: input.offset, count: input.count })}`, init: { method: 'GET' }, }), 'list' @@ -513,7 +514,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/access-profiles/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/access-profiles/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'accessProfile' @@ -523,7 +524,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/access-profiles/v1/${encodeId(input.id)}/entitlements${queryString({ filters: input.filters, sorters: input.sorters, limit: input.limit, offset: input.offset, count: input.count })}`, + url: `${hosts.apiBaseUrl}/access-profiles/v1/${encodeId(input.id, 'id')}/entitlements${queryString({ filters: input.filters, sorters: input.sorters, limit: input.limit, offset: input.offset, count: input.count })}`, init: { method: 'GET' }, }), 'list' @@ -543,7 +544,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/sources/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/sources/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'source' @@ -563,7 +564,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/account-activities/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/account-activities/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'accountActivity' @@ -583,7 +584,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/campaigns/v1/${encodeId(input.id)}${queryString({ detail: input.detail })}`, + url: `${hosts.apiBaseUrl}/campaigns/v1/${encodeId(input.id, 'id')}${queryString({ detail: input.detail })}`, init: { method: 'GET' }, }), 'campaign' @@ -603,7 +604,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'certification' @@ -613,7 +614,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id)}/access-review-items${queryString({ filters: input.filters, sorters: input.sorters, entitlements: input.entitlements, 'access-profiles': input.accessProfiles, roles: input.roles, limit: input.limit, offset: input.offset, count: input.count })}`, + url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id, 'id')}/access-review-items${queryString({ filters: input.filters, sorters: input.sorters, entitlements: input.entitlements, 'access-profiles': input.accessProfiles, roles: input.roles, limit: input.limit, offset: input.offset, count: input.count })}`, init: { method: 'GET' }, }), 'list' @@ -623,7 +624,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id)}/decide`, + url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id, 'id')}/decide`, init: jsonRequest(input.decisions), }), 'certification' @@ -633,7 +634,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id)}/sign-off`, + url: `${hosts.apiBaseUrl}/certifications/v1/${encodeId(input.id, 'id')}/sign-off`, init: { method: 'POST' }, }), 'certification' @@ -717,7 +718,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/access-request-approvals/v1/${encodeId(input.approvalId)}/${action}`, + url: `${hosts.apiBaseUrl}/access-request-approvals/v1/${encodeId(input.approvalId, 'approvalId')}/${action}`, init, }), 'write' @@ -728,7 +729,7 @@ export async function executeSailPointOperation( credentials, context, (hosts) => ({ - url: `${hosts.apiBaseUrl}/task-status/v1/${encodeId(input.id)}`, + url: `${hosts.apiBaseUrl}/task-status/v1/${encodeId(input.id, 'id')}`, init: { method: 'GET' }, }), 'task' diff --git a/apps/sim/lib/internal/sailpoint/schema.ts b/apps/sim/lib/internal/sailpoint/schema.ts index f7462bb2a9f..abde70b0b8b 100644 --- a/apps/sim/lib/internal/sailpoint/schema.ts +++ b/apps/sim/lib/internal/sailpoint/schema.ts @@ -6,9 +6,9 @@ const MAX_ID_LENGTH = 1024 const MAX_FILTER_LENGTH = 20_000 const MAX_QUERY_LENGTH = 100_000 const MAX_METADATA_ENTRIES = 100 -const STANDARD_LIMIT_MAX = 250 -const SEARCH_LIMIT_MAX = 10_000 -const ROLE_LIMIT_MAX = 50 +export const STANDARD_LIMIT_MAX = 250 +export const SEARCH_LIMIT_MAX = 10_000 +export const ROLE_LIMIT_MAX = 50 function parseJson(value: unknown): unknown { if (typeof value !== 'string') return value diff --git a/apps/sim/lib/knowledge/connectors/sync-content-pass.test.ts b/apps/sim/lib/knowledge/connectors/sync-content-pass.test.ts index 2c9e0bd9185..fd7a162093f 100644 --- a/apps/sim/lib/knowledge/connectors/sync-content-pass.test.ts +++ b/apps/sim/lib/knowledge/connectors/sync-content-pass.test.ts @@ -31,7 +31,10 @@ import { windowScan, windowScans, } from '@/lib/knowledge/connectors/reconciliation-window.test-helpers' -import { runConnectorContentPass } from '@/lib/knowledge/connectors/sync-content-pass' +import { + removeDisabledImages, + runConnectorContentPass, +} from '@/lib/knowledge/connectors/sync-content-pass' import { SOURCE_CONTENT_ERROR, SOURCE_PERMISSION_ERROR, @@ -263,6 +266,7 @@ describe('completed listing removal counts', () => { docsUnchanged: 0, docsSkipped: 0, docsFailed: 0, + processingDispatch: { requested: 0, accepted: 0, failed: 0 }, } const pass = await runConnectorContentPass({ connectorId: 'connector', @@ -454,6 +458,42 @@ function contentWrite(): Record { return call![0] } +describe('disabled image removal', () => { + it('keeps walking past a page that removed nothing', async () => { + queueTableRows(schemaMock.document, [{ id: 'detached' }]) + queueTableRows(schemaMock.document, [{ id: 'image' }]) + queueTableRows(schemaMock.document, []) + mocks.hardDelete.mockResolvedValueOnce(0).mockResolvedValueOnce(1) + const result: SyncResult = { + docsAdded: 0, + docsUpdated: 0, + docsDeleted: 0, + docsUnchanged: 0, + docsSkipped: 0, + docsFailed: 0, + processingDispatch: { requested: 0, accepted: 0, failed: 0 }, + } + + const finished = await removeDisabledImages({ + connectorId: 'connector', + connector: { knowledgeBaseId: 'kb', connectorType: 'confluence', imageRemovalPending: true }, + lease: { + stillHeld: () => stillHoldsSyncLock('connector', 'run'), + beatIfDue: async () => undefined, + beatLive: async () => undefined, + }, + leaseKind: 'content', + runId: 'run', + deadlineAt: Date.now() + 60_000, + result, + }) + + expect(finished).toBe(true) + expect(mocks.hardDelete.mock.calls.map((call) => call[0])).toEqual([['detached'], ['image']]) + expect(result.docsDeleted).toBe(1) + }) +}) + describe('content pass checkpoint intent', () => { it('does not reconcile deletions after a user listing failed, even without the unsafe marker', async () => { sourceBody = { value: '

Current content

' } diff --git a/apps/sim/lib/knowledge/connectors/sync-content-pass.ts b/apps/sim/lib/knowledge/connectors/sync-content-pass.ts index 78f7f6694da..4e615047aa4 100644 --- a/apps/sim/lib/knowledge/connectors/sync-content-pass.ts +++ b/apps/sim/lib/knowledge/connectors/sync-content-pass.ts @@ -445,8 +445,6 @@ export async function removeDisabledImages( lease: input.leaseKind, } ) - /** Nothing removable is left, e.g. a row detached from this connector mid-walk. */ - if (removed === 0) break input.result.docsDeleted += removed } await db diff --git a/apps/sim/lib/logs/fetch-log-detail.test.ts b/apps/sim/lib/logs/fetch-log-detail.test.ts index b83e057960f..05f3c4b71ed 100644 --- a/apps/sim/lib/logs/fetch-log-detail.test.ts +++ b/apps/sim/lib/logs/fetch-log-detail.test.ts @@ -48,6 +48,7 @@ function queueWorkflowLogRow(overrides: Record = {}): void { workflowWorkspaceId: 'workspace-1', workflowCreatedAt: new Date('2026-01-01T00:00:00.000Z'), workflowUpdatedAt: new Date('2026-01-01T00:00:00.000Z'), + workflowArchivedAt: null, pausedStatus: null, pausedTotalPauseCount: 0, pausedResumedCount: 0, @@ -158,6 +159,7 @@ describe('readLogDetail', () => { workflowWorkspaceId: 'workspace-1', workflowCreatedAt: new Date('2026-01-01T00:00:00.000Z'), workflowUpdatedAt: new Date('2026-01-01T00:00:00.000Z'), + workflowArchivedAt: null, pausedStatus: null, pausedTotalPauseCount: 0, pausedResumedCount: 0, diff --git a/apps/sim/lib/logs/fetch-log-detail.ts b/apps/sim/lib/logs/fetch-log-detail.ts index d54b6182b13..af38acf7914 100644 --- a/apps/sim/lib/logs/fetch-log-detail.ts +++ b/apps/sim/lib/logs/fetch-log-detail.ts @@ -194,6 +194,7 @@ export async function readLogDetail({ workflowWorkspaceId: workflow.workspaceId, workflowCreatedAt: workflow.createdAt, workflowUpdatedAt: workflow.updatedAt, + workflowArchivedAt: workflow.archivedAt, deploymentVersion: workflowDeploymentVersion.version, deploymentVersionName: workflowDeploymentVersion.name, pausedStatus: pausedExecutions.status, @@ -224,6 +225,7 @@ export async function readLogDetail({ workspaceId: log.workflowWorkspaceId, createdAt: log.workflowCreatedAt?.toISOString() ?? null, updatedAt: log.workflowUpdatedAt?.toISOString() ?? null, + archivedAt: log.workflowArchivedAt?.toISOString() ?? null, } : null diff --git a/apps/sim/lib/logs/list-logs.test.ts b/apps/sim/lib/logs/list-logs.test.ts index e4f2b447f17..3ffff3ea40f 100644 --- a/apps/sim/lib/logs/list-logs.test.ts +++ b/apps/sim/lib/logs/list-logs.test.ts @@ -32,6 +32,7 @@ function workflowRow(overrides: Record = {}) { workflowWorkspaceId: 'ws-1', workflowCreatedAt: new Date('2026-01-01T00:00:00.000Z'), workflowUpdatedAt: new Date('2026-01-01T00:00:00.000Z'), + workflowArchivedAt: null, pausedStatus: null, pausedTotalPauseCount: 0, pausedResumedCount: 0, diff --git a/apps/sim/lib/logs/list-logs.ts b/apps/sim/lib/logs/list-logs.ts index c3919ad7e9b..58e3fd50319 100644 --- a/apps/sim/lib/logs/list-logs.ts +++ b/apps/sim/lib/logs/list-logs.ts @@ -227,6 +227,7 @@ async function readLogsWithDatabase( workflowWorkspaceId: workflow.workspaceId, workflowCreatedAt: workflow.createdAt, workflowUpdatedAt: workflow.updatedAt, + workflowArchivedAt: workflow.archivedAt, pausedStatus: pausedExecutions.status, pausedTotalPauseCount: pausedExecutions.totalPauseCount, pausedResumedCount: pausedExecutions.resumedCount, @@ -394,6 +395,7 @@ async function readLogsWithDatabase( workspaceId: log.workflowWorkspaceId, createdAt: log.workflowCreatedAt?.toISOString() ?? null, updatedAt: log.workflowUpdatedAt?.toISOString() ?? null, + archivedAt: log.workflowArchivedAt?.toISOString() ?? null, } : null, jobTitle: null, diff --git a/apps/sim/lib/mothership/chat/process-contents.ts b/apps/sim/lib/mothership/chat/process-contents.ts index 047d571b2b6..6e4f456b83a 100644 --- a/apps/sim/lib/mothership/chat/process-contents.ts +++ b/apps/sim/lib/mothership/chat/process-contents.ts @@ -429,7 +429,7 @@ export async function processContextsServer( ? { ...resolved, content: `Workspace ${workspaceId}:\n${resolved.content}` } : resolved } catch (error) { - logger.error('Failed processing context (server)', { ctx, error }) + logger.error('Failed processing context (server)', { kind: ctx.kind, error }) return null } } diff --git a/apps/sim/lib/mothership/tools/server/files/anchored-edit.ts b/apps/sim/lib/mothership/tools/server/files/anchored-edit.ts new file mode 100644 index 00000000000..80cf3425c1c --- /dev/null +++ b/apps/sim/lib/mothership/tools/server/files/anchored-edit.ts @@ -0,0 +1,42 @@ +import { toNumberOrNull, toStringOrNull } from '@sim/utils/coerce' +import type { WorkspaceFileContentEdit } from '@/lib/workspace-files/edit-content' + +type AnchoredContentEdit = Exclude + +/** + * Maps a staged `anchored` patch onto the shared workspace-file edit, so the + * durable write and its streamed preview apply the same anchor semantics. + */ +export function toAnchoredContentEdit( + edit: Record, + content: string +): { edit: AnchoredContentEdit } | { error: string } { + const read = (key: string) => toStringOrNull(edit[key]) ?? '' + const occurrence = toNumberOrNull(edit.occurrence) ?? undefined + + switch (edit.mode) { + case 'replace_between': { + const beforeAnchor = read('before_anchor') + const afterAnchor = read('after_anchor') + if (!beforeAnchor || !afterAnchor) { + return { error: 'replace_between requires before_anchor and after_anchor' } + } + return { edit: { mode: 'replace_between', beforeAnchor, afterAnchor, content, occurrence } } + } + case 'insert_after': { + const anchor = read('anchor') + if (!anchor) return { error: 'insert_after requires anchor' } + return { edit: { mode: 'insert_after', anchor, content, occurrence } } + } + case 'delete_between': { + const startAnchor = read('start_anchor') + const endAnchor = read('end_anchor') + if (!startAnchor || !endAnchor) { + return { error: 'delete_between requires start_anchor and end_anchor' } + } + return { edit: { mode: 'delete_between', startAnchor, endAnchor, occurrence } } + } + default: + return { error: `Unknown anchored patch mode: "${String(edit.mode)}"` } + } +} diff --git a/apps/sim/lib/mothership/tools/server/files/edit-content.ts b/apps/sim/lib/mothership/tools/server/files/edit-content.ts index 3eb0fbb7fd1..8dcf62ae32a 100644 --- a/apps/sim/lib/mothership/tools/server/files/edit-content.ts +++ b/apps/sim/lib/mothership/tools/server/files/edit-content.ts @@ -12,8 +12,10 @@ import { type BaseServerTool, type ServerToolContext, } from '@/lib/mothership/tools/server/base-tool' +import { toAnchoredContentEdit } from '@/lib/mothership/tools/server/files/anchored-edit' import { getE2BDocFormat } from '@/lib/uploads/documents/compile' import { updateWorkspaceFileContent } from '@/lib/workspace-files/application/update-workspace-file-content' +import { applyWorkspaceFileContentEdit, EditContentError } from '@/lib/workspace-files/edit-content' import { collectSimPageDiagnostics, HAND_WRITTEN_PAGE_MESSAGE, @@ -164,96 +166,15 @@ export const editContentServerTool: BaseServerTool { - const trimmed = anchor.trim() - let count = 0 - for (let i = afterIndex + 1; i < lines.length; i++) { - if (lines[i].trim() === trimmed) { - count++ - if (count === occurrence) return { index: i } - } - } - if (count === 0) { - return { - index: -1, - error: `Anchor line not found in "${fileRecord.name}": "${anchor.slice(0, 100)}"`, - } - } - return { - index: -1, - error: `Anchor line occurrence ${occurrence} not found (only ${count} match${count > 1 ? 'es' : ''}) in "${fileRecord.name}": "${anchor.slice(0, 100)}"`, - } - } - - if (intent.edit.mode === 'replace_between') { - if (!intent.edit.before_anchor || !intent.edit.after_anchor) { - return { - success: false, - message: 'replace_between requires before_anchor and after_anchor', - } - } - const before = findAnchorLine(intent.edit.before_anchor) - if (before.error) return { success: false, message: `Patch failed: ${before.error}` } - const after = findAnchorLine( - intent.edit.after_anchor, - defaultOccurrence, - before.index - ) - if (after.error) return { success: false, message: `Patch failed: ${after.error}` } - if (after.index <= before.index) { - return { - success: false, - message: 'Patch failed: after_anchor must appear after before_anchor in the file', - } - } - const newLines = [ - ...lines.slice(0, before.index + 1), - ...content.split('\n'), - ...lines.slice(after.index), - ] - finalContent = newLines.join('\n') - } else if (intent.edit.mode === 'insert_after') { - if (!intent.edit.anchor) { - return { success: false, message: 'insert_after requires anchor' } - } - const found = findAnchorLine(intent.edit.anchor) - if (found.error) return { success: false, message: `Patch failed: ${found.error}` } - const newLines = [ - ...lines.slice(0, found.index + 1), - ...content.split('\n'), - ...lines.slice(found.index + 1), - ] - finalContent = newLines.join('\n') - } else if (intent.edit.mode === 'delete_between') { - if (!intent.edit.start_anchor || !intent.edit.end_anchor) { - return { - success: false, - message: 'delete_between requires start_anchor and end_anchor', - } - } - const start = findAnchorLine(intent.edit.start_anchor) - if (start.error) return { success: false, message: `Patch failed: ${start.error}` } - const end = findAnchorLine(intent.edit.end_anchor, defaultOccurrence, start.index) - if (end.error) return { success: false, message: `Patch failed: ${end.error}` } - if (end.index <= start.index) { - return { - success: false, - message: 'Patch failed: end_anchor must appear after start_anchor in the file', - } - } - const newLines = [...lines.slice(0, start.index), ...lines.slice(end.index)] - finalContent = newLines.join('\n') - } else { + const anchored = toAnchoredContentEdit(intent.edit, content) + if ('error' in anchored) return { success: false, message: anchored.error } + try { + finalContent = applyWorkspaceFileContentEdit(existing, anchored.edit) + } catch (error) { + if (!(error instanceof EditContentError)) throw error return { success: false, - message: `Unknown anchored patch mode: "${intent.edit.mode}"`, + message: `Patch failed in "${fileRecord.name}": ${error.message}`, } } } else { diff --git a/apps/sim/lib/mothership/tools/server/files/file-preview.ts b/apps/sim/lib/mothership/tools/server/files/file-preview.ts index d97093614e8..44413948724 100644 --- a/apps/sim/lib/mothership/tools/server/files/file-preview.ts +++ b/apps/sim/lib/mothership/tools/server/files/file-preview.ts @@ -3,7 +3,9 @@ import { toError } from '@sim/utils/errors' import { observeContentOutsideModel } from '@/lib/core/application' import { executeCopilotFileUseCase } from '@/lib/mothership/application/execute-file-use-case' import type { ExecutionContext } from '@/lib/mothership/request/types' +import { toAnchoredContentEdit } from '@/lib/mothership/tools/server/files/anchored-edit' import { readWorkspaceFileContent } from '@/lib/workspace-files/application/read-workspace-file-content' +import { applyWorkspaceFileContentEdit, EditContentError } from '@/lib/workspace-files/edit-content' const logger = createLogger('CopilotFilePreview') const MAX_PREVIEW_SOURCE_BYTES = 5 * 1024 * 1024 @@ -15,27 +17,12 @@ interface BuildFilePreviewTextOptions { edit?: Record } -function findAnchorIndex(lines: string[], anchor: string, occurrence = 1, afterIndex = -1): number { - const trimmed = anchor.trim() - let count = 0 - for (let i = afterIndex + 1; i < lines.length; i++) { - if (lines[i].trim() === trimmed) { - count++ - if (count === occurrence) return i - } - } - return -1 -} - function extractPatchPreview( streamedContent: string, existingContent: string, edit?: Record ): string | undefined { const strategy = typeof edit?.strategy === 'string' ? edit.strategy : undefined - const lines = existingContent.split('\n') - const occurrence = - typeof edit?.occurrence === 'number' && Number.isFinite(edit.occurrence) ? edit.occurrence : 1 if (strategy === 'search_replace') { const search = typeof edit?.search === 'string' ? edit.search : '' @@ -52,55 +39,15 @@ function extractPatchPreview( ) } - const mode = typeof edit?.mode === 'string' ? edit.mode : undefined - if (!mode) return undefined - - if (mode === 'replace_between') { - const beforeAnchor = typeof edit?.before_anchor === 'string' ? edit.before_anchor : undefined - const afterAnchor = typeof edit?.after_anchor === 'string' ? edit.after_anchor : undefined - if (!beforeAnchor || !afterAnchor) return undefined - - const beforeIdx = findAnchorIndex(lines, beforeAnchor, occurrence) - const afterIdx = findAnchorIndex(lines, afterAnchor, occurrence, beforeIdx) - if (beforeIdx === -1 || afterIdx === -1 || afterIdx <= beforeIdx) return undefined - - const spliced = [ - ...lines.slice(0, beforeIdx + 1), - ...(streamedContent.length > 0 ? streamedContent.split('\n') : []), - ...lines.slice(afterIdx), - ] - return spliced.join('\n') - } - - if (mode === 'insert_after') { - const anchor = typeof edit?.anchor === 'string' ? edit.anchor : undefined - if (!anchor) return undefined - - const anchorIdx = findAnchorIndex(lines, anchor, occurrence) - if (anchorIdx === -1) return undefined - - const spliced = [ - ...lines.slice(0, anchorIdx + 1), - ...(streamedContent.length > 0 ? streamedContent.split('\n') : []), - ...lines.slice(anchorIdx + 1), - ] - return spliced.join('\n') - } - - if (mode === 'delete_between') { - const startAnchor = typeof edit?.start_anchor === 'string' ? edit.start_anchor : undefined - const endAnchor = typeof edit?.end_anchor === 'string' ? edit.end_anchor : undefined - if (!startAnchor || !endAnchor) return undefined - - const startIdx = findAnchorIndex(lines, startAnchor, occurrence) - const endIdx = findAnchorIndex(lines, endAnchor, occurrence, startIdx) - if (startIdx === -1 || endIdx === -1 || endIdx <= startIdx) return undefined - - const spliced = [...lines.slice(0, startIdx), ...lines.slice(endIdx)] - return spliced.join('\n') + if (strategy !== 'anchored' || !edit) return undefined + const anchored = toAnchoredContentEdit(edit, streamedContent) + if ('error' in anchored) return undefined + try { + return applyWorkspaceFileContentEdit(existingContent, anchored.edit) + } catch (error) { + if (error instanceof EditContentError) return undefined + throw error } - - return undefined } function shouldApplyPatchPreview(streamedContent: string, edit?: Record): boolean { diff --git a/apps/sim/lib/projects/__integration__/foundation.integration.ts b/apps/sim/lib/projects/__integration__/foundation.integration.ts index 193dcccaa04..e69f1107753 100644 --- a/apps/sim/lib/projects/__integration__/foundation.integration.ts +++ b/apps/sim/lib/projects/__integration__/foundation.integration.ts @@ -47,6 +47,7 @@ import { createProjectRecord, lockProject, lockWorkspaceProject, + requireForkProject, splitForkProject, transferWorkspaceProjects, } from '@/lib/projects/membership' @@ -1279,32 +1280,39 @@ describe('Project foundation at the database and application boundary', () => { } ) - check( - 'file writers in sibling environments share the Project lock that still fences a lifecycle holder', - async () => { - const f = await fixture(false) - const held = createDeferred() - const release = createDeferred() - const writer = db.transaction(async (tx) => { - await lockWorkspaceProject(tx, f.ids[0]) - const [connection] = await tx.execute<{ pid: number }>(sql`SELECT pg_backend_pid() AS pid`) - held.resolve(connection.pid) - await release.promise - }) - const blocker = await held.promise - let lifecycle: Promise | undefined - try { - const sibling = await db.transaction((tx) => lockWorkspaceProject(tx, f.ids[1])) - expect(sibling?.id).toBe(f.projectId) - lifecycle = db.transaction((tx) => lockProject(tx, f.projectId)) - await waitUntilBlockedBy(blocker, "lock='project'") - } finally { - release.resolve() - await writer + for (const [holder, holdProject] of [ + ['file writers', lockWorkspaceProject], + ['fork creation', requireForkProject], + ] as const) { + check( + `${holder} share the Project lock with sibling environments and still fence a lifecycle holder`, + async () => { + const f = await fixture(false) + const held = createDeferred() + const release = createDeferred() + const writer = db.transaction(async (tx) => { + await holdProject(tx, f.ids[0]) + const [connection] = await tx.execute<{ pid: number }>( + sql`SELECT pg_backend_pid() AS pid` + ) + held.resolve(connection.pid) + await release.promise + }) + const blocker = await held.promise + let lifecycle: Promise | undefined + try { + const sibling = await db.transaction((tx) => lockWorkspaceProject(tx, f.ids[1])) + expect(sibling?.id).toBe(f.projectId) + lifecycle = db.transaction((tx) => lockProject(tx, f.projectId)) + await waitUntilBlockedBy(blocker, "lock='project'") + } finally { + release.resolve() + await writer + } + await lifecycle } - await lifecycle - } - ) + ) + } check( 'account teardown transfers surviving Projects and atomically removes wholly private Projects', diff --git a/apps/sim/lib/projects/account-deletion.ts b/apps/sim/lib/projects/account-deletion.ts index a8669845704..162240aaac6 100644 --- a/apps/sim/lib/projects/account-deletion.ts +++ b/apps/sim/lib/projects/account-deletion.ts @@ -27,7 +27,7 @@ async function loadRelatedProjects(executor: DbOrTx, userId: string, doomedWorks .from(workspace) .where(inArray(workspace.id, doomedWorkspaceIds)) : [] - const projectIds = doomedMemberships.flatMap((row) => (row.projectId ? [row.projectId] : [])) + const projectIds = doomedMemberships.map((row) => row.projectId) return executor .select() .from(project) @@ -65,7 +65,6 @@ async function loadProjectEnvironments(executor: DbOrTx, projectIds: string[]) { : [] const byProject = new Map() for (const { projectId, ...environment } of rows) { - if (!projectId) continue const environments = byProject.get(projectId) if (environments) environments.push(environment) else byProject.set(projectId, [environment]) diff --git a/apps/sim/lib/projects/application/use-cases.ts b/apps/sim/lib/projects/application/use-cases.ts index 70af2249d1b..2bddc64ba58 100644 --- a/apps/sim/lib/projects/application/use-cases.ts +++ b/apps/sim/lib/projects/application/use-cases.ts @@ -221,7 +221,7 @@ export const getWorkspaceProject: OperationUseCase< .from(workspace) .where(eq(workspace.id, input.workspaceId)) .limit(1) - if (!membership?.projectId) throw new OrchestrationError('not_found', 'Project not found') + if (!membership) throw new OrchestrationError('not_found', 'Project not found') return { project: presentProject( await authorizeProject( diff --git a/apps/sim/lib/projects/files/__integration__/retention.integration.ts b/apps/sim/lib/projects/files/__integration__/retention.integration.ts index 14d913fe360..3090c5c55aa 100644 --- a/apps/sim/lib/projects/files/__integration__/retention.integration.ts +++ b/apps/sim/lib/projects/files/__integration__/retention.integration.ts @@ -23,6 +23,7 @@ import { featureFlagsMock, featureFlagsMockFns } from '@sim/testing/mocks/featur import { getErrorMessage } from '@sim/utils/errors' import { sleep } from '@sim/utils/helpers' import { generateId } from '@sim/utils/id' +import { compareStrings } from '@sim/utils/string' import { and, eq, inArray, sql } from 'drizzle-orm' import postgres from 'postgres' import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' @@ -35,13 +36,13 @@ import { type CleanupJobPayload, runCleanupWithLimits } from '@/lib/billing/clea import { changeWorkspaceStoragePayerInTx } from '@/lib/billing/storage/payer-transfer' import { createCleanupBudgets } from '@/lib/cleanup/limits' import { beginFileArchiveCleanup, cleanupFileVersions } from '@/lib/file-retention' +import { FILES_PER_QUERY } from '@/lib/file-retention/versions' import { authorizeProject } from '@/lib/projects/application/authorization' import { projectOperations } from '@/lib/projects/application/operations' import { lockWorkspaceProject } from '@/lib/projects/membership' import { processWorkspaceFileStorageCleanupsNow } from '@/lib/uploads/contexts/workspace/workspace-file-storage-cleanup-outbox' import { deleteWorkspaceFileVersionInTx } from '@/lib/uploads/contexts/workspace/workspace-file-versions' import { deleteFile, downloadFile, uploadFile } from '@/lib/uploads/core/storage-service' -import type { FileOwner } from '@/lib/workspace-files/ownership' import { runCleanupFileVersions } from '@/background/cleanup-file-versions' import { runCleanupSoftDeletes } from '@/background/cleanup-soft-deletes' @@ -289,38 +290,6 @@ describe('Project file retention follows the current payer in PostgreSQL', () => } ) - check( - 'unsupported file owners reject the whole batch before any history or archive deletion', - async () => { - const f = await fixture() - const expired = await seedFile(f, 40 * 24, true) - const owners: FileOwner[] = [ - { entityType: 'project', entityId: f.projectId }, - { entityType: 'user', entityId: f.ownerId }, - ] - const options = { plan: 'free' as const, cutoff: new Date(), label: 'invalid-owner-batch' } - await expect(cleanupFileVersions(owners, options, 10)).rejects.toThrow( - 'File owner is unavailable' - ) - await expect(beginFileArchiveCleanup(owners, options)).rejects.toThrow( - 'File owner is unavailable' - ) - expect(await versions(expired.fileId)).toHaveLength(12) - expect( - await db - .select({ id: workspaceFiles.id }) - .from(workspaceFiles) - .where(eq(workspaceFiles.id, expired.fileId)) - ).toEqual([{ id: expired.fileId }]) - expect(await cleanupEvents(f.projectId)).toEqual([]) - const [usage] = await db - .select({ bytes: userStats.storageUsedBytes }) - .from(userStats) - .where(eq(userStats.userId, f.ownerId)) - expect(usage.bytes).toBe(15) - } - ) - check( 'a mixed queued batch retains each owner policy and leaves unselected workspace history alone', async () => { @@ -378,6 +347,50 @@ describe('Project file retention follows the current payer in PostgreSQL', () => } ) + check( + 'a Project that fails after committing a version batch still spends the shared run limit', + async () => { + const [first, second] = [await fixture(), await fixture()].sort((a, b) => + a.projectId < b.projectId ? -1 : 1 + ) + // One more file than a batch reads, so the first batch commits and the second one fails. + const files = await Promise.all( + Array.from({ length: FILES_PER_QUERY + 1 }, () => seedFile(first, 60 * 24)) + ) + const failingFileId = files + .map(({ fileId }) => fileId) + .sort(compareStrings) + .at(-1) + const survivor = await seedFile(second, 60 * 24) + const trigger = `version_retention_failure_${generateId().replaceAll('-', '')}` + await db.execute( + sql.raw(`CREATE FUNCTION ${trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN + IF OLD.file_id = '${failingFileId}' THEN RAISE EXCEPTION 'version retention failure fixture'; END IF; RETURN OLD; END $$`) + ) + await db.execute( + sql.raw( + `CREATE TRIGGER ${trigger} BEFORE DELETE ON workspace_file_version FOR EACH ROW EXECUTE FUNCTION ${trigger}()` + ) + ) + const committedByFirst = FILES_PER_QUERY * 2 + try { + const deleted = await cleanupFileVersions( + [ + { entityType: 'project', entityId: first.projectId }, + { entityType: 'project', entityId: second.projectId }, + ], + { plan: 'free', cutoff: new Date(), label: 'failed-owner-spends-limit' }, + committedByFirst + 1 + ) + expect(deleted).toBe(committedByFirst + 1) + expect(await versions(survivor.fileId)).toHaveLength(11) + } finally { + await db.execute(sql.raw(`DROP TRIGGER ${trigger} ON workspace_file_version`)) + await db.execute(sql.raw(`DROP FUNCTION ${trigger}()`)) + } + } + ) + check( 'omits empty Projects and batches retained Projects with the same cleanup policy', async () => { @@ -584,6 +597,11 @@ describe('Project file retention follows the current payer in PostgreSQL', () => .from(userStats) .where(eq(userStats.userId, f.ownerId)) expect(usage.bytes).toBe(10) + const [environment] = await db + .select({ bytes: workspace.storageUsedBytes }) + .from(workspace) + .where(eq(workspace.id, f.workspaceId)) + expect(environment.bytes).toBe(5) } ) @@ -631,60 +649,89 @@ describe('Project file retention follows the current payer in PostgreSQL', () => } ) - check('workspace file folder deletion failure rolls back child locations and names', async () => { - const f = await fixture() - const parentId = generateId() - const childId = generateId() - const owner = { workspaceId: f.workspaceId, userId: f.ownerId, resourceType: 'file' as const } - await db.insert(folder).values({ - ...owner, - id: parentId, - name: 'Archived parent', - deletedAt: new Date(Date.now() - 40 * 24 * HOUR), - }) - await db.insert(folder).values({ ...owner, id: childId, name: 'Child', parentId }) - const file = await seedFile(f, 1, false, 'workspace') - await db - .update(workspaceFiles) - .set({ folderId: parentId, originalName: 'Child.md' }) - .where(eq(workspaceFiles.id, file.fileId)) - const trigger = `folder_retention_failure_${generateId().replaceAll('-', '')}` - await db.execute( - sql.raw(`CREATE FUNCTION ${trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN - IF OLD.id = '${parentId}' THEN RAISE EXCEPTION 'folder retention rollback fixture'; END IF; RETURN OLD; END $$`) - ) - await db.execute( - sql.raw( - `CREATE TRIGGER ${trigger} BEFORE DELETE ON folder FOR EACH ROW EXECUTE FUNCTION ${trigger}()` - ) - ) - try { - await expect( - runCleanupSoftDeletes( + for (const budgeted of [true, false]) { + check( + budgeted + ? 'workspace file folder deletion failure rolls back child locations and names' + : 'an unbudgeted workspace file folder failure rolls back and the rest of the purge runs', + async () => { + const f = await fixture() + const parentId = generateId() + const childId = generateId() + const expiredWorkflowFolderId = generateId() + const owner = { workspaceId: f.workspaceId, userId: f.ownerId } + const expiredAt = new Date(Date.now() - 40 * 24 * HOUR) + await db.insert(folder).values([ + { + ...owner, + id: parentId, + resourceType: 'file', + name: 'Archived parent', + deletedAt: expiredAt, + }, { - workspaceIds: [f.workspaceId], - plan: 'free', - retentionHours: 30 * 24, - label: 'workspace-folder-rollback', + ...owner, + id: expiredWorkflowFolderId, + resourceType: 'workflow', + name: 'Archived workflows', + deletedAt: expiredAt, }, - createCleanupBudgets({ folders: 1 }) + ]) + await db + .insert(folder) + .values({ ...owner, id: childId, resourceType: 'file', name: 'Child', parentId }) + const file = await seedFile(f, 1, false, 'workspace') + await db + .update(workspaceFiles) + .set({ folderId: parentId, originalName: 'Child.md' }) + .where(eq(workspaceFiles.id, file.fileId)) + const trigger = `folder_retention_failure_${generateId().replaceAll('-', '')}` + await db.execute( + sql.raw(`CREATE FUNCTION ${trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN + IF OLD.id = '${parentId}' THEN RAISE EXCEPTION 'folder retention rollback fixture'; END IF; RETURN OLD; END $$`) ) - ).rejects.toMatchObject({ cause: { message: 'folder retention rollback fixture' } }) - const [child] = await db - .select({ parentId: folder.parentId, name: folder.name }) - .from(folder) - .where(eq(folder.id, childId)) - expect(child).toEqual({ parentId, name: 'Child' }) - const [head] = await db - .select({ folderId: workspaceFiles.folderId, name: workspaceFiles.originalName }) - .from(workspaceFiles) - .where(eq(workspaceFiles.id, file.fileId)) - expect(head).toEqual({ folderId: parentId, name: 'Child.md' }) - } finally { - await db.execute(sql.raw(`DROP TRIGGER ${trigger} ON folder`)) - await db.execute(sql.raw(`DROP FUNCTION ${trigger}()`)) - } - }) + await db.execute( + sql.raw( + `CREATE TRIGGER ${trigger} BEFORE DELETE ON folder FOR EACH ROW EXECUTE FUNCTION ${trigger}()` + ) + ) + try { + const run = runCleanupSoftDeletes( + { + workspaceIds: [f.workspaceId], + plan: 'free', + retentionHours: 30 * 24, + label: 'workspace-folder-rollback', + }, + budgeted ? createCleanupBudgets({ folders: 1 }) : undefined + ) + if (budgeted) { + await expect(run).rejects.toMatchObject({ + cause: { message: 'folder retention rollback fixture' }, + }) + } else { + await expect(run).resolves.toBeUndefined() + expect( + await db.select().from(folder).where(eq(folder.id, expiredWorkflowFolderId)) + ).toEqual([]) + } + const [child] = await db + .select({ parentId: folder.parentId, name: folder.name }) + .from(folder) + .where(eq(folder.id, childId)) + expect(child).toEqual({ parentId, name: 'Child' }) + const [head] = await db + .select({ folderId: workspaceFiles.folderId, name: workspaceFiles.originalName }) + .from(workspaceFiles) + .where(eq(workspaceFiles.id, file.fileId)) + expect(head).toEqual({ folderId: parentId, name: 'Child.md' }) + } finally { + await db.execute(sql.raw(`DROP TRIGGER ${trigger} ON folder`)) + await db.execute(sql.raw(`DROP FUNCTION ${trigger}()`)) + } + } + ) + } check( 'workspace file folder purge waits for a concurrent restore and leaves its children in place', diff --git a/apps/sim/lib/projects/files/retention.ts b/apps/sim/lib/projects/files/retention.ts index fa7299ab29e..3409c585039 100644 --- a/apps/sim/lib/projects/files/retention.ts +++ b/apps/sim/lib/projects/files/retention.ts @@ -16,6 +16,7 @@ import type { DbTransaction } from '@/lib/db/types' import { cleanupExpiredFileFolderInTx } from '@/lib/file-retention/folders' import { FILES_PER_QUERY, + FREE_MAX_SUPERSEDED_VERSIONS, KEEP_SUPERSEDED, releaseExpiredFileVersions, selectExpiredFileVersions, @@ -43,7 +44,6 @@ async function prepareRetention(tx: DbTransaction, projectId: string, job: FileR tx ) if (isBillingEnabled && owner.organizationId && billing.plan === null) return null - const mutation = await prepareFileStorageMutationInTx(tx, billing) const plan = isBillingEnabled ? getPlanType(billing.plan) : 'enterprise' const config = CLEANUP_CONFIG[job] let hours: number | null = config.defaults[plan] @@ -57,25 +57,30 @@ async function prepareRetention(tx: DbTransaction, projectId: string, job: FileR } if (hours === null) return null if (!Number.isFinite(hours) || hours < 0) throw new Error('Invalid Project retention policy') - return { cutoff: new Date(Date.now() - hours * 60 * 60 * 1000), plan, mutation } + return { cutoff: new Date(Date.now() - hours * 60 * 60 * 1000), plan, billing } } function projectFiles(projectId: string) { return fileOwnerCondition({ entityType: 'project', entityId: projectId }) } -/** Releases only superseded history, retaining the current head and the newest nine predecessors. */ +/** + * Releases only superseded history, retaining the current head and the newest nine predecessors. + * Each committed batch is charged to `budget` as it commits, so a later failure cannot erase + * progress the shared run limit has to count. + */ export async function cleanupProjectFileVersions( projectId: string, - limit: number + budget: RowBudget ): Promise { let released = 0 let afterId = '' - while (released < limit) { + while (budget.remaining > 0) { const batch = await cleanupDb.transaction(async (tx) => { const policy = await prepareRetention(tx, projectId, 'cleanup-file-versions') if (!policy) return null - const maximum = policy.plan === 'free' ? 99 : MAX_SUPERSEDED_FILE_VERSIONS + const maximum = + policy.plan === 'free' ? FREE_MAX_SUPERSEDED_VERSIONS : MAX_SUPERSEDED_FILE_VERSIONS const candidates = await tx .select({ id: workspaceFiles.id }) .from(workspaceFiles) @@ -114,7 +119,7 @@ export async function cleanupProjectFileVersions( ) .orderBy(asc(workspaceFiles.id)) .for('update') - const versionLimit = Math.min(DEFAULT_DELETE_CHUNK_SIZE, limit - released) + const versionLimit = Math.min(DEFAULT_DELETE_CHUNK_SIZE, budget.remaining) const expired = await selectExpiredFileVersions( tx, candidates.map(({ id }) => id), @@ -129,6 +134,7 @@ export async function cleanupProjectFileVersions( } }) if (!batch) break + consumeRowBudget(budget, batch.removed) afterId = batch.lastId released += batch.removed } @@ -145,6 +151,7 @@ export async function cleanupArchivedProjectFiles( const removed = await cleanupDb.transaction(async (tx) => { const policy = await prepareRetention(tx, projectId, 'cleanup-soft-deletes') if (!policy) return 0 + const mutation = await prepareFileStorageMutationInTx(tx, policy.billing) await lockFileDirectories(tx, [{ entityType: 'project', entityId: projectId }]) const rows = await tx .select({ @@ -188,7 +195,7 @@ export async function cleanupArchivedProjectFiles( await tx .delete(workspaceFiles) .where(and(projectFiles(projectId), inArray(workspaceFiles.id, ids))) - await policy.mutation.applyDelta(-bytes) + await mutation.applyDelta(-bytes) return rows.length }) total += removed diff --git a/apps/sim/lib/projects/membership.ts b/apps/sim/lib/projects/membership.ts index a9f8e6d5fee..95bc518c063 100644 --- a/apps/sim/lib/projects/membership.ts +++ b/apps/sim/lib/projects/membership.ts @@ -147,8 +147,6 @@ async function lockWorkspaceProjectInMode(tx: DbTransaction, workspaceId: string .where(eq(workspace.id, workspaceId)) .limit(1) if (!membership) return null - if (!membership.projectId) - throw new ProjectConflictError('Project membership needs reconciliation') await acquireAdvisoryXactLocks(tx, 'project', [ { key: projectLockKey(membership.projectId), shared }, ]) @@ -176,12 +174,13 @@ async function lockWorkspaceProjectInMode(tx: DbTransaction, workspaceId: string /** * Takes the workspace’s Project lock SHARED and rechecks membership; null means the * workspace no longer exists. For writers inside one environment (workspace files, - * shares): it fences them against exclusive Project holders (fork, split, archive, - * payer transfer, departure handoff, account deletion, retention) without serializing - * writers against each other. A transaction holding it must never later take the same - * Project exclusively ({@link lockProject}, {@link lockProjects}, {@link tryLockProjects}, - * {@link lockWorkspaceProjectExclusive}): two such holders would each wait on the other's - * shared hold. Take {@link lockWorkspaceProjectExclusive} up front instead. + * shares) and fork creation: it fences them against exclusive Project holders (split, + * archive, payer transfer, departure handoff, account deletion, retention) without + * serializing them against each other. A transaction holding it must never later take + * the same Project exclusively ({@link lockProject}, {@link lockProjects}, + * {@link tryLockProjects}, {@link lockWorkspaceProjectExclusive}): two such holders would + * each wait on the other's shared hold. Take {@link lockWorkspaceProjectExclusive} up + * front instead. */ export async function lockWorkspaceProject(tx: DbTransaction, workspaceId: string) { return lockWorkspaceProjectInMode(tx, workspaceId, true) @@ -189,17 +188,21 @@ export async function lockWorkspaceProject(tx: DbTransaction, workspaceId: strin /** * Takes the workspace’s Project lock EXCLUSIVELY and rechecks membership; null means the - * workspace no longer exists. For callers that change Project membership or lifecycle - * (fork, split, environment archive), which must exclude every writer and each other. + * workspace no longer exists. For callers that move environments between Projects or + * archive them (split, environment archive), which must exclude every writer and each other. * Take it before any shared hold on the same Project in the transaction, never after. */ export async function lockWorkspaceProjectExclusive(tx: DbTransaction, workspaceId: string) { return lockWorkspaceProjectInMode(tx, workspaceId, false) } -/** Locks the parent's Project for a new fork and refuses an archived Project. */ +/** + * Locks the parent's Project SHARED for a new fork and refuses an archived Project. Adding + * an environment only needs fencing from the exclusive holders that archive, split, or + * re-home the Project, so sibling writers need not wait out the fork's content copy. + */ export async function requireForkProject(tx: DbTransaction, parentWorkspaceId: string) { - const parent = await lockWorkspaceProjectExclusive(tx, parentWorkspaceId) + const parent = await lockWorkspaceProject(tx, parentWorkspaceId) if (!parent) throw new ProjectConflictError('Source workspace no longer exists') if (parent.archivedAt) throw new ProjectConflictError('Cannot fork an archived Project') return parent diff --git a/apps/sim/lib/table/llm/enrichment.ts b/apps/sim/lib/table/llm/enrichment.ts index 1626224c3f3..d446fee6d4b 100644 --- a/apps/sim/lib/table/llm/enrichment.ts +++ b/apps/sim/lib/table/llm/enrichment.ts @@ -1,4 +1,5 @@ import { columnTypeById, MULTI_SELECT_OPS, SINGLE_SELECT_OPS } from '@/lib/table/column-types' +import { FILTER_OPS } from '@/lib/table/constants' import type { TableSummary } from '@/lib/table/types' /** @@ -148,9 +149,9 @@ INSTRUCTIONS: 1. Build the filter yourself from the user's question - do NOT ask for confirmation. If the question names no condition at all ("the 5 most recent rows"), omit filter entirely and use order and limit instead of inventing one 2. A single condition is a plain object: {"field":"","op":"","value":}; use an array value for in/nin and omit value for isNull, isNotNull, isEmpty, and isNotEmpty 3. For multiple conditions wrap them in {"all":[...]} for AND or {"any":[...]} for OR; groups nest -4. Operators: eq, ne, gt, gte, lt, lte, in, nin, like, ilike, nlike, nilike, contains, ncontains, startsWith, endsWith, isNull, isNotNull, isEmpty, isNotEmpty +4. Operators: ${FILTER_OPS.join(', ')} ${wildcardRule} -6. Any column listed below with a restricted operator set accepts ONLY those operators - the query is rejected outright otherwise. JSON columns reject eq, ne, gt, gte, lt, lte, in, and nin; a multi-select cell holds a list, so match it by option name with contains, never ilike +6. Any column listed below with a restricted operator set accepts ONLY those operators - the query is rejected outright otherwise. JSON columns reject eq, ne, gt, gte, lt, lte, in, and nin; boolean columns reject gt, gte, lt, and lte; a multi-select cell holds a list, so match it by option name with contains, never ilike 7. For substring matching on a text column use ilike with *x* 8. For ranking queries (highest, lowest, Nth, top N) set order and a small limit, e.g. limit 1 for the highest, 2 for the second highest 9. Omit limit to return every matching row; the query fails if the result exceeds 5MB, so narrow with a filter instead of guessing a limit @@ -265,7 +266,7 @@ export function enrichTableToolParameters( if (enrichedProperties.filter) { enrichedProperties.filter = { ...enrichedProperties.filter, - description: `Predicate built from the user's question using columns: ${columnNames}. One condition is {"field":"","op":"","value":}; combine with {"all":[...]} for AND or {"any":[...]} for OR. Operators: eq, ne, gt, gte, lt, lte, in, nin, like, ilike, nlike, nilike, contains, ncontains, startsWith, endsWith, isNull, isNotNull, isEmpty, isNotEmpty.${hasSelectColumn(table) ? selectRestrictionNote(table) : ''} Omit only to match every row.`, + description: `Predicate built from the user's question using columns: ${columnNames}. One condition is {"field":"","op":"","value":}; combine with {"all":[...]} for AND or {"any":[...]} for OR. Operators: ${FILTER_OPS.join(', ')}.${hasSelectColumn(table) ? selectRestrictionNote(table) : ''} Omit only to match every row.`, } } diff --git a/apps/sim/lib/table/sql.ts b/apps/sim/lib/table/sql.ts index 96c3a99639b..e1e3818ccaa 100644 --- a/apps/sim/lib/table/sql.ts +++ b/apps/sim/lib/table/sql.ts @@ -35,11 +35,8 @@ import type { TablePredicate, } from '@/lib/table/types' -/** - * Re-exported: the `$`-prefixed wire whitelists now live with the `select` type - * definition, but this module is where callers and tests already look for them. - */ -export { MULTI_SELECT_OPERATORS, MULTI_SELECT_OPS, SINGLE_SELECT_OPERATORS, SINGLE_SELECT_OPS } +/** The `$`-prefixed wire whitelists, defined with the `select` column type. */ +export { MULTI_SELECT_OPERATORS, SINGLE_SELECT_OPERATORS } type ColumnType = ColumnDefinition['type'] type ColumnMap = ReadonlyMap diff --git a/apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts b/apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts index 143bae83071..d0741beb130 100644 --- a/apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts +++ b/apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts @@ -1,7 +1,8 @@ /** - * Real PostgreSQL lock ordering between new-file finalization and a concurrent content write in - * the same workspace. Both reach the workspace row and the per-workspace search dispatch queue row - * (through the `workspace_files` search-index trigger), so they must take them in the same order. + * Real PostgreSQL lock ordering between new-file finalization and concurrent writers in the same + * workspace. They reach the workspace row, the file directory lock, and the per-workspace search + * dispatch queue row (through the `workspace_files` search-index trigger), so every writer must + * take them in the same order. */ import { mkdtempSync } from 'node:fs' import { rm } from 'node:fs/promises' @@ -33,12 +34,14 @@ import { createKnowledgeAclFixtureIds, seedKnowledgeAclFixture, } from '@/lib/knowledge/__integration__/seed-source-access-fixture' +import { bulkArchiveWorkspaceFileItems } from '@/lib/uploads/contexts/workspace/workspace-file-folder-manager' import { generateWorkspaceFileKey, registerUploadedWorkspaceFile, uploadWorkspaceFile, } from '@/lib/uploads/contexts/workspace/workspace-file-manager' import { uploadFile } from '@/lib/uploads/core/storage-service' +import { lockFileDirectories } from '@/lib/workspace-files/locks' type FinalizeNewFile = (workspaceId: string, userId: string) => Promise @@ -164,4 +167,53 @@ describe.skipIf(!migrated)('workspace file storage lock order in PostgreSQL', () expect(results.filter((result) => result.status === 'rejected')).toEqual([]) } ) + + it.each([ + ['uploadWorkspaceFile', finalizeByUpload], + ['registerUploadedWorkspaceFile', finalizeByRegistration], + ])( + '%s commits alongside a bulk archive queued first on the directory lock', + async (_name, finalize) => { + const fixture = await seedWorkspaceWithFile() + const ready = createDeferred() + const release = createDeferred() + + const directoryHolder = db.transaction(async (tx) => { + await lockFileDirectories(tx, [{ entityType: 'workspace', entityId: fixture.workspaceId }]) + const [connection] = await tx.execute<{ pid: number }>(sql`SELECT pg_backend_pid() AS pid`) + ready.resolve(connection.pid) + await release.promise + }) + const holderPid = await ready.promise + + // Waiters blocked by the holder, or by a backend the holder blocks: only this test's queue. + const waitingOnLocks = async () => + ( + await db.execute( + sql`SELECT pid FROM pg_stat_activity + WHERE ${holderPid} = ANY(pg_blocking_pids(pid)) + OR pg_blocking_pids(pid) && ARRAY( + SELECT pid FROM pg_stat_activity WHERE ${holderPid} = ANY(pg_blocking_pids(pid)) + )` + ) + ).length + const archived = bulkArchiveWorkspaceFileItems({ + workspaceId: fixture.workspaceId, + fileIds: [fixture.existingFileId], + }) + const outcomes: Promise[]>[] = [ + Promise.allSettled([directoryHolder, archived]), + ] + try { + await expect.poll(waitingOnLocks, { timeout: 4000 }).toBe(1) + outcomes.push(Promise.allSettled([finalize(fixture.workspaceId, fixture.aliceId)])) + await expect.poll(waitingOnLocks, { timeout: 4000 }).toBe(2) + } finally { + release.resolve() + } + + const results = (await Promise.all(outcomes)).flat() + expect(results.filter((result) => result.status === 'rejected')).toEqual([]) + } + ) }) diff --git a/apps/sim/lib/uploads/contexts/workspace/creator-handoff.ts b/apps/sim/lib/uploads/contexts/workspace/creator-handoff.ts index 552ce8b178d..5dce2c8360f 100644 --- a/apps/sim/lib/uploads/contexts/workspace/creator-handoff.ts +++ b/apps/sim/lib/uploads/contexts/workspace/creator-handoff.ts @@ -4,7 +4,7 @@ import type { DbTransaction } from '@/lib/db/types' import { snapshotWorkspaceFileSecretProvenanceInTx } from '@/lib/uploads/contexts/workspace/workspace-file-secret-provenance' import { isVersionHeadCurrent, - loadWorkspaceFileVersionHead, + loadWorkspaceFileVersionHeads, materializeWorkspaceFileVersionInTx, } from '@/lib/uploads/contexts/workspace/workspace-file-versions' @@ -21,9 +21,15 @@ export async function handoffFileCreatorsInTx( .where(condition) .orderBy(asc(workspaceFiles.id)) .for('update') - for (const file of files) { - if (file.context !== 'workspace' && file.context !== 'project') continue - const head = await loadWorkspaceFileVersionHead(file.id, tx) + const versioned = files.filter( + (file) => file.context === 'workspace' || file.context === 'project' + ) + const heads = await loadWorkspaceFileVersionHeads( + versioned.map((file) => file.id), + tx + ) + for (const file of versioned) { + const head = heads.get(file.id) if (isVersionHeadCurrent(head, file)) continue const provenance = file.projectId && file.secretProvenanceVersion === null diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-folder-manager.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-folder-manager.ts index 07b38663228..691af34002b 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-folder-manager.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-folder-manager.ts @@ -311,13 +311,16 @@ async function acquireFileFolderMutationLock(tx: DbTransaction, owner: EditableF /** * For operations that archive or restore many file rows: their search-index trigger upserts the - * dispatch queue row, so the workspace row must be locked first, the order - * {@link lockWorkspaceStorageForMutationInTx} requires of every file writer. + * dispatch queue row, so the workspace row is locked after the Project lock and before the + * directory lock, the order {@link lockWorkspaceStorageForMutationInTx} requires of every file + * writer. */ async function acquireFileItemsMutationLock(tx: DbTransaction, owner: EditableFileOwner) { - await acquireFileFolderMutationLock(tx, owner) - if (owner.entityType === 'workspace') + if (owner.entityType === 'workspace') { + await lockWorkspaceProject(tx, owner.entityId) await lockWorkspaceStorageForMutationInTx(tx, owner.entityId) + } + await lockFileDirectories(tx, [owner]) } export function buildWorkspaceFileFolderPathMap( @@ -616,7 +619,7 @@ export async function resolveWorkspaceFileFolderTarget( } /** Checks an active destination without treating a foreign folder ID as a root selection. */ -async function assertFileFolderTarget( +export async function assertFileFolderTarget( owner: EditableFileOwner, folderId?: string | null, client: DbOrTx = db diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts index 92cf3008363..01a4605b483 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts @@ -126,11 +126,15 @@ import { MAX_SIM_PAGE_UPLOAD_SNIFF_BYTES, restoreSimPageSourceBuffer, } from '@/lib/workspace-files/page-source-embed' -import { contentWritableWorkspaceFileContextCondition } from '@/lib/workspace-files/query-scope' +import { + contentWritableWorkspaceFileContextCondition, + workspaceFileFolderScopeCondition, +} from '@/lib/workspace-files/query-scope' import { getWorkspaceWithOwner } from '@/lib/workspaces/permissions/utils' import { isUuid } from '@/executor/constants' import type { UserFile } from '@/executor/types' import { + assertFileFolderTarget, assertWorkspaceFileFolderTarget, buildWorkspaceFileFolderPathMap, fileNameExistsInWorkspaceFolder, @@ -650,8 +654,10 @@ async function commitFileCreateInTx( } if (args.owner.entityType === 'workspace') await lockWorkspaceProject(tx, args.owner.entityId) await lockFileDirectories(tx, [args.owner]) - const target = await resolveFileFolderTarget(args.owner, args, tx) - const folderId = target?.id ?? null + const folderId = + args.folderPath === undefined + ? await assertFileFolderTarget(args.owner, args.folderId, tx) + : ((await resolveFileFolderTarget(args.owner, args, tx))?.id ?? null) const exists = async (name: string) => { const [row] = await tx .select({ id: workspaceFiles.id }) @@ -1745,6 +1751,7 @@ const WORKSPACE_FILE_SORTS = { } satisfies Record[]> export interface QueryWorkspaceFilesOptions { + /** Exact file ids. */ fileIds?: readonly string[] scope?: WorkspaceFileScope /** Restrict to one file folder. */ @@ -1759,6 +1766,8 @@ export interface QueryWorkspaceFilesOptions { folderId?: string | null | readonly string[] /** A resolved union of folder ids and workspace-root files. */ folderScope?: FolderIdScope + /** Exact file name. */ + name?: string /** Case-insensitive substring match on the file name. */ search?: string sortBy: V2FileSortBy @@ -1784,16 +1793,6 @@ function workspaceFileFolderCondition( return eq(workspaceFiles.folderId, folderId as string) } -/** The SQL predicate for a folder scope that can include both ids and root files. */ -function workspaceFileFolderScopeCondition(scope: FolderIdScope | undefined): SQL | undefined { - if (!scope) return undefined - const ids = [...scope.folderIds] - const inScope = ids.length > 0 ? inArray(workspaceFiles.folderId, ids) : undefined - const atRoot = scope.includeRootItems ? isNull(workspaceFiles.folderId) : undefined - if (inScope && atRoot) return or(inScope, atRoot) - return inScope ?? atRoot ?? sql`false` -} - /** * One filtered, sorted, bounded page of a workspace's files. * @@ -1833,6 +1832,7 @@ async function queryFileRecords( scope = 'active', folderId, folderScope, + name, search, sortBy, sortOrder, @@ -1861,6 +1861,7 @@ async function queryFileRecords( : isNull(workspaceFiles.deletedAt), workspaceFileFolderCondition(folderId), workspaceFileFolderScopeCondition(folderScope), + name === undefined ? undefined : eq(workspaceFiles.originalName, name), searchFilter(workspaceFiles.originalName, search), resumeAfter, ] @@ -2410,10 +2411,9 @@ interface CommitFileContentOptions { secretProvenancePolicy?: WorkspaceFileSecretProvenancePolicy } -/** Commits bytes, history and provenance after the caller has locked its canonical billing payer. */ +/** Commits bytes, history and provenance after the caller has prepared the owner's accounting. */ async function commitFileContentInTx(tx: DbTransaction, options: CommitFileContentOptions) { const { owner, fileId, staged } = options - if (owner.entityType === 'workspace') await lockWorkspaceProject(tx, owner.entityId) if (staged.contentHash === null) throw new Error('Content replacement requires staged bytes with a verified content hash') assertStagedFileOwner(owner, staged) diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-storage-accounting.test.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-storage-accounting.test.ts index 38d8389cc6e..0ebfce493cb 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-storage-accounting.test.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-storage-accounting.test.ts @@ -16,7 +16,6 @@ import { workspaceFileSecretProvenanceMock, workspaceFileSecretProvenanceMockFns, } from '@sim/testing/mocks/workspace-file-secret-provenance.mock' -import { toRecord } from '@sim/utils/object' import { eq } from 'drizzle-orm' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -228,11 +227,8 @@ describe('workspace file metadata and storage accounting', () => { mockResolveFolderPathFromIndex .mockReturnValueOnce('folder-initial') .mockReturnValueOnce('folder-final') - workspaceFileFoldersMockFns.mockResolveFileFolderTarget.mockImplementationOnce( - async (_owner, target) => { - const folderId = toRecord(target).folderId - return typeof folderId === 'string' ? { id: folderId } : null - } + workspaceFileFoldersMockFns.mockAssertFileFolderTarget.mockImplementationOnce( + async (_owner, folderId) => (typeof folderId === 'string' ? folderId : null) ) dbChainMockFns.returning.mockResolvedValueOnce([inserted]) diff --git a/apps/sim/lib/uploads/contexts/workspace/workspace-file-versions.ts b/apps/sim/lib/uploads/contexts/workspace/workspace-file-versions.ts index 76d9488c00e..5ae14a0949e 100644 --- a/apps/sim/lib/uploads/contexts/workspace/workspace-file-versions.ts +++ b/apps/sim/lib/uploads/contexts/workspace/workspace-file-versions.ts @@ -103,6 +103,30 @@ export async function loadWorkspaceFileVersionHead( return head } +/** + * Batched {@link loadWorkspaceFileVersionHead}: each file's highest version, keyed by file id. + * The max is resolved from the `(file_id, version)` index first, so only head rows are fetched + * rather than every retained version. + */ +export async function loadWorkspaceFileVersionHeads( + fileIds: string[], + executor: DbOrTx = db +): Promise> { + if (!fileIds.length) return new Map() + const heads = await executor + .select(versionSummaryColumns) + .from(workspaceFileVersion) + .where( + sql`(${workspaceFileVersion.fileId}, ${workspaceFileVersion.version}) IN ( + SELECT ${workspaceFileVersion.fileId}, max(${workspaceFileVersion.version}) + FROM ${workspaceFileVersion} + WHERE ${inArray(workspaceFileVersion.fileId, fileIds)} + GROUP BY ${workspaceFileVersion.fileId} + )` + ) + return new Map(heads.map((head) => [head.fileId, head])) +} + /** * Whether the head row describes the file's current bytes. When it does not — no rows yet, or a * writer changed the key outside this module — the outgoing bytes must be materialized as their own diff --git a/apps/sim/lib/uploads/server/content-effects.test.ts b/apps/sim/lib/uploads/server/content-effects.test.ts index 7345b98375f..8efbeaae89f 100644 --- a/apps/sim/lib/uploads/server/content-effects.test.ts +++ b/apps/sim/lib/uploads/server/content-effects.test.ts @@ -1,5 +1,4 @@ -import { getMockLogger } from '@sim/testing/mocks/logger.mock' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { describe, expect, it, vi } from 'vitest' const mocks = vi.hoisted(() => ({ cleanup: vi.fn(), @@ -12,56 +11,17 @@ vi.mock('@/lib/uploads/contexts/workspace/workspace-file-live-doc-outbox', () => processWorkspaceFileLiveDocReconciliationNow: mocks.reconcile, })) -const logger = getMockLogger('FileContentEffects') - import { finishFileContentEffects } from '@/lib/uploads/server/content-effects' -const effects = { cleanupIds: ['cleanup-1'], liveDocEventId: 'event-1' } -describe('committed file reconciliation policy', () => { - beforeEach(() => { +describe('committed file content effects', () => { + it('resolves when inline live-document reconciliation throws, leaving the outbox to retry', async () => { mocks.cleanup.mockResolvedValue(undefined) - }) - it.each(['completed', 'pending', 'processing', 'lease_lost'])( - 'allows a %s result to finish or retry durably', - async (status) => { - mocks.reconcile.mockResolvedValue(status) - await expect(finishFileContentEffects(effects, { fileId: 'file-1' })).resolves.toBeUndefined() - expect(mocks.cleanup).toHaveBeenCalledExactlyOnceWith(['cleanup-1'], { fileId: 'file-1' }) - expect(mocks.reconcile).toHaveBeenCalledExactlyOnceWith('event-1') - expect(logger.error).not.toHaveBeenCalled() - if (status === 'completed') expect(logger.warn).not.toHaveBeenCalled() - else - expect(logger.warn).toHaveBeenCalledExactlyOnceWith( - 'Live document reconciliation remains pending', - { fileId: 'file-1', eventId: 'event-1', result: status } - ) - } - ) - it.each(['dead_letter', 'not_found'])( - 'reports a terminal %s result without failing the committed write', - async (status) => { - mocks.reconcile.mockResolvedValue(status) - await expect(finishFileContentEffects(effects, { fileId: 'file-1' })).resolves.toBeUndefined() - expect(mocks.cleanup).toHaveBeenCalledExactlyOnceWith(['cleanup-1'], { fileId: 'file-1' }) - expect(mocks.reconcile).toHaveBeenCalledExactlyOnceWith('event-1') - expect(logger.error).toHaveBeenCalledExactlyOnceWith( - 'Committed file live-document reconciliation requires intervention', - { fileId: 'file-1', eventId: 'event-1', result: status } - ) - } - ) - it('reports an inline processing exception without failing the committed write', async () => { mocks.reconcile.mockRejectedValue(new Error('database unavailable')) - await expect(finishFileContentEffects(effects, { fileId: 'file-1' })).resolves.toBeUndefined() - expect(mocks.cleanup).toHaveBeenCalledExactlyOnceWith(['cleanup-1'], { fileId: 'file-1' }) - expect(mocks.reconcile).toHaveBeenCalledExactlyOnceWith('event-1') - expect(logger.error).toHaveBeenCalledExactlyOnceWith( - 'Committed file live-document reconciliation failed inline', - expect.objectContaining({ - fileId: 'file-1', - eventId: 'event-1', - error: expect.objectContaining({ message: 'database unavailable' }), - }) - ) + await expect( + finishFileContentEffects( + { cleanupIds: ['cleanup-1'], liveDocEventId: 'event-1' }, + { fileId: 'file-1' } + ) + ).resolves.toBeUndefined() }) }) diff --git a/apps/sim/lib/uploads/server/content-effects.ts b/apps/sim/lib/uploads/server/content-effects.ts index 4ab1822747f..797ab45f71b 100644 --- a/apps/sim/lib/uploads/server/content-effects.ts +++ b/apps/sim/lib/uploads/server/content-effects.ts @@ -24,7 +24,7 @@ export async function finishFileContentEffects( logger.warn('Live document reconciliation remains pending', { ...context, result }) } } catch (error) { - logger.error('Committed file live-document reconciliation failed inline', { + logger.warn('Live document reconciliation deferred after inline processing error', { ...context, error: describeError(error), }) diff --git a/apps/sim/lib/webhooks/deploy.test.ts b/apps/sim/lib/webhooks/deploy.test.ts index 5867a54e4d2..54a4ae39ff3 100644 --- a/apps/sim/lib/webhooks/deploy.test.ts +++ b/apps/sim/lib/webhooks/deploy.test.ts @@ -33,6 +33,9 @@ vi.mock('@/lib/webhooks/provider-subscriptions', () => ({ createExternalWebhookSubscription: vi.fn(), hasWebhookConfigChanged: vi.fn(), })) +vi.mock('@/lib/webhooks/registration-service', () => ({ + prepareStableWebhookRegistrations: vi.fn(), +})) vi.mock('@/lib/webhooks/utils.server', () => ({ findConflictingWebhookPathOwner: vi.fn(), })) @@ -67,6 +70,7 @@ vi.mock('@/lib/webhooks/quickbooks-credentials', () => ({ import { buildProviderConfig, cleanupInactiveDeploymentWebhooks, + prepareStableTriggerWebhooksForDeploy, resolveTriggerCredentialId, resolveWebhookConfigForBlock, saveTriggerWebhooksForDeploy, @@ -78,7 +82,9 @@ import { hasWebhookConfigChanged, } from '@/lib/webhooks/provider-subscriptions' import { getProviderHandler } from '@/lib/webhooks/providers' +import { WebhookDeploymentConfigurationError } from '@/lib/webhooks/providers/errors' import { quickBooksHandler } from '@/lib/webhooks/providers/quickbooks' +import { prepareStableWebhookRegistrations } from '@/lib/webhooks/registration-service' import { findConflictingWebhookPathOwner } from '@/lib/webhooks/utils.server' import { getBlock } from '@/blocks' import { getTrigger } from '@/triggers' @@ -747,3 +753,31 @@ describe('saveTriggerWebhooksForDeploy activation recovery', () => { } ) }) + +describe('prepareStableTriggerWebhooksForDeploy failure status', () => { + const prepare = (failure: unknown) => { + vi.mocked(prepareStableWebhookRegistrations).mockRejectedValueOnce(failure) + return prepareStableTriggerWebhooksForDeploy({ + request: createMockRequest('POST'), + workflowId: 'workflow-1', + workflow: { id: 'workflow-1', workspaceId: 'workspace-1' }, + userId: 'user-1', + blocks: {}, + requestId: 'request-1', + deploymentVersionId: 'version-1', + operationId: 'operation-1', + generation: 1, + }) + } + const rejected = new WebhookDeploymentConfigurationError('Zoho Desk webhook creation failed') + + it('fails terminally when every candidate was rejected for its configuration', async () => { + const result = await prepare(new AggregateError([rejected, rejected])) + expect(result.error?.status).toBe(400) + }) + + it('stays retryable when any candidate failed transiently', async () => { + const result = await prepare(new AggregateError([rejected, new Error('HTTP 503')])) + expect(result.error?.status).toBe(500) + }) +}) diff --git a/apps/sim/lib/webhooks/deploy.ts b/apps/sim/lib/webhooks/deploy.ts index 3289637fd89..fc706d3d197 100644 --- a/apps/sim/lib/webhooks/deploy.ts +++ b/apps/sim/lib/webhooks/deploy.ts @@ -726,7 +726,7 @@ export async function resolveWebhookConfigForBlock(input: { success: false, error: { message: getErrorMessage(error, `Could not prepare ${triggerDef.name || triggerId}.`), - status: error instanceof WebhookDeploymentConfigurationError ? 400 : 500, + status: webhookConfigFailureStatus(error), }, } } @@ -766,6 +766,20 @@ async function configurePollingIfNeeded( return null } +/** + * 400 when the provider rejected the trigger's configuration, so the deploy + * fails terminally instead of retrying a create that cannot succeed. Stable + * registration reports candidate failures as an `AggregateError`, which is + * permanent only when every candidate failed that way. + */ +function webhookConfigFailureStatus(error: unknown): number { + const failures = error instanceof AggregateError ? error.errors : [error] + const permanent = + failures.length > 0 && + failures.every((failure) => failure instanceof WebhookDeploymentConfigurationError) + return permanent ? 400 : 500 +} + export interface PrepareStableTriggerWebhooksInput { request: NextRequest workflowId: string @@ -850,10 +864,7 @@ export async function prepareStableTriggerWebhooksForDeploy({ success: false, error: { message: getErrorMessage(error, 'Failed to prepare webhook registrations'), - // Propagate a provider-attached status (e.g. Zoho's 4xx edition/validation - // failures) so the deploy outbox fails terminally instead of retrying, - // matching the legacy save path's status-aware mapping below. - status: (error as { status?: number })?.status ?? 500, + status: webhookConfigFailureStatus(error), }, } } @@ -1133,11 +1144,8 @@ export async function saveTriggerWebhooksForDeploy({ (cleanupFailure as Error)?.message || (error as Error)?.message || 'Failed to create external subscription', - // Propagate a 4xx from the provider handler (e.g. a permanent Zoho - // config/permission/invalid-data failure) so the outbox classifies it - // as non-retryable; anything else (network, provider 5xx) stays 500 and - // retryable. cleanupFailure never overrides the root cause's status. - status: (error as { status?: number })?.status ?? 500, + // cleanupFailure never overrides the root cause's status. + status: webhookConfigFailureStatus(error), }, } } diff --git a/apps/sim/lib/webhooks/pending-verification.ts b/apps/sim/lib/webhooks/pending-verification.ts index 522ae8c4574..d4ac76de444 100644 --- a/apps/sim/lib/webhooks/pending-verification.ts +++ b/apps/sim/lib/webhooks/pending-verification.ts @@ -72,7 +72,6 @@ const pendingWebhookVerificationProbeMatchers: Record< method === 'GET' || method === 'HEAD' || (method === 'POST' && (!body || Object.keys(body).length === 0)), - // Zoho Desk sends a GET reachability probe at subscription-create time. // Zoho sends a GET reachability probe at subscription-create time and, if that // does not return 200, falls back to a POST probe before failing the create. // Match the empty-bodied POST too, as grain/generic/salesforce do. diff --git a/apps/sim/lib/webhooks/providers/ashby.ts b/apps/sim/lib/webhooks/providers/ashby.ts index c6b6bcff0ba..18315250edf 100644 --- a/apps/sim/lib/webhooks/providers/ashby.ts +++ b/apps/sim/lib/webhooks/providers/ashby.ts @@ -17,51 +17,7 @@ import type { WebhookProviderHandler, } from '@/lib/webhooks/providers/types' import { buildFallbackDeliveryFingerprint } from '@/lib/webhooks/providers/utils' - -/** - * Kept local rather than imported from `@/tools/ashby/utils`, which has the same - * logic. The webhook providers are reachable from workspace page graphs, and the - * knowledge page graph currently sits exactly at the ceiling - * `check:tool-registry-boundary` allows - so neither an import edge into - * `@/tools/**` nor an extra module in this directory fits. Both copies derive - * from the same three documented Ashby error shapes and are covered - * independently by `tools/ashby/utils.test.ts` and `ashby.test.ts` here. - */ -/** - * Extract a human-readable error message from an Ashby error response. Ashby - * documents two shapes and uses three in practice: - * - * - `errorInfo: { code, message, requestId }` - * - `errors: ['webhook_not_found']` - plain strings - * - `errors: [{ message, parameter }]` - objects, which is the form a 403 for a - * missing module permission arrives in, and which stringifies to - * `[object Object]` unless the message is read explicitly - * - * A single response can carry more than one of these at once. - */ -function ashbyErrorMessage(data: unknown, fallback: string): string { - if (!data || typeof data !== 'object') return fallback - const d = data as Record - const info = d.errorInfo as Record | undefined - if (info && typeof info.message === 'string' && info.message) return info.message - if (Array.isArray(d.errors) && d.errors.length > 0) { - const messages = d.errors - .map((e) => { - if (typeof e === 'string') return e - if (e && typeof e === 'object') { - const entry = e as Record - const message = typeof entry.message === 'string' ? entry.message : '' - const parameter = typeof entry.parameter === 'string' ? entry.parameter : '' - if (message && parameter) return `${message} (${parameter})` - if (message) return message - } - return '' - }) - .filter(Boolean) - if (messages.length > 0) return messages.join('; ') - } - return fallback -} +import { ashbyErrorMessage } from '@/tools/ashby/utils' /** * Whether an Ashby error response means the webhook id no longer exists. diff --git a/apps/sim/lib/webhooks/providers/zoho-desk.test.ts b/apps/sim/lib/webhooks/providers/zoho-desk.test.ts index 714e74ee0bf..118f9d013c6 100644 --- a/apps/sim/lib/webhooks/providers/zoho-desk.test.ts +++ b/apps/sim/lib/webhooks/providers/zoho-desk.test.ts @@ -11,12 +11,9 @@ vi.mock('@/lib/webhooks/provider-subscription-utils', () => ({ import { refreshAccessTokenIfNeeded } from '@/lib/oauth/credential-service' import { matchesPendingWebhookVerificationProbe } from '@/lib/webhooks/pending-verification' import { getCredentialOwner } from '@/lib/webhooks/provider-subscription-utils' +import { WebhookDeploymentConfigurationError } from '@/lib/webhooks/providers/errors' import { mapZohoWebhookError, zohoDeskHandler } from '@/lib/webhooks/providers/zoho-desk' -function errorStatus(err: unknown): number | undefined { - return (err as { status?: number })?.status -} - function makeAuthContext(headers: Record, providerConfig: Record) { return { webhook: {}, @@ -73,10 +70,10 @@ describe('zohoDeskHandler', () => { return undefined } - it('fails terminally (400) when the organization ID is missing', async () => { + it('fails terminally when the organization ID is missing', async () => { const error = await captureCreateError({ eventType: 'Ticket_Add' }) expect((error as Error)?.message).toMatch(/Organization ID/i) - expect(errorStatus(error)).toBe(400) + expect(error).toBeInstanceOf(WebhookDeploymentConfigurationError) }) }) @@ -237,7 +234,7 @@ describe('zohoDeskHandler', () => { }) describe('mapZohoWebhookError', () => { - it('surfaces INVALID_DATA field errors and is non-retryable (4xx)', () => { + it('surfaces INVALID_DATA field errors and is non-retryable', () => { const err = mapZohoWebhookError( 422, JSON.stringify({ @@ -254,7 +251,7 @@ describe('zohoDeskHandler', () => { expect(err.message).toContain('INVALID_DATA') expect(err.message).toContain('/ignoreSourceId') expect(err.message).not.toContain('Professional edition') - expect(errorStatus(err)).toBe(422) + expect(err).toBeInstanceOf(WebhookDeploymentConfigurationError) }) it('only claims the edition/permission cause when Zoho indicates it', () => { @@ -264,12 +261,12 @@ describe('zohoDeskHandler', () => { ) expect(err.message).toContain('permission') expect(err.message).toContain('Professional edition') - expect(errorStatus(err)).toBe(403) + expect(err).toBeInstanceOf(WebhookDeploymentConfigurationError) }) it('keeps provider 5xx retryable', () => { const err = mapZohoWebhookError(500, JSON.stringify({ errorCode: 'INTERNAL_ERROR' })) - expect(errorStatus(err)).toBe(503) + expect(err).not.toBeInstanceOf(WebhookDeploymentConfigurationError) }) }) diff --git a/apps/sim/lib/webhooks/providers/zoho-desk.ts b/apps/sim/lib/webhooks/providers/zoho-desk.ts index 1fc90809cf0..4ca373f9eed 100644 --- a/apps/sim/lib/webhooks/providers/zoho-desk.ts +++ b/apps/sim/lib/webhooks/providers/zoho-desk.ts @@ -5,9 +5,11 @@ import { toError } from '@sim/utils/errors' import { truncate } from '@sim/utils/string' import { eq } from 'drizzle-orm' import * as jose from 'jose' +import { LRUCache } from 'lru-cache' import { NextResponse } from 'next/server' import { refreshAccessTokenIfNeeded } from '@/lib/oauth/credential-service' import { getCredentialOwner, getNotificationUrl } from '@/lib/webhooks/provider-subscription-utils' +import { WebhookDeploymentConfigurationError } from '@/lib/webhooks/providers/errors' import type { AuthContext, DeleteSubscriptionContext, @@ -17,22 +19,28 @@ import type { SubscriptionResult, WebhookProviderHandler, } from '@/lib/webhooks/providers/types' -import { isZohoHost } from '@/tools/zoho_desk/host-allowlist' +import { safeUrlPathSegment } from '@/tools/url-path' +import { + DEFAULT_ZOHO_DESK_BASE, + extractZohoDeskBaseFromScope, + safeZohoDeskBase, +} from '@/tools/zoho_desk/host-allowlist' import { withDerivedContentText } from '@/tools/zoho_desk/utils' const logger = createLogger('WebhookProvider:ZohoDesk') -const DEFAULT_ZOHO_DESK_BASE = 'https://desk.zoho.com' -// Stop at a comma or whitespace: better-auth persists Zoho's scopes comma-joined -// (no spaces), so a greedy `\S+` would swallow the whole scope list into the host. -const ZOHO_DESK_BASE_URL_REGEX = /__zoho_domain__:([^\s,]+)/ - /** * Remote JWKS sets are cached per Desk data-center host. `createRemoteJWKSet` * caches keys and coalesces refetches internally, so a module-scoped cache keeps * verification within Zoho's 5-second delivery deadline. + * + * The host comes from `providerConfig.apiDomain`, which `SYSTEM_MANAGED_FIELDS` + * protects from *diffing* but not from being written by a workspace member. + * `safeZohoDeskBase` clamps it to a Zoho apex, but any `*.zoho.com` label still + * passes, so the ceiling stops repeated writes from growing one JWKS instance + * per label. Zoho has a handful of data centers. */ -const jwksCache = new Map>() +const jwksCache = new LRUCache>({ max: 16 }) /** * Events whose subscription filter accepts `departmentIds`. Zoho documents the @@ -63,26 +71,9 @@ const PREV_STATE_EVENTS = new Set([ 'Article_Update', ]) -/** - * Bound on distinct Desk hosts held in {@link jwksCache}. The host is derived - * from `providerConfig.apiDomain`, which `SYSTEM_MANAGED_FIELDS` protects from - * *diffing* but not from being written by a workspace member. `safeZohoDeskBase` - * already clamps it to a Zoho apex so no key material is ever fetched off-Zoho, - * but any `*.zoho.com` label still passes - so without a cap, repeated writes - * plus webhook hits could grow one JWKS instance (and its key cache) per label. - * Zoho has a handful of data centers; anything beyond this is not legitimate - * traffic, so evicting oldest-first is safe. - */ -const JWKS_CACHE_MAX_ENTRIES = 16 - function getJwks(deskHost: string): ReturnType { - const set = jwksCache.get(deskHost) - if (set) return set - - if (jwksCache.size >= JWKS_CACHE_MAX_ENTRIES) { - const oldest = jwksCache.keys().next() - if (!oldest.done) jwksCache.delete(oldest.value) - } + const cached = jwksCache.get(deskHost) + if (cached) return cached // Zoho fails a delivery that is not answered within 5 seconds and publishes no // retry, so a cold-start JWKS fetch must not consume the whole budget - // jose's default timeoutDuration is 5000ms, exactly the deadline. @@ -94,23 +85,14 @@ function getJwks(deskHost: string): ReturnType { } /** - * Anchor a Desk base URL to the Zoho apex allowlist before it is used to build a - * token-carrying request or a JWKS fetch. `providerConfig.apiDomain` is only - * written by `createSubscription`, but `SYSTEM_MANAGED_FIELDS` governs config - * *diffing*, not writability - it does not strip a caller-supplied `apiDomain` - * from an incoming providerConfig. Falling back to the US base on anything - * unrecognized keeps a hostile value from ever receiving the OAuth token or - * standing in as the JWKS issuer. + * Anchor a stored Desk base URL to the Zoho apex allowlist before it is used to + * build a token-carrying request or a JWKS fetch. `providerConfig.apiDomain` is + * only written by `createSubscription`, but `SYSTEM_MANAGED_FIELDS` governs + * config *diffing*, not writability, so a hostile value falls back to the US base + * rather than receiving the OAuth token or standing in as the JWKS issuer. */ -function safeZohoDeskBase(candidate: unknown): string { - if (typeof candidate !== 'string' || !candidate) return DEFAULT_ZOHO_DESK_BASE - try { - const url = new URL(candidate) - if (url.protocol !== 'https:' || !isZohoHost(url.hostname)) return DEFAULT_ZOHO_DESK_BASE - return candidate.replace(/\/+$/, '') - } catch { - return DEFAULT_ZOHO_DESK_BASE - } +function storedZohoDeskBase(apiDomain: string): string { + return safeZohoDeskBase(apiDomain) ?? DEFAULT_ZOHO_DESK_BASE } /** Read the persisted data-center Desk base URL from the credential's scope marker. */ @@ -121,9 +103,7 @@ async function resolveZohoDeskApiDomain(accountId: string): Promise { .from(account) .where(eq(account.id, accountId)) .limit(1) - const scope = rows[0]?.scope - const match = typeof scope === 'string' ? scope.match(ZOHO_DESK_BASE_URL_REGEX) : null - return safeZohoDeskBase(match?.[1]) + return extractZohoDeskBaseFromScope(rows[0]?.scope) ?? DEFAULT_ZOHO_DESK_BASE } catch (error) { logger.warn('Failed to resolve Zoho Desk api domain from credential', { message: toError(error).message, @@ -165,24 +145,16 @@ function resolveConfigOrgId(config: Record): string | undefined return undefined } -/** Error carrying an HTTP status so the deploy outbox can classify retryability. */ -function statusError(message: string, status: number): Error { - const err = new Error(message) as Error & { status: number } - err.status = status - return err -} - /** Zoho errorCode / message patterns that genuinely indicate a permission or edition denial. */ const ZOHO_EDITION_PERMISSION_PATTERN = /permission|not\s+(allowed|permitted|supported)|edition|upgrade|feature/i /** * Map a Zoho Desk webhook-creation failure to an actionable error that surfaces - * Zoho's real errorCode / message / field errors instead of a catch-all. The - * returned error carries an HTTP `status`: permanent client failures (4xx - - * invalid data, permission, unprocessable) keep their 4xx so the deploy outbox - * fails the deploy terminally (NonRetryableDeploymentError) with the true - * reason; rate limits and provider 5xx map to 5xx so they stay retryable. + * Zoho's real errorCode / message / field errors instead of a catch-all. + * Permanent client failures (invalid data, permission, unprocessable) are a + * {@link WebhookDeploymentConfigurationError} so the deploy fails terminally with + * the true reason; rate limits and provider 5xx stay plain, retryable errors. */ export function mapZohoWebhookError(status: number, bodyText: string): Error { let errorCode: string | undefined @@ -229,12 +201,8 @@ export function mapZohoWebhookError(status: number, bodyText: string): Error { ' (Zoho Desk webhooks require a Professional edition or higher and the Desk.webhooks.CREATE scope.)' } - // Rate limits and provider-side 5xx are transient -> keep retryable. - if (status === 429 || status >= 500) { - return statusError(realMessage, 503) - } - // Other client errors cannot succeed on retry -> carry the 4xx status. - return statusError(realMessage, status >= 400 && status < 500 ? status : 422) + if (status === 429 || status >= 500) return new Error(realMessage) + return new WebhookDeploymentConfigurationError(realMessage) } export const zohoDeskHandler: WebhookProviderHandler = { @@ -254,19 +222,14 @@ export const zohoDeskHandler: WebhookProviderHandler = { const orgId = resolveConfigOrgId(config) const eventType = typeof config.eventType === 'string' ? config.eventType : undefined - // Missing configuration is permanent - carry a 4xx so the deploy outbox fails - // terminally (NonRetryableDeploymentError) instead of retrying a create that - // can never succeed without the user fixing the trigger config. if (!orgId) { - throw statusError( - 'Zoho Desk Organization ID is required to create the webhook subscription.', - 400 + throw new WebhookDeploymentConfigurationError( + 'Zoho Desk Organization ID is required to create the webhook subscription.' ) } if (!eventType) { - throw statusError( - 'A Zoho Desk event type is required to create the webhook subscription.', - 400 + throw new WebhookDeploymentConfigurationError( + 'A Zoho Desk event type is required to create the webhook subscription.' ) } @@ -275,13 +238,12 @@ export const zohoDeskHandler: WebhookProviderHandler = { ? await refreshAccessTokenIfNeeded(owner.accountId, owner.userId, requestId) : null if (!accessToken || !owner) { - throw statusError( - 'Zoho Desk account connection required. Please connect your Zoho Desk account in the trigger configuration and try again.', - 400 + throw new WebhookDeploymentConfigurationError( + 'Zoho Desk account connection required. Please connect your Zoho Desk account in the trigger configuration and try again.' ) } - // Already allowlist-anchored by resolveZohoDeskApiDomain -> safeZohoDeskBase. + // Already allowlist-anchored by extractZohoDeskBaseFromScope. const apiDomain = await resolveZohoDeskApiDomain(owner.accountId) const notificationUrl = getNotificationUrl(webhookRecord) @@ -353,8 +315,10 @@ export const zohoDeskHandler: WebhookProviderHandler = { // verifyAuth checks, so an empty one would force verification to fail open. if (!externalId) { // Zoho reported success but gave no id: a webhook may have been created and - // is unidentifiable, so retrying risks duplicates. Fail terminally (4xx). - throw statusError('Zoho Desk webhook creation succeeded but returned no webhook id', 422) + // is unidentifiable, so retrying risks duplicates. Fail terminally. + throw new WebhookDeploymentConfigurationError( + 'Zoho Desk webhook creation succeeded but returned no webhook id' + ) } logger.info(`[${requestId}] Created Zoho Desk webhook`, { externalId }) @@ -395,10 +359,11 @@ export const zohoDeskHandler: WebhookProviderHandler = { const apiDomain = typeof config.apiDomain === 'string' && config.apiDomain - ? safeZohoDeskBase(config.apiDomain) + ? storedZohoDeskBase(config.apiDomain) : await resolveZohoDeskApiDomain(owner.accountId) - const response = await fetch(`${apiDomain}/api/v1/webhooks/${externalId}`, { + const webhookPath = safeUrlPathSegment(externalId, 'externalId') + const response = await fetch(`${apiDomain}/api/v1/webhooks/${webhookPath}`, { method: 'DELETE', headers: { Authorization: `Zoho-oauthtoken ${accessToken}`, @@ -449,7 +414,7 @@ export const zohoDeskHandler: WebhookProviderHandler = { // than defaulting to the US host and rejecting legitimate events. let apiDomain = typeof providerConfig.apiDomain === 'string' && providerConfig.apiDomain - ? safeZohoDeskBase(providerConfig.apiDomain) + ? storedZohoDeskBase(providerConfig.apiDomain) : '' if (!apiDomain) { const credentialId = diff --git a/apps/sim/lib/workflows/editing/validation.test.ts b/apps/sim/lib/workflows/editing/validation.test.ts index f83de885a0d..085cae64679 100644 --- a/apps/sim/lib/workflows/editing/validation.test.ts +++ b/apps/sim/lib/workflows/editing/validation.test.ts @@ -374,8 +374,9 @@ describe('validateInputsForBlock', () => { ) }) - it('refuses sim-auto, unknown models, missing models, and non-arrays', () => { + it('refuses sim-auto, evaluation models, unknown models, missing models, and non-arrays', () => { expect(validate([{ model: 'sim-auto' }]).valid).toBe(false) + expect(validate([{ model: 'jev-latest' }]).valid).toBe(false) expect(validate([{ model: 'definitely-not-a-model-9000' }]).valid).toBe(false) expect(validate([{ apiKey: '{{KEY}}' }]).valid).toBe(false) expect(validate({ model: 'claude-sonnet-5' }).valid).toBe(false) diff --git a/apps/sim/lib/workflows/editing/validation.ts b/apps/sim/lib/workflows/editing/validation.ts index 2e38c7dd9e6..a816ec9f8d0 100644 --- a/apps/sim/lib/workflows/editing/validation.ts +++ b/apps/sim/lib/workflows/editing/validation.ts @@ -44,6 +44,7 @@ import { getHostedModels, isAutoModel, isCustomModelId, + isEvaluationModel, isKnownModelId, suggestModelIdsForUnknownModel, } from '@/providers/models' @@ -487,6 +488,9 @@ function validateFallbackModelEntry(item: any, index: number): string | null { if (isAutoModel(model)) { return `${where}: sim-auto cannot be a fallback model; it already routes and falls back on its own` } + if (isEvaluationModel(model)) { + return `${where}: ${model} is an evaluation model and cannot serve as a chat fallback` + } if (!isKnownModelId(model) && !isCustomModelId(model)) { const suggestions = suggestModelIdsForUnknownModel(model) const suggestionText = diff --git a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.test.ts b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.test.ts index 526902b3882..7b8f7081aca 100644 --- a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.test.ts +++ b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.test.ts @@ -17,6 +17,8 @@ import { import { largeValueMetadataMock } from '@sim/testing/mocks/large-value-metadata.mock' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createTimeoutAbortController, getExecutionDeadlineAt } from '@/lib/core/execution-limits' +import { markExecutionCancelled } from '@/lib/execution/cancellation' +import { getExecutionSignalHub } from '@/lib/execution/execution-signal' import { abortManualExecution } from '@/lib/execution/manual-cancellation' import { terminalExecutionLogFields } from '@/lib/logs/execution/cancellation' import { LoggingSession } from '@/lib/logs/execution/logging-session' @@ -329,6 +331,31 @@ describe('what a failed resume did to its paused execution', () => { } ) + it('keeps the pause retryable when the cancellation subscription cannot be opened', async () => { + const managerInternals = PauseResumeManager as unknown as PauseResumeManagerInternals + const subscribeSpy = vi + .spyOn(getExecutionSignalHub(), 'subscribe') + .mockRejectedValueOnce(new Error('Redis subscriber connection ended')) + const markResumeAttemptFailedSpy = vi + .spyOn(PauseResumeManager, 'markResumeAttemptFailed') + .mockResolvedValueOnce(true) + const markResumeFailedSpy = vi.spyOn(managerInternals, 'markResumeFailed') + const { outcomes, args } = argsReportingOutcomes() + + try { + await expect(PauseResumeManager.startResumeExecution(args)).rejects.toThrow( + 'Run buffer temporarily unavailable' + ) + expect(markResumeAttemptFailedSpy).toHaveBeenCalledTimes(1) + expect(markResumeFailedSpy).not.toHaveBeenCalled() + expect(outcomes).toEqual(['pause_retained']) + } finally { + subscribeSpy.mockRestore() + markResumeAttemptFailedSpy.mockRestore() + markResumeFailedSpy.mockRestore() + } + }) + describe('when the resumed run pauses but its pause cannot be saved', () => { const spies: { mockRestore: () => void }[] = [] @@ -2161,32 +2188,7 @@ describe('repeated human review pauses', () => { } } - it('keeps the next pause snapshot attached to the log that admission claimed', async () => { - dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'log-1', deploymentVersionId: null }]) - const stopAfterSnapshot = new Error('stop after snapshot construction') - mockPreprocessExecution.mockRejectedValueOnce(stopAfterSnapshot) - - await expect(runResumeExecution(createRepeatedReviewResumeArgs())).rejects.toBe( - stopAfterSnapshot - ) - - expect(humanInTheLoopLogger.info).toHaveBeenCalledWith( - 'Created resume snapshot', - expect.objectContaining({ metadata: expect.objectContaining({ executionId: 'durable-run' }) }) - ) - expect( - dbChainMockFns.where.mock.calls.some(([condition]) => - flattenMockConditions(condition).some( - (part) => - part.type === 'eq' && - part.left === 'workflowExecutionLogs.executionId' && - part.right === 'durable-run' - ) - ) - ).toBe(true) - }) - - it('cleans the base64 cache under the durable run that the resumed blocks wrote to', async () => { + function mockCompletedResumeRun() { dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'log-1', deploymentVersionId: null }]) mockPreprocessExecution.mockResolvedValueOnce({ success: true, @@ -2205,6 +2207,32 @@ describe('repeated human review pauses', () => { logs: [], metadata: { executionId: 'durable-run' }, }) + } + + it('keeps the next pause snapshot attached to the log that admission claimed', async () => { + mockCompletedResumeRun() + + await expect(runResumeExecution(createRepeatedReviewResumeArgs())).resolves.toMatchObject({ + status: 'completed', + }) + + expect(mockExecuteWorkflowCore.mock.calls[0][0].snapshot.metadata.executionId).toBe( + 'durable-run' + ) + expect( + dbChainMockFns.where.mock.calls.some(([condition]) => + flattenMockConditions(condition).some( + (part) => + part.type === 'eq' && + part.left === 'workflowExecutionLogs.executionId' && + part.right === 'durable-run' + ) + ) + ).toBe(true) + }) + + it('cleans the base64 cache under the durable run that the resumed blocks wrote to', async () => { + mockCompletedResumeRun() await expect(runResumeExecution(createRepeatedReviewResumeArgs())).resolves.toMatchObject({ status: 'completed', @@ -2293,3 +2321,49 @@ describe('repeated human review pauses', () => { } }) }) + +describe('stopping an active resume from another replica', () => { + beforeEach(() => { + resetDbChainMock() + }) + + it('aborts the attempt when Stop records cancellation under the attempt id', async () => { + let attemptSignal: AbortSignal | undefined + const runSpy = vi + .spyOn(PauseResumeManager as unknown as PauseResumeManagerInternals, 'runResumeExecution') + .mockImplementationOnce(async (args) => { + attemptSignal = (args as { abortSignal: AbortSignal }).abortSignal + await markExecutionCancelled('stopped-attempt') + return { status: 'cancelled', success: false, output: {}, logs: [] } + }) + const attemptFailedSpy = vi + .spyOn(PauseResumeManager, 'markResumeAttemptFailed') + .mockResolvedValueOnce(false) + const processSpy = vi.spyOn(PauseResumeManager, 'processQueuedResumes').mockResolvedValueOnce() + type Args = Parameters[0] + try { + await PauseResumeManager.startResumeExecution({ + resumeEntryId: 'entry-1', + resumeExecutionId: 'stopped-attempt', + contextId: 'hitl', + resumeInput: {}, + userId: 'user-1', + pausedExecution: { + id: 'pause-1', + executionId: 'durable-run', + workflowId: 'workflow-1', + pausePoints: { hitl: { contextId: 'hitl', blockId: 'hitl' } }, + executionSnapshot: createSnapshotSeed(), + metadata: {}, + } as Args['pausedExecution'], + }) + + expect(attemptSignal?.aborted).toBe(true) + expect(attemptSignal?.reason).toMatchObject({ name: 'AbortError', message: 'user' }) + } finally { + runSpy.mockRestore() + attemptFailedSpy.mockRestore() + processSpy.mockRestore() + } + }) +}) diff --git a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts index 98900735ae4..db927c850b9 100644 --- a/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts +++ b/apps/sim/lib/workflows/executor/human-in-the-loop-manager.ts @@ -15,6 +15,7 @@ import { getTimeoutErrorMessage, type TimeoutAbortController, } from '@/lib/core/execution-limits' +import { subscribeToExecutionCancellation } from '@/lib/execution/cancellation' import { createExecutionEventWriter, flushExecutionStreamReplayBuffer, @@ -904,6 +905,7 @@ export class PauseResumeManager { ) let attemptTimeoutController: TimeoutAbortController | undefined + let unsubscribeCancellation: (() => void) | undefined try { attemptTimeoutController = createResumeAttemptTimeoutController( pausedExecution.executionSnapshot as SerializedSnapshot, @@ -911,6 +913,13 @@ export class PauseResumeManager { pausedExecution.metadata ) registerManualExecutionAborter(resumeExecutionId, attemptTimeoutController.abort) + /** Stop signals the attempt id, while the engine subscribes under the durable run id. */ + unsubscribeCancellation = await subscribeToExecutionCancellation( + resumeExecutionId, + attemptTimeoutController.abort + ).catch((cause: unknown) => { + throw new Error(RUN_BUFFER_UNAVAILABLE_ERROR, { cause }) + }) const result = await PauseResumeManager.runResumeExecution({ reservationId: resumeEntryId, resumeExecutionId, @@ -1070,6 +1079,7 @@ export class PauseResumeManager { } throw error } finally { + unsubscribeCancellation?.() if (attemptTimeoutController) { unregisterManualExecutionAborter(resumeExecutionId, attemptTimeoutController.abort) } diff --git a/apps/sim/lib/workflows/lifecycle.ts b/apps/sim/lib/workflows/lifecycle.ts index 183f7ee0c2f..14fef2a268e 100644 --- a/apps/sim/lib/workflows/lifecycle.ts +++ b/apps/sim/lib/workflows/lifecycle.ts @@ -11,6 +11,7 @@ import { workspace, } from '@sim/db/schema' import { createLogger } from '@sim/logger' +import { getErrorMessage } from '@sim/utils/errors' import { and, eq, inArray, isNull } from 'drizzle-orm' import { env } from '@/lib/core/config/env' import { PlatformEvents } from '@/lib/core/telemetry' @@ -308,8 +309,26 @@ export async function disableUserResources(userId: string): Promise { .from(workspace) .where(and(eq(workspace.ownerId, userId), isNull(workspace.archivedAt))) + const failures: { workspaceId: string; error: unknown }[] = [] + // Sequential: environments of one Project contend on its exclusive lock. for (const row of ownedWorkspaces) { - await archiveWorkspace(row.id, { requestId, expectedOwnerId: userId }) + try { + await archiveWorkspace(row.id, { requestId, expectedOwnerId: userId }) + } catch (error) { + failures.push({ workspaceId: row.id, error }) + } + } + if (failures.length > 0) { + logger.error(`[${requestId}] Failed to archive workspaces for banned user ${userId}`, { + failures: failures.map(({ workspaceId, error }) => ({ + workspaceId, + error: getErrorMessage(error), + })), + }) + throw new AggregateError( + failures.map((failure) => failure.error), + `Failed to archive ${failures.length} of ${ownedWorkspaces.length} workspaces for banned user ${userId}` + ) } logger.info( diff --git a/apps/sim/lib/workflows/persistence/utils.ts b/apps/sim/lib/workflows/persistence/utils.ts index f2be891b240..bd38961c71c 100644 --- a/apps/sim/lib/workflows/persistence/utils.ts +++ b/apps/sim/lib/workflows/persistence/utils.ts @@ -258,9 +258,9 @@ export async function materializeDeploymentState( deploymentVersionId: version.id, } - if (options.cache !== false) { - deployedStateCache.set(version.id, { workflowId, state: deployedState }) - } + if (options.cache === false) return deployedState + /** The cached entry is shared, so each caller gets its own copy. */ + deployedStateCache.set(version.id, { workflowId, state: deployedState }) return structuredClone(deployedState) } diff --git a/apps/sim/lib/workflows/references/dependent-reconfigs.ts b/apps/sim/lib/workflows/references/dependent-reconfigs.ts index 30e98a60492..a4dca9feef0 100644 --- a/apps/sim/lib/workflows/references/dependent-reconfigs.ts +++ b/apps/sim/lib/workflows/references/dependent-reconfigs.ts @@ -26,7 +26,10 @@ import { isNonEmptyValue, scopeCanonicalModesForTool, } from '@/lib/workflows/subblocks/visibility' -import { resolveToolParamRequired } from '@/lib/workflows/tool-input/param-visibility' +import { + buildToolParamVisibilityMap, + resolveToolParamRequired, +} from '@/lib/workflows/tool-input/param-visibility' import { getBlock } from '@/blocks/registry' import type { SubBlockConfig } from '@/blocks/types' import type { WorkflowState } from '@/stores/workflows/workflow/types' @@ -253,9 +256,9 @@ function emitAnchoredDependents(params: EmitAnchoredParams): void { // `user-or-llm` / `llm-only` param is filled by the model at runtime, so a blank // one is intentional. Applies to the nested pass only, where visibility is known. // - // Testing `rawSourceValue` directly is sound: the dormant guard above has already + // Testing `rawDependentValue` directly is sound: the dormant guard above has already // returned for any pair in advanced mode, so the pair is basic-active here and - // `rawSourceValue` IS the group's active canonical value. + // `rawDependentValue` IS the group's active canonical value. const configuredRequired = resolveToolParamRequired(dependent, values, paramVisibilityById) out.push({ parentKind: anchor.parentKind, @@ -277,9 +280,9 @@ function emitAnchoredDependents(params: EmitAnchoredParams): void { // `sourceValue` stays the raw source reference (the copy-resolved parent's seed). currentValue: rawSourceValue, // Ask the emptiness question of the RAW value, not the string-coerced one: - // `rawSourceValue` flattens every non-string (a multi-select selector stores an - // array) to `''`, which would report a populated field as blank and silently - // un-gate it. `isNonEmptyValue` handles arrays and non-strings on purpose. + // `rawSourceValue` joins a string array but flattens any other non-string to `''`, + // which would report a populated field as blank and silently un-gate it. + // `isNonEmptyValue` handles arrays and non-strings on purpose. required: configuredRequired && (params.includeUnconfiguredRequired || @@ -415,28 +418,15 @@ export function collectForkDependentReconfigs( typeof tool.title === 'string' && tool.title ? tool.title : toolConfig.name const toolInputKey = cfg.id const toolIndex = index - // Resolved `ParameterVisibility` per param, from the same resolver the tool-row UI - // and the rest of fork remapping use - so "is this the user's to fill?" is answered - // identically in the editor and in the sync gate. Keyed by both the sub-block id and - // its canonical param id, since a nested tool stores picks under either. - const paramVisibilityById = new Map() - for (const resolved of getToolInputParamConfigs({ - tool: { ...tool, type: tool.type, params: toolParams }, - toolIndex, - parentCanonicalModes: block.data?.canonicalModes, - })) { - if (!resolved.authoritative) continue - const visibility = resolved.config.paramVisibility - paramVisibilityById.set(resolved.paramId, visibility) - // The canonical id is an ALIAS, so it must never clobber a param that owns that - // key as its own `paramId` - first (own-id) write wins. - if ( - resolved.config.canonicalParamId && - !paramVisibilityById.has(resolved.config.canonicalParamId) - ) { - paramVisibilityById.set(resolved.config.canonicalParamId, visibility) - } - } + // Same resolver the tool-row UI and fork remapping use, so "is this the user's to + // fill?" is answered identically in the editor and in the sync gate. + const paramVisibilityById = buildToolParamVisibilityMap( + getToolInputParamConfigs({ + tool: { ...tool, type: tool.type, params: toolParams }, + toolIndex, + parentCanonicalModes: block.data?.canonicalModes, + }) + ) emitAnchoredDependents({ config: toolConfig, includeUnconfiguredRequired, diff --git a/apps/sim/lib/workflows/references/remap-references.ts b/apps/sim/lib/workflows/references/remap-references.ts index 3a3e88609cb..0397bf31a45 100644 --- a/apps/sim/lib/workflows/references/remap-references.ts +++ b/apps/sim/lib/workflows/references/remap-references.ts @@ -41,6 +41,7 @@ import { scopeCanonicalModesForTool, } from '@/lib/workflows/subblocks/visibility' import { + buildToolParamVisibilityMap, isSubBlockRequired, resolveToolParamRequired, } from '@/lib/workflows/tool-input/param-visibility' @@ -49,7 +50,6 @@ import { isCustomBlockType, RESERVED_PARAMS } from '@/blocks/custom/build-config import { getBlock } from '@/blocks/registry' import type { SubBlockConfig } from '@/blocks/types' import { isEnvVarReference, isReference } from '@/executor/constants' -import type { ParameterVisibility } from '@/tools/types' /** * Resource kinds the fork remapper rewrites across workspaces, derived from the @@ -1690,22 +1690,13 @@ function collectClearedToolParamDependents( // (a model-supplied param is not the user's to fill) and then this collector would mark it // required, which SKIPS the target's redeploy in `promote.ts` - leaving the fork silently // running its previous deployed version. - const paramVisibilityById = new Map() - for (const resolved of getToolInputParamConfigs({ - tool: { ...tool, type: tool.type, params: mergedParams }, - toolIndex: index, - parentCanonicalModes, - })) { - if (!resolved.authoritative) continue - const visibility = resolved.config.paramVisibility - paramVisibilityById.set(resolved.paramId, visibility) - if ( - resolved.config.canonicalParamId && - !paramVisibilityById.has(resolved.config.canonicalParamId) - ) { - paramVisibilityById.set(resolved.config.canonicalParamId, visibility) - } - } + const paramVisibilityById = buildToolParamVisibilityMap( + getToolInputParamConfigs({ + tool: { ...tool, type: tool.type, params: mergedParams }, + toolIndex: index, + parentCanonicalModes, + }) + ) for (const cfg of toolConfig.subBlocks) { if (!cfg.dependsOn || !cfg.id) continue // Only flag a param the TARGET tool had configured (not one the source carried in). diff --git a/apps/sim/lib/workflows/search-replace/resources/resolvers.test.ts b/apps/sim/lib/workflows/search-replace/resources/resolvers.test.ts index 13ec0bba1a1..720cacbb2cf 100644 --- a/apps/sim/lib/workflows/search-replace/resources/resolvers.test.ts +++ b/apps/sim/lib/workflows/search-replace/resources/resolvers.test.ts @@ -118,10 +118,9 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => { }) /** - * The bucketed dedupe replaced an O(n^2) linear rescan. This pins it to a - * transcription of the original algorithm over randomized inputs, so any - * divergence in which overlapping match wins shows up as a diff rather than - * as a subtly wrong result the fixed examples above would miss. + * Pins the bucketed dedupe to a plain linear-scan oracle over randomized + * inputs, so any divergence in which overlapping match wins shows up as a + * diff rather than as a subtly wrong result the fixed examples would miss. */ describe('bucketed dedupe matches the original linear scan', () => { function scopeKey(match: WorkflowSearchMatch): string | null { @@ -145,7 +144,7 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => { return false } - /** Straight transcription of the pre-optimization implementation. */ + /** The plain linear-scan dedupe, as an oracle. */ function referenceDedupe(matches: WorkflowSearchMatch[]): WorkflowSearchMatch[] { const deduped: WorkflowSearchMatch[] = [] for (const match of matches) { @@ -215,11 +214,7 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => { }) } - /** - * A sequential sweep, not a handful of hand-picked seeds. An earlier version - * pinned 8 seeds that happened to be clean while 7.7% of the space diverged, - * so the count is what gives this test its power - keep it wide. - */ + /** A wide sequential sweep: a handful of hand-picked seeds can all be clean while divergences hide elsewhere. */ it('agrees with the original scan across 400 seeded inputs', () => { const diverged: number[] = [] @@ -234,9 +229,9 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => { }) /** - * The exact shape that broke the `maxEnd` short-circuit: a shorter range - * evicts a longer one but ends further right, so a stale `maxEnd` let the - * next match skip the overlap scan and leak through as a duplicate. + * A shorter range evicts a longer one but ends further right; a stale + * `maxEnd` would let the next match skip the overlap scan and leak through + * as a duplicate. */ it.each([ { @@ -297,9 +292,8 @@ describe('dedupeOverlappingWorkflowSearchMatches', () => { /** * Pins the asymptotics, not a stopwatch. 20k disjoint hits in one scope run - * in single-digit ms bucketed; the O(n^2) rescan this replaced took ~30s on - * the same input, so the bound has roughly three orders of magnitude of - * headroom and only trips on a genuine complexity regression. + * in single-digit ms; a quadratic rescan takes ~30s on the same input, so + * the bound only trips on a genuine complexity regression. */ it('stays sub-quadratic on a single scope full of disjoint ranges', () => { const matches = Array.from({ length: 20_000 }, (_, index) => diff --git a/apps/sim/lib/workflows/search-replace/resources/resolvers.ts b/apps/sim/lib/workflows/search-replace/resources/resolvers.ts index b75490b6d51..adc7f89ff9c 100644 --- a/apps/sim/lib/workflows/search-replace/resources/resolvers.ts +++ b/apps/sim/lib/workflows/search-replace/resources/resolvers.ts @@ -136,44 +136,18 @@ function widenScopeBucket(bucket: RangeMatchScopeBucket, end: number): void { } /** - * Overlap resolution is scoped to one value inside one subblock, so candidates - * are bucketed by that scope rather than rescanned. The previous `findIndex` - * over the whole accumulated list recomputed every candidate's scope key on - * every iteration - O(n^2) string builds, which cost ~1.4s on a workflow - * producing ~500 matches and froze the search field while typing. + * Collapses overlapping range matches, keeping the preferred one per overlap. * - * A bucket only ever holds entries that have both a scope key and a range, and - * those are exactly the entries the old predicate could match. Buckets keep - * insertion order and the scan stops at the first overlap, so this picks the - * same candidate the linear scan did. + * Overlap is only possible within one value of one subblock, so kept entries + * are bucketed by that scope key, in insertion order; the first overlapping + * entry in the bucket is the one compared against. * - * `maxEnd` is a monotonic high-water mark, not the exact current maximum: a - * replacement can swap in a range that ends earlier without lowering it. Only - * the upper bound is load-bearing. A match starting at or after it cannot - * overlap anything in the bucket, so the scan is skipped; a bound left too high - * only costs a scan that would have been skipped, never a wrong answer. - * - * Recomputing the exact maximum on every shrinking replacement is a net loss - - * it walks the bucket, which is the cost this is here to avoid, and staleness - * is capped at one token length because every range spans a matched token - * (`query.length`, or a reference's `rawValue.length`) rather than the field. - * Measured on the realistic overlap shape at 10k matches: 23ms as written, - * 45ms with the recompute, against 1010ms for the scan this replaced. - * - * The bound keeps a field full of disjoint hits linear instead of quadratic - * within its own bucket, to the extent its matches arrive in ascending offset - * order; out-of-order producers just fall back to scanning. - * - * It must be refreshed on the replacement path too, not only on append: - * `shouldPreferOverlappingMatch` prefers the SHORTER range, and a shorter range - * can still end further right than the one it evicts. Leaving `maxEnd` stale - * there let the short-circuit skip genuine overlaps and leak duplicates. - * - * Only `NaN` ends are ignored. `Math.max` with `NaN` would pin `maxEnd` at - * `NaN`, and since every comparison against `NaN` is false that would silently - * switch dedupe off for the rest of the scope. A `NaN`-ended range cannot - * overlap anything anyway, while positive infinity is an unbounded end that - * can overlap later ranges and therefore must widen the high-water mark. + * Each bucket's `maxEnd` is an upper bound on its entries' `range.end`, not the + * exact maximum: a match starting at or after it cannot overlap anything in the + * bucket, so the scan is skipped. It must be widened on replacement as well as + * on append, because a shorter preferred range can still end further right. + * `NaN` ends are ignored (they overlap nothing and would poison `Math.max`); + * `+Infinity` widens it. */ export function dedupeOverlappingWorkflowSearchMatches( matches: T[] diff --git a/apps/sim/lib/workflows/tool-input/param-visibility.test.ts b/apps/sim/lib/workflows/tool-input/param-visibility.test.ts index bc2c10c0034..a4659886213 100644 --- a/apps/sim/lib/workflows/tool-input/param-visibility.test.ts +++ b/apps/sim/lib/workflows/tool-input/param-visibility.test.ts @@ -1,9 +1,10 @@ import { describe, expect, it } from 'vitest' import { isSubBlockRequired, - isToolParamUserRequired, isUserSuppliedToolParam, + resolveToolParamRequired, } from '@/lib/workflows/tool-input/param-visibility' +import type { ParameterVisibility } from '@/tools/types' describe('isUserSuppliedToolParam', () => { it('is true only for user-only', () => { @@ -14,29 +15,36 @@ describe('isUserSuppliedToolParam', () => { }) }) -describe('isToolParamUserRequired', () => { +describe('resolveToolParamRequired', () => { + const visibleAs = (paramVisibility: ParameterVisibility) => new Map([['param', paramVisibility]]) + it('requires both user-only visibility and a satisfied required condition', () => { - expect(isToolParamUserRequired({ paramVisibility: 'user-only', required: true }, {})).toBe(true) - expect(isToolParamUserRequired({ paramVisibility: 'user-only', required: false }, {})).toBe( - false - ) + expect( + resolveToolParamRequired({ id: 'param', required: true }, {}, visibleAs('user-only')) + ).toBe(true) + expect( + resolveToolParamRequired({ id: 'param', required: false }, {}, visibleAs('user-only')) + ).toBe(false) }) it('never requires a param the model can supply, even when required is true', () => { // `required: true` still drives the model-facing schema; it just is not the USER's to fill. - expect(isToolParamUserRequired({ paramVisibility: 'user-or-llm', required: true }, {})).toBe( - false - ) - expect(isToolParamUserRequired({ paramVisibility: 'llm-only', required: true }, {})).toBe(false) + expect( + resolveToolParamRequired({ id: 'param', required: true }, {}, visibleAs('user-or-llm')) + ).toBe(false) + expect( + resolveToolParamRequired({ id: 'param', required: true }, {}, visibleAs('llm-only')) + ).toBe(false) }) it('evaluates the condition form against the surrounding values', () => { - const config = { - paramVisibility: 'user-only' as const, - required: { field: 'operation', value: ['write'] }, - } - expect(isToolParamUserRequired(config, { operation: 'write' })).toBe(true) - expect(isToolParamUserRequired(config, { operation: 'read' })).toBe(false) + const config = { id: 'param', required: { field: 'operation', value: ['write'] } } + expect(resolveToolParamRequired(config, { operation: 'write' }, visibleAs('user-only'))).toBe( + true + ) + expect(resolveToolParamRequired(config, { operation: 'read' }, visibleAs('user-only'))).toBe( + false + ) }) }) diff --git a/apps/sim/lib/workflows/tool-input/param-visibility.ts b/apps/sim/lib/workflows/tool-input/param-visibility.ts index 22272e692ec..0e8f25d39aa 100644 --- a/apps/sim/lib/workflows/tool-input/param-visibility.ts +++ b/apps/sim/lib/workflows/tool-input/param-visibility.ts @@ -1,3 +1,4 @@ +import type { ResolvedToolInputParamConfig } from '@/lib/workflows/search-replace/indexer' import { evaluateSubBlockCondition } from '@/lib/workflows/subblocks/visibility' import type { SubBlockConfig } from '@/blocks/types' import type { ParameterVisibility } from '@/tools/types' @@ -22,7 +23,7 @@ const DEFAULT_TOOL_PARAM_VISIBILITY: ParameterVisibility = 'user-or-llm' * for different reasons, and a denylist would silently mis-classify any visibility added * later. */ -export function isToolParamUserRequired( +function isToolParamUserRequired( config: Pick, values: Record ): boolean { @@ -42,6 +43,27 @@ export function isUserSuppliedToolParam(config: Pick { + const visibilityById = new Map() + for (const resolved of resolvedParams) { + if (!resolved.authoritative) continue + const visibility = resolved.config.paramVisibility + visibilityById.set(resolved.paramId, visibility) + const canonicalParamId = resolved.config.canonicalParamId + if (canonicalParamId && !visibilityById.has(canonicalParamId)) { + visibilityById.set(canonicalParamId, visibility) + } + } + return visibilityById +} + /** * Whether a `tool-input` param must be supplied by the user, resolving its visibility from a * map keyed by sub-block id and canonical param id. diff --git a/apps/sim/lib/workspace-files/application/list-workspace-files.ts b/apps/sim/lib/workspace-files/application/list-workspace-files.ts index 1df52e43c46..39aa01f21b2 100644 --- a/apps/sim/lib/workspace-files/application/list-workspace-files.ts +++ b/apps/sim/lib/workspace-files/application/list-workspace-files.ts @@ -31,6 +31,10 @@ export interface QueryWorkspaceFilePageInput { * default — see the v2 route, where a `search` implies a recursive look. */ recursive?: boolean + /** Exact file id. */ + fileId?: string + /** Exact file name. */ + name?: string search?: string sortBy: 'name' | 'size' | 'uploadedAt' | 'updatedAt' sortOrder: 'asc' | 'desc' @@ -118,6 +122,8 @@ export const queryWorkspaceFilePage = defineAuthorizedWorkspaceFileUseCase({ scope: input.scope, folderId, folderScope: input.folderScope, + fileIds: input.fileId === undefined ? undefined : [input.fileId], + name: input.name, search: input.search, sortBy: input.sortBy, sortOrder: input.sortOrder, diff --git a/apps/sim/lib/workspace-files/application/search-workspace-file-content.ts b/apps/sim/lib/workspace-files/application/search-workspace-file-content.ts index c132784cba7..83c3cdf52f3 100644 --- a/apps/sim/lib/workspace-files/application/search-workspace-file-content.ts +++ b/apps/sim/lib/workspace-files/application/search-workspace-file-content.ts @@ -54,14 +54,9 @@ export const searchWorkspaceFileContent = defineAuthorizedWorkspaceFileUseCase({ signal?.throwIfAborted() /** * Resolved here rather than at the surface so every caller (the File - * block, the v2 route) is confined by the same check. A folder tree - * holding one subtree per user makes this scope the isolation boundary, - * not a convenience filter. - */ - /** - * `!== undefined`, not a length check: an explicitly empty list is a scope - * that names no folder, which must match nothing. Treating it as "absent" - * would answer a request for nothing with the whole workspace. + * block, the v2 route) narrows by the same rules. `!== undefined`, not a + * length check: an explicitly empty list is a scope that names no folder, + * which must match nothing rather than the whole workspace. */ const folderScope = input.folderPaths !== undefined diff --git a/apps/sim/lib/workspace-files/edit-content.test.ts b/apps/sim/lib/workspace-files/edit-content.test.ts index 8c93ab46c38..1b24d855229 100644 --- a/apps/sim/lib/workspace-files/edit-content.test.ts +++ b/apps/sim/lib/workspace-files/edit-content.test.ts @@ -188,7 +188,7 @@ describe('applyWorkspaceFileContentEdit', () => { ).toThrow(/whole number/) }) - it('refuses a matching end-anchor occurrence that precedes the start anchor', () => { + it('refuses an end anchor that only appears before the start anchor', () => { expect(() => applyWorkspaceFileContentEdit('end\nstart', { mode: 'replace_between', @@ -196,7 +196,75 @@ describe('applyWorkspaceFileContentEdit', () => { afterAnchor: 'end', content: 'x', }) - ).toThrow(/end anchor must follow the start anchor/) + ).toThrow(/Anchor line not found after the start anchor/) + }) + + it('ends the range at the first end anchor after the start, wherever earlier ones sit', () => { + expect( + applyWorkspaceFileContentEdit('a {\n}\nb {\nx\n}', { + mode: 'delete_between', + startAnchor: 'b {', + endAnchor: '}', + }) + ).toBe('a {\n}\n}') + + expect( + applyWorkspaceFileContentEdit(['s', 'e', 's', 'e', 's', 'one', 'e'].join('\n'), { + mode: 'replace_between', + beforeAnchor: 's', + afterAnchor: 'e', + occurrence: 3, + content: 'two', + }) + ).toBe(['s', 'e', 's', 'e', 's', 'two', 'e'].join('\n')) + }) + + it('keeps each untouched line ending in a file with mixed endings', () => { + expect( + applyWorkspaceFileContentEdit('crlf\r\nanchor\nlf\nend\r\n', { + mode: 'insert_after', + anchor: 'anchor', + content: 'one\r\ntwo', + }) + ).toBe('crlf\r\nanchor\none\ntwo\nlf\nend\r\n') + + expect( + applyWorkspaceFileContentEdit('start\r\nold\nend\nlf\n', { + mode: 'replace_between', + beforeAnchor: 'start', + afterAnchor: 'end', + content: 'a\nb', + }) + ).toBe('start\r\na\r\nb\nend\nlf\n') + + expect( + applyWorkspaceFileContentEdit('before\r\nold\nend', { + mode: 'replace_between', + beforeAnchor: 'before', + afterAnchor: 'end', + content: '', + }) + ).toBe('before\r\nend') + }) + + it('inserts more lines than a single call can take as arguments', () => { + const content = Array.from({ length: 1_000_000 }, () => 'x').join('\n') + const expected = `start\n${content}\nend` + expect( + applyWorkspaceFileContentEdit('start\nend', { + mode: 'insert_after', + anchor: 'start', + content, + }) + ).toBe(expected) + expect( + applyWorkspaceFileContentEdit('start\nold\nend', { + mode: 'replace_between', + beforeAnchor: 'start', + afterAnchor: 'end', + content, + }) + ).toBe(expected) }) }) diff --git a/apps/sim/lib/workspace-files/edit-content.ts b/apps/sim/lib/workspace-files/edit-content.ts index 27934028f36..c30fcf072ea 100644 --- a/apps/sim/lib/workspace-files/edit-content.ts +++ b/apps/sim/lib/workspace-files/edit-content.ts @@ -5,7 +5,6 @@ export type EditContentFailure = | { reason: 'not_found' } | { reason: 'ambiguous'; lineNumbers: number[] } | { reason: 'invalid_occurrence' } - | { reason: 'invalid_anchor_order' } | { reason: 'output_too_large'; maxBytes: number } export interface EditContentOptions { @@ -186,23 +185,31 @@ function validateOccurrence(occurrence: number | undefined): number { return value } -function anchorLineIndex(lines: string[], anchor: string, occurrence: number): number { +/** Index of the `occurrence`th line at or after `fromIndex` whose trimmed text equals the anchor. */ +function anchorLineIndex( + lines: string[], + anchor: string, + occurrence: number, + fromIndex = 0 +): number { const normalizedAnchor = anchor.trim() if (!normalizedAnchor) { throw new EditContentError('Anchor cannot be empty', { reason: 'not_found' }) } let matches = 0 - for (let index = 0; index < lines.length; index++) { + for (let index = fromIndex; index < lines.length; index++) { if (lines[index].trim() !== normalizedAnchor) continue matches++ if (matches === occurrence) return index } if (matches === 0) { - throw new EditContentError(`Anchor line not found: ${JSON.stringify(anchor.slice(0, 100))}`, { - reason: 'not_found', - }) + const where = fromIndex > 0 ? ' after the start anchor' : '' + throw new EditContentError( + `Anchor line not found${where}: ${JSON.stringify(anchor.slice(0, 100))}`, + { reason: 'not_found' } + ) } throw new EditContentError( `Anchor occurrence ${occurrence} not found; only ${matches} matching line${matches === 1 ? '' : 's'} exist`, @@ -216,15 +223,42 @@ function contentLines(content: string): string[] { return lines.at(-1) === '' ? lines.slice(0, -1) : lines } +/** + * Lines plus the separator that follows each one, so an edit rewrites only + * the lines it touches. `separators[i]` joins `lines[i]` to `lines[i + 1]`. + */ +interface SeparatedLines { + lines: string[] + separators: string[] +} + +function splitSeparatedLines(text: string): SeparatedLines { + const parts = text.split(/(\r\n|\n)/) + return { + lines: parts.filter((_, index) => index % 2 === 0), + separators: parts.filter((_, index) => index % 2 === 1), + } +} + +function joinSeparatedLines({ lines, separators }: SeparatedLines): string { + let text = lines[0] + for (let index = 1; index < lines.length; index++) { + text += separators[index - 1] + lines[index] + } + return text +} + /** * Applies one deterministic in-place edit to text. * * Exact replacement operates on byte-equivalent strings and refuses ambiguity * unless `replaceAll` is explicit. Anchored edits match complete trimmed lines, - * which makes them stable when surrounding line numbers move. Boundary anchors - * remain in place for replacement and insertion. Deletion matches the - * `apply_file_edit` protocol: it removes the start anchor and everything before - * the end anchor, while preserving the end anchor. + * which makes them stable when surrounding line numbers move. `occurrence` + * selects the start anchor (or the `insert_after` anchor); the end anchor is + * the first match after it. Boundary anchors remain in place for replacement + * and insertion. Deletion removes the start anchor and everything before the + * end anchor, while preserving the end anchor. Untouched lines keep their own + * line endings; inserted lines take the ending that follows the anchor line. */ export function applyWorkspaceFileContentEdit( text: string, @@ -235,40 +269,51 @@ export function applyWorkspaceFileContentEdit( return applyStringReplacement(text, edit.search, edit.content, edit.replaceAll, options) } - const lines = splitLines(text) + const { lines, separators } = splitSeparatedLines(text) const occurrence = validateOccurrence(edit.occurrence) - const eol = detectLineEnding(text) if (edit.mode === 'insert_after') { const anchorIndex = anchorLineIndex(lines, edit.anchor, occurrence) - return [ - ...lines.slice(0, anchorIndex + 1), - ...contentLines(edit.content), - ...lines.slice(anchorIndex + 1), - ].join(eol) + const inserted = contentLines(edit.content) + const separator = separators[anchorIndex] ?? detectLineEnding(text) + return joinSeparatedLines({ + lines: [...lines.slice(0, anchorIndex + 1), ...inserted, ...lines.slice(anchorIndex + 1)], + separators: [ + ...separators.slice(0, anchorIndex), + ...inserted.map(() => separator), + ...separators.slice(anchorIndex), + ], + }) } const startAnchor = edit.mode === 'replace_between' ? edit.beforeAnchor : edit.startAnchor const endAnchor = edit.mode === 'replace_between' ? edit.afterAnchor : edit.endAnchor const startIndex = anchorLineIndex(lines, startAnchor, occurrence) - const endIndex = anchorLineIndex(lines, endAnchor, occurrence) - - if (endIndex <= startIndex) { - throw new EditContentError( - `Anchor occurrence ${occurrence} is not ordered: the end anchor must follow the start anchor`, - { reason: 'invalid_anchor_order' } - ) - } + const endIndex = anchorLineIndex(lines, endAnchor, 1, startIndex + 1) if (edit.mode === 'replace_between') { - return [ - ...lines.slice(0, startIndex + 1), - ...contentLines(edit.content), - ...lines.slice(endIndex), - ].join(eol) + const inserted = contentLines(edit.content) + const separator = separators[startIndex] + // The last inserted line keeps the ending of the last replaced line; with nothing inserted, + // the start anchor keeps its own. + const keptSeparators = + inserted.length === 0 + ? [...separators.slice(0, startIndex + 1), ...separators.slice(endIndex)] + : [ + ...separators.slice(0, startIndex), + ...inserted.map(() => separator), + ...separators.slice(endIndex - 1), + ] + return joinSeparatedLines({ + lines: [...lines.slice(0, startIndex + 1), ...inserted, ...lines.slice(endIndex)], + separators: keptSeparators, + }) } - return [...lines.slice(0, startIndex), ...lines.slice(endIndex)].join(eol) + return joinSeparatedLines({ + lines: [...lines.slice(0, startIndex), ...lines.slice(endIndex)], + separators: [...separators.slice(0, startIndex), ...separators.slice(endIndex)], + }) } /** diff --git a/apps/sim/lib/workspace-files/folder-path-selection.test.ts b/apps/sim/lib/workspace-files/folder-path-selection.test.ts index 4dfa6dbc384..f7a1c66b7ad 100644 --- a/apps/sim/lib/workspace-files/folder-path-selection.test.ts +++ b/apps/sim/lib/workspace-files/folder-path-selection.test.ts @@ -19,10 +19,10 @@ const folders = [ describe('resolveFolderIdsForPaths', () => { /* - * A per-user memory tree makes the scope an isolation boundary rather than a - * filter, so the sibling cases below are the ones that matter: `user-a` and - * `user-a-2` share a prefix, and a resolver comparing raw strings instead of - * decoded segments would hand one user the other's notes. + * In a per-user memory tree the scope is what picks one user's notes, so the + * sibling cases below are the ones that matter: `user-a` and `user-a-2` share + * a prefix, and a resolver comparing raw strings instead of decoded segments + * would read the other user's notes. */ const memory = [ { id: 'memory', parentId: null, path: 'memory' }, @@ -38,33 +38,33 @@ describe('resolveFolderIdsForPaths', () => { it("keeps one user's subtree out of another's scope", () => { const result = resolveFolderIdsForPaths(memory, ['/memory/user-a']) - expect([...(result.folderIds ?? [])].sort()).toEqual(['a', 'a-commitments', 'a-people']) + expect([...(result.scope?.folderIds ?? [])].sort()).toEqual(['a', 'a-commitments', 'a-people']) }) it('does not let a shared name prefix widen the scope', () => { const result = resolveFolderIdsForPaths(memory, ['/memory/user-a']) - expect(result.folderIds?.has('a2')).toBe(false) - expect(result.folderIds?.has('a2-people')).toBe(false) + expect(result.scope?.folderIds.has('a2')).toBe(false) + expect(result.scope?.folderIds.has('a2-people')).toBe(false) }) it('never climbs to a parent of the requested folder', () => { const result = resolveFolderIdsForPaths(memory, ['/memory/user-a/people']) - expect([...(result.folderIds ?? [])]).toEqual(['a-people']) + expect([...(result.scope?.folderIds ?? [])]).toEqual(['a-people']) }) it('matches a folder whose name contains a slash', () => { const result = resolveFolderIdsForPaths(folders, ['/Reports/Q3%2FQ4']) - expect([...(result.folderIds ?? [])]).toEqual(['slashy']) + expect([...(result.scope?.folderIds ?? [])]).toEqual(['slashy']) }) it('reports a path that matches nothing rather than reading less', () => { const result = resolveFolderIdsForPaths(folders, ['/Reports', '/Nope']) expect(result.missingPath).toBe('/Nope') - expect(result.folderIds).toBeUndefined() + expect(result.scope?.folderIds).toBeUndefined() }) }) @@ -84,7 +84,7 @@ describe('either path spelling resolves', () => { it('still tells a slash in a name from a level separator', () => { expect([ - ...(resolveFolderIdsForPaths(canonical, ['/Reports/Q3%2FQ4']).folderIds ?? []), + ...(resolveFolderIdsForPaths(canonical, ['/Reports/Q3%2FQ4']).scope?.folderIds ?? []), ]).toEqual(['slashy']) }) }) @@ -116,16 +116,27 @@ describe('isFileInFolderScope', () => { */ describe('the workspace root', () => { it('selects files that carry no folder id', () => { - const result = resolveFolderIdsForPaths(folders, ['/']) + const result = resolveFolderIdsForPaths(folders, ['/'], { includeSubfolders: false }) expect(result.missingPath).toBeUndefined() - expect(result.includeRootItems).toBe(true) + expect(result.scope?.includeRootItems).toBe(true) + }) + + it('with its subfolders is the whole workspace, not a list of every folder', () => { + const result = resolveFolderIdsForPaths(folders, ['/', '/Reports']) + + expect(result.missingPath).toBeUndefined() + expect(result.scope).toBeUndefined() + }) + + it('still reports a missing path listed beside it', () => { + expect(resolveFolderIdsForPaths(folders, ['/', '/Nope']).missingPath).toBe('/Nope') }) it('is not implied by an ordinary folder scope', () => { const result = resolveFolderIdsForPaths(folders, ['/Reports']) - expect(result.includeRootItems).toBe(false) + expect(result.scope?.includeRootItems).toBe(false) }) }) @@ -138,8 +149,8 @@ describe('isFileInFolderScope and the root', () => { it('agrees with the id resolver for the same scope', () => { const shallowRoot = resolveFolderIdsForPaths(folders, ['/'], { includeSubfolders: false }) - expect(shallowRoot.includeRootItems).toBe(true) - expect([...(shallowRoot.folderIds ?? [])]).toEqual([]) + expect(shallowRoot.scope?.includeRootItems).toBe(true) + expect([...(shallowRoot.scope?.folderIds ?? [])]).toEqual([]) expect(isFileInFolderScope('Reports', '/', { includeSubfolders: false })).toBe(false) }) }) diff --git a/apps/sim/lib/workspace-files/folder-path-selection.ts b/apps/sim/lib/workspace-files/folder-path-selection.ts index a271644ae3f..399af5fb998 100644 --- a/apps/sim/lib/workspace-files/folder-path-selection.ts +++ b/apps/sim/lib/workspace-files/folder-path-selection.ts @@ -15,9 +15,14 @@ export interface SelectableFolder { path: string } +/** + * A resolved selection. `scope` is `undefined` when the selection covers every + * file in the workspace (the root with its subfolders), matching how the query + * builders spell "no folder filter". + */ export type FolderPathSelection = - | (FolderIdScope & { missingPath?: undefined }) - | { folderIds?: undefined; includeRootItems?: undefined; missingPath: string } + | { scope: FolderIdScope | undefined; missingPath?: undefined } + | { scope?: undefined; missingPath: string } /** * Resolves canonical folder paths to the folder ids a run should read from. @@ -38,7 +43,9 @@ export type FolderPathSelection = * in `folderIds`, because the root is the *absence* of a folder id: a file at * the root carries `null`, so there is no id to match. Encoding it as a * sentinel string in a set of real ids would survive every type check and then - * match nothing the first time the set reached a SQL `in (...)`. + * match nothing the first time the set reached a SQL `in (...)`. The root with + * its subfolders is every file, so it resolves to no scope rather than to a list + * of every folder id. */ export function resolveFolderIdsForPaths( folders: readonly SelectableFolder[], @@ -49,6 +56,7 @@ export function resolveFolderIdsForPaths( const maxDepth = includeSubfolders ? undefined : 0 const folderIds = new Set() let includeRootItems = false + let coversWorkspace = false for (const folderPath of folderPaths) { const segments = parseFolderPath(folderPath) @@ -60,9 +68,7 @@ export function resolveFolderIdsForPaths( */ if (segments.length === 0) { includeRootItems = true - if (includeSubfolders) { - for (const folder of folders) folderIds.add(folder.id) - } + if (includeSubfolders) coversWorkspace = true continue } @@ -81,7 +87,7 @@ export function resolveFolderIdsForPaths( } } - return { folderIds, includeRootItems } + return { scope: coversWorkspace ? undefined : { folderIds, includeRootItems } } } /** diff --git a/apps/sim/lib/workspace-files/locks.ts b/apps/sim/lib/workspace-files/locks.ts index 5ff62dfad40..ee4ec528be1 100644 --- a/apps/sim/lib/workspace-files/locks.ts +++ b/apps/sim/lib/workspace-files/locks.ts @@ -2,7 +2,7 @@ import type { DbTransaction } from '@/lib/db/types' import { acquireFolderMutationLock } from '@/lib/folders/locks' import type { EditableFileOwner } from '@/lib/workspace-files/ownership' -/** Locks owner directories in a stable order after lifecycle and billing locks are held. */ +/** Locks owner directories in a stable order after lifecycle and owner storage locks are held. */ export async function lockFileDirectories( tx: DbTransaction, owners: readonly EditableFileOwner[] diff --git a/apps/sim/lib/workspace-files/query-scope.ts b/apps/sim/lib/workspace-files/query-scope.ts index 4470f572ce6..428916a747e 100644 --- a/apps/sim/lib/workspace-files/query-scope.ts +++ b/apps/sim/lib/workspace-files/query-scope.ts @@ -1,5 +1,6 @@ import { workspaceFiles } from '@sim/db/schema' -import { eq, inArray, isNull } from 'drizzle-orm' +import { eq, inArray, isNull, or, type SQL, sql } from 'drizzle-orm' +import type { FolderIdScope } from '@/lib/folders/scope' import { OWNED_FILE_CONTEXTS } from '@/lib/workspace-files/owned-files' /** Durable files available to workspace resource pickers, reference mappings, and fork copies. */ @@ -16,3 +17,21 @@ export const contentWritableWorkspaceFileContextCondition = inArray(workspaceFil 'workspace', ...OWNED_FILE_CONTEXTS, ]) + +/** + * The `workspaceFiles` predicate for a resolved folder scope, or none when the + * scope is absent. A root scope selects the files that carry no folder id, so + * the two halves are OR-ed. An empty scope selects nothing: every path resolved + * to a folder holding no subtree, and answering it unrestricted would widen it + * to the whole workspace. + */ +export function workspaceFileFolderScopeCondition( + scope: FolderIdScope | undefined +): SQL | undefined { + if (!scope) return undefined + const ids = [...scope.folderIds] + const inScope = ids.length > 0 ? inArray(workspaceFiles.folderId, ids) : undefined + const atRoot = scope.includeRootItems ? isNull(workspaceFiles.folderId) : undefined + if (inScope && atRoot) return or(inScope, atRoot) + return inScope ?? atRoot ?? sql`false` +} diff --git a/apps/sim/lib/workspace-files/resolve-folder-scope.ts b/apps/sim/lib/workspace-files/resolve-folder-scope.ts index 9e17a5300e5..53cd8907f55 100644 --- a/apps/sim/lib/workspace-files/resolve-folder-scope.ts +++ b/apps/sim/lib/workspace-files/resolve-folder-scope.ts @@ -20,14 +20,15 @@ import { resolveFolderIdsForPaths } from '@/lib/workspace-files/folder-path-sele * * Folders only. Callers that need the files filter their own listing against * the result; the search path pushes it into SQL instead, which is why the - * folder half is resolved separately from the file half. + * folder half is resolved separately from the file half. Returns `undefined` + * when the paths cover the whole workspace. */ export async function resolveWorkspaceFolderScope(args: { principal: Principal workspaceId: string folderPaths: readonly string[] includeSubfolders: boolean | undefined -}): Promise { +}): Promise { const { folders } = await listWorkspaceFileFoldersOperation.execute({ principal: args.principal, input: { workspaceId: args.workspaceId }, @@ -45,5 +46,5 @@ export async function resolveWorkspaceFolderScope(args: { throw new OrchestrationError('not_found', `Folder not found: ${selection.missingPath}`) } - return { folderIds: selection.folderIds, includeRootItems: selection.includeRootItems } + return selection.scope } diff --git a/apps/sim/lib/workspace-files/search/delivery.ts b/apps/sim/lib/workspace-files/search/delivery.ts index b4d19e18cc8..8efb27f55b2 100644 --- a/apps/sim/lib/workspace-files/search/delivery.ts +++ b/apps/sim/lib/workspace-files/search/delivery.ts @@ -41,7 +41,7 @@ export function resolveFileSearchFolderScope( ) if (selection.missingPath !== undefined) throw new OrchestrationError('not_found', `Folder not found: ${selection.missingPath}`) - return { folderIds: selection.folderIds, includeRootItems: selection.includeRootItems } + return selection.scope } /** diff --git a/apps/sim/lib/workspace-files/search/repository.ts b/apps/sim/lib/workspace-files/search/repository.ts index 6d50fcd6c8b..74a7c2d49c0 100644 --- a/apps/sim/lib/workspace-files/search/repository.ts +++ b/apps/sim/lib/workspace-files/search/repository.ts @@ -1,11 +1,12 @@ import { dbFor } from '@sim/db' import { fileSearchDependency, workspaceFileSearchRevision, workspaceFiles } from '@sim/db/schema' import { getPostgresErrorCode } from '@sim/utils/errors' -import { and, eq, inArray, isNull, or, type SQL, type SQLWrapper, sql } from 'drizzle-orm' +import { and, eq, inArray, isNull, type SQLWrapper, sql } from 'drizzle-orm' import type { DbTransaction } from '@/lib/db/types' import type { FolderIdScope } from '@/lib/folders/scope' import type { WorkspaceFileSecretProvenanceIdentity } from '@/lib/uploads/contexts/workspace/workspace-file-secret-provenance' import type { EditableFileOwner } from '@/lib/workspace-files/ownership' +import { workspaceFileFolderScopeCondition } from '@/lib/workspace-files/query-scope' import { fileSearchAdmission } from '@/lib/workspace-files/search/admission' import { probeFileSearchCandidates, @@ -150,22 +151,6 @@ function toPreviewRange( return alignToCodePoints(content, { start, end: units }) } -/** - * The `workspaceFiles` predicate for a resolved folder scope. - * - * A root scope selects the files that carry no folder id, so the two halves are - * OR-ed rather than folded into one list. An empty scope selects nothing: it - * means every path resolved to a folder holding no subtree, and answering it - * with an unrestricted search would leak the whole workspace. - */ -function buildFolderPredicate(scope: FolderIdScope): SQL | undefined { - const ids = [...scope.folderIds] - const inScope = ids.length > 0 ? inArray(workspaceFiles.folderId, ids) : undefined - const atRoot = scope.includeRootItems ? isNull(workspaceFiles.folderId) : undefined - if (inScope && atRoot) return or(inScope, atRoot) - return inScope ?? atRoot ?? sql`false` -} - type SearchLine = { lineNumber: number content: string @@ -238,14 +223,8 @@ export async function searchFileIndex({ ) } - /** - * The chunk table carries no folder id, so a scope has to travel through - * the `workspaceFiles` join both queries already make. A scope that resolved - * to nothing must match nothing: `inArray` with an empty list would be a - * SQL error, and omitting the predicate would silently search everything, - * which for a per-user folder tree is a cross-user read. - */ - const folderPredicate = folderScope ? buildFolderPredicate(folderScope) : undefined + /** The chunk table carries no folder id, so a scope travels through the `workspaceFiles` join. */ + const folderPredicate = workspaceFileFolderScopeCondition(folderScope) return fileSearchAdmission.run( `${owner.entityType}:${owner.entityId}`, diff --git a/apps/sim/lib/workspaces/access/shared-resource-handoff.integration.ts b/apps/sim/lib/workspaces/access/shared-resource-handoff.integration.ts index 88be3d41a65..f91f1483927 100644 --- a/apps/sim/lib/workspaces/access/shared-resource-handoff.integration.ts +++ b/apps/sim/lib/workspaces/access/shared-resource-handoff.integration.ts @@ -309,7 +309,7 @@ describe('shared resource retention across workspace departure and account erasu expect(result).toEqual({ revoked: false, reason: 'unresolved-workflows', - unresolvedWorkflows: [fixture.workspaceId], + unresolvedWorkspaceIds: [fixture.workspaceId], }) expect( await db diff --git a/apps/sim/lib/workspaces/access/workspace-access.ts b/apps/sim/lib/workspaces/access/workspace-access.ts index e9a2ed41053..8c4da6b7678 100644 --- a/apps/sim/lib/workspaces/access/workspace-access.ts +++ b/apps/sim/lib/workspaces/access/workspace-access.ts @@ -7,10 +7,7 @@ import type { DbOrTx, DbTransaction } from '@/lib/db/types' import { lockProjectsForResourceDepartureTx } from '@/lib/projects/resource-handoff' import { removeWorkspaceSkillMembershipsTx } from '@/lib/skills/access' import { reassignSharedResourceOwnershipForWorkspaceMemberRemovalTx } from '@/lib/workspaces/resource-handoff' -import { - transferWorkspaceOwnershipToBilledAccountForMemberRemovalTx, - WorkspaceBillingAccountRemovalError, -} from '@/lib/workspaces/utils' +import { transferWorkspaceOwnershipToBilledAccountForMemberRemovalTx } from '@/lib/workspaces/utils' /** * Workspace access as a shared domain primitive. @@ -119,9 +116,7 @@ export type RevokeWorkspaceAccessResult = /** `ownershipTransferred` is true when the departing user owned the workspace and it moved to the billed account. */ | { revoked: true; ownershipTransferred: boolean } /** Workspace IDs whose shared resources or ownership lack a successor; access remains unchanged. */ - | { revoked: false; reason: 'unresolved-workflows'; unresolvedWorkflows: string[] } - /** The user owns the workspace and it has no billed account to hand it to. */ - | { revoked: false; reason: 'workspace-owner-without-successor' } + | { revoked: false; reason: 'unresolved-workflows'; unresolvedWorkspaceIds: string[] } /** The requested membership no longer exists; a replacement grant must remain untouched. */ | { revoked: false; reason: 'membership-changed' } @@ -162,23 +157,16 @@ export async function revokeWorkspaceAccessTx( return { revoked: false, reason: 'unresolved-workflows', - unresolvedWorkflows: reassignment.unresolved, + unresolvedWorkspaceIds: reassignment.unresolved, } } - let ownershipTransferred: boolean - try { - ownershipTransferred = await transferWorkspaceOwnershipToBilledAccountForMemberRemovalTx({ - tx, - workspaceId: params.workspaceId, - departingUserId: params.userId, - }) - } catch (error) { - if (error instanceof WorkspaceBillingAccountRemovalError) { - return { revoked: false, reason: 'workspace-owner-without-successor' } - } - throw error - } + // The handoff above already refused an owner without a successor, under the workspace row lock. + const ownershipTransferred = await transferWorkspaceOwnershipToBilledAccountForMemberRemovalTx({ + tx, + workspaceId: params.workspaceId, + departingUserId: params.userId, + }) await tx .delete(permissions) diff --git a/apps/sim/lib/workspaces/admin-move-source-impact.ts b/apps/sim/lib/workspaces/admin-move-source-impact.ts index 6e0d74aad00..6b71eb2ab86 100644 --- a/apps/sim/lib/workspaces/admin-move-source-impact.ts +++ b/apps/sim/lib/workspaces/admin-move-source-impact.ts @@ -15,11 +15,14 @@ import { user, userStats, workflow, + workflowBlocks, + workflowDeploymentVersion, workspace, workspaceBYOKKeys, workspaceEnvironment, } from '@sim/db/schema' -import { and, eq, inArray, isNull, ne, or } from 'drizzle-orm' +import { escapeLikePattern } from '@sim/utils/string' +import { and, eq, inArray, isNull, ne, or, sql } from 'drizzle-orm' import { alias } from 'drizzle-orm/pg-core' import type { OrganizationSettingsSection } from '@/components/settings/navigation' import { isSubscriptionBackedEntitlement } from '@/lib/billing/core/subscription' @@ -27,6 +30,7 @@ import { isEnterprise } from '@/lib/billing/plan-helpers' import { USABLE_SUBSCRIPTION_STATUSES } from '@/lib/billing/subscriptions/utils' import type { DbOrTx } from '@/lib/db/types' import { getCustomBlockUsageCounts } from '@/lib/workflows/custom-blocks/operations' +import { CUSTOM_BLOCK_TYPE_PREFIX } from '@/blocks/custom/build-config' /** * Everything the source organization loses when a workspace leaves it, plus the @@ -305,6 +309,98 @@ export async function findUnpublishableCustomBlocks( return { items, total: rows.length } } +export interface InboundSourceOrgCustomBlock { + id: string + type: string + name: string + /** Same meaning as {@link UnpublishableCustomBlock.movingWorkspaceUsage}. */ + movingWorkspaceUsage: { live: number; deployed: number } +} + +/** + * Source-org custom blocks published from a workflow elsewhere in the source org but placed in + * the moving workspace's workflows. They stay published, yet `getCustomBlockAuthority` resolves + * a placement through the consumer's organization, so after the move these placements fail at + * run time. Blocks bound inside the moving workspace are excluded: those are unpublished and + * reported by {@link findUnpublishableCustomBlocks}. + * + * Placement predicates mirror `getCustomBlockUsageCounts` (live editor rows and active + * deployment snapshots of non-archived workflows, prefiltered with an escaped LIKE), batched + * across every block so preflight pays two queries instead of two per block. Preflight only. + */ +export async function findInboundSourceOrgCustomBlockUsage( + workspaceId: string, + sourceOrganizationId: string +): Promise { + const boundWorkflow = alias(workflow, 'bound_workflow') + const placedBlock = { + id: customBlock.id, + type: customBlock.type, + name: customBlock.name, + workflowId: workflow.id, + } + const movingWorkflow = and(eq(workflow.workspaceId, workspaceId), isNull(workflow.archivedAt)) + const boundElsewhereInSourceOrg = and( + eq(customBlock.organizationId, sourceOrganizationId), + ne(boundWorkflow.workspaceId, workspaceId) + ) + const prefixPattern = `%${escapeLikePattern(CUSTOM_BLOCK_TYPE_PREFIX)}%` + + const [liveRows, deployedRows] = await Promise.all([ + db + .selectDistinct(placedBlock) + .from(workflowBlocks) + .innerJoin(workflow, eq(workflow.id, workflowBlocks.workflowId)) + .innerJoin(customBlock, eq(customBlock.type, workflowBlocks.type)) + .innerJoin(boundWorkflow, eq(boundWorkflow.id, customBlock.workflowId)) + .where(and(movingWorkflow, boundElsewhereInSourceOrg)), + db + .selectDistinct(placedBlock) + .from(workflowDeploymentVersion) + .innerJoin(workflow, eq(workflow.id, workflowDeploymentVersion.workflowId)) + .crossJoinLateral( + sql`jsonb_each((${workflowDeploymentVersion.state})::jsonb -> 'blocks') AS placed_block` + ) + .innerJoin(customBlock, sql`${customBlock.type} = placed_block.value ->> 'type'`) + .innerJoin(boundWorkflow, eq(boundWorkflow.id, customBlock.workflowId)) + .where( + and( + eq(workflowDeploymentVersion.isActive, true), + eq(workflow.isDeployed, true), + movingWorkflow, + boundElsewhereInSourceOrg, + sql`${workflowDeploymentVersion.state}::text LIKE ${prefixPattern} ESCAPE '\\'` + ) + ), + ]) + + const byBlock = new Map< + string, + { id: string; type: string; name: string; using: Set; deployed: number } + >() + for (const [rows, deployed] of [ + [liveRows, false], + [deployedRows, true], + ] as const) { + for (const row of rows) { + const entry = byBlock.get(row.id) ?? { + id: row.id, + type: row.type, + name: row.name, + using: new Set(), + deployed: 0, + } + entry.using.add(row.workflowId) + if (deployed) entry.deployed += 1 + byBlock.set(row.id, entry) + } + } + return [...byBlock.values()].map(({ using, deployed, ...block }) => ({ + ...block, + movingWorkspaceUsage: { live: using.size, deployed }, + })) +} + export interface WorkspaceMoveCredentialSummaryRow { items: Array<{ id: string diff --git a/apps/sim/lib/workspaces/admin-move.integration.ts b/apps/sim/lib/workspaces/admin-move.integration.ts new file mode 100644 index 00000000000..9f72f0db116 --- /dev/null +++ b/apps/sim/lib/workspaces/admin-move.integration.ts @@ -0,0 +1,314 @@ +import { db } from '@sim/db' +import { + customBlock, + member, + organization, + outboxEvent, + permissions, + user, + userStats, + workflow, + workflowBlocks, + workflowDeploymentVersion, + workspace, +} from '@sim/db/schema' +import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' +import { generateId, generateShortId } from '@sim/utils/id' +import { eq, inArray } from 'drizzle-orm' +import { afterAll, describe, expect, it } from 'vitest' +import { + getWorkspaceMoveOperation, + getWorkspaceMovePreflight, + moveWorkspaceToOrganization, + type WorkspaceMoveError, +} from '@/lib/workspaces/admin-move' +import { WORKSPACE_MODE } from '@/lib/workspaces/policy' + +readTestDatabaseUrl() + +const userIds: string[] = [] +const organizationIds: string[] = [] +const workspaceIds: string[] = [] +const operationIds: string[] = [] + +async function createUser() { + const id = generateId() + userIds.push(id) + await db.insert(user).values({ + id, + name: 'Move fixture', + email: `${id}@example.test`, + emailVerified: true, + createdAt: new Date(), + updatedAt: new Date(), + }) + await db.insert(userStats).values({ id: generateId(), userId: id }) + return id +} + +async function createOrganization() { + const ownerId = await createUser() + const id = generateId() + organizationIds.push(id) + await db.insert(organization).values({ id, name: `Org ${id}`, slug: id, createdAt: new Date() }) + await db + .insert(member) + .values({ id: generateId(), organizationId: id, userId: ownerId, role: 'owner' }) + return { id, ownerId } +} + +async function createWorkspace(params: { + ownerId: string + organization?: { id: string; ownerId: string } + forkedFromWorkspaceId?: string +}) { + const id = generateId() + workspaceIds.push(id) + await insertWorkspaceFixture(db, { + id, + name: `Workspace ${id}`, + ownerId: params.ownerId, + organizationId: params.organization?.id ?? null, + workspaceMode: params.organization ? WORKSPACE_MODE.ORGANIZATION : WORKSPACE_MODE.PERSONAL, + billedAccountUserId: params.organization?.ownerId ?? params.ownerId, + forkedFromWorkspaceId: params.forkedFromWorkspaceId ?? null, + }) + await db.insert(permissions).values({ + id: generateId(), + userId: params.ownerId, + entityType: 'workspace', + entityId: id, + permissionType: 'admin', + }) + return id +} + +async function createWorkflow(workspaceId: string, userId: string, isDeployed = false) { + const id = generateId() + const now = new Date() + await db.insert(workflow).values({ + id, + userId, + workspaceId, + name: `Move fixture workflow ${id}`, + lastSynced: now, + createdAt: now, + updatedAt: now, + isDeployed, + }) + return id +} + +async function publishCustomBlock(organizationId: string, workflowId: string) { + const id = generateId() + const type = `custom_block_${generateShortId(10).toLowerCase()}` + await db + .insert(customBlock) + .values({ id, organizationId, workflowId, type, name: `Block ${type}` }) + return { id, type } +} + +async function placeBlock(workflowId: string, type: string) { + await db.insert(workflowBlocks).values({ + id: generateId(), + workflowId, + type, + name: 'Placed custom block', + positionX: '0', + positionY: '0', + }) +} + +function newOperationId() { + const id = generateId() + operationIds.push(id) + return id +} + +async function readWorkspace(id: string) { + const [row] = await db + .select({ + organizationId: workspace.organizationId, + workspaceMode: workspace.workspaceMode, + billedAccountUserId: workspace.billedAccountUserId, + }) + .from(workspace) + .where(eq(workspace.id, id)) + return row +} + +afterAll(async () => { + if (operationIds.length) await db.delete(outboxEvent).where(inArray(outboxEvent.id, operationIds)) + if (workspaceIds.length) { + await db.delete(permissions).where(inArray(permissions.entityId, workspaceIds)) + await deleteWorkspaceFixture(db, inArray(workspace.id, workspaceIds)) + } + if (organizationIds.length) + await db.delete(organization).where(inArray(organization.id, organizationIds)) + if (userIds.length) await db.delete(user).where(inArray(user.id, userIds)) +}) + +describe('moveWorkspaceToOrganization against PostgreSQL', () => { + it('moves a personal workspace into an organization and replays the confirmed operation', async () => { + const ownerId = await createUser() + const destination = await createOrganization() + const workspaceId = await createWorkspace({ ownerId }) + const operationId = newOperationId() + const move = () => + moveWorkspaceToOrganization({ + workspaceId, + destinationOrganizationId: destination.id, + adminEmail: 'admin@example.test', + expectedOwnerId: ownerId, + auditOperationId: operationId, + operationCorrelationId: operationId, + durableOperationId: operationId, + }) + + const applied = await move() + + expect(applied.workspace).toMatchObject({ id: workspaceId, organizationId: destination.id }) + expect(applied.sourceOrganization).toBeNull() + expect(await readWorkspace(workspaceId)).toEqual({ + organizationId: destination.id, + workspaceMode: WORKSPACE_MODE.ORGANIZATION, + billedAccountUserId: destination.ownerId, + }) + const destinationOwnerGrant = await db + .select({ permissionType: permissions.permissionType }) + .from(permissions) + .where(eq(permissions.userId, destination.ownerId)) + expect(destinationOwnerGrant).toEqual([{ permissionType: 'admin' }]) + const [operation] = await db + .select({ status: outboxEvent.status, payload: outboxEvent.payload }) + .from(outboxEvent) + .where(eq(outboxEvent.id, operationId)) + expect(operation).toMatchObject({ + status: 'completed', + payload: { audit: { sourceOrganizationId: null } }, + }) + + const replayed = await move() + expect(replayed.workspace).toMatchObject({ id: workspaceId, organizationId: destination.id }) + expect( + await db + .select({ id: outboxEvent.id }) + .from(outboxEvent) + .where(eq(outboxEvent.id, operationId)) + ).toHaveLength(1) + + await expect( + moveWorkspaceToOrganization({ + workspaceId, + destinationOrganizationId: destination.id, + adminEmail: 'admin@example.test', + expectedOwnerId: ownerId, + durableOperationId: newOperationId(), + }) + ).rejects.toMatchObject>({ code: 'already-organization-workspace' }) + + const reloaded = await getWorkspaceMoveOperation( + workspaceId, + destination.id, + ownerId, + operationId + ) + expect(reloaded).toMatchObject({ operationId, sourceOrganization: null }) + }) + + it('moves between organizations, unpublishing only blocks bound to the moving workspace', async () => { + const source = await createOrganization() + const destination = await createOrganization() + const workspaceId = await createWorkspace({ ownerId: source.ownerId, organization: source }) + const siblingId = await createWorkspace({ ownerId: source.ownerId, organization: source }) + const movingWorkflow = await createWorkflow(workspaceId, source.ownerId) + const deployedMovingWorkflow = await createWorkflow(workspaceId, source.ownerId, true) + const siblingWorkflow = await createWorkflow(siblingId, source.ownerId) + const outbound = await publishCustomBlock(source.id, movingWorkflow) + const inbound = await publishCustomBlock(source.id, siblingWorkflow) + await placeBlock(movingWorkflow, outbound.type) + await placeBlock(movingWorkflow, inbound.type) + await db.insert(workflowDeploymentVersion).values({ + id: generateId(), + workflowId: deployedMovingWorkflow, + version: 1, + isActive: true, + state: { blocks: { placed: { id: 'placed', type: inbound.type } }, edges: [] }, + }) + + const preflight = await getWorkspaceMovePreflight(workspaceId, destination.id) + + expect(preflight.blockers).toEqual([]) + expect(preflight.sourceOrganizationImpact.unpublishedCustomBlocks).toEqual([ + expect.objectContaining({ id: outbound.id }), + ]) + expect(preflight.sourceOrganizationImpact.inboundCustomBlocks).toEqual([ + expect.objectContaining({ id: inbound.id, movingWorkspaceUsage: { live: 2, deployed: 1 } }), + ]) + expect(preflight.notices).toContainEqual(expect.stringContaining('will stop resolving')) + + const operationId = newOperationId() + const move = () => + moveWorkspaceToOrganization({ + workspaceId, + destinationOrganizationId: destination.id, + adminEmail: 'admin@example.test', + auditOperationId: operationId, + operationCorrelationId: operationId, + durableOperationId: operationId, + }) + + const applied = await move() + + expect(applied.sourceOrganization?.id).toBe(source.id) + expect(applied.sourceOrganizationImpact.unpublishedCustomBlocks.map(({ id }) => id)).toEqual([ + outbound.id, + ]) + expect(await readWorkspace(workspaceId)).toEqual({ + organizationId: destination.id, + workspaceMode: WORKSPACE_MODE.ORGANIZATION, + billedAccountUserId: destination.ownerId, + }) + const remainingBlocks = await db + .select({ id: customBlock.id }) + .from(customBlock) + .where(inArray(customBlock.id, [outbound.id, inbound.id])) + expect(remainingBlocks).toEqual([{ id: inbound.id }]) + + const replayed = await move() + expect(replayed.sourceOrganization?.id).toBe(source.id) + expect(replayed.sourceOrganizationImpact.unpublishedCustomBlocks.map(({ id }) => id)).toEqual([ + outbound.id, + ]) + }) + + it('refuses a move that would split a fork lineage across organizations', async () => { + const source = await createOrganization() + const destination = await createOrganization() + const parentId = await createWorkspace({ ownerId: source.ownerId, organization: source }) + const childId = await createWorkspace({ + ownerId: source.ownerId, + organization: source, + forkedFromWorkspaceId: parentId, + }) + const operationId = newOperationId() + + await expect( + moveWorkspaceToOrganization({ + workspaceId: childId, + destinationOrganizationId: destination.id, + adminEmail: 'admin@example.test', + durableOperationId: operationId, + }) + ).rejects.toMatchObject>({ code: 'fork-lineage-conflict' }) + + expect(await readWorkspace(childId)).toMatchObject({ organizationId: source.id }) + expect( + await db + .select({ id: outboxEvent.id }) + .from(outboxEvent) + .where(eq(outboxEvent.id, operationId)) + ).toEqual([]) + }) +}) diff --git a/apps/sim/lib/workspaces/admin-move.test.ts b/apps/sim/lib/workspaces/admin-move.test.ts index 92fe2792118..315d11e7287 100644 --- a/apps/sim/lib/workspaces/admin-move.test.ts +++ b/apps/sim/lib/workspaces/admin-move.test.ts @@ -9,15 +9,12 @@ import { workspace, } from '@sim/db/schema' import { dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing' -import { auditMock, auditMockFns } from '@sim/testing/mocks/audit.mock' +import { auditMock } from '@sim/testing/mocks/audit.mock' import { billingPayerTransferMock, billingPayerTransferMockFns, } from '@sim/testing/mocks/billing-payer-transfer.mock' -import { - customBlockOperationsMock, - customBlockOperationsMockFns, -} from '@sim/testing/mocks/custom-block-operations.mock' +import { customBlockOperationsMock } from '@sim/testing/mocks/custom-block-operations.mock' import { invitationsCoreMock, invitationsCoreMockFns, @@ -26,12 +23,9 @@ import { invitationsSendMock, invitationsSendMockFns, } from '@sim/testing/mocks/invitations-send.mock' -import { - organizationMembershipMock, - organizationMembershipMockFns, -} from '@sim/testing/mocks/organization-membership.mock' -import { outboxServiceMock, outboxServiceMockFns } from '@sim/testing/mocks/outbox-service.mock' -import { tableBillingMock, tableBillingMockFns } from '@sim/testing/mocks/table-billing.mock' +import { organizationMembershipMock } from '@sim/testing/mocks/organization-membership.mock' +import { outboxServiceMock } from '@sim/testing/mocks/outbox-service.mock' +import { tableBillingMock } from '@sim/testing/mocks/table-billing.mock' import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' import type { WorkspaceMoveError } from '@/lib/workspaces/admin-move' import { @@ -84,14 +78,6 @@ const { mockChangeProjectAndWorkspaceStoragePayersInTx: changeProjectAndWorkspaceStoragePayersInTx, } = billingPayerTransferMockFns -const { mockRecordAudit: recordAudit, mockRecordAuditOnce: recordAuditOnce } = auditMockFns -const { mockAcquireOrganizationMutationLock: acquireOrganizationMutationLock } = - organizationMembershipMockFns -const { mockDeleteCustomBlock: deleteCustomBlock } = customBlockOperationsMockFns -const { mockEnqueueOrReschedulePendingOutboxEvent: enqueueOrReschedulePendingOutboxEvent } = - outboxServiceMockFns -const { mockInvalidateWorkspaceTableLimitsCache: invalidateWorkspaceTableLimitsCache } = - tableBillingMockFns const { mockGetInvitationById: getInvitationById, mockIsInvitationExpired: isInvitationExpired } = invitationsCoreMockFns const { mockSendInvitationEmail: sendInvitationEmail } = invitationsSendMockFns @@ -151,6 +137,7 @@ vi.mock('@/lib/workspaces/admin-move-source-impact', () => ({ })), findAttachedPermissionGroups: vi.fn(() => Promise.resolve([])), findCrossOrgForkEdges, + findInboundSourceOrgCustomBlockUsage: vi.fn(() => Promise.resolve([])), findRetainedCollaboratorCaps: vi.fn(() => Promise.resolve([])), findUnpublishableCustomBlocks, findSourceOrgCustomBlocksForWorkspace, @@ -479,72 +466,6 @@ describe('migrated invitation email outbox', () => { }) describe('moveWorkspaceToOrganization retries', () => { - it('returns the existing destination summary without repeating side effects', async () => { - queueMoveSelects(movedWorkspace) - - const result = await moveWorkspaceToOrganization({ - workspaceId: movedWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - }) - - expect(result.workspace).toMatchObject({ - id: movedWorkspace.id, - organizationId: destination.id, - workspaceMode: WORKSPACE_MODE.ORGANIZATION, - }) - expect(enqueueOrReschedulePendingOutboxEvent).not.toHaveBeenCalled() - expect(recordAudit).not.toHaveBeenCalled() - expect(invalidateWorkspaceTableLimitsCache).not.toHaveBeenCalled() - expect(dbChainMockFns.insert).not.toHaveBeenCalled() - expect(dbChainMockFns.update).not.toHaveBeenCalled() - expect(changeProjectAndWorkspaceStoragePayersInTx).not.toHaveBeenCalled() - }) - - it('persists a standalone operation marker atomically with a new move', async () => { - queueMoveSelects(personalWorkspace) - - await moveWorkspaceToOrganization({ - workspaceId: personalWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - expectedOwnerId: personalWorkspace.ownerId, - auditOperationId: 'operation-1', - operationCorrelationId: 'operation-1', - durableOperationId: 'operation-1', - }) - - expect(dbChainMockFns.values.mock.calls.map(([values]) => values)).toContainEqual( - expect.objectContaining({ - id: 'operation-1', - eventType: 'admin.workspace-move-operation', - status: 'completed', - payload: { - request: { - workspaceId: personalWorkspace.id, - destinationOrganizationId: destination.id, - expectedOwnerId: personalWorkspace.ownerId, - }, - audit: { - actor: { id: null, name: 'Admin Panel', email: 'admin@sim.ai' }, - previousBillingOwnerId: personalWorkspace.billedAccountUserId, - newBillingOwnerId: destination.ownerId, - organizationAssignedAt: expect.any(String), - /** - * Persisted so a reload of a completed operation can still name the - * organization the workspace came from — the payer transfer has - * already overwritten `workspace.organizationId` by then. - */ - sourceOrganizationId: null, - /** Persisted so the reload path can replay the source-org audit. */ - unpublishedCustomBlocks: [], - detachedPermissionGroupIds: [], - }, - }, - }) - ) - }) - it('refuses a move that would exceed the locked Enterprise seat capacity', async () => { queueMoveSelects(personalWorkspace) queueTableRows(subscription, [ @@ -576,26 +497,6 @@ describe('moveWorkspaceToOrganization retries', () => { expect(changeProjectAndWorkspaceStoragePayersInTx).not.toHaveBeenCalled() }) - it('does not let a new operation ID claim a workspace moved by another operation', async () => { - queueMoveSelects(movedWorkspace) - - await expect( - moveWorkspaceToOrganization({ - workspaceId: movedWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - expectedOwnerId: movedWorkspace.ownerId, - auditOperationId: 'operation-2', - operationCorrelationId: 'operation-2', - durableOperationId: 'operation-2', - }) - ).rejects.toMatchObject>({ - code: 'already-organization-workspace', - }) - - expect(recordAuditOnce).not.toHaveBeenCalled() - }) - it('reports the workspace credentials when a completed operation is reloaded', async () => { collectWorkspaceCredentialSummary.mockResolvedValueOnce(POPULATED_CREDENTIALS) queueMoveOperationSelects({ @@ -618,53 +519,6 @@ describe('moveWorkspaceToOrganization retries', () => { expect(view.credentials).toEqual(POPULATED_CREDENTIALS) }) - it('locks both organizations in ascending id order, after invitation locks and before the row lock', async () => { - queueMoveSelects(organizationWorkspace) - - await moveWorkspaceToOrganization({ - workspaceId: organizationWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - durableOperationId: 'operation-1', - }) - - const lockedOrganizationIds = acquireOrganizationMutationLock.mock.calls.map( - (call) => call[1] as string - ) - expect(lockedOrganizationIds).toEqual(['org-1', 'org-source']) - const invitationLock = acquireInvitationMutationLocks.mock.invocationCallOrder[0] - const firstOrganizationLock = acquireOrganizationMutationLock.mock.invocationCallOrder[0] - const firstForUpdate = dbChainMockFns.for.mock.invocationCallOrder[0] - expect(firstOrganizationLock).toBeGreaterThan(invitationLock) - expect(firstForUpdate).toBeGreaterThan(firstOrganizationLock) - }) - - it('fences the payer transfer on the source organization it read under the locks', async () => { - queueMoveSelects(organizationWorkspace) - - await moveWorkspaceToOrganization({ - workspaceId: organizationWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - durableOperationId: 'operation-1', - }) - - expect(changeProjectAndWorkspaceStoragePayersInTx).toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ - workspaceChanges: [ - expect.objectContaining({ - organizationId: destination.id, - expectedCurrentPayer: { - organizationId: 'org-source', - billedAccountUserId: organizationWorkspace.billedAccountUserId, - }, - }), - ], - }) - ) - }) - it('re-fences the payer transfer after a SourceOrganizationChangedError retry', async () => { /** * The optimistic pre-transaction organization read decides which @@ -707,50 +561,6 @@ describe('moveWorkspaceToOrganization retries', () => { ) }) - it('unpublishes source-organization custom blocks bound to the moving workspace', async () => { - queueMoveSelects(organizationWorkspace) - findSourceOrgCustomBlocksForWorkspace.mockResolvedValueOnce([ - { id: 'block-1', type: 'custom_block_1', name: 'Reporter' }, - ] as never) - - await moveWorkspaceToOrganization({ - workspaceId: organizationWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - durableOperationId: 'operation-1', - }) - - expect(deleteCustomBlock).toHaveBeenCalledWith('block-1', expect.anything()) - expect(cleanupSourceOrganizationArtifactsTx).toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ sourceOrganizationId: 'org-source' }) - ) - }) - - it('refuses a cross-organization fork edge without mutating anything', async () => { - queueMoveSelects(organizationWorkspace) - findCrossOrgForkEdges.mockResolvedValueOnce([ - { - workspaceId: 'parent-1', - name: 'Parent', - organizationId: 'org-source', - direction: 'parent', - }, - ] as never) - - await expect( - moveWorkspaceToOrganization({ - workspaceId: organizationWorkspace.id, - destinationOrganizationId: destination.id, - adminEmail: 'admin@sim.ai', - durableOperationId: 'operation-1', - }) - ).rejects.toMatchObject>({ code: 'fork-lineage-conflict' }) - - expect(changeProjectAndWorkspaceStoragePayersInTx).not.toHaveBeenCalled() - expect(deleteCustomBlock).not.toHaveBeenCalled() - }) - it('refuses an organization source without a durable operation id', async () => { queueMoveSelects(organizationWorkspace) @@ -775,11 +585,7 @@ describe('moveWorkspaceToOrganization retries', () => { it('refuses an entitlement downgrade without mutating anything', async () => { queueMoveSelects(organizationWorkspace) - /** - * Not `...Once`: the resolver runs twice — once before the transaction for - * preflight reporting, and again under the locks as the fence. - */ - resolveMoveEntitlements.mockResolvedValue({ + resolveMoveEntitlements.mockResolvedValueOnce({ sourceIsEnterprise: true, destinationIsEnterprise: false, capabilitiesLost: ['permission groups', 'workspace forking'], diff --git a/apps/sim/lib/workspaces/admin-move.ts b/apps/sim/lib/workspaces/admin-move.ts index de3b6cbd232..97d4f0a6847 100644 --- a/apps/sim/lib/workspaces/admin-move.ts +++ b/apps/sim/lib/workspaces/admin-move.ts @@ -53,10 +53,12 @@ import { countRetentionRulesForWorkspace, findAttachedPermissionGroups, findCrossOrgForkEdges, + findInboundSourceOrgCustomBlockUsage, findRetainedCollaboratorCaps, findSourceOrgCustomBlocksForWorkspace, findUnpublishableCustomBlocks, getSourceOrganization, + type InboundSourceOrgCustomBlock, resolveMoveEntitlements, willBrandingChange, } from '@/lib/workspaces/admin-move-source-impact' @@ -115,7 +117,6 @@ export class WorkspaceMoveError extends Error { | 'seat-capacity-exceeded' | 'invitation-volume-exceeded' | 'source-equals-destination' - | 'move-operation-parameter-mismatch' | 'destination-entitlement-downgrade' | 'fork-lineage-conflict' | 'pending-invitations-present' @@ -175,6 +176,11 @@ export interface WorkspaceMoveSourceImpact { movingWorkspaceUsage: { live: number; deployed: number } sourceOrgElsewhereUsage: { live: number; deployed: number } }> + /** + * See {@link findInboundSourceOrgCustomBlockUsage}. Advisory, never blocking; a preflight + * projection, so empty on an applied or replayed move. + */ + inboundCustomBlocks: InboundSourceOrgCustomBlock[] /** Fork edges crossing the org boundary. Non-empty blocks the move. */ blockingForkEdges: Array<{ workspaceId: string @@ -195,6 +201,7 @@ export interface WorkspaceMoveSourceImpact { /** Rows omitted to stay inside the contract's array bounds, or `null`. */ truncated: { customBlocks: number + inboundCustomBlocks: number permissionGroups: number collaboratorCaps: number forkEdges: number @@ -460,28 +467,61 @@ async function resolveRecordedSourceOrganization( } /** - * The truncation record for an applied summary, merging what the move's own - * lists dropped with what the credential summary dropped — the same merge - * preflight performs, so a partial applied review is never presented as a - * complete one. + * What a completed move did to the source organization, for its applied and replayed + * summaries. Usage counts are zero rather than measured: they describe how much breaks in the + * source organization, the reads that produce them are not transaction-safe, and preflight + * carries the real numbers. Lists are bounded like preflight's, with the same truncation merge, + * so a partial review is never presented as a complete one. */ -function buildAppliedTruncation(params: { - unpublishedCustomBlocks: number - detachedPermissionGroups: number +function buildAppliedSourceImpact(params: { + unpublishedCustomBlocks: Array<{ id: string; type: string; name: string }> + detachedPermissionGroupIds: string[] credentials: WorkspaceMoveCredentialSummary -}): WorkspaceMoveSourceImpact['truncated'] { +}): WorkspaceMoveSourceImpact { + const blocks = boundList(params.unpublishedCustomBlocks, PREFLIGHT_LIST_LIMITS.customBlocks) + const groups = boundList( + params.detachedPermissionGroupIds, + PREFLIGHT_LIST_LIMITS.permissionGroups + ) const truncated = { - customBlocks: Math.max(params.unpublishedCustomBlocks - PREFLIGHT_LIST_LIMITS.customBlocks, 0), - permissionGroups: Math.max( - params.detachedPermissionGroups - PREFLIGHT_LIST_LIMITS.permissionGroups, - 0 - ), + customBlocks: blocks.dropped, + inboundCustomBlocks: 0, + permissionGroups: groups.dropped, collaboratorCaps: 0, forkEdges: 0, credentials: params.credentials.truncatedCredentials, environmentVariableKeys: params.credentials.truncatedEnvironmentVariableKeys, } - return Object.values(truncated).some((dropped) => dropped > 0) ? truncated : null + return { + unpublishedCustomBlocks: blocks.items.map(({ id, type, name }) => ({ + id, + type, + name, + movingWorkspaceUsage: { live: 0, deployed: 0 }, + sourceOrgElsewhereUsage: { live: 0, deployed: 0 }, + })), + detachedPermissionGroups: groups.items.map((permissionGroupId) => ({ + permissionGroupId, + name: '', + })), + /** The remaining fields project the pre-move state, which is gone once the move lands. */ + inboundCustomBlocks: [], + blockingForkEdges: [], + strippedRetentionRules: { piiRedactionRules: 0, retentionOverrides: 0 }, + retainedCollaboratorCaps: [], + brandingChanges: false, + truncated: Object.values(truncated).some((dropped) => dropped > 0) ? truncated : null, + } +} + +/** + * A completed move passed the downgrade fence, so it lost no capabilities; the plan flags it + * was checked against are not recorded, so a replay cannot report them. + */ +const REPLAYED_MOVE_ENTITLEMENTS: WorkspaceMoveEntitlements = { + sourceIsEnterprise: false, + destinationIsEnterprise: false, + capabilitiesLost: [], } function workspaceMoveOperationMatches( @@ -550,7 +590,7 @@ export async function searchWorkspaceMoveCandidates(search: string, limit = 20, .from(workspace) .innerJoin(user, eq(user.id, workspace.ownerId)) .leftJoin(organization, eq(organization.id, workspace.organizationId)) - .where(and(or(eq(workspace.id, query), ilike(workspace.name, `%${query}%`)), undefined)) + .where(or(eq(workspace.id, query), ilike(workspace.name, `%${query}%`))) .orderBy(asc(workspace.name)) .limit(Math.min(Math.max(limit, 1), 50)) .offset(Math.max(offset, 0)) @@ -689,6 +729,7 @@ export async function getWorkspaceMovePreflight( */ const droppedTotal = (sourceImpact.truncated?.customBlocks ?? 0) + + (sourceImpact.truncated?.inboundCustomBlocks ?? 0) + (sourceImpact.truncated?.permissionGroups ?? 0) + (sourceImpact.truncated?.collaboratorCaps ?? 0) + boundedForkEdges.dropped + @@ -698,6 +739,7 @@ export async function getWorkspaceMovePreflight( droppedTotal > 0 ? { customBlocks: sourceImpact.truncated?.customBlocks ?? 0, + inboundCustomBlocks: sourceImpact.truncated?.inboundCustomBlocks ?? 0, permissionGroups: sourceImpact.truncated?.permissionGroups ?? 0, collaboratorCaps: sourceImpact.truncated?.collaboratorCaps ?? 0, forkEdges: boundedForkEdges.dropped, @@ -798,7 +840,7 @@ function buildMoveNotices(params: { if (params.sourceImpact.truncated) { const t = params.sourceImpact.truncated notices.push( - `This review is incomplete — some lists were truncated to stay within response limits: ${t.customBlocks} custom block(s), ${t.permissionGroups} permission group(s), ${t.collaboratorCaps} collaborator cap(s), ${t.forkEdges} fork edge(s), ${t.credentials} credential(s) and ${t.environmentVariableKeys} environment variable(s) not shown.` + `This review is incomplete — some lists were truncated to stay within response limits: ${t.customBlocks} custom block(s), ${t.inboundCustomBlocks} custom block(s) used from elsewhere, ${t.permissionGroups} permission group(s), ${t.collaboratorCaps} collaborator cap(s), ${t.forkEdges} fork edge(s), ${t.credentials} credential(s) and ${t.environmentVariableKeys} environment variable(s) not shown.` ) } if (!params.sourceOrganization) return notices @@ -815,6 +857,12 @@ function buildMoveNotices(params: { `${params.sourceImpact.unpublishedCustomBlocks.length} custom block${params.sourceImpact.unpublishedCustomBlocks.length === 1 ? '' : 's'} will be unpublished from ${params.sourceOrganization.name}${strandedDeployments > 0 ? `, breaking ${strandedDeployments} deployed workflow${strandedDeployments === 1 ? '' : 's'} that stay behind` : ''}.` ) } + const inboundBlocks = params.sourceImpact.inboundCustomBlocks.length + if (inboundBlocks > 0) { + notices.push( + `${inboundBlocks} custom block${inboundBlocks === 1 ? '' : 's'} published elsewhere in ${params.sourceOrganization.name} ${inboundBlocks === 1 ? 'is' : 'are'} used by this workspace's workflows and will stop resolving after the move. Publish ${inboundBlocks === 1 ? 'it' : 'them'} in ${params.destinationOrganization.name} or remove ${inboundBlocks === 1 ? 'it' : 'them'} from those workflows.` + ) + } const sourceBackedCredentials = params.credentials.items.filter( (item) => item.backedBySourceOrgMember ).length @@ -880,6 +928,7 @@ async function collectSourceOrganizationImpact( if (!sourceOrganizationId) { return { unpublishedCustomBlocks: [], + inboundCustomBlocks: [], detachedPermissionGroups: [], strippedRetentionRules: { piiRedactionRules: 0, retentionOverrides: 0 }, retainedCollaboratorCaps: [], @@ -888,27 +937,36 @@ async function collectSourceOrganizationImpact( } } - const [customBlocks, permissionGroups, retentionSettings, collaboratorCaps, brandingChanges] = - await Promise.all([ - findUnpublishableCustomBlocks(workspaceId, sourceOrganizationId), - findAttachedPermissionGroups(workspaceId), - db - .select({ dataRetentionSettings: organization.dataRetentionSettings }) - .from(organization) - .where(eq(organization.id, sourceOrganizationId)) - .limit(1), - findRetainedCollaboratorCaps(workspaceId, sourceOrganizationId), - willBrandingChange(sourceOrganizationId, destinationOrganizationId), - ]) + const [ + customBlocks, + inboundCustomBlocks, + permissionGroups, + retentionSettings, + collaboratorCaps, + brandingChanges, + ] = await Promise.all([ + findUnpublishableCustomBlocks(workspaceId, sourceOrganizationId), + findInboundSourceOrgCustomBlockUsage(workspaceId, sourceOrganizationId), + findAttachedPermissionGroups(workspaceId), + db + .select({ dataRetentionSettings: organization.dataRetentionSettings }) + .from(organization) + .where(eq(organization.id, sourceOrganizationId)) + .limit(1), + findRetainedCollaboratorCaps(workspaceId, sourceOrganizationId), + willBrandingChange(sourceOrganizationId, destinationOrganizationId), + ]) const boundedBlocks = boundList(customBlocks.items, PREFLIGHT_LIST_LIMITS.customBlocks) /** Enrichment already capped the slice, so the gap comes from the true total. */ const droppedBlocks = Math.max(customBlocks.total - boundedBlocks.items.length, 0) + const boundedInbound = boundList(inboundCustomBlocks, PREFLIGHT_LIST_LIMITS.customBlocks) const boundedGroups = boundList(permissionGroups, PREFLIGHT_LIST_LIMITS.permissionGroups) const boundedCaps = boundList(collaboratorCaps, PREFLIGHT_LIST_LIMITS.collaboratorCaps) return { unpublishedCustomBlocks: boundedBlocks.items, + inboundCustomBlocks: boundedInbound.items, detachedPermissionGroups: boundedGroups.items, strippedRetentionRules: countRetentionRulesForWorkspace( retentionSettings[0]?.dataRetentionSettings, @@ -917,9 +975,10 @@ async function collectSourceOrganizationImpact( retainedCollaboratorCaps: boundedCaps.items, brandingChanges, truncated: - droppedBlocks + boundedGroups.dropped + boundedCaps.dropped > 0 + droppedBlocks + boundedInbound.dropped + boundedGroups.dropped + boundedCaps.dropped > 0 ? { customBlocks: droppedBlocks, + inboundCustomBlocks: boundedInbound.dropped, permissionGroups: boundedGroups.dropped, collaboratorCaps: boundedCaps.dropped, forkEdges: 0, @@ -959,15 +1018,6 @@ export async function moveWorkspaceToOrganization(params: { * retry through the existing loop when it moved underneath us. */ let candidateSourceOrganizationId = await readWorkspaceOrganizationId(params.workspaceId) - /** - * Resolved outside the transaction on purpose — see the entitlement check - * inside it for why. Recomputed per attempt so a retry after a source-org - * change re-evaluates against the organization actually being left. - */ - let entitlements = await resolveMoveEntitlements( - candidateSourceOrganizationId, - params.destinationOrganizationId - ) let result: MoveTransactionResult | undefined for (let attempt = 0; attempt < 5; attempt += 1) { @@ -1070,8 +1120,6 @@ export async function moveWorkspaceToOrganization(params: { throw new SourceOrganizationChangedError(workspaceRow.organizationId) } const sourceOrganizationId = workspaceRow.organizationId - /** Set by the in-transaction fence; the summary reports this, not the optimistic read. */ - let fencedEntitlements: WorkspaceMoveEntitlements | undefined const moveState = classifyWorkspaceMoveState(workspaceRow, params.destinationOrganizationId) const destination = await getDestinationOrganization(params.destinationOrganizationId, tx) @@ -1127,20 +1175,13 @@ export async function moveWorkspaceToOrganization(params: { invitationEvents: [], summary: await getMovedWorkspaceSummary(tx, params.workspaceId, destination, { sourceOrganization: recordedSource.organization, - sourceOrganizationImpact: { - ...EMPTY_SOURCE_IMPACT, - truncated: buildAppliedTruncation({ - unpublishedCustomBlocks: 0, - detachedPermissionGroups: 0, - credentials: replayedCredentials, - }), - }, + sourceOrganizationImpact: buildAppliedSourceImpact({ + unpublishedCustomBlocks: recordedAudit?.unpublishedCustomBlocks ?? [], + detachedPermissionGroupIds: recordedAudit?.detachedPermissionGroupIds ?? [], + credentials: replayedCredentials, + }), credentials: replayedCredentials, - entitlements: { - sourceIsEnterprise: false, - destinationIsEnterprise: false, - capabilitiesLost: [], - }, + entitlements: REPLAYED_MOVE_ENTITLEMENTS, notices: recordedSource.unknown ? [ 'This workspace was already in the destination organization, so the organization it originally came from is no longer recoverable.', @@ -1190,19 +1231,11 @@ export async function moveWorkspaceToOrganization(params: { } /** - * The three org-to-org blockers, re-checked under the locks. Preflight - * evaluated them too, but a subscription can lapse, a fork can be - * created, and an invitation can arrive in between — and each of these - * either violates a cross-org invariant or silently rewrites a promise - * the source organization made. - */ - /** - * The fork check is NOT gated on an organization source. A personal - * workspace whose parent has since moved into an organization still - * produces a cross-organization edge when it lands in a different one, - * and the invariant admits no exceptions. Preflight already reports it - * unconditionally; gating it here would let the transaction accept a - * move preflight had refused. + * Preflight's blockers, re-checked under the locks: a fork can be created, a + * subscription can lapse, and an invitation can arrive in between. The fork check is + * not gated on an organization source: a personal workspace whose parent has since + * joined an organization still yields a cross-organization edge when it lands in a + * different one, and preflight reports it unconditionally too. */ const forkEdges = await findCrossOrgForkEdges( params.workspaceId, @@ -1244,52 +1277,25 @@ export async function moveWorkspaceToOrganization(params: { ) } - if (sourceOrganizationId) { - /** - * Entitlements are resolved BEFORE the transaction, not here. - * `isOrganizationOnEnterprisePlan` reads through the global client - * with no executor seam, so calling it inside the transaction trips - * the transaction tripwire outside production and reserves a second - * pool connection in it. The check is a precondition, not an - * invariant: a plan lapsing in the seconds between the read and the - * commit lands the workspace in an organization that just lost its - * entitlements, which is recoverable by moving it back — unlike a - * cross-organization artifact, which is not. - */ - /** - * Evaluate BOTH organizations under the locks when entitlement is - * subscription-backed, rather than trusting the pre-transaction - * verdict. That verdict is still what preflight reports, but as a - * blocker it is stale in both directions: a destination that lapsed - * after it was read, and a source that GAINED entitlement after it - * was read, which would otherwise skip the fence entirely. - * - * `isSubscriptionBackedEntitlement` is exported from the same module - * as `resolveOrganizationEnterprisePlan`'s short-circuits, so the two - * modes where entitlement is granted by deployment configuration — - * and no `subscription` row need exist — cannot drift away from this. - */ - /** - * Re-run the SAME resolver under the locks, on `tx`. The - * pre-transaction verdict is stale in both directions — a destination - * that lapsed after it was read, and a source that gained Enterprise - * after it was read, which would otherwise skip the check entirely. - * Reusing `resolveMoveEntitlements` rather than re-deriving the - * predicate here is what keeps the fence and preflight from ever - * disagreeing about what counts as a downgrade. - */ - fencedEntitlements = await resolveMoveEntitlements( - sourceOrganizationId, - params.destinationOrganizationId, - tx + /** + * The downgrade fence, re-resolved under the locks with the same resolver preflight + * uses, so the two never disagree about what counts as a downgrade and a plan change + * since preflight (a destination that lapsed, a source that gained Enterprise) is + * caught here. A personal source has nothing to lose and resolves without a query. + */ + const entitlements = await resolveMoveEntitlements( + sourceOrganizationId, + params.destinationOrganizationId, + tx + ) + if (entitlements.capabilitiesLost.length > 0) { + throw new WorkspaceMoveError( + `The destination organization is not on Enterprise, so this workspace would lose ${entitlements.capabilitiesLost.join(', ')}`, + 'destination-entitlement-downgrade' ) - if (fencedEntitlements.capabilitiesLost.length > 0) { - throw new WorkspaceMoveError( - `The destination organization is not on Enterprise, so this workspace would lose ${fencedEntitlements.capabilitiesLost.join(', ')}`, - 'destination-entitlement-downgrade' - ) - } + } + if (sourceOrganizationId) { const pendingInvitations = await getPendingInvitationSummaries(params.workspaceId, tx) if (pendingInvitations.length > 0) { throw new WorkspaceMoveError( @@ -1335,9 +1341,8 @@ export async function moveWorkspaceToOrganization(params: { /** * Enforce the cross-org invariants before the payer moves, while the * source organization is still the one on the row. Unpublishing a - * custom block is the product's own `deleteCustomBlock`; the usage - * counts are captured first so the source org's audit entry can say how - * much it cost. + * custom block is the product's own `deleteCustomBlock`; the blocks are + * read first so the source org's audit entry can name them. */ const sourceOrganization = sourceOrganizationId ? await getSourceOrganization(sourceOrganizationId, tx) @@ -1472,46 +1477,13 @@ export async function moveWorkspaceToOrganization(params: { : null, summary: await getMovedWorkspaceSummary(tx, params.workspaceId, destination, { sourceOrganization, - /** - * What the move actually did, not what preflight projected. The - * rest of the impact described the pre-move state and is not - * recoverable — or meaningful — once the workspace has landed. - */ - sourceOrganizationImpact: { - ...EMPTY_SOURCE_IMPACT, - /** - * Usage counts are zero here rather than measured: they describe - * how much breaks in the source organization, and the reads that - * produce them are not transaction-safe. Preflight carries the - * real numbers; this reports which blocks were unpublished. - */ - unpublishedCustomBlocks: boundList( - unpublishedCustomBlocks, - PREFLIGHT_LIST_LIMITS.customBlocks - ).items.map((block) => ({ - ...block, - movingWorkspaceUsage: { live: 0, deployed: 0 }, - sourceOrgElsewhereUsage: { live: 0, deployed: 0 }, - })), - detachedPermissionGroups: boundList( - cleanup.detachedPermissionGroupIds, - PREFLIGHT_LIST_LIMITS.permissionGroups - ).items.map((permissionGroupId) => ({ permissionGroupId, name: '' })), - /** The applied response is bounded by the same limits as preflight. */ - truncated: buildAppliedTruncation({ - unpublishedCustomBlocks: unpublishedCustomBlocks.length, - detachedPermissionGroups: cleanup.detachedPermissionGroupIds.length, - credentials: movedCredentials, - }), - }, + sourceOrganizationImpact: buildAppliedSourceImpact({ + unpublishedCustomBlocks, + detachedPermissionGroupIds: cleanup.detachedPermissionGroupIds, + credentials: movedCredentials, + }), credentials: movedCredentials, - /** - * The fenced result, not the optimistic one: the response must - * describe the entitlement state the move was actually allowed - * against, or a destination that gained Enterprise between the two - * reads is reported as a downgrade it no longer is. - */ - entitlements: fencedEntitlements ?? entitlements, + entitlements, notices: [], }), } satisfies MoveTransactionResult @@ -1527,10 +1499,6 @@ export async function moveWorkspaceToOrganization(params: { } if (error instanceof SourceOrganizationChangedError) { candidateSourceOrganizationId = error.organizationId - entitlements = await resolveMoveEntitlements( - candidateSourceOrganizationId, - params.destinationOrganizationId - ) continue } if (isConcurrentPendingInvitationInsert(error)) { @@ -1960,20 +1928,13 @@ export async function getWorkspaceMoveOperation( return toWorkspaceMoveOperationView( await getMovedWorkspaceSummary(db, workspaceId, destination, { sourceOrganization: recordedSource.organization, - sourceOrganizationImpact: { - ...EMPTY_SOURCE_IMPACT, - truncated: buildAppliedTruncation({ - unpublishedCustomBlocks: 0, - detachedPermissionGroups: 0, - credentials, - }), - }, + sourceOrganizationImpact: buildAppliedSourceImpact({ + unpublishedCustomBlocks: operationPayload.audit.unpublishedCustomBlocks ?? [], + detachedPermissionGroupIds: operationPayload.audit.detachedPermissionGroupIds ?? [], + credentials, + }), credentials, - entitlements: { - sourceIsEnterprise: false, - destinationIsEnterprise: false, - capabilitiesLost: [], - }, + entitlements: REPLAYED_MOVE_ENTITLEMENTS, /** * A recorded `null` means the workspace came from a personal source and * there is nothing to name — not that the record is defective. @@ -2754,7 +2715,7 @@ async function getMovedWorkspaceSummary( executor: DbOrTx, workspaceId: string, destination: WorkspaceMoveDestination, - appliedContext?: AppliedMoveContext + appliedContext: AppliedMoveContext ): Promise { const [movedRow] = await executor .select({ @@ -2802,7 +2763,7 @@ async function getMovedWorkspaceSummary( sourceMember, and( eq(sourceMember.userId, permissions.userId), - appliedContext?.sourceOrganization + appliedContext.sourceOrganization ? eq(sourceMember.organizationId, appliedContext.sourceOrganization.id) : sql`false` ) @@ -2811,7 +2772,7 @@ async function getMovedWorkspaceSummary( return { workspace: workspaceRow, - sourceOrganization: appliedContext?.sourceOrganization ?? null, + sourceOrganization: appliedContext.sourceOrganization, destinationOrganization: destination, collaborators: collaboratorRows.map((row) => ({ userId: row.userId, @@ -2824,39 +2785,15 @@ async function getMovedWorkspaceSummary( invitations: (await getPendingInvitationSummaries(workspaceId, executor)).map( ({ organizationId: _organizationId, ...row }) => row ), - sourceOrganizationImpact: appliedContext?.sourceOrganizationImpact ?? EMPTY_SOURCE_IMPACT, - credentials: appliedContext?.credentials ?? EMPTY_CREDENTIAL_SUMMARY, - entitlements: appliedContext?.entitlements ?? { - sourceIsEnterprise: false, - destinationIsEnterprise: false, - capabilitiesLost: [], - }, + sourceOrganizationImpact: appliedContext.sourceOrganizationImpact, + credentials: appliedContext.credentials, + entitlements: appliedContext.entitlements, blockers: [], - notices: appliedContext?.notices ?? [], + notices: appliedContext.notices, warning: null, } } -/** A move that is already applied has no source-org context to report. */ -const EMPTY_SOURCE_IMPACT: WorkspaceMoveSourceImpact = { - unpublishedCustomBlocks: [], - blockingForkEdges: [], - detachedPermissionGroups: [], - strippedRetentionRules: { piiRedactionRules: 0, retentionOverrides: 0 }, - retainedCollaboratorCaps: [], - brandingChanges: false, - truncated: null, -} - -const EMPTY_CREDENTIAL_SUMMARY: WorkspaceMoveCredentialSummary = { - items: [], - credentialGroupCount: 0, - environmentVariableKeys: [], - byokKeyCount: 0, - truncatedCredentials: 0, - truncatedEnvironmentVariableKeys: 0, -} - /** * The workspace's current organization, read outside the move transaction so * both organizations can be locked in a deterministic order. Always re-verified diff --git a/apps/sim/lib/workspaces/application/remove-member.test.ts b/apps/sim/lib/workspaces/application/remove-member.test.ts index 91ab7cb2f3c..e79a7496af2 100644 --- a/apps/sim/lib/workspaces/application/remove-member.test.ts +++ b/apps/sim/lib/workspaces/application/remove-member.test.ts @@ -158,7 +158,7 @@ describe('workspace member removal', () => { mocks.revoke.mockResolvedValue({ revoked: false, reason: 'unresolved-workflows', - unresolvedWorkflows: ['workflow'], + unresolvedWorkspaceIds: ['workflow'], }) let committed = false dbChainMockFns.transaction.mockImplementationOnce(async (callback) => { diff --git a/apps/sim/lib/workspaces/resource-handoff.ts b/apps/sim/lib/workspaces/resource-handoff.ts index a2882b37898..ffbfcc1bac0 100644 --- a/apps/sim/lib/workspaces/resource-handoff.ts +++ b/apps/sim/lib/workspaces/resource-handoff.ts @@ -43,7 +43,7 @@ export async function listSharedResourceWorkspaceIdsForUser(executor: DbOrTx, us UNION ALL SELECT ${userTableDefinitions.workspaceId} FROM ${userTableDefinitions} WHERE ${userTableDefinitions.createdBy} = ${userId} UNION ALL SELECT ${knowledgeBase.workspaceId} FROM ${knowledgeBase} WHERE ${knowledgeBase.userId} = ${userId} UNION ALL SELECT ${workspaceFiles.workspaceId} FROM ${workspaceFiles} - WHERE ${workspaceFiles.userId} = ${userId} AND ${workspaceFiles.context} IN ('workspace', 'knowledge-base', 'execution', 'chat', 'workspace-logos') AND ${workspaceFiles.chatId} IS NULL + WHERE ${workspaceFiles.userId} = ${userId} AND ${inArray(workspaceFiles.context, SHARED_FILE_CONTEXTS)} AND ${workspaceFiles.chatId} IS NULL UNION ALL SELECT ${workspaceFile.workspaceId} FROM ${workspaceFile} WHERE ${workspaceFile.uploadedBy} = ${userId} UNION ALL SELECT ${workflowMcpServer.workspaceId} FROM ${workflowMcpServer} WHERE ${workflowMcpServer.createdBy} = ${userId} AND NOT ${workflowMcpServer.isPublic} diff --git a/apps/sim/scripts/backfill-projects.ts b/apps/sim/scripts/backfill-projects.ts index 82a9cf46e76..ca924b17f93 100644 --- a/apps/sim/scripts/backfill-projects.ts +++ b/apps/sim/scripts/backfill-projects.ts @@ -161,7 +161,7 @@ async function main() { Routine assignment and enforcement run through db:migrate.\n From apps/sim: bun --no-env-file scripts/backfill-projects.ts [options]\n plan Read-only discovery; writes a new --manifest PATH. Optional --review PATH carries reviewed grouping decisions.\nrepair Repair reviewed archives and explicitly selected detached families; requires --report PATH.\napply Optional maintenance: assign reviewed families from --manifest; requires --report PATH.\nverify Validate the database and repair completion; requires --manifest and --report.\nstatus Read --report; does not connect or resume.\n -Writes require MIGRATION_DATABASE_URL pointing directly to the primary and --ack-release-drained.\nDeploy #8830 and verify old servers/workers drained first. Keep the reviewed manifest and report\nin durable private job storage. Review conflicts and repairs before repair/apply. Repair requires\nthe app runtime environment (DATABASE_URL must target the same database) for provider cleanup.\n +Writes require MIGRATION_DATABASE_URL pointing directly to the primary and --ack-release-drained.\nDeploy v0.9.18 and verify old servers/workers drained first. Keep the reviewed manifest and report\nin durable private job storage. Review conflicts and repairs before repair/apply. Repair requires\nthe app runtime environment (DATABASE_URL must target the same database) for provider cleanup.\n Defaults: --batch-size 50 (1–50), --max-batches 100 (1–10000), --seconds 60 (1–3600), --pause-ms 100 (1–60000).\nA budget limits scheduling. Transactions use 3s statement limits and a 5s total limit on PG17+,\notherwise a 5s idle-transaction limit on PG16. Reuse the same report\nto resume; lost reports can be recreated safely from the same manifest. Changed families need a\nnew plan/report. Review groupings explicitly: retain a legitimate group, or select one detachRootId and keep its generated destinationProjectId for replay. After detach, rediscover. Resolve all other conflicts and complete archive/provider cleanup before #8590. The registered runner can read exact retained-group decisions from PROJECT_BACKFILL_REVIEW_PATH. @@ -199,7 +199,7 @@ The registered migration first commits the durable column-authority switch after const databaseId = projectBackfillDatabaseId(rawUrl) const live = command === 'repair' || command === 'apply' if (live && !values['ack-release-drained']) - throw new Error('Verify #8830 server/worker drainage and pass --ack-release-drained') + throw new Error('Verify v0.9.18 server/worker drainage and pass --ack-release-drained') const batchSize = boundedInteger(values['batch-size'], 50, 50) const maxBatches = boundedInteger(values['max-batches'], 100, 10000) const seconds = boundedInteger(values.seconds, 60, 3600) diff --git a/apps/sim/tools/ashby/ashby.test.ts b/apps/sim/tools/ashby/ashby.test.ts index ea412a04b8d..e46da7cc0cc 100644 --- a/apps/sim/tools/ashby/ashby.test.ts +++ b/apps/sim/tools/ashby/ashby.test.ts @@ -311,13 +311,6 @@ describe('ashby request bodies', () => { ).toThrow(/required. Pass null to clear/) }) - it('set_custom_field_values sends the values array', () => { - const values = [{ fieldId: 'f', fieldValue: 'High' }] - expect( - requestBody(setCustomFieldValuesTool, { objectId: 'o', objectType: 'Job', values }).values - ).toEqual(values) - }) - it('rejects an empty values array before it reaches Ashby', () => { expect(() => requestBody(setCustomFieldValuesTool, { objectId: 'o', objectType: 'Job', values: [] }) @@ -436,41 +429,6 @@ describe('ashby response transforms', () => { expect(result.output.nextSyncCursor).toBeNull() }) - it('set_custom_field_value maps the single result object', async () => { - const result = await transform(setCustomFieldValueTool, { - success: true, - results: { id: 'cf', title: 'Priority', isPrivate: false, value: 'High', valueLabel: 'High' }, - }) - expect(result.output.customField).toEqual({ - id: 'cf', - title: 'Priority', - isPrivate: false, - value: 'High', - valueLabel: 'High', - }) - }) - - it('set_custom_field_values maps the result array', async () => { - // The plural endpoint returns an array where the singular returns an object. - const result = await transform(setCustomFieldValuesTool, { - success: true, - results: [ - { id: 'a', title: 'A', value: 1 }, - { id: 'b', title: 'B', value: null }, - ], - }) - expect(result.output.customFields).toHaveLength(2) - expect(result.output.customFields[1].value).toBeNull() - }) - - it('delete_application surfaces the deleted id', async () => { - const result = await transform(deleteApplicationTool, { - success: true, - results: { applicationId: 'app-1' }, - }) - expect(result.output.applicationId).toBe('app-1') - }) - it('delete_application reports a missing permission readably', async () => { // candidatesDelete is a separate module permission, so this is the most // likely failure for this operation and must not surface as [object Object]. @@ -497,24 +455,6 @@ describe('ashby response transforms', () => { } }) - it('anonymize_candidate maps the returned candidate', async () => { - const result = await transform(anonymizeCandidateTool, { - success: true, - results: { id: 'cand-1', name: 'Anonymous cand-1', primaryEmailAddress: null }, - }) - expect(result.output.id).toBe('cand-1') - expect(result.output.name).toBe('Anonymous cand-1') - }) - - it('change_application_source maps the returned application', async () => { - const result = await transform(changeApplicationSourceTool, { - success: true, - results: { id: 'app-1', status: 'Active', source: null }, - }) - expect(result.output.id).toBe('app-1') - expect(result.output.source).toBeNull() - }) - it('keeps an array valueLabel intact for MultiValueSelect fields', async () => { // This is why AshbyCustomField.valueLabel widened to string | string[]; // a mapper that coerced it would silently drop the labels. @@ -526,20 +466,6 @@ describe('ashby response transforms', () => { expect(result.output.customField.value).toEqual(['a', 'b']) }) - it('list_job_postings surfaces draft status on the new field', async () => { - const result = await transform(listJobPostingsTool, { - success: true, - results: [ - { id: 'p1', title: 'A', status: 'Published' }, - { id: 'p2', title: 'B', status: 'Draft' }, - ], - }) - expect(result.output.jobPostings.map((p: { status: string }) => p.status)).toEqual([ - 'Published', - 'Draft', - ]) - }) - it('defaults missing result payloads instead of throwing', async () => { const deleted = await transform(deleteApplicationTool, { success: true, results: {} }) expect(deleted.output.applicationId).toBe('') diff --git a/apps/sim/tools/ashby/utils.ts b/apps/sim/tools/ashby/utils.ts index 059f296542f..ebe994286da 100644 --- a/apps/sim/tools/ashby/utils.ts +++ b/apps/sim/tools/ashby/utils.ts @@ -86,7 +86,10 @@ export function ashbyLimit(value: unknown, parameter = 'perPage'): number | unde * array whose entries are either plain strings or `{ message, parameter }` * objects. An object entry stringifies to `[object Object]` unless its message * is read explicitly, which is the form a 403 for a missing module permission - * arrives in. + * arrives in. A single response can carry more than one of these at once. + * + * Shared with the Ashby webhook provider, which webhook routes reach, so this + * module keeps type-only imports and adds no runtime dependencies to that graph. */ export function ashbyErrorMessage(data: unknown, fallback: string): string { if (!data || typeof data !== 'object') return fallback diff --git a/apps/sim/tools/file/edit.ts b/apps/sim/tools/file/edit.ts index 78b274cd72b..cc2a4177e47 100644 --- a/apps/sim/tools/file/edit.ts +++ b/apps/sim/tools/file/edit.ts @@ -142,7 +142,7 @@ export const fileEditTool: InternalToolConfig = { required: false, visibility: 'user-or-llm', description: - 'For anchored edits, which matching anchor occurrence to use, starting at 1. Defaults to 1.', + 'For anchored edits, which start or insert anchor match to use, starting at 1. Defaults to 1. The end anchor is the first match after it.', }, expectedRevision: { type: 'string', diff --git a/apps/sim/tools/generated/tool-metadata.ts b/apps/sim/tools/generated/tool-metadata.ts index 6754b2e0958..8bef0a8da7a 100644 --- a/apps/sim/tools/generated/tool-metadata.ts +++ b/apps/sim/tools/generated/tool-metadata.ts @@ -3,7 +3,7 @@ /** Serializable metadata for every built-in tool, keyed by tool id. */ const toolMetadata: Record = JSON.parse( - '{"a2a_cancel_task":{"id":"a2a_cancel_task","name":"A2A Cancel Task","description":"Request cancellation of an in-progress A2A task.","version":"1.0.0","params":{"agentUrl":{"type":"string","required":true,"visibility":"user-only","description":"The A2A agent endpoint URL"},"taskId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The task ID to cancel"},"apiKey":{"type":"string","required":false,"visibility":"user-only","description":"API key for authentication (if required)"}},"hostedApiKey":"none"},"a2a_get_agent_card":{"id":"a2a_get_agent_card","name":"A2A Get Agent Card","description":"Fetch the Agent Card (discovery document) for an external A2A agent.","version":"1.0.0","params":{"agentUrl":{"type":"string","required":true,"visibility":"user-only","description":"The A2A agent endpoint URL"},"apiKey":{"type":"string","required":false,"visibility":"user-only","description":"API key for authentication (if required)"}},"hostedApiKey":"none"},"a2a_get_task":{"id":"a2a_get_task","name":"A2A Get Task","description":"Retrieve the current state and result of an A2A task.","version":"1.0.0","params":{"agentUrl":{"type":"string","required":true,"visibility":"user-only","description":"The A2A agent endpoint URL"},"taskId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The task ID to retrieve"},"historyLength":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of history messages to include"},"apiKey":{"type":"string","required":false,"visibility":"user-only","description":"API key for authentication (if required)"}},"hostedApiKey":"none"},"a2a_send_message":{"id":"a2a_send_message","name":"A2A Send Message","description":"Send a message to an external A2A agent and return its response.","version":"1.0.0","params":{"agentUrl":{"type":"string","required":true,"visibility":"user-only","description":"The A2A agent endpoint URL"},"message":{"type":"string","required":true,"visibility":"user-or-llm","description":"The message text to send"},"data":{"type":"json","required":false,"visibility":"user-or-llm","description":"Optional structured JSON data to attach"},"files":{"type":"json","required":false,"visibility":"user-or-llm","description":"Optional files to attach"},"taskId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Existing task ID to continue"},"contextId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Conversation context ID to continue"},"apiKey":{"type":"string","required":false,"visibility":"user-only","description":"API key for authentication (if required)"}},"hostedApiKey":"none"},"affinity_batch_update_entity_fields":{"id":"affinity_batch_update_entity_fields","name":"Affinity Batch Update Entity Fields","description":"Write up to 100 non-list field values on one company or person in a single request.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to write the fields on: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"updates":{"type":"json","required":true,"visibility":"user-or-llm","description":"Up to 100 field updates as [{\\"id\\":\\"\\",\\"value\\":{\\"type\\":\\"…\\",\\"data\\":…}}], using the same value shapes as a single field update"}},"hostedApiKey":"none"},"affinity_batch_update_list_entry_fields":{"id":"affinity_batch_update_list_entry_fields","name":"Affinity Batch Update List Entry Fields","description":"Write up to 100 field values on one list row in a single request. Requires the \\"Export data from Lists\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"updates":{"type":"json","required":true,"visibility":"user-or-llm","description":"Up to 100 field updates as [{\\"id\\":\\"\\",\\"value\\":{\\"type\\":\\"…\\",\\"data\\":…}}], using the same value shapes as a single field update"}},"hostedApiKey":"none"},"affinity_create_list":{"id":"affinity_create_list","name":"Affinity Create List","description":"Create a list. Its type fixes which entities it can hold, and the API key holder becomes its creator and owner.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the new list"},"type":{"type":"string","required":true,"visibility":"user-or-llm","description":"Entity kind the list holds: company, opportunity, or person"},"isPublic":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Whether everyone in the organization can see the list"}},"hostedApiKey":"none"},"affinity_create_list_field_dropdown_option":{"id":"affinity_create_list_field_dropdown_option","name":"Affinity Create List Field Dropdown Option","description":"Add a selectable option to a dropdown field on a list. A ranked or status option also needs a rank and a color.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown field ID on that list"},"type":{"type":"string","required":true,"visibility":"user-or-llm","description":"Kind of option to create, matching the field. dropdown takes only a label; ranked-dropdown also requires rank and color; status-dropdown additionally requires a status category. Sending a field the kind does not accept is rejected"},"text":{"type":"string","required":true,"visibility":"user-or-llm","description":"The option label"},"rank":{"type":"number","required":false,"visibility":"user-or-llm","description":"Sort order. Required on a ranked-dropdown or status-dropdown option"},"color":{"type":"string","required":false,"visibility":"user-or-llm","description":"Option color: white, gray, blue, green, purple, orange, or red. Required on a ranked-dropdown or status-dropdown option"},"statusCategory":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pipeline meaning of the option: open, won, lost, or on-hold. Status-dropdown options only"},"winRate":{"type":"number","required":false,"visibility":"user-or-llm","description":"Expected win rate of the status. Status-dropdown options only"}},"hostedApiKey":"none"},"affinity_create_merge":{"id":"affinity_create_merge","name":"Affinity Create Merge","description":"Fold a duplicate company or person into the record you are keeping. The merge runs asynchronously — poll the returned task to see it finish. Requires the \\"Manage duplicates\\" permission and an admin role.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"What to merge: companies or persons"},"primaryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to keep"},"duplicateId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the duplicate record to fold in"}},"hostedApiKey":"none"},"affinity_create_note":{"id":"affinity_create_note","name":"Affinity Create Note","description":"Write a note — attached to companies, persons, and opportunities, anchored to a meeting, call, or chat message, or posted as a reply to an existing note.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"type":{"type":"string","required":true,"visibility":"user-or-llm","description":"Note shape: entities to attach it to records, interaction to anchor it to a meeting, call, or chat message, or user-reply to reply to a note"},"html":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note body as HTML"},"companyIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Companies to attach the note to, e.g. [1, 2]. Not used on a reply"},"personIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Persons to attach the note to, e.g. [1, 2]. Not used on a reply"},"opportunityIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Opportunities to attach the note to, e.g. [1, 2]. Not used on a reply"},"interactionId":{"type":"string","required":false,"visibility":"user-or-llm","description":"The interaction to anchor the note to. Required for an interaction note"},"interactionType":{"type":"string","required":false,"visibility":"user-or-llm","description":"Kind of the anchoring interaction: meeting, call, or chat-message. Required for an interaction note"},"parentId":{"type":"string","required":false,"visibility":"user-or-llm","description":"The note being replied to. Required for a user-reply note"},"creatorId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Attribute the note to another internal person. Defaults to the API key holder"},"createdAt":{"type":"string","required":false,"visibility":"user-or-llm","description":"Backdate the note to this ISO 8601 timestamp"}},"hostedApiKey":"none"},"affinity_create_reminder":{"id":"affinity_create_reminder","name":"Affinity Create Reminder","description":"Create a reminder on one company, person, or opportunity. A recurring reminder resets whenever the chosen signal happens instead of firing once.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"type":{"type":"string","required":true,"visibility":"user-or-llm","description":"one-time to fire once, or recurring to reset on a signal"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"What the reminder is about: company, person, or opportunity"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company, person, or opportunity"},"dueDate":{"type":"string","required":false,"visibility":"user-or-llm","description":"When the reminder is due, as an ISO 8601 timestamp. Required for a one-time reminder; on a recurring one Affinity computes it from the period when omitted"},"content":{"type":"string","required":false,"visibility":"user-or-llm","description":"What the reminder says"},"ownerId":{"type":"string","required":true,"visibility":"user-or-llm","description":"User the reminder is assigned to. Must be an internal user. The API key holder is recorded as the creator, which is a separate field"},"resetTrigger":{"type":"string","required":false,"visibility":"user-or-llm","description":"What restarts a recurring reminder: interaction, email, or event. Required when the type is recurring"},"periodDays":{"type":"number","required":false,"visibility":"user-or-llm","description":"Days between firings of a recurring reminder. Required when the type is recurring"}},"hostedApiKey":"none"},"affinity_delete_list_field_dropdown_option":{"id":"affinity_delete_list_field_dropdown_option","name":"Affinity Delete List Field Dropdown Option","description":"Permanently delete a dropdown option on a list field. Every list entry currently set to it is cleared, and those values cannot be recovered.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown field ID on that list"},"dropdownOptionId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown option ID to delete"}},"hostedApiKey":"none"},"affinity_delete_note":{"id":"affinity_delete_note","name":"Affinity Delete Note","description":"Delete a note you created. Deleting a root note also deletes its replies; deleting a reply removes only that reply.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID to delete"}},"hostedApiKey":"none"},"affinity_get_company":{"id":"affinity_get_company","name":"Affinity Get Company","description":"Look up one company by ID. Field data is returned only for the Field IDs or Field Types asked for.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"companyId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The company ID"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"}},"hostedApiKey":"none"},"affinity_get_current_user":{"id":"affinity_get_current_user","name":"Affinity Get Current User","description":"Verify an Affinity API key and return the tenant, the user behind the key, and the scopes the grant carries.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"}},"hostedApiKey":"none"},"affinity_get_entity_field_value":{"id":"affinity_get_entity_field_value","name":"Affinity Get Entity Field Value","description":"Read one non-list field value from a company or person.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to read the field from: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The field ID to read"}},"hostedApiKey":"none"},"affinity_get_list":{"id":"affinity_get_list","name":"Affinity Get List","description":"Read one list — its name, type, owner, and privacy setting.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"}},"hostedApiKey":"none"},"affinity_get_list_entry":{"id":"affinity_get_list_entry","name":"Affinity Get List Entry","description":"Read one row of a list with its entity. Field data is returned only for the Field IDs or Field Types asked for.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, list, or relationship-intelligence. Mutually exclusive with Field IDs"}},"hostedApiKey":"none"},"affinity_get_list_entry_field":{"id":"affinity_get_list_entry_field","name":"Affinity Get List Entry Field","description":"Read one field value on a list row.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The field ID to read"}},"hostedApiKey":"none"},"affinity_get_list_field_dropdown_option":{"id":"affinity_get_list_field_dropdown_option","name":"Affinity Get List Field Dropdown Option","description":"Read one dropdown option on a list field.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown field ID on that list"},"dropdownOptionId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown option ID"}},"hostedApiKey":"none"},"affinity_get_merge":{"id":"affinity_get_merge","name":"Affinity Get Merge","description":"Read the status of one company or person merge, including why it failed if it did.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which merge to read: companies or persons"},"mergeId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The merge ID"}},"hostedApiKey":"none"},"affinity_get_merge_task":{"id":"affinity_get_merge_task","name":"Affinity Get Merge Task","description":"Read one merge task and how its merges are progressing. Poll this after starting a merge.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which merge task to read: companies or persons"},"taskId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The merge task ID"}},"hostedApiKey":"none"},"affinity_get_note":{"id":"affinity_get_note","name":"Affinity Get Note","description":"Read one note with its body, author, mentions, and attached records.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID"},"includes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Extra properties to return, e.g. [\\"repliesCount\\",\\"personsPreview\\",\\"companiesPreview\\",\\"opportunitiesPreview\\"]. Those four fields are omitted unless requested here"}},"hostedApiKey":"none"},"affinity_get_opportunity":{"id":"affinity_get_opportunity","name":"Affinity Get Opportunity","description":"Read one opportunity and the list it belongs to. Its field data lives on the list entry.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"opportunityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The opportunity ID"}},"hostedApiKey":"none"},"affinity_get_person":{"id":"affinity_get_person","name":"Affinity Get Person","description":"Look up one person by ID. Field data is returned only for the Field IDs or Field Types asked for.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"personId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The person ID"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"}},"hostedApiKey":"none"},"affinity_get_saved_view":{"id":"affinity_get_saved_view","name":"Affinity Get Saved View","description":"Read one saved view — its name, kind, and creation date.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"viewId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The saved view ID"}},"hostedApiKey":"none"},"affinity_get_transcript":{"id":"affinity_get_transcript","name":"Affinity Get Transcript","description":"Read one transcript with its first 100 fragments. Page the fragments endpoint for a longer meeting.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"transcriptId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The transcript ID"}},"hostedApiKey":"none"},"affinity_get_user":{"id":"affinity_get_user","name":"Affinity Get User","description":"Read one internal user. A user and their person record share the same numeric ID, so a person ID works here.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"userId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The user ID, which is also their person ID"}},"hostedApiKey":"none"},"affinity_list_calls":{"id":"affinity_list_calls","name":"Affinity List Calls","description":"Page through logged calls and their participants. Only calls the API key holder can see are returned.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_chat_messages":{"id":"affinity_list_chat_messages","name":"Affinity List Chat Messages","description":"Page through logged chat messages and their participants. Only messages the API key holder can see are returned.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_companies":{"id":"affinity_list_companies","name":"Affinity List Companies","description":"Page through companies. Companies come back without field data unless Field IDs or Field Types asks for it.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the page to these company IDs, e.g. [1, 2, 3]"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_coworker_connections":{"id":"affinity_list_coworker_connections","name":"Affinity List Coworker Connections","description":"Find warm paths into a company through shared work history: who in your Affinity data once worked alongside the people you want to reach. Grouped by target, strongest first.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":true,"visibility":"user-or-llm","description":"Required scope. The only supported filter is target.currentCompany.id, e.g. \\"target.currentCompany.id=123\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of targets to return per page, 1-50. Defaults to 20"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_emails":{"id":"affinity_list_emails","name":"Affinity List Emails","description":"Page through email metadata — subject, participants, and timestamps. Affinity never exposes email bodies through the API.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_entity_field_values":{"id":"affinity_list_entity_field_values","name":"Affinity List Entity Field Values","description":"Page through a company\'s or person\'s non-list field values. List fields are not returned here — read those through the list entry.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to read field values from: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict to these field IDs. Mutually exclusive with Field Types"},"types":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict to these field categories: enriched, global, relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 20"}},"hostedApiKey":"none"},"affinity_list_entity_list_entries":{"id":"affinity_list_entity_list_entries","name":"Affinity List Entity List Entries","description":"Page through a company\'s or person\'s rows across every list, each carrying that list\'s field values and when the entity was added.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to look up the rows of: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_entity_lists":{"id":"affinity_list_entity_lists","name":"Affinity List Entity Lists","description":"List every list a company or person appears on that the caller can view.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to look up the lists of: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_entity_notes":{"id":"affinity_list_entity_notes","name":"Affinity List Entity Notes","description":"List the notes relevant to one company, person, or opportunity — directly attached notes plus notes reaching it through its people and meetings.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity the notes hang off: companies, persons, or opportunities"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company, person, or opportunity"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_entity_relationships":{"id":"affinity_list_entity_relationships","name":"Affinity List Entity Relationships","description":"List who knows a company or person, scored 0.0 to 1.0 by how much the two actually interact. Strongest first by default.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to look up relationships for: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression. This endpoint filters on interactionScore only, e.g. \\"interactionScore>=0.5\\""},"orderBy":{"type":"json","required":false,"visibility":"user-or-llm","description":"Sort order: [\\"interactionScore\\"] for weakest first, [\\"-interactionScore\\"] for strongest first (the default)"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_field_dropdown_options":{"id":"affinity_list_field_dropdown_options","name":"Affinity List Field Dropdown Options","description":"List the selectable options on a dropdown or ranked-dropdown company or person field. Writing such a field needs the option ID, not its text.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which field family the field belongs to: companies or persons"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown or ranked-dropdown field ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_field_metadata":{"id":"affinity_list_field_metadata","name":"Affinity List Field Metadata","description":"List the non-list company or person fields, with the value type, filter operators, and sort support of each. Start here to find the Field IDs the read and write tools take.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which fields to describe: companies or persons"},"includes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Extra properties to return: [\\"filterability\\",\\"sortability\\"]. Both are omitted unless requested here"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression. This endpoint filters on name only, e.g. \\"name=~Status\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_field_value_changes":{"id":"affinity_list_field_value_changes","name":"Affinity List Field Value Changes","description":"Page through field value changes across the whole workspace. Built for delta sync: follow nextCursor to the end of a run, then resume from the last cursor next time.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression over field.id, listEntry.id, changer.id, changedAt, or actionType. Resume a sync with e.g. \\"changedAt>2026-06-01T12:00:00Z\\""},"orderBy":{"type":"json","required":false,"visibility":"user-or-llm","description":"Sort order: [\\"changedAt\\"] for oldest first (the default), [\\"-changedAt\\"] for newest first"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_investor_executive_connections":{"id":"affinity_list_investor_executive_connections","name":"Affinity List Investor Executive Connections","description":"Find warm paths into a company through investment history: which investors in your Affinity data backed a company the people you want to reach once led. Grouped by target, strongest first.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":true,"visibility":"user-or-llm","description":"Required scope. The only supported filter is target.currentCompany.id, e.g. \\"target.currentCompany.id=123\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of targets to return per page, 1-50. Defaults to 20"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_list_entries":{"id":"affinity_list_list_entries","name":"Affinity List List Entries","description":"Page through the rows of a list. Rows come back without field data unless Field IDs or Field Types asks for it.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, list, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_list_entry_field_value_changes":{"id":"affinity_list_list_entry_field_value_changes","name":"Affinity List List Entry Field Value Changes","description":"Page through the history of one list row — who changed which field, when, and to what.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression over field.id, changer.id, changedAt, or actionType, e.g. \\"field.id=field-1234\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_list_entry_fields":{"id":"affinity_list_list_entry_fields","name":"Affinity List List Entry Fields","description":"Page through every field value on one list row, including the list-specific columns. All fields are returned unless narrowed.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict to these field IDs. Mutually exclusive with Field Types"},"types":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict to these field categories: enriched, global, list, relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 20"}},"hostedApiKey":"none"},"affinity_list_list_field_dropdown_options":{"id":"affinity_list_list_field_dropdown_options","name":"Affinity List List Field Dropdown Options","description":"List the selectable options on a dropdown, ranked-dropdown, or status-dropdown field of a list.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown field ID on that list"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_list_fields":{"id":"affinity_list_list_fields","name":"Affinity List List Fields","description":"List the fields available on one list, including its list-specific columns. Use these Field IDs when reading or writing list entries.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"includes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Extra properties to return: [\\"filterability\\",\\"sortability\\"]. Both are omitted unless requested here"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression. This endpoint filters on name only, e.g. \\"name=~Stage\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_lists":{"id":"affinity_list_lists","name":"Affinity List Lists","description":"Page through the lists in the organization that the caller can view.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"term":{"type":"string","required":false,"visibility":"user-or-llm","description":"Case-insensitive substring match on the list name"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_meetings":{"id":"affinity_list_meetings","name":"Affinity List Meetings","description":"Page through past and upcoming meetings with their organizer and attendees.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_merge_tasks":{"id":"affinity_list_merge_tasks","name":"Affinity List Merge Tasks","description":"Page through merge tasks, each summarizing how many of its merges are in progress, succeeded, or failed.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which merge tasks to list: companies or persons"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression. This endpoint filters on status only, e.g. \\"status=in-progress\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_merges":{"id":"affinity_list_merges","name":"Affinity List Merges","description":"Page through the company or person merges the organization has run, with the status and the records involved in each.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which merges to list: companies or persons"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression over status or taskId, e.g. \\"status=failed\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_note_attached_companies":{"id":"affinity_list_note_attached_companies","name":"Affinity List Note Attached Companies","description":"List the companies directly attached to one note.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_note_attached_opportunities":{"id":"affinity_list_note_attached_opportunities","name":"Affinity List Note Attached Opportunities","description":"List the opportunities directly attached to one note.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_note_attached_persons":{"id":"affinity_list_note_attached_persons","name":"Affinity List Note Attached Persons","description":"List the persons directly attached to one note.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_note_replies":{"id":"affinity_list_note_replies","name":"Affinity List Note Replies","description":"Page through the replies on one note, including AI Notetaker replies.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID whose replies to read"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_notes":{"id":"affinity_list_notes","name":"Affinity List Notes","description":"Page through every note the caller can see. Replies are excluded.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"includes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Extra properties to return, e.g. [\\"repliesCount\\",\\"personsPreview\\",\\"companiesPreview\\",\\"opportunitiesPreview\\"]. Those four fields are omitted unless requested here"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_opportunities":{"id":"affinity_list_opportunities","name":"Affinity List Opportunities","description":"Page through opportunities. Field data lives on the list entry, not here — read it through the list or saved view the opportunity belongs to.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the page to these opportunity IDs, e.g. [1, 2, 3]"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_persons":{"id":"affinity_list_persons","name":"Affinity List Persons","description":"Page through persons. Persons come back without field data unless Field IDs or Field Types asks for it.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the page to these person IDs, e.g. [1, 2, 3]"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_reminders":{"id":"affinity_list_reminders","name":"Affinity List Reminders","description":"Page through the reminders the caller can see. Filter by status to surface what is overdue.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_saved_view_entries":{"id":"affinity_list_saved_view_entries","name":"Affinity List Saved View Entries","description":"Page through the rows of a saved view. The view\'s own filters and columns decide which rows and which field data come back.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"viewId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The saved view ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_saved_views":{"id":"affinity_list_saved_views","name":"Affinity List Saved Views","description":"List the saved views on a list that the caller can view.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_list_transcript_fragments":{"id":"affinity_list_transcript_fragments","name":"Affinity List Transcript Fragments","description":"Page through everything said in a meeting, segment by segment with the speaker.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"transcriptId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The transcript ID"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_transcripts":{"id":"affinity_list_transcripts","name":"Affinity List Transcripts","description":"Page through meeting transcript metadata. Read one transcript to get what was actually said.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression, e.g. \\"createdAt>=2026-01-01\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_list_users":{"id":"affinity_list_users","name":"Affinity List Users","description":"Page through the internal users in the organization. Email addresses and roles are returned only to callers with the \\"Manage Users\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"term":{"type":"string","required":false,"visibility":"user-or-llm","description":"Case-insensitive match across first name, last name, and primary email"},"filter":{"type":"string","required":false,"visibility":"user-or-llm","description":"Affinity Filtering Language expression over id or status, e.g. \\"status=active\\""},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_search_companies":{"id":"affinity_search_companies","name":"Affinity Search Companies","description":"Search companies by filters, sorts, and a free-text term. Requires the \\"Export All Organizations directory\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Filter group as {operator: \\"and\\"|\\"or\\", filters: [...]}, at most 50 leaves. Each leaf is {valueType, fieldId, operator, value}, and a leaf may itself be a nested group"},"searchTerm":{"type":"string","required":false,"visibility":"user-or-llm","description":"Free-text term matched against the searchable fields. At least 3 characters"},"searchFieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs the search term is matched against. Defaults to the searchable fields"},"sorts":{"type":"json","required":false,"visibility":"user-or-llm","description":"Sort order as [{fieldId, direction: \\"asc\\"|\\"desc\\", attributeId?}], up to 5, applied in order"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_search_files":{"id":"affinity_search_files","name":"Affinity Search Files","description":"Search files by keyword, ordered by relevance. Narrow to specific files or to one company, or leave both unset to search the whole account.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"prompt":{"type":"string","required":true,"visibility":"user-or-llm","description":"What to search for. Between 3 and 500 characters"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the search to these file IDs. Cannot be combined with Company ID"},"companyId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Restrict the search to one company\'s files. Cannot be combined with file IDs"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of files to return, 1-100. Defaults to 20"}},"hostedApiKey":"none"},"affinity_search_list_entries":{"id":"affinity_search_list_entries","name":"Affinity Search List Entries","description":"Search the rows of one list by filters, sorts, and a free-text term. Requires the \\"Export data from Lists\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID to search"},"filters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Filter group as {operator: \\"and\\"|\\"or\\", filters: [...]}, at most 50 leaves. Each leaf is {valueType, fieldId, operator, value}, and a leaf may itself be a nested group"},"searchTerm":{"type":"string","required":false,"visibility":"user-or-llm","description":"Free-text term matched against the searchable fields. At least 3 characters"},"searchFieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs the search term is matched against. Defaults to the searchable fields"},"sorts":{"type":"json","required":false,"visibility":"user-or-llm","description":"Sort order as [{fieldId, direction: \\"asc\\"|\\"desc\\", attributeId?}], up to 5, applied in order"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, list, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_search_notes":{"id":"affinity_search_notes","name":"Affinity Search Notes","description":"Search notes by keyword, ordered by relevance. Narrow to specific notes or to one company, or leave both unset to search the whole account.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"prompt":{"type":"string","required":true,"visibility":"user-or-llm","description":"What to search for. Between 3 and 500 characters"},"ids":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the search to these note IDs. Cannot be combined with Company ID"},"companyId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Restrict the search to one company\'s notes. Cannot be combined with note IDs"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of notes to return, 1-100. Defaults to 20"}},"hostedApiKey":"none"},"affinity_search_persons":{"id":"affinity_search_persons","name":"Affinity Search Persons","description":"Search persons by filters, sorts, and a free-text term. Requires the \\"Export All People directory\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"filters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Filter group as {operator: \\"and\\"|\\"or\\", filters: [...]}, at most 50 leaves. Each leaf is {valueType, fieldId, operator, value}, and a leaf may itself be a nested group"},"searchTerm":{"type":"string","required":false,"visibility":"user-or-llm","description":"Free-text term matched against the searchable fields. At least 3 characters"},"searchFieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs the search term is matched against. Defaults to the searchable fields"},"sorts":{"type":"json","required":false,"visibility":"user-or-llm","description":"Sort order as [{fieldId, direction: \\"asc\\"|\\"desc\\", attributeId?}], up to 5, applied in order"},"fieldIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field IDs to return values for, e.g. [\\"affinity-data-location\\"]. Mutually exclusive with Field Types"},"fieldTypes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Field categories to return values for: enriched, global, or relationship-intelligence. Mutually exclusive with Field IDs"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous page, returned as nextCursor or prevCursor"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to return per page, 1-100. Defaults to 100"},"totalCount":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the total size of the collection. Costs an extra query"}},"hostedApiKey":"none"},"affinity_semantic_search":{"id":"affinity_semantic_search","name":"Affinity Semantic Search","description":"Find companies from a description in plain language — industry, technology, stage, or business model. Currently searches companies only.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"prompt":{"type":"string","required":true,"visibility":"user-or-llm","description":"What to look for, in plain language, e.g. \\"climate tech companies in our pipeline\\". Up to 500 characters"},"listIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Restrict the search to companies on these lists, e.g. [1, 2]"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of companies to return, 1-100. Defaults to 100"}},"hostedApiKey":"none"},"affinity_update_entity_field_value":{"id":"affinity_update_entity_field_value","name":"Affinity Update Entity Field Value","description":"Write one non-list field value on a company or person. The value type must match how the field is defined.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"entityType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Which entity to write the field on: companies or persons"},"entityId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of that company or person"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The field ID to write"},"value":{"type":"json","required":true,"visibility":"user-or-llm","description":"The new value as {type, data}, where type matches the field\'s value type. Examples: {\\"type\\":\\"text\\",\\"data\\":\\"Series B\\"}, {\\"type\\":\\"number\\",\\"data\\":42}, {\\"type\\":\\"dropdown\\",\\"data\\":{\\"dropdownOptionId\\":7}}, {\\"type\\":\\"person\\",\\"data\\":{\\"id\\":123}}, {\\"type\\":\\"person-multi\\",\\"data\\":[{\\"id\\":123}]}. Pass data as null to clear the field"}},"hostedApiKey":"none"},"affinity_update_list_entry_field":{"id":"affinity_update_list_entry_field","name":"Affinity Update List Entry Field","description":"Write one field value on a list row. Requires the \\"Export data from Lists\\" permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"listEntryId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list entry ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The field ID to write"},"value":{"type":"json","required":true,"visibility":"user-or-llm","description":"The new value as {type, data}, where type matches the field\'s value type. Examples: {\\"type\\":\\"text\\",\\"data\\":\\"Series B\\"}, {\\"type\\":\\"number\\",\\"data\\":42}, {\\"type\\":\\"dropdown\\",\\"data\\":{\\"dropdownOptionId\\":7}}, {\\"type\\":\\"person\\",\\"data\\":{\\"id\\":123}}, {\\"type\\":\\"person-multi\\",\\"data\\":[{\\"id\\":123}]}. Pass data as null to clear the field"}},"hostedApiKey":"none"},"affinity_update_list_field_dropdown_option":{"id":"affinity_update_list_field_dropdown_option","name":"Affinity Update List Field Dropdown Option","description":"Change a dropdown option on a list field. Every field is optional — supply only what should change, and only fields the option\'s kind actually has.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"listId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The list ID"},"fieldId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown field ID on that list"},"dropdownOptionId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The dropdown option ID to update"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Replacement option label. Supply at least one field to change"},"rank":{"type":"number","required":false,"visibility":"user-or-llm","description":"Sort order. Required on a ranked-dropdown or status-dropdown option"},"color":{"type":"string","required":false,"visibility":"user-or-llm","description":"Option color: white, gray, blue, green, purple, orange, or red. Required on a ranked-dropdown or status-dropdown option"},"statusCategory":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pipeline meaning of the option: open, won, lost, or on-hold. Status-dropdown options only"},"winRate":{"type":"number","required":false,"visibility":"user-or-llm","description":"Expected win rate of the status. Status-dropdown options only"}},"hostedApiKey":"none"},"affinity_update_note":{"id":"affinity_update_note","name":"Affinity Update Note","description":"Rewrite a note\'s body or replace which records it is attached to. Each list of IDs replaces that association wholesale, an empty list clears it, and omitting one leaves it untouched. A note\'s type cannot be changed.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Affinity API key, sent as a bearer token"},"noteId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note ID to update"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"Replacement note body as HTML"},"companyIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Replacement set of attached companies, e.g. [1, 2]. Send [] to detach every company; omit to leave them unchanged"},"personIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Replacement set of attached persons, e.g. [1, 2]. Send [] to detach every person; omit to leave them unchanged"},"opportunityIds":{"type":"json","required":false,"visibility":"user-or-llm","description":"Replacement set of attached opportunities, e.g. [1, 2]. Send [] to detach every opportunity; omit to leave them unchanged"}},"hostedApiKey":"none"},"agentmail_create_draft":{"id":"agentmail_create_draft","name":"Create Draft","description":"Create a new email draft in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to create the draft in"},"to":{"type":"string","required":false,"visibility":"user-or-llm","description":"Recipient email addresses (comma-separated)"},"subject":{"type":"string","required":false,"visibility":"user-or-llm","description":"Draft subject line"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Plain text draft body"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"HTML draft body"},"cc":{"type":"string","required":false,"visibility":"user-or-llm","description":"CC recipient email addresses (comma-separated)"},"bcc":{"type":"string","required":false,"visibility":"user-or-llm","description":"BCC recipient email addresses (comma-separated)"},"inReplyTo":{"type":"string","required":false,"visibility":"user-or-llm","description":"ID of message being replied to"},"sendAt":{"type":"string","required":false,"visibility":"user-or-llm","description":"ISO 8601 timestamp to schedule sending"}},"hostedApiKey":"none"},"agentmail_create_inbox":{"id":"agentmail_create_inbox","name":"Create Inbox","description":"Create a new email inbox with AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"username":{"type":"string","required":false,"visibility":"user-or-llm","description":"Username for the inbox email address"},"domain":{"type":"string","required":false,"visibility":"user-or-llm","description":"Domain for the inbox email address"},"displayName":{"type":"string","required":false,"visibility":"user-or-llm","description":"Display name for the inbox"}},"hostedApiKey":"none"},"agentmail_delete_draft":{"id":"agentmail_delete_draft","name":"Delete Draft","description":"Delete an email draft in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the draft"},"draftId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the draft to delete"}},"hostedApiKey":"none"},"agentmail_delete_inbox":{"id":"agentmail_delete_inbox","name":"Delete Inbox","description":"Delete an email inbox in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to delete"}},"hostedApiKey":"none"},"agentmail_delete_thread":{"id":"agentmail_delete_thread","name":"Delete Thread","description":"Delete an email thread in AgentMail (moves to trash, or permanently deletes if already in trash)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the thread"},"threadId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the thread to delete"},"permanent":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Force permanent deletion instead of moving to trash"}},"hostedApiKey":"none"},"agentmail_forward_message":{"id":"agentmail_forward_message","name":"Forward Message","description":"Forward an email message to new recipients in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the message"},"messageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the message to forward"},"to":{"type":"string","required":true,"visibility":"user-or-llm","description":"Recipient email addresses (comma-separated)"},"subject":{"type":"string","required":false,"visibility":"user-or-llm","description":"Override subject line"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Additional plain text to prepend"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"Additional HTML to prepend"},"cc":{"type":"string","required":false,"visibility":"user-or-llm","description":"CC recipient email addresses (comma-separated)"},"bcc":{"type":"string","required":false,"visibility":"user-or-llm","description":"BCC recipient email addresses (comma-separated)"}},"hostedApiKey":"none"},"agentmail_get_draft":{"id":"agentmail_get_draft","name":"Get Draft","description":"Get details of a specific email draft in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox the draft belongs to"},"draftId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the draft to retrieve"}},"hostedApiKey":"none"},"agentmail_get_inbox":{"id":"agentmail_get_inbox","name":"Get Inbox","description":"Get details of a specific email inbox in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to retrieve"}},"hostedApiKey":"none"},"agentmail_get_message":{"id":"agentmail_get_message","name":"Get Message","description":"Get details of a specific email message in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the message"},"messageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the message to retrieve"}},"hostedApiKey":"none"},"agentmail_get_thread":{"id":"agentmail_get_thread","name":"Get Thread","description":"Get details of a specific email thread including messages in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the thread"},"threadId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the thread to retrieve"}},"hostedApiKey":"none"},"agentmail_list_drafts":{"id":"agentmail_list_drafts","name":"List Drafts","description":"List email drafts in an inbox in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to list drafts from"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of drafts to return"},"pageToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token for next page of results"}},"hostedApiKey":"none"},"agentmail_list_inboxes":{"id":"agentmail_list_inboxes","name":"List Inboxes","description":"List all email inboxes in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of inboxes to return"},"pageToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token for next page of results"}},"hostedApiKey":"none"},"agentmail_list_messages":{"id":"agentmail_list_messages","name":"List Messages","description":"List messages in an inbox in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to list messages from"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of messages to return"},"pageToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token for next page of results"}},"hostedApiKey":"none"},"agentmail_list_threads":{"id":"agentmail_list_threads","name":"List Threads","description":"List email threads in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to list threads from"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of threads to return"},"pageToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token for next page of results"},"labels":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated labels to filter threads by"},"before":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter threads before this ISO 8601 timestamp"},"after":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter threads after this ISO 8601 timestamp"}},"hostedApiKey":"none"},"agentmail_reply_message":{"id":"agentmail_reply_message","name":"Reply to Message","description":"Reply to an existing email message in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to reply from"},"messageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the message to reply to"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Plain text reply body"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"HTML reply body"},"to":{"type":"string","required":false,"visibility":"user-or-llm","description":"Override recipient email addresses (comma-separated)"},"cc":{"type":"string","required":false,"visibility":"user-or-llm","description":"CC email addresses (comma-separated)"},"bcc":{"type":"string","required":false,"visibility":"user-or-llm","description":"BCC email addresses (comma-separated)"},"replyAll":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Reply to all recipients of the original message"}},"hostedApiKey":"none"},"agentmail_send_draft":{"id":"agentmail_send_draft","name":"Send Draft","description":"Send an existing email draft in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the draft"},"draftId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the draft to send"}},"hostedApiKey":"none"},"agentmail_send_message":{"id":"agentmail_send_message","name":"Send Message","description":"Send an email message from an AgentMail inbox","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to send from"},"to":{"type":"string","required":true,"visibility":"user-or-llm","description":"Recipient email address (comma-separated for multiple)"},"subject":{"type":"string","required":true,"visibility":"user-or-llm","description":"Email subject line"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Plain text email body"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"HTML email body"},"cc":{"type":"string","required":false,"visibility":"user-or-llm","description":"CC recipient email addresses (comma-separated)"},"bcc":{"type":"string","required":false,"visibility":"user-or-llm","description":"BCC recipient email addresses (comma-separated)"}},"hostedApiKey":"none"},"agentmail_update_draft":{"id":"agentmail_update_draft","name":"Update Draft","description":"Update an existing email draft in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the draft"},"draftId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the draft to update"},"to":{"type":"string","required":false,"visibility":"user-or-llm","description":"Recipient email addresses (comma-separated)"},"subject":{"type":"string","required":false,"visibility":"user-or-llm","description":"Draft subject line"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Plain text draft body"},"html":{"type":"string","required":false,"visibility":"user-or-llm","description":"HTML draft body"},"cc":{"type":"string","required":false,"visibility":"user-or-llm","description":"CC recipient email addresses (comma-separated)"},"bcc":{"type":"string","required":false,"visibility":"user-or-llm","description":"BCC recipient email addresses (comma-separated)"},"sendAt":{"type":"string","required":false,"visibility":"user-or-llm","description":"ISO 8601 timestamp to schedule sending"}},"hostedApiKey":"none"},"agentmail_update_inbox":{"id":"agentmail_update_inbox","name":"Update Inbox","description":"Update the display name of an email inbox in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox to update"},"displayName":{"type":"string","required":true,"visibility":"user-or-llm","description":"New display name for the inbox"}},"hostedApiKey":"none"},"agentmail_update_message":{"id":"agentmail_update_message","name":"Update Message","description":"Add or remove labels on an email message in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the message"},"messageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the message to update"},"addLabels":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated labels to add to the message"},"removeLabels":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated labels to remove from the message"}},"hostedApiKey":"none"},"agentmail_update_thread":{"id":"agentmail_update_thread","name":"Update Thread Labels","description":"Add or remove labels on an email thread in AgentMail","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentMail API key"},"inboxId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the inbox containing the thread"},"threadId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the thread to update"},"addLabels":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated labels to add to the thread"},"removeLabels":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated labels to remove from the thread"}},"hostedApiKey":"none"},"agentphone_create_call":{"id":"agentphone_create_call","name":"Create Outbound Call","description":"Initiate an outbound voice call from an AgentPhone agent","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"agentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Agent that will handle the call"},"toNumber":{"type":"string","required":true,"visibility":"user-or-llm","description":"Phone number to call in E.164 format (e.g. +14155551234)"},"fromNumberId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Phone number ID to use as caller ID. Must belong to the agent. If omitted, the agent\'s first assigned number is used."},"initialGreeting":{"type":"string","required":false,"visibility":"user-or-llm","description":"Optional greeting spoken when the recipient answers"},"voice":{"type":"string","required":false,"visibility":"user-or-llm","description":"Voice ID override for this call (defaults to the agent\'s configured voice)"},"systemPrompt":{"type":"string","required":false,"visibility":"user-or-llm","description":"When provided, uses a built-in LLM for the conversation instead of forwarding to your webhook"}},"hostedApiKey":"none"},"agentphone_create_contact":{"id":"agentphone_create_contact","name":"Create Contact","description":"Create a new contact in AgentPhone","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"phoneNumber":{"type":"string","required":true,"visibility":"user-or-llm","description":"Phone number in E.164 format (e.g. +14155551234)"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Contact\'s full name"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"Contact\'s email address"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"Freeform notes stored on the contact"}},"hostedApiKey":"none"},"agentphone_create_number":{"id":"agentphone_create_number","name":"Create Phone Number","description":"Provision a new SMS- and voice-enabled phone number","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Two-letter country code (e.g. US, CA). Defaults to US."},"areaCode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Preferred area code (US/CA only, e.g. \\"415\\"). Best-effort — may be ignored if unavailable."},"agentId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Optionally attach the number to an agent immediately"}},"hostedApiKey":"none"},"agentphone_delete_contact":{"id":"agentphone_delete_contact","name":"Delete Contact","description":"Delete a contact by ID","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"contactId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Contact ID"}},"hostedApiKey":"none"},"agentphone_get_call":{"id":"agentphone_get_call","name":"Get Call","description":"Fetch a call and its full transcript","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"callId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the call to retrieve"}},"hostedApiKey":"none"},"agentphone_get_call_transcript":{"id":"agentphone_get_call_transcript","name":"Get Call Transcript","description":"Get the full ordered transcript for a call","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"callId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the call to retrieve the transcript for"}},"hostedApiKey":"none"},"agentphone_get_contact":{"id":"agentphone_get_contact","name":"Get Contact","description":"Fetch a single contact by ID","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"contactId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Contact ID"}},"hostedApiKey":"none"},"agentphone_get_conversation":{"id":"agentphone_get_conversation","name":"Get Conversation","description":"Get a conversation along with its recent messages","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"conversationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Conversation ID"},"messageLimit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of recent messages to include (default 50, max 100)"}},"hostedApiKey":"none"},"agentphone_get_conversation_messages":{"id":"agentphone_get_conversation_messages","name":"Get Conversation Messages","description":"Get paginated messages for a conversation","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"conversationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Conversation ID"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of messages to return (default 50, max 200)"},"before":{"type":"string","required":false,"visibility":"user-or-llm","description":"Return messages received before this ISO 8601 timestamp"},"after":{"type":"string","required":false,"visibility":"user-or-llm","description":"Return messages received after this ISO 8601 timestamp"}},"hostedApiKey":"none"},"agentphone_get_number_messages":{"id":"agentphone_get_number_messages","name":"Get Phone Number Messages","description":"Fetch messages received on a specific phone number","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"numberId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the phone number"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of messages to return (default 50, max 200)"},"before":{"type":"string","required":false,"visibility":"user-or-llm","description":"Return messages received before this ISO 8601 timestamp"},"after":{"type":"string","required":false,"visibility":"user-or-llm","description":"Return messages received after this ISO 8601 timestamp"}},"hostedApiKey":"none"},"agentphone_get_usage":{"id":"agentphone_get_usage","name":"Get Usage","description":"Retrieve current usage statistics for the AgentPhone account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"}},"hostedApiKey":"none"},"agentphone_get_usage_daily":{"id":"agentphone_get_usage_daily","name":"Get Daily Usage","description":"Get a daily breakdown of usage (messages, calls, webhooks) for the last N days","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"days":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of days to return (1-365, default 30)"}},"hostedApiKey":"none"},"agentphone_get_usage_monthly":{"id":"agentphone_get_usage_monthly","name":"Get Monthly Usage","description":"Get monthly usage aggregation (messages, calls, webhooks) for the last N months","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"months":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of months to return (1-24, default 6)"}},"hostedApiKey":"none"},"agentphone_list_calls":{"id":"agentphone_list_calls","name":"List Calls","description":"List voice calls for this AgentPhone account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to return (default 20, max 100)"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to skip (min 0)"},"status":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter by status (completed, in-progress, failed)"},"direction":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter by direction (inbound, outbound)"},"type":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter by call type (pstn, web)"},"search":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search by phone number (matches fromNumber or toNumber)"}},"hostedApiKey":"none"},"agentphone_list_contacts":{"id":"agentphone_list_contacts","name":"List Contacts","description":"List contacts for this AgentPhone account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"search":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter by name or phone number (case-insensitive contains)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to return (default 50, max 200)"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to skip (min 0)"}},"hostedApiKey":"none"},"agentphone_list_conversations":{"id":"agentphone_list_conversations","name":"List Conversations","description":"List conversations (message threads) for this AgentPhone account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to return (default 20, max 100)"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to skip (min 0)"}},"hostedApiKey":"none"},"agentphone_list_numbers":{"id":"agentphone_list_numbers","name":"List Phone Numbers","description":"List all phone numbers provisioned for this AgentPhone account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to return (default 20, max 100)"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to skip (min 0)"}},"hostedApiKey":"none"},"agentphone_react_to_message":{"id":"agentphone_react_to_message","name":"React to Message","description":"Send an iMessage tapback reaction to a message (iMessage only)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"messageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the message to react to"},"reaction":{"type":"string","required":true,"visibility":"user-or-llm","description":"Reaction type: love, like, dislike, laugh, emphasize, or question"}},"hostedApiKey":"none"},"agentphone_release_number":{"id":"agentphone_release_number","name":"Release Phone Number","description":"Release (delete) a phone number. This action is irreversible.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"numberId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the phone number to release"}},"hostedApiKey":"none"},"agentphone_send_message":{"id":"agentphone_send_message","name":"Send Message","description":"Send an outbound SMS or iMessage from an AgentPhone agent","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"agentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Agent sending the message"},"toNumber":{"type":"string","required":true,"visibility":"user-or-llm","description":"Recipient phone number in E.164 format (e.g. +14155551234)"},"body":{"type":"string","required":true,"visibility":"user-or-llm","description":"Message text to send"},"mediaUrl":{"type":"string","required":false,"visibility":"user-or-llm","description":"Optional URL of an image, video, or file to attach"},"numberId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Phone number ID to send from. If omitted, the agent\'s first assigned number is used."}},"hostedApiKey":"none"},"agentphone_update_contact":{"id":"agentphone_update_contact","name":"Update Contact","description":"Update a contact\'s fields","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"contactId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Contact ID"},"phoneNumber":{"type":"string","required":false,"visibility":"user-or-llm","description":"New phone number in E.164 format"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"New contact name"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"New email address"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"New freeform notes"}},"hostedApiKey":"none"},"agentphone_update_conversation":{"id":"agentphone_update_conversation","name":"Update Conversation","description":"Update conversation metadata (stored state). Pass null to clear existing metadata.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"AgentPhone API key"},"conversationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Conversation ID"},"metadata":{"type":"json","required":false,"visibility":"user-or-llm","description":"Custom key-value metadata to store on the conversation. Pass null to clear existing metadata."}},"hostedApiKey":"none"},"agiloft_async_status":{"id":"agiloft_async_status","name":"Agiloft Async Status","description":"Check whether an asynchronous Agiloft call, such as a run action button, has completed.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table the asynchronous call was made against"},"callbackId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Callback ID returned by the asynchronous call, e.g. from Run Action Button"}},"hostedApiKey":"none"},"agiloft_attach_file":{"id":"agiloft_attach_file","name":"Agiloft Attach File","description":"Attach a file to a field in an Agiloft record.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to attach the file to"},"fieldName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the attachment field"},"file":{"type":"file","required":true,"visibility":"user-or-llm","description":"File to attach"},"fileName":{"type":"string","required":false,"visibility":"user-or-llm","description":"Name to assign to the file (defaults to original file name)"},"overwrite":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Replace the contents of the field instead of adding another file to it"}},"hostedApiKey":"none"},"agiloft_attachment_info":{"id":"agiloft_attachment_info","name":"Agiloft Attachment Info","description":"Get information about file attachments on a record field.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to check attachments on"},"fieldName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the attachment field to inspect"}},"hostedApiKey":"none"},"agiloft_create_record":{"id":"agiloft_create_record","name":"Agiloft Create Record","description":"Create a new record in an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"data":{"type":"string","required":true,"visibility":"user-or-llm","description":"Record field values as a JSON object (e.g., {\\"first_name\\": \\"John\\", \\"status\\": \\"Active\\"})"}},"hostedApiKey":"none"},"agiloft_delete_record":{"id":"agiloft_delete_record","name":"Agiloft Delete Record","description":"Delete a record from an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to delete"},"substituteIds":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated IDs of records that adopt the dependants of the deleted record. Read only when the delete rule is REPLACE_WITH_ANOTHER."},"deleteRule":{"type":"string","required":false,"visibility":"user-or-llm","description":"How to treat records that depend on this one: ERROR_IF_DEPENDANTS (default — fails rather than cascading), APPLY_DELETE_WHERE_POSSIBLE, DELETE_WHERE_POSSIBLE_OTHERWISE_UNLINK, APPLY_UNLINK, UNLINK_WHERE_POSSIBLE_OTHERWISE_DELETE, or REPLACE_WITH_ANOTHER"}},"hostedApiKey":"none"},"agiloft_get_choice_line_id":{"id":"agiloft_get_choice_line_id","name":"Agiloft Get Choice Line ID","description":"Resolve the internal numeric ID of a choice-list value, for use in EWSelect WHERE clauses against choice fields.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"case\\", \\"contracts\\")"},"fieldName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Choice field name (e.g., \\"priority\\", \\"status\\")"},"value":{"type":"string","required":true,"visibility":"user-or-llm","description":"Choice display value to resolve (e.g., \\"High\\", \\"Active\\")"}},"hostedApiKey":"none"},"agiloft_list_tables":{"id":"agiloft_list_tables","name":"Agiloft List Tables","description":"List the tables and fields in an Agiloft knowledge base, to discover the logical names other operations need.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":false,"visibility":"user-or-llm","description":"Logical name of a single table to describe (e.g., \\"contacts\\"). Leave empty to list every table in the knowledge base."},"includeLinkedInfo":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Include the source table and column behind each linked field"},"skipColumnsInfo":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Return table names only, omitting field details, for a much smaller response"}},"hostedApiKey":"none"},"agiloft_lock_record":{"id":"agiloft_lock_record","name":"Agiloft Lock Record","description":"Lock, unlock, or check the lock status of an Agiloft record.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to lock, unlock, or check"},"lockAction":{"type":"string","required":true,"visibility":"user-or-llm","description":"Action to perform: \\"lock\\", \\"unlock\\", or \\"check\\""},"force":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Unlock only: release a lock held by another user."}},"hostedApiKey":"none"},"agiloft_nlp_search":{"id":"agiloft_nlp_search","name":"Agiloft Natural Language Search","description":"Search Agiloft records by describing what you want in plain language, such as \\"active NDAs submitted last month\\".","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"nlpQuery":{"type":"string","required":true,"visibility":"user-or-llm","description":"The request in plain language, e.g. \\"Show me open, high-priority contracts\\". Structured field filters are not accepted — use Search Records for those."},"fields":{"type":"string","required":true,"visibility":"user-or-llm","description":"Comma-separated field names to return, e.g. \\"id, contract_title1, company_name\\""},"page":{"type":"string","required":false,"visibility":"user-or-llm","description":"Page number, starting from 0"},"limit":{"type":"string","required":false,"visibility":"user-or-llm","description":"Records per page"}},"hostedApiKey":"none"},"agiloft_read_record":{"id":"agiloft_read_record","name":"Agiloft Read Record","description":"Read a record by ID from an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to read"},"fields":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of field names to include in the response"}},"hostedApiKey":"none"},"agiloft_remove_attachment":{"id":"agiloft_remove_attachment","name":"Agiloft Remove Attachment","description":"Remove an attached file from a field in an Agiloft record.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record containing the attachment"},"fieldName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the attachment field"},"position":{"type":"string","required":true,"visibility":"user-or-llm","description":"Position index of the file to remove (starting from 0)"}},"hostedApiKey":"none"},"agiloft_retrieve_attachment":{"id":"agiloft_retrieve_attachment","name":"Agiloft Retrieve Attachment","description":"Download an attached file from an Agiloft record field.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record containing the attachment"},"fieldName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the attachment field"},"position":{"type":"string","required":true,"visibility":"user-or-llm","description":"Position index of the file in the field (starting from 0)"}},"hostedApiKey":"none"},"agiloft_run_action_button":{"id":"agiloft_run_action_button","name":"Agiloft Run Action Button","description":"Run an action button on an Agiloft record, such as an approval or send-for-signature step.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"case\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to run the action button on"},"actionButtonField":{"type":"string","required":true,"visibility":"user-or-llm","description":"Logical name of the field holding the action button (e.g., \\"ab_field\\")"}},"hostedApiKey":"none"},"agiloft_saved_search":{"id":"agiloft_saved_search","name":"Agiloft Saved Search","description":"List the saved searches defined for an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Logical table name to list saved searches for (e.g., \\"contract\\")"}},"hostedApiKey":"none"},"agiloft_search_records":{"id":"agiloft_search_records","name":"Agiloft Search Records","description":"Search for records in an Agiloft table using a query.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name to search in (e.g., \\"contracts\\", \\"contacts.employees\\")"},"query":{"type":"string","required":false,"visibility":"user-or-llm","description":"Ad hoc EWSearch query. Combine conditions with && (and) or || (or) and quote every value — e.g. \\"summary~=\'test\'&&priority=\'High\'\\". Required unless a saved search is given."},"search":{"type":"string","required":false,"visibility":"user-or-llm","description":"Label of a saved search defined on the table (e.g., \\"C: Status is Closed\\"). Can be combined with a query to narrow it further."},"fields":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of field names to include in the results"},"page":{"type":"string","required":false,"visibility":"user-or-llm","description":"Page number for paginated results (starting from 0)"},"limit":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum number of records to return per page. Agiloft treats 0 as \\"all records\\", so leave it unset or use a positive value to keep result sizes bounded."}},"hostedApiKey":"none"},"agiloft_select_records":{"id":"agiloft_select_records","name":"Agiloft Select Records","description":"Select record IDs matching a SQL WHERE clause from an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"where":{"type":"string","required":true,"visibility":"user-or-llm","description":"SQL WHERE clause using database column names (e.g., \\"summary like \'%new%\'\\" or \\"assigned_person=\'John Doe\'\\"). EWSelect has no page size and returns every matching ID, so append a database limit such as \\"limit 0,200\\" to bound the result."}},"hostedApiKey":"none"},"agiloft_update_record":{"id":"agiloft_update_record","name":"Agiloft Update Record","description":"Update an existing record in an Agiloft table.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the record to update"},"data":{"type":"string","required":true,"visibility":"user-or-llm","description":"Updated field values as a JSON object (e.g., {\\"status\\": \\"Active\\", \\"priority\\": \\"High\\"})"}},"hostedApiKey":"none"},"agiloft_upsert_record":{"id":"agiloft_upsert_record","name":"Agiloft Upsert Record","description":"Create an Agiloft record, or update it when a record already matches the given fields.","version":"1.0.0","params":{"instanceUrl":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft instance URL (e.g., https://mycompany.agiloft.com)"},"knowledgeBase":{"type":"string","required":true,"visibility":"user-only","description":"Knowledge base name"},"login":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft username"},"password":{"type":"string","required":true,"visibility":"user-only","description":"Agiloft password"},"table":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table name (e.g., \\"contracts\\", \\"contacts.employees\\")"},"match":{"type":"string","required":true,"visibility":"user-or-llm","description":"Field used to find an existing record (e.g., \\"ext_id\\"). Pick something that identifies a record uniquely — if more than one record matches, Agiloft writes nothing and returns a conflict."},"async":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Queue the write instead of waiting for it. Returns a callback ID instead of a record ID; pass that to Async Status to poll the result."},"data":{"type":"string","required":true,"visibility":"user-or-llm","description":"Field values as a JSON object. On create these populate the new record; on update only the supplied fields change."}},"hostedApiKey":"none"},"ahrefs_anchors":{"id":"ahrefs_anchors","name":"Ahrefs Anchors","description":"Get the anchor text distribution for a target domain or URL\'s backlinks, showing how many links and referring domains use each anchor text.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"history":{"type":"string","required":false,"visibility":"user-or-llm","description":"Historical scope: \\"live\\" (currently live), \\"all_time\\" (default, includes lost backlinks), or \\"since:YYYY-MM-DD\\" (backlinks found since a date)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_backlinks":{"id":"ahrefs_backlinks","name":"Ahrefs Backlinks","description":"Get a list of backlinks pointing to a target domain or URL. Returns details about each backlink including source URL, anchor text, and domain rating.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"history":{"type":"string","required":false,"visibility":"user-or-llm","description":"Historical scope: \\"live\\" (currently live backlinks), \\"all_time\\" (default, includes lost backlinks), or \\"since:YYYY-MM-DD\\" (backlinks found since a date)."},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_backlinks_stats":{"id":"ahrefs_backlinks_stats","name":"Ahrefs Backlinks Stats","description":"Get backlink and referring domain totals for a target domain or URL, both currently live and across all time.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_batch_analysis":{"id":"ahrefs_batch_analysis","name":"Ahrefs Batch Analysis","description":"Get bulk SEO metrics (Domain Rating, backlinks, referring domains, organic traffic, and more) for multiple domains or URLs in a single request. Useful for comparing many competitors at once.","version":"1.0.0","params":{"targets":{"type":"string","required":true,"visibility":"user-or-llm","description":"Comma-separated list of domains or URLs to analyze. Example: \\"example.com,competitor.com\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode applied to every target: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"protocol":{"type":"string","required":false,"visibility":"user-or-llm","description":"Protocol applied to every target: \\"both\\" (default), \\"http\\", or \\"https\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for traffic data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"volumeMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search volume calculation: \\"monthly\\" or \\"average\\" (default: \\"monthly\\")"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_broken_backlinks":{"id":"ahrefs_broken_backlinks","name":"Ahrefs Broken Backlinks","description":"Get a list of broken backlinks pointing to a target domain or URL. Useful for identifying link reclamation opportunities.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_domain_rating":{"id":"ahrefs_domain_rating","name":"Ahrefs Domain Rating","description":"Get the Domain Rating (DR) and Ahrefs Rank for a target domain. Domain Rating shows the strength of a website\'s backlink profile on a scale from 0 to 100.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain to analyze (e.g., example.com)"},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date for historical data in YYYY-MM-DD format (defaults to today)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_domain_rating_history":{"id":"ahrefs_domain_rating_history","name":"Ahrefs Domain Rating History","description":"Get the historical Domain Rating (DR) trend for a target domain or URL over a date range, grouped daily, weekly, or monthly.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"dateFrom":{"type":"string","required":true,"visibility":"user-only","description":"Start date of the historical period, in YYYY-MM-DD format"},"dateTo":{"type":"string","required":false,"visibility":"user-only","description":"End date of the historical period, in YYYY-MM-DD format (defaults to today)"},"historyGrouping":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval for grouping data points: \\"daily\\", \\"weekly\\", or \\"monthly\\" (default: \\"monthly\\")"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_keyword_overview":{"id":"ahrefs_keyword_overview","name":"Ahrefs Keyword Overview","description":"Get detailed metrics for a keyword including search volume, keyword difficulty, CPC, clicks, and traffic potential.","version":"1.0.0","params":{"keyword":{"type":"string","required":true,"visibility":"user-or-llm","description":"The keyword to analyze"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for keyword data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_keywords_history":{"id":"ahrefs_keywords_history","name":"Ahrefs Keywords History","description":"Get the historical organic keyword ranking distribution for a target domain or URL over a date range: how many keywords rank in each position bucket at each point in time.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"dateFrom":{"type":"string","required":true,"visibility":"user-only","description":"Start date of the historical period, in YYYY-MM-DD format"},"dateTo":{"type":"string","required":false,"visibility":"user-only","description":"End date of the historical period, in YYYY-MM-DD format (defaults to today)"},"historyGrouping":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval for grouping data points: \\"daily\\", \\"weekly\\", or \\"monthly\\" (default: \\"monthly\\")"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for search results. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_metrics":{"id":"ahrefs_metrics","name":"Ahrefs Metrics","description":"Get a one-call organic and paid search overview for a target domain or URL: organic traffic, organic keywords, paid traffic, paid keywords, and estimated traffic cost.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for traffic data. Example: \\"us\\", \\"gb\\", \\"de\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_metrics_history":{"id":"ahrefs_metrics_history","name":"Ahrefs Metrics History","description":"Get the historical organic and paid traffic trend for a target domain or URL over a date range: organic traffic/cost and paid traffic/cost at each point in time.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"dateFrom":{"type":"string","required":true,"visibility":"user-only","description":"Start date of the historical period, in YYYY-MM-DD format"},"dateTo":{"type":"string","required":false,"visibility":"user-only","description":"End date of the historical period, in YYYY-MM-DD format (defaults to today)"},"volumeMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search volume calculation: \\"monthly\\" or \\"average\\" (default: \\"monthly\\")"},"historyGrouping":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval for grouping data points: \\"daily\\", \\"weekly\\", or \\"monthly\\" (default: \\"monthly\\")"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for traffic data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_organic_competitors":{"id":"ahrefs_organic_competitors","name":"Ahrefs Organic Competitors","description":"Get domains that compete with a target domain or URL for the same organic keywords, ranked by keyword overlap.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for search results. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_organic_keywords":{"id":"ahrefs_organic_keywords","name":"Ahrefs Organic Keywords","description":"Get organic keywords that a target domain or URL ranks for in Google search results. Returns keyword details including search volume, ranking position, and estimated traffic.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for search results. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_paid_pages":{"id":"ahrefs_paid_pages","name":"Ahrefs Paid Pages","description":"Get a target domain\'s pages that receive paid search traffic, sorted by estimated paid traffic. Returns page URLs with their paid traffic, keyword counts, and estimated spend.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for traffic data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_rank_tracker_competitors_overview":{"id":"ahrefs_rank_tracker_competitors_overview","name":"Ahrefs Rank Tracker Competitors Overview","description":"Get competitor rankings for the keywords tracked in an Ahrefs Rank Tracker project: each tracked keyword\'s volume and difficulty alongside every competitor\'s position, traffic, and traffic value. This endpoint is free and does not consume API units.","version":"1.0.0","params":{"projectId":{"type":"number","required":true,"visibility":"user-or-llm","description":"The Rank Tracker project ID (found in the project URL in Ahrefs)"},"date":{"type":"string","required":true,"visibility":"user-only","description":"Date to report rankings for, in YYYY-MM-DD format"},"device":{"type":"string","required":true,"visibility":"user-or-llm","description":"Rankings device type: \\"desktop\\" or \\"mobile\\""},"dateCompared":{"type":"string","required":false,"visibility":"user-only","description":"Comparison date in YYYY-MM-DD format, to compute position/traffic deltas"},"volumeMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search volume calculation: \\"monthly\\" or \\"average\\" (default: \\"monthly\\")"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_rank_tracker_competitors_stats":{"id":"ahrefs_rank_tracker_competitors_stats","name":"Ahrefs Rank Tracker Competitors Stats","description":"Get aggregate competitor stats for an Ahrefs Rank Tracker project: each competitor\'s traffic, traffic value, average position, and share of voice across all tracked keywords. This endpoint is free and does not consume API units.","version":"1.0.0","params":{"projectId":{"type":"number","required":true,"visibility":"user-or-llm","description":"The Rank Tracker project ID (found in the project URL in Ahrefs)"},"date":{"type":"string","required":true,"visibility":"user-only","description":"Date to report metrics for, in YYYY-MM-DD format"},"device":{"type":"string","required":true,"visibility":"user-or-llm","description":"Rankings device type: \\"desktop\\" or \\"mobile\\""},"volumeMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search volume calculation: \\"monthly\\" or \\"average\\" (default: \\"monthly\\")"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_rank_tracker_overview":{"id":"ahrefs_rank_tracker_overview","name":"Ahrefs Rank Tracker Overview","description":"Get ranking overview metrics for the keywords tracked in an Ahrefs Rank Tracker project: position, search volume, keyword difficulty, and estimated traffic. This endpoint is free and does not consume API units.","version":"1.0.0","params":{"projectId":{"type":"number","required":true,"visibility":"user-or-llm","description":"The Rank Tracker project ID (found in the project URL in Ahrefs)"},"date":{"type":"string","required":true,"visibility":"user-only","description":"Date to report rankings for, in YYYY-MM-DD format"},"device":{"type":"string","required":true,"visibility":"user-or-llm","description":"Rankings device type: \\"desktop\\" or \\"mobile\\""},"dateCompared":{"type":"string","required":false,"visibility":"user-only","description":"Comparison date in YYYY-MM-DD format, to compute position/traffic deltas"},"volumeMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search volume calculation: \\"monthly\\" or \\"average\\" (default: \\"monthly\\")"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_rank_tracker_serp_overview":{"id":"ahrefs_rank_tracker_serp_overview","name":"Ahrefs Rank Tracker SERP Overview","description":"Get the full SERP (search engine results page) for a keyword tracked in an Ahrefs Rank Tracker project, including every ranking URL with its position, title, and authority metrics. This endpoint is free and does not consume API units.","version":"1.0.0","params":{"projectId":{"type":"number","required":true,"visibility":"user-or-llm","description":"The Rank Tracker project ID (found in the project URL in Ahrefs)"},"keyword":{"type":"string","required":true,"visibility":"user-or-llm","description":"The tracked keyword to retrieve SERP data for"},"country":{"type":"string","required":true,"visibility":"user-or-llm","description":"Country code for the tracked keyword. Example: \\"us\\", \\"gb\\", \\"de\\""},"device":{"type":"string","required":true,"visibility":"user-or-llm","description":"Rankings device type: \\"desktop\\" or \\"mobile\\""},"topPositions":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of top organic positions to return (defaults to all available)"},"date":{"type":"string","required":false,"visibility":"user-only","description":"Timestamp to return the last available SERP Overview at, in YYYY-MM-DDThh:mm:ss format"},"locationId":{"type":"number","required":false,"visibility":"user-or-llm","description":"Location ID of the tracked keyword, if tracked at a specific location"},"languageCode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Language code of the tracked keyword"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_refdomains_history":{"id":"ahrefs_refdomains_history","name":"Ahrefs Referring Domains History","description":"Get the historical referring domains trend for a target domain or URL over a date range, grouped daily, weekly, or monthly.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\""},"dateFrom":{"type":"string","required":true,"visibility":"user-only","description":"Start date of the historical period, in YYYY-MM-DD format"},"dateTo":{"type":"string","required":false,"visibility":"user-only","description":"End date of the historical period, in YYYY-MM-DD format (defaults to today)"},"historyGrouping":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval for grouping data points: \\"daily\\", \\"weekly\\", or \\"monthly\\" (default: \\"monthly\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_referring_domains":{"id":"ahrefs_referring_domains","name":"Ahrefs Referring Domains","description":"Get a list of domains that link to a target domain or URL. Returns unique referring domains with their domain rating, backlink counts, and discovery dates.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain or URL to analyze. Example: \\"example.com\\" or \\"https://example.com/page\\""},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"history":{"type":"string","required":false,"visibility":"user-or-llm","description":"Historical scope: \\"live\\" (currently live), \\"all_time\\" (default, includes lost domains), or \\"since:YYYY-MM-DD\\" (domains found since a date)."},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_related_terms":{"id":"ahrefs_related_terms","name":"Ahrefs Related Terms","description":"Get keyword ideas related to a seed keyword: terms the same top-ranking pages also rank for (\\"also rank for\\") or also discuss (\\"also talk about\\"), with volume, difficulty, and CPC.","version":"1.0.0","params":{"keyword":{"type":"string","required":true,"visibility":"user-or-llm","description":"The seed keyword to find related terms for"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for keyword data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"terms":{"type":"string","required":false,"visibility":"user-or-llm","description":"Type of related keywords to return: \\"also_rank_for\\", \\"also_talk_about\\", or \\"all\\" (default: \\"all\\")"},"viewFor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Whether to derive related terms from the top 10 or top 100 ranking pages (default: \\"top_10\\")"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_site_audit_page_explorer":{"id":"ahrefs_site_audit_page_explorer","name":"Ahrefs Site Audit Page Explorer","description":"Get crawled pages from an Ahrefs Site Audit project with health and SEO metrics: HTTP status, title, link counts, backlinks, indexability, and traffic. Optionally filter to pages affected by a specific issue.","version":"1.0.0","params":{"projectId":{"type":"number","required":true,"visibility":"user-or-llm","description":"The Site Audit project ID (found in the project URL in Ahrefs)"},"date":{"type":"string","required":false,"visibility":"user-only","description":"Crawl date in YYYY-MM-DDThh:mm:ss format (defaults to the most recent crawl)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of results to skip, for pagination"},"issueId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Only return pages affected by this issue ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"ahrefs_top_pages":{"id":"ahrefs_top_pages","name":"Ahrefs Top Pages","description":"Get the top pages of a target domain sorted by organic traffic. Returns page URLs with their traffic, keyword counts, and estimated traffic value.","version":"1.0.0","params":{"target":{"type":"string","required":true,"visibility":"user-or-llm","description":"The target domain to analyze. Example: \\"example.com\\""},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Country code for traffic data. Example: \\"us\\", \\"gb\\", \\"de\\" (default: \\"us\\")"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Analysis mode: domain (entire domain), prefix (URL prefix), subdomains (include all subdomains, default), exact (exact URL match). Example: \\"domain\\""},"date":{"type":"string","required":false,"visibility":"user-only","description":"Date to report metrics on, in YYYY-MM-DD format (defaults to today)"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of results to return. Example: 50 (default: 1000)"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ahrefs API Key"}},"hostedApiKey":"none"},"airtable_create_records":{"id":"airtable_create_records","name":"Airtable Create Records","description":"Write new records to an Airtable table","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"records":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of records to create, each with a `fields` object"},"typecast":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"When true, Airtable automatically converts string values to the field type"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_delete_records":{"id":"airtable_delete_records","name":"Airtable Delete Records","description":"Delete one or more records from an Airtable table by ID","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"recordIds":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of record IDs to delete (each starts with \\"rec\\", e.g., [\\"recXXXXXXXXXXXXXX\\"]). Pass a single-element array to delete one record."}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_get_base_schema":{"id":"airtable_get_base_schema","name":"Airtable Get Base Schema","description":"Get the schema of all tables, fields, and views in an Airtable base","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_get_record":{"id":"airtable_get_record","name":"Airtable Get Record","description":"Retrieve a single record from an Airtable table by its ID","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Record ID to retrieve (starts with \\"rec\\", e.g., \\"recXXXXXXXXXXXXXX\\")"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_list_bases":{"id":"airtable_list_bases","name":"Airtable List Bases","description":"List all bases the authenticated user has access to","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"offset":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination offset for retrieving additional bases"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_list_records":{"id":"airtable_list_records","name":"Airtable List Records","description":"Read records from an Airtable table","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"maxRecords":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of records to return (default: all records)"},"filterFormula":{"type":"string","required":false,"visibility":"user-or-llm","description":"Formula to filter records (e.g., \\"({Field Name} = \'Value\')\\")"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_list_tables":{"id":"airtable_list_tables","name":"Airtable List Tables","description":"List all tables and their schema in an Airtable base","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_update_multiple_records":{"id":"airtable_update_multiple_records","name":"Airtable Update Multiple Records","description":"Update multiple existing records in an Airtable table","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"records":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of records to update, each with an `id` and a `fields` object"},"typecast":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"When true, Airtable automatically converts string values to the field type"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_update_record":{"id":"airtable_update_record","name":"Airtable Update Record","description":"Update an existing record in an Airtable table by ID","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"recordId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Record ID to update (starts with \\"rec\\", e.g., \\"recXXXXXXXXXXXXXX\\")"},"fields":{"type":"json","required":true,"visibility":"user-or-llm","description":"An object containing the field names and their new values"},"typecast":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"When true, Airtable automatically converts string values to the field type"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"airtable_upsert_records":{"id":"airtable_upsert_records","name":"Airtable Upsert Records","description":"Update existing records or create new ones in an Airtable table, matching on the specified merge fields","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token"},"baseId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Airtable base ID (starts with \\"app\\", e.g., \\"appXXXXXXXXXXXXXX\\")"},"tableId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Table ID (starts with \\"tbl\\") or table name"},"records":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of records to upsert, each with a `fields` object"},"fieldsToMergeOn":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of field names used to match existing records (max 3). A record is updated when all merge fields match, otherwise it is created. Example: [\\"Name\\"]"},"typecast":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"When true, Airtable automatically converts string values to the field type"}},"oauth":{"required":true,"provider":"airtable"},"hostedApiKey":"none"},"algolia_add_record":{"id":"algolia_add_record","name":"Algolia Add Record","description":"Add or replace a record in an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"objectID":{"type":"string","required":false,"visibility":"user-or-llm","description":"Object ID for the record (auto-generated if not provided)"},"record":{"type":"json","required":true,"visibility":"user-or-llm","description":"JSON object representing the record to add"}},"hostedApiKey":"none"},"algolia_batch_operations":{"id":"algolia_batch_operations","name":"Algolia Batch Operations","description":"Perform batch add, update, partial update, or delete operations on records in an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"requests":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of batch operations. Each item has \\"action\\" (addObject, updateObject, partialUpdateObject, partialUpdateObjectNoCreate, deleteObject, delete, clear) and \\"body\\" (the record data; must include objectID for update/delete; use an empty object {} for the index-level delete/clear actions)"}},"hostedApiKey":"none"},"algolia_browse_records":{"id":"algolia_browse_records","name":"Algolia Browse Records","description":"Browse and iterate over all records in an Algolia index using cursor pagination","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key (must have browse ACL)"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index to browse"},"query":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search query to filter browsed records"},"filters":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter string to narrow down results"},"attributesToRetrieve":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of attributes to retrieve"},"hitsPerPage":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of hits per page (default: 1000, max: 1000)"},"cursor":{"type":"string","required":false,"visibility":"user-or-llm","description":"Cursor from a previous browse response for pagination"},"aroundLatLng":{"type":"string","required":false,"visibility":"user-or-llm","description":"Coordinates for geo-search (e.g., \\"40.71,-74.01\\")"},"aroundRadius":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum radius in meters for geo-search, or \\"all\\" for unlimited"},"insideBoundingBox":{"type":"json","required":false,"visibility":"user-or-llm","description":"Bounding box coordinates as [[lat1, lng1, lat2, lng2]] for geo-search"},"insidePolygon":{"type":"json","required":false,"visibility":"user-or-llm","description":"Polygon coordinates as [[lat1, lng1, lat2, lng2, lat3, lng3, ...]] for geo-search"}},"hostedApiKey":"none"},"algolia_clear_records":{"id":"algolia_clear_records","name":"Algolia Clear Records","description":"Clear all records from an Algolia index while keeping settings, synonyms, and rules","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key (must have deleteIndex ACL)"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index to clear"}},"hostedApiKey":"none"},"algolia_copy_move_index":{"id":"algolia_copy_move_index","name":"Algolia Copy/Move Index","description":"Copy or move an Algolia index to a new destination","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the source index"},"operation":{"type":"string","required":true,"visibility":"user-or-llm","description":"Operation to perform: \\"copy\\" or \\"move\\""},"destination":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the destination index"},"scope":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of scopes to copy (only for \\"copy\\" operation): [\\"settings\\", \\"synonyms\\", \\"rules\\"]. Omit to copy everything including records."}},"hostedApiKey":"none"},"algolia_delete_by_filter":{"id":"algolia_delete_by_filter","name":"Algolia Delete By Filter","description":"Delete all records matching a filter from an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key (must have deleteIndex ACL)"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"filters":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter expression to match records for deletion (e.g., \\"category:outdated\\")"},"facetFilters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of facet filters (e.g., [\\"brand:Acme\\"])"},"numericFilters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of numeric filters (e.g., [\\"price > 100\\"])"},"tagFilters":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of tag filters using the _tags attribute (e.g., [\\"published\\"])"},"aroundLatLng":{"type":"string","required":false,"visibility":"user-or-llm","description":"Coordinates for geo-search filter (e.g., \\"40.71,-74.01\\")"},"aroundRadius":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum radius in meters for geo-search, or \\"all\\" for unlimited"},"insideBoundingBox":{"type":"json","required":false,"visibility":"user-or-llm","description":"Bounding box coordinates as [[lat1, lng1, lat2, lng2]] for geo-search filter"},"insidePolygon":{"type":"json","required":false,"visibility":"user-or-llm","description":"Polygon coordinates as [[lat1, lng1, lat2, lng2, lat3, lng3, ...]] for geo-search filter"}},"hostedApiKey":"none"},"algolia_delete_index":{"id":"algolia_delete_index","name":"Algolia Delete Index","description":"Delete an entire Algolia index and all its records","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key (must have deleteIndex ACL)"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index to delete"}},"hostedApiKey":"none"},"algolia_delete_record":{"id":"algolia_delete_record","name":"Algolia Delete Record","description":"Delete a record by objectID from an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"objectID":{"type":"string","required":true,"visibility":"user-or-llm","description":"The objectID of the record to delete"}},"hostedApiKey":"none"},"algolia_get_record":{"id":"algolia_get_record","name":"Algolia Get Record","description":"Get a record by objectID from an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"objectID":{"type":"string","required":true,"visibility":"user-or-llm","description":"The objectID of the record to retrieve"},"attributesToRetrieve":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of attributes to retrieve"}},"hostedApiKey":"none"},"algolia_get_records":{"id":"algolia_get_records","name":"Algolia Get Records","description":"Retrieve multiple records by objectID from one or more Algolia indices","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Default index name for all requests"},"requests":{"type":"json","required":true,"visibility":"user-or-llm","description":"Array of objects specifying records to retrieve. Each must have \\"objectID\\" and optionally \\"indexName\\" and \\"attributesToRetrieve\\"."}},"hostedApiKey":"none"},"algolia_get_settings":{"id":"algolia_get_settings","name":"Algolia Get Settings","description":"Retrieve the settings of an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"}},"hostedApiKey":"none"},"algolia_get_task_status":{"id":"algolia_get_task_status","name":"Algolia Get Task Status","description":"Check whether an Algolia indexing task has finished publishing","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index the task ran against"},"taskID":{"type":"number","required":true,"visibility":"user-or-llm","description":"The taskID returned by a previous write operation"}},"hostedApiKey":"none"},"algolia_list_indices":{"id":"algolia_list_indices","name":"Algolia List Indices","description":"List all indices in an Algolia application","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for paginating indices (default: not paginated)"},"hitsPerPage":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of indices per page (default: 100)"}},"hostedApiKey":"none"},"algolia_partial_update_record":{"id":"algolia_partial_update_record","name":"Algolia Partial Update Record","description":"Partially update a record in an Algolia index without replacing it entirely","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"objectID":{"type":"string","required":true,"visibility":"user-or-llm","description":"The objectID of the record to update"},"attributes":{"type":"json","required":true,"visibility":"user-or-llm","description":"JSON object with attributes to update. Supports built-in operations like {\\"stock\\": {\\"_operation\\": \\"Decrement\\", \\"value\\": 1}}"},"createIfNotExists":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Whether to create the record if it does not exist (default: true)"}},"hostedApiKey":"none"},"algolia_search":{"id":"algolia_search","name":"Algolia Search","description":"Search an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia API Key"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index to search"},"query":{"type":"string","required":true,"visibility":"user-or-llm","description":"Search query text"},"hitsPerPage":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of hits per page (default: 20)"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number to retrieve (default: 0)"},"filters":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter string (e.g., \\"category:electronics AND price < 100\\")"},"attributesToRetrieve":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of attributes to retrieve"},"facets":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of facet attribute names to retrieve counts for (use \\"*\\" for all)"},"getRankingInfo":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Whether to include detailed ranking information in each hit"},"aroundLatLng":{"type":"string","required":false,"visibility":"user-or-llm","description":"Coordinates for geo-search (e.g., \\"40.71,-74.01\\")"},"aroundRadius":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum radius in meters for geo-search, or \\"all\\" for unlimited"},"insideBoundingBox":{"type":"json","required":false,"visibility":"user-or-llm","description":"Bounding box coordinates as [[lat1, lng1, lat2, lng2]] for geo-search"},"insidePolygon":{"type":"json","required":false,"visibility":"user-or-llm","description":"Polygon coordinates as [[lat1, lng1, lat2, lng2, lat3, lng3, ...]] for geo-search"}},"hostedApiKey":"none"},"algolia_update_settings":{"id":"algolia_update_settings","name":"Algolia Update Settings","description":"Update the settings of an Algolia index","version":"1.0","params":{"applicationId":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Application ID"},"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Algolia Admin API Key (must have editSettings ACL)"},"indexName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the Algolia index"},"settings":{"type":"json","required":true,"visibility":"user-or-llm","description":"JSON object with settings to update (e.g., {\\"searchableAttributes\\": [\\"name\\", \\"description\\"], \\"customRanking\\": [\\"desc(popularity)\\"]})"},"forwardToReplicas":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Whether to apply changes to replica indices (default: false)"}},"hostedApiKey":"none"},"amplitude_event_segmentation":{"id":"amplitude_event_segmentation","name":"Amplitude Event Segmentation","description":"Query event analytics data with segmentation. Get event counts, uniques, averages, and more.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"eventType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Event type name to analyze"},"start":{"type":"string","required":true,"visibility":"user-or-llm","description":"Start date in YYYYMMDD format"},"end":{"type":"string","required":true,"visibility":"user-or-llm","description":"End date in YYYYMMDD format"},"metric":{"type":"string","required":false,"visibility":"user-or-llm","description":"Metric type: uniques, totals, pct_dau, average, histogram, sums, value_avg, or formula (default: uniques)"},"interval":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval: 1 (daily), 7 (weekly), or 30 (monthly)"},"groupBy":{"type":"string","required":false,"visibility":"user-or-llm","description":"Property name to group by (prefix custom user properties with \\"gp:\\")"},"groupBy2":{"type":"string","required":false,"visibility":"user-or-llm","description":"Second property name to group by (prefix custom user properties with \\"gp:\\")"},"limit":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum number of group-by values (max 1000)"},"filters":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON array of filter objects applied to the event, e.g. [{\\"subprop_type\\":\\"event\\",\\"subprop_key\\":\\"city\\",\\"subprop_op\\":\\"is\\",\\"subprop_value\\":[\\"San Francisco\\"]}]"},"formula":{"type":"string","required":false,"visibility":"user-or-llm","description":"Required when metric is \\"formula\\", e.g. \\"UNIQUES(A)/UNIQUES(B)\\""},"segment":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON segment definition(s) applied to the query"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_funnels":{"id":"amplitude_funnels","name":"Amplitude Funnels","description":"Analyze conversion rates and drop-off between a sequence of events.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"events":{"type":"string","required":true,"visibility":"user-or-llm","description":"JSON array of event objects, one per funnel step in order, e.g. [{\\"event_type\\":\\"signup\\"},{\\"event_type\\":\\"purchase\\"}]"},"start":{"type":"string","required":true,"visibility":"user-or-llm","description":"Start date in YYYYMMDD format"},"end":{"type":"string","required":true,"visibility":"user-or-llm","description":"End date in YYYYMMDD format"},"mode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Funnel ordering: \\"ordered\\", \\"unordered\\", or \\"sequential\\" (default: ordered)"},"userType":{"type":"string","required":false,"visibility":"user-or-llm","description":"User type: \\"new\\" or \\"active\\" (default: active)"},"interval":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval: -300000 (real-time), -3600000 (hourly), 1 (daily), 7 (weekly), or 30 (monthly)"},"conversionWindowSeconds":{"type":"string","required":false,"visibility":"user-or-llm","description":"Conversion window in seconds (default: 2592000, i.e. 30 days)"},"groupBy":{"type":"string","required":false,"visibility":"user-or-llm","description":"Property to group by (limit: one; prefix custom properties with \\"gp:\\")"},"limit":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum number of group-by values (default: 100, max: 1000)"},"segment":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON segment definition(s) applied to the query"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_get_active_users":{"id":"amplitude_get_active_users","name":"Amplitude Get Active Users","description":"Get active or new user counts over a date range from the Dashboard REST API.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"start":{"type":"string","required":true,"visibility":"user-or-llm","description":"Start date in YYYYMMDD format"},"end":{"type":"string","required":true,"visibility":"user-or-llm","description":"End date in YYYYMMDD format"},"metric":{"type":"string","required":false,"visibility":"user-or-llm","description":"Metric type: \\"active\\" or \\"new\\" (default: active)"},"interval":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval: 1 (daily), 7 (weekly), or 30 (monthly)"},"groupBy":{"type":"string","required":false,"visibility":"user-or-llm","description":"Property name to group by"},"segment":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON segment definition(s) applied to the query"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_get_revenue":{"id":"amplitude_get_revenue","name":"Amplitude Get Revenue","description":"Get revenue LTV data including ARPU, ARPPU, total revenue, and paying user counts.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"start":{"type":"string","required":true,"visibility":"user-or-llm","description":"Start date in YYYYMMDD format"},"end":{"type":"string","required":true,"visibility":"user-or-llm","description":"End date in YYYYMMDD format"},"metric":{"type":"string","required":false,"visibility":"user-or-llm","description":"Metric: 0 (ARPU), 1 (ARPPU), 2 (Total Revenue), 3 (Paying Users)"},"interval":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval: 1 (daily), 7 (weekly), or 30 (monthly)"},"groupBy":{"type":"string","required":false,"visibility":"user-or-llm","description":"Property name to group by (limit: one)"},"segment":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON segment definition(s) applied to the query"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_group_identify":{"id":"amplitude_group_identify","name":"Amplitude Group Identify","description":"Set group-level properties in Amplitude. Supports $set, $setOnce, $add, $append, $unset operations.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"groupType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Group classification (e.g., \\"company\\", \\"org_id\\")"},"groupValue":{"type":"string","required":true,"visibility":"user-or-llm","description":"Specific group identifier (e.g., \\"Acme Corp\\")"},"groupProperties":{"type":"string","required":true,"visibility":"user-or-llm","description":"JSON object of group properties. Use operations like $set, $setOnce, $add, $append, $unset."},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_identify_user":{"id":"amplitude_identify_user","name":"Amplitude Identify User","description":"Set user properties in Amplitude using the Identify API. Supports $set, $setOnce, $add, $append, $unset operations.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"userId":{"type":"string","required":false,"visibility":"user-or-llm","description":"User ID (required if no device_id)"},"deviceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Device ID (required if no user_id)"},"userProperties":{"type":"string","required":true,"visibility":"user-or-llm","description":"JSON object of user properties. Use operations like $set, $setOnce, $add, $append, $unset."},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_list_events":{"id":"amplitude_list_events","name":"Amplitude List Events","description":"List all event types in the Amplitude project with their weekly totals and unique counts.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_realtime_active_users":{"id":"amplitude_realtime_active_users","name":"Amplitude Real-time Active Users","description":"Get real-time active user counts at 5-minute granularity for the last 2 days.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_retention":{"id":"amplitude_retention","name":"Amplitude Retention","description":"Measure how many users return to perform an action after a starting action.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"startEvent":{"type":"string","required":true,"visibility":"user-or-llm","description":"JSON starting event object, e.g. {\\"event_type\\":\\"_new\\"} or {\\"event_type\\":\\"_active\\"}"},"returnEvent":{"type":"string","required":true,"visibility":"user-or-llm","description":"JSON returning event object, e.g. {\\"event_type\\":\\"_all\\"} or {\\"event_type\\":\\"_active\\"}"},"start":{"type":"string","required":true,"visibility":"user-or-llm","description":"Start date in YYYYMMDD format"},"end":{"type":"string","required":true,"visibility":"user-or-llm","description":"End date in YYYYMMDD format"},"retentionMode":{"type":"string","required":false,"visibility":"user-or-llm","description":"Retention type: \\"bracket\\", \\"rolling\\", or \\"n-day\\" (default: n-day)"},"retentionBrackets":{"type":"string","required":false,"visibility":"user-or-llm","description":"Required when Retention Mode is \\"bracket\\". Day ranges, e.g. [[0,4]]"},"interval":{"type":"string","required":false,"visibility":"user-or-llm","description":"Time interval: 1 (daily), 7 (weekly), or 30 (monthly)"},"groupBy":{"type":"string","required":false,"visibility":"user-or-llm","description":"Property to group by (limit: one; prefix custom properties with \\"gp:\\")"},"segment":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON segment definition(s) applied to the query"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_send_event":{"id":"amplitude_send_event","name":"Amplitude Send Event","description":"Track an event in Amplitude using the HTTP V2 API.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"userId":{"type":"string","required":false,"visibility":"user-or-llm","description":"User ID (required if no device_id)"},"deviceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Device ID (required if no user_id)"},"eventType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the event (e.g., \\"page_view\\", \\"purchase\\")"},"eventProperties":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON object of custom event properties"},"userProperties":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON object of user properties to set (supports $set, $setOnce, $add, $append, $unset)"},"time":{"type":"string","required":false,"visibility":"user-or-llm","description":"Event timestamp in milliseconds since epoch"},"sessionId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Session start time in milliseconds since epoch"},"insertId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Unique ID for deduplication (within 7-day window)"},"appVersion":{"type":"string","required":false,"visibility":"user-or-llm","description":"Application version string"},"platform":{"type":"string","required":false,"visibility":"user-or-llm","description":"Platform (e.g., \\"Web\\", \\"iOS\\", \\"Android\\")"},"country":{"type":"string","required":false,"visibility":"user-or-llm","description":"Two-letter country code"},"language":{"type":"string","required":false,"visibility":"user-or-llm","description":"Language code (e.g., \\"en\\")"},"ip":{"type":"string","required":false,"visibility":"user-or-llm","description":"IP address for geo-location"},"price":{"type":"string","required":false,"visibility":"user-or-llm","description":"Price of the item purchased"},"quantity":{"type":"string","required":false,"visibility":"user-or-llm","description":"Quantity of items purchased"},"revenue":{"type":"string","required":false,"visibility":"user-or-llm","description":"Revenue amount"},"productId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Product identifier"},"revenueType":{"type":"string","required":false,"visibility":"user-or-llm","description":"Revenue type (e.g., \\"purchase\\", \\"refund\\")"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_user_activity":{"id":"amplitude_user_activity","name":"Amplitude User Activity","description":"Get the event stream for a specific user by their Amplitude ID.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"amplitudeId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Amplitude internal user ID"},"offset":{"type":"string","required":false,"visibility":"user-or-llm","description":"Offset for pagination (default 0)"},"limit":{"type":"string","required":false,"visibility":"user-or-llm","description":"Maximum number of events to return (default 1000, max 1000)"},"direction":{"type":"string","required":false,"visibility":"user-or-llm","description":"Sort direction: \\"latest\\" or \\"earliest\\" (default: latest)"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"amplitude_user_profile":{"id":"amplitude_user_profile","name":"Amplitude User Profile","description":"Get a user profile including properties, cohort memberships, and computed properties. Not available for EU data-residency projects.","version":"1.0.0","params":{"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"userId":{"type":"string","required":false,"visibility":"user-or-llm","description":"External user ID (required if no device_id)"},"deviceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Device ID (required if no user_id)"},"getAmpProps":{"type":"string","required":false,"visibility":"user-or-llm","description":"Include Amplitude user properties (true/false, default: false)"},"getCohortIds":{"type":"string","required":false,"visibility":"user-or-llm","description":"Include cohort IDs the user belongs to (true/false, default: false)"},"getComputations":{"type":"string","required":false,"visibility":"user-or-llm","description":"Include computed user properties (true/false, default: false)"}},"hostedApiKey":"none"},"amplitude_user_search":{"id":"amplitude_user_search","name":"Amplitude User Search","description":"Search for a user by User ID, Device ID, or Amplitude ID using the Dashboard REST API.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude API Key"},"secretKey":{"type":"string","required":true,"visibility":"user-only","description":"Amplitude Secret Key"},"user":{"type":"string","required":true,"visibility":"user-or-llm","description":"User ID, Device ID, or Amplitude ID to search for"},"dataResidency":{"type":"string","required":false,"visibility":"user-or-llm","description":"Data residency region: \\"us\\" (default) or \\"eu\\""}},"hostedApiKey":"none"},"apify_get_dataset_items":{"id":"apify_get_dataset_items","name":"APIFY Get Dataset Items","description":"Retrieve items stored in an APIFY dataset","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"APIFY API token from console.apify.com/account#/integrations"},"datasetId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Dataset ID to read items from. Example: \\"9RnD3Pql2vGZkc5H5\\""},"itemLimit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Max items to return (1-250000). Default: all items. Example: 500"},"offset":{"type":"number","required":false,"visibility":"user-or-llm","description":"Number of items to skip at the start. Default: 0"},"fields":{"type":"string","required":false,"visibility":"user-or-llm","description":"Comma-separated list of fields to include. Example: \\"title,url,price\\""}},"hostedApiKey":"none"},"apify_get_run":{"id":"apify_get_run","name":"APIFY Get Run","description":"Get the status and details of an APIFY actor run","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"APIFY API token from console.apify.com/account#/integrations"},"runId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Actor run ID to fetch. Example: \\"HG7ML7M8z78YcAPEB\\""}},"hostedApiKey":"none"},"apify_run_actor_async":{"id":"apify_run_actor_async","name":"APIFY Run Actor (Async)","description":"Run an APIFY actor asynchronously with polling for long-running tasks","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"APIFY API token from console.apify.com/account#/integrations"},"actorId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Actor ID or username/actor-name. Examples: \\"apify/web-scraper\\", \\"janedoe/my-actor\\", \\"moJRLRc85AitArpNN\\""},"input":{"type":"string","required":false,"visibility":"user-or-llm","description":"Actor input as JSON string. Example: { \\"startUrls\\": [ { \\"url\\": \\"https://example.com\\" } ], \\"maxPages\\": 10 }"},"waitForFinish":{"type":"number","required":false,"visibility":"user-or-llm","description":"Initial wait time in seconds (0-60) before polling starts. Example: 30"},"itemLimit":{"type":"number","required":false,"default":100,"visibility":"user-or-llm","description":"Max dataset items to fetch (1-250000). Default: 100. Example: 500"},"memory":{"type":"number","required":false,"visibility":"user-or-llm","description":"Memory in megabytes allocated for the actor run (128-32768). Example: 1024 for 1GB, 2048 for 2GB"},"timeout":{"type":"number","required":false,"visibility":"user-or-llm","description":"Timeout in seconds for the actor run. Example: 300 for 5 minutes, 3600 for 1 hour"},"build":{"type":"string","required":false,"visibility":"user-or-llm","description":"Actor build to run. Examples: \\"latest\\", \\"beta\\", \\"1.2.3\\", \\"build-tag-name\\""}},"hostedApiKey":"none"},"apify_run_actor_sync":{"id":"apify_run_actor_sync","name":"APIFY Run Actor (Sync)","description":"Run an APIFY actor synchronously and get results (max 5 minutes)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"APIFY API token from console.apify.com/account#/integrations"},"actorId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Actor ID or username/actor-name. Examples: \\"apify/web-scraper\\", \\"janedoe/my-actor\\", \\"moJRLRc85AitArpNN\\""},"input":{"type":"string","required":false,"visibility":"user-or-llm","description":"Actor input as JSON string. Example: { \\"startUrls\\": [ { \\"url\\": \\"https://example.com\\" } ], \\"maxPages\\": 10 }"},"memory":{"type":"number","required":false,"visibility":"user-or-llm","description":"Memory in megabytes allocated for the actor run (128-32768). Example: 1024 for 1GB, 2048 for 2GB"},"timeout":{"type":"number","required":false,"visibility":"user-or-llm","description":"Timeout in seconds for the actor run. Example: 300 for 5 minutes, 3600 for 1 hour"},"build":{"type":"string","required":false,"visibility":"user-or-llm","description":"Actor build to run. Examples: \\"latest\\", \\"beta\\", \\"1.2.3\\", \\"build-tag-name\\""}},"hostedApiKey":"none"},"apify_run_task":{"id":"apify_run_task","name":"APIFY Run Task","description":"Run a saved APIFY actor task synchronously and get dataset items (max 5 minutes)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"APIFY API token from console.apify.com/account#/integrations"},"taskId":{"type":"string","required":true,"visibility":"user-or-llm","description":"Task ID or username/task-name. Examples: \\"janedoe/my-task\\", \\"moJRLRc85AitArpNN\\""},"input":{"type":"string","required":false,"visibility":"user-or-llm","description":"JSON string that overrides the task\'s saved input. Example: { \\"startUrls\\": [ { \\"url\\": \\"https://example.com\\" } ] }"},"itemLimit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Max dataset items to return (1-250000). Example: 500"},"memory":{"type":"number","required":false,"visibility":"user-or-llm","description":"Memory in megabytes allocated for the run (128-32768). Example: 1024 for 1GB"},"timeout":{"type":"number","required":false,"visibility":"user-or-llm","description":"Timeout in seconds for the run. Example: 300 for 5 minutes"},"build":{"type":"string","required":false,"visibility":"user-or-llm","description":"Actor build to run. Examples: \\"latest\\", \\"beta\\", \\"1.2.3\\""}},"hostedApiKey":"none"},"apollo_account_bulk_create":{"id":"apollo_account_bulk_create","name":"Apollo Bulk Create Accounts","description":"Create up to 100 accounts at once in your Apollo database. Set run_dedupe=true to deduplicate by domain, organization_id, and name. Master key required.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"accounts":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of accounts to create (max 100). Each account should include a name, and may optionally include domain, phone, phone_status_cd, raw_address, owner_id, linkedin_url, facebook_url, twitter_url, salesforce_id, and hubspot_id."},"append_label_names":{"type":"array","required":false,"visibility":"user-only","description":"Array of label names to add to ALL accounts in this request"},"run_dedupe":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"When true, performs aggressive deduplication by domain, organization_id, and name (defaults to false)"}},"hostedApiKey":"none"},"apollo_account_bulk_update":{"id":"apollo_account_bulk_update","name":"Apollo Bulk Update Accounts","description":"Update up to 1000 existing accounts at once in your Apollo database (higher limit than contacts!). Each account must include an id field. Master key required.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"account_ids":{"type":"array","required":false,"visibility":"user-or-llm","description":"Array of account IDs to update with the same values (max 1000). Use with name/owner_id for uniform updates. Use either this OR account_attributes."},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"When using account_ids, apply this name to all accounts"},"owner_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"When using account_ids, apply this owner to all accounts"},"account_stage_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"When using account_ids, apply this account stage to all accounts"},"account_attributes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of account objects with individual updates (each must include id). Example: [{\\"id\\": \\"acc1\\", \\"name\\": \\"Acme\\", \\"owner_id\\": \\"u1\\", \\"account_stage_id\\": \\"s1\\", \\"typed_custom_fields\\": {\\"field_id\\": \\"value\\"}}]"},"async":{"type":"boolean","required":false,"visibility":"user-only","description":"When true, processes the update asynchronously. Only supported when using account_ids; returns 422 if used with account_attributes."}},"hostedApiKey":"none"},"apollo_account_create":{"id":"apollo_account_create","name":"Apollo Create Account","description":"Create a new account (company) in your Apollo database","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Company name (e.g., \\"Acme Corporation\\")"},"domain":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company domain without www. prefix (e.g., \\"acme.com\\")"},"phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Primary phone number for the account"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"Apollo user ID of the account owner"},"account_stage_id":{"type":"string","required":false,"visibility":"user-only","description":"Apollo ID for the account stage to assign this account to"},"raw_address":{"type":"string","required":false,"visibility":"user-or-llm","description":"Corporate location (e.g., \\"San Francisco, CA, USA\\")"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-only","description":"Custom field values as { custom_field_id: value } map"}},"hostedApiKey":"none"},"apollo_account_search":{"id":"apollo_account_search","name":"Apollo Search Accounts","description":"Search your team\'s accounts in Apollo. Display limit: 50,000 records (100 records per page, 500 pages max). Use filters to narrow results. Master key required.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"q_organization_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter accounts by organization name (partial-match search)"},"account_stage_ids":{"type":"array","required":false,"visibility":"user-only","description":"Filter by account stage IDs"},"account_label_ids":{"type":"array","required":false,"visibility":"user-only","description":"Filter by account label IDs"},"sort_by_field":{"type":"string","required":false,"visibility":"user-or-llm","description":"Sort field: \\"account_last_activity_date\\", \\"account_created_at\\", or \\"account_updated_at\\""},"sort_ascending":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Sort ascending when true. Defaults to descending."},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_account_update":{"id":"apollo_account_update","name":"Apollo Update Account","description":"Update an existing account in your Apollo database","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"account_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the account to update (e.g., \\"acc_abc123\\")"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company name (e.g., \\"Acme Corporation\\")"},"domain":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company domain (e.g., \\"acme.com\\")"},"phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company phone number"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"Apollo user ID of the account owner"},"account_stage_id":{"type":"string","required":false,"visibility":"user-only","description":"Apollo ID for the account stage to assign this account to"},"raw_address":{"type":"string","required":false,"visibility":"user-or-llm","description":"Corporate location (e.g., \\"San Francisco, CA, USA\\")"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-only","description":"Custom field values as { custom_field_id: value } map"}},"hostedApiKey":"none"},"apollo_contact_bulk_create":{"id":"apollo_contact_bulk_create","name":"Apollo Bulk Create Contacts","description":"Create up to 100 contacts at once in your Apollo database. Supports deduplication to prevent creating duplicate contacts. Master key required.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"contacts":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of contacts to create (max 100). Each contact may include first_name, last_name, email, title, organization_name, account_id, owner_id, contact_stage_id, linkedin_url, phone (single string) or phone_numbers (array of {raw_number, position}), contact_emails, typed_custom_fields, and CRM IDs (salesforce_contact_id, hubspot_id, team_id) for cross-system matching"},"append_label_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Label names to add to all contacts in this request (e.g., [\\"Hot Lead\\"])"},"run_dedupe":{"type":"boolean","required":false,"visibility":"user-only","description":"Enable deduplication to prevent creating duplicate contacts. When true, existing contacts are returned without modification"}},"hostedApiKey":"none"},"apollo_contact_bulk_update":{"id":"apollo_contact_bulk_update","name":"Apollo Bulk Update Contacts","description":"Update up to 100 existing contacts at once in your Apollo database. Each contact must include an id field. Master key required.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"contact_ids":{"type":"array","required":false,"visibility":"user-or-llm","description":"Array of contact IDs to update. Must be paired with an object-form contact_attributes specifying the fields to apply uniformly to all listed contacts."},"contact_attributes":{"type":"json","required":false,"visibility":"user-or-llm","description":"Required. Either an array of per-contact updates (each with id) — used standalone — or a single object of attributes to apply to all contact_ids. Supported fields: owner_id, email, organization_name, title, first_name, last_name, account_id, present_raw_address, linkedin_url, typed_custom_fields"},"async":{"type":"boolean","required":false,"visibility":"user-only","description":"Force asynchronous processing. Automatically enabled for >100 contacts"}},"hostedApiKey":"none"},"apollo_contact_create":{"id":"apollo_contact_create","name":"Apollo Create Contact","description":"Create a new contact in your Apollo database","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"first_name":{"type":"string","required":true,"visibility":"user-or-llm","description":"First name of the contact"},"last_name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Last name of the contact"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"Email address of the contact"},"title":{"type":"string","required":false,"visibility":"user-or-llm","description":"Job title (e.g., \\"VP of Sales\\", \\"Software Engineer\\")"},"account_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"Apollo account ID to associate with (e.g., \\"acc_abc123\\")"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"User ID of the contact owner (accepted by Apollo but not officially documented for POST /contacts)"},"organization_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Name of the contact\'s employer (e.g., \\"Apollo\\")"},"website_url":{"type":"string","required":false,"visibility":"user-or-llm","description":"Corporate website URL (e.g., \\"https://www.apollo.io/\\")"},"label_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Lists/labels to add the contact to (e.g., [\\"Prospects\\"])"},"contact_stage_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"Apollo ID for the contact stage"},"present_raw_address":{"type":"string","required":false,"visibility":"user-or-llm","description":"Personal location for the contact (e.g., \\"Atlanta, United States\\")"},"direct_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Primary phone number"},"corporate_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Work/office phone number"},"mobile_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Mobile phone number"},"home_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Home phone number"},"other_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Alternative phone number"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-or-llm","description":"Custom field values keyed by custom field ID"},"run_dedupe":{"type":"boolean","required":false,"visibility":"user-only","description":"When true, Apollo deduplicates against existing contacts"}},"hostedApiKey":"none"},"apollo_contact_search":{"id":"apollo_contact_search","name":"Apollo Search Contacts","description":"Search your team\'s contacts in Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"q_keywords":{"type":"string","required":false,"visibility":"user-or-llm","description":"Keywords to search for"},"contact_stage_ids":{"type":"array","required":false,"visibility":"user-only","description":"Filter by contact stage IDs"},"contact_label_ids":{"type":"array","required":false,"visibility":"user-only","description":"Filter by Apollo label IDs (lists)"},"sort_by_field":{"type":"string","required":false,"visibility":"user-only","description":"Sort field: contact_last_activity_date, contact_email_last_opened_at, contact_email_last_clicked_at, contact_created_at, or contact_updated_at"},"sort_ascending":{"type":"boolean","required":false,"visibility":"user-only","description":"When true, sort ascending. Must be used together with sort_by_field"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_contact_update":{"id":"apollo_contact_update","name":"Apollo Update Contact","description":"Update an existing contact in your Apollo database","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"contact_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the contact to update (e.g., \\"con_abc123\\")"},"first_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"First name of the contact"},"last_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Last name of the contact"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"Email address"},"title":{"type":"string","required":false,"visibility":"user-or-llm","description":"Job title (e.g., \\"VP of Sales\\", \\"Software Engineer\\")"},"account_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"Apollo account ID (e.g., \\"acc_abc123\\")"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"User ID of the contact owner (accepted by Apollo but not officially documented for PATCH /contacts/{id})"},"organization_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Name of the contact\'s employer (e.g., \\"Apollo\\")"},"website_url":{"type":"string","required":false,"visibility":"user-or-llm","description":"Corporate website URL (e.g., \\"https://www.apollo.io/\\")"},"label_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Lists/labels to add the contact to (e.g., [\\"Prospects\\"])"},"contact_stage_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"Apollo ID for the contact stage"},"present_raw_address":{"type":"string","required":false,"visibility":"user-or-llm","description":"Personal location for the contact (e.g., \\"Atlanta, United States\\")"},"direct_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Primary phone number"},"corporate_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Work/office phone number"},"mobile_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Mobile phone number"},"home_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Home phone number"},"other_phone":{"type":"string","required":false,"visibility":"user-or-llm","description":"Alternative phone number"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-or-llm","description":"Custom field values keyed by custom field ID"}},"hostedApiKey":"none"},"apollo_email_accounts":{"id":"apollo_email_accounts","name":"Apollo Get Email Accounts","description":"Get list of team\'s linked email accounts in Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"}},"hostedApiKey":"none"},"apollo_opportunity_create":{"id":"apollo_opportunity_create","name":"Apollo Create Opportunity","description":"Create a new deal for an account in your Apollo database (master key required)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the opportunity/deal (e.g., \\"Enterprise License - Q1\\")"},"account_id":{"type":"string","required":false,"visibility":"user-or-llm","description":"ID of the account this opportunity belongs to (e.g., \\"acc_abc123\\")"},"amount":{"type":"string","required":false,"visibility":"user-or-llm","description":"Monetary value as a plain number string with no commas or currency symbols"},"opportunity_stage_id":{"type":"string","required":false,"visibility":"user-only","description":"ID of the opportunity stage"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"User ID of the opportunity owner"},"closed_date":{"type":"string","required":false,"visibility":"user-or-llm","description":"Expected close date in YYYY-MM-DD format"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-only","description":"Custom field values as { custom_field_id: value } map"}},"hostedApiKey":"none"},"apollo_opportunity_get":{"id":"apollo_opportunity_get","name":"Apollo Get Opportunity","description":"Retrieve complete details of a specific deal/opportunity by ID","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"opportunity_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the opportunity to retrieve (e.g., \\"opp_abc123\\")"}},"hostedApiKey":"none"},"apollo_opportunity_search":{"id":"apollo_opportunity_search","name":"Apollo Search Opportunities","description":"Search and list all deals/opportunities in your team\'s Apollo account","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"sort_by_field":{"type":"string","required":false,"visibility":"user-or-llm","description":"Sort field: \\"amount\\", \\"is_closed\\", or \\"is_won\\""},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_opportunity_update":{"id":"apollo_opportunity_update","name":"Apollo Update Opportunity","description":"Update an existing deal/opportunity in your Apollo database","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"opportunity_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the opportunity to update (e.g., \\"opp_abc123\\")"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Name of the opportunity/deal (e.g., \\"Enterprise License - Q1\\")"},"amount":{"type":"string","required":false,"visibility":"user-or-llm","description":"Monetary value as a plain number string with no commas or currency symbols"},"opportunity_stage_id":{"type":"string","required":false,"visibility":"user-only","description":"ID of the opportunity stage"},"owner_id":{"type":"string","required":false,"visibility":"user-only","description":"User ID of the opportunity owner"},"closed_date":{"type":"string","required":false,"visibility":"user-or-llm","description":"Expected close date in YYYY-MM-DD format"},"typed_custom_fields":{"type":"json","required":false,"visibility":"user-only","description":"Custom field values as { custom_field_id: value } map"}},"hostedApiKey":"none"},"apollo_organization_bulk_enrich":{"id":"apollo_organization_bulk_enrich","name":"Apollo Bulk Organization Enrichment","description":"Enrich data for up to 10 organizations at once using Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"domains":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of company domains to enrich (max 10, no www. or @, e.g., [\\"apollo.io\\", \\"stripe.com\\"])"}},"hostedApiKey":"none"},"apollo_organization_enrich":{"id":"apollo_organization_enrich","name":"Apollo Organization Enrichment","description":"Enrich data for a single organization using Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"domain":{"type":"string","required":true,"visibility":"user-or-llm","description":"Company domain (e.g., \\"apollo.io\\", \\"acme.com\\")"}},"hostedApiKey":"none"},"apollo_organization_search":{"id":"apollo_organization_search","name":"Apollo Organization Search","description":"Search Apollo\'s database for companies using filters","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"organization_locations":{"type":"array","required":false,"visibility":"user-or-llm","description":"Company HQ locations (cities, US states, or countries)"},"organization_not_locations":{"type":"array","required":false,"visibility":"user-or-llm","description":"Exclude companies whose HQ is in these locations"},"organization_num_employees_ranges":{"type":"array","required":false,"visibility":"user-or-llm","description":"Employee count ranges as \\"min,max\\" strings (e.g., [\\"1,10\\", \\"250,500\\", \\"10000,20000\\"])"},"q_organization_keyword_tags":{"type":"array","required":false,"visibility":"user-or-llm","description":"Industry or keyword tags"},"q_organization_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Organization name to search for (e.g., \\"Acme\\", \\"TechCorp\\")"},"organization_ids":{"type":"array","required":false,"visibility":"user-or-llm","description":"Apollo organization IDs to include (e.g., [\\"5e66b6381e05b4008c8331b8\\"])"},"q_organization_domains_list":{"type":"array","required":false,"visibility":"user-or-llm","description":"Domain names to filter by (no www. or @, up to 1,000)"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_people_bulk_enrich":{"id":"apollo_people_bulk_enrich","name":"Apollo Bulk People Enrichment","description":"Enrich data for up to 10 people at once using Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"people":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of people to enrich (max 10)"},"reveal_personal_emails":{"type":"boolean","required":false,"visibility":"user-only","description":"Reveal personal email addresses (uses credits)"},"reveal_phone_number":{"type":"boolean","required":false,"visibility":"user-only","description":"Reveal phone numbers (uses credits, requires webhook_url)"},"webhook_url":{"type":"string","required":false,"visibility":"user-only","description":"Webhook URL for async phone number delivery (required when reveal_phone_number is true)"}},"hostedApiKey":"none"},"apollo_people_enrich":{"id":"apollo_people_enrich","name":"Apollo People Enrichment","description":"Enrich data for a single person using Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"first_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"First name of the person"},"last_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Last name of the person"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Full name of the person (alternative to first_name/last_name)"},"id":{"type":"string","required":false,"visibility":"user-or-llm","description":"Apollo ID for the person"},"hashed_email":{"type":"string","required":false,"visibility":"user-or-llm","description":"MD5 or SHA-256 hashed email"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"Email address of the person"},"organization_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company name where the person works"},"domain":{"type":"string","required":false,"visibility":"user-or-llm","description":"Company domain (e.g., \\"apollo.io\\", \\"acme.com\\")"},"linkedin_url":{"type":"string","required":false,"visibility":"user-or-llm","description":"LinkedIn profile URL"},"reveal_personal_emails":{"type":"boolean","required":false,"visibility":"user-only","description":"Reveal personal email addresses (uses credits)"},"reveal_phone_number":{"type":"boolean","required":false,"visibility":"user-only","description":"Reveal phone numbers (uses credits, requires webhook_url)"},"webhook_url":{"type":"string","required":false,"visibility":"user-only","description":"Webhook URL for async phone number delivery (required when reveal_phone_number is true)"}},"hostedApiKey":"none"},"apollo_people_search":{"id":"apollo_people_search","name":"Apollo People Search","description":"Search Apollo\'s database for people using demographic filters","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key"},"person_titles":{"type":"array","required":false,"visibility":"user-or-llm","description":"Job titles to search for (e.g., [\\"CEO\\", \\"VP of Sales\\"])"},"include_similar_titles":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Whether to return people with job titles similar to person_titles"},"person_locations":{"type":"array","required":false,"visibility":"user-or-llm","description":"Locations to search in (e.g., [\\"San Francisco, CA\\", \\"New York, NY\\"])"},"person_seniorities":{"type":"array","required":false,"visibility":"user-or-llm","description":"Seniority levels (one of: owner, founder, c_suite, partner, vp, head, director, manager, senior, entry, intern)"},"organization_ids":{"type":"array","required":false,"visibility":"user-or-llm","description":"Apollo organization IDs to filter by (e.g., [\\"5e66b6381e05b4008c8331b8\\"])"},"organization_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Company names to search within (legacy filter)"},"organization_locations":{"type":"array","required":false,"visibility":"user-or-llm","description":"Headquarters locations of the people\'s current employer (e.g., [\'texas\', \'tokyo\', \'spain\'])"},"q_organization_domains_list":{"type":"array","required":false,"visibility":"user-or-llm","description":"Employer domain names (e.g., [\\"apollo.io\\", \\"microsoft.com\\"]) — up to 1,000, no www. or @"},"organization_num_employees_ranges":{"type":"array","required":false,"visibility":"user-or-llm","description":"Employee count ranges for the person\'s current employer. Each entry is \\"min,max\\" (e.g., [\\"1,10\\", \\"250,500\\", \\"10000,20000\\"])"},"contact_email_status":{"type":"array","required":false,"visibility":"user-or-llm","description":"Email statuses to filter by: \\"verified\\", \\"unverified\\", \\"likely to engage\\", \\"unavailable\\""},"q_keywords":{"type":"string","required":false,"visibility":"user-or-llm","description":"Keywords to search for"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination, default 1 (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, default 25, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_sequence_add_contacts":{"id":"apollo_sequence_add_contacts","name":"Apollo Add Contacts to Sequence","description":"Add contacts to an Apollo sequence","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"sequence_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the sequence to add contacts to (e.g., \\"seq_abc123\\")"},"contact_ids":{"type":"array","required":false,"visibility":"user-or-llm","description":"Array of contact IDs to add to the sequence (e.g., [\\"con_abc123\\", \\"con_def456\\"]). Either contact_ids or label_names must be provided."},"label_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Array of label names to identify contacts to add to the sequence. Either contact_ids or label_names must be provided."},"send_email_from_email_account_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the email account to send from. Use the Get Email Accounts operation to look this up."},"send_email_from_email_address":{"type":"string","required":false,"visibility":"user-only","description":"Specific email address to send from within the email account."},"sequence_no_email":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts even if they have no email address"},"sequence_unverified_email":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts with unverified email addresses"},"sequence_job_change":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts who recently changed jobs"},"sequence_active_in_other_campaigns":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts active in other campaigns"},"sequence_finished_in_other_campaigns":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts who finished other campaigns"},"sequence_same_company_in_same_campaign":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts even if others from the same company are in the sequence"},"contacts_without_ownership_permission":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts without ownership permission"},"add_if_in_queue":{"type":"boolean","required":false,"visibility":"user-only","description":"Add contacts even if they are in the queue"},"contact_verification_skipped":{"type":"boolean","required":false,"visibility":"user-only","description":"Skip contact verification when adding"},"user_id":{"type":"string","required":false,"visibility":"user-only","description":"ID of the user performing the action"},"status":{"type":"string","required":false,"visibility":"user-only","description":"Initial status for added contacts: \\"active\\" or \\"paused\\""},"auto_unpause_at":{"type":"string","required":false,"visibility":"user-only","description":"ISO 8601 datetime to automatically unpause contacts"}},"hostedApiKey":"none"},"apollo_sequence_search":{"id":"apollo_sequence_search","name":"Apollo Search Sequences","description":"Search for sequences/campaigns in your team\'s Apollo account (master key required)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"q_name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Search sequences by name (e.g., \\"Outbound Q1\\", \\"Follow-up\\")"},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"apollo_task_create":{"id":"apollo_task_create","name":"Apollo Create Task","description":"Create one or more tasks in Apollo (one task per contact_id, master key required)","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"user_id":{"type":"string","required":true,"visibility":"user-or-llm","description":"ID of the Apollo user the task is assigned to"},"contact_ids":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of contact IDs. One task is created per contact."},"priority":{"type":"string","required":false,"visibility":"user-or-llm","description":"Task priority: \\"high\\", \\"medium\\", or \\"low\\" (defaults to \\"medium\\")"},"due_at":{"type":"string","required":true,"visibility":"user-or-llm","description":"Due date/time in ISO 8601 format (e.g., \\"2024-12-31T23:59:59Z\\")"},"type":{"type":"string","required":true,"visibility":"user-or-llm","description":"Task type: \\"call\\", \\"outreach_manual_email\\", \\"linkedin_step_connect\\", \\"linkedin_step_message\\", \\"linkedin_step_view_profile\\", \\"linkedin_step_interact_post\\", or \\"action_item\\""},"status":{"type":"string","required":true,"visibility":"user-or-llm","description":"Task status: \\"scheduled\\", \\"completed\\", or \\"skipped\\""},"note":{"type":"string","required":false,"visibility":"user-or-llm","description":"Free-form note providing context for the task"}},"hostedApiKey":"none"},"apollo_task_search":{"id":"apollo_task_search","name":"Apollo Search Tasks","description":"Search for tasks in Apollo","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Apollo API key (master key required)"},"sort_by_field":{"type":"string","required":false,"visibility":"user-or-llm","description":"Sort field: \\"task_due_at\\" or \\"task_priority\\""},"open_factor_names":{"type":"array","required":false,"visibility":"user-or-llm","description":"Filter by status. Common values: [\\"task_types\\"] for open tasks, [\\"task_completed_at\\"] for completed tasks."},"page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Page number for pagination (e.g., 1, 2, 3)"},"per_page":{"type":"number","required":false,"visibility":"user-or-llm","description":"Results per page, max 100 (e.g., 25, 50, 100)"}},"hostedApiKey":"none"},"appconfig_create_application":{"id":"appconfig_create_application","name":"AppConfig Create Application","description":"Create an application in AWS AppConfig","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the application to create"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"Description of the application"}},"hostedApiKey":"none"},"appconfig_create_configuration_profile":{"id":"appconfig_create_configuration_profile","name":"AppConfig Create Configuration Profile","description":"Create a configuration profile in an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to create the configuration profile in"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the configuration profile"},"locationUri":{"type":"string","required":true,"visibility":"user-or-llm","description":"Where the configuration is stored. Use \\"hosted\\" for AppConfig-hosted configurations, or an SSM/S3 URI"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"Description of the configuration profile"},"retrievalRoleArn":{"type":"string","required":false,"visibility":"user-or-llm","description":"ARN of an IAM role to retrieve the configuration (required for non-hosted URIs)"},"type":{"type":"string","required":false,"visibility":"user-or-llm","description":"Profile type: AWS.Freeform (default) or AWS.AppConfig.FeatureFlags"}},"hostedApiKey":"none"},"appconfig_create_environment":{"id":"appconfig_create_environment","name":"AppConfig Create Environment","description":"Create an environment for an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to create the environment in"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the environment to create"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"Description of the environment"}},"hostedApiKey":"none"},"appconfig_create_hosted_configuration_version":{"id":"appconfig_create_hosted_configuration_version","name":"AppConfig Create Hosted Configuration Version","description":"Create a new hosted configuration version for an AppConfig configuration profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to add the version to"},"content":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration content (e.g., a JSON or YAML document)"},"contentType":{"type":"string","required":true,"visibility":"user-or-llm","description":"Content type of the configuration (e.g., application/json, text/plain)"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"Description of the configuration version"},"latestVersionNumber":{"type":"number","required":false,"visibility":"user-or-llm","description":"The version number of the latest version, used for optimistic concurrency"},"versionLabel":{"type":"string","required":false,"visibility":"user-or-llm","description":"A user-defined label for the configuration version"}},"hostedApiKey":"none"},"appconfig_delete_application":{"id":"appconfig_delete_application","name":"AppConfig Delete Application","description":"Delete an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to delete"}},"hostedApiKey":"none"},"appconfig_delete_configuration_profile":{"id":"appconfig_delete_configuration_profile","name":"AppConfig Delete Configuration Profile","description":"Delete an AWS AppConfig configuration profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to delete"}},"hostedApiKey":"none"},"appconfig_delete_environment":{"id":"appconfig_delete_environment","name":"AppConfig Delete Environment","description":"Delete an AWS AppConfig environment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the environment"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID to delete"}},"hostedApiKey":"none"},"appconfig_delete_hosted_configuration_version":{"id":"appconfig_delete_hosted_configuration_version","name":"AppConfig Delete Hosted Configuration Version","description":"Delete a specific hosted configuration version from an AppConfig profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID that owns the version"},"versionNumber":{"type":"number","required":true,"visibility":"user-or-llm","description":"The version number to delete"}},"hostedApiKey":"none"},"appconfig_get_application":{"id":"appconfig_get_application","name":"AppConfig Get Application","description":"Get details about a single AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to retrieve"}},"hostedApiKey":"none"},"appconfig_get_configuration":{"id":"appconfig_get_configuration","name":"AppConfig Get Configuration","description":"Retrieve the latest deployed configuration for an AppConfig application, environment, and profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID or name to retrieve configuration for"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID or name to retrieve configuration for"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID or name to retrieve"}},"hostedApiKey":"none"},"appconfig_get_configuration_profile":{"id":"appconfig_get_configuration_profile","name":"AppConfig Get Configuration Profile","description":"Get details about a single AWS AppConfig configuration profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to retrieve"}},"hostedApiKey":"none"},"appconfig_get_deployment":{"id":"appconfig_get_deployment","name":"AppConfig Get Deployment","description":"Get details about a specific AWS AppConfig deployment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID of the deployment"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID of the deployment"},"deploymentNumber":{"type":"number","required":true,"visibility":"user-or-llm","description":"The sequence number of the deployment"}},"hostedApiKey":"none"},"appconfig_get_environment":{"id":"appconfig_get_environment","name":"AppConfig Get Environment","description":"Get details about a single AWS AppConfig environment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the environment"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID to retrieve"}},"hostedApiKey":"none"},"appconfig_get_hosted_configuration_version":{"id":"appconfig_get_hosted_configuration_version","name":"AppConfig Get Hosted Configuration Version","description":"Retrieve a specific hosted configuration version from an AppConfig profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to read the version from"},"versionNumber":{"type":"number","required":true,"visibility":"user-or-llm","description":"The version number to retrieve"}},"hostedApiKey":"none"},"appconfig_list_applications":{"id":"appconfig_list_applications","name":"AppConfig List Applications","description":"List applications in AWS AppConfig","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of applications to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_list_configuration_profiles":{"id":"appconfig_list_configuration_profiles","name":"AppConfig List Configuration Profiles","description":"List configuration profiles for an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profiles"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of configuration profiles to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_list_deployment_strategies":{"id":"appconfig_list_deployment_strategies","name":"AppConfig List Deployment Strategies","description":"List deployment strategies available in AWS AppConfig","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of deployment strategies to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_list_deployments":{"id":"appconfig_list_deployments","name":"AppConfig List Deployments","description":"List deployments for an AWS AppConfig environment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID of the deployments"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID of the deployments"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of deployments to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_list_environments":{"id":"appconfig_list_environments","name":"AppConfig List Environments","description":"List environments for an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the environments"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of environments to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_list_hosted_configuration_versions":{"id":"appconfig_list_hosted_configuration_versions","name":"AppConfig List Hosted Configuration Versions","description":"List hosted configuration versions for an AWS AppConfig configuration profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to list versions for"},"maxResults":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of versions to return (1-50)"},"nextToken":{"type":"string","required":false,"visibility":"user-or-llm","description":"Pagination token from a previous response"}},"hostedApiKey":"none"},"appconfig_start_deployment":{"id":"appconfig_start_deployment","name":"AppConfig Start Deployment","description":"Start deploying a configuration version to an AWS AppConfig environment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to deploy in"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID to deploy to"},"deploymentStrategyId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The deployment strategy ID to use"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to deploy"},"configurationVersion":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration version to deploy"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"Description of the deployment"}},"hostedApiKey":"none"},"appconfig_stop_deployment":{"id":"appconfig_stop_deployment","name":"AppConfig Stop Deployment","description":"Stop an in-progress AWS AppConfig deployment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID of the deployment"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID of the deployment"},"deploymentNumber":{"type":"number","required":true,"visibility":"user-or-llm","description":"The sequence number of the deployment to stop"}},"hostedApiKey":"none"},"appconfig_update_application":{"id":"appconfig_update_application","name":"AppConfig Update Application","description":"Update the name or description of an AWS AppConfig application","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID to update"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"New name for the application"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"New description for the application"}},"hostedApiKey":"none"},"appconfig_update_configuration_profile":{"id":"appconfig_update_configuration_profile","name":"AppConfig Update Configuration Profile","description":"Update the name, description, or retrieval role of an AppConfig configuration profile","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the configuration profile"},"configurationProfileId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The configuration profile ID to update"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"New name for the configuration profile"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"New description for the configuration profile"},"retrievalRoleArn":{"type":"string","required":false,"visibility":"user-or-llm","description":"New ARN of the IAM role used to retrieve the configuration"}},"hostedApiKey":"none"},"appconfig_update_environment":{"id":"appconfig_update_environment","name":"AppConfig Update Environment","description":"Update the name or description of an AWS AppConfig environment","version":"1.0","params":{"region":{"type":"string","required":true,"visibility":"user-only","description":"AWS region (e.g., us-east-1)"},"accessKeyId":{"type":"string","required":true,"visibility":"user-only","description":"AWS access key ID"},"secretAccessKey":{"type":"string","required":true,"visibility":"user-only","description":"AWS secret access key"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The application ID that owns the environment"},"environmentId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The environment ID to update"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"New name for the environment"},"description":{"type":"string","required":false,"visibility":"user-or-llm","description":"New description for the environment"}},"hostedApiKey":"none"},"arxiv_get_author_papers":{"id":"arxiv_get_author_papers","name":"ArXiv Get Author Papers","description":"Search for papers by a specific author on ArXiv.","version":"1.0.0","params":{"authorName":{"type":"string","required":true,"visibility":"user-or-llm","description":"Author name to search for"},"maxResults":{"type":"number","required":false,"visibility":"user-only","description":"Maximum number of results to return (default: 10, max: 2000)"}},"hostedApiKey":"none"},"arxiv_get_paper":{"id":"arxiv_get_paper","name":"ArXiv Get Paper","description":"Get detailed information about a specific ArXiv paper by its ID.","version":"1.0.0","params":{"paperId":{"type":"string","required":true,"visibility":"user-or-llm","description":"ArXiv paper ID (e.g., \\"1706.03762\\")"}},"hostedApiKey":"none"},"arxiv_search":{"id":"arxiv_search","name":"ArXiv Search","description":"Search for academic papers on ArXiv by keywords, authors, titles, or other fields.","version":"1.0.0","params":{"searchQuery":{"type":"string","required":true,"visibility":"user-or-llm","description":"The search query to execute"},"searchField":{"type":"string","required":false,"visibility":"user-only","description":"Field to search in: all, ti (title), au (author), abs (abstract), co (comment), jr (journal), cat (category), rn (report number)"},"maxResults":{"type":"number","required":false,"visibility":"user-only","description":"Maximum number of results to return (default: 10, max: 2000)"},"sortBy":{"type":"string","required":false,"visibility":"user-only","description":"Sort by: relevance, lastUpdatedDate, submittedDate (default: relevance)"},"sortOrder":{"type":"string","required":false,"visibility":"user-only","description":"Sort order: ascending, descending (default: descending)"}},"hostedApiKey":"none"},"asana_add_comment":{"id":"asana_add_comment","name":"Asana Add Comment","description":"Add a comment (story) to an Asana task","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana task GID (numeric string)"},"text":{"type":"string","required":true,"visibility":"user-or-llm","description":"The text content of the comment"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_add_followers":{"id":"asana_add_followers","name":"Asana Add Followers","description":"Add one or more followers to an Asana task","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"GID of the Asana task (numeric string)"},"followers":{"type":"array","required":true,"visibility":"user-or-llm","description":"Array of user GIDs to add as followers to the task"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_create_project":{"id":"asana_create_project","name":"Asana Create Project","description":"Create a new project in an Asana workspace","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"workspace":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana workspace GID (numeric string) where the project will be created"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the project"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"Notes or description for the project"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_create_section":{"id":"asana_create_section","name":"Asana Create Section","description":"Create a new section in an Asana project","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"projectGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"GID of the Asana project (numeric string) to add the section to"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the section"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_create_subtask":{"id":"asana_create_subtask","name":"Asana Create Subtask","description":"Create a subtask under an existing Asana task","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"GID of the parent Asana task (numeric string)"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the subtask"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"Notes or description for the subtask"},"assignee":{"type":"string","required":false,"visibility":"user-or-llm","description":"User GID to assign the subtask to"},"due_on":{"type":"string","required":false,"visibility":"user-or-llm","description":"Due date in YYYY-MM-DD format"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_create_task":{"id":"asana_create_task","name":"Asana Create Task","description":"Create a new task in Asana","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"workspace":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana workspace GID (numeric string) where the task will be created"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"Name of the task"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"Notes or description for the task"},"assignee":{"type":"string","required":false,"visibility":"user-or-llm","description":"User GID to assign the task to"},"due_on":{"type":"string","required":false,"visibility":"user-or-llm","description":"Due date in YYYY-MM-DD format"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_delete_task":{"id":"asana_delete_task","name":"Asana Delete Task","description":"Delete an Asana task by its GID (moves it to the trash)","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"GID of the Asana task to delete (numeric string)"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_get_project":{"id":"asana_get_project","name":"Asana Get Project","description":"Retrieve a single Asana project by its GID","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"projectGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana project GID (numeric string) to retrieve"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_get_projects":{"id":"asana_get_projects","name":"Asana Get Projects","description":"Retrieve all projects from an Asana workspace","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"workspace":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana workspace GID (numeric string) to retrieve projects from"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_get_task":{"id":"asana_get_task","name":"Asana Get Task","description":"Retrieve a single task by GID or get multiple tasks with filters","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":false,"visibility":"user-or-llm","description":"The globally unique identifier (GID) of the task. If not provided, will get multiple tasks."},"workspace":{"type":"string","required":false,"visibility":"user-or-llm","description":"Asana workspace GID (numeric string) to filter tasks (required when not using taskGid)"},"project":{"type":"string","required":false,"visibility":"user-or-llm","description":"Asana project GID (numeric string) to filter tasks"},"limit":{"type":"number","required":false,"visibility":"user-or-llm","description":"Maximum number of tasks to return (default: 50)"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_list_sections":{"id":"asana_list_sections","name":"Asana List Sections","description":"List all sections in an Asana project","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"projectGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"GID of the Asana project (numeric string) to list sections from"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_list_workspaces":{"id":"asana_list_workspaces","name":"Asana List Workspaces","description":"List all Asana workspaces and organizations the authenticated user belongs to","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_search_tasks":{"id":"asana_search_tasks","name":"Asana Search Tasks","description":"Search for tasks in an Asana workspace","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"workspace":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana workspace GID (numeric string) to search tasks in"},"text":{"type":"string","required":false,"visibility":"user-or-llm","description":"Text to search for in task names"},"assignee":{"type":"string","required":false,"visibility":"user-or-llm","description":"Filter tasks by assignee user GID"},"projects":{"type":"array","required":false,"visibility":"user-or-llm","description":"Array of Asana project GIDs (numeric strings) to filter tasks by"},"completed":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Filter by completion status"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"asana_update_task":{"id":"asana_update_task","name":"Asana Update Task","description":"Update an existing task in Asana","version":"1.0.0","params":{"accessToken":{"type":"string","required":true,"visibility":"hidden","description":"OAuth access token for Asana"},"taskGid":{"type":"string","required":true,"visibility":"user-or-llm","description":"Asana task GID (numeric string) of the task to update"},"name":{"type":"string","required":false,"visibility":"user-or-llm","description":"Updated name for the task"},"notes":{"type":"string","required":false,"visibility":"user-or-llm","description":"Updated notes or description for the task"},"assignee":{"type":"string","required":false,"visibility":"user-or-llm","description":"Updated assignee user GID"},"completed":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Mark task as completed or not completed"},"due_on":{"type":"string","required":false,"visibility":"user-or-llm","description":"Updated due date in YYYY-MM-DD format"}},"oauth":{"required":true,"provider":"asana"},"hostedApiKey":"none"},"ashby_add_candidate_tag":{"id":"ashby_add_candidate_tag","name":"Ashby Add Candidate Tag","description":"Adds a tag to a candidate in Ashby and returns the updated candidate.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"candidateId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the candidate to add the tag to"},"tagId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the tag to add"}},"hostedApiKey":"none"},"ashby_anonymize_candidate":{"id":"ashby_anonymize_candidate","name":"Ashby Anonymize Candidate","description":"Strips personally identifiable information from a candidate in Ashby. This does not delete the candidate - the record and its applications remain, with the PII removed. Ashby exposes no candidate deletion endpoint; true deletion is UI-only, restricted by role, and limited to a 10-day window. Requires the candidatesWrite permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"candidateId":{"type":"string","required":true,"visibility":"user-or-llm","description":"UUID of the candidate to anonymize"}},"hostedApiKey":"none"},"ashby_change_application_source":{"id":"ashby_change_application_source","name":"Ashby Change Application Source","description":"Changes the source attributed to an existing application, so programmatically created applications report correctly on the recruiting side. Requires the candidatesWrite permission.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"UUID of the application whose source should change"},"sourceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the source to attribute the application to, as returned by List Sources. Omit only when unsetSource is true."},"unsetSource":{"type":"boolean","required":false,"visibility":"user-or-llm","description":"Set true to deliberately clear the application source. Required to unset, so that a missing or empty sourceId cannot wipe attribution by accident."}},"hostedApiKey":"none"},"ashby_change_application_stage":{"id":"ashby_change_application_stage","name":"Ashby Change Application Stage","description":"Moves an application to a different interview stage. Requires an archive reason when moving to an Archived stage.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"applicationId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the application to update the stage of"},"interviewStageId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the interview stage to move the application to"},"archiveReasonId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Archive reason UUID. Required when moving to an Archived stage, ignored otherwise"},"archiveEmail":{"type":"json","required":false,"visibility":"user-or-llm","description":"Archive email configuration with communicationTemplateId and optional sendAt ISO 8601 timestamp. Pass null or omit to send no archive email."}},"hostedApiKey":"none"},"ashby_create_application":{"id":"ashby_create_application","name":"Ashby Create Application","description":"Creates a new application for a candidate on a job. Optionally specify interview plan, stage, source, and credited user.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"candidateId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the candidate to consider for the job"},"jobId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the job to consider the candidate for"},"interviewPlanId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the interview plan to use (defaults to the job default plan)"},"interviewStageId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the interview stage to place the application in, or FirstPreInterviewScreen (defaults to the first Lead stage)"},"sourceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the source to set on the application"},"creditedToUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the user the application is credited to"},"createdAt":{"type":"string","required":false,"visibility":"user-or-llm","description":"ISO 8601 timestamp to set as the application creation date (defaults to now)"},"applicationHistory":{"type":"json","required":false,"visibility":"user-or-llm","description":"Optional documented application history entries to create with the application"}},"hostedApiKey":"none"},"ashby_create_candidate":{"id":"ashby_create_candidate","name":"Ashby Create Candidate","description":"Creates a new candidate record in Ashby.","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"name":{"type":"string","required":true,"visibility":"user-or-llm","description":"The candidate full name"},"email":{"type":"string","required":false,"visibility":"user-or-llm","description":"Primary email address for the candidate"},"phoneNumber":{"type":"string","required":false,"visibility":"user-or-llm","description":"Primary phone number for the candidate"},"linkedInUrl":{"type":"string","required":false,"visibility":"user-or-llm","description":"LinkedIn profile URL"},"githubUrl":{"type":"string","required":false,"visibility":"user-or-llm","description":"GitHub profile URL"},"website":{"type":"string","required":false,"visibility":"user-or-llm","description":"Personal website URL"},"sourceId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the source to attribute the candidate to"},"creditedToUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"UUID of the Ashby user to credit with sourcing this candidate"},"createdAt":{"type":"string","required":false,"visibility":"user-or-llm","description":"Backdated creation timestamp in ISO 8601 (e.g. 2024-01-01T00:00:00Z). Defaults to now."},"alternateEmailAddresses":{"type":"json","required":false,"visibility":"user-or-llm","description":"Array of additional email address strings to add to the candidate, e.g. [\\"a@x.com\\",\\"b@y.com\\"]"},"location":{"type":"json","required":false,"visibility":"user-or-llm","description":"Candidate location object with optional city, region, and country"}},"hostedApiKey":"none"},"ashby_create_note":{"id":"ashby_create_note","name":"Ashby Create Note","description":"Creates a note on a candidate in Ashby. Supports plain text and HTML content (bold, italic, underline, links, lists, code).","version":"1.0.0","params":{"apiKey":{"type":"string","required":true,"visibility":"user-only","description":"Ashby API Key"},"onBehalfOfUserId":{"type":"string","required":false,"visibility":"user-or-llm","description":"Active Ashby user UUID to attribute this mutation to; the API key must permit on-behalf-of calls"},"candidateId":{"type":"string","required":true,"visibility":"user-or-llm","description":"The UUID of the candidate to add the note to"},"note":{"type":"string","required":true,"visibility":"user-or-llm","description":"The note content. If noteType is text/html, supports: , , , ,