nuabase 2.3.3 → 2.3.5
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 +96 -31
- package/dist/cjs/chunks/logger-Co5uZG0A.cjs +90 -0
- package/dist/cjs/index.cjs +1510 -1444
- package/dist/cjs/local-agent.cjs +886 -0
- package/dist/esm/chunks/logger-DnkHzkX_.mjs +88 -0
- package/dist/esm/index.mjs +1511 -1445
- package/dist/esm/local-agent.mjs +884 -0
- package/dist/types/__tests__/helpers/client-factory.d.ts +33 -11
- package/dist/types/__tests__/helpers/index.d.ts +2 -1
- package/dist/types/backend/direct.d.ts +20 -8
- package/dist/types/backend/types.d.ts +9 -4
- package/dist/types/index.d.cts +3 -2
- package/dist/types/index.d.mts +3 -2
- package/dist/types/local-agent.d.cts +2 -0
- package/dist/types/local-agent.d.mts +2 -0
- package/dist/types/nua-llm-core/index.d.ts +23 -0
- package/dist/types/nua-llm-core/lib/error-utils.d.ts +6 -0
- package/dist/types/nua-llm-core/lib/logger.d.ts +44 -0
- package/dist/types/nua-llm-core/lib/nua-errors.d.ts +12 -0
- package/dist/types/nua-llm-core/lib/prompt-template-utils.d.ts +2 -0
- package/dist/types/nua-llm-core/lib/schema-utils.d.ts +1 -0
- package/dist/types/nua-llm-core/lib/stable-stringify.d.ts +1 -0
- package/dist/types/nua-llm-core/lib/strict-json-schema.d.ts +7 -0
- package/dist/types/nua-llm-core/lib/types.d.ts +8 -0
- package/dist/types/nua-llm-core/lib/validate-mappable-input-data.d.ts +4 -0
- package/dist/types/nua-llm-core/local-agent.d.ts +7 -0
- package/dist/types/nua-llm-core/modules/agent/agent-loop.d.ts +10 -0
- package/dist/types/nua-llm-core/modules/agent/agent-turn.d.ts +17 -0
- package/dist/types/nua-llm-core/modules/agent/provider-formatters.d.ts +22 -0
- package/dist/types/nua-llm-core/modules/agent/run-agent.d.ts +7 -0
- package/dist/types/nua-llm-core/modules/agent/types.d.ts +90 -0
- package/dist/types/nua-llm-core/modules/cast/cast-request.d.ts +32 -0
- package/dist/types/nua-llm-core/modules/cast/envelope.d.ts +9 -0
- package/dist/types/nua-llm-core/modules/cast/list-schema.d.ts +32 -0
- package/dist/types/nua-llm-core/modules/cast/prompts/list-prompt.d.ts +3 -0
- package/dist/types/nua-llm-core/modules/cast/prompts/render-prompt.d.ts +7 -0
- package/dist/types/nua-llm-core/modules/cast/prompts/value-prompt.d.ts +3 -0
- package/dist/types/nua-llm-core/modules/cast/run-cast.d.ts +44 -0
- package/dist/types/nua-llm-core/modules/engine/engine-router.d.ts +23 -0
- package/dist/types/nua-llm-core/modules/engine/http/http-engine.d.ts +12 -0
- package/dist/types/nua-llm-core/modules/engine/http/provider-call.d.ts +33 -0
- package/dist/types/nua-llm-core/modules/engine/http/provider-config.d.ts +65 -0
- package/dist/types/nua-llm-core/modules/engine/http/provider-engines.d.ts +35 -0
- package/dist/types/nua-llm-core/modules/engine/llm-engine.d.ts +73 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/adapters/claude-code.d.ts +2 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/adapters/codex.d.ts +2 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/adapters/failure-classification.d.ts +4 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/adapters/index.d.ts +3 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/adapters/types.d.ts +61 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/agent-id.d.ts +4 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/create-local-agent.d.ts +36 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/detect.d.ts +20 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/local-agent-engine.d.ts +32 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/local-agent.d.ts +25 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/process-runner.d.ts +16 -0
- package/dist/types/nua-llm-core/modules/engine/local-agent/semaphore.d.ts +8 -0
- package/dist/types/nua-llm-core/modules/execution/llm-response-extraction.d.ts +5 -0
- package/dist/types/nua-llm-core/modules/json-schema-validation/ajv-instances.d.ts +12 -0
- package/dist/types/nua-llm-core/modules/json-schema-validation/nua-json-schema-value-validation.d.ts +10 -0
- package/dist/types/nua-llm-core/modules/json-schema-validation/validate-json-schema.d.ts +2 -0
- package/dist/types/nua-llm-core/modules/model-info/index.d.ts +15 -0
- package/dist/types/nua-llm-core/modules/streaming/sse-parser.d.ts +7 -0
- package/dist/types/nua-llm-core/nua-llm-client.d.ts +37 -0
- package/dist/types/nua.d.ts +8 -2
- package/package.json +19 -2
|
@@ -1,17 +1,39 @@
|
|
|
1
1
|
import { Nua } from '../../nua';
|
|
2
|
-
|
|
2
|
+
import type { LocalAgentId } from '../../nua-llm-core/index';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param mode - 'gateway' or 'direct'
|
|
10
|
-
* @returns Configured Nua instance
|
|
4
|
+
* One way of running casts that the shared tests exercise:
|
|
5
|
+
* - gateway: the Nuabase API at NUABASE_API_URL
|
|
6
|
+
* - direct: LLM providers' APIs, with API keys
|
|
7
|
+
* - local-agent: a coding-agent CLI on this machine, with its own login
|
|
11
8
|
*/
|
|
12
|
-
export
|
|
9
|
+
export type TestMode = {
|
|
10
|
+
name: string;
|
|
11
|
+
} & ({
|
|
12
|
+
kind: 'gateway';
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'direct';
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'local-agent';
|
|
17
|
+
agent: LocalAgentId | 'auto';
|
|
18
|
+
});
|
|
13
19
|
/**
|
|
14
|
-
* Returns the
|
|
15
|
-
*
|
|
20
|
+
* Returns the modes to run the shared tests in.
|
|
21
|
+
* - gateway, unless SKIP_GATEWAY_TESTS is set.
|
|
22
|
+
* - direct, when a provider API key is set, unless SKIP_DIRECT_TESTS is set.
|
|
23
|
+
* - one local-agent mode per agent in NUA_LOCAL_AGENTS (e.g. "claude-code,codex", or "auto").
|
|
16
24
|
*/
|
|
17
25
|
export declare function getTestModes(): TestMode[];
|
|
26
|
+
/**
|
|
27
|
+
* Creates a Nua client for the specified test mode.
|
|
28
|
+
*
|
|
29
|
+
* Gateway mode: Requires NUABASE_API_URL and NUABASE_API_KEY for a running Nuabase API
|
|
30
|
+
* Direct mode: Requires one of GROQ_API_KEY, OPENROUTER_API_KEY, CEREBRAS_API_KEY, or GEMINI_API_KEY
|
|
31
|
+
* Local-agent mode: Requires that agent installed and logged in on this machine
|
|
32
|
+
*/
|
|
33
|
+
export declare function createTestClient(mode: TestMode): Promise<Nua>;
|
|
34
|
+
/** The result `source` a mode produces: local agents run through the direct backend. */
|
|
35
|
+
export declare function expectedSource(mode: TestMode): 'gateway' | 'direct';
|
|
36
|
+
/** The part of a direct result's `meta` that says which engine answered. */
|
|
37
|
+
export declare function expectedDirectMeta(mode: TestMode): Record<string, unknown>;
|
|
38
|
+
/** Per-test time limit. A coding-agent CLI starts a process and may reason before answering. */
|
|
39
|
+
export declare function testTimeoutMs(mode: TestMode): number;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { createTestClient, getTestModes,
|
|
1
|
+
export { createTestClient, expectedDirectMeta, expectedSource, getTestModes, testTimeoutMs, } from './client-factory';
|
|
2
|
+
export type { TestMode } from './client-factory';
|
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
import { CastArrayParams, CastResult, CastValueParams, LlmBackend } from './types';
|
|
2
|
-
import { LlmProviderId } from 'nua-llm-core';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
2
|
+
import type { LlmProviderId, ModelInput } from '../nua-llm-core/index';
|
|
3
|
+
import type { LocalAgent } from '../nua-llm-core/local-agent';
|
|
4
|
+
/** API keys for the LLM providers direct mode may call. */
|
|
5
|
+
export type ProviderApiKeys = {
|
|
6
|
+
[key in LlmProviderId]?: {
|
|
7
|
+
apiKey: string;
|
|
9
8
|
};
|
|
10
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* Either `providers`: API keys for LLM providers, with an optional default `model`.
|
|
12
|
+
* Or `localAgent`: a coding-agent CLI on this machine, from `localAgent()` or
|
|
13
|
+
* `findLocalAgent()` in "nuabase/local-agent".
|
|
14
|
+
*/
|
|
15
|
+
export type DirectConfig = {
|
|
16
|
+
providers: ProviderApiKeys;
|
|
17
|
+
model?: ModelInput;
|
|
18
|
+
localAgent?: never;
|
|
19
|
+
} | {
|
|
20
|
+
localAgent: LocalAgent;
|
|
21
|
+
providers?: never;
|
|
22
|
+
model?: never;
|
|
23
|
+
};
|
|
11
24
|
export declare class DirectBackend implements LlmBackend {
|
|
12
25
|
private readonly client;
|
|
13
|
-
private readonly model;
|
|
14
26
|
constructor(config: DirectConfig);
|
|
15
27
|
castValue<T>(params: CastValueParams): Promise<CastResult<T>>;
|
|
16
28
|
castArray<T>(params: CastArrayParams): Promise<CastResult<T[]>>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { AnswerOrigin, ModelInput, SchemaEnforcementKind } from '../nua-llm-core/index';
|
|
1
2
|
export type CastValueParams = {
|
|
2
3
|
prompt: string;
|
|
3
4
|
data: unknown;
|
|
4
5
|
outputName: string;
|
|
5
6
|
outputSchema: object;
|
|
6
|
-
model?:
|
|
7
|
+
model?: ModelInput;
|
|
7
8
|
};
|
|
8
9
|
export type CastArrayParams = {
|
|
9
10
|
prompt: string;
|
|
@@ -11,7 +12,7 @@ export type CastArrayParams = {
|
|
|
11
12
|
primaryKey: string;
|
|
12
13
|
outputName: string;
|
|
13
14
|
outputSchema: object;
|
|
14
|
-
model?:
|
|
15
|
+
model?: ModelInput;
|
|
15
16
|
};
|
|
16
17
|
export type NormalizedUsage = {
|
|
17
18
|
promptTokens: number;
|
|
@@ -24,7 +25,11 @@ export type GatewayMeta = {
|
|
|
24
25
|
llmUsage: NormalizedUsage;
|
|
25
26
|
cacheUsage: NormalizedUsage;
|
|
26
27
|
};
|
|
27
|
-
export type DirectMeta =
|
|
28
|
+
export type DirectMeta = AnswerOrigin & {
|
|
29
|
+
/** How the output schema was enforced. The result is validated against the schema either way. */
|
|
30
|
+
schemaEnforcement: SchemaEnforcementKind;
|
|
31
|
+
};
|
|
32
|
+
export type CastResultSource = 'gateway' | 'direct';
|
|
28
33
|
type CastResultSuccess<T> = {
|
|
29
34
|
success: true;
|
|
30
35
|
data: T;
|
|
@@ -41,7 +46,7 @@ type CastResultSuccess<T> = {
|
|
|
41
46
|
type CastResultFailure = {
|
|
42
47
|
success: false;
|
|
43
48
|
error: string;
|
|
44
|
-
source:
|
|
49
|
+
source: CastResultSource;
|
|
45
50
|
latencyMs: number;
|
|
46
51
|
};
|
|
47
52
|
export type CastResult<T> = CastResultSuccess<T> | CastResultFailure;
|
package/dist/types/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Nua } from './nua';
|
|
2
2
|
export type { OutputDef, GetOptions, ListOptions, ListResultRow } from './nua';
|
|
3
|
-
export type { LlmBackend, CastResult, CastValueParams, CastArrayParams, NormalizedUsage, GatewayMeta, DirectMeta, QueueResult, } from './backend/types';
|
|
3
|
+
export type { LlmBackend, CastResult, CastValueParams, CastArrayParams, NormalizedUsage, GatewayMeta, DirectMeta, CastResultSource, QueueResult, } from './backend/types';
|
|
4
4
|
export type { GatewayConfig } from './backend/gateway';
|
|
5
|
-
export type { DirectConfig } from './backend/direct';
|
|
5
|
+
export type { DirectConfig, ProviderApiKeys } from './backend/direct';
|
|
6
|
+
export type { AnswerOrigin, LocalAgentId, ModelInput, SchemaEnforcementKind } from './nua-llm-core/index';
|
|
6
7
|
export { z } from 'zod';
|
package/dist/types/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Nua } from './nua';
|
|
2
2
|
export type { OutputDef, GetOptions, ListOptions, ListResultRow } from './nua';
|
|
3
|
-
export type { LlmBackend, CastResult, CastValueParams, CastArrayParams, NormalizedUsage, GatewayMeta, DirectMeta, QueueResult, } from './backend/types';
|
|
3
|
+
export type { LlmBackend, CastResult, CastValueParams, CastArrayParams, NormalizedUsage, GatewayMeta, DirectMeta, CastResultSource, QueueResult, } from './backend/types';
|
|
4
4
|
export type { GatewayConfig } from './backend/gateway';
|
|
5
|
-
export type { DirectConfig } from './backend/direct';
|
|
5
|
+
export type { DirectConfig, ProviderApiKeys } from './backend/direct';
|
|
6
|
+
export type { AnswerOrigin, LocalAgentId, ModelInput, SchemaEnforcementKind } from './nua-llm-core/index';
|
|
6
7
|
export { z } from 'zod';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { detectLocalAgents, findLocalAgent, isLocalAgentId, LOCAL_AGENT_IDS, localAgent, } from './nua-llm-core/local-agent';
|
|
2
|
+
export type { LocalAgent, LocalAgentAuthMode, LocalAgentConfig, LocalAgentId, LocalAgentOptions, LocalAgentStatus, } from './nua-llm-core/local-agent';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { detectLocalAgents, findLocalAgent, isLocalAgentId, LOCAL_AGENT_IDS, localAgent, } from './nua-llm-core/local-agent';
|
|
2
|
+
export type { LocalAgent, LocalAgentAuthMode, LocalAgentConfig, LocalAgentId, LocalAgentOptions, LocalAgentStatus, } from './nua-llm-core/local-agent';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export * from "./nua-llm-client";
|
|
2
|
+
export * from "./lib/logger";
|
|
3
|
+
export * from "./lib/types";
|
|
4
|
+
export * from "./lib/nua-errors";
|
|
5
|
+
export { LLM_PROVIDER_IDS, normalizedUsageZero, providerConfigs, } from "./modules/engine/http/provider-config";
|
|
6
|
+
export type { LlmProviderId } from "./modules/engine/http/provider-config";
|
|
7
|
+
export { providerEngines } from "./modules/engine/http/provider-engines";
|
|
8
|
+
export type { ProviderEngines, ProviderEnginesConfig } from "./modules/engine/http/provider-engines";
|
|
9
|
+
export type { EngineRouter, Route } from "./modules/engine/engine-router";
|
|
10
|
+
export type { AnswerOrigin, LlmEngine, SchemaEnforcementKind, } from "./modules/engine/llm-engine";
|
|
11
|
+
export type { LocalAgentId } from "./modules/engine/local-agent/agent-id";
|
|
12
|
+
export { validateJsonSchema } from "./modules/json-schema-validation/validate-json-schema";
|
|
13
|
+
export { stableStringify } from "./lib/stable-stringify";
|
|
14
|
+
export * from "./modules/model-info";
|
|
15
|
+
export * from "./lib/schema-utils";
|
|
16
|
+
export { listSchema, parseListSchema } from "./modules/cast/list-schema";
|
|
17
|
+
export type { ListRowKeys, ListSchema } from "./modules/cast/list-schema";
|
|
18
|
+
export type { CastResult, CastSuccess, CastFailure } from "./modules/cast/run-cast";
|
|
19
|
+
export type { RenderedPrompt } from "./modules/cast/cast-request";
|
|
20
|
+
export type { ToolDefinition, ToolExecutionResult, AgentTool, TextContent, ToolCallContent, AssistantContentBlock, UserMessage, AssistantMessage, ToolResultMessage, ConversationMessage, AgenticParsedResponse, AgentRunParams, AgentCompletionReason, AgentResult, AgentEvent, AgentEventHandler, } from "./modules/agent/types";
|
|
21
|
+
export { runAgent } from "./modules/agent/run-agent";
|
|
22
|
+
export { runAgentLoop } from "./modules/agent/agent-loop";
|
|
23
|
+
export type { SendAgenticRequestFn } from "./modules/agent/agent-loop";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export interface Logger {
|
|
2
|
+
debug(message: string, context?: object): void;
|
|
3
|
+
info(message: string, context?: object): void;
|
|
4
|
+
warn(message: string, context?: object): void;
|
|
5
|
+
error(message: string, context?: object): void;
|
|
6
|
+
}
|
|
7
|
+
export declare class ConsoleLogger implements Logger {
|
|
8
|
+
debug(message: string, context?: object): void;
|
|
9
|
+
info(message: string, context?: object): void;
|
|
10
|
+
warn(message: string, context?: object): void;
|
|
11
|
+
error(message: string, context?: object): void;
|
|
12
|
+
}
|
|
13
|
+
export declare const generateSpanId: () => string;
|
|
14
|
+
/**
|
|
15
|
+
* Generic header filtering with allowlist and optional redaction
|
|
16
|
+
*/
|
|
17
|
+
export declare function pickHeaders(headers: Record<string, string | string[] | undefined>, allowlist: Set<string>, redactSet?: Set<string>): Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Error normalization for logging
|
|
20
|
+
*/
|
|
21
|
+
export declare function normalizeError(error: unknown): {
|
|
22
|
+
message: string;
|
|
23
|
+
stack?: string;
|
|
24
|
+
} | string;
|
|
25
|
+
interface LlmRequestLog {
|
|
26
|
+
url: string;
|
|
27
|
+
httpMethod: string;
|
|
28
|
+
model: string;
|
|
29
|
+
maxTokens: number;
|
|
30
|
+
}
|
|
31
|
+
interface LlmResponseLog {
|
|
32
|
+
status: number;
|
|
33
|
+
responseLength: number;
|
|
34
|
+
headers: Record<string, string>;
|
|
35
|
+
usage?: {
|
|
36
|
+
promptTokens: number;
|
|
37
|
+
completionTokens: number;
|
|
38
|
+
totalTokens: number;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export declare const logLlmCallStart: (logger: Logger, spanId: string, service: string, method: string, request: LlmRequestLog) => void;
|
|
42
|
+
export declare const logLlmCallComplete: (logger: Logger, spanId: string, service: string, method: string, response: LlmResponseLog, duration: number) => void;
|
|
43
|
+
export declare const logLlmCallError: (logger: Logger, spanId: string, service: string, method: string, error: unknown, duration: number) => void;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type NuaError = NuaInternalError | NuaValidationError;
|
|
2
|
+
export type NuaValidationError = {
|
|
3
|
+
kind: "validation-error";
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const isNuaValidationError: (a: unknown) => a is NuaValidationError;
|
|
7
|
+
export type NuaInternalError = {
|
|
8
|
+
kind: "internal-error";
|
|
9
|
+
message: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const isNuaInternalError: (a: unknown) => a is NuaInternalError;
|
|
12
|
+
export declare const isNuaError: (a: unknown) => a is NuaError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type JsonSchema = Record<string, unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function stableStringify(value: unknown): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { JsonSchema } from "./schema-utils";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a schema to OpenAI strict mode, or returns null when that would
|
|
4
|
+
* change which values are valid (optional properties, open-ended objects,
|
|
5
|
+
* unsupported keywords).
|
|
6
|
+
*/
|
|
7
|
+
export declare function toStrictSchema(schema: JsonSchema): JsonSchema | null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type UserDataPKValue = string | number;
|
|
2
|
+
export type MappableInputDataRow = Record<string, unknown>;
|
|
3
|
+
export type MappableInputData = MappableInputDataRow[];
|
|
4
|
+
export type NormalizedUsage = {
|
|
5
|
+
promptTokens: number;
|
|
6
|
+
completionTokens: number;
|
|
7
|
+
totalTokens: number;
|
|
8
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { NuaValidationError } from "./nua-errors";
|
|
2
|
+
export type MappableInputDataRow = Record<string, unknown>;
|
|
3
|
+
export type MappableInputData = MappableInputDataRow[];
|
|
4
|
+
export declare function validateMappableInputData(data: unknown, primaryKey: string | null): NuaValidationError | MappableInputData;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { findLocalAgent, localAgent } from "./modules/engine/local-agent/create-local-agent";
|
|
2
|
+
export type { LocalAgentAuthMode, LocalAgentConfig, LocalAgentOptions, } from "./modules/engine/local-agent/create-local-agent";
|
|
3
|
+
export type { LocalAgent } from "./modules/engine/local-agent/local-agent";
|
|
4
|
+
export { detectLocalAgents } from "./modules/engine/local-agent/detect";
|
|
5
|
+
export type { LocalAgentStatus } from "./modules/engine/local-agent/detect";
|
|
6
|
+
export { LOCAL_AGENT_IDS, isLocalAgentId } from "./modules/engine/local-agent/agent-id";
|
|
7
|
+
export type { LocalAgentId } from "./modules/engine/local-agent/agent-id";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AgentEventHandler, AgentResult, AgentTool, AgenticParsedResponse, ConversationMessage } from "./types";
|
|
2
|
+
export type SendAgenticRequestFn = (messages: ConversationMessage[], tools: AgentTool[], systemPrompt?: string) => Promise<AgenticParsedResponse>;
|
|
3
|
+
export declare function runAgentLoop(params: {
|
|
4
|
+
messages: ConversationMessage[];
|
|
5
|
+
tools: AgentTool[];
|
|
6
|
+
systemPrompt?: string;
|
|
7
|
+
maxTurns: number;
|
|
8
|
+
sendRequest: SendAgenticRequestFn;
|
|
9
|
+
onEvent?: AgentEventHandler;
|
|
10
|
+
}): Promise<AgentResult>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Logger } from "../../lib/logger";
|
|
2
|
+
import { ProviderTarget } from "../engine/http/provider-call";
|
|
3
|
+
import { AgentEventHandler, AgenticParsedResponse, ConversationMessage, ToolDefinition } from "./types";
|
|
4
|
+
export type AgentTurn = {
|
|
5
|
+
target: ProviderTarget;
|
|
6
|
+
maxTokens: number;
|
|
7
|
+
messages: ConversationMessage[];
|
|
8
|
+
tools: ToolDefinition[];
|
|
9
|
+
systemPrompt?: string;
|
|
10
|
+
/** When set, the reply is streamed and reported through it as it arrives. */
|
|
11
|
+
onEvent?: AgentEventHandler;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Sends one turn of a tool-calling conversation to an LLM provider's HTTP API.
|
|
15
|
+
* Throws when the call fails; the agent loop reports that as the end of the conversation.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sendAgentTurn(turn: AgentTurn, logger: Logger): Promise<AgenticParsedResponse>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ProviderRequestBase } from "../engine/http/provider-config";
|
|
2
|
+
import { AgentEventHandler, AgenticParsedResponse, ConversationMessage, ToolDefinition } from "./types";
|
|
3
|
+
export declare function buildOpenAiAgenticRequest(options: {
|
|
4
|
+
messages: ConversationMessage[];
|
|
5
|
+
tools: ToolDefinition[];
|
|
6
|
+
model: string;
|
|
7
|
+
maxTokens: number;
|
|
8
|
+
systemPrompt?: string;
|
|
9
|
+
stream?: boolean;
|
|
10
|
+
}, baseUrl: string, apiKey: string): ProviderRequestBase;
|
|
11
|
+
export declare function parseOpenAiAgenticResponse(response: Response, errorLabel: string): Promise<AgenticParsedResponse>;
|
|
12
|
+
export declare function buildGeminiAgenticRequest(options: {
|
|
13
|
+
messages: ConversationMessage[];
|
|
14
|
+
tools: ToolDefinition[];
|
|
15
|
+
model: string;
|
|
16
|
+
maxTokens: number;
|
|
17
|
+
systemPrompt?: string;
|
|
18
|
+
stream?: boolean;
|
|
19
|
+
}, apiKey: string): ProviderRequestBase;
|
|
20
|
+
export declare function parseGeminiAgenticResponse(response: Response): Promise<AgenticParsedResponse>;
|
|
21
|
+
export declare function streamOpenAiAgenticResponse(response: Response, errorLabel: string, onEvent?: AgentEventHandler): Promise<AgenticParsedResponse>;
|
|
22
|
+
export declare function streamGeminiAgenticResponse(response: Response, onEvent?: AgentEventHandler): Promise<AgenticParsedResponse>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ProviderEngines } from "../engine/http/provider-engines";
|
|
2
|
+
import { AgentResult, AgentRunParams } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Runs a multi-turn, tool-calling conversation on an LLM provider's API. It needs
|
|
5
|
+
* the providers' API directly, so it takes provider engines rather than any engine router.
|
|
6
|
+
*/
|
|
7
|
+
export declare function runAgent(providers: ProviderEngines, params: AgentRunParams): Promise<AgentResult>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { NormalizedUsage } from "../engine/http/provider-config";
|
|
2
|
+
import { ModelInput } from "../model-info";
|
|
3
|
+
export type ToolDefinition = {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
parameters: Record<string, unknown>;
|
|
7
|
+
};
|
|
8
|
+
export type ToolExecutionResult = {
|
|
9
|
+
content: string;
|
|
10
|
+
isError?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type AgentTool = ToolDefinition & {
|
|
13
|
+
execute: (args: Record<string, unknown>) => Promise<ToolExecutionResult>;
|
|
14
|
+
};
|
|
15
|
+
export type TextContent = {
|
|
16
|
+
type: "text";
|
|
17
|
+
text: string;
|
|
18
|
+
};
|
|
19
|
+
export type ToolCallContent = {
|
|
20
|
+
type: "toolCall";
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
arguments: Record<string, unknown>;
|
|
24
|
+
};
|
|
25
|
+
export type AssistantContentBlock = TextContent | ToolCallContent;
|
|
26
|
+
export type UserMessage = {
|
|
27
|
+
role: "user";
|
|
28
|
+
content: string;
|
|
29
|
+
};
|
|
30
|
+
export type AssistantMessage = {
|
|
31
|
+
role: "assistant";
|
|
32
|
+
content: AssistantContentBlock[];
|
|
33
|
+
};
|
|
34
|
+
export type ToolResultMessage = {
|
|
35
|
+
role: "toolResult";
|
|
36
|
+
toolCallId: string;
|
|
37
|
+
toolName: string;
|
|
38
|
+
content: string;
|
|
39
|
+
isError: boolean;
|
|
40
|
+
};
|
|
41
|
+
export type ConversationMessage = UserMessage | AssistantMessage | ToolResultMessage;
|
|
42
|
+
export type AgenticParsedResponse = {
|
|
43
|
+
message: AssistantMessage;
|
|
44
|
+
usage: NormalizedUsage;
|
|
45
|
+
stopReason: "stop" | "tool_use";
|
|
46
|
+
};
|
|
47
|
+
export type AgentEvent = {
|
|
48
|
+
type: "turn_start";
|
|
49
|
+
turn: number;
|
|
50
|
+
} | {
|
|
51
|
+
type: "text_delta";
|
|
52
|
+
text: string;
|
|
53
|
+
} | {
|
|
54
|
+
type: "response_complete";
|
|
55
|
+
message: AssistantMessage;
|
|
56
|
+
usage: NormalizedUsage;
|
|
57
|
+
stopReason: "stop" | "tool_use";
|
|
58
|
+
} | {
|
|
59
|
+
type: "tool_start";
|
|
60
|
+
toolCallId: string;
|
|
61
|
+
toolName: string;
|
|
62
|
+
arguments: Record<string, unknown>;
|
|
63
|
+
} | {
|
|
64
|
+
type: "tool_complete";
|
|
65
|
+
toolCallId: string;
|
|
66
|
+
toolName: string;
|
|
67
|
+
result: ToolExecutionResult;
|
|
68
|
+
} | {
|
|
69
|
+
type: "error";
|
|
70
|
+
error: string;
|
|
71
|
+
};
|
|
72
|
+
export type AgentEventHandler = (event: AgentEvent) => void;
|
|
73
|
+
export type AgentRunParams = {
|
|
74
|
+
model?: ModelInput;
|
|
75
|
+
systemPrompt?: string;
|
|
76
|
+
messages: ConversationMessage[];
|
|
77
|
+
tools: AgentTool[];
|
|
78
|
+
maxTokens?: number;
|
|
79
|
+
maxTurns?: number;
|
|
80
|
+
onEvent?: AgentEventHandler;
|
|
81
|
+
};
|
|
82
|
+
export type AgentCompletionReason = "stop" | "max_turns" | "error";
|
|
83
|
+
export type AgentResult = {
|
|
84
|
+
success: boolean;
|
|
85
|
+
completionReason: AgentCompletionReason;
|
|
86
|
+
messages: ConversationMessage[];
|
|
87
|
+
textResponse?: string;
|
|
88
|
+
usage: NormalizedUsage;
|
|
89
|
+
error?: string;
|
|
90
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { JsonSchema } from "../../lib/schema-utils";
|
|
2
|
+
import { MappableInputData } from "../../lib/types";
|
|
3
|
+
/** A cast ready to run, as plain data. Its prompt is rendered per attempt (see prompts/render-prompt). */
|
|
4
|
+
export type CastRequest = ValueCastRequest | ListCastRequest;
|
|
5
|
+
/** Asks for one value. */
|
|
6
|
+
export type ValueCastRequest = {
|
|
7
|
+
kind: "value";
|
|
8
|
+
/** The caller's instructions. */
|
|
9
|
+
prompt: string;
|
|
10
|
+
data: unknown;
|
|
11
|
+
outputName: string;
|
|
12
|
+
/** The schema the result must satisfy. */
|
|
13
|
+
schema: JsonSchema;
|
|
14
|
+
maxTokens: number;
|
|
15
|
+
};
|
|
16
|
+
/** Asks for one result row per input row, each keyed by the input row's primary key. */
|
|
17
|
+
export type ListCastRequest = {
|
|
18
|
+
kind: "list";
|
|
19
|
+
/** The caller's instructions. */
|
|
20
|
+
prompt: string;
|
|
21
|
+
rows: MappableInputData;
|
|
22
|
+
primaryKey: string;
|
|
23
|
+
outputName: string;
|
|
24
|
+
/** The schema the whole list must satisfy (see list-schema). */
|
|
25
|
+
schema: JsonSchema;
|
|
26
|
+
maxTokens: number;
|
|
27
|
+
};
|
|
28
|
+
/** The prompt one attempt sends. `system` is the part that tells the model how to shape its answer. */
|
|
29
|
+
export type RenderedPrompt = {
|
|
30
|
+
system: string;
|
|
31
|
+
full: string;
|
|
32
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JsonSchema } from "../../lib/schema-utils";
|
|
2
|
+
export declare const ENVELOPE_KEY = "value";
|
|
3
|
+
export declare function wrapInEnvelope(schema: JsonSchema): JsonSchema;
|
|
4
|
+
export declare function unwrapEnvelope(envelope: unknown): {
|
|
5
|
+
found: true;
|
|
6
|
+
value: unknown;
|
|
7
|
+
} | {
|
|
8
|
+
found: false;
|
|
9
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { NuaValidationError } from "../../lib/nua-errors";
|
|
2
|
+
import { JsonSchema } from "../../lib/schema-utils";
|
|
3
|
+
declare const listSchemaBrand: unique symbol;
|
|
4
|
+
/** Names the two fields of every row in a list result. */
|
|
5
|
+
export type ListRowKeys = {
|
|
6
|
+
/** Holds the primary key of the input row the result row answers. */
|
|
7
|
+
primaryKey: string;
|
|
8
|
+
/** Holds the output value. */
|
|
9
|
+
outputName: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The schema of a list cast's result, with the row keys it was built for. Only
|
|
13
|
+
* listSchema() and parseListSchema() make one, so a castArray call cannot be
|
|
14
|
+
* given an item schema by mistake.
|
|
15
|
+
*/
|
|
16
|
+
export type ListSchema = ListRowKeys & {
|
|
17
|
+
readonly [listSchemaBrand]: true;
|
|
18
|
+
/** The JSON schema of the whole list, as sent to the model and validated against. */
|
|
19
|
+
jsonSchema: JsonSchema;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Builds the list schema whose rows each hold one `itemSchema` value:
|
|
23
|
+
*
|
|
24
|
+
* array<{ <primaryKey>: string | number, <outputName>: <itemSchema> }>
|
|
25
|
+
*/
|
|
26
|
+
export declare function listSchema(itemSchema: JsonSchema, keys: ListRowKeys): ListSchema;
|
|
27
|
+
/**
|
|
28
|
+
* Reads back a list schema that was built by listSchema() and stored as JSON. It is
|
|
29
|
+
* used exactly as stored, so a change to listSchema() does not alter stored ones.
|
|
30
|
+
*/
|
|
31
|
+
export declare function parseListSchema(jsonSchema: unknown, keys: ListRowKeys): ListSchema | NuaValidationError;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SchemaEnforcementKind } from "../../engine/llm-engine";
|
|
2
|
+
import { CastRequest, RenderedPrompt } from "../cast-request";
|
|
3
|
+
/**
|
|
4
|
+
* The prompt for one attempt at a cast. It differs by enforcement: with native
|
|
5
|
+
* enforcement the model is told its answer goes in an envelope field.
|
|
6
|
+
*/
|
|
7
|
+
export declare function renderPrompt(cast: CastRequest, enforcement: SchemaEnforcementKind): RenderedPrompt;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Logger } from "../../lib/logger";
|
|
2
|
+
import { NormalizedUsage } from "../engine/http/provider-config";
|
|
3
|
+
import { AnswerOrigin, AttemptOutcome, LlmEngine, SchemaEnforcementKind } from "../engine/llm-engine";
|
|
4
|
+
import { ValidationResult } from "../json-schema-validation/nua-json-schema-value-validation";
|
|
5
|
+
import { CastRequest, RenderedPrompt } from "./cast-request";
|
|
6
|
+
export type CastSuccess<T> = {
|
|
7
|
+
success: true;
|
|
8
|
+
data: T;
|
|
9
|
+
/** Usage of the attempt that succeeded. */
|
|
10
|
+
usage: NormalizedUsage;
|
|
11
|
+
/** The prompt of the attempt that succeeded. */
|
|
12
|
+
prompt: RenderedPrompt;
|
|
13
|
+
origin: AnswerOrigin;
|
|
14
|
+
schemaEnforcement: SchemaEnforcementKind;
|
|
15
|
+
};
|
|
16
|
+
export type CastFailure = {
|
|
17
|
+
success: false;
|
|
18
|
+
error: string;
|
|
19
|
+
/** The prompt of the last attempt, when one was made. */
|
|
20
|
+
prompt?: RenderedPrompt;
|
|
21
|
+
};
|
|
22
|
+
export type CastResult<T> = CastSuccess<T> | CastFailure;
|
|
23
|
+
/**
|
|
24
|
+
* Runs a cast on an engine: asks for an answer, checks it against the schema,
|
|
25
|
+
* and tries again when another attempt could help.
|
|
26
|
+
*/
|
|
27
|
+
export declare function runCast<T>(engine: LlmEngine, request: CastRequest, logger: Logger): Promise<CastResult<T>>;
|
|
28
|
+
export type Verdict = {
|
|
29
|
+
kind: "accept";
|
|
30
|
+
data: unknown;
|
|
31
|
+
usage: NormalizedUsage;
|
|
32
|
+
origin: AnswerOrigin;
|
|
33
|
+
} | {
|
|
34
|
+
kind: "retry";
|
|
35
|
+
error: string;
|
|
36
|
+
} | {
|
|
37
|
+
kind: "retry-in-prompt";
|
|
38
|
+
error: string;
|
|
39
|
+
} | {
|
|
40
|
+
kind: "give-up";
|
|
41
|
+
error: string;
|
|
42
|
+
};
|
|
43
|
+
/** Decides what one attempt means for the cast. */
|
|
44
|
+
export declare function judgeOutcome(outcome: AttemptOutcome, validate: (data: object) => ValidationResult): Verdict;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Logger } from "../../lib/logger";
|
|
2
|
+
import { ModelInput } from "../model-info";
|
|
3
|
+
import { LlmEngine } from "./llm-engine";
|
|
4
|
+
/** The engine that runs a model choice, or why none can. */
|
|
5
|
+
export type Route = {
|
|
6
|
+
kind: "routed";
|
|
7
|
+
engine: LlmEngine;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "unroutable";
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Where a client's calls run. Every setup answers the same question: for this
|
|
14
|
+
* model choice, which engine runs it? Created by providerEngines() for LLM
|
|
15
|
+
* providers' APIs, or by localAgent() / findLocalAgent() in the Node-only
|
|
16
|
+
* "nua-llm-core/local-agent" entry point for a coding-agent CLI.
|
|
17
|
+
*/
|
|
18
|
+
export interface EngineRouter {
|
|
19
|
+
/** Where the engines log their calls. Casts log their retries here too. */
|
|
20
|
+
readonly logger: Logger;
|
|
21
|
+
/** `model` undefined means this router's default model. */
|
|
22
|
+
route(model: ModelInput | undefined): Route;
|
|
23
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Logger } from "../../../lib/logger";
|
|
2
|
+
import { AttemptOutcome, AttemptRequest, LlmEngine } from "../llm-engine";
|
|
3
|
+
import { ProviderTarget } from "./provider-call";
|
|
4
|
+
/** Answers cast attempts on one model of an LLM provider's HTTP API. */
|
|
5
|
+
export declare class HttpEngine implements LlmEngine {
|
|
6
|
+
private readonly target;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
constructor(target: ProviderTarget, logger: Logger);
|
|
9
|
+
/** Provider APIs are given the schema in the prompt; none is enforced natively yet. */
|
|
10
|
+
nativeSchema(): null;
|
|
11
|
+
attempt(request: AttemptRequest): Promise<AttemptOutcome>;
|
|
12
|
+
}
|