MCPcopy Index your code
hub / github.com/anomalyco/opencode / messagePrompt

Function messagePrompt

packages/opencode/src/cli/cmd/run/session.shared.ts:63–130  ·  view source on GitHub ↗
(msg: SessionMessages[number])

Source from the content-addressed store, hash-verified

61}
62
63export function messagePrompt(msg: SessionMessages[number]): RunPrompt {
64 const parts: RunPrompt["parts"] = []
65 let text = msg.parts
66 .filter((part): part is Extract<SessionMessages[number]["parts"][number], { type: "text" }> => {
67 return part.type === "text" && !part.synthetic
68 })
69 .map((part) => part.text)
70 .join("")
71 let cursor = Bun.stringWidth(text)
72 const used: Array<{ start: number; end: number }> = []
73
74 const take = (value: string): { start: number; end: number; value: string } | undefined => {
75 let from = 0
76 while (true) {
77 const idx = text.indexOf(value, from)
78 if (idx === -1) {
79 return undefined
80 }
81
82 const start = Bun.stringWidth(text.slice(0, idx))
83 const end = start + Bun.stringWidth(value)
84 if (!used.some((item) => item.start < end && start < item.end)) {
85 return { start, end, value }
86 }
87
88 from = idx + value.length
89 }
90 }
91
92 const add = (value: string) => {
93 const gap = text ? " " : ""
94 const start = cursor + Bun.stringWidth(gap)
95 text += gap + value
96 const end = start + Bun.stringWidth(value)
97 cursor = end
98 return { start, end, value }
99 }
100
101 for (const part of msg.parts) {
102 if (part.type === "file") {
103 const next = part.source?.text ? structuredClone(part.source.text) : take("@" + fileName(part.url, part.filename))
104 const span = next ?? add("@" + fileName(part.url, part.filename))
105 used.push({ start: span.start, end: span.end })
106 parts.push({
107 type: "file",
108 mime: part.mime,
109 filename: part.filename,
110 url: part.url,
111 source: fileSource(part, span),
112 })
113 continue
114 }
115
116 if (part.type !== "agent") {
117 continue
118 }
119
120 const span = part.source ? structuredClone(part.source) : (take("@" + part.name) ?? add("@" + part.name))

Callers 3

isShellSyntheticUserFunction · 0.90
replayMessageFunction · 0.90
turnFunction · 0.85

Calls 5

fileNameFunction · 0.85
fileSourceFunction · 0.85
pushMethod · 0.80
takeFunction · 0.70
addFunction · 0.70

Tested by

no test coverage detected