@cognipeer/agent-sdk 0.8.12 → 0.9.0

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.cjs CHANGED
@@ -7556,6 +7556,127 @@ function createSkillTools(deps) {
7556
7556
  if (isSearchDisclosure(deps.policy)) tools.unshift(createSearchSkillsTool(deps));
7557
7557
  return tools;
7558
7558
  }
7559
+ var PREOPEN_TOOL_CALL_PREFIX = "preopen_skill_";
7560
+ var OPEN_SKILL_TOOL_NAME = "open_skill";
7561
+ function preopenToolCallId(skillKey) {
7562
+ return `${PREOPEN_TOOL_CALL_PREFIX}${skillKey.replace(/[^a-zA-Z0-9_-]/g, "_")}`;
7563
+ }
7564
+ function messagesContainCallId(messages, callId) {
7565
+ if (!Array.isArray(messages)) return false;
7566
+ return messages.some((message) => {
7567
+ const calls = message?.tool_calls;
7568
+ return Array.isArray(calls) && calls.some((call) => call?.id === callId);
7569
+ });
7570
+ }
7571
+ function serializeToolContent(result) {
7572
+ if (typeof result === "string") return result;
7573
+ try {
7574
+ return JSON.stringify(result);
7575
+ } catch {
7576
+ return String(result);
7577
+ }
7578
+ }
7579
+ async function preopenSkills(input) {
7580
+ const empty = { messages: [], toolHistory: [], openedKeys: [], skipped: [] };
7581
+ const requested = [...new Set(
7582
+ (input.skillKeys ?? []).map((key) => String(key ?? "").trim()).filter(Boolean)
7583
+ )];
7584
+ if (requested.length === 0) return empty;
7585
+ const openSkillTool = input.tools.find((tool) => tool?.name === OPEN_SKILL_TOOL_NAME);
7586
+ if (!openSkillTool) {
7587
+ return { ...empty, skipped: requested.map((skillKey) => ({ skillKey, reason: "open_skill tool is not wired" })) };
7588
+ }
7589
+ const alreadyOpen = new Set(input.alreadyOpenedKeys ?? []);
7590
+ const toolCalls = [];
7591
+ const toolMessages = [];
7592
+ const toolHistory = [];
7593
+ const openedKeys = [];
7594
+ const skipped = [];
7595
+ for (const skillKey of requested) {
7596
+ const toolCallId = preopenToolCallId(skillKey);
7597
+ if (alreadyOpen.has(skillKey) || messagesContainCallId(input.existingMessages, toolCallId)) {
7598
+ skipped.push({ skillKey, reason: "already open" });
7599
+ continue;
7600
+ }
7601
+ const args = { skillKey };
7602
+ input.onEvent?.({ type: "tool_call", phase: "start", name: OPEN_SKILL_TOOL_NAME, id: toolCallId, args });
7603
+ const start = Date.now();
7604
+ let result;
7605
+ try {
7606
+ result = await openSkillTool.invoke(args);
7607
+ } catch (error) {
7608
+ const message = error instanceof Error ? error.message : "open_skill failed";
7609
+ input.onEvent?.({
7610
+ type: "tool_call",
7611
+ phase: "error",
7612
+ name: OPEN_SKILL_TOOL_NAME,
7613
+ id: toolCallId,
7614
+ args,
7615
+ error: { message },
7616
+ durationMs: Date.now() - start
7617
+ });
7618
+ skipped.push({ skillKey, reason: message });
7619
+ continue;
7620
+ }
7621
+ const durationMs = Date.now() - start;
7622
+ if (result && typeof result === "object" && typeof result.error === "string") {
7623
+ input.onEvent?.({
7624
+ type: "tool_call",
7625
+ phase: "skipped",
7626
+ name: OPEN_SKILL_TOOL_NAME,
7627
+ id: toolCallId,
7628
+ args,
7629
+ error: { message: result.error },
7630
+ durationMs
7631
+ });
7632
+ skipped.push({ skillKey, reason: result.error });
7633
+ continue;
7634
+ }
7635
+ if (result && typeof result === "object") delete result.__runtimeToolsDelta;
7636
+ toolCalls.push({
7637
+ id: toolCallId,
7638
+ type: "function",
7639
+ function: { name: OPEN_SKILL_TOOL_NAME, arguments: JSON.stringify(args) }
7640
+ });
7641
+ toolMessages.push({
7642
+ role: "tool",
7643
+ name: OPEN_SKILL_TOOL_NAME,
7644
+ tool_call_id: toolCallId,
7645
+ content: serializeToolContent(result)
7646
+ });
7647
+ toolHistory.push({
7648
+ executionId: nanoid.nanoid(),
7649
+ toolName: OPEN_SKILL_TOOL_NAME,
7650
+ args,
7651
+ output: result,
7652
+ rawOutput: result,
7653
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
7654
+ tool_call_id: toolCallId,
7655
+ summarized: false,
7656
+ retentionPolicy: "keep_full",
7657
+ status: "success"
7658
+ });
7659
+ openedKeys.push(skillKey);
7660
+ input.onEvent?.({
7661
+ type: "tool_call",
7662
+ phase: "success",
7663
+ name: OPEN_SKILL_TOOL_NAME,
7664
+ id: toolCallId,
7665
+ args,
7666
+ result,
7667
+ durationMs
7668
+ });
7669
+ }
7670
+ if (toolCalls.length === 0) {
7671
+ return { ...empty, skipped };
7672
+ }
7673
+ const assistantMessage = {
7674
+ role: "assistant",
7675
+ content: `Opening the ${openedKeys.join(", ")} skill${openedKeys.length > 1 ? "s" : ""} required for this task.`,
7676
+ tool_calls: toolCalls
7677
+ };
7678
+ return { messages: [assistantMessage, ...toolMessages], toolHistory, openedKeys, skipped };
7679
+ }
7559
7680
 
7560
7681
  // src/smart/subagents/types.ts
7561
7682
  var DEFAULT_SUBAGENT_POLICY = {
@@ -8262,7 +8383,25 @@ function createSmartAgent(opts) {
8262
8383
  const disclosureBlock = [skillBlock, subagentBlock].filter(Boolean).join("\n\n") || void 0;
8263
8384
  const alreadyHasSystem = Array.isArray(input.messages) && input.messages[0]?.role === "system";
8264
8385
  const seedMessages = alreadyHasSystem ? [...input.messages || []] : [systemMessage2(disclosureBlock), ...input.messages || []];
8265
- let state = syncRuntimeTools(await syncMemory({ ...input, messages: seedMessages }), seedMessages, toolSet);
8386
+ let seedToolHistory = input.toolHistory;
8387
+ if (skillsEnabled && toolSet.skillRegistryRef) {
8388
+ const preopenKeys = config?.preopenedSkills ?? [];
8389
+ if (preopenKeys.length > 0) {
8390
+ const preopened = await preopenSkills({
8391
+ skillKeys: preopenKeys,
8392
+ tools: toolSet.toolsWithoutRecovery,
8393
+ alreadyOpenedKeys: toolSet.skillRegistryRef.openedSkillKeys,
8394
+ existingMessages: seedMessages,
8395
+ onEvent: config?.onEvent ?? opts.onEvent
8396
+ });
8397
+ if (preopened.messages.length > 0) {
8398
+ seedMessages.push(...preopened.messages);
8399
+ seedToolHistory = [...seedToolHistory ?? [], ...preopened.toolHistory];
8400
+ stateRef.toolHistory = seedToolHistory;
8401
+ }
8402
+ }
8403
+ }
8404
+ let state = syncRuntimeTools(await syncMemory({ ...input, messages: seedMessages, toolHistory: seedToolHistory }), seedMessages, toolSet);
8266
8405
  let lastResult = null;
8267
8406
  let rawMessages = [...seedMessages];
8268
8407
  const effectiveMaxToolCalls = config?.limits?.maxToolCalls ?? resolved.limits.maxToolCalls ?? 10;
@@ -11544,6 +11683,7 @@ exports.InMemoryMemoryStore = InMemoryMemoryStore;
11544
11683
  exports.NativeJsonSchemaStrategy = NativeJsonSchemaStrategy;
11545
11684
  exports.OpenAICompatibleProvider = OpenAICompatibleProvider;
11546
11685
  exports.OpenAIProvider = OpenAIProvider;
11686
+ exports.PREOPEN_TOOL_CALL_PREFIX = PREOPEN_TOOL_CALL_PREFIX;
11547
11687
  exports.ProviderError = ProviderError;
11548
11688
  exports.SMALL_TIER_SKILL_POLICY = SMALL_TIER_SKILL_POLICY;
11549
11689
  exports.StructuredOutputManager = StructuredOutputManager;
@@ -11630,6 +11770,8 @@ exports.loadSkillsFromDisk = loadSkillsFromDisk;
11630
11770
  exports.normalizeSmartAgentOptions = normalizeSmartAgentOptions;
11631
11771
  exports.otlpSink = otlpSink;
11632
11772
  exports.parseSkillFrontmatter = parseSkillFrontmatter;
11773
+ exports.preopenSkills = preopenSkills;
11774
+ exports.preopenToolCallId = preopenToolCallId;
11633
11775
  exports.readMemoryFacts = readMemoryFacts;
11634
11776
  exports.regexRule = regexRule;
11635
11777
  exports.resolveAvailableSkills = resolveAvailableSkills;