diff --git a/apps/sim/tools/elasticsearch/bulk.ts b/apps/sim/tools/elasticsearch/bulk.ts index 040f7625f0e..c4835ee79fe 100644 --- a/apps/sim/tools/elasticsearch/bulk.ts +++ b/apps/sim/tools/elasticsearch/bulk.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const bulkTool: ToolConfig = { id: 'elasticsearch_bulk', @@ -77,7 +78,7 @@ export const bulkTool: ToolConfig { const baseUrl = buildBaseUrl(params) let url = params.index - ? `${baseUrl}/${encodeURIComponent(params.index)}/_bulk` + ? `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_bulk` : `${baseUrl}/_bulk` if (params.refresh) { diff --git a/apps/sim/tools/elasticsearch/count.ts b/apps/sim/tools/elasticsearch/count.ts index 19a555f12bd..7bd841bb925 100644 --- a/apps/sim/tools/elasticsearch/count.ts +++ b/apps/sim/tools/elasticsearch/count.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const countTool: ToolConfig = { id: 'elasticsearch_count', @@ -70,7 +71,7 @@ export const countTool: ToolConfig { const baseUrl = buildBaseUrl(params) - return `${baseUrl}/${encodeURIComponent(params.index)}/_count` + return `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_count` }, method: 'POST', headers: (params) => buildAuthHeaders(params), diff --git a/apps/sim/tools/elasticsearch/create_index.ts b/apps/sim/tools/elasticsearch/create_index.ts index 44a3c97f6a0..b2b12282c35 100644 --- a/apps/sim/tools/elasticsearch/create_index.ts +++ b/apps/sim/tools/elasticsearch/create_index.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const createIndexTool: ToolConfig< ElasticsearchCreateIndexParams, @@ -76,7 +77,7 @@ export const createIndexTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - return `${baseUrl}/${encodeURIComponent(params.index)}` + return `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}` }, method: 'PUT', headers: (params) => buildAuthHeaders(params), diff --git a/apps/sim/tools/elasticsearch/delete_document.ts b/apps/sim/tools/elasticsearch/delete_document.ts index 0572fc714fa..272225f9fc8 100644 --- a/apps/sim/tools/elasticsearch/delete_document.ts +++ b/apps/sim/tools/elasticsearch/delete_document.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const deleteDocumentTool: ToolConfig< ElasticsearchDeleteDocumentParams, @@ -77,7 +78,14 @@ export const deleteDocumentTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - let url = `${baseUrl}/${encodeURIComponent(params.index)}/_doc/${encodeURIComponent(params.documentId)}` + let url = `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_doc/${safeUrlPathSegment( + params.documentId, + 'documentId', + { + allowSeparators: true, + preserveWhitespace: true, + } + )}` if (params.refresh) { url += `?refresh=${params.refresh}` diff --git a/apps/sim/tools/elasticsearch/delete_index.ts b/apps/sim/tools/elasticsearch/delete_index.ts index 62154f1df60..eff20d97ac5 100644 --- a/apps/sim/tools/elasticsearch/delete_index.ts +++ b/apps/sim/tools/elasticsearch/delete_index.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const deleteIndexTool: ToolConfig< ElasticsearchDeleteIndexParams, @@ -66,7 +67,7 @@ export const deleteIndexTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - return `${baseUrl}/${encodeURIComponent(params.index)}` + return `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}` }, method: 'DELETE', headers: (params) => buildAuthHeaders(params), diff --git a/apps/sim/tools/elasticsearch/get_document.ts b/apps/sim/tools/elasticsearch/get_document.ts index 6b62b51fd8b..04ec60f9538 100644 --- a/apps/sim/tools/elasticsearch/get_document.ts +++ b/apps/sim/tools/elasticsearch/get_document.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const getDocumentTool: ToolConfig< ElasticsearchGetDocumentParams, @@ -82,7 +83,14 @@ export const getDocumentTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - let url = `${baseUrl}/${encodeURIComponent(params.index)}/_doc/${encodeURIComponent(params.documentId)}` + let url = `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_doc/${safeUrlPathSegment( + params.documentId, + 'documentId', + { + allowSeparators: true, + preserveWhitespace: true, + } + )}` const queryParams: string[] = [] if (params.sourceIncludes) { diff --git a/apps/sim/tools/elasticsearch/get_index.ts b/apps/sim/tools/elasticsearch/get_index.ts index 4b6ea6a0fca..4e84cf2c0e1 100644 --- a/apps/sim/tools/elasticsearch/get_index.ts +++ b/apps/sim/tools/elasticsearch/get_index.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const getIndexTool: ToolConfig = { @@ -64,7 +65,7 @@ export const getIndexTool: ToolConfig { const baseUrl = buildBaseUrl(params) - return `${baseUrl}/${encodeURIComponent(params.index)}` + return `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}` }, method: 'GET', headers: (params) => buildAuthHeaders(params), diff --git a/apps/sim/tools/elasticsearch/index_document.ts b/apps/sim/tools/elasticsearch/index_document.ts index c7f9b18067d..04ac52ccb65 100644 --- a/apps/sim/tools/elasticsearch/index_document.ts +++ b/apps/sim/tools/elasticsearch/index_document.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const indexDocumentTool: ToolConfig< ElasticsearchIndexDocumentParams, @@ -82,9 +83,12 @@ export const indexDocumentTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - let url = `${baseUrl}/${encodeURIComponent(params.index)}/_doc` + let url = `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_doc` if (params.documentId) { - url += `/${encodeURIComponent(params.documentId)}` + url += `/${safeUrlPathSegment(params.documentId, 'documentId', { + allowSeparators: true, + preserveWhitespace: true, + })}` } if (params.refresh) { url += `?refresh=${params.refresh}` diff --git a/apps/sim/tools/elasticsearch/search.ts b/apps/sim/tools/elasticsearch/search.ts index 9419c6a6152..d9c960f0bf9 100644 --- a/apps/sim/tools/elasticsearch/search.ts +++ b/apps/sim/tools/elasticsearch/search.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const searchTool: ToolConfig = { id: 'elasticsearch_search', @@ -105,7 +106,7 @@ export const searchTool: ToolConfig { const baseUrl = buildBaseUrl(params) - return `${baseUrl}/${encodeURIComponent(params.index)}/_search` + return `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_search` }, method: 'POST', headers: (params) => buildAuthHeaders(params), diff --git a/apps/sim/tools/elasticsearch/update_document.ts b/apps/sim/tools/elasticsearch/update_document.ts index 5d90adeb996..9b71504de5f 100644 --- a/apps/sim/tools/elasticsearch/update_document.ts +++ b/apps/sim/tools/elasticsearch/update_document.ts @@ -4,6 +4,7 @@ import type { } from '@/tools/elasticsearch/types' import { buildAuthHeaders, buildBaseUrl } from '@/tools/elasticsearch/utils' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const updateDocumentTool: ToolConfig< ElasticsearchUpdateDocumentParams, @@ -82,7 +83,14 @@ export const updateDocumentTool: ToolConfig< request: { url: (params) => { const baseUrl = buildBaseUrl(params) - let url = `${baseUrl}/${encodeURIComponent(params.index)}/_update/${encodeURIComponent(params.documentId)}` + let url = `${baseUrl}/${safeUrlPathSegment(params.index, 'index')}/_update/${safeUrlPathSegment( + params.documentId, + 'documentId', + { + allowSeparators: true, + preserveWhitespace: true, + } + )}` if (params.retryOnConflict !== undefined) { url += `?retry_on_conflict=${params.retryOnConflict}` diff --git a/apps/sim/tools/nested-model-input-adapters.test.ts b/apps/sim/tools/nested-model-input-adapters.test.ts index b544d5a31cc..3a19ba69695 100644 --- a/apps/sim/tools/nested-model-input-adapters.test.ts +++ b/apps/sim/tools/nested-model-input-adapters.test.ts @@ -374,8 +374,9 @@ describe('nested model-input adapters', () => { apiKey: 'key', rerank, searchQuery: patch.searchQuery as string, - }) as { rerank: { query: { text: string } } } - expect(body.rerank.query.text).toBe('{{SEARCH_QUERY}}') + }) as { query: { inputs: { text: string } }; rerank: { query?: string } } + expect(body.query.inputs.text).toBe('{{SEARCH_QUERY}}') + expect(body.rerank.query).toBeUndefined() }) it('preserves Pinecone singleton and legacy NDJSON input forms', () => { diff --git a/apps/sim/tools/pinecone/fetch.ts b/apps/sim/tools/pinecone/fetch.ts index 8425e4780de..1179fefafb5 100644 --- a/apps/sim/tools/pinecone/fetch.ts +++ b/apps/sim/tools/pinecone/fetch.ts @@ -1,4 +1,5 @@ import type { PineconeFetchParams, PineconeResponse, PineconeVector } from '@/tools/pinecone/types' +import { parseJsonParam } from '@/tools/pinecone/utils' import type { ToolConfig } from '@/tools/types' export const fetchTool: ToolConfig = { @@ -39,7 +40,10 @@ export const fetchTool: ToolConfig = { url: (params) => { const baseUrl = `${params.indexHost}/vectors/fetch` const queryParams = new URLSearchParams() - queryParams.append('ids', params.ids.join(',')) + const ids = parseJsonParam(params.ids, 'ids') + for (const id of Array.isArray(ids) ? ids : [ids]) { + queryParams.append('ids', String(id)) + } if (params.namespace) { queryParams.append('namespace', params.namespace) } diff --git a/apps/sim/tools/pinecone/requests.test.ts b/apps/sim/tools/pinecone/requests.test.ts new file mode 100644 index 00000000000..99eb37f2b31 --- /dev/null +++ b/apps/sim/tools/pinecone/requests.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from 'vitest' +import { fetchTool } from '@/tools/pinecone/fetch' +import { searchTextTool } from '@/tools/pinecone/search_text' +import type { PineconeFetchParams, PineconeSearchTextParams } from '@/tools/pinecone/types' + +const INDEX_HOST = 'https://example-index.svc.example.pinecone.io' + +function buildUrl

(tool: { request: { url: unknown } }, params: P): URL { + const url = tool.request.url as (params: P) => string + return new URL(url(params)) +} + +describe('pinecone_fetch request', () => { + it('repeats the ids query parameter once per vector id', () => { + const url = buildUrl(fetchTool, { + indexHost: INDEX_HOST, + ids: ['vec-1', 'vec-2'], + namespace: 'docs', + apiKey: 'key', + }) + + expect(url.searchParams.getAll('ids')).toEqual(['vec-1', 'vec-2']) + }) +}) + +describe('pinecone_search_text request', () => { + const baseParams: PineconeSearchTextParams = { + indexHost: INDEX_HOST, + searchQuery: 'capital of France', + apiKey: 'key', + } + + it('rejects a missing namespace instead of searching a namespace named "undefined"', () => { + expect(() => buildUrl(searchTextTool, baseParams)).toThrow( + /namespace is required/ + ) + }) + + it('sends the rerank query as a string', () => { + const body = searchTextTool.request.body as ( + params: PineconeSearchTextParams + ) => Record> + + const withoutQuery = body({ + ...baseParams, + rerank: { model: 'bge-reranker-v2-m3', rank_fields: ['chunk_text'] }, + }) + expect(withoutQuery.rerank.query).toBeUndefined() + + const withObjectQuery = body({ + ...baseParams, + rerank: { model: 'bge-reranker-v2-m3', query: { text: 'Paris' } }, + }) + expect(withObjectQuery.rerank.query).toBe('Paris') + + const withBlankQuery = body({ + ...baseParams, + rerank: { model: 'bge-reranker-v2-m3', query: ' ' }, + }) + expect(withBlankQuery.rerank.query).toBeUndefined() + }) +}) diff --git a/apps/sim/tools/pinecone/search_text.ts b/apps/sim/tools/pinecone/search_text.ts index c102da2ea21..39fc5eb739d 100644 --- a/apps/sim/tools/pinecone/search_text.ts +++ b/apps/sim/tools/pinecone/search_text.ts @@ -4,6 +4,7 @@ import type { PineconeSearchTextParams, } from '@/tools/pinecone/types' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' function parseRerank( value: PineconeSearchTextParams['rerank'] @@ -22,6 +23,7 @@ function selectRerankModelInput( const rerank = parseRerank(value) if (!rerank) return undefined const query = rerank.query + if (typeof query === 'string') return { query } if ( query === null || typeof query !== 'object' || @@ -132,7 +134,8 @@ export const searchTextTool: ToolConfig `${params.indexHost}/records/namespaces/${params.namespace}/search`, + url: (params) => + `${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace ?? '', 'namespace', { allowSeparators: true, preserveWhitespace: true })}/search`, headers: (params) => ({ 'Api-Key': params.apiKey, 'Content-Type': 'application/json', @@ -162,11 +165,16 @@ export const searchTextTool: ToolConfig).text + : rerankQuery + // Pinecone takes `rerank.query` as a string and reranks against the search query when unset. + body.rerank = + typeof queryText === 'string' && queryText.trim() + ? { ...rerank, query: queryText } + : rerank } return body diff --git a/apps/sim/tools/pinecone/types.ts b/apps/sim/tools/pinecone/types.ts index ef911c9cc2a..0b023a9053b 100644 --- a/apps/sim/tools/pinecone/types.ts +++ b/apps/sim/tools/pinecone/types.ts @@ -81,13 +81,14 @@ export interface PineconeUpsertTextParams extends PineconeBaseParams { interface PineconeRerank { model: string - rank_fields: string[] + rank_fields?: string[] top_n?: number parameters?: Record - query?: { text: string } + query?: string | { text: string } } -export interface PineconeSearchTextParams extends PineconeBaseParams { +export interface PineconeSearchTextParams extends Omit { + namespace?: string searchQuery: string topK?: string fields?: string[] | string diff --git a/apps/sim/tools/pinecone/upsert_text.ts b/apps/sim/tools/pinecone/upsert_text.ts index 62218eb572a..c4f2122f593 100644 --- a/apps/sim/tools/pinecone/upsert_text.ts +++ b/apps/sim/tools/pinecone/upsert_text.ts @@ -4,6 +4,7 @@ import type { PineconeUpsertTextRecord, } from '@/tools/pinecone/types' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' function parseUpsertTextRecords( input: PineconeUpsertTextParams['records'] @@ -90,7 +91,8 @@ export const upsertTextTool: ToolConfig `${params.indexHost}/records/namespaces/${params.namespace}/upsert`, + url: (params) => + `${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace, 'namespace', { allowSeparators: true, preserveWhitespace: true })}/upsert`, headers: (params) => ({ 'Api-Key': params.apiKey, 'Content-Type': 'application/x-ndjson', diff --git a/apps/sim/tools/qdrant/fetch_points.ts b/apps/sim/tools/qdrant/fetch_points.ts index 5350755f34e..d7de769809b 100644 --- a/apps/sim/tools/qdrant/fetch_points.ts +++ b/apps/sim/tools/qdrant/fetch_points.ts @@ -2,6 +2,7 @@ import type { QdrantFetchParams, QdrantResponse } from '@/tools/qdrant/types' import { POINT_OUTPUT_PROPERTIES, QDRANT_RESPONSE_OUTPUT_PROPERTIES } from '@/tools/qdrant/types' import { QDRANT_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const fetchPointsTool: ToolConfig = { id: 'qdrant_fetch_points', @@ -57,7 +58,8 @@ export const fetchPointsTool: ToolConfig = { request: { method: 'POST', - url: (params) => `${params.url.replace(/\/$/, '')}/collections/${params.collection}/points`, + url: (params) => + `${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true })}/points`, headers: (params) => ({ 'Content-Type': 'application/json', ...(params.apiKey ? { 'api-key': params.apiKey } : {}), diff --git a/apps/sim/tools/qdrant/search_vector.ts b/apps/sim/tools/qdrant/search_vector.ts index 35a01061e30..9099c151774 100644 --- a/apps/sim/tools/qdrant/search_vector.ts +++ b/apps/sim/tools/qdrant/search_vector.ts @@ -5,6 +5,7 @@ import { } from '@/tools/qdrant/types' import { QDRANT_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const searchVectorTool: ToolConfig = { id: 'qdrant_search_vector', @@ -74,7 +75,7 @@ export const searchVectorTool: ToolConfig = request: { method: 'POST', url: (params) => - `${params.url.replace(/\/$/, '')}/collections/${encodeURIComponent(params.collection)}/points/query`, + `${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true })}/points/query`, headers: (params) => ({ 'Content-Type': 'application/json', ...(params.apiKey ? { 'api-key': params.apiKey } : {}), diff --git a/apps/sim/tools/qdrant/upsert_points.ts b/apps/sim/tools/qdrant/upsert_points.ts index 10260c90ebc..1a5cef231e8 100644 --- a/apps/sim/tools/qdrant/upsert_points.ts +++ b/apps/sim/tools/qdrant/upsert_points.ts @@ -5,6 +5,7 @@ import { } from '@/tools/qdrant/types' import { QDRANT_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials' import type { ToolConfig } from '@/tools/types' +import { safeUrlPathSegment } from '@/tools/url-path' export const upsertPointsTool: ToolConfig = { id: 'qdrant_upsert_points', @@ -42,7 +43,8 @@ export const upsertPointsTool: ToolConfig = request: { method: 'PUT', - url: (params) => `${params.url.replace(/\/$/, '')}/collections/${params.collection}/points`, + url: (params) => + `${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true })}/points`, headers: (params) => ({ 'Content-Type': 'application/json', ...(params.apiKey ? { 'api-key': params.apiKey } : {}), diff --git a/apps/sim/tools/url-path.test.ts b/apps/sim/tools/url-path.test.ts index 9f81de42b79..92a0ca53530 100644 --- a/apps/sim/tools/url-path.test.ts +++ b/apps/sim/tools/url-path.test.ts @@ -103,6 +103,12 @@ describe('safeUrlPathSegment', () => { expect(safeUrlPathSegment(' users ', 'table')).toBe('users') }) + it.concurrent('keeps surrounding whitespace when asked to preserve it', () => { + expect(safeUrlPathSegment(' doc-1 ', 'id', { preserveWhitespace: true })).toBe('%20doc-1%20') + expect(() => safeUrlPathSegment('..', 'id', { preserveWhitespace: true })).toThrow(/id/) + expect(() => safeUrlPathSegment(' ', 'id', { preserveWhitespace: true })).toThrow(/id/) + }) + it.concurrent('every vector either throws or keeps the exact path shape', () => { for (const value of [...SEGMENT_REJECTED, ...NEUTRALIZED]) { let built: string | null = null diff --git a/apps/sim/tools/url-path.ts b/apps/sim/tools/url-path.ts index 944fe68b487..cc53c978fc3 100644 --- a/apps/sim/tools/url-path.ts +++ b/apps/sim/tools/url-path.ts @@ -168,6 +168,9 @@ function encodeSegment(segment: string, paramName: string): string { * @param options.allowSeparators - Percent-encode `/` and `\` instead of * rejecting them, for opaque ids (such as base64) that may legitimately * contain them. Empty values and dot segments are still rejected. + * @param options.preserveWhitespace - Encode the value as given instead of + * trimming it, for ids where surrounding whitespace is significant (an + * exact-match document id). Blank values and dot segments are still rejected. * @returns The trimmed, percent-encoded segment, safe to interpolate. * @throws If the value is not a string or a usable number, is empty, is a dot * segment, contains a path separator (unless allowed), or cannot be encoded. @@ -175,9 +178,10 @@ function encodeSegment(segment: string, paramName: string): string { export function safeUrlPathSegment( value: string | number | bigint, paramName: string, - options?: { allowSeparators?: boolean } + options?: { allowSeparators?: boolean; preserveWhitespace?: boolean } ): string { - const trimmed = toGuardedString(value, paramName).trim() + const raw = toGuardedString(value, paramName) + const trimmed = raw.trim() if (!trimmed) { throw new Error(`${paramName} is required`) @@ -191,7 +195,7 @@ export function safeUrlPathSegment( throw new Error(`${paramName} cannot contain a path separator`) } - return encodeSegment(trimmed, paramName) + return encodeSegment(options?.preserveWhitespace ? raw : trimmed, paramName) } /** Matches a scheme and authority, which end at the first `/`, `\`, `?`, or `#` in a special-scheme URL. */ diff --git a/apps/sim/tools/zep/add_messages.ts b/apps/sim/tools/zep/add_messages.ts index b8a22cb666b..047fddc8f78 100644 --- a/apps/sim/tools/zep/add_messages.ts +++ b/apps/sim/tools/zep/add_messages.ts @@ -1,5 +1,6 @@ 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' @@ -100,7 +101,11 @@ export const zepAddMessagesTool: ToolConfig = } }, }, - url: (params) => `https://api.getzep.com/api/v2/threads/${params.threadId}/messages`, + url: (params) => + `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', { + allowSeparators: true, + preserveWhitespace: true, + })}/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 24757a56a60..9e20095ccb3 100644 --- a/apps/sim/tools/zep/delete_thread.ts +++ b/apps/sim/tools/zep/delete_thread.ts @@ -1,5 +1,6 @@ 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' export const zepDeleteThreadTool: ToolConfig = { @@ -25,7 +26,11 @@ export const zepDeleteThreadTool: ToolConfig = { }, request: { - url: (params) => `https://api.getzep.com/api/v2/threads/${params.threadId}`, + url: (params) => + `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', { + allowSeparators: true, + preserveWhitespace: true, + })}`, 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 6fae8b3f5d2..5a617f66b98 100644 --- a/apps/sim/tools/zep/get_context.ts +++ b/apps/sim/tools/zep/get_context.ts @@ -1,5 +1,6 @@ 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' export const zepGetContextTool: ToolConfig = { @@ -44,7 +45,14 @@ 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/${params.threadId}/context?${queryParams.toString()}` + return `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment( + params.threadId, + 'threadId', + { + allowSeparators: true, + preserveWhitespace: true, + } + )}/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 d4067dc0be5..26b9af44606 100644 --- a/apps/sim/tools/zep/get_messages.ts +++ b/apps/sim/tools/zep/get_messages.ts @@ -1,5 +1,6 @@ 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' @@ -51,7 +52,14 @@ 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/${params.threadId}/messages${queryString ? `?${queryString}` : ''}` + return `https://api.getzep.com/api/v2/threads/${safeUrlPathSegment( + params.threadId, + 'threadId', + { + allowSeparators: true, + preserveWhitespace: true, + } + )}/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 ee66925c54d..b3f6e20adb6 100644 --- a/apps/sim/tools/zep/get_user.ts +++ b/apps/sim/tools/zep/get_user.ts @@ -1,5 +1,6 @@ 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' @@ -26,7 +27,11 @@ export const zepGetUserTool: ToolConfig = { }, request: { - url: (params) => `https://api.getzep.com/api/v2/users/${params.userId}`, + url: (params) => + `https://api.getzep.com/api/v2/users/${safeUrlPathSegment(params.userId, 'userId', { + allowSeparators: true, + preserveWhitespace: true, + })}`, 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 cc13efc7322..138da91306b 100644 --- a/apps/sim/tools/zep/get_user_threads.ts +++ b/apps/sim/tools/zep/get_user_threads.ts @@ -1,5 +1,6 @@ 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' @@ -35,7 +36,10 @@ export const zepGetUserThreadsTool: ToolConfig = { request: { url: (params) => { const limit = Number(params.limit || 10) - return `https://api.getzep.com/api/v2/users/${params.userId}/threads?limit=${limit}` + return `https://api.getzep.com/api/v2/users/${safeUrlPathSegment(params.userId, 'userId', { + allowSeparators: true, + preserveWhitespace: true, + })}/threads?limit=${limit}` }, method: 'GET', headers: (params) => ({