-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathutils.ts
More file actions
120 lines (108 loc) · 3.47 KB
/
utils.ts
File metadata and controls
120 lines (108 loc) · 3.47 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { UserMessage } from "@opencode-ai/sdk"
import { Logger } from "../logger"
import type { WithParts } from "../state"
/**
* Extracts a human-readable key from tool metadata for display purposes.
*/
export const extractParameterKey = (tool: string, parameters: any): string => {
if (!parameters) return ''
if (tool === "read" && parameters.filePath) {
return parameters.filePath
}
if (tool === "write" && parameters.filePath) {
return parameters.filePath
}
if (tool === "edit" && parameters.filePath) {
return parameters.filePath
}
if (tool === "list") {
return parameters.path || '(current directory)'
}
if (tool === "glob") {
if (parameters.pattern) {
const pathInfo = parameters.path ? ` in ${parameters.path}` : ""
return `"${parameters.pattern}"${pathInfo}`
}
return '(unknown pattern)'
}
if (tool === "grep") {
if (parameters.pattern) {
const pathInfo = parameters.path ? ` in ${parameters.path}` : ""
return `"${parameters.pattern}"${pathInfo}`
}
return '(unknown pattern)'
}
if (tool === "bash") {
if (parameters.description) return parameters.description
if (parameters.command) {
return parameters.command.length > 50
? parameters.command.substring(0, 50) + "..."
: parameters.command
}
}
if (tool === "webfetch" && parameters.url) {
return parameters.url
}
if (tool === "websearch" && parameters.query) {
return `"${parameters.query}"`
}
if (tool === "codesearch" && parameters.query) {
return `"${parameters.query}"`
}
if (tool === "todowrite") {
return `${parameters.todos?.length || 0} todos`
}
if (tool === "todoread") {
return "read todo list"
}
if (tool === "task" && parameters.description) {
return parameters.description
}
const paramStr = JSON.stringify(parameters)
if (paramStr === '{}' || paramStr === '[]' || paramStr === 'null') {
return ''
}
return paramStr.substring(0, 50)
}
export const getLastUserMessage = (
messages: WithParts[]
): WithParts | null => {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i]
if (msg.info.role === 'user') {
return msg
}
}
return null
}
export function getCurrentParams(
messages: WithParts[],
logger: Logger
): {
providerId: string | undefined,
modelId: string | undefined,
agent: string | undefined
} {
const userMsg = getLastUserMessage(messages)
if (!userMsg) {
logger.debug("No user message found when determining current params")
return { providerId: undefined, modelId: undefined, agent: undefined }
}
const agent: string = (userMsg.info as UserMessage).agent
const providerId: string | undefined = (userMsg.info as UserMessage).model.providerID
const modelId: string | undefined = (userMsg.info as UserMessage).model.modelID
return { providerId, modelId, agent }
}
export function buildToolIdList(messages: WithParts[]): string[] {
const toolIds: string[] = []
for (const msg of messages) {
if (msg.parts) {
for (const part of msg.parts) {
if (part.type === 'tool' && part.callID && part.tool) {
toolIds.push(part.callID)
}
}
}
}
return toolIds
}