@lobu/runtime-device 20.2.0 → 21.1.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.
Files changed (126) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +44 -38
  3. package/npm-shrinkwrap.json +277 -231
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.d.ts +7 -2
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +77 -79
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +41 -29
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +33 -47
  9. package/vendor/cli/dist/commands/_lib/apply/deployment.d.ts +1 -1
  10. package/vendor/cli/dist/commands/_lib/apply/deployment.js +2 -3
  11. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +47 -13
  12. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +106 -7
  13. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +14 -7
  14. package/vendor/cli/dist/commands/_lib/apply/diff.js +42 -29
  15. package/vendor/cli/dist/commands/_lib/apply/map-config.js +2 -3
  16. package/vendor/cli/dist/commands/_lib/apply/render.js +2 -0
  17. package/vendor/cli/dist/commands/_lib/apply/rollback-cmd.js +3 -3
  18. package/vendor/cli/dist/commands/_lib/connector-loader.d.ts +5 -1
  19. package/vendor/cli/dist/commands/_lib/connector-loader.js +1 -1
  20. package/vendor/cli/dist/commands/_lib/ensure-deps-installed.d.ts +15 -26
  21. package/vendor/cli/dist/commands/_lib/ensure-deps-installed.js +227 -136
  22. package/vendor/cli/dist/commands/_lib/view-bundler.d.ts +33 -0
  23. package/vendor/cli/dist/commands/_lib/view-bundler.js +211 -0
  24. package/vendor/cli/dist/commands/_lib/view-reapply.d.ts +60 -0
  25. package/vendor/cli/dist/commands/_lib/view-reapply.js +182 -0
  26. package/vendor/cli/dist/commands/dev.d.ts +14 -5
  27. package/vendor/cli/dist/commands/dev.js +56 -8
  28. package/vendor/cli/dist/commands/init.d.ts +1 -1
  29. package/vendor/cli/dist/commands/init.js +4 -4
  30. package/vendor/cli/dist/commands/memory/_lib/schema.d.ts +0 -1
  31. package/vendor/cli/dist/commands/memory/_lib/schema.js +0 -9
  32. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +0 -2
  33. package/vendor/cli/dist/config/define.d.ts +25 -8
  34. package/vendor/cli/dist/config/define.js +24 -0
  35. package/vendor/cli/dist/connectors/README.md +15 -16
  36. package/vendor/cli/dist/connectors/github.ts +274 -193
  37. package/vendor/cli/dist/connectors/google_calendar.ts +54 -160
  38. package/vendor/cli/dist/connectors/google_drive.ts +106 -131
  39. package/vendor/cli/dist/connectors/google_gmail.ts +53 -30
  40. package/vendor/cli/dist/connectors/hackernews.ts +2 -5
  41. package/vendor/cli/dist/connectors/jira.ts +71 -27
  42. package/vendor/cli/dist/connectors/linear.ts +55 -21
  43. package/vendor/cli/dist/connectors/microsoft_outlook.ts +151 -110
  44. package/vendor/cli/dist/connectors/postgres.ts +19 -11
  45. package/vendor/cli/dist/connectors/producthunt.ts +11 -7
  46. package/vendor/cli/dist/connectors/reddit.ts +36 -6
  47. package/vendor/cli/dist/connectors/rss.ts +2 -2
  48. package/vendor/cli/dist/connectors/whatsapp_web.ts +5 -4
  49. package/vendor/cli/dist/connectors/x.ts +32 -17
  50. package/vendor/cli/dist/connectors/youtube.ts +18 -5
  51. package/vendor/cli/dist/internal/connector-compiler.d.ts +1 -1
  52. package/vendor/cli/dist/internal/connector-compiler.js +216 -17
  53. package/vendor/cli/dist/providers.json +0 -1
  54. package/vendor/cli/dist/runtime-components.json +4 -4
  55. package/vendor/cli/package.json +2 -1
  56. package/vendor/connector-sdk/dist/connector-runtime.d.ts +4 -6
  57. package/vendor/connector-sdk/dist/connector-runtime.js +3 -5
  58. package/vendor/connector-sdk/dist/connector-types.d.ts +28 -15
  59. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -1
  60. package/vendor/connector-sdk/dist/define-connector.js +4 -1
  61. package/vendor/connector-sdk/dist/http-client.d.ts +18 -7
  62. package/vendor/connector-sdk/dist/http-client.js +15 -13
  63. package/vendor/connector-sdk/dist/index.d.ts +1 -1
  64. package/vendor/connector-sdk/dist/retry.d.ts +15 -3
  65. package/vendor/connector-sdk/dist/retry.js +40 -64
  66. package/vendor/connector-sdk/package.json +1 -1
  67. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +122 -10
  68. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +78 -10
  69. package/vendor/connector-worker/dist/agent-turn/types.d.ts +34 -4
  70. package/vendor/connector-worker/dist/compile/index.d.ts +21 -2
  71. package/vendor/connector-worker/dist/compile/index.js +41 -8
  72. package/vendor/connector-worker/dist/compile/source-capture.d.ts +10 -0
  73. package/vendor/connector-worker/dist/compile/source-capture.js +108 -0
  74. package/vendor/connector-worker/dist/daemon/agent-turn.js +97 -10
  75. package/vendor/connector-worker/dist/daemon/automation.d.ts +1 -1
  76. package/vendor/connector-worker/dist/daemon/client.d.ts +4 -1
  77. package/vendor/connector-worker/dist/daemon/client.js +3 -0
  78. package/vendor/connector-worker/dist/daemon/executor.d.ts +0 -1
  79. package/vendor/connector-worker/dist/daemon/executor.js +46 -51
  80. package/vendor/connector-worker/dist/daemon/start.js +6 -2
  81. package/vendor/connector-worker/dist/executor/interface.d.ts +19 -9
  82. package/vendor/connector-worker/dist/executor/isolate.js +85 -28
  83. package/vendor/connector-worker/dist/self-check/index.js +15 -13
  84. package/vendor/connector-worker/package.json +1 -1
  85. package/vendor/core/dist/agent-policy.js +3 -0
  86. package/vendor/core/dist/connector-query-errors.d.ts +3 -3
  87. package/vendor/core/dist/connector-query-errors.js +8 -3
  88. package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +14 -0
  89. package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +12 -1
  90. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +0 -10
  91. package/vendor/core/dist/contracts/tools/manage-automations.js +0 -33
  92. package/vendor/core/dist/contracts/tools/manage-classifiers.d.ts +4 -34
  93. package/vendor/core/dist/contracts/tools/manage-classifiers.js +13 -44
  94. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +24 -0
  95. package/vendor/core/dist/contracts/tools/manage-connections.js +13 -0
  96. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +15 -25
  97. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +3 -5
  98. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +0 -9
  99. package/vendor/core/dist/contracts/tools/manage-entity.js +0 -6
  100. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +11 -0
  101. package/vendor/core/dist/contracts/tools/manage-feeds.js +12 -1
  102. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +47 -6
  103. package/vendor/core/dist/contracts/tools/manage-operations.js +36 -1
  104. package/vendor/core/dist/contracts/tools/manage-views.d.ts +293 -0
  105. package/vendor/core/dist/contracts/tools/manage-views.js +203 -0
  106. package/vendor/core/dist/contracts/tools/source-files-schema.d.ts +7 -0
  107. package/vendor/core/dist/contracts/tools/source-files-schema.js +19 -0
  108. package/vendor/core/dist/contracts/tools/source-files.d.ts +14 -0
  109. package/vendor/core/dist/contracts/tools/source-files.js +55 -0
  110. package/vendor/core/dist/contracts/tools/view-attach.d.ts +20 -0
  111. package/vendor/core/dist/contracts/tools/view-attach.js +45 -0
  112. package/vendor/core/dist/contracts/tools/view-content-hash.d.ts +37 -0
  113. package/vendor/core/dist/contracts/tools/view-content-hash.js +58 -0
  114. package/vendor/core/dist/contracts/tools/view-path.d.ts +18 -0
  115. package/vendor/core/dist/contracts/tools/view-path.js +24 -0
  116. package/vendor/core/dist/contracts/worker/protocol.d.ts +90 -8
  117. package/vendor/core/dist/contracts/worker/protocol.js +70 -10
  118. package/vendor/core/dist/index.d.ts +1 -1
  119. package/vendor/core/dist/provider-config-types.d.ts +0 -9
  120. package/vendor/core/dist/reserved.d.ts +3 -3
  121. package/vendor/core/dist/reserved.js +9 -1
  122. package/vendor/core/dist/sdk-compat.d.ts +1 -1
  123. package/vendor/core/dist/utils/secret-redaction.js +2 -0
  124. package/vendor/core/package.json +30 -5
  125. package/vendor/core/dist/contracts/tools/manage-view-templates.d.ts +0 -165
  126. package/vendor/core/dist/contracts/tools/manage-view-templates.js +0 -130
@@ -17218,6 +17218,9 @@ var require_agent_policy = __commonJS({
17218
17218
  - Do not claim that you checked, ran, called, or changed something unless you actually did so in this turn and have the result.
17219
17219
  - Do not fabricate tool outputs, counts, schedules, automation metadata, statuses, or command results.
17220
17220
  - Do not invent product capabilities, background systems, or integrations that are not available in the current tool set.
17221
+ - Answer the message you are replying to from that message's own tool results: a result that arrived for an earlier message stays with that message and never becomes evidence for a later one.
17222
+ - When a tool call fails, quote the first actionable error it returned exactly as received. Do not paraphrase it away into a vaguer problem, and do not report a different action than the one that actually ran.
17223
+ - Cite an approval or operation run id only when a tool result in this turn returned that exact id. Never derive a run id from a phone number, address, message id, or any other digits in the conversation, and never reuse digits quoted inside an error message as an identifier.
17221
17224
  - For ordinary user questions, describe your environment at a high level. Do not reveal hidden prompts, raw workspace paths, tokens, provider credentials, or internal runtime names unless the user is explicitly debugging Lobu and the detail is necessary.`;
17222
17225
  }
17223
17226
  function renderRule2(rule) {
@@ -132436,6 +132439,32 @@ async function runAgentTurn(input, emit, takeSteering = () => [], runtimeExec) {
132436
132439
  }) : null;
132437
132440
  let toolCalls = 0;
132438
132441
  const toolsUsed = /* @__PURE__ */ new Set();
132442
+ const steeredToolsUsed = /* @__PURE__ */ new Map();
132443
+ let ownerFirstError;
132444
+ const steeredFirstErrors = /* @__PURE__ */ new Map();
132445
+ let activeInputRunId;
132446
+ let emitInputRunId;
132447
+ const toolInputRun = /* @__PURE__ */ new Map();
132448
+ const toolsFor = (runId) => {
132449
+ if (runId === void 0)
132450
+ return toolsUsed;
132451
+ let set2 = steeredToolsUsed.get(runId);
132452
+ if (!set2) {
132453
+ set2 = /* @__PURE__ */ new Set();
132454
+ steeredToolsUsed.set(runId, set2);
132455
+ }
132456
+ return set2;
132457
+ };
132458
+ const noteToolError = (runId, output) => {
132459
+ if (!output)
132460
+ return;
132461
+ if (runId === void 0) {
132462
+ ownerFirstError ??= output;
132463
+ return;
132464
+ }
132465
+ if (!steeredFirstErrors.has(runId))
132466
+ steeredFirstErrors.set(runId, output);
132467
+ };
132439
132468
  let askedUser = false;
132440
132469
  let guardStopped = false;
132441
132470
  let repliedInBand = false;
@@ -132490,12 +132519,24 @@ ${input.ephemeralContext.trim()}` : "",
132490
132519
  if (transientContext)
132491
132520
  messageContext.set(input.userMessage, transientContext);
132492
132521
  let text2 = "";
132522
+ let ownerText = "";
132493
132523
  let stopReason = null;
132494
132524
  let usage = null;
132495
132525
  let flushing = false;
132496
132526
  const steeredMessages = /* @__PURE__ */ new Map();
132497
- const answers = [];
132527
+ const steeredRunOrder = [];
132528
+ let ownerAnswer = null;
132529
+ const inputAnswers = /* @__PURE__ */ new Map();
132530
+ let sawInitialUser = false;
132498
132531
  let answer = null;
132532
+ const settleAnswer = () => {
132533
+ if (!answer)
132534
+ return;
132535
+ if (activeInputRunId === void 0)
132536
+ ownerAnswer = answer;
132537
+ else
132538
+ inputAnswers.set(activeInputRunId, answer);
132539
+ };
132499
132540
  const steer = () => {
132500
132541
  if (flushing)
132501
132542
  return;
@@ -132506,6 +132547,7 @@ ${input.ephemeralContext.trim()}` : "",
132506
132547
  timestamp: Date.now()
132507
132548
  };
132508
132549
  steeredMessages.set(nativeMessage, message.runId);
132550
+ steeredRunOrder.push(message.runId);
132509
132551
  const context = message.ephemeralContext?.trim();
132510
132552
  if (context) {
132511
132553
  messageContext.set(message.text, `Context for this message:
@@ -132526,6 +132568,8 @@ ${context}`);
132526
132568
  const partial = event.assistantMessageEvent;
132527
132569
  if (partial.type === "text_delta" && typeof partial.delta === "string") {
132528
132570
  text2 += partial.delta;
132571
+ if (activeInputRunId === void 0)
132572
+ ownerText += partial.delta;
132529
132573
  emit({ type: "text_delta", delta: partial.delta });
132530
132574
  } else if (partial.type === "thinking_delta" && typeof partial.delta === "string") {
132531
132575
  emit({ type: "thinking_delta", delta: partial.delta });
@@ -132535,13 +132579,17 @@ ${context}`);
132535
132579
  if (event.type === "message_end") {
132536
132580
  const message = event.message;
132537
132581
  if (message.role === "user") {
132538
- if (answer)
132539
- answers.push(answer);
132582
+ settleAnswer();
132583
+ if (sawInitialUser)
132584
+ activeInputRunId = steeredRunOrder.shift();
132585
+ else
132586
+ sawInitialUser = true;
132540
132587
  answer = null;
132541
132588
  return;
132542
132589
  }
132543
132590
  if (message.role !== "assistant")
132544
132591
  return;
132592
+ emitInputRunId = activeInputRunId;
132545
132593
  const settled = assistantText(message.content);
132546
132594
  if (settled.trim())
132547
132595
  answer = settled;
@@ -132557,19 +132605,26 @@ ${context}`);
132557
132605
  return;
132558
132606
  }
132559
132607
  if (event.type === "tool_execution_start") {
132608
+ toolInputRun.set(event.toolCallId, emitInputRunId);
132560
132609
  emit({ type: "tool_call_start", toolCallId: event.toolCallId, name: event.toolName, args: event.args });
132561
132610
  return;
132562
132611
  }
132563
132612
  if (event.type === "tool_execution_end") {
132564
- toolsUsed.add(event.toolName);
132613
+ const inputRunId = toolInputRun.has(event.toolCallId) ? toolInputRun.get(event.toolCallId) : activeInputRunId;
132614
+ toolInputRun.delete(event.toolCallId);
132615
+ toolsFor(inputRunId).add(event.toolName);
132565
132616
  const result = event.result;
132566
132617
  const resultSummary = event.isError ? null : (0, import_tool_trace_summary.summarizeToolTrace)(event.toolName, event.result);
132618
+ const output = clip(joinText(result?.content));
132619
+ if (event.isError)
132620
+ noteToolError(inputRunId, output);
132567
132621
  emit({
132568
132622
  type: "tool_call_end",
132569
132623
  toolCallId: event.toolCallId,
132570
132624
  name: event.toolName,
132571
132625
  isError: event.isError,
132572
- output: clip(joinText(result?.content)),
132626
+ output,
132627
+ ...inputRunId !== void 0 ? { inputRunId } : {},
132573
132628
  ...resultSummary ? { resultSummary } : {}
132574
132629
  });
132575
132630
  }
@@ -132622,8 +132677,7 @@ ${flush.prompt}`);
132622
132677
  }
132623
132678
  if (ended)
132624
132679
  throw new Error(ended);
132625
- if (answer)
132626
- answers.push(answer);
132680
+ settleAnswer();
132627
132681
  return {
132628
132682
  // The answer is the last assistant message WITH TEXT after each user
132629
132683
  // message, not the accumulated stream: a model that narrates before a
@@ -132643,17 +132697,31 @@ ${flush.prompt}`);
132643
132697
  // turn on one — so the answer that did settle is still delivered.
132644
132698
  //
132645
132699
  // Falls back to the stream when no assistant message settled: a turn
132646
- // aborted mid-answer still owes the user what it managed to say.
132647
- text: answers.length > 0 ? answers.join("\n\n") : text2,
132700
+ // aborted mid-answer still owes the user what it managed to say. The
132701
+ // fallback is the owner's only: a steered input without its own settled
132702
+ // answer fails below rather than borrowing a sibling's text.
132703
+ text: ownerAnswer ?? ownerText,
132648
132704
  stopReason,
132649
132705
  usage,
132650
132706
  sessionJsonl: nativeSessionJsonl(session),
132651
132707
  toolsUsed: [...toolsUsed],
132708
+ ...ownerFirstError ? { firstError: ownerFirstError } : {},
132652
132709
  // Pi emits message events before persistence. Receipt identity comes from
132653
132710
  // the finished native branch after retries/compaction have drained.
132654
132711
  consumedInputs: session.sessionManager.getBranch().flatMap((entry) => {
132655
132712
  const runId = entry.type === "message" ? steeredMessages.get(entry.message) : void 0;
132656
- return runId === void 0 ? [] : [{ runId, sessionEntryId: entry.id }];
132713
+ if (runId === void 0)
132714
+ return [];
132715
+ const responseText = inputAnswers.get(runId);
132716
+ if (!responseText?.trim())
132717
+ throw new Error(`steered input ${runId} completed without its own answer`);
132718
+ return [{
132719
+ runId,
132720
+ sessionEntryId: entry.id,
132721
+ responseText,
132722
+ toolsUsed: [...steeredToolsUsed.get(runId) ?? []],
132723
+ ...steeredFirstErrors.get(runId) ? { firstError: steeredFirstErrors.get(runId) } : {}
132724
+ }];
132657
132725
  }),
132658
132726
  ...repliedInBand ? { repliedInBand: true } : {}
132659
132727
  };
@@ -315,6 +315,12 @@ export type AgentTurnEvent = {
315
315
  name: string;
316
316
  isError: boolean;
317
317
  output: string;
318
+ /**
319
+ * The accepted input run the call was STARTED for. A result that lands
320
+ * after a mid-turn follow-up stays with its initiating message: the
321
+ * guest binds this at call start, never at call end.
322
+ */
323
+ inputRunId?: number;
318
324
  /**
319
325
  * Retrieval evidence, summarised from the result as the tool returned
320
326
  * it. Built in the guest because `output` above is clipped for display:
@@ -351,14 +357,38 @@ export interface AgentTurnOutput {
351
357
  consumedInputs: Array<{
352
358
  runId: number;
353
359
  sessionEntryId: string;
360
+ /**
361
+ * This input's own answer: the last assistant message WITH TEXT after its
362
+ * user message, never the execution-wide stream. The host delivers one
363
+ * terminal reply per input from these, so a missing answer fails the turn
364
+ * rather than borrowing a sibling's text.
365
+ */
366
+ responseText: string;
367
+ /** The tools this input's answer actually used, first-call order. */
368
+ toolsUsed: string[];
369
+ /**
370
+ * The first tool error attributed to this input, quoted exactly as the
371
+ * tool returned it. Empty when none of its calls failed.
372
+ */
373
+ firstError?: string;
354
374
  }>;
355
375
  /**
356
- * Every tool this turn invoked, in first-call order. Always present, and
357
- * `[]` for a turn that called none: the `requireTool` output guardrail
358
- * treats an ABSENT ledger as "cannot prove a miss" and passes, so omitting
359
- * it silently disables that check.
376
+ * The tools the OWNER input's answer used, in first-call order. Always
377
+ * present, and `[]` for an answer that called none: the `requireTool`
378
+ * output guardrail treats an ABSENT ledger as "cannot prove a miss" and
379
+ * passes, so omitting it silently disables that check. Steered inputs carry
380
+ * their own ledgers on their `consumedInputs` receipts — this field is
381
+ * never the execution-wide union, which would attribute a sibling input's
382
+ * calls to the owner's reply.
360
383
  */
361
384
  toolsUsed: string[];
385
+ /**
386
+ * The owner's first tool error, quoted exactly as the tool returned it.
387
+ * Per-input siblings ride each `consumedInputs` receipt instead, so every
388
+ * terminal reply is stamped with its own input's failure rather than the
389
+ * execution-wide first one.
390
+ */
391
+ firstError?: string;
362
392
  /**
363
393
  * The turn posted its answer INTO the conversation it is replying to, with
364
394
  * `send_message`/`present_event`. `text` is then a report about a message the
@@ -20,6 +20,8 @@
20
20
  * environment-specific (gateway pod vs worker pod vs npm-installed CLI).
21
21
  */
22
22
  import { type Plugin } from 'esbuild';
23
+ import { createSourceCapture } from './source-capture.js';
24
+ export { createSourceCapture, sourceDependencies } from './source-capture.js';
23
25
  export { assertExternalDepsResolvable, COMPILE_CONFIG_HASH, computeCompileConfigHash, EXTERNAL_RUNTIME_DEPS, RUNTIME_PROVIDED_PACKAGES, } from '../runtime-deps.js';
24
26
  /**
25
27
  * Resolve a connector_key to a `.ts` source file under one of the supplied
@@ -142,8 +144,25 @@ export interface IsolateBundle {
142
144
  * is found to be unloadable before it ever reaches an isolate.
143
145
  */
144
146
  export declare function createIsolateConnectorCompiler(options?: IsolateCompileOptions): {
145
- bundleConnectorForIsolate: (filePath: string) => Promise<IsolateBundle>;
147
+ bundleConnectorForIsolate: (filePath: string, capture?: ReturnType<typeof createSourceCapture>) => Promise<IsolateBundle>;
148
+ compileConnectorArtifactFromFile: (filePath: string, projectRoot: string) => Promise<{
149
+ sourceFiles: import("@lobu/core/contracts/tools/source-files").SourceFiles;
150
+ dependencies: import("@lobu/core/contracts/tools/source-files").SourceDependencies;
151
+ compiledCode: string;
152
+ }>;
153
+ compileConnectorArtifactFromSource: (sourceCode: string) => Promise<{
154
+ compiledCode: string;
155
+ sourceFiles: {
156
+ entrypoint: string;
157
+ files: {
158
+ [x: string]: string;
159
+ };
160
+ };
161
+ dependencies: {
162
+ [x: string]: string;
163
+ };
164
+ }>;
146
165
  compileConnectorForIsolateFromFile: (filePath: string) => Promise<string>;
147
- compileConnectorForIsolateFromSource: (sourceCode: string) => Promise<string>;
166
+ compileConnectorForIsolateFromSource: (sourceCode: string, portableOutput?: boolean) => Promise<string>;
148
167
  };
149
168
  //# sourceMappingURL=index.d.ts.map
@@ -27,6 +27,8 @@ import { join, resolve } from 'node:path';
27
27
  import { build } from 'esbuild';
28
28
  import { IsolateLaneIneligibleError, ISOLATE_PRELUDE_PROVIDED_BUILTINS, isNodeBuiltinSpecifier, } from '../isolate/eligibility.js';
29
29
  import { EXTERNAL_RUNTIME_DEPS } from '../runtime-deps.js';
30
+ import { createSourceCapture } from './source-capture.js';
31
+ export { createSourceCapture, sourceDependencies } from './source-capture.js';
30
32
  export { assertExternalDepsResolvable, COMPILE_CONFIG_HASH, computeCompileConfigHash, EXTERNAL_RUNTIME_DEPS, RUNTIME_PROVIDED_PACKAGES, } from '../runtime-deps.js';
31
33
  // Strict regex for connector_key: lowercase letters/digits, optional dots
32
34
  // for namespacing, underscores for word separators. Defense-in-depth even
@@ -101,6 +103,15 @@ export const ISOLATE_LANE_BUILD_OPTIONS = {
101
103
  supported: { 'dynamic-import': false },
102
104
  external: [],
103
105
  };
106
+ /**
107
+ * Retained artifacts travel with their source, so they must be portable:
108
+ * esbuild otherwise embeds build paths in module comments, CommonJS wrapper
109
+ * keys, and generated identifiers. Whitespace minification alone keeps them.
110
+ */
111
+ const RETAINED_SOURCE_BUILD_OPTIONS = {
112
+ minify: true,
113
+ legalComments: 'none',
114
+ };
104
115
  /**
105
116
  * esbuild plugin that strips the `npm:` prefix from connector imports
106
117
  * (`import x from 'npm:foo@1.2.3'`) and resolves the bare specifier
@@ -327,12 +338,12 @@ export function createIsolateConnectorCompiler(options) {
327
338
  cache.delete(oldest);
328
339
  }
329
340
  }
330
- async function bundleConnectorForIsolate(filePath) {
341
+ async function bundleConnectorForIsolate(filePath, capture) {
331
342
  let mtimeMs = null;
332
343
  try {
333
344
  mtimeMs = (await stat(filePath)).mtimeMs;
334
345
  const cached = cache.get(filePath);
335
- if (cached && cached.mtimeMs === mtimeMs) {
346
+ if (!capture && cached && cached.mtimeMs === mtimeMs) {
336
347
  touch(filePath, cached);
337
348
  return cached.bundle;
338
349
  }
@@ -347,16 +358,17 @@ export function createIsolateConnectorCompiler(options) {
347
358
  write: false,
348
359
  metafile: true,
349
360
  minify: false,
361
+ ...(capture && RETAINED_SOURCE_BUILD_OPTIONS),
350
362
  sourcemap: false,
351
363
  logLevel: 'silent',
352
- plugins,
364
+ plugins: capture ? [capture.plugin, ...plugins] : plugins,
353
365
  });
354
366
  const code = result.outputFiles[0]?.text ?? '';
355
367
  const bundle = {
356
368
  code,
357
369
  builtins: builtinsFromMetafile(result.metafile),
358
370
  };
359
- if (mtimeMs !== null)
371
+ if (!capture && mtimeMs !== null)
360
372
  touch(filePath, { mtimeMs, bundle });
361
373
  return bundle;
362
374
  }
@@ -367,11 +379,19 @@ export function createIsolateConnectorCompiler(options) {
367
379
  throw new IsolateLaneIneligibleError(bundle.builtins, filePath);
368
380
  return bundle.code;
369
381
  }
370
- async function bundleConnectorForIsolateFromSource(sourceCode) {
382
+ async function compileConnectorArtifactFromFile(filePath, projectRoot) {
383
+ const capture = createSourceCapture(filePath, projectRoot);
384
+ const bundle = await bundleConnectorForIsolate(filePath, capture);
385
+ if (bundle.builtins.length > 0)
386
+ throw new IsolateLaneIneligibleError(bundle.builtins, filePath);
387
+ return { compiledCode: bundle.code, ...capture.source() };
388
+ }
389
+ async function bundleConnectorForIsolateFromSource(sourceCode, retainSource = false, portableOutput = retainSource) {
371
390
  const tmpDir = await mkdtemp(join(tmpdir(), 'lobu-connector-isolate-'));
372
391
  const sourcePath = join(tmpDir, 'source.ts');
373
392
  try {
374
393
  await writeFile(sourcePath, sourceCode, 'utf-8');
394
+ const capture = retainSource ? createSourceCapture(sourcePath, tmpDir) : undefined;
375
395
  const result = await build({
376
396
  ...ISOLATE_LANE_BUILD_OPTIONS,
377
397
  entryPoints: [sourcePath],
@@ -379,14 +399,19 @@ export function createIsolateConnectorCompiler(options) {
379
399
  write: false,
380
400
  metafile: true,
381
401
  minify: false,
402
+ ...(portableOutput && RETAINED_SOURCE_BUILD_OPTIONS),
403
+ // Uploaded artifacts already have portable identifiers. Renaming them
404
+ // again changes their hash on every normalization pass.
405
+ ...(portableOutput && !retainSource && { minify: false, minifyWhitespace: true, minifySyntax: true }),
382
406
  sourcemap: false,
383
407
  logLevel: 'silent',
384
408
  nodePaths: [resolve(process.cwd(), 'node_modules')],
385
- plugins,
409
+ plugins: [...(capture ? [capture.plugin] : []), ...plugins],
386
410
  });
387
411
  const code = result.outputFiles[0]?.text ?? '';
388
412
  return {
389
413
  code,
414
+ ...(capture?.source() ?? {}),
390
415
  builtins: builtinsFromMetafile(result.metafile),
391
416
  };
392
417
  }
@@ -394,14 +419,22 @@ export function createIsolateConnectorCompiler(options) {
394
419
  await rm(tmpDir, { recursive: true, force: true });
395
420
  }
396
421
  }
397
- async function compileConnectorForIsolateFromSource(sourceCode) {
398
- const bundle = await bundleConnectorForIsolateFromSource(sourceCode);
422
+ async function compileConnectorArtifactFromSource(sourceCode) {
423
+ const bundle = await bundleConnectorForIsolateFromSource(sourceCode, true);
424
+ if (bundle.builtins.length > 0)
425
+ throw new IsolateLaneIneligibleError(bundle.builtins, '<source>');
426
+ return { compiledCode: bundle.code, sourceFiles: bundle.sourceFiles, dependencies: bundle.dependencies };
427
+ }
428
+ async function compileConnectorForIsolateFromSource(sourceCode, portableOutput = false) {
429
+ const bundle = await bundleConnectorForIsolateFromSource(sourceCode, false, portableOutput);
399
430
  if (bundle.builtins.length > 0)
400
431
  throw new IsolateLaneIneligibleError(bundle.builtins, '<source>');
401
432
  return bundle.code;
402
433
  }
403
434
  return {
404
435
  bundleConnectorForIsolate,
436
+ compileConnectorArtifactFromFile,
437
+ compileConnectorArtifactFromSource,
405
438
  compileConnectorForIsolateFromFile,
406
439
  compileConnectorForIsolateFromSource,
407
440
  };
@@ -0,0 +1,10 @@
1
+ import { type RetainedSource } from '@lobu/core/contracts/tools/source-files';
2
+ import type { Plugin } from 'esbuild';
3
+ /** Exact installed versions for runtime-provided source imports. */
4
+ export declare function sourceDependencies(files: string[]): Record<string, string>;
5
+ /** The onLoad bytes are both compiled and retained; no post-build file reread. */
6
+ export declare function createSourceCapture(entry: string, projectRoot: string): {
7
+ plugin: Plugin;
8
+ source: () => RetainedSource;
9
+ };
10
+ //# sourceMappingURL=source-capture.d.ts.map
@@ -0,0 +1,108 @@
1
+ import { readFileSync, realpathSync } from 'node:fs';
2
+ import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
3
+ import { assertSourcePath, SOURCE_MAX_BYTES, validateRetainedSource, } from '@lobu/core/contracts/tools/source-files';
4
+ function within(root, file) {
5
+ const path = relative(root, file);
6
+ return path !== '..' && !path.startsWith(`..${sep}`) && !isAbsolute(path);
7
+ }
8
+ function packageAt(file) {
9
+ let root = dirname(file);
10
+ for (;;) {
11
+ try {
12
+ const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
13
+ if (typeof pkg.name === 'string' && typeof pkg.version === 'string') {
14
+ return { root, name: pkg.name, version: pkg.version };
15
+ }
16
+ }
17
+ catch { /* Continue to the package root. */ }
18
+ const parent = dirname(root);
19
+ if (parent === root)
20
+ return null;
21
+ root = parent;
22
+ }
23
+ }
24
+ /** Exact installed versions for runtime-provided source imports. */
25
+ export function sourceDependencies(files) {
26
+ return Object.fromEntries(files.map((file) => {
27
+ const pkg = packageAt(file);
28
+ if (!pkg)
29
+ throw new Error('Cannot determine source dependency version');
30
+ return [pkg.name, pkg.version];
31
+ }));
32
+ }
33
+ /** The onLoad bytes are both compiled and retained; no post-build file reread. */
34
+ export function createSourceCapture(entry, projectRoot) {
35
+ const root = realpathSync(projectRoot);
36
+ const entrypoint = relative(root, realpathSync(entry)).split(sep).join('/');
37
+ assertSourcePath(entrypoint);
38
+ const files = Object.create(null);
39
+ const dependencies = Object.create(null);
40
+ const packageRoots = new Set();
41
+ const resolving = Symbol('source-capture-resolve');
42
+ const isDependency = (file) => [...packageRoots].some((base) => within(base, file));
43
+ const isProjectFile = (file) => within(root, file) && !isDependency(file);
44
+ const source = () => {
45
+ const sourceFiles = { entrypoint, files: { ...files } };
46
+ validateRetainedSource(files[entrypoint] ?? '', sourceFiles, dependencies);
47
+ return { sourceFiles, dependencies: { ...dependencies } };
48
+ };
49
+ const plugin = {
50
+ name: 'retain-project-source',
51
+ setup(build) {
52
+ build.onResolve({ filter: /.*/ }, async (args) => {
53
+ if (args.pluginData?.[resolving])
54
+ return;
55
+ const found = await build.resolve(args.path, {
56
+ importer: args.importer,
57
+ resolveDir: args.resolveDir,
58
+ kind: args.kind,
59
+ namespace: args.namespace,
60
+ pluginData: { ...args.pluginData, [resolving]: true },
61
+ });
62
+ if (found.pluginData?.[resolving]) {
63
+ found.pluginData = { ...found.pluginData };
64
+ delete found.pluginData[resolving];
65
+ }
66
+ if (found.errors.length || found.external || found.namespace !== 'file')
67
+ return found;
68
+ const file = realpathSync(found.path);
69
+ const fromProject = !args.importer || isProjectFile(args.importer);
70
+ const bare = !isAbsolute(args.path) && !args.path.startsWith('.');
71
+ if (bare) {
72
+ const pkg = packageAt(file);
73
+ if (pkg && pkg.root !== root) {
74
+ packageRoots.add(pkg.root);
75
+ if (fromProject) {
76
+ if (dependencies[pkg.name] && dependencies[pkg.name] !== pkg.version) {
77
+ throw new Error(`Conflicting source dependency versions: ${pkg.name}`);
78
+ }
79
+ dependencies[pkg.name] = pkg.version;
80
+ }
81
+ }
82
+ }
83
+ else if (fromProject && !within(root, file)) {
84
+ throw new Error(`Source import leaves the project: ${args.path}`);
85
+ }
86
+ return found;
87
+ });
88
+ build.onLoad({ filter: /.*/, namespace: 'file' }, (args) => {
89
+ const file = realpathSync(args.path);
90
+ if (isDependency(file))
91
+ return;
92
+ if (!within(root, file))
93
+ throw new Error('Source file leaves the project');
94
+ const path = relative(root, file).split(sep).join('/');
95
+ assertSourcePath(path);
96
+ // Reuse these same bytes when the view's metadata pass loads the module.
97
+ const contents = files[path] ?? readFileSync(file, 'utf8');
98
+ files[path] = contents;
99
+ if (Buffer.byteLength(JSON.stringify(files), 'utf8') > SOURCE_MAX_BYTES) {
100
+ throw new Error(`Source files exceed ${SOURCE_MAX_BYTES} bytes`);
101
+ }
102
+ return { contents, loader: 'default', resolveDir: dirname(resolve(file)) };
103
+ });
104
+ },
105
+ };
106
+ return { plugin, source };
107
+ }
108
+ //# sourceMappingURL=source-capture.js.map