-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(managed-agents): add Claude Managed Agents workflow block #5778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6496608
feat(managed-agents): add Claude Managed Agents workflow block
waleedlatif1 f88b08d
improvement(managed-agents): complete session inputs/outputs; trim bl…
waleedlatif1 ccdb679
improvement(managed-agents): select a Claude Platform credential inst…
waleedlatif1 3a0dbb9
fix(managed-agents): harden reconnect loop; fix credential-picker reg…
waleedlatif1 6898f40
fix(managed-agents): address review round 2
waleedlatif1 81ae4b1
fix(managed-agents): propagate cancellation, fix reconnect ordering, …
waleedlatif1 e0e9140
fix(managed-agents): stop leaked sessions on all give-up paths; harde…
waleedlatif1 e1ec209
fix(managed-agents): interrupt the session on a mid-run stream/API fa…
waleedlatif1 7ed0664
fix(managed-agents): skip idless stream previews; resolve service-acc…
waleedlatif1 01ba403
fix(managed-agents): keep idless terminals and preserve live pending …
waleedlatif1 67c8d26
fix(managed-agents): route memory via metadata on self-hosted environ…
waleedlatif1 c8b9bd7
feat(managed-agents): environment-type selector; hide cloud-only fiel…
waleedlatif1 80a8aa2
fix(managed-agents): track requires_action by processed_at, not histo…
waleedlatif1 79dc46e
fix(managed-agents): treat missing processed_at as oldest, not newest
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit' | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { | ||
| listManagedAgentOptionsContract, | ||
| type ManagedAgentOption, | ||
| type ManagedAgentResource, | ||
| } from '@/lib/api/contracts/managed-agents' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { authorizeCredentialUse } from '@/lib/auth/credential-access' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { CLAUDE_PLATFORM_SERVICE_ACCOUNT_PROVIDER_ID } from '@/lib/credentials/token-service-accounts/descriptors' | ||
| import { AGENT_MEMORY_BETA, managedAgentsList } from '@/lib/managed-agents/session-client' | ||
| import { captureServerEvent } from '@/lib/posthog/server' | ||
| import { resolveOAuthAccountId, resolveServiceAccountToken } from '@/app/api/auth/oauth/utils' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const logger = createLogger('ManagedAgentListAPI') | ||
|
|
||
| interface AnthropicListRow { | ||
| id?: string | ||
| name?: string | null | ||
| config?: { type?: 'cloud' | 'self_hosted' } | ||
| } | ||
|
|
||
| /** | ||
| * Anthropic list path + the beta header it requires. Memory-store endpoints | ||
| * use `agent-memory-2026-07-22`; combining it with the managed-agents beta is | ||
| * a documented 400, so each resource declares exactly one. | ||
| */ | ||
| const RESOURCE_ENDPOINTS: Record<ManagedAgentResource, { path: string; beta?: string }> = { | ||
| agents: { path: '/v1/agents' }, | ||
| environments: { path: '/v1/environments' }, | ||
| vaults: { path: '/v1/vaults' }, | ||
| 'memory-stores': { path: '/v1/memory_stores', beta: AGENT_MEMORY_BETA }, | ||
| } | ||
|
|
||
| function toOption( | ||
| resource: ManagedAgentResource, | ||
| row: AnthropicListRow | ||
| ): ManagedAgentOption | null { | ||
| if (!row.id) return null | ||
| const name = row.name?.trim() | ||
| if (resource === 'environments') { | ||
| const type = row.config?.type | ||
| const suffix = type ? ` (${type})` : '' | ||
| return { id: row.id, label: `${name || row.id}${suffix}`, ...(type ? { type } : {}) } | ||
| } | ||
| if (resource === 'vaults') { | ||
| return { id: row.id, label: name || row.id } | ||
| } | ||
| return { id: row.id, label: name ? `${name} (${row.id})` : row.id } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves Managed Agent dropdown options (agents / environments / vaults / | ||
| * memory stores) for the block editor against a selected Claude Platform | ||
| * credential. The credential's API key is decrypted server-side and never | ||
| * crosses the client boundary — the browser only ever receives `{ id, label }` | ||
| * options. | ||
| */ | ||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| const parsed = await parseRequest(listManagedAgentOptionsContract, request, {}) | ||
| if (!parsed.success) return parsed.response | ||
| const { credentialId, resource } = parsed.data.query | ||
|
|
||
| // Authenticates the caller AND verifies they may use this credential. | ||
| const authz = await authorizeCredentialUse(request, { | ||
| credentialId, | ||
| requireWorkflowIdForInternal: false, | ||
| }) | ||
| if (!authz.ok) { | ||
| return NextResponse.json({ error: authz.error || 'Unauthorized' }, { status: 403 }) | ||
| } | ||
|
|
||
| const resolved = await resolveOAuthAccountId(credentialId) | ||
| if ( | ||
| resolved?.credentialType !== 'service_account' || | ||
| resolved.providerId !== CLAUDE_PLATFORM_SERVICE_ACCOUNT_PROVIDER_ID | ||
| ) { | ||
| return NextResponse.json({ error: 'Not a Claude Platform credential' }, { status: 400 }) | ||
| } | ||
|
|
||
| let apiKey: string | ||
| try { | ||
| const token = await resolveServiceAccountToken( | ||
| credentialId, | ||
| CLAUDE_PLATFORM_SERVICE_ACCOUNT_PROVIDER_ID | ||
| ) | ||
| apiKey = token.accessToken | ||
| } catch (error) { | ||
| logger.warn('Failed to resolve Claude Platform credential', { error: getErrorMessage(error) }) | ||
| return NextResponse.json({ options: [] }) | ||
| } | ||
|
|
||
| // Decrypting and using the credential's key is a credential access — record | ||
| // it, mirroring the OAuth token route's service-account audit trail. | ||
| const actorId = authz.requesterUserId | ||
| const workspaceId = resolved.workspaceId ?? authz.workspaceId ?? null | ||
| if (actorId) { | ||
| recordAudit({ | ||
| workspaceId, | ||
| actorId, | ||
| action: AuditAction.CREDENTIAL_ACCESSED, | ||
| resourceType: AuditResourceType.CREDENTIAL, | ||
| resourceId: credentialId, | ||
| description: 'Accessed Claude Platform credential to list Managed Agent resources', | ||
| metadata: { | ||
| provider: CLAUDE_PLATFORM_SERVICE_ACCOUNT_PROVIDER_ID, | ||
| credentialType: 'service_account', | ||
| }, | ||
| request, | ||
| }) | ||
| captureServerEvent( | ||
| actorId, | ||
| 'credential_used', | ||
| { | ||
| credential_type: 'service_account', | ||
| provider_id: CLAUDE_PLATFORM_SERVICE_ACCOUNT_PROVIDER_ID, | ||
| ...(workspaceId ? { workspace_id: workspaceId } : {}), | ||
| }, | ||
| workspaceId ? { groups: { workspace: workspaceId } } : undefined | ||
| ) | ||
| } | ||
|
|
||
| try { | ||
| const endpoint = RESOURCE_ENDPOINTS[resource] | ||
| const rows = await managedAgentsList<AnthropicListRow>({ | ||
| apiKey, | ||
| path: endpoint.path, | ||
| beta: endpoint.beta, | ||
| signal: request.signal, | ||
| }) | ||
| const options = rows | ||
| .map((row) => toOption(resource, row)) | ||
| .filter((option): option is ManagedAgentOption => option !== null) | ||
| return NextResponse.json({ options }) | ||
| } catch (error) { | ||
| // Some beta workspaces may not expose every resource (e.g. vaults). Log | ||
| // and degrade to an empty list rather than breaking the editor. | ||
| logger.warn('Managed agent list proxy failed', { resource, error: getErrorMessage(error) }) | ||
| return NextResponse.json({ options: [] }) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.