@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,91 +0,0 @@
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
- }