@ai-sdk/code-mode 1.0.20 → 1.0.22

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 (50) hide show
  1. package/README.md +1 -1
  2. package/dist/continuation-capability.js +5 -0
  3. package/dist/continuation-capability.js.map +1 -1
  4. package/dist/errors.js +1 -136
  5. package/dist/errors.js.map +1 -1
  6. package/dist/host-interrupt.js +2 -14
  7. package/dist/host-interrupt.js.map +1 -1
  8. package/dist/index.d.ts +14 -44
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/interrupt-continuation.js +3 -8
  12. package/dist/interrupt-continuation.js.map +1 -1
  13. package/dist/run-code-mode.js +543 -2
  14. package/dist/run-code-mode.js.map +1 -1
  15. package/dist/tool-invocation.js +9 -23
  16. package/dist/tool-invocation.js.map +1 -1
  17. package/dist/utils/serialization.js +4 -0
  18. package/dist/utils/serialization.js.map +1 -1
  19. package/package.json +8 -7
  20. package/src/continuation-capability.ts +18 -0
  21. package/src/errors.ts +0 -190
  22. package/src/host-interrupt.ts +2 -31
  23. package/src/index.ts +1 -1
  24. package/src/interrupt-continuation.ts +13 -15
  25. package/src/run-code-mode.ts +706 -3
  26. package/src/tool-invocation.ts +9 -23
  27. package/src/types.ts +38 -68
  28. package/src/utils/serialization.ts +4 -0
  29. package/dist/runtime/guest-sources.js +0 -350
  30. package/dist/runtime/guest-sources.js.map +0 -1
  31. package/dist/runtime/manager.js +0 -794
  32. package/dist/runtime/manager.js.map +0 -1
  33. package/dist/runtime/max-workers.js +0 -40
  34. package/dist/runtime/max-workers.js.map +0 -1
  35. package/dist/runtime/protocol.js +0 -1
  36. package/dist/runtime/protocol.js.map +0 -1
  37. package/dist/runtime/worker-source.d.ts +0 -11
  38. package/dist/runtime/worker-source.js +0 -2
  39. package/dist/utils/options.js +0 -76
  40. package/dist/utils/options.js.map +0 -1
  41. package/dist/utils/source-cache.js +0 -77
  42. package/dist/utils/source-cache.js.map +0 -1
  43. package/src/runtime/guest-sources.ts +0 -351
  44. package/src/runtime/manager.ts +0 -963
  45. package/src/runtime/max-workers.ts +0 -65
  46. package/src/runtime/protocol.ts +0 -74
  47. package/src/runtime/worker-source.ts +0 -10
  48. package/src/runtime/worker.ts +0 -607
  49. package/src/utils/options.ts +0 -83
  50. package/src/utils/source-cache.ts +0 -91
@@ -1,65 +0,0 @@
1
- import os from 'node:os';
2
-
3
- const DEFAULT_MAX_WORKERS_CAP = 32;
4
- const DEFAULT_WORKER_OVERHEAD_BYTES = 48 * 1024 * 1024;
5
-
6
- let configuredMaxWorkers: number | undefined;
7
-
8
- /**
9
- * Sets the process-global maximum number of active code-mode workers.
10
- *
11
- * Pass `undefined` or call without an argument to restore the dynamic
12
- * memory-based default. The default admits at least one invocation and admits
13
- * additional workers only when available memory can cover another worker.
14
- *
15
- * @param maxWorkers - Positive integer worker cap, or `undefined` to reset.
16
- */
17
- export function setMaxWorkers(maxWorkers?: number): void {
18
- if (maxWorkers === undefined) {
19
- configuredMaxWorkers = undefined;
20
- return;
21
- }
22
- if (!Number.isInteger(maxWorkers) || maxWorkers <= 0) {
23
- throw new TypeError('maxWorkers must be a positive integer.');
24
- }
25
- configuredMaxWorkers = maxWorkers;
26
- }
27
-
28
- /**
29
- * Returns the currently effective worker cap.
30
- *
31
- * @internal
32
- */
33
- export function getMaxWorkers({
34
- memoryLimitBytes,
35
- activeWorkers,
36
- }: {
37
- memoryLimitBytes: number;
38
- activeWorkers: number;
39
- }): number {
40
- if (configuredMaxWorkers !== undefined) {
41
- return configuredMaxWorkers;
42
- }
43
-
44
- const estimatedBytesPerWorker =
45
- memoryLimitBytes + DEFAULT_WORKER_OVERHEAD_BYTES;
46
- const additionalWorkers = Math.floor(
47
- availableMemory() / estimatedBytesPerWorker,
48
- );
49
-
50
- return Math.max(
51
- 1,
52
- Math.min(DEFAULT_MAX_WORKERS_CAP, activeWorkers + additionalWorkers),
53
- );
54
- }
55
-
56
- function availableMemory(): number {
57
- const processWithAvailableMemory = process as typeof process & {
58
- availableMemory?: () => number;
59
- };
60
- const bytes =
61
- typeof processWithAvailableMemory.availableMemory === 'function'
62
- ? processWithAvailableMemory.availableMemory()
63
- : os.freemem();
64
- return Number.isFinite(bytes) && bytes > 0 ? bytes : 0;
65
- }
@@ -1,74 +0,0 @@
1
- import type {
2
- CodeModeDeterminismState,
3
- NormalizedCodeModeOptions,
4
- SerializableError,
5
- } from '../types.js';
6
-
7
- export interface WorkerRunMessage {
8
- type: 'run';
9
- invocationId: string;
10
- js: string;
11
- determinism: CodeModeDeterminismState;
12
- options: Pick<
13
- NormalizedCodeModeOptions,
14
- | 'timeoutMs'
15
- | 'memoryLimitBytes'
16
- | 'maxStackSizeBytes'
17
- | 'maxResultBytes'
18
- | 'maxConsoleOutputBytes'
19
- >;
20
- }
21
-
22
- export interface WorkerToolRequest {
23
- type: 'tool-request';
24
- invocationId: string;
25
- requestId: string;
26
- toolName: string;
27
- inputJson: string;
28
- }
29
-
30
- export interface WorkerBridgeResponse {
31
- type: 'bridge-response';
32
- invocationId: string;
33
- requestId: string;
34
- success: boolean;
35
- dateNowMs: number;
36
- valueJson?: string;
37
- error?: SerializableError;
38
- }
39
-
40
- export interface WorkerBridgeDrainRequest {
41
- type: 'bridge-drain';
42
- invocationId: string;
43
- drainId: string;
44
- }
45
-
46
- export interface WorkerBridgeDrainedMessage {
47
- type: 'bridge-drained';
48
- invocationId: string;
49
- drainId: string;
50
- }
51
-
52
- export interface WorkerResultMessage {
53
- type: 'result';
54
- invocationId: string;
55
- success: boolean;
56
- valueJson?: string;
57
- error?: SerializableError;
58
- }
59
-
60
- export interface WorkerReadyMessage {
61
- type: 'ready';
62
- invocationId: string;
63
- }
64
-
65
- export type WorkerToMainMessage =
66
- | WorkerToolRequest
67
- | WorkerBridgeDrainedMessage
68
- | WorkerResultMessage
69
- | WorkerReadyMessage;
70
-
71
- export type MainToWorkerMessage =
72
- | WorkerRunMessage
73
- | WorkerBridgeResponse
74
- | WorkerBridgeDrainRequest;
@@ -1,10 +0,0 @@
1
- /**
2
- * Inline worker source generated into `dist/runtime/worker-source.js` during
3
- * package builds.
4
- *
5
- * The source placeholder is intentionally unusable before the build step so a
6
- * broken package build fails loudly instead of trying to run an empty worker.
7
- *
8
- */
9
- export const INLINE_CODE_MODE_WORKER_SOURCE =
10
- 'throw new Error("Code mode inline worker source was not generated.");';