({
client,
spec,
meta,
opts,
}: {
client: CoreApiClient
spec: CommandSpec
meta: OperationMeta
opts: Record<string, string | undefined>
})
| 26 | } |
| 27 | |
| 28 | function parseBodyValue({ name, value }: { name: string; value: string }): unknown { |
| 29 | if (name === 'messages') |
| 30 | try { |
| 31 | return JSON.parse(value) as unknown |
| 32 | } catch { |
| 33 | return [{ role: 'user', content: value }] |
| 34 | } |
| 35 | |
| 36 | if (name === 'stream') { |
| 37 | if (value === 'true' || value === '1') return true |
| 38 | if (value === 'false' || value === '0') return false |
| 39 | throw new Error(`Invalid --stream value: ${value}. Use true, 1, false, or 0`) |
| 40 | } |
| 41 | |
| 42 | if (name === 'temperature') { |
| 43 | const n = Number.parseFloat(value) |
| 44 | if (!Number.isFinite(n)) |
| 45 | throw new Error(`Invalid --temperature value: ${value}. Must be a finite number`) |
| 46 | return n |
| 47 | } |
| 48 | |
| 49 | if (name === 'tools') |
| 50 | try { |
| 51 | return JSON.parse(value) as unknown |
| 52 | } catch { |
| 53 | throw new Error(`Invalid --tools JSON: ${value}`) |
| 54 | } |
| 55 | |
| 56 | return value |
| 57 | } |
| 58 | |
| 59 | export async function runCommand({ |
| 60 | client, |
| 61 | spec, |
| 62 | meta, |
| 63 | opts, |
| 64 | }: { |
| 65 | client: CoreApiClient |
| 66 | spec: CommandSpec |
| 67 | meta: OperationMeta |
| 68 | opts: Record<string, string | undefined> |
| 69 | }): Promise<unknown> { |
| 70 | const clientPath = buildClientPath(spec) |
| 71 | const fn = getNested(client, clientPath) |
| 72 | if (!fn) throw new Error(`Command not found: ${spec.path.join(' ')}`) |
| 73 | |
| 74 | const pathParams: Record<string, string> = {} |
| 75 | for (const p of meta.pathParams) { |
| 76 | const v = opts[p.name] |
| 77 | if (v) pathParams[p.name] = v |
| 78 | } |
| 79 | const bodyParams: Record<string, unknown> = {} |
| 80 | for (const p of meta.bodyParams) { |
| 81 | const v = opts[p.name] |
| 82 | if (v !== undefined && v !== '') bodyParams[p.name] = parseBodyValue({ name: p.name, value: v }) |
| 83 | } |
| 84 | |
| 85 | const callOpts: Record<string, unknown> = {} |
no test coverage detected