cli-mock-llm.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type { Context } from 'cordis'
  2. import {
  3. CallId,
  4. LlmAdapter,
  5. ReasoningEffortId,
  6. type GenerateOptions,
  7. type LlmResolvedModelInfo,
  8. type StreamChunk,
  9. } from '@deepseek-ai/dsh-llm'
  10. const HIGH = ReasoningEffortId('high')
  11. const OFF = ReasoningEffortId('off')
  12. /** Keyless headless-agent adapter: one real bash call followed by a final answer. */
  13. class CliMockAdapter extends LlmAdapter {
  14. override async resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
  15. return {
  16. provider,
  17. id: model,
  18. name: model,
  19. reasoning: {
  20. efforts: [
  21. { id: OFF, name: 'Off' },
  22. { id: HIGH, name: 'High' },
  23. ],
  24. defaultEffort: HIGH,
  25. },
  26. }
  27. }
  28. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  29. const toolResult = options.messages.at(-1)?.content.find(block => block.type === 'tool-result')
  30. if (toolResult === undefined) {
  31. const args = JSON.stringify({ command: 'printf CLI_TOOL_ROUND_TRIP', description: 'Prove the CLI tool round trip.' })
  32. yield { type: 'block-start', index: 0, blockType: 'tool-call' }
  33. yield { type: 'tool-call-delta', index: 0, id: CallId('cli-smoke-call'), name: 'bash', argumentsDelta: args }
  34. yield { type: 'block-end', index: 0, block: { type: 'tool-call', id: CallId('cli-smoke-call'), name: 'bash', arguments: args } }
  35. yield { type: 'usage', usage: { inputTokens: 11, outputTokens: 3, cacheReadTokens: 2 } }
  36. yield { type: 'finish', reason: { kind: 'tool-calls' } }
  37. return
  38. }
  39. const toolText = toolResult.content
  40. .filter(block => block.type === 'text')
  41. .map(block => block.text)
  42. .join('')
  43. const reply = `CLI tool round trip complete: ${toolText.trim()}`
  44. yield { type: 'block-start', index: 0, blockType: 'text' }
  45. yield { type: 'text-delta', index: 0, text: reply }
  46. yield { type: 'block-end', index: 0, block: { type: 'text', text: reply } }
  47. yield { type: 'usage', usage: { inputTokens: 7, outputTokens: 5, reasoningTokens: 1 } }
  48. yield { type: 'finish', reason: { kind: 'stop' } }
  49. }
  50. }
  51. export const name = 'cli-mock-llm'
  52. export const inject = ['llm']
  53. /** Register the keyless `cli-mock` adapter. */
  54. export function apply(ctx: Context): void {
  55. ctx.llm.registerAdapter(['cli-mock'], new CliMockAdapter())
  56. ctx.on('agent/request', async (_agent, _turn, step, _signal, next) => {
  57. const config = await next()
  58. return step === 2 ? { ...config, reasoningEffort: OFF } : config
  59. })
  60. }