@cuylabs/agent-core 0.5.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.
- package/README.md +85 -372
- package/dist/{builder-RcTZuYnO.d.ts → builder-BRvqCcIk.d.ts} +2 -2
- package/dist/{resolver-DOfZ-xuk.d.ts → capability-resolver-CgRGsWVX.d.ts} +1 -1
- package/dist/{chunk-IMGQOTU2.js → chunk-3HNO5SVI.js} +286 -690
- package/dist/chunk-5K7AQVOU.js +619 -0
- package/dist/{chunk-QAQADS4X.js → chunk-BNSHUWCV.js} +1 -0
- package/dist/{chunk-OTUGSCED.js → chunk-CDTV2UYU.js} +159 -1
- package/dist/chunk-IEFIQENH.js +73 -0
- package/dist/chunk-N7P4PN3O.js +84 -0
- package/dist/{chunk-QWFMX226.js → chunk-QGOGIP7T.js} +148 -15
- package/dist/chunk-VNQBHPCT.js +398 -0
- package/dist/{chunk-X635CM2F.js → chunk-ZPMACVZK.js} +1 -1
- package/dist/context/index.js +1 -1
- package/dist/host/index.d.ts +45 -0
- package/dist/host/index.js +8 -0
- package/dist/{index-p0kOsVsE.d.ts → index-C33hlD6H.d.ts} +12 -7
- package/dist/{index-tmhaADz5.d.ts → index-CfBGYrpd.d.ts} +121 -2
- package/dist/index.d.ts +107 -126
- package/dist/index.js +322 -597
- package/dist/inference/index.d.ts +59 -0
- package/dist/inference/index.js +25 -0
- package/dist/middleware/index.d.ts +8 -4
- package/dist/middleware/index.js +5 -3
- package/dist/models/index.d.ts +104 -2
- package/dist/models/index.js +40 -6
- package/dist/prompt/index.d.ts +10 -6
- package/dist/reasoning/index.d.ts +54 -8
- package/dist/reasoning/index.js +2 -3
- package/dist/{registry-CuRWWtcT.d.ts → registry-BDLIHOQB.d.ts} +1 -1
- package/dist/{runner-C7aMP_x3.d.ts → runner-DSKaEz3z.d.ts} +290 -7
- package/dist/runtime/index.d.ts +41 -7
- package/dist/runtime/index.js +15 -6
- package/dist/scope/index.d.ts +10 -0
- package/dist/scope/index.js +14 -0
- package/dist/{session-manager-Uawm2Le7.d.ts → session-manager-B_CWGTsl.d.ts} +1 -1
- package/dist/skill/index.d.ts +7 -5
- package/dist/storage/index.d.ts +2 -2
- package/dist/sub-agent/index.d.ts +12 -8
- package/dist/tool/index.d.ts +8 -4
- package/dist/tool/index.js +4 -3
- package/dist/{tool-pFAnJc5Y.d.ts → tool-Db1Ue-1U.d.ts} +1 -1
- package/dist/{tool-DYp6-cC3.d.ts → tool-HUtkiVBx.d.ts} +5 -99
- package/dist/tracking/index.d.ts +3 -1
- package/dist/types-9jGQUjqW.d.ts +29 -0
- package/dist/types-CHiPh8U2.d.ts +100 -0
- package/dist/types-CqDZTh4d.d.ts +335 -0
- package/dist/types-FRpzzg_9.d.ts +355 -0
- package/package.json +19 -8
- package/dist/capabilities/index.d.ts +0 -97
- package/dist/capabilities/index.js +0 -46
- package/dist/chunk-6TDTQJ4P.js +0 -116
- package/dist/chunk-DWYX7ASF.js +0 -26
- package/dist/chunk-FG4MD5MU.js +0 -54
- package/dist/config-D2xeGEHK.d.ts +0 -52
- package/dist/identifiers-BLUxFqV_.d.ts +0 -12
- package/dist/network-D76DS5ot.d.ts +0 -5
- package/dist/types-MM1JoX5T.d.ts +0 -810
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/middleware/runner.ts
|
|
2
|
+
function isBlockedModelCall(value) {
|
|
3
|
+
return "block" in value && value.block === true;
|
|
4
|
+
}
|
|
2
5
|
var MiddlewareRunner = class {
|
|
3
6
|
stack;
|
|
4
7
|
constructor(middleware = []) {
|
|
@@ -17,6 +20,76 @@ var MiddlewareRunner = class {
|
|
|
17
20
|
return this.stack;
|
|
18
21
|
}
|
|
19
22
|
// --------------------------------------------------------------------------
|
|
23
|
+
// model.input — array order, first block wins
|
|
24
|
+
// --------------------------------------------------------------------------
|
|
25
|
+
async runModelInput(input, ctx) {
|
|
26
|
+
let current = input;
|
|
27
|
+
for (const mw of this.stack) {
|
|
28
|
+
if (!mw.model?.input) continue;
|
|
29
|
+
try {
|
|
30
|
+
const next = await mw.model.input(current, ctx);
|
|
31
|
+
if (!next) continue;
|
|
32
|
+
if (isBlockedModelCall(next)) {
|
|
33
|
+
return next;
|
|
34
|
+
}
|
|
35
|
+
current = next;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
return {
|
|
38
|
+
block: true,
|
|
39
|
+
reason: `Middleware "${mw.name}" model input error: ${err instanceof Error ? err.message : String(err)}`
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return current;
|
|
44
|
+
}
|
|
45
|
+
// --------------------------------------------------------------------------
|
|
46
|
+
// model.chunk — array order, streamed transformation
|
|
47
|
+
// --------------------------------------------------------------------------
|
|
48
|
+
async runModelChunk(chunk, ctx) {
|
|
49
|
+
let current = chunk;
|
|
50
|
+
for (const mw of this.stack) {
|
|
51
|
+
if (!mw.model?.chunk || current === null) continue;
|
|
52
|
+
try {
|
|
53
|
+
const next = await mw.model.chunk(current, ctx);
|
|
54
|
+
if (next === null) {
|
|
55
|
+
current = null;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (next !== void 0) {
|
|
59
|
+
current = next;
|
|
60
|
+
}
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.warn(
|
|
63
|
+
`[middleware] "${mw.name}" model.chunk error:`,
|
|
64
|
+
err instanceof Error ? err.message : String(err)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return current ?? void 0;
|
|
69
|
+
}
|
|
70
|
+
// --------------------------------------------------------------------------
|
|
71
|
+
// model.output — reverse order (innermost first)
|
|
72
|
+
// --------------------------------------------------------------------------
|
|
73
|
+
async runModelOutput(output, ctx) {
|
|
74
|
+
let current = output;
|
|
75
|
+
for (let i = this.stack.length - 1; i >= 0; i--) {
|
|
76
|
+
const mw = this.stack[i];
|
|
77
|
+
if (!mw.model?.output) continue;
|
|
78
|
+
try {
|
|
79
|
+
const next = await mw.model.output(current, ctx);
|
|
80
|
+
if (next) {
|
|
81
|
+
current = next;
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.warn(
|
|
85
|
+
`[middleware] "${mw.name}" model.output error:`,
|
|
86
|
+
err instanceof Error ? err.message : String(err)
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return current;
|
|
91
|
+
}
|
|
92
|
+
// --------------------------------------------------------------------------
|
|
20
93
|
// beforeToolCall — array order, first "deny" wins
|
|
21
94
|
// --------------------------------------------------------------------------
|
|
22
95
|
/**
|
|
@@ -679,6 +752,90 @@ async function createAndRegisterProvider(spanProcessor, serviceName) {
|
|
|
679
752
|
return provider;
|
|
680
753
|
}
|
|
681
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
|
+
|
|
682
839
|
export {
|
|
683
840
|
MiddlewareRunner,
|
|
684
841
|
otelMiddleware,
|
|
@@ -687,5 +844,6 @@ export {
|
|
|
687
844
|
ApprovalDeniedError,
|
|
688
845
|
ApprovalTimeoutError,
|
|
689
846
|
createApprovalHandler,
|
|
690
|
-
approvalMiddleware
|
|
847
|
+
approvalMiddleware,
|
|
848
|
+
promptCacheMiddleware
|
|
691
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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/
|
|
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,
|