@aaac/runtime 0.3.1 → 0.3.2
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 +1 -1
- package/dist/adapters/claude-agent-sdk.d.ts +1 -1
- package/dist/adapters/claude-agent-sdk.js +1 -1
- package/dist/adapters/mock.d.ts +1 -1
- package/dist/adapters/openai-agents-sdk.d.ts +1 -1
- package/dist/adapters/openai-agents-sdk.js +2 -2
- package/dist/{chunk-EBMQHLXY.js → chunk-CRKN7CIO.js} +2 -2
- package/dist/{chunk-N2EA3TGS.js → chunk-DNSVEP4U.js} +2 -2
- package/dist/{chunk-BHTHXGA3.js → chunk-DUADHXC2.js} +4 -4
- package/dist/{chunk-KTVMMG33.js → chunk-DX3RJTWJ.js} +2 -17
- package/dist/{chunk-KTVMMG33.js.map → chunk-DX3RJTWJ.js.map} +1 -1
- package/dist/{chunk-GGBNVCPV.js → chunk-EYK4E3XE.js} +3 -3
- package/dist/{chunk-5ZDW4RP7.js → chunk-K5XDDJFK.js} +2 -2
- package/dist/{chunk-2SG3TBI3.js → chunk-QQTFYLUX.js} +2 -2
- package/dist/{chunk-EA4V5DVD.js → chunk-RIXKPQFC.js} +1 -2
- package/dist/{chunk-EA4V5DVD.js.map → chunk-RIXKPQFC.js.map} +1 -1
- package/dist/generator/index.js +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +39 -48
- package/dist/index.js.map +1 -1
- package/dist/lib/claude-native/hooks-lite.js +4 -4
- package/dist/lib/claude-native/hooks-prompt.js +4 -4
- package/dist/lib/claude-native/index.js +7 -7
- package/dist/{task-runner-CgQx1KUq.d.ts → task-runner-Cpr-GTrl.d.ts} +4 -6
- package/package.json +6 -17
- package/dist/adapters/adk-sdk.d.ts +0 -139
- package/dist/adapters/adk-sdk.js +0 -312
- package/dist/adapters/adk-sdk.js.map +0 -1
- /package/dist/{chunk-EBMQHLXY.js.map → chunk-CRKN7CIO.js.map} +0 -0
- /package/dist/{chunk-N2EA3TGS.js.map → chunk-DNSVEP4U.js.map} +0 -0
- /package/dist/{chunk-BHTHXGA3.js.map → chunk-DUADHXC2.js.map} +0 -0
- /package/dist/{chunk-GGBNVCPV.js.map → chunk-EYK4E3XE.js.map} +0 -0
- /package/dist/{chunk-5ZDW4RP7.js.map → chunk-K5XDDJFK.js.map} +0 -0
- /package/dist/{chunk-2SG3TBI3.js.map → chunk-QQTFYLUX.js.map} +0 -0
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
createWorkspacePreToolUseHandler,
|
|
4
4
|
handleFileSuggestion,
|
|
5
5
|
handleWorkspaceFileSuggestion
|
|
6
|
-
} from "../../chunk-
|
|
6
|
+
} from "../../chunk-DUADHXC2.js";
|
|
7
7
|
import {
|
|
8
8
|
DEFAULT_CACHE_KEY,
|
|
9
9
|
buildContextResponse,
|
|
@@ -12,11 +12,11 @@ import {
|
|
|
12
12
|
isCachedRecord,
|
|
13
13
|
readFreshNavigationBuild,
|
|
14
14
|
readHookInput
|
|
15
|
-
} from "../../chunk-
|
|
15
|
+
} from "../../chunk-QQTFYLUX.js";
|
|
16
16
|
import {
|
|
17
17
|
listArtifacts
|
|
18
|
-
} from "../../chunk-
|
|
19
|
-
import "../../chunk-
|
|
18
|
+
} from "../../chunk-CRKN7CIO.js";
|
|
19
|
+
import "../../chunk-RIXKPQFC.js";
|
|
20
20
|
import "../../chunk-BJ7B4R26.js";
|
|
21
21
|
export {
|
|
22
22
|
DEFAULT_CACHE_KEY,
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
renderMemberSection,
|
|
7
7
|
renderNavigationDiagnostics,
|
|
8
8
|
runEnsureNavigationIndexBin
|
|
9
|
-
} from "../../chunk-
|
|
9
|
+
} from "../../chunk-EYK4E3XE.js";
|
|
10
10
|
import {
|
|
11
11
|
isNavigationIndex,
|
|
12
12
|
renderNavigationIndexPrompt
|
|
@@ -14,9 +14,9 @@ import {
|
|
|
14
14
|
import {
|
|
15
15
|
emitHookOutput,
|
|
16
16
|
readHookInput
|
|
17
|
-
} from "../../chunk-
|
|
18
|
-
import "../../chunk-
|
|
19
|
-
import "../../chunk-
|
|
17
|
+
} from "../../chunk-QQTFYLUX.js";
|
|
18
|
+
import "../../chunk-CRKN7CIO.js";
|
|
19
|
+
import "../../chunk-RIXKPQFC.js";
|
|
20
20
|
import "../../chunk-BJ7B4R26.js";
|
|
21
21
|
export {
|
|
22
22
|
createUserPromptSubmitHandler,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildNavigationContext,
|
|
3
3
|
resolveNavigationSources
|
|
4
|
-
} from "../../chunk-
|
|
4
|
+
} from "../../chunk-DNSVEP4U.js";
|
|
5
5
|
import "../../chunk-ET6TGQRT.js";
|
|
6
|
-
import "../../chunk-
|
|
6
|
+
import "../../chunk-K5XDDJFK.js";
|
|
7
7
|
import {
|
|
8
8
|
createPreToolUseHandler,
|
|
9
9
|
createWorkspacePreToolUseHandler,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
handleWorkspaceFileSuggestion,
|
|
14
14
|
routePathToMember,
|
|
15
15
|
stripMemberPrefix
|
|
16
|
-
} from "../../chunk-
|
|
16
|
+
} from "../../chunk-DUADHXC2.js";
|
|
17
17
|
import {
|
|
18
18
|
createUserPromptSubmitHandler,
|
|
19
19
|
createWorkspaceUserPromptSubmitHandler,
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
renderMemberSection,
|
|
22
22
|
renderNavigationDiagnostics,
|
|
23
23
|
runEnsureNavigationIndexBin
|
|
24
|
-
} from "../../chunk-
|
|
24
|
+
} from "../../chunk-EYK4E3XE.js";
|
|
25
25
|
import {
|
|
26
26
|
renderNavigationIndexPrompt
|
|
27
27
|
} from "../../chunk-3CLWG72V.js";
|
|
@@ -39,13 +39,13 @@ import {
|
|
|
39
39
|
parseHookInput,
|
|
40
40
|
readFreshNavigationBuild,
|
|
41
41
|
readHookInput
|
|
42
|
-
} from "../../chunk-
|
|
42
|
+
} from "../../chunk-QQTFYLUX.js";
|
|
43
43
|
import {
|
|
44
44
|
isProtectedArtifact,
|
|
45
45
|
listArtifacts,
|
|
46
46
|
resolvePathToArtifacts
|
|
47
|
-
} from "../../chunk-
|
|
48
|
-
import "../../chunk-
|
|
47
|
+
} from "../../chunk-CRKN7CIO.js";
|
|
48
|
+
import "../../chunk-RIXKPQFC.js";
|
|
49
49
|
import "../../chunk-BJ7B4R26.js";
|
|
50
50
|
|
|
51
51
|
// src/lib/claude-native/latency.ts
|
|
@@ -23,9 +23,9 @@ interface MemoryRef {
|
|
|
23
23
|
/**
|
|
24
24
|
* Pluggable store for memory content that backs MemoryRef pointers.
|
|
25
25
|
*
|
|
26
|
-
* Adapters that use server-side state (OpenAI
|
|
26
|
+
* Adapters that use server-side state (OpenAI) may not need
|
|
27
27
|
* the store — the ref ID alone is sufficient for resume. Adapters
|
|
28
|
-
* with local state (Claude transcript
|
|
28
|
+
* with local state (Claude transcript) use the
|
|
29
29
|
* store to persist and retrieve content.
|
|
30
30
|
*/
|
|
31
31
|
interface MemoryStore {
|
|
@@ -265,7 +265,6 @@ declare function emitToolchainClose(observer: ObserverEmitFn | undefined, spanId
|
|
|
265
265
|
* Each adapter receives tools in its native format:
|
|
266
266
|
* - Claude Agent SDK: MCP tool via createSdkMcpServer + tool()
|
|
267
267
|
* - OpenAI Agents SDK: function tool object
|
|
268
|
-
* - Google ADK: FunctionDeclaration tool
|
|
269
268
|
*/
|
|
270
269
|
|
|
271
270
|
/**
|
|
@@ -337,7 +336,6 @@ declare function adapterErrorKind(error: unknown): AdapterErrorKind;
|
|
|
337
336
|
*
|
|
338
337
|
* - Claude `Options.agents: Record<string, AgentDefinition>`
|
|
339
338
|
* - OpenAI `Agent.handoffs: Agent[]`
|
|
340
|
-
* - ADK `LlmAgent.subAgents: BaseAgent[]`
|
|
341
339
|
*
|
|
342
340
|
* `CandidateAgent` is the common, SDK-independent contract. Each adapter maps it
|
|
343
341
|
* onto its provider's native shape. The selection rule (which agents become
|
|
@@ -600,7 +598,7 @@ interface AdapterSendOptions {
|
|
|
600
598
|
/**
|
|
601
599
|
* Candidate sub-agents the entry agent's LLM may delegate to (model B,
|
|
602
600
|
* LLM-internal routing). Adapters register these via their native subagent
|
|
603
|
-
* mechanism (Claude `agents`, OpenAI `handoffs
|
|
601
|
+
* mechanism (Claude `agents`, OpenAI `handoffs`). When
|
|
604
602
|
* omitted or empty, adapters behave exactly as before (single agent).
|
|
605
603
|
*/
|
|
606
604
|
agents?: CandidateAgent[];
|
|
@@ -613,7 +611,7 @@ interface AdapterSendOptions {
|
|
|
613
611
|
/**
|
|
614
612
|
* In-process library tools to register with the SDK as function tools.
|
|
615
613
|
* Adapters convert these to their native tool format (Claude MCP, OpenAI
|
|
616
|
-
* function tools
|
|
614
|
+
* function tools). I/O is validated against the tool's schemas.
|
|
617
615
|
*/
|
|
618
616
|
libraryTools?: LibraryToolRegistry;
|
|
619
617
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aaac/runtime",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Runtime SDK for the AaaC toolchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -18,10 +18,6 @@
|
|
|
18
18
|
"types": "./dist/adapters/openai-agents-sdk.d.ts",
|
|
19
19
|
"import": "./dist/adapters/openai-agents-sdk.js"
|
|
20
20
|
},
|
|
21
|
-
"./adapters/adk-sdk": {
|
|
22
|
-
"types": "./dist/adapters/adk-sdk.d.ts",
|
|
23
|
-
"import": "./dist/adapters/adk-sdk.js"
|
|
24
|
-
},
|
|
25
21
|
"./adapters/mock": {
|
|
26
22
|
"types": "./dist/adapters/mock.d.ts",
|
|
27
23
|
"import": "./dist/adapters/mock.js"
|
|
@@ -68,26 +64,19 @@
|
|
|
68
64
|
"node": ">=22.0.0"
|
|
69
65
|
},
|
|
70
66
|
"dependencies": {
|
|
71
|
-
"@aaac/contracts": "^0.3.
|
|
72
|
-
"@aaac/observability": "^0.3.
|
|
73
|
-
"@aaac/toolchain": "^0.3.
|
|
67
|
+
"@aaac/contracts": "^0.3.2",
|
|
68
|
+
"@aaac/observability": "^0.3.2",
|
|
69
|
+
"@aaac/toolchain": "^0.3.2",
|
|
74
70
|
"agent-contracts": "^0.35.9",
|
|
75
|
-
"commander": "^12.1.0",
|
|
76
|
-
"dotenv": "^17.4.2",
|
|
77
|
-
"tsx": "^4.22.0",
|
|
78
71
|
"yaml": "^2.8.3",
|
|
79
72
|
"zod": "^4.6.7"
|
|
80
73
|
},
|
|
81
74
|
"peerDependencies": {
|
|
82
75
|
"@anthropic-ai/claude-agent-sdk": ">=0.2.0",
|
|
83
|
-
"@google/adk": ">=2.2.0",
|
|
84
76
|
"@openai/agents": ">=0.10.0",
|
|
85
|
-
"artifact-contracts-analyzer": ">=0.3.
|
|
77
|
+
"artifact-contracts-analyzer": ">=0.3.11"
|
|
86
78
|
},
|
|
87
79
|
"peerDependenciesMeta": {
|
|
88
|
-
"@google/adk": {
|
|
89
|
-
"optional": true
|
|
90
|
-
},
|
|
91
80
|
"@anthropic-ai/claude-agent-sdk": {
|
|
92
81
|
"optional": true
|
|
93
82
|
},
|
|
@@ -100,7 +89,7 @@
|
|
|
100
89
|
},
|
|
101
90
|
"devDependencies": {
|
|
102
91
|
"@opentelemetry/api": "^1.9.1",
|
|
103
|
-
"artifact-contracts-analyzer": "^0.3.
|
|
92
|
+
"artifact-contracts-analyzer": "^0.3.11",
|
|
104
93
|
"esbuild": "^0.28.0",
|
|
105
94
|
"handlebars": "^4.7.10",
|
|
106
95
|
"tsup": "^8.5.0",
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { S as SdkAdapter, C as CacheConfig, A as AdapterSendOptions, a as AgentExecutionRequest, M as MemoryRef, T as TokenUsage } from '../task-runner-CgQx1KUq.js';
|
|
2
|
-
import { G as GuardrailHooks } from '../guardrail-hooks-BgHe8PrP.js';
|
|
3
|
-
import 'zod';
|
|
4
|
-
import '@aaac/contracts';
|
|
5
|
-
import '@aaac/observability';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Google ADK Adapter — implements SdkAdapter for @google/adk (ADK-TS / adk-js)
|
|
9
|
-
*
|
|
10
|
-
* Replaces the raw @google/genai adapter. ADK provides a native subagent
|
|
11
|
-
* mechanism (`LlmAgent.subAgents` + description-based agent transfer), which is
|
|
12
|
-
* required for model B (LLM-internal routing): the entry agent's LLM delegates
|
|
13
|
-
* to candidate sub-agents registered in the agent tree.
|
|
14
|
-
*
|
|
15
|
-
* Supports:
|
|
16
|
-
* - send() — build a root LlmAgent (+ candidate subAgents), run once,
|
|
17
|
-
* return the final response text
|
|
18
|
-
* - sendExecution() — rich path using full contract context
|
|
19
|
-
*
|
|
20
|
-
* No followUp(): ADK's InMemoryRunner uses ephemeral sessions, so the runtime's
|
|
21
|
-
* recovery loop falls back to a fresh send() with reconstructed context.
|
|
22
|
-
*
|
|
23
|
-
* API used (verified against the installed package's type definitions under
|
|
24
|
-
* node_modules/@google/adk/dist/types):
|
|
25
|
-
* - class LlmAgent (agents/llm_agent.d.ts): constructor(config) where config
|
|
26
|
-
* extends BaseAgentConfig { name; description?; subAgents?: BaseAgent[] }
|
|
27
|
-
* and adds { model?: string | BaseLlm; instruction?: string; tools? }.
|
|
28
|
-
* - class InMemoryRunner (runner/in_memory_runner.d.ts): constructor({ agent,
|
|
29
|
-
* appName? }); inherits runEphemeral({ userId, newMessage: Content })
|
|
30
|
-
* returning AsyncGenerator<Event> (runner/runner.d.ts).
|
|
31
|
-
* - isFinalResponse(event) / stringifyContent(event) (events/event.d.ts).
|
|
32
|
-
* - class Gemini (models/google_llm.d.ts): constructor({ model, apiKey })
|
|
33
|
-
* — used to inject an explicit API key; otherwise ADK reads
|
|
34
|
-
* GOOGLE_GENAI_API_KEY / GEMINI_API_KEY from the environment.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
type AdkContentPart = {
|
|
38
|
-
text?: string;
|
|
39
|
-
functionCall?: {
|
|
40
|
-
name?: string;
|
|
41
|
-
args?: Record<string, unknown>;
|
|
42
|
-
};
|
|
43
|
-
functionResponse?: {
|
|
44
|
-
name?: string;
|
|
45
|
-
response?: unknown;
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
type AdkContent = {
|
|
49
|
-
role?: string;
|
|
50
|
-
parts?: AdkContentPart[];
|
|
51
|
-
};
|
|
52
|
-
type AdkEvent = {
|
|
53
|
-
author?: string;
|
|
54
|
-
content?: AdkContent;
|
|
55
|
-
usageMetadata?: {
|
|
56
|
-
promptTokenCount?: number;
|
|
57
|
-
candidatesTokenCount?: number;
|
|
58
|
-
totalTokenCount?: number;
|
|
59
|
-
cachedContentTokenCount?: number;
|
|
60
|
-
};
|
|
61
|
-
[key: string]: unknown;
|
|
62
|
-
};
|
|
63
|
-
type LlmAgentConfig = {
|
|
64
|
-
name: string;
|
|
65
|
-
description?: string;
|
|
66
|
-
model?: string | object;
|
|
67
|
-
instruction?: string;
|
|
68
|
-
tools?: unknown[];
|
|
69
|
-
subAgents?: unknown[];
|
|
70
|
-
};
|
|
71
|
-
type LlmAgentInstance = object;
|
|
72
|
-
type LlmAgentConstructor = new (config: LlmAgentConfig) => LlmAgentInstance;
|
|
73
|
-
type RunnerInstance = {
|
|
74
|
-
runEphemeral(params: {
|
|
75
|
-
userId: string;
|
|
76
|
-
newMessage: AdkContent;
|
|
77
|
-
stateDelta?: Record<string, unknown>;
|
|
78
|
-
}): AsyncGenerator<AdkEvent, void, unknown>;
|
|
79
|
-
};
|
|
80
|
-
type InMemoryRunnerConstructor = new (params: {
|
|
81
|
-
agent: LlmAgentInstance;
|
|
82
|
-
appName?: string;
|
|
83
|
-
}) => RunnerInstance;
|
|
84
|
-
type GeminiConstructor = new (params: {
|
|
85
|
-
model?: string;
|
|
86
|
-
apiKey?: string;
|
|
87
|
-
}) => object;
|
|
88
|
-
interface AdkSdk {
|
|
89
|
-
LlmAgent: LlmAgentConstructor;
|
|
90
|
-
InMemoryRunner: InMemoryRunnerConstructor;
|
|
91
|
-
isFinalResponse: (event: AdkEvent) => boolean;
|
|
92
|
-
stringifyContent: (event: AdkEvent) => string;
|
|
93
|
-
Gemini: GeminiConstructor;
|
|
94
|
-
}
|
|
95
|
-
interface AdkSdkAdapterConfig {
|
|
96
|
-
/** API key for the Gemini API. Falls back to GOOGLE_GENAI_API_KEY / GEMINI_API_KEY env. */
|
|
97
|
-
apiKey?: string;
|
|
98
|
-
/** Gemini model identifier (e.g. "gemini-2.5-flash", "gemini-2.5-pro"). */
|
|
99
|
-
model?: string;
|
|
100
|
-
/** Root agent name (must be a JS identifier). Defaults to "root_agent". */
|
|
101
|
-
rootAgentName?: string;
|
|
102
|
-
/** Runtime guardrail hooks. Applied as input-level checks before agent execution. */
|
|
103
|
-
guardrailHooks?: GuardrailHooks;
|
|
104
|
-
/** Prompt caching configuration. Enabled by default. */
|
|
105
|
-
cacheConfig?: CacheConfig;
|
|
106
|
-
}
|
|
107
|
-
declare class AdkSdkAdapter implements SdkAdapter {
|
|
108
|
-
private apiKey;
|
|
109
|
-
private model;
|
|
110
|
-
private rootAgentName;
|
|
111
|
-
private guardrailHooks;
|
|
112
|
-
private cacheConfig;
|
|
113
|
-
private lastMemoryRef;
|
|
114
|
-
private lastTokenUsage;
|
|
115
|
-
private runCounter;
|
|
116
|
-
private sdk;
|
|
117
|
-
constructor(config?: AdkSdkAdapterConfig);
|
|
118
|
-
private resolveSdk;
|
|
119
|
-
/** Resolve the `model` field for LlmAgent — a Gemini instance when an explicit
|
|
120
|
-
* API key is supplied, otherwise the bare model id (ADK reads env keys). */
|
|
121
|
-
private resolveModel;
|
|
122
|
-
/** Build the root LlmAgent with candidate sub-agents registered for transfer. */
|
|
123
|
-
private buildRootAgent;
|
|
124
|
-
private resolveLibraryTools;
|
|
125
|
-
private runAgent;
|
|
126
|
-
private nextMemoryRef;
|
|
127
|
-
send(prompt: string, options: AdapterSendOptions): Promise<string>;
|
|
128
|
-
sendExecution(request: AgentExecutionRequest): Promise<string>;
|
|
129
|
-
getLastMemoryRef(): MemoryRef | null;
|
|
130
|
-
getLastTokenUsage(): TokenUsage | null;
|
|
131
|
-
isCompatible(compat: string): boolean;
|
|
132
|
-
/**
|
|
133
|
-
* Inject the resolved ADK SDK surface for testing.
|
|
134
|
-
* Bypasses the dynamic import of @google/adk and the API key requirement.
|
|
135
|
-
*/
|
|
136
|
-
static withSdk(sdk: AdkSdk, config?: AdkSdkAdapterConfig): AdkSdkAdapter;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export { AdkSdkAdapter, type AdkSdkAdapterConfig };
|
package/dist/adapters/adk-sdk.js
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildAdkFunctionTools,
|
|
3
|
-
emitLlmTurnClose,
|
|
4
|
-
emitLlmTurnOpen,
|
|
5
|
-
emitToolExecClose,
|
|
6
|
-
emitToolExecOpen
|
|
7
|
-
} from "../chunk-KTVMMG33.js";
|
|
8
|
-
import {
|
|
9
|
-
getBlockingViolations,
|
|
10
|
-
renderViolations
|
|
11
|
-
} from "../chunk-EA4V5DVD.js";
|
|
12
|
-
import "../chunk-BJ7B4R26.js";
|
|
13
|
-
|
|
14
|
-
// src/adapters/adk-sdk.ts
|
|
15
|
-
function sanitizeAgentName(name) {
|
|
16
|
-
let id = name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
17
|
-
if (id.length === 0 || /^[0-9]/.test(id)) id = `agent_${id}`;
|
|
18
|
-
if (id === "user") id = "user_agent";
|
|
19
|
-
return id;
|
|
20
|
-
}
|
|
21
|
-
function extractEventText(event) {
|
|
22
|
-
const parts = event.content?.parts;
|
|
23
|
-
if (!parts) return "";
|
|
24
|
-
return parts.map((p) => p.text ?? "").join("");
|
|
25
|
-
}
|
|
26
|
-
function truncateInputSummary(input, maxLen = 80) {
|
|
27
|
-
if (input == null) return "";
|
|
28
|
-
let str;
|
|
29
|
-
if (typeof input === "string") {
|
|
30
|
-
str = input;
|
|
31
|
-
} else {
|
|
32
|
-
try {
|
|
33
|
-
str = JSON.stringify(input);
|
|
34
|
-
} catch {
|
|
35
|
-
str = String(input);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return str.length > maxLen ? str.slice(0, maxLen - 3) + "..." : str;
|
|
39
|
-
}
|
|
40
|
-
var AdkSdkAdapter = class _AdkSdkAdapter {
|
|
41
|
-
apiKey;
|
|
42
|
-
model;
|
|
43
|
-
rootAgentName;
|
|
44
|
-
guardrailHooks;
|
|
45
|
-
cacheConfig;
|
|
46
|
-
lastMemoryRef = null;
|
|
47
|
-
lastTokenUsage = null;
|
|
48
|
-
runCounter = 0;
|
|
49
|
-
sdk = null;
|
|
50
|
-
constructor(config = {}) {
|
|
51
|
-
this.apiKey = config.apiKey;
|
|
52
|
-
this.model = config.model ?? "gemini-2.5-flash";
|
|
53
|
-
this.rootAgentName = config.rootAgentName ?? "root_agent";
|
|
54
|
-
this.guardrailHooks = config.guardrailHooks;
|
|
55
|
-
this.cacheConfig = config.cacheConfig ?? { enabled: true };
|
|
56
|
-
}
|
|
57
|
-
// -------------------------------------------------------------------------
|
|
58
|
-
// Internal helpers
|
|
59
|
-
// -------------------------------------------------------------------------
|
|
60
|
-
async resolveSdk() {
|
|
61
|
-
if (this.sdk) return this.sdk;
|
|
62
|
-
const mod = await import("@google/adk");
|
|
63
|
-
this.sdk = {
|
|
64
|
-
LlmAgent: mod.LlmAgent,
|
|
65
|
-
InMemoryRunner: mod.InMemoryRunner,
|
|
66
|
-
isFinalResponse: mod.isFinalResponse,
|
|
67
|
-
stringifyContent: mod.stringifyContent,
|
|
68
|
-
Gemini: mod.Gemini
|
|
69
|
-
};
|
|
70
|
-
return this.sdk;
|
|
71
|
-
}
|
|
72
|
-
/** Resolve the `model` field for LlmAgent — a Gemini instance when an explicit
|
|
73
|
-
* API key is supplied, otherwise the bare model id (ADK reads env keys). */
|
|
74
|
-
resolveModel(sdk) {
|
|
75
|
-
if (this.apiKey) {
|
|
76
|
-
return new sdk.Gemini({ model: this.model, apiKey: this.apiKey });
|
|
77
|
-
}
|
|
78
|
-
return this.model;
|
|
79
|
-
}
|
|
80
|
-
/** Build the root LlmAgent with candidate sub-agents registered for transfer. */
|
|
81
|
-
buildRootAgent(sdk, instruction, agents, extraTools) {
|
|
82
|
-
const subAgents = (agents ?? []).map((a) => {
|
|
83
|
-
const config = {
|
|
84
|
-
name: sanitizeAgentName(a.name),
|
|
85
|
-
description: a.description,
|
|
86
|
-
instruction: a.prompt,
|
|
87
|
-
model: a.model ?? this.resolveModel(sdk)
|
|
88
|
-
};
|
|
89
|
-
if (a.tools) config.tools = a.tools;
|
|
90
|
-
return new sdk.LlmAgent(config);
|
|
91
|
-
});
|
|
92
|
-
const rootConfig = {
|
|
93
|
-
name: sanitizeAgentName(this.rootAgentName),
|
|
94
|
-
instruction,
|
|
95
|
-
model: this.resolveModel(sdk)
|
|
96
|
-
};
|
|
97
|
-
if (subAgents.length > 0) rootConfig.subAgents = subAgents;
|
|
98
|
-
if (extraTools && extraTools.length > 0) rootConfig.tools = extraTools;
|
|
99
|
-
return new sdk.LlmAgent(rootConfig);
|
|
100
|
-
}
|
|
101
|
-
async resolveLibraryTools(libraryTools) {
|
|
102
|
-
if (!libraryTools || libraryTools.getAll().length === 0) return [];
|
|
103
|
-
try {
|
|
104
|
-
return await buildAdkFunctionTools(libraryTools);
|
|
105
|
-
} catch {
|
|
106
|
-
return [];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async runAgent(instruction, userMessage, agents, onProgress, libraryTools, observer, parentSpanId, agentSessionId) {
|
|
110
|
-
if (this.guardrailHooks) {
|
|
111
|
-
const results = this.guardrailHooks.runChecks({ command: userMessage });
|
|
112
|
-
const blocking = getBlockingViolations(results);
|
|
113
|
-
if (blocking.length > 0) {
|
|
114
|
-
throw new Error(renderViolations(blocking));
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
const sdk = await this.resolveSdk();
|
|
118
|
-
const extraTools = await this.resolveLibraryTools(libraryTools);
|
|
119
|
-
const root = this.buildRootAgent(sdk, instruction, agents, extraTools);
|
|
120
|
-
const runner = new sdk.InMemoryRunner({ agent: root });
|
|
121
|
-
let finalText = "";
|
|
122
|
-
const stream = runner.runEphemeral({
|
|
123
|
-
userId: "agent-runtime",
|
|
124
|
-
newMessage: { role: "user", parts: [{ text: userMessage }] }
|
|
125
|
-
});
|
|
126
|
-
let turnNumber = 0;
|
|
127
|
-
let currentTurnSpanId;
|
|
128
|
-
let currentTurnStart = 0;
|
|
129
|
-
let toolCallsInTurn = 0;
|
|
130
|
-
let turnInputTokens = 0;
|
|
131
|
-
let turnOutputTokens = 0;
|
|
132
|
-
const openToolSpans = [];
|
|
133
|
-
let totalInputTokens = 0;
|
|
134
|
-
let totalOutputTokens = 0;
|
|
135
|
-
let totalTokens = 0;
|
|
136
|
-
let hasUsageData = false;
|
|
137
|
-
const closePendingToolSpans = () => {
|
|
138
|
-
for (const ts of openToolSpans) {
|
|
139
|
-
emitToolExecClose(observer, ts.spanId, ts.toolName, Date.now() - ts.startMs, agentSessionId);
|
|
140
|
-
}
|
|
141
|
-
openToolSpans.length = 0;
|
|
142
|
-
};
|
|
143
|
-
const closeCurrentTurn = () => {
|
|
144
|
-
if (currentTurnSpanId) {
|
|
145
|
-
emitLlmTurnClose(observer, currentTurnSpanId, turnNumber, Date.now() - currentTurnStart, {
|
|
146
|
-
inputTokens: turnInputTokens || void 0,
|
|
147
|
-
outputTokens: turnOutputTokens || void 0,
|
|
148
|
-
toolCallsCount: toolCallsInTurn || void 0
|
|
149
|
-
}, agentSessionId);
|
|
150
|
-
currentTurnSpanId = void 0;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
const captureUsage = (usageMetadata) => {
|
|
154
|
-
const inTok = typeof usageMetadata.promptTokenCount === "number" ? usageMetadata.promptTokenCount : 0;
|
|
155
|
-
const outTok = typeof usageMetadata.candidatesTokenCount === "number" ? usageMetadata.candidatesTokenCount : 0;
|
|
156
|
-
if (inTok > 0 || outTok > 0) {
|
|
157
|
-
totalInputTokens += inTok;
|
|
158
|
-
totalOutputTokens += outTok;
|
|
159
|
-
turnInputTokens += inTok;
|
|
160
|
-
turnOutputTokens += outTok;
|
|
161
|
-
hasUsageData = true;
|
|
162
|
-
}
|
|
163
|
-
if (typeof usageMetadata.totalTokenCount === "number") {
|
|
164
|
-
totalTokens += usageMetadata.totalTokenCount;
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
for await (const event of stream) {
|
|
168
|
-
if (onProgress) emitProgressFromAdkEvent(event, onProgress);
|
|
169
|
-
const ev = event;
|
|
170
|
-
if (ev.errorCode || ev.errorMessage) {
|
|
171
|
-
closePendingToolSpans();
|
|
172
|
-
closeCurrentTurn();
|
|
173
|
-
throw new Error(
|
|
174
|
-
`Gemini API error (${ev.errorCode ?? "unknown"}): ${ev.errorMessage ?? "no details"}`
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
const role = event.content?.role;
|
|
178
|
-
const parts = event.content?.parts;
|
|
179
|
-
if (role === "model" && parts && parts.length > 0) {
|
|
180
|
-
closePendingToolSpans();
|
|
181
|
-
closeCurrentTurn();
|
|
182
|
-
turnNumber++;
|
|
183
|
-
toolCallsInTurn = 0;
|
|
184
|
-
turnInputTokens = 0;
|
|
185
|
-
turnOutputTokens = 0;
|
|
186
|
-
currentTurnStart = Date.now();
|
|
187
|
-
currentTurnSpanId = emitLlmTurnOpen(observer, parentSpanId, turnNumber, this.model, void 0, agentSessionId);
|
|
188
|
-
for (const part of parts) {
|
|
189
|
-
if (part.functionCall?.name) {
|
|
190
|
-
toolCallsInTurn++;
|
|
191
|
-
const toolName = part.functionCall.name;
|
|
192
|
-
const summary = truncateInputSummary(part.functionCall.args);
|
|
193
|
-
const toolSpanId = emitToolExecOpen(observer, currentTurnSpanId, toolName, summary, turnNumber, agentSessionId);
|
|
194
|
-
if (toolSpanId) {
|
|
195
|
-
openToolSpans.push({ spanId: toolSpanId, toolName, startMs: Date.now() });
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
} else if (parts) {
|
|
200
|
-
for (const part of parts) {
|
|
201
|
-
if (part.functionResponse?.name) {
|
|
202
|
-
if (openToolSpans.length > 0) {
|
|
203
|
-
const ts = openToolSpans.shift();
|
|
204
|
-
emitToolExecClose(observer, ts.spanId, ts.toolName, Date.now() - ts.startMs, agentSessionId);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
if (event.usageMetadata) {
|
|
210
|
-
captureUsage(event.usageMetadata);
|
|
211
|
-
}
|
|
212
|
-
if (sdk.isFinalResponse(event) && event.content) {
|
|
213
|
-
const text = sdk.stringifyContent(event) || extractEventText(event);
|
|
214
|
-
if (text) finalText = text;
|
|
215
|
-
closePendingToolSpans();
|
|
216
|
-
closeCurrentTurn();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
closePendingToolSpans();
|
|
220
|
-
closeCurrentTurn();
|
|
221
|
-
const tokenUsage = hasUsageData ? {
|
|
222
|
-
inputTokens: totalInputTokens,
|
|
223
|
-
outputTokens: totalOutputTokens,
|
|
224
|
-
totalTokens: totalTokens || totalInputTokens + totalOutputTokens
|
|
225
|
-
} : null;
|
|
226
|
-
return { text: finalText, tokenUsage };
|
|
227
|
-
}
|
|
228
|
-
nextMemoryRef() {
|
|
229
|
-
return {
|
|
230
|
-
id: `adk-run-${++this.runCounter}-${Date.now()}`,
|
|
231
|
-
provider: "google-adk",
|
|
232
|
-
compat: `google-adk@${this.model}`,
|
|
233
|
-
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
// -------------------------------------------------------------------------
|
|
237
|
-
// SdkAdapter interface
|
|
238
|
-
// -------------------------------------------------------------------------
|
|
239
|
-
async send(prompt, options) {
|
|
240
|
-
this.lastMemoryRef = null;
|
|
241
|
-
this.lastTokenUsage = null;
|
|
242
|
-
const split = options.splitPrompt;
|
|
243
|
-
let instruction = "";
|
|
244
|
-
let userMessage;
|
|
245
|
-
if (split && this.cacheConfig.enabled !== false) {
|
|
246
|
-
instruction = split.system.join("\n\n---\n\n");
|
|
247
|
-
userMessage = split.user;
|
|
248
|
-
} else {
|
|
249
|
-
userMessage = prompt;
|
|
250
|
-
}
|
|
251
|
-
const { text, tokenUsage } = await this.runAgent(instruction, userMessage, options.agents, options.onProgress, options.libraryTools, options.observer, options.parentSpanId, options.sessionId);
|
|
252
|
-
this.lastMemoryRef = this.nextMemoryRef();
|
|
253
|
-
this.lastTokenUsage = tokenUsage;
|
|
254
|
-
return text;
|
|
255
|
-
}
|
|
256
|
-
async sendExecution(request) {
|
|
257
|
-
this.lastMemoryRef = null;
|
|
258
|
-
this.lastTokenUsage = null;
|
|
259
|
-
const split = request.splitPrompt;
|
|
260
|
-
let instruction = "";
|
|
261
|
-
let userMessage;
|
|
262
|
-
if (split && this.cacheConfig.enabled !== false) {
|
|
263
|
-
instruction = split.system.join("\n\n---\n\n");
|
|
264
|
-
userMessage = split.user;
|
|
265
|
-
} else {
|
|
266
|
-
userMessage = request.prompt;
|
|
267
|
-
}
|
|
268
|
-
const agents = request.options.agents ?? request.agents;
|
|
269
|
-
const { text, tokenUsage } = await this.runAgent(instruction, userMessage, agents, request.options.onProgress, request.options.libraryTools, request.options.observer, request.options.parentSpanId, request.options.sessionId);
|
|
270
|
-
this.lastMemoryRef = this.nextMemoryRef();
|
|
271
|
-
this.lastTokenUsage = tokenUsage;
|
|
272
|
-
return text;
|
|
273
|
-
}
|
|
274
|
-
getLastMemoryRef() {
|
|
275
|
-
return this.lastMemoryRef;
|
|
276
|
-
}
|
|
277
|
-
getLastTokenUsage() {
|
|
278
|
-
return this.lastTokenUsage;
|
|
279
|
-
}
|
|
280
|
-
isCompatible(compat) {
|
|
281
|
-
return compat.startsWith("google-adk@");
|
|
282
|
-
}
|
|
283
|
-
// -------------------------------------------------------------------------
|
|
284
|
-
// Test support
|
|
285
|
-
// -------------------------------------------------------------------------
|
|
286
|
-
/**
|
|
287
|
-
* Inject the resolved ADK SDK surface for testing.
|
|
288
|
-
* Bypasses the dynamic import of @google/adk and the API key requirement.
|
|
289
|
-
*/
|
|
290
|
-
static withSdk(sdk, config) {
|
|
291
|
-
const adapter = new _AdkSdkAdapter(config);
|
|
292
|
-
adapter.sdk = sdk;
|
|
293
|
-
return adapter;
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
function emitProgressFromAdkEvent(event, onProgress) {
|
|
297
|
-
const parts = event.content?.parts;
|
|
298
|
-
if (!parts) return;
|
|
299
|
-
for (const part of parts) {
|
|
300
|
-
if (part.functionCall?.name) {
|
|
301
|
-
onProgress({ type: "tool_use", tool_name: part.functionCall.name, input: part.functionCall.args });
|
|
302
|
-
} else if (part.functionResponse?.name) {
|
|
303
|
-
onProgress({ type: "tool_result", tool_name: part.functionResponse.name });
|
|
304
|
-
} else if (part.text) {
|
|
305
|
-
onProgress({ type: "text", message: part.text });
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
export {
|
|
310
|
-
AdkSdkAdapter
|
|
311
|
-
};
|
|
312
|
-
//# sourceMappingURL=adk-sdk.js.map
|