dsh-codearts 1.1.1 → 1.1.4

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.
@@ -0,0 +1,113 @@
1
+ import { n as __exportAll } from "./rolldown-runtime-B4iAMlE-.mjs";
2
+ //#region node_modules/.pnpm/@earendil-works+pi-ai@1.3.0_@aws-sdk+credential-provider-node@3.972.84_@smithy+signature-v4@5.7.4_ws@8.22.0_zod@4.6.5/node_modules/@earendil-works/pi-ai/dist/utils/text.js
3
+ /** Extract and join text from message content. */
4
+ function contentText(content, separator = "\n") {
5
+ if (typeof content === "string") return content;
6
+ return content.filter((block) => block.type === "text").map((block) => block.text).join(separator);
7
+ }
8
+ /** Render a system message as a complete prompt: its content followed by its sections. */
9
+ function getSystemMessageText(message) {
10
+ const parts = [contentText(message.content)];
11
+ for (const text of Object.values(message.sections ?? {})) if (text !== null) parts.push(text);
12
+ return parts.filter((part) => part.length > 0).join("\n\n");
13
+ }
14
+ /**
15
+ * Render a later system message for APIs that accept system messages mid-conversation.
16
+ * Section changes are framed by name so the model can relate them to the leading prompt.
17
+ * This framing is request-time only and may change between versions.
18
+ */
19
+ function renderSystemMessageUpdate(message) {
20
+ const parts = [];
21
+ const text = contentText(message.content);
22
+ if (text.length > 0) parts.push(text);
23
+ for (const [name, value] of Object.entries(message.sections ?? {})) parts.push(value === null ? `Removed system prompt section "${name}".` : `Updated system prompt section "${name}":\n\n${value}`);
24
+ return parts.join("\n\n");
25
+ }
26
+ //#endregion
27
+ //#region node_modules/.pnpm/@earendil-works+pi-ai@1.3.0_@aws-sdk+credential-provider-node@3.972.84_@smithy+signature-v4@5.7.4_ws@8.22.0_zod@4.6.5/node_modules/@earendil-works/pi-ai/dist/utils/estimate.js
28
+ var estimate_exports = /* @__PURE__ */ __exportAll({
29
+ calculateContextTokens: () => calculateContextTokens,
30
+ estimateContextTokens: () => estimateContextTokens,
31
+ estimateMessageTokens: () => estimateMessageTokens,
32
+ estimateTextAndImageContentTokens: () => estimateTextAndImageContentTokens,
33
+ estimateTextTokens: () => estimateTextTokens
34
+ });
35
+ const CHARS_PER_TOKEN = 3.5;
36
+ const ESTIMATED_IMAGE_CHARS = 4800;
37
+ function calculateContextTokens(usage) {
38
+ return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
39
+ }
40
+ function safeJsonStringify(value) {
41
+ try {
42
+ return JSON.stringify(value) ?? "undefined";
43
+ } catch {
44
+ return "[unserializable]";
45
+ }
46
+ }
47
+ function estimateTextAndImageContentChars(content) {
48
+ if (typeof content === "string") return content.length;
49
+ let chars = 0;
50
+ for (const block of content) chars += block.type === "text" ? block.text.length : ESTIMATED_IMAGE_CHARS;
51
+ return chars;
52
+ }
53
+ function estimateTextTokens(text) {
54
+ return Math.ceil(text.length / CHARS_PER_TOKEN);
55
+ }
56
+ function estimateTextAndImageContentTokens(content) {
57
+ return Math.ceil(estimateTextAndImageContentChars(content) / CHARS_PER_TOKEN);
58
+ }
59
+ function estimateMessageTokens(message) {
60
+ let chars = 0;
61
+ if (message.role === "system") return estimateTextTokens(getSystemMessageText(message)) + estimateToolsTokens(message.toolsAdded) + estimateToolsTokens(message.toolsRemoved);
62
+ if (message.role === "user") return estimateTextAndImageContentTokens(message.content);
63
+ if (message.role === "toolResult") return estimateTextAndImageContentTokens(message.content);
64
+ for (const block of message.content) if (block.type === "text") chars += block.text.length;
65
+ else if (block.type === "thinking") chars += block.thinking.length;
66
+ else chars += block.name.length + safeJsonStringify(block.arguments).length;
67
+ return Math.ceil(chars / CHARS_PER_TOKEN);
68
+ }
69
+ function getLastAssistantUsageInfo(messages) {
70
+ let latestPrefixTimestamp = Number.NEGATIVE_INFINITY;
71
+ let usageInfo;
72
+ for (let i = 0; i < messages.length; i++) {
73
+ const message = messages[i];
74
+ if (message.role === "assistant") {
75
+ const assistant = message;
76
+ if (assistant.timestamp >= latestPrefixTimestamp && assistant.stopReason !== "aborted" && assistant.stopReason !== "error" && calculateContextTokens(assistant.usage) > 0) usageInfo = {
77
+ usage: assistant.usage,
78
+ index: i
79
+ };
80
+ }
81
+ latestPrefixTimestamp = Math.max(latestPrefixTimestamp, message.timestamp);
82
+ }
83
+ return usageInfo;
84
+ }
85
+ function estimateContextTokens(context) {
86
+ const messages = "messages" in context ? context.messages : context;
87
+ const usageInfo = getLastAssistantUsageInfo(messages);
88
+ if (usageInfo) {
89
+ const usageTokens = calculateContextTokens(usageInfo.usage);
90
+ let trailingTokens = 0;
91
+ for (let i = usageInfo.index + 1; i < messages.length; i++) trailingTokens += estimateMessageTokens(messages[i]);
92
+ return {
93
+ tokens: usageTokens + trailingTokens,
94
+ usageTokens,
95
+ trailingTokens,
96
+ lastUsageIndex: usageInfo.index
97
+ };
98
+ }
99
+ let tokens = 0;
100
+ for (const message of messages) tokens += estimateMessageTokens(message);
101
+ return {
102
+ tokens,
103
+ usageTokens: 0,
104
+ trailingTokens: tokens,
105
+ lastUsageIndex: null
106
+ };
107
+ }
108
+ function estimateToolsTokens(tools) {
109
+ if (!tools || tools.length === 0) return 0;
110
+ return estimateTextTokens(safeJsonStringify(tools));
111
+ }
112
+ //#endregion
113
+ export { renderSystemMessageUpdate as a, getSystemMessageText as i, estimate_exports as n, contentText as r, estimateContextTokens as t };