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
37 changes: 37 additions & 0 deletions apps/sim/tools/arxiv/get_paper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest'
import { getPaperTool } from '@/tools/arxiv/get_paper'

function requestedId(paperId: string): string | null {
const url = getPaperTool.request.url
if (typeof url !== 'function') throw new Error('get paper tool has no url builder')
return new URL(url({ paperId })).searchParams.get('id_list')
}

describe('arXiv get paper ID parsing', () => {
it.each([
['1706.03762', '1706.03762'],
[' 1706.03762 ', '1706.03762'],
['hep-th/9901001', 'hep-th/9901001'],
['https://arxiv.org/abs/1706.03762', '1706.03762'],
['http://arxiv.org/abs/1706.03762v5', '1706.03762v5'],
['https://arxiv.org/pdf/1706.03762v2.pdf', '1706.03762v2'],
['https://arxiv.org/pdf/1706.03762v2', '1706.03762v2'],
['https://arxiv.org/abs/1706.03762?context=cs#abstract', '1706.03762'],
['https://arxiv.org/abs/hep-th/9901001', 'hep-th/9901001'],
['https://www.arxiv.org/abs/1706.03762', '1706.03762'],
['https://export.arxiv.org/abs/1706.03762', '1706.03762'],
['arxiv.org/abs/1706.03762', '1706.03762'],
['www.arxiv.org/pdf/1706.03762.pdf', '1706.03762'],
])('reads %s as %s', (input, expected) => {
expect(requestedId(input)).toBe(expected)
})

it.each([
'https://evil.com/?x=arxiv.org/abs/1234',
'https://evil.com/arxiv.org/abs/1234',
'https://notarxiv.org/abs/1234',
'https://arxiv.org.evil.com/abs/1234',
])('does not unwrap %s as an arXiv link', (input) => {
expect(requestedId(input)).not.toBe('1234')
})
})
37 changes: 31 additions & 6 deletions apps/sim/tools/arxiv/get_paper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ import type { ArxivGetPaperParams, ArxivGetPaperResponse } from '@/tools/arxiv/t
import { parseArxivXML } from '@/tools/arxiv/utils'
import type { ToolConfig } from '@/tools/types'

/** A scheme-less link on arxiv.org or one of its subdomains, such as `www.arxiv.org/abs/<id>`. */
const SCHEMELESS_ARXIV_LINK = /^(?:[a-z0-9-]+\.)*arxiv\.org\//i

/**
* The paper ID from a bare ID or an arxiv.org `/abs/` or `/pdf/` link. Only a link whose host is
* arxiv.org or a subdomain of it is unwrapped; any other input is taken as a bare ID.
*/
function arxivPaperId(input: string): string {
const trimmed = input.trim()
const linkPath = arxivLinkPath(
SCHEMELESS_ARXIV_LINK.test(trimmed) ? `https://${trimmed}` : trimmed
)
const id = linkPath ?? trimmed.replace(/[?#].*$/, '')
return id.replace(/\.pdf$/, '')
}

/** The ID after `/abs/` or `/pdf/` when `candidate` is an http(s) arxiv.org link. */
function arxivLinkPath(candidate: string): string | undefined {
let url: URL
try {
url = new URL(candidate)
} catch {
return undefined
}
if (url.protocol !== 'https:' && url.protocol !== 'http:') return undefined
const host = url.hostname.toLowerCase()
if (host !== 'arxiv.org' && !host.endsWith('.arxiv.org')) return undefined
return /^\/(?:abs|pdf)\/(.+)$/.exec(url.pathname)?.[1]
}

export const getPaperTool: ToolConfig<ArxivGetPaperParams, ArxivGetPaperResponse> = {
id: 'arxiv_get_paper',
name: 'ArXiv Get Paper',
Expand All @@ -19,12 +49,7 @@ export const getPaperTool: ToolConfig<ArxivGetPaperParams, ArxivGetPaperResponse

request: {
url: (params: ArxivGetPaperParams) => {
const paperId = params.paperId
.trim()
.replace(/^.*arxiv\.org\/(?:abs|pdf)\//, '')
.replace(/[?#].*$/, '')
.replace(/\.pdf$/, '')

const paperId = arxivPaperId(params.paperId)
const baseUrl = 'https://export.arxiv.org/api/query'
const searchParams = new URLSearchParams()
searchParams.append('id_list', paperId)
Expand Down
19 changes: 19 additions & 0 deletions apps/sim/tools/kalshi/create_order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ describe('Kalshi create order on the V2 event-order endpoint', () => {
expect(() => createBody({ yesPrice: '56', noPrice: '50' })).toThrow(/different prices/)
})

it('refuses an order flag that is neither true nor false instead of dropping the restriction', () => {
expect(() => createBody({ yesPrice: '56', postOnly: 'ture' })).toThrow(
/Post Only must be true or false, got "ture"/
)
expect(() => createBody({ yesPrice: '56', reduceOnly: 'yes' })).toThrow(/Reduce Only/)
expect(() => createBody({ yesPrice: '56', cancelOrderOnPause: '1' })).toThrow(
/Cancel Order on Pause/
)
expect(
createBody({
yesPrice: '56',
postOnly: ' TRUE ',
reduceOnly: true as unknown as string,
cancelOrderOnPause: 'False',
})
).toMatchObject({ post_only: true, reduce_only: true, cancel_order_on_pause: false })
expect(createBody({ yesPrice: '56', postOnly: '' })).not.toHaveProperty('post_only')
})

it('posts to /portfolio/events/orders and signs that path', () => {
const { url, headers } = kalshiCreateOrderTool.request
if (typeof url !== 'function' || typeof headers !== 'function') {
Expand Down
20 changes: 14 additions & 6 deletions apps/sim/tools/kalshi/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,17 @@ function oneOf(value: unknown, allowed: string[], fallback: string, label: strin
return text
}

/** `undefined` when unset, so an untouched field sends nothing; `'false'` stays an explicit false. */
function optionalFlag(value: unknown): boolean | undefined {
return isBlank(value) ? undefined : String(value).trim().toLowerCase() === 'true'
/**
* `undefined` when unset, so an untouched field sends nothing; `'false'` stays an explicit false.
* Anything other than true or false throws, so a typo never drops a requested safety restriction.
*/
function optionalFlag(value: unknown, label: string): boolean | undefined {
if (typeof value === 'boolean') return value
if (isBlank(value)) return undefined
const text = String(value).trim().toLowerCase()
if (text === 'true') return true
if (text === 'false') return false
throw new Error(`${label} must be true or false, got "${String(value).trim()}"`)
}

/** The V2 book side for a legacy (side, action): buy yes and sell no bid; buy no and sell yes ask. */
Expand Down Expand Up @@ -194,11 +202,11 @@ export function buildKalshiCreateOrderBody(
if (!isBlank(params.expirationTs)) {
body.expiration_time = parseScaled(params.expirationTs, 0, 'Expiration')
}
const postOnly = optionalFlag(params.postOnly)
const postOnly = optionalFlag(params.postOnly, 'Post Only')
if (postOnly !== undefined) body.post_only = postOnly
const reduceOnly = optionalFlag(params.reduceOnly)
const reduceOnly = optionalFlag(params.reduceOnly, 'Reduce Only')
if (reduceOnly !== undefined) body.reduce_only = reduceOnly
const cancelOnPause = optionalFlag(params.cancelOrderOnPause)
const cancelOnPause = optionalFlag(params.cancelOrderOnPause, 'Cancel Order on Pause')
if (cancelOnPause !== undefined) body.cancel_order_on_pause = cancelOnPause
if (!isBlank(params.orderGroupId)) body.order_group_id = params.orderGroupId
if (!isBlank(params.subaccount)) body.subaccount = parseScaled(params.subaccount, 0, 'Subaccount')
Expand Down
Loading