{"version":3,"file":"xllm_dev.mjs","sources":["../src/xllm/dev/scripted.ts"],"sourcesContent":["// A scripted LlmClient for development and tests (Rust wishkit `ScriptedModel`):\n// replies by call number, never by matching prompt text.\n\nimport type { AiMessage, AiResponse, AiToolCall, AiUsage, JsonValue, LLMComputeError, LlmClient, LlmInferenceRequest } from '../core/types'\nimport { LLMComputeException } from '../core/errors'\nimport { cloneJson } from '../core/json'\n\nexport interface ScriptedCall {\n  name: string\n  args?: AiToolCall['args']\n  /** Default `call_<reply#>_<n>`. */\n  call_id?: string\n}\n\nexport type ScriptedReply =\n  | { text?: string; tool_calls?: ScriptedCall[]; usage?: AiUsage }\n  | { message: AiMessage; usage?: AiUsage }\n  | { error: LLMComputeError }\n  /** Never resolves (until the request signal aborts): an inference in flight. */\n  | { hang: true }\n  /** Resolves after `ms` with `then`. */\n  | { delay_ms: number; then: ScriptedReply }\n\nexport type ScriptedStep = ScriptedReply | ((req: LlmInferenceRequest, index: number) => ScriptedReply | Promise<ScriptedReply>)\n\nexport interface ScriptedLlmOptions {\n  /** Reply when the script is exhausted (default: an `internal` error). */\n  fallback?: ScriptedReply\n  /** Usage reported for every reply without its own (default `{ total_tokens: 10, request_units: 1 }`). */\n  usage?: AiUsage\n}\n\nexport class ScriptedLlmClient implements LlmClient {\n  /** Every request received, in order. */\n  readonly requests: LlmInferenceRequest[] = []\n  private index = 0\n\n  constructor(private readonly script: ScriptedStep[], private readonly options: ScriptedLlmOptions = {}) {}\n\n  /** Number of inferences requested so far. */\n  get calls(): number {\n    return this.index\n  }\n\n  async infer(req: LlmInferenceRequest): Promise<AiResponse> {\n    const i = this.index++\n    this.requests.push({ ...req, messages: cloneJson(req.messages) })\n    const step = this.script[i]\n    let reply: ScriptedReply\n    if (step === undefined) {\n      reply = this.options.fallback ?? { error: { kind: 'internal', message: `scripted model has no reply #${i + 1}` } }\n    } else {\n      reply = typeof step === 'function' ? await step(req, i) : step\n    }\n    return this.play(reply, req, i)\n  }\n\n  private async play(reply: ScriptedReply, req: LlmInferenceRequest, i: number): Promise<AiResponse> {\n    if ('hang' in reply) {\n      return new Promise((_, reject) => {\n        const fail = () => reject(new LLMComputeException({ kind: 'cancelled' }))\n        if (req.signal.aborted) fail()\n        else req.signal.addEventListener('abort', fail, { once: true })\n      })\n    }\n    if ('delay_ms' in reply) {\n      await new Promise(resolve => setTimeout(resolve, reply.delay_ms))\n      return this.play(reply.then, req, i)\n    }\n    if ('error' in reply) throw new LLMComputeException(reply.error)\n    const usage = reply.usage ?? this.options.usage ?? { total_tokens: 10, request_units: 1 }\n    if ('message' in reply) return { message: cloneJson(reply.message), usage }\n    const content: AiMessage['content'] = []\n    if (reply.text !== undefined && reply.text !== '') content.push({ type: 'text', text: reply.text })\n    let n = 0\n    for (const call of reply.tool_calls ?? []) {\n      n += 1\n      content.push({\n        type: 'tool_use',\n        call_id: call.call_id ?? `call_${i + 1}_${n}`,\n        name: call.name,\n        args: (call.args ?? {}) as Record<string, JsonValue>,\n      })\n    }\n    if (content.length === 0) content.push({ type: 'text', text: '' })\n    return { message: { role: 'assistant', content }, usage }\n  }\n}\n"],"names":[],"mappings":";;;AAgCO,MAAM,kBAAuC;AAAA,EAKlD,YAA6B,QAAyC,UAA8B,IAAI;AAA3E,SAAA,SAAA;AAAyC,SAAA,UAAA;AAHtE,SAAS,WAAkC;AAC3C,SAAQ,QAAQ;AAAA,EAEyF;AAAA;AAAA,EAGzG,IAAI,QAAgB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,MAAM,KAA+C;AACzD,UAAM,IAAI,KAAK;AACV,SAAA,SAAS,KAAK,EAAE,GAAG,KAAK,UAAU,UAAU,IAAI,QAAQ,EAAA,CAAG;AAC1D,UAAA,OAAO,KAAK,OAAO,CAAC;AACtB,QAAA;AACJ,QAAI,SAAS,QAAW;AACtB,cAAQ,KAAK,QAAQ,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,SAAS,gCAAgC,IAAI,CAAC,GAAK,EAAA;AAAA,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