chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { getErrorMessage } from '@sim/utils/errors'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import {
|
||||
type A2AFileInput,
|
||||
buildUserMessage,
|
||||
createA2AClient,
|
||||
isTaskResult,
|
||||
messageOutput,
|
||||
taskErrored,
|
||||
taskOutput,
|
||||
} from '@/lib/a2a/client'
|
||||
import { a2aSendMessageContract } from '@/lib/api/contracts/tools/a2a'
|
||||
import { getValidationErrorMessage, parseRequest } from '@/lib/api/server'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { enforceUserOrIpRateLimit } from '@/lib/core/rate-limiter'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
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'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
/** Blocking sends wait until the agent reaches a terminal/interrupted state. */
|
||||
export const maxDuration = 300
|
||||
|
||||
/** Per-file cap on attachments resolved from storage. */
|
||||
const A2A_MAX_FILE_BYTES = 10 * 1024 * 1024
|
||||
|
||||
const logger = createLogger('A2ASendMessageAPI')
|
||||
|
||||
export const POST = withRouteHandler(async (request: NextRequest) => {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: auth.error || 'Authentication required' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const rateLimited = await enforceUserOrIpRateLimit('a2a-send-message', auth.userId, request)
|
||||
if (rateLimited) return rateLimited
|
||||
|
||||
const parsed = await parseRequest(
|
||||
a2aSendMessageContract,
|
||||
request,
|
||||
{},
|
||||
{
|
||||
validationErrorResponse: (error) =>
|
||||
NextResponse.json(
|
||||
{ success: false, error: getValidationErrorMessage(error, 'Invalid request data') },
|
||||
{ status: 400 }
|
||||
),
|
||||
}
|
||||
)
|
||||
if (!parsed.success) return parsed.response
|
||||
const body = parsed.data.body
|
||||
|
||||
let data: unknown
|
||||
if (body.data !== undefined) {
|
||||
if (typeof body.data === 'string') {
|
||||
try {
|
||||
data = JSON.parse(body.data)
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Data must be valid JSON' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
} else {
|
||||
data = body.data
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
let files: A2AFileInput[] | undefined
|
||||
if (body.files?.length) {
|
||||
if (!auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: 'Authentication required to attach files' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
const userFiles = processFilesToUserFiles(body.files, requestId, logger)
|
||||
for (const userFile of userFiles) {
|
||||
const denied = await assertToolFileAccess(userFile.key, auth.userId, requestId, logger)
|
||||
if (denied) return denied
|
||||
}
|
||||
files = await Promise.all(
|
||||
userFiles.map(async (userFile) => {
|
||||
const { buffer, contentType } = await downloadServableFileFromStorage(
|
||||
userFile,
|
||||
requestId,
|
||||
logger,
|
||||
{ maxBytes: A2A_MAX_FILE_BYTES }
|
||||
)
|
||||
return {
|
||||
bytes: buffer,
|
||||
name: userFile.name,
|
||||
mediaType: contentType || userFile.type || 'application/octet-stream',
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const client = await createA2AClient(body.agentUrl, body.apiKey, { signal: request.signal })
|
||||
const message = buildUserMessage({
|
||||
text: body.message,
|
||||
data,
|
||||
files,
|
||||
taskId: body.taskId,
|
||||
contextId: body.contextId,
|
||||
})
|
||||
|
||||
const result = await client.sendMessage({
|
||||
tenant: '',
|
||||
message,
|
||||
configuration: undefined,
|
||||
metadata: undefined,
|
||||
})
|
||||
|
||||
if (!isTaskResult(result)) {
|
||||
logger.info(`[${requestId}] A2A send returned a direct message`)
|
||||
return NextResponse.json({ success: true, output: messageOutput(result) })
|
||||
}
|
||||
|
||||
const output = taskOutput(result)
|
||||
const errored = taskErrored(result)
|
||||
logger.info(`[${requestId}] A2A send produced task ${result.id} (${output.state})`)
|
||||
return NextResponse.json({
|
||||
success: !errored,
|
||||
...(errored ? { error: output.content || `Agent task ${output.state}` } : {}),
|
||||
output,
|
||||
})
|
||||
} catch (error) {
|
||||
const notReady = docNotReadyResponse(error)
|
||||
if (notReady) return notReady
|
||||
logger.error(`[${requestId}] A2A send failed`, { error: getErrorMessage(error) })
|
||||
return NextResponse.json({ success: false, error: getErrorMessage(error) }, { status: 502 })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user