Unified · Split
| @@ -0,0 +1,25 @@ | ||
| 1 | +import type { CodeExecutor } from "@executor/codemode-core"; | |
| 2 | +import { makeDenoSubprocessExecutor } from "@executor/runtime-deno-subprocess"; | |
| 3 | +import { makeQuickJsExecutor } from "@executor/runtime-quickjs"; | |
| 4 | +import { makeSesExecutor } from "@executor/runtime-ses"; | |
| 5 | +import type { LocalExecutorConfig, LocalExecutorRuntime } from "#schema"; | |
| 6 | + | |
| 7 | +const DEFAULT_EXECUTION_RUNTIME: LocalExecutorRuntime = "quickjs"; | |
| 8 | + | |
| 9 | +export const resolveConfiguredExecutionRuntime = ( | |
| 10 | + config: LocalExecutorConfig | null | undefined, | |
| 11 | +): LocalExecutorRuntime => config?.runtime ?? DEFAULT_EXECUTION_RUNTIME; | |
| 12 | + | |
| 13 | +export const createCodeExecutorForRuntime = ( | |
| 14 | + runtime: LocalExecutorRuntime, | |
| 15 | +): CodeExecutor => { | |
| 16 | + switch (runtime) { | |
| 17 | + case "deno": | |
| 18 | + return makeDenoSubprocessExecutor(); | |
| 19 | + case "ses": | |
| 20 | + return makeSesExecutor(); | |
| 21 | + case "quickjs": | |
| 22 | + default: | |
| 23 | + return makeQuickJsExecutor(); | |
| 24 | + } | |
| 25 | +}; | |
| @@ -1,676 +1,62 @@ | ||
| 1 | 1 | import { |
| 2 | − type CodeExecutor, | |
| 3 | − createSystemToolMap, | |
| 4 | − createToolCatalogFromEntries, | |
| 5 | 2 | createToolCatalogFromTools, |
| 6 | 3 | makeToolInvokerFromTools, |
| 7 | − mergeToolCatalogs, | |
| 8 | − mergeToolMaps, | |
| 9 | − type ToolCatalog, | |
| 10 | − type ToolInvoker, | |
| 11 | − type ToolPath, | |
| 12 | 4 | } from "@executor/codemode-core"; |
| 13 | −import { makeDenoSubprocessExecutor } from "@executor/runtime-deno-subprocess"; | |
| 14 | −import { makeQuickJsExecutor } from "@executor/runtime-quickjs"; | |
| 15 | −import { makeSesExecutor } from "@executor/runtime-ses"; | |
| 16 | 5 | import type { |
| 17 | 6 | AccountId, |
| 18 | − LocalExecutorConfig, | |
| 19 | − LocalExecutorRuntime, | |
| 20 | 7 | Source, |
| 21 | 8 | } from "#schema"; |
| 22 | 9 | import * as Context from "effect/Context"; |
| 23 | −import * as Either from "effect/Either"; | |
| 24 | 10 | import * as Effect from "effect/Effect"; |
| 25 | 11 | import * as Layer from "effect/Layer"; |
| 26 | −import * as Schema from "effect/Schema"; | |
| 27 | 12 | |
| 28 | 13 | import type { |
| 29 | 14 | ExecutionEnvironment, |
| 30 | 15 | ResolveExecutionEnvironment, |
| 31 | 16 | } from "./execution-state"; |
| 32 | −import { createExecutorToolMap } from "../sources/executor-tools"; | |
| 17 | +import { | |
| 18 | + createCodeExecutorForRuntime, | |
| 19 | + resolveConfiguredExecutionRuntime, | |
| 20 | +} from "./execution-runtime"; | |
| 21 | +import { createWorkspaceToolInvoker } from "./workspace-tool-invoker"; | |
| 33 | 22 | import { |
| 34 | 23 | RuntimeSourceAuthServiceTag, |
| 35 | − type RuntimeSourceAuthService, | |
| 36 | 24 | } from "../sources/source-auth-service"; |
| 37 | 25 | import { |
| 38 | 26 | RuntimeSourceCatalogStoreService, |
| 39 | − type LoadedSourceCatalogToolIndexEntry, | |
| 40 | − catalogToolCatalogEntry, | |
| 41 | 27 | } from "../catalog/source-catalog-runtime"; |
| 42 | −import { | |
| 43 | − invocationDescriptorFromTool, | |
| 44 | − invokeIrTool, | |
| 45 | −} from "./ir-execution"; | |
| 46 | 28 | import { RuntimeSourceAuthMaterialService } from "../auth/source-auth-material"; |
| 47 | −import { | |
| 48 | − type SecretMaterialResolveContext, | |
| 49 | −} from "../local/secret-material-providers"; | |
| 50 | −import { | |
| 51 | − evaluateInvocationPolicy, | |
| 52 | − type InvocationDescriptor, | |
| 53 | −} from "../policy/invocation-policy-engine"; | |
| 54 | −import { loadRuntimeLocalWorkspacePolicies } from "../policy/policies-operations"; | |
| 55 | 29 | import { |
| 56 | 30 | getRuntimeLocalWorkspaceOption, |
| 57 | − provideOptionalRuntimeLocalWorkspace, | |
| 58 | − type RuntimeLocalWorkspaceState, | |
| 59 | 31 | } from "../local/local-runtime-context"; |
| 60 | 32 | import { |
| 61 | 33 | LocalToolRuntimeLoaderService, |
| 62 | − type LocalToolRuntime, | |
| 63 | 34 | type LocalToolRuntimeLoaderShape, |
| 35 | + type LocalToolRuntime, | |
| 64 | 36 | } from "../local/local-tools"; |
| 65 | 37 | import { |
| 66 | 38 | SourceArtifactStore, |
| 67 | − makeWorkspaceStorageLayer, | |
| 68 | 39 | type SourceArtifactStoreShape, |
| 69 | 40 | WorkspaceConfigStore, |
| 70 | 41 | type WorkspaceConfigStoreShape, |
| 71 | 42 | WorkspaceStateStore, |
| 72 | 43 | type WorkspaceStateStoreShape, |
| 73 | − type WorkspaceStorageServices, | |
| 74 | 44 | } from "../local/local-storage"; |
| 75 | − | |
| 76 | −const asToolPath = (value: string): ToolPath => value as ToolPath; | |
| 77 | − | |
| 78 | −const DEFAULT_EXECUTION_RUNTIME: LocalExecutorRuntime = "quickjs"; | |
| 79 | − | |
| 80 | −export const resolveConfiguredExecutionRuntime = ( | |
| 81 | − config: LocalExecutorConfig | null | undefined, | |
| 82 | −): LocalExecutorRuntime => config?.runtime ?? DEFAULT_EXECUTION_RUNTIME; | |
| 83 | − | |
| 84 | −export const createCodeExecutorForRuntime = ( | |
| 85 | − runtime: LocalExecutorRuntime, | |
| 86 | −): CodeExecutor => { | |
| 87 | − switch (runtime) { | |
| 88 | − case "deno": | |
| 89 | − return makeDenoSubprocessExecutor(); | |
| 90 | − case "ses": | |
| 91 | − return makeSesExecutor(); | |
| 92 | − case "quickjs": | |
| 93 | − default: | |
| 94 | − return makeQuickJsExecutor(); | |
| 95 | − } | |
| 96 | −}; | |
| 97 | − | |
| 98 | −const tokenize = (value: string): string[] => | |
| 99 | − value | |
| 100 | − .trim() | |
| 101 | − .toLowerCase() | |
| 102 | − .split(/[^a-z0-9]+/) | |
| 103 | − .filter(Boolean); | |
| 104 | − | |
| 105 | −const LOW_SIGNAL_QUERY_TOKENS = new Set([ | |
| 106 | − "a", | |
| 107 | − "an", | |
| 108 | − "the", | |
| 109 | − "am", | |
| 110 | − "as", | |
| 111 | − "for", | |
| 112 | − "from", | |
| 113 | − "get", | |
| 114 | − "i", | |
| 115 | − "in", | |
| 116 | − "is", | |
| 117 | − "list", | |
| 118 | − "me", | |
| 119 | − "my", | |
| 120 | − "of", | |
| 121 | − "on", | |
| 122 | − "or", | |
| 123 | − "signed", | |
| 124 | − "to", | |
| 125 | − "who", | |
| 126 | −]); | |
| 127 | − | |
| 128 | −const singularizeToken = (value: string): string => | |
| 129 | − value.length > 3 && value.endsWith("s") ? value.slice(0, -1) : value; | |
| 130 | − | |
| 131 | −const tokenEquals = (left: string, right: string): boolean => | |
| 132 | − left === right || singularizeToken(left) === singularizeToken(right); | |
| 133 | − | |
| 134 | −const hasTokenMatch = ( | |
| 135 | − tokens: readonly string[], | |
| 136 | − queryToken: string, | |
| 137 | −): boolean => tokens.some((token) => tokenEquals(token, queryToken)); | |
| 138 | − | |
| 139 | −const hasSubstringMatch = (value: string, queryToken: string): boolean => { | |
| 140 | − if (value.includes(queryToken)) { | |
| 141 | − return true; | |
| 142 | − } | |
| 143 | − | |
| 144 | − const singular = singularizeToken(queryToken); | |
| 145 | − return singular !== queryToken && value.includes(singular); | |
| 146 | −}; | |
| 147 | − | |
| 148 | −const SecretResolutionContextEnvelopeSchema = Schema.Struct({ | |
| 149 | − params: Schema.optional( | |
| 150 | − Schema.Record({ | |
| 151 | − key: Schema.String, | |
| 152 | − value: Schema.String, | |
| 153 | − }), | |
| 154 | − ), | |
| 45 | +export { | |
| 46 | + createCodeExecutorForRuntime, | |
| 47 | + resolveConfiguredExecutionRuntime, | |
| 48 | +} from "./execution-runtime"; | |
| 49 | + | |
| 50 | +const createEmptyLocalToolRuntime = (): LocalToolRuntime => ({ | |
| 51 | + tools: {}, | |
| 52 | + catalog: createToolCatalogFromTools({ tools: {} }), | |
| 53 | + toolInvoker: makeToolInvokerFromTools({ tools: {} }), | |
| 54 | + toolPaths: new Set<string>(), | |
| 155 | 55 | }); |
| 156 | 56 | |
| 157 | −const decodeSecretResolutionContextEnvelope = Schema.decodeUnknownEither( | |
| 158 | − SecretResolutionContextEnvelopeSchema, | |
| 159 | −); | |
| 160 | −const toSecretResolutionContext = ( | |
| 161 | − value: unknown, | |
| 162 | −): SecretMaterialResolveContext | undefined => { | |
| 163 | − const decoded = decodeSecretResolutionContextEnvelope(value); | |
| 164 | − if (Either.isLeft(decoded) || decoded.right.params === undefined) { | |
| 165 | − return undefined; | |
| 166 | − } | |
| 167 | − | |
| 168 | − return { | |
| 169 | − params: decoded.right.params, | |
| 170 | − }; | |
| 171 | −}; | |
| 172 | − | |
| 173 | −const queryTokenWeight = (token: string): number => | |
| 174 | − LOW_SIGNAL_QUERY_TOKENS.has(token) ? 0.25 : 1; | |
| 175 | − | |
| 176 | −const loadWorkspaceCatalogTools = (input: { | |
| 177 | − workspaceId: Source["workspaceId"]; | |
| 178 | − accountId: AccountId; | |
| 179 | − sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 180 | − includeSchemas: boolean; | |
| 181 | −}): Effect.Effect<readonly LoadedSourceCatalogToolIndexEntry[], Error, WorkspaceStorageServices> => | |
| 182 | − Effect.map( | |
| 183 | − input.sourceCatalogStore.loadWorkspaceSourceCatalogToolIndex({ | |
| 184 | − workspaceId: input.workspaceId, | |
| 185 | − actorAccountId: input.accountId, | |
| 186 | − includeSchemas: input.includeSchemas, | |
| 187 | − }), | |
| 188 | − (tools) => | |
| 189 | − tools.filter( | |
| 190 | − (tool) => tool.source.enabled && tool.source.status === "connected", | |
| 191 | − ), | |
| 192 | − ); | |
| 193 | − | |
| 194 | −const loadWorkspaceCatalogToolByPath = (input: { | |
| 195 | − workspaceId: Source["workspaceId"]; | |
| 196 | − accountId: AccountId; | |
| 197 | − sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 198 | − path: string; | |
| 199 | − includeSchemas: boolean; | |
| 200 | −}): Effect.Effect<LoadedSourceCatalogToolIndexEntry | null, Error, WorkspaceStorageServices> => | |
| 201 | − input.sourceCatalogStore.loadWorkspaceSourceCatalogToolByPath({ | |
| 202 | − workspaceId: input.workspaceId, | |
| 203 | − path: input.path, | |
| 204 | − actorAccountId: input.accountId, | |
| 205 | − includeSchemas: input.includeSchemas, | |
| 206 | − }).pipe( | |
| 207 | − Effect.map((tool) => | |
| 208 | − tool && tool.source.enabled && tool.source.status === "connected" | |
| 209 | − ? tool | |
| 210 | − : null, | |
| 211 | − ), | |
| 212 | − ); | |
| 213 | − | |
| 214 | −const scoreCatalogTool = ( | |
| 215 | − queryTokens: readonly string[], | |
| 216 | − tool: LoadedSourceCatalogToolIndexEntry, | |
| 217 | −): number => { | |
| 218 | − const pathText = tool.path.toLowerCase(); | |
| 219 | − const namespaceText = tool.searchNamespace.toLowerCase(); | |
| 220 | − const toolIdText = tool.path.split(".").at(-1)?.toLowerCase() ?? ""; | |
| 221 | − const titleText = tool.capability.surface.title?.toLowerCase() ?? ""; | |
| 222 | − const descriptionText = | |
| 223 | − tool.capability.surface.summary?.toLowerCase() | |
| 224 | − ?? tool.capability.surface.description?.toLowerCase() | |
| 225 | − ?? ""; | |
| 226 | − const templateText = | |
| 227 | − tool.executable.protocol === "http" | |
| 228 | − ? tool.executable.pathTemplate.toLowerCase() | |
| 229 | − : tool.executable.protocol === "graphql" | |
| 230 | − ? tool.executable.rootField.toLowerCase() | |
| 231 | − : tool.executable.toolName.toLowerCase(); | |
| 232 | − | |
| 233 | − const pathTokens = tokenize(`${tool.path} ${toolIdText}`); | |
| 234 | − const namespaceTokens = tokenize(tool.searchNamespace); | |
| 235 | − const titleTokens = tokenize(tool.capability.surface.title ?? ""); | |
| 236 | − const templateTokens = tokenize(templateText); | |
| 237 | − | |
| 238 | − let score = 0; | |
| 239 | − let structuralHits = 0; | |
| 240 | − let namespaceHits = 0; | |
| 241 | − let pathHits = 0; | |
| 242 | − | |
| 243 | − for (const token of queryTokens) { | |
| 244 | − const weight = queryTokenWeight(token); | |
| 245 | − | |
| 246 | − if (hasTokenMatch(pathTokens, token)) { | |
| 247 | − score += 12 * weight; | |
| 248 | − structuralHits += 1; | |
| 249 | − pathHits += 1; | |
| 250 | − continue; | |
| 251 | − } | |
| 252 | − | |
| 253 | − if (hasTokenMatch(namespaceTokens, token)) { | |
| 254 | − score += 11 * weight; | |
| 255 | − structuralHits += 1; | |
| 256 | − namespaceHits += 1; | |
| 257 | − continue; | |
| 258 | − } | |
| 259 | − | |
| 260 | − if (hasTokenMatch(titleTokens, token)) { | |
| 261 | − score += 9 * weight; | |
| 262 | − structuralHits += 1; | |
| 263 | − continue; | |
| 264 | − } | |
| 265 | − | |
| 266 | − if (hasTokenMatch(templateTokens, token)) { | |
| 267 | − score += 8 * weight; | |
| 268 | − structuralHits += 1; | |
| 269 | − continue; | |
| 270 | − } | |
| 271 | − | |
| 272 | − if ( | |
| 273 | − hasSubstringMatch(pathText, token) || | |
| 274 | − hasSubstringMatch(toolIdText, token) | |
| 275 | − ) { | |
| 276 | − score += 6 * weight; | |
| 277 | − structuralHits += 1; | |
| 278 | − pathHits += 1; | |
| 279 | − continue; | |
| 280 | − } | |
| 281 | − | |
| 282 | − if (hasSubstringMatch(namespaceText, token)) { | |
| 283 | − score += 5 * weight; | |
| 284 | − structuralHits += 1; | |
| 285 | − namespaceHits += 1; | |
| 286 | − continue; | |
| 287 | − } | |
| 288 | − | |
| 289 | − if ( | |
| 290 | − hasSubstringMatch(titleText, token) || | |
| 291 | − hasSubstringMatch(templateText, token) | |
| 292 | − ) { | |
| 293 | − score += 4 * weight; | |
| 294 | − structuralHits += 1; | |
| 295 | − continue; | |
| 296 | − } | |
| 297 | − | |
| 298 | − if (hasSubstringMatch(descriptionText, token)) { | |
| 299 | − score += 0.5 * weight; | |
| 300 | − } | |
| 301 | − } | |
| 302 | − | |
| 303 | − const strongTokens = queryTokens.filter( | |
| 304 | − (token) => queryTokenWeight(token) >= 1, | |
| 305 | − ); | |
| 306 | − if (strongTokens.length >= 2) { | |
| 307 | − for (let index = 0; index < strongTokens.length - 1; index += 1) { | |
| 308 | − const current = strongTokens[index]!; | |
| 309 | − const next = strongTokens[index + 1]!; | |
| 310 | − const phrases = [ | |
| 311 | − `${current}-${next}`, | |
| 312 | − `${current}.${next}`, | |
| 313 | − `${current}/${next}`, | |
| 314 | − ]; | |
| 315 | − | |
| 316 | − if ( | |
| 317 | − phrases.some( | |
| 318 | − (phrase) => | |
| 319 | − pathText.includes(phrase) || templateText.includes(phrase), | |
| 320 | − ) | |
| 321 | − ) { | |
| 322 | − score += 10; | |
| 323 | − } | |
| 324 | − } | |
| 325 | − } | |
| 326 | − | |
| 327 | − if (namespaceHits > 0 && pathHits > 0) { | |
| 328 | − score += 8; | |
| 329 | − } | |
| 330 | − | |
| 331 | − if (structuralHits === 0 && score > 0) { | |
| 332 | − score *= 0.25; | |
| 333 | − } | |
| 334 | − | |
| 335 | − return score; | |
| 336 | −}; | |
| 337 | − | |
| 338 | −const approvalSchema = { | |
| 339 | − type: "object", | |
| 340 | − properties: { | |
| 341 | − approve: { | |
| 342 | − type: "boolean", | |
| 343 | − description: "Whether to approve this tool execution", | |
| 344 | − }, | |
| 345 | − }, | |
| 346 | − required: ["approve"], | |
| 347 | − additionalProperties: false, | |
| 348 | −} satisfies Record<string, unknown>; | |
| 349 | − | |
| 350 | −const approvalMessageForInvocation = ( | |
| 351 | − descriptor: InvocationDescriptor, | |
| 352 | −): string => { | |
| 353 | − if (descriptor.approvalLabel) { | |
| 354 | − return `Allow ${descriptor.approvalLabel}?`; | |
| 355 | − } | |
| 356 | − | |
| 357 | − return `Allow tool call: ${descriptor.toolPath}?`; | |
| 358 | −}; | |
| 359 | − | |
| 360 | −const toInvocationDescriptorFromCatalogTool = (input: { | |
| 361 | − tool: LoadedSourceCatalogToolIndexEntry; | |
| 362 | −}): InvocationDescriptor => invocationDescriptorFromTool(input) satisfies InvocationDescriptor; | |
| 363 | − | |
| 364 | −const authorizePersistedToolInvocation = (input: { | |
| 365 | − workspaceId: Source["workspaceId"]; | |
| 366 | − accountId: AccountId; | |
| 367 | − descriptor: InvocationDescriptor; | |
| 368 | − args: unknown; | |
| 369 | − source: Source; | |
| 370 | − context?: Record<string, unknown>; | |
| 371 | − onElicitation?: Parameters< | |
| 372 | − typeof makeToolInvokerFromTools | |
| 373 | − >[0]["onElicitation"]; | |
| 374 | −}): Effect.Effect<void, Error, WorkspaceStorageServices> => | |
| 375 | − Effect.gen(function* () { | |
| 376 | − const localWorkspacePolicies = yield* loadRuntimeLocalWorkspacePolicies( | |
| 377 | − input.workspaceId, | |
| 378 | − ).pipe( | |
| 379 | − Effect.mapError((cause) => | |
| 380 | − cause instanceof Error ? cause : new Error(String(cause)), | |
| 381 | − ), | |
| 382 | − ); | |
| 383 | − | |
| 384 | − const decision = evaluateInvocationPolicy({ | |
| 385 | − descriptor: input.descriptor, | |
| 386 | − args: input.args, | |
| 387 | − policies: localWorkspacePolicies.policies, | |
| 388 | − context: { | |
| 389 | − workspaceId: input.workspaceId, | |
| 390 | − }, | |
| 391 | − }); | |
| 392 | − | |
| 393 | − if (decision.kind === "allow") { | |
| 394 | − return; | |
| 395 | − } | |
| 396 | − | |
| 397 | − if (decision.kind === "deny") { | |
| 398 | − return yield* Effect.fail(new Error(decision.reason)); | |
| 399 | − } | |
| 400 | − | |
| 401 | − if (!input.onElicitation) { | |
| 402 | − return yield* Effect.fail( | |
| 403 | − new Error( | |
| 404 | − `Approval required for ${input.descriptor.toolPath}, but no elicitation-capable host is available`, | |
| 405 | − ), | |
| 406 | − ); | |
| 407 | − } | |
| 408 | − | |
| 409 | − const interactionId = | |
| 410 | − typeof input.context?.callId === "string" && | |
| 411 | − input.context.callId.length > 0 | |
| 412 | − ? `tool_execution_gate:${input.context.callId}` | |
| 413 | − : `tool_execution_gate:${crypto.randomUUID()}`; | |
| 414 | − const response = yield* input | |
| 415 | − .onElicitation({ | |
| 416 | − interactionId, | |
| 417 | − path: asToolPath(input.descriptor.toolPath), | |
| 418 | − sourceKey: input.source.id, | |
| 419 | − args: input.args, | |
| 420 | − context: { | |
| 421 | − ...(input.context ?? {}), | |
| 422 | − interactionPurpose: "tool_execution_gate", | |
| 423 | − interactionReason: decision.reason, | |
| 424 | − invocationDescriptor: { | |
| 425 | − operationKind: input.descriptor.operationKind, | |
| 426 | − interaction: input.descriptor.interaction, | |
| 427 | − approvalLabel: input.descriptor.approvalLabel, | |
| 428 | − sourceId: input.source.id, | |
| 429 | − sourceName: input.source.name, | |
| 430 | − }, | |
| 431 | − }, | |
| 432 | − elicitation: { | |
| 433 | − mode: "form", | |
| 434 | − message: approvalMessageForInvocation(input.descriptor), | |
| 435 | − requestedSchema: approvalSchema, | |
| 436 | − }, | |
| 437 | − }) | |
| 438 | − .pipe( | |
| 439 | − Effect.mapError((cause) => | |
| 440 | − cause instanceof Error ? cause : new Error(String(cause)), | |
| 441 | − ), | |
| 442 | − ); | |
| 443 | − | |
| 444 | − if (response.action !== "accept") { | |
| 445 | − return yield* Effect.fail( | |
| 446 | − new Error( | |
| 447 | − `Tool invocation not approved for ${input.descriptor.toolPath}`, | |
| 448 | − ), | |
| 449 | − ); | |
| 450 | − } | |
| 451 | − }); | |
| 452 | − | |
| 453 | −const provideRuntimeLocalWorkspace = <A, E, R>( | |
| 454 | − effect: Effect.Effect<A, E, R>, | |
| 455 | − runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null, | |
| 456 | −): Effect.Effect<A, E, R> => | |
| 457 | − provideOptionalRuntimeLocalWorkspace(effect, runtimeLocalWorkspace); | |
| 458 | − | |
| 459 | −const createWorkspaceSourceCatalog = (input: { | |
| 460 | − workspaceId: Source["workspaceId"]; | |
| 461 | − accountId: AccountId; | |
| 462 | − sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 463 | − workspaceConfigStore: WorkspaceConfigStoreShape; | |
| 464 | − workspaceStateStore: WorkspaceStateStoreShape; | |
| 465 | − sourceArtifactStore: SourceArtifactStoreShape; | |
| 466 | − runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null; | |
| 467 | −}): ToolCatalog => { | |
| 468 | − const workspaceStorageLayer = makeWorkspaceStorageLayer({ | |
| 469 | − workspaceConfigStore: input.workspaceConfigStore, | |
| 470 | − workspaceStateStore: input.workspaceStateStore, | |
| 471 | − sourceArtifactStore: input.sourceArtifactStore, | |
| 472 | − }); | |
| 473 | − const provideWorkspaceStorage = <A, E, R>(effect: Effect.Effect<A, E, R>) => | |
| 474 | − effect.pipe(Effect.provide(workspaceStorageLayer)); | |
| 475 | − | |
| 476 | − const createSharedCatalog = (includeSchemas: boolean): Effect.Effect<ToolCatalog, Error, never> => | |
| 477 | − provideWorkspaceStorage(Effect.gen(function* () { | |
| 478 | − const catalogTools = yield* loadWorkspaceCatalogTools({ | |
| 479 | − workspaceId: input.workspaceId, | |
| 480 | − accountId: input.accountId, | |
| 481 | − sourceCatalogStore: input.sourceCatalogStore, | |
| 482 | − includeSchemas, | |
| 483 | − }); | |
| 484 | − | |
| 485 | − return createToolCatalogFromEntries({ | |
| 486 | − entries: catalogTools.map((tool) => | |
| 487 | − catalogToolCatalogEntry({ | |
| 488 | − tool, | |
| 489 | − score: (queryTokens) => scoreCatalogTool(queryTokens, tool), | |
| 490 | − }), | |
| 491 | − ), | |
| 492 | − }); | |
| 493 | − })); | |
| 494 | − | |
| 495 | − return { | |
| 496 | − listNamespaces: ({ limit }) => | |
| 497 | − provideRuntimeLocalWorkspace( | |
| 498 | − Effect.flatMap(createSharedCatalog(false), (catalog) => | |
| 499 | − catalog.listNamespaces({ limit }), | |
| 500 | − ), | |
| 501 | − input.runtimeLocalWorkspace, | |
| 502 | − ), | |
| 503 | − | |
| 504 | − listTools: ({ namespace, query, limit, includeSchemas = false }) => | |
| 505 | − provideRuntimeLocalWorkspace( | |
| 506 | − Effect.flatMap(createSharedCatalog(includeSchemas), (catalog) => | |
| 507 | − catalog.listTools({ | |
| 508 | − ...(namespace !== undefined ? { namespace } : {}), | |
| 509 | − ...(query !== undefined ? { query } : {}), | |
| 510 | − limit, | |
| 511 | − includeSchemas, | |
| 512 | − }), | |
| 513 | − ), | |
| 514 | − input.runtimeLocalWorkspace, | |
| 515 | − ), | |
| 516 | − | |
| 517 | − getToolByPath: ({ path, includeSchemas }) => | |
| 518 | − provideRuntimeLocalWorkspace( | |
| 519 | − Effect.flatMap(createSharedCatalog(includeSchemas), (catalog) => | |
| 520 | − catalog.getToolByPath({ path, includeSchemas }), | |
| 521 | − ), | |
| 522 | − input.runtimeLocalWorkspace, | |
| 523 | − ), | |
| 524 | − | |
| 525 | − searchTools: ({ query, namespace, limit }) => | |
| 526 | − provideRuntimeLocalWorkspace( | |
| 527 | − Effect.flatMap(createSharedCatalog(false), (catalog) => | |
| 528 | − catalog.searchTools({ | |
| 529 | − query, | |
| 530 | − ...(namespace !== undefined ? { namespace } : {}), | |
| 531 | − limit, | |
| 532 | − }), | |
| 533 | − ), | |
| 534 | − input.runtimeLocalWorkspace, | |
| 535 | − ), | |
| 536 | − } satisfies ToolCatalog; | |
| 537 | −}; | |
| 538 | − | |
| 539 | −const createWorkspaceToolInvoker = (input: { | |
| 540 | − workspaceId: Source["workspaceId"]; | |
| 541 | − accountId: AccountId; | |
| 542 | − sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 543 | − workspaceConfigStore: WorkspaceConfigStoreShape; | |
| 544 | − workspaceStateStore: WorkspaceStateStoreShape; | |
| 545 | − sourceArtifactStore: SourceArtifactStoreShape; | |
| 546 | − sourceAuthMaterialService: Effect.Effect.Success<typeof RuntimeSourceAuthMaterialService>; | |
| 547 | − sourceAuthService: RuntimeSourceAuthService; | |
| 548 | − runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null; | |
| 549 | − localToolRuntime: LocalToolRuntime; | |
| 550 | − onElicitation?: Parameters< | |
| 551 | − typeof makeToolInvokerFromTools | |
| 552 | − >[0]["onElicitation"]; | |
| 553 | −}): { | |
| 554 | − catalog: ToolCatalog; | |
| 555 | − toolInvoker: ToolInvoker; | |
| 556 | −} => { | |
| 557 | − const workspaceStorageLayer = makeWorkspaceStorageLayer({ | |
| 558 | − workspaceConfigStore: input.workspaceConfigStore, | |
| 559 | − workspaceStateStore: input.workspaceStateStore, | |
| 560 | − sourceArtifactStore: input.sourceArtifactStore, | |
| 561 | − }); | |
| 562 | − const provideWorkspaceStorage = <A, E, R>(effect: Effect.Effect<A, E, R>) => | |
| 563 | − effect.pipe(Effect.provide(workspaceStorageLayer)); | |
| 564 | − | |
| 565 | − const executorTools = createExecutorToolMap({ | |
| 566 | − workspaceId: input.workspaceId, | |
| 567 | − accountId: input.accountId, | |
| 568 | − sourceAuthService: input.sourceAuthService, | |
| 569 | − runtimeLocalWorkspace: input.runtimeLocalWorkspace, | |
| 570 | − }); | |
| 571 | − const sourceCatalog = createWorkspaceSourceCatalog({ | |
| 572 | − workspaceId: input.workspaceId, | |
| 573 | − accountId: input.accountId, | |
| 574 | − sourceCatalogStore: input.sourceCatalogStore, | |
| 575 | − workspaceConfigStore: input.workspaceConfigStore, | |
| 576 | − workspaceStateStore: input.workspaceStateStore, | |
| 577 | − sourceArtifactStore: input.sourceArtifactStore, | |
| 578 | − runtimeLocalWorkspace: input.runtimeLocalWorkspace, | |
| 579 | − }); | |
| 580 | − let catalog: ToolCatalog | null = null; | |
| 581 | − const systemTools = createSystemToolMap({ | |
| 582 | − getCatalog: () => { | |
| 583 | − if (catalog === null) { | |
| 584 | − throw new Error("Workspace tool catalog has not been initialized"); | |
| 585 | − } | |
| 586 | − | |
| 587 | − return catalog; | |
| 588 | − }, | |
| 589 | − }); | |
| 590 | − const authoredTools = mergeToolMaps([ | |
| 591 | − systemTools, | |
| 592 | − executorTools, | |
| 593 | − input.localToolRuntime.tools, | |
| 594 | − ]); | |
| 595 | − const authoredCatalog = createToolCatalogFromTools({ | |
| 596 | − tools: authoredTools, | |
| 597 | − }); | |
| 598 | − catalog = mergeToolCatalogs({ | |
| 599 | − catalogs: [authoredCatalog, sourceCatalog], | |
| 600 | − }); | |
| 601 | − const authoredToolPaths = new Set(Object.keys(authoredTools)); | |
| 602 | − const authoredInvoker = makeToolInvokerFromTools({ | |
| 603 | − tools: authoredTools, | |
| 604 | − onElicitation: input.onElicitation, | |
| 605 | − }); | |
| 606 | − | |
| 607 | − const invokePersistedTool = (invocation: { | |
| 608 | − path: string; | |
| 609 | − args: unknown; | |
| 610 | − context?: Record<string, unknown>; | |
| 611 | − }) => | |
| 612 | − provideRuntimeLocalWorkspace( | |
| 613 | − provideWorkspaceStorage(Effect.gen(function* () { | |
| 614 | − const catalogTool = yield* loadWorkspaceCatalogToolByPath({ | |
| 615 | − workspaceId: input.workspaceId, | |
| 616 | − accountId: input.accountId, | |
| 617 | − sourceCatalogStore: input.sourceCatalogStore, | |
| 618 | − path: invocation.path, | |
| 619 | − includeSchemas: false, | |
| 620 | − }); | |
| 621 | − if (!catalogTool) { | |
| 622 | − return yield* Effect.fail( | |
| 623 | − new Error(`Unknown tool path: ${invocation.path}`), | |
| 624 | − ); | |
| 625 | − } | |
| 626 | − | |
| 627 | − yield* authorizePersistedToolInvocation({ | |
| 628 | − workspaceId: input.workspaceId, | |
| 629 | − accountId: input.accountId, | |
| 630 | − descriptor: toInvocationDescriptorFromCatalogTool({ | |
| 631 | − tool: catalogTool, | |
| 632 | − }), | |
| 633 | − args: invocation.args, | |
| 634 | − source: catalogTool.source, | |
| 635 | − context: invocation.context, | |
| 636 | − onElicitation: input.onElicitation, | |
| 637 | − }); | |
| 638 | − | |
| 639 | − const auth = yield* input.sourceAuthMaterialService.resolve({ | |
| 640 | − source: catalogTool.source, | |
| 641 | − actorAccountId: input.accountId, | |
| 642 | − context: toSecretResolutionContext(invocation.context), | |
| 643 | − }); | |
| 644 | − return yield* invokeIrTool({ | |
| 645 | − workspaceId: input.workspaceId, | |
| 646 | − accountId: input.accountId, | |
| 647 | − tool: catalogTool, | |
| 648 | − auth, | |
| 649 | − args: invocation.args, | |
| 650 | − onElicitation: input.onElicitation, | |
| 651 | − context: invocation.context, | |
| 652 | − }); | |
| 653 | − })), | |
| 654 | − input.runtimeLocalWorkspace, | |
| 655 | − ); | |
| 656 | − | |
| 657 | − return { | |
| 658 | − catalog, | |
| 659 | − toolInvoker: { | |
| 660 | − invoke: ({ path, args, context }) => | |
| 661 | − provideRuntimeLocalWorkspace( | |
| 662 | − authoredToolPaths.has(path) | |
| 663 | − ? authoredInvoker.invoke({ path, args, context }) | |
| 664 | − : invokePersistedTool({ path, args, context }), | |
| 665 | − input.runtimeLocalWorkspace, | |
| 666 | − ), | |
| 667 | − }, | |
| 668 | − }; | |
| 669 | −}; | |
| 670 | − | |
| 671 | 57 | export const createWorkspaceExecutionEnvironmentResolver = (input: { |
| 672 | 58 | sourceAuthMaterialService: Effect.Effect.Success<typeof RuntimeSourceAuthMaterialService>; |
| 673 | − sourceAuthService: RuntimeSourceAuthService; | |
| 59 | + sourceAuthService: Effect.Effect.Success<typeof RuntimeSourceAuthServiceTag>; | |
| 674 | 60 | sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; |
| 675 | 61 | localToolRuntimeLoader: LocalToolRuntimeLoaderShape; |
| 676 | 62 | workspaceConfigStore: WorkspaceConfigStoreShape; |
| @@ -686,12 +72,7 @@ export const createWorkspaceExecutionEnvironmentResolver = (input: { · 9 unmodified lines | ||
| 686 | 72 | : yield* input.workspaceConfigStore.load(runtimeLocalWorkspace.context); |
| 687 | 73 | const localToolRuntime = |
| 688 | 74 | runtimeLocalWorkspace === null |
| 689 | − ? { | |
| 690 | − tools: {}, | |
| 691 | − catalog: createToolCatalogFromTools({ tools: {} }), | |
| 692 | − toolInvoker: makeToolInvokerFromTools({ tools: {} }), | |
| 693 | − toolPaths: new Set<string>(), | |
| 694 | − } | |
| 75 | + ? createEmptyLocalToolRuntime() | |
| 695 | 76 | : yield* input.localToolRuntimeLoader.load(runtimeLocalWorkspace.context); |
| 696 | 77 | const { catalog, toolInvoker } = createWorkspaceToolInvoker({ |
| 697 | 78 | workspaceId, |
| @@ -0,0 +1,154 @@ | ||
| 1 | +import { type ToolPath, makeToolInvokerFromTools } from "@executor/codemode-core"; | |
| 2 | +import type { Source } from "#schema"; | |
| 3 | +import * as Either from "effect/Either"; | |
| 4 | +import * as Effect from "effect/Effect"; | |
| 5 | +import * as Schema from "effect/Schema"; | |
| 6 | + | |
| 7 | +import type { LoadedSourceCatalogToolIndexEntry } from "../catalog/source-catalog-runtime"; | |
| 8 | +import type { SecretMaterialResolveContext } from "../local/secret-material-providers"; | |
| 9 | +import type { WorkspaceStorageServices } from "../local/local-storage"; | |
| 10 | +import { invocationDescriptorFromTool } from "./ir-execution"; | |
| 11 | +import { evaluateInvocationPolicy } from "../policy/invocation-policy-engine"; | |
| 12 | +import { loadRuntimeLocalWorkspacePolicies } from "../policy/policies-operations"; | |
| 13 | + | |
| 14 | +const asToolPath = (value: string): ToolPath => value as ToolPath; | |
| 15 | + | |
| 16 | +const approvalSchema = { | |
| 17 | + type: "object", | |
| 18 | + properties: { | |
| 19 | + approve: { | |
| 20 | + type: "boolean", | |
| 21 | + description: "Whether to approve this tool execution", | |
| 22 | + }, | |
| 23 | + }, | |
| 24 | + required: ["approve"], | |
| 25 | + additionalProperties: false, | |
| 26 | +} satisfies Record<string, unknown>; | |
| 27 | + | |
| 28 | +const approvalMessageForInvocation = ( | |
| 29 | + descriptor: ReturnType<typeof invocationDescriptorFromTool>, | |
| 30 | +): string => { | |
| 31 | + if (descriptor.approvalLabel) { | |
| 32 | + return `Allow ${descriptor.approvalLabel}?`; | |
| 33 | + } | |
| 34 | + | |
| 35 | + return `Allow tool call: ${descriptor.toolPath}?`; | |
| 36 | +}; | |
| 37 | + | |
| 38 | +const SecretResolutionContextEnvelopeSchema = Schema.Struct({ | |
| 39 | + params: Schema.optional( | |
| 40 | + Schema.Record({ | |
| 41 | + key: Schema.String, | |
| 42 | + value: Schema.String, | |
| 43 | + }), | |
| 44 | + ), | |
| 45 | +}); | |
| 46 | + | |
| 47 | +const decodeSecretResolutionContextEnvelope = Schema.decodeUnknownEither( | |
| 48 | + SecretResolutionContextEnvelopeSchema, | |
| 49 | +); | |
| 50 | + | |
| 51 | +export const toSecretResolutionContext = ( | |
| 52 | + value: unknown, | |
| 53 | +): SecretMaterialResolveContext | undefined => { | |
| 54 | + const decoded = decodeSecretResolutionContextEnvelope(value); | |
| 55 | + if (Either.isLeft(decoded) || decoded.right.params === undefined) { | |
| 56 | + return undefined; | |
| 57 | + } | |
| 58 | + | |
| 59 | + return { | |
| 60 | + params: decoded.right.params, | |
| 61 | + }; | |
| 62 | +}; | |
| 63 | + | |
| 64 | +type WorkspaceToolElicitation = Parameters< | |
| 65 | + typeof makeToolInvokerFromTools | |
| 66 | +>[0]["onElicitation"]; | |
| 67 | + | |
| 68 | +export const authorizePersistedToolInvocation = (input: { | |
| 69 | + workspaceId: Source["workspaceId"]; | |
| 70 | + tool: LoadedSourceCatalogToolIndexEntry; | |
| 71 | + args: unknown; | |
| 72 | + context?: Record<string, unknown>; | |
| 73 | + onElicitation?: WorkspaceToolElicitation; | |
| 74 | +}): Effect.Effect<void, Error, WorkspaceStorageServices> => | |
| 75 | + Effect.gen(function* () { | |
| 76 | + const descriptor = invocationDescriptorFromTool({ | |
| 77 | + tool: input.tool, | |
| 78 | + }); | |
| 79 | + const localWorkspacePolicies = yield* loadRuntimeLocalWorkspacePolicies( | |
| 80 | + input.workspaceId, | |
| 81 | + ).pipe( | |
| 82 | + Effect.mapError((cause) => | |
| 83 | + cause instanceof Error ? cause : new Error(String(cause)), | |
| 84 | + ), | |
| 85 | + ); | |
| 86 | + | |
| 87 | + const decision = evaluateInvocationPolicy({ | |
| 88 | + descriptor, | |
| 89 | + args: input.args, | |
| 90 | + policies: localWorkspacePolicies.policies, | |
| 91 | + context: { | |
| 92 | + workspaceId: input.workspaceId, | |
| 93 | + }, | |
| 94 | + }); | |
| 95 | + | |
| 96 | + if (decision.kind === "allow") { | |
| 97 | + return; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if (decision.kind === "deny") { | |
| 101 | + return yield* Effect.fail(new Error(decision.reason)); | |
| 102 | + } | |
| 103 | + | |
| 104 | + if (!input.onElicitation) { | |
| 105 | + return yield* Effect.fail( | |
| 106 | + new Error( | |
| 107 | + `Approval required for ${descriptor.toolPath}, but no elicitation-capable host is available`, | |
| 108 | + ), | |
| 109 | + ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + const interactionId = | |
| 113 | + typeof input.context?.callId === "string" && | |
| 114 | + input.context.callId.length > 0 | |
| 115 | + ? `tool_execution_gate:${input.context.callId}` | |
| 116 | + : `tool_execution_gate:${crypto.randomUUID()}`; | |
| 117 | + const response = yield* input | |
| 118 | + .onElicitation({ | |
| 119 | + interactionId, | |
| 120 | + path: asToolPath(descriptor.toolPath), | |
| 121 | + sourceKey: input.tool.source.id, | |
| 122 | + args: input.args, | |
| 123 | + context: { | |
| 124 | + ...(input.context ?? {}), | |
| 125 | + interactionPurpose: "tool_execution_gate", | |
| 126 | + interactionReason: decision.reason, | |
| 127 | + invocationDescriptor: { | |
| 128 | + operationKind: descriptor.operationKind, | |
| 129 | + interaction: descriptor.interaction, | |
| 130 | + approvalLabel: descriptor.approvalLabel, | |
| 131 | + sourceId: input.tool.source.id, | |
| 132 | + sourceName: input.tool.source.name, | |
| 133 | + }, | |
| 134 | + }, | |
| 135 | + elicitation: { | |
| 136 | + mode: "form", | |
| 137 | + message: approvalMessageForInvocation(descriptor), | |
| 138 | + requestedSchema: approvalSchema, | |
| 139 | + }, | |
| 140 | + }) | |
| 141 | + .pipe( | |
| 142 | + Effect.mapError((cause) => | |
| 143 | + cause instanceof Error ? cause : new Error(String(cause)), | |
| 144 | + ), | |
| 145 | + ); | |
| 146 | + | |
| 147 | + if (response.action !== "accept") { | |
| 148 | + return yield* Effect.fail( | |
| 149 | + new Error( | |
| 150 | + `Tool invocation not approved for ${descriptor.toolPath}`, | |
| 151 | + ), | |
| 152 | + ); | |
| 153 | + } | |
| 154 | + }); | |
| @@ -0,0 +1,12 @@ | ||
| 1 | +import * as Effect from "effect/Effect"; | |
| 2 | + | |
| 3 | +import { | |
| 4 | + provideOptionalRuntimeLocalWorkspace, | |
| 5 | + type RuntimeLocalWorkspaceState, | |
| 6 | +} from "../local/local-runtime-context"; | |
| 7 | + | |
| 8 | +export const provideRuntimeLocalWorkspace = <A, E, R>( | |
| 9 | + effect: Effect.Effect<A, E, R>, | |
| 10 | + runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null, | |
| 11 | +): Effect.Effect<A, E, R> => | |
| 12 | + provideOptionalRuntimeLocalWorkspace(effect, runtimeLocalWorkspace); | |
| @@ -0,0 +1,324 @@ | ||
| 1 | +import { | |
| 2 | + createToolCatalogFromEntries, | |
| 3 | + type ToolCatalog, | |
| 4 | +} from "@executor/codemode-core"; | |
| 5 | +import type { AccountId, Source } from "#schema"; | |
| 6 | +import * as Effect from "effect/Effect"; | |
| 7 | + | |
| 8 | +import { | |
| 9 | + RuntimeSourceCatalogStoreService, | |
| 10 | + type LoadedSourceCatalogToolIndexEntry, | |
| 11 | + catalogToolCatalogEntry, | |
| 12 | +} from "../catalog/source-catalog-runtime"; | |
| 13 | +import type { RuntimeLocalWorkspaceState } from "../local/local-runtime-context"; | |
| 14 | +import { | |
| 15 | + makeWorkspaceStorageLayer, | |
| 16 | + type SourceArtifactStoreShape, | |
| 17 | + type WorkspaceConfigStoreShape, | |
| 18 | + type WorkspaceStateStoreShape, | |
| 19 | + type WorkspaceStorageServices, | |
| 20 | +} from "../local/local-storage"; | |
| 21 | +import { provideRuntimeLocalWorkspace } from "./workspace-runtime-local"; | |
| 22 | + | |
| 23 | +const tokenize = (value: string): string[] => | |
| 24 | + value | |
| 25 | + .trim() | |
| 26 | + .toLowerCase() | |
| 27 | + .split(/[^a-z0-9]+/) | |
| 28 | + .filter(Boolean); | |
| 29 | + | |
| 30 | +const LOW_SIGNAL_QUERY_TOKENS = new Set([ | |
| 31 | + "a", | |
| 32 | + "an", | |
| 33 | + "the", | |
| 34 | + "am", | |
| 35 | + "as", | |
| 36 | + "for", | |
| 37 | + "from", | |
| 38 | + "get", | |
| 39 | + "i", | |
| 40 | + "in", | |
| 41 | + "is", | |
| 42 | + "list", | |
| 43 | + "me", | |
| 44 | + "my", | |
| 45 | + "of", | |
| 46 | + "on", | |
| 47 | + "or", | |
| 48 | + "signed", | |
| 49 | + "to", | |
| 50 | + "who", | |
| 51 | +]); | |
| 52 | + | |
| 53 | +const singularizeToken = (value: string): string => | |
| 54 | + value.length > 3 && value.endsWith("s") ? value.slice(0, -1) : value; | |
| 55 | + | |
| 56 | +const tokenEquals = (left: string, right: string): boolean => | |
| 57 | + left === right || singularizeToken(left) === singularizeToken(right); | |
| 58 | + | |
| 59 | +const hasTokenMatch = ( | |
| 60 | + tokens: readonly string[], | |
| 61 | + queryToken: string, | |
| 62 | +): boolean => tokens.some((token) => tokenEquals(token, queryToken)); | |
| 63 | + | |
| 64 | +const hasSubstringMatch = (value: string, queryToken: string): boolean => { | |
| 65 | + if (value.includes(queryToken)) { | |
| 66 | + return true; | |
| 67 | + } | |
| 68 | + | |
| 69 | + const singular = singularizeToken(queryToken); | |
| 70 | + return singular !== queryToken && value.includes(singular); | |
| 71 | +}; | |
| 72 | + | |
| 73 | +const queryTokenWeight = (token: string): number => | |
| 74 | + LOW_SIGNAL_QUERY_TOKENS.has(token) ? 0.25 : 1; | |
| 75 | + | |
| 76 | +export const loadWorkspaceCatalogTools = (input: { | |
| 77 | + workspaceId: Source["workspaceId"]; | |
| 78 | + accountId: AccountId; | |
| 79 | + sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 80 | + includeSchemas: boolean; | |
| 81 | +}): Effect.Effect< | |
| 82 | + readonly LoadedSourceCatalogToolIndexEntry[], | |
| 83 | + Error, | |
| 84 | + WorkspaceStorageServices | |
| 85 | +> => | |
| 86 | + Effect.map( | |
| 87 | + input.sourceCatalogStore.loadWorkspaceSourceCatalogToolIndex({ | |
| 88 | + workspaceId: input.workspaceId, | |
| 89 | + actorAccountId: input.accountId, | |
| 90 | + includeSchemas: input.includeSchemas, | |
| 91 | + }), | |
| 92 | + (tools) => | |
| 93 | + tools.filter( | |
| 94 | + (tool) => tool.source.enabled && tool.source.status === "connected", | |
| 95 | + ), | |
| 96 | + ); | |
| 97 | + | |
| 98 | +export const loadWorkspaceCatalogToolByPath = (input: { | |
| 99 | + workspaceId: Source["workspaceId"]; | |
| 100 | + accountId: AccountId; | |
| 101 | + sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 102 | + path: string; | |
| 103 | + includeSchemas: boolean; | |
| 104 | +}): Effect.Effect< | |
| 105 | + LoadedSourceCatalogToolIndexEntry | null, | |
| 106 | + Error, | |
| 107 | + WorkspaceStorageServices | |
| 108 | +> => | |
| 109 | + input.sourceCatalogStore.loadWorkspaceSourceCatalogToolByPath({ | |
| 110 | + workspaceId: input.workspaceId, | |
| 111 | + path: input.path, | |
| 112 | + actorAccountId: input.accountId, | |
| 113 | + includeSchemas: input.includeSchemas, | |
| 114 | + }).pipe( | |
| 115 | + Effect.map((tool) => | |
| 116 | + tool && tool.source.enabled && tool.source.status === "connected" | |
| 117 | + ? tool | |
| 118 | + : null, | |
| 119 | + ), | |
| 120 | + ); | |
| 121 | + | |
| 122 | +const scoreCatalogTool = ( | |
| 123 | + queryTokens: readonly string[], | |
| 124 | + tool: LoadedSourceCatalogToolIndexEntry, | |
| 125 | +): number => { | |
| 126 | + const pathText = tool.path.toLowerCase(); | |
| 127 | + const namespaceText = tool.searchNamespace.toLowerCase(); | |
| 128 | + const toolIdText = tool.path.split(".").at(-1)?.toLowerCase() ?? ""; | |
| 129 | + const titleText = tool.capability.surface.title?.toLowerCase() ?? ""; | |
| 130 | + const descriptionText = | |
| 131 | + tool.capability.surface.summary?.toLowerCase() | |
| 132 | + ?? tool.capability.surface.description?.toLowerCase() | |
| 133 | + ?? ""; | |
| 134 | + const templateText = | |
| 135 | + tool.executable.protocol === "http" | |
| 136 | + ? tool.executable.pathTemplate.toLowerCase() | |
| 137 | + : tool.executable.protocol === "graphql" | |
| 138 | + ? tool.executable.rootField.toLowerCase() | |
| 139 | + : tool.executable.toolName.toLowerCase(); | |
| 140 | + | |
| 141 | + const pathTokens = tokenize(`${tool.path} ${toolIdText}`); | |
| 142 | + const namespaceTokens = tokenize(tool.searchNamespace); | |
| 143 | + const titleTokens = tokenize(tool.capability.surface.title ?? ""); | |
| 144 | + const templateTokens = tokenize(templateText); | |
| 145 | + | |
| 146 | + let score = 0; | |
| 147 | + let structuralHits = 0; | |
| 148 | + let namespaceHits = 0; | |
| 149 | + let pathHits = 0; | |
| 150 | + | |
| 151 | + for (const token of queryTokens) { | |
| 152 | + const weight = queryTokenWeight(token); | |
| 153 | + | |
| 154 | + if (hasTokenMatch(pathTokens, token)) { | |
| 155 | + score += 12 * weight; | |
| 156 | + structuralHits += 1; | |
| 157 | + pathHits += 1; | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + | |
| 161 | + if (hasTokenMatch(namespaceTokens, token)) { | |
| 162 | + score += 11 * weight; | |
| 163 | + structuralHits += 1; | |
| 164 | + namespaceHits += 1; | |
| 165 | + continue; | |
| 166 | + } | |
| 167 | + | |
| 168 | + if (hasTokenMatch(titleTokens, token)) { | |
| 169 | + score += 9 * weight; | |
| 170 | + structuralHits += 1; | |
| 171 | + continue; | |
| 172 | + } | |
| 173 | + | |
| 174 | + if (hasTokenMatch(templateTokens, token)) { | |
| 175 | + score += 8 * weight; | |
| 176 | + structuralHits += 1; | |
| 177 | + continue; | |
| 178 | + } | |
| 179 | + | |
| 180 | + if ( | |
| 181 | + hasSubstringMatch(pathText, token) || | |
| 182 | + hasSubstringMatch(toolIdText, token) | |
| 183 | + ) { | |
| 184 | + score += 6 * weight; | |
| 185 | + structuralHits += 1; | |
| 186 | + pathHits += 1; | |
| 187 | + continue; | |
| 188 | + } | |
| 189 | + | |
| 190 | + if (hasSubstringMatch(namespaceText, token)) { | |
| 191 | + score += 5 * weight; | |
| 192 | + structuralHits += 1; | |
| 193 | + namespaceHits += 1; | |
| 194 | + continue; | |
| 195 | + } | |
| 196 | + | |
| 197 | + if ( | |
| 198 | + hasSubstringMatch(titleText, token) || | |
| 199 | + hasSubstringMatch(templateText, token) | |
| 200 | + ) { | |
| 201 | + score += 4 * weight; | |
| 202 | + structuralHits += 1; | |
| 203 | + continue; | |
| 204 | + } | |
| 205 | + | |
| 206 | + if (hasSubstringMatch(descriptionText, token)) { | |
| 207 | + score += 0.5 * weight; | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + const strongTokens = queryTokens.filter( | |
| 212 | + (token) => queryTokenWeight(token) >= 1, | |
| 213 | + ); | |
| 214 | + if (strongTokens.length >= 2) { | |
| 215 | + for (let index = 0; index < strongTokens.length - 1; index += 1) { | |
| 216 | + const current = strongTokens[index]!; | |
| 217 | + const next = strongTokens[index + 1]!; | |
| 218 | + const phrases = [ | |
| 219 | + `${current}-${next}`, | |
| 220 | + `${current}.${next}`, | |
| 221 | + `${current}/${next}`, | |
| 222 | + ]; | |
| 223 | + | |
| 224 | + if ( | |
| 225 | + phrases.some( | |
| 226 | + (phrase) => | |
| 227 | + pathText.includes(phrase) || templateText.includes(phrase), | |
| 228 | + ) | |
| 229 | + ) { | |
| 230 | + score += 10; | |
| 231 | + } | |
| 232 | + } | |
| 233 | + } | |
| 234 | + | |
| 235 | + if (namespaceHits > 0 && pathHits > 0) { | |
| 236 | + score += 8; | |
| 237 | + } | |
| 238 | + | |
| 239 | + if (structuralHits === 0 && score > 0) { | |
| 240 | + score *= 0.25; | |
| 241 | + } | |
| 242 | + | |
| 243 | + return score; | |
| 244 | +}; | |
| 245 | + | |
| 246 | +export const createWorkspaceSourceCatalog = (input: { | |
| 247 | + workspaceId: Source["workspaceId"]; | |
| 248 | + accountId: AccountId; | |
| 249 | + sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 250 | + workspaceConfigStore: WorkspaceConfigStoreShape; | |
| 251 | + workspaceStateStore: WorkspaceStateStoreShape; | |
| 252 | + sourceArtifactStore: SourceArtifactStoreShape; | |
| 253 | + runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null; | |
| 254 | +}): ToolCatalog => { | |
| 255 | + const workspaceStorageLayer = makeWorkspaceStorageLayer({ | |
| 256 | + workspaceConfigStore: input.workspaceConfigStore, | |
| 257 | + workspaceStateStore: input.workspaceStateStore, | |
| 258 | + sourceArtifactStore: input.sourceArtifactStore, | |
| 259 | + }); | |
| 260 | + const provideWorkspaceStorage = <A, E, R>(effect: Effect.Effect<A, E, R>) => | |
| 261 | + effect.pipe(Effect.provide(workspaceStorageLayer)); | |
| 262 | + | |
| 263 | + const createSharedCatalog = (includeSchemas: boolean): Effect.Effect<ToolCatalog, Error, never> => | |
| 264 | + provideWorkspaceStorage(Effect.gen(function* () { | |
| 265 | + const catalogTools = yield* loadWorkspaceCatalogTools({ | |
| 266 | + workspaceId: input.workspaceId, | |
| 267 | + accountId: input.accountId, | |
| 268 | + sourceCatalogStore: input.sourceCatalogStore, | |
| 269 | + includeSchemas, | |
| 270 | + }); | |
| 271 | + | |
| 272 | + return createToolCatalogFromEntries({ | |
| 273 | + entries: catalogTools.map((tool) => | |
| 274 | + catalogToolCatalogEntry({ | |
| 275 | + tool, | |
| 276 | + score: (queryTokens) => scoreCatalogTool(queryTokens, tool), | |
| 277 | + }), | |
| 278 | + ), | |
| 279 | + }); | |
| 280 | + })); | |
| 281 | + | |
| 282 | + return { | |
| 283 | + listNamespaces: ({ limit }) => | |
| 284 | + provideRuntimeLocalWorkspace( | |
| 285 | + Effect.flatMap(createSharedCatalog(false), (catalog) => | |
| 286 | + catalog.listNamespaces({ limit }), | |
| 287 | + ), | |
| 288 | + input.runtimeLocalWorkspace, | |
| 289 | + ), | |
| 290 | + | |
| 291 | + listTools: ({ namespace, query, limit, includeSchemas = false }) => | |
| 292 | + provideRuntimeLocalWorkspace( | |
| 293 | + Effect.flatMap(createSharedCatalog(includeSchemas), (catalog) => | |
| 294 | + catalog.listTools({ | |
| 295 | + ...(namespace !== undefined ? { namespace } : {}), | |
| 296 | + ...(query !== undefined ? { query } : {}), | |
| 297 | + limit, | |
| 298 | + includeSchemas, | |
| 299 | + }), | |
| 300 | + ), | |
| 301 | + input.runtimeLocalWorkspace, | |
| 302 | + ), | |
| 303 | + | |
| 304 | + getToolByPath: ({ path, includeSchemas }) => | |
| 305 | + provideRuntimeLocalWorkspace( | |
| 306 | + Effect.flatMap(createSharedCatalog(includeSchemas), (catalog) => | |
| 307 | + catalog.getToolByPath({ path, includeSchemas }), | |
| 308 | + ), | |
| 309 | + input.runtimeLocalWorkspace, | |
| 310 | + ), | |
| 311 | + | |
| 312 | + searchTools: ({ query, namespace, limit }) => | |
| 313 | + provideRuntimeLocalWorkspace( | |
| 314 | + Effect.flatMap(createSharedCatalog(false), (catalog) => | |
| 315 | + catalog.searchTools({ | |
| 316 | + query, | |
| 317 | + ...(namespace !== undefined ? { namespace } : {}), | |
| 318 | + limit, | |
| 319 | + }), | |
| 320 | + ), | |
| 321 | + input.runtimeLocalWorkspace, | |
| 322 | + ), | |
| 323 | + } satisfies ToolCatalog; | |
| 324 | +}; | |
| @@ -0,0 +1,166 @@ | ||
| 1 | +import { | |
| 2 | + createSystemToolMap, | |
| 3 | + createToolCatalogFromTools, | |
| 4 | + makeToolInvokerFromTools, | |
| 5 | + mergeToolCatalogs, | |
| 6 | + mergeToolMaps, | |
| 7 | + type ToolCatalog, | |
| 8 | + type ToolInvoker, | |
| 9 | +} from "@executor/codemode-core"; | |
| 10 | +import type { AccountId, Source } from "#schema"; | |
| 11 | +import * as Effect from "effect/Effect"; | |
| 12 | + | |
| 13 | +import { RuntimeSourceAuthMaterialService } from "../auth/source-auth-material"; | |
| 14 | +import { RuntimeSourceCatalogStoreService } from "../catalog/source-catalog-runtime"; | |
| 15 | +import type { RuntimeLocalWorkspaceState } from "../local/local-runtime-context"; | |
| 16 | +import { | |
| 17 | + type LocalToolRuntime, | |
| 18 | +} from "../local/local-tools"; | |
| 19 | +import { | |
| 20 | + makeWorkspaceStorageLayer, | |
| 21 | + type SourceArtifactStoreShape, | |
| 22 | + type WorkspaceConfigStoreShape, | |
| 23 | + type WorkspaceStateStoreShape, | |
| 24 | +} from "../local/local-storage"; | |
| 25 | +import { | |
| 26 | + RuntimeSourceAuthService, | |
| 27 | +} from "../sources/source-auth-service"; | |
| 28 | +import { createExecutorToolMap } from "../sources/executor-tools"; | |
| 29 | +import { invokeIrTool } from "./ir-execution"; | |
| 30 | +import { | |
| 31 | + authorizePersistedToolInvocation, | |
| 32 | + toSecretResolutionContext, | |
| 33 | +} from "./workspace-persisted-tool-authorization"; | |
| 34 | +import { provideRuntimeLocalWorkspace } from "./workspace-runtime-local"; | |
| 35 | +import { | |
| 36 | + createWorkspaceSourceCatalog, | |
| 37 | + loadWorkspaceCatalogToolByPath, | |
| 38 | +} from "./workspace-source-catalog"; | |
| 39 | + | |
| 40 | +export const createWorkspaceToolInvoker = (input: { | |
| 41 | + workspaceId: Source["workspaceId"]; | |
| 42 | + accountId: AccountId; | |
| 43 | + sourceCatalogStore: Effect.Effect.Success<typeof RuntimeSourceCatalogStoreService>; | |
| 44 | + workspaceConfigStore: WorkspaceConfigStoreShape; | |
| 45 | + workspaceStateStore: WorkspaceStateStoreShape; | |
| 46 | + sourceArtifactStore: SourceArtifactStoreShape; | |
| 47 | + sourceAuthMaterialService: Effect.Effect.Success<typeof RuntimeSourceAuthMaterialService>; | |
| 48 | + sourceAuthService: RuntimeSourceAuthService; | |
| 49 | + runtimeLocalWorkspace: RuntimeLocalWorkspaceState | null; | |
| 50 | + localToolRuntime: LocalToolRuntime; | |
| 51 | + onElicitation?: Parameters< | |
| 52 | + typeof makeToolInvokerFromTools | |
| 53 | + >[0]["onElicitation"]; | |
| 54 | +}): { | |
| 55 | + catalog: ToolCatalog; | |
| 56 | + toolInvoker: ToolInvoker; | |
| 57 | +} => { | |
| 58 | + const workspaceStorageLayer = makeWorkspaceStorageLayer({ | |
| 59 | + workspaceConfigStore: input.workspaceConfigStore, | |
| 60 | + workspaceStateStore: input.workspaceStateStore, | |
| 61 | + sourceArtifactStore: input.sourceArtifactStore, | |
| 62 | + }); | |
| 63 | + const provideWorkspaceStorage = <A, E, R>(effect: Effect.Effect<A, E, R>) => | |
| 64 | + effect.pipe(Effect.provide(workspaceStorageLayer)); | |
| 65 | + | |
| 66 | + const executorTools = createExecutorToolMap({ | |
| 67 | + workspaceId: input.workspaceId, | |
| 68 | + accountId: input.accountId, | |
| 69 | + sourceAuthService: input.sourceAuthService, | |
| 70 | + runtimeLocalWorkspace: input.runtimeLocalWorkspace, | |
| 71 | + }); | |
| 72 | + const sourceCatalog = createWorkspaceSourceCatalog({ | |
| 73 | + workspaceId: input.workspaceId, | |
| 74 | + accountId: input.accountId, | |
| 75 | + sourceCatalogStore: input.sourceCatalogStore, | |
| 76 | + workspaceConfigStore: input.workspaceConfigStore, | |
| 77 | + workspaceStateStore: input.workspaceStateStore, | |
| 78 | + sourceArtifactStore: input.sourceArtifactStore, | |
| 79 | + runtimeLocalWorkspace: input.runtimeLocalWorkspace, | |
| 80 | + }); | |
| 81 | + let catalog: ToolCatalog | null = null; | |
| 82 | + const systemTools = createSystemToolMap({ | |
| 83 | + getCatalog: () => { | |
| 84 | + if (catalog === null) { | |
| 85 | + throw new Error("Workspace tool catalog has not been initialized"); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return catalog; | |
| 89 | + }, | |
| 90 | + }); | |
| 91 | + const authoredTools = mergeToolMaps([ | |
| 92 | + systemTools, | |
| 93 | + executorTools, | |
| 94 | + input.localToolRuntime.tools, | |
| 95 | + ]); | |
| 96 | + const authoredCatalog = createToolCatalogFromTools({ | |
| 97 | + tools: authoredTools, | |
| 98 | + }); | |
| 99 | + catalog = mergeToolCatalogs({ | |
| 100 | + catalogs: [authoredCatalog, sourceCatalog], | |
| 101 | + }); | |
| 102 | + const authoredToolPaths = new Set(Object.keys(authoredTools)); | |
| 103 | + const authoredInvoker = makeToolInvokerFromTools({ | |
| 104 | + tools: authoredTools, | |
| 105 | + onElicitation: input.onElicitation, | |
| 106 | + }); | |
| 107 | + | |
| 108 | + const invokePersistedTool = (invocation: { | |
| 109 | + path: string; | |
| 110 | + args: unknown; | |
| 111 | + context?: Record<string, unknown>; | |
| 112 | + }) => | |
| 113 | + provideRuntimeLocalWorkspace( | |
| 114 | + provideWorkspaceStorage(Effect.gen(function* () { | |
| 115 | + const catalogTool = yield* loadWorkspaceCatalogToolByPath({ | |
| 116 | + workspaceId: input.workspaceId, | |
| 117 | + accountId: input.accountId, | |
| 118 | + sourceCatalogStore: input.sourceCatalogStore, | |
| 119 | + path: invocation.path, | |
| 120 | + includeSchemas: false, | |
| 121 | + }); | |
| 122 | + if (!catalogTool) { | |
| 123 | + return yield* Effect.fail( | |
| 124 | + new Error(`Unknown tool path: ${invocation.path}`), | |
| 125 | + ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + yield* authorizePersistedToolInvocation({ | |
| 129 | + workspaceId: input.workspaceId, | |
| 130 | + tool: catalogTool, | |
| 131 | + args: invocation.args, | |
| 132 | + context: invocation.context, | |
| 133 | + onElicitation: input.onElicitation, | |
| 134 | + }); | |
| 135 | + | |
| 136 | + const auth = yield* input.sourceAuthMaterialService.resolve({ | |
| 137 | + source: catalogTool.source, | |
| 138 | + actorAccountId: input.accountId, | |
| 139 | + context: toSecretResolutionContext(invocation.context), | |
| 140 | + }); | |
| 141 | + return yield* invokeIrTool({ | |
| 142 | + workspaceId: input.workspaceId, | |
| 143 | + accountId: input.accountId, | |
| 144 | + tool: catalogTool, | |
| 145 | + auth, | |
| 146 | + args: invocation.args, | |
| 147 | + onElicitation: input.onElicitation, | |
| 148 | + context: invocation.context, | |
| 149 | + }); | |
| 150 | + })), | |
| 151 | + input.runtimeLocalWorkspace, | |
| 152 | + ); | |
| 153 | + | |
| 154 | + return { | |
| 155 | + catalog, | |
| 156 | + toolInvoker: { | |
| 157 | + invoke: ({ path, args, context }) => | |
| 158 | + provideRuntimeLocalWorkspace( | |
| 159 | + authoredToolPaths.has(path) | |
| 160 | + ? authoredInvoker.invoke({ path, args, context }) | |
| 161 | + : invokePersistedTool({ path, args, context }), | |
| 162 | + input.runtimeLocalWorkspace, | |
| 163 | + ), | |
| 164 | + }, | |
| 165 | + }; | |
| 166 | +}; | |