@gringow/gringow 0.0.9 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,12 +54,12 @@ var configSchema = z.object({
54
54
  model: z.string().nullable(),
55
55
  temperature: z.number().optional(),
56
56
  sleep: z.number().optional(),
57
- prePrompt: z.string().optional(),
58
- postPrompt: z.string().optional(),
57
+ prePrompt: z.string().max(128).optional(),
58
+ postPrompt: z.string().max(128).optional(),
59
59
  prePromptReplace: z.string().optional(),
60
60
  postPromptReplace: z.string().optional(),
61
61
  prePromptAppend: z.string().optional(),
62
- postPromptAppend: z.string().optional()
62
+ postPromptPrepend: z.string().optional()
63
63
  }),
64
64
  globby: z.string().regex(/^\.\/.*/).optional()
65
65
  }).refine(
@@ -85,8 +85,16 @@ async function loadUserConfig() {
85
85
  try {
86
86
  const mergedConfig = merge({}, defaultConfig, result?.config);
87
87
  const parsedConfig = await configSchema.parseAsync(mergedConfig);
88
- parsedConfig.llm.prePrompt = prePrompt(parsedConfig);
89
- parsedConfig.llm.postPrompt = postPrompt(parsedConfig);
88
+ parsedConfig.llm.prePrompt = prePrompt(
89
+ parsedConfig,
90
+ parsedConfig.llm.prePromptAppend ?? "",
91
+ parsedConfig.llm.prePromptReplace ?? ""
92
+ );
93
+ parsedConfig.llm.postPrompt = postPrompt(
94
+ parsedConfig,
95
+ parsedConfig.llm.postPromptPrepend ?? "",
96
+ parsedConfig.llm.postPromptReplace ?? ""
97
+ );
90
98
  return parsedConfig;
91
99
  } catch (error) {
92
100
  console.warn(`${error?.message}, using default configuration`);
@@ -135,16 +143,16 @@ function defaultPostPrompt(_config) {
135
143
  [/POST-MESSAGE]
136
144
  `;
137
145
  }
138
- function prePrompt(config) {
146
+ function prePrompt(config, append, replace) {
139
147
  return [
140
- config.llm.prePromptReplace ? `${config.llm.prePromptReplace}` : defaultPrePrompt(config),
141
- config.llm.prePromptAppend ? ` ${config.llm.prePromptAppend}` : ""
148
+ ...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
149
+ replace ? String(replace) : defaultPrePrompt(config)
142
150
  ].join(" ").trim();
143
151
  }
144
- function postPrompt(config) {
152
+ function postPrompt(config, prepend, replace) {
145
153
  return [
146
- config?.llm?.postPromptReplace ? `${config.llm.postPromptReplace}` : defaultPostPrompt(),
147
- config.llm.postPromptAppend ? ` ${config.llm.postPromptAppend}` : ""
154
+ ...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
155
+ replace ? String(replace) : defaultPostPrompt()
148
156
  ].join(" ").trim();
149
157
  }
150
158
  var optsArg = (opts) => {
@@ -1,4 +1,4 @@
1
- export { default as Gringow } from './index.mjs';
1
+ export { default as Gringow, rebuild } from './index.mjs';
2
2
  import './config.mjs';
3
3
  import './constants/default-config.mjs';
4
4
  import 'zod';
package/dist/gringow.mjs CHANGED
@@ -58,12 +58,12 @@ var configSchema = z.object({
58
58
  model: z.string().nullable(),
59
59
  temperature: z.number().optional(),
60
60
  sleep: z.number().optional(),
61
- prePrompt: z.string().optional(),
62
- postPrompt: z.string().optional(),
61
+ prePrompt: z.string().max(128).optional(),
62
+ postPrompt: z.string().max(128).optional(),
63
63
  prePromptReplace: z.string().optional(),
64
64
  postPromptReplace: z.string().optional(),
65
65
  prePromptAppend: z.string().optional(),
66
- postPromptAppend: z.string().optional()
66
+ postPromptPrepend: z.string().optional()
67
67
  }),
68
68
  globby: z.string().regex(/^\.\/.*/).optional()
69
69
  }).refine(
@@ -89,8 +89,16 @@ async function loadUserConfig() {
89
89
  try {
90
90
  const mergedConfig = merge({}, defaultConfig, result?.config);
91
91
  const parsedConfig = await configSchema.parseAsync(mergedConfig);
92
- parsedConfig.llm.prePrompt = prePrompt(parsedConfig);
93
- parsedConfig.llm.postPrompt = postPrompt(parsedConfig);
92
+ parsedConfig.llm.prePrompt = prePrompt(
93
+ parsedConfig,
94
+ parsedConfig.llm.prePromptAppend ?? "",
95
+ parsedConfig.llm.prePromptReplace ?? ""
96
+ );
97
+ parsedConfig.llm.postPrompt = postPrompt(
98
+ parsedConfig,
99
+ parsedConfig.llm.postPromptPrepend ?? "",
100
+ parsedConfig.llm.postPromptReplace ?? ""
101
+ );
94
102
  return parsedConfig;
95
103
  } catch (error) {
96
104
  console.warn(`${error?.message}, using default configuration`);
@@ -139,16 +147,16 @@ function defaultPostPrompt(_config) {
139
147
  [/POST-MESSAGE]
140
148
  `;
141
149
  }
142
- function prePrompt(config) {
150
+ function prePrompt(config, append, replace) {
143
151
  return [
144
- config.llm.prePromptReplace ? `${config.llm.prePromptReplace}` : defaultPrePrompt(config),
145
- config.llm.prePromptAppend ? ` ${config.llm.prePromptAppend}` : ""
152
+ ...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
153
+ replace ? String(replace) : defaultPrePrompt(config)
146
154
  ].join(" ").trim();
147
155
  }
148
- function postPrompt(config) {
156
+ function postPrompt(config, prepend, replace) {
149
157
  return [
150
- config?.llm?.postPromptReplace ? `${config.llm.postPromptReplace}` : defaultPostPrompt(),
151
- config.llm.postPromptAppend ? ` ${config.llm.postPromptAppend}` : ""
158
+ ...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
159
+ replace ? String(replace) : defaultPostPrompt()
152
160
  ].join(" ").trim();
153
161
  }
154
162
  var optsArg = (opts) => {
@@ -480,7 +488,7 @@ function flatTemplateString(givenStrings, givenValues) {
480
488
  }, "");
481
489
  return flatted;
482
490
  }
483
- async function askAnthropic(message) {
491
+ async function askAnthropic(prompt, prePrompt2, postPrompt2) {
484
492
  const config = await awaitedConfig;
485
493
  if (Number(config.llm.sleep)) {
486
494
  await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
@@ -492,19 +500,20 @@ async function askAnthropic(message) {
492
500
  apiKey: config.keys.anthropicApiKey,
493
501
  dangerouslyAllowBrowser: true
494
502
  });
503
+ const fullPrompt = String(`${prePrompt2}
504
+
505
+
506
+ ${prompt}
507
+
508
+
509
+ ${postPrompt2}`).trim();
495
510
  const response = await anthropic.messages.create({
496
511
  model: config.llm.model,
497
512
  max_tokens: 1024,
498
513
  messages: [
499
514
  {
500
515
  role: "user",
501
- content: `${config.llm.prePrompt}
502
-
503
-
504
- ${message}
505
-
506
-
507
- ${config.llm.postPrompt}`
516
+ content: fullPrompt
508
517
  }
509
518
  ]
510
519
  });
@@ -528,7 +537,7 @@ function parseAnthropic(unparsed) {
528
537
  const parsed = JSON.parse(unparsed);
529
538
  return parsed;
530
539
  }
531
- async function generateResponse(message, temperature = 1) {
540
+ async function generateResponse(prompt, temperature = 1) {
532
541
  const config = await awaitedConfig;
533
542
  if (!config.keys.geminiApiKey) {
534
543
  throw new Error("Gemini API key is not set in the config");
@@ -540,20 +549,13 @@ async function generateResponse(message, temperature = 1) {
540
549
  temperature
541
550
  }
542
551
  });
543
- const prompt = String(`${config.llm.prePrompt}
544
-
545
-
546
- ${message}
547
-
548
-
549
- ${config.llm.postPrompt}`).trim();
550
552
  if (config.debug) {
551
553
  console.log("Gemini model prompt:", JSON.stringify({ prompt }));
552
554
  }
553
555
  const response = await model.generateContent([prompt]);
554
556
  return response;
555
557
  }
556
- async function askGemini(message) {
558
+ async function askGemini(prompt, prePrompt2, postPrompt2) {
557
559
  const config = await awaitedConfig;
558
560
  if (Number(config.llm.sleep)) {
559
561
  await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
@@ -562,11 +564,18 @@ async function askGemini(message) {
562
564
  throw new Error("Gemini API key is not set in the config");
563
565
  }
564
566
  if (config.debug) {
565
- console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
567
+ console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
566
568
  }
569
+ const fullPrompt = String(`${prePrompt2}
570
+
571
+
572
+ ${prompt}
573
+
574
+
575
+ ${postPrompt2}`).trim();
567
576
  let response;
568
577
  for (let i = 0; i < 3; i++) {
569
- const maybeResponse = await generateResponse(message, config.llm.temperature);
578
+ const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
570
579
  if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
571
580
  response = maybeResponse;
572
581
  break;
@@ -886,23 +895,49 @@ var llmChoices = {
886
895
  parse: parseHuggingface
887
896
  }
888
897
  };
889
- async function processLLMChoice(llmChoice, prompt) {
890
- const response = await llmChoice.ask(prompt);
898
+ async function processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2) {
899
+ const response = await llmChoice.ask(prompt, prePrompt2, postPrompt2);
891
900
  const unparsed = llmChoice.commitResponse(response);
892
901
  const parsed = llmChoice.parse(unparsed);
893
902
  return parsed;
894
903
  }
895
- async function askLLM(prompt) {
904
+ async function askLLM(prompt, prePrompt2, postPrompt2) {
896
905
  const config = await awaitedConfig;
897
906
  const llmChoiceName = config.llm.choice;
898
907
  const llmChoice = llmChoices[llmChoiceName];
899
908
  if (!llmChoice) {
900
909
  throw new Error("LLMChoice not found");
901
910
  }
902
- return processLLMChoice(llmChoice, prompt);
911
+ return processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2);
903
912
  }
904
913
 
905
914
  // src/gringow.ts
915
+ function getPrependAndAppend(config, cached) {
916
+ const prepend = prePrompt(
917
+ config,
918
+ cached?.prepend ?? config.llm.prePromptAppend ?? "",
919
+ config.llm.prePromptReplace ?? ""
920
+ );
921
+ const append = postPrompt(config, cached?.append ?? config.llm.postPrompt ?? "", config.llm.postPromptReplace ?? "");
922
+ return { prepend, append };
923
+ }
924
+ async function rebuild(cacheId) {
925
+ const config = await awaitedConfig;
926
+ const cached = await checkCache(cacheId);
927
+ if (!cached) throw new Error("Cache item not found");
928
+ const { prepend, append } = getPrependAndAppend(config, cached);
929
+ const translation = await askLLM(cached.original, prepend, append);
930
+ const output = {
931
+ ...translation,
932
+ prepend: cached?.prepend ?? void 0,
933
+ append: cached?.append ?? void 0
934
+ };
935
+ console.log("output:", output);
936
+ if (config.cache.write) {
937
+ await writeCacheToDisk(cacheId, output);
938
+ }
939
+ return { cacheId, translation: output };
940
+ }
906
941
  async function Gringow(strings, ...values) {
907
942
  const config = await awaitedConfig;
908
943
  let flatten;
@@ -913,14 +948,20 @@ async function Gringow(strings, ...values) {
913
948
  }
914
949
  const cacheId = createCacheId(flatten);
915
950
  const cached = await checkCache(cacheId);
916
- if (cached !== void 0 && cached.original !== "undefined") {
951
+ if (cached !== void 0 && cached.original !== "undefined" && !cached.rebuild) {
917
952
  return { cacheId, translation: cached };
918
953
  }
919
- const translation = await askLLM(flatten);
954
+ const { prepend, append } = getPrependAndAppend(config, cached);
955
+ const translation = await askLLM(flatten, prepend, append);
956
+ const output = {
957
+ ...translation,
958
+ prepend: cached?.prepend ?? void 0,
959
+ append: cached?.append ?? void 0
960
+ };
920
961
  if (config.cache.write) {
921
- await writeCacheToDisk(cacheId, translation);
962
+ await writeCacheToDisk(cacheId, output);
922
963
  }
923
- return { cacheId, translation };
964
+ return { cacheId, translation: output };
924
965
  }
925
966
 
926
- export { Gringow };
967
+ export { Gringow, rebuild };
package/dist/index.d.mts CHANGED
@@ -56,6 +56,9 @@ type GringowTranslation = {
56
56
  type GringowCacheItem = {
57
57
  original: string;
58
58
  translation: GringowTranslation;
59
+ append?: string;
60
+ prepend?: string;
61
+ rebuild?: boolean;
59
62
  };
60
63
  type GringowCache = {
61
64
  [key: string]: GringowCacheItem;
@@ -69,6 +72,7 @@ type GringowError = Error & {
69
72
  cause?: Error;
70
73
  };
71
74
 
75
+ declare function rebuild(cacheId: string): Promise<GringowResponse>;
72
76
  /**
73
77
  * Gringow is a function that translates a string or a template string into a
74
78
  * translated string.
@@ -79,4 +83,4 @@ type GringowError = Error & {
79
83
  */
80
84
  declare function Gringow(strings: TemplateStringsArray | string, ...values: Array<unknown>): Promise<GringowResponse>;
81
85
 
82
- export { type GringowCache, type GringowCacheItem, type GringowError, type GringowResponse, type GringowTranslation, checkCache, Gringow as default, deleteCacheItem, getOrCreateCache, writeCacheToDisk };
86
+ export { type GringowCache, type GringowCacheItem, type GringowError, type GringowResponse, type GringowTranslation, checkCache, Gringow as default, deleteCacheItem, getOrCreateCache, rebuild, writeCacheToDisk };
package/dist/index.mjs CHANGED
@@ -58,12 +58,12 @@ var configSchema = z.object({
58
58
  model: z.string().nullable(),
59
59
  temperature: z.number().optional(),
60
60
  sleep: z.number().optional(),
61
- prePrompt: z.string().optional(),
62
- postPrompt: z.string().optional(),
61
+ prePrompt: z.string().max(128).optional(),
62
+ postPrompt: z.string().max(128).optional(),
63
63
  prePromptReplace: z.string().optional(),
64
64
  postPromptReplace: z.string().optional(),
65
65
  prePromptAppend: z.string().optional(),
66
- postPromptAppend: z.string().optional()
66
+ postPromptPrepend: z.string().optional()
67
67
  }),
68
68
  globby: z.string().regex(/^\.\/.*/).optional()
69
69
  }).refine(
@@ -95,8 +95,16 @@ async function loadUserConfig() {
95
95
  try {
96
96
  const mergedConfig = merge({}, defaultConfig, result?.config);
97
97
  const parsedConfig = await configSchema.parseAsync(mergedConfig);
98
- parsedConfig.llm.prePrompt = prePrompt(parsedConfig);
99
- parsedConfig.llm.postPrompt = postPrompt(parsedConfig);
98
+ parsedConfig.llm.prePrompt = prePrompt(
99
+ parsedConfig,
100
+ parsedConfig.llm.prePromptAppend ?? "",
101
+ parsedConfig.llm.prePromptReplace ?? ""
102
+ );
103
+ parsedConfig.llm.postPrompt = postPrompt(
104
+ parsedConfig,
105
+ parsedConfig.llm.postPromptPrepend ?? "",
106
+ parsedConfig.llm.postPromptReplace ?? ""
107
+ );
100
108
  return parsedConfig;
101
109
  } catch (error) {
102
110
  console.warn(`${error?.message}, using default configuration`);
@@ -145,16 +153,16 @@ function defaultPostPrompt(_config) {
145
153
  [/POST-MESSAGE]
146
154
  `;
147
155
  }
148
- function prePrompt(config) {
156
+ function prePrompt(config, append, replace) {
149
157
  return [
150
- config.llm.prePromptReplace ? `${config.llm.prePromptReplace}` : defaultPrePrompt(config),
151
- config.llm.prePromptAppend ? ` ${config.llm.prePromptAppend}` : ""
158
+ ...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
159
+ replace ? String(replace) : defaultPrePrompt(config)
152
160
  ].join(" ").trim();
153
161
  }
154
- function postPrompt(config) {
162
+ function postPrompt(config, prepend, replace) {
155
163
  return [
156
- config?.llm?.postPromptReplace ? `${config.llm.postPromptReplace}` : defaultPostPrompt(),
157
- config.llm.postPromptAppend ? ` ${config.llm.postPromptAppend}` : ""
164
+ ...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
165
+ replace ? String(replace) : defaultPostPrompt()
158
166
  ].join(" ").trim();
159
167
  }
160
168
  var optsArg = (opts) => {
@@ -502,7 +510,7 @@ function flatTemplateString(givenStrings, givenValues) {
502
510
  }, "");
503
511
  return flatted;
504
512
  }
505
- async function askAnthropic(message) {
513
+ async function askAnthropic(prompt, prePrompt2, postPrompt2) {
506
514
  const config = await awaitedConfig;
507
515
  if (Number(config.llm.sleep)) {
508
516
  await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
@@ -514,19 +522,20 @@ async function askAnthropic(message) {
514
522
  apiKey: config.keys.anthropicApiKey,
515
523
  dangerouslyAllowBrowser: true
516
524
  });
525
+ const fullPrompt = String(`${prePrompt2}
526
+
527
+
528
+ ${prompt}
529
+
530
+
531
+ ${postPrompt2}`).trim();
517
532
  const response = await anthropic.messages.create({
518
533
  model: config.llm.model,
519
534
  max_tokens: 1024,
520
535
  messages: [
521
536
  {
522
537
  role: "user",
523
- content: `${config.llm.prePrompt}
524
-
525
-
526
- ${message}
527
-
528
-
529
- ${config.llm.postPrompt}`
538
+ content: fullPrompt
530
539
  }
531
540
  ]
532
541
  });
@@ -550,7 +559,7 @@ function parseAnthropic(unparsed) {
550
559
  const parsed = JSON.parse(unparsed);
551
560
  return parsed;
552
561
  }
553
- async function generateResponse(message, temperature = 1) {
562
+ async function generateResponse(prompt, temperature = 1) {
554
563
  const config = await awaitedConfig;
555
564
  if (!config.keys.geminiApiKey) {
556
565
  throw new Error("Gemini API key is not set in the config");
@@ -562,20 +571,13 @@ async function generateResponse(message, temperature = 1) {
562
571
  temperature
563
572
  }
564
573
  });
565
- const prompt = String(`${config.llm.prePrompt}
566
-
567
-
568
- ${message}
569
-
570
-
571
- ${config.llm.postPrompt}`).trim();
572
574
  if (config.debug) {
573
575
  console.log("Gemini model prompt:", JSON.stringify({ prompt }));
574
576
  }
575
577
  const response = await model.generateContent([prompt]);
576
578
  return response;
577
579
  }
578
- async function askGemini(message) {
580
+ async function askGemini(prompt, prePrompt2, postPrompt2) {
579
581
  const config = await awaitedConfig;
580
582
  if (Number(config.llm.sleep)) {
581
583
  await new Promise((resolve2) => setTimeout(resolve2, config.llm.sleep ?? 1));
@@ -584,11 +586,18 @@ async function askGemini(message) {
584
586
  throw new Error("Gemini API key is not set in the config");
585
587
  }
586
588
  if (config.debug) {
587
- console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message }));
589
+ console.log("\nAsking Gemini model:", JSON.stringify({ model: config.llm.model, message: prompt }));
588
590
  }
591
+ const fullPrompt = String(`${prePrompt2}
592
+
593
+
594
+ ${prompt}
595
+
596
+
597
+ ${postPrompt2}`).trim();
589
598
  let response;
590
599
  for (let i = 0; i < 3; i++) {
591
- const maybeResponse = await generateResponse(message, config.llm.temperature);
600
+ const maybeResponse = await generateResponse(fullPrompt, config.llm.temperature);
592
601
  if (!String(maybeResponse.response.candidates?.[0]?.content?.parts?.[0]?.text ?? "").includes("```json")) {
593
602
  response = maybeResponse;
594
603
  break;
@@ -908,23 +917,49 @@ var llmChoices = {
908
917
  parse: parseHuggingface
909
918
  }
910
919
  };
911
- async function processLLMChoice(llmChoice, prompt) {
912
- const response = await llmChoice.ask(prompt);
920
+ async function processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2) {
921
+ const response = await llmChoice.ask(prompt, prePrompt2, postPrompt2);
913
922
  const unparsed = llmChoice.commitResponse(response);
914
923
  const parsed = llmChoice.parse(unparsed);
915
924
  return parsed;
916
925
  }
917
- async function askLLM(prompt) {
926
+ async function askLLM(prompt, prePrompt2, postPrompt2) {
918
927
  const config = await awaitedConfig;
919
928
  const llmChoiceName = config.llm.choice;
920
929
  const llmChoice = llmChoices[llmChoiceName];
921
930
  if (!llmChoice) {
922
931
  throw new Error("LLMChoice not found");
923
932
  }
924
- return processLLMChoice(llmChoice, prompt);
933
+ return processLLMChoice(llmChoice, prompt, prePrompt2, postPrompt2);
925
934
  }
926
935
 
927
936
  // src/gringow.ts
937
+ function getPrependAndAppend(config, cached) {
938
+ const prepend = prePrompt(
939
+ config,
940
+ cached?.prepend ?? config.llm.prePromptAppend ?? "",
941
+ config.llm.prePromptReplace ?? ""
942
+ );
943
+ const append = postPrompt(config, cached?.append ?? config.llm.postPrompt ?? "", config.llm.postPromptReplace ?? "");
944
+ return { prepend, append };
945
+ }
946
+ async function rebuild(cacheId) {
947
+ const config = await awaitedConfig;
948
+ const cached = await checkCache(cacheId);
949
+ if (!cached) throw new Error("Cache item not found");
950
+ const { prepend, append } = getPrependAndAppend(config, cached);
951
+ const translation = await askLLM(cached.original, prepend, append);
952
+ const output = {
953
+ ...translation,
954
+ prepend: cached?.prepend ?? void 0,
955
+ append: cached?.append ?? void 0
956
+ };
957
+ console.log("output:", output);
958
+ if (config.cache.write) {
959
+ await writeCacheToDisk(cacheId, output);
960
+ }
961
+ return { cacheId, translation: output };
962
+ }
928
963
  async function Gringow(strings, ...values) {
929
964
  const config = await awaitedConfig;
930
965
  let flatten;
@@ -935,14 +970,20 @@ async function Gringow(strings, ...values) {
935
970
  }
936
971
  const cacheId = createCacheId(flatten);
937
972
  const cached = await checkCache(cacheId);
938
- if (cached !== void 0 && cached.original !== "undefined") {
973
+ if (cached !== void 0 && cached.original !== "undefined" && !cached.rebuild) {
939
974
  return { cacheId, translation: cached };
940
975
  }
941
- const translation = await askLLM(flatten);
976
+ const { prepend, append } = getPrependAndAppend(config, cached);
977
+ const translation = await askLLM(flatten, prepend, append);
978
+ const output = {
979
+ ...translation,
980
+ prepend: cached?.prepend ?? void 0,
981
+ append: cached?.append ?? void 0
982
+ };
942
983
  if (config.cache.write) {
943
- await writeCacheToDisk(cacheId, translation);
984
+ await writeCacheToDisk(cacheId, output);
944
985
  }
945
- return { cacheId, translation };
986
+ return { cacheId, translation: output };
946
987
  }
947
988
 
948
- export { awaitedConfig, checkCache, clearCache, createCacheId, Gringow as default, deleteCacheItem, flatTemplateString, getCache, getConfigSource, getOrCreateCache, isLocalConfigFound, writeCacheToDisk };
989
+ export { awaitedConfig, checkCache, clearCache, createCacheId, Gringow as default, deleteCacheItem, flatTemplateString, getCache, getConfigSource, getOrCreateCache, isLocalConfigFound, rebuild, writeCacheToDisk };
@@ -3,9 +3,9 @@ import Anthropic from '@anthropic-ai/sdk';
3
3
  /**
4
4
  * Asks the Anthropic model for a response.
5
5
  *
6
- * @param message - The message to ask the model.
6
+ * @param prompt - The message to ask the model.
7
7
  * @returns The response from the model.
8
8
  */
9
- declare function askAnthropic(message: string): Promise<Anthropic.Messages.Message>;
9
+ declare function askAnthropic(prompt: string, prePrompt: string, postPrompt: string): Promise<Anthropic.Messages.Message>;
10
10
 
11
11
  export { askAnthropic };
@@ -52,12 +52,12 @@ var configSchema = z.object({
52
52
  model: z.string().nullable(),
53
53
  temperature: z.number().optional(),
54
54
  sleep: z.number().optional(),
55
- prePrompt: z.string().optional(),
56
- postPrompt: z.string().optional(),
55
+ prePrompt: z.string().max(128).optional(),
56
+ postPrompt: z.string().max(128).optional(),
57
57
  prePromptReplace: z.string().optional(),
58
58
  postPromptReplace: z.string().optional(),
59
59
  prePromptAppend: z.string().optional(),
60
- postPromptAppend: z.string().optional()
60
+ postPromptPrepend: z.string().optional()
61
61
  }),
62
62
  globby: z.string().regex(/^\.\/.*/).optional()
63
63
  }).refine(
@@ -83,8 +83,16 @@ async function loadUserConfig() {
83
83
  try {
84
84
  const mergedConfig = merge({}, defaultConfig, result?.config);
85
85
  const parsedConfig = await configSchema.parseAsync(mergedConfig);
86
- parsedConfig.llm.prePrompt = prePrompt(parsedConfig);
87
- parsedConfig.llm.postPrompt = postPrompt(parsedConfig);
86
+ parsedConfig.llm.prePrompt = prePrompt(
87
+ parsedConfig,
88
+ parsedConfig.llm.prePromptAppend ?? "",
89
+ parsedConfig.llm.prePromptReplace ?? ""
90
+ );
91
+ parsedConfig.llm.postPrompt = postPrompt(
92
+ parsedConfig,
93
+ parsedConfig.llm.postPromptPrepend ?? "",
94
+ parsedConfig.llm.postPromptReplace ?? ""
95
+ );
88
96
  return parsedConfig;
89
97
  } catch (error) {
90
98
  console.warn(`${error?.message}, using default configuration`);
@@ -133,21 +141,21 @@ function defaultPostPrompt(_config) {
133
141
  [/POST-MESSAGE]
134
142
  `;
135
143
  }
136
- function prePrompt(config) {
144
+ function prePrompt(config, append, replace) {
137
145
  return [
138
- config.llm.prePromptReplace ? `${config.llm.prePromptReplace}` : defaultPrePrompt(config),
139
- config.llm.prePromptAppend ? ` ${config.llm.prePromptAppend}` : ""
146
+ ...append ? ["[SPECIFIC_INPUTS]", String(append), "[/SPECIFIC_INPUTS]"] : [],
147
+ replace ? String(replace) : defaultPrePrompt(config)
140
148
  ].join(" ").trim();
141
149
  }
142
- function postPrompt(config) {
150
+ function postPrompt(config, prepend, replace) {
143
151
  return [
144
- config?.llm?.postPromptReplace ? `${config.llm.postPromptReplace}` : defaultPostPrompt(),
145
- config.llm.postPromptAppend ? ` ${config.llm.postPromptAppend}` : ""
152
+ ...prepend ? ["[SPECIFIC_INPUTS]", String(prepend), "[/SPECIFIC_INPUTS]"] : [],
153
+ replace ? String(replace) : defaultPostPrompt()
146
154
  ].join(" ").trim();
147
155
  }
148
156
 
149
157
  // src/io/anthropic/ask.ts
150
- async function askAnthropic(message) {
158
+ async function askAnthropic(prompt, prePrompt2, postPrompt2) {
151
159
  const config = await awaitedConfig;
152
160
  if (Number(config.llm.sleep)) {
153
161
  await new Promise((resolve) => setTimeout(resolve, config.llm.sleep ?? 1));
@@ -159,19 +167,20 @@ async function askAnthropic(message) {
159
167
  apiKey: config.keys.anthropicApiKey,
160
168
  dangerouslyAllowBrowser: true
161
169
  });
170
+ const fullPrompt = String(`${prePrompt2}
171
+
172
+
173
+ ${prompt}
174
+
175
+
176
+ ${postPrompt2}`).trim();
162
177
  const response = await anthropic.messages.create({
163
178
  model: config.llm.model,
164
179
  max_tokens: 1024,
165
180
  messages: [
166
181
  {
167
182
  role: "user",
168
- content: `${config.llm.prePrompt}
169
-
170
-
171
- ${message}
172
-
173
-
174
- ${config.llm.postPrompt}`
183
+ content: fullPrompt
175
184
  }
176
185
  ]
177
186
  });