mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-07 05:05:15 -05:00
Compare commits
13 Commits
feat/the-c
...
fix/logs-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ebe751e8f | ||
|
|
f615be61f2 | ||
|
|
c9691fc437 | ||
|
|
7ce442f499 | ||
|
|
cf1792e408 | ||
|
|
36e6133a08 | ||
|
|
94ad777e5e | ||
|
|
6ef3b96395 | ||
|
|
8b6796eabe | ||
|
|
895eec3c41 | ||
|
|
0ca25bbab6 | ||
|
|
1edaf197b2 | ||
|
|
474b1af145 |
@@ -5462,3 +5462,24 @@ export function EnrichSoIcon(props: SVGProps<SVGSVGElement>) {
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
export function AgentSkillsIcon(props: SVGProps<SVGSVGElement>) {
|
||||
return (
|
||||
<svg
|
||||
{...props}
|
||||
xmlns='http://www.w3.org/2000/svg'
|
||||
width='16'
|
||||
height='16'
|
||||
viewBox='0 0 16 16'
|
||||
fill='none'
|
||||
>
|
||||
<path
|
||||
d='M8 1L14.0622 4.5V11.5L8 15L1.93782 11.5V4.5L8 1Z'
|
||||
stroke='currentColor'
|
||||
strokeWidth='1.5'
|
||||
fill='none'
|
||||
/>
|
||||
<path d='M8 4.5L11 6.25V9.75L8 11.5L5 9.75V6.25L8 4.5Z' fill='currentColor' />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ This means you can attach many skills to an agent without bloating its context w
|
||||
|
||||
## Creating Skills
|
||||
|
||||
Go to **Settings** (gear icon) and select **Skills** under the Tools section.
|
||||
Go to **Settings** and select **Skills** under the Tools section.
|
||||
|
||||

|
||||
|
||||
Click **Add** to create a new skill with three fields:
|
||||
|
||||
@@ -52,11 +54,22 @@ Use when the user asks you to write, optimize, or debug SQL queries.
|
||||
...
|
||||
```
|
||||
|
||||
**Recommended structure:**
|
||||
- **When to use** — Specific triggers and scenarios
|
||||
- **Instructions** — Step-by-step guidance with numbered lists
|
||||
- **Examples** — Input/output samples showing expected behavior
|
||||
- **Common Patterns** — Reusable approaches for frequent tasks
|
||||
- **Edge Cases** — Gotchas and special considerations
|
||||
|
||||
Keep skills focused and under 500 lines. If a skill grows too large, split it into multiple specialized skills.
|
||||
|
||||
## Adding Skills to an Agent
|
||||
|
||||
Open any **Agent** block and find the **Skills** dropdown below the tools section. Select the skills you want the agent to have access to.
|
||||
|
||||
Selected skills appear as chips that you can click to edit or remove.
|
||||

|
||||
|
||||
Selected skills appear as cards that you can click to edit or remove.
|
||||
|
||||
### What Happens at Runtime
|
||||
|
||||
@@ -69,12 +82,50 @@ When the workflow runs:
|
||||
|
||||
This works across all supported LLM providers — the `load_skill` tool uses standard tool-calling, so no provider-specific configuration is needed.
|
||||
|
||||
## Tips
|
||||
## Common Use Cases
|
||||
|
||||
- **Keep descriptions actionable** — Instead of "Helps with SQL", write "Write optimized SQL queries for PostgreSQL, MySQL, and SQLite, including index recommendations and query plan analysis"
|
||||
Skills are most valuable when agents need specialized knowledge or multi-step workflows:
|
||||
|
||||
**Domain Expertise**
|
||||
- `api-integration-expert` — Best practices for calling specific APIs (authentication, rate limiting, error handling)
|
||||
- `data-transformation` — ETL patterns, data cleaning, and validation rules
|
||||
- `code-reviewer` — Code review guidelines specific to your team's standards
|
||||
|
||||
**Workflow Templates**
|
||||
- `bug-investigation` — Step-by-step debugging methodology (reproduce → isolate → test → fix)
|
||||
- `feature-implementation` — Development workflow from requirements to deployment
|
||||
- `document-generator` — Templates and formatting rules for technical documentation
|
||||
|
||||
**Company-Specific Knowledge**
|
||||
- `our-architecture` — System architecture diagrams, service dependencies, and deployment processes
|
||||
- `style-guide` — Brand guidelines, writing tone, UI/UX patterns
|
||||
- `customer-onboarding` — Standard procedures and common customer questions
|
||||
|
||||
**When to use skills vs. agent instructions:**
|
||||
- Use **skills** for knowledge that applies across multiple workflows or changes frequently
|
||||
- Use **agent instructions** for task-specific context that's unique to a single agent
|
||||
|
||||
## Best Practices
|
||||
|
||||
**Writing Effective Descriptions**
|
||||
- **Be specific and keyword-rich** — Instead of "Helps with SQL", write "Write optimized SQL queries for PostgreSQL, MySQL, and SQLite, including index recommendations and query plan analysis"
|
||||
- **Include activation triggers** — Mention specific words or phrases that should prompt the skill (e.g., "Use when the user mentions PDFs, forms, or document extraction")
|
||||
- **Keep it under 200 words** — Agents scan descriptions quickly; make every word count
|
||||
|
||||
**Skill Scope and Organization**
|
||||
- **One skill per domain** — A focused `sql-expert` skill works better than a broad `database-everything` skill
|
||||
- **Use markdown structure** — Headers, lists, and code blocks help the agent parse and follow instructions
|
||||
- **Test iteratively** — Run your workflow and check if the agent activates the skill when expected
|
||||
- **Limit to 5-10 skills per agent** — More skills = more decision overhead; start small and add as needed
|
||||
- **Split large skills** — If a skill exceeds 500 lines, break it into focused sub-skills
|
||||
|
||||
**Content Structure**
|
||||
- **Use markdown formatting** — Headers, lists, and code blocks help agents parse and follow instructions
|
||||
- **Provide examples** — Show input/output pairs so agents understand expected behavior
|
||||
- **Be explicit about edge cases** — Don't assume agents will infer special handling
|
||||
|
||||
**Testing and Iteration**
|
||||
- **Test activation** — Run your workflow and verify the agent loads the skill when expected
|
||||
- **Check for false positives** — Make sure skills aren't activating when they shouldn't
|
||||
- **Refine descriptions** — If a skill isn't loading when needed, add more keywords to the description
|
||||
|
||||
## Learn More
|
||||
|
||||
|
||||
@@ -10,6 +10,21 @@ import { BlockInfoCard } from "@/components/ui/block-info-card"
|
||||
color="#6366F1"
|
||||
/>
|
||||
|
||||
{/* MANUAL-CONTENT-START:intro */}
|
||||
[Airweave](https://airweave.ai/) is an AI-powered semantic search platform that helps you discover and retrieve knowledge across all your synced data sources. Built for modern teams, Airweave enables fast, relevant search results using neural, hybrid, or keyword-based strategies tailored to your needs.
|
||||
|
||||
With Airweave, you can:
|
||||
|
||||
- **Search smarter**: Use natural language queries to uncover information stored across your connected tools and databases
|
||||
- **Unify your data**: Seamlessly access content from sources like code, docs, chat, emails, cloud files, and more
|
||||
- **Customize retrieval**: Select between hybrid (semantic + keyword), neural, or keyword search strategies for optimal results
|
||||
- **Boost recall**: Expand search queries with AI to find more comprehensive answers
|
||||
- **Rerank results using AI**: Prioritize the most relevant answers with powerful language models
|
||||
- **Get instant answers**: Generate clear, AI-powered responses synthesized from your data
|
||||
|
||||
In Sim, the Airweave integration empowers your agents to search, summarize, and extract insights from all your organization’s data via a single tool. Use Airweave to drive rich, contextual knowledge retrieval within your workflows—whether answering questions, generating summaries, or supporting dynamic decision-making.
|
||||
{/* MANUAL-CONTENT-END */}
|
||||
|
||||
## Usage Instructions
|
||||
|
||||
Search across your synced data sources using Airweave. Supports semantic search with hybrid, neural, or keyword retrieval strategies. Optionally generate AI-powered answers from search results.
|
||||
|
||||
BIN
apps/docs/public/static/skills/add-skill.png
Normal file
BIN
apps/docs/public/static/skills/add-skill.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
BIN
apps/docs/public/static/skills/manage-skills.png
Normal file
BIN
apps/docs/public/static/skills/manage-skills.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 56 KiB |
@@ -5,7 +5,7 @@ import { eq } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { generateAgentCard, generateSkillsFromWorkflow } from '@/lib/a2a/agent-card'
|
||||
import type { AgentCapabilities, AgentSkill } from '@/lib/a2a/types'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { getRedisClient } from '@/lib/core/config/redis'
|
||||
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
|
||||
import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils'
|
||||
@@ -40,7 +40,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<Ro
|
||||
}
|
||||
|
||||
if (!agent.agent.isPublished) {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success) {
|
||||
return NextResponse.json({ error: 'Agent not published' }, { status: 404 })
|
||||
}
|
||||
@@ -81,7 +81,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<Ro
|
||||
const { agentId } = await params
|
||||
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
@@ -151,7 +151,7 @@ export async function DELETE(request: NextRequest, { params }: { params: Promise
|
||||
const { agentId } = await params
|
||||
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
@@ -189,7 +189,7 @@ export async function POST(request: NextRequest, { params }: { params: Promise<R
|
||||
const { agentId } = await params
|
||||
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
logger.warn('A2A agent publish auth failed:', { error: auth.error, hasUserId: !!auth.userId })
|
||||
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
||||
|
||||
@@ -13,7 +13,7 @@ import { v4 as uuidv4 } from 'uuid'
|
||||
import { generateSkillsFromWorkflow } from '@/lib/a2a/agent-card'
|
||||
import { A2A_DEFAULT_CAPABILITIES } from '@/lib/a2a/constants'
|
||||
import { sanitizeAgentName } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
|
||||
import { hasValidStartBlockInState } from '@/lib/workflows/triggers/trigger-utils'
|
||||
import { getWorkspaceById } from '@/lib/workspaces/permissions/utils'
|
||||
@@ -27,7 +27,7 @@ export const dynamic = 'force-dynamic'
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
@@ -87,7 +87,7 @@ export async function GET(request: NextRequest) {
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { and, eq } from 'drizzle-orm'
|
||||
import { jwtDecode } from 'jwt-decode'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { evaluateScopeCoverage, type OAuthProvider, parseProvider } from '@/lib/oauth'
|
||||
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
@@ -81,7 +81,7 @@ export async function GET(request: NextRequest) {
|
||||
const { provider: providerParam, workflowId, credentialId } = parseResult.data
|
||||
|
||||
// Authenticate requester (supports session, API key, internal JWT)
|
||||
const authResult = await checkHybridAuth(request)
|
||||
const authResult = await checkSessionOrInternalAuth(request)
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthenticated credentials request rejected`)
|
||||
return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
|
||||
|
||||
@@ -12,7 +12,7 @@ describe('OAuth Token API Routes', () => {
|
||||
const mockRefreshTokenIfNeeded = vi.fn()
|
||||
const mockGetOAuthToken = vi.fn()
|
||||
const mockAuthorizeCredentialUse = vi.fn()
|
||||
const mockCheckHybridAuth = vi.fn()
|
||||
const mockCheckSessionOrInternalAuth = vi.fn()
|
||||
|
||||
const mockLogger = createMockLogger()
|
||||
|
||||
@@ -42,7 +42,7 @@ describe('OAuth Token API Routes', () => {
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: mockCheckHybridAuth,
|
||||
checkSessionOrInternalAuth: mockCheckSessionOrInternalAuth,
|
||||
}))
|
||||
})
|
||||
|
||||
@@ -235,7 +235,7 @@ describe('OAuth Token API Routes', () => {
|
||||
|
||||
describe('credentialAccountUserId + providerId path', () => {
|
||||
it('should reject unauthenticated requests', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: false,
|
||||
error: 'Authentication required',
|
||||
})
|
||||
@@ -255,30 +255,8 @@ describe('OAuth Token API Routes', () => {
|
||||
expect(mockGetOAuthToken).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should reject API key authentication', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'api_key',
|
||||
userId: 'test-user-id',
|
||||
})
|
||||
|
||||
const req = createMockRequest('POST', {
|
||||
credentialAccountUserId: 'test-user-id',
|
||||
providerId: 'google',
|
||||
})
|
||||
|
||||
const { POST } = await import('@/app/api/auth/oauth/token/route')
|
||||
|
||||
const response = await POST(req)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
expect(data).toHaveProperty('error', 'User not authenticated')
|
||||
expect(mockGetOAuthToken).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should reject internal JWT authentication', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'internal_jwt',
|
||||
userId: 'test-user-id',
|
||||
@@ -300,7 +278,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should reject requests for other users credentials', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'attacker-user-id',
|
||||
@@ -322,7 +300,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should allow session-authenticated users to access their own credentials', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
@@ -345,7 +323,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should return 404 when credential not found for user', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
@@ -373,7 +351,7 @@ describe('OAuth Token API Routes', () => {
|
||||
*/
|
||||
describe('GET handler', () => {
|
||||
it('should return access token successfully', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
@@ -402,7 +380,7 @@ describe('OAuth Token API Routes', () => {
|
||||
expect(response.status).toBe(200)
|
||||
expect(data).toHaveProperty('accessToken', 'fresh-token')
|
||||
|
||||
expect(mockCheckHybridAuth).toHaveBeenCalled()
|
||||
expect(mockCheckSessionOrInternalAuth).toHaveBeenCalled()
|
||||
expect(mockGetCredential).toHaveBeenCalledWith(mockRequestId, 'credential-id', 'test-user-id')
|
||||
expect(mockRefreshTokenIfNeeded).toHaveBeenCalled()
|
||||
})
|
||||
@@ -421,7 +399,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should handle authentication failure', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: false,
|
||||
error: 'Authentication required',
|
||||
})
|
||||
@@ -440,7 +418,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should handle credential not found', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
@@ -461,7 +439,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should handle missing access token', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
@@ -487,7 +465,7 @@ describe('OAuth Token API Routes', () => {
|
||||
})
|
||||
|
||||
it('should handle token refresh failure', async () => {
|
||||
mockCheckHybridAuth.mockResolvedValueOnce({
|
||||
mockCheckSessionOrInternalAuth.mockResolvedValueOnce({
|
||||
success: true,
|
||||
authType: 'session',
|
||||
userId: 'test-user-id',
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { getCredential, getOAuthToken, refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
|
||||
|
||||
@@ -71,7 +71,7 @@ export async function POST(request: NextRequest) {
|
||||
providerId,
|
||||
})
|
||||
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || auth.authType !== 'session' || !auth.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized request for credentialAccountUserId path`, {
|
||||
success: auth.success,
|
||||
@@ -187,7 +187,7 @@ export async function GET(request: NextRequest) {
|
||||
const { credentialId } = parseResult.data
|
||||
|
||||
// For GET requests, we only support session-based authentication
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || auth.authType !== 'session' || !auth.userId) {
|
||||
return NextResponse.json({ error: 'User not authenticated' }, { status: 401 })
|
||||
}
|
||||
|
||||
@@ -285,6 +285,14 @@ export async function POST(req: NextRequest) {
|
||||
apiVersion: 'preview',
|
||||
endpoint: env.AZURE_OPENAI_ENDPOINT,
|
||||
}
|
||||
} else if (providerEnv === 'azure-anthropic') {
|
||||
providerConfig = {
|
||||
provider: 'azure-anthropic',
|
||||
model: envModel,
|
||||
apiKey: env.AZURE_ANTHROPIC_API_KEY,
|
||||
apiVersion: env.AZURE_ANTHROPIC_API_VERSION,
|
||||
endpoint: env.AZURE_ANTHROPIC_ENDPOINT,
|
||||
}
|
||||
} else if (providerEnv === 'vertex') {
|
||||
providerConfig = {
|
||||
provider: 'vertex',
|
||||
|
||||
@@ -29,7 +29,7 @@ function setupFileApiMocks(
|
||||
}
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: authenticated,
|
||||
userId: authenticated ? 'test-user-id' : undefined,
|
||||
error: authenticated ? undefined : 'Unauthorized',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import type { StorageContext } from '@/lib/uploads/config'
|
||||
import { deleteFile, hasCloudStorage } from '@/lib/uploads/core/storage-service'
|
||||
import { extractStorageKey, inferContextFromKey } from '@/lib/uploads/utils/file-utils'
|
||||
@@ -24,7 +24,7 @@ const logger = createLogger('FilesDeleteAPI')
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn('Unauthorized file delete request', {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import type { StorageContext } from '@/lib/uploads/config'
|
||||
import { hasCloudStorage } from '@/lib/uploads/core/storage-service'
|
||||
import { verifyFileAccess } from '@/app/api/files/authorization'
|
||||
@@ -12,7 +12,7 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn('Unauthorized download URL request', {
|
||||
|
||||
@@ -35,7 +35,7 @@ function setupFileApiMocks(
|
||||
}
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: authenticated,
|
||||
userId: authenticated ? 'test-user-id' : undefined,
|
||||
error: authenticated ? undefined : 'Unauthorized',
|
||||
|
||||
@@ -5,7 +5,7 @@ import path from 'path'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import binaryExtensionsList from 'binary-extensions'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkInternalAuth } from '@/lib/auth/hybrid'
|
||||
import {
|
||||
secureFetchWithPinnedIP,
|
||||
validateUrlWithDNS,
|
||||
@@ -66,7 +66,7 @@ export async function POST(request: NextRequest) {
|
||||
const startTime = Date.now()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: true })
|
||||
const authResult = await checkInternalAuth(request, { requireWorkflowId: true })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn('Unauthorized file parse request', {
|
||||
|
||||
@@ -55,7 +55,7 @@ describe('File Serve API Route', () => {
|
||||
})
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'test-user-id',
|
||||
}),
|
||||
@@ -165,7 +165,7 @@ describe('File Serve API Route', () => {
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'test-user-id',
|
||||
}),
|
||||
@@ -226,7 +226,7 @@ describe('File Serve API Route', () => {
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'test-user-id',
|
||||
}),
|
||||
@@ -291,7 +291,7 @@ describe('File Serve API Route', () => {
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'test-user-id',
|
||||
}),
|
||||
@@ -350,7 +350,7 @@ describe('File Serve API Route', () => {
|
||||
for (const test of contentTypeTests) {
|
||||
it(`should serve ${test.ext} file with correct content type`, async () => {
|
||||
vi.doMock('@/lib/auth/hybrid', () => ({
|
||||
checkHybridAuth: vi.fn().mockResolvedValue({
|
||||
checkSessionOrInternalAuth: vi.fn().mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'test-user-id',
|
||||
}),
|
||||
|
||||
@@ -2,7 +2,7 @@ import { readFile } from 'fs/promises'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { CopilotFiles, isUsingCloudStorage } from '@/lib/uploads'
|
||||
import type { StorageContext } from '@/lib/uploads/config'
|
||||
import { downloadFile } from '@/lib/uploads/core/storage-service'
|
||||
@@ -49,7 +49,7 @@ export async function GET(
|
||||
return await handleLocalFilePublic(fullPath)
|
||||
}
|
||||
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn('Unauthorized file access attempt', {
|
||||
|
||||
@@ -845,6 +845,8 @@ export async function POST(req: NextRequest) {
|
||||
contextVariables,
|
||||
timeoutMs: timeout,
|
||||
requestId,
|
||||
ownerKey: `user:${auth.userId}`,
|
||||
ownerWeight: 1,
|
||||
})
|
||||
|
||||
const executionTime = Date.now() - startTime
|
||||
|
||||
@@ -23,7 +23,16 @@ export async function POST(request: NextRequest) {
|
||||
topK,
|
||||
model,
|
||||
apiKey,
|
||||
azureEndpoint,
|
||||
azureApiVersion,
|
||||
vertexProject,
|
||||
vertexLocation,
|
||||
vertexCredential,
|
||||
bedrockAccessKeyId,
|
||||
bedrockSecretKey,
|
||||
bedrockRegion,
|
||||
workflowId,
|
||||
workspaceId,
|
||||
piiEntityTypes,
|
||||
piiMode,
|
||||
piiLanguage,
|
||||
@@ -110,7 +119,18 @@ export async function POST(request: NextRequest) {
|
||||
topK,
|
||||
model,
|
||||
apiKey,
|
||||
{
|
||||
azureEndpoint,
|
||||
azureApiVersion,
|
||||
vertexProject,
|
||||
vertexLocation,
|
||||
vertexCredential,
|
||||
bedrockAccessKeyId,
|
||||
bedrockSecretKey,
|
||||
bedrockRegion,
|
||||
},
|
||||
workflowId,
|
||||
workspaceId,
|
||||
piiEntityTypes,
|
||||
piiMode,
|
||||
piiLanguage,
|
||||
@@ -178,7 +198,18 @@ async function executeValidation(
|
||||
topK: string | undefined,
|
||||
model: string,
|
||||
apiKey: string | undefined,
|
||||
providerCredentials: {
|
||||
azureEndpoint?: string
|
||||
azureApiVersion?: string
|
||||
vertexProject?: string
|
||||
vertexLocation?: string
|
||||
vertexCredential?: string
|
||||
bedrockAccessKeyId?: string
|
||||
bedrockSecretKey?: string
|
||||
bedrockRegion?: string
|
||||
},
|
||||
workflowId: string | undefined,
|
||||
workspaceId: string | undefined,
|
||||
piiEntityTypes: string[] | undefined,
|
||||
piiMode: string | undefined,
|
||||
piiLanguage: string | undefined,
|
||||
@@ -219,7 +250,9 @@ async function executeValidation(
|
||||
topK: topK ? Number.parseInt(topK) : 10, // Default topK is 10
|
||||
model: model,
|
||||
apiKey,
|
||||
providerCredentials,
|
||||
workflowId,
|
||||
workspaceId,
|
||||
requestId,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { randomUUID } from 'crypto'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { SUPPORTED_FIELD_TYPES } from '@/lib/knowledge/constants'
|
||||
import { createTagDefinition, getTagDefinitions } from '@/lib/knowledge/tags/service'
|
||||
import { checkKnowledgeBaseAccess } from '@/app/api/knowledge/utils'
|
||||
@@ -19,19 +19,11 @@ export async function GET(req: NextRequest, { params }: { params: Promise<{ id:
|
||||
try {
|
||||
logger.info(`[${requestId}] Getting tag definitions for knowledge base ${knowledgeBaseId}`)
|
||||
|
||||
const auth = await checkHybridAuth(req, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(req, { requireWorkflowId: false })
|
||||
if (!auth.success) {
|
||||
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
// Only allow session and internal JWT auth (not API key)
|
||||
if (auth.authType === 'api_key') {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key auth not supported for this endpoint' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
// For session auth, verify KB access. Internal JWT is trusted.
|
||||
if (auth.authType === 'session' && auth.userId) {
|
||||
const accessCheck = await checkKnowledgeBaseAccess(knowledgeBaseId, auth.userId)
|
||||
@@ -64,19 +56,11 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
|
||||
try {
|
||||
logger.info(`[${requestId}] Creating tag definition for knowledge base ${knowledgeBaseId}`)
|
||||
|
||||
const auth = await checkHybridAuth(req, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(req, { requireWorkflowId: false })
|
||||
if (!auth.success) {
|
||||
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
// Only allow session and internal JWT auth (not API key)
|
||||
if (auth.authType === 'api_key') {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key auth not supported for this endpoint' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
// For session auth, verify KB access. Internal JWT is trusted.
|
||||
if (auth.authType === 'session' && auth.userId) {
|
||||
const accessCheck = await checkKnowledgeBaseAccess(knowledgeBaseId, auth.userId)
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { and, eq, inArray } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import type { TraceSpan, WorkflowExecutionLog } from '@/lib/logs/types'
|
||||
|
||||
@@ -23,7 +23,7 @@ export async function GET(
|
||||
try {
|
||||
const { executionId } = await params
|
||||
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized execution data access attempt for: ${executionId}`)
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -4,7 +4,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
@@ -36,7 +36,7 @@ async function validateMemoryAccess(
|
||||
requestId: string,
|
||||
action: 'read' | 'write'
|
||||
): Promise<{ userId: string } | { error: NextResponse }> {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized memory ${action} attempt`)
|
||||
return {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { memory } from '@sim/db/schema'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { and, eq, isNull, like } from 'drizzle-orm'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
@@ -16,7 +16,7 @@ export async function GET(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request)
|
||||
const authResult = await checkInternalAuth(request)
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized memory access attempt`)
|
||||
return NextResponse.json(
|
||||
@@ -89,7 +89,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request)
|
||||
const authResult = await checkInternalAuth(request)
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized memory creation attempt`)
|
||||
return NextResponse.json(
|
||||
@@ -228,7 +228,7 @@ export async function DELETE(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request)
|
||||
const authResult = await checkInternalAuth(request)
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
logger.warn(`[${requestId}] Unauthorized memory deletion attempt`)
|
||||
return NextResponse.json(
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
const logger = createLogger('A2ACancelTaskAPI')
|
||||
@@ -20,7 +20,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A cancel task attempt`)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
@@ -20,7 +20,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
@@ -18,7 +18,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A get agent card attempt: ${authResult.error}`)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
@@ -19,7 +19,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
@@ -21,7 +21,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A get task attempt: ${authResult.error}`)
|
||||
|
||||
@@ -10,7 +10,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient, extractTextContent, isTerminalState } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
const logger = createLogger('A2AResubscribeAPI')
|
||||
@@ -27,7 +27,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A resubscribe attempt`)
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient, extractTextContent, isTerminalState } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { validateUrlWithDNS } from '@/lib/core/security/input-validation.server'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
@@ -32,7 +32,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A send message attempt: ${authResult.error}`)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { createA2AClient } from '@/lib/a2a/utils'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { validateUrlWithDNS } from '@/lib/core/security/input-validation.server'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
|
||||
@@ -22,7 +22,7 @@ export async function POST(request: NextRequest) {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
|
||||
if (!authResult.success) {
|
||||
logger.warn(`[${requestId}] Unauthorized A2A set push notification attempt`, {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { z } from 'zod'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { getUserUsageLogs, type UsageLogSource } from '@/lib/billing/core/usage-log'
|
||||
|
||||
const logger = createLogger('UsageLogsAPI')
|
||||
@@ -20,7 +20,7 @@ const QuerySchema = z.object({
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const auth = await checkHybridAuth(req, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(req, { requireWorkflowId: false })
|
||||
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
@@ -325,6 +325,11 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
|
||||
requestId
|
||||
)
|
||||
|
||||
// Client-side sessions and personal API keys bill/permission-check the
|
||||
// authenticated user, not the workspace billed account.
|
||||
const useAuthenticatedUserAsActor =
|
||||
isClientSession || (auth.authType === 'api_key' && auth.apiKeyType === 'personal')
|
||||
|
||||
const preprocessResult = await preprocessExecution({
|
||||
workflowId,
|
||||
userId,
|
||||
@@ -334,6 +339,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
|
||||
checkDeployment: !shouldUseDraftState,
|
||||
loggingSession,
|
||||
useDraftState: shouldUseDraftState,
|
||||
useAuthenticatedUserAsActor,
|
||||
})
|
||||
|
||||
if (!preprocessResult.success) {
|
||||
|
||||
@@ -74,8 +74,7 @@ function FileCard({ file, isExecutionFile = false, workspaceId }: FileCardProps)
|
||||
}
|
||||
|
||||
if (isExecutionFile) {
|
||||
const serveUrl =
|
||||
file.url || `/api/files/serve/${encodeURIComponent(file.key)}?context=execution`
|
||||
const serveUrl = `/api/files/serve/${encodeURIComponent(file.key)}?context=execution`
|
||||
window.open(serveUrl, '_blank')
|
||||
logger.info(`Opened execution file serve URL: ${serveUrl}`)
|
||||
} else {
|
||||
@@ -88,16 +87,12 @@ function FileCard({ file, isExecutionFile = false, workspaceId }: FileCardProps)
|
||||
logger.warn(
|
||||
`Could not construct viewer URL for file: ${file.name}, falling back to serve URL`
|
||||
)
|
||||
const serveUrl =
|
||||
file.url || `/api/files/serve/${encodeURIComponent(file.key)}?context=workspace`
|
||||
const serveUrl = `/api/files/serve/${encodeURIComponent(file.key)}?context=workspace`
|
||||
window.open(serveUrl, '_blank')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Failed to download file ${file.name}:`, error)
|
||||
if (file.url) {
|
||||
window.open(file.url, '_blank')
|
||||
}
|
||||
} finally {
|
||||
setIsDownloading(false)
|
||||
}
|
||||
@@ -198,8 +193,7 @@ export function FileDownload({
|
||||
}
|
||||
|
||||
if (isExecutionFile) {
|
||||
const serveUrl =
|
||||
file.url || `/api/files/serve/${encodeURIComponent(file.key)}?context=execution`
|
||||
const serveUrl = `/api/files/serve/${encodeURIComponent(file.key)}?context=execution`
|
||||
window.open(serveUrl, '_blank')
|
||||
logger.info(`Opened execution file serve URL: ${serveUrl}`)
|
||||
} else {
|
||||
@@ -212,16 +206,12 @@ export function FileDownload({
|
||||
logger.warn(
|
||||
`Could not construct viewer URL for file: ${file.name}, falling back to serve URL`
|
||||
)
|
||||
const serveUrl =
|
||||
file.url || `/api/files/serve/${encodeURIComponent(file.key)}?context=workspace`
|
||||
const serveUrl = `/api/files/serve/${encodeURIComponent(file.key)}?context=workspace`
|
||||
window.open(serveUrl, '_blank')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Failed to download file ${file.name}:`, error)
|
||||
if (file.url) {
|
||||
window.open(file.url, '_blank')
|
||||
}
|
||||
} finally {
|
||||
setIsDownloading(false)
|
||||
}
|
||||
|
||||
@@ -130,39 +130,52 @@ export function SkillInput({
|
||||
onOpenChange={setOpen}
|
||||
/>
|
||||
|
||||
{selectedSkills.length > 0 && (
|
||||
<div className='flex flex-wrap gap-[4px]'>
|
||||
{selectedSkills.map((stored) => {
|
||||
const fullSkill = workspaceSkills.find((s) => s.id === stored.skillId)
|
||||
return (
|
||||
{selectedSkills.length > 0 &&
|
||||
selectedSkills.map((stored) => {
|
||||
const fullSkill = workspaceSkills.find((s) => s.id === stored.skillId)
|
||||
return (
|
||||
<div
|
||||
key={stored.skillId}
|
||||
className='group relative flex flex-col overflow-hidden rounded-[4px] border border-[var(--border-1)] transition-all duration-200 ease-in-out'
|
||||
>
|
||||
<div
|
||||
key={stored.skillId}
|
||||
className='flex cursor-pointer items-center gap-[4px] rounded-[4px] border border-[var(--border-1)] bg-[var(--surface-5)] px-[6px] py-[2px] font-medium text-[12px] text-[var(--text-secondary)] hover:bg-[var(--surface-6)]'
|
||||
className='flex cursor-pointer items-center justify-between gap-[8px] rounded-t-[4px] bg-[var(--surface-4)] px-[8px] py-[6.5px]'
|
||||
onClick={() => {
|
||||
if (fullSkill && !disabled && !isPreview) {
|
||||
setEditingSkill(fullSkill)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<AgentSkillsIcon className='h-[10px] w-[10px] text-[var(--text-tertiary)]' />
|
||||
<span className='max-w-[140px] truncate'>{resolveSkillName(stored)}</span>
|
||||
{!disabled && !isPreview && (
|
||||
<button
|
||||
type='button'
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
handleRemove(stored.skillId)
|
||||
}}
|
||||
className='ml-[2px] rounded-[2px] p-[1px] text-[var(--text-tertiary)] hover:bg-[var(--surface-7)] hover:text-[var(--text-secondary)]'
|
||||
<div className='flex min-w-0 flex-1 items-center gap-[8px]'>
|
||||
<div
|
||||
className='flex h-[16px] w-[16px] flex-shrink-0 items-center justify-center rounded-[4px]'
|
||||
style={{ backgroundColor: '#e0e0e0' }}
|
||||
>
|
||||
<XIcon className='h-[10px] w-[10px]' />
|
||||
</button>
|
||||
)}
|
||||
<AgentSkillsIcon className='h-[10px] w-[10px] text-[#333]' />
|
||||
</div>
|
||||
<span className='truncate font-medium text-[13px] text-[var(--text-primary)]'>
|
||||
{resolveSkillName(stored)}
|
||||
</span>
|
||||
</div>
|
||||
<div className='flex flex-shrink-0 items-center gap-[8px]'>
|
||||
{!disabled && !isPreview && (
|
||||
<button
|
||||
type='button'
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
handleRemove(stored.skillId)
|
||||
}}
|
||||
className='flex items-center justify-center text-[var(--text-tertiary)] transition-colors hover:text-[var(--text-primary)]'
|
||||
aria-label='Remove skill'
|
||||
>
|
||||
<XIcon className='h-[13px] w-[13px]' />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<SkillModal
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
isSubBlockVisibleForMode,
|
||||
} from '@/lib/workflows/subblocks/visibility'
|
||||
import type { BlockConfig, SubBlockConfig, SubBlockType } from '@/blocks/types'
|
||||
import { usePermissionConfig } from '@/hooks/use-permission-config'
|
||||
import { useWorkflowDiffStore } from '@/stores/workflow-diff'
|
||||
import { mergeSubblockState } from '@/stores/workflows/utils'
|
||||
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
|
||||
@@ -35,6 +36,7 @@ export function useEditorSubblockLayout(
|
||||
const blockDataFromStore = useWorkflowStore(
|
||||
useCallback((state) => state.blocks?.[blockId]?.data, [blockId])
|
||||
)
|
||||
const { config: permissionConfig } = usePermissionConfig()
|
||||
|
||||
return useMemo(() => {
|
||||
// Guard against missing config or block selection
|
||||
@@ -100,6 +102,9 @@ export function useEditorSubblockLayout(
|
||||
const visibleSubBlocks = (config.subBlocks || []).filter((block) => {
|
||||
if (block.hidden) return false
|
||||
|
||||
// Hide skill-input subblock when skills are disabled via permissions
|
||||
if (block.type === 'skill-input' && permissionConfig.disableSkills) return false
|
||||
|
||||
// Check required feature if specified - declarative feature gating
|
||||
if (!isSubBlockFeatureEnabled(block)) return false
|
||||
|
||||
@@ -149,5 +154,6 @@ export function useEditorSubblockLayout(
|
||||
activeWorkflowId,
|
||||
isSnapshotView,
|
||||
blockDataFromStore,
|
||||
permissionConfig.disableSkills,
|
||||
])
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import { useCustomTools } from '@/hooks/queries/custom-tools'
|
||||
import { useMcpServers, useMcpToolsQuery } from '@/hooks/queries/mcp'
|
||||
import { useCredentialName } from '@/hooks/queries/oauth-credentials'
|
||||
import { useReactivateSchedule, useScheduleInfo } from '@/hooks/queries/schedules'
|
||||
import { useSkills } from '@/hooks/queries/skills'
|
||||
import { useDeployChildWorkflow } from '@/hooks/queries/workflows'
|
||||
import { useSelectorDisplayName } from '@/hooks/use-selector-display-name'
|
||||
import { useVariablesStore } from '@/stores/panel'
|
||||
@@ -618,6 +619,48 @@ const SubBlockRow = memo(function SubBlockRow({
|
||||
return `${toolNames[0]}, ${toolNames[1]} +${toolNames.length - 2}`
|
||||
}, [subBlock?.type, rawValue, customTools, workspaceId])
|
||||
|
||||
/**
|
||||
* Hydrates skill references to display names.
|
||||
* Resolves skill IDs to their current names from the skills query.
|
||||
*/
|
||||
const { data: workspaceSkills = [] } = useSkills(workspaceId || '')
|
||||
|
||||
const skillsDisplayValue = useMemo(() => {
|
||||
if (subBlock?.type !== 'skill-input' || !Array.isArray(rawValue) || rawValue.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
interface StoredSkill {
|
||||
skillId: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
const skillNames = rawValue
|
||||
.map((skill: StoredSkill) => {
|
||||
if (!skill || typeof skill !== 'object') return null
|
||||
|
||||
// Priority 1: Resolve skill name from the skills query (fresh data)
|
||||
if (skill.skillId) {
|
||||
const foundSkill = workspaceSkills.find((s) => s.id === skill.skillId)
|
||||
if (foundSkill?.name) return foundSkill.name
|
||||
}
|
||||
|
||||
// Priority 2: Fall back to stored name (for deleted skills)
|
||||
if (skill.name && typeof skill.name === 'string') return skill.name
|
||||
|
||||
// Priority 3: Use skillId as last resort
|
||||
if (skill.skillId) return skill.skillId
|
||||
|
||||
return null
|
||||
})
|
||||
.filter((name): name is string => !!name)
|
||||
|
||||
if (skillNames.length === 0) return null
|
||||
if (skillNames.length === 1) return skillNames[0]
|
||||
if (skillNames.length === 2) return `${skillNames[0]}, ${skillNames[1]}`
|
||||
return `${skillNames[0]}, ${skillNames[1]} +${skillNames.length - 2}`
|
||||
}, [subBlock?.type, rawValue, workspaceSkills])
|
||||
|
||||
const isPasswordField = subBlock?.password === true
|
||||
const maskedValue = isPasswordField && value && value !== '-' ? '•••' : null
|
||||
|
||||
@@ -627,6 +670,7 @@ const SubBlockRow = memo(function SubBlockRow({
|
||||
dropdownLabel ||
|
||||
variablesDisplayValue ||
|
||||
toolsDisplayValue ||
|
||||
skillsDisplayValue ||
|
||||
knowledgeBaseDisplayName ||
|
||||
workflowSelectionName ||
|
||||
mcpServerDisplayName ||
|
||||
|
||||
@@ -27,6 +27,13 @@ interface SkillModalProps {
|
||||
|
||||
const KEBAB_CASE_REGEX = /^[a-z0-9]+(-[a-z0-9]+)*$/
|
||||
|
||||
interface FieldErrors {
|
||||
name?: string
|
||||
description?: string
|
||||
content?: string
|
||||
general?: string
|
||||
}
|
||||
|
||||
export function SkillModal({
|
||||
open,
|
||||
onOpenChange,
|
||||
@@ -43,7 +50,7 @@ export function SkillModal({
|
||||
const [name, setName] = useState('')
|
||||
const [description, setDescription] = useState('')
|
||||
const [content, setContent] = useState('')
|
||||
const [formError, setFormError] = useState('')
|
||||
const [errors, setErrors] = useState<FieldErrors>({})
|
||||
const [saving, setSaving] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -57,7 +64,7 @@ export function SkillModal({
|
||||
setDescription('')
|
||||
setContent('')
|
||||
}
|
||||
setFormError('')
|
||||
setErrors({})
|
||||
}
|
||||
}, [open, initialValues])
|
||||
|
||||
@@ -71,24 +78,26 @@ export function SkillModal({
|
||||
}, [name, description, content, initialValues])
|
||||
|
||||
const handleSave = async () => {
|
||||
const newErrors: FieldErrors = {}
|
||||
|
||||
if (!name.trim()) {
|
||||
setFormError('Name is required')
|
||||
return
|
||||
}
|
||||
if (name.length > 64) {
|
||||
setFormError('Name must be 64 characters or less')
|
||||
return
|
||||
}
|
||||
if (!KEBAB_CASE_REGEX.test(name)) {
|
||||
setFormError('Name must be kebab-case (e.g. my-skill)')
|
||||
return
|
||||
newErrors.name = 'Name is required'
|
||||
} else if (name.length > 64) {
|
||||
newErrors.name = 'Name must be 64 characters or less'
|
||||
} else if (!KEBAB_CASE_REGEX.test(name)) {
|
||||
newErrors.name = 'Name must be kebab-case (e.g. my-skill)'
|
||||
}
|
||||
|
||||
if (!description.trim()) {
|
||||
setFormError('Description is required')
|
||||
return
|
||||
newErrors.description = 'Description is required'
|
||||
}
|
||||
|
||||
if (!content.trim()) {
|
||||
setFormError('Content is required')
|
||||
newErrors.content = 'Content is required'
|
||||
}
|
||||
|
||||
if (Object.keys(newErrors).length > 0) {
|
||||
setErrors(newErrors)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -113,7 +122,7 @@ export function SkillModal({
|
||||
error instanceof Error && error.message.includes('already exists')
|
||||
? error.message
|
||||
: 'Failed to save skill. Please try again.'
|
||||
setFormError(message)
|
||||
setErrors({ general: message })
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
@@ -135,12 +144,17 @@ export function SkillModal({
|
||||
value={name}
|
||||
onChange={(e) => {
|
||||
setName(e.target.value)
|
||||
if (formError) setFormError('')
|
||||
if (errors.name || errors.general)
|
||||
setErrors((prev) => ({ ...prev, name: undefined, general: undefined }))
|
||||
}}
|
||||
/>
|
||||
<span className='text-[11px] text-[var(--text-muted)]'>
|
||||
Lowercase letters, numbers, and hyphens (e.g. my-skill)
|
||||
</span>
|
||||
{errors.name ? (
|
||||
<p className='text-[12px] text-[var(--text-error)]'>{errors.name}</p>
|
||||
) : (
|
||||
<span className='text-[11px] text-[var(--text-muted)]'>
|
||||
Lowercase letters, numbers, and hyphens (e.g. my-skill)
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='flex flex-col gap-[4px]'>
|
||||
@@ -153,10 +167,14 @@ export function SkillModal({
|
||||
value={description}
|
||||
onChange={(e) => {
|
||||
setDescription(e.target.value)
|
||||
if (formError) setFormError('')
|
||||
if (errors.description || errors.general)
|
||||
setErrors((prev) => ({ ...prev, description: undefined, general: undefined }))
|
||||
}}
|
||||
maxLength={1024}
|
||||
/>
|
||||
{errors.description && (
|
||||
<p className='text-[12px] text-[var(--text-error)]'>{errors.description}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className='flex flex-col gap-[4px]'>
|
||||
@@ -169,13 +187,19 @@ export function SkillModal({
|
||||
value={content}
|
||||
onChange={(e: ChangeEvent<HTMLTextAreaElement>) => {
|
||||
setContent(e.target.value)
|
||||
if (formError) setFormError('')
|
||||
if (errors.content || errors.general)
|
||||
setErrors((prev) => ({ ...prev, content: undefined, general: undefined }))
|
||||
}}
|
||||
className='min-h-[200px] resize-y font-mono text-[13px]'
|
||||
/>
|
||||
{errors.content && (
|
||||
<p className='text-[12px] text-[var(--text-error)]'>{errors.content}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{formError && <span className='text-[11px] text-[var(--text-error)]'>{formError}</span>}
|
||||
{errors.general && (
|
||||
<p className='text-[12px] text-[var(--text-error)]'>{errors.general}</p>
|
||||
)}
|
||||
</div>
|
||||
</ModalBody>
|
||||
<ModalFooter className='items-center justify-between'>
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { AgentIcon } from '@/components/icons'
|
||||
import { isHosted } from '@/lib/core/config/feature-flags'
|
||||
import type { BlockConfig } from '@/blocks/types'
|
||||
import { AuthMode } from '@/blocks/types'
|
||||
import { getApiKeyCondition } from '@/blocks/utils'
|
||||
import {
|
||||
getBaseModelProviders,
|
||||
getHostedModels,
|
||||
getMaxTemperature,
|
||||
getProviderIcon,
|
||||
getReasoningEffortValuesForModel,
|
||||
@@ -17,15 +16,6 @@ import {
|
||||
providers,
|
||||
supportsTemperature,
|
||||
} from '@/providers/utils'
|
||||
|
||||
const getCurrentOllamaModels = () => {
|
||||
return useProvidersStore.getState().providers.ollama.models
|
||||
}
|
||||
|
||||
const getCurrentVLLMModels = () => {
|
||||
return useProvidersStore.getState().providers.vllm.models
|
||||
}
|
||||
|
||||
import { useProvidersStore } from '@/stores/providers'
|
||||
import type { ToolResponse } from '@/tools/types'
|
||||
|
||||
@@ -333,11 +323,11 @@ Return ONLY the JSON array.`,
|
||||
id: 'azureApiVersion',
|
||||
title: 'Azure API Version',
|
||||
type: 'short-input',
|
||||
placeholder: '2024-07-01-preview',
|
||||
placeholder: 'Enter API version',
|
||||
connectionDroppable: false,
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: providers['azure-openai'].models,
|
||||
value: [...providers['azure-openai'].models, ...providers['azure-anthropic'].models],
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -421,23 +411,7 @@ Return ONLY the JSON array.`,
|
||||
password: true,
|
||||
connectionDroppable: false,
|
||||
required: true,
|
||||
// Hide API key for hosted models, Ollama models, vLLM models, Vertex models (uses OAuth), and Bedrock (uses AWS credentials)
|
||||
condition: isHosted
|
||||
? {
|
||||
field: 'model',
|
||||
value: [...getHostedModels(), ...providers.vertex.models, ...providers.bedrock.models],
|
||||
not: true, // Show for all models EXCEPT those listed
|
||||
}
|
||||
: () => ({
|
||||
field: 'model',
|
||||
value: [
|
||||
...getCurrentOllamaModels(),
|
||||
...getCurrentVLLMModels(),
|
||||
...providers.vertex.models,
|
||||
...providers.bedrock.models,
|
||||
],
|
||||
not: true, // Show for all models EXCEPT Ollama, vLLM, Vertex, and Bedrock models
|
||||
}),
|
||||
condition: getApiKeyCondition(),
|
||||
},
|
||||
{
|
||||
id: 'memoryType',
|
||||
@@ -715,7 +689,7 @@ Example 3 (Array Input):
|
||||
},
|
||||
model: { type: 'string', description: 'AI model to use' },
|
||||
apiKey: { type: 'string', description: 'Provider API key' },
|
||||
azureEndpoint: { type: 'string', description: 'Azure OpenAI endpoint URL' },
|
||||
azureEndpoint: { type: 'string', description: 'Azure endpoint URL' },
|
||||
azureApiVersion: { type: 'string', description: 'Azure API version' },
|
||||
vertexProject: { type: 'string', description: 'Google Cloud project ID for Vertex AI' },
|
||||
vertexLocation: { type: 'string', description: 'Google Cloud location for Vertex AI' },
|
||||
|
||||
@@ -76,8 +76,9 @@ export const TranslateBlock: BlockConfig = {
|
||||
vertexProject: params.vertexProject,
|
||||
vertexLocation: params.vertexLocation,
|
||||
vertexCredential: params.vertexCredential,
|
||||
bedrockRegion: params.bedrockRegion,
|
||||
bedrockAccessKeyId: params.bedrockAccessKeyId,
|
||||
bedrockSecretKey: params.bedrockSecretKey,
|
||||
bedrockRegion: params.bedrockRegion,
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -208,7 +208,7 @@ export interface SubBlockConfig {
|
||||
not?: boolean
|
||||
}
|
||||
}
|
||||
| (() => {
|
||||
| ((values?: Record<string, unknown>) => {
|
||||
field: string
|
||||
value: string | number | boolean | Array<string | number | boolean>
|
||||
not?: boolean
|
||||
@@ -261,7 +261,7 @@ export interface SubBlockConfig {
|
||||
not?: boolean
|
||||
}
|
||||
}
|
||||
| (() => {
|
||||
| ((values?: Record<string, unknown>) => {
|
||||
field: string
|
||||
value: string | number | boolean | Array<string | number | boolean>
|
||||
not?: boolean
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { isHosted } from '@/lib/core/config/feature-flags'
|
||||
import type { BlockOutput, OutputFieldDefinition, SubBlockConfig } from '@/blocks/types'
|
||||
import { getHostedModels, providers } from '@/providers/utils'
|
||||
import { getHostedModels, getProviderFromModel, providers } from '@/providers/utils'
|
||||
import { useProvidersStore } from '@/stores/providers/store'
|
||||
|
||||
/**
|
||||
@@ -48,11 +48,54 @@ const getCurrentOllamaModels = () => {
|
||||
return useProvidersStore.getState().providers.ollama.models
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to get current vLLM models from store
|
||||
*/
|
||||
const getCurrentVLLMModels = () => {
|
||||
return useProvidersStore.getState().providers.vllm.models
|
||||
function buildModelVisibilityCondition(model: string, shouldShow: boolean) {
|
||||
if (!model) {
|
||||
return { field: 'model', value: '__no_model_selected__' }
|
||||
}
|
||||
|
||||
return shouldShow ? { field: 'model', value: model } : { field: 'model', value: model, not: true }
|
||||
}
|
||||
|
||||
function shouldRequireApiKeyForModel(model: string): boolean {
|
||||
const normalizedModel = model.trim().toLowerCase()
|
||||
if (!normalizedModel) return false
|
||||
|
||||
const hostedModels = getHostedModels()
|
||||
const isHostedModel = hostedModels.some(
|
||||
(hostedModel) => hostedModel.toLowerCase() === normalizedModel
|
||||
)
|
||||
if (isHosted && isHostedModel) return false
|
||||
|
||||
if (normalizedModel.startsWith('vertex/') || normalizedModel.startsWith('bedrock/')) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (normalizedModel.startsWith('vllm/')) {
|
||||
return false
|
||||
}
|
||||
|
||||
const currentOllamaModels = getCurrentOllamaModels()
|
||||
if (currentOllamaModels.some((ollamaModel) => ollamaModel.toLowerCase() === normalizedModel)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!isHosted) {
|
||||
try {
|
||||
const providerId = getProviderFromModel(model)
|
||||
if (
|
||||
providerId === 'ollama' ||
|
||||
providerId === 'vllm' ||
|
||||
providerId === 'vertex' ||
|
||||
providerId === 'bedrock'
|
||||
) {
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
// If model resolution fails, fall through and require an API key.
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,27 +103,16 @@ const getCurrentVLLMModels = () => {
|
||||
* Handles hosted vs self-hosted environments and excludes providers that don't need API key.
|
||||
*/
|
||||
export function getApiKeyCondition() {
|
||||
return isHosted
|
||||
? {
|
||||
field: 'model',
|
||||
value: [...getHostedModels(), ...providers.vertex.models, ...providers.bedrock.models],
|
||||
not: true,
|
||||
}
|
||||
: () => ({
|
||||
field: 'model',
|
||||
value: [
|
||||
...getCurrentOllamaModels(),
|
||||
...getCurrentVLLMModels(),
|
||||
...providers.vertex.models,
|
||||
...providers.bedrock.models,
|
||||
],
|
||||
not: true,
|
||||
})
|
||||
return (values?: Record<string, unknown>) => {
|
||||
const model = typeof values?.model === 'string' ? values.model : ''
|
||||
const shouldShow = shouldRequireApiKeyForModel(model)
|
||||
return buildModelVisibilityCondition(model, shouldShow)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the standard provider credential subblocks used by LLM-based blocks.
|
||||
* This includes: Vertex AI OAuth, API Key, Azure OpenAI, Vertex AI config, and Bedrock config.
|
||||
* This includes: Vertex AI OAuth, API Key, Azure (OpenAI + Anthropic), Vertex AI config, and Bedrock config.
|
||||
*
|
||||
* Usage: Spread into your block's subBlocks array after block-specific fields
|
||||
*/
|
||||
@@ -111,25 +143,25 @@ export function getProviderCredentialSubBlocks(): SubBlockConfig[] {
|
||||
},
|
||||
{
|
||||
id: 'azureEndpoint',
|
||||
title: 'Azure OpenAI Endpoint',
|
||||
title: 'Azure Endpoint',
|
||||
type: 'short-input',
|
||||
password: true,
|
||||
placeholder: 'https://your-resource.openai.azure.com',
|
||||
placeholder: 'https://your-resource.services.ai.azure.com',
|
||||
connectionDroppable: false,
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: providers['azure-openai'].models,
|
||||
value: [...providers['azure-openai'].models, ...providers['azure-anthropic'].models],
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'azureApiVersion',
|
||||
title: 'Azure API Version',
|
||||
type: 'short-input',
|
||||
placeholder: '2024-07-01-preview',
|
||||
placeholder: 'Enter API version',
|
||||
connectionDroppable: false,
|
||||
condition: {
|
||||
field: 'model',
|
||||
value: providers['azure-openai'].models,
|
||||
value: [...providers['azure-openai'].models, ...providers['azure-anthropic'].models],
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -202,7 +234,7 @@ export function getProviderCredentialSubBlocks(): SubBlockConfig[] {
|
||||
*/
|
||||
export const PROVIDER_CREDENTIAL_INPUTS = {
|
||||
apiKey: { type: 'string', description: 'Provider API key' },
|
||||
azureEndpoint: { type: 'string', description: 'Azure OpenAI endpoint URL' },
|
||||
azureEndpoint: { type: 'string', description: 'Azure endpoint URL' },
|
||||
azureApiVersion: { type: 'string', description: 'Azure API version' },
|
||||
vertexProject: { type: 'string', description: 'Google Cloud project ID for Vertex AI' },
|
||||
vertexLocation: { type: 'string', description: 'Google Cloud location for Vertex AI' },
|
||||
|
||||
@@ -5468,18 +5468,18 @@ export function AgentSkillsIcon(props: SVGProps<SVGSVGElement>) {
|
||||
<svg
|
||||
{...props}
|
||||
xmlns='http://www.w3.org/2000/svg'
|
||||
width='24'
|
||||
height='24'
|
||||
viewBox='0 0 32 32'
|
||||
width='16'
|
||||
height='16'
|
||||
viewBox='0 0 16 16'
|
||||
fill='none'
|
||||
>
|
||||
<path d='M16 0.5L29.4234 8.25V23.75L16 31.5L2.57661 23.75V8.25L16 0.5Z' fill='currentColor' />
|
||||
<path
|
||||
d='M16 6L24.6603 11V21L16 26L7.33975 21V11L16 6Z'
|
||||
fill='currentColor'
|
||||
stroke='var(--background, white)'
|
||||
strokeWidth='3'
|
||||
d='M8 1L14.0622 4.5V11.5L8 15L1.93782 11.5V4.5L8 1Z'
|
||||
stroke='currentColor'
|
||||
strokeWidth='1.5'
|
||||
fill='none'
|
||||
/>
|
||||
<path d='M8 4.5L11 6.25V9.75L8 11.5L5 9.75V6.25L8 4.5Z' fill='currentColor' />
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -326,6 +326,7 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
_context: {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
},
|
||||
@@ -377,6 +378,9 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
if (ctx.workflowId) {
|
||||
params.workflowId = ctx.workflowId
|
||||
}
|
||||
if (ctx.userId) {
|
||||
params.userId = ctx.userId
|
||||
}
|
||||
|
||||
const url = buildAPIUrl('/api/tools/custom', params)
|
||||
const response = await fetch(url.toString(), {
|
||||
@@ -487,7 +491,9 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
usageControl: tool.usageControl || 'auto',
|
||||
executeFunction: async (callParams: Record<string, any>) => {
|
||||
const headers = await buildAuthHeaders()
|
||||
const execUrl = buildAPIUrl('/api/mcp/tools/execute')
|
||||
const execParams: Record<string, string> = {}
|
||||
if (ctx.userId) execParams.userId = ctx.userId
|
||||
const execUrl = buildAPIUrl('/api/mcp/tools/execute', execParams)
|
||||
|
||||
const execResponse = await fetch(execUrl.toString(), {
|
||||
method: 'POST',
|
||||
@@ -596,6 +602,7 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
serverId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
workflowId: ctx.workflowId,
|
||||
...(ctx.userId ? { userId: ctx.userId } : {}),
|
||||
})
|
||||
|
||||
const maxAttempts = 2
|
||||
@@ -670,7 +677,9 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
usageControl: tool.usageControl || 'auto',
|
||||
executeFunction: async (callParams: Record<string, any>) => {
|
||||
const headers = await buildAuthHeaders()
|
||||
const execUrl = buildAPIUrl('/api/mcp/tools/execute')
|
||||
const discoverExecParams: Record<string, string> = {}
|
||||
if (ctx.userId) discoverExecParams.userId = ctx.userId
|
||||
const execUrl = buildAPIUrl('/api/mcp/tools/execute', discoverExecParams)
|
||||
|
||||
const execResponse = await fetch(execUrl.toString(), {
|
||||
method: 'POST',
|
||||
@@ -1055,6 +1064,7 @@ export class AgentBlockHandler implements BlockHandler {
|
||||
responseFormat: providerRequest.responseFormat,
|
||||
workflowId: providerRequest.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
userId: ctx.userId,
|
||||
stream: providerRequest.stream,
|
||||
messages: 'messages' in providerRequest ? providerRequest.messages : undefined,
|
||||
environmentVariables: ctx.environmentVariables || {},
|
||||
|
||||
@@ -72,6 +72,7 @@ export class ApiBlockHandler implements BlockHandler {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
executionId: ctx.executionId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -48,6 +48,7 @@ export async function evaluateConditionExpression(
|
||||
_context: {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -104,7 +104,7 @@ export class EvaluatorBlockHandler implements BlockHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
const url = buildAPIUrl('/api/providers')
|
||||
const url = buildAPIUrl('/api/providers', ctx.userId ? { userId: ctx.userId } : {})
|
||||
|
||||
const providerRequest: Record<string, any> = {
|
||||
provider: providerId,
|
||||
@@ -121,26 +121,17 @@ export class EvaluatorBlockHandler implements BlockHandler {
|
||||
|
||||
temperature: EVALUATOR.DEFAULT_TEMPERATURE,
|
||||
apiKey: finalApiKey,
|
||||
azureEndpoint: inputs.azureEndpoint,
|
||||
azureApiVersion: inputs.azureApiVersion,
|
||||
vertexProject: evaluatorConfig.vertexProject,
|
||||
vertexLocation: evaluatorConfig.vertexLocation,
|
||||
bedrockAccessKeyId: evaluatorConfig.bedrockAccessKeyId,
|
||||
bedrockSecretKey: evaluatorConfig.bedrockSecretKey,
|
||||
bedrockRegion: evaluatorConfig.bedrockRegion,
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
}
|
||||
|
||||
if (providerId === 'vertex') {
|
||||
providerRequest.vertexProject = evaluatorConfig.vertexProject
|
||||
providerRequest.vertexLocation = evaluatorConfig.vertexLocation
|
||||
}
|
||||
|
||||
if (providerId === 'azure-openai') {
|
||||
providerRequest.azureEndpoint = inputs.azureEndpoint
|
||||
providerRequest.azureApiVersion = inputs.azureApiVersion
|
||||
}
|
||||
|
||||
if (providerId === 'bedrock') {
|
||||
providerRequest.bedrockAccessKeyId = evaluatorConfig.bedrockAccessKeyId
|
||||
providerRequest.bedrockSecretKey = evaluatorConfig.bedrockSecretKey
|
||||
providerRequest.bedrockRegion = evaluatorConfig.bedrockRegion
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: 'POST',
|
||||
headers: await buildAuthHeaders(),
|
||||
|
||||
@@ -39,6 +39,7 @@ export class FunctionBlockHandler implements BlockHandler {
|
||||
_context: {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -66,6 +66,7 @@ export class GenericBlockHandler implements BlockHandler {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
executionId: ctx.executionId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -605,6 +605,7 @@ export class HumanInTheLoopBlockHandler implements BlockHandler {
|
||||
_context: {
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
userId: ctx.userId,
|
||||
isDeployedContext: ctx.isDeployedContext,
|
||||
},
|
||||
blockData: blockDataWithPause,
|
||||
|
||||
@@ -80,6 +80,7 @@ export class RouterBlockHandler implements BlockHandler {
|
||||
|
||||
try {
|
||||
const url = new URL('/api/providers', getBaseUrl())
|
||||
if (ctx.userId) url.searchParams.set('userId', ctx.userId)
|
||||
|
||||
const messages = [{ role: 'user', content: routerConfig.prompt }]
|
||||
const systemPrompt = generateRouterPrompt(routerConfig.prompt, targetBlocks)
|
||||
@@ -96,26 +97,17 @@ export class RouterBlockHandler implements BlockHandler {
|
||||
context: JSON.stringify(messages),
|
||||
temperature: ROUTER.INFERENCE_TEMPERATURE,
|
||||
apiKey: finalApiKey,
|
||||
azureEndpoint: inputs.azureEndpoint,
|
||||
azureApiVersion: inputs.azureApiVersion,
|
||||
vertexProject: routerConfig.vertexProject,
|
||||
vertexLocation: routerConfig.vertexLocation,
|
||||
bedrockAccessKeyId: routerConfig.bedrockAccessKeyId,
|
||||
bedrockSecretKey: routerConfig.bedrockSecretKey,
|
||||
bedrockRegion: routerConfig.bedrockRegion,
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
}
|
||||
|
||||
if (providerId === 'vertex') {
|
||||
providerRequest.vertexProject = routerConfig.vertexProject
|
||||
providerRequest.vertexLocation = routerConfig.vertexLocation
|
||||
}
|
||||
|
||||
if (providerId === 'azure-openai') {
|
||||
providerRequest.azureEndpoint = inputs.azureEndpoint
|
||||
providerRequest.azureApiVersion = inputs.azureApiVersion
|
||||
}
|
||||
|
||||
if (providerId === 'bedrock') {
|
||||
providerRequest.bedrockAccessKeyId = routerConfig.bedrockAccessKeyId
|
||||
providerRequest.bedrockSecretKey = routerConfig.bedrockSecretKey
|
||||
providerRequest.bedrockRegion = routerConfig.bedrockRegion
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: 'POST',
|
||||
headers: await buildAuthHeaders(),
|
||||
@@ -218,6 +210,7 @@ export class RouterBlockHandler implements BlockHandler {
|
||||
|
||||
try {
|
||||
const url = new URL('/api/providers', getBaseUrl())
|
||||
if (ctx.userId) url.searchParams.set('userId', ctx.userId)
|
||||
|
||||
const messages = [{ role: 'user', content: routerConfig.context }]
|
||||
const systemPrompt = generateRouterV2Prompt(routerConfig.context, routes)
|
||||
@@ -234,6 +227,13 @@ export class RouterBlockHandler implements BlockHandler {
|
||||
context: JSON.stringify(messages),
|
||||
temperature: ROUTER.INFERENCE_TEMPERATURE,
|
||||
apiKey: finalApiKey,
|
||||
azureEndpoint: inputs.azureEndpoint,
|
||||
azureApiVersion: inputs.azureApiVersion,
|
||||
vertexProject: routerConfig.vertexProject,
|
||||
vertexLocation: routerConfig.vertexLocation,
|
||||
bedrockAccessKeyId: routerConfig.bedrockAccessKeyId,
|
||||
bedrockSecretKey: routerConfig.bedrockSecretKey,
|
||||
bedrockRegion: routerConfig.bedrockRegion,
|
||||
workflowId: ctx.workflowId,
|
||||
workspaceId: ctx.workspaceId,
|
||||
responseFormat: {
|
||||
@@ -257,22 +257,6 @@ export class RouterBlockHandler implements BlockHandler {
|
||||
},
|
||||
}
|
||||
|
||||
if (providerId === 'vertex') {
|
||||
providerRequest.vertexProject = routerConfig.vertexProject
|
||||
providerRequest.vertexLocation = routerConfig.vertexLocation
|
||||
}
|
||||
|
||||
if (providerId === 'azure-openai') {
|
||||
providerRequest.azureEndpoint = inputs.azureEndpoint
|
||||
providerRequest.azureApiVersion = inputs.azureApiVersion
|
||||
}
|
||||
|
||||
if (providerId === 'bedrock') {
|
||||
providerRequest.bedrockAccessKeyId = routerConfig.bedrockAccessKeyId
|
||||
providerRequest.bedrockSecretKey = routerConfig.bedrockSecretKey
|
||||
providerRequest.bedrockRegion = routerConfig.bedrockRegion
|
||||
}
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: 'POST',
|
||||
headers: await buildAuthHeaders(),
|
||||
|
||||
@@ -511,6 +511,8 @@ export class LoopOrchestrator {
|
||||
contextVariables: {},
|
||||
timeoutMs: LOOP_CONDITION_TIMEOUT_MS,
|
||||
requestId,
|
||||
ownerKey: `user:${ctx.userId}`,
|
||||
ownerWeight: 1,
|
||||
})
|
||||
|
||||
if (vmResult.error) {
|
||||
|
||||
@@ -2,13 +2,13 @@ import { db } from '@sim/db'
|
||||
import { account, workflow as workflowTable } from '@sim/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
|
||||
export interface CredentialAccessResult {
|
||||
ok: boolean
|
||||
error?: string
|
||||
authType?: 'session' | 'api_key' | 'internal_jwt'
|
||||
authType?: 'session' | 'internal_jwt'
|
||||
requesterUserId?: string
|
||||
credentialOwnerUserId?: string
|
||||
workspaceId?: string
|
||||
@@ -16,10 +16,10 @@ export interface CredentialAccessResult {
|
||||
|
||||
/**
|
||||
* Centralizes auth + collaboration rules for credential use.
|
||||
* - Uses checkHybridAuth to authenticate the caller
|
||||
* - Uses checkSessionOrInternalAuth to authenticate the caller
|
||||
* - Fetches credential owner
|
||||
* - Authorization rules:
|
||||
* - session/api_key: allow if requester owns the credential; otherwise require workflowId and
|
||||
* - session: allow if requester owns the credential; otherwise require workflowId and
|
||||
* verify BOTH requester and owner have access to the workflow's workspace
|
||||
* - internal_jwt: require workflowId (by default) and verify credential owner has access to the
|
||||
* workflow's workspace (requester identity is the system/workflow)
|
||||
@@ -30,7 +30,9 @@ export async function authorizeCredentialUse(
|
||||
): Promise<CredentialAccessResult> {
|
||||
const { credentialId, workflowId, requireWorkflowIdForInternal = true } = params
|
||||
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: requireWorkflowIdForInternal })
|
||||
const auth = await checkSessionOrInternalAuth(request, {
|
||||
requireWorkflowId: requireWorkflowIdForInternal,
|
||||
})
|
||||
if (!auth.success || !auth.userId) {
|
||||
return { ok: false, error: auth.error || 'Authentication required' }
|
||||
}
|
||||
@@ -52,7 +54,7 @@ export async function authorizeCredentialUse(
|
||||
if (auth.authType !== 'internal_jwt' && auth.userId === credentialOwnerUserId) {
|
||||
return {
|
||||
ok: true,
|
||||
authType: auth.authType,
|
||||
authType: auth.authType as CredentialAccessResult['authType'],
|
||||
requesterUserId: auth.userId,
|
||||
credentialOwnerUserId,
|
||||
}
|
||||
@@ -85,14 +87,14 @@ export async function authorizeCredentialUse(
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
authType: auth.authType,
|
||||
authType: auth.authType as CredentialAccessResult['authType'],
|
||||
requesterUserId: auth.userId,
|
||||
credentialOwnerUserId,
|
||||
workspaceId: wf.workspaceId,
|
||||
}
|
||||
}
|
||||
|
||||
// Session/API key: verify BOTH requester and owner belong to the workflow's workspace
|
||||
// Session: verify BOTH requester and owner belong to the workflow's workspace
|
||||
const requesterPerm = await getUserEntityPermissions(auth.userId, 'workspace', wf.workspaceId)
|
||||
const ownerPerm = await getUserEntityPermissions(
|
||||
credentialOwnerUserId,
|
||||
@@ -105,7 +107,7 @@ export async function authorizeCredentialUse(
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
authType: auth.authType,
|
||||
authType: auth.authType as CredentialAccessResult['authType'],
|
||||
requesterUserId: auth.userId,
|
||||
credentialOwnerUserId,
|
||||
workspaceId: wf.workspaceId,
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { db } from '@sim/db'
|
||||
import { workflow } from '@sim/db/schema'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { authenticateApiKeyFromHeader, updateApiKeyLastUsed } from '@/lib/api-key/service'
|
||||
import { getSession } from '@/lib/auth'
|
||||
@@ -13,35 +10,33 @@ export interface AuthResult {
|
||||
success: boolean
|
||||
userId?: string
|
||||
authType?: 'session' | 'api_key' | 'internal_jwt'
|
||||
apiKeyType?: 'personal' | 'workspace'
|
||||
error?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves userId from a verified internal JWT token.
|
||||
* Extracts workflowId/userId from URL params or POST body, then looks up userId if needed.
|
||||
* Extracts userId from the JWT payload, URL search params, or POST body.
|
||||
*/
|
||||
async function resolveUserFromJwt(
|
||||
request: NextRequest,
|
||||
verificationUserId: string | null,
|
||||
options: { requireWorkflowId?: boolean }
|
||||
): Promise<AuthResult> {
|
||||
let workflowId: string | null = null
|
||||
let userId: string | null = verificationUserId
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
workflowId = searchParams.get('workflowId')
|
||||
if (!userId) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
userId = searchParams.get('userId')
|
||||
}
|
||||
|
||||
if (!workflowId && !userId && request.method === 'POST') {
|
||||
if (!userId && request.method === 'POST') {
|
||||
try {
|
||||
const clonedRequest = request.clone()
|
||||
const bodyText = await clonedRequest.text()
|
||||
if (bodyText) {
|
||||
const body = JSON.parse(bodyText)
|
||||
workflowId = body.workflowId || body._context?.workflowId
|
||||
userId = userId || body.userId || body._context?.userId
|
||||
userId = body.userId || body._context?.userId || null
|
||||
}
|
||||
} catch {
|
||||
// Ignore JSON parse errors
|
||||
@@ -52,22 +47,8 @@ async function resolveUserFromJwt(
|
||||
return { success: true, userId, authType: 'internal_jwt' }
|
||||
}
|
||||
|
||||
if (workflowId) {
|
||||
const [workflowData] = await db
|
||||
.select({ userId: workflow.userId })
|
||||
.from(workflow)
|
||||
.where(eq(workflow.id, workflowId))
|
||||
.limit(1)
|
||||
|
||||
if (!workflowData) {
|
||||
return { success: false, error: 'Workflow not found' }
|
||||
}
|
||||
|
||||
return { success: true, userId: workflowData.userId, authType: 'internal_jwt' }
|
||||
}
|
||||
|
||||
if (options.requireWorkflowId !== false) {
|
||||
return { success: false, error: 'workflowId or userId required for internal JWT calls' }
|
||||
return { success: false, error: 'userId required for internal JWT calls' }
|
||||
}
|
||||
|
||||
return { success: true, authType: 'internal_jwt' }
|
||||
@@ -222,6 +203,7 @@ export async function checkHybridAuth(
|
||||
success: true,
|
||||
userId: result.userId!,
|
||||
authType: 'api_key',
|
||||
apiKeyType: result.keyType,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ const VALID_PROVIDER_IDS: readonly ProviderId[] = [
|
||||
'openai',
|
||||
'azure-openai',
|
||||
'anthropic',
|
||||
'azure-anthropic',
|
||||
'google',
|
||||
'deepseek',
|
||||
'xai',
|
||||
|
||||
@@ -147,6 +147,13 @@ export type CopilotProviderConfig =
|
||||
apiVersion?: string
|
||||
endpoint?: string
|
||||
}
|
||||
| {
|
||||
provider: 'azure-anthropic'
|
||||
model: string
|
||||
apiKey?: string
|
||||
apiVersion?: string
|
||||
endpoint?: string
|
||||
}
|
||||
| {
|
||||
provider: 'vertex'
|
||||
model: string
|
||||
@@ -155,7 +162,7 @@ export type CopilotProviderConfig =
|
||||
vertexLocation?: string
|
||||
}
|
||||
| {
|
||||
provider: Exclude<ProviderId, 'azure-openai' | 'vertex'>
|
||||
provider: Exclude<ProviderId, 'azure-openai' | 'azure-anthropic' | 'vertex'>
|
||||
model?: string
|
||||
apiKey?: string
|
||||
}
|
||||
|
||||
@@ -95,6 +95,9 @@ export const env = createEnv({
|
||||
AZURE_OPENAI_ENDPOINT: z.string().url().optional(), // Shared Azure OpenAI service endpoint
|
||||
AZURE_OPENAI_API_VERSION: z.string().optional(), // Shared Azure OpenAI API version
|
||||
AZURE_OPENAI_API_KEY: z.string().min(1).optional(), // Shared Azure OpenAI API key
|
||||
AZURE_ANTHROPIC_ENDPOINT: z.string().url().optional(), // Azure Anthropic service endpoint
|
||||
AZURE_ANTHROPIC_API_KEY: z.string().min(1).optional(), // Azure Anthropic API key
|
||||
AZURE_ANTHROPIC_API_VERSION: z.string().min(1).optional(), // Azure Anthropic API version (e.g. 2023-06-01)
|
||||
KB_OPENAI_MODEL_NAME: z.string().optional(), // Knowledge base OpenAI model name (works with both regular OpenAI and Azure OpenAI)
|
||||
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
|
||||
@@ -180,6 +183,24 @@ export const env = createEnv({
|
||||
EXECUTION_TIMEOUT_ASYNC_TEAM: z.string().optional().default('5400'), // 90 minutes
|
||||
EXECUTION_TIMEOUT_ASYNC_ENTERPRISE: z.string().optional().default('5400'), // 90 minutes
|
||||
|
||||
// Isolated-VM Worker Pool Configuration
|
||||
IVM_POOL_SIZE: z.string().optional().default('4'), // Max worker processes in pool
|
||||
IVM_MAX_CONCURRENT: z.string().optional().default('10000'), // Max concurrent executions globally
|
||||
IVM_MAX_PER_WORKER: z.string().optional().default('2500'), // Max concurrent executions per worker
|
||||
IVM_WORKER_IDLE_TIMEOUT_MS: z.string().optional().default('60000'), // Worker idle cleanup timeout (ms)
|
||||
IVM_MAX_QUEUE_SIZE: z.string().optional().default('10000'), // Max pending queued executions in memory
|
||||
IVM_MAX_FETCH_RESPONSE_BYTES: z.string().optional().default('8388608'),// Max bytes read from sandbox fetch responses
|
||||
IVM_MAX_FETCH_RESPONSE_CHARS: z.string().optional().default('4000000'),// Max chars returned to sandbox from fetch body
|
||||
IVM_MAX_FETCH_OPTIONS_JSON_CHARS: z.string().optional().default('262144'), // Max JSON payload size for sandbox fetch options
|
||||
IVM_MAX_FETCH_URL_LENGTH: z.string().optional().default('8192'), // Max URL length accepted by sandbox fetch
|
||||
IVM_MAX_STDOUT_CHARS: z.string().optional().default('200000'), // Max captured stdout characters per execution
|
||||
IVM_MAX_ACTIVE_PER_OWNER: z.string().optional().default('200'), // Max active executions per owner (per process)
|
||||
IVM_MAX_QUEUED_PER_OWNER: z.string().optional().default('2000'), // Max queued executions per owner (per process)
|
||||
IVM_MAX_OWNER_WEIGHT: z.string().optional().default('5'), // Max accepted weight for weighted owner scheduling
|
||||
IVM_DISTRIBUTED_MAX_INFLIGHT_PER_OWNER:z.string().optional().default('2200'), // Max owner in-flight leases across replicas
|
||||
IVM_DISTRIBUTED_LEASE_MIN_TTL_MS: z.string().optional().default('120000'), // Min TTL for distributed in-flight leases (ms)
|
||||
IVM_QUEUE_TIMEOUT_MS: z.string().optional().default('300000'), // Max queue wait before rejection (ms)
|
||||
|
||||
// Knowledge Base Processing Configuration - Shared across all processing methods
|
||||
KB_CONFIG_MAX_DURATION: z.number().optional().default(600), // Max processing duration in seconds (10 minutes)
|
||||
KB_CONFIG_MAX_ATTEMPTS: z.number().optional().default(3), // Max retry attempts
|
||||
|
||||
@@ -103,6 +103,7 @@ export interface SecureFetchOptions {
|
||||
body?: string | Buffer | Uint8Array
|
||||
timeout?: number
|
||||
maxRedirects?: number
|
||||
maxResponseBytes?: number
|
||||
}
|
||||
|
||||
export class SecureFetchHeaders {
|
||||
@@ -165,6 +166,7 @@ export async function secureFetchWithPinnedIP(
|
||||
redirectCount = 0
|
||||
): Promise<SecureFetchResponse> {
|
||||
const maxRedirects = options.maxRedirects ?? DEFAULT_MAX_REDIRECTS
|
||||
const maxResponseBytes = options.maxResponseBytes
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const parsed = new URL(url)
|
||||
@@ -237,14 +239,32 @@ export async function secureFetchWithPinnedIP(
|
||||
}
|
||||
|
||||
const chunks: Buffer[] = []
|
||||
let totalBytes = 0
|
||||
let responseTerminated = false
|
||||
|
||||
res.on('data', (chunk: Buffer) => chunks.push(chunk))
|
||||
res.on('data', (chunk: Buffer) => {
|
||||
if (responseTerminated) return
|
||||
|
||||
totalBytes += chunk.length
|
||||
if (
|
||||
typeof maxResponseBytes === 'number' &&
|
||||
maxResponseBytes > 0 &&
|
||||
totalBytes > maxResponseBytes
|
||||
) {
|
||||
responseTerminated = true
|
||||
res.destroy(new Error(`Response exceeded maximum size of ${maxResponseBytes} bytes`))
|
||||
return
|
||||
}
|
||||
|
||||
chunks.push(chunk)
|
||||
})
|
||||
|
||||
res.on('error', (error) => {
|
||||
reject(error)
|
||||
})
|
||||
|
||||
res.on('end', () => {
|
||||
if (responseTerminated) return
|
||||
const bodyBuffer = Buffer.concat(chunks)
|
||||
const body = bodyBuffer.toString('utf-8')
|
||||
const headersRecord: Record<string, string> = {}
|
||||
|
||||
@@ -9,6 +9,21 @@ const USER_CODE_START_LINE = 4
|
||||
const pendingFetches = new Map()
|
||||
let fetchIdCounter = 0
|
||||
const FETCH_TIMEOUT_MS = 300000 // 5 minutes
|
||||
const MAX_STDOUT_CHARS = Number.parseInt(process.env.IVM_MAX_STDOUT_CHARS || '', 10) || 200000
|
||||
const MAX_FETCH_OPTIONS_JSON_CHARS =
|
||||
Number.parseInt(process.env.IVM_MAX_FETCH_OPTIONS_JSON_CHARS || '', 10) || 256 * 1024
|
||||
|
||||
function stringifyLogValue(value) {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return String(value)
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.stringify(value)
|
||||
} catch {
|
||||
return '[unserializable]'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract line and column from error stack or message
|
||||
@@ -101,8 +116,32 @@ function convertToCompatibleError(errorInfo, userCode) {
|
||||
async function executeCode(request) {
|
||||
const { code, params, envVars, contextVariables, timeoutMs, requestId } = request
|
||||
const stdoutChunks = []
|
||||
let stdoutLength = 0
|
||||
let stdoutTruncated = false
|
||||
let isolate = null
|
||||
|
||||
const appendStdout = (line) => {
|
||||
if (stdoutTruncated || !line) return
|
||||
|
||||
const remaining = MAX_STDOUT_CHARS - stdoutLength
|
||||
if (remaining <= 0) {
|
||||
stdoutTruncated = true
|
||||
stdoutChunks.push('[stdout truncated]\n')
|
||||
return
|
||||
}
|
||||
|
||||
if (line.length <= remaining) {
|
||||
stdoutChunks.push(line)
|
||||
stdoutLength += line.length
|
||||
return
|
||||
}
|
||||
|
||||
stdoutChunks.push(line.slice(0, remaining))
|
||||
stdoutChunks.push('\n[stdout truncated]\n')
|
||||
stdoutLength = MAX_STDOUT_CHARS
|
||||
stdoutTruncated = true
|
||||
}
|
||||
|
||||
try {
|
||||
isolate = new ivm.Isolate({ memoryLimit: 128 })
|
||||
const context = await isolate.createContext()
|
||||
@@ -111,18 +150,14 @@ async function executeCode(request) {
|
||||
await jail.set('global', jail.derefInto())
|
||||
|
||||
const logCallback = new ivm.Callback((...args) => {
|
||||
const message = args
|
||||
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
||||
.join(' ')
|
||||
stdoutChunks.push(`${message}\n`)
|
||||
const message = args.map((arg) => stringifyLogValue(arg)).join(' ')
|
||||
appendStdout(`${message}\n`)
|
||||
})
|
||||
await jail.set('__log', logCallback)
|
||||
|
||||
const errorCallback = new ivm.Callback((...args) => {
|
||||
const message = args
|
||||
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
||||
.join(' ')
|
||||
stdoutChunks.push(`ERROR: ${message}\n`)
|
||||
const message = args.map((arg) => stringifyLogValue(arg)).join(' ')
|
||||
appendStdout(`ERROR: ${message}\n`)
|
||||
})
|
||||
await jail.set('__error', errorCallback)
|
||||
|
||||
@@ -178,6 +213,9 @@ async function executeCode(request) {
|
||||
} catch {
|
||||
throw new Error('fetch options must be JSON-serializable');
|
||||
}
|
||||
if (optionsJson.length > ${MAX_FETCH_OPTIONS_JSON_CHARS}) {
|
||||
throw new Error('fetch options exceed maximum payload size');
|
||||
}
|
||||
}
|
||||
const resultJson = await __fetchRef.apply(undefined, [url, optionsJson], { result: { promise: true } });
|
||||
let result;
|
||||
|
||||
500
apps/sim/lib/execution/isolated-vm.test.ts
Normal file
500
apps/sim/lib/execution/isolated-vm.test.ts
Normal file
@@ -0,0 +1,500 @@
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
type MockProc = EventEmitter & {
|
||||
connected: boolean
|
||||
stderr: EventEmitter
|
||||
send: (message: unknown) => boolean
|
||||
kill: () => boolean
|
||||
}
|
||||
|
||||
type SpawnFactory = () => MockProc
|
||||
type RedisEval = (...args: any[]) => unknown | Promise<unknown>
|
||||
type SecureFetchImpl = (...args: any[]) => unknown | Promise<unknown>
|
||||
|
||||
function createBaseProc(): MockProc {
|
||||
const proc = new EventEmitter() as MockProc
|
||||
proc.connected = true
|
||||
proc.stderr = new EventEmitter()
|
||||
proc.send = () => true
|
||||
proc.kill = () => {
|
||||
if (!proc.connected) return true
|
||||
proc.connected = false
|
||||
setImmediate(() => proc.emit('exit', 0))
|
||||
return true
|
||||
}
|
||||
return proc
|
||||
}
|
||||
|
||||
function createStartupFailureProc(): MockProc {
|
||||
const proc = createBaseProc()
|
||||
setImmediate(() => {
|
||||
proc.connected = false
|
||||
proc.emit('exit', 1)
|
||||
})
|
||||
return proc
|
||||
}
|
||||
|
||||
function createReadyProc(result: unknown): MockProc {
|
||||
const proc = createBaseProc()
|
||||
proc.send = (message: unknown) => {
|
||||
const msg = message as { type?: string; executionId?: number }
|
||||
if (msg.type === 'execute') {
|
||||
setImmediate(() => {
|
||||
proc.emit('message', {
|
||||
type: 'result',
|
||||
executionId: msg.executionId,
|
||||
result: { result, stdout: '' },
|
||||
})
|
||||
})
|
||||
}
|
||||
return true
|
||||
}
|
||||
setImmediate(() => proc.emit('message', { type: 'ready' }))
|
||||
return proc
|
||||
}
|
||||
|
||||
function createReadyProcWithDelay(delayMs: number): MockProc {
|
||||
const proc = createBaseProc()
|
||||
proc.send = (message: unknown) => {
|
||||
const msg = message as { type?: string; executionId?: number; request?: { requestId?: string } }
|
||||
if (msg.type === 'execute') {
|
||||
setTimeout(() => {
|
||||
proc.emit('message', {
|
||||
type: 'result',
|
||||
executionId: msg.executionId,
|
||||
result: { result: msg.request?.requestId ?? 'unknown', stdout: '' },
|
||||
})
|
||||
}, delayMs)
|
||||
}
|
||||
return true
|
||||
}
|
||||
setImmediate(() => proc.emit('message', { type: 'ready' }))
|
||||
return proc
|
||||
}
|
||||
|
||||
function createReadyFetchProxyProc(fetchMessage: { url: string; optionsJson?: string }): MockProc {
|
||||
const proc = createBaseProc()
|
||||
let currentExecutionId = 0
|
||||
|
||||
proc.send = (message: unknown) => {
|
||||
const msg = message as { type?: string; executionId?: number; request?: { requestId?: string } }
|
||||
|
||||
if (msg.type === 'execute') {
|
||||
currentExecutionId = msg.executionId ?? 0
|
||||
setImmediate(() => {
|
||||
proc.emit('message', {
|
||||
type: 'fetch',
|
||||
fetchId: 1,
|
||||
requestId: msg.request?.requestId ?? 'fetch-test',
|
||||
url: fetchMessage.url,
|
||||
optionsJson: fetchMessage.optionsJson,
|
||||
})
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
if (msg.type === 'fetchResponse') {
|
||||
const fetchResponse = message as { response?: string }
|
||||
setImmediate(() => {
|
||||
proc.emit('message', {
|
||||
type: 'result',
|
||||
executionId: currentExecutionId,
|
||||
result: { result: fetchResponse.response ?? '', stdout: '' },
|
||||
})
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
setImmediate(() => proc.emit('message', { type: 'ready' }))
|
||||
return proc
|
||||
}
|
||||
|
||||
async function loadExecutionModule(options: {
|
||||
envOverrides?: Record<string, string>
|
||||
spawns: SpawnFactory[]
|
||||
redisEvalImpl?: RedisEval
|
||||
secureFetchImpl?: SecureFetchImpl
|
||||
}) {
|
||||
vi.resetModules()
|
||||
|
||||
const spawnQueue = [...options.spawns]
|
||||
const spawnMock = vi.fn(() => {
|
||||
const next = spawnQueue.shift()
|
||||
if (!next) {
|
||||
throw new Error('No mock spawn factory configured')
|
||||
}
|
||||
return next() as any
|
||||
})
|
||||
|
||||
vi.doMock('@sim/logger', () => ({
|
||||
createLogger: () => ({
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
const secureFetchMock = vi.fn(
|
||||
options.secureFetchImpl ??
|
||||
(async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: new Map<string, string>(),
|
||||
text: async () => '',
|
||||
json: async () => ({}),
|
||||
arrayBuffer: async () => new ArrayBuffer(0),
|
||||
}))
|
||||
)
|
||||
vi.doMock('@/lib/core/security/input-validation.server', () => ({
|
||||
secureFetchWithValidation: secureFetchMock,
|
||||
}))
|
||||
|
||||
vi.doMock('@/lib/core/config/env', () => ({
|
||||
env: {
|
||||
IVM_POOL_SIZE: '1',
|
||||
IVM_MAX_CONCURRENT: '100',
|
||||
IVM_MAX_PER_WORKER: '100',
|
||||
IVM_WORKER_IDLE_TIMEOUT_MS: '60000',
|
||||
IVM_MAX_QUEUE_SIZE: '10',
|
||||
IVM_MAX_ACTIVE_PER_OWNER: '100',
|
||||
IVM_MAX_QUEUED_PER_OWNER: '10',
|
||||
IVM_MAX_OWNER_WEIGHT: '5',
|
||||
IVM_DISTRIBUTED_MAX_INFLIGHT_PER_OWNER: '100',
|
||||
IVM_DISTRIBUTED_LEASE_MIN_TTL_MS: '1000',
|
||||
IVM_QUEUE_TIMEOUT_MS: '1000',
|
||||
...(options.envOverrides ?? {}),
|
||||
},
|
||||
}))
|
||||
|
||||
const redisEval = options.redisEvalImpl ? vi.fn(options.redisEvalImpl) : undefined
|
||||
vi.doMock('@/lib/core/config/redis', () => ({
|
||||
getRedisClient: vi.fn(() =>
|
||||
redisEval
|
||||
? ({
|
||||
eval: redisEval,
|
||||
} as any)
|
||||
: null
|
||||
),
|
||||
}))
|
||||
|
||||
vi.doMock('node:child_process', () => ({
|
||||
execSync: vi.fn(() => Buffer.from('v23.11.0')),
|
||||
spawn: spawnMock,
|
||||
}))
|
||||
|
||||
const mod = await import('./isolated-vm')
|
||||
return { ...mod, spawnMock, secureFetchMock }
|
||||
}
|
||||
|
||||
describe('isolated-vm scheduler', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.resetModules()
|
||||
})
|
||||
|
||||
it('recovers from an initial spawn failure and drains queued work', async () => {
|
||||
const { executeInIsolatedVM, spawnMock } = await loadExecutionModule({
|
||||
spawns: [createStartupFailureProc, () => createReadyProc('ok')],
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "ok"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-1',
|
||||
})
|
||||
|
||||
expect(result.error).toBeUndefined()
|
||||
expect(result.result).toBe('ok')
|
||||
expect(spawnMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('rejects new requests when the queue is full', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_MAX_QUEUE_SIZE: '1',
|
||||
IVM_QUEUE_TIMEOUT_MS: '200',
|
||||
},
|
||||
spawns: [createStartupFailureProc, createStartupFailureProc, createStartupFailureProc],
|
||||
})
|
||||
|
||||
const firstPromise = executeInIsolatedVM({
|
||||
code: 'return 1',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-2',
|
||||
ownerKey: 'user:a',
|
||||
})
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 25))
|
||||
|
||||
const second = await executeInIsolatedVM({
|
||||
code: 'return 2',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-3',
|
||||
ownerKey: 'user:b',
|
||||
})
|
||||
|
||||
expect(second.error?.message).toContain('at capacity')
|
||||
|
||||
const first = await firstPromise
|
||||
expect(first.error?.message).toContain('timed out waiting')
|
||||
})
|
||||
|
||||
it('enforces per-owner queued limit', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_MAX_QUEUED_PER_OWNER: '1',
|
||||
IVM_QUEUE_TIMEOUT_MS: '200',
|
||||
},
|
||||
spawns: [createStartupFailureProc, createStartupFailureProc, createStartupFailureProc],
|
||||
})
|
||||
|
||||
const firstPromise = executeInIsolatedVM({
|
||||
code: 'return 1',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-4',
|
||||
ownerKey: 'user:hog',
|
||||
})
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 25))
|
||||
|
||||
const second = await executeInIsolatedVM({
|
||||
code: 'return 2',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-5',
|
||||
ownerKey: 'user:hog',
|
||||
})
|
||||
|
||||
expect(second.error?.message).toContain('Too many concurrent')
|
||||
|
||||
const first = await firstPromise
|
||||
expect(first.error?.message).toContain('timed out waiting')
|
||||
})
|
||||
|
||||
it('enforces distributed owner in-flight lease limit when Redis is configured', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_DISTRIBUTED_MAX_INFLIGHT_PER_OWNER: '1',
|
||||
REDIS_URL: 'redis://localhost:6379',
|
||||
},
|
||||
spawns: [() => createReadyProc('ok')],
|
||||
redisEvalImpl: (...args: any[]) => {
|
||||
const script = String(args[0] ?? '')
|
||||
if (script.includes('ZREMRANGEBYSCORE')) {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
},
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "blocked"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-6',
|
||||
ownerKey: 'user:distributed',
|
||||
})
|
||||
|
||||
expect(result.error?.message).toContain('Too many concurrent')
|
||||
})
|
||||
|
||||
it('fails closed when Redis is configured but unavailable', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
REDIS_URL: 'redis://localhost:6379',
|
||||
},
|
||||
spawns: [() => createReadyProc('ok')],
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "blocked"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-7',
|
||||
ownerKey: 'user:redis-down',
|
||||
})
|
||||
|
||||
expect(result.error?.message).toContain('temporarily unavailable')
|
||||
})
|
||||
|
||||
it('fails closed when Redis lease evaluation errors', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
REDIS_URL: 'redis://localhost:6379',
|
||||
},
|
||||
spawns: [() => createReadyProc('ok')],
|
||||
redisEvalImpl: (...args: any[]) => {
|
||||
const script = String(args[0] ?? '')
|
||||
if (script.includes('ZREMRANGEBYSCORE')) {
|
||||
throw new Error('redis timeout')
|
||||
}
|
||||
return 1
|
||||
},
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "blocked"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-8',
|
||||
ownerKey: 'user:redis-error',
|
||||
})
|
||||
|
||||
expect(result.error?.message).toContain('temporarily unavailable')
|
||||
})
|
||||
|
||||
it('applies weighted owner scheduling when draining queued executions', async () => {
|
||||
const { executeInIsolatedVM } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_MAX_PER_WORKER: '1',
|
||||
},
|
||||
spawns: [() => createReadyProcWithDelay(10)],
|
||||
})
|
||||
|
||||
const completionOrder: string[] = []
|
||||
const pushCompletion = (label: string) => (res: { result: unknown }) => {
|
||||
completionOrder.push(String(res.result ?? label))
|
||||
return res
|
||||
}
|
||||
|
||||
const p1 = executeInIsolatedVM({
|
||||
code: 'return 1',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 500,
|
||||
requestId: 'a-1',
|
||||
ownerKey: 'user:a',
|
||||
ownerWeight: 2,
|
||||
}).then(pushCompletion('a-1'))
|
||||
|
||||
const p2 = executeInIsolatedVM({
|
||||
code: 'return 2',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 500,
|
||||
requestId: 'a-2',
|
||||
ownerKey: 'user:a',
|
||||
ownerWeight: 2,
|
||||
}).then(pushCompletion('a-2'))
|
||||
|
||||
const p3 = executeInIsolatedVM({
|
||||
code: 'return 3',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 500,
|
||||
requestId: 'b-1',
|
||||
ownerKey: 'user:b',
|
||||
ownerWeight: 1,
|
||||
}).then(pushCompletion('b-1'))
|
||||
|
||||
const p4 = executeInIsolatedVM({
|
||||
code: 'return 4',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 500,
|
||||
requestId: 'b-2',
|
||||
ownerKey: 'user:b',
|
||||
ownerWeight: 1,
|
||||
}).then(pushCompletion('b-2'))
|
||||
|
||||
const p5 = executeInIsolatedVM({
|
||||
code: 'return 5',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 500,
|
||||
requestId: 'a-3',
|
||||
ownerKey: 'user:a',
|
||||
ownerWeight: 2,
|
||||
}).then(pushCompletion('a-3'))
|
||||
|
||||
await Promise.all([p1, p2, p3, p4, p5])
|
||||
|
||||
expect(completionOrder.slice(0, 3)).toEqual(['a-1', 'a-2', 'a-3'])
|
||||
expect(completionOrder).toEqual(['a-1', 'a-2', 'a-3', 'b-1', 'b-2'])
|
||||
})
|
||||
|
||||
it('rejects oversized fetch options payloads before outbound call', async () => {
|
||||
const { executeInIsolatedVM, secureFetchMock } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_MAX_FETCH_OPTIONS_JSON_CHARS: '50',
|
||||
},
|
||||
spawns: [
|
||||
() =>
|
||||
createReadyFetchProxyProc({
|
||||
url: 'https://example.com',
|
||||
optionsJson: 'x'.repeat(100),
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "fetch-options"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-fetch-options',
|
||||
})
|
||||
|
||||
const payload = JSON.parse(String(result.result))
|
||||
expect(payload.error).toContain('Fetch options exceed maximum payload size')
|
||||
expect(secureFetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects overly long fetch URLs before outbound call', async () => {
|
||||
const { executeInIsolatedVM, secureFetchMock } = await loadExecutionModule({
|
||||
envOverrides: {
|
||||
IVM_MAX_FETCH_URL_LENGTH: '30',
|
||||
},
|
||||
spawns: [
|
||||
() =>
|
||||
createReadyFetchProxyProc({
|
||||
url: 'https://example.com/path/to/a/very/long/resource',
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
const result = await executeInIsolatedVM({
|
||||
code: 'return "fetch-url"',
|
||||
params: {},
|
||||
envVars: {},
|
||||
contextVariables: {},
|
||||
timeoutMs: 100,
|
||||
requestId: 'req-fetch-url',
|
||||
})
|
||||
|
||||
const payload = JSON.parse(String(result.result))
|
||||
expect(payload.error).toContain('fetch URL exceeds maximum length')
|
||||
expect(secureFetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -124,6 +124,7 @@ export interface PreprocessExecutionOptions {
|
||||
workspaceId?: string // If known, used for billing resolution
|
||||
loggingSession?: LoggingSession // If provided, will be used for error logging
|
||||
isResumeContext?: boolean // If true, allows fallback billing on resolution failure (for paused workflow resumes)
|
||||
useAuthenticatedUserAsActor?: boolean // If true, use the authenticated userId as actorUserId (for client-side executions and personal API keys)
|
||||
/** @deprecated No longer used - background/async executions always use deployed state */
|
||||
useDraftState?: boolean
|
||||
}
|
||||
@@ -170,6 +171,7 @@ export async function preprocessExecution(
|
||||
workspaceId: providedWorkspaceId,
|
||||
loggingSession: providedLoggingSession,
|
||||
isResumeContext = false,
|
||||
useAuthenticatedUserAsActor = false,
|
||||
} = options
|
||||
|
||||
logger.info(`[${requestId}] Starting execution preprocessing`, {
|
||||
@@ -257,7 +259,14 @@ export async function preprocessExecution(
|
||||
let actorUserId: string | null = null
|
||||
|
||||
try {
|
||||
if (workspaceId) {
|
||||
// For client-side executions and personal API keys, the authenticated
|
||||
// user is the billing and permission actor — not the workspace owner.
|
||||
if (useAuthenticatedUserAsActor && userId) {
|
||||
actorUserId = userId
|
||||
logger.info(`[${requestId}] Using authenticated user as actor: ${actorUserId}`)
|
||||
}
|
||||
|
||||
if (!actorUserId && workspaceId) {
|
||||
actorUserId = await getWorkspaceBilledAccountUserId(workspaceId)
|
||||
if (actorUserId) {
|
||||
logger.info(`[${requestId}] Using workspace billed account: ${actorUserId}`)
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { db } from '@sim/db'
|
||||
import { account } from '@sim/db/schema'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { getBaseUrl } from '@/lib/core/utils/urls'
|
||||
import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
|
||||
import { executeProviderRequest } from '@/providers'
|
||||
import { getApiKey, getProviderFromModel } from '@/providers/utils'
|
||||
import { getProviderFromModel } from '@/providers/utils'
|
||||
|
||||
const logger = createLogger('HallucinationValidator')
|
||||
|
||||
@@ -19,7 +23,18 @@ export interface HallucinationValidationInput {
|
||||
topK: number // Number of chunks to retrieve, default 10
|
||||
model: string
|
||||
apiKey?: string
|
||||
providerCredentials?: {
|
||||
azureEndpoint?: string
|
||||
azureApiVersion?: string
|
||||
vertexProject?: string
|
||||
vertexLocation?: string
|
||||
vertexCredential?: string
|
||||
bedrockAccessKeyId?: string
|
||||
bedrockSecretKey?: string
|
||||
bedrockRegion?: string
|
||||
}
|
||||
workflowId?: string
|
||||
workspaceId?: string
|
||||
requestId: string
|
||||
}
|
||||
|
||||
@@ -89,7 +104,9 @@ async function scoreHallucinationWithLLM(
|
||||
userInput: string,
|
||||
ragContext: string[],
|
||||
model: string,
|
||||
apiKey: string,
|
||||
apiKey: string | undefined,
|
||||
providerCredentials: HallucinationValidationInput['providerCredentials'],
|
||||
workspaceId: string | undefined,
|
||||
requestId: string
|
||||
): Promise<{ score: number; reasoning: string }> {
|
||||
try {
|
||||
@@ -127,6 +144,23 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
|
||||
|
||||
const providerId = getProviderFromModel(model)
|
||||
|
||||
let finalApiKey: string | undefined = apiKey
|
||||
if (providerId === 'vertex' && providerCredentials?.vertexCredential) {
|
||||
const credential = await db.query.account.findFirst({
|
||||
where: eq(account.id, providerCredentials.vertexCredential),
|
||||
})
|
||||
if (credential) {
|
||||
const { accessToken } = await refreshTokenIfNeeded(
|
||||
requestId,
|
||||
credential,
|
||||
providerCredentials.vertexCredential
|
||||
)
|
||||
if (accessToken) {
|
||||
finalApiKey = accessToken
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const response = await executeProviderRequest(providerId, {
|
||||
model,
|
||||
systemPrompt,
|
||||
@@ -137,7 +171,15 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
|
||||
},
|
||||
],
|
||||
temperature: 0.1, // Low temperature for consistent scoring
|
||||
apiKey,
|
||||
apiKey: finalApiKey,
|
||||
azureEndpoint: providerCredentials?.azureEndpoint,
|
||||
azureApiVersion: providerCredentials?.azureApiVersion,
|
||||
vertexProject: providerCredentials?.vertexProject,
|
||||
vertexLocation: providerCredentials?.vertexLocation,
|
||||
bedrockAccessKeyId: providerCredentials?.bedrockAccessKeyId,
|
||||
bedrockSecretKey: providerCredentials?.bedrockSecretKey,
|
||||
bedrockRegion: providerCredentials?.bedrockRegion,
|
||||
workspaceId,
|
||||
})
|
||||
|
||||
if (response instanceof ReadableStream || ('stream' in response && 'execution' in response)) {
|
||||
@@ -184,8 +226,18 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
|
||||
export async function validateHallucination(
|
||||
input: HallucinationValidationInput
|
||||
): Promise<HallucinationValidationResult> {
|
||||
const { userInput, knowledgeBaseId, threshold, topK, model, apiKey, workflowId, requestId } =
|
||||
input
|
||||
const {
|
||||
userInput,
|
||||
knowledgeBaseId,
|
||||
threshold,
|
||||
topK,
|
||||
model,
|
||||
apiKey,
|
||||
providerCredentials,
|
||||
workflowId,
|
||||
workspaceId,
|
||||
requestId,
|
||||
} = input
|
||||
|
||||
try {
|
||||
if (!userInput || userInput.trim().length === 0) {
|
||||
@@ -202,17 +254,6 @@ export async function validateHallucination(
|
||||
}
|
||||
}
|
||||
|
||||
let finalApiKey: string
|
||||
try {
|
||||
const providerId = getProviderFromModel(model)
|
||||
finalApiKey = getApiKey(providerId, model, apiKey)
|
||||
} catch (error: any) {
|
||||
return {
|
||||
passed: false,
|
||||
error: `API key error: ${error.message}`,
|
||||
}
|
||||
}
|
||||
|
||||
// Step 1: Query knowledge base with RAG
|
||||
const ragContext = await queryKnowledgeBase(
|
||||
knowledgeBaseId,
|
||||
@@ -234,7 +275,9 @@ export async function validateHallucination(
|
||||
userInput,
|
||||
ragContext,
|
||||
model,
|
||||
finalApiKey,
|
||||
apiKey,
|
||||
providerCredentials,
|
||||
workspaceId,
|
||||
requestId
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import type { NextRequest, NextResponse } from 'next/server'
|
||||
import { checkHybridAuth } from '@/lib/auth/hybrid'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { createMcpErrorResponse } from '@/lib/mcp/utils'
|
||||
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
||||
@@ -43,7 +43,7 @@ async function validateMcpAuth(
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
logger.warn(`[${requestId}] Authentication failed: ${auth.error}`)
|
||||
return {
|
||||
|
||||
@@ -21,6 +21,11 @@ export const TOKENIZATION_CONFIG = {
|
||||
confidence: 'high',
|
||||
supportedMethods: ['heuristic', 'fallback'],
|
||||
},
|
||||
'azure-anthropic': {
|
||||
avgCharsPerToken: 4.5,
|
||||
confidence: 'high',
|
||||
supportedMethods: ['heuristic', 'fallback'],
|
||||
},
|
||||
google: {
|
||||
avgCharsPerToken: 5,
|
||||
confidence: 'medium',
|
||||
|
||||
@@ -204,6 +204,7 @@ export function estimateTokenCount(text: string, providerId?: string): TokenEsti
|
||||
estimatedTokens = estimateOpenAITokens(text)
|
||||
break
|
||||
case 'anthropic':
|
||||
case 'azure-anthropic':
|
||||
estimatedTokens = estimateAnthropicTokens(text)
|
||||
break
|
||||
case 'google':
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
validateTypeformSignature,
|
||||
verifyProviderWebhook,
|
||||
} from '@/lib/webhooks/utils.server'
|
||||
import { getWorkspaceBilledAccountUserId } from '@/lib/workspaces/utils'
|
||||
import { executeWebhookJob } from '@/background/webhook-execution'
|
||||
import { resolveEnvVarReferences } from '@/executor/utils/reference-validation'
|
||||
import { isGitHubEventMatch } from '@/triggers/github/utils'
|
||||
@@ -1003,10 +1004,23 @@ export async function queueWebhookExecution(
|
||||
}
|
||||
}
|
||||
|
||||
if (!foundWorkflow.workspaceId) {
|
||||
logger.error(`[${options.requestId}] Workflow ${foundWorkflow.id} has no workspaceId`)
|
||||
return NextResponse.json({ error: 'Workflow has no associated workspace' }, { status: 500 })
|
||||
}
|
||||
|
||||
const actorUserId = await getWorkspaceBilledAccountUserId(foundWorkflow.workspaceId)
|
||||
if (!actorUserId) {
|
||||
logger.error(
|
||||
`[${options.requestId}] No billing account for workspace ${foundWorkflow.workspaceId}`
|
||||
)
|
||||
return NextResponse.json({ error: 'Unable to resolve billing account' }, { status: 500 })
|
||||
}
|
||||
|
||||
const payload = {
|
||||
webhookId: foundWebhook.id,
|
||||
workflowId: foundWorkflow.id,
|
||||
userId: foundWorkflow.userId,
|
||||
userId: actorUserId,
|
||||
provider: foundWebhook.provider,
|
||||
body,
|
||||
headers,
|
||||
@@ -1017,7 +1031,7 @@ export async function queueWebhookExecution(
|
||||
|
||||
const jobQueue = await getJobQueue()
|
||||
const jobId = await jobQueue.enqueue('webhook-execution', payload, {
|
||||
metadata: { workflowId: foundWorkflow.id, userId: foundWorkflow.userId },
|
||||
metadata: { workflowId: foundWorkflow.id, userId: actorUserId },
|
||||
})
|
||||
logger.info(
|
||||
`[${options.requestId}] Queued webhook execution task ${jobId} for ${foundWebhook.provider} webhook`
|
||||
|
||||
@@ -156,6 +156,15 @@ describe('evaluateSubBlockCondition', () => {
|
||||
expect(evaluateSubBlockCondition(condition, values)).toBe(true)
|
||||
})
|
||||
|
||||
it.concurrent('passes current values into function conditions', () => {
|
||||
const condition = (values?: Record<string, unknown>) => ({
|
||||
field: 'model',
|
||||
value: typeof values?.model === 'string' ? values.model : '__no_model_selected__',
|
||||
})
|
||||
const values = { model: 'ollama/gemma3:4b' }
|
||||
expect(evaluateSubBlockCondition(condition, values)).toBe(true)
|
||||
})
|
||||
|
||||
it.concurrent('handles boolean values', () => {
|
||||
const condition = { field: 'enabled', value: true }
|
||||
const values = { enabled: true }
|
||||
|
||||
@@ -100,11 +100,14 @@ export function resolveCanonicalMode(
|
||||
* Evaluate a subblock condition against a map of raw values.
|
||||
*/
|
||||
export function evaluateSubBlockCondition(
|
||||
condition: SubBlockCondition | (() => SubBlockCondition) | undefined,
|
||||
condition:
|
||||
| SubBlockCondition
|
||||
| ((values?: Record<string, unknown>) => SubBlockCondition)
|
||||
| undefined,
|
||||
values: Record<string, unknown>
|
||||
): boolean {
|
||||
if (!condition) return true
|
||||
const actual = typeof condition === 'function' ? condition() : condition
|
||||
const actual = typeof condition === 'function' ? condition(values) : condition
|
||||
const fieldValue = values[actual.field]
|
||||
const valueMatch = Array.isArray(actual.value)
|
||||
? fieldValue != null &&
|
||||
|
||||
@@ -35,6 +35,8 @@ export const azureAnthropicProvider: ProviderConfig = {
|
||||
// The SDK appends /v1/messages automatically
|
||||
const baseURL = `${request.azureEndpoint.replace(/\/$/, '')}/anthropic`
|
||||
|
||||
const anthropicVersion = request.azureApiVersion || '2023-06-01'
|
||||
|
||||
return executeAnthropicProviderRequest(
|
||||
{
|
||||
...request,
|
||||
@@ -49,7 +51,7 @@ export const azureAnthropicProvider: ProviderConfig = {
|
||||
apiKey,
|
||||
defaultHeaders: {
|
||||
'api-key': apiKey,
|
||||
'anthropic-version': '2023-06-01',
|
||||
'anthropic-version': anthropicVersion,
|
||||
...(useNativeStructuredOutputs
|
||||
? { 'anthropic-beta': 'structured-outputs-2025-11-13' }
|
||||
: {}),
|
||||
|
||||
@@ -9,6 +9,14 @@ export interface GuardrailsValidateInput {
|
||||
topK?: string
|
||||
model?: string
|
||||
apiKey?: string
|
||||
azureEndpoint?: string
|
||||
azureApiVersion?: string
|
||||
vertexProject?: string
|
||||
vertexLocation?: string
|
||||
vertexCredential?: string
|
||||
bedrockAccessKeyId?: string
|
||||
bedrockSecretKey?: string
|
||||
bedrockRegion?: string
|
||||
piiEntityTypes?: string[]
|
||||
piiMode?: string
|
||||
piiLanguage?: string
|
||||
@@ -166,6 +174,14 @@ export const guardrailsValidateTool: ToolConfig<GuardrailsValidateInput, Guardra
|
||||
topK: params.topK,
|
||||
model: params.model,
|
||||
apiKey: params.apiKey,
|
||||
azureEndpoint: params.azureEndpoint,
|
||||
azureApiVersion: params.azureApiVersion,
|
||||
vertexProject: params.vertexProject,
|
||||
vertexLocation: params.vertexLocation,
|
||||
vertexCredential: params.vertexCredential,
|
||||
bedrockAccessKeyId: params.bedrockAccessKeyId,
|
||||
bedrockSecretKey: params.bedrockSecretKey,
|
||||
bedrockRegion: params.bedrockRegion,
|
||||
piiEntityTypes: params.piiEntityTypes,
|
||||
piiMode: params.piiMode,
|
||||
piiLanguage: params.piiLanguage,
|
||||
|
||||
@@ -247,7 +247,8 @@ export async function executeTool(
|
||||
// If it's a custom tool, use the async version with workflowId
|
||||
if (isCustomTool(normalizedToolId)) {
|
||||
const workflowId = params._context?.workflowId
|
||||
tool = await getToolAsync(normalizedToolId, workflowId)
|
||||
const userId = params._context?.userId
|
||||
tool = await getToolAsync(normalizedToolId, workflowId, userId)
|
||||
if (!tool) {
|
||||
logger.error(`[${requestId}] Custom tool not found: ${normalizedToolId}`)
|
||||
}
|
||||
@@ -286,26 +287,25 @@ export async function executeTool(
|
||||
try {
|
||||
const baseUrl = getBaseUrl()
|
||||
|
||||
const workflowId = contextParams._context?.workflowId
|
||||
const userId = contextParams._context?.userId
|
||||
|
||||
const tokenPayload: OAuthTokenPayload = {
|
||||
credentialId: contextParams.credential as string,
|
||||
}
|
||||
|
||||
// Add workflowId if it exists in params, context, or executionContext
|
||||
const workflowId =
|
||||
contextParams.workflowId ||
|
||||
contextParams._context?.workflowId ||
|
||||
executionContext?.workflowId
|
||||
if (workflowId) {
|
||||
tokenPayload.workflowId = workflowId
|
||||
}
|
||||
|
||||
logger.info(`[${requestId}] Fetching access token from ${baseUrl}/api/auth/oauth/token`)
|
||||
|
||||
// Build token URL and also include workflowId in query so server auth can read it
|
||||
const tokenUrlObj = new URL('/api/auth/oauth/token', baseUrl)
|
||||
if (workflowId) {
|
||||
tokenUrlObj.searchParams.set('workflowId', workflowId)
|
||||
}
|
||||
if (userId) {
|
||||
tokenUrlObj.searchParams.set('userId', userId)
|
||||
}
|
||||
|
||||
// Always send Content-Type; add internal auth on server-side runs
|
||||
const tokenHeaders: Record<string, string> = { 'Content-Type': 'application/json' }
|
||||
@@ -609,6 +609,10 @@ async function executeToolRequest(
|
||||
if (workflowId) {
|
||||
fullUrlObj.searchParams.set('workflowId', workflowId)
|
||||
}
|
||||
const userId = params._context?.userId
|
||||
if (userId) {
|
||||
fullUrlObj.searchParams.set('userId', userId)
|
||||
}
|
||||
}
|
||||
|
||||
const fullUrl = fullUrlObj.toString()
|
||||
@@ -957,6 +961,7 @@ async function executeMcpTool(
|
||||
|
||||
const workspaceId = params._context?.workspaceId || executionContext?.workspaceId
|
||||
const workflowId = params._context?.workflowId || executionContext?.workflowId
|
||||
const userId = params._context?.userId || executionContext?.userId
|
||||
|
||||
if (!workspaceId) {
|
||||
return {
|
||||
@@ -998,7 +1003,12 @@ async function executeMcpTool(
|
||||
hasToolSchema: !!toolSchema,
|
||||
})
|
||||
|
||||
const response = await fetch(`${baseUrl}/api/mcp/tools/execute`, {
|
||||
const mcpUrl = new URL('/api/mcp/tools/execute', baseUrl)
|
||||
if (userId) {
|
||||
mcpUrl.searchParams.set('userId', userId)
|
||||
}
|
||||
|
||||
const response = await fetch(mcpUrl.toString(), {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body,
|
||||
|
||||
@@ -311,7 +311,8 @@ export function getTool(toolId: string): ToolConfig | undefined {
|
||||
// Get a tool by its ID asynchronously (supports server-side)
|
||||
export async function getToolAsync(
|
||||
toolId: string,
|
||||
workflowId?: string
|
||||
workflowId?: string,
|
||||
userId?: string
|
||||
): Promise<ToolConfig | undefined> {
|
||||
// Check for built-in tools
|
||||
const builtInTool = tools[toolId]
|
||||
@@ -319,7 +320,7 @@ export async function getToolAsync(
|
||||
|
||||
// Check if it's a custom tool
|
||||
if (isCustomTool(toolId)) {
|
||||
return fetchCustomToolFromAPI(toolId, workflowId)
|
||||
return fetchCustomToolFromAPI(toolId, workflowId, userId)
|
||||
}
|
||||
|
||||
return undefined
|
||||
@@ -366,7 +367,8 @@ function createToolConfig(customTool: any, customToolId: string): ToolConfig {
|
||||
// Create a tool config from a custom tool definition by fetching from API
|
||||
async function fetchCustomToolFromAPI(
|
||||
customToolId: string,
|
||||
workflowId?: string
|
||||
workflowId?: string,
|
||||
userId?: string
|
||||
): Promise<ToolConfig | undefined> {
|
||||
const identifier = customToolId.replace('custom_', '')
|
||||
|
||||
@@ -374,10 +376,12 @@ async function fetchCustomToolFromAPI(
|
||||
const baseUrl = getBaseUrl()
|
||||
const url = new URL('/api/tools/custom', baseUrl)
|
||||
|
||||
// Add workflowId as a query parameter if available
|
||||
if (workflowId) {
|
||||
url.searchParams.append('workflowId', workflowId)
|
||||
}
|
||||
if (userId) {
|
||||
url.searchParams.append('userId', userId)
|
||||
}
|
||||
|
||||
// For server-side calls (during workflow execution), use internal JWT token
|
||||
const headers: Record<string, string> = {}
|
||||
|
||||
@@ -139,7 +139,25 @@ app:
|
||||
EXECUTION_TIMEOUT_ASYNC_PRO: "5400" # Pro tier async timeout (90 minutes)
|
||||
EXECUTION_TIMEOUT_ASYNC_TEAM: "5400" # Team tier async timeout (90 minutes)
|
||||
EXECUTION_TIMEOUT_ASYNC_ENTERPRISE: "5400" # Enterprise tier async timeout (90 minutes)
|
||||
|
||||
|
||||
# Isolated-VM Worker Pool Configuration
|
||||
IVM_POOL_SIZE: "4" # Max worker processes in pool
|
||||
IVM_MAX_CONCURRENT: "10000" # Max concurrent executions globally
|
||||
IVM_MAX_PER_WORKER: "2500" # Max concurrent executions per worker
|
||||
IVM_WORKER_IDLE_TIMEOUT_MS: "60000" # Worker idle cleanup timeout (ms)
|
||||
IVM_QUEUE_TIMEOUT_MS: "300000" # Max queue wait before rejection (ms)
|
||||
IVM_MAX_QUEUE_SIZE: "10000" # Max queued executions globally
|
||||
IVM_MAX_ACTIVE_PER_OWNER: "200" # Max concurrent executions per user
|
||||
IVM_MAX_QUEUED_PER_OWNER: "2000" # Max queued executions per user
|
||||
IVM_MAX_OWNER_WEIGHT: "5" # Max scheduling weight per user
|
||||
IVM_DISTRIBUTED_MAX_INFLIGHT_PER_OWNER: "2200" # Max in-flight per user across instances (Redis)
|
||||
IVM_DISTRIBUTED_LEASE_MIN_TTL_MS: "120000" # Min distributed lease TTL (ms)
|
||||
IVM_MAX_FETCH_RESPONSE_BYTES: "8388608" # Max fetch response size (8MB)
|
||||
IVM_MAX_FETCH_RESPONSE_CHARS: "4000000" # Max fetch response chars
|
||||
IVM_MAX_FETCH_URL_LENGTH: "8192" # Max fetch URL length
|
||||
IVM_MAX_FETCH_OPTIONS_JSON_CHARS: "262144" # Max fetch options payload (256KB)
|
||||
IVM_MAX_STDOUT_CHARS: "200000" # Max stdout capture per execution
|
||||
|
||||
# UI Branding & Whitelabeling Configuration
|
||||
NEXT_PUBLIC_BRAND_NAME: "Sim" # Custom brand name
|
||||
NEXT_PUBLIC_BRAND_LOGO_URL: "" # Custom logo URL (leave empty for default)
|
||||
|
||||
Reference in New Issue
Block a user