@cognipeer/agent-sdk 0.8.12 → 0.9.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.cjs CHANGED
@@ -434,7 +434,7 @@ function createTool({
434
434
  run: execute,
435
435
  func: execute
436
436
  };
437
- if (typeof needsApproval === "boolean") {
437
+ if (typeof needsApproval === "function" || typeof needsApproval === "boolean") {
438
438
  toolRecord.needsApproval = needsApproval;
439
439
  }
440
440
  if (approvalPrompt !== void 0) {
@@ -3483,6 +3483,29 @@ function selectPendingToolCalls(messages) {
3483
3483
  }
3484
3484
 
3485
3485
  // src/nodes/tools.ts
3486
+ function resolveNeedsApproval(tool, args) {
3487
+ const spec = tool?.needsApproval;
3488
+ if (typeof spec === "function") {
3489
+ try {
3490
+ return Boolean(spec(args));
3491
+ } catch {
3492
+ return true;
3493
+ }
3494
+ }
3495
+ return Boolean(spec);
3496
+ }
3497
+ function resolveApprovalPrompt(tool, args) {
3498
+ const prompt = tool?.approvalPrompt;
3499
+ if (typeof prompt === "function") {
3500
+ try {
3501
+ const resolved = prompt(args);
3502
+ return typeof resolved === "string" && resolved.length > 0 ? resolved : void 0;
3503
+ } catch {
3504
+ return void 0;
3505
+ }
3506
+ }
3507
+ return typeof prompt === "string" ? prompt : void 0;
3508
+ }
3486
3509
  function normalizeMaxExecutionsPerRun(value) {
3487
3510
  if (value === null || value === void 0 || value === "") return null;
3488
3511
  const parsed = Number(value);
@@ -3529,10 +3552,13 @@ function buildToolDetails(tool, toolName) {
3529
3552
  }
3530
3553
  const inputSchema = normalizeToolSchema(tool, toolName);
3531
3554
  if (inputSchema !== void 0) details.inputSchema = inputSchema;
3532
- const needsApproval = typeof tool.needsApproval === "boolean" ? tool.needsApproval : void 0;
3533
- if (needsApproval !== void 0 || tool.approvalPrompt !== void 0 || tool.approvalDefaults !== void 0) {
3555
+ const approvalSpec = tool.needsApproval;
3556
+ const needsApproval = typeof approvalSpec === "boolean" ? approvalSpec : void 0;
3557
+ const conditionalApproval = typeof approvalSpec === "function" ? true : void 0;
3558
+ if (needsApproval !== void 0 || conditionalApproval !== void 0 || tool.approvalPrompt !== void 0 || tool.approvalDefaults !== void 0) {
3534
3559
  details.approval = {
3535
3560
  required: needsApproval,
3561
+ conditional: conditionalApproval,
3536
3562
  prompt: typeof tool.approvalPrompt === "string" ? tool.approvalPrompt : void 0,
3537
3563
  defaults: tool.approvalDefaults !== void 0 ? sanitizeTracePayload(tool.approvalDefaults) : void 0
3538
3564
  };
@@ -3757,7 +3783,7 @@ function createToolsNode(initialTools, opts) {
3757
3783
  }
3758
3784
  }
3759
3785
  let approvalEntry = pendingByCallId.get(toolCallId);
3760
- const needsApproval = Boolean(tool.needsApproval);
3786
+ const needsApproval = resolveNeedsApproval(tool, args);
3761
3787
  if (needsApproval) {
3762
3788
  if (!approvalEntry) {
3763
3789
  approvalEntry = {
@@ -3767,7 +3793,7 @@ function createToolsNode(initialTools, opts) {
3767
3793
  args,
3768
3794
  status: "pending",
3769
3795
  requestedAt: (/* @__PURE__ */ new Date()).toISOString(),
3770
- metadata: tool.approvalPrompt || tool.approvalDefaults ? { prompt: tool.approvalPrompt, defaults: tool.approvalDefaults } : void 0
3796
+ metadata: resolveApprovalPrompt(tool, args) !== void 0 || tool.approvalDefaults !== void 0 ? { prompt: resolveApprovalPrompt(tool, args), defaults: tool.approvalDefaults } : void 0
3771
3797
  };
3772
3798
  pendingApprovals.push(approvalEntry);
3773
3799
  pendingByCallId.set(toolCallId, approvalEntry);
@@ -7556,6 +7582,127 @@ function createSkillTools(deps) {
7556
7582
  if (isSearchDisclosure(deps.policy)) tools.unshift(createSearchSkillsTool(deps));
7557
7583
  return tools;
7558
7584
  }
7585
+ var PREOPEN_TOOL_CALL_PREFIX = "preopen_skill_";
7586
+ var OPEN_SKILL_TOOL_NAME = "open_skill";
7587
+ function preopenToolCallId(skillKey) {
7588
+ return `${PREOPEN_TOOL_CALL_PREFIX}${skillKey.replace(/[^a-zA-Z0-9_-]/g, "_")}`;
7589
+ }
7590
+ function messagesContainCallId(messages, callId) {
7591
+ if (!Array.isArray(messages)) return false;
7592
+ return messages.some((message) => {
7593
+ const calls = message?.tool_calls;
7594
+ return Array.isArray(calls) && calls.some((call) => call?.id === callId);
7595
+ });
7596
+ }
7597
+ function serializeToolContent(result) {
7598
+ if (typeof result === "string") return result;
7599
+ try {
7600
+ return JSON.stringify(result);
7601
+ } catch {
7602
+ return String(result);
7603
+ }
7604
+ }
7605
+ async function preopenSkills(input) {
7606
+ const empty = { messages: [], toolHistory: [], openedKeys: [], skipped: [] };
7607
+ const requested = [...new Set(
7608
+ (input.skillKeys ?? []).map((key) => String(key ?? "").trim()).filter(Boolean)
7609
+ )];
7610
+ if (requested.length === 0) return empty;
7611
+ const openSkillTool = input.tools.find((tool) => tool?.name === OPEN_SKILL_TOOL_NAME);
7612
+ if (!openSkillTool) {
7613
+ return { ...empty, skipped: requested.map((skillKey) => ({ skillKey, reason: "open_skill tool is not wired" })) };
7614
+ }
7615
+ const alreadyOpen = new Set(input.alreadyOpenedKeys ?? []);
7616
+ const toolCalls = [];
7617
+ const toolMessages = [];
7618
+ const toolHistory = [];
7619
+ const openedKeys = [];
7620
+ const skipped = [];
7621
+ for (const skillKey of requested) {
7622
+ const toolCallId = preopenToolCallId(skillKey);
7623
+ if (alreadyOpen.has(skillKey) || messagesContainCallId(input.existingMessages, toolCallId)) {
7624
+ skipped.push({ skillKey, reason: "already open" });
7625
+ continue;
7626
+ }
7627
+ const args = { skillKey };
7628
+ input.onEvent?.({ type: "tool_call", phase: "start", name: OPEN_SKILL_TOOL_NAME, id: toolCallId, args });
7629
+ const start = Date.now();
7630
+ let result;
7631
+ try {
7632
+ result = await openSkillTool.invoke(args);
7633
+ } catch (error) {
7634
+ const message = error instanceof Error ? error.message : "open_skill failed";
7635
+ input.onEvent?.({
7636
+ type: "tool_call",
7637
+ phase: "error",
7638
+ name: OPEN_SKILL_TOOL_NAME,
7639
+ id: toolCallId,
7640
+ args,
7641
+ error: { message },
7642
+ durationMs: Date.now() - start
7643
+ });
7644
+ skipped.push({ skillKey, reason: message });
7645
+ continue;
7646
+ }
7647
+ const durationMs = Date.now() - start;
7648
+ if (result && typeof result === "object" && typeof result.error === "string") {
7649
+ input.onEvent?.({
7650
+ type: "tool_call",
7651
+ phase: "skipped",
7652
+ name: OPEN_SKILL_TOOL_NAME,
7653
+ id: toolCallId,
7654
+ args,
7655
+ error: { message: result.error },
7656
+ durationMs
7657
+ });
7658
+ skipped.push({ skillKey, reason: result.error });
7659
+ continue;
7660
+ }
7661
+ if (result && typeof result === "object") delete result.__runtimeToolsDelta;
7662
+ toolCalls.push({
7663
+ id: toolCallId,
7664
+ type: "function",
7665
+ function: { name: OPEN_SKILL_TOOL_NAME, arguments: JSON.stringify(args) }
7666
+ });
7667
+ toolMessages.push({
7668
+ role: "tool",
7669
+ name: OPEN_SKILL_TOOL_NAME,
7670
+ tool_call_id: toolCallId,
7671
+ content: serializeToolContent(result)
7672
+ });
7673
+ toolHistory.push({
7674
+ executionId: nanoid.nanoid(),
7675
+ toolName: OPEN_SKILL_TOOL_NAME,
7676
+ args,
7677
+ output: result,
7678
+ rawOutput: result,
7679
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
7680
+ tool_call_id: toolCallId,
7681
+ summarized: false,
7682
+ retentionPolicy: "keep_full",
7683
+ status: "success"
7684
+ });
7685
+ openedKeys.push(skillKey);
7686
+ input.onEvent?.({
7687
+ type: "tool_call",
7688
+ phase: "success",
7689
+ name: OPEN_SKILL_TOOL_NAME,
7690
+ id: toolCallId,
7691
+ args,
7692
+ result,
7693
+ durationMs
7694
+ });
7695
+ }
7696
+ if (toolCalls.length === 0) {
7697
+ return { ...empty, skipped };
7698
+ }
7699
+ const assistantMessage = {
7700
+ role: "assistant",
7701
+ content: `Opening the ${openedKeys.join(", ")} skill${openedKeys.length > 1 ? "s" : ""} required for this task.`,
7702
+ tool_calls: toolCalls
7703
+ };
7704
+ return { messages: [assistantMessage, ...toolMessages], toolHistory, openedKeys, skipped };
7705
+ }
7559
7706
 
7560
7707
  // src/smart/subagents/types.ts
7561
7708
  var DEFAULT_SUBAGENT_POLICY = {
@@ -8262,7 +8409,25 @@ function createSmartAgent(opts) {
8262
8409
  const disclosureBlock = [skillBlock, subagentBlock].filter(Boolean).join("\n\n") || void 0;
8263
8410
  const alreadyHasSystem = Array.isArray(input.messages) && input.messages[0]?.role === "system";
8264
8411
  const seedMessages = alreadyHasSystem ? [...input.messages || []] : [systemMessage2(disclosureBlock), ...input.messages || []];
8265
- let state = syncRuntimeTools(await syncMemory({ ...input, messages: seedMessages }), seedMessages, toolSet);
8412
+ let seedToolHistory = input.toolHistory;
8413
+ if (skillsEnabled && toolSet.skillRegistryRef) {
8414
+ const preopenKeys = config?.preopenedSkills ?? [];
8415
+ if (preopenKeys.length > 0) {
8416
+ const preopened = await preopenSkills({
8417
+ skillKeys: preopenKeys,
8418
+ tools: toolSet.toolsWithoutRecovery,
8419
+ alreadyOpenedKeys: toolSet.skillRegistryRef.openedSkillKeys,
8420
+ existingMessages: seedMessages,
8421
+ onEvent: config?.onEvent ?? opts.onEvent
8422
+ });
8423
+ if (preopened.messages.length > 0) {
8424
+ seedMessages.push(...preopened.messages);
8425
+ seedToolHistory = [...seedToolHistory ?? [], ...preopened.toolHistory];
8426
+ stateRef.toolHistory = seedToolHistory;
8427
+ }
8428
+ }
8429
+ }
8430
+ let state = syncRuntimeTools(await syncMemory({ ...input, messages: seedMessages, toolHistory: seedToolHistory }), seedMessages, toolSet);
8266
8431
  let lastResult = null;
8267
8432
  let rawMessages = [...seedMessages];
8268
8433
  const effectiveMaxToolCalls = config?.limits?.maxToolCalls ?? resolved.limits.maxToolCalls ?? 10;
@@ -11544,6 +11709,7 @@ exports.InMemoryMemoryStore = InMemoryMemoryStore;
11544
11709
  exports.NativeJsonSchemaStrategy = NativeJsonSchemaStrategy;
11545
11710
  exports.OpenAICompatibleProvider = OpenAICompatibleProvider;
11546
11711
  exports.OpenAIProvider = OpenAIProvider;
11712
+ exports.PREOPEN_TOOL_CALL_PREFIX = PREOPEN_TOOL_CALL_PREFIX;
11547
11713
  exports.ProviderError = ProviderError;
11548
11714
  exports.SMALL_TIER_SKILL_POLICY = SMALL_TIER_SKILL_POLICY;
11549
11715
  exports.StructuredOutputManager = StructuredOutputManager;
@@ -11630,6 +11796,8 @@ exports.loadSkillsFromDisk = loadSkillsFromDisk;
11630
11796
  exports.normalizeSmartAgentOptions = normalizeSmartAgentOptions;
11631
11797
  exports.otlpSink = otlpSink;
11632
11798
  exports.parseSkillFrontmatter = parseSkillFrontmatter;
11799
+ exports.preopenSkills = preopenSkills;
11800
+ exports.preopenToolCallId = preopenToolCallId;
11633
11801
  exports.readMemoryFacts = readMemoryFacts;
11634
11802
  exports.regexRule = regexRule;
11635
11803
  exports.resolveAvailableSkills = resolveAvailableSkills;