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
|
import type { SystemPromptVariable } from "@dispatch/transport-contract";
/**
* Pure core for the system prompt builder — zero DOM, zero effects, zero Svelte.
*
* The system prompt is a global template, stored on the backend, resolved once
* per conversation at first turn (then persisted for prompt-cache safety). The
* frontend builder exposes the template text plus a palette of available
* variables; clicking a variable inserts `[type:name]` at the cursor. This module
* holds the pure logic: tag construction, insertion into a template, and
* grouping of variables. The HTTP edge is injected via the ports defined below.
*/
// ── Injected ports (composition root adapts the store to these shapes) ─────────
export type SystemPromptLoadResult =
| { readonly ok: true; readonly template: string }
| { readonly ok: false; readonly error: string };
export type LoadSystemPrompt = () => Promise<SystemPromptLoadResult>;
export type SystemPromptSaveResult =
| { readonly ok: true; readonly template: string }
| { readonly ok: false; readonly error: string };
export type SaveSystemPrompt = (template: string) => Promise<SystemPromptSaveResult>;
export type SystemPromptVariablesResult =
| { readonly ok: true; readonly variables: readonly SystemPromptVariable[] }
| { readonly ok: false; readonly error: string };
export type LoadSystemPromptVariables = () => Promise<SystemPromptVariablesResult>;
// ── Template helpers ──────────────────────────────────────────────────────────
/** Build the literal placeholder `[type:name]` for a variable. */
export function buildTag(type: string, name: string): string {
return `[${type}:${name}]`;
}
/** Build the literal placeholder `[if type:name]` for a conditional block. */
export function buildIfTag(type: string, name: string): string {
return `[if ${type}:${name}]`;
}
/** Build the literal placeholder `[if !type:name]` for a negated conditional block. */
export function buildIfNotTag(type: string, name: string): string {
return `[if !${type}:${name}]`;
}
export interface Insertion {
/** Template text after insertion. */
template: string;
/** New cursor position (caret index) after insertion. */
cursor: number;
}
/**
* Insert `tag` into `template` at the current selection range. Replaces any
* selected text. Returns both the new template and the new cursor position so
* the caller can restore the caret after the inserted tag.
*/
export function insertTag(
template: string,
tag: string,
selectionStart: number,
selectionEnd: number,
): Insertion {
const before = template.slice(0, selectionStart);
const after = template.slice(selectionEnd);
const next = before + tag + after;
return { template: next, cursor: selectionStart + tag.length };
}
// ── Variable grouping ─────────────────────────────────────────────────────────
export interface VariableGroup {
/** Variable type (e.g. `"system"`, `"file"`, `"prompt"`, `"git"`). */
readonly type: string;
/** Variables of this type. */
readonly variables: readonly SystemPromptVariable[];
}
/**
* Group variables by type, preserving the order of first appearance within each
* group and the order of first-appearing types.
*/
export function groupVariables(
variables: readonly SystemPromptVariable[],
): readonly VariableGroup[] {
const order: string[] = [];
const map = new Map<string, SystemPromptVariable[]>();
for (const v of variables) {
if (!map.has(v.type)) {
map.set(v.type, []);
order.push(v.type);
}
map.get(v.type)?.push(v);
}
return order.map((type) => ({ type, variables: map.get(type) ?? [] }));
}
/** Whether a variable is "dynamic" (any name is valid, e.g. `file:<path>`). */
export function isDynamicVariable(variable: SystemPromptVariable): boolean {
return variable.dynamic === true;
}
|