@cuylabs/agent-core 0.6.0 → 0.7.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.
Files changed (57) hide show
  1. package/README.md +5 -1
  2. package/dist/{builder-BKkipazh.d.ts → builder-BRvqCcIk.d.ts} +2 -2
  3. package/dist/{resolver-DOfZ-xuk.d.ts → capability-resolver-CgRGsWVX.d.ts} +1 -1
  4. package/dist/{chunk-3C4VKG4P.js → chunk-3HNO5SVI.js} +273 -807
  5. package/dist/chunk-5K7AQVOU.js +619 -0
  6. package/dist/{chunk-QAQADS4X.js → chunk-BNSHUWCV.js} +1 -0
  7. package/dist/{chunk-O2ZCFQL6.js → chunk-CDTV2UYU.js} +86 -1
  8. package/dist/chunk-IEFIQENH.js +73 -0
  9. package/dist/chunk-N7P4PN3O.js +84 -0
  10. package/dist/{chunk-QWFMX226.js → chunk-QGOGIP7T.js} +148 -15
  11. package/dist/chunk-VNQBHPCT.js +398 -0
  12. package/dist/{chunk-X635CM2F.js → chunk-ZPMACVZK.js} +1 -1
  13. package/dist/context/index.js +1 -1
  14. package/dist/host/index.d.ts +45 -0
  15. package/dist/host/index.js +8 -0
  16. package/dist/{index-DZQJD_hp.d.ts → index-C33hlD6H.d.ts} +12 -7
  17. package/dist/{index-ipP3_ztp.d.ts → index-CfBGYrpd.d.ts} +121 -2
  18. package/dist/index.d.ts +107 -126
  19. package/dist/index.js +321 -601
  20. package/dist/inference/index.d.ts +59 -0
  21. package/dist/inference/index.js +25 -0
  22. package/dist/middleware/index.d.ts +7 -4
  23. package/dist/middleware/index.js +5 -3
  24. package/dist/models/index.d.ts +104 -2
  25. package/dist/models/index.js +40 -6
  26. package/dist/prompt/index.d.ts +9 -6
  27. package/dist/reasoning/index.d.ts +54 -8
  28. package/dist/reasoning/index.js +2 -3
  29. package/dist/{registry-CuRWWtcT.d.ts → registry-BDLIHOQB.d.ts} +1 -1
  30. package/dist/{runner-G1wxEgac.d.ts → runner-DSKaEz3z.d.ts} +35 -8
  31. package/dist/runtime/index.d.ts +41 -7
  32. package/dist/runtime/index.js +15 -6
  33. package/dist/scope/index.d.ts +10 -0
  34. package/dist/scope/index.js +14 -0
  35. package/dist/{session-manager-Uawm2Le7.d.ts → session-manager-B_CWGTsl.d.ts} +1 -1
  36. package/dist/skill/index.d.ts +7 -5
  37. package/dist/storage/index.d.ts +2 -2
  38. package/dist/sub-agent/index.d.ts +12 -8
  39. package/dist/tool/index.d.ts +7 -4
  40. package/dist/tool/index.js +4 -3
  41. package/dist/{tool-pFAnJc5Y.d.ts → tool-Db1Ue-1U.d.ts} +1 -1
  42. package/dist/{tool-DYp6-cC3.d.ts → tool-HUtkiVBx.d.ts} +5 -99
  43. package/dist/tracking/index.d.ts +3 -1
  44. package/dist/types-9jGQUjqW.d.ts +29 -0
  45. package/dist/types-CHiPh8U2.d.ts +100 -0
  46. package/dist/types-CqDZTh4d.d.ts +335 -0
  47. package/dist/types-FRpzzg_9.d.ts +355 -0
  48. package/package.json +19 -8
  49. package/dist/capabilities/index.d.ts +0 -97
  50. package/dist/capabilities/index.js +0 -46
  51. package/dist/chunk-6TDTQJ4P.js +0 -116
  52. package/dist/chunk-DWYX7ASF.js +0 -26
  53. package/dist/chunk-FG4MD5MU.js +0 -54
  54. package/dist/config-D2xeGEHK.d.ts +0 -52
  55. package/dist/identifiers-BLUxFqV_.d.ts +0 -12
  56. package/dist/network-D76DS5ot.d.ts +0 -5
  57. package/dist/types-BWo810L_.d.ts +0 -648
@@ -752,6 +752,90 @@ async function createAndRegisterProvider(spanProcessor, serviceName) {
752
752
  return provider;
753
753
  }
754
754
 
755
+ // src/middleware/prompt-cache/cache.ts
756
+ var MAX_ANTHROPIC_BREAKPOINTS = 4;
757
+ var DEFAULT_TTL = "5m";
758
+ var DEFAULT_MESSAGE_BREAKPOINTS = 1;
759
+ function isAnthropicModel(model) {
760
+ if (typeof model === "object" && model !== null) {
761
+ if ("provider" in model && typeof model.provider === "string") {
762
+ if (model.provider.toLowerCase().startsWith("anthropic")) return true;
763
+ }
764
+ if ("modelId" in model && typeof model.modelId === "string") {
765
+ if (/claude/i.test(model.modelId)) return true;
766
+ }
767
+ }
768
+ return false;
769
+ }
770
+ function buildCacheControl(ttl) {
771
+ return {
772
+ anthropic: {
773
+ cacheControl: { type: "ephemeral", ttl }
774
+ }
775
+ };
776
+ }
777
+ function buildCachedSystemMessages(system, ttl) {
778
+ const filtered = system.filter(Boolean);
779
+ if (filtered.length === 0) return [];
780
+ return filtered.map((content, i) => {
781
+ const isLast = i === filtered.length - 1;
782
+ const msg = {
783
+ role: "system",
784
+ content
785
+ };
786
+ if (isLast) {
787
+ msg.providerOptions = buildCacheControl(ttl);
788
+ }
789
+ return msg;
790
+ });
791
+ }
792
+ function findMessageBreakpointIndices(messages, count) {
793
+ if (count <= 0 || messages.length < 2) return [];
794
+ const indices = [];
795
+ for (let i = messages.length - 2; i >= 0 && indices.length < count; i--) {
796
+ const msg = messages[i];
797
+ if (msg.role === "user" || msg.role === "assistant") {
798
+ indices.push(i);
799
+ }
800
+ }
801
+ return indices;
802
+ }
803
+ function promptCacheMiddleware(config) {
804
+ const ttl = config?.ttl ?? DEFAULT_TTL;
805
+ const maxMsgBreakpoints = Math.min(
806
+ config?.messageBreakpoints ?? DEFAULT_MESSAGE_BREAKPOINTS,
807
+ MAX_ANTHROPIC_BREAKPOINTS - 1
808
+ // Reserve 1 for system
809
+ );
810
+ return {
811
+ name: "prompt-cache",
812
+ model: {
813
+ async input(input, _ctx) {
814
+ if (!isAnthropicModel(input.model)) return;
815
+ const hasSystem = input.system.some((s) => s.length > 0);
816
+ if (hasSystem) {
817
+ input.systemMessages = buildCachedSystemMessages(input.system, ttl);
818
+ }
819
+ if (maxMsgBreakpoints > 0) {
820
+ const breakpointIndices = findMessageBreakpointIndices(
821
+ input.messages,
822
+ maxMsgBreakpoints
823
+ );
824
+ const cacheControl = buildCacheControl(ttl);
825
+ for (const idx of breakpointIndices) {
826
+ const msg = input.messages[idx];
827
+ msg.providerOptions = {
828
+ ...msg.providerOptions,
829
+ ...cacheControl
830
+ };
831
+ }
832
+ }
833
+ return input;
834
+ }
835
+ }
836
+ };
837
+ }
838
+
755
839
  export {
756
840
  MiddlewareRunner,
757
841
  otelMiddleware,
@@ -760,5 +844,6 @@ export {
760
844
  ApprovalDeniedError,
761
845
  ApprovalTimeoutError,
762
846
  createApprovalHandler,
763
- approvalMiddleware
847
+ approvalMiddleware,
848
+ promptCacheMiddleware
764
849
  };
@@ -0,0 +1,73 @@
1
+ import {
2
+ snapshotScope,
3
+ withinScope
4
+ } from "./chunk-N7P4PN3O.js";
5
+ import {
6
+ extractFilePathsFromArgs,
7
+ shouldCaptureBaseline
8
+ } from "./chunk-VEKUXUVF.js";
9
+
10
+ // src/tool/executor.ts
11
+ async function executeAgentToolCall(options) {
12
+ return withinScope(
13
+ {
14
+ kind: "tool",
15
+ name: "tool-call",
16
+ sessionId: options.sessionID,
17
+ attributes: {
18
+ toolName: options.toolName,
19
+ messageId: options.messageID,
20
+ agent: options.agent ?? "default"
21
+ }
22
+ },
23
+ async () => {
24
+ const initialized = await options.tool.init({ cwd: options.cwd });
25
+ const ctx = {
26
+ cwd: options.cwd,
27
+ abort: options.abort,
28
+ sessionID: options.sessionID,
29
+ messageID: options.messageID,
30
+ agent: options.agent ?? "default",
31
+ scope: snapshotScope(),
32
+ ...options.host ? { host: options.host } : {},
33
+ ...options.turnTracker ? { turnTracker: options.turnTracker } : {}
34
+ };
35
+ if (options.middleware?.hasMiddleware) {
36
+ const decision = await options.middleware.runBeforeToolCall(
37
+ options.toolName,
38
+ options.params,
39
+ ctx
40
+ );
41
+ if (decision.action === "deny") {
42
+ return {
43
+ output: decision.reason ?? `Tool call denied: ${options.toolName}`
44
+ };
45
+ }
46
+ }
47
+ if (options.turnTracker && initialized.fileOps && shouldCaptureBaseline(initialized.fileOps)) {
48
+ const paths = extractFilePathsFromArgs(
49
+ options.params,
50
+ initialized.fileOps
51
+ );
52
+ for (const path of paths) {
53
+ await options.turnTracker.beforeWrite(path);
54
+ }
55
+ }
56
+ const result = await initialized.execute(options.params, ctx);
57
+ if (options.middleware?.hasMiddleware) {
58
+ const transformed = await options.middleware.runAfterToolCall(
59
+ options.toolName,
60
+ options.params,
61
+ result,
62
+ ctx
63
+ );
64
+ return { output: transformed.output };
65
+ }
66
+ return { output: result.output };
67
+ }
68
+ );
69
+ }
70
+
71
+ export {
72
+ executeAgentToolCall
73
+ };
@@ -0,0 +1,84 @@
1
+ // src/scope/store.ts
2
+ import { AsyncLocalStorage } from "async_hooks";
3
+ import { randomUUID } from "crypto";
4
+ var scopeStore = new AsyncLocalStorage();
5
+ function cloneAttributes(attributes) {
6
+ return { ...attributes };
7
+ }
8
+ function cloneScope(scope) {
9
+ return {
10
+ ...scope,
11
+ attributes: cloneAttributes(scope.attributes)
12
+ };
13
+ }
14
+ function getStoredScope() {
15
+ return scopeStore.getStore();
16
+ }
17
+ function buildScope(options, current) {
18
+ const parent = options.parent === void 0 ? current : options.parent ?? void 0;
19
+ const id = options.id ?? randomUUID();
20
+ return {
21
+ id,
22
+ rootId: parent?.rootId ?? id,
23
+ kind: options.kind,
24
+ name: options.name,
25
+ parentId: parent?.id,
26
+ depth: (parent?.depth ?? -1) + 1,
27
+ startedAt: (/* @__PURE__ */ new Date()).toISOString(),
28
+ sessionId: options.sessionId ?? parent?.sessionId,
29
+ taskId: options.taskId ?? parent?.taskId,
30
+ step: options.step ?? parent?.step,
31
+ attributes: cloneAttributes(options.attributes ?? {})
32
+ };
33
+ }
34
+ function runWithScope(scope, fn) {
35
+ return Promise.resolve(scopeStore.run(scope, fn));
36
+ }
37
+
38
+ // src/scope/run.ts
39
+ function currentScope() {
40
+ const scope = getStoredScope();
41
+ return scope ? cloneScope(scope) : void 0;
42
+ }
43
+ function snapshotScope(scope = getStoredScope()) {
44
+ return scope ? cloneScope(scope) : void 0;
45
+ }
46
+ function createScope(options) {
47
+ return buildScope(options, getStoredScope());
48
+ }
49
+ function withinScope(options, fn) {
50
+ return runWithScope(buildScope(options, getStoredScope()), fn);
51
+ }
52
+ function restoreScope(snapshot, fn) {
53
+ if (!snapshot) {
54
+ return Promise.resolve(fn());
55
+ }
56
+ return runWithScope(cloneScope(snapshot), fn);
57
+ }
58
+ async function* streamWithinScope(options, iterable) {
59
+ const scope = buildScope(options, getStoredScope());
60
+ const iterator = await runWithScope(scope, () => iterable[Symbol.asyncIterator]());
61
+ try {
62
+ while (true) {
63
+ const next = await runWithScope(scope, () => iterator.next());
64
+ if (next.done) {
65
+ return next.value;
66
+ }
67
+ yield next.value;
68
+ }
69
+ } finally {
70
+ const returnFn = iterator.return?.bind(iterator);
71
+ if (returnFn) {
72
+ await runWithScope(scope, () => returnFn(void 0));
73
+ }
74
+ }
75
+ }
76
+
77
+ export {
78
+ currentScope,
79
+ snapshotScope,
80
+ createScope,
81
+ withinScope,
82
+ restoreScope,
83
+ streamWithinScope
84
+ };
@@ -1,9 +1,139 @@
1
- import {
2
- getModelId,
3
- getProviderId
4
- } from "./chunk-DWYX7ASF.js";
1
+ // src/models/identifiers.ts
2
+ function getModelId(model) {
3
+ if (typeof model === "string") return model;
4
+ if (typeof model === "object" && model !== null && "modelId" in model) {
5
+ return String(model.modelId);
6
+ }
7
+ return String(model);
8
+ }
9
+ function getProviderId(model) {
10
+ if (typeof model === "string") {
11
+ if (model.includes("/")) {
12
+ return model.split("/")[0];
13
+ }
14
+ return void 0;
15
+ }
16
+ if (typeof model === "object" && model !== null && "provider" in model) {
17
+ const provider = String(model.provider);
18
+ return provider.split(".")[0];
19
+ }
20
+ return void 0;
21
+ }
22
+
23
+ // src/models/resolver.ts
24
+ function parseKey(input) {
25
+ const [engineId, ...rest] = input.split("/");
26
+ if (!engineId || rest.length === 0) return null;
27
+ return { engineId, modelId: rest.join("/") };
28
+ }
29
+ function mergeSettings(base, override) {
30
+ return {
31
+ apiKey: override?.apiKey ?? base?.apiKey,
32
+ baseUrl: override?.baseUrl ?? base?.baseUrl,
33
+ headers: {
34
+ ...base?.headers ?? {},
35
+ ...override?.headers ?? {}
36
+ },
37
+ extra: {
38
+ ...base?.extra ?? {},
39
+ ...override?.extra ?? {}
40
+ }
41
+ };
42
+ }
43
+ function settingsKey(settings, adapter, engineId) {
44
+ return JSON.stringify({
45
+ engineId,
46
+ adapter,
47
+ apiKey: settings.apiKey ?? "",
48
+ baseUrl: settings.baseUrl ?? "",
49
+ headers: settings.headers ?? {},
50
+ extra: settings.extra ?? {}
51
+ });
52
+ }
53
+ function buildOptions(settings) {
54
+ const opts = { ...settings.extra ?? {} };
55
+ if (settings.apiKey) opts.apiKey = settings.apiKey;
56
+ if (settings.baseUrl) opts.baseURL = settings.baseUrl;
57
+ if (settings.headers && Object.keys(settings.headers).length > 0) opts.headers = settings.headers;
58
+ return opts;
59
+ }
60
+ async function createFactory(adapter, settings) {
61
+ const asModel = (m) => m;
62
+ const opts = buildOptions(settings);
63
+ switch (adapter) {
64
+ case "openai": {
65
+ const { createOpenAI } = await import("@ai-sdk/openai").catch(() => {
66
+ throw new Error(
67
+ `Provider "@ai-sdk/openai" is required for the "openai" adapter. Install it with: pnpm add @ai-sdk/openai`
68
+ );
69
+ });
70
+ const provider = createOpenAI(opts);
71
+ return (modelId) => provider.languageModel(modelId);
72
+ }
73
+ case "anthropic": {
74
+ const { createAnthropic } = await import("@ai-sdk/anthropic").catch(() => {
75
+ throw new Error(
76
+ `Provider "@ai-sdk/anthropic" is required for the "anthropic" adapter. Install it with: pnpm add @ai-sdk/anthropic`
77
+ );
78
+ });
79
+ const provider = createAnthropic(opts);
80
+ return (modelId) => provider.languageModel(modelId);
81
+ }
82
+ case "google": {
83
+ const { createGoogleGenerativeAI } = await import("@ai-sdk/google").catch(() => {
84
+ throw new Error(
85
+ `Provider "@ai-sdk/google" is required for the "google" adapter. Install it with: pnpm add @ai-sdk/google`
86
+ );
87
+ });
88
+ const provider = createGoogleGenerativeAI(opts);
89
+ return (modelId) => asModel(provider.languageModel(modelId));
90
+ }
91
+ case "openai-compatible": {
92
+ const { createOpenAICompatible } = await import("@ai-sdk/openai-compatible").catch(() => {
93
+ throw new Error(
94
+ `Provider "@ai-sdk/openai-compatible" is required for the "openai-compatible" adapter. Install it with: pnpm add @ai-sdk/openai-compatible`
95
+ );
96
+ });
97
+ const provider = createOpenAICompatible({
98
+ name: opts.name ?? "custom",
99
+ baseURL: opts.baseURL ?? "",
100
+ ...opts.apiKey ? { apiKey: opts.apiKey } : {},
101
+ ...opts.headers ? { headers: opts.headers } : {}
102
+ });
103
+ return (modelId) => provider.languageModel(modelId);
104
+ }
105
+ default:
106
+ throw new Error(`No factory registered for adapter: ${adapter}`);
107
+ }
108
+ }
109
+ function createResolver(directory) {
110
+ const factoryCache = /* @__PURE__ */ new Map();
111
+ return async (key) => {
112
+ const parsed = parseKey(key);
113
+ const entry = parsed ? void 0 : directory.entries?.[key];
114
+ const engineId = parsed?.engineId ?? entry?.engine;
115
+ const modelId = parsed?.modelId ?? entry?.id;
116
+ if (!engineId || !modelId) {
117
+ throw new Error(`Unknown model reference: ${key}`);
118
+ }
119
+ const engine = directory.engines[engineId];
120
+ if (!engine) {
121
+ throw new Error(`Unknown engine: ${engineId}`);
122
+ }
123
+ const settings = mergeSettings(engine.settings, entry?.settings);
124
+ if (engine.build) {
125
+ return engine.build(modelId, settings);
126
+ }
127
+ const cacheKey2 = settingsKey(settings, engine.adapter, engineId);
128
+ const cached = factoryCache.get(cacheKey2);
129
+ if (cached) return cached(modelId);
130
+ const factory = await createFactory(engine.adapter, settings);
131
+ factoryCache.set(cacheKey2, factory);
132
+ return factory(modelId);
133
+ };
134
+ }
5
135
 
6
- // src/capabilities/types.ts
136
+ // src/models/types.ts
7
137
  var SourcePriority = /* @__PURE__ */ ((SourcePriority3) => {
8
138
  SourcePriority3[SourcePriority3["UserConfig"] = 0] = "UserConfig";
9
139
  SourcePriority3[SourcePriority3["LocalCache"] = 1] = "LocalCache";
@@ -23,7 +153,7 @@ var DEFAULT_RESOLVER_OPTIONS = {
23
153
  modelOverrides: {}
24
154
  };
25
155
 
26
- // src/capabilities/patterns.ts
156
+ // src/models/profiles.ts
27
157
  var REASONING_PATTERNS = [
28
158
  // OpenAI o-series
29
159
  {
@@ -194,7 +324,7 @@ function getProviderCompatibility(modelId, provider) {
194
324
  return match?.rule.compatibility;
195
325
  }
196
326
 
197
- // src/capabilities/overrides.ts
327
+ // src/models/overrides.ts
198
328
  function normalizeKey(value) {
199
329
  const trimmed = value?.trim();
200
330
  return trimmed && trimmed.length > 0 ? trimmed : void 0;
@@ -228,7 +358,7 @@ function applyCapabilityOverride(entry, override) {
228
358
  };
229
359
  }
230
360
 
231
- // src/capabilities/cache/adapters.ts
361
+ // src/models/cache/adapters.ts
232
362
  function isNodeEnvironment() {
233
363
  return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
234
364
  }
@@ -325,7 +455,7 @@ function createCapabilityCacheAdapter(cachePath) {
325
455
  return new MemoryAdapter();
326
456
  }
327
457
 
328
- // src/capabilities/cache/types.ts
458
+ // src/models/cache/types.ts
329
459
  var CACHE_VERSION = 1;
330
460
  function cacheKey(modelId, provider) {
331
461
  return provider ? `${provider}:${modelId}` : modelId;
@@ -334,7 +464,7 @@ function isExpired(data) {
334
464
  return Date.now() > new Date(data.expiresAt).getTime();
335
465
  }
336
466
 
337
- // src/capabilities/cache/manager.ts
467
+ // src/models/cache/manager.ts
338
468
  var CapabilityCache = class {
339
469
  adapter;
340
470
  memoryCache = /* @__PURE__ */ new Map();
@@ -436,7 +566,7 @@ var CacheCapabilitySource = class {
436
566
  }
437
567
  };
438
568
 
439
- // src/capabilities/remote/network.ts
569
+ // src/models/remote/network.ts
440
570
  function hasBrowserNetworkAPI() {
441
571
  return typeof globalThis.navigator !== "undefined" && "onLine" in globalThis.navigator;
442
572
  }
@@ -509,7 +639,7 @@ async function fetchWithTimeout(url, timeoutMs) {
509
639
  }
510
640
  }
511
641
 
512
- // src/capabilities/remote/transform.ts
642
+ // src/models/remote/transform.ts
513
643
  function transformModelsDevEntry(raw) {
514
644
  const capabilities = {
515
645
  reasoning: raw.reasoning ?? false,
@@ -533,7 +663,7 @@ function transformModelsDevEntry(raw) {
533
663
  };
534
664
  }
535
665
 
536
- // src/capabilities/remote/fetcher.ts
666
+ // src/models/remote/fetcher.ts
537
667
  var RemoteCapabilityFetcher = class {
538
668
  apiUrl;
539
669
  timeoutMs;
@@ -592,7 +722,7 @@ var RemoteCapabilityFetcher = class {
592
722
  }
593
723
  };
594
724
 
595
- // src/capabilities/remote/source.ts
725
+ // src/models/remote/source.ts
596
726
  var RemoteCapabilitySource = class {
597
727
  priority = 4 /* RemoteAPI */;
598
728
  name = "Remote API (models.dev)";
@@ -659,7 +789,7 @@ var RemoteCapabilitySource = class {
659
789
  }
660
790
  };
661
791
 
662
- // src/capabilities/resolver.ts
792
+ // src/models/capability-resolver.ts
663
793
  function extractModelId(model) {
664
794
  return getModelId(model);
665
795
  }
@@ -858,6 +988,9 @@ function configureResolver(options) {
858
988
  }
859
989
 
860
990
  export {
991
+ getModelId,
992
+ getProviderId,
993
+ createResolver,
861
994
  SourcePriority,
862
995
  DEFAULT_RESOLVER_OPTIONS,
863
996
  inferProvider,