Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/docs/content/docs/integrations/kalshi.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ Cancel an existing order on Kalshi
| --------- | ---- | -------- | ----------- |
| `orderId` | string | Yes | Order ID to cancel \(e.g., "abc123-def456-ghi789"\) |
| `ticker` | string | No | Market ticker of the order \(e.g., "KXBTC-24DEC31"\), so the cancel reaches the order's exchange shard. Leave empty to look it up from the order first |
| `subaccount` | string | No | Subaccount the order belongs to \(0 for primary, 1-63 for numbered subaccounts\). Leave empty for the primary account |

#### Output

Expand Down Expand Up @@ -858,6 +859,7 @@ Modify the price or quantity of an existing order on Kalshi
| `yesPriceDollars` | string | No | Updated yes price in dollars \(e.g., "0.56"\) |
| `noPriceDollars` | string | No | Updated no price in dollars \(e.g., "0.56"\) |
| `countFp` | string | No | Count in fixed-point for fractional contracts |
| `subaccount` | string | No | Subaccount the order belongs to \(0 for primary, 1-63 for numbered subaccounts\). Leave empty for the primary account |

#### Output

Expand Down
20 changes: 18 additions & 2 deletions apps/sim/lib/catalog/projection/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { HostedApiKeySupport } from '@/tools/hosted-api-key'
import { getToolMetadata, type ToolMetadata } from '@/tools/metadata'
import { getToolOutputsMetadata } from '@/tools/metadata-outputs'
import { resolveToolId } from '@/tools/tool-ids'
import type { ToolConfig } from '@/tools/types'
import type { OutputProperty, ToolConfig } from '@/tools/types'

/**
* Surface-neutral projection of a built-in tool.
Expand Down Expand Up @@ -141,11 +141,27 @@ export function projectToolOutputs(
const projected: Record<string, CatalogToolOutput> = {}
for (const [id, output] of Object.entries(outputs ?? {})) {
if (!output || isHiddenFromDisplay(output)) continue
projected[id] = output as CatalogToolOutput
projected[id] = publicOutput(output)
}
return projected
}

/** A declared output in its public shape, without the internal `open` marker at any depth. */
function publicOutput(output: OutputProperty): CatalogToolOutput {
const { open, properties, items, ...rest } = output
const projected: CatalogToolOutput = { ...rest }
if (properties) projected.properties = publicOutputs(properties)
if (items) {
const { open: itemOpen, properties: itemProperties, ...item } = items
projected.items = itemProperties ? { ...item, properties: publicOutputs(itemProperties) } : item
}
return projected
}

function publicOutputs(outputs: Record<string, OutputProperty>): Record<string, CatalogToolOutput> {
return Object.fromEntries(Object.entries(outputs).map(([id, node]) => [id, publicOutput(node)]))
}

/**
* Projects a tool to its full catalog entry, or `undefined` when no such tool
* exists.
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/tools/arxiv/get_paper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function arxivPaperId(input: string): string {
const linkPath = arxivLinkPath(
SCHEMELESS_ARXIV_LINK.test(trimmed) ? `https://${trimmed}` : trimmed
)
const id = linkPath ?? trimmed.replace(/[?#].*$/, '')
const id = linkPath ?? trimmed.split(/[?#]/, 1)[0]
return id.replace(/\.pdf$/, '')
}

Expand Down
23 changes: 15 additions & 8 deletions apps/sim/tools/firecrawl/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export const agentTool: ToolConfig<AgentParams, AgentResponse> = {
transformResponse: async (response: Response) => {
const data = await response.json()

if (!data.id) {
throw new Error(data.error || 'Firecrawl did not return an agent job id to poll')
}

return {
success: true,
output: {
Expand All @@ -123,14 +127,17 @@ export const agentTool: ToolConfig<AgentParams, AgentResponse> = {
while (elapsedTime < MAX_POLL_TIME_MS) {
try {
signal?.throwIfAborted()
const statusResponse = await outboundFetch(`https://api.firecrawl.dev/v2/agent/${jobId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${params.apiKey}`,
'Content-Type': 'application/json',
},
signal,
})
const statusResponse = await outboundFetch(
`https://api.firecrawl.dev/v2/agent/${encodeURIComponent(jobId)}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${params.apiKey}`,
'Content-Type': 'application/json',
},
signal,
}
)

if (!statusResponse.ok) {
throw new Error(`Failed to get agent status: ${statusResponse.statusText}`)
Expand Down
9 changes: 8 additions & 1 deletion apps/sim/tools/firecrawl/batch-scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ export const batchScrapeTool: ToolConfig<FirecrawlBatchScrapeParams, FirecrawlBa
}

const jobId = result.output.jobId
if (!jobId) {
return {
...result,
success: false,
error: 'Firecrawl did not return a batch scrape job ID to poll',
}
}
const signal = context?.signal
const invalidURLs = result.output.invalidURLs ?? []
logger.info(`Firecrawl batch scrape job ${jobId} created, polling for completion...`)
Expand All @@ -202,7 +209,7 @@ export const batchScrapeTool: ToolConfig<FirecrawlBatchScrapeParams, FirecrawlBa
try {
signal?.throwIfAborted()
const statusResponse = await outboundFetch(
`https://api.firecrawl.dev/v2/batch/scrape/${jobId}`,
`https://api.firecrawl.dev/v2/batch/scrape/${encodeURIComponent(jobId)}`,
{
method: 'GET',
headers: {
Expand Down
22 changes: 14 additions & 8 deletions apps/sim/tools/firecrawl/crawl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ export const crawlTool: ToolConfig<FirecrawlCrawlParams, FirecrawlCrawlResponse>
}

const jobId = result.output.jobId
if (!jobId) {
return { ...result, success: false, error: 'Firecrawl did not return a crawl job ID to poll' }
}
const signal = context?.signal
logger.info(`Firecrawl crawl job ${jobId} created, polling for completion...`)

Expand All @@ -248,14 +251,17 @@ export const crawlTool: ToolConfig<FirecrawlCrawlParams, FirecrawlCrawlResponse>
while (elapsedTime < MAX_POLL_TIME_MS) {
try {
signal?.throwIfAborted()
const statusResponse = await outboundFetch(`https://api.firecrawl.dev/v2/crawl/${jobId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${params.apiKey}`,
'Content-Type': 'application/json',
},
signal,
})
const statusResponse = await outboundFetch(
`https://api.firecrawl.dev/v2/crawl/${encodeURIComponent(jobId)}`,
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
{
method: 'GET',
headers: {
Authorization: `Bearer ${params.apiKey}`,
'Content-Type': 'application/json',
},
signal,
}
)

if (!statusResponse.ok) {
throw new Error(`Failed to get crawl status: ${statusResponse.statusText}`)
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/tools/firecrawl/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export const extractTool: ToolConfig<ExtractParams, ExtractResponse> = {
transformResponse: async (response: Response) => {
const data = await response.json()

if (!data.id) {
throw new Error(data.error || 'Firecrawl did not return an extract job id to poll')
}

return {
success: true,
output: {
Expand All @@ -177,7 +181,7 @@ export const extractTool: ToolConfig<ExtractParams, ExtractResponse> = {
try {
signal?.throwIfAborted()
const statusResponse = await outboundFetch(
`https://api.firecrawl.dev/v2/extract/${jobId}`,
`https://api.firecrawl.dev/v2/extract/${encodeURIComponent(jobId)}`,
{
method: 'GET',
headers: {
Expand Down
31 changes: 31 additions & 0 deletions apps/sim/tools/firecrawl/job-polling.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { jsonResponse } from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { agentTool } from '@/tools/firecrawl/agent'
import { crawlTool } from '@/tools/firecrawl/crawl'
import { extractTool } from '@/tools/firecrawl/extract'
import type { FirecrawlCrawlResponse } from '@/tools/firecrawl/types'

const fetchMock = vi.fn()
Expand Down Expand Up @@ -51,4 +53,33 @@ describe('Firecrawl crawl polling', () => {
expect.objectContaining({ method: 'DELETE' })
)
})

it('fails at once instead of polling when Firecrawl returned no job ID', async () => {
if (!crawlTool.postProcess) throw new Error('crawlTool has no postProcess')
const result = await crawlTool.postProcess(
{ success: true, output: { pages: [], total: 0 } },
params,
executeTool
)

expect(result).toMatchObject({ success: false, error: expect.stringMatching(/job ID/) })
expect(fetchMock).not.toHaveBeenCalled()
})
})

describe('Firecrawl agent and extract job start', () => {
it.each([
['agent', agentTool],
['extract', extractTool],
] as const)(
'%s refuses a start response with no job id instead of polling it',
async (_, tool) => {
await expect(tool.transformResponse?.(jsonResponse({ success: true }))).rejects.toThrow(
/did not return an? \w+ job id/
)
await expect(
tool.transformResponse?.(jsonResponse({ success: false, error: 'Invalid schema' }))
).rejects.toThrow('Invalid schema')
}
)
})
2 changes: 1 addition & 1 deletion apps/sim/tools/generated/tool-metadata.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/sim/tools/generated/tool-outputs.ts

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions apps/sim/tools/kalshi/amend_order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ describe('Kalshi amend order on the V2 event-order endpoint', () => {
})
})

it('sends the subaccount on the amend, not on the order read', () => {
const subaccountOf = (
tool: typeof kalshiAmendOrderV2Tool | typeof kalshiAmendOrderTool,
params: KalshiAmendOrderV2Params
) => {
const { url } = tool.request
if (typeof url !== 'function') throw new Error('amend tool has no url builder')
return new URL(url(params)).searchParams.get('subaccount')
}
for (const tool of [kalshiAmendOrderV2Tool, kalshiAmendOrderTool]) {
expect(subaccountOf(tool, { ...BASE, count: '8', yesPrice: '57', subaccount: '3' })).toBe('3')
expect(subaccountOf(tool, { ...BASE, count: '8', subaccount: '3' })).toBeNull()
expect(subaccountOf(tool, { ...BASE, count: '8', yesPrice: '57' })).toBeNull()
expect(
sentRequest(tool, { ...BASE, count: '8', yesPrice: '57', subaccount: '3' }).signatureValid
).toBe(true)
}
})

it('quotes a no-side amend on the YES book', () => {
const request = sentRequest(kalshiAmendOrderTool, {
...BASE,
Expand Down Expand Up @@ -206,6 +225,26 @@ describe('Kalshi amend order on the V2 event-order endpoint', () => {
})
})

it('keeps the subaccount on the amend it sends after the order read', async () => {
const params = { ...BASE, count: '10', subaccount: '3' }
const read = await kalshiAmendOrderV2Tool.transformResponse?.(
Response.json({ order: CURRENT_ORDER }),
params
)
const postProcess = kalshiAmendOrderV2Tool.postProcess
const { url } = kalshiAmendOrderV2Tool.request
if (!read || !postProcess || typeof url !== 'function')
throw new Error('amend tool cannot read')
const forwarded: Array<string | null> = []
await postProcess(read, params, async (_toolId, next) => {
forwarded.push(
new URL(url(next as unknown as KalshiAmendOrderV2Params)).searchParams.get('subaccount')
)
return { success: true, output: {} }
})
expect(forwarded).toEqual(['3'])
})

it("reads Kalshi's 6-decimal response values when the extra digits are zeros", async () => {
const params = { ...BASE, count: '10' }
const read = await kalshiAmendOrderV2Tool.transformResponse?.(
Expand Down
20 changes: 18 additions & 2 deletions apps/sim/tools/kalshi/amend_order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { getErrorMessage } from '@sim/utils/errors'
import { toRecord } from '@sim/utils/object'
import {
buildKalshiAmendOrderBody,
KALSHI_ORDER_SUBACCOUNT_PARAM,
type KalshiLegacyOrderParams,
kalshiAmendNeedsCurrentOrder,
kalshiAmendParamsFromOrder,
kalshiCountNumber,
kalshiLegacyOrder,
kalshiOrderPath,
kalshiOrderReadPath,
kalshiOrderWriteUrl,
kalshiWrittenOrder,
} from '@/tools/kalshi/orders'
import type { KalshiAuthParams } from '@/tools/kalshi/types'
Expand All @@ -29,6 +31,16 @@ function amendRequestPath(
: kalshiOrderPath(params.orderId, 'amend')
}

/** The read takes no subaccount (GetOrder has no such param); the amend does. */
function amendRequestUrl(
params: KalshiAuthParams & KalshiLegacyOrderParams & { orderId: string }
): string {
const path = amendRequestPath(params)
return kalshiAmendNeedsCurrentOrder(params)
? buildKalshiUrl(path)
: kalshiOrderWriteUrl(path, params.subaccount)
}

function amendRequestMethod(params: KalshiLegacyOrderParams): 'GET' | 'POST' {
return kalshiAmendNeedsCurrentOrder(params) ? 'GET' : 'POST'
}
Expand Down Expand Up @@ -83,6 +95,7 @@ export interface KalshiAmendOrderParams extends KalshiAuthParams {
noPrice?: string // Updated no price in cents (1-99)
yesPriceDollars?: string // Updated yes price in dollars
noPriceDollars?: string // Updated no price in dollars
subaccount?: string // Subaccount the order belongs to
}

export interface KalshiAmendOrderResponse {
Expand Down Expand Up @@ -180,10 +193,11 @@ export const kalshiAmendOrderTool: ToolConfig<KalshiAmendOrderParams, KalshiAmen
visibility: 'user-or-llm',
description: 'Updated no price in dollars (e.g., "0.56")',
},
subaccount: KALSHI_ORDER_SUBACCOUNT_PARAM,
},

request: {
url: (params) => buildKalshiUrl(amendRequestPath(params)),
url: (params) => amendRequestUrl(params),
method: (params) => amendRequestMethod(params),
headers: (params) =>
buildKalshiAuthHeaders(
Expand Down Expand Up @@ -249,6 +263,7 @@ export interface KalshiAmendOrderV2Params extends KalshiAuthParams {
yesPriceDollars?: string // Updated yes price in dollars
noPriceDollars?: string // Updated no price in dollars
countFp?: string // Count in fixed-point for fractional contracts
subaccount?: string // Subaccount the order belongs to
}

interface KalshiAmendOrderV2Order {
Expand Down Expand Up @@ -430,10 +445,11 @@ export const kalshiAmendOrderV2Tool: ToolConfig<
visibility: 'user-or-llm',
description: 'Count in fixed-point for fractional contracts',
},
subaccount: KALSHI_ORDER_SUBACCOUNT_PARAM,
},

request: {
url: (params) => buildKalshiUrl(amendRequestPath(params)),
url: (params) => amendRequestUrl(params),
method: (params) => amendRequestMethod(params),
headers: (params) =>
buildKalshiAuthHeaders(
Expand Down
34 changes: 34 additions & 0 deletions apps/sim/tools/kalshi/cancel_order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,40 @@ describe('Kalshi cancel order routes to the order shard by ticker', () => {
})
})

describe('Kalshi cancel order on a subaccount', () => {
const query = (tool: CancelTool, params: KalshiCancelOrderV2Params) => {
const { url } = tool.request
if (typeof url !== 'function') throw new Error('cancel tool has no url builder')
return new URL(url(params)).searchParams
}

it.each(TOOLS)('%s sends the subaccount on the cancel, not on the order read', (_, tool) => {
expect(query(tool, { ...BASE, ticker: TICKER, subaccount: '2' }).get('subaccount')).toBe('2')
expect(query(tool, { ...BASE, subaccount: '2' }).has('subaccount')).toBe(false)
expect(query(tool, { ...BASE, ticker: TICKER }).has('subaccount')).toBe(false)
expect(sentRequest(tool, { ...BASE, ticker: TICKER, subaccount: '2' }).signatureValid).toBe(
true
)
})

it.each(TOOLS)(
'%s keeps the subaccount on the cancel it sends after the order read',
async (_, tool) => {
const { read, postProcess } = await readOrder(tool)
const forwarded: Array<URLSearchParams> = []
await postProcess(read as never, { ...BASE, subaccount: '2' }, async (_toolId, params) => {
forwarded.push(query(tool, params as unknown as KalshiCancelOrderV2Params))
return { success: true, output: {} }
})
expect(forwarded.map((search) => search.get('subaccount'))).toEqual(['2'])
}
)

it.each(TOOLS)('%s refuses a subaccount that is not a whole number', (_, tool) => {
expect(() => query(tool, { ...BASE, ticker: TICKER, subaccount: 'two' })).toThrow(/Subaccount/)
})
})

describe('Kalshi block cancel ticker', () => {
const mapped = (stored: Record<string, unknown>) => {
const inputs: Record<string, unknown> = {
Expand Down
Loading
Loading