opencode-acp-provider 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/prompt.ts ADDED
@@ -0,0 +1,100 @@
1
+ export interface ChatMessage {
2
+ role: string
3
+ content?: string | ReadonlyArray<{ type?: string; text?: unknown }>
4
+ }
5
+
6
+ interface HistoryLimits {
7
+ turns: number
8
+ chars: number
9
+ }
10
+
11
+ export function historyLimits(options: { historyTurns?: number; historyChars?: number }): HistoryLimits {
12
+ const count = (value: number | undefined, fallback: number) =>
13
+ value !== undefined && Number.isFinite(value) ? Math.max(0, Math.floor(value)) : fallback
14
+ return { turns: count(options.historyTurns, 16), chars: count(options.historyChars, 8_192) }
15
+ }
16
+
17
+ export function isAuxiliaryRequest(kind: unknown) {
18
+ return kind === "title" || kind === "generate" || kind === "compaction"
19
+ }
20
+
21
+ export function textOf(message: ChatMessage): string {
22
+ if (typeof message.content === "string") return message.content
23
+ if (!Array.isArray(message.content)) return ""
24
+ return message.content
25
+ .flatMap((part) => part?.type === "text" && typeof part.text === "string" && part.text ? [part.text] : [])
26
+ .join("\n")
27
+ }
28
+
29
+ const conversational = (message: ChatMessage) => message.role === "user" || message.role === "assistant"
30
+ const speaker = (message: ChatMessage) => message.role === "assistant" ? "Assistant: " : "User: "
31
+
32
+ /**
33
+ * Keep the current turn intact, plus the suffix needed to reconstruct bounded
34
+ * replay. One extra history character lets the shim detect truncation without
35
+ * losing the original clipping or sending the whole oldest retained message.
36
+ */
37
+ export function boundedMessages<T extends ChatMessage>(
38
+ messages: readonly T[],
39
+ limits: HistoryLimits,
40
+ includeSystem: boolean,
41
+ ): Array<{ message: T; text: string }> {
42
+ const kept: Array<{ message: T; text: string }> = []
43
+ let latest = false
44
+ let turns = 0
45
+ let budget = limits.chars > 0 ? limits.chars + 1 : 0
46
+ for (let i = messages.length - 1; i >= 0; i--) {
47
+ const message = messages[i]
48
+ if (!message) continue
49
+ const historyFull = turns >= limits.turns || budget <= 0
50
+ if (latest && historyFull && !includeSystem) break
51
+ if (message.role === "system") {
52
+ if (includeSystem) {
53
+ const text = textOf(message)
54
+ if (text.trim()) kept.push({ message, text })
55
+ }
56
+ continue
57
+ }
58
+ if (!conversational(message) || (latest && historyFull)) continue
59
+ const text = textOf(message)
60
+ if (!text.trim()) continue
61
+ if (!latest) {
62
+ kept.push({ message, text })
63
+ latest = true
64
+ continue
65
+ }
66
+ const tail = text.length > budget ? text.slice(-budget) : text
67
+ // The visible prefix is clipped away; it keeps a whitespace-only tail
68
+ // from being mistaken for an originally empty turn by the shim.
69
+ kept.push({ message, text: tail.trim() ? tail : `…${tail}` })
70
+ budget -= text.length + speaker(message).length + (turns > 0 ? 2 : 0)
71
+ turns++
72
+ }
73
+ return kept.reverse()
74
+ }
75
+
76
+ function historyText(turns: Array<{ message: ChatMessage; text: string }>, chars: number) {
77
+ const text = turns.map(({ message, text }) => `${speaker(message)}${text}`).join("\n\n")
78
+ return text.length > chars ? `[...earlier history omitted...]\n\n${text.slice(-chars)}` : text
79
+ }
80
+
81
+ /** Recent visible turns for an OpenCode compaction summary. */
82
+ export function recentConversation(messages: ChatMessage[], limits: HistoryLimits) {
83
+ if (!limits.turns || !limits.chars) return ""
84
+ const turns = boundedMessages(messages, { ...limits, turns: limits.turns - 1 }, false)
85
+ return historyText(turns, limits.chars)
86
+ }
87
+
88
+ /** Render both forms: only the manager knows whether the ACP session retained context. */
89
+ export function buildPrompts(messages: ChatMessage[], limits: HistoryLimits, includeSystem: boolean) {
90
+ const turns = boundedMessages(messages, limits, includeSystem)
91
+ const conversation = turns.filter(({ message }) => conversational(message))
92
+ const latest = conversation.pop()?.text ?? ""
93
+ const history = historyText(conversation, limits.chars)
94
+ const system = turns.filter(({ message }) => message.role === "system").map(({ text }) => text).join("\n\n")
95
+ return {
96
+ fresh: [system && `System instructions:\n${system}`, history && `Conversation so far:\n\n${history}`, latest]
97
+ .filter(Boolean).join("\n\n---\n\n"),
98
+ reused: [system && `System instructions:\n${system}`, latest].filter(Boolean).join("\n\n---\n\n"),
99
+ }
100
+ }