{"version":3,"file":"xllm_dev.cjs","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":["constructor","script","options","this","requests","index","calls","infer","req","i","push","messages","cloneJson","step","reply","fallback","error","kind","message","play","Promise","_","reject","fail","LLMComputeException","signal","aborted","addEventListener","once","resolve","setTimeout","delay_ms","then","usage","total_tokens","request_units","content","text","type","n","call","tool_calls","call_id","name","args","length","role"],"mappings":"2VAgCO,MAKL,WAAAA,CAA6BC,EAAyCC,EAA8B,IAAvEC,KAAAF,OAAAA,EAAyCE,KAAAD,QAAAA,EAHtEC,KAASC,SAAkC,GAC3CD,KAAQE,MAAQ,CAEyF,CAGzG,SAAIC,GACF,OAAOH,KAAKE,KACd,CAEA,WAAME,CAAMC,GACV,MAAMC,EAAIN,KAAKE,QACVF,KAAAC,SAASM,KAAK,IAAKF,EAAKG,SAAUC,EAAAA,UAAUJ,EAAIG,YAC/C,MAAAE,EAAOV,KAAKF,OAAOQ,GACrB,IAAAK,EAMJ,O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