mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
refactor(cli): organize command dispatch and orchestration owners (#41562)
* refactor(cli): organize command dispatch and orchestration owners * test(orchestration): retire superseded unbound mail refusal case
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { readOpenAiKeyFromStdin } from './speech'
|
||||
|
||||
async function* chunks(...parts: string[]): AsyncGenerator<Buffer> {
|
||||
for (const part of parts) {
|
||||
yield Buffer.from(part)
|
||||
}
|
||||
}
|
||||
|
||||
function piped(...parts: string[]): AsyncIterable<unknown> & { isTTY?: boolean } {
|
||||
return chunks(...parts)
|
||||
}
|
||||
|
||||
describe('orca speech openai-key set', () => {
|
||||
it('reads the key from piped stdin and trims the trailing newline', async () => {
|
||||
await expect(readOpenAiKeyFromStdin(piped('sk-test-', 'abc\n'))).resolves.toBe('sk-test-abc')
|
||||
})
|
||||
|
||||
it('refuses an interactive terminal instead of waiting on it', async () => {
|
||||
const terminal = Object.assign(chunks(), { isTTY: true })
|
||||
await expect(readOpenAiKeyFromStdin(terminal)).rejects.toThrow('Pipe the key on stdin')
|
||||
})
|
||||
|
||||
it('refuses empty and oversized input', async () => {
|
||||
await expect(readOpenAiKeyFromStdin(piped(' \n'))).rejects.toThrow('No key on stdin')
|
||||
await expect(readOpenAiKeyFromStdin(piped('x'.repeat(1025)))).rejects.toThrow('too long')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,82 @@
|
||||
import type { CommandHandler, HandlerContext } from '../dispatch/dispatch'
|
||||
import { printResult } from '../output/command-result-output'
|
||||
import { RuntimeClientError } from '../runtime/types'
|
||||
import type { RuntimeRpcSuccess } from '../../shared/runtime-rpc-envelope'
|
||||
import { OPENAI_SPEECH_KEY_MAX_LENGTH } from '../../shared/rpc-contract/speech-params'
|
||||
|
||||
type OpenAiSpeechKeyStatus = { configured: boolean; protection: string | null }
|
||||
|
||||
function formatKeyStatus(status: OpenAiSpeechKeyStatus): string {
|
||||
if (!status.configured) {
|
||||
return 'No OpenAI key for voice dictation on this host.'
|
||||
}
|
||||
return status.protection === 'plaintext'
|
||||
? 'OpenAI key saved on this host, unencrypted: this host cannot seal secrets.'
|
||||
: 'OpenAI key saved on this host.'
|
||||
}
|
||||
|
||||
// Keep secrets out of argv, shell history, and process listings.
|
||||
export async function readOpenAiKeyFromStdin(
|
||||
stdin: AsyncIterable<unknown> & { isTTY?: boolean } = process.stdin
|
||||
): Promise<string> {
|
||||
if (stdin.isTTY) {
|
||||
throw new RuntimeClientError(
|
||||
'invalid_argument',
|
||||
'Pipe the key on stdin, e.g. printf %s "$OPENAI_API_KEY" | orca speech openai-key set'
|
||||
)
|
||||
}
|
||||
const chunks: Buffer[] = []
|
||||
let bytes = 0
|
||||
for await (const chunk of stdin) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
|
||||
bytes += buffer.length
|
||||
if (bytes > OPENAI_SPEECH_KEY_MAX_LENGTH * 4) {
|
||||
throw new RuntimeClientError('invalid_argument', 'That is too long to be an OpenAI key')
|
||||
}
|
||||
chunks.push(buffer)
|
||||
}
|
||||
const key = Buffer.concat(chunks).toString('utf8').trim()
|
||||
if (!key) {
|
||||
throw new RuntimeClientError('invalid_argument', 'No key on stdin')
|
||||
}
|
||||
if (key.length > OPENAI_SPEECH_KEY_MAX_LENGTH) {
|
||||
throw new RuntimeClientError('invalid_argument', 'That is too long to be an OpenAI key')
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
export async function callOpenAiKeyMethod(
|
||||
client: Pick<HandlerContext['client'], 'call'>,
|
||||
method: string,
|
||||
params?: { apiKey: string }
|
||||
): Promise<RuntimeRpcSuccess<OpenAiSpeechKeyStatus>> {
|
||||
try {
|
||||
return params === undefined
|
||||
? await client.call<OpenAiSpeechKeyStatus>(method)
|
||||
: await client.call<OpenAiSpeechKeyStatus>(method, params)
|
||||
} catch (error) {
|
||||
if (error instanceof RuntimeClientError && error.code === 'method_not_found') {
|
||||
throw new RuntimeClientError(
|
||||
'incompatible_runtime',
|
||||
'This Orca server cannot store a voice dictation key yet. Update Orca on the server and try again.'
|
||||
)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export const SPEECH_HANDLERS: Record<string, CommandHandler> = {
|
||||
'speech openai-key status': async ({ client, json }) => {
|
||||
const result = await callOpenAiKeyMethod(client, 'speech.openAiKey.status')
|
||||
printResult(result, json, formatKeyStatus)
|
||||
},
|
||||
'speech openai-key set': async ({ client, json }) => {
|
||||
const apiKey = await readOpenAiKeyFromStdin()
|
||||
const result = await callOpenAiKeyMethod(client, 'speech.openAiKey.set', { apiKey })
|
||||
printResult(result, json, formatKeyStatus)
|
||||
},
|
||||
'speech openai-key clear': async ({ client, json }) => {
|
||||
const result = await callOpenAiKeyMethod(client, 'speech.openAiKey.clear')
|
||||
printResult(result, json, formatKeyStatus)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user