@kwirthmagnify/kwirth-common-ai 0.5.80 → 0.5.81
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/back.d.ts +7 -2
- package/dist/back.js +27 -8
- package/dist/index.d.ts +4 -0
- package/package.json +1 -1
package/dist/back.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig, IToolsetGrant, IUsageAmounts, IUsageLimits } from './index';
|
|
1
|
+
import { ILlm, ILlmProvider, ECapability, EToolEffect, EToolSensitivity, EUsagePeriod, EUsageUnit, IAiToolInfo, IAiToolsetInfo, IToolsetConfig, IToolsetGrant, IUsageAmounts, IUsageLimits, TAiToolsetConfig } from './index';
|
|
2
2
|
import type { AppsV1Api, CoreV1Api, CustomObjectsApi, NetworkingV1Api } from '@kubernetes/client-node';
|
|
3
3
|
import type { IProviderHandle } from '@kwirthmagnify/kwirth-common-back';
|
|
4
4
|
interface ILogChannel {
|
|
@@ -206,6 +206,7 @@ export interface IProvidersCapability {
|
|
|
206
206
|
export interface IToolHost {
|
|
207
207
|
/** Records the invocation. NOT a capability: it is always lent, and never declared. */
|
|
208
208
|
trace: (toolName: string, args: Record<string, unknown>) => void;
|
|
209
|
+
config: TAiToolsetConfig;
|
|
209
210
|
k8s?: IK8sCapability;
|
|
210
211
|
metrics?: IMetricsCapability;
|
|
211
212
|
events?: IEventsCapability;
|
|
@@ -249,6 +250,10 @@ export declare const isBuiltInToolsetId: (id: string) => boolean;
|
|
|
249
250
|
* Repeated plugins are merged: the union of their tools, and `allTools` if any of them says so.
|
|
250
251
|
*/
|
|
251
252
|
export declare const setToolsetGrants: (toolsetId: string, grants: IToolsetGrant[]) => void;
|
|
253
|
+
/** Sets a toolset's settings (the defaults already applied); `undefined` removes them. Called by the core. */
|
|
254
|
+
export declare const setToolsetConfig: (toolsetId: string, config: TAiToolsetConfig | undefined) => void;
|
|
255
|
+
/** A toolset's settings as its tools receive them: a copy, `{}` when it has none. */
|
|
256
|
+
export declare const getToolsetConfig: (toolsetId: string) => TAiToolsetConfig;
|
|
252
257
|
/** A toolset's grant, as it lives in the registry. Empty = nobody, which is the default state. */
|
|
253
258
|
export declare const getToolsetGrants: (toolsetId: string) => IToolsetGrant[];
|
|
254
259
|
/** Whether THAT plugin may use THAT toolset: at least one of its tools is granted to it. */
|
|
@@ -274,7 +279,7 @@ export declare const listToolsetInfos: () => IAiToolsetInfo[];
|
|
|
274
279
|
* if every site assembled its own object, one of them being too generous would be enough for the
|
|
275
280
|
* declaration to stop meaning anything.
|
|
276
281
|
*/
|
|
277
|
-
export declare const buildToolHost: (requires: ECapability[], context: IToolContext, providers?: string[]) => IToolHost;
|
|
282
|
+
export declare const buildToolHost: (requires: ECapability[], context: IToolContext, providers?: string[], config?: TAiToolsetConfig) => IToolHost;
|
|
278
283
|
/**
|
|
279
284
|
* Invokes a tool by its qualified reference. A single invocation path for built-in and installed alike,
|
|
280
285
|
* with the host built according to the `requires` of ITS toolset.
|
package/dist/back.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildAgentTools = exports.resolveTools = exports.resolveToolRef = exports.invokeToolRef = exports.buildToolHost = exports.listToolsetInfos = exports.listToolsets = exports.getToolset = exports.unregisterToolset = exports.registerToolset = exports.isToolGrantedTo = exports.isToolsetGrantedTo = exports.getToolsetGrants = exports.setToolsetGrants = exports.isBuiltInToolsetId = exports.defineTool = exports.runWithToolContext = exports.EToolEffect = exports.zodFromExample = exports.z = exports.tool = exports.stepCountIs = exports.Output = exports.streamText = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.effectiveLimits = exports.usageSubjectForKey = exports.getUsageService = exports.setUsageService = exports.setAiLogger = exports.UsageLimitError = exports.EUsageScope = exports.buildModel = void 0;
|
|
3
|
+
exports.buildAgentTools = exports.resolveTools = exports.resolveToolRef = exports.invokeToolRef = exports.buildToolHost = exports.listToolsetInfos = exports.listToolsets = exports.getToolset = exports.unregisterToolset = exports.registerToolset = exports.isToolGrantedTo = exports.isToolsetGrantedTo = exports.getToolsetGrants = exports.getToolsetConfig = exports.setToolsetConfig = exports.setToolsetGrants = exports.isBuiltInToolsetId = exports.defineTool = exports.runWithToolContext = exports.EToolEffect = exports.zodFromExample = exports.z = exports.tool = exports.stepCountIs = exports.Output = exports.streamText = exports.generateText = exports.loadModels = exports.generateVision = exports.runWithUsageGuard = exports.effectiveLimits = exports.usageSubjectForKey = exports.getUsageService = exports.setUsageService = exports.setAiLogger = exports.UsageLimitError = exports.EUsageScope = exports.buildModel = void 0;
|
|
4
4
|
const index_1 = require("./index");
|
|
5
5
|
// Aliased on purpose: the generateText this module EXPORTS is the wrapped one, and nothing below should
|
|
6
6
|
// be able to reach the raw SDK call by accident — that is exactly how a call would escape the counters.
|
|
@@ -557,7 +557,7 @@ exports.generateText = ((options) => (0, exports.runWithUsageGuard)(options, ()
|
|
|
557
557
|
ends (the SDK reports it with the last chunk). The whole call cannot go through runWithUsageGuard —
|
|
558
558
|
the guard's promise resolves when the call finishes, but the caller needs the result object
|
|
559
559
|
IMMEDIATELY to consume textStream; awaiting the guard would deliver it only after the stream is over.
|
|
560
|
-
A failed stream records nothing, exactly like a failed generateText. First consumer: the
|
|
560
|
+
A failed stream records nothing, exactly like a failed generateText. First consumer: the llm-router channel (then vllm),
|
|
561
561
|
which re-streams what the model it routed to produces.
|
|
562
562
|
*/
|
|
563
563
|
exports.streamText = (async (options) => {
|
|
@@ -756,6 +756,24 @@ const setToolsetGrants = (toolsetId, grants) => {
|
|
|
756
756
|
toolsetGrants.set(toolsetId, byPlugin);
|
|
757
757
|
};
|
|
758
758
|
exports.setToolsetGrants = setToolsetGrants;
|
|
759
|
+
/*
|
|
760
|
+
Each toolset's SETTINGS (plans/extension-config, P3), with the same pattern as the grants: in memory,
|
|
761
|
+
loaded and kept up to date by the core, which owns the Secret they are saved in. No invocation reads
|
|
762
|
+
the Secret: a tool gets `host.config` from here. With several replicas a save applies from the next
|
|
763
|
+
call in the replica that saved, as the grants do.
|
|
764
|
+
*/
|
|
765
|
+
const toolsetConfigs = new Map();
|
|
766
|
+
/** Sets a toolset's settings (the defaults already applied); `undefined` removes them. Called by the core. */
|
|
767
|
+
const setToolsetConfig = (toolsetId, config) => {
|
|
768
|
+
if (config === undefined)
|
|
769
|
+
toolsetConfigs.delete(toolsetId);
|
|
770
|
+
else
|
|
771
|
+
toolsetConfigs.set(toolsetId, Object.assign({}, config));
|
|
772
|
+
};
|
|
773
|
+
exports.setToolsetConfig = setToolsetConfig;
|
|
774
|
+
/** A toolset's settings as its tools receive them: a copy, `{}` when it has none. */
|
|
775
|
+
const getToolsetConfig = (toolsetId) => { var _a; return (Object.assign({}, ((_a = toolsetConfigs.get(toolsetId)) !== null && _a !== void 0 ? _a : {}))); };
|
|
776
|
+
exports.getToolsetConfig = getToolsetConfig;
|
|
759
777
|
/** A toolset's grant, as it lives in the registry. Empty = nobody, which is the default state. */
|
|
760
778
|
const getToolsetGrants = (toolsetId) => { var _a, _b; return [...((_b = (_a = toolsetGrants.get(toolsetId)) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [])].map(g => (Object.assign(Object.assign({}, g), { tools: [...g.tools] }))); };
|
|
761
779
|
exports.getToolsetGrants = getToolsetGrants;
|
|
@@ -793,8 +811,9 @@ const unregisterToolset = (id) => {
|
|
|
793
811
|
if (builtInToolsetIds.has(id))
|
|
794
812
|
return false;
|
|
795
813
|
// The grant goes with the toolset: leaving it orphaned would make reinstalling resurrect permissions
|
|
796
|
-
// nobody has granted again.
|
|
814
|
+
// nobody has granted again. The settings too: the core hands them back when it registers it again.
|
|
797
815
|
toolsetGrants.delete(id);
|
|
816
|
+
toolsetConfigs.delete(id);
|
|
798
817
|
return toolsetRegistry.delete(id);
|
|
799
818
|
};
|
|
800
819
|
exports.unregisterToolset = unregisterToolset;
|
|
@@ -803,7 +822,7 @@ exports.getToolset = getToolset;
|
|
|
803
822
|
const listToolsets = () => [...toolsetRegistry.values()];
|
|
804
823
|
exports.listToolsets = listToolsets;
|
|
805
824
|
/** What is sent to the front end: the descriptors, without inputSchema or execute. */
|
|
806
|
-
const listToolsetInfos = () => (0, exports.listToolsets)().map(t => (Object.assign(Object.assign({ id: t.id, version: t.version, displayName: t.displayName, description: t.description, requires: t.requires }, (t.providers ? { providers: t.providers } : {})), { tools: t.tools.map(x => ({ name: x.name, description: x.description, effect: x.effect, sensitivity: x.sensitivity })) })));
|
|
825
|
+
const listToolsetInfos = () => (0, exports.listToolsets)().map(t => (Object.assign(Object.assign(Object.assign({ id: t.id, version: t.version, displayName: t.displayName, description: t.description, requires: t.requires }, (t.providers ? { providers: t.providers } : {})), (t.configSchema ? { configSchema: t.configSchema } : {})), { tools: t.tools.map(x => ({ name: x.name, description: x.description, effect: x.effect, sensitivity: x.sensitivity })) })));
|
|
807
826
|
exports.listToolsetInfos = listToolsetInfos;
|
|
808
827
|
/**
|
|
809
828
|
* Builds a toolset's host from the core context, provisioning ONLY what was declared.
|
|
@@ -812,9 +831,9 @@ exports.listToolsetInfos = listToolsetInfos;
|
|
|
812
831
|
* if every site assembled its own object, one of them being too generous would be enough for the
|
|
813
832
|
* declaration to stop meaning anything.
|
|
814
833
|
*/
|
|
815
|
-
const buildToolHost = (requires, context, providers) => {
|
|
834
|
+
const buildToolHost = (requires, context, providers, config = {}) => {
|
|
816
835
|
var _a, _b, _c;
|
|
817
|
-
const host = { trace: context.trace };
|
|
836
|
+
const host = { trace: context.trace, config };
|
|
818
837
|
const ci = context.clusterInfo;
|
|
819
838
|
// No cluster means no cluster capability, even when declared: better that the tool receives
|
|
820
839
|
// 'undefined' and says so, than handing it a half-built facade that fails from the inside.
|
|
@@ -862,7 +881,7 @@ const invokeToolRef = async (ref, args, context) => {
|
|
|
862
881
|
const resolved = (0, exports.resolveToolRef)(ref);
|
|
863
882
|
if (!resolved)
|
|
864
883
|
throw new Error(`[common-ai] unknown tool '${ref}'`);
|
|
865
|
-
return resolved.tool.execute(args, (0, exports.buildToolHost)(resolved.toolset.requires, context, resolved.toolset.providers));
|
|
884
|
+
return resolved.tool.execute(args, (0, exports.buildToolHost)(resolved.toolset.requires, context, resolved.toolset.providers, (0, exports.getToolsetConfig)(resolved.toolset.id)));
|
|
866
885
|
};
|
|
867
886
|
exports.invokeToolRef = invokeToolRef;
|
|
868
887
|
/** Resolves a qualified reference '<toolset>/<tool>' against the registry. */
|
|
@@ -964,7 +983,7 @@ requesterId) => {
|
|
|
964
983
|
(_a = hooks.observe) === null || _a === void 0 ? void 0 : _a.call(hooks, invocation, { ok: false, error: denial.error, ms: Date.now() - started, denied: true });
|
|
965
984
|
return denial;
|
|
966
985
|
}
|
|
967
|
-
const host = (0, exports.buildToolHost)((_c = (_b = (0, exports.getToolset)(e.toolsetId)) === null || _b === void 0 ? void 0 : _b.requires) !== null && _c !== void 0 ? _c : [], context, (_d = (0, exports.getToolset)(e.toolsetId)) === null || _d === void 0 ? void 0 : _d.providers);
|
|
986
|
+
const host = (0, exports.buildToolHost)((_c = (_b = (0, exports.getToolset)(e.toolsetId)) === null || _b === void 0 ? void 0 : _b.requires) !== null && _c !== void 0 ? _c : [], context, (_d = (0, exports.getToolset)(e.toolsetId)) === null || _d === void 0 ? void 0 : _d.providers, (0, exports.getToolsetConfig)(e.toolsetId));
|
|
968
987
|
try {
|
|
969
988
|
// runWithToolContext also wraps the tools written against the OLD contract (the ones
|
|
970
989
|
// that read ctx()). That is what lets the 43 be migrated package by package in S3,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IConfigFieldDef } from '@kwirthmagnify/kwirth-common';
|
|
1
2
|
export declare const STORAGE_KEY_PROVIDERS = "kwirth-ai-providers";
|
|
2
3
|
export declare const STORAGE_KEY_LLMS = "kwirth-ai-llms";
|
|
3
4
|
export declare const PROVIDERS_AVAILABLE: string[];
|
|
@@ -113,8 +114,11 @@ export interface IAiToolsetInfo {
|
|
|
113
114
|
requires: ECapability[];
|
|
114
115
|
/** With ECapability.PROVIDERS: the ids of the providers it needs (e.g. ['aws']). The host lends ONLY these. */
|
|
115
116
|
providers?: string[];
|
|
117
|
+
configSchema?: IConfigFieldDef[];
|
|
116
118
|
tools: IAiToolInfo[];
|
|
117
119
|
}
|
|
120
|
+
/** A toolset's settings: what its `configSchema` describes, the defaults applied. Like `TRouterConfig`. */
|
|
121
|
+
export type TAiToolsetConfig = Record<string, unknown>;
|
|
118
122
|
export interface IToolsetGrant {
|
|
119
123
|
pluginId: string;
|
|
120
124
|
allTools: boolean;
|