Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { FileSystem } from 'effect/FileSystem'; |
| 2 | import { Path } from 'effect/Path'; |
| 3 | import { Effect } from 'effect'; |
| 4 | |
| 5 | import { getErrorLocationFrom } from './get-error-location-from-file-path'; |
| 6 | import { getSourceCode } from './get-source-code'; |
| 7 | import { getSourcesFromMapFile } from './get-sources-from-map-file'; |
| 8 | import { type ErrorRelatedSources, MappedSources, type RawErrorLocation } from './mapped-sources'; |
| 9 | |
| 10 | export const getErrorRelatedSources = ( |
| 11 | name: string, |
| 12 | sourceFile: string |
| 13 | ): Effect.Effect<ErrorRelatedSources | RawErrorLocation | undefined, never, FileSystem | Path> => |
| 14 | Effect.gen(function* () { |
| 15 | const location = getErrorLocationFrom(sourceFile); |
| 16 | if (location === undefined) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | const { filePath, line, column } = location; |
| 21 | |
| 22 | const isTypescriptFile = filePath.endsWith('.ts') || filePath.endsWith('.tsx'); |
| 23 | if (isTypescriptFile) { |
| 24 | const path = yield* Path; |
| 25 | const workingDirectory = path.resolve('.'); |
| 26 | |
| 27 | // Reading the source is best-effort enrichment, exactly as it is for the |
| 28 | // source-map branch below: an unreadable file falls back to the location. |
| 29 | return yield* getSourceCode(location).pipe( |
| 30 | Effect.map(source => |
| 31 | MappedSources.sources({ |
| 32 | name, |
| 33 | runPath: `${filePath}:${line}:${column}`, |
| 34 | sourcesPath: undefined, |
| 35 | source, |
| 36 | }) |
| 37 | ), |
| 38 | Effect.orElseSucceed(() => |
| 39 | MappedSources.location({ |
| 40 | name, |
| 41 | ...location, |
| 42 | filePath: filePath.replace(workingDirectory, ''), |
| 43 | }) |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | return yield* getSourcesFromMapFile(name, location); |
| 49 | }); |