@codelia/runtime 0.1.89 → 0.1.90

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.
@@ -325,6 +325,7 @@ var updateTuiTheme = async (workingDir, theme) => {
325
325
  await updateTuiConfig(target.path, { theme });
326
326
  return target;
327
327
  };
328
+ var normalizeModelLevel = (value) => value.trim().toLowerCase();
328
329
  var resolveReasoningEffort = (value) => {
329
330
  return resolveModelLevelOption(value, "model.reasoning", {
330
331
  allowXhigh: true
@@ -337,7 +338,7 @@ var resolveTextVerbosity = (value) => {
337
338
  };
338
339
  function resolveModelLevelOption(value, fieldName, options) {
339
340
  if (!value) return void 0;
340
- const normalized = value.trim().toLowerCase();
341
+ const normalized = normalizeModelLevel(value);
341
342
  if (normalized === "low" || normalized === "medium" || normalized === "high") {
342
343
  return normalized;
343
344
  }
@@ -367,16 +368,20 @@ Working directory: ${workingDir}`;
367
368
 
368
369
  // src/rpc/ui-requests.ts
369
370
  import {
370
- RPC_ERROR_CODE
371
+ RPC_ERROR_CODE as RPC_ERROR_CODE2
371
372
  } from "@codelia/protocol";
372
373
 
373
- // src/rpc/transport.ts
374
- import { AsyncLocalStorage } from "async_hooks";
375
-
376
374
  // src/logger.ts
377
375
  import { debugLog, isDebugEnabled, log } from "@codelia/logger";
378
376
 
377
+ // src/rpc/protocol.ts
378
+ import {
379
+ protocolMethods,
380
+ RPC_ERROR_CODE
381
+ } from "@codelia/protocol";
382
+
379
383
  // src/rpc/transport.ts
384
+ import { AsyncLocalStorage } from "async_hooks";
380
385
  var responseContext = new AsyncLocalStorage();
381
386
  var withRpcResponseContext = (id, callback) => responseContext.run({ id, sent: false }, callback);
382
387
  var hasRpcResponse = (id) => {
@@ -477,65 +482,107 @@ var sendResponse = (response) => {
477
482
  var sendAgentEvent = (state, runId, event) => {
478
483
  if (state.shouldSuppressEvent(runId)) return null;
479
484
  const seq = state.nextSequence(runId);
480
- const notify = {
481
- jsonrpc: "2.0",
482
- method: "agent.event",
483
- params: {
484
- run_id: runId,
485
- seq,
486
- event
487
- }
488
- };
489
- sendNotification(state, notify);
485
+ sendNotification(
486
+ state,
487
+ notificationMessage("agent.event", { run_id: runId, seq, event })
488
+ );
490
489
  return seq;
491
490
  };
492
491
  var sendRunStatus = (state, runId, status, message, error, extras) => {
493
- const notify = {
494
- jsonrpc: "2.0",
495
- method: "run.status",
496
- params: {
497
- run_id: runId,
498
- status,
499
- message,
500
- ...error ? { error } : {},
501
- ...extras?.unconsumed_steer_ids?.length ? { unconsumed_steer_ids: extras.unconsumed_steer_ids } : {}
502
- }
503
- };
492
+ const notify = notificationMessage("run.status", {
493
+ run_id: runId,
494
+ status,
495
+ message,
496
+ ...error ? { error } : {},
497
+ ...extras?.unconsumed_steer_ids?.length ? { unconsumed_steer_ids: extras.unconsumed_steer_ids } : {},
498
+ ...extras?.unconsumed_client_steer_ids?.length ? { unconsumed_client_steer_ids: extras.unconsumed_client_steer_ids } : {}
499
+ });
504
500
  sendNotification(state, notify);
505
501
  };
506
502
  var sendRunContext = (state, runId, contextLeftPercent) => {
507
- const notify = {
508
- jsonrpc: "2.0",
509
- method: "run.context",
510
- params: {
503
+ sendNotification(
504
+ state,
505
+ notificationMessage("run.context", {
511
506
  run_id: runId,
512
507
  context_left_percent: contextLeftPercent
513
- }
514
- };
515
- sendNotification(state, notify);
508
+ })
509
+ );
516
510
  };
517
511
  var sendRunDiagnostics = (state, params) => {
518
- const notify = {
519
- jsonrpc: "2.0",
520
- method: "run.diagnostics",
521
- params
522
- };
523
- sendNotification(state, notify);
512
+ sendNotification(state, notificationMessage("run.diagnostics", params));
524
513
  };
525
514
  var sendRunMetrics = (state, params) => {
526
- sendNotification(state, { jsonrpc: "2.0", method: "run.metrics", params });
515
+ sendNotification(state, notificationMessage("run.metrics", params));
516
+ };
517
+
518
+ // src/rpc/protocol.ts
519
+ var MAX_ISSUES = 10;
520
+ var describeIssues = (error) => error.issues.slice(0, MAX_ISSUES).map((issue) => ({
521
+ path: issue.path.map(String).join("."),
522
+ code: issue.code,
523
+ message: issue.message
524
+ }));
525
+ var schemaFor = (method, slot) => {
526
+ const spec = protocolMethods[method];
527
+ if (slot === "params") return spec.params;
528
+ if (!("result" in spec))
529
+ throw new Error(`${method} is a notification without a result`);
530
+ return spec.result;
531
+ };
532
+ var assertOutgoing = (method, slot, value) => {
533
+ if (process.env.CODELIA_PROTOCOL_ASSERT !== "1") return;
534
+ const result = schemaFor(method, slot).safeParse(value);
535
+ if (!result.success)
536
+ throw new Error(
537
+ `protocol assertion failed for ${method} ${slot}: ${JSON.stringify(describeIssues(result.error))}`
538
+ );
539
+ };
540
+ var parseRequestParams = (method, raw) => {
541
+ const result = schemaFor(method, "params").safeParse(
542
+ raw === void 0 ? {} : raw
543
+ );
544
+ if (result.success) return { ok: true, params: result.data };
545
+ const issues = describeIssues(result.error);
546
+ const first = issues[0];
547
+ return {
548
+ ok: false,
549
+ error: {
550
+ code: RPC_ERROR_CODE.INVALID_PARAMS,
551
+ // The first issue names the field; issue messages never echo values.
552
+ message: first ? `invalid ${method} params: ${first.path || "params"}: ${first.message}` : `invalid ${method} params`,
553
+ data: { issues }
554
+ }
555
+ };
556
+ };
557
+ var parseUiNotificationParams = (method, raw) => {
558
+ const result = schemaFor(method, "params").safeParse(
559
+ raw === void 0 ? {} : raw
560
+ );
561
+ return result.success ? result.data : null;
562
+ };
563
+ var sendRequestResult = (method, id, result) => {
564
+ assertOutgoing(method, "result", result);
565
+ sendResult(id, result);
566
+ };
567
+ var notificationMessage = (method, params) => {
568
+ assertOutgoing(method, "params", params);
569
+ return { jsonrpc: "2.0", method, params };
570
+ };
571
+ var requestMessage = (method, id, params) => {
572
+ assertOutgoing(method, "params", params);
573
+ return { jsonrpc: "2.0", id, method, params };
574
+ };
575
+ var parseUiResult = (method, raw) => {
576
+ const result = schemaFor(method, "result").safeParse(raw);
577
+ if (result.success) return { ok: true, result: result.data };
578
+ return { ok: false, issues: describeIssues(result.error) };
527
579
  };
528
580
 
529
581
  // src/rpc/ui-requests.ts
530
582
  var requestUi = async (state, method, params, signal) => {
531
583
  if (signal?.aborted) return null;
532
584
  const id = state.nextUiRequestId();
533
- const request = {
534
- jsonrpc: "2.0",
535
- id,
536
- method,
537
- params
538
- };
585
+ const request = requestMessage(method, id, params);
539
586
  const response = state.waitForUiResponse(id);
540
587
  let sent = false;
541
588
  const abort = () => {
@@ -543,13 +590,12 @@ var requestUi = async (state, method, params, signal) => {
543
590
  jsonrpc: "2.0",
544
591
  id,
545
592
  error: {
546
- code: RPC_ERROR_CODE.RUNTIME_INTERNAL,
593
+ code: RPC_ERROR_CODE2.RUNTIME_INTERNAL,
547
594
  message: "UI request cancelled"
548
595
  }
549
596
  });
550
- if (cancelled && sent && method === "ui.pick.request") {
551
- send({ jsonrpc: "2.0", method: "ui.pick.cancelled", params: { id } });
552
- }
597
+ if (!cancelled || !sent || method !== "ui.pick.request") return;
598
+ send(notificationMessage("ui.pick.cancelled", { id }));
553
599
  };
554
600
  signal?.addEventListener("abort", abort, { once: true });
555
601
  try {
@@ -558,7 +604,12 @@ var requestUi = async (state, method, params, signal) => {
558
604
  send(request);
559
605
  sent = true;
560
606
  }
561
- return await response;
607
+ const parsed = parseUiResult(method, await response);
608
+ if (parsed.ok) return parsed.result;
609
+ log(
610
+ `protocol mismatch: ${method} result: ${JSON.stringify(parsed.issues)}`
611
+ );
612
+ return null;
562
613
  } catch {
563
614
  return null;
564
615
  } finally {
@@ -5438,6 +5489,7 @@ var RuntimeState = class {
5438
5489
  authority
5439
5490
  ]);
5440
5491
  }
5492
+ acceptedSteerIds = /* @__PURE__ */ new Map();
5441
5493
  pendingSteersByRun = /* @__PURE__ */ new Map();
5442
5494
  steerClosedRuns = /* @__PURE__ */ new Set();
5443
5495
  uiRequestCounter = 0;
@@ -5572,6 +5624,7 @@ var RuntimeState = class {
5572
5624
  this.lastContextLeftPercent = null;
5573
5625
  this.runSeq.delete(runId);
5574
5626
  this.pendingSteersByRun.delete(runId);
5627
+ this.acceptedSteerIds.delete(runId);
5575
5628
  this.steerClosedRuns.delete(runId);
5576
5629
  if (this.activeRunId === null) {
5577
5630
  this.sessionAppend = null;
@@ -5584,10 +5637,18 @@ var RuntimeState = class {
5584
5637
  canSteerRun(runId) {
5585
5638
  return this.activeRunId === runId && !this.cancelRequested && !this.steerClosedRuns.has(runId);
5586
5639
  }
5640
+ acceptedRunSteerId(runId, clientSteerId) {
5641
+ return this.acceptedSteerIds.get(runId)?.get(clientSteerId);
5642
+ }
5587
5643
  enqueueRunSteer(runId, content, clientSteerId, approvalUserText) {
5588
5644
  this.signalApprovalChange();
5589
5645
  this.runSteerCounter += 1;
5590
5646
  const steerId = `s${this.runSteerCounter}`;
5647
+ if (clientSteerId !== void 0) {
5648
+ const accepted = this.acceptedSteerIds.get(runId) ?? /* @__PURE__ */ new Map();
5649
+ accepted.set(clientSteerId, steerId);
5650
+ this.acceptedSteerIds.set(runId, accepted);
5651
+ }
5591
5652
  if (this.approvalIntent?.runId === runId) {
5592
5653
  this.approvalIntent.steers.push({
5593
5654
  id: steerId,
@@ -5598,7 +5659,7 @@ var RuntimeState = class {
5598
5659
  const queue = this.pendingSteersByRun.get(runId) ?? [];
5599
5660
  queue.push({
5600
5661
  steer_id: steerId,
5601
- ...clientSteerId ? { client_steer_id: clientSteerId } : {},
5662
+ ...clientSteerId !== void 0 ? { client_steer_id: clientSteerId } : {},
5602
5663
  content
5603
5664
  });
5604
5665
  this.pendingSteersByRun.set(runId, queue);
@@ -7846,6 +7907,7 @@ export {
7846
7907
  resolveMcpServers,
7847
7908
  updateModel,
7848
7909
  updateTuiTheme,
7910
+ normalizeModelLevel,
7849
7911
  resolveReasoningEffort,
7850
7912
  loadSystemPrompt,
7851
7913
  builtinApiKeyBase,
@@ -7855,12 +7917,17 @@ export {
7855
7917
  sendNotificationAsync,
7856
7918
  send,
7857
7919
  sendError,
7858
- sendResult,
7859
7920
  sendAgentEvent,
7860
7921
  sendRunStatus,
7861
7922
  sendRunContext,
7862
7923
  sendRunDiagnostics,
7863
7924
  sendRunMetrics,
7925
+ parseRequestParams,
7926
+ parseUiNotificationParams,
7927
+ sendRequestResult,
7928
+ notificationMessage,
7929
+ requestMessage,
7930
+ parseUiResult,
7864
7931
  requestUiConfirm,
7865
7932
  requestUiPrompt,
7866
7933
  requestUiPick,