Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

111 lines
3.9 KiB
TypeScript

import { truncate } from '@sim/utils/string'
import { validateExternalUrl } from '@/lib/core/security/input-validation'
import type {
ConvexFunctionCallApiResponse,
ConvexFunctionCallResponse,
} from '@/tools/convex/types'
/**
* Builds a Convex deployment API URL from the user-provided deployment URL.
* Accepts URLs with or without a trailing slash.
*
* The deployment URL is validated with the shared SSRF guard so invalid hosts
* fail fast with a clear message; the tool executor additionally re-validates
* with DNS resolution and pins the resolved IP for the actual request.
*/
export function convexApiUrl(deploymentUrl: string, path: string): string {
const trimmed = deploymentUrl.trim().replace(/\/+$/, '')
const validation = validateExternalUrl(trimmed, 'Deployment URL')
if (!validation.isValid) {
throw new Error(`${validation.error} (e.g., https://your-deployment.convex.cloud)`)
}
const parsed = new URL(trimmed)
if (parsed.search || parsed.hash) {
throw new Error(
'Deployment URL must not include a query string or fragment (e.g., https://your-deployment.convex.cloud)'
)
}
return `${trimmed}${path}`
}
/**
* Builds the deployment admin authorization header for Convex HTTP API requests.
* @see https://docs.convex.dev/http-api/#api-authentication
*/
export function convexAuthHeaders(deployKey: string): Record<string, string> {
return { Authorization: `Convex ${deployKey.trim()}` }
}
/**
* Parses function arguments that may arrive as a JSON string or an object.
* Convex function endpoints require a named-argument object.
*/
export function parseFunctionArgs(
args: Record<string, unknown> | string | undefined
): Record<string, unknown> {
if (args === undefined || args === null) return {}
if (typeof args === 'string') {
const trimmed = args.trim()
if (!trimmed) return {}
let parsed: unknown
try {
parsed = JSON.parse(trimmed)
} catch {
throw new Error('Invalid function arguments: expected a JSON object like {"key": "value"}')
}
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('Invalid function arguments: expected a JSON object, not an array or scalar')
}
return parsed as Record<string, unknown>
}
if (typeof args !== 'object' || Array.isArray(args)) {
throw new Error('Invalid function arguments: expected a JSON object, not an array or scalar')
}
return args
}
/**
* Parses a Convex API response body, surfacing non-OK HTTP statuses (e.g. 401
* from an invalid deploy key) as descriptive errors instead of empty results.
*/
export async function parseConvexResponse(response: Response): Promise<unknown> {
if (!response.ok) {
const text = await response.text().catch(() => '')
throw new Error(
`Convex request failed (HTTP ${response.status})${text ? `: ${truncate(text.trim(), 300)}` : ''}`
)
}
return response.json()
}
/**
* Transforms a Convex function-call response. Convex returns HTTP 200 with an
* in-band `status: "error"` payload when the function itself fails, so errors
* must be surfaced here rather than relying on the HTTP status code.
* @see https://docs.convex.dev/http-api/#post-apiquery-apimutation-apiaction
*/
export async function transformFunctionCallResponse(
response: Response,
functionType: 'query' | 'mutation' | 'action' | 'function'
): Promise<ConvexFunctionCallResponse> {
const data = (await parseConvexResponse(response)) as ConvexFunctionCallApiResponse
if (data.status === 'error') {
const details =
data.errorData !== undefined && data.errorData !== null
? ` (${JSON.stringify(data.errorData)})`
: ''
throw new Error(
`Convex ${functionType} failed: ${data.errorMessage || 'Unknown error'}${details}`
)
}
return {
success: true,
output: {
value: data.value ?? null,
logLines: data.logLines ?? [],
},
}
}