| 1 | /*! Adapted dsh-persona/dsh-system-prompt; upstream licence: |
| 2 | MIT License |
| 3 | |
| 4 | Copyright (c) 2026 DeepSeek |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | /** Bounded additive projection of dsh-persona 0.1.6-alpha.1 (MIT), pinned |
| 25 | * 0d1f50007f9bca3f52b06e1c3074fa14d5fb0720. Core retains its prompt and |
| 26 | * runtime context. Prefix/suffix become exact entry-scoped contributions. */ |
| 27 | import { normalizePromptSection, validatePromptTemplate, MAX_PROMPT_SECTION_BYTES } from '../shims/prompt.ts' |
| 28 | import { ownerStorage } from '../root.ts' |
| 29 | |
| 30 | export const PERSONA = '@deepseek-ai/dsh-persona' |
| 31 | export interface PersonaConfig { prefix: string; suffix: string } |
| 32 | |
| 33 | /** Used by both nonexecuting catalog readiness and actual Loader activation. */ |
| 34 | export function normalizePersonaConfig(value: unknown): PersonaConfig { |
| 35 | if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('persona config must be plain data') |
| 36 | const config = value as Record<string, unknown> |
| 37 | if (Object.keys(config).some(key => !['prefix', 'suffix', 'complete', 'includeRuntimeContext'].includes(key))) throw new TypeError('persona config has an unsupported field') |
| 38 | if (config.complete !== undefined && config.complete !== false) throw new TypeError('persona replacement belongs to the Core prompt and cannot be imported') |
| 39 | if (config.includeRuntimeContext !== undefined && config.includeRuntimeContext !== true) throw new TypeError('persona runtime-context suppression belongs to the Core prompt and cannot be imported') |
| 40 | if (typeof config.prefix !== 'string' || (config.suffix !== undefined && typeof config.suffix !== 'string')) throw new TypeError('persona prefix and suffix must be text') |
| 41 | const result = {prefix: config.prefix, suffix: config.suffix ?? ''} as PersonaConfig |
| 42 | for (const [position, text] of Object.entries(result)) { |
| 43 | if (Buffer.byteLength(text, 'utf8') > MAX_PROMPT_SECTION_BYTES) throw new RangeError('persona text exceeds 4096 UTF-8 bytes') |
| 44 | if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/u.test(text)) throw new TypeError('persona text contains control characters') |
| 45 | validatePromptTemplate(text) |
| 46 | // Empty persona sections are absent, matching the borrowed upstream API. |
| 47 | if (text.trim().length > 0) normalizePromptSection({id: `persona-${position}`, text, interpolate: 'model-cwd'}) |
| 48 | } |
| 49 | return Object.freeze(result) |
| 50 | } |
| 51 | |
| 52 | export const reviewedPersonaModule = Object.freeze({ |
| 53 | name: 'persona', |
| 54 | inject: ['prompt'], |
| 55 | apply(ctx: any, value: unknown): void { |
| 56 | if (!ownerStorage.getStore()?.scope) throw new Error('persona requires an exact Core-selected entry scope') |
| 57 | const config = normalizePersonaConfig(value) |
| 58 | for (const [position, text] of Object.entries(config)) { |
| 59 | if (text.trim().length > 0) ctx.prompt.registerSection({id: `persona-${position}`, text, interpolate: 'model-cwd'}) |
| 60 | } |
| 61 | }, |
| 62 | }) |
| 63 |