All files / app/mods/ai summary.ts

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

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 125117x         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 summaryQueryPlugin: Plugin = {
  tag: 'plugin/delta/ai/summary',
  name: $localize`✂️️💭️ Summarize`,
  config: {
    mod: $localize`✂️ Summarize`,
    version: 1,
    type: 'tool',
    default: false,
    add: true,
    signature: '+plugin/delta/ai/summary',
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    description: $localize`Send this Ref to the ai to create a summary response.`,
    filters: [
      { query: 'plugin/delta/ai/summary', label: $localize`✂️️💭️ summarized`, title: $localize`Has AI generated summary`, group: $localize`Notifications ✉️` },
    ],
    advancedActions: [
      { tag: 'plugin/delta/ai/summary', labelOff: $localize`summarize`, global: true }
    ],
    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 ? 'Summary of: ' + ref.title : 'Summary',
        comment: '+plugin/delta/ai/summary is working...',
        tags: ['+plugin/placeholder', 'plugin/llm'],
        plugins: {
          'plugin/llm': {
            ignoreThread: true,
          }
        }
      };
      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);
      }
      response.sources.push('system:summary-prompt');
      console.log(JSON.stringify(bundle));
    `
  }
};
 
export const summaryPlugin: Plugin = {
  tag: '+plugin/delta/ai/summary',
  name: $localize`✂️️ Summary`,
  config: {
    mod: $localize`✂️ Summarize`,
    version: 1,
    type: 'tool',
    default: false,
    signature: '+plugin/delta/ai/summary',
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    description: $localize`AI signature tag. Plugin configures AI to respond to 'plugin/delta/ai/summary' prompts
    and sign this response with this tag.`,
    icons: [{ thumbnail: $localize`✂️️`, order: 1 }],
    filters: [
      { query: '+plugin/delta/ai/summary', label: $localize`✂️️ summary`, title: $localize`Summaries generated by AI`, group: $localize`Delta Δ` },
    ],
    advancedActions: [
      { tag: '+plugin/delta/ai/summary', labelOn: $localize`redo`, title: $localize`Redo summary` },
      { tag: 'plugin/alias/plugin/delta/ai/summary', labelOff: $localize`redo`, title: $localize`Redo summary` },
    ]
  }
};
 
export const summaryPrompt: Ref = {
  url: 'system:summary-prompt',
  title: $localize`Summarize Prompt`,
  tags: ['public', 'internal', '+system/prompt'],
  comment: $localize`Summarize the following without loosing any important details and do not include any salutations:`,
};
 
export const summaryMod: Mod = {
  ref: [
    summaryPrompt,
  ],
  plugin: [
    summaryQueryPlugin,
    summaryPlugin,
  ],
};