| 1 | import { deepFreeze } from '@deepseek-ai/dsh-util-values' |
| 2 | |
| 3 | function brandString(value) { |
| 4 | return value; |
| 5 | } |
| 6 | |
| 7 | function randomUUID() { |
| 8 | const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16)); |
| 9 | const hex = Array.from(bytes, (byte, index) => { |
| 10 | return (index === 6 ? byte & 15 | 64 : index === 8 ? byte & 63 | 128 : byte).toString(16).padStart(2, "0"); |
| 11 | }).join(""); |
| 12 | return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; |
| 13 | } |
| 14 | |
| 15 | function createMessage(input) { |
| 16 | return deepFreeze(structuredClone({ |
| 17 | ...input, |
| 18 | id: brandString(randomUUID()) |
| 19 | })); |
| 20 | } |
| 21 | |
| 22 | function createUserMessage(input) { |
| 23 | return createMessage({ |
| 24 | ...input, |
| 25 | role: "user" |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | function createAssistantMessage(input) { |
| 30 | return createMessage({ |
| 31 | ...input, |
| 32 | role: "assistant" |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | function createSystemMessage(input) { |
| 37 | return createMessage({ |
| 38 | ...input, |
| 39 | role: "system" |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | function createToolResultMessage(input) { |
| 44 | return createMessage({ |
| 45 | ...input, |
| 46 | role: "tool" |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | function freezeMessage(message) { |
| 51 | return deepFreeze(structuredClone(message)); |
| 52 | } |
| 53 | |
| 54 | function contentHasImage(content) { |
| 55 | if (!Array.isArray(content)) return false; |
| 56 | return content.some((block) => block !== null && typeof block === "object" && (block.type === "image" || block.type === "image_ref")); |
| 57 | } |
| 58 | |
| 59 | function ReasoningEffortId(id) { |
| 60 | return brandString(id); |
| 61 | } |
| 62 | |
| 63 | function MessageId(id) { |
| 64 | return brandString(id); |
| 65 | } |
| 66 | |
| 67 | function ToolCallId(id) { |
| 68 | return brandString(id); |
| 69 | } |
| 70 | |
| 71 | function isAgentLoopRequest(request) { |
| 72 | return Boolean(request && typeof request === "object" && Array.isArray(request.messages)); |
| 73 | } |
| 74 | |
| 75 | function resolveRetryPolicy(policy) { |
| 76 | const base = policy && typeof policy === "object" ? policy : {}; |
| 77 | return Object.freeze({ |
| 78 | maxRetries: typeof base.maxRetries === "number" && Number.isInteger(base.maxRetries) && base.maxRetries >= 0 ? base.maxRetries : 3, |
| 79 | initialDelayMs: typeof base.initialDelayMs === "number" && base.initialDelayMs >= 0 ? base.initialDelayMs : 500, |
| 80 | maxDelayMs: typeof base.maxDelayMs === "number" && base.maxDelayMs >= 0 ? base.maxDelayMs : 15000, |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | function boundContextSummary(summary, maxChars = 4000) { |
| 85 | if (typeof summary !== "string") return ""; |
| 86 | if (summary.length <= maxChars) return summary; |
| 87 | return `${summary.slice(0, Math.max(0, maxChars - 1))}…`; |
| 88 | } |
| 89 | |
| 90 | class BlockAssembler { |
| 91 | constructor() { |
| 92 | this._blocks = []; |
| 93 | } |
| 94 | push(delta) { |
| 95 | if (!delta || typeof delta !== "object") return; |
| 96 | if (delta.type === "text_delta" && typeof delta.text === "string") { |
| 97 | const last = this._blocks[this._blocks.length - 1]; |
| 98 | if (last && last.type === "text") { |
| 99 | last.text += delta.text; |
| 100 | } else { |
| 101 | this._blocks.push({ type: "text", text: delta.text }); |
| 102 | } |
| 103 | return; |
| 104 | } |
| 105 | if (delta.type === "thinking_delta" && typeof delta.thinking === "string") { |
| 106 | const last = this._blocks[this._blocks.length - 1]; |
| 107 | if (last && last.type === "thinking") { |
| 108 | last.thinking += delta.thinking; |
| 109 | } else { |
| 110 | this._blocks.push({ type: "thinking", thinking: delta.thinking }); |
| 111 | } |
| 112 | return; |
| 113 | } |
| 114 | if (delta.type === "text" || delta.type === "thinking" || delta.type === "tool_use" || delta.type === "image") { |
| 115 | this._blocks.push(structuredClone(delta)); |
| 116 | } |
| 117 | } |
| 118 | snapshot() { |
| 119 | return structuredClone(this._blocks); |
| 120 | } |
| 121 | finish() { |
| 122 | return deepFreeze(structuredClone(this._blocks)); |
| 123 | } |
| 124 | reset() { |
| 125 | this._blocks = []; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | class LlmAdapter { |
| 130 | constructor(name = "adapter") { |
| 131 | this.name = name; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | function SessionId(id) { |
| 136 | return brandString(id); |
| 137 | } |
| 138 | |
| 139 | const SESSION_FORMAT_VERSION = 1; |
| 140 | |
| 141 | const KNOWN_SESSION_EVENT_TYPES = new Set([ |
| 142 | "session/start", |
| 143 | "session/end", |
| 144 | "session/metadata", |
| 145 | "turn/start", |
| 146 | "turn/end", |
| 147 | "message/user", |
| 148 | "message/assistant", |
| 149 | "message/system", |
| 150 | "message/tool", |
| 151 | "tool/call", |
| 152 | "tool/result", |
| 153 | "compaction", |
| 154 | "error", |
| 155 | "subagent/start", |
| 156 | "subagent/end", |
| 157 | ]); |
| 158 | |
| 159 | function deriveEventMessage(event) { |
| 160 | if (!event || typeof event !== "object") return undefined; |
| 161 | if (event.message && typeof event.message === "object") { |
| 162 | return event.message; |
| 163 | } |
| 164 | if (event.payload && typeof event.payload === "object" && event.payload.message && typeof event.payload.message === "object") { |
| 165 | return event.payload.message; |
| 166 | } |
| 167 | const type = event.type; |
| 168 | if (type === "message/user" || type === "message/assistant" || type === "message/system" || type === "message/tool") { |
| 169 | const role = type.slice("message/".length); |
| 170 | const content = Array.isArray(event.content) |
| 171 | ? event.content |
| 172 | : Array.isArray(event.payload?.content) |
| 173 | ? event.payload.content |
| 174 | : typeof event.text === "string" |
| 175 | ? [{ type: "text", text: event.text }] |
| 176 | : []; |
| 177 | return deepFreeze(structuredClone({ |
| 178 | id: typeof event.id === "string" ? event.id : brandString(randomUUID()), |
| 179 | role, |
| 180 | content, |
| 181 | })); |
| 182 | } |
| 183 | return undefined; |
| 184 | } |
| 185 | |
| 186 | const PERSONA_PREFIX_SECTION = "deployment:persona-prefix"; |
| 187 | |
| 188 | var HarnessError = class extends Error { |
| 189 | code; |
| 190 | constructor(message, code, options) { |
| 191 | super(message, options); |
| 192 | this.code = code; |
| 193 | this.name = new.target.name; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | var LlmError = class extends HarnessError { |
| 198 | constructor(message, code, options) { |
| 199 | super(message, code, options); |
| 200 | this.name = "LlmError"; |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | var UserQuestionError = class extends HarnessError { |
| 205 | constructor(message, code, options) { |
| 206 | super(message, code, options); |
| 207 | this.name = "UserQuestionError"; |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | Object.defineProperty(HarnessError, "name", { value: "HarnessError" }); |
| 212 | Object.defineProperty(LlmError, "name", { value: "LlmError" }); |
| 213 | Object.defineProperty(UserQuestionError, "name", { value: "UserQuestionError" }); |
| 214 | |
| 215 | export { |
| 216 | createUserMessage, |
| 217 | createAssistantMessage, |
| 218 | createSystemMessage, |
| 219 | createToolResultMessage, |
| 220 | freezeMessage, |
| 221 | contentHasImage, |
| 222 | ReasoningEffortId, |
| 223 | MessageId, |
| 224 | ToolCallId, |
| 225 | isAgentLoopRequest, |
| 226 | resolveRetryPolicy, |
| 227 | boundContextSummary, |
| 228 | BlockAssembler, |
| 229 | LlmAdapter, |
| 230 | LlmError, |
| 231 | SessionId, |
| 232 | SESSION_FORMAT_VERSION, |
| 233 | KNOWN_SESSION_EVENT_TYPES, |
| 234 | deriveEventMessage, |
| 235 | PERSONA_PREFIX_SECTION, |
| 236 | HarnessError, |
| 237 | UserQuestionError, |
| 238 | } |
| 239 |