All files / app/mods/ai translate.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 0/0
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214117x         117x                                                                                                                                                                                                                         117x                                                     117x             117x             117x             117x             117x             117x             117x             117x             117x                                
import { DateTime } from 'luxon';
import { Plugin } from '../../model/plugin';
import { Ref } from '../../model/ref';
import { Mod } from '../../model/tag';
 
export const translateQueryPlugin: Plugin = {
  tag: 'plugin/delta/ai/translate',
  name: $localize`🔡️️💭️ Translate`,
  config: {
    mod: $localize`🔡️ Translate`,
    version: 1,
    type: 'tool',
    default: false,
    add: true,
    signature: '+plugin/delta/ai/translate',
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    description: $localize`Send this Ref to the AI to translate.`,
    filters: [
      { query: 'plugin/delta/ai/translate', label: $localize`🔡️️💭️ translated`, title: $localize`Has AI generated translation`, group: $localize`Notifications ✉️` },
    ],
    timeoutMs: 30_000,
    language: 'javascript',
    // language=JavaScript
    script: `
      const bundle = { ref: [] };
      const uuid = require('uuid');
      const axios = require('axios');
      const ref = JSON.parse(require('fs').readFileSync(0, 'utf-8'));
      const origin = ref.origin || '';
      const authors = ref.tags.filter(tag => tag === '+user' || tag === '_user' || tag.startsWith('+user/') || tag.startsWith('_user/'));
      const existingResponse = (await axios.get(process.env.JASPER_API + '/api/v1/ref/page', {
        headers: {
          'Local-Origin': origin || 'default',
          'User-Tag': authors[0] || '',
        },
        params: {
          query: '+plugin/placeholder:!+plugin/delta:' + authors.map(a => a.substring(1)).join(':'),
          responses: ref.url,
          size: 1,
        },
      }).catch(e => {
          console.error(e.response.data);
          throw new Error(e);
        })).data.content[0];
      if (existingResponse) process.exit(0);
      const response = {
        origin,
        url: 'ai:' + uuid.v4(),
        title: ref.title ? 'Translation of: ' + ref.title : 'Translation',
        comment: '+plugin/delta/ai/translate is working...',
        tags: ['+plugin/placeholder', 'plugin/llm'],
        plugins: {
          'plugin/llm': {
            ignoreThread: true,
            provider: 'gemini',
          }
        }
      };
      bundle.ref.push(response);
      response.tags.push(...authors.map(a => a.startsWith('+') || a.startsWith('_') ? a.substring(1) : a));
      if (ref.tags.includes('public')) response.tags.push('public');
      if (ref.tags.includes('internal')) response.tags.push('internal');
      if (ref.tags.includes('dm')) response.tags.push('dm', 'internal', 'plugin/thread');
      if (ref.tags.includes('plugin/comment')) response.tags.push('plugin/comment', 'internal');
      if (ref.tags.includes('plugin/thread')) response.tags.push('plugin/thread', 'internal');
      const chatTags = ref.tags.filter(t => t === 'chat' || t.startsWith('chat/'));
      if (chatTags.length) {
        response.tags.push(chatTags);
      }
      const uniq = (v, i, a) => a.indexOf(v) === i;
      response.tags = response.tags.filter(uniq);
      response.sources = [ref.url];
      if (ref.sources && (ref.tags.includes('plugin/thread') || ref.tags.includes('plugin/comment'))) {
        response.sources.push(ref.sources[1] || ref.sources[0] || ref.url);
      } else {
        response.sources.push(ref.url);
      }
      const locale = ref.plugins?.['plugin/delta/ai/translate']?.language || 'autodetect';
      if (['en', 'fr', 'jp', 'es', 'pt', 'de', 'it'].includes(locale)) {
        response.sources.push('system:translation-prompt-' + locale);
      } else {
        response.sources.push('system:translation-prompt');
      }
      console.log(JSON.stringify(bundle));
    `,
    form: [{
      key: 'language',
      type: 'select',
      defaultValue: 'autodetect',
      props: {
        label: $localize`Translate to:`,
        options: [
          { value: 'autodetect', label: $localize`Detect Automatically` },
          { value: 'en', label: $localize`English` },
          { value: 'fr', label: $localize`French` },
          { value: 'jp', label: $localize`Japanese` },
          { value: 'es', label: $localize`Spanish` },
          { value: 'pt', label: $localize`Portuguese` },
          { value: 'de', label: $localize`German` },
          { value: 'it', label: $localize`Italian` },
        ],
      },
    }],
  },
  defaults: {
    language: 'autodetect'
  },
  schema: {
    optionalProperties: {
      language: { type: 'string' },
    },
  },
};
 
export const translatePlugin: Plugin = {
  tag: '+plugin/delta/ai/translate',
  name: $localize`🔡️️ Translation`,
  config: {
    mod: $localize`🔡️ Translate`,
    version: 1,
    type: 'tool',
    default: false,
    genId: true,
    submit: $localize`🔡️💭️`,
    submitDm: true,
    signature: '+plugin/delta/ai/translate',
    reply: ['plugin/delta/ai/translate'],
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    description: $localize`AI signature tag. Plugin configures AI to respond to 'plugin/delta/ai/translate' prompts
    and sign this response with this tag.`,
    icons: [{ thumbnail: $localize`🔡️️`, order: 1 }],
    filters: [
      { query: '+plugin/delta/ai/translate', label: $localize`🔡️️ summary`, title: $localize`Translations generated by AI`, group: $localize`Delta Δ` },
    ],
    advancedActions: [
      { tag: '+plugin/delta/ai/translate', labelOn: $localize`redo`, title: $localize`Redo translation` },
      { tag: 'plugin/alias/plugin/delta/ai/translate', labelOff: $localize`redo`, title: $localize`Redo translation` },
    ]
  }
};
 
export const translatePrompt: Ref = {
  url: 'system:translation-prompt',
  title: $localize`Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into the language I'm currently using:`,
};
 
export const translatePromptEn: Ref = {
  url: 'system:translation-prompt-en',
  title: $localize`English Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into English:`,
};
 
export const translatePromptFr: Ref = {
  url: 'system:translation-prompt-fr',
  title: $localize`French Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into French:`,
};
 
export const translatePromptJp: Ref = {
  url: 'system:translation-prompt-jp',
  title: $localize`Japanese Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into Japanese:`,
};
 
export const translatePromptEs: Ref = {
  url: 'system:translation-prompt-es',
  title: $localize`Spanish Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into Spanish:`,
};
 
export const translatePromptPt: Ref = {
  url: 'system:translation-prompt-pt',
  title: $localize`Portuguese Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into Portuguese:`,
};
 
export const translatePromptDe: Ref = {
  url: 'system:translation-prompt-de',
  title: $localize`German Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into German:`,
};
 
export const translatePromptIt: Ref = {
  url: 'system:translation-prompt-it',
  title: $localize`Italian Translation Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Translate the following into Italian:`,
};
 
export const translateMod: Mod = {
  ref: [
    translatePrompt,
    translatePromptEn,
    translatePromptFr,
    translatePromptJp,
    translatePromptEs,
    translatePromptPt,
    translatePromptDe,
    translatePromptIt,
  ],
  plugin: [
    translateQueryPlugin,
    translatePlugin,
  ],
};