1
0

cli-mock-llm.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { Context } from '@deepseek-ai/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. if (process.env.DSH_CLI_MOCK_FAILURE === '1') {
  30. yield { type: 'finish', reason: { kind: 'error', failure: { code: 'SERVER', message: 'CLI mock provider failed' } } }
  31. return
  32. }
  33. const toolResult = options.messages.at(-1)?.content.find(block => block.type === 'tool-result')
  34. if (toolResult === undefined) {
  35. const args = JSON.stringify({ command: 'printf CLI_TOOL_ROUND_TRIP', description: 'Prove the CLI tool round trip.' })
  36. yield { type: 'block-start', index: 0, blockType: 'tool-call' }
  37. yield { type: 'tool-call-delta', index: 0, id: CallId('cli-smoke-call'), name: 'bash', argumentsDelta: args }
  38. yield { type: 'block-end', index: 0, block: { type: 'tool-call', id: CallId('cli-smoke-call'), name: 'bash', arguments: args } }
  39. yield { type: 'usage', usage: { inputTokens: 11, outputTokens: 3, cacheReadTokens: 2 } }
  40. yield { type: 'finish', reason: { kind: 'tool-calls' } }
  41. return
  42. }
  43. const toolText = toolResult.content
  44. .filter(block => block.type === 'text')
  45. .map(block => block.text)
  46. .join('')
  47. const reply = `CLI tool round trip complete: ${toolText.trim()}`
  48. yield { type: 'block-start', index: 0, blockType: 'text' }
  49. yield { type: 'text-delta', index: 0, text: reply }
  50. yield { type: 'block-end', index: 0, block: { type: 'text', text: reply } }
  51. yield { type: 'usage', usage: { inputTokens: 7, outputTokens: 5, reasoningTokens: 1 } }
  52. yield { type: 'finish', reason: { kind: 'stop' } }
  53. }
  54. }
  55. export const name = 'cli-mock-llm'
  56. export const inject = ['llm']
  57. /** Register the keyless `cli-mock` adapter. */
  58. export function apply(ctx: Context): void {
  59. ctx.llm.registerAdapter(['cli-mock'], new CliMockAdapter())
  60. ctx.on('agent/request', async ({ step }, next) => {
  61. const config = await next()
  62. return step === 2 ? { ...config, reasoningEffort: OFF } : config
  63. })
  64. }