Skip to content

Commit ce2d810

Browse files
committed
fix(integrations): migrate kalshi create and amend to the v2 order api, address review
1 parent ff56708 commit ce2d810

17 files changed

Lines changed: 1337 additions & 374 deletions

File tree

‎apps/docs/content/docs/integrations/kalshi.mdx‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,18 @@ Create a new order on a Kalshi prediction market
722722
| `side` | string | Yes | Side of the order: "yes" or "no" |
723723
| `action` | string | Yes | Action type: "buy" or "sell" |
724724
| `count` | string | No | Number of contracts to trade \(e.g., "10", "100"\). Provide count or countFp |
725-
| `type` | string | No | Order type: "limit" or "market" \(default: "limit"\) |
725+
| `type` | string | No | Order type: "limit" \(default\). Kalshi no longer accepts "market" orders; set a price instead |
726726
| `yesPrice` | string | No | Yes price in cents \(1-99\) |
727727
| `noPrice` | string | No | No price in cents \(1-99\) |
728728
| `yesPriceDollars` | string | No | Yes price in dollars \(e.g., "0.56"\) |
729729
| `noPriceDollars` | string | No | No price in dollars \(e.g., "0.56"\) |
730730
| `clientOrderId` | string | No | Custom order identifier |
731731
| `expirationTs` | string | No | Unix timestamp for order expiration |
732-
| `timeInForce` | string | No | Time in force: 'fill_or_kill', 'good_till_canceled', 'immediate_or_cancel' |
733-
| `buyMaxCost` | string | No | Maximum cost in cents \(auto-enables fill_or_kill\) |
732+
| `timeInForce` | string | No | Time in force: 'good_till_canceled' \(default\), 'fill_or_kill', or 'immediate_or_cancel' |
733+
| `buyMaxCost` | string | No | No longer supported by Kalshi: set a limit price and count with timeInForce fill_or_kill instead |
734734
| `postOnly` | string | No | Set to 'true' for maker-only orders |
735735
| `reduceOnly` | string | No | Set to 'true' for position reduction only |
736-
| `selfTradePreventionType` | string | No | Self-trade prevention: 'taker_at_cross' or 'maker' |
736+
| `selfTradePreventionType` | string | No | Self-trade prevention: 'taker_at_cross' \(default\) or 'maker' |
737737
| `orderGroupId` | string | No | Associated order group ID |
738738
| `countFp` | string | No | Count in fixed-point for fractional contracts |
739739
| `cancelOrderOnPause` | string | No | Set to 'true' to cancel order on market pause |
@@ -743,7 +743,7 @@ Create a new order on a Kalshi prediction market
743743

744744
| Parameter | Type | Description |
745745
| --------- | ---- | ----------- |
746-
| `order` | object | The created order object with full API response fields |
746+
| `order` | object | The created order. Kalshi reports only its ID and fill counts on create; the direction, price and settings are the ones this request sent, and fields Kalshi no longer reports are null |
747747
| ↳ `order_id` | string | Order ID |
748748
| ↳ `user_id` | string | User ID |
749749
| ↳ `client_order_id` | string | Client order ID |
@@ -851,7 +851,7 @@ Modify the price or quantity of an existing order on Kalshi
851851
| `action` | string | Yes | Action type: "buy" or "sell" |
852852
| `clientOrderId` | string | No | Original client-specified order ID |
853853
| `updatedClientOrderId` | string | No | New client-specified order ID after amendment |
854-
| `count` | string | No | Updated quantity for the order \(e.g., "10", "100"\) |
854+
| `count` | string | No | New total contracts for the order: already filled plus desired resting \(e.g., "10"\). Leave empty to keep the current total |
855855
| `yesPrice` | string | No | Updated yes price in cents \(1-99\) |
856856
| `noPrice` | string | No | Updated no price in cents \(1-99\) |
857857
| `yesPriceDollars` | string | No | Updated yes price in dollars \(e.g., "0.56"\) |
@@ -862,7 +862,7 @@ Modify the price or quantity of an existing order on Kalshi
862862

863863
| Parameter | Type | Description |
864864
| --------- | ---- | ----------- |
865-
| `old_order` | object | The original order object before amendment |
865+
| `old_order` | object | The order before the amend when it was read first \(an amend with no count or price\); otherwise only order_id is known |
866866
| ↳ `order_id` | string | Order ID |
867867
| ↳ `user_id` | string | User ID |
868868
| ↳ `ticker` | string | Market ticker |
@@ -872,9 +872,13 @@ Modify the price or quantity of an existing order on Kalshi
872872
| ↳ `type` | string | Order type \(limit/market\) |
873873
| ↳ `yes_price` | number | Yes price in cents |
874874
| ↳ `no_price` | number | No price in cents |
875+
| ↳ `yes_price_dollars` | string | Yes price in dollars |
876+
| ↳ `no_price_dollars` | string | No price in dollars |
875877
| ↳ `action` | string | Action \(buy/sell\) |
876878
| ↳ `count` | number | Number of contracts |
877879
| ↳ `remaining_count` | number | Remaining contracts |
880+
| ↳ `fill_count_fp` | string | Filled count \(fixed-point\) |
881+
| ↳ `remaining_count_fp` | string | Remaining count \(fixed-point\) |
878882
| ↳ `created_time` | string | Order creation time |
879883
| ↳ `expiration_time` | string | Order expiration time |
880884
| ↳ `order_group_id` | string | Order group ID |
@@ -891,7 +895,7 @@ Modify the price or quantity of an existing order on Kalshi
891895
| ↳ `stop_loss_order_id` | string | Stop loss order ID |
892896
| ↳ `amend_count` | number | Amend count |
893897
| ↳ `amend_taker_fill_count` | number | Amend taker fill count |
894-
| `order` | object | The amended order object with full API response fields |
898+
| `order` | object | The amended order: order_id, client_order_id and counts from Kalshi, with the direction, price and total count this amend sent; fields Kalshi no longer reports are null |
895899
| ↳ `order_id` | string | Order ID |
896900
| ↳ `user_id` | string | User ID |
897901
| ↳ `ticker` | string | Market ticker |
@@ -901,9 +905,13 @@ Modify the price or quantity of an existing order on Kalshi
901905
| ↳ `type` | string | Order type \(limit/market\) |
902906
| ↳ `yes_price` | number | Yes price in cents |
903907
| ↳ `no_price` | number | No price in cents |
908+
| ↳ `yes_price_dollars` | string | Yes price in dollars |
909+
| ↳ `no_price_dollars` | string | No price in dollars |
904910
| ↳ `action` | string | Action \(buy/sell\) |
905911
| ↳ `count` | number | Number of contracts |
906912
| ↳ `remaining_count` | number | Remaining contracts |
913+
| ↳ `fill_count_fp` | string | Filled count \(fixed-point\) |
914+
| ↳ `remaining_count_fp` | string | Remaining count \(fixed-point\) |
907915
| ↳ `created_time` | string | Order creation time |
908916
| ↳ `expiration_time` | string | Order expiration time |
909917
| ↳ `order_group_id` | string | Order group ID |

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ function hasMultipleOperations(block: BlockConfig | undefined): boolean {
281281
* @param block - The block config to get operations for
282282
* @returns Array of operation options with label and id properties
283283
*/
284-
function getOperationOptions(block: BlockConfig | undefined): { label: string; id: string }[] {
284+
function getOperationOptions(
285+
block: BlockConfig | undefined
286+
): { label: string; id: string; hidden?: boolean }[] {
285287
if (!block || !block.tools?.access) return []
286288

287289
const operationSubBlock = block.subBlocks.find((sb) => sb.id === OPERATION_SUBBLOCK_ID)
@@ -290,7 +292,7 @@ function getOperationOptions(block: BlockConfig | undefined): { label: string; i
290292
operationSubBlock.type === 'dropdown' &&
291293
Array.isArray(operationSubBlock.options)
292294
) {
293-
return operationSubBlock.options as { label: string; id: string }[]
295+
return operationSubBlock.options as { label: string; id: string; hidden?: boolean }[]
294296
}
295297

296298
return block.tools.access.map((toolId) => {
@@ -612,8 +614,9 @@ export const ToolInput = memo(function ToolInput({
612614
const { getDeniedOperations } = useOperationAccess()
613615

614616
/**
615-
* A tool block's selectable operations paired with the ones the caller's
616-
* permission group denies.
617+
* A tool block's selectable operations paired with the ones the caller may not
618+
* pick: those the caller's permission group denies, and retired operations the
619+
* block keeps as `hidden` options so saved workflows stay valid.
617620
*
618621
* Both callers derive from this single result so they cannot drift: the
619622
* picker *removes* denied operations (it must never offer or default to one),
@@ -623,12 +626,14 @@ export const ToolInput = memo(function ToolInput({
623626
const getOperationChoices = useCallback(
624627
(block: BlockConfig | undefined) => {
625628
const options = getOperationOptions(block).filter((option) => option.id !== '')
629+
const denied = getDeniedOperations(
630+
block,
631+
options.map((option) => option.id)
632+
)
633+
const hiddenIds = options.filter((option) => option.hidden).map((option) => option.id)
626634
return {
627635
options,
628-
denied: getDeniedOperations(
629-
block,
630-
options.map((option) => option.id)
631-
),
636+
denied: hiddenIds.length > 0 ? new Set([...denied, ...hiddenIds]) : denied,
632637
}
633638
},
634639
[getDeniedOperations]

‎apps/sim/blocks/blocks/kalshi.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ Return ONLY the numeric timestamp (seconds since Unix epoch) - no explanations,
479479
id: 'countAmend',
480480
title: 'Contracts',
481481
type: 'short-input',
482-
placeholder: 'Updated number of contracts (optional)',
482+
placeholder: 'New total contracts, filled plus resting (empty keeps the current total)',
483483
condition: { field: 'operation', value: ['amend_order'] },
484484
},
485485
{
@@ -488,7 +488,8 @@ Return ONLY the numeric timestamp (seconds since Unix epoch) - no explanations,
488488
type: 'dropdown',
489489
options: [
490490
{ label: 'Limit', id: 'limit' },
491-
{ label: 'Market', id: 'market' },
491+
// Kalshi removed market orders (2026-02-11); kept hidden so saved blocks stay valid.
492+
{ label: 'Market', id: 'market', hidden: true },
492493
],
493494
condition: { field: 'operation', value: ['create_order'] },
494495
},
@@ -542,6 +543,18 @@ Return ONLY the numeric timestamp (seconds since Unix epoch) - no explanations,
542543
condition: { field: 'operation', value: ['create_order'] },
543544
mode: 'advanced',
544545
},
546+
{
547+
id: 'orderSelfTradePrevention',
548+
title: 'Self-Trade Prevention',
549+
type: 'dropdown',
550+
options: [
551+
{ label: 'Taker at cross (default)', id: '' },
552+
{ label: 'Maker', id: 'maker' },
553+
{ label: 'Taker at cross', id: 'taker_at_cross', hidden: true },
554+
],
555+
condition: { field: 'operation', value: ['create_order'] },
556+
mode: 'advanced',
557+
},
545558
{
546559
id: 'expirationTs',
547560
title: 'Expiration',
@@ -712,6 +725,7 @@ Return ONLY the numeric timestamp (seconds since Unix epoch) - no explanations,
712725
orderIdParam,
713726
tickerOrder,
714727
orderType,
728+
orderSelfTradePrevention,
715729
countAmend,
716730
clientOrderIdAmend,
717731
...rest
@@ -787,6 +801,7 @@ Return ONLY the numeric timestamp (seconds since Unix epoch) - no explanations,
787801

788802
if (operation === 'create_order') {
789803
mapField('type', orderType)
804+
mapField('selfTradePreventionType', orderSelfTradePrevention)
790805
}
791806

792807
if (operation === 'amend_order') {

‎apps/sim/blocks/blocks/spotify.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ export const SpotifyBlock: BlockConfig = {
895895
title: 'Limit',
896896
type: 'short-input',
897897
placeholder:
898-
'Number of results (empty for the default; Development Mode apps allow at most 10)',
898+
'Number of results (empty for the default; Search allows at most 10 for Development Mode apps)',
899899
condition: {
900900
field: 'operation',
901901
value: [

‎apps/sim/tools/error-extractors.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { describe, expect, it } from 'vitest'
2-
import { ErrorExtractorId, type ErrorInfo, extractErrorMessage } from '@/tools/error-extractors'
2+
import {
3+
ErrorExtractorId,
4+
type ErrorInfo,
5+
extractErrorMessage,
6+
redactErrorData,
7+
} from '@/tools/error-extractors'
38

49
describe('Error Extractors', () => {
510
describe('extractErrorMessage', () => {
@@ -468,6 +473,14 @@ describe('tavily-errors', () => {
468473
expect(page.startsWith(message.slice(0, -1))).toBe(true)
469474
})
470475

476+
it('caps the retained plain-text body as well as the message', () => {
477+
const page = `<html><body>${'Bad gateway '.repeat(200)}</body></html>`
478+
const retained = redactErrorData({ status: 502, data: page }, ErrorExtractorId.TAVILY_ERRORS)
479+
expect(typeof retained === 'string' && retained.length).toBe(501)
480+
const json = { detail: { error: 'bad key' } }
481+
expect(redactErrorData({ status: 401, data: json }, ErrorExtractorId.TAVILY_ERRORS)).toBe(json)
482+
})
483+
471484
it('keeps a short plain-text error whole', () => {
472485
expect(
473486
extractErrorMessage(

‎apps/sim/tools/error-extractors.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ const ERROR_EXTRACTORS: ErrorExtractorConfig[] = [
651651
'Tavily error bodies: FastAPI `detail` as an object with `error`, a string, or a validation array; a top-level `error`; or a plain-text body',
652652
examples: ['Tavily'],
653653
explicitOnly: true,
654+
// A proxy or gateway page is capped on the retained body too, not only in the message.
655+
redactData: (errorInfo) =>
656+
typeof errorInfo?.data === 'string'
657+
? truncateAtCodePoint(errorInfo.data, TAVILY_MAX_TEXT_ERROR_LENGTH, '…')
658+
: errorInfo?.data,
654659
extract: (errorInfo) => {
655660
const data = errorInfo?.data
656661
if (typeof data === 'string') {

‎apps/sim/tools/generated/tool-metadata.ts‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎apps/sim/tools/generated/tool-outputs.ts‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)