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 * as FileSystem from 'effect/FileSystem'; |
| 2 | import * as Path from 'effect/Path'; |
| 3 | import { Effect, Option } from 'effect'; |
| 4 | import { APP_CONFIG } from 'src/config'; |
| 5 | import { getOrCreateProbablyMyCliSessionIdForCurrentCwd } from 'src/services/consumer-short-term-cache'; |
| 6 | import { ComposioCliUserConfig } from 'src/services/cli-user-config'; |
| 7 | import { NodeOs } from 'src/services/node-os'; |
| 8 | |
| 9 | export const resolveArtifactsRoot = Effect.gen(function* () { |
| 10 | const path = yield* Path.Path; |
| 11 | const os = yield* NodeOs; |
| 12 | const cliUserConfig = yield* ComposioCliUserConfig; |
| 13 | const sessionDirectory = yield* APP_CONFIG.SESSION_DIR; |
| 14 | const cacheDirectory = yield* APP_CONFIG.CACHE_DIR; |
| 15 | |
| 16 | return ( |
| 17 | sessionDirectory || |
| 18 | cacheDirectory || |
| 19 | cliUserConfig.data.artifactDirectory?.trim() || |
| 20 | path.join(os.tmpdir, 'composio') |
| 21 | ); |
| 22 | }); |
| 23 | |
| 24 | const SESSION_HISTORY_FILE = 'session-history.jsonl'; |
| 25 | |
| 26 | export type CliSessionArtifacts = { |
| 27 | readonly sessionId: string; |
| 28 | readonly directoryPath: string; |
| 29 | readonly historyFilePath: string; |
| 30 | }; |
| 31 | |
| 32 | const randomToken = (length = 8) => crypto.randomUUID().replace(/-/g, '').slice(0, length); |
| 33 | |
| 34 | const sanitizeArtifactName = (value: string): string => |
| 35 | value.replace(/[^A-Z0-9_]+/gi, '_').replace(/^_+|_+$/g, '') || 'ARTIFACT'; |
| 36 | |
| 37 | export const resolveCliSessionArtifacts = (params?: { |
| 38 | readonly orgId?: string; |
| 39 | readonly consumerUserId?: string; |
| 40 | }) => |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const path = yield* Path.Path; |
| 44 | const sessionIdOption = yield* getOrCreateProbablyMyCliSessionIdForCurrentCwd(params).pipe( |
| 45 | Effect.option, |
| 46 | Effect.map(Option.flatten) |
| 47 | ); |
| 48 | if (Option.isNone(sessionIdOption)) { |
| 49 | return Option.none<CliSessionArtifacts>(); |
| 50 | } |
| 51 | |
| 52 | const directoryPath = path.join(yield* resolveArtifactsRoot, sessionIdOption.value); |
| 53 | const directoryCreated = yield* fs |
| 54 | .makeDirectory(directoryPath, { recursive: true }) |
| 55 | .pipe(Effect.option, Effect.map(Option.isSome)); |
| 56 | if (!directoryCreated) { |
| 57 | return Option.none<CliSessionArtifacts>(); |
| 58 | } |
| 59 | |
| 60 | return Option.some({ |
| 61 | sessionId: sessionIdOption.value, |
| 62 | directoryPath, |
| 63 | historyFilePath: path.join(directoryPath, SESSION_HISTORY_FILE), |
| 64 | } satisfies CliSessionArtifacts); |
| 65 | }); |
| 66 | |
| 67 | export const appendCliSessionHistory = (params: { |
| 68 | readonly entry: Record<string, unknown>; |
| 69 | readonly orgId?: string; |
| 70 | readonly consumerUserId?: string; |
| 71 | }) => |
| 72 | Effect.gen(function* () { |
| 73 | const fs = yield* FileSystem.FileSystem; |
| 74 | const artifactsOption = yield* resolveCliSessionArtifacts({ |
| 75 | orgId: params.orgId, |
| 76 | consumerUserId: params.consumerUserId, |
| 77 | }); |
| 78 | if (Option.isNone(artifactsOption)) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | const line = JSON.stringify({ |
| 83 | timestamp: new Date().toISOString(), |
| 84 | sessionId: artifactsOption.value.sessionId, |
| 85 | ...params.entry, |
| 86 | }); |
| 87 | yield* fs |
| 88 | .writeFileString(artifactsOption.value.historyFilePath, `${line}\n`, { flag: 'a' }) |
| 89 | .pipe(Effect.ignore); |
| 90 | }); |
| 91 | |
| 92 | export const storeCliSessionArtifact = (params: { |
| 93 | readonly contents: string; |
| 94 | readonly name: string; |
| 95 | readonly extension?: string; |
| 96 | readonly directoryPath?: string; |
| 97 | readonly orgId?: string; |
| 98 | readonly consumerUserId?: string; |
| 99 | }) => |
| 100 | Effect.gen(function* () { |
| 101 | const fs = yield* FileSystem.FileSystem; |
| 102 | const path = yield* Path.Path; |
| 103 | const directoryPath = yield* Effect.gen(function* () { |
| 104 | if (params.directoryPath) { |
| 105 | return params.directoryPath; |
| 106 | } |
| 107 | |
| 108 | const artifactsOption = yield* resolveCliSessionArtifacts({ |
| 109 | orgId: params.orgId, |
| 110 | consumerUserId: params.consumerUserId, |
| 111 | }); |
| 112 | if (Option.isSome(artifactsOption)) { |
| 113 | return artifactsOption.value.directoryPath; |
| 114 | } |
| 115 | |
| 116 | return path.join(yield* resolveArtifactsRoot, `adhoc_${randomToken(12)}`); |
| 117 | }); |
| 118 | |
| 119 | return yield* Effect.gen(function* () { |
| 120 | yield* fs.makeDirectory(directoryPath, { recursive: true }); |
| 121 | const extension = (params.extension ?? 'json').replace(/^\.+/, '') || 'json'; |
| 122 | const filePath = path.join( |
| 123 | directoryPath, |
| 124 | `${sanitizeArtifactName(params.name)}_${randomToken()}.${extension}` |
| 125 | ); |
| 126 | yield* fs.writeFileString(filePath, params.contents); |
| 127 | return filePath; |
| 128 | }).pipe(Effect.option, Effect.map(Option.getOrUndefined)); |
| 129 | }); |