|
2 | 2 |
|
3 | 3 | import util from 'node:util'; |
4 | 4 |
|
| 5 | +import Fuse from 'fuse.js'; |
5 | 6 | import ts from 'typescript'; |
6 | 7 |
|
7 | 8 | import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types'; |
@@ -39,8 +40,98 @@ function getRunFunctionNode( |
39 | 40 | return null; |
40 | 41 | } |
41 | 42 |
|
| 43 | +const fuse = new Fuse( |
| 44 | + [ |
| 45 | + 'client.casParser.camsKfintech', |
| 46 | + 'client.casParser.cdsl', |
| 47 | + 'client.casParser.nsdl', |
| 48 | + 'client.casParser.smartParse', |
| 49 | + 'client.casGenerator.generateCas', |
| 50 | + ], |
| 51 | + { threshold: 1, shouldSort: true }, |
| 52 | +); |
| 53 | + |
| 54 | +function getMethodSuggestions(fullyQualifiedMethodName: string): string[] { |
| 55 | + return fuse |
| 56 | + .search(fullyQualifiedMethodName) |
| 57 | + .map(({ item }) => item) |
| 58 | + .slice(0, 5); |
| 59 | +} |
| 60 | + |
| 61 | +const proxyToObj = new WeakMap<any, any>(); |
| 62 | +const objToProxy = new WeakMap<any, any>(); |
| 63 | + |
| 64 | +type ClientProxyConfig = { |
| 65 | + path: string[]; |
| 66 | + isBelievedBad?: boolean; |
| 67 | +}; |
| 68 | + |
| 69 | +function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T { |
| 70 | + let proxy: T = objToProxy.get(obj); |
| 71 | + |
| 72 | + if (!proxy) { |
| 73 | + proxy = new Proxy(obj, { |
| 74 | + get(target, prop, receiver) { |
| 75 | + const propPath = [...path, String(prop)]; |
| 76 | + const value = Reflect.get(target, prop, receiver); |
| 77 | + |
| 78 | + if (isBelievedBad || (!(prop in target) && value === undefined)) { |
| 79 | + // If we're accessing a path that doesn't exist, it will probably eventually error. |
| 80 | + // Let's proxy it and mark it bad so that we can control the error message. |
| 81 | + // We proxy an empty class so that an invocation or construction attempt is possible. |
| 82 | + return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true }); |
| 83 | + } |
| 84 | + |
| 85 | + if (value !== null && (typeof value === 'object' || typeof value === 'function')) { |
| 86 | + return makeSdkProxy(value, { path: propPath, isBelievedBad }); |
| 87 | + } |
| 88 | + |
| 89 | + return value; |
| 90 | + }, |
| 91 | + |
| 92 | + apply(target, thisArg, args) { |
| 93 | + if (isBelievedBad || typeof target !== 'function') { |
| 94 | + const fullyQualifiedMethodName = path.join('.'); |
| 95 | + const suggestions = getMethodSuggestions(fullyQualifiedMethodName); |
| 96 | + throw new Error( |
| 97 | + `${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args); |
| 102 | + }, |
| 103 | + |
| 104 | + construct(target, args, newTarget) { |
| 105 | + if (isBelievedBad || typeof target !== 'function') { |
| 106 | + const fullyQualifiedMethodName = path.join('.'); |
| 107 | + const suggestions = getMethodSuggestions(fullyQualifiedMethodName); |
| 108 | + throw new Error( |
| 109 | + `${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return Reflect.construct(target, args, newTarget); |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + objToProxy.set(obj, proxy); |
| 118 | + proxyToObj.set(proxy, obj); |
| 119 | + } |
| 120 | + |
| 121 | + return proxy; |
| 122 | +} |
| 123 | + |
42 | 124 | const fetch = async (req: Request): Promise<Response> => { |
43 | 125 | const { opts, code } = (await req.json()) as WorkerInput; |
| 126 | + if (code == null) { |
| 127 | + return Response.json( |
| 128 | + { |
| 129 | + message: |
| 130 | + 'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```', |
| 131 | + } satisfies WorkerError, |
| 132 | + { status: 400, statusText: 'Code execution error' }, |
| 133 | + ); |
| 134 | + } |
44 | 135 |
|
45 | 136 | const runFunctionNode = getRunFunctionNode(code); |
46 | 137 | if (!runFunctionNode) { |
@@ -73,7 +164,7 @@ const fetch = async (req: Request): Promise<Response> => { |
73 | 164 | ${code} |
74 | 165 | run_ = run; |
75 | 166 | `); |
76 | | - const result = await run_(client); |
| 167 | + const result = await run_(makeSdkProxy(client, { path: ['client'] })); |
77 | 168 | return Response.json({ |
78 | 169 | result, |
79 | 170 | logLines, |
|
0 commit comments