1
0

cli-mock-llm.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { Context } from 'cordis'
  2. import { CallId, LlmAdapter, type GenerateOptions, type StreamChunk } from '@deepseek-ai/dsh-llm'
  3. /** Keyless headless-agent adapter: one real bash call followed by a final answer. */
  4. class CliMockAdapter extends LlmAdapter {
  5. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  6. const toolResult = options.messages.at(-1)?.content.find(block => block.type === 'tool-result')
  7. if (toolResult === undefined) {
  8. const args = JSON.stringify({ command: 'printf CLI_TOOL_ROUND_TRIP', description: 'Prove the CLI tool round trip.' })
  9. yield { type: 'block-start', index: 0, blockType: 'tool-call' }
  10. yield { type: 'tool-call-delta', index: 0, id: CallId('cli-smoke-call'), name: 'bash', argumentsDelta: args }
  11. yield { type: 'block-end', index: 0, block: { type: 'tool-call', id: CallId('cli-smoke-call'), name: 'bash', arguments: args } }
  12. yield { type: 'usage', usage: { inputTokens: 11, outputTokens: 3, cacheReadTokens: 2 } }
  13. yield { type: 'finish', reason: { kind: 'tool-calls' } }
  14. return
  15. }
  16. const toolText = toolResult.content
  17. .filter(block => block.type === 'text')
  18. .map(block => block.text)
  19. .join('')
  20. const reply = `CLI tool round trip complete: ${toolText.trim()}`
  21. yield { type: 'block-start', index: 0, blockType: 'text' }
  22. yield { type: 'text-delta', index: 0, text: reply }
  23. yield { type: 'block-end', index: 0, block: { type: 'text', text: reply } }
  24. yield { type: 'usage', usage: { inputTokens: 7, outputTokens: 5, reasoningTokens: 1 } }
  25. yield { type: 'finish', reason: { kind: 'stop' } }
  26. }
  27. }
  28. export const name = 'cli-mock-llm'
  29. export const inject = ['llm']
  30. /** Register the keyless `cli-mock` adapter. */
  31. export function apply(ctx: Context): void {
  32. ctx.llm.registerAdapter(['cli-mock'], new CliMockAdapter())
  33. }