| 1 | export type GeneratorFn<T> = (args: { |
| 2 | tokens: string[]; |
| 3 | executeCommand: Fig.ExecuteCommandFunction; |
| 4 | generatorContext: Fig.GeneratorContext; |
| 5 | }) => Promise<T> | T; |
| 6 | |
| 7 | const MAX_TOKENS = 4097; |
| 8 | const TOKEN_TO_CHAR_RATIO = 4; |
| 9 | const MARGIN_RATIO = 0.8; |
| 10 | const MAX_CHARS = MAX_TOKENS * TOKEN_TO_CHAR_RATIO * MARGIN_RATIO; |
| 11 | |
| 12 | /** |
| 13 | * A generator that uses the Fig AI API to generate suggestions. |
| 14 | * |
| 15 | * @param prompt The prompt to use for the AI. Can be a string or a generator function. |
| 16 | * @param message The message to send to the AI. Can be a string or a generator function. |
| 17 | * @param postProcess A function to post-process the AI's response. |
| 18 | * @param temperature The temperature to use for the AI. |
| 19 | * @returns A Fig generator. |
| 20 | */ |
| 21 | export function ai({ |
| 22 | name, |
| 23 | prompt, |
| 24 | message, |
| 25 | postProcess, |
| 26 | temperature, |
| 27 | splitOn, |
| 28 | }: { |
| 29 | name: string; |
| 30 | prompt?: string | GeneratorFn<string>; |
| 31 | message: string | GeneratorFn<string | null> | null; |
| 32 | postProcess?: (out: string) => Fig.Suggestion[]; |
| 33 | temperature?: number; |
| 34 | splitOn?: string; |
| 35 | }): Fig.Generator { |
| 36 | return { |
| 37 | scriptTimeout: 15000, |
| 38 | custom: async (tokens, executeCommand, generatorContext) => { |
| 39 | const settingOutput = await executeCommand({ |
| 40 | command: "fig", |
| 41 | args: ["settings", "--format", "json", "autocomplete.ai.enabled"], |
| 42 | }); |
| 43 | |
| 44 | if (!JSON.parse(settingOutput.stdout)) { |
| 45 | return []; |
| 46 | } |
| 47 | |
| 48 | const promptString = |
| 49 | typeof prompt === "function" |
| 50 | ? await prompt({ |
| 51 | tokens, |
| 52 | executeCommand, |
| 53 | generatorContext, |
| 54 | }) |
| 55 | : prompt; |
| 56 | |
| 57 | const messageString = |
| 58 | typeof message === "function" |
| 59 | ? await message({ |
| 60 | tokens, |
| 61 | executeCommand, |
| 62 | generatorContext, |
| 63 | }) |
| 64 | : message; |
| 65 | |
| 66 | if (messageString === null || messageString.length === 0) { |
| 67 | console.warn("No message provided to AI generator"); |
| 68 | return []; |
| 69 | } |
| 70 | |
| 71 | const budget = MAX_CHARS - (promptString?.length ?? 0); |
| 72 | |
| 73 | const body = { |
| 74 | model: "gpt-3.5-turbo", |
| 75 | source: "autocomplete", |
| 76 | name, |
| 77 | messages: [ |
| 78 | ...(promptString |
| 79 | ? [ |
| 80 | { |
| 81 | role: "system", |
| 82 | content: promptString, |
| 83 | }, |
| 84 | ] |
| 85 | : []), |
| 86 | { |
| 87 | role: "user", |
| 88 | content: messageString.slice(0, budget), |
| 89 | }, |
| 90 | ], |
| 91 | temperature, |
| 92 | }; |
| 93 | |
| 94 | const bodyJson = JSON.stringify(body); |
| 95 | |
| 96 | const requestOutput = await executeCommand({ |
| 97 | command: "fig", |
| 98 | args: ["_", "request", "--route", "/ai/chat", "--method", "POST", "--body", bodyJson], |
| 99 | }); |
| 100 | const json = JSON.parse(requestOutput.stdout); |
| 101 | |
| 102 | const a = |
| 103 | json?.choices |
| 104 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 105 | .map((c: any) => c?.message?.content) |
| 106 | .filter((c: unknown) => typeof c === "string") |
| 107 | .flatMap((c: string) => |
| 108 | splitOn ? c.split(splitOn).filter((s) => s.trim().length > 0) : [c] |
| 109 | ) |
| 110 | .map((out: string) => { |
| 111 | if (postProcess) { |
| 112 | return postProcess(out); |
| 113 | } |
| 114 | const text = out.trim().replace(/\n/g, " "); |
| 115 | return { |
| 116 | icon: "🪄", |
| 117 | name: text, |
| 118 | insertValue: `'${text}'`, |
| 119 | description: "Generated by Fig AI", |
| 120 | } as Fig.Suggestion; |
| 121 | }) ?? []; |
| 122 | |
| 123 | return a; |
| 124 | }, |
| 125 | }; |
| 126 | } |