@alejandroroman/agent-kit 0.1.4 → 0.2.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.
Files changed (77) hide show
  1. package/dist/agent/loop.js +213 -111
  2. package/dist/agent/types.d.ts +2 -0
  3. package/dist/api/errors.d.ts +3 -0
  4. package/dist/api/errors.js +37 -0
  5. package/dist/api/events.d.ts +5 -0
  6. package/dist/api/events.js +28 -0
  7. package/dist/api/router.js +10 -0
  8. package/dist/api/traces.d.ts +3 -0
  9. package/dist/api/traces.js +35 -0
  10. package/dist/api/types.d.ts +2 -0
  11. package/dist/bootstrap.d.ts +3 -1
  12. package/dist/bootstrap.js +26 -7
  13. package/dist/cli/chat.js +3 -1
  14. package/dist/cli/claude-md-template.d.ts +5 -0
  15. package/dist/cli/claude-md-template.js +220 -0
  16. package/dist/cli/config-writer.js +3 -0
  17. package/dist/cli/env.d.ts +14 -0
  18. package/dist/cli/env.js +68 -0
  19. package/dist/cli/init.js +10 -0
  20. package/dist/cli/setup-agent/index.js +61 -18
  21. package/dist/cli/slack-setup.d.ts +6 -0
  22. package/dist/cli/slack-setup.js +234 -0
  23. package/dist/cli/start.js +65 -16
  24. package/dist/cli/ui.d.ts +2 -0
  25. package/dist/cli/ui.js +4 -1
  26. package/dist/cli/whats-new.d.ts +1 -0
  27. package/dist/cli/whats-new.js +69 -0
  28. package/dist/cli.js +14 -0
  29. package/dist/config/resolve.d.ts +1 -0
  30. package/dist/config/resolve.js +1 -0
  31. package/dist/config/schema.d.ts +2 -0
  32. package/dist/config/schema.js +1 -0
  33. package/dist/config/writer.d.ts +18 -0
  34. package/dist/config/writer.js +85 -0
  35. package/dist/cron/scheduler.d.ts +4 -1
  36. package/dist/cron/scheduler.js +99 -52
  37. package/dist/gateways/slack/client.d.ts +1 -0
  38. package/dist/gateways/slack/client.js +9 -0
  39. package/dist/gateways/slack/handler.js +2 -1
  40. package/dist/gateways/slack/index.js +75 -29
  41. package/dist/gateways/slack/listener.d.ts +8 -1
  42. package/dist/gateways/slack/listener.js +36 -10
  43. package/dist/heartbeat/runner.js +99 -82
  44. package/dist/llm/anthropic.d.ts +1 -0
  45. package/dist/llm/anthropic.js +11 -2
  46. package/dist/llm/fallback.js +34 -2
  47. package/dist/llm/openai.d.ts +2 -0
  48. package/dist/llm/openai.js +33 -2
  49. package/dist/llm/types.d.ts +16 -2
  50. package/dist/llm/types.js +9 -0
  51. package/dist/logger.d.ts +1 -0
  52. package/dist/logger.js +11 -0
  53. package/dist/media/sanitize.d.ts +5 -0
  54. package/dist/media/sanitize.js +53 -0
  55. package/dist/multi/spawn.js +29 -10
  56. package/dist/session/compaction.js +3 -1
  57. package/dist/session/prune-images.d.ts +9 -0
  58. package/dist/session/prune-images.js +42 -0
  59. package/dist/skills/activate.d.ts +6 -0
  60. package/dist/skills/activate.js +72 -27
  61. package/dist/skills/index.d.ts +1 -1
  62. package/dist/skills/index.js +1 -1
  63. package/dist/telemetry/db.d.ts +63 -0
  64. package/dist/telemetry/db.js +193 -0
  65. package/dist/telemetry/index.d.ts +17 -0
  66. package/dist/telemetry/index.js +82 -0
  67. package/dist/telemetry/sanitize.d.ts +6 -0
  68. package/dist/telemetry/sanitize.js +48 -0
  69. package/dist/telemetry/sqlite-processor.d.ts +11 -0
  70. package/dist/telemetry/sqlite-processor.js +108 -0
  71. package/dist/telemetry/types.d.ts +30 -0
  72. package/dist/telemetry/types.js +31 -0
  73. package/dist/tools/builtin/index.d.ts +2 -0
  74. package/dist/tools/builtin/index.js +2 -0
  75. package/dist/tools/builtin/self-config.d.ts +4 -0
  76. package/dist/tools/builtin/self-config.js +182 -0
  77. package/package.json +10 -2
@@ -5,9 +5,11 @@ import { setupAgentSession } from "../agent/setup.js";
5
5
  import { resolveAgent, resolveModelAlias, resolveWebSearch } from "../config/resolve.js";
6
6
  import { createBuiltinRegistry } from "../tools/builtin/index.js";
7
7
  import { registerSpawnWrappers } from "../tools/builtin/spawn.js";
8
- import { createActivateSkillTool } from "../skills/index.js";
8
+ import { createActivateSkillTool, preActivateSkills } from "../skills/index.js";
9
9
  import { createLogger } from "../logger.js";
10
10
  import { dateContext } from "../text.js";
11
+ import { context, trace, SpanStatusCode } from "@opentelemetry/api";
12
+ import { getTracer, ATTR } from "../telemetry/index.js";
11
13
  const log = createLogger("heartbeat");
12
14
  const HEARTBEAT_OK = "HEARTBEAT_OK";
13
15
  export function isHeartbeatSuppressed(text) {
@@ -64,93 +66,99 @@ export class HeartbeatRunner {
64
66
  if (!agentDef?.heartbeat)
65
67
  return undefined;
66
68
  const hb = agentDef.heartbeat;
67
- if (!isWithinActiveHours(hb.activeHours)) {
68
- log.debug({ agent: agentName }, "outside active hours, skipping");
69
- return undefined;
70
- }
71
- const instructions = this.loadHeartbeatInstructions(agentName);
72
- if (!instructions) {
73
- log.warn({ agent: agentName }, "no HEARTBEAT.md found, skipping");
74
- return undefined;
75
- }
76
- // Isolated tool registry per tick (same pattern as CronScheduler)
77
- const sandbox = agentDef.sandbox ?? this.config.defaults.sandbox;
78
- const memoryConfig = this.config.defaults.memory;
79
- const tickRegistry = createBuiltinRegistry({
80
- allowedCommands: sandbox?.allowedCommands,
81
- allowedPaths: sandbox?.allowedPaths,
82
- memoryConfig,
83
- webSearch: resolveWebSearch(agentName, this.config),
69
+ const tracer = getTracer("heartbeat");
70
+ const span = tracer.startSpan("source.heartbeat", {
71
+ attributes: {
72
+ [ATTR.AGENT]: agentName,
73
+ [ATTR.SOURCE]: "heartbeat",
74
+ },
84
75
  });
85
- const resolved = resolveAgent(agentName, this.config, tickRegistry, this.skillsDir);
86
- // Skills setup — auto-activate all skills upfront so Haiku doesn't need to call activate_skill
87
- const promptFragments = [];
88
- let skillsIndex = "";
89
- if (resolved.skills.length > 0) {
90
- const ctx = {
91
- manifests: resolved.skills,
92
- skillsDir: this.skillsDir,
93
- toolRegistry: tickRegistry,
94
- promptFragments,
95
- activatedSkills: new Set(),
96
- };
97
- const activateTool = createActivateSkillTool(ctx);
98
- tickRegistry.register(activateTool);
99
- // Auto-activate all skills upfront
100
- for (const manifest of resolved.skills) {
101
- try {
102
- const result = await activateTool.execute({ skill_name: manifest.name });
103
- if (typeof result === "string" && result.startsWith("Error")) {
104
- log.error({ agent: agentName, skill: manifest.name, result }, "failed to auto-activate skill");
105
- }
106
- }
107
- catch (err) {
108
- log.error({ err, agent: agentName, skill: manifest.name }, "skill auto-activation threw");
109
- }
76
+ const spanCtx = trace.setSpan(context.active(), span);
77
+ try {
78
+ if (!isWithinActiveHours(hb.activeHours)) {
79
+ log.debug({ agent: agentName }, "outside active hours, skipping");
80
+ span.setAttribute(ATTR.SUPPRESSED, "outside_active_hours");
81
+ return undefined;
110
82
  }
111
- const activatedNames = [...ctx.activatedSkills];
112
- if (activatedNames.length > 0) {
113
- skillsIndex = "\n\nThe following skills are pre-activated: "
114
- + activatedNames.join(", ")
115
- + ".\nTheir tools are already available — you do not need to call activate_skill.";
83
+ const instructions = this.loadHeartbeatInstructions(agentName);
84
+ if (!instructions) {
85
+ log.warn({ agent: agentName }, "no HEARTBEAT.md found, skipping");
86
+ span.setAttribute(ATTR.SUPPRESSED, "no_instructions");
87
+ return undefined;
116
88
  }
89
+ return await context.with(spanCtx, async () => {
90
+ // Isolated tool registry per tick (same pattern as CronScheduler)
91
+ const sandbox = agentDef.sandbox ?? this.config.defaults.sandbox;
92
+ const memoryConfig = this.config.defaults.memory;
93
+ const tickRegistry = createBuiltinRegistry({
94
+ allowedCommands: sandbox?.allowedCommands,
95
+ allowedPaths: sandbox?.allowedPaths,
96
+ memoryConfig,
97
+ webSearch: resolveWebSearch(agentName, this.config),
98
+ });
99
+ const resolved = resolveAgent(agentName, this.config, tickRegistry, this.skillsDir);
100
+ // Skills setup — auto-activate all skills upfront so Haiku doesn't need to call activate_skill
101
+ const promptFragments = [];
102
+ let skillsIndex = "";
103
+ if (resolved.skills.length > 0) {
104
+ const ctx = {
105
+ manifests: resolved.skills,
106
+ skillsDir: this.skillsDir,
107
+ toolRegistry: tickRegistry,
108
+ promptFragments,
109
+ activatedSkills: new Set(),
110
+ };
111
+ const activateTool = createActivateSkillTool(ctx);
112
+ tickRegistry.register(activateTool);
113
+ // Heartbeat always auto-activates (Haiku can't reliably call activate_skill)
114
+ skillsIndex = await preActivateSkills(ctx, activateTool, log);
115
+ }
116
+ // Spawn wrappers
117
+ if (resolved.canSpawn.length > 0) {
118
+ registerSpawnWrappers(resolved.canSpawn, this.config, this.agentRegistry, tickRegistry, this.usageStore);
119
+ }
120
+ // Ephemeral session: no history accumulation, append-only audit log
121
+ const sessionId = `heartbeat-${agentName}`;
122
+ const { soul, session } = setupAgentSession(this.dataDir, agentName, sessionId);
123
+ // Build tick prompt with fresh instructions + timestamp
124
+ const tickPrompt = `[Heartbeat tick — ${new Date().toISOString()}]\n\n${instructions}`;
125
+ const userMsg = { role: "user", content: tickPrompt };
126
+ session.append(userMsg); // audit only
127
+ const messages = [userMsg]; // ephemeral — no history
128
+ // Model: heartbeat.model overrides agent model
129
+ const model = hb.model
130
+ ? resolveModelAlias(hb.model, this.config.models)
131
+ : resolved.model;
132
+ const systemPrompt = [soul, dateContext(), skillsIndex, ...promptFragments]
133
+ .filter(Boolean)
134
+ .join("\n\n") || undefined;
135
+ const result = await runAgentLoop(messages, {
136
+ model,
137
+ fallbacks: resolved.fallbacks,
138
+ systemPrompt,
139
+ toolRegistry: tickRegistry,
140
+ maxIterations: hb.maxIterations ?? resolved.maxIterations,
141
+ compactionThreshold: resolved.compactionThreshold,
142
+ maxToolResultSize: resolved.maxToolResultSize,
143
+ agentName,
144
+ usageStore: this.usageStore,
145
+ source: "heartbeat",
146
+ });
147
+ // Audit: append all new messages from the agent loop
148
+ for (const msg of result.messages.slice(1)) {
149
+ session.append(msg);
150
+ }
151
+ return result;
152
+ });
117
153
  }
118
- // Spawn wrappers
119
- if (resolved.canSpawn.length > 0) {
120
- registerSpawnWrappers(resolved.canSpawn, this.config, this.agentRegistry, tickRegistry, this.usageStore);
154
+ catch (err) {
155
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
156
+ span.recordException(err instanceof Error ? err : new Error(String(err)));
157
+ throw err;
121
158
  }
122
- // Ephemeral session: no history accumulation, append-only audit log
123
- const sessionId = `heartbeat-${agentName}`;
124
- const { soul, session } = setupAgentSession(this.dataDir, agentName, sessionId);
125
- // Build tick prompt with fresh instructions + timestamp
126
- const tickPrompt = `[Heartbeat tick — ${new Date().toISOString()}]\n\n${instructions}`;
127
- const userMsg = { role: "user", content: tickPrompt };
128
- session.append(userMsg); // audit only
129
- const messages = [userMsg]; // ephemeral — no history
130
- // Model: heartbeat.model overrides agent model
131
- const model = hb.model
132
- ? resolveModelAlias(hb.model, this.config.models)
133
- : resolved.model;
134
- const systemPrompt = [soul, dateContext(), skillsIndex, ...promptFragments]
135
- .filter(Boolean)
136
- .join("\n\n") || undefined;
137
- const result = await runAgentLoop(messages, {
138
- model,
139
- fallbacks: resolved.fallbacks,
140
- systemPrompt,
141
- toolRegistry: tickRegistry,
142
- maxIterations: hb.maxIterations ?? resolved.maxIterations,
143
- compactionThreshold: resolved.compactionThreshold,
144
- maxToolResultSize: resolved.maxToolResultSize,
145
- agentName,
146
- usageStore: this.usageStore,
147
- source: "heartbeat",
148
- });
149
- // Audit: append all new messages from the agent loop
150
- for (const msg of result.messages.slice(1)) {
151
- session.append(msg);
159
+ finally {
160
+ span.end();
152
161
  }
153
- return result;
154
162
  }
155
163
  start(callbacks) {
156
164
  for (const [agentName, agentDef] of Object.entries(this.config.agents)) {
@@ -162,6 +170,15 @@ export class HeartbeatRunner {
162
170
  const runTick = async () => {
163
171
  if (this.running.has(agentName)) {
164
172
  log.info({ agent: agentName }, "previous tick still running, skipping");
173
+ const tracer = getTracer("heartbeat");
174
+ const skipSpan = tracer.startSpan("source.heartbeat", {
175
+ attributes: {
176
+ [ATTR.AGENT]: agentName,
177
+ [ATTR.SOURCE]: "heartbeat",
178
+ [ATTR.SUPPRESSED]: "already_running",
179
+ },
180
+ });
181
+ skipSpan.end();
165
182
  return;
166
183
  }
167
184
  this.running.add(agentName);
@@ -7,6 +7,7 @@ export declare class AnthropicProvider implements LLMProvider {
7
7
  complete(model: string, messages: Message[], options?: CompleteOptions): Promise<LLMResponse>;
8
8
  stream(model: string, messages: Message[], options?: CompleteOptions): AsyncIterable<StreamEvent>;
9
9
  private toAnthropicMessages;
10
+ private mapUserContent;
10
11
  private fromAnthropicResponse;
11
12
  private mapStopReason;
12
13
  }
@@ -39,7 +39,7 @@ export class AnthropicProvider {
39
39
  toAnthropicMessages(messages) {
40
40
  return messages.map((msg) => {
41
41
  if (msg.role === "user")
42
- return { role: "user", content: msg.content };
42
+ return { role: "user", content: this.mapUserContent(msg.content) };
43
43
  if (msg.role === "assistant") {
44
44
  return {
45
45
  role: "assistant",
@@ -51,11 +51,20 @@ export class AnthropicProvider {
51
51
  };
52
52
  }
53
53
  if (msg.role === "tool_result") {
54
- return { role: "user", content: [{ type: "tool_result", tool_use_id: msg.tool_call_id, content: msg.content }] };
54
+ return { role: "user", content: [{ type: "tool_result", tool_use_id: msg.tool_call_id, content: this.mapUserContent(msg.content) }] };
55
55
  }
56
56
  return msg;
57
57
  });
58
58
  }
59
+ mapUserContent(content) {
60
+ if (typeof content === "string")
61
+ return content;
62
+ return content.map((block) => {
63
+ if (block.type === "text")
64
+ return { type: "text", text: block.text };
65
+ return { type: "image", source: block.source };
66
+ });
67
+ }
59
68
  fromAnthropicResponse(response) {
60
69
  const content = response.content.map((block) => {
61
70
  if (block.type === "text")
@@ -1,4 +1,6 @@
1
1
  import { createLogger } from "../logger.js";
2
+ import { getTracer, ATTR } from "../telemetry/index.js";
3
+ import { SpanStatusCode, context } from "@opentelemetry/api";
2
4
  const log = createLogger("llm");
3
5
  const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503]);
4
6
  export function isRetryableError(err) {
@@ -12,17 +14,47 @@ export function isRetryableError(err) {
12
14
  return false;
13
15
  }
14
16
  export async function completeWithFallback(primary, fallbacks, messages, options, completeFn) {
17
+ const tracer = getTracer("llm");
15
18
  const models = [primary, ...fallbacks];
16
19
  let lastError;
17
- for (const model of models) {
20
+ for (let idx = 0; idx < models.length; idx++) {
21
+ const model = models[idx];
22
+ const [provider = "unknown"] = model.split(":");
23
+ const attemptSpan = tracer.startSpan("llm.attempt", {}, context.active());
24
+ attemptSpan.setAttribute(ATTR.PROVIDER, provider);
25
+ attemptSpan.setAttribute(ATTR.MODEL, model);
18
26
  try {
19
- return await completeFn(model, messages, options);
27
+ const response = await completeFn(model, messages, options);
28
+ // Set token attributes on successful attempt
29
+ attemptSpan.setAttribute(ATTR.INPUT_TOKENS, response.usage.inputTokens);
30
+ attemptSpan.setAttribute(ATTR.OUTPUT_TOKENS, response.usage.outputTokens);
31
+ attemptSpan.setAttribute(ATTR.CACHE_CREATION_TOKENS, response.usage.cacheCreationTokens);
32
+ attemptSpan.setAttribute(ATTR.CACHE_READ_TOKENS, response.usage.cacheReadTokens);
33
+ attemptSpan.setAttribute(ATTR.STOP_REASON, response.stopReason);
34
+ if (response.latencyMs != null) {
35
+ attemptSpan.setAttribute(ATTR.LATENCY_MS, response.latencyMs);
36
+ }
37
+ attemptSpan.end();
38
+ return response;
20
39
  }
21
40
  catch (err) {
22
41
  lastError = err;
42
+ const errMsg = err instanceof Error ? err.message : String(err);
43
+ attemptSpan.setStatus({ code: SpanStatusCode.ERROR, message: errMsg });
44
+ if (err instanceof Error)
45
+ attemptSpan.recordException(err);
46
+ attemptSpan.end();
23
47
  if (!isRetryableError(err) || model === models[models.length - 1]) {
24
48
  throw err;
25
49
  }
50
+ // Record fallback span for the transition to the next model
51
+ const nextModel = models[idx + 1];
52
+ const fallbackSpan = tracer.startSpan("llm.fallback", {}, context.active());
53
+ fallbackSpan.setAttribute(ATTR.FALLBACK_ORIGINAL, model);
54
+ fallbackSpan.setAttribute(ATTR.FALLBACK_MODEL, nextModel);
55
+ fallbackSpan.setAttribute(ATTR.FALLBACK_ERROR, errMsg);
56
+ fallbackSpan.setStatus({ code: SpanStatusCode.ERROR, message: errMsg });
57
+ fallbackSpan.end();
26
58
  log.warn({ err, model }, "model failed, trying next");
27
59
  }
28
60
  }
@@ -7,6 +7,8 @@ export declare class OpenAIProvider implements LLMProvider {
7
7
  complete(model: string, messages: Message[], options?: CompleteOptions): Promise<LLMResponse>;
8
8
  stream(model: string, messages: Message[], options?: CompleteOptions): AsyncIterable<StreamEvent>;
9
9
  private toOpenAIMessages;
10
+ private mapImageBlock;
11
+ private mapUserContentToOpenAI;
10
12
  private fromOpenAIResponse;
11
13
  private mapStopReason;
12
14
  }
@@ -30,7 +30,7 @@ export class OpenAIProvider {
30
30
  result.push({ role: "system", content: systemPrompt });
31
31
  for (const msg of messages) {
32
32
  if (msg.role === "user") {
33
- result.push({ role: "user", content: msg.content });
33
+ result.push({ role: "user", content: this.mapUserContentToOpenAI(msg.content) });
34
34
  }
35
35
  else if (msg.role === "assistant") {
36
36
  const textParts = msg.content.filter((b) => b.type === "text").map((b) => b.text).join("");
@@ -45,11 +45,42 @@ export class OpenAIProvider {
45
45
  result.push(openaiMsg);
46
46
  }
47
47
  else if (msg.role === "tool_result") {
48
- result.push({ role: "tool", tool_call_id: msg.tool_call_id, content: msg.content });
48
+ if (typeof msg.content === "string") {
49
+ result.push({ role: "tool", tool_call_id: msg.tool_call_id, content: msg.content });
50
+ }
51
+ else {
52
+ const textParts = msg.content.filter((b) => b.type === "text").map((b) => b.text).join("");
53
+ const imageBlocks = msg.content.filter((b) => b.type === "image");
54
+ result.push({ role: "tool", tool_call_id: msg.tool_call_id, content: textParts });
55
+ if (imageBlocks.length) {
56
+ result.push({
57
+ role: "user",
58
+ content: [
59
+ { type: "text", text: "Image(s) from tool result:" },
60
+ ...imageBlocks.map((b) => this.mapImageBlock(b)),
61
+ ],
62
+ });
63
+ }
64
+ }
49
65
  }
50
66
  }
51
67
  return result;
52
68
  }
69
+ mapImageBlock(block) {
70
+ if (block.source.type === "base64") {
71
+ return { type: "image_url", image_url: { url: `data:${block.source.media_type};base64,${block.source.data}` } };
72
+ }
73
+ return { type: "image_url", image_url: { url: block.source.url } };
74
+ }
75
+ mapUserContentToOpenAI(content) {
76
+ if (typeof content === "string")
77
+ return content;
78
+ return content.map((block) => {
79
+ if (block.type === "text")
80
+ return { type: "text", text: block.text };
81
+ return this.mapImageBlock(block);
82
+ });
83
+ }
53
84
  fromOpenAIResponse(response) {
54
85
  const choice = response.choices[0];
55
86
  const content = [];
@@ -8,10 +8,22 @@ export interface ToolCallBlock {
8
8
  name: string;
9
9
  arguments: Record<string, unknown>;
10
10
  }
11
+ export interface ImageBlock {
12
+ type: "image";
13
+ source: {
14
+ type: "base64";
15
+ media_type: string;
16
+ data: string;
17
+ } | {
18
+ type: "url";
19
+ url: string;
20
+ };
21
+ }
22
+ export type UserContentBlock = TextBlock | ImageBlock;
11
23
  export type ContentBlock = TextBlock | ToolCallBlock;
12
24
  export interface UserMessage {
13
25
  role: "user";
14
- content: string;
26
+ content: string | UserContentBlock[];
15
27
  }
16
28
  export interface AssistantMessage {
17
29
  role: "assistant";
@@ -20,7 +32,7 @@ export interface AssistantMessage {
20
32
  export interface ToolResultMessage {
21
33
  role: "tool_result";
22
34
  tool_call_id: string;
23
- content: string;
35
+ content: string | UserContentBlock[];
24
36
  }
25
37
  export type Message = UserMessage | AssistantMessage | ToolResultMessage;
26
38
  export interface ToolDefinition {
@@ -71,3 +83,5 @@ export interface ErrorEvent {
71
83
  }
72
84
  export type StreamEvent = TextDeltaEvent | ToolCallStartEvent | ToolCallDeltaEvent | DoneEvent | ErrorEvent;
73
85
  export declare function extractText(content: ContentBlock[]): string;
86
+ /** Extract text from user content (string or UserContentBlock[]) */
87
+ export declare function extractUserText(content: string | UserContentBlock[]): string;
package/dist/llm/types.js CHANGED
@@ -4,3 +4,12 @@ export function extractText(content) {
4
4
  .map((b) => b.text)
5
5
  .join("");
6
6
  }
7
+ /** Extract text from user content (string or UserContentBlock[]) */
8
+ export function extractUserText(content) {
9
+ if (typeof content === "string")
10
+ return content;
11
+ return content
12
+ .filter((b) => b.type === "text")
13
+ .map((b) => b.text)
14
+ .join("");
15
+ }
package/dist/logger.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import pino from "pino";
2
2
  export declare function createLogger(name: string): pino.Logger<never, boolean>;
3
+ export declare function setLogLevel(level: pino.Level | "silent"): void;
package/dist/logger.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import pino from "pino";
2
+ import { trace } from "@opentelemetry/api";
2
3
  const root = pino({
3
4
  level: process.env.LOG_LEVEL ?? "info",
4
5
  transport: {
@@ -9,7 +10,17 @@ const root = pino({
9
10
  colorize: true,
10
11
  },
11
12
  },
13
+ mixin() {
14
+ const span = trace.getActiveSpan();
15
+ if (!span)
16
+ return {};
17
+ const ctx = span.spanContext();
18
+ return { trace_id: ctx.traceId, span_id: ctx.spanId };
19
+ },
12
20
  });
13
21
  export function createLogger(name) {
14
22
  return root.child({ component: name });
15
23
  }
24
+ export function setLogLevel(level) {
25
+ root.level = level;
26
+ }
@@ -0,0 +1,5 @@
1
+ export interface SanitizedImage {
2
+ base64: string;
3
+ mediaType: string;
4
+ }
5
+ export declare function sanitizeImage(buffer: Buffer, claimedMimeType?: string): Promise<SanitizedImage>;
@@ -0,0 +1,53 @@
1
+ import sharp from "sharp";
2
+ const MAX_BYTES = 20 * 1024 * 1024; // 20MB — raw upload limit (sharp will resize/compress)
3
+ const MAX_DIMENSION = 1200;
4
+ const JPEG_QUALITY = 85;
5
+ const SUPPORTED_TYPES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"]);
6
+ // Magic byte signatures
7
+ const SIGNATURES = [
8
+ [Buffer.from([0x89, 0x50, 0x4e, 0x47]), "image/png"],
9
+ [Buffer.from([0xff, 0xd8, 0xff]), "image/jpeg"],
10
+ [Buffer.from("GIF87a"), "image/gif"],
11
+ [Buffer.from("GIF89a"), "image/gif"],
12
+ [Buffer.from("RIFF"), "image/webp"], // RIFF....WEBP
13
+ ];
14
+ function sniffMimeType(buffer) {
15
+ for (const [sig, type] of SIGNATURES) {
16
+ if (buffer.length >= sig.length && buffer.subarray(0, sig.length).equals(sig)) {
17
+ // For WEBP, also check bytes 8-12 for "WEBP"
18
+ if (type === "image/webp") {
19
+ if (buffer.length >= 12 && buffer.subarray(8, 12).toString() === "WEBP")
20
+ return type;
21
+ continue;
22
+ }
23
+ return type;
24
+ }
25
+ }
26
+ return undefined;
27
+ }
28
+ export async function sanitizeImage(buffer, claimedMimeType) {
29
+ if (buffer.length > MAX_BYTES) {
30
+ throw new Error(`Image exceeds 20 MB size limit (${(buffer.length / 1024 / 1024).toFixed(1)} MB)`);
31
+ }
32
+ const sniffed = sniffMimeType(buffer);
33
+ const mediaType = sniffed ?? claimedMimeType;
34
+ if (!mediaType || !SUPPORTED_TYPES.has(mediaType)) {
35
+ throw new Error(`Unsupported image type: ${mediaType ?? "unknown"}`);
36
+ }
37
+ const meta = await sharp(buffer).metadata();
38
+ const width = meta.width ?? 0;
39
+ const height = meta.height ?? 0;
40
+ let output;
41
+ if (width > MAX_DIMENSION || height > MAX_DIMENSION) {
42
+ // Resize to fit within MAX_DIMENSION, preserving aspect ratio
43
+ // Recompress as JPEG for smaller output
44
+ output = await sharp(buffer)
45
+ .resize(MAX_DIMENSION, MAX_DIMENSION, { fit: "inside", withoutEnlargement: true })
46
+ .jpeg({ quality: JPEG_QUALITY })
47
+ .toBuffer();
48
+ return { base64: output.toString("base64"), mediaType: "image/jpeg" };
49
+ }
50
+ // No resize needed — return as-is
51
+ output = buffer;
52
+ return { base64: output.toString("base64"), mediaType };
53
+ }
@@ -1,14 +1,33 @@
1
1
  import { runAgentLoop } from "../agent/loop.js";
2
+ import { SpanStatusCode } from "@opentelemetry/api";
3
+ import { getTracer, ATTR } from "../telemetry/index.js";
2
4
  export async function spawnAgent(options) {
3
- return runAgentLoop([{ role: "user", content: options.task }], {
4
- model: options.model,
5
- fallbacks: options.fallbacks,
6
- systemPrompt: options.systemPrompt,
7
- tools: options.tools,
8
- maxIterations: options.maxIterations ?? 10,
9
- label: options.label,
10
- agentName: options.label,
11
- usageStore: options.usageStore,
12
- source: "spawn",
5
+ const tracer = getTracer("multi");
6
+ const span = tracer.startSpan("agent.spawn", {
7
+ attributes: {
8
+ [ATTR.SPAWN_CHILD]: options.label ?? "unknown",
9
+ },
13
10
  });
11
+ try {
12
+ const result = await runAgentLoop([{ role: "user", content: options.task }], {
13
+ model: options.model,
14
+ fallbacks: options.fallbacks,
15
+ systemPrompt: options.systemPrompt,
16
+ tools: options.tools,
17
+ maxIterations: options.maxIterations ?? 10,
18
+ label: options.label,
19
+ agentName: options.label,
20
+ usageStore: options.usageStore,
21
+ source: "spawn",
22
+ });
23
+ return result;
24
+ }
25
+ catch (err) {
26
+ span.setStatus({ code: SpanStatusCode.ERROR, message: String(err) });
27
+ span.recordException(err instanceof Error ? err : new Error(String(err)));
28
+ throw err;
29
+ }
30
+ finally {
31
+ span.end();
32
+ }
14
33
  }
@@ -1,5 +1,6 @@
1
1
  import { complete } from "../llm/index.js";
2
2
  import { extractText } from "../llm/types.js";
3
+ import { pruneImages } from "./prune-images.js";
3
4
  export function estimateTokens(message) {
4
5
  return Math.ceil(JSON.stringify(message).length / 4);
5
6
  }
@@ -13,6 +14,7 @@ export async function compactMessages(messages, model, tokenThreshold = 100_000)
13
14
  const splitPoint = Math.floor(messages.length / 2);
14
15
  const oldMessages = messages.slice(0, splitPoint);
15
16
  const recentMessages = messages.slice(splitPoint);
17
+ const prunedOld = pruneImages(oldMessages, 0);
16
18
  const summaryResponse = await complete(model, [{
17
19
  role: "user",
18
20
  content: "Summarize this conversation concisely. Preserve:\n" +
@@ -20,7 +22,7 @@ export async function compactMessages(messages, model, tokenThreshold = 100_000)
20
22
  "- Important decisions made\n" +
21
23
  "- Open tasks or TODOs\n" +
22
24
  "- File paths and code changes discussed\n\n" +
23
- JSON.stringify(oldMessages, null, 2),
25
+ JSON.stringify(prunedOld, null, 2),
24
26
  }]);
25
27
  const summaryText = extractText(summaryResponse.content);
26
28
  return [
@@ -0,0 +1,9 @@
1
+ import type { Message } from "../llm/types.js";
2
+ /**
3
+ * Returns a shallow copy of messages with ImageBlocks replaced by text placeholders
4
+ * in user messages older than `keepLastNTurns` user turns from the end.
5
+ * A "turn" is defined by a user message — all messages (assistant, tool_result)
6
+ * between two user messages belong to the same turn as the preceding user message.
7
+ * Does NOT mutate the original array.
8
+ */
9
+ export declare function pruneImages(messages: Message[], keepLastNTurns?: number): Message[];
@@ -0,0 +1,42 @@
1
+ const IMAGE_PLACEHOLDER = {
2
+ type: "text",
3
+ text: "[image previously shared \u2014 already processed]",
4
+ };
5
+ /**
6
+ * Returns a shallow copy of messages with ImageBlocks replaced by text placeholders
7
+ * in user messages older than `keepLastNTurns` user turns from the end.
8
+ * A "turn" is defined by a user message — all messages (assistant, tool_result)
9
+ * between two user messages belong to the same turn as the preceding user message.
10
+ * Does NOT mutate the original array.
11
+ */
12
+ export function pruneImages(messages, keepLastNTurns = 4) {
13
+ // Find the Nth user message from the end — this is the start of the kept range.
14
+ // Everything before it (older turns and their tool results) gets image-pruned.
15
+ let turnsSeen = 0;
16
+ let cutoff = -1;
17
+ for (let i = messages.length - 1; i >= 0; i--) {
18
+ if (messages[i].role === "user") {
19
+ turnsSeen++;
20
+ if (turnsSeen === keepLastNTurns) {
21
+ cutoff = i;
22
+ break;
23
+ }
24
+ }
25
+ }
26
+ if (turnsSeen < keepLastNTurns)
27
+ return messages; // fewer turns than threshold
28
+ return messages.map((msg, i) => {
29
+ if (i >= cutoff)
30
+ return msg;
31
+ if (msg.role === "user" || msg.role === "tool_result") {
32
+ if (typeof msg.content === "string")
33
+ return msg;
34
+ const hasImage = msg.content.some((b) => b.type === "image");
35
+ if (!hasImage)
36
+ return msg;
37
+ const pruned = msg.content.map((b) => b.type === "image" ? IMAGE_PLACEHOLDER : b);
38
+ return { ...msg, content: pruned };
39
+ }
40
+ return msg;
41
+ });
42
+ }
@@ -1,6 +1,7 @@
1
1
  import type { Tool } from "../tools/types.js";
2
2
  import type { ToolRegistry } from "../tools/registry.js";
3
3
  import type { SkillManifest } from "./schema.js";
4
+ import type { Logger } from "pino";
4
5
  export interface SkillActivationContext {
5
6
  manifests: SkillManifest[];
6
7
  skillsDir: string;
@@ -8,4 +9,9 @@ export interface SkillActivationContext {
8
9
  promptFragments: string[];
9
10
  activatedSkills: Set<string>;
10
11
  }
12
+ /**
13
+ * Auto-activate all skills upfront so the LLM doesn't need to call activate_skill.
14
+ * Returns the skillsIndex string to append to the system prompt.
15
+ */
16
+ export declare function preActivateSkills(ctx: SkillActivationContext, activateTool: Tool, logger?: Logger): Promise<string>;
11
17
  export declare function createActivateSkillTool(ctx: SkillActivationContext): Tool;