@ziggs-ai/agent-sdk 0.1.3 → 0.1.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 +3 -1
- package/package.json +9 -4
- package/src/AgentHost.ts +495 -0
- package/src/adapters/OpenAIAdapter.ts +146 -0
- package/src/agent/Agent.ts +101 -0
- package/src/cognition/validateContext.ts +95 -0
- package/src/context/applyEffects.ts +80 -0
- package/src/context/batch.ts +17 -0
- package/src/context/classifyEnvelope.ts +38 -0
- package/src/context/routingLabels.ts +46 -0
- package/src/defineAgent.ts +62 -0
- package/src/formatters/AgreementFormatter.ts +111 -0
- package/src/formatters/HistoryFormatter.ts +166 -0
- package/src/formatters/index.ts +2 -0
- package/src/index.ts +105 -0
- package/src/ingress/normalizeIncoming.ts +162 -0
- package/src/memory/MemoryStore.ts +104 -0
- package/src/pricing/fleetDefaults.ts +218 -0
- package/src/pricing/fleetEvalFree.ts +24 -0
- package/src/pricing/fleetFreeTierA.gen.ts +12 -0
- package/src/pricing/fleetTierByAgentId.gen.ts +1022 -0
- package/src/runtime/AgentMachine.ts +364 -0
- package/src/runtime/PromptBuilder.ts +463 -0
- package/src/runtime/buildOutcome.ts +518 -0
- package/src/runtime/defaults.ts +75 -0
- package/src/runtime/runTurn.ts +691 -0
- package/src/runtime/validateWorkflow.ts +181 -0
- package/src/server/ConnectionPool.ts +155 -0
- package/src/server/EventQueue.ts +133 -0
- package/src/server/InboxCatchUp.ts +251 -0
- package/src/server/OutboxBuffer.ts +90 -0
- package/src/server/SeenMessages.ts +27 -0
- package/src/server/ZiggsEffectHandler.ts +409 -0
- package/src/server/agreements/AgreementService.ts +117 -0
- package/src/server/createHealthServer.ts +85 -0
- package/src/server/proactive/ProactiveTrigger.ts +83 -0
- package/src/server/runLauncher.ts +146 -0
- package/src/server/tasks/TaskService.ts +110 -0
- package/src/server/tasks/index.ts +1 -0
- package/src/server/telemetryIngest.ts +91 -0
- package/src/server/tools/index.ts +46 -0
- package/src/server/tools/tier1/protocolRunner.ts +133 -0
- package/src/server/tools/tier1/protocolTools.ts +99 -0
- package/src/server/tools/tier2/connectionTools.ts +75 -0
- package/src/server/tools/tier2/contextTools.ts +74 -0
- package/src/server/tools/tier2/discoveryTools.ts +34 -0
- package/src/server/tools/tier2/marketplaceTools.ts +25 -0
- package/src/server/tools/tier2/paymentTools.ts +193 -0
- package/src/server/ziggsconnect/ZiggsConnectClient.ts +126 -0
- package/src/server/ziggscontext/ZiggsContextClient.ts +137 -0
- package/src/server/ziggspay/ZiggsPayClient.ts +193 -0
- package/src/shared/ids.ts +3 -0
- package/src/shared/runtimeLog.ts +72 -0
- package/src/shared/types.ts +29 -0
- package/src/tasks/protocolRegistry.ts +25 -0
- package/src/tools/ToolManager.ts +95 -0
- package/src/tools/{ToolProvider.js → ToolProvider.ts} +5 -15
- package/src/tools/defineTool.ts +90 -0
- package/src/tools/index.ts +5 -0
- package/src/types.ts +407 -0
- package/src/utils/jsonExtractor.ts +100 -0
- package/src/ConnectionPool.js +0 -133
- package/src/adapters/OpenAIAdapter.js +0 -73
- package/src/agent/Agent.js +0 -121
- package/src/agent/EventQueue.js +0 -68
- package/src/agent/OutboxBuffer.js +0 -62
- package/src/cognition/PromptBuilder.js +0 -312
- package/src/cognition/resolveActionTool.js +0 -12
- package/src/cognition/runTurn.js +0 -578
- package/src/context/applyEffects.js +0 -133
- package/src/context/batch.js +0 -25
- package/src/context/classifyEnvelope.js +0 -82
- package/src/context/routingLabels.js +0 -54
- package/src/createHealthServer.js +0 -28
- package/src/formatters/HistoryFormatter.js +0 -257
- package/src/formatters/TaskFormatter.js +0 -180
- package/src/formatters/index.js +0 -9
- package/src/index.js +0 -76
- package/src/ingress/normalizeIncoming.js +0 -70
- package/src/runLauncher.js +0 -159
- package/src/shared/ids.js +0 -7
- package/src/shared/types.js +0 -86
- package/src/tasks/TaskService.js +0 -247
- package/src/tasks/index.js +0 -9
- package/src/tasks/taskCore.js +0 -229
- package/src/tasks/taskProtocolRegistry.js +0 -22
- package/src/tasks/taskProtocolRunner.js +0 -107
- package/src/tasks/taskProtocolTools.js +0 -87
- package/src/tools/ToolManager.js +0 -79
- package/src/tools/defineTool.js +0 -82
- package/src/tools/index.js +0 -11
- package/src/utils/jsonExtractor.js +0 -139
- package/src/workflow/AgentMachine.js +0 -250
- package/src/workflow/WorkflowRuntime.js +0 -63
- package/src/workflow/dsl.js +0 -287
- package/src/workflow/motifs.js +0 -435
- package/src/ziggs/runtime.js +0 -192
- /package/src/adapters/{index.js → index.ts} +0 -0
package/README.md
CHANGED
|
@@ -62,7 +62,9 @@ Examples in the repo: `examples/agents/*.js` (e.g. coffee, expense, delivery age
|
|
|
62
62
|
- **`ZiggsAgent`**, **`createAgent`**, **`defineAgent`**
|
|
63
63
|
- **`Agent`**, **`AgentMachine`**, **`runTurn`**
|
|
64
64
|
- **Prompt / tools:** `PromptBuilder`, `ToolManager`, `defineTool`
|
|
65
|
-
- **
|
|
65
|
+
- **Tools come in two tiers — single home: `server/tools/`:**
|
|
66
|
+
- **Tier 1 — protocol grammar (`server/tools/tier1/`, `PROTOCOL_TOOLS`).** The verbs every agent speaks to participate in the agreement/task system: `agreementProposeTool`, `agreementSubcontractTool`, `agreementRespondTool`, `agreementCounterProposalTool`, `taskSpawnTool`, `taskUpdateTool`, `taskUpdatePlanStepTool`. **Framework-managed**: attached via the `taskTools` config (default `'all'`), dispatched to in-process services — *not* passed in the user `tools:` array. Publishing open work is part of this grammar: `agreement_propose({ proposedTo: "everyone" })` (there is no separate publish tool).
|
|
67
|
+
- **Tier 2 — capability bundles (`server/tools/tier2/`, opt-in, HTTP-backed).** Discrete capability domains an agent chooses to have. **Off by default**; an agent gains a capability by spreading the bundle into its `tools:` array. Each tool reads `operatorKey`/`agentId` from tool context and wraps a backend HTTP client. Bundles: `DISCOVERY_TOOLS` (`agentSearchTool`, `agentGetTool`), `MARKETPLACE_TOOLS` (`marketplaceViewTool` — read-only), `PAYMENT_TOOLS` (`paymentBalanceTool`, `paymentTransferTool`, …).
|
|
66
68
|
- **Adapters / utils:** `OpenAIAdapter`, JSON helpers, formatters
|
|
67
69
|
- **Re-exported** from `@ziggs-ai/api-client`: `WebSocketClient`, `ContextReader`, `ContextWriter`, URL helpers, etc.
|
|
68
70
|
|
package/package.json
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ziggs-ai/agent-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Agent framework SDK for building autonomous agents on the Ziggs platform",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "src/index.
|
|
6
|
+
"main": "src/index.ts",
|
|
7
7
|
"exports": {
|
|
8
|
-
".": "./src/index.
|
|
8
|
+
".": "./src/index.ts"
|
|
9
9
|
},
|
|
10
10
|
"engines": {
|
|
11
11
|
"node": ">=18"
|
|
12
12
|
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
15
|
+
"test": "node --experimental-test-module-mocks --import tsx/esm --test --test-reporter=spec test/*.test.ts",
|
|
16
|
+
"test:watch": "node --experimental-test-module-mocks --import tsx/esm --test --watch test/*.test.ts"
|
|
17
|
+
},
|
|
13
18
|
"dependencies": {
|
|
14
|
-
"@ziggs-ai/api-client": "^0.1.
|
|
19
|
+
"@ziggs-ai/api-client": "^0.1.9",
|
|
15
20
|
"ajv": "^8.17.1",
|
|
16
21
|
"dotenv": "^17.2.3",
|
|
17
22
|
"openai": "^4.0.0"
|
package/src/AgentHost.ts
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import type { AgentConfig, Ctx, EffectHandler, Identity, Workflow } from './types.js';
|
|
2
|
+
import { Agent } from './agent/Agent.js';
|
|
3
|
+
import { EventQueue } from './server/EventQueue.js';
|
|
4
|
+
import { createZiggsEffectHandler } from './server/ZiggsEffectHandler.js';
|
|
5
|
+
import { normalizeIncomingEvent, type NormalizedEvent } from './ingress/normalizeIncoming.js';
|
|
6
|
+
import { PromptBuilder } from './runtime/PromptBuilder.js';
|
|
7
|
+
import { InMemoryStore, type MemoryStore } from './memory/MemoryStore.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Operator-provided FSM session persistence.
|
|
11
|
+
*
|
|
12
|
+
* AgentHost calls load() before each tick when a lane has no in-memory state
|
|
13
|
+
* (e.g. after a container restart) and save() after every successful tick.
|
|
14
|
+
* A null return from load() means "no prior state — start fresh."
|
|
15
|
+
*/
|
|
16
|
+
export interface SessionStore {
|
|
17
|
+
load(laneKey: string): Promise<{ ctx?: Ctx; state?: string } | null | undefined>;
|
|
18
|
+
save(laneKey: string, session: { ctx?: Ctx; state?: string }): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
import { ToolManager } from './tools/ToolManager.js';
|
|
21
|
+
import type { ToolDefinition } from './tools/defineTool.js';
|
|
22
|
+
import { PROTOCOL_TOOLS } from './server/tools/tier1/protocolTools.js';
|
|
23
|
+
import { OpenAIAdapter } from './adapters/OpenAIAdapter.js';
|
|
24
|
+
import {
|
|
25
|
+
WebSocketClient,
|
|
26
|
+
MessagesClient,
|
|
27
|
+
ArtifactsClient,
|
|
28
|
+
ScopeClient,
|
|
29
|
+
TelemetryClient,
|
|
30
|
+
type MessageMetadata,
|
|
31
|
+
} from '@ziggs-ai/api-client';
|
|
32
|
+
import { TaskService } from './server/tasks/TaskService.js';
|
|
33
|
+
import { AgreementService } from './server/agreements/AgreementService.js';
|
|
34
|
+
import { runInboxCatchUp } from './server/InboxCatchUp.js';
|
|
35
|
+
import { SeenMessages } from './server/SeenMessages.js';
|
|
36
|
+
import { runtimeLog } from './shared/runtimeLog.js';
|
|
37
|
+
|
|
38
|
+
// ConnectionPool lives in a separate module; resolved via a computed-path
|
|
39
|
+
// dynamic import to avoid a circular import with AgentHost.
|
|
40
|
+
async function loadConnectionPool(): Promise<new (opts?: { maxActive?: number; idleTimeoutMs?: number }) => unknown> {
|
|
41
|
+
const modPath = './server/ConnectionPool.js';
|
|
42
|
+
const mod = await import(/* @vite-ignore */ modPath) as { ConnectionPool: new (opts?: { maxActive?: number; idleTimeoutMs?: number }) => unknown };
|
|
43
|
+
return mod.ConnectionPool;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Given a normalized event, return the sessionId for this agent. Return
|
|
48
|
+
* `null` to skip processing. Lets each Agent define what "session" means —
|
|
49
|
+
* chat-scoped agents bucket by chatId (the default), task-scoped agents
|
|
50
|
+
* could bucket by taskId, cron agents by schedule id, etc.
|
|
51
|
+
*/
|
|
52
|
+
export type SessionResolver = (normalized: NormalizedEvent, ownAgentId: string) => string | null;
|
|
53
|
+
|
|
54
|
+
const DEFAULT_SESSION_RESOLVER: SessionResolver = (normalized) => normalized.chatId;
|
|
55
|
+
|
|
56
|
+
interface LlmAdapter {
|
|
57
|
+
chatMessages(messages: unknown[], tools?: unknown[], options?: Record<string, unknown>): Promise<unknown>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface AgentServices {
|
|
61
|
+
tools?: ToolDefinition[];
|
|
62
|
+
llm?: { model?: string } | LlmAdapter;
|
|
63
|
+
[key: string]: unknown;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AgentHostOptions {
|
|
67
|
+
openaiKey?: string;
|
|
68
|
+
model?: string;
|
|
69
|
+
operatorKey?: string;
|
|
70
|
+
agentId?: string;
|
|
71
|
+
name?: string | null;
|
|
72
|
+
wsUrl?: string;
|
|
73
|
+
description?: string;
|
|
74
|
+
specialization?: string | null;
|
|
75
|
+
tools?: ToolDefinition[];
|
|
76
|
+
taskTools?: 'all' | 'none' | string[];
|
|
77
|
+
services?: AgentServices;
|
|
78
|
+
workflow?: Workflow;
|
|
79
|
+
// Convenience: states + initial without an explicit workflow.
|
|
80
|
+
states?: Workflow['states'];
|
|
81
|
+
initial?: string;
|
|
82
|
+
tickTimeoutMs?: number;
|
|
83
|
+
/** Per-agent session keying. Defaults to `(n) => n.chatId`. */
|
|
84
|
+
sessionResolver?: SessionResolver;
|
|
85
|
+
/**
|
|
86
|
+
* Agent-private long-term memory. Operator-provided. Default: a fresh
|
|
87
|
+
* `InMemoryStore` (non-durable; lost on process restart). For durability,
|
|
88
|
+
* pass `new FileMemoryStore(path)` or your own implementation.
|
|
89
|
+
*/
|
|
90
|
+
memory?: MemoryStore;
|
|
91
|
+
/**
|
|
92
|
+
* FSM session persistence. Operator-provided. When set, AgentHost loads
|
|
93
|
+
* prior state before each tick (cold-start recovery) and saves after every
|
|
94
|
+
* successful tick. Omit to keep state in-process only (default behaviour).
|
|
95
|
+
*/
|
|
96
|
+
sessionStore?: SessionStore;
|
|
97
|
+
/** Observability tap: called after every effect with the effect and its response. */
|
|
98
|
+
effectTap?: (effect: import('./types.js').Effect, response: unknown) => void;
|
|
99
|
+
/** Observability tap: called on every FSM state transition, no-match, depth-guard, or error-escape (ZIG-442). */
|
|
100
|
+
transitionTap?: (event: import('./runtime/AgentMachine.js').TransitionEvent) => void;
|
|
101
|
+
/** Production monitoring ingest (ZIG-360). Default on; set false to disable. */
|
|
102
|
+
telemetryIngest?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Pull inbox on every WS connect/reconnect (ZIG-454). Default true.
|
|
105
|
+
* Recovers messages lost when backend in-memory pending queues are dropped.
|
|
106
|
+
*/
|
|
107
|
+
inboxCatchUp?: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function createToolManager(options: AgentHostOptions): ToolManager {
|
|
111
|
+
const toolManager = new ToolManager();
|
|
112
|
+
const userTools = options.tools || options.services?.tools || [];
|
|
113
|
+
let taskTools: ToolDefinition[];
|
|
114
|
+
if (options.taskTools === 'none') taskTools = [];
|
|
115
|
+
else if (Array.isArray(options.taskTools)) taskTools = PROTOCOL_TOOLS.filter((t: ToolDefinition) => (options.taskTools as string[]).includes(t.schema.function.name as string));
|
|
116
|
+
else taskTools = PROTOCOL_TOOLS; // 'all' or undefined
|
|
117
|
+
toolManager.registerAll([...taskTools, ...(Array.isArray(userTools) ? userTools : [])]);
|
|
118
|
+
return toolManager;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function isLlmAdapter(v: unknown): v is LlmAdapter {
|
|
122
|
+
return typeof v === 'object' && v !== null && typeof (v as LlmAdapter).chatMessages === 'function';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function createBackendServices(options: AgentHostOptions) {
|
|
126
|
+
const toolManager = createToolManager(options);
|
|
127
|
+
const llm = isLlmAdapter(options.services?.llm)
|
|
128
|
+
? options.services!.llm as LlmAdapter
|
|
129
|
+
: new OpenAIAdapter({ key: options.openaiKey, model: (options.services?.llm as { model?: string } | undefined)?.model || options.model });
|
|
130
|
+
const operatorKey = options.operatorKey!;
|
|
131
|
+
const agentId = options.agentId!;
|
|
132
|
+
const messagesClient = new MessagesClient(operatorKey, agentId);
|
|
133
|
+
const artifactsClient = new ArtifactsClient(operatorKey, agentId);
|
|
134
|
+
const scopeClient = new ScopeClient(operatorKey, agentId);
|
|
135
|
+
const promptBuilder = new PromptBuilder({ description: options.description, specialization: options.specialization });
|
|
136
|
+
const taskService = new TaskService(operatorKey, agentId);
|
|
137
|
+
const agreementService = new AgreementService(operatorKey, agentId);
|
|
138
|
+
const telemetryClient = new TelemetryClient(operatorKey, agentId);
|
|
139
|
+
return {
|
|
140
|
+
toolManager,
|
|
141
|
+
llm,
|
|
142
|
+
messagesClient,
|
|
143
|
+
artifactsClient,
|
|
144
|
+
scopeClient,
|
|
145
|
+
promptBuilder,
|
|
146
|
+
taskService,
|
|
147
|
+
agreementService,
|
|
148
|
+
telemetryClient,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
interface LaneSession {
|
|
153
|
+
sessionId: string;
|
|
154
|
+
ownAgentId: string;
|
|
155
|
+
ctx?: Ctx;
|
|
156
|
+
state?: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* AgentHost — the Server. Owns a pure {@link Agent}, an {@link EffectHandler},
|
|
161
|
+
* a transport (Ziggs WebSocket), and the lane router (per-session FIFO queue
|
|
162
|
+
* + in-memory session state).
|
|
163
|
+
*
|
|
164
|
+
* transport (WS) → handleMessage → agent.tick(... , eff) → ...
|
|
165
|
+
*/
|
|
166
|
+
export class AgentHost {
|
|
167
|
+
options: Required<Pick<AgentHostOptions, 'agentId' | 'operatorKey' | 'openaiKey'>> & AgentHostOptions;
|
|
168
|
+
toolManager!: ToolManager;
|
|
169
|
+
llm!: OpenAIAdapter | LlmAdapter; // retained for external inspection
|
|
170
|
+
agent!: Agent;
|
|
171
|
+
wsClient!: WebSocketClient;
|
|
172
|
+
private eff!: EffectHandler;
|
|
173
|
+
private eventQueue!: EventQueue;
|
|
174
|
+
private sessions = new Map<string, LaneSession>();
|
|
175
|
+
private sessionResolver: SessionResolver;
|
|
176
|
+
private tickTimeoutMs: number;
|
|
177
|
+
private memory: MemoryStore;
|
|
178
|
+
private sessionStore: SessionStore | null;
|
|
179
|
+
private taskService!: TaskService;
|
|
180
|
+
private agreementService!: AgreementService;
|
|
181
|
+
private messageSender!: (message: string, receiverId: string, sessionId: string) => Promise<unknown>;
|
|
182
|
+
private seenMessages = new SeenMessages();
|
|
183
|
+
private catchUpInFlight: Promise<void> | null = null;
|
|
184
|
+
private inboxCatchUpEnabled: boolean;
|
|
185
|
+
|
|
186
|
+
constructor(options: AgentHostOptions = {}) {
|
|
187
|
+
this.options = {
|
|
188
|
+
openaiKey: options.openaiKey || process.env.OPENAI_API_KEY,
|
|
189
|
+
model: options.model || (!isLlmAdapter(options.services?.llm) ? options.services?.llm?.model : undefined) || 'gpt-5.4',
|
|
190
|
+
operatorKey: options.operatorKey || process.env.ZIGGS_OPERATOR_KEY || undefined,
|
|
191
|
+
agentId: options.agentId || '',
|
|
192
|
+
name: options.name || null,
|
|
193
|
+
wsUrl: options.wsUrl,
|
|
194
|
+
description: options.description || 'A helpful assistant',
|
|
195
|
+
specialization: options.specialization ?? null,
|
|
196
|
+
tools: options.tools || [],
|
|
197
|
+
taskTools: options.taskTools,
|
|
198
|
+
states: options.states,
|
|
199
|
+
initial: options.initial,
|
|
200
|
+
services: options.services,
|
|
201
|
+
workflow: options.workflow,
|
|
202
|
+
tickTimeoutMs: options.tickTimeoutMs,
|
|
203
|
+
sessionResolver: options.sessionResolver,
|
|
204
|
+
effectTap: options.effectTap,
|
|
205
|
+
transitionTap: options.transitionTap,
|
|
206
|
+
inboxCatchUp: options.inboxCatchUp,
|
|
207
|
+
} as Required<Pick<AgentHostOptions, 'agentId' | 'operatorKey' | 'openaiKey'>> & AgentHostOptions;
|
|
208
|
+
this.inboxCatchUpEnabled = options.inboxCatchUp !== false;
|
|
209
|
+
this.sessionResolver = options.sessionResolver || DEFAULT_SESSION_RESOLVER;
|
|
210
|
+
this.tickTimeoutMs = options.tickTimeoutMs ?? 120_000;
|
|
211
|
+
this.memory = options.memory ?? new InMemoryStore();
|
|
212
|
+
this.sessionStore = options.sessionStore ?? null;
|
|
213
|
+
this._validate();
|
|
214
|
+
this._setup();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private _validate(): void {
|
|
218
|
+
if (!this.options.openaiKey) throw new Error('openaiKey is required');
|
|
219
|
+
if (!this.options.operatorKey) throw new Error('operatorKey is required — pass { operatorKey } or set ZIGGS_OPERATOR_KEY');
|
|
220
|
+
if (!this.options.agentId) throw new Error('agentId is required');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private _setup(): void {
|
|
224
|
+
const services = createBackendServices(this.options);
|
|
225
|
+
this.toolManager = services.toolManager;
|
|
226
|
+
this.llm = services.llm;
|
|
227
|
+
|
|
228
|
+
this.wsClient = new WebSocketClient({
|
|
229
|
+
wsUrl: this.options.wsUrl,
|
|
230
|
+
operatorKey: this.options.operatorKey,
|
|
231
|
+
agentId: this.options.agentId,
|
|
232
|
+
label: this.options.name || 'agent',
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const messageSender = (message: string, receiverId: string, sessionId: string, messageId?: string): Promise<unknown> => {
|
|
236
|
+
this.wsClient.send(sessionId, receiverId, message, messageId ? { messageId } : undefined);
|
|
237
|
+
return Promise.resolve(undefined);
|
|
238
|
+
};
|
|
239
|
+
const chunkSender = (sessionId: string, receiverId: string, text: string, messageId: string): void => {
|
|
240
|
+
this.wsClient.send(sessionId, receiverId, text, { partial: true, messageId });
|
|
241
|
+
};
|
|
242
|
+
this.taskService = services.taskService;
|
|
243
|
+
this.agreementService = services.agreementService;
|
|
244
|
+
this.messageSender = messageSender;
|
|
245
|
+
|
|
246
|
+
let workflow = this.options.workflow;
|
|
247
|
+
if (!workflow && this.options.states) {
|
|
248
|
+
workflow = {
|
|
249
|
+
id: this.options.agentId,
|
|
250
|
+
description: this.options.description!,
|
|
251
|
+
initial: this.options.initial || 'idle',
|
|
252
|
+
states: this.options.states,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
if (!workflow?.states) throw new Error('AgentHost: workflow (or states + initial) is required');
|
|
256
|
+
|
|
257
|
+
this.eff = createZiggsEffectHandler({
|
|
258
|
+
llm: services.llm as unknown as import('./server/ZiggsEffectHandler.js').ZiggsEffectDeps['llm'],
|
|
259
|
+
messagesClient: services.messagesClient,
|
|
260
|
+
artifactsClient: services.artifactsClient,
|
|
261
|
+
scopeClient: services.scopeClient,
|
|
262
|
+
taskService: services.taskService,
|
|
263
|
+
agreementService: services.agreementService,
|
|
264
|
+
messageSender,
|
|
265
|
+
chunkSender,
|
|
266
|
+
toolManager: services.toolManager,
|
|
267
|
+
operatorKey: this.options.operatorKey!,
|
|
268
|
+
agentId: this.options.agentId!,
|
|
269
|
+
telemetryClient: services.telemetryClient,
|
|
270
|
+
telemetryIngest: this.options.telemetryIngest,
|
|
271
|
+
effectTap: this.options.effectTap,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
this.agent = new Agent({
|
|
275
|
+
workflow,
|
|
276
|
+
toolManager: services.toolManager,
|
|
277
|
+
promptBuilder: services.promptBuilder,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
this.eventQueue = new EventQueue((event, laneKey) => this._processEvent(event, laneKey));
|
|
281
|
+
|
|
282
|
+
this.wsClient.setMessageHandler((text: string, metadata: MessageMetadata) => this.handleMessage(text, metadata as unknown as Record<string, unknown>));
|
|
283
|
+
this.wsClient.setResourceEventHandler((event) => this.handleResourceEvent(event as unknown as Record<string, unknown>));
|
|
284
|
+
|
|
285
|
+
if (this.inboxCatchUpEnabled) {
|
|
286
|
+
this.wsClient.onConnect(() => {
|
|
287
|
+
void this._ensureCatchUp();
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Coalesce concurrent connect/reconnect bursts into one catch-up pass.
|
|
294
|
+
*/
|
|
295
|
+
private _ensureCatchUp(): Promise<void> {
|
|
296
|
+
if (!this.catchUpInFlight) {
|
|
297
|
+
this.catchUpInFlight = this._runInboxCatchUp().finally(() => {
|
|
298
|
+
this.catchUpInFlight = null;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
return this.catchUpInFlight;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private async _runInboxCatchUp(): Promise<void> {
|
|
305
|
+
const label = this.options.name || 'agent';
|
|
306
|
+
try {
|
|
307
|
+
await runInboxCatchUp({
|
|
308
|
+
creds: {
|
|
309
|
+
operatorKey: this.options.operatorKey!,
|
|
310
|
+
agentId: this.options.agentId!,
|
|
311
|
+
},
|
|
312
|
+
ownAgentId: this.options.agentId!,
|
|
313
|
+
baseUrl: this.options.wsUrl ? undefined : undefined,
|
|
314
|
+
seen: this.seenMessages,
|
|
315
|
+
label,
|
|
316
|
+
onMessage: (text, metadata) => this.handleMessage(text, metadata),
|
|
317
|
+
onResourceEvent: (event) => this.handleResourceEvent(event),
|
|
318
|
+
});
|
|
319
|
+
} catch (err: unknown) {
|
|
320
|
+
runtimeLog.warn(
|
|
321
|
+
label,
|
|
322
|
+
`inbox catch-up failed: ${(err as Error)?.message ?? err}`,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Push notification handler — invoked when the backend sees a relevant
|
|
329
|
+
* resource change (artifact created, task state transition, agreement
|
|
330
|
+
* updated). The agent decides which session/lane to wake based on the
|
|
331
|
+
* event payload; it pulls the actual data via the primitive endpoints
|
|
332
|
+
* from inside the tick.
|
|
333
|
+
*
|
|
334
|
+
* Lightweight: defers all work to a tick by enqueueing a synthetic event
|
|
335
|
+
* carrying just enough context for the agent to route it.
|
|
336
|
+
*/
|
|
337
|
+
async handleResourceEvent(event: Record<string, unknown>): Promise<void> {
|
|
338
|
+
if (!event || typeof event !== 'object') return;
|
|
339
|
+
const ownAgentId = this.options.agentId;
|
|
340
|
+
if (!ownAgentId) return;
|
|
341
|
+
|
|
342
|
+
// Map the event to a candidate session key using the configured resolver
|
|
343
|
+
// on a synthetic normalized-event shape. Default resolver buckets by
|
|
344
|
+
// chatId; agents using a different sessionResolver (agreementId, taskId,
|
|
345
|
+
// counterpartyId) will get the right lane here as long as the wire
|
|
346
|
+
// event carries the corresponding id.
|
|
347
|
+
const fakeNormalized = {
|
|
348
|
+
chatId: event.chatId as string | null,
|
|
349
|
+
event: { type: 'message' as const, timestamp: Date.now(), text: '' },
|
|
350
|
+
shouldProcess: true,
|
|
351
|
+
reason: null,
|
|
352
|
+
} as NormalizedEvent;
|
|
353
|
+
const sessionId = this.sessionResolver(fakeNormalized, ownAgentId);
|
|
354
|
+
if (!sessionId) return;
|
|
355
|
+
|
|
356
|
+
const laneKey = `${ownAgentId}:${sessionId}`;
|
|
357
|
+
if (!this.sessions.has(laneKey)) {
|
|
358
|
+
this.sessions.set(laneKey, { sessionId, ownAgentId });
|
|
359
|
+
}
|
|
360
|
+
return this.eventQueue.enqueue({ type: 'resource_changed', resource: event, chatId: event.chatId, taskId: event.taskId, agreementId: event.agreementId }, laneKey);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Wire entry. Normalize → resolve session → enqueue on the right lane. */
|
|
364
|
+
async handleMessage(text: string, metadata: Record<string, unknown> = {}): Promise<void> {
|
|
365
|
+
const messageId = metadata.messageId as string | undefined;
|
|
366
|
+
if (messageId && this.seenMessages.has(messageId)) {
|
|
367
|
+
runtimeLog.debug('AgentHost', `skip duplicate messageId=${messageId}`);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (messageId) this.seenMessages.mark(messageId);
|
|
371
|
+
|
|
372
|
+
const ownAgentId = this._resolveOwnAgentId(metadata);
|
|
373
|
+
if (!ownAgentId) {
|
|
374
|
+
runtimeLog.warn('AgentHost', 'handleMessage: ownAgentId missing — dropping event');
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let enrichedText = typeof text === 'string' ? text : '';
|
|
379
|
+
if (!enrichedText.trim()) {
|
|
380
|
+
const agreement = metadata.agreement as Record<string, unknown> | undefined;
|
|
381
|
+
const terms = agreement?.terms as { description?: string } | undefined;
|
|
382
|
+
if (terms?.description) {
|
|
383
|
+
enrichedText = String(terms.description);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const normalized = normalizeIncomingEvent({ text: enrichedText, metadata, ownAgentId });
|
|
388
|
+
if (!normalized.shouldProcess) {
|
|
389
|
+
const receiverId = (metadata.receiver as Record<string, unknown> | undefined)?.id ?? metadata.receiverId;
|
|
390
|
+
runtimeLog.warn('AgentHost', `skip reason=${normalized.reason || 'not_relevant'} ownAgentId=${ownAgentId} receiverId=${receiverId ?? 'null'} chatId=${normalized.chatId ?? 'null'}`);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const sessionId = this.sessionResolver(normalized, ownAgentId);
|
|
395
|
+
if (!sessionId) {
|
|
396
|
+
runtimeLog.warn('AgentHost', `session resolver returned null ownAgentId=${ownAgentId} chatId=${normalized.chatId ?? 'null'}`);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const laneKey = `${ownAgentId}:${sessionId}`;
|
|
401
|
+
if (!this.sessions.has(laneKey)) {
|
|
402
|
+
this.sessions.set(laneKey, { sessionId, ownAgentId });
|
|
403
|
+
}
|
|
404
|
+
return this.eventQueue.enqueue(normalized.event, laneKey);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private async _processEvent(event: unknown, laneKey: string): Promise<void> {
|
|
408
|
+
const session = this.sessions.get(laneKey);
|
|
409
|
+
if (!session?.sessionId) return;
|
|
410
|
+
|
|
411
|
+
// Cold-start recovery: restore FSM state from sessionStore when the
|
|
412
|
+
// in-memory lane has no prior context (e.g. after a container restart).
|
|
413
|
+
if (this.sessionStore && session.ctx === undefined && session.state === undefined) {
|
|
414
|
+
try {
|
|
415
|
+
const persisted = await this.sessionStore.load(laneKey);
|
|
416
|
+
if (persisted) {
|
|
417
|
+
session.ctx = persisted.ctx;
|
|
418
|
+
session.state = persisted.state;
|
|
419
|
+
}
|
|
420
|
+
} catch (err: unknown) {
|
|
421
|
+
runtimeLog.warn('AgentHost', `sessionStore.load failed laneKey=${laneKey}: ${(err as Error)?.message || err}`);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const identity: Identity = {
|
|
426
|
+
agentId: session.ownAgentId || this.options.agentId,
|
|
427
|
+
sessionId: session.sessionId,
|
|
428
|
+
laneKey,
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
try {
|
|
432
|
+
const result = await Promise.race([
|
|
433
|
+
this.agent.tick({ event, ctx: session.ctx, state: session.state, identity, eff: this.eff, memory: this.memory, transitionTap: this.options.transitionTap }),
|
|
434
|
+
new Promise<never>((_, reject) =>
|
|
435
|
+
setTimeout(() => reject(new Error('tick timeout')), this.tickTimeoutMs),
|
|
436
|
+
),
|
|
437
|
+
]);
|
|
438
|
+
session.ctx = result.ctx;
|
|
439
|
+
session.state = result.state;
|
|
440
|
+
this.sessions.set(laneKey, session);
|
|
441
|
+
|
|
442
|
+
if (this.sessionStore) {
|
|
443
|
+
this.sessionStore.save(laneKey, { ctx: session.ctx, state: session.state }).catch((err: unknown) => {
|
|
444
|
+
runtimeLog.warn('AgentHost', `sessionStore.save failed laneKey=${laneKey}: ${(err as Error)?.message || err}`);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
} catch (err: unknown) {
|
|
448
|
+
runtimeLog.warn('AgentHost', `tick failed laneKey=${laneKey}: ${(err as Error)?.message || err}`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private _resolveOwnAgentId(metadata: Record<string, unknown> = {}): string | null {
|
|
453
|
+
const self = metadata.self as Record<string, unknown> | undefined;
|
|
454
|
+
const agent = metadata.agent as Record<string, unknown> | undefined;
|
|
455
|
+
return this.options.agentId || (self?.id as string) || (agent?.id as string) || (metadata.ziggsAgentId as string) || null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async connectAsync(timeout?: number) {
|
|
459
|
+
await this.wsClient.connectAsync(timeout);
|
|
460
|
+
if (this.inboxCatchUpEnabled) {
|
|
461
|
+
await this._ensureCatchUp();
|
|
462
|
+
// Backend flushPendingMessages can emit after auth; re-poll once so inbox
|
|
463
|
+
// ack catches stragglers and we don't leave newMessages > 0 (ZIG-462).
|
|
464
|
+
await new Promise((r) => setTimeout(r, 750));
|
|
465
|
+
this.catchUpInFlight = null;
|
|
466
|
+
await this._ensureCatchUp();
|
|
467
|
+
}
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
disconnect() { this.wsClient.disconnect(); }
|
|
471
|
+
/** Waits for all in-flight ticks to complete. Call before process.exit() on SIGTERM. */
|
|
472
|
+
drain(timeoutMs = 30_000): Promise<void> { return this.eventQueue.waitForIdle(timeoutMs); }
|
|
473
|
+
registerTool(tool: ToolDefinition) { return this.toolManager.register(tool); }
|
|
474
|
+
isConnected() { return this.wsClient.isConnected(); }
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* createAgent(config) → AgentHost
|
|
479
|
+
* createAgentPool([cfg, ...]) → ConnectionPool (lazy fleet, async-loaded)
|
|
480
|
+
*/
|
|
481
|
+
export function createAgent(options: AgentHostOptions | AgentConfig = {}): AgentHost {
|
|
482
|
+
return new AgentHost(options as AgentHostOptions);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export async function createAgentPool(
|
|
486
|
+
configs: AgentHostOptions[] | AgentConfig[],
|
|
487
|
+
opts: { poolOptions?: { maxActive?: number; idleTimeoutMs?: number } } = {},
|
|
488
|
+
): Promise<unknown> {
|
|
489
|
+
const ConnectionPool = await loadConnectionPool();
|
|
490
|
+
const pool = new ConnectionPool(opts.poolOptions);
|
|
491
|
+
(pool as { register: (configs: unknown[]) => void }).register(configs);
|
|
492
|
+
return pool;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export default AgentHost;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import type { ToolDefinition } from '../tools/defineTool.js';
|
|
3
|
+
import { runtimeLog } from '../shared/runtimeLog.js';
|
|
4
|
+
|
|
5
|
+
interface ChatResult {
|
|
6
|
+
content: string | null;
|
|
7
|
+
tool_calls: OpenAI.Chat.ChatCompletionMessageToolCall[] | null;
|
|
8
|
+
message: OpenAI.Chat.ChatCompletionMessage;
|
|
9
|
+
usage: OpenAI.CompletionUsage | undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface AdapterOptions {
|
|
13
|
+
key?: string;
|
|
14
|
+
model?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class OpenAIAdapter {
|
|
18
|
+
private client: OpenAI;
|
|
19
|
+
model: string;
|
|
20
|
+
|
|
21
|
+
constructor({ key, model = 'gpt-5.4' }: AdapterOptions) {
|
|
22
|
+
this.client = new OpenAI({ apiKey: key });
|
|
23
|
+
this.model = model;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async chat(
|
|
27
|
+
prompt: string,
|
|
28
|
+
tools: ToolDefinition[] = [],
|
|
29
|
+
temperature = 0.2,
|
|
30
|
+
systemHints: OpenAI.Chat.ChatCompletionMessageParam[] = [],
|
|
31
|
+
options: Record<string, unknown> = {},
|
|
32
|
+
): Promise<ChatResult> {
|
|
33
|
+
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
|
|
34
|
+
{ role: 'system', content: 'Respond with valid JSON.' },
|
|
35
|
+
...systemHints,
|
|
36
|
+
{ role: 'user', content: prompt },
|
|
37
|
+
];
|
|
38
|
+
return this.chatMessages(messages, tools, { ...options, temperature });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async chatMessages(
|
|
42
|
+
messages: OpenAI.Chat.ChatCompletionMessageParam[],
|
|
43
|
+
tools: ToolDefinition[] = [],
|
|
44
|
+
options: Record<string, unknown> = {},
|
|
45
|
+
): Promise<ChatResult> {
|
|
46
|
+
const openAITools: OpenAI.Chat.ChatCompletionTool[] | undefined = tools.length > 0
|
|
47
|
+
? tools.map(t => {
|
|
48
|
+
const fn = (t.schema.function || t.schema) as Record<string, unknown>;
|
|
49
|
+
const tAny = t as unknown as Record<string, unknown>;
|
|
50
|
+
const usageWhen = tAny['usage']
|
|
51
|
+
? ` Use when: ${(tAny['usage'] as Record<string, unknown>)['when']}`
|
|
52
|
+
: '';
|
|
53
|
+
return {
|
|
54
|
+
type: 'function' as const,
|
|
55
|
+
function: {
|
|
56
|
+
name: fn['name'] as string,
|
|
57
|
+
description: ((fn['description'] as string) || '') + usageWhen,
|
|
58
|
+
parameters: fn['parameters'] as Record<string, unknown>,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
})
|
|
62
|
+
: undefined;
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const { usage: _u, ...restOptions } = options as Record<string, unknown>;
|
|
66
|
+
// Verbose LLM request/response logging — commented out to reduce log noise.
|
|
67
|
+
// Re-enable by uncommenting to see full prompt/output blocks per LLM call.
|
|
68
|
+
// const divider = '─'.repeat(80);
|
|
69
|
+
// console.log(`\n${divider}`);
|
|
70
|
+
// console.log(`📤 LLM REQUEST → ${this.model} (${messages.length} msg${messages.length === 1 ? '' : 's'})`);
|
|
71
|
+
// console.log(divider);
|
|
72
|
+
// for (const m of messages) {
|
|
73
|
+
// const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content);
|
|
74
|
+
// const len = content?.length ?? 0;
|
|
75
|
+
// console.log(`\n[${m.role.toUpperCase()}] ${len} chars`);
|
|
76
|
+
// console.log(content ?? '');
|
|
77
|
+
// }
|
|
78
|
+
// console.log(`${divider}\n`);
|
|
79
|
+
const res = await this.client.chat.completions.create({
|
|
80
|
+
model: this.model,
|
|
81
|
+
messages,
|
|
82
|
+
stream: false,
|
|
83
|
+
max_completion_tokens: 3000,
|
|
84
|
+
response_format: openAITools ? undefined : ((restOptions['response_format'] as OpenAI.ResponseFormatJSONObject) ?? { type: 'json_object' }),
|
|
85
|
+
...(openAITools && { tools: openAITools }),
|
|
86
|
+
...restOptions,
|
|
87
|
+
} as OpenAI.Chat.ChatCompletionCreateParamsNonStreaming);
|
|
88
|
+
|
|
89
|
+
const msg = res.choices[0].message;
|
|
90
|
+
const content = msg.content?.trim() ?? null;
|
|
91
|
+
const toolCalls = msg.tool_calls?.length ? msg.tool_calls : null;
|
|
92
|
+
// Verbose LLM response logging — commented out to reduce log noise.
|
|
93
|
+
// console.log(`\n${divider}`);
|
|
94
|
+
// console.log(`📥 LLM RESPONSE ← ${this.model}`);
|
|
95
|
+
// console.log(divider);
|
|
96
|
+
// if (content) {
|
|
97
|
+
// console.log(`\n[CONTENT] ${content.length} chars`);
|
|
98
|
+
// console.log(content);
|
|
99
|
+
// }
|
|
100
|
+
// if (toolCalls) {
|
|
101
|
+
// console.log(`\n[TOOL CALLS] ${toolCalls.length}`);
|
|
102
|
+
// for (const tc of toolCalls) {
|
|
103
|
+
// console.log(` → ${tc.function.name}(${tc.function.arguments})`);
|
|
104
|
+
// }
|
|
105
|
+
// }
|
|
106
|
+
// if (!content && !toolCalls) console.log('\n(no content, no tool calls)');
|
|
107
|
+
// console.log(`${divider}\n`);
|
|
108
|
+
return {
|
|
109
|
+
content,
|
|
110
|
+
tool_calls: toolCalls,
|
|
111
|
+
message: msg,
|
|
112
|
+
usage: res.usage,
|
|
113
|
+
};
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const err = error as Error & { status?: number; code?: string };
|
|
116
|
+
runtimeLog.error('OpenAIAdapter', `LLM API call failed: ${err.message}`, {
|
|
117
|
+
model: this.model,
|
|
118
|
+
error: err.message,
|
|
119
|
+
status: err.status || 'unknown',
|
|
120
|
+
code: err.code || 'unknown',
|
|
121
|
+
});
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async chatMessagesStream(
|
|
127
|
+
messages: OpenAI.Chat.ChatCompletionMessageParam[],
|
|
128
|
+
onChunk: (text: string) => void,
|
|
129
|
+
): Promise<string> {
|
|
130
|
+
const stream = await this.client.chat.completions.create({
|
|
131
|
+
model: this.model,
|
|
132
|
+
messages,
|
|
133
|
+
stream: true,
|
|
134
|
+
max_completion_tokens: 3000,
|
|
135
|
+
});
|
|
136
|
+
let text = '';
|
|
137
|
+
for await (const chunk of stream) {
|
|
138
|
+
const delta = chunk.choices[0]?.delta?.content;
|
|
139
|
+
if (delta) {
|
|
140
|
+
text += delta;
|
|
141
|
+
onChunk(delta);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
146
|
+
}
|