{"version":3,"sources":["../../src/openai/index.ts","../../src/openai/predict-input-content.ts"],"sourcesContent":["export { predictInputContent } from './predict-input-content'\n","'use server'\n\nimport 'openai/shims/node'\nimport OpenAI from 'openai'\nimport 'server-only'\n\n\nconst INSTRUCTIONS = `System prompt:\n- You are an autocomplete system that predicts the most likely next input\n- The input text is the preceding text in a comment form\n- Predict the most likely next input (at most 8 new words OR the necessary suffix to finish the current word) based on the text provided.\n- If the next input is more likely to be short, do not err on the side of longer predictions. (\"In france they speak\" -> \" French.\")\n- Respond in json format. \n\"type\" should be one of \"prediction\" if you are predicting next input, or \"error\" if you are unable to predict input.\n\"content\" should be at most 8 new words OR the necessary suffix to finish the current word if \"type\" is \"prediction\", or an error message if \"type\" is \"error\".\n\nExamples:\nExample 1 Input:\n\"This is a comment on a \"\nExample 1 JSON response:\n{\n  \"type\": \"prediction\",\n  \"content\": \"Linear issue.\",\n}\n\nExample 2 Input:\n\"New York is a city in the\"\nExample 2 JSON response:\n{\n  \"type\": \"prediction\",\n  \"content\": \" United States.\",\n}\n\nExample 3 Input:\n\"This is a comment on a Lin\"\nExample 3 JSON response:\n{\n  \"type\": \"prediction\",\n  \"content\": \"ear issue.\",\n}\n`\n\ntype PredictInputContentResponse = {\n  type: 'prediction' | 'error'\n  content: string\n}\n\nclass PredictiveTextareaError extends Error {\n  constructor(message: string) {\n    super(message)\n    this.name = 'PredictiveTextareaError'\n  }\n}\n\nclass PredictInputContentError extends PredictiveTextareaError {\n  constructor(message: string) {\n    super(message)\n    this.name = 'PredictInputContentError'\n  }\n}\n\nlet client: OpenAI | null = null\n\nfunction getClient(): OpenAI {\n  const apiKey = process.env.OPENAI_API_KEY\n  if (!apiKey) {\n    throw new PredictiveTextareaError('OPENAI_API_KEY is not set')\n  }\n  if (!client) {\n    client = new OpenAI({\n      apiKey,\n    })\n  }\n  return client\n}\n\nasync function predictInputContent(text: string): Promise<string> {\n  const client = getClient()\n  const response = await client.responses.create({\n    model: process.env.OPENAI_MODEL || 'gpt-4o-mini',\n    instructions: INSTRUCTIONS,\n    input: `Respond in json format. Content: \"${text}\"`,\n    text: {\n      format: {\n        type: 'json_object'\n      }\n    }\n  })\n\n  let output: PredictInputContentResponse\n\n  try {\n    output = JSON.parse(response.output_text || '{}') as PredictInputContentResponse\n  }\n  catch (error) {\n    throw new PredictInputContentError(error as string)\n  }\n\n  if (output.type === 'prediction') {\n    return Promise.resolve(output.content)\n  }\n  else {\n    return Promise.reject(new PredictInputContentError(output.content || 'Unknown error'))\n  }\n}\n\nexport { predictInputContent }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,kBAAO;AACP,oBAAmB;AACnB,yBAAO;AAGP,IAAM,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwCrB,IAAM,0BAAN,cAAsC,MAAM;AAAA,EAC1C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEA,IAAM,2BAAN,cAAuC,wBAAwB;AAAA,EAC7D,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEA,IAAI,SAAwB;AAE5B,SAAS,YAAoB;AAC3B,QAAM,SAAS,QAAQ,IAAI;AAC3B,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,wBAAwB,2BAA2B;AAAA,EAC/D;AACA,MAAI,CAAC,QAAQ;AACX,aAAS,IAAI,cAAAA,QAAO;AAAA,MAClB;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEA,eAAe,oBAAoB,MAA+B;AAChE,QAAMC,UAAS,UAAU;AACzB,QAAM,WAAW,MAAMA,Q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