@ai-sdk/code-mode 0.0.0-2d998b35-20260731001744
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.
- package/LICENSE +13 -0
- package/README.md +99 -0
- package/dist/code-mode-tool.js +40 -0
- package/dist/code-mode-tool.js.map +1 -0
- package/dist/errors.js +192 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +179 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/run-code-mode.js +8 -0
- package/dist/run-code-mode.js.map +1 -0
- package/dist/runtime/guest-sources.js +260 -0
- package/dist/runtime/guest-sources.js.map +1 -0
- package/dist/runtime/manager.js +414 -0
- package/dist/runtime/manager.js.map +1 -0
- package/dist/runtime/max-workers.js +40 -0
- package/dist/runtime/max-workers.js.map +1 -0
- package/dist/runtime/protocol.js +1 -0
- package/dist/runtime/protocol.js.map +1 -0
- package/dist/runtime/worker-source.d.ts +11 -0
- package/dist/runtime/worker-source.js +2 -0
- package/dist/tool-invocation.js +129 -0
- package/dist/tool-invocation.js.map +1 -0
- package/dist/tool-prompt.js +440 -0
- package/dist/tool-prompt.js.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/options.js +76 -0
- package/dist/utils/options.js.map +1 -0
- package/dist/utils/serialization.js +49 -0
- package/dist/utils/serialization.js.map +1 -0
- package/dist/utils/source-cache.js +77 -0
- package/dist/utils/source-cache.js.map +1 -0
- package/package.json +77 -0
- package/src/code-mode-tool.ts +60 -0
- package/src/errors.ts +267 -0
- package/src/index.ts +26 -0
- package/src/run-code-mode.ts +15 -0
- package/src/runtime/guest-sources.ts +260 -0
- package/src/runtime/manager.ts +498 -0
- package/src/runtime/max-workers.ts +65 -0
- package/src/runtime/protocol.ts +52 -0
- package/src/runtime/worker-source.ts +10 -0
- package/src/runtime/worker.ts +497 -0
- package/src/tool-invocation.ts +181 -0
- package/src/tool-prompt.ts +585 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +111 -0
- package/src/utils/options.ts +83 -0
- package/src/utils/serialization.ts +66 -0
- package/src/utils/source-cache.ts +91 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { CodeModeOptions, NormalizedCodeModeOptions } from '../types.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
4
|
+
const DEFAULT_MEMORY_LIMIT_BYTES = 64 * 1024 * 1024;
|
|
5
|
+
const DEFAULT_STACK_LIMIT_BYTES = 2 * 1024 * 1024;
|
|
6
|
+
const DEFAULT_MAX_RESULT_BYTES = 1024 * 1024;
|
|
7
|
+
const DEFAULT_MAX_CONSOLE_OUTPUT_BYTES = 64 * 1024;
|
|
8
|
+
const DEFAULT_MAX_SOURCE_BYTES = 256 * 1024;
|
|
9
|
+
const DEFAULT_MAX_TOOL_INPUT_BYTES = 1024 * 1024;
|
|
10
|
+
const DEFAULT_MAX_TOOL_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
11
|
+
const DEFAULT_MAX_BRIDGE_REQUESTS = 256;
|
|
12
|
+
const DEFAULT_MAX_IN_FLIGHT_BRIDGE_REQUESTS = 32;
|
|
13
|
+
|
|
14
|
+
export function normalizeOptions(
|
|
15
|
+
options: CodeModeOptions = {},
|
|
16
|
+
): NormalizedCodeModeOptions {
|
|
17
|
+
const executionPolicy = options.executionPolicy ?? {};
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
timeoutMs: positiveInteger(
|
|
21
|
+
executionPolicy.timeoutMs,
|
|
22
|
+
DEFAULT_TIMEOUT_MS,
|
|
23
|
+
'executionPolicy.timeoutMs',
|
|
24
|
+
),
|
|
25
|
+
memoryLimitBytes: positiveInteger(
|
|
26
|
+
executionPolicy.memoryLimitBytes,
|
|
27
|
+
DEFAULT_MEMORY_LIMIT_BYTES,
|
|
28
|
+
'executionPolicy.memoryLimitBytes',
|
|
29
|
+
),
|
|
30
|
+
maxStackSizeBytes: positiveInteger(
|
|
31
|
+
executionPolicy.maxStackSizeBytes,
|
|
32
|
+
DEFAULT_STACK_LIMIT_BYTES,
|
|
33
|
+
'executionPolicy.maxStackSizeBytes',
|
|
34
|
+
),
|
|
35
|
+
maxResultBytes: positiveInteger(
|
|
36
|
+
executionPolicy.maxResultBytes,
|
|
37
|
+
DEFAULT_MAX_RESULT_BYTES,
|
|
38
|
+
'executionPolicy.maxResultBytes',
|
|
39
|
+
),
|
|
40
|
+
maxConsoleOutputBytes: positiveInteger(
|
|
41
|
+
executionPolicy.maxConsoleOutputBytes,
|
|
42
|
+
DEFAULT_MAX_CONSOLE_OUTPUT_BYTES,
|
|
43
|
+
'executionPolicy.maxConsoleOutputBytes',
|
|
44
|
+
),
|
|
45
|
+
maxSourceBytes: positiveInteger(
|
|
46
|
+
executionPolicy.maxSourceBytes,
|
|
47
|
+
DEFAULT_MAX_SOURCE_BYTES,
|
|
48
|
+
'executionPolicy.maxSourceBytes',
|
|
49
|
+
),
|
|
50
|
+
maxToolInputBytes: positiveInteger(
|
|
51
|
+
executionPolicy.maxToolInputBytes,
|
|
52
|
+
DEFAULT_MAX_TOOL_INPUT_BYTES,
|
|
53
|
+
'executionPolicy.maxToolInputBytes',
|
|
54
|
+
),
|
|
55
|
+
maxToolOutputBytes: positiveInteger(
|
|
56
|
+
executionPolicy.maxToolOutputBytes,
|
|
57
|
+
DEFAULT_MAX_TOOL_OUTPUT_BYTES,
|
|
58
|
+
'executionPolicy.maxToolOutputBytes',
|
|
59
|
+
),
|
|
60
|
+
maxBridgeRequests: positiveInteger(
|
|
61
|
+
executionPolicy.maxBridgeRequests,
|
|
62
|
+
DEFAULT_MAX_BRIDGE_REQUESTS,
|
|
63
|
+
'executionPolicy.maxBridgeRequests',
|
|
64
|
+
),
|
|
65
|
+
maxInFlightBridgeRequests: positiveInteger(
|
|
66
|
+
executionPolicy.maxInFlightBridgeRequests,
|
|
67
|
+
DEFAULT_MAX_IN_FLIGHT_BRIDGE_REQUESTS,
|
|
68
|
+
'executionPolicy.maxInFlightBridgeRequests',
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function positiveInteger(
|
|
74
|
+
value: number | undefined,
|
|
75
|
+
fallback: number,
|
|
76
|
+
path: string,
|
|
77
|
+
): number {
|
|
78
|
+
const resolved = value ?? fallback;
|
|
79
|
+
if (!Number.isInteger(resolved) || resolved <= 0) {
|
|
80
|
+
throw new TypeError(`${path} must be a positive integer.`);
|
|
81
|
+
}
|
|
82
|
+
return resolved;
|
|
83
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { CodeModeError } from '../errors.js';
|
|
2
|
+
|
|
3
|
+
export function assertJsonSerializable(
|
|
4
|
+
value: unknown,
|
|
5
|
+
maxBytes: number,
|
|
6
|
+
label: string,
|
|
7
|
+
): void {
|
|
8
|
+
void toJsonPayload(value, maxBytes, label);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function toJsonPayload(
|
|
12
|
+
value: unknown,
|
|
13
|
+
maxBytes: number,
|
|
14
|
+
label: string,
|
|
15
|
+
): string {
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
return '';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return toStrictJsonPayload(value, maxBytes, label);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function toStrictJsonPayload(
|
|
24
|
+
value: unknown,
|
|
25
|
+
maxBytes: number,
|
|
26
|
+
label: string,
|
|
27
|
+
): string {
|
|
28
|
+
let encoded: string | undefined;
|
|
29
|
+
try {
|
|
30
|
+
encoded = JSON.stringify(value);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new CodeModeError(
|
|
33
|
+
`${label} is not JSON-serializable: ${getErrorMessage(error)}`,
|
|
34
|
+
'CODE_MODE_SERIALIZATION_ERROR',
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (encoded === undefined) {
|
|
39
|
+
throw new CodeModeError(
|
|
40
|
+
`${label} is not JSON-serializable.`,
|
|
41
|
+
'CODE_MODE_SERIALIZATION_ERROR',
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
assertJsonPayloadSize(encoded, maxBytes, label);
|
|
46
|
+
return encoded;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function assertJsonPayloadSize(
|
|
50
|
+
valueJson: string,
|
|
51
|
+
maxBytes: number,
|
|
52
|
+
label: string,
|
|
53
|
+
): void {
|
|
54
|
+
const bytes = new TextEncoder().encode(valueJson).byteLength;
|
|
55
|
+
if (bytes > maxBytes) {
|
|
56
|
+
throw new CodeModeError(
|
|
57
|
+
`${label} exceeds the ${maxBytes} byte size limit.`,
|
|
58
|
+
'CODE_MODE_SERIALIZATION_ERROR',
|
|
59
|
+
{ bytes, maxBytes },
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getErrorMessage(error: unknown): string {
|
|
65
|
+
return error instanceof Error ? error.message : String(error);
|
|
66
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { stripTypeScriptTypes } from 'node:module';
|
|
3
|
+
import { CodeModeSourceTooLargeError } from '../errors.js';
|
|
4
|
+
|
|
5
|
+
const MAX_CACHE_ENTRIES = 256;
|
|
6
|
+
const MAX_CACHE_BYTES = 4 * 1024 * 1024;
|
|
7
|
+
const MAX_CACHE_ENTRY_BYTES = 64 * 1024;
|
|
8
|
+
const transformedSourceCache = new Map<
|
|
9
|
+
string,
|
|
10
|
+
{
|
|
11
|
+
source: string;
|
|
12
|
+
bytes: number;
|
|
13
|
+
}
|
|
14
|
+
>();
|
|
15
|
+
let transformedSourceCacheBytes = 0;
|
|
16
|
+
|
|
17
|
+
export function assertSourceSize(source: string, maxSourceBytes: number): void {
|
|
18
|
+
const bytes = byteLength(source);
|
|
19
|
+
if (bytes > maxSourceBytes) {
|
|
20
|
+
throw new CodeModeSourceTooLargeError(bytes, maxSourceBytes);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function transformSource(source: string): string {
|
|
25
|
+
const hash = createHash('sha256')
|
|
26
|
+
.update('strip:')
|
|
27
|
+
.update(source)
|
|
28
|
+
.digest('hex');
|
|
29
|
+
const cached = transformedSourceCache.get(hash);
|
|
30
|
+
if (cached !== undefined) {
|
|
31
|
+
transformedSourceCache.delete(hash);
|
|
32
|
+
transformedSourceCache.set(hash, cached);
|
|
33
|
+
return cached.source;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const transformed = stripSnippetTypes(source);
|
|
37
|
+
const transformedBytes = byteLength(transformed);
|
|
38
|
+
if (transformedBytes <= MAX_CACHE_ENTRY_BYTES) {
|
|
39
|
+
transformedSourceCache.set(hash, {
|
|
40
|
+
source: transformed,
|
|
41
|
+
bytes: transformedBytes,
|
|
42
|
+
});
|
|
43
|
+
transformedSourceCacheBytes += transformedBytes;
|
|
44
|
+
evictTransformedSourceCache();
|
|
45
|
+
}
|
|
46
|
+
return transformed;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getTransformedSourceCacheStats() {
|
|
50
|
+
return {
|
|
51
|
+
entries: transformedSourceCache.size,
|
|
52
|
+
bytes: transformedSourceCacheBytes,
|
|
53
|
+
maxEntries: MAX_CACHE_ENTRIES,
|
|
54
|
+
maxBytes: MAX_CACHE_BYTES,
|
|
55
|
+
maxEntryBytes: MAX_CACHE_ENTRY_BYTES,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function clearTransformedSourceCache(): void {
|
|
60
|
+
transformedSourceCache.clear();
|
|
61
|
+
transformedSourceCacheBytes = 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function stripSnippetTypes(source: string): string {
|
|
65
|
+
const prefix = 'async function __codeModeUser__(){\n';
|
|
66
|
+
const suffix = '\n}';
|
|
67
|
+
const stripped = stripTypeScriptTypes(`${prefix}${source}${suffix}`);
|
|
68
|
+
if (!stripped.startsWith(prefix) || !stripped.endsWith(suffix)) {
|
|
69
|
+
return source;
|
|
70
|
+
}
|
|
71
|
+
return stripped.slice(prefix.length, -suffix.length);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function evictTransformedSourceCache(): void {
|
|
75
|
+
while (
|
|
76
|
+
transformedSourceCache.size > MAX_CACHE_ENTRIES ||
|
|
77
|
+
transformedSourceCacheBytes > MAX_CACHE_BYTES
|
|
78
|
+
) {
|
|
79
|
+
const oldest = transformedSourceCache.keys().next().value;
|
|
80
|
+
if (oldest === undefined) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const entry = transformedSourceCache.get(oldest);
|
|
84
|
+
transformedSourceCache.delete(oldest);
|
|
85
|
+
transformedSourceCacheBytes -= entry?.bytes ?? 0;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function byteLength(value: string): number {
|
|
90
|
+
return new TextEncoder().encode(value).byteLength;
|
|
91
|
+
}
|