@kehto/paja 0.26.1 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13399,6 +13399,10 @@ var REQUEST_TYPES = /* @__PURE__ */ new Set(["llm.select", "llm.describe", "llm.
13399
13399
  var CANCELLED = { code: "cancelled", message: "Generation cancelled; provider charges may still apply." };
13400
13400
  var TIMEOUT = { code: "timeout", message: "Generation deadline reached; provider charges may still apply." };
13401
13401
  var EXPIRED = { code: "selection-expired", message: "Unknown or expired model handle; select again." };
13402
+ var CHANGED_DURING_SELECT = { code: "selection-expired", message: "Connections changed during selection; select again." };
13403
+ var CHANGED_DURING_CONSENT = { code: "selection-expired", message: "The selected connection changed before submission; select again." };
13404
+ var SELECT_REVOKED = { code: "cancelled", message: "Selection cancelled; authorization was revoked." };
13405
+ var DEFAULT_MAX_HANDLES = 32;
13402
13406
  function failure(error) {
13403
13407
  if (error instanceof LlmServiceError) return { code: error.code, message: error.message };
13404
13408
  return { code: "provider-error", message: "The provider request failed." };
@@ -13453,9 +13457,55 @@ function cancelWindow(state, windowId, error) {
13453
13457
  settle6(state, windowId, requestId, generation2, error ? { type: "llm.generate.result", id: requestId, error } : null);
13454
13458
  }
13455
13459
  }
13460
+ function expireStaleConsent(state) {
13461
+ for (const [windowId, perWindow] of state.active) {
13462
+ for (const [requestId, generation2] of perWindow) {
13463
+ if (!generation2.awaitingConsent) continue;
13464
+ if (generation2.epoch === state.epoch && state.handles.get(windowId)?.has(generation2.handle)) continue;
13465
+ failGeneration(state, windowId, requestId, generation2, CHANGED_DURING_CONSENT);
13466
+ }
13467
+ }
13468
+ }
13469
+ function settleSelects(state, windowId, error) {
13470
+ const windows = windowId === null ? [...state.selects.keys()] : [windowId];
13471
+ for (const id of windows) {
13472
+ for (const pending of state.selects.get(id) ?? []) {
13473
+ pending.done = true;
13474
+ pending.controller.abort();
13475
+ if (error) reply(pending.send, "llm.select", pending.id, { error });
13476
+ }
13477
+ state.selects.delete(id);
13478
+ }
13479
+ }
13456
13480
  function invalidateHandles(state) {
13457
13481
  state.epoch += 1;
13458
13482
  state.handles.clear();
13483
+ settleSelects(state, null, CHANGED_DURING_SELECT);
13484
+ expireStaleConsent(state);
13485
+ }
13486
+ function storeHandle(state, windowId, handle, entry) {
13487
+ const entries = windowHandles(state, windowId);
13488
+ entries.set(handle, entry);
13489
+ let evicted = false;
13490
+ while (entries.size > state.maxHandles) {
13491
+ const oldest = entries.keys().next().value;
13492
+ entries.delete(oldest);
13493
+ evicted = true;
13494
+ }
13495
+ if (evicted) expireStaleConsent(state);
13496
+ }
13497
+ function trackSelect(state, windowId, pending) {
13498
+ let perWindow = state.selects.get(windowId);
13499
+ if (!perWindow) {
13500
+ perWindow = /* @__PURE__ */ new Set();
13501
+ state.selects.set(windowId, perWindow);
13502
+ }
13503
+ perWindow.add(pending);
13504
+ return () => {
13505
+ const current = state.selects.get(windowId);
13506
+ current?.delete(pending);
13507
+ if (current?.size === 0) state.selects.delete(windowId);
13508
+ };
13459
13509
  }
13460
13510
  async function handleSelect(state, windowId, message22, send) {
13461
13511
  const id = message22.id;
@@ -13466,9 +13516,11 @@ async function handleSelect(state, windowId, message22, send) {
13466
13516
  }
13467
13517
  const wanted = requirements2 ?? {};
13468
13518
  const startEpoch = state.epoch;
13519
+ const pending = { controller: new AbortController(), id, send, done: false };
13520
+ const untrack = trackSelect(state, windowId, pending);
13469
13521
  try {
13470
- const connection = await state.host.select({ windowId }, wanted);
13471
- if (state.closed) return;
13522
+ const connection = await state.host.select({ windowId }, wanted, { signal: pending.controller.signal });
13523
+ if (pending.done || state.closed) return;
13472
13524
  if (connection === null) {
13473
13525
  reply(send, "llm.select", id, { error: { code: "user-cancelled", message: "The user declined to select a model connection." } });
13474
13526
  return;
@@ -13478,14 +13530,17 @@ async function handleSelect(state, windowId, message22, send) {
13478
13530
  return;
13479
13531
  }
13480
13532
  if (startEpoch !== state.epoch) {
13481
- reply(send, "llm.select", id, { error: { code: "selection-expired", message: "Connections changed during selection; select again." } });
13533
+ reply(send, "llm.select", id, { error: CHANGED_DURING_SELECT });
13482
13534
  return;
13483
13535
  }
13484
13536
  const handle = state.createHandle();
13485
- windowHandles(state, windowId).set(handle, { connectionId: connection.id, snapshot: connection });
13537
+ storeHandle(state, windowId, handle, { connectionId: connection.id, snapshot: connection });
13486
13538
  reply(send, "llm.select", id, { model: toModel(handle, connection) });
13487
13539
  } catch (error) {
13488
- if (!state.closed) reply(send, "llm.select", id, { error: failure(error) });
13540
+ if (!pending.done && !state.closed) reply(send, "llm.select", id, { error: failure(error) });
13541
+ } finally {
13542
+ pending.done = true;
13543
+ untrack();
13489
13544
  }
13490
13545
  }
13491
13546
  async function revalidate(state, windowId, handle) {
@@ -13493,10 +13548,14 @@ async function revalidate(state, windowId, handle) {
13493
13548
  const entry = windowHandles(state, windowId).get(handle);
13494
13549
  if (!entry) return EXPIRED;
13495
13550
  const current = await state.host.describe({ windowId }, entry.connectionId);
13551
+ const entries = windowHandles(state, windowId);
13552
+ if (entries.get(handle) !== entry) return EXPIRED;
13553
+ entries.delete(handle);
13496
13554
  if (!current || !sameConnection(current, entry.snapshot)) {
13497
- windowHandles(state, windowId).delete(handle);
13555
+ expireStaleConsent(state);
13498
13556
  return EXPIRED;
13499
13557
  }
13558
+ entries.set(handle, entry);
13500
13559
  return toModel(handle, current);
13501
13560
  }
13502
13561
  async function handleDescribe(state, windowId, message22, send) {
@@ -13530,6 +13589,18 @@ function validateGeneration(generation2, result2) {
13530
13589
  }
13531
13590
  return result2;
13532
13591
  }
13592
+ async function awaitConsent(state, context, connection, request, generation2) {
13593
+ const prompt = new AbortController();
13594
+ const withdraw = () => prompt.abort();
13595
+ generation2.controller.signal.addEventListener("abort", withdraw, { once: true });
13596
+ generation2.awaitingConsent = true;
13597
+ try {
13598
+ return await state.host.authorize(context, connection, request, { signal: prompt.signal });
13599
+ } finally {
13600
+ generation2.awaitingConsent = false;
13601
+ generation2.controller.signal.removeEventListener("abort", withdraw);
13602
+ }
13603
+ }
13533
13604
  async function runGeneration(state, windowId, requestId, request, generation2, send) {
13534
13605
  const context = { windowId };
13535
13606
  const fail2 = (error) => failGeneration(state, windowId, requestId, generation2, error);
@@ -13546,14 +13617,14 @@ async function runGeneration(state, windowId, requestId, request, generation2, s
13546
13617
  fail2(rejected2);
13547
13618
  return;
13548
13619
  }
13549
- const authorized = await state.host.authorize(context, entry.snapshot, request);
13620
+ const authorized = await awaitConsent(state, context, entry.snapshot, request, generation2);
13550
13621
  if (generation2.settled) return;
13551
13622
  if (!authorized) {
13552
13623
  fail2({ code: "user-cancelled", message: "The user declined to disclose this request." });
13553
13624
  return;
13554
13625
  }
13555
13626
  if (generation2.epoch !== state.epoch || !windowHandles(state, windowId).has(request.handle)) {
13556
- fail2({ code: "selection-expired", message: "The selected connection changed before submission; select again." });
13627
+ fail2(CHANGED_DURING_CONSENT);
13557
13628
  return;
13558
13629
  }
13559
13630
  const result2 = await state.host.generate(context, entry.snapshot, request, {
@@ -13581,6 +13652,8 @@ function handleGenerate(state, windowId, message22, send) {
13581
13652
  const request = message22.request;
13582
13653
  const generation2 = {
13583
13654
  controller: new AbortController(),
13655
+ handle: request.handle,
13656
+ awaitingConsent: false,
13584
13657
  send,
13585
13658
  format: request.format ?? "text",
13586
13659
  epoch: state.epoch,
@@ -13620,15 +13693,18 @@ function createLlmService(options) {
13620
13693
  deadlineMs: options.deadlineMs ?? 12e4,
13621
13694
  maxConcurrent: options.maxConcurrentPerWindow ?? 4,
13622
13695
  maxRequestChars: options.maxRequestChars ?? 4 * 1024 * 1024,
13696
+ maxHandles: Math.max(1, Math.floor(options.maxHandlesPerWindow ?? DEFAULT_MAX_HANDLES)),
13623
13697
  createHandle: options.createHandle ?? defaultHandleFactory(),
13624
13698
  handles: /* @__PURE__ */ new Map(),
13625
13699
  active: /* @__PURE__ */ new Map(),
13700
+ selects: /* @__PURE__ */ new Map(),
13626
13701
  epoch: 0,
13627
13702
  closed: false
13628
13703
  };
13629
13704
  let unsubscribe;
13630
13705
  function revokeWindow(windowId) {
13631
13706
  cancelWindow(state, windowId, CANCELLED);
13707
+ settleSelects(state, windowId, SELECT_REVOKED);
13632
13708
  state.handles.delete(windowId);
13633
13709
  }
13634
13710
  return {
@@ -13640,8 +13716,8 @@ function createLlmService(options) {
13640
13716
  state.closed = false;
13641
13717
  unsubscribe?.();
13642
13718
  unsubscribe = state.host.subscribe?.(() => {
13643
- invalidateHandles(state);
13644
13719
  for (const windowId of state.active.keys()) cancelWindow(state, windowId, CANCELLED);
13720
+ invalidateHandles(state);
13645
13721
  });
13646
13722
  },
13647
13723
  onUnregistered() {
@@ -13649,6 +13725,7 @@ function createLlmService(options) {
13649
13725
  unsubscribe?.();
13650
13726
  unsubscribe = void 0;
13651
13727
  for (const windowId of state.active.keys()) cancelWindow(state, windowId, null);
13728
+ settleSelects(state, null, null);
13652
13729
  state.handles.clear();
13653
13730
  },
13654
13731
  handleMessage(windowId, message22, send) {
@@ -13671,6 +13748,7 @@ function createLlmService(options) {
13671
13748
  },
13672
13749
  onWindowDestroyed(windowId) {
13673
13750
  cancelWindow(state, windowId, null);
13751
+ settleSelects(state, windowId, null);
13674
13752
  state.handles.delete(windowId);
13675
13753
  },
13676
13754
  onCapabilityRevoked(windowId, capability) {
@@ -13721,12 +13799,13 @@ function openAiContent(parts) {
13721
13799
  if (!hasImage(parts)) return textOf(parts);
13722
13800
  return parts.map((part2) => part2.type === "text" ? { type: "text", text: part2.text } : { type: "image_url", image_url: { url: `data:${part2.mimeType};base64,${part2.data}` } });
13723
13801
  }
13724
- function buildOpenAiChat(endpoint, request) {
13802
+ function buildOpenAiChat(endpoint, request, stream) {
13725
13803
  const body = {
13726
13804
  model: endpoint.model,
13727
13805
  messages: request.messages.map((message22) => ({ role: message22.role, content: openAiContent(message22.content) })),
13728
- stream: false
13806
+ stream
13729
13807
  };
13808
+ if (stream) body.stream_options = { include_usage: true };
13730
13809
  if (request.format === "json") body.response_format = { type: "json_object" };
13731
13810
  if (endpoint.maxOutputTokens !== void 0) body.max_tokens = endpoint.maxOutputTokens;
13732
13811
  return {
@@ -13745,16 +13824,21 @@ function parseOpenAiChat(json) {
13745
13824
  const tokens = usageFrom(usage2?.prompt_tokens, usage2?.completion_tokens);
13746
13825
  if (typeof message22.refusal === "string" && message22.refusal.length > 0) return finished("", "refusal", tokens);
13747
13826
  const text2 = typeof message22.content === "string" ? message22.content : "";
13748
- switch (choice.finish_reason) {
13749
- case "stop":
13750
- return finished(text2, "stop", tokens);
13751
- case "length":
13752
- return finished(text2, "length", tokens);
13753
- case "content_filter":
13754
- return finished(text2, "refusal", tokens);
13755
- default:
13756
- throw providerError("The provider response had no recognised completion marker.");
13757
- }
13827
+ return completed(text2, openAiFinish(choice.finish_reason), tokens);
13828
+ }
13829
+ function openAiFinish(reason) {
13830
+ if (reason === "stop") return "stop";
13831
+ if (reason === "length") return "length";
13832
+ return reason === "content_filter" ? "refusal" : null;
13833
+ }
13834
+ function anthropicFinish(reason) {
13835
+ if (reason === "end_turn" || reason === "stop_sequence") return "stop";
13836
+ if (reason === "max_tokens") return "length";
13837
+ return reason === "refusal" ? "refusal" : null;
13838
+ }
13839
+ function completed(text2, finish, usage2) {
13840
+ if (finish === null) throw providerError("The provider response had no recognised completion marker.");
13841
+ return finished(text2, finish, usage2);
13758
13842
  }
13759
13843
  function anthropicBlocks(parts) {
13760
13844
  return parts.map((part2) => part2.type === "text" ? { type: "text", text: part2.text } : { type: "image", source: { type: "base64", media_type: part2.mimeType, data: part2.data } });
@@ -13772,7 +13856,7 @@ function splitSystem(messages) {
13772
13856
  }
13773
13857
  return { system: systemParts.join("\n"), turns };
13774
13858
  }
13775
- function buildAnthropicMessages(endpoint, request) {
13859
+ function buildAnthropicMessages(endpoint, request, stream) {
13776
13860
  const { system, turns } = splitSystem(request.messages);
13777
13861
  if (turns.length === 0 || turns[0].role !== "user") throw unsupported4("This provider requires the first non-system message to come from the user.");
13778
13862
  const merged = [];
@@ -13787,7 +13871,7 @@ function buildAnthropicMessages(endpoint, request) {
13787
13871
  model: endpoint.model,
13788
13872
  max_tokens: endpoint.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS,
13789
13873
  messages: merged,
13790
- stream: false
13874
+ stream
13791
13875
  };
13792
13876
  if (systemText) body.system = systemText;
13793
13877
  return {
@@ -13812,22 +13896,12 @@ function parseAnthropicMessages(json) {
13812
13896
  }).join("");
13813
13897
  const usage2 = record2(root.usage);
13814
13898
  const tokens = usageFrom(usage2?.input_tokens, usage2?.output_tokens);
13815
- switch (root.stop_reason) {
13816
- case "end_turn":
13817
- case "stop_sequence":
13818
- return finished(text2, "stop", tokens);
13819
- case "max_tokens":
13820
- return finished(text2, "length", tokens);
13821
- case "refusal":
13822
- return finished(text2, "refusal", tokens);
13823
- default:
13824
- throw providerError("The provider response had no recognised completion marker.");
13825
- }
13899
+ return completed(text2, anthropicFinish(root.stop_reason), tokens);
13826
13900
  }
13827
13901
  function geminiParts(parts) {
13828
13902
  return parts.map((part2) => part2.type === "text" ? { text: part2.text } : { inlineData: { mimeType: part2.mimeType, data: part2.data } });
13829
13903
  }
13830
- function buildGemini(endpoint, request) {
13904
+ function buildGemini(endpoint, request, stream) {
13831
13905
  const { system, turns } = splitSystem(request.messages);
13832
13906
  if (turns.length === 0) throw unsupported4("This provider requires at least one user or assistant message.");
13833
13907
  const generationConfig = {};
@@ -13839,7 +13913,7 @@ function buildGemini(endpoint, request) {
13839
13913
  if (system) body.systemInstruction = { parts: [{ text: system }] };
13840
13914
  if (Object.keys(generationConfig).length > 0) body.generationConfig = generationConfig;
13841
13915
  return {
13842
- url: `${trimBase(endpoint.baseUrl)}/models/${encodeURIComponent(endpoint.model)}:generateContent`,
13916
+ url: `${trimBase(endpoint.baseUrl)}/models/${encodeURIComponent(endpoint.model)}:${stream ? "streamGenerateContent?alt=sse" : "generateContent"}`,
13843
13917
  method: "POST",
13844
13918
  headers: { "content-type": "application/json", ...endpoint.headers, ...credentialHeaders(endpoint) },
13845
13919
  body: JSON.stringify(body)
@@ -13859,20 +13933,22 @@ function parseGemini(json) {
13859
13933
  const entry = record2(part2);
13860
13934
  return typeof entry?.text === "string" ? entry.text : "";
13861
13935
  }).join("");
13862
- const reason = candidate.finishReason;
13863
- if (reason === "STOP") return finished(text2, "stop", tokens);
13864
- if (reason === "MAX_TOKENS") return finished(text2, "length", tokens);
13865
- if (typeof reason === "string" && GEMINI_REFUSALS.has(reason)) return finished(text2, "refusal", tokens);
13866
- throw providerError("The provider response had no recognised completion marker.");
13936
+ return completed(text2, geminiFinish(candidate.finishReason), tokens);
13867
13937
  }
13868
- function buildLlmProviderRequest(endpoint, request) {
13938
+ function geminiFinish(reason) {
13939
+ if (reason === "STOP") return "stop";
13940
+ if (reason === "MAX_TOKENS") return "length";
13941
+ return typeof reason === "string" && GEMINI_REFUSALS.has(reason) ? "refusal" : null;
13942
+ }
13943
+ function buildLlmProviderRequest(endpoint, request, options = {}) {
13944
+ const stream = options.stream === true;
13869
13945
  switch (endpoint.protocol) {
13870
13946
  case "openai-chat":
13871
- return buildOpenAiChat(endpoint, request);
13947
+ return buildOpenAiChat(endpoint, request, stream);
13872
13948
  case "anthropic-messages":
13873
- return buildAnthropicMessages(endpoint, request);
13949
+ return buildAnthropicMessages(endpoint, request, stream);
13874
13950
  case "gemini":
13875
- return buildGemini(endpoint, request);
13951
+ return buildGemini(endpoint, request, stream);
13876
13952
  default:
13877
13953
  throw unsupported4("Unknown provider protocol.");
13878
13954
  }
@@ -13889,6 +13965,162 @@ function parseLlmProviderResponse(protocol, json) {
13889
13965
  throw providerError("Unknown provider protocol.");
13890
13966
  }
13891
13967
  }
13968
+ var STREAM_ERROR = "The provider reported an error while streaming.";
13969
+ function textField(value) {
13970
+ return typeof value === "string" ? value : "";
13971
+ }
13972
+ function openAiAccumulator() {
13973
+ let text2 = "";
13974
+ let refused = false;
13975
+ let finish = null;
13976
+ let usage2;
13977
+ return {
13978
+ push(event) {
13979
+ const root = record2(event);
13980
+ if (root?.error !== void 0) throw providerError(STREAM_ERROR);
13981
+ const reported = record2(root?.usage);
13982
+ if (reported) usage2 = usageFrom(reported.prompt_tokens, reported.completion_tokens) ?? usage2;
13983
+ const choice = record2(Array.isArray(root?.choices) ? root?.choices[0] : void 0);
13984
+ if (!choice) return "";
13985
+ const delta = record2(choice.delta);
13986
+ if (textField(delta?.refusal).length > 0) refused = true;
13987
+ finish = openAiFinish(choice.finish_reason) ?? finish;
13988
+ const appended = textField(delta?.content);
13989
+ text2 += appended;
13990
+ return appended;
13991
+ },
13992
+ finish: () => refused ? completed("", "refusal", usage2) : completed(text2, finish, usage2)
13993
+ };
13994
+ }
13995
+ function anthropicAccumulator() {
13996
+ let text2 = "";
13997
+ let finish = null;
13998
+ let inputTokens;
13999
+ let outputTokens;
14000
+ return {
14001
+ push(event) {
14002
+ const root = record2(event);
14003
+ switch (root?.type) {
14004
+ case "error":
14005
+ throw providerError(STREAM_ERROR);
14006
+ case "message_start":
14007
+ inputTokens = record2(record2(root.message)?.usage)?.input_tokens;
14008
+ return "";
14009
+ case "message_delta": {
14010
+ finish = anthropicFinish(record2(root.delta)?.stop_reason) ?? finish;
14011
+ const usage2 = record2(root.usage);
14012
+ if (usage2?.output_tokens !== void 0) outputTokens = usage2.output_tokens;
14013
+ if (usage2?.input_tokens !== void 0) inputTokens = usage2.input_tokens;
14014
+ return "";
14015
+ }
14016
+ case "content_block_delta": {
14017
+ const delta = record2(root.delta);
14018
+ const appended = delta?.type === "text_delta" ? textField(delta.text) : "";
14019
+ text2 += appended;
14020
+ return appended;
14021
+ }
14022
+ default:
14023
+ return "";
14024
+ }
14025
+ },
14026
+ finish: () => completed(text2, finish, usageFrom(inputTokens, outputTokens))
14027
+ };
14028
+ }
14029
+ function geminiAccumulator() {
14030
+ let text2 = "";
14031
+ let blocked = false;
14032
+ let finish = null;
14033
+ let usage2;
14034
+ return {
14035
+ push(event) {
14036
+ const root = record2(event);
14037
+ if (root?.error !== void 0) throw providerError(STREAM_ERROR);
14038
+ const metadata = record2(root?.usageMetadata);
14039
+ if (metadata) usage2 = usageFrom(metadata.promptTokenCount, metadata.candidatesTokenCount) ?? usage2;
14040
+ if (typeof record2(root?.promptFeedback)?.blockReason === "string") blocked = true;
14041
+ const candidate = record2(Array.isArray(root?.candidates) ? root?.candidates[0] : void 0);
14042
+ if (!candidate) return "";
14043
+ finish = geminiFinish(candidate.finishReason) ?? finish;
14044
+ const parts = record2(candidate.content)?.parts;
14045
+ const appended = Array.isArray(parts) ? parts.map((part2) => textField(record2(part2)?.text)).join("") : "";
14046
+ text2 += appended;
14047
+ return appended;
14048
+ },
14049
+ finish: () => blocked ? completed("", "refusal", usage2) : completed(text2, finish, usage2)
14050
+ };
14051
+ }
14052
+ function createLlmStreamAccumulator(protocol) {
14053
+ switch (protocol) {
14054
+ case "openai-chat":
14055
+ return openAiAccumulator();
14056
+ case "anthropic-messages":
14057
+ return anthropicAccumulator();
14058
+ case "gemini":
14059
+ return geminiAccumulator();
14060
+ default:
14061
+ throw providerError("Unknown provider protocol.");
14062
+ }
14063
+ }
14064
+ function nextLineBreak(buffer, final) {
14065
+ const index = buffer.search(/[\r\n]/);
14066
+ if (index === -1) return null;
14067
+ if (buffer[index] === "\n") return { index, length: 1 };
14068
+ if (index === buffer.length - 1 && !final) return null;
14069
+ return { index, length: buffer[index + 1] === "\n" ? 2 : 1 };
14070
+ }
14071
+ function drainLines(state, final, events) {
14072
+ for (let found = nextLineBreak(state.buffer, final); found; found = nextLineBreak(state.buffer, final)) {
14073
+ const line = state.buffer.slice(0, found.index);
14074
+ state.buffer = state.buffer.slice(found.index + found.length);
14075
+ if (line === "") {
14076
+ if (state.data.length > 0) events.push(state.data.join("\n"));
14077
+ state.data = [];
14078
+ continue;
14079
+ }
14080
+ if (line.startsWith(":")) continue;
14081
+ const colon = line.indexOf(":");
14082
+ const field = colon === -1 ? line : line.slice(0, colon);
14083
+ if (field !== "data") continue;
14084
+ const value = colon === -1 ? "" : line.slice(colon + 1);
14085
+ state.data.push(value.startsWith(" ") ? value.slice(1) : value);
14086
+ }
14087
+ }
14088
+ async function* readLlmSseData(body, signal) {
14089
+ const reader = body.getReader();
14090
+ const decoder = new TextDecoder();
14091
+ const state = { buffer: "", data: [] };
14092
+ const cancel = () => {
14093
+ void reader.cancel().catch(() => void 0);
14094
+ };
14095
+ let complete = false;
14096
+ signal?.addEventListener("abort", cancel, { once: true });
14097
+ try {
14098
+ for (; ; ) {
14099
+ if (signal?.aborted) return;
14100
+ const { done, value } = await reader.read();
14101
+ const events = [];
14102
+ if (done) {
14103
+ complete = true;
14104
+ state.buffer += decoder.decode();
14105
+ drainLines(state, true, events);
14106
+ if (state.buffer !== "") {
14107
+ state.buffer += "\n";
14108
+ drainLines(state, true, events);
14109
+ }
14110
+ if (state.data.length > 0) events.push(state.data.join("\n"));
14111
+ yield* events;
14112
+ return;
14113
+ }
14114
+ state.buffer += decoder.decode(value, { stream: true });
14115
+ drainLines(state, false, events);
14116
+ yield* events;
14117
+ }
14118
+ } finally {
14119
+ signal?.removeEventListener("abort", cancel);
14120
+ if (!complete) cancel();
14121
+ reader.releaseLock();
14122
+ }
14123
+ }
13892
14124
  var PRESET_ROWS = [
13893
14125
  { id: "openai", label: "OpenAI", protocol: "openai-chat", baseUrl: "https://api.openai.com/v1", requiresCredential: true, local: false, docsUrl: "https://platform.openai.com/docs/api-reference/chat", exampleModels: ["gpt-4.1-mini", "gpt-4o-mini"] },
13894
14126
  { id: "anthropic", label: "Anthropic", protocol: "anthropic-messages", baseUrl: "https://api.anthropic.com/v1", requiresCredential: true, local: false, docsUrl: "https://docs.anthropic.com/en/api/messages", exampleModels: ["claude-sonnet-4-5", "claude-haiku-4-5"] },
@@ -13911,10 +14143,44 @@ var PRESET_ROWS = [
13911
14143
  var LLM_PROVIDER_PRESETS = Object.freeze(
13912
14144
  PRESET_ROWS.map((preset) => Object.freeze({ ...preset, exampleModels: Object.freeze([...preset.exampleModels]) }))
13913
14145
  );
14146
+ async function readJson(response) {
14147
+ try {
14148
+ return await response.json();
14149
+ } catch {
14150
+ throw new LlmServiceError("provider-error", "The provider response was not valid JSON.");
14151
+ }
14152
+ }
14153
+ function parseStreamEvent(data) {
14154
+ try {
14155
+ return JSON.parse(data);
14156
+ } catch {
14157
+ throw new LlmServiceError("provider-error", "The provider stream carried an invalid event.");
14158
+ }
14159
+ }
14160
+ async function readStream(protocol, body, signal, onDelta) {
14161
+ const accumulator = createLlmStreamAccumulator(protocol);
14162
+ try {
14163
+ for await (const data of readLlmSseData(body, signal)) {
14164
+ if (data === "[DONE]") continue;
14165
+ const appended = accumulator.push(parseStreamEvent(data));
14166
+ if (appended && !signal.aborted) onDelta?.(appended);
14167
+ }
14168
+ } catch (error) {
14169
+ if (signal.aborted) throw abortReason(signal);
14170
+ if (error instanceof LlmServiceError) throw error;
14171
+ throw new LlmServiceError("provider-error", "The provider stream was interrupted.");
14172
+ }
14173
+ if (signal.aborted) throw abortReason(signal);
14174
+ return accumulator.finish();
14175
+ }
14176
+ function abortReason(signal) {
14177
+ return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
14178
+ }
13914
14179
  function createHttpLlmProvider(options = {}) {
13915
14180
  const fetchImpl = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
13916
- return async (endpoint, request, signal) => {
13917
- const http = buildLlmProviderRequest(endpoint, request);
14181
+ const stream = options.stream === true;
14182
+ return async (endpoint, request, signal, onDelta) => {
14183
+ const http = buildLlmProviderRequest(endpoint, request, { stream });
13918
14184
  let response;
13919
14185
  try {
13920
14186
  response = await fetchImpl(http.url, { method: http.method, headers: { ...http.headers }, body: http.body, signal });
@@ -13923,13 +14189,10 @@ function createHttpLlmProvider(options = {}) {
13923
14189
  throw new LlmServiceError("provider-error", "The provider could not be reached.");
13924
14190
  }
13925
14191
  if (!response.ok) throw new LlmServiceError("provider-error", `The provider responded with HTTP ${response.status}.`);
13926
- let json;
13927
- try {
13928
- json = await response.json();
13929
- } catch {
13930
- throw new LlmServiceError("provider-error", "The provider response was not valid JSON.");
13931
- }
13932
- return parseLlmProviderResponse(endpoint.protocol, json);
14192
+ const buffered = !stream || response.headers?.get("content-type")?.toLowerCase().includes("application/json") === true;
14193
+ if (buffered) return parseLlmProviderResponse(endpoint.protocol, await readJson(response));
14194
+ if (!response.body) throw new LlmServiceError("provider-error", "The provider response had no readable stream.");
14195
+ return readStream(endpoint.protocol, response.body, signal, onDelta);
13933
14196
  };
13934
14197
  }
13935
14198
 
@@ -14649,8 +14912,8 @@ function countCharacters(request) {
14649
14912
  }
14650
14913
  function createPajaLlmHost(options) {
14651
14914
  const { store: store2, vault, confirm, getIdentity } = options;
14652
- const provider = options.provider ?? createHttpLlmProvider();
14653
- async function ensureCredential(record3) {
14915
+ const provider = options.provider ?? createHttpLlmProvider({ stream: true });
14916
+ async function ensureCredential(record3, signal) {
14654
14917
  if (record3.credentialStorage === "none") return void 0;
14655
14918
  const existing = vault.getCredential(record3.id);
14656
14919
  if (existing !== void 0) return existing;
@@ -14659,7 +14922,7 @@ function createPajaLlmHost(options) {
14659
14922
  throw new LlmServiceError("unsupported", "This connection's API key was kept for one session only. Enter it again in Language models.");
14660
14923
  }
14661
14924
  if (!vault.isUnlocked()) {
14662
- const unlocked = await options.requestUnlock();
14925
+ const unlocked = await options.requestUnlock(signal);
14663
14926
  if (!unlocked) throw new LlmServiceError("user-cancelled", "The credential vault stayed locked.");
14664
14927
  }
14665
14928
  const credential = vault.getCredential(record3.id);
@@ -14669,16 +14932,17 @@ function createPajaLlmHost(options) {
14669
14932
  }
14670
14933
  return credential;
14671
14934
  }
14672
- async function select2(context, requirements2) {
14935
+ async function select2(context, requirements2, prompt) {
14936
+ const signal = prompt?.signal;
14673
14937
  const napplet = getIdentity(context.windowId);
14674
14938
  const candidates = store2.list().filter((record3) => meets(record3, requirements2));
14675
14939
  if (candidates.length === 0) {
14676
- const configure = await confirm({ action: "llm-configure", windowId: context.windowId, napplet, requirements: describeRequirements(requirements2), configured: store2.list().length });
14940
+ const configure = await confirm({ action: "llm-configure", windowId: context.windowId, napplet, requirements: describeRequirements(requirements2), configured: store2.list().length, ...signal ? { signal } : {} });
14677
14941
  if (!configure) return null;
14678
14942
  options.openPanel();
14679
14943
  throw new LlmServiceError("unsupported", "No compatible language model connection is configured yet. Add one in Paja's Language models panel, then select again.");
14680
14944
  }
14681
- const chosen = await options.chooseConnection(candidates, { windowId: context.windowId, napplet, requirements: requirements2 });
14945
+ const chosen = await options.chooseConnection(candidates, { windowId: context.windowId, napplet, requirements: requirements2, ...signal ? { signal } : {} });
14682
14946
  if (!chosen) return null;
14683
14947
  const current = store2.get(chosen.id);
14684
14948
  return current && meets(current, requirements2) ? toConnection(current) : null;
@@ -14690,11 +14954,14 @@ function createPajaLlmHost(options) {
14690
14954
  const record3 = store2.get(connectionId);
14691
14955
  return record3 ? toConnection(record3) : null;
14692
14956
  },
14693
- async authorize(context, connection, request) {
14957
+ async authorize(context, connection, request, prompt) {
14958
+ const signal = prompt?.signal;
14694
14959
  const record3 = store2.get(connection.id);
14695
14960
  if (!record3) throw new LlmServiceError("selection-expired", "The selected connection was removed; select again.");
14696
- await ensureCredential(record3);
14961
+ await ensureCredential(record3, signal);
14962
+ if (signal?.aborted) return false;
14697
14963
  return confirm({
14964
+ ...signal ? { signal } : {},
14698
14965
  action: "llm",
14699
14966
  windowId: context.windowId,
14700
14967
  napplet: getIdentity(context.windowId),
@@ -14718,7 +14985,7 @@ function createPajaLlmHost(options) {
14718
14985
  model: record3.model,
14719
14986
  ...credential === void 0 ? {} : { credential },
14720
14987
  ...record3.credentialHeader ? { credentialHeader: record3.credentialHeader } : {}
14721
- }, request, hooks.signal);
14988
+ }, request, hooks.signal, (text2) => hooks.onDelta(text2));
14722
14989
  },
14723
14990
  subscribe(listener) {
14724
14991
  const stopStore = store2.subscribe(listener);
@@ -14895,6 +15162,11 @@ function defaultId() {
14895
15162
  const random = typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID().slice(0, 8) : Math.random().toString(16).slice(2, 10);
14896
15163
  return `conn-${random}`;
14897
15164
  }
15165
+ function withdrawOnAbort(signal, withdraw) {
15166
+ if (!signal) return () => void 0;
15167
+ signal.addEventListener("abort", withdraw, { once: true });
15168
+ return () => signal.removeEventListener("abort", withdraw);
15169
+ }
14898
15170
  function createPasswordDialog() {
14899
15171
  const dialog = byId("paja-llm-vault-dialog", HTMLDialogElement);
14900
15172
  const title = byId("paja-llm-vault-title", HTMLElement);
@@ -14932,14 +15204,22 @@ function createPasswordDialog() {
14932
15204
  cancel.addEventListener("click", onCancel);
14933
15205
  dialog.addEventListener("cancel", onCancel);
14934
15206
  return {
14935
- prompt(mode) {
15207
+ prompt(mode, signal) {
14936
15208
  if (settle7) finish(null);
15209
+ if (signal?.aborted) return Promise.resolve(null);
14937
15210
  dialog.dataset.mode = mode;
14938
15211
  title.textContent = mode === "create" ? "Create a credential vault password" : "Unlock the credential vault";
14939
15212
  copy.textContent = mode === "create" ? `Paja encrypts remembered API keys with this password (PBKDF2-SHA-256 and AES-GCM). It is never stored or sent anywhere; use at least ${String(12)} characters.` : "Enter the vault password to use a remembered API key for this session.";
14940
15213
  confirm.textContent = mode === "create" ? "Create vault" : "Unlock";
14941
15214
  return new Promise((resolve) => {
14942
- settle7 = resolve;
15215
+ const mine = (value) => {
15216
+ detach();
15217
+ resolve(value);
15218
+ };
15219
+ const detach = withdrawOnAbort(signal, () => {
15220
+ if (settle7 === mine) finish(null);
15221
+ });
15222
+ settle7 = mine;
14943
15223
  dialog.showModal();
14944
15224
  input.focus();
14945
15225
  });
@@ -14977,6 +15257,7 @@ function createChooserDialog() {
14977
15257
  return {
14978
15258
  choose(candidates, context) {
14979
15259
  if (settle7) finish(null);
15260
+ if (context.signal?.aborted) return Promise.resolve(null);
14980
15261
  const wanted = [context.requirements.model ? `model ${context.requirements.model}` : null, context.requirements.images ? "image input" : null, context.requirements.json ? "JSON output" : null].filter(Boolean).join(", ");
14981
15262
  copy.textContent = `${context.napplet.dTag} (${context.windowId}) asks to use a language model${wanted ? ` with ${wanted}` : ""}. Selecting does not send anything yet; each generation asks for consent.`;
14982
15263
  select2.replaceChildren(...candidates.map((record3) => {
@@ -14987,7 +15268,14 @@ function createChooserDialog() {
14987
15268
  }));
14988
15269
  select2.selectedIndex = candidates.length > 0 ? 0 : -1;
14989
15270
  return new Promise((resolve) => {
14990
- settle7 = (value) => resolve(candidates.find((record3) => record3.id === value) ?? null);
15271
+ const mine = (value) => {
15272
+ detach();
15273
+ resolve(candidates.find((record3) => record3.id === value) ?? null);
15274
+ };
15275
+ const detach = withdrawOnAbort(context.signal, () => {
15276
+ if (settle7 === mine) finish(null);
15277
+ });
15278
+ settle7 = mine;
14991
15279
  dialog.showModal();
14992
15280
  select2.focus();
14993
15281
  });
@@ -15039,10 +15327,10 @@ function installPajaLlmPanel(options) {
15039
15327
  }
15040
15328
  return requestUnlock();
15041
15329
  }
15042
- async function requestUnlock() {
15330
+ async function requestUnlock(signal) {
15043
15331
  for (; ; ) {
15044
15332
  if (vault.isUnlocked()) return true;
15045
- const entered = await password.prompt("unlock");
15333
+ const entered = await password.prompt("unlock", signal);
15046
15334
  if (entered === null) return false;
15047
15335
  if (await vault.unlock(entered)) return true;
15048
15336
  setStatus2("Wrong vault password. Try again or clear the vault to start over.", true);
@@ -25605,12 +25893,22 @@ function createPajaConfirmationController(getState, options = {}) {
25605
25893
  };
25606
25894
  return {
25607
25895
  confirm(request) {
25608
- if (!ready || disposed) {
25896
+ if (!ready || disposed || request.signal?.aborted) {
25609
25897
  recordPajaConfirmation(getState(), request, false);
25610
25898
  return false;
25611
25899
  }
25900
+ const { signal } = request;
25612
25901
  return new Promise((resolve) => {
25613
- queue.push({ request, approve: () => resolve(true), deny: () => resolve(false) });
25902
+ const withdraw = () => cancelMatching((candidate) => candidate === request);
25903
+ const detach = () => signal?.removeEventListener("abort", withdraw);
25904
+ signal?.addEventListener("abort", withdraw, { once: true });
25905
+ queue.push({ request, approve: () => {
25906
+ detach();
25907
+ resolve(true);
25908
+ }, deny: () => {
25909
+ detach();
25910
+ resolve(false);
25911
+ } });
25614
25912
  pump();
25615
25913
  });
25616
25914
  },
@@ -25675,6 +25973,7 @@ function signerConsentCandidate(request) {
25675
25973
  return { context: request.signerContext, kind };
25676
25974
  }
25677
25975
  function isRequestCurrent(request) {
25976
+ if (request.signal?.aborted) return false;
25678
25977
  if (request.isCurrent && !request.isCurrent()) return false;
25679
25978
  return !("signerContext" in request) || !request.signerContext?.isCurrent || request.signerContext.isCurrent();
25680
25979
  }