File size: 2,715 Bytes
21ad36a
 
36a170e
21ad36a
36a170e
21ad36a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897170b
21ad36a
 
 
 
 
 
897170b
21ad36a
 
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
import type { EngineChatTool } from '../engine';
import { AGENT_TOOLS, HTML_ARTIFACT_TOOL } from './agent-tools';
import { DEFAULT_MAX_TOKENS } from './runtime-defaults';

export const HTML_ARTIFACT_MIN_TOKENS = DEFAULT_MAX_TOKENS;

const CREATION_VERB = /(?:\b(?:build|code|create|design|develop|generate|implement|make|write)\b|(?:созда(?:й|йте)|сдела(?:й|йте)|напиш(?:и|ите)|сгенерир(?:уй|уйте)|сверста(?:й|йте)|реализ(?:уй|уйте)|разработа(?:й|йте)|собер(?:и|ите)|постро(?:й|йте)))/iu;
const HTML_PRODUCT_TARGET = /(?:\bweb[\s-]?(?:app|application|page|site)\b|\bwebsite\b|\blanding[\s-]?page\b|(?:веб|html)[\s-]?(?:приложени\w*|страниц\w*|сайт\w*|интерфейс\w*)|(?:^|[\s,.:;!?])(?:сайт\w*|лендинг\w*))/iu;
const HTML_LANGUAGE_TARGET = /\bhtml\b/iu;
const DISCUSSION_REQUEST = /(?:\b(?:about|article|describe|discuss|explain|review|standard)\b|(?:объясн\w*|расскаж\w*|что\s+такое|кратко\s+о|о\s+стандарт\w*|обзор\w*|стать\w*))/iu;

export interface ClientToolPlan {
  tools?: EngineChatTool[];
  toolChoice: 'none' | 'auto' | 'required';
  minimumMaxTokens: number;
  requiredToolName?: string;
  systemInstruction?: string;
}

export function requestsHtmlArtifact(prompt: string): boolean {
  if (!CREATION_VERB.test(prompt)) return false;
  if (HTML_PRODUCT_TARGET.test(prompt)) return true;
  return HTML_LANGUAGE_TARGET.test(prompt) && !DISCUSSION_REQUEST.test(prompt);
}

export function planClientTools(prompt: string, enabled: boolean, completedToolRounds: number): ClientToolPlan {
  if (!enabled) {
    return { toolChoice: 'none', minimumMaxTokens: 0 };
  }
  if (!requestsHtmlArtifact(prompt)) {
    return { tools: AGENT_TOOLS, toolChoice: 'auto', minimumMaxTokens: 0 };
  }
  if (completedToolRounds > 0) {
    return { tools: AGENT_TOOLS, toolChoice: 'auto', minimumMaxTokens: 0 };
  }
  return {
    tools: [HTML_ARTIFACT_TOOL],
    toolChoice: 'required',
    minimumMaxTokens: HTML_ARTIFACT_MIN_TOKENS,
    requiredToolName: HTML_ARTIFACT_TOOL.function.name,
    systemInstruction: 'The user explicitly requested an HTML artifact. Call html_artifact exactly once with complete, self-contained HTML, CSS, and any needed inline JavaScript. Keep the source compact enough to finish within the completion budget. Do not answer with a Markdown code block instead. Read the returned evaluation before claiming success. If it reports errors, call artifact_update with the returned artifact ID, then inspect the new evaluation and continue until it passes or you can clearly explain the remaining limitation.',
  };
}