|
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,160 @@ function getRunFunctionNode( |
39 | 40 | return null; |
40 | 41 | } |
41 | 42 |
|
| 43 | +const fuse = new Fuse( |
| 44 | + [ |
| 45 | + 'client.agents.create', |
| 46 | + 'client.agents.createOrUpdate', |
| 47 | + 'client.agents.delete', |
| 48 | + 'client.agents.get', |
| 49 | + 'client.agents.list', |
| 50 | + 'client.agents.listModels', |
| 51 | + 'client.agents.reset', |
| 52 | + 'client.agents.update', |
| 53 | + 'client.agents.tools.create', |
| 54 | + 'client.agents.tools.delete', |
| 55 | + 'client.agents.tools.list', |
| 56 | + 'client.agents.tools.reset', |
| 57 | + 'client.agents.tools.update', |
| 58 | + 'client.agents.docs.bulkDelete', |
| 59 | + 'client.agents.docs.create', |
| 60 | + 'client.agents.docs.delete', |
| 61 | + 'client.agents.docs.list', |
| 62 | + 'client.agents.docs.search', |
| 63 | + 'client.files.create', |
| 64 | + 'client.files.delete', |
| 65 | + 'client.files.get', |
| 66 | + 'client.files.list', |
| 67 | + 'client.sessions.chat', |
| 68 | + 'client.sessions.create', |
| 69 | + 'client.sessions.createOrUpdate', |
| 70 | + 'client.sessions.delete', |
| 71 | + 'client.sessions.get', |
| 72 | + 'client.sessions.history', |
| 73 | + 'client.sessions.list', |
| 74 | + 'client.sessions.render', |
| 75 | + 'client.sessions.reset', |
| 76 | + 'client.sessions.update', |
| 77 | + 'client.users.create', |
| 78 | + 'client.users.createOrUpdate', |
| 79 | + 'client.users.delete', |
| 80 | + 'client.users.get', |
| 81 | + 'client.users.list', |
| 82 | + 'client.users.reset', |
| 83 | + 'client.users.update', |
| 84 | + 'client.users.docs.bulkDelete', |
| 85 | + 'client.users.docs.create', |
| 86 | + 'client.users.docs.delete', |
| 87 | + 'client.users.docs.list', |
| 88 | + 'client.users.docs.search', |
| 89 | + 'client.jobs.get', |
| 90 | + 'client.docs.embed', |
| 91 | + 'client.docs.get', |
| 92 | + 'client.tasks.create', |
| 93 | + 'client.tasks.createOrUpdate', |
| 94 | + 'client.tasks.get', |
| 95 | + 'client.tasks.list', |
| 96 | + 'client.executions.changeStatus', |
| 97 | + 'client.executions.create', |
| 98 | + 'client.executions.get', |
| 99 | + 'client.executions.list', |
| 100 | + 'client.executions.transitions.list', |
| 101 | + 'client.executions.transitions.retrieve', |
| 102 | + 'client.executions.transitions.stream', |
| 103 | + 'client.executions.status.get', |
| 104 | + 'client.executions.status.stream', |
| 105 | + 'client.secrets.create', |
| 106 | + 'client.secrets.delete', |
| 107 | + 'client.secrets.list', |
| 108 | + 'client.secrets.update', |
| 109 | + 'client.projects.create', |
| 110 | + 'client.projects.list', |
| 111 | + 'client.healthz.check', |
| 112 | + ], |
| 113 | + { threshold: 1, shouldSort: true }, |
| 114 | +); |
| 115 | + |
| 116 | +function getMethodSuggestions(fullyQualifiedMethodName: string): string[] { |
| 117 | + return fuse |
| 118 | + .search(fullyQualifiedMethodName) |
| 119 | + .map(({ item }) => item) |
| 120 | + .slice(0, 5); |
| 121 | +} |
| 122 | + |
| 123 | +const proxyToObj = new WeakMap<any, any>(); |
| 124 | +const objToProxy = new WeakMap<any, any>(); |
| 125 | + |
| 126 | +type ClientProxyConfig = { |
| 127 | + path: string[]; |
| 128 | + isBelievedBad?: boolean; |
| 129 | +}; |
| 130 | + |
| 131 | +function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T { |
| 132 | + let proxy: T = objToProxy.get(obj); |
| 133 | + |
| 134 | + if (!proxy) { |
| 135 | + proxy = new Proxy(obj, { |
| 136 | + get(target, prop, receiver) { |
| 137 | + const propPath = [...path, String(prop)]; |
| 138 | + const value = Reflect.get(target, prop, receiver); |
| 139 | + |
| 140 | + if (isBelievedBad || (!(prop in target) && value === undefined)) { |
| 141 | + // If we're accessing a path that doesn't exist, it will probably eventually error. |
| 142 | + // Let's proxy it and mark it bad so that we can control the error message. |
| 143 | + // We proxy an empty class so that an invocation or construction attempt is possible. |
| 144 | + return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true }); |
| 145 | + } |
| 146 | + |
| 147 | + if (value !== null && (typeof value === 'object' || typeof value === 'function')) { |
| 148 | + return makeSdkProxy(value, { path: propPath, isBelievedBad }); |
| 149 | + } |
| 150 | + |
| 151 | + return value; |
| 152 | + }, |
| 153 | + |
| 154 | + apply(target, thisArg, args) { |
| 155 | + if (isBelievedBad || typeof target !== 'function') { |
| 156 | + const fullyQualifiedMethodName = path.join('.'); |
| 157 | + const suggestions = getMethodSuggestions(fullyQualifiedMethodName); |
| 158 | + throw new Error( |
| 159 | + `${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`, |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args); |
| 164 | + }, |
| 165 | + |
| 166 | + construct(target, args, newTarget) { |
| 167 | + if (isBelievedBad || typeof target !== 'function') { |
| 168 | + const fullyQualifiedMethodName = path.join('.'); |
| 169 | + const suggestions = getMethodSuggestions(fullyQualifiedMethodName); |
| 170 | + throw new Error( |
| 171 | + `${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`, |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + return Reflect.construct(target, args, newTarget); |
| 176 | + }, |
| 177 | + }); |
| 178 | + |
| 179 | + objToProxy.set(obj, proxy); |
| 180 | + proxyToObj.set(proxy, obj); |
| 181 | + } |
| 182 | + |
| 183 | + return proxy; |
| 184 | +} |
| 185 | + |
42 | 186 | const fetch = async (req: Request): Promise<Response> => { |
43 | 187 | const { opts, code } = (await req.json()) as WorkerInput; |
| 188 | + if (code == null) { |
| 189 | + return Response.json( |
| 190 | + { |
| 191 | + message: |
| 192 | + '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```', |
| 193 | + } satisfies WorkerError, |
| 194 | + { status: 400, statusText: 'Code execution error' }, |
| 195 | + ); |
| 196 | + } |
44 | 197 |
|
45 | 198 | const runFunctionNode = getRunFunctionNode(code); |
46 | 199 | if (!runFunctionNode) { |
@@ -73,7 +226,7 @@ const fetch = async (req: Request): Promise<Response> => { |
73 | 226 | ${code} |
74 | 227 | run_ = run; |
75 | 228 | `); |
76 | | - const result = await run_(client); |
| 229 | + const result = await run_(makeSdkProxy(client, { path: ['client'] })); |
77 | 230 | return Response.json({ |
78 | 231 | result, |
79 | 232 | logLines, |
|
0 commit comments