@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,260 @@
|
|
|
1
|
+
const HARDENING_SOURCE = `
|
|
2
|
+
(function() {
|
|
3
|
+
try { delete globalThis.crypto; } catch {}
|
|
4
|
+
if ('crypto' in globalThis) {
|
|
5
|
+
Object.defineProperty(globalThis, 'crypto', {
|
|
6
|
+
value: undefined,
|
|
7
|
+
writable: false,
|
|
8
|
+
configurable: false,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
try { delete globalThis.performance; } catch {}
|
|
12
|
+
if ('performance' in globalThis) {
|
|
13
|
+
Object.defineProperty(globalThis, 'performance', {
|
|
14
|
+
value: undefined,
|
|
15
|
+
writable: false,
|
|
16
|
+
configurable: false,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Object.defineProperty(globalThis, 'eval', {
|
|
21
|
+
value: undefined,
|
|
22
|
+
writable: false,
|
|
23
|
+
configurable: false,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const OriginalFunction = Function;
|
|
27
|
+
const BlockedFunction = function() {
|
|
28
|
+
throw new TypeError('Function constructor is not allowed');
|
|
29
|
+
};
|
|
30
|
+
BlockedFunction.prototype = OriginalFunction.prototype;
|
|
31
|
+
|
|
32
|
+
const AsyncFunction = (async function(){}).constructor;
|
|
33
|
+
const GeneratorFunction = (function*(){}).constructor;
|
|
34
|
+
const AsyncGeneratorFunction = (async function*(){}).constructor;
|
|
35
|
+
for (const proto of [
|
|
36
|
+
OriginalFunction.prototype,
|
|
37
|
+
AsyncFunction.prototype,
|
|
38
|
+
GeneratorFunction.prototype,
|
|
39
|
+
AsyncGeneratorFunction.prototype,
|
|
40
|
+
]) {
|
|
41
|
+
Object.defineProperty(proto, 'constructor', {
|
|
42
|
+
value: BlockedFunction,
|
|
43
|
+
writable: false,
|
|
44
|
+
configurable: false,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
Object.defineProperty(globalThis, 'Function', {
|
|
48
|
+
value: BlockedFunction,
|
|
49
|
+
writable: false,
|
|
50
|
+
configurable: false,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const g = globalThis;
|
|
54
|
+
const toFreeze = [
|
|
55
|
+
Object, Object.prototype,
|
|
56
|
+
OriginalFunction, OriginalFunction.prototype,
|
|
57
|
+
AsyncFunction, AsyncFunction.prototype,
|
|
58
|
+
GeneratorFunction, GeneratorFunction.prototype,
|
|
59
|
+
AsyncGeneratorFunction, AsyncGeneratorFunction.prototype,
|
|
60
|
+
Array, Array.prototype,
|
|
61
|
+
String, String.prototype,
|
|
62
|
+
Number, Number.prototype,
|
|
63
|
+
g.BigInt, g.BigInt && g.BigInt.prototype,
|
|
64
|
+
Boolean, Boolean.prototype,
|
|
65
|
+
g.Symbol, g.Symbol && g.Symbol.prototype,
|
|
66
|
+
RegExp, RegExp.prototype,
|
|
67
|
+
Date, Date.prototype,
|
|
68
|
+
Map, Map.prototype,
|
|
69
|
+
Set, Set.prototype,
|
|
70
|
+
WeakMap, WeakMap.prototype,
|
|
71
|
+
WeakSet, WeakSet.prototype,
|
|
72
|
+
Promise, Promise.prototype,
|
|
73
|
+
ArrayBuffer, ArrayBuffer.prototype,
|
|
74
|
+
g.DataView, g.DataView && g.DataView.prototype,
|
|
75
|
+
JSON, Math, g.Reflect, g.Proxy,
|
|
76
|
+
];
|
|
77
|
+
for (const name of [
|
|
78
|
+
'Int8Array','Uint8Array','Uint8ClampedArray',
|
|
79
|
+
'Int16Array','Uint16Array','Int32Array','Uint32Array',
|
|
80
|
+
'Float32Array','Float64Array',
|
|
81
|
+
'BigInt64Array','BigUint64Array',
|
|
82
|
+
]) {
|
|
83
|
+
if (g[name]) toFreeze.push(g[name], g[name].prototype);
|
|
84
|
+
}
|
|
85
|
+
for (const obj of toFreeze) {
|
|
86
|
+
if (obj != null) {
|
|
87
|
+
try { Object.freeze(obj); } catch {}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
const BRIDGE_TRACKING_SOURCE = `
|
|
94
|
+
(function() {
|
|
95
|
+
var nextRecordId = 0;
|
|
96
|
+
var records = [];
|
|
97
|
+
|
|
98
|
+
function detachedError(message, record) {
|
|
99
|
+
var error = new Error(message);
|
|
100
|
+
error.name = 'CodeModeDetachedBridgeRequestError';
|
|
101
|
+
error.code = 'CODE_MODE_DETACHED_BRIDGE_REQUEST';
|
|
102
|
+
error.details = {
|
|
103
|
+
id: record.id,
|
|
104
|
+
kind: record.kind,
|
|
105
|
+
name: record.name,
|
|
106
|
+
status: record.status
|
|
107
|
+
};
|
|
108
|
+
return error;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
Object.defineProperty(globalThis, '__codeModeCreateBridgePromise', {
|
|
112
|
+
value: function(kind, name, start) {
|
|
113
|
+
var record = {
|
|
114
|
+
id: ++nextRecordId,
|
|
115
|
+
kind: String(kind),
|
|
116
|
+
name: String(name || ''),
|
|
117
|
+
observed: false,
|
|
118
|
+
status: 'idle'
|
|
119
|
+
};
|
|
120
|
+
var promise;
|
|
121
|
+
records.push(record);
|
|
122
|
+
|
|
123
|
+
function getPromise() {
|
|
124
|
+
record.observed = true;
|
|
125
|
+
if (!promise) {
|
|
126
|
+
record.status = 'pending';
|
|
127
|
+
promise = Promise.resolve().then(start).then(
|
|
128
|
+
function(value) {
|
|
129
|
+
record.status = 'fulfilled';
|
|
130
|
+
return value;
|
|
131
|
+
},
|
|
132
|
+
function(error) {
|
|
133
|
+
record.status = 'rejected';
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
return promise;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return Object.freeze({
|
|
142
|
+
then: function(onFulfilled, onRejected) {
|
|
143
|
+
return getPromise().then(onFulfilled, onRejected);
|
|
144
|
+
},
|
|
145
|
+
catch: function(onRejected) {
|
|
146
|
+
return getPromise().catch(onRejected);
|
|
147
|
+
},
|
|
148
|
+
finally: function(onFinally) {
|
|
149
|
+
return getPromise().finally(onFinally);
|
|
150
|
+
},
|
|
151
|
+
get [Symbol.toStringTag]() {
|
|
152
|
+
return 'Promise';
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
writable: false,
|
|
157
|
+
configurable: false
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
Object.defineProperty(globalThis, '__codeModeAssertNoDetachedBridgeCalls', {
|
|
161
|
+
value: function() {
|
|
162
|
+
for (var i = 0; i < records.length; i++) {
|
|
163
|
+
var record = records[i];
|
|
164
|
+
if (!record.observed) {
|
|
165
|
+
throw detachedError(
|
|
166
|
+
'Code mode created an unawaited ' + record.kind + ' bridge request: ' + record.name + '.',
|
|
167
|
+
record
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (record.status === 'pending') {
|
|
171
|
+
throw detachedError(
|
|
172
|
+
'Code mode returned while a ' + record.kind + ' bridge request was still pending: ' + record.name + '.',
|
|
173
|
+
record
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
writable: false,
|
|
179
|
+
configurable: false
|
|
180
|
+
});
|
|
181
|
+
})();
|
|
182
|
+
`;
|
|
183
|
+
|
|
184
|
+
const TOOLS_PROXY_SOURCE = `
|
|
185
|
+
(function(invokeTool) {
|
|
186
|
+
globalThis.tools = (function makeProxy(path) {
|
|
187
|
+
return new Proxy(function(){}, {
|
|
188
|
+
get: function(_target, prop) {
|
|
189
|
+
if (prop === 'then' || typeof prop === 'symbol') return undefined;
|
|
190
|
+
return makeProxy(path.concat([String(prop)]));
|
|
191
|
+
},
|
|
192
|
+
apply: function(_target, _thisArg, args) {
|
|
193
|
+
var toolPath = path.join('.');
|
|
194
|
+
if (!toolPath) throw new Error('Tool path missing in invocation');
|
|
195
|
+
var inputJson = args.length > 0 ? JSON.stringify(args[0]) : '';
|
|
196
|
+
if (inputJson === undefined) {
|
|
197
|
+
throw new Error('Tool input must be JSON-serializable');
|
|
198
|
+
}
|
|
199
|
+
return globalThis.__codeModeCreateBridgePromise('tool', toolPath, async function() {
|
|
200
|
+
var resultJson = await invokeTool(toolPath, inputJson);
|
|
201
|
+
return resultJson === '' ? undefined : JSON.parse(resultJson);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
})([]);
|
|
206
|
+
})(__codeModeInvokeTool);
|
|
207
|
+
`;
|
|
208
|
+
|
|
209
|
+
const SERIALIZATION_GUARD_SOURCE = `
|
|
210
|
+
(function() {
|
|
211
|
+
function serializeJsonPayload(value) {
|
|
212
|
+
if (typeof globalThis.__codeModeAssertNoDetachedBridgeCalls === 'function') {
|
|
213
|
+
globalThis.__codeModeAssertNoDetachedBridgeCalls();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (value === undefined) {
|
|
217
|
+
return '';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
var encoded;
|
|
221
|
+
try {
|
|
222
|
+
encoded = JSON.stringify(value);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
var message = error && error.message ? String(error.message) : String(error);
|
|
225
|
+
throw new TypeError('Code mode result is not JSON-serializable: ' + message);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (encoded === undefined) {
|
|
229
|
+
throw new TypeError('Code mode result is not JSON-serializable.');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return encoded;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
Object.defineProperty(globalThis, '__codeModeSerializeJsonPayload', {
|
|
236
|
+
value: serializeJsonPayload,
|
|
237
|
+
writable: false,
|
|
238
|
+
configurable: false,
|
|
239
|
+
});
|
|
240
|
+
})();
|
|
241
|
+
`;
|
|
242
|
+
|
|
243
|
+
export function buildGuestRuntimeSetupSource(): string {
|
|
244
|
+
return `
|
|
245
|
+
(function(__codeModeInvokeTool) {
|
|
246
|
+
${HARDENING_SOURCE}
|
|
247
|
+
${BRIDGE_TRACKING_SOURCE}
|
|
248
|
+
${TOOLS_PROXY_SOURCE}
|
|
249
|
+
${SERIALIZATION_GUARD_SOURCE}
|
|
250
|
+
})
|
|
251
|
+
`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function wrapUserCode(js: string): string {
|
|
255
|
+
return `
|
|
256
|
+
globalThis.__codeModeResult = (async () => {
|
|
257
|
+
${js}
|
|
258
|
+
})();
|
|
259
|
+
`;
|
|
260
|
+
}
|