diff --git a/apps/docs/content/docs/integrations/huggingface.mdx b/apps/docs/content/docs/integrations/huggingface.mdx index a09ac12149d..43aa594f554 100644 --- a/apps/docs/content/docs/integrations/huggingface.mdx +++ b/apps/docs/content/docs/integrations/huggingface.mdx @@ -11,6 +11,8 @@ import { ReferenceDetails } from "@/components/ui/reference-details" {/* MANUAL-CONTENT-START:intro */} Use [Hugging Face](https://huggingface.co/) in Sim to generate text completions with the Hugging Face Inference API. Choose a model and supply the prompt for the workflow step. + +The **Provider** list shows the providers that serve chat completions through the Hugging Face router: Novita, Cerebras, Cohere, Fireworks, HF Inference, Nscale and Together. Fal AI, Hyperbolic, Nebius, Replicate and SambaNova are no longer listed because Hugging Face does not route chat completions to them. A block that already uses one of them keeps its saved setting. {/* MANUAL-CONTENT-END */} ## Use the block [#usage-instructions] diff --git a/apps/sim/blocks/blocks/huggingface.ts b/apps/sim/blocks/blocks/huggingface.ts index 7824c951161..f0c177abf7c 100644 --- a/apps/sim/blocks/blocks/huggingface.ts +++ b/apps/sim/blocks/blocks/huggingface.ts @@ -47,18 +47,19 @@ export const HuggingFaceBlock: BlockConfig = { title: 'Provider', type: 'dropdown', required: true, + // Hidden providers have no chat-completions route in @huggingface/inference; kept so saved workflows still validate. options: [ { label: 'Novita', id: 'novita' }, { label: 'Cerebras', id: 'cerebras' }, { label: 'Cohere', id: 'cohere' }, - { label: 'Fal AI', id: 'fal' }, + { label: 'Fal AI', id: 'fal', hidden: true }, { label: 'Fireworks', id: 'fireworks' }, - { label: 'Hyperbolic', id: 'hyperbolic' }, + { label: 'Hyperbolic', id: 'hyperbolic', hidden: true }, { label: 'HF Inference', id: 'hf-inference' }, - { label: 'Nebius', id: 'nebius' }, + { label: 'Nebius', id: 'nebius', hidden: true }, { label: 'Nscale', id: 'nscale' }, - { label: 'Replicate', id: 'replicate' }, - { label: 'SambaNova', id: 'sambanova' }, + { label: 'Replicate', id: 'replicate', hidden: true }, + { label: 'SambaNova', id: 'sambanova', hidden: true }, { label: 'Together', id: 'together' }, ], value: () => 'novita', diff --git a/apps/sim/components/settings/navigation.ts b/apps/sim/components/settings/navigation.ts index 3dfe2728de0..86b7a34dfd0 100644 --- a/apps/sim/components/settings/navigation.ts +++ b/apps/sim/components/settings/navigation.ts @@ -370,7 +370,7 @@ export const SETTINGS_SECTION_REGISTRY: readonly SettingsSectionRegistryEntry[] requiresDesktopSurface: 'settings', }, /** Device settings, not workspace data, so surfaces without a workspace host them too. */ - planes: { account: { id: 'desktop', group: 'account', order: 1 } }, + planes: { account: { id: 'desktop', group: 'account', order: 2 } }, }, { label: 'Browser', diff --git a/apps/sim/tools/posthog/create_annotation.ts b/apps/sim/tools/posthog/create_annotation.ts index 788cd4880de..408c86140d3 100644 --- a/apps/sim/tools/posthog/create_annotation.ts +++ b/apps/sim/tools/posthog/create_annotation.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogCreateAnnotationParams { @@ -108,7 +108,7 @@ export const createAnnotationTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/annotations/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/annotations/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_cohort.ts b/apps/sim/tools/posthog/create_cohort.ts index c68711bce4a..a0e099ad179 100644 --- a/apps/sim/tools/posthog/create_cohort.ts +++ b/apps/sim/tools/posthog/create_cohort.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogCreateCohortParams { @@ -112,7 +112,7 @@ export const createCohortTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/cohorts/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/cohorts/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_dashboard.ts b/apps/sim/tools/posthog/create_dashboard.ts index 4cfa617fd63..8e9058f56cc 100644 --- a/apps/sim/tools/posthog/create_dashboard.ts +++ b/apps/sim/tools/posthog/create_dashboard.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogCreateDashboardParams { @@ -101,7 +101,7 @@ export const createDashboardTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/dashboards/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/dashboards/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_experiment.ts b/apps/sim/tools/posthog/create_experiment.ts index de06391cd75..f7af018b685 100644 --- a/apps/sim/tools/posthog/create_experiment.ts +++ b/apps/sim/tools/posthog/create_experiment.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface CreateExperimentParams { @@ -118,7 +118,7 @@ export const createExperimentTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/experiments/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/experiments/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_feature_flag.ts b/apps/sim/tools/posthog/create_feature_flag.ts index 3aef9455d20..a017349ac2f 100644 --- a/apps/sim/tools/posthog/create_feature_flag.ts +++ b/apps/sim/tools/posthog/create_feature_flag.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface CreateFeatureFlagParams { @@ -128,7 +128,7 @@ export const createFeatureFlagTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/feature_flags/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/feature_flags/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_insight.ts b/apps/sim/tools/posthog/create_insight.ts index 885197f8259..2db3f6dc928 100644 --- a/apps/sim/tools/posthog/create_insight.ts +++ b/apps/sim/tools/posthog/create_insight.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogCreateInsightParams { @@ -101,7 +101,7 @@ export const createInsightTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/insights/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/insights/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/create_survey.ts b/apps/sim/tools/posthog/create_survey.ts index 13fdc1568f8..0fe4eec2514 100644 --- a/apps/sim/tools/posthog/create_survey.ts +++ b/apps/sim/tools/posthog/create_survey.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogSurveyQuestion { @@ -165,7 +165,7 @@ export const createSurveyTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/surveys/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/surveys/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/delete_feature_flag.ts b/apps/sim/tools/posthog/delete_feature_flag.ts index 92d2f1c4209..1b7805888c2 100644 --- a/apps/sim/tools/posthog/delete_feature_flag.ts +++ b/apps/sim/tools/posthog/delete_feature_flag.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface DeleteFeatureFlagParams { @@ -61,7 +61,7 @@ export const deleteFeatureFlagTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/feature_flags/${params.flagId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/feature_flags/${postHogPathSegment(params.flagId, 'flagId')}/` }, // PostHog does not allow a hard DELETE on feature flags (always returns 405). // Deletion is a soft-delete via PATCH with deleted: true. diff --git a/apps/sim/tools/posthog/delete_person.ts b/apps/sim/tools/posthog/delete_person.ts index 3d5d1553bcf..ca998d37be0 100644 --- a/apps/sim/tools/posthog/delete_person.ts +++ b/apps/sim/tools/posthog/delete_person.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogDeletePersonParams { @@ -65,7 +65,7 @@ export const deletePersonTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/persons/bulk_delete/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/persons/bulk_delete/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/delete_survey.ts b/apps/sim/tools/posthog/delete_survey.ts index 293f22035d7..a5b8fb2acb6 100644 --- a/apps/sim/tools/posthog/delete_survey.ts +++ b/apps/sim/tools/posthog/delete_survey.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogDeleteSurveyParams { @@ -62,7 +62,7 @@ export const deleteSurveyTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/surveys/${params.surveyId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/surveys/${postHogPathSegment(params.surveyId, 'surveyId')}/` }, method: 'DELETE', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_cohort.ts b/apps/sim/tools/posthog/get_cohort.ts index f4887544aba..7fb852c685a 100644 --- a/apps/sim/tools/posthog/get_cohort.ts +++ b/apps/sim/tools/posthog/get_cohort.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogGetCohortParams { @@ -76,7 +76,7 @@ export const getCohortTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/cohorts/${params.cohortId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/cohorts/${postHogPathSegment(params.cohortId, 'cohortId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_dashboard.ts b/apps/sim/tools/posthog/get_dashboard.ts index 4ba2cb33168..f8de0857702 100644 --- a/apps/sim/tools/posthog/get_dashboard.ts +++ b/apps/sim/tools/posthog/get_dashboard.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogGetDashboardParams { @@ -74,7 +74,7 @@ export const getDashboardTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/dashboards/${params.dashboardId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/dashboards/${postHogPathSegment(params.dashboardId, 'dashboardId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_event_definition.ts b/apps/sim/tools/posthog/get_event_definition.ts index d3c42a30016..0d3c1f2730b 100644 --- a/apps/sim/tools/posthog/get_event_definition.ts +++ b/apps/sim/tools/posthog/get_event_definition.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogGetEventDefinitionParams { @@ -77,7 +77,7 @@ export const getEventDefinitionTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/event_definitions/${params.eventDefinitionId}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/event_definitions/${postHogPathSegment(params.eventDefinitionId, 'eventDefinitionId')}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_experiment.ts b/apps/sim/tools/posthog/get_experiment.ts index 8cdd2327e4a..3f9a87fbd9d 100644 --- a/apps/sim/tools/posthog/get_experiment.ts +++ b/apps/sim/tools/posthog/get_experiment.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface GetExperimentParams { @@ -77,7 +77,7 @@ export const getExperimentTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/experiments/${params.experimentId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/experiments/${postHogPathSegment(params.experimentId, 'experimentId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_feature_flag.ts b/apps/sim/tools/posthog/get_feature_flag.ts index d8fc6d3f0e0..7896dfa2953 100644 --- a/apps/sim/tools/posthog/get_feature_flag.ts +++ b/apps/sim/tools/posthog/get_feature_flag.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface GetFeatureFlagParams { @@ -76,7 +76,7 @@ export const getFeatureFlagTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/feature_flags/${params.flagId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/feature_flags/${postHogPathSegment(params.flagId, 'flagId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_insight.ts b/apps/sim/tools/posthog/get_insight.ts index cd52a54d020..25bc926b7ff 100644 --- a/apps/sim/tools/posthog/get_insight.ts +++ b/apps/sim/tools/posthog/get_insight.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogGetInsightParams { @@ -72,7 +72,7 @@ export const getInsightTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - return `${baseUrl}/api/projects/${params.projectId}/insights/${params.insightId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/insights/${postHogPathSegment(params.insightId, 'insightId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_organization.ts b/apps/sim/tools/posthog/get_organization.ts index 8648df07c03..2e7a2e53b9c 100644 --- a/apps/sim/tools/posthog/get_organization.ts +++ b/apps/sim/tools/posthog/get_organization.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogGetOrganizationParams { @@ -83,7 +83,7 @@ export const getOrganizationTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/organizations/${params.organizationId}/` + return `${baseUrl}/api/organizations/${postHogPathSegment(params.organizationId, 'organizationId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_person.ts b/apps/sim/tools/posthog/get_person.ts index 5b022377ebf..ad2e2faeff1 100644 --- a/apps/sim/tools/posthog/get_person.ts +++ b/apps/sim/tools/posthog/get_person.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogGetPersonParams { @@ -68,7 +68,7 @@ export const getPersonTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/persons/${params.personId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/persons/${postHogPathSegment(params.personId, 'personId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_project.ts b/apps/sim/tools/posthog/get_project.ts index 6d3f42bf6d7..3dd19dd1090 100644 --- a/apps/sim/tools/posthog/get_project.ts +++ b/apps/sim/tools/posthog/get_project.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogGetProjectParams { @@ -84,7 +84,7 @@ export const getProjectTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_property_definition.ts b/apps/sim/tools/posthog/get_property_definition.ts index 73956b879bb..8df5782d00d 100644 --- a/apps/sim/tools/posthog/get_property_definition.ts +++ b/apps/sim/tools/posthog/get_property_definition.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogGetPropertyDefinitionParams { @@ -80,7 +80,7 @@ export const getPropertyDefinitionTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/property_definitions/${params.propertyDefinitionId}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/property_definitions/${postHogPathSegment(params.propertyDefinitionId, 'propertyDefinitionId')}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_session_recording.ts b/apps/sim/tools/posthog/get_session_recording.ts index 831591fd11f..fa595c95f24 100644 --- a/apps/sim/tools/posthog/get_session_recording.ts +++ b/apps/sim/tools/posthog/get_session_recording.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogGetSessionRecordingParams { @@ -87,7 +87,7 @@ export const getSessionRecordingTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/session_recordings/${params.recordingId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/session_recordings/${postHogPathSegment(params.recordingId, 'recordingId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/get_survey.ts b/apps/sim/tools/posthog/get_survey.ts index 9331e634d6f..6c31872be93 100644 --- a/apps/sim/tools/posthog/get_survey.ts +++ b/apps/sim/tools/posthog/get_survey.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogGetSurveyParams { @@ -90,7 +90,7 @@ export const getSurveyTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/surveys/${params.surveyId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/surveys/${postHogPathSegment(params.surveyId, 'surveyId')}/` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/list_actions.ts b/apps/sim/tools/posthog/list_actions.ts index 1bd7a92fdc6..74f9dc669c2 100644 --- a/apps/sim/tools/posthog/list_actions.ts +++ b/apps/sim/tools/posthog/list_actions.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListActionsParams { @@ -92,7 +92,7 @@ export const listActionsTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - let url = `${baseUrl}/api/projects/${params.projectId}/actions/` + let url = `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/actions/` const queryParams = [] if (params.limit) queryParams.push(`limit=${params.limit}`) diff --git a/apps/sim/tools/posthog/list_annotations.ts b/apps/sim/tools/posthog/list_annotations.ts index cd3602a4647..60070232e49 100644 --- a/apps/sim/tools/posthog/list_annotations.ts +++ b/apps/sim/tools/posthog/list_annotations.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListAnnotationsParams { @@ -88,7 +88,7 @@ export const listAnnotationsTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - let url = `${baseUrl}/api/projects/${params.projectId}/annotations/` + let url = `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/annotations/` const queryParams = [] if (params.limit) queryParams.push(`limit=${params.limit}`) diff --git a/apps/sim/tools/posthog/list_cohorts.ts b/apps/sim/tools/posthog/list_cohorts.ts index d7be4511de8..456e576ebd1 100644 --- a/apps/sim/tools/posthog/list_cohorts.ts +++ b/apps/sim/tools/posthog/list_cohorts.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListCohortsParams { @@ -87,7 +87,7 @@ export const listCohortsTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - let url = `${baseUrl}/api/projects/${params.projectId}/cohorts/` + let url = `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/cohorts/` const queryParams = [] if (params.limit) queryParams.push(`limit=${params.limit}`) diff --git a/apps/sim/tools/posthog/list_dashboards.ts b/apps/sim/tools/posthog/list_dashboards.ts index c19ffbe2cd0..eb10393f274 100644 --- a/apps/sim/tools/posthog/list_dashboards.ts +++ b/apps/sim/tools/posthog/list_dashboards.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListDashboardsParams { @@ -87,7 +87,7 @@ export const listDashboardsTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - let url = `${baseUrl}/api/projects/${params.projectId}/dashboards/` + let url = `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/dashboards/` const queryParams = [] if (params.limit) queryParams.push(`limit=${params.limit}`) diff --git a/apps/sim/tools/posthog/list_event_definitions.ts b/apps/sim/tools/posthog/list_event_definitions.ts index 2a67cc18e7e..c2b11550701 100644 --- a/apps/sim/tools/posthog/list_event_definitions.ts +++ b/apps/sim/tools/posthog/list_event_definitions.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogListEventDefinitionsParams { @@ -102,7 +102,7 @@ export const listEventDefinitionsTool: ToolConfig< if (params.search) queryParams.append('search', params.search) const query = queryParams.toString() - return `${baseUrl}/api/projects/${params.projectId}/event_definitions/${query ? `?${query}` : ''}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/event_definitions/${query ? `?${query}` : ''}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/list_experiments.ts b/apps/sim/tools/posthog/list_experiments.ts index d331b0390b6..4f58d30a008 100644 --- a/apps/sim/tools/posthog/list_experiments.ts +++ b/apps/sim/tools/posthog/list_experiments.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface ListExperimentsParams { @@ -85,7 +85,9 @@ export const listExperimentsTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - const url = new URL(`${baseUrl}/api/projects/${params.projectId}/experiments/`) + const url = new URL( + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/experiments/` + ) if (params.limit) url.searchParams.append('limit', String(params.limit)) if (params.offset) url.searchParams.append('offset', String(params.offset)) diff --git a/apps/sim/tools/posthog/list_feature_flags.ts b/apps/sim/tools/posthog/list_feature_flags.ts index 8ae7cdd624a..df2b793d1f6 100644 --- a/apps/sim/tools/posthog/list_feature_flags.ts +++ b/apps/sim/tools/posthog/list_feature_flags.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface ListFeatureFlagsParams { @@ -84,7 +84,9 @@ export const listFeatureFlagsTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - const url = new URL(`${baseUrl}/api/projects/${params.projectId}/feature_flags/`) + const url = new URL( + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/feature_flags/` + ) if (params.limit) url.searchParams.append('limit', String(params.limit)) if (params.offset) url.searchParams.append('offset', String(params.offset)) diff --git a/apps/sim/tools/posthog/list_insights.ts b/apps/sim/tools/posthog/list_insights.ts index 15aff43ae6a..1e7c74db764 100644 --- a/apps/sim/tools/posthog/list_insights.ts +++ b/apps/sim/tools/posthog/list_insights.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListInsightsParams { @@ -83,7 +83,7 @@ export const listInsightsTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region as 'us' | 'eu' | undefined, params.host) - let url = `${baseUrl}/api/projects/${params.projectId}/insights/` + let url = `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/insights/` const queryParams = [] if (params.limit) queryParams.push(`limit=${params.limit}`) diff --git a/apps/sim/tools/posthog/list_persons.ts b/apps/sim/tools/posthog/list_persons.ts index 442bf5b9c99..e349871eb93 100644 --- a/apps/sim/tools/posthog/list_persons.ts +++ b/apps/sim/tools/posthog/list_persons.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogListPersonsParams { @@ -94,7 +94,9 @@ export const listPersonsTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - const url = new URL(`${baseUrl}/api/projects/${params.projectId}/persons/`) + const url = new URL( + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/persons/` + ) if (params.limit) url.searchParams.append('limit', params.limit.toString()) if (params.offset) url.searchParams.append('offset', params.offset.toString()) diff --git a/apps/sim/tools/posthog/list_property_definitions.ts b/apps/sim/tools/posthog/list_property_definitions.ts index 406d23e8474..c01921a4b86 100644 --- a/apps/sim/tools/posthog/list_property_definitions.ts +++ b/apps/sim/tools/posthog/list_property_definitions.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogListPropertyDefinitionsParams { @@ -113,7 +113,7 @@ export const listPropertyDefinitionsTool: ToolConfig< if (params.type) queryParams.append('type', params.type) const query = queryParams.toString() - return `${baseUrl}/api/projects/${params.projectId}/property_definitions/${query ? `?${query}` : ''}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/property_definitions/${query ? `?${query}` : ''}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/list_recording_playlists.ts b/apps/sim/tools/posthog/list_recording_playlists.ts index e1addcdb1ba..274cf1196da 100644 --- a/apps/sim/tools/posthog/list_recording_playlists.ts +++ b/apps/sim/tools/posthog/list_recording_playlists.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListRecordingPlaylistsParams { @@ -96,7 +96,7 @@ export const listRecordingPlaylistsTool: ToolConfig< url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) const url = new URL( - `${baseUrl}/api/projects/${params.projectId}/session_recording_playlists/` + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/session_recording_playlists/` ) if (params.limit) { diff --git a/apps/sim/tools/posthog/list_session_recordings.ts b/apps/sim/tools/posthog/list_session_recordings.ts index 0e7eb433a1e..00b83a581b1 100644 --- a/apps/sim/tools/posthog/list_session_recordings.ts +++ b/apps/sim/tools/posthog/list_session_recordings.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListSessionRecordingsParams { @@ -96,7 +96,9 @@ export const listSessionRecordingsTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - const url = new URL(`${baseUrl}/api/projects/${params.projectId}/session_recordings/`) + const url = new URL( + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/session_recordings/` + ) if (params.limit) { url.searchParams.set('limit', params.limit.toString()) diff --git a/apps/sim/tools/posthog/list_surveys.ts b/apps/sim/tools/posthog/list_surveys.ts index 4ea049f0b60..5217d219f73 100644 --- a/apps/sim/tools/posthog/list_surveys.ts +++ b/apps/sim/tools/posthog/list_surveys.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogListSurveysParams { @@ -93,7 +93,9 @@ export const listSurveysTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - const url = new URL(`${baseUrl}/api/projects/${params.projectId}/surveys/`) + const url = new URL( + `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/surveys/` + ) if (params.limit) { url.searchParams.set('limit', params.limit.toString()) diff --git a/apps/sim/tools/posthog/posthog.test.ts b/apps/sim/tools/posthog/posthog.test.ts index ad225ba4506..2aa693db3d7 100644 --- a/apps/sim/tools/posthog/posthog.test.ts +++ b/apps/sim/tools/posthog/posthog.test.ts @@ -99,3 +99,27 @@ describe('posthog block rollout mapping', () => { } ) }) + +describe('posthog path ids', () => { + const featureFlagUrl = (projectId: string, flagId: string) => { + const url = posthogTools.posthogGetFeatureFlagTool.request.url + if (typeof url !== 'function') throw new Error('PostHog URL must be built per request') + return url({ projectId, flagId, region: 'us', apiKey: 'test-key' }) + } + + it('keeps the @current project alias literal', () => { + expect(featureFlagUrl('@current', '42')).toBe( + 'https://us.posthog.com/api/projects/@current/feature_flags/42/' + ) + }) + + it('encodes a query character in an id instead of sending it as a query string', () => { + expect(featureFlagUrl('12345', '7?active=false')).toBe( + 'https://us.posthog.com/api/projects/12345/feature_flags/7%3Factive%3Dfalse/' + ) + }) + + it.each(['..', '1/../2'])('refuses the flag id %j', (flagId) => { + expect(() => featureFlagUrl('12345', flagId)).toThrow('flagId') + }) +}) diff --git a/apps/sim/tools/posthog/query.ts b/apps/sim/tools/posthog/query.ts index c78b29320a7..7f01320fb40 100644 --- a/apps/sim/tools/posthog/query.ts +++ b/apps/sim/tools/posthog/query.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' export interface PostHogQueryParams { @@ -75,7 +75,7 @@ export const queryTool: ToolConfig = { request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/query/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/query/` }, method: 'POST', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_cohort.ts b/apps/sim/tools/posthog/update_cohort.ts index d2cd36b220f..4ff67a0fa15 100644 --- a/apps/sim/tools/posthog/update_cohort.ts +++ b/apps/sim/tools/posthog/update_cohort.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogUpdateCohortParams { @@ -124,7 +124,7 @@ export const updateCohortTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/cohorts/${params.cohortId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/cohorts/${postHogPathSegment(params.cohortId, 'cohortId')}/` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_event_definition.ts b/apps/sim/tools/posthog/update_event_definition.ts index 80e9e7f5501..a09a4ef155b 100644 --- a/apps/sim/tools/posthog/update_event_definition.ts +++ b/apps/sim/tools/posthog/update_event_definition.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogUpdateEventDefinitionParams { @@ -98,7 +98,7 @@ export const updateEventDefinitionTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/event_definitions/${params.eventDefinitionId}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/event_definitions/${postHogPathSegment(params.eventDefinitionId, 'eventDefinitionId')}` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_experiment.ts b/apps/sim/tools/posthog/update_experiment.ts index 48709238633..423ff83f348 100644 --- a/apps/sim/tools/posthog/update_experiment.ts +++ b/apps/sim/tools/posthog/update_experiment.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface UpdateExperimentParams { @@ -126,7 +126,7 @@ export const updateExperimentTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/experiments/${params.experimentId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/experiments/${postHogPathSegment(params.experimentId, 'experimentId')}/` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_feature_flag.ts b/apps/sim/tools/posthog/update_feature_flag.ts index 1775abe275f..19f73ede761 100644 --- a/apps/sim/tools/posthog/update_feature_flag.ts +++ b/apps/sim/tools/posthog/update_feature_flag.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface UpdateFeatureFlagParams { @@ -117,7 +117,7 @@ export const updateFeatureFlagTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/feature_flags/${params.flagId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/feature_flags/${postHogPathSegment(params.flagId, 'flagId')}/` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_insight.ts b/apps/sim/tools/posthog/update_insight.ts index f08e9af2424..445ba9138fa 100644 --- a/apps/sim/tools/posthog/update_insight.ts +++ b/apps/sim/tools/posthog/update_insight.ts @@ -1,5 +1,5 @@ import { getErrorMessage } from '@sim/utils/errors' -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogUpdateInsightParams { @@ -116,7 +116,7 @@ export const updateInsightTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/insights/${params.insightId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/insights/${postHogPathSegment(params.insightId, 'insightId')}/` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_property_definition.ts b/apps/sim/tools/posthog/update_property_definition.ts index 3be008a1579..d30b7839da8 100644 --- a/apps/sim/tools/posthog/update_property_definition.ts +++ b/apps/sim/tools/posthog/update_property_definition.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig, ToolResponse } from '@/tools/types' interface PostHogUpdatePropertyDefinitionParams { @@ -108,7 +108,7 @@ export const updatePropertyDefinitionTool: ToolConfig< request: { url: (params) => { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/property_definitions/${params.propertyDefinitionId}` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/property_definitions/${postHogPathSegment(params.propertyDefinitionId, 'propertyDefinitionId')}` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/update_survey.ts b/apps/sim/tools/posthog/update_survey.ts index 7b419a21f4d..e728142aa2a 100644 --- a/apps/sim/tools/posthog/update_survey.ts +++ b/apps/sim/tools/posthog/update_survey.ts @@ -1,4 +1,4 @@ -import { getPostHogAppBaseUrl } from '@/tools/posthog/utils' +import { getPostHogAppBaseUrl, postHogPathSegment } from '@/tools/posthog/utils' import type { ToolConfig } from '@/tools/types' interface PostHogSurveyQuestion { @@ -177,7 +177,7 @@ export const updateSurveyTool: ToolConfig { const baseUrl = getPostHogAppBaseUrl(params.region, params.host) - return `${baseUrl}/api/projects/${params.projectId}/surveys/${params.surveyId}/` + return `${baseUrl}/api/projects/${postHogPathSegment(params.projectId, 'projectId')}/surveys/${postHogPathSegment(params.surveyId, 'surveyId')}/` }, method: 'PATCH', headers: (params) => ({ diff --git a/apps/sim/tools/posthog/utils.ts b/apps/sim/tools/posthog/utils.ts index fde423d8699..cbf13eb8856 100644 --- a/apps/sim/tools/posthog/utils.ts +++ b/apps/sim/tools/posthog/utils.ts @@ -1,4 +1,5 @@ import { validateExternalUrl } from '@/lib/core/security/input-validation' +import { safeUrlPathSegment } from '@/tools/url-path' /** * Shared PostHog base URL resolution. @@ -38,3 +39,11 @@ function normalizeHost(host: string): string { } return withProtocol } + +/** + * Encodes one ID path segment of a PostHog REST URL, keeping `@` literal so the `@current` + * aliases send the same URL as before. Tabs and newlines are stripped, as the URL parser did. + */ +export function postHogPathSegment(value: string, paramName: string): string { + return safeUrlPathSegment(value, paramName, { stripTabsAndNewlines: true }).replaceAll('%40', '@') +} diff --git a/apps/sim/tools/zep/add_messages.ts b/apps/sim/tools/zep/add_messages.ts index a2b7c2fb0e7..ae4b76c7789 100644 --- a/apps/sim/tools/zep/add_messages.ts +++ b/apps/sim/tools/zep/add_messages.ts @@ -1,8 +1,8 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' import { THREAD_OUTPUT_PROPERTIES } from '@/tools/zep/types' +import { zepPathSegment } from '@/tools/zep/utils' interface ZepAddMessagesParams { threadId: string @@ -102,11 +102,7 @@ export const zepAddMessagesTool: ToolConfig = }, }, url: (params) => - `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - })}/messages`, + `https://api.getzep.com/api/v2/threads/${zepPathSegment(params.threadId, 'threadId')}/messages`, method: 'POST', headers: (params) => ({ Authorization: `Api-Key ${params.apiKey}`, diff --git a/apps/sim/tools/zep/delete_thread.ts b/apps/sim/tools/zep/delete_thread.ts index 62bd7fe665f..246ce16ef67 100644 --- a/apps/sim/tools/zep/delete_thread.ts +++ b/apps/sim/tools/zep/delete_thread.ts @@ -1,7 +1,7 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' +import { zepFinalPathSegment } from '@/tools/zep/utils' export const zepDeleteThreadTool: ToolConfig = { id: 'zep_delete_thread', @@ -27,11 +27,7 @@ export const zepDeleteThreadTool: ToolConfig = { request: { url: (params) => - `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - })}`, + `https://api.getzep.com/api/v2/threads/${zepFinalPathSegment(params.threadId, 'threadId')}`, method: 'DELETE', headers: (params) => ({ Authorization: `Api-Key ${params.apiKey}`, diff --git a/apps/sim/tools/zep/get_context.ts b/apps/sim/tools/zep/get_context.ts index 13c1052c37c..aa5e8373f4b 100644 --- a/apps/sim/tools/zep/get_context.ts +++ b/apps/sim/tools/zep/get_context.ts @@ -1,7 +1,7 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' +import { zepPathSegment } from '@/tools/zep/utils' export const zepGetContextTool: ToolConfig = { id: 'zep_get_context', @@ -45,15 +45,7 @@ export const zepGetContextTool: ToolConfig = { queryParams.append('mode', mode) if (params.minRating !== undefined) queryParams.append('minRating', String(Number(params.minRating))) - return `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment( - params.threadId, - 'threadId', - { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - } - )}/context?${queryParams.toString()}` + return `https://api.getzep.com/api/v2/threads/${zepPathSegment(params.threadId, 'threadId')}/context?${queryParams.toString()}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/zep/get_messages.ts b/apps/sim/tools/zep/get_messages.ts index e337d5db589..f5feb5d6e8a 100644 --- a/apps/sim/tools/zep/get_messages.ts +++ b/apps/sim/tools/zep/get_messages.ts @@ -1,8 +1,8 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' import { MESSAGES_ARRAY_OUTPUT, PAGINATION_OUTPUT_PROPERTIES } from '@/tools/zep/types' +import { zepPathSegment } from '@/tools/zep/utils' export const zepGetMessagesTool: ToolConfig = { id: 'zep_get_messages', @@ -52,15 +52,7 @@ export const zepGetMessagesTool: ToolConfig = { if (params.lastn) queryParams.append('lastn', String(Number(params.lastn))) const queryString = queryParams.toString() - return `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment( - params.threadId, - 'threadId', - { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - } - )}/messages${queryString ? `?${queryString}` : ''}` + return `https://api.getzep.com/api/v2/threads/${zepPathSegment(params.threadId, 'threadId')}/messages${queryString ? `?${queryString}` : ''}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/zep/get_user.ts b/apps/sim/tools/zep/get_user.ts index d0c88f8d980..7186ff2526d 100644 --- a/apps/sim/tools/zep/get_user.ts +++ b/apps/sim/tools/zep/get_user.ts @@ -1,8 +1,8 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' import { USER_OUTPUT_PROPERTIES } from '@/tools/zep/types' +import { zepFinalPathSegment } from '@/tools/zep/utils' export const zepGetUserTool: ToolConfig = { id: 'zep_get_user', @@ -28,11 +28,7 @@ export const zepGetUserTool: ToolConfig = { request: { url: (params) => - `https://api.getzep.com/api/v2/users/${safeUrlPathSegment(params.userId, 'userId', { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - })}`, + `https://api.getzep.com/api/v2/users/${zepFinalPathSegment(params.userId, 'userId')}`, method: 'GET', headers: (params) => ({ Authorization: `Api-Key ${params.apiKey}`, diff --git a/apps/sim/tools/zep/get_user_threads.ts b/apps/sim/tools/zep/get_user_threads.ts index d0ecd9c3568..36b0f548eef 100644 --- a/apps/sim/tools/zep/get_user_threads.ts +++ b/apps/sim/tools/zep/get_user_threads.ts @@ -1,8 +1,8 @@ import { ZEP_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' -import { safeUrlPathSegment } from '@/tools/url-path' import type { ZepResponse } from '@/tools/zep/types' import { PAGINATION_OUTPUT_PROPERTIES, THREADS_ARRAY_OUTPUT } from '@/tools/zep/types' +import { zepPathSegment } from '@/tools/zep/utils' export const zepGetUserThreadsTool: ToolConfig = { id: 'zep_get_user_threads', @@ -36,11 +36,7 @@ export const zepGetUserThreadsTool: ToolConfig = { request: { url: (params) => { const limit = Number(params.limit || 10) - return `https://api.getzep.com/api/v2/users/${safeUrlPathSegment(params.userId, 'userId', { - allowSeparators: true, - preserveWhitespace: true, - stripTabsAndNewlines: true, - })}/threads?limit=${limit}` + return `https://api.getzep.com/api/v2/users/${zepPathSegment(params.userId, 'userId')}/threads?limit=${limit}` }, method: 'GET', headers: (params) => ({ diff --git a/apps/sim/tools/zep/utils.ts b/apps/sim/tools/zep/utils.ts new file mode 100644 index 00000000000..bc4e62e4992 --- /dev/null +++ b/apps/sim/tools/zep/utils.ts @@ -0,0 +1,25 @@ +import { safeUrlPathSegment } from '@/tools/url-path' + +/** Spaces and C0 controls the URL parser trims from the end of a whole URL. */ +const URL_TRAILING_TRIM = /[\u0000- ]+$/ + +/** + * Encodes a Zep id used mid-path. Ids are opaque, so separators and surrounding whitespace are + * encoded as given; tabs and newlines are dropped as the URL parser dropped them from raw ids. + */ +export function zepPathSegment(value: string | number, paramName: string): string { + return safeUrlPathSegment(value, paramName, { + allowSeparators: true, + preserveWhitespace: true, + stripTabsAndNewlines: true, + }) +} + +/** + * Encodes a Zep id that is the last component of the request URL. When these ids were sent raw, + * the URL parser trimmed their trailing spaces and control characters, so those are trimmed here. + */ +export function zepFinalPathSegment(value: string | number, paramName: string): string { + const id = typeof value === 'string' ? value.replace(URL_TRAILING_TRIM, '') : value + return zepPathSegment(id, paramName) +} diff --git a/apps/sim/tools/zep/zep.test.ts b/apps/sim/tools/zep/zep.test.ts new file mode 100644 index 00000000000..7ef024dfd36 --- /dev/null +++ b/apps/sim/tools/zep/zep.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' +import { zepDeleteThreadTool } from '@/tools/zep/delete_thread' +import { zepGetUserTool } from '@/tools/zep/get_user' + +const API = 'https://api.getzep.com/api/v2' + +function requestUrl(tool: typeof zepDeleteThreadTool, params: Record): string { + const url = tool.request.url + if (typeof url !== 'function') throw new Error('Zep URL must be built per request') + return url(params) +} + +describe('zep ids at the end of the URL', () => { + it.each([ + ['thread-1 ', `${API}/threads/thread-1`], + ['thread 1 \u0001', `${API}/threads/thread%201`], + ])('deletes thread %j at the URL a raw id resolved to', (threadId, expected) => { + expect(requestUrl(zepDeleteThreadTool, { threadId, apiKey: 'k' })).toBe(expected) + }) + + it('gets the user at the URL a raw id resolved to', () => { + expect(requestUrl(zepGetUserTool, { userId: 'user-1 ', apiKey: 'k' })).toBe( + `${API}/users/user-1` + ) + }) +})