@honeagents/hone 0.1.0 → 0.1.1
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/README.md +203 -4
- package/dist/index.d.mts +99 -48
- package/dist/index.d.ts +99 -48
- package/dist/index.js +252 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +250 -108
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,230 @@
|
|
|
1
|
+
// src/tools.ts
|
|
2
|
+
function createToolCallMessage(toolCalls, content = "") {
|
|
3
|
+
return {
|
|
4
|
+
role: "assistant",
|
|
5
|
+
content,
|
|
6
|
+
tool_calls: toolCalls
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function createToolResultMessage(toolCallId, result) {
|
|
10
|
+
const content = typeof result === "string" ? result : JSON.stringify(result);
|
|
11
|
+
return {
|
|
12
|
+
role: "tool",
|
|
13
|
+
content,
|
|
14
|
+
tool_call_id: toolCallId
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function extractOpenAIMessages(response) {
|
|
18
|
+
const messages = [];
|
|
19
|
+
for (const choice of response.choices) {
|
|
20
|
+
const msg = choice.message;
|
|
21
|
+
const message = {
|
|
22
|
+
role: msg.role,
|
|
23
|
+
content: msg.content ?? ""
|
|
24
|
+
};
|
|
25
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
26
|
+
message.tool_calls = msg.tool_calls.filter((tc) => tc.type === "function").map((tc) => ({
|
|
27
|
+
id: tc.id,
|
|
28
|
+
name: tc.function.name,
|
|
29
|
+
arguments: tc.function.arguments
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
messages.push(message);
|
|
33
|
+
}
|
|
34
|
+
return messages;
|
|
35
|
+
}
|
|
36
|
+
function extractAnthropicMessages(response) {
|
|
37
|
+
const messages = [];
|
|
38
|
+
const textContent = response.content.filter((block) => block.type === "text").map((block) => block.text).join("\n");
|
|
39
|
+
const toolUseBlocks = response.content.filter(
|
|
40
|
+
(block) => block.type === "tool_use"
|
|
41
|
+
);
|
|
42
|
+
if (toolUseBlocks.length > 0) {
|
|
43
|
+
const toolCalls = toolUseBlocks.map((block) => ({
|
|
44
|
+
id: block.id,
|
|
45
|
+
name: block.name,
|
|
46
|
+
arguments: JSON.stringify(block.input)
|
|
47
|
+
}));
|
|
48
|
+
messages.push({
|
|
49
|
+
role: "assistant",
|
|
50
|
+
content: textContent,
|
|
51
|
+
tool_calls: toolCalls
|
|
52
|
+
});
|
|
53
|
+
} else {
|
|
54
|
+
messages.push({
|
|
55
|
+
role: response.role,
|
|
56
|
+
content: textContent
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return messages;
|
|
60
|
+
}
|
|
61
|
+
function extractGeminiMessages(response) {
|
|
62
|
+
const messages = [];
|
|
63
|
+
const candidates = response.response.candidates;
|
|
64
|
+
if (!candidates || candidates.length === 0) {
|
|
65
|
+
return messages;
|
|
66
|
+
}
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
if (!candidate.content?.parts) continue;
|
|
69
|
+
const textParts = [];
|
|
70
|
+
const functionCalls = [];
|
|
71
|
+
for (const part of candidate.content.parts) {
|
|
72
|
+
if ("text" in part && part.text) {
|
|
73
|
+
textParts.push(part.text);
|
|
74
|
+
} else if ("functionCall" in part && part.functionCall) {
|
|
75
|
+
functionCalls.push({
|
|
76
|
+
name: part.functionCall.name,
|
|
77
|
+
args: part.functionCall.args || {}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const textContent = textParts.join("\n");
|
|
82
|
+
if (functionCalls.length > 0) {
|
|
83
|
+
const toolCalls = functionCalls.map((fc, index) => ({
|
|
84
|
+
id: `gemini_${fc.name}_${index}_${Date.now()}`,
|
|
85
|
+
name: fc.name,
|
|
86
|
+
arguments: JSON.stringify(fc.args)
|
|
87
|
+
}));
|
|
88
|
+
messages.push({
|
|
89
|
+
role: "assistant",
|
|
90
|
+
content: textContent,
|
|
91
|
+
tool_calls: toolCalls
|
|
92
|
+
});
|
|
93
|
+
} else if (textContent) {
|
|
94
|
+
messages.push({
|
|
95
|
+
role: candidate.content.role === "model" ? "assistant" : candidate.content.role || "assistant",
|
|
96
|
+
content: textContent
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return messages;
|
|
101
|
+
}
|
|
102
|
+
function normalizeOpenAIMessages(messages) {
|
|
103
|
+
const result = [];
|
|
104
|
+
for (const m of messages) {
|
|
105
|
+
if (m.role === "system" || m.role === "user" || m.role === "assistant") {
|
|
106
|
+
const content = typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.filter((c) => c.type === "text").map((c) => c.text).join("\n") : "";
|
|
107
|
+
const message = {
|
|
108
|
+
role: m.role,
|
|
109
|
+
content: content || ""
|
|
110
|
+
};
|
|
111
|
+
if (m.role === "assistant" && "tool_calls" in m && m.tool_calls) {
|
|
112
|
+
message.tool_calls = m.tool_calls.filter((tc) => tc.type === "function").map((tc) => ({
|
|
113
|
+
id: tc.id,
|
|
114
|
+
name: tc.function.name,
|
|
115
|
+
arguments: tc.function.arguments
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
result.push(message);
|
|
119
|
+
} else if (m.role === "tool") {
|
|
120
|
+
result.push({
|
|
121
|
+
role: "tool",
|
|
122
|
+
content: typeof m.content === "string" ? m.content : "",
|
|
123
|
+
tool_call_id: m.tool_call_id
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
function normalizeAnthropicMessages(messages) {
|
|
130
|
+
const result = [];
|
|
131
|
+
for (const m of messages) {
|
|
132
|
+
if (typeof m.content === "string") {
|
|
133
|
+
result.push({
|
|
134
|
+
role: m.role,
|
|
135
|
+
content: m.content
|
|
136
|
+
});
|
|
137
|
+
} else if (Array.isArray(m.content)) {
|
|
138
|
+
const textParts = [];
|
|
139
|
+
const toolCalls = [];
|
|
140
|
+
const toolResults = [];
|
|
141
|
+
for (const block of m.content) {
|
|
142
|
+
if (block.type === "text") {
|
|
143
|
+
textParts.push(block.text);
|
|
144
|
+
} else if (block.type === "tool_use") {
|
|
145
|
+
toolCalls.push({
|
|
146
|
+
id: block.id,
|
|
147
|
+
name: block.name,
|
|
148
|
+
arguments: JSON.stringify(block.input)
|
|
149
|
+
});
|
|
150
|
+
} else if (block.type === "tool_result") {
|
|
151
|
+
const content = typeof block.content === "string" ? block.content : Array.isArray(block.content) ? block.content.filter((c) => c.type === "text").map((c) => c.text).join("\n") : "";
|
|
152
|
+
toolResults.push({
|
|
153
|
+
tool_use_id: block.tool_use_id,
|
|
154
|
+
content
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (textParts.length > 0 || toolCalls.length > 0) {
|
|
159
|
+
const message = {
|
|
160
|
+
role: m.role,
|
|
161
|
+
content: textParts.join("\n")
|
|
162
|
+
};
|
|
163
|
+
if (toolCalls.length > 0) {
|
|
164
|
+
message.tool_calls = toolCalls;
|
|
165
|
+
}
|
|
166
|
+
result.push(message);
|
|
167
|
+
}
|
|
168
|
+
for (const tr of toolResults) {
|
|
169
|
+
result.push({
|
|
170
|
+
role: "tool",
|
|
171
|
+
content: tr.content,
|
|
172
|
+
tool_call_id: tr.tool_use_id
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
function normalizeGeminiContents(contents) {
|
|
180
|
+
const result = [];
|
|
181
|
+
for (const c of contents) {
|
|
182
|
+
const textParts = [];
|
|
183
|
+
const toolCalls = [];
|
|
184
|
+
const toolResults = [];
|
|
185
|
+
for (const part of c.parts) {
|
|
186
|
+
if ("text" in part && part.text) {
|
|
187
|
+
textParts.push(part.text);
|
|
188
|
+
} else if ("functionCall" in part && part.functionCall) {
|
|
189
|
+
toolCalls.push({
|
|
190
|
+
id: `gemini_${part.functionCall.name}_${Date.now()}`,
|
|
191
|
+
name: part.functionCall.name,
|
|
192
|
+
arguments: JSON.stringify(part.functionCall.args)
|
|
193
|
+
});
|
|
194
|
+
} else if ("functionResponse" in part && part.functionResponse) {
|
|
195
|
+
toolResults.push({
|
|
196
|
+
name: part.functionResponse.name,
|
|
197
|
+
content: JSON.stringify(part.functionResponse.response)
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const role = c.role === "model" ? "assistant" : "user";
|
|
202
|
+
if (textParts.length > 0 || toolCalls.length > 0) {
|
|
203
|
+
const message = {
|
|
204
|
+
role,
|
|
205
|
+
content: textParts.join("\n")
|
|
206
|
+
};
|
|
207
|
+
if (toolCalls.length > 0) {
|
|
208
|
+
message.tool_calls = toolCalls;
|
|
209
|
+
}
|
|
210
|
+
result.push(message);
|
|
211
|
+
}
|
|
212
|
+
for (const tr of toolResults) {
|
|
213
|
+
result.push({
|
|
214
|
+
role: "tool",
|
|
215
|
+
content: tr.content,
|
|
216
|
+
tool_call_id: tr.name
|
|
217
|
+
// Gemini uses function name, not ID
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
var toolResult = createToolResultMessage;
|
|
224
|
+
var fromOpenAI = extractOpenAIMessages;
|
|
225
|
+
var fromAnthropic = extractAnthropicMessages;
|
|
226
|
+
var fromGemini = extractGeminiMessages;
|
|
227
|
+
|
|
1
228
|
// src/agent.ts
|
|
2
229
|
function isAgentOptions(value) {
|
|
3
230
|
return typeof value === "object" && value !== null && "defaultPrompt" in value && "model" in value && "provider" in value;
|
|
@@ -370,10 +597,30 @@ var Hone = class {
|
|
|
370
597
|
};
|
|
371
598
|
}
|
|
372
599
|
}
|
|
373
|
-
async track(id,
|
|
600
|
+
async track(id, input, options) {
|
|
601
|
+
let normalizedMessages;
|
|
602
|
+
if (Array.isArray(input)) {
|
|
603
|
+
normalizedMessages = input;
|
|
604
|
+
} else if (input.provider === "openai") {
|
|
605
|
+
const inputMessages = normalizeOpenAIMessages(input.messages);
|
|
606
|
+
const responseMessages = extractOpenAIMessages(input.response);
|
|
607
|
+
normalizedMessages = [...inputMessages, ...responseMessages];
|
|
608
|
+
} else if (input.provider === "anthropic") {
|
|
609
|
+
const systemMessage = input.system ? [{ role: "system", content: input.system }] : [];
|
|
610
|
+
const inputMessages = normalizeAnthropicMessages(input.messages);
|
|
611
|
+
const responseMessages = extractAnthropicMessages(input.response);
|
|
612
|
+
normalizedMessages = [...systemMessage, ...inputMessages, ...responseMessages];
|
|
613
|
+
} else if (input.provider === "gemini") {
|
|
614
|
+
const systemMessage = input.systemInstruction ? [{ role: "system", content: input.systemInstruction }] : [];
|
|
615
|
+
const inputMessages = normalizeGeminiContents(input.contents);
|
|
616
|
+
const responseMessages = extractGeminiMessages(input.response);
|
|
617
|
+
normalizedMessages = [...systemMessage, ...inputMessages, ...responseMessages];
|
|
618
|
+
} else {
|
|
619
|
+
throw new Error("Invalid track input: must be Message[] or provider-specific input");
|
|
620
|
+
}
|
|
374
621
|
await this.makeRequest("/insert_runs", "POST", {
|
|
375
622
|
id,
|
|
376
|
-
messages,
|
|
623
|
+
messages: normalizedMessages,
|
|
377
624
|
sessionId: options.sessionId,
|
|
378
625
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
379
626
|
});
|
|
@@ -433,111 +680,6 @@ function getProviderDisplayName(provider) {
|
|
|
433
680
|
return PROVIDER_DISPLAY_NAMES[provider] || provider;
|
|
434
681
|
}
|
|
435
682
|
|
|
436
|
-
|
|
437
|
-
function createToolCallMessage(toolCalls, content = "") {
|
|
438
|
-
return {
|
|
439
|
-
role: "assistant",
|
|
440
|
-
content,
|
|
441
|
-
tool_calls: toolCalls
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
function createToolResultMessage(toolCallId, result) {
|
|
445
|
-
const content = typeof result === "string" ? result : JSON.stringify(result);
|
|
446
|
-
return {
|
|
447
|
-
role: "tool",
|
|
448
|
-
content,
|
|
449
|
-
tool_call_id: toolCallId
|
|
450
|
-
};
|
|
451
|
-
}
|
|
452
|
-
function extractOpenAIMessages(response) {
|
|
453
|
-
const messages = [];
|
|
454
|
-
for (const choice of response.choices) {
|
|
455
|
-
const msg = choice.message;
|
|
456
|
-
const message = {
|
|
457
|
-
role: msg.role,
|
|
458
|
-
content: msg.content ?? ""
|
|
459
|
-
};
|
|
460
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
461
|
-
message.tool_calls = msg.tool_calls.map((tc) => ({
|
|
462
|
-
id: tc.id,
|
|
463
|
-
name: tc.function.name,
|
|
464
|
-
arguments: tc.function.arguments
|
|
465
|
-
}));
|
|
466
|
-
}
|
|
467
|
-
messages.push(message);
|
|
468
|
-
}
|
|
469
|
-
return messages;
|
|
470
|
-
}
|
|
471
|
-
function extractAnthropicMessages(response) {
|
|
472
|
-
const messages = [];
|
|
473
|
-
const textBlocks = response.content.filter(
|
|
474
|
-
(block) => block.type === "text"
|
|
475
|
-
);
|
|
476
|
-
const toolUseBlocks = response.content.filter(
|
|
477
|
-
(block) => block.type === "tool_use"
|
|
478
|
-
);
|
|
479
|
-
const textContent = textBlocks.map((b) => b.text).join("\n");
|
|
480
|
-
if (toolUseBlocks.length > 0) {
|
|
481
|
-
const toolCalls = toolUseBlocks.map((block) => ({
|
|
482
|
-
id: block.id,
|
|
483
|
-
name: block.name,
|
|
484
|
-
arguments: JSON.stringify(block.input)
|
|
485
|
-
}));
|
|
486
|
-
messages.push({
|
|
487
|
-
role: "assistant",
|
|
488
|
-
content: textContent,
|
|
489
|
-
tool_calls: toolCalls
|
|
490
|
-
});
|
|
491
|
-
} else {
|
|
492
|
-
messages.push({
|
|
493
|
-
role: response.role,
|
|
494
|
-
content: textContent
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
return messages;
|
|
498
|
-
}
|
|
499
|
-
function extractGeminiMessages(response) {
|
|
500
|
-
const messages = [];
|
|
501
|
-
if (!response.candidates || response.candidates.length === 0) {
|
|
502
|
-
return messages;
|
|
503
|
-
}
|
|
504
|
-
for (const candidate of response.candidates) {
|
|
505
|
-
if (!candidate.content?.parts) continue;
|
|
506
|
-
const textParts = [];
|
|
507
|
-
const functionCalls = [];
|
|
508
|
-
for (const part of candidate.content.parts) {
|
|
509
|
-
if ("text" in part) {
|
|
510
|
-
textParts.push(part.text);
|
|
511
|
-
} else if ("functionCall" in part) {
|
|
512
|
-
functionCalls.push(part.functionCall);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
const textContent = textParts.join("\n");
|
|
516
|
-
if (functionCalls.length > 0) {
|
|
517
|
-
const toolCalls = functionCalls.map((fc, index) => ({
|
|
518
|
-
id: `gemini_${fc.name}_${index}_${Date.now()}`,
|
|
519
|
-
name: fc.name,
|
|
520
|
-
arguments: JSON.stringify(fc.args)
|
|
521
|
-
}));
|
|
522
|
-
messages.push({
|
|
523
|
-
role: "assistant",
|
|
524
|
-
content: textContent,
|
|
525
|
-
tool_calls: toolCalls
|
|
526
|
-
});
|
|
527
|
-
} else if (textContent) {
|
|
528
|
-
messages.push({
|
|
529
|
-
role: candidate.content.role === "model" ? "assistant" : candidate.content.role || "assistant",
|
|
530
|
-
content: textContent
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
return messages;
|
|
535
|
-
}
|
|
536
|
-
var toolResult = createToolResultMessage;
|
|
537
|
-
var fromOpenAI = extractOpenAIMessages;
|
|
538
|
-
var fromAnthropic = extractAnthropicMessages;
|
|
539
|
-
var fromGemini = extractGeminiMessages;
|
|
540
|
-
|
|
541
|
-
export { AIProvider, AI_PROVIDER_VALUES, Hone, createHoneClient, createToolCallMessage, createToolResultMessage, evaluateAgent, evaluatePrompt, extractAnthropicMessages, extractGeminiMessages, extractOpenAIMessages, formatAgentRequest, formatPromptRequest, fromAnthropic, fromGemini, fromOpenAI, getAgentNode, getPromptNode, getProviderDisplayName, insertParamsIntoPrompt, isValidProvider, toolResult, traverseAgentNode, traversePromptNode, updateAgentNodes, updatePromptNodes };
|
|
683
|
+
export { AIProvider, AI_PROVIDER_VALUES, Hone, createHoneClient, createToolCallMessage, createToolResultMessage, evaluateAgent, evaluatePrompt, extractAnthropicMessages, extractGeminiMessages, extractOpenAIMessages, formatAgentRequest, formatPromptRequest, fromAnthropic, fromGemini, fromOpenAI, getAgentNode, getPromptNode, getProviderDisplayName, insertParamsIntoPrompt, isValidProvider, normalizeAnthropicMessages, normalizeGeminiContents, normalizeOpenAIMessages, toolResult, traverseAgentNode, traversePromptNode, updateAgentNodes, updatePromptNodes };
|
|
542
684
|
//# sourceMappingURL=index.mjs.map
|
|
543
685
|
//# sourceMappingURL=index.mjs.map
|