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